2 * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
3 * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
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>
52 /* Name of this kernel module. */
53 #define DRV_NAME "ib_srpt"
54 #define DRV_VERSION "2.0.0"
55 #define DRV_RELDATE "2011-02-14"
57 #define SRPT_ID_STRING "Linux SRP target"
60 #define pr_fmt(fmt) DRV_NAME " " fmt
62 MODULE_AUTHOR("Vu Pham and Bart Van Assche");
63 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
64 "v" DRV_VERSION " (" DRV_RELDATE ")");
65 MODULE_LICENSE("Dual BSD/GPL");
71 static u64 srpt_service_guid;
72 static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
73 static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
75 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
76 module_param(srp_max_req_size, int, 0444);
77 MODULE_PARM_DESC(srp_max_req_size,
78 "Maximum size of SRP request messages in bytes.");
80 static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
81 module_param(srpt_srq_size, int, 0444);
82 MODULE_PARM_DESC(srpt_srq_size,
83 "Shared receive queue (SRQ) size.");
85 static int srpt_get_u64_x(char *buffer, const struct kernel_param *kp)
87 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
89 module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
91 MODULE_PARM_DESC(srpt_service_guid,
92 "Using this value for ioc_guid, id_ext, and cm_listen_id"
93 " instead of using the node_guid of the first HCA.");
95 static struct ib_client srpt_client;
96 /* Protects both rdma_cm_port and rdma_cm_id. */
97 static DEFINE_MUTEX(rdma_cm_mutex);
98 /* Port number RDMA/CM will bind to. */
99 static u16 rdma_cm_port;
100 static struct rdma_cm_id *rdma_cm_id;
101 static void srpt_release_cmd(struct se_cmd *se_cmd);
102 static void srpt_free_ch(struct kref *kref);
103 static int srpt_queue_status(struct se_cmd *cmd);
104 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
105 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
106 static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
109 * The only allowed channel state changes are those that change the channel
110 * state into a state with a higher numerical value. Hence the new > prev test.
112 static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
115 enum rdma_ch_state prev;
116 bool changed = false;
118 spin_lock_irqsave(&ch->spinlock, flags);
124 spin_unlock_irqrestore(&ch->spinlock, flags);
130 * srpt_event_handler - asynchronous IB event callback function
131 * @handler: IB event handler registered by ib_register_event_handler().
132 * @event: Description of the event that occurred.
134 * Callback function called by the InfiniBand core when an asynchronous IB
135 * event occurs. This callback may occur in interrupt context. See also
136 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
137 * Architecture Specification.
139 static void srpt_event_handler(struct ib_event_handler *handler,
140 struct ib_event *event)
142 struct srpt_device *sdev;
143 struct srpt_port *sport;
146 sdev = ib_get_client_data(event->device, &srpt_client);
147 if (!sdev || sdev->device != event->device)
150 pr_debug("ASYNC event= %d on device= %s\n", event->event,
153 switch (event->event) {
154 case IB_EVENT_PORT_ERR:
155 port_num = event->element.port_num - 1;
156 if (port_num < sdev->device->phys_port_cnt) {
157 sport = &sdev->port[port_num];
161 WARN(true, "event %d: port_num %d out of range 1..%d\n",
162 event->event, port_num + 1,
163 sdev->device->phys_port_cnt);
166 case IB_EVENT_PORT_ACTIVE:
167 case IB_EVENT_LID_CHANGE:
168 case IB_EVENT_PKEY_CHANGE:
169 case IB_EVENT_SM_CHANGE:
170 case IB_EVENT_CLIENT_REREGISTER:
171 case IB_EVENT_GID_CHANGE:
172 /* Refresh port data asynchronously. */
173 port_num = event->element.port_num - 1;
174 if (port_num < sdev->device->phys_port_cnt) {
175 sport = &sdev->port[port_num];
176 if (!sport->lid && !sport->sm_lid)
177 schedule_work(&sport->work);
179 WARN(true, "event %d: port_num %d out of range 1..%d\n",
180 event->event, port_num + 1,
181 sdev->device->phys_port_cnt);
185 pr_err("received unrecognized IB event %d\n", event->event);
191 * srpt_srq_event - SRQ event callback function
192 * @event: Description of the event that occurred.
193 * @ctx: Context pointer specified at SRQ creation time.
195 static void srpt_srq_event(struct ib_event *event, void *ctx)
197 pr_debug("SRQ event %d\n", event->event);
200 static const char *get_ch_state_name(enum rdma_ch_state s)
207 case CH_DISCONNECTING:
208 return "disconnecting";
211 case CH_DISCONNECTED:
212 return "disconnected";
218 * srpt_qp_event - QP event callback function
219 * @event: Description of the event that occurred.
220 * @ch: SRPT RDMA channel.
222 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
224 pr_debug("QP event %d on ch=%p sess_name=%s state=%d\n",
225 event->event, ch, ch->sess_name, ch->state);
227 switch (event->event) {
228 case IB_EVENT_COMM_EST:
229 if (ch->using_rdma_cm)
230 rdma_notify(ch->rdma_cm.cm_id, event->event);
232 ib_cm_notify(ch->ib_cm.cm_id, event->event);
234 case IB_EVENT_QP_LAST_WQE_REACHED:
235 pr_debug("%s-%d, state %s: received Last WQE event.\n",
236 ch->sess_name, ch->qp->qp_num,
237 get_ch_state_name(ch->state));
240 pr_err("received unrecognized IB QP event %d\n", event->event);
246 * srpt_set_ioc - initialize a IOUnitInfo structure
247 * @c_list: controller list.
248 * @slot: one-based slot number.
249 * @value: four-bit value.
251 * Copies the lowest four bits of value in element slot of the array of four
252 * bit elements called c_list (controller list). The index slot is one-based.
254 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
261 tmp = c_list[id] & 0xf;
262 c_list[id] = (value << 4) | tmp;
264 tmp = c_list[id] & 0xf0;
265 c_list[id] = (value & 0xf) | tmp;
270 * srpt_get_class_port_info - copy ClassPortInfo to a management datagram
271 * @mad: Datagram that will be sent as response to DM_ATTR_CLASS_PORT_INFO.
273 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
276 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
278 struct ib_class_port_info *cif;
280 cif = (struct ib_class_port_info *)mad->data;
281 memset(cif, 0, sizeof(*cif));
282 cif->base_version = 1;
283 cif->class_version = 1;
285 ib_set_cpi_resp_time(cif, 20);
286 mad->mad_hdr.status = 0;
290 * srpt_get_iou - write IOUnitInfo to a management datagram
291 * @mad: Datagram that will be sent as response to DM_ATTR_IOU_INFO.
293 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
294 * Specification. See also section B.7, table B.6 in the SRP r16a document.
296 static void srpt_get_iou(struct ib_dm_mad *mad)
298 struct ib_dm_iou_info *ioui;
302 ioui = (struct ib_dm_iou_info *)mad->data;
303 ioui->change_id = cpu_to_be16(1);
304 ioui->max_controllers = 16;
306 /* set present for slot 1 and empty for the rest */
307 srpt_set_ioc(ioui->controller_list, 1, 1);
308 for (i = 1, slot = 2; i < 16; i++, slot++)
309 srpt_set_ioc(ioui->controller_list, slot, 0);
311 mad->mad_hdr.status = 0;
315 * srpt_get_ioc - write IOControllerprofile to a management datagram
316 * @sport: HCA port through which the MAD has been received.
317 * @slot: Slot number specified in DM_ATTR_IOC_PROFILE query.
318 * @mad: Datagram that will be sent as response to DM_ATTR_IOC_PROFILE.
320 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
321 * Architecture Specification. See also section B.7, table B.7 in the SRP
324 static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
325 struct ib_dm_mad *mad)
327 struct srpt_device *sdev = sport->sdev;
328 struct ib_dm_ioc_profile *iocp;
329 int send_queue_depth;
331 iocp = (struct ib_dm_ioc_profile *)mad->data;
333 if (!slot || slot > 16) {
335 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
341 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
346 send_queue_depth = sdev->srq_size;
348 send_queue_depth = min(MAX_SRPT_RQ_SIZE,
349 sdev->device->attrs.max_qp_wr);
351 memset(iocp, 0, sizeof(*iocp));
352 strcpy(iocp->id_string, SRPT_ID_STRING);
353 iocp->guid = cpu_to_be64(srpt_service_guid);
354 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
355 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
356 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
357 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
358 iocp->subsys_device_id = 0x0;
359 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
360 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
361 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
362 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
363 iocp->send_queue_depth = cpu_to_be16(send_queue_depth);
364 iocp->rdma_read_depth = 4;
365 iocp->send_size = cpu_to_be32(srp_max_req_size);
366 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
368 iocp->num_svc_entries = 1;
369 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
370 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
372 mad->mad_hdr.status = 0;
376 * srpt_get_svc_entries - write ServiceEntries to a management datagram
377 * @ioc_guid: I/O controller GUID to use in reply.
378 * @slot: I/O controller number.
379 * @hi: End of the range of service entries to be specified in the reply.
380 * @lo: Start of the range of service entries to be specified in the reply..
381 * @mad: Datagram that will be sent as response to DM_ATTR_SVC_ENTRIES.
383 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
384 * Specification. See also section B.7, table B.8 in the SRP r16a document.
386 static void srpt_get_svc_entries(u64 ioc_guid,
387 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
389 struct ib_dm_svc_entries *svc_entries;
393 if (!slot || slot > 16) {
395 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
399 if (slot > 2 || lo > hi || hi > 1) {
401 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
405 svc_entries = (struct ib_dm_svc_entries *)mad->data;
406 memset(svc_entries, 0, sizeof(*svc_entries));
407 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
408 snprintf(svc_entries->service_entries[0].name,
409 sizeof(svc_entries->service_entries[0].name),
411 SRP_SERVICE_NAME_PREFIX,
414 mad->mad_hdr.status = 0;
418 * srpt_mgmt_method_get - process a received management datagram
419 * @sp: HCA port through which the MAD has been received.
420 * @rq_mad: received MAD.
421 * @rsp_mad: response MAD.
423 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
424 struct ib_dm_mad *rsp_mad)
430 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
432 case DM_ATTR_CLASS_PORT_INFO:
433 srpt_get_class_port_info(rsp_mad);
435 case DM_ATTR_IOU_INFO:
436 srpt_get_iou(rsp_mad);
438 case DM_ATTR_IOC_PROFILE:
439 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
440 srpt_get_ioc(sp, slot, rsp_mad);
442 case DM_ATTR_SVC_ENTRIES:
443 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
444 hi = (u8) ((slot >> 8) & 0xff);
445 lo = (u8) (slot & 0xff);
446 slot = (u16) ((slot >> 16) & 0xffff);
447 srpt_get_svc_entries(srpt_service_guid,
448 slot, hi, lo, rsp_mad);
451 rsp_mad->mad_hdr.status =
452 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
458 * srpt_mad_send_handler - MAD send completion callback
459 * @mad_agent: Return value of ib_register_mad_agent().
460 * @mad_wc: Work completion reporting that the MAD has been sent.
462 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
463 struct ib_mad_send_wc *mad_wc)
465 rdma_destroy_ah(mad_wc->send_buf->ah);
466 ib_free_send_mad(mad_wc->send_buf);
470 * srpt_mad_recv_handler - MAD reception callback function
471 * @mad_agent: Return value of ib_register_mad_agent().
472 * @send_buf: Not used.
473 * @mad_wc: Work completion reporting that a MAD has been received.
475 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
476 struct ib_mad_send_buf *send_buf,
477 struct ib_mad_recv_wc *mad_wc)
479 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
481 struct ib_mad_send_buf *rsp;
482 struct ib_dm_mad *dm_mad;
484 if (!mad_wc || !mad_wc->recv_buf.mad)
487 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
488 mad_wc->recv_buf.grh, mad_agent->port_num);
492 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
494 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
495 mad_wc->wc->pkey_index, 0,
496 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
498 IB_MGMT_BASE_VERSION);
505 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
506 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
507 dm_mad->mad_hdr.status = 0;
509 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
510 case IB_MGMT_METHOD_GET:
511 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
513 case IB_MGMT_METHOD_SET:
514 dm_mad->mad_hdr.status =
515 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
518 dm_mad->mad_hdr.status =
519 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
523 if (!ib_post_send_mad(rsp, NULL)) {
524 ib_free_recv_mad(mad_wc);
525 /* will destroy_ah & free_send_mad in send completion */
529 ib_free_send_mad(rsp);
534 ib_free_recv_mad(mad_wc);
537 static int srpt_format_guid(char *buf, unsigned int size, const __be64 *guid)
539 const __be16 *g = (const __be16 *)guid;
541 return snprintf(buf, size, "%04x:%04x:%04x:%04x",
542 be16_to_cpu(g[0]), be16_to_cpu(g[1]),
543 be16_to_cpu(g[2]), be16_to_cpu(g[3]));
547 * srpt_refresh_port - configure a HCA port
548 * @sport: SRPT HCA port.
550 * Enable InfiniBand management datagram processing, update the cached sm_lid,
551 * lid and gid values, and register a callback function for processing MADs
552 * on the specified port.
554 * Note: It is safe to call this function more than once for the same port.
556 static int srpt_refresh_port(struct srpt_port *sport)
558 struct ib_mad_reg_req reg_req;
559 struct ib_port_modify port_modify;
560 struct ib_port_attr port_attr;
563 memset(&port_modify, 0, sizeof(port_modify));
564 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
565 port_modify.clr_port_cap_mask = 0;
567 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
571 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
575 sport->sm_lid = port_attr.sm_lid;
576 sport->lid = port_attr.lid;
578 ret = rdma_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
582 sport->port_guid_wwn.priv = sport;
583 srpt_format_guid(sport->port_guid, sizeof(sport->port_guid),
584 &sport->gid.global.interface_id);
585 sport->port_gid_wwn.priv = sport;
586 snprintf(sport->port_gid, sizeof(sport->port_gid),
588 be64_to_cpu(sport->gid.global.subnet_prefix),
589 be64_to_cpu(sport->gid.global.interface_id));
591 if (!sport->mad_agent) {
592 memset(®_req, 0, sizeof(reg_req));
593 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
594 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
595 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
596 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
598 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
602 srpt_mad_send_handler,
603 srpt_mad_recv_handler,
605 if (IS_ERR(sport->mad_agent)) {
606 ret = PTR_ERR(sport->mad_agent);
607 sport->mad_agent = NULL;
616 port_modify.set_port_cap_mask = 0;
617 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
618 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
626 * srpt_unregister_mad_agent - unregister MAD callback functions
627 * @sdev: SRPT HCA pointer.
629 * Note: It is safe to call this function more than once for the same device.
631 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
633 struct ib_port_modify port_modify = {
634 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
636 struct srpt_port *sport;
639 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
640 sport = &sdev->port[i - 1];
641 WARN_ON(sport->port != i);
642 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
643 pr_err("disabling MAD processing failed.\n");
644 if (sport->mad_agent) {
645 ib_unregister_mad_agent(sport->mad_agent);
646 sport->mad_agent = NULL;
652 * srpt_alloc_ioctx - allocate a SRPT I/O context structure
653 * @sdev: SRPT HCA pointer.
654 * @ioctx_size: I/O context size.
655 * @dma_size: Size of I/O context DMA buffer.
656 * @dir: DMA data direction.
658 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
659 int ioctx_size, int dma_size,
660 enum dma_data_direction dir)
662 struct srpt_ioctx *ioctx;
664 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
668 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
672 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
673 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
687 * srpt_free_ioctx - free a SRPT I/O context structure
688 * @sdev: SRPT HCA pointer.
689 * @ioctx: I/O context pointer.
690 * @dma_size: Size of I/O context DMA buffer.
691 * @dir: DMA data direction.
693 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
694 int dma_size, enum dma_data_direction dir)
699 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
705 * srpt_alloc_ioctx_ring - allocate a ring of SRPT I/O context structures
706 * @sdev: Device to allocate the I/O context ring for.
707 * @ring_size: Number of elements in the I/O context ring.
708 * @ioctx_size: I/O context size.
709 * @dma_size: DMA buffer size.
710 * @dir: DMA data direction.
712 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
713 int ring_size, int ioctx_size,
714 int dma_size, enum dma_data_direction dir)
716 struct srpt_ioctx **ring;
719 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
720 && ioctx_size != sizeof(struct srpt_send_ioctx));
722 ring = kvmalloc_array(ring_size, sizeof(ring[0]), GFP_KERNEL);
725 for (i = 0; i < ring_size; ++i) {
726 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
735 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
743 * srpt_free_ioctx_ring - free the ring of SRPT I/O context structures
744 * @ioctx_ring: I/O context ring to be freed.
745 * @sdev: SRPT HCA pointer.
746 * @ring_size: Number of ring elements.
747 * @dma_size: Size of I/O context DMA buffer.
748 * @dir: DMA data direction.
750 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
751 struct srpt_device *sdev, int ring_size,
752 int dma_size, enum dma_data_direction dir)
759 for (i = 0; i < ring_size; ++i)
760 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
765 * srpt_set_cmd_state - set the state of a SCSI command
766 * @ioctx: Send I/O context.
767 * @new: New I/O context state.
769 * Does not modify the state of aborted commands. Returns the previous command
772 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
773 enum srpt_command_state new)
775 enum srpt_command_state previous;
777 previous = ioctx->state;
778 if (previous != SRPT_STATE_DONE)
785 * srpt_test_and_set_cmd_state - test and set the state of a command
786 * @ioctx: Send I/O context.
787 * @old: Current I/O context state.
788 * @new: New I/O context state.
790 * Returns true if and only if the previous command state was equal to 'old'.
792 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
793 enum srpt_command_state old,
794 enum srpt_command_state new)
796 enum srpt_command_state previous;
799 WARN_ON(old == SRPT_STATE_DONE);
800 WARN_ON(new == SRPT_STATE_NEW);
802 previous = ioctx->state;
806 return previous == old;
810 * srpt_post_recv - post an IB receive request
811 * @sdev: SRPT HCA pointer.
812 * @ch: SRPT RDMA channel.
813 * @ioctx: Receive I/O context pointer.
815 static int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch,
816 struct srpt_recv_ioctx *ioctx)
819 struct ib_recv_wr wr;
822 list.addr = ioctx->ioctx.dma;
823 list.length = srp_max_req_size;
824 list.lkey = sdev->lkey;
826 ioctx->ioctx.cqe.done = srpt_recv_done;
827 wr.wr_cqe = &ioctx->ioctx.cqe;
833 return ib_post_srq_recv(sdev->srq, &wr, NULL);
835 return ib_post_recv(ch->qp, &wr, NULL);
839 * srpt_zerolength_write - perform a zero-length RDMA write
840 * @ch: SRPT RDMA channel.
842 * A quote from the InfiniBand specification: C9-88: For an HCA responder
843 * using Reliable Connection service, for each zero-length RDMA READ or WRITE
844 * request, the R_Key shall not be validated, even if the request includes
847 static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
849 struct ib_rdma_wr wr = {
852 { .wr_cqe = &ch->zw_cqe, },
853 .opcode = IB_WR_RDMA_WRITE,
854 .send_flags = IB_SEND_SIGNALED,
858 pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
861 return ib_post_send(ch->qp, &wr.wr, NULL);
864 static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
866 struct srpt_rdma_ch *ch = cq->cq_context;
868 pr_debug("%s-%d wc->status %d\n", ch->sess_name, ch->qp->qp_num,
871 if (wc->status == IB_WC_SUCCESS) {
872 srpt_process_wait_list(ch);
874 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
875 schedule_work(&ch->release_work);
877 pr_debug("%s-%d: already disconnected.\n",
878 ch->sess_name, ch->qp->qp_num);
882 static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
883 struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
886 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
887 struct srpt_rdma_ch *ch = ioctx->ch;
888 struct scatterlist *prev = NULL;
893 ioctx->rw_ctxs = &ioctx->s_rw_ctx;
895 ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
901 for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
902 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
903 u64 remote_addr = be64_to_cpu(db->va);
904 u32 size = be32_to_cpu(db->len);
905 u32 rkey = be32_to_cpu(db->key);
907 ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
912 ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
913 ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
915 target_free_sgl(ctx->sg, ctx->nents);
919 ioctx->n_rdma += ret;
923 sg_unmark_end(&prev[prev_nents - 1]);
924 sg_chain(prev, prev_nents + 1, ctx->sg);
930 prev_nents = ctx->nents;
932 *sg_cnt += ctx->nents;
939 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
941 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
942 ctx->sg, ctx->nents, dir);
943 target_free_sgl(ctx->sg, ctx->nents);
945 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
946 kfree(ioctx->rw_ctxs);
950 static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
951 struct srpt_send_ioctx *ioctx)
953 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
956 for (i = 0; i < ioctx->n_rw_ctx; i++) {
957 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
959 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
960 ctx->sg, ctx->nents, dir);
961 target_free_sgl(ctx->sg, ctx->nents);
964 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
965 kfree(ioctx->rw_ctxs);
968 static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
971 * The pointer computations below will only be compiled correctly
972 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
973 * whether srp_cmd::add_data has been declared as a byte pointer.
975 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
976 !__same_type(srp_cmd->add_data[0], (u8)0));
979 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
980 * CDB LENGTH' field are reserved and the size in bytes of this field
981 * is four times the value specified in bits 3..7. Hence the "& ~3".
983 return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
987 * srpt_get_desc_tbl - parse the data descriptors of a SRP_CMD request
988 * @ioctx: Pointer to the I/O context associated with the request.
989 * @srp_cmd: Pointer to the SRP_CMD request data.
990 * @dir: Pointer to the variable to which the transfer direction will be
992 * @sg: [out] scatterlist allocated for the parsed SRP_CMD.
993 * @sg_cnt: [out] length of @sg.
994 * @data_len: Pointer to the variable to which the total data length of all
995 * descriptors in the SRP_CMD request will be written.
997 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
999 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
1000 * -ENOMEM when memory allocation fails and zero upon success.
1002 static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
1003 struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
1004 struct scatterlist **sg, unsigned *sg_cnt, u64 *data_len)
1010 * The lower four bits of the buffer format field contain the DATA-IN
1011 * buffer descriptor format, and the highest four bits contain the
1012 * DATA-OUT buffer descriptor format.
1014 if (srp_cmd->buf_fmt & 0xf)
1015 /* DATA-IN: transfer data from target to initiator (read). */
1016 *dir = DMA_FROM_DEVICE;
1017 else if (srp_cmd->buf_fmt >> 4)
1018 /* DATA-OUT: transfer data from initiator to target (write). */
1019 *dir = DMA_TO_DEVICE;
1023 /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
1024 ioctx->cmd.data_direction = *dir;
1026 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
1027 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
1028 struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
1030 *data_len = be32_to_cpu(db->len);
1031 return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
1032 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
1033 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
1034 struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
1035 int nbufs = be32_to_cpu(idb->table_desc.len) /
1036 sizeof(struct srp_direct_buf);
1039 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
1040 pr_err("received unsupported SRP_CMD request"
1041 " type (%u out + %u in != %u / %zu)\n",
1042 srp_cmd->data_out_desc_cnt,
1043 srp_cmd->data_in_desc_cnt,
1044 be32_to_cpu(idb->table_desc.len),
1045 sizeof(struct srp_direct_buf));
1049 *data_len = be32_to_cpu(idb->len);
1050 return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
1059 * srpt_init_ch_qp - initialize queue pair attributes
1060 * @ch: SRPT RDMA channel.
1061 * @qp: Queue pair pointer.
1063 * Initialized the attributes of queue pair 'qp' by allowing local write,
1064 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
1066 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1068 struct ib_qp_attr *attr;
1071 WARN_ON_ONCE(ch->using_rdma_cm);
1073 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1077 attr->qp_state = IB_QPS_INIT;
1078 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1079 attr->port_num = ch->sport->port;
1081 ret = ib_find_cached_pkey(ch->sport->sdev->device, ch->sport->port,
1082 ch->pkey, &attr->pkey_index);
1084 pr_err("Translating pkey %#x failed (%d) - using index 0\n",
1087 ret = ib_modify_qp(qp, attr,
1088 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
1096 * srpt_ch_qp_rtr - change the state of a channel to 'ready to receive' (RTR)
1097 * @ch: channel of the queue pair.
1098 * @qp: queue pair to change the state of.
1100 * Returns zero upon success and a negative value upon failure.
1102 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1103 * If this structure ever becomes larger, it might be necessary to allocate
1104 * it dynamically instead of on the stack.
1106 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1108 struct ib_qp_attr qp_attr;
1112 WARN_ON_ONCE(ch->using_rdma_cm);
1114 qp_attr.qp_state = IB_QPS_RTR;
1115 ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
1119 qp_attr.max_dest_rd_atomic = 4;
1121 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1128 * srpt_ch_qp_rts - change the state of a channel to 'ready to send' (RTS)
1129 * @ch: channel of the queue pair.
1130 * @qp: queue pair to change the state of.
1132 * Returns zero upon success and a negative value upon failure.
1134 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1135 * If this structure ever becomes larger, it might be necessary to allocate
1136 * it dynamically instead of on the stack.
1138 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1140 struct ib_qp_attr qp_attr;
1144 qp_attr.qp_state = IB_QPS_RTS;
1145 ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
1149 qp_attr.max_rd_atomic = 4;
1151 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1158 * srpt_ch_qp_err - set the channel queue pair state to 'error'
1159 * @ch: SRPT RDMA channel.
1161 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1163 struct ib_qp_attr qp_attr;
1165 qp_attr.qp_state = IB_QPS_ERR;
1166 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1170 * srpt_get_send_ioctx - obtain an I/O context for sending to the initiator
1171 * @ch: SRPT RDMA channel.
1173 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1175 struct srpt_send_ioctx *ioctx;
1176 unsigned long flags;
1181 spin_lock_irqsave(&ch->spinlock, flags);
1182 if (!list_empty(&ch->free_list)) {
1183 ioctx = list_first_entry(&ch->free_list,
1184 struct srpt_send_ioctx, free_list);
1185 list_del(&ioctx->free_list);
1187 spin_unlock_irqrestore(&ch->spinlock, flags);
1192 BUG_ON(ioctx->ch != ch);
1193 ioctx->state = SRPT_STATE_NEW;
1195 ioctx->n_rw_ctx = 0;
1196 ioctx->queue_status_only = false;
1198 * transport_init_se_cmd() does not initialize all fields, so do it
1201 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1202 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1208 * srpt_abort_cmd - abort a SCSI command
1209 * @ioctx: I/O context associated with the SCSI command.
1211 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1213 enum srpt_command_state state;
1218 * If the command is in a state where the target core is waiting for
1219 * the ib_srpt driver, change the state to the next state.
1222 state = ioctx->state;
1224 case SRPT_STATE_NEED_DATA:
1225 ioctx->state = SRPT_STATE_DATA_IN;
1227 case SRPT_STATE_CMD_RSP_SENT:
1228 case SRPT_STATE_MGMT_RSP_SENT:
1229 ioctx->state = SRPT_STATE_DONE;
1232 WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
1237 pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state,
1238 ioctx->state, ioctx->cmd.tag);
1241 case SRPT_STATE_NEW:
1242 case SRPT_STATE_DATA_IN:
1243 case SRPT_STATE_MGMT:
1244 case SRPT_STATE_DONE:
1246 * Do nothing - defer abort processing until
1247 * srpt_queue_response() is invoked.
1250 case SRPT_STATE_NEED_DATA:
1251 pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
1252 transport_generic_request_failure(&ioctx->cmd,
1253 TCM_CHECK_CONDITION_ABORT_CMD);
1255 case SRPT_STATE_CMD_RSP_SENT:
1257 * SRP_RSP sending failed or the SRP_RSP send completion has
1258 * not been received in time.
1260 transport_generic_free_cmd(&ioctx->cmd, 0);
1262 case SRPT_STATE_MGMT_RSP_SENT:
1263 transport_generic_free_cmd(&ioctx->cmd, 0);
1266 WARN(1, "Unexpected command state (%d)", state);
1274 * srpt_rdma_read_done - RDMA read completion callback
1275 * @cq: Completion queue.
1276 * @wc: Work completion.
1278 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1279 * the data that has been transferred via IB RDMA had to be postponed until the
1280 * check_stop_free() callback. None of this is necessary anymore and needs to
1283 static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1285 struct srpt_rdma_ch *ch = cq->cq_context;
1286 struct srpt_send_ioctx *ioctx =
1287 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
1289 WARN_ON(ioctx->n_rdma <= 0);
1290 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1293 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1294 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1296 srpt_abort_cmd(ioctx);
1300 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1301 SRPT_STATE_DATA_IN))
1302 target_execute_cmd(&ioctx->cmd);
1304 pr_err("%s[%d]: wrong state = %d\n", __func__,
1305 __LINE__, ioctx->state);
1309 * srpt_build_cmd_rsp - build a SRP_RSP response
1310 * @ch: RDMA channel through which the request has been received.
1311 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1312 * be built in the buffer ioctx->buf points at and hence this function will
1313 * overwrite the request data.
1314 * @tag: tag of the request for which this response is being generated.
1315 * @status: value for the STATUS field of the SRP_RSP information unit.
1317 * Returns the size in bytes of the SRP_RSP response.
1319 * An SRP_RSP response contains a SCSI status or service response. See also
1320 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1321 * response. See also SPC-2 for more information about sense data.
1323 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1324 struct srpt_send_ioctx *ioctx, u64 tag,
1327 struct srp_rsp *srp_rsp;
1328 const u8 *sense_data;
1329 int sense_data_len, max_sense_len;
1332 * The lowest bit of all SAM-3 status codes is zero (see also
1333 * paragraph 5.3 in SAM-3).
1335 WARN_ON(status & 1);
1337 srp_rsp = ioctx->ioctx.buf;
1340 sense_data = ioctx->sense_data;
1341 sense_data_len = ioctx->cmd.scsi_sense_length;
1342 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1344 memset(srp_rsp, 0, sizeof(*srp_rsp));
1345 srp_rsp->opcode = SRP_RSP;
1346 srp_rsp->req_lim_delta =
1347 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1349 srp_rsp->status = status;
1351 if (sense_data_len) {
1352 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1353 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1354 if (sense_data_len > max_sense_len) {
1355 pr_warn("truncated sense data from %d to %d"
1356 " bytes\n", sense_data_len, max_sense_len);
1357 sense_data_len = max_sense_len;
1360 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1361 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1362 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1365 return sizeof(*srp_rsp) + sense_data_len;
1369 * srpt_build_tskmgmt_rsp - build a task management response
1370 * @ch: RDMA channel through which the request has been received.
1371 * @ioctx: I/O context in which the SRP_RSP response will be built.
1372 * @rsp_code: RSP_CODE that will be stored in the response.
1373 * @tag: Tag of the request for which this response is being generated.
1375 * Returns the size in bytes of the SRP_RSP response.
1377 * An SRP_RSP response contains a SCSI status or service response. See also
1378 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1381 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1382 struct srpt_send_ioctx *ioctx,
1383 u8 rsp_code, u64 tag)
1385 struct srp_rsp *srp_rsp;
1390 resp_len = sizeof(*srp_rsp) + resp_data_len;
1392 srp_rsp = ioctx->ioctx.buf;
1394 memset(srp_rsp, 0, sizeof(*srp_rsp));
1396 srp_rsp->opcode = SRP_RSP;
1397 srp_rsp->req_lim_delta =
1398 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1401 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1402 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1403 srp_rsp->data[3] = rsp_code;
1408 static int srpt_check_stop_free(struct se_cmd *cmd)
1410 struct srpt_send_ioctx *ioctx = container_of(cmd,
1411 struct srpt_send_ioctx, cmd);
1413 return target_put_sess_cmd(&ioctx->cmd);
1417 * srpt_handle_cmd - process a SRP_CMD information unit
1418 * @ch: SRPT RDMA channel.
1419 * @recv_ioctx: Receive I/O context.
1420 * @send_ioctx: Send I/O context.
1422 static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1423 struct srpt_recv_ioctx *recv_ioctx,
1424 struct srpt_send_ioctx *send_ioctx)
1427 struct srp_cmd *srp_cmd;
1428 struct scatterlist *sg = NULL;
1429 unsigned sg_cnt = 0;
1431 enum dma_data_direction dir;
1434 BUG_ON(!send_ioctx);
1436 srp_cmd = recv_ioctx->ioctx.buf;
1437 cmd = &send_ioctx->cmd;
1438 cmd->tag = srp_cmd->tag;
1440 switch (srp_cmd->task_attr) {
1441 case SRP_CMD_SIMPLE_Q:
1442 cmd->sam_task_attr = TCM_SIMPLE_TAG;
1444 case SRP_CMD_ORDERED_Q:
1446 cmd->sam_task_attr = TCM_ORDERED_TAG;
1448 case SRP_CMD_HEAD_OF_Q:
1449 cmd->sam_task_attr = TCM_HEAD_TAG;
1452 cmd->sam_task_attr = TCM_ACA_TAG;
1456 rc = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &sg, &sg_cnt,
1459 if (rc != -EAGAIN) {
1460 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1466 rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
1467 &send_ioctx->sense_data[0],
1468 scsilun_to_int(&srp_cmd->lun), data_len,
1469 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
1470 sg, sg_cnt, NULL, 0, NULL, 0);
1472 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1479 send_ioctx->state = SRPT_STATE_DONE;
1480 srpt_release_cmd(cmd);
1483 static int srp_tmr_to_tcm(int fn)
1486 case SRP_TSK_ABORT_TASK:
1487 return TMR_ABORT_TASK;
1488 case SRP_TSK_ABORT_TASK_SET:
1489 return TMR_ABORT_TASK_SET;
1490 case SRP_TSK_CLEAR_TASK_SET:
1491 return TMR_CLEAR_TASK_SET;
1492 case SRP_TSK_LUN_RESET:
1493 return TMR_LUN_RESET;
1494 case SRP_TSK_CLEAR_ACA:
1495 return TMR_CLEAR_ACA;
1502 * srpt_handle_tsk_mgmt - process a SRP_TSK_MGMT information unit
1503 * @ch: SRPT RDMA channel.
1504 * @recv_ioctx: Receive I/O context.
1505 * @send_ioctx: Send I/O context.
1507 * Returns 0 if and only if the request will be processed by the target core.
1509 * For more information about SRP_TSK_MGMT information units, see also section
1510 * 6.7 in the SRP r16a document.
1512 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1513 struct srpt_recv_ioctx *recv_ioctx,
1514 struct srpt_send_ioctx *send_ioctx)
1516 struct srp_tsk_mgmt *srp_tsk;
1518 struct se_session *sess = ch->sess;
1522 BUG_ON(!send_ioctx);
1524 srp_tsk = recv_ioctx->ioctx.buf;
1525 cmd = &send_ioctx->cmd;
1527 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld ch %p sess %p\n",
1528 srp_tsk->tsk_mgmt_func, srp_tsk->task_tag, srp_tsk->tag, ch,
1531 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1532 send_ioctx->cmd.tag = srp_tsk->tag;
1533 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1534 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1535 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1536 GFP_KERNEL, srp_tsk->task_tag,
1537 TARGET_SCF_ACK_KREF);
1539 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1544 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
1548 * srpt_handle_new_iu - process a newly received information unit
1549 * @ch: RDMA channel through which the information unit has been received.
1550 * @recv_ioctx: Receive I/O context associated with the information unit.
1553 srpt_handle_new_iu(struct srpt_rdma_ch *ch, struct srpt_recv_ioctx *recv_ioctx)
1555 struct srpt_send_ioctx *send_ioctx = NULL;
1556 struct srp_cmd *srp_cmd;
1561 BUG_ON(!recv_ioctx);
1563 if (unlikely(ch->state == CH_CONNECTING))
1566 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1567 recv_ioctx->ioctx.dma, srp_max_req_size,
1570 srp_cmd = recv_ioctx->ioctx.buf;
1571 opcode = srp_cmd->opcode;
1572 if (opcode == SRP_CMD || opcode == SRP_TSK_MGMT) {
1573 send_ioctx = srpt_get_send_ioctx(ch);
1574 if (unlikely(!send_ioctx))
1578 if (!list_empty(&recv_ioctx->wait_list)) {
1579 WARN_ON_ONCE(!ch->processing_wait_list);
1580 list_del_init(&recv_ioctx->wait_list);
1585 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1588 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1591 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
1594 pr_debug("received SRP_CRED_RSP\n");
1597 pr_debug("received SRP_AER_RSP\n");
1600 pr_err("Received SRP_RSP\n");
1603 pr_err("received IU with unknown opcode 0x%x\n", opcode);
1607 srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
1614 if (list_empty(&recv_ioctx->wait_list)) {
1615 WARN_ON_ONCE(ch->processing_wait_list);
1616 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1621 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1623 struct srpt_rdma_ch *ch = cq->cq_context;
1624 struct srpt_recv_ioctx *ioctx =
1625 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
1627 if (wc->status == IB_WC_SUCCESS) {
1630 req_lim = atomic_dec_return(&ch->req_lim);
1631 if (unlikely(req_lim < 0))
1632 pr_err("req_lim = %d < 0\n", req_lim);
1633 srpt_handle_new_iu(ch, ioctx);
1635 pr_info_ratelimited("receiving failed for ioctx %p with status %d\n",
1641 * This function must be called from the context in which RDMA completions are
1642 * processed because it accesses the wait list without protection against
1643 * access from other threads.
1645 static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
1647 struct srpt_recv_ioctx *recv_ioctx, *tmp;
1649 WARN_ON_ONCE(ch->state == CH_CONNECTING);
1651 if (list_empty(&ch->cmd_wait_list))
1654 WARN_ON_ONCE(ch->processing_wait_list);
1655 ch->processing_wait_list = true;
1656 list_for_each_entry_safe(recv_ioctx, tmp, &ch->cmd_wait_list,
1658 if (!srpt_handle_new_iu(ch, recv_ioctx))
1661 ch->processing_wait_list = false;
1665 * srpt_send_done - send completion callback
1666 * @cq: Completion queue.
1667 * @wc: Work completion.
1669 * Note: Although this has not yet been observed during tests, at least in
1670 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1671 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1672 * value in each response is set to one, and it is possible that this response
1673 * makes the initiator send a new request before the send completion for that
1674 * response has been processed. This could e.g. happen if the call to
1675 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1676 * if IB retransmission causes generation of the send completion to be
1677 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1678 * are queued on cmd_wait_list. The code below processes these delayed
1679 * requests one at a time.
1681 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
1683 struct srpt_rdma_ch *ch = cq->cq_context;
1684 struct srpt_send_ioctx *ioctx =
1685 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1686 enum srpt_command_state state;
1688 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1690 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1691 state != SRPT_STATE_MGMT_RSP_SENT);
1693 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
1695 if (wc->status != IB_WC_SUCCESS)
1696 pr_info("sending response for ioctx 0x%p failed"
1697 " with status %d\n", ioctx, wc->status);
1699 if (state != SRPT_STATE_DONE) {
1700 transport_generic_free_cmd(&ioctx->cmd, 0);
1702 pr_err("IB completion has been received too late for"
1703 " wr_id = %u.\n", ioctx->ioctx.index);
1706 srpt_process_wait_list(ch);
1710 * srpt_create_ch_ib - create receive and send completion queues
1711 * @ch: SRPT RDMA channel.
1713 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1715 struct ib_qp_init_attr *qp_init;
1716 struct srpt_port *sport = ch->sport;
1717 struct srpt_device *sdev = sport->sdev;
1718 const struct ib_device_attr *attrs = &sdev->device->attrs;
1719 int sq_size = sport->port_attrib.srp_sq_size;
1722 WARN_ON(ch->rq_size < 1);
1725 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
1730 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + sq_size,
1731 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
1732 if (IS_ERR(ch->cq)) {
1733 ret = PTR_ERR(ch->cq);
1734 pr_err("failed to create CQ cqe= %d ret= %d\n",
1735 ch->rq_size + sq_size, ret);
1739 qp_init->qp_context = (void *)ch;
1740 qp_init->event_handler
1741 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1742 qp_init->send_cq = ch->cq;
1743 qp_init->recv_cq = ch->cq;
1744 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1745 qp_init->qp_type = IB_QPT_RC;
1747 * We divide up our send queue size into half SEND WRs to send the
1748 * completions, and half R/W contexts to actually do the RDMA
1749 * READ/WRITE transfers. Note that we need to allocate CQ slots for
1750 * both both, as RDMA contexts will also post completions for the
1753 qp_init->cap.max_send_wr = min(sq_size / 2, attrs->max_qp_wr);
1754 qp_init->cap.max_rdma_ctxs = sq_size / 2;
1755 qp_init->cap.max_send_sge = min(attrs->max_send_sge,
1756 SRPT_MAX_SG_PER_WQE);
1757 qp_init->port_num = ch->sport->port;
1758 if (sdev->use_srq) {
1759 qp_init->srq = sdev->srq;
1761 qp_init->cap.max_recv_wr = ch->rq_size;
1762 qp_init->cap.max_recv_sge = min(attrs->max_recv_sge,
1763 SRPT_MAX_SG_PER_WQE);
1766 if (ch->using_rdma_cm) {
1767 ret = rdma_create_qp(ch->rdma_cm.cm_id, sdev->pd, qp_init);
1768 ch->qp = ch->rdma_cm.cm_id->qp;
1770 ch->qp = ib_create_qp(sdev->pd, qp_init);
1771 if (!IS_ERR(ch->qp)) {
1772 ret = srpt_init_ch_qp(ch, ch->qp);
1774 ib_destroy_qp(ch->qp);
1776 ret = PTR_ERR(ch->qp);
1780 bool retry = sq_size > MIN_SRPT_SQ_SIZE;
1783 pr_debug("failed to create queue pair with sq_size = %d (%d) - retrying\n",
1786 sq_size = max(sq_size / 2, MIN_SRPT_SQ_SIZE);
1789 pr_err("failed to create queue pair with sq_size = %d (%d)\n",
1791 goto err_destroy_cq;
1795 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1797 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d ch= %p\n",
1798 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1799 qp_init->cap.max_send_wr, ch);
1802 for (i = 0; i < ch->rq_size; i++)
1803 srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]);
1815 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1817 ib_destroy_qp(ch->qp);
1822 * srpt_close_ch - close a RDMA channel
1823 * @ch: SRPT RDMA channel.
1825 * Make sure all resources associated with the channel will be deallocated at
1826 * an appropriate time.
1828 * Returns true if and only if the channel state has been modified into
1831 static bool srpt_close_ch(struct srpt_rdma_ch *ch)
1835 if (!srpt_set_ch_state(ch, CH_DRAINING)) {
1836 pr_debug("%s: already closed\n", ch->sess_name);
1840 kref_get(&ch->kref);
1842 ret = srpt_ch_qp_err(ch);
1844 pr_err("%s-%d: changing queue pair into error state failed: %d\n",
1845 ch->sess_name, ch->qp->qp_num, ret);
1847 ret = srpt_zerolength_write(ch);
1849 pr_err("%s-%d: queuing zero-length write failed: %d\n",
1850 ch->sess_name, ch->qp->qp_num, ret);
1851 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
1852 schedule_work(&ch->release_work);
1857 kref_put(&ch->kref, srpt_free_ch);
1863 * Change the channel state into CH_DISCONNECTING. If a channel has not yet
1864 * reached the connected state, close it. If a channel is in the connected
1865 * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
1866 * the responsibility of the caller to ensure that this function is not
1867 * invoked concurrently with the code that accepts a connection. This means
1868 * that this function must either be invoked from inside a CM callback
1869 * function or that it must be invoked with the srpt_port.mutex held.
1871 static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
1875 if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
1878 if (ch->using_rdma_cm) {
1879 ret = rdma_disconnect(ch->rdma_cm.cm_id);
1881 ret = ib_send_cm_dreq(ch->ib_cm.cm_id, NULL, 0);
1883 ret = ib_send_cm_drep(ch->ib_cm.cm_id, NULL, 0);
1886 if (ret < 0 && srpt_close_ch(ch))
1892 static bool srpt_ch_closed(struct srpt_port *sport, struct srpt_rdma_ch *ch)
1894 struct srpt_nexus *nexus;
1895 struct srpt_rdma_ch *ch2;
1899 list_for_each_entry(nexus, &sport->nexus_list, entry) {
1900 list_for_each_entry(ch2, &nexus->ch_list, list) {
1913 /* Send DREQ and wait for DREP. */
1914 static void srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch)
1916 struct srpt_port *sport = ch->sport;
1918 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
1921 mutex_lock(&sport->mutex);
1922 srpt_disconnect_ch(ch);
1923 mutex_unlock(&sport->mutex);
1925 while (wait_event_timeout(sport->ch_releaseQ, srpt_ch_closed(sport, ch),
1927 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
1928 ch->sess_name, ch->qp->qp_num, ch->state);
1932 static void __srpt_close_all_ch(struct srpt_port *sport)
1934 struct srpt_nexus *nexus;
1935 struct srpt_rdma_ch *ch;
1937 lockdep_assert_held(&sport->mutex);
1939 list_for_each_entry(nexus, &sport->nexus_list, entry) {
1940 list_for_each_entry(ch, &nexus->ch_list, list) {
1941 if (srpt_disconnect_ch(ch) >= 0)
1942 pr_info("Closing channel %s because target %s_%d has been disabled\n",
1944 sport->sdev->device->name, sport->port);
1951 * Look up (i_port_id, t_port_id) in sport->nexus_list. Create an entry if
1952 * it does not yet exist.
1954 static struct srpt_nexus *srpt_get_nexus(struct srpt_port *sport,
1955 const u8 i_port_id[16],
1956 const u8 t_port_id[16])
1958 struct srpt_nexus *nexus = NULL, *tmp_nexus = NULL, *n;
1961 mutex_lock(&sport->mutex);
1962 list_for_each_entry(n, &sport->nexus_list, entry) {
1963 if (memcmp(n->i_port_id, i_port_id, 16) == 0 &&
1964 memcmp(n->t_port_id, t_port_id, 16) == 0) {
1969 if (!nexus && tmp_nexus) {
1970 list_add_tail_rcu(&tmp_nexus->entry,
1971 &sport->nexus_list);
1972 swap(nexus, tmp_nexus);
1974 mutex_unlock(&sport->mutex);
1978 tmp_nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
1980 nexus = ERR_PTR(-ENOMEM);
1983 INIT_LIST_HEAD(&tmp_nexus->ch_list);
1984 memcpy(tmp_nexus->i_port_id, i_port_id, 16);
1985 memcpy(tmp_nexus->t_port_id, t_port_id, 16);
1993 static void srpt_set_enabled(struct srpt_port *sport, bool enabled)
1994 __must_hold(&sport->mutex)
1996 lockdep_assert_held(&sport->mutex);
1998 if (sport->enabled == enabled)
2000 sport->enabled = enabled;
2002 __srpt_close_all_ch(sport);
2005 static void srpt_free_ch(struct kref *kref)
2007 struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
2012 static void srpt_release_channel_work(struct work_struct *w)
2014 struct srpt_rdma_ch *ch;
2015 struct srpt_device *sdev;
2016 struct srpt_port *sport;
2017 struct se_session *se_sess;
2019 ch = container_of(w, struct srpt_rdma_ch, release_work);
2020 pr_debug("%s-%d\n", ch->sess_name, ch->qp->qp_num);
2022 sdev = ch->sport->sdev;
2028 target_sess_cmd_list_set_waiting(se_sess);
2029 target_wait_for_sess_cmds(se_sess);
2031 transport_deregister_session_configfs(se_sess);
2032 transport_deregister_session(se_sess);
2035 if (ch->using_rdma_cm)
2036 rdma_destroy_id(ch->rdma_cm.cm_id);
2038 ib_destroy_cm_id(ch->ib_cm.cm_id);
2040 srpt_destroy_ch_ib(ch);
2042 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2043 ch->sport->sdev, ch->rq_size,
2044 ch->max_rsp_size, DMA_TO_DEVICE);
2046 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2048 srp_max_req_size, DMA_FROM_DEVICE);
2051 mutex_lock(&sport->mutex);
2052 list_del_rcu(&ch->list);
2053 mutex_unlock(&sport->mutex);
2055 wake_up(&sport->ch_releaseQ);
2057 kref_put(&ch->kref, srpt_free_ch);
2061 * srpt_cm_req_recv - process the event IB_CM_REQ_RECEIVED
2062 * @sdev: HCA through which the login request was received.
2063 * @ib_cm_id: IB/CM connection identifier in case of IB/CM.
2064 * @rdma_cm_id: RDMA/CM connection identifier in case of RDMA/CM.
2065 * @port_num: Port through which the REQ message was received.
2066 * @pkey: P_Key of the incoming connection.
2067 * @req: SRP login request.
2068 * @src_addr: GID (IB/CM) or IP address (RDMA/CM) of the port that submitted
2069 * the login request.
2071 * Ownership of the cm_id is transferred to the target session if this
2072 * function returns zero. Otherwise the caller remains the owner of cm_id.
2074 static int srpt_cm_req_recv(struct srpt_device *const sdev,
2075 struct ib_cm_id *ib_cm_id,
2076 struct rdma_cm_id *rdma_cm_id,
2077 u8 port_num, __be16 pkey,
2078 const struct srp_login_req *req,
2079 const char *src_addr)
2081 struct srpt_port *sport = &sdev->port[port_num - 1];
2082 struct srpt_nexus *nexus;
2083 struct srp_login_rsp *rsp = NULL;
2084 struct srp_login_rej *rej = NULL;
2086 struct rdma_conn_param rdma_cm;
2087 struct ib_cm_rep_param ib_cm;
2088 } *rep_param = NULL;
2089 struct srpt_rdma_ch *ch = NULL;
2094 WARN_ON_ONCE(irqs_disabled());
2096 if (WARN_ON(!sdev || !req))
2099 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2101 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",
2102 req->initiator_port_id, req->target_port_id, it_iu_len,
2103 port_num, &sport->gid, be16_to_cpu(pkey));
2105 nexus = srpt_get_nexus(sport, req->initiator_port_id,
2106 req->target_port_id);
2107 if (IS_ERR(nexus)) {
2108 ret = PTR_ERR(nexus);
2113 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2114 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2115 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
2116 if (!rsp || !rej || !rep_param)
2120 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2121 rej->reason = cpu_to_be32(
2122 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2123 pr_err("rejected SRP_LOGIN_REQ because its length (%d bytes) is out of range (%d .. %d)\n",
2124 it_iu_len, 64, srp_max_req_size);
2128 if (!sport->enabled) {
2129 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2130 pr_info("rejected SRP_LOGIN_REQ because target port %s_%d has not yet been enabled\n",
2131 sport->sdev->device->name, port_num);
2135 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2136 || *(__be64 *)(req->target_port_id + 8) !=
2137 cpu_to_be64(srpt_service_guid)) {
2138 rej->reason = cpu_to_be32(
2139 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2140 pr_err("rejected SRP_LOGIN_REQ because it has an invalid target port identifier.\n");
2145 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
2147 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2148 pr_err("rejected SRP_LOGIN_REQ because out of memory.\n");
2152 kref_init(&ch->kref);
2153 ch->pkey = be16_to_cpu(pkey);
2155 ch->zw_cqe.done = srpt_zerolength_write_done;
2156 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2159 ch->ib_cm.cm_id = ib_cm_id;
2160 ib_cm_id->context = ch;
2162 ch->using_rdma_cm = true;
2163 ch->rdma_cm.cm_id = rdma_cm_id;
2164 rdma_cm_id->context = ch;
2167 * ch->rq_size should be at least as large as the initiator queue
2168 * depth to avoid that the initiator driver has to report QUEUE_FULL
2169 * to the SCSI mid-layer.
2171 ch->rq_size = min(MAX_SRPT_RQ_SIZE, sdev->device->attrs.max_qp_wr);
2172 spin_lock_init(&ch->spinlock);
2173 ch->state = CH_CONNECTING;
2174 INIT_LIST_HEAD(&ch->cmd_wait_list);
2175 ch->max_rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2177 ch->ioctx_ring = (struct srpt_send_ioctx **)
2178 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2179 sizeof(*ch->ioctx_ring[0]),
2180 ch->max_rsp_size, DMA_TO_DEVICE);
2181 if (!ch->ioctx_ring) {
2182 pr_err("rejected SRP_LOGIN_REQ because creating a new QP SQ ring failed.\n");
2183 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2187 INIT_LIST_HEAD(&ch->free_list);
2188 for (i = 0; i < ch->rq_size; i++) {
2189 ch->ioctx_ring[i]->ch = ch;
2190 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2192 if (!sdev->use_srq) {
2193 ch->ioctx_recv_ring = (struct srpt_recv_ioctx **)
2194 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2195 sizeof(*ch->ioctx_recv_ring[0]),
2198 if (!ch->ioctx_recv_ring) {
2199 pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n");
2201 cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2204 for (i = 0; i < ch->rq_size; i++)
2205 INIT_LIST_HEAD(&ch->ioctx_recv_ring[i]->wait_list);
2208 ret = srpt_create_ch_ib(ch);
2210 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2211 pr_err("rejected SRP_LOGIN_REQ because creating a new RDMA channel failed.\n");
2212 goto free_recv_ring;
2215 strlcpy(ch->sess_name, src_addr, sizeof(ch->sess_name));
2216 snprintf(i_port_id, sizeof(i_port_id), "0x%016llx%016llx",
2217 be64_to_cpu(*(__be64 *)nexus->i_port_id),
2218 be64_to_cpu(*(__be64 *)(nexus->i_port_id + 8)));
2220 pr_debug("registering session %s\n", ch->sess_name);
2222 if (sport->port_guid_tpg.se_tpg_wwn)
2223 ch->sess = target_alloc_session(&sport->port_guid_tpg, 0, 0,
2225 ch->sess_name, ch, NULL);
2226 if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2227 ch->sess = target_alloc_session(&sport->port_gid_tpg, 0, 0,
2228 TARGET_PROT_NORMAL, i_port_id, ch,
2230 /* Retry without leading "0x" */
2231 if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2232 ch->sess = target_alloc_session(&sport->port_gid_tpg, 0, 0,
2234 i_port_id + 2, ch, NULL);
2235 if (IS_ERR_OR_NULL(ch->sess)) {
2236 WARN_ON_ONCE(ch->sess == NULL);
2237 ret = PTR_ERR(ch->sess);
2239 pr_info("Rejected login for initiator %s: ret = %d.\n",
2240 ch->sess_name, ret);
2241 rej->reason = cpu_to_be32(ret == -ENOMEM ?
2242 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
2243 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2247 mutex_lock(&sport->mutex);
2249 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2250 struct srpt_rdma_ch *ch2;
2252 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2254 list_for_each_entry(ch2, &nexus->ch_list, list) {
2255 if (srpt_disconnect_ch(ch2) < 0)
2257 pr_info("Relogin - closed existing channel %s\n",
2259 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2262 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2265 list_add_tail_rcu(&ch->list, &nexus->ch_list);
2267 if (!sport->enabled) {
2268 rej->reason = cpu_to_be32(
2269 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2270 pr_info("rejected SRP_LOGIN_REQ because target %s_%d is not enabled\n",
2271 sdev->device->name, port_num);
2272 mutex_unlock(&sport->mutex);
2276 mutex_unlock(&sport->mutex);
2278 ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rtr(ch, ch->qp);
2280 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2281 pr_err("rejected SRP_LOGIN_REQ because enabling RTR failed (error code = %d)\n",
2286 pr_debug("Establish connection sess=%p name=%s ch=%p\n", ch->sess,
2289 /* create srp_login_response */
2290 rsp->opcode = SRP_LOGIN_RSP;
2291 rsp->tag = req->tag;
2292 rsp->max_it_iu_len = req->req_it_iu_len;
2293 rsp->max_ti_iu_len = req->req_it_iu_len;
2294 ch->max_ti_iu_len = it_iu_len;
2295 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
2296 SRP_BUF_FORMAT_INDIRECT);
2297 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2298 atomic_set(&ch->req_lim, ch->rq_size);
2299 atomic_set(&ch->req_lim_delta, 0);
2301 /* create cm reply */
2302 if (ch->using_rdma_cm) {
2303 rep_param->rdma_cm.private_data = (void *)rsp;
2304 rep_param->rdma_cm.private_data_len = sizeof(*rsp);
2305 rep_param->rdma_cm.rnr_retry_count = 7;
2306 rep_param->rdma_cm.flow_control = 1;
2307 rep_param->rdma_cm.responder_resources = 4;
2308 rep_param->rdma_cm.initiator_depth = 4;
2310 rep_param->ib_cm.qp_num = ch->qp->qp_num;
2311 rep_param->ib_cm.private_data = (void *)rsp;
2312 rep_param->ib_cm.private_data_len = sizeof(*rsp);
2313 rep_param->ib_cm.rnr_retry_count = 7;
2314 rep_param->ib_cm.flow_control = 1;
2315 rep_param->ib_cm.failover_accepted = 0;
2316 rep_param->ib_cm.srq = 1;
2317 rep_param->ib_cm.responder_resources = 4;
2318 rep_param->ib_cm.initiator_depth = 4;
2322 * Hold the sport mutex while accepting a connection to avoid that
2323 * srpt_disconnect_ch() is invoked concurrently with this code.
2325 mutex_lock(&sport->mutex);
2326 if (sport->enabled && ch->state == CH_CONNECTING) {
2327 if (ch->using_rdma_cm)
2328 ret = rdma_accept(rdma_cm_id, &rep_param->rdma_cm);
2330 ret = ib_send_cm_rep(ib_cm_id, &rep_param->ib_cm);
2334 mutex_unlock(&sport->mutex);
2342 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2343 pr_err("sending SRP_LOGIN_REQ response failed (error code = %d)\n",
2351 srpt_destroy_ch_ib(ch);
2354 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2355 ch->sport->sdev, ch->rq_size,
2356 srp_max_req_size, DMA_FROM_DEVICE);
2359 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2360 ch->sport->sdev, ch->rq_size,
2361 ch->max_rsp_size, DMA_TO_DEVICE);
2365 rdma_cm_id->context = NULL;
2367 ib_cm_id->context = NULL;
2371 WARN_ON_ONCE(ret == 0);
2374 pr_info("Rejecting login with reason %#x\n", be32_to_cpu(rej->reason));
2375 rej->opcode = SRP_LOGIN_REJ;
2376 rej->tag = req->tag;
2377 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
2378 SRP_BUF_FORMAT_INDIRECT);
2381 rdma_reject(rdma_cm_id, rej, sizeof(*rej));
2383 ib_send_cm_rej(ib_cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2386 if (ch && ch->sess) {
2389 * Tell the caller not to free cm_id since
2390 * srpt_release_channel_work() will do that.
2403 static int srpt_ib_cm_req_recv(struct ib_cm_id *cm_id,
2404 struct ib_cm_req_event_param *param,
2409 srpt_format_guid(sguid, sizeof(sguid),
2410 ¶m->primary_path->dgid.global.interface_id);
2412 return srpt_cm_req_recv(cm_id->context, cm_id, NULL, param->port,
2413 param->primary_path->pkey,
2414 private_data, sguid);
2417 static int srpt_rdma_cm_req_recv(struct rdma_cm_id *cm_id,
2418 struct rdma_cm_event *event)
2420 struct srpt_device *sdev;
2421 struct srp_login_req req;
2422 const struct srp_login_req_rdma *req_rdma;
2425 sdev = ib_get_client_data(cm_id->device, &srpt_client);
2427 return -ECONNREFUSED;
2429 if (event->param.conn.private_data_len < sizeof(*req_rdma))
2432 /* Transform srp_login_req_rdma into srp_login_req. */
2433 req_rdma = event->param.conn.private_data;
2434 memset(&req, 0, sizeof(req));
2435 req.opcode = req_rdma->opcode;
2436 req.tag = req_rdma->tag;
2437 req.req_it_iu_len = req_rdma->req_it_iu_len;
2438 req.req_buf_fmt = req_rdma->req_buf_fmt;
2439 req.req_flags = req_rdma->req_flags;
2440 memcpy(req.initiator_port_id, req_rdma->initiator_port_id, 16);
2441 memcpy(req.target_port_id, req_rdma->target_port_id, 16);
2443 snprintf(src_addr, sizeof(src_addr), "%pIS",
2444 &cm_id->route.addr.src_addr);
2446 return srpt_cm_req_recv(sdev, NULL, cm_id, cm_id->port_num,
2447 cm_id->route.path_rec->pkey, &req, src_addr);
2450 static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
2451 enum ib_cm_rej_reason reason,
2452 const u8 *private_data,
2453 u8 private_data_len)
2458 if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
2460 for (i = 0; i < private_data_len; i++)
2461 sprintf(priv + 3 * i, " %02x", private_data[i]);
2463 pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
2464 ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
2465 "; private data" : "", priv ? priv : " (?)");
2470 * srpt_cm_rtu_recv - process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event
2471 * @ch: SRPT RDMA channel.
2473 * An RTU (ready to use) message indicates that the connection has been
2474 * established and that the recipient may begin transmitting.
2476 static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
2480 ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rts(ch, ch->qp);
2482 pr_err("%s-%d: QP transition to RTS failed\n", ch->sess_name,
2489 * Note: calling srpt_close_ch() if the transition to the LIVE state
2490 * fails is not necessary since that means that that function has
2491 * already been invoked from another thread.
2493 if (!srpt_set_ch_state(ch, CH_LIVE)) {
2494 pr_err("%s-%d: channel transition to LIVE state failed\n",
2495 ch->sess_name, ch->qp->qp_num);
2499 /* Trigger wait list processing. */
2500 ret = srpt_zerolength_write(ch);
2501 WARN_ONCE(ret < 0, "%d\n", ret);
2505 * srpt_cm_handler - IB connection manager callback function
2506 * @cm_id: IB/CM connection identifier.
2507 * @event: IB/CM event.
2509 * A non-zero return value will cause the caller destroy the CM ID.
2511 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2512 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2513 * a non-zero value in any other case will trigger a race with the
2514 * ib_destroy_cm_id() call in srpt_release_channel().
2516 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2518 struct srpt_rdma_ch *ch = cm_id->context;
2522 switch (event->event) {
2523 case IB_CM_REQ_RECEIVED:
2524 ret = srpt_ib_cm_req_recv(cm_id, &event->param.req_rcvd,
2525 event->private_data);
2527 case IB_CM_REJ_RECEIVED:
2528 srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
2529 event->private_data,
2530 IB_CM_REJ_PRIVATE_DATA_SIZE);
2532 case IB_CM_RTU_RECEIVED:
2533 case IB_CM_USER_ESTABLISHED:
2534 srpt_cm_rtu_recv(ch);
2536 case IB_CM_DREQ_RECEIVED:
2537 srpt_disconnect_ch(ch);
2539 case IB_CM_DREP_RECEIVED:
2540 pr_info("Received CM DREP message for ch %s-%d.\n",
2541 ch->sess_name, ch->qp->qp_num);
2544 case IB_CM_TIMEWAIT_EXIT:
2545 pr_info("Received CM TimeWait exit for ch %s-%d.\n",
2546 ch->sess_name, ch->qp->qp_num);
2549 case IB_CM_REP_ERROR:
2550 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2553 case IB_CM_DREQ_ERROR:
2554 pr_info("Received CM DREQ ERROR event.\n");
2556 case IB_CM_MRA_RECEIVED:
2557 pr_info("Received CM MRA event\n");
2560 pr_err("received unrecognized CM event %d\n", event->event);
2567 static int srpt_rdma_cm_handler(struct rdma_cm_id *cm_id,
2568 struct rdma_cm_event *event)
2570 struct srpt_rdma_ch *ch = cm_id->context;
2573 switch (event->event) {
2574 case RDMA_CM_EVENT_CONNECT_REQUEST:
2575 ret = srpt_rdma_cm_req_recv(cm_id, event);
2577 case RDMA_CM_EVENT_REJECTED:
2578 srpt_cm_rej_recv(ch, event->status,
2579 event->param.conn.private_data,
2580 event->param.conn.private_data_len);
2582 case RDMA_CM_EVENT_ESTABLISHED:
2583 srpt_cm_rtu_recv(ch);
2585 case RDMA_CM_EVENT_DISCONNECTED:
2586 if (ch->state < CH_DISCONNECTING)
2587 srpt_disconnect_ch(ch);
2591 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2594 case RDMA_CM_EVENT_UNREACHABLE:
2595 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2598 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2599 case RDMA_CM_EVENT_ADDR_CHANGE:
2602 pr_err("received unrecognized RDMA CM event %d\n",
2610 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2612 struct srpt_send_ioctx *ioctx;
2614 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2615 return ioctx->state == SRPT_STATE_NEED_DATA;
2619 * srpt_write_pending - Start data transfer from initiator to target (write).
2621 static int srpt_write_pending(struct se_cmd *se_cmd)
2623 struct srpt_send_ioctx *ioctx =
2624 container_of(se_cmd, struct srpt_send_ioctx, cmd);
2625 struct srpt_rdma_ch *ch = ioctx->ch;
2626 struct ib_send_wr *first_wr = NULL;
2627 struct ib_cqe *cqe = &ioctx->rdma_cqe;
2628 enum srpt_command_state new_state;
2631 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2632 WARN_ON(new_state == SRPT_STATE_DONE);
2634 if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
2635 pr_warn("%s: IB send queue full (needed %d)\n",
2636 __func__, ioctx->n_rdma);
2641 cqe->done = srpt_rdma_read_done;
2642 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2643 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2645 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
2650 ret = ib_post_send(ch->qp, first_wr, NULL);
2652 pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
2653 __func__, ret, ioctx->n_rdma,
2654 atomic_read(&ch->sq_wr_avail));
2660 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
2664 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2666 switch (tcm_mgmt_status) {
2667 case TMR_FUNCTION_COMPLETE:
2668 return SRP_TSK_MGMT_SUCCESS;
2669 case TMR_FUNCTION_REJECTED:
2670 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2672 return SRP_TSK_MGMT_FAILED;
2676 * srpt_queue_response - transmit the response to a SCSI command
2677 * @cmd: SCSI target command.
2679 * Callback function called by the TCM core. Must not block since it can be
2680 * invoked on the context of the IB completion handler.
2682 static void srpt_queue_response(struct se_cmd *cmd)
2684 struct srpt_send_ioctx *ioctx =
2685 container_of(cmd, struct srpt_send_ioctx, cmd);
2686 struct srpt_rdma_ch *ch = ioctx->ch;
2687 struct srpt_device *sdev = ch->sport->sdev;
2688 struct ib_send_wr send_wr, *first_wr = &send_wr;
2690 enum srpt_command_state state;
2691 int resp_len, ret, i;
2696 state = ioctx->state;
2698 case SRPT_STATE_NEW:
2699 case SRPT_STATE_DATA_IN:
2700 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2702 case SRPT_STATE_MGMT:
2703 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2706 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2707 ch, ioctx->ioctx.index, ioctx->state);
2711 if (unlikely(WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)))
2714 /* For read commands, transfer the data to the initiator. */
2715 if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
2716 ioctx->cmd.data_length &&
2717 !ioctx->queue_status_only) {
2718 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2719 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2721 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
2722 ch->sport->port, NULL, first_wr);
2726 if (state != SRPT_STATE_MGMT)
2727 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
2731 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2732 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
2736 atomic_inc(&ch->req_lim);
2738 if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
2739 &ch->sq_wr_avail) < 0)) {
2740 pr_warn("%s: IB send queue full (needed %d)\n",
2741 __func__, ioctx->n_rdma);
2746 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
2749 sge.addr = ioctx->ioctx.dma;
2750 sge.length = resp_len;
2751 sge.lkey = sdev->lkey;
2753 ioctx->ioctx.cqe.done = srpt_send_done;
2754 send_wr.next = NULL;
2755 send_wr.wr_cqe = &ioctx->ioctx.cqe;
2756 send_wr.sg_list = &sge;
2757 send_wr.num_sge = 1;
2758 send_wr.opcode = IB_WR_SEND;
2759 send_wr.send_flags = IB_SEND_SIGNALED;
2761 ret = ib_post_send(ch->qp, first_wr, NULL);
2763 pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
2764 __func__, ioctx->cmd.tag, ret);
2771 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
2772 atomic_dec(&ch->req_lim);
2773 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2774 target_put_sess_cmd(&ioctx->cmd);
2777 static int srpt_queue_data_in(struct se_cmd *cmd)
2779 srpt_queue_response(cmd);
2783 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2785 srpt_queue_response(cmd);
2788 static void srpt_aborted_task(struct se_cmd *cmd)
2792 static int srpt_queue_status(struct se_cmd *cmd)
2794 struct srpt_send_ioctx *ioctx;
2796 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2797 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2798 if (cmd->se_cmd_flags &
2799 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2800 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2801 ioctx->queue_status_only = true;
2802 srpt_queue_response(cmd);
2806 static void srpt_refresh_port_work(struct work_struct *work)
2808 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2810 srpt_refresh_port(sport);
2813 static bool srpt_ch_list_empty(struct srpt_port *sport)
2815 struct srpt_nexus *nexus;
2819 list_for_each_entry(nexus, &sport->nexus_list, entry)
2820 if (!list_empty(&nexus->ch_list))
2828 * srpt_release_sport - disable login and wait for associated channels
2829 * @sport: SRPT HCA port.
2831 static int srpt_release_sport(struct srpt_port *sport)
2833 struct srpt_nexus *nexus, *next_n;
2834 struct srpt_rdma_ch *ch;
2836 WARN_ON_ONCE(irqs_disabled());
2838 mutex_lock(&sport->mutex);
2839 srpt_set_enabled(sport, false);
2840 mutex_unlock(&sport->mutex);
2842 while (wait_event_timeout(sport->ch_releaseQ,
2843 srpt_ch_list_empty(sport), 5 * HZ) <= 0) {
2844 pr_info("%s_%d: waiting for session unregistration ...\n",
2845 sport->sdev->device->name, sport->port);
2847 list_for_each_entry(nexus, &sport->nexus_list, entry) {
2848 list_for_each_entry(ch, &nexus->ch_list, list) {
2849 pr_info("%s-%d: state %s\n",
2850 ch->sess_name, ch->qp->qp_num,
2851 get_ch_state_name(ch->state));
2857 mutex_lock(&sport->mutex);
2858 list_for_each_entry_safe(nexus, next_n, &sport->nexus_list, entry) {
2859 list_del(&nexus->entry);
2860 kfree_rcu(nexus, rcu);
2862 mutex_unlock(&sport->mutex);
2867 static struct se_wwn *__srpt_lookup_wwn(const char *name)
2869 struct ib_device *dev;
2870 struct srpt_device *sdev;
2871 struct srpt_port *sport;
2874 list_for_each_entry(sdev, &srpt_dev_list, list) {
2879 for (i = 0; i < dev->phys_port_cnt; i++) {
2880 sport = &sdev->port[i];
2882 if (strcmp(sport->port_guid, name) == 0)
2883 return &sport->port_guid_wwn;
2884 if (strcmp(sport->port_gid, name) == 0)
2885 return &sport->port_gid_wwn;
2892 static struct se_wwn *srpt_lookup_wwn(const char *name)
2896 spin_lock(&srpt_dev_lock);
2897 wwn = __srpt_lookup_wwn(name);
2898 spin_unlock(&srpt_dev_lock);
2903 static void srpt_free_srq(struct srpt_device *sdev)
2908 ib_destroy_srq(sdev->srq);
2909 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2910 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2914 static int srpt_alloc_srq(struct srpt_device *sdev)
2916 struct ib_srq_init_attr srq_attr = {
2917 .event_handler = srpt_srq_event,
2918 .srq_context = (void *)sdev,
2919 .attr.max_wr = sdev->srq_size,
2921 .srq_type = IB_SRQT_BASIC,
2923 struct ib_device *device = sdev->device;
2927 WARN_ON_ONCE(sdev->srq);
2928 srq = ib_create_srq(sdev->pd, &srq_attr);
2930 pr_debug("ib_create_srq() failed: %ld\n", PTR_ERR(srq));
2931 return PTR_ERR(srq);
2934 pr_debug("create SRQ #wr= %d max_allow=%d dev= %s\n", sdev->srq_size,
2935 sdev->device->attrs.max_srq_wr, device->name);
2937 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2938 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2939 sizeof(*sdev->ioctx_ring[0]),
2940 srp_max_req_size, DMA_FROM_DEVICE);
2941 if (!sdev->ioctx_ring) {
2942 ib_destroy_srq(srq);
2946 sdev->use_srq = true;
2949 for (i = 0; i < sdev->srq_size; ++i) {
2950 INIT_LIST_HEAD(&sdev->ioctx_ring[i]->wait_list);
2951 srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]);
2957 static int srpt_use_srq(struct srpt_device *sdev, bool use_srq)
2959 struct ib_device *device = sdev->device;
2963 srpt_free_srq(sdev);
2964 sdev->use_srq = false;
2965 } else if (use_srq && !sdev->srq) {
2966 ret = srpt_alloc_srq(sdev);
2968 pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__, device->name,
2969 sdev->use_srq, ret);
2974 * srpt_add_one - InfiniBand device addition callback function
2975 * @device: Describes a HCA.
2977 static void srpt_add_one(struct ib_device *device)
2979 struct srpt_device *sdev;
2980 struct srpt_port *sport;
2983 pr_debug("device = %p\n", device);
2985 sdev = kzalloc(struct_size(sdev, port, device->phys_port_cnt),
2990 sdev->device = device;
2991 mutex_init(&sdev->sdev_mutex);
2993 sdev->pd = ib_alloc_pd(device, 0);
2994 if (IS_ERR(sdev->pd))
2997 sdev->lkey = sdev->pd->local_dma_lkey;
2999 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
3001 srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq);
3003 if (!srpt_service_guid)
3004 srpt_service_guid = be64_to_cpu(device->node_guid);
3006 if (rdma_port_get_link_layer(device, 1) == IB_LINK_LAYER_INFINIBAND)
3007 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3008 if (IS_ERR(sdev->cm_id)) {
3009 pr_info("ib_create_cm_id() failed: %ld\n",
3010 PTR_ERR(sdev->cm_id));
3016 /* print out target login information */
3017 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3018 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3019 srpt_service_guid, srpt_service_guid);
3022 * We do not have a consistent service_id (ie. also id_ext of target_id)
3023 * to identify this target. We currently use the guid of the first HCA
3024 * in the system as service_id; therefore, the target_id will change
3025 * if this HCA is gone bad and replaced by different HCA
3028 ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0) :
3031 pr_err("ib_cm_listen() failed: %d (cm_id state = %d)\n", ret,
3032 sdev->cm_id->state);
3036 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3037 srpt_event_handler);
3038 ib_register_event_handler(&sdev->event_handler);
3040 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3041 sport = &sdev->port[i - 1];
3042 INIT_LIST_HEAD(&sport->nexus_list);
3043 init_waitqueue_head(&sport->ch_releaseQ);
3044 mutex_init(&sport->mutex);
3047 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3048 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3049 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3050 sport->port_attrib.use_srq = false;
3051 INIT_WORK(&sport->work, srpt_refresh_port_work);
3053 if (srpt_refresh_port(sport)) {
3054 pr_err("MAD registration failed for %s-%d.\n",
3055 sdev->device->name, i);
3060 spin_lock(&srpt_dev_lock);
3061 list_add_tail(&sdev->list, &srpt_dev_list);
3062 spin_unlock(&srpt_dev_lock);
3065 ib_set_client_data(device, &srpt_client, sdev);
3066 pr_debug("added %s.\n", device->name);
3070 ib_unregister_event_handler(&sdev->event_handler);
3073 ib_destroy_cm_id(sdev->cm_id);
3075 srpt_free_srq(sdev);
3076 ib_dealloc_pd(sdev->pd);
3081 pr_info("%s(%s) failed.\n", __func__, device->name);
3086 * srpt_remove_one - InfiniBand device removal callback function
3087 * @device: Describes a HCA.
3088 * @client_data: The value passed as the third argument to ib_set_client_data().
3090 static void srpt_remove_one(struct ib_device *device, void *client_data)
3092 struct srpt_device *sdev = client_data;
3096 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
3100 srpt_unregister_mad_agent(sdev);
3102 ib_unregister_event_handler(&sdev->event_handler);
3104 /* Cancel any work queued by the just unregistered IB event handler. */
3105 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3106 cancel_work_sync(&sdev->port[i].work);
3109 ib_destroy_cm_id(sdev->cm_id);
3111 ib_set_client_data(device, &srpt_client, NULL);
3114 * Unregistering a target must happen after destroying sdev->cm_id
3115 * such that no new SRP_LOGIN_REQ information units can arrive while
3116 * destroying the target.
3118 spin_lock(&srpt_dev_lock);
3119 list_del(&sdev->list);
3120 spin_unlock(&srpt_dev_lock);
3122 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3123 srpt_release_sport(&sdev->port[i]);
3125 srpt_free_srq(sdev);
3127 ib_dealloc_pd(sdev->pd);
3132 static struct ib_client srpt_client = {
3134 .add = srpt_add_one,
3135 .remove = srpt_remove_one
3138 static int srpt_check_true(struct se_portal_group *se_tpg)
3143 static int srpt_check_false(struct se_portal_group *se_tpg)
3148 static char *srpt_get_fabric_name(void)
3153 static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg)
3155 return tpg->se_tpg_wwn->priv;
3158 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3160 struct srpt_port *sport = srpt_tpg_to_sport(tpg);
3162 WARN_ON_ONCE(tpg != &sport->port_guid_tpg &&
3163 tpg != &sport->port_gid_tpg);
3164 return tpg == &sport->port_guid_tpg ? sport->port_guid :
3168 static u16 srpt_get_tag(struct se_portal_group *tpg)
3173 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3178 static void srpt_release_cmd(struct se_cmd *se_cmd)
3180 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3181 struct srpt_send_ioctx, cmd);
3182 struct srpt_rdma_ch *ch = ioctx->ch;
3183 unsigned long flags;
3185 WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
3186 !(ioctx->cmd.transport_state & CMD_T_ABORTED));
3188 if (ioctx->n_rw_ctx) {
3189 srpt_free_rw_ctxs(ch, ioctx);
3190 ioctx->n_rw_ctx = 0;
3193 spin_lock_irqsave(&ch->spinlock, flags);
3194 list_add(&ioctx->free_list, &ch->free_list);
3195 spin_unlock_irqrestore(&ch->spinlock, flags);
3199 * srpt_close_session - forcibly close a session
3200 * @se_sess: SCSI target session.
3202 * Callback function invoked by the TCM core to clean up sessions associated
3203 * with a node ACL when the user invokes
3204 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3206 static void srpt_close_session(struct se_session *se_sess)
3208 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
3210 srpt_disconnect_ch_sync(ch);
3214 * srpt_sess_get_index - return the value of scsiAttIntrPortIndex (SCSI-MIB)
3215 * @se_sess: SCSI target session.
3217 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3218 * This object represents an arbitrary integer used to uniquely identify a
3219 * particular attached remote initiator port to a particular SCSI target port
3220 * within a particular SCSI target device within a particular SCSI instance.
3222 static u32 srpt_sess_get_index(struct se_session *se_sess)
3227 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3231 /* Note: only used from inside debug printk's by the TCM core. */
3232 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3234 struct srpt_send_ioctx *ioctx;
3236 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3237 return ioctx->state;
3240 static int srpt_parse_guid(u64 *guid, const char *name)
3245 if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4)
3247 *guid = get_unaligned_be64(w);
3254 * srpt_parse_i_port_id - parse an initiator port ID
3255 * @name: ASCII representation of a 128-bit initiator port ID.
3256 * @i_port_id: Binary 128-bit port ID.
3258 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3261 unsigned len, count, leading_zero_bytes;
3265 if (strncasecmp(p, "0x", 2) == 0)
3271 count = min(len / 2, 16U);
3272 leading_zero_bytes = 16 - count;
3273 memset(i_port_id, 0, leading_zero_bytes);
3274 ret = hex2bin(i_port_id + leading_zero_bytes, p, count);
3281 * configfs callback function invoked for mkdir
3282 * /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3284 * i_port_id must be an initiator port GUID, GID or IP address. See also the
3285 * target_alloc_session() calls in this driver. Examples of valid initiator
3287 * 0x0000000000000000505400fffe4a0b7b
3288 * 0000000000000000505400fffe4a0b7b
3289 * 5054:00ff:fe4a:0b7b
3292 static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
3294 struct sockaddr_storage sa;
3299 ret = srpt_parse_guid(&guid, name);
3301 ret = srpt_parse_i_port_id(i_port_id, name);
3303 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, name, NULL,
3306 pr_err("invalid initiator port ID %s\n", name);
3310 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3313 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3314 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3316 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3319 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3320 const char *page, size_t count)
3322 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3323 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3327 ret = kstrtoul(page, 0, &val);
3329 pr_err("kstrtoul() failed with ret: %d\n", ret);
3332 if (val > MAX_SRPT_RDMA_SIZE) {
3333 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3334 MAX_SRPT_RDMA_SIZE);
3337 if (val < DEFAULT_MAX_RDMA_SIZE) {
3338 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3339 val, DEFAULT_MAX_RDMA_SIZE);
3342 sport->port_attrib.srp_max_rdma_size = val;
3347 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3350 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3351 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3353 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3356 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3357 const char *page, size_t count)
3359 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3360 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3364 ret = kstrtoul(page, 0, &val);
3366 pr_err("kstrtoul() failed with ret: %d\n", ret);
3369 if (val > MAX_SRPT_RSP_SIZE) {
3370 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3374 if (val < MIN_MAX_RSP_SIZE) {
3375 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3379 sport->port_attrib.srp_max_rsp_size = val;
3384 static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3387 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3388 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3390 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3393 static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3394 const char *page, size_t count)
3396 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3397 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3401 ret = kstrtoul(page, 0, &val);
3403 pr_err("kstrtoul() failed with ret: %d\n", ret);
3406 if (val > MAX_SRPT_SRQ_SIZE) {
3407 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3411 if (val < MIN_SRPT_SRQ_SIZE) {
3412 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3416 sport->port_attrib.srp_sq_size = val;
3421 static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item,
3424 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3425 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3427 return sprintf(page, "%d\n", sport->port_attrib.use_srq);
3430 static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item,
3431 const char *page, size_t count)
3433 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3434 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3435 struct srpt_device *sdev = sport->sdev;
3440 ret = kstrtoul(page, 0, &val);
3446 ret = mutex_lock_interruptible(&sdev->sdev_mutex);
3449 ret = mutex_lock_interruptible(&sport->mutex);
3452 enabled = sport->enabled;
3453 /* Log out all initiator systems before changing 'use_srq'. */
3454 srpt_set_enabled(sport, false);
3455 sport->port_attrib.use_srq = val;
3456 srpt_use_srq(sdev, sport->port_attrib.use_srq);
3457 srpt_set_enabled(sport, enabled);
3459 mutex_unlock(&sport->mutex);
3461 mutex_unlock(&sdev->sdev_mutex);
3466 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3467 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3468 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
3469 CONFIGFS_ATTR(srpt_tpg_attrib_, use_srq);
3471 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3472 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3473 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3474 &srpt_tpg_attrib_attr_srp_sq_size,
3475 &srpt_tpg_attrib_attr_use_srq,
3479 static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr)
3481 struct rdma_cm_id *rdma_cm_id;
3484 rdma_cm_id = rdma_create_id(&init_net, srpt_rdma_cm_handler,
3485 NULL, RDMA_PS_TCP, IB_QPT_RC);
3486 if (IS_ERR(rdma_cm_id)) {
3487 pr_err("RDMA/CM ID creation failed: %ld\n",
3488 PTR_ERR(rdma_cm_id));
3492 ret = rdma_bind_addr(rdma_cm_id, listen_addr);
3496 snprintf(addr_str, sizeof(addr_str), "%pISp", listen_addr);
3497 pr_err("Binding RDMA/CM ID to address %s failed: %d\n",
3499 rdma_destroy_id(rdma_cm_id);
3500 rdma_cm_id = ERR_PTR(ret);
3504 ret = rdma_listen(rdma_cm_id, 128);
3506 pr_err("rdma_listen() failed: %d\n", ret);
3507 rdma_destroy_id(rdma_cm_id);
3508 rdma_cm_id = ERR_PTR(ret);
3515 static ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page)
3517 return sprintf(page, "%d\n", rdma_cm_port);
3520 static ssize_t srpt_rdma_cm_port_store(struct config_item *item,
3521 const char *page, size_t count)
3523 struct sockaddr_in addr4 = { .sin_family = AF_INET };
3524 struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6 };
3525 struct rdma_cm_id *new_id = NULL;
3529 ret = kstrtou16(page, 0, &val);
3533 if (rdma_cm_port == val)
3537 addr6.sin6_port = cpu_to_be16(val);
3538 new_id = srpt_create_rdma_id((struct sockaddr *)&addr6);
3539 if (IS_ERR(new_id)) {
3540 addr4.sin_port = cpu_to_be16(val);
3541 new_id = srpt_create_rdma_id((struct sockaddr *)&addr4);
3542 if (IS_ERR(new_id)) {
3543 ret = PTR_ERR(new_id);
3549 mutex_lock(&rdma_cm_mutex);
3551 swap(rdma_cm_id, new_id);
3552 mutex_unlock(&rdma_cm_mutex);
3555 rdma_destroy_id(new_id);
3561 CONFIGFS_ATTR(srpt_, rdma_cm_port);
3563 static struct configfs_attribute *srpt_da_attrs[] = {
3564 &srpt_attr_rdma_cm_port,
3568 static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
3570 struct se_portal_group *se_tpg = to_tpg(item);
3571 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3573 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3576 static ssize_t srpt_tpg_enable_store(struct config_item *item,
3577 const char *page, size_t count)
3579 struct se_portal_group *se_tpg = to_tpg(item);
3580 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3584 ret = kstrtoul(page, 0, &tmp);
3586 pr_err("Unable to extract srpt_tpg_store_enable\n");
3590 if ((tmp != 0) && (tmp != 1)) {
3591 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3595 mutex_lock(&sport->mutex);
3596 srpt_set_enabled(sport, tmp);
3597 mutex_unlock(&sport->mutex);
3602 CONFIGFS_ATTR(srpt_tpg_, enable);
3604 static struct configfs_attribute *srpt_tpg_attrs[] = {
3605 &srpt_tpg_attr_enable,
3610 * srpt_make_tpg - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port/$tpg
3611 * @wwn: Corresponds to $driver/$port.
3615 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3616 struct config_group *group,
3619 struct srpt_port *sport = wwn->priv;
3620 static struct se_portal_group *tpg;
3623 WARN_ON_ONCE(wwn != &sport->port_guid_wwn &&
3624 wwn != &sport->port_gid_wwn);
3625 tpg = wwn == &sport->port_guid_wwn ? &sport->port_guid_tpg :
3626 &sport->port_gid_tpg;
3627 res = core_tpg_register(wwn, tpg, SCSI_PROTOCOL_SRP);
3629 return ERR_PTR(res);
3635 * srpt_drop_tpg - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port/$tpg
3636 * @tpg: Target portal group to deregister.
3638 static void srpt_drop_tpg(struct se_portal_group *tpg)
3640 struct srpt_port *sport = srpt_tpg_to_sport(tpg);
3642 sport->enabled = false;
3643 core_tpg_deregister(tpg);
3647 * srpt_make_tport - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port
3652 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3653 struct config_group *group,
3656 return srpt_lookup_wwn(name) ? : ERR_PTR(-EINVAL);
3660 * srpt_drop_tport - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port
3663 static void srpt_drop_tport(struct se_wwn *wwn)
3667 static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
3669 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3672 CONFIGFS_ATTR_RO(srpt_wwn_, version);
3674 static struct configfs_attribute *srpt_wwn_attrs[] = {
3675 &srpt_wwn_attr_version,
3679 static const struct target_core_fabric_ops srpt_template = {
3680 .module = THIS_MODULE,
3682 .get_fabric_name = srpt_get_fabric_name,
3683 .tpg_get_wwn = srpt_get_fabric_wwn,
3684 .tpg_get_tag = srpt_get_tag,
3685 .tpg_check_demo_mode = srpt_check_false,
3686 .tpg_check_demo_mode_cache = srpt_check_true,
3687 .tpg_check_demo_mode_write_protect = srpt_check_true,
3688 .tpg_check_prod_mode_write_protect = srpt_check_false,
3689 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3690 .release_cmd = srpt_release_cmd,
3691 .check_stop_free = srpt_check_stop_free,
3692 .close_session = srpt_close_session,
3693 .sess_get_index = srpt_sess_get_index,
3694 .sess_get_initiator_sid = NULL,
3695 .write_pending = srpt_write_pending,
3696 .write_pending_status = srpt_write_pending_status,
3697 .set_default_node_attributes = srpt_set_default_node_attrs,
3698 .get_cmd_state = srpt_get_tcm_cmd_state,
3699 .queue_data_in = srpt_queue_data_in,
3700 .queue_status = srpt_queue_status,
3701 .queue_tm_rsp = srpt_queue_tm_rsp,
3702 .aborted_task = srpt_aborted_task,
3704 * Setup function pointers for generic logic in
3705 * target_core_fabric_configfs.c
3707 .fabric_make_wwn = srpt_make_tport,
3708 .fabric_drop_wwn = srpt_drop_tport,
3709 .fabric_make_tpg = srpt_make_tpg,
3710 .fabric_drop_tpg = srpt_drop_tpg,
3711 .fabric_init_nodeacl = srpt_init_nodeacl,
3713 .tfc_discovery_attrs = srpt_da_attrs,
3714 .tfc_wwn_attrs = srpt_wwn_attrs,
3715 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3716 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
3720 * srpt_init_module - kernel module initialization
3722 * Note: Since ib_register_client() registers callback functions, and since at
3723 * least one of these callback functions (srpt_add_one()) calls target core
3724 * functions, this driver must be registered with the target core before
3725 * ib_register_client() is called.
3727 static int __init srpt_init_module(void)
3732 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3733 pr_err("invalid value %d for kernel module parameter"
3734 " srp_max_req_size -- must be at least %d.\n",
3735 srp_max_req_size, MIN_MAX_REQ_SIZE);
3739 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3740 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3741 pr_err("invalid value %d for kernel module parameter"
3742 " srpt_srq_size -- must be in the range [%d..%d].\n",
3743 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3747 ret = target_register_template(&srpt_template);
3751 ret = ib_register_client(&srpt_client);
3753 pr_err("couldn't register IB client\n");
3754 goto out_unregister_target;
3759 out_unregister_target:
3760 target_unregister_template(&srpt_template);
3765 static void __exit srpt_cleanup_module(void)
3768 rdma_destroy_id(rdma_cm_id);
3769 ib_unregister_client(&srpt_client);
3770 target_unregister_template(&srpt_template);
3773 module_init(srpt_init_module);
3774 module_exit(srpt_cleanup_module);