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