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 case IB_EVENT_GID_CHANGE:
202 /* Refresh port data asynchronously. */
203 if (event->element.port_num <= sdev->device->phys_port_cnt) {
204 sport = &sdev->port[event->element.port_num - 1];
205 if (!sport->lid && !sport->sm_lid)
206 schedule_work(&sport->work);
210 printk(KERN_ERR "received unrecognized IB event %d\n",
217 * srpt_srq_event() - SRQ event callback function.
219 static void srpt_srq_event(struct ib_event *event, void *ctx)
221 printk(KERN_INFO "SRQ event %d\n", event->event);
225 * srpt_qp_event() - QP event callback function.
227 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
229 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
230 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
232 switch (event->event) {
233 case IB_EVENT_COMM_EST:
234 ib_cm_notify(ch->cm_id, event->event);
236 case IB_EVENT_QP_LAST_WQE_REACHED:
237 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
239 srpt_release_channel(ch);
241 pr_debug("%s: state %d - ignored LAST_WQE.\n",
242 ch->sess_name, srpt_get_ch_state(ch));
245 printk(KERN_ERR "received unrecognized IB QP event %d\n",
252 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
254 * @slot: one-based slot number.
255 * @value: four-bit value.
257 * Copies the lowest four bits of value in element slot of the array of four
258 * bit elements called c_list (controller list). The index slot is one-based.
260 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
267 tmp = c_list[id] & 0xf;
268 c_list[id] = (value << 4) | tmp;
270 tmp = c_list[id] & 0xf0;
271 c_list[id] = (value & 0xf) | tmp;
276 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
278 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
281 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
283 struct ib_class_port_info *cif;
285 cif = (struct ib_class_port_info *)mad->data;
286 memset(cif, 0, sizeof *cif);
287 cif->base_version = 1;
288 cif->class_version = 1;
289 cif->resp_time_value = 20;
291 mad->mad_hdr.status = 0;
295 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
297 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
298 * Specification. See also section B.7, table B.6 in the SRP r16a document.
300 static void srpt_get_iou(struct ib_dm_mad *mad)
302 struct ib_dm_iou_info *ioui;
306 ioui = (struct ib_dm_iou_info *)mad->data;
307 ioui->change_id = __constant_cpu_to_be16(1);
308 ioui->max_controllers = 16;
310 /* set present for slot 1 and empty for the rest */
311 srpt_set_ioc(ioui->controller_list, 1, 1);
312 for (i = 1, slot = 2; i < 16; i++, slot++)
313 srpt_set_ioc(ioui->controller_list, slot, 0);
315 mad->mad_hdr.status = 0;
319 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
321 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
322 * Architecture Specification. See also section B.7, table B.7 in the SRP
325 static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
326 struct ib_dm_mad *mad)
328 struct srpt_device *sdev = sport->sdev;
329 struct ib_dm_ioc_profile *iocp;
331 iocp = (struct ib_dm_ioc_profile *)mad->data;
333 if (!slot || slot > 16) {
335 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
341 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
345 memset(iocp, 0, sizeof *iocp);
346 strcpy(iocp->id_string, SRPT_ID_STRING);
347 iocp->guid = cpu_to_be64(srpt_service_guid);
348 iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
349 iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
350 iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
351 iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
352 iocp->subsys_device_id = 0x0;
353 iocp->io_class = __constant_cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
354 iocp->io_subclass = __constant_cpu_to_be16(SRP_IO_SUBCLASS);
355 iocp->protocol = __constant_cpu_to_be16(SRP_PROTOCOL);
356 iocp->protocol_version = __constant_cpu_to_be16(SRP_PROTOCOL_VERSION);
357 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
358 iocp->rdma_read_depth = 4;
359 iocp->send_size = cpu_to_be32(srp_max_req_size);
360 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
362 iocp->num_svc_entries = 1;
363 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
364 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
366 mad->mad_hdr.status = 0;
370 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
372 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
373 * Specification. See also section B.7, table B.8 in the SRP r16a document.
375 static void srpt_get_svc_entries(u64 ioc_guid,
376 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
378 struct ib_dm_svc_entries *svc_entries;
382 if (!slot || slot > 16) {
384 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
388 if (slot > 2 || lo > hi || hi > 1) {
390 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
394 svc_entries = (struct ib_dm_svc_entries *)mad->data;
395 memset(svc_entries, 0, sizeof *svc_entries);
396 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
397 snprintf(svc_entries->service_entries[0].name,
398 sizeof(svc_entries->service_entries[0].name),
400 SRP_SERVICE_NAME_PREFIX,
403 mad->mad_hdr.status = 0;
407 * srpt_mgmt_method_get() - Process a received management datagram.
408 * @sp: source port through which the MAD has been received.
409 * @rq_mad: received MAD.
410 * @rsp_mad: response MAD.
412 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
413 struct ib_dm_mad *rsp_mad)
419 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
421 case DM_ATTR_CLASS_PORT_INFO:
422 srpt_get_class_port_info(rsp_mad);
424 case DM_ATTR_IOU_INFO:
425 srpt_get_iou(rsp_mad);
427 case DM_ATTR_IOC_PROFILE:
428 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
429 srpt_get_ioc(sp, slot, rsp_mad);
431 case DM_ATTR_SVC_ENTRIES:
432 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
433 hi = (u8) ((slot >> 8) & 0xff);
434 lo = (u8) (slot & 0xff);
435 slot = (u16) ((slot >> 16) & 0xffff);
436 srpt_get_svc_entries(srpt_service_guid,
437 slot, hi, lo, rsp_mad);
440 rsp_mad->mad_hdr.status =
441 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
447 * srpt_mad_send_handler() - Post MAD-send callback function.
449 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
450 struct ib_mad_send_wc *mad_wc)
452 ib_destroy_ah(mad_wc->send_buf->ah);
453 ib_free_send_mad(mad_wc->send_buf);
457 * srpt_mad_recv_handler() - MAD reception callback function.
459 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
460 struct ib_mad_recv_wc *mad_wc)
462 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
464 struct ib_mad_send_buf *rsp;
465 struct ib_dm_mad *dm_mad;
467 if (!mad_wc || !mad_wc->recv_buf.mad)
470 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
471 mad_wc->recv_buf.grh, mad_agent->port_num);
475 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
477 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
478 mad_wc->wc->pkey_index, 0,
479 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
487 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
488 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
489 dm_mad->mad_hdr.status = 0;
491 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
492 case IB_MGMT_METHOD_GET:
493 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
495 case IB_MGMT_METHOD_SET:
496 dm_mad->mad_hdr.status =
497 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
500 dm_mad->mad_hdr.status =
501 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
505 if (!ib_post_send_mad(rsp, NULL)) {
506 ib_free_recv_mad(mad_wc);
507 /* will destroy_ah & free_send_mad in send completion */
511 ib_free_send_mad(rsp);
516 ib_free_recv_mad(mad_wc);
520 * srpt_refresh_port() - Configure a HCA port.
522 * Enable InfiniBand management datagram processing, update the cached sm_lid,
523 * lid and gid values, and register a callback function for processing MADs
524 * on the specified port.
526 * Note: It is safe to call this function more than once for the same port.
528 static int srpt_refresh_port(struct srpt_port *sport)
530 struct ib_mad_reg_req reg_req;
531 struct ib_port_modify port_modify;
532 struct ib_port_attr port_attr;
535 memset(&port_modify, 0, sizeof port_modify);
536 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
537 port_modify.clr_port_cap_mask = 0;
539 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
543 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
547 sport->sm_lid = port_attr.sm_lid;
548 sport->lid = port_attr.lid;
550 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
554 if (!sport->mad_agent) {
555 memset(®_req, 0, sizeof reg_req);
556 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
557 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
558 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
559 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
561 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
565 srpt_mad_send_handler,
566 srpt_mad_recv_handler,
568 if (IS_ERR(sport->mad_agent)) {
569 ret = PTR_ERR(sport->mad_agent);
570 sport->mad_agent = NULL;
579 port_modify.set_port_cap_mask = 0;
580 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
581 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
589 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
591 * Note: It is safe to call this function more than once for the same device.
593 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
595 struct ib_port_modify port_modify = {
596 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
598 struct srpt_port *sport;
601 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
602 sport = &sdev->port[i - 1];
603 WARN_ON(sport->port != i);
604 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
605 printk(KERN_ERR "disabling MAD processing failed.\n");
606 if (sport->mad_agent) {
607 ib_unregister_mad_agent(sport->mad_agent);
608 sport->mad_agent = NULL;
614 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
616 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
617 int ioctx_size, int dma_size,
618 enum dma_data_direction dir)
620 struct srpt_ioctx *ioctx;
622 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
626 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
630 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
631 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
645 * srpt_free_ioctx() - Free an SRPT I/O context structure.
647 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
648 int dma_size, enum dma_data_direction dir)
653 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
659 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
660 * @sdev: Device to allocate the I/O context ring for.
661 * @ring_size: Number of elements in the I/O context ring.
662 * @ioctx_size: I/O context size.
663 * @dma_size: DMA buffer size.
664 * @dir: DMA data direction.
666 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
667 int ring_size, int ioctx_size,
668 int dma_size, enum dma_data_direction dir)
670 struct srpt_ioctx **ring;
673 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
674 && ioctx_size != sizeof(struct srpt_send_ioctx));
676 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
679 for (i = 0; i < ring_size; ++i) {
680 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
689 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
697 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
699 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
700 struct srpt_device *sdev, int ring_size,
701 int dma_size, enum dma_data_direction dir)
705 for (i = 0; i < ring_size; ++i)
706 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
711 * srpt_get_cmd_state() - Get the state of a SCSI command.
713 static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
715 enum srpt_command_state state;
720 spin_lock_irqsave(&ioctx->spinlock, flags);
721 state = ioctx->state;
722 spin_unlock_irqrestore(&ioctx->spinlock, flags);
727 * srpt_set_cmd_state() - Set the state of a SCSI command.
729 * Does not modify the state of aborted commands. Returns the previous command
732 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
733 enum srpt_command_state new)
735 enum srpt_command_state previous;
740 spin_lock_irqsave(&ioctx->spinlock, flags);
741 previous = ioctx->state;
742 if (previous != SRPT_STATE_DONE)
744 spin_unlock_irqrestore(&ioctx->spinlock, flags);
750 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
752 * Returns true if and only if the previous command state was equal to 'old'.
754 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
755 enum srpt_command_state old,
756 enum srpt_command_state new)
758 enum srpt_command_state previous;
762 WARN_ON(old == SRPT_STATE_DONE);
763 WARN_ON(new == SRPT_STATE_NEW);
765 spin_lock_irqsave(&ioctx->spinlock, flags);
766 previous = ioctx->state;
769 spin_unlock_irqrestore(&ioctx->spinlock, flags);
770 return previous == old;
774 * srpt_post_recv() - Post an IB receive request.
776 static int srpt_post_recv(struct srpt_device *sdev,
777 struct srpt_recv_ioctx *ioctx)
780 struct ib_recv_wr wr, *bad_wr;
783 wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
785 list.addr = ioctx->ioctx.dma;
786 list.length = srp_max_req_size;
787 list.lkey = sdev->mr->lkey;
793 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
797 * srpt_post_send() - Post an IB send request.
799 * Returns zero upon success and a non-zero value upon failure.
801 static int srpt_post_send(struct srpt_rdma_ch *ch,
802 struct srpt_send_ioctx *ioctx, int len)
805 struct ib_send_wr wr, *bad_wr;
806 struct srpt_device *sdev = ch->sport->sdev;
809 atomic_inc(&ch->req_lim);
812 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
813 printk(KERN_WARNING "IB send queue full (needed 1)\n");
817 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
820 list.addr = ioctx->ioctx.dma;
822 list.lkey = sdev->mr->lkey;
825 wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
828 wr.opcode = IB_WR_SEND;
829 wr.send_flags = IB_SEND_SIGNALED;
831 ret = ib_post_send(ch->qp, &wr, &bad_wr);
835 atomic_inc(&ch->sq_wr_avail);
836 atomic_dec(&ch->req_lim);
842 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
843 * @ioctx: Pointer to the I/O context associated with the request.
844 * @srp_cmd: Pointer to the SRP_CMD request data.
845 * @dir: Pointer to the variable to which the transfer direction will be
847 * @data_len: Pointer to the variable to which the total data length of all
848 * descriptors in the SRP_CMD request will be written.
850 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
852 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
853 * -ENOMEM when memory allocation fails and zero upon success.
855 static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
856 struct srp_cmd *srp_cmd,
857 enum dma_data_direction *dir, u64 *data_len)
859 struct srp_indirect_buf *idb;
860 struct srp_direct_buf *db;
861 unsigned add_cdb_offset;
865 * The pointer computations below will only be compiled correctly
866 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
867 * whether srp_cmd::add_data has been declared as a byte pointer.
869 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
870 && !__same_type(srp_cmd->add_data[0], (u8)0));
879 * The lower four bits of the buffer format field contain the DATA-IN
880 * buffer descriptor format, and the highest four bits contain the
881 * DATA-OUT buffer descriptor format.
884 if (srp_cmd->buf_fmt & 0xf)
885 /* DATA-IN: transfer data from target to initiator (read). */
886 *dir = DMA_FROM_DEVICE;
887 else if (srp_cmd->buf_fmt >> 4)
888 /* DATA-OUT: transfer data from initiator to target (write). */
889 *dir = DMA_TO_DEVICE;
892 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
893 * CDB LENGTH' field are reserved and the size in bytes of this field
894 * is four times the value specified in bits 3..7. Hence the "& ~3".
896 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
897 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
898 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
900 ioctx->rbufs = &ioctx->single_rbuf;
902 db = (struct srp_direct_buf *)(srp_cmd->add_data
904 memcpy(ioctx->rbufs, db, sizeof *db);
905 *data_len = be32_to_cpu(db->len);
906 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
907 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
908 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
911 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
914 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
915 printk(KERN_ERR "received unsupported SRP_CMD request"
916 " type (%u out + %u in != %u / %zu)\n",
917 srp_cmd->data_out_desc_cnt,
918 srp_cmd->data_in_desc_cnt,
919 be32_to_cpu(idb->table_desc.len),
926 if (ioctx->n_rbuf == 1)
927 ioctx->rbufs = &ioctx->single_rbuf;
930 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
939 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
940 *data_len = be32_to_cpu(idb->len);
947 * srpt_init_ch_qp() - Initialize queue pair attributes.
949 * Initialized the attributes of queue pair 'qp' by allowing local write,
950 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
952 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
954 struct ib_qp_attr *attr;
957 attr = kzalloc(sizeof *attr, GFP_KERNEL);
961 attr->qp_state = IB_QPS_INIT;
962 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
963 IB_ACCESS_REMOTE_WRITE;
964 attr->port_num = ch->sport->port;
965 attr->pkey_index = 0;
967 ret = ib_modify_qp(qp, attr,
968 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
976 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
977 * @ch: channel of the queue pair.
978 * @qp: queue pair to change the state of.
980 * Returns zero upon success and a negative value upon failure.
982 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
983 * If this structure ever becomes larger, it might be necessary to allocate
984 * it dynamically instead of on the stack.
986 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
988 struct ib_qp_attr qp_attr;
992 qp_attr.qp_state = IB_QPS_RTR;
993 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
997 qp_attr.max_dest_rd_atomic = 4;
999 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1006 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1007 * @ch: channel of the queue pair.
1008 * @qp: queue pair to change the state of.
1010 * Returns zero upon success and a negative value upon failure.
1012 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1013 * If this structure ever becomes larger, it might be necessary to allocate
1014 * it dynamically instead of on the stack.
1016 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1018 struct ib_qp_attr qp_attr;
1022 qp_attr.qp_state = IB_QPS_RTS;
1023 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1027 qp_attr.max_rd_atomic = 4;
1029 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1036 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1038 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1040 struct ib_qp_attr qp_attr;
1042 qp_attr.qp_state = IB_QPS_ERR;
1043 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1047 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1049 static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1050 struct srpt_send_ioctx *ioctx)
1052 struct scatterlist *sg;
1053 enum dma_data_direction dir;
1057 BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
1059 while (ioctx->n_rdma)
1060 kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
1062 kfree(ioctx->rdma_ius);
1063 ioctx->rdma_ius = NULL;
1065 if (ioctx->mapped_sg_count) {
1068 dir = ioctx->cmd.data_direction;
1069 BUG_ON(dir == DMA_NONE);
1070 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1071 opposite_dma_dir(dir));
1072 ioctx->mapped_sg_count = 0;
1077 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1079 static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1080 struct srpt_send_ioctx *ioctx)
1082 struct ib_device *dev = ch->sport->sdev->device;
1084 struct scatterlist *sg, *sg_orig;
1086 enum dma_data_direction dir;
1087 struct rdma_iu *riu;
1088 struct srp_direct_buf *db;
1089 dma_addr_t dma_addr;
1101 dir = cmd->data_direction;
1102 BUG_ON(dir == DMA_NONE);
1104 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1105 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
1107 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1108 opposite_dma_dir(dir));
1109 if (unlikely(!count))
1112 ioctx->mapped_sg_count = count;
1114 if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1115 nrdma = ioctx->n_rdma_ius;
1117 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1120 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
1121 if (!ioctx->rdma_ius)
1124 ioctx->n_rdma_ius = nrdma;
1128 tsize = cmd->data_length;
1129 dma_len = ib_sg_dma_len(dev, &sg[0]);
1130 riu = ioctx->rdma_ius;
1133 * For each remote desc - calculate the #ib_sge.
1134 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1135 * each remote desc rdma_iu is required a rdma wr;
1137 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1141 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1142 rsize = be32_to_cpu(db->len);
1143 raddr = be64_to_cpu(db->va);
1145 riu->rkey = be32_to_cpu(db->key);
1148 /* calculate how many sge required for this remote_buf */
1149 while (rsize > 0 && tsize > 0) {
1151 if (rsize >= dma_len) {
1160 dma_len = ib_sg_dma_len(
1172 if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1175 kmalloc(riu->sge_cnt * sizeof *riu->sge,
1183 riu->rkey = be32_to_cpu(db->key);
1188 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1195 tsize = cmd->data_length;
1196 riu = ioctx->rdma_ius;
1198 dma_len = ib_sg_dma_len(dev, &sg[0]);
1199 dma_addr = ib_sg_dma_address(dev, &sg[0]);
1201 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1203 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1204 rsize = be32_to_cpu(db->len);
1208 while (rsize > 0 && tsize > 0) {
1209 sge->addr = dma_addr;
1210 sge->lkey = ch->sport->sdev->mr->lkey;
1212 if (rsize >= dma_len) {
1214 (tsize < dma_len) ? tsize : dma_len;
1222 dma_len = ib_sg_dma_len(
1224 dma_addr = ib_sg_dma_address(
1229 sge->length = (tsize < rsize) ? tsize : rsize;
1237 if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1241 } else if (rsize > 0 && tsize > 0)
1249 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1255 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1257 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1259 struct srpt_send_ioctx *ioctx;
1260 unsigned long flags;
1265 spin_lock_irqsave(&ch->spinlock, flags);
1266 if (!list_empty(&ch->free_list)) {
1267 ioctx = list_first_entry(&ch->free_list,
1268 struct srpt_send_ioctx, free_list);
1269 list_del(&ioctx->free_list);
1271 spin_unlock_irqrestore(&ch->spinlock, flags);
1276 BUG_ON(ioctx->ch != ch);
1277 spin_lock_init(&ioctx->spinlock);
1278 ioctx->state = SRPT_STATE_NEW;
1280 ioctx->rbufs = NULL;
1282 ioctx->n_rdma_ius = 0;
1283 ioctx->rdma_ius = NULL;
1284 ioctx->mapped_sg_count = 0;
1285 init_completion(&ioctx->tx_done);
1286 ioctx->queue_status_only = false;
1288 * transport_init_se_cmd() does not initialize all fields, so do it
1291 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1292 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1298 * srpt_abort_cmd() - Abort a SCSI command.
1299 * @ioctx: I/O context associated with the SCSI command.
1300 * @context: Preferred execution context.
1302 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1304 enum srpt_command_state state;
1305 unsigned long flags;
1310 * If the command is in a state where the target core is waiting for
1311 * the ib_srpt driver, change the state to the next state. Changing
1312 * the state of the command from SRPT_STATE_NEED_DATA to
1313 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1314 * function a second time.
1317 spin_lock_irqsave(&ioctx->spinlock, flags);
1318 state = ioctx->state;
1320 case SRPT_STATE_NEED_DATA:
1321 ioctx->state = SRPT_STATE_DATA_IN;
1323 case SRPT_STATE_DATA_IN:
1324 case SRPT_STATE_CMD_RSP_SENT:
1325 case SRPT_STATE_MGMT_RSP_SENT:
1326 ioctx->state = SRPT_STATE_DONE;
1331 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1333 if (state == SRPT_STATE_DONE) {
1334 struct srpt_rdma_ch *ch = ioctx->ch;
1336 BUG_ON(ch->sess == NULL);
1338 target_put_sess_cmd(ch->sess, &ioctx->cmd);
1342 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1346 case SRPT_STATE_NEW:
1347 case SRPT_STATE_DATA_IN:
1348 case SRPT_STATE_MGMT:
1350 * Do nothing - defer abort processing until
1351 * srpt_queue_response() is invoked.
1353 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1355 case SRPT_STATE_NEED_DATA:
1356 /* DMA_TO_DEVICE (write) - RDMA read error. */
1358 /* XXX(hch): this is a horrible layering violation.. */
1359 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1360 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
1361 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
1363 case SRPT_STATE_CMD_RSP_SENT:
1365 * SRP_RSP sending failed or the SRP_RSP send completion has
1366 * not been received in time.
1368 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
1369 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
1371 case SRPT_STATE_MGMT_RSP_SENT:
1372 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1373 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
1376 WARN(1, "Unexpected command state (%d)", state);
1385 * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1387 static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1389 struct srpt_send_ioctx *ioctx;
1390 enum srpt_command_state state;
1394 atomic_inc(&ch->sq_wr_avail);
1396 index = idx_from_wr_id(wr_id);
1397 ioctx = ch->ioctx_ring[index];
1398 state = srpt_get_cmd_state(ioctx);
1401 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1402 && state != SRPT_STATE_MGMT_RSP_SENT
1403 && state != SRPT_STATE_NEED_DATA
1404 && state != SRPT_STATE_DONE);
1406 /* If SRP_RSP sending failed, undo the ch->req_lim change. */
1407 if (state == SRPT_STATE_CMD_RSP_SENT
1408 || state == SRPT_STATE_MGMT_RSP_SENT)
1409 atomic_dec(&ch->req_lim);
1411 srpt_abort_cmd(ioctx);
1415 * srpt_handle_send_comp() - Process an IB send completion notification.
1417 static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1418 struct srpt_send_ioctx *ioctx)
1420 enum srpt_command_state state;
1422 atomic_inc(&ch->sq_wr_avail);
1424 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1426 if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1427 && state != SRPT_STATE_MGMT_RSP_SENT
1428 && state != SRPT_STATE_DONE))
1429 pr_debug("state = %d\n", state);
1431 if (state != SRPT_STATE_DONE) {
1432 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1433 transport_generic_free_cmd(&ioctx->cmd, 0);
1435 printk(KERN_ERR "IB completion has been received too late for"
1436 " wr_id = %u.\n", ioctx->ioctx.index);
1441 * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1443 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1444 * the data that has been transferred via IB RDMA had to be postponed until the
1445 * check_stop_free() callback. None of this is necessary anymore and needs to
1448 static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1449 struct srpt_send_ioctx *ioctx,
1450 enum srpt_opcode opcode)
1452 WARN_ON(ioctx->n_rdma <= 0);
1453 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1455 if (opcode == SRPT_RDMA_READ_LAST) {
1456 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1457 SRPT_STATE_DATA_IN))
1458 target_execute_cmd(&ioctx->cmd);
1460 printk(KERN_ERR "%s[%d]: wrong state = %d\n", __func__,
1461 __LINE__, srpt_get_cmd_state(ioctx));
1462 } else if (opcode == SRPT_RDMA_ABORT) {
1463 ioctx->rdma_aborted = true;
1465 WARN(true, "unexpected opcode %d\n", opcode);
1470 * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1472 static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1473 struct srpt_send_ioctx *ioctx,
1474 enum srpt_opcode opcode)
1477 enum srpt_command_state state;
1480 state = srpt_get_cmd_state(ioctx);
1482 case SRPT_RDMA_READ_LAST:
1483 if (ioctx->n_rdma <= 0) {
1484 printk(KERN_ERR "Received invalid RDMA read"
1485 " error completion with idx %d\n",
1486 ioctx->ioctx.index);
1489 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1490 if (state == SRPT_STATE_NEED_DATA)
1491 srpt_abort_cmd(ioctx);
1493 printk(KERN_ERR "%s[%d]: wrong state = %d\n",
1494 __func__, __LINE__, state);
1496 case SRPT_RDMA_WRITE_LAST:
1499 printk(KERN_ERR "%s[%d]: opcode = %u\n", __func__,
1506 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1507 * @ch: RDMA channel through which the request has been received.
1508 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1509 * be built in the buffer ioctx->buf points at and hence this function will
1510 * overwrite the request data.
1511 * @tag: tag of the request for which this response is being generated.
1512 * @status: value for the STATUS field of the SRP_RSP information unit.
1514 * Returns the size in bytes of the SRP_RSP response.
1516 * An SRP_RSP response contains a SCSI status or service response. See also
1517 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1518 * response. See also SPC-2 for more information about sense data.
1520 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1521 struct srpt_send_ioctx *ioctx, u64 tag,
1524 struct srp_rsp *srp_rsp;
1525 const u8 *sense_data;
1526 int sense_data_len, max_sense_len;
1529 * The lowest bit of all SAM-3 status codes is zero (see also
1530 * paragraph 5.3 in SAM-3).
1532 WARN_ON(status & 1);
1534 srp_rsp = ioctx->ioctx.buf;
1537 sense_data = ioctx->sense_data;
1538 sense_data_len = ioctx->cmd.scsi_sense_length;
1539 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1541 memset(srp_rsp, 0, sizeof *srp_rsp);
1542 srp_rsp->opcode = SRP_RSP;
1543 srp_rsp->req_lim_delta =
1544 __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1546 srp_rsp->status = status;
1548 if (sense_data_len) {
1549 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1550 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1551 if (sense_data_len > max_sense_len) {
1552 printk(KERN_WARNING "truncated sense data from %d to %d"
1553 " bytes\n", sense_data_len, max_sense_len);
1554 sense_data_len = max_sense_len;
1557 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1558 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1559 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1562 return sizeof(*srp_rsp) + sense_data_len;
1566 * srpt_build_tskmgmt_rsp() - Build a task management response.
1567 * @ch: RDMA channel through which the request has been received.
1568 * @ioctx: I/O context in which the SRP_RSP response will be built.
1569 * @rsp_code: RSP_CODE that will be stored in the response.
1570 * @tag: Tag of the request for which this response is being generated.
1572 * Returns the size in bytes of the SRP_RSP response.
1574 * An SRP_RSP response contains a SCSI status or service response. See also
1575 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1578 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1579 struct srpt_send_ioctx *ioctx,
1580 u8 rsp_code, u64 tag)
1582 struct srp_rsp *srp_rsp;
1587 resp_len = sizeof(*srp_rsp) + resp_data_len;
1589 srp_rsp = ioctx->ioctx.buf;
1591 memset(srp_rsp, 0, sizeof *srp_rsp);
1593 srp_rsp->opcode = SRP_RSP;
1594 srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
1595 + atomic_xchg(&ch->req_lim_delta, 0));
1598 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1599 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1600 srp_rsp->data[3] = rsp_code;
1605 #define NO_SUCH_LUN ((uint64_t)-1LL)
1608 * SCSI LUN addressing method. See also SAM-2 and the section about
1611 enum scsi_lun_addr_method {
1612 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1613 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1614 SCSI_LUN_ADDR_METHOD_LUN = 2,
1615 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1619 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1621 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1622 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1623 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1625 static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1627 uint64_t res = NO_SUCH_LUN;
1628 int addressing_method;
1630 if (unlikely(len < 2)) {
1631 printk(KERN_ERR "Illegal LUN length %d, expected 2 bytes or "
1638 if ((*((__be64 *)lun) &
1639 __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1643 if (*((__be16 *)&lun[2]) != 0)
1647 if (*((__be32 *)&lun[2]) != 0)
1656 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1657 switch (addressing_method) {
1658 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1659 case SCSI_LUN_ADDR_METHOD_FLAT:
1660 case SCSI_LUN_ADDR_METHOD_LUN:
1661 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1664 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1666 printk(KERN_ERR "Unimplemented LUN addressing method %u",
1675 printk(KERN_ERR "Support for multi-level LUNs has not yet been"
1680 static int srpt_check_stop_free(struct se_cmd *cmd)
1682 struct srpt_send_ioctx *ioctx = container_of(cmd,
1683 struct srpt_send_ioctx, cmd);
1685 return target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
1689 * srpt_handle_cmd() - Process SRP_CMD.
1691 static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1692 struct srpt_recv_ioctx *recv_ioctx,
1693 struct srpt_send_ioctx *send_ioctx)
1696 struct srp_cmd *srp_cmd;
1697 uint64_t unpacked_lun;
1699 enum dma_data_direction dir;
1703 BUG_ON(!send_ioctx);
1705 srp_cmd = recv_ioctx->ioctx.buf;
1706 cmd = &send_ioctx->cmd;
1707 send_ioctx->tag = srp_cmd->tag;
1709 switch (srp_cmd->task_attr) {
1710 case SRP_CMD_SIMPLE_Q:
1711 cmd->sam_task_attr = MSG_SIMPLE_TAG;
1713 case SRP_CMD_ORDERED_Q:
1715 cmd->sam_task_attr = MSG_ORDERED_TAG;
1717 case SRP_CMD_HEAD_OF_Q:
1718 cmd->sam_task_attr = MSG_HEAD_TAG;
1721 cmd->sam_task_attr = MSG_ACA_TAG;
1725 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
1726 printk(KERN_ERR "0x%llx: parsing SRP descriptor table failed.\n",
1728 ret = TCM_INVALID_CDB_FIELD;
1732 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1733 sizeof(srp_cmd->lun));
1734 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1735 &send_ioctx->sense_data[0], unpacked_lun, data_len,
1736 MSG_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
1738 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1744 transport_send_check_condition_and_sense(cmd, ret, 0);
1749 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1750 * @ch: RDMA channel of the task management request.
1751 * @fn: Task management function to perform.
1752 * @req_tag: Tag of the SRP task management request.
1753 * @mgmt_ioctx: I/O context of the task management request.
1755 * Returns zero if the target core will process the task management
1756 * request asynchronously.
1758 * Note: It is assumed that the initiator serializes tag-based task management
1761 static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1763 struct srpt_device *sdev;
1764 struct srpt_rdma_ch *ch;
1765 struct srpt_send_ioctx *target;
1772 sdev = ch->sport->sdev;
1774 spin_lock_irq(&sdev->spinlock);
1775 for (i = 0; i < ch->rq_size; ++i) {
1776 target = ch->ioctx_ring[i];
1777 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
1778 target->tag == tag &&
1779 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1781 /* now let the target core abort &target->cmd; */
1785 spin_unlock_irq(&sdev->spinlock);
1789 static int srp_tmr_to_tcm(int fn)
1792 case SRP_TSK_ABORT_TASK:
1793 return TMR_ABORT_TASK;
1794 case SRP_TSK_ABORT_TASK_SET:
1795 return TMR_ABORT_TASK_SET;
1796 case SRP_TSK_CLEAR_TASK_SET:
1797 return TMR_CLEAR_TASK_SET;
1798 case SRP_TSK_LUN_RESET:
1799 return TMR_LUN_RESET;
1800 case SRP_TSK_CLEAR_ACA:
1801 return TMR_CLEAR_ACA;
1808 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1810 * Returns 0 if and only if the request will be processed by the target core.
1812 * For more information about SRP_TSK_MGMT information units, see also section
1813 * 6.7 in the SRP r16a document.
1815 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1816 struct srpt_recv_ioctx *recv_ioctx,
1817 struct srpt_send_ioctx *send_ioctx)
1819 struct srp_tsk_mgmt *srp_tsk;
1821 struct se_session *sess = ch->sess;
1822 uint64_t unpacked_lun;
1827 BUG_ON(!send_ioctx);
1829 srp_tsk = recv_ioctx->ioctx.buf;
1830 cmd = &send_ioctx->cmd;
1832 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1833 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1834 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1836 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1837 send_ioctx->tag = srp_tsk->tag;
1838 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1840 send_ioctx->cmd.se_tmr_req->response =
1841 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
1844 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1845 sizeof(srp_tsk->lun));
1847 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK) {
1848 rc = srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1850 send_ioctx->cmd.se_tmr_req->response =
1851 TMR_TASK_DOES_NOT_EXIST;
1854 tag = srp_tsk->task_tag;
1856 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
1857 srp_tsk, tcm_tmr, GFP_KERNEL, tag,
1858 TARGET_SCF_ACK_KREF);
1860 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1865 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
1869 * srpt_handle_new_iu() - Process a newly received information unit.
1870 * @ch: RDMA channel through which the information unit has been received.
1871 * @ioctx: SRPT I/O context associated with the information unit.
1873 static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1874 struct srpt_recv_ioctx *recv_ioctx,
1875 struct srpt_send_ioctx *send_ioctx)
1877 struct srp_cmd *srp_cmd;
1878 enum rdma_ch_state ch_state;
1881 BUG_ON(!recv_ioctx);
1883 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1884 recv_ioctx->ioctx.dma, srp_max_req_size,
1887 ch_state = srpt_get_ch_state(ch);
1888 if (unlikely(ch_state == CH_CONNECTING)) {
1889 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1893 if (unlikely(ch_state != CH_LIVE))
1896 srp_cmd = recv_ioctx->ioctx.buf;
1897 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1899 send_ioctx = srpt_get_send_ioctx(ch);
1900 if (unlikely(!send_ioctx)) {
1901 list_add_tail(&recv_ioctx->wait_list,
1902 &ch->cmd_wait_list);
1907 switch (srp_cmd->opcode) {
1909 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1912 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1915 printk(KERN_ERR "Not yet implemented: SRP_I_LOGOUT\n");
1918 pr_debug("received SRP_CRED_RSP\n");
1921 pr_debug("received SRP_AER_RSP\n");
1924 printk(KERN_ERR "Received SRP_RSP\n");
1927 printk(KERN_ERR "received IU with unknown opcode 0x%x\n",
1932 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1937 static void srpt_process_rcv_completion(struct ib_cq *cq,
1938 struct srpt_rdma_ch *ch,
1941 struct srpt_device *sdev = ch->sport->sdev;
1942 struct srpt_recv_ioctx *ioctx;
1945 index = idx_from_wr_id(wc->wr_id);
1946 if (wc->status == IB_WC_SUCCESS) {
1949 req_lim = atomic_dec_return(&ch->req_lim);
1950 if (unlikely(req_lim < 0))
1951 printk(KERN_ERR "req_lim = %d < 0\n", req_lim);
1952 ioctx = sdev->ioctx_ring[index];
1953 srpt_handle_new_iu(ch, ioctx, NULL);
1955 printk(KERN_INFO "receiving failed for idx %u with status %d\n",
1961 * srpt_process_send_completion() - Process an IB send completion.
1963 * Note: Although this has not yet been observed during tests, at least in
1964 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1965 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1966 * value in each response is set to one, and it is possible that this response
1967 * makes the initiator send a new request before the send completion for that
1968 * response has been processed. This could e.g. happen if the call to
1969 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1970 * if IB retransmission causes generation of the send completion to be
1971 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1972 * are queued on cmd_wait_list. The code below processes these delayed
1973 * requests one at a time.
1975 static void srpt_process_send_completion(struct ib_cq *cq,
1976 struct srpt_rdma_ch *ch,
1979 struct srpt_send_ioctx *send_ioctx;
1981 enum srpt_opcode opcode;
1983 index = idx_from_wr_id(wc->wr_id);
1984 opcode = opcode_from_wr_id(wc->wr_id);
1985 send_ioctx = ch->ioctx_ring[index];
1986 if (wc->status == IB_WC_SUCCESS) {
1987 if (opcode == SRPT_SEND)
1988 srpt_handle_send_comp(ch, send_ioctx);
1990 WARN_ON(opcode != SRPT_RDMA_ABORT &&
1991 wc->opcode != IB_WC_RDMA_READ);
1992 srpt_handle_rdma_comp(ch, send_ioctx, opcode);
1995 if (opcode == SRPT_SEND) {
1996 printk(KERN_INFO "sending response for idx %u failed"
1997 " with status %d\n", index, wc->status);
1998 srpt_handle_send_err_comp(ch, wc->wr_id);
1999 } else if (opcode != SRPT_RDMA_MID) {
2000 printk(KERN_INFO "RDMA t %d for idx %u failed with"
2001 " status %d", opcode, index, wc->status);
2002 srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
2006 while (unlikely(opcode == SRPT_SEND
2007 && !list_empty(&ch->cmd_wait_list)
2008 && srpt_get_ch_state(ch) == CH_LIVE
2009 && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
2010 struct srpt_recv_ioctx *recv_ioctx;
2012 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
2013 struct srpt_recv_ioctx,
2015 list_del(&recv_ioctx->wait_list);
2016 srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
2020 static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
2022 struct ib_wc *const wc = ch->wc;
2025 WARN_ON(cq != ch->cq);
2027 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
2028 while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
2029 for (i = 0; i < n; i++) {
2030 if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
2031 srpt_process_rcv_completion(cq, ch, &wc[i]);
2033 srpt_process_send_completion(cq, ch, &wc[i]);
2039 * srpt_completion() - IB completion queue callback function.
2042 * - It is guaranteed that a completion handler will never be invoked
2043 * concurrently on two different CPUs for the same completion queue. See also
2044 * Documentation/infiniband/core_locking.txt and the implementation of
2045 * handle_edge_irq() in kernel/irq/chip.c.
2046 * - When threaded IRQs are enabled, completion handlers are invoked in thread
2047 * context instead of interrupt context.
2049 static void srpt_completion(struct ib_cq *cq, void *ctx)
2051 struct srpt_rdma_ch *ch = ctx;
2053 wake_up_interruptible(&ch->wait_queue);
2056 static int srpt_compl_thread(void *arg)
2058 struct srpt_rdma_ch *ch;
2060 /* Hibernation / freezing of the SRPT kernel thread is not supported. */
2061 current->flags |= PF_NOFREEZE;
2065 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) started\n",
2066 ch->sess_name, ch->thread->comm, current->pid);
2067 while (!kthread_should_stop()) {
2068 wait_event_interruptible(ch->wait_queue,
2069 (srpt_process_completion(ch->cq, ch),
2070 kthread_should_stop()));
2072 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) stopped\n",
2073 ch->sess_name, ch->thread->comm, current->pid);
2078 * srpt_create_ch_ib() - Create receive and send completion queues.
2080 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2082 struct ib_qp_init_attr *qp_init;
2083 struct srpt_port *sport = ch->sport;
2084 struct srpt_device *sdev = sport->sdev;
2085 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
2088 WARN_ON(ch->rq_size < 1);
2091 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2095 ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
2096 ch->rq_size + srp_sq_size, 0);
2097 if (IS_ERR(ch->cq)) {
2098 ret = PTR_ERR(ch->cq);
2099 printk(KERN_ERR "failed to create CQ cqe= %d ret= %d\n",
2100 ch->rq_size + srp_sq_size, ret);
2104 qp_init->qp_context = (void *)ch;
2105 qp_init->event_handler
2106 = (void(*)(struct ib_event *, void*))srpt_qp_event;
2107 qp_init->send_cq = ch->cq;
2108 qp_init->recv_cq = ch->cq;
2109 qp_init->srq = sdev->srq;
2110 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2111 qp_init->qp_type = IB_QPT_RC;
2112 qp_init->cap.max_send_wr = srp_sq_size;
2113 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2115 ch->qp = ib_create_qp(sdev->pd, qp_init);
2116 if (IS_ERR(ch->qp)) {
2117 ret = PTR_ERR(ch->qp);
2118 printk(KERN_ERR "failed to create_qp ret= %d\n", ret);
2119 goto err_destroy_cq;
2122 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2124 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2125 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2126 qp_init->cap.max_send_wr, ch->cm_id);
2128 ret = srpt_init_ch_qp(ch, ch->qp);
2130 goto err_destroy_qp;
2132 init_waitqueue_head(&ch->wait_queue);
2134 pr_debug("creating thread for session %s\n", ch->sess_name);
2136 ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2137 if (IS_ERR(ch->thread)) {
2138 printk(KERN_ERR "failed to create kernel thread %ld\n",
2139 PTR_ERR(ch->thread));
2141 goto err_destroy_qp;
2149 ib_destroy_qp(ch->qp);
2151 ib_destroy_cq(ch->cq);
2155 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2158 kthread_stop(ch->thread);
2160 ib_destroy_qp(ch->qp);
2161 ib_destroy_cq(ch->cq);
2165 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2167 * Reset the QP and make sure all resources associated with the channel will
2168 * be deallocated at an appropriate time.
2170 * Note: The caller must hold ch->sport->sdev->spinlock.
2172 static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2174 struct srpt_device *sdev;
2175 enum rdma_ch_state prev_state;
2176 unsigned long flags;
2178 sdev = ch->sport->sdev;
2180 spin_lock_irqsave(&ch->spinlock, flags);
2181 prev_state = ch->state;
2182 switch (prev_state) {
2185 ch->state = CH_DISCONNECTING;
2190 spin_unlock_irqrestore(&ch->spinlock, flags);
2192 switch (prev_state) {
2194 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2198 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
2199 printk(KERN_ERR "sending CM DREQ failed.\n");
2201 case CH_DISCONNECTING:
2210 * srpt_close_ch() - Close an RDMA channel.
2212 static void srpt_close_ch(struct srpt_rdma_ch *ch)
2214 struct srpt_device *sdev;
2216 sdev = ch->sport->sdev;
2217 spin_lock_irq(&sdev->spinlock);
2218 __srpt_close_ch(ch);
2219 spin_unlock_irq(&sdev->spinlock);
2223 * srpt_shutdown_session() - Whether or not a session may be shut down.
2225 static int srpt_shutdown_session(struct se_session *se_sess)
2227 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2228 unsigned long flags;
2230 spin_lock_irqsave(&ch->spinlock, flags);
2231 if (ch->in_shutdown) {
2232 spin_unlock_irqrestore(&ch->spinlock, flags);
2236 ch->in_shutdown = true;
2237 target_sess_cmd_list_set_waiting(se_sess);
2238 spin_unlock_irqrestore(&ch->spinlock, flags);
2244 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2245 * @cm_id: Pointer to the CM ID of the channel to be drained.
2247 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2248 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2249 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2250 * waits until all target sessions for the associated IB device have been
2251 * unregistered and target session registration involves a call to
2252 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2253 * this function has finished).
2255 static void srpt_drain_channel(struct ib_cm_id *cm_id)
2257 struct srpt_device *sdev;
2258 struct srpt_rdma_ch *ch;
2260 bool do_reset = false;
2262 WARN_ON_ONCE(irqs_disabled());
2264 sdev = cm_id->context;
2266 spin_lock_irq(&sdev->spinlock);
2267 list_for_each_entry(ch, &sdev->rch_list, list) {
2268 if (ch->cm_id == cm_id) {
2269 do_reset = srpt_test_and_set_ch_state(ch,
2270 CH_CONNECTING, CH_DRAINING) ||
2271 srpt_test_and_set_ch_state(ch,
2272 CH_LIVE, CH_DRAINING) ||
2273 srpt_test_and_set_ch_state(ch,
2274 CH_DISCONNECTING, CH_DRAINING);
2278 spin_unlock_irq(&sdev->spinlock);
2282 srpt_shutdown_session(ch->sess);
2284 ret = srpt_ch_qp_err(ch);
2286 printk(KERN_ERR "Setting queue pair in error state"
2287 " failed: %d\n", ret);
2292 * srpt_find_channel() - Look up an RDMA channel.
2293 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2295 * Return NULL if no matching RDMA channel has been found.
2297 static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2298 struct ib_cm_id *cm_id)
2300 struct srpt_rdma_ch *ch;
2303 WARN_ON_ONCE(irqs_disabled());
2307 spin_lock_irq(&sdev->spinlock);
2308 list_for_each_entry(ch, &sdev->rch_list, list) {
2309 if (ch->cm_id == cm_id) {
2314 spin_unlock_irq(&sdev->spinlock);
2316 return found ? ch : NULL;
2320 * srpt_release_channel() - Release channel resources.
2322 * Schedules the actual release because:
2323 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2324 * trigger a deadlock.
2325 * - It is not safe to call TCM transport_* functions from interrupt context.
2327 static void srpt_release_channel(struct srpt_rdma_ch *ch)
2329 schedule_work(&ch->release_work);
2332 static void srpt_release_channel_work(struct work_struct *w)
2334 struct srpt_rdma_ch *ch;
2335 struct srpt_device *sdev;
2336 struct se_session *se_sess;
2338 ch = container_of(w, struct srpt_rdma_ch, release_work);
2339 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2342 sdev = ch->sport->sdev;
2348 target_wait_for_sess_cmds(se_sess);
2350 transport_deregister_session_configfs(se_sess);
2351 transport_deregister_session(se_sess);
2354 ib_destroy_cm_id(ch->cm_id);
2356 srpt_destroy_ch_ib(ch);
2358 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2359 ch->sport->sdev, ch->rq_size,
2360 ch->rsp_size, DMA_TO_DEVICE);
2362 spin_lock_irq(&sdev->spinlock);
2363 list_del(&ch->list);
2364 spin_unlock_irq(&sdev->spinlock);
2366 if (ch->release_done)
2367 complete(ch->release_done);
2369 wake_up(&sdev->ch_releaseQ);
2374 static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2377 struct srpt_node_acl *nacl;
2379 list_for_each_entry(nacl, &sport->port_acl_list, list)
2380 if (memcmp(nacl->i_port_id, i_port_id,
2381 sizeof(nacl->i_port_id)) == 0)
2387 static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2390 struct srpt_node_acl *nacl;
2392 spin_lock_irq(&sport->port_acl_lock);
2393 nacl = __srpt_lookup_acl(sport, i_port_id);
2394 spin_unlock_irq(&sport->port_acl_lock);
2400 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2402 * Ownership of the cm_id is transferred to the target session if this
2403 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2405 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2406 struct ib_cm_req_event_param *param,
2409 struct srpt_device *sdev = cm_id->context;
2410 struct srpt_port *sport = &sdev->port[param->port - 1];
2411 struct srp_login_req *req;
2412 struct srp_login_rsp *rsp;
2413 struct srp_login_rej *rej;
2414 struct ib_cm_rep_param *rep_param;
2415 struct srpt_rdma_ch *ch, *tmp_ch;
2416 struct srpt_node_acl *nacl;
2421 WARN_ON_ONCE(irqs_disabled());
2423 if (WARN_ON(!sdev || !private_data))
2426 req = (struct srp_login_req *)private_data;
2428 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2430 printk(KERN_INFO "Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2431 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2432 " (guid=0x%llx:0x%llx)\n",
2433 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2434 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2435 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2436 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2439 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2440 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
2442 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2443 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2444 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2446 if (!rsp || !rej || !rep_param) {
2451 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2452 rej->reason = __constant_cpu_to_be32(
2453 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2455 printk(KERN_ERR "rejected SRP_LOGIN_REQ because its"
2456 " length (%d bytes) is out of range (%d .. %d)\n",
2457 it_iu_len, 64, srp_max_req_size);
2461 if (!sport->enabled) {
2462 rej->reason = __constant_cpu_to_be32(
2463 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2465 printk(KERN_ERR "rejected SRP_LOGIN_REQ because the target port"
2466 " has not yet been enabled\n");
2470 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2471 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2473 spin_lock_irq(&sdev->spinlock);
2475 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2476 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2477 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2478 && param->port == ch->sport->port
2479 && param->listen_id == ch->sport->sdev->cm_id
2481 enum rdma_ch_state ch_state;
2483 ch_state = srpt_get_ch_state(ch);
2484 if (ch_state != CH_CONNECTING
2485 && ch_state != CH_LIVE)
2488 /* found an existing channel */
2489 pr_debug("Found existing channel %s"
2490 " cm_id= %p state= %d\n",
2491 ch->sess_name, ch->cm_id, ch_state);
2493 __srpt_close_ch(ch);
2496 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2500 spin_unlock_irq(&sdev->spinlock);
2503 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2505 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2506 || *(__be64 *)(req->target_port_id + 8) !=
2507 cpu_to_be64(srpt_service_guid)) {
2508 rej->reason = __constant_cpu_to_be32(
2509 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2511 printk(KERN_ERR "rejected SRP_LOGIN_REQ because it"
2512 " has an invalid target port identifier.\n");
2516 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2518 rej->reason = __constant_cpu_to_be32(
2519 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2520 printk(KERN_ERR "rejected SRP_LOGIN_REQ because no memory.\n");
2525 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2526 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2527 memcpy(ch->t_port_id, req->target_port_id, 16);
2528 ch->sport = &sdev->port[param->port - 1];
2531 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2532 * for the SRP protocol to the command queue size.
2534 ch->rq_size = SRPT_RQ_SIZE;
2535 spin_lock_init(&ch->spinlock);
2536 ch->state = CH_CONNECTING;
2537 INIT_LIST_HEAD(&ch->cmd_wait_list);
2538 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2540 ch->ioctx_ring = (struct srpt_send_ioctx **)
2541 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2542 sizeof(*ch->ioctx_ring[0]),
2543 ch->rsp_size, DMA_TO_DEVICE);
2544 if (!ch->ioctx_ring)
2547 INIT_LIST_HEAD(&ch->free_list);
2548 for (i = 0; i < ch->rq_size; i++) {
2549 ch->ioctx_ring[i]->ch = ch;
2550 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2553 ret = srpt_create_ch_ib(ch);
2555 rej->reason = __constant_cpu_to_be32(
2556 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2557 printk(KERN_ERR "rejected SRP_LOGIN_REQ because creating"
2558 " a new RDMA channel failed.\n");
2562 ret = srpt_ch_qp_rtr(ch, ch->qp);
2564 rej->reason = __constant_cpu_to_be32(
2565 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2566 printk(KERN_ERR "rejected SRP_LOGIN_REQ because enabling"
2567 " RTR failed (error code = %d)\n", ret);
2571 * Use the initator port identifier as the session name.
2573 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2574 be64_to_cpu(*(__be64 *)ch->i_port_id),
2575 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2577 pr_debug("registering session %s\n", ch->sess_name);
2579 nacl = srpt_lookup_acl(sport, ch->i_port_id);
2581 printk(KERN_INFO "Rejected login because no ACL has been"
2582 " configured yet for initiator %s.\n", ch->sess_name);
2583 rej->reason = __constant_cpu_to_be32(
2584 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2588 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
2589 if (IS_ERR(ch->sess)) {
2590 rej->reason = __constant_cpu_to_be32(
2591 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2592 pr_debug("Failed to create session\n");
2593 goto deregister_session;
2595 ch->sess->se_node_acl = &nacl->nacl;
2596 transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2598 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2599 ch->sess_name, ch->cm_id);
2601 /* create srp_login_response */
2602 rsp->opcode = SRP_LOGIN_RSP;
2603 rsp->tag = req->tag;
2604 rsp->max_it_iu_len = req->req_it_iu_len;
2605 rsp->max_ti_iu_len = req->req_it_iu_len;
2606 ch->max_ti_iu_len = it_iu_len;
2607 rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2608 | SRP_BUF_FORMAT_INDIRECT);
2609 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2610 atomic_set(&ch->req_lim, ch->rq_size);
2611 atomic_set(&ch->req_lim_delta, 0);
2613 /* create cm reply */
2614 rep_param->qp_num = ch->qp->qp_num;
2615 rep_param->private_data = (void *)rsp;
2616 rep_param->private_data_len = sizeof *rsp;
2617 rep_param->rnr_retry_count = 7;
2618 rep_param->flow_control = 1;
2619 rep_param->failover_accepted = 0;
2621 rep_param->responder_resources = 4;
2622 rep_param->initiator_depth = 4;
2624 ret = ib_send_cm_rep(cm_id, rep_param);
2626 printk(KERN_ERR "sending SRP_LOGIN_REQ response failed"
2627 " (error code = %d)\n", ret);
2628 goto release_channel;
2631 spin_lock_irq(&sdev->spinlock);
2632 list_add_tail(&ch->list, &sdev->rch_list);
2633 spin_unlock_irq(&sdev->spinlock);
2638 srpt_set_ch_state(ch, CH_RELEASING);
2639 transport_deregister_session_configfs(ch->sess);
2642 transport_deregister_session(ch->sess);
2646 srpt_destroy_ch_ib(ch);
2649 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2650 ch->sport->sdev, ch->rq_size,
2651 ch->rsp_size, DMA_TO_DEVICE);
2656 rej->opcode = SRP_LOGIN_REJ;
2657 rej->tag = req->tag;
2658 rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2659 | SRP_BUF_FORMAT_INDIRECT);
2661 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2662 (void *)rej, sizeof *rej);
2672 static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2674 printk(KERN_INFO "Received IB REJ for cm_id %p.\n", cm_id);
2675 srpt_drain_channel(cm_id);
2679 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2681 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2682 * and that the recipient may begin transmitting (RTU = ready to use).
2684 static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2686 struct srpt_rdma_ch *ch;
2689 ch = srpt_find_channel(cm_id->context, cm_id);
2692 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2693 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2695 ret = srpt_ch_qp_rts(ch, ch->qp);
2697 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2699 list_del(&ioctx->wait_list);
2700 srpt_handle_new_iu(ch, ioctx, NULL);
2707 static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2709 printk(KERN_INFO "Received IB TimeWait exit for cm_id %p.\n", cm_id);
2710 srpt_drain_channel(cm_id);
2713 static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2715 printk(KERN_INFO "Received IB REP error for cm_id %p.\n", cm_id);
2716 srpt_drain_channel(cm_id);
2720 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2722 static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2724 struct srpt_rdma_ch *ch;
2725 unsigned long flags;
2726 bool send_drep = false;
2728 ch = srpt_find_channel(cm_id->context, cm_id);
2731 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2733 spin_lock_irqsave(&ch->spinlock, flags);
2734 switch (ch->state) {
2738 ch->state = CH_DISCONNECTING;
2740 case CH_DISCONNECTING:
2743 WARN(true, "unexpected channel state %d\n", ch->state);
2746 spin_unlock_irqrestore(&ch->spinlock, flags);
2749 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
2750 printk(KERN_ERR "Sending IB DREP failed.\n");
2751 printk(KERN_INFO "Received DREQ and sent DREP for session %s.\n",
2757 * srpt_cm_drep_recv() - Process reception of a DREP message.
2759 static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2761 printk(KERN_INFO "Received InfiniBand DREP message for cm_id %p.\n",
2763 srpt_drain_channel(cm_id);
2767 * srpt_cm_handler() - IB connection manager callback function.
2769 * A non-zero return value will cause the caller destroy the CM ID.
2771 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2772 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2773 * a non-zero value in any other case will trigger a race with the
2774 * ib_destroy_cm_id() call in srpt_release_channel().
2776 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2781 switch (event->event) {
2782 case IB_CM_REQ_RECEIVED:
2783 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2784 event->private_data);
2786 case IB_CM_REJ_RECEIVED:
2787 srpt_cm_rej_recv(cm_id);
2789 case IB_CM_RTU_RECEIVED:
2790 case IB_CM_USER_ESTABLISHED:
2791 srpt_cm_rtu_recv(cm_id);
2793 case IB_CM_DREQ_RECEIVED:
2794 srpt_cm_dreq_recv(cm_id);
2796 case IB_CM_DREP_RECEIVED:
2797 srpt_cm_drep_recv(cm_id);
2799 case IB_CM_TIMEWAIT_EXIT:
2800 srpt_cm_timewait_exit(cm_id);
2802 case IB_CM_REP_ERROR:
2803 srpt_cm_rep_error(cm_id);
2805 case IB_CM_DREQ_ERROR:
2806 printk(KERN_INFO "Received IB DREQ ERROR event.\n");
2808 case IB_CM_MRA_RECEIVED:
2809 printk(KERN_INFO "Received IB MRA event\n");
2812 printk(KERN_ERR "received unrecognized IB CM event %d\n",
2821 * srpt_perform_rdmas() - Perform IB RDMA.
2823 * Returns zero upon success or a negative number upon failure.
2825 static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2826 struct srpt_send_ioctx *ioctx)
2828 struct ib_send_wr wr;
2829 struct ib_send_wr *bad_wr;
2830 struct rdma_iu *riu;
2834 enum dma_data_direction dir;
2835 const int n_rdma = ioctx->n_rdma;
2837 dir = ioctx->cmd.data_direction;
2838 if (dir == DMA_TO_DEVICE) {
2841 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2842 if (sq_wr_avail < 0) {
2843 printk(KERN_WARNING "IB send queue full (needed %d)\n",
2849 ioctx->rdma_aborted = false;
2851 riu = ioctx->rdma_ius;
2852 memset(&wr, 0, sizeof wr);
2854 for (i = 0; i < n_rdma; ++i, ++riu) {
2855 if (dir == DMA_FROM_DEVICE) {
2856 wr.opcode = IB_WR_RDMA_WRITE;
2857 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2858 SRPT_RDMA_WRITE_LAST :
2860 ioctx->ioctx.index);
2862 wr.opcode = IB_WR_RDMA_READ;
2863 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2864 SRPT_RDMA_READ_LAST :
2866 ioctx->ioctx.index);
2869 wr.wr.rdma.remote_addr = riu->raddr;
2870 wr.wr.rdma.rkey = riu->rkey;
2871 wr.num_sge = riu->sge_cnt;
2872 wr.sg_list = riu->sge;
2874 /* only get completion event for the last rdma write */
2875 if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
2876 wr.send_flags = IB_SEND_SIGNALED;
2878 ret = ib_post_send(ch->qp, &wr, &bad_wr);
2884 printk(KERN_ERR "%s[%d]: ib_post_send() returned %d for %d/%d",
2885 __func__, __LINE__, ret, i, n_rdma);
2888 wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2889 wr.send_flags = IB_SEND_SIGNALED;
2890 while (ch->state == CH_LIVE &&
2891 ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
2892 printk(KERN_INFO "Trying to abort failed RDMA transfer [%d]",
2893 ioctx->ioctx.index);
2896 while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
2897 printk(KERN_INFO "Waiting until RDMA abort finished [%d]",
2898 ioctx->ioctx.index);
2903 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2904 atomic_add(n_rdma, &ch->sq_wr_avail);
2909 * srpt_xfer_data() - Start data transfer from initiator to target.
2911 static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2912 struct srpt_send_ioctx *ioctx)
2916 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2918 printk(KERN_ERR "%s[%d] ret=%d\n", __func__, __LINE__, ret);
2922 ret = srpt_perform_rdmas(ch, ioctx);
2924 if (ret == -EAGAIN || ret == -ENOMEM)
2925 printk(KERN_INFO "%s[%d] queue full -- ret=%d\n",
2926 __func__, __LINE__, ret);
2928 printk(KERN_ERR "%s[%d] fatal error -- ret=%d\n",
2929 __func__, __LINE__, ret);
2936 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2940 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2942 struct srpt_send_ioctx *ioctx;
2944 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2945 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2949 * srpt_write_pending() - Start data transfer from initiator to target (write).
2951 static int srpt_write_pending(struct se_cmd *se_cmd)
2953 struct srpt_rdma_ch *ch;
2954 struct srpt_send_ioctx *ioctx;
2955 enum srpt_command_state new_state;
2956 enum rdma_ch_state ch_state;
2959 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2961 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2962 WARN_ON(new_state == SRPT_STATE_DONE);
2967 ch_state = srpt_get_ch_state(ch);
2970 WARN(true, "unexpected channel state %d\n", ch_state);
2975 case CH_DISCONNECTING:
2978 pr_debug("cmd with tag %lld: channel disconnecting\n",
2980 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2984 ret = srpt_xfer_data(ch, ioctx);
2990 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2992 switch (tcm_mgmt_status) {
2993 case TMR_FUNCTION_COMPLETE:
2994 return SRP_TSK_MGMT_SUCCESS;
2995 case TMR_FUNCTION_REJECTED:
2996 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2998 return SRP_TSK_MGMT_FAILED;
3002 * srpt_queue_response() - Transmits the response to a SCSI command.
3004 * Callback function called by the TCM core. Must not block since it can be
3005 * invoked on the context of the IB completion handler.
3007 static void srpt_queue_response(struct se_cmd *cmd)
3009 struct srpt_rdma_ch *ch;
3010 struct srpt_send_ioctx *ioctx;
3011 enum srpt_command_state state;
3012 unsigned long flags;
3014 enum dma_data_direction dir;
3018 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3022 spin_lock_irqsave(&ioctx->spinlock, flags);
3023 state = ioctx->state;
3025 case SRPT_STATE_NEW:
3026 case SRPT_STATE_DATA_IN:
3027 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
3029 case SRPT_STATE_MGMT:
3030 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
3033 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
3034 ch, ioctx->ioctx.index, ioctx->state);
3037 spin_unlock_irqrestore(&ioctx->spinlock, flags);
3039 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
3040 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
3041 atomic_inc(&ch->req_lim_delta);
3042 srpt_abort_cmd(ioctx);
3046 dir = ioctx->cmd.data_direction;
3048 /* For read commands, transfer the data to the initiator. */
3049 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
3050 !ioctx->queue_status_only) {
3051 ret = srpt_xfer_data(ch, ioctx);
3053 printk(KERN_ERR "xfer_data failed for tag %llu\n",
3059 if (state != SRPT_STATE_MGMT)
3060 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
3064 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3065 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
3068 ret = srpt_post_send(ch, ioctx, resp_len);
3070 printk(KERN_ERR "sending cmd response failed for tag %llu\n",
3072 srpt_unmap_sg_to_ib_sge(ch, ioctx);
3073 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
3074 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
3078 static int srpt_queue_data_in(struct se_cmd *cmd)
3080 srpt_queue_response(cmd);
3084 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
3086 srpt_queue_response(cmd);
3089 static void srpt_aborted_task(struct se_cmd *cmd)
3091 struct srpt_send_ioctx *ioctx = container_of(cmd,
3092 struct srpt_send_ioctx, cmd);
3094 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
3097 static int srpt_queue_status(struct se_cmd *cmd)
3099 struct srpt_send_ioctx *ioctx;
3101 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3102 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3103 if (cmd->se_cmd_flags &
3104 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3105 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3106 ioctx->queue_status_only = true;
3107 srpt_queue_response(cmd);
3111 static void srpt_refresh_port_work(struct work_struct *work)
3113 struct srpt_port *sport = container_of(work, struct srpt_port, work);
3115 srpt_refresh_port(sport);
3118 static int srpt_ch_list_empty(struct srpt_device *sdev)
3122 spin_lock_irq(&sdev->spinlock);
3123 res = list_empty(&sdev->rch_list);
3124 spin_unlock_irq(&sdev->spinlock);
3130 * srpt_release_sdev() - Free the channel resources associated with a target.
3132 static int srpt_release_sdev(struct srpt_device *sdev)
3134 struct srpt_rdma_ch *ch, *tmp_ch;
3137 WARN_ON_ONCE(irqs_disabled());
3141 spin_lock_irq(&sdev->spinlock);
3142 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3143 __srpt_close_ch(ch);
3144 spin_unlock_irq(&sdev->spinlock);
3146 res = wait_event_interruptible(sdev->ch_releaseQ,
3147 srpt_ch_list_empty(sdev));
3149 printk(KERN_ERR "%s: interrupted.\n", __func__);
3154 static struct srpt_port *__srpt_lookup_port(const char *name)
3156 struct ib_device *dev;
3157 struct srpt_device *sdev;
3158 struct srpt_port *sport;
3161 list_for_each_entry(sdev, &srpt_dev_list, list) {
3166 for (i = 0; i < dev->phys_port_cnt; i++) {
3167 sport = &sdev->port[i];
3169 if (!strcmp(sport->port_guid, name))
3177 static struct srpt_port *srpt_lookup_port(const char *name)
3179 struct srpt_port *sport;
3181 spin_lock(&srpt_dev_lock);
3182 sport = __srpt_lookup_port(name);
3183 spin_unlock(&srpt_dev_lock);
3189 * srpt_add_one() - Infiniband device addition callback function.
3191 static void srpt_add_one(struct ib_device *device)
3193 struct srpt_device *sdev;
3194 struct srpt_port *sport;
3195 struct ib_srq_init_attr srq_attr;
3198 pr_debug("device = %p, device->dma_ops = %p\n", device,
3201 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3205 sdev->device = device;
3206 INIT_LIST_HEAD(&sdev->rch_list);
3207 init_waitqueue_head(&sdev->ch_releaseQ);
3208 spin_lock_init(&sdev->spinlock);
3210 if (ib_query_device(device, &sdev->dev_attr))
3213 sdev->pd = ib_alloc_pd(device);
3214 if (IS_ERR(sdev->pd))
3217 sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
3218 if (IS_ERR(sdev->mr))
3221 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3223 srq_attr.event_handler = srpt_srq_event;
3224 srq_attr.srq_context = (void *)sdev;
3225 srq_attr.attr.max_wr = sdev->srq_size;
3226 srq_attr.attr.max_sge = 1;
3227 srq_attr.attr.srq_limit = 0;
3228 srq_attr.srq_type = IB_SRQT_BASIC;
3230 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3231 if (IS_ERR(sdev->srq))
3234 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3235 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3238 if (!srpt_service_guid)
3239 srpt_service_guid = be64_to_cpu(device->node_guid);
3241 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3242 if (IS_ERR(sdev->cm_id))
3245 /* print out target login information */
3246 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3247 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3248 srpt_service_guid, srpt_service_guid);
3251 * We do not have a consistent service_id (ie. also id_ext of target_id)
3252 * to identify this target. We currently use the guid of the first HCA
3253 * in the system as service_id; therefore, the target_id will change
3254 * if this HCA is gone bad and replaced by different HCA
3256 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
3259 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3260 srpt_event_handler);
3261 if (ib_register_event_handler(&sdev->event_handler))
3264 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3265 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3266 sizeof(*sdev->ioctx_ring[0]),
3267 srp_max_req_size, DMA_FROM_DEVICE);
3268 if (!sdev->ioctx_ring)
3271 for (i = 0; i < sdev->srq_size; ++i)
3272 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3274 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
3276 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3277 sport = &sdev->port[i - 1];
3280 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3281 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3282 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3283 INIT_WORK(&sport->work, srpt_refresh_port_work);
3284 INIT_LIST_HEAD(&sport->port_acl_list);
3285 spin_lock_init(&sport->port_acl_lock);
3287 if (srpt_refresh_port(sport)) {
3288 printk(KERN_ERR "MAD registration failed for %s-%d.\n",
3289 srpt_sdev_name(sdev), i);
3292 snprintf(sport->port_guid, sizeof(sport->port_guid),
3294 be64_to_cpu(sport->gid.global.subnet_prefix),
3295 be64_to_cpu(sport->gid.global.interface_id));
3298 spin_lock(&srpt_dev_lock);
3299 list_add_tail(&sdev->list, &srpt_dev_list);
3300 spin_unlock(&srpt_dev_lock);
3303 ib_set_client_data(device, &srpt_client, sdev);
3304 pr_debug("added %s.\n", device->name);
3308 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3309 sdev->srq_size, srp_max_req_size,
3312 ib_unregister_event_handler(&sdev->event_handler);
3314 ib_destroy_cm_id(sdev->cm_id);
3316 ib_destroy_srq(sdev->srq);
3318 ib_dereg_mr(sdev->mr);
3320 ib_dealloc_pd(sdev->pd);
3325 printk(KERN_INFO "%s(%s) failed.\n", __func__, device->name);
3330 * srpt_remove_one() - InfiniBand device removal callback function.
3332 static void srpt_remove_one(struct ib_device *device)
3334 struct srpt_device *sdev;
3337 sdev = ib_get_client_data(device, &srpt_client);
3339 printk(KERN_INFO "%s(%s): nothing to do.\n", __func__,
3344 srpt_unregister_mad_agent(sdev);
3346 ib_unregister_event_handler(&sdev->event_handler);
3348 /* Cancel any work queued by the just unregistered IB event handler. */
3349 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3350 cancel_work_sync(&sdev->port[i].work);
3352 ib_destroy_cm_id(sdev->cm_id);
3355 * Unregistering a target must happen after destroying sdev->cm_id
3356 * such that no new SRP_LOGIN_REQ information units can arrive while
3357 * destroying the target.
3359 spin_lock(&srpt_dev_lock);
3360 list_del(&sdev->list);
3361 spin_unlock(&srpt_dev_lock);
3362 srpt_release_sdev(sdev);
3364 ib_destroy_srq(sdev->srq);
3365 ib_dereg_mr(sdev->mr);
3366 ib_dealloc_pd(sdev->pd);
3368 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3369 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3370 sdev->ioctx_ring = NULL;
3374 static struct ib_client srpt_client = {
3376 .add = srpt_add_one,
3377 .remove = srpt_remove_one
3380 static int srpt_check_true(struct se_portal_group *se_tpg)
3385 static int srpt_check_false(struct se_portal_group *se_tpg)
3390 static char *srpt_get_fabric_name(void)
3395 static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
3397 return SCSI_TRANSPORTID_PROTOCOLID_SRP;
3400 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3402 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3404 return sport->port_guid;
3407 static u16 srpt_get_tag(struct se_portal_group *tpg)
3412 static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
3417 static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
3418 struct se_node_acl *se_nacl,
3419 struct t10_pr_registration *pr_reg,
3420 int *format_code, unsigned char *buf)
3422 struct srpt_node_acl *nacl;
3423 struct spc_rdma_transport_id *tr_id;
3425 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3426 tr_id = (void *)buf;
3427 tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
3428 memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
3429 return sizeof(*tr_id);
3432 static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
3433 struct se_node_acl *se_nacl,
3434 struct t10_pr_registration *pr_reg,
3438 return sizeof(struct spc_rdma_transport_id);
3441 static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
3442 const char *buf, u32 *out_tid_len,
3443 char **port_nexus_ptr)
3445 struct spc_rdma_transport_id *tr_id;
3447 *port_nexus_ptr = NULL;
3448 *out_tid_len = sizeof(struct spc_rdma_transport_id);
3449 tr_id = (void *)buf;
3450 return (char *)tr_id->i_port_id;
3453 static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
3455 struct srpt_node_acl *nacl;
3457 nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
3459 printk(KERN_ERR "Unable to allocate struct srpt_node_acl\n");
3466 static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
3467 struct se_node_acl *se_nacl)
3469 struct srpt_node_acl *nacl;
3471 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3475 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3480 static void srpt_release_cmd(struct se_cmd *se_cmd)
3482 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3483 struct srpt_send_ioctx, cmd);
3484 struct srpt_rdma_ch *ch = ioctx->ch;
3485 unsigned long flags;
3487 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3488 WARN_ON(ioctx->mapped_sg_count != 0);
3490 if (ioctx->n_rbuf > 1) {
3491 kfree(ioctx->rbufs);
3492 ioctx->rbufs = NULL;
3496 spin_lock_irqsave(&ch->spinlock, flags);
3497 list_add(&ioctx->free_list, &ch->free_list);
3498 spin_unlock_irqrestore(&ch->spinlock, flags);
3502 * srpt_close_session() - Forcibly close a session.
3504 * Callback function invoked by the TCM core to clean up sessions associated
3505 * with a node ACL when the user invokes
3506 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3508 static void srpt_close_session(struct se_session *se_sess)
3510 DECLARE_COMPLETION_ONSTACK(release_done);
3511 struct srpt_rdma_ch *ch;
3512 struct srpt_device *sdev;
3515 ch = se_sess->fabric_sess_ptr;
3516 WARN_ON(ch->sess != se_sess);
3518 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3520 sdev = ch->sport->sdev;
3521 spin_lock_irq(&sdev->spinlock);
3522 BUG_ON(ch->release_done);
3523 ch->release_done = &release_done;
3524 __srpt_close_ch(ch);
3525 spin_unlock_irq(&sdev->spinlock);
3527 res = wait_for_completion_timeout(&release_done, 60 * HZ);
3532 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3534 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3535 * This object represents an arbitrary integer used to uniquely identify a
3536 * particular attached remote initiator port to a particular SCSI target port
3537 * within a particular SCSI target device within a particular SCSI instance.
3539 static u32 srpt_sess_get_index(struct se_session *se_sess)
3544 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3548 static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
3550 struct srpt_send_ioctx *ioctx;
3552 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3556 /* Note: only used from inside debug printk's by the TCM core. */
3557 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3559 struct srpt_send_ioctx *ioctx;
3561 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3562 return srpt_get_cmd_state(ioctx);
3566 * srpt_parse_i_port_id() - Parse an initiator port ID.
3567 * @name: ASCII representation of a 128-bit initiator port ID.
3568 * @i_port_id: Binary 128-bit port ID.
3570 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3573 unsigned len, count, leading_zero_bytes;
3577 if (strncasecmp(p, "0x", 2) == 0)
3583 count = min(len / 2, 16U);
3584 leading_zero_bytes = 16 - count;
3585 memset(i_port_id, 0, leading_zero_bytes);
3586 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3588 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3595 * configfs callback function invoked for
3596 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3598 static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
3599 struct config_group *group,
3602 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3603 struct se_node_acl *se_nacl, *se_nacl_new;
3604 struct srpt_node_acl *nacl;
3606 u32 nexus_depth = 1;
3609 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
3610 printk(KERN_ERR "invalid initiator port ID %s\n", name);
3615 se_nacl_new = srpt_alloc_fabric_acl(tpg);
3621 * nacl_new may be released by core_tpg_add_initiator_node_acl()
3622 * when converting a node ACL from demo mode to explict
3624 se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
3626 if (IS_ERR(se_nacl)) {
3627 ret = PTR_ERR(se_nacl);
3630 /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
3631 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3632 memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3633 nacl->sport = sport;
3635 spin_lock_irq(&sport->port_acl_lock);
3636 list_add_tail(&nacl->list, &sport->port_acl_list);
3637 spin_unlock_irq(&sport->port_acl_lock);
3641 return ERR_PTR(ret);
3645 * configfs callback function invoked for
3646 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3648 static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
3650 struct srpt_node_acl *nacl;
3651 struct srpt_device *sdev;
3652 struct srpt_port *sport;
3654 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3655 sport = nacl->sport;
3657 spin_lock_irq(&sport->port_acl_lock);
3658 list_del(&nacl->list);
3659 spin_unlock_irq(&sport->port_acl_lock);
3660 core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
3661 srpt_release_fabric_acl(NULL, se_nacl);
3664 static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
3665 struct se_portal_group *se_tpg,
3668 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3670 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3673 static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
3674 struct se_portal_group *se_tpg,
3678 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3682 ret = kstrtoul(page, 0, &val);
3684 pr_err("kstrtoul() failed with ret: %d\n", ret);
3687 if (val > MAX_SRPT_RDMA_SIZE) {
3688 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3689 MAX_SRPT_RDMA_SIZE);
3692 if (val < DEFAULT_MAX_RDMA_SIZE) {
3693 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3694 val, DEFAULT_MAX_RDMA_SIZE);
3697 sport->port_attrib.srp_max_rdma_size = val;
3702 TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
3704 static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
3705 struct se_portal_group *se_tpg,
3708 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3710 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3713 static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
3714 struct se_portal_group *se_tpg,
3718 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3722 ret = kstrtoul(page, 0, &val);
3724 pr_err("kstrtoul() failed with ret: %d\n", ret);
3727 if (val > MAX_SRPT_RSP_SIZE) {
3728 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3732 if (val < MIN_MAX_RSP_SIZE) {
3733 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3737 sport->port_attrib.srp_max_rsp_size = val;
3742 TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
3744 static ssize_t srpt_tpg_attrib_show_srp_sq_size(
3745 struct se_portal_group *se_tpg,
3748 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3750 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3753 static ssize_t srpt_tpg_attrib_store_srp_sq_size(
3754 struct se_portal_group *se_tpg,
3758 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3762 ret = kstrtoul(page, 0, &val);
3764 pr_err("kstrtoul() failed with ret: %d\n", ret);
3767 if (val > MAX_SRPT_SRQ_SIZE) {
3768 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3772 if (val < MIN_SRPT_SRQ_SIZE) {
3773 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3777 sport->port_attrib.srp_sq_size = val;
3782 TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
3784 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3785 &srpt_tpg_attrib_srp_max_rdma_size.attr,
3786 &srpt_tpg_attrib_srp_max_rsp_size.attr,
3787 &srpt_tpg_attrib_srp_sq_size.attr,
3791 static ssize_t srpt_tpg_show_enable(
3792 struct se_portal_group *se_tpg,
3795 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3797 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3800 static ssize_t srpt_tpg_store_enable(
3801 struct se_portal_group *se_tpg,
3805 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3809 ret = kstrtoul(page, 0, &tmp);
3811 printk(KERN_ERR "Unable to extract srpt_tpg_store_enable\n");
3815 if ((tmp != 0) && (tmp != 1)) {
3816 printk(KERN_ERR "Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3820 sport->enabled = true;
3822 sport->enabled = false;
3827 TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
3829 static struct configfs_attribute *srpt_tpg_attrs[] = {
3830 &srpt_tpg_enable.attr,
3835 * configfs callback invoked for
3836 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3838 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3839 struct config_group *group,
3842 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3845 /* Initialize sport->port_wwn and sport->port_tpg_1 */
3846 res = core_tpg_register(&srpt_target->tf_ops, &sport->port_wwn,
3847 &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
3849 return ERR_PTR(res);
3851 return &sport->port_tpg_1;
3855 * configfs callback invoked for
3856 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3858 static void srpt_drop_tpg(struct se_portal_group *tpg)
3860 struct srpt_port *sport = container_of(tpg,
3861 struct srpt_port, port_tpg_1);
3863 sport->enabled = false;
3864 core_tpg_deregister(&sport->port_tpg_1);
3868 * configfs callback invoked for
3869 * mkdir /sys/kernel/config/target/$driver/$port
3871 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3872 struct config_group *group,
3875 struct srpt_port *sport;
3878 sport = srpt_lookup_port(name);
3879 pr_debug("make_tport(%s)\n", name);
3884 return &sport->port_wwn;
3887 return ERR_PTR(ret);
3891 * configfs callback invoked for
3892 * rmdir /sys/kernel/config/target/$driver/$port
3894 static void srpt_drop_tport(struct se_wwn *wwn)
3896 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3898 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3901 static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
3904 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3907 TF_WWN_ATTR_RO(srpt, version);
3909 static struct configfs_attribute *srpt_wwn_attrs[] = {
3910 &srpt_wwn_version.attr,
3914 static struct target_core_fabric_ops srpt_template = {
3915 .get_fabric_name = srpt_get_fabric_name,
3916 .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
3917 .tpg_get_wwn = srpt_get_fabric_wwn,
3918 .tpg_get_tag = srpt_get_tag,
3919 .tpg_get_default_depth = srpt_get_default_depth,
3920 .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
3921 .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
3922 .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
3923 .tpg_check_demo_mode = srpt_check_false,
3924 .tpg_check_demo_mode_cache = srpt_check_true,
3925 .tpg_check_demo_mode_write_protect = srpt_check_true,
3926 .tpg_check_prod_mode_write_protect = srpt_check_false,
3927 .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
3928 .tpg_release_fabric_acl = srpt_release_fabric_acl,
3929 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3930 .release_cmd = srpt_release_cmd,
3931 .check_stop_free = srpt_check_stop_free,
3932 .shutdown_session = srpt_shutdown_session,
3933 .close_session = srpt_close_session,
3934 .sess_get_index = srpt_sess_get_index,
3935 .sess_get_initiator_sid = NULL,
3936 .write_pending = srpt_write_pending,
3937 .write_pending_status = srpt_write_pending_status,
3938 .set_default_node_attributes = srpt_set_default_node_attrs,
3939 .get_task_tag = srpt_get_task_tag,
3940 .get_cmd_state = srpt_get_tcm_cmd_state,
3941 .queue_data_in = srpt_queue_data_in,
3942 .queue_status = srpt_queue_status,
3943 .queue_tm_rsp = srpt_queue_tm_rsp,
3944 .aborted_task = srpt_aborted_task,
3946 * Setup function pointers for generic logic in
3947 * target_core_fabric_configfs.c
3949 .fabric_make_wwn = srpt_make_tport,
3950 .fabric_drop_wwn = srpt_drop_tport,
3951 .fabric_make_tpg = srpt_make_tpg,
3952 .fabric_drop_tpg = srpt_drop_tpg,
3953 .fabric_post_link = NULL,
3954 .fabric_pre_unlink = NULL,
3955 .fabric_make_np = NULL,
3956 .fabric_drop_np = NULL,
3957 .fabric_make_nodeacl = srpt_make_nodeacl,
3958 .fabric_drop_nodeacl = srpt_drop_nodeacl,
3962 * srpt_init_module() - Kernel module initialization.
3964 * Note: Since ib_register_client() registers callback functions, and since at
3965 * least one of these callback functions (srpt_add_one()) calls target core
3966 * functions, this driver must be registered with the target core before
3967 * ib_register_client() is called.
3969 static int __init srpt_init_module(void)
3974 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3975 printk(KERN_ERR "invalid value %d for kernel module parameter"
3976 " srp_max_req_size -- must be at least %d.\n",
3977 srp_max_req_size, MIN_MAX_REQ_SIZE);
3981 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3982 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3983 printk(KERN_ERR "invalid value %d for kernel module parameter"
3984 " srpt_srq_size -- must be in the range [%d..%d].\n",
3985 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3989 srpt_target = target_fabric_configfs_init(THIS_MODULE, "srpt");
3990 if (IS_ERR(srpt_target)) {
3991 printk(KERN_ERR "couldn't register\n");
3992 ret = PTR_ERR(srpt_target);
3996 srpt_target->tf_ops = srpt_template;
3999 * Set up default attribute lists.
4001 srpt_target->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = srpt_wwn_attrs;
4002 srpt_target->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = srpt_tpg_attrs;
4003 srpt_target->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = srpt_tpg_attrib_attrs;
4004 srpt_target->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
4005 srpt_target->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
4006 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
4007 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
4008 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
4009 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
4011 ret = target_fabric_configfs_register(srpt_target);
4013 printk(KERN_ERR "couldn't register\n");
4014 goto out_free_target;
4017 ret = ib_register_client(&srpt_client);
4019 printk(KERN_ERR "couldn't register IB client\n");
4020 goto out_unregister_target;
4025 out_unregister_target:
4026 target_fabric_configfs_deregister(srpt_target);
4030 target_fabric_configfs_free(srpt_target);
4035 static void __exit srpt_cleanup_module(void)
4037 ib_unregister_client(&srpt_client);
4038 target_fabric_configfs_deregister(srpt_target);
4042 module_init(srpt_init_module);
4043 module_exit(srpt_cleanup_module);