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/configfs_macros.h>
47 #include <target/target_core_base.h>
48 #include <target/target_core_fabric_configfs.h>
49 #include <target/target_core_fabric.h>
50 #include <target/target_core_configfs.h>
53 /* Name of this kernel module. */
54 #define DRV_NAME "ib_srpt"
55 #define DRV_VERSION "2.0.0"
56 #define DRV_RELDATE "2011-02-14"
58 #define SRPT_ID_STRING "Linux SRP target"
61 #define pr_fmt(fmt) DRV_NAME " " fmt
63 MODULE_AUTHOR("Vu Pham and Bart Van Assche");
64 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
65 "v" DRV_VERSION " (" DRV_RELDATE ")");
66 MODULE_LICENSE("Dual BSD/GPL");
72 static u64 srpt_service_guid;
73 static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
74 static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
76 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
77 module_param(srp_max_req_size, int, 0444);
78 MODULE_PARM_DESC(srp_max_req_size,
79 "Maximum size of SRP request messages in bytes.");
81 static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
82 module_param(srpt_srq_size, int, 0444);
83 MODULE_PARM_DESC(srpt_srq_size,
84 "Shared receive queue (SRQ) size.");
86 static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
88 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
90 module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
92 MODULE_PARM_DESC(srpt_service_guid,
93 "Using this value for ioc_guid, id_ext, and cm_listen_id"
94 " instead of using the node_guid of the first HCA.");
96 static struct ib_client srpt_client;
97 static const struct target_core_fabric_ops srpt_template;
98 static void srpt_release_channel(struct srpt_rdma_ch *ch);
99 static int srpt_queue_status(struct se_cmd *cmd);
102 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
105 enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
108 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
109 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
115 * srpt_sdev_name() - Return the name associated with the HCA.
117 * Examples are ib0, ib1, ...
119 static inline const char *srpt_sdev_name(struct srpt_device *sdev)
121 return sdev->device->name;
124 static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
127 enum rdma_ch_state state;
129 spin_lock_irqsave(&ch->spinlock, flags);
131 spin_unlock_irqrestore(&ch->spinlock, flags);
135 static enum rdma_ch_state
136 srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
139 enum rdma_ch_state prev;
141 spin_lock_irqsave(&ch->spinlock, flags);
143 ch->state = new_state;
144 spin_unlock_irqrestore(&ch->spinlock, flags);
149 * srpt_test_and_set_ch_state() - Test and set the channel state.
151 * Returns true if and only if the channel state has been set to the new state.
154 srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
155 enum rdma_ch_state new)
158 enum rdma_ch_state prev;
160 spin_lock_irqsave(&ch->spinlock, flags);
164 spin_unlock_irqrestore(&ch->spinlock, flags);
169 * srpt_event_handler() - Asynchronous IB event callback function.
171 * Callback function called by the InfiniBand core when an asynchronous IB
172 * event occurs. This callback may occur in interrupt context. See also
173 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
174 * Architecture Specification.
176 static void srpt_event_handler(struct ib_event_handler *handler,
177 struct ib_event *event)
179 struct srpt_device *sdev;
180 struct srpt_port *sport;
182 sdev = ib_get_client_data(event->device, &srpt_client);
183 if (!sdev || sdev->device != event->device)
186 pr_debug("ASYNC event= %d on device= %s\n", event->event,
187 srpt_sdev_name(sdev));
189 switch (event->event) {
190 case IB_EVENT_PORT_ERR:
191 if (event->element.port_num <= sdev->device->phys_port_cnt) {
192 sport = &sdev->port[event->element.port_num - 1];
197 case IB_EVENT_PORT_ACTIVE:
198 case IB_EVENT_LID_CHANGE:
199 case IB_EVENT_PKEY_CHANGE:
200 case IB_EVENT_SM_CHANGE:
201 case IB_EVENT_CLIENT_REREGISTER:
202 case IB_EVENT_GID_CHANGE:
203 /* Refresh port data asynchronously. */
204 if (event->element.port_num <= sdev->device->phys_port_cnt) {
205 sport = &sdev->port[event->element.port_num - 1];
206 if (!sport->lid && !sport->sm_lid)
207 schedule_work(&sport->work);
211 pr_err("received unrecognized IB event %d\n",
218 * srpt_srq_event() - SRQ event callback function.
220 static void srpt_srq_event(struct ib_event *event, void *ctx)
222 pr_info("SRQ event %d\n", event->event);
226 * srpt_qp_event() - QP event callback function.
228 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
230 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
231 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
233 switch (event->event) {
234 case IB_EVENT_COMM_EST:
235 ib_cm_notify(ch->cm_id, event->event);
237 case IB_EVENT_QP_LAST_WQE_REACHED:
238 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
240 srpt_release_channel(ch);
242 pr_debug("%s: state %d - ignored LAST_WQE.\n",
243 ch->sess_name, srpt_get_ch_state(ch));
246 pr_err("received unrecognized IB QP event %d\n", event->event);
252 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
254 * @slot: one-based slot number.
255 * @value: four-bit value.
257 * Copies the lowest four bits of value in element slot of the array of four
258 * bit elements called c_list (controller list). The index slot is one-based.
260 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
267 tmp = c_list[id] & 0xf;
268 c_list[id] = (value << 4) | tmp;
270 tmp = c_list[id] & 0xf0;
271 c_list[id] = (value & 0xf) | tmp;
276 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
278 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
281 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
283 struct ib_class_port_info *cif;
285 cif = (struct ib_class_port_info *)mad->data;
286 memset(cif, 0, sizeof *cif);
287 cif->base_version = 1;
288 cif->class_version = 1;
289 cif->resp_time_value = 20;
291 mad->mad_hdr.status = 0;
295 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
297 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
298 * Specification. See also section B.7, table B.6 in the SRP r16a document.
300 static void srpt_get_iou(struct ib_dm_mad *mad)
302 struct ib_dm_iou_info *ioui;
306 ioui = (struct ib_dm_iou_info *)mad->data;
307 ioui->change_id = __constant_cpu_to_be16(1);
308 ioui->max_controllers = 16;
310 /* set present for slot 1 and empty for the rest */
311 srpt_set_ioc(ioui->controller_list, 1, 1);
312 for (i = 1, slot = 2; i < 16; i++, slot++)
313 srpt_set_ioc(ioui->controller_list, slot, 0);
315 mad->mad_hdr.status = 0;
319 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
321 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
322 * Architecture Specification. See also section B.7, table B.7 in the SRP
325 static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
326 struct ib_dm_mad *mad)
328 struct srpt_device *sdev = sport->sdev;
329 struct ib_dm_ioc_profile *iocp;
331 iocp = (struct ib_dm_ioc_profile *)mad->data;
333 if (!slot || slot > 16) {
335 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
341 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
345 memset(iocp, 0, sizeof *iocp);
346 strcpy(iocp->id_string, SRPT_ID_STRING);
347 iocp->guid = cpu_to_be64(srpt_service_guid);
348 iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
349 iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
350 iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
351 iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
352 iocp->subsys_device_id = 0x0;
353 iocp->io_class = __constant_cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
354 iocp->io_subclass = __constant_cpu_to_be16(SRP_IO_SUBCLASS);
355 iocp->protocol = __constant_cpu_to_be16(SRP_PROTOCOL);
356 iocp->protocol_version = __constant_cpu_to_be16(SRP_PROTOCOL_VERSION);
357 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
358 iocp->rdma_read_depth = 4;
359 iocp->send_size = cpu_to_be32(srp_max_req_size);
360 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
362 iocp->num_svc_entries = 1;
363 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
364 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
366 mad->mad_hdr.status = 0;
370 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
372 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
373 * Specification. See also section B.7, table B.8 in the SRP r16a document.
375 static void srpt_get_svc_entries(u64 ioc_guid,
376 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
378 struct ib_dm_svc_entries *svc_entries;
382 if (!slot || slot > 16) {
384 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
388 if (slot > 2 || lo > hi || hi > 1) {
390 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
394 svc_entries = (struct ib_dm_svc_entries *)mad->data;
395 memset(svc_entries, 0, sizeof *svc_entries);
396 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
397 snprintf(svc_entries->service_entries[0].name,
398 sizeof(svc_entries->service_entries[0].name),
400 SRP_SERVICE_NAME_PREFIX,
403 mad->mad_hdr.status = 0;
407 * srpt_mgmt_method_get() - Process a received management datagram.
408 * @sp: source port through which the MAD has been received.
409 * @rq_mad: received MAD.
410 * @rsp_mad: response MAD.
412 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
413 struct ib_dm_mad *rsp_mad)
419 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
421 case DM_ATTR_CLASS_PORT_INFO:
422 srpt_get_class_port_info(rsp_mad);
424 case DM_ATTR_IOU_INFO:
425 srpt_get_iou(rsp_mad);
427 case DM_ATTR_IOC_PROFILE:
428 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
429 srpt_get_ioc(sp, slot, rsp_mad);
431 case DM_ATTR_SVC_ENTRIES:
432 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
433 hi = (u8) ((slot >> 8) & 0xff);
434 lo = (u8) (slot & 0xff);
435 slot = (u16) ((slot >> 16) & 0xffff);
436 srpt_get_svc_entries(srpt_service_guid,
437 slot, hi, lo, rsp_mad);
440 rsp_mad->mad_hdr.status =
441 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
447 * srpt_mad_send_handler() - Post MAD-send callback function.
449 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
450 struct ib_mad_send_wc *mad_wc)
452 ib_destroy_ah(mad_wc->send_buf->ah);
453 ib_free_send_mad(mad_wc->send_buf);
457 * srpt_mad_recv_handler() - MAD reception callback function.
459 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
460 struct ib_mad_recv_wc *mad_wc)
462 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
464 struct ib_mad_send_buf *rsp;
465 struct ib_dm_mad *dm_mad;
467 if (!mad_wc || !mad_wc->recv_buf.mad)
470 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
471 mad_wc->recv_buf.grh, mad_agent->port_num);
475 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
477 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
478 mad_wc->wc->pkey_index, 0,
479 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
481 IB_MGMT_BASE_VERSION);
488 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
489 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
490 dm_mad->mad_hdr.status = 0;
492 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
493 case IB_MGMT_METHOD_GET:
494 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
496 case IB_MGMT_METHOD_SET:
497 dm_mad->mad_hdr.status =
498 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
501 dm_mad->mad_hdr.status =
502 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
506 if (!ib_post_send_mad(rsp, NULL)) {
507 ib_free_recv_mad(mad_wc);
508 /* will destroy_ah & free_send_mad in send completion */
512 ib_free_send_mad(rsp);
517 ib_free_recv_mad(mad_wc);
521 * srpt_refresh_port() - Configure a HCA port.
523 * Enable InfiniBand management datagram processing, update the cached sm_lid,
524 * lid and gid values, and register a callback function for processing MADs
525 * on the specified port.
527 * Note: It is safe to call this function more than once for the same port.
529 static int srpt_refresh_port(struct srpt_port *sport)
531 struct ib_mad_reg_req reg_req;
532 struct ib_port_modify port_modify;
533 struct ib_port_attr port_attr;
536 memset(&port_modify, 0, sizeof port_modify);
537 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
538 port_modify.clr_port_cap_mask = 0;
540 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
544 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
548 sport->sm_lid = port_attr.sm_lid;
549 sport->lid = port_attr.lid;
551 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
555 if (!sport->mad_agent) {
556 memset(®_req, 0, sizeof reg_req);
557 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
558 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
559 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
560 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
562 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
566 srpt_mad_send_handler,
567 srpt_mad_recv_handler,
569 if (IS_ERR(sport->mad_agent)) {
570 ret = PTR_ERR(sport->mad_agent);
571 sport->mad_agent = NULL;
580 port_modify.set_port_cap_mask = 0;
581 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
582 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
590 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
592 * Note: It is safe to call this function more than once for the same device.
594 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
596 struct ib_port_modify port_modify = {
597 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
599 struct srpt_port *sport;
602 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
603 sport = &sdev->port[i - 1];
604 WARN_ON(sport->port != i);
605 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
606 pr_err("disabling MAD processing failed.\n");
607 if (sport->mad_agent) {
608 ib_unregister_mad_agent(sport->mad_agent);
609 sport->mad_agent = NULL;
615 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
617 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
618 int ioctx_size, int dma_size,
619 enum dma_data_direction dir)
621 struct srpt_ioctx *ioctx;
623 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
627 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
631 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
632 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
646 * srpt_free_ioctx() - Free an SRPT I/O context structure.
648 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
649 int dma_size, enum dma_data_direction dir)
654 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
660 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
661 * @sdev: Device to allocate the I/O context ring for.
662 * @ring_size: Number of elements in the I/O context ring.
663 * @ioctx_size: I/O context size.
664 * @dma_size: DMA buffer size.
665 * @dir: DMA data direction.
667 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
668 int ring_size, int ioctx_size,
669 int dma_size, enum dma_data_direction dir)
671 struct srpt_ioctx **ring;
674 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
675 && ioctx_size != sizeof(struct srpt_send_ioctx));
677 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
680 for (i = 0; i < ring_size; ++i) {
681 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
690 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
698 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
700 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
701 struct srpt_device *sdev, int ring_size,
702 int dma_size, enum dma_data_direction dir)
706 for (i = 0; i < ring_size; ++i)
707 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
712 * srpt_get_cmd_state() - Get the state of a SCSI command.
714 static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
716 enum srpt_command_state state;
721 spin_lock_irqsave(&ioctx->spinlock, flags);
722 state = ioctx->state;
723 spin_unlock_irqrestore(&ioctx->spinlock, flags);
728 * srpt_set_cmd_state() - Set the state of a SCSI command.
730 * Does not modify the state of aborted commands. Returns the previous command
733 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
734 enum srpt_command_state new)
736 enum srpt_command_state previous;
741 spin_lock_irqsave(&ioctx->spinlock, flags);
742 previous = ioctx->state;
743 if (previous != SRPT_STATE_DONE)
745 spin_unlock_irqrestore(&ioctx->spinlock, flags);
751 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
753 * Returns true if and only if the previous command state was equal to 'old'.
755 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
756 enum srpt_command_state old,
757 enum srpt_command_state new)
759 enum srpt_command_state previous;
763 WARN_ON(old == SRPT_STATE_DONE);
764 WARN_ON(new == SRPT_STATE_NEW);
766 spin_lock_irqsave(&ioctx->spinlock, flags);
767 previous = ioctx->state;
770 spin_unlock_irqrestore(&ioctx->spinlock, flags);
771 return previous == old;
775 * srpt_post_recv() - Post an IB receive request.
777 static int srpt_post_recv(struct srpt_device *sdev,
778 struct srpt_recv_ioctx *ioctx)
781 struct ib_recv_wr wr, *bad_wr;
784 wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
786 list.addr = ioctx->ioctx.dma;
787 list.length = srp_max_req_size;
788 list.lkey = sdev->mr->lkey;
794 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
798 * srpt_post_send() - Post an IB send request.
800 * Returns zero upon success and a non-zero value upon failure.
802 static int srpt_post_send(struct srpt_rdma_ch *ch,
803 struct srpt_send_ioctx *ioctx, int len)
806 struct ib_send_wr wr, *bad_wr;
807 struct srpt_device *sdev = ch->sport->sdev;
810 atomic_inc(&ch->req_lim);
813 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
814 pr_warn("IB send queue full (needed 1)\n");
818 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
821 list.addr = ioctx->ioctx.dma;
823 list.lkey = sdev->mr->lkey;
826 wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
829 wr.opcode = IB_WR_SEND;
830 wr.send_flags = IB_SEND_SIGNALED;
832 ret = ib_post_send(ch->qp, &wr, &bad_wr);
836 atomic_inc(&ch->sq_wr_avail);
837 atomic_dec(&ch->req_lim);
843 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
844 * @ioctx: Pointer to the I/O context associated with the request.
845 * @srp_cmd: Pointer to the SRP_CMD request data.
846 * @dir: Pointer to the variable to which the transfer direction will be
848 * @data_len: Pointer to the variable to which the total data length of all
849 * descriptors in the SRP_CMD request will be written.
851 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
853 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
854 * -ENOMEM when memory allocation fails and zero upon success.
856 static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
857 struct srp_cmd *srp_cmd,
858 enum dma_data_direction *dir, u64 *data_len)
860 struct srp_indirect_buf *idb;
861 struct srp_direct_buf *db;
862 unsigned add_cdb_offset;
866 * The pointer computations below will only be compiled correctly
867 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
868 * whether srp_cmd::add_data has been declared as a byte pointer.
870 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
871 && !__same_type(srp_cmd->add_data[0], (u8)0));
880 * The lower four bits of the buffer format field contain the DATA-IN
881 * buffer descriptor format, and the highest four bits contain the
882 * DATA-OUT buffer descriptor format.
885 if (srp_cmd->buf_fmt & 0xf)
886 /* DATA-IN: transfer data from target to initiator (read). */
887 *dir = DMA_FROM_DEVICE;
888 else if (srp_cmd->buf_fmt >> 4)
889 /* DATA-OUT: transfer data from initiator to target (write). */
890 *dir = DMA_TO_DEVICE;
893 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
894 * CDB LENGTH' field are reserved and the size in bytes of this field
895 * is four times the value specified in bits 3..7. Hence the "& ~3".
897 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
898 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
899 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
901 ioctx->rbufs = &ioctx->single_rbuf;
903 db = (struct srp_direct_buf *)(srp_cmd->add_data
905 memcpy(ioctx->rbufs, db, sizeof *db);
906 *data_len = be32_to_cpu(db->len);
907 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
908 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
909 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
912 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
915 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
916 pr_err("received unsupported SRP_CMD request"
917 " type (%u out + %u in != %u / %zu)\n",
918 srp_cmd->data_out_desc_cnt,
919 srp_cmd->data_in_desc_cnt,
920 be32_to_cpu(idb->table_desc.len),
927 if (ioctx->n_rbuf == 1)
928 ioctx->rbufs = &ioctx->single_rbuf;
931 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
940 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
941 *data_len = be32_to_cpu(idb->len);
948 * srpt_init_ch_qp() - Initialize queue pair attributes.
950 * Initialized the attributes of queue pair 'qp' by allowing local write,
951 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
953 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
955 struct ib_qp_attr *attr;
958 attr = kzalloc(sizeof *attr, GFP_KERNEL);
962 attr->qp_state = IB_QPS_INIT;
963 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
964 IB_ACCESS_REMOTE_WRITE;
965 attr->port_num = ch->sport->port;
966 attr->pkey_index = 0;
968 ret = ib_modify_qp(qp, attr,
969 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
977 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
978 * @ch: channel of the queue pair.
979 * @qp: queue pair to change the state of.
981 * Returns zero upon success and a negative value upon failure.
983 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
984 * If this structure ever becomes larger, it might be necessary to allocate
985 * it dynamically instead of on the stack.
987 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
989 struct ib_qp_attr qp_attr;
993 qp_attr.qp_state = IB_QPS_RTR;
994 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
998 qp_attr.max_dest_rd_atomic = 4;
1000 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1007 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1008 * @ch: channel of the queue pair.
1009 * @qp: queue pair to change the state of.
1011 * Returns zero upon success and a negative value upon failure.
1013 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1014 * If this structure ever becomes larger, it might be necessary to allocate
1015 * it dynamically instead of on the stack.
1017 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1019 struct ib_qp_attr qp_attr;
1023 qp_attr.qp_state = IB_QPS_RTS;
1024 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1028 qp_attr.max_rd_atomic = 4;
1030 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1037 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1039 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1041 struct ib_qp_attr qp_attr;
1043 qp_attr.qp_state = IB_QPS_ERR;
1044 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1048 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1050 static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1051 struct srpt_send_ioctx *ioctx)
1053 struct scatterlist *sg;
1054 enum dma_data_direction dir;
1058 BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
1060 while (ioctx->n_rdma)
1061 kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
1063 kfree(ioctx->rdma_ius);
1064 ioctx->rdma_ius = NULL;
1066 if (ioctx->mapped_sg_count) {
1069 dir = ioctx->cmd.data_direction;
1070 BUG_ON(dir == DMA_NONE);
1071 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1072 opposite_dma_dir(dir));
1073 ioctx->mapped_sg_count = 0;
1078 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1080 static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1081 struct srpt_send_ioctx *ioctx)
1083 struct ib_device *dev = ch->sport->sdev->device;
1085 struct scatterlist *sg, *sg_orig;
1087 enum dma_data_direction dir;
1088 struct rdma_iu *riu;
1089 struct srp_direct_buf *db;
1090 dma_addr_t dma_addr;
1102 dir = cmd->data_direction;
1103 BUG_ON(dir == DMA_NONE);
1105 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1106 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
1108 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1109 opposite_dma_dir(dir));
1110 if (unlikely(!count))
1113 ioctx->mapped_sg_count = count;
1115 if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1116 nrdma = ioctx->n_rdma_ius;
1118 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1121 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
1122 if (!ioctx->rdma_ius)
1125 ioctx->n_rdma_ius = nrdma;
1129 tsize = cmd->data_length;
1130 dma_len = ib_sg_dma_len(dev, &sg[0]);
1131 riu = ioctx->rdma_ius;
1134 * For each remote desc - calculate the #ib_sge.
1135 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1136 * each remote desc rdma_iu is required a rdma wr;
1138 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1142 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1143 rsize = be32_to_cpu(db->len);
1144 raddr = be64_to_cpu(db->va);
1146 riu->rkey = be32_to_cpu(db->key);
1149 /* calculate how many sge required for this remote_buf */
1150 while (rsize > 0 && tsize > 0) {
1152 if (rsize >= dma_len) {
1161 dma_len = ib_sg_dma_len(
1173 if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1176 kmalloc(riu->sge_cnt * sizeof *riu->sge,
1184 riu->rkey = be32_to_cpu(db->key);
1189 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1196 tsize = cmd->data_length;
1197 riu = ioctx->rdma_ius;
1199 dma_len = ib_sg_dma_len(dev, &sg[0]);
1200 dma_addr = ib_sg_dma_address(dev, &sg[0]);
1202 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1204 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1205 rsize = be32_to_cpu(db->len);
1209 while (rsize > 0 && tsize > 0) {
1210 sge->addr = dma_addr;
1211 sge->lkey = ch->sport->sdev->mr->lkey;
1213 if (rsize >= dma_len) {
1215 (tsize < dma_len) ? tsize : dma_len;
1223 dma_len = ib_sg_dma_len(
1225 dma_addr = ib_sg_dma_address(
1230 sge->length = (tsize < rsize) ? tsize : rsize;
1238 if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1242 } else if (rsize > 0 && tsize > 0)
1250 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1256 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1258 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1260 struct srpt_send_ioctx *ioctx;
1261 unsigned long flags;
1266 spin_lock_irqsave(&ch->spinlock, flags);
1267 if (!list_empty(&ch->free_list)) {
1268 ioctx = list_first_entry(&ch->free_list,
1269 struct srpt_send_ioctx, free_list);
1270 list_del(&ioctx->free_list);
1272 spin_unlock_irqrestore(&ch->spinlock, flags);
1277 BUG_ON(ioctx->ch != ch);
1278 spin_lock_init(&ioctx->spinlock);
1279 ioctx->state = SRPT_STATE_NEW;
1281 ioctx->rbufs = NULL;
1283 ioctx->n_rdma_ius = 0;
1284 ioctx->rdma_ius = NULL;
1285 ioctx->mapped_sg_count = 0;
1286 init_completion(&ioctx->tx_done);
1287 ioctx->queue_status_only = false;
1289 * transport_init_se_cmd() does not initialize all fields, so do it
1292 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1293 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1299 * srpt_abort_cmd() - Abort a SCSI command.
1300 * @ioctx: I/O context associated with the SCSI command.
1301 * @context: Preferred execution context.
1303 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1305 enum srpt_command_state state;
1306 unsigned long flags;
1311 * If the command is in a state where the target core is waiting for
1312 * the ib_srpt driver, change the state to the next state. Changing
1313 * the state of the command from SRPT_STATE_NEED_DATA to
1314 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1315 * function a second time.
1318 spin_lock_irqsave(&ioctx->spinlock, flags);
1319 state = ioctx->state;
1321 case SRPT_STATE_NEED_DATA:
1322 ioctx->state = SRPT_STATE_DATA_IN;
1324 case SRPT_STATE_DATA_IN:
1325 case SRPT_STATE_CMD_RSP_SENT:
1326 case SRPT_STATE_MGMT_RSP_SENT:
1327 ioctx->state = SRPT_STATE_DONE;
1332 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1334 if (state == SRPT_STATE_DONE) {
1335 struct srpt_rdma_ch *ch = ioctx->ch;
1337 BUG_ON(ch->sess == NULL);
1339 target_put_sess_cmd(ch->sess, &ioctx->cmd);
1343 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1347 case SRPT_STATE_NEW:
1348 case SRPT_STATE_DATA_IN:
1349 case SRPT_STATE_MGMT:
1351 * Do nothing - defer abort processing until
1352 * srpt_queue_response() is invoked.
1354 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1356 case SRPT_STATE_NEED_DATA:
1357 /* DMA_TO_DEVICE (write) - RDMA read error. */
1359 /* XXX(hch): this is a horrible layering violation.. */
1360 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1361 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
1362 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
1364 case SRPT_STATE_CMD_RSP_SENT:
1366 * SRP_RSP sending failed or the SRP_RSP send completion has
1367 * not been received in time.
1369 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
1370 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
1372 case SRPT_STATE_MGMT_RSP_SENT:
1373 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1374 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
1377 WARN(1, "Unexpected command state (%d)", state);
1386 * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1388 static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1390 struct srpt_send_ioctx *ioctx;
1391 enum srpt_command_state state;
1395 atomic_inc(&ch->sq_wr_avail);
1397 index = idx_from_wr_id(wr_id);
1398 ioctx = ch->ioctx_ring[index];
1399 state = srpt_get_cmd_state(ioctx);
1402 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1403 && state != SRPT_STATE_MGMT_RSP_SENT
1404 && state != SRPT_STATE_NEED_DATA
1405 && state != SRPT_STATE_DONE);
1407 /* If SRP_RSP sending failed, undo the ch->req_lim change. */
1408 if (state == SRPT_STATE_CMD_RSP_SENT
1409 || state == SRPT_STATE_MGMT_RSP_SENT)
1410 atomic_dec(&ch->req_lim);
1412 srpt_abort_cmd(ioctx);
1416 * srpt_handle_send_comp() - Process an IB send completion notification.
1418 static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1419 struct srpt_send_ioctx *ioctx)
1421 enum srpt_command_state state;
1423 atomic_inc(&ch->sq_wr_avail);
1425 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1427 if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1428 && state != SRPT_STATE_MGMT_RSP_SENT
1429 && state != SRPT_STATE_DONE))
1430 pr_debug("state = %d\n", state);
1432 if (state != SRPT_STATE_DONE) {
1433 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1434 transport_generic_free_cmd(&ioctx->cmd, 0);
1436 pr_err("IB completion has been received too late for"
1437 " wr_id = %u.\n", ioctx->ioctx.index);
1442 * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1444 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1445 * the data that has been transferred via IB RDMA had to be postponed until the
1446 * check_stop_free() callback. None of this is necessary anymore and needs to
1449 static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1450 struct srpt_send_ioctx *ioctx,
1451 enum srpt_opcode opcode)
1453 WARN_ON(ioctx->n_rdma <= 0);
1454 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1456 if (opcode == SRPT_RDMA_READ_LAST) {
1457 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1458 SRPT_STATE_DATA_IN))
1459 target_execute_cmd(&ioctx->cmd);
1461 pr_err("%s[%d]: wrong state = %d\n", __func__,
1462 __LINE__, srpt_get_cmd_state(ioctx));
1463 } else if (opcode == SRPT_RDMA_ABORT) {
1464 ioctx->rdma_aborted = true;
1466 WARN(true, "unexpected opcode %d\n", opcode);
1471 * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1473 static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1474 struct srpt_send_ioctx *ioctx,
1475 enum srpt_opcode opcode)
1478 enum srpt_command_state state;
1481 state = srpt_get_cmd_state(ioctx);
1483 case SRPT_RDMA_READ_LAST:
1484 if (ioctx->n_rdma <= 0) {
1485 pr_err("Received invalid RDMA read"
1486 " error completion with idx %d\n",
1487 ioctx->ioctx.index);
1490 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1491 if (state == SRPT_STATE_NEED_DATA)
1492 srpt_abort_cmd(ioctx);
1494 pr_err("%s[%d]: wrong state = %d\n",
1495 __func__, __LINE__, state);
1497 case SRPT_RDMA_WRITE_LAST:
1500 pr_err("%s[%d]: opcode = %u\n", __func__, __LINE__, opcode);
1506 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1507 * @ch: RDMA channel through which the request has been received.
1508 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1509 * be built in the buffer ioctx->buf points at and hence this function will
1510 * overwrite the request data.
1511 * @tag: tag of the request for which this response is being generated.
1512 * @status: value for the STATUS field of the SRP_RSP information unit.
1514 * Returns the size in bytes of the SRP_RSP response.
1516 * An SRP_RSP response contains a SCSI status or service response. See also
1517 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1518 * response. See also SPC-2 for more information about sense data.
1520 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1521 struct srpt_send_ioctx *ioctx, u64 tag,
1524 struct srp_rsp *srp_rsp;
1525 const u8 *sense_data;
1526 int sense_data_len, max_sense_len;
1529 * The lowest bit of all SAM-3 status codes is zero (see also
1530 * paragraph 5.3 in SAM-3).
1532 WARN_ON(status & 1);
1534 srp_rsp = ioctx->ioctx.buf;
1537 sense_data = ioctx->sense_data;
1538 sense_data_len = ioctx->cmd.scsi_sense_length;
1539 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1541 memset(srp_rsp, 0, sizeof *srp_rsp);
1542 srp_rsp->opcode = SRP_RSP;
1543 srp_rsp->req_lim_delta =
1544 __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1546 srp_rsp->status = status;
1548 if (sense_data_len) {
1549 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1550 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1551 if (sense_data_len > max_sense_len) {
1552 pr_warn("truncated sense data from %d to %d"
1553 " bytes\n", sense_data_len, max_sense_len);
1554 sense_data_len = max_sense_len;
1557 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1558 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1559 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1562 return sizeof(*srp_rsp) + sense_data_len;
1566 * srpt_build_tskmgmt_rsp() - Build a task management response.
1567 * @ch: RDMA channel through which the request has been received.
1568 * @ioctx: I/O context in which the SRP_RSP response will be built.
1569 * @rsp_code: RSP_CODE that will be stored in the response.
1570 * @tag: Tag of the request for which this response is being generated.
1572 * Returns the size in bytes of the SRP_RSP response.
1574 * An SRP_RSP response contains a SCSI status or service response. See also
1575 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1578 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1579 struct srpt_send_ioctx *ioctx,
1580 u8 rsp_code, u64 tag)
1582 struct srp_rsp *srp_rsp;
1587 resp_len = sizeof(*srp_rsp) + resp_data_len;
1589 srp_rsp = ioctx->ioctx.buf;
1591 memset(srp_rsp, 0, sizeof *srp_rsp);
1593 srp_rsp->opcode = SRP_RSP;
1594 srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
1595 + atomic_xchg(&ch->req_lim_delta, 0));
1598 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1599 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1600 srp_rsp->data[3] = rsp_code;
1605 #define NO_SUCH_LUN ((uint64_t)-1LL)
1608 * SCSI LUN addressing method. See also SAM-2 and the section about
1611 enum scsi_lun_addr_method {
1612 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1613 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1614 SCSI_LUN_ADDR_METHOD_LUN = 2,
1615 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1619 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1621 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1622 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1623 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1625 static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1627 uint64_t res = NO_SUCH_LUN;
1628 int addressing_method;
1630 if (unlikely(len < 2)) {
1631 pr_err("Illegal LUN length %d, expected 2 bytes or more\n",
1638 if ((*((__be64 *)lun) &
1639 __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1643 if (*((__be16 *)&lun[2]) != 0)
1647 if (*((__be32 *)&lun[2]) != 0)
1656 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1657 switch (addressing_method) {
1658 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1659 case SCSI_LUN_ADDR_METHOD_FLAT:
1660 case SCSI_LUN_ADDR_METHOD_LUN:
1661 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1664 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1666 pr_err("Unimplemented LUN addressing method %u\n",
1675 pr_err("Support for multi-level LUNs has not yet been implemented\n");
1679 static int srpt_check_stop_free(struct se_cmd *cmd)
1681 struct srpt_send_ioctx *ioctx = container_of(cmd,
1682 struct srpt_send_ioctx, cmd);
1684 return target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
1688 * srpt_handle_cmd() - Process SRP_CMD.
1690 static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1691 struct srpt_recv_ioctx *recv_ioctx,
1692 struct srpt_send_ioctx *send_ioctx)
1695 struct srp_cmd *srp_cmd;
1696 uint64_t unpacked_lun;
1698 enum dma_data_direction dir;
1702 BUG_ON(!send_ioctx);
1704 srp_cmd = recv_ioctx->ioctx.buf;
1705 cmd = &send_ioctx->cmd;
1706 send_ioctx->tag = srp_cmd->tag;
1708 switch (srp_cmd->task_attr) {
1709 case SRP_CMD_SIMPLE_Q:
1710 cmd->sam_task_attr = TCM_SIMPLE_TAG;
1712 case SRP_CMD_ORDERED_Q:
1714 cmd->sam_task_attr = TCM_ORDERED_TAG;
1716 case SRP_CMD_HEAD_OF_Q:
1717 cmd->sam_task_attr = TCM_HEAD_TAG;
1720 cmd->sam_task_attr = TCM_ACA_TAG;
1724 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
1725 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1727 ret = TCM_INVALID_CDB_FIELD;
1731 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1732 sizeof(srp_cmd->lun));
1733 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1734 &send_ioctx->sense_data[0], unpacked_lun, data_len,
1735 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
1737 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1743 transport_send_check_condition_and_sense(cmd, ret, 0);
1748 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1749 * @ch: RDMA channel of the task management request.
1750 * @fn: Task management function to perform.
1751 * @req_tag: Tag of the SRP task management request.
1752 * @mgmt_ioctx: I/O context of the task management request.
1754 * Returns zero if the target core will process the task management
1755 * request asynchronously.
1757 * Note: It is assumed that the initiator serializes tag-based task management
1760 static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1762 struct srpt_device *sdev;
1763 struct srpt_rdma_ch *ch;
1764 struct srpt_send_ioctx *target;
1771 sdev = ch->sport->sdev;
1773 spin_lock_irq(&sdev->spinlock);
1774 for (i = 0; i < ch->rq_size; ++i) {
1775 target = ch->ioctx_ring[i];
1776 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
1777 target->tag == tag &&
1778 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1780 /* now let the target core abort &target->cmd; */
1784 spin_unlock_irq(&sdev->spinlock);
1788 static int srp_tmr_to_tcm(int fn)
1791 case SRP_TSK_ABORT_TASK:
1792 return TMR_ABORT_TASK;
1793 case SRP_TSK_ABORT_TASK_SET:
1794 return TMR_ABORT_TASK_SET;
1795 case SRP_TSK_CLEAR_TASK_SET:
1796 return TMR_CLEAR_TASK_SET;
1797 case SRP_TSK_LUN_RESET:
1798 return TMR_LUN_RESET;
1799 case SRP_TSK_CLEAR_ACA:
1800 return TMR_CLEAR_ACA;
1807 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1809 * Returns 0 if and only if the request will be processed by the target core.
1811 * For more information about SRP_TSK_MGMT information units, see also section
1812 * 6.7 in the SRP r16a document.
1814 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1815 struct srpt_recv_ioctx *recv_ioctx,
1816 struct srpt_send_ioctx *send_ioctx)
1818 struct srp_tsk_mgmt *srp_tsk;
1820 struct se_session *sess = ch->sess;
1821 uint64_t unpacked_lun;
1826 BUG_ON(!send_ioctx);
1828 srp_tsk = recv_ioctx->ioctx.buf;
1829 cmd = &send_ioctx->cmd;
1831 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1832 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1833 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1835 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1836 send_ioctx->tag = srp_tsk->tag;
1837 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1839 send_ioctx->cmd.se_tmr_req->response =
1840 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
1843 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1844 sizeof(srp_tsk->lun));
1846 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK) {
1847 rc = srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1849 send_ioctx->cmd.se_tmr_req->response =
1850 TMR_TASK_DOES_NOT_EXIST;
1853 tag = srp_tsk->task_tag;
1855 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
1856 srp_tsk, tcm_tmr, GFP_KERNEL, tag,
1857 TARGET_SCF_ACK_KREF);
1859 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1864 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
1868 * srpt_handle_new_iu() - Process a newly received information unit.
1869 * @ch: RDMA channel through which the information unit has been received.
1870 * @ioctx: SRPT I/O context associated with the information unit.
1872 static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1873 struct srpt_recv_ioctx *recv_ioctx,
1874 struct srpt_send_ioctx *send_ioctx)
1876 struct srp_cmd *srp_cmd;
1877 enum rdma_ch_state ch_state;
1880 BUG_ON(!recv_ioctx);
1882 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1883 recv_ioctx->ioctx.dma, srp_max_req_size,
1886 ch_state = srpt_get_ch_state(ch);
1887 if (unlikely(ch_state == CH_CONNECTING)) {
1888 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1892 if (unlikely(ch_state != CH_LIVE))
1895 srp_cmd = recv_ioctx->ioctx.buf;
1896 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1898 send_ioctx = srpt_get_send_ioctx(ch);
1899 if (unlikely(!send_ioctx)) {
1900 list_add_tail(&recv_ioctx->wait_list,
1901 &ch->cmd_wait_list);
1906 switch (srp_cmd->opcode) {
1908 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1911 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1914 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
1917 pr_debug("received SRP_CRED_RSP\n");
1920 pr_debug("received SRP_AER_RSP\n");
1923 pr_err("Received SRP_RSP\n");
1926 pr_err("received IU with unknown opcode 0x%x\n",
1931 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1936 static void srpt_process_rcv_completion(struct ib_cq *cq,
1937 struct srpt_rdma_ch *ch,
1940 struct srpt_device *sdev = ch->sport->sdev;
1941 struct srpt_recv_ioctx *ioctx;
1944 index = idx_from_wr_id(wc->wr_id);
1945 if (wc->status == IB_WC_SUCCESS) {
1948 req_lim = atomic_dec_return(&ch->req_lim);
1949 if (unlikely(req_lim < 0))
1950 pr_err("req_lim = %d < 0\n", req_lim);
1951 ioctx = sdev->ioctx_ring[index];
1952 srpt_handle_new_iu(ch, ioctx, NULL);
1954 pr_info("receiving failed for idx %u with status %d\n",
1960 * srpt_process_send_completion() - Process an IB send completion.
1962 * Note: Although this has not yet been observed during tests, at least in
1963 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1964 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1965 * value in each response is set to one, and it is possible that this response
1966 * makes the initiator send a new request before the send completion for that
1967 * response has been processed. This could e.g. happen if the call to
1968 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1969 * if IB retransmission causes generation of the send completion to be
1970 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1971 * are queued on cmd_wait_list. The code below processes these delayed
1972 * requests one at a time.
1974 static void srpt_process_send_completion(struct ib_cq *cq,
1975 struct srpt_rdma_ch *ch,
1978 struct srpt_send_ioctx *send_ioctx;
1980 enum srpt_opcode opcode;
1982 index = idx_from_wr_id(wc->wr_id);
1983 opcode = opcode_from_wr_id(wc->wr_id);
1984 send_ioctx = ch->ioctx_ring[index];
1985 if (wc->status == IB_WC_SUCCESS) {
1986 if (opcode == SRPT_SEND)
1987 srpt_handle_send_comp(ch, send_ioctx);
1989 WARN_ON(opcode != SRPT_RDMA_ABORT &&
1990 wc->opcode != IB_WC_RDMA_READ);
1991 srpt_handle_rdma_comp(ch, send_ioctx, opcode);
1994 if (opcode == SRPT_SEND) {
1995 pr_info("sending response for idx %u failed"
1996 " with status %d\n", index, wc->status);
1997 srpt_handle_send_err_comp(ch, wc->wr_id);
1998 } else if (opcode != SRPT_RDMA_MID) {
1999 pr_info("RDMA t %d for idx %u failed with"
2000 " status %d\n", opcode, index, wc->status);
2001 srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
2005 while (unlikely(opcode == SRPT_SEND
2006 && !list_empty(&ch->cmd_wait_list)
2007 && srpt_get_ch_state(ch) == CH_LIVE
2008 && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
2009 struct srpt_recv_ioctx *recv_ioctx;
2011 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
2012 struct srpt_recv_ioctx,
2014 list_del(&recv_ioctx->wait_list);
2015 srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
2019 static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
2021 struct ib_wc *const wc = ch->wc;
2024 WARN_ON(cq != ch->cq);
2026 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
2027 while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
2028 for (i = 0; i < n; i++) {
2029 if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
2030 srpt_process_rcv_completion(cq, ch, &wc[i]);
2032 srpt_process_send_completion(cq, ch, &wc[i]);
2038 * srpt_completion() - IB completion queue callback function.
2041 * - It is guaranteed that a completion handler will never be invoked
2042 * concurrently on two different CPUs for the same completion queue. See also
2043 * Documentation/infiniband/core_locking.txt and the implementation of
2044 * handle_edge_irq() in kernel/irq/chip.c.
2045 * - When threaded IRQs are enabled, completion handlers are invoked in thread
2046 * context instead of interrupt context.
2048 static void srpt_completion(struct ib_cq *cq, void *ctx)
2050 struct srpt_rdma_ch *ch = ctx;
2052 wake_up_interruptible(&ch->wait_queue);
2055 static int srpt_compl_thread(void *arg)
2057 struct srpt_rdma_ch *ch;
2059 /* Hibernation / freezing of the SRPT kernel thread is not supported. */
2060 current->flags |= PF_NOFREEZE;
2064 pr_info("Session %s: kernel thread %s (PID %d) started\n",
2065 ch->sess_name, ch->thread->comm, current->pid);
2066 while (!kthread_should_stop()) {
2067 wait_event_interruptible(ch->wait_queue,
2068 (srpt_process_completion(ch->cq, ch),
2069 kthread_should_stop()));
2071 pr_info("Session %s: kernel thread %s (PID %d) stopped\n",
2072 ch->sess_name, ch->thread->comm, current->pid);
2077 * srpt_create_ch_ib() - Create receive and send completion queues.
2079 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2081 struct ib_qp_init_attr *qp_init;
2082 struct srpt_port *sport = ch->sport;
2083 struct srpt_device *sdev = sport->sdev;
2084 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
2085 struct ib_cq_init_attr cq_attr = {};
2088 WARN_ON(ch->rq_size < 1);
2091 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2096 cq_attr.cqe = ch->rq_size + srp_sq_size;
2097 ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
2099 if (IS_ERR(ch->cq)) {
2100 ret = PTR_ERR(ch->cq);
2101 pr_err("failed to create CQ cqe= %d ret= %d\n",
2102 ch->rq_size + srp_sq_size, ret);
2106 qp_init->qp_context = (void *)ch;
2107 qp_init->event_handler
2108 = (void(*)(struct ib_event *, void*))srpt_qp_event;
2109 qp_init->send_cq = ch->cq;
2110 qp_init->recv_cq = ch->cq;
2111 qp_init->srq = sdev->srq;
2112 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2113 qp_init->qp_type = IB_QPT_RC;
2114 qp_init->cap.max_send_wr = srp_sq_size;
2115 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2117 ch->qp = ib_create_qp(sdev->pd, qp_init);
2118 if (IS_ERR(ch->qp)) {
2119 ret = PTR_ERR(ch->qp);
2120 if (ret == -ENOMEM) {
2122 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
2123 ib_destroy_cq(ch->cq);
2127 pr_err("failed to create_qp ret= %d\n", ret);
2128 goto err_destroy_cq;
2131 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2133 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2134 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2135 qp_init->cap.max_send_wr, ch->cm_id);
2137 ret = srpt_init_ch_qp(ch, ch->qp);
2139 goto err_destroy_qp;
2141 init_waitqueue_head(&ch->wait_queue);
2143 pr_debug("creating thread for session %s\n", ch->sess_name);
2145 ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2146 if (IS_ERR(ch->thread)) {
2147 pr_err("failed to create kernel thread %ld\n",
2148 PTR_ERR(ch->thread));
2150 goto err_destroy_qp;
2158 ib_destroy_qp(ch->qp);
2160 ib_destroy_cq(ch->cq);
2164 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2167 kthread_stop(ch->thread);
2169 ib_destroy_qp(ch->qp);
2170 ib_destroy_cq(ch->cq);
2174 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2176 * Reset the QP and make sure all resources associated with the channel will
2177 * be deallocated at an appropriate time.
2179 * Note: The caller must hold ch->sport->sdev->spinlock.
2181 static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2183 struct srpt_device *sdev;
2184 enum rdma_ch_state prev_state;
2185 unsigned long flags;
2187 sdev = ch->sport->sdev;
2189 spin_lock_irqsave(&ch->spinlock, flags);
2190 prev_state = ch->state;
2191 switch (prev_state) {
2194 ch->state = CH_DISCONNECTING;
2199 spin_unlock_irqrestore(&ch->spinlock, flags);
2201 switch (prev_state) {
2203 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2207 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
2208 pr_err("sending CM DREQ failed.\n");
2210 case CH_DISCONNECTING:
2219 * srpt_close_ch() - Close an RDMA channel.
2221 static void srpt_close_ch(struct srpt_rdma_ch *ch)
2223 struct srpt_device *sdev;
2225 sdev = ch->sport->sdev;
2226 spin_lock_irq(&sdev->spinlock);
2227 __srpt_close_ch(ch);
2228 spin_unlock_irq(&sdev->spinlock);
2232 * srpt_shutdown_session() - Whether or not a session may be shut down.
2234 static int srpt_shutdown_session(struct se_session *se_sess)
2236 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2237 unsigned long flags;
2239 spin_lock_irqsave(&ch->spinlock, flags);
2240 if (ch->in_shutdown) {
2241 spin_unlock_irqrestore(&ch->spinlock, flags);
2245 ch->in_shutdown = true;
2246 target_sess_cmd_list_set_waiting(se_sess);
2247 spin_unlock_irqrestore(&ch->spinlock, flags);
2253 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2254 * @cm_id: Pointer to the CM ID of the channel to be drained.
2256 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2257 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2258 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2259 * waits until all target sessions for the associated IB device have been
2260 * unregistered and target session registration involves a call to
2261 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2262 * this function has finished).
2264 static void srpt_drain_channel(struct ib_cm_id *cm_id)
2266 struct srpt_device *sdev;
2267 struct srpt_rdma_ch *ch;
2269 bool do_reset = false;
2271 WARN_ON_ONCE(irqs_disabled());
2273 sdev = cm_id->context;
2275 spin_lock_irq(&sdev->spinlock);
2276 list_for_each_entry(ch, &sdev->rch_list, list) {
2277 if (ch->cm_id == cm_id) {
2278 do_reset = srpt_test_and_set_ch_state(ch,
2279 CH_CONNECTING, CH_DRAINING) ||
2280 srpt_test_and_set_ch_state(ch,
2281 CH_LIVE, CH_DRAINING) ||
2282 srpt_test_and_set_ch_state(ch,
2283 CH_DISCONNECTING, CH_DRAINING);
2287 spin_unlock_irq(&sdev->spinlock);
2291 srpt_shutdown_session(ch->sess);
2293 ret = srpt_ch_qp_err(ch);
2295 pr_err("Setting queue pair in error state"
2296 " failed: %d\n", ret);
2301 * srpt_find_channel() - Look up an RDMA channel.
2302 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2304 * Return NULL if no matching RDMA channel has been found.
2306 static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2307 struct ib_cm_id *cm_id)
2309 struct srpt_rdma_ch *ch;
2312 WARN_ON_ONCE(irqs_disabled());
2316 spin_lock_irq(&sdev->spinlock);
2317 list_for_each_entry(ch, &sdev->rch_list, list) {
2318 if (ch->cm_id == cm_id) {
2323 spin_unlock_irq(&sdev->spinlock);
2325 return found ? ch : NULL;
2329 * srpt_release_channel() - Release channel resources.
2331 * Schedules the actual release because:
2332 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2333 * trigger a deadlock.
2334 * - It is not safe to call TCM transport_* functions from interrupt context.
2336 static void srpt_release_channel(struct srpt_rdma_ch *ch)
2338 schedule_work(&ch->release_work);
2341 static void srpt_release_channel_work(struct work_struct *w)
2343 struct srpt_rdma_ch *ch;
2344 struct srpt_device *sdev;
2345 struct se_session *se_sess;
2347 ch = container_of(w, struct srpt_rdma_ch, release_work);
2348 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2351 sdev = ch->sport->sdev;
2357 target_wait_for_sess_cmds(se_sess);
2359 transport_deregister_session_configfs(se_sess);
2360 transport_deregister_session(se_sess);
2363 ib_destroy_cm_id(ch->cm_id);
2365 srpt_destroy_ch_ib(ch);
2367 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2368 ch->sport->sdev, ch->rq_size,
2369 ch->rsp_size, DMA_TO_DEVICE);
2371 spin_lock_irq(&sdev->spinlock);
2372 list_del(&ch->list);
2373 spin_unlock_irq(&sdev->spinlock);
2375 if (ch->release_done)
2376 complete(ch->release_done);
2378 wake_up(&sdev->ch_releaseQ);
2383 static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2386 struct srpt_node_acl *nacl;
2388 list_for_each_entry(nacl, &sport->port_acl_list, list)
2389 if (memcmp(nacl->i_port_id, i_port_id,
2390 sizeof(nacl->i_port_id)) == 0)
2396 static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2399 struct srpt_node_acl *nacl;
2401 spin_lock_irq(&sport->port_acl_lock);
2402 nacl = __srpt_lookup_acl(sport, i_port_id);
2403 spin_unlock_irq(&sport->port_acl_lock);
2409 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2411 * Ownership of the cm_id is transferred to the target session if this
2412 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2414 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2415 struct ib_cm_req_event_param *param,
2418 struct srpt_device *sdev = cm_id->context;
2419 struct srpt_port *sport = &sdev->port[param->port - 1];
2420 struct srp_login_req *req;
2421 struct srp_login_rsp *rsp;
2422 struct srp_login_rej *rej;
2423 struct ib_cm_rep_param *rep_param;
2424 struct srpt_rdma_ch *ch, *tmp_ch;
2425 struct srpt_node_acl *nacl;
2430 WARN_ON_ONCE(irqs_disabled());
2432 if (WARN_ON(!sdev || !private_data))
2435 req = (struct srp_login_req *)private_data;
2437 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2439 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2440 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2441 " (guid=0x%llx:0x%llx)\n",
2442 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2443 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2444 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2445 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2448 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2449 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
2451 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2452 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2453 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2455 if (!rsp || !rej || !rep_param) {
2460 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2461 rej->reason = __constant_cpu_to_be32(
2462 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2464 pr_err("rejected SRP_LOGIN_REQ because its"
2465 " length (%d bytes) is out of range (%d .. %d)\n",
2466 it_iu_len, 64, srp_max_req_size);
2470 if (!sport->enabled) {
2471 rej->reason = __constant_cpu_to_be32(
2472 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2474 pr_err("rejected SRP_LOGIN_REQ because the target port"
2475 " has not yet been enabled\n");
2479 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2480 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2482 spin_lock_irq(&sdev->spinlock);
2484 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2485 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2486 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2487 && param->port == ch->sport->port
2488 && param->listen_id == ch->sport->sdev->cm_id
2490 enum rdma_ch_state ch_state;
2492 ch_state = srpt_get_ch_state(ch);
2493 if (ch_state != CH_CONNECTING
2494 && ch_state != CH_LIVE)
2497 /* found an existing channel */
2498 pr_debug("Found existing channel %s"
2499 " cm_id= %p state= %d\n",
2500 ch->sess_name, ch->cm_id, ch_state);
2502 __srpt_close_ch(ch);
2505 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2509 spin_unlock_irq(&sdev->spinlock);
2512 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2514 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2515 || *(__be64 *)(req->target_port_id + 8) !=
2516 cpu_to_be64(srpt_service_guid)) {
2517 rej->reason = __constant_cpu_to_be32(
2518 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2520 pr_err("rejected SRP_LOGIN_REQ because it"
2521 " has an invalid target port identifier.\n");
2525 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2527 rej->reason = __constant_cpu_to_be32(
2528 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2529 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
2534 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2535 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2536 memcpy(ch->t_port_id, req->target_port_id, 16);
2537 ch->sport = &sdev->port[param->port - 1];
2540 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2541 * for the SRP protocol to the command queue size.
2543 ch->rq_size = SRPT_RQ_SIZE;
2544 spin_lock_init(&ch->spinlock);
2545 ch->state = CH_CONNECTING;
2546 INIT_LIST_HEAD(&ch->cmd_wait_list);
2547 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2549 ch->ioctx_ring = (struct srpt_send_ioctx **)
2550 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2551 sizeof(*ch->ioctx_ring[0]),
2552 ch->rsp_size, DMA_TO_DEVICE);
2553 if (!ch->ioctx_ring)
2556 INIT_LIST_HEAD(&ch->free_list);
2557 for (i = 0; i < ch->rq_size; i++) {
2558 ch->ioctx_ring[i]->ch = ch;
2559 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2562 ret = srpt_create_ch_ib(ch);
2564 rej->reason = __constant_cpu_to_be32(
2565 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2566 pr_err("rejected SRP_LOGIN_REQ because creating"
2567 " a new RDMA channel failed.\n");
2571 ret = srpt_ch_qp_rtr(ch, ch->qp);
2573 rej->reason = __constant_cpu_to_be32(
2574 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2575 pr_err("rejected SRP_LOGIN_REQ because enabling"
2576 " RTR failed (error code = %d)\n", ret);
2580 * Use the initator port identifier as the session name.
2582 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2583 be64_to_cpu(*(__be64 *)ch->i_port_id),
2584 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2586 pr_debug("registering session %s\n", ch->sess_name);
2588 nacl = srpt_lookup_acl(sport, ch->i_port_id);
2590 pr_info("Rejected login because no ACL has been"
2591 " configured yet for initiator %s.\n", ch->sess_name);
2592 rej->reason = __constant_cpu_to_be32(
2593 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2597 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
2598 if (IS_ERR(ch->sess)) {
2599 rej->reason = __constant_cpu_to_be32(
2600 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2601 pr_debug("Failed to create session\n");
2602 goto deregister_session;
2604 ch->sess->se_node_acl = &nacl->nacl;
2605 transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2607 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2608 ch->sess_name, ch->cm_id);
2610 /* create srp_login_response */
2611 rsp->opcode = SRP_LOGIN_RSP;
2612 rsp->tag = req->tag;
2613 rsp->max_it_iu_len = req->req_it_iu_len;
2614 rsp->max_ti_iu_len = req->req_it_iu_len;
2615 ch->max_ti_iu_len = it_iu_len;
2616 rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2617 | SRP_BUF_FORMAT_INDIRECT);
2618 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2619 atomic_set(&ch->req_lim, ch->rq_size);
2620 atomic_set(&ch->req_lim_delta, 0);
2622 /* create cm reply */
2623 rep_param->qp_num = ch->qp->qp_num;
2624 rep_param->private_data = (void *)rsp;
2625 rep_param->private_data_len = sizeof *rsp;
2626 rep_param->rnr_retry_count = 7;
2627 rep_param->flow_control = 1;
2628 rep_param->failover_accepted = 0;
2630 rep_param->responder_resources = 4;
2631 rep_param->initiator_depth = 4;
2633 ret = ib_send_cm_rep(cm_id, rep_param);
2635 pr_err("sending SRP_LOGIN_REQ response failed"
2636 " (error code = %d)\n", ret);
2637 goto release_channel;
2640 spin_lock_irq(&sdev->spinlock);
2641 list_add_tail(&ch->list, &sdev->rch_list);
2642 spin_unlock_irq(&sdev->spinlock);
2647 srpt_set_ch_state(ch, CH_RELEASING);
2648 transport_deregister_session_configfs(ch->sess);
2651 transport_deregister_session(ch->sess);
2655 srpt_destroy_ch_ib(ch);
2658 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2659 ch->sport->sdev, ch->rq_size,
2660 ch->rsp_size, DMA_TO_DEVICE);
2665 rej->opcode = SRP_LOGIN_REJ;
2666 rej->tag = req->tag;
2667 rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2668 | SRP_BUF_FORMAT_INDIRECT);
2670 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2671 (void *)rej, sizeof *rej);
2681 static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2683 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
2684 srpt_drain_channel(cm_id);
2688 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2690 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2691 * and that the recipient may begin transmitting (RTU = ready to use).
2693 static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2695 struct srpt_rdma_ch *ch;
2698 ch = srpt_find_channel(cm_id->context, cm_id);
2701 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2702 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2704 ret = srpt_ch_qp_rts(ch, ch->qp);
2706 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2708 list_del(&ioctx->wait_list);
2709 srpt_handle_new_iu(ch, ioctx, NULL);
2716 static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2718 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
2719 srpt_drain_channel(cm_id);
2722 static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2724 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
2725 srpt_drain_channel(cm_id);
2729 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2731 static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2733 struct srpt_rdma_ch *ch;
2734 unsigned long flags;
2735 bool send_drep = false;
2737 ch = srpt_find_channel(cm_id->context, cm_id);
2740 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2742 spin_lock_irqsave(&ch->spinlock, flags);
2743 switch (ch->state) {
2747 ch->state = CH_DISCONNECTING;
2749 case CH_DISCONNECTING:
2752 WARN(true, "unexpected channel state %d\n", ch->state);
2755 spin_unlock_irqrestore(&ch->spinlock, flags);
2758 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
2759 pr_err("Sending IB DREP failed.\n");
2760 pr_info("Received DREQ and sent DREP for session %s.\n",
2766 * srpt_cm_drep_recv() - Process reception of a DREP message.
2768 static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2770 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
2771 srpt_drain_channel(cm_id);
2775 * srpt_cm_handler() - IB connection manager callback function.
2777 * A non-zero return value will cause the caller destroy the CM ID.
2779 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2780 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2781 * a non-zero value in any other case will trigger a race with the
2782 * ib_destroy_cm_id() call in srpt_release_channel().
2784 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2789 switch (event->event) {
2790 case IB_CM_REQ_RECEIVED:
2791 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2792 event->private_data);
2794 case IB_CM_REJ_RECEIVED:
2795 srpt_cm_rej_recv(cm_id);
2797 case IB_CM_RTU_RECEIVED:
2798 case IB_CM_USER_ESTABLISHED:
2799 srpt_cm_rtu_recv(cm_id);
2801 case IB_CM_DREQ_RECEIVED:
2802 srpt_cm_dreq_recv(cm_id);
2804 case IB_CM_DREP_RECEIVED:
2805 srpt_cm_drep_recv(cm_id);
2807 case IB_CM_TIMEWAIT_EXIT:
2808 srpt_cm_timewait_exit(cm_id);
2810 case IB_CM_REP_ERROR:
2811 srpt_cm_rep_error(cm_id);
2813 case IB_CM_DREQ_ERROR:
2814 pr_info("Received IB DREQ ERROR event.\n");
2816 case IB_CM_MRA_RECEIVED:
2817 pr_info("Received IB MRA event\n");
2820 pr_err("received unrecognized IB CM event %d\n", event->event);
2828 * srpt_perform_rdmas() - Perform IB RDMA.
2830 * Returns zero upon success or a negative number upon failure.
2832 static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2833 struct srpt_send_ioctx *ioctx)
2835 struct ib_send_wr wr;
2836 struct ib_send_wr *bad_wr;
2837 struct rdma_iu *riu;
2841 enum dma_data_direction dir;
2842 const int n_rdma = ioctx->n_rdma;
2844 dir = ioctx->cmd.data_direction;
2845 if (dir == DMA_TO_DEVICE) {
2848 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2849 if (sq_wr_avail < 0) {
2850 pr_warn("IB send queue full (needed %d)\n",
2856 ioctx->rdma_aborted = false;
2858 riu = ioctx->rdma_ius;
2859 memset(&wr, 0, sizeof wr);
2861 for (i = 0; i < n_rdma; ++i, ++riu) {
2862 if (dir == DMA_FROM_DEVICE) {
2863 wr.opcode = IB_WR_RDMA_WRITE;
2864 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2865 SRPT_RDMA_WRITE_LAST :
2867 ioctx->ioctx.index);
2869 wr.opcode = IB_WR_RDMA_READ;
2870 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2871 SRPT_RDMA_READ_LAST :
2873 ioctx->ioctx.index);
2876 wr.wr.rdma.remote_addr = riu->raddr;
2877 wr.wr.rdma.rkey = riu->rkey;
2878 wr.num_sge = riu->sge_cnt;
2879 wr.sg_list = riu->sge;
2881 /* only get completion event for the last rdma write */
2882 if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
2883 wr.send_flags = IB_SEND_SIGNALED;
2885 ret = ib_post_send(ch->qp, &wr, &bad_wr);
2891 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
2892 __func__, __LINE__, ret, i, n_rdma);
2895 wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2896 wr.send_flags = IB_SEND_SIGNALED;
2897 while (ch->state == CH_LIVE &&
2898 ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
2899 pr_info("Trying to abort failed RDMA transfer [%d]\n",
2900 ioctx->ioctx.index);
2903 while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
2904 pr_info("Waiting until RDMA abort finished [%d]\n",
2905 ioctx->ioctx.index);
2910 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2911 atomic_add(n_rdma, &ch->sq_wr_avail);
2916 * srpt_xfer_data() - Start data transfer from initiator to target.
2918 static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2919 struct srpt_send_ioctx *ioctx)
2923 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2925 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
2929 ret = srpt_perform_rdmas(ch, ioctx);
2931 if (ret == -EAGAIN || ret == -ENOMEM)
2932 pr_info("%s[%d] queue full -- ret=%d\n",
2933 __func__, __LINE__, ret);
2935 pr_err("%s[%d] fatal error -- ret=%d\n",
2936 __func__, __LINE__, ret);
2943 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2947 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2949 struct srpt_send_ioctx *ioctx;
2951 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2952 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2956 * srpt_write_pending() - Start data transfer from initiator to target (write).
2958 static int srpt_write_pending(struct se_cmd *se_cmd)
2960 struct srpt_rdma_ch *ch;
2961 struct srpt_send_ioctx *ioctx;
2962 enum srpt_command_state new_state;
2963 enum rdma_ch_state ch_state;
2966 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2968 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2969 WARN_ON(new_state == SRPT_STATE_DONE);
2974 ch_state = srpt_get_ch_state(ch);
2977 WARN(true, "unexpected channel state %d\n", ch_state);
2982 case CH_DISCONNECTING:
2985 pr_debug("cmd with tag %lld: channel disconnecting\n",
2987 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2991 ret = srpt_xfer_data(ch, ioctx);
2997 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2999 switch (tcm_mgmt_status) {
3000 case TMR_FUNCTION_COMPLETE:
3001 return SRP_TSK_MGMT_SUCCESS;
3002 case TMR_FUNCTION_REJECTED:
3003 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
3005 return SRP_TSK_MGMT_FAILED;
3009 * srpt_queue_response() - Transmits the response to a SCSI command.
3011 * Callback function called by the TCM core. Must not block since it can be
3012 * invoked on the context of the IB completion handler.
3014 static void srpt_queue_response(struct se_cmd *cmd)
3016 struct srpt_rdma_ch *ch;
3017 struct srpt_send_ioctx *ioctx;
3018 enum srpt_command_state state;
3019 unsigned long flags;
3021 enum dma_data_direction dir;
3025 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3029 spin_lock_irqsave(&ioctx->spinlock, flags);
3030 state = ioctx->state;
3032 case SRPT_STATE_NEW:
3033 case SRPT_STATE_DATA_IN:
3034 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
3036 case SRPT_STATE_MGMT:
3037 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
3040 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
3041 ch, ioctx->ioctx.index, ioctx->state);
3044 spin_unlock_irqrestore(&ioctx->spinlock, flags);
3046 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
3047 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
3048 atomic_inc(&ch->req_lim_delta);
3049 srpt_abort_cmd(ioctx);
3053 dir = ioctx->cmd.data_direction;
3055 /* For read commands, transfer the data to the initiator. */
3056 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
3057 !ioctx->queue_status_only) {
3058 ret = srpt_xfer_data(ch, ioctx);
3060 pr_err("xfer_data failed for tag %llu\n",
3066 if (state != SRPT_STATE_MGMT)
3067 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
3071 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3072 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
3075 ret = srpt_post_send(ch, ioctx, resp_len);
3077 pr_err("sending cmd response failed for tag %llu\n",
3079 srpt_unmap_sg_to_ib_sge(ch, ioctx);
3080 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
3081 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
3085 static int srpt_queue_data_in(struct se_cmd *cmd)
3087 srpt_queue_response(cmd);
3091 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
3093 srpt_queue_response(cmd);
3096 static void srpt_aborted_task(struct se_cmd *cmd)
3098 struct srpt_send_ioctx *ioctx = container_of(cmd,
3099 struct srpt_send_ioctx, cmd);
3101 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
3104 static int srpt_queue_status(struct se_cmd *cmd)
3106 struct srpt_send_ioctx *ioctx;
3108 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3109 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3110 if (cmd->se_cmd_flags &
3111 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3112 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3113 ioctx->queue_status_only = true;
3114 srpt_queue_response(cmd);
3118 static void srpt_refresh_port_work(struct work_struct *work)
3120 struct srpt_port *sport = container_of(work, struct srpt_port, work);
3122 srpt_refresh_port(sport);
3125 static int srpt_ch_list_empty(struct srpt_device *sdev)
3129 spin_lock_irq(&sdev->spinlock);
3130 res = list_empty(&sdev->rch_list);
3131 spin_unlock_irq(&sdev->spinlock);
3137 * srpt_release_sdev() - Free the channel resources associated with a target.
3139 static int srpt_release_sdev(struct srpt_device *sdev)
3141 struct srpt_rdma_ch *ch, *tmp_ch;
3144 WARN_ON_ONCE(irqs_disabled());
3148 spin_lock_irq(&sdev->spinlock);
3149 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3150 __srpt_close_ch(ch);
3151 spin_unlock_irq(&sdev->spinlock);
3153 res = wait_event_interruptible(sdev->ch_releaseQ,
3154 srpt_ch_list_empty(sdev));
3156 pr_err("%s: interrupted.\n", __func__);
3161 static struct srpt_port *__srpt_lookup_port(const char *name)
3163 struct ib_device *dev;
3164 struct srpt_device *sdev;
3165 struct srpt_port *sport;
3168 list_for_each_entry(sdev, &srpt_dev_list, list) {
3173 for (i = 0; i < dev->phys_port_cnt; i++) {
3174 sport = &sdev->port[i];
3176 if (!strcmp(sport->port_guid, name))
3184 static struct srpt_port *srpt_lookup_port(const char *name)
3186 struct srpt_port *sport;
3188 spin_lock(&srpt_dev_lock);
3189 sport = __srpt_lookup_port(name);
3190 spin_unlock(&srpt_dev_lock);
3196 * srpt_add_one() - Infiniband device addition callback function.
3198 static void srpt_add_one(struct ib_device *device)
3200 struct srpt_device *sdev;
3201 struct srpt_port *sport;
3202 struct ib_srq_init_attr srq_attr;
3205 pr_debug("device = %p, device->dma_ops = %p\n", device,
3208 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3212 sdev->device = device;
3213 INIT_LIST_HEAD(&sdev->rch_list);
3214 init_waitqueue_head(&sdev->ch_releaseQ);
3215 spin_lock_init(&sdev->spinlock);
3217 if (ib_query_device(device, &sdev->dev_attr))
3220 sdev->pd = ib_alloc_pd(device);
3221 if (IS_ERR(sdev->pd))
3224 sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
3225 if (IS_ERR(sdev->mr))
3228 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3230 srq_attr.event_handler = srpt_srq_event;
3231 srq_attr.srq_context = (void *)sdev;
3232 srq_attr.attr.max_wr = sdev->srq_size;
3233 srq_attr.attr.max_sge = 1;
3234 srq_attr.attr.srq_limit = 0;
3235 srq_attr.srq_type = IB_SRQT_BASIC;
3237 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3238 if (IS_ERR(sdev->srq))
3241 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3242 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3245 if (!srpt_service_guid)
3246 srpt_service_guid = be64_to_cpu(device->node_guid);
3248 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3249 if (IS_ERR(sdev->cm_id))
3252 /* print out target login information */
3253 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3254 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3255 srpt_service_guid, srpt_service_guid);
3258 * We do not have a consistent service_id (ie. also id_ext of target_id)
3259 * to identify this target. We currently use the guid of the first HCA
3260 * in the system as service_id; therefore, the target_id will change
3261 * if this HCA is gone bad and replaced by different HCA
3263 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
3266 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3267 srpt_event_handler);
3268 if (ib_register_event_handler(&sdev->event_handler))
3271 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3272 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3273 sizeof(*sdev->ioctx_ring[0]),
3274 srp_max_req_size, DMA_FROM_DEVICE);
3275 if (!sdev->ioctx_ring)
3278 for (i = 0; i < sdev->srq_size; ++i)
3279 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3281 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
3283 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3284 sport = &sdev->port[i - 1];
3287 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3288 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3289 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3290 INIT_WORK(&sport->work, srpt_refresh_port_work);
3291 INIT_LIST_HEAD(&sport->port_acl_list);
3292 spin_lock_init(&sport->port_acl_lock);
3294 if (srpt_refresh_port(sport)) {
3295 pr_err("MAD registration failed for %s-%d.\n",
3296 srpt_sdev_name(sdev), i);
3299 snprintf(sport->port_guid, sizeof(sport->port_guid),
3301 be64_to_cpu(sport->gid.global.subnet_prefix),
3302 be64_to_cpu(sport->gid.global.interface_id));
3305 spin_lock(&srpt_dev_lock);
3306 list_add_tail(&sdev->list, &srpt_dev_list);
3307 spin_unlock(&srpt_dev_lock);
3310 ib_set_client_data(device, &srpt_client, sdev);
3311 pr_debug("added %s.\n", device->name);
3315 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3316 sdev->srq_size, srp_max_req_size,
3319 ib_unregister_event_handler(&sdev->event_handler);
3321 ib_destroy_cm_id(sdev->cm_id);
3323 ib_destroy_srq(sdev->srq);
3325 ib_dereg_mr(sdev->mr);
3327 ib_dealloc_pd(sdev->pd);
3332 pr_info("%s(%s) failed.\n", __func__, device->name);
3337 * srpt_remove_one() - InfiniBand device removal callback function.
3339 static void srpt_remove_one(struct ib_device *device)
3341 struct srpt_device *sdev;
3344 sdev = ib_get_client_data(device, &srpt_client);
3346 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
3350 srpt_unregister_mad_agent(sdev);
3352 ib_unregister_event_handler(&sdev->event_handler);
3354 /* Cancel any work queued by the just unregistered IB event handler. */
3355 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3356 cancel_work_sync(&sdev->port[i].work);
3358 ib_destroy_cm_id(sdev->cm_id);
3361 * Unregistering a target must happen after destroying sdev->cm_id
3362 * such that no new SRP_LOGIN_REQ information units can arrive while
3363 * destroying the target.
3365 spin_lock(&srpt_dev_lock);
3366 list_del(&sdev->list);
3367 spin_unlock(&srpt_dev_lock);
3368 srpt_release_sdev(sdev);
3370 ib_destroy_srq(sdev->srq);
3371 ib_dereg_mr(sdev->mr);
3372 ib_dealloc_pd(sdev->pd);
3374 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3375 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3376 sdev->ioctx_ring = NULL;
3380 static struct ib_client srpt_client = {
3382 .add = srpt_add_one,
3383 .remove = srpt_remove_one
3386 static int srpt_check_true(struct se_portal_group *se_tpg)
3391 static int srpt_check_false(struct se_portal_group *se_tpg)
3396 static char *srpt_get_fabric_name(void)
3401 static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
3403 return SCSI_TRANSPORTID_PROTOCOLID_SRP;
3406 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3408 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3410 return sport->port_guid;
3413 static u16 srpt_get_tag(struct se_portal_group *tpg)
3418 static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
3423 static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
3424 struct se_node_acl *se_nacl,
3425 struct t10_pr_registration *pr_reg,
3426 int *format_code, unsigned char *buf)
3428 struct srpt_node_acl *nacl;
3429 struct spc_rdma_transport_id *tr_id;
3431 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3432 tr_id = (void *)buf;
3433 tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
3434 memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
3435 return sizeof(*tr_id);
3438 static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
3439 struct se_node_acl *se_nacl,
3440 struct t10_pr_registration *pr_reg,
3444 return sizeof(struct spc_rdma_transport_id);
3447 static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
3448 const char *buf, u32 *out_tid_len,
3449 char **port_nexus_ptr)
3451 struct spc_rdma_transport_id *tr_id;
3453 *port_nexus_ptr = NULL;
3454 *out_tid_len = sizeof(struct spc_rdma_transport_id);
3455 tr_id = (void *)buf;
3456 return (char *)tr_id->i_port_id;
3459 static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
3461 struct srpt_node_acl *nacl;
3463 nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
3465 pr_err("Unable to allocate struct srpt_node_acl\n");
3472 static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
3473 struct se_node_acl *se_nacl)
3475 struct srpt_node_acl *nacl;
3477 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3481 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3486 static void srpt_release_cmd(struct se_cmd *se_cmd)
3488 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3489 struct srpt_send_ioctx, cmd);
3490 struct srpt_rdma_ch *ch = ioctx->ch;
3491 unsigned long flags;
3493 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3494 WARN_ON(ioctx->mapped_sg_count != 0);
3496 if (ioctx->n_rbuf > 1) {
3497 kfree(ioctx->rbufs);
3498 ioctx->rbufs = NULL;
3502 spin_lock_irqsave(&ch->spinlock, flags);
3503 list_add(&ioctx->free_list, &ch->free_list);
3504 spin_unlock_irqrestore(&ch->spinlock, flags);
3508 * srpt_close_session() - Forcibly close a session.
3510 * Callback function invoked by the TCM core to clean up sessions associated
3511 * with a node ACL when the user invokes
3512 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3514 static void srpt_close_session(struct se_session *se_sess)
3516 DECLARE_COMPLETION_ONSTACK(release_done);
3517 struct srpt_rdma_ch *ch;
3518 struct srpt_device *sdev;
3521 ch = se_sess->fabric_sess_ptr;
3522 WARN_ON(ch->sess != se_sess);
3524 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3526 sdev = ch->sport->sdev;
3527 spin_lock_irq(&sdev->spinlock);
3528 BUG_ON(ch->release_done);
3529 ch->release_done = &release_done;
3530 __srpt_close_ch(ch);
3531 spin_unlock_irq(&sdev->spinlock);
3533 res = wait_for_completion_timeout(&release_done, 60 * HZ);
3538 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3540 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3541 * This object represents an arbitrary integer used to uniquely identify a
3542 * particular attached remote initiator port to a particular SCSI target port
3543 * within a particular SCSI target device within a particular SCSI instance.
3545 static u32 srpt_sess_get_index(struct se_session *se_sess)
3550 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3554 static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
3556 struct srpt_send_ioctx *ioctx;
3558 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3562 /* Note: only used from inside debug printk's by the TCM core. */
3563 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3565 struct srpt_send_ioctx *ioctx;
3567 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3568 return srpt_get_cmd_state(ioctx);
3572 * srpt_parse_i_port_id() - Parse an initiator port ID.
3573 * @name: ASCII representation of a 128-bit initiator port ID.
3574 * @i_port_id: Binary 128-bit port ID.
3576 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3579 unsigned len, count, leading_zero_bytes;
3583 if (strncasecmp(p, "0x", 2) == 0)
3589 count = min(len / 2, 16U);
3590 leading_zero_bytes = 16 - count;
3591 memset(i_port_id, 0, leading_zero_bytes);
3592 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3594 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3601 * configfs callback function invoked for
3602 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3604 static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
3605 struct config_group *group,
3608 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3609 struct se_node_acl *se_nacl, *se_nacl_new;
3610 struct srpt_node_acl *nacl;
3612 u32 nexus_depth = 1;
3615 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
3616 pr_err("invalid initiator port ID %s\n", name);
3621 se_nacl_new = srpt_alloc_fabric_acl(tpg);
3627 * nacl_new may be released by core_tpg_add_initiator_node_acl()
3628 * when converting a node ACL from demo mode to explict
3630 se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
3632 if (IS_ERR(se_nacl)) {
3633 ret = PTR_ERR(se_nacl);
3636 /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
3637 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3638 memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3639 nacl->sport = sport;
3641 spin_lock_irq(&sport->port_acl_lock);
3642 list_add_tail(&nacl->list, &sport->port_acl_list);
3643 spin_unlock_irq(&sport->port_acl_lock);
3647 return ERR_PTR(ret);
3651 * configfs callback function invoked for
3652 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3654 static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
3656 struct srpt_node_acl *nacl;
3657 struct srpt_device *sdev;
3658 struct srpt_port *sport;
3660 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3661 sport = nacl->sport;
3663 spin_lock_irq(&sport->port_acl_lock);
3664 list_del(&nacl->list);
3665 spin_unlock_irq(&sport->port_acl_lock);
3666 core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
3667 srpt_release_fabric_acl(NULL, se_nacl);
3670 static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
3671 struct se_portal_group *se_tpg,
3674 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3676 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3679 static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
3680 struct se_portal_group *se_tpg,
3684 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3688 ret = kstrtoul(page, 0, &val);
3690 pr_err("kstrtoul() failed with ret: %d\n", ret);
3693 if (val > MAX_SRPT_RDMA_SIZE) {
3694 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3695 MAX_SRPT_RDMA_SIZE);
3698 if (val < DEFAULT_MAX_RDMA_SIZE) {
3699 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3700 val, DEFAULT_MAX_RDMA_SIZE);
3703 sport->port_attrib.srp_max_rdma_size = val;
3708 TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
3710 static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
3711 struct se_portal_group *se_tpg,
3714 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3716 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3719 static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
3720 struct se_portal_group *se_tpg,
3724 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3728 ret = kstrtoul(page, 0, &val);
3730 pr_err("kstrtoul() failed with ret: %d\n", ret);
3733 if (val > MAX_SRPT_RSP_SIZE) {
3734 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3738 if (val < MIN_MAX_RSP_SIZE) {
3739 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3743 sport->port_attrib.srp_max_rsp_size = val;
3748 TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
3750 static ssize_t srpt_tpg_attrib_show_srp_sq_size(
3751 struct se_portal_group *se_tpg,
3754 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3756 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3759 static ssize_t srpt_tpg_attrib_store_srp_sq_size(
3760 struct se_portal_group *se_tpg,
3764 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3768 ret = kstrtoul(page, 0, &val);
3770 pr_err("kstrtoul() failed with ret: %d\n", ret);
3773 if (val > MAX_SRPT_SRQ_SIZE) {
3774 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3778 if (val < MIN_SRPT_SRQ_SIZE) {
3779 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3783 sport->port_attrib.srp_sq_size = val;
3788 TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
3790 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3791 &srpt_tpg_attrib_srp_max_rdma_size.attr,
3792 &srpt_tpg_attrib_srp_max_rsp_size.attr,
3793 &srpt_tpg_attrib_srp_sq_size.attr,
3797 static ssize_t srpt_tpg_show_enable(
3798 struct se_portal_group *se_tpg,
3801 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3803 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3806 static ssize_t srpt_tpg_store_enable(
3807 struct se_portal_group *se_tpg,
3811 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3815 ret = kstrtoul(page, 0, &tmp);
3817 pr_err("Unable to extract srpt_tpg_store_enable\n");
3821 if ((tmp != 0) && (tmp != 1)) {
3822 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3826 sport->enabled = true;
3828 sport->enabled = false;
3833 TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
3835 static struct configfs_attribute *srpt_tpg_attrs[] = {
3836 &srpt_tpg_enable.attr,
3841 * configfs callback invoked for
3842 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3844 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3845 struct config_group *group,
3848 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3851 /* Initialize sport->port_wwn and sport->port_tpg_1 */
3852 res = core_tpg_register(&srpt_template, &sport->port_wwn,
3853 &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
3855 return ERR_PTR(res);
3857 return &sport->port_tpg_1;
3861 * configfs callback invoked for
3862 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3864 static void srpt_drop_tpg(struct se_portal_group *tpg)
3866 struct srpt_port *sport = container_of(tpg,
3867 struct srpt_port, port_tpg_1);
3869 sport->enabled = false;
3870 core_tpg_deregister(&sport->port_tpg_1);
3874 * configfs callback invoked for
3875 * mkdir /sys/kernel/config/target/$driver/$port
3877 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3878 struct config_group *group,
3881 struct srpt_port *sport;
3884 sport = srpt_lookup_port(name);
3885 pr_debug("make_tport(%s)\n", name);
3890 return &sport->port_wwn;
3893 return ERR_PTR(ret);
3897 * configfs callback invoked for
3898 * rmdir /sys/kernel/config/target/$driver/$port
3900 static void srpt_drop_tport(struct se_wwn *wwn)
3902 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3904 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3907 static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
3910 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3913 TF_WWN_ATTR_RO(srpt, version);
3915 static struct configfs_attribute *srpt_wwn_attrs[] = {
3916 &srpt_wwn_version.attr,
3920 static const struct target_core_fabric_ops srpt_template = {
3921 .module = THIS_MODULE,
3923 .get_fabric_name = srpt_get_fabric_name,
3924 .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
3925 .tpg_get_wwn = srpt_get_fabric_wwn,
3926 .tpg_get_tag = srpt_get_tag,
3927 .tpg_get_default_depth = srpt_get_default_depth,
3928 .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
3929 .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
3930 .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
3931 .tpg_check_demo_mode = srpt_check_false,
3932 .tpg_check_demo_mode_cache = srpt_check_true,
3933 .tpg_check_demo_mode_write_protect = srpt_check_true,
3934 .tpg_check_prod_mode_write_protect = srpt_check_false,
3935 .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
3936 .tpg_release_fabric_acl = srpt_release_fabric_acl,
3937 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3938 .release_cmd = srpt_release_cmd,
3939 .check_stop_free = srpt_check_stop_free,
3940 .shutdown_session = srpt_shutdown_session,
3941 .close_session = srpt_close_session,
3942 .sess_get_index = srpt_sess_get_index,
3943 .sess_get_initiator_sid = NULL,
3944 .write_pending = srpt_write_pending,
3945 .write_pending_status = srpt_write_pending_status,
3946 .set_default_node_attributes = srpt_set_default_node_attrs,
3947 .get_task_tag = srpt_get_task_tag,
3948 .get_cmd_state = srpt_get_tcm_cmd_state,
3949 .queue_data_in = srpt_queue_data_in,
3950 .queue_status = srpt_queue_status,
3951 .queue_tm_rsp = srpt_queue_tm_rsp,
3952 .aborted_task = srpt_aborted_task,
3954 * Setup function pointers for generic logic in
3955 * target_core_fabric_configfs.c
3957 .fabric_make_wwn = srpt_make_tport,
3958 .fabric_drop_wwn = srpt_drop_tport,
3959 .fabric_make_tpg = srpt_make_tpg,
3960 .fabric_drop_tpg = srpt_drop_tpg,
3961 .fabric_post_link = NULL,
3962 .fabric_pre_unlink = NULL,
3963 .fabric_make_np = NULL,
3964 .fabric_drop_np = NULL,
3965 .fabric_make_nodeacl = srpt_make_nodeacl,
3966 .fabric_drop_nodeacl = srpt_drop_nodeacl,
3968 .tfc_wwn_attrs = srpt_wwn_attrs,
3969 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3970 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
3974 * srpt_init_module() - Kernel module initialization.
3976 * Note: Since ib_register_client() registers callback functions, and since at
3977 * least one of these callback functions (srpt_add_one()) calls target core
3978 * functions, this driver must be registered with the target core before
3979 * ib_register_client() is called.
3981 static int __init srpt_init_module(void)
3986 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3987 pr_err("invalid value %d for kernel module parameter"
3988 " srp_max_req_size -- must be at least %d.\n",
3989 srp_max_req_size, MIN_MAX_REQ_SIZE);
3993 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3994 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3995 pr_err("invalid value %d for kernel module parameter"
3996 " srpt_srq_size -- must be in the range [%d..%d].\n",
3997 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
4001 ret = target_register_template(&srpt_template);
4005 ret = ib_register_client(&srpt_client);
4007 pr_err("couldn't register IB client\n");
4008 goto out_unregister_target;
4013 out_unregister_target:
4014 target_unregister_template(&srpt_template);
4019 static void __exit srpt_cleanup_module(void)
4021 ib_unregister_client(&srpt_client);
4022 target_unregister_template(&srpt_template);
4025 module_init(srpt_init_module);
4026 module_exit(srpt_cleanup_module);