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 <scsi/scsi_proto.h>
45 #include <scsi/scsi_tcq.h>
46 #include <target/target_core_base.h>
47 #include <target/target_core_fabric.h>
50 /* Name of this kernel module. */
51 #define DRV_NAME "ib_srpt"
52 #define DRV_VERSION "2.0.0"
53 #define DRV_RELDATE "2011-02-14"
55 #define SRPT_ID_STRING "Linux SRP target"
58 #define pr_fmt(fmt) DRV_NAME " " fmt
60 MODULE_AUTHOR("Vu Pham and Bart Van Assche");
61 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63 MODULE_LICENSE("Dual BSD/GPL");
69 static u64 srpt_service_guid;
70 static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
71 static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
73 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
74 module_param(srp_max_req_size, int, 0444);
75 MODULE_PARM_DESC(srp_max_req_size,
76 "Maximum size of SRP request messages in bytes.");
78 static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
79 module_param(srpt_srq_size, int, 0444);
80 MODULE_PARM_DESC(srpt_srq_size,
81 "Shared receive queue (SRQ) size.");
83 static int srpt_get_u64_x(char *buffer, const struct kernel_param *kp)
85 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
87 module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
89 MODULE_PARM_DESC(srpt_service_guid,
90 "Using this value for ioc_guid, id_ext, and cm_listen_id"
91 " instead of using the node_guid of the first HCA.");
93 static struct ib_client srpt_client;
94 static void srpt_release_cmd(struct se_cmd *se_cmd);
95 static void srpt_free_ch(struct kref *kref);
96 static int srpt_queue_status(struct se_cmd *cmd);
97 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
98 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
99 static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
102 * The only allowed channel state changes are those that change the channel
103 * state into a state with a higher numerical value. Hence the new > prev test.
105 static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
108 enum rdma_ch_state prev;
109 bool changed = false;
111 spin_lock_irqsave(&ch->spinlock, flags);
117 spin_unlock_irqrestore(&ch->spinlock, flags);
123 * srpt_event_handler() - Asynchronous IB event callback function.
125 * Callback function called by the InfiniBand core when an asynchronous IB
126 * event occurs. This callback may occur in interrupt context. See also
127 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
128 * Architecture Specification.
130 static void srpt_event_handler(struct ib_event_handler *handler,
131 struct ib_event *event)
133 struct srpt_device *sdev;
134 struct srpt_port *sport;
136 sdev = ib_get_client_data(event->device, &srpt_client);
137 if (!sdev || sdev->device != event->device)
140 pr_debug("ASYNC event= %d on device= %s\n", event->event,
143 switch (event->event) {
144 case IB_EVENT_PORT_ERR:
145 if (event->element.port_num <= sdev->device->phys_port_cnt) {
146 sport = &sdev->port[event->element.port_num - 1];
151 case IB_EVENT_PORT_ACTIVE:
152 case IB_EVENT_LID_CHANGE:
153 case IB_EVENT_PKEY_CHANGE:
154 case IB_EVENT_SM_CHANGE:
155 case IB_EVENT_CLIENT_REREGISTER:
156 case IB_EVENT_GID_CHANGE:
157 /* Refresh port data asynchronously. */
158 if (event->element.port_num <= sdev->device->phys_port_cnt) {
159 sport = &sdev->port[event->element.port_num - 1];
160 if (!sport->lid && !sport->sm_lid)
161 schedule_work(&sport->work);
165 pr_err("received unrecognized IB event %d\n",
172 * srpt_srq_event() - SRQ event callback function.
174 static void srpt_srq_event(struct ib_event *event, void *ctx)
176 pr_info("SRQ event %d\n", event->event);
179 static const char *get_ch_state_name(enum rdma_ch_state s)
186 case CH_DISCONNECTING:
187 return "disconnecting";
190 case CH_DISCONNECTED:
191 return "disconnected";
197 * srpt_qp_event() - QP event callback function.
199 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
201 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
202 event->event, ch->cm_id, ch->sess_name, ch->state);
204 switch (event->event) {
205 case IB_EVENT_COMM_EST:
206 ib_cm_notify(ch->cm_id, event->event);
208 case IB_EVENT_QP_LAST_WQE_REACHED:
209 pr_debug("%s-%d, state %s: received Last WQE event.\n",
210 ch->sess_name, ch->qp->qp_num,
211 get_ch_state_name(ch->state));
214 pr_err("received unrecognized IB QP event %d\n", event->event);
220 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
222 * @slot: one-based slot number.
223 * @value: four-bit value.
225 * Copies the lowest four bits of value in element slot of the array of four
226 * bit elements called c_list (controller list). The index slot is one-based.
228 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
235 tmp = c_list[id] & 0xf;
236 c_list[id] = (value << 4) | tmp;
238 tmp = c_list[id] & 0xf0;
239 c_list[id] = (value & 0xf) | tmp;
244 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
246 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
249 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
251 struct ib_class_port_info *cif;
253 cif = (struct ib_class_port_info *)mad->data;
254 memset(cif, 0, sizeof(*cif));
255 cif->base_version = 1;
256 cif->class_version = 1;
258 ib_set_cpi_resp_time(cif, 20);
259 mad->mad_hdr.status = 0;
263 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
265 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
266 * Specification. See also section B.7, table B.6 in the SRP r16a document.
268 static void srpt_get_iou(struct ib_dm_mad *mad)
270 struct ib_dm_iou_info *ioui;
274 ioui = (struct ib_dm_iou_info *)mad->data;
275 ioui->change_id = cpu_to_be16(1);
276 ioui->max_controllers = 16;
278 /* set present for slot 1 and empty for the rest */
279 srpt_set_ioc(ioui->controller_list, 1, 1);
280 for (i = 1, slot = 2; i < 16; i++, slot++)
281 srpt_set_ioc(ioui->controller_list, slot, 0);
283 mad->mad_hdr.status = 0;
287 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
289 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
290 * Architecture Specification. See also section B.7, table B.7 in the SRP
293 static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
294 struct ib_dm_mad *mad)
296 struct srpt_device *sdev = sport->sdev;
297 struct ib_dm_ioc_profile *iocp;
298 int send_queue_depth;
300 iocp = (struct ib_dm_ioc_profile *)mad->data;
302 if (!slot || slot > 16) {
304 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
310 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
315 send_queue_depth = sdev->srq_size;
317 send_queue_depth = min(SRPT_RQ_SIZE,
318 sdev->device->attrs.max_qp_wr);
320 memset(iocp, 0, sizeof(*iocp));
321 strcpy(iocp->id_string, SRPT_ID_STRING);
322 iocp->guid = cpu_to_be64(srpt_service_guid);
323 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
324 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
325 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
326 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
327 iocp->subsys_device_id = 0x0;
328 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
329 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
330 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
331 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
332 iocp->send_queue_depth = cpu_to_be16(send_queue_depth);
333 iocp->rdma_read_depth = 4;
334 iocp->send_size = cpu_to_be32(srp_max_req_size);
335 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
337 iocp->num_svc_entries = 1;
338 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
339 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
341 mad->mad_hdr.status = 0;
345 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
347 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
348 * Specification. See also section B.7, table B.8 in the SRP r16a document.
350 static void srpt_get_svc_entries(u64 ioc_guid,
351 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
353 struct ib_dm_svc_entries *svc_entries;
357 if (!slot || slot > 16) {
359 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
363 if (slot > 2 || lo > hi || hi > 1) {
365 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
369 svc_entries = (struct ib_dm_svc_entries *)mad->data;
370 memset(svc_entries, 0, sizeof(*svc_entries));
371 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
372 snprintf(svc_entries->service_entries[0].name,
373 sizeof(svc_entries->service_entries[0].name),
375 SRP_SERVICE_NAME_PREFIX,
378 mad->mad_hdr.status = 0;
382 * srpt_mgmt_method_get() - Process a received management datagram.
383 * @sp: source port through which the MAD has been received.
384 * @rq_mad: received MAD.
385 * @rsp_mad: response MAD.
387 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
388 struct ib_dm_mad *rsp_mad)
394 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
396 case DM_ATTR_CLASS_PORT_INFO:
397 srpt_get_class_port_info(rsp_mad);
399 case DM_ATTR_IOU_INFO:
400 srpt_get_iou(rsp_mad);
402 case DM_ATTR_IOC_PROFILE:
403 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
404 srpt_get_ioc(sp, slot, rsp_mad);
406 case DM_ATTR_SVC_ENTRIES:
407 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
408 hi = (u8) ((slot >> 8) & 0xff);
409 lo = (u8) (slot & 0xff);
410 slot = (u16) ((slot >> 16) & 0xffff);
411 srpt_get_svc_entries(srpt_service_guid,
412 slot, hi, lo, rsp_mad);
415 rsp_mad->mad_hdr.status =
416 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
422 * srpt_mad_send_handler() - Post MAD-send callback function.
424 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
425 struct ib_mad_send_wc *mad_wc)
427 rdma_destroy_ah(mad_wc->send_buf->ah);
428 ib_free_send_mad(mad_wc->send_buf);
432 * srpt_mad_recv_handler() - MAD reception callback function.
434 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
435 struct ib_mad_send_buf *send_buf,
436 struct ib_mad_recv_wc *mad_wc)
438 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
440 struct ib_mad_send_buf *rsp;
441 struct ib_dm_mad *dm_mad;
443 if (!mad_wc || !mad_wc->recv_buf.mad)
446 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
447 mad_wc->recv_buf.grh, mad_agent->port_num);
451 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
453 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
454 mad_wc->wc->pkey_index, 0,
455 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
457 IB_MGMT_BASE_VERSION);
464 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
465 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
466 dm_mad->mad_hdr.status = 0;
468 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
469 case IB_MGMT_METHOD_GET:
470 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
472 case IB_MGMT_METHOD_SET:
473 dm_mad->mad_hdr.status =
474 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
477 dm_mad->mad_hdr.status =
478 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
482 if (!ib_post_send_mad(rsp, NULL)) {
483 ib_free_recv_mad(mad_wc);
484 /* will destroy_ah & free_send_mad in send completion */
488 ib_free_send_mad(rsp);
493 ib_free_recv_mad(mad_wc);
497 * srpt_refresh_port() - Configure a HCA port.
499 * Enable InfiniBand management datagram processing, update the cached sm_lid,
500 * lid and gid values, and register a callback function for processing MADs
501 * on the specified port.
503 * Note: It is safe to call this function more than once for the same port.
505 static int srpt_refresh_port(struct srpt_port *sport)
507 struct ib_mad_reg_req reg_req;
508 struct ib_port_modify port_modify;
509 struct ib_port_attr port_attr;
513 memset(&port_modify, 0, sizeof(port_modify));
514 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
515 port_modify.clr_port_cap_mask = 0;
517 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
521 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
525 sport->sm_lid = port_attr.sm_lid;
526 sport->lid = port_attr.lid;
528 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
533 sport->port_guid_wwn.priv = sport;
534 guid = (__be16 *)&sport->gid.global.interface_id;
535 snprintf(sport->port_guid, sizeof(sport->port_guid),
536 "%04x:%04x:%04x:%04x",
537 be16_to_cpu(guid[0]), be16_to_cpu(guid[1]),
538 be16_to_cpu(guid[2]), be16_to_cpu(guid[3]));
539 sport->port_gid_wwn.priv = sport;
540 snprintf(sport->port_gid, sizeof(sport->port_gid),
542 be64_to_cpu(sport->gid.global.subnet_prefix),
543 be64_to_cpu(sport->gid.global.interface_id));
545 if (!sport->mad_agent) {
546 memset(®_req, 0, sizeof(reg_req));
547 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
548 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
549 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
550 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
552 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
556 srpt_mad_send_handler,
557 srpt_mad_recv_handler,
559 if (IS_ERR(sport->mad_agent)) {
560 ret = PTR_ERR(sport->mad_agent);
561 sport->mad_agent = NULL;
570 port_modify.set_port_cap_mask = 0;
571 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
572 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
580 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
582 * Note: It is safe to call this function more than once for the same device.
584 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
586 struct ib_port_modify port_modify = {
587 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
589 struct srpt_port *sport;
592 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
593 sport = &sdev->port[i - 1];
594 WARN_ON(sport->port != i);
595 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
596 pr_err("disabling MAD processing failed.\n");
597 if (sport->mad_agent) {
598 ib_unregister_mad_agent(sport->mad_agent);
599 sport->mad_agent = NULL;
605 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
607 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
608 int ioctx_size, int dma_size,
609 enum dma_data_direction dir)
611 struct srpt_ioctx *ioctx;
613 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
617 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
621 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
622 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
636 * srpt_free_ioctx() - Free an SRPT I/O context structure.
638 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
639 int dma_size, enum dma_data_direction dir)
644 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
650 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
651 * @sdev: Device to allocate the I/O context ring for.
652 * @ring_size: Number of elements in the I/O context ring.
653 * @ioctx_size: I/O context size.
654 * @dma_size: DMA buffer size.
655 * @dir: DMA data direction.
657 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
658 int ring_size, int ioctx_size,
659 int dma_size, enum dma_data_direction dir)
661 struct srpt_ioctx **ring;
664 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
665 && ioctx_size != sizeof(struct srpt_send_ioctx));
667 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
670 for (i = 0; i < ring_size; ++i) {
671 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
680 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
688 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
690 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
691 struct srpt_device *sdev, int ring_size,
692 int dma_size, enum dma_data_direction dir)
699 for (i = 0; i < ring_size; ++i)
700 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
705 * srpt_get_cmd_state() - Get the state of a SCSI command.
707 static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
709 enum srpt_command_state state;
714 spin_lock_irqsave(&ioctx->spinlock, flags);
715 state = ioctx->state;
716 spin_unlock_irqrestore(&ioctx->spinlock, flags);
721 * srpt_set_cmd_state() - Set the state of a SCSI command.
723 * Does not modify the state of aborted commands. Returns the previous command
726 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
727 enum srpt_command_state new)
729 enum srpt_command_state previous;
734 spin_lock_irqsave(&ioctx->spinlock, flags);
735 previous = ioctx->state;
736 if (previous != SRPT_STATE_DONE)
738 spin_unlock_irqrestore(&ioctx->spinlock, flags);
744 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
746 * Returns true if and only if the previous command state was equal to 'old'.
748 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
749 enum srpt_command_state old,
750 enum srpt_command_state new)
752 enum srpt_command_state previous;
756 WARN_ON(old == SRPT_STATE_DONE);
757 WARN_ON(new == SRPT_STATE_NEW);
759 spin_lock_irqsave(&ioctx->spinlock, flags);
760 previous = ioctx->state;
763 spin_unlock_irqrestore(&ioctx->spinlock, flags);
764 return previous == old;
768 * srpt_post_recv() - Post an IB receive request.
770 static int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch,
771 struct srpt_recv_ioctx *ioctx)
774 struct ib_recv_wr wr, *bad_wr;
777 list.addr = ioctx->ioctx.dma;
778 list.length = srp_max_req_size;
779 list.lkey = sdev->lkey;
781 ioctx->ioctx.cqe.done = srpt_recv_done;
782 wr.wr_cqe = &ioctx->ioctx.cqe;
788 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
790 return ib_post_recv(ch->qp, &wr, &bad_wr);
794 * srpt_zerolength_write() - Perform a zero-length RDMA write.
796 * A quote from the InfiniBand specification: C9-88: For an HCA responder
797 * using Reliable Connection service, for each zero-length RDMA READ or WRITE
798 * request, the R_Key shall not be validated, even if the request includes
801 static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
803 struct ib_send_wr wr, *bad_wr;
805 memset(&wr, 0, sizeof(wr));
806 wr.opcode = IB_WR_RDMA_WRITE;
807 wr.wr_cqe = &ch->zw_cqe;
808 wr.send_flags = IB_SEND_SIGNALED;
809 return ib_post_send(ch->qp, &wr, &bad_wr);
812 static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
814 struct srpt_rdma_ch *ch = cq->cq_context;
816 if (wc->status == IB_WC_SUCCESS) {
817 srpt_process_wait_list(ch);
819 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
820 schedule_work(&ch->release_work);
822 WARN_ONCE(1, "%s-%d\n", ch->sess_name, ch->qp->qp_num);
826 static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
827 struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
830 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
831 struct srpt_rdma_ch *ch = ioctx->ch;
832 struct scatterlist *prev = NULL;
837 ioctx->rw_ctxs = &ioctx->s_rw_ctx;
839 ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
845 for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
846 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
847 u64 remote_addr = be64_to_cpu(db->va);
848 u32 size = be32_to_cpu(db->len);
849 u32 rkey = be32_to_cpu(db->key);
851 ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
856 ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
857 ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
859 target_free_sgl(ctx->sg, ctx->nents);
863 ioctx->n_rdma += ret;
867 sg_unmark_end(&prev[prev_nents - 1]);
868 sg_chain(prev, prev_nents + 1, ctx->sg);
874 prev_nents = ctx->nents;
876 *sg_cnt += ctx->nents;
883 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
885 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
886 ctx->sg, ctx->nents, dir);
887 target_free_sgl(ctx->sg, ctx->nents);
889 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
890 kfree(ioctx->rw_ctxs);
894 static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
895 struct srpt_send_ioctx *ioctx)
897 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
900 for (i = 0; i < ioctx->n_rw_ctx; i++) {
901 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
903 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
904 ctx->sg, ctx->nents, dir);
905 target_free_sgl(ctx->sg, ctx->nents);
908 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
909 kfree(ioctx->rw_ctxs);
912 static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
915 * The pointer computations below will only be compiled correctly
916 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
917 * whether srp_cmd::add_data has been declared as a byte pointer.
919 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
920 !__same_type(srp_cmd->add_data[0], (u8)0));
923 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
924 * CDB LENGTH' field are reserved and the size in bytes of this field
925 * is four times the value specified in bits 3..7. Hence the "& ~3".
927 return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
931 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
932 * @ioctx: Pointer to the I/O context associated with the request.
933 * @srp_cmd: Pointer to the SRP_CMD request data.
934 * @dir: Pointer to the variable to which the transfer direction will be
936 * @data_len: Pointer to the variable to which the total data length of all
937 * descriptors in the SRP_CMD request will be written.
939 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
941 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
942 * -ENOMEM when memory allocation fails and zero upon success.
944 static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
945 struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
946 struct scatterlist **sg, unsigned *sg_cnt, u64 *data_len)
952 * The lower four bits of the buffer format field contain the DATA-IN
953 * buffer descriptor format, and the highest four bits contain the
954 * DATA-OUT buffer descriptor format.
956 if (srp_cmd->buf_fmt & 0xf)
957 /* DATA-IN: transfer data from target to initiator (read). */
958 *dir = DMA_FROM_DEVICE;
959 else if (srp_cmd->buf_fmt >> 4)
960 /* DATA-OUT: transfer data from initiator to target (write). */
961 *dir = DMA_TO_DEVICE;
965 /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
966 ioctx->cmd.data_direction = *dir;
968 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
969 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
970 struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
972 *data_len = be32_to_cpu(db->len);
973 return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
974 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
975 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
976 struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
977 int nbufs = be32_to_cpu(idb->table_desc.len) /
978 sizeof(struct srp_direct_buf);
981 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
982 pr_err("received unsupported SRP_CMD request"
983 " type (%u out + %u in != %u / %zu)\n",
984 srp_cmd->data_out_desc_cnt,
985 srp_cmd->data_in_desc_cnt,
986 be32_to_cpu(idb->table_desc.len),
987 sizeof(struct srp_direct_buf));
991 *data_len = be32_to_cpu(idb->len);
992 return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
1001 * srpt_init_ch_qp() - Initialize queue pair attributes.
1003 * Initialized the attributes of queue pair 'qp' by allowing local write,
1004 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
1006 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1008 struct ib_qp_attr *attr;
1011 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1015 attr->qp_state = IB_QPS_INIT;
1016 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
1017 IB_ACCESS_REMOTE_WRITE;
1018 attr->port_num = ch->sport->port;
1019 attr->pkey_index = 0;
1021 ret = ib_modify_qp(qp, attr,
1022 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
1030 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
1031 * @ch: channel of the queue pair.
1032 * @qp: queue pair to change the state of.
1034 * Returns zero upon success and a negative value upon failure.
1036 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1037 * If this structure ever becomes larger, it might be necessary to allocate
1038 * it dynamically instead of on the stack.
1040 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1042 struct ib_qp_attr qp_attr;
1046 qp_attr.qp_state = IB_QPS_RTR;
1047 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1051 qp_attr.max_dest_rd_atomic = 4;
1053 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1060 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1061 * @ch: channel of the queue pair.
1062 * @qp: queue pair to change the state of.
1064 * Returns zero upon success and a negative value upon failure.
1066 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1067 * If this structure ever becomes larger, it might be necessary to allocate
1068 * it dynamically instead of on the stack.
1070 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1072 struct ib_qp_attr qp_attr;
1076 qp_attr.qp_state = IB_QPS_RTS;
1077 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1081 qp_attr.max_rd_atomic = 4;
1083 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1090 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1092 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1094 struct ib_qp_attr qp_attr;
1096 qp_attr.qp_state = IB_QPS_ERR;
1097 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1101 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1103 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1105 struct srpt_send_ioctx *ioctx;
1106 unsigned long flags;
1111 spin_lock_irqsave(&ch->spinlock, flags);
1112 if (!list_empty(&ch->free_list)) {
1113 ioctx = list_first_entry(&ch->free_list,
1114 struct srpt_send_ioctx, free_list);
1115 list_del(&ioctx->free_list);
1117 spin_unlock_irqrestore(&ch->spinlock, flags);
1122 BUG_ON(ioctx->ch != ch);
1123 spin_lock_init(&ioctx->spinlock);
1124 ioctx->state = SRPT_STATE_NEW;
1126 ioctx->n_rw_ctx = 0;
1127 init_completion(&ioctx->tx_done);
1128 ioctx->queue_status_only = false;
1130 * transport_init_se_cmd() does not initialize all fields, so do it
1133 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1134 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1140 * srpt_abort_cmd() - Abort a SCSI command.
1141 * @ioctx: I/O context associated with the SCSI command.
1142 * @context: Preferred execution context.
1144 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1146 enum srpt_command_state state;
1147 unsigned long flags;
1152 * If the command is in a state where the target core is waiting for
1153 * the ib_srpt driver, change the state to the next state.
1156 spin_lock_irqsave(&ioctx->spinlock, flags);
1157 state = ioctx->state;
1159 case SRPT_STATE_NEED_DATA:
1160 ioctx->state = SRPT_STATE_DATA_IN;
1162 case SRPT_STATE_CMD_RSP_SENT:
1163 case SRPT_STATE_MGMT_RSP_SENT:
1164 ioctx->state = SRPT_STATE_DONE;
1167 WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
1171 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1173 pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state,
1174 ioctx->state, ioctx->cmd.tag);
1177 case SRPT_STATE_NEW:
1178 case SRPT_STATE_DATA_IN:
1179 case SRPT_STATE_MGMT:
1180 case SRPT_STATE_DONE:
1182 * Do nothing - defer abort processing until
1183 * srpt_queue_response() is invoked.
1186 case SRPT_STATE_NEED_DATA:
1187 pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
1188 transport_generic_request_failure(&ioctx->cmd,
1189 TCM_CHECK_CONDITION_ABORT_CMD);
1191 case SRPT_STATE_CMD_RSP_SENT:
1193 * SRP_RSP sending failed or the SRP_RSP send completion has
1194 * not been received in time.
1196 transport_generic_free_cmd(&ioctx->cmd, 0);
1198 case SRPT_STATE_MGMT_RSP_SENT:
1199 transport_generic_free_cmd(&ioctx->cmd, 0);
1202 WARN(1, "Unexpected command state (%d)", state);
1210 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1211 * the data that has been transferred via IB RDMA had to be postponed until the
1212 * check_stop_free() callback. None of this is necessary anymore and needs to
1215 static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1217 struct srpt_rdma_ch *ch = cq->cq_context;
1218 struct srpt_send_ioctx *ioctx =
1219 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
1221 WARN_ON(ioctx->n_rdma <= 0);
1222 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1225 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1226 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1228 srpt_abort_cmd(ioctx);
1232 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1233 SRPT_STATE_DATA_IN))
1234 target_execute_cmd(&ioctx->cmd);
1236 pr_err("%s[%d]: wrong state = %d\n", __func__,
1237 __LINE__, srpt_get_cmd_state(ioctx));
1241 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1242 * @ch: RDMA channel through which the request has been received.
1243 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1244 * be built in the buffer ioctx->buf points at and hence this function will
1245 * overwrite the request data.
1246 * @tag: tag of the request for which this response is being generated.
1247 * @status: value for the STATUS field of the SRP_RSP information unit.
1249 * Returns the size in bytes of the SRP_RSP response.
1251 * An SRP_RSP response contains a SCSI status or service response. See also
1252 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1253 * response. See also SPC-2 for more information about sense data.
1255 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1256 struct srpt_send_ioctx *ioctx, u64 tag,
1259 struct srp_rsp *srp_rsp;
1260 const u8 *sense_data;
1261 int sense_data_len, max_sense_len;
1264 * The lowest bit of all SAM-3 status codes is zero (see also
1265 * paragraph 5.3 in SAM-3).
1267 WARN_ON(status & 1);
1269 srp_rsp = ioctx->ioctx.buf;
1272 sense_data = ioctx->sense_data;
1273 sense_data_len = ioctx->cmd.scsi_sense_length;
1274 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1276 memset(srp_rsp, 0, sizeof(*srp_rsp));
1277 srp_rsp->opcode = SRP_RSP;
1278 srp_rsp->req_lim_delta =
1279 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1281 srp_rsp->status = status;
1283 if (sense_data_len) {
1284 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1285 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1286 if (sense_data_len > max_sense_len) {
1287 pr_warn("truncated sense data from %d to %d"
1288 " bytes\n", sense_data_len, max_sense_len);
1289 sense_data_len = max_sense_len;
1292 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1293 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1294 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1297 return sizeof(*srp_rsp) + sense_data_len;
1301 * srpt_build_tskmgmt_rsp() - Build a task management response.
1302 * @ch: RDMA channel through which the request has been received.
1303 * @ioctx: I/O context in which the SRP_RSP response will be built.
1304 * @rsp_code: RSP_CODE that will be stored in the response.
1305 * @tag: Tag of the request for which this response is being generated.
1307 * Returns the size in bytes of the SRP_RSP response.
1309 * An SRP_RSP response contains a SCSI status or service response. See also
1310 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1313 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1314 struct srpt_send_ioctx *ioctx,
1315 u8 rsp_code, u64 tag)
1317 struct srp_rsp *srp_rsp;
1322 resp_len = sizeof(*srp_rsp) + resp_data_len;
1324 srp_rsp = ioctx->ioctx.buf;
1326 memset(srp_rsp, 0, sizeof(*srp_rsp));
1328 srp_rsp->opcode = SRP_RSP;
1329 srp_rsp->req_lim_delta =
1330 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1333 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1334 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1335 srp_rsp->data[3] = rsp_code;
1340 static int srpt_check_stop_free(struct se_cmd *cmd)
1342 struct srpt_send_ioctx *ioctx = container_of(cmd,
1343 struct srpt_send_ioctx, cmd);
1345 return target_put_sess_cmd(&ioctx->cmd);
1349 * srpt_handle_cmd() - Process SRP_CMD.
1351 static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1352 struct srpt_recv_ioctx *recv_ioctx,
1353 struct srpt_send_ioctx *send_ioctx)
1356 struct srp_cmd *srp_cmd;
1357 struct scatterlist *sg = NULL;
1358 unsigned sg_cnt = 0;
1360 enum dma_data_direction dir;
1363 BUG_ON(!send_ioctx);
1365 srp_cmd = recv_ioctx->ioctx.buf;
1366 cmd = &send_ioctx->cmd;
1367 cmd->tag = srp_cmd->tag;
1369 switch (srp_cmd->task_attr) {
1370 case SRP_CMD_SIMPLE_Q:
1371 cmd->sam_task_attr = TCM_SIMPLE_TAG;
1373 case SRP_CMD_ORDERED_Q:
1375 cmd->sam_task_attr = TCM_ORDERED_TAG;
1377 case SRP_CMD_HEAD_OF_Q:
1378 cmd->sam_task_attr = TCM_HEAD_TAG;
1381 cmd->sam_task_attr = TCM_ACA_TAG;
1385 rc = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &sg, &sg_cnt,
1388 if (rc != -EAGAIN) {
1389 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1395 rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
1396 &send_ioctx->sense_data[0],
1397 scsilun_to_int(&srp_cmd->lun), data_len,
1398 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
1399 sg, sg_cnt, NULL, 0, NULL, 0);
1401 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1408 send_ioctx->state = SRPT_STATE_DONE;
1409 srpt_release_cmd(cmd);
1412 static int srp_tmr_to_tcm(int fn)
1415 case SRP_TSK_ABORT_TASK:
1416 return TMR_ABORT_TASK;
1417 case SRP_TSK_ABORT_TASK_SET:
1418 return TMR_ABORT_TASK_SET;
1419 case SRP_TSK_CLEAR_TASK_SET:
1420 return TMR_CLEAR_TASK_SET;
1421 case SRP_TSK_LUN_RESET:
1422 return TMR_LUN_RESET;
1423 case SRP_TSK_CLEAR_ACA:
1424 return TMR_CLEAR_ACA;
1431 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1433 * Returns 0 if and only if the request will be processed by the target core.
1435 * For more information about SRP_TSK_MGMT information units, see also section
1436 * 6.7 in the SRP r16a document.
1438 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1439 struct srpt_recv_ioctx *recv_ioctx,
1440 struct srpt_send_ioctx *send_ioctx)
1442 struct srp_tsk_mgmt *srp_tsk;
1444 struct se_session *sess = ch->sess;
1448 BUG_ON(!send_ioctx);
1450 srp_tsk = recv_ioctx->ioctx.buf;
1451 cmd = &send_ioctx->cmd;
1453 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1454 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1455 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1457 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1458 send_ioctx->cmd.tag = srp_tsk->tag;
1459 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1460 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1461 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1462 GFP_KERNEL, srp_tsk->task_tag,
1463 TARGET_SCF_ACK_KREF);
1465 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1470 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
1474 * srpt_handle_new_iu() - Process a newly received information unit.
1475 * @ch: RDMA channel through which the information unit has been received.
1476 * @ioctx: SRPT I/O context associated with the information unit.
1478 static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1479 struct srpt_recv_ioctx *recv_ioctx,
1480 struct srpt_send_ioctx *send_ioctx)
1482 struct srp_cmd *srp_cmd;
1485 BUG_ON(!recv_ioctx);
1487 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1488 recv_ioctx->ioctx.dma, srp_max_req_size,
1491 if (unlikely(ch->state == CH_CONNECTING))
1494 if (unlikely(ch->state != CH_LIVE))
1497 srp_cmd = recv_ioctx->ioctx.buf;
1498 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1500 if (!list_empty(&ch->cmd_wait_list))
1502 send_ioctx = srpt_get_send_ioctx(ch);
1504 if (unlikely(!send_ioctx))
1508 switch (srp_cmd->opcode) {
1510 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1513 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1516 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
1519 pr_debug("received SRP_CRED_RSP\n");
1522 pr_debug("received SRP_AER_RSP\n");
1525 pr_err("Received SRP_RSP\n");
1528 pr_err("received IU with unknown opcode 0x%x\n",
1533 srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
1537 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1540 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1542 struct srpt_rdma_ch *ch = cq->cq_context;
1543 struct srpt_recv_ioctx *ioctx =
1544 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
1546 if (wc->status == IB_WC_SUCCESS) {
1549 req_lim = atomic_dec_return(&ch->req_lim);
1550 if (unlikely(req_lim < 0))
1551 pr_err("req_lim = %d < 0\n", req_lim);
1552 srpt_handle_new_iu(ch, ioctx, NULL);
1554 pr_info("receiving failed for ioctx %p with status %d\n",
1560 * This function must be called from the context in which RDMA completions are
1561 * processed because it accesses the wait list without protection against
1562 * access from other threads.
1564 static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
1566 struct srpt_send_ioctx *ioctx;
1568 while (!list_empty(&ch->cmd_wait_list) &&
1569 ch->state >= CH_LIVE &&
1570 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
1571 struct srpt_recv_ioctx *recv_ioctx;
1573 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1574 struct srpt_recv_ioctx,
1576 list_del(&recv_ioctx->wait_list);
1577 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
1582 * Note: Although this has not yet been observed during tests, at least in
1583 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1584 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1585 * value in each response is set to one, and it is possible that this response
1586 * makes the initiator send a new request before the send completion for that
1587 * response has been processed. This could e.g. happen if the call to
1588 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1589 * if IB retransmission causes generation of the send completion to be
1590 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1591 * are queued on cmd_wait_list. The code below processes these delayed
1592 * requests one at a time.
1594 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
1596 struct srpt_rdma_ch *ch = cq->cq_context;
1597 struct srpt_send_ioctx *ioctx =
1598 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1599 enum srpt_command_state state;
1601 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1603 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1604 state != SRPT_STATE_MGMT_RSP_SENT);
1606 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
1608 if (wc->status != IB_WC_SUCCESS)
1609 pr_info("sending response for ioctx 0x%p failed"
1610 " with status %d\n", ioctx, wc->status);
1612 if (state != SRPT_STATE_DONE) {
1613 transport_generic_free_cmd(&ioctx->cmd, 0);
1615 pr_err("IB completion has been received too late for"
1616 " wr_id = %u.\n", ioctx->ioctx.index);
1619 srpt_process_wait_list(ch);
1623 * srpt_create_ch_ib() - Create receive and send completion queues.
1625 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1627 struct ib_qp_init_attr *qp_init;
1628 struct srpt_port *sport = ch->sport;
1629 struct srpt_device *sdev = sport->sdev;
1630 const struct ib_device_attr *attrs = &sdev->device->attrs;
1631 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1634 WARN_ON(ch->rq_size < 1);
1637 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
1642 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1643 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
1644 if (IS_ERR(ch->cq)) {
1645 ret = PTR_ERR(ch->cq);
1646 pr_err("failed to create CQ cqe= %d ret= %d\n",
1647 ch->rq_size + srp_sq_size, ret);
1651 qp_init->qp_context = (void *)ch;
1652 qp_init->event_handler
1653 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1654 qp_init->send_cq = ch->cq;
1655 qp_init->recv_cq = ch->cq;
1656 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1657 qp_init->qp_type = IB_QPT_RC;
1659 * We divide up our send queue size into half SEND WRs to send the
1660 * completions, and half R/W contexts to actually do the RDMA
1661 * READ/WRITE transfers. Note that we need to allocate CQ slots for
1662 * both both, as RDMA contexts will also post completions for the
1665 qp_init->cap.max_send_wr = min(srp_sq_size / 2, attrs->max_qp_wr + 0U);
1666 qp_init->cap.max_rdma_ctxs = srp_sq_size / 2;
1667 qp_init->cap.max_send_sge = min(attrs->max_sge, SRPT_MAX_SG_PER_WQE);
1668 qp_init->port_num = ch->sport->port;
1669 if (sdev->use_srq) {
1670 qp_init->srq = sdev->srq;
1672 qp_init->cap.max_recv_wr = ch->rq_size;
1673 qp_init->cap.max_recv_sge = qp_init->cap.max_send_sge;
1676 ch->qp = ib_create_qp(sdev->pd, qp_init);
1677 if (IS_ERR(ch->qp)) {
1678 ret = PTR_ERR(ch->qp);
1679 if (ret == -ENOMEM) {
1681 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1682 ib_destroy_cq(ch->cq);
1686 pr_err("failed to create_qp ret= %d\n", ret);
1687 goto err_destroy_cq;
1690 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1692 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1693 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1694 qp_init->cap.max_send_wr, ch->cm_id);
1696 ret = srpt_init_ch_qp(ch, ch->qp);
1698 goto err_destroy_qp;
1701 for (i = 0; i < ch->rq_size; i++)
1702 srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]);
1709 ib_destroy_qp(ch->qp);
1715 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1717 ib_destroy_qp(ch->qp);
1722 * srpt_close_ch() - Close an RDMA channel.
1724 * Make sure all resources associated with the channel will be deallocated at
1725 * an appropriate time.
1727 * Returns true if and only if the channel state has been modified into
1730 static bool srpt_close_ch(struct srpt_rdma_ch *ch)
1734 if (!srpt_set_ch_state(ch, CH_DRAINING)) {
1735 pr_debug("%s-%d: already closed\n", ch->sess_name,
1740 kref_get(&ch->kref);
1742 ret = srpt_ch_qp_err(ch);
1744 pr_err("%s-%d: changing queue pair into error state failed: %d\n",
1745 ch->sess_name, ch->qp->qp_num, ret);
1747 pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
1749 ret = srpt_zerolength_write(ch);
1751 pr_err("%s-%d: queuing zero-length write failed: %d\n",
1752 ch->sess_name, ch->qp->qp_num, ret);
1753 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
1754 schedule_work(&ch->release_work);
1759 kref_put(&ch->kref, srpt_free_ch);
1765 * Change the channel state into CH_DISCONNECTING. If a channel has not yet
1766 * reached the connected state, close it. If a channel is in the connected
1767 * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
1768 * the responsibility of the caller to ensure that this function is not
1769 * invoked concurrently with the code that accepts a connection. This means
1770 * that this function must either be invoked from inside a CM callback
1771 * function or that it must be invoked with the srpt_port.mutex held.
1773 static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
1777 if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
1780 ret = ib_send_cm_dreq(ch->cm_id, NULL, 0);
1782 ret = ib_send_cm_drep(ch->cm_id, NULL, 0);
1784 if (ret < 0 && srpt_close_ch(ch))
1791 * Send DREQ and wait for DREP. Return true if and only if this function
1792 * changed the state of @ch.
1794 static bool srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch)
1795 __must_hold(&sdev->mutex)
1797 DECLARE_COMPLETION_ONSTACK(release_done);
1798 struct srpt_device *sdev = ch->sport->sdev;
1801 lockdep_assert_held(&sdev->mutex);
1803 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
1806 WARN_ON(ch->release_done);
1807 ch->release_done = &release_done;
1808 wait = !list_empty(&ch->list);
1809 srpt_disconnect_ch(ch);
1810 mutex_unlock(&sdev->mutex);
1815 while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
1816 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
1817 ch->sess_name, ch->qp->qp_num, ch->state);
1820 mutex_lock(&sdev->mutex);
1824 static void srpt_set_enabled(struct srpt_port *sport, bool enabled)
1825 __must_hold(&sdev->mutex)
1827 struct srpt_device *sdev = sport->sdev;
1828 struct srpt_rdma_ch *ch;
1830 lockdep_assert_held(&sdev->mutex);
1832 if (sport->enabled == enabled)
1834 sport->enabled = enabled;
1839 list_for_each_entry(ch, &sdev->rch_list, list) {
1840 if (ch->sport == sport) {
1841 pr_info("%s: closing channel %s-%d\n",
1842 sdev->device->name, ch->sess_name,
1844 if (srpt_disconnect_ch_sync(ch))
1851 static void srpt_free_ch(struct kref *kref)
1853 struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
1858 static void srpt_release_channel_work(struct work_struct *w)
1860 struct srpt_rdma_ch *ch;
1861 struct srpt_device *sdev;
1862 struct se_session *se_sess;
1864 ch = container_of(w, struct srpt_rdma_ch, release_work);
1865 pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name,
1866 ch->qp->qp_num, ch->release_done);
1868 sdev = ch->sport->sdev;
1874 target_sess_cmd_list_set_waiting(se_sess);
1875 target_wait_for_sess_cmds(se_sess);
1877 transport_deregister_session_configfs(se_sess);
1878 transport_deregister_session(se_sess);
1881 ib_destroy_cm_id(ch->cm_id);
1883 srpt_destroy_ch_ib(ch);
1885 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
1886 ch->sport->sdev, ch->rq_size,
1887 ch->rsp_size, DMA_TO_DEVICE);
1889 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
1891 srp_max_req_size, DMA_FROM_DEVICE);
1893 mutex_lock(&sdev->mutex);
1894 list_del_init(&ch->list);
1895 if (ch->release_done)
1896 complete(ch->release_done);
1897 mutex_unlock(&sdev->mutex);
1899 wake_up(&sdev->ch_releaseQ);
1901 kref_put(&ch->kref, srpt_free_ch);
1905 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
1907 * Ownership of the cm_id is transferred to the target session if this
1908 * functions returns zero. Otherwise the caller remains the owner of cm_id.
1910 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
1911 struct ib_cm_req_event_param *param,
1914 struct srpt_device *sdev = cm_id->context;
1915 struct srpt_port *sport = &sdev->port[param->port - 1];
1916 struct srp_login_req *req;
1917 struct srp_login_rsp *rsp;
1918 struct srp_login_rej *rej;
1919 struct ib_cm_rep_param *rep_param;
1920 struct srpt_rdma_ch *ch, *tmp_ch;
1925 WARN_ON_ONCE(irqs_disabled());
1927 if (WARN_ON(!sdev || !private_data))
1930 req = (struct srp_login_req *)private_data;
1932 it_iu_len = be32_to_cpu(req->req_it_iu_len);
1934 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
1935 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
1936 " (guid=0x%llx:0x%llx)\n",
1937 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
1938 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
1939 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
1940 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
1943 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
1944 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
1946 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
1947 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
1948 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
1950 if (!rsp || !rej || !rep_param) {
1955 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
1956 rej->reason = cpu_to_be32(
1957 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
1959 pr_err("rejected SRP_LOGIN_REQ because its"
1960 " length (%d bytes) is out of range (%d .. %d)\n",
1961 it_iu_len, 64, srp_max_req_size);
1965 if (!sport->enabled) {
1966 rej->reason = cpu_to_be32(
1967 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1969 pr_err("rejected SRP_LOGIN_REQ because the target port"
1970 " has not yet been enabled\n");
1974 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
1975 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
1977 mutex_lock(&sdev->mutex);
1979 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
1980 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
1981 && !memcmp(ch->t_port_id, req->target_port_id, 16)
1982 && param->port == ch->sport->port
1983 && param->listen_id == ch->sport->sdev->cm_id
1985 if (srpt_disconnect_ch(ch) < 0)
1987 pr_info("Relogin - closed existing channel %s\n",
1990 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
1994 mutex_unlock(&sdev->mutex);
1997 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
1999 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2000 || *(__be64 *)(req->target_port_id + 8) !=
2001 cpu_to_be64(srpt_service_guid)) {
2002 rej->reason = cpu_to_be32(
2003 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2005 pr_err("rejected SRP_LOGIN_REQ because it"
2006 " has an invalid target port identifier.\n");
2010 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
2012 rej->reason = cpu_to_be32(
2013 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2014 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
2019 kref_init(&ch->kref);
2020 ch->zw_cqe.done = srpt_zerolength_write_done;
2021 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2022 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2023 memcpy(ch->t_port_id, req->target_port_id, 16);
2024 ch->sport = &sdev->port[param->port - 1];
2026 cm_id->context = ch;
2028 * ch->rq_size should be at least as large as the initiator queue
2029 * depth to avoid that the initiator driver has to report QUEUE_FULL
2030 * to the SCSI mid-layer.
2032 ch->rq_size = min(SRPT_RQ_SIZE, sdev->device->attrs.max_qp_wr);
2033 spin_lock_init(&ch->spinlock);
2034 ch->state = CH_CONNECTING;
2035 INIT_LIST_HEAD(&ch->cmd_wait_list);
2036 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2038 ch->ioctx_ring = (struct srpt_send_ioctx **)
2039 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2040 sizeof(*ch->ioctx_ring[0]),
2041 ch->rsp_size, DMA_TO_DEVICE);
2042 if (!ch->ioctx_ring)
2045 INIT_LIST_HEAD(&ch->free_list);
2046 for (i = 0; i < ch->rq_size; i++) {
2047 ch->ioctx_ring[i]->ch = ch;
2048 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2050 if (!sdev->use_srq) {
2051 ch->ioctx_recv_ring = (struct srpt_recv_ioctx **)
2052 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2053 sizeof(*ch->ioctx_recv_ring[0]),
2056 if (!ch->ioctx_recv_ring) {
2057 pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n");
2059 cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2064 ret = srpt_create_ch_ib(ch);
2066 rej->reason = cpu_to_be32(
2067 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2068 pr_err("rejected SRP_LOGIN_REQ because creating"
2069 " a new RDMA channel failed.\n");
2070 goto free_recv_ring;
2073 ret = srpt_ch_qp_rtr(ch, ch->qp);
2075 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2076 pr_err("rejected SRP_LOGIN_REQ because enabling"
2077 " RTR failed (error code = %d)\n", ret);
2081 guid = (__be16 *)¶m->primary_path->sgid.global.interface_id;
2082 snprintf(ch->ini_guid, sizeof(ch->ini_guid), "%04x:%04x:%04x:%04x",
2083 be16_to_cpu(guid[0]), be16_to_cpu(guid[1]),
2084 be16_to_cpu(guid[2]), be16_to_cpu(guid[3]));
2085 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2086 be64_to_cpu(*(__be64 *)ch->i_port_id),
2087 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2089 pr_debug("registering session %s\n", ch->sess_name);
2091 if (sport->port_guid_tpg.se_tpg_wwn)
2092 ch->sess = target_alloc_session(&sport->port_guid_tpg, 0, 0,
2094 ch->ini_guid, ch, NULL);
2095 if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2096 ch->sess = target_alloc_session(&sport->port_gid_tpg, 0, 0,
2097 TARGET_PROT_NORMAL, ch->sess_name, ch,
2099 /* Retry without leading "0x" */
2100 if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2101 ch->sess = target_alloc_session(&sport->port_gid_tpg, 0, 0,
2103 ch->sess_name + 2, ch, NULL);
2104 if (IS_ERR_OR_NULL(ch->sess)) {
2105 pr_info("Rejected login because no ACL has been configured yet for initiator %s.\n",
2107 rej->reason = cpu_to_be32((PTR_ERR(ch->sess) == -ENOMEM) ?
2108 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
2109 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2113 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2114 ch->sess_name, ch->cm_id);
2116 /* create srp_login_response */
2117 rsp->opcode = SRP_LOGIN_RSP;
2118 rsp->tag = req->tag;
2119 rsp->max_it_iu_len = req->req_it_iu_len;
2120 rsp->max_ti_iu_len = req->req_it_iu_len;
2121 ch->max_ti_iu_len = it_iu_len;
2122 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2123 | SRP_BUF_FORMAT_INDIRECT);
2124 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2125 atomic_set(&ch->req_lim, ch->rq_size);
2126 atomic_set(&ch->req_lim_delta, 0);
2128 /* create cm reply */
2129 rep_param->qp_num = ch->qp->qp_num;
2130 rep_param->private_data = (void *)rsp;
2131 rep_param->private_data_len = sizeof(*rsp);
2132 rep_param->rnr_retry_count = 7;
2133 rep_param->flow_control = 1;
2134 rep_param->failover_accepted = 0;
2136 rep_param->responder_resources = 4;
2137 rep_param->initiator_depth = 4;
2139 ret = ib_send_cm_rep(cm_id, rep_param);
2141 pr_err("sending SRP_LOGIN_REQ response failed"
2142 " (error code = %d)\n", ret);
2143 goto release_channel;
2146 mutex_lock(&sdev->mutex);
2147 list_add_tail(&ch->list, &sdev->rch_list);
2148 mutex_unlock(&sdev->mutex);
2153 srpt_disconnect_ch(ch);
2154 transport_deregister_session_configfs(ch->sess);
2155 transport_deregister_session(ch->sess);
2159 srpt_destroy_ch_ib(ch);
2162 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2163 ch->sport->sdev, ch->rq_size,
2164 srp_max_req_size, DMA_FROM_DEVICE);
2167 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2168 ch->sport->sdev, ch->rq_size,
2169 ch->rsp_size, DMA_TO_DEVICE);
2174 rej->opcode = SRP_LOGIN_REJ;
2175 rej->tag = req->tag;
2176 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2177 | SRP_BUF_FORMAT_INDIRECT);
2179 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2180 (void *)rej, sizeof(*rej));
2190 static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
2191 enum ib_cm_rej_reason reason,
2192 const u8 *private_data,
2193 u8 private_data_len)
2198 if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
2200 for (i = 0; i < private_data_len; i++)
2201 sprintf(priv + 3 * i, " %02x", private_data[i]);
2203 pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
2204 ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
2205 "; private data" : "", priv ? priv : " (?)");
2210 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2212 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2213 * and that the recipient may begin transmitting (RTU = ready to use).
2215 static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
2219 if (srpt_set_ch_state(ch, CH_LIVE)) {
2220 ret = srpt_ch_qp_rts(ch, ch->qp);
2223 /* Trigger wait list processing. */
2224 ret = srpt_zerolength_write(ch);
2225 WARN_ONCE(ret < 0, "%d\n", ret);
2233 * srpt_cm_handler() - IB connection manager callback function.
2235 * A non-zero return value will cause the caller destroy the CM ID.
2237 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2238 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2239 * a non-zero value in any other case will trigger a race with the
2240 * ib_destroy_cm_id() call in srpt_release_channel().
2242 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2244 struct srpt_rdma_ch *ch = cm_id->context;
2248 switch (event->event) {
2249 case IB_CM_REQ_RECEIVED:
2250 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2251 event->private_data);
2253 case IB_CM_REJ_RECEIVED:
2254 srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
2255 event->private_data,
2256 IB_CM_REJ_PRIVATE_DATA_SIZE);
2258 case IB_CM_RTU_RECEIVED:
2259 case IB_CM_USER_ESTABLISHED:
2260 srpt_cm_rtu_recv(ch);
2262 case IB_CM_DREQ_RECEIVED:
2263 srpt_disconnect_ch(ch);
2265 case IB_CM_DREP_RECEIVED:
2266 pr_info("Received CM DREP message for ch %s-%d.\n",
2267 ch->sess_name, ch->qp->qp_num);
2270 case IB_CM_TIMEWAIT_EXIT:
2271 pr_info("Received CM TimeWait exit for ch %s-%d.\n",
2272 ch->sess_name, ch->qp->qp_num);
2275 case IB_CM_REP_ERROR:
2276 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2279 case IB_CM_DREQ_ERROR:
2280 pr_info("Received CM DREQ ERROR event.\n");
2282 case IB_CM_MRA_RECEIVED:
2283 pr_info("Received CM MRA event\n");
2286 pr_err("received unrecognized CM event %d\n", event->event);
2293 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2295 struct srpt_send_ioctx *ioctx;
2297 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2298 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2302 * srpt_write_pending() - Start data transfer from initiator to target (write).
2304 static int srpt_write_pending(struct se_cmd *se_cmd)
2306 struct srpt_send_ioctx *ioctx =
2307 container_of(se_cmd, struct srpt_send_ioctx, cmd);
2308 struct srpt_rdma_ch *ch = ioctx->ch;
2309 struct ib_send_wr *first_wr = NULL, *bad_wr;
2310 struct ib_cqe *cqe = &ioctx->rdma_cqe;
2311 enum srpt_command_state new_state;
2314 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2315 WARN_ON(new_state == SRPT_STATE_DONE);
2317 if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
2318 pr_warn("%s: IB send queue full (needed %d)\n",
2319 __func__, ioctx->n_rdma);
2324 cqe->done = srpt_rdma_read_done;
2325 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2326 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2328 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
2333 ret = ib_post_send(ch->qp, first_wr, &bad_wr);
2335 pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
2336 __func__, ret, ioctx->n_rdma,
2337 atomic_read(&ch->sq_wr_avail));
2343 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
2347 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2349 switch (tcm_mgmt_status) {
2350 case TMR_FUNCTION_COMPLETE:
2351 return SRP_TSK_MGMT_SUCCESS;
2352 case TMR_FUNCTION_REJECTED:
2353 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2355 return SRP_TSK_MGMT_FAILED;
2359 * srpt_queue_response() - Transmits the response to a SCSI command.
2361 * Callback function called by the TCM core. Must not block since it can be
2362 * invoked on the context of the IB completion handler.
2364 static void srpt_queue_response(struct se_cmd *cmd)
2366 struct srpt_send_ioctx *ioctx =
2367 container_of(cmd, struct srpt_send_ioctx, cmd);
2368 struct srpt_rdma_ch *ch = ioctx->ch;
2369 struct srpt_device *sdev = ch->sport->sdev;
2370 struct ib_send_wr send_wr, *first_wr = &send_wr, *bad_wr;
2372 enum srpt_command_state state;
2373 unsigned long flags;
2374 int resp_len, ret, i;
2379 spin_lock_irqsave(&ioctx->spinlock, flags);
2380 state = ioctx->state;
2382 case SRPT_STATE_NEW:
2383 case SRPT_STATE_DATA_IN:
2384 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2386 case SRPT_STATE_MGMT:
2387 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2390 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2391 ch, ioctx->ioctx.index, ioctx->state);
2394 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2396 if (unlikely(WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)))
2399 /* For read commands, transfer the data to the initiator. */
2400 if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
2401 ioctx->cmd.data_length &&
2402 !ioctx->queue_status_only) {
2403 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2404 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2406 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
2407 ch->sport->port, NULL, first_wr);
2411 if (state != SRPT_STATE_MGMT)
2412 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
2416 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2417 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
2421 atomic_inc(&ch->req_lim);
2423 if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
2424 &ch->sq_wr_avail) < 0)) {
2425 pr_warn("%s: IB send queue full (needed %d)\n",
2426 __func__, ioctx->n_rdma);
2431 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
2434 sge.addr = ioctx->ioctx.dma;
2435 sge.length = resp_len;
2436 sge.lkey = sdev->lkey;
2438 ioctx->ioctx.cqe.done = srpt_send_done;
2439 send_wr.next = NULL;
2440 send_wr.wr_cqe = &ioctx->ioctx.cqe;
2441 send_wr.sg_list = &sge;
2442 send_wr.num_sge = 1;
2443 send_wr.opcode = IB_WR_SEND;
2444 send_wr.send_flags = IB_SEND_SIGNALED;
2446 ret = ib_post_send(ch->qp, first_wr, &bad_wr);
2448 pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
2449 __func__, ioctx->cmd.tag, ret);
2456 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
2457 atomic_dec(&ch->req_lim);
2458 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2459 target_put_sess_cmd(&ioctx->cmd);
2462 static int srpt_queue_data_in(struct se_cmd *cmd)
2464 srpt_queue_response(cmd);
2468 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2470 srpt_queue_response(cmd);
2473 static void srpt_aborted_task(struct se_cmd *cmd)
2477 static int srpt_queue_status(struct se_cmd *cmd)
2479 struct srpt_send_ioctx *ioctx;
2481 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2482 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2483 if (cmd->se_cmd_flags &
2484 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2485 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2486 ioctx->queue_status_only = true;
2487 srpt_queue_response(cmd);
2491 static void srpt_refresh_port_work(struct work_struct *work)
2493 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2495 srpt_refresh_port(sport);
2499 * srpt_release_sdev() - Free the channel resources associated with a target.
2501 static int srpt_release_sdev(struct srpt_device *sdev)
2505 WARN_ON_ONCE(irqs_disabled());
2509 mutex_lock(&sdev->mutex);
2510 for (i = 0; i < ARRAY_SIZE(sdev->port); i++)
2511 srpt_set_enabled(&sdev->port[i], false);
2512 mutex_unlock(&sdev->mutex);
2514 res = wait_event_interruptible(sdev->ch_releaseQ,
2515 list_empty_careful(&sdev->rch_list));
2517 pr_err("%s: interrupted.\n", __func__);
2522 static struct se_wwn *__srpt_lookup_wwn(const char *name)
2524 struct ib_device *dev;
2525 struct srpt_device *sdev;
2526 struct srpt_port *sport;
2529 list_for_each_entry(sdev, &srpt_dev_list, list) {
2534 for (i = 0; i < dev->phys_port_cnt; i++) {
2535 sport = &sdev->port[i];
2537 if (strcmp(sport->port_guid, name) == 0)
2538 return &sport->port_guid_wwn;
2539 if (strcmp(sport->port_gid, name) == 0)
2540 return &sport->port_gid_wwn;
2547 static struct se_wwn *srpt_lookup_wwn(const char *name)
2551 spin_lock(&srpt_dev_lock);
2552 wwn = __srpt_lookup_wwn(name);
2553 spin_unlock(&srpt_dev_lock);
2558 static void srpt_free_srq(struct srpt_device *sdev)
2563 ib_destroy_srq(sdev->srq);
2564 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2565 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2569 static int srpt_alloc_srq(struct srpt_device *sdev)
2571 struct ib_srq_init_attr srq_attr = {
2572 .event_handler = srpt_srq_event,
2573 .srq_context = (void *)sdev,
2574 .attr.max_wr = sdev->srq_size,
2576 .srq_type = IB_SRQT_BASIC,
2578 struct ib_device *device = sdev->device;
2582 WARN_ON_ONCE(sdev->srq);
2583 srq = ib_create_srq(sdev->pd, &srq_attr);
2585 pr_debug("ib_create_srq() failed: %ld\n", PTR_ERR(srq));
2586 return PTR_ERR(srq);
2589 pr_debug("create SRQ #wr= %d max_allow=%d dev= %s\n", sdev->srq_size,
2590 sdev->device->attrs.max_srq_wr, device->name);
2592 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2593 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2594 sizeof(*sdev->ioctx_ring[0]),
2595 srp_max_req_size, DMA_FROM_DEVICE);
2596 if (!sdev->ioctx_ring) {
2597 ib_destroy_srq(srq);
2601 sdev->use_srq = true;
2604 for (i = 0; i < sdev->srq_size; ++i)
2605 srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]);
2610 static int srpt_use_srq(struct srpt_device *sdev, bool use_srq)
2612 struct ib_device *device = sdev->device;
2616 srpt_free_srq(sdev);
2617 sdev->use_srq = false;
2618 } else if (use_srq && !sdev->srq) {
2619 ret = srpt_alloc_srq(sdev);
2621 pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__, device->name,
2622 sdev->use_srq, ret);
2627 * srpt_add_one() - Infiniband device addition callback function.
2629 static void srpt_add_one(struct ib_device *device)
2631 struct srpt_device *sdev;
2632 struct srpt_port *sport;
2635 pr_debug("device = %p\n", device);
2637 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
2641 sdev->device = device;
2642 INIT_LIST_HEAD(&sdev->rch_list);
2643 init_waitqueue_head(&sdev->ch_releaseQ);
2644 mutex_init(&sdev->mutex);
2646 sdev->pd = ib_alloc_pd(device, 0);
2647 if (IS_ERR(sdev->pd))
2650 sdev->lkey = sdev->pd->local_dma_lkey;
2652 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
2654 srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq);
2656 if (!srpt_service_guid)
2657 srpt_service_guid = be64_to_cpu(device->node_guid);
2659 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2660 if (IS_ERR(sdev->cm_id))
2663 /* print out target login information */
2664 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2665 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2666 srpt_service_guid, srpt_service_guid);
2669 * We do not have a consistent service_id (ie. also id_ext of target_id)
2670 * to identify this target. We currently use the guid of the first HCA
2671 * in the system as service_id; therefore, the target_id will change
2672 * if this HCA is gone bad and replaced by different HCA
2674 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
2677 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2678 srpt_event_handler);
2679 ib_register_event_handler(&sdev->event_handler);
2681 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
2683 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2684 sport = &sdev->port[i - 1];
2687 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2688 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2689 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2690 sport->port_attrib.use_srq = false;
2691 INIT_WORK(&sport->work, srpt_refresh_port_work);
2693 if (srpt_refresh_port(sport)) {
2694 pr_err("MAD registration failed for %s-%d.\n",
2695 sdev->device->name, i);
2700 spin_lock(&srpt_dev_lock);
2701 list_add_tail(&sdev->list, &srpt_dev_list);
2702 spin_unlock(&srpt_dev_lock);
2705 ib_set_client_data(device, &srpt_client, sdev);
2706 pr_debug("added %s.\n", device->name);
2710 ib_unregister_event_handler(&sdev->event_handler);
2712 ib_destroy_cm_id(sdev->cm_id);
2714 srpt_free_srq(sdev);
2715 ib_dealloc_pd(sdev->pd);
2720 pr_info("%s(%s) failed.\n", __func__, device->name);
2725 * srpt_remove_one() - InfiniBand device removal callback function.
2727 static void srpt_remove_one(struct ib_device *device, void *client_data)
2729 struct srpt_device *sdev = client_data;
2733 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
2737 srpt_unregister_mad_agent(sdev);
2739 ib_unregister_event_handler(&sdev->event_handler);
2741 /* Cancel any work queued by the just unregistered IB event handler. */
2742 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2743 cancel_work_sync(&sdev->port[i].work);
2745 ib_destroy_cm_id(sdev->cm_id);
2748 * Unregistering a target must happen after destroying sdev->cm_id
2749 * such that no new SRP_LOGIN_REQ information units can arrive while
2750 * destroying the target.
2752 spin_lock(&srpt_dev_lock);
2753 list_del(&sdev->list);
2754 spin_unlock(&srpt_dev_lock);
2755 srpt_release_sdev(sdev);
2757 srpt_free_srq(sdev);
2759 ib_dealloc_pd(sdev->pd);
2764 static struct ib_client srpt_client = {
2766 .add = srpt_add_one,
2767 .remove = srpt_remove_one
2770 static int srpt_check_true(struct se_portal_group *se_tpg)
2775 static int srpt_check_false(struct se_portal_group *se_tpg)
2780 static char *srpt_get_fabric_name(void)
2785 static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg)
2787 return tpg->se_tpg_wwn->priv;
2790 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
2792 struct srpt_port *sport = srpt_tpg_to_sport(tpg);
2794 WARN_ON_ONCE(tpg != &sport->port_guid_tpg &&
2795 tpg != &sport->port_gid_tpg);
2796 return tpg == &sport->port_guid_tpg ? sport->port_guid :
2800 static u16 srpt_get_tag(struct se_portal_group *tpg)
2805 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
2810 static void srpt_release_cmd(struct se_cmd *se_cmd)
2812 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
2813 struct srpt_send_ioctx, cmd);
2814 struct srpt_rdma_ch *ch = ioctx->ch;
2815 unsigned long flags;
2817 WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
2818 !(ioctx->cmd.transport_state & CMD_T_ABORTED));
2820 if (ioctx->n_rw_ctx) {
2821 srpt_free_rw_ctxs(ch, ioctx);
2822 ioctx->n_rw_ctx = 0;
2825 spin_lock_irqsave(&ch->spinlock, flags);
2826 list_add(&ioctx->free_list, &ch->free_list);
2827 spin_unlock_irqrestore(&ch->spinlock, flags);
2831 * srpt_close_session() - Forcibly close a session.
2833 * Callback function invoked by the TCM core to clean up sessions associated
2834 * with a node ACL when the user invokes
2835 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
2837 static void srpt_close_session(struct se_session *se_sess)
2839 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2840 struct srpt_device *sdev = ch->sport->sdev;
2842 mutex_lock(&sdev->mutex);
2843 srpt_disconnect_ch_sync(ch);
2844 mutex_unlock(&sdev->mutex);
2848 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
2850 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
2851 * This object represents an arbitrary integer used to uniquely identify a
2852 * particular attached remote initiator port to a particular SCSI target port
2853 * within a particular SCSI target device within a particular SCSI instance.
2855 static u32 srpt_sess_get_index(struct se_session *se_sess)
2860 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
2864 /* Note: only used from inside debug printk's by the TCM core. */
2865 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
2867 struct srpt_send_ioctx *ioctx;
2869 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2870 return srpt_get_cmd_state(ioctx);
2873 static int srpt_parse_guid(u64 *guid, const char *name)
2878 if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4)
2880 *guid = get_unaligned_be64(w);
2887 * srpt_parse_i_port_id() - Parse an initiator port ID.
2888 * @name: ASCII representation of a 128-bit initiator port ID.
2889 * @i_port_id: Binary 128-bit port ID.
2891 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
2894 unsigned len, count, leading_zero_bytes;
2898 if (strncasecmp(p, "0x", 2) == 0)
2904 count = min(len / 2, 16U);
2905 leading_zero_bytes = 16 - count;
2906 memset(i_port_id, 0, leading_zero_bytes);
2907 ret = hex2bin(i_port_id + leading_zero_bytes, p, count);
2909 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", ret);
2915 * configfs callback function invoked for
2916 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
2918 static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
2924 ret = srpt_parse_guid(&guid, name);
2926 ret = srpt_parse_i_port_id(i_port_id, name);
2928 pr_err("invalid initiator port ID %s\n", name);
2932 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
2935 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2936 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
2938 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
2941 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
2942 const char *page, size_t count)
2944 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2945 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
2949 ret = kstrtoul(page, 0, &val);
2951 pr_err("kstrtoul() failed with ret: %d\n", ret);
2954 if (val > MAX_SRPT_RDMA_SIZE) {
2955 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
2956 MAX_SRPT_RDMA_SIZE);
2959 if (val < DEFAULT_MAX_RDMA_SIZE) {
2960 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
2961 val, DEFAULT_MAX_RDMA_SIZE);
2964 sport->port_attrib.srp_max_rdma_size = val;
2969 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
2972 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2973 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
2975 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
2978 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
2979 const char *page, size_t count)
2981 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2982 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
2986 ret = kstrtoul(page, 0, &val);
2988 pr_err("kstrtoul() failed with ret: %d\n", ret);
2991 if (val > MAX_SRPT_RSP_SIZE) {
2992 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
2996 if (val < MIN_MAX_RSP_SIZE) {
2997 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3001 sport->port_attrib.srp_max_rsp_size = val;
3006 static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3009 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3010 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3012 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3015 static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3016 const char *page, size_t count)
3018 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3019 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3023 ret = kstrtoul(page, 0, &val);
3025 pr_err("kstrtoul() failed with ret: %d\n", ret);
3028 if (val > MAX_SRPT_SRQ_SIZE) {
3029 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3033 if (val < MIN_SRPT_SRQ_SIZE) {
3034 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3038 sport->port_attrib.srp_sq_size = val;
3043 static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item,
3046 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3047 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3049 return sprintf(page, "%d\n", sport->port_attrib.use_srq);
3052 static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item,
3053 const char *page, size_t count)
3055 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3056 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3057 struct srpt_device *sdev = sport->sdev;
3062 ret = kstrtoul(page, 0, &val);
3068 ret = mutex_lock_interruptible(&sdev->mutex);
3071 enabled = sport->enabled;
3072 /* Log out all initiator systems before changing 'use_srq'. */
3073 srpt_set_enabled(sport, false);
3074 sport->port_attrib.use_srq = val;
3075 srpt_use_srq(sdev, sport->port_attrib.use_srq);
3076 srpt_set_enabled(sport, enabled);
3077 mutex_unlock(&sdev->mutex);
3082 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3083 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3084 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
3085 CONFIGFS_ATTR(srpt_tpg_attrib_, use_srq);
3087 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3088 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3089 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3090 &srpt_tpg_attrib_attr_srp_sq_size,
3091 &srpt_tpg_attrib_attr_use_srq,
3095 static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
3097 struct se_portal_group *se_tpg = to_tpg(item);
3098 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3100 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3103 static ssize_t srpt_tpg_enable_store(struct config_item *item,
3104 const char *page, size_t count)
3106 struct se_portal_group *se_tpg = to_tpg(item);
3107 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3108 struct srpt_device *sdev = sport->sdev;
3112 ret = kstrtoul(page, 0, &tmp);
3114 pr_err("Unable to extract srpt_tpg_store_enable\n");
3118 if ((tmp != 0) && (tmp != 1)) {
3119 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3123 mutex_lock(&sdev->mutex);
3124 srpt_set_enabled(sport, tmp);
3125 mutex_unlock(&sdev->mutex);
3130 CONFIGFS_ATTR(srpt_tpg_, enable);
3132 static struct configfs_attribute *srpt_tpg_attrs[] = {
3133 &srpt_tpg_attr_enable,
3138 * configfs callback invoked for
3139 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3141 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3142 struct config_group *group,
3145 struct srpt_port *sport = wwn->priv;
3146 static struct se_portal_group *tpg;
3149 WARN_ON_ONCE(wwn != &sport->port_guid_wwn &&
3150 wwn != &sport->port_gid_wwn);
3151 tpg = wwn == &sport->port_guid_wwn ? &sport->port_guid_tpg :
3152 &sport->port_gid_tpg;
3153 res = core_tpg_register(wwn, tpg, SCSI_PROTOCOL_SRP);
3155 return ERR_PTR(res);
3161 * configfs callback invoked for
3162 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3164 static void srpt_drop_tpg(struct se_portal_group *tpg)
3166 struct srpt_port *sport = srpt_tpg_to_sport(tpg);
3168 sport->enabled = false;
3169 core_tpg_deregister(tpg);
3173 * configfs callback invoked for
3174 * mkdir /sys/kernel/config/target/$driver/$port
3176 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3177 struct config_group *group,
3180 return srpt_lookup_wwn(name) ? : ERR_PTR(-EINVAL);
3184 * configfs callback invoked for
3185 * rmdir /sys/kernel/config/target/$driver/$port
3187 static void srpt_drop_tport(struct se_wwn *wwn)
3191 static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
3193 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3196 CONFIGFS_ATTR_RO(srpt_wwn_, version);
3198 static struct configfs_attribute *srpt_wwn_attrs[] = {
3199 &srpt_wwn_attr_version,
3203 static const struct target_core_fabric_ops srpt_template = {
3204 .module = THIS_MODULE,
3206 .get_fabric_name = srpt_get_fabric_name,
3207 .tpg_get_wwn = srpt_get_fabric_wwn,
3208 .tpg_get_tag = srpt_get_tag,
3209 .tpg_check_demo_mode = srpt_check_false,
3210 .tpg_check_demo_mode_cache = srpt_check_true,
3211 .tpg_check_demo_mode_write_protect = srpt_check_true,
3212 .tpg_check_prod_mode_write_protect = srpt_check_false,
3213 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3214 .release_cmd = srpt_release_cmd,
3215 .check_stop_free = srpt_check_stop_free,
3216 .close_session = srpt_close_session,
3217 .sess_get_index = srpt_sess_get_index,
3218 .sess_get_initiator_sid = NULL,
3219 .write_pending = srpt_write_pending,
3220 .write_pending_status = srpt_write_pending_status,
3221 .set_default_node_attributes = srpt_set_default_node_attrs,
3222 .get_cmd_state = srpt_get_tcm_cmd_state,
3223 .queue_data_in = srpt_queue_data_in,
3224 .queue_status = srpt_queue_status,
3225 .queue_tm_rsp = srpt_queue_tm_rsp,
3226 .aborted_task = srpt_aborted_task,
3228 * Setup function pointers for generic logic in
3229 * target_core_fabric_configfs.c
3231 .fabric_make_wwn = srpt_make_tport,
3232 .fabric_drop_wwn = srpt_drop_tport,
3233 .fabric_make_tpg = srpt_make_tpg,
3234 .fabric_drop_tpg = srpt_drop_tpg,
3235 .fabric_init_nodeacl = srpt_init_nodeacl,
3237 .tfc_wwn_attrs = srpt_wwn_attrs,
3238 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3239 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
3243 * srpt_init_module() - Kernel module initialization.
3245 * Note: Since ib_register_client() registers callback functions, and since at
3246 * least one of these callback functions (srpt_add_one()) calls target core
3247 * functions, this driver must be registered with the target core before
3248 * ib_register_client() is called.
3250 static int __init srpt_init_module(void)
3255 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3256 pr_err("invalid value %d for kernel module parameter"
3257 " srp_max_req_size -- must be at least %d.\n",
3258 srp_max_req_size, MIN_MAX_REQ_SIZE);
3262 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3263 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3264 pr_err("invalid value %d for kernel module parameter"
3265 " srpt_srq_size -- must be in the range [%d..%d].\n",
3266 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3270 ret = target_register_template(&srpt_template);
3274 ret = ib_register_client(&srpt_client);
3276 pr_err("couldn't register IB client\n");
3277 goto out_unregister_target;
3282 out_unregister_target:
3283 target_unregister_template(&srpt_template);
3288 static void __exit srpt_cleanup_module(void)
3290 ib_unregister_client(&srpt_client);
3291 target_unregister_template(&srpt_template);
3294 module_init(srpt_init_module);
3295 module_exit(srpt_cleanup_module);