blob: 9f5cd3ff3fcb2fcc5efdfe69f565e1ab6be53ad5 [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;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800284 memset(cif, 0, sizeof(*cif));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000285 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
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800343 memset(iocp, 0, sizeof(*iocp));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000344 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;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800393 memset(svc_entries, 0, sizeof(*svc_entries));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000394 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;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800487 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000488 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
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800535 memset(&port_modify, 0, sizeof(port_modify));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000536 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) {
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800556 memset(&reg_req, 0, sizeof(reg_req));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000557 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);
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800906 memcpy(ioctx->rbufs, db, sizeof(*db));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000907 *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
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800913 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof(*db);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000914
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 =
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800932 kmalloc(ioctx->n_rbuf * sizeof(*db), GFP_ATOMIC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000933 if (!ioctx->rbufs) {
934 ioctx->n_rbuf = 0;
935 ret = -ENOMEM;
936 goto out;
937 }
938 }
939
940 db = idb->desc_list;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800941 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof(*db));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000942 *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
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -0800959 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000960 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 Hellwig7d680f32011-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 Hellwig7d680f32011-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
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001467 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001468 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);
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001517 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001518
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
Bart Van Asschea42d9852011-10-14 01:30:46 +00001673static int srp_tmr_to_tcm(int fn)
1674{
1675 switch (fn) {
1676 case SRP_TSK_ABORT_TASK:
1677 return TMR_ABORT_TASK;
1678 case SRP_TSK_ABORT_TASK_SET:
1679 return TMR_ABORT_TASK_SET;
1680 case SRP_TSK_CLEAR_TASK_SET:
1681 return TMR_CLEAR_TASK_SET;
1682 case SRP_TSK_LUN_RESET:
1683 return TMR_LUN_RESET;
1684 case SRP_TSK_CLEAR_ACA:
1685 return TMR_CLEAR_ACA;
1686 default:
1687 return -1;
1688 }
1689}
1690
1691/**
1692 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1693 *
1694 * Returns 0 if and only if the request will be processed by the target core.
1695 *
1696 * For more information about SRP_TSK_MGMT information units, see also section
1697 * 6.7 in the SRP r16a document.
1698 */
1699static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1700 struct srpt_recv_ioctx *recv_ioctx,
1701 struct srpt_send_ioctx *send_ioctx)
1702{
1703 struct srp_tsk_mgmt *srp_tsk;
1704 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001705 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001706 uint64_t unpacked_lun;
1707 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001708 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001709
1710 BUG_ON(!send_ioctx);
1711
1712 srp_tsk = recv_ioctx->ioctx.buf;
1713 cmd = &send_ioctx->cmd;
1714
1715 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1716 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1717 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1718
1719 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001720 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001721 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001722 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1723 sizeof(srp_tsk->lun));
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001724 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
Bart Van Assche51093252016-02-11 11:03:09 -08001725 srp_tsk, tcm_tmr, GFP_KERNEL, srp_tsk->task_tag,
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001726 TARGET_SCF_ACK_KREF);
1727 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001728 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001729 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001730 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001731 return;
1732fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001733 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001734}
1735
1736/**
1737 * srpt_handle_new_iu() - Process a newly received information unit.
1738 * @ch: RDMA channel through which the information unit has been received.
1739 * @ioctx: SRPT I/O context associated with the information unit.
1740 */
1741static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1742 struct srpt_recv_ioctx *recv_ioctx,
1743 struct srpt_send_ioctx *send_ioctx)
1744{
1745 struct srp_cmd *srp_cmd;
1746 enum rdma_ch_state ch_state;
1747
1748 BUG_ON(!ch);
1749 BUG_ON(!recv_ioctx);
1750
1751 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1752 recv_ioctx->ioctx.dma, srp_max_req_size,
1753 DMA_FROM_DEVICE);
1754
1755 ch_state = srpt_get_ch_state(ch);
1756 if (unlikely(ch_state == CH_CONNECTING)) {
1757 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1758 goto out;
1759 }
1760
1761 if (unlikely(ch_state != CH_LIVE))
1762 goto out;
1763
1764 srp_cmd = recv_ioctx->ioctx.buf;
1765 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1766 if (!send_ioctx)
1767 send_ioctx = srpt_get_send_ioctx(ch);
1768 if (unlikely(!send_ioctx)) {
1769 list_add_tail(&recv_ioctx->wait_list,
1770 &ch->cmd_wait_list);
1771 goto out;
1772 }
1773 }
1774
Bart Van Asschea42d9852011-10-14 01:30:46 +00001775 switch (srp_cmd->opcode) {
1776 case SRP_CMD:
1777 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1778 break;
1779 case SRP_TSK_MGMT:
1780 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1781 break;
1782 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001783 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001784 break;
1785 case SRP_CRED_RSP:
1786 pr_debug("received SRP_CRED_RSP\n");
1787 break;
1788 case SRP_AER_RSP:
1789 pr_debug("received SRP_AER_RSP\n");
1790 break;
1791 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001792 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001793 break;
1794 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001795 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001796 srp_cmd->opcode);
1797 break;
1798 }
1799
1800 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1801out:
1802 return;
1803}
1804
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001805static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001806{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001807 struct srpt_rdma_ch *ch = cq->cq_context;
1808 struct srpt_recv_ioctx *ioctx =
1809 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001810
Bart Van Asschea42d9852011-10-14 01:30:46 +00001811 if (wc->status == IB_WC_SUCCESS) {
1812 int req_lim;
1813
1814 req_lim = atomic_dec_return(&ch->req_lim);
1815 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001816 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001817 srpt_handle_new_iu(ch, ioctx, NULL);
1818 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001819 pr_info("receiving failed for ioctx %p with status %d\n",
1820 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001821 }
1822}
1823
1824/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001825 * Note: Although this has not yet been observed during tests, at least in
1826 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1827 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1828 * value in each response is set to one, and it is possible that this response
1829 * makes the initiator send a new request before the send completion for that
1830 * response has been processed. This could e.g. happen if the call to
1831 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1832 * if IB retransmission causes generation of the send completion to be
1833 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1834 * are queued on cmd_wait_list. The code below processes these delayed
1835 * requests one at a time.
1836 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001837static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001838{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001839 struct srpt_rdma_ch *ch = cq->cq_context;
1840 struct srpt_send_ioctx *ioctx =
1841 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1842 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001843
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001844 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1845
1846 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1847 state != SRPT_STATE_MGMT_RSP_SENT);
1848
1849 atomic_inc(&ch->sq_wr_avail);
1850
1851 if (wc->status != IB_WC_SUCCESS) {
1852 pr_info("sending response for ioctx 0x%p failed"
1853 " with status %d\n", ioctx, wc->status);
1854
1855 atomic_dec(&ch->req_lim);
1856 srpt_abort_cmd(ioctx);
1857 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001858 }
1859
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001860 if (state != SRPT_STATE_DONE) {
1861 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1862 transport_generic_free_cmd(&ioctx->cmd, 0);
1863 } else {
1864 pr_err("IB completion has been received too late for"
1865 " wr_id = %u.\n", ioctx->ioctx.index);
1866 }
1867
1868out:
1869 while (!list_empty(&ch->cmd_wait_list) &&
1870 srpt_get_ch_state(ch) == CH_LIVE &&
1871 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001872 struct srpt_recv_ioctx *recv_ioctx;
1873
1874 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1875 struct srpt_recv_ioctx,
1876 wait_list);
1877 list_del(&recv_ioctx->wait_list);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001878 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001879 }
1880}
1881
Bart Van Asschea42d9852011-10-14 01:30:46 +00001882/**
1883 * srpt_create_ch_ib() - Create receive and send completion queues.
1884 */
1885static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1886{
1887 struct ib_qp_init_attr *qp_init;
1888 struct srpt_port *sport = ch->sport;
1889 struct srpt_device *sdev = sport->sdev;
1890 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1891 int ret;
1892
1893 WARN_ON(ch->rq_size < 1);
1894
1895 ret = -ENOMEM;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08001896 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001897 if (!qp_init)
1898 goto out;
1899
Bart Van Asscheab477c12014-10-19 18:05:33 +03001900retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001901 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1902 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001903 if (IS_ERR(ch->cq)) {
1904 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001905 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001906 ch->rq_size + srp_sq_size, ret);
1907 goto out;
1908 }
1909
1910 qp_init->qp_context = (void *)ch;
1911 qp_init->event_handler
1912 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1913 qp_init->send_cq = ch->cq;
1914 qp_init->recv_cq = ch->cq;
1915 qp_init->srq = sdev->srq;
1916 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1917 qp_init->qp_type = IB_QPT_RC;
1918 qp_init->cap.max_send_wr = srp_sq_size;
1919 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1920
1921 ch->qp = ib_create_qp(sdev->pd, qp_init);
1922 if (IS_ERR(ch->qp)) {
1923 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001924 if (ret == -ENOMEM) {
1925 srp_sq_size /= 2;
1926 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1927 ib_destroy_cq(ch->cq);
1928 goto retry;
1929 }
1930 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001931 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001932 goto err_destroy_cq;
1933 }
1934
1935 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1936
1937 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1938 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1939 qp_init->cap.max_send_wr, ch->cm_id);
1940
1941 ret = srpt_init_ch_qp(ch, ch->qp);
1942 if (ret)
1943 goto err_destroy_qp;
1944
Bart Van Asschea42d9852011-10-14 01:30:46 +00001945out:
1946 kfree(qp_init);
1947 return ret;
1948
1949err_destroy_qp:
1950 ib_destroy_qp(ch->qp);
1951err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001952 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001953 goto out;
1954}
1955
1956static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1957{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001958 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001959 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001960}
1961
1962/**
1963 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
1964 *
1965 * Reset the QP and make sure all resources associated with the channel will
1966 * be deallocated at an appropriate time.
1967 *
1968 * Note: The caller must hold ch->sport->sdev->spinlock.
1969 */
1970static void __srpt_close_ch(struct srpt_rdma_ch *ch)
1971{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001972 enum rdma_ch_state prev_state;
1973 unsigned long flags;
1974
Bart Van Asschea42d9852011-10-14 01:30:46 +00001975 spin_lock_irqsave(&ch->spinlock, flags);
1976 prev_state = ch->state;
1977 switch (prev_state) {
1978 case CH_CONNECTING:
1979 case CH_LIVE:
1980 ch->state = CH_DISCONNECTING;
1981 break;
1982 default:
1983 break;
1984 }
1985 spin_unlock_irqrestore(&ch->spinlock, flags);
1986
1987 switch (prev_state) {
1988 case CH_CONNECTING:
1989 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
1990 NULL, 0);
1991 /* fall through */
1992 case CH_LIVE:
1993 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001994 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001995 break;
1996 case CH_DISCONNECTING:
1997 break;
1998 case CH_DRAINING:
1999 case CH_RELEASING:
2000 break;
2001 }
2002}
2003
2004/**
2005 * srpt_close_ch() - Close an RDMA channel.
2006 */
2007static void srpt_close_ch(struct srpt_rdma_ch *ch)
2008{
2009 struct srpt_device *sdev;
2010
2011 sdev = ch->sport->sdev;
2012 spin_lock_irq(&sdev->spinlock);
2013 __srpt_close_ch(ch);
2014 spin_unlock_irq(&sdev->spinlock);
2015}
2016
2017/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002018 * srpt_shutdown_session() - Whether or not a session may be shut down.
2019 */
2020static int srpt_shutdown_session(struct se_session *se_sess)
2021{
2022 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2023 unsigned long flags;
2024
2025 spin_lock_irqsave(&ch->spinlock, flags);
2026 if (ch->in_shutdown) {
2027 spin_unlock_irqrestore(&ch->spinlock, flags);
2028 return true;
2029 }
2030
2031 ch->in_shutdown = true;
2032 target_sess_cmd_list_set_waiting(se_sess);
2033 spin_unlock_irqrestore(&ch->spinlock, flags);
2034
2035 return true;
2036}
2037
2038/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002039 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2040 * @cm_id: Pointer to the CM ID of the channel to be drained.
2041 *
2042 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2043 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2044 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2045 * waits until all target sessions for the associated IB device have been
2046 * unregistered and target session registration involves a call to
2047 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2048 * this function has finished).
2049 */
2050static void srpt_drain_channel(struct ib_cm_id *cm_id)
2051{
2052 struct srpt_device *sdev;
2053 struct srpt_rdma_ch *ch;
2054 int ret;
2055 bool do_reset = false;
2056
2057 WARN_ON_ONCE(irqs_disabled());
2058
2059 sdev = cm_id->context;
2060 BUG_ON(!sdev);
2061 spin_lock_irq(&sdev->spinlock);
2062 list_for_each_entry(ch, &sdev->rch_list, list) {
2063 if (ch->cm_id == cm_id) {
2064 do_reset = srpt_test_and_set_ch_state(ch,
2065 CH_CONNECTING, CH_DRAINING) ||
2066 srpt_test_and_set_ch_state(ch,
2067 CH_LIVE, CH_DRAINING) ||
2068 srpt_test_and_set_ch_state(ch,
2069 CH_DISCONNECTING, CH_DRAINING);
2070 break;
2071 }
2072 }
2073 spin_unlock_irq(&sdev->spinlock);
2074
2075 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002076 if (ch->sess)
2077 srpt_shutdown_session(ch->sess);
2078
Bart Van Asschea42d9852011-10-14 01:30:46 +00002079 ret = srpt_ch_qp_err(ch);
2080 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002081 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002082 " failed: %d\n", ret);
2083 }
2084}
2085
2086/**
2087 * srpt_find_channel() - Look up an RDMA channel.
2088 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2089 *
2090 * Return NULL if no matching RDMA channel has been found.
2091 */
2092static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2093 struct ib_cm_id *cm_id)
2094{
2095 struct srpt_rdma_ch *ch;
2096 bool found;
2097
2098 WARN_ON_ONCE(irqs_disabled());
2099 BUG_ON(!sdev);
2100
2101 found = false;
2102 spin_lock_irq(&sdev->spinlock);
2103 list_for_each_entry(ch, &sdev->rch_list, list) {
2104 if (ch->cm_id == cm_id) {
2105 found = true;
2106 break;
2107 }
2108 }
2109 spin_unlock_irq(&sdev->spinlock);
2110
2111 return found ? ch : NULL;
2112}
2113
2114/**
2115 * srpt_release_channel() - Release channel resources.
2116 *
2117 * Schedules the actual release because:
2118 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2119 * trigger a deadlock.
2120 * - It is not safe to call TCM transport_* functions from interrupt context.
2121 */
2122static void srpt_release_channel(struct srpt_rdma_ch *ch)
2123{
2124 schedule_work(&ch->release_work);
2125}
2126
2127static void srpt_release_channel_work(struct work_struct *w)
2128{
2129 struct srpt_rdma_ch *ch;
2130 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002131 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002132
2133 ch = container_of(w, struct srpt_rdma_ch, release_work);
2134 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2135 ch->release_done);
2136
2137 sdev = ch->sport->sdev;
2138 BUG_ON(!sdev);
2139
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002140 se_sess = ch->sess;
2141 BUG_ON(!se_sess);
2142
Joern Engelbe646c2d2013-05-15 00:44:07 -07002143 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002144
2145 transport_deregister_session_configfs(se_sess);
2146 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002147 ch->sess = NULL;
2148
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002149 ib_destroy_cm_id(ch->cm_id);
2150
Bart Van Asschea42d9852011-10-14 01:30:46 +00002151 srpt_destroy_ch_ib(ch);
2152
2153 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2154 ch->sport->sdev, ch->rq_size,
2155 ch->rsp_size, DMA_TO_DEVICE);
2156
2157 spin_lock_irq(&sdev->spinlock);
2158 list_del(&ch->list);
2159 spin_unlock_irq(&sdev->spinlock);
2160
Bart Van Asschea42d9852011-10-14 01:30:46 +00002161 if (ch->release_done)
2162 complete(ch->release_done);
2163
2164 wake_up(&sdev->ch_releaseQ);
2165
2166 kfree(ch);
2167}
2168
Bart Van Asschea42d9852011-10-14 01:30:46 +00002169/**
2170 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2171 *
2172 * Ownership of the cm_id is transferred to the target session if this
2173 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2174 */
2175static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2176 struct ib_cm_req_event_param *param,
2177 void *private_data)
2178{
2179 struct srpt_device *sdev = cm_id->context;
2180 struct srpt_port *sport = &sdev->port[param->port - 1];
2181 struct srp_login_req *req;
2182 struct srp_login_rsp *rsp;
2183 struct srp_login_rej *rej;
2184 struct ib_cm_rep_param *rep_param;
2185 struct srpt_rdma_ch *ch, *tmp_ch;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002186 struct se_node_acl *se_acl;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002187 u32 it_iu_len;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002188 int i, ret = 0;
2189 unsigned char *p;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002190
2191 WARN_ON_ONCE(irqs_disabled());
2192
2193 if (WARN_ON(!sdev || !private_data))
2194 return -EINVAL;
2195
2196 req = (struct srp_login_req *)private_data;
2197
2198 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2199
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002200 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2201 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2202 " (guid=0x%llx:0x%llx)\n",
2203 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2204 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2205 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2206 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2207 it_iu_len,
2208 param->port,
2209 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2210 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002211
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002212 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2213 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2214 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002215
2216 if (!rsp || !rej || !rep_param) {
2217 ret = -ENOMEM;
2218 goto out;
2219 }
2220
2221 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302222 rej->reason = cpu_to_be32(
2223 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002224 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002225 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002226 " length (%d bytes) is out of range (%d .. %d)\n",
2227 it_iu_len, 64, srp_max_req_size);
2228 goto reject;
2229 }
2230
2231 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302232 rej->reason = cpu_to_be32(
2233 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002234 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002235 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002236 " has not yet been enabled\n");
2237 goto reject;
2238 }
2239
2240 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2241 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2242
2243 spin_lock_irq(&sdev->spinlock);
2244
2245 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2246 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2247 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2248 && param->port == ch->sport->port
2249 && param->listen_id == ch->sport->sdev->cm_id
2250 && ch->cm_id) {
2251 enum rdma_ch_state ch_state;
2252
2253 ch_state = srpt_get_ch_state(ch);
2254 if (ch_state != CH_CONNECTING
2255 && ch_state != CH_LIVE)
2256 continue;
2257
2258 /* found an existing channel */
2259 pr_debug("Found existing channel %s"
2260 " cm_id= %p state= %d\n",
2261 ch->sess_name, ch->cm_id, ch_state);
2262
2263 __srpt_close_ch(ch);
2264
2265 rsp->rsp_flags =
2266 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2267 }
2268 }
2269
2270 spin_unlock_irq(&sdev->spinlock);
2271
2272 } else
2273 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2274
2275 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2276 || *(__be64 *)(req->target_port_id + 8) !=
2277 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302278 rej->reason = cpu_to_be32(
2279 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002280 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002281 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002282 " has an invalid target port identifier.\n");
2283 goto reject;
2284 }
2285
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002286 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002287 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302288 rej->reason = cpu_to_be32(
2289 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002290 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002291 ret = -ENOMEM;
2292 goto reject;
2293 }
2294
2295 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2296 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2297 memcpy(ch->t_port_id, req->target_port_id, 16);
2298 ch->sport = &sdev->port[param->port - 1];
2299 ch->cm_id = cm_id;
2300 /*
2301 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2302 * for the SRP protocol to the command queue size.
2303 */
2304 ch->rq_size = SRPT_RQ_SIZE;
2305 spin_lock_init(&ch->spinlock);
2306 ch->state = CH_CONNECTING;
2307 INIT_LIST_HEAD(&ch->cmd_wait_list);
2308 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2309
2310 ch->ioctx_ring = (struct srpt_send_ioctx **)
2311 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2312 sizeof(*ch->ioctx_ring[0]),
2313 ch->rsp_size, DMA_TO_DEVICE);
2314 if (!ch->ioctx_ring)
2315 goto free_ch;
2316
2317 INIT_LIST_HEAD(&ch->free_list);
2318 for (i = 0; i < ch->rq_size; i++) {
2319 ch->ioctx_ring[i]->ch = ch;
2320 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2321 }
2322
2323 ret = srpt_create_ch_ib(ch);
2324 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302325 rej->reason = cpu_to_be32(
2326 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002327 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002328 " a new RDMA channel failed.\n");
2329 goto free_ring;
2330 }
2331
2332 ret = srpt_ch_qp_rtr(ch, ch->qp);
2333 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302334 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002335 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002336 " RTR failed (error code = %d)\n", ret);
2337 goto destroy_ib;
2338 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002339
Bart Van Asschea42d9852011-10-14 01:30:46 +00002340 /*
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002341 * Use the initator port identifier as the session name, when
2342 * checking against se_node_acl->initiatorname[] this can be
2343 * with or without preceeding '0x'.
Bart Van Asschea42d9852011-10-14 01:30:46 +00002344 */
2345 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2346 be64_to_cpu(*(__be64 *)ch->i_port_id),
2347 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2348
2349 pr_debug("registering session %s\n", ch->sess_name);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002350 p = &ch->sess_name[0];
Bart Van Asschea42d9852011-10-14 01:30:46 +00002351
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002352 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002353 if (IS_ERR(ch->sess)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302354 rej->reason = cpu_to_be32(
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002355 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002356 pr_debug("Failed to create session\n");
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002357 goto destroy_ib;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002358 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002359
2360try_again:
2361 se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p);
2362 if (!se_acl) {
2363 pr_info("Rejected login because no ACL has been"
2364 " configured yet for initiator %s.\n", ch->sess_name);
2365 /*
2366 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2367 */
2368 if (p == &ch->sess_name[0]) {
2369 p += 2;
2370 goto try_again;
2371 }
2372 rej->reason = cpu_to_be32(
2373 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2374 transport_free_session(ch->sess);
2375 goto destroy_ib;
2376 }
2377 ch->sess->se_node_acl = se_acl;
2378
2379 transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002380
2381 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2382 ch->sess_name, ch->cm_id);
2383
2384 /* create srp_login_response */
2385 rsp->opcode = SRP_LOGIN_RSP;
2386 rsp->tag = req->tag;
2387 rsp->max_it_iu_len = req->req_it_iu_len;
2388 rsp->max_ti_iu_len = req->req_it_iu_len;
2389 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302390 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2391 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002392 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2393 atomic_set(&ch->req_lim, ch->rq_size);
2394 atomic_set(&ch->req_lim_delta, 0);
2395
2396 /* create cm reply */
2397 rep_param->qp_num = ch->qp->qp_num;
2398 rep_param->private_data = (void *)rsp;
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002399 rep_param->private_data_len = sizeof(*rsp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002400 rep_param->rnr_retry_count = 7;
2401 rep_param->flow_control = 1;
2402 rep_param->failover_accepted = 0;
2403 rep_param->srq = 1;
2404 rep_param->responder_resources = 4;
2405 rep_param->initiator_depth = 4;
2406
2407 ret = ib_send_cm_rep(cm_id, rep_param);
2408 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002409 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002410 " (error code = %d)\n", ret);
2411 goto release_channel;
2412 }
2413
2414 spin_lock_irq(&sdev->spinlock);
2415 list_add_tail(&ch->list, &sdev->rch_list);
2416 spin_unlock_irq(&sdev->spinlock);
2417
2418 goto out;
2419
2420release_channel:
2421 srpt_set_ch_state(ch, CH_RELEASING);
2422 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002423 transport_deregister_session(ch->sess);
2424 ch->sess = NULL;
2425
2426destroy_ib:
2427 srpt_destroy_ch_ib(ch);
2428
2429free_ring:
2430 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2431 ch->sport->sdev, ch->rq_size,
2432 ch->rsp_size, DMA_TO_DEVICE);
2433free_ch:
2434 kfree(ch);
2435
2436reject:
2437 rej->opcode = SRP_LOGIN_REJ;
2438 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302439 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2440 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002441
2442 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002443 (void *)rej, sizeof(*rej));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002444
2445out:
2446 kfree(rep_param);
2447 kfree(rsp);
2448 kfree(rej);
2449
2450 return ret;
2451}
2452
2453static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2454{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002455 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002456 srpt_drain_channel(cm_id);
2457}
2458
2459/**
2460 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2461 *
2462 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2463 * and that the recipient may begin transmitting (RTU = ready to use).
2464 */
2465static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2466{
2467 struct srpt_rdma_ch *ch;
2468 int ret;
2469
2470 ch = srpt_find_channel(cm_id->context, cm_id);
2471 BUG_ON(!ch);
2472
2473 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2474 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2475
2476 ret = srpt_ch_qp_rts(ch, ch->qp);
2477
2478 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2479 wait_list) {
2480 list_del(&ioctx->wait_list);
2481 srpt_handle_new_iu(ch, ioctx, NULL);
2482 }
2483 if (ret)
2484 srpt_close_ch(ch);
2485 }
2486}
2487
2488static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2489{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002490 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002491 srpt_drain_channel(cm_id);
2492}
2493
2494static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2495{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002496 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002497 srpt_drain_channel(cm_id);
2498}
2499
2500/**
2501 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2502 */
2503static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2504{
2505 struct srpt_rdma_ch *ch;
2506 unsigned long flags;
2507 bool send_drep = false;
2508
2509 ch = srpt_find_channel(cm_id->context, cm_id);
2510 BUG_ON(!ch);
2511
2512 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2513
2514 spin_lock_irqsave(&ch->spinlock, flags);
2515 switch (ch->state) {
2516 case CH_CONNECTING:
2517 case CH_LIVE:
2518 send_drep = true;
2519 ch->state = CH_DISCONNECTING;
2520 break;
2521 case CH_DISCONNECTING:
2522 case CH_DRAINING:
2523 case CH_RELEASING:
2524 WARN(true, "unexpected channel state %d\n", ch->state);
2525 break;
2526 }
2527 spin_unlock_irqrestore(&ch->spinlock, flags);
2528
2529 if (send_drep) {
2530 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002531 pr_err("Sending IB DREP failed.\n");
2532 pr_info("Received DREQ and sent DREP for session %s.\n",
2533 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002534 }
2535}
2536
2537/**
2538 * srpt_cm_drep_recv() - Process reception of a DREP message.
2539 */
2540static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2541{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002542 pr_info("Received InfiniBand DREP message 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_handler() - IB connection manager callback function.
2548 *
2549 * A non-zero return value will cause the caller destroy the CM ID.
2550 *
2551 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2552 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2553 * a non-zero value in any other case will trigger a race with the
2554 * ib_destroy_cm_id() call in srpt_release_channel().
2555 */
2556static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2557{
2558 int ret;
2559
2560 ret = 0;
2561 switch (event->event) {
2562 case IB_CM_REQ_RECEIVED:
2563 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2564 event->private_data);
2565 break;
2566 case IB_CM_REJ_RECEIVED:
2567 srpt_cm_rej_recv(cm_id);
2568 break;
2569 case IB_CM_RTU_RECEIVED:
2570 case IB_CM_USER_ESTABLISHED:
2571 srpt_cm_rtu_recv(cm_id);
2572 break;
2573 case IB_CM_DREQ_RECEIVED:
2574 srpt_cm_dreq_recv(cm_id);
2575 break;
2576 case IB_CM_DREP_RECEIVED:
2577 srpt_cm_drep_recv(cm_id);
2578 break;
2579 case IB_CM_TIMEWAIT_EXIT:
2580 srpt_cm_timewait_exit(cm_id);
2581 break;
2582 case IB_CM_REP_ERROR:
2583 srpt_cm_rep_error(cm_id);
2584 break;
2585 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002586 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002587 break;
2588 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002589 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002590 break;
2591 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002592 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002593 break;
2594 }
2595
2596 return ret;
2597}
2598
2599/**
2600 * srpt_perform_rdmas() - Perform IB RDMA.
2601 *
2602 * Returns zero upon success or a negative number upon failure.
2603 */
2604static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2605 struct srpt_send_ioctx *ioctx)
2606{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002607 struct ib_send_wr *bad_wr;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002608 int sq_wr_avail, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002609 enum dma_data_direction dir;
2610 const int n_rdma = ioctx->n_rdma;
2611
2612 dir = ioctx->cmd.data_direction;
2613 if (dir == DMA_TO_DEVICE) {
2614 /* write */
2615 ret = -ENOMEM;
2616 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2617 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002618 pr_warn("IB send queue full (needed %d)\n",
2619 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002620 goto out;
2621 }
2622 }
2623
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002624 for (i = 0; i < n_rdma; i++) {
2625 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002626
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002627 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2628 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2629
2630 if (i == n_rdma - 1) {
2631 /* only get completion event for the last rdma read */
2632 if (dir == DMA_TO_DEVICE) {
2633 wr->send_flags = IB_SEND_SIGNALED;
2634 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2635 } else {
2636 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2637 }
2638 wr->wr_cqe = &ioctx->rdma_cqe;
2639 wr->next = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002640 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002641 wr->wr_cqe = NULL;
2642 wr->next = &ioctx->rdma_wrs[i + 1].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002643 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002644 }
2645
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002646 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002647 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002648 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002649 __func__, __LINE__, ret, i, n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002650out:
2651 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2652 atomic_add(n_rdma, &ch->sq_wr_avail);
2653 return ret;
2654}
2655
2656/**
2657 * srpt_xfer_data() - Start data transfer from initiator to target.
2658 */
2659static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2660 struct srpt_send_ioctx *ioctx)
2661{
2662 int ret;
2663
2664 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2665 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002666 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002667 goto out;
2668 }
2669
2670 ret = srpt_perform_rdmas(ch, ioctx);
2671 if (ret) {
2672 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002673 pr_info("%s[%d] queue full -- ret=%d\n",
2674 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002675 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002676 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002677 __func__, __LINE__, ret);
2678 goto out_unmap;
2679 }
2680
2681out:
2682 return ret;
2683out_unmap:
2684 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2685 goto out;
2686}
2687
2688static int srpt_write_pending_status(struct se_cmd *se_cmd)
2689{
2690 struct srpt_send_ioctx *ioctx;
2691
2692 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2693 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2694}
2695
2696/*
2697 * srpt_write_pending() - Start data transfer from initiator to target (write).
2698 */
2699static int srpt_write_pending(struct se_cmd *se_cmd)
2700{
2701 struct srpt_rdma_ch *ch;
2702 struct srpt_send_ioctx *ioctx;
2703 enum srpt_command_state new_state;
2704 enum rdma_ch_state ch_state;
2705 int ret;
2706
2707 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2708
2709 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2710 WARN_ON(new_state == SRPT_STATE_DONE);
2711
2712 ch = ioctx->ch;
2713 BUG_ON(!ch);
2714
2715 ch_state = srpt_get_ch_state(ch);
2716 switch (ch_state) {
2717 case CH_CONNECTING:
2718 WARN(true, "unexpected channel state %d\n", ch_state);
2719 ret = -EINVAL;
2720 goto out;
2721 case CH_LIVE:
2722 break;
2723 case CH_DISCONNECTING:
2724 case CH_DRAINING:
2725 case CH_RELEASING:
2726 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002727 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002728 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2729 ret = -EINVAL;
2730 goto out;
2731 }
2732 ret = srpt_xfer_data(ch, ioctx);
2733
2734out:
2735 return ret;
2736}
2737
2738static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2739{
2740 switch (tcm_mgmt_status) {
2741 case TMR_FUNCTION_COMPLETE:
2742 return SRP_TSK_MGMT_SUCCESS;
2743 case TMR_FUNCTION_REJECTED:
2744 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2745 }
2746 return SRP_TSK_MGMT_FAILED;
2747}
2748
2749/**
2750 * srpt_queue_response() - Transmits the response to a SCSI command.
2751 *
2752 * Callback function called by the TCM core. Must not block since it can be
2753 * invoked on the context of the IB completion handler.
2754 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002755static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002756{
2757 struct srpt_rdma_ch *ch;
2758 struct srpt_send_ioctx *ioctx;
2759 enum srpt_command_state state;
2760 unsigned long flags;
2761 int ret;
2762 enum dma_data_direction dir;
2763 int resp_len;
2764 u8 srp_tm_status;
2765
Bart Van Asschea42d9852011-10-14 01:30:46 +00002766 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2767 ch = ioctx->ch;
2768 BUG_ON(!ch);
2769
2770 spin_lock_irqsave(&ioctx->spinlock, flags);
2771 state = ioctx->state;
2772 switch (state) {
2773 case SRPT_STATE_NEW:
2774 case SRPT_STATE_DATA_IN:
2775 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2776 break;
2777 case SRPT_STATE_MGMT:
2778 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2779 break;
2780 default:
2781 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2782 ch, ioctx->ioctx.index, ioctx->state);
2783 break;
2784 }
2785 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2786
2787 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2788 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2789 atomic_inc(&ch->req_lim_delta);
2790 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04002791 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002792 }
2793
2794 dir = ioctx->cmd.data_direction;
2795
2796 /* For read commands, transfer the data to the initiator. */
2797 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2798 !ioctx->queue_status_only) {
2799 ret = srpt_xfer_data(ch, ioctx);
2800 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002801 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002802 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04002803 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002804 }
2805 }
2806
2807 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002808 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002809 cmd->scsi_status);
2810 else {
2811 srp_tm_status
2812 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2813 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002814 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002815 }
2816 ret = srpt_post_send(ch, ioctx, resp_len);
2817 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002818 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002819 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002820 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2821 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02002822 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002823 }
Joern Engelb79fafa2013-07-03 11:22:17 -04002824}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002825
Joern Engelb79fafa2013-07-03 11:22:17 -04002826static int srpt_queue_data_in(struct se_cmd *cmd)
2827{
2828 srpt_queue_response(cmd);
2829 return 0;
2830}
2831
2832static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2833{
2834 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002835}
2836
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002837static void srpt_aborted_task(struct se_cmd *cmd)
2838{
2839 struct srpt_send_ioctx *ioctx = container_of(cmd,
2840 struct srpt_send_ioctx, cmd);
2841
2842 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2843}
2844
Bart Van Asschea42d9852011-10-14 01:30:46 +00002845static int srpt_queue_status(struct se_cmd *cmd)
2846{
2847 struct srpt_send_ioctx *ioctx;
2848
2849 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2850 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2851 if (cmd->se_cmd_flags &
2852 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2853 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2854 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002855 srpt_queue_response(cmd);
2856 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002857}
2858
2859static void srpt_refresh_port_work(struct work_struct *work)
2860{
2861 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2862
2863 srpt_refresh_port(sport);
2864}
2865
2866static int srpt_ch_list_empty(struct srpt_device *sdev)
2867{
2868 int res;
2869
2870 spin_lock_irq(&sdev->spinlock);
2871 res = list_empty(&sdev->rch_list);
2872 spin_unlock_irq(&sdev->spinlock);
2873
2874 return res;
2875}
2876
2877/**
2878 * srpt_release_sdev() - Free the channel resources associated with a target.
2879 */
2880static int srpt_release_sdev(struct srpt_device *sdev)
2881{
2882 struct srpt_rdma_ch *ch, *tmp_ch;
2883 int res;
2884
2885 WARN_ON_ONCE(irqs_disabled());
2886
2887 BUG_ON(!sdev);
2888
2889 spin_lock_irq(&sdev->spinlock);
2890 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2891 __srpt_close_ch(ch);
2892 spin_unlock_irq(&sdev->spinlock);
2893
2894 res = wait_event_interruptible(sdev->ch_releaseQ,
2895 srpt_ch_list_empty(sdev));
2896 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002897 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002898
2899 return 0;
2900}
2901
2902static struct srpt_port *__srpt_lookup_port(const char *name)
2903{
2904 struct ib_device *dev;
2905 struct srpt_device *sdev;
2906 struct srpt_port *sport;
2907 int i;
2908
2909 list_for_each_entry(sdev, &srpt_dev_list, list) {
2910 dev = sdev->device;
2911 if (!dev)
2912 continue;
2913
2914 for (i = 0; i < dev->phys_port_cnt; i++) {
2915 sport = &sdev->port[i];
2916
2917 if (!strcmp(sport->port_guid, name))
2918 return sport;
2919 }
2920 }
2921
2922 return NULL;
2923}
2924
2925static struct srpt_port *srpt_lookup_port(const char *name)
2926{
2927 struct srpt_port *sport;
2928
2929 spin_lock(&srpt_dev_lock);
2930 sport = __srpt_lookup_port(name);
2931 spin_unlock(&srpt_dev_lock);
2932
2933 return sport;
2934}
2935
2936/**
2937 * srpt_add_one() - Infiniband device addition callback function.
2938 */
2939static void srpt_add_one(struct ib_device *device)
2940{
2941 struct srpt_device *sdev;
2942 struct srpt_port *sport;
2943 struct ib_srq_init_attr srq_attr;
2944 int i;
2945
2946 pr_debug("device = %p, device->dma_ops = %p\n", device,
2947 device->dma_ops);
2948
Bart Van Assche9d2aa2b2016-02-11 11:03:31 -08002949 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002950 if (!sdev)
2951 goto err;
2952
2953 sdev->device = device;
2954 INIT_LIST_HEAD(&sdev->rch_list);
2955 init_waitqueue_head(&sdev->ch_releaseQ);
2956 spin_lock_init(&sdev->spinlock);
2957
Bart Van Asschea42d9852011-10-14 01:30:46 +00002958 sdev->pd = ib_alloc_pd(device);
2959 if (IS_ERR(sdev->pd))
2960 goto free_dev;
2961
Or Gerlitz4a061b22015-12-18 10:59:46 +02002962 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002963
2964 srq_attr.event_handler = srpt_srq_event;
2965 srq_attr.srq_context = (void *)sdev;
2966 srq_attr.attr.max_wr = sdev->srq_size;
2967 srq_attr.attr.max_sge = 1;
2968 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07002969 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002970
2971 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2972 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06002973 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002974
2975 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
Or Gerlitz4a061b22015-12-18 10:59:46 +02002976 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002977 device->name);
2978
2979 if (!srpt_service_guid)
2980 srpt_service_guid = be64_to_cpu(device->node_guid);
2981
2982 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2983 if (IS_ERR(sdev->cm_id))
2984 goto err_srq;
2985
2986 /* print out target login information */
2987 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2988 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2989 srpt_service_guid, srpt_service_guid);
2990
2991 /*
2992 * We do not have a consistent service_id (ie. also id_ext of target_id)
2993 * to identify this target. We currently use the guid of the first HCA
2994 * in the system as service_id; therefore, the target_id will change
2995 * if this HCA is gone bad and replaced by different HCA
2996 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03002997 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00002998 goto err_cm;
2999
3000 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3001 srpt_event_handler);
3002 if (ib_register_event_handler(&sdev->event_handler))
3003 goto err_cm;
3004
3005 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3006 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3007 sizeof(*sdev->ioctx_ring[0]),
3008 srp_max_req_size, DMA_FROM_DEVICE);
3009 if (!sdev->ioctx_ring)
3010 goto err_event;
3011
3012 for (i = 0; i < sdev->srq_size; ++i)
3013 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3014
Roland Dreierf2250662012-02-02 12:55:58 -08003015 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00003016
3017 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3018 sport = &sdev->port[i - 1];
3019 sport->sdev = sdev;
3020 sport->port = i;
3021 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3022 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3023 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3024 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003025
3026 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003027 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003028 srpt_sdev_name(sdev), i);
3029 goto err_ring;
3030 }
3031 snprintf(sport->port_guid, sizeof(sport->port_guid),
3032 "0x%016llx%016llx",
3033 be64_to_cpu(sport->gid.global.subnet_prefix),
3034 be64_to_cpu(sport->gid.global.interface_id));
3035 }
3036
3037 spin_lock(&srpt_dev_lock);
3038 list_add_tail(&sdev->list, &srpt_dev_list);
3039 spin_unlock(&srpt_dev_lock);
3040
3041out:
3042 ib_set_client_data(device, &srpt_client, sdev);
3043 pr_debug("added %s.\n", device->name);
3044 return;
3045
3046err_ring:
3047 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3048 sdev->srq_size, srp_max_req_size,
3049 DMA_FROM_DEVICE);
3050err_event:
3051 ib_unregister_event_handler(&sdev->event_handler);
3052err_cm:
3053 ib_destroy_cm_id(sdev->cm_id);
3054err_srq:
3055 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003056err_pd:
3057 ib_dealloc_pd(sdev->pd);
3058free_dev:
3059 kfree(sdev);
3060err:
3061 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003062 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003063 goto out;
3064}
3065
3066/**
3067 * srpt_remove_one() - InfiniBand device removal callback function.
3068 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03003069static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003070{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003071 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003072 int i;
3073
Bart Van Asschea42d9852011-10-14 01:30:46 +00003074 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003075 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003076 return;
3077 }
3078
3079 srpt_unregister_mad_agent(sdev);
3080
3081 ib_unregister_event_handler(&sdev->event_handler);
3082
3083 /* Cancel any work queued by the just unregistered IB event handler. */
3084 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3085 cancel_work_sync(&sdev->port[i].work);
3086
3087 ib_destroy_cm_id(sdev->cm_id);
3088
3089 /*
3090 * Unregistering a target must happen after destroying sdev->cm_id
3091 * such that no new SRP_LOGIN_REQ information units can arrive while
3092 * destroying the target.
3093 */
3094 spin_lock(&srpt_dev_lock);
3095 list_del(&sdev->list);
3096 spin_unlock(&srpt_dev_lock);
3097 srpt_release_sdev(sdev);
3098
3099 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003100 ib_dealloc_pd(sdev->pd);
3101
3102 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3103 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3104 sdev->ioctx_ring = NULL;
3105 kfree(sdev);
3106}
3107
3108static struct ib_client srpt_client = {
3109 .name = DRV_NAME,
3110 .add = srpt_add_one,
3111 .remove = srpt_remove_one
3112};
3113
3114static int srpt_check_true(struct se_portal_group *se_tpg)
3115{
3116 return 1;
3117}
3118
3119static int srpt_check_false(struct se_portal_group *se_tpg)
3120{
3121 return 0;
3122}
3123
3124static char *srpt_get_fabric_name(void)
3125{
3126 return "srpt";
3127}
3128
Bart Van Asschea42d9852011-10-14 01:30:46 +00003129static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3130{
3131 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3132
3133 return sport->port_guid;
3134}
3135
3136static u16 srpt_get_tag(struct se_portal_group *tpg)
3137{
3138 return 1;
3139}
3140
Bart Van Asschea42d9852011-10-14 01:30:46 +00003141static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3142{
3143 return 1;
3144}
3145
3146static void srpt_release_cmd(struct se_cmd *se_cmd)
3147{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003148 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3149 struct srpt_send_ioctx, cmd);
3150 struct srpt_rdma_ch *ch = ioctx->ch;
3151 unsigned long flags;
3152
3153 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3154 WARN_ON(ioctx->mapped_sg_count != 0);
3155
3156 if (ioctx->n_rbuf > 1) {
3157 kfree(ioctx->rbufs);
3158 ioctx->rbufs = NULL;
3159 ioctx->n_rbuf = 0;
3160 }
3161
3162 spin_lock_irqsave(&ch->spinlock, flags);
3163 list_add(&ioctx->free_list, &ch->free_list);
3164 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003165}
3166
3167/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003168 * srpt_close_session() - Forcibly close a session.
3169 *
3170 * Callback function invoked by the TCM core to clean up sessions associated
3171 * with a node ACL when the user invokes
3172 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3173 */
3174static void srpt_close_session(struct se_session *se_sess)
3175{
3176 DECLARE_COMPLETION_ONSTACK(release_done);
3177 struct srpt_rdma_ch *ch;
3178 struct srpt_device *sdev;
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003179 unsigned long res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003180
3181 ch = se_sess->fabric_sess_ptr;
3182 WARN_ON(ch->sess != se_sess);
3183
3184 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3185
3186 sdev = ch->sport->sdev;
3187 spin_lock_irq(&sdev->spinlock);
3188 BUG_ON(ch->release_done);
3189 ch->release_done = &release_done;
3190 __srpt_close_ch(ch);
3191 spin_unlock_irq(&sdev->spinlock);
3192
3193 res = wait_for_completion_timeout(&release_done, 60 * HZ);
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003194 WARN_ON(res == 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003195}
3196
3197/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003198 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3199 *
3200 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3201 * This object represents an arbitrary integer used to uniquely identify a
3202 * particular attached remote initiator port to a particular SCSI target port
3203 * within a particular SCSI target device within a particular SCSI instance.
3204 */
3205static u32 srpt_sess_get_index(struct se_session *se_sess)
3206{
3207 return 0;
3208}
3209
3210static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3211{
3212}
3213
Bart Van Asschea42d9852011-10-14 01:30:46 +00003214/* Note: only used from inside debug printk's by the TCM core. */
3215static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3216{
3217 struct srpt_send_ioctx *ioctx;
3218
3219 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3220 return srpt_get_cmd_state(ioctx);
3221}
3222
Bart Van Asschea42d9852011-10-14 01:30:46 +00003223/**
3224 * srpt_parse_i_port_id() - Parse an initiator port ID.
3225 * @name: ASCII representation of a 128-bit initiator port ID.
3226 * @i_port_id: Binary 128-bit port ID.
3227 */
3228static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3229{
3230 const char *p;
3231 unsigned len, count, leading_zero_bytes;
3232 int ret, rc;
3233
3234 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003235 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003236 p += 2;
3237 ret = -EINVAL;
3238 len = strlen(p);
3239 if (len % 2)
3240 goto out;
3241 count = min(len / 2, 16U);
3242 leading_zero_bytes = 16 - count;
3243 memset(i_port_id, 0, leading_zero_bytes);
3244 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3245 if (rc < 0)
3246 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3247 ret = 0;
3248out:
3249 return ret;
3250}
3251
3252/*
3253 * configfs callback function invoked for
3254 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3255 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003256static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003257{
Bart Van Asschea42d9852011-10-14 01:30:46 +00003258 u8 i_port_id[16];
3259
3260 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003261 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003262 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003263 }
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003264 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003265}
3266
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003267static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3268 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003269{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003270 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003271 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3272
3273 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3274}
3275
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003276static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3277 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003278{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003279 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003280 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3281 unsigned long val;
3282 int ret;
3283
Jingoo Han9d8abf42014-02-05 11:22:05 +09003284 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003285 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003286 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003287 return -EINVAL;
3288 }
3289 if (val > MAX_SRPT_RDMA_SIZE) {
3290 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3291 MAX_SRPT_RDMA_SIZE);
3292 return -EINVAL;
3293 }
3294 if (val < DEFAULT_MAX_RDMA_SIZE) {
3295 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3296 val, DEFAULT_MAX_RDMA_SIZE);
3297 return -EINVAL;
3298 }
3299 sport->port_attrib.srp_max_rdma_size = val;
3300
3301 return count;
3302}
3303
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003304static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3305 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003306{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003307 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003308 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3309
3310 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3311}
3312
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003313static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3314 const char *page, size_t count)
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 unsigned long val;
3319 int ret;
3320
Jingoo Han9d8abf42014-02-05 11:22:05 +09003321 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003322 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003323 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003324 return -EINVAL;
3325 }
3326 if (val > MAX_SRPT_RSP_SIZE) {
3327 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3328 MAX_SRPT_RSP_SIZE);
3329 return -EINVAL;
3330 }
3331 if (val < MIN_MAX_RSP_SIZE) {
3332 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3333 MIN_MAX_RSP_SIZE);
3334 return -EINVAL;
3335 }
3336 sport->port_attrib.srp_max_rsp_size = val;
3337
3338 return count;
3339}
3340
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003341static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3342 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003343{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003344 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003345 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3346
3347 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3348}
3349
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003350static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3351 const char *page, size_t count)
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 unsigned long val;
3356 int ret;
3357
Jingoo Han9d8abf42014-02-05 11:22:05 +09003358 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003359 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003360 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003361 return -EINVAL;
3362 }
3363 if (val > MAX_SRPT_SRQ_SIZE) {
3364 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3365 MAX_SRPT_SRQ_SIZE);
3366 return -EINVAL;
3367 }
3368 if (val < MIN_SRPT_SRQ_SIZE) {
3369 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3370 MIN_SRPT_SRQ_SIZE);
3371 return -EINVAL;
3372 }
3373 sport->port_attrib.srp_sq_size = val;
3374
3375 return count;
3376}
3377
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003378CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3379CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3380CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003381
3382static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003383 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3384 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3385 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003386 NULL,
3387};
3388
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003389static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003390{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003391 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003392 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3393
3394 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3395}
3396
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003397static ssize_t srpt_tpg_enable_store(struct config_item *item,
3398 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003399{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003400 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003401 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3402 unsigned long tmp;
3403 int ret;
3404
Jingoo Han9d8abf42014-02-05 11:22:05 +09003405 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003406 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003407 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003408 return -EINVAL;
3409 }
3410
3411 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003412 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003413 return -EINVAL;
3414 }
3415 if (tmp == 1)
3416 sport->enabled = true;
3417 else
3418 sport->enabled = false;
3419
3420 return count;
3421}
3422
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003423CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003424
3425static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003426 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003427 NULL,
3428};
3429
3430/**
3431 * configfs callback invoked for
3432 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3433 */
3434static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3435 struct config_group *group,
3436 const char *name)
3437{
3438 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3439 int res;
3440
3441 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003442 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003443 if (res)
3444 return ERR_PTR(res);
3445
3446 return &sport->port_tpg_1;
3447}
3448
3449/**
3450 * configfs callback invoked for
3451 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3452 */
3453static void srpt_drop_tpg(struct se_portal_group *tpg)
3454{
3455 struct srpt_port *sport = container_of(tpg,
3456 struct srpt_port, port_tpg_1);
3457
3458 sport->enabled = false;
3459 core_tpg_deregister(&sport->port_tpg_1);
3460}
3461
3462/**
3463 * configfs callback invoked for
3464 * mkdir /sys/kernel/config/target/$driver/$port
3465 */
3466static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3467 struct config_group *group,
3468 const char *name)
3469{
3470 struct srpt_port *sport;
3471 int ret;
3472
3473 sport = srpt_lookup_port(name);
3474 pr_debug("make_tport(%s)\n", name);
3475 ret = -EINVAL;
3476 if (!sport)
3477 goto err;
3478
3479 return &sport->port_wwn;
3480
3481err:
3482 return ERR_PTR(ret);
3483}
3484
3485/**
3486 * configfs callback invoked for
3487 * rmdir /sys/kernel/config/target/$driver/$port
3488 */
3489static void srpt_drop_tport(struct se_wwn *wwn)
3490{
3491 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3492
3493 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3494}
3495
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003496static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003497{
3498 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3499}
3500
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003501CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003502
3503static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003504 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003505 NULL,
3506};
3507
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003508static const struct target_core_fabric_ops srpt_template = {
3509 .module = THIS_MODULE,
3510 .name = "srpt",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003511 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003512 .tpg_get_wwn = srpt_get_fabric_wwn,
3513 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003514 .tpg_check_demo_mode = srpt_check_false,
3515 .tpg_check_demo_mode_cache = srpt_check_true,
3516 .tpg_check_demo_mode_write_protect = srpt_check_true,
3517 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003518 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3519 .release_cmd = srpt_release_cmd,
3520 .check_stop_free = srpt_check_stop_free,
3521 .shutdown_session = srpt_shutdown_session,
3522 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003523 .sess_get_index = srpt_sess_get_index,
3524 .sess_get_initiator_sid = NULL,
3525 .write_pending = srpt_write_pending,
3526 .write_pending_status = srpt_write_pending_status,
3527 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003528 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003529 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003530 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003531 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003532 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003533 /*
3534 * Setup function pointers for generic logic in
3535 * target_core_fabric_configfs.c
3536 */
3537 .fabric_make_wwn = srpt_make_tport,
3538 .fabric_drop_wwn = srpt_drop_tport,
3539 .fabric_make_tpg = srpt_make_tpg,
3540 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003541 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003542
3543 .tfc_wwn_attrs = srpt_wwn_attrs,
3544 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3545 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003546};
3547
3548/**
3549 * srpt_init_module() - Kernel module initialization.
3550 *
3551 * Note: Since ib_register_client() registers callback functions, and since at
3552 * least one of these callback functions (srpt_add_one()) calls target core
3553 * functions, this driver must be registered with the target core before
3554 * ib_register_client() is called.
3555 */
3556static int __init srpt_init_module(void)
3557{
3558 int ret;
3559
3560 ret = -EINVAL;
3561 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003562 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003563 " srp_max_req_size -- must be at least %d.\n",
3564 srp_max_req_size, MIN_MAX_REQ_SIZE);
3565 goto out;
3566 }
3567
3568 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3569 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003570 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003571 " srpt_srq_size -- must be in the range [%d..%d].\n",
3572 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3573 goto out;
3574 }
3575
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003576 ret = target_register_template(&srpt_template);
3577 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003578 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003579
3580 ret = ib_register_client(&srpt_client);
3581 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003582 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003583 goto out_unregister_target;
3584 }
3585
3586 return 0;
3587
3588out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003589 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003590out:
3591 return ret;
3592}
3593
3594static void __exit srpt_cleanup_module(void)
3595{
3596 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003597 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003598}
3599
3600module_init(srpt_init_module);
3601module_exit(srpt_cleanup_module);