2 * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
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 <linux/inet.h>
45 #include <rdma/ib_cache.h>
46 #include <scsi/scsi_proto.h>
47 #include <scsi/scsi_tcq.h>
48 #include <target/target_core_base.h>
49 #include <target/target_core_fabric.h>
52 /* Name of this kernel module. */
53 #define DRV_NAME "ib_srpt"
55 #define SRPT_ID_STRING "Linux SRP target"
58 #define pr_fmt(fmt) DRV_NAME " " fmt
60 MODULE_AUTHOR("Vu Pham and Bart Van Assche");
61 MODULE_DESCRIPTION("SCSI RDMA Protocol target driver");
62 MODULE_LICENSE("Dual BSD/GPL");
68 static u64 srpt_service_guid;
69 static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
70 static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
72 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
73 module_param(srp_max_req_size, int, 0444);
74 MODULE_PARM_DESC(srp_max_req_size,
75 "Maximum size of SRP request messages in bytes.");
77 static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
78 module_param(srpt_srq_size, int, 0444);
79 MODULE_PARM_DESC(srpt_srq_size,
80 "Shared receive queue (SRQ) size.");
82 static int srpt_get_u64_x(char *buffer, const struct kernel_param *kp)
84 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
86 module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
88 MODULE_PARM_DESC(srpt_service_guid,
89 "Using this value for ioc_guid, id_ext, and cm_listen_id instead of using the node_guid of the first HCA.");
91 static struct ib_client srpt_client;
92 /* Protects both rdma_cm_port and rdma_cm_id. */
93 static DEFINE_MUTEX(rdma_cm_mutex);
94 /* Port number RDMA/CM will bind to. */
95 static u16 rdma_cm_port;
96 static struct rdma_cm_id *rdma_cm_id;
97 static void srpt_release_cmd(struct se_cmd *se_cmd);
98 static void srpt_free_ch(struct kref *kref);
99 static int srpt_queue_status(struct se_cmd *cmd);
100 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
101 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
102 static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
105 * The only allowed channel state changes are those that change the channel
106 * state into a state with a higher numerical value. Hence the new > prev test.
108 static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
111 enum rdma_ch_state prev;
112 bool changed = false;
114 spin_lock_irqsave(&ch->spinlock, flags);
120 spin_unlock_irqrestore(&ch->spinlock, flags);
126 * srpt_event_handler - asynchronous IB event callback function
127 * @handler: IB event handler registered by ib_register_event_handler().
128 * @event: Description of the event that occurred.
130 * Callback function called by the InfiniBand core when an asynchronous IB
131 * event occurs. This callback may occur in interrupt context. See also
132 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
133 * Architecture Specification.
135 static void srpt_event_handler(struct ib_event_handler *handler,
136 struct ib_event *event)
138 struct srpt_device *sdev;
139 struct srpt_port *sport;
142 sdev = ib_get_client_data(event->device, &srpt_client);
143 if (!sdev || sdev->device != event->device)
146 pr_debug("ASYNC event= %d on device= %s\n", event->event,
147 dev_name(&sdev->device->dev));
149 switch (event->event) {
150 case IB_EVENT_PORT_ERR:
151 port_num = event->element.port_num - 1;
152 if (port_num < sdev->device->phys_port_cnt) {
153 sport = &sdev->port[port_num];
157 WARN(true, "event %d: port_num %d out of range 1..%d\n",
158 event->event, port_num + 1,
159 sdev->device->phys_port_cnt);
162 case IB_EVENT_PORT_ACTIVE:
163 case IB_EVENT_LID_CHANGE:
164 case IB_EVENT_PKEY_CHANGE:
165 case IB_EVENT_SM_CHANGE:
166 case IB_EVENT_CLIENT_REREGISTER:
167 case IB_EVENT_GID_CHANGE:
168 /* Refresh port data asynchronously. */
169 port_num = event->element.port_num - 1;
170 if (port_num < sdev->device->phys_port_cnt) {
171 sport = &sdev->port[port_num];
172 if (!sport->lid && !sport->sm_lid)
173 schedule_work(&sport->work);
175 WARN(true, "event %d: port_num %d out of range 1..%d\n",
176 event->event, port_num + 1,
177 sdev->device->phys_port_cnt);
181 pr_err("received unrecognized IB event %d\n", event->event);
187 * srpt_srq_event - SRQ event callback function
188 * @event: Description of the event that occurred.
189 * @ctx: Context pointer specified at SRQ creation time.
191 static void srpt_srq_event(struct ib_event *event, void *ctx)
193 pr_debug("SRQ event %d\n", event->event);
196 static const char *get_ch_state_name(enum rdma_ch_state s)
203 case CH_DISCONNECTING:
204 return "disconnecting";
207 case CH_DISCONNECTED:
208 return "disconnected";
214 * srpt_qp_event - QP event callback function
215 * @event: Description of the event that occurred.
216 * @ch: SRPT RDMA channel.
218 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
220 pr_debug("QP event %d on ch=%p sess_name=%s state=%d\n",
221 event->event, ch, ch->sess_name, ch->state);
223 switch (event->event) {
224 case IB_EVENT_COMM_EST:
225 if (ch->using_rdma_cm)
226 rdma_notify(ch->rdma_cm.cm_id, event->event);
228 ib_cm_notify(ch->ib_cm.cm_id, event->event);
230 case IB_EVENT_QP_LAST_WQE_REACHED:
231 pr_debug("%s-%d, state %s: received Last WQE event.\n",
232 ch->sess_name, ch->qp->qp_num,
233 get_ch_state_name(ch->state));
236 pr_err("received unrecognized IB QP event %d\n", event->event);
242 * srpt_set_ioc - initialize a IOUnitInfo structure
243 * @c_list: controller list.
244 * @slot: one-based slot number.
245 * @value: four-bit value.
247 * Copies the lowest four bits of value in element slot of the array of four
248 * bit elements called c_list (controller list). The index slot is one-based.
250 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
257 tmp = c_list[id] & 0xf;
258 c_list[id] = (value << 4) | tmp;
260 tmp = c_list[id] & 0xf0;
261 c_list[id] = (value & 0xf) | tmp;
266 * srpt_get_class_port_info - copy ClassPortInfo to a management datagram
267 * @mad: Datagram that will be sent as response to DM_ATTR_CLASS_PORT_INFO.
269 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
272 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
274 struct ib_class_port_info *cif;
276 cif = (struct ib_class_port_info *)mad->data;
277 memset(cif, 0, sizeof(*cif));
278 cif->base_version = 1;
279 cif->class_version = 1;
281 ib_set_cpi_resp_time(cif, 20);
282 mad->mad_hdr.status = 0;
286 * srpt_get_iou - write IOUnitInfo to a management datagram
287 * @mad: Datagram that will be sent as response to DM_ATTR_IOU_INFO.
289 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
290 * Specification. See also section B.7, table B.6 in the SRP r16a document.
292 static void srpt_get_iou(struct ib_dm_mad *mad)
294 struct ib_dm_iou_info *ioui;
298 ioui = (struct ib_dm_iou_info *)mad->data;
299 ioui->change_id = cpu_to_be16(1);
300 ioui->max_controllers = 16;
302 /* set present for slot 1 and empty for the rest */
303 srpt_set_ioc(ioui->controller_list, 1, 1);
304 for (i = 1, slot = 2; i < 16; i++, slot++)
305 srpt_set_ioc(ioui->controller_list, slot, 0);
307 mad->mad_hdr.status = 0;
311 * srpt_get_ioc - write IOControllerprofile to a management datagram
312 * @sport: HCA port through which the MAD has been received.
313 * @slot: Slot number specified in DM_ATTR_IOC_PROFILE query.
314 * @mad: Datagram that will be sent as response to DM_ATTR_IOC_PROFILE.
316 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
317 * Architecture Specification. See also section B.7, table B.7 in the SRP
320 static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
321 struct ib_dm_mad *mad)
323 struct srpt_device *sdev = sport->sdev;
324 struct ib_dm_ioc_profile *iocp;
325 int send_queue_depth;
327 iocp = (struct ib_dm_ioc_profile *)mad->data;
329 if (!slot || slot > 16) {
331 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
337 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
342 send_queue_depth = sdev->srq_size;
344 send_queue_depth = min(MAX_SRPT_RQ_SIZE,
345 sdev->device->attrs.max_qp_wr);
347 memset(iocp, 0, sizeof(*iocp));
348 strcpy(iocp->id_string, SRPT_ID_STRING);
349 iocp->guid = cpu_to_be64(srpt_service_guid);
350 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
351 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
352 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
353 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
354 iocp->subsys_device_id = 0x0;
355 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
356 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
357 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
358 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
359 iocp->send_queue_depth = cpu_to_be16(send_queue_depth);
360 iocp->rdma_read_depth = 4;
361 iocp->send_size = cpu_to_be32(srp_max_req_size);
362 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
364 iocp->num_svc_entries = 1;
365 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
366 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
368 mad->mad_hdr.status = 0;
372 * srpt_get_svc_entries - write ServiceEntries to a management datagram
373 * @ioc_guid: I/O controller GUID to use in reply.
374 * @slot: I/O controller number.
375 * @hi: End of the range of service entries to be specified in the reply.
376 * @lo: Start of the range of service entries to be specified in the reply..
377 * @mad: Datagram that will be sent as response to DM_ATTR_SVC_ENTRIES.
379 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
380 * Specification. See also section B.7, table B.8 in the SRP r16a document.
382 static void srpt_get_svc_entries(u64 ioc_guid,
383 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
385 struct ib_dm_svc_entries *svc_entries;
389 if (!slot || slot > 16) {
391 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
395 if (slot > 2 || lo > hi || hi > 1) {
397 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
401 svc_entries = (struct ib_dm_svc_entries *)mad->data;
402 memset(svc_entries, 0, sizeof(*svc_entries));
403 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
404 snprintf(svc_entries->service_entries[0].name,
405 sizeof(svc_entries->service_entries[0].name),
407 SRP_SERVICE_NAME_PREFIX,
410 mad->mad_hdr.status = 0;
414 * srpt_mgmt_method_get - process a received management datagram
415 * @sp: HCA port through which the MAD has been received.
416 * @rq_mad: received MAD.
417 * @rsp_mad: response MAD.
419 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
420 struct ib_dm_mad *rsp_mad)
426 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
428 case DM_ATTR_CLASS_PORT_INFO:
429 srpt_get_class_port_info(rsp_mad);
431 case DM_ATTR_IOU_INFO:
432 srpt_get_iou(rsp_mad);
434 case DM_ATTR_IOC_PROFILE:
435 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
436 srpt_get_ioc(sp, slot, rsp_mad);
438 case DM_ATTR_SVC_ENTRIES:
439 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
440 hi = (u8) ((slot >> 8) & 0xff);
441 lo = (u8) (slot & 0xff);
442 slot = (u16) ((slot >> 16) & 0xffff);
443 srpt_get_svc_entries(srpt_service_guid,
444 slot, hi, lo, rsp_mad);
447 rsp_mad->mad_hdr.status =
448 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
454 * srpt_mad_send_handler - MAD send completion callback
455 * @mad_agent: Return value of ib_register_mad_agent().
456 * @mad_wc: Work completion reporting that the MAD has been sent.
458 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
459 struct ib_mad_send_wc *mad_wc)
461 rdma_destroy_ah(mad_wc->send_buf->ah, RDMA_DESTROY_AH_SLEEPABLE);
462 ib_free_send_mad(mad_wc->send_buf);
466 * srpt_mad_recv_handler - MAD reception callback function
467 * @mad_agent: Return value of ib_register_mad_agent().
468 * @send_buf: Not used.
469 * @mad_wc: Work completion reporting that a MAD has been received.
471 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
472 struct ib_mad_send_buf *send_buf,
473 struct ib_mad_recv_wc *mad_wc)
475 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
477 struct ib_mad_send_buf *rsp;
478 struct ib_dm_mad *dm_mad;
480 if (!mad_wc || !mad_wc->recv_buf.mad)
483 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
484 mad_wc->recv_buf.grh, mad_agent->port_num);
488 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
490 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
491 mad_wc->wc->pkey_index, 0,
492 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
494 IB_MGMT_BASE_VERSION);
501 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
502 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
503 dm_mad->mad_hdr.status = 0;
505 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
506 case IB_MGMT_METHOD_GET:
507 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
509 case IB_MGMT_METHOD_SET:
510 dm_mad->mad_hdr.status =
511 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
514 dm_mad->mad_hdr.status =
515 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
519 if (!ib_post_send_mad(rsp, NULL)) {
520 ib_free_recv_mad(mad_wc);
521 /* will destroy_ah & free_send_mad in send completion */
525 ib_free_send_mad(rsp);
528 rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE);
530 ib_free_recv_mad(mad_wc);
533 static int srpt_format_guid(char *buf, unsigned int size, const __be64 *guid)
535 const __be16 *g = (const __be16 *)guid;
537 return snprintf(buf, size, "%04x:%04x:%04x:%04x",
538 be16_to_cpu(g[0]), be16_to_cpu(g[1]),
539 be16_to_cpu(g[2]), be16_to_cpu(g[3]));
543 * srpt_refresh_port - configure a HCA port
544 * @sport: SRPT HCA port.
546 * Enable InfiniBand management datagram processing, update the cached sm_lid,
547 * lid and gid values, and register a callback function for processing MADs
548 * on the specified port.
550 * Note: It is safe to call this function more than once for the same port.
552 static int srpt_refresh_port(struct srpt_port *sport)
554 struct ib_mad_reg_req reg_req;
555 struct ib_port_modify port_modify;
556 struct ib_port_attr port_attr;
559 memset(&port_modify, 0, sizeof(port_modify));
560 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
561 port_modify.clr_port_cap_mask = 0;
563 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
567 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
571 sport->sm_lid = port_attr.sm_lid;
572 sport->lid = port_attr.lid;
574 ret = rdma_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
578 sport->port_guid_wwn.priv = sport;
579 srpt_format_guid(sport->port_guid, sizeof(sport->port_guid),
580 &sport->gid.global.interface_id);
581 sport->port_gid_wwn.priv = sport;
582 snprintf(sport->port_gid, sizeof(sport->port_gid),
584 be64_to_cpu(sport->gid.global.subnet_prefix),
585 be64_to_cpu(sport->gid.global.interface_id));
587 if (!sport->mad_agent) {
588 memset(®_req, 0, sizeof(reg_req));
589 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
590 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
591 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
592 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
594 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
598 srpt_mad_send_handler,
599 srpt_mad_recv_handler,
601 if (IS_ERR(sport->mad_agent)) {
602 ret = PTR_ERR(sport->mad_agent);
603 sport->mad_agent = NULL;
612 port_modify.set_port_cap_mask = 0;
613 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
614 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
622 * srpt_unregister_mad_agent - unregister MAD callback functions
623 * @sdev: SRPT HCA pointer.
625 * Note: It is safe to call this function more than once for the same device.
627 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
629 struct ib_port_modify port_modify = {
630 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
632 struct srpt_port *sport;
635 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
636 sport = &sdev->port[i - 1];
637 WARN_ON(sport->port != i);
638 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
639 pr_err("disabling MAD processing failed.\n");
640 if (sport->mad_agent) {
641 ib_unregister_mad_agent(sport->mad_agent);
642 sport->mad_agent = NULL;
648 * srpt_alloc_ioctx - allocate a SRPT I/O context structure
649 * @sdev: SRPT HCA pointer.
650 * @ioctx_size: I/O context size.
651 * @buf_cache: I/O buffer cache.
652 * @dir: DMA data direction.
654 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
656 struct kmem_cache *buf_cache,
657 enum dma_data_direction dir)
659 struct srpt_ioctx *ioctx;
661 ioctx = kzalloc(ioctx_size, GFP_KERNEL);
665 ioctx->buf = kmem_cache_alloc(buf_cache, GFP_KERNEL);
669 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf,
670 kmem_cache_size(buf_cache), dir);
671 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
677 kmem_cache_free(buf_cache, ioctx->buf);
685 * srpt_free_ioctx - free a SRPT I/O context structure
686 * @sdev: SRPT HCA pointer.
687 * @ioctx: I/O context pointer.
688 * @buf_cache: I/O buffer cache.
689 * @dir: DMA data direction.
691 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
692 struct kmem_cache *buf_cache,
693 enum dma_data_direction dir)
698 ib_dma_unmap_single(sdev->device, ioctx->dma,
699 kmem_cache_size(buf_cache), dir);
700 kmem_cache_free(buf_cache, ioctx->buf);
705 * srpt_alloc_ioctx_ring - allocate a ring of SRPT I/O context structures
706 * @sdev: Device to allocate the I/O context ring for.
707 * @ring_size: Number of elements in the I/O context ring.
708 * @ioctx_size: I/O context size.
709 * @buf_cache: I/O buffer cache.
710 * @alignment_offset: Offset in each ring buffer at which the SRP information
712 * @dir: DMA data direction.
714 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
715 int ring_size, int ioctx_size,
716 struct kmem_cache *buf_cache,
717 int alignment_offset,
718 enum dma_data_direction dir)
720 struct srpt_ioctx **ring;
723 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx) &&
724 ioctx_size != sizeof(struct srpt_send_ioctx));
726 ring = kvmalloc_array(ring_size, sizeof(ring[0]), GFP_KERNEL);
729 for (i = 0; i < ring_size; ++i) {
730 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, buf_cache, dir);
734 ring[i]->offset = alignment_offset;
740 srpt_free_ioctx(sdev, ring[i], buf_cache, dir);
748 * srpt_free_ioctx_ring - free the ring of SRPT I/O context structures
749 * @ioctx_ring: I/O context ring to be freed.
750 * @sdev: SRPT HCA pointer.
751 * @ring_size: Number of ring elements.
752 * @buf_cache: I/O buffer cache.
753 * @dir: DMA data direction.
755 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
756 struct srpt_device *sdev, int ring_size,
757 struct kmem_cache *buf_cache,
758 enum dma_data_direction dir)
765 for (i = 0; i < ring_size; ++i)
766 srpt_free_ioctx(sdev, ioctx_ring[i], buf_cache, dir);
771 * srpt_set_cmd_state - set the state of a SCSI command
772 * @ioctx: Send I/O context.
773 * @new: New I/O context state.
775 * Does not modify the state of aborted commands. Returns the previous command
778 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
779 enum srpt_command_state new)
781 enum srpt_command_state previous;
783 previous = ioctx->state;
784 if (previous != SRPT_STATE_DONE)
791 * srpt_test_and_set_cmd_state - test and set the state of a command
792 * @ioctx: Send I/O context.
793 * @old: Current I/O context state.
794 * @new: New I/O context state.
796 * Returns true if and only if the previous command state was equal to 'old'.
798 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
799 enum srpt_command_state old,
800 enum srpt_command_state new)
802 enum srpt_command_state previous;
805 WARN_ON(old == SRPT_STATE_DONE);
806 WARN_ON(new == SRPT_STATE_NEW);
808 previous = ioctx->state;
812 return previous == old;
816 * srpt_post_recv - post an IB receive request
817 * @sdev: SRPT HCA pointer.
818 * @ch: SRPT RDMA channel.
819 * @ioctx: Receive I/O context pointer.
821 static int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch,
822 struct srpt_recv_ioctx *ioctx)
825 struct ib_recv_wr wr;
828 list.addr = ioctx->ioctx.dma + ioctx->ioctx.offset;
829 list.length = srp_max_req_size;
830 list.lkey = sdev->lkey;
832 ioctx->ioctx.cqe.done = srpt_recv_done;
833 wr.wr_cqe = &ioctx->ioctx.cqe;
839 return ib_post_srq_recv(sdev->srq, &wr, NULL);
841 return ib_post_recv(ch->qp, &wr, NULL);
845 * srpt_zerolength_write - perform a zero-length RDMA write
846 * @ch: SRPT RDMA channel.
848 * A quote from the InfiniBand specification: C9-88: For an HCA responder
849 * using Reliable Connection service, for each zero-length RDMA READ or WRITE
850 * request, the R_Key shall not be validated, even if the request includes
853 static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
855 struct ib_rdma_wr wr = {
858 { .wr_cqe = &ch->zw_cqe, },
859 .opcode = IB_WR_RDMA_WRITE,
860 .send_flags = IB_SEND_SIGNALED,
864 pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
867 return ib_post_send(ch->qp, &wr.wr, NULL);
870 static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
872 struct srpt_rdma_ch *ch = cq->cq_context;
874 pr_debug("%s-%d wc->status %d\n", ch->sess_name, ch->qp->qp_num,
877 if (wc->status == IB_WC_SUCCESS) {
878 srpt_process_wait_list(ch);
880 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
881 schedule_work(&ch->release_work);
883 pr_debug("%s-%d: already disconnected.\n",
884 ch->sess_name, ch->qp->qp_num);
888 static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
889 struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
892 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
893 struct srpt_rdma_ch *ch = ioctx->ch;
894 struct scatterlist *prev = NULL;
899 ioctx->rw_ctxs = &ioctx->s_rw_ctx;
901 ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
907 for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
908 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
909 u64 remote_addr = be64_to_cpu(db->va);
910 u32 size = be32_to_cpu(db->len);
911 u32 rkey = be32_to_cpu(db->key);
913 ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
918 ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
919 ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
921 target_free_sgl(ctx->sg, ctx->nents);
925 ioctx->n_rdma += ret;
929 sg_unmark_end(&prev[prev_nents - 1]);
930 sg_chain(prev, prev_nents + 1, ctx->sg);
936 prev_nents = ctx->nents;
938 *sg_cnt += ctx->nents;
945 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
947 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
948 ctx->sg, ctx->nents, dir);
949 target_free_sgl(ctx->sg, ctx->nents);
951 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
952 kfree(ioctx->rw_ctxs);
956 static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
957 struct srpt_send_ioctx *ioctx)
959 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
962 for (i = 0; i < ioctx->n_rw_ctx; i++) {
963 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
965 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
966 ctx->sg, ctx->nents, dir);
967 target_free_sgl(ctx->sg, ctx->nents);
970 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
971 kfree(ioctx->rw_ctxs);
974 static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
977 * The pointer computations below will only be compiled correctly
978 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
979 * whether srp_cmd::add_data has been declared as a byte pointer.
981 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
982 !__same_type(srp_cmd->add_data[0], (u8)0));
985 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
986 * CDB LENGTH' field are reserved and the size in bytes of this field
987 * is four times the value specified in bits 3..7. Hence the "& ~3".
989 return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
993 * srpt_get_desc_tbl - parse the data descriptors of a SRP_CMD request
994 * @recv_ioctx: I/O context associated with the received command @srp_cmd.
995 * @ioctx: I/O context that will be used for responding to the initiator.
996 * @srp_cmd: Pointer to the SRP_CMD request data.
997 * @dir: Pointer to the variable to which the transfer direction will be
999 * @sg: [out] scatterlist for the parsed SRP_CMD.
1000 * @sg_cnt: [out] length of @sg.
1001 * @data_len: Pointer to the variable to which the total data length of all
1002 * descriptors in the SRP_CMD request will be written.
1003 * @imm_data_offset: [in] Offset in SRP_CMD requests at which immediate data
1006 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
1008 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
1009 * -ENOMEM when memory allocation fails and zero upon success.
1011 static int srpt_get_desc_tbl(struct srpt_recv_ioctx *recv_ioctx,
1012 struct srpt_send_ioctx *ioctx,
1013 struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
1014 struct scatterlist **sg, unsigned int *sg_cnt, u64 *data_len,
1015 u16 imm_data_offset)
1021 * The lower four bits of the buffer format field contain the DATA-IN
1022 * buffer descriptor format, and the highest four bits contain the
1023 * DATA-OUT buffer descriptor format.
1025 if (srp_cmd->buf_fmt & 0xf)
1026 /* DATA-IN: transfer data from target to initiator (read). */
1027 *dir = DMA_FROM_DEVICE;
1028 else if (srp_cmd->buf_fmt >> 4)
1029 /* DATA-OUT: transfer data from initiator to target (write). */
1030 *dir = DMA_TO_DEVICE;
1034 /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
1035 ioctx->cmd.data_direction = *dir;
1037 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
1038 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
1039 struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
1041 *data_len = be32_to_cpu(db->len);
1042 return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
1043 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
1044 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
1045 struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
1046 int nbufs = be32_to_cpu(idb->table_desc.len) /
1047 sizeof(struct srp_direct_buf);
1050 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
1051 pr_err("received unsupported SRP_CMD request type (%u out + %u in != %u / %zu)\n",
1052 srp_cmd->data_out_desc_cnt,
1053 srp_cmd->data_in_desc_cnt,
1054 be32_to_cpu(idb->table_desc.len),
1055 sizeof(struct srp_direct_buf));
1059 *data_len = be32_to_cpu(idb->len);
1060 return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
1062 } else if ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_IMM) {
1063 struct srp_imm_buf *imm_buf = srpt_get_desc_buf(srp_cmd);
1064 void *data = (void *)srp_cmd + imm_data_offset;
1065 uint32_t len = be32_to_cpu(imm_buf->len);
1066 uint32_t req_size = imm_data_offset + len;
1068 if (req_size > srp_max_req_size) {
1069 pr_err("Immediate data (length %d + %d) exceeds request size %d\n",
1070 imm_data_offset, len, srp_max_req_size);
1073 if (recv_ioctx->byte_len < req_size) {
1074 pr_err("Received too few data - %d < %d\n",
1075 recv_ioctx->byte_len, req_size);
1079 * The immediate data buffer descriptor must occur before the
1080 * immediate data itself.
1082 if ((void *)(imm_buf + 1) > (void *)data) {
1083 pr_err("Received invalid write request\n");
1087 ioctx->recv_ioctx = recv_ioctx;
1088 if ((uintptr_t)data & 511) {
1089 pr_warn_once("Internal error - the receive buffers are not aligned properly.\n");
1092 sg_init_one(&ioctx->imm_sg, data, len);
1093 *sg = &ioctx->imm_sg;
1103 * srpt_init_ch_qp - initialize queue pair attributes
1104 * @ch: SRPT RDMA channel.
1105 * @qp: Queue pair pointer.
1107 * Initialized the attributes of queue pair 'qp' by allowing local write,
1108 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
1110 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1112 struct ib_qp_attr *attr;
1115 WARN_ON_ONCE(ch->using_rdma_cm);
1117 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1121 attr->qp_state = IB_QPS_INIT;
1122 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1123 attr->port_num = ch->sport->port;
1125 ret = ib_find_cached_pkey(ch->sport->sdev->device, ch->sport->port,
1126 ch->pkey, &attr->pkey_index);
1128 pr_err("Translating pkey %#x failed (%d) - using index 0\n",
1131 ret = ib_modify_qp(qp, attr,
1132 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
1140 * srpt_ch_qp_rtr - change the state of a channel to 'ready to receive' (RTR)
1141 * @ch: channel of the queue pair.
1142 * @qp: queue pair to change the state of.
1144 * Returns zero upon success and a negative value upon failure.
1146 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1147 * If this structure ever becomes larger, it might be necessary to allocate
1148 * it dynamically instead of on the stack.
1150 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1152 struct ib_qp_attr qp_attr;
1156 WARN_ON_ONCE(ch->using_rdma_cm);
1158 qp_attr.qp_state = IB_QPS_RTR;
1159 ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
1163 qp_attr.max_dest_rd_atomic = 4;
1165 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1172 * srpt_ch_qp_rts - change the state of a channel to 'ready to send' (RTS)
1173 * @ch: channel of the queue pair.
1174 * @qp: queue pair to change the state of.
1176 * Returns zero upon success and a negative value upon failure.
1178 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1179 * If this structure ever becomes larger, it might be necessary to allocate
1180 * it dynamically instead of on the stack.
1182 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1184 struct ib_qp_attr qp_attr;
1188 qp_attr.qp_state = IB_QPS_RTS;
1189 ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
1193 qp_attr.max_rd_atomic = 4;
1195 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1202 * srpt_ch_qp_err - set the channel queue pair state to 'error'
1203 * @ch: SRPT RDMA channel.
1205 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1207 struct ib_qp_attr qp_attr;
1209 qp_attr.qp_state = IB_QPS_ERR;
1210 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1214 * srpt_get_send_ioctx - obtain an I/O context for sending to the initiator
1215 * @ch: SRPT RDMA channel.
1217 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1219 struct srpt_send_ioctx *ioctx;
1224 tag = sbitmap_queue_get(&ch->sess->sess_tag_pool, &cpu);
1228 ioctx = ch->ioctx_ring[tag];
1229 BUG_ON(ioctx->ch != ch);
1230 ioctx->state = SRPT_STATE_NEW;
1231 WARN_ON_ONCE(ioctx->recv_ioctx);
1233 ioctx->n_rw_ctx = 0;
1234 ioctx->queue_status_only = false;
1236 * transport_init_se_cmd() does not initialize all fields, so do it
1239 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1240 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1241 ioctx->cmd.map_tag = tag;
1242 ioctx->cmd.map_cpu = cpu;
1248 * srpt_abort_cmd - abort a SCSI command
1249 * @ioctx: I/O context associated with the SCSI command.
1251 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1253 enum srpt_command_state state;
1258 * If the command is in a state where the target core is waiting for
1259 * the ib_srpt driver, change the state to the next state.
1262 state = ioctx->state;
1264 case SRPT_STATE_NEED_DATA:
1265 ioctx->state = SRPT_STATE_DATA_IN;
1267 case SRPT_STATE_CMD_RSP_SENT:
1268 case SRPT_STATE_MGMT_RSP_SENT:
1269 ioctx->state = SRPT_STATE_DONE;
1272 WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
1277 pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state,
1278 ioctx->state, ioctx->cmd.tag);
1281 case SRPT_STATE_NEW:
1282 case SRPT_STATE_DATA_IN:
1283 case SRPT_STATE_MGMT:
1284 case SRPT_STATE_DONE:
1286 * Do nothing - defer abort processing until
1287 * srpt_queue_response() is invoked.
1290 case SRPT_STATE_NEED_DATA:
1291 pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
1292 transport_generic_request_failure(&ioctx->cmd,
1293 TCM_CHECK_CONDITION_ABORT_CMD);
1295 case SRPT_STATE_CMD_RSP_SENT:
1297 * SRP_RSP sending failed or the SRP_RSP send completion has
1298 * not been received in time.
1300 transport_generic_free_cmd(&ioctx->cmd, 0);
1302 case SRPT_STATE_MGMT_RSP_SENT:
1303 transport_generic_free_cmd(&ioctx->cmd, 0);
1306 WARN(1, "Unexpected command state (%d)", state);
1314 * srpt_rdma_read_done - RDMA read completion callback
1315 * @cq: Completion queue.
1316 * @wc: Work completion.
1318 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1319 * the data that has been transferred via IB RDMA had to be postponed until the
1320 * check_stop_free() callback. None of this is necessary anymore and needs to
1323 static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1325 struct srpt_rdma_ch *ch = cq->cq_context;
1326 struct srpt_send_ioctx *ioctx =
1327 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
1329 WARN_ON(ioctx->n_rdma <= 0);
1330 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1333 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1334 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1336 srpt_abort_cmd(ioctx);
1340 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1341 SRPT_STATE_DATA_IN))
1342 target_execute_cmd(&ioctx->cmd);
1344 pr_err("%s[%d]: wrong state = %d\n", __func__,
1345 __LINE__, ioctx->state);
1349 * srpt_build_cmd_rsp - build a SRP_RSP response
1350 * @ch: RDMA channel through which the request has been received.
1351 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1352 * be built in the buffer ioctx->buf points at and hence this function will
1353 * overwrite the request data.
1354 * @tag: tag of the request for which this response is being generated.
1355 * @status: value for the STATUS field of the SRP_RSP information unit.
1357 * Returns the size in bytes of the SRP_RSP response.
1359 * An SRP_RSP response contains a SCSI status or service response. See also
1360 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1361 * response. See also SPC-2 for more information about sense data.
1363 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1364 struct srpt_send_ioctx *ioctx, u64 tag,
1367 struct srp_rsp *srp_rsp;
1368 const u8 *sense_data;
1369 int sense_data_len, max_sense_len;
1372 * The lowest bit of all SAM-3 status codes is zero (see also
1373 * paragraph 5.3 in SAM-3).
1375 WARN_ON(status & 1);
1377 srp_rsp = ioctx->ioctx.buf;
1380 sense_data = ioctx->sense_data;
1381 sense_data_len = ioctx->cmd.scsi_sense_length;
1382 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1384 memset(srp_rsp, 0, sizeof(*srp_rsp));
1385 srp_rsp->opcode = SRP_RSP;
1386 srp_rsp->req_lim_delta =
1387 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1389 srp_rsp->status = status;
1391 if (sense_data_len) {
1392 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1393 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1394 if (sense_data_len > max_sense_len) {
1395 pr_warn("truncated sense data from %d to %d bytes\n",
1396 sense_data_len, max_sense_len);
1397 sense_data_len = max_sense_len;
1400 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1401 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1402 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1405 return sizeof(*srp_rsp) + sense_data_len;
1409 * srpt_build_tskmgmt_rsp - build a task management response
1410 * @ch: RDMA channel through which the request has been received.
1411 * @ioctx: I/O context in which the SRP_RSP response will be built.
1412 * @rsp_code: RSP_CODE that will be stored in the response.
1413 * @tag: Tag of the request for which this response is being generated.
1415 * Returns the size in bytes of the SRP_RSP response.
1417 * An SRP_RSP response contains a SCSI status or service response. See also
1418 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1421 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1422 struct srpt_send_ioctx *ioctx,
1423 u8 rsp_code, u64 tag)
1425 struct srp_rsp *srp_rsp;
1430 resp_len = sizeof(*srp_rsp) + resp_data_len;
1432 srp_rsp = ioctx->ioctx.buf;
1434 memset(srp_rsp, 0, sizeof(*srp_rsp));
1436 srp_rsp->opcode = SRP_RSP;
1437 srp_rsp->req_lim_delta =
1438 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1441 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1442 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1443 srp_rsp->data[3] = rsp_code;
1448 static int srpt_check_stop_free(struct se_cmd *cmd)
1450 struct srpt_send_ioctx *ioctx = container_of(cmd,
1451 struct srpt_send_ioctx, cmd);
1453 return target_put_sess_cmd(&ioctx->cmd);
1457 * srpt_handle_cmd - process a SRP_CMD information unit
1458 * @ch: SRPT RDMA channel.
1459 * @recv_ioctx: Receive I/O context.
1460 * @send_ioctx: Send I/O context.
1462 static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1463 struct srpt_recv_ioctx *recv_ioctx,
1464 struct srpt_send_ioctx *send_ioctx)
1467 struct srp_cmd *srp_cmd;
1468 struct scatterlist *sg = NULL;
1469 unsigned sg_cnt = 0;
1471 enum dma_data_direction dir;
1474 BUG_ON(!send_ioctx);
1476 srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset;
1477 cmd = &send_ioctx->cmd;
1478 cmd->tag = srp_cmd->tag;
1480 switch (srp_cmd->task_attr) {
1481 case SRP_CMD_SIMPLE_Q:
1482 cmd->sam_task_attr = TCM_SIMPLE_TAG;
1484 case SRP_CMD_ORDERED_Q:
1486 cmd->sam_task_attr = TCM_ORDERED_TAG;
1488 case SRP_CMD_HEAD_OF_Q:
1489 cmd->sam_task_attr = TCM_HEAD_TAG;
1492 cmd->sam_task_attr = TCM_ACA_TAG;
1496 rc = srpt_get_desc_tbl(recv_ioctx, send_ioctx, srp_cmd, &dir,
1497 &sg, &sg_cnt, &data_len, ch->imm_data_offset);
1499 if (rc != -EAGAIN) {
1500 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1506 rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
1507 &send_ioctx->sense_data[0],
1508 scsilun_to_int(&srp_cmd->lun), data_len,
1509 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
1510 sg, sg_cnt, NULL, 0, NULL, 0);
1512 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1519 target_send_busy(cmd);
1522 static int srp_tmr_to_tcm(int fn)
1525 case SRP_TSK_ABORT_TASK:
1526 return TMR_ABORT_TASK;
1527 case SRP_TSK_ABORT_TASK_SET:
1528 return TMR_ABORT_TASK_SET;
1529 case SRP_TSK_CLEAR_TASK_SET:
1530 return TMR_CLEAR_TASK_SET;
1531 case SRP_TSK_LUN_RESET:
1532 return TMR_LUN_RESET;
1533 case SRP_TSK_CLEAR_ACA:
1534 return TMR_CLEAR_ACA;
1541 * srpt_handle_tsk_mgmt - process a SRP_TSK_MGMT information unit
1542 * @ch: SRPT RDMA channel.
1543 * @recv_ioctx: Receive I/O context.
1544 * @send_ioctx: Send I/O context.
1546 * Returns 0 if and only if the request will be processed by the target core.
1548 * For more information about SRP_TSK_MGMT information units, see also section
1549 * 6.7 in the SRP r16a document.
1551 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1552 struct srpt_recv_ioctx *recv_ioctx,
1553 struct srpt_send_ioctx *send_ioctx)
1555 struct srp_tsk_mgmt *srp_tsk;
1557 struct se_session *sess = ch->sess;
1561 BUG_ON(!send_ioctx);
1563 srp_tsk = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset;
1564 cmd = &send_ioctx->cmd;
1566 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld ch %p sess %p\n",
1567 srp_tsk->tsk_mgmt_func, srp_tsk->task_tag, srp_tsk->tag, ch,
1570 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1571 send_ioctx->cmd.tag = srp_tsk->tag;
1572 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1573 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1574 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1575 GFP_KERNEL, srp_tsk->task_tag,
1576 TARGET_SCF_ACK_KREF);
1578 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1579 cmd->se_tfo->queue_tm_rsp(cmd);
1585 * srpt_handle_new_iu - process a newly received information unit
1586 * @ch: RDMA channel through which the information unit has been received.
1587 * @recv_ioctx: Receive I/O context associated with the information unit.
1590 srpt_handle_new_iu(struct srpt_rdma_ch *ch, struct srpt_recv_ioctx *recv_ioctx)
1592 struct srpt_send_ioctx *send_ioctx = NULL;
1593 struct srp_cmd *srp_cmd;
1598 BUG_ON(!recv_ioctx);
1600 if (unlikely(ch->state == CH_CONNECTING))
1603 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1604 recv_ioctx->ioctx.dma,
1605 recv_ioctx->ioctx.offset + srp_max_req_size,
1608 srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset;
1609 opcode = srp_cmd->opcode;
1610 if (opcode == SRP_CMD || opcode == SRP_TSK_MGMT) {
1611 send_ioctx = srpt_get_send_ioctx(ch);
1612 if (unlikely(!send_ioctx))
1616 if (!list_empty(&recv_ioctx->wait_list)) {
1617 WARN_ON_ONCE(!ch->processing_wait_list);
1618 list_del_init(&recv_ioctx->wait_list);
1623 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1626 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1629 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
1632 pr_debug("received SRP_CRED_RSP\n");
1635 pr_debug("received SRP_AER_RSP\n");
1638 pr_err("Received SRP_RSP\n");
1641 pr_err("received IU with unknown opcode 0x%x\n", opcode);
1645 if (!send_ioctx || !send_ioctx->recv_ioctx)
1646 srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
1653 if (list_empty(&recv_ioctx->wait_list)) {
1654 WARN_ON_ONCE(ch->processing_wait_list);
1655 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1660 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1662 struct srpt_rdma_ch *ch = cq->cq_context;
1663 struct srpt_recv_ioctx *ioctx =
1664 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
1666 if (wc->status == IB_WC_SUCCESS) {
1669 req_lim = atomic_dec_return(&ch->req_lim);
1670 if (unlikely(req_lim < 0))
1671 pr_err("req_lim = %d < 0\n", req_lim);
1672 ioctx->byte_len = wc->byte_len;
1673 srpt_handle_new_iu(ch, ioctx);
1675 pr_info_ratelimited("receiving failed for ioctx %p with status %d\n",
1681 * This function must be called from the context in which RDMA completions are
1682 * processed because it accesses the wait list without protection against
1683 * access from other threads.
1685 static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
1687 struct srpt_recv_ioctx *recv_ioctx, *tmp;
1689 WARN_ON_ONCE(ch->state == CH_CONNECTING);
1691 if (list_empty(&ch->cmd_wait_list))
1694 WARN_ON_ONCE(ch->processing_wait_list);
1695 ch->processing_wait_list = true;
1696 list_for_each_entry_safe(recv_ioctx, tmp, &ch->cmd_wait_list,
1698 if (!srpt_handle_new_iu(ch, recv_ioctx))
1701 ch->processing_wait_list = false;
1705 * srpt_send_done - send completion callback
1706 * @cq: Completion queue.
1707 * @wc: Work completion.
1709 * Note: Although this has not yet been observed during tests, at least in
1710 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1711 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1712 * value in each response is set to one, and it is possible that this response
1713 * makes the initiator send a new request before the send completion for that
1714 * response has been processed. This could e.g. happen if the call to
1715 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1716 * if IB retransmission causes generation of the send completion to be
1717 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1718 * are queued on cmd_wait_list. The code below processes these delayed
1719 * requests one at a time.
1721 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
1723 struct srpt_rdma_ch *ch = cq->cq_context;
1724 struct srpt_send_ioctx *ioctx =
1725 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1726 enum srpt_command_state state;
1728 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1730 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1731 state != SRPT_STATE_MGMT_RSP_SENT);
1733 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
1735 if (wc->status != IB_WC_SUCCESS)
1736 pr_info("sending response for ioctx 0x%p failed with status %d\n",
1739 if (state != SRPT_STATE_DONE) {
1740 transport_generic_free_cmd(&ioctx->cmd, 0);
1742 pr_err("IB completion has been received too late for wr_id = %u.\n",
1743 ioctx->ioctx.index);
1746 srpt_process_wait_list(ch);
1750 * srpt_create_ch_ib - create receive and send completion queues
1751 * @ch: SRPT RDMA channel.
1753 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1755 struct ib_qp_init_attr *qp_init;
1756 struct srpt_port *sport = ch->sport;
1757 struct srpt_device *sdev = sport->sdev;
1758 const struct ib_device_attr *attrs = &sdev->device->attrs;
1759 int sq_size = sport->port_attrib.srp_sq_size;
1762 WARN_ON(ch->rq_size < 1);
1765 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
1770 ch->cq = ib_alloc_cq_any(sdev->device, ch, ch->rq_size + sq_size,
1772 if (IS_ERR(ch->cq)) {
1773 ret = PTR_ERR(ch->cq);
1774 pr_err("failed to create CQ cqe= %d ret= %d\n",
1775 ch->rq_size + sq_size, ret);
1779 qp_init->qp_context = (void *)ch;
1780 qp_init->event_handler
1781 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1782 qp_init->send_cq = ch->cq;
1783 qp_init->recv_cq = ch->cq;
1784 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1785 qp_init->qp_type = IB_QPT_RC;
1787 * We divide up our send queue size into half SEND WRs to send the
1788 * completions, and half R/W contexts to actually do the RDMA
1789 * READ/WRITE transfers. Note that we need to allocate CQ slots for
1790 * both both, as RDMA contexts will also post completions for the
1793 qp_init->cap.max_send_wr = min(sq_size / 2, attrs->max_qp_wr);
1794 qp_init->cap.max_rdma_ctxs = sq_size / 2;
1795 qp_init->cap.max_send_sge = min(attrs->max_send_sge,
1796 SRPT_MAX_SG_PER_WQE);
1797 qp_init->cap.max_recv_sge = min(attrs->max_recv_sge,
1798 SRPT_MAX_SG_PER_WQE);
1799 qp_init->port_num = ch->sport->port;
1800 if (sdev->use_srq) {
1801 qp_init->srq = sdev->srq;
1803 qp_init->cap.max_recv_wr = ch->rq_size;
1804 qp_init->cap.max_recv_sge = min(attrs->max_recv_sge,
1805 SRPT_MAX_SG_PER_WQE);
1808 if (ch->using_rdma_cm) {
1809 ret = rdma_create_qp(ch->rdma_cm.cm_id, sdev->pd, qp_init);
1810 ch->qp = ch->rdma_cm.cm_id->qp;
1812 ch->qp = ib_create_qp(sdev->pd, qp_init);
1813 if (!IS_ERR(ch->qp)) {
1814 ret = srpt_init_ch_qp(ch, ch->qp);
1816 ib_destroy_qp(ch->qp);
1818 ret = PTR_ERR(ch->qp);
1822 bool retry = sq_size > MIN_SRPT_SQ_SIZE;
1825 pr_debug("failed to create queue pair with sq_size = %d (%d) - retrying\n",
1828 sq_size = max(sq_size / 2, MIN_SRPT_SQ_SIZE);
1831 pr_err("failed to create queue pair with sq_size = %d (%d)\n",
1833 goto err_destroy_cq;
1837 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1839 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d ch= %p\n",
1840 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1841 qp_init->cap.max_send_wr, ch);
1844 for (i = 0; i < ch->rq_size; i++)
1845 srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]);
1857 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1859 ib_destroy_qp(ch->qp);
1864 * srpt_close_ch - close a RDMA channel
1865 * @ch: SRPT RDMA channel.
1867 * Make sure all resources associated with the channel will be deallocated at
1868 * an appropriate time.
1870 * Returns true if and only if the channel state has been modified into
1873 static bool srpt_close_ch(struct srpt_rdma_ch *ch)
1877 if (!srpt_set_ch_state(ch, CH_DRAINING)) {
1878 pr_debug("%s: already closed\n", ch->sess_name);
1882 kref_get(&ch->kref);
1884 ret = srpt_ch_qp_err(ch);
1886 pr_err("%s-%d: changing queue pair into error state failed: %d\n",
1887 ch->sess_name, ch->qp->qp_num, ret);
1889 ret = srpt_zerolength_write(ch);
1891 pr_err("%s-%d: queuing zero-length write failed: %d\n",
1892 ch->sess_name, ch->qp->qp_num, ret);
1893 if (srpt_set_ch_state(ch, CH_DISCONNECTED))
1894 schedule_work(&ch->release_work);
1899 kref_put(&ch->kref, srpt_free_ch);
1905 * Change the channel state into CH_DISCONNECTING. If a channel has not yet
1906 * reached the connected state, close it. If a channel is in the connected
1907 * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
1908 * the responsibility of the caller to ensure that this function is not
1909 * invoked concurrently with the code that accepts a connection. This means
1910 * that this function must either be invoked from inside a CM callback
1911 * function or that it must be invoked with the srpt_port.mutex held.
1913 static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
1917 if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
1920 if (ch->using_rdma_cm) {
1921 ret = rdma_disconnect(ch->rdma_cm.cm_id);
1923 ret = ib_send_cm_dreq(ch->ib_cm.cm_id, NULL, 0);
1925 ret = ib_send_cm_drep(ch->ib_cm.cm_id, NULL, 0);
1928 if (ret < 0 && srpt_close_ch(ch))
1934 static bool srpt_ch_closed(struct srpt_port *sport, struct srpt_rdma_ch *ch)
1936 struct srpt_nexus *nexus;
1937 struct srpt_rdma_ch *ch2;
1941 list_for_each_entry(nexus, &sport->nexus_list, entry) {
1942 list_for_each_entry(ch2, &nexus->ch_list, list) {
1955 /* Send DREQ and wait for DREP. */
1956 static void srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch)
1958 struct srpt_port *sport = ch->sport;
1960 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
1963 mutex_lock(&sport->mutex);
1964 srpt_disconnect_ch(ch);
1965 mutex_unlock(&sport->mutex);
1967 while (wait_event_timeout(sport->ch_releaseQ, srpt_ch_closed(sport, ch),
1969 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
1970 ch->sess_name, ch->qp->qp_num, ch->state);
1974 static void __srpt_close_all_ch(struct srpt_port *sport)
1976 struct srpt_nexus *nexus;
1977 struct srpt_rdma_ch *ch;
1979 lockdep_assert_held(&sport->mutex);
1981 list_for_each_entry(nexus, &sport->nexus_list, entry) {
1982 list_for_each_entry(ch, &nexus->ch_list, list) {
1983 if (srpt_disconnect_ch(ch) >= 0)
1984 pr_info("Closing channel %s because target %s_%d has been disabled\n",
1986 dev_name(&sport->sdev->device->dev),
1994 * Look up (i_port_id, t_port_id) in sport->nexus_list. Create an entry if
1995 * it does not yet exist.
1997 static struct srpt_nexus *srpt_get_nexus(struct srpt_port *sport,
1998 const u8 i_port_id[16],
1999 const u8 t_port_id[16])
2001 struct srpt_nexus *nexus = NULL, *tmp_nexus = NULL, *n;
2004 mutex_lock(&sport->mutex);
2005 list_for_each_entry(n, &sport->nexus_list, entry) {
2006 if (memcmp(n->i_port_id, i_port_id, 16) == 0 &&
2007 memcmp(n->t_port_id, t_port_id, 16) == 0) {
2012 if (!nexus && tmp_nexus) {
2013 list_add_tail_rcu(&tmp_nexus->entry,
2014 &sport->nexus_list);
2015 swap(nexus, tmp_nexus);
2017 mutex_unlock(&sport->mutex);
2021 tmp_nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
2023 nexus = ERR_PTR(-ENOMEM);
2026 INIT_LIST_HEAD(&tmp_nexus->ch_list);
2027 memcpy(tmp_nexus->i_port_id, i_port_id, 16);
2028 memcpy(tmp_nexus->t_port_id, t_port_id, 16);
2036 static void srpt_set_enabled(struct srpt_port *sport, bool enabled)
2037 __must_hold(&sport->mutex)
2039 lockdep_assert_held(&sport->mutex);
2041 if (sport->enabled == enabled)
2043 sport->enabled = enabled;
2045 __srpt_close_all_ch(sport);
2048 static void srpt_free_ch(struct kref *kref)
2050 struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
2056 * Shut down the SCSI target session, tell the connection manager to
2057 * disconnect the associated RDMA channel, transition the QP to the error
2058 * state and remove the channel from the channel list. This function is
2059 * typically called from inside srpt_zerolength_write_done(). Concurrent
2060 * srpt_zerolength_write() calls from inside srpt_close_ch() are possible
2061 * as long as the channel is on sport->nexus_list.
2063 static void srpt_release_channel_work(struct work_struct *w)
2065 struct srpt_rdma_ch *ch;
2066 struct srpt_device *sdev;
2067 struct srpt_port *sport;
2068 struct se_session *se_sess;
2070 ch = container_of(w, struct srpt_rdma_ch, release_work);
2071 pr_debug("%s-%d\n", ch->sess_name, ch->qp->qp_num);
2073 sdev = ch->sport->sdev;
2079 target_sess_cmd_list_set_waiting(se_sess);
2080 target_wait_for_sess_cmds(se_sess);
2082 target_remove_session(se_sess);
2085 if (ch->using_rdma_cm)
2086 rdma_destroy_id(ch->rdma_cm.cm_id);
2088 ib_destroy_cm_id(ch->ib_cm.cm_id);
2091 mutex_lock(&sport->mutex);
2092 list_del_rcu(&ch->list);
2093 mutex_unlock(&sport->mutex);
2095 srpt_destroy_ch_ib(ch);
2097 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2098 ch->sport->sdev, ch->rq_size,
2099 ch->rsp_buf_cache, DMA_TO_DEVICE);
2101 kmem_cache_destroy(ch->rsp_buf_cache);
2103 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2105 ch->req_buf_cache, DMA_FROM_DEVICE);
2107 kmem_cache_destroy(ch->req_buf_cache);
2109 wake_up(&sport->ch_releaseQ);
2111 kref_put(&ch->kref, srpt_free_ch);
2115 * srpt_cm_req_recv - process the event IB_CM_REQ_RECEIVED
2116 * @sdev: HCA through which the login request was received.
2117 * @ib_cm_id: IB/CM connection identifier in case of IB/CM.
2118 * @rdma_cm_id: RDMA/CM connection identifier in case of RDMA/CM.
2119 * @port_num: Port through which the REQ message was received.
2120 * @pkey: P_Key of the incoming connection.
2121 * @req: SRP login request.
2122 * @src_addr: GID (IB/CM) or IP address (RDMA/CM) of the port that submitted
2123 * the login request.
2125 * Ownership of the cm_id is transferred to the target session if this
2126 * function returns zero. Otherwise the caller remains the owner of cm_id.
2128 static int srpt_cm_req_recv(struct srpt_device *const sdev,
2129 struct ib_cm_id *ib_cm_id,
2130 struct rdma_cm_id *rdma_cm_id,
2131 u8 port_num, __be16 pkey,
2132 const struct srp_login_req *req,
2133 const char *src_addr)
2135 struct srpt_port *sport = &sdev->port[port_num - 1];
2136 struct srpt_nexus *nexus;
2137 struct srp_login_rsp *rsp = NULL;
2138 struct srp_login_rej *rej = NULL;
2140 struct rdma_conn_param rdma_cm;
2141 struct ib_cm_rep_param ib_cm;
2142 } *rep_param = NULL;
2143 struct srpt_rdma_ch *ch = NULL;
2146 int i, tag_num, tag_size, ret;
2148 WARN_ON_ONCE(irqs_disabled());
2150 if (WARN_ON(!sdev || !req))
2153 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2155 pr_info("Received SRP_LOGIN_REQ with i_port_id %pI6, t_port_id %pI6 and it_iu_len %d on port %d (guid=%pI6); pkey %#04x\n",
2156 req->initiator_port_id, req->target_port_id, it_iu_len,
2157 port_num, &sport->gid, be16_to_cpu(pkey));
2159 nexus = srpt_get_nexus(sport, req->initiator_port_id,
2160 req->target_port_id);
2161 if (IS_ERR(nexus)) {
2162 ret = PTR_ERR(nexus);
2167 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2168 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2169 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
2170 if (!rsp || !rej || !rep_param)
2174 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2175 rej->reason = cpu_to_be32(
2176 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2177 pr_err("rejected SRP_LOGIN_REQ because its length (%d bytes) is out of range (%d .. %d)\n",
2178 it_iu_len, 64, srp_max_req_size);
2182 if (!sport->enabled) {
2183 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2184 pr_info("rejected SRP_LOGIN_REQ because target port %s_%d has not yet been enabled\n",
2185 dev_name(&sport->sdev->device->dev), port_num);
2189 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2190 || *(__be64 *)(req->target_port_id + 8) !=
2191 cpu_to_be64(srpt_service_guid)) {
2192 rej->reason = cpu_to_be32(
2193 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2194 pr_err("rejected SRP_LOGIN_REQ because it has an invalid target port identifier.\n");
2199 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
2201 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2202 pr_err("rejected SRP_LOGIN_REQ because out of memory.\n");
2206 kref_init(&ch->kref);
2207 ch->pkey = be16_to_cpu(pkey);
2209 ch->zw_cqe.done = srpt_zerolength_write_done;
2210 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2213 ch->ib_cm.cm_id = ib_cm_id;
2214 ib_cm_id->context = ch;
2216 ch->using_rdma_cm = true;
2217 ch->rdma_cm.cm_id = rdma_cm_id;
2218 rdma_cm_id->context = ch;
2221 * ch->rq_size should be at least as large as the initiator queue
2222 * depth to avoid that the initiator driver has to report QUEUE_FULL
2223 * to the SCSI mid-layer.
2225 ch->rq_size = min(MAX_SRPT_RQ_SIZE, sdev->device->attrs.max_qp_wr);
2226 spin_lock_init(&ch->spinlock);
2227 ch->state = CH_CONNECTING;
2228 INIT_LIST_HEAD(&ch->cmd_wait_list);
2229 ch->max_rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2231 ch->rsp_buf_cache = kmem_cache_create("srpt-rsp-buf", ch->max_rsp_size,
2233 if (!ch->rsp_buf_cache)
2236 ch->ioctx_ring = (struct srpt_send_ioctx **)
2237 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2238 sizeof(*ch->ioctx_ring[0]),
2239 ch->rsp_buf_cache, 0, DMA_TO_DEVICE);
2240 if (!ch->ioctx_ring) {
2241 pr_err("rejected SRP_LOGIN_REQ because creating a new QP SQ ring failed.\n");
2242 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2243 goto free_rsp_cache;
2246 for (i = 0; i < ch->rq_size; i++)
2247 ch->ioctx_ring[i]->ch = ch;
2248 if (!sdev->use_srq) {
2249 u16 imm_data_offset = req->req_flags & SRP_IMMED_REQUESTED ?
2250 be16_to_cpu(req->imm_data_offset) : 0;
2251 u16 alignment_offset;
2254 if (req->req_flags & SRP_IMMED_REQUESTED)
2255 pr_debug("imm_data_offset = %d\n",
2256 be16_to_cpu(req->imm_data_offset));
2257 if (imm_data_offset >= sizeof(struct srp_cmd)) {
2258 ch->imm_data_offset = imm_data_offset;
2259 rsp->rsp_flags |= SRP_LOGIN_RSP_IMMED_SUPP;
2261 ch->imm_data_offset = 0;
2263 alignment_offset = round_up(imm_data_offset, 512) -
2265 req_sz = alignment_offset + imm_data_offset + srp_max_req_size;
2266 ch->req_buf_cache = kmem_cache_create("srpt-req-buf", req_sz,
2268 if (!ch->req_buf_cache)
2271 ch->ioctx_recv_ring = (struct srpt_recv_ioctx **)
2272 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2273 sizeof(*ch->ioctx_recv_ring[0]),
2277 if (!ch->ioctx_recv_ring) {
2278 pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n");
2280 cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2281 goto free_recv_cache;
2283 for (i = 0; i < ch->rq_size; i++)
2284 INIT_LIST_HEAD(&ch->ioctx_recv_ring[i]->wait_list);
2287 ret = srpt_create_ch_ib(ch);
2289 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2290 pr_err("rejected SRP_LOGIN_REQ because creating a new RDMA channel failed.\n");
2291 goto free_recv_ring;
2294 strlcpy(ch->sess_name, src_addr, sizeof(ch->sess_name));
2295 snprintf(i_port_id, sizeof(i_port_id), "0x%016llx%016llx",
2296 be64_to_cpu(*(__be64 *)nexus->i_port_id),
2297 be64_to_cpu(*(__be64 *)(nexus->i_port_id + 8)));
2299 pr_debug("registering session %s\n", ch->sess_name);
2301 tag_num = ch->rq_size;
2302 tag_size = 1; /* ib_srpt does not use se_sess->sess_cmd_map */
2303 if (sport->port_guid_tpg.se_tpg_wwn)
2304 ch->sess = target_setup_session(&sport->port_guid_tpg, tag_num,
2305 tag_size, TARGET_PROT_NORMAL,
2306 ch->sess_name, ch, NULL);
2307 if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2308 ch->sess = target_setup_session(&sport->port_gid_tpg, tag_num,
2309 tag_size, TARGET_PROT_NORMAL, i_port_id,
2311 /* Retry without leading "0x" */
2312 if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
2313 ch->sess = target_setup_session(&sport->port_gid_tpg, tag_num,
2314 tag_size, TARGET_PROT_NORMAL,
2315 i_port_id + 2, ch, NULL);
2316 if (IS_ERR_OR_NULL(ch->sess)) {
2317 WARN_ON_ONCE(ch->sess == NULL);
2318 ret = PTR_ERR(ch->sess);
2320 pr_info("Rejected login for initiator %s: ret = %d.\n",
2321 ch->sess_name, ret);
2322 rej->reason = cpu_to_be32(ret == -ENOMEM ?
2323 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
2324 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2328 mutex_lock(&sport->mutex);
2330 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2331 struct srpt_rdma_ch *ch2;
2333 list_for_each_entry(ch2, &nexus->ch_list, list) {
2334 if (srpt_disconnect_ch(ch2) < 0)
2336 pr_info("Relogin - closed existing channel %s\n",
2338 rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2341 rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2344 list_add_tail_rcu(&ch->list, &nexus->ch_list);
2346 if (!sport->enabled) {
2347 rej->reason = cpu_to_be32(
2348 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2349 pr_info("rejected SRP_LOGIN_REQ because target %s_%d is not enabled\n",
2350 dev_name(&sdev->device->dev), port_num);
2351 mutex_unlock(&sport->mutex);
2355 mutex_unlock(&sport->mutex);
2357 ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rtr(ch, ch->qp);
2359 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2360 pr_err("rejected SRP_LOGIN_REQ because enabling RTR failed (error code = %d)\n",
2365 pr_debug("Establish connection sess=%p name=%s ch=%p\n", ch->sess,
2368 /* create srp_login_response */
2369 rsp->opcode = SRP_LOGIN_RSP;
2370 rsp->tag = req->tag;
2371 rsp->max_it_iu_len = cpu_to_be32(srp_max_req_size);
2372 rsp->max_ti_iu_len = req->req_it_iu_len;
2373 ch->max_ti_iu_len = it_iu_len;
2374 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
2375 SRP_BUF_FORMAT_INDIRECT);
2376 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2377 atomic_set(&ch->req_lim, ch->rq_size);
2378 atomic_set(&ch->req_lim_delta, 0);
2380 /* create cm reply */
2381 if (ch->using_rdma_cm) {
2382 rep_param->rdma_cm.private_data = (void *)rsp;
2383 rep_param->rdma_cm.private_data_len = sizeof(*rsp);
2384 rep_param->rdma_cm.rnr_retry_count = 7;
2385 rep_param->rdma_cm.flow_control = 1;
2386 rep_param->rdma_cm.responder_resources = 4;
2387 rep_param->rdma_cm.initiator_depth = 4;
2389 rep_param->ib_cm.qp_num = ch->qp->qp_num;
2390 rep_param->ib_cm.private_data = (void *)rsp;
2391 rep_param->ib_cm.private_data_len = sizeof(*rsp);
2392 rep_param->ib_cm.rnr_retry_count = 7;
2393 rep_param->ib_cm.flow_control = 1;
2394 rep_param->ib_cm.failover_accepted = 0;
2395 rep_param->ib_cm.srq = 1;
2396 rep_param->ib_cm.responder_resources = 4;
2397 rep_param->ib_cm.initiator_depth = 4;
2401 * Hold the sport mutex while accepting a connection to avoid that
2402 * srpt_disconnect_ch() is invoked concurrently with this code.
2404 mutex_lock(&sport->mutex);
2405 if (sport->enabled && ch->state == CH_CONNECTING) {
2406 if (ch->using_rdma_cm)
2407 ret = rdma_accept(rdma_cm_id, &rep_param->rdma_cm);
2409 ret = ib_send_cm_rep(ib_cm_id, &rep_param->ib_cm);
2413 mutex_unlock(&sport->mutex);
2421 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2422 pr_err("sending SRP_LOGIN_REQ response failed (error code = %d)\n",
2430 srpt_destroy_ch_ib(ch);
2433 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2434 ch->sport->sdev, ch->rq_size,
2435 ch->req_buf_cache, DMA_FROM_DEVICE);
2438 kmem_cache_destroy(ch->req_buf_cache);
2441 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2442 ch->sport->sdev, ch->rq_size,
2443 ch->rsp_buf_cache, DMA_TO_DEVICE);
2446 kmem_cache_destroy(ch->rsp_buf_cache);
2450 rdma_cm_id->context = NULL;
2452 ib_cm_id->context = NULL;
2456 WARN_ON_ONCE(ret == 0);
2459 pr_info("Rejecting login with reason %#x\n", be32_to_cpu(rej->reason));
2460 rej->opcode = SRP_LOGIN_REJ;
2461 rej->tag = req->tag;
2462 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
2463 SRP_BUF_FORMAT_INDIRECT);
2466 rdma_reject(rdma_cm_id, rej, sizeof(*rej));
2468 ib_send_cm_rej(ib_cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2471 if (ch && ch->sess) {
2474 * Tell the caller not to free cm_id since
2475 * srpt_release_channel_work() will do that.
2488 static int srpt_ib_cm_req_recv(struct ib_cm_id *cm_id,
2489 const struct ib_cm_req_event_param *param,
2494 srpt_format_guid(sguid, sizeof(sguid),
2495 ¶m->primary_path->dgid.global.interface_id);
2497 return srpt_cm_req_recv(cm_id->context, cm_id, NULL, param->port,
2498 param->primary_path->pkey,
2499 private_data, sguid);
2502 static int srpt_rdma_cm_req_recv(struct rdma_cm_id *cm_id,
2503 struct rdma_cm_event *event)
2505 struct srpt_device *sdev;
2506 struct srp_login_req req;
2507 const struct srp_login_req_rdma *req_rdma;
2510 sdev = ib_get_client_data(cm_id->device, &srpt_client);
2512 return -ECONNREFUSED;
2514 if (event->param.conn.private_data_len < sizeof(*req_rdma))
2517 /* Transform srp_login_req_rdma into srp_login_req. */
2518 req_rdma = event->param.conn.private_data;
2519 memset(&req, 0, sizeof(req));
2520 req.opcode = req_rdma->opcode;
2521 req.tag = req_rdma->tag;
2522 req.req_it_iu_len = req_rdma->req_it_iu_len;
2523 req.req_buf_fmt = req_rdma->req_buf_fmt;
2524 req.req_flags = req_rdma->req_flags;
2525 memcpy(req.initiator_port_id, req_rdma->initiator_port_id, 16);
2526 memcpy(req.target_port_id, req_rdma->target_port_id, 16);
2527 req.imm_data_offset = req_rdma->imm_data_offset;
2529 snprintf(src_addr, sizeof(src_addr), "%pIS",
2530 &cm_id->route.addr.src_addr);
2532 return srpt_cm_req_recv(sdev, NULL, cm_id, cm_id->port_num,
2533 cm_id->route.path_rec->pkey, &req, src_addr);
2536 static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
2537 enum ib_cm_rej_reason reason,
2538 const u8 *private_data,
2539 u8 private_data_len)
2544 if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
2546 for (i = 0; i < private_data_len; i++)
2547 sprintf(priv + 3 * i, " %02x", private_data[i]);
2549 pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
2550 ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
2551 "; private data" : "", priv ? priv : " (?)");
2556 * srpt_cm_rtu_recv - process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event
2557 * @ch: SRPT RDMA channel.
2559 * An RTU (ready to use) message indicates that the connection has been
2560 * established and that the recipient may begin transmitting.
2562 static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
2566 ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rts(ch, ch->qp);
2568 pr_err("%s-%d: QP transition to RTS failed\n", ch->sess_name,
2575 * Note: calling srpt_close_ch() if the transition to the LIVE state
2576 * fails is not necessary since that means that that function has
2577 * already been invoked from another thread.
2579 if (!srpt_set_ch_state(ch, CH_LIVE)) {
2580 pr_err("%s-%d: channel transition to LIVE state failed\n",
2581 ch->sess_name, ch->qp->qp_num);
2585 /* Trigger wait list processing. */
2586 ret = srpt_zerolength_write(ch);
2587 WARN_ONCE(ret < 0, "%d\n", ret);
2591 * srpt_cm_handler - IB connection manager callback function
2592 * @cm_id: IB/CM connection identifier.
2593 * @event: IB/CM event.
2595 * A non-zero return value will cause the caller destroy the CM ID.
2597 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2598 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2599 * a non-zero value in any other case will trigger a race with the
2600 * ib_destroy_cm_id() call in srpt_release_channel().
2602 static int srpt_cm_handler(struct ib_cm_id *cm_id,
2603 const struct ib_cm_event *event)
2605 struct srpt_rdma_ch *ch = cm_id->context;
2609 switch (event->event) {
2610 case IB_CM_REQ_RECEIVED:
2611 ret = srpt_ib_cm_req_recv(cm_id, &event->param.req_rcvd,
2612 event->private_data);
2614 case IB_CM_REJ_RECEIVED:
2615 srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
2616 event->private_data,
2617 IB_CM_REJ_PRIVATE_DATA_SIZE);
2619 case IB_CM_RTU_RECEIVED:
2620 case IB_CM_USER_ESTABLISHED:
2621 srpt_cm_rtu_recv(ch);
2623 case IB_CM_DREQ_RECEIVED:
2624 srpt_disconnect_ch(ch);
2626 case IB_CM_DREP_RECEIVED:
2627 pr_info("Received CM DREP message for ch %s-%d.\n",
2628 ch->sess_name, ch->qp->qp_num);
2631 case IB_CM_TIMEWAIT_EXIT:
2632 pr_info("Received CM TimeWait exit for ch %s-%d.\n",
2633 ch->sess_name, ch->qp->qp_num);
2636 case IB_CM_REP_ERROR:
2637 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2640 case IB_CM_DREQ_ERROR:
2641 pr_info("Received CM DREQ ERROR event.\n");
2643 case IB_CM_MRA_RECEIVED:
2644 pr_info("Received CM MRA event\n");
2647 pr_err("received unrecognized CM event %d\n", event->event);
2654 static int srpt_rdma_cm_handler(struct rdma_cm_id *cm_id,
2655 struct rdma_cm_event *event)
2657 struct srpt_rdma_ch *ch = cm_id->context;
2660 switch (event->event) {
2661 case RDMA_CM_EVENT_CONNECT_REQUEST:
2662 ret = srpt_rdma_cm_req_recv(cm_id, event);
2664 case RDMA_CM_EVENT_REJECTED:
2665 srpt_cm_rej_recv(ch, event->status,
2666 event->param.conn.private_data,
2667 event->param.conn.private_data_len);
2669 case RDMA_CM_EVENT_ESTABLISHED:
2670 srpt_cm_rtu_recv(ch);
2672 case RDMA_CM_EVENT_DISCONNECTED:
2673 if (ch->state < CH_DISCONNECTING)
2674 srpt_disconnect_ch(ch);
2678 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2681 case RDMA_CM_EVENT_UNREACHABLE:
2682 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2685 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2686 case RDMA_CM_EVENT_ADDR_CHANGE:
2689 pr_err("received unrecognized RDMA CM event %d\n",
2698 * srpt_write_pending - Start data transfer from initiator to target (write).
2700 static int srpt_write_pending(struct se_cmd *se_cmd)
2702 struct srpt_send_ioctx *ioctx =
2703 container_of(se_cmd, struct srpt_send_ioctx, cmd);
2704 struct srpt_rdma_ch *ch = ioctx->ch;
2705 struct ib_send_wr *first_wr = NULL;
2706 struct ib_cqe *cqe = &ioctx->rdma_cqe;
2707 enum srpt_command_state new_state;
2710 if (ioctx->recv_ioctx) {
2711 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2712 target_execute_cmd(&ioctx->cmd);
2716 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2717 WARN_ON(new_state == SRPT_STATE_DONE);
2719 if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
2720 pr_warn("%s: IB send queue full (needed %d)\n",
2721 __func__, ioctx->n_rdma);
2726 cqe->done = srpt_rdma_read_done;
2727 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2728 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2730 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
2735 ret = ib_post_send(ch->qp, first_wr, NULL);
2737 pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
2738 __func__, ret, ioctx->n_rdma,
2739 atomic_read(&ch->sq_wr_avail));
2745 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
2749 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2751 switch (tcm_mgmt_status) {
2752 case TMR_FUNCTION_COMPLETE:
2753 return SRP_TSK_MGMT_SUCCESS;
2754 case TMR_FUNCTION_REJECTED:
2755 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2757 return SRP_TSK_MGMT_FAILED;
2761 * srpt_queue_response - transmit the response to a SCSI command
2762 * @cmd: SCSI target command.
2764 * Callback function called by the TCM core. Must not block since it can be
2765 * invoked on the context of the IB completion handler.
2767 static void srpt_queue_response(struct se_cmd *cmd)
2769 struct srpt_send_ioctx *ioctx =
2770 container_of(cmd, struct srpt_send_ioctx, cmd);
2771 struct srpt_rdma_ch *ch = ioctx->ch;
2772 struct srpt_device *sdev = ch->sport->sdev;
2773 struct ib_send_wr send_wr, *first_wr = &send_wr;
2775 enum srpt_command_state state;
2776 int resp_len, ret, i;
2781 state = ioctx->state;
2783 case SRPT_STATE_NEW:
2784 case SRPT_STATE_DATA_IN:
2785 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2787 case SRPT_STATE_MGMT:
2788 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2791 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2792 ch, ioctx->ioctx.index, ioctx->state);
2796 if (WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))
2799 /* For read commands, transfer the data to the initiator. */
2800 if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
2801 ioctx->cmd.data_length &&
2802 !ioctx->queue_status_only) {
2803 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2804 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2806 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
2807 ch->sport->port, NULL, first_wr);
2811 if (state != SRPT_STATE_MGMT)
2812 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
2816 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2817 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
2821 atomic_inc(&ch->req_lim);
2823 if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
2824 &ch->sq_wr_avail) < 0)) {
2825 pr_warn("%s: IB send queue full (needed %d)\n",
2826 __func__, ioctx->n_rdma);
2831 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
2834 sge.addr = ioctx->ioctx.dma;
2835 sge.length = resp_len;
2836 sge.lkey = sdev->lkey;
2838 ioctx->ioctx.cqe.done = srpt_send_done;
2839 send_wr.next = NULL;
2840 send_wr.wr_cqe = &ioctx->ioctx.cqe;
2841 send_wr.sg_list = &sge;
2842 send_wr.num_sge = 1;
2843 send_wr.opcode = IB_WR_SEND;
2844 send_wr.send_flags = IB_SEND_SIGNALED;
2846 ret = ib_post_send(ch->qp, first_wr, NULL);
2848 pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
2849 __func__, ioctx->cmd.tag, ret);
2856 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
2857 atomic_dec(&ch->req_lim);
2858 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2859 target_put_sess_cmd(&ioctx->cmd);
2862 static int srpt_queue_data_in(struct se_cmd *cmd)
2864 srpt_queue_response(cmd);
2868 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2870 srpt_queue_response(cmd);
2874 * This function is called for aborted commands if no response is sent to the
2875 * initiator. Make sure that the credits freed by aborting a command are
2876 * returned to the initiator the next time a response is sent by incrementing
2877 * ch->req_lim_delta.
2879 static void srpt_aborted_task(struct se_cmd *cmd)
2881 struct srpt_send_ioctx *ioctx = container_of(cmd,
2882 struct srpt_send_ioctx, cmd);
2883 struct srpt_rdma_ch *ch = ioctx->ch;
2885 atomic_inc(&ch->req_lim_delta);
2888 static int srpt_queue_status(struct se_cmd *cmd)
2890 struct srpt_send_ioctx *ioctx;
2892 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2893 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2894 if (cmd->se_cmd_flags &
2895 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2896 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2897 ioctx->queue_status_only = true;
2898 srpt_queue_response(cmd);
2902 static void srpt_refresh_port_work(struct work_struct *work)
2904 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2906 srpt_refresh_port(sport);
2909 static bool srpt_ch_list_empty(struct srpt_port *sport)
2911 struct srpt_nexus *nexus;
2915 list_for_each_entry(nexus, &sport->nexus_list, entry)
2916 if (!list_empty(&nexus->ch_list))
2924 * srpt_release_sport - disable login and wait for associated channels
2925 * @sport: SRPT HCA port.
2927 static int srpt_release_sport(struct srpt_port *sport)
2929 struct srpt_nexus *nexus, *next_n;
2930 struct srpt_rdma_ch *ch;
2932 WARN_ON_ONCE(irqs_disabled());
2934 mutex_lock(&sport->mutex);
2935 srpt_set_enabled(sport, false);
2936 mutex_unlock(&sport->mutex);
2938 while (wait_event_timeout(sport->ch_releaseQ,
2939 srpt_ch_list_empty(sport), 5 * HZ) <= 0) {
2940 pr_info("%s_%d: waiting for session unregistration ...\n",
2941 dev_name(&sport->sdev->device->dev), sport->port);
2943 list_for_each_entry(nexus, &sport->nexus_list, entry) {
2944 list_for_each_entry(ch, &nexus->ch_list, list) {
2945 pr_info("%s-%d: state %s\n",
2946 ch->sess_name, ch->qp->qp_num,
2947 get_ch_state_name(ch->state));
2953 mutex_lock(&sport->mutex);
2954 list_for_each_entry_safe(nexus, next_n, &sport->nexus_list, entry) {
2955 list_del(&nexus->entry);
2956 kfree_rcu(nexus, rcu);
2958 mutex_unlock(&sport->mutex);
2963 static struct se_wwn *__srpt_lookup_wwn(const char *name)
2965 struct ib_device *dev;
2966 struct srpt_device *sdev;
2967 struct srpt_port *sport;
2970 list_for_each_entry(sdev, &srpt_dev_list, list) {
2975 for (i = 0; i < dev->phys_port_cnt; i++) {
2976 sport = &sdev->port[i];
2978 if (strcmp(sport->port_guid, name) == 0)
2979 return &sport->port_guid_wwn;
2980 if (strcmp(sport->port_gid, name) == 0)
2981 return &sport->port_gid_wwn;
2988 static struct se_wwn *srpt_lookup_wwn(const char *name)
2992 spin_lock(&srpt_dev_lock);
2993 wwn = __srpt_lookup_wwn(name);
2994 spin_unlock(&srpt_dev_lock);
2999 static void srpt_free_srq(struct srpt_device *sdev)
3004 ib_destroy_srq(sdev->srq);
3005 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3006 sdev->srq_size, sdev->req_buf_cache,
3008 kmem_cache_destroy(sdev->req_buf_cache);
3012 static int srpt_alloc_srq(struct srpt_device *sdev)
3014 struct ib_srq_init_attr srq_attr = {
3015 .event_handler = srpt_srq_event,
3016 .srq_context = (void *)sdev,
3017 .attr.max_wr = sdev->srq_size,
3019 .srq_type = IB_SRQT_BASIC,
3021 struct ib_device *device = sdev->device;
3025 WARN_ON_ONCE(sdev->srq);
3026 srq = ib_create_srq(sdev->pd, &srq_attr);
3028 pr_debug("ib_create_srq() failed: %ld\n", PTR_ERR(srq));
3029 return PTR_ERR(srq);
3032 pr_debug("create SRQ #wr= %d max_allow=%d dev= %s\n", sdev->srq_size,
3033 sdev->device->attrs.max_srq_wr, dev_name(&device->dev));
3035 sdev->req_buf_cache = kmem_cache_create("srpt-srq-req-buf",
3036 srp_max_req_size, 0, 0, NULL);
3037 if (!sdev->req_buf_cache)
3040 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3041 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3042 sizeof(*sdev->ioctx_ring[0]),
3043 sdev->req_buf_cache, 0, DMA_FROM_DEVICE);
3044 if (!sdev->ioctx_ring)
3047 sdev->use_srq = true;
3050 for (i = 0; i < sdev->srq_size; ++i) {
3051 INIT_LIST_HEAD(&sdev->ioctx_ring[i]->wait_list);
3052 srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]);
3058 kmem_cache_destroy(sdev->req_buf_cache);
3061 ib_destroy_srq(srq);
3065 static int srpt_use_srq(struct srpt_device *sdev, bool use_srq)
3067 struct ib_device *device = sdev->device;
3071 srpt_free_srq(sdev);
3072 sdev->use_srq = false;
3073 } else if (use_srq && !sdev->srq) {
3074 ret = srpt_alloc_srq(sdev);
3076 pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__,
3077 dev_name(&device->dev), sdev->use_srq, ret);
3082 * srpt_add_one - InfiniBand device addition callback function
3083 * @device: Describes a HCA.
3085 static void srpt_add_one(struct ib_device *device)
3087 struct srpt_device *sdev;
3088 struct srpt_port *sport;
3091 pr_debug("device = %p\n", device);
3093 sdev = kzalloc(struct_size(sdev, port, device->phys_port_cnt),
3098 sdev->device = device;
3099 mutex_init(&sdev->sdev_mutex);
3101 sdev->pd = ib_alloc_pd(device, 0);
3102 if (IS_ERR(sdev->pd))
3105 sdev->lkey = sdev->pd->local_dma_lkey;
3107 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
3109 srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq);
3111 if (!srpt_service_guid)
3112 srpt_service_guid = be64_to_cpu(device->node_guid);
3114 if (rdma_port_get_link_layer(device, 1) == IB_LINK_LAYER_INFINIBAND)
3115 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3116 if (IS_ERR(sdev->cm_id)) {
3117 pr_info("ib_create_cm_id() failed: %ld\n",
3118 PTR_ERR(sdev->cm_id));
3124 /* print out target login information */
3125 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,pkey=ffff,service_id=%016llx\n",
3126 srpt_service_guid, srpt_service_guid, srpt_service_guid);
3129 * We do not have a consistent service_id (ie. also id_ext of target_id)
3130 * to identify this target. We currently use the guid of the first HCA
3131 * in the system as service_id; therefore, the target_id will change
3132 * if this HCA is gone bad and replaced by different HCA
3135 ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0) :
3138 pr_err("ib_cm_listen() failed: %d (cm_id state = %d)\n", ret,
3139 sdev->cm_id->state);
3143 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3144 srpt_event_handler);
3145 ib_register_event_handler(&sdev->event_handler);
3147 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3148 sport = &sdev->port[i - 1];
3149 INIT_LIST_HEAD(&sport->nexus_list);
3150 init_waitqueue_head(&sport->ch_releaseQ);
3151 mutex_init(&sport->mutex);
3154 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3155 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3156 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3157 sport->port_attrib.use_srq = false;
3158 INIT_WORK(&sport->work, srpt_refresh_port_work);
3160 if (srpt_refresh_port(sport)) {
3161 pr_err("MAD registration failed for %s-%d.\n",
3162 dev_name(&sdev->device->dev), i);
3167 spin_lock(&srpt_dev_lock);
3168 list_add_tail(&sdev->list, &srpt_dev_list);
3169 spin_unlock(&srpt_dev_lock);
3172 ib_set_client_data(device, &srpt_client, sdev);
3173 pr_debug("added %s.\n", dev_name(&device->dev));
3177 ib_unregister_event_handler(&sdev->event_handler);
3180 ib_destroy_cm_id(sdev->cm_id);
3182 srpt_free_srq(sdev);
3183 ib_dealloc_pd(sdev->pd);
3188 pr_info("%s(%s) failed.\n", __func__, dev_name(&device->dev));
3193 * srpt_remove_one - InfiniBand device removal callback function
3194 * @device: Describes a HCA.
3195 * @client_data: The value passed as the third argument to ib_set_client_data().
3197 static void srpt_remove_one(struct ib_device *device, void *client_data)
3199 struct srpt_device *sdev = client_data;
3203 pr_info("%s(%s): nothing to do.\n", __func__,
3204 dev_name(&device->dev));
3208 srpt_unregister_mad_agent(sdev);
3210 ib_unregister_event_handler(&sdev->event_handler);
3212 /* Cancel any work queued by the just unregistered IB event handler. */
3213 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3214 cancel_work_sync(&sdev->port[i].work);
3217 ib_destroy_cm_id(sdev->cm_id);
3219 ib_set_client_data(device, &srpt_client, NULL);
3222 * Unregistering a target must happen after destroying sdev->cm_id
3223 * such that no new SRP_LOGIN_REQ information units can arrive while
3224 * destroying the target.
3226 spin_lock(&srpt_dev_lock);
3227 list_del(&sdev->list);
3228 spin_unlock(&srpt_dev_lock);
3230 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3231 srpt_release_sport(&sdev->port[i]);
3233 srpt_free_srq(sdev);
3235 ib_dealloc_pd(sdev->pd);
3240 static struct ib_client srpt_client = {
3242 .add = srpt_add_one,
3243 .remove = srpt_remove_one
3246 static int srpt_check_true(struct se_portal_group *se_tpg)
3251 static int srpt_check_false(struct se_portal_group *se_tpg)
3256 static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg)
3258 return tpg->se_tpg_wwn->priv;
3261 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3263 struct srpt_port *sport = srpt_tpg_to_sport(tpg);
3265 WARN_ON_ONCE(tpg != &sport->port_guid_tpg &&
3266 tpg != &sport->port_gid_tpg);
3267 return tpg == &sport->port_guid_tpg ? sport->port_guid :
3271 static u16 srpt_get_tag(struct se_portal_group *tpg)
3276 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3281 static void srpt_release_cmd(struct se_cmd *se_cmd)
3283 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3284 struct srpt_send_ioctx, cmd);
3285 struct srpt_rdma_ch *ch = ioctx->ch;
3286 struct srpt_recv_ioctx *recv_ioctx = ioctx->recv_ioctx;
3288 WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
3289 !(ioctx->cmd.transport_state & CMD_T_ABORTED));
3292 WARN_ON_ONCE(!list_empty(&recv_ioctx->wait_list));
3293 ioctx->recv_ioctx = NULL;
3294 srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
3297 if (ioctx->n_rw_ctx) {
3298 srpt_free_rw_ctxs(ch, ioctx);
3299 ioctx->n_rw_ctx = 0;
3302 target_free_tag(se_cmd->se_sess, se_cmd);
3306 * srpt_close_session - forcibly close a session
3307 * @se_sess: SCSI target session.
3309 * Callback function invoked by the TCM core to clean up sessions associated
3310 * with a node ACL when the user invokes
3311 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3313 static void srpt_close_session(struct se_session *se_sess)
3315 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
3317 srpt_disconnect_ch_sync(ch);
3321 * srpt_sess_get_index - return the value of scsiAttIntrPortIndex (SCSI-MIB)
3322 * @se_sess: SCSI target session.
3324 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3325 * This object represents an arbitrary integer used to uniquely identify a
3326 * particular attached remote initiator port to a particular SCSI target port
3327 * within a particular SCSI target device within a particular SCSI instance.
3329 static u32 srpt_sess_get_index(struct se_session *se_sess)
3334 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3338 /* Note: only used from inside debug printk's by the TCM core. */
3339 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3341 struct srpt_send_ioctx *ioctx;
3343 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3344 return ioctx->state;
3347 static int srpt_parse_guid(u64 *guid, const char *name)
3352 if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4)
3354 *guid = get_unaligned_be64(w);
3361 * srpt_parse_i_port_id - parse an initiator port ID
3362 * @name: ASCII representation of a 128-bit initiator port ID.
3363 * @i_port_id: Binary 128-bit port ID.
3365 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3368 unsigned len, count, leading_zero_bytes;
3372 if (strncasecmp(p, "0x", 2) == 0)
3378 count = min(len / 2, 16U);
3379 leading_zero_bytes = 16 - count;
3380 memset(i_port_id, 0, leading_zero_bytes);
3381 ret = hex2bin(i_port_id + leading_zero_bytes, p, count);
3388 * configfs callback function invoked for mkdir
3389 * /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3391 * i_port_id must be an initiator port GUID, GID or IP address. See also the
3392 * target_alloc_session() calls in this driver. Examples of valid initiator
3394 * 0x0000000000000000505400fffe4a0b7b
3395 * 0000000000000000505400fffe4a0b7b
3396 * 5054:00ff:fe4a:0b7b
3399 static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
3401 struct sockaddr_storage sa;
3406 ret = srpt_parse_guid(&guid, name);
3408 ret = srpt_parse_i_port_id(i_port_id, name);
3410 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, name, NULL,
3413 pr_err("invalid initiator port ID %s\n", name);
3417 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3420 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3421 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3423 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3426 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3427 const char *page, size_t count)
3429 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3430 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3434 ret = kstrtoul(page, 0, &val);
3436 pr_err("kstrtoul() failed with ret: %d\n", ret);
3439 if (val > MAX_SRPT_RDMA_SIZE) {
3440 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3441 MAX_SRPT_RDMA_SIZE);
3444 if (val < DEFAULT_MAX_RDMA_SIZE) {
3445 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3446 val, DEFAULT_MAX_RDMA_SIZE);
3449 sport->port_attrib.srp_max_rdma_size = val;
3454 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3457 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3458 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3460 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3463 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3464 const char *page, size_t count)
3466 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3467 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3471 ret = kstrtoul(page, 0, &val);
3473 pr_err("kstrtoul() failed with ret: %d\n", ret);
3476 if (val > MAX_SRPT_RSP_SIZE) {
3477 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3481 if (val < MIN_MAX_RSP_SIZE) {
3482 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3486 sport->port_attrib.srp_max_rsp_size = val;
3491 static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3494 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3495 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3497 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3500 static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3501 const char *page, size_t count)
3503 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3504 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3508 ret = kstrtoul(page, 0, &val);
3510 pr_err("kstrtoul() failed with ret: %d\n", ret);
3513 if (val > MAX_SRPT_SRQ_SIZE) {
3514 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3518 if (val < MIN_SRPT_SRQ_SIZE) {
3519 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3523 sport->port_attrib.srp_sq_size = val;
3528 static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item,
3531 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3532 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3534 return sprintf(page, "%d\n", sport->port_attrib.use_srq);
3537 static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item,
3538 const char *page, size_t count)
3540 struct se_portal_group *se_tpg = attrib_to_tpg(item);
3541 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3542 struct srpt_device *sdev = sport->sdev;
3547 ret = kstrtoul(page, 0, &val);
3553 ret = mutex_lock_interruptible(&sdev->sdev_mutex);
3556 ret = mutex_lock_interruptible(&sport->mutex);
3559 enabled = sport->enabled;
3560 /* Log out all initiator systems before changing 'use_srq'. */
3561 srpt_set_enabled(sport, false);
3562 sport->port_attrib.use_srq = val;
3563 srpt_use_srq(sdev, sport->port_attrib.use_srq);
3564 srpt_set_enabled(sport, enabled);
3566 mutex_unlock(&sport->mutex);
3568 mutex_unlock(&sdev->sdev_mutex);
3573 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3574 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3575 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
3576 CONFIGFS_ATTR(srpt_tpg_attrib_, use_srq);
3578 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3579 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3580 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3581 &srpt_tpg_attrib_attr_srp_sq_size,
3582 &srpt_tpg_attrib_attr_use_srq,
3586 static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr)
3588 struct rdma_cm_id *rdma_cm_id;
3591 rdma_cm_id = rdma_create_id(&init_net, srpt_rdma_cm_handler,
3592 NULL, RDMA_PS_TCP, IB_QPT_RC);
3593 if (IS_ERR(rdma_cm_id)) {
3594 pr_err("RDMA/CM ID creation failed: %ld\n",
3595 PTR_ERR(rdma_cm_id));
3599 ret = rdma_bind_addr(rdma_cm_id, listen_addr);
3603 snprintf(addr_str, sizeof(addr_str), "%pISp", listen_addr);
3604 pr_err("Binding RDMA/CM ID to address %s failed: %d\n",
3606 rdma_destroy_id(rdma_cm_id);
3607 rdma_cm_id = ERR_PTR(ret);
3611 ret = rdma_listen(rdma_cm_id, 128);
3613 pr_err("rdma_listen() failed: %d\n", ret);
3614 rdma_destroy_id(rdma_cm_id);
3615 rdma_cm_id = ERR_PTR(ret);
3622 static ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page)
3624 return sprintf(page, "%d\n", rdma_cm_port);
3627 static ssize_t srpt_rdma_cm_port_store(struct config_item *item,
3628 const char *page, size_t count)
3630 struct sockaddr_in addr4 = { .sin_family = AF_INET };
3631 struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6 };
3632 struct rdma_cm_id *new_id = NULL;
3636 ret = kstrtou16(page, 0, &val);
3640 if (rdma_cm_port == val)
3644 addr6.sin6_port = cpu_to_be16(val);
3645 new_id = srpt_create_rdma_id((struct sockaddr *)&addr6);
3646 if (IS_ERR(new_id)) {
3647 addr4.sin_port = cpu_to_be16(val);
3648 new_id = srpt_create_rdma_id((struct sockaddr *)&addr4);
3649 if (IS_ERR(new_id)) {
3650 ret = PTR_ERR(new_id);
3656 mutex_lock(&rdma_cm_mutex);
3658 swap(rdma_cm_id, new_id);
3659 mutex_unlock(&rdma_cm_mutex);
3662 rdma_destroy_id(new_id);
3668 CONFIGFS_ATTR(srpt_, rdma_cm_port);
3670 static struct configfs_attribute *srpt_da_attrs[] = {
3671 &srpt_attr_rdma_cm_port,
3675 static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
3677 struct se_portal_group *se_tpg = to_tpg(item);
3678 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3680 return snprintf(page, PAGE_SIZE, "%d\n", sport->enabled);
3683 static ssize_t srpt_tpg_enable_store(struct config_item *item,
3684 const char *page, size_t count)
3686 struct se_portal_group *se_tpg = to_tpg(item);
3687 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3691 ret = kstrtoul(page, 0, &tmp);
3693 pr_err("Unable to extract srpt_tpg_store_enable\n");
3697 if ((tmp != 0) && (tmp != 1)) {
3698 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3702 mutex_lock(&sport->mutex);
3703 srpt_set_enabled(sport, tmp);
3704 mutex_unlock(&sport->mutex);
3709 CONFIGFS_ATTR(srpt_tpg_, enable);
3711 static struct configfs_attribute *srpt_tpg_attrs[] = {
3712 &srpt_tpg_attr_enable,
3717 * srpt_make_tpg - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port/$tpg
3718 * @wwn: Corresponds to $driver/$port.
3721 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3724 struct srpt_port *sport = wwn->priv;
3725 struct se_portal_group *tpg;
3728 WARN_ON_ONCE(wwn != &sport->port_guid_wwn &&
3729 wwn != &sport->port_gid_wwn);
3730 tpg = wwn == &sport->port_guid_wwn ? &sport->port_guid_tpg :
3731 &sport->port_gid_tpg;
3732 res = core_tpg_register(wwn, tpg, SCSI_PROTOCOL_SRP);
3734 return ERR_PTR(res);
3740 * srpt_drop_tpg - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port/$tpg
3741 * @tpg: Target portal group to deregister.
3743 static void srpt_drop_tpg(struct se_portal_group *tpg)
3745 struct srpt_port *sport = srpt_tpg_to_sport(tpg);
3747 sport->enabled = false;
3748 core_tpg_deregister(tpg);
3752 * srpt_make_tport - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port
3757 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3758 struct config_group *group,
3761 return srpt_lookup_wwn(name) ? : ERR_PTR(-EINVAL);
3765 * srpt_drop_tport - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port
3768 static void srpt_drop_tport(struct se_wwn *wwn)
3772 static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
3774 return scnprintf(buf, PAGE_SIZE, "\n");
3777 CONFIGFS_ATTR_RO(srpt_wwn_, version);
3779 static struct configfs_attribute *srpt_wwn_attrs[] = {
3780 &srpt_wwn_attr_version,
3784 static const struct target_core_fabric_ops srpt_template = {
3785 .module = THIS_MODULE,
3786 .fabric_name = "srpt",
3787 .tpg_get_wwn = srpt_get_fabric_wwn,
3788 .tpg_get_tag = srpt_get_tag,
3789 .tpg_check_demo_mode = srpt_check_false,
3790 .tpg_check_demo_mode_cache = srpt_check_true,
3791 .tpg_check_demo_mode_write_protect = srpt_check_true,
3792 .tpg_check_prod_mode_write_protect = srpt_check_false,
3793 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3794 .release_cmd = srpt_release_cmd,
3795 .check_stop_free = srpt_check_stop_free,
3796 .close_session = srpt_close_session,
3797 .sess_get_index = srpt_sess_get_index,
3798 .sess_get_initiator_sid = NULL,
3799 .write_pending = srpt_write_pending,
3800 .set_default_node_attributes = srpt_set_default_node_attrs,
3801 .get_cmd_state = srpt_get_tcm_cmd_state,
3802 .queue_data_in = srpt_queue_data_in,
3803 .queue_status = srpt_queue_status,
3804 .queue_tm_rsp = srpt_queue_tm_rsp,
3805 .aborted_task = srpt_aborted_task,
3807 * Setup function pointers for generic logic in
3808 * target_core_fabric_configfs.c
3810 .fabric_make_wwn = srpt_make_tport,
3811 .fabric_drop_wwn = srpt_drop_tport,
3812 .fabric_make_tpg = srpt_make_tpg,
3813 .fabric_drop_tpg = srpt_drop_tpg,
3814 .fabric_init_nodeacl = srpt_init_nodeacl,
3816 .tfc_discovery_attrs = srpt_da_attrs,
3817 .tfc_wwn_attrs = srpt_wwn_attrs,
3818 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3819 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
3823 * srpt_init_module - kernel module initialization
3825 * Note: Since ib_register_client() registers callback functions, and since at
3826 * least one of these callback functions (srpt_add_one()) calls target core
3827 * functions, this driver must be registered with the target core before
3828 * ib_register_client() is called.
3830 static int __init srpt_init_module(void)
3835 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3836 pr_err("invalid value %d for kernel module parameter srp_max_req_size -- must be at least %d.\n",
3837 srp_max_req_size, MIN_MAX_REQ_SIZE);
3841 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3842 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3843 pr_err("invalid value %d for kernel module parameter srpt_srq_size -- must be in the range [%d..%d].\n",
3844 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3848 ret = target_register_template(&srpt_template);
3852 ret = ib_register_client(&srpt_client);
3854 pr_err("couldn't register IB client\n");
3855 goto out_unregister_target;
3860 out_unregister_target:
3861 target_unregister_template(&srpt_template);
3866 static void __exit srpt_cleanup_module(void)
3869 rdma_destroy_id(rdma_cm_id);
3870 ib_unregister_client(&srpt_client);
3871 target_unregister_template(&srpt_template);
3874 module_init(srpt_init_module);
3875 module_exit(srpt_cleanup_module);