blob: cea207e8b5d2a5d76346b07d906d2ce98e1f4bee [file] [log] [blame]
Bart Van Asschea42d9852011-10-14 01:30:46 +00001/*
2 * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
3 * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
4 *
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:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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.
32 *
33 */
34
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>
Bart Van Asschea42d9852011-10-14 01:30:46 +000049#include "ib_srpt.h"
50
51/* Name of this kernel module. */
52#define DRV_NAME "ib_srpt"
53#define DRV_VERSION "2.0.0"
54#define DRV_RELDATE "2011-02-14"
55
56#define SRPT_ID_STRING "Linux SRP target"
57
58#undef pr_fmt
59#define pr_fmt(fmt) DRV_NAME " " fmt
60
61MODULE_AUTHOR("Vu Pham and Bart Van Assche");
62MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
63 "v" DRV_VERSION " (" DRV_RELDATE ")");
64MODULE_LICENSE("Dual BSD/GPL");
65
66/*
67 * Global Variables
68 */
69
70static u64 srpt_service_guid;
Roland Dreier486d8b92012-02-02 12:55:58 -080071static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
72static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
Bart Van Asschea42d9852011-10-14 01:30:46 +000073
74static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
75module_param(srp_max_req_size, int, 0444);
76MODULE_PARM_DESC(srp_max_req_size,
77 "Maximum size of SRP request messages in bytes.");
78
79static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
80module_param(srpt_srq_size, int, 0444);
81MODULE_PARM_DESC(srpt_srq_size,
82 "Shared receive queue (SRQ) size.");
83
84static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
85{
86 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
87}
88module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
89 0444);
90MODULE_PARM_DESC(srpt_service_guid,
91 "Using this value for ioc_guid, id_ext, and cm_listen_id"
92 " instead of using the node_guid of the first HCA.");
93
94static struct ib_client srpt_client;
Bart Van Asschea42d9852011-10-14 01:30:46 +000095static void srpt_release_channel(struct srpt_rdma_ch *ch);
96static int srpt_queue_status(struct se_cmd *cmd);
97
98/**
99 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
100 */
101static inline
102enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
103{
104 switch (dir) {
105 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
106 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
107 default: return dir;
108 }
109}
110
111/**
112 * srpt_sdev_name() - Return the name associated with the HCA.
113 *
114 * Examples are ib0, ib1, ...
115 */
116static inline const char *srpt_sdev_name(struct srpt_device *sdev)
117{
118 return sdev->device->name;
119}
120
121static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
122{
123 unsigned long flags;
124 enum rdma_ch_state state;
125
126 spin_lock_irqsave(&ch->spinlock, flags);
127 state = ch->state;
128 spin_unlock_irqrestore(&ch->spinlock, flags);
129 return state;
130}
131
132static enum rdma_ch_state
133srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
134{
135 unsigned long flags;
136 enum rdma_ch_state prev;
137
138 spin_lock_irqsave(&ch->spinlock, flags);
139 prev = ch->state;
140 ch->state = new_state;
141 spin_unlock_irqrestore(&ch->spinlock, flags);
142 return prev;
143}
144
145/**
146 * srpt_test_and_set_ch_state() - Test and set the channel state.
147 *
148 * Returns true if and only if the channel state has been set to the new state.
149 */
150static bool
151srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
152 enum rdma_ch_state new)
153{
154 unsigned long flags;
155 enum rdma_ch_state prev;
156
157 spin_lock_irqsave(&ch->spinlock, flags);
158 prev = ch->state;
159 if (prev == old)
160 ch->state = new;
161 spin_unlock_irqrestore(&ch->spinlock, flags);
162 return prev == old;
163}
164
165/**
166 * srpt_event_handler() - Asynchronous IB event callback function.
167 *
168 * Callback function called by the InfiniBand core when an asynchronous IB
169 * event occurs. This callback may occur in interrupt context. See also
170 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
171 * Architecture Specification.
172 */
173static void srpt_event_handler(struct ib_event_handler *handler,
174 struct ib_event *event)
175{
176 struct srpt_device *sdev;
177 struct srpt_port *sport;
178
179 sdev = ib_get_client_data(event->device, &srpt_client);
180 if (!sdev || sdev->device != event->device)
181 return;
182
183 pr_debug("ASYNC event= %d on device= %s\n", event->event,
184 srpt_sdev_name(sdev));
185
186 switch (event->event) {
187 case IB_EVENT_PORT_ERR:
188 if (event->element.port_num <= sdev->device->phys_port_cnt) {
189 sport = &sdev->port[event->element.port_num - 1];
190 sport->lid = 0;
191 sport->sm_lid = 0;
192 }
193 break;
194 case IB_EVENT_PORT_ACTIVE:
195 case IB_EVENT_LID_CHANGE:
196 case IB_EVENT_PKEY_CHANGE:
197 case IB_EVENT_SM_CHANGE:
198 case IB_EVENT_CLIENT_REREGISTER:
Doug Ledford2aa1cf62014-08-12 19:20:10 -0400199 case IB_EVENT_GID_CHANGE:
Bart Van Asschea42d9852011-10-14 01:30:46 +0000200 /* Refresh port data asynchronously. */
201 if (event->element.port_num <= sdev->device->phys_port_cnt) {
202 sport = &sdev->port[event->element.port_num - 1];
203 if (!sport->lid && !sport->sm_lid)
204 schedule_work(&sport->work);
205 }
206 break;
207 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400208 pr_err("received unrecognized IB event %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +0000209 event->event);
210 break;
211 }
212}
213
214/**
215 * srpt_srq_event() - SRQ event callback function.
216 */
217static void srpt_srq_event(struct ib_event *event, void *ctx)
218{
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400219 pr_info("SRQ event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000220}
221
222/**
223 * srpt_qp_event() - QP event callback function.
224 */
225static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
226{
227 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
228 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
229
230 switch (event->event) {
231 case IB_EVENT_COMM_EST:
232 ib_cm_notify(ch->cm_id, event->event);
233 break;
234 case IB_EVENT_QP_LAST_WQE_REACHED:
235 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
236 CH_RELEASING))
237 srpt_release_channel(ch);
238 else
239 pr_debug("%s: state %d - ignored LAST_WQE.\n",
240 ch->sess_name, srpt_get_ch_state(ch));
241 break;
242 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400243 pr_err("received unrecognized IB QP event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000244 break;
245 }
246}
247
248/**
249 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
250 *
251 * @slot: one-based slot number.
252 * @value: four-bit value.
253 *
254 * Copies the lowest four bits of value in element slot of the array of four
255 * bit elements called c_list (controller list). The index slot is one-based.
256 */
257static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
258{
259 u16 id;
260 u8 tmp;
261
262 id = (slot - 1) / 2;
263 if (slot & 0x1) {
264 tmp = c_list[id] & 0xf;
265 c_list[id] = (value << 4) | tmp;
266 } else {
267 tmp = c_list[id] & 0xf0;
268 c_list[id] = (value & 0xf) | tmp;
269 }
270}
271
272/**
273 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
274 *
275 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
276 * Specification.
277 */
278static void srpt_get_class_port_info(struct ib_dm_mad *mad)
279{
280 struct ib_class_port_info *cif;
281
282 cif = (struct ib_class_port_info *)mad->data;
283 memset(cif, 0, sizeof *cif);
284 cif->base_version = 1;
285 cif->class_version = 1;
286 cif->resp_time_value = 20;
287
288 mad->mad_hdr.status = 0;
289}
290
291/**
292 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
293 *
294 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
295 * Specification. See also section B.7, table B.6 in the SRP r16a document.
296 */
297static void srpt_get_iou(struct ib_dm_mad *mad)
298{
299 struct ib_dm_iou_info *ioui;
300 u8 slot;
301 int i;
302
303 ioui = (struct ib_dm_iou_info *)mad->data;
304 ioui->change_id = __constant_cpu_to_be16(1);
305 ioui->max_controllers = 16;
306
307 /* set present for slot 1 and empty for the rest */
308 srpt_set_ioc(ioui->controller_list, 1, 1);
309 for (i = 1, slot = 2; i < 16; i++, slot++)
310 srpt_set_ioc(ioui->controller_list, slot, 0);
311
312 mad->mad_hdr.status = 0;
313}
314
315/**
316 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
317 *
318 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
319 * Architecture Specification. See also section B.7, table B.7 in the SRP
320 * r16a document.
321 */
322static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
323 struct ib_dm_mad *mad)
324{
325 struct srpt_device *sdev = sport->sdev;
326 struct ib_dm_ioc_profile *iocp;
327
328 iocp = (struct ib_dm_ioc_profile *)mad->data;
329
330 if (!slot || slot > 16) {
331 mad->mad_hdr.status
332 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
333 return;
334 }
335
336 if (slot > 2) {
337 mad->mad_hdr.status
338 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
339 return;
340 }
341
342 memset(iocp, 0, sizeof *iocp);
343 strcpy(iocp->id_string, SRPT_ID_STRING);
344 iocp->guid = cpu_to_be64(srpt_service_guid);
345 iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
346 iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
347 iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
348 iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
349 iocp->subsys_device_id = 0x0;
350 iocp->io_class = __constant_cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
351 iocp->io_subclass = __constant_cpu_to_be16(SRP_IO_SUBCLASS);
352 iocp->protocol = __constant_cpu_to_be16(SRP_PROTOCOL);
353 iocp->protocol_version = __constant_cpu_to_be16(SRP_PROTOCOL_VERSION);
354 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
355 iocp->rdma_read_depth = 4;
356 iocp->send_size = cpu_to_be32(srp_max_req_size);
357 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
358 1U << 24));
359 iocp->num_svc_entries = 1;
360 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
361 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
362
363 mad->mad_hdr.status = 0;
364}
365
366/**
367 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
368 *
369 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
370 * Specification. See also section B.7, table B.8 in the SRP r16a document.
371 */
372static void srpt_get_svc_entries(u64 ioc_guid,
373 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
374{
375 struct ib_dm_svc_entries *svc_entries;
376
377 WARN_ON(!ioc_guid);
378
379 if (!slot || slot > 16) {
380 mad->mad_hdr.status
381 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
382 return;
383 }
384
385 if (slot > 2 || lo > hi || hi > 1) {
386 mad->mad_hdr.status
387 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
388 return;
389 }
390
391 svc_entries = (struct ib_dm_svc_entries *)mad->data;
392 memset(svc_entries, 0, sizeof *svc_entries);
393 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
394 snprintf(svc_entries->service_entries[0].name,
395 sizeof(svc_entries->service_entries[0].name),
396 "%s%016llx",
397 SRP_SERVICE_NAME_PREFIX,
398 ioc_guid);
399
400 mad->mad_hdr.status = 0;
401}
402
403/**
404 * srpt_mgmt_method_get() - Process a received management datagram.
405 * @sp: source port through which the MAD has been received.
406 * @rq_mad: received MAD.
407 * @rsp_mad: response MAD.
408 */
409static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
410 struct ib_dm_mad *rsp_mad)
411{
412 u16 attr_id;
413 u32 slot;
414 u8 hi, lo;
415
416 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
417 switch (attr_id) {
418 case DM_ATTR_CLASS_PORT_INFO:
419 srpt_get_class_port_info(rsp_mad);
420 break;
421 case DM_ATTR_IOU_INFO:
422 srpt_get_iou(rsp_mad);
423 break;
424 case DM_ATTR_IOC_PROFILE:
425 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
426 srpt_get_ioc(sp, slot, rsp_mad);
427 break;
428 case DM_ATTR_SVC_ENTRIES:
429 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
430 hi = (u8) ((slot >> 8) & 0xff);
431 lo = (u8) (slot & 0xff);
432 slot = (u16) ((slot >> 16) & 0xffff);
433 srpt_get_svc_entries(srpt_service_guid,
434 slot, hi, lo, rsp_mad);
435 break;
436 default:
437 rsp_mad->mad_hdr.status =
438 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
439 break;
440 }
441}
442
443/**
444 * srpt_mad_send_handler() - Post MAD-send callback function.
445 */
446static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
447 struct ib_mad_send_wc *mad_wc)
448{
449 ib_destroy_ah(mad_wc->send_buf->ah);
450 ib_free_send_mad(mad_wc->send_buf);
451}
452
453/**
454 * srpt_mad_recv_handler() - MAD reception callback function.
455 */
456static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
457 struct ib_mad_recv_wc *mad_wc)
458{
459 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
460 struct ib_ah *ah;
461 struct ib_mad_send_buf *rsp;
462 struct ib_dm_mad *dm_mad;
463
464 if (!mad_wc || !mad_wc->recv_buf.mad)
465 return;
466
467 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
468 mad_wc->recv_buf.grh, mad_agent->port_num);
469 if (IS_ERR(ah))
470 goto err;
471
472 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
473
474 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
475 mad_wc->wc->pkey_index, 0,
476 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
477 GFP_KERNEL);
478 if (IS_ERR(rsp))
479 goto err_rsp;
480
481 rsp->ah = ah;
482
483 dm_mad = rsp->mad;
484 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
485 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
486 dm_mad->mad_hdr.status = 0;
487
488 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
489 case IB_MGMT_METHOD_GET:
490 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
491 break;
492 case IB_MGMT_METHOD_SET:
493 dm_mad->mad_hdr.status =
494 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
495 break;
496 default:
497 dm_mad->mad_hdr.status =
498 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
499 break;
500 }
501
502 if (!ib_post_send_mad(rsp, NULL)) {
503 ib_free_recv_mad(mad_wc);
504 /* will destroy_ah & free_send_mad in send completion */
505 return;
506 }
507
508 ib_free_send_mad(rsp);
509
510err_rsp:
511 ib_destroy_ah(ah);
512err:
513 ib_free_recv_mad(mad_wc);
514}
515
516/**
517 * srpt_refresh_port() - Configure a HCA port.
518 *
519 * Enable InfiniBand management datagram processing, update the cached sm_lid,
520 * lid and gid values, and register a callback function for processing MADs
521 * on the specified port.
522 *
523 * Note: It is safe to call this function more than once for the same port.
524 */
525static int srpt_refresh_port(struct srpt_port *sport)
526{
527 struct ib_mad_reg_req reg_req;
528 struct ib_port_modify port_modify;
529 struct ib_port_attr port_attr;
530 int ret;
531
532 memset(&port_modify, 0, sizeof port_modify);
533 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
534 port_modify.clr_port_cap_mask = 0;
535
536 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
537 if (ret)
538 goto err_mod_port;
539
540 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
541 if (ret)
542 goto err_query_port;
543
544 sport->sm_lid = port_attr.sm_lid;
545 sport->lid = port_attr.lid;
546
547 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
548 if (ret)
549 goto err_query_port;
550
551 if (!sport->mad_agent) {
552 memset(&reg_req, 0, sizeof reg_req);
553 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
554 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
555 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
556 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
557
558 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
559 sport->port,
560 IB_QPT_GSI,
561 &reg_req, 0,
562 srpt_mad_send_handler,
563 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400564 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000565 if (IS_ERR(sport->mad_agent)) {
566 ret = PTR_ERR(sport->mad_agent);
567 sport->mad_agent = NULL;
568 goto err_query_port;
569 }
570 }
571
572 return 0;
573
574err_query_port:
575
576 port_modify.set_port_cap_mask = 0;
577 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
578 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
579
580err_mod_port:
581
582 return ret;
583}
584
585/**
586 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
587 *
588 * Note: It is safe to call this function more than once for the same device.
589 */
590static void srpt_unregister_mad_agent(struct srpt_device *sdev)
591{
592 struct ib_port_modify port_modify = {
593 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
594 };
595 struct srpt_port *sport;
596 int i;
597
598 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
599 sport = &sdev->port[i - 1];
600 WARN_ON(sport->port != i);
601 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400602 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000603 if (sport->mad_agent) {
604 ib_unregister_mad_agent(sport->mad_agent);
605 sport->mad_agent = NULL;
606 }
607 }
608}
609
610/**
611 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
612 */
613static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
614 int ioctx_size, int dma_size,
615 enum dma_data_direction dir)
616{
617 struct srpt_ioctx *ioctx;
618
619 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
620 if (!ioctx)
621 goto err;
622
623 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
624 if (!ioctx->buf)
625 goto err_free_ioctx;
626
627 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
628 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
629 goto err_free_buf;
630
631 return ioctx;
632
633err_free_buf:
634 kfree(ioctx->buf);
635err_free_ioctx:
636 kfree(ioctx);
637err:
638 return NULL;
639}
640
641/**
642 * srpt_free_ioctx() - Free an SRPT I/O context structure.
643 */
644static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
645 int dma_size, enum dma_data_direction dir)
646{
647 if (!ioctx)
648 return;
649
650 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
651 kfree(ioctx->buf);
652 kfree(ioctx);
653}
654
655/**
656 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
657 * @sdev: Device to allocate the I/O context ring for.
658 * @ring_size: Number of elements in the I/O context ring.
659 * @ioctx_size: I/O context size.
660 * @dma_size: DMA buffer size.
661 * @dir: DMA data direction.
662 */
663static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
664 int ring_size, int ioctx_size,
665 int dma_size, enum dma_data_direction dir)
666{
667 struct srpt_ioctx **ring;
668 int i;
669
670 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
671 && ioctx_size != sizeof(struct srpt_send_ioctx));
672
673 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
674 if (!ring)
675 goto out;
676 for (i = 0; i < ring_size; ++i) {
677 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
678 if (!ring[i])
679 goto err;
680 ring[i]->index = i;
681 }
682 goto out;
683
684err:
685 while (--i >= 0)
686 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
687 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100688 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000689out:
690 return ring;
691}
692
693/**
694 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
695 */
696static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
697 struct srpt_device *sdev, int ring_size,
698 int dma_size, enum dma_data_direction dir)
699{
700 int i;
701
702 for (i = 0; i < ring_size; ++i)
703 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
704 kfree(ioctx_ring);
705}
706
707/**
708 * srpt_get_cmd_state() - Get the state of a SCSI command.
709 */
710static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
711{
712 enum srpt_command_state state;
713 unsigned long flags;
714
715 BUG_ON(!ioctx);
716
717 spin_lock_irqsave(&ioctx->spinlock, flags);
718 state = ioctx->state;
719 spin_unlock_irqrestore(&ioctx->spinlock, flags);
720 return state;
721}
722
723/**
724 * srpt_set_cmd_state() - Set the state of a SCSI command.
725 *
726 * Does not modify the state of aborted commands. Returns the previous command
727 * state.
728 */
729static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
730 enum srpt_command_state new)
731{
732 enum srpt_command_state previous;
733 unsigned long flags;
734
735 BUG_ON(!ioctx);
736
737 spin_lock_irqsave(&ioctx->spinlock, flags);
738 previous = ioctx->state;
739 if (previous != SRPT_STATE_DONE)
740 ioctx->state = new;
741 spin_unlock_irqrestore(&ioctx->spinlock, flags);
742
743 return previous;
744}
745
746/**
747 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
748 *
749 * Returns true if and only if the previous command state was equal to 'old'.
750 */
751static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
752 enum srpt_command_state old,
753 enum srpt_command_state new)
754{
755 enum srpt_command_state previous;
756 unsigned long flags;
757
758 WARN_ON(!ioctx);
759 WARN_ON(old == SRPT_STATE_DONE);
760 WARN_ON(new == SRPT_STATE_NEW);
761
762 spin_lock_irqsave(&ioctx->spinlock, flags);
763 previous = ioctx->state;
764 if (previous == old)
765 ioctx->state = new;
766 spin_unlock_irqrestore(&ioctx->spinlock, flags);
767 return previous == old;
768}
769
770/**
771 * srpt_post_recv() - Post an IB receive request.
772 */
773static int srpt_post_recv(struct srpt_device *sdev,
774 struct srpt_recv_ioctx *ioctx)
775{
776 struct ib_sge list;
777 struct ib_recv_wr wr, *bad_wr;
778
779 BUG_ON(!sdev);
780 wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
781
782 list.addr = ioctx->ioctx.dma;
783 list.length = srp_max_req_size;
784 list.lkey = sdev->mr->lkey;
785
786 wr.next = NULL;
787 wr.sg_list = &list;
788 wr.num_sge = 1;
789
790 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
791}
792
793/**
794 * srpt_post_send() - Post an IB send request.
795 *
796 * Returns zero upon success and a non-zero value upon failure.
797 */
798static int srpt_post_send(struct srpt_rdma_ch *ch,
799 struct srpt_send_ioctx *ioctx, int len)
800{
801 struct ib_sge list;
802 struct ib_send_wr wr, *bad_wr;
803 struct srpt_device *sdev = ch->sport->sdev;
804 int ret;
805
806 atomic_inc(&ch->req_lim);
807
808 ret = -ENOMEM;
809 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400810 pr_warn("IB send queue full (needed 1)\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000811 goto out;
812 }
813
814 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
815 DMA_TO_DEVICE);
816
817 list.addr = ioctx->ioctx.dma;
818 list.length = len;
819 list.lkey = sdev->mr->lkey;
820
821 wr.next = NULL;
822 wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
823 wr.sg_list = &list;
824 wr.num_sge = 1;
825 wr.opcode = IB_WR_SEND;
826 wr.send_flags = IB_SEND_SIGNALED;
827
828 ret = ib_post_send(ch->qp, &wr, &bad_wr);
829
830out:
831 if (ret < 0) {
832 atomic_inc(&ch->sq_wr_avail);
833 atomic_dec(&ch->req_lim);
834 }
835 return ret;
836}
837
838/**
839 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
840 * @ioctx: Pointer to the I/O context associated with the request.
841 * @srp_cmd: Pointer to the SRP_CMD request data.
842 * @dir: Pointer to the variable to which the transfer direction will be
843 * written.
844 * @data_len: Pointer to the variable to which the total data length of all
845 * descriptors in the SRP_CMD request will be written.
846 *
847 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
848 *
849 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
850 * -ENOMEM when memory allocation fails and zero upon success.
851 */
852static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
853 struct srp_cmd *srp_cmd,
854 enum dma_data_direction *dir, u64 *data_len)
855{
856 struct srp_indirect_buf *idb;
857 struct srp_direct_buf *db;
858 unsigned add_cdb_offset;
859 int ret;
860
861 /*
862 * The pointer computations below will only be compiled correctly
863 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
864 * whether srp_cmd::add_data has been declared as a byte pointer.
865 */
866 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
867 && !__same_type(srp_cmd->add_data[0], (u8)0));
868
869 BUG_ON(!dir);
870 BUG_ON(!data_len);
871
872 ret = 0;
873 *data_len = 0;
874
875 /*
876 * The lower four bits of the buffer format field contain the DATA-IN
877 * buffer descriptor format, and the highest four bits contain the
878 * DATA-OUT buffer descriptor format.
879 */
880 *dir = DMA_NONE;
881 if (srp_cmd->buf_fmt & 0xf)
882 /* DATA-IN: transfer data from target to initiator (read). */
883 *dir = DMA_FROM_DEVICE;
884 else if (srp_cmd->buf_fmt >> 4)
885 /* DATA-OUT: transfer data from initiator to target (write). */
886 *dir = DMA_TO_DEVICE;
887
888 /*
889 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
890 * CDB LENGTH' field are reserved and the size in bytes of this field
891 * is four times the value specified in bits 3..7. Hence the "& ~3".
892 */
893 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
894 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
895 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
896 ioctx->n_rbuf = 1;
897 ioctx->rbufs = &ioctx->single_rbuf;
898
899 db = (struct srp_direct_buf *)(srp_cmd->add_data
900 + add_cdb_offset);
901 memcpy(ioctx->rbufs, db, sizeof *db);
902 *data_len = be32_to_cpu(db->len);
903 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
904 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
905 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
906 + add_cdb_offset);
907
908 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
909
910 if (ioctx->n_rbuf >
911 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400912 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000913 " type (%u out + %u in != %u / %zu)\n",
914 srp_cmd->data_out_desc_cnt,
915 srp_cmd->data_in_desc_cnt,
916 be32_to_cpu(idb->table_desc.len),
917 sizeof(*db));
918 ioctx->n_rbuf = 0;
919 ret = -EINVAL;
920 goto out;
921 }
922
923 if (ioctx->n_rbuf == 1)
924 ioctx->rbufs = &ioctx->single_rbuf;
925 else {
926 ioctx->rbufs =
927 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
928 if (!ioctx->rbufs) {
929 ioctx->n_rbuf = 0;
930 ret = -ENOMEM;
931 goto out;
932 }
933 }
934
935 db = idb->desc_list;
936 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
937 *data_len = be32_to_cpu(idb->len);
938 }
939out:
940 return ret;
941}
942
943/**
944 * srpt_init_ch_qp() - Initialize queue pair attributes.
945 *
946 * Initialized the attributes of queue pair 'qp' by allowing local write,
947 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
948 */
949static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
950{
951 struct ib_qp_attr *attr;
952 int ret;
953
954 attr = kzalloc(sizeof *attr, GFP_KERNEL);
955 if (!attr)
956 return -ENOMEM;
957
958 attr->qp_state = IB_QPS_INIT;
959 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
960 IB_ACCESS_REMOTE_WRITE;
961 attr->port_num = ch->sport->port;
962 attr->pkey_index = 0;
963
964 ret = ib_modify_qp(qp, attr,
965 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
966 IB_QP_PKEY_INDEX);
967
968 kfree(attr);
969 return ret;
970}
971
972/**
973 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
974 * @ch: channel of the queue pair.
975 * @qp: queue pair to change the state of.
976 *
977 * Returns zero upon success and a negative value upon failure.
978 *
979 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
980 * If this structure ever becomes larger, it might be necessary to allocate
981 * it dynamically instead of on the stack.
982 */
983static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
984{
985 struct ib_qp_attr qp_attr;
986 int attr_mask;
987 int ret;
988
989 qp_attr.qp_state = IB_QPS_RTR;
990 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
991 if (ret)
992 goto out;
993
994 qp_attr.max_dest_rd_atomic = 4;
995
996 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
997
998out:
999 return ret;
1000}
1001
1002/**
1003 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1004 * @ch: channel of the queue pair.
1005 * @qp: queue pair to change the state of.
1006 *
1007 * Returns zero upon success and a negative value upon failure.
1008 *
1009 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1010 * If this structure ever becomes larger, it might be necessary to allocate
1011 * it dynamically instead of on the stack.
1012 */
1013static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1014{
1015 struct ib_qp_attr qp_attr;
1016 int attr_mask;
1017 int ret;
1018
1019 qp_attr.qp_state = IB_QPS_RTS;
1020 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1021 if (ret)
1022 goto out;
1023
1024 qp_attr.max_rd_atomic = 4;
1025
1026 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1027
1028out:
1029 return ret;
1030}
1031
1032/**
1033 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1034 */
1035static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1036{
1037 struct ib_qp_attr qp_attr;
1038
1039 qp_attr.qp_state = IB_QPS_ERR;
1040 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1041}
1042
1043/**
1044 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1045 */
1046static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1047 struct srpt_send_ioctx *ioctx)
1048{
1049 struct scatterlist *sg;
1050 enum dma_data_direction dir;
1051
1052 BUG_ON(!ch);
1053 BUG_ON(!ioctx);
1054 BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
1055
1056 while (ioctx->n_rdma)
1057 kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
1058
1059 kfree(ioctx->rdma_ius);
1060 ioctx->rdma_ius = NULL;
1061
1062 if (ioctx->mapped_sg_count) {
1063 sg = ioctx->sg;
1064 WARN_ON(!sg);
1065 dir = ioctx->cmd.data_direction;
1066 BUG_ON(dir == DMA_NONE);
1067 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1068 opposite_dma_dir(dir));
1069 ioctx->mapped_sg_count = 0;
1070 }
1071}
1072
1073/**
1074 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1075 */
1076static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1077 struct srpt_send_ioctx *ioctx)
1078{
Mike Marciniszynb0768082014-04-07 13:58:35 -04001079 struct ib_device *dev = ch->sport->sdev->device;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001080 struct se_cmd *cmd;
1081 struct scatterlist *sg, *sg_orig;
1082 int sg_cnt;
1083 enum dma_data_direction dir;
1084 struct rdma_iu *riu;
1085 struct srp_direct_buf *db;
1086 dma_addr_t dma_addr;
1087 struct ib_sge *sge;
1088 u64 raddr;
1089 u32 rsize;
1090 u32 tsize;
1091 u32 dma_len;
1092 int count, nrdma;
1093 int i, j, k;
1094
1095 BUG_ON(!ch);
1096 BUG_ON(!ioctx);
1097 cmd = &ioctx->cmd;
1098 dir = cmd->data_direction;
1099 BUG_ON(dir == DMA_NONE);
1100
Roland Dreier6f9e7f02012-03-30 11:29:12 -07001101 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1102 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001103
1104 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1105 opposite_dma_dir(dir));
1106 if (unlikely(!count))
1107 return -EAGAIN;
1108
1109 ioctx->mapped_sg_count = count;
1110
1111 if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1112 nrdma = ioctx->n_rdma_ius;
1113 else {
1114 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1115 + ioctx->n_rbuf;
1116
1117 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
1118 if (!ioctx->rdma_ius)
1119 goto free_mem;
1120
1121 ioctx->n_rdma_ius = nrdma;
1122 }
1123
1124 db = ioctx->rbufs;
1125 tsize = cmd->data_length;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001126 dma_len = ib_sg_dma_len(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001127 riu = ioctx->rdma_ius;
1128
1129 /*
1130 * For each remote desc - calculate the #ib_sge.
1131 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1132 * each remote desc rdma_iu is required a rdma wr;
1133 * else
1134 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1135 * another rdma wr
1136 */
1137 for (i = 0, j = 0;
1138 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1139 rsize = be32_to_cpu(db->len);
1140 raddr = be64_to_cpu(db->va);
1141 riu->raddr = raddr;
1142 riu->rkey = be32_to_cpu(db->key);
1143 riu->sge_cnt = 0;
1144
1145 /* calculate how many sge required for this remote_buf */
1146 while (rsize > 0 && tsize > 0) {
1147
1148 if (rsize >= dma_len) {
1149 tsize -= dma_len;
1150 rsize -= dma_len;
1151 raddr += dma_len;
1152
1153 if (tsize > 0) {
1154 ++j;
1155 if (j < count) {
1156 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001157 dma_len = ib_sg_dma_len(
1158 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001159 }
1160 }
1161 } else {
1162 tsize -= rsize;
1163 dma_len -= rsize;
1164 rsize = 0;
1165 }
1166
1167 ++riu->sge_cnt;
1168
1169 if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1170 ++ioctx->n_rdma;
1171 riu->sge =
1172 kmalloc(riu->sge_cnt * sizeof *riu->sge,
1173 GFP_KERNEL);
1174 if (!riu->sge)
1175 goto free_mem;
1176
1177 ++riu;
1178 riu->sge_cnt = 0;
1179 riu->raddr = raddr;
1180 riu->rkey = be32_to_cpu(db->key);
1181 }
1182 }
1183
1184 ++ioctx->n_rdma;
1185 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1186 GFP_KERNEL);
1187 if (!riu->sge)
1188 goto free_mem;
1189 }
1190
1191 db = ioctx->rbufs;
1192 tsize = cmd->data_length;
1193 riu = ioctx->rdma_ius;
1194 sg = sg_orig;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001195 dma_len = ib_sg_dma_len(dev, &sg[0]);
1196 dma_addr = ib_sg_dma_address(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001197
1198 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1199 for (i = 0, j = 0;
1200 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1201 rsize = be32_to_cpu(db->len);
1202 sge = riu->sge;
1203 k = 0;
1204
1205 while (rsize > 0 && tsize > 0) {
1206 sge->addr = dma_addr;
1207 sge->lkey = ch->sport->sdev->mr->lkey;
1208
1209 if (rsize >= dma_len) {
1210 sge->length =
1211 (tsize < dma_len) ? tsize : dma_len;
1212 tsize -= dma_len;
1213 rsize -= dma_len;
1214
1215 if (tsize > 0) {
1216 ++j;
1217 if (j < count) {
1218 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001219 dma_len = ib_sg_dma_len(
1220 dev, sg);
1221 dma_addr = ib_sg_dma_address(
1222 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001223 }
1224 }
1225 } else {
1226 sge->length = (tsize < rsize) ? tsize : rsize;
1227 tsize -= rsize;
1228 dma_len -= rsize;
1229 dma_addr += rsize;
1230 rsize = 0;
1231 }
1232
1233 ++k;
1234 if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1235 ++riu;
1236 sge = riu->sge;
1237 k = 0;
1238 } else if (rsize > 0 && tsize > 0)
1239 ++sge;
1240 }
1241 }
1242
1243 return 0;
1244
1245free_mem:
1246 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1247
1248 return -ENOMEM;
1249}
1250
1251/**
1252 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1253 */
1254static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1255{
1256 struct srpt_send_ioctx *ioctx;
1257 unsigned long flags;
1258
1259 BUG_ON(!ch);
1260
1261 ioctx = NULL;
1262 spin_lock_irqsave(&ch->spinlock, flags);
1263 if (!list_empty(&ch->free_list)) {
1264 ioctx = list_first_entry(&ch->free_list,
1265 struct srpt_send_ioctx, free_list);
1266 list_del(&ioctx->free_list);
1267 }
1268 spin_unlock_irqrestore(&ch->spinlock, flags);
1269
1270 if (!ioctx)
1271 return ioctx;
1272
1273 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001274 spin_lock_init(&ioctx->spinlock);
1275 ioctx->state = SRPT_STATE_NEW;
1276 ioctx->n_rbuf = 0;
1277 ioctx->rbufs = NULL;
1278 ioctx->n_rdma = 0;
1279 ioctx->n_rdma_ius = 0;
1280 ioctx->rdma_ius = NULL;
1281 ioctx->mapped_sg_count = 0;
1282 init_completion(&ioctx->tx_done);
1283 ioctx->queue_status_only = false;
1284 /*
1285 * transport_init_se_cmd() does not initialize all fields, so do it
1286 * here.
1287 */
1288 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1289 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1290
1291 return ioctx;
1292}
1293
1294/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001295 * srpt_abort_cmd() - Abort a SCSI command.
1296 * @ioctx: I/O context associated with the SCSI command.
1297 * @context: Preferred execution context.
1298 */
1299static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1300{
1301 enum srpt_command_state state;
1302 unsigned long flags;
1303
1304 BUG_ON(!ioctx);
1305
1306 /*
1307 * If the command is in a state where the target core is waiting for
1308 * the ib_srpt driver, change the state to the next state. Changing
1309 * the state of the command from SRPT_STATE_NEED_DATA to
1310 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1311 * function a second time.
1312 */
1313
1314 spin_lock_irqsave(&ioctx->spinlock, flags);
1315 state = ioctx->state;
1316 switch (state) {
1317 case SRPT_STATE_NEED_DATA:
1318 ioctx->state = SRPT_STATE_DATA_IN;
1319 break;
1320 case SRPT_STATE_DATA_IN:
1321 case SRPT_STATE_CMD_RSP_SENT:
1322 case SRPT_STATE_MGMT_RSP_SENT:
1323 ioctx->state = SRPT_STATE_DONE;
1324 break;
1325 default:
1326 break;
1327 }
1328 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1329
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001330 if (state == SRPT_STATE_DONE) {
1331 struct srpt_rdma_ch *ch = ioctx->ch;
1332
1333 BUG_ON(ch->sess == NULL);
1334
Bart Van Asscheafc16602015-04-27 13:52:36 +02001335 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001336 goto out;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001337 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001338
1339 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
Bart Van Assche649ee052015-04-14 13:26:44 +02001340 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001341
1342 switch (state) {
1343 case SRPT_STATE_NEW:
1344 case SRPT_STATE_DATA_IN:
1345 case SRPT_STATE_MGMT:
1346 /*
1347 * Do nothing - defer abort processing until
1348 * srpt_queue_response() is invoked.
1349 */
1350 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1351 break;
1352 case SRPT_STATE_NEED_DATA:
1353 /* DMA_TO_DEVICE (write) - RDMA read error. */
Christoph Hellwige672a472012-07-08 15:58:43 -04001354
1355 /* XXX(hch): this is a horrible layering violation.. */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001356 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
Christoph Hellwige672a472012-07-08 15:58:43 -04001357 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001358 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001359 break;
1360 case SRPT_STATE_CMD_RSP_SENT:
1361 /*
1362 * SRP_RSP sending failed or the SRP_RSP send completion has
1363 * not been received in time.
1364 */
1365 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001366 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001367 break;
1368 case SRPT_STATE_MGMT_RSP_SENT:
1369 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001370 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001371 break;
1372 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001373 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001374 break;
1375 }
1376
1377out:
1378 return state;
1379}
1380
1381/**
1382 * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1383 */
1384static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1385{
1386 struct srpt_send_ioctx *ioctx;
1387 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001388 u32 index;
1389
1390 atomic_inc(&ch->sq_wr_avail);
1391
1392 index = idx_from_wr_id(wr_id);
1393 ioctx = ch->ioctx_ring[index];
1394 state = srpt_get_cmd_state(ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001395
1396 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1397 && state != SRPT_STATE_MGMT_RSP_SENT
1398 && state != SRPT_STATE_NEED_DATA
1399 && state != SRPT_STATE_DONE);
1400
1401 /* If SRP_RSP sending failed, undo the ch->req_lim change. */
1402 if (state == SRPT_STATE_CMD_RSP_SENT
1403 || state == SRPT_STATE_MGMT_RSP_SENT)
1404 atomic_dec(&ch->req_lim);
1405
1406 srpt_abort_cmd(ioctx);
1407}
1408
1409/**
1410 * srpt_handle_send_comp() - Process an IB send completion notification.
1411 */
1412static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1413 struct srpt_send_ioctx *ioctx)
1414{
1415 enum srpt_command_state state;
1416
1417 atomic_inc(&ch->sq_wr_avail);
1418
1419 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1420
1421 if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1422 && state != SRPT_STATE_MGMT_RSP_SENT
1423 && state != SRPT_STATE_DONE))
1424 pr_debug("state = %d\n", state);
1425
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001426 if (state != SRPT_STATE_DONE) {
1427 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1428 transport_generic_free_cmd(&ioctx->cmd, 0);
1429 } else {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001430 pr_err("IB completion has been received too late for"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001431 " wr_id = %u.\n", ioctx->ioctx.index);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001432 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001433}
1434
1435/**
1436 * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1437 *
Christoph Hellwige672a472012-07-08 15:58:43 -04001438 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1439 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001440 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001441 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001442 */
1443static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1444 struct srpt_send_ioctx *ioctx,
1445 enum srpt_opcode opcode)
1446{
1447 WARN_ON(ioctx->n_rdma <= 0);
1448 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1449
1450 if (opcode == SRPT_RDMA_READ_LAST) {
1451 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1452 SRPT_STATE_DATA_IN))
Christoph Hellwige672a472012-07-08 15:58:43 -04001453 target_execute_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001454 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001455 pr_err("%s[%d]: wrong state = %d\n", __func__,
Bart Van Asschea42d9852011-10-14 01:30:46 +00001456 __LINE__, srpt_get_cmd_state(ioctx));
1457 } else if (opcode == SRPT_RDMA_ABORT) {
1458 ioctx->rdma_aborted = true;
1459 } else {
1460 WARN(true, "unexpected opcode %d\n", opcode);
1461 }
1462}
1463
1464/**
1465 * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1466 */
1467static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1468 struct srpt_send_ioctx *ioctx,
1469 enum srpt_opcode opcode)
1470{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001471 enum srpt_command_state state;
1472
Bart Van Asschea42d9852011-10-14 01:30:46 +00001473 state = srpt_get_cmd_state(ioctx);
1474 switch (opcode) {
1475 case SRPT_RDMA_READ_LAST:
1476 if (ioctx->n_rdma <= 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001477 pr_err("Received invalid RDMA read"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001478 " error completion with idx %d\n",
1479 ioctx->ioctx.index);
1480 break;
1481 }
1482 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1483 if (state == SRPT_STATE_NEED_DATA)
1484 srpt_abort_cmd(ioctx);
1485 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001486 pr_err("%s[%d]: wrong state = %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001487 __func__, __LINE__, state);
1488 break;
1489 case SRPT_RDMA_WRITE_LAST:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001490 break;
1491 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001492 pr_err("%s[%d]: opcode = %u\n", __func__, __LINE__, opcode);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001493 break;
1494 }
1495}
1496
1497/**
1498 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1499 * @ch: RDMA channel through which the request has been received.
1500 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1501 * be built in the buffer ioctx->buf points at and hence this function will
1502 * overwrite the request data.
1503 * @tag: tag of the request for which this response is being generated.
1504 * @status: value for the STATUS field of the SRP_RSP information unit.
1505 *
1506 * Returns the size in bytes of the SRP_RSP response.
1507 *
1508 * An SRP_RSP response contains a SCSI status or service response. See also
1509 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1510 * response. See also SPC-2 for more information about sense data.
1511 */
1512static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1513 struct srpt_send_ioctx *ioctx, u64 tag,
1514 int status)
1515{
1516 struct srp_rsp *srp_rsp;
1517 const u8 *sense_data;
1518 int sense_data_len, max_sense_len;
1519
1520 /*
1521 * The lowest bit of all SAM-3 status codes is zero (see also
1522 * paragraph 5.3 in SAM-3).
1523 */
1524 WARN_ON(status & 1);
1525
1526 srp_rsp = ioctx->ioctx.buf;
1527 BUG_ON(!srp_rsp);
1528
1529 sense_data = ioctx->sense_data;
1530 sense_data_len = ioctx->cmd.scsi_sense_length;
1531 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1532
1533 memset(srp_rsp, 0, sizeof *srp_rsp);
1534 srp_rsp->opcode = SRP_RSP;
1535 srp_rsp->req_lim_delta =
1536 __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1537 srp_rsp->tag = tag;
1538 srp_rsp->status = status;
1539
1540 if (sense_data_len) {
1541 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1542 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1543 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001544 pr_warn("truncated sense data from %d to %d"
1545 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001546 sense_data_len = max_sense_len;
1547 }
1548
1549 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1550 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1551 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1552 }
1553
1554 return sizeof(*srp_rsp) + sense_data_len;
1555}
1556
1557/**
1558 * srpt_build_tskmgmt_rsp() - Build a task management response.
1559 * @ch: RDMA channel through which the request has been received.
1560 * @ioctx: I/O context in which the SRP_RSP response will be built.
1561 * @rsp_code: RSP_CODE that will be stored in the response.
1562 * @tag: Tag of the request for which this response is being generated.
1563 *
1564 * Returns the size in bytes of the SRP_RSP response.
1565 *
1566 * An SRP_RSP response contains a SCSI status or service response. See also
1567 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1568 * response.
1569 */
1570static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1571 struct srpt_send_ioctx *ioctx,
1572 u8 rsp_code, u64 tag)
1573{
1574 struct srp_rsp *srp_rsp;
1575 int resp_data_len;
1576 int resp_len;
1577
Jack Wangc807f642013-09-30 10:09:05 +02001578 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001579 resp_len = sizeof(*srp_rsp) + resp_data_len;
1580
1581 srp_rsp = ioctx->ioctx.buf;
1582 BUG_ON(!srp_rsp);
1583 memset(srp_rsp, 0, sizeof *srp_rsp);
1584
1585 srp_rsp->opcode = SRP_RSP;
1586 srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
1587 + atomic_xchg(&ch->req_lim_delta, 0));
1588 srp_rsp->tag = tag;
1589
Jack Wangc807f642013-09-30 10:09:05 +02001590 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1591 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1592 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001593
1594 return resp_len;
1595}
1596
1597#define NO_SUCH_LUN ((uint64_t)-1LL)
1598
1599/*
1600 * SCSI LUN addressing method. See also SAM-2 and the section about
1601 * eight byte LUNs.
1602 */
1603enum scsi_lun_addr_method {
1604 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1605 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1606 SCSI_LUN_ADDR_METHOD_LUN = 2,
1607 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1608};
1609
1610/*
1611 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1612 *
1613 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1614 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1615 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1616 */
1617static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1618{
1619 uint64_t res = NO_SUCH_LUN;
1620 int addressing_method;
1621
1622 if (unlikely(len < 2)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001623 pr_err("Illegal LUN length %d, expected 2 bytes or more\n",
1624 len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001625 goto out;
1626 }
1627
1628 switch (len) {
1629 case 8:
1630 if ((*((__be64 *)lun) &
1631 __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1632 goto out_err;
1633 break;
1634 case 4:
1635 if (*((__be16 *)&lun[2]) != 0)
1636 goto out_err;
1637 break;
1638 case 6:
1639 if (*((__be32 *)&lun[2]) != 0)
1640 goto out_err;
1641 break;
1642 case 2:
1643 break;
1644 default:
1645 goto out_err;
1646 }
1647
1648 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1649 switch (addressing_method) {
1650 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1651 case SCSI_LUN_ADDR_METHOD_FLAT:
1652 case SCSI_LUN_ADDR_METHOD_LUN:
1653 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1654 break;
1655
1656 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1657 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001658 pr_err("Unimplemented LUN addressing method %u\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001659 addressing_method);
1660 break;
1661 }
1662
1663out:
1664 return res;
1665
1666out_err:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001667 pr_err("Support for multi-level LUNs has not yet been implemented\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001668 goto out;
1669}
1670
1671static int srpt_check_stop_free(struct se_cmd *cmd)
1672{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001673 struct srpt_send_ioctx *ioctx = container_of(cmd,
1674 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001675
Bart Van Asscheafc16602015-04-27 13:52:36 +02001676 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001677}
1678
1679/**
1680 * srpt_handle_cmd() - Process SRP_CMD.
1681 */
1682static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1683 struct srpt_recv_ioctx *recv_ioctx,
1684 struct srpt_send_ioctx *send_ioctx)
1685{
1686 struct se_cmd *cmd;
1687 struct srp_cmd *srp_cmd;
1688 uint64_t unpacked_lun;
1689 u64 data_len;
1690 enum dma_data_direction dir;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001691 sense_reason_t ret;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001692 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001693
1694 BUG_ON(!send_ioctx);
1695
1696 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001697 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001698 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001699
1700 switch (srp_cmd->task_attr) {
1701 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001702 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001703 break;
1704 case SRP_CMD_ORDERED_Q:
1705 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001706 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001707 break;
1708 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001709 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001710 break;
1711 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001712 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001713 break;
1714 }
1715
Christoph Hellwigde103c92012-11-06 12:24:09 -08001716 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001717 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001718 srp_cmd->tag);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001719 ret = TCM_INVALID_CDB_FIELD;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001720 goto send_sense;
1721 }
1722
Bart Van Asschea42d9852011-10-14 01:30:46 +00001723 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1724 sizeof(srp_cmd->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001725 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1726 &send_ioctx->sense_data[0], unpacked_lun, data_len,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001727 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001728 if (rc != 0) {
1729 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001730 goto send_sense;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001731 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001732 return 0;
1733
1734send_sense:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001735 transport_send_check_condition_and_sense(cmd, ret, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001736 return -1;
1737}
1738
1739/**
1740 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1741 * @ch: RDMA channel of the task management request.
1742 * @fn: Task management function to perform.
1743 * @req_tag: Tag of the SRP task management request.
1744 * @mgmt_ioctx: I/O context of the task management request.
1745 *
1746 * Returns zero if the target core will process the task management
1747 * request asynchronously.
1748 *
1749 * Note: It is assumed that the initiator serializes tag-based task management
1750 * requests.
1751 */
1752static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1753{
1754 struct srpt_device *sdev;
1755 struct srpt_rdma_ch *ch;
1756 struct srpt_send_ioctx *target;
1757 int ret, i;
1758
1759 ret = -EINVAL;
1760 ch = ioctx->ch;
1761 BUG_ON(!ch);
1762 BUG_ON(!ch->sport);
1763 sdev = ch->sport->sdev;
1764 BUG_ON(!sdev);
1765 spin_lock_irq(&sdev->spinlock);
1766 for (i = 0; i < ch->rq_size; ++i) {
1767 target = ch->ioctx_ring[i];
1768 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
Bart Van Assche649ee052015-04-14 13:26:44 +02001769 target->cmd.tag == tag &&
Bart Van Asschea42d9852011-10-14 01:30:46 +00001770 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1771 ret = 0;
1772 /* now let the target core abort &target->cmd; */
1773 break;
1774 }
1775 }
1776 spin_unlock_irq(&sdev->spinlock);
1777 return ret;
1778}
1779
1780static int srp_tmr_to_tcm(int fn)
1781{
1782 switch (fn) {
1783 case SRP_TSK_ABORT_TASK:
1784 return TMR_ABORT_TASK;
1785 case SRP_TSK_ABORT_TASK_SET:
1786 return TMR_ABORT_TASK_SET;
1787 case SRP_TSK_CLEAR_TASK_SET:
1788 return TMR_CLEAR_TASK_SET;
1789 case SRP_TSK_LUN_RESET:
1790 return TMR_LUN_RESET;
1791 case SRP_TSK_CLEAR_ACA:
1792 return TMR_CLEAR_ACA;
1793 default:
1794 return -1;
1795 }
1796}
1797
1798/**
1799 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1800 *
1801 * Returns 0 if and only if the request will be processed by the target core.
1802 *
1803 * For more information about SRP_TSK_MGMT information units, see also section
1804 * 6.7 in the SRP r16a document.
1805 */
1806static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1807 struct srpt_recv_ioctx *recv_ioctx,
1808 struct srpt_send_ioctx *send_ioctx)
1809{
1810 struct srp_tsk_mgmt *srp_tsk;
1811 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001812 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001813 uint64_t unpacked_lun;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001814 uint32_t tag = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001815 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001816 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001817
1818 BUG_ON(!send_ioctx);
1819
1820 srp_tsk = recv_ioctx->ioctx.buf;
1821 cmd = &send_ioctx->cmd;
1822
1823 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1824 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1825 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1826
1827 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001828 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001829 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1830 if (tcm_tmr < 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001831 send_ioctx->cmd.se_tmr_req->response =
1832 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001833 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001834 }
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001835 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1836 sizeof(srp_tsk->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001837
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001838 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK) {
1839 rc = srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1840 if (rc < 0) {
1841 send_ioctx->cmd.se_tmr_req->response =
1842 TMR_TASK_DOES_NOT_EXIST;
1843 goto fail;
1844 }
1845 tag = srp_tsk->task_tag;
1846 }
1847 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
1848 srp_tsk, tcm_tmr, GFP_KERNEL, tag,
1849 TARGET_SCF_ACK_KREF);
1850 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001851 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001852 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001853 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001854 return;
1855fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001856 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001857}
1858
1859/**
1860 * srpt_handle_new_iu() - Process a newly received information unit.
1861 * @ch: RDMA channel through which the information unit has been received.
1862 * @ioctx: SRPT I/O context associated with the information unit.
1863 */
1864static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1865 struct srpt_recv_ioctx *recv_ioctx,
1866 struct srpt_send_ioctx *send_ioctx)
1867{
1868 struct srp_cmd *srp_cmd;
1869 enum rdma_ch_state ch_state;
1870
1871 BUG_ON(!ch);
1872 BUG_ON(!recv_ioctx);
1873
1874 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1875 recv_ioctx->ioctx.dma, srp_max_req_size,
1876 DMA_FROM_DEVICE);
1877
1878 ch_state = srpt_get_ch_state(ch);
1879 if (unlikely(ch_state == CH_CONNECTING)) {
1880 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1881 goto out;
1882 }
1883
1884 if (unlikely(ch_state != CH_LIVE))
1885 goto out;
1886
1887 srp_cmd = recv_ioctx->ioctx.buf;
1888 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1889 if (!send_ioctx)
1890 send_ioctx = srpt_get_send_ioctx(ch);
1891 if (unlikely(!send_ioctx)) {
1892 list_add_tail(&recv_ioctx->wait_list,
1893 &ch->cmd_wait_list);
1894 goto out;
1895 }
1896 }
1897
Bart Van Asschea42d9852011-10-14 01:30:46 +00001898 switch (srp_cmd->opcode) {
1899 case SRP_CMD:
1900 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1901 break;
1902 case SRP_TSK_MGMT:
1903 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1904 break;
1905 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001906 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001907 break;
1908 case SRP_CRED_RSP:
1909 pr_debug("received SRP_CRED_RSP\n");
1910 break;
1911 case SRP_AER_RSP:
1912 pr_debug("received SRP_AER_RSP\n");
1913 break;
1914 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001915 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001916 break;
1917 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001918 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001919 srp_cmd->opcode);
1920 break;
1921 }
1922
1923 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1924out:
1925 return;
1926}
1927
1928static void srpt_process_rcv_completion(struct ib_cq *cq,
1929 struct srpt_rdma_ch *ch,
1930 struct ib_wc *wc)
1931{
1932 struct srpt_device *sdev = ch->sport->sdev;
1933 struct srpt_recv_ioctx *ioctx;
1934 u32 index;
1935
1936 index = idx_from_wr_id(wc->wr_id);
1937 if (wc->status == IB_WC_SUCCESS) {
1938 int req_lim;
1939
1940 req_lim = atomic_dec_return(&ch->req_lim);
1941 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001942 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001943 ioctx = sdev->ioctx_ring[index];
1944 srpt_handle_new_iu(ch, ioctx, NULL);
1945 } else {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001946 pr_info("receiving failed for idx %u with status %d\n",
1947 index, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001948 }
1949}
1950
1951/**
1952 * srpt_process_send_completion() - Process an IB send completion.
1953 *
1954 * Note: Although this has not yet been observed during tests, at least in
1955 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1956 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1957 * value in each response is set to one, and it is possible that this response
1958 * makes the initiator send a new request before the send completion for that
1959 * response has been processed. This could e.g. happen if the call to
1960 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1961 * if IB retransmission causes generation of the send completion to be
1962 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1963 * are queued on cmd_wait_list. The code below processes these delayed
1964 * requests one at a time.
1965 */
1966static void srpt_process_send_completion(struct ib_cq *cq,
1967 struct srpt_rdma_ch *ch,
1968 struct ib_wc *wc)
1969{
1970 struct srpt_send_ioctx *send_ioctx;
1971 uint32_t index;
1972 enum srpt_opcode opcode;
1973
1974 index = idx_from_wr_id(wc->wr_id);
1975 opcode = opcode_from_wr_id(wc->wr_id);
1976 send_ioctx = ch->ioctx_ring[index];
1977 if (wc->status == IB_WC_SUCCESS) {
1978 if (opcode == SRPT_SEND)
1979 srpt_handle_send_comp(ch, send_ioctx);
1980 else {
1981 WARN_ON(opcode != SRPT_RDMA_ABORT &&
1982 wc->opcode != IB_WC_RDMA_READ);
1983 srpt_handle_rdma_comp(ch, send_ioctx, opcode);
1984 }
1985 } else {
1986 if (opcode == SRPT_SEND) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001987 pr_info("sending response for idx %u failed"
1988 " with status %d\n", index, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001989 srpt_handle_send_err_comp(ch, wc->wr_id);
1990 } else if (opcode != SRPT_RDMA_MID) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001991 pr_info("RDMA t %d for idx %u failed with"
1992 " status %d\n", opcode, index, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001993 srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
1994 }
1995 }
1996
1997 while (unlikely(opcode == SRPT_SEND
1998 && !list_empty(&ch->cmd_wait_list)
1999 && srpt_get_ch_state(ch) == CH_LIVE
2000 && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
2001 struct srpt_recv_ioctx *recv_ioctx;
2002
2003 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
2004 struct srpt_recv_ioctx,
2005 wait_list);
2006 list_del(&recv_ioctx->wait_list);
2007 srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
2008 }
2009}
2010
2011static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
2012{
2013 struct ib_wc *const wc = ch->wc;
2014 int i, n;
2015
2016 WARN_ON(cq != ch->cq);
2017
2018 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
2019 while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
2020 for (i = 0; i < n; i++) {
2021 if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
2022 srpt_process_rcv_completion(cq, ch, &wc[i]);
2023 else
2024 srpt_process_send_completion(cq, ch, &wc[i]);
2025 }
2026 }
2027}
2028
2029/**
2030 * srpt_completion() - IB completion queue callback function.
2031 *
2032 * Notes:
2033 * - It is guaranteed that a completion handler will never be invoked
2034 * concurrently on two different CPUs for the same completion queue. See also
2035 * Documentation/infiniband/core_locking.txt and the implementation of
2036 * handle_edge_irq() in kernel/irq/chip.c.
2037 * - When threaded IRQs are enabled, completion handlers are invoked in thread
2038 * context instead of interrupt context.
2039 */
2040static void srpt_completion(struct ib_cq *cq, void *ctx)
2041{
2042 struct srpt_rdma_ch *ch = ctx;
2043
2044 wake_up_interruptible(&ch->wait_queue);
2045}
2046
2047static int srpt_compl_thread(void *arg)
2048{
2049 struct srpt_rdma_ch *ch;
2050
2051 /* Hibernation / freezing of the SRPT kernel thread is not supported. */
2052 current->flags |= PF_NOFREEZE;
2053
2054 ch = arg;
2055 BUG_ON(!ch);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002056 pr_info("Session %s: kernel thread %s (PID %d) started\n",
2057 ch->sess_name, ch->thread->comm, current->pid);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002058 while (!kthread_should_stop()) {
2059 wait_event_interruptible(ch->wait_queue,
2060 (srpt_process_completion(ch->cq, ch),
2061 kthread_should_stop()));
2062 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002063 pr_info("Session %s: kernel thread %s (PID %d) stopped\n",
2064 ch->sess_name, ch->thread->comm, current->pid);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002065 return 0;
2066}
2067
2068/**
2069 * srpt_create_ch_ib() - Create receive and send completion queues.
2070 */
2071static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2072{
2073 struct ib_qp_init_attr *qp_init;
2074 struct srpt_port *sport = ch->sport;
2075 struct srpt_device *sdev = sport->sdev;
2076 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
2077 int ret;
2078
2079 WARN_ON(ch->rq_size < 1);
2080
2081 ret = -ENOMEM;
2082 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2083 if (!qp_init)
2084 goto out;
2085
Bart Van Asscheab477c12014-10-19 18:05:33 +03002086retry:
Bart Van Asschea42d9852011-10-14 01:30:46 +00002087 ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
2088 ch->rq_size + srp_sq_size, 0);
2089 if (IS_ERR(ch->cq)) {
2090 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002091 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002092 ch->rq_size + srp_sq_size, ret);
2093 goto out;
2094 }
2095
2096 qp_init->qp_context = (void *)ch;
2097 qp_init->event_handler
2098 = (void(*)(struct ib_event *, void*))srpt_qp_event;
2099 qp_init->send_cq = ch->cq;
2100 qp_init->recv_cq = ch->cq;
2101 qp_init->srq = sdev->srq;
2102 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2103 qp_init->qp_type = IB_QPT_RC;
2104 qp_init->cap.max_send_wr = srp_sq_size;
2105 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2106
2107 ch->qp = ib_create_qp(sdev->pd, qp_init);
2108 if (IS_ERR(ch->qp)) {
2109 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03002110 if (ret == -ENOMEM) {
2111 srp_sq_size /= 2;
2112 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
2113 ib_destroy_cq(ch->cq);
2114 goto retry;
2115 }
2116 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002117 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002118 goto err_destroy_cq;
2119 }
2120
2121 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2122
2123 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2124 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2125 qp_init->cap.max_send_wr, ch->cm_id);
2126
2127 ret = srpt_init_ch_qp(ch, ch->qp);
2128 if (ret)
2129 goto err_destroy_qp;
2130
2131 init_waitqueue_head(&ch->wait_queue);
2132
2133 pr_debug("creating thread for session %s\n", ch->sess_name);
2134
2135 ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2136 if (IS_ERR(ch->thread)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002137 pr_err("failed to create kernel thread %ld\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002138 PTR_ERR(ch->thread));
2139 ch->thread = NULL;
2140 goto err_destroy_qp;
2141 }
2142
2143out:
2144 kfree(qp_init);
2145 return ret;
2146
2147err_destroy_qp:
2148 ib_destroy_qp(ch->qp);
2149err_destroy_cq:
2150 ib_destroy_cq(ch->cq);
2151 goto out;
2152}
2153
2154static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2155{
2156 if (ch->thread)
2157 kthread_stop(ch->thread);
2158
2159 ib_destroy_qp(ch->qp);
2160 ib_destroy_cq(ch->cq);
2161}
2162
2163/**
2164 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2165 *
2166 * Reset the QP and make sure all resources associated with the channel will
2167 * be deallocated at an appropriate time.
2168 *
2169 * Note: The caller must hold ch->sport->sdev->spinlock.
2170 */
2171static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2172{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002173 enum rdma_ch_state prev_state;
2174 unsigned long flags;
2175
Bart Van Asschea42d9852011-10-14 01:30:46 +00002176 spin_lock_irqsave(&ch->spinlock, flags);
2177 prev_state = ch->state;
2178 switch (prev_state) {
2179 case CH_CONNECTING:
2180 case CH_LIVE:
2181 ch->state = CH_DISCONNECTING;
2182 break;
2183 default:
2184 break;
2185 }
2186 spin_unlock_irqrestore(&ch->spinlock, flags);
2187
2188 switch (prev_state) {
2189 case CH_CONNECTING:
2190 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2191 NULL, 0);
2192 /* fall through */
2193 case CH_LIVE:
2194 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002195 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002196 break;
2197 case CH_DISCONNECTING:
2198 break;
2199 case CH_DRAINING:
2200 case CH_RELEASING:
2201 break;
2202 }
2203}
2204
2205/**
2206 * srpt_close_ch() - Close an RDMA channel.
2207 */
2208static void srpt_close_ch(struct srpt_rdma_ch *ch)
2209{
2210 struct srpt_device *sdev;
2211
2212 sdev = ch->sport->sdev;
2213 spin_lock_irq(&sdev->spinlock);
2214 __srpt_close_ch(ch);
2215 spin_unlock_irq(&sdev->spinlock);
2216}
2217
2218/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002219 * srpt_shutdown_session() - Whether or not a session may be shut down.
2220 */
2221static int srpt_shutdown_session(struct se_session *se_sess)
2222{
2223 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2224 unsigned long flags;
2225
2226 spin_lock_irqsave(&ch->spinlock, flags);
2227 if (ch->in_shutdown) {
2228 spin_unlock_irqrestore(&ch->spinlock, flags);
2229 return true;
2230 }
2231
2232 ch->in_shutdown = true;
2233 target_sess_cmd_list_set_waiting(se_sess);
2234 spin_unlock_irqrestore(&ch->spinlock, flags);
2235
2236 return true;
2237}
2238
2239/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002240 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2241 * @cm_id: Pointer to the CM ID of the channel to be drained.
2242 *
2243 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2244 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2245 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2246 * waits until all target sessions for the associated IB device have been
2247 * unregistered and target session registration involves a call to
2248 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2249 * this function has finished).
2250 */
2251static void srpt_drain_channel(struct ib_cm_id *cm_id)
2252{
2253 struct srpt_device *sdev;
2254 struct srpt_rdma_ch *ch;
2255 int ret;
2256 bool do_reset = false;
2257
2258 WARN_ON_ONCE(irqs_disabled());
2259
2260 sdev = cm_id->context;
2261 BUG_ON(!sdev);
2262 spin_lock_irq(&sdev->spinlock);
2263 list_for_each_entry(ch, &sdev->rch_list, list) {
2264 if (ch->cm_id == cm_id) {
2265 do_reset = srpt_test_and_set_ch_state(ch,
2266 CH_CONNECTING, CH_DRAINING) ||
2267 srpt_test_and_set_ch_state(ch,
2268 CH_LIVE, CH_DRAINING) ||
2269 srpt_test_and_set_ch_state(ch,
2270 CH_DISCONNECTING, CH_DRAINING);
2271 break;
2272 }
2273 }
2274 spin_unlock_irq(&sdev->spinlock);
2275
2276 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002277 if (ch->sess)
2278 srpt_shutdown_session(ch->sess);
2279
Bart Van Asschea42d9852011-10-14 01:30:46 +00002280 ret = srpt_ch_qp_err(ch);
2281 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002282 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002283 " failed: %d\n", ret);
2284 }
2285}
2286
2287/**
2288 * srpt_find_channel() - Look up an RDMA channel.
2289 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2290 *
2291 * Return NULL if no matching RDMA channel has been found.
2292 */
2293static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2294 struct ib_cm_id *cm_id)
2295{
2296 struct srpt_rdma_ch *ch;
2297 bool found;
2298
2299 WARN_ON_ONCE(irqs_disabled());
2300 BUG_ON(!sdev);
2301
2302 found = false;
2303 spin_lock_irq(&sdev->spinlock);
2304 list_for_each_entry(ch, &sdev->rch_list, list) {
2305 if (ch->cm_id == cm_id) {
2306 found = true;
2307 break;
2308 }
2309 }
2310 spin_unlock_irq(&sdev->spinlock);
2311
2312 return found ? ch : NULL;
2313}
2314
2315/**
2316 * srpt_release_channel() - Release channel resources.
2317 *
2318 * Schedules the actual release because:
2319 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2320 * trigger a deadlock.
2321 * - It is not safe to call TCM transport_* functions from interrupt context.
2322 */
2323static void srpt_release_channel(struct srpt_rdma_ch *ch)
2324{
2325 schedule_work(&ch->release_work);
2326}
2327
2328static void srpt_release_channel_work(struct work_struct *w)
2329{
2330 struct srpt_rdma_ch *ch;
2331 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002332 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002333
2334 ch = container_of(w, struct srpt_rdma_ch, release_work);
2335 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2336 ch->release_done);
2337
2338 sdev = ch->sport->sdev;
2339 BUG_ON(!sdev);
2340
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002341 se_sess = ch->sess;
2342 BUG_ON(!se_sess);
2343
Joern Engelbe646c2d2013-05-15 00:44:07 -07002344 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002345
2346 transport_deregister_session_configfs(se_sess);
2347 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002348 ch->sess = NULL;
2349
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002350 ib_destroy_cm_id(ch->cm_id);
2351
Bart Van Asschea42d9852011-10-14 01:30:46 +00002352 srpt_destroy_ch_ib(ch);
2353
2354 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2355 ch->sport->sdev, ch->rq_size,
2356 ch->rsp_size, DMA_TO_DEVICE);
2357
2358 spin_lock_irq(&sdev->spinlock);
2359 list_del(&ch->list);
2360 spin_unlock_irq(&sdev->spinlock);
2361
Bart Van Asschea42d9852011-10-14 01:30:46 +00002362 if (ch->release_done)
2363 complete(ch->release_done);
2364
2365 wake_up(&sdev->ch_releaseQ);
2366
2367 kfree(ch);
2368}
2369
2370static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2371 u8 i_port_id[16])
2372{
2373 struct srpt_node_acl *nacl;
2374
2375 list_for_each_entry(nacl, &sport->port_acl_list, list)
2376 if (memcmp(nacl->i_port_id, i_port_id,
2377 sizeof(nacl->i_port_id)) == 0)
2378 return nacl;
2379
2380 return NULL;
2381}
2382
2383static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2384 u8 i_port_id[16])
2385{
2386 struct srpt_node_acl *nacl;
2387
2388 spin_lock_irq(&sport->port_acl_lock);
2389 nacl = __srpt_lookup_acl(sport, i_port_id);
2390 spin_unlock_irq(&sport->port_acl_lock);
2391
2392 return nacl;
2393}
2394
2395/**
2396 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2397 *
2398 * Ownership of the cm_id is transferred to the target session if this
2399 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2400 */
2401static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2402 struct ib_cm_req_event_param *param,
2403 void *private_data)
2404{
2405 struct srpt_device *sdev = cm_id->context;
2406 struct srpt_port *sport = &sdev->port[param->port - 1];
2407 struct srp_login_req *req;
2408 struct srp_login_rsp *rsp;
2409 struct srp_login_rej *rej;
2410 struct ib_cm_rep_param *rep_param;
2411 struct srpt_rdma_ch *ch, *tmp_ch;
2412 struct srpt_node_acl *nacl;
2413 u32 it_iu_len;
2414 int i;
2415 int ret = 0;
2416
2417 WARN_ON_ONCE(irqs_disabled());
2418
2419 if (WARN_ON(!sdev || !private_data))
2420 return -EINVAL;
2421
2422 req = (struct srp_login_req *)private_data;
2423
2424 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2425
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002426 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2427 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2428 " (guid=0x%llx:0x%llx)\n",
2429 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2430 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2431 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2432 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2433 it_iu_len,
2434 param->port,
2435 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2436 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002437
2438 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2439 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2440 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2441
2442 if (!rsp || !rej || !rep_param) {
2443 ret = -ENOMEM;
2444 goto out;
2445 }
2446
2447 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2448 rej->reason = __constant_cpu_to_be32(
2449 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2450 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002451 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002452 " length (%d bytes) is out of range (%d .. %d)\n",
2453 it_iu_len, 64, srp_max_req_size);
2454 goto reject;
2455 }
2456
2457 if (!sport->enabled) {
2458 rej->reason = __constant_cpu_to_be32(
2459 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2460 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002461 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002462 " has not yet been enabled\n");
2463 goto reject;
2464 }
2465
2466 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2467 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2468
2469 spin_lock_irq(&sdev->spinlock);
2470
2471 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2472 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2473 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2474 && param->port == ch->sport->port
2475 && param->listen_id == ch->sport->sdev->cm_id
2476 && ch->cm_id) {
2477 enum rdma_ch_state ch_state;
2478
2479 ch_state = srpt_get_ch_state(ch);
2480 if (ch_state != CH_CONNECTING
2481 && ch_state != CH_LIVE)
2482 continue;
2483
2484 /* found an existing channel */
2485 pr_debug("Found existing channel %s"
2486 " cm_id= %p state= %d\n",
2487 ch->sess_name, ch->cm_id, ch_state);
2488
2489 __srpt_close_ch(ch);
2490
2491 rsp->rsp_flags =
2492 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2493 }
2494 }
2495
2496 spin_unlock_irq(&sdev->spinlock);
2497
2498 } else
2499 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2500
2501 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2502 || *(__be64 *)(req->target_port_id + 8) !=
2503 cpu_to_be64(srpt_service_guid)) {
2504 rej->reason = __constant_cpu_to_be32(
2505 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2506 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002507 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002508 " has an invalid target port identifier.\n");
2509 goto reject;
2510 }
2511
2512 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2513 if (!ch) {
2514 rej->reason = __constant_cpu_to_be32(
2515 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002516 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002517 ret = -ENOMEM;
2518 goto reject;
2519 }
2520
2521 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2522 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2523 memcpy(ch->t_port_id, req->target_port_id, 16);
2524 ch->sport = &sdev->port[param->port - 1];
2525 ch->cm_id = cm_id;
2526 /*
2527 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2528 * for the SRP protocol to the command queue size.
2529 */
2530 ch->rq_size = SRPT_RQ_SIZE;
2531 spin_lock_init(&ch->spinlock);
2532 ch->state = CH_CONNECTING;
2533 INIT_LIST_HEAD(&ch->cmd_wait_list);
2534 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2535
2536 ch->ioctx_ring = (struct srpt_send_ioctx **)
2537 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2538 sizeof(*ch->ioctx_ring[0]),
2539 ch->rsp_size, DMA_TO_DEVICE);
2540 if (!ch->ioctx_ring)
2541 goto free_ch;
2542
2543 INIT_LIST_HEAD(&ch->free_list);
2544 for (i = 0; i < ch->rq_size; i++) {
2545 ch->ioctx_ring[i]->ch = ch;
2546 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2547 }
2548
2549 ret = srpt_create_ch_ib(ch);
2550 if (ret) {
2551 rej->reason = __constant_cpu_to_be32(
2552 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002553 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002554 " a new RDMA channel failed.\n");
2555 goto free_ring;
2556 }
2557
2558 ret = srpt_ch_qp_rtr(ch, ch->qp);
2559 if (ret) {
2560 rej->reason = __constant_cpu_to_be32(
2561 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002562 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002563 " RTR failed (error code = %d)\n", ret);
2564 goto destroy_ib;
2565 }
2566 /*
2567 * Use the initator port identifier as the session name.
2568 */
2569 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2570 be64_to_cpu(*(__be64 *)ch->i_port_id),
2571 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2572
2573 pr_debug("registering session %s\n", ch->sess_name);
2574
2575 nacl = srpt_lookup_acl(sport, ch->i_port_id);
2576 if (!nacl) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002577 pr_info("Rejected login because no ACL has been"
2578 " configured yet for initiator %s.\n", ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002579 rej->reason = __constant_cpu_to_be32(
2580 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2581 goto destroy_ib;
2582 }
2583
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002584 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002585 if (IS_ERR(ch->sess)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002586 rej->reason = __constant_cpu_to_be32(
2587 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2588 pr_debug("Failed to create session\n");
2589 goto deregister_session;
2590 }
2591 ch->sess->se_node_acl = &nacl->nacl;
2592 transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2593
2594 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2595 ch->sess_name, ch->cm_id);
2596
2597 /* create srp_login_response */
2598 rsp->opcode = SRP_LOGIN_RSP;
2599 rsp->tag = req->tag;
2600 rsp->max_it_iu_len = req->req_it_iu_len;
2601 rsp->max_ti_iu_len = req->req_it_iu_len;
2602 ch->max_ti_iu_len = it_iu_len;
2603 rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2604 | SRP_BUF_FORMAT_INDIRECT);
2605 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2606 atomic_set(&ch->req_lim, ch->rq_size);
2607 atomic_set(&ch->req_lim_delta, 0);
2608
2609 /* create cm reply */
2610 rep_param->qp_num = ch->qp->qp_num;
2611 rep_param->private_data = (void *)rsp;
2612 rep_param->private_data_len = sizeof *rsp;
2613 rep_param->rnr_retry_count = 7;
2614 rep_param->flow_control = 1;
2615 rep_param->failover_accepted = 0;
2616 rep_param->srq = 1;
2617 rep_param->responder_resources = 4;
2618 rep_param->initiator_depth = 4;
2619
2620 ret = ib_send_cm_rep(cm_id, rep_param);
2621 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002622 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002623 " (error code = %d)\n", ret);
2624 goto release_channel;
2625 }
2626
2627 spin_lock_irq(&sdev->spinlock);
2628 list_add_tail(&ch->list, &sdev->rch_list);
2629 spin_unlock_irq(&sdev->spinlock);
2630
2631 goto out;
2632
2633release_channel:
2634 srpt_set_ch_state(ch, CH_RELEASING);
2635 transport_deregister_session_configfs(ch->sess);
2636
2637deregister_session:
2638 transport_deregister_session(ch->sess);
2639 ch->sess = NULL;
2640
2641destroy_ib:
2642 srpt_destroy_ch_ib(ch);
2643
2644free_ring:
2645 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2646 ch->sport->sdev, ch->rq_size,
2647 ch->rsp_size, DMA_TO_DEVICE);
2648free_ch:
2649 kfree(ch);
2650
2651reject:
2652 rej->opcode = SRP_LOGIN_REJ;
2653 rej->tag = req->tag;
2654 rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2655 | SRP_BUF_FORMAT_INDIRECT);
2656
2657 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2658 (void *)rej, sizeof *rej);
2659
2660out:
2661 kfree(rep_param);
2662 kfree(rsp);
2663 kfree(rej);
2664
2665 return ret;
2666}
2667
2668static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2669{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002670 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002671 srpt_drain_channel(cm_id);
2672}
2673
2674/**
2675 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2676 *
2677 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2678 * and that the recipient may begin transmitting (RTU = ready to use).
2679 */
2680static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2681{
2682 struct srpt_rdma_ch *ch;
2683 int ret;
2684
2685 ch = srpt_find_channel(cm_id->context, cm_id);
2686 BUG_ON(!ch);
2687
2688 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2689 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2690
2691 ret = srpt_ch_qp_rts(ch, ch->qp);
2692
2693 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2694 wait_list) {
2695 list_del(&ioctx->wait_list);
2696 srpt_handle_new_iu(ch, ioctx, NULL);
2697 }
2698 if (ret)
2699 srpt_close_ch(ch);
2700 }
2701}
2702
2703static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2704{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002705 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002706 srpt_drain_channel(cm_id);
2707}
2708
2709static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2710{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002711 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002712 srpt_drain_channel(cm_id);
2713}
2714
2715/**
2716 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2717 */
2718static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2719{
2720 struct srpt_rdma_ch *ch;
2721 unsigned long flags;
2722 bool send_drep = false;
2723
2724 ch = srpt_find_channel(cm_id->context, cm_id);
2725 BUG_ON(!ch);
2726
2727 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2728
2729 spin_lock_irqsave(&ch->spinlock, flags);
2730 switch (ch->state) {
2731 case CH_CONNECTING:
2732 case CH_LIVE:
2733 send_drep = true;
2734 ch->state = CH_DISCONNECTING;
2735 break;
2736 case CH_DISCONNECTING:
2737 case CH_DRAINING:
2738 case CH_RELEASING:
2739 WARN(true, "unexpected channel state %d\n", ch->state);
2740 break;
2741 }
2742 spin_unlock_irqrestore(&ch->spinlock, flags);
2743
2744 if (send_drep) {
2745 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002746 pr_err("Sending IB DREP failed.\n");
2747 pr_info("Received DREQ and sent DREP for session %s.\n",
2748 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002749 }
2750}
2751
2752/**
2753 * srpt_cm_drep_recv() - Process reception of a DREP message.
2754 */
2755static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2756{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002757 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002758 srpt_drain_channel(cm_id);
2759}
2760
2761/**
2762 * srpt_cm_handler() - IB connection manager callback function.
2763 *
2764 * A non-zero return value will cause the caller destroy the CM ID.
2765 *
2766 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2767 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2768 * a non-zero value in any other case will trigger a race with the
2769 * ib_destroy_cm_id() call in srpt_release_channel().
2770 */
2771static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2772{
2773 int ret;
2774
2775 ret = 0;
2776 switch (event->event) {
2777 case IB_CM_REQ_RECEIVED:
2778 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2779 event->private_data);
2780 break;
2781 case IB_CM_REJ_RECEIVED:
2782 srpt_cm_rej_recv(cm_id);
2783 break;
2784 case IB_CM_RTU_RECEIVED:
2785 case IB_CM_USER_ESTABLISHED:
2786 srpt_cm_rtu_recv(cm_id);
2787 break;
2788 case IB_CM_DREQ_RECEIVED:
2789 srpt_cm_dreq_recv(cm_id);
2790 break;
2791 case IB_CM_DREP_RECEIVED:
2792 srpt_cm_drep_recv(cm_id);
2793 break;
2794 case IB_CM_TIMEWAIT_EXIT:
2795 srpt_cm_timewait_exit(cm_id);
2796 break;
2797 case IB_CM_REP_ERROR:
2798 srpt_cm_rep_error(cm_id);
2799 break;
2800 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002801 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002802 break;
2803 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002804 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002805 break;
2806 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002807 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002808 break;
2809 }
2810
2811 return ret;
2812}
2813
2814/**
2815 * srpt_perform_rdmas() - Perform IB RDMA.
2816 *
2817 * Returns zero upon success or a negative number upon failure.
2818 */
2819static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2820 struct srpt_send_ioctx *ioctx)
2821{
2822 struct ib_send_wr wr;
2823 struct ib_send_wr *bad_wr;
2824 struct rdma_iu *riu;
2825 int i;
2826 int ret;
2827 int sq_wr_avail;
2828 enum dma_data_direction dir;
2829 const int n_rdma = ioctx->n_rdma;
2830
2831 dir = ioctx->cmd.data_direction;
2832 if (dir == DMA_TO_DEVICE) {
2833 /* write */
2834 ret = -ENOMEM;
2835 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2836 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002837 pr_warn("IB send queue full (needed %d)\n",
2838 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002839 goto out;
2840 }
2841 }
2842
2843 ioctx->rdma_aborted = false;
2844 ret = 0;
2845 riu = ioctx->rdma_ius;
2846 memset(&wr, 0, sizeof wr);
2847
2848 for (i = 0; i < n_rdma; ++i, ++riu) {
2849 if (dir == DMA_FROM_DEVICE) {
2850 wr.opcode = IB_WR_RDMA_WRITE;
2851 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2852 SRPT_RDMA_WRITE_LAST :
2853 SRPT_RDMA_MID,
2854 ioctx->ioctx.index);
2855 } else {
2856 wr.opcode = IB_WR_RDMA_READ;
2857 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2858 SRPT_RDMA_READ_LAST :
2859 SRPT_RDMA_MID,
2860 ioctx->ioctx.index);
2861 }
2862 wr.next = NULL;
2863 wr.wr.rdma.remote_addr = riu->raddr;
2864 wr.wr.rdma.rkey = riu->rkey;
2865 wr.num_sge = riu->sge_cnt;
2866 wr.sg_list = riu->sge;
2867
2868 /* only get completion event for the last rdma write */
2869 if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
2870 wr.send_flags = IB_SEND_SIGNALED;
2871
2872 ret = ib_post_send(ch->qp, &wr, &bad_wr);
2873 if (ret)
2874 break;
2875 }
2876
2877 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002878 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002879 __func__, __LINE__, ret, i, n_rdma);
2880 if (ret && i > 0) {
2881 wr.num_sge = 0;
2882 wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2883 wr.send_flags = IB_SEND_SIGNALED;
2884 while (ch->state == CH_LIVE &&
2885 ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002886 pr_info("Trying to abort failed RDMA transfer [%d]\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002887 ioctx->ioctx.index);
2888 msleep(1000);
2889 }
2890 while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002891 pr_info("Waiting until RDMA abort finished [%d]\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002892 ioctx->ioctx.index);
2893 msleep(1000);
2894 }
2895 }
2896out:
2897 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2898 atomic_add(n_rdma, &ch->sq_wr_avail);
2899 return ret;
2900}
2901
2902/**
2903 * srpt_xfer_data() - Start data transfer from initiator to target.
2904 */
2905static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2906 struct srpt_send_ioctx *ioctx)
2907{
2908 int ret;
2909
2910 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2911 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002912 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002913 goto out;
2914 }
2915
2916 ret = srpt_perform_rdmas(ch, ioctx);
2917 if (ret) {
2918 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002919 pr_info("%s[%d] queue full -- ret=%d\n",
2920 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002921 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002922 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002923 __func__, __LINE__, ret);
2924 goto out_unmap;
2925 }
2926
2927out:
2928 return ret;
2929out_unmap:
2930 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2931 goto out;
2932}
2933
2934static int srpt_write_pending_status(struct se_cmd *se_cmd)
2935{
2936 struct srpt_send_ioctx *ioctx;
2937
2938 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2939 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2940}
2941
2942/*
2943 * srpt_write_pending() - Start data transfer from initiator to target (write).
2944 */
2945static int srpt_write_pending(struct se_cmd *se_cmd)
2946{
2947 struct srpt_rdma_ch *ch;
2948 struct srpt_send_ioctx *ioctx;
2949 enum srpt_command_state new_state;
2950 enum rdma_ch_state ch_state;
2951 int ret;
2952
2953 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2954
2955 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2956 WARN_ON(new_state == SRPT_STATE_DONE);
2957
2958 ch = ioctx->ch;
2959 BUG_ON(!ch);
2960
2961 ch_state = srpt_get_ch_state(ch);
2962 switch (ch_state) {
2963 case CH_CONNECTING:
2964 WARN(true, "unexpected channel state %d\n", ch_state);
2965 ret = -EINVAL;
2966 goto out;
2967 case CH_LIVE:
2968 break;
2969 case CH_DISCONNECTING:
2970 case CH_DRAINING:
2971 case CH_RELEASING:
2972 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002973 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002974 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2975 ret = -EINVAL;
2976 goto out;
2977 }
2978 ret = srpt_xfer_data(ch, ioctx);
2979
2980out:
2981 return ret;
2982}
2983
2984static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2985{
2986 switch (tcm_mgmt_status) {
2987 case TMR_FUNCTION_COMPLETE:
2988 return SRP_TSK_MGMT_SUCCESS;
2989 case TMR_FUNCTION_REJECTED:
2990 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2991 }
2992 return SRP_TSK_MGMT_FAILED;
2993}
2994
2995/**
2996 * srpt_queue_response() - Transmits the response to a SCSI command.
2997 *
2998 * Callback function called by the TCM core. Must not block since it can be
2999 * invoked on the context of the IB completion handler.
3000 */
Joern Engelb79fafa2013-07-03 11:22:17 -04003001static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003002{
3003 struct srpt_rdma_ch *ch;
3004 struct srpt_send_ioctx *ioctx;
3005 enum srpt_command_state state;
3006 unsigned long flags;
3007 int ret;
3008 enum dma_data_direction dir;
3009 int resp_len;
3010 u8 srp_tm_status;
3011
Bart Van Asschea42d9852011-10-14 01:30:46 +00003012 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3013 ch = ioctx->ch;
3014 BUG_ON(!ch);
3015
3016 spin_lock_irqsave(&ioctx->spinlock, flags);
3017 state = ioctx->state;
3018 switch (state) {
3019 case SRPT_STATE_NEW:
3020 case SRPT_STATE_DATA_IN:
3021 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
3022 break;
3023 case SRPT_STATE_MGMT:
3024 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
3025 break;
3026 default:
3027 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
3028 ch, ioctx->ioctx.index, ioctx->state);
3029 break;
3030 }
3031 spin_unlock_irqrestore(&ioctx->spinlock, flags);
3032
3033 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
3034 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
3035 atomic_inc(&ch->req_lim_delta);
3036 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04003037 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003038 }
3039
3040 dir = ioctx->cmd.data_direction;
3041
3042 /* For read commands, transfer the data to the initiator. */
3043 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
3044 !ioctx->queue_status_only) {
3045 ret = srpt_xfer_data(ch, ioctx);
3046 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003047 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02003048 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04003049 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003050 }
3051 }
3052
3053 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02003054 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003055 cmd->scsi_status);
3056 else {
3057 srp_tm_status
3058 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3059 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02003060 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003061 }
3062 ret = srpt_post_send(ch, ioctx, resp_len);
3063 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003064 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02003065 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003066 srpt_unmap_sg_to_ib_sge(ch, ioctx);
3067 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02003068 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003069 }
Joern Engelb79fafa2013-07-03 11:22:17 -04003070}
Bart Van Asschea42d9852011-10-14 01:30:46 +00003071
Joern Engelb79fafa2013-07-03 11:22:17 -04003072static int srpt_queue_data_in(struct se_cmd *cmd)
3073{
3074 srpt_queue_response(cmd);
3075 return 0;
3076}
3077
3078static void srpt_queue_tm_rsp(struct se_cmd *cmd)
3079{
3080 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003081}
3082
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003083static void srpt_aborted_task(struct se_cmd *cmd)
3084{
3085 struct srpt_send_ioctx *ioctx = container_of(cmd,
3086 struct srpt_send_ioctx, cmd);
3087
3088 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
3089}
3090
Bart Van Asschea42d9852011-10-14 01:30:46 +00003091static int srpt_queue_status(struct se_cmd *cmd)
3092{
3093 struct srpt_send_ioctx *ioctx;
3094
3095 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3096 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3097 if (cmd->se_cmd_flags &
3098 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3099 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3100 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04003101 srpt_queue_response(cmd);
3102 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003103}
3104
3105static void srpt_refresh_port_work(struct work_struct *work)
3106{
3107 struct srpt_port *sport = container_of(work, struct srpt_port, work);
3108
3109 srpt_refresh_port(sport);
3110}
3111
3112static int srpt_ch_list_empty(struct srpt_device *sdev)
3113{
3114 int res;
3115
3116 spin_lock_irq(&sdev->spinlock);
3117 res = list_empty(&sdev->rch_list);
3118 spin_unlock_irq(&sdev->spinlock);
3119
3120 return res;
3121}
3122
3123/**
3124 * srpt_release_sdev() - Free the channel resources associated with a target.
3125 */
3126static int srpt_release_sdev(struct srpt_device *sdev)
3127{
3128 struct srpt_rdma_ch *ch, *tmp_ch;
3129 int res;
3130
3131 WARN_ON_ONCE(irqs_disabled());
3132
3133 BUG_ON(!sdev);
3134
3135 spin_lock_irq(&sdev->spinlock);
3136 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3137 __srpt_close_ch(ch);
3138 spin_unlock_irq(&sdev->spinlock);
3139
3140 res = wait_event_interruptible(sdev->ch_releaseQ,
3141 srpt_ch_list_empty(sdev));
3142 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003143 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003144
3145 return 0;
3146}
3147
3148static struct srpt_port *__srpt_lookup_port(const char *name)
3149{
3150 struct ib_device *dev;
3151 struct srpt_device *sdev;
3152 struct srpt_port *sport;
3153 int i;
3154
3155 list_for_each_entry(sdev, &srpt_dev_list, list) {
3156 dev = sdev->device;
3157 if (!dev)
3158 continue;
3159
3160 for (i = 0; i < dev->phys_port_cnt; i++) {
3161 sport = &sdev->port[i];
3162
3163 if (!strcmp(sport->port_guid, name))
3164 return sport;
3165 }
3166 }
3167
3168 return NULL;
3169}
3170
3171static struct srpt_port *srpt_lookup_port(const char *name)
3172{
3173 struct srpt_port *sport;
3174
3175 spin_lock(&srpt_dev_lock);
3176 sport = __srpt_lookup_port(name);
3177 spin_unlock(&srpt_dev_lock);
3178
3179 return sport;
3180}
3181
3182/**
3183 * srpt_add_one() - Infiniband device addition callback function.
3184 */
3185static void srpt_add_one(struct ib_device *device)
3186{
3187 struct srpt_device *sdev;
3188 struct srpt_port *sport;
3189 struct ib_srq_init_attr srq_attr;
3190 int i;
3191
3192 pr_debug("device = %p, device->dma_ops = %p\n", device,
3193 device->dma_ops);
3194
3195 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3196 if (!sdev)
3197 goto err;
3198
3199 sdev->device = device;
3200 INIT_LIST_HEAD(&sdev->rch_list);
3201 init_waitqueue_head(&sdev->ch_releaseQ);
3202 spin_lock_init(&sdev->spinlock);
3203
3204 if (ib_query_device(device, &sdev->dev_attr))
3205 goto free_dev;
3206
3207 sdev->pd = ib_alloc_pd(device);
3208 if (IS_ERR(sdev->pd))
3209 goto free_dev;
3210
3211 sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
3212 if (IS_ERR(sdev->mr))
3213 goto err_pd;
3214
3215 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3216
3217 srq_attr.event_handler = srpt_srq_event;
3218 srq_attr.srq_context = (void *)sdev;
3219 srq_attr.attr.max_wr = sdev->srq_size;
3220 srq_attr.attr.max_sge = 1;
3221 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07003222 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003223
3224 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3225 if (IS_ERR(sdev->srq))
3226 goto err_mr;
3227
3228 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3229 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3230 device->name);
3231
3232 if (!srpt_service_guid)
3233 srpt_service_guid = be64_to_cpu(device->node_guid);
3234
3235 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3236 if (IS_ERR(sdev->cm_id))
3237 goto err_srq;
3238
3239 /* print out target login information */
3240 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3241 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3242 srpt_service_guid, srpt_service_guid);
3243
3244 /*
3245 * We do not have a consistent service_id (ie. also id_ext of target_id)
3246 * to identify this target. We currently use the guid of the first HCA
3247 * in the system as service_id; therefore, the target_id will change
3248 * if this HCA is gone bad and replaced by different HCA
3249 */
3250 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
3251 goto err_cm;
3252
3253 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3254 srpt_event_handler);
3255 if (ib_register_event_handler(&sdev->event_handler))
3256 goto err_cm;
3257
3258 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3259 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3260 sizeof(*sdev->ioctx_ring[0]),
3261 srp_max_req_size, DMA_FROM_DEVICE);
3262 if (!sdev->ioctx_ring)
3263 goto err_event;
3264
3265 for (i = 0; i < sdev->srq_size; ++i)
3266 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3267
Roland Dreierf2250662012-02-02 12:55:58 -08003268 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00003269
3270 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3271 sport = &sdev->port[i - 1];
3272 sport->sdev = sdev;
3273 sport->port = i;
3274 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3275 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3276 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3277 INIT_WORK(&sport->work, srpt_refresh_port_work);
3278 INIT_LIST_HEAD(&sport->port_acl_list);
3279 spin_lock_init(&sport->port_acl_lock);
3280
3281 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003282 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003283 srpt_sdev_name(sdev), i);
3284 goto err_ring;
3285 }
3286 snprintf(sport->port_guid, sizeof(sport->port_guid),
3287 "0x%016llx%016llx",
3288 be64_to_cpu(sport->gid.global.subnet_prefix),
3289 be64_to_cpu(sport->gid.global.interface_id));
3290 }
3291
3292 spin_lock(&srpt_dev_lock);
3293 list_add_tail(&sdev->list, &srpt_dev_list);
3294 spin_unlock(&srpt_dev_lock);
3295
3296out:
3297 ib_set_client_data(device, &srpt_client, sdev);
3298 pr_debug("added %s.\n", device->name);
3299 return;
3300
3301err_ring:
3302 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3303 sdev->srq_size, srp_max_req_size,
3304 DMA_FROM_DEVICE);
3305err_event:
3306 ib_unregister_event_handler(&sdev->event_handler);
3307err_cm:
3308 ib_destroy_cm_id(sdev->cm_id);
3309err_srq:
3310 ib_destroy_srq(sdev->srq);
3311err_mr:
3312 ib_dereg_mr(sdev->mr);
3313err_pd:
3314 ib_dealloc_pd(sdev->pd);
3315free_dev:
3316 kfree(sdev);
3317err:
3318 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003319 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003320 goto out;
3321}
3322
3323/**
3324 * srpt_remove_one() - InfiniBand device removal callback function.
3325 */
3326static void srpt_remove_one(struct ib_device *device)
3327{
3328 struct srpt_device *sdev;
3329 int i;
3330
3331 sdev = ib_get_client_data(device, &srpt_client);
3332 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003333 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003334 return;
3335 }
3336
3337 srpt_unregister_mad_agent(sdev);
3338
3339 ib_unregister_event_handler(&sdev->event_handler);
3340
3341 /* Cancel any work queued by the just unregistered IB event handler. */
3342 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3343 cancel_work_sync(&sdev->port[i].work);
3344
3345 ib_destroy_cm_id(sdev->cm_id);
3346
3347 /*
3348 * Unregistering a target must happen after destroying sdev->cm_id
3349 * such that no new SRP_LOGIN_REQ information units can arrive while
3350 * destroying the target.
3351 */
3352 spin_lock(&srpt_dev_lock);
3353 list_del(&sdev->list);
3354 spin_unlock(&srpt_dev_lock);
3355 srpt_release_sdev(sdev);
3356
3357 ib_destroy_srq(sdev->srq);
3358 ib_dereg_mr(sdev->mr);
3359 ib_dealloc_pd(sdev->pd);
3360
3361 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3362 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3363 sdev->ioctx_ring = NULL;
3364 kfree(sdev);
3365}
3366
3367static struct ib_client srpt_client = {
3368 .name = DRV_NAME,
3369 .add = srpt_add_one,
3370 .remove = srpt_remove_one
3371};
3372
3373static int srpt_check_true(struct se_portal_group *se_tpg)
3374{
3375 return 1;
3376}
3377
3378static int srpt_check_false(struct se_portal_group *se_tpg)
3379{
3380 return 0;
3381}
3382
3383static char *srpt_get_fabric_name(void)
3384{
3385 return "srpt";
3386}
3387
Bart Van Asschea42d9852011-10-14 01:30:46 +00003388static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3389{
3390 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3391
3392 return sport->port_guid;
3393}
3394
3395static u16 srpt_get_tag(struct se_portal_group *tpg)
3396{
3397 return 1;
3398}
3399
Bart Van Asschea42d9852011-10-14 01:30:46 +00003400static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3401{
3402 return 1;
3403}
3404
3405static void srpt_release_cmd(struct se_cmd *se_cmd)
3406{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003407 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3408 struct srpt_send_ioctx, cmd);
3409 struct srpt_rdma_ch *ch = ioctx->ch;
3410 unsigned long flags;
3411
3412 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3413 WARN_ON(ioctx->mapped_sg_count != 0);
3414
3415 if (ioctx->n_rbuf > 1) {
3416 kfree(ioctx->rbufs);
3417 ioctx->rbufs = NULL;
3418 ioctx->n_rbuf = 0;
3419 }
3420
3421 spin_lock_irqsave(&ch->spinlock, flags);
3422 list_add(&ioctx->free_list, &ch->free_list);
3423 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003424}
3425
3426/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003427 * srpt_close_session() - Forcibly close a session.
3428 *
3429 * Callback function invoked by the TCM core to clean up sessions associated
3430 * with a node ACL when the user invokes
3431 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3432 */
3433static void srpt_close_session(struct se_session *se_sess)
3434{
3435 DECLARE_COMPLETION_ONSTACK(release_done);
3436 struct srpt_rdma_ch *ch;
3437 struct srpt_device *sdev;
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003438 unsigned long res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003439
3440 ch = se_sess->fabric_sess_ptr;
3441 WARN_ON(ch->sess != se_sess);
3442
3443 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3444
3445 sdev = ch->sport->sdev;
3446 spin_lock_irq(&sdev->spinlock);
3447 BUG_ON(ch->release_done);
3448 ch->release_done = &release_done;
3449 __srpt_close_ch(ch);
3450 spin_unlock_irq(&sdev->spinlock);
3451
3452 res = wait_for_completion_timeout(&release_done, 60 * HZ);
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003453 WARN_ON(res == 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003454}
3455
3456/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003457 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3458 *
3459 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3460 * This object represents an arbitrary integer used to uniquely identify a
3461 * particular attached remote initiator port to a particular SCSI target port
3462 * within a particular SCSI target device within a particular SCSI instance.
3463 */
3464static u32 srpt_sess_get_index(struct se_session *se_sess)
3465{
3466 return 0;
3467}
3468
3469static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3470{
3471}
3472
Bart Van Asschea42d9852011-10-14 01:30:46 +00003473/* Note: only used from inside debug printk's by the TCM core. */
3474static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3475{
3476 struct srpt_send_ioctx *ioctx;
3477
3478 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3479 return srpt_get_cmd_state(ioctx);
3480}
3481
Bart Van Asschea42d9852011-10-14 01:30:46 +00003482/**
3483 * srpt_parse_i_port_id() - Parse an initiator port ID.
3484 * @name: ASCII representation of a 128-bit initiator port ID.
3485 * @i_port_id: Binary 128-bit port ID.
3486 */
3487static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3488{
3489 const char *p;
3490 unsigned len, count, leading_zero_bytes;
3491 int ret, rc;
3492
3493 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003494 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003495 p += 2;
3496 ret = -EINVAL;
3497 len = strlen(p);
3498 if (len % 2)
3499 goto out;
3500 count = min(len / 2, 16U);
3501 leading_zero_bytes = 16 - count;
3502 memset(i_port_id, 0, leading_zero_bytes);
3503 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3504 if (rc < 0)
3505 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3506 ret = 0;
3507out:
3508 return ret;
3509}
3510
3511/*
3512 * configfs callback function invoked for
3513 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3514 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003515static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003516{
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003517 struct srpt_port *sport =
3518 container_of(se_nacl->se_tpg, struct srpt_port, port_tpg_1);
3519 struct srpt_node_acl *nacl =
3520 container_of(se_nacl, struct srpt_node_acl, nacl);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003521 u8 i_port_id[16];
3522
3523 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003524 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003525 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003526 }
3527
Bart Van Asschea42d9852011-10-14 01:30:46 +00003528 memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3529 nacl->sport = sport;
3530
3531 spin_lock_irq(&sport->port_acl_lock);
3532 list_add_tail(&nacl->list, &sport->port_acl_list);
3533 spin_unlock_irq(&sport->port_acl_lock);
3534
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003535 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003536}
3537
3538/*
3539 * configfs callback function invoked for
3540 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3541 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003542static void srpt_cleanup_nodeacl(struct se_node_acl *se_nacl)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003543{
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003544 struct srpt_node_acl *nacl =
3545 container_of(se_nacl, struct srpt_node_acl, nacl);
3546 struct srpt_port *sport = nacl->sport;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003547
Bart Van Asschea42d9852011-10-14 01:30:46 +00003548 spin_lock_irq(&sport->port_acl_lock);
3549 list_del(&nacl->list);
3550 spin_unlock_irq(&sport->port_acl_lock);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003551}
3552
3553static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
3554 struct se_portal_group *se_tpg,
3555 char *page)
3556{
3557 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3558
3559 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3560}
3561
3562static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
3563 struct se_portal_group *se_tpg,
3564 const char *page,
3565 size_t count)
3566{
3567 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3568 unsigned long val;
3569 int ret;
3570
Jingoo Han9d8abf42014-02-05 11:22:05 +09003571 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003572 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003573 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003574 return -EINVAL;
3575 }
3576 if (val > MAX_SRPT_RDMA_SIZE) {
3577 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3578 MAX_SRPT_RDMA_SIZE);
3579 return -EINVAL;
3580 }
3581 if (val < DEFAULT_MAX_RDMA_SIZE) {
3582 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3583 val, DEFAULT_MAX_RDMA_SIZE);
3584 return -EINVAL;
3585 }
3586 sport->port_attrib.srp_max_rdma_size = val;
3587
3588 return count;
3589}
3590
3591TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
3592
3593static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
3594 struct se_portal_group *se_tpg,
3595 char *page)
3596{
3597 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3598
3599 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3600}
3601
3602static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
3603 struct se_portal_group *se_tpg,
3604 const char *page,
3605 size_t count)
3606{
3607 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3608 unsigned long val;
3609 int ret;
3610
Jingoo Han9d8abf42014-02-05 11:22:05 +09003611 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003612 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003613 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003614 return -EINVAL;
3615 }
3616 if (val > MAX_SRPT_RSP_SIZE) {
3617 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3618 MAX_SRPT_RSP_SIZE);
3619 return -EINVAL;
3620 }
3621 if (val < MIN_MAX_RSP_SIZE) {
3622 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3623 MIN_MAX_RSP_SIZE);
3624 return -EINVAL;
3625 }
3626 sport->port_attrib.srp_max_rsp_size = val;
3627
3628 return count;
3629}
3630
3631TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
3632
3633static ssize_t srpt_tpg_attrib_show_srp_sq_size(
3634 struct se_portal_group *se_tpg,
3635 char *page)
3636{
3637 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3638
3639 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3640}
3641
3642static ssize_t srpt_tpg_attrib_store_srp_sq_size(
3643 struct se_portal_group *se_tpg,
3644 const char *page,
3645 size_t count)
3646{
3647 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3648 unsigned long val;
3649 int ret;
3650
Jingoo Han9d8abf42014-02-05 11:22:05 +09003651 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003652 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003653 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003654 return -EINVAL;
3655 }
3656 if (val > MAX_SRPT_SRQ_SIZE) {
3657 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3658 MAX_SRPT_SRQ_SIZE);
3659 return -EINVAL;
3660 }
3661 if (val < MIN_SRPT_SRQ_SIZE) {
3662 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3663 MIN_SRPT_SRQ_SIZE);
3664 return -EINVAL;
3665 }
3666 sport->port_attrib.srp_sq_size = val;
3667
3668 return count;
3669}
3670
3671TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
3672
3673static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3674 &srpt_tpg_attrib_srp_max_rdma_size.attr,
3675 &srpt_tpg_attrib_srp_max_rsp_size.attr,
3676 &srpt_tpg_attrib_srp_sq_size.attr,
3677 NULL,
3678};
3679
3680static ssize_t srpt_tpg_show_enable(
3681 struct se_portal_group *se_tpg,
3682 char *page)
3683{
3684 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3685
3686 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3687}
3688
3689static ssize_t srpt_tpg_store_enable(
3690 struct se_portal_group *se_tpg,
3691 const char *page,
3692 size_t count)
3693{
3694 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3695 unsigned long tmp;
3696 int ret;
3697
Jingoo Han9d8abf42014-02-05 11:22:05 +09003698 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003699 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003700 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003701 return -EINVAL;
3702 }
3703
3704 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003705 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003706 return -EINVAL;
3707 }
3708 if (tmp == 1)
3709 sport->enabled = true;
3710 else
3711 sport->enabled = false;
3712
3713 return count;
3714}
3715
3716TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
3717
3718static struct configfs_attribute *srpt_tpg_attrs[] = {
3719 &srpt_tpg_enable.attr,
3720 NULL,
3721};
3722
3723/**
3724 * configfs callback invoked for
3725 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3726 */
3727static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3728 struct config_group *group,
3729 const char *name)
3730{
3731 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3732 int res;
3733
3734 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003735 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003736 if (res)
3737 return ERR_PTR(res);
3738
3739 return &sport->port_tpg_1;
3740}
3741
3742/**
3743 * configfs callback invoked for
3744 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3745 */
3746static void srpt_drop_tpg(struct se_portal_group *tpg)
3747{
3748 struct srpt_port *sport = container_of(tpg,
3749 struct srpt_port, port_tpg_1);
3750
3751 sport->enabled = false;
3752 core_tpg_deregister(&sport->port_tpg_1);
3753}
3754
3755/**
3756 * configfs callback invoked for
3757 * mkdir /sys/kernel/config/target/$driver/$port
3758 */
3759static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3760 struct config_group *group,
3761 const char *name)
3762{
3763 struct srpt_port *sport;
3764 int ret;
3765
3766 sport = srpt_lookup_port(name);
3767 pr_debug("make_tport(%s)\n", name);
3768 ret = -EINVAL;
3769 if (!sport)
3770 goto err;
3771
3772 return &sport->port_wwn;
3773
3774err:
3775 return ERR_PTR(ret);
3776}
3777
3778/**
3779 * configfs callback invoked for
3780 * rmdir /sys/kernel/config/target/$driver/$port
3781 */
3782static void srpt_drop_tport(struct se_wwn *wwn)
3783{
3784 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3785
3786 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3787}
3788
3789static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
3790 char *buf)
3791{
3792 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3793}
3794
3795TF_WWN_ATTR_RO(srpt, version);
3796
3797static struct configfs_attribute *srpt_wwn_attrs[] = {
3798 &srpt_wwn_version.attr,
3799 NULL,
3800};
3801
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003802static const struct target_core_fabric_ops srpt_template = {
3803 .module = THIS_MODULE,
3804 .name = "srpt",
Christoph Hellwig144bc4c2015-04-13 19:51:16 +02003805 .node_acl_size = sizeof(struct srpt_node_acl),
Bart Van Asschea42d9852011-10-14 01:30:46 +00003806 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003807 .tpg_get_wwn = srpt_get_fabric_wwn,
3808 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003809 .tpg_check_demo_mode = srpt_check_false,
3810 .tpg_check_demo_mode_cache = srpt_check_true,
3811 .tpg_check_demo_mode_write_protect = srpt_check_true,
3812 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003813 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3814 .release_cmd = srpt_release_cmd,
3815 .check_stop_free = srpt_check_stop_free,
3816 .shutdown_session = srpt_shutdown_session,
3817 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003818 .sess_get_index = srpt_sess_get_index,
3819 .sess_get_initiator_sid = NULL,
3820 .write_pending = srpt_write_pending,
3821 .write_pending_status = srpt_write_pending_status,
3822 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003823 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003824 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003825 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003826 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003827 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003828 /*
3829 * Setup function pointers for generic logic in
3830 * target_core_fabric_configfs.c
3831 */
3832 .fabric_make_wwn = srpt_make_tport,
3833 .fabric_drop_wwn = srpt_drop_tport,
3834 .fabric_make_tpg = srpt_make_tpg,
3835 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003836 .fabric_init_nodeacl = srpt_init_nodeacl,
3837 .fabric_cleanup_nodeacl = srpt_cleanup_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003838
3839 .tfc_wwn_attrs = srpt_wwn_attrs,
3840 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3841 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003842};
3843
3844/**
3845 * srpt_init_module() - Kernel module initialization.
3846 *
3847 * Note: Since ib_register_client() registers callback functions, and since at
3848 * least one of these callback functions (srpt_add_one()) calls target core
3849 * functions, this driver must be registered with the target core before
3850 * ib_register_client() is called.
3851 */
3852static int __init srpt_init_module(void)
3853{
3854 int ret;
3855
3856 ret = -EINVAL;
3857 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003858 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003859 " srp_max_req_size -- must be at least %d.\n",
3860 srp_max_req_size, MIN_MAX_REQ_SIZE);
3861 goto out;
3862 }
3863
3864 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3865 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003866 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003867 " srpt_srq_size -- must be in the range [%d..%d].\n",
3868 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3869 goto out;
3870 }
3871
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003872 ret = target_register_template(&srpt_template);
3873 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003874 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003875
3876 ret = ib_register_client(&srpt_client);
3877 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003878 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003879 goto out_unregister_target;
3880 }
3881
3882 return 0;
3883
3884out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003885 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003886out:
3887 return ret;
3888}
3889
3890static void __exit srpt_cleanup_module(void)
3891{
3892 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003893 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003894}
3895
3896module_init(srpt_init_module);
3897module_exit(srpt_cleanup_module);