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)
1895 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
1896 unsigned long flags;
1898 spin_lock_irqsave(&ch->spinlock, flags);
1899 if (ch->in_shutdown) {
1900 spin_unlock_irqrestore(&ch->spinlock, flags);
1904 ch->in_shutdown = true;
1905 target_sess_cmd_list_set_waiting(se_sess);
1906 spin_unlock_irqrestore(&ch->spinlock, flags);
1912 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
1913 * @cm_id: Pointer to the CM ID of the channel to be drained.
1915 * Note: Must be called from inside srpt_cm_handler to avoid a race between
1916 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
1917 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
1918 * waits until all target sessions for the associated IB device have been
1919 * unregistered and target session registration involves a call to
1920 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
1921 * this function has finished).
1923 static void srpt_drain_channel(struct ib_cm_id *cm_id)
1925 struct srpt_device *sdev;
1926 struct srpt_rdma_ch *ch;
1928 bool do_reset = false;
1930 WARN_ON_ONCE(irqs_disabled());
1932 sdev = cm_id->context;
1934 spin_lock_irq(&sdev->spinlock);
1935 list_for_each_entry(ch, &sdev->rch_list, list) {
1936 if (ch->cm_id == cm_id) {
1937 do_reset = srpt_set_ch_state(ch, CH_DRAINING);
1941 spin_unlock_irq(&sdev->spinlock);
1945 srpt_shutdown_session(ch->sess);
1947 ret = srpt_ch_qp_err(ch);
1949 pr_err("Setting queue pair in error state"
1950 " failed: %d\n", ret);
1955 * srpt_find_channel() - Look up an RDMA channel.
1956 * @cm_id: Pointer to the CM ID of the channel to be looked up.
1958 * Return NULL if no matching RDMA channel has been found.
1960 static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
1961 struct ib_cm_id *cm_id)
1963 struct srpt_rdma_ch *ch;
1966 WARN_ON_ONCE(irqs_disabled());
1970 spin_lock_irq(&sdev->spinlock);
1971 list_for_each_entry(ch, &sdev->rch_list, list) {
1972 if (ch->cm_id == cm_id) {
1977 spin_unlock_irq(&sdev->spinlock);
1979 return found ? ch : NULL;
1983 * srpt_release_channel() - Release channel resources.
1985 * Schedules the actual release because:
1986 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
1987 * trigger a deadlock.
1988 * - It is not safe to call TCM transport_* functions from interrupt context.
1990 static void srpt_release_channel(struct srpt_rdma_ch *ch)
1992 schedule_work(&ch->release_work);
1995 static void srpt_release_channel_work(struct work_struct *w)
1997 struct srpt_rdma_ch *ch;
1998 struct srpt_device *sdev;
1999 struct se_session *se_sess;
2001 ch = container_of(w, struct srpt_rdma_ch, release_work);
2002 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2005 sdev = ch->sport->sdev;
2011 target_wait_for_sess_cmds(se_sess);
2013 transport_deregister_session_configfs(se_sess);
2014 transport_deregister_session(se_sess);
2017 ib_destroy_cm_id(ch->cm_id);
2019 srpt_destroy_ch_ib(ch);
2021 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2022 ch->sport->sdev, ch->rq_size,
2023 ch->rsp_size, DMA_TO_DEVICE);
2025 spin_lock_irq(&sdev->spinlock);
2026 list_del(&ch->list);
2027 spin_unlock_irq(&sdev->spinlock);
2029 if (ch->release_done)
2030 complete(ch->release_done);
2032 wake_up(&sdev->ch_releaseQ);
2038 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2040 * Ownership of the cm_id is transferred to the target session if this
2041 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2043 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2044 struct ib_cm_req_event_param *param,
2047 struct srpt_device *sdev = cm_id->context;
2048 struct srpt_port *sport = &sdev->port[param->port - 1];
2049 struct srp_login_req *req;
2050 struct srp_login_rsp *rsp;
2051 struct srp_login_rej *rej;
2052 struct ib_cm_rep_param *rep_param;
2053 struct srpt_rdma_ch *ch, *tmp_ch;
2054 struct se_node_acl *se_acl;
2059 WARN_ON_ONCE(irqs_disabled());
2061 if (WARN_ON(!sdev || !private_data))
2064 req = (struct srp_login_req *)private_data;
2066 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2068 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2069 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2070 " (guid=0x%llx:0x%llx)\n",
2071 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2072 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2073 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2074 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2077 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2078 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
2080 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2081 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2082 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
2084 if (!rsp || !rej || !rep_param) {
2089 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2090 rej->reason = cpu_to_be32(
2091 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2093 pr_err("rejected SRP_LOGIN_REQ because its"
2094 " length (%d bytes) is out of range (%d .. %d)\n",
2095 it_iu_len, 64, srp_max_req_size);
2099 if (!sport->enabled) {
2100 rej->reason = cpu_to_be32(
2101 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2103 pr_err("rejected SRP_LOGIN_REQ because the target port"
2104 " has not yet been enabled\n");
2108 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2109 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2111 spin_lock_irq(&sdev->spinlock);
2113 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2114 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2115 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2116 && param->port == ch->sport->port
2117 && param->listen_id == ch->sport->sdev->cm_id
2119 if (ch->state != CH_CONNECTING
2120 && ch->state != CH_LIVE)
2123 /* found an existing channel */
2124 pr_debug("Found existing channel %s"
2125 " cm_id= %p state= %d\n",
2126 ch->sess_name, ch->cm_id, ch->state);
2128 __srpt_close_ch(ch);
2131 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2135 spin_unlock_irq(&sdev->spinlock);
2138 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2140 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2141 || *(__be64 *)(req->target_port_id + 8) !=
2142 cpu_to_be64(srpt_service_guid)) {
2143 rej->reason = cpu_to_be32(
2144 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2146 pr_err("rejected SRP_LOGIN_REQ because it"
2147 " has an invalid target port identifier.\n");
2151 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
2153 rej->reason = cpu_to_be32(
2154 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2155 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
2160 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2161 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2162 memcpy(ch->t_port_id, req->target_port_id, 16);
2163 ch->sport = &sdev->port[param->port - 1];
2166 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2167 * for the SRP protocol to the command queue size.
2169 ch->rq_size = SRPT_RQ_SIZE;
2170 spin_lock_init(&ch->spinlock);
2171 ch->state = CH_CONNECTING;
2172 INIT_LIST_HEAD(&ch->cmd_wait_list);
2173 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2175 ch->ioctx_ring = (struct srpt_send_ioctx **)
2176 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2177 sizeof(*ch->ioctx_ring[0]),
2178 ch->rsp_size, DMA_TO_DEVICE);
2179 if (!ch->ioctx_ring)
2182 INIT_LIST_HEAD(&ch->free_list);
2183 for (i = 0; i < ch->rq_size; i++) {
2184 ch->ioctx_ring[i]->ch = ch;
2185 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2188 ret = srpt_create_ch_ib(ch);
2190 rej->reason = cpu_to_be32(
2191 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2192 pr_err("rejected SRP_LOGIN_REQ because creating"
2193 " a new RDMA channel failed.\n");
2197 ret = srpt_ch_qp_rtr(ch, ch->qp);
2199 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2200 pr_err("rejected SRP_LOGIN_REQ because enabling"
2201 " RTR failed (error code = %d)\n", ret);
2206 * Use the initator port identifier as the session name, when
2207 * checking against se_node_acl->initiatorname[] this can be
2208 * with or without preceeding '0x'.
2210 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2211 be64_to_cpu(*(__be64 *)ch->i_port_id),
2212 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2214 pr_debug("registering session %s\n", ch->sess_name);
2215 p = &ch->sess_name[0];
2217 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
2218 if (IS_ERR(ch->sess)) {
2219 rej->reason = cpu_to_be32(
2220 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2221 pr_debug("Failed to create session\n");
2226 se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p);
2228 pr_info("Rejected login because no ACL has been"
2229 " configured yet for initiator %s.\n", ch->sess_name);
2231 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2233 if (p == &ch->sess_name[0]) {
2237 rej->reason = cpu_to_be32(
2238 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2239 transport_free_session(ch->sess);
2242 ch->sess->se_node_acl = se_acl;
2244 transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch);
2246 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2247 ch->sess_name, ch->cm_id);
2249 /* create srp_login_response */
2250 rsp->opcode = SRP_LOGIN_RSP;
2251 rsp->tag = req->tag;
2252 rsp->max_it_iu_len = req->req_it_iu_len;
2253 rsp->max_ti_iu_len = req->req_it_iu_len;
2254 ch->max_ti_iu_len = it_iu_len;
2255 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2256 | SRP_BUF_FORMAT_INDIRECT);
2257 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2258 atomic_set(&ch->req_lim, ch->rq_size);
2259 atomic_set(&ch->req_lim_delta, 0);
2261 /* create cm reply */
2262 rep_param->qp_num = ch->qp->qp_num;
2263 rep_param->private_data = (void *)rsp;
2264 rep_param->private_data_len = sizeof(*rsp);
2265 rep_param->rnr_retry_count = 7;
2266 rep_param->flow_control = 1;
2267 rep_param->failover_accepted = 0;
2269 rep_param->responder_resources = 4;
2270 rep_param->initiator_depth = 4;
2272 ret = ib_send_cm_rep(cm_id, rep_param);
2274 pr_err("sending SRP_LOGIN_REQ response failed"
2275 " (error code = %d)\n", ret);
2276 goto release_channel;
2279 spin_lock_irq(&sdev->spinlock);
2280 list_add_tail(&ch->list, &sdev->rch_list);
2281 spin_unlock_irq(&sdev->spinlock);
2286 srpt_set_ch_state(ch, CH_RELEASING);
2287 transport_deregister_session_configfs(ch->sess);
2288 transport_deregister_session(ch->sess);
2292 srpt_destroy_ch_ib(ch);
2295 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2296 ch->sport->sdev, ch->rq_size,
2297 ch->rsp_size, DMA_TO_DEVICE);
2302 rej->opcode = SRP_LOGIN_REJ;
2303 rej->tag = req->tag;
2304 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2305 | SRP_BUF_FORMAT_INDIRECT);
2307 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2308 (void *)rej, sizeof(*rej));
2318 static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2320 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
2321 srpt_drain_channel(cm_id);
2325 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2327 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2328 * and that the recipient may begin transmitting (RTU = ready to use).
2330 static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2332 struct srpt_rdma_ch *ch;
2335 ch = srpt_find_channel(cm_id->context, cm_id);
2338 if (srpt_set_ch_state(ch, CH_LIVE)) {
2339 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2341 ret = srpt_ch_qp_rts(ch, ch->qp);
2343 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2345 list_del(&ioctx->wait_list);
2346 srpt_handle_new_iu(ch, ioctx, NULL);
2353 static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2355 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
2356 srpt_drain_channel(cm_id);
2359 static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2361 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
2362 srpt_drain_channel(cm_id);
2366 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2368 static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2370 struct srpt_rdma_ch *ch;
2371 unsigned long flags;
2372 bool send_drep = false;
2374 ch = srpt_find_channel(cm_id->context, cm_id);
2377 pr_debug("cm_id= %p ch->state= %d\n", cm_id, ch->state);
2379 spin_lock_irqsave(&ch->spinlock, flags);
2380 switch (ch->state) {
2384 ch->state = CH_DISCONNECTING;
2386 case CH_DISCONNECTING:
2389 WARN(true, "unexpected channel state %d\n", ch->state);
2392 spin_unlock_irqrestore(&ch->spinlock, flags);
2395 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
2396 pr_err("Sending IB DREP failed.\n");
2397 pr_info("Received DREQ and sent DREP for session %s.\n",
2403 * srpt_cm_drep_recv() - Process reception of a DREP message.
2405 static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2407 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
2408 srpt_drain_channel(cm_id);
2412 * srpt_cm_handler() - IB connection manager callback function.
2414 * A non-zero return value will cause the caller destroy the CM ID.
2416 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2417 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2418 * a non-zero value in any other case will trigger a race with the
2419 * ib_destroy_cm_id() call in srpt_release_channel().
2421 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2426 switch (event->event) {
2427 case IB_CM_REQ_RECEIVED:
2428 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2429 event->private_data);
2431 case IB_CM_REJ_RECEIVED:
2432 srpt_cm_rej_recv(cm_id);
2434 case IB_CM_RTU_RECEIVED:
2435 case IB_CM_USER_ESTABLISHED:
2436 srpt_cm_rtu_recv(cm_id);
2438 case IB_CM_DREQ_RECEIVED:
2439 srpt_cm_dreq_recv(cm_id);
2441 case IB_CM_DREP_RECEIVED:
2442 srpt_cm_drep_recv(cm_id);
2444 case IB_CM_TIMEWAIT_EXIT:
2445 srpt_cm_timewait_exit(cm_id);
2447 case IB_CM_REP_ERROR:
2448 srpt_cm_rep_error(cm_id);
2450 case IB_CM_DREQ_ERROR:
2451 pr_info("Received IB DREQ ERROR event.\n");
2453 case IB_CM_MRA_RECEIVED:
2454 pr_info("Received IB MRA event\n");
2457 pr_err("received unrecognized IB CM event %d\n", event->event);
2465 * srpt_perform_rdmas() - Perform IB RDMA.
2467 * Returns zero upon success or a negative number upon failure.
2469 static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2470 struct srpt_send_ioctx *ioctx)
2472 struct ib_send_wr *bad_wr;
2473 int sq_wr_avail, ret, i;
2474 enum dma_data_direction dir;
2475 const int n_rdma = ioctx->n_rdma;
2477 dir = ioctx->cmd.data_direction;
2478 if (dir == DMA_TO_DEVICE) {
2481 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2482 if (sq_wr_avail < 0) {
2483 pr_warn("IB send queue full (needed %d)\n",
2489 for (i = 0; i < n_rdma; i++) {
2490 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
2492 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2493 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2495 if (i == n_rdma - 1) {
2496 /* only get completion event for the last rdma read */
2497 if (dir == DMA_TO_DEVICE) {
2498 wr->send_flags = IB_SEND_SIGNALED;
2499 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2501 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2503 wr->wr_cqe = &ioctx->rdma_cqe;
2507 wr->next = &ioctx->rdma_wrs[i + 1].wr;
2511 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
2513 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
2514 __func__, __LINE__, ret, i, n_rdma);
2516 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2517 atomic_add(n_rdma, &ch->sq_wr_avail);
2522 * srpt_xfer_data() - Start data transfer from initiator to target.
2524 static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2525 struct srpt_send_ioctx *ioctx)
2529 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2531 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
2535 ret = srpt_perform_rdmas(ch, ioctx);
2537 if (ret == -EAGAIN || ret == -ENOMEM)
2538 pr_info("%s[%d] queue full -- ret=%d\n",
2539 __func__, __LINE__, ret);
2541 pr_err("%s[%d] fatal error -- ret=%d\n",
2542 __func__, __LINE__, ret);
2549 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2553 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2555 struct srpt_send_ioctx *ioctx;
2557 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2558 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2562 * srpt_write_pending() - Start data transfer from initiator to target (write).
2564 static int srpt_write_pending(struct se_cmd *se_cmd)
2566 struct srpt_rdma_ch *ch;
2567 struct srpt_send_ioctx *ioctx;
2568 enum srpt_command_state new_state;
2571 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2573 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2574 WARN_ON(new_state == SRPT_STATE_DONE);
2579 switch (ch->state) {
2581 WARN(true, "unexpected channel state %d\n", ch->state);
2586 case CH_DISCONNECTING:
2589 pr_debug("cmd with tag %lld: channel disconnecting\n",
2591 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2595 ret = srpt_xfer_data(ch, ioctx);
2601 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2603 switch (tcm_mgmt_status) {
2604 case TMR_FUNCTION_COMPLETE:
2605 return SRP_TSK_MGMT_SUCCESS;
2606 case TMR_FUNCTION_REJECTED:
2607 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2609 return SRP_TSK_MGMT_FAILED;
2613 * srpt_queue_response() - Transmits the response to a SCSI command.
2615 * Callback function called by the TCM core. Must not block since it can be
2616 * invoked on the context of the IB completion handler.
2618 static void srpt_queue_response(struct se_cmd *cmd)
2620 struct srpt_rdma_ch *ch;
2621 struct srpt_send_ioctx *ioctx;
2622 enum srpt_command_state state;
2623 unsigned long flags;
2625 enum dma_data_direction dir;
2629 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2633 spin_lock_irqsave(&ioctx->spinlock, flags);
2634 state = ioctx->state;
2636 case SRPT_STATE_NEW:
2637 case SRPT_STATE_DATA_IN:
2638 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2640 case SRPT_STATE_MGMT:
2641 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2644 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2645 ch, ioctx->ioctx.index, ioctx->state);
2648 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2650 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2651 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2652 atomic_inc(&ch->req_lim_delta);
2653 srpt_abort_cmd(ioctx);
2657 dir = ioctx->cmd.data_direction;
2659 /* For read commands, transfer the data to the initiator. */
2660 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2661 !ioctx->queue_status_only) {
2662 ret = srpt_xfer_data(ch, ioctx);
2664 pr_err("xfer_data failed for tag %llu\n",
2670 if (state != SRPT_STATE_MGMT)
2671 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
2675 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2676 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
2679 ret = srpt_post_send(ch, ioctx, resp_len);
2681 pr_err("sending cmd response failed for tag %llu\n",
2683 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2684 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2685 target_put_sess_cmd(&ioctx->cmd);
2689 static int srpt_queue_data_in(struct se_cmd *cmd)
2691 srpt_queue_response(cmd);
2695 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2697 srpt_queue_response(cmd);
2700 static void srpt_aborted_task(struct se_cmd *cmd)
2702 struct srpt_send_ioctx *ioctx = container_of(cmd,
2703 struct srpt_send_ioctx, cmd);
2705 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2708 static int srpt_queue_status(struct se_cmd *cmd)
2710 struct srpt_send_ioctx *ioctx;
2712 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2713 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2714 if (cmd->se_cmd_flags &
2715 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2716 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2717 ioctx->queue_status_only = true;
2718 srpt_queue_response(cmd);
2722 static void srpt_refresh_port_work(struct work_struct *work)
2724 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2726 srpt_refresh_port(sport);
2729 static int srpt_ch_list_empty(struct srpt_device *sdev)
2733 spin_lock_irq(&sdev->spinlock);
2734 res = list_empty(&sdev->rch_list);
2735 spin_unlock_irq(&sdev->spinlock);
2741 * srpt_release_sdev() - Free the channel resources associated with a target.
2743 static int srpt_release_sdev(struct srpt_device *sdev)
2745 struct srpt_rdma_ch *ch, *tmp_ch;
2748 WARN_ON_ONCE(irqs_disabled());
2752 spin_lock_irq(&sdev->spinlock);
2753 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2754 __srpt_close_ch(ch);
2755 spin_unlock_irq(&sdev->spinlock);
2757 res = wait_event_interruptible(sdev->ch_releaseQ,
2758 srpt_ch_list_empty(sdev));
2760 pr_err("%s: interrupted.\n", __func__);
2765 static struct srpt_port *__srpt_lookup_port(const char *name)
2767 struct ib_device *dev;
2768 struct srpt_device *sdev;
2769 struct srpt_port *sport;
2772 list_for_each_entry(sdev, &srpt_dev_list, list) {
2777 for (i = 0; i < dev->phys_port_cnt; i++) {
2778 sport = &sdev->port[i];
2780 if (!strcmp(sport->port_guid, name))
2788 static struct srpt_port *srpt_lookup_port(const char *name)
2790 struct srpt_port *sport;
2792 spin_lock(&srpt_dev_lock);
2793 sport = __srpt_lookup_port(name);
2794 spin_unlock(&srpt_dev_lock);
2800 * srpt_add_one() - Infiniband device addition callback function.
2802 static void srpt_add_one(struct ib_device *device)
2804 struct srpt_device *sdev;
2805 struct srpt_port *sport;
2806 struct ib_srq_init_attr srq_attr;
2809 pr_debug("device = %p, device->dma_ops = %p\n", device,
2812 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
2816 sdev->device = device;
2817 INIT_LIST_HEAD(&sdev->rch_list);
2818 init_waitqueue_head(&sdev->ch_releaseQ);
2819 spin_lock_init(&sdev->spinlock);
2821 sdev->pd = ib_alloc_pd(device);
2822 if (IS_ERR(sdev->pd))
2825 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
2827 srq_attr.event_handler = srpt_srq_event;
2828 srq_attr.srq_context = (void *)sdev;
2829 srq_attr.attr.max_wr = sdev->srq_size;
2830 srq_attr.attr.max_sge = 1;
2831 srq_attr.attr.srq_limit = 0;
2832 srq_attr.srq_type = IB_SRQT_BASIC;
2834 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2835 if (IS_ERR(sdev->srq))
2838 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
2839 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
2842 if (!srpt_service_guid)
2843 srpt_service_guid = be64_to_cpu(device->node_guid);
2845 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2846 if (IS_ERR(sdev->cm_id))
2849 /* print out target login information */
2850 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2851 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2852 srpt_service_guid, srpt_service_guid);
2855 * We do not have a consistent service_id (ie. also id_ext of target_id)
2856 * to identify this target. We currently use the guid of the first HCA
2857 * in the system as service_id; therefore, the target_id will change
2858 * if this HCA is gone bad and replaced by different HCA
2860 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
2863 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2864 srpt_event_handler);
2865 if (ib_register_event_handler(&sdev->event_handler))
2868 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2869 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2870 sizeof(*sdev->ioctx_ring[0]),
2871 srp_max_req_size, DMA_FROM_DEVICE);
2872 if (!sdev->ioctx_ring)
2875 for (i = 0; i < sdev->srq_size; ++i)
2876 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2878 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
2880 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2881 sport = &sdev->port[i - 1];
2884 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2885 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2886 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2887 INIT_WORK(&sport->work, srpt_refresh_port_work);
2889 if (srpt_refresh_port(sport)) {
2890 pr_err("MAD registration failed for %s-%d.\n",
2891 sdev->device->name, i);
2894 snprintf(sport->port_guid, sizeof(sport->port_guid),
2896 be64_to_cpu(sport->gid.global.subnet_prefix),
2897 be64_to_cpu(sport->gid.global.interface_id));
2900 spin_lock(&srpt_dev_lock);
2901 list_add_tail(&sdev->list, &srpt_dev_list);
2902 spin_unlock(&srpt_dev_lock);
2905 ib_set_client_data(device, &srpt_client, sdev);
2906 pr_debug("added %s.\n", device->name);
2910 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2911 sdev->srq_size, srp_max_req_size,
2914 ib_unregister_event_handler(&sdev->event_handler);
2916 ib_destroy_cm_id(sdev->cm_id);
2918 ib_destroy_srq(sdev->srq);
2920 ib_dealloc_pd(sdev->pd);
2925 pr_info("%s(%s) failed.\n", __func__, device->name);
2930 * srpt_remove_one() - InfiniBand device removal callback function.
2932 static void srpt_remove_one(struct ib_device *device, void *client_data)
2934 struct srpt_device *sdev = client_data;
2938 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
2942 srpt_unregister_mad_agent(sdev);
2944 ib_unregister_event_handler(&sdev->event_handler);
2946 /* Cancel any work queued by the just unregistered IB event handler. */
2947 for (i = 0; i < sdev->device->phys_port_cnt; i++)
2948 cancel_work_sync(&sdev->port[i].work);
2950 ib_destroy_cm_id(sdev->cm_id);
2953 * Unregistering a target must happen after destroying sdev->cm_id
2954 * such that no new SRP_LOGIN_REQ information units can arrive while
2955 * destroying the target.
2957 spin_lock(&srpt_dev_lock);
2958 list_del(&sdev->list);
2959 spin_unlock(&srpt_dev_lock);
2960 srpt_release_sdev(sdev);
2962 ib_destroy_srq(sdev->srq);
2963 ib_dealloc_pd(sdev->pd);
2965 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
2966 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
2967 sdev->ioctx_ring = NULL;
2971 static struct ib_client srpt_client = {
2973 .add = srpt_add_one,
2974 .remove = srpt_remove_one
2977 static int srpt_check_true(struct se_portal_group *se_tpg)
2982 static int srpt_check_false(struct se_portal_group *se_tpg)
2987 static char *srpt_get_fabric_name(void)
2992 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
2994 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
2996 return sport->port_guid;
2999 static u16 srpt_get_tag(struct se_portal_group *tpg)
3004 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3009 static void srpt_release_cmd(struct se_cmd *se_cmd)
3011 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3012 struct srpt_send_ioctx, cmd);
3013 struct srpt_rdma_ch *ch = ioctx->ch;
3014 unsigned long flags;
3016 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3017 WARN_ON(ioctx->mapped_sg_count != 0);
3019 if (ioctx->n_rbuf > 1) {
3020 kfree(ioctx->rbufs);
3021 ioctx->rbufs = NULL;
3025 spin_lock_irqsave(&ch->spinlock, flags);
3026 list_add(&ioctx->free_list, &ch->free_list);
3027 spin_unlock_irqrestore(&ch->spinlock, flags);
3031 * srpt_close_session() - Forcibly close a session.
3033 * Callback function invoked by the TCM core to clean up sessions associated
3034 * with a node ACL when the user invokes
3035 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3037 static void srpt_close_session(struct se_session *se_sess)
3039 DECLARE_COMPLETION_ONSTACK(release_done);
3040 struct srpt_rdma_ch *ch;
3041 struct srpt_device *sdev;
3044 ch = se_sess->fabric_sess_ptr;
3045 WARN_ON(ch->sess != se_sess);
3047 pr_debug("ch %p state %d\n", ch, ch->state);
3049 sdev = ch->sport->sdev;
3050 spin_lock_irq(&sdev->spinlock);
3051 BUG_ON(ch->release_done);
3052 ch->release_done = &release_done;
3053 __srpt_close_ch(ch);
3054 spin_unlock_irq(&sdev->spinlock);
3056 res = wait_for_completion_timeout(&release_done, 60 * HZ);
3061 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3063 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3064 * This object represents an arbitrary integer used to uniquely identify a
3065 * particular attached remote initiator port to a particular SCSI target port
3066 * within a particular SCSI target device within a particular SCSI instance.
3068 static u32 srpt_sess_get_index(struct se_session *se_sess)
3073 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3077 /* Note: only used from inside debug printk's by the TCM core. */
3078 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3080 struct srpt_send_ioctx *ioctx;
3082 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3083 return srpt_get_cmd_state(ioctx);
3087 * srpt_parse_i_port_id() - Parse an initiator port ID.
3088 * @name: ASCII representation of a 128-bit initiator port ID.
3089 * @i_port_id: Binary 128-bit port ID.
3091 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3094 unsigned len, count, leading_zero_bytes;
3098 if (strncasecmp(p, "0x", 2) == 0)
3104 count = min(len / 2, 16U);
3105 leading_zero_bytes = 16 - count;
3106 memset(i_port_id, 0, leading_zero_bytes);
3107 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3109 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3116 * configfs callback function invoked for
3117 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3119 static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
3123 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
3124 pr_err("invalid initiator port ID %s\n", name);
3130 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3133 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3134 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3136 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3139 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3140 const char *page, size_t count)
3142 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3143 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3147 ret = kstrtoul(page, 0, &val);
3149 pr_err("kstrtoul() failed with ret: %d\n", ret);
3152 if (val > MAX_SRPT_RDMA_SIZE) {
3153 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3154 MAX_SRPT_RDMA_SIZE);
3157 if (val < DEFAULT_MAX_RDMA_SIZE) {
3158 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3159 val, DEFAULT_MAX_RDMA_SIZE);
3162 sport->port_attrib.srp_max_rdma_size = val;
3167 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3170 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3171 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3173 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3176 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3177 const char *page, size_t count)
3179 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3180 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3184 ret = kstrtoul(page, 0, &val);
3186 pr_err("kstrtoul() failed with ret: %d\n", ret);
3189 if (val > MAX_SRPT_RSP_SIZE) {
3190 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3194 if (val < MIN_MAX_RSP_SIZE) {
3195 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3199 sport->port_attrib.srp_max_rsp_size = val;
3204 static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3207 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3208 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3210 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3213 static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3214 const char *page, size_t count)
3216 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3217 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3221 ret = kstrtoul(page, 0, &val);
3223 pr_err("kstrtoul() failed with ret: %d\n", ret);
3226 if (val > MAX_SRPT_SRQ_SIZE) {
3227 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3231 if (val < MIN_SRPT_SRQ_SIZE) {
3232 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3236 sport->port_attrib.srp_sq_size = val;
3241 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3242 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3243 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
3245 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3246 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3247 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3248 &srpt_tpg_attrib_attr_srp_sq_size,
3252 static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
3254 struct se_portal_group *se_tpg = to_tpg(item);
3255 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3257 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3260 static ssize_t srpt_tpg_enable_store(struct config_item *item,
3261 const char *page, size_t count)
3263 struct se_portal_group *se_tpg = to_tpg(item);
3264 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3268 ret = kstrtoul(page, 0, &tmp);
3270 pr_err("Unable to extract srpt_tpg_store_enable\n");
3274 if ((tmp != 0) && (tmp != 1)) {
3275 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3279 sport->enabled = true;
3281 sport->enabled = false;
3286 CONFIGFS_ATTR(srpt_tpg_, enable);
3288 static struct configfs_attribute *srpt_tpg_attrs[] = {
3289 &srpt_tpg_attr_enable,
3294 * configfs callback invoked for
3295 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3297 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3298 struct config_group *group,
3301 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3304 /* Initialize sport->port_wwn and sport->port_tpg_1 */
3305 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
3307 return ERR_PTR(res);
3309 return &sport->port_tpg_1;
3313 * configfs callback invoked for
3314 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3316 static void srpt_drop_tpg(struct se_portal_group *tpg)
3318 struct srpt_port *sport = container_of(tpg,
3319 struct srpt_port, port_tpg_1);
3321 sport->enabled = false;
3322 core_tpg_deregister(&sport->port_tpg_1);
3326 * configfs callback invoked for
3327 * mkdir /sys/kernel/config/target/$driver/$port
3329 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3330 struct config_group *group,
3333 struct srpt_port *sport;
3336 sport = srpt_lookup_port(name);
3337 pr_debug("make_tport(%s)\n", name);
3342 return &sport->port_wwn;
3345 return ERR_PTR(ret);
3349 * configfs callback invoked for
3350 * rmdir /sys/kernel/config/target/$driver/$port
3352 static void srpt_drop_tport(struct se_wwn *wwn)
3354 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3356 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3359 static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
3361 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3364 CONFIGFS_ATTR_RO(srpt_wwn_, version);
3366 static struct configfs_attribute *srpt_wwn_attrs[] = {
3367 &srpt_wwn_attr_version,
3371 static const struct target_core_fabric_ops srpt_template = {
3372 .module = THIS_MODULE,
3374 .get_fabric_name = srpt_get_fabric_name,
3375 .tpg_get_wwn = srpt_get_fabric_wwn,
3376 .tpg_get_tag = srpt_get_tag,
3377 .tpg_check_demo_mode = srpt_check_false,
3378 .tpg_check_demo_mode_cache = srpt_check_true,
3379 .tpg_check_demo_mode_write_protect = srpt_check_true,
3380 .tpg_check_prod_mode_write_protect = srpt_check_false,
3381 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3382 .release_cmd = srpt_release_cmd,
3383 .check_stop_free = srpt_check_stop_free,
3384 .shutdown_session = srpt_shutdown_session,
3385 .close_session = srpt_close_session,
3386 .sess_get_index = srpt_sess_get_index,
3387 .sess_get_initiator_sid = NULL,
3388 .write_pending = srpt_write_pending,
3389 .write_pending_status = srpt_write_pending_status,
3390 .set_default_node_attributes = srpt_set_default_node_attrs,
3391 .get_cmd_state = srpt_get_tcm_cmd_state,
3392 .queue_data_in = srpt_queue_data_in,
3393 .queue_status = srpt_queue_status,
3394 .queue_tm_rsp = srpt_queue_tm_rsp,
3395 .aborted_task = srpt_aborted_task,
3397 * Setup function pointers for generic logic in
3398 * target_core_fabric_configfs.c
3400 .fabric_make_wwn = srpt_make_tport,
3401 .fabric_drop_wwn = srpt_drop_tport,
3402 .fabric_make_tpg = srpt_make_tpg,
3403 .fabric_drop_tpg = srpt_drop_tpg,
3404 .fabric_init_nodeacl = srpt_init_nodeacl,
3406 .tfc_wwn_attrs = srpt_wwn_attrs,
3407 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3408 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
3412 * srpt_init_module() - Kernel module initialization.
3414 * Note: Since ib_register_client() registers callback functions, and since at
3415 * least one of these callback functions (srpt_add_one()) calls target core
3416 * functions, this driver must be registered with the target core before
3417 * ib_register_client() is called.
3419 static int __init srpt_init_module(void)
3424 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3425 pr_err("invalid value %d for kernel module parameter"
3426 " srp_max_req_size -- must be at least %d.\n",
3427 srp_max_req_size, MIN_MAX_REQ_SIZE);
3431 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3432 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3433 pr_err("invalid value %d for kernel module parameter"
3434 " srpt_srq_size -- must be in the range [%d..%d].\n",
3435 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3439 ret = target_register_template(&srpt_template);
3443 ret = ib_register_client(&srpt_client);
3445 pr_err("couldn't register IB client\n");
3446 goto out_unregister_target;
3451 out_unregister_target:
3452 target_unregister_template(&srpt_template);
3457 static void __exit srpt_cleanup_module(void)
3459 ib_unregister_client(&srpt_client);
3460 target_unregister_template(&srpt_template);
3463 module_init(srpt_init_module);
3464 module_exit(srpt_cleanup_module);