mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / infiniband / ulp / srpt / ib_srpt.c
blob201b604f63713b22579c7742a583bbcdd91636df
1 /*
2 * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
3 * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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
31 * SOFTWARE.
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/err.h>
39 #include <linux/ctype.h>
40 #include <linux/kthread.h>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/atomic.h>
44 #include <scsi/scsi_tcq.h>
45 #include <target/configfs_macros.h>
46 #include <target/target_core_base.h>
47 #include <target/target_core_fabric_configfs.h>
48 #include <target/target_core_fabric.h>
49 #include <target/target_core_configfs.h>
50 #include "ib_srpt.h"
52 /* Name of this kernel module. */
53 #define DRV_NAME "ib_srpt"
54 #define DRV_VERSION "2.0.0"
55 #define DRV_RELDATE "2011-02-14"
57 #define SRPT_ID_STRING "Linux SRP target"
59 #undef pr_fmt
60 #define pr_fmt(fmt) DRV_NAME " " fmt
62 MODULE_AUTHOR("Vu Pham and Bart Van Assche");
63 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
64 "v" DRV_VERSION " (" DRV_RELDATE ")");
65 MODULE_LICENSE("Dual BSD/GPL");
68 * Global Variables
71 static u64 srpt_service_guid;
72 static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
73 static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
75 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
76 module_param(srp_max_req_size, int, 0444);
77 MODULE_PARM_DESC(srp_max_req_size,
78 "Maximum size of SRP request messages in bytes.");
80 static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
81 module_param(srpt_srq_size, int, 0444);
82 MODULE_PARM_DESC(srpt_srq_size,
83 "Shared receive queue (SRQ) size.");
85 static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
87 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
89 module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
90 0444);
91 MODULE_PARM_DESC(srpt_service_guid,
92 "Using this value for ioc_guid, id_ext, and cm_listen_id"
93 " instead of using the node_guid of the first HCA.");
95 static struct ib_client srpt_client;
96 static struct target_fabric_configfs *srpt_target;
97 static void srpt_release_channel(struct srpt_rdma_ch *ch);
98 static int srpt_queue_status(struct se_cmd *cmd);
101 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
103 static inline
104 enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
106 switch (dir) {
107 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
108 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
109 default: return dir;
114 * srpt_sdev_name() - Return the name associated with the HCA.
116 * Examples are ib0, ib1, ...
118 static inline const char *srpt_sdev_name(struct srpt_device *sdev)
120 return sdev->device->name;
123 static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
125 unsigned long flags;
126 enum rdma_ch_state state;
128 spin_lock_irqsave(&ch->spinlock, flags);
129 state = ch->state;
130 spin_unlock_irqrestore(&ch->spinlock, flags);
131 return state;
134 static enum rdma_ch_state
135 srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
137 unsigned long flags;
138 enum rdma_ch_state prev;
140 spin_lock_irqsave(&ch->spinlock, flags);
141 prev = ch->state;
142 ch->state = new_state;
143 spin_unlock_irqrestore(&ch->spinlock, flags);
144 return prev;
148 * srpt_test_and_set_ch_state() - Test and set the channel state.
150 * Returns true if and only if the channel state has been set to the new state.
152 static bool
153 srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
154 enum rdma_ch_state new)
156 unsigned long flags;
157 enum rdma_ch_state prev;
159 spin_lock_irqsave(&ch->spinlock, flags);
160 prev = ch->state;
161 if (prev == old)
162 ch->state = new;
163 spin_unlock_irqrestore(&ch->spinlock, flags);
164 return prev == old;
168 * srpt_event_handler() - Asynchronous IB event callback function.
170 * Callback function called by the InfiniBand core when an asynchronous IB
171 * event occurs. This callback may occur in interrupt context. See also
172 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
173 * Architecture Specification.
175 static void srpt_event_handler(struct ib_event_handler *handler,
176 struct ib_event *event)
178 struct srpt_device *sdev;
179 struct srpt_port *sport;
181 sdev = ib_get_client_data(event->device, &srpt_client);
182 if (!sdev || sdev->device != event->device)
183 return;
185 pr_debug("ASYNC event= %d on device= %s\n", event->event,
186 srpt_sdev_name(sdev));
188 switch (event->event) {
189 case IB_EVENT_PORT_ERR:
190 if (event->element.port_num <= sdev->device->phys_port_cnt) {
191 sport = &sdev->port[event->element.port_num - 1];
192 sport->lid = 0;
193 sport->sm_lid = 0;
195 break;
196 case IB_EVENT_PORT_ACTIVE:
197 case IB_EVENT_LID_CHANGE:
198 case IB_EVENT_PKEY_CHANGE:
199 case IB_EVENT_SM_CHANGE:
200 case IB_EVENT_CLIENT_REREGISTER:
201 /* Refresh port data asynchronously. */
202 if (event->element.port_num <= sdev->device->phys_port_cnt) {
203 sport = &sdev->port[event->element.port_num - 1];
204 if (!sport->lid && !sport->sm_lid)
205 schedule_work(&sport->work);
207 break;
208 default:
209 printk(KERN_ERR "received unrecognized IB event %d\n",
210 event->event);
211 break;
216 * srpt_srq_event() - SRQ event callback function.
218 static void srpt_srq_event(struct ib_event *event, void *ctx)
220 printk(KERN_INFO "SRQ event %d\n", event->event);
224 * srpt_qp_event() - QP event callback function.
226 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
228 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
229 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
231 switch (event->event) {
232 case IB_EVENT_COMM_EST:
233 ib_cm_notify(ch->cm_id, event->event);
234 break;
235 case IB_EVENT_QP_LAST_WQE_REACHED:
236 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
237 CH_RELEASING))
238 srpt_release_channel(ch);
239 else
240 pr_debug("%s: state %d - ignored LAST_WQE.\n",
241 ch->sess_name, srpt_get_ch_state(ch));
242 break;
243 default:
244 printk(KERN_ERR "received unrecognized IB QP event %d\n",
245 event->event);
246 break;
251 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
253 * @slot: one-based slot number.
254 * @value: four-bit value.
256 * Copies the lowest four bits of value in element slot of the array of four
257 * bit elements called c_list (controller list). The index slot is one-based.
259 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
261 u16 id;
262 u8 tmp;
264 id = (slot - 1) / 2;
265 if (slot & 0x1) {
266 tmp = c_list[id] & 0xf;
267 c_list[id] = (value << 4) | tmp;
268 } else {
269 tmp = c_list[id] & 0xf0;
270 c_list[id] = (value & 0xf) | tmp;
275 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
277 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
278 * Specification.
280 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
282 struct ib_class_port_info *cif;
284 cif = (struct ib_class_port_info *)mad->data;
285 memset(cif, 0, sizeof *cif);
286 cif->base_version = 1;
287 cif->class_version = 1;
288 cif->resp_time_value = 20;
290 mad->mad_hdr.status = 0;
294 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
296 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
297 * Specification. See also section B.7, table B.6 in the SRP r16a document.
299 static void srpt_get_iou(struct ib_dm_mad *mad)
301 struct ib_dm_iou_info *ioui;
302 u8 slot;
303 int i;
305 ioui = (struct ib_dm_iou_info *)mad->data;
306 ioui->change_id = __constant_cpu_to_be16(1);
307 ioui->max_controllers = 16;
309 /* set present for slot 1 and empty for the rest */
310 srpt_set_ioc(ioui->controller_list, 1, 1);
311 for (i = 1, slot = 2; i < 16; i++, slot++)
312 srpt_set_ioc(ioui->controller_list, slot, 0);
314 mad->mad_hdr.status = 0;
318 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
320 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
321 * Architecture Specification. See also section B.7, table B.7 in the SRP
322 * r16a document.
324 static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
325 struct ib_dm_mad *mad)
327 struct srpt_device *sdev = sport->sdev;
328 struct ib_dm_ioc_profile *iocp;
330 iocp = (struct ib_dm_ioc_profile *)mad->data;
332 if (!slot || slot > 16) {
333 mad->mad_hdr.status
334 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
335 return;
338 if (slot > 2) {
339 mad->mad_hdr.status
340 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
341 return;
344 memset(iocp, 0, sizeof *iocp);
345 strcpy(iocp->id_string, SRPT_ID_STRING);
346 iocp->guid = cpu_to_be64(srpt_service_guid);
347 iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
348 iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
349 iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
350 iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
351 iocp->subsys_device_id = 0x0;
352 iocp->io_class = __constant_cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
353 iocp->io_subclass = __constant_cpu_to_be16(SRP_IO_SUBCLASS);
354 iocp->protocol = __constant_cpu_to_be16(SRP_PROTOCOL);
355 iocp->protocol_version = __constant_cpu_to_be16(SRP_PROTOCOL_VERSION);
356 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
357 iocp->rdma_read_depth = 4;
358 iocp->send_size = cpu_to_be32(srp_max_req_size);
359 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
360 1U << 24));
361 iocp->num_svc_entries = 1;
362 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
363 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
365 mad->mad_hdr.status = 0;
369 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
371 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
372 * Specification. See also section B.7, table B.8 in the SRP r16a document.
374 static void srpt_get_svc_entries(u64 ioc_guid,
375 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
377 struct ib_dm_svc_entries *svc_entries;
379 WARN_ON(!ioc_guid);
381 if (!slot || slot > 16) {
382 mad->mad_hdr.status
383 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
384 return;
387 if (slot > 2 || lo > hi || hi > 1) {
388 mad->mad_hdr.status
389 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
390 return;
393 svc_entries = (struct ib_dm_svc_entries *)mad->data;
394 memset(svc_entries, 0, sizeof *svc_entries);
395 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
396 snprintf(svc_entries->service_entries[0].name,
397 sizeof(svc_entries->service_entries[0].name),
398 "%s%016llx",
399 SRP_SERVICE_NAME_PREFIX,
400 ioc_guid);
402 mad->mad_hdr.status = 0;
406 * srpt_mgmt_method_get() - Process a received management datagram.
407 * @sp: source port through which the MAD has been received.
408 * @rq_mad: received MAD.
409 * @rsp_mad: response MAD.
411 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
412 struct ib_dm_mad *rsp_mad)
414 u16 attr_id;
415 u32 slot;
416 u8 hi, lo;
418 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
419 switch (attr_id) {
420 case DM_ATTR_CLASS_PORT_INFO:
421 srpt_get_class_port_info(rsp_mad);
422 break;
423 case DM_ATTR_IOU_INFO:
424 srpt_get_iou(rsp_mad);
425 break;
426 case DM_ATTR_IOC_PROFILE:
427 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
428 srpt_get_ioc(sp, slot, rsp_mad);
429 break;
430 case DM_ATTR_SVC_ENTRIES:
431 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
432 hi = (u8) ((slot >> 8) & 0xff);
433 lo = (u8) (slot & 0xff);
434 slot = (u16) ((slot >> 16) & 0xffff);
435 srpt_get_svc_entries(srpt_service_guid,
436 slot, hi, lo, rsp_mad);
437 break;
438 default:
439 rsp_mad->mad_hdr.status =
440 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
441 break;
446 * srpt_mad_send_handler() - Post MAD-send callback function.
448 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
449 struct ib_mad_send_wc *mad_wc)
451 ib_destroy_ah(mad_wc->send_buf->ah);
452 ib_free_send_mad(mad_wc->send_buf);
456 * srpt_mad_recv_handler() - MAD reception callback function.
458 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
459 struct ib_mad_recv_wc *mad_wc)
461 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
462 struct ib_ah *ah;
463 struct ib_mad_send_buf *rsp;
464 struct ib_dm_mad *dm_mad;
466 if (!mad_wc || !mad_wc->recv_buf.mad)
467 return;
469 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
470 mad_wc->recv_buf.grh, mad_agent->port_num);
471 if (IS_ERR(ah))
472 goto err;
474 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
476 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
477 mad_wc->wc->pkey_index, 0,
478 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
479 GFP_KERNEL);
480 if (IS_ERR(rsp))
481 goto err_rsp;
483 rsp->ah = ah;
485 dm_mad = rsp->mad;
486 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
487 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
488 dm_mad->mad_hdr.status = 0;
490 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
491 case IB_MGMT_METHOD_GET:
492 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
493 break;
494 case IB_MGMT_METHOD_SET:
495 dm_mad->mad_hdr.status =
496 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
497 break;
498 default:
499 dm_mad->mad_hdr.status =
500 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
501 break;
504 if (!ib_post_send_mad(rsp, NULL)) {
505 ib_free_recv_mad(mad_wc);
506 /* will destroy_ah & free_send_mad in send completion */
507 return;
510 ib_free_send_mad(rsp);
512 err_rsp:
513 ib_destroy_ah(ah);
514 err:
515 ib_free_recv_mad(mad_wc);
519 * srpt_refresh_port() - Configure a HCA port.
521 * Enable InfiniBand management datagram processing, update the cached sm_lid,
522 * lid and gid values, and register a callback function for processing MADs
523 * on the specified port.
525 * Note: It is safe to call this function more than once for the same port.
527 static int srpt_refresh_port(struct srpt_port *sport)
529 struct ib_mad_reg_req reg_req;
530 struct ib_port_modify port_modify;
531 struct ib_port_attr port_attr;
532 int ret;
534 memset(&port_modify, 0, sizeof port_modify);
535 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
536 port_modify.clr_port_cap_mask = 0;
538 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
539 if (ret)
540 goto err_mod_port;
542 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
543 if (ret)
544 goto err_query_port;
546 sport->sm_lid = port_attr.sm_lid;
547 sport->lid = port_attr.lid;
549 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
550 if (ret)
551 goto err_query_port;
553 if (!sport->mad_agent) {
554 memset(&reg_req, 0, sizeof reg_req);
555 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
556 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
557 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
558 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
560 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
561 sport->port,
562 IB_QPT_GSI,
563 &reg_req, 0,
564 srpt_mad_send_handler,
565 srpt_mad_recv_handler,
566 sport);
567 if (IS_ERR(sport->mad_agent)) {
568 ret = PTR_ERR(sport->mad_agent);
569 sport->mad_agent = NULL;
570 goto err_query_port;
574 return 0;
576 err_query_port:
578 port_modify.set_port_cap_mask = 0;
579 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
580 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
582 err_mod_port:
584 return ret;
588 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
590 * Note: It is safe to call this function more than once for the same device.
592 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
594 struct ib_port_modify port_modify = {
595 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
597 struct srpt_port *sport;
598 int i;
600 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
601 sport = &sdev->port[i - 1];
602 WARN_ON(sport->port != i);
603 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
604 printk(KERN_ERR "disabling MAD processing failed.\n");
605 if (sport->mad_agent) {
606 ib_unregister_mad_agent(sport->mad_agent);
607 sport->mad_agent = NULL;
613 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
615 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
616 int ioctx_size, int dma_size,
617 enum dma_data_direction dir)
619 struct srpt_ioctx *ioctx;
621 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
622 if (!ioctx)
623 goto err;
625 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
626 if (!ioctx->buf)
627 goto err_free_ioctx;
629 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
630 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
631 goto err_free_buf;
633 return ioctx;
635 err_free_buf:
636 kfree(ioctx->buf);
637 err_free_ioctx:
638 kfree(ioctx);
639 err:
640 return NULL;
644 * srpt_free_ioctx() - Free an SRPT I/O context structure.
646 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
647 int dma_size, enum dma_data_direction dir)
649 if (!ioctx)
650 return;
652 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
653 kfree(ioctx->buf);
654 kfree(ioctx);
658 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
659 * @sdev: Device to allocate the I/O context ring for.
660 * @ring_size: Number of elements in the I/O context ring.
661 * @ioctx_size: I/O context size.
662 * @dma_size: DMA buffer size.
663 * @dir: DMA data direction.
665 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
666 int ring_size, int ioctx_size,
667 int dma_size, enum dma_data_direction dir)
669 struct srpt_ioctx **ring;
670 int i;
672 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
673 && ioctx_size != sizeof(struct srpt_send_ioctx));
675 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
676 if (!ring)
677 goto out;
678 for (i = 0; i < ring_size; ++i) {
679 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
680 if (!ring[i])
681 goto err;
682 ring[i]->index = i;
684 goto out;
686 err:
687 while (--i >= 0)
688 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
689 kfree(ring);
690 ring = NULL;
691 out:
692 return ring;
696 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
698 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
699 struct srpt_device *sdev, int ring_size,
700 int dma_size, enum dma_data_direction dir)
702 int i;
704 for (i = 0; i < ring_size; ++i)
705 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
706 kfree(ioctx_ring);
710 * srpt_get_cmd_state() - Get the state of a SCSI command.
712 static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
714 enum srpt_command_state state;
715 unsigned long flags;
717 BUG_ON(!ioctx);
719 spin_lock_irqsave(&ioctx->spinlock, flags);
720 state = ioctx->state;
721 spin_unlock_irqrestore(&ioctx->spinlock, flags);
722 return state;
726 * srpt_set_cmd_state() - Set the state of a SCSI command.
728 * Does not modify the state of aborted commands. Returns the previous command
729 * state.
731 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
732 enum srpt_command_state new)
734 enum srpt_command_state previous;
735 unsigned long flags;
737 BUG_ON(!ioctx);
739 spin_lock_irqsave(&ioctx->spinlock, flags);
740 previous = ioctx->state;
741 if (previous != SRPT_STATE_DONE)
742 ioctx->state = new;
743 spin_unlock_irqrestore(&ioctx->spinlock, flags);
745 return previous;
749 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
751 * Returns true if and only if the previous command state was equal to 'old'.
753 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
754 enum srpt_command_state old,
755 enum srpt_command_state new)
757 enum srpt_command_state previous;
758 unsigned long flags;
760 WARN_ON(!ioctx);
761 WARN_ON(old == SRPT_STATE_DONE);
762 WARN_ON(new == SRPT_STATE_NEW);
764 spin_lock_irqsave(&ioctx->spinlock, flags);
765 previous = ioctx->state;
766 if (previous == old)
767 ioctx->state = new;
768 spin_unlock_irqrestore(&ioctx->spinlock, flags);
769 return previous == old;
773 * srpt_post_recv() - Post an IB receive request.
775 static int srpt_post_recv(struct srpt_device *sdev,
776 struct srpt_recv_ioctx *ioctx)
778 struct ib_sge list;
779 struct ib_recv_wr wr, *bad_wr;
781 BUG_ON(!sdev);
782 wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
784 list.addr = ioctx->ioctx.dma;
785 list.length = srp_max_req_size;
786 list.lkey = sdev->mr->lkey;
788 wr.next = NULL;
789 wr.sg_list = &list;
790 wr.num_sge = 1;
792 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
796 * srpt_post_send() - Post an IB send request.
798 * Returns zero upon success and a non-zero value upon failure.
800 static int srpt_post_send(struct srpt_rdma_ch *ch,
801 struct srpt_send_ioctx *ioctx, int len)
803 struct ib_sge list;
804 struct ib_send_wr wr, *bad_wr;
805 struct srpt_device *sdev = ch->sport->sdev;
806 int ret;
808 atomic_inc(&ch->req_lim);
810 ret = -ENOMEM;
811 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
812 printk(KERN_WARNING "IB send queue full (needed 1)\n");
813 goto out;
816 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
817 DMA_TO_DEVICE);
819 list.addr = ioctx->ioctx.dma;
820 list.length = len;
821 list.lkey = sdev->mr->lkey;
823 wr.next = NULL;
824 wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
825 wr.sg_list = &list;
826 wr.num_sge = 1;
827 wr.opcode = IB_WR_SEND;
828 wr.send_flags = IB_SEND_SIGNALED;
830 ret = ib_post_send(ch->qp, &wr, &bad_wr);
832 out:
833 if (ret < 0) {
834 atomic_inc(&ch->sq_wr_avail);
835 atomic_dec(&ch->req_lim);
837 return ret;
841 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
842 * @ioctx: Pointer to the I/O context associated with the request.
843 * @srp_cmd: Pointer to the SRP_CMD request data.
844 * @dir: Pointer to the variable to which the transfer direction will be
845 * written.
846 * @data_len: Pointer to the variable to which the total data length of all
847 * descriptors in the SRP_CMD request will be written.
849 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
851 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
852 * -ENOMEM when memory allocation fails and zero upon success.
854 static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
855 struct srp_cmd *srp_cmd,
856 enum dma_data_direction *dir, u64 *data_len)
858 struct srp_indirect_buf *idb;
859 struct srp_direct_buf *db;
860 unsigned add_cdb_offset;
861 int ret;
864 * The pointer computations below will only be compiled correctly
865 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
866 * whether srp_cmd::add_data has been declared as a byte pointer.
868 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
869 && !__same_type(srp_cmd->add_data[0], (u8)0));
871 BUG_ON(!dir);
872 BUG_ON(!data_len);
874 ret = 0;
875 *data_len = 0;
878 * The lower four bits of the buffer format field contain the DATA-IN
879 * buffer descriptor format, and the highest four bits contain the
880 * DATA-OUT buffer descriptor format.
882 *dir = DMA_NONE;
883 if (srp_cmd->buf_fmt & 0xf)
884 /* DATA-IN: transfer data from target to initiator (read). */
885 *dir = DMA_FROM_DEVICE;
886 else if (srp_cmd->buf_fmt >> 4)
887 /* DATA-OUT: transfer data from initiator to target (write). */
888 *dir = DMA_TO_DEVICE;
891 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
892 * CDB LENGTH' field are reserved and the size in bytes of this field
893 * is four times the value specified in bits 3..7. Hence the "& ~3".
895 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
896 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
897 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
898 ioctx->n_rbuf = 1;
899 ioctx->rbufs = &ioctx->single_rbuf;
901 db = (struct srp_direct_buf *)(srp_cmd->add_data
902 + add_cdb_offset);
903 memcpy(ioctx->rbufs, db, sizeof *db);
904 *data_len = be32_to_cpu(db->len);
905 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
906 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
907 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
908 + add_cdb_offset);
910 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
912 if (ioctx->n_rbuf >
913 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
914 printk(KERN_ERR "received unsupported SRP_CMD request"
915 " type (%u out + %u in != %u / %zu)\n",
916 srp_cmd->data_out_desc_cnt,
917 srp_cmd->data_in_desc_cnt,
918 be32_to_cpu(idb->table_desc.len),
919 sizeof(*db));
920 ioctx->n_rbuf = 0;
921 ret = -EINVAL;
922 goto out;
925 if (ioctx->n_rbuf == 1)
926 ioctx->rbufs = &ioctx->single_rbuf;
927 else {
928 ioctx->rbufs =
929 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
930 if (!ioctx->rbufs) {
931 ioctx->n_rbuf = 0;
932 ret = -ENOMEM;
933 goto out;
937 db = idb->desc_list;
938 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
939 *data_len = be32_to_cpu(idb->len);
941 out:
942 return ret;
946 * srpt_init_ch_qp() - Initialize queue pair attributes.
948 * Initialized the attributes of queue pair 'qp' by allowing local write,
949 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
951 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
953 struct ib_qp_attr *attr;
954 int ret;
956 attr = kzalloc(sizeof *attr, GFP_KERNEL);
957 if (!attr)
958 return -ENOMEM;
960 attr->qp_state = IB_QPS_INIT;
961 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
962 IB_ACCESS_REMOTE_WRITE;
963 attr->port_num = ch->sport->port;
964 attr->pkey_index = 0;
966 ret = ib_modify_qp(qp, attr,
967 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
968 IB_QP_PKEY_INDEX);
970 kfree(attr);
971 return ret;
975 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
976 * @ch: channel of the queue pair.
977 * @qp: queue pair to change the state of.
979 * Returns zero upon success and a negative value upon failure.
981 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
982 * If this structure ever becomes larger, it might be necessary to allocate
983 * it dynamically instead of on the stack.
985 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
987 struct ib_qp_attr qp_attr;
988 int attr_mask;
989 int ret;
991 qp_attr.qp_state = IB_QPS_RTR;
992 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
993 if (ret)
994 goto out;
996 qp_attr.max_dest_rd_atomic = 4;
998 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1000 out:
1001 return ret;
1005 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1006 * @ch: channel of the queue pair.
1007 * @qp: queue pair to change the state of.
1009 * Returns zero upon success and a negative value upon failure.
1011 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1012 * If this structure ever becomes larger, it might be necessary to allocate
1013 * it dynamically instead of on the stack.
1015 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1017 struct ib_qp_attr qp_attr;
1018 int attr_mask;
1019 int ret;
1021 qp_attr.qp_state = IB_QPS_RTS;
1022 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1023 if (ret)
1024 goto out;
1026 qp_attr.max_rd_atomic = 4;
1028 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1030 out:
1031 return ret;
1035 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1037 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1039 struct ib_qp_attr qp_attr;
1041 qp_attr.qp_state = IB_QPS_ERR;
1042 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1046 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1048 static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1049 struct srpt_send_ioctx *ioctx)
1051 struct scatterlist *sg;
1052 enum dma_data_direction dir;
1054 BUG_ON(!ch);
1055 BUG_ON(!ioctx);
1056 BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
1058 while (ioctx->n_rdma)
1059 kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
1061 kfree(ioctx->rdma_ius);
1062 ioctx->rdma_ius = NULL;
1064 if (ioctx->mapped_sg_count) {
1065 sg = ioctx->sg;
1066 WARN_ON(!sg);
1067 dir = ioctx->cmd.data_direction;
1068 BUG_ON(dir == DMA_NONE);
1069 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1070 opposite_dma_dir(dir));
1071 ioctx->mapped_sg_count = 0;
1076 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1078 static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1079 struct srpt_send_ioctx *ioctx)
1081 struct ib_device *dev = ch->sport->sdev->device;
1082 struct se_cmd *cmd;
1083 struct scatterlist *sg, *sg_orig;
1084 int sg_cnt;
1085 enum dma_data_direction dir;
1086 struct rdma_iu *riu;
1087 struct srp_direct_buf *db;
1088 dma_addr_t dma_addr;
1089 struct ib_sge *sge;
1090 u64 raddr;
1091 u32 rsize;
1092 u32 tsize;
1093 u32 dma_len;
1094 int count, nrdma;
1095 int i, j, k;
1097 BUG_ON(!ch);
1098 BUG_ON(!ioctx);
1099 cmd = &ioctx->cmd;
1100 dir = cmd->data_direction;
1101 BUG_ON(dir == DMA_NONE);
1103 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1104 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
1106 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1107 opposite_dma_dir(dir));
1108 if (unlikely(!count))
1109 return -EAGAIN;
1111 ioctx->mapped_sg_count = count;
1113 if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1114 nrdma = ioctx->n_rdma_ius;
1115 else {
1116 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1117 + ioctx->n_rbuf;
1119 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
1120 if (!ioctx->rdma_ius)
1121 goto free_mem;
1123 ioctx->n_rdma_ius = nrdma;
1126 db = ioctx->rbufs;
1127 tsize = cmd->data_length;
1128 dma_len = ib_sg_dma_len(dev, &sg[0]);
1129 riu = ioctx->rdma_ius;
1132 * For each remote desc - calculate the #ib_sge.
1133 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1134 * each remote desc rdma_iu is required a rdma wr;
1135 * else
1136 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1137 * another rdma wr
1139 for (i = 0, j = 0;
1140 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1141 rsize = be32_to_cpu(db->len);
1142 raddr = be64_to_cpu(db->va);
1143 riu->raddr = raddr;
1144 riu->rkey = be32_to_cpu(db->key);
1145 riu->sge_cnt = 0;
1147 /* calculate how many sge required for this remote_buf */
1148 while (rsize > 0 && tsize > 0) {
1150 if (rsize >= dma_len) {
1151 tsize -= dma_len;
1152 rsize -= dma_len;
1153 raddr += dma_len;
1155 if (tsize > 0) {
1156 ++j;
1157 if (j < count) {
1158 sg = sg_next(sg);
1159 dma_len = ib_sg_dma_len(
1160 dev, sg);
1163 } else {
1164 tsize -= rsize;
1165 dma_len -= rsize;
1166 rsize = 0;
1169 ++riu->sge_cnt;
1171 if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1172 ++ioctx->n_rdma;
1173 riu->sge =
1174 kmalloc(riu->sge_cnt * sizeof *riu->sge,
1175 GFP_KERNEL);
1176 if (!riu->sge)
1177 goto free_mem;
1179 ++riu;
1180 riu->sge_cnt = 0;
1181 riu->raddr = raddr;
1182 riu->rkey = be32_to_cpu(db->key);
1186 ++ioctx->n_rdma;
1187 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1188 GFP_KERNEL);
1189 if (!riu->sge)
1190 goto free_mem;
1193 db = ioctx->rbufs;
1194 tsize = cmd->data_length;
1195 riu = ioctx->rdma_ius;
1196 sg = sg_orig;
1197 dma_len = ib_sg_dma_len(dev, &sg[0]);
1198 dma_addr = ib_sg_dma_address(dev, &sg[0]);
1200 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1201 for (i = 0, j = 0;
1202 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1203 rsize = be32_to_cpu(db->len);
1204 sge = riu->sge;
1205 k = 0;
1207 while (rsize > 0 && tsize > 0) {
1208 sge->addr = dma_addr;
1209 sge->lkey = ch->sport->sdev->mr->lkey;
1211 if (rsize >= dma_len) {
1212 sge->length =
1213 (tsize < dma_len) ? tsize : dma_len;
1214 tsize -= dma_len;
1215 rsize -= dma_len;
1217 if (tsize > 0) {
1218 ++j;
1219 if (j < count) {
1220 sg = sg_next(sg);
1221 dma_len = ib_sg_dma_len(
1222 dev, sg);
1223 dma_addr = ib_sg_dma_address(
1224 dev, sg);
1227 } else {
1228 sge->length = (tsize < rsize) ? tsize : rsize;
1229 tsize -= rsize;
1230 dma_len -= rsize;
1231 dma_addr += rsize;
1232 rsize = 0;
1235 ++k;
1236 if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1237 ++riu;
1238 sge = riu->sge;
1239 k = 0;
1240 } else if (rsize > 0 && tsize > 0)
1241 ++sge;
1245 return 0;
1247 free_mem:
1248 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1250 return -ENOMEM;
1254 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1256 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1258 struct srpt_send_ioctx *ioctx;
1259 unsigned long flags;
1261 BUG_ON(!ch);
1263 ioctx = NULL;
1264 spin_lock_irqsave(&ch->spinlock, flags);
1265 if (!list_empty(&ch->free_list)) {
1266 ioctx = list_first_entry(&ch->free_list,
1267 struct srpt_send_ioctx, free_list);
1268 list_del(&ioctx->free_list);
1270 spin_unlock_irqrestore(&ch->spinlock, flags);
1272 if (!ioctx)
1273 return ioctx;
1275 BUG_ON(ioctx->ch != ch);
1276 spin_lock_init(&ioctx->spinlock);
1277 ioctx->state = SRPT_STATE_NEW;
1278 ioctx->n_rbuf = 0;
1279 ioctx->rbufs = NULL;
1280 ioctx->n_rdma = 0;
1281 ioctx->n_rdma_ius = 0;
1282 ioctx->rdma_ius = NULL;
1283 ioctx->mapped_sg_count = 0;
1284 init_completion(&ioctx->tx_done);
1285 ioctx->queue_status_only = false;
1287 * transport_init_se_cmd() does not initialize all fields, so do it
1288 * here.
1290 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1291 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1293 return ioctx;
1297 * srpt_abort_cmd() - Abort a SCSI command.
1298 * @ioctx: I/O context associated with the SCSI command.
1299 * @context: Preferred execution context.
1301 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1303 enum srpt_command_state state;
1304 unsigned long flags;
1306 BUG_ON(!ioctx);
1309 * If the command is in a state where the target core is waiting for
1310 * the ib_srpt driver, change the state to the next state. Changing
1311 * the state of the command from SRPT_STATE_NEED_DATA to
1312 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1313 * function a second time.
1316 spin_lock_irqsave(&ioctx->spinlock, flags);
1317 state = ioctx->state;
1318 switch (state) {
1319 case SRPT_STATE_NEED_DATA:
1320 ioctx->state = SRPT_STATE_DATA_IN;
1321 break;
1322 case SRPT_STATE_DATA_IN:
1323 case SRPT_STATE_CMD_RSP_SENT:
1324 case SRPT_STATE_MGMT_RSP_SENT:
1325 ioctx->state = SRPT_STATE_DONE;
1326 break;
1327 default:
1328 break;
1330 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1332 if (state == SRPT_STATE_DONE) {
1333 struct srpt_rdma_ch *ch = ioctx->ch;
1335 BUG_ON(ch->sess == NULL);
1337 target_put_sess_cmd(ch->sess, &ioctx->cmd);
1338 goto out;
1341 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1342 ioctx->tag);
1344 switch (state) {
1345 case SRPT_STATE_NEW:
1346 case SRPT_STATE_DATA_IN:
1347 case SRPT_STATE_MGMT:
1349 * Do nothing - defer abort processing until
1350 * srpt_queue_response() is invoked.
1352 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1353 break;
1354 case SRPT_STATE_NEED_DATA:
1355 /* DMA_TO_DEVICE (write) - RDMA read error. */
1357 /* XXX(hch): this is a horrible layering violation.. */
1358 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1359 ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
1360 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
1361 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
1363 complete(&ioctx->cmd.transport_lun_stop_comp);
1364 break;
1365 case SRPT_STATE_CMD_RSP_SENT:
1367 * SRP_RSP sending failed or the SRP_RSP send completion has
1368 * not been received in time.
1370 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
1371 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1372 ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
1373 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
1374 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
1375 break;
1376 case SRPT_STATE_MGMT_RSP_SENT:
1377 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1378 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
1379 break;
1380 default:
1381 WARN(1, "Unexpected command state (%d)", state);
1382 break;
1385 out:
1386 return state;
1390 * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1392 static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1394 struct srpt_send_ioctx *ioctx;
1395 enum srpt_command_state state;
1396 struct se_cmd *cmd;
1397 u32 index;
1399 atomic_inc(&ch->sq_wr_avail);
1401 index = idx_from_wr_id(wr_id);
1402 ioctx = ch->ioctx_ring[index];
1403 state = srpt_get_cmd_state(ioctx);
1404 cmd = &ioctx->cmd;
1406 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1407 && state != SRPT_STATE_MGMT_RSP_SENT
1408 && state != SRPT_STATE_NEED_DATA
1409 && state != SRPT_STATE_DONE);
1411 /* If SRP_RSP sending failed, undo the ch->req_lim change. */
1412 if (state == SRPT_STATE_CMD_RSP_SENT
1413 || state == SRPT_STATE_MGMT_RSP_SENT)
1414 atomic_dec(&ch->req_lim);
1416 srpt_abort_cmd(ioctx);
1420 * srpt_handle_send_comp() - Process an IB send completion notification.
1422 static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1423 struct srpt_send_ioctx *ioctx)
1425 enum srpt_command_state state;
1427 atomic_inc(&ch->sq_wr_avail);
1429 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1431 if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1432 && state != SRPT_STATE_MGMT_RSP_SENT
1433 && state != SRPT_STATE_DONE))
1434 pr_debug("state = %d\n", state);
1436 if (state != SRPT_STATE_DONE) {
1437 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1438 transport_generic_free_cmd(&ioctx->cmd, 0);
1439 } else {
1440 printk(KERN_ERR "IB completion has been received too late for"
1441 " wr_id = %u.\n", ioctx->ioctx.index);
1446 * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1448 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1449 * the data that has been transferred via IB RDMA had to be postponed until the
1450 * check_stop_free() callback. None of this is necessary anymore and needs to
1451 * be cleaned up.
1453 static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1454 struct srpt_send_ioctx *ioctx,
1455 enum srpt_opcode opcode)
1457 WARN_ON(ioctx->n_rdma <= 0);
1458 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1460 if (opcode == SRPT_RDMA_READ_LAST) {
1461 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1462 SRPT_STATE_DATA_IN))
1463 target_execute_cmd(&ioctx->cmd);
1464 else
1465 printk(KERN_ERR "%s[%d]: wrong state = %d\n", __func__,
1466 __LINE__, srpt_get_cmd_state(ioctx));
1467 } else if (opcode == SRPT_RDMA_ABORT) {
1468 ioctx->rdma_aborted = true;
1469 } else {
1470 WARN(true, "unexpected opcode %d\n", opcode);
1475 * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1477 static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1478 struct srpt_send_ioctx *ioctx,
1479 enum srpt_opcode opcode)
1481 struct se_cmd *cmd;
1482 enum srpt_command_state state;
1483 unsigned long flags;
1485 cmd = &ioctx->cmd;
1486 state = srpt_get_cmd_state(ioctx);
1487 switch (opcode) {
1488 case SRPT_RDMA_READ_LAST:
1489 if (ioctx->n_rdma <= 0) {
1490 printk(KERN_ERR "Received invalid RDMA read"
1491 " error completion with idx %d\n",
1492 ioctx->ioctx.index);
1493 break;
1495 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1496 if (state == SRPT_STATE_NEED_DATA)
1497 srpt_abort_cmd(ioctx);
1498 else
1499 printk(KERN_ERR "%s[%d]: wrong state = %d\n",
1500 __func__, __LINE__, state);
1501 break;
1502 case SRPT_RDMA_WRITE_LAST:
1503 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1504 ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
1505 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
1506 break;
1507 default:
1508 printk(KERN_ERR "%s[%d]: opcode = %u\n", __func__,
1509 __LINE__, opcode);
1510 break;
1515 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1516 * @ch: RDMA channel through which the request has been received.
1517 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1518 * be built in the buffer ioctx->buf points at and hence this function will
1519 * overwrite the request data.
1520 * @tag: tag of the request for which this response is being generated.
1521 * @status: value for the STATUS field of the SRP_RSP information unit.
1523 * Returns the size in bytes of the SRP_RSP response.
1525 * An SRP_RSP response contains a SCSI status or service response. See also
1526 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1527 * response. See also SPC-2 for more information about sense data.
1529 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1530 struct srpt_send_ioctx *ioctx, u64 tag,
1531 int status)
1533 struct srp_rsp *srp_rsp;
1534 const u8 *sense_data;
1535 int sense_data_len, max_sense_len;
1538 * The lowest bit of all SAM-3 status codes is zero (see also
1539 * paragraph 5.3 in SAM-3).
1541 WARN_ON(status & 1);
1543 srp_rsp = ioctx->ioctx.buf;
1544 BUG_ON(!srp_rsp);
1546 sense_data = ioctx->sense_data;
1547 sense_data_len = ioctx->cmd.scsi_sense_length;
1548 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1550 memset(srp_rsp, 0, sizeof *srp_rsp);
1551 srp_rsp->opcode = SRP_RSP;
1552 srp_rsp->req_lim_delta =
1553 __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1554 srp_rsp->tag = tag;
1555 srp_rsp->status = status;
1557 if (sense_data_len) {
1558 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1559 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1560 if (sense_data_len > max_sense_len) {
1561 printk(KERN_WARNING "truncated sense data from %d to %d"
1562 " bytes\n", sense_data_len, max_sense_len);
1563 sense_data_len = max_sense_len;
1566 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1567 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1568 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1571 return sizeof(*srp_rsp) + sense_data_len;
1575 * srpt_build_tskmgmt_rsp() - Build a task management response.
1576 * @ch: RDMA channel through which the request has been received.
1577 * @ioctx: I/O context in which the SRP_RSP response will be built.
1578 * @rsp_code: RSP_CODE that will be stored in the response.
1579 * @tag: Tag of the request for which this response is being generated.
1581 * Returns the size in bytes of the SRP_RSP response.
1583 * An SRP_RSP response contains a SCSI status or service response. See also
1584 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1585 * response.
1587 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1588 struct srpt_send_ioctx *ioctx,
1589 u8 rsp_code, u64 tag)
1591 struct srp_rsp *srp_rsp;
1592 int resp_data_len;
1593 int resp_len;
1595 resp_data_len = 4;
1596 resp_len = sizeof(*srp_rsp) + resp_data_len;
1598 srp_rsp = ioctx->ioctx.buf;
1599 BUG_ON(!srp_rsp);
1600 memset(srp_rsp, 0, sizeof *srp_rsp);
1602 srp_rsp->opcode = SRP_RSP;
1603 srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
1604 + atomic_xchg(&ch->req_lim_delta, 0));
1605 srp_rsp->tag = tag;
1607 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1608 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1609 srp_rsp->data[3] = rsp_code;
1611 return resp_len;
1614 #define NO_SUCH_LUN ((uint64_t)-1LL)
1617 * SCSI LUN addressing method. See also SAM-2 and the section about
1618 * eight byte LUNs.
1620 enum scsi_lun_addr_method {
1621 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1622 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1623 SCSI_LUN_ADDR_METHOD_LUN = 2,
1624 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1628 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1630 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1631 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1632 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1634 static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1636 uint64_t res = NO_SUCH_LUN;
1637 int addressing_method;
1639 if (unlikely(len < 2)) {
1640 printk(KERN_ERR "Illegal LUN length %d, expected 2 bytes or "
1641 "more", len);
1642 goto out;
1645 switch (len) {
1646 case 8:
1647 if ((*((__be64 *)lun) &
1648 __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1649 goto out_err;
1650 break;
1651 case 4:
1652 if (*((__be16 *)&lun[2]) != 0)
1653 goto out_err;
1654 break;
1655 case 6:
1656 if (*((__be32 *)&lun[2]) != 0)
1657 goto out_err;
1658 break;
1659 case 2:
1660 break;
1661 default:
1662 goto out_err;
1665 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1666 switch (addressing_method) {
1667 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1668 case SCSI_LUN_ADDR_METHOD_FLAT:
1669 case SCSI_LUN_ADDR_METHOD_LUN:
1670 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1671 break;
1673 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1674 default:
1675 printk(KERN_ERR "Unimplemented LUN addressing method %u",
1676 addressing_method);
1677 break;
1680 out:
1681 return res;
1683 out_err:
1684 printk(KERN_ERR "Support for multi-level LUNs has not yet been"
1685 " implemented");
1686 goto out;
1689 static int srpt_check_stop_free(struct se_cmd *cmd)
1691 struct srpt_send_ioctx *ioctx = container_of(cmd,
1692 struct srpt_send_ioctx, cmd);
1694 return target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
1698 * srpt_handle_cmd() - Process SRP_CMD.
1700 static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1701 struct srpt_recv_ioctx *recv_ioctx,
1702 struct srpt_send_ioctx *send_ioctx)
1704 struct se_cmd *cmd;
1705 struct srp_cmd *srp_cmd;
1706 uint64_t unpacked_lun;
1707 u64 data_len;
1708 enum dma_data_direction dir;
1709 sense_reason_t ret;
1710 int rc;
1712 BUG_ON(!send_ioctx);
1714 srp_cmd = recv_ioctx->ioctx.buf;
1715 cmd = &send_ioctx->cmd;
1716 send_ioctx->tag = srp_cmd->tag;
1718 switch (srp_cmd->task_attr) {
1719 case SRP_CMD_SIMPLE_Q:
1720 cmd->sam_task_attr = MSG_SIMPLE_TAG;
1721 break;
1722 case SRP_CMD_ORDERED_Q:
1723 default:
1724 cmd->sam_task_attr = MSG_ORDERED_TAG;
1725 break;
1726 case SRP_CMD_HEAD_OF_Q:
1727 cmd->sam_task_attr = MSG_HEAD_TAG;
1728 break;
1729 case SRP_CMD_ACA:
1730 cmd->sam_task_attr = MSG_ACA_TAG;
1731 break;
1734 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
1735 printk(KERN_ERR "0x%llx: parsing SRP descriptor table failed.\n",
1736 srp_cmd->tag);
1737 ret = TCM_INVALID_CDB_FIELD;
1738 goto send_sense;
1741 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1742 sizeof(srp_cmd->lun));
1743 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1744 &send_ioctx->sense_data[0], unpacked_lun, data_len,
1745 MSG_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
1746 if (rc != 0) {
1747 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1748 goto send_sense;
1750 return 0;
1752 send_sense:
1753 transport_send_check_condition_and_sense(cmd, ret, 0);
1754 return -1;
1757 static int srp_tmr_to_tcm(int fn)
1759 switch (fn) {
1760 case SRP_TSK_ABORT_TASK:
1761 return TMR_ABORT_TASK;
1762 case SRP_TSK_ABORT_TASK_SET:
1763 return TMR_ABORT_TASK_SET;
1764 case SRP_TSK_CLEAR_TASK_SET:
1765 return TMR_CLEAR_TASK_SET;
1766 case SRP_TSK_LUN_RESET:
1767 return TMR_LUN_RESET;
1768 case SRP_TSK_CLEAR_ACA:
1769 return TMR_CLEAR_ACA;
1770 default:
1771 return -1;
1776 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1778 * Returns 0 if and only if the request will be processed by the target core.
1780 * For more information about SRP_TSK_MGMT information units, see also section
1781 * 6.7 in the SRP r16a document.
1783 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1784 struct srpt_recv_ioctx *recv_ioctx,
1785 struct srpt_send_ioctx *send_ioctx)
1787 struct srp_tsk_mgmt *srp_tsk;
1788 struct se_cmd *cmd;
1789 struct se_session *sess = ch->sess;
1790 uint64_t unpacked_lun;
1791 int tcm_tmr;
1792 int rc;
1794 BUG_ON(!send_ioctx);
1796 srp_tsk = recv_ioctx->ioctx.buf;
1797 cmd = &send_ioctx->cmd;
1799 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1800 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1801 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1803 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1804 send_ioctx->tag = srp_tsk->tag;
1805 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1806 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1807 sizeof(srp_tsk->lun));
1808 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
1809 srp_tsk, tcm_tmr, GFP_KERNEL, srp_tsk->task_tag,
1810 TARGET_SCF_ACK_KREF);
1811 if (rc != 0) {
1812 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1813 goto fail;
1815 return;
1816 fail:
1817 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
1821 * srpt_handle_new_iu() - Process a newly received information unit.
1822 * @ch: RDMA channel through which the information unit has been received.
1823 * @ioctx: SRPT I/O context associated with the information unit.
1825 static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1826 struct srpt_recv_ioctx *recv_ioctx,
1827 struct srpt_send_ioctx *send_ioctx)
1829 struct srp_cmd *srp_cmd;
1830 enum rdma_ch_state ch_state;
1832 BUG_ON(!ch);
1833 BUG_ON(!recv_ioctx);
1835 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1836 recv_ioctx->ioctx.dma, srp_max_req_size,
1837 DMA_FROM_DEVICE);
1839 ch_state = srpt_get_ch_state(ch);
1840 if (unlikely(ch_state == CH_CONNECTING)) {
1841 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1842 goto out;
1845 if (unlikely(ch_state != CH_LIVE))
1846 goto out;
1848 srp_cmd = recv_ioctx->ioctx.buf;
1849 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1850 if (!send_ioctx)
1851 send_ioctx = srpt_get_send_ioctx(ch);
1852 if (unlikely(!send_ioctx)) {
1853 list_add_tail(&recv_ioctx->wait_list,
1854 &ch->cmd_wait_list);
1855 goto out;
1859 switch (srp_cmd->opcode) {
1860 case SRP_CMD:
1861 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1862 break;
1863 case SRP_TSK_MGMT:
1864 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1865 break;
1866 case SRP_I_LOGOUT:
1867 printk(KERN_ERR "Not yet implemented: SRP_I_LOGOUT\n");
1868 break;
1869 case SRP_CRED_RSP:
1870 pr_debug("received SRP_CRED_RSP\n");
1871 break;
1872 case SRP_AER_RSP:
1873 pr_debug("received SRP_AER_RSP\n");
1874 break;
1875 case SRP_RSP:
1876 printk(KERN_ERR "Received SRP_RSP\n");
1877 break;
1878 default:
1879 printk(KERN_ERR "received IU with unknown opcode 0x%x\n",
1880 srp_cmd->opcode);
1881 break;
1884 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1885 out:
1886 return;
1889 static void srpt_process_rcv_completion(struct ib_cq *cq,
1890 struct srpt_rdma_ch *ch,
1891 struct ib_wc *wc)
1893 struct srpt_device *sdev = ch->sport->sdev;
1894 struct srpt_recv_ioctx *ioctx;
1895 u32 index;
1897 index = idx_from_wr_id(wc->wr_id);
1898 if (wc->status == IB_WC_SUCCESS) {
1899 int req_lim;
1901 req_lim = atomic_dec_return(&ch->req_lim);
1902 if (unlikely(req_lim < 0))
1903 printk(KERN_ERR "req_lim = %d < 0\n", req_lim);
1904 ioctx = sdev->ioctx_ring[index];
1905 srpt_handle_new_iu(ch, ioctx, NULL);
1906 } else {
1907 printk(KERN_INFO "receiving failed for idx %u with status %d\n",
1908 index, wc->status);
1913 * srpt_process_send_completion() - Process an IB send completion.
1915 * Note: Although this has not yet been observed during tests, at least in
1916 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1917 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1918 * value in each response is set to one, and it is possible that this response
1919 * makes the initiator send a new request before the send completion for that
1920 * response has been processed. This could e.g. happen if the call to
1921 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1922 * if IB retransmission causes generation of the send completion to be
1923 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1924 * are queued on cmd_wait_list. The code below processes these delayed
1925 * requests one at a time.
1927 static void srpt_process_send_completion(struct ib_cq *cq,
1928 struct srpt_rdma_ch *ch,
1929 struct ib_wc *wc)
1931 struct srpt_send_ioctx *send_ioctx;
1932 uint32_t index;
1933 enum srpt_opcode opcode;
1935 index = idx_from_wr_id(wc->wr_id);
1936 opcode = opcode_from_wr_id(wc->wr_id);
1937 send_ioctx = ch->ioctx_ring[index];
1938 if (wc->status == IB_WC_SUCCESS) {
1939 if (opcode == SRPT_SEND)
1940 srpt_handle_send_comp(ch, send_ioctx);
1941 else {
1942 WARN_ON(opcode != SRPT_RDMA_ABORT &&
1943 wc->opcode != IB_WC_RDMA_READ);
1944 srpt_handle_rdma_comp(ch, send_ioctx, opcode);
1946 } else {
1947 if (opcode == SRPT_SEND) {
1948 printk(KERN_INFO "sending response for idx %u failed"
1949 " with status %d\n", index, wc->status);
1950 srpt_handle_send_err_comp(ch, wc->wr_id);
1951 } else if (opcode != SRPT_RDMA_MID) {
1952 printk(KERN_INFO "RDMA t %d for idx %u failed with"
1953 " status %d", opcode, index, wc->status);
1954 srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
1958 while (unlikely(opcode == SRPT_SEND
1959 && !list_empty(&ch->cmd_wait_list)
1960 && srpt_get_ch_state(ch) == CH_LIVE
1961 && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
1962 struct srpt_recv_ioctx *recv_ioctx;
1964 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1965 struct srpt_recv_ioctx,
1966 wait_list);
1967 list_del(&recv_ioctx->wait_list);
1968 srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
1972 static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
1974 struct ib_wc *const wc = ch->wc;
1975 int i, n;
1977 WARN_ON(cq != ch->cq);
1979 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1980 while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
1981 for (i = 0; i < n; i++) {
1982 if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
1983 srpt_process_rcv_completion(cq, ch, &wc[i]);
1984 else
1985 srpt_process_send_completion(cq, ch, &wc[i]);
1991 * srpt_completion() - IB completion queue callback function.
1993 * Notes:
1994 * - It is guaranteed that a completion handler will never be invoked
1995 * concurrently on two different CPUs for the same completion queue. See also
1996 * Documentation/infiniband/core_locking.txt and the implementation of
1997 * handle_edge_irq() in kernel/irq/chip.c.
1998 * - When threaded IRQs are enabled, completion handlers are invoked in thread
1999 * context instead of interrupt context.
2001 static void srpt_completion(struct ib_cq *cq, void *ctx)
2003 struct srpt_rdma_ch *ch = ctx;
2005 wake_up_interruptible(&ch->wait_queue);
2008 static int srpt_compl_thread(void *arg)
2010 struct srpt_rdma_ch *ch;
2012 /* Hibernation / freezing of the SRPT kernel thread is not supported. */
2013 current->flags |= PF_NOFREEZE;
2015 ch = arg;
2016 BUG_ON(!ch);
2017 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) started\n",
2018 ch->sess_name, ch->thread->comm, current->pid);
2019 while (!kthread_should_stop()) {
2020 wait_event_interruptible(ch->wait_queue,
2021 (srpt_process_completion(ch->cq, ch),
2022 kthread_should_stop()));
2024 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) stopped\n",
2025 ch->sess_name, ch->thread->comm, current->pid);
2026 return 0;
2030 * srpt_create_ch_ib() - Create receive and send completion queues.
2032 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2034 struct ib_qp_init_attr *qp_init;
2035 struct srpt_port *sport = ch->sport;
2036 struct srpt_device *sdev = sport->sdev;
2037 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
2038 int ret;
2040 WARN_ON(ch->rq_size < 1);
2042 ret = -ENOMEM;
2043 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2044 if (!qp_init)
2045 goto out;
2047 retry:
2048 ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
2049 ch->rq_size + srp_sq_size, 0);
2050 if (IS_ERR(ch->cq)) {
2051 ret = PTR_ERR(ch->cq);
2052 printk(KERN_ERR "failed to create CQ cqe= %d ret= %d\n",
2053 ch->rq_size + srp_sq_size, ret);
2054 goto out;
2057 qp_init->qp_context = (void *)ch;
2058 qp_init->event_handler
2059 = (void(*)(struct ib_event *, void*))srpt_qp_event;
2060 qp_init->send_cq = ch->cq;
2061 qp_init->recv_cq = ch->cq;
2062 qp_init->srq = sdev->srq;
2063 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2064 qp_init->qp_type = IB_QPT_RC;
2065 qp_init->cap.max_send_wr = srp_sq_size;
2066 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2068 ch->qp = ib_create_qp(sdev->pd, qp_init);
2069 if (IS_ERR(ch->qp)) {
2070 ret = PTR_ERR(ch->qp);
2071 if (ret == -ENOMEM) {
2072 srp_sq_size /= 2;
2073 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
2074 ib_destroy_cq(ch->cq);
2075 goto retry;
2078 printk(KERN_ERR "failed to create_qp ret= %d\n", ret);
2079 goto err_destroy_cq;
2082 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2084 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2085 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2086 qp_init->cap.max_send_wr, ch->cm_id);
2088 ret = srpt_init_ch_qp(ch, ch->qp);
2089 if (ret)
2090 goto err_destroy_qp;
2092 init_waitqueue_head(&ch->wait_queue);
2094 pr_debug("creating thread for session %s\n", ch->sess_name);
2096 ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2097 if (IS_ERR(ch->thread)) {
2098 printk(KERN_ERR "failed to create kernel thread %ld\n",
2099 PTR_ERR(ch->thread));
2100 ch->thread = NULL;
2101 goto err_destroy_qp;
2104 out:
2105 kfree(qp_init);
2106 return ret;
2108 err_destroy_qp:
2109 ib_destroy_qp(ch->qp);
2110 err_destroy_cq:
2111 ib_destroy_cq(ch->cq);
2112 goto out;
2115 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2117 if (ch->thread)
2118 kthread_stop(ch->thread);
2120 ib_destroy_qp(ch->qp);
2121 ib_destroy_cq(ch->cq);
2125 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2127 * Reset the QP and make sure all resources associated with the channel will
2128 * be deallocated at an appropriate time.
2130 * Note: The caller must hold ch->sport->sdev->spinlock.
2132 static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2134 struct srpt_device *sdev;
2135 enum rdma_ch_state prev_state;
2136 unsigned long flags;
2138 sdev = ch->sport->sdev;
2140 spin_lock_irqsave(&ch->spinlock, flags);
2141 prev_state = ch->state;
2142 switch (prev_state) {
2143 case CH_CONNECTING:
2144 case CH_LIVE:
2145 ch->state = CH_DISCONNECTING;
2146 break;
2147 default:
2148 break;
2150 spin_unlock_irqrestore(&ch->spinlock, flags);
2152 switch (prev_state) {
2153 case CH_CONNECTING:
2154 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2155 NULL, 0);
2156 /* fall through */
2157 case CH_LIVE:
2158 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
2159 printk(KERN_ERR "sending CM DREQ failed.\n");
2160 break;
2161 case CH_DISCONNECTING:
2162 break;
2163 case CH_DRAINING:
2164 case CH_RELEASING:
2165 break;
2170 * srpt_close_ch() - Close an RDMA channel.
2172 static void srpt_close_ch(struct srpt_rdma_ch *ch)
2174 struct srpt_device *sdev;
2176 sdev = ch->sport->sdev;
2177 spin_lock_irq(&sdev->spinlock);
2178 __srpt_close_ch(ch);
2179 spin_unlock_irq(&sdev->spinlock);
2183 * srpt_shutdown_session() - Whether or not a session may be shut down.
2185 static int srpt_shutdown_session(struct se_session *se_sess)
2187 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2188 unsigned long flags;
2190 spin_lock_irqsave(&ch->spinlock, flags);
2191 if (ch->in_shutdown) {
2192 spin_unlock_irqrestore(&ch->spinlock, flags);
2193 return true;
2196 ch->in_shutdown = true;
2197 target_sess_cmd_list_set_waiting(se_sess);
2198 spin_unlock_irqrestore(&ch->spinlock, flags);
2200 return true;
2204 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2205 * @cm_id: Pointer to the CM ID of the channel to be drained.
2207 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2208 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2209 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2210 * waits until all target sessions for the associated IB device have been
2211 * unregistered and target session registration involves a call to
2212 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2213 * this function has finished).
2215 static void srpt_drain_channel(struct ib_cm_id *cm_id)
2217 struct srpt_device *sdev;
2218 struct srpt_rdma_ch *ch;
2219 int ret;
2220 bool do_reset = false;
2222 WARN_ON_ONCE(irqs_disabled());
2224 sdev = cm_id->context;
2225 BUG_ON(!sdev);
2226 spin_lock_irq(&sdev->spinlock);
2227 list_for_each_entry(ch, &sdev->rch_list, list) {
2228 if (ch->cm_id == cm_id) {
2229 do_reset = srpt_test_and_set_ch_state(ch,
2230 CH_CONNECTING, CH_DRAINING) ||
2231 srpt_test_and_set_ch_state(ch,
2232 CH_LIVE, CH_DRAINING) ||
2233 srpt_test_and_set_ch_state(ch,
2234 CH_DISCONNECTING, CH_DRAINING);
2235 break;
2238 spin_unlock_irq(&sdev->spinlock);
2240 if (do_reset) {
2241 if (ch->sess)
2242 srpt_shutdown_session(ch->sess);
2244 ret = srpt_ch_qp_err(ch);
2245 if (ret < 0)
2246 printk(KERN_ERR "Setting queue pair in error state"
2247 " failed: %d\n", ret);
2252 * srpt_find_channel() - Look up an RDMA channel.
2253 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2255 * Return NULL if no matching RDMA channel has been found.
2257 static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2258 struct ib_cm_id *cm_id)
2260 struct srpt_rdma_ch *ch;
2261 bool found;
2263 WARN_ON_ONCE(irqs_disabled());
2264 BUG_ON(!sdev);
2266 found = false;
2267 spin_lock_irq(&sdev->spinlock);
2268 list_for_each_entry(ch, &sdev->rch_list, list) {
2269 if (ch->cm_id == cm_id) {
2270 found = true;
2271 break;
2274 spin_unlock_irq(&sdev->spinlock);
2276 return found ? ch : NULL;
2280 * srpt_release_channel() - Release channel resources.
2282 * Schedules the actual release because:
2283 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2284 * trigger a deadlock.
2285 * - It is not safe to call TCM transport_* functions from interrupt context.
2287 static void srpt_release_channel(struct srpt_rdma_ch *ch)
2289 schedule_work(&ch->release_work);
2292 static void srpt_release_channel_work(struct work_struct *w)
2294 struct srpt_rdma_ch *ch;
2295 struct srpt_device *sdev;
2296 struct se_session *se_sess;
2298 ch = container_of(w, struct srpt_rdma_ch, release_work);
2299 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2300 ch->release_done);
2302 sdev = ch->sport->sdev;
2303 BUG_ON(!sdev);
2305 se_sess = ch->sess;
2306 BUG_ON(!se_sess);
2308 target_wait_for_sess_cmds(se_sess);
2310 transport_deregister_session_configfs(se_sess);
2311 transport_deregister_session(se_sess);
2312 ch->sess = NULL;
2314 ib_destroy_cm_id(ch->cm_id);
2316 srpt_destroy_ch_ib(ch);
2318 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2319 ch->sport->sdev, ch->rq_size,
2320 ch->rsp_size, DMA_TO_DEVICE);
2322 spin_lock_irq(&sdev->spinlock);
2323 list_del(&ch->list);
2324 spin_unlock_irq(&sdev->spinlock);
2326 if (ch->release_done)
2327 complete(ch->release_done);
2329 wake_up(&sdev->ch_releaseQ);
2331 kfree(ch);
2334 static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2335 u8 i_port_id[16])
2337 struct srpt_node_acl *nacl;
2339 list_for_each_entry(nacl, &sport->port_acl_list, list)
2340 if (memcmp(nacl->i_port_id, i_port_id,
2341 sizeof(nacl->i_port_id)) == 0)
2342 return nacl;
2344 return NULL;
2347 static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2348 u8 i_port_id[16])
2350 struct srpt_node_acl *nacl;
2352 spin_lock_irq(&sport->port_acl_lock);
2353 nacl = __srpt_lookup_acl(sport, i_port_id);
2354 spin_unlock_irq(&sport->port_acl_lock);
2356 return nacl;
2360 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2362 * Ownership of the cm_id is transferred to the target session if this
2363 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2365 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2366 struct ib_cm_req_event_param *param,
2367 void *private_data)
2369 struct srpt_device *sdev = cm_id->context;
2370 struct srpt_port *sport = &sdev->port[param->port - 1];
2371 struct srp_login_req *req;
2372 struct srp_login_rsp *rsp;
2373 struct srp_login_rej *rej;
2374 struct ib_cm_rep_param *rep_param;
2375 struct srpt_rdma_ch *ch, *tmp_ch;
2376 struct srpt_node_acl *nacl;
2377 u32 it_iu_len;
2378 int i;
2379 int ret = 0;
2381 WARN_ON_ONCE(irqs_disabled());
2383 if (WARN_ON(!sdev || !private_data))
2384 return -EINVAL;
2386 req = (struct srp_login_req *)private_data;
2388 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2390 printk(KERN_INFO "Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2391 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2392 " (guid=0x%llx:0x%llx)\n",
2393 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2394 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2395 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2396 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2397 it_iu_len,
2398 param->port,
2399 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2400 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
2402 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2403 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2404 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2406 if (!rsp || !rej || !rep_param) {
2407 ret = -ENOMEM;
2408 goto out;
2411 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2412 rej->reason = __constant_cpu_to_be32(
2413 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2414 ret = -EINVAL;
2415 printk(KERN_ERR "rejected SRP_LOGIN_REQ because its"
2416 " length (%d bytes) is out of range (%d .. %d)\n",
2417 it_iu_len, 64, srp_max_req_size);
2418 goto reject;
2421 if (!sport->enabled) {
2422 rej->reason = __constant_cpu_to_be32(
2423 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2424 ret = -EINVAL;
2425 printk(KERN_ERR "rejected SRP_LOGIN_REQ because the target port"
2426 " has not yet been enabled\n");
2427 goto reject;
2430 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2431 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2433 spin_lock_irq(&sdev->spinlock);
2435 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2436 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2437 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2438 && param->port == ch->sport->port
2439 && param->listen_id == ch->sport->sdev->cm_id
2440 && ch->cm_id) {
2441 enum rdma_ch_state ch_state;
2443 ch_state = srpt_get_ch_state(ch);
2444 if (ch_state != CH_CONNECTING
2445 && ch_state != CH_LIVE)
2446 continue;
2448 /* found an existing channel */
2449 pr_debug("Found existing channel %s"
2450 " cm_id= %p state= %d\n",
2451 ch->sess_name, ch->cm_id, ch_state);
2453 __srpt_close_ch(ch);
2455 rsp->rsp_flags =
2456 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2460 spin_unlock_irq(&sdev->spinlock);
2462 } else
2463 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2465 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2466 || *(__be64 *)(req->target_port_id + 8) !=
2467 cpu_to_be64(srpt_service_guid)) {
2468 rej->reason = __constant_cpu_to_be32(
2469 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2470 ret = -ENOMEM;
2471 printk(KERN_ERR "rejected SRP_LOGIN_REQ because it"
2472 " has an invalid target port identifier.\n");
2473 goto reject;
2476 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2477 if (!ch) {
2478 rej->reason = __constant_cpu_to_be32(
2479 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2480 printk(KERN_ERR "rejected SRP_LOGIN_REQ because no memory.\n");
2481 ret = -ENOMEM;
2482 goto reject;
2485 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2486 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2487 memcpy(ch->t_port_id, req->target_port_id, 16);
2488 ch->sport = &sdev->port[param->port - 1];
2489 ch->cm_id = cm_id;
2491 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2492 * for the SRP protocol to the command queue size.
2494 ch->rq_size = SRPT_RQ_SIZE;
2495 spin_lock_init(&ch->spinlock);
2496 ch->state = CH_CONNECTING;
2497 INIT_LIST_HEAD(&ch->cmd_wait_list);
2498 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2500 ch->ioctx_ring = (struct srpt_send_ioctx **)
2501 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2502 sizeof(*ch->ioctx_ring[0]),
2503 ch->rsp_size, DMA_TO_DEVICE);
2504 if (!ch->ioctx_ring)
2505 goto free_ch;
2507 INIT_LIST_HEAD(&ch->free_list);
2508 for (i = 0; i < ch->rq_size; i++) {
2509 ch->ioctx_ring[i]->ch = ch;
2510 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2513 ret = srpt_create_ch_ib(ch);
2514 if (ret) {
2515 rej->reason = __constant_cpu_to_be32(
2516 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2517 printk(KERN_ERR "rejected SRP_LOGIN_REQ because creating"
2518 " a new RDMA channel failed.\n");
2519 goto free_ring;
2522 ret = srpt_ch_qp_rtr(ch, ch->qp);
2523 if (ret) {
2524 rej->reason = __constant_cpu_to_be32(
2525 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2526 printk(KERN_ERR "rejected SRP_LOGIN_REQ because enabling"
2527 " RTR failed (error code = %d)\n", ret);
2528 goto destroy_ib;
2531 * Use the initator port identifier as the session name.
2533 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2534 be64_to_cpu(*(__be64 *)ch->i_port_id),
2535 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2537 pr_debug("registering session %s\n", ch->sess_name);
2539 nacl = srpt_lookup_acl(sport, ch->i_port_id);
2540 if (!nacl) {
2541 printk(KERN_INFO "Rejected login because no ACL has been"
2542 " configured yet for initiator %s.\n", ch->sess_name);
2543 rej->reason = __constant_cpu_to_be32(
2544 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2545 goto destroy_ib;
2548 ch->sess = transport_init_session();
2549 if (IS_ERR(ch->sess)) {
2550 rej->reason = __constant_cpu_to_be32(
2551 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2552 pr_debug("Failed to create session\n");
2553 goto deregister_session;
2555 ch->sess->se_node_acl = &nacl->nacl;
2556 transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2558 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2559 ch->sess_name, ch->cm_id);
2561 /* create srp_login_response */
2562 rsp->opcode = SRP_LOGIN_RSP;
2563 rsp->tag = req->tag;
2564 rsp->max_it_iu_len = req->req_it_iu_len;
2565 rsp->max_ti_iu_len = req->req_it_iu_len;
2566 ch->max_ti_iu_len = it_iu_len;
2567 rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2568 | SRP_BUF_FORMAT_INDIRECT);
2569 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2570 atomic_set(&ch->req_lim, ch->rq_size);
2571 atomic_set(&ch->req_lim_delta, 0);
2573 /* create cm reply */
2574 rep_param->qp_num = ch->qp->qp_num;
2575 rep_param->private_data = (void *)rsp;
2576 rep_param->private_data_len = sizeof *rsp;
2577 rep_param->rnr_retry_count = 7;
2578 rep_param->flow_control = 1;
2579 rep_param->failover_accepted = 0;
2580 rep_param->srq = 1;
2581 rep_param->responder_resources = 4;
2582 rep_param->initiator_depth = 4;
2584 ret = ib_send_cm_rep(cm_id, rep_param);
2585 if (ret) {
2586 printk(KERN_ERR "sending SRP_LOGIN_REQ response failed"
2587 " (error code = %d)\n", ret);
2588 goto release_channel;
2591 spin_lock_irq(&sdev->spinlock);
2592 list_add_tail(&ch->list, &sdev->rch_list);
2593 spin_unlock_irq(&sdev->spinlock);
2595 goto out;
2597 release_channel:
2598 srpt_set_ch_state(ch, CH_RELEASING);
2599 transport_deregister_session_configfs(ch->sess);
2601 deregister_session:
2602 transport_deregister_session(ch->sess);
2603 ch->sess = NULL;
2605 destroy_ib:
2606 srpt_destroy_ch_ib(ch);
2608 free_ring:
2609 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2610 ch->sport->sdev, ch->rq_size,
2611 ch->rsp_size, DMA_TO_DEVICE);
2612 free_ch:
2613 kfree(ch);
2615 reject:
2616 rej->opcode = SRP_LOGIN_REJ;
2617 rej->tag = req->tag;
2618 rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2619 | SRP_BUF_FORMAT_INDIRECT);
2621 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2622 (void *)rej, sizeof *rej);
2624 out:
2625 kfree(rep_param);
2626 kfree(rsp);
2627 kfree(rej);
2629 return ret;
2632 static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2634 printk(KERN_INFO "Received IB REJ for cm_id %p.\n", cm_id);
2635 srpt_drain_channel(cm_id);
2639 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2641 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2642 * and that the recipient may begin transmitting (RTU = ready to use).
2644 static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2646 struct srpt_rdma_ch *ch;
2647 int ret;
2649 ch = srpt_find_channel(cm_id->context, cm_id);
2650 BUG_ON(!ch);
2652 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2653 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2655 ret = srpt_ch_qp_rts(ch, ch->qp);
2657 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2658 wait_list) {
2659 list_del(&ioctx->wait_list);
2660 srpt_handle_new_iu(ch, ioctx, NULL);
2662 if (ret)
2663 srpt_close_ch(ch);
2667 static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2669 printk(KERN_INFO "Received IB TimeWait exit for cm_id %p.\n", cm_id);
2670 srpt_drain_channel(cm_id);
2673 static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2675 printk(KERN_INFO "Received IB REP error for cm_id %p.\n", cm_id);
2676 srpt_drain_channel(cm_id);
2680 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2682 static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2684 struct srpt_rdma_ch *ch;
2685 unsigned long flags;
2686 bool send_drep = false;
2688 ch = srpt_find_channel(cm_id->context, cm_id);
2689 BUG_ON(!ch);
2691 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2693 spin_lock_irqsave(&ch->spinlock, flags);
2694 switch (ch->state) {
2695 case CH_CONNECTING:
2696 case CH_LIVE:
2697 send_drep = true;
2698 ch->state = CH_DISCONNECTING;
2699 break;
2700 case CH_DISCONNECTING:
2701 case CH_DRAINING:
2702 case CH_RELEASING:
2703 WARN(true, "unexpected channel state %d\n", ch->state);
2704 break;
2706 spin_unlock_irqrestore(&ch->spinlock, flags);
2708 if (send_drep) {
2709 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
2710 printk(KERN_ERR "Sending IB DREP failed.\n");
2711 printk(KERN_INFO "Received DREQ and sent DREP for session %s.\n",
2712 ch->sess_name);
2717 * srpt_cm_drep_recv() - Process reception of a DREP message.
2719 static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2721 printk(KERN_INFO "Received InfiniBand DREP message for cm_id %p.\n",
2722 cm_id);
2723 srpt_drain_channel(cm_id);
2727 * srpt_cm_handler() - IB connection manager callback function.
2729 * A non-zero return value will cause the caller destroy the CM ID.
2731 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2732 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2733 * a non-zero value in any other case will trigger a race with the
2734 * ib_destroy_cm_id() call in srpt_release_channel().
2736 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2738 int ret;
2740 ret = 0;
2741 switch (event->event) {
2742 case IB_CM_REQ_RECEIVED:
2743 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2744 event->private_data);
2745 break;
2746 case IB_CM_REJ_RECEIVED:
2747 srpt_cm_rej_recv(cm_id);
2748 break;
2749 case IB_CM_RTU_RECEIVED:
2750 case IB_CM_USER_ESTABLISHED:
2751 srpt_cm_rtu_recv(cm_id);
2752 break;
2753 case IB_CM_DREQ_RECEIVED:
2754 srpt_cm_dreq_recv(cm_id);
2755 break;
2756 case IB_CM_DREP_RECEIVED:
2757 srpt_cm_drep_recv(cm_id);
2758 break;
2759 case IB_CM_TIMEWAIT_EXIT:
2760 srpt_cm_timewait_exit(cm_id);
2761 break;
2762 case IB_CM_REP_ERROR:
2763 srpt_cm_rep_error(cm_id);
2764 break;
2765 case IB_CM_DREQ_ERROR:
2766 printk(KERN_INFO "Received IB DREQ ERROR event.\n");
2767 break;
2768 case IB_CM_MRA_RECEIVED:
2769 printk(KERN_INFO "Received IB MRA event\n");
2770 break;
2771 default:
2772 printk(KERN_ERR "received unrecognized IB CM event %d\n",
2773 event->event);
2774 break;
2777 return ret;
2781 * srpt_perform_rdmas() - Perform IB RDMA.
2783 * Returns zero upon success or a negative number upon failure.
2785 static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2786 struct srpt_send_ioctx *ioctx)
2788 struct ib_send_wr wr;
2789 struct ib_send_wr *bad_wr;
2790 struct rdma_iu *riu;
2791 int i;
2792 int ret;
2793 int sq_wr_avail;
2794 enum dma_data_direction dir;
2795 const int n_rdma = ioctx->n_rdma;
2797 dir = ioctx->cmd.data_direction;
2798 if (dir == DMA_TO_DEVICE) {
2799 /* write */
2800 ret = -ENOMEM;
2801 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2802 if (sq_wr_avail < 0) {
2803 printk(KERN_WARNING "IB send queue full (needed %d)\n",
2804 n_rdma);
2805 goto out;
2809 ioctx->rdma_aborted = false;
2810 ret = 0;
2811 riu = ioctx->rdma_ius;
2812 memset(&wr, 0, sizeof wr);
2814 for (i = 0; i < n_rdma; ++i, ++riu) {
2815 if (dir == DMA_FROM_DEVICE) {
2816 wr.opcode = IB_WR_RDMA_WRITE;
2817 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2818 SRPT_RDMA_WRITE_LAST :
2819 SRPT_RDMA_MID,
2820 ioctx->ioctx.index);
2821 } else {
2822 wr.opcode = IB_WR_RDMA_READ;
2823 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2824 SRPT_RDMA_READ_LAST :
2825 SRPT_RDMA_MID,
2826 ioctx->ioctx.index);
2828 wr.next = NULL;
2829 wr.wr.rdma.remote_addr = riu->raddr;
2830 wr.wr.rdma.rkey = riu->rkey;
2831 wr.num_sge = riu->sge_cnt;
2832 wr.sg_list = riu->sge;
2834 /* only get completion event for the last rdma write */
2835 if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
2836 wr.send_flags = IB_SEND_SIGNALED;
2838 ret = ib_post_send(ch->qp, &wr, &bad_wr);
2839 if (ret)
2840 break;
2843 if (ret)
2844 printk(KERN_ERR "%s[%d]: ib_post_send() returned %d for %d/%d",
2845 __func__, __LINE__, ret, i, n_rdma);
2846 if (ret && i > 0) {
2847 wr.num_sge = 0;
2848 wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2849 wr.send_flags = IB_SEND_SIGNALED;
2850 while (ch->state == CH_LIVE &&
2851 ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
2852 printk(KERN_INFO "Trying to abort failed RDMA transfer [%d]",
2853 ioctx->ioctx.index);
2854 msleep(1000);
2856 while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
2857 printk(KERN_INFO "Waiting until RDMA abort finished [%d]",
2858 ioctx->ioctx.index);
2859 msleep(1000);
2862 out:
2863 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2864 atomic_add(n_rdma, &ch->sq_wr_avail);
2865 return ret;
2869 * srpt_xfer_data() - Start data transfer from initiator to target.
2871 static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2872 struct srpt_send_ioctx *ioctx)
2874 int ret;
2876 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2877 if (ret) {
2878 printk(KERN_ERR "%s[%d] ret=%d\n", __func__, __LINE__, ret);
2879 goto out;
2882 ret = srpt_perform_rdmas(ch, ioctx);
2883 if (ret) {
2884 if (ret == -EAGAIN || ret == -ENOMEM)
2885 printk(KERN_INFO "%s[%d] queue full -- ret=%d\n",
2886 __func__, __LINE__, ret);
2887 else
2888 printk(KERN_ERR "%s[%d] fatal error -- ret=%d\n",
2889 __func__, __LINE__, ret);
2890 goto out_unmap;
2893 out:
2894 return ret;
2895 out_unmap:
2896 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2897 goto out;
2900 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2902 struct srpt_send_ioctx *ioctx;
2904 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2905 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2909 * srpt_write_pending() - Start data transfer from initiator to target (write).
2911 static int srpt_write_pending(struct se_cmd *se_cmd)
2913 struct srpt_rdma_ch *ch;
2914 struct srpt_send_ioctx *ioctx;
2915 enum srpt_command_state new_state;
2916 enum rdma_ch_state ch_state;
2917 int ret;
2919 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2921 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2922 WARN_ON(new_state == SRPT_STATE_DONE);
2924 ch = ioctx->ch;
2925 BUG_ON(!ch);
2927 ch_state = srpt_get_ch_state(ch);
2928 switch (ch_state) {
2929 case CH_CONNECTING:
2930 WARN(true, "unexpected channel state %d\n", ch_state);
2931 ret = -EINVAL;
2932 goto out;
2933 case CH_LIVE:
2934 break;
2935 case CH_DISCONNECTING:
2936 case CH_DRAINING:
2937 case CH_RELEASING:
2938 pr_debug("cmd with tag %lld: channel disconnecting\n",
2939 ioctx->tag);
2940 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2941 ret = -EINVAL;
2942 goto out;
2944 ret = srpt_xfer_data(ch, ioctx);
2946 out:
2947 return ret;
2950 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2952 switch (tcm_mgmt_status) {
2953 case TMR_FUNCTION_COMPLETE:
2954 return SRP_TSK_MGMT_SUCCESS;
2955 case TMR_FUNCTION_REJECTED:
2956 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2958 return SRP_TSK_MGMT_FAILED;
2962 * srpt_queue_response() - Transmits the response to a SCSI command.
2964 * Callback function called by the TCM core. Must not block since it can be
2965 * invoked on the context of the IB completion handler.
2967 static void srpt_queue_response(struct se_cmd *cmd)
2969 struct srpt_rdma_ch *ch;
2970 struct srpt_send_ioctx *ioctx;
2971 enum srpt_command_state state;
2972 unsigned long flags;
2973 int ret;
2974 enum dma_data_direction dir;
2975 int resp_len;
2976 u8 srp_tm_status;
2978 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2979 ch = ioctx->ch;
2980 BUG_ON(!ch);
2982 spin_lock_irqsave(&ioctx->spinlock, flags);
2983 state = ioctx->state;
2984 switch (state) {
2985 case SRPT_STATE_NEW:
2986 case SRPT_STATE_DATA_IN:
2987 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2988 break;
2989 case SRPT_STATE_MGMT:
2990 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2991 break;
2992 default:
2993 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2994 ch, ioctx->ioctx.index, ioctx->state);
2995 break;
2997 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2999 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
3000 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
3001 atomic_inc(&ch->req_lim_delta);
3002 srpt_abort_cmd(ioctx);
3003 return;
3006 dir = ioctx->cmd.data_direction;
3008 /* For read commands, transfer the data to the initiator. */
3009 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
3010 !ioctx->queue_status_only) {
3011 ret = srpt_xfer_data(ch, ioctx);
3012 if (ret) {
3013 printk(KERN_ERR "xfer_data failed for tag %llu\n",
3014 ioctx->tag);
3015 return;
3019 if (state != SRPT_STATE_MGMT)
3020 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
3021 cmd->scsi_status);
3022 else {
3023 srp_tm_status
3024 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3025 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
3026 ioctx->tag);
3028 ret = srpt_post_send(ch, ioctx, resp_len);
3029 if (ret) {
3030 printk(KERN_ERR "sending cmd response failed for tag %llu\n",
3031 ioctx->tag);
3032 srpt_unmap_sg_to_ib_sge(ch, ioctx);
3033 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
3034 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
3038 static int srpt_queue_data_in(struct se_cmd *cmd)
3040 srpt_queue_response(cmd);
3041 return 0;
3044 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
3046 srpt_queue_response(cmd);
3049 static int srpt_queue_status(struct se_cmd *cmd)
3051 struct srpt_send_ioctx *ioctx;
3053 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3054 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3055 if (cmd->se_cmd_flags &
3056 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3057 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3058 ioctx->queue_status_only = true;
3059 srpt_queue_response(cmd);
3060 return 0;
3063 static void srpt_refresh_port_work(struct work_struct *work)
3065 struct srpt_port *sport = container_of(work, struct srpt_port, work);
3067 srpt_refresh_port(sport);
3070 static int srpt_ch_list_empty(struct srpt_device *sdev)
3072 int res;
3074 spin_lock_irq(&sdev->spinlock);
3075 res = list_empty(&sdev->rch_list);
3076 spin_unlock_irq(&sdev->spinlock);
3078 return res;
3082 * srpt_release_sdev() - Free the channel resources associated with a target.
3084 static int srpt_release_sdev(struct srpt_device *sdev)
3086 struct srpt_rdma_ch *ch, *tmp_ch;
3087 int res;
3089 WARN_ON_ONCE(irqs_disabled());
3091 BUG_ON(!sdev);
3093 spin_lock_irq(&sdev->spinlock);
3094 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3095 __srpt_close_ch(ch);
3096 spin_unlock_irq(&sdev->spinlock);
3098 res = wait_event_interruptible(sdev->ch_releaseQ,
3099 srpt_ch_list_empty(sdev));
3100 if (res)
3101 printk(KERN_ERR "%s: interrupted.\n", __func__);
3103 return 0;
3106 static struct srpt_port *__srpt_lookup_port(const char *name)
3108 struct ib_device *dev;
3109 struct srpt_device *sdev;
3110 struct srpt_port *sport;
3111 int i;
3113 list_for_each_entry(sdev, &srpt_dev_list, list) {
3114 dev = sdev->device;
3115 if (!dev)
3116 continue;
3118 for (i = 0; i < dev->phys_port_cnt; i++) {
3119 sport = &sdev->port[i];
3121 if (!strcmp(sport->port_guid, name))
3122 return sport;
3126 return NULL;
3129 static struct srpt_port *srpt_lookup_port(const char *name)
3131 struct srpt_port *sport;
3133 spin_lock(&srpt_dev_lock);
3134 sport = __srpt_lookup_port(name);
3135 spin_unlock(&srpt_dev_lock);
3137 return sport;
3141 * srpt_add_one() - Infiniband device addition callback function.
3143 static void srpt_add_one(struct ib_device *device)
3145 struct srpt_device *sdev;
3146 struct srpt_port *sport;
3147 struct ib_srq_init_attr srq_attr;
3148 int i;
3150 pr_debug("device = %p, device->dma_ops = %p\n", device,
3151 device->dma_ops);
3153 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3154 if (!sdev)
3155 goto err;
3157 sdev->device = device;
3158 INIT_LIST_HEAD(&sdev->rch_list);
3159 init_waitqueue_head(&sdev->ch_releaseQ);
3160 spin_lock_init(&sdev->spinlock);
3162 if (ib_query_device(device, &sdev->dev_attr))
3163 goto free_dev;
3165 sdev->pd = ib_alloc_pd(device);
3166 if (IS_ERR(sdev->pd))
3167 goto free_dev;
3169 sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
3170 if (IS_ERR(sdev->mr))
3171 goto err_pd;
3173 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3175 srq_attr.event_handler = srpt_srq_event;
3176 srq_attr.srq_context = (void *)sdev;
3177 srq_attr.attr.max_wr = sdev->srq_size;
3178 srq_attr.attr.max_sge = 1;
3179 srq_attr.attr.srq_limit = 0;
3180 srq_attr.srq_type = IB_SRQT_BASIC;
3182 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3183 if (IS_ERR(sdev->srq))
3184 goto err_mr;
3186 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3187 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3188 device->name);
3190 if (!srpt_service_guid)
3191 srpt_service_guid = be64_to_cpu(device->node_guid);
3193 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3194 if (IS_ERR(sdev->cm_id))
3195 goto err_srq;
3197 /* print out target login information */
3198 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3199 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3200 srpt_service_guid, srpt_service_guid);
3203 * We do not have a consistent service_id (ie. also id_ext of target_id)
3204 * to identify this target. We currently use the guid of the first HCA
3205 * in the system as service_id; therefore, the target_id will change
3206 * if this HCA is gone bad and replaced by different HCA
3208 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
3209 goto err_cm;
3211 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3212 srpt_event_handler);
3213 if (ib_register_event_handler(&sdev->event_handler))
3214 goto err_cm;
3216 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3217 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3218 sizeof(*sdev->ioctx_ring[0]),
3219 srp_max_req_size, DMA_FROM_DEVICE);
3220 if (!sdev->ioctx_ring)
3221 goto err_event;
3223 for (i = 0; i < sdev->srq_size; ++i)
3224 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3226 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
3228 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3229 sport = &sdev->port[i - 1];
3230 sport->sdev = sdev;
3231 sport->port = i;
3232 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3233 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3234 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3235 INIT_WORK(&sport->work, srpt_refresh_port_work);
3236 INIT_LIST_HEAD(&sport->port_acl_list);
3237 spin_lock_init(&sport->port_acl_lock);
3239 if (srpt_refresh_port(sport)) {
3240 printk(KERN_ERR "MAD registration failed for %s-%d.\n",
3241 srpt_sdev_name(sdev), i);
3242 goto err_ring;
3244 snprintf(sport->port_guid, sizeof(sport->port_guid),
3245 "0x%016llx%016llx",
3246 be64_to_cpu(sport->gid.global.subnet_prefix),
3247 be64_to_cpu(sport->gid.global.interface_id));
3250 spin_lock(&srpt_dev_lock);
3251 list_add_tail(&sdev->list, &srpt_dev_list);
3252 spin_unlock(&srpt_dev_lock);
3254 out:
3255 ib_set_client_data(device, &srpt_client, sdev);
3256 pr_debug("added %s.\n", device->name);
3257 return;
3259 err_ring:
3260 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3261 sdev->srq_size, srp_max_req_size,
3262 DMA_FROM_DEVICE);
3263 err_event:
3264 ib_unregister_event_handler(&sdev->event_handler);
3265 err_cm:
3266 ib_destroy_cm_id(sdev->cm_id);
3267 err_srq:
3268 ib_destroy_srq(sdev->srq);
3269 err_mr:
3270 ib_dereg_mr(sdev->mr);
3271 err_pd:
3272 ib_dealloc_pd(sdev->pd);
3273 free_dev:
3274 kfree(sdev);
3275 err:
3276 sdev = NULL;
3277 printk(KERN_INFO "%s(%s) failed.\n", __func__, device->name);
3278 goto out;
3282 * srpt_remove_one() - InfiniBand device removal callback function.
3284 static void srpt_remove_one(struct ib_device *device)
3286 struct srpt_device *sdev;
3287 int i;
3289 sdev = ib_get_client_data(device, &srpt_client);
3290 if (!sdev) {
3291 printk(KERN_INFO "%s(%s): nothing to do.\n", __func__,
3292 device->name);
3293 return;
3296 srpt_unregister_mad_agent(sdev);
3298 ib_unregister_event_handler(&sdev->event_handler);
3300 /* Cancel any work queued by the just unregistered IB event handler. */
3301 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3302 cancel_work_sync(&sdev->port[i].work);
3304 ib_destroy_cm_id(sdev->cm_id);
3307 * Unregistering a target must happen after destroying sdev->cm_id
3308 * such that no new SRP_LOGIN_REQ information units can arrive while
3309 * destroying the target.
3311 spin_lock(&srpt_dev_lock);
3312 list_del(&sdev->list);
3313 spin_unlock(&srpt_dev_lock);
3314 srpt_release_sdev(sdev);
3316 ib_destroy_srq(sdev->srq);
3317 ib_dereg_mr(sdev->mr);
3318 ib_dealloc_pd(sdev->pd);
3320 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3321 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3322 sdev->ioctx_ring = NULL;
3323 kfree(sdev);
3326 static struct ib_client srpt_client = {
3327 .name = DRV_NAME,
3328 .add = srpt_add_one,
3329 .remove = srpt_remove_one
3332 static int srpt_check_true(struct se_portal_group *se_tpg)
3334 return 1;
3337 static int srpt_check_false(struct se_portal_group *se_tpg)
3339 return 0;
3342 static char *srpt_get_fabric_name(void)
3344 return "srpt";
3347 static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
3349 return SCSI_TRANSPORTID_PROTOCOLID_SRP;
3352 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3354 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3356 return sport->port_guid;
3359 static u16 srpt_get_tag(struct se_portal_group *tpg)
3361 return 1;
3364 static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
3366 return 1;
3369 static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
3370 struct se_node_acl *se_nacl,
3371 struct t10_pr_registration *pr_reg,
3372 int *format_code, unsigned char *buf)
3374 struct srpt_node_acl *nacl;
3375 struct spc_rdma_transport_id *tr_id;
3377 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3378 tr_id = (void *)buf;
3379 tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
3380 memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
3381 return sizeof(*tr_id);
3384 static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
3385 struct se_node_acl *se_nacl,
3386 struct t10_pr_registration *pr_reg,
3387 int *format_code)
3389 *format_code = 0;
3390 return sizeof(struct spc_rdma_transport_id);
3393 static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
3394 const char *buf, u32 *out_tid_len,
3395 char **port_nexus_ptr)
3397 struct spc_rdma_transport_id *tr_id;
3399 *port_nexus_ptr = NULL;
3400 *out_tid_len = sizeof(struct spc_rdma_transport_id);
3401 tr_id = (void *)buf;
3402 return (char *)tr_id->i_port_id;
3405 static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
3407 struct srpt_node_acl *nacl;
3409 nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
3410 if (!nacl) {
3411 printk(KERN_ERR "Unable to allocate struct srpt_node_acl\n");
3412 return NULL;
3415 return &nacl->nacl;
3418 static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
3419 struct se_node_acl *se_nacl)
3421 struct srpt_node_acl *nacl;
3423 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3424 kfree(nacl);
3427 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3429 return 1;
3432 static void srpt_release_cmd(struct se_cmd *se_cmd)
3434 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3435 struct srpt_send_ioctx, cmd);
3436 struct srpt_rdma_ch *ch = ioctx->ch;
3437 unsigned long flags;
3439 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3440 WARN_ON(ioctx->mapped_sg_count != 0);
3442 if (ioctx->n_rbuf > 1) {
3443 kfree(ioctx->rbufs);
3444 ioctx->rbufs = NULL;
3445 ioctx->n_rbuf = 0;
3448 spin_lock_irqsave(&ch->spinlock, flags);
3449 list_add(&ioctx->free_list, &ch->free_list);
3450 spin_unlock_irqrestore(&ch->spinlock, flags);
3454 * srpt_close_session() - Forcibly close a session.
3456 * Callback function invoked by the TCM core to clean up sessions associated
3457 * with a node ACL when the user invokes
3458 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3460 static void srpt_close_session(struct se_session *se_sess)
3462 DECLARE_COMPLETION_ONSTACK(release_done);
3463 struct srpt_rdma_ch *ch;
3464 struct srpt_device *sdev;
3465 int res;
3467 ch = se_sess->fabric_sess_ptr;
3468 WARN_ON(ch->sess != se_sess);
3470 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3472 sdev = ch->sport->sdev;
3473 spin_lock_irq(&sdev->spinlock);
3474 BUG_ON(ch->release_done);
3475 ch->release_done = &release_done;
3476 __srpt_close_ch(ch);
3477 spin_unlock_irq(&sdev->spinlock);
3479 res = wait_for_completion_timeout(&release_done, 60 * HZ);
3480 WARN_ON(res <= 0);
3484 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3486 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3487 * This object represents an arbitrary integer used to uniquely identify a
3488 * particular attached remote initiator port to a particular SCSI target port
3489 * within a particular SCSI target device within a particular SCSI instance.
3491 static u32 srpt_sess_get_index(struct se_session *se_sess)
3493 return 0;
3496 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3500 static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
3502 struct srpt_send_ioctx *ioctx;
3504 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3505 return ioctx->tag;
3508 /* Note: only used from inside debug printk's by the TCM core. */
3509 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3511 struct srpt_send_ioctx *ioctx;
3513 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3514 return srpt_get_cmd_state(ioctx);
3518 * srpt_parse_i_port_id() - Parse an initiator port ID.
3519 * @name: ASCII representation of a 128-bit initiator port ID.
3520 * @i_port_id: Binary 128-bit port ID.
3522 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3524 const char *p;
3525 unsigned len, count, leading_zero_bytes;
3526 int ret, rc;
3528 p = name;
3529 if (strnicmp(p, "0x", 2) == 0)
3530 p += 2;
3531 ret = -EINVAL;
3532 len = strlen(p);
3533 if (len % 2)
3534 goto out;
3535 count = min(len / 2, 16U);
3536 leading_zero_bytes = 16 - count;
3537 memset(i_port_id, 0, leading_zero_bytes);
3538 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3539 if (rc < 0)
3540 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3541 ret = 0;
3542 out:
3543 return ret;
3547 * configfs callback function invoked for
3548 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3550 static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
3551 struct config_group *group,
3552 const char *name)
3554 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3555 struct se_node_acl *se_nacl, *se_nacl_new;
3556 struct srpt_node_acl *nacl;
3557 int ret = 0;
3558 u32 nexus_depth = 1;
3559 u8 i_port_id[16];
3561 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
3562 printk(KERN_ERR "invalid initiator port ID %s\n", name);
3563 ret = -EINVAL;
3564 goto err;
3567 se_nacl_new = srpt_alloc_fabric_acl(tpg);
3568 if (!se_nacl_new) {
3569 ret = -ENOMEM;
3570 goto err;
3573 * nacl_new may be released by core_tpg_add_initiator_node_acl()
3574 * when converting a node ACL from demo mode to explict
3576 se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
3577 nexus_depth);
3578 if (IS_ERR(se_nacl)) {
3579 ret = PTR_ERR(se_nacl);
3580 goto err;
3582 /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
3583 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3584 memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3585 nacl->sport = sport;
3587 spin_lock_irq(&sport->port_acl_lock);
3588 list_add_tail(&nacl->list, &sport->port_acl_list);
3589 spin_unlock_irq(&sport->port_acl_lock);
3591 return se_nacl;
3592 err:
3593 return ERR_PTR(ret);
3597 * configfs callback function invoked for
3598 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3600 static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
3602 struct srpt_node_acl *nacl;
3603 struct srpt_device *sdev;
3604 struct srpt_port *sport;
3606 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3607 sport = nacl->sport;
3608 sdev = sport->sdev;
3609 spin_lock_irq(&sport->port_acl_lock);
3610 list_del(&nacl->list);
3611 spin_unlock_irq(&sport->port_acl_lock);
3612 core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
3613 srpt_release_fabric_acl(NULL, se_nacl);
3616 static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
3617 struct se_portal_group *se_tpg,
3618 char *page)
3620 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3622 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3625 static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
3626 struct se_portal_group *se_tpg,
3627 const char *page,
3628 size_t count)
3630 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3631 unsigned long val;
3632 int ret;
3634 ret = strict_strtoul(page, 0, &val);
3635 if (ret < 0) {
3636 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3637 return -EINVAL;
3639 if (val > MAX_SRPT_RDMA_SIZE) {
3640 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3641 MAX_SRPT_RDMA_SIZE);
3642 return -EINVAL;
3644 if (val < DEFAULT_MAX_RDMA_SIZE) {
3645 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3646 val, DEFAULT_MAX_RDMA_SIZE);
3647 return -EINVAL;
3649 sport->port_attrib.srp_max_rdma_size = val;
3651 return count;
3654 TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
3656 static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
3657 struct se_portal_group *se_tpg,
3658 char *page)
3660 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3662 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3665 static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
3666 struct se_portal_group *se_tpg,
3667 const char *page,
3668 size_t count)
3670 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3671 unsigned long val;
3672 int ret;
3674 ret = strict_strtoul(page, 0, &val);
3675 if (ret < 0) {
3676 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3677 return -EINVAL;
3679 if (val > MAX_SRPT_RSP_SIZE) {
3680 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3681 MAX_SRPT_RSP_SIZE);
3682 return -EINVAL;
3684 if (val < MIN_MAX_RSP_SIZE) {
3685 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3686 MIN_MAX_RSP_SIZE);
3687 return -EINVAL;
3689 sport->port_attrib.srp_max_rsp_size = val;
3691 return count;
3694 TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
3696 static ssize_t srpt_tpg_attrib_show_srp_sq_size(
3697 struct se_portal_group *se_tpg,
3698 char *page)
3700 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3702 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3705 static ssize_t srpt_tpg_attrib_store_srp_sq_size(
3706 struct se_portal_group *se_tpg,
3707 const char *page,
3708 size_t count)
3710 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3711 unsigned long val;
3712 int ret;
3714 ret = strict_strtoul(page, 0, &val);
3715 if (ret < 0) {
3716 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3717 return -EINVAL;
3719 if (val > MAX_SRPT_SRQ_SIZE) {
3720 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3721 MAX_SRPT_SRQ_SIZE);
3722 return -EINVAL;
3724 if (val < MIN_SRPT_SRQ_SIZE) {
3725 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3726 MIN_SRPT_SRQ_SIZE);
3727 return -EINVAL;
3729 sport->port_attrib.srp_sq_size = val;
3731 return count;
3734 TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
3736 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3737 &srpt_tpg_attrib_srp_max_rdma_size.attr,
3738 &srpt_tpg_attrib_srp_max_rsp_size.attr,
3739 &srpt_tpg_attrib_srp_sq_size.attr,
3740 NULL,
3743 static ssize_t srpt_tpg_show_enable(
3744 struct se_portal_group *se_tpg,
3745 char *page)
3747 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3749 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3752 static ssize_t srpt_tpg_store_enable(
3753 struct se_portal_group *se_tpg,
3754 const char *page,
3755 size_t count)
3757 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3758 unsigned long tmp;
3759 int ret;
3761 ret = strict_strtoul(page, 0, &tmp);
3762 if (ret < 0) {
3763 printk(KERN_ERR "Unable to extract srpt_tpg_store_enable\n");
3764 return -EINVAL;
3767 if ((tmp != 0) && (tmp != 1)) {
3768 printk(KERN_ERR "Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3769 return -EINVAL;
3771 if (tmp == 1)
3772 sport->enabled = true;
3773 else
3774 sport->enabled = false;
3776 return count;
3779 TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
3781 static struct configfs_attribute *srpt_tpg_attrs[] = {
3782 &srpt_tpg_enable.attr,
3783 NULL,
3787 * configfs callback invoked for
3788 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3790 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3791 struct config_group *group,
3792 const char *name)
3794 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3795 int res;
3797 /* Initialize sport->port_wwn and sport->port_tpg_1 */
3798 res = core_tpg_register(&srpt_target->tf_ops, &sport->port_wwn,
3799 &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
3800 if (res)
3801 return ERR_PTR(res);
3803 return &sport->port_tpg_1;
3807 * configfs callback invoked for
3808 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3810 static void srpt_drop_tpg(struct se_portal_group *tpg)
3812 struct srpt_port *sport = container_of(tpg,
3813 struct srpt_port, port_tpg_1);
3815 sport->enabled = false;
3816 core_tpg_deregister(&sport->port_tpg_1);
3820 * configfs callback invoked for
3821 * mkdir /sys/kernel/config/target/$driver/$port
3823 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3824 struct config_group *group,
3825 const char *name)
3827 struct srpt_port *sport;
3828 int ret;
3830 sport = srpt_lookup_port(name);
3831 pr_debug("make_tport(%s)\n", name);
3832 ret = -EINVAL;
3833 if (!sport)
3834 goto err;
3836 return &sport->port_wwn;
3838 err:
3839 return ERR_PTR(ret);
3843 * configfs callback invoked for
3844 * rmdir /sys/kernel/config/target/$driver/$port
3846 static void srpt_drop_tport(struct se_wwn *wwn)
3848 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3850 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3853 static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
3854 char *buf)
3856 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3859 TF_WWN_ATTR_RO(srpt, version);
3861 static struct configfs_attribute *srpt_wwn_attrs[] = {
3862 &srpt_wwn_version.attr,
3863 NULL,
3866 static struct target_core_fabric_ops srpt_template = {
3867 .get_fabric_name = srpt_get_fabric_name,
3868 .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
3869 .tpg_get_wwn = srpt_get_fabric_wwn,
3870 .tpg_get_tag = srpt_get_tag,
3871 .tpg_get_default_depth = srpt_get_default_depth,
3872 .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
3873 .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
3874 .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
3875 .tpg_check_demo_mode = srpt_check_false,
3876 .tpg_check_demo_mode_cache = srpt_check_true,
3877 .tpg_check_demo_mode_write_protect = srpt_check_true,
3878 .tpg_check_prod_mode_write_protect = srpt_check_false,
3879 .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
3880 .tpg_release_fabric_acl = srpt_release_fabric_acl,
3881 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3882 .release_cmd = srpt_release_cmd,
3883 .check_stop_free = srpt_check_stop_free,
3884 .shutdown_session = srpt_shutdown_session,
3885 .close_session = srpt_close_session,
3886 .sess_get_index = srpt_sess_get_index,
3887 .sess_get_initiator_sid = NULL,
3888 .write_pending = srpt_write_pending,
3889 .write_pending_status = srpt_write_pending_status,
3890 .set_default_node_attributes = srpt_set_default_node_attrs,
3891 .get_task_tag = srpt_get_task_tag,
3892 .get_cmd_state = srpt_get_tcm_cmd_state,
3893 .queue_data_in = srpt_queue_data_in,
3894 .queue_status = srpt_queue_status,
3895 .queue_tm_rsp = srpt_queue_tm_rsp,
3897 * Setup function pointers for generic logic in
3898 * target_core_fabric_configfs.c
3900 .fabric_make_wwn = srpt_make_tport,
3901 .fabric_drop_wwn = srpt_drop_tport,
3902 .fabric_make_tpg = srpt_make_tpg,
3903 .fabric_drop_tpg = srpt_drop_tpg,
3904 .fabric_post_link = NULL,
3905 .fabric_pre_unlink = NULL,
3906 .fabric_make_np = NULL,
3907 .fabric_drop_np = NULL,
3908 .fabric_make_nodeacl = srpt_make_nodeacl,
3909 .fabric_drop_nodeacl = srpt_drop_nodeacl,
3913 * srpt_init_module() - Kernel module initialization.
3915 * Note: Since ib_register_client() registers callback functions, and since at
3916 * least one of these callback functions (srpt_add_one()) calls target core
3917 * functions, this driver must be registered with the target core before
3918 * ib_register_client() is called.
3920 static int __init srpt_init_module(void)
3922 int ret;
3924 ret = -EINVAL;
3925 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3926 printk(KERN_ERR "invalid value %d for kernel module parameter"
3927 " srp_max_req_size -- must be at least %d.\n",
3928 srp_max_req_size, MIN_MAX_REQ_SIZE);
3929 goto out;
3932 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3933 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3934 printk(KERN_ERR "invalid value %d for kernel module parameter"
3935 " srpt_srq_size -- must be in the range [%d..%d].\n",
3936 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3937 goto out;
3940 srpt_target = target_fabric_configfs_init(THIS_MODULE, "srpt");
3941 if (IS_ERR(srpt_target)) {
3942 printk(KERN_ERR "couldn't register\n");
3943 ret = PTR_ERR(srpt_target);
3944 goto out;
3947 srpt_target->tf_ops = srpt_template;
3950 * Set up default attribute lists.
3952 srpt_target->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = srpt_wwn_attrs;
3953 srpt_target->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = srpt_tpg_attrs;
3954 srpt_target->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = srpt_tpg_attrib_attrs;
3955 srpt_target->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
3956 srpt_target->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
3957 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
3958 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
3959 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
3960 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
3962 ret = target_fabric_configfs_register(srpt_target);
3963 if (ret < 0) {
3964 printk(KERN_ERR "couldn't register\n");
3965 goto out_free_target;
3968 ret = ib_register_client(&srpt_client);
3969 if (ret) {
3970 printk(KERN_ERR "couldn't register IB client\n");
3971 goto out_unregister_target;
3974 return 0;
3976 out_unregister_target:
3977 target_fabric_configfs_deregister(srpt_target);
3978 srpt_target = NULL;
3979 out_free_target:
3980 if (srpt_target)
3981 target_fabric_configfs_free(srpt_target);
3982 out:
3983 return ret;
3986 static void __exit srpt_cleanup_module(void)
3988 ib_unregister_client(&srpt_client);
3989 target_fabric_configfs_deregister(srpt_target);
3990 srpt_target = NULL;
3993 module_init(srpt_init_module);
3994 module_exit(srpt_cleanup_module);