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