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_tcq.h>
45 #include <target/configfs_macros.h>
46 #include <target/target_core_base.h>
47 #include <target/target_core_fabric_configfs.h>
48 #include <target/target_core_fabric.h>
49 #include <target/target_core_configfs.h>
52 /* Name of this kernel module. */
53 #define DRV_NAME "ib_srpt"
54 #define DRV_VERSION "2.0.0"
55 #define DRV_RELDATE "2011-02-14"
57 #define SRPT_ID_STRING "Linux SRP target"
60 #define pr_fmt(fmt) DRV_NAME " " fmt
62 MODULE_AUTHOR("Vu Pham and Bart Van Assche");
63 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
64 "v" DRV_VERSION " (" DRV_RELDATE ")");
65 MODULE_LICENSE("Dual BSD/GPL");
71 static u64 srpt_service_guid;
72 static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
73 static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
75 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
76 module_param(srp_max_req_size, int, 0444);
77 MODULE_PARM_DESC(srp_max_req_size,
78 "Maximum size of SRP request messages in bytes.");
80 static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
81 module_param(srpt_srq_size, int, 0444);
82 MODULE_PARM_DESC(srpt_srq_size,
83 "Shared receive queue (SRQ) size.");
85 static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
87 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
89 module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
91 MODULE_PARM_DESC(srpt_service_guid,
92 "Using this value for ioc_guid, id_ext, and cm_listen_id"
93 " instead of using the node_guid of the first HCA.");
95 static struct ib_client srpt_client;
96 static struct target_fabric_configfs *srpt_target;
97 static void srpt_release_channel(struct srpt_rdma_ch *ch);
98 static int srpt_queue_status(struct se_cmd *cmd);
101 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
104 enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
107 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
108 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
114 * srpt_sdev_name() - Return the name associated with the HCA.
116 * Examples are ib0, ib1, ...
118 static inline const char *srpt_sdev_name(struct srpt_device *sdev)
120 return sdev->device->name;
123 static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
126 enum rdma_ch_state state;
128 spin_lock_irqsave(&ch->spinlock, flags);
130 spin_unlock_irqrestore(&ch->spinlock, flags);
134 static enum rdma_ch_state
135 srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
138 enum rdma_ch_state prev;
140 spin_lock_irqsave(&ch->spinlock, flags);
142 ch->state = new_state;
143 spin_unlock_irqrestore(&ch->spinlock, flags);
148 * srpt_test_and_set_ch_state() - Test and set the channel state.
150 * Returns true if and only if the channel state has been set to the new state.
153 srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
154 enum rdma_ch_state new)
157 enum rdma_ch_state prev;
159 spin_lock_irqsave(&ch->spinlock, flags);
163 spin_unlock_irqrestore(&ch->spinlock, flags);
168 * srpt_event_handler() - Asynchronous IB event callback function.
170 * Callback function called by the InfiniBand core when an asynchronous IB
171 * event occurs. This callback may occur in interrupt context. See also
172 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
173 * Architecture Specification.
175 static void srpt_event_handler(struct ib_event_handler *handler,
176 struct ib_event *event)
178 struct srpt_device *sdev;
179 struct srpt_port *sport;
181 sdev = ib_get_client_data(event->device, &srpt_client);
182 if (!sdev || sdev->device != event->device)
185 pr_debug("ASYNC event= %d on device= %s\n", event->event,
186 srpt_sdev_name(sdev));
188 switch (event->event) {
189 case IB_EVENT_PORT_ERR:
190 if (event->element.port_num <= sdev->device->phys_port_cnt) {
191 sport = &sdev->port[event->element.port_num - 1];
196 case IB_EVENT_PORT_ACTIVE:
197 case IB_EVENT_LID_CHANGE:
198 case IB_EVENT_PKEY_CHANGE:
199 case IB_EVENT_SM_CHANGE:
200 case IB_EVENT_CLIENT_REREGISTER:
201 /* Refresh port data asynchronously. */
202 if (event->element.port_num <= sdev->device->phys_port_cnt) {
203 sport = &sdev->port[event->element.port_num - 1];
204 if (!sport->lid && !sport->sm_lid)
205 schedule_work(&sport->work);
209 printk(KERN_ERR "received unrecognized IB event %d\n",
216 * srpt_srq_event() - SRQ event callback function.
218 static void srpt_srq_event(struct ib_event *event, void *ctx)
220 printk(KERN_INFO "SRQ event %d\n", event->event);
224 * srpt_qp_event() - QP event callback function.
226 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
228 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
229 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
231 switch (event->event) {
232 case IB_EVENT_COMM_EST:
233 ib_cm_notify(ch->cm_id, event->event);
235 case IB_EVENT_QP_LAST_WQE_REACHED:
236 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
238 srpt_release_channel(ch);
240 pr_debug("%s: state %d - ignored LAST_WQE.\n",
241 ch->sess_name, srpt_get_ch_state(ch));
244 printk(KERN_ERR "received unrecognized IB QP event %d\n",
251 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
253 * @slot: one-based slot number.
254 * @value: four-bit value.
256 * Copies the lowest four bits of value in element slot of the array of four
257 * bit elements called c_list (controller list). The index slot is one-based.
259 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
266 tmp = c_list[id] & 0xf;
267 c_list[id] = (value << 4) | tmp;
269 tmp = c_list[id] & 0xf0;
270 c_list[id] = (value & 0xf) | tmp;
275 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
277 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
280 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
282 struct ib_class_port_info *cif;
284 cif = (struct ib_class_port_info *)mad->data;
285 memset(cif, 0, sizeof *cif);
286 cif->base_version = 1;
287 cif->class_version = 1;
288 cif->resp_time_value = 20;
290 mad->mad_hdr.status = 0;
294 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
296 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
297 * Specification. See also section B.7, table B.6 in the SRP r16a document.
299 static void srpt_get_iou(struct ib_dm_mad *mad)
301 struct ib_dm_iou_info *ioui;
305 ioui = (struct ib_dm_iou_info *)mad->data;
306 ioui->change_id = __constant_cpu_to_be16(1);
307 ioui->max_controllers = 16;
309 /* set present for slot 1 and empty for the rest */
310 srpt_set_ioc(ioui->controller_list, 1, 1);
311 for (i = 1, slot = 2; i < 16; i++, slot++)
312 srpt_set_ioc(ioui->controller_list, slot, 0);
314 mad->mad_hdr.status = 0;
318 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
320 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
321 * Architecture Specification. See also section B.7, table B.7 in the SRP
324 static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
325 struct ib_dm_mad *mad)
327 struct srpt_device *sdev = sport->sdev;
328 struct ib_dm_ioc_profile *iocp;
330 iocp = (struct ib_dm_ioc_profile *)mad->data;
332 if (!slot || slot > 16) {
334 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
340 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
344 memset(iocp, 0, sizeof *iocp);
345 strcpy(iocp->id_string, SRPT_ID_STRING);
346 iocp->guid = cpu_to_be64(srpt_service_guid);
347 iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
348 iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
349 iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
350 iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
351 iocp->subsys_device_id = 0x0;
352 iocp->io_class = __constant_cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
353 iocp->io_subclass = __constant_cpu_to_be16(SRP_IO_SUBCLASS);
354 iocp->protocol = __constant_cpu_to_be16(SRP_PROTOCOL);
355 iocp->protocol_version = __constant_cpu_to_be16(SRP_PROTOCOL_VERSION);
356 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
357 iocp->rdma_read_depth = 4;
358 iocp->send_size = cpu_to_be32(srp_max_req_size);
359 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
361 iocp->num_svc_entries = 1;
362 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
363 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
365 mad->mad_hdr.status = 0;
369 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
371 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
372 * Specification. See also section B.7, table B.8 in the SRP r16a document.
374 static void srpt_get_svc_entries(u64 ioc_guid,
375 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
377 struct ib_dm_svc_entries *svc_entries;
381 if (!slot || slot > 16) {
383 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
387 if (slot > 2 || lo > hi || hi > 1) {
389 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
393 svc_entries = (struct ib_dm_svc_entries *)mad->data;
394 memset(svc_entries, 0, sizeof *svc_entries);
395 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
396 snprintf(svc_entries->service_entries[0].name,
397 sizeof(svc_entries->service_entries[0].name),
399 SRP_SERVICE_NAME_PREFIX,
402 mad->mad_hdr.status = 0;
406 * srpt_mgmt_method_get() - Process a received management datagram.
407 * @sp: source port through which the MAD has been received.
408 * @rq_mad: received MAD.
409 * @rsp_mad: response MAD.
411 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
412 struct ib_dm_mad *rsp_mad)
418 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
420 case DM_ATTR_CLASS_PORT_INFO:
421 srpt_get_class_port_info(rsp_mad);
423 case DM_ATTR_IOU_INFO:
424 srpt_get_iou(rsp_mad);
426 case DM_ATTR_IOC_PROFILE:
427 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
428 srpt_get_ioc(sp, slot, rsp_mad);
430 case DM_ATTR_SVC_ENTRIES:
431 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
432 hi = (u8) ((slot >> 8) & 0xff);
433 lo = (u8) (slot & 0xff);
434 slot = (u16) ((slot >> 16) & 0xffff);
435 srpt_get_svc_entries(srpt_service_guid,
436 slot, hi, lo, rsp_mad);
439 rsp_mad->mad_hdr.status =
440 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
446 * srpt_mad_send_handler() - Post MAD-send callback function.
448 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
449 struct ib_mad_send_wc *mad_wc)
451 ib_destroy_ah(mad_wc->send_buf->ah);
452 ib_free_send_mad(mad_wc->send_buf);
456 * srpt_mad_recv_handler() - MAD reception callback function.
458 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
459 struct ib_mad_recv_wc *mad_wc)
461 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
463 struct ib_mad_send_buf *rsp;
464 struct ib_dm_mad *dm_mad;
466 if (!mad_wc || !mad_wc->recv_buf.mad)
469 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
470 mad_wc->recv_buf.grh, mad_agent->port_num);
474 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
476 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
477 mad_wc->wc->pkey_index, 0,
478 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
486 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
487 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
488 dm_mad->mad_hdr.status = 0;
490 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
491 case IB_MGMT_METHOD_GET:
492 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
494 case IB_MGMT_METHOD_SET:
495 dm_mad->mad_hdr.status =
496 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
499 dm_mad->mad_hdr.status =
500 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
504 if (!ib_post_send_mad(rsp, NULL)) {
505 ib_free_recv_mad(mad_wc);
506 /* will destroy_ah & free_send_mad in send completion */
510 ib_free_send_mad(rsp);
515 ib_free_recv_mad(mad_wc);
519 * srpt_refresh_port() - Configure a HCA port.
521 * Enable InfiniBand management datagram processing, update the cached sm_lid,
522 * lid and gid values, and register a callback function for processing MADs
523 * on the specified port.
525 * Note: It is safe to call this function more than once for the same port.
527 static int srpt_refresh_port(struct srpt_port *sport)
529 struct ib_mad_reg_req reg_req;
530 struct ib_port_modify port_modify;
531 struct ib_port_attr port_attr;
534 memset(&port_modify, 0, sizeof port_modify);
535 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
536 port_modify.clr_port_cap_mask = 0;
538 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
542 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
546 sport->sm_lid = port_attr.sm_lid;
547 sport->lid = port_attr.lid;
549 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
553 if (!sport->mad_agent) {
554 memset(®_req, 0, sizeof reg_req);
555 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
556 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
557 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
558 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
560 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
564 srpt_mad_send_handler,
565 srpt_mad_recv_handler,
567 if (IS_ERR(sport->mad_agent)) {
568 ret = PTR_ERR(sport->mad_agent);
569 sport->mad_agent = NULL;
578 port_modify.set_port_cap_mask = 0;
579 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
580 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
588 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
590 * Note: It is safe to call this function more than once for the same device.
592 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
594 struct ib_port_modify port_modify = {
595 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
597 struct srpt_port *sport;
600 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
601 sport = &sdev->port[i - 1];
602 WARN_ON(sport->port != i);
603 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
604 printk(KERN_ERR "disabling MAD processing failed.\n");
605 if (sport->mad_agent) {
606 ib_unregister_mad_agent(sport->mad_agent);
607 sport->mad_agent = NULL;
613 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
615 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
616 int ioctx_size, int dma_size,
617 enum dma_data_direction dir)
619 struct srpt_ioctx *ioctx;
621 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
625 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
629 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
630 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
644 * srpt_free_ioctx() - Free an SRPT I/O context structure.
646 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
647 int dma_size, enum dma_data_direction dir)
652 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
658 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
659 * @sdev: Device to allocate the I/O context ring for.
660 * @ring_size: Number of elements in the I/O context ring.
661 * @ioctx_size: I/O context size.
662 * @dma_size: DMA buffer size.
663 * @dir: DMA data direction.
665 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
666 int ring_size, int ioctx_size,
667 int dma_size, enum dma_data_direction dir)
669 struct srpt_ioctx **ring;
672 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
673 && ioctx_size != sizeof(struct srpt_send_ioctx));
675 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
678 for (i = 0; i < ring_size; ++i) {
679 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
688 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
696 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
698 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
699 struct srpt_device *sdev, int ring_size,
700 int dma_size, enum dma_data_direction dir)
704 for (i = 0; i < ring_size; ++i)
705 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
710 * srpt_get_cmd_state() - Get the state of a SCSI command.
712 static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
714 enum srpt_command_state state;
719 spin_lock_irqsave(&ioctx->spinlock, flags);
720 state = ioctx->state;
721 spin_unlock_irqrestore(&ioctx->spinlock, flags);
726 * srpt_set_cmd_state() - Set the state of a SCSI command.
728 * Does not modify the state of aborted commands. Returns the previous command
731 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
732 enum srpt_command_state new)
734 enum srpt_command_state previous;
739 spin_lock_irqsave(&ioctx->spinlock, flags);
740 previous = ioctx->state;
741 if (previous != SRPT_STATE_DONE)
743 spin_unlock_irqrestore(&ioctx->spinlock, flags);
749 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
751 * Returns true if and only if the previous command state was equal to 'old'.
753 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
754 enum srpt_command_state old,
755 enum srpt_command_state new)
757 enum srpt_command_state previous;
761 WARN_ON(old == SRPT_STATE_DONE);
762 WARN_ON(new == SRPT_STATE_NEW);
764 spin_lock_irqsave(&ioctx->spinlock, flags);
765 previous = ioctx->state;
768 spin_unlock_irqrestore(&ioctx->spinlock, flags);
769 return previous == old;
773 * srpt_post_recv() - Post an IB receive request.
775 static int srpt_post_recv(struct srpt_device *sdev,
776 struct srpt_recv_ioctx *ioctx)
779 struct ib_recv_wr wr, *bad_wr;
782 wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
784 list.addr = ioctx->ioctx.dma;
785 list.length = srp_max_req_size;
786 list.lkey = sdev->mr->lkey;
792 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
796 * srpt_post_send() - Post an IB send request.
798 * Returns zero upon success and a non-zero value upon failure.
800 static int srpt_post_send(struct srpt_rdma_ch *ch,
801 struct srpt_send_ioctx *ioctx, int len)
804 struct ib_send_wr wr, *bad_wr;
805 struct srpt_device *sdev = ch->sport->sdev;
808 atomic_inc(&ch->req_lim);
811 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
812 printk(KERN_WARNING "IB send queue full (needed 1)\n");
816 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
819 list.addr = ioctx->ioctx.dma;
821 list.lkey = sdev->mr->lkey;
824 wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
827 wr.opcode = IB_WR_SEND;
828 wr.send_flags = IB_SEND_SIGNALED;
830 ret = ib_post_send(ch->qp, &wr, &bad_wr);
834 atomic_inc(&ch->sq_wr_avail);
835 atomic_dec(&ch->req_lim);
841 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
842 * @ioctx: Pointer to the I/O context associated with the request.
843 * @srp_cmd: Pointer to the SRP_CMD request data.
844 * @dir: Pointer to the variable to which the transfer direction will be
846 * @data_len: Pointer to the variable to which the total data length of all
847 * descriptors in the SRP_CMD request will be written.
849 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
851 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
852 * -ENOMEM when memory allocation fails and zero upon success.
854 static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
855 struct srp_cmd *srp_cmd,
856 enum dma_data_direction *dir, u64 *data_len)
858 struct srp_indirect_buf *idb;
859 struct srp_direct_buf *db;
860 unsigned add_cdb_offset;
864 * The pointer computations below will only be compiled correctly
865 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
866 * whether srp_cmd::add_data has been declared as a byte pointer.
868 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
869 && !__same_type(srp_cmd->add_data[0], (u8)0));
878 * The lower four bits of the buffer format field contain the DATA-IN
879 * buffer descriptor format, and the highest four bits contain the
880 * DATA-OUT buffer descriptor format.
883 if (srp_cmd->buf_fmt & 0xf)
884 /* DATA-IN: transfer data from target to initiator (read). */
885 *dir = DMA_FROM_DEVICE;
886 else if (srp_cmd->buf_fmt >> 4)
887 /* DATA-OUT: transfer data from initiator to target (write). */
888 *dir = DMA_TO_DEVICE;
891 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
892 * CDB LENGTH' field are reserved and the size in bytes of this field
893 * is four times the value specified in bits 3..7. Hence the "& ~3".
895 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
896 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
897 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
899 ioctx->rbufs = &ioctx->single_rbuf;
901 db = (struct srp_direct_buf *)(srp_cmd->add_data
903 memcpy(ioctx->rbufs, db, sizeof *db);
904 *data_len = be32_to_cpu(db->len);
905 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
906 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
907 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
910 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
913 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
914 printk(KERN_ERR "received unsupported SRP_CMD request"
915 " type (%u out + %u in != %u / %zu)\n",
916 srp_cmd->data_out_desc_cnt,
917 srp_cmd->data_in_desc_cnt,
918 be32_to_cpu(idb->table_desc.len),
925 if (ioctx->n_rbuf == 1)
926 ioctx->rbufs = &ioctx->single_rbuf;
929 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
938 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
939 *data_len = be32_to_cpu(idb->len);
946 * srpt_init_ch_qp() - Initialize queue pair attributes.
948 * Initialized the attributes of queue pair 'qp' by allowing local write,
949 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
951 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
953 struct ib_qp_attr *attr;
956 attr = kzalloc(sizeof *attr, GFP_KERNEL);
960 attr->qp_state = IB_QPS_INIT;
961 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
962 IB_ACCESS_REMOTE_WRITE;
963 attr->port_num = ch->sport->port;
964 attr->pkey_index = 0;
966 ret = ib_modify_qp(qp, attr,
967 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
975 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
976 * @ch: channel of the queue pair.
977 * @qp: queue pair to change the state of.
979 * Returns zero upon success and a negative value upon failure.
981 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
982 * If this structure ever becomes larger, it might be necessary to allocate
983 * it dynamically instead of on the stack.
985 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
987 struct ib_qp_attr qp_attr;
991 qp_attr.qp_state = IB_QPS_RTR;
992 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
996 qp_attr.max_dest_rd_atomic = 4;
998 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1005 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1006 * @ch: channel of the queue pair.
1007 * @qp: queue pair to change the state of.
1009 * Returns zero upon success and a negative value upon failure.
1011 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1012 * If this structure ever becomes larger, it might be necessary to allocate
1013 * it dynamically instead of on the stack.
1015 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1017 struct ib_qp_attr qp_attr;
1021 qp_attr.qp_state = IB_QPS_RTS;
1022 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1026 qp_attr.max_rd_atomic = 4;
1028 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1035 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1037 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1039 struct ib_qp_attr qp_attr;
1041 qp_attr.qp_state = IB_QPS_ERR;
1042 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1046 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1048 static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1049 struct srpt_send_ioctx *ioctx)
1051 struct scatterlist *sg;
1052 enum dma_data_direction dir;
1056 BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
1058 while (ioctx->n_rdma)
1059 kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
1061 kfree(ioctx->rdma_ius);
1062 ioctx->rdma_ius = NULL;
1064 if (ioctx->mapped_sg_count) {
1067 dir = ioctx->cmd.data_direction;
1068 BUG_ON(dir == DMA_NONE);
1069 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1070 opposite_dma_dir(dir));
1071 ioctx->mapped_sg_count = 0;
1076 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1078 static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1079 struct srpt_send_ioctx *ioctx)
1082 struct scatterlist *sg, *sg_orig;
1084 enum dma_data_direction dir;
1085 struct rdma_iu *riu;
1086 struct srp_direct_buf *db;
1087 dma_addr_t dma_addr;
1099 dir = cmd->data_direction;
1100 BUG_ON(dir == DMA_NONE);
1102 transport_do_task_sg_chain(cmd);
1103 ioctx->sg = sg = sg_orig = cmd->t_tasks_sg_chained;
1104 ioctx->sg_cnt = sg_cnt = cmd->t_tasks_sg_chained_no;
1106 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1107 opposite_dma_dir(dir));
1108 if (unlikely(!count))
1111 ioctx->mapped_sg_count = count;
1113 if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1114 nrdma = ioctx->n_rdma_ius;
1116 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1119 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
1120 if (!ioctx->rdma_ius)
1123 ioctx->n_rdma_ius = nrdma;
1127 tsize = cmd->data_length;
1128 dma_len = sg_dma_len(&sg[0]);
1129 riu = ioctx->rdma_ius;
1132 * For each remote desc - calculate the #ib_sge.
1133 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1134 * each remote desc rdma_iu is required a rdma wr;
1136 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1140 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1141 rsize = be32_to_cpu(db->len);
1142 raddr = be64_to_cpu(db->va);
1144 riu->rkey = be32_to_cpu(db->key);
1147 /* calculate how many sge required for this remote_buf */
1148 while (rsize > 0 && tsize > 0) {
1150 if (rsize >= dma_len) {
1159 dma_len = sg_dma_len(sg);
1170 if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1173 kmalloc(riu->sge_cnt * sizeof *riu->sge,
1181 riu->rkey = be32_to_cpu(db->key);
1186 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1193 tsize = cmd->data_length;
1194 riu = ioctx->rdma_ius;
1196 dma_len = sg_dma_len(&sg[0]);
1197 dma_addr = sg_dma_address(&sg[0]);
1199 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1201 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1202 rsize = be32_to_cpu(db->len);
1206 while (rsize > 0 && tsize > 0) {
1207 sge->addr = dma_addr;
1208 sge->lkey = ch->sport->sdev->mr->lkey;
1210 if (rsize >= dma_len) {
1212 (tsize < dma_len) ? tsize : dma_len;
1220 dma_len = sg_dma_len(sg);
1221 dma_addr = sg_dma_address(sg);
1225 sge->length = (tsize < rsize) ? tsize : rsize;
1233 if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1237 } else if (rsize > 0 && tsize > 0)
1245 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1251 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1253 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1255 struct srpt_send_ioctx *ioctx;
1256 unsigned long flags;
1261 spin_lock_irqsave(&ch->spinlock, flags);
1262 if (!list_empty(&ch->free_list)) {
1263 ioctx = list_first_entry(&ch->free_list,
1264 struct srpt_send_ioctx, free_list);
1265 list_del(&ioctx->free_list);
1267 spin_unlock_irqrestore(&ch->spinlock, flags);
1272 BUG_ON(ioctx->ch != ch);
1273 kref_init(&ioctx->kref);
1274 spin_lock_init(&ioctx->spinlock);
1275 ioctx->state = SRPT_STATE_NEW;
1277 ioctx->rbufs = NULL;
1279 ioctx->n_rdma_ius = 0;
1280 ioctx->rdma_ius = NULL;
1281 ioctx->mapped_sg_count = 0;
1282 init_completion(&ioctx->tx_done);
1283 ioctx->queue_status_only = false;
1285 * transport_init_se_cmd() does not initialize all fields, so do it
1288 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1289 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1295 * srpt_put_send_ioctx() - Free up resources.
1297 static void srpt_put_send_ioctx(struct srpt_send_ioctx *ioctx)
1299 struct srpt_rdma_ch *ch;
1300 unsigned long flags;
1306 WARN_ON(srpt_get_cmd_state(ioctx) != SRPT_STATE_DONE);
1308 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
1309 transport_generic_free_cmd(&ioctx->cmd, 0);
1311 if (ioctx->n_rbuf > 1) {
1312 kfree(ioctx->rbufs);
1313 ioctx->rbufs = NULL;
1317 spin_lock_irqsave(&ch->spinlock, flags);
1318 list_add(&ioctx->free_list, &ch->free_list);
1319 spin_unlock_irqrestore(&ch->spinlock, flags);
1322 static void srpt_put_send_ioctx_kref(struct kref *kref)
1324 srpt_put_send_ioctx(container_of(kref, struct srpt_send_ioctx, kref));
1328 * srpt_abort_cmd() - Abort a SCSI command.
1329 * @ioctx: I/O context associated with the SCSI command.
1330 * @context: Preferred execution context.
1332 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1334 enum srpt_command_state state;
1335 unsigned long flags;
1340 * If the command is in a state where the target core is waiting for
1341 * the ib_srpt driver, change the state to the next state. Changing
1342 * the state of the command from SRPT_STATE_NEED_DATA to
1343 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1344 * function a second time.
1347 spin_lock_irqsave(&ioctx->spinlock, flags);
1348 state = ioctx->state;
1350 case SRPT_STATE_NEED_DATA:
1351 ioctx->state = SRPT_STATE_DATA_IN;
1353 case SRPT_STATE_DATA_IN:
1354 case SRPT_STATE_CMD_RSP_SENT:
1355 case SRPT_STATE_MGMT_RSP_SENT:
1356 ioctx->state = SRPT_STATE_DONE;
1361 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1363 if (state == SRPT_STATE_DONE)
1366 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1370 case SRPT_STATE_NEW:
1371 case SRPT_STATE_DATA_IN:
1372 case SRPT_STATE_MGMT:
1374 * Do nothing - defer abort processing until
1375 * srpt_queue_response() is invoked.
1377 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1379 case SRPT_STATE_NEED_DATA:
1380 /* DMA_TO_DEVICE (write) - RDMA read error. */
1381 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1382 ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
1383 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
1384 transport_generic_handle_data(&ioctx->cmd);
1386 case SRPT_STATE_CMD_RSP_SENT:
1388 * SRP_RSP sending failed or the SRP_RSP send completion has
1389 * not been received in time.
1391 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
1392 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1393 ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
1394 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
1395 kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
1397 case SRPT_STATE_MGMT_RSP_SENT:
1398 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1399 kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
1402 WARN_ON("ERROR: unexpected command state");
1411 * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1413 static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1415 struct srpt_send_ioctx *ioctx;
1416 enum srpt_command_state state;
1420 atomic_inc(&ch->sq_wr_avail);
1422 index = idx_from_wr_id(wr_id);
1423 ioctx = ch->ioctx_ring[index];
1424 state = srpt_get_cmd_state(ioctx);
1427 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1428 && state != SRPT_STATE_MGMT_RSP_SENT
1429 && state != SRPT_STATE_NEED_DATA
1430 && state != SRPT_STATE_DONE);
1432 /* If SRP_RSP sending failed, undo the ch->req_lim change. */
1433 if (state == SRPT_STATE_CMD_RSP_SENT
1434 || state == SRPT_STATE_MGMT_RSP_SENT)
1435 atomic_dec(&ch->req_lim);
1437 srpt_abort_cmd(ioctx);
1441 * srpt_handle_send_comp() - Process an IB send completion notification.
1443 static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1444 struct srpt_send_ioctx *ioctx)
1446 enum srpt_command_state state;
1448 atomic_inc(&ch->sq_wr_avail);
1450 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1452 if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1453 && state != SRPT_STATE_MGMT_RSP_SENT
1454 && state != SRPT_STATE_DONE))
1455 pr_debug("state = %d\n", state);
1457 if (state != SRPT_STATE_DONE)
1458 kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
1460 printk(KERN_ERR "IB completion has been received too late for"
1461 " wr_id = %u.\n", ioctx->ioctx.index);
1465 * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1467 * Note: transport_generic_handle_data() is asynchronous so unmapping the
1468 * data that has been transferred via IB RDMA must be postponed until the
1469 * check_stop_free() callback.
1471 static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1472 struct srpt_send_ioctx *ioctx,
1473 enum srpt_opcode opcode)
1475 WARN_ON(ioctx->n_rdma <= 0);
1476 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1478 if (opcode == SRPT_RDMA_READ_LAST) {
1479 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1480 SRPT_STATE_DATA_IN))
1481 transport_generic_handle_data(&ioctx->cmd);
1483 printk(KERN_ERR "%s[%d]: wrong state = %d\n", __func__,
1484 __LINE__, srpt_get_cmd_state(ioctx));
1485 } else if (opcode == SRPT_RDMA_ABORT) {
1486 ioctx->rdma_aborted = true;
1488 WARN(true, "unexpected opcode %d\n", opcode);
1493 * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1495 static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1496 struct srpt_send_ioctx *ioctx,
1497 enum srpt_opcode opcode)
1500 enum srpt_command_state state;
1501 unsigned long flags;
1504 state = srpt_get_cmd_state(ioctx);
1506 case SRPT_RDMA_READ_LAST:
1507 if (ioctx->n_rdma <= 0) {
1508 printk(KERN_ERR "Received invalid RDMA read"
1509 " error completion with idx %d\n",
1510 ioctx->ioctx.index);
1513 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1514 if (state == SRPT_STATE_NEED_DATA)
1515 srpt_abort_cmd(ioctx);
1517 printk(KERN_ERR "%s[%d]: wrong state = %d\n",
1518 __func__, __LINE__, state);
1520 case SRPT_RDMA_WRITE_LAST:
1521 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1522 ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
1523 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
1526 printk(KERN_ERR "%s[%d]: opcode = %u\n", __func__,
1533 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1534 * @ch: RDMA channel through which the request has been received.
1535 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1536 * be built in the buffer ioctx->buf points at and hence this function will
1537 * overwrite the request data.
1538 * @tag: tag of the request for which this response is being generated.
1539 * @status: value for the STATUS field of the SRP_RSP information unit.
1541 * Returns the size in bytes of the SRP_RSP response.
1543 * An SRP_RSP response contains a SCSI status or service response. See also
1544 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1545 * response. See also SPC-2 for more information about sense data.
1547 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1548 struct srpt_send_ioctx *ioctx, u64 tag,
1551 struct srp_rsp *srp_rsp;
1552 const u8 *sense_data;
1553 int sense_data_len, max_sense_len;
1556 * The lowest bit of all SAM-3 status codes is zero (see also
1557 * paragraph 5.3 in SAM-3).
1559 WARN_ON(status & 1);
1561 srp_rsp = ioctx->ioctx.buf;
1564 sense_data = ioctx->sense_data;
1565 sense_data_len = ioctx->cmd.scsi_sense_length;
1566 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1568 memset(srp_rsp, 0, sizeof *srp_rsp);
1569 srp_rsp->opcode = SRP_RSP;
1570 srp_rsp->req_lim_delta =
1571 __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1573 srp_rsp->status = status;
1575 if (sense_data_len) {
1576 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1577 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1578 if (sense_data_len > max_sense_len) {
1579 printk(KERN_WARNING "truncated sense data from %d to %d"
1580 " bytes\n", sense_data_len, max_sense_len);
1581 sense_data_len = max_sense_len;
1584 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1585 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1586 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1589 return sizeof(*srp_rsp) + sense_data_len;
1593 * srpt_build_tskmgmt_rsp() - Build a task management response.
1594 * @ch: RDMA channel through which the request has been received.
1595 * @ioctx: I/O context in which the SRP_RSP response will be built.
1596 * @rsp_code: RSP_CODE that will be stored in the response.
1597 * @tag: Tag of the request for which this response is being generated.
1599 * Returns the size in bytes of the SRP_RSP response.
1601 * An SRP_RSP response contains a SCSI status or service response. See also
1602 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1605 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1606 struct srpt_send_ioctx *ioctx,
1607 u8 rsp_code, u64 tag)
1609 struct srp_rsp *srp_rsp;
1613 resp_data_len = (rsp_code == SRP_TSK_MGMT_SUCCESS) ? 0 : 4;
1614 resp_len = sizeof(*srp_rsp) + resp_data_len;
1616 srp_rsp = ioctx->ioctx.buf;
1618 memset(srp_rsp, 0, sizeof *srp_rsp);
1620 srp_rsp->opcode = SRP_RSP;
1621 srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
1622 + atomic_xchg(&ch->req_lim_delta, 0));
1625 if (rsp_code != SRP_TSK_MGMT_SUCCESS) {
1626 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1627 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1628 srp_rsp->data[3] = rsp_code;
1634 #define NO_SUCH_LUN ((uint64_t)-1LL)
1637 * SCSI LUN addressing method. See also SAM-2 and the section about
1640 enum scsi_lun_addr_method {
1641 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1642 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1643 SCSI_LUN_ADDR_METHOD_LUN = 2,
1644 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1648 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1650 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1651 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1652 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1654 static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1656 uint64_t res = NO_SUCH_LUN;
1657 int addressing_method;
1659 if (unlikely(len < 2)) {
1660 printk(KERN_ERR "Illegal LUN length %d, expected 2 bytes or "
1667 if ((*((__be64 *)lun) &
1668 __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1672 if (*((__be16 *)&lun[2]) != 0)
1676 if (*((__be32 *)&lun[2]) != 0)
1685 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1686 switch (addressing_method) {
1687 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1688 case SCSI_LUN_ADDR_METHOD_FLAT:
1689 case SCSI_LUN_ADDR_METHOD_LUN:
1690 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1693 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1695 printk(KERN_ERR "Unimplemented LUN addressing method %u",
1704 printk(KERN_ERR "Support for multi-level LUNs has not yet been"
1709 static int srpt_check_stop_free(struct se_cmd *cmd)
1711 struct srpt_send_ioctx *ioctx;
1713 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
1714 return kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
1718 * srpt_handle_cmd() - Process SRP_CMD.
1720 static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1721 struct srpt_recv_ioctx *recv_ioctx,
1722 struct srpt_send_ioctx *send_ioctx)
1725 struct srp_cmd *srp_cmd;
1726 uint64_t unpacked_lun;
1728 enum dma_data_direction dir;
1731 BUG_ON(!send_ioctx);
1733 srp_cmd = recv_ioctx->ioctx.buf;
1734 kref_get(&send_ioctx->kref);
1735 cmd = &send_ioctx->cmd;
1736 send_ioctx->tag = srp_cmd->tag;
1738 switch (srp_cmd->task_attr) {
1739 case SRP_CMD_SIMPLE_Q:
1740 cmd->sam_task_attr = MSG_SIMPLE_TAG;
1742 case SRP_CMD_ORDERED_Q:
1744 cmd->sam_task_attr = MSG_ORDERED_TAG;
1746 case SRP_CMD_HEAD_OF_Q:
1747 cmd->sam_task_attr = MSG_HEAD_TAG;
1750 cmd->sam_task_attr = MSG_ACA_TAG;
1754 ret = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len);
1756 printk(KERN_ERR "0x%llx: parsing SRP descriptor table failed.\n",
1758 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1759 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1760 kref_put(&send_ioctx->kref, srpt_put_send_ioctx_kref);
1764 cmd->data_length = data_len;
1765 cmd->data_direction = dir;
1766 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1767 sizeof(srp_cmd->lun));
1768 if (transport_lookup_cmd_lun(cmd, unpacked_lun) < 0) {
1769 kref_put(&send_ioctx->kref, srpt_put_send_ioctx_kref);
1772 ret = transport_generic_allocate_tasks(cmd, srp_cmd->cdb);
1774 kref_put(&send_ioctx->kref, srpt_put_send_ioctx_kref);
1775 if (cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT) {
1776 srpt_queue_status(cmd);
1782 transport_handle_cdb_direct(cmd);
1786 transport_send_check_condition_and_sense(cmd, cmd->scsi_sense_reason,
1792 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1793 * @ch: RDMA channel of the task management request.
1794 * @fn: Task management function to perform.
1795 * @req_tag: Tag of the SRP task management request.
1796 * @mgmt_ioctx: I/O context of the task management request.
1798 * Returns zero if the target core will process the task management
1799 * request asynchronously.
1801 * Note: It is assumed that the initiator serializes tag-based task management
1804 static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1806 struct srpt_device *sdev;
1807 struct srpt_rdma_ch *ch;
1808 struct srpt_send_ioctx *target;
1815 sdev = ch->sport->sdev;
1817 spin_lock_irq(&sdev->spinlock);
1818 for (i = 0; i < ch->rq_size; ++i) {
1819 target = ch->ioctx_ring[i];
1820 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
1821 target->tag == tag &&
1822 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1824 /* now let the target core abort &target->cmd; */
1828 spin_unlock_irq(&sdev->spinlock);
1832 static int srp_tmr_to_tcm(int fn)
1835 case SRP_TSK_ABORT_TASK:
1836 return TMR_ABORT_TASK;
1837 case SRP_TSK_ABORT_TASK_SET:
1838 return TMR_ABORT_TASK_SET;
1839 case SRP_TSK_CLEAR_TASK_SET:
1840 return TMR_CLEAR_TASK_SET;
1841 case SRP_TSK_LUN_RESET:
1842 return TMR_LUN_RESET;
1843 case SRP_TSK_CLEAR_ACA:
1844 return TMR_CLEAR_ACA;
1851 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1853 * Returns 0 if and only if the request will be processed by the target core.
1855 * For more information about SRP_TSK_MGMT information units, see also section
1856 * 6.7 in the SRP r16a document.
1858 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1859 struct srpt_recv_ioctx *recv_ioctx,
1860 struct srpt_send_ioctx *send_ioctx)
1862 struct srp_tsk_mgmt *srp_tsk;
1864 uint64_t unpacked_lun;
1868 BUG_ON(!send_ioctx);
1870 srp_tsk = recv_ioctx->ioctx.buf;
1871 cmd = &send_ioctx->cmd;
1873 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1874 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1875 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1877 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1878 send_ioctx->tag = srp_tsk->tag;
1879 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1881 send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1882 send_ioctx->cmd.se_tmr_req->response =
1883 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
1886 res = core_tmr_alloc_req(cmd, NULL, tcm_tmr, GFP_KERNEL);
1888 send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1889 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1893 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1894 sizeof(srp_tsk->lun));
1895 res = transport_lookup_tmr_lun(&send_ioctx->cmd, unpacked_lun);
1897 pr_debug("rejecting TMR for LUN %lld\n", unpacked_lun);
1898 send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1899 send_ioctx->cmd.se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1903 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK)
1904 srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1907 kref_get(&send_ioctx->kref);
1908 if (!(send_ioctx->cmd.se_cmd_flags & SCF_SCSI_CDB_EXCEPTION))
1909 transport_generic_handle_tmr(&send_ioctx->cmd);
1911 transport_send_check_condition_and_sense(cmd,
1912 cmd->scsi_sense_reason, 0);
1917 * srpt_handle_new_iu() - Process a newly received information unit.
1918 * @ch: RDMA channel through which the information unit has been received.
1919 * @ioctx: SRPT I/O context associated with the information unit.
1921 static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1922 struct srpt_recv_ioctx *recv_ioctx,
1923 struct srpt_send_ioctx *send_ioctx)
1925 struct srp_cmd *srp_cmd;
1926 enum rdma_ch_state ch_state;
1929 BUG_ON(!recv_ioctx);
1931 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1932 recv_ioctx->ioctx.dma, srp_max_req_size,
1935 ch_state = srpt_get_ch_state(ch);
1936 if (unlikely(ch_state == CH_CONNECTING)) {
1937 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1941 if (unlikely(ch_state != CH_LIVE))
1944 srp_cmd = recv_ioctx->ioctx.buf;
1945 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1947 send_ioctx = srpt_get_send_ioctx(ch);
1948 if (unlikely(!send_ioctx)) {
1949 list_add_tail(&recv_ioctx->wait_list,
1950 &ch->cmd_wait_list);
1955 transport_init_se_cmd(&send_ioctx->cmd, &srpt_target->tf_ops, ch->sess,
1956 0, DMA_NONE, MSG_SIMPLE_TAG,
1957 send_ioctx->sense_data);
1959 switch (srp_cmd->opcode) {
1961 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1964 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1967 printk(KERN_ERR "Not yet implemented: SRP_I_LOGOUT\n");
1970 pr_debug("received SRP_CRED_RSP\n");
1973 pr_debug("received SRP_AER_RSP\n");
1976 printk(KERN_ERR "Received SRP_RSP\n");
1979 printk(KERN_ERR "received IU with unknown opcode 0x%x\n",
1984 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1989 static void srpt_process_rcv_completion(struct ib_cq *cq,
1990 struct srpt_rdma_ch *ch,
1993 struct srpt_device *sdev = ch->sport->sdev;
1994 struct srpt_recv_ioctx *ioctx;
1997 index = idx_from_wr_id(wc->wr_id);
1998 if (wc->status == IB_WC_SUCCESS) {
2001 req_lim = atomic_dec_return(&ch->req_lim);
2002 if (unlikely(req_lim < 0))
2003 printk(KERN_ERR "req_lim = %d < 0\n", req_lim);
2004 ioctx = sdev->ioctx_ring[index];
2005 srpt_handle_new_iu(ch, ioctx, NULL);
2007 printk(KERN_INFO "receiving failed for idx %u with status %d\n",
2013 * srpt_process_send_completion() - Process an IB send completion.
2015 * Note: Although this has not yet been observed during tests, at least in
2016 * theory it is possible that the srpt_get_send_ioctx() call invoked by
2017 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
2018 * value in each response is set to one, and it is possible that this response
2019 * makes the initiator send a new request before the send completion for that
2020 * response has been processed. This could e.g. happen if the call to
2021 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
2022 * if IB retransmission causes generation of the send completion to be
2023 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
2024 * are queued on cmd_wait_list. The code below processes these delayed
2025 * requests one at a time.
2027 static void srpt_process_send_completion(struct ib_cq *cq,
2028 struct srpt_rdma_ch *ch,
2031 struct srpt_send_ioctx *send_ioctx;
2033 enum srpt_opcode opcode;
2035 index = idx_from_wr_id(wc->wr_id);
2036 opcode = opcode_from_wr_id(wc->wr_id);
2037 send_ioctx = ch->ioctx_ring[index];
2038 if (wc->status == IB_WC_SUCCESS) {
2039 if (opcode == SRPT_SEND)
2040 srpt_handle_send_comp(ch, send_ioctx);
2042 WARN_ON(opcode != SRPT_RDMA_ABORT &&
2043 wc->opcode != IB_WC_RDMA_READ);
2044 srpt_handle_rdma_comp(ch, send_ioctx, opcode);
2047 if (opcode == SRPT_SEND) {
2048 printk(KERN_INFO "sending response for idx %u failed"
2049 " with status %d\n", index, wc->status);
2050 srpt_handle_send_err_comp(ch, wc->wr_id);
2051 } else if (opcode != SRPT_RDMA_MID) {
2052 printk(KERN_INFO "RDMA t %d for idx %u failed with"
2053 " status %d", opcode, index, wc->status);
2054 srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
2058 while (unlikely(opcode == SRPT_SEND
2059 && !list_empty(&ch->cmd_wait_list)
2060 && srpt_get_ch_state(ch) == CH_LIVE
2061 && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
2062 struct srpt_recv_ioctx *recv_ioctx;
2064 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
2065 struct srpt_recv_ioctx,
2067 list_del(&recv_ioctx->wait_list);
2068 srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
2072 static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
2074 struct ib_wc *const wc = ch->wc;
2077 WARN_ON(cq != ch->cq);
2079 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
2080 while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
2081 for (i = 0; i < n; i++) {
2082 if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
2083 srpt_process_rcv_completion(cq, ch, &wc[i]);
2085 srpt_process_send_completion(cq, ch, &wc[i]);
2091 * srpt_completion() - IB completion queue callback function.
2094 * - It is guaranteed that a completion handler will never be invoked
2095 * concurrently on two different CPUs for the same completion queue. See also
2096 * Documentation/infiniband/core_locking.txt and the implementation of
2097 * handle_edge_irq() in kernel/irq/chip.c.
2098 * - When threaded IRQs are enabled, completion handlers are invoked in thread
2099 * context instead of interrupt context.
2101 static void srpt_completion(struct ib_cq *cq, void *ctx)
2103 struct srpt_rdma_ch *ch = ctx;
2105 wake_up_interruptible(&ch->wait_queue);
2108 static int srpt_compl_thread(void *arg)
2110 struct srpt_rdma_ch *ch;
2112 /* Hibernation / freezing of the SRPT kernel thread is not supported. */
2113 current->flags |= PF_NOFREEZE;
2117 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) started\n",
2118 ch->sess_name, ch->thread->comm, current->pid);
2119 while (!kthread_should_stop()) {
2120 wait_event_interruptible(ch->wait_queue,
2121 (srpt_process_completion(ch->cq, ch),
2122 kthread_should_stop()));
2124 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) stopped\n",
2125 ch->sess_name, ch->thread->comm, current->pid);
2130 * srpt_create_ch_ib() - Create receive and send completion queues.
2132 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2134 struct ib_qp_init_attr *qp_init;
2135 struct srpt_port *sport = ch->sport;
2136 struct srpt_device *sdev = sport->sdev;
2137 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
2140 WARN_ON(ch->rq_size < 1);
2143 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2147 ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
2148 ch->rq_size + srp_sq_size, 0);
2149 if (IS_ERR(ch->cq)) {
2150 ret = PTR_ERR(ch->cq);
2151 printk(KERN_ERR "failed to create CQ cqe= %d ret= %d\n",
2152 ch->rq_size + srp_sq_size, ret);
2156 qp_init->qp_context = (void *)ch;
2157 qp_init->event_handler
2158 = (void(*)(struct ib_event *, void*))srpt_qp_event;
2159 qp_init->send_cq = ch->cq;
2160 qp_init->recv_cq = ch->cq;
2161 qp_init->srq = sdev->srq;
2162 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2163 qp_init->qp_type = IB_QPT_RC;
2164 qp_init->cap.max_send_wr = srp_sq_size;
2165 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2167 ch->qp = ib_create_qp(sdev->pd, qp_init);
2168 if (IS_ERR(ch->qp)) {
2169 ret = PTR_ERR(ch->qp);
2170 printk(KERN_ERR "failed to create_qp ret= %d\n", ret);
2171 goto err_destroy_cq;
2174 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2176 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2177 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2178 qp_init->cap.max_send_wr, ch->cm_id);
2180 ret = srpt_init_ch_qp(ch, ch->qp);
2182 goto err_destroy_qp;
2184 init_waitqueue_head(&ch->wait_queue);
2186 pr_debug("creating thread for session %s\n", ch->sess_name);
2188 ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2189 if (IS_ERR(ch->thread)) {
2190 printk(KERN_ERR "failed to create kernel thread %ld\n",
2191 PTR_ERR(ch->thread));
2193 goto err_destroy_qp;
2201 ib_destroy_qp(ch->qp);
2203 ib_destroy_cq(ch->cq);
2207 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2210 kthread_stop(ch->thread);
2212 ib_destroy_qp(ch->qp);
2213 ib_destroy_cq(ch->cq);
2217 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2219 * Reset the QP and make sure all resources associated with the channel will
2220 * be deallocated at an appropriate time.
2222 * Note: The caller must hold ch->sport->sdev->spinlock.
2224 static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2226 struct srpt_device *sdev;
2227 enum rdma_ch_state prev_state;
2228 unsigned long flags;
2230 sdev = ch->sport->sdev;
2232 spin_lock_irqsave(&ch->spinlock, flags);
2233 prev_state = ch->state;
2234 switch (prev_state) {
2237 ch->state = CH_DISCONNECTING;
2242 spin_unlock_irqrestore(&ch->spinlock, flags);
2244 switch (prev_state) {
2246 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2250 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
2251 printk(KERN_ERR "sending CM DREQ failed.\n");
2253 case CH_DISCONNECTING:
2262 * srpt_close_ch() - Close an RDMA channel.
2264 static void srpt_close_ch(struct srpt_rdma_ch *ch)
2266 struct srpt_device *sdev;
2268 sdev = ch->sport->sdev;
2269 spin_lock_irq(&sdev->spinlock);
2270 __srpt_close_ch(ch);
2271 spin_unlock_irq(&sdev->spinlock);
2275 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2276 * @cm_id: Pointer to the CM ID of the channel to be drained.
2278 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2279 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2280 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2281 * waits until all target sessions for the associated IB device have been
2282 * unregistered and target session registration involves a call to
2283 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2284 * this function has finished).
2286 static void srpt_drain_channel(struct ib_cm_id *cm_id)
2288 struct srpt_device *sdev;
2289 struct srpt_rdma_ch *ch;
2291 bool do_reset = false;
2293 WARN_ON_ONCE(irqs_disabled());
2295 sdev = cm_id->context;
2297 spin_lock_irq(&sdev->spinlock);
2298 list_for_each_entry(ch, &sdev->rch_list, list) {
2299 if (ch->cm_id == cm_id) {
2300 do_reset = srpt_test_and_set_ch_state(ch,
2301 CH_CONNECTING, CH_DRAINING) ||
2302 srpt_test_and_set_ch_state(ch,
2303 CH_LIVE, CH_DRAINING) ||
2304 srpt_test_and_set_ch_state(ch,
2305 CH_DISCONNECTING, CH_DRAINING);
2309 spin_unlock_irq(&sdev->spinlock);
2312 ret = srpt_ch_qp_err(ch);
2314 printk(KERN_ERR "Setting queue pair in error state"
2315 " failed: %d\n", ret);
2320 * srpt_find_channel() - Look up an RDMA channel.
2321 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2323 * Return NULL if no matching RDMA channel has been found.
2325 static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2326 struct ib_cm_id *cm_id)
2328 struct srpt_rdma_ch *ch;
2331 WARN_ON_ONCE(irqs_disabled());
2335 spin_lock_irq(&sdev->spinlock);
2336 list_for_each_entry(ch, &sdev->rch_list, list) {
2337 if (ch->cm_id == cm_id) {
2342 spin_unlock_irq(&sdev->spinlock);
2344 return found ? ch : NULL;
2348 * srpt_release_channel() - Release channel resources.
2350 * Schedules the actual release because:
2351 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2352 * trigger a deadlock.
2353 * - It is not safe to call TCM transport_* functions from interrupt context.
2355 static void srpt_release_channel(struct srpt_rdma_ch *ch)
2357 schedule_work(&ch->release_work);
2360 static void srpt_release_channel_work(struct work_struct *w)
2362 struct srpt_rdma_ch *ch;
2363 struct srpt_device *sdev;
2365 ch = container_of(w, struct srpt_rdma_ch, release_work);
2366 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2369 sdev = ch->sport->sdev;
2372 transport_deregister_session_configfs(ch->sess);
2373 transport_deregister_session(ch->sess);
2376 srpt_destroy_ch_ib(ch);
2378 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2379 ch->sport->sdev, ch->rq_size,
2380 ch->rsp_size, DMA_TO_DEVICE);
2382 spin_lock_irq(&sdev->spinlock);
2383 list_del(&ch->list);
2384 spin_unlock_irq(&sdev->spinlock);
2386 ib_destroy_cm_id(ch->cm_id);
2388 if (ch->release_done)
2389 complete(ch->release_done);
2391 wake_up(&sdev->ch_releaseQ);
2396 static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2399 struct srpt_node_acl *nacl;
2401 list_for_each_entry(nacl, &sport->port_acl_list, list)
2402 if (memcmp(nacl->i_port_id, i_port_id,
2403 sizeof(nacl->i_port_id)) == 0)
2409 static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2412 struct srpt_node_acl *nacl;
2414 spin_lock_irq(&sport->port_acl_lock);
2415 nacl = __srpt_lookup_acl(sport, i_port_id);
2416 spin_unlock_irq(&sport->port_acl_lock);
2422 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2424 * Ownership of the cm_id is transferred to the target session if this
2425 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2427 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2428 struct ib_cm_req_event_param *param,
2431 struct srpt_device *sdev = cm_id->context;
2432 struct srpt_port *sport = &sdev->port[param->port - 1];
2433 struct srp_login_req *req;
2434 struct srp_login_rsp *rsp;
2435 struct srp_login_rej *rej;
2436 struct ib_cm_rep_param *rep_param;
2437 struct srpt_rdma_ch *ch, *tmp_ch;
2438 struct srpt_node_acl *nacl;
2443 WARN_ON_ONCE(irqs_disabled());
2445 if (WARN_ON(!sdev || !private_data))
2448 req = (struct srp_login_req *)private_data;
2450 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2452 printk(KERN_INFO "Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2453 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2454 " (guid=0x%llx:0x%llx)\n",
2455 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2456 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2457 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2458 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2461 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2462 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
2464 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2465 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2466 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2468 if (!rsp || !rej || !rep_param) {
2473 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2474 rej->reason = __constant_cpu_to_be32(
2475 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2477 printk(KERN_ERR "rejected SRP_LOGIN_REQ because its"
2478 " length (%d bytes) is out of range (%d .. %d)\n",
2479 it_iu_len, 64, srp_max_req_size);
2483 if (!sport->enabled) {
2484 rej->reason = __constant_cpu_to_be32(
2485 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2487 printk(KERN_ERR "rejected SRP_LOGIN_REQ because the target port"
2488 " has not yet been enabled\n");
2492 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2493 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2495 spin_lock_irq(&sdev->spinlock);
2497 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2498 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2499 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2500 && param->port == ch->sport->port
2501 && param->listen_id == ch->sport->sdev->cm_id
2503 enum rdma_ch_state ch_state;
2505 ch_state = srpt_get_ch_state(ch);
2506 if (ch_state != CH_CONNECTING
2507 && ch_state != CH_LIVE)
2510 /* found an existing channel */
2511 pr_debug("Found existing channel %s"
2512 " cm_id= %p state= %d\n",
2513 ch->sess_name, ch->cm_id, ch_state);
2515 __srpt_close_ch(ch);
2518 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2522 spin_unlock_irq(&sdev->spinlock);
2525 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2527 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2528 || *(__be64 *)(req->target_port_id + 8) !=
2529 cpu_to_be64(srpt_service_guid)) {
2530 rej->reason = __constant_cpu_to_be32(
2531 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2533 printk(KERN_ERR "rejected SRP_LOGIN_REQ because it"
2534 " has an invalid target port identifier.\n");
2538 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2540 rej->reason = __constant_cpu_to_be32(
2541 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2542 printk(KERN_ERR "rejected SRP_LOGIN_REQ because no memory.\n");
2547 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2548 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2549 memcpy(ch->t_port_id, req->target_port_id, 16);
2550 ch->sport = &sdev->port[param->port - 1];
2553 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2554 * for the SRP protocol to the command queue size.
2556 ch->rq_size = SRPT_RQ_SIZE;
2557 spin_lock_init(&ch->spinlock);
2558 ch->state = CH_CONNECTING;
2559 INIT_LIST_HEAD(&ch->cmd_wait_list);
2560 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2562 ch->ioctx_ring = (struct srpt_send_ioctx **)
2563 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2564 sizeof(*ch->ioctx_ring[0]),
2565 ch->rsp_size, DMA_TO_DEVICE);
2566 if (!ch->ioctx_ring)
2569 INIT_LIST_HEAD(&ch->free_list);
2570 for (i = 0; i < ch->rq_size; i++) {
2571 ch->ioctx_ring[i]->ch = ch;
2572 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2575 ret = srpt_create_ch_ib(ch);
2577 rej->reason = __constant_cpu_to_be32(
2578 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2579 printk(KERN_ERR "rejected SRP_LOGIN_REQ because creating"
2580 " a new RDMA channel failed.\n");
2584 ret = srpt_ch_qp_rtr(ch, ch->qp);
2586 rej->reason = __constant_cpu_to_be32(
2587 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2588 printk(KERN_ERR "rejected SRP_LOGIN_REQ because enabling"
2589 " RTR failed (error code = %d)\n", ret);
2593 * Use the initator port identifier as the session name.
2595 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2596 be64_to_cpu(*(__be64 *)ch->i_port_id),
2597 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2599 pr_debug("registering session %s\n", ch->sess_name);
2601 nacl = srpt_lookup_acl(sport, ch->i_port_id);
2603 printk(KERN_INFO "Rejected login because no ACL has been"
2604 " configured yet for initiator %s.\n", ch->sess_name);
2605 rej->reason = __constant_cpu_to_be32(
2606 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2610 ch->sess = transport_init_session();
2611 if (IS_ERR(ch->sess)) {
2612 rej->reason = __constant_cpu_to_be32(
2613 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2614 pr_debug("Failed to create session\n");
2615 goto deregister_session;
2617 ch->sess->se_node_acl = &nacl->nacl;
2618 transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2620 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2621 ch->sess_name, ch->cm_id);
2623 /* create srp_login_response */
2624 rsp->opcode = SRP_LOGIN_RSP;
2625 rsp->tag = req->tag;
2626 rsp->max_it_iu_len = req->req_it_iu_len;
2627 rsp->max_ti_iu_len = req->req_it_iu_len;
2628 ch->max_ti_iu_len = it_iu_len;
2629 rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2630 | SRP_BUF_FORMAT_INDIRECT);
2631 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2632 atomic_set(&ch->req_lim, ch->rq_size);
2633 atomic_set(&ch->req_lim_delta, 0);
2635 /* create cm reply */
2636 rep_param->qp_num = ch->qp->qp_num;
2637 rep_param->private_data = (void *)rsp;
2638 rep_param->private_data_len = sizeof *rsp;
2639 rep_param->rnr_retry_count = 7;
2640 rep_param->flow_control = 1;
2641 rep_param->failover_accepted = 0;
2643 rep_param->responder_resources = 4;
2644 rep_param->initiator_depth = 4;
2646 ret = ib_send_cm_rep(cm_id, rep_param);
2648 printk(KERN_ERR "sending SRP_LOGIN_REQ response failed"
2649 " (error code = %d)\n", ret);
2650 goto release_channel;
2653 spin_lock_irq(&sdev->spinlock);
2654 list_add_tail(&ch->list, &sdev->rch_list);
2655 spin_unlock_irq(&sdev->spinlock);
2660 srpt_set_ch_state(ch, CH_RELEASING);
2661 transport_deregister_session_configfs(ch->sess);
2664 transport_deregister_session(ch->sess);
2668 srpt_destroy_ch_ib(ch);
2671 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2672 ch->sport->sdev, ch->rq_size,
2673 ch->rsp_size, DMA_TO_DEVICE);
2678 rej->opcode = SRP_LOGIN_REJ;
2679 rej->tag = req->tag;
2680 rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2681 | SRP_BUF_FORMAT_INDIRECT);
2683 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2684 (void *)rej, sizeof *rej);
2694 static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2696 printk(KERN_INFO "Received IB REJ for cm_id %p.\n", cm_id);
2697 srpt_drain_channel(cm_id);
2701 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2703 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2704 * and that the recipient may begin transmitting (RTU = ready to use).
2706 static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2708 struct srpt_rdma_ch *ch;
2711 ch = srpt_find_channel(cm_id->context, cm_id);
2714 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2715 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2717 ret = srpt_ch_qp_rts(ch, ch->qp);
2719 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2721 list_del(&ioctx->wait_list);
2722 srpt_handle_new_iu(ch, ioctx, NULL);
2729 static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2731 printk(KERN_INFO "Received IB TimeWait exit for cm_id %p.\n", cm_id);
2732 srpt_drain_channel(cm_id);
2735 static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2737 printk(KERN_INFO "Received IB REP error for cm_id %p.\n", cm_id);
2738 srpt_drain_channel(cm_id);
2742 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2744 static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2746 struct srpt_rdma_ch *ch;
2747 unsigned long flags;
2748 bool send_drep = false;
2750 ch = srpt_find_channel(cm_id->context, cm_id);
2753 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2755 spin_lock_irqsave(&ch->spinlock, flags);
2756 switch (ch->state) {
2760 ch->state = CH_DISCONNECTING;
2762 case CH_DISCONNECTING:
2765 WARN(true, "unexpected channel state %d\n", ch->state);
2768 spin_unlock_irqrestore(&ch->spinlock, flags);
2771 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
2772 printk(KERN_ERR "Sending IB DREP failed.\n");
2773 printk(KERN_INFO "Received DREQ and sent DREP for session %s.\n",
2779 * srpt_cm_drep_recv() - Process reception of a DREP message.
2781 static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2783 printk(KERN_INFO "Received InfiniBand DREP message for cm_id %p.\n",
2785 srpt_drain_channel(cm_id);
2789 * srpt_cm_handler() - IB connection manager callback function.
2791 * A non-zero return value will cause the caller destroy the CM ID.
2793 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2794 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2795 * a non-zero value in any other case will trigger a race with the
2796 * ib_destroy_cm_id() call in srpt_release_channel().
2798 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2803 switch (event->event) {
2804 case IB_CM_REQ_RECEIVED:
2805 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2806 event->private_data);
2808 case IB_CM_REJ_RECEIVED:
2809 srpt_cm_rej_recv(cm_id);
2811 case IB_CM_RTU_RECEIVED:
2812 case IB_CM_USER_ESTABLISHED:
2813 srpt_cm_rtu_recv(cm_id);
2815 case IB_CM_DREQ_RECEIVED:
2816 srpt_cm_dreq_recv(cm_id);
2818 case IB_CM_DREP_RECEIVED:
2819 srpt_cm_drep_recv(cm_id);
2821 case IB_CM_TIMEWAIT_EXIT:
2822 srpt_cm_timewait_exit(cm_id);
2824 case IB_CM_REP_ERROR:
2825 srpt_cm_rep_error(cm_id);
2827 case IB_CM_DREQ_ERROR:
2828 printk(KERN_INFO "Received IB DREQ ERROR event.\n");
2830 case IB_CM_MRA_RECEIVED:
2831 printk(KERN_INFO "Received IB MRA event\n");
2834 printk(KERN_ERR "received unrecognized IB CM event %d\n",
2843 * srpt_perform_rdmas() - Perform IB RDMA.
2845 * Returns zero upon success or a negative number upon failure.
2847 static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2848 struct srpt_send_ioctx *ioctx)
2850 struct ib_send_wr wr;
2851 struct ib_send_wr *bad_wr;
2852 struct rdma_iu *riu;
2856 enum dma_data_direction dir;
2857 const int n_rdma = ioctx->n_rdma;
2859 dir = ioctx->cmd.data_direction;
2860 if (dir == DMA_TO_DEVICE) {
2863 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2864 if (sq_wr_avail < 0) {
2865 printk(KERN_WARNING "IB send queue full (needed %d)\n",
2871 ioctx->rdma_aborted = false;
2873 riu = ioctx->rdma_ius;
2874 memset(&wr, 0, sizeof wr);
2876 for (i = 0; i < n_rdma; ++i, ++riu) {
2877 if (dir == DMA_FROM_DEVICE) {
2878 wr.opcode = IB_WR_RDMA_WRITE;
2879 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2880 SRPT_RDMA_WRITE_LAST :
2882 ioctx->ioctx.index);
2884 wr.opcode = IB_WR_RDMA_READ;
2885 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2886 SRPT_RDMA_READ_LAST :
2888 ioctx->ioctx.index);
2891 wr.wr.rdma.remote_addr = riu->raddr;
2892 wr.wr.rdma.rkey = riu->rkey;
2893 wr.num_sge = riu->sge_cnt;
2894 wr.sg_list = riu->sge;
2896 /* only get completion event for the last rdma write */
2897 if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
2898 wr.send_flags = IB_SEND_SIGNALED;
2900 ret = ib_post_send(ch->qp, &wr, &bad_wr);
2906 printk(KERN_ERR "%s[%d]: ib_post_send() returned %d for %d/%d",
2907 __func__, __LINE__, ret, i, n_rdma);
2910 wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2911 wr.send_flags = IB_SEND_SIGNALED;
2912 while (ch->state == CH_LIVE &&
2913 ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
2914 printk(KERN_INFO "Trying to abort failed RDMA transfer [%d]",
2915 ioctx->ioctx.index);
2918 while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
2919 printk(KERN_INFO "Waiting until RDMA abort finished [%d]",
2920 ioctx->ioctx.index);
2925 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2926 atomic_add(n_rdma, &ch->sq_wr_avail);
2931 * srpt_xfer_data() - Start data transfer from initiator to target.
2933 static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2934 struct srpt_send_ioctx *ioctx)
2938 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2940 printk(KERN_ERR "%s[%d] ret=%d\n", __func__, __LINE__, ret);
2944 ret = srpt_perform_rdmas(ch, ioctx);
2946 if (ret == -EAGAIN || ret == -ENOMEM)
2947 printk(KERN_INFO "%s[%d] queue full -- ret=%d\n",
2948 __func__, __LINE__, ret);
2950 printk(KERN_ERR "%s[%d] fatal error -- ret=%d\n",
2951 __func__, __LINE__, ret);
2958 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2962 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2964 struct srpt_send_ioctx *ioctx;
2966 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2967 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2971 * srpt_write_pending() - Start data transfer from initiator to target (write).
2973 static int srpt_write_pending(struct se_cmd *se_cmd)
2975 struct srpt_rdma_ch *ch;
2976 struct srpt_send_ioctx *ioctx;
2977 enum srpt_command_state new_state;
2978 enum rdma_ch_state ch_state;
2981 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2983 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2984 WARN_ON(new_state == SRPT_STATE_DONE);
2989 ch_state = srpt_get_ch_state(ch);
2992 WARN(true, "unexpected channel state %d\n", ch_state);
2997 case CH_DISCONNECTING:
3000 pr_debug("cmd with tag %lld: channel disconnecting\n",
3002 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
3006 ret = srpt_xfer_data(ch, ioctx);
3012 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
3014 switch (tcm_mgmt_status) {
3015 case TMR_FUNCTION_COMPLETE:
3016 return SRP_TSK_MGMT_SUCCESS;
3017 case TMR_FUNCTION_REJECTED:
3018 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
3020 return SRP_TSK_MGMT_FAILED;
3024 * srpt_queue_response() - Transmits the response to a SCSI command.
3026 * Callback function called by the TCM core. Must not block since it can be
3027 * invoked on the context of the IB completion handler.
3029 static int srpt_queue_response(struct se_cmd *cmd)
3031 struct srpt_rdma_ch *ch;
3032 struct srpt_send_ioctx *ioctx;
3033 enum srpt_command_state state;
3034 unsigned long flags;
3036 enum dma_data_direction dir;
3042 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3046 spin_lock_irqsave(&ioctx->spinlock, flags);
3047 state = ioctx->state;
3049 case SRPT_STATE_NEW:
3050 case SRPT_STATE_DATA_IN:
3051 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
3053 case SRPT_STATE_MGMT:
3054 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
3057 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
3058 ch, ioctx->ioctx.index, ioctx->state);
3061 spin_unlock_irqrestore(&ioctx->spinlock, flags);
3063 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
3064 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
3065 atomic_inc(&ch->req_lim_delta);
3066 srpt_abort_cmd(ioctx);
3070 dir = ioctx->cmd.data_direction;
3072 /* For read commands, transfer the data to the initiator. */
3073 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
3074 !ioctx->queue_status_only) {
3075 ret = srpt_xfer_data(ch, ioctx);
3077 printk(KERN_ERR "xfer_data failed for tag %llu\n",
3083 if (state != SRPT_STATE_MGMT)
3084 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
3088 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3089 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
3092 ret = srpt_post_send(ch, ioctx, resp_len);
3094 printk(KERN_ERR "sending cmd response failed for tag %llu\n",
3096 srpt_unmap_sg_to_ib_sge(ch, ioctx);
3097 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
3098 kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
3105 static int srpt_queue_status(struct se_cmd *cmd)
3107 struct srpt_send_ioctx *ioctx;
3109 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3110 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3111 if (cmd->se_cmd_flags &
3112 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3113 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3114 ioctx->queue_status_only = true;
3115 return srpt_queue_response(cmd);
3118 static void srpt_refresh_port_work(struct work_struct *work)
3120 struct srpt_port *sport = container_of(work, struct srpt_port, work);
3122 srpt_refresh_port(sport);
3125 static int srpt_ch_list_empty(struct srpt_device *sdev)
3129 spin_lock_irq(&sdev->spinlock);
3130 res = list_empty(&sdev->rch_list);
3131 spin_unlock_irq(&sdev->spinlock);
3137 * srpt_release_sdev() - Free the channel resources associated with a target.
3139 static int srpt_release_sdev(struct srpt_device *sdev)
3141 struct srpt_rdma_ch *ch, *tmp_ch;
3144 WARN_ON_ONCE(irqs_disabled());
3148 spin_lock_irq(&sdev->spinlock);
3149 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3150 __srpt_close_ch(ch);
3151 spin_unlock_irq(&sdev->spinlock);
3153 res = wait_event_interruptible(sdev->ch_releaseQ,
3154 srpt_ch_list_empty(sdev));
3156 printk(KERN_ERR "%s: interrupted.\n", __func__);
3161 static struct srpt_port *__srpt_lookup_port(const char *name)
3163 struct ib_device *dev;
3164 struct srpt_device *sdev;
3165 struct srpt_port *sport;
3168 list_for_each_entry(sdev, &srpt_dev_list, list) {
3173 for (i = 0; i < dev->phys_port_cnt; i++) {
3174 sport = &sdev->port[i];
3176 if (!strcmp(sport->port_guid, name))
3184 static struct srpt_port *srpt_lookup_port(const char *name)
3186 struct srpt_port *sport;
3188 spin_lock(&srpt_dev_lock);
3189 sport = __srpt_lookup_port(name);
3190 spin_unlock(&srpt_dev_lock);
3196 * srpt_add_one() - Infiniband device addition callback function.
3198 static void srpt_add_one(struct ib_device *device)
3200 struct srpt_device *sdev;
3201 struct srpt_port *sport;
3202 struct ib_srq_init_attr srq_attr;
3205 pr_debug("device = %p, device->dma_ops = %p\n", device,
3208 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3212 sdev->device = device;
3213 INIT_LIST_HEAD(&sdev->rch_list);
3214 init_waitqueue_head(&sdev->ch_releaseQ);
3215 spin_lock_init(&sdev->spinlock);
3217 if (ib_query_device(device, &sdev->dev_attr))
3220 sdev->pd = ib_alloc_pd(device);
3221 if (IS_ERR(sdev->pd))
3224 sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
3225 if (IS_ERR(sdev->mr))
3228 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3230 srq_attr.event_handler = srpt_srq_event;
3231 srq_attr.srq_context = (void *)sdev;
3232 srq_attr.attr.max_wr = sdev->srq_size;
3233 srq_attr.attr.max_sge = 1;
3234 srq_attr.attr.srq_limit = 0;
3235 srq_attr.srq_type = IB_SRQT_BASIC;
3237 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3238 if (IS_ERR(sdev->srq))
3241 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3242 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3245 if (!srpt_service_guid)
3246 srpt_service_guid = be64_to_cpu(device->node_guid);
3248 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3249 if (IS_ERR(sdev->cm_id))
3252 /* print out target login information */
3253 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3254 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3255 srpt_service_guid, srpt_service_guid);
3258 * We do not have a consistent service_id (ie. also id_ext of target_id)
3259 * to identify this target. We currently use the guid of the first HCA
3260 * in the system as service_id; therefore, the target_id will change
3261 * if this HCA is gone bad and replaced by different HCA
3263 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
3266 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3267 srpt_event_handler);
3268 if (ib_register_event_handler(&sdev->event_handler))
3271 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3272 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3273 sizeof(*sdev->ioctx_ring[0]),
3274 srp_max_req_size, DMA_FROM_DEVICE);
3275 if (!sdev->ioctx_ring)
3278 for (i = 0; i < sdev->srq_size; ++i)
3279 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3281 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
3283 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3284 sport = &sdev->port[i - 1];
3287 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3288 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3289 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3290 INIT_WORK(&sport->work, srpt_refresh_port_work);
3291 INIT_LIST_HEAD(&sport->port_acl_list);
3292 spin_lock_init(&sport->port_acl_lock);
3294 if (srpt_refresh_port(sport)) {
3295 printk(KERN_ERR "MAD registration failed for %s-%d.\n",
3296 srpt_sdev_name(sdev), i);
3299 snprintf(sport->port_guid, sizeof(sport->port_guid),
3301 be64_to_cpu(sport->gid.global.subnet_prefix),
3302 be64_to_cpu(sport->gid.global.interface_id));
3305 spin_lock(&srpt_dev_lock);
3306 list_add_tail(&sdev->list, &srpt_dev_list);
3307 spin_unlock(&srpt_dev_lock);
3310 ib_set_client_data(device, &srpt_client, sdev);
3311 pr_debug("added %s.\n", device->name);
3315 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3316 sdev->srq_size, srp_max_req_size,
3319 ib_unregister_event_handler(&sdev->event_handler);
3321 ib_destroy_cm_id(sdev->cm_id);
3323 ib_destroy_srq(sdev->srq);
3325 ib_dereg_mr(sdev->mr);
3327 ib_dealloc_pd(sdev->pd);
3332 printk(KERN_INFO "%s(%s) failed.\n", __func__, device->name);
3337 * srpt_remove_one() - InfiniBand device removal callback function.
3339 static void srpt_remove_one(struct ib_device *device)
3341 struct srpt_device *sdev;
3344 sdev = ib_get_client_data(device, &srpt_client);
3346 printk(KERN_INFO "%s(%s): nothing to do.\n", __func__,
3351 srpt_unregister_mad_agent(sdev);
3353 ib_unregister_event_handler(&sdev->event_handler);
3355 /* Cancel any work queued by the just unregistered IB event handler. */
3356 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3357 cancel_work_sync(&sdev->port[i].work);
3359 ib_destroy_cm_id(sdev->cm_id);
3362 * Unregistering a target must happen after destroying sdev->cm_id
3363 * such that no new SRP_LOGIN_REQ information units can arrive while
3364 * destroying the target.
3366 spin_lock(&srpt_dev_lock);
3367 list_del(&sdev->list);
3368 spin_unlock(&srpt_dev_lock);
3369 srpt_release_sdev(sdev);
3371 ib_destroy_srq(sdev->srq);
3372 ib_dereg_mr(sdev->mr);
3373 ib_dealloc_pd(sdev->pd);
3375 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3376 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3377 sdev->ioctx_ring = NULL;
3381 static struct ib_client srpt_client = {
3383 .add = srpt_add_one,
3384 .remove = srpt_remove_one
3387 static int srpt_check_true(struct se_portal_group *se_tpg)
3392 static int srpt_check_false(struct se_portal_group *se_tpg)
3397 static char *srpt_get_fabric_name(void)
3402 static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
3404 return SCSI_TRANSPORTID_PROTOCOLID_SRP;
3407 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3409 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3411 return sport->port_guid;
3414 static u16 srpt_get_tag(struct se_portal_group *tpg)
3419 static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
3424 static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
3425 struct se_node_acl *se_nacl,
3426 struct t10_pr_registration *pr_reg,
3427 int *format_code, unsigned char *buf)
3429 struct srpt_node_acl *nacl;
3430 struct spc_rdma_transport_id *tr_id;
3432 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3433 tr_id = (void *)buf;
3434 tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
3435 memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
3436 return sizeof(*tr_id);
3439 static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
3440 struct se_node_acl *se_nacl,
3441 struct t10_pr_registration *pr_reg,
3445 return sizeof(struct spc_rdma_transport_id);
3448 static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
3449 const char *buf, u32 *out_tid_len,
3450 char **port_nexus_ptr)
3452 struct spc_rdma_transport_id *tr_id;
3454 *port_nexus_ptr = NULL;
3455 *out_tid_len = sizeof(struct spc_rdma_transport_id);
3456 tr_id = (void *)buf;
3457 return (char *)tr_id->i_port_id;
3460 static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
3462 struct srpt_node_acl *nacl;
3464 nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
3466 printk(KERN_ERR "Unable to allocate struct srpt_node_acl\n");
3473 static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
3474 struct se_node_acl *se_nacl)
3476 struct srpt_node_acl *nacl;
3478 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3482 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3487 static void srpt_release_cmd(struct se_cmd *se_cmd)
3492 * srpt_shutdown_session() - Whether or not a session may be shut down.
3494 static int srpt_shutdown_session(struct se_session *se_sess)
3500 * srpt_close_session() - Forcibly close a session.
3502 * Callback function invoked by the TCM core to clean up sessions associated
3503 * with a node ACL when the user invokes
3504 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3506 static void srpt_close_session(struct se_session *se_sess)
3508 DECLARE_COMPLETION_ONSTACK(release_done);
3509 struct srpt_rdma_ch *ch;
3510 struct srpt_device *sdev;
3513 ch = se_sess->fabric_sess_ptr;
3514 WARN_ON(ch->sess != se_sess);
3516 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3518 sdev = ch->sport->sdev;
3519 spin_lock_irq(&sdev->spinlock);
3520 BUG_ON(ch->release_done);
3521 ch->release_done = &release_done;
3522 __srpt_close_ch(ch);
3523 spin_unlock_irq(&sdev->spinlock);
3525 res = wait_for_completion_timeout(&release_done, 60 * HZ);
3530 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3532 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3533 * This object represents an arbitrary integer used to uniquely identify a
3534 * particular attached remote initiator port to a particular SCSI target port
3535 * within a particular SCSI target device within a particular SCSI instance.
3537 static u32 srpt_sess_get_index(struct se_session *se_sess)
3542 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3546 static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
3548 struct srpt_send_ioctx *ioctx;
3550 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3554 /* Note: only used from inside debug printk's by the TCM core. */
3555 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3557 struct srpt_send_ioctx *ioctx;
3559 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3560 return srpt_get_cmd_state(ioctx);
3563 static u16 srpt_set_fabric_sense_len(struct se_cmd *cmd, u32 sense_length)
3568 static u16 srpt_get_fabric_sense_len(void)
3574 * srpt_parse_i_port_id() - Parse an initiator port ID.
3575 * @name: ASCII representation of a 128-bit initiator port ID.
3576 * @i_port_id: Binary 128-bit port ID.
3578 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3581 unsigned len, count, leading_zero_bytes;
3585 if (strnicmp(p, "0x", 2) == 0)
3591 count = min(len / 2, 16U);
3592 leading_zero_bytes = 16 - count;
3593 memset(i_port_id, 0, leading_zero_bytes);
3594 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3596 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3603 * configfs callback function invoked for
3604 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3606 static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
3607 struct config_group *group,
3610 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3611 struct se_node_acl *se_nacl, *se_nacl_new;
3612 struct srpt_node_acl *nacl;
3614 u32 nexus_depth = 1;
3617 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
3618 printk(KERN_ERR "invalid initiator port ID %s\n", name);
3623 se_nacl_new = srpt_alloc_fabric_acl(tpg);
3629 * nacl_new may be released by core_tpg_add_initiator_node_acl()
3630 * when converting a node ACL from demo mode to explict
3632 se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
3634 if (IS_ERR(se_nacl)) {
3635 ret = PTR_ERR(se_nacl);
3638 /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
3639 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3640 memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3641 nacl->sport = sport;
3643 spin_lock_irq(&sport->port_acl_lock);
3644 list_add_tail(&nacl->list, &sport->port_acl_list);
3645 spin_unlock_irq(&sport->port_acl_lock);
3649 return ERR_PTR(ret);
3653 * configfs callback function invoked for
3654 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3656 static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
3658 struct srpt_node_acl *nacl;
3659 struct srpt_device *sdev;
3660 struct srpt_port *sport;
3662 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3663 sport = nacl->sport;
3665 spin_lock_irq(&sport->port_acl_lock);
3666 list_del(&nacl->list);
3667 spin_unlock_irq(&sport->port_acl_lock);
3668 core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
3669 srpt_release_fabric_acl(NULL, se_nacl);
3672 static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
3673 struct se_portal_group *se_tpg,
3676 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3678 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3681 static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
3682 struct se_portal_group *se_tpg,
3686 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3690 ret = strict_strtoul(page, 0, &val);
3692 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3695 if (val > MAX_SRPT_RDMA_SIZE) {
3696 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3697 MAX_SRPT_RDMA_SIZE);
3700 if (val < DEFAULT_MAX_RDMA_SIZE) {
3701 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3702 val, DEFAULT_MAX_RDMA_SIZE);
3705 sport->port_attrib.srp_max_rdma_size = val;
3710 TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
3712 static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
3713 struct se_portal_group *se_tpg,
3716 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3718 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3721 static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
3722 struct se_portal_group *se_tpg,
3726 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3730 ret = strict_strtoul(page, 0, &val);
3732 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3735 if (val > MAX_SRPT_RSP_SIZE) {
3736 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3740 if (val < MIN_MAX_RSP_SIZE) {
3741 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3745 sport->port_attrib.srp_max_rsp_size = val;
3750 TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
3752 static ssize_t srpt_tpg_attrib_show_srp_sq_size(
3753 struct se_portal_group *se_tpg,
3756 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3758 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3761 static ssize_t srpt_tpg_attrib_store_srp_sq_size(
3762 struct se_portal_group *se_tpg,
3766 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3770 ret = strict_strtoul(page, 0, &val);
3772 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3775 if (val > MAX_SRPT_SRQ_SIZE) {
3776 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3780 if (val < MIN_SRPT_SRQ_SIZE) {
3781 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3785 sport->port_attrib.srp_sq_size = val;
3790 TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
3792 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3793 &srpt_tpg_attrib_srp_max_rdma_size.attr,
3794 &srpt_tpg_attrib_srp_max_rsp_size.attr,
3795 &srpt_tpg_attrib_srp_sq_size.attr,
3799 static ssize_t srpt_tpg_show_enable(
3800 struct se_portal_group *se_tpg,
3803 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3805 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3808 static ssize_t srpt_tpg_store_enable(
3809 struct se_portal_group *se_tpg,
3813 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3817 ret = strict_strtoul(page, 0, &tmp);
3819 printk(KERN_ERR "Unable to extract srpt_tpg_store_enable\n");
3823 if ((tmp != 0) && (tmp != 1)) {
3824 printk(KERN_ERR "Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3828 sport->enabled = true;
3830 sport->enabled = false;
3835 TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
3837 static struct configfs_attribute *srpt_tpg_attrs[] = {
3838 &srpt_tpg_enable.attr,
3843 * configfs callback invoked for
3844 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3846 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3847 struct config_group *group,
3850 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3853 /* Initialize sport->port_wwn and sport->port_tpg_1 */
3854 res = core_tpg_register(&srpt_target->tf_ops, &sport->port_wwn,
3855 &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
3857 return ERR_PTR(res);
3859 return &sport->port_tpg_1;
3863 * configfs callback invoked for
3864 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3866 static void srpt_drop_tpg(struct se_portal_group *tpg)
3868 struct srpt_port *sport = container_of(tpg,
3869 struct srpt_port, port_tpg_1);
3871 sport->enabled = false;
3872 core_tpg_deregister(&sport->port_tpg_1);
3876 * configfs callback invoked for
3877 * mkdir /sys/kernel/config/target/$driver/$port
3879 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3880 struct config_group *group,
3883 struct srpt_port *sport;
3886 sport = srpt_lookup_port(name);
3887 pr_debug("make_tport(%s)\n", name);
3892 return &sport->port_wwn;
3895 return ERR_PTR(ret);
3899 * configfs callback invoked for
3900 * rmdir /sys/kernel/config/target/$driver/$port
3902 static void srpt_drop_tport(struct se_wwn *wwn)
3904 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3906 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3909 static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
3912 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3915 TF_WWN_ATTR_RO(srpt, version);
3917 static struct configfs_attribute *srpt_wwn_attrs[] = {
3918 &srpt_wwn_version.attr,
3922 static struct target_core_fabric_ops srpt_template = {
3923 .get_fabric_name = srpt_get_fabric_name,
3924 .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
3925 .tpg_get_wwn = srpt_get_fabric_wwn,
3926 .tpg_get_tag = srpt_get_tag,
3927 .tpg_get_default_depth = srpt_get_default_depth,
3928 .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
3929 .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
3930 .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
3931 .tpg_check_demo_mode = srpt_check_false,
3932 .tpg_check_demo_mode_cache = srpt_check_true,
3933 .tpg_check_demo_mode_write_protect = srpt_check_true,
3934 .tpg_check_prod_mode_write_protect = srpt_check_false,
3935 .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
3936 .tpg_release_fabric_acl = srpt_release_fabric_acl,
3937 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3938 .release_cmd = srpt_release_cmd,
3939 .check_stop_free = srpt_check_stop_free,
3940 .shutdown_session = srpt_shutdown_session,
3941 .close_session = srpt_close_session,
3942 .sess_get_index = srpt_sess_get_index,
3943 .sess_get_initiator_sid = NULL,
3944 .write_pending = srpt_write_pending,
3945 .write_pending_status = srpt_write_pending_status,
3946 .set_default_node_attributes = srpt_set_default_node_attrs,
3947 .get_task_tag = srpt_get_task_tag,
3948 .get_cmd_state = srpt_get_tcm_cmd_state,
3949 .queue_data_in = srpt_queue_response,
3950 .queue_status = srpt_queue_status,
3951 .queue_tm_rsp = srpt_queue_response,
3952 .get_fabric_sense_len = srpt_get_fabric_sense_len,
3953 .set_fabric_sense_len = srpt_set_fabric_sense_len,
3955 * Setup function pointers for generic logic in
3956 * target_core_fabric_configfs.c
3958 .fabric_make_wwn = srpt_make_tport,
3959 .fabric_drop_wwn = srpt_drop_tport,
3960 .fabric_make_tpg = srpt_make_tpg,
3961 .fabric_drop_tpg = srpt_drop_tpg,
3962 .fabric_post_link = NULL,
3963 .fabric_pre_unlink = NULL,
3964 .fabric_make_np = NULL,
3965 .fabric_drop_np = NULL,
3966 .fabric_make_nodeacl = srpt_make_nodeacl,
3967 .fabric_drop_nodeacl = srpt_drop_nodeacl,
3971 * srpt_init_module() - Kernel module initialization.
3973 * Note: Since ib_register_client() registers callback functions, and since at
3974 * least one of these callback functions (srpt_add_one()) calls target core
3975 * functions, this driver must be registered with the target core before
3976 * ib_register_client() is called.
3978 static int __init srpt_init_module(void)
3983 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3984 printk(KERN_ERR "invalid value %d for kernel module parameter"
3985 " srp_max_req_size -- must be at least %d.\n",
3986 srp_max_req_size, MIN_MAX_REQ_SIZE);
3990 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3991 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3992 printk(KERN_ERR "invalid value %d for kernel module parameter"
3993 " srpt_srq_size -- must be in the range [%d..%d].\n",
3994 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3998 srpt_target = target_fabric_configfs_init(THIS_MODULE, "srpt");
3999 if (IS_ERR(srpt_target)) {
4000 printk(KERN_ERR "couldn't register\n");
4001 ret = PTR_ERR(srpt_target);
4005 srpt_target->tf_ops = srpt_template;
4007 /* Enable SG chaining */
4008 srpt_target->tf_ops.task_sg_chaining = true;
4011 * Set up default attribute lists.
4013 srpt_target->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = srpt_wwn_attrs;
4014 srpt_target->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = srpt_tpg_attrs;
4015 srpt_target->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = srpt_tpg_attrib_attrs;
4016 srpt_target->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
4017 srpt_target->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
4018 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
4019 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
4020 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
4021 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
4023 ret = target_fabric_configfs_register(srpt_target);
4025 printk(KERN_ERR "couldn't register\n");
4026 goto out_free_target;
4029 ret = ib_register_client(&srpt_client);
4031 printk(KERN_ERR "couldn't register IB client\n");
4032 goto out_unregister_target;
4037 out_unregister_target:
4038 target_fabric_configfs_deregister(srpt_target);
4042 target_fabric_configfs_free(srpt_target);
4047 static void __exit srpt_cleanup_module(void)
4049 ib_unregister_client(&srpt_client);
4050 target_fabric_configfs_deregister(srpt_target);
4054 module_init(srpt_init_module);
4055 module_exit(srpt_cleanup_module);