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, 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;
299 iocp = (struct ib_dm_ioc_profile *)mad->data;
301 if (!slot || slot > 16) {
303 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
309 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
313 memset(iocp, 0, sizeof(*iocp));
314 strcpy(iocp->id_string, SRPT_ID_STRING);
315 iocp->guid = cpu_to_be64(srpt_service_guid);
316 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
317 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
318 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
319 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
320 iocp->subsys_device_id = 0x0;
321 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
322 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
323 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
324 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
325 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
326 iocp->rdma_read_depth = 4;
327 iocp->send_size = cpu_to_be32(srp_max_req_size);
328 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
330 iocp->num_svc_entries = 1;
331 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
332 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
334 mad->mad_hdr.status = 0;
338 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
340 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
341 * Specification. See also section B.7, table B.8 in the SRP r16a document.
343 static void srpt_get_svc_entries(u64 ioc_guid,
344 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
346 struct ib_dm_svc_entries *svc_entries;
350 if (!slot || slot > 16) {
352 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
356 if (slot > 2 || lo > hi || hi > 1) {
358 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
362 svc_entries = (struct ib_dm_svc_entries *)mad->data;
363 memset(svc_entries, 0, sizeof(*svc_entries));
364 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
365 snprintf(svc_entries->service_entries[0].name,
366 sizeof(svc_entries->service_entries[0].name),
368 SRP_SERVICE_NAME_PREFIX,
371 mad->mad_hdr.status = 0;
375 * srpt_mgmt_method_get() - Process a received management datagram.
376 * @sp: source port through which the MAD has been received.
377 * @rq_mad: received MAD.
378 * @rsp_mad: response MAD.
380 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
381 struct ib_dm_mad *rsp_mad)
387 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
389 case DM_ATTR_CLASS_PORT_INFO:
390 srpt_get_class_port_info(rsp_mad);
392 case DM_ATTR_IOU_INFO:
393 srpt_get_iou(rsp_mad);
395 case DM_ATTR_IOC_PROFILE:
396 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
397 srpt_get_ioc(sp, slot, rsp_mad);
399 case DM_ATTR_SVC_ENTRIES:
400 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
401 hi = (u8) ((slot >> 8) & 0xff);
402 lo = (u8) (slot & 0xff);
403 slot = (u16) ((slot >> 16) & 0xffff);
404 srpt_get_svc_entries(srpt_service_guid,
405 slot, hi, lo, rsp_mad);
408 rsp_mad->mad_hdr.status =
409 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
415 * srpt_mad_send_handler() - Post MAD-send callback function.
417 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
418 struct ib_mad_send_wc *mad_wc)
420 ib_destroy_ah(mad_wc->send_buf->ah);
421 ib_free_send_mad(mad_wc->send_buf);
425 * srpt_mad_recv_handler() - MAD reception callback function.
427 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
428 struct ib_mad_send_buf *send_buf,
429 struct ib_mad_recv_wc *mad_wc)
431 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
433 struct ib_mad_send_buf *rsp;
434 struct ib_dm_mad *dm_mad;
436 if (!mad_wc || !mad_wc->recv_buf.mad)
439 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
440 mad_wc->recv_buf.grh, mad_agent->port_num);
444 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
446 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
447 mad_wc->wc->pkey_index, 0,
448 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
450 IB_MGMT_BASE_VERSION);
457 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
458 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
459 dm_mad->mad_hdr.status = 0;
461 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
462 case IB_MGMT_METHOD_GET:
463 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
465 case IB_MGMT_METHOD_SET:
466 dm_mad->mad_hdr.status =
467 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
470 dm_mad->mad_hdr.status =
471 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
475 if (!ib_post_send_mad(rsp, NULL)) {
476 ib_free_recv_mad(mad_wc);
477 /* will destroy_ah & free_send_mad in send completion */
481 ib_free_send_mad(rsp);
486 ib_free_recv_mad(mad_wc);
490 * srpt_refresh_port() - Configure a HCA port.
492 * Enable InfiniBand management datagram processing, update the cached sm_lid,
493 * lid and gid values, and register a callback function for processing MADs
494 * on the specified port.
496 * Note: It is safe to call this function more than once for the same port.
498 static int srpt_refresh_port(struct srpt_port *sport)
500 struct ib_mad_reg_req reg_req;
501 struct ib_port_modify port_modify;
502 struct ib_port_attr port_attr;
505 memset(&port_modify, 0, sizeof(port_modify));
506 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
507 port_modify.clr_port_cap_mask = 0;
509 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
513 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
517 sport->sm_lid = port_attr.sm_lid;
518 sport->lid = port_attr.lid;
520 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
525 snprintf(sport->port_guid, sizeof(sport->port_guid),
527 be64_to_cpu(sport->gid.global.subnet_prefix),
528 be64_to_cpu(sport->gid.global.interface_id));
530 if (!sport->mad_agent) {
531 memset(®_req, 0, sizeof(reg_req));
532 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
533 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
534 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
535 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
537 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
541 srpt_mad_send_handler,
542 srpt_mad_recv_handler,
544 if (IS_ERR(sport->mad_agent)) {
545 ret = PTR_ERR(sport->mad_agent);
546 sport->mad_agent = NULL;
555 port_modify.set_port_cap_mask = 0;
556 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
557 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
565 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
567 * Note: It is safe to call this function more than once for the same device.
569 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
571 struct ib_port_modify port_modify = {
572 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
574 struct srpt_port *sport;
577 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
578 sport = &sdev->port[i - 1];
579 WARN_ON(sport->port != i);
580 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
581 pr_err("disabling MAD processing failed.\n");
582 if (sport->mad_agent) {
583 ib_unregister_mad_agent(sport->mad_agent);
584 sport->mad_agent = NULL;
590 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
592 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
593 int ioctx_size, int dma_size,
594 enum dma_data_direction dir)
596 struct srpt_ioctx *ioctx;
598 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
602 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
606 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
607 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
621 * srpt_free_ioctx() - Free an SRPT I/O context structure.
623 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
624 int dma_size, enum dma_data_direction dir)
629 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
635 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
636 * @sdev: Device to allocate the I/O context ring for.
637 * @ring_size: Number of elements in the I/O context ring.
638 * @ioctx_size: I/O context size.
639 * @dma_size: DMA buffer size.
640 * @dir: DMA data direction.
642 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
643 int ring_size, int ioctx_size,
644 int dma_size, enum dma_data_direction dir)
646 struct srpt_ioctx **ring;
649 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
650 && ioctx_size != sizeof(struct srpt_send_ioctx));
652 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
655 for (i = 0; i < ring_size; ++i) {
656 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
665 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
673 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
675 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
676 struct srpt_device *sdev, int ring_size,
677 int dma_size, enum dma_data_direction dir)
681 for (i = 0; i < ring_size; ++i)
682 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
687 * srpt_get_cmd_state() - Get the state of a SCSI command.
689 static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
691 enum srpt_command_state state;
696 spin_lock_irqsave(&ioctx->spinlock, flags);
697 state = ioctx->state;
698 spin_unlock_irqrestore(&ioctx->spinlock, flags);
703 * srpt_set_cmd_state() - Set the state of a SCSI command.
705 * Does not modify the state of aborted commands. Returns the previous command
708 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
709 enum srpt_command_state new)
711 enum srpt_command_state previous;
716 spin_lock_irqsave(&ioctx->spinlock, flags);
717 previous = ioctx->state;
718 if (previous != SRPT_STATE_DONE)
720 spin_unlock_irqrestore(&ioctx->spinlock, flags);
726 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
728 * Returns true if and only if the previous command state was equal to 'old'.
730 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
731 enum srpt_command_state old,
732 enum srpt_command_state new)
734 enum srpt_command_state previous;
738 WARN_ON(old == SRPT_STATE_DONE);
739 WARN_ON(new == SRPT_STATE_NEW);
741 spin_lock_irqsave(&ioctx->spinlock, flags);
742 previous = ioctx->state;
745 spin_unlock_irqrestore(&ioctx->spinlock, flags);
746 return previous == old;
750 * srpt_post_recv() - Post an IB receive request.
752 static int srpt_post_recv(struct srpt_device *sdev,
753 struct srpt_recv_ioctx *ioctx)
756 struct ib_recv_wr wr, *bad_wr;
759 list.addr = ioctx->ioctx.dma;
760 list.length = srp_max_req_size;
761 list.lkey = sdev->pd->local_dma_lkey;
763 ioctx->ioctx.cqe.done = srpt_recv_done;
764 wr.wr_cqe = &ioctx->ioctx.cqe;
769 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
773 * srpt_zerolength_write() - Perform a zero-length RDMA write.
775 * A quote from the InfiniBand specification: C9-88: For an HCA responder
776 * using Reliable Connection service, for each zero-length RDMA READ or WRITE
777 * request, the R_Key shall not be validated, even if the request includes
780 static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
782 struct ib_send_wr wr, *bad_wr;
784 memset(&wr, 0, sizeof(wr));
785 wr.opcode = IB_WR_RDMA_WRITE;
786 wr.wr_cqe = &ch->zw_cqe;
787 wr.send_flags = IB_SEND_SIGNALED;
788 return ib_post_send(ch->qp, &wr, &bad_wr);
791 static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
793 struct srpt_rdma_ch *ch = cq->cq_context;
795 if (wc->status == IB_WC_SUCCESS) {
796 srpt_process_wait_list(ch);
798 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
799 schedule_work(&ch->release_work);
801 WARN_ONCE(1, "%s-%d\n", ch->sess_name, ch->qp->qp_num);
805 static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
806 struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
809 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
810 struct srpt_rdma_ch *ch = ioctx->ch;
811 struct scatterlist *prev = NULL;
816 ioctx->rw_ctxs = &ioctx->s_rw_ctx;
818 ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
824 for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
825 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
826 u64 remote_addr = be64_to_cpu(db->va);
827 u32 size = be32_to_cpu(db->len);
828 u32 rkey = be32_to_cpu(db->key);
830 ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
835 ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
836 ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
838 target_free_sgl(ctx->sg, ctx->nents);
842 ioctx->n_rdma += ret;
846 sg_unmark_end(&prev[prev_nents - 1]);
847 sg_chain(prev, prev_nents + 1, ctx->sg);
853 prev_nents = ctx->nents;
855 *sg_cnt += ctx->nents;
862 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
864 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
865 ctx->sg, ctx->nents, dir);
866 target_free_sgl(ctx->sg, ctx->nents);
868 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
869 kfree(ioctx->rw_ctxs);
873 static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
874 struct srpt_send_ioctx *ioctx)
876 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
879 for (i = 0; i < ioctx->n_rw_ctx; i++) {
880 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
882 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
883 ctx->sg, ctx->nents, dir);
884 target_free_sgl(ctx->sg, ctx->nents);
887 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
888 kfree(ioctx->rw_ctxs);
891 static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
894 * The pointer computations below will only be compiled correctly
895 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
896 * whether srp_cmd::add_data has been declared as a byte pointer.
898 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
899 !__same_type(srp_cmd->add_data[0], (u8)0));
902 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
903 * CDB LENGTH' field are reserved and the size in bytes of this field
904 * is four times the value specified in bits 3..7. Hence the "& ~3".
906 return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
910 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
911 * @ioctx: Pointer to the I/O context associated with the request.
912 * @srp_cmd: Pointer to the SRP_CMD request data.
913 * @dir: Pointer to the variable to which the transfer direction will be
915 * @data_len: Pointer to the variable to which the total data length of all
916 * descriptors in the SRP_CMD request will be written.
918 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
920 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
921 * -ENOMEM when memory allocation fails and zero upon success.
923 static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
924 struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
925 struct scatterlist **sg, unsigned *sg_cnt, u64 *data_len)
931 * The lower four bits of the buffer format field contain the DATA-IN
932 * buffer descriptor format, and the highest four bits contain the
933 * DATA-OUT buffer descriptor format.
935 if (srp_cmd->buf_fmt & 0xf)
936 /* DATA-IN: transfer data from target to initiator (read). */
937 *dir = DMA_FROM_DEVICE;
938 else if (srp_cmd->buf_fmt >> 4)
939 /* DATA-OUT: transfer data from initiator to target (write). */
940 *dir = DMA_TO_DEVICE;
944 /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
945 ioctx->cmd.data_direction = *dir;
947 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
948 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
949 struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
951 *data_len = be32_to_cpu(db->len);
952 return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
953 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
954 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
955 struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
956 int nbufs = be32_to_cpu(idb->table_desc.len) /
957 sizeof(struct srp_direct_buf);
960 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
961 pr_err("received unsupported SRP_CMD request"
962 " type (%u out + %u in != %u / %zu)\n",
963 srp_cmd->data_out_desc_cnt,
964 srp_cmd->data_in_desc_cnt,
965 be32_to_cpu(idb->table_desc.len),
966 sizeof(struct srp_direct_buf));
970 *data_len = be32_to_cpu(idb->len);
971 return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
980 * srpt_init_ch_qp() - Initialize queue pair attributes.
982 * Initialized the attributes of queue pair 'qp' by allowing local write,
983 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
985 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
987 struct ib_qp_attr *attr;
990 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
994 attr->qp_state = IB_QPS_INIT;
995 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
996 attr->port_num = ch->sport->port;
997 attr->pkey_index = 0;
999 ret = ib_modify_qp(qp, attr,
1000 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
1008 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
1009 * @ch: channel of the queue pair.
1010 * @qp: queue pair to change the state of.
1012 * Returns zero upon success and a negative value upon failure.
1014 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1015 * If this structure ever becomes larger, it might be necessary to allocate
1016 * it dynamically instead of on the stack.
1018 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1020 struct ib_qp_attr qp_attr;
1024 qp_attr.qp_state = IB_QPS_RTR;
1025 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1029 qp_attr.max_dest_rd_atomic = 4;
1031 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1038 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1039 * @ch: channel of the queue pair.
1040 * @qp: queue pair to change the state of.
1042 * Returns zero upon success and a negative value upon failure.
1044 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1045 * If this structure ever becomes larger, it might be necessary to allocate
1046 * it dynamically instead of on the stack.
1048 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1050 struct ib_qp_attr qp_attr;
1054 qp_attr.qp_state = IB_QPS_RTS;
1055 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1059 qp_attr.max_rd_atomic = 4;
1061 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1068 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1070 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1072 struct ib_qp_attr qp_attr;
1074 qp_attr.qp_state = IB_QPS_ERR;
1075 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1079 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1081 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1083 struct srpt_send_ioctx *ioctx;
1084 unsigned long flags;
1089 spin_lock_irqsave(&ch->spinlock, flags);
1090 if (!list_empty(&ch->free_list)) {
1091 ioctx = list_first_entry(&ch->free_list,
1092 struct srpt_send_ioctx, free_list);
1093 list_del(&ioctx->free_list);
1095 spin_unlock_irqrestore(&ch->spinlock, flags);
1100 BUG_ON(ioctx->ch != ch);
1101 spin_lock_init(&ioctx->spinlock);
1102 ioctx->state = SRPT_STATE_NEW;
1104 ioctx->n_rw_ctx = 0;
1105 init_completion(&ioctx->tx_done);
1106 ioctx->queue_status_only = false;
1108 * transport_init_se_cmd() does not initialize all fields, so do it
1111 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1112 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1118 * srpt_abort_cmd() - Abort a SCSI command.
1119 * @ioctx: I/O context associated with the SCSI command.
1120 * @context: Preferred execution context.
1122 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1124 enum srpt_command_state state;
1125 unsigned long flags;
1130 * If the command is in a state where the target core is waiting for
1131 * the ib_srpt driver, change the state to the next state.
1134 spin_lock_irqsave(&ioctx->spinlock, flags);
1135 state = ioctx->state;
1137 case SRPT_STATE_NEED_DATA:
1138 ioctx->state = SRPT_STATE_DATA_IN;
1140 case SRPT_STATE_CMD_RSP_SENT:
1141 case SRPT_STATE_MGMT_RSP_SENT:
1142 ioctx->state = SRPT_STATE_DONE;
1145 WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
1149 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1151 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1155 case SRPT_STATE_NEW:
1156 case SRPT_STATE_DATA_IN:
1157 case SRPT_STATE_MGMT:
1158 case SRPT_STATE_DONE:
1160 * Do nothing - defer abort processing until
1161 * srpt_queue_response() is invoked.
1164 case SRPT_STATE_NEED_DATA:
1165 pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
1166 transport_generic_request_failure(&ioctx->cmd,
1167 TCM_CHECK_CONDITION_ABORT_CMD);
1169 case SRPT_STATE_CMD_RSP_SENT:
1171 * SRP_RSP sending failed or the SRP_RSP send completion has
1172 * not been received in time.
1174 transport_generic_free_cmd(&ioctx->cmd, 0);
1176 case SRPT_STATE_MGMT_RSP_SENT:
1177 transport_generic_free_cmd(&ioctx->cmd, 0);
1180 WARN(1, "Unexpected command state (%d)", state);
1188 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1189 * the data that has been transferred via IB RDMA had to be postponed until the
1190 * check_stop_free() callback. None of this is necessary anymore and needs to
1193 static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1195 struct srpt_rdma_ch *ch = cq->cq_context;
1196 struct srpt_send_ioctx *ioctx =
1197 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
1199 WARN_ON(ioctx->n_rdma <= 0);
1200 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1203 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1204 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1206 srpt_abort_cmd(ioctx);
1210 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1211 SRPT_STATE_DATA_IN))
1212 target_execute_cmd(&ioctx->cmd);
1214 pr_err("%s[%d]: wrong state = %d\n", __func__,
1215 __LINE__, srpt_get_cmd_state(ioctx));
1219 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1220 * @ch: RDMA channel through which the request has been received.
1221 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1222 * be built in the buffer ioctx->buf points at and hence this function will
1223 * overwrite the request data.
1224 * @tag: tag of the request for which this response is being generated.
1225 * @status: value for the STATUS field of the SRP_RSP information unit.
1227 * Returns the size in bytes of the SRP_RSP response.
1229 * An SRP_RSP response contains a SCSI status or service response. See also
1230 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1231 * response. See also SPC-2 for more information about sense data.
1233 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1234 struct srpt_send_ioctx *ioctx, u64 tag,
1237 struct se_cmd *cmd = &ioctx->cmd;
1238 struct srp_rsp *srp_rsp;
1239 const u8 *sense_data;
1240 int sense_data_len, max_sense_len;
1241 u32 resid = cmd->residual_count;
1244 * The lowest bit of all SAM-3 status codes is zero (see also
1245 * paragraph 5.3 in SAM-3).
1247 WARN_ON(status & 1);
1249 srp_rsp = ioctx->ioctx.buf;
1252 sense_data = ioctx->sense_data;
1253 sense_data_len = ioctx->cmd.scsi_sense_length;
1254 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1256 memset(srp_rsp, 0, sizeof(*srp_rsp));
1257 srp_rsp->opcode = SRP_RSP;
1258 srp_rsp->req_lim_delta =
1259 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1261 srp_rsp->status = status;
1263 if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
1264 if (cmd->data_direction == DMA_TO_DEVICE) {
1265 /* residual data from an underflow write */
1266 srp_rsp->flags = SRP_RSP_FLAG_DOUNDER;
1267 srp_rsp->data_out_res_cnt = cpu_to_be32(resid);
1268 } else if (cmd->data_direction == DMA_FROM_DEVICE) {
1269 /* residual data from an underflow read */
1270 srp_rsp->flags = SRP_RSP_FLAG_DIUNDER;
1271 srp_rsp->data_in_res_cnt = cpu_to_be32(resid);
1273 } else if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1274 if (cmd->data_direction == DMA_TO_DEVICE) {
1275 /* residual data from an overflow write */
1276 srp_rsp->flags = SRP_RSP_FLAG_DOOVER;
1277 srp_rsp->data_out_res_cnt = cpu_to_be32(resid);
1278 } else if (cmd->data_direction == DMA_FROM_DEVICE) {
1279 /* residual data from an overflow read */
1280 srp_rsp->flags = SRP_RSP_FLAG_DIOVER;
1281 srp_rsp->data_in_res_cnt = cpu_to_be32(resid);
1285 if (sense_data_len) {
1286 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1287 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1288 if (sense_data_len > max_sense_len) {
1289 pr_warn("truncated sense data from %d to %d"
1290 " bytes\n", sense_data_len, max_sense_len);
1291 sense_data_len = max_sense_len;
1294 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1295 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1296 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1299 return sizeof(*srp_rsp) + sense_data_len;
1303 * srpt_build_tskmgmt_rsp() - Build a task management response.
1304 * @ch: RDMA channel through which the request has been received.
1305 * @ioctx: I/O context in which the SRP_RSP response will be built.
1306 * @rsp_code: RSP_CODE that will be stored in the response.
1307 * @tag: Tag of the request for which this response is being generated.
1309 * Returns the size in bytes of the SRP_RSP response.
1311 * An SRP_RSP response contains a SCSI status or service response. See also
1312 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1315 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1316 struct srpt_send_ioctx *ioctx,
1317 u8 rsp_code, u64 tag)
1319 struct srp_rsp *srp_rsp;
1324 resp_len = sizeof(*srp_rsp) + resp_data_len;
1326 srp_rsp = ioctx->ioctx.buf;
1328 memset(srp_rsp, 0, sizeof(*srp_rsp));
1330 srp_rsp->opcode = SRP_RSP;
1331 srp_rsp->req_lim_delta =
1332 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1335 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1336 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1337 srp_rsp->data[3] = rsp_code;
1342 static int srpt_check_stop_free(struct se_cmd *cmd)
1344 struct srpt_send_ioctx *ioctx = container_of(cmd,
1345 struct srpt_send_ioctx, cmd);
1347 return target_put_sess_cmd(&ioctx->cmd);
1351 * srpt_handle_cmd() - Process SRP_CMD.
1353 static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1354 struct srpt_recv_ioctx *recv_ioctx,
1355 struct srpt_send_ioctx *send_ioctx)
1358 struct srp_cmd *srp_cmd;
1359 struct scatterlist *sg = NULL;
1360 unsigned sg_cnt = 0;
1362 enum dma_data_direction dir;
1365 BUG_ON(!send_ioctx);
1367 srp_cmd = recv_ioctx->ioctx.buf;
1368 cmd = &send_ioctx->cmd;
1369 cmd->tag = srp_cmd->tag;
1371 switch (srp_cmd->task_attr) {
1372 case SRP_CMD_SIMPLE_Q:
1373 cmd->sam_task_attr = TCM_SIMPLE_TAG;
1375 case SRP_CMD_ORDERED_Q:
1377 cmd->sam_task_attr = TCM_ORDERED_TAG;
1379 case SRP_CMD_HEAD_OF_Q:
1380 cmd->sam_task_attr = TCM_HEAD_TAG;
1383 cmd->sam_task_attr = TCM_ACA_TAG;
1387 rc = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &sg, &sg_cnt,
1390 if (rc != -EAGAIN) {
1391 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1397 rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
1398 &send_ioctx->sense_data[0],
1399 scsilun_to_int(&srp_cmd->lun), data_len,
1400 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
1401 sg, sg_cnt, NULL, 0, NULL, 0);
1403 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1410 send_ioctx->state = SRPT_STATE_DONE;
1411 srpt_release_cmd(cmd);
1414 static int srp_tmr_to_tcm(int fn)
1417 case SRP_TSK_ABORT_TASK:
1418 return TMR_ABORT_TASK;
1419 case SRP_TSK_ABORT_TASK_SET:
1420 return TMR_ABORT_TASK_SET;
1421 case SRP_TSK_CLEAR_TASK_SET:
1422 return TMR_CLEAR_TASK_SET;
1423 case SRP_TSK_LUN_RESET:
1424 return TMR_LUN_RESET;
1425 case SRP_TSK_CLEAR_ACA:
1426 return TMR_CLEAR_ACA;
1433 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1435 * Returns 0 if and only if the request will be processed by the target core.
1437 * For more information about SRP_TSK_MGMT information units, see also section
1438 * 6.7 in the SRP r16a document.
1440 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1441 struct srpt_recv_ioctx *recv_ioctx,
1442 struct srpt_send_ioctx *send_ioctx)
1444 struct srp_tsk_mgmt *srp_tsk;
1446 struct se_session *sess = ch->sess;
1450 BUG_ON(!send_ioctx);
1452 srp_tsk = recv_ioctx->ioctx.buf;
1453 cmd = &send_ioctx->cmd;
1455 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1456 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1457 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1459 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1460 send_ioctx->cmd.tag = srp_tsk->tag;
1461 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1462 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1463 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1464 GFP_KERNEL, srp_tsk->task_tag,
1465 TARGET_SCF_ACK_KREF);
1467 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1472 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
1476 * srpt_handle_new_iu() - Process a newly received information unit.
1477 * @ch: RDMA channel through which the information unit has been received.
1478 * @ioctx: SRPT I/O context associated with the information unit.
1480 static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1481 struct srpt_recv_ioctx *recv_ioctx,
1482 struct srpt_send_ioctx *send_ioctx)
1484 struct srp_cmd *srp_cmd;
1487 BUG_ON(!recv_ioctx);
1489 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1490 recv_ioctx->ioctx.dma, srp_max_req_size,
1493 if (unlikely(ch->state == CH_CONNECTING))
1496 if (unlikely(ch->state != CH_LIVE))
1499 srp_cmd = recv_ioctx->ioctx.buf;
1500 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1502 if (!list_empty(&ch->cmd_wait_list))
1504 send_ioctx = srpt_get_send_ioctx(ch);
1506 if (unlikely(!send_ioctx))
1510 switch (srp_cmd->opcode) {
1512 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1515 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1518 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
1521 pr_debug("received SRP_CRED_RSP\n");
1524 pr_debug("received SRP_AER_RSP\n");
1527 pr_err("Received SRP_RSP\n");
1530 pr_err("received IU with unknown opcode 0x%x\n",
1535 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1539 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1542 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1544 struct srpt_rdma_ch *ch = cq->cq_context;
1545 struct srpt_recv_ioctx *ioctx =
1546 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
1548 if (wc->status == IB_WC_SUCCESS) {
1551 req_lim = atomic_dec_return(&ch->req_lim);
1552 if (unlikely(req_lim < 0))
1553 pr_err("req_lim = %d < 0\n", req_lim);
1554 srpt_handle_new_iu(ch, ioctx, NULL);
1556 pr_info("receiving failed for ioctx %p with status %d\n",
1562 * This function must be called from the context in which RDMA completions are
1563 * processed because it accesses the wait list without protection against
1564 * access from other threads.
1566 static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
1568 struct srpt_send_ioctx *ioctx;
1570 while (!list_empty(&ch->cmd_wait_list) &&
1571 ch->state >= CH_LIVE &&
1572 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
1573 struct srpt_recv_ioctx *recv_ioctx;
1575 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1576 struct srpt_recv_ioctx,
1578 list_del(&recv_ioctx->wait_list);
1579 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
1584 * Note: Although this has not yet been observed during tests, at least in
1585 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1586 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1587 * value in each response is set to one, and it is possible that this response
1588 * makes the initiator send a new request before the send completion for that
1589 * response has been processed. This could e.g. happen if the call to
1590 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1591 * if IB retransmission causes generation of the send completion to be
1592 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1593 * are queued on cmd_wait_list. The code below processes these delayed
1594 * requests one at a time.
1596 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
1598 struct srpt_rdma_ch *ch = cq->cq_context;
1599 struct srpt_send_ioctx *ioctx =
1600 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1601 enum srpt_command_state state;
1603 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1605 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1606 state != SRPT_STATE_MGMT_RSP_SENT);
1608 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
1610 if (wc->status != IB_WC_SUCCESS)
1611 pr_info("sending response for ioctx 0x%p failed"
1612 " with status %d\n", ioctx, wc->status);
1614 if (state != SRPT_STATE_DONE) {
1615 transport_generic_free_cmd(&ioctx->cmd, 0);
1617 pr_err("IB completion has been received too late for"
1618 " wr_id = %u.\n", ioctx->ioctx.index);
1621 srpt_process_wait_list(ch);
1625 * srpt_create_ch_ib() - Create receive and send completion queues.
1627 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1629 struct ib_qp_init_attr *qp_init;
1630 struct srpt_port *sport = ch->sport;
1631 struct srpt_device *sdev = sport->sdev;
1632 const struct ib_device_attr *attrs = &sdev->device->attrs;
1633 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1636 WARN_ON(ch->rq_size < 1);
1639 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
1644 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1645 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
1646 if (IS_ERR(ch->cq)) {
1647 ret = PTR_ERR(ch->cq);
1648 pr_err("failed to create CQ cqe= %d ret= %d\n",
1649 ch->rq_size + srp_sq_size, ret);
1653 qp_init->qp_context = (void *)ch;
1654 qp_init->event_handler
1655 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1656 qp_init->send_cq = ch->cq;
1657 qp_init->recv_cq = ch->cq;
1658 qp_init->srq = sdev->srq;
1659 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1660 qp_init->qp_type = IB_QPT_RC;
1662 * We divide up our send queue size into half SEND WRs to send the
1663 * completions, and half R/W contexts to actually do the RDMA
1664 * READ/WRITE transfers. Note that we need to allocate CQ slots for
1665 * both both, as RDMA contexts will also post completions for the
1668 qp_init->cap.max_send_wr = srp_sq_size / 2;
1669 qp_init->cap.max_rdma_ctxs = srp_sq_size / 2;
1670 qp_init->cap.max_send_sge = min(attrs->max_sge, SRPT_MAX_SG_PER_WQE);
1671 qp_init->port_num = ch->sport->port;
1673 ch->qp = ib_create_qp(sdev->pd, qp_init);
1674 if (IS_ERR(ch->qp)) {
1675 ret = PTR_ERR(ch->qp);
1676 if (ret == -ENOMEM) {
1678 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1679 ib_destroy_cq(ch->cq);
1683 pr_err("failed to create_qp ret= %d\n", ret);
1684 goto err_destroy_cq;
1687 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1689 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1690 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1691 qp_init->cap.max_send_wr, ch->cm_id);
1693 ret = srpt_init_ch_qp(ch, ch->qp);
1695 goto err_destroy_qp;
1702 ib_destroy_qp(ch->qp);
1708 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1710 ib_destroy_qp(ch->qp);
1715 * srpt_close_ch() - Close an RDMA channel.
1717 * Make sure all resources associated with the channel will be deallocated at
1718 * an appropriate time.
1720 * Returns true if and only if the channel state has been modified into
1723 static bool srpt_close_ch(struct srpt_rdma_ch *ch)
1727 if (!srpt_set_ch_state(ch, CH_DRAINING)) {
1728 pr_debug("%s: already closed\n", ch->sess_name);
1732 kref_get(&ch->kref);
1734 ret = srpt_ch_qp_err(ch);
1736 pr_err("%s-%d: changing queue pair into error state failed: %d\n",
1737 ch->sess_name, ch->qp->qp_num, ret);
1739 pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
1741 ret = srpt_zerolength_write(ch);
1743 pr_err("%s-%d: queuing zero-length write failed: %d\n",
1744 ch->sess_name, ch->qp->qp_num, ret);
1745 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
1746 schedule_work(&ch->release_work);
1751 kref_put(&ch->kref, srpt_free_ch);
1757 * Change the channel state into CH_DISCONNECTING. If a channel has not yet
1758 * reached the connected state, close it. If a channel is in the connected
1759 * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
1760 * the responsibility of the caller to ensure that this function is not
1761 * invoked concurrently with the code that accepts a connection. This means
1762 * that this function must either be invoked from inside a CM callback
1763 * function or that it must be invoked with the srpt_port.mutex held.
1765 static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
1769 if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
1772 ret = ib_send_cm_dreq(ch->cm_id, NULL, 0);
1774 ret = ib_send_cm_drep(ch->cm_id, NULL, 0);
1776 if (ret < 0 && srpt_close_ch(ch))
1782 static void __srpt_close_all_ch(struct srpt_device *sdev)
1784 struct srpt_rdma_ch *ch;
1786 lockdep_assert_held(&sdev->mutex);
1788 list_for_each_entry(ch, &sdev->rch_list, list) {
1789 if (srpt_disconnect_ch(ch) >= 0)
1790 pr_info("Closing channel %s because target %s has been disabled\n",
1792 sdev->device->name);
1797 static void srpt_free_ch(struct kref *kref)
1799 struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
1804 static void srpt_release_channel_work(struct work_struct *w)
1806 struct srpt_rdma_ch *ch;
1807 struct srpt_device *sdev;
1808 struct se_session *se_sess;
1810 ch = container_of(w, struct srpt_rdma_ch, release_work);
1811 pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name,
1812 ch->qp->qp_num, ch->release_done);
1814 sdev = ch->sport->sdev;
1820 target_sess_cmd_list_set_waiting(se_sess);
1821 target_wait_for_sess_cmds(se_sess);
1823 transport_deregister_session_configfs(se_sess);
1824 transport_deregister_session(se_sess);
1827 ib_destroy_cm_id(ch->cm_id);
1829 srpt_destroy_ch_ib(ch);
1831 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
1832 ch->sport->sdev, ch->rq_size,
1833 ch->rsp_size, DMA_TO_DEVICE);
1835 mutex_lock(&sdev->mutex);
1836 list_del_init(&ch->list);
1837 if (ch->release_done)
1838 complete(ch->release_done);
1839 mutex_unlock(&sdev->mutex);
1841 wake_up(&sdev->ch_releaseQ);
1843 kref_put(&ch->kref, srpt_free_ch);
1847 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
1849 * Ownership of the cm_id is transferred to the target session if this
1850 * functions returns zero. Otherwise the caller remains the owner of cm_id.
1852 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
1853 struct ib_cm_req_event_param *param,
1856 struct srpt_device *sdev = cm_id->context;
1857 struct srpt_port *sport = &sdev->port[param->port - 1];
1858 struct srp_login_req *req;
1859 struct srp_login_rsp *rsp;
1860 struct srp_login_rej *rej;
1861 struct ib_cm_rep_param *rep_param;
1862 struct srpt_rdma_ch *ch, *tmp_ch;
1867 WARN_ON_ONCE(irqs_disabled());
1869 if (WARN_ON(!sdev || !private_data))
1872 req = (struct srp_login_req *)private_data;
1874 it_iu_len = be32_to_cpu(req->req_it_iu_len);
1876 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
1877 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
1878 " (guid=0x%llx:0x%llx)\n",
1879 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
1880 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
1881 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
1882 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
1885 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
1886 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
1888 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
1889 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
1890 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
1892 if (!rsp || !rej || !rep_param) {
1897 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
1898 rej->reason = cpu_to_be32(
1899 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
1901 pr_err("rejected SRP_LOGIN_REQ because its"
1902 " length (%d bytes) is out of range (%d .. %d)\n",
1903 it_iu_len, 64, srp_max_req_size);
1907 if (!sport->enabled) {
1908 rej->reason = cpu_to_be32(
1909 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1911 pr_err("rejected SRP_LOGIN_REQ because the target port"
1912 " has not yet been enabled\n");
1916 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
1917 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
1919 mutex_lock(&sdev->mutex);
1921 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
1922 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
1923 && !memcmp(ch->t_port_id, req->target_port_id, 16)
1924 && param->port == ch->sport->port
1925 && param->listen_id == ch->sport->sdev->cm_id
1927 if (srpt_disconnect_ch(ch) < 0)
1929 pr_info("Relogin - closed existing channel %s\n",
1932 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
1936 mutex_unlock(&sdev->mutex);
1939 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
1941 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
1942 || *(__be64 *)(req->target_port_id + 8) !=
1943 cpu_to_be64(srpt_service_guid)) {
1944 rej->reason = cpu_to_be32(
1945 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
1947 pr_err("rejected SRP_LOGIN_REQ because it"
1948 " has an invalid target port identifier.\n");
1952 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
1954 rej->reason = cpu_to_be32(
1955 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1956 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
1961 kref_init(&ch->kref);
1962 ch->zw_cqe.done = srpt_zerolength_write_done;
1963 INIT_WORK(&ch->release_work, srpt_release_channel_work);
1964 memcpy(ch->i_port_id, req->initiator_port_id, 16);
1965 memcpy(ch->t_port_id, req->target_port_id, 16);
1966 ch->sport = &sdev->port[param->port - 1];
1968 cm_id->context = ch;
1970 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
1971 * for the SRP protocol to the command queue size.
1973 ch->rq_size = SRPT_RQ_SIZE;
1974 spin_lock_init(&ch->spinlock);
1975 ch->state = CH_CONNECTING;
1976 INIT_LIST_HEAD(&ch->cmd_wait_list);
1977 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
1979 ch->ioctx_ring = (struct srpt_send_ioctx **)
1980 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
1981 sizeof(*ch->ioctx_ring[0]),
1982 ch->rsp_size, DMA_TO_DEVICE);
1983 if (!ch->ioctx_ring)
1986 INIT_LIST_HEAD(&ch->free_list);
1987 for (i = 0; i < ch->rq_size; i++) {
1988 ch->ioctx_ring[i]->ch = ch;
1989 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
1992 ret = srpt_create_ch_ib(ch);
1994 rej->reason = cpu_to_be32(
1995 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
1996 pr_err("rejected SRP_LOGIN_REQ because creating"
1997 " a new RDMA channel failed.\n");
2001 ret = srpt_ch_qp_rtr(ch, ch->qp);
2003 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2004 pr_err("rejected SRP_LOGIN_REQ because enabling"
2005 " RTR failed (error code = %d)\n", ret);
2010 * Use the initator port identifier as the session name, when
2011 * checking against se_node_acl->initiatorname[] this can be
2012 * with or without preceeding '0x'.
2014 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2015 be64_to_cpu(*(__be64 *)ch->i_port_id),
2016 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2018 pr_debug("registering session %s\n", ch->sess_name);
2019 p = &ch->sess_name[0];
2022 ch->sess = target_alloc_session(&sport->port_tpg_1, 0, 0,
2023 TARGET_PROT_NORMAL, p, ch, NULL);
2024 if (IS_ERR(ch->sess)) {
2025 pr_info("Rejected login because no ACL has been"
2026 " configured yet for initiator %s.\n", p);
2028 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2030 if (p == &ch->sess_name[0]) {
2034 rej->reason = cpu_to_be32((PTR_ERR(ch->sess) == -ENOMEM) ?
2035 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
2036 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2040 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2041 ch->sess_name, ch->cm_id);
2043 /* create srp_login_response */
2044 rsp->opcode = SRP_LOGIN_RSP;
2045 rsp->tag = req->tag;
2046 rsp->max_it_iu_len = req->req_it_iu_len;
2047 rsp->max_ti_iu_len = req->req_it_iu_len;
2048 ch->max_ti_iu_len = it_iu_len;
2049 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2050 | SRP_BUF_FORMAT_INDIRECT);
2051 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2052 atomic_set(&ch->req_lim, ch->rq_size);
2053 atomic_set(&ch->req_lim_delta, 0);
2055 /* create cm reply */
2056 rep_param->qp_num = ch->qp->qp_num;
2057 rep_param->private_data = (void *)rsp;
2058 rep_param->private_data_len = sizeof(*rsp);
2059 rep_param->rnr_retry_count = 7;
2060 rep_param->flow_control = 1;
2061 rep_param->failover_accepted = 0;
2063 rep_param->responder_resources = 4;
2064 rep_param->initiator_depth = 4;
2066 ret = ib_send_cm_rep(cm_id, rep_param);
2068 pr_err("sending SRP_LOGIN_REQ response failed"
2069 " (error code = %d)\n", ret);
2070 goto release_channel;
2073 mutex_lock(&sdev->mutex);
2074 list_add_tail(&ch->list, &sdev->rch_list);
2075 mutex_unlock(&sdev->mutex);
2080 srpt_disconnect_ch(ch);
2081 transport_deregister_session_configfs(ch->sess);
2082 transport_deregister_session(ch->sess);
2086 srpt_destroy_ch_ib(ch);
2089 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2090 ch->sport->sdev, ch->rq_size,
2091 ch->rsp_size, DMA_TO_DEVICE);
2096 rej->opcode = SRP_LOGIN_REJ;
2097 rej->tag = req->tag;
2098 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2099 | SRP_BUF_FORMAT_INDIRECT);
2101 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2102 (void *)rej, sizeof(*rej));
2112 static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
2113 enum ib_cm_rej_reason reason,
2114 const u8 *private_data,
2115 u8 private_data_len)
2120 if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
2122 for (i = 0; i < private_data_len; i++)
2123 sprintf(priv + 3 * i, " %02x", private_data[i]);
2125 pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
2126 ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
2127 "; private data" : "", priv ? priv : " (?)");
2132 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2134 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2135 * and that the recipient may begin transmitting (RTU = ready to use).
2137 static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
2141 if (srpt_set_ch_state(ch, CH_LIVE)) {
2142 ret = srpt_ch_qp_rts(ch, ch->qp);
2145 /* Trigger wait list processing. */
2146 ret = srpt_zerolength_write(ch);
2147 WARN_ONCE(ret < 0, "%d\n", ret);
2155 * srpt_cm_handler() - IB connection manager callback function.
2157 * A non-zero return value will cause the caller destroy the CM ID.
2159 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2160 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2161 * a non-zero value in any other case will trigger a race with the
2162 * ib_destroy_cm_id() call in srpt_release_channel().
2164 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2166 struct srpt_rdma_ch *ch = cm_id->context;
2170 switch (event->event) {
2171 case IB_CM_REQ_RECEIVED:
2172 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2173 event->private_data);
2175 case IB_CM_REJ_RECEIVED:
2176 srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
2177 event->private_data,
2178 IB_CM_REJ_PRIVATE_DATA_SIZE);
2180 case IB_CM_RTU_RECEIVED:
2181 case IB_CM_USER_ESTABLISHED:
2182 srpt_cm_rtu_recv(ch);
2184 case IB_CM_DREQ_RECEIVED:
2185 srpt_disconnect_ch(ch);
2187 case IB_CM_DREP_RECEIVED:
2188 pr_info("Received CM DREP message for ch %s-%d.\n",
2189 ch->sess_name, ch->qp->qp_num);
2192 case IB_CM_TIMEWAIT_EXIT:
2193 pr_info("Received CM TimeWait exit for ch %s-%d.\n",
2194 ch->sess_name, ch->qp->qp_num);
2197 case IB_CM_REP_ERROR:
2198 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2201 case IB_CM_DREQ_ERROR:
2202 pr_info("Received CM DREQ ERROR event.\n");
2204 case IB_CM_MRA_RECEIVED:
2205 pr_info("Received CM MRA event\n");
2208 pr_err("received unrecognized CM event %d\n", event->event);
2215 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2217 struct srpt_send_ioctx *ioctx;
2219 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2220 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2224 * srpt_write_pending() - Start data transfer from initiator to target (write).
2226 static int srpt_write_pending(struct se_cmd *se_cmd)
2228 struct srpt_send_ioctx *ioctx =
2229 container_of(se_cmd, struct srpt_send_ioctx, cmd);
2230 struct srpt_rdma_ch *ch = ioctx->ch;
2231 struct ib_send_wr *first_wr = NULL, *bad_wr;
2232 struct ib_cqe *cqe = &ioctx->rdma_cqe;
2233 enum srpt_command_state new_state;
2236 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2237 WARN_ON(new_state == SRPT_STATE_DONE);
2239 if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
2240 pr_warn("%s: IB send queue full (needed %d)\n",
2241 __func__, ioctx->n_rdma);
2246 cqe->done = srpt_rdma_read_done;
2247 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2248 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2250 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
2255 ret = ib_post_send(ch->qp, first_wr, &bad_wr);
2257 pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
2258 __func__, ret, ioctx->n_rdma,
2259 atomic_read(&ch->sq_wr_avail));
2265 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
2269 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2271 switch (tcm_mgmt_status) {
2272 case TMR_FUNCTION_COMPLETE:
2273 return SRP_TSK_MGMT_SUCCESS;
2274 case TMR_FUNCTION_REJECTED:
2275 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2277 return SRP_TSK_MGMT_FAILED;
2281 * srpt_queue_response() - Transmits the response to a SCSI command.
2283 * Callback function called by the TCM core. Must not block since it can be
2284 * invoked on the context of the IB completion handler.
2286 static void srpt_queue_response(struct se_cmd *cmd)
2288 struct srpt_send_ioctx *ioctx =
2289 container_of(cmd, struct srpt_send_ioctx, cmd);
2290 struct srpt_rdma_ch *ch = ioctx->ch;
2291 struct srpt_device *sdev = ch->sport->sdev;
2292 struct ib_send_wr send_wr, *first_wr = &send_wr, *bad_wr;
2294 enum srpt_command_state state;
2295 unsigned long flags;
2296 int resp_len, ret, i;
2301 spin_lock_irqsave(&ioctx->spinlock, flags);
2302 state = ioctx->state;
2304 case SRPT_STATE_NEW:
2305 case SRPT_STATE_DATA_IN:
2306 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2308 case SRPT_STATE_MGMT:
2309 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2312 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2313 ch, ioctx->ioctx.index, ioctx->state);
2316 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2318 if (unlikely(WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)))
2321 /* For read commands, transfer the data to the initiator. */
2322 if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
2323 ioctx->cmd.data_length &&
2324 !ioctx->queue_status_only) {
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,
2329 ch->sport->port, NULL, first_wr);
2333 if (state != SRPT_STATE_MGMT)
2334 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
2338 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2339 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
2343 atomic_inc(&ch->req_lim);
2345 if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
2346 &ch->sq_wr_avail) < 0)) {
2347 pr_warn("%s: IB send queue full (needed %d)\n",
2348 __func__, ioctx->n_rdma);
2353 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
2356 sge.addr = ioctx->ioctx.dma;
2357 sge.length = resp_len;
2358 sge.lkey = sdev->pd->local_dma_lkey;
2360 ioctx->ioctx.cqe.done = srpt_send_done;
2361 send_wr.next = NULL;
2362 send_wr.wr_cqe = &ioctx->ioctx.cqe;
2363 send_wr.sg_list = &sge;
2364 send_wr.num_sge = 1;
2365 send_wr.opcode = IB_WR_SEND;
2366 send_wr.send_flags = IB_SEND_SIGNALED;
2368 ret = ib_post_send(ch->qp, first_wr, &bad_wr);
2370 pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
2371 __func__, ioctx->cmd.tag, ret);
2378 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
2379 atomic_dec(&ch->req_lim);
2380 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2381 target_put_sess_cmd(&ioctx->cmd);
2384 static int srpt_queue_data_in(struct se_cmd *cmd)
2386 srpt_queue_response(cmd);
2390 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2392 srpt_queue_response(cmd);
2396 * This function is called for aborted commands if no response is sent to the
2397 * initiator. Make sure that the credits freed by aborting a command are
2398 * returned to the initiator the next time a response is sent by incrementing
2399 * ch->req_lim_delta.
2401 static void srpt_aborted_task(struct se_cmd *cmd)
2403 struct srpt_send_ioctx *ioctx = container_of(cmd,
2404 struct srpt_send_ioctx, cmd);
2405 struct srpt_rdma_ch *ch = ioctx->ch;
2407 atomic_inc(&ch->req_lim_delta);
2410 static int srpt_queue_status(struct se_cmd *cmd)
2412 struct srpt_send_ioctx *ioctx;
2414 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2415 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2416 if (cmd->se_cmd_flags &
2417 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2418 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2419 ioctx->queue_status_only = true;
2420 srpt_queue_response(cmd);
2424 static void srpt_refresh_port_work(struct work_struct *work)
2426 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2428 srpt_refresh_port(sport);
2432 * srpt_release_sdev() - Free the channel resources associated with a target.
2434 static int srpt_release_sdev(struct srpt_device *sdev)
2438 WARN_ON_ONCE(irqs_disabled());
2442 mutex_lock(&sdev->mutex);
2443 for (i = 0; i < ARRAY_SIZE(sdev->port); i++)
2444 sdev->port[i].enabled = false;
2445 __srpt_close_all_ch(sdev);
2446 mutex_unlock(&sdev->mutex);
2448 res = wait_event_interruptible(sdev->ch_releaseQ,
2449 list_empty_careful(&sdev->rch_list));
2451 pr_err("%s: interrupted.\n", __func__);
2456 static struct srpt_port *__srpt_lookup_port(const char *name)
2458 struct ib_device *dev;
2459 struct srpt_device *sdev;
2460 struct srpt_port *sport;
2463 list_for_each_entry(sdev, &srpt_dev_list, list) {
2468 for (i = 0; i < dev->phys_port_cnt; i++) {
2469 sport = &sdev->port[i];
2471 if (!strcmp(sport->port_guid, name))
2479 static struct srpt_port *srpt_lookup_port(const char *name)
2481 struct srpt_port *sport;
2483 spin_lock(&srpt_dev_lock);
2484 sport = __srpt_lookup_port(name);
2485 spin_unlock(&srpt_dev_lock);
2491 * srpt_add_one() - Infiniband device addition callback function.
2493 static void srpt_add_one(struct ib_device *device)
2495 struct srpt_device *sdev;
2496 struct srpt_port *sport;
2497 struct ib_srq_init_attr srq_attr;
2500 pr_debug("device = %p, device->dma_ops = %p\n", device,
2503 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
2507 sdev->device = device;
2508 INIT_LIST_HEAD(&sdev->rch_list);
2509 init_waitqueue_head(&sdev->ch_releaseQ);
2510 mutex_init(&sdev->mutex);
2512 sdev->pd = ib_alloc_pd(device, 0);
2513 if (IS_ERR(sdev->pd))
2516 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
2518 srq_attr.event_handler = srpt_srq_event;
2519 srq_attr.srq_context = (void *)sdev;
2520 srq_attr.attr.max_wr = sdev->srq_size;
2521 srq_attr.attr.max_sge = 1;
2522 srq_attr.attr.srq_limit = 0;
2523 srq_attr.srq_type = IB_SRQT_BASIC;
2525 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2526 if (IS_ERR(sdev->srq))
2529 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
2530 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
2533 if (!srpt_service_guid)
2534 srpt_service_guid = be64_to_cpu(device->node_guid);
2536 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2537 if (IS_ERR(sdev->cm_id))
2540 /* print out target login information */
2541 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2542 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2543 srpt_service_guid, srpt_service_guid);
2546 * We do not have a consistent service_id (ie. also id_ext of target_id)
2547 * to identify this target. We currently use the guid of the first HCA
2548 * in the system as service_id; therefore, the target_id will change
2549 * if this HCA is gone bad and replaced by different HCA
2551 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
2554 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2555 srpt_event_handler);
2556 if (ib_register_event_handler(&sdev->event_handler))
2559 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2560 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2561 sizeof(*sdev->ioctx_ring[0]),
2562 srp_max_req_size, DMA_FROM_DEVICE);
2563 if (!sdev->ioctx_ring)
2566 for (i = 0; i < sdev->srq_size; ++i)
2567 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2569 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
2571 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2572 sport = &sdev->port[i - 1];
2575 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2576 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2577 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2578 INIT_WORK(&sport->work, srpt_refresh_port_work);
2580 if (srpt_refresh_port(sport)) {
2581 pr_err("MAD registration failed for %s-%d.\n",
2582 sdev->device->name, i);
2587 spin_lock(&srpt_dev_lock);
2588 list_add_tail(&sdev->list, &srpt_dev_list);
2589 spin_unlock(&srpt_dev_lock);
2592 ib_set_client_data(device, &srpt_client, sdev);
2593 pr_debug("added %s.\n", device->name);
2597 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2598 sdev->srq_size, srp_max_req_size,
2601 ib_unregister_event_handler(&sdev->event_handler);
2603 ib_destroy_cm_id(sdev->cm_id);
2605 ib_destroy_srq(sdev->srq);
2607 ib_dealloc_pd(sdev->pd);
2612 pr_info("%s(%s) failed.\n", __func__, device->name);
2617 * srpt_remove_one() - InfiniBand device removal callback function.
2619 static void srpt_remove_one(struct ib_device *device, void *client_data)
2621 struct srpt_device *sdev = client_data;
2625 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
2629 srpt_unregister_mad_agent(sdev);
2631 ib_unregister_event_handler(&sdev->event_handler);
2633 /* Cancel any work queued by the just unregistered IB event handler. */
2634 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2635 cancel_work_sync(&sdev->port[i].work);
2637 ib_destroy_cm_id(sdev->cm_id);
2640 * Unregistering a target must happen after destroying sdev->cm_id
2641 * such that no new SRP_LOGIN_REQ information units can arrive while
2642 * destroying the target.
2644 spin_lock(&srpt_dev_lock);
2645 list_del(&sdev->list);
2646 spin_unlock(&srpt_dev_lock);
2647 srpt_release_sdev(sdev);
2649 ib_destroy_srq(sdev->srq);
2650 ib_dealloc_pd(sdev->pd);
2652 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2653 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2654 sdev->ioctx_ring = NULL;
2658 static struct ib_client srpt_client = {
2660 .add = srpt_add_one,
2661 .remove = srpt_remove_one
2664 static int srpt_check_true(struct se_portal_group *se_tpg)
2669 static int srpt_check_false(struct se_portal_group *se_tpg)
2674 static char *srpt_get_fabric_name(void)
2679 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
2681 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
2683 return sport->port_guid;
2686 static u16 srpt_get_tag(struct se_portal_group *tpg)
2691 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
2696 static void srpt_release_cmd(struct se_cmd *se_cmd)
2698 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
2699 struct srpt_send_ioctx, cmd);
2700 struct srpt_rdma_ch *ch = ioctx->ch;
2701 unsigned long flags;
2703 WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
2704 !(ioctx->cmd.transport_state & CMD_T_ABORTED));
2706 if (ioctx->n_rw_ctx) {
2707 srpt_free_rw_ctxs(ch, ioctx);
2708 ioctx->n_rw_ctx = 0;
2711 spin_lock_irqsave(&ch->spinlock, flags);
2712 list_add(&ioctx->free_list, &ch->free_list);
2713 spin_unlock_irqrestore(&ch->spinlock, flags);
2717 * srpt_close_session() - Forcibly close a session.
2719 * Callback function invoked by the TCM core to clean up sessions associated
2720 * with a node ACL when the user invokes
2721 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
2723 static void srpt_close_session(struct se_session *se_sess)
2725 DECLARE_COMPLETION_ONSTACK(release_done);
2726 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2727 struct srpt_device *sdev = ch->sport->sdev;
2730 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
2733 mutex_lock(&sdev->mutex);
2734 BUG_ON(ch->release_done);
2735 ch->release_done = &release_done;
2736 wait = !list_empty(&ch->list);
2737 srpt_disconnect_ch(ch);
2738 mutex_unlock(&sdev->mutex);
2743 while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
2744 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
2745 ch->sess_name, ch->qp->qp_num, ch->state);
2749 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
2751 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
2752 * This object represents an arbitrary integer used to uniquely identify a
2753 * particular attached remote initiator port to a particular SCSI target port
2754 * within a particular SCSI target device within a particular SCSI instance.
2756 static u32 srpt_sess_get_index(struct se_session *se_sess)
2761 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
2765 /* Note: only used from inside debug printk's by the TCM core. */
2766 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
2768 struct srpt_send_ioctx *ioctx;
2770 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2771 return srpt_get_cmd_state(ioctx);
2775 * srpt_parse_i_port_id() - Parse an initiator port ID.
2776 * @name: ASCII representation of a 128-bit initiator port ID.
2777 * @i_port_id: Binary 128-bit port ID.
2779 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
2782 unsigned len, count, leading_zero_bytes;
2786 if (strncasecmp(p, "0x", 2) == 0)
2792 count = min(len / 2, 16U);
2793 leading_zero_bytes = 16 - count;
2794 memset(i_port_id, 0, leading_zero_bytes);
2795 ret = hex2bin(i_port_id + leading_zero_bytes, p, count);
2797 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", ret);
2803 * configfs callback function invoked for
2804 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
2806 static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
2810 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
2811 pr_err("invalid initiator port ID %s\n", name);
2817 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
2820 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2821 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2823 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
2826 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
2827 const char *page, size_t count)
2829 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2830 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2834 ret = kstrtoul(page, 0, &val);
2836 pr_err("kstrtoul() failed with ret: %d\n", ret);
2839 if (val > MAX_SRPT_RDMA_SIZE) {
2840 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
2841 MAX_SRPT_RDMA_SIZE);
2844 if (val < DEFAULT_MAX_RDMA_SIZE) {
2845 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
2846 val, DEFAULT_MAX_RDMA_SIZE);
2849 sport->port_attrib.srp_max_rdma_size = val;
2854 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
2857 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2858 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2860 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
2863 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
2864 const char *page, size_t count)
2866 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2867 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2871 ret = kstrtoul(page, 0, &val);
2873 pr_err("kstrtoul() failed with ret: %d\n", ret);
2876 if (val > MAX_SRPT_RSP_SIZE) {
2877 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
2881 if (val < MIN_MAX_RSP_SIZE) {
2882 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
2886 sport->port_attrib.srp_max_rsp_size = val;
2891 static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
2894 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2895 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2897 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
2900 static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
2901 const char *page, size_t count)
2903 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2904 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2908 ret = kstrtoul(page, 0, &val);
2910 pr_err("kstrtoul() failed with ret: %d\n", ret);
2913 if (val > MAX_SRPT_SRQ_SIZE) {
2914 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
2918 if (val < MIN_SRPT_SRQ_SIZE) {
2919 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
2923 sport->port_attrib.srp_sq_size = val;
2928 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
2929 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
2930 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
2932 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
2933 &srpt_tpg_attrib_attr_srp_max_rdma_size,
2934 &srpt_tpg_attrib_attr_srp_max_rsp_size,
2935 &srpt_tpg_attrib_attr_srp_sq_size,
2939 static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
2941 struct se_portal_group *se_tpg = to_tpg(item);
2942 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2944 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
2947 static ssize_t srpt_tpg_enable_store(struct config_item *item,
2948 const char *page, size_t count)
2950 struct se_portal_group *se_tpg = to_tpg(item);
2951 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
2952 struct srpt_device *sdev = sport->sdev;
2953 struct srpt_rdma_ch *ch;
2957 ret = kstrtoul(page, 0, &tmp);
2959 pr_err("Unable to extract srpt_tpg_store_enable\n");
2963 if ((tmp != 0) && (tmp != 1)) {
2964 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
2967 if (sport->enabled == tmp)
2969 sport->enabled = tmp;
2973 mutex_lock(&sdev->mutex);
2974 list_for_each_entry(ch, &sdev->rch_list, list) {
2975 if (ch->sport == sport) {
2976 pr_debug("%s: ch %p %s-%d\n", __func__, ch,
2977 ch->sess_name, ch->qp->qp_num);
2978 srpt_disconnect_ch(ch);
2982 mutex_unlock(&sdev->mutex);
2988 CONFIGFS_ATTR(srpt_tpg_, enable);
2990 static struct configfs_attribute *srpt_tpg_attrs[] = {
2991 &srpt_tpg_attr_enable,
2996 * configfs callback invoked for
2997 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
2999 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3000 struct config_group *group,
3003 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3006 /* Initialize sport->port_wwn and sport->port_tpg_1 */
3007 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
3009 return ERR_PTR(res);
3011 return &sport->port_tpg_1;
3015 * configfs callback invoked for
3016 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3018 static void srpt_drop_tpg(struct se_portal_group *tpg)
3020 struct srpt_port *sport = container_of(tpg,
3021 struct srpt_port, port_tpg_1);
3023 sport->enabled = false;
3024 core_tpg_deregister(&sport->port_tpg_1);
3028 * configfs callback invoked for
3029 * mkdir /sys/kernel/config/target/$driver/$port
3031 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3032 struct config_group *group,
3035 struct srpt_port *sport;
3038 sport = srpt_lookup_port(name);
3039 pr_debug("make_tport(%s)\n", name);
3044 return &sport->port_wwn;
3047 return ERR_PTR(ret);
3051 * configfs callback invoked for
3052 * rmdir /sys/kernel/config/target/$driver/$port
3054 static void srpt_drop_tport(struct se_wwn *wwn)
3056 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3058 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3061 static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
3063 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3066 CONFIGFS_ATTR_RO(srpt_wwn_, version);
3068 static struct configfs_attribute *srpt_wwn_attrs[] = {
3069 &srpt_wwn_attr_version,
3073 static const struct target_core_fabric_ops srpt_template = {
3074 .module = THIS_MODULE,
3076 .get_fabric_name = srpt_get_fabric_name,
3077 .tpg_get_wwn = srpt_get_fabric_wwn,
3078 .tpg_get_tag = srpt_get_tag,
3079 .tpg_check_demo_mode = srpt_check_false,
3080 .tpg_check_demo_mode_cache = srpt_check_true,
3081 .tpg_check_demo_mode_write_protect = srpt_check_true,
3082 .tpg_check_prod_mode_write_protect = srpt_check_false,
3083 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3084 .release_cmd = srpt_release_cmd,
3085 .check_stop_free = srpt_check_stop_free,
3086 .close_session = srpt_close_session,
3087 .sess_get_index = srpt_sess_get_index,
3088 .sess_get_initiator_sid = NULL,
3089 .write_pending = srpt_write_pending,
3090 .write_pending_status = srpt_write_pending_status,
3091 .set_default_node_attributes = srpt_set_default_node_attrs,
3092 .get_cmd_state = srpt_get_tcm_cmd_state,
3093 .queue_data_in = srpt_queue_data_in,
3094 .queue_status = srpt_queue_status,
3095 .queue_tm_rsp = srpt_queue_tm_rsp,
3096 .aborted_task = srpt_aborted_task,
3098 * Setup function pointers for generic logic in
3099 * target_core_fabric_configfs.c
3101 .fabric_make_wwn = srpt_make_tport,
3102 .fabric_drop_wwn = srpt_drop_tport,
3103 .fabric_make_tpg = srpt_make_tpg,
3104 .fabric_drop_tpg = srpt_drop_tpg,
3105 .fabric_init_nodeacl = srpt_init_nodeacl,
3107 .tfc_wwn_attrs = srpt_wwn_attrs,
3108 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3109 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
3113 * srpt_init_module() - Kernel module initialization.
3115 * Note: Since ib_register_client() registers callback functions, and since at
3116 * least one of these callback functions (srpt_add_one()) calls target core
3117 * functions, this driver must be registered with the target core before
3118 * ib_register_client() is called.
3120 static int __init srpt_init_module(void)
3125 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3126 pr_err("invalid value %d for kernel module parameter"
3127 " srp_max_req_size -- must be at least %d.\n",
3128 srp_max_req_size, MIN_MAX_REQ_SIZE);
3132 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3133 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3134 pr_err("invalid value %d for kernel module parameter"
3135 " srpt_srq_size -- must be in the range [%d..%d].\n",
3136 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3140 ret = target_register_template(&srpt_template);
3144 ret = ib_register_client(&srpt_client);
3146 pr_err("couldn't register IB client\n");
3147 goto out_unregister_target;
3152 out_unregister_target:
3153 target_unregister_template(&srpt_template);
3158 static void __exit srpt_cleanup_module(void)
3160 ib_unregister_client(&srpt_client);
3161 target_unregister_template(&srpt_template);
3164 module_init(srpt_init_module);
3165 module_exit(srpt_cleanup_module);