blob: 0b2857b1b112aa2421d9f10d08e5d1bcb679d489 [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>
49#include <target/target_core_configfs.h>
50#include "ib_srpt.h"
51
52/* Name of this kernel module. */
53#define DRV_NAME "ib_srpt"
54#define DRV_VERSION "2.0.0"
55#define DRV_RELDATE "2011-02-14"
56
57#define SRPT_ID_STRING "Linux SRP target"
58
59#undef pr_fmt
60#define pr_fmt(fmt) DRV_NAME " " fmt
61
62MODULE_AUTHOR("Vu Pham and Bart Van Assche");
63MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
64 "v" DRV_VERSION " (" DRV_RELDATE ")");
65MODULE_LICENSE("Dual BSD/GPL");
66
67/*
68 * Global Variables
69 */
70
71static u64 srpt_service_guid;
Roland Dreier486d8b92012-02-02 12:55:58 -080072static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
73static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
Bart Van Asschea42d9852011-10-14 01:30:46 +000074
75static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
76module_param(srp_max_req_size, int, 0444);
77MODULE_PARM_DESC(srp_max_req_size,
78 "Maximum size of SRP request messages in bytes.");
79
80static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
81module_param(srpt_srq_size, int, 0444);
82MODULE_PARM_DESC(srpt_srq_size,
83 "Shared receive queue (SRQ) size.");
84
85static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
86{
87 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
88}
89module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
90 0444);
91MODULE_PARM_DESC(srpt_service_guid,
92 "Using this value for ioc_guid, id_ext, and cm_listen_id"
93 " instead of using the node_guid of the first HCA.");
94
95static struct ib_client srpt_client;
Christoph Hellwig9ac89282015-04-08 20:01:35 +020096static const struct target_core_fabric_ops srpt_template;
Bart Van Asschea42d9852011-10-14 01:30:46 +000097static void srpt_release_channel(struct srpt_rdma_ch *ch);
98static int srpt_queue_status(struct se_cmd *cmd);
99
100/**
101 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
102 */
103static inline
104enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
105{
106 switch (dir) {
107 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
108 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
109 default: return dir;
110 }
111}
112
113/**
114 * srpt_sdev_name() - Return the name associated with the HCA.
115 *
116 * Examples are ib0, ib1, ...
117 */
118static inline const char *srpt_sdev_name(struct srpt_device *sdev)
119{
120 return sdev->device->name;
121}
122
123static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
124{
125 unsigned long flags;
126 enum rdma_ch_state state;
127
128 spin_lock_irqsave(&ch->spinlock, flags);
129 state = ch->state;
130 spin_unlock_irqrestore(&ch->spinlock, flags);
131 return state;
132}
133
134static enum rdma_ch_state
135srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
136{
137 unsigned long flags;
138 enum rdma_ch_state prev;
139
140 spin_lock_irqsave(&ch->spinlock, flags);
141 prev = ch->state;
142 ch->state = new_state;
143 spin_unlock_irqrestore(&ch->spinlock, flags);
144 return prev;
145}
146
147/**
148 * srpt_test_and_set_ch_state() - Test and set the channel state.
149 *
150 * Returns true if and only if the channel state has been set to the new state.
151 */
152static bool
153srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
154 enum rdma_ch_state new)
155{
156 unsigned long flags;
157 enum rdma_ch_state prev;
158
159 spin_lock_irqsave(&ch->spinlock, flags);
160 prev = ch->state;
161 if (prev == old)
162 ch->state = new;
163 spin_unlock_irqrestore(&ch->spinlock, flags);
164 return prev == old;
165}
166
167/**
168 * srpt_event_handler() - Asynchronous IB event callback function.
169 *
170 * Callback function called by the InfiniBand core when an asynchronous IB
171 * event occurs. This callback may occur in interrupt context. See also
172 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
173 * Architecture Specification.
174 */
175static void srpt_event_handler(struct ib_event_handler *handler,
176 struct ib_event *event)
177{
178 struct srpt_device *sdev;
179 struct srpt_port *sport;
180
181 sdev = ib_get_client_data(event->device, &srpt_client);
182 if (!sdev || sdev->device != event->device)
183 return;
184
185 pr_debug("ASYNC event= %d on device= %s\n", event->event,
186 srpt_sdev_name(sdev));
187
188 switch (event->event) {
189 case IB_EVENT_PORT_ERR:
190 if (event->element.port_num <= sdev->device->phys_port_cnt) {
191 sport = &sdev->port[event->element.port_num - 1];
192 sport->lid = 0;
193 sport->sm_lid = 0;
194 }
195 break;
196 case IB_EVENT_PORT_ACTIVE:
197 case IB_EVENT_LID_CHANGE:
198 case IB_EVENT_PKEY_CHANGE:
199 case IB_EVENT_SM_CHANGE:
200 case IB_EVENT_CLIENT_REREGISTER:
Doug Ledford2aa1cf62014-08-12 19:20:10 -0400201 case IB_EVENT_GID_CHANGE:
Bart Van Asschea42d9852011-10-14 01:30:46 +0000202 /* Refresh port data asynchronously. */
203 if (event->element.port_num <= sdev->device->phys_port_cnt) {
204 sport = &sdev->port[event->element.port_num - 1];
205 if (!sport->lid && !sport->sm_lid)
206 schedule_work(&sport->work);
207 }
208 break;
209 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400210 pr_err("received unrecognized IB event %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +0000211 event->event);
212 break;
213 }
214}
215
216/**
217 * srpt_srq_event() - SRQ event callback function.
218 */
219static void srpt_srq_event(struct ib_event *event, void *ctx)
220{
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400221 pr_info("SRQ event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000222}
223
224/**
225 * srpt_qp_event() - QP event callback function.
226 */
227static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
228{
229 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
230 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
231
232 switch (event->event) {
233 case IB_EVENT_COMM_EST:
234 ib_cm_notify(ch->cm_id, event->event);
235 break;
236 case IB_EVENT_QP_LAST_WQE_REACHED:
237 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
238 CH_RELEASING))
239 srpt_release_channel(ch);
240 else
241 pr_debug("%s: state %d - ignored LAST_WQE.\n",
242 ch->sess_name, srpt_get_ch_state(ch));
243 break;
244 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400245 pr_err("received unrecognized IB QP event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000246 break;
247 }
248}
249
250/**
251 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
252 *
253 * @slot: one-based slot number.
254 * @value: four-bit value.
255 *
256 * Copies the lowest four bits of value in element slot of the array of four
257 * bit elements called c_list (controller list). The index slot is one-based.
258 */
259static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
260{
261 u16 id;
262 u8 tmp;
263
264 id = (slot - 1) / 2;
265 if (slot & 0x1) {
266 tmp = c_list[id] & 0xf;
267 c_list[id] = (value << 4) | tmp;
268 } else {
269 tmp = c_list[id] & 0xf0;
270 c_list[id] = (value & 0xf) | tmp;
271 }
272}
273
274/**
275 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
276 *
277 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
278 * Specification.
279 */
280static void srpt_get_class_port_info(struct ib_dm_mad *mad)
281{
282 struct ib_class_port_info *cif;
283
284 cif = (struct ib_class_port_info *)mad->data;
285 memset(cif, 0, sizeof *cif);
286 cif->base_version = 1;
287 cif->class_version = 1;
288 cif->resp_time_value = 20;
289
290 mad->mad_hdr.status = 0;
291}
292
293/**
294 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
295 *
296 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
297 * Specification. See also section B.7, table B.6 in the SRP r16a document.
298 */
299static void srpt_get_iou(struct ib_dm_mad *mad)
300{
301 struct ib_dm_iou_info *ioui;
302 u8 slot;
303 int i;
304
305 ioui = (struct ib_dm_iou_info *)mad->data;
306 ioui->change_id = __constant_cpu_to_be16(1);
307 ioui->max_controllers = 16;
308
309 /* set present for slot 1 and empty for the rest */
310 srpt_set_ioc(ioui->controller_list, 1, 1);
311 for (i = 1, slot = 2; i < 16; i++, slot++)
312 srpt_set_ioc(ioui->controller_list, slot, 0);
313
314 mad->mad_hdr.status = 0;
315}
316
317/**
318 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
319 *
320 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
321 * Architecture Specification. See also section B.7, table B.7 in the SRP
322 * r16a document.
323 */
324static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
325 struct ib_dm_mad *mad)
326{
327 struct srpt_device *sdev = sport->sdev;
328 struct ib_dm_ioc_profile *iocp;
329
330 iocp = (struct ib_dm_ioc_profile *)mad->data;
331
332 if (!slot || slot > 16) {
333 mad->mad_hdr.status
334 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
335 return;
336 }
337
338 if (slot > 2) {
339 mad->mad_hdr.status
340 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
341 return;
342 }
343
344 memset(iocp, 0, sizeof *iocp);
345 strcpy(iocp->id_string, SRPT_ID_STRING);
346 iocp->guid = cpu_to_be64(srpt_service_guid);
347 iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
348 iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
349 iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
350 iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
351 iocp->subsys_device_id = 0x0;
352 iocp->io_class = __constant_cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
353 iocp->io_subclass = __constant_cpu_to_be16(SRP_IO_SUBCLASS);
354 iocp->protocol = __constant_cpu_to_be16(SRP_PROTOCOL);
355 iocp->protocol_version = __constant_cpu_to_be16(SRP_PROTOCOL_VERSION);
356 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
357 iocp->rdma_read_depth = 4;
358 iocp->send_size = cpu_to_be32(srp_max_req_size);
359 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
360 1U << 24));
361 iocp->num_svc_entries = 1;
362 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
363 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
364
365 mad->mad_hdr.status = 0;
366}
367
368/**
369 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
370 *
371 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
372 * Specification. See also section B.7, table B.8 in the SRP r16a document.
373 */
374static void srpt_get_svc_entries(u64 ioc_guid,
375 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
376{
377 struct ib_dm_svc_entries *svc_entries;
378
379 WARN_ON(!ioc_guid);
380
381 if (!slot || slot > 16) {
382 mad->mad_hdr.status
383 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
384 return;
385 }
386
387 if (slot > 2 || lo > hi || hi > 1) {
388 mad->mad_hdr.status
389 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
390 return;
391 }
392
393 svc_entries = (struct ib_dm_svc_entries *)mad->data;
394 memset(svc_entries, 0, sizeof *svc_entries);
395 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
396 snprintf(svc_entries->service_entries[0].name,
397 sizeof(svc_entries->service_entries[0].name),
398 "%s%016llx",
399 SRP_SERVICE_NAME_PREFIX,
400 ioc_guid);
401
402 mad->mad_hdr.status = 0;
403}
404
405/**
406 * srpt_mgmt_method_get() - Process a received management datagram.
407 * @sp: source port through which the MAD has been received.
408 * @rq_mad: received MAD.
409 * @rsp_mad: response MAD.
410 */
411static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
412 struct ib_dm_mad *rsp_mad)
413{
414 u16 attr_id;
415 u32 slot;
416 u8 hi, lo;
417
418 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
419 switch (attr_id) {
420 case DM_ATTR_CLASS_PORT_INFO:
421 srpt_get_class_port_info(rsp_mad);
422 break;
423 case DM_ATTR_IOU_INFO:
424 srpt_get_iou(rsp_mad);
425 break;
426 case DM_ATTR_IOC_PROFILE:
427 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
428 srpt_get_ioc(sp, slot, rsp_mad);
429 break;
430 case DM_ATTR_SVC_ENTRIES:
431 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
432 hi = (u8) ((slot >> 8) & 0xff);
433 lo = (u8) (slot & 0xff);
434 slot = (u16) ((slot >> 16) & 0xffff);
435 srpt_get_svc_entries(srpt_service_guid,
436 slot, hi, lo, rsp_mad);
437 break;
438 default:
439 rsp_mad->mad_hdr.status =
440 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
441 break;
442 }
443}
444
445/**
446 * srpt_mad_send_handler() - Post MAD-send callback function.
447 */
448static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
449 struct ib_mad_send_wc *mad_wc)
450{
451 ib_destroy_ah(mad_wc->send_buf->ah);
452 ib_free_send_mad(mad_wc->send_buf);
453}
454
455/**
456 * srpt_mad_recv_handler() - MAD reception callback function.
457 */
458static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
459 struct ib_mad_recv_wc *mad_wc)
460{
461 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
462 struct ib_ah *ah;
463 struct ib_mad_send_buf *rsp;
464 struct ib_dm_mad *dm_mad;
465
466 if (!mad_wc || !mad_wc->recv_buf.mad)
467 return;
468
469 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
470 mad_wc->recv_buf.grh, mad_agent->port_num);
471 if (IS_ERR(ah))
472 goto err;
473
474 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
475
476 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
477 mad_wc->wc->pkey_index, 0,
478 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400479 GFP_KERNEL,
480 IB_MGMT_BASE_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000481 if (IS_ERR(rsp))
482 goto err_rsp;
483
484 rsp->ah = ah;
485
486 dm_mad = rsp->mad;
487 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
488 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
489 dm_mad->mad_hdr.status = 0;
490
491 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
492 case IB_MGMT_METHOD_GET:
493 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
494 break;
495 case IB_MGMT_METHOD_SET:
496 dm_mad->mad_hdr.status =
497 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
498 break;
499 default:
500 dm_mad->mad_hdr.status =
501 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
502 break;
503 }
504
505 if (!ib_post_send_mad(rsp, NULL)) {
506 ib_free_recv_mad(mad_wc);
507 /* will destroy_ah & free_send_mad in send completion */
508 return;
509 }
510
511 ib_free_send_mad(rsp);
512
513err_rsp:
514 ib_destroy_ah(ah);
515err:
516 ib_free_recv_mad(mad_wc);
517}
518
519/**
520 * srpt_refresh_port() - Configure a HCA port.
521 *
522 * Enable InfiniBand management datagram processing, update the cached sm_lid,
523 * lid and gid values, and register a callback function for processing MADs
524 * on the specified port.
525 *
526 * Note: It is safe to call this function more than once for the same port.
527 */
528static int srpt_refresh_port(struct srpt_port *sport)
529{
530 struct ib_mad_reg_req reg_req;
531 struct ib_port_modify port_modify;
532 struct ib_port_attr port_attr;
533 int ret;
534
535 memset(&port_modify, 0, sizeof port_modify);
536 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
537 port_modify.clr_port_cap_mask = 0;
538
539 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
540 if (ret)
541 goto err_mod_port;
542
543 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
544 if (ret)
545 goto err_query_port;
546
547 sport->sm_lid = port_attr.sm_lid;
548 sport->lid = port_attr.lid;
549
550 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
551 if (ret)
552 goto err_query_port;
553
554 if (!sport->mad_agent) {
555 memset(&reg_req, 0, sizeof reg_req);
556 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
557 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
558 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
559 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
560
561 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
562 sport->port,
563 IB_QPT_GSI,
564 &reg_req, 0,
565 srpt_mad_send_handler,
566 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400567 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000568 if (IS_ERR(sport->mad_agent)) {
569 ret = PTR_ERR(sport->mad_agent);
570 sport->mad_agent = NULL;
571 goto err_query_port;
572 }
573 }
574
575 return 0;
576
577err_query_port:
578
579 port_modify.set_port_cap_mask = 0;
580 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
581 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
582
583err_mod_port:
584
585 return ret;
586}
587
588/**
589 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
590 *
591 * Note: It is safe to call this function more than once for the same device.
592 */
593static void srpt_unregister_mad_agent(struct srpt_device *sdev)
594{
595 struct ib_port_modify port_modify = {
596 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
597 };
598 struct srpt_port *sport;
599 int i;
600
601 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
602 sport = &sdev->port[i - 1];
603 WARN_ON(sport->port != i);
604 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400605 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000606 if (sport->mad_agent) {
607 ib_unregister_mad_agent(sport->mad_agent);
608 sport->mad_agent = NULL;
609 }
610 }
611}
612
613/**
614 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
615 */
616static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
617 int ioctx_size, int dma_size,
618 enum dma_data_direction dir)
619{
620 struct srpt_ioctx *ioctx;
621
622 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
623 if (!ioctx)
624 goto err;
625
626 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
627 if (!ioctx->buf)
628 goto err_free_ioctx;
629
630 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
631 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
632 goto err_free_buf;
633
634 return ioctx;
635
636err_free_buf:
637 kfree(ioctx->buf);
638err_free_ioctx:
639 kfree(ioctx);
640err:
641 return NULL;
642}
643
644/**
645 * srpt_free_ioctx() - Free an SRPT I/O context structure.
646 */
647static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
648 int dma_size, enum dma_data_direction dir)
649{
650 if (!ioctx)
651 return;
652
653 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
654 kfree(ioctx->buf);
655 kfree(ioctx);
656}
657
658/**
659 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
660 * @sdev: Device to allocate the I/O context ring for.
661 * @ring_size: Number of elements in the I/O context ring.
662 * @ioctx_size: I/O context size.
663 * @dma_size: DMA buffer size.
664 * @dir: DMA data direction.
665 */
666static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
667 int ring_size, int ioctx_size,
668 int dma_size, enum dma_data_direction dir)
669{
670 struct srpt_ioctx **ring;
671 int i;
672
673 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
674 && ioctx_size != sizeof(struct srpt_send_ioctx));
675
676 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
677 if (!ring)
678 goto out;
679 for (i = 0; i < ring_size; ++i) {
680 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
681 if (!ring[i])
682 goto err;
683 ring[i]->index = i;
684 }
685 goto out;
686
687err:
688 while (--i >= 0)
689 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
690 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100691 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000692out:
693 return ring;
694}
695
696/**
697 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
698 */
699static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
700 struct srpt_device *sdev, int ring_size,
701 int dma_size, enum dma_data_direction dir)
702{
703 int i;
704
705 for (i = 0; i < ring_size; ++i)
706 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
707 kfree(ioctx_ring);
708}
709
710/**
711 * srpt_get_cmd_state() - Get the state of a SCSI command.
712 */
713static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
714{
715 enum srpt_command_state state;
716 unsigned long flags;
717
718 BUG_ON(!ioctx);
719
720 spin_lock_irqsave(&ioctx->spinlock, flags);
721 state = ioctx->state;
722 spin_unlock_irqrestore(&ioctx->spinlock, flags);
723 return state;
724}
725
726/**
727 * srpt_set_cmd_state() - Set the state of a SCSI command.
728 *
729 * Does not modify the state of aborted commands. Returns the previous command
730 * state.
731 */
732static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
733 enum srpt_command_state new)
734{
735 enum srpt_command_state previous;
736 unsigned long flags;
737
738 BUG_ON(!ioctx);
739
740 spin_lock_irqsave(&ioctx->spinlock, flags);
741 previous = ioctx->state;
742 if (previous != SRPT_STATE_DONE)
743 ioctx->state = new;
744 spin_unlock_irqrestore(&ioctx->spinlock, flags);
745
746 return previous;
747}
748
749/**
750 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
751 *
752 * Returns true if and only if the previous command state was equal to 'old'.
753 */
754static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
755 enum srpt_command_state old,
756 enum srpt_command_state new)
757{
758 enum srpt_command_state previous;
759 unsigned long flags;
760
761 WARN_ON(!ioctx);
762 WARN_ON(old == SRPT_STATE_DONE);
763 WARN_ON(new == SRPT_STATE_NEW);
764
765 spin_lock_irqsave(&ioctx->spinlock, flags);
766 previous = ioctx->state;
767 if (previous == old)
768 ioctx->state = new;
769 spin_unlock_irqrestore(&ioctx->spinlock, flags);
770 return previous == old;
771}
772
773/**
774 * srpt_post_recv() - Post an IB receive request.
775 */
776static int srpt_post_recv(struct srpt_device *sdev,
777 struct srpt_recv_ioctx *ioctx)
778{
779 struct ib_sge list;
780 struct ib_recv_wr wr, *bad_wr;
781
782 BUG_ON(!sdev);
783 wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
784
785 list.addr = ioctx->ioctx.dma;
786 list.length = srp_max_req_size;
787 list.lkey = sdev->mr->lkey;
788
789 wr.next = NULL;
790 wr.sg_list = &list;
791 wr.num_sge = 1;
792
793 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
794}
795
796/**
797 * srpt_post_send() - Post an IB send request.
798 *
799 * Returns zero upon success and a non-zero value upon failure.
800 */
801static int srpt_post_send(struct srpt_rdma_ch *ch,
802 struct srpt_send_ioctx *ioctx, int len)
803{
804 struct ib_sge list;
805 struct ib_send_wr wr, *bad_wr;
806 struct srpt_device *sdev = ch->sport->sdev;
807 int ret;
808
809 atomic_inc(&ch->req_lim);
810
811 ret = -ENOMEM;
812 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400813 pr_warn("IB send queue full (needed 1)\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000814 goto out;
815 }
816
817 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
818 DMA_TO_DEVICE);
819
820 list.addr = ioctx->ioctx.dma;
821 list.length = len;
822 list.lkey = sdev->mr->lkey;
823
824 wr.next = NULL;
825 wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
826 wr.sg_list = &list;
827 wr.num_sge = 1;
828 wr.opcode = IB_WR_SEND;
829 wr.send_flags = IB_SEND_SIGNALED;
830
831 ret = ib_post_send(ch->qp, &wr, &bad_wr);
832
833out:
834 if (ret < 0) {
835 atomic_inc(&ch->sq_wr_avail);
836 atomic_dec(&ch->req_lim);
837 }
838 return ret;
839}
840
841/**
842 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
843 * @ioctx: Pointer to the I/O context associated with the request.
844 * @srp_cmd: Pointer to the SRP_CMD request data.
845 * @dir: Pointer to the variable to which the transfer direction will be
846 * written.
847 * @data_len: Pointer to the variable to which the total data length of all
848 * descriptors in the SRP_CMD request will be written.
849 *
850 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
851 *
852 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
853 * -ENOMEM when memory allocation fails and zero upon success.
854 */
855static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
856 struct srp_cmd *srp_cmd,
857 enum dma_data_direction *dir, u64 *data_len)
858{
859 struct srp_indirect_buf *idb;
860 struct srp_direct_buf *db;
861 unsigned add_cdb_offset;
862 int ret;
863
864 /*
865 * The pointer computations below will only be compiled correctly
866 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
867 * whether srp_cmd::add_data has been declared as a byte pointer.
868 */
869 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
870 && !__same_type(srp_cmd->add_data[0], (u8)0));
871
872 BUG_ON(!dir);
873 BUG_ON(!data_len);
874
875 ret = 0;
876 *data_len = 0;
877
878 /*
879 * The lower four bits of the buffer format field contain the DATA-IN
880 * buffer descriptor format, and the highest four bits contain the
881 * DATA-OUT buffer descriptor format.
882 */
883 *dir = DMA_NONE;
884 if (srp_cmd->buf_fmt & 0xf)
885 /* DATA-IN: transfer data from target to initiator (read). */
886 *dir = DMA_FROM_DEVICE;
887 else if (srp_cmd->buf_fmt >> 4)
888 /* DATA-OUT: transfer data from initiator to target (write). */
889 *dir = DMA_TO_DEVICE;
890
891 /*
892 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
893 * CDB LENGTH' field are reserved and the size in bytes of this field
894 * is four times the value specified in bits 3..7. Hence the "& ~3".
895 */
896 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
897 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
898 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
899 ioctx->n_rbuf = 1;
900 ioctx->rbufs = &ioctx->single_rbuf;
901
902 db = (struct srp_direct_buf *)(srp_cmd->add_data
903 + add_cdb_offset);
904 memcpy(ioctx->rbufs, db, sizeof *db);
905 *data_len = be32_to_cpu(db->len);
906 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
907 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
908 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
909 + add_cdb_offset);
910
911 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
912
913 if (ioctx->n_rbuf >
914 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400915 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000916 " type (%u out + %u in != %u / %zu)\n",
917 srp_cmd->data_out_desc_cnt,
918 srp_cmd->data_in_desc_cnt,
919 be32_to_cpu(idb->table_desc.len),
920 sizeof(*db));
921 ioctx->n_rbuf = 0;
922 ret = -EINVAL;
923 goto out;
924 }
925
926 if (ioctx->n_rbuf == 1)
927 ioctx->rbufs = &ioctx->single_rbuf;
928 else {
929 ioctx->rbufs =
930 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
931 if (!ioctx->rbufs) {
932 ioctx->n_rbuf = 0;
933 ret = -ENOMEM;
934 goto out;
935 }
936 }
937
938 db = idb->desc_list;
939 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
940 *data_len = be32_to_cpu(idb->len);
941 }
942out:
943 return ret;
944}
945
946/**
947 * srpt_init_ch_qp() - Initialize queue pair attributes.
948 *
949 * Initialized the attributes of queue pair 'qp' by allowing local write,
950 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
951 */
952static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
953{
954 struct ib_qp_attr *attr;
955 int ret;
956
957 attr = kzalloc(sizeof *attr, GFP_KERNEL);
958 if (!attr)
959 return -ENOMEM;
960
961 attr->qp_state = IB_QPS_INIT;
962 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
963 IB_ACCESS_REMOTE_WRITE;
964 attr->port_num = ch->sport->port;
965 attr->pkey_index = 0;
966
967 ret = ib_modify_qp(qp, attr,
968 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
969 IB_QP_PKEY_INDEX);
970
971 kfree(attr);
972 return ret;
973}
974
975/**
976 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
977 * @ch: channel of the queue pair.
978 * @qp: queue pair to change the state of.
979 *
980 * Returns zero upon success and a negative value upon failure.
981 *
982 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
983 * If this structure ever becomes larger, it might be necessary to allocate
984 * it dynamically instead of on the stack.
985 */
986static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
987{
988 struct ib_qp_attr qp_attr;
989 int attr_mask;
990 int ret;
991
992 qp_attr.qp_state = IB_QPS_RTR;
993 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
994 if (ret)
995 goto out;
996
997 qp_attr.max_dest_rd_atomic = 4;
998
999 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1000
1001out:
1002 return ret;
1003}
1004
1005/**
1006 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1007 * @ch: channel of the queue pair.
1008 * @qp: queue pair to change the state of.
1009 *
1010 * Returns zero upon success and a negative value upon failure.
1011 *
1012 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1013 * If this structure ever becomes larger, it might be necessary to allocate
1014 * it dynamically instead of on the stack.
1015 */
1016static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1017{
1018 struct ib_qp_attr qp_attr;
1019 int attr_mask;
1020 int ret;
1021
1022 qp_attr.qp_state = IB_QPS_RTS;
1023 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1024 if (ret)
1025 goto out;
1026
1027 qp_attr.max_rd_atomic = 4;
1028
1029 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1030
1031out:
1032 return ret;
1033}
1034
1035/**
1036 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1037 */
1038static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1039{
1040 struct ib_qp_attr qp_attr;
1041
1042 qp_attr.qp_state = IB_QPS_ERR;
1043 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1044}
1045
1046/**
1047 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1048 */
1049static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1050 struct srpt_send_ioctx *ioctx)
1051{
1052 struct scatterlist *sg;
1053 enum dma_data_direction dir;
1054
1055 BUG_ON(!ch);
1056 BUG_ON(!ioctx);
1057 BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
1058
1059 while (ioctx->n_rdma)
1060 kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
1061
1062 kfree(ioctx->rdma_ius);
1063 ioctx->rdma_ius = NULL;
1064
1065 if (ioctx->mapped_sg_count) {
1066 sg = ioctx->sg;
1067 WARN_ON(!sg);
1068 dir = ioctx->cmd.data_direction;
1069 BUG_ON(dir == DMA_NONE);
1070 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1071 opposite_dma_dir(dir));
1072 ioctx->mapped_sg_count = 0;
1073 }
1074}
1075
1076/**
1077 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1078 */
1079static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1080 struct srpt_send_ioctx *ioctx)
1081{
Mike Marciniszynb0768082014-04-07 13:58:35 -04001082 struct ib_device *dev = ch->sport->sdev->device;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001083 struct se_cmd *cmd;
1084 struct scatterlist *sg, *sg_orig;
1085 int sg_cnt;
1086 enum dma_data_direction dir;
1087 struct rdma_iu *riu;
1088 struct srp_direct_buf *db;
1089 dma_addr_t dma_addr;
1090 struct ib_sge *sge;
1091 u64 raddr;
1092 u32 rsize;
1093 u32 tsize;
1094 u32 dma_len;
1095 int count, nrdma;
1096 int i, j, k;
1097
1098 BUG_ON(!ch);
1099 BUG_ON(!ioctx);
1100 cmd = &ioctx->cmd;
1101 dir = cmd->data_direction;
1102 BUG_ON(dir == DMA_NONE);
1103
Roland Dreier6f9e7f02012-03-30 11:29:12 -07001104 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1105 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001106
1107 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1108 opposite_dma_dir(dir));
1109 if (unlikely(!count))
1110 return -EAGAIN;
1111
1112 ioctx->mapped_sg_count = count;
1113
1114 if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1115 nrdma = ioctx->n_rdma_ius;
1116 else {
1117 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1118 + ioctx->n_rbuf;
1119
1120 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
1121 if (!ioctx->rdma_ius)
1122 goto free_mem;
1123
1124 ioctx->n_rdma_ius = nrdma;
1125 }
1126
1127 db = ioctx->rbufs;
1128 tsize = cmd->data_length;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001129 dma_len = ib_sg_dma_len(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001130 riu = ioctx->rdma_ius;
1131
1132 /*
1133 * For each remote desc - calculate the #ib_sge.
1134 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1135 * each remote desc rdma_iu is required a rdma wr;
1136 * else
1137 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1138 * another rdma wr
1139 */
1140 for (i = 0, j = 0;
1141 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1142 rsize = be32_to_cpu(db->len);
1143 raddr = be64_to_cpu(db->va);
1144 riu->raddr = raddr;
1145 riu->rkey = be32_to_cpu(db->key);
1146 riu->sge_cnt = 0;
1147
1148 /* calculate how many sge required for this remote_buf */
1149 while (rsize > 0 && tsize > 0) {
1150
1151 if (rsize >= dma_len) {
1152 tsize -= dma_len;
1153 rsize -= dma_len;
1154 raddr += dma_len;
1155
1156 if (tsize > 0) {
1157 ++j;
1158 if (j < count) {
1159 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001160 dma_len = ib_sg_dma_len(
1161 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001162 }
1163 }
1164 } else {
1165 tsize -= rsize;
1166 dma_len -= rsize;
1167 rsize = 0;
1168 }
1169
1170 ++riu->sge_cnt;
1171
1172 if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1173 ++ioctx->n_rdma;
1174 riu->sge =
1175 kmalloc(riu->sge_cnt * sizeof *riu->sge,
1176 GFP_KERNEL);
1177 if (!riu->sge)
1178 goto free_mem;
1179
1180 ++riu;
1181 riu->sge_cnt = 0;
1182 riu->raddr = raddr;
1183 riu->rkey = be32_to_cpu(db->key);
1184 }
1185 }
1186
1187 ++ioctx->n_rdma;
1188 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1189 GFP_KERNEL);
1190 if (!riu->sge)
1191 goto free_mem;
1192 }
1193
1194 db = ioctx->rbufs;
1195 tsize = cmd->data_length;
1196 riu = ioctx->rdma_ius;
1197 sg = sg_orig;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001198 dma_len = ib_sg_dma_len(dev, &sg[0]);
1199 dma_addr = ib_sg_dma_address(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001200
1201 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1202 for (i = 0, j = 0;
1203 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1204 rsize = be32_to_cpu(db->len);
1205 sge = riu->sge;
1206 k = 0;
1207
1208 while (rsize > 0 && tsize > 0) {
1209 sge->addr = dma_addr;
1210 sge->lkey = ch->sport->sdev->mr->lkey;
1211
1212 if (rsize >= dma_len) {
1213 sge->length =
1214 (tsize < dma_len) ? tsize : dma_len;
1215 tsize -= dma_len;
1216 rsize -= dma_len;
1217
1218 if (tsize > 0) {
1219 ++j;
1220 if (j < count) {
1221 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001222 dma_len = ib_sg_dma_len(
1223 dev, sg);
1224 dma_addr = ib_sg_dma_address(
1225 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001226 }
1227 }
1228 } else {
1229 sge->length = (tsize < rsize) ? tsize : rsize;
1230 tsize -= rsize;
1231 dma_len -= rsize;
1232 dma_addr += rsize;
1233 rsize = 0;
1234 }
1235
1236 ++k;
1237 if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1238 ++riu;
1239 sge = riu->sge;
1240 k = 0;
1241 } else if (rsize > 0 && tsize > 0)
1242 ++sge;
1243 }
1244 }
1245
1246 return 0;
1247
1248free_mem:
1249 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1250
1251 return -ENOMEM;
1252}
1253
1254/**
1255 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1256 */
1257static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1258{
1259 struct srpt_send_ioctx *ioctx;
1260 unsigned long flags;
1261
1262 BUG_ON(!ch);
1263
1264 ioctx = NULL;
1265 spin_lock_irqsave(&ch->spinlock, flags);
1266 if (!list_empty(&ch->free_list)) {
1267 ioctx = list_first_entry(&ch->free_list,
1268 struct srpt_send_ioctx, free_list);
1269 list_del(&ioctx->free_list);
1270 }
1271 spin_unlock_irqrestore(&ch->spinlock, flags);
1272
1273 if (!ioctx)
1274 return ioctx;
1275
1276 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001277 spin_lock_init(&ioctx->spinlock);
1278 ioctx->state = SRPT_STATE_NEW;
1279 ioctx->n_rbuf = 0;
1280 ioctx->rbufs = NULL;
1281 ioctx->n_rdma = 0;
1282 ioctx->n_rdma_ius = 0;
1283 ioctx->rdma_ius = NULL;
1284 ioctx->mapped_sg_count = 0;
1285 init_completion(&ioctx->tx_done);
1286 ioctx->queue_status_only = false;
1287 /*
1288 * transport_init_se_cmd() does not initialize all fields, so do it
1289 * here.
1290 */
1291 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1292 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1293
1294 return ioctx;
1295}
1296
1297/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001298 * srpt_abort_cmd() - Abort a SCSI command.
1299 * @ioctx: I/O context associated with the SCSI command.
1300 * @context: Preferred execution context.
1301 */
1302static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1303{
1304 enum srpt_command_state state;
1305 unsigned long flags;
1306
1307 BUG_ON(!ioctx);
1308
1309 /*
1310 * If the command is in a state where the target core is waiting for
1311 * the ib_srpt driver, change the state to the next state. Changing
1312 * the state of the command from SRPT_STATE_NEED_DATA to
1313 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1314 * function a second time.
1315 */
1316
1317 spin_lock_irqsave(&ioctx->spinlock, flags);
1318 state = ioctx->state;
1319 switch (state) {
1320 case SRPT_STATE_NEED_DATA:
1321 ioctx->state = SRPT_STATE_DATA_IN;
1322 break;
1323 case SRPT_STATE_DATA_IN:
1324 case SRPT_STATE_CMD_RSP_SENT:
1325 case SRPT_STATE_MGMT_RSP_SENT:
1326 ioctx->state = SRPT_STATE_DONE;
1327 break;
1328 default:
1329 break;
1330 }
1331 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1332
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001333 if (state == SRPT_STATE_DONE) {
1334 struct srpt_rdma_ch *ch = ioctx->ch;
1335
1336 BUG_ON(ch->sess == NULL);
1337
1338 target_put_sess_cmd(ch->sess, &ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001339 goto out;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001340 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001341
1342 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1343 ioctx->tag);
1344
1345 switch (state) {
1346 case SRPT_STATE_NEW:
1347 case SRPT_STATE_DATA_IN:
1348 case SRPT_STATE_MGMT:
1349 /*
1350 * Do nothing - defer abort processing until
1351 * srpt_queue_response() is invoked.
1352 */
1353 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1354 break;
1355 case SRPT_STATE_NEED_DATA:
1356 /* DMA_TO_DEVICE (write) - RDMA read error. */
Christoph Hellwige672a472012-07-08 15:58:43 -04001357
1358 /* XXX(hch): this is a horrible layering violation.. */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001359 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
Christoph Hellwige672a472012-07-08 15:58:43 -04001360 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001361 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001362 break;
1363 case SRPT_STATE_CMD_RSP_SENT:
1364 /*
1365 * SRP_RSP sending failed or the SRP_RSP send completion has
1366 * not been received in time.
1367 */
1368 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001369 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001370 break;
1371 case SRPT_STATE_MGMT_RSP_SENT:
1372 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001373 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001374 break;
1375 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001376 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001377 break;
1378 }
1379
1380out:
1381 return state;
1382}
1383
1384/**
1385 * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1386 */
1387static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1388{
1389 struct srpt_send_ioctx *ioctx;
1390 enum srpt_command_state state;
1391 struct se_cmd *cmd;
1392 u32 index;
1393
1394 atomic_inc(&ch->sq_wr_avail);
1395
1396 index = idx_from_wr_id(wr_id);
1397 ioctx = ch->ioctx_ring[index];
1398 state = srpt_get_cmd_state(ioctx);
1399 cmd = &ioctx->cmd;
1400
1401 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1402 && state != SRPT_STATE_MGMT_RSP_SENT
1403 && state != SRPT_STATE_NEED_DATA
1404 && state != SRPT_STATE_DONE);
1405
1406 /* If SRP_RSP sending failed, undo the ch->req_lim change. */
1407 if (state == SRPT_STATE_CMD_RSP_SENT
1408 || state == SRPT_STATE_MGMT_RSP_SENT)
1409 atomic_dec(&ch->req_lim);
1410
1411 srpt_abort_cmd(ioctx);
1412}
1413
1414/**
1415 * srpt_handle_send_comp() - Process an IB send completion notification.
1416 */
1417static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1418 struct srpt_send_ioctx *ioctx)
1419{
1420 enum srpt_command_state state;
1421
1422 atomic_inc(&ch->sq_wr_avail);
1423
1424 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1425
1426 if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1427 && state != SRPT_STATE_MGMT_RSP_SENT
1428 && state != SRPT_STATE_DONE))
1429 pr_debug("state = %d\n", state);
1430
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001431 if (state != SRPT_STATE_DONE) {
1432 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1433 transport_generic_free_cmd(&ioctx->cmd, 0);
1434 } else {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001435 pr_err("IB completion has been received too late for"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001436 " wr_id = %u.\n", ioctx->ioctx.index);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001437 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001438}
1439
1440/**
1441 * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1442 *
Christoph Hellwige672a472012-07-08 15:58:43 -04001443 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1444 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001445 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001446 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001447 */
1448static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1449 struct srpt_send_ioctx *ioctx,
1450 enum srpt_opcode opcode)
1451{
1452 WARN_ON(ioctx->n_rdma <= 0);
1453 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1454
1455 if (opcode == SRPT_RDMA_READ_LAST) {
1456 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1457 SRPT_STATE_DATA_IN))
Christoph Hellwige672a472012-07-08 15:58:43 -04001458 target_execute_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001459 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001460 pr_err("%s[%d]: wrong state = %d\n", __func__,
Bart Van Asschea42d9852011-10-14 01:30:46 +00001461 __LINE__, srpt_get_cmd_state(ioctx));
1462 } else if (opcode == SRPT_RDMA_ABORT) {
1463 ioctx->rdma_aborted = true;
1464 } else {
1465 WARN(true, "unexpected opcode %d\n", opcode);
1466 }
1467}
1468
1469/**
1470 * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1471 */
1472static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1473 struct srpt_send_ioctx *ioctx,
1474 enum srpt_opcode opcode)
1475{
1476 struct se_cmd *cmd;
1477 enum srpt_command_state state;
1478
1479 cmd = &ioctx->cmd;
1480 state = srpt_get_cmd_state(ioctx);
1481 switch (opcode) {
1482 case SRPT_RDMA_READ_LAST:
1483 if (ioctx->n_rdma <= 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001484 pr_err("Received invalid RDMA read"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001485 " error completion with idx %d\n",
1486 ioctx->ioctx.index);
1487 break;
1488 }
1489 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1490 if (state == SRPT_STATE_NEED_DATA)
1491 srpt_abort_cmd(ioctx);
1492 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001493 pr_err("%s[%d]: wrong state = %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001494 __func__, __LINE__, state);
1495 break;
1496 case SRPT_RDMA_WRITE_LAST:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001497 break;
1498 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001499 pr_err("%s[%d]: opcode = %u\n", __func__, __LINE__, opcode);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001500 break;
1501 }
1502}
1503
1504/**
1505 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1506 * @ch: RDMA channel through which the request has been received.
1507 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1508 * be built in the buffer ioctx->buf points at and hence this function will
1509 * overwrite the request data.
1510 * @tag: tag of the request for which this response is being generated.
1511 * @status: value for the STATUS field of the SRP_RSP information unit.
1512 *
1513 * Returns the size in bytes of the SRP_RSP response.
1514 *
1515 * An SRP_RSP response contains a SCSI status or service response. See also
1516 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1517 * response. See also SPC-2 for more information about sense data.
1518 */
1519static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1520 struct srpt_send_ioctx *ioctx, u64 tag,
1521 int status)
1522{
1523 struct srp_rsp *srp_rsp;
1524 const u8 *sense_data;
1525 int sense_data_len, max_sense_len;
1526
1527 /*
1528 * The lowest bit of all SAM-3 status codes is zero (see also
1529 * paragraph 5.3 in SAM-3).
1530 */
1531 WARN_ON(status & 1);
1532
1533 srp_rsp = ioctx->ioctx.buf;
1534 BUG_ON(!srp_rsp);
1535
1536 sense_data = ioctx->sense_data;
1537 sense_data_len = ioctx->cmd.scsi_sense_length;
1538 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1539
1540 memset(srp_rsp, 0, sizeof *srp_rsp);
1541 srp_rsp->opcode = SRP_RSP;
1542 srp_rsp->req_lim_delta =
1543 __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1544 srp_rsp->tag = tag;
1545 srp_rsp->status = status;
1546
1547 if (sense_data_len) {
1548 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1549 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1550 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001551 pr_warn("truncated sense data from %d to %d"
1552 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001553 sense_data_len = max_sense_len;
1554 }
1555
1556 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1557 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1558 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1559 }
1560
1561 return sizeof(*srp_rsp) + sense_data_len;
1562}
1563
1564/**
1565 * srpt_build_tskmgmt_rsp() - Build a task management response.
1566 * @ch: RDMA channel through which the request has been received.
1567 * @ioctx: I/O context in which the SRP_RSP response will be built.
1568 * @rsp_code: RSP_CODE that will be stored in the response.
1569 * @tag: Tag of the request for which this response is being generated.
1570 *
1571 * Returns the size in bytes of the SRP_RSP response.
1572 *
1573 * An SRP_RSP response contains a SCSI status or service response. See also
1574 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1575 * response.
1576 */
1577static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1578 struct srpt_send_ioctx *ioctx,
1579 u8 rsp_code, u64 tag)
1580{
1581 struct srp_rsp *srp_rsp;
1582 int resp_data_len;
1583 int resp_len;
1584
Jack Wangc807f642013-09-30 10:09:05 +02001585 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001586 resp_len = sizeof(*srp_rsp) + resp_data_len;
1587
1588 srp_rsp = ioctx->ioctx.buf;
1589 BUG_ON(!srp_rsp);
1590 memset(srp_rsp, 0, sizeof *srp_rsp);
1591
1592 srp_rsp->opcode = SRP_RSP;
1593 srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
1594 + atomic_xchg(&ch->req_lim_delta, 0));
1595 srp_rsp->tag = tag;
1596
Jack Wangc807f642013-09-30 10:09:05 +02001597 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1598 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1599 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001600
1601 return resp_len;
1602}
1603
1604#define NO_SUCH_LUN ((uint64_t)-1LL)
1605
1606/*
1607 * SCSI LUN addressing method. See also SAM-2 and the section about
1608 * eight byte LUNs.
1609 */
1610enum scsi_lun_addr_method {
1611 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1612 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1613 SCSI_LUN_ADDR_METHOD_LUN = 2,
1614 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1615};
1616
1617/*
1618 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1619 *
1620 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1621 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1622 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1623 */
1624static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1625{
1626 uint64_t res = NO_SUCH_LUN;
1627 int addressing_method;
1628
1629 if (unlikely(len < 2)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001630 pr_err("Illegal LUN length %d, expected 2 bytes or more\n",
1631 len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001632 goto out;
1633 }
1634
1635 switch (len) {
1636 case 8:
1637 if ((*((__be64 *)lun) &
1638 __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1639 goto out_err;
1640 break;
1641 case 4:
1642 if (*((__be16 *)&lun[2]) != 0)
1643 goto out_err;
1644 break;
1645 case 6:
1646 if (*((__be32 *)&lun[2]) != 0)
1647 goto out_err;
1648 break;
1649 case 2:
1650 break;
1651 default:
1652 goto out_err;
1653 }
1654
1655 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1656 switch (addressing_method) {
1657 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1658 case SCSI_LUN_ADDR_METHOD_FLAT:
1659 case SCSI_LUN_ADDR_METHOD_LUN:
1660 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1661 break;
1662
1663 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1664 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001665 pr_err("Unimplemented LUN addressing method %u\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001666 addressing_method);
1667 break;
1668 }
1669
1670out:
1671 return res;
1672
1673out_err:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001674 pr_err("Support for multi-level LUNs has not yet been implemented\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001675 goto out;
1676}
1677
1678static int srpt_check_stop_free(struct se_cmd *cmd)
1679{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001680 struct srpt_send_ioctx *ioctx = container_of(cmd,
1681 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001682
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001683 return target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001684}
1685
1686/**
1687 * srpt_handle_cmd() - Process SRP_CMD.
1688 */
1689static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1690 struct srpt_recv_ioctx *recv_ioctx,
1691 struct srpt_send_ioctx *send_ioctx)
1692{
1693 struct se_cmd *cmd;
1694 struct srp_cmd *srp_cmd;
1695 uint64_t unpacked_lun;
1696 u64 data_len;
1697 enum dma_data_direction dir;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001698 sense_reason_t ret;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001699 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001700
1701 BUG_ON(!send_ioctx);
1702
1703 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001704 cmd = &send_ioctx->cmd;
1705 send_ioctx->tag = srp_cmd->tag;
1706
1707 switch (srp_cmd->task_attr) {
1708 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001709 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001710 break;
1711 case SRP_CMD_ORDERED_Q:
1712 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001713 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001714 break;
1715 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001716 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001717 break;
1718 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001719 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001720 break;
1721 }
1722
Christoph Hellwigde103c92012-11-06 12:24:09 -08001723 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001724 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001725 srp_cmd->tag);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001726 ret = TCM_INVALID_CDB_FIELD;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001727 goto send_sense;
1728 }
1729
Bart Van Asschea42d9852011-10-14 01:30:46 +00001730 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1731 sizeof(srp_cmd->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001732 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1733 &send_ioctx->sense_data[0], unpacked_lun, data_len,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001734 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001735 if (rc != 0) {
1736 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001737 goto send_sense;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001738 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001739 return 0;
1740
1741send_sense:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001742 transport_send_check_condition_and_sense(cmd, ret, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001743 return -1;
1744}
1745
1746/**
1747 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1748 * @ch: RDMA channel of the task management request.
1749 * @fn: Task management function to perform.
1750 * @req_tag: Tag of the SRP task management request.
1751 * @mgmt_ioctx: I/O context of the task management request.
1752 *
1753 * Returns zero if the target core will process the task management
1754 * request asynchronously.
1755 *
1756 * Note: It is assumed that the initiator serializes tag-based task management
1757 * requests.
1758 */
1759static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1760{
1761 struct srpt_device *sdev;
1762 struct srpt_rdma_ch *ch;
1763 struct srpt_send_ioctx *target;
1764 int ret, i;
1765
1766 ret = -EINVAL;
1767 ch = ioctx->ch;
1768 BUG_ON(!ch);
1769 BUG_ON(!ch->sport);
1770 sdev = ch->sport->sdev;
1771 BUG_ON(!sdev);
1772 spin_lock_irq(&sdev->spinlock);
1773 for (i = 0; i < ch->rq_size; ++i) {
1774 target = ch->ioctx_ring[i];
1775 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
1776 target->tag == tag &&
1777 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1778 ret = 0;
1779 /* now let the target core abort &target->cmd; */
1780 break;
1781 }
1782 }
1783 spin_unlock_irq(&sdev->spinlock);
1784 return ret;
1785}
1786
1787static int srp_tmr_to_tcm(int fn)
1788{
1789 switch (fn) {
1790 case SRP_TSK_ABORT_TASK:
1791 return TMR_ABORT_TASK;
1792 case SRP_TSK_ABORT_TASK_SET:
1793 return TMR_ABORT_TASK_SET;
1794 case SRP_TSK_CLEAR_TASK_SET:
1795 return TMR_CLEAR_TASK_SET;
1796 case SRP_TSK_LUN_RESET:
1797 return TMR_LUN_RESET;
1798 case SRP_TSK_CLEAR_ACA:
1799 return TMR_CLEAR_ACA;
1800 default:
1801 return -1;
1802 }
1803}
1804
1805/**
1806 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1807 *
1808 * Returns 0 if and only if the request will be processed by the target core.
1809 *
1810 * For more information about SRP_TSK_MGMT information units, see also section
1811 * 6.7 in the SRP r16a document.
1812 */
1813static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1814 struct srpt_recv_ioctx *recv_ioctx,
1815 struct srpt_send_ioctx *send_ioctx)
1816{
1817 struct srp_tsk_mgmt *srp_tsk;
1818 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001819 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001820 uint64_t unpacked_lun;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001821 uint32_t tag = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001822 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001823 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001824
1825 BUG_ON(!send_ioctx);
1826
1827 srp_tsk = recv_ioctx->ioctx.buf;
1828 cmd = &send_ioctx->cmd;
1829
1830 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1831 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1832 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1833
1834 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1835 send_ioctx->tag = srp_tsk->tag;
1836 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1837 if (tcm_tmr < 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001838 send_ioctx->cmd.se_tmr_req->response =
1839 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001840 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001841 }
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001842 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1843 sizeof(srp_tsk->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001844
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001845 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK) {
1846 rc = srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1847 if (rc < 0) {
1848 send_ioctx->cmd.se_tmr_req->response =
1849 TMR_TASK_DOES_NOT_EXIST;
1850 goto fail;
1851 }
1852 tag = srp_tsk->task_tag;
1853 }
1854 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
1855 srp_tsk, tcm_tmr, GFP_KERNEL, tag,
1856 TARGET_SCF_ACK_KREF);
1857 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001858 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001859 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001860 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001861 return;
1862fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001863 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001864}
1865
1866/**
1867 * srpt_handle_new_iu() - Process a newly received information unit.
1868 * @ch: RDMA channel through which the information unit has been received.
1869 * @ioctx: SRPT I/O context associated with the information unit.
1870 */
1871static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1872 struct srpt_recv_ioctx *recv_ioctx,
1873 struct srpt_send_ioctx *send_ioctx)
1874{
1875 struct srp_cmd *srp_cmd;
1876 enum rdma_ch_state ch_state;
1877
1878 BUG_ON(!ch);
1879 BUG_ON(!recv_ioctx);
1880
1881 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1882 recv_ioctx->ioctx.dma, srp_max_req_size,
1883 DMA_FROM_DEVICE);
1884
1885 ch_state = srpt_get_ch_state(ch);
1886 if (unlikely(ch_state == CH_CONNECTING)) {
1887 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1888 goto out;
1889 }
1890
1891 if (unlikely(ch_state != CH_LIVE))
1892 goto out;
1893
1894 srp_cmd = recv_ioctx->ioctx.buf;
1895 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1896 if (!send_ioctx)
1897 send_ioctx = srpt_get_send_ioctx(ch);
1898 if (unlikely(!send_ioctx)) {
1899 list_add_tail(&recv_ioctx->wait_list,
1900 &ch->cmd_wait_list);
1901 goto out;
1902 }
1903 }
1904
Bart Van Asschea42d9852011-10-14 01:30:46 +00001905 switch (srp_cmd->opcode) {
1906 case SRP_CMD:
1907 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1908 break;
1909 case SRP_TSK_MGMT:
1910 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1911 break;
1912 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001913 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001914 break;
1915 case SRP_CRED_RSP:
1916 pr_debug("received SRP_CRED_RSP\n");
1917 break;
1918 case SRP_AER_RSP:
1919 pr_debug("received SRP_AER_RSP\n");
1920 break;
1921 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001922 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001923 break;
1924 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001925 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001926 srp_cmd->opcode);
1927 break;
1928 }
1929
1930 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1931out:
1932 return;
1933}
1934
1935static void srpt_process_rcv_completion(struct ib_cq *cq,
1936 struct srpt_rdma_ch *ch,
1937 struct ib_wc *wc)
1938{
1939 struct srpt_device *sdev = ch->sport->sdev;
1940 struct srpt_recv_ioctx *ioctx;
1941 u32 index;
1942
1943 index = idx_from_wr_id(wc->wr_id);
1944 if (wc->status == IB_WC_SUCCESS) {
1945 int req_lim;
1946
1947 req_lim = atomic_dec_return(&ch->req_lim);
1948 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001949 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001950 ioctx = sdev->ioctx_ring[index];
1951 srpt_handle_new_iu(ch, ioctx, NULL);
1952 } else {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001953 pr_info("receiving failed for idx %u with status %d\n",
1954 index, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001955 }
1956}
1957
1958/**
1959 * srpt_process_send_completion() - Process an IB send completion.
1960 *
1961 * Note: Although this has not yet been observed during tests, at least in
1962 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1963 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1964 * value in each response is set to one, and it is possible that this response
1965 * makes the initiator send a new request before the send completion for that
1966 * response has been processed. This could e.g. happen if the call to
1967 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1968 * if IB retransmission causes generation of the send completion to be
1969 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1970 * are queued on cmd_wait_list. The code below processes these delayed
1971 * requests one at a time.
1972 */
1973static void srpt_process_send_completion(struct ib_cq *cq,
1974 struct srpt_rdma_ch *ch,
1975 struct ib_wc *wc)
1976{
1977 struct srpt_send_ioctx *send_ioctx;
1978 uint32_t index;
1979 enum srpt_opcode opcode;
1980
1981 index = idx_from_wr_id(wc->wr_id);
1982 opcode = opcode_from_wr_id(wc->wr_id);
1983 send_ioctx = ch->ioctx_ring[index];
1984 if (wc->status == IB_WC_SUCCESS) {
1985 if (opcode == SRPT_SEND)
1986 srpt_handle_send_comp(ch, send_ioctx);
1987 else {
1988 WARN_ON(opcode != SRPT_RDMA_ABORT &&
1989 wc->opcode != IB_WC_RDMA_READ);
1990 srpt_handle_rdma_comp(ch, send_ioctx, opcode);
1991 }
1992 } else {
1993 if (opcode == SRPT_SEND) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001994 pr_info("sending response for idx %u failed"
1995 " with status %d\n", index, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001996 srpt_handle_send_err_comp(ch, wc->wr_id);
1997 } else if (opcode != SRPT_RDMA_MID) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001998 pr_info("RDMA t %d for idx %u failed with"
1999 " status %d\n", opcode, index, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002000 srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
2001 }
2002 }
2003
2004 while (unlikely(opcode == SRPT_SEND
2005 && !list_empty(&ch->cmd_wait_list)
2006 && srpt_get_ch_state(ch) == CH_LIVE
2007 && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
2008 struct srpt_recv_ioctx *recv_ioctx;
2009
2010 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
2011 struct srpt_recv_ioctx,
2012 wait_list);
2013 list_del(&recv_ioctx->wait_list);
2014 srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
2015 }
2016}
2017
2018static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
2019{
2020 struct ib_wc *const wc = ch->wc;
2021 int i, n;
2022
2023 WARN_ON(cq != ch->cq);
2024
2025 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
2026 while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
2027 for (i = 0; i < n; i++) {
2028 if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
2029 srpt_process_rcv_completion(cq, ch, &wc[i]);
2030 else
2031 srpt_process_send_completion(cq, ch, &wc[i]);
2032 }
2033 }
2034}
2035
2036/**
2037 * srpt_completion() - IB completion queue callback function.
2038 *
2039 * Notes:
2040 * - It is guaranteed that a completion handler will never be invoked
2041 * concurrently on two different CPUs for the same completion queue. See also
2042 * Documentation/infiniband/core_locking.txt and the implementation of
2043 * handle_edge_irq() in kernel/irq/chip.c.
2044 * - When threaded IRQs are enabled, completion handlers are invoked in thread
2045 * context instead of interrupt context.
2046 */
2047static void srpt_completion(struct ib_cq *cq, void *ctx)
2048{
2049 struct srpt_rdma_ch *ch = ctx;
2050
2051 wake_up_interruptible(&ch->wait_queue);
2052}
2053
2054static int srpt_compl_thread(void *arg)
2055{
2056 struct srpt_rdma_ch *ch;
2057
2058 /* Hibernation / freezing of the SRPT kernel thread is not supported. */
2059 current->flags |= PF_NOFREEZE;
2060
2061 ch = arg;
2062 BUG_ON(!ch);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002063 pr_info("Session %s: kernel thread %s (PID %d) started\n",
2064 ch->sess_name, ch->thread->comm, current->pid);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002065 while (!kthread_should_stop()) {
2066 wait_event_interruptible(ch->wait_queue,
2067 (srpt_process_completion(ch->cq, ch),
2068 kthread_should_stop()));
2069 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002070 pr_info("Session %s: kernel thread %s (PID %d) stopped\n",
2071 ch->sess_name, ch->thread->comm, current->pid);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002072 return 0;
2073}
2074
2075/**
2076 * srpt_create_ch_ib() - Create receive and send completion queues.
2077 */
2078static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2079{
2080 struct ib_qp_init_attr *qp_init;
2081 struct srpt_port *sport = ch->sport;
2082 struct srpt_device *sdev = sport->sdev;
2083 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
Matan Barak8e372102015-06-11 16:35:21 +03002084 struct ib_cq_init_attr cq_attr = {};
Bart Van Asschea42d9852011-10-14 01:30:46 +00002085 int ret;
2086
2087 WARN_ON(ch->rq_size < 1);
2088
2089 ret = -ENOMEM;
2090 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2091 if (!qp_init)
2092 goto out;
2093
Bart Van Asscheab477c12014-10-19 18:05:33 +03002094retry:
Matan Barak8e372102015-06-11 16:35:21 +03002095 cq_attr.cqe = ch->rq_size + srp_sq_size;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002096 ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
Matan Barak8e372102015-06-11 16:35:21 +03002097 &cq_attr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002098 if (IS_ERR(ch->cq)) {
2099 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002100 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002101 ch->rq_size + srp_sq_size, ret);
2102 goto out;
2103 }
2104
2105 qp_init->qp_context = (void *)ch;
2106 qp_init->event_handler
2107 = (void(*)(struct ib_event *, void*))srpt_qp_event;
2108 qp_init->send_cq = ch->cq;
2109 qp_init->recv_cq = ch->cq;
2110 qp_init->srq = sdev->srq;
2111 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2112 qp_init->qp_type = IB_QPT_RC;
2113 qp_init->cap.max_send_wr = srp_sq_size;
2114 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2115
2116 ch->qp = ib_create_qp(sdev->pd, qp_init);
2117 if (IS_ERR(ch->qp)) {
2118 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03002119 if (ret == -ENOMEM) {
2120 srp_sq_size /= 2;
2121 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
2122 ib_destroy_cq(ch->cq);
2123 goto retry;
2124 }
2125 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002126 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002127 goto err_destroy_cq;
2128 }
2129
2130 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2131
2132 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2133 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2134 qp_init->cap.max_send_wr, ch->cm_id);
2135
2136 ret = srpt_init_ch_qp(ch, ch->qp);
2137 if (ret)
2138 goto err_destroy_qp;
2139
2140 init_waitqueue_head(&ch->wait_queue);
2141
2142 pr_debug("creating thread for session %s\n", ch->sess_name);
2143
2144 ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2145 if (IS_ERR(ch->thread)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002146 pr_err("failed to create kernel thread %ld\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002147 PTR_ERR(ch->thread));
2148 ch->thread = NULL;
2149 goto err_destroy_qp;
2150 }
2151
2152out:
2153 kfree(qp_init);
2154 return ret;
2155
2156err_destroy_qp:
2157 ib_destroy_qp(ch->qp);
2158err_destroy_cq:
2159 ib_destroy_cq(ch->cq);
2160 goto out;
2161}
2162
2163static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2164{
2165 if (ch->thread)
2166 kthread_stop(ch->thread);
2167
2168 ib_destroy_qp(ch->qp);
2169 ib_destroy_cq(ch->cq);
2170}
2171
2172/**
2173 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2174 *
2175 * Reset the QP and make sure all resources associated with the channel will
2176 * be deallocated at an appropriate time.
2177 *
2178 * Note: The caller must hold ch->sport->sdev->spinlock.
2179 */
2180static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2181{
2182 struct srpt_device *sdev;
2183 enum rdma_ch_state prev_state;
2184 unsigned long flags;
2185
2186 sdev = ch->sport->sdev;
2187
2188 spin_lock_irqsave(&ch->spinlock, flags);
2189 prev_state = ch->state;
2190 switch (prev_state) {
2191 case CH_CONNECTING:
2192 case CH_LIVE:
2193 ch->state = CH_DISCONNECTING;
2194 break;
2195 default:
2196 break;
2197 }
2198 spin_unlock_irqrestore(&ch->spinlock, flags);
2199
2200 switch (prev_state) {
2201 case CH_CONNECTING:
2202 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2203 NULL, 0);
2204 /* fall through */
2205 case CH_LIVE:
2206 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002207 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002208 break;
2209 case CH_DISCONNECTING:
2210 break;
2211 case CH_DRAINING:
2212 case CH_RELEASING:
2213 break;
2214 }
2215}
2216
2217/**
2218 * srpt_close_ch() - Close an RDMA channel.
2219 */
2220static void srpt_close_ch(struct srpt_rdma_ch *ch)
2221{
2222 struct srpt_device *sdev;
2223
2224 sdev = ch->sport->sdev;
2225 spin_lock_irq(&sdev->spinlock);
2226 __srpt_close_ch(ch);
2227 spin_unlock_irq(&sdev->spinlock);
2228}
2229
2230/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002231 * srpt_shutdown_session() - Whether or not a session may be shut down.
2232 */
2233static int srpt_shutdown_session(struct se_session *se_sess)
2234{
2235 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2236 unsigned long flags;
2237
2238 spin_lock_irqsave(&ch->spinlock, flags);
2239 if (ch->in_shutdown) {
2240 spin_unlock_irqrestore(&ch->spinlock, flags);
2241 return true;
2242 }
2243
2244 ch->in_shutdown = true;
2245 target_sess_cmd_list_set_waiting(se_sess);
2246 spin_unlock_irqrestore(&ch->spinlock, flags);
2247
2248 return true;
2249}
2250
2251/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002252 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2253 * @cm_id: Pointer to the CM ID of the channel to be drained.
2254 *
2255 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2256 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2257 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2258 * waits until all target sessions for the associated IB device have been
2259 * unregistered and target session registration involves a call to
2260 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2261 * this function has finished).
2262 */
2263static void srpt_drain_channel(struct ib_cm_id *cm_id)
2264{
2265 struct srpt_device *sdev;
2266 struct srpt_rdma_ch *ch;
2267 int ret;
2268 bool do_reset = false;
2269
2270 WARN_ON_ONCE(irqs_disabled());
2271
2272 sdev = cm_id->context;
2273 BUG_ON(!sdev);
2274 spin_lock_irq(&sdev->spinlock);
2275 list_for_each_entry(ch, &sdev->rch_list, list) {
2276 if (ch->cm_id == cm_id) {
2277 do_reset = srpt_test_and_set_ch_state(ch,
2278 CH_CONNECTING, CH_DRAINING) ||
2279 srpt_test_and_set_ch_state(ch,
2280 CH_LIVE, CH_DRAINING) ||
2281 srpt_test_and_set_ch_state(ch,
2282 CH_DISCONNECTING, CH_DRAINING);
2283 break;
2284 }
2285 }
2286 spin_unlock_irq(&sdev->spinlock);
2287
2288 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002289 if (ch->sess)
2290 srpt_shutdown_session(ch->sess);
2291
Bart Van Asschea42d9852011-10-14 01:30:46 +00002292 ret = srpt_ch_qp_err(ch);
2293 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002294 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002295 " failed: %d\n", ret);
2296 }
2297}
2298
2299/**
2300 * srpt_find_channel() - Look up an RDMA channel.
2301 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2302 *
2303 * Return NULL if no matching RDMA channel has been found.
2304 */
2305static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2306 struct ib_cm_id *cm_id)
2307{
2308 struct srpt_rdma_ch *ch;
2309 bool found;
2310
2311 WARN_ON_ONCE(irqs_disabled());
2312 BUG_ON(!sdev);
2313
2314 found = false;
2315 spin_lock_irq(&sdev->spinlock);
2316 list_for_each_entry(ch, &sdev->rch_list, list) {
2317 if (ch->cm_id == cm_id) {
2318 found = true;
2319 break;
2320 }
2321 }
2322 spin_unlock_irq(&sdev->spinlock);
2323
2324 return found ? ch : NULL;
2325}
2326
2327/**
2328 * srpt_release_channel() - Release channel resources.
2329 *
2330 * Schedules the actual release because:
2331 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2332 * trigger a deadlock.
2333 * - It is not safe to call TCM transport_* functions from interrupt context.
2334 */
2335static void srpt_release_channel(struct srpt_rdma_ch *ch)
2336{
2337 schedule_work(&ch->release_work);
2338}
2339
2340static void srpt_release_channel_work(struct work_struct *w)
2341{
2342 struct srpt_rdma_ch *ch;
2343 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002344 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002345
2346 ch = container_of(w, struct srpt_rdma_ch, release_work);
2347 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2348 ch->release_done);
2349
2350 sdev = ch->sport->sdev;
2351 BUG_ON(!sdev);
2352
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002353 se_sess = ch->sess;
2354 BUG_ON(!se_sess);
2355
Joern Engelbe646c2d2013-05-15 00:44:07 -07002356 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002357
2358 transport_deregister_session_configfs(se_sess);
2359 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002360 ch->sess = NULL;
2361
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002362 ib_destroy_cm_id(ch->cm_id);
2363
Bart Van Asschea42d9852011-10-14 01:30:46 +00002364 srpt_destroy_ch_ib(ch);
2365
2366 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2367 ch->sport->sdev, ch->rq_size,
2368 ch->rsp_size, DMA_TO_DEVICE);
2369
2370 spin_lock_irq(&sdev->spinlock);
2371 list_del(&ch->list);
2372 spin_unlock_irq(&sdev->spinlock);
2373
Bart Van Asschea42d9852011-10-14 01:30:46 +00002374 if (ch->release_done)
2375 complete(ch->release_done);
2376
2377 wake_up(&sdev->ch_releaseQ);
2378
2379 kfree(ch);
2380}
2381
2382static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2383 u8 i_port_id[16])
2384{
2385 struct srpt_node_acl *nacl;
2386
2387 list_for_each_entry(nacl, &sport->port_acl_list, list)
2388 if (memcmp(nacl->i_port_id, i_port_id,
2389 sizeof(nacl->i_port_id)) == 0)
2390 return nacl;
2391
2392 return NULL;
2393}
2394
2395static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2396 u8 i_port_id[16])
2397{
2398 struct srpt_node_acl *nacl;
2399
2400 spin_lock_irq(&sport->port_acl_lock);
2401 nacl = __srpt_lookup_acl(sport, i_port_id);
2402 spin_unlock_irq(&sport->port_acl_lock);
2403
2404 return nacl;
2405}
2406
2407/**
2408 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2409 *
2410 * Ownership of the cm_id is transferred to the target session if this
2411 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2412 */
2413static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2414 struct ib_cm_req_event_param *param,
2415 void *private_data)
2416{
2417 struct srpt_device *sdev = cm_id->context;
2418 struct srpt_port *sport = &sdev->port[param->port - 1];
2419 struct srp_login_req *req;
2420 struct srp_login_rsp *rsp;
2421 struct srp_login_rej *rej;
2422 struct ib_cm_rep_param *rep_param;
2423 struct srpt_rdma_ch *ch, *tmp_ch;
2424 struct srpt_node_acl *nacl;
2425 u32 it_iu_len;
2426 int i;
2427 int ret = 0;
2428
2429 WARN_ON_ONCE(irqs_disabled());
2430
2431 if (WARN_ON(!sdev || !private_data))
2432 return -EINVAL;
2433
2434 req = (struct srp_login_req *)private_data;
2435
2436 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2437
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002438 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2439 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2440 " (guid=0x%llx:0x%llx)\n",
2441 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2442 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2443 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2444 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2445 it_iu_len,
2446 param->port,
2447 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2448 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002449
2450 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2451 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2452 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2453
2454 if (!rsp || !rej || !rep_param) {
2455 ret = -ENOMEM;
2456 goto out;
2457 }
2458
2459 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2460 rej->reason = __constant_cpu_to_be32(
2461 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2462 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002463 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002464 " length (%d bytes) is out of range (%d .. %d)\n",
2465 it_iu_len, 64, srp_max_req_size);
2466 goto reject;
2467 }
2468
2469 if (!sport->enabled) {
2470 rej->reason = __constant_cpu_to_be32(
2471 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2472 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002473 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002474 " has not yet been enabled\n");
2475 goto reject;
2476 }
2477
2478 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2479 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2480
2481 spin_lock_irq(&sdev->spinlock);
2482
2483 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2484 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2485 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2486 && param->port == ch->sport->port
2487 && param->listen_id == ch->sport->sdev->cm_id
2488 && ch->cm_id) {
2489 enum rdma_ch_state ch_state;
2490
2491 ch_state = srpt_get_ch_state(ch);
2492 if (ch_state != CH_CONNECTING
2493 && ch_state != CH_LIVE)
2494 continue;
2495
2496 /* found an existing channel */
2497 pr_debug("Found existing channel %s"
2498 " cm_id= %p state= %d\n",
2499 ch->sess_name, ch->cm_id, ch_state);
2500
2501 __srpt_close_ch(ch);
2502
2503 rsp->rsp_flags =
2504 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2505 }
2506 }
2507
2508 spin_unlock_irq(&sdev->spinlock);
2509
2510 } else
2511 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2512
2513 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2514 || *(__be64 *)(req->target_port_id + 8) !=
2515 cpu_to_be64(srpt_service_guid)) {
2516 rej->reason = __constant_cpu_to_be32(
2517 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2518 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002519 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002520 " has an invalid target port identifier.\n");
2521 goto reject;
2522 }
2523
2524 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2525 if (!ch) {
2526 rej->reason = __constant_cpu_to_be32(
2527 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002528 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002529 ret = -ENOMEM;
2530 goto reject;
2531 }
2532
2533 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2534 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2535 memcpy(ch->t_port_id, req->target_port_id, 16);
2536 ch->sport = &sdev->port[param->port - 1];
2537 ch->cm_id = cm_id;
2538 /*
2539 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2540 * for the SRP protocol to the command queue size.
2541 */
2542 ch->rq_size = SRPT_RQ_SIZE;
2543 spin_lock_init(&ch->spinlock);
2544 ch->state = CH_CONNECTING;
2545 INIT_LIST_HEAD(&ch->cmd_wait_list);
2546 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2547
2548 ch->ioctx_ring = (struct srpt_send_ioctx **)
2549 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2550 sizeof(*ch->ioctx_ring[0]),
2551 ch->rsp_size, DMA_TO_DEVICE);
2552 if (!ch->ioctx_ring)
2553 goto free_ch;
2554
2555 INIT_LIST_HEAD(&ch->free_list);
2556 for (i = 0; i < ch->rq_size; i++) {
2557 ch->ioctx_ring[i]->ch = ch;
2558 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2559 }
2560
2561 ret = srpt_create_ch_ib(ch);
2562 if (ret) {
2563 rej->reason = __constant_cpu_to_be32(
2564 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002565 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002566 " a new RDMA channel failed.\n");
2567 goto free_ring;
2568 }
2569
2570 ret = srpt_ch_qp_rtr(ch, ch->qp);
2571 if (ret) {
2572 rej->reason = __constant_cpu_to_be32(
2573 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002574 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002575 " RTR failed (error code = %d)\n", ret);
2576 goto destroy_ib;
2577 }
2578 /*
2579 * Use the initator port identifier as the session name.
2580 */
2581 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2582 be64_to_cpu(*(__be64 *)ch->i_port_id),
2583 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2584
2585 pr_debug("registering session %s\n", ch->sess_name);
2586
2587 nacl = srpt_lookup_acl(sport, ch->i_port_id);
2588 if (!nacl) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002589 pr_info("Rejected login because no ACL has been"
2590 " configured yet for initiator %s.\n", ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002591 rej->reason = __constant_cpu_to_be32(
2592 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2593 goto destroy_ib;
2594 }
2595
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002596 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002597 if (IS_ERR(ch->sess)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002598 rej->reason = __constant_cpu_to_be32(
2599 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2600 pr_debug("Failed to create session\n");
2601 goto deregister_session;
2602 }
2603 ch->sess->se_node_acl = &nacl->nacl;
2604 transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2605
2606 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2607 ch->sess_name, ch->cm_id);
2608
2609 /* create srp_login_response */
2610 rsp->opcode = SRP_LOGIN_RSP;
2611 rsp->tag = req->tag;
2612 rsp->max_it_iu_len = req->req_it_iu_len;
2613 rsp->max_ti_iu_len = req->req_it_iu_len;
2614 ch->max_ti_iu_len = it_iu_len;
2615 rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2616 | SRP_BUF_FORMAT_INDIRECT);
2617 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2618 atomic_set(&ch->req_lim, ch->rq_size);
2619 atomic_set(&ch->req_lim_delta, 0);
2620
2621 /* create cm reply */
2622 rep_param->qp_num = ch->qp->qp_num;
2623 rep_param->private_data = (void *)rsp;
2624 rep_param->private_data_len = sizeof *rsp;
2625 rep_param->rnr_retry_count = 7;
2626 rep_param->flow_control = 1;
2627 rep_param->failover_accepted = 0;
2628 rep_param->srq = 1;
2629 rep_param->responder_resources = 4;
2630 rep_param->initiator_depth = 4;
2631
2632 ret = ib_send_cm_rep(cm_id, rep_param);
2633 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002634 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002635 " (error code = %d)\n", ret);
2636 goto release_channel;
2637 }
2638
2639 spin_lock_irq(&sdev->spinlock);
2640 list_add_tail(&ch->list, &sdev->rch_list);
2641 spin_unlock_irq(&sdev->spinlock);
2642
2643 goto out;
2644
2645release_channel:
2646 srpt_set_ch_state(ch, CH_RELEASING);
2647 transport_deregister_session_configfs(ch->sess);
2648
2649deregister_session:
2650 transport_deregister_session(ch->sess);
2651 ch->sess = NULL;
2652
2653destroy_ib:
2654 srpt_destroy_ch_ib(ch);
2655
2656free_ring:
2657 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2658 ch->sport->sdev, ch->rq_size,
2659 ch->rsp_size, DMA_TO_DEVICE);
2660free_ch:
2661 kfree(ch);
2662
2663reject:
2664 rej->opcode = SRP_LOGIN_REJ;
2665 rej->tag = req->tag;
2666 rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2667 | SRP_BUF_FORMAT_INDIRECT);
2668
2669 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2670 (void *)rej, sizeof *rej);
2671
2672out:
2673 kfree(rep_param);
2674 kfree(rsp);
2675 kfree(rej);
2676
2677 return ret;
2678}
2679
2680static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2681{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002682 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002683 srpt_drain_channel(cm_id);
2684}
2685
2686/**
2687 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2688 *
2689 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2690 * and that the recipient may begin transmitting (RTU = ready to use).
2691 */
2692static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2693{
2694 struct srpt_rdma_ch *ch;
2695 int ret;
2696
2697 ch = srpt_find_channel(cm_id->context, cm_id);
2698 BUG_ON(!ch);
2699
2700 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2701 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2702
2703 ret = srpt_ch_qp_rts(ch, ch->qp);
2704
2705 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2706 wait_list) {
2707 list_del(&ioctx->wait_list);
2708 srpt_handle_new_iu(ch, ioctx, NULL);
2709 }
2710 if (ret)
2711 srpt_close_ch(ch);
2712 }
2713}
2714
2715static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2716{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002717 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002718 srpt_drain_channel(cm_id);
2719}
2720
2721static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2722{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002723 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002724 srpt_drain_channel(cm_id);
2725}
2726
2727/**
2728 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2729 */
2730static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2731{
2732 struct srpt_rdma_ch *ch;
2733 unsigned long flags;
2734 bool send_drep = false;
2735
2736 ch = srpt_find_channel(cm_id->context, cm_id);
2737 BUG_ON(!ch);
2738
2739 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2740
2741 spin_lock_irqsave(&ch->spinlock, flags);
2742 switch (ch->state) {
2743 case CH_CONNECTING:
2744 case CH_LIVE:
2745 send_drep = true;
2746 ch->state = CH_DISCONNECTING;
2747 break;
2748 case CH_DISCONNECTING:
2749 case CH_DRAINING:
2750 case CH_RELEASING:
2751 WARN(true, "unexpected channel state %d\n", ch->state);
2752 break;
2753 }
2754 spin_unlock_irqrestore(&ch->spinlock, flags);
2755
2756 if (send_drep) {
2757 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002758 pr_err("Sending IB DREP failed.\n");
2759 pr_info("Received DREQ and sent DREP for session %s.\n",
2760 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002761 }
2762}
2763
2764/**
2765 * srpt_cm_drep_recv() - Process reception of a DREP message.
2766 */
2767static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2768{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002769 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002770 srpt_drain_channel(cm_id);
2771}
2772
2773/**
2774 * srpt_cm_handler() - IB connection manager callback function.
2775 *
2776 * A non-zero return value will cause the caller destroy the CM ID.
2777 *
2778 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2779 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2780 * a non-zero value in any other case will trigger a race with the
2781 * ib_destroy_cm_id() call in srpt_release_channel().
2782 */
2783static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2784{
2785 int ret;
2786
2787 ret = 0;
2788 switch (event->event) {
2789 case IB_CM_REQ_RECEIVED:
2790 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2791 event->private_data);
2792 break;
2793 case IB_CM_REJ_RECEIVED:
2794 srpt_cm_rej_recv(cm_id);
2795 break;
2796 case IB_CM_RTU_RECEIVED:
2797 case IB_CM_USER_ESTABLISHED:
2798 srpt_cm_rtu_recv(cm_id);
2799 break;
2800 case IB_CM_DREQ_RECEIVED:
2801 srpt_cm_dreq_recv(cm_id);
2802 break;
2803 case IB_CM_DREP_RECEIVED:
2804 srpt_cm_drep_recv(cm_id);
2805 break;
2806 case IB_CM_TIMEWAIT_EXIT:
2807 srpt_cm_timewait_exit(cm_id);
2808 break;
2809 case IB_CM_REP_ERROR:
2810 srpt_cm_rep_error(cm_id);
2811 break;
2812 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002813 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002814 break;
2815 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002816 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002817 break;
2818 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002819 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002820 break;
2821 }
2822
2823 return ret;
2824}
2825
2826/**
2827 * srpt_perform_rdmas() - Perform IB RDMA.
2828 *
2829 * Returns zero upon success or a negative number upon failure.
2830 */
2831static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2832 struct srpt_send_ioctx *ioctx)
2833{
2834 struct ib_send_wr wr;
2835 struct ib_send_wr *bad_wr;
2836 struct rdma_iu *riu;
2837 int i;
2838 int ret;
2839 int sq_wr_avail;
2840 enum dma_data_direction dir;
2841 const int n_rdma = ioctx->n_rdma;
2842
2843 dir = ioctx->cmd.data_direction;
2844 if (dir == DMA_TO_DEVICE) {
2845 /* write */
2846 ret = -ENOMEM;
2847 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2848 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002849 pr_warn("IB send queue full (needed %d)\n",
2850 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002851 goto out;
2852 }
2853 }
2854
2855 ioctx->rdma_aborted = false;
2856 ret = 0;
2857 riu = ioctx->rdma_ius;
2858 memset(&wr, 0, sizeof wr);
2859
2860 for (i = 0; i < n_rdma; ++i, ++riu) {
2861 if (dir == DMA_FROM_DEVICE) {
2862 wr.opcode = IB_WR_RDMA_WRITE;
2863 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2864 SRPT_RDMA_WRITE_LAST :
2865 SRPT_RDMA_MID,
2866 ioctx->ioctx.index);
2867 } else {
2868 wr.opcode = IB_WR_RDMA_READ;
2869 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2870 SRPT_RDMA_READ_LAST :
2871 SRPT_RDMA_MID,
2872 ioctx->ioctx.index);
2873 }
2874 wr.next = NULL;
2875 wr.wr.rdma.remote_addr = riu->raddr;
2876 wr.wr.rdma.rkey = riu->rkey;
2877 wr.num_sge = riu->sge_cnt;
2878 wr.sg_list = riu->sge;
2879
2880 /* only get completion event for the last rdma write */
2881 if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
2882 wr.send_flags = IB_SEND_SIGNALED;
2883
2884 ret = ib_post_send(ch->qp, &wr, &bad_wr);
2885 if (ret)
2886 break;
2887 }
2888
2889 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002890 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002891 __func__, __LINE__, ret, i, n_rdma);
2892 if (ret && i > 0) {
2893 wr.num_sge = 0;
2894 wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2895 wr.send_flags = IB_SEND_SIGNALED;
2896 while (ch->state == CH_LIVE &&
2897 ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002898 pr_info("Trying to abort failed RDMA transfer [%d]\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002899 ioctx->ioctx.index);
2900 msleep(1000);
2901 }
2902 while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002903 pr_info("Waiting until RDMA abort finished [%d]\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002904 ioctx->ioctx.index);
2905 msleep(1000);
2906 }
2907 }
2908out:
2909 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2910 atomic_add(n_rdma, &ch->sq_wr_avail);
2911 return ret;
2912}
2913
2914/**
2915 * srpt_xfer_data() - Start data transfer from initiator to target.
2916 */
2917static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2918 struct srpt_send_ioctx *ioctx)
2919{
2920 int ret;
2921
2922 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2923 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002924 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002925 goto out;
2926 }
2927
2928 ret = srpt_perform_rdmas(ch, ioctx);
2929 if (ret) {
2930 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002931 pr_info("%s[%d] queue full -- ret=%d\n",
2932 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002933 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002934 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002935 __func__, __LINE__, ret);
2936 goto out_unmap;
2937 }
2938
2939out:
2940 return ret;
2941out_unmap:
2942 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2943 goto out;
2944}
2945
2946static int srpt_write_pending_status(struct se_cmd *se_cmd)
2947{
2948 struct srpt_send_ioctx *ioctx;
2949
2950 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2951 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2952}
2953
2954/*
2955 * srpt_write_pending() - Start data transfer from initiator to target (write).
2956 */
2957static int srpt_write_pending(struct se_cmd *se_cmd)
2958{
2959 struct srpt_rdma_ch *ch;
2960 struct srpt_send_ioctx *ioctx;
2961 enum srpt_command_state new_state;
2962 enum rdma_ch_state ch_state;
2963 int ret;
2964
2965 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2966
2967 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2968 WARN_ON(new_state == SRPT_STATE_DONE);
2969
2970 ch = ioctx->ch;
2971 BUG_ON(!ch);
2972
2973 ch_state = srpt_get_ch_state(ch);
2974 switch (ch_state) {
2975 case CH_CONNECTING:
2976 WARN(true, "unexpected channel state %d\n", ch_state);
2977 ret = -EINVAL;
2978 goto out;
2979 case CH_LIVE:
2980 break;
2981 case CH_DISCONNECTING:
2982 case CH_DRAINING:
2983 case CH_RELEASING:
2984 pr_debug("cmd with tag %lld: channel disconnecting\n",
2985 ioctx->tag);
2986 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2987 ret = -EINVAL;
2988 goto out;
2989 }
2990 ret = srpt_xfer_data(ch, ioctx);
2991
2992out:
2993 return ret;
2994}
2995
2996static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2997{
2998 switch (tcm_mgmt_status) {
2999 case TMR_FUNCTION_COMPLETE:
3000 return SRP_TSK_MGMT_SUCCESS;
3001 case TMR_FUNCTION_REJECTED:
3002 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
3003 }
3004 return SRP_TSK_MGMT_FAILED;
3005}
3006
3007/**
3008 * srpt_queue_response() - Transmits the response to a SCSI command.
3009 *
3010 * Callback function called by the TCM core. Must not block since it can be
3011 * invoked on the context of the IB completion handler.
3012 */
Joern Engelb79fafa2013-07-03 11:22:17 -04003013static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003014{
3015 struct srpt_rdma_ch *ch;
3016 struct srpt_send_ioctx *ioctx;
3017 enum srpt_command_state state;
3018 unsigned long flags;
3019 int ret;
3020 enum dma_data_direction dir;
3021 int resp_len;
3022 u8 srp_tm_status;
3023
Bart Van Asschea42d9852011-10-14 01:30:46 +00003024 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3025 ch = ioctx->ch;
3026 BUG_ON(!ch);
3027
3028 spin_lock_irqsave(&ioctx->spinlock, flags);
3029 state = ioctx->state;
3030 switch (state) {
3031 case SRPT_STATE_NEW:
3032 case SRPT_STATE_DATA_IN:
3033 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
3034 break;
3035 case SRPT_STATE_MGMT:
3036 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
3037 break;
3038 default:
3039 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
3040 ch, ioctx->ioctx.index, ioctx->state);
3041 break;
3042 }
3043 spin_unlock_irqrestore(&ioctx->spinlock, flags);
3044
3045 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
3046 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
3047 atomic_inc(&ch->req_lim_delta);
3048 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04003049 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003050 }
3051
3052 dir = ioctx->cmd.data_direction;
3053
3054 /* For read commands, transfer the data to the initiator. */
3055 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
3056 !ioctx->queue_status_only) {
3057 ret = srpt_xfer_data(ch, ioctx);
3058 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003059 pr_err("xfer_data failed for tag %llu\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003060 ioctx->tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04003061 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003062 }
3063 }
3064
3065 if (state != SRPT_STATE_MGMT)
3066 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
3067 cmd->scsi_status);
3068 else {
3069 srp_tm_status
3070 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3071 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
3072 ioctx->tag);
3073 }
3074 ret = srpt_post_send(ch, ioctx, resp_len);
3075 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003076 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003077 ioctx->tag);
3078 srpt_unmap_sg_to_ib_sge(ch, ioctx);
3079 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003080 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003081 }
Joern Engelb79fafa2013-07-03 11:22:17 -04003082}
Bart Van Asschea42d9852011-10-14 01:30:46 +00003083
Joern Engelb79fafa2013-07-03 11:22:17 -04003084static int srpt_queue_data_in(struct se_cmd *cmd)
3085{
3086 srpt_queue_response(cmd);
3087 return 0;
3088}
3089
3090static void srpt_queue_tm_rsp(struct se_cmd *cmd)
3091{
3092 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003093}
3094
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003095static void srpt_aborted_task(struct se_cmd *cmd)
3096{
3097 struct srpt_send_ioctx *ioctx = container_of(cmd,
3098 struct srpt_send_ioctx, cmd);
3099
3100 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
3101}
3102
Bart Van Asschea42d9852011-10-14 01:30:46 +00003103static int srpt_queue_status(struct se_cmd *cmd)
3104{
3105 struct srpt_send_ioctx *ioctx;
3106
3107 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3108 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3109 if (cmd->se_cmd_flags &
3110 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3111 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3112 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04003113 srpt_queue_response(cmd);
3114 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003115}
3116
3117static void srpt_refresh_port_work(struct work_struct *work)
3118{
3119 struct srpt_port *sport = container_of(work, struct srpt_port, work);
3120
3121 srpt_refresh_port(sport);
3122}
3123
3124static int srpt_ch_list_empty(struct srpt_device *sdev)
3125{
3126 int res;
3127
3128 spin_lock_irq(&sdev->spinlock);
3129 res = list_empty(&sdev->rch_list);
3130 spin_unlock_irq(&sdev->spinlock);
3131
3132 return res;
3133}
3134
3135/**
3136 * srpt_release_sdev() - Free the channel resources associated with a target.
3137 */
3138static int srpt_release_sdev(struct srpt_device *sdev)
3139{
3140 struct srpt_rdma_ch *ch, *tmp_ch;
3141 int res;
3142
3143 WARN_ON_ONCE(irqs_disabled());
3144
3145 BUG_ON(!sdev);
3146
3147 spin_lock_irq(&sdev->spinlock);
3148 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3149 __srpt_close_ch(ch);
3150 spin_unlock_irq(&sdev->spinlock);
3151
3152 res = wait_event_interruptible(sdev->ch_releaseQ,
3153 srpt_ch_list_empty(sdev));
3154 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003155 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003156
3157 return 0;
3158}
3159
3160static struct srpt_port *__srpt_lookup_port(const char *name)
3161{
3162 struct ib_device *dev;
3163 struct srpt_device *sdev;
3164 struct srpt_port *sport;
3165 int i;
3166
3167 list_for_each_entry(sdev, &srpt_dev_list, list) {
3168 dev = sdev->device;
3169 if (!dev)
3170 continue;
3171
3172 for (i = 0; i < dev->phys_port_cnt; i++) {
3173 sport = &sdev->port[i];
3174
3175 if (!strcmp(sport->port_guid, name))
3176 return sport;
3177 }
3178 }
3179
3180 return NULL;
3181}
3182
3183static struct srpt_port *srpt_lookup_port(const char *name)
3184{
3185 struct srpt_port *sport;
3186
3187 spin_lock(&srpt_dev_lock);
3188 sport = __srpt_lookup_port(name);
3189 spin_unlock(&srpt_dev_lock);
3190
3191 return sport;
3192}
3193
3194/**
3195 * srpt_add_one() - Infiniband device addition callback function.
3196 */
3197static void srpt_add_one(struct ib_device *device)
3198{
3199 struct srpt_device *sdev;
3200 struct srpt_port *sport;
3201 struct ib_srq_init_attr srq_attr;
3202 int i;
3203
3204 pr_debug("device = %p, device->dma_ops = %p\n", device,
3205 device->dma_ops);
3206
3207 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3208 if (!sdev)
3209 goto err;
3210
3211 sdev->device = device;
3212 INIT_LIST_HEAD(&sdev->rch_list);
3213 init_waitqueue_head(&sdev->ch_releaseQ);
3214 spin_lock_init(&sdev->spinlock);
3215
3216 if (ib_query_device(device, &sdev->dev_attr))
3217 goto free_dev;
3218
3219 sdev->pd = ib_alloc_pd(device);
3220 if (IS_ERR(sdev->pd))
3221 goto free_dev;
3222
3223 sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
3224 if (IS_ERR(sdev->mr))
3225 goto err_pd;
3226
3227 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3228
3229 srq_attr.event_handler = srpt_srq_event;
3230 srq_attr.srq_context = (void *)sdev;
3231 srq_attr.attr.max_wr = sdev->srq_size;
3232 srq_attr.attr.max_sge = 1;
3233 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07003234 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003235
3236 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3237 if (IS_ERR(sdev->srq))
3238 goto err_mr;
3239
3240 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3241 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3242 device->name);
3243
3244 if (!srpt_service_guid)
3245 srpt_service_guid = be64_to_cpu(device->node_guid);
3246
3247 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3248 if (IS_ERR(sdev->cm_id))
3249 goto err_srq;
3250
3251 /* print out target login information */
3252 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3253 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3254 srpt_service_guid, srpt_service_guid);
3255
3256 /*
3257 * We do not have a consistent service_id (ie. also id_ext of target_id)
3258 * to identify this target. We currently use the guid of the first HCA
3259 * in the system as service_id; therefore, the target_id will change
3260 * if this HCA is gone bad and replaced by different HCA
3261 */
3262 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
3263 goto err_cm;
3264
3265 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3266 srpt_event_handler);
3267 if (ib_register_event_handler(&sdev->event_handler))
3268 goto err_cm;
3269
3270 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3271 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3272 sizeof(*sdev->ioctx_ring[0]),
3273 srp_max_req_size, DMA_FROM_DEVICE);
3274 if (!sdev->ioctx_ring)
3275 goto err_event;
3276
3277 for (i = 0; i < sdev->srq_size; ++i)
3278 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3279
Roland Dreierf2250662012-02-02 12:55:58 -08003280 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00003281
3282 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3283 sport = &sdev->port[i - 1];
3284 sport->sdev = sdev;
3285 sport->port = i;
3286 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3287 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3288 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3289 INIT_WORK(&sport->work, srpt_refresh_port_work);
3290 INIT_LIST_HEAD(&sport->port_acl_list);
3291 spin_lock_init(&sport->port_acl_lock);
3292
3293 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003294 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003295 srpt_sdev_name(sdev), i);
3296 goto err_ring;
3297 }
3298 snprintf(sport->port_guid, sizeof(sport->port_guid),
3299 "0x%016llx%016llx",
3300 be64_to_cpu(sport->gid.global.subnet_prefix),
3301 be64_to_cpu(sport->gid.global.interface_id));
3302 }
3303
3304 spin_lock(&srpt_dev_lock);
3305 list_add_tail(&sdev->list, &srpt_dev_list);
3306 spin_unlock(&srpt_dev_lock);
3307
3308out:
3309 ib_set_client_data(device, &srpt_client, sdev);
3310 pr_debug("added %s.\n", device->name);
3311 return;
3312
3313err_ring:
3314 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3315 sdev->srq_size, srp_max_req_size,
3316 DMA_FROM_DEVICE);
3317err_event:
3318 ib_unregister_event_handler(&sdev->event_handler);
3319err_cm:
3320 ib_destroy_cm_id(sdev->cm_id);
3321err_srq:
3322 ib_destroy_srq(sdev->srq);
3323err_mr:
3324 ib_dereg_mr(sdev->mr);
3325err_pd:
3326 ib_dealloc_pd(sdev->pd);
3327free_dev:
3328 kfree(sdev);
3329err:
3330 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003331 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003332 goto out;
3333}
3334
3335/**
3336 * srpt_remove_one() - InfiniBand device removal callback function.
3337 */
3338static void srpt_remove_one(struct ib_device *device)
3339{
3340 struct srpt_device *sdev;
3341 int i;
3342
3343 sdev = ib_get_client_data(device, &srpt_client);
3344 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003345 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003346 return;
3347 }
3348
3349 srpt_unregister_mad_agent(sdev);
3350
3351 ib_unregister_event_handler(&sdev->event_handler);
3352
3353 /* Cancel any work queued by the just unregistered IB event handler. */
3354 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3355 cancel_work_sync(&sdev->port[i].work);
3356
3357 ib_destroy_cm_id(sdev->cm_id);
3358
3359 /*
3360 * Unregistering a target must happen after destroying sdev->cm_id
3361 * such that no new SRP_LOGIN_REQ information units can arrive while
3362 * destroying the target.
3363 */
3364 spin_lock(&srpt_dev_lock);
3365 list_del(&sdev->list);
3366 spin_unlock(&srpt_dev_lock);
3367 srpt_release_sdev(sdev);
3368
3369 ib_destroy_srq(sdev->srq);
3370 ib_dereg_mr(sdev->mr);
3371 ib_dealloc_pd(sdev->pd);
3372
3373 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3374 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3375 sdev->ioctx_ring = NULL;
3376 kfree(sdev);
3377}
3378
3379static struct ib_client srpt_client = {
3380 .name = DRV_NAME,
3381 .add = srpt_add_one,
3382 .remove = srpt_remove_one
3383};
3384
3385static int srpt_check_true(struct se_portal_group *se_tpg)
3386{
3387 return 1;
3388}
3389
3390static int srpt_check_false(struct se_portal_group *se_tpg)
3391{
3392 return 0;
3393}
3394
3395static char *srpt_get_fabric_name(void)
3396{
3397 return "srpt";
3398}
3399
3400static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
3401{
3402 return SCSI_TRANSPORTID_PROTOCOLID_SRP;
3403}
3404
3405static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3406{
3407 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3408
3409 return sport->port_guid;
3410}
3411
3412static u16 srpt_get_tag(struct se_portal_group *tpg)
3413{
3414 return 1;
3415}
3416
3417static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
3418{
3419 return 1;
3420}
3421
3422static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
3423 struct se_node_acl *se_nacl,
3424 struct t10_pr_registration *pr_reg,
3425 int *format_code, unsigned char *buf)
3426{
3427 struct srpt_node_acl *nacl;
3428 struct spc_rdma_transport_id *tr_id;
3429
3430 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3431 tr_id = (void *)buf;
3432 tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
3433 memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
3434 return sizeof(*tr_id);
3435}
3436
3437static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
3438 struct se_node_acl *se_nacl,
3439 struct t10_pr_registration *pr_reg,
3440 int *format_code)
3441{
3442 *format_code = 0;
3443 return sizeof(struct spc_rdma_transport_id);
3444}
3445
3446static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
3447 const char *buf, u32 *out_tid_len,
3448 char **port_nexus_ptr)
3449{
3450 struct spc_rdma_transport_id *tr_id;
3451
3452 *port_nexus_ptr = NULL;
3453 *out_tid_len = sizeof(struct spc_rdma_transport_id);
3454 tr_id = (void *)buf;
3455 return (char *)tr_id->i_port_id;
3456}
3457
3458static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
3459{
3460 struct srpt_node_acl *nacl;
3461
3462 nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
3463 if (!nacl) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003464 pr_err("Unable to allocate struct srpt_node_acl\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003465 return NULL;
3466 }
3467
3468 return &nacl->nacl;
3469}
3470
3471static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
3472 struct se_node_acl *se_nacl)
3473{
3474 struct srpt_node_acl *nacl;
3475
3476 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3477 kfree(nacl);
3478}
3479
3480static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3481{
3482 return 1;
3483}
3484
3485static void srpt_release_cmd(struct se_cmd *se_cmd)
3486{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003487 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3488 struct srpt_send_ioctx, cmd);
3489 struct srpt_rdma_ch *ch = ioctx->ch;
3490 unsigned long flags;
3491
3492 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3493 WARN_ON(ioctx->mapped_sg_count != 0);
3494
3495 if (ioctx->n_rbuf > 1) {
3496 kfree(ioctx->rbufs);
3497 ioctx->rbufs = NULL;
3498 ioctx->n_rbuf = 0;
3499 }
3500
3501 spin_lock_irqsave(&ch->spinlock, flags);
3502 list_add(&ioctx->free_list, &ch->free_list);
3503 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003504}
3505
3506/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003507 * srpt_close_session() - Forcibly close a session.
3508 *
3509 * Callback function invoked by the TCM core to clean up sessions associated
3510 * with a node ACL when the user invokes
3511 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3512 */
3513static void srpt_close_session(struct se_session *se_sess)
3514{
3515 DECLARE_COMPLETION_ONSTACK(release_done);
3516 struct srpt_rdma_ch *ch;
3517 struct srpt_device *sdev;
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003518 unsigned long res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003519
3520 ch = se_sess->fabric_sess_ptr;
3521 WARN_ON(ch->sess != se_sess);
3522
3523 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3524
3525 sdev = ch->sport->sdev;
3526 spin_lock_irq(&sdev->spinlock);
3527 BUG_ON(ch->release_done);
3528 ch->release_done = &release_done;
3529 __srpt_close_ch(ch);
3530 spin_unlock_irq(&sdev->spinlock);
3531
3532 res = wait_for_completion_timeout(&release_done, 60 * HZ);
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003533 WARN_ON(res == 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003534}
3535
3536/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003537 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3538 *
3539 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3540 * This object represents an arbitrary integer used to uniquely identify a
3541 * particular attached remote initiator port to a particular SCSI target port
3542 * within a particular SCSI target device within a particular SCSI instance.
3543 */
3544static u32 srpt_sess_get_index(struct se_session *se_sess)
3545{
3546 return 0;
3547}
3548
3549static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3550{
3551}
3552
3553static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
3554{
3555 struct srpt_send_ioctx *ioctx;
3556
3557 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3558 return ioctx->tag;
3559}
3560
3561/* Note: only used from inside debug printk's by the TCM core. */
3562static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3563{
3564 struct srpt_send_ioctx *ioctx;
3565
3566 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3567 return srpt_get_cmd_state(ioctx);
3568}
3569
Bart Van Asschea42d9852011-10-14 01:30:46 +00003570/**
3571 * srpt_parse_i_port_id() - Parse an initiator port ID.
3572 * @name: ASCII representation of a 128-bit initiator port ID.
3573 * @i_port_id: Binary 128-bit port ID.
3574 */
3575static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3576{
3577 const char *p;
3578 unsigned len, count, leading_zero_bytes;
3579 int ret, rc;
3580
3581 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003582 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003583 p += 2;
3584 ret = -EINVAL;
3585 len = strlen(p);
3586 if (len % 2)
3587 goto out;
3588 count = min(len / 2, 16U);
3589 leading_zero_bytes = 16 - count;
3590 memset(i_port_id, 0, leading_zero_bytes);
3591 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3592 if (rc < 0)
3593 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3594 ret = 0;
3595out:
3596 return ret;
3597}
3598
3599/*
3600 * configfs callback function invoked for
3601 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3602 */
3603static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
3604 struct config_group *group,
3605 const char *name)
3606{
3607 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3608 struct se_node_acl *se_nacl, *se_nacl_new;
3609 struct srpt_node_acl *nacl;
3610 int ret = 0;
3611 u32 nexus_depth = 1;
3612 u8 i_port_id[16];
3613
3614 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003615 pr_err("invalid initiator port ID %s\n", name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003616 ret = -EINVAL;
3617 goto err;
3618 }
3619
3620 se_nacl_new = srpt_alloc_fabric_acl(tpg);
3621 if (!se_nacl_new) {
3622 ret = -ENOMEM;
3623 goto err;
3624 }
3625 /*
3626 * nacl_new may be released by core_tpg_add_initiator_node_acl()
3627 * when converting a node ACL from demo mode to explict
3628 */
3629 se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
3630 nexus_depth);
3631 if (IS_ERR(se_nacl)) {
3632 ret = PTR_ERR(se_nacl);
3633 goto err;
3634 }
3635 /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
3636 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3637 memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3638 nacl->sport = sport;
3639
3640 spin_lock_irq(&sport->port_acl_lock);
3641 list_add_tail(&nacl->list, &sport->port_acl_list);
3642 spin_unlock_irq(&sport->port_acl_lock);
3643
3644 return se_nacl;
3645err:
3646 return ERR_PTR(ret);
3647}
3648
3649/*
3650 * configfs callback function invoked for
3651 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3652 */
3653static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
3654{
3655 struct srpt_node_acl *nacl;
3656 struct srpt_device *sdev;
3657 struct srpt_port *sport;
3658
3659 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3660 sport = nacl->sport;
3661 sdev = sport->sdev;
3662 spin_lock_irq(&sport->port_acl_lock);
3663 list_del(&nacl->list);
3664 spin_unlock_irq(&sport->port_acl_lock);
3665 core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
3666 srpt_release_fabric_acl(NULL, se_nacl);
3667}
3668
3669static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
3670 struct se_portal_group *se_tpg,
3671 char *page)
3672{
3673 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3674
3675 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3676}
3677
3678static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
3679 struct se_portal_group *se_tpg,
3680 const char *page,
3681 size_t count)
3682{
3683 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3684 unsigned long val;
3685 int ret;
3686
Jingoo Han9d8abf42014-02-05 11:22:05 +09003687 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003688 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003689 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003690 return -EINVAL;
3691 }
3692 if (val > MAX_SRPT_RDMA_SIZE) {
3693 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3694 MAX_SRPT_RDMA_SIZE);
3695 return -EINVAL;
3696 }
3697 if (val < DEFAULT_MAX_RDMA_SIZE) {
3698 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3699 val, DEFAULT_MAX_RDMA_SIZE);
3700 return -EINVAL;
3701 }
3702 sport->port_attrib.srp_max_rdma_size = val;
3703
3704 return count;
3705}
3706
3707TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
3708
3709static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
3710 struct se_portal_group *se_tpg,
3711 char *page)
3712{
3713 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3714
3715 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3716}
3717
3718static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
3719 struct se_portal_group *se_tpg,
3720 const char *page,
3721 size_t count)
3722{
3723 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3724 unsigned long val;
3725 int ret;
3726
Jingoo Han9d8abf42014-02-05 11:22:05 +09003727 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003728 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003729 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003730 return -EINVAL;
3731 }
3732 if (val > MAX_SRPT_RSP_SIZE) {
3733 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3734 MAX_SRPT_RSP_SIZE);
3735 return -EINVAL;
3736 }
3737 if (val < MIN_MAX_RSP_SIZE) {
3738 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3739 MIN_MAX_RSP_SIZE);
3740 return -EINVAL;
3741 }
3742 sport->port_attrib.srp_max_rsp_size = val;
3743
3744 return count;
3745}
3746
3747TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
3748
3749static ssize_t srpt_tpg_attrib_show_srp_sq_size(
3750 struct se_portal_group *se_tpg,
3751 char *page)
3752{
3753 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3754
3755 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3756}
3757
3758static ssize_t srpt_tpg_attrib_store_srp_sq_size(
3759 struct se_portal_group *se_tpg,
3760 const char *page,
3761 size_t count)
3762{
3763 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3764 unsigned long val;
3765 int ret;
3766
Jingoo Han9d8abf42014-02-05 11:22:05 +09003767 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003768 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003769 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003770 return -EINVAL;
3771 }
3772 if (val > MAX_SRPT_SRQ_SIZE) {
3773 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3774 MAX_SRPT_SRQ_SIZE);
3775 return -EINVAL;
3776 }
3777 if (val < MIN_SRPT_SRQ_SIZE) {
3778 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3779 MIN_SRPT_SRQ_SIZE);
3780 return -EINVAL;
3781 }
3782 sport->port_attrib.srp_sq_size = val;
3783
3784 return count;
3785}
3786
3787TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
3788
3789static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3790 &srpt_tpg_attrib_srp_max_rdma_size.attr,
3791 &srpt_tpg_attrib_srp_max_rsp_size.attr,
3792 &srpt_tpg_attrib_srp_sq_size.attr,
3793 NULL,
3794};
3795
3796static ssize_t srpt_tpg_show_enable(
3797 struct se_portal_group *se_tpg,
3798 char *page)
3799{
3800 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3801
3802 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3803}
3804
3805static ssize_t srpt_tpg_store_enable(
3806 struct se_portal_group *se_tpg,
3807 const char *page,
3808 size_t count)
3809{
3810 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3811 unsigned long tmp;
3812 int ret;
3813
Jingoo Han9d8abf42014-02-05 11:22:05 +09003814 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003815 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003816 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003817 return -EINVAL;
3818 }
3819
3820 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003821 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003822 return -EINVAL;
3823 }
3824 if (tmp == 1)
3825 sport->enabled = true;
3826 else
3827 sport->enabled = false;
3828
3829 return count;
3830}
3831
3832TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
3833
3834static struct configfs_attribute *srpt_tpg_attrs[] = {
3835 &srpt_tpg_enable.attr,
3836 NULL,
3837};
3838
3839/**
3840 * configfs callback invoked for
3841 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3842 */
3843static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3844 struct config_group *group,
3845 const char *name)
3846{
3847 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3848 int res;
3849
3850 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003851 res = core_tpg_register(&srpt_template, &sport->port_wwn,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003852 &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
3853 if (res)
3854 return ERR_PTR(res);
3855
3856 return &sport->port_tpg_1;
3857}
3858
3859/**
3860 * configfs callback invoked for
3861 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3862 */
3863static void srpt_drop_tpg(struct se_portal_group *tpg)
3864{
3865 struct srpt_port *sport = container_of(tpg,
3866 struct srpt_port, port_tpg_1);
3867
3868 sport->enabled = false;
3869 core_tpg_deregister(&sport->port_tpg_1);
3870}
3871
3872/**
3873 * configfs callback invoked for
3874 * mkdir /sys/kernel/config/target/$driver/$port
3875 */
3876static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3877 struct config_group *group,
3878 const char *name)
3879{
3880 struct srpt_port *sport;
3881 int ret;
3882
3883 sport = srpt_lookup_port(name);
3884 pr_debug("make_tport(%s)\n", name);
3885 ret = -EINVAL;
3886 if (!sport)
3887 goto err;
3888
3889 return &sport->port_wwn;
3890
3891err:
3892 return ERR_PTR(ret);
3893}
3894
3895/**
3896 * configfs callback invoked for
3897 * rmdir /sys/kernel/config/target/$driver/$port
3898 */
3899static void srpt_drop_tport(struct se_wwn *wwn)
3900{
3901 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3902
3903 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3904}
3905
3906static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
3907 char *buf)
3908{
3909 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3910}
3911
3912TF_WWN_ATTR_RO(srpt, version);
3913
3914static struct configfs_attribute *srpt_wwn_attrs[] = {
3915 &srpt_wwn_version.attr,
3916 NULL,
3917};
3918
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003919static const struct target_core_fabric_ops srpt_template = {
3920 .module = THIS_MODULE,
3921 .name = "srpt",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003922 .get_fabric_name = srpt_get_fabric_name,
3923 .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
3924 .tpg_get_wwn = srpt_get_fabric_wwn,
3925 .tpg_get_tag = srpt_get_tag,
3926 .tpg_get_default_depth = srpt_get_default_depth,
3927 .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
3928 .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
3929 .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
3930 .tpg_check_demo_mode = srpt_check_false,
3931 .tpg_check_demo_mode_cache = srpt_check_true,
3932 .tpg_check_demo_mode_write_protect = srpt_check_true,
3933 .tpg_check_prod_mode_write_protect = srpt_check_false,
3934 .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
3935 .tpg_release_fabric_acl = srpt_release_fabric_acl,
3936 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3937 .release_cmd = srpt_release_cmd,
3938 .check_stop_free = srpt_check_stop_free,
3939 .shutdown_session = srpt_shutdown_session,
3940 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003941 .sess_get_index = srpt_sess_get_index,
3942 .sess_get_initiator_sid = NULL,
3943 .write_pending = srpt_write_pending,
3944 .write_pending_status = srpt_write_pending_status,
3945 .set_default_node_attributes = srpt_set_default_node_attrs,
3946 .get_task_tag = srpt_get_task_tag,
3947 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003948 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003949 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003950 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003951 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003952 /*
3953 * Setup function pointers for generic logic in
3954 * target_core_fabric_configfs.c
3955 */
3956 .fabric_make_wwn = srpt_make_tport,
3957 .fabric_drop_wwn = srpt_drop_tport,
3958 .fabric_make_tpg = srpt_make_tpg,
3959 .fabric_drop_tpg = srpt_drop_tpg,
3960 .fabric_post_link = NULL,
3961 .fabric_pre_unlink = NULL,
3962 .fabric_make_np = NULL,
3963 .fabric_drop_np = NULL,
3964 .fabric_make_nodeacl = srpt_make_nodeacl,
3965 .fabric_drop_nodeacl = srpt_drop_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003966
3967 .tfc_wwn_attrs = srpt_wwn_attrs,
3968 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3969 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003970};
3971
3972/**
3973 * srpt_init_module() - Kernel module initialization.
3974 *
3975 * Note: Since ib_register_client() registers callback functions, and since at
3976 * least one of these callback functions (srpt_add_one()) calls target core
3977 * functions, this driver must be registered with the target core before
3978 * ib_register_client() is called.
3979 */
3980static int __init srpt_init_module(void)
3981{
3982 int ret;
3983
3984 ret = -EINVAL;
3985 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003986 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003987 " srp_max_req_size -- must be at least %d.\n",
3988 srp_max_req_size, MIN_MAX_REQ_SIZE);
3989 goto out;
3990 }
3991
3992 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3993 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003994 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003995 " srpt_srq_size -- must be in the range [%d..%d].\n",
3996 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3997 goto out;
3998 }
3999
Christoph Hellwig9ac89282015-04-08 20:01:35 +02004000 ret = target_register_template(&srpt_template);
4001 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00004002 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00004003
4004 ret = ib_register_client(&srpt_client);
4005 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04004006 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00004007 goto out_unregister_target;
4008 }
4009
4010 return 0;
4011
4012out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02004013 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00004014out:
4015 return ret;
4016}
4017
4018static void __exit srpt_cleanup_module(void)
4019{
4020 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02004021 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00004022}
4023
4024module_init(srpt_init_module);
4025module_exit(srpt_cleanup_module);