Commit | Line | Data |
---|---|---|
a42d985b BVA |
1 | /* |
2 | * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved. | |
3 | * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>. | |
4 | * | |
5 | * This software is available to you under a choice of one of two | |
6 | * licenses. You may choose to be licensed under the terms of the GNU | |
7 | * General Public License (GPL) Version 2, available from the file | |
8 | * COPYING in the main directory of this source tree, or the | |
9 | * OpenIB.org BSD license below: | |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or | |
12 | * without modification, are permitted provided that the following | |
13 | * conditions are met: | |
14 | * | |
15 | * - Redistributions of source code must retain the above | |
16 | * copyright notice, this list of conditions and the following | |
17 | * disclaimer. | |
18 | * | |
19 | * - Redistributions in binary form must reproduce the above | |
20 | * copyright notice, this list of conditions and the following | |
21 | * disclaimer in the documentation and/or other materials | |
22 | * provided with the distribution. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
31 | * SOFTWARE. | |
32 | * | |
33 | */ | |
34 | ||
35 | #include <linux/module.h> | |
36 | #include <linux/init.h> | |
37 | #include <linux/slab.h> | |
38 | #include <linux/err.h> | |
39 | #include <linux/ctype.h> | |
40 | #include <linux/kthread.h> | |
41 | #include <linux/string.h> | |
42 | #include <linux/delay.h> | |
43 | #include <linux/atomic.h> | |
63cf1a90 | 44 | #include <linux/inet.h> |
c5efb621 | 45 | #include <rdma/ib_cache.h> |
ba929992 | 46 | #include <scsi/scsi_proto.h> |
a42d985b | 47 | #include <scsi/scsi_tcq.h> |
a42d985b | 48 | #include <target/target_core_base.h> |
a42d985b | 49 | #include <target/target_core_fabric.h> |
a42d985b BVA |
50 | #include "ib_srpt.h" |
51 | ||
52 | /* Name of this kernel module. */ | |
53 | #define DRV_NAME "ib_srpt" | |
a42d985b BVA |
54 | |
55 | #define SRPT_ID_STRING "Linux SRP target" | |
56 | ||
57 | #undef pr_fmt | |
58 | #define pr_fmt(fmt) DRV_NAME " " fmt | |
59 | ||
60 | MODULE_AUTHOR("Vu Pham and Bart Van Assche"); | |
6feb64ff | 61 | MODULE_DESCRIPTION("SCSI RDMA Protocol target driver"); |
a42d985b BVA |
62 | MODULE_LICENSE("Dual BSD/GPL"); |
63 | ||
64 | /* | |
65 | * Global Variables | |
66 | */ | |
67 | ||
68 | static u64 srpt_service_guid; | |
486d8b9f RD |
69 | static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */ |
70 | static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */ | |
a42d985b BVA |
71 | |
72 | static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE; | |
73 | module_param(srp_max_req_size, int, 0444); | |
74 | MODULE_PARM_DESC(srp_max_req_size, | |
75 | "Maximum size of SRP request messages in bytes."); | |
76 | ||
77 | static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE; | |
78 | module_param(srpt_srq_size, int, 0444); | |
79 | MODULE_PARM_DESC(srpt_srq_size, | |
80 | "Shared receive queue (SRQ) size."); | |
81 | ||
e4dca7b7 | 82 | static int srpt_get_u64_x(char *buffer, const struct kernel_param *kp) |
a42d985b BVA |
83 | { |
84 | return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg); | |
85 | } | |
86 | module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid, | |
87 | 0444); | |
88 | MODULE_PARM_DESC(srpt_service_guid, | |
75d79b80 | 89 | "Using this value for ioc_guid, id_ext, and cm_listen_id instead of using the node_guid of the first HCA."); |
a42d985b BVA |
90 | |
91 | static struct ib_client srpt_client; | |
63cf1a90 BVA |
92 | /* Protects both rdma_cm_port and rdma_cm_id. */ |
93 | static DEFINE_MUTEX(rdma_cm_mutex); | |
94 | /* Port number RDMA/CM will bind to. */ | |
95 | static u16 rdma_cm_port; | |
96 | static struct rdma_cm_id *rdma_cm_id; | |
2c7f37ff | 97 | static void srpt_release_cmd(struct se_cmd *se_cmd); |
aaf45bd8 | 98 | static void srpt_free_ch(struct kref *kref); |
a42d985b | 99 | static int srpt_queue_status(struct se_cmd *cmd); |
59fae4de CH |
100 | static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc); |
101 | static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc); | |
387add46 | 102 | static void srpt_process_wait_list(struct srpt_rdma_ch *ch); |
a42d985b | 103 | |
f130c220 BVA |
104 | /* |
105 | * The only allowed channel state changes are those that change the channel | |
106 | * state into a state with a higher numerical value. Hence the new > prev test. | |
a42d985b | 107 | */ |
f130c220 | 108 | static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new) |
a42d985b BVA |
109 | { |
110 | unsigned long flags; | |
111 | enum rdma_ch_state prev; | |
f130c220 | 112 | bool changed = false; |
a42d985b BVA |
113 | |
114 | spin_lock_irqsave(&ch->spinlock, flags); | |
115 | prev = ch->state; | |
f130c220 | 116 | if (new > prev) { |
a42d985b | 117 | ch->state = new; |
f130c220 BVA |
118 | changed = true; |
119 | } | |
a42d985b | 120 | spin_unlock_irqrestore(&ch->spinlock, flags); |
f130c220 BVA |
121 | |
122 | return changed; | |
a42d985b BVA |
123 | } |
124 | ||
125 | /** | |
10eac19b BVA |
126 | * srpt_event_handler - asynchronous IB event callback function |
127 | * @handler: IB event handler registered by ib_register_event_handler(). | |
128 | * @event: Description of the event that occurred. | |
a42d985b BVA |
129 | * |
130 | * Callback function called by the InfiniBand core when an asynchronous IB | |
131 | * event occurs. This callback may occur in interrupt context. See also | |
132 | * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand | |
133 | * Architecture Specification. | |
134 | */ | |
135 | static void srpt_event_handler(struct ib_event_handler *handler, | |
136 | struct ib_event *event) | |
137 | { | |
138 | struct srpt_device *sdev; | |
139 | struct srpt_port *sport; | |
5f1141b1 | 140 | u8 port_num; |
a42d985b BVA |
141 | |
142 | sdev = ib_get_client_data(event->device, &srpt_client); | |
143 | if (!sdev || sdev->device != event->device) | |
144 | return; | |
145 | ||
146 | pr_debug("ASYNC event= %d on device= %s\n", event->event, | |
6c854111 | 147 | dev_name(&sdev->device->dev)); |
a42d985b BVA |
148 | |
149 | switch (event->event) { | |
150 | case IB_EVENT_PORT_ERR: | |
5f1141b1 BVA |
151 | port_num = event->element.port_num - 1; |
152 | if (port_num < sdev->device->phys_port_cnt) { | |
153 | sport = &sdev->port[port_num]; | |
a42d985b BVA |
154 | sport->lid = 0; |
155 | sport->sm_lid = 0; | |
5f1141b1 BVA |
156 | } else { |
157 | WARN(true, "event %d: port_num %d out of range 1..%d\n", | |
158 | event->event, port_num + 1, | |
159 | sdev->device->phys_port_cnt); | |
a42d985b BVA |
160 | } |
161 | break; | |
162 | case IB_EVENT_PORT_ACTIVE: | |
163 | case IB_EVENT_LID_CHANGE: | |
164 | case IB_EVENT_PKEY_CHANGE: | |
165 | case IB_EVENT_SM_CHANGE: | |
166 | case IB_EVENT_CLIENT_REREGISTER: | |
2aa1cf64 | 167 | case IB_EVENT_GID_CHANGE: |
a42d985b | 168 | /* Refresh port data asynchronously. */ |
5f1141b1 BVA |
169 | port_num = event->element.port_num - 1; |
170 | if (port_num < sdev->device->phys_port_cnt) { | |
171 | sport = &sdev->port[port_num]; | |
a42d985b BVA |
172 | if (!sport->lid && !sport->sm_lid) |
173 | schedule_work(&sport->work); | |
5f1141b1 BVA |
174 | } else { |
175 | WARN(true, "event %d: port_num %d out of range 1..%d\n", | |
176 | event->event, port_num + 1, | |
177 | sdev->device->phys_port_cnt); | |
a42d985b BVA |
178 | } |
179 | break; | |
180 | default: | |
5f1141b1 | 181 | pr_err("received unrecognized IB event %d\n", event->event); |
a42d985b BVA |
182 | break; |
183 | } | |
184 | } | |
185 | ||
186 | /** | |
10eac19b BVA |
187 | * srpt_srq_event - SRQ event callback function |
188 | * @event: Description of the event that occurred. | |
189 | * @ctx: Context pointer specified at SRQ creation time. | |
a42d985b BVA |
190 | */ |
191 | static void srpt_srq_event(struct ib_event *event, void *ctx) | |
192 | { | |
d9f45ae6 | 193 | pr_debug("SRQ event %d\n", event->event); |
a42d985b BVA |
194 | } |
195 | ||
aaf45bd8 BVA |
196 | static const char *get_ch_state_name(enum rdma_ch_state s) |
197 | { | |
198 | switch (s) { | |
199 | case CH_CONNECTING: | |
200 | return "connecting"; | |
201 | case CH_LIVE: | |
202 | return "live"; | |
203 | case CH_DISCONNECTING: | |
204 | return "disconnecting"; | |
205 | case CH_DRAINING: | |
206 | return "draining"; | |
207 | case CH_DISCONNECTED: | |
208 | return "disconnected"; | |
209 | } | |
210 | return "???"; | |
211 | } | |
212 | ||
a42d985b | 213 | /** |
10eac19b BVA |
214 | * srpt_qp_event - QP event callback function |
215 | * @event: Description of the event that occurred. | |
216 | * @ch: SRPT RDMA channel. | |
a42d985b BVA |
217 | */ |
218 | static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch) | |
219 | { | |
090fa24b BVA |
220 | pr_debug("QP event %d on ch=%p sess_name=%s state=%d\n", |
221 | event->event, ch, ch->sess_name, ch->state); | |
a42d985b BVA |
222 | |
223 | switch (event->event) { | |
224 | case IB_EVENT_COMM_EST: | |
63cf1a90 BVA |
225 | if (ch->using_rdma_cm) |
226 | rdma_notify(ch->rdma_cm.cm_id, event->event); | |
227 | else | |
228 | ib_cm_notify(ch->ib_cm.cm_id, event->event); | |
a42d985b BVA |
229 | break; |
230 | case IB_EVENT_QP_LAST_WQE_REACHED: | |
aaf45bd8 BVA |
231 | pr_debug("%s-%d, state %s: received Last WQE event.\n", |
232 | ch->sess_name, ch->qp->qp_num, | |
233 | get_ch_state_name(ch->state)); | |
a42d985b BVA |
234 | break; |
235 | default: | |
9f5d32af | 236 | pr_err("received unrecognized IB QP event %d\n", event->event); |
a42d985b BVA |
237 | break; |
238 | } | |
239 | } | |
240 | ||
241 | /** | |
10eac19b BVA |
242 | * srpt_set_ioc - initialize a IOUnitInfo structure |
243 | * @c_list: controller list. | |
a42d985b BVA |
244 | * @slot: one-based slot number. |
245 | * @value: four-bit value. | |
246 | * | |
247 | * Copies the lowest four bits of value in element slot of the array of four | |
248 | * bit elements called c_list (controller list). The index slot is one-based. | |
249 | */ | |
250 | static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value) | |
251 | { | |
252 | u16 id; | |
253 | u8 tmp; | |
254 | ||
255 | id = (slot - 1) / 2; | |
256 | if (slot & 0x1) { | |
257 | tmp = c_list[id] & 0xf; | |
258 | c_list[id] = (value << 4) | tmp; | |
259 | } else { | |
260 | tmp = c_list[id] & 0xf0; | |
261 | c_list[id] = (value & 0xf) | tmp; | |
262 | } | |
263 | } | |
264 | ||
265 | /** | |
10eac19b BVA |
266 | * srpt_get_class_port_info - copy ClassPortInfo to a management datagram |
267 | * @mad: Datagram that will be sent as response to DM_ATTR_CLASS_PORT_INFO. | |
a42d985b BVA |
268 | * |
269 | * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture | |
270 | * Specification. | |
271 | */ | |
272 | static void srpt_get_class_port_info(struct ib_dm_mad *mad) | |
273 | { | |
274 | struct ib_class_port_info *cif; | |
275 | ||
276 | cif = (struct ib_class_port_info *)mad->data; | |
9d2aa2b4 | 277 | memset(cif, 0, sizeof(*cif)); |
a42d985b BVA |
278 | cif->base_version = 1; |
279 | cif->class_version = 1; | |
a42d985b | 280 | |
507f6afa | 281 | ib_set_cpi_resp_time(cif, 20); |
a42d985b BVA |
282 | mad->mad_hdr.status = 0; |
283 | } | |
284 | ||
285 | /** | |
10eac19b BVA |
286 | * srpt_get_iou - write IOUnitInfo to a management datagram |
287 | * @mad: Datagram that will be sent as response to DM_ATTR_IOU_INFO. | |
a42d985b BVA |
288 | * |
289 | * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture | |
290 | * Specification. See also section B.7, table B.6 in the SRP r16a document. | |
291 | */ | |
292 | static void srpt_get_iou(struct ib_dm_mad *mad) | |
293 | { | |
294 | struct ib_dm_iou_info *ioui; | |
295 | u8 slot; | |
296 | int i; | |
297 | ||
298 | ioui = (struct ib_dm_iou_info *)mad->data; | |
b356c1c1 | 299 | ioui->change_id = cpu_to_be16(1); |
a42d985b BVA |
300 | ioui->max_controllers = 16; |
301 | ||
302 | /* set present for slot 1 and empty for the rest */ | |
303 | srpt_set_ioc(ioui->controller_list, 1, 1); | |
304 | for (i = 1, slot = 2; i < 16; i++, slot++) | |
305 | srpt_set_ioc(ioui->controller_list, slot, 0); | |
306 | ||
307 | mad->mad_hdr.status = 0; | |
308 | } | |
309 | ||
310 | /** | |
10eac19b BVA |
311 | * srpt_get_ioc - write IOControllerprofile to a management datagram |
312 | * @sport: HCA port through which the MAD has been received. | |
313 | * @slot: Slot number specified in DM_ATTR_IOC_PROFILE query. | |
314 | * @mad: Datagram that will be sent as response to DM_ATTR_IOC_PROFILE. | |
a42d985b BVA |
315 | * |
316 | * See also section 16.3.3.4 IOControllerProfile in the InfiniBand | |
317 | * Architecture Specification. See also section B.7, table B.7 in the SRP | |
318 | * r16a document. | |
319 | */ | |
320 | static void srpt_get_ioc(struct srpt_port *sport, u32 slot, | |
321 | struct ib_dm_mad *mad) | |
322 | { | |
323 | struct srpt_device *sdev = sport->sdev; | |
324 | struct ib_dm_ioc_profile *iocp; | |
dea26209 | 325 | int send_queue_depth; |
a42d985b BVA |
326 | |
327 | iocp = (struct ib_dm_ioc_profile *)mad->data; | |
328 | ||
329 | if (!slot || slot > 16) { | |
330 | mad->mad_hdr.status | |
b356c1c1 | 331 | = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD); |
a42d985b BVA |
332 | return; |
333 | } | |
334 | ||
335 | if (slot > 2) { | |
336 | mad->mad_hdr.status | |
b356c1c1 | 337 | = cpu_to_be16(DM_MAD_STATUS_NO_IOC); |
a42d985b BVA |
338 | return; |
339 | } | |
340 | ||
dea26209 BVA |
341 | if (sdev->use_srq) |
342 | send_queue_depth = sdev->srq_size; | |
343 | else | |
ed262287 | 344 | send_queue_depth = min(MAX_SRPT_RQ_SIZE, |
dea26209 BVA |
345 | sdev->device->attrs.max_qp_wr); |
346 | ||
9d2aa2b4 | 347 | memset(iocp, 0, sizeof(*iocp)); |
a42d985b BVA |
348 | strcpy(iocp->id_string, SRPT_ID_STRING); |
349 | iocp->guid = cpu_to_be64(srpt_service_guid); | |
4a061b28 OG |
350 | iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id); |
351 | iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id); | |
352 | iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver); | |
353 | iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id); | |
a42d985b | 354 | iocp->subsys_device_id = 0x0; |
b356c1c1 VT |
355 | iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS); |
356 | iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS); | |
357 | iocp->protocol = cpu_to_be16(SRP_PROTOCOL); | |
358 | iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION); | |
dea26209 | 359 | iocp->send_queue_depth = cpu_to_be16(send_queue_depth); |
a42d985b BVA |
360 | iocp->rdma_read_depth = 4; |
361 | iocp->send_size = cpu_to_be32(srp_max_req_size); | |
362 | iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size, | |
363 | 1U << 24)); | |
364 | iocp->num_svc_entries = 1; | |
365 | iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC | | |
366 | SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC; | |
367 | ||
368 | mad->mad_hdr.status = 0; | |
369 | } | |
370 | ||
371 | /** | |
10eac19b BVA |
372 | * srpt_get_svc_entries - write ServiceEntries to a management datagram |
373 | * @ioc_guid: I/O controller GUID to use in reply. | |
374 | * @slot: I/O controller number. | |
375 | * @hi: End of the range of service entries to be specified in the reply. | |
376 | * @lo: Start of the range of service entries to be specified in the reply.. | |
377 | * @mad: Datagram that will be sent as response to DM_ATTR_SVC_ENTRIES. | |
a42d985b BVA |
378 | * |
379 | * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture | |
380 | * Specification. See also section B.7, table B.8 in the SRP r16a document. | |
381 | */ | |
382 | static void srpt_get_svc_entries(u64 ioc_guid, | |
383 | u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad) | |
384 | { | |
385 | struct ib_dm_svc_entries *svc_entries; | |
386 | ||
387 | WARN_ON(!ioc_guid); | |
388 | ||
389 | if (!slot || slot > 16) { | |
390 | mad->mad_hdr.status | |
b356c1c1 | 391 | = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD); |
a42d985b BVA |
392 | return; |
393 | } | |
394 | ||
395 | if (slot > 2 || lo > hi || hi > 1) { | |
396 | mad->mad_hdr.status | |
b356c1c1 | 397 | = cpu_to_be16(DM_MAD_STATUS_NO_IOC); |
a42d985b BVA |
398 | return; |
399 | } | |
400 | ||
401 | svc_entries = (struct ib_dm_svc_entries *)mad->data; | |
9d2aa2b4 | 402 | memset(svc_entries, 0, sizeof(*svc_entries)); |
a42d985b BVA |
403 | svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid); |
404 | snprintf(svc_entries->service_entries[0].name, | |
405 | sizeof(svc_entries->service_entries[0].name), | |
406 | "%s%016llx", | |
407 | SRP_SERVICE_NAME_PREFIX, | |
408 | ioc_guid); | |
409 | ||
410 | mad->mad_hdr.status = 0; | |
411 | } | |
412 | ||
413 | /** | |
10eac19b BVA |
414 | * srpt_mgmt_method_get - process a received management datagram |
415 | * @sp: HCA port through which the MAD has been received. | |
a42d985b BVA |
416 | * @rq_mad: received MAD. |
417 | * @rsp_mad: response MAD. | |
418 | */ | |
419 | static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad, | |
420 | struct ib_dm_mad *rsp_mad) | |
421 | { | |
422 | u16 attr_id; | |
423 | u32 slot; | |
424 | u8 hi, lo; | |
425 | ||
426 | attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id); | |
427 | switch (attr_id) { | |
428 | case DM_ATTR_CLASS_PORT_INFO: | |
429 | srpt_get_class_port_info(rsp_mad); | |
430 | break; | |
431 | case DM_ATTR_IOU_INFO: | |
432 | srpt_get_iou(rsp_mad); | |
433 | break; | |
434 | case DM_ATTR_IOC_PROFILE: | |
435 | slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod); | |
436 | srpt_get_ioc(sp, slot, rsp_mad); | |
437 | break; | |
438 | case DM_ATTR_SVC_ENTRIES: | |
439 | slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod); | |
440 | hi = (u8) ((slot >> 8) & 0xff); | |
441 | lo = (u8) (slot & 0xff); | |
442 | slot = (u16) ((slot >> 16) & 0xffff); | |
443 | srpt_get_svc_entries(srpt_service_guid, | |
444 | slot, hi, lo, rsp_mad); | |
445 | break; | |
446 | default: | |
447 | rsp_mad->mad_hdr.status = | |
b356c1c1 | 448 | cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR); |
a42d985b BVA |
449 | break; |
450 | } | |
451 | } | |
452 | ||
453 | /** | |
10eac19b BVA |
454 | * srpt_mad_send_handler - MAD send completion callback |
455 | * @mad_agent: Return value of ib_register_mad_agent(). | |
456 | * @mad_wc: Work completion reporting that the MAD has been sent. | |
a42d985b BVA |
457 | */ |
458 | static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent, | |
459 | struct ib_mad_send_wc *mad_wc) | |
460 | { | |
2553ba21 | 461 | rdma_destroy_ah(mad_wc->send_buf->ah, RDMA_DESTROY_AH_SLEEPABLE); |
a42d985b BVA |
462 | ib_free_send_mad(mad_wc->send_buf); |
463 | } | |
464 | ||
465 | /** | |
10eac19b BVA |
466 | * srpt_mad_recv_handler - MAD reception callback function |
467 | * @mad_agent: Return value of ib_register_mad_agent(). | |
468 | * @send_buf: Not used. | |
469 | * @mad_wc: Work completion reporting that a MAD has been received. | |
a42d985b BVA |
470 | */ |
471 | static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent, | |
ca281265 | 472 | struct ib_mad_send_buf *send_buf, |
a42d985b BVA |
473 | struct ib_mad_recv_wc *mad_wc) |
474 | { | |
475 | struct srpt_port *sport = (struct srpt_port *)mad_agent->context; | |
476 | struct ib_ah *ah; | |
477 | struct ib_mad_send_buf *rsp; | |
478 | struct ib_dm_mad *dm_mad; | |
479 | ||
480 | if (!mad_wc || !mad_wc->recv_buf.mad) | |
481 | return; | |
482 | ||
483 | ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc, | |
484 | mad_wc->recv_buf.grh, mad_agent->port_num); | |
485 | if (IS_ERR(ah)) | |
486 | goto err; | |
487 | ||
488 | BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR); | |
489 | ||
490 | rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp, | |
491 | mad_wc->wc->pkey_index, 0, | |
492 | IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA, | |
da2dfaa3 IW |
493 | GFP_KERNEL, |
494 | IB_MGMT_BASE_VERSION); | |
a42d985b BVA |
495 | if (IS_ERR(rsp)) |
496 | goto err_rsp; | |
497 | ||
498 | rsp->ah = ah; | |
499 | ||
500 | dm_mad = rsp->mad; | |
9d2aa2b4 | 501 | memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad)); |
a42d985b BVA |
502 | dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP; |
503 | dm_mad->mad_hdr.status = 0; | |
504 | ||
505 | switch (mad_wc->recv_buf.mad->mad_hdr.method) { | |
506 | case IB_MGMT_METHOD_GET: | |
507 | srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad); | |
508 | break; | |
509 | case IB_MGMT_METHOD_SET: | |
510 | dm_mad->mad_hdr.status = | |
b356c1c1 | 511 | cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR); |
a42d985b BVA |
512 | break; |
513 | default: | |
514 | dm_mad->mad_hdr.status = | |
b356c1c1 | 515 | cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD); |
a42d985b BVA |
516 | break; |
517 | } | |
518 | ||
519 | if (!ib_post_send_mad(rsp, NULL)) { | |
520 | ib_free_recv_mad(mad_wc); | |
521 | /* will destroy_ah & free_send_mad in send completion */ | |
522 | return; | |
523 | } | |
524 | ||
525 | ib_free_send_mad(rsp); | |
526 | ||
527 | err_rsp: | |
2553ba21 | 528 | rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE); |
a42d985b BVA |
529 | err: |
530 | ib_free_recv_mad(mad_wc); | |
531 | } | |
532 | ||
b14cb744 BVA |
533 | static int srpt_format_guid(char *buf, unsigned int size, const __be64 *guid) |
534 | { | |
535 | const __be16 *g = (const __be16 *)guid; | |
536 | ||
537 | return snprintf(buf, size, "%04x:%04x:%04x:%04x", | |
538 | be16_to_cpu(g[0]), be16_to_cpu(g[1]), | |
539 | be16_to_cpu(g[2]), be16_to_cpu(g[3])); | |
540 | } | |
541 | ||
a42d985b | 542 | /** |
10eac19b BVA |
543 | * srpt_refresh_port - configure a HCA port |
544 | * @sport: SRPT HCA port. | |
a42d985b BVA |
545 | * |
546 | * Enable InfiniBand management datagram processing, update the cached sm_lid, | |
547 | * lid and gid values, and register a callback function for processing MADs | |
548 | * on the specified port. | |
549 | * | |
550 | * Note: It is safe to call this function more than once for the same port. | |
551 | */ | |
552 | static int srpt_refresh_port(struct srpt_port *sport) | |
553 | { | |
554 | struct ib_mad_reg_req reg_req; | |
555 | struct ib_port_modify port_modify; | |
556 | struct ib_port_attr port_attr; | |
557 | int ret; | |
558 | ||
a42d985b BVA |
559 | ret = ib_query_port(sport->sdev->device, sport->port, &port_attr); |
560 | if (ret) | |
09f8a148 | 561 | return ret; |
a42d985b BVA |
562 | |
563 | sport->sm_lid = port_attr.sm_lid; | |
564 | sport->lid = port_attr.lid; | |
565 | ||
1dfce294 | 566 | ret = rdma_query_gid(sport->sdev->device, sport->port, 0, &sport->gid); |
a42d985b | 567 | if (ret) |
09f8a148 | 568 | return ret; |
a42d985b | 569 | |
2bce1a6d | 570 | sport->port_guid_wwn.priv = sport; |
b14cb744 BVA |
571 | srpt_format_guid(sport->port_guid, sizeof(sport->port_guid), |
572 | &sport->gid.global.interface_id); | |
2bce1a6d BVA |
573 | sport->port_gid_wwn.priv = sport; |
574 | snprintf(sport->port_gid, sizeof(sport->port_gid), | |
575 | "0x%016llx%016llx", | |
576 | be64_to_cpu(sport->gid.global.subnet_prefix), | |
577 | be64_to_cpu(sport->gid.global.interface_id)); | |
716b076b | 578 | |
09f8a148 BVA |
579 | if (rdma_protocol_iwarp(sport->sdev->device, sport->port)) |
580 | return 0; | |
581 | ||
582 | memset(&port_modify, 0, sizeof(port_modify)); | |
583 | port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP; | |
584 | port_modify.clr_port_cap_mask = 0; | |
585 | ||
586 | ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify); | |
587 | if (ret) { | |
588 | pr_warn("%s-%d: enabling device management failed (%d). Note: this is expected if SR-IOV is enabled.\n", | |
589 | dev_name(&sport->sdev->device->dev), sport->port, ret); | |
590 | return 0; | |
591 | } | |
592 | ||
a42d985b | 593 | if (!sport->mad_agent) { |
9d2aa2b4 | 594 | memset(®_req, 0, sizeof(reg_req)); |
a42d985b BVA |
595 | reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT; |
596 | reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION; | |
597 | set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask); | |
598 | set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask); | |
599 | ||
600 | sport->mad_agent = ib_register_mad_agent(sport->sdev->device, | |
601 | sport->port, | |
602 | IB_QPT_GSI, | |
603 | ®_req, 0, | |
604 | srpt_mad_send_handler, | |
605 | srpt_mad_recv_handler, | |
0f29b46d | 606 | sport, 0); |
a42d985b | 607 | if (IS_ERR(sport->mad_agent)) { |
09f8a148 BVA |
608 | pr_err("%s-%d: MAD agent registration failed (%ld). Note: this is expected if SR-IOV is enabled.\n", |
609 | dev_name(&sport->sdev->device->dev), sport->port, | |
610 | PTR_ERR(sport->mad_agent)); | |
a42d985b | 611 | sport->mad_agent = NULL; |
a42d985b BVA |
612 | } |
613 | } | |
614 | ||
615 | return 0; | |
a42d985b BVA |
616 | } |
617 | ||
618 | /** | |
10eac19b BVA |
619 | * srpt_unregister_mad_agent - unregister MAD callback functions |
620 | * @sdev: SRPT HCA pointer. | |
a42d985b BVA |
621 | * |
622 | * Note: It is safe to call this function more than once for the same device. | |
623 | */ | |
624 | static void srpt_unregister_mad_agent(struct srpt_device *sdev) | |
625 | { | |
626 | struct ib_port_modify port_modify = { | |
627 | .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP, | |
628 | }; | |
629 | struct srpt_port *sport; | |
630 | int i; | |
631 | ||
632 | for (i = 1; i <= sdev->device->phys_port_cnt; i++) { | |
633 | sport = &sdev->port[i - 1]; | |
634 | WARN_ON(sport->port != i); | |
635 | if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0) | |
9f5d32af | 636 | pr_err("disabling MAD processing failed.\n"); |
a42d985b BVA |
637 | if (sport->mad_agent) { |
638 | ib_unregister_mad_agent(sport->mad_agent); | |
639 | sport->mad_agent = NULL; | |
640 | } | |
641 | } | |
642 | } | |
643 | ||
644 | /** | |
10eac19b BVA |
645 | * srpt_alloc_ioctx - allocate a SRPT I/O context structure |
646 | * @sdev: SRPT HCA pointer. | |
647 | * @ioctx_size: I/O context size. | |
5dabcd04 | 648 | * @buf_cache: I/O buffer cache. |
10eac19b | 649 | * @dir: DMA data direction. |
a42d985b BVA |
650 | */ |
651 | static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev, | |
5dabcd04 BVA |
652 | int ioctx_size, |
653 | struct kmem_cache *buf_cache, | |
a42d985b BVA |
654 | enum dma_data_direction dir) |
655 | { | |
656 | struct srpt_ioctx *ioctx; | |
657 | ||
5dabcd04 | 658 | ioctx = kzalloc(ioctx_size, GFP_KERNEL); |
a42d985b BVA |
659 | if (!ioctx) |
660 | goto err; | |
661 | ||
5dabcd04 | 662 | ioctx->buf = kmem_cache_alloc(buf_cache, GFP_KERNEL); |
a42d985b BVA |
663 | if (!ioctx->buf) |
664 | goto err_free_ioctx; | |
665 | ||
5dabcd04 BVA |
666 | ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, |
667 | kmem_cache_size(buf_cache), dir); | |
a42d985b BVA |
668 | if (ib_dma_mapping_error(sdev->device, ioctx->dma)) |
669 | goto err_free_buf; | |
670 | ||
671 | return ioctx; | |
672 | ||
673 | err_free_buf: | |
f617e5ff | 674 | kmem_cache_free(buf_cache, ioctx->buf); |
a42d985b BVA |
675 | err_free_ioctx: |
676 | kfree(ioctx); | |
677 | err: | |
678 | return NULL; | |
679 | } | |
680 | ||
681 | /** | |
10eac19b BVA |
682 | * srpt_free_ioctx - free a SRPT I/O context structure |
683 | * @sdev: SRPT HCA pointer. | |
684 | * @ioctx: I/O context pointer. | |
5dabcd04 | 685 | * @buf_cache: I/O buffer cache. |
10eac19b | 686 | * @dir: DMA data direction. |
a42d985b BVA |
687 | */ |
688 | static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx, | |
5dabcd04 BVA |
689 | struct kmem_cache *buf_cache, |
690 | enum dma_data_direction dir) | |
a42d985b BVA |
691 | { |
692 | if (!ioctx) | |
693 | return; | |
694 | ||
5dabcd04 BVA |
695 | ib_dma_unmap_single(sdev->device, ioctx->dma, |
696 | kmem_cache_size(buf_cache), dir); | |
697 | kmem_cache_free(buf_cache, ioctx->buf); | |
a42d985b BVA |
698 | kfree(ioctx); |
699 | } | |
700 | ||
701 | /** | |
10eac19b | 702 | * srpt_alloc_ioctx_ring - allocate a ring of SRPT I/O context structures |
a42d985b BVA |
703 | * @sdev: Device to allocate the I/O context ring for. |
704 | * @ring_size: Number of elements in the I/O context ring. | |
705 | * @ioctx_size: I/O context size. | |
5dabcd04 BVA |
706 | * @buf_cache: I/O buffer cache. |
707 | * @alignment_offset: Offset in each ring buffer at which the SRP information | |
708 | * unit starts. | |
a42d985b BVA |
709 | * @dir: DMA data direction. |
710 | */ | |
711 | static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev, | |
712 | int ring_size, int ioctx_size, | |
5dabcd04 BVA |
713 | struct kmem_cache *buf_cache, |
714 | int alignment_offset, | |
715 | enum dma_data_direction dir) | |
a42d985b BVA |
716 | { |
717 | struct srpt_ioctx **ring; | |
718 | int i; | |
719 | ||
ffd59806 BVA |
720 | WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx) && |
721 | ioctx_size != sizeof(struct srpt_send_ioctx)); | |
a42d985b | 722 | |
781a4016 | 723 | ring = kvmalloc_array(ring_size, sizeof(ring[0]), GFP_KERNEL); |
a42d985b BVA |
724 | if (!ring) |
725 | goto out; | |
726 | for (i = 0; i < ring_size; ++i) { | |
5dabcd04 | 727 | ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, buf_cache, dir); |
a42d985b BVA |
728 | if (!ring[i]) |
729 | goto err; | |
730 | ring[i]->index = i; | |
5dabcd04 | 731 | ring[i]->offset = alignment_offset; |
a42d985b BVA |
732 | } |
733 | goto out; | |
734 | ||
735 | err: | |
736 | while (--i >= 0) | |
5dabcd04 | 737 | srpt_free_ioctx(sdev, ring[i], buf_cache, dir); |
781a4016 | 738 | kvfree(ring); |
715252d4 | 739 | ring = NULL; |
a42d985b BVA |
740 | out: |
741 | return ring; | |
742 | } | |
743 | ||
744 | /** | |
10eac19b BVA |
745 | * srpt_free_ioctx_ring - free the ring of SRPT I/O context structures |
746 | * @ioctx_ring: I/O context ring to be freed. | |
747 | * @sdev: SRPT HCA pointer. | |
748 | * @ring_size: Number of ring elements. | |
5dabcd04 | 749 | * @buf_cache: I/O buffer cache. |
10eac19b | 750 | * @dir: DMA data direction. |
a42d985b BVA |
751 | */ |
752 | static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring, | |
753 | struct srpt_device *sdev, int ring_size, | |
5dabcd04 BVA |
754 | struct kmem_cache *buf_cache, |
755 | enum dma_data_direction dir) | |
a42d985b BVA |
756 | { |
757 | int i; | |
758 | ||
dea26209 BVA |
759 | if (!ioctx_ring) |
760 | return; | |
761 | ||
a42d985b | 762 | for (i = 0; i < ring_size; ++i) |
5dabcd04 | 763 | srpt_free_ioctx(sdev, ioctx_ring[i], buf_cache, dir); |
781a4016 | 764 | kvfree(ioctx_ring); |
a42d985b BVA |
765 | } |
766 | ||
a42d985b | 767 | /** |
10eac19b BVA |
768 | * srpt_set_cmd_state - set the state of a SCSI command |
769 | * @ioctx: Send I/O context. | |
770 | * @new: New I/O context state. | |
a42d985b BVA |
771 | * |
772 | * Does not modify the state of aborted commands. Returns the previous command | |
773 | * state. | |
774 | */ | |
775 | static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx, | |
776 | enum srpt_command_state new) | |
777 | { | |
778 | enum srpt_command_state previous; | |
a42d985b | 779 | |
a42d985b BVA |
780 | previous = ioctx->state; |
781 | if (previous != SRPT_STATE_DONE) | |
782 | ioctx->state = new; | |
a42d985b BVA |
783 | |
784 | return previous; | |
785 | } | |
786 | ||
787 | /** | |
10eac19b BVA |
788 | * srpt_test_and_set_cmd_state - test and set the state of a command |
789 | * @ioctx: Send I/O context. | |
790 | * @old: Current I/O context state. | |
791 | * @new: New I/O context state. | |
a42d985b BVA |
792 | * |
793 | * Returns true if and only if the previous command state was equal to 'old'. | |
794 | */ | |
795 | static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx, | |
796 | enum srpt_command_state old, | |
797 | enum srpt_command_state new) | |
798 | { | |
799 | enum srpt_command_state previous; | |
a42d985b BVA |
800 | |
801 | WARN_ON(!ioctx); | |
802 | WARN_ON(old == SRPT_STATE_DONE); | |
803 | WARN_ON(new == SRPT_STATE_NEW); | |
804 | ||
a42d985b BVA |
805 | previous = ioctx->state; |
806 | if (previous == old) | |
807 | ioctx->state = new; | |
2d67017c | 808 | |
a42d985b BVA |
809 | return previous == old; |
810 | } | |
811 | ||
812 | /** | |
10eac19b BVA |
813 | * srpt_post_recv - post an IB receive request |
814 | * @sdev: SRPT HCA pointer. | |
815 | * @ch: SRPT RDMA channel. | |
816 | * @ioctx: Receive I/O context pointer. | |
a42d985b | 817 | */ |
dea26209 | 818 | static int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch, |
a42d985b BVA |
819 | struct srpt_recv_ioctx *ioctx) |
820 | { | |
821 | struct ib_sge list; | |
9b32a596 | 822 | struct ib_recv_wr wr; |
a42d985b BVA |
823 | |
824 | BUG_ON(!sdev); | |
5dabcd04 | 825 | list.addr = ioctx->ioctx.dma + ioctx->ioctx.offset; |
a42d985b | 826 | list.length = srp_max_req_size; |
74333f12 | 827 | list.lkey = sdev->lkey; |
a42d985b | 828 | |
59fae4de CH |
829 | ioctx->ioctx.cqe.done = srpt_recv_done; |
830 | wr.wr_cqe = &ioctx->ioctx.cqe; | |
a42d985b BVA |
831 | wr.next = NULL; |
832 | wr.sg_list = &list; | |
833 | wr.num_sge = 1; | |
834 | ||
dea26209 | 835 | if (sdev->use_srq) |
9b32a596 | 836 | return ib_post_srq_recv(sdev->srq, &wr, NULL); |
dea26209 | 837 | else |
9b32a596 | 838 | return ib_post_recv(ch->qp, &wr, NULL); |
a42d985b BVA |
839 | } |
840 | ||
aaf45bd8 | 841 | /** |
10eac19b BVA |
842 | * srpt_zerolength_write - perform a zero-length RDMA write |
843 | * @ch: SRPT RDMA channel. | |
aaf45bd8 BVA |
844 | * |
845 | * A quote from the InfiniBand specification: C9-88: For an HCA responder | |
846 | * using Reliable Connection service, for each zero-length RDMA READ or WRITE | |
847 | * request, the R_Key shall not be validated, even if the request includes | |
848 | * Immediate data. | |
849 | */ | |
850 | static int srpt_zerolength_write(struct srpt_rdma_ch *ch) | |
851 | { | |
2a78cb4d BVA |
852 | struct ib_rdma_wr wr = { |
853 | .wr = { | |
06892cc1 AM |
854 | .next = NULL, |
855 | { .wr_cqe = &ch->zw_cqe, }, | |
2a78cb4d | 856 | .opcode = IB_WR_RDMA_WRITE, |
2a78cb4d BVA |
857 | .send_flags = IB_SEND_SIGNALED, |
858 | } | |
859 | }; | |
aaf45bd8 | 860 | |
63d370a6 BVA |
861 | pr_debug("%s-%d: queued zerolength write\n", ch->sess_name, |
862 | ch->qp->qp_num); | |
863 | ||
9b32a596 | 864 | return ib_post_send(ch->qp, &wr.wr, NULL); |
aaf45bd8 BVA |
865 | } |
866 | ||
867 | static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc) | |
868 | { | |
869 | struct srpt_rdma_ch *ch = cq->cq_context; | |
870 | ||
63d370a6 BVA |
871 | pr_debug("%s-%d wc->status %d\n", ch->sess_name, ch->qp->qp_num, |
872 | wc->status); | |
873 | ||
387add46 BVA |
874 | if (wc->status == IB_WC_SUCCESS) { |
875 | srpt_process_wait_list(ch); | |
876 | } else { | |
877 | if (srpt_set_ch_state(ch, CH_DISCONNECTED)) | |
878 | schedule_work(&ch->release_work); | |
879 | else | |
ea51d2e1 BVA |
880 | pr_debug("%s-%d: already disconnected.\n", |
881 | ch->sess_name, ch->qp->qp_num); | |
387add46 | 882 | } |
aaf45bd8 BVA |
883 | } |
884 | ||
b99f8e4d CH |
885 | static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx, |
886 | struct srp_direct_buf *db, int nbufs, struct scatterlist **sg, | |
887 | unsigned *sg_cnt) | |
888 | { | |
889 | enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd); | |
890 | struct srpt_rdma_ch *ch = ioctx->ch; | |
891 | struct scatterlist *prev = NULL; | |
892 | unsigned prev_nents; | |
893 | int ret, i; | |
894 | ||
895 | if (nbufs == 1) { | |
896 | ioctx->rw_ctxs = &ioctx->s_rw_ctx; | |
897 | } else { | |
898 | ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs), | |
899 | GFP_KERNEL); | |
900 | if (!ioctx->rw_ctxs) | |
901 | return -ENOMEM; | |
902 | } | |
903 | ||
904 | for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) { | |
905 | struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; | |
906 | u64 remote_addr = be64_to_cpu(db->va); | |
907 | u32 size = be32_to_cpu(db->len); | |
908 | u32 rkey = be32_to_cpu(db->key); | |
909 | ||
910 | ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false, | |
911 | i < nbufs - 1); | |
912 | if (ret) | |
913 | goto unwind; | |
914 | ||
915 | ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port, | |
916 | ctx->sg, ctx->nents, 0, remote_addr, rkey, dir); | |
917 | if (ret < 0) { | |
918 | target_free_sgl(ctx->sg, ctx->nents); | |
919 | goto unwind; | |
920 | } | |
921 | ||
922 | ioctx->n_rdma += ret; | |
923 | ioctx->n_rw_ctx++; | |
924 | ||
925 | if (prev) { | |
926 | sg_unmark_end(&prev[prev_nents - 1]); | |
927 | sg_chain(prev, prev_nents + 1, ctx->sg); | |
928 | } else { | |
929 | *sg = ctx->sg; | |
930 | } | |
931 | ||
932 | prev = ctx->sg; | |
933 | prev_nents = ctx->nents; | |
934 | ||
935 | *sg_cnt += ctx->nents; | |
936 | } | |
937 | ||
938 | return 0; | |
939 | ||
940 | unwind: | |
941 | while (--i >= 0) { | |
942 | struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; | |
943 | ||
944 | rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port, | |
945 | ctx->sg, ctx->nents, dir); | |
946 | target_free_sgl(ctx->sg, ctx->nents); | |
947 | } | |
948 | if (ioctx->rw_ctxs != &ioctx->s_rw_ctx) | |
949 | kfree(ioctx->rw_ctxs); | |
950 | return ret; | |
951 | } | |
952 | ||
953 | static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch, | |
954 | struct srpt_send_ioctx *ioctx) | |
955 | { | |
956 | enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd); | |
957 | int i; | |
958 | ||
959 | for (i = 0; i < ioctx->n_rw_ctx; i++) { | |
960 | struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; | |
961 | ||
962 | rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port, | |
963 | ctx->sg, ctx->nents, dir); | |
964 | target_free_sgl(ctx->sg, ctx->nents); | |
965 | } | |
966 | ||
967 | if (ioctx->rw_ctxs != &ioctx->s_rw_ctx) | |
968 | kfree(ioctx->rw_ctxs); | |
969 | } | |
970 | ||
971 | static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd) | |
972 | { | |
973 | /* | |
974 | * The pointer computations below will only be compiled correctly | |
975 | * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check | |
976 | * whether srp_cmd::add_data has been declared as a byte pointer. | |
977 | */ | |
978 | BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) && | |
979 | !__same_type(srp_cmd->add_data[0], (u8)0)); | |
980 | ||
981 | /* | |
982 | * According to the SRP spec, the lower two bits of the 'ADDITIONAL | |
983 | * CDB LENGTH' field are reserved and the size in bytes of this field | |
984 | * is four times the value specified in bits 3..7. Hence the "& ~3". | |
985 | */ | |
986 | return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3); | |
987 | } | |
988 | ||
a42d985b | 989 | /** |
10eac19b | 990 | * srpt_get_desc_tbl - parse the data descriptors of a SRP_CMD request |
5dabcd04 BVA |
991 | * @recv_ioctx: I/O context associated with the received command @srp_cmd. |
992 | * @ioctx: I/O context that will be used for responding to the initiator. | |
a42d985b BVA |
993 | * @srp_cmd: Pointer to the SRP_CMD request data. |
994 | * @dir: Pointer to the variable to which the transfer direction will be | |
995 | * written. | |
5dabcd04 | 996 | * @sg: [out] scatterlist for the parsed SRP_CMD. |
10eac19b | 997 | * @sg_cnt: [out] length of @sg. |
a42d985b BVA |
998 | * @data_len: Pointer to the variable to which the total data length of all |
999 | * descriptors in the SRP_CMD request will be written. | |
5dabcd04 BVA |
1000 | * @imm_data_offset: [in] Offset in SRP_CMD requests at which immediate data |
1001 | * starts. | |
a42d985b BVA |
1002 | * |
1003 | * This function initializes ioctx->nrbuf and ioctx->r_bufs. | |
1004 | * | |
1005 | * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors; | |
1006 | * -ENOMEM when memory allocation fails and zero upon success. | |
1007 | */ | |
5dabcd04 BVA |
1008 | static int srpt_get_desc_tbl(struct srpt_recv_ioctx *recv_ioctx, |
1009 | struct srpt_send_ioctx *ioctx, | |
b99f8e4d | 1010 | struct srp_cmd *srp_cmd, enum dma_data_direction *dir, |
5dabcd04 BVA |
1011 | struct scatterlist **sg, unsigned int *sg_cnt, u64 *data_len, |
1012 | u16 imm_data_offset) | |
a42d985b | 1013 | { |
a42d985b BVA |
1014 | BUG_ON(!dir); |
1015 | BUG_ON(!data_len); | |
1016 | ||
a42d985b BVA |
1017 | /* |
1018 | * The lower four bits of the buffer format field contain the DATA-IN | |
1019 | * buffer descriptor format, and the highest four bits contain the | |
1020 | * DATA-OUT buffer descriptor format. | |
1021 | */ | |
a42d985b BVA |
1022 | if (srp_cmd->buf_fmt & 0xf) |
1023 | /* DATA-IN: transfer data from target to initiator (read). */ | |
1024 | *dir = DMA_FROM_DEVICE; | |
1025 | else if (srp_cmd->buf_fmt >> 4) | |
1026 | /* DATA-OUT: transfer data from initiator to target (write). */ | |
1027 | *dir = DMA_TO_DEVICE; | |
b99f8e4d CH |
1028 | else |
1029 | *dir = DMA_NONE; | |
1030 | ||
1031 | /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */ | |
1032 | ioctx->cmd.data_direction = *dir; | |
a42d985b | 1033 | |
a42d985b BVA |
1034 | if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) || |
1035 | ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) { | |
ffd59806 | 1036 | struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd); |
a42d985b | 1037 | |
a42d985b | 1038 | *data_len = be32_to_cpu(db->len); |
b99f8e4d | 1039 | return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt); |
a42d985b BVA |
1040 | } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) || |
1041 | ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) { | |
b99f8e4d CH |
1042 | struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd); |
1043 | int nbufs = be32_to_cpu(idb->table_desc.len) / | |
1044 | sizeof(struct srp_direct_buf); | |
a42d985b | 1045 | |
b99f8e4d | 1046 | if (nbufs > |
a42d985b | 1047 | (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) { |
75d79b80 | 1048 | pr_err("received unsupported SRP_CMD request type (%u out + %u in != %u / %zu)\n", |
a42d985b BVA |
1049 | srp_cmd->data_out_desc_cnt, |
1050 | srp_cmd->data_in_desc_cnt, | |
1051 | be32_to_cpu(idb->table_desc.len), | |
b99f8e4d CH |
1052 | sizeof(struct srp_direct_buf)); |
1053 | return -EINVAL; | |
a42d985b BVA |
1054 | } |
1055 | ||
a42d985b | 1056 | *data_len = be32_to_cpu(idb->len); |
b99f8e4d CH |
1057 | return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs, |
1058 | sg, sg_cnt); | |
5dabcd04 BVA |
1059 | } else if ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_IMM) { |
1060 | struct srp_imm_buf *imm_buf = srpt_get_desc_buf(srp_cmd); | |
1061 | void *data = (void *)srp_cmd + imm_data_offset; | |
1062 | uint32_t len = be32_to_cpu(imm_buf->len); | |
1063 | uint32_t req_size = imm_data_offset + len; | |
1064 | ||
1065 | if (req_size > srp_max_req_size) { | |
1066 | pr_err("Immediate data (length %d + %d) exceeds request size %d\n", | |
1067 | imm_data_offset, len, srp_max_req_size); | |
1068 | return -EINVAL; | |
1069 | } | |
1070 | if (recv_ioctx->byte_len < req_size) { | |
1071 | pr_err("Received too few data - %d < %d\n", | |
1072 | recv_ioctx->byte_len, req_size); | |
1073 | return -EIO; | |
1074 | } | |
1075 | /* | |
1076 | * The immediate data buffer descriptor must occur before the | |
1077 | * immediate data itself. | |
1078 | */ | |
1079 | if ((void *)(imm_buf + 1) > (void *)data) { | |
1080 | pr_err("Received invalid write request\n"); | |
1081 | return -EINVAL; | |
1082 | } | |
1083 | *data_len = len; | |
1084 | ioctx->recv_ioctx = recv_ioctx; | |
1085 | if ((uintptr_t)data & 511) { | |
1086 | pr_warn_once("Internal error - the receive buffers are not aligned properly.\n"); | |
1087 | return -EINVAL; | |
1088 | } | |
1089 | sg_init_one(&ioctx->imm_sg, data, len); | |
1090 | *sg = &ioctx->imm_sg; | |
1091 | *sg_cnt = 1; | |
1092 | return 0; | |
b99f8e4d CH |
1093 | } else { |
1094 | *data_len = 0; | |
1095 | return 0; | |
a42d985b | 1096 | } |
a42d985b BVA |
1097 | } |
1098 | ||
1099 | /** | |
10eac19b BVA |
1100 | * srpt_init_ch_qp - initialize queue pair attributes |
1101 | * @ch: SRPT RDMA channel. | |
1102 | * @qp: Queue pair pointer. | |
a42d985b BVA |
1103 | * |
1104 | * Initialized the attributes of queue pair 'qp' by allowing local write, | |
1105 | * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT. | |
1106 | */ | |
1107 | static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp) | |
1108 | { | |
1109 | struct ib_qp_attr *attr; | |
1110 | int ret; | |
1111 | ||
63cf1a90 BVA |
1112 | WARN_ON_ONCE(ch->using_rdma_cm); |
1113 | ||
9d2aa2b4 | 1114 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
a42d985b BVA |
1115 | if (!attr) |
1116 | return -ENOMEM; | |
1117 | ||
1118 | attr->qp_state = IB_QPS_INIT; | |
bec40c26 | 1119 | attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE; |
a42d985b | 1120 | attr->port_num = ch->sport->port; |
c5efb621 BVA |
1121 | |
1122 | ret = ib_find_cached_pkey(ch->sport->sdev->device, ch->sport->port, | |
1123 | ch->pkey, &attr->pkey_index); | |
1124 | if (ret < 0) | |
1125 | pr_err("Translating pkey %#x failed (%d) - using index 0\n", | |
1126 | ch->pkey, ret); | |
a42d985b BVA |
1127 | |
1128 | ret = ib_modify_qp(qp, attr, | |
1129 | IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT | | |
1130 | IB_QP_PKEY_INDEX); | |
1131 | ||
1132 | kfree(attr); | |
1133 | return ret; | |
1134 | } | |
1135 | ||
1136 | /** | |
10eac19b | 1137 | * srpt_ch_qp_rtr - change the state of a channel to 'ready to receive' (RTR) |
a42d985b BVA |
1138 | * @ch: channel of the queue pair. |
1139 | * @qp: queue pair to change the state of. | |
1140 | * | |
1141 | * Returns zero upon success and a negative value upon failure. | |
1142 | * | |
1143 | * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system. | |
1144 | * If this structure ever becomes larger, it might be necessary to allocate | |
1145 | * it dynamically instead of on the stack. | |
1146 | */ | |
1147 | static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp) | |
1148 | { | |
1149 | struct ib_qp_attr qp_attr; | |
1150 | int attr_mask; | |
1151 | int ret; | |
1152 | ||
63cf1a90 BVA |
1153 | WARN_ON_ONCE(ch->using_rdma_cm); |
1154 | ||
a42d985b | 1155 | qp_attr.qp_state = IB_QPS_RTR; |
090fa24b | 1156 | ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask); |
a42d985b BVA |
1157 | if (ret) |
1158 | goto out; | |
1159 | ||
1160 | qp_attr.max_dest_rd_atomic = 4; | |
1161 | ||
1162 | ret = ib_modify_qp(qp, &qp_attr, attr_mask); | |
1163 | ||
1164 | out: | |
1165 | return ret; | |
1166 | } | |
1167 | ||
1168 | /** | |
10eac19b | 1169 | * srpt_ch_qp_rts - change the state of a channel to 'ready to send' (RTS) |
a42d985b BVA |
1170 | * @ch: channel of the queue pair. |
1171 | * @qp: queue pair to change the state of. | |
1172 | * | |
1173 | * Returns zero upon success and a negative value upon failure. | |
1174 | * | |
1175 | * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system. | |
1176 | * If this structure ever becomes larger, it might be necessary to allocate | |
1177 | * it dynamically instead of on the stack. | |
1178 | */ | |
1179 | static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp) | |
1180 | { | |
1181 | struct ib_qp_attr qp_attr; | |
1182 | int attr_mask; | |
1183 | int ret; | |
1184 | ||
1185 | qp_attr.qp_state = IB_QPS_RTS; | |
090fa24b | 1186 | ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask); |
a42d985b BVA |
1187 | if (ret) |
1188 | goto out; | |
1189 | ||
1190 | qp_attr.max_rd_atomic = 4; | |
1191 | ||
1192 | ret = ib_modify_qp(qp, &qp_attr, attr_mask); | |
1193 | ||
1194 | out: | |
1195 | return ret; | |
1196 | } | |
1197 | ||
1198 | /** | |
10eac19b BVA |
1199 | * srpt_ch_qp_err - set the channel queue pair state to 'error' |
1200 | * @ch: SRPT RDMA channel. | |
a42d985b BVA |
1201 | */ |
1202 | static int srpt_ch_qp_err(struct srpt_rdma_ch *ch) | |
1203 | { | |
1204 | struct ib_qp_attr qp_attr; | |
1205 | ||
1206 | qp_attr.qp_state = IB_QPS_ERR; | |
1207 | return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE); | |
1208 | } | |
1209 | ||
a42d985b | 1210 | /** |
10eac19b BVA |
1211 | * srpt_get_send_ioctx - obtain an I/O context for sending to the initiator |
1212 | * @ch: SRPT RDMA channel. | |
a42d985b BVA |
1213 | */ |
1214 | static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch) | |
1215 | { | |
1216 | struct srpt_send_ioctx *ioctx; | |
fd1b6687 | 1217 | int tag, cpu; |
a42d985b BVA |
1218 | |
1219 | BUG_ON(!ch); | |
1220 | ||
fd1b6687 BVA |
1221 | tag = sbitmap_queue_get(&ch->sess->sess_tag_pool, &cpu); |
1222 | if (tag < 0) | |
1223 | return NULL; | |
3c968887 | 1224 | |
fd1b6687 | 1225 | ioctx = ch->ioctx_ring[tag]; |
3c968887 | 1226 | BUG_ON(ioctx->ch != ch); |
a42d985b | 1227 | ioctx->state = SRPT_STATE_NEW; |
5dabcd04 | 1228 | WARN_ON_ONCE(ioctx->recv_ioctx); |
3c968887 | 1229 | ioctx->n_rdma = 0; |
b99f8e4d | 1230 | ioctx->n_rw_ctx = 0; |
3c968887 BVA |
1231 | ioctx->queue_status_only = false; |
1232 | /* | |
1233 | * transport_init_se_cmd() does not initialize all fields, so do it | |
1234 | * here. | |
1235 | */ | |
1236 | memset(&ioctx->cmd, 0, sizeof(ioctx->cmd)); | |
1237 | memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data)); | |
fd1b6687 BVA |
1238 | ioctx->cmd.map_tag = tag; |
1239 | ioctx->cmd.map_cpu = cpu; | |
a42d985b BVA |
1240 | |
1241 | return ioctx; | |
1242 | } | |
1243 | ||
a42d985b | 1244 | /** |
10eac19b | 1245 | * srpt_abort_cmd - abort a SCSI command |
a42d985b | 1246 | * @ioctx: I/O context associated with the SCSI command. |
a42d985b BVA |
1247 | */ |
1248 | static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx) | |
1249 | { | |
1250 | enum srpt_command_state state; | |
a42d985b BVA |
1251 | |
1252 | BUG_ON(!ioctx); | |
1253 | ||
1254 | /* | |
1255 | * If the command is in a state where the target core is waiting for | |
49f40163 | 1256 | * the ib_srpt driver, change the state to the next state. |
a42d985b BVA |
1257 | */ |
1258 | ||
a42d985b BVA |
1259 | state = ioctx->state; |
1260 | switch (state) { | |
1261 | case SRPT_STATE_NEED_DATA: | |
1262 | ioctx->state = SRPT_STATE_DATA_IN; | |
1263 | break; | |
a42d985b BVA |
1264 | case SRPT_STATE_CMD_RSP_SENT: |
1265 | case SRPT_STATE_MGMT_RSP_SENT: | |
1266 | ioctx->state = SRPT_STATE_DONE; | |
1267 | break; | |
1268 | default: | |
49f40163 BVA |
1269 | WARN_ONCE(true, "%s: unexpected I/O context state %d\n", |
1270 | __func__, state); | |
a42d985b BVA |
1271 | break; |
1272 | } | |
a42d985b | 1273 | |
13fdd445 BVA |
1274 | pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state, |
1275 | ioctx->state, ioctx->cmd.tag); | |
a42d985b BVA |
1276 | |
1277 | switch (state) { | |
1278 | case SRPT_STATE_NEW: | |
1279 | case SRPT_STATE_DATA_IN: | |
1280 | case SRPT_STATE_MGMT: | |
49f40163 | 1281 | case SRPT_STATE_DONE: |
a42d985b BVA |
1282 | /* |
1283 | * Do nothing - defer abort processing until | |
1284 | * srpt_queue_response() is invoked. | |
1285 | */ | |
a42d985b BVA |
1286 | break; |
1287 | case SRPT_STATE_NEED_DATA: | |
49f40163 BVA |
1288 | pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag); |
1289 | transport_generic_request_failure(&ioctx->cmd, | |
1290 | TCM_CHECK_CONDITION_ABORT_CMD); | |
a42d985b BVA |
1291 | break; |
1292 | case SRPT_STATE_CMD_RSP_SENT: | |
1293 | /* | |
1294 | * SRP_RSP sending failed or the SRP_RSP send completion has | |
1295 | * not been received in time. | |
1296 | */ | |
49f40163 | 1297 | transport_generic_free_cmd(&ioctx->cmd, 0); |
a42d985b BVA |
1298 | break; |
1299 | case SRPT_STATE_MGMT_RSP_SENT: | |
49f40163 | 1300 | transport_generic_free_cmd(&ioctx->cmd, 0); |
a42d985b BVA |
1301 | break; |
1302 | default: | |
532ec6f1 | 1303 | WARN(1, "Unexpected command state (%d)", state); |
a42d985b BVA |
1304 | break; |
1305 | } | |
1306 | ||
a42d985b BVA |
1307 | return state; |
1308 | } | |
1309 | ||
1310 | /** | |
10eac19b BVA |
1311 | * srpt_rdma_read_done - RDMA read completion callback |
1312 | * @cq: Completion queue. | |
1313 | * @wc: Work completion. | |
1314 | * | |
e672a47f CH |
1315 | * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping |
1316 | * the data that has been transferred via IB RDMA had to be postponed until the | |
142ad5db | 1317 | * check_stop_free() callback. None of this is necessary anymore and needs to |
e672a47f | 1318 | * be cleaned up. |
a42d985b | 1319 | */ |
59fae4de | 1320 | static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc) |
a42d985b | 1321 | { |
59fae4de CH |
1322 | struct srpt_rdma_ch *ch = cq->cq_context; |
1323 | struct srpt_send_ioctx *ioctx = | |
19f57298 | 1324 | container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe); |
59fae4de | 1325 | |
a42d985b BVA |
1326 | WARN_ON(ioctx->n_rdma <= 0); |
1327 | atomic_add(ioctx->n_rdma, &ch->sq_wr_avail); | |
b99f8e4d | 1328 | ioctx->n_rdma = 0; |
a42d985b | 1329 | |
59fae4de CH |
1330 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
1331 | pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n", | |
1332 | ioctx, wc->status); | |
1333 | srpt_abort_cmd(ioctx); | |
1334 | return; | |
a42d985b | 1335 | } |
59fae4de CH |
1336 | |
1337 | if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA, | |
1338 | SRPT_STATE_DATA_IN)) | |
1339 | target_execute_cmd(&ioctx->cmd); | |
1340 | else | |
1341 | pr_err("%s[%d]: wrong state = %d\n", __func__, | |
dd3bec86 | 1342 | __LINE__, ioctx->state); |
a42d985b BVA |
1343 | } |
1344 | ||
a42d985b | 1345 | /** |
10eac19b | 1346 | * srpt_build_cmd_rsp - build a SRP_RSP response |
a42d985b BVA |
1347 | * @ch: RDMA channel through which the request has been received. |
1348 | * @ioctx: I/O context associated with the SRP_CMD request. The response will | |
1349 | * be built in the buffer ioctx->buf points at and hence this function will | |
1350 | * overwrite the request data. | |
1351 | * @tag: tag of the request for which this response is being generated. | |
1352 | * @status: value for the STATUS field of the SRP_RSP information unit. | |
1353 | * | |
1354 | * Returns the size in bytes of the SRP_RSP response. | |
1355 | * | |
1356 | * An SRP_RSP response contains a SCSI status or service response. See also | |
1357 | * section 6.9 in the SRP r16a document for the format of an SRP_RSP | |
1358 | * response. See also SPC-2 for more information about sense data. | |
1359 | */ | |
1360 | static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch, | |
1361 | struct srpt_send_ioctx *ioctx, u64 tag, | |
1362 | int status) | |
1363 | { | |
1364 | struct srp_rsp *srp_rsp; | |
1365 | const u8 *sense_data; | |
1366 | int sense_data_len, max_sense_len; | |
1367 | ||
1368 | /* | |
1369 | * The lowest bit of all SAM-3 status codes is zero (see also | |
1370 | * paragraph 5.3 in SAM-3). | |
1371 | */ | |
1372 | WARN_ON(status & 1); | |
1373 | ||
1374 | srp_rsp = ioctx->ioctx.buf; | |
1375 | BUG_ON(!srp_rsp); | |
1376 | ||
1377 | sense_data = ioctx->sense_data; | |
1378 | sense_data_len = ioctx->cmd.scsi_sense_length; | |
1379 | WARN_ON(sense_data_len > sizeof(ioctx->sense_data)); | |
1380 | ||
9d2aa2b4 | 1381 | memset(srp_rsp, 0, sizeof(*srp_rsp)); |
a42d985b BVA |
1382 | srp_rsp->opcode = SRP_RSP; |
1383 | srp_rsp->req_lim_delta = | |
b356c1c1 | 1384 | cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0)); |
a42d985b BVA |
1385 | srp_rsp->tag = tag; |
1386 | srp_rsp->status = status; | |
1387 | ||
1388 | if (sense_data_len) { | |
1389 | BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp)); | |
1390 | max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp); | |
1391 | if (sense_data_len > max_sense_len) { | |
75d79b80 BVA |
1392 | pr_warn("truncated sense data from %d to %d bytes\n", |
1393 | sense_data_len, max_sense_len); | |
a42d985b BVA |
1394 | sense_data_len = max_sense_len; |
1395 | } | |
1396 | ||
1397 | srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID; | |
1398 | srp_rsp->sense_data_len = cpu_to_be32(sense_data_len); | |
1399 | memcpy(srp_rsp + 1, sense_data, sense_data_len); | |
1400 | } | |
1401 | ||
1402 | return sizeof(*srp_rsp) + sense_data_len; | |
1403 | } | |
1404 | ||
1405 | /** | |
10eac19b | 1406 | * srpt_build_tskmgmt_rsp - build a task management response |
a42d985b BVA |
1407 | * @ch: RDMA channel through which the request has been received. |
1408 | * @ioctx: I/O context in which the SRP_RSP response will be built. | |
1409 | * @rsp_code: RSP_CODE that will be stored in the response. | |
1410 | * @tag: Tag of the request for which this response is being generated. | |
1411 | * | |
1412 | * Returns the size in bytes of the SRP_RSP response. | |
1413 | * | |
1414 | * An SRP_RSP response contains a SCSI status or service response. See also | |
1415 | * section 6.9 in the SRP r16a document for the format of an SRP_RSP | |
1416 | * response. | |
1417 | */ | |
1418 | static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch, | |
1419 | struct srpt_send_ioctx *ioctx, | |
1420 | u8 rsp_code, u64 tag) | |
1421 | { | |
1422 | struct srp_rsp *srp_rsp; | |
1423 | int resp_data_len; | |
1424 | int resp_len; | |
1425 | ||
c807f643 | 1426 | resp_data_len = 4; |
a42d985b BVA |
1427 | resp_len = sizeof(*srp_rsp) + resp_data_len; |
1428 | ||
1429 | srp_rsp = ioctx->ioctx.buf; | |
1430 | BUG_ON(!srp_rsp); | |
9d2aa2b4 | 1431 | memset(srp_rsp, 0, sizeof(*srp_rsp)); |
a42d985b BVA |
1432 | |
1433 | srp_rsp->opcode = SRP_RSP; | |
b356c1c1 VT |
1434 | srp_rsp->req_lim_delta = |
1435 | cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0)); | |
a42d985b BVA |
1436 | srp_rsp->tag = tag; |
1437 | ||
c807f643 JW |
1438 | srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID; |
1439 | srp_rsp->resp_data_len = cpu_to_be32(resp_data_len); | |
1440 | srp_rsp->data[3] = rsp_code; | |
a42d985b BVA |
1441 | |
1442 | return resp_len; | |
1443 | } | |
1444 | ||
a42d985b BVA |
1445 | static int srpt_check_stop_free(struct se_cmd *cmd) |
1446 | { | |
9474b043 NB |
1447 | struct srpt_send_ioctx *ioctx = container_of(cmd, |
1448 | struct srpt_send_ioctx, cmd); | |
a42d985b | 1449 | |
afc16604 | 1450 | return target_put_sess_cmd(&ioctx->cmd); |
a42d985b BVA |
1451 | } |
1452 | ||
1453 | /** | |
10eac19b BVA |
1454 | * srpt_handle_cmd - process a SRP_CMD information unit |
1455 | * @ch: SRPT RDMA channel. | |
1456 | * @recv_ioctx: Receive I/O context. | |
1457 | * @send_ioctx: Send I/O context. | |
a42d985b | 1458 | */ |
2c7f37ff BVA |
1459 | static void srpt_handle_cmd(struct srpt_rdma_ch *ch, |
1460 | struct srpt_recv_ioctx *recv_ioctx, | |
1461 | struct srpt_send_ioctx *send_ioctx) | |
a42d985b BVA |
1462 | { |
1463 | struct se_cmd *cmd; | |
1464 | struct srp_cmd *srp_cmd; | |
b99f8e4d CH |
1465 | struct scatterlist *sg = NULL; |
1466 | unsigned sg_cnt = 0; | |
a42d985b BVA |
1467 | u64 data_len; |
1468 | enum dma_data_direction dir; | |
9474b043 | 1469 | int rc; |
a42d985b BVA |
1470 | |
1471 | BUG_ON(!send_ioctx); | |
1472 | ||
5dabcd04 | 1473 | srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset; |
a42d985b | 1474 | cmd = &send_ioctx->cmd; |
649ee054 | 1475 | cmd->tag = srp_cmd->tag; |
a42d985b BVA |
1476 | |
1477 | switch (srp_cmd->task_attr) { | |
1478 | case SRP_CMD_SIMPLE_Q: | |
68d81f40 | 1479 | cmd->sam_task_attr = TCM_SIMPLE_TAG; |
a42d985b BVA |
1480 | break; |
1481 | case SRP_CMD_ORDERED_Q: | |
1482 | default: | |
68d81f40 | 1483 | cmd->sam_task_attr = TCM_ORDERED_TAG; |
a42d985b BVA |
1484 | break; |
1485 | case SRP_CMD_HEAD_OF_Q: | |
68d81f40 | 1486 | cmd->sam_task_attr = TCM_HEAD_TAG; |
a42d985b BVA |
1487 | break; |
1488 | case SRP_CMD_ACA: | |
68d81f40 | 1489 | cmd->sam_task_attr = TCM_ACA_TAG; |
a42d985b BVA |
1490 | break; |
1491 | } | |
1492 | ||
5dabcd04 BVA |
1493 | rc = srpt_get_desc_tbl(recv_ioctx, send_ioctx, srp_cmd, &dir, |
1494 | &sg, &sg_cnt, &data_len, ch->imm_data_offset); | |
b99f8e4d CH |
1495 | if (rc) { |
1496 | if (rc != -EAGAIN) { | |
1497 | pr_err("0x%llx: parsing SRP descriptor table failed.\n", | |
1498 | srp_cmd->tag); | |
1499 | } | |
8b8807b9 | 1500 | goto busy; |
a42d985b BVA |
1501 | } |
1502 | ||
b99f8e4d | 1503 | rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb, |
e1dd413c BVA |
1504 | &send_ioctx->sense_data[0], |
1505 | scsilun_to_int(&srp_cmd->lun), data_len, | |
b99f8e4d CH |
1506 | TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF, |
1507 | sg, sg_cnt, NULL, 0, NULL, 0); | |
9474b043 | 1508 | if (rc != 0) { |
2c7f37ff BVA |
1509 | pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc, |
1510 | srp_cmd->tag); | |
8b8807b9 | 1511 | goto busy; |
187e70a5 | 1512 | } |
2c7f37ff | 1513 | return; |
a42d985b | 1514 | |
8b8807b9 BVA |
1515 | busy: |
1516 | target_send_busy(cmd); | |
a42d985b BVA |
1517 | } |
1518 | ||
a42d985b BVA |
1519 | static int srp_tmr_to_tcm(int fn) |
1520 | { | |
1521 | switch (fn) { | |
1522 | case SRP_TSK_ABORT_TASK: | |
1523 | return TMR_ABORT_TASK; | |
1524 | case SRP_TSK_ABORT_TASK_SET: | |
1525 | return TMR_ABORT_TASK_SET; | |
1526 | case SRP_TSK_CLEAR_TASK_SET: | |
1527 | return TMR_CLEAR_TASK_SET; | |
1528 | case SRP_TSK_LUN_RESET: | |
1529 | return TMR_LUN_RESET; | |
1530 | case SRP_TSK_CLEAR_ACA: | |
1531 | return TMR_CLEAR_ACA; | |
1532 | default: | |
1533 | return -1; | |
1534 | } | |
1535 | } | |
1536 | ||
1537 | /** | |
10eac19b BVA |
1538 | * srpt_handle_tsk_mgmt - process a SRP_TSK_MGMT information unit |
1539 | * @ch: SRPT RDMA channel. | |
1540 | * @recv_ioctx: Receive I/O context. | |
1541 | * @send_ioctx: Send I/O context. | |
a42d985b BVA |
1542 | * |
1543 | * Returns 0 if and only if the request will be processed by the target core. | |
1544 | * | |
1545 | * For more information about SRP_TSK_MGMT information units, see also section | |
1546 | * 6.7 in the SRP r16a document. | |
1547 | */ | |
1548 | static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch, | |
1549 | struct srpt_recv_ioctx *recv_ioctx, | |
1550 | struct srpt_send_ioctx *send_ioctx) | |
1551 | { | |
1552 | struct srp_tsk_mgmt *srp_tsk; | |
1553 | struct se_cmd *cmd; | |
3e4f5748 | 1554 | struct se_session *sess = ch->sess; |
a42d985b | 1555 | int tcm_tmr; |
3e4f5748 | 1556 | int rc; |
a42d985b BVA |
1557 | |
1558 | BUG_ON(!send_ioctx); | |
1559 | ||
5dabcd04 | 1560 | srp_tsk = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset; |
a42d985b BVA |
1561 | cmd = &send_ioctx->cmd; |
1562 | ||
090fa24b BVA |
1563 | pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld ch %p sess %p\n", |
1564 | srp_tsk->tsk_mgmt_func, srp_tsk->task_tag, srp_tsk->tag, ch, | |
1565 | ch->sess); | |
a42d985b BVA |
1566 | |
1567 | srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT); | |
649ee054 | 1568 | send_ioctx->cmd.tag = srp_tsk->tag; |
a42d985b | 1569 | tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func); |
e1dd413c BVA |
1570 | rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, |
1571 | scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr, | |
1572 | GFP_KERNEL, srp_tsk->task_tag, | |
1573 | TARGET_SCF_ACK_KREF); | |
3e4f5748 NB |
1574 | if (rc != 0) { |
1575 | send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED; | |
337ec69e | 1576 | cmd->se_tfo->queue_tm_rsp(cmd); |
a42d985b | 1577 | } |
de103c93 | 1578 | return; |
a42d985b BVA |
1579 | } |
1580 | ||
1581 | /** | |
10eac19b | 1582 | * srpt_handle_new_iu - process a newly received information unit |
a42d985b | 1583 | * @ch: RDMA channel through which the information unit has been received. |
10eac19b | 1584 | * @recv_ioctx: Receive I/O context associated with the information unit. |
a42d985b | 1585 | */ |
fcf58936 BVA |
1586 | static bool |
1587 | srpt_handle_new_iu(struct srpt_rdma_ch *ch, struct srpt_recv_ioctx *recv_ioctx) | |
a42d985b | 1588 | { |
fcf58936 | 1589 | struct srpt_send_ioctx *send_ioctx = NULL; |
a42d985b | 1590 | struct srp_cmd *srp_cmd; |
fcf58936 BVA |
1591 | bool res = false; |
1592 | u8 opcode; | |
a42d985b BVA |
1593 | |
1594 | BUG_ON(!ch); | |
1595 | BUG_ON(!recv_ioctx); | |
1596 | ||
fcf58936 BVA |
1597 | if (unlikely(ch->state == CH_CONNECTING)) |
1598 | goto push; | |
1599 | ||
a42d985b | 1600 | ib_dma_sync_single_for_cpu(ch->sport->sdev->device, |
5dabcd04 BVA |
1601 | recv_ioctx->ioctx.dma, |
1602 | recv_ioctx->ioctx.offset + srp_max_req_size, | |
a42d985b BVA |
1603 | DMA_FROM_DEVICE); |
1604 | ||
5dabcd04 | 1605 | srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset; |
fcf58936 BVA |
1606 | opcode = srp_cmd->opcode; |
1607 | if (opcode == SRP_CMD || opcode == SRP_TSK_MGMT) { | |
1608 | send_ioctx = srpt_get_send_ioctx(ch); | |
b99f8e4d | 1609 | if (unlikely(!send_ioctx)) |
fcf58936 | 1610 | goto push; |
a42d985b BVA |
1611 | } |
1612 | ||
fcf58936 BVA |
1613 | if (!list_empty(&recv_ioctx->wait_list)) { |
1614 | WARN_ON_ONCE(!ch->processing_wait_list); | |
1615 | list_del_init(&recv_ioctx->wait_list); | |
1616 | } | |
1617 | ||
1618 | switch (opcode) { | |
a42d985b BVA |
1619 | case SRP_CMD: |
1620 | srpt_handle_cmd(ch, recv_ioctx, send_ioctx); | |
1621 | break; | |
1622 | case SRP_TSK_MGMT: | |
1623 | srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx); | |
1624 | break; | |
1625 | case SRP_I_LOGOUT: | |
9f5d32af | 1626 | pr_err("Not yet implemented: SRP_I_LOGOUT\n"); |
a42d985b BVA |
1627 | break; |
1628 | case SRP_CRED_RSP: | |
1629 | pr_debug("received SRP_CRED_RSP\n"); | |
1630 | break; | |
1631 | case SRP_AER_RSP: | |
1632 | pr_debug("received SRP_AER_RSP\n"); | |
1633 | break; | |
1634 | case SRP_RSP: | |
9f5d32af | 1635 | pr_err("Received SRP_RSP\n"); |
a42d985b BVA |
1636 | break; |
1637 | default: | |
fcf58936 | 1638 | pr_err("received IU with unknown opcode 0x%x\n", opcode); |
a42d985b BVA |
1639 | break; |
1640 | } | |
1641 | ||
5dabcd04 BVA |
1642 | if (!send_ioctx || !send_ioctx->recv_ioctx) |
1643 | srpt_post_recv(ch->sport->sdev, ch, recv_ioctx); | |
fcf58936 | 1644 | res = true; |
b99f8e4d | 1645 | |
fcf58936 BVA |
1646 | out: |
1647 | return res; | |
1648 | ||
1649 | push: | |
1650 | if (list_empty(&recv_ioctx->wait_list)) { | |
1651 | WARN_ON_ONCE(ch->processing_wait_list); | |
1652 | list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list); | |
1653 | } | |
1654 | goto out; | |
a42d985b BVA |
1655 | } |
1656 | ||
59fae4de | 1657 | static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc) |
a42d985b | 1658 | { |
59fae4de CH |
1659 | struct srpt_rdma_ch *ch = cq->cq_context; |
1660 | struct srpt_recv_ioctx *ioctx = | |
1661 | container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe); | |
a42d985b | 1662 | |
a42d985b BVA |
1663 | if (wc->status == IB_WC_SUCCESS) { |
1664 | int req_lim; | |
1665 | ||
1666 | req_lim = atomic_dec_return(&ch->req_lim); | |
1667 | if (unlikely(req_lim < 0)) | |
9f5d32af | 1668 | pr_err("req_lim = %d < 0\n", req_lim); |
5dabcd04 | 1669 | ioctx->byte_len = wc->byte_len; |
fcf58936 | 1670 | srpt_handle_new_iu(ch, ioctx); |
a42d985b | 1671 | } else { |
3fa7f0e9 BVA |
1672 | pr_info_ratelimited("receiving failed for ioctx %p with status %d\n", |
1673 | ioctx, wc->status); | |
a42d985b BVA |
1674 | } |
1675 | } | |
1676 | ||
539b3248 BVA |
1677 | /* |
1678 | * This function must be called from the context in which RDMA completions are | |
1679 | * processed because it accesses the wait list without protection against | |
1680 | * access from other threads. | |
1681 | */ | |
1682 | static void srpt_process_wait_list(struct srpt_rdma_ch *ch) | |
1683 | { | |
fcf58936 | 1684 | struct srpt_recv_ioctx *recv_ioctx, *tmp; |
539b3248 | 1685 | |
fcf58936 | 1686 | WARN_ON_ONCE(ch->state == CH_CONNECTING); |
539b3248 | 1687 | |
fcf58936 BVA |
1688 | if (list_empty(&ch->cmd_wait_list)) |
1689 | return; | |
1690 | ||
1691 | WARN_ON_ONCE(ch->processing_wait_list); | |
1692 | ch->processing_wait_list = true; | |
1693 | list_for_each_entry_safe(recv_ioctx, tmp, &ch->cmd_wait_list, | |
1694 | wait_list) { | |
1695 | if (!srpt_handle_new_iu(ch, recv_ioctx)) | |
1696 | break; | |
539b3248 | 1697 | } |
fcf58936 | 1698 | ch->processing_wait_list = false; |
539b3248 BVA |
1699 | } |
1700 | ||
a42d985b | 1701 | /** |
10eac19b BVA |
1702 | * srpt_send_done - send completion callback |
1703 | * @cq: Completion queue. | |
1704 | * @wc: Work completion. | |
1705 | * | |
a42d985b BVA |
1706 | * Note: Although this has not yet been observed during tests, at least in |
1707 | * theory it is possible that the srpt_get_send_ioctx() call invoked by | |
1708 | * srpt_handle_new_iu() fails. This is possible because the req_lim_delta | |
1709 | * value in each response is set to one, and it is possible that this response | |
1710 | * makes the initiator send a new request before the send completion for that | |
1711 | * response has been processed. This could e.g. happen if the call to | |
1712 | * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or | |
1713 | * if IB retransmission causes generation of the send completion to be | |
1714 | * delayed. Incoming information units for which srpt_get_send_ioctx() fails | |
1715 | * are queued on cmd_wait_list. The code below processes these delayed | |
1716 | * requests one at a time. | |
1717 | */ | |
59fae4de | 1718 | static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc) |
a42d985b | 1719 | { |
59fae4de CH |
1720 | struct srpt_rdma_ch *ch = cq->cq_context; |
1721 | struct srpt_send_ioctx *ioctx = | |
1722 | container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe); | |
1723 | enum srpt_command_state state; | |
a42d985b | 1724 | |
59fae4de CH |
1725 | state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); |
1726 | ||
1727 | WARN_ON(state != SRPT_STATE_CMD_RSP_SENT && | |
1728 | state != SRPT_STATE_MGMT_RSP_SENT); | |
1729 | ||
b99f8e4d | 1730 | atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail); |
59fae4de | 1731 | |
49f40163 | 1732 | if (wc->status != IB_WC_SUCCESS) |
75d79b80 BVA |
1733 | pr_info("sending response for ioctx 0x%p failed with status %d\n", |
1734 | ioctx, wc->status); | |
59fae4de | 1735 | |
59fae4de | 1736 | if (state != SRPT_STATE_DONE) { |
59fae4de | 1737 | transport_generic_free_cmd(&ioctx->cmd, 0); |
a42d985b | 1738 | } else { |
75d79b80 BVA |
1739 | pr_err("IB completion has been received too late for wr_id = %u.\n", |
1740 | ioctx->ioctx.index); | |
a42d985b BVA |
1741 | } |
1742 | ||
539b3248 | 1743 | srpt_process_wait_list(ch); |
a42d985b BVA |
1744 | } |
1745 | ||
a42d985b | 1746 | /** |
10eac19b BVA |
1747 | * srpt_create_ch_ib - create receive and send completion queues |
1748 | * @ch: SRPT RDMA channel. | |
a42d985b BVA |
1749 | */ |
1750 | static int srpt_create_ch_ib(struct srpt_rdma_ch *ch) | |
1751 | { | |
1752 | struct ib_qp_init_attr *qp_init; | |
1753 | struct srpt_port *sport = ch->sport; | |
1754 | struct srpt_device *sdev = sport->sdev; | |
30c6d877 | 1755 | const struct ib_device_attr *attrs = &sdev->device->attrs; |
ed262287 | 1756 | int sq_size = sport->port_attrib.srp_sq_size; |
dea26209 | 1757 | int i, ret; |
a42d985b BVA |
1758 | |
1759 | WARN_ON(ch->rq_size < 1); | |
1760 | ||
1761 | ret = -ENOMEM; | |
9d2aa2b4 | 1762 | qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL); |
a42d985b BVA |
1763 | if (!qp_init) |
1764 | goto out; | |
1765 | ||
ab477c1f | 1766 | retry: |
20cf4e02 CL |
1767 | ch->cq = ib_alloc_cq_any(sdev->device, ch, ch->rq_size + sq_size, |
1768 | IB_POLL_WORKQUEUE); | |
a42d985b BVA |
1769 | if (IS_ERR(ch->cq)) { |
1770 | ret = PTR_ERR(ch->cq); | |
9f5d32af | 1771 | pr_err("failed to create CQ cqe= %d ret= %d\n", |
ed262287 | 1772 | ch->rq_size + sq_size, ret); |
a42d985b BVA |
1773 | goto out; |
1774 | } | |
1775 | ||
1776 | qp_init->qp_context = (void *)ch; | |
1777 | qp_init->event_handler | |
1778 | = (void(*)(struct ib_event *, void*))srpt_qp_event; | |
1779 | qp_init->send_cq = ch->cq; | |
1780 | qp_init->recv_cq = ch->cq; | |
a42d985b BVA |
1781 | qp_init->sq_sig_type = IB_SIGNAL_REQ_WR; |
1782 | qp_init->qp_type = IB_QPT_RC; | |
b99f8e4d CH |
1783 | /* |
1784 | * We divide up our send queue size into half SEND WRs to send the | |
1785 | * completions, and half R/W contexts to actually do the RDMA | |
1786 | * READ/WRITE transfers. Note that we need to allocate CQ slots for | |
1787 | * both both, as RDMA contexts will also post completions for the | |
1788 | * RDMA READ case. | |
1789 | */ | |
ed262287 BVA |
1790 | qp_init->cap.max_send_wr = min(sq_size / 2, attrs->max_qp_wr); |
1791 | qp_init->cap.max_rdma_ctxs = sq_size / 2; | |
33023fb8 SW |
1792 | qp_init->cap.max_send_sge = min(attrs->max_send_sge, |
1793 | SRPT_MAX_SG_PER_WQE); | |
5dabcd04 BVA |
1794 | qp_init->cap.max_recv_sge = min(attrs->max_recv_sge, |
1795 | SRPT_MAX_SG_PER_WQE); | |
b99f8e4d | 1796 | qp_init->port_num = ch->sport->port; |
dea26209 BVA |
1797 | if (sdev->use_srq) { |
1798 | qp_init->srq = sdev->srq; | |
1799 | } else { | |
1800 | qp_init->cap.max_recv_wr = ch->rq_size; | |
33023fb8 SW |
1801 | qp_init->cap.max_recv_sge = min(attrs->max_recv_sge, |
1802 | SRPT_MAX_SG_PER_WQE); | |
dea26209 | 1803 | } |
a42d985b | 1804 | |
63cf1a90 BVA |
1805 | if (ch->using_rdma_cm) { |
1806 | ret = rdma_create_qp(ch->rdma_cm.cm_id, sdev->pd, qp_init); | |
1807 | ch->qp = ch->rdma_cm.cm_id->qp; | |
1808 | } else { | |
1809 | ch->qp = ib_create_qp(sdev->pd, qp_init); | |
1810 | if (!IS_ERR(ch->qp)) { | |
1811 | ret = srpt_init_ch_qp(ch, ch->qp); | |
1812 | if (ret) | |
1813 | ib_destroy_qp(ch->qp); | |
1814 | } else { | |
1815 | ret = PTR_ERR(ch->qp); | |
1816 | } | |
1817 | } | |
1818 | if (ret) { | |
1819 | bool retry = sq_size > MIN_SRPT_SQ_SIZE; | |
1820 | ||
1821 | if (retry) { | |
1822 | pr_debug("failed to create queue pair with sq_size = %d (%d) - retrying\n", | |
1823 | sq_size, ret); | |
1824 | ib_free_cq(ch->cq); | |
1825 | sq_size = max(sq_size / 2, MIN_SRPT_SQ_SIZE); | |
1826 | goto retry; | |
1827 | } else { | |
1828 | pr_err("failed to create queue pair with sq_size = %d (%d)\n", | |
1829 | sq_size, ret); | |
1830 | goto err_destroy_cq; | |
ab477c1f | 1831 | } |
a42d985b BVA |
1832 | } |
1833 | ||
1834 | atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr); | |
1835 | ||
090fa24b | 1836 | pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d ch= %p\n", |
a42d985b | 1837 | __func__, ch->cq->cqe, qp_init->cap.max_send_sge, |
090fa24b | 1838 | qp_init->cap.max_send_wr, ch); |
a42d985b | 1839 | |
321e329b MM |
1840 | if (!sdev->use_srq) |
1841 | for (i = 0; i < ch->rq_size; i++) | |
1842 | srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]); | |
1843 | ||
a42d985b BVA |
1844 | out: |
1845 | kfree(qp_init); | |
1846 | return ret; | |
1847 | ||
a42d985b | 1848 | err_destroy_cq: |
63cf1a90 | 1849 | ch->qp = NULL; |
59fae4de | 1850 | ib_free_cq(ch->cq); |
a42d985b BVA |
1851 | goto out; |
1852 | } | |
1853 | ||
1854 | static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch) | |
1855 | { | |
a42d985b | 1856 | ib_destroy_qp(ch->qp); |
59fae4de | 1857 | ib_free_cq(ch->cq); |
a42d985b BVA |
1858 | } |
1859 | ||
1860 | /** | |
10eac19b BVA |
1861 | * srpt_close_ch - close a RDMA channel |
1862 | * @ch: SRPT RDMA channel. | |
a42d985b | 1863 | * |
aaf45bd8 BVA |
1864 | * Make sure all resources associated with the channel will be deallocated at |
1865 | * an appropriate time. | |
a42d985b | 1866 | * |
aaf45bd8 BVA |
1867 | * Returns true if and only if the channel state has been modified into |
1868 | * CH_DRAINING. | |
a42d985b | 1869 | */ |
aaf45bd8 | 1870 | static bool srpt_close_ch(struct srpt_rdma_ch *ch) |
a42d985b | 1871 | { |
aaf45bd8 | 1872 | int ret; |
a42d985b | 1873 | |
aaf45bd8 | 1874 | if (!srpt_set_ch_state(ch, CH_DRAINING)) { |
99525095 | 1875 | pr_debug("%s: already closed\n", ch->sess_name); |
aaf45bd8 | 1876 | return false; |
a42d985b | 1877 | } |
a42d985b | 1878 | |
aaf45bd8 | 1879 | kref_get(&ch->kref); |
a42d985b | 1880 | |
aaf45bd8 BVA |
1881 | ret = srpt_ch_qp_err(ch); |
1882 | if (ret < 0) | |
1883 | pr_err("%s-%d: changing queue pair into error state failed: %d\n", | |
1884 | ch->sess_name, ch->qp->qp_num, ret); | |
a42d985b | 1885 | |
aaf45bd8 BVA |
1886 | ret = srpt_zerolength_write(ch); |
1887 | if (ret < 0) { | |
1888 | pr_err("%s-%d: queuing zero-length write failed: %d\n", | |
1889 | ch->sess_name, ch->qp->qp_num, ret); | |
1890 | if (srpt_set_ch_state(ch, CH_DISCONNECTED)) | |
1891 | schedule_work(&ch->release_work); | |
1892 | else | |
1893 | WARN_ON_ONCE(true); | |
1894 | } | |
a42d985b | 1895 | |
aaf45bd8 BVA |
1896 | kref_put(&ch->kref, srpt_free_ch); |
1897 | ||
1898 | return true; | |
1d19f780 NB |
1899 | } |
1900 | ||
aaf45bd8 BVA |
1901 | /* |
1902 | * Change the channel state into CH_DISCONNECTING. If a channel has not yet | |
1903 | * reached the connected state, close it. If a channel is in the connected | |
1904 | * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is | |
1905 | * the responsibility of the caller to ensure that this function is not | |
1906 | * invoked concurrently with the code that accepts a connection. This means | |
1907 | * that this function must either be invoked from inside a CM callback | |
1908 | * function or that it must be invoked with the srpt_port.mutex held. | |
a42d985b | 1909 | */ |
aaf45bd8 | 1910 | static int srpt_disconnect_ch(struct srpt_rdma_ch *ch) |
a42d985b | 1911 | { |
a42d985b | 1912 | int ret; |
a42d985b | 1913 | |
aaf45bd8 BVA |
1914 | if (!srpt_set_ch_state(ch, CH_DISCONNECTING)) |
1915 | return -ENOTCONN; | |
a42d985b | 1916 | |
63cf1a90 BVA |
1917 | if (ch->using_rdma_cm) { |
1918 | ret = rdma_disconnect(ch->rdma_cm.cm_id); | |
1919 | } else { | |
1920 | ret = ib_send_cm_dreq(ch->ib_cm.cm_id, NULL, 0); | |
1921 | if (ret < 0) | |
1922 | ret = ib_send_cm_drep(ch->ib_cm.cm_id, NULL, 0); | |
1923 | } | |
a42d985b | 1924 | |
aaf45bd8 BVA |
1925 | if (ret < 0 && srpt_close_ch(ch)) |
1926 | ret = 0; | |
1d19f780 | 1927 | |
aaf45bd8 BVA |
1928 | return ret; |
1929 | } | |
1930 | ||
ba60c84f | 1931 | static bool srpt_ch_closed(struct srpt_port *sport, struct srpt_rdma_ch *ch) |
44138344 | 1932 | { |
a1125314 | 1933 | struct srpt_nexus *nexus; |
44138344 BVA |
1934 | struct srpt_rdma_ch *ch2; |
1935 | bool res = true; | |
1936 | ||
1937 | rcu_read_lock(); | |
a1125314 BVA |
1938 | list_for_each_entry(nexus, &sport->nexus_list, entry) { |
1939 | list_for_each_entry(ch2, &nexus->ch_list, list) { | |
1940 | if (ch2 == ch) { | |
1941 | res = false; | |
1942 | goto done; | |
1943 | } | |
44138344 BVA |
1944 | } |
1945 | } | |
a1125314 | 1946 | done: |
44138344 BVA |
1947 | rcu_read_unlock(); |
1948 | ||
1949 | return res; | |
1950 | } | |
1951 | ||
940874f8 BVA |
1952 | /* Send DREQ and wait for DREP. */ |
1953 | static void srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch) | |
aaf45bd8 | 1954 | { |
ba60c84f | 1955 | struct srpt_port *sport = ch->sport; |
01b3ee13 BVA |
1956 | |
1957 | pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num, | |
1958 | ch->state); | |
1959 | ||
940874f8 BVA |
1960 | mutex_lock(&sport->mutex); |
1961 | srpt_disconnect_ch(ch); | |
ba60c84f | 1962 | mutex_unlock(&sport->mutex); |
01b3ee13 | 1963 | |
ba60c84f | 1964 | while (wait_event_timeout(sport->ch_releaseQ, srpt_ch_closed(sport, ch), |
44138344 | 1965 | 5 * HZ) == 0) |
01b3ee13 BVA |
1966 | pr_info("%s(%s-%d state %d): still waiting ...\n", __func__, |
1967 | ch->sess_name, ch->qp->qp_num, ch->state); | |
1968 | ||
01b3ee13 BVA |
1969 | } |
1970 | ||
a1125314 | 1971 | static void __srpt_close_all_ch(struct srpt_port *sport) |
aaf45bd8 | 1972 | { |
a1125314 | 1973 | struct srpt_nexus *nexus; |
aaf45bd8 BVA |
1974 | struct srpt_rdma_ch *ch; |
1975 | ||
ba60c84f | 1976 | lockdep_assert_held(&sport->mutex); |
aaf45bd8 | 1977 | |
a1125314 BVA |
1978 | list_for_each_entry(nexus, &sport->nexus_list, entry) { |
1979 | list_for_each_entry(ch, &nexus->ch_list, list) { | |
1980 | if (srpt_disconnect_ch(ch) >= 0) | |
14d15c2b BVA |
1981 | pr_info("Closing channel %s because target %s_%d has been disabled\n", |
1982 | ch->sess_name, | |
6c854111 JG |
1983 | dev_name(&sport->sdev->device->dev), |
1984 | sport->port); | |
a1125314 BVA |
1985 | srpt_close_ch(ch); |
1986 | } | |
1987 | } | |
1988 | } | |
1989 | ||
1990 | /* | |
1991 | * Look up (i_port_id, t_port_id) in sport->nexus_list. Create an entry if | |
1992 | * it does not yet exist. | |
1993 | */ | |
1994 | static struct srpt_nexus *srpt_get_nexus(struct srpt_port *sport, | |
1995 | const u8 i_port_id[16], | |
1996 | const u8 t_port_id[16]) | |
1997 | { | |
1998 | struct srpt_nexus *nexus = NULL, *tmp_nexus = NULL, *n; | |
8b6dc529 | 1999 | |
a1125314 BVA |
2000 | for (;;) { |
2001 | mutex_lock(&sport->mutex); | |
2002 | list_for_each_entry(n, &sport->nexus_list, entry) { | |
2003 | if (memcmp(n->i_port_id, i_port_id, 16) == 0 && | |
2004 | memcmp(n->t_port_id, t_port_id, 16) == 0) { | |
2005 | nexus = n; | |
2006 | break; | |
2007 | } | |
2008 | } | |
2009 | if (!nexus && tmp_nexus) { | |
2010 | list_add_tail_rcu(&tmp_nexus->entry, | |
2011 | &sport->nexus_list); | |
2012 | swap(nexus, tmp_nexus); | |
2013 | } | |
2014 | mutex_unlock(&sport->mutex); | |
2015 | ||
2016 | if (nexus) | |
2017 | break; | |
2018 | tmp_nexus = kzalloc(sizeof(*nexus), GFP_KERNEL); | |
2019 | if (!tmp_nexus) { | |
2020 | nexus = ERR_PTR(-ENOMEM); | |
2021 | break; | |
8b6dc529 | 2022 | } |
a1125314 BVA |
2023 | INIT_LIST_HEAD(&tmp_nexus->ch_list); |
2024 | memcpy(tmp_nexus->i_port_id, i_port_id, 16); | |
2025 | memcpy(tmp_nexus->t_port_id, t_port_id, 16); | |
a42d985b | 2026 | } |
8b6dc529 | 2027 | |
a1125314 BVA |
2028 | kfree(tmp_nexus); |
2029 | ||
2030 | return nexus; | |
2031 | } | |
2032 | ||
2033 | static void srpt_set_enabled(struct srpt_port *sport, bool enabled) | |
2034 | __must_hold(&sport->mutex) | |
2035 | { | |
2036 | lockdep_assert_held(&sport->mutex); | |
2037 | ||
2038 | if (sport->enabled == enabled) | |
2039 | return; | |
2040 | sport->enabled = enabled; | |
2041 | if (!enabled) | |
2042 | __srpt_close_all_ch(sport); | |
a42d985b BVA |
2043 | } |
2044 | ||
aaf45bd8 BVA |
2045 | static void srpt_free_ch(struct kref *kref) |
2046 | { | |
2047 | struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref); | |
2048 | ||
795bc112 | 2049 | kfree_rcu(ch, rcu); |
a42d985b BVA |
2050 | } |
2051 | ||
ed041919 BVA |
2052 | /* |
2053 | * Shut down the SCSI target session, tell the connection manager to | |
2054 | * disconnect the associated RDMA channel, transition the QP to the error | |
2055 | * state and remove the channel from the channel list. This function is | |
2056 | * typically called from inside srpt_zerolength_write_done(). Concurrent | |
2057 | * srpt_zerolength_write() calls from inside srpt_close_ch() are possible | |
2058 | * as long as the channel is on sport->nexus_list. | |
2059 | */ | |
a42d985b BVA |
2060 | static void srpt_release_channel_work(struct work_struct *w) |
2061 | { | |
2062 | struct srpt_rdma_ch *ch; | |
2063 | struct srpt_device *sdev; | |
ba60c84f | 2064 | struct srpt_port *sport; |
9474b043 | 2065 | struct se_session *se_sess; |
a42d985b BVA |
2066 | |
2067 | ch = container_of(w, struct srpt_rdma_ch, release_work); | |
44138344 | 2068 | pr_debug("%s-%d\n", ch->sess_name, ch->qp->qp_num); |
a42d985b BVA |
2069 | |
2070 | sdev = ch->sport->sdev; | |
2071 | BUG_ON(!sdev); | |
2072 | ||
9474b043 NB |
2073 | se_sess = ch->sess; |
2074 | BUG_ON(!se_sess); | |
2075 | ||
88936259 | 2076 | target_sess_cmd_list_set_waiting(se_sess); |
be646c2d | 2077 | target_wait_for_sess_cmds(se_sess); |
9474b043 | 2078 | |
b287e351 | 2079 | target_remove_session(se_sess); |
a42d985b BVA |
2080 | ch->sess = NULL; |
2081 | ||
63cf1a90 BVA |
2082 | if (ch->using_rdma_cm) |
2083 | rdma_destroy_id(ch->rdma_cm.cm_id); | |
2084 | else | |
2085 | ib_destroy_cm_id(ch->ib_cm.cm_id); | |
0b41d6ca | 2086 | |
ed041919 BVA |
2087 | sport = ch->sport; |
2088 | mutex_lock(&sport->mutex); | |
2089 | list_del_rcu(&ch->list); | |
2090 | mutex_unlock(&sport->mutex); | |
2091 | ||
a42d985b BVA |
2092 | srpt_destroy_ch_ib(ch); |
2093 | ||
2094 | srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring, | |
2095 | ch->sport->sdev, ch->rq_size, | |
5dabcd04 BVA |
2096 | ch->rsp_buf_cache, DMA_TO_DEVICE); |
2097 | ||
2098 | kmem_cache_destroy(ch->rsp_buf_cache); | |
a42d985b | 2099 | |
dea26209 BVA |
2100 | srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring, |
2101 | sdev, ch->rq_size, | |
5dabcd04 | 2102 | ch->req_buf_cache, DMA_FROM_DEVICE); |
dea26209 | 2103 | |
5dabcd04 | 2104 | kmem_cache_destroy(ch->req_buf_cache); |
795bc112 | 2105 | |
ba60c84f | 2106 | wake_up(&sport->ch_releaseQ); |
a42d985b | 2107 | |
aaf45bd8 | 2108 | kref_put(&ch->kref, srpt_free_ch); |
a42d985b BVA |
2109 | } |
2110 | ||
a42d985b | 2111 | /** |
10eac19b | 2112 | * srpt_cm_req_recv - process the event IB_CM_REQ_RECEIVED |
63cf1a90 BVA |
2113 | * @sdev: HCA through which the login request was received. |
2114 | * @ib_cm_id: IB/CM connection identifier in case of IB/CM. | |
2115 | * @rdma_cm_id: RDMA/CM connection identifier in case of RDMA/CM. | |
2116 | * @port_num: Port through which the REQ message was received. | |
2ffcf042 BVA |
2117 | * @pkey: P_Key of the incoming connection. |
2118 | * @req: SRP login request. | |
63cf1a90 BVA |
2119 | * @src_addr: GID (IB/CM) or IP address (RDMA/CM) of the port that submitted |
2120 | * the login request. | |
a42d985b BVA |
2121 | * |
2122 | * Ownership of the cm_id is transferred to the target session if this | |
63cf1a90 | 2123 | * function returns zero. Otherwise the caller remains the owner of cm_id. |
a42d985b | 2124 | */ |
63cf1a90 BVA |
2125 | static int srpt_cm_req_recv(struct srpt_device *const sdev, |
2126 | struct ib_cm_id *ib_cm_id, | |
2127 | struct rdma_cm_id *rdma_cm_id, | |
2ffcf042 BVA |
2128 | u8 port_num, __be16 pkey, |
2129 | const struct srp_login_req *req, | |
2130 | const char *src_addr) | |
a42d985b | 2131 | { |
2ffcf042 | 2132 | struct srpt_port *sport = &sdev->port[port_num - 1]; |
a1125314 | 2133 | struct srpt_nexus *nexus; |
a1125314 BVA |
2134 | struct srp_login_rsp *rsp = NULL; |
2135 | struct srp_login_rej *rej = NULL; | |
63cf1a90 BVA |
2136 | union { |
2137 | struct rdma_conn_param rdma_cm; | |
2138 | struct ib_cm_rep_param ib_cm; | |
2139 | } *rep_param = NULL; | |
847462de | 2140 | struct srpt_rdma_ch *ch = NULL; |
2dc98f09 | 2141 | char i_port_id[36]; |
a42d985b | 2142 | u32 it_iu_len; |
fd1b6687 | 2143 | int i, tag_num, tag_size, ret; |
a42d985b BVA |
2144 | |
2145 | WARN_ON_ONCE(irqs_disabled()); | |
2146 | ||
2ffcf042 | 2147 | if (WARN_ON(!sdev || !req)) |
a42d985b BVA |
2148 | return -EINVAL; |
2149 | ||
a42d985b BVA |
2150 | it_iu_len = be32_to_cpu(req->req_it_iu_len); |
2151 | ||
c5efb621 | 2152 | pr_info("Received SRP_LOGIN_REQ with i_port_id %pI6, t_port_id %pI6 and it_iu_len %d on port %d (guid=%pI6); pkey %#04x\n", |
b185d3f8 | 2153 | req->initiator_port_id, req->target_port_id, it_iu_len, |
2ffcf042 | 2154 | port_num, &sport->gid, be16_to_cpu(pkey)); |
a42d985b | 2155 | |
a1125314 BVA |
2156 | nexus = srpt_get_nexus(sport, req->initiator_port_id, |
2157 | req->target_port_id); | |
2158 | if (IS_ERR(nexus)) { | |
2159 | ret = PTR_ERR(nexus); | |
2160 | goto out; | |
2161 | } | |
2162 | ||
db7683d7 | 2163 | ret = -ENOMEM; |
9d2aa2b4 BVA |
2164 | rsp = kzalloc(sizeof(*rsp), GFP_KERNEL); |
2165 | rej = kzalloc(sizeof(*rej), GFP_KERNEL); | |
2166 | rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL); | |
db7683d7 | 2167 | if (!rsp || !rej || !rep_param) |
a42d985b | 2168 | goto out; |
a42d985b | 2169 | |
db7683d7 | 2170 | ret = -EINVAL; |
a42d985b | 2171 | if (it_iu_len > srp_max_req_size || it_iu_len < 64) { |
b356c1c1 | 2172 | rej->reason = cpu_to_be32( |
db7683d7 BVA |
2173 | SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE); |
2174 | pr_err("rejected SRP_LOGIN_REQ because its length (%d bytes) is out of range (%d .. %d)\n", | |
a42d985b BVA |
2175 | it_iu_len, 64, srp_max_req_size); |
2176 | goto reject; | |
2177 | } | |
2178 | ||
2179 | if (!sport->enabled) { | |
db7683d7 BVA |
2180 | rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
2181 | pr_info("rejected SRP_LOGIN_REQ because target port %s_%d has not yet been enabled\n", | |
6c854111 | 2182 | dev_name(&sport->sdev->device->dev), port_num); |
a42d985b BVA |
2183 | goto reject; |
2184 | } | |
2185 | ||
a42d985b BVA |
2186 | if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid) |
2187 | || *(__be64 *)(req->target_port_id + 8) != | |
2188 | cpu_to_be64(srpt_service_guid)) { | |
b356c1c1 | 2189 | rej->reason = cpu_to_be32( |
db7683d7 BVA |
2190 | SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL); |
2191 | pr_err("rejected SRP_LOGIN_REQ because it has an invalid target port identifier.\n"); | |
a42d985b BVA |
2192 | goto reject; |
2193 | } | |
2194 | ||
db7683d7 | 2195 | ret = -ENOMEM; |
9d2aa2b4 | 2196 | ch = kzalloc(sizeof(*ch), GFP_KERNEL); |
a42d985b | 2197 | if (!ch) { |
db7683d7 BVA |
2198 | rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
2199 | pr_err("rejected SRP_LOGIN_REQ because out of memory.\n"); | |
a42d985b BVA |
2200 | goto reject; |
2201 | } | |
2202 | ||
aaf45bd8 | 2203 | kref_init(&ch->kref); |
2ffcf042 | 2204 | ch->pkey = be16_to_cpu(pkey); |
a1125314 | 2205 | ch->nexus = nexus; |
aaf45bd8 | 2206 | ch->zw_cqe.done = srpt_zerolength_write_done; |
a42d985b | 2207 | INIT_WORK(&ch->release_work, srpt_release_channel_work); |
2ffcf042 | 2208 | ch->sport = sport; |
63cf1a90 BVA |
2209 | if (ib_cm_id) { |
2210 | ch->ib_cm.cm_id = ib_cm_id; | |
2211 | ib_cm_id->context = ch; | |
2212 | } else { | |
2213 | ch->using_rdma_cm = true; | |
2214 | ch->rdma_cm.cm_id = rdma_cm_id; | |
2215 | rdma_cm_id->context = ch; | |
2216 | } | |
a42d985b | 2217 | /* |
7a01d05c BVA |
2218 | * ch->rq_size should be at least as large as the initiator queue |
2219 | * depth to avoid that the initiator driver has to report QUEUE_FULL | |
2220 | * to the SCSI mid-layer. | |
a42d985b | 2221 | */ |
ed262287 | 2222 | ch->rq_size = min(MAX_SRPT_RQ_SIZE, sdev->device->attrs.max_qp_wr); |
a42d985b BVA |
2223 | spin_lock_init(&ch->spinlock); |
2224 | ch->state = CH_CONNECTING; | |
2225 | INIT_LIST_HEAD(&ch->cmd_wait_list); | |
ed262287 | 2226 | ch->max_rsp_size = ch->sport->port_attrib.srp_max_rsp_size; |
a42d985b | 2227 | |
5dabcd04 BVA |
2228 | ch->rsp_buf_cache = kmem_cache_create("srpt-rsp-buf", ch->max_rsp_size, |
2229 | 512, 0, NULL); | |
2230 | if (!ch->rsp_buf_cache) | |
2231 | goto free_ch; | |
2232 | ||
a42d985b BVA |
2233 | ch->ioctx_ring = (struct srpt_send_ioctx **) |
2234 | srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size, | |
2235 | sizeof(*ch->ioctx_ring[0]), | |
5dabcd04 | 2236 | ch->rsp_buf_cache, 0, DMA_TO_DEVICE); |
db7683d7 BVA |
2237 | if (!ch->ioctx_ring) { |
2238 | pr_err("rejected SRP_LOGIN_REQ because creating a new QP SQ ring failed.\n"); | |
2239 | rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); | |
5dabcd04 | 2240 | goto free_rsp_cache; |
db7683d7 | 2241 | } |
a42d985b | 2242 | |
fd1b6687 | 2243 | for (i = 0; i < ch->rq_size; i++) |
3c968887 | 2244 | ch->ioctx_ring[i]->ch = ch; |
dea26209 | 2245 | if (!sdev->use_srq) { |
5dabcd04 BVA |
2246 | u16 imm_data_offset = req->req_flags & SRP_IMMED_REQUESTED ? |
2247 | be16_to_cpu(req->imm_data_offset) : 0; | |
2248 | u16 alignment_offset; | |
2249 | u32 req_sz; | |
2250 | ||
2251 | if (req->req_flags & SRP_IMMED_REQUESTED) | |
2252 | pr_debug("imm_data_offset = %d\n", | |
2253 | be16_to_cpu(req->imm_data_offset)); | |
2254 | if (imm_data_offset >= sizeof(struct srp_cmd)) { | |
2255 | ch->imm_data_offset = imm_data_offset; | |
2256 | rsp->rsp_flags |= SRP_LOGIN_RSP_IMMED_SUPP; | |
2257 | } else { | |
2258 | ch->imm_data_offset = 0; | |
2259 | } | |
2260 | alignment_offset = round_up(imm_data_offset, 512) - | |
2261 | imm_data_offset; | |
2262 | req_sz = alignment_offset + imm_data_offset + srp_max_req_size; | |
2263 | ch->req_buf_cache = kmem_cache_create("srpt-req-buf", req_sz, | |
2264 | 512, 0, NULL); | |
2265 | if (!ch->req_buf_cache) | |
2266 | goto free_rsp_ring; | |
2267 | ||
dea26209 BVA |
2268 | ch->ioctx_recv_ring = (struct srpt_recv_ioctx **) |
2269 | srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size, | |
2270 | sizeof(*ch->ioctx_recv_ring[0]), | |
5dabcd04 BVA |
2271 | ch->req_buf_cache, |
2272 | alignment_offset, | |
dea26209 BVA |
2273 | DMA_FROM_DEVICE); |
2274 | if (!ch->ioctx_recv_ring) { | |
2275 | pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n"); | |
2276 | rej->reason = | |
2277 | cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); | |
5dabcd04 | 2278 | goto free_recv_cache; |
dea26209 | 2279 | } |
fcf58936 BVA |
2280 | for (i = 0; i < ch->rq_size; i++) |
2281 | INIT_LIST_HEAD(&ch->ioctx_recv_ring[i]->wait_list); | |
dea26209 | 2282 | } |
3c968887 | 2283 | |
a42d985b | 2284 | ret = srpt_create_ch_ib(ch); |
a42d985b | 2285 | if (ret) { |
b356c1c1 | 2286 | rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); |
db7683d7 BVA |
2287 | pr_err("rejected SRP_LOGIN_REQ because creating a new RDMA channel failed.\n"); |
2288 | goto free_recv_ring; | |
a42d985b | 2289 | } |
f246c941 | 2290 | |
2ffcf042 | 2291 | strlcpy(ch->sess_name, src_addr, sizeof(ch->sess_name)); |
2dc98f09 | 2292 | snprintf(i_port_id, sizeof(i_port_id), "0x%016llx%016llx", |
a1125314 BVA |
2293 | be64_to_cpu(*(__be64 *)nexus->i_port_id), |
2294 | be64_to_cpu(*(__be64 *)(nexus->i_port_id + 8))); | |
a42d985b BVA |
2295 | |
2296 | pr_debug("registering session %s\n", ch->sess_name); | |
2297 | ||
fd1b6687 BVA |
2298 | tag_num = ch->rq_size; |
2299 | tag_size = 1; /* ib_srpt does not use se_sess->sess_cmd_map */ | |
2bce1a6d | 2300 | if (sport->port_guid_tpg.se_tpg_wwn) |
fd1b6687 BVA |
2301 | ch->sess = target_setup_session(&sport->port_guid_tpg, tag_num, |
2302 | tag_size, TARGET_PROT_NORMAL, | |
2dc98f09 | 2303 | ch->sess_name, ch, NULL); |
2bce1a6d | 2304 | if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess)) |
fd1b6687 BVA |
2305 | ch->sess = target_setup_session(&sport->port_gid_tpg, tag_num, |
2306 | tag_size, TARGET_PROT_NORMAL, i_port_id, | |
2307 | ch, NULL); | |
0d38c240 | 2308 | /* Retry without leading "0x" */ |
2bce1a6d | 2309 | if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess)) |
fd1b6687 BVA |
2310 | ch->sess = target_setup_session(&sport->port_gid_tpg, tag_num, |
2311 | tag_size, TARGET_PROT_NORMAL, | |
2dc98f09 | 2312 | i_port_id + 2, ch, NULL); |
2bce1a6d | 2313 | if (IS_ERR_OR_NULL(ch->sess)) { |
847462de | 2314 | WARN_ON_ONCE(ch->sess == NULL); |
db7683d7 | 2315 | ret = PTR_ERR(ch->sess); |
847462de | 2316 | ch->sess = NULL; |
db7683d7 BVA |
2317 | pr_info("Rejected login for initiator %s: ret = %d.\n", |
2318 | ch->sess_name, ret); | |
2319 | rej->reason = cpu_to_be32(ret == -ENOMEM ? | |
b42057ab | 2320 | SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES : |
f246c941 | 2321 | SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED); |
847462de | 2322 | goto destroy_ib; |
db7683d7 BVA |
2323 | } |
2324 | ||
2325 | mutex_lock(&sport->mutex); | |
2326 | ||
2327 | if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) { | |
2328 | struct srpt_rdma_ch *ch2; | |
2329 | ||
db7683d7 BVA |
2330 | list_for_each_entry(ch2, &nexus->ch_list, list) { |
2331 | if (srpt_disconnect_ch(ch2) < 0) | |
2332 | continue; | |
2333 | pr_info("Relogin - closed existing channel %s\n", | |
2334 | ch2->sess_name); | |
5dabcd04 | 2335 | rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_TERMINATED; |
db7683d7 BVA |
2336 | } |
2337 | } else { | |
5dabcd04 | 2338 | rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_MAINTAINED; |
db7683d7 BVA |
2339 | } |
2340 | ||
2341 | list_add_tail_rcu(&ch->list, &nexus->ch_list); | |
2342 | ||
2343 | if (!sport->enabled) { | |
2344 | rej->reason = cpu_to_be32( | |
2345 | SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); | |
2346 | pr_info("rejected SRP_LOGIN_REQ because target %s_%d is not enabled\n", | |
6c854111 | 2347 | dev_name(&sdev->device->dev), port_num); |
db7683d7 BVA |
2348 | mutex_unlock(&sport->mutex); |
2349 | goto reject; | |
2350 | } | |
2351 | ||
2352 | mutex_unlock(&sport->mutex); | |
2353 | ||
63cf1a90 | 2354 | ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rtr(ch, ch->qp); |
db7683d7 BVA |
2355 | if (ret) { |
2356 | rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); | |
2357 | pr_err("rejected SRP_LOGIN_REQ because enabling RTR failed (error code = %d)\n", | |
2358 | ret); | |
847462de | 2359 | goto reject; |
a42d985b | 2360 | } |
a42d985b | 2361 | |
090fa24b BVA |
2362 | pr_debug("Establish connection sess=%p name=%s ch=%p\n", ch->sess, |
2363 | ch->sess_name, ch); | |
a42d985b BVA |
2364 | |
2365 | /* create srp_login_response */ | |
2366 | rsp->opcode = SRP_LOGIN_RSP; | |
2367 | rsp->tag = req->tag; | |
5dabcd04 | 2368 | rsp->max_it_iu_len = cpu_to_be32(srp_max_req_size); |
a42d985b BVA |
2369 | rsp->max_ti_iu_len = req->req_it_iu_len; |
2370 | ch->max_ti_iu_len = it_iu_len; | |
db7683d7 BVA |
2371 | rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | |
2372 | SRP_BUF_FORMAT_INDIRECT); | |
a42d985b BVA |
2373 | rsp->req_lim_delta = cpu_to_be32(ch->rq_size); |
2374 | atomic_set(&ch->req_lim, ch->rq_size); | |
2375 | atomic_set(&ch->req_lim_delta, 0); | |
2376 | ||
2377 | /* create cm reply */ | |
63cf1a90 BVA |
2378 | if (ch->using_rdma_cm) { |
2379 | rep_param->rdma_cm.private_data = (void *)rsp; | |
2380 | rep_param->rdma_cm.private_data_len = sizeof(*rsp); | |
2381 | rep_param->rdma_cm.rnr_retry_count = 7; | |
2382 | rep_param->rdma_cm.flow_control = 1; | |
2383 | rep_param->rdma_cm.responder_resources = 4; | |
2384 | rep_param->rdma_cm.initiator_depth = 4; | |
2385 | } else { | |
2386 | rep_param->ib_cm.qp_num = ch->qp->qp_num; | |
2387 | rep_param->ib_cm.private_data = (void *)rsp; | |
2388 | rep_param->ib_cm.private_data_len = sizeof(*rsp); | |
2389 | rep_param->ib_cm.rnr_retry_count = 7; | |
2390 | rep_param->ib_cm.flow_control = 1; | |
2391 | rep_param->ib_cm.failover_accepted = 0; | |
2392 | rep_param->ib_cm.srq = 1; | |
2393 | rep_param->ib_cm.responder_resources = 4; | |
2394 | rep_param->ib_cm.initiator_depth = 4; | |
2395 | } | |
a42d985b | 2396 | |
db7683d7 BVA |
2397 | /* |
2398 | * Hold the sport mutex while accepting a connection to avoid that | |
2399 | * srpt_disconnect_ch() is invoked concurrently with this code. | |
2400 | */ | |
ba60c84f | 2401 | mutex_lock(&sport->mutex); |
63cf1a90 BVA |
2402 | if (sport->enabled && ch->state == CH_CONNECTING) { |
2403 | if (ch->using_rdma_cm) | |
2404 | ret = rdma_accept(rdma_cm_id, &rep_param->rdma_cm); | |
2405 | else | |
2406 | ret = ib_send_cm_rep(ib_cm_id, &rep_param->ib_cm); | |
2407 | } else { | |
db7683d7 | 2408 | ret = -EINVAL; |
63cf1a90 | 2409 | } |
ba60c84f | 2410 | mutex_unlock(&sport->mutex); |
a42d985b | 2411 | |
db7683d7 BVA |
2412 | switch (ret) { |
2413 | case 0: | |
2414 | break; | |
2415 | case -EINVAL: | |
2416 | goto reject; | |
2417 | default: | |
2418 | rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); | |
2419 | pr_err("sending SRP_LOGIN_REQ response failed (error code = %d)\n", | |
2420 | ret); | |
2421 | goto reject; | |
2422 | } | |
a42d985b | 2423 | |
db7683d7 | 2424 | goto out; |
a42d985b BVA |
2425 | |
2426 | destroy_ib: | |
2427 | srpt_destroy_ch_ib(ch); | |
2428 | ||
dea26209 BVA |
2429 | free_recv_ring: |
2430 | srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring, | |
2431 | ch->sport->sdev, ch->rq_size, | |
5dabcd04 | 2432 | ch->req_buf_cache, DMA_FROM_DEVICE); |
dea26209 | 2433 | |
5dabcd04 BVA |
2434 | free_recv_cache: |
2435 | kmem_cache_destroy(ch->req_buf_cache); | |
dea26209 | 2436 | |
5dabcd04 | 2437 | free_rsp_ring: |
a42d985b BVA |
2438 | srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring, |
2439 | ch->sport->sdev, ch->rq_size, | |
5dabcd04 BVA |
2440 | ch->rsp_buf_cache, DMA_TO_DEVICE); |
2441 | ||
2442 | free_rsp_cache: | |
2443 | kmem_cache_destroy(ch->rsp_buf_cache); | |
6869e000 | 2444 | |
a42d985b | 2445 | free_ch: |
6869e000 BVA |
2446 | if (rdma_cm_id) |
2447 | rdma_cm_id->context = NULL; | |
2448 | else | |
63cf1a90 | 2449 | ib_cm_id->context = NULL; |
a42d985b | 2450 | kfree(ch); |
db7683d7 BVA |
2451 | ch = NULL; |
2452 | ||
2453 | WARN_ON_ONCE(ret == 0); | |
a42d985b BVA |
2454 | |
2455 | reject: | |
db7683d7 | 2456 | pr_info("Rejecting login with reason %#x\n", be32_to_cpu(rej->reason)); |
a42d985b BVA |
2457 | rej->opcode = SRP_LOGIN_REJ; |
2458 | rej->tag = req->tag; | |
db7683d7 BVA |
2459 | rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | |
2460 | SRP_BUF_FORMAT_INDIRECT); | |
a42d985b | 2461 | |
63cf1a90 BVA |
2462 | if (rdma_cm_id) |
2463 | rdma_reject(rdma_cm_id, rej, sizeof(*rej)); | |
2464 | else | |
2465 | ib_send_cm_rej(ib_cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0, | |
2466 | rej, sizeof(*rej)); | |
a42d985b | 2467 | |
847462de BVA |
2468 | if (ch && ch->sess) { |
2469 | srpt_close_ch(ch); | |
2470 | /* | |
2471 | * Tell the caller not to free cm_id since | |
2472 | * srpt_release_channel_work() will do that. | |
2473 | */ | |
2474 | ret = 0; | |
2475 | } | |
2476 | ||
a42d985b BVA |
2477 | out: |
2478 | kfree(rep_param); | |
2479 | kfree(rsp); | |
2480 | kfree(rej); | |
2481 | ||
2482 | return ret; | |
2483 | } | |
2484 | ||
2ffcf042 | 2485 | static int srpt_ib_cm_req_recv(struct ib_cm_id *cm_id, |
e7ff98ae | 2486 | const struct ib_cm_req_event_param *param, |
2ffcf042 BVA |
2487 | void *private_data) |
2488 | { | |
2489 | char sguid[40]; | |
2490 | ||
2491 | srpt_format_guid(sguid, sizeof(sguid), | |
2492 | ¶m->primary_path->dgid.global.interface_id); | |
2493 | ||
63cf1a90 BVA |
2494 | return srpt_cm_req_recv(cm_id->context, cm_id, NULL, param->port, |
2495 | param->primary_path->pkey, | |
2ffcf042 BVA |
2496 | private_data, sguid); |
2497 | } | |
2498 | ||
63cf1a90 BVA |
2499 | static int srpt_rdma_cm_req_recv(struct rdma_cm_id *cm_id, |
2500 | struct rdma_cm_event *event) | |
2501 | { | |
2502 | struct srpt_device *sdev; | |
2503 | struct srp_login_req req; | |
2504 | const struct srp_login_req_rdma *req_rdma; | |
cbca2442 | 2505 | struct sa_path_rec *path_rec = cm_id->route.path_rec; |
63cf1a90 BVA |
2506 | char src_addr[40]; |
2507 | ||
2508 | sdev = ib_get_client_data(cm_id->device, &srpt_client); | |
2509 | if (!sdev) | |
2510 | return -ECONNREFUSED; | |
2511 | ||
2512 | if (event->param.conn.private_data_len < sizeof(*req_rdma)) | |
2513 | return -EINVAL; | |
2514 | ||
2515 | /* Transform srp_login_req_rdma into srp_login_req. */ | |
2516 | req_rdma = event->param.conn.private_data; | |
2517 | memset(&req, 0, sizeof(req)); | |
2518 | req.opcode = req_rdma->opcode; | |
2519 | req.tag = req_rdma->tag; | |
2520 | req.req_it_iu_len = req_rdma->req_it_iu_len; | |
2521 | req.req_buf_fmt = req_rdma->req_buf_fmt; | |
2522 | req.req_flags = req_rdma->req_flags; | |
2523 | memcpy(req.initiator_port_id, req_rdma->initiator_port_id, 16); | |
2524 | memcpy(req.target_port_id, req_rdma->target_port_id, 16); | |
5dabcd04 | 2525 | req.imm_data_offset = req_rdma->imm_data_offset; |
63cf1a90 BVA |
2526 | |
2527 | snprintf(src_addr, sizeof(src_addr), "%pIS", | |
2528 | &cm_id->route.addr.src_addr); | |
2529 | ||
2530 | return srpt_cm_req_recv(sdev, NULL, cm_id, cm_id->port_num, | |
cbca2442 | 2531 | path_rec ? path_rec->pkey : 0, &req, src_addr); |
63cf1a90 BVA |
2532 | } |
2533 | ||
2739b592 BVA |
2534 | static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch, |
2535 | enum ib_cm_rej_reason reason, | |
2536 | const u8 *private_data, | |
2537 | u8 private_data_len) | |
a42d985b | 2538 | { |
c13c90ea BVA |
2539 | char *priv = NULL; |
2540 | int i; | |
2541 | ||
2542 | if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1, | |
2543 | GFP_KERNEL))) { | |
2544 | for (i = 0; i < private_data_len; i++) | |
2545 | sprintf(priv + 3 * i, " %02x", private_data[i]); | |
2546 | } | |
2547 | pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n", | |
2548 | ch->sess_name, ch->qp->qp_num, reason, private_data_len ? | |
2549 | "; private data" : "", priv ? priv : " (?)"); | |
2550 | kfree(priv); | |
a42d985b BVA |
2551 | } |
2552 | ||
2553 | /** | |
10eac19b BVA |
2554 | * srpt_cm_rtu_recv - process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event |
2555 | * @ch: SRPT RDMA channel. | |
a42d985b | 2556 | * |
63cf1a90 BVA |
2557 | * An RTU (ready to use) message indicates that the connection has been |
2558 | * established and that the recipient may begin transmitting. | |
a42d985b | 2559 | */ |
2739b592 | 2560 | static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch) |
a42d985b | 2561 | { |
a42d985b BVA |
2562 | int ret; |
2563 | ||
63cf1a90 | 2564 | ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rts(ch, ch->qp); |
db7683d7 BVA |
2565 | if (ret < 0) { |
2566 | pr_err("%s-%d: QP transition to RTS failed\n", ch->sess_name, | |
2567 | ch->qp->qp_num); | |
2568 | srpt_close_ch(ch); | |
2569 | return; | |
a42d985b | 2570 | } |
db7683d7 | 2571 | |
db7683d7 BVA |
2572 | /* |
2573 | * Note: calling srpt_close_ch() if the transition to the LIVE state | |
2574 | * fails is not necessary since that means that that function has | |
2575 | * already been invoked from another thread. | |
2576 | */ | |
e28a547d | 2577 | if (!srpt_set_ch_state(ch, CH_LIVE)) { |
db7683d7 BVA |
2578 | pr_err("%s-%d: channel transition to LIVE state failed\n", |
2579 | ch->sess_name, ch->qp->qp_num); | |
e28a547d BVA |
2580 | return; |
2581 | } | |
2582 | ||
2583 | /* Trigger wait list processing. */ | |
2584 | ret = srpt_zerolength_write(ch); | |
2585 | WARN_ONCE(ret < 0, "%d\n", ret); | |
a42d985b BVA |
2586 | } |
2587 | ||
a42d985b | 2588 | /** |
10eac19b BVA |
2589 | * srpt_cm_handler - IB connection manager callback function |
2590 | * @cm_id: IB/CM connection identifier. | |
2591 | * @event: IB/CM event. | |
a42d985b BVA |
2592 | * |
2593 | * A non-zero return value will cause the caller destroy the CM ID. | |
2594 | * | |
2595 | * Note: srpt_cm_handler() must only return a non-zero value when transferring | |
2596 | * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning | |
2597 | * a non-zero value in any other case will trigger a race with the | |
2598 | * ib_destroy_cm_id() call in srpt_release_channel(). | |
2599 | */ | |
e7ff98ae PP |
2600 | static int srpt_cm_handler(struct ib_cm_id *cm_id, |
2601 | const struct ib_cm_event *event) | |
a42d985b | 2602 | { |
2739b592 | 2603 | struct srpt_rdma_ch *ch = cm_id->context; |
a42d985b BVA |
2604 | int ret; |
2605 | ||
2606 | ret = 0; | |
2607 | switch (event->event) { | |
2608 | case IB_CM_REQ_RECEIVED: | |
2ffcf042 BVA |
2609 | ret = srpt_ib_cm_req_recv(cm_id, &event->param.req_rcvd, |
2610 | event->private_data); | |
a42d985b BVA |
2611 | break; |
2612 | case IB_CM_REJ_RECEIVED: | |
2739b592 BVA |
2613 | srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason, |
2614 | event->private_data, | |
2615 | IB_CM_REJ_PRIVATE_DATA_SIZE); | |
a42d985b BVA |
2616 | break; |
2617 | case IB_CM_RTU_RECEIVED: | |
2618 | case IB_CM_USER_ESTABLISHED: | |
2739b592 | 2619 | srpt_cm_rtu_recv(ch); |
a42d985b BVA |
2620 | break; |
2621 | case IB_CM_DREQ_RECEIVED: | |
aaf45bd8 | 2622 | srpt_disconnect_ch(ch); |
a42d985b BVA |
2623 | break; |
2624 | case IB_CM_DREP_RECEIVED: | |
2739b592 BVA |
2625 | pr_info("Received CM DREP message for ch %s-%d.\n", |
2626 | ch->sess_name, ch->qp->qp_num); | |
aaf45bd8 | 2627 | srpt_close_ch(ch); |
a42d985b BVA |
2628 | break; |
2629 | case IB_CM_TIMEWAIT_EXIT: | |
2739b592 BVA |
2630 | pr_info("Received CM TimeWait exit for ch %s-%d.\n", |
2631 | ch->sess_name, ch->qp->qp_num); | |
aaf45bd8 | 2632 | srpt_close_ch(ch); |
a42d985b BVA |
2633 | break; |
2634 | case IB_CM_REP_ERROR: | |
2739b592 BVA |
2635 | pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name, |
2636 | ch->qp->qp_num); | |
a42d985b BVA |
2637 | break; |
2638 | case IB_CM_DREQ_ERROR: | |
1e20a2a5 | 2639 | pr_info("Received CM DREQ ERROR event.\n"); |
a42d985b BVA |
2640 | break; |
2641 | case IB_CM_MRA_RECEIVED: | |
1e20a2a5 | 2642 | pr_info("Received CM MRA event\n"); |
a42d985b BVA |
2643 | break; |
2644 | default: | |
1e20a2a5 | 2645 | pr_err("received unrecognized CM event %d\n", event->event); |
a42d985b BVA |
2646 | break; |
2647 | } | |
2648 | ||
2649 | return ret; | |
2650 | } | |
2651 | ||
63cf1a90 BVA |
2652 | static int srpt_rdma_cm_handler(struct rdma_cm_id *cm_id, |
2653 | struct rdma_cm_event *event) | |
2654 | { | |
2655 | struct srpt_rdma_ch *ch = cm_id->context; | |
2656 | int ret = 0; | |
2657 | ||
2658 | switch (event->event) { | |
2659 | case RDMA_CM_EVENT_CONNECT_REQUEST: | |
2660 | ret = srpt_rdma_cm_req_recv(cm_id, event); | |
2661 | break; | |
2662 | case RDMA_CM_EVENT_REJECTED: | |
2663 | srpt_cm_rej_recv(ch, event->status, | |
2664 | event->param.conn.private_data, | |
2665 | event->param.conn.private_data_len); | |
2666 | break; | |
2667 | case RDMA_CM_EVENT_ESTABLISHED: | |
2668 | srpt_cm_rtu_recv(ch); | |
2669 | break; | |
2670 | case RDMA_CM_EVENT_DISCONNECTED: | |
2671 | if (ch->state < CH_DISCONNECTING) | |
2672 | srpt_disconnect_ch(ch); | |
2673 | else | |
2674 | srpt_close_ch(ch); | |
2675 | break; | |
2676 | case RDMA_CM_EVENT_TIMEWAIT_EXIT: | |
2677 | srpt_close_ch(ch); | |
2678 | break; | |
2679 | case RDMA_CM_EVENT_UNREACHABLE: | |
2680 | pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name, | |
2681 | ch->qp->qp_num); | |
2682 | break; | |
2683 | case RDMA_CM_EVENT_DEVICE_REMOVAL: | |
2684 | case RDMA_CM_EVENT_ADDR_CHANGE: | |
2685 | break; | |
2686 | default: | |
2687 | pr_err("received unrecognized RDMA CM event %d\n", | |
2688 | event->event); | |
2689 | break; | |
2690 | } | |
2691 | ||
2692 | return ret; | |
2693 | } | |
2694 | ||
a42d985b | 2695 | /* |
10eac19b | 2696 | * srpt_write_pending - Start data transfer from initiator to target (write). |
a42d985b BVA |
2697 | */ |
2698 | static int srpt_write_pending(struct se_cmd *se_cmd) | |
2699 | { | |
fc3af58d BVA |
2700 | struct srpt_send_ioctx *ioctx = |
2701 | container_of(se_cmd, struct srpt_send_ioctx, cmd); | |
2702 | struct srpt_rdma_ch *ch = ioctx->ch; | |
9b32a596 | 2703 | struct ib_send_wr *first_wr = NULL; |
b99f8e4d | 2704 | struct ib_cqe *cqe = &ioctx->rdma_cqe; |
a42d985b | 2705 | enum srpt_command_state new_state; |
b99f8e4d | 2706 | int ret, i; |
a42d985b | 2707 | |
5dabcd04 BVA |
2708 | if (ioctx->recv_ioctx) { |
2709 | srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN); | |
2710 | target_execute_cmd(&ioctx->cmd); | |
2711 | return 0; | |
2712 | } | |
2713 | ||
a42d985b BVA |
2714 | new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA); |
2715 | WARN_ON(new_state == SRPT_STATE_DONE); | |
b99f8e4d CH |
2716 | |
2717 | if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) { | |
2718 | pr_warn("%s: IB send queue full (needed %d)\n", | |
2719 | __func__, ioctx->n_rdma); | |
2720 | ret = -ENOMEM; | |
2721 | goto out_undo; | |
2722 | } | |
2723 | ||
2724 | cqe->done = srpt_rdma_read_done; | |
2725 | for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) { | |
2726 | struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; | |
2727 | ||
2728 | first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port, | |
2729 | cqe, first_wr); | |
2730 | cqe = NULL; | |
2731 | } | |
dcc9881e | 2732 | |
9b32a596 | 2733 | ret = ib_post_send(ch->qp, first_wr, NULL); |
b99f8e4d CH |
2734 | if (ret) { |
2735 | pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n", | |
2736 | __func__, ret, ioctx->n_rdma, | |
2737 | atomic_read(&ch->sq_wr_avail)); | |
2738 | goto out_undo; | |
2739 | } | |
2740 | ||
2741 | return 0; | |
2742 | out_undo: | |
2743 | atomic_add(ioctx->n_rdma, &ch->sq_wr_avail); | |
2744 | return ret; | |
a42d985b BVA |
2745 | } |
2746 | ||
2747 | static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status) | |
2748 | { | |
2749 | switch (tcm_mgmt_status) { | |
2750 | case TMR_FUNCTION_COMPLETE: | |
2751 | return SRP_TSK_MGMT_SUCCESS; | |
2752 | case TMR_FUNCTION_REJECTED: | |
2753 | return SRP_TSK_MGMT_FUNC_NOT_SUPP; | |
2754 | } | |
2755 | return SRP_TSK_MGMT_FAILED; | |
2756 | } | |
2757 | ||
2758 | /** | |
10eac19b BVA |
2759 | * srpt_queue_response - transmit the response to a SCSI command |
2760 | * @cmd: SCSI target command. | |
a42d985b BVA |
2761 | * |
2762 | * Callback function called by the TCM core. Must not block since it can be | |
2763 | * invoked on the context of the IB completion handler. | |
2764 | */ | |
b79fafac | 2765 | static void srpt_queue_response(struct se_cmd *cmd) |
a42d985b | 2766 | { |
b99f8e4d CH |
2767 | struct srpt_send_ioctx *ioctx = |
2768 | container_of(cmd, struct srpt_send_ioctx, cmd); | |
2769 | struct srpt_rdma_ch *ch = ioctx->ch; | |
2770 | struct srpt_device *sdev = ch->sport->sdev; | |
9b32a596 | 2771 | struct ib_send_wr send_wr, *first_wr = &send_wr; |
b99f8e4d | 2772 | struct ib_sge sge; |
a42d985b | 2773 | enum srpt_command_state state; |
b99f8e4d | 2774 | int resp_len, ret, i; |
a42d985b BVA |
2775 | u8 srp_tm_status; |
2776 | ||
a42d985b BVA |
2777 | BUG_ON(!ch); |
2778 | ||
a42d985b BVA |
2779 | state = ioctx->state; |
2780 | switch (state) { | |
2781 | case SRPT_STATE_NEW: | |
2782 | case SRPT_STATE_DATA_IN: | |
2783 | ioctx->state = SRPT_STATE_CMD_RSP_SENT; | |
2784 | break; | |
2785 | case SRPT_STATE_MGMT: | |
2786 | ioctx->state = SRPT_STATE_MGMT_RSP_SENT; | |
2787 | break; | |
2788 | default: | |
2789 | WARN(true, "ch %p; cmd %d: unexpected command state %d\n", | |
2790 | ch, ioctx->ioctx.index, ioctx->state); | |
2791 | break; | |
2792 | } | |
a42d985b | 2793 | |
882dff28 | 2794 | if (WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)) |
b79fafac | 2795 | return; |
a42d985b | 2796 | |
a42d985b | 2797 | /* For read commands, transfer the data to the initiator. */ |
b99f8e4d CH |
2798 | if (ioctx->cmd.data_direction == DMA_FROM_DEVICE && |
2799 | ioctx->cmd.data_length && | |
a42d985b | 2800 | !ioctx->queue_status_only) { |
b99f8e4d CH |
2801 | for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) { |
2802 | struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; | |
2803 | ||
2804 | first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, | |
10fce586 | 2805 | ch->sport->port, NULL, first_wr); |
a42d985b BVA |
2806 | } |
2807 | } | |
2808 | ||
2809 | if (state != SRPT_STATE_MGMT) | |
649ee054 | 2810 | resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag, |
a42d985b BVA |
2811 | cmd->scsi_status); |
2812 | else { | |
2813 | srp_tm_status | |
2814 | = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response); | |
2815 | resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status, | |
649ee054 | 2816 | ioctx->cmd.tag); |
a42d985b | 2817 | } |
b99f8e4d CH |
2818 | |
2819 | atomic_inc(&ch->req_lim); | |
2820 | ||
2821 | if (unlikely(atomic_sub_return(1 + ioctx->n_rdma, | |
2822 | &ch->sq_wr_avail) < 0)) { | |
2823 | pr_warn("%s: IB send queue full (needed %d)\n", | |
2824 | __func__, ioctx->n_rdma); | |
2825 | ret = -ENOMEM; | |
2826 | goto out; | |
2827 | } | |
2828 | ||
2829 | ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len, | |
2830 | DMA_TO_DEVICE); | |
2831 | ||
2832 | sge.addr = ioctx->ioctx.dma; | |
2833 | sge.length = resp_len; | |
74333f12 | 2834 | sge.lkey = sdev->lkey; |
b99f8e4d CH |
2835 | |
2836 | ioctx->ioctx.cqe.done = srpt_send_done; | |
2837 | send_wr.next = NULL; | |
2838 | send_wr.wr_cqe = &ioctx->ioctx.cqe; | |
2839 | send_wr.sg_list = &sge; | |
2840 | send_wr.num_sge = 1; | |
2841 | send_wr.opcode = IB_WR_SEND; | |
2842 | send_wr.send_flags = IB_SEND_SIGNALED; | |
2843 | ||
9b32a596 | 2844 | ret = ib_post_send(ch->qp, first_wr, NULL); |
b99f8e4d CH |
2845 | if (ret < 0) { |
2846 | pr_err("%s: sending cmd response failed for tag %llu (%d)\n", | |
2847 | __func__, ioctx->cmd.tag, ret); | |
2848 | goto out; | |
a42d985b | 2849 | } |
b99f8e4d CH |
2850 | |
2851 | return; | |
2852 | ||
2853 | out: | |
2854 | atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail); | |
2855 | atomic_dec(&ch->req_lim); | |
2856 | srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); | |
2857 | target_put_sess_cmd(&ioctx->cmd); | |
b79fafac | 2858 | } |
a42d985b | 2859 | |
b79fafac JE |
2860 | static int srpt_queue_data_in(struct se_cmd *cmd) |
2861 | { | |
2862 | srpt_queue_response(cmd); | |
2863 | return 0; | |
2864 | } | |
2865 | ||
2866 | static void srpt_queue_tm_rsp(struct se_cmd *cmd) | |
2867 | { | |
2868 | srpt_queue_response(cmd); | |
a42d985b BVA |
2869 | } |
2870 | ||
40ca8757 BVA |
2871 | /* |
2872 | * This function is called for aborted commands if no response is sent to the | |
2873 | * initiator. Make sure that the credits freed by aborting a command are | |
2874 | * returned to the initiator the next time a response is sent by incrementing | |
2875 | * ch->req_lim_delta. | |
2876 | */ | |
131e6abc NB |
2877 | static void srpt_aborted_task(struct se_cmd *cmd) |
2878 | { | |
40ca8757 BVA |
2879 | struct srpt_send_ioctx *ioctx = container_of(cmd, |
2880 | struct srpt_send_ioctx, cmd); | |
2881 | struct srpt_rdma_ch *ch = ioctx->ch; | |
2882 | ||
2883 | atomic_inc(&ch->req_lim_delta); | |
131e6abc NB |
2884 | } |
2885 | ||
a42d985b BVA |
2886 | static int srpt_queue_status(struct se_cmd *cmd) |
2887 | { | |
2888 | struct srpt_send_ioctx *ioctx; | |
2889 | ||
2890 | ioctx = container_of(cmd, struct srpt_send_ioctx, cmd); | |
2891 | BUG_ON(ioctx->sense_data != cmd->sense_buffer); | |
2892 | if (cmd->se_cmd_flags & | |
2893 | (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE)) | |
2894 | WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION); | |
2895 | ioctx->queue_status_only = true; | |
b79fafac JE |
2896 | srpt_queue_response(cmd); |
2897 | return 0; | |
a42d985b BVA |
2898 | } |
2899 | ||
2900 | static void srpt_refresh_port_work(struct work_struct *work) | |
2901 | { | |
2902 | struct srpt_port *sport = container_of(work, struct srpt_port, work); | |
2903 | ||
2904 | srpt_refresh_port(sport); | |
2905 | } | |
2906 | ||
a1125314 BVA |
2907 | static bool srpt_ch_list_empty(struct srpt_port *sport) |
2908 | { | |
2909 | struct srpt_nexus *nexus; | |
2910 | bool res = true; | |
2911 | ||
2912 | rcu_read_lock(); | |
2913 | list_for_each_entry(nexus, &sport->nexus_list, entry) | |
2914 | if (!list_empty(&nexus->ch_list)) | |
2915 | res = false; | |
2916 | rcu_read_unlock(); | |
2917 | ||
2918 | return res; | |
2919 | } | |
2920 | ||
a42d985b | 2921 | /** |
ba60c84f BVA |
2922 | * srpt_release_sport - disable login and wait for associated channels |
2923 | * @sport: SRPT HCA port. | |
a42d985b | 2924 | */ |
ba60c84f | 2925 | static int srpt_release_sport(struct srpt_port *sport) |
a42d985b | 2926 | { |
a1125314 BVA |
2927 | struct srpt_nexus *nexus, *next_n; |
2928 | struct srpt_rdma_ch *ch; | |
a42d985b BVA |
2929 | |
2930 | WARN_ON_ONCE(irqs_disabled()); | |
2931 | ||
ba60c84f BVA |
2932 | mutex_lock(&sport->mutex); |
2933 | srpt_set_enabled(sport, false); | |
2934 | mutex_unlock(&sport->mutex); | |
a42d985b | 2935 | |
a1125314 BVA |
2936 | while (wait_event_timeout(sport->ch_releaseQ, |
2937 | srpt_ch_list_empty(sport), 5 * HZ) <= 0) { | |
2938 | pr_info("%s_%d: waiting for session unregistration ...\n", | |
6c854111 | 2939 | dev_name(&sport->sdev->device->dev), sport->port); |
a1125314 BVA |
2940 | rcu_read_lock(); |
2941 | list_for_each_entry(nexus, &sport->nexus_list, entry) { | |
2942 | list_for_each_entry(ch, &nexus->ch_list, list) { | |
2943 | pr_info("%s-%d: state %s\n", | |
2944 | ch->sess_name, ch->qp->qp_num, | |
2945 | get_ch_state_name(ch->state)); | |
2946 | } | |
2947 | } | |
2948 | rcu_read_unlock(); | |
2949 | } | |
2950 | ||
2951 | mutex_lock(&sport->mutex); | |
2952 | list_for_each_entry_safe(nexus, next_n, &sport->nexus_list, entry) { | |
2953 | list_del(&nexus->entry); | |
2954 | kfree_rcu(nexus, rcu); | |
2955 | } | |
2956 | mutex_unlock(&sport->mutex); | |
a42d985b BVA |
2957 | |
2958 | return 0; | |
2959 | } | |
2960 | ||
2bce1a6d | 2961 | static struct se_wwn *__srpt_lookup_wwn(const char *name) |
a42d985b BVA |
2962 | { |
2963 | struct ib_device *dev; | |
2964 | struct srpt_device *sdev; | |
2965 | struct srpt_port *sport; | |
2966 | int i; | |
2967 | ||
2968 | list_for_each_entry(sdev, &srpt_dev_list, list) { | |
2969 | dev = sdev->device; | |
2970 | if (!dev) | |
2971 | continue; | |
2972 | ||
2973 | for (i = 0; i < dev->phys_port_cnt; i++) { | |
2974 | sport = &sdev->port[i]; | |
2975 | ||
2bce1a6d BVA |
2976 | if (strcmp(sport->port_guid, name) == 0) |
2977 | return &sport->port_guid_wwn; | |
2978 | if (strcmp(sport->port_gid, name) == 0) | |
2979 | return &sport->port_gid_wwn; | |
a42d985b BVA |
2980 | } |
2981 | } | |
2982 | ||
2983 | return NULL; | |
2984 | } | |
2985 | ||
2bce1a6d | 2986 | static struct se_wwn *srpt_lookup_wwn(const char *name) |
a42d985b | 2987 | { |
2bce1a6d | 2988 | struct se_wwn *wwn; |
a42d985b BVA |
2989 | |
2990 | spin_lock(&srpt_dev_lock); | |
2bce1a6d | 2991 | wwn = __srpt_lookup_wwn(name); |
a42d985b BVA |
2992 | spin_unlock(&srpt_dev_lock); |
2993 | ||
2bce1a6d | 2994 | return wwn; |
a42d985b BVA |
2995 | } |
2996 | ||
c76d7d64 BVA |
2997 | static void srpt_free_srq(struct srpt_device *sdev) |
2998 | { | |
2999 | if (!sdev->srq) | |
3000 | return; | |
3001 | ||
3002 | ib_destroy_srq(sdev->srq); | |
3003 | srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev, | |
5dabcd04 BVA |
3004 | sdev->srq_size, sdev->req_buf_cache, |
3005 | DMA_FROM_DEVICE); | |
3006 | kmem_cache_destroy(sdev->req_buf_cache); | |
c76d7d64 BVA |
3007 | sdev->srq = NULL; |
3008 | } | |
3009 | ||
3010 | static int srpt_alloc_srq(struct srpt_device *sdev) | |
3011 | { | |
3012 | struct ib_srq_init_attr srq_attr = { | |
3013 | .event_handler = srpt_srq_event, | |
3014 | .srq_context = (void *)sdev, | |
3015 | .attr.max_wr = sdev->srq_size, | |
3016 | .attr.max_sge = 1, | |
3017 | .srq_type = IB_SRQT_BASIC, | |
3018 | }; | |
3019 | struct ib_device *device = sdev->device; | |
3020 | struct ib_srq *srq; | |
3021 | int i; | |
3022 | ||
3023 | WARN_ON_ONCE(sdev->srq); | |
3024 | srq = ib_create_srq(sdev->pd, &srq_attr); | |
3025 | if (IS_ERR(srq)) { | |
3026 | pr_debug("ib_create_srq() failed: %ld\n", PTR_ERR(srq)); | |
3027 | return PTR_ERR(srq); | |
3028 | } | |
3029 | ||
3030 | pr_debug("create SRQ #wr= %d max_allow=%d dev= %s\n", sdev->srq_size, | |
6c854111 | 3031 | sdev->device->attrs.max_srq_wr, dev_name(&device->dev)); |
c76d7d64 | 3032 | |
5dabcd04 BVA |
3033 | sdev->req_buf_cache = kmem_cache_create("srpt-srq-req-buf", |
3034 | srp_max_req_size, 0, 0, NULL); | |
3035 | if (!sdev->req_buf_cache) | |
3036 | goto free_srq; | |
3037 | ||
c76d7d64 BVA |
3038 | sdev->ioctx_ring = (struct srpt_recv_ioctx **) |
3039 | srpt_alloc_ioctx_ring(sdev, sdev->srq_size, | |
3040 | sizeof(*sdev->ioctx_ring[0]), | |
5dabcd04 | 3041 | sdev->req_buf_cache, 0, DMA_FROM_DEVICE); |
82305f82 | 3042 | if (!sdev->ioctx_ring) |
5dabcd04 | 3043 | goto free_cache; |
c76d7d64 BVA |
3044 | |
3045 | sdev->use_srq = true; | |
3046 | sdev->srq = srq; | |
3047 | ||
fcf58936 BVA |
3048 | for (i = 0; i < sdev->srq_size; ++i) { |
3049 | INIT_LIST_HEAD(&sdev->ioctx_ring[i]->wait_list); | |
c76d7d64 | 3050 | srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]); |
fcf58936 | 3051 | } |
c76d7d64 BVA |
3052 | |
3053 | return 0; | |
82305f82 | 3054 | |
5dabcd04 BVA |
3055 | free_cache: |
3056 | kmem_cache_destroy(sdev->req_buf_cache); | |
3057 | ||
82305f82 BVA |
3058 | free_srq: |
3059 | ib_destroy_srq(srq); | |
3060 | return -ENOMEM; | |
c76d7d64 BVA |
3061 | } |
3062 | ||
3063 | static int srpt_use_srq(struct srpt_device *sdev, bool use_srq) | |
3064 | { | |
3065 | struct ib_device *device = sdev->device; | |
3066 | int ret = 0; | |
3067 | ||
3068 | if (!use_srq) { | |
3069 | srpt_free_srq(sdev); | |
3070 | sdev->use_srq = false; | |
3071 | } else if (use_srq && !sdev->srq) { | |
3072 | ret = srpt_alloc_srq(sdev); | |
3073 | } | |
6c854111 JG |
3074 | pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__, |
3075 | dev_name(&device->dev), sdev->use_srq, ret); | |
c76d7d64 BVA |
3076 | return ret; |
3077 | } | |
3078 | ||
a42d985b | 3079 | /** |
10eac19b BVA |
3080 | * srpt_add_one - InfiniBand device addition callback function |
3081 | * @device: Describes a HCA. | |
a42d985b BVA |
3082 | */ |
3083 | static void srpt_add_one(struct ib_device *device) | |
3084 | { | |
3085 | struct srpt_device *sdev; | |
3086 | struct srpt_port *sport; | |
63cf1a90 | 3087 | int i, ret; |
a42d985b | 3088 | |
e3dfa60c | 3089 | pr_debug("device = %p\n", device); |
a42d985b | 3090 | |
e620ebfc BVA |
3091 | sdev = kzalloc(struct_size(sdev, port, device->phys_port_cnt), |
3092 | GFP_KERNEL); | |
a42d985b BVA |
3093 | if (!sdev) |
3094 | goto err; | |
3095 | ||
3096 | sdev->device = device; | |
ba60c84f | 3097 | mutex_init(&sdev->sdev_mutex); |
a42d985b | 3098 | |
ed082d36 | 3099 | sdev->pd = ib_alloc_pd(device, 0); |
a42d985b BVA |
3100 | if (IS_ERR(sdev->pd)) |
3101 | goto free_dev; | |
3102 | ||
74333f12 | 3103 | sdev->lkey = sdev->pd->local_dma_lkey; |
a42d985b | 3104 | |
4a061b28 | 3105 | sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr); |
a42d985b | 3106 | |
c76d7d64 | 3107 | srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq); |
a42d985b BVA |
3108 | |
3109 | if (!srpt_service_guid) | |
3110 | srpt_service_guid = be64_to_cpu(device->node_guid); | |
3111 | ||
63cf1a90 BVA |
3112 | if (rdma_port_get_link_layer(device, 1) == IB_LINK_LAYER_INFINIBAND) |
3113 | sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev); | |
3114 | if (IS_ERR(sdev->cm_id)) { | |
3115 | pr_info("ib_create_cm_id() failed: %ld\n", | |
3116 | PTR_ERR(sdev->cm_id)); | |
3117 | sdev->cm_id = NULL; | |
3118 | if (!rdma_cm_id) | |
3119 | goto err_ring; | |
3120 | } | |
a42d985b BVA |
3121 | |
3122 | /* print out target login information */ | |
75d79b80 BVA |
3123 | pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,pkey=ffff,service_id=%016llx\n", |
3124 | srpt_service_guid, srpt_service_guid, srpt_service_guid); | |
a42d985b BVA |
3125 | |
3126 | /* | |
3127 | * We do not have a consistent service_id (ie. also id_ext of target_id) | |
3128 | * to identify this target. We currently use the guid of the first HCA | |
3129 | * in the system as service_id; therefore, the target_id will change | |
3130 | * if this HCA is gone bad and replaced by different HCA | |
3131 | */ | |
63cf1a90 BVA |
3132 | ret = sdev->cm_id ? |
3133 | ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0) : | |
3134 | 0; | |
3135 | if (ret < 0) { | |
3136 | pr_err("ib_cm_listen() failed: %d (cm_id state = %d)\n", ret, | |
3137 | sdev->cm_id->state); | |
a42d985b | 3138 | goto err_cm; |
63cf1a90 | 3139 | } |
a42d985b BVA |
3140 | |
3141 | INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device, | |
3142 | srpt_event_handler); | |
dcc9881e | 3143 | ib_register_event_handler(&sdev->event_handler); |
a42d985b | 3144 | |
a42d985b BVA |
3145 | for (i = 1; i <= sdev->device->phys_port_cnt; i++) { |
3146 | sport = &sdev->port[i - 1]; | |
a1125314 | 3147 | INIT_LIST_HEAD(&sport->nexus_list); |
ba60c84f BVA |
3148 | init_waitqueue_head(&sport->ch_releaseQ); |
3149 | mutex_init(&sport->mutex); | |
a42d985b BVA |
3150 | sport->sdev = sdev; |
3151 | sport->port = i; | |
3152 | sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE; | |
3153 | sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE; | |
3154 | sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE; | |
dea26209 | 3155 | sport->port_attrib.use_srq = false; |
a42d985b | 3156 | INIT_WORK(&sport->work, srpt_refresh_port_work); |
a42d985b BVA |
3157 | |
3158 | if (srpt_refresh_port(sport)) { | |
9f5d32af | 3159 | pr_err("MAD registration failed for %s-%d.\n", |
6c854111 | 3160 | dev_name(&sdev->device->dev), i); |
dea26209 | 3161 | goto err_event; |
a42d985b | 3162 | } |
a42d985b BVA |
3163 | } |
3164 | ||
3165 | spin_lock(&srpt_dev_lock); | |
3166 | list_add_tail(&sdev->list, &srpt_dev_list); | |
3167 | spin_unlock(&srpt_dev_lock); | |
3168 | ||
3169 | out: | |
3170 | ib_set_client_data(device, &srpt_client, sdev); | |
6c854111 | 3171 | pr_debug("added %s.\n", dev_name(&device->dev)); |
a42d985b BVA |
3172 | return; |
3173 | ||
a42d985b BVA |
3174 | err_event: |
3175 | ib_unregister_event_handler(&sdev->event_handler); | |
3176 | err_cm: | |
63cf1a90 BVA |
3177 | if (sdev->cm_id) |
3178 | ib_destroy_cm_id(sdev->cm_id); | |
dea26209 | 3179 | err_ring: |
c76d7d64 | 3180 | srpt_free_srq(sdev); |
a42d985b BVA |
3181 | ib_dealloc_pd(sdev->pd); |
3182 | free_dev: | |
3183 | kfree(sdev); | |
3184 | err: | |
3185 | sdev = NULL; | |
6c854111 | 3186 | pr_info("%s(%s) failed.\n", __func__, dev_name(&device->dev)); |
a42d985b BVA |
3187 | goto out; |
3188 | } | |
3189 | ||
3190 | /** | |
10eac19b BVA |
3191 | * srpt_remove_one - InfiniBand device removal callback function |
3192 | * @device: Describes a HCA. | |
3193 | * @client_data: The value passed as the third argument to ib_set_client_data(). | |
a42d985b | 3194 | */ |
7c1eb45a | 3195 | static void srpt_remove_one(struct ib_device *device, void *client_data) |
a42d985b | 3196 | { |
7c1eb45a | 3197 | struct srpt_device *sdev = client_data; |
a42d985b BVA |
3198 | int i; |
3199 | ||
a42d985b | 3200 | if (!sdev) { |
6c854111 JG |
3201 | pr_info("%s(%s): nothing to do.\n", __func__, |
3202 | dev_name(&device->dev)); | |
a42d985b BVA |
3203 | return; |
3204 | } | |
3205 | ||
3206 | srpt_unregister_mad_agent(sdev); | |
3207 | ||
3208 | ib_unregister_event_handler(&sdev->event_handler); | |
3209 | ||
3210 | /* Cancel any work queued by the just unregistered IB event handler. */ | |
3211 | for (i = 0; i < sdev->device->phys_port_cnt; i++) | |
3212 | cancel_work_sync(&sdev->port[i].work); | |
3213 | ||
63cf1a90 BVA |
3214 | if (sdev->cm_id) |
3215 | ib_destroy_cm_id(sdev->cm_id); | |
3216 | ||
3217 | ib_set_client_data(device, &srpt_client, NULL); | |
a42d985b BVA |
3218 | |
3219 | /* | |
3220 | * Unregistering a target must happen after destroying sdev->cm_id | |
3221 | * such that no new SRP_LOGIN_REQ information units can arrive while | |
3222 | * destroying the target. | |
3223 | */ | |
3224 | spin_lock(&srpt_dev_lock); | |
3225 | list_del(&sdev->list); | |
3226 | spin_unlock(&srpt_dev_lock); | |
ba60c84f BVA |
3227 | |
3228 | for (i = 0; i < sdev->device->phys_port_cnt; i++) | |
3229 | srpt_release_sport(&sdev->port[i]); | |
a42d985b | 3230 | |
c76d7d64 BVA |
3231 | srpt_free_srq(sdev); |
3232 | ||
a42d985b BVA |
3233 | ib_dealloc_pd(sdev->pd); |
3234 | ||
a42d985b BVA |
3235 | kfree(sdev); |
3236 | } | |
3237 | ||
3238 | static struct ib_client srpt_client = { | |
3239 | .name = DRV_NAME, | |
3240 | .add = srpt_add_one, | |
3241 | .remove = srpt_remove_one | |
3242 | }; | |
3243 | ||
3244 | static int srpt_check_true(struct se_portal_group *se_tpg) | |
3245 | { | |
3246 | return 1; | |
3247 | } | |
3248 | ||
3249 | static int srpt_check_false(struct se_portal_group *se_tpg) | |
3250 | { | |
3251 | return 0; | |
3252 | } | |
3253 | ||
2bce1a6d BVA |
3254 | static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg) |
3255 | { | |
3256 | return tpg->se_tpg_wwn->priv; | |
3257 | } | |
3258 | ||
a42d985b BVA |
3259 | static char *srpt_get_fabric_wwn(struct se_portal_group *tpg) |
3260 | { | |
2bce1a6d | 3261 | struct srpt_port *sport = srpt_tpg_to_sport(tpg); |
a42d985b | 3262 | |
2bce1a6d BVA |
3263 | WARN_ON_ONCE(tpg != &sport->port_guid_tpg && |
3264 | tpg != &sport->port_gid_tpg); | |
3265 | return tpg == &sport->port_guid_tpg ? sport->port_guid : | |
3266 | sport->port_gid; | |
a42d985b BVA |
3267 | } |
3268 | ||
3269 | static u16 srpt_get_tag(struct se_portal_group *tpg) | |
3270 | { | |
3271 | return 1; | |
3272 | } | |
3273 | ||
a42d985b BVA |
3274 | static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg) |
3275 | { | |
3276 | return 1; | |
3277 | } | |
3278 | ||
3279 | static void srpt_release_cmd(struct se_cmd *se_cmd) | |
3280 | { | |
9474b043 NB |
3281 | struct srpt_send_ioctx *ioctx = container_of(se_cmd, |
3282 | struct srpt_send_ioctx, cmd); | |
3283 | struct srpt_rdma_ch *ch = ioctx->ch; | |
5dabcd04 | 3284 | struct srpt_recv_ioctx *recv_ioctx = ioctx->recv_ioctx; |
9474b043 | 3285 | |
bd2c52d7 BVA |
3286 | WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE && |
3287 | !(ioctx->cmd.transport_state & CMD_T_ABORTED)); | |
9474b043 | 3288 | |
5dabcd04 BVA |
3289 | if (recv_ioctx) { |
3290 | WARN_ON_ONCE(!list_empty(&recv_ioctx->wait_list)); | |
3291 | ioctx->recv_ioctx = NULL; | |
3292 | srpt_post_recv(ch->sport->sdev, ch, recv_ioctx); | |
3293 | } | |
3294 | ||
b99f8e4d CH |
3295 | if (ioctx->n_rw_ctx) { |
3296 | srpt_free_rw_ctxs(ch, ioctx); | |
3297 | ioctx->n_rw_ctx = 0; | |
9474b043 NB |
3298 | } |
3299 | ||
fd1b6687 | 3300 | target_free_tag(se_cmd->se_sess, se_cmd); |
a42d985b BVA |
3301 | } |
3302 | ||
a42d985b | 3303 | /** |
10eac19b BVA |
3304 | * srpt_close_session - forcibly close a session |
3305 | * @se_sess: SCSI target session. | |
a42d985b BVA |
3306 | * |
3307 | * Callback function invoked by the TCM core to clean up sessions associated | |
3308 | * with a node ACL when the user invokes | |
3309 | * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id | |
3310 | */ | |
3311 | static void srpt_close_session(struct se_session *se_sess) | |
3312 | { | |
f108f0f6 | 3313 | struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr; |
a42d985b | 3314 | |
01b3ee13 | 3315 | srpt_disconnect_ch_sync(ch); |
a42d985b BVA |
3316 | } |
3317 | ||
a42d985b | 3318 | /** |
10eac19b BVA |
3319 | * srpt_sess_get_index - return the value of scsiAttIntrPortIndex (SCSI-MIB) |
3320 | * @se_sess: SCSI target session. | |
a42d985b BVA |
3321 | * |
3322 | * A quote from RFC 4455 (SCSI-MIB) about this MIB object: | |
3323 | * This object represents an arbitrary integer used to uniquely identify a | |
3324 | * particular attached remote initiator port to a particular SCSI target port | |
3325 | * within a particular SCSI target device within a particular SCSI instance. | |
3326 | */ | |
3327 | static u32 srpt_sess_get_index(struct se_session *se_sess) | |
3328 | { | |
3329 | return 0; | |
3330 | } | |
3331 | ||
3332 | static void srpt_set_default_node_attrs(struct se_node_acl *nacl) | |
3333 | { | |
3334 | } | |
3335 | ||
a42d985b BVA |
3336 | /* Note: only used from inside debug printk's by the TCM core. */ |
3337 | static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd) | |
3338 | { | |
3339 | struct srpt_send_ioctx *ioctx; | |
3340 | ||
3341 | ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd); | |
dd3bec86 | 3342 | return ioctx->state; |
a42d985b BVA |
3343 | } |
3344 | ||
2bce1a6d BVA |
3345 | static int srpt_parse_guid(u64 *guid, const char *name) |
3346 | { | |
3347 | u16 w[4]; | |
3348 | int ret = -EINVAL; | |
3349 | ||
3350 | if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4) | |
3351 | goto out; | |
3352 | *guid = get_unaligned_be64(w); | |
3353 | ret = 0; | |
3354 | out: | |
3355 | return ret; | |
3356 | } | |
3357 | ||
a42d985b | 3358 | /** |
10eac19b | 3359 | * srpt_parse_i_port_id - parse an initiator port ID |
a42d985b BVA |
3360 | * @name: ASCII representation of a 128-bit initiator port ID. |
3361 | * @i_port_id: Binary 128-bit port ID. | |
3362 | */ | |
3363 | static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name) | |
3364 | { | |
3365 | const char *p; | |
3366 | unsigned len, count, leading_zero_bytes; | |
c70ca389 | 3367 | int ret; |
a42d985b BVA |
3368 | |
3369 | p = name; | |
b60459f0 | 3370 | if (strncasecmp(p, "0x", 2) == 0) |
a42d985b BVA |
3371 | p += 2; |
3372 | ret = -EINVAL; | |
3373 | len = strlen(p); | |
3374 | if (len % 2) | |
3375 | goto out; | |
3376 | count = min(len / 2, 16U); | |
3377 | leading_zero_bytes = 16 - count; | |
3378 | memset(i_port_id, 0, leading_zero_bytes); | |
c70ca389 | 3379 | ret = hex2bin(i_port_id + leading_zero_bytes, p, count); |
63cf1a90 | 3380 | |
a42d985b BVA |
3381 | out: |
3382 | return ret; | |
3383 | } | |
3384 | ||
3385 | /* | |
63cf1a90 BVA |
3386 | * configfs callback function invoked for mkdir |
3387 | * /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id | |
3388 | * | |
3389 | * i_port_id must be an initiator port GUID, GID or IP address. See also the | |
3390 | * target_alloc_session() calls in this driver. Examples of valid initiator | |
3391 | * port IDs: | |
3392 | * 0x0000000000000000505400fffe4a0b7b | |
3393 | * 0000000000000000505400fffe4a0b7b | |
3394 | * 5054:00ff:fe4a:0b7b | |
3395 | * 192.168.122.76 | |
a42d985b | 3396 | */ |
c7d6a803 | 3397 | static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name) |
a42d985b | 3398 | { |
63cf1a90 | 3399 | struct sockaddr_storage sa; |
2bce1a6d | 3400 | u64 guid; |
a42d985b | 3401 | u8 i_port_id[16]; |
2bce1a6d | 3402 | int ret; |
a42d985b | 3403 | |
2bce1a6d BVA |
3404 | ret = srpt_parse_guid(&guid, name); |
3405 | if (ret < 0) | |
3406 | ret = srpt_parse_i_port_id(i_port_id, name); | |
63cf1a90 BVA |
3407 | if (ret < 0) |
3408 | ret = inet_pton_with_scope(&init_net, AF_UNSPEC, name, NULL, | |
3409 | &sa); | |
2bce1a6d | 3410 | if (ret < 0) |
9f5d32af | 3411 | pr_err("invalid initiator port ID %s\n", name); |
2bce1a6d | 3412 | return ret; |
a42d985b BVA |
3413 | } |
3414 | ||
2eafd729 CH |
3415 | static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item, |
3416 | char *page) | |
a42d985b | 3417 | { |
2eafd729 | 3418 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
2bce1a6d | 3419 | struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); |
a42d985b BVA |
3420 | |
3421 | return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size); | |
3422 | } | |
3423 | ||
2eafd729 CH |
3424 | static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item, |
3425 | const char *page, size_t count) | |
a42d985b | 3426 | { |
2eafd729 | 3427 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
2bce1a6d | 3428 | struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); |
a42d985b BVA |
3429 | unsigned long val; |
3430 | int ret; | |
3431 | ||
9d8abf45 | 3432 | ret = kstrtoul(page, 0, &val); |
a42d985b | 3433 | if (ret < 0) { |
9d8abf45 | 3434 | pr_err("kstrtoul() failed with ret: %d\n", ret); |
a42d985b BVA |
3435 | return -EINVAL; |
3436 | } | |
3437 | if (val > MAX_SRPT_RDMA_SIZE) { | |
3438 | pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val, | |
3439 | MAX_SRPT_RDMA_SIZE); | |
3440 | return -EINVAL; | |
3441 | } | |
3442 | if (val < DEFAULT_MAX_RDMA_SIZE) { | |
3443 | pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n", | |
3444 | val, DEFAULT_MAX_RDMA_SIZE); | |
3445 | return -EINVAL; | |
3446 | } | |
3447 | sport->port_attrib.srp_max_rdma_size = val; | |
3448 | ||
3449 | return count; | |
3450 | } | |
3451 | ||
2eafd729 CH |
3452 | static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item, |
3453 | char *page) | |
a42d985b | 3454 | { |
2eafd729 | 3455 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
2bce1a6d | 3456 | struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); |
a42d985b BVA |
3457 | |
3458 | return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size); | |
3459 | } | |
3460 | ||
2eafd729 CH |
3461 | static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item, |
3462 | const char *page, size_t count) | |
a42d985b | 3463 | { |
2eafd729 | 3464 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
2bce1a6d | 3465 | struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); |
a42d985b BVA |
3466 | unsigned long val; |
3467 | int ret; | |
3468 | ||
9d8abf45 | 3469 | ret = kstrtoul(page, 0, &val); |
a42d985b | 3470 | if (ret < 0) { |
9d8abf45 | 3471 | pr_err("kstrtoul() failed with ret: %d\n", ret); |
a42d985b BVA |
3472 | return -EINVAL; |
3473 | } | |
3474 | if (val > MAX_SRPT_RSP_SIZE) { | |
3475 | pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val, | |
3476 | MAX_SRPT_RSP_SIZE); | |
3477 | return -EINVAL; | |
3478 | } | |
3479 | if (val < MIN_MAX_RSP_SIZE) { | |
3480 | pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val, | |
3481 | MIN_MAX_RSP_SIZE); | |
3482 | return -EINVAL; | |
3483 | } | |
3484 | sport->port_attrib.srp_max_rsp_size = val; | |
3485 | ||
3486 | return count; | |
3487 | } | |
3488 | ||
2eafd729 CH |
3489 | static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item, |
3490 | char *page) | |
a42d985b | 3491 | { |
2eafd729 | 3492 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
2bce1a6d | 3493 | struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); |
a42d985b BVA |
3494 | |
3495 | return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size); | |
3496 | } | |
3497 | ||
2eafd729 CH |
3498 | static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item, |
3499 | const char *page, size_t count) | |
a42d985b | 3500 | { |
2eafd729 | 3501 | struct se_portal_group *se_tpg = attrib_to_tpg(item); |
2bce1a6d | 3502 | struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); |
a42d985b BVA |
3503 | unsigned long val; |
3504 | int ret; | |
3505 | ||
9d8abf45 | 3506 | ret = kstrtoul(page, 0, &val); |
a42d985b | 3507 | if (ret < 0) { |
9d8abf45 | 3508 | pr_err("kstrtoul() failed with ret: %d\n", ret); |
a42d985b BVA |
3509 | return -EINVAL; |
3510 | } | |
3511 | if (val > MAX_SRPT_SRQ_SIZE) { | |
3512 | pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val, | |
3513 | MAX_SRPT_SRQ_SIZE); | |
3514 | return -EINVAL; | |
3515 | } | |
3516 | if (val < MIN_SRPT_SRQ_SIZE) { | |
3517 | pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val, | |
3518 | MIN_SRPT_SRQ_SIZE); | |
3519 | return -EINVAL; | |
3520 | } | |
3521 | sport->port_attrib.srp_sq_size = val; | |
3522 | ||
3523 | return count; | |
3524 | } | |
3525 | ||
dea26209 BVA |
3526 | static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item, |
3527 | char *page) | |
3528 | { | |
3529 | struct se_portal_group *se_tpg = attrib_to_tpg(item); | |
3530 | struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); | |
3531 | ||
3532 | return sprintf(page, "%d\n", sport->port_attrib.use_srq); | |
3533 | } | |
3534 | ||
3535 | static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item, | |
3536 | const char *page, size_t count) | |
3537 | { | |
3538 | struct se_portal_group *se_tpg = attrib_to_tpg(item); | |
3539 | struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); | |
57b0c460 | 3540 | struct srpt_device *sdev = sport->sdev; |
dea26209 | 3541 | unsigned long val; |
57b0c460 | 3542 | bool enabled; |
dea26209 BVA |
3543 | int ret; |
3544 | ||
3545 | ret = kstrtoul(page, 0, &val); | |
3546 | if (ret < 0) | |
3547 | return ret; | |
3548 | if (val != !!val) | |
3549 | return -EINVAL; | |
57b0c460 | 3550 | |
ba60c84f | 3551 | ret = mutex_lock_interruptible(&sdev->sdev_mutex); |
57b0c460 BVA |
3552 | if (ret < 0) |
3553 | return ret; | |
ba60c84f BVA |
3554 | ret = mutex_lock_interruptible(&sport->mutex); |
3555 | if (ret < 0) | |
3556 | goto unlock_sdev; | |
57b0c460 BVA |
3557 | enabled = sport->enabled; |
3558 | /* Log out all initiator systems before changing 'use_srq'. */ | |
3559 | srpt_set_enabled(sport, false); | |
dea26209 | 3560 | sport->port_attrib.use_srq = val; |
57b0c460 BVA |
3561 | srpt_use_srq(sdev, sport->port_attrib.use_srq); |
3562 | srpt_set_enabled(sport, enabled); | |
ba60c84f BVA |
3563 | ret = count; |
3564 | mutex_unlock(&sport->mutex); | |
3565 | unlock_sdev: | |
3566 | mutex_unlock(&sdev->sdev_mutex); | |
dea26209 | 3567 | |
ba60c84f | 3568 | return ret; |
dea26209 BVA |
3569 | } |
3570 | ||
2eafd729 CH |
3571 | CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size); |
3572 | CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size); | |
3573 | CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size); | |
dea26209 | 3574 | CONFIGFS_ATTR(srpt_tpg_attrib_, use_srq); |
a42d985b BVA |
3575 | |
3576 | static struct configfs_attribute *srpt_tpg_attrib_attrs[] = { | |
2eafd729 CH |
3577 | &srpt_tpg_attrib_attr_srp_max_rdma_size, |
3578 | &srpt_tpg_attrib_attr_srp_max_rsp_size, | |
3579 | &srpt_tpg_attrib_attr_srp_sq_size, | |
dea26209 | 3580 | &srpt_tpg_attrib_attr_use_srq, |
a42d985b BVA |
3581 | NULL, |
3582 | }; | |
3583 | ||
63cf1a90 BVA |
3584 | static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr) |
3585 | { | |
3586 | struct rdma_cm_id *rdma_cm_id; | |
3587 | int ret; | |
3588 | ||
3589 | rdma_cm_id = rdma_create_id(&init_net, srpt_rdma_cm_handler, | |
3590 | NULL, RDMA_PS_TCP, IB_QPT_RC); | |
3591 | if (IS_ERR(rdma_cm_id)) { | |
3592 | pr_err("RDMA/CM ID creation failed: %ld\n", | |
3593 | PTR_ERR(rdma_cm_id)); | |
3594 | goto out; | |
3595 | } | |
3596 | ||
3597 | ret = rdma_bind_addr(rdma_cm_id, listen_addr); | |
3598 | if (ret) { | |
3599 | char addr_str[64]; | |
3600 | ||
3601 | snprintf(addr_str, sizeof(addr_str), "%pISp", listen_addr); | |
3602 | pr_err("Binding RDMA/CM ID to address %s failed: %d\n", | |
3603 | addr_str, ret); | |
3604 | rdma_destroy_id(rdma_cm_id); | |
3605 | rdma_cm_id = ERR_PTR(ret); | |
3606 | goto out; | |
3607 | } | |
3608 | ||
3609 | ret = rdma_listen(rdma_cm_id, 128); | |
3610 | if (ret) { | |
3611 | pr_err("rdma_listen() failed: %d\n", ret); | |
3612 | rdma_destroy_id(rdma_cm_id); | |
3613 | rdma_cm_id = ERR_PTR(ret); | |
3614 | } | |
3615 | ||
3616 | out: | |
3617 | return rdma_cm_id; | |
3618 | } | |
3619 | ||
3620 | static ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page) | |
3621 | { | |
3622 | return sprintf(page, "%d\n", rdma_cm_port); | |
3623 | } | |
3624 | ||
3625 | static ssize_t srpt_rdma_cm_port_store(struct config_item *item, | |
3626 | const char *page, size_t count) | |
3627 | { | |
3628 | struct sockaddr_in addr4 = { .sin_family = AF_INET }; | |
3629 | struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6 }; | |
3630 | struct rdma_cm_id *new_id = NULL; | |
3631 | u16 val; | |
3632 | int ret; | |
3633 | ||
3634 | ret = kstrtou16(page, 0, &val); | |
3635 | if (ret < 0) | |
3636 | return ret; | |
3637 | ret = count; | |
3638 | if (rdma_cm_port == val) | |
3639 | goto out; | |
3640 | ||
3641 | if (val) { | |
3642 | addr6.sin6_port = cpu_to_be16(val); | |
3643 | new_id = srpt_create_rdma_id((struct sockaddr *)&addr6); | |
3644 | if (IS_ERR(new_id)) { | |
3645 | addr4.sin_port = cpu_to_be16(val); | |
3646 | new_id = srpt_create_rdma_id((struct sockaddr *)&addr4); | |
3647 | if (IS_ERR(new_id)) { | |
3648 | ret = PTR_ERR(new_id); | |
3649 | goto out; | |
3650 | } | |
3651 | } | |
3652 | } | |
3653 | ||
3654 | mutex_lock(&rdma_cm_mutex); | |
3655 | rdma_cm_port = val; | |
3656 | swap(rdma_cm_id, new_id); | |
3657 | mutex_unlock(&rdma_cm_mutex); | |
3658 | ||
3659 | if (new_id) | |
3660 | rdma_destroy_id(new_id); | |
3661 | ret = count; | |
3662 | out: | |
3663 | return ret; | |
3664 | } | |
3665 | ||
3666 | CONFIGFS_ATTR(srpt_, rdma_cm_port); | |
3667 | ||
3668 | static struct configfs_attribute *srpt_da_attrs[] = { | |
3669 | &srpt_attr_rdma_cm_port, | |
3670 | NULL, | |
3671 | }; | |
3672 | ||
2eafd729 | 3673 | static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page) |
a42d985b | 3674 | { |
2eafd729 | 3675 | struct se_portal_group *se_tpg = to_tpg(item); |
2bce1a6d | 3676 | struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); |
a42d985b | 3677 | |
ffd59806 | 3678 | return snprintf(page, PAGE_SIZE, "%d\n", sport->enabled); |
a42d985b BVA |
3679 | } |
3680 | ||
2eafd729 CH |
3681 | static ssize_t srpt_tpg_enable_store(struct config_item *item, |
3682 | const char *page, size_t count) | |
a42d985b | 3683 | { |
2eafd729 | 3684 | struct se_portal_group *se_tpg = to_tpg(item); |
2bce1a6d | 3685 | struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); |
a42d985b | 3686 | unsigned long tmp; |
ffd59806 | 3687 | int ret; |
a42d985b | 3688 | |
9d8abf45 | 3689 | ret = kstrtoul(page, 0, &tmp); |
a42d985b | 3690 | if (ret < 0) { |
9f5d32af | 3691 | pr_err("Unable to extract srpt_tpg_store_enable\n"); |
a42d985b BVA |
3692 | return -EINVAL; |
3693 | } | |
3694 | ||
3695 | if ((tmp != 0) && (tmp != 1)) { | |
9f5d32af | 3696 | pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp); |
a42d985b BVA |
3697 | return -EINVAL; |
3698 | } | |
a42d985b | 3699 | |
ba60c84f | 3700 | mutex_lock(&sport->mutex); |
8b6dc529 | 3701 | srpt_set_enabled(sport, tmp); |
ba60c84f | 3702 | mutex_unlock(&sport->mutex); |
043a6806 | 3703 | |
a42d985b BVA |
3704 | return count; |
3705 | } | |
3706 | ||
2eafd729 | 3707 | CONFIGFS_ATTR(srpt_tpg_, enable); |
a42d985b BVA |
3708 | |
3709 | static struct configfs_attribute *srpt_tpg_attrs[] = { | |
2eafd729 | 3710 | &srpt_tpg_attr_enable, |
a42d985b BVA |
3711 | NULL, |
3712 | }; | |
3713 | ||
3714 | /** | |
10eac19b BVA |
3715 | * srpt_make_tpg - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port/$tpg |
3716 | * @wwn: Corresponds to $driver/$port. | |
10eac19b | 3717 | * @name: $tpg. |
a42d985b BVA |
3718 | */ |
3719 | static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn, | |
a42d985b BVA |
3720 | const char *name) |
3721 | { | |
2bce1a6d | 3722 | struct srpt_port *sport = wwn->priv; |
89180e81 | 3723 | struct se_portal_group *tpg; |
a42d985b BVA |
3724 | int res; |
3725 | ||
2bce1a6d BVA |
3726 | WARN_ON_ONCE(wwn != &sport->port_guid_wwn && |
3727 | wwn != &sport->port_gid_wwn); | |
3728 | tpg = wwn == &sport->port_guid_wwn ? &sport->port_guid_tpg : | |
3729 | &sport->port_gid_tpg; | |
3730 | res = core_tpg_register(wwn, tpg, SCSI_PROTOCOL_SRP); | |
a42d985b BVA |
3731 | if (res) |
3732 | return ERR_PTR(res); | |
3733 | ||
2bce1a6d | 3734 | return tpg; |
a42d985b BVA |
3735 | } |
3736 | ||
3737 | /** | |
10eac19b BVA |
3738 | * srpt_drop_tpg - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port/$tpg |
3739 | * @tpg: Target portal group to deregister. | |
a42d985b BVA |
3740 | */ |
3741 | static void srpt_drop_tpg(struct se_portal_group *tpg) | |
3742 | { | |
2bce1a6d | 3743 | struct srpt_port *sport = srpt_tpg_to_sport(tpg); |
a42d985b BVA |
3744 | |
3745 | sport->enabled = false; | |
2bce1a6d | 3746 | core_tpg_deregister(tpg); |
a42d985b BVA |
3747 | } |
3748 | ||
3749 | /** | |
10eac19b BVA |
3750 | * srpt_make_tport - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port |
3751 | * @tf: Not used. | |
3752 | * @group: Not used. | |
3753 | * @name: $port. | |
a42d985b BVA |
3754 | */ |
3755 | static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf, | |
3756 | struct config_group *group, | |
3757 | const char *name) | |
3758 | { | |
2bce1a6d | 3759 | return srpt_lookup_wwn(name) ? : ERR_PTR(-EINVAL); |
a42d985b BVA |
3760 | } |
3761 | ||
3762 | /** | |
10eac19b BVA |
3763 | * srpt_drop_tport - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port |
3764 | * @wwn: $port. | |
a42d985b BVA |
3765 | */ |
3766 | static void srpt_drop_tport(struct se_wwn *wwn) | |
3767 | { | |
a42d985b BVA |
3768 | } |
3769 | ||
2eafd729 | 3770 | static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf) |
a42d985b | 3771 | { |
6feb64ff | 3772 | return scnprintf(buf, PAGE_SIZE, "\n"); |
a42d985b BVA |
3773 | } |
3774 | ||
2eafd729 | 3775 | CONFIGFS_ATTR_RO(srpt_wwn_, version); |
a42d985b BVA |
3776 | |
3777 | static struct configfs_attribute *srpt_wwn_attrs[] = { | |
2eafd729 | 3778 | &srpt_wwn_attr_version, |
a42d985b BVA |
3779 | NULL, |
3780 | }; | |
3781 | ||
9ac8928e CH |
3782 | static const struct target_core_fabric_ops srpt_template = { |
3783 | .module = THIS_MODULE, | |
30c7ca93 | 3784 | .fabric_name = "srpt", |
a42d985b BVA |
3785 | .tpg_get_wwn = srpt_get_fabric_wwn, |
3786 | .tpg_get_tag = srpt_get_tag, | |
a42d985b BVA |
3787 | .tpg_check_demo_mode = srpt_check_false, |
3788 | .tpg_check_demo_mode_cache = srpt_check_true, | |
3789 | .tpg_check_demo_mode_write_protect = srpt_check_true, | |
3790 | .tpg_check_prod_mode_write_protect = srpt_check_false, | |
a42d985b BVA |
3791 | .tpg_get_inst_index = srpt_tpg_get_inst_index, |
3792 | .release_cmd = srpt_release_cmd, | |
3793 | .check_stop_free = srpt_check_stop_free, | |
a42d985b | 3794 | .close_session = srpt_close_session, |
a42d985b BVA |
3795 | .sess_get_index = srpt_sess_get_index, |
3796 | .sess_get_initiator_sid = NULL, | |
3797 | .write_pending = srpt_write_pending, | |
a42d985b | 3798 | .set_default_node_attributes = srpt_set_default_node_attrs, |
a42d985b | 3799 | .get_cmd_state = srpt_get_tcm_cmd_state, |
b79fafac | 3800 | .queue_data_in = srpt_queue_data_in, |
a42d985b | 3801 | .queue_status = srpt_queue_status, |
b79fafac | 3802 | .queue_tm_rsp = srpt_queue_tm_rsp, |
131e6abc | 3803 | .aborted_task = srpt_aborted_task, |
a42d985b BVA |
3804 | /* |
3805 | * Setup function pointers for generic logic in | |
3806 | * target_core_fabric_configfs.c | |
3807 | */ | |
3808 | .fabric_make_wwn = srpt_make_tport, | |
3809 | .fabric_drop_wwn = srpt_drop_tport, | |
3810 | .fabric_make_tpg = srpt_make_tpg, | |
3811 | .fabric_drop_tpg = srpt_drop_tpg, | |
c7d6a803 | 3812 | .fabric_init_nodeacl = srpt_init_nodeacl, |
9ac8928e | 3813 | |
63cf1a90 | 3814 | .tfc_discovery_attrs = srpt_da_attrs, |
9ac8928e CH |
3815 | .tfc_wwn_attrs = srpt_wwn_attrs, |
3816 | .tfc_tpg_base_attrs = srpt_tpg_attrs, | |
3817 | .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs, | |
a42d985b BVA |
3818 | }; |
3819 | ||
3820 | /** | |
10eac19b | 3821 | * srpt_init_module - kernel module initialization |
a42d985b BVA |
3822 | * |
3823 | * Note: Since ib_register_client() registers callback functions, and since at | |
3824 | * least one of these callback functions (srpt_add_one()) calls target core | |
3825 | * functions, this driver must be registered with the target core before | |
3826 | * ib_register_client() is called. | |
3827 | */ | |
3828 | static int __init srpt_init_module(void) | |
3829 | { | |
3830 | int ret; | |
3831 | ||
3832 | ret = -EINVAL; | |
3833 | if (srp_max_req_size < MIN_MAX_REQ_SIZE) { | |
75d79b80 | 3834 | pr_err("invalid value %d for kernel module parameter srp_max_req_size -- must be at least %d.\n", |
a42d985b BVA |
3835 | srp_max_req_size, MIN_MAX_REQ_SIZE); |
3836 | goto out; | |
3837 | } | |
3838 | ||
3839 | if (srpt_srq_size < MIN_SRPT_SRQ_SIZE | |
3840 | || srpt_srq_size > MAX_SRPT_SRQ_SIZE) { | |
75d79b80 | 3841 | pr_err("invalid value %d for kernel module parameter srpt_srq_size -- must be in the range [%d..%d].\n", |
a42d985b BVA |
3842 | srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE); |
3843 | goto out; | |
3844 | } | |
3845 | ||
9ac8928e CH |
3846 | ret = target_register_template(&srpt_template); |
3847 | if (ret) | |
a42d985b | 3848 | goto out; |
a42d985b BVA |
3849 | |
3850 | ret = ib_register_client(&srpt_client); | |
3851 | if (ret) { | |
9f5d32af | 3852 | pr_err("couldn't register IB client\n"); |
a42d985b BVA |
3853 | goto out_unregister_target; |
3854 | } | |
3855 | ||
3856 | return 0; | |
3857 | ||
3858 | out_unregister_target: | |
9ac8928e | 3859 | target_unregister_template(&srpt_template); |
a42d985b BVA |
3860 | out: |
3861 | return ret; | |
3862 | } | |
3863 | ||
3864 | static void __exit srpt_cleanup_module(void) | |
3865 | { | |
63cf1a90 BVA |
3866 | if (rdma_cm_id) |
3867 | rdma_destroy_id(rdma_cm_id); | |
a42d985b | 3868 | ib_unregister_client(&srpt_client); |
9ac8928e | 3869 | target_unregister_template(&srpt_template); |
a42d985b BVA |
3870 | } |
3871 | ||
3872 | module_init(srpt_init_module); | |
3873 | module_exit(srpt_cleanup_module); |