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