blob: 72a671ed0b46f31cad39aaa4c02b949cb34e6133 [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>
Bart Van Asscheba929992015-05-08 10:11:12 +020044#include <scsi/scsi_proto.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000045#include <scsi/scsi_tcq.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000046#include <target/target_core_base.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000047#include <target/target_core_fabric.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000048#include "ib_srpt.h"
49
50/* Name of this kernel module. */
51#define DRV_NAME "ib_srpt"
52#define DRV_VERSION "2.0.0"
53#define DRV_RELDATE "2011-02-14"
54
55#define SRPT_ID_STRING "Linux SRP target"
56
57#undef pr_fmt
58#define pr_fmt(fmt) DRV_NAME " " fmt
59
60MODULE_AUTHOR("Vu Pham and Bart Van Assche");
61MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63MODULE_LICENSE("Dual BSD/GPL");
64
65/*
66 * Global Variables
67 */
68
69static u64 srpt_service_guid;
Roland Dreier486d8b92012-02-02 12:55:58 -080070static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
71static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
Bart Van Asschea42d9852011-10-14 01:30:46 +000072
73static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
74module_param(srp_max_req_size, int, 0444);
75MODULE_PARM_DESC(srp_max_req_size,
76 "Maximum size of SRP request messages in bytes.");
77
78static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
79module_param(srpt_srq_size, int, 0444);
80MODULE_PARM_DESC(srpt_srq_size,
81 "Shared receive queue (SRQ) size.");
82
83static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
84{
85 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
86}
87module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
88 0444);
89MODULE_PARM_DESC(srpt_service_guid,
90 "Using this value for ioc_guid, id_ext, and cm_listen_id"
91 " instead of using the node_guid of the first HCA.");
92
93static struct ib_client srpt_client;
Bart Van Asschea42d9852011-10-14 01:30:46 +000094static void srpt_release_channel(struct srpt_rdma_ch *ch);
95static int srpt_queue_status(struct se_cmd *cmd);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +020096static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
97static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
Bart Van Asschea42d9852011-10-14 01:30:46 +000098
99/**
100 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
101 */
102static inline
103enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
104{
105 switch (dir) {
106 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
107 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
108 default: return dir;
109 }
110}
111
112/**
113 * srpt_sdev_name() - Return the name associated with the HCA.
114 *
115 * Examples are ib0, ib1, ...
116 */
117static inline const char *srpt_sdev_name(struct srpt_device *sdev)
118{
119 return sdev->device->name;
120}
121
122static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
123{
124 unsigned long flags;
125 enum rdma_ch_state state;
126
127 spin_lock_irqsave(&ch->spinlock, flags);
128 state = ch->state;
129 spin_unlock_irqrestore(&ch->spinlock, flags);
130 return state;
131}
132
133static enum rdma_ch_state
134srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
135{
136 unsigned long flags;
137 enum rdma_ch_state prev;
138
139 spin_lock_irqsave(&ch->spinlock, flags);
140 prev = ch->state;
141 ch->state = new_state;
142 spin_unlock_irqrestore(&ch->spinlock, flags);
143 return prev;
144}
145
146/**
147 * srpt_test_and_set_ch_state() - Test and set the channel state.
148 *
149 * Returns true if and only if the channel state has been set to the new state.
150 */
151static bool
152srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
153 enum rdma_ch_state new)
154{
155 unsigned long flags;
156 enum rdma_ch_state prev;
157
158 spin_lock_irqsave(&ch->spinlock, flags);
159 prev = ch->state;
160 if (prev == old)
161 ch->state = new;
162 spin_unlock_irqrestore(&ch->spinlock, flags);
163 return prev == old;
164}
165
166/**
167 * srpt_event_handler() - Asynchronous IB event callback function.
168 *
169 * Callback function called by the InfiniBand core when an asynchronous IB
170 * event occurs. This callback may occur in interrupt context. See also
171 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
172 * Architecture Specification.
173 */
174static void srpt_event_handler(struct ib_event_handler *handler,
175 struct ib_event *event)
176{
177 struct srpt_device *sdev;
178 struct srpt_port *sport;
179
180 sdev = ib_get_client_data(event->device, &srpt_client);
181 if (!sdev || sdev->device != event->device)
182 return;
183
184 pr_debug("ASYNC event= %d on device= %s\n", event->event,
185 srpt_sdev_name(sdev));
186
187 switch (event->event) {
188 case IB_EVENT_PORT_ERR:
189 if (event->element.port_num <= sdev->device->phys_port_cnt) {
190 sport = &sdev->port[event->element.port_num - 1];
191 sport->lid = 0;
192 sport->sm_lid = 0;
193 }
194 break;
195 case IB_EVENT_PORT_ACTIVE:
196 case IB_EVENT_LID_CHANGE:
197 case IB_EVENT_PKEY_CHANGE:
198 case IB_EVENT_SM_CHANGE:
199 case IB_EVENT_CLIENT_REREGISTER:
Doug Ledford2aa1cf62014-08-12 19:20:10 -0400200 case IB_EVENT_GID_CHANGE:
Bart Van Asschea42d9852011-10-14 01:30:46 +0000201 /* Refresh port data asynchronously. */
202 if (event->element.port_num <= sdev->device->phys_port_cnt) {
203 sport = &sdev->port[event->element.port_num - 1];
204 if (!sport->lid && !sport->sm_lid)
205 schedule_work(&sport->work);
206 }
207 break;
208 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400209 pr_err("received unrecognized IB event %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +0000210 event->event);
211 break;
212 }
213}
214
215/**
216 * srpt_srq_event() - SRQ event callback function.
217 */
218static void srpt_srq_event(struct ib_event *event, void *ctx)
219{
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400220 pr_info("SRQ event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000221}
222
223/**
224 * srpt_qp_event() - QP event callback function.
225 */
226static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
227{
228 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
229 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
230
231 switch (event->event) {
232 case IB_EVENT_COMM_EST:
233 ib_cm_notify(ch->cm_id, event->event);
234 break;
235 case IB_EVENT_QP_LAST_WQE_REACHED:
236 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
237 CH_RELEASING))
238 srpt_release_channel(ch);
239 else
240 pr_debug("%s: state %d - ignored LAST_WQE.\n",
241 ch->sess_name, srpt_get_ch_state(ch));
242 break;
243 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400244 pr_err("received unrecognized IB QP event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000245 break;
246 }
247}
248
249/**
250 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
251 *
252 * @slot: one-based slot number.
253 * @value: four-bit value.
254 *
255 * Copies the lowest four bits of value in element slot of the array of four
256 * bit elements called c_list (controller list). The index slot is one-based.
257 */
258static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
259{
260 u16 id;
261 u8 tmp;
262
263 id = (slot - 1) / 2;
264 if (slot & 0x1) {
265 tmp = c_list[id] & 0xf;
266 c_list[id] = (value << 4) | tmp;
267 } else {
268 tmp = c_list[id] & 0xf0;
269 c_list[id] = (value & 0xf) | tmp;
270 }
271}
272
273/**
274 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
275 *
276 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
277 * Specification.
278 */
279static void srpt_get_class_port_info(struct ib_dm_mad *mad)
280{
281 struct ib_class_port_info *cif;
282
283 cif = (struct ib_class_port_info *)mad->data;
284 memset(cif, 0, sizeof *cif);
285 cif->base_version = 1;
286 cif->class_version = 1;
287 cif->resp_time_value = 20;
288
289 mad->mad_hdr.status = 0;
290}
291
292/**
293 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
294 *
295 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
296 * Specification. See also section B.7, table B.6 in the SRP r16a document.
297 */
298static void srpt_get_iou(struct ib_dm_mad *mad)
299{
300 struct ib_dm_iou_info *ioui;
301 u8 slot;
302 int i;
303
304 ioui = (struct ib_dm_iou_info *)mad->data;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530305 ioui->change_id = cpu_to_be16(1);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000306 ioui->max_controllers = 16;
307
308 /* set present for slot 1 and empty for the rest */
309 srpt_set_ioc(ioui->controller_list, 1, 1);
310 for (i = 1, slot = 2; i < 16; i++, slot++)
311 srpt_set_ioc(ioui->controller_list, slot, 0);
312
313 mad->mad_hdr.status = 0;
314}
315
316/**
317 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
318 *
319 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
320 * Architecture Specification. See also section B.7, table B.7 in the SRP
321 * r16a document.
322 */
323static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
324 struct ib_dm_mad *mad)
325{
326 struct srpt_device *sdev = sport->sdev;
327 struct ib_dm_ioc_profile *iocp;
328
329 iocp = (struct ib_dm_ioc_profile *)mad->data;
330
331 if (!slot || slot > 16) {
332 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530333 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000334 return;
335 }
336
337 if (slot > 2) {
338 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530339 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000340 return;
341 }
342
343 memset(iocp, 0, sizeof *iocp);
344 strcpy(iocp->id_string, SRPT_ID_STRING);
345 iocp->guid = cpu_to_be64(srpt_service_guid);
Or Gerlitz4a061b22015-12-18 10:59:46 +0200346 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
347 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
348 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
349 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000350 iocp->subsys_device_id = 0x0;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530351 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
352 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
353 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
354 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000355 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
356 iocp->rdma_read_depth = 4;
357 iocp->send_size = cpu_to_be32(srp_max_req_size);
358 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
359 1U << 24));
360 iocp->num_svc_entries = 1;
361 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
362 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
363
364 mad->mad_hdr.status = 0;
365}
366
367/**
368 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
369 *
370 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
371 * Specification. See also section B.7, table B.8 in the SRP r16a document.
372 */
373static void srpt_get_svc_entries(u64 ioc_guid,
374 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
375{
376 struct ib_dm_svc_entries *svc_entries;
377
378 WARN_ON(!ioc_guid);
379
380 if (!slot || slot > 16) {
381 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530382 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000383 return;
384 }
385
386 if (slot > 2 || lo > hi || hi > 1) {
387 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530388 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000389 return;
390 }
391
392 svc_entries = (struct ib_dm_svc_entries *)mad->data;
393 memset(svc_entries, 0, sizeof *svc_entries);
394 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
395 snprintf(svc_entries->service_entries[0].name,
396 sizeof(svc_entries->service_entries[0].name),
397 "%s%016llx",
398 SRP_SERVICE_NAME_PREFIX,
399 ioc_guid);
400
401 mad->mad_hdr.status = 0;
402}
403
404/**
405 * srpt_mgmt_method_get() - Process a received management datagram.
406 * @sp: source port through which the MAD has been received.
407 * @rq_mad: received MAD.
408 * @rsp_mad: response MAD.
409 */
410static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
411 struct ib_dm_mad *rsp_mad)
412{
413 u16 attr_id;
414 u32 slot;
415 u8 hi, lo;
416
417 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
418 switch (attr_id) {
419 case DM_ATTR_CLASS_PORT_INFO:
420 srpt_get_class_port_info(rsp_mad);
421 break;
422 case DM_ATTR_IOU_INFO:
423 srpt_get_iou(rsp_mad);
424 break;
425 case DM_ATTR_IOC_PROFILE:
426 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
427 srpt_get_ioc(sp, slot, rsp_mad);
428 break;
429 case DM_ATTR_SVC_ENTRIES:
430 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
431 hi = (u8) ((slot >> 8) & 0xff);
432 lo = (u8) (slot & 0xff);
433 slot = (u16) ((slot >> 16) & 0xffff);
434 srpt_get_svc_entries(srpt_service_guid,
435 slot, hi, lo, rsp_mad);
436 break;
437 default:
438 rsp_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530439 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000440 break;
441 }
442}
443
444/**
445 * srpt_mad_send_handler() - Post MAD-send callback function.
446 */
447static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
448 struct ib_mad_send_wc *mad_wc)
449{
450 ib_destroy_ah(mad_wc->send_buf->ah);
451 ib_free_send_mad(mad_wc->send_buf);
452}
453
454/**
455 * srpt_mad_recv_handler() - MAD reception callback function.
456 */
457static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
Christoph Hellwigca281262016-01-04 14:15:58 +0100458 struct ib_mad_send_buf *send_buf,
Bart Van Asschea42d9852011-10-14 01:30:46 +0000459 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 =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530497 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000498 break;
499 default:
500 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530501 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000502 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
Matan Barak55ee3ab2015-10-15 18:38:45 +0300550 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
551 NULL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000552 if (ret)
553 goto err_query_port;
554
555 if (!sport->mad_agent) {
556 memset(&reg_req, 0, sizeof reg_req);
557 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
558 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
559 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
560 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
561
562 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
563 sport->port,
564 IB_QPT_GSI,
565 &reg_req, 0,
566 srpt_mad_send_handler,
567 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400568 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000569 if (IS_ERR(sport->mad_agent)) {
570 ret = PTR_ERR(sport->mad_agent);
571 sport->mad_agent = NULL;
572 goto err_query_port;
573 }
574 }
575
576 return 0;
577
578err_query_port:
579
580 port_modify.set_port_cap_mask = 0;
581 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
582 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
583
584err_mod_port:
585
586 return ret;
587}
588
589/**
590 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
591 *
592 * Note: It is safe to call this function more than once for the same device.
593 */
594static void srpt_unregister_mad_agent(struct srpt_device *sdev)
595{
596 struct ib_port_modify port_modify = {
597 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
598 };
599 struct srpt_port *sport;
600 int i;
601
602 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
603 sport = &sdev->port[i - 1];
604 WARN_ON(sport->port != i);
605 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400606 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000607 if (sport->mad_agent) {
608 ib_unregister_mad_agent(sport->mad_agent);
609 sport->mad_agent = NULL;
610 }
611 }
612}
613
614/**
615 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
616 */
617static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
618 int ioctx_size, int dma_size,
619 enum dma_data_direction dir)
620{
621 struct srpt_ioctx *ioctx;
622
623 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
624 if (!ioctx)
625 goto err;
626
627 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
628 if (!ioctx->buf)
629 goto err_free_ioctx;
630
631 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
632 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
633 goto err_free_buf;
634
635 return ioctx;
636
637err_free_buf:
638 kfree(ioctx->buf);
639err_free_ioctx:
640 kfree(ioctx);
641err:
642 return NULL;
643}
644
645/**
646 * srpt_free_ioctx() - Free an SRPT I/O context structure.
647 */
648static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
649 int dma_size, enum dma_data_direction dir)
650{
651 if (!ioctx)
652 return;
653
654 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
655 kfree(ioctx->buf);
656 kfree(ioctx);
657}
658
659/**
660 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
661 * @sdev: Device to allocate the I/O context ring for.
662 * @ring_size: Number of elements in the I/O context ring.
663 * @ioctx_size: I/O context size.
664 * @dma_size: DMA buffer size.
665 * @dir: DMA data direction.
666 */
667static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
668 int ring_size, int ioctx_size,
669 int dma_size, enum dma_data_direction dir)
670{
671 struct srpt_ioctx **ring;
672 int i;
673
674 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
675 && ioctx_size != sizeof(struct srpt_send_ioctx));
676
677 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
678 if (!ring)
679 goto out;
680 for (i = 0; i < ring_size; ++i) {
681 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
682 if (!ring[i])
683 goto err;
684 ring[i]->index = i;
685 }
686 goto out;
687
688err:
689 while (--i >= 0)
690 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
691 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100692 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000693out:
694 return ring;
695}
696
697/**
698 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
699 */
700static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
701 struct srpt_device *sdev, int ring_size,
702 int dma_size, enum dma_data_direction dir)
703{
704 int i;
705
706 for (i = 0; i < ring_size; ++i)
707 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
708 kfree(ioctx_ring);
709}
710
711/**
712 * srpt_get_cmd_state() - Get the state of a SCSI command.
713 */
714static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
715{
716 enum srpt_command_state state;
717 unsigned long flags;
718
719 BUG_ON(!ioctx);
720
721 spin_lock_irqsave(&ioctx->spinlock, flags);
722 state = ioctx->state;
723 spin_unlock_irqrestore(&ioctx->spinlock, flags);
724 return state;
725}
726
727/**
728 * srpt_set_cmd_state() - Set the state of a SCSI command.
729 *
730 * Does not modify the state of aborted commands. Returns the previous command
731 * state.
732 */
733static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
734 enum srpt_command_state new)
735{
736 enum srpt_command_state previous;
737 unsigned long flags;
738
739 BUG_ON(!ioctx);
740
741 spin_lock_irqsave(&ioctx->spinlock, flags);
742 previous = ioctx->state;
743 if (previous != SRPT_STATE_DONE)
744 ioctx->state = new;
745 spin_unlock_irqrestore(&ioctx->spinlock, flags);
746
747 return previous;
748}
749
750/**
751 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
752 *
753 * Returns true if and only if the previous command state was equal to 'old'.
754 */
755static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
756 enum srpt_command_state old,
757 enum srpt_command_state new)
758{
759 enum srpt_command_state previous;
760 unsigned long flags;
761
762 WARN_ON(!ioctx);
763 WARN_ON(old == SRPT_STATE_DONE);
764 WARN_ON(new == SRPT_STATE_NEW);
765
766 spin_lock_irqsave(&ioctx->spinlock, flags);
767 previous = ioctx->state;
768 if (previous == old)
769 ioctx->state = new;
770 spin_unlock_irqrestore(&ioctx->spinlock, flags);
771 return previous == old;
772}
773
774/**
775 * srpt_post_recv() - Post an IB receive request.
776 */
777static int srpt_post_recv(struct srpt_device *sdev,
778 struct srpt_recv_ioctx *ioctx)
779{
780 struct ib_sge list;
781 struct ib_recv_wr wr, *bad_wr;
782
783 BUG_ON(!sdev);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000784 list.addr = ioctx->ioctx.dma;
785 list.length = srp_max_req_size;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600786 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000787
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200788 ioctx->ioctx.cqe.done = srpt_recv_done;
789 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000790 wr.next = NULL;
791 wr.sg_list = &list;
792 wr.num_sge = 1;
793
794 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
795}
796
797/**
798 * srpt_post_send() - Post an IB send request.
799 *
800 * Returns zero upon success and a non-zero value upon failure.
801 */
802static int srpt_post_send(struct srpt_rdma_ch *ch,
803 struct srpt_send_ioctx *ioctx, int len)
804{
805 struct ib_sge list;
806 struct ib_send_wr wr, *bad_wr;
807 struct srpt_device *sdev = ch->sport->sdev;
808 int ret;
809
810 atomic_inc(&ch->req_lim);
811
812 ret = -ENOMEM;
813 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400814 pr_warn("IB send queue full (needed 1)\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000815 goto out;
816 }
817
818 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
819 DMA_TO_DEVICE);
820
821 list.addr = ioctx->ioctx.dma;
822 list.length = len;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600823 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000824
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200825 ioctx->ioctx.cqe.done = srpt_send_done;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000826 wr.next = NULL;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200827 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000828 wr.sg_list = &list;
829 wr.num_sge = 1;
830 wr.opcode = IB_WR_SEND;
831 wr.send_flags = IB_SEND_SIGNALED;
832
833 ret = ib_post_send(ch->qp, &wr, &bad_wr);
834
835out:
836 if (ret < 0) {
837 atomic_inc(&ch->sq_wr_avail);
838 atomic_dec(&ch->req_lim);
839 }
840 return ret;
841}
842
843/**
844 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
845 * @ioctx: Pointer to the I/O context associated with the request.
846 * @srp_cmd: Pointer to the SRP_CMD request data.
847 * @dir: Pointer to the variable to which the transfer direction will be
848 * written.
849 * @data_len: Pointer to the variable to which the total data length of all
850 * descriptors in the SRP_CMD request will be written.
851 *
852 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
853 *
854 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
855 * -ENOMEM when memory allocation fails and zero upon success.
856 */
857static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
858 struct srp_cmd *srp_cmd,
859 enum dma_data_direction *dir, u64 *data_len)
860{
861 struct srp_indirect_buf *idb;
862 struct srp_direct_buf *db;
863 unsigned add_cdb_offset;
864 int ret;
865
866 /*
867 * The pointer computations below will only be compiled correctly
868 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
869 * whether srp_cmd::add_data has been declared as a byte pointer.
870 */
871 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
872 && !__same_type(srp_cmd->add_data[0], (u8)0));
873
874 BUG_ON(!dir);
875 BUG_ON(!data_len);
876
877 ret = 0;
878 *data_len = 0;
879
880 /*
881 * The lower four bits of the buffer format field contain the DATA-IN
882 * buffer descriptor format, and the highest four bits contain the
883 * DATA-OUT buffer descriptor format.
884 */
885 *dir = DMA_NONE;
886 if (srp_cmd->buf_fmt & 0xf)
887 /* DATA-IN: transfer data from target to initiator (read). */
888 *dir = DMA_FROM_DEVICE;
889 else if (srp_cmd->buf_fmt >> 4)
890 /* DATA-OUT: transfer data from initiator to target (write). */
891 *dir = DMA_TO_DEVICE;
892
893 /*
894 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
895 * CDB LENGTH' field are reserved and the size in bytes of this field
896 * is four times the value specified in bits 3..7. Hence the "& ~3".
897 */
898 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
899 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
900 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
901 ioctx->n_rbuf = 1;
902 ioctx->rbufs = &ioctx->single_rbuf;
903
904 db = (struct srp_direct_buf *)(srp_cmd->add_data
905 + add_cdb_offset);
906 memcpy(ioctx->rbufs, db, sizeof *db);
907 *data_len = be32_to_cpu(db->len);
908 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
909 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
910 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
911 + add_cdb_offset);
912
913 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
914
915 if (ioctx->n_rbuf >
916 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400917 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000918 " type (%u out + %u in != %u / %zu)\n",
919 srp_cmd->data_out_desc_cnt,
920 srp_cmd->data_in_desc_cnt,
921 be32_to_cpu(idb->table_desc.len),
922 sizeof(*db));
923 ioctx->n_rbuf = 0;
924 ret = -EINVAL;
925 goto out;
926 }
927
928 if (ioctx->n_rbuf == 1)
929 ioctx->rbufs = &ioctx->single_rbuf;
930 else {
931 ioctx->rbufs =
932 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
933 if (!ioctx->rbufs) {
934 ioctx->n_rbuf = 0;
935 ret = -ENOMEM;
936 goto out;
937 }
938 }
939
940 db = idb->desc_list;
941 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
942 *data_len = be32_to_cpu(idb->len);
943 }
944out:
945 return ret;
946}
947
948/**
949 * srpt_init_ch_qp() - Initialize queue pair attributes.
950 *
951 * Initialized the attributes of queue pair 'qp' by allowing local write,
952 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
953 */
954static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
955{
956 struct ib_qp_attr *attr;
957 int ret;
958
959 attr = kzalloc(sizeof *attr, GFP_KERNEL);
960 if (!attr)
961 return -ENOMEM;
962
963 attr->qp_state = IB_QPS_INIT;
964 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
965 IB_ACCESS_REMOTE_WRITE;
966 attr->port_num = ch->sport->port;
967 attr->pkey_index = 0;
968
969 ret = ib_modify_qp(qp, attr,
970 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
971 IB_QP_PKEY_INDEX);
972
973 kfree(attr);
974 return ret;
975}
976
977/**
978 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
979 * @ch: channel of the queue pair.
980 * @qp: queue pair to change the state of.
981 *
982 * Returns zero upon success and a negative value upon failure.
983 *
984 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
985 * If this structure ever becomes larger, it might be necessary to allocate
986 * it dynamically instead of on the stack.
987 */
988static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
989{
990 struct ib_qp_attr qp_attr;
991 int attr_mask;
992 int ret;
993
994 qp_attr.qp_state = IB_QPS_RTR;
995 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
996 if (ret)
997 goto out;
998
999 qp_attr.max_dest_rd_atomic = 4;
1000
1001 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1002
1003out:
1004 return ret;
1005}
1006
1007/**
1008 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1009 * @ch: channel of the queue pair.
1010 * @qp: queue pair to change the state of.
1011 *
1012 * Returns zero upon success and a negative value upon failure.
1013 *
1014 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1015 * If this structure ever becomes larger, it might be necessary to allocate
1016 * it dynamically instead of on the stack.
1017 */
1018static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1019{
1020 struct ib_qp_attr qp_attr;
1021 int attr_mask;
1022 int ret;
1023
1024 qp_attr.qp_state = IB_QPS_RTS;
1025 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1026 if (ret)
1027 goto out;
1028
1029 qp_attr.max_rd_atomic = 4;
1030
1031 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1032
1033out:
1034 return ret;
1035}
1036
1037/**
1038 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1039 */
1040static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1041{
1042 struct ib_qp_attr qp_attr;
1043
1044 qp_attr.qp_state = IB_QPS_ERR;
1045 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1046}
1047
1048/**
1049 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1050 */
1051static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1052 struct srpt_send_ioctx *ioctx)
1053{
1054 struct scatterlist *sg;
1055 enum dma_data_direction dir;
1056
1057 BUG_ON(!ch);
1058 BUG_ON(!ioctx);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001059 BUG_ON(ioctx->n_rdma && !ioctx->rdma_wrs);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001060
1061 while (ioctx->n_rdma)
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001062 kfree(ioctx->rdma_wrs[--ioctx->n_rdma].wr.sg_list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001063
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001064 kfree(ioctx->rdma_wrs);
1065 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001066
1067 if (ioctx->mapped_sg_count) {
1068 sg = ioctx->sg;
1069 WARN_ON(!sg);
1070 dir = ioctx->cmd.data_direction;
1071 BUG_ON(dir == DMA_NONE);
1072 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1073 opposite_dma_dir(dir));
1074 ioctx->mapped_sg_count = 0;
1075 }
1076}
1077
1078/**
1079 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1080 */
1081static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1082 struct srpt_send_ioctx *ioctx)
1083{
Mike Marciniszynb0768082014-04-07 13:58:35 -04001084 struct ib_device *dev = ch->sport->sdev->device;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001085 struct se_cmd *cmd;
1086 struct scatterlist *sg, *sg_orig;
1087 int sg_cnt;
1088 enum dma_data_direction dir;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001089 struct ib_rdma_wr *riu;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001090 struct srp_direct_buf *db;
1091 dma_addr_t dma_addr;
1092 struct ib_sge *sge;
1093 u64 raddr;
1094 u32 rsize;
1095 u32 tsize;
1096 u32 dma_len;
1097 int count, nrdma;
1098 int i, j, k;
1099
1100 BUG_ON(!ch);
1101 BUG_ON(!ioctx);
1102 cmd = &ioctx->cmd;
1103 dir = cmd->data_direction;
1104 BUG_ON(dir == DMA_NONE);
1105
Roland Dreier6f9e7f02012-03-30 11:29:12 -07001106 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1107 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001108
1109 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1110 opposite_dma_dir(dir));
1111 if (unlikely(!count))
1112 return -EAGAIN;
1113
1114 ioctx->mapped_sg_count = count;
1115
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001116 if (ioctx->rdma_wrs && ioctx->n_rdma_wrs)
1117 nrdma = ioctx->n_rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001118 else {
1119 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1120 + ioctx->n_rbuf;
1121
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001122 ioctx->rdma_wrs = kcalloc(nrdma, sizeof(*ioctx->rdma_wrs),
1123 GFP_KERNEL);
1124 if (!ioctx->rdma_wrs)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001125 goto free_mem;
1126
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001127 ioctx->n_rdma_wrs = nrdma;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001128 }
1129
1130 db = ioctx->rbufs;
1131 tsize = cmd->data_length;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001132 dma_len = ib_sg_dma_len(dev, &sg[0]);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001133 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001134
1135 /*
1136 * For each remote desc - calculate the #ib_sge.
1137 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1138 * each remote desc rdma_iu is required a rdma wr;
1139 * else
1140 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1141 * another rdma wr
1142 */
1143 for (i = 0, j = 0;
1144 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1145 rsize = be32_to_cpu(db->len);
1146 raddr = be64_to_cpu(db->va);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001147 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001148 riu->rkey = be32_to_cpu(db->key);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001149 riu->wr.num_sge = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001150
1151 /* calculate how many sge required for this remote_buf */
1152 while (rsize > 0 && tsize > 0) {
1153
1154 if (rsize >= dma_len) {
1155 tsize -= dma_len;
1156 rsize -= dma_len;
1157 raddr += dma_len;
1158
1159 if (tsize > 0) {
1160 ++j;
1161 if (j < count) {
1162 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001163 dma_len = ib_sg_dma_len(
1164 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001165 }
1166 }
1167 } else {
1168 tsize -= rsize;
1169 dma_len -= rsize;
1170 rsize = 0;
1171 }
1172
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001173 ++riu->wr.num_sge;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001174
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001175 if (rsize > 0 &&
1176 riu->wr.num_sge == SRPT_DEF_SG_PER_WQE) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001177 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001178 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1179 sizeof(*riu->wr.sg_list),
1180 GFP_KERNEL);
1181 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001182 goto free_mem;
1183
1184 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001185 riu->wr.num_sge = 0;
1186 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001187 riu->rkey = be32_to_cpu(db->key);
1188 }
1189 }
1190
1191 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001192 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1193 sizeof(*riu->wr.sg_list),
1194 GFP_KERNEL);
1195 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001196 goto free_mem;
1197 }
1198
1199 db = ioctx->rbufs;
1200 tsize = cmd->data_length;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001201 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001202 sg = sg_orig;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001203 dma_len = ib_sg_dma_len(dev, &sg[0]);
1204 dma_addr = ib_sg_dma_address(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001205
1206 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1207 for (i = 0, j = 0;
1208 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1209 rsize = be32_to_cpu(db->len);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001210 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001211 k = 0;
1212
1213 while (rsize > 0 && tsize > 0) {
1214 sge->addr = dma_addr;
Jason Gunthorpe5a783952015-07-30 17:22:24 -06001215 sge->lkey = ch->sport->sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001216
1217 if (rsize >= dma_len) {
1218 sge->length =
1219 (tsize < dma_len) ? tsize : dma_len;
1220 tsize -= dma_len;
1221 rsize -= dma_len;
1222
1223 if (tsize > 0) {
1224 ++j;
1225 if (j < count) {
1226 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001227 dma_len = ib_sg_dma_len(
1228 dev, sg);
1229 dma_addr = ib_sg_dma_address(
1230 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001231 }
1232 }
1233 } else {
1234 sge->length = (tsize < rsize) ? tsize : rsize;
1235 tsize -= rsize;
1236 dma_len -= rsize;
1237 dma_addr += rsize;
1238 rsize = 0;
1239 }
1240
1241 ++k;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001242 if (k == riu->wr.num_sge && rsize > 0 && tsize > 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001243 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001244 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001245 k = 0;
1246 } else if (rsize > 0 && tsize > 0)
1247 ++sge;
1248 }
1249 }
1250
1251 return 0;
1252
1253free_mem:
1254 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1255
1256 return -ENOMEM;
1257}
1258
1259/**
1260 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1261 */
1262static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1263{
1264 struct srpt_send_ioctx *ioctx;
1265 unsigned long flags;
1266
1267 BUG_ON(!ch);
1268
1269 ioctx = NULL;
1270 spin_lock_irqsave(&ch->spinlock, flags);
1271 if (!list_empty(&ch->free_list)) {
1272 ioctx = list_first_entry(&ch->free_list,
1273 struct srpt_send_ioctx, free_list);
1274 list_del(&ioctx->free_list);
1275 }
1276 spin_unlock_irqrestore(&ch->spinlock, flags);
1277
1278 if (!ioctx)
1279 return ioctx;
1280
1281 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001282 spin_lock_init(&ioctx->spinlock);
1283 ioctx->state = SRPT_STATE_NEW;
1284 ioctx->n_rbuf = 0;
1285 ioctx->rbufs = NULL;
1286 ioctx->n_rdma = 0;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001287 ioctx->n_rdma_wrs = 0;
1288 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001289 ioctx->mapped_sg_count = 0;
1290 init_completion(&ioctx->tx_done);
1291 ioctx->queue_status_only = false;
1292 /*
1293 * transport_init_se_cmd() does not initialize all fields, so do it
1294 * here.
1295 */
1296 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1297 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1298
1299 return ioctx;
1300}
1301
1302/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001303 * srpt_abort_cmd() - Abort a SCSI command.
1304 * @ioctx: I/O context associated with the SCSI command.
1305 * @context: Preferred execution context.
1306 */
1307static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1308{
1309 enum srpt_command_state state;
1310 unsigned long flags;
1311
1312 BUG_ON(!ioctx);
1313
1314 /*
1315 * If the command is in a state where the target core is waiting for
1316 * the ib_srpt driver, change the state to the next state. Changing
1317 * the state of the command from SRPT_STATE_NEED_DATA to
1318 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1319 * function a second time.
1320 */
1321
1322 spin_lock_irqsave(&ioctx->spinlock, flags);
1323 state = ioctx->state;
1324 switch (state) {
1325 case SRPT_STATE_NEED_DATA:
1326 ioctx->state = SRPT_STATE_DATA_IN;
1327 break;
1328 case SRPT_STATE_DATA_IN:
1329 case SRPT_STATE_CMD_RSP_SENT:
1330 case SRPT_STATE_MGMT_RSP_SENT:
1331 ioctx->state = SRPT_STATE_DONE;
1332 break;
1333 default:
1334 break;
1335 }
1336 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1337
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001338 if (state == SRPT_STATE_DONE) {
1339 struct srpt_rdma_ch *ch = ioctx->ch;
1340
1341 BUG_ON(ch->sess == NULL);
1342
Bart Van Asscheafc16602015-04-27 13:52:36 +02001343 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001344 goto out;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001345 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001346
1347 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
Bart Van Assche649ee052015-04-14 13:26:44 +02001348 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001349
1350 switch (state) {
1351 case SRPT_STATE_NEW:
1352 case SRPT_STATE_DATA_IN:
1353 case SRPT_STATE_MGMT:
1354 /*
1355 * Do nothing - defer abort processing until
1356 * srpt_queue_response() is invoked.
1357 */
1358 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1359 break;
1360 case SRPT_STATE_NEED_DATA:
1361 /* DMA_TO_DEVICE (write) - RDMA read error. */
Christoph Hellwige672a472012-07-08 15:58:43 -04001362
1363 /* XXX(hch): this is a horrible layering violation.. */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001364 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
Christoph Hellwige672a472012-07-08 15:58:43 -04001365 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001366 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001367 break;
1368 case SRPT_STATE_CMD_RSP_SENT:
1369 /*
1370 * SRP_RSP sending failed or the SRP_RSP send completion has
1371 * not been received in time.
1372 */
1373 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001374 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001375 break;
1376 case SRPT_STATE_MGMT_RSP_SENT:
1377 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001378 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001379 break;
1380 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001381 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001382 break;
1383 }
1384
1385out:
1386 return state;
1387}
1388
1389/**
Christoph Hellwige672a472012-07-08 15:58:43 -04001390 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1391 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001392 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001393 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001394 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001395static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001396{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001397 struct srpt_rdma_ch *ch = cq->cq_context;
1398 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001399 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001400
Bart Van Asschea42d9852011-10-14 01:30:46 +00001401 WARN_ON(ioctx->n_rdma <= 0);
1402 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1403
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001404 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1405 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1406 ioctx, wc->status);
1407 srpt_abort_cmd(ioctx);
1408 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001409 }
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001410
1411 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1412 SRPT_STATE_DATA_IN))
1413 target_execute_cmd(&ioctx->cmd);
1414 else
1415 pr_err("%s[%d]: wrong state = %d\n", __func__,
1416 __LINE__, srpt_get_cmd_state(ioctx));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001417}
1418
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001419static void srpt_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001420{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001421 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001422 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001423
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001424 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1425 pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n",
1426 ioctx, wc->status);
1427 srpt_abort_cmd(ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001428 }
1429}
1430
1431/**
1432 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1433 * @ch: RDMA channel through which the request has been received.
1434 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1435 * be built in the buffer ioctx->buf points at and hence this function will
1436 * overwrite the request data.
1437 * @tag: tag of the request for which this response is being generated.
1438 * @status: value for the STATUS field of the SRP_RSP information unit.
1439 *
1440 * Returns the size in bytes of the SRP_RSP response.
1441 *
1442 * An SRP_RSP response contains a SCSI status or service response. See also
1443 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1444 * response. See also SPC-2 for more information about sense data.
1445 */
1446static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1447 struct srpt_send_ioctx *ioctx, u64 tag,
1448 int status)
1449{
1450 struct srp_rsp *srp_rsp;
1451 const u8 *sense_data;
1452 int sense_data_len, max_sense_len;
1453
1454 /*
1455 * The lowest bit of all SAM-3 status codes is zero (see also
1456 * paragraph 5.3 in SAM-3).
1457 */
1458 WARN_ON(status & 1);
1459
1460 srp_rsp = ioctx->ioctx.buf;
1461 BUG_ON(!srp_rsp);
1462
1463 sense_data = ioctx->sense_data;
1464 sense_data_len = ioctx->cmd.scsi_sense_length;
1465 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1466
1467 memset(srp_rsp, 0, sizeof *srp_rsp);
1468 srp_rsp->opcode = SRP_RSP;
1469 srp_rsp->req_lim_delta =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301470 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001471 srp_rsp->tag = tag;
1472 srp_rsp->status = status;
1473
1474 if (sense_data_len) {
1475 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1476 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1477 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001478 pr_warn("truncated sense data from %d to %d"
1479 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001480 sense_data_len = max_sense_len;
1481 }
1482
1483 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1484 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1485 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1486 }
1487
1488 return sizeof(*srp_rsp) + sense_data_len;
1489}
1490
1491/**
1492 * srpt_build_tskmgmt_rsp() - Build a task management response.
1493 * @ch: RDMA channel through which the request has been received.
1494 * @ioctx: I/O context in which the SRP_RSP response will be built.
1495 * @rsp_code: RSP_CODE that will be stored in the response.
1496 * @tag: Tag of the request for which this response is being generated.
1497 *
1498 * Returns the size in bytes of the SRP_RSP response.
1499 *
1500 * An SRP_RSP response contains a SCSI status or service response. See also
1501 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1502 * response.
1503 */
1504static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1505 struct srpt_send_ioctx *ioctx,
1506 u8 rsp_code, u64 tag)
1507{
1508 struct srp_rsp *srp_rsp;
1509 int resp_data_len;
1510 int resp_len;
1511
Jack Wangc807f642013-09-30 10:09:05 +02001512 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001513 resp_len = sizeof(*srp_rsp) + resp_data_len;
1514
1515 srp_rsp = ioctx->ioctx.buf;
1516 BUG_ON(!srp_rsp);
1517 memset(srp_rsp, 0, sizeof *srp_rsp);
1518
1519 srp_rsp->opcode = SRP_RSP;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301520 srp_rsp->req_lim_delta =
1521 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001522 srp_rsp->tag = tag;
1523
Jack Wangc807f642013-09-30 10:09:05 +02001524 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1525 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1526 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001527
1528 return resp_len;
1529}
1530
1531#define NO_SUCH_LUN ((uint64_t)-1LL)
1532
1533/*
1534 * SCSI LUN addressing method. See also SAM-2 and the section about
1535 * eight byte LUNs.
1536 */
1537enum scsi_lun_addr_method {
1538 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1539 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1540 SCSI_LUN_ADDR_METHOD_LUN = 2,
1541 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1542};
1543
1544/*
1545 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1546 *
1547 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1548 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1549 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1550 */
1551static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1552{
1553 uint64_t res = NO_SUCH_LUN;
1554 int addressing_method;
1555
1556 if (unlikely(len < 2)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001557 pr_err("Illegal LUN length %d, expected 2 bytes or more\n",
1558 len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001559 goto out;
1560 }
1561
1562 switch (len) {
1563 case 8:
1564 if ((*((__be64 *)lun) &
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301565 cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001566 goto out_err;
1567 break;
1568 case 4:
1569 if (*((__be16 *)&lun[2]) != 0)
1570 goto out_err;
1571 break;
1572 case 6:
1573 if (*((__be32 *)&lun[2]) != 0)
1574 goto out_err;
1575 break;
1576 case 2:
1577 break;
1578 default:
1579 goto out_err;
1580 }
1581
1582 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1583 switch (addressing_method) {
1584 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1585 case SCSI_LUN_ADDR_METHOD_FLAT:
1586 case SCSI_LUN_ADDR_METHOD_LUN:
1587 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1588 break;
1589
1590 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1591 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001592 pr_err("Unimplemented LUN addressing method %u\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001593 addressing_method);
1594 break;
1595 }
1596
1597out:
1598 return res;
1599
1600out_err:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001601 pr_err("Support for multi-level LUNs has not yet been implemented\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001602 goto out;
1603}
1604
1605static int srpt_check_stop_free(struct se_cmd *cmd)
1606{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001607 struct srpt_send_ioctx *ioctx = container_of(cmd,
1608 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001609
Bart Van Asscheafc16602015-04-27 13:52:36 +02001610 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001611}
1612
1613/**
1614 * srpt_handle_cmd() - Process SRP_CMD.
1615 */
1616static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1617 struct srpt_recv_ioctx *recv_ioctx,
1618 struct srpt_send_ioctx *send_ioctx)
1619{
1620 struct se_cmd *cmd;
1621 struct srp_cmd *srp_cmd;
1622 uint64_t unpacked_lun;
1623 u64 data_len;
1624 enum dma_data_direction dir;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001625 sense_reason_t ret;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001626 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001627
1628 BUG_ON(!send_ioctx);
1629
1630 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001631 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001632 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001633
1634 switch (srp_cmd->task_attr) {
1635 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001636 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001637 break;
1638 case SRP_CMD_ORDERED_Q:
1639 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001640 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001641 break;
1642 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001643 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001644 break;
1645 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001646 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001647 break;
1648 }
1649
Christoph Hellwigde103c92012-11-06 12:24:09 -08001650 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001651 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001652 srp_cmd->tag);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001653 ret = TCM_INVALID_CDB_FIELD;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001654 goto send_sense;
1655 }
1656
Bart Van Asschea42d9852011-10-14 01:30:46 +00001657 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1658 sizeof(srp_cmd->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001659 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1660 &send_ioctx->sense_data[0], unpacked_lun, data_len,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001661 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001662 if (rc != 0) {
1663 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001664 goto send_sense;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001665 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001666 return 0;
1667
1668send_sense:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001669 transport_send_check_condition_and_sense(cmd, ret, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001670 return -1;
1671}
1672
1673/**
1674 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1675 * @ch: RDMA channel of the task management request.
1676 * @fn: Task management function to perform.
1677 * @req_tag: Tag of the SRP task management request.
1678 * @mgmt_ioctx: I/O context of the task management request.
1679 *
1680 * Returns zero if the target core will process the task management
1681 * request asynchronously.
1682 *
1683 * Note: It is assumed that the initiator serializes tag-based task management
1684 * requests.
1685 */
1686static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1687{
1688 struct srpt_device *sdev;
1689 struct srpt_rdma_ch *ch;
1690 struct srpt_send_ioctx *target;
1691 int ret, i;
1692
1693 ret = -EINVAL;
1694 ch = ioctx->ch;
1695 BUG_ON(!ch);
1696 BUG_ON(!ch->sport);
1697 sdev = ch->sport->sdev;
1698 BUG_ON(!sdev);
1699 spin_lock_irq(&sdev->spinlock);
1700 for (i = 0; i < ch->rq_size; ++i) {
1701 target = ch->ioctx_ring[i];
1702 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
Bart Van Assche649ee052015-04-14 13:26:44 +02001703 target->cmd.tag == tag &&
Bart Van Asschea42d9852011-10-14 01:30:46 +00001704 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1705 ret = 0;
1706 /* now let the target core abort &target->cmd; */
1707 break;
1708 }
1709 }
1710 spin_unlock_irq(&sdev->spinlock);
1711 return ret;
1712}
1713
1714static int srp_tmr_to_tcm(int fn)
1715{
1716 switch (fn) {
1717 case SRP_TSK_ABORT_TASK:
1718 return TMR_ABORT_TASK;
1719 case SRP_TSK_ABORT_TASK_SET:
1720 return TMR_ABORT_TASK_SET;
1721 case SRP_TSK_CLEAR_TASK_SET:
1722 return TMR_CLEAR_TASK_SET;
1723 case SRP_TSK_LUN_RESET:
1724 return TMR_LUN_RESET;
1725 case SRP_TSK_CLEAR_ACA:
1726 return TMR_CLEAR_ACA;
1727 default:
1728 return -1;
1729 }
1730}
1731
1732/**
1733 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1734 *
1735 * Returns 0 if and only if the request will be processed by the target core.
1736 *
1737 * For more information about SRP_TSK_MGMT information units, see also section
1738 * 6.7 in the SRP r16a document.
1739 */
1740static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1741 struct srpt_recv_ioctx *recv_ioctx,
1742 struct srpt_send_ioctx *send_ioctx)
1743{
1744 struct srp_tsk_mgmt *srp_tsk;
1745 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001746 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001747 uint64_t unpacked_lun;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001748 uint32_t tag = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001749 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001750 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001751
1752 BUG_ON(!send_ioctx);
1753
1754 srp_tsk = recv_ioctx->ioctx.buf;
1755 cmd = &send_ioctx->cmd;
1756
1757 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1758 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1759 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1760
1761 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001762 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001763 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1764 if (tcm_tmr < 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001765 send_ioctx->cmd.se_tmr_req->response =
1766 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001767 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001768 }
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001769 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1770 sizeof(srp_tsk->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001771
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001772 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK) {
1773 rc = srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1774 if (rc < 0) {
1775 send_ioctx->cmd.se_tmr_req->response =
1776 TMR_TASK_DOES_NOT_EXIST;
1777 goto fail;
1778 }
1779 tag = srp_tsk->task_tag;
1780 }
1781 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
1782 srp_tsk, tcm_tmr, GFP_KERNEL, tag,
1783 TARGET_SCF_ACK_KREF);
1784 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001785 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001786 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001787 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001788 return;
1789fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001790 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001791}
1792
1793/**
1794 * srpt_handle_new_iu() - Process a newly received information unit.
1795 * @ch: RDMA channel through which the information unit has been received.
1796 * @ioctx: SRPT I/O context associated with the information unit.
1797 */
1798static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1799 struct srpt_recv_ioctx *recv_ioctx,
1800 struct srpt_send_ioctx *send_ioctx)
1801{
1802 struct srp_cmd *srp_cmd;
1803 enum rdma_ch_state ch_state;
1804
1805 BUG_ON(!ch);
1806 BUG_ON(!recv_ioctx);
1807
1808 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1809 recv_ioctx->ioctx.dma, srp_max_req_size,
1810 DMA_FROM_DEVICE);
1811
1812 ch_state = srpt_get_ch_state(ch);
1813 if (unlikely(ch_state == CH_CONNECTING)) {
1814 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1815 goto out;
1816 }
1817
1818 if (unlikely(ch_state != CH_LIVE))
1819 goto out;
1820
1821 srp_cmd = recv_ioctx->ioctx.buf;
1822 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1823 if (!send_ioctx)
1824 send_ioctx = srpt_get_send_ioctx(ch);
1825 if (unlikely(!send_ioctx)) {
1826 list_add_tail(&recv_ioctx->wait_list,
1827 &ch->cmd_wait_list);
1828 goto out;
1829 }
1830 }
1831
Bart Van Asschea42d9852011-10-14 01:30:46 +00001832 switch (srp_cmd->opcode) {
1833 case SRP_CMD:
1834 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1835 break;
1836 case SRP_TSK_MGMT:
1837 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1838 break;
1839 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001840 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001841 break;
1842 case SRP_CRED_RSP:
1843 pr_debug("received SRP_CRED_RSP\n");
1844 break;
1845 case SRP_AER_RSP:
1846 pr_debug("received SRP_AER_RSP\n");
1847 break;
1848 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001849 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001850 break;
1851 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001852 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001853 srp_cmd->opcode);
1854 break;
1855 }
1856
1857 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1858out:
1859 return;
1860}
1861
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001862static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001863{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001864 struct srpt_rdma_ch *ch = cq->cq_context;
1865 struct srpt_recv_ioctx *ioctx =
1866 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001867
Bart Van Asschea42d9852011-10-14 01:30:46 +00001868 if (wc->status == IB_WC_SUCCESS) {
1869 int req_lim;
1870
1871 req_lim = atomic_dec_return(&ch->req_lim);
1872 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001873 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001874 srpt_handle_new_iu(ch, ioctx, NULL);
1875 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001876 pr_info("receiving failed for ioctx %p with status %d\n",
1877 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001878 }
1879}
1880
1881/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001882 * Note: Although this has not yet been observed during tests, at least in
1883 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1884 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1885 * value in each response is set to one, and it is possible that this response
1886 * makes the initiator send a new request before the send completion for that
1887 * response has been processed. This could e.g. happen if the call to
1888 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1889 * if IB retransmission causes generation of the send completion to be
1890 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1891 * are queued on cmd_wait_list. The code below processes these delayed
1892 * requests one at a time.
1893 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001894static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001895{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001896 struct srpt_rdma_ch *ch = cq->cq_context;
1897 struct srpt_send_ioctx *ioctx =
1898 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1899 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001900
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001901 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1902
1903 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1904 state != SRPT_STATE_MGMT_RSP_SENT);
1905
1906 atomic_inc(&ch->sq_wr_avail);
1907
1908 if (wc->status != IB_WC_SUCCESS) {
1909 pr_info("sending response for ioctx 0x%p failed"
1910 " with status %d\n", ioctx, wc->status);
1911
1912 atomic_dec(&ch->req_lim);
1913 srpt_abort_cmd(ioctx);
1914 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001915 }
1916
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001917 if (state != SRPT_STATE_DONE) {
1918 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1919 transport_generic_free_cmd(&ioctx->cmd, 0);
1920 } else {
1921 pr_err("IB completion has been received too late for"
1922 " wr_id = %u.\n", ioctx->ioctx.index);
1923 }
1924
1925out:
1926 while (!list_empty(&ch->cmd_wait_list) &&
1927 srpt_get_ch_state(ch) == CH_LIVE &&
1928 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001929 struct srpt_recv_ioctx *recv_ioctx;
1930
1931 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1932 struct srpt_recv_ioctx,
1933 wait_list);
1934 list_del(&recv_ioctx->wait_list);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001935 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001936 }
1937}
1938
Bart Van Asschea42d9852011-10-14 01:30:46 +00001939/**
1940 * srpt_create_ch_ib() - Create receive and send completion queues.
1941 */
1942static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1943{
1944 struct ib_qp_init_attr *qp_init;
1945 struct srpt_port *sport = ch->sport;
1946 struct srpt_device *sdev = sport->sdev;
1947 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1948 int ret;
1949
1950 WARN_ON(ch->rq_size < 1);
1951
1952 ret = -ENOMEM;
1953 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
1954 if (!qp_init)
1955 goto out;
1956
Bart Van Asscheab477c12014-10-19 18:05:33 +03001957retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001958 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1959 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001960 if (IS_ERR(ch->cq)) {
1961 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001962 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001963 ch->rq_size + srp_sq_size, ret);
1964 goto out;
1965 }
1966
1967 qp_init->qp_context = (void *)ch;
1968 qp_init->event_handler
1969 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1970 qp_init->send_cq = ch->cq;
1971 qp_init->recv_cq = ch->cq;
1972 qp_init->srq = sdev->srq;
1973 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1974 qp_init->qp_type = IB_QPT_RC;
1975 qp_init->cap.max_send_wr = srp_sq_size;
1976 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1977
1978 ch->qp = ib_create_qp(sdev->pd, qp_init);
1979 if (IS_ERR(ch->qp)) {
1980 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001981 if (ret == -ENOMEM) {
1982 srp_sq_size /= 2;
1983 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1984 ib_destroy_cq(ch->cq);
1985 goto retry;
1986 }
1987 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001988 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001989 goto err_destroy_cq;
1990 }
1991
1992 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1993
1994 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1995 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1996 qp_init->cap.max_send_wr, ch->cm_id);
1997
1998 ret = srpt_init_ch_qp(ch, ch->qp);
1999 if (ret)
2000 goto err_destroy_qp;
2001
Bart Van Asschea42d9852011-10-14 01:30:46 +00002002out:
2003 kfree(qp_init);
2004 return ret;
2005
2006err_destroy_qp:
2007 ib_destroy_qp(ch->qp);
2008err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002009 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002010 goto out;
2011}
2012
2013static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2014{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002015 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002016 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002017}
2018
2019/**
2020 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2021 *
2022 * Reset the QP and make sure all resources associated with the channel will
2023 * be deallocated at an appropriate time.
2024 *
2025 * Note: The caller must hold ch->sport->sdev->spinlock.
2026 */
2027static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2028{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002029 enum rdma_ch_state prev_state;
2030 unsigned long flags;
2031
Bart Van Asschea42d9852011-10-14 01:30:46 +00002032 spin_lock_irqsave(&ch->spinlock, flags);
2033 prev_state = ch->state;
2034 switch (prev_state) {
2035 case CH_CONNECTING:
2036 case CH_LIVE:
2037 ch->state = CH_DISCONNECTING;
2038 break;
2039 default:
2040 break;
2041 }
2042 spin_unlock_irqrestore(&ch->spinlock, flags);
2043
2044 switch (prev_state) {
2045 case CH_CONNECTING:
2046 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2047 NULL, 0);
2048 /* fall through */
2049 case CH_LIVE:
2050 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002051 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002052 break;
2053 case CH_DISCONNECTING:
2054 break;
2055 case CH_DRAINING:
2056 case CH_RELEASING:
2057 break;
2058 }
2059}
2060
2061/**
2062 * srpt_close_ch() - Close an RDMA channel.
2063 */
2064static void srpt_close_ch(struct srpt_rdma_ch *ch)
2065{
2066 struct srpt_device *sdev;
2067
2068 sdev = ch->sport->sdev;
2069 spin_lock_irq(&sdev->spinlock);
2070 __srpt_close_ch(ch);
2071 spin_unlock_irq(&sdev->spinlock);
2072}
2073
2074/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002075 * srpt_shutdown_session() - Whether or not a session may be shut down.
2076 */
2077static int srpt_shutdown_session(struct se_session *se_sess)
2078{
2079 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2080 unsigned long flags;
2081
2082 spin_lock_irqsave(&ch->spinlock, flags);
2083 if (ch->in_shutdown) {
2084 spin_unlock_irqrestore(&ch->spinlock, flags);
2085 return true;
2086 }
2087
2088 ch->in_shutdown = true;
2089 target_sess_cmd_list_set_waiting(se_sess);
2090 spin_unlock_irqrestore(&ch->spinlock, flags);
2091
2092 return true;
2093}
2094
2095/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002096 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2097 * @cm_id: Pointer to the CM ID of the channel to be drained.
2098 *
2099 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2100 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2101 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2102 * waits until all target sessions for the associated IB device have been
2103 * unregistered and target session registration involves a call to
2104 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2105 * this function has finished).
2106 */
2107static void srpt_drain_channel(struct ib_cm_id *cm_id)
2108{
2109 struct srpt_device *sdev;
2110 struct srpt_rdma_ch *ch;
2111 int ret;
2112 bool do_reset = false;
2113
2114 WARN_ON_ONCE(irqs_disabled());
2115
2116 sdev = cm_id->context;
2117 BUG_ON(!sdev);
2118 spin_lock_irq(&sdev->spinlock);
2119 list_for_each_entry(ch, &sdev->rch_list, list) {
2120 if (ch->cm_id == cm_id) {
2121 do_reset = srpt_test_and_set_ch_state(ch,
2122 CH_CONNECTING, CH_DRAINING) ||
2123 srpt_test_and_set_ch_state(ch,
2124 CH_LIVE, CH_DRAINING) ||
2125 srpt_test_and_set_ch_state(ch,
2126 CH_DISCONNECTING, CH_DRAINING);
2127 break;
2128 }
2129 }
2130 spin_unlock_irq(&sdev->spinlock);
2131
2132 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002133 if (ch->sess)
2134 srpt_shutdown_session(ch->sess);
2135
Bart Van Asschea42d9852011-10-14 01:30:46 +00002136 ret = srpt_ch_qp_err(ch);
2137 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002138 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002139 " failed: %d\n", ret);
2140 }
2141}
2142
2143/**
2144 * srpt_find_channel() - Look up an RDMA channel.
2145 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2146 *
2147 * Return NULL if no matching RDMA channel has been found.
2148 */
2149static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2150 struct ib_cm_id *cm_id)
2151{
2152 struct srpt_rdma_ch *ch;
2153 bool found;
2154
2155 WARN_ON_ONCE(irqs_disabled());
2156 BUG_ON(!sdev);
2157
2158 found = false;
2159 spin_lock_irq(&sdev->spinlock);
2160 list_for_each_entry(ch, &sdev->rch_list, list) {
2161 if (ch->cm_id == cm_id) {
2162 found = true;
2163 break;
2164 }
2165 }
2166 spin_unlock_irq(&sdev->spinlock);
2167
2168 return found ? ch : NULL;
2169}
2170
2171/**
2172 * srpt_release_channel() - Release channel resources.
2173 *
2174 * Schedules the actual release because:
2175 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2176 * trigger a deadlock.
2177 * - It is not safe to call TCM transport_* functions from interrupt context.
2178 */
2179static void srpt_release_channel(struct srpt_rdma_ch *ch)
2180{
2181 schedule_work(&ch->release_work);
2182}
2183
2184static void srpt_release_channel_work(struct work_struct *w)
2185{
2186 struct srpt_rdma_ch *ch;
2187 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002188 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002189
2190 ch = container_of(w, struct srpt_rdma_ch, release_work);
2191 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2192 ch->release_done);
2193
2194 sdev = ch->sport->sdev;
2195 BUG_ON(!sdev);
2196
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002197 se_sess = ch->sess;
2198 BUG_ON(!se_sess);
2199
Joern Engelbe646c2d2013-05-15 00:44:07 -07002200 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002201
2202 transport_deregister_session_configfs(se_sess);
2203 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002204 ch->sess = NULL;
2205
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002206 ib_destroy_cm_id(ch->cm_id);
2207
Bart Van Asschea42d9852011-10-14 01:30:46 +00002208 srpt_destroy_ch_ib(ch);
2209
2210 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2211 ch->sport->sdev, ch->rq_size,
2212 ch->rsp_size, DMA_TO_DEVICE);
2213
2214 spin_lock_irq(&sdev->spinlock);
2215 list_del(&ch->list);
2216 spin_unlock_irq(&sdev->spinlock);
2217
Bart Van Asschea42d9852011-10-14 01:30:46 +00002218 if (ch->release_done)
2219 complete(ch->release_done);
2220
2221 wake_up(&sdev->ch_releaseQ);
2222
2223 kfree(ch);
2224}
2225
Bart Van Asschea42d9852011-10-14 01:30:46 +00002226/**
2227 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2228 *
2229 * Ownership of the cm_id is transferred to the target session if this
2230 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2231 */
2232static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2233 struct ib_cm_req_event_param *param,
2234 void *private_data)
2235{
2236 struct srpt_device *sdev = cm_id->context;
2237 struct srpt_port *sport = &sdev->port[param->port - 1];
2238 struct srp_login_req *req;
2239 struct srp_login_rsp *rsp;
2240 struct srp_login_rej *rej;
2241 struct ib_cm_rep_param *rep_param;
2242 struct srpt_rdma_ch *ch, *tmp_ch;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002243 u32 it_iu_len;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002244 int i, ret = 0;
2245 unsigned char *p;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002246
2247 WARN_ON_ONCE(irqs_disabled());
2248
2249 if (WARN_ON(!sdev || !private_data))
2250 return -EINVAL;
2251
2252 req = (struct srp_login_req *)private_data;
2253
2254 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2255
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002256 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2257 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2258 " (guid=0x%llx:0x%llx)\n",
2259 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2260 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2261 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2262 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2263 it_iu_len,
2264 param->port,
2265 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2266 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002267
2268 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2269 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2270 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2271
2272 if (!rsp || !rej || !rep_param) {
2273 ret = -ENOMEM;
2274 goto out;
2275 }
2276
2277 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302278 rej->reason = cpu_to_be32(
2279 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002280 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002281 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002282 " length (%d bytes) is out of range (%d .. %d)\n",
2283 it_iu_len, 64, srp_max_req_size);
2284 goto reject;
2285 }
2286
2287 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302288 rej->reason = cpu_to_be32(
2289 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002290 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002291 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002292 " has not yet been enabled\n");
2293 goto reject;
2294 }
2295
2296 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2297 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2298
2299 spin_lock_irq(&sdev->spinlock);
2300
2301 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2302 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2303 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2304 && param->port == ch->sport->port
2305 && param->listen_id == ch->sport->sdev->cm_id
2306 && ch->cm_id) {
2307 enum rdma_ch_state ch_state;
2308
2309 ch_state = srpt_get_ch_state(ch);
2310 if (ch_state != CH_CONNECTING
2311 && ch_state != CH_LIVE)
2312 continue;
2313
2314 /* found an existing channel */
2315 pr_debug("Found existing channel %s"
2316 " cm_id= %p state= %d\n",
2317 ch->sess_name, ch->cm_id, ch_state);
2318
2319 __srpt_close_ch(ch);
2320
2321 rsp->rsp_flags =
2322 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2323 }
2324 }
2325
2326 spin_unlock_irq(&sdev->spinlock);
2327
2328 } else
2329 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2330
2331 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2332 || *(__be64 *)(req->target_port_id + 8) !=
2333 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302334 rej->reason = cpu_to_be32(
2335 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002336 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002337 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002338 " has an invalid target port identifier.\n");
2339 goto reject;
2340 }
2341
2342 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2343 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302344 rej->reason = cpu_to_be32(
2345 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002346 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002347 ret = -ENOMEM;
2348 goto reject;
2349 }
2350
2351 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2352 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2353 memcpy(ch->t_port_id, req->target_port_id, 16);
2354 ch->sport = &sdev->port[param->port - 1];
2355 ch->cm_id = cm_id;
2356 /*
2357 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2358 * for the SRP protocol to the command queue size.
2359 */
2360 ch->rq_size = SRPT_RQ_SIZE;
2361 spin_lock_init(&ch->spinlock);
2362 ch->state = CH_CONNECTING;
2363 INIT_LIST_HEAD(&ch->cmd_wait_list);
2364 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2365
2366 ch->ioctx_ring = (struct srpt_send_ioctx **)
2367 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2368 sizeof(*ch->ioctx_ring[0]),
2369 ch->rsp_size, DMA_TO_DEVICE);
2370 if (!ch->ioctx_ring)
2371 goto free_ch;
2372
2373 INIT_LIST_HEAD(&ch->free_list);
2374 for (i = 0; i < ch->rq_size; i++) {
2375 ch->ioctx_ring[i]->ch = ch;
2376 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2377 }
2378
2379 ret = srpt_create_ch_ib(ch);
2380 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302381 rej->reason = cpu_to_be32(
2382 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002383 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002384 " a new RDMA channel failed.\n");
2385 goto free_ring;
2386 }
2387
2388 ret = srpt_ch_qp_rtr(ch, ch->qp);
2389 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302390 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002391 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002392 " RTR failed (error code = %d)\n", ret);
2393 goto destroy_ib;
2394 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002395
Bart Van Asschea42d9852011-10-14 01:30:46 +00002396 /*
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002397 * Use the initator port identifier as the session name, when
2398 * checking against se_node_acl->initiatorname[] this can be
2399 * with or without preceeding '0x'.
Bart Van Asschea42d9852011-10-14 01:30:46 +00002400 */
2401 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2402 be64_to_cpu(*(__be64 *)ch->i_port_id),
2403 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2404
2405 pr_debug("registering session %s\n", ch->sess_name);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002406 p = &ch->sess_name[0];
Bart Van Asschea42d9852011-10-14 01:30:46 +00002407
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002408try_again:
Nicholas Bellingerb42057a2016-01-09 20:42:43 -08002409 ch->sess = target_alloc_session(&sport->port_tpg_1, 0, 0,
2410 TARGET_PROT_NORMAL, p, ch, NULL);
2411 if (IS_ERR(ch->sess)) {
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002412 pr_info("Rejected login because no ACL has been"
Nicholas Bellingerb42057a2016-01-09 20:42:43 -08002413 " configured yet for initiator %s.\n", p);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002414 /*
2415 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2416 */
2417 if (p == &ch->sess_name[0]) {
2418 p += 2;
2419 goto try_again;
2420 }
Nicholas Bellingerb42057a2016-01-09 20:42:43 -08002421 rej->reason = cpu_to_be32((PTR_ERR(ch->sess) == -ENOMEM) ?
2422 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002423 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002424 goto destroy_ib;
2425 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002426
2427 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2428 ch->sess_name, ch->cm_id);
2429
2430 /* create srp_login_response */
2431 rsp->opcode = SRP_LOGIN_RSP;
2432 rsp->tag = req->tag;
2433 rsp->max_it_iu_len = req->req_it_iu_len;
2434 rsp->max_ti_iu_len = req->req_it_iu_len;
2435 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302436 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2437 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002438 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2439 atomic_set(&ch->req_lim, ch->rq_size);
2440 atomic_set(&ch->req_lim_delta, 0);
2441
2442 /* create cm reply */
2443 rep_param->qp_num = ch->qp->qp_num;
2444 rep_param->private_data = (void *)rsp;
2445 rep_param->private_data_len = sizeof *rsp;
2446 rep_param->rnr_retry_count = 7;
2447 rep_param->flow_control = 1;
2448 rep_param->failover_accepted = 0;
2449 rep_param->srq = 1;
2450 rep_param->responder_resources = 4;
2451 rep_param->initiator_depth = 4;
2452
2453 ret = ib_send_cm_rep(cm_id, rep_param);
2454 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002455 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002456 " (error code = %d)\n", ret);
2457 goto release_channel;
2458 }
2459
2460 spin_lock_irq(&sdev->spinlock);
2461 list_add_tail(&ch->list, &sdev->rch_list);
2462 spin_unlock_irq(&sdev->spinlock);
2463
2464 goto out;
2465
2466release_channel:
2467 srpt_set_ch_state(ch, CH_RELEASING);
2468 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002469 transport_deregister_session(ch->sess);
2470 ch->sess = NULL;
2471
2472destroy_ib:
2473 srpt_destroy_ch_ib(ch);
2474
2475free_ring:
2476 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2477 ch->sport->sdev, ch->rq_size,
2478 ch->rsp_size, DMA_TO_DEVICE);
2479free_ch:
2480 kfree(ch);
2481
2482reject:
2483 rej->opcode = SRP_LOGIN_REJ;
2484 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302485 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2486 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002487
2488 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2489 (void *)rej, sizeof *rej);
2490
2491out:
2492 kfree(rep_param);
2493 kfree(rsp);
2494 kfree(rej);
2495
2496 return ret;
2497}
2498
2499static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2500{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002501 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002502 srpt_drain_channel(cm_id);
2503}
2504
2505/**
2506 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2507 *
2508 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2509 * and that the recipient may begin transmitting (RTU = ready to use).
2510 */
2511static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2512{
2513 struct srpt_rdma_ch *ch;
2514 int ret;
2515
2516 ch = srpt_find_channel(cm_id->context, cm_id);
2517 BUG_ON(!ch);
2518
2519 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2520 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2521
2522 ret = srpt_ch_qp_rts(ch, ch->qp);
2523
2524 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2525 wait_list) {
2526 list_del(&ioctx->wait_list);
2527 srpt_handle_new_iu(ch, ioctx, NULL);
2528 }
2529 if (ret)
2530 srpt_close_ch(ch);
2531 }
2532}
2533
2534static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2535{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002536 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002537 srpt_drain_channel(cm_id);
2538}
2539
2540static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2541{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002542 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002543 srpt_drain_channel(cm_id);
2544}
2545
2546/**
2547 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2548 */
2549static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2550{
2551 struct srpt_rdma_ch *ch;
2552 unsigned long flags;
2553 bool send_drep = false;
2554
2555 ch = srpt_find_channel(cm_id->context, cm_id);
2556 BUG_ON(!ch);
2557
2558 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2559
2560 spin_lock_irqsave(&ch->spinlock, flags);
2561 switch (ch->state) {
2562 case CH_CONNECTING:
2563 case CH_LIVE:
2564 send_drep = true;
2565 ch->state = CH_DISCONNECTING;
2566 break;
2567 case CH_DISCONNECTING:
2568 case CH_DRAINING:
2569 case CH_RELEASING:
2570 WARN(true, "unexpected channel state %d\n", ch->state);
2571 break;
2572 }
2573 spin_unlock_irqrestore(&ch->spinlock, flags);
2574
2575 if (send_drep) {
2576 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002577 pr_err("Sending IB DREP failed.\n");
2578 pr_info("Received DREQ and sent DREP for session %s.\n",
2579 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002580 }
2581}
2582
2583/**
2584 * srpt_cm_drep_recv() - Process reception of a DREP message.
2585 */
2586static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2587{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002588 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002589 srpt_drain_channel(cm_id);
2590}
2591
2592/**
2593 * srpt_cm_handler() - IB connection manager callback function.
2594 *
2595 * A non-zero return value will cause the caller destroy the CM ID.
2596 *
2597 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2598 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2599 * a non-zero value in any other case will trigger a race with the
2600 * ib_destroy_cm_id() call in srpt_release_channel().
2601 */
2602static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2603{
2604 int ret;
2605
2606 ret = 0;
2607 switch (event->event) {
2608 case IB_CM_REQ_RECEIVED:
2609 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2610 event->private_data);
2611 break;
2612 case IB_CM_REJ_RECEIVED:
2613 srpt_cm_rej_recv(cm_id);
2614 break;
2615 case IB_CM_RTU_RECEIVED:
2616 case IB_CM_USER_ESTABLISHED:
2617 srpt_cm_rtu_recv(cm_id);
2618 break;
2619 case IB_CM_DREQ_RECEIVED:
2620 srpt_cm_dreq_recv(cm_id);
2621 break;
2622 case IB_CM_DREP_RECEIVED:
2623 srpt_cm_drep_recv(cm_id);
2624 break;
2625 case IB_CM_TIMEWAIT_EXIT:
2626 srpt_cm_timewait_exit(cm_id);
2627 break;
2628 case IB_CM_REP_ERROR:
2629 srpt_cm_rep_error(cm_id);
2630 break;
2631 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002632 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002633 break;
2634 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002635 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002636 break;
2637 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002638 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002639 break;
2640 }
2641
2642 return ret;
2643}
2644
2645/**
2646 * srpt_perform_rdmas() - Perform IB RDMA.
2647 *
2648 * Returns zero upon success or a negative number upon failure.
2649 */
2650static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2651 struct srpt_send_ioctx *ioctx)
2652{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002653 struct ib_send_wr *bad_wr;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002654 int sq_wr_avail, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002655 enum dma_data_direction dir;
2656 const int n_rdma = ioctx->n_rdma;
2657
2658 dir = ioctx->cmd.data_direction;
2659 if (dir == DMA_TO_DEVICE) {
2660 /* write */
2661 ret = -ENOMEM;
2662 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2663 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002664 pr_warn("IB send queue full (needed %d)\n",
2665 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002666 goto out;
2667 }
2668 }
2669
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002670 for (i = 0; i < n_rdma; i++) {
2671 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002672
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002673 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2674 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2675
2676 if (i == n_rdma - 1) {
2677 /* only get completion event for the last rdma read */
2678 if (dir == DMA_TO_DEVICE) {
2679 wr->send_flags = IB_SEND_SIGNALED;
2680 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2681 } else {
2682 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2683 }
2684 wr->wr_cqe = &ioctx->rdma_cqe;
2685 wr->next = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002686 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002687 wr->wr_cqe = NULL;
2688 wr->next = &ioctx->rdma_wrs[i + 1].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002689 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002690 }
2691
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002692 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002693 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002694 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002695 __func__, __LINE__, ret, i, n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002696out:
2697 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2698 atomic_add(n_rdma, &ch->sq_wr_avail);
2699 return ret;
2700}
2701
2702/**
2703 * srpt_xfer_data() - Start data transfer from initiator to target.
2704 */
2705static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2706 struct srpt_send_ioctx *ioctx)
2707{
2708 int ret;
2709
2710 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2711 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002712 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002713 goto out;
2714 }
2715
2716 ret = srpt_perform_rdmas(ch, ioctx);
2717 if (ret) {
2718 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002719 pr_info("%s[%d] queue full -- ret=%d\n",
2720 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002721 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002722 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002723 __func__, __LINE__, ret);
2724 goto out_unmap;
2725 }
2726
2727out:
2728 return ret;
2729out_unmap:
2730 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2731 goto out;
2732}
2733
2734static int srpt_write_pending_status(struct se_cmd *se_cmd)
2735{
2736 struct srpt_send_ioctx *ioctx;
2737
2738 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2739 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2740}
2741
2742/*
2743 * srpt_write_pending() - Start data transfer from initiator to target (write).
2744 */
2745static int srpt_write_pending(struct se_cmd *se_cmd)
2746{
2747 struct srpt_rdma_ch *ch;
2748 struct srpt_send_ioctx *ioctx;
2749 enum srpt_command_state new_state;
2750 enum rdma_ch_state ch_state;
2751 int ret;
2752
2753 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2754
2755 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2756 WARN_ON(new_state == SRPT_STATE_DONE);
2757
2758 ch = ioctx->ch;
2759 BUG_ON(!ch);
2760
2761 ch_state = srpt_get_ch_state(ch);
2762 switch (ch_state) {
2763 case CH_CONNECTING:
2764 WARN(true, "unexpected channel state %d\n", ch_state);
2765 ret = -EINVAL;
2766 goto out;
2767 case CH_LIVE:
2768 break;
2769 case CH_DISCONNECTING:
2770 case CH_DRAINING:
2771 case CH_RELEASING:
2772 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002773 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002774 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2775 ret = -EINVAL;
2776 goto out;
2777 }
2778 ret = srpt_xfer_data(ch, ioctx);
2779
2780out:
2781 return ret;
2782}
2783
2784static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2785{
2786 switch (tcm_mgmt_status) {
2787 case TMR_FUNCTION_COMPLETE:
2788 return SRP_TSK_MGMT_SUCCESS;
2789 case TMR_FUNCTION_REJECTED:
2790 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2791 }
2792 return SRP_TSK_MGMT_FAILED;
2793}
2794
2795/**
2796 * srpt_queue_response() - Transmits the response to a SCSI command.
2797 *
2798 * Callback function called by the TCM core. Must not block since it can be
2799 * invoked on the context of the IB completion handler.
2800 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002801static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002802{
2803 struct srpt_rdma_ch *ch;
2804 struct srpt_send_ioctx *ioctx;
2805 enum srpt_command_state state;
2806 unsigned long flags;
2807 int ret;
2808 enum dma_data_direction dir;
2809 int resp_len;
2810 u8 srp_tm_status;
2811
Bart Van Asschea42d9852011-10-14 01:30:46 +00002812 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2813 ch = ioctx->ch;
2814 BUG_ON(!ch);
2815
2816 spin_lock_irqsave(&ioctx->spinlock, flags);
2817 state = ioctx->state;
2818 switch (state) {
2819 case SRPT_STATE_NEW:
2820 case SRPT_STATE_DATA_IN:
2821 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2822 break;
2823 case SRPT_STATE_MGMT:
2824 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2825 break;
2826 default:
2827 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2828 ch, ioctx->ioctx.index, ioctx->state);
2829 break;
2830 }
2831 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2832
2833 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2834 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2835 atomic_inc(&ch->req_lim_delta);
2836 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04002837 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002838 }
2839
2840 dir = ioctx->cmd.data_direction;
2841
2842 /* For read commands, transfer the data to the initiator. */
2843 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2844 !ioctx->queue_status_only) {
2845 ret = srpt_xfer_data(ch, ioctx);
2846 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002847 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002848 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04002849 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002850 }
2851 }
2852
2853 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002854 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002855 cmd->scsi_status);
2856 else {
2857 srp_tm_status
2858 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2859 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002860 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002861 }
2862 ret = srpt_post_send(ch, ioctx, resp_len);
2863 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002864 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002865 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002866 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2867 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02002868 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002869 }
Joern Engelb79fafa2013-07-03 11:22:17 -04002870}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002871
Joern Engelb79fafa2013-07-03 11:22:17 -04002872static int srpt_queue_data_in(struct se_cmd *cmd)
2873{
2874 srpt_queue_response(cmd);
2875 return 0;
2876}
2877
2878static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2879{
2880 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002881}
2882
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002883static void srpt_aborted_task(struct se_cmd *cmd)
2884{
2885 struct srpt_send_ioctx *ioctx = container_of(cmd,
2886 struct srpt_send_ioctx, cmd);
2887
2888 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2889}
2890
Bart Van Asschea42d9852011-10-14 01:30:46 +00002891static int srpt_queue_status(struct se_cmd *cmd)
2892{
2893 struct srpt_send_ioctx *ioctx;
2894
2895 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2896 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2897 if (cmd->se_cmd_flags &
2898 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2899 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2900 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002901 srpt_queue_response(cmd);
2902 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002903}
2904
2905static void srpt_refresh_port_work(struct work_struct *work)
2906{
2907 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2908
2909 srpt_refresh_port(sport);
2910}
2911
2912static int srpt_ch_list_empty(struct srpt_device *sdev)
2913{
2914 int res;
2915
2916 spin_lock_irq(&sdev->spinlock);
2917 res = list_empty(&sdev->rch_list);
2918 spin_unlock_irq(&sdev->spinlock);
2919
2920 return res;
2921}
2922
2923/**
2924 * srpt_release_sdev() - Free the channel resources associated with a target.
2925 */
2926static int srpt_release_sdev(struct srpt_device *sdev)
2927{
2928 struct srpt_rdma_ch *ch, *tmp_ch;
2929 int res;
2930
2931 WARN_ON_ONCE(irqs_disabled());
2932
2933 BUG_ON(!sdev);
2934
2935 spin_lock_irq(&sdev->spinlock);
2936 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2937 __srpt_close_ch(ch);
2938 spin_unlock_irq(&sdev->spinlock);
2939
2940 res = wait_event_interruptible(sdev->ch_releaseQ,
2941 srpt_ch_list_empty(sdev));
2942 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002943 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002944
2945 return 0;
2946}
2947
2948static struct srpt_port *__srpt_lookup_port(const char *name)
2949{
2950 struct ib_device *dev;
2951 struct srpt_device *sdev;
2952 struct srpt_port *sport;
2953 int i;
2954
2955 list_for_each_entry(sdev, &srpt_dev_list, list) {
2956 dev = sdev->device;
2957 if (!dev)
2958 continue;
2959
2960 for (i = 0; i < dev->phys_port_cnt; i++) {
2961 sport = &sdev->port[i];
2962
2963 if (!strcmp(sport->port_guid, name))
2964 return sport;
2965 }
2966 }
2967
2968 return NULL;
2969}
2970
2971static struct srpt_port *srpt_lookup_port(const char *name)
2972{
2973 struct srpt_port *sport;
2974
2975 spin_lock(&srpt_dev_lock);
2976 sport = __srpt_lookup_port(name);
2977 spin_unlock(&srpt_dev_lock);
2978
2979 return sport;
2980}
2981
2982/**
2983 * srpt_add_one() - Infiniband device addition callback function.
2984 */
2985static void srpt_add_one(struct ib_device *device)
2986{
2987 struct srpt_device *sdev;
2988 struct srpt_port *sport;
2989 struct ib_srq_init_attr srq_attr;
2990 int i;
2991
2992 pr_debug("device = %p, device->dma_ops = %p\n", device,
2993 device->dma_ops);
2994
2995 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
2996 if (!sdev)
2997 goto err;
2998
2999 sdev->device = device;
3000 INIT_LIST_HEAD(&sdev->rch_list);
3001 init_waitqueue_head(&sdev->ch_releaseQ);
3002 spin_lock_init(&sdev->spinlock);
3003
Bart Van Asschea42d9852011-10-14 01:30:46 +00003004 sdev->pd = ib_alloc_pd(device);
3005 if (IS_ERR(sdev->pd))
3006 goto free_dev;
3007
Or Gerlitz4a061b22015-12-18 10:59:46 +02003008 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003009
3010 srq_attr.event_handler = srpt_srq_event;
3011 srq_attr.srq_context = (void *)sdev;
3012 srq_attr.attr.max_wr = sdev->srq_size;
3013 srq_attr.attr.max_sge = 1;
3014 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07003015 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003016
3017 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3018 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06003019 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003020
3021 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
Or Gerlitz4a061b22015-12-18 10:59:46 +02003022 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003023 device->name);
3024
3025 if (!srpt_service_guid)
3026 srpt_service_guid = be64_to_cpu(device->node_guid);
3027
3028 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3029 if (IS_ERR(sdev->cm_id))
3030 goto err_srq;
3031
3032 /* print out target login information */
3033 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3034 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3035 srpt_service_guid, srpt_service_guid);
3036
3037 /*
3038 * We do not have a consistent service_id (ie. also id_ext of target_id)
3039 * to identify this target. We currently use the guid of the first HCA
3040 * in the system as service_id; therefore, the target_id will change
3041 * if this HCA is gone bad and replaced by different HCA
3042 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03003043 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00003044 goto err_cm;
3045
3046 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3047 srpt_event_handler);
3048 if (ib_register_event_handler(&sdev->event_handler))
3049 goto err_cm;
3050
3051 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3052 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3053 sizeof(*sdev->ioctx_ring[0]),
3054 srp_max_req_size, DMA_FROM_DEVICE);
3055 if (!sdev->ioctx_ring)
3056 goto err_event;
3057
3058 for (i = 0; i < sdev->srq_size; ++i)
3059 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3060
Roland Dreierf2250662012-02-02 12:55:58 -08003061 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00003062
3063 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3064 sport = &sdev->port[i - 1];
3065 sport->sdev = sdev;
3066 sport->port = i;
3067 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3068 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3069 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3070 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003071
3072 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003073 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003074 srpt_sdev_name(sdev), i);
3075 goto err_ring;
3076 }
3077 snprintf(sport->port_guid, sizeof(sport->port_guid),
3078 "0x%016llx%016llx",
3079 be64_to_cpu(sport->gid.global.subnet_prefix),
3080 be64_to_cpu(sport->gid.global.interface_id));
3081 }
3082
3083 spin_lock(&srpt_dev_lock);
3084 list_add_tail(&sdev->list, &srpt_dev_list);
3085 spin_unlock(&srpt_dev_lock);
3086
3087out:
3088 ib_set_client_data(device, &srpt_client, sdev);
3089 pr_debug("added %s.\n", device->name);
3090 return;
3091
3092err_ring:
3093 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3094 sdev->srq_size, srp_max_req_size,
3095 DMA_FROM_DEVICE);
3096err_event:
3097 ib_unregister_event_handler(&sdev->event_handler);
3098err_cm:
3099 ib_destroy_cm_id(sdev->cm_id);
3100err_srq:
3101 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003102err_pd:
3103 ib_dealloc_pd(sdev->pd);
3104free_dev:
3105 kfree(sdev);
3106err:
3107 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003108 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003109 goto out;
3110}
3111
3112/**
3113 * srpt_remove_one() - InfiniBand device removal callback function.
3114 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03003115static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003116{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003117 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003118 int i;
3119
Bart Van Asschea42d9852011-10-14 01:30:46 +00003120 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003121 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003122 return;
3123 }
3124
3125 srpt_unregister_mad_agent(sdev);
3126
3127 ib_unregister_event_handler(&sdev->event_handler);
3128
3129 /* Cancel any work queued by the just unregistered IB event handler. */
3130 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3131 cancel_work_sync(&sdev->port[i].work);
3132
3133 ib_destroy_cm_id(sdev->cm_id);
3134
3135 /*
3136 * Unregistering a target must happen after destroying sdev->cm_id
3137 * such that no new SRP_LOGIN_REQ information units can arrive while
3138 * destroying the target.
3139 */
3140 spin_lock(&srpt_dev_lock);
3141 list_del(&sdev->list);
3142 spin_unlock(&srpt_dev_lock);
3143 srpt_release_sdev(sdev);
3144
3145 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003146 ib_dealloc_pd(sdev->pd);
3147
3148 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3149 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3150 sdev->ioctx_ring = NULL;
3151 kfree(sdev);
3152}
3153
3154static struct ib_client srpt_client = {
3155 .name = DRV_NAME,
3156 .add = srpt_add_one,
3157 .remove = srpt_remove_one
3158};
3159
3160static int srpt_check_true(struct se_portal_group *se_tpg)
3161{
3162 return 1;
3163}
3164
3165static int srpt_check_false(struct se_portal_group *se_tpg)
3166{
3167 return 0;
3168}
3169
3170static char *srpt_get_fabric_name(void)
3171{
3172 return "srpt";
3173}
3174
Bart Van Asschea42d9852011-10-14 01:30:46 +00003175static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3176{
3177 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3178
3179 return sport->port_guid;
3180}
3181
3182static u16 srpt_get_tag(struct se_portal_group *tpg)
3183{
3184 return 1;
3185}
3186
Bart Van Asschea42d9852011-10-14 01:30:46 +00003187static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3188{
3189 return 1;
3190}
3191
3192static void srpt_release_cmd(struct se_cmd *se_cmd)
3193{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003194 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3195 struct srpt_send_ioctx, cmd);
3196 struct srpt_rdma_ch *ch = ioctx->ch;
3197 unsigned long flags;
3198
3199 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3200 WARN_ON(ioctx->mapped_sg_count != 0);
3201
3202 if (ioctx->n_rbuf > 1) {
3203 kfree(ioctx->rbufs);
3204 ioctx->rbufs = NULL;
3205 ioctx->n_rbuf = 0;
3206 }
3207
3208 spin_lock_irqsave(&ch->spinlock, flags);
3209 list_add(&ioctx->free_list, &ch->free_list);
3210 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003211}
3212
3213/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003214 * srpt_close_session() - Forcibly close a session.
3215 *
3216 * Callback function invoked by the TCM core to clean up sessions associated
3217 * with a node ACL when the user invokes
3218 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3219 */
3220static void srpt_close_session(struct se_session *se_sess)
3221{
3222 DECLARE_COMPLETION_ONSTACK(release_done);
3223 struct srpt_rdma_ch *ch;
3224 struct srpt_device *sdev;
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003225 unsigned long res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003226
3227 ch = se_sess->fabric_sess_ptr;
3228 WARN_ON(ch->sess != se_sess);
3229
3230 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3231
3232 sdev = ch->sport->sdev;
3233 spin_lock_irq(&sdev->spinlock);
3234 BUG_ON(ch->release_done);
3235 ch->release_done = &release_done;
3236 __srpt_close_ch(ch);
3237 spin_unlock_irq(&sdev->spinlock);
3238
3239 res = wait_for_completion_timeout(&release_done, 60 * HZ);
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003240 WARN_ON(res == 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003241}
3242
3243/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003244 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3245 *
3246 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3247 * This object represents an arbitrary integer used to uniquely identify a
3248 * particular attached remote initiator port to a particular SCSI target port
3249 * within a particular SCSI target device within a particular SCSI instance.
3250 */
3251static u32 srpt_sess_get_index(struct se_session *se_sess)
3252{
3253 return 0;
3254}
3255
3256static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3257{
3258}
3259
Bart Van Asschea42d9852011-10-14 01:30:46 +00003260/* Note: only used from inside debug printk's by the TCM core. */
3261static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3262{
3263 struct srpt_send_ioctx *ioctx;
3264
3265 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3266 return srpt_get_cmd_state(ioctx);
3267}
3268
Bart Van Asschea42d9852011-10-14 01:30:46 +00003269/**
3270 * srpt_parse_i_port_id() - Parse an initiator port ID.
3271 * @name: ASCII representation of a 128-bit initiator port ID.
3272 * @i_port_id: Binary 128-bit port ID.
3273 */
3274static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3275{
3276 const char *p;
3277 unsigned len, count, leading_zero_bytes;
3278 int ret, rc;
3279
3280 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003281 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003282 p += 2;
3283 ret = -EINVAL;
3284 len = strlen(p);
3285 if (len % 2)
3286 goto out;
3287 count = min(len / 2, 16U);
3288 leading_zero_bytes = 16 - count;
3289 memset(i_port_id, 0, leading_zero_bytes);
3290 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3291 if (rc < 0)
3292 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3293 ret = 0;
3294out:
3295 return ret;
3296}
3297
3298/*
3299 * configfs callback function invoked for
3300 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3301 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003302static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003303{
Bart Van Asschea42d9852011-10-14 01:30:46 +00003304 u8 i_port_id[16];
3305
3306 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003307 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003308 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003309 }
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003310 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003311}
3312
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003313static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3314 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003315{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003316 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003317 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3318
3319 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3320}
3321
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003322static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3323 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003324{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003325 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003326 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3327 unsigned long val;
3328 int ret;
3329
Jingoo Han9d8abf42014-02-05 11:22:05 +09003330 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003331 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003332 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003333 return -EINVAL;
3334 }
3335 if (val > MAX_SRPT_RDMA_SIZE) {
3336 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3337 MAX_SRPT_RDMA_SIZE);
3338 return -EINVAL;
3339 }
3340 if (val < DEFAULT_MAX_RDMA_SIZE) {
3341 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3342 val, DEFAULT_MAX_RDMA_SIZE);
3343 return -EINVAL;
3344 }
3345 sport->port_attrib.srp_max_rdma_size = val;
3346
3347 return count;
3348}
3349
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003350static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3351 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003352{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003353 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003354 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3355
3356 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3357}
3358
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003359static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3360 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003361{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003362 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003363 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3364 unsigned long val;
3365 int ret;
3366
Jingoo Han9d8abf42014-02-05 11:22:05 +09003367 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003368 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003369 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003370 return -EINVAL;
3371 }
3372 if (val > MAX_SRPT_RSP_SIZE) {
3373 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3374 MAX_SRPT_RSP_SIZE);
3375 return -EINVAL;
3376 }
3377 if (val < MIN_MAX_RSP_SIZE) {
3378 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3379 MIN_MAX_RSP_SIZE);
3380 return -EINVAL;
3381 }
3382 sport->port_attrib.srp_max_rsp_size = val;
3383
3384 return count;
3385}
3386
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003387static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3388 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003389{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003390 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003391 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3392
3393 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3394}
3395
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003396static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3397 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003398{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003399 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003400 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3401 unsigned long val;
3402 int ret;
3403
Jingoo Han9d8abf42014-02-05 11:22:05 +09003404 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003405 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003406 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003407 return -EINVAL;
3408 }
3409 if (val > MAX_SRPT_SRQ_SIZE) {
3410 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3411 MAX_SRPT_SRQ_SIZE);
3412 return -EINVAL;
3413 }
3414 if (val < MIN_SRPT_SRQ_SIZE) {
3415 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3416 MIN_SRPT_SRQ_SIZE);
3417 return -EINVAL;
3418 }
3419 sport->port_attrib.srp_sq_size = val;
3420
3421 return count;
3422}
3423
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003424CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3425CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3426CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003427
3428static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003429 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3430 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3431 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003432 NULL,
3433};
3434
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003435static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003436{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003437 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003438 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3439
3440 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3441}
3442
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003443static ssize_t srpt_tpg_enable_store(struct config_item *item,
3444 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003445{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003446 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003447 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3448 unsigned long tmp;
3449 int ret;
3450
Jingoo Han9d8abf42014-02-05 11:22:05 +09003451 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003452 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003453 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003454 return -EINVAL;
3455 }
3456
3457 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003458 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003459 return -EINVAL;
3460 }
3461 if (tmp == 1)
3462 sport->enabled = true;
3463 else
3464 sport->enabled = false;
3465
3466 return count;
3467}
3468
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003469CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003470
3471static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003472 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003473 NULL,
3474};
3475
3476/**
3477 * configfs callback invoked for
3478 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3479 */
3480static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3481 struct config_group *group,
3482 const char *name)
3483{
3484 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3485 int res;
3486
3487 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003488 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003489 if (res)
3490 return ERR_PTR(res);
3491
3492 return &sport->port_tpg_1;
3493}
3494
3495/**
3496 * configfs callback invoked for
3497 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3498 */
3499static void srpt_drop_tpg(struct se_portal_group *tpg)
3500{
3501 struct srpt_port *sport = container_of(tpg,
3502 struct srpt_port, port_tpg_1);
3503
3504 sport->enabled = false;
3505 core_tpg_deregister(&sport->port_tpg_1);
3506}
3507
3508/**
3509 * configfs callback invoked for
3510 * mkdir /sys/kernel/config/target/$driver/$port
3511 */
3512static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3513 struct config_group *group,
3514 const char *name)
3515{
3516 struct srpt_port *sport;
3517 int ret;
3518
3519 sport = srpt_lookup_port(name);
3520 pr_debug("make_tport(%s)\n", name);
3521 ret = -EINVAL;
3522 if (!sport)
3523 goto err;
3524
3525 return &sport->port_wwn;
3526
3527err:
3528 return ERR_PTR(ret);
3529}
3530
3531/**
3532 * configfs callback invoked for
3533 * rmdir /sys/kernel/config/target/$driver/$port
3534 */
3535static void srpt_drop_tport(struct se_wwn *wwn)
3536{
3537 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3538
3539 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3540}
3541
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003542static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003543{
3544 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3545}
3546
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003547CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003548
3549static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003550 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003551 NULL,
3552};
3553
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003554static const struct target_core_fabric_ops srpt_template = {
3555 .module = THIS_MODULE,
3556 .name = "srpt",
Christoph Hellwig144bc4c2015-04-13 19:51:16 +02003557 .node_acl_size = sizeof(struct srpt_node_acl),
Bart Van Asschea42d9852011-10-14 01:30:46 +00003558 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003559 .tpg_get_wwn = srpt_get_fabric_wwn,
3560 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003561 .tpg_check_demo_mode = srpt_check_false,
3562 .tpg_check_demo_mode_cache = srpt_check_true,
3563 .tpg_check_demo_mode_write_protect = srpt_check_true,
3564 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003565 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3566 .release_cmd = srpt_release_cmd,
3567 .check_stop_free = srpt_check_stop_free,
3568 .shutdown_session = srpt_shutdown_session,
3569 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003570 .sess_get_index = srpt_sess_get_index,
3571 .sess_get_initiator_sid = NULL,
3572 .write_pending = srpt_write_pending,
3573 .write_pending_status = srpt_write_pending_status,
3574 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003575 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003576 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003577 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003578 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003579 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003580 /*
3581 * Setup function pointers for generic logic in
3582 * target_core_fabric_configfs.c
3583 */
3584 .fabric_make_wwn = srpt_make_tport,
3585 .fabric_drop_wwn = srpt_drop_tport,
3586 .fabric_make_tpg = srpt_make_tpg,
3587 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003588 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003589
3590 .tfc_wwn_attrs = srpt_wwn_attrs,
3591 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3592 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003593};
3594
3595/**
3596 * srpt_init_module() - Kernel module initialization.
3597 *
3598 * Note: Since ib_register_client() registers callback functions, and since at
3599 * least one of these callback functions (srpt_add_one()) calls target core
3600 * functions, this driver must be registered with the target core before
3601 * ib_register_client() is called.
3602 */
3603static int __init srpt_init_module(void)
3604{
3605 int ret;
3606
3607 ret = -EINVAL;
3608 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003609 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003610 " srp_max_req_size -- must be at least %d.\n",
3611 srp_max_req_size, MIN_MAX_REQ_SIZE);
3612 goto out;
3613 }
3614
3615 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3616 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003617 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003618 " srpt_srq_size -- must be in the range [%d..%d].\n",
3619 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3620 goto out;
3621 }
3622
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003623 ret = target_register_template(&srpt_template);
3624 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003625 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003626
3627 ret = ib_register_client(&srpt_client);
3628 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003629 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003630 goto out_unregister_target;
3631 }
3632
3633 return 0;
3634
3635out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003636 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003637out:
3638 return ret;
3639}
3640
3641static void __exit srpt_cleanup_module(void)
3642{
3643 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003644 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003645}
3646
3647module_init(srpt_init_module);
3648module_exit(srpt_cleanup_module);