2 * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
3 * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/err.h>
39 #include <linux/ctype.h>
40 #include <linux/kthread.h>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/atomic.h>
44 #include <scsi/scsi_proto.h>
45 #include <scsi/scsi_tcq.h>
46 #include <target/target_core_base.h>
47 #include <target/target_core_fabric.h>
50 /* Name of this kernel module. */
51 #define DRV_NAME "ib_srpt"
52 #define DRV_VERSION "2.0.0"
53 #define DRV_RELDATE "2011-02-14"
55 #define SRPT_ID_STRING "Linux SRP target"
58 #define pr_fmt(fmt) DRV_NAME " " fmt
60 MODULE_AUTHOR("Vu Pham and Bart Van Assche");
61 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63 MODULE_LICENSE("Dual BSD/GPL");
69 static u64 srpt_service_guid;
70 static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
71 static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
73 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
74 module_param(srp_max_req_size, int, 0444);
75 MODULE_PARM_DESC(srp_max_req_size,
76 "Maximum size of SRP request messages in bytes.");
78 static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
79 module_param(srpt_srq_size, int, 0444);
80 MODULE_PARM_DESC(srpt_srq_size,
81 "Shared receive queue (SRQ) size.");
83 static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
85 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
87 module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
89 MODULE_PARM_DESC(srpt_service_guid,
90 "Using this value for ioc_guid, id_ext, and cm_listen_id"
91 " instead of using the node_guid of the first HCA.");
93 static struct ib_client srpt_client;
94 static void srpt_release_channel(struct srpt_rdma_ch *ch);
95 static int srpt_queue_status(struct se_cmd *cmd);
96 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
97 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
100 * The only allowed channel state changes are those that change the channel
101 * state into a state with a higher numerical value. Hence the new > prev test.
103 static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
106 enum rdma_ch_state prev;
107 bool changed = false;
109 spin_lock_irqsave(&ch->spinlock, flags);
115 spin_unlock_irqrestore(&ch->spinlock, flags);
121 * srpt_event_handler() - Asynchronous IB event callback function.
123 * Callback function called by the InfiniBand core when an asynchronous IB
124 * event occurs. This callback may occur in interrupt context. See also
125 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
126 * Architecture Specification.
128 static void srpt_event_handler(struct ib_event_handler *handler,
129 struct ib_event *event)
131 struct srpt_device *sdev;
132 struct srpt_port *sport;
134 sdev = ib_get_client_data(event->device, &srpt_client);
135 if (!sdev || sdev->device != event->device)
138 pr_debug("ASYNC event= %d on device= %s\n", event->event,
141 switch (event->event) {
142 case IB_EVENT_PORT_ERR:
143 if (event->element.port_num <= sdev->device->phys_port_cnt) {
144 sport = &sdev->port[event->element.port_num - 1];
149 case IB_EVENT_PORT_ACTIVE:
150 case IB_EVENT_LID_CHANGE:
151 case IB_EVENT_PKEY_CHANGE:
152 case IB_EVENT_SM_CHANGE:
153 case IB_EVENT_CLIENT_REREGISTER:
154 case IB_EVENT_GID_CHANGE:
155 /* Refresh port data asynchronously. */
156 if (event->element.port_num <= sdev->device->phys_port_cnt) {
157 sport = &sdev->port[event->element.port_num - 1];
158 if (!sport->lid && !sport->sm_lid)
159 schedule_work(&sport->work);
163 pr_err("received unrecognized IB event %d\n",
170 * srpt_srq_event() - SRQ event callback function.
172 static void srpt_srq_event(struct ib_event *event, void *ctx)
174 pr_info("SRQ event %d\n", event->event);
178 * srpt_qp_event() - QP event callback function.
180 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
182 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
183 event->event, ch->cm_id, ch->sess_name, ch->state);
185 switch (event->event) {
186 case IB_EVENT_COMM_EST:
187 ib_cm_notify(ch->cm_id, event->event);
189 case IB_EVENT_QP_LAST_WQE_REACHED:
190 if (srpt_set_ch_state(ch, CH_RELEASING))
191 srpt_release_channel(ch);
193 pr_debug("%s: state %d - ignored LAST_WQE.\n",
194 ch->sess_name, ch->state);
197 pr_err("received unrecognized IB QP event %d\n", event->event);
203 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
205 * @slot: one-based slot number.
206 * @value: four-bit value.
208 * Copies the lowest four bits of value in element slot of the array of four
209 * bit elements called c_list (controller list). The index slot is one-based.
211 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
218 tmp = c_list[id] & 0xf;
219 c_list[id] = (value << 4) | tmp;
221 tmp = c_list[id] & 0xf0;
222 c_list[id] = (value & 0xf) | tmp;
227 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
229 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
232 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
234 struct ib_class_port_info *cif;
236 cif = (struct ib_class_port_info *)mad->data;
237 memset(cif, 0, sizeof(*cif));
238 cif->base_version = 1;
239 cif->class_version = 1;
240 cif->resp_time_value = 20;
242 mad->mad_hdr.status = 0;
246 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
248 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
249 * Specification. See also section B.7, table B.6 in the SRP r16a document.
251 static void srpt_get_iou(struct ib_dm_mad *mad)
253 struct ib_dm_iou_info *ioui;
257 ioui = (struct ib_dm_iou_info *)mad->data;
258 ioui->change_id = cpu_to_be16(1);
259 ioui->max_controllers = 16;
261 /* set present for slot 1 and empty for the rest */
262 srpt_set_ioc(ioui->controller_list, 1, 1);
263 for (i = 1, slot = 2; i < 16; i++, slot++)
264 srpt_set_ioc(ioui->controller_list, slot, 0);
266 mad->mad_hdr.status = 0;
270 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
272 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
273 * Architecture Specification. See also section B.7, table B.7 in the SRP
276 static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
277 struct ib_dm_mad *mad)
279 struct srpt_device *sdev = sport->sdev;
280 struct ib_dm_ioc_profile *iocp;
282 iocp = (struct ib_dm_ioc_profile *)mad->data;
284 if (!slot || slot > 16) {
286 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
292 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
296 memset(iocp, 0, sizeof(*iocp));
297 strcpy(iocp->id_string, SRPT_ID_STRING);
298 iocp->guid = cpu_to_be64(srpt_service_guid);
299 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
300 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
301 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
302 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
303 iocp->subsys_device_id = 0x0;
304 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
305 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
306 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
307 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
308 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
309 iocp->rdma_read_depth = 4;
310 iocp->send_size = cpu_to_be32(srp_max_req_size);
311 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
313 iocp->num_svc_entries = 1;
314 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
315 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
317 mad->mad_hdr.status = 0;
321 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
323 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
324 * Specification. See also section B.7, table B.8 in the SRP r16a document.
326 static void srpt_get_svc_entries(u64 ioc_guid,
327 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
329 struct ib_dm_svc_entries *svc_entries;
333 if (!slot || slot > 16) {
335 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
339 if (slot > 2 || lo > hi || hi > 1) {
341 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
345 svc_entries = (struct ib_dm_svc_entries *)mad->data;
346 memset(svc_entries, 0, sizeof(*svc_entries));
347 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
348 snprintf(svc_entries->service_entries[0].name,
349 sizeof(svc_entries->service_entries[0].name),
351 SRP_SERVICE_NAME_PREFIX,
354 mad->mad_hdr.status = 0;
358 * srpt_mgmt_method_get() - Process a received management datagram.
359 * @sp: source port through which the MAD has been received.
360 * @rq_mad: received MAD.
361 * @rsp_mad: response MAD.
363 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
364 struct ib_dm_mad *rsp_mad)
370 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
372 case DM_ATTR_CLASS_PORT_INFO:
373 srpt_get_class_port_info(rsp_mad);
375 case DM_ATTR_IOU_INFO:
376 srpt_get_iou(rsp_mad);
378 case DM_ATTR_IOC_PROFILE:
379 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
380 srpt_get_ioc(sp, slot, rsp_mad);
382 case DM_ATTR_SVC_ENTRIES:
383 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
384 hi = (u8) ((slot >> 8) & 0xff);
385 lo = (u8) (slot & 0xff);
386 slot = (u16) ((slot >> 16) & 0xffff);
387 srpt_get_svc_entries(srpt_service_guid,
388 slot, hi, lo, rsp_mad);
391 rsp_mad->mad_hdr.status =
392 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
398 * srpt_mad_send_handler() - Post MAD-send callback function.
400 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
401 struct ib_mad_send_wc *mad_wc)
403 ib_destroy_ah(mad_wc->send_buf->ah);
404 ib_free_send_mad(mad_wc->send_buf);
408 * srpt_mad_recv_handler() - MAD reception callback function.
410 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
411 struct ib_mad_send_buf *send_buf,
412 struct ib_mad_recv_wc *mad_wc)
414 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
416 struct ib_mad_send_buf *rsp;
417 struct ib_dm_mad *dm_mad;
419 if (!mad_wc || !mad_wc->recv_buf.mad)
422 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
423 mad_wc->recv_buf.grh, mad_agent->port_num);
427 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
429 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
430 mad_wc->wc->pkey_index, 0,
431 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
433 IB_MGMT_BASE_VERSION);
440 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
441 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
442 dm_mad->mad_hdr.status = 0;
444 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
445 case IB_MGMT_METHOD_GET:
446 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
448 case IB_MGMT_METHOD_SET:
449 dm_mad->mad_hdr.status =
450 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
453 dm_mad->mad_hdr.status =
454 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
458 if (!ib_post_send_mad(rsp, NULL)) {
459 ib_free_recv_mad(mad_wc);
460 /* will destroy_ah & free_send_mad in send completion */
464 ib_free_send_mad(rsp);
469 ib_free_recv_mad(mad_wc);
473 * srpt_refresh_port() - Configure a HCA port.
475 * Enable InfiniBand management datagram processing, update the cached sm_lid,
476 * lid and gid values, and register a callback function for processing MADs
477 * on the specified port.
479 * Note: It is safe to call this function more than once for the same port.
481 static int srpt_refresh_port(struct srpt_port *sport)
483 struct ib_mad_reg_req reg_req;
484 struct ib_port_modify port_modify;
485 struct ib_port_attr port_attr;
488 memset(&port_modify, 0, sizeof(port_modify));
489 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
490 port_modify.clr_port_cap_mask = 0;
492 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
496 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
500 sport->sm_lid = port_attr.sm_lid;
501 sport->lid = port_attr.lid;
503 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
508 if (!sport->mad_agent) {
509 memset(®_req, 0, sizeof(reg_req));
510 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
511 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
512 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
513 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
515 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
519 srpt_mad_send_handler,
520 srpt_mad_recv_handler,
522 if (IS_ERR(sport->mad_agent)) {
523 ret = PTR_ERR(sport->mad_agent);
524 sport->mad_agent = NULL;
533 port_modify.set_port_cap_mask = 0;
534 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
535 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
543 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
545 * Note: It is safe to call this function more than once for the same device.
547 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
549 struct ib_port_modify port_modify = {
550 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
552 struct srpt_port *sport;
555 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
556 sport = &sdev->port[i - 1];
557 WARN_ON(sport->port != i);
558 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
559 pr_err("disabling MAD processing failed.\n");
560 if (sport->mad_agent) {
561 ib_unregister_mad_agent(sport->mad_agent);
562 sport->mad_agent = NULL;
568 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
570 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
571 int ioctx_size, int dma_size,
572 enum dma_data_direction dir)
574 struct srpt_ioctx *ioctx;
576 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
580 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
584 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
585 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
599 * srpt_free_ioctx() - Free an SRPT I/O context structure.
601 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
602 int dma_size, enum dma_data_direction dir)
607 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
613 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
614 * @sdev: Device to allocate the I/O context ring for.
615 * @ring_size: Number of elements in the I/O context ring.
616 * @ioctx_size: I/O context size.
617 * @dma_size: DMA buffer size.
618 * @dir: DMA data direction.
620 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
621 int ring_size, int ioctx_size,
622 int dma_size, enum dma_data_direction dir)
624 struct srpt_ioctx **ring;
627 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
628 && ioctx_size != sizeof(struct srpt_send_ioctx));
630 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
633 for (i = 0; i < ring_size; ++i) {
634 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
643 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
651 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
653 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
654 struct srpt_device *sdev, int ring_size,
655 int dma_size, enum dma_data_direction dir)
659 for (i = 0; i < ring_size; ++i)
660 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
665 * srpt_get_cmd_state() - Get the state of a SCSI command.
667 static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
669 enum srpt_command_state state;
674 spin_lock_irqsave(&ioctx->spinlock, flags);
675 state = ioctx->state;
676 spin_unlock_irqrestore(&ioctx->spinlock, flags);
681 * srpt_set_cmd_state() - Set the state of a SCSI command.
683 * Does not modify the state of aborted commands. Returns the previous command
686 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
687 enum srpt_command_state new)
689 enum srpt_command_state previous;
694 spin_lock_irqsave(&ioctx->spinlock, flags);
695 previous = ioctx->state;
696 if (previous != SRPT_STATE_DONE)
698 spin_unlock_irqrestore(&ioctx->spinlock, flags);
704 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
706 * Returns true if and only if the previous command state was equal to 'old'.
708 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
709 enum srpt_command_state old,
710 enum srpt_command_state new)
712 enum srpt_command_state previous;
716 WARN_ON(old == SRPT_STATE_DONE);
717 WARN_ON(new == SRPT_STATE_NEW);
719 spin_lock_irqsave(&ioctx->spinlock, flags);
720 previous = ioctx->state;
723 spin_unlock_irqrestore(&ioctx->spinlock, flags);
724 return previous == old;
728 * srpt_post_recv() - Post an IB receive request.
730 static int srpt_post_recv(struct srpt_device *sdev,
731 struct srpt_recv_ioctx *ioctx)
734 struct ib_recv_wr wr, *bad_wr;
737 list.addr = ioctx->ioctx.dma;
738 list.length = srp_max_req_size;
739 list.lkey = sdev->pd->local_dma_lkey;
741 ioctx->ioctx.cqe.done = srpt_recv_done;
742 wr.wr_cqe = &ioctx->ioctx.cqe;
747 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
751 * srpt_post_send() - Post an IB send request.
753 * Returns zero upon success and a non-zero value upon failure.
755 static int srpt_post_send(struct srpt_rdma_ch *ch,
756 struct srpt_send_ioctx *ioctx, int len)
759 struct ib_send_wr wr, *bad_wr;
760 struct srpt_device *sdev = ch->sport->sdev;
763 atomic_inc(&ch->req_lim);
766 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
767 pr_warn("IB send queue full (needed 1)\n");
771 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
774 list.addr = ioctx->ioctx.dma;
776 list.lkey = sdev->pd->local_dma_lkey;
778 ioctx->ioctx.cqe.done = srpt_send_done;
780 wr.wr_cqe = &ioctx->ioctx.cqe;
783 wr.opcode = IB_WR_SEND;
784 wr.send_flags = IB_SEND_SIGNALED;
786 ret = ib_post_send(ch->qp, &wr, &bad_wr);
790 atomic_inc(&ch->sq_wr_avail);
791 atomic_dec(&ch->req_lim);
797 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
798 * @ioctx: Pointer to the I/O context associated with the request.
799 * @srp_cmd: Pointer to the SRP_CMD request data.
800 * @dir: Pointer to the variable to which the transfer direction will be
802 * @data_len: Pointer to the variable to which the total data length of all
803 * descriptors in the SRP_CMD request will be written.
805 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
807 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
808 * -ENOMEM when memory allocation fails and zero upon success.
810 static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
811 struct srp_cmd *srp_cmd,
812 enum dma_data_direction *dir, u64 *data_len)
814 struct srp_indirect_buf *idb;
815 struct srp_direct_buf *db;
816 unsigned add_cdb_offset;
820 * The pointer computations below will only be compiled correctly
821 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
822 * whether srp_cmd::add_data has been declared as a byte pointer.
824 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
825 && !__same_type(srp_cmd->add_data[0], (u8)0));
834 * The lower four bits of the buffer format field contain the DATA-IN
835 * buffer descriptor format, and the highest four bits contain the
836 * DATA-OUT buffer descriptor format.
839 if (srp_cmd->buf_fmt & 0xf)
840 /* DATA-IN: transfer data from target to initiator (read). */
841 *dir = DMA_FROM_DEVICE;
842 else if (srp_cmd->buf_fmt >> 4)
843 /* DATA-OUT: transfer data from initiator to target (write). */
844 *dir = DMA_TO_DEVICE;
847 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
848 * CDB LENGTH' field are reserved and the size in bytes of this field
849 * is four times the value specified in bits 3..7. Hence the "& ~3".
851 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
852 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
853 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
855 ioctx->rbufs = &ioctx->single_rbuf;
857 db = (struct srp_direct_buf *)(srp_cmd->add_data
859 memcpy(ioctx->rbufs, db, sizeof(*db));
860 *data_len = be32_to_cpu(db->len);
861 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
862 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
863 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
866 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof(*db);
869 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
870 pr_err("received unsupported SRP_CMD request"
871 " type (%u out + %u in != %u / %zu)\n",
872 srp_cmd->data_out_desc_cnt,
873 srp_cmd->data_in_desc_cnt,
874 be32_to_cpu(idb->table_desc.len),
881 if (ioctx->n_rbuf == 1)
882 ioctx->rbufs = &ioctx->single_rbuf;
885 kmalloc(ioctx->n_rbuf * sizeof(*db), GFP_ATOMIC);
894 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof(*db));
895 *data_len = be32_to_cpu(idb->len);
902 * srpt_init_ch_qp() - Initialize queue pair attributes.
904 * Initialized the attributes of queue pair 'qp' by allowing local write,
905 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
907 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
909 struct ib_qp_attr *attr;
912 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
916 attr->qp_state = IB_QPS_INIT;
917 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
918 IB_ACCESS_REMOTE_WRITE;
919 attr->port_num = ch->sport->port;
920 attr->pkey_index = 0;
922 ret = ib_modify_qp(qp, attr,
923 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
931 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
932 * @ch: channel of the queue pair.
933 * @qp: queue pair to change the state of.
935 * Returns zero upon success and a negative value upon failure.
937 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
938 * If this structure ever becomes larger, it might be necessary to allocate
939 * it dynamically instead of on the stack.
941 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
943 struct ib_qp_attr qp_attr;
947 qp_attr.qp_state = IB_QPS_RTR;
948 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
952 qp_attr.max_dest_rd_atomic = 4;
954 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
961 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
962 * @ch: channel of the queue pair.
963 * @qp: queue pair to change the state of.
965 * Returns zero upon success and a negative value upon failure.
967 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
968 * If this structure ever becomes larger, it might be necessary to allocate
969 * it dynamically instead of on the stack.
971 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
973 struct ib_qp_attr qp_attr;
977 qp_attr.qp_state = IB_QPS_RTS;
978 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
982 qp_attr.max_rd_atomic = 4;
984 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
991 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
993 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
995 struct ib_qp_attr qp_attr;
997 qp_attr.qp_state = IB_QPS_ERR;
998 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1002 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1004 static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1005 struct srpt_send_ioctx *ioctx)
1007 struct scatterlist *sg;
1008 enum dma_data_direction dir;
1012 BUG_ON(ioctx->n_rdma && !ioctx->rdma_wrs);
1014 while (ioctx->n_rdma)
1015 kfree(ioctx->rdma_wrs[--ioctx->n_rdma].wr.sg_list);
1017 kfree(ioctx->rdma_wrs);
1018 ioctx->rdma_wrs = NULL;
1020 if (ioctx->mapped_sg_count) {
1023 dir = ioctx->cmd.data_direction;
1024 BUG_ON(dir == DMA_NONE);
1025 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1026 target_reverse_dma_direction(&ioctx->cmd));
1027 ioctx->mapped_sg_count = 0;
1032 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1034 static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1035 struct srpt_send_ioctx *ioctx)
1037 struct ib_device *dev = ch->sport->sdev->device;
1039 struct scatterlist *sg, *sg_orig;
1041 enum dma_data_direction dir;
1042 struct ib_rdma_wr *riu;
1043 struct srp_direct_buf *db;
1044 dma_addr_t dma_addr;
1056 dir = cmd->data_direction;
1057 BUG_ON(dir == DMA_NONE);
1059 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1060 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
1062 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1063 target_reverse_dma_direction(cmd));
1064 if (unlikely(!count))
1067 ioctx->mapped_sg_count = count;
1069 if (ioctx->rdma_wrs && ioctx->n_rdma_wrs)
1070 nrdma = ioctx->n_rdma_wrs;
1072 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1075 ioctx->rdma_wrs = kcalloc(nrdma, sizeof(*ioctx->rdma_wrs),
1077 if (!ioctx->rdma_wrs)
1080 ioctx->n_rdma_wrs = nrdma;
1084 tsize = cmd->data_length;
1085 dma_len = ib_sg_dma_len(dev, &sg[0]);
1086 riu = ioctx->rdma_wrs;
1089 * For each remote desc - calculate the #ib_sge.
1090 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1091 * each remote desc rdma_iu is required a rdma wr;
1093 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1097 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1098 rsize = be32_to_cpu(db->len);
1099 raddr = be64_to_cpu(db->va);
1100 riu->remote_addr = raddr;
1101 riu->rkey = be32_to_cpu(db->key);
1102 riu->wr.num_sge = 0;
1104 /* calculate how many sge required for this remote_buf */
1105 while (rsize > 0 && tsize > 0) {
1107 if (rsize >= dma_len) {
1116 dma_len = ib_sg_dma_len(
1129 riu->wr.num_sge == SRPT_DEF_SG_PER_WQE) {
1131 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1132 sizeof(*riu->wr.sg_list),
1134 if (!riu->wr.sg_list)
1138 riu->wr.num_sge = 0;
1139 riu->remote_addr = raddr;
1140 riu->rkey = be32_to_cpu(db->key);
1145 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1146 sizeof(*riu->wr.sg_list),
1148 if (!riu->wr.sg_list)
1153 tsize = cmd->data_length;
1154 riu = ioctx->rdma_wrs;
1156 dma_len = ib_sg_dma_len(dev, &sg[0]);
1157 dma_addr = ib_sg_dma_address(dev, &sg[0]);
1159 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1161 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1162 rsize = be32_to_cpu(db->len);
1163 sge = riu->wr.sg_list;
1166 while (rsize > 0 && tsize > 0) {
1167 sge->addr = dma_addr;
1168 sge->lkey = ch->sport->sdev->pd->local_dma_lkey;
1170 if (rsize >= dma_len) {
1172 (tsize < dma_len) ? tsize : dma_len;
1180 dma_len = ib_sg_dma_len(
1182 dma_addr = ib_sg_dma_address(
1187 sge->length = (tsize < rsize) ? tsize : rsize;
1195 if (k == riu->wr.num_sge && rsize > 0 && tsize > 0) {
1197 sge = riu->wr.sg_list;
1199 } else if (rsize > 0 && tsize > 0)
1207 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1213 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1215 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1217 struct srpt_send_ioctx *ioctx;
1218 unsigned long flags;
1223 spin_lock_irqsave(&ch->spinlock, flags);
1224 if (!list_empty(&ch->free_list)) {
1225 ioctx = list_first_entry(&ch->free_list,
1226 struct srpt_send_ioctx, free_list);
1227 list_del(&ioctx->free_list);
1229 spin_unlock_irqrestore(&ch->spinlock, flags);
1234 BUG_ON(ioctx->ch != ch);
1235 spin_lock_init(&ioctx->spinlock);
1236 ioctx->state = SRPT_STATE_NEW;
1238 ioctx->rbufs = NULL;
1240 ioctx->n_rdma_wrs = 0;
1241 ioctx->rdma_wrs = NULL;
1242 ioctx->mapped_sg_count = 0;
1243 init_completion(&ioctx->tx_done);
1244 ioctx->queue_status_only = false;
1246 * transport_init_se_cmd() does not initialize all fields, so do it
1249 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1250 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1256 * srpt_abort_cmd() - Abort a SCSI command.
1257 * @ioctx: I/O context associated with the SCSI command.
1258 * @context: Preferred execution context.
1260 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1262 enum srpt_command_state state;
1263 unsigned long flags;
1268 * If the command is in a state where the target core is waiting for
1269 * the ib_srpt driver, change the state to the next state. Changing
1270 * the state of the command from SRPT_STATE_NEED_DATA to
1271 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1272 * function a second time.
1275 spin_lock_irqsave(&ioctx->spinlock, flags);
1276 state = ioctx->state;
1278 case SRPT_STATE_NEED_DATA:
1279 ioctx->state = SRPT_STATE_DATA_IN;
1281 case SRPT_STATE_DATA_IN:
1282 case SRPT_STATE_CMD_RSP_SENT:
1283 case SRPT_STATE_MGMT_RSP_SENT:
1284 ioctx->state = SRPT_STATE_DONE;
1289 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1291 if (state == SRPT_STATE_DONE) {
1292 struct srpt_rdma_ch *ch = ioctx->ch;
1294 BUG_ON(ch->sess == NULL);
1296 target_put_sess_cmd(&ioctx->cmd);
1300 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1304 case SRPT_STATE_NEW:
1305 case SRPT_STATE_DATA_IN:
1306 case SRPT_STATE_MGMT:
1308 * Do nothing - defer abort processing until
1309 * srpt_queue_response() is invoked.
1311 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1313 case SRPT_STATE_NEED_DATA:
1314 /* DMA_TO_DEVICE (write) - RDMA read error. */
1316 /* XXX(hch): this is a horrible layering violation.. */
1317 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1318 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
1319 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
1321 case SRPT_STATE_CMD_RSP_SENT:
1323 * SRP_RSP sending failed or the SRP_RSP send completion has
1324 * not been received in time.
1326 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
1327 target_put_sess_cmd(&ioctx->cmd);
1329 case SRPT_STATE_MGMT_RSP_SENT:
1330 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1331 target_put_sess_cmd(&ioctx->cmd);
1334 WARN(1, "Unexpected command state (%d)", state);
1343 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1344 * the data that has been transferred via IB RDMA had to be postponed until the
1345 * check_stop_free() callback. None of this is necessary anymore and needs to
1348 static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1350 struct srpt_rdma_ch *ch = cq->cq_context;
1351 struct srpt_send_ioctx *ioctx =
1352 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
1354 WARN_ON(ioctx->n_rdma <= 0);
1355 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1357 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1358 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1360 srpt_abort_cmd(ioctx);
1364 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1365 SRPT_STATE_DATA_IN))
1366 target_execute_cmd(&ioctx->cmd);
1368 pr_err("%s[%d]: wrong state = %d\n", __func__,
1369 __LINE__, srpt_get_cmd_state(ioctx));
1372 static void srpt_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
1374 struct srpt_send_ioctx *ioctx =
1375 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
1377 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1378 pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n",
1380 srpt_abort_cmd(ioctx);
1385 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1386 * @ch: RDMA channel through which the request has been received.
1387 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1388 * be built in the buffer ioctx->buf points at and hence this function will
1389 * overwrite the request data.
1390 * @tag: tag of the request for which this response is being generated.
1391 * @status: value for the STATUS field of the SRP_RSP information unit.
1393 * Returns the size in bytes of the SRP_RSP response.
1395 * An SRP_RSP response contains a SCSI status or service response. See also
1396 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1397 * response. See also SPC-2 for more information about sense data.
1399 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1400 struct srpt_send_ioctx *ioctx, u64 tag,
1403 struct srp_rsp *srp_rsp;
1404 const u8 *sense_data;
1405 int sense_data_len, max_sense_len;
1408 * The lowest bit of all SAM-3 status codes is zero (see also
1409 * paragraph 5.3 in SAM-3).
1411 WARN_ON(status & 1);
1413 srp_rsp = ioctx->ioctx.buf;
1416 sense_data = ioctx->sense_data;
1417 sense_data_len = ioctx->cmd.scsi_sense_length;
1418 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1420 memset(srp_rsp, 0, sizeof(*srp_rsp));
1421 srp_rsp->opcode = SRP_RSP;
1422 srp_rsp->req_lim_delta =
1423 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1425 srp_rsp->status = status;
1427 if (sense_data_len) {
1428 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1429 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1430 if (sense_data_len > max_sense_len) {
1431 pr_warn("truncated sense data from %d to %d"
1432 " bytes\n", sense_data_len, max_sense_len);
1433 sense_data_len = max_sense_len;
1436 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1437 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1438 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1441 return sizeof(*srp_rsp) + sense_data_len;
1445 * srpt_build_tskmgmt_rsp() - Build a task management response.
1446 * @ch: RDMA channel through which the request has been received.
1447 * @ioctx: I/O context in which the SRP_RSP response will be built.
1448 * @rsp_code: RSP_CODE that will be stored in the response.
1449 * @tag: Tag of the request for which this response is being generated.
1451 * Returns the size in bytes of the SRP_RSP response.
1453 * An SRP_RSP response contains a SCSI status or service response. See also
1454 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1457 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1458 struct srpt_send_ioctx *ioctx,
1459 u8 rsp_code, u64 tag)
1461 struct srp_rsp *srp_rsp;
1466 resp_len = sizeof(*srp_rsp) + resp_data_len;
1468 srp_rsp = ioctx->ioctx.buf;
1470 memset(srp_rsp, 0, sizeof(*srp_rsp));
1472 srp_rsp->opcode = SRP_RSP;
1473 srp_rsp->req_lim_delta =
1474 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1477 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1478 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1479 srp_rsp->data[3] = rsp_code;
1484 static int srpt_check_stop_free(struct se_cmd *cmd)
1486 struct srpt_send_ioctx *ioctx = container_of(cmd,
1487 struct srpt_send_ioctx, cmd);
1489 return target_put_sess_cmd(&ioctx->cmd);
1493 * srpt_handle_cmd() - Process SRP_CMD.
1495 static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1496 struct srpt_recv_ioctx *recv_ioctx,
1497 struct srpt_send_ioctx *send_ioctx)
1500 struct srp_cmd *srp_cmd;
1502 enum dma_data_direction dir;
1506 BUG_ON(!send_ioctx);
1508 srp_cmd = recv_ioctx->ioctx.buf;
1509 cmd = &send_ioctx->cmd;
1510 cmd->tag = srp_cmd->tag;
1512 switch (srp_cmd->task_attr) {
1513 case SRP_CMD_SIMPLE_Q:
1514 cmd->sam_task_attr = TCM_SIMPLE_TAG;
1516 case SRP_CMD_ORDERED_Q:
1518 cmd->sam_task_attr = TCM_ORDERED_TAG;
1520 case SRP_CMD_HEAD_OF_Q:
1521 cmd->sam_task_attr = TCM_HEAD_TAG;
1524 cmd->sam_task_attr = TCM_ACA_TAG;
1528 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
1529 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1531 ret = TCM_INVALID_CDB_FIELD;
1535 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1536 &send_ioctx->sense_data[0],
1537 scsilun_to_int(&srp_cmd->lun), data_len,
1538 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
1540 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1546 transport_send_check_condition_and_sense(cmd, ret, 0);
1550 static int srp_tmr_to_tcm(int fn)
1553 case SRP_TSK_ABORT_TASK:
1554 return TMR_ABORT_TASK;
1555 case SRP_TSK_ABORT_TASK_SET:
1556 return TMR_ABORT_TASK_SET;
1557 case SRP_TSK_CLEAR_TASK_SET:
1558 return TMR_CLEAR_TASK_SET;
1559 case SRP_TSK_LUN_RESET:
1560 return TMR_LUN_RESET;
1561 case SRP_TSK_CLEAR_ACA:
1562 return TMR_CLEAR_ACA;
1569 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1571 * Returns 0 if and only if the request will be processed by the target core.
1573 * For more information about SRP_TSK_MGMT information units, see also section
1574 * 6.7 in the SRP r16a document.
1576 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1577 struct srpt_recv_ioctx *recv_ioctx,
1578 struct srpt_send_ioctx *send_ioctx)
1580 struct srp_tsk_mgmt *srp_tsk;
1582 struct se_session *sess = ch->sess;
1586 BUG_ON(!send_ioctx);
1588 srp_tsk = recv_ioctx->ioctx.buf;
1589 cmd = &send_ioctx->cmd;
1591 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1592 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1593 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1595 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1596 send_ioctx->cmd.tag = srp_tsk->tag;
1597 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1598 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1599 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1600 GFP_KERNEL, srp_tsk->task_tag,
1601 TARGET_SCF_ACK_KREF);
1603 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1608 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
1612 * srpt_handle_new_iu() - Process a newly received information unit.
1613 * @ch: RDMA channel through which the information unit has been received.
1614 * @ioctx: SRPT I/O context associated with the information unit.
1616 static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1617 struct srpt_recv_ioctx *recv_ioctx,
1618 struct srpt_send_ioctx *send_ioctx)
1620 struct srp_cmd *srp_cmd;
1623 BUG_ON(!recv_ioctx);
1625 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1626 recv_ioctx->ioctx.dma, srp_max_req_size,
1629 if (unlikely(ch->state == CH_CONNECTING)) {
1630 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1634 if (unlikely(ch->state != CH_LIVE))
1637 srp_cmd = recv_ioctx->ioctx.buf;
1638 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1640 send_ioctx = srpt_get_send_ioctx(ch);
1641 if (unlikely(!send_ioctx)) {
1642 list_add_tail(&recv_ioctx->wait_list,
1643 &ch->cmd_wait_list);
1648 switch (srp_cmd->opcode) {
1650 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1653 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1656 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
1659 pr_debug("received SRP_CRED_RSP\n");
1662 pr_debug("received SRP_AER_RSP\n");
1665 pr_err("Received SRP_RSP\n");
1668 pr_err("received IU with unknown opcode 0x%x\n",
1673 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1678 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1680 struct srpt_rdma_ch *ch = cq->cq_context;
1681 struct srpt_recv_ioctx *ioctx =
1682 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
1684 if (wc->status == IB_WC_SUCCESS) {
1687 req_lim = atomic_dec_return(&ch->req_lim);
1688 if (unlikely(req_lim < 0))
1689 pr_err("req_lim = %d < 0\n", req_lim);
1690 srpt_handle_new_iu(ch, ioctx, NULL);
1692 pr_info("receiving failed for ioctx %p with status %d\n",
1698 * Note: Although this has not yet been observed during tests, at least in
1699 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1700 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1701 * value in each response is set to one, and it is possible that this response
1702 * makes the initiator send a new request before the send completion for that
1703 * response has been processed. This could e.g. happen if the call to
1704 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1705 * if IB retransmission causes generation of the send completion to be
1706 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1707 * are queued on cmd_wait_list. The code below processes these delayed
1708 * requests one at a time.
1710 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
1712 struct srpt_rdma_ch *ch = cq->cq_context;
1713 struct srpt_send_ioctx *ioctx =
1714 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1715 enum srpt_command_state state;
1717 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1719 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1720 state != SRPT_STATE_MGMT_RSP_SENT);
1722 atomic_inc(&ch->sq_wr_avail);
1724 if (wc->status != IB_WC_SUCCESS) {
1725 pr_info("sending response for ioctx 0x%p failed"
1726 " with status %d\n", ioctx, wc->status);
1728 atomic_dec(&ch->req_lim);
1729 srpt_abort_cmd(ioctx);
1733 if (state != SRPT_STATE_DONE) {
1734 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1735 transport_generic_free_cmd(&ioctx->cmd, 0);
1737 pr_err("IB completion has been received too late for"
1738 " wr_id = %u.\n", ioctx->ioctx.index);
1742 while (!list_empty(&ch->cmd_wait_list) &&
1743 ch->state == CH_LIVE &&
1744 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
1745 struct srpt_recv_ioctx *recv_ioctx;
1747 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1748 struct srpt_recv_ioctx,
1750 list_del(&recv_ioctx->wait_list);
1751 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
1756 * srpt_create_ch_ib() - Create receive and send completion queues.
1758 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1760 struct ib_qp_init_attr *qp_init;
1761 struct srpt_port *sport = ch->sport;
1762 struct srpt_device *sdev = sport->sdev;
1763 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1766 WARN_ON(ch->rq_size < 1);
1769 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
1774 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1775 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
1776 if (IS_ERR(ch->cq)) {
1777 ret = PTR_ERR(ch->cq);
1778 pr_err("failed to create CQ cqe= %d ret= %d\n",
1779 ch->rq_size + srp_sq_size, ret);
1783 qp_init->qp_context = (void *)ch;
1784 qp_init->event_handler
1785 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1786 qp_init->send_cq = ch->cq;
1787 qp_init->recv_cq = ch->cq;
1788 qp_init->srq = sdev->srq;
1789 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1790 qp_init->qp_type = IB_QPT_RC;
1791 qp_init->cap.max_send_wr = srp_sq_size;
1792 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1794 ch->qp = ib_create_qp(sdev->pd, qp_init);
1795 if (IS_ERR(ch->qp)) {
1796 ret = PTR_ERR(ch->qp);
1797 if (ret == -ENOMEM) {
1799 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1800 ib_destroy_cq(ch->cq);
1804 pr_err("failed to create_qp ret= %d\n", ret);
1805 goto err_destroy_cq;
1808 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1810 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1811 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1812 qp_init->cap.max_send_wr, ch->cm_id);
1814 ret = srpt_init_ch_qp(ch, ch->qp);
1816 goto err_destroy_qp;
1823 ib_destroy_qp(ch->qp);
1829 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1831 ib_destroy_qp(ch->qp);
1836 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
1838 * Reset the QP and make sure all resources associated with the channel will
1839 * be deallocated at an appropriate time.
1841 * Note: The caller must hold ch->sport->sdev->spinlock.
1843 static void __srpt_close_ch(struct srpt_rdma_ch *ch)
1845 enum rdma_ch_state prev_state;
1846 unsigned long flags;
1848 spin_lock_irqsave(&ch->spinlock, flags);
1849 prev_state = ch->state;
1850 switch (prev_state) {
1853 ch->state = CH_DISCONNECTING;
1858 spin_unlock_irqrestore(&ch->spinlock, flags);
1860 switch (prev_state) {
1862 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
1866 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
1867 pr_err("sending CM DREQ failed.\n");
1869 case CH_DISCONNECTING:
1878 * srpt_close_ch() - Close an RDMA channel.
1880 static void srpt_close_ch(struct srpt_rdma_ch *ch)
1882 struct srpt_device *sdev;
1884 sdev = ch->sport->sdev;
1885 spin_lock_irq(&sdev->spinlock);
1886 __srpt_close_ch(ch);
1887 spin_unlock_irq(&sdev->spinlock);
1891 * srpt_shutdown_session() - Whether or not a session may be shut down.
1893 static int srpt_shutdown_session(struct se_session *se_sess)
1899 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
1900 * @cm_id: Pointer to the CM ID of the channel to be drained.
1902 * Note: Must be called from inside srpt_cm_handler to avoid a race between
1903 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
1904 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
1905 * waits until all target sessions for the associated IB device have been
1906 * unregistered and target session registration involves a call to
1907 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
1908 * this function has finished).
1910 static void srpt_drain_channel(struct ib_cm_id *cm_id)
1912 struct srpt_device *sdev;
1913 struct srpt_rdma_ch *ch;
1915 bool do_reset = false;
1917 WARN_ON_ONCE(irqs_disabled());
1919 sdev = cm_id->context;
1921 spin_lock_irq(&sdev->spinlock);
1922 list_for_each_entry(ch, &sdev->rch_list, list) {
1923 if (ch->cm_id == cm_id) {
1924 do_reset = srpt_set_ch_state(ch, CH_DRAINING);
1928 spin_unlock_irq(&sdev->spinlock);
1932 srpt_shutdown_session(ch->sess);
1934 ret = srpt_ch_qp_err(ch);
1936 pr_err("Setting queue pair in error state"
1937 " failed: %d\n", ret);
1942 * srpt_find_channel() - Look up an RDMA channel.
1943 * @cm_id: Pointer to the CM ID of the channel to be looked up.
1945 * Return NULL if no matching RDMA channel has been found.
1947 static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
1948 struct ib_cm_id *cm_id)
1950 struct srpt_rdma_ch *ch;
1953 WARN_ON_ONCE(irqs_disabled());
1957 spin_lock_irq(&sdev->spinlock);
1958 list_for_each_entry(ch, &sdev->rch_list, list) {
1959 if (ch->cm_id == cm_id) {
1964 spin_unlock_irq(&sdev->spinlock);
1966 return found ? ch : NULL;
1970 * srpt_release_channel() - Release channel resources.
1972 * Schedules the actual release because:
1973 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
1974 * trigger a deadlock.
1975 * - It is not safe to call TCM transport_* functions from interrupt context.
1977 static void srpt_release_channel(struct srpt_rdma_ch *ch)
1979 schedule_work(&ch->release_work);
1982 static void srpt_release_channel_work(struct work_struct *w)
1984 struct srpt_rdma_ch *ch;
1985 struct srpt_device *sdev;
1986 struct se_session *se_sess;
1988 ch = container_of(w, struct srpt_rdma_ch, release_work);
1989 pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name,
1990 ch->qp->qp_num, ch->release_done);
1992 sdev = ch->sport->sdev;
1998 target_sess_cmd_list_set_waiting(se_sess);
1999 target_wait_for_sess_cmds(se_sess);
2001 transport_deregister_session_configfs(se_sess);
2002 transport_deregister_session(se_sess);
2005 ib_destroy_cm_id(ch->cm_id);
2007 srpt_destroy_ch_ib(ch);
2009 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2010 ch->sport->sdev, ch->rq_size,
2011 ch->rsp_size, DMA_TO_DEVICE);
2013 spin_lock_irq(&sdev->spinlock);
2014 list_del_init(&ch->list);
2015 if (ch->release_done)
2016 complete(ch->release_done);
2017 spin_unlock_irq(&sdev->spinlock);
2019 wake_up(&sdev->ch_releaseQ);
2025 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2027 * Ownership of the cm_id is transferred to the target session if this
2028 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2030 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2031 struct ib_cm_req_event_param *param,
2034 struct srpt_device *sdev = cm_id->context;
2035 struct srpt_port *sport = &sdev->port[param->port - 1];
2036 struct srp_login_req *req;
2037 struct srp_login_rsp *rsp;
2038 struct srp_login_rej *rej;
2039 struct ib_cm_rep_param *rep_param;
2040 struct srpt_rdma_ch *ch, *tmp_ch;
2041 struct se_node_acl *se_acl;
2046 WARN_ON_ONCE(irqs_disabled());
2048 if (WARN_ON(!sdev || !private_data))
2051 req = (struct srp_login_req *)private_data;
2053 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2055 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2056 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2057 " (guid=0x%llx:0x%llx)\n",
2058 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2059 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2060 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2061 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2064 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2065 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
2067 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2068 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2069 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
2071 if (!rsp || !rej || !rep_param) {
2076 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2077 rej->reason = cpu_to_be32(
2078 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2080 pr_err("rejected SRP_LOGIN_REQ because its"
2081 " length (%d bytes) is out of range (%d .. %d)\n",
2082 it_iu_len, 64, srp_max_req_size);
2086 if (!sport->enabled) {
2087 rej->reason = cpu_to_be32(
2088 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2090 pr_err("rejected SRP_LOGIN_REQ because the target port"
2091 " has not yet been enabled\n");
2095 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2096 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2098 spin_lock_irq(&sdev->spinlock);
2100 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2101 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2102 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2103 && param->port == ch->sport->port
2104 && param->listen_id == ch->sport->sdev->cm_id
2106 if (ch->state != CH_CONNECTING
2107 && ch->state != CH_LIVE)
2110 /* found an existing channel */
2111 pr_debug("Found existing channel %s"
2112 " cm_id= %p state= %d\n",
2113 ch->sess_name, ch->cm_id, ch->state);
2115 __srpt_close_ch(ch);
2118 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2122 spin_unlock_irq(&sdev->spinlock);
2125 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2127 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2128 || *(__be64 *)(req->target_port_id + 8) !=
2129 cpu_to_be64(srpt_service_guid)) {
2130 rej->reason = cpu_to_be32(
2131 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2133 pr_err("rejected SRP_LOGIN_REQ because it"
2134 " has an invalid target port identifier.\n");
2138 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
2140 rej->reason = cpu_to_be32(
2141 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2142 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
2147 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2148 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2149 memcpy(ch->t_port_id, req->target_port_id, 16);
2150 ch->sport = &sdev->port[param->port - 1];
2153 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2154 * for the SRP protocol to the command queue size.
2156 ch->rq_size = SRPT_RQ_SIZE;
2157 spin_lock_init(&ch->spinlock);
2158 ch->state = CH_CONNECTING;
2159 INIT_LIST_HEAD(&ch->cmd_wait_list);
2160 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2162 ch->ioctx_ring = (struct srpt_send_ioctx **)
2163 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2164 sizeof(*ch->ioctx_ring[0]),
2165 ch->rsp_size, DMA_TO_DEVICE);
2166 if (!ch->ioctx_ring)
2169 INIT_LIST_HEAD(&ch->free_list);
2170 for (i = 0; i < ch->rq_size; i++) {
2171 ch->ioctx_ring[i]->ch = ch;
2172 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2175 ret = srpt_create_ch_ib(ch);
2177 rej->reason = cpu_to_be32(
2178 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2179 pr_err("rejected SRP_LOGIN_REQ because creating"
2180 " a new RDMA channel failed.\n");
2184 ret = srpt_ch_qp_rtr(ch, ch->qp);
2186 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2187 pr_err("rejected SRP_LOGIN_REQ because enabling"
2188 " RTR failed (error code = %d)\n", ret);
2193 * Use the initator port identifier as the session name, when
2194 * checking against se_node_acl->initiatorname[] this can be
2195 * with or without preceeding '0x'.
2197 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2198 be64_to_cpu(*(__be64 *)ch->i_port_id),
2199 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2201 pr_debug("registering session %s\n", ch->sess_name);
2202 p = &ch->sess_name[0];
2204 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
2205 if (IS_ERR(ch->sess)) {
2206 rej->reason = cpu_to_be32(
2207 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2208 pr_debug("Failed to create session\n");
2213 se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p);
2215 pr_info("Rejected login because no ACL has been"
2216 " configured yet for initiator %s.\n", ch->sess_name);
2218 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2220 if (p == &ch->sess_name[0]) {
2224 rej->reason = cpu_to_be32(
2225 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2226 transport_free_session(ch->sess);
2229 ch->sess->se_node_acl = se_acl;
2231 transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch);
2233 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2234 ch->sess_name, ch->cm_id);
2236 /* create srp_login_response */
2237 rsp->opcode = SRP_LOGIN_RSP;
2238 rsp->tag = req->tag;
2239 rsp->max_it_iu_len = req->req_it_iu_len;
2240 rsp->max_ti_iu_len = req->req_it_iu_len;
2241 ch->max_ti_iu_len = it_iu_len;
2242 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2243 | SRP_BUF_FORMAT_INDIRECT);
2244 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2245 atomic_set(&ch->req_lim, ch->rq_size);
2246 atomic_set(&ch->req_lim_delta, 0);
2248 /* create cm reply */
2249 rep_param->qp_num = ch->qp->qp_num;
2250 rep_param->private_data = (void *)rsp;
2251 rep_param->private_data_len = sizeof(*rsp);
2252 rep_param->rnr_retry_count = 7;
2253 rep_param->flow_control = 1;
2254 rep_param->failover_accepted = 0;
2256 rep_param->responder_resources = 4;
2257 rep_param->initiator_depth = 4;
2259 ret = ib_send_cm_rep(cm_id, rep_param);
2261 pr_err("sending SRP_LOGIN_REQ response failed"
2262 " (error code = %d)\n", ret);
2263 goto release_channel;
2266 spin_lock_irq(&sdev->spinlock);
2267 list_add_tail(&ch->list, &sdev->rch_list);
2268 spin_unlock_irq(&sdev->spinlock);
2273 srpt_set_ch_state(ch, CH_RELEASING);
2274 transport_deregister_session_configfs(ch->sess);
2275 transport_deregister_session(ch->sess);
2279 srpt_destroy_ch_ib(ch);
2282 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2283 ch->sport->sdev, ch->rq_size,
2284 ch->rsp_size, DMA_TO_DEVICE);
2289 rej->opcode = SRP_LOGIN_REJ;
2290 rej->tag = req->tag;
2291 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2292 | SRP_BUF_FORMAT_INDIRECT);
2294 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2295 (void *)rej, sizeof(*rej));
2305 static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2307 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
2308 srpt_drain_channel(cm_id);
2312 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2314 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2315 * and that the recipient may begin transmitting (RTU = ready to use).
2317 static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2319 struct srpt_rdma_ch *ch;
2322 ch = srpt_find_channel(cm_id->context, cm_id);
2325 if (srpt_set_ch_state(ch, CH_LIVE)) {
2326 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2328 ret = srpt_ch_qp_rts(ch, ch->qp);
2330 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2332 list_del(&ioctx->wait_list);
2333 srpt_handle_new_iu(ch, ioctx, NULL);
2340 static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2342 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
2343 srpt_drain_channel(cm_id);
2346 static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2348 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
2349 srpt_drain_channel(cm_id);
2353 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2355 static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2357 struct srpt_rdma_ch *ch;
2358 unsigned long flags;
2359 bool send_drep = false;
2361 ch = srpt_find_channel(cm_id->context, cm_id);
2364 pr_debug("cm_id= %p ch->state= %d\n", cm_id, ch->state);
2366 spin_lock_irqsave(&ch->spinlock, flags);
2367 switch (ch->state) {
2371 ch->state = CH_DISCONNECTING;
2373 case CH_DISCONNECTING:
2376 WARN(true, "unexpected channel state %d\n", ch->state);
2379 spin_unlock_irqrestore(&ch->spinlock, flags);
2382 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
2383 pr_err("Sending IB DREP failed.\n");
2384 pr_info("Received DREQ and sent DREP for session %s.\n",
2390 * srpt_cm_drep_recv() - Process reception of a DREP message.
2392 static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2394 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
2395 srpt_drain_channel(cm_id);
2399 * srpt_cm_handler() - IB connection manager callback function.
2401 * A non-zero return value will cause the caller destroy the CM ID.
2403 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2404 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2405 * a non-zero value in any other case will trigger a race with the
2406 * ib_destroy_cm_id() call in srpt_release_channel().
2408 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2413 switch (event->event) {
2414 case IB_CM_REQ_RECEIVED:
2415 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2416 event->private_data);
2418 case IB_CM_REJ_RECEIVED:
2419 srpt_cm_rej_recv(cm_id);
2421 case IB_CM_RTU_RECEIVED:
2422 case IB_CM_USER_ESTABLISHED:
2423 srpt_cm_rtu_recv(cm_id);
2425 case IB_CM_DREQ_RECEIVED:
2426 srpt_cm_dreq_recv(cm_id);
2428 case IB_CM_DREP_RECEIVED:
2429 srpt_cm_drep_recv(cm_id);
2431 case IB_CM_TIMEWAIT_EXIT:
2432 srpt_cm_timewait_exit(cm_id);
2434 case IB_CM_REP_ERROR:
2435 srpt_cm_rep_error(cm_id);
2437 case IB_CM_DREQ_ERROR:
2438 pr_info("Received IB DREQ ERROR event.\n");
2440 case IB_CM_MRA_RECEIVED:
2441 pr_info("Received IB MRA event\n");
2444 pr_err("received unrecognized IB CM event %d\n", event->event);
2452 * srpt_perform_rdmas() - Perform IB RDMA.
2454 * Returns zero upon success or a negative number upon failure.
2456 static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2457 struct srpt_send_ioctx *ioctx)
2459 struct ib_send_wr *bad_wr;
2460 int sq_wr_avail, ret, i;
2461 enum dma_data_direction dir;
2462 const int n_rdma = ioctx->n_rdma;
2464 dir = ioctx->cmd.data_direction;
2465 if (dir == DMA_TO_DEVICE) {
2468 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2469 if (sq_wr_avail < 0) {
2470 pr_warn("IB send queue full (needed %d)\n",
2476 for (i = 0; i < n_rdma; i++) {
2477 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
2479 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2480 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2482 if (i == n_rdma - 1) {
2483 /* only get completion event for the last rdma read */
2484 if (dir == DMA_TO_DEVICE) {
2485 wr->send_flags = IB_SEND_SIGNALED;
2486 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2488 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2490 wr->wr_cqe = &ioctx->rdma_cqe;
2494 wr->next = &ioctx->rdma_wrs[i + 1].wr;
2498 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
2500 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
2501 __func__, __LINE__, ret, i, n_rdma);
2503 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2504 atomic_add(n_rdma, &ch->sq_wr_avail);
2509 * srpt_xfer_data() - Start data transfer from initiator to target.
2511 static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2512 struct srpt_send_ioctx *ioctx)
2516 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2518 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
2522 ret = srpt_perform_rdmas(ch, ioctx);
2524 if (ret == -EAGAIN || ret == -ENOMEM)
2525 pr_info("%s[%d] queue full -- ret=%d\n",
2526 __func__, __LINE__, ret);
2528 pr_err("%s[%d] fatal error -- ret=%d\n",
2529 __func__, __LINE__, ret);
2536 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2540 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2542 struct srpt_send_ioctx *ioctx;
2544 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2545 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2549 * srpt_write_pending() - Start data transfer from initiator to target (write).
2551 static int srpt_write_pending(struct se_cmd *se_cmd)
2553 struct srpt_rdma_ch *ch;
2554 struct srpt_send_ioctx *ioctx;
2555 enum srpt_command_state new_state;
2558 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2560 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2561 WARN_ON(new_state == SRPT_STATE_DONE);
2566 switch (ch->state) {
2568 WARN(true, "unexpected channel state %d\n", ch->state);
2573 case CH_DISCONNECTING:
2576 pr_debug("cmd with tag %lld: channel disconnecting\n",
2578 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2582 ret = srpt_xfer_data(ch, ioctx);
2588 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2590 switch (tcm_mgmt_status) {
2591 case TMR_FUNCTION_COMPLETE:
2592 return SRP_TSK_MGMT_SUCCESS;
2593 case TMR_FUNCTION_REJECTED:
2594 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2596 return SRP_TSK_MGMT_FAILED;
2600 * srpt_queue_response() - Transmits the response to a SCSI command.
2602 * Callback function called by the TCM core. Must not block since it can be
2603 * invoked on the context of the IB completion handler.
2605 static void srpt_queue_response(struct se_cmd *cmd)
2607 struct srpt_rdma_ch *ch;
2608 struct srpt_send_ioctx *ioctx;
2609 enum srpt_command_state state;
2610 unsigned long flags;
2612 enum dma_data_direction dir;
2616 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2620 spin_lock_irqsave(&ioctx->spinlock, flags);
2621 state = ioctx->state;
2623 case SRPT_STATE_NEW:
2624 case SRPT_STATE_DATA_IN:
2625 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2627 case SRPT_STATE_MGMT:
2628 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2631 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2632 ch, ioctx->ioctx.index, ioctx->state);
2635 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2637 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2638 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2639 atomic_inc(&ch->req_lim_delta);
2640 srpt_abort_cmd(ioctx);
2644 dir = ioctx->cmd.data_direction;
2646 /* For read commands, transfer the data to the initiator. */
2647 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2648 !ioctx->queue_status_only) {
2649 ret = srpt_xfer_data(ch, ioctx);
2651 pr_err("xfer_data failed for tag %llu\n",
2657 if (state != SRPT_STATE_MGMT)
2658 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
2662 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2663 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
2666 ret = srpt_post_send(ch, ioctx, resp_len);
2668 pr_err("sending cmd response failed for tag %llu\n",
2670 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2671 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2672 target_put_sess_cmd(&ioctx->cmd);
2676 static int srpt_queue_data_in(struct se_cmd *cmd)
2678 srpt_queue_response(cmd);
2682 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2684 srpt_queue_response(cmd);
2687 static void srpt_aborted_task(struct se_cmd *cmd)
2689 struct srpt_send_ioctx *ioctx = container_of(cmd,
2690 struct srpt_send_ioctx, cmd);
2692 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2695 static int srpt_queue_status(struct se_cmd *cmd)
2697 struct srpt_send_ioctx *ioctx;
2699 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2700 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2701 if (cmd->se_cmd_flags &
2702 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2703 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2704 ioctx->queue_status_only = true;
2705 srpt_queue_response(cmd);
2709 static void srpt_refresh_port_work(struct work_struct *work)
2711 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2713 srpt_refresh_port(sport);
2716 static int srpt_ch_list_empty(struct srpt_device *sdev)
2720 spin_lock_irq(&sdev->spinlock);
2721 res = list_empty(&sdev->rch_list);
2722 spin_unlock_irq(&sdev->spinlock);
2728 * srpt_release_sdev() - Free the channel resources associated with a target.
2730 static int srpt_release_sdev(struct srpt_device *sdev)
2732 struct srpt_rdma_ch *ch, *tmp_ch;
2735 WARN_ON_ONCE(irqs_disabled());
2739 spin_lock_irq(&sdev->spinlock);
2740 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2741 __srpt_close_ch(ch);
2742 spin_unlock_irq(&sdev->spinlock);
2744 res = wait_event_interruptible(sdev->ch_releaseQ,
2745 srpt_ch_list_empty(sdev));
2747 pr_err("%s: interrupted.\n", __func__);
2752 static struct srpt_port *__srpt_lookup_port(const char *name)
2754 struct ib_device *dev;
2755 struct srpt_device *sdev;
2756 struct srpt_port *sport;
2759 list_for_each_entry(sdev, &srpt_dev_list, list) {
2764 for (i = 0; i < dev->phys_port_cnt; i++) {
2765 sport = &sdev->port[i];
2767 if (!strcmp(sport->port_guid, name))
2775 static struct srpt_port *srpt_lookup_port(const char *name)
2777 struct srpt_port *sport;
2779 spin_lock(&srpt_dev_lock);
2780 sport = __srpt_lookup_port(name);
2781 spin_unlock(&srpt_dev_lock);
2787 * srpt_add_one() - Infiniband device addition callback function.
2789 static void srpt_add_one(struct ib_device *device)
2791 struct srpt_device *sdev;
2792 struct srpt_port *sport;
2793 struct ib_srq_init_attr srq_attr;
2796 pr_debug("device = %p, device->dma_ops = %p\n", device,
2799 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
2803 sdev->device = device;
2804 INIT_LIST_HEAD(&sdev->rch_list);
2805 init_waitqueue_head(&sdev->ch_releaseQ);
2806 spin_lock_init(&sdev->spinlock);
2808 sdev->pd = ib_alloc_pd(device);
2809 if (IS_ERR(sdev->pd))
2812 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
2814 srq_attr.event_handler = srpt_srq_event;
2815 srq_attr.srq_context = (void *)sdev;
2816 srq_attr.attr.max_wr = sdev->srq_size;
2817 srq_attr.attr.max_sge = 1;
2818 srq_attr.attr.srq_limit = 0;
2819 srq_attr.srq_type = IB_SRQT_BASIC;
2821 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2822 if (IS_ERR(sdev->srq))
2825 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
2826 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
2829 if (!srpt_service_guid)
2830 srpt_service_guid = be64_to_cpu(device->node_guid);
2832 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2833 if (IS_ERR(sdev->cm_id))
2836 /* print out target login information */
2837 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2838 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2839 srpt_service_guid, srpt_service_guid);
2842 * We do not have a consistent service_id (ie. also id_ext of target_id)
2843 * to identify this target. We currently use the guid of the first HCA
2844 * in the system as service_id; therefore, the target_id will change
2845 * if this HCA is gone bad and replaced by different HCA
2847 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
2850 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2851 srpt_event_handler);
2852 if (ib_register_event_handler(&sdev->event_handler))
2855 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2856 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2857 sizeof(*sdev->ioctx_ring[0]),
2858 srp_max_req_size, DMA_FROM_DEVICE);
2859 if (!sdev->ioctx_ring)
2862 for (i = 0; i < sdev->srq_size; ++i)
2863 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2865 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
2867 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2868 sport = &sdev->port[i - 1];
2871 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2872 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2873 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2874 INIT_WORK(&sport->work, srpt_refresh_port_work);
2876 if (srpt_refresh_port(sport)) {
2877 pr_err("MAD registration failed for %s-%d.\n",
2878 sdev->device->name, i);
2881 snprintf(sport->port_guid, sizeof(sport->port_guid),
2883 be64_to_cpu(sport->gid.global.subnet_prefix),
2884 be64_to_cpu(sport->gid.global.interface_id));
2887 spin_lock(&srpt_dev_lock);
2888 list_add_tail(&sdev->list, &srpt_dev_list);
2889 spin_unlock(&srpt_dev_lock);
2892 ib_set_client_data(device, &srpt_client, sdev);
2893 pr_debug("added %s.\n", device->name);
2897 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2898 sdev->srq_size, srp_max_req_size,
2901 ib_unregister_event_handler(&sdev->event_handler);
2903 ib_destroy_cm_id(sdev->cm_id);
2905 ib_destroy_srq(sdev->srq);
2907 ib_dealloc_pd(sdev->pd);
2912 pr_info("%s(%s) failed.\n", __func__, device->name);
2917 * srpt_remove_one() - InfiniBand device removal callback function.
2919 static void srpt_remove_one(struct ib_device *device, void *client_data)
2921 struct srpt_device *sdev = client_data;
2925 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
2929 srpt_unregister_mad_agent(sdev);
2931 ib_unregister_event_handler(&sdev->event_handler);
2933 /* Cancel any work queued by the just unregistered IB event handler. */
2934 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2935 cancel_work_sync(&sdev->port[i].work);
2937 ib_destroy_cm_id(sdev->cm_id);
2940 * Unregistering a target must happen after destroying sdev->cm_id
2941 * such that no new SRP_LOGIN_REQ information units can arrive while
2942 * destroying the target.
2944 spin_lock(&srpt_dev_lock);
2945 list_del(&sdev->list);
2946 spin_unlock(&srpt_dev_lock);
2947 srpt_release_sdev(sdev);
2949 ib_destroy_srq(sdev->srq);
2950 ib_dealloc_pd(sdev->pd);
2952 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2953 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2954 sdev->ioctx_ring = NULL;
2958 static struct ib_client srpt_client = {
2960 .add = srpt_add_one,
2961 .remove = srpt_remove_one
2964 static int srpt_check_true(struct se_portal_group *se_tpg)
2969 static int srpt_check_false(struct se_portal_group *se_tpg)
2974 static char *srpt_get_fabric_name(void)
2979 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
2981 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
2983 return sport->port_guid;
2986 static u16 srpt_get_tag(struct se_portal_group *tpg)
2991 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
2996 static void srpt_release_cmd(struct se_cmd *se_cmd)
2998 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
2999 struct srpt_send_ioctx, cmd);
3000 struct srpt_rdma_ch *ch = ioctx->ch;
3001 unsigned long flags;
3003 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3004 WARN_ON(ioctx->mapped_sg_count != 0);
3006 if (ioctx->n_rbuf > 1) {
3007 kfree(ioctx->rbufs);
3008 ioctx->rbufs = NULL;
3012 spin_lock_irqsave(&ch->spinlock, flags);
3013 list_add(&ioctx->free_list, &ch->free_list);
3014 spin_unlock_irqrestore(&ch->spinlock, flags);
3018 * srpt_close_session() - Forcibly close a session.
3020 * Callback function invoked by the TCM core to clean up sessions associated
3021 * with a node ACL when the user invokes
3022 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3024 static void srpt_close_session(struct se_session *se_sess)
3026 DECLARE_COMPLETION_ONSTACK(release_done);
3027 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
3028 struct srpt_device *sdev = ch->sport->sdev;
3031 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
3034 spin_lock_irq(&sdev->spinlock);
3035 BUG_ON(ch->release_done);
3036 ch->release_done = &release_done;
3037 wait = !list_empty(&ch->list);
3038 __srpt_close_ch(ch);
3039 spin_unlock_irq(&sdev->spinlock);
3044 while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
3045 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
3046 ch->sess_name, ch->qp->qp_num, ch->state);
3050 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3052 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3053 * This object represents an arbitrary integer used to uniquely identify a
3054 * particular attached remote initiator port to a particular SCSI target port
3055 * within a particular SCSI target device within a particular SCSI instance.
3057 static u32 srpt_sess_get_index(struct se_session *se_sess)
3062 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3066 /* Note: only used from inside debug printk's by the TCM core. */
3067 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3069 struct srpt_send_ioctx *ioctx;
3071 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3072 return srpt_get_cmd_state(ioctx);
3076 * srpt_parse_i_port_id() - Parse an initiator port ID.
3077 * @name: ASCII representation of a 128-bit initiator port ID.
3078 * @i_port_id: Binary 128-bit port ID.
3080 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3083 unsigned len, count, leading_zero_bytes;
3087 if (strncasecmp(p, "0x", 2) == 0)
3093 count = min(len / 2, 16U);
3094 leading_zero_bytes = 16 - count;
3095 memset(i_port_id, 0, leading_zero_bytes);
3096 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3098 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3105 * configfs callback function invoked for
3106 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3108 static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
3112 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
3113 pr_err("invalid initiator port ID %s\n", name);
3119 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3122 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3123 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3125 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3128 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3129 const char *page, size_t count)
3131 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3132 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3136 ret = kstrtoul(page, 0, &val);
3138 pr_err("kstrtoul() failed with ret: %d\n", ret);
3141 if (val > MAX_SRPT_RDMA_SIZE) {
3142 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3143 MAX_SRPT_RDMA_SIZE);
3146 if (val < DEFAULT_MAX_RDMA_SIZE) {
3147 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3148 val, DEFAULT_MAX_RDMA_SIZE);
3151 sport->port_attrib.srp_max_rdma_size = val;
3156 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3159 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3160 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3162 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3165 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3166 const char *page, size_t count)
3168 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3169 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3173 ret = kstrtoul(page, 0, &val);
3175 pr_err("kstrtoul() failed with ret: %d\n", ret);
3178 if (val > MAX_SRPT_RSP_SIZE) {
3179 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3183 if (val < MIN_MAX_RSP_SIZE) {
3184 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3188 sport->port_attrib.srp_max_rsp_size = val;
3193 static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3196 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3197 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3199 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3202 static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3203 const char *page, size_t count)
3205 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3206 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3210 ret = kstrtoul(page, 0, &val);
3212 pr_err("kstrtoul() failed with ret: %d\n", ret);
3215 if (val > MAX_SRPT_SRQ_SIZE) {
3216 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3220 if (val < MIN_SRPT_SRQ_SIZE) {
3221 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3225 sport->port_attrib.srp_sq_size = val;
3230 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3231 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3232 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
3234 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3235 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3236 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3237 &srpt_tpg_attrib_attr_srp_sq_size,
3241 static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
3243 struct se_portal_group *se_tpg = to_tpg(item);
3244 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3246 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3249 static ssize_t srpt_tpg_enable_store(struct config_item *item,
3250 const char *page, size_t count)
3252 struct se_portal_group *se_tpg = to_tpg(item);
3253 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3257 ret = kstrtoul(page, 0, &tmp);
3259 pr_err("Unable to extract srpt_tpg_store_enable\n");
3263 if ((tmp != 0) && (tmp != 1)) {
3264 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3268 sport->enabled = true;
3270 sport->enabled = false;
3275 CONFIGFS_ATTR(srpt_tpg_, enable);
3277 static struct configfs_attribute *srpt_tpg_attrs[] = {
3278 &srpt_tpg_attr_enable,
3283 * configfs callback invoked for
3284 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3286 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3287 struct config_group *group,
3290 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3293 /* Initialize sport->port_wwn and sport->port_tpg_1 */
3294 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
3296 return ERR_PTR(res);
3298 return &sport->port_tpg_1;
3302 * configfs callback invoked for
3303 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3305 static void srpt_drop_tpg(struct se_portal_group *tpg)
3307 struct srpt_port *sport = container_of(tpg,
3308 struct srpt_port, port_tpg_1);
3310 sport->enabled = false;
3311 core_tpg_deregister(&sport->port_tpg_1);
3315 * configfs callback invoked for
3316 * mkdir /sys/kernel/config/target/$driver/$port
3318 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3319 struct config_group *group,
3322 struct srpt_port *sport;
3325 sport = srpt_lookup_port(name);
3326 pr_debug("make_tport(%s)\n", name);
3331 return &sport->port_wwn;
3334 return ERR_PTR(ret);
3338 * configfs callback invoked for
3339 * rmdir /sys/kernel/config/target/$driver/$port
3341 static void srpt_drop_tport(struct se_wwn *wwn)
3343 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3345 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3348 static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
3350 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3353 CONFIGFS_ATTR_RO(srpt_wwn_, version);
3355 static struct configfs_attribute *srpt_wwn_attrs[] = {
3356 &srpt_wwn_attr_version,
3360 static const struct target_core_fabric_ops srpt_template = {
3361 .module = THIS_MODULE,
3363 .get_fabric_name = srpt_get_fabric_name,
3364 .tpg_get_wwn = srpt_get_fabric_wwn,
3365 .tpg_get_tag = srpt_get_tag,
3366 .tpg_check_demo_mode = srpt_check_false,
3367 .tpg_check_demo_mode_cache = srpt_check_true,
3368 .tpg_check_demo_mode_write_protect = srpt_check_true,
3369 .tpg_check_prod_mode_write_protect = srpt_check_false,
3370 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3371 .release_cmd = srpt_release_cmd,
3372 .check_stop_free = srpt_check_stop_free,
3373 .shutdown_session = srpt_shutdown_session,
3374 .close_session = srpt_close_session,
3375 .sess_get_index = srpt_sess_get_index,
3376 .sess_get_initiator_sid = NULL,
3377 .write_pending = srpt_write_pending,
3378 .write_pending_status = srpt_write_pending_status,
3379 .set_default_node_attributes = srpt_set_default_node_attrs,
3380 .get_cmd_state = srpt_get_tcm_cmd_state,
3381 .queue_data_in = srpt_queue_data_in,
3382 .queue_status = srpt_queue_status,
3383 .queue_tm_rsp = srpt_queue_tm_rsp,
3384 .aborted_task = srpt_aborted_task,
3386 * Setup function pointers for generic logic in
3387 * target_core_fabric_configfs.c
3389 .fabric_make_wwn = srpt_make_tport,
3390 .fabric_drop_wwn = srpt_drop_tport,
3391 .fabric_make_tpg = srpt_make_tpg,
3392 .fabric_drop_tpg = srpt_drop_tpg,
3393 .fabric_init_nodeacl = srpt_init_nodeacl,
3395 .tfc_wwn_attrs = srpt_wwn_attrs,
3396 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3397 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
3401 * srpt_init_module() - Kernel module initialization.
3403 * Note: Since ib_register_client() registers callback functions, and since at
3404 * least one of these callback functions (srpt_add_one()) calls target core
3405 * functions, this driver must be registered with the target core before
3406 * ib_register_client() is called.
3408 static int __init srpt_init_module(void)
3413 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3414 pr_err("invalid value %d for kernel module parameter"
3415 " srp_max_req_size -- must be at least %d.\n",
3416 srp_max_req_size, MIN_MAX_REQ_SIZE);
3420 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3421 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3422 pr_err("invalid value %d for kernel module parameter"
3423 " srpt_srq_size -- must be in the range [%d..%d].\n",
3424 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3428 ret = target_register_template(&srpt_template);
3432 ret = ib_register_client(&srpt_client);
3434 pr_err("couldn't register IB client\n");
3435 goto out_unregister_target;
3440 out_unregister_target:
3441 target_unregister_template(&srpt_template);
3446 static void __exit srpt_cleanup_module(void)
3448 ib_unregister_client(&srpt_client);
3449 target_unregister_template(&srpt_template);
3452 module_init(srpt_init_module);
3453 module_exit(srpt_cleanup_module);