blob: 6d1a1379478cabd52f6cb81ca8597868db8168a1 [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
Bart Van Asschea42d9852011-10-14 01:30:46 +0000112static enum rdma_ch_state
113srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
114{
115 unsigned long flags;
116 enum rdma_ch_state prev;
117
118 spin_lock_irqsave(&ch->spinlock, flags);
119 prev = ch->state;
120 ch->state = new_state;
121 spin_unlock_irqrestore(&ch->spinlock, flags);
122 return prev;
123}
124
125/**
126 * srpt_test_and_set_ch_state() - Test and set the channel state.
127 *
128 * Returns true if and only if the channel state has been set to the new state.
129 */
130static bool
131srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
132 enum rdma_ch_state new)
133{
134 unsigned long flags;
135 enum rdma_ch_state prev;
136
137 spin_lock_irqsave(&ch->spinlock, flags);
138 prev = ch->state;
139 if (prev == old)
140 ch->state = new;
141 spin_unlock_irqrestore(&ch->spinlock, flags);
142 return prev == old;
143}
144
145/**
146 * srpt_event_handler() - Asynchronous IB event callback function.
147 *
148 * Callback function called by the InfiniBand core when an asynchronous IB
149 * event occurs. This callback may occur in interrupt context. See also
150 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
151 * Architecture Specification.
152 */
153static void srpt_event_handler(struct ib_event_handler *handler,
154 struct ib_event *event)
155{
156 struct srpt_device *sdev;
157 struct srpt_port *sport;
158
159 sdev = ib_get_client_data(event->device, &srpt_client);
160 if (!sdev || sdev->device != event->device)
161 return;
162
163 pr_debug("ASYNC event= %d on device= %s\n", event->event,
Bart Van Asschef68cba4e92016-02-11 11:04:20 -0800164 sdev->device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000165
166 switch (event->event) {
167 case IB_EVENT_PORT_ERR:
168 if (event->element.port_num <= sdev->device->phys_port_cnt) {
169 sport = &sdev->port[event->element.port_num - 1];
170 sport->lid = 0;
171 sport->sm_lid = 0;
172 }
173 break;
174 case IB_EVENT_PORT_ACTIVE:
175 case IB_EVENT_LID_CHANGE:
176 case IB_EVENT_PKEY_CHANGE:
177 case IB_EVENT_SM_CHANGE:
178 case IB_EVENT_CLIENT_REREGISTER:
Doug Ledford2aa1cf62014-08-12 19:20:10 -0400179 case IB_EVENT_GID_CHANGE:
Bart Van Asschea42d9852011-10-14 01:30:46 +0000180 /* Refresh port data asynchronously. */
181 if (event->element.port_num <= sdev->device->phys_port_cnt) {
182 sport = &sdev->port[event->element.port_num - 1];
183 if (!sport->lid && !sport->sm_lid)
184 schedule_work(&sport->work);
185 }
186 break;
187 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400188 pr_err("received unrecognized IB event %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +0000189 event->event);
190 break;
191 }
192}
193
194/**
195 * srpt_srq_event() - SRQ event callback function.
196 */
197static void srpt_srq_event(struct ib_event *event, void *ctx)
198{
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400199 pr_info("SRQ event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000200}
201
202/**
203 * srpt_qp_event() - QP event callback function.
204 */
205static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
206{
207 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
Bart Van Assche33912d72016-02-11 11:04:43 -0800208 event->event, ch->cm_id, ch->sess_name, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000209
210 switch (event->event) {
211 case IB_EVENT_COMM_EST:
212 ib_cm_notify(ch->cm_id, event->event);
213 break;
214 case IB_EVENT_QP_LAST_WQE_REACHED:
215 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
216 CH_RELEASING))
217 srpt_release_channel(ch);
218 else
219 pr_debug("%s: state %d - ignored LAST_WQE.\n",
Bart Van Assche33912d72016-02-11 11:04:43 -0800220 ch->sess_name, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000221 break;
222 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400223 pr_err("received unrecognized IB QP event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000224 break;
225 }
226}
227
228/**
229 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
230 *
231 * @slot: one-based slot number.
232 * @value: four-bit value.
233 *
234 * Copies the lowest four bits of value in element slot of the array of four
235 * bit elements called c_list (controller list). The index slot is one-based.
236 */
237static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
238{
239 u16 id;
240 u8 tmp;
241
242 id = (slot - 1) / 2;
243 if (slot & 0x1) {
244 tmp = c_list[id] & 0xf;
245 c_list[id] = (value << 4) | tmp;
246 } else {
247 tmp = c_list[id] & 0xf0;
248 c_list[id] = (value & 0xf) | tmp;
249 }
250}
251
252/**
253 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
254 *
255 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
256 * Specification.
257 */
258static void srpt_get_class_port_info(struct ib_dm_mad *mad)
259{
260 struct ib_class_port_info *cif;
261
262 cif = (struct ib_class_port_info *)mad->data;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800263 memset(cif, 0, sizeof(*cif));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000264 cif->base_version = 1;
265 cif->class_version = 1;
266 cif->resp_time_value = 20;
267
268 mad->mad_hdr.status = 0;
269}
270
271/**
272 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
273 *
274 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
275 * Specification. See also section B.7, table B.6 in the SRP r16a document.
276 */
277static void srpt_get_iou(struct ib_dm_mad *mad)
278{
279 struct ib_dm_iou_info *ioui;
280 u8 slot;
281 int i;
282
283 ioui = (struct ib_dm_iou_info *)mad->data;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530284 ioui->change_id = cpu_to_be16(1);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000285 ioui->max_controllers = 16;
286
287 /* set present for slot 1 and empty for the rest */
288 srpt_set_ioc(ioui->controller_list, 1, 1);
289 for (i = 1, slot = 2; i < 16; i++, slot++)
290 srpt_set_ioc(ioui->controller_list, slot, 0);
291
292 mad->mad_hdr.status = 0;
293}
294
295/**
296 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
297 *
298 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
299 * Architecture Specification. See also section B.7, table B.7 in the SRP
300 * r16a document.
301 */
302static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
303 struct ib_dm_mad *mad)
304{
305 struct srpt_device *sdev = sport->sdev;
306 struct ib_dm_ioc_profile *iocp;
307
308 iocp = (struct ib_dm_ioc_profile *)mad->data;
309
310 if (!slot || slot > 16) {
311 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530312 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000313 return;
314 }
315
316 if (slot > 2) {
317 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530318 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000319 return;
320 }
321
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800322 memset(iocp, 0, sizeof(*iocp));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000323 strcpy(iocp->id_string, SRPT_ID_STRING);
324 iocp->guid = cpu_to_be64(srpt_service_guid);
Or Gerlitz4a061b22015-12-18 10:59:46 +0200325 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
326 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
327 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
328 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000329 iocp->subsys_device_id = 0x0;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530330 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
331 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
332 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
333 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000334 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
335 iocp->rdma_read_depth = 4;
336 iocp->send_size = cpu_to_be32(srp_max_req_size);
337 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
338 1U << 24));
339 iocp->num_svc_entries = 1;
340 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
341 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
342
343 mad->mad_hdr.status = 0;
344}
345
346/**
347 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
348 *
349 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
350 * Specification. See also section B.7, table B.8 in the SRP r16a document.
351 */
352static void srpt_get_svc_entries(u64 ioc_guid,
353 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
354{
355 struct ib_dm_svc_entries *svc_entries;
356
357 WARN_ON(!ioc_guid);
358
359 if (!slot || slot > 16) {
360 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530361 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000362 return;
363 }
364
365 if (slot > 2 || lo > hi || hi > 1) {
366 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530367 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000368 return;
369 }
370
371 svc_entries = (struct ib_dm_svc_entries *)mad->data;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800372 memset(svc_entries, 0, sizeof(*svc_entries));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000373 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
374 snprintf(svc_entries->service_entries[0].name,
375 sizeof(svc_entries->service_entries[0].name),
376 "%s%016llx",
377 SRP_SERVICE_NAME_PREFIX,
378 ioc_guid);
379
380 mad->mad_hdr.status = 0;
381}
382
383/**
384 * srpt_mgmt_method_get() - Process a received management datagram.
385 * @sp: source port through which the MAD has been received.
386 * @rq_mad: received MAD.
387 * @rsp_mad: response MAD.
388 */
389static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
390 struct ib_dm_mad *rsp_mad)
391{
392 u16 attr_id;
393 u32 slot;
394 u8 hi, lo;
395
396 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
397 switch (attr_id) {
398 case DM_ATTR_CLASS_PORT_INFO:
399 srpt_get_class_port_info(rsp_mad);
400 break;
401 case DM_ATTR_IOU_INFO:
402 srpt_get_iou(rsp_mad);
403 break;
404 case DM_ATTR_IOC_PROFILE:
405 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
406 srpt_get_ioc(sp, slot, rsp_mad);
407 break;
408 case DM_ATTR_SVC_ENTRIES:
409 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
410 hi = (u8) ((slot >> 8) & 0xff);
411 lo = (u8) (slot & 0xff);
412 slot = (u16) ((slot >> 16) & 0xffff);
413 srpt_get_svc_entries(srpt_service_guid,
414 slot, hi, lo, rsp_mad);
415 break;
416 default:
417 rsp_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530418 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000419 break;
420 }
421}
422
423/**
424 * srpt_mad_send_handler() - Post MAD-send callback function.
425 */
426static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
427 struct ib_mad_send_wc *mad_wc)
428{
429 ib_destroy_ah(mad_wc->send_buf->ah);
430 ib_free_send_mad(mad_wc->send_buf);
431}
432
433/**
434 * srpt_mad_recv_handler() - MAD reception callback function.
435 */
436static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
Christoph Hellwigca281262016-01-04 14:15:58 +0100437 struct ib_mad_send_buf *send_buf,
Bart Van Asschea42d9852011-10-14 01:30:46 +0000438 struct ib_mad_recv_wc *mad_wc)
439{
440 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
441 struct ib_ah *ah;
442 struct ib_mad_send_buf *rsp;
443 struct ib_dm_mad *dm_mad;
444
445 if (!mad_wc || !mad_wc->recv_buf.mad)
446 return;
447
448 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
449 mad_wc->recv_buf.grh, mad_agent->port_num);
450 if (IS_ERR(ah))
451 goto err;
452
453 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
454
455 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
456 mad_wc->wc->pkey_index, 0,
457 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400458 GFP_KERNEL,
459 IB_MGMT_BASE_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000460 if (IS_ERR(rsp))
461 goto err_rsp;
462
463 rsp->ah = ah;
464
465 dm_mad = rsp->mad;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800466 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000467 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
468 dm_mad->mad_hdr.status = 0;
469
470 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
471 case IB_MGMT_METHOD_GET:
472 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
473 break;
474 case IB_MGMT_METHOD_SET:
475 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530476 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000477 break;
478 default:
479 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530480 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000481 break;
482 }
483
484 if (!ib_post_send_mad(rsp, NULL)) {
485 ib_free_recv_mad(mad_wc);
486 /* will destroy_ah & free_send_mad in send completion */
487 return;
488 }
489
490 ib_free_send_mad(rsp);
491
492err_rsp:
493 ib_destroy_ah(ah);
494err:
495 ib_free_recv_mad(mad_wc);
496}
497
498/**
499 * srpt_refresh_port() - Configure a HCA port.
500 *
501 * Enable InfiniBand management datagram processing, update the cached sm_lid,
502 * lid and gid values, and register a callback function for processing MADs
503 * on the specified port.
504 *
505 * Note: It is safe to call this function more than once for the same port.
506 */
507static int srpt_refresh_port(struct srpt_port *sport)
508{
509 struct ib_mad_reg_req reg_req;
510 struct ib_port_modify port_modify;
511 struct ib_port_attr port_attr;
512 int ret;
513
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800514 memset(&port_modify, 0, sizeof(port_modify));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000515 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
516 port_modify.clr_port_cap_mask = 0;
517
518 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
519 if (ret)
520 goto err_mod_port;
521
522 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
523 if (ret)
524 goto err_query_port;
525
526 sport->sm_lid = port_attr.sm_lid;
527 sport->lid = port_attr.lid;
528
Matan Barak55ee3ab2015-10-15 18:38:45 +0300529 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
530 NULL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000531 if (ret)
532 goto err_query_port;
533
534 if (!sport->mad_agent) {
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800535 memset(&reg_req, 0, sizeof(reg_req));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000536 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
537 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
538 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
539 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
540
541 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
542 sport->port,
543 IB_QPT_GSI,
544 &reg_req, 0,
545 srpt_mad_send_handler,
546 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400547 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000548 if (IS_ERR(sport->mad_agent)) {
549 ret = PTR_ERR(sport->mad_agent);
550 sport->mad_agent = NULL;
551 goto err_query_port;
552 }
553 }
554
555 return 0;
556
557err_query_port:
558
559 port_modify.set_port_cap_mask = 0;
560 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
561 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
562
563err_mod_port:
564
565 return ret;
566}
567
568/**
569 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
570 *
571 * Note: It is safe to call this function more than once for the same device.
572 */
573static void srpt_unregister_mad_agent(struct srpt_device *sdev)
574{
575 struct ib_port_modify port_modify = {
576 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
577 };
578 struct srpt_port *sport;
579 int i;
580
581 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
582 sport = &sdev->port[i - 1];
583 WARN_ON(sport->port != i);
584 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400585 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000586 if (sport->mad_agent) {
587 ib_unregister_mad_agent(sport->mad_agent);
588 sport->mad_agent = NULL;
589 }
590 }
591}
592
593/**
594 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
595 */
596static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
597 int ioctx_size, int dma_size,
598 enum dma_data_direction dir)
599{
600 struct srpt_ioctx *ioctx;
601
602 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
603 if (!ioctx)
604 goto err;
605
606 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
607 if (!ioctx->buf)
608 goto err_free_ioctx;
609
610 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
611 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
612 goto err_free_buf;
613
614 return ioctx;
615
616err_free_buf:
617 kfree(ioctx->buf);
618err_free_ioctx:
619 kfree(ioctx);
620err:
621 return NULL;
622}
623
624/**
625 * srpt_free_ioctx() - Free an SRPT I/O context structure.
626 */
627static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
628 int dma_size, enum dma_data_direction dir)
629{
630 if (!ioctx)
631 return;
632
633 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
634 kfree(ioctx->buf);
635 kfree(ioctx);
636}
637
638/**
639 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
640 * @sdev: Device to allocate the I/O context ring for.
641 * @ring_size: Number of elements in the I/O context ring.
642 * @ioctx_size: I/O context size.
643 * @dma_size: DMA buffer size.
644 * @dir: DMA data direction.
645 */
646static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
647 int ring_size, int ioctx_size,
648 int dma_size, enum dma_data_direction dir)
649{
650 struct srpt_ioctx **ring;
651 int i;
652
653 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
654 && ioctx_size != sizeof(struct srpt_send_ioctx));
655
656 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
657 if (!ring)
658 goto out;
659 for (i = 0; i < ring_size; ++i) {
660 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
661 if (!ring[i])
662 goto err;
663 ring[i]->index = i;
664 }
665 goto out;
666
667err:
668 while (--i >= 0)
669 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
670 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100671 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000672out:
673 return ring;
674}
675
676/**
677 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
678 */
679static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
680 struct srpt_device *sdev, int ring_size,
681 int dma_size, enum dma_data_direction dir)
682{
683 int i;
684
685 for (i = 0; i < ring_size; ++i)
686 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
687 kfree(ioctx_ring);
688}
689
690/**
691 * srpt_get_cmd_state() - Get the state of a SCSI command.
692 */
693static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
694{
695 enum srpt_command_state state;
696 unsigned long flags;
697
698 BUG_ON(!ioctx);
699
700 spin_lock_irqsave(&ioctx->spinlock, flags);
701 state = ioctx->state;
702 spin_unlock_irqrestore(&ioctx->spinlock, flags);
703 return state;
704}
705
706/**
707 * srpt_set_cmd_state() - Set the state of a SCSI command.
708 *
709 * Does not modify the state of aborted commands. Returns the previous command
710 * state.
711 */
712static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
713 enum srpt_command_state new)
714{
715 enum srpt_command_state previous;
716 unsigned long flags;
717
718 BUG_ON(!ioctx);
719
720 spin_lock_irqsave(&ioctx->spinlock, flags);
721 previous = ioctx->state;
722 if (previous != SRPT_STATE_DONE)
723 ioctx->state = new;
724 spin_unlock_irqrestore(&ioctx->spinlock, flags);
725
726 return previous;
727}
728
729/**
730 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
731 *
732 * Returns true if and only if the previous command state was equal to 'old'.
733 */
734static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
735 enum srpt_command_state old,
736 enum srpt_command_state new)
737{
738 enum srpt_command_state previous;
739 unsigned long flags;
740
741 WARN_ON(!ioctx);
742 WARN_ON(old == SRPT_STATE_DONE);
743 WARN_ON(new == SRPT_STATE_NEW);
744
745 spin_lock_irqsave(&ioctx->spinlock, flags);
746 previous = ioctx->state;
747 if (previous == old)
748 ioctx->state = new;
749 spin_unlock_irqrestore(&ioctx->spinlock, flags);
750 return previous == old;
751}
752
753/**
754 * srpt_post_recv() - Post an IB receive request.
755 */
756static int srpt_post_recv(struct srpt_device *sdev,
757 struct srpt_recv_ioctx *ioctx)
758{
759 struct ib_sge list;
760 struct ib_recv_wr wr, *bad_wr;
761
762 BUG_ON(!sdev);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000763 list.addr = ioctx->ioctx.dma;
764 list.length = srp_max_req_size;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600765 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000766
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200767 ioctx->ioctx.cqe.done = srpt_recv_done;
768 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000769 wr.next = NULL;
770 wr.sg_list = &list;
771 wr.num_sge = 1;
772
773 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
774}
775
776/**
777 * srpt_post_send() - Post an IB send request.
778 *
779 * Returns zero upon success and a non-zero value upon failure.
780 */
781static int srpt_post_send(struct srpt_rdma_ch *ch,
782 struct srpt_send_ioctx *ioctx, int len)
783{
784 struct ib_sge list;
785 struct ib_send_wr wr, *bad_wr;
786 struct srpt_device *sdev = ch->sport->sdev;
787 int ret;
788
789 atomic_inc(&ch->req_lim);
790
791 ret = -ENOMEM;
792 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400793 pr_warn("IB send queue full (needed 1)\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000794 goto out;
795 }
796
797 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
798 DMA_TO_DEVICE);
799
800 list.addr = ioctx->ioctx.dma;
801 list.length = len;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600802 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000803
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200804 ioctx->ioctx.cqe.done = srpt_send_done;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000805 wr.next = NULL;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200806 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000807 wr.sg_list = &list;
808 wr.num_sge = 1;
809 wr.opcode = IB_WR_SEND;
810 wr.send_flags = IB_SEND_SIGNALED;
811
812 ret = ib_post_send(ch->qp, &wr, &bad_wr);
813
814out:
815 if (ret < 0) {
816 atomic_inc(&ch->sq_wr_avail);
817 atomic_dec(&ch->req_lim);
818 }
819 return ret;
820}
821
822/**
823 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
824 * @ioctx: Pointer to the I/O context associated with the request.
825 * @srp_cmd: Pointer to the SRP_CMD request data.
826 * @dir: Pointer to the variable to which the transfer direction will be
827 * written.
828 * @data_len: Pointer to the variable to which the total data length of all
829 * descriptors in the SRP_CMD request will be written.
830 *
831 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
832 *
833 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
834 * -ENOMEM when memory allocation fails and zero upon success.
835 */
836static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
837 struct srp_cmd *srp_cmd,
838 enum dma_data_direction *dir, u64 *data_len)
839{
840 struct srp_indirect_buf *idb;
841 struct srp_direct_buf *db;
842 unsigned add_cdb_offset;
843 int ret;
844
845 /*
846 * The pointer computations below will only be compiled correctly
847 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
848 * whether srp_cmd::add_data has been declared as a byte pointer.
849 */
850 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
851 && !__same_type(srp_cmd->add_data[0], (u8)0));
852
853 BUG_ON(!dir);
854 BUG_ON(!data_len);
855
856 ret = 0;
857 *data_len = 0;
858
859 /*
860 * The lower four bits of the buffer format field contain the DATA-IN
861 * buffer descriptor format, and the highest four bits contain the
862 * DATA-OUT buffer descriptor format.
863 */
864 *dir = DMA_NONE;
865 if (srp_cmd->buf_fmt & 0xf)
866 /* DATA-IN: transfer data from target to initiator (read). */
867 *dir = DMA_FROM_DEVICE;
868 else if (srp_cmd->buf_fmt >> 4)
869 /* DATA-OUT: transfer data from initiator to target (write). */
870 *dir = DMA_TO_DEVICE;
871
872 /*
873 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
874 * CDB LENGTH' field are reserved and the size in bytes of this field
875 * is four times the value specified in bits 3..7. Hence the "& ~3".
876 */
877 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
878 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
879 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
880 ioctx->n_rbuf = 1;
881 ioctx->rbufs = &ioctx->single_rbuf;
882
883 db = (struct srp_direct_buf *)(srp_cmd->add_data
884 + add_cdb_offset);
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800885 memcpy(ioctx->rbufs, db, sizeof(*db));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000886 *data_len = be32_to_cpu(db->len);
887 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
888 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
889 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
890 + add_cdb_offset);
891
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800892 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof(*db);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000893
894 if (ioctx->n_rbuf >
895 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400896 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000897 " type (%u out + %u in != %u / %zu)\n",
898 srp_cmd->data_out_desc_cnt,
899 srp_cmd->data_in_desc_cnt,
900 be32_to_cpu(idb->table_desc.len),
901 sizeof(*db));
902 ioctx->n_rbuf = 0;
903 ret = -EINVAL;
904 goto out;
905 }
906
907 if (ioctx->n_rbuf == 1)
908 ioctx->rbufs = &ioctx->single_rbuf;
909 else {
910 ioctx->rbufs =
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800911 kmalloc(ioctx->n_rbuf * sizeof(*db), GFP_ATOMIC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000912 if (!ioctx->rbufs) {
913 ioctx->n_rbuf = 0;
914 ret = -ENOMEM;
915 goto out;
916 }
917 }
918
919 db = idb->desc_list;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800920 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof(*db));
Bart Van Asschea42d9852011-10-14 01:30:46 +0000921 *data_len = be32_to_cpu(idb->len);
922 }
923out:
924 return ret;
925}
926
927/**
928 * srpt_init_ch_qp() - Initialize queue pair attributes.
929 *
930 * Initialized the attributes of queue pair 'qp' by allowing local write,
931 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
932 */
933static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
934{
935 struct ib_qp_attr *attr;
936 int ret;
937
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -0800938 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000939 if (!attr)
940 return -ENOMEM;
941
942 attr->qp_state = IB_QPS_INIT;
943 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
944 IB_ACCESS_REMOTE_WRITE;
945 attr->port_num = ch->sport->port;
946 attr->pkey_index = 0;
947
948 ret = ib_modify_qp(qp, attr,
949 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
950 IB_QP_PKEY_INDEX);
951
952 kfree(attr);
953 return ret;
954}
955
956/**
957 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
958 * @ch: channel of the queue pair.
959 * @qp: queue pair to change the state of.
960 *
961 * Returns zero upon success and a negative value upon failure.
962 *
963 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
964 * If this structure ever becomes larger, it might be necessary to allocate
965 * it dynamically instead of on the stack.
966 */
967static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
968{
969 struct ib_qp_attr qp_attr;
970 int attr_mask;
971 int ret;
972
973 qp_attr.qp_state = IB_QPS_RTR;
974 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
975 if (ret)
976 goto out;
977
978 qp_attr.max_dest_rd_atomic = 4;
979
980 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
981
982out:
983 return ret;
984}
985
986/**
987 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
988 * @ch: channel of the queue pair.
989 * @qp: queue pair to change the state of.
990 *
991 * Returns zero upon success and a negative value upon failure.
992 *
993 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
994 * If this structure ever becomes larger, it might be necessary to allocate
995 * it dynamically instead of on the stack.
996 */
997static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
998{
999 struct ib_qp_attr qp_attr;
1000 int attr_mask;
1001 int ret;
1002
1003 qp_attr.qp_state = IB_QPS_RTS;
1004 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1005 if (ret)
1006 goto out;
1007
1008 qp_attr.max_rd_atomic = 4;
1009
1010 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1011
1012out:
1013 return ret;
1014}
1015
1016/**
1017 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1018 */
1019static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1020{
1021 struct ib_qp_attr qp_attr;
1022
1023 qp_attr.qp_state = IB_QPS_ERR;
1024 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1025}
1026
1027/**
1028 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1029 */
1030static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1031 struct srpt_send_ioctx *ioctx)
1032{
1033 struct scatterlist *sg;
1034 enum dma_data_direction dir;
1035
1036 BUG_ON(!ch);
1037 BUG_ON(!ioctx);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001038 BUG_ON(ioctx->n_rdma && !ioctx->rdma_wrs);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001039
1040 while (ioctx->n_rdma)
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001041 kfree(ioctx->rdma_wrs[--ioctx->n_rdma].wr.sg_list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001042
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001043 kfree(ioctx->rdma_wrs);
1044 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001045
1046 if (ioctx->mapped_sg_count) {
1047 sg = ioctx->sg;
1048 WARN_ON(!sg);
1049 dir = ioctx->cmd.data_direction;
1050 BUG_ON(dir == DMA_NONE);
1051 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1052 opposite_dma_dir(dir));
1053 ioctx->mapped_sg_count = 0;
1054 }
1055}
1056
1057/**
1058 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1059 */
1060static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1061 struct srpt_send_ioctx *ioctx)
1062{
Mike Marciniszynb0768082014-04-07 13:58:35 -04001063 struct ib_device *dev = ch->sport->sdev->device;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001064 struct se_cmd *cmd;
1065 struct scatterlist *sg, *sg_orig;
1066 int sg_cnt;
1067 enum dma_data_direction dir;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001068 struct ib_rdma_wr *riu;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001069 struct srp_direct_buf *db;
1070 dma_addr_t dma_addr;
1071 struct ib_sge *sge;
1072 u64 raddr;
1073 u32 rsize;
1074 u32 tsize;
1075 u32 dma_len;
1076 int count, nrdma;
1077 int i, j, k;
1078
1079 BUG_ON(!ch);
1080 BUG_ON(!ioctx);
1081 cmd = &ioctx->cmd;
1082 dir = cmd->data_direction;
1083 BUG_ON(dir == DMA_NONE);
1084
Roland Dreier6f9e7f02012-03-30 11:29:12 -07001085 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1086 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001087
1088 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1089 opposite_dma_dir(dir));
1090 if (unlikely(!count))
1091 return -EAGAIN;
1092
1093 ioctx->mapped_sg_count = count;
1094
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001095 if (ioctx->rdma_wrs && ioctx->n_rdma_wrs)
1096 nrdma = ioctx->n_rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001097 else {
1098 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1099 + ioctx->n_rbuf;
1100
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001101 ioctx->rdma_wrs = kcalloc(nrdma, sizeof(*ioctx->rdma_wrs),
1102 GFP_KERNEL);
1103 if (!ioctx->rdma_wrs)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001104 goto free_mem;
1105
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001106 ioctx->n_rdma_wrs = nrdma;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001107 }
1108
1109 db = ioctx->rbufs;
1110 tsize = cmd->data_length;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001111 dma_len = ib_sg_dma_len(dev, &sg[0]);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001112 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001113
1114 /*
1115 * For each remote desc - calculate the #ib_sge.
1116 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1117 * each remote desc rdma_iu is required a rdma wr;
1118 * else
1119 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1120 * another rdma wr
1121 */
1122 for (i = 0, j = 0;
1123 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1124 rsize = be32_to_cpu(db->len);
1125 raddr = be64_to_cpu(db->va);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001126 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001127 riu->rkey = be32_to_cpu(db->key);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001128 riu->wr.num_sge = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001129
1130 /* calculate how many sge required for this remote_buf */
1131 while (rsize > 0 && tsize > 0) {
1132
1133 if (rsize >= dma_len) {
1134 tsize -= dma_len;
1135 rsize -= dma_len;
1136 raddr += dma_len;
1137
1138 if (tsize > 0) {
1139 ++j;
1140 if (j < count) {
1141 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001142 dma_len = ib_sg_dma_len(
1143 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001144 }
1145 }
1146 } else {
1147 tsize -= rsize;
1148 dma_len -= rsize;
1149 rsize = 0;
1150 }
1151
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001152 ++riu->wr.num_sge;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001153
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001154 if (rsize > 0 &&
1155 riu->wr.num_sge == SRPT_DEF_SG_PER_WQE) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001156 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001157 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1158 sizeof(*riu->wr.sg_list),
1159 GFP_KERNEL);
1160 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001161 goto free_mem;
1162
1163 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001164 riu->wr.num_sge = 0;
1165 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001166 riu->rkey = be32_to_cpu(db->key);
1167 }
1168 }
1169
1170 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001171 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1172 sizeof(*riu->wr.sg_list),
1173 GFP_KERNEL);
1174 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001175 goto free_mem;
1176 }
1177
1178 db = ioctx->rbufs;
1179 tsize = cmd->data_length;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001180 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001181 sg = sg_orig;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001182 dma_len = ib_sg_dma_len(dev, &sg[0]);
1183 dma_addr = ib_sg_dma_address(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001184
1185 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1186 for (i = 0, j = 0;
1187 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1188 rsize = be32_to_cpu(db->len);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001189 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001190 k = 0;
1191
1192 while (rsize > 0 && tsize > 0) {
1193 sge->addr = dma_addr;
Jason Gunthorpe5a783952015-07-30 17:22:24 -06001194 sge->lkey = ch->sport->sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001195
1196 if (rsize >= dma_len) {
1197 sge->length =
1198 (tsize < dma_len) ? tsize : dma_len;
1199 tsize -= dma_len;
1200 rsize -= dma_len;
1201
1202 if (tsize > 0) {
1203 ++j;
1204 if (j < count) {
1205 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001206 dma_len = ib_sg_dma_len(
1207 dev, sg);
1208 dma_addr = ib_sg_dma_address(
1209 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001210 }
1211 }
1212 } else {
1213 sge->length = (tsize < rsize) ? tsize : rsize;
1214 tsize -= rsize;
1215 dma_len -= rsize;
1216 dma_addr += rsize;
1217 rsize = 0;
1218 }
1219
1220 ++k;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001221 if (k == riu->wr.num_sge && rsize > 0 && tsize > 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001222 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001223 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001224 k = 0;
1225 } else if (rsize > 0 && tsize > 0)
1226 ++sge;
1227 }
1228 }
1229
1230 return 0;
1231
1232free_mem:
1233 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1234
1235 return -ENOMEM;
1236}
1237
1238/**
1239 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1240 */
1241static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1242{
1243 struct srpt_send_ioctx *ioctx;
1244 unsigned long flags;
1245
1246 BUG_ON(!ch);
1247
1248 ioctx = NULL;
1249 spin_lock_irqsave(&ch->spinlock, flags);
1250 if (!list_empty(&ch->free_list)) {
1251 ioctx = list_first_entry(&ch->free_list,
1252 struct srpt_send_ioctx, free_list);
1253 list_del(&ioctx->free_list);
1254 }
1255 spin_unlock_irqrestore(&ch->spinlock, flags);
1256
1257 if (!ioctx)
1258 return ioctx;
1259
1260 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001261 spin_lock_init(&ioctx->spinlock);
1262 ioctx->state = SRPT_STATE_NEW;
1263 ioctx->n_rbuf = 0;
1264 ioctx->rbufs = NULL;
1265 ioctx->n_rdma = 0;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001266 ioctx->n_rdma_wrs = 0;
1267 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001268 ioctx->mapped_sg_count = 0;
1269 init_completion(&ioctx->tx_done);
1270 ioctx->queue_status_only = false;
1271 /*
1272 * transport_init_se_cmd() does not initialize all fields, so do it
1273 * here.
1274 */
1275 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1276 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1277
1278 return ioctx;
1279}
1280
1281/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001282 * srpt_abort_cmd() - Abort a SCSI command.
1283 * @ioctx: I/O context associated with the SCSI command.
1284 * @context: Preferred execution context.
1285 */
1286static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1287{
1288 enum srpt_command_state state;
1289 unsigned long flags;
1290
1291 BUG_ON(!ioctx);
1292
1293 /*
1294 * If the command is in a state where the target core is waiting for
1295 * the ib_srpt driver, change the state to the next state. Changing
1296 * the state of the command from SRPT_STATE_NEED_DATA to
1297 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1298 * function a second time.
1299 */
1300
1301 spin_lock_irqsave(&ioctx->spinlock, flags);
1302 state = ioctx->state;
1303 switch (state) {
1304 case SRPT_STATE_NEED_DATA:
1305 ioctx->state = SRPT_STATE_DATA_IN;
1306 break;
1307 case SRPT_STATE_DATA_IN:
1308 case SRPT_STATE_CMD_RSP_SENT:
1309 case SRPT_STATE_MGMT_RSP_SENT:
1310 ioctx->state = SRPT_STATE_DONE;
1311 break;
1312 default:
1313 break;
1314 }
1315 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1316
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001317 if (state == SRPT_STATE_DONE) {
1318 struct srpt_rdma_ch *ch = ioctx->ch;
1319
1320 BUG_ON(ch->sess == NULL);
1321
Bart Van Asscheafc16602015-04-27 13:52:36 +02001322 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001323 goto out;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001324 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001325
1326 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
Bart Van Assche649ee052015-04-14 13:26:44 +02001327 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001328
1329 switch (state) {
1330 case SRPT_STATE_NEW:
1331 case SRPT_STATE_DATA_IN:
1332 case SRPT_STATE_MGMT:
1333 /*
1334 * Do nothing - defer abort processing until
1335 * srpt_queue_response() is invoked.
1336 */
1337 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1338 break;
1339 case SRPT_STATE_NEED_DATA:
1340 /* DMA_TO_DEVICE (write) - RDMA read error. */
Christoph Hellwige672a472012-07-08 15:58:43 -04001341
1342 /* XXX(hch): this is a horrible layering violation.. */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001343 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
Christoph Hellwige672a472012-07-08 15:58:43 -04001344 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001345 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001346 break;
1347 case SRPT_STATE_CMD_RSP_SENT:
1348 /*
1349 * SRP_RSP sending failed or the SRP_RSP send completion has
1350 * not been received in time.
1351 */
1352 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001353 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001354 break;
1355 case SRPT_STATE_MGMT_RSP_SENT:
1356 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001357 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001358 break;
1359 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001360 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001361 break;
1362 }
1363
1364out:
1365 return state;
1366}
1367
1368/**
Christoph Hellwige672a472012-07-08 15:58:43 -04001369 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1370 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001371 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001372 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001373 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001374static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001375{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001376 struct srpt_rdma_ch *ch = cq->cq_context;
1377 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001378 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001379
Bart Van Asschea42d9852011-10-14 01:30:46 +00001380 WARN_ON(ioctx->n_rdma <= 0);
1381 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1382
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001383 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1384 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1385 ioctx, wc->status);
1386 srpt_abort_cmd(ioctx);
1387 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001388 }
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001389
1390 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1391 SRPT_STATE_DATA_IN))
1392 target_execute_cmd(&ioctx->cmd);
1393 else
1394 pr_err("%s[%d]: wrong state = %d\n", __func__,
1395 __LINE__, srpt_get_cmd_state(ioctx));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001396}
1397
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001398static void srpt_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001399{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001400 struct srpt_send_ioctx *ioctx =
Bart Van Assche19f57292015-12-31 09:56:43 +01001401 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001402
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001403 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1404 pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n",
1405 ioctx, wc->status);
1406 srpt_abort_cmd(ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001407 }
1408}
1409
1410/**
1411 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1412 * @ch: RDMA channel through which the request has been received.
1413 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1414 * be built in the buffer ioctx->buf points at and hence this function will
1415 * overwrite the request data.
1416 * @tag: tag of the request for which this response is being generated.
1417 * @status: value for the STATUS field of the SRP_RSP information unit.
1418 *
1419 * Returns the size in bytes of the SRP_RSP response.
1420 *
1421 * An SRP_RSP response contains a SCSI status or service response. See also
1422 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1423 * response. See also SPC-2 for more information about sense data.
1424 */
1425static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1426 struct srpt_send_ioctx *ioctx, u64 tag,
1427 int status)
1428{
1429 struct srp_rsp *srp_rsp;
1430 const u8 *sense_data;
1431 int sense_data_len, max_sense_len;
1432
1433 /*
1434 * The lowest bit of all SAM-3 status codes is zero (see also
1435 * paragraph 5.3 in SAM-3).
1436 */
1437 WARN_ON(status & 1);
1438
1439 srp_rsp = ioctx->ioctx.buf;
1440 BUG_ON(!srp_rsp);
1441
1442 sense_data = ioctx->sense_data;
1443 sense_data_len = ioctx->cmd.scsi_sense_length;
1444 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1445
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001446 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001447 srp_rsp->opcode = SRP_RSP;
1448 srp_rsp->req_lim_delta =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301449 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001450 srp_rsp->tag = tag;
1451 srp_rsp->status = status;
1452
1453 if (sense_data_len) {
1454 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1455 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1456 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001457 pr_warn("truncated sense data from %d to %d"
1458 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001459 sense_data_len = max_sense_len;
1460 }
1461
1462 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1463 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1464 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1465 }
1466
1467 return sizeof(*srp_rsp) + sense_data_len;
1468}
1469
1470/**
1471 * srpt_build_tskmgmt_rsp() - Build a task management response.
1472 * @ch: RDMA channel through which the request has been received.
1473 * @ioctx: I/O context in which the SRP_RSP response will be built.
1474 * @rsp_code: RSP_CODE that will be stored in the response.
1475 * @tag: Tag of the request for which this response is being generated.
1476 *
1477 * Returns the size in bytes of the SRP_RSP response.
1478 *
1479 * An SRP_RSP response contains a SCSI status or service response. See also
1480 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1481 * response.
1482 */
1483static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1484 struct srpt_send_ioctx *ioctx,
1485 u8 rsp_code, u64 tag)
1486{
1487 struct srp_rsp *srp_rsp;
1488 int resp_data_len;
1489 int resp_len;
1490
Jack Wangc807f642013-09-30 10:09:05 +02001491 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001492 resp_len = sizeof(*srp_rsp) + resp_data_len;
1493
1494 srp_rsp = ioctx->ioctx.buf;
1495 BUG_ON(!srp_rsp);
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001496 memset(srp_rsp, 0, sizeof(*srp_rsp));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001497
1498 srp_rsp->opcode = SRP_RSP;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301499 srp_rsp->req_lim_delta =
1500 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001501 srp_rsp->tag = tag;
1502
Jack Wangc807f642013-09-30 10:09:05 +02001503 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1504 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1505 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001506
1507 return resp_len;
1508}
1509
1510#define NO_SUCH_LUN ((uint64_t)-1LL)
1511
1512/*
1513 * SCSI LUN addressing method. See also SAM-2 and the section about
1514 * eight byte LUNs.
1515 */
1516enum scsi_lun_addr_method {
1517 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1518 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1519 SCSI_LUN_ADDR_METHOD_LUN = 2,
1520 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1521};
1522
1523/*
1524 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1525 *
1526 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1527 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1528 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1529 */
1530static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1531{
1532 uint64_t res = NO_SUCH_LUN;
1533 int addressing_method;
1534
1535 if (unlikely(len < 2)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001536 pr_err("Illegal LUN length %d, expected 2 bytes or more\n",
1537 len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001538 goto out;
1539 }
1540
1541 switch (len) {
1542 case 8:
1543 if ((*((__be64 *)lun) &
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301544 cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001545 goto out_err;
1546 break;
1547 case 4:
1548 if (*((__be16 *)&lun[2]) != 0)
1549 goto out_err;
1550 break;
1551 case 6:
1552 if (*((__be32 *)&lun[2]) != 0)
1553 goto out_err;
1554 break;
1555 case 2:
1556 break;
1557 default:
1558 goto out_err;
1559 }
1560
1561 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1562 switch (addressing_method) {
1563 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1564 case SCSI_LUN_ADDR_METHOD_FLAT:
1565 case SCSI_LUN_ADDR_METHOD_LUN:
1566 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1567 break;
1568
1569 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1570 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001571 pr_err("Unimplemented LUN addressing method %u\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001572 addressing_method);
1573 break;
1574 }
1575
1576out:
1577 return res;
1578
1579out_err:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001580 pr_err("Support for multi-level LUNs has not yet been implemented\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001581 goto out;
1582}
1583
1584static int srpt_check_stop_free(struct se_cmd *cmd)
1585{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001586 struct srpt_send_ioctx *ioctx = container_of(cmd,
1587 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001588
Bart Van Asscheafc16602015-04-27 13:52:36 +02001589 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001590}
1591
1592/**
1593 * srpt_handle_cmd() - Process SRP_CMD.
1594 */
1595static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1596 struct srpt_recv_ioctx *recv_ioctx,
1597 struct srpt_send_ioctx *send_ioctx)
1598{
1599 struct se_cmd *cmd;
1600 struct srp_cmd *srp_cmd;
1601 uint64_t unpacked_lun;
1602 u64 data_len;
1603 enum dma_data_direction dir;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001604 sense_reason_t ret;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001605 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001606
1607 BUG_ON(!send_ioctx);
1608
1609 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001610 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001611 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001612
1613 switch (srp_cmd->task_attr) {
1614 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001615 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001616 break;
1617 case SRP_CMD_ORDERED_Q:
1618 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001619 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001620 break;
1621 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001622 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001623 break;
1624 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001625 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001626 break;
1627 }
1628
Christoph Hellwigde103c92012-11-06 12:24:09 -08001629 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001630 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001631 srp_cmd->tag);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001632 ret = TCM_INVALID_CDB_FIELD;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001633 goto send_sense;
1634 }
1635
Bart Van Asschea42d9852011-10-14 01:30:46 +00001636 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1637 sizeof(srp_cmd->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001638 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1639 &send_ioctx->sense_data[0], unpacked_lun, data_len,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001640 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001641 if (rc != 0) {
1642 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001643 goto send_sense;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001644 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001645 return 0;
1646
1647send_sense:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001648 transport_send_check_condition_and_sense(cmd, ret, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001649 return -1;
1650}
1651
Bart Van Asschea42d9852011-10-14 01:30:46 +00001652static int srp_tmr_to_tcm(int fn)
1653{
1654 switch (fn) {
1655 case SRP_TSK_ABORT_TASK:
1656 return TMR_ABORT_TASK;
1657 case SRP_TSK_ABORT_TASK_SET:
1658 return TMR_ABORT_TASK_SET;
1659 case SRP_TSK_CLEAR_TASK_SET:
1660 return TMR_CLEAR_TASK_SET;
1661 case SRP_TSK_LUN_RESET:
1662 return TMR_LUN_RESET;
1663 case SRP_TSK_CLEAR_ACA:
1664 return TMR_CLEAR_ACA;
1665 default:
1666 return -1;
1667 }
1668}
1669
1670/**
1671 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1672 *
1673 * Returns 0 if and only if the request will be processed by the target core.
1674 *
1675 * For more information about SRP_TSK_MGMT information units, see also section
1676 * 6.7 in the SRP r16a document.
1677 */
1678static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1679 struct srpt_recv_ioctx *recv_ioctx,
1680 struct srpt_send_ioctx *send_ioctx)
1681{
1682 struct srp_tsk_mgmt *srp_tsk;
1683 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001684 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001685 uint64_t unpacked_lun;
1686 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001687 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001688
1689 BUG_ON(!send_ioctx);
1690
1691 srp_tsk = recv_ioctx->ioctx.buf;
1692 cmd = &send_ioctx->cmd;
1693
1694 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1695 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1696 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1697
1698 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001699 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001700 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001701 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1702 sizeof(srp_tsk->lun));
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001703 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
Bart Van Assche51093252016-02-11 11:03:09 -08001704 srp_tsk, tcm_tmr, GFP_KERNEL, srp_tsk->task_tag,
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001705 TARGET_SCF_ACK_KREF);
1706 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001707 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001708 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001709 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001710 return;
1711fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001712 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001713}
1714
1715/**
1716 * srpt_handle_new_iu() - Process a newly received information unit.
1717 * @ch: RDMA channel through which the information unit has been received.
1718 * @ioctx: SRPT I/O context associated with the information unit.
1719 */
1720static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1721 struct srpt_recv_ioctx *recv_ioctx,
1722 struct srpt_send_ioctx *send_ioctx)
1723{
1724 struct srp_cmd *srp_cmd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001725
1726 BUG_ON(!ch);
1727 BUG_ON(!recv_ioctx);
1728
1729 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1730 recv_ioctx->ioctx.dma, srp_max_req_size,
1731 DMA_FROM_DEVICE);
1732
Bart Van Assche33912d72016-02-11 11:04:43 -08001733 if (unlikely(ch->state == CH_CONNECTING)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001734 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1735 goto out;
1736 }
1737
Bart Van Assche33912d72016-02-11 11:04:43 -08001738 if (unlikely(ch->state != CH_LIVE))
Bart Van Asschea42d9852011-10-14 01:30:46 +00001739 goto out;
1740
1741 srp_cmd = recv_ioctx->ioctx.buf;
1742 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1743 if (!send_ioctx)
1744 send_ioctx = srpt_get_send_ioctx(ch);
1745 if (unlikely(!send_ioctx)) {
1746 list_add_tail(&recv_ioctx->wait_list,
1747 &ch->cmd_wait_list);
1748 goto out;
1749 }
1750 }
1751
Bart Van Asschea42d9852011-10-14 01:30:46 +00001752 switch (srp_cmd->opcode) {
1753 case SRP_CMD:
1754 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1755 break;
1756 case SRP_TSK_MGMT:
1757 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1758 break;
1759 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001760 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001761 break;
1762 case SRP_CRED_RSP:
1763 pr_debug("received SRP_CRED_RSP\n");
1764 break;
1765 case SRP_AER_RSP:
1766 pr_debug("received SRP_AER_RSP\n");
1767 break;
1768 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001769 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001770 break;
1771 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001772 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001773 srp_cmd->opcode);
1774 break;
1775 }
1776
1777 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1778out:
1779 return;
1780}
1781
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001782static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001783{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001784 struct srpt_rdma_ch *ch = cq->cq_context;
1785 struct srpt_recv_ioctx *ioctx =
1786 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001787
Bart Van Asschea42d9852011-10-14 01:30:46 +00001788 if (wc->status == IB_WC_SUCCESS) {
1789 int req_lim;
1790
1791 req_lim = atomic_dec_return(&ch->req_lim);
1792 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001793 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001794 srpt_handle_new_iu(ch, ioctx, NULL);
1795 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001796 pr_info("receiving failed for ioctx %p with status %d\n",
1797 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001798 }
1799}
1800
1801/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001802 * Note: Although this has not yet been observed during tests, at least in
1803 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1804 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1805 * value in each response is set to one, and it is possible that this response
1806 * makes the initiator send a new request before the send completion for that
1807 * response has been processed. This could e.g. happen if the call to
1808 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1809 * if IB retransmission causes generation of the send completion to be
1810 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1811 * are queued on cmd_wait_list. The code below processes these delayed
1812 * requests one at a time.
1813 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001814static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001815{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001816 struct srpt_rdma_ch *ch = cq->cq_context;
1817 struct srpt_send_ioctx *ioctx =
1818 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1819 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001820
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001821 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1822
1823 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1824 state != SRPT_STATE_MGMT_RSP_SENT);
1825
1826 atomic_inc(&ch->sq_wr_avail);
1827
1828 if (wc->status != IB_WC_SUCCESS) {
1829 pr_info("sending response for ioctx 0x%p failed"
1830 " with status %d\n", ioctx, wc->status);
1831
1832 atomic_dec(&ch->req_lim);
1833 srpt_abort_cmd(ioctx);
1834 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001835 }
1836
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001837 if (state != SRPT_STATE_DONE) {
1838 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1839 transport_generic_free_cmd(&ioctx->cmd, 0);
1840 } else {
1841 pr_err("IB completion has been received too late for"
1842 " wr_id = %u.\n", ioctx->ioctx.index);
1843 }
1844
1845out:
1846 while (!list_empty(&ch->cmd_wait_list) &&
Bart Van Assche33912d72016-02-11 11:04:43 -08001847 ch->state == CH_LIVE &&
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001848 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001849 struct srpt_recv_ioctx *recv_ioctx;
1850
1851 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1852 struct srpt_recv_ioctx,
1853 wait_list);
1854 list_del(&recv_ioctx->wait_list);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001855 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001856 }
1857}
1858
Bart Van Asschea42d9852011-10-14 01:30:46 +00001859/**
1860 * srpt_create_ch_ib() - Create receive and send completion queues.
1861 */
1862static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1863{
1864 struct ib_qp_init_attr *qp_init;
1865 struct srpt_port *sport = ch->sport;
1866 struct srpt_device *sdev = sport->sdev;
1867 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1868 int ret;
1869
1870 WARN_ON(ch->rq_size < 1);
1871
1872 ret = -ENOMEM;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08001873 qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001874 if (!qp_init)
1875 goto out;
1876
Bart Van Asscheab477c12014-10-19 18:05:33 +03001877retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001878 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1879 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001880 if (IS_ERR(ch->cq)) {
1881 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001882 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001883 ch->rq_size + srp_sq_size, ret);
1884 goto out;
1885 }
1886
1887 qp_init->qp_context = (void *)ch;
1888 qp_init->event_handler
1889 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1890 qp_init->send_cq = ch->cq;
1891 qp_init->recv_cq = ch->cq;
1892 qp_init->srq = sdev->srq;
1893 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1894 qp_init->qp_type = IB_QPT_RC;
1895 qp_init->cap.max_send_wr = srp_sq_size;
1896 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1897
1898 ch->qp = ib_create_qp(sdev->pd, qp_init);
1899 if (IS_ERR(ch->qp)) {
1900 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001901 if (ret == -ENOMEM) {
1902 srp_sq_size /= 2;
1903 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1904 ib_destroy_cq(ch->cq);
1905 goto retry;
1906 }
1907 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001908 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001909 goto err_destroy_cq;
1910 }
1911
1912 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1913
1914 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1915 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1916 qp_init->cap.max_send_wr, ch->cm_id);
1917
1918 ret = srpt_init_ch_qp(ch, ch->qp);
1919 if (ret)
1920 goto err_destroy_qp;
1921
Bart Van Asschea42d9852011-10-14 01:30:46 +00001922out:
1923 kfree(qp_init);
1924 return ret;
1925
1926err_destroy_qp:
1927 ib_destroy_qp(ch->qp);
1928err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001929 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001930 goto out;
1931}
1932
1933static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1934{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001935 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001936 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001937}
1938
1939/**
1940 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
1941 *
1942 * Reset the QP and make sure all resources associated with the channel will
1943 * be deallocated at an appropriate time.
1944 *
1945 * Note: The caller must hold ch->sport->sdev->spinlock.
1946 */
1947static void __srpt_close_ch(struct srpt_rdma_ch *ch)
1948{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001949 enum rdma_ch_state prev_state;
1950 unsigned long flags;
1951
Bart Van Asschea42d9852011-10-14 01:30:46 +00001952 spin_lock_irqsave(&ch->spinlock, flags);
1953 prev_state = ch->state;
1954 switch (prev_state) {
1955 case CH_CONNECTING:
1956 case CH_LIVE:
1957 ch->state = CH_DISCONNECTING;
1958 break;
1959 default:
1960 break;
1961 }
1962 spin_unlock_irqrestore(&ch->spinlock, flags);
1963
1964 switch (prev_state) {
1965 case CH_CONNECTING:
1966 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
1967 NULL, 0);
1968 /* fall through */
1969 case CH_LIVE:
1970 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001971 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001972 break;
1973 case CH_DISCONNECTING:
1974 break;
1975 case CH_DRAINING:
1976 case CH_RELEASING:
1977 break;
1978 }
1979}
1980
1981/**
1982 * srpt_close_ch() - Close an RDMA channel.
1983 */
1984static void srpt_close_ch(struct srpt_rdma_ch *ch)
1985{
1986 struct srpt_device *sdev;
1987
1988 sdev = ch->sport->sdev;
1989 spin_lock_irq(&sdev->spinlock);
1990 __srpt_close_ch(ch);
1991 spin_unlock_irq(&sdev->spinlock);
1992}
1993
1994/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07001995 * srpt_shutdown_session() - Whether or not a session may be shut down.
1996 */
1997static int srpt_shutdown_session(struct se_session *se_sess)
1998{
1999 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2000 unsigned long flags;
2001
2002 spin_lock_irqsave(&ch->spinlock, flags);
2003 if (ch->in_shutdown) {
2004 spin_unlock_irqrestore(&ch->spinlock, flags);
2005 return true;
2006 }
2007
2008 ch->in_shutdown = true;
2009 target_sess_cmd_list_set_waiting(se_sess);
2010 spin_unlock_irqrestore(&ch->spinlock, flags);
2011
2012 return true;
2013}
2014
2015/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002016 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2017 * @cm_id: Pointer to the CM ID of the channel to be drained.
2018 *
2019 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2020 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2021 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2022 * waits until all target sessions for the associated IB device have been
2023 * unregistered and target session registration involves a call to
2024 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2025 * this function has finished).
2026 */
2027static void srpt_drain_channel(struct ib_cm_id *cm_id)
2028{
2029 struct srpt_device *sdev;
2030 struct srpt_rdma_ch *ch;
2031 int ret;
2032 bool do_reset = false;
2033
2034 WARN_ON_ONCE(irqs_disabled());
2035
2036 sdev = cm_id->context;
2037 BUG_ON(!sdev);
2038 spin_lock_irq(&sdev->spinlock);
2039 list_for_each_entry(ch, &sdev->rch_list, list) {
2040 if (ch->cm_id == cm_id) {
2041 do_reset = srpt_test_and_set_ch_state(ch,
2042 CH_CONNECTING, CH_DRAINING) ||
2043 srpt_test_and_set_ch_state(ch,
2044 CH_LIVE, CH_DRAINING) ||
2045 srpt_test_and_set_ch_state(ch,
2046 CH_DISCONNECTING, CH_DRAINING);
2047 break;
2048 }
2049 }
2050 spin_unlock_irq(&sdev->spinlock);
2051
2052 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002053 if (ch->sess)
2054 srpt_shutdown_session(ch->sess);
2055
Bart Van Asschea42d9852011-10-14 01:30:46 +00002056 ret = srpt_ch_qp_err(ch);
2057 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002058 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002059 " failed: %d\n", ret);
2060 }
2061}
2062
2063/**
2064 * srpt_find_channel() - Look up an RDMA channel.
2065 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2066 *
2067 * Return NULL if no matching RDMA channel has been found.
2068 */
2069static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2070 struct ib_cm_id *cm_id)
2071{
2072 struct srpt_rdma_ch *ch;
2073 bool found;
2074
2075 WARN_ON_ONCE(irqs_disabled());
2076 BUG_ON(!sdev);
2077
2078 found = false;
2079 spin_lock_irq(&sdev->spinlock);
2080 list_for_each_entry(ch, &sdev->rch_list, list) {
2081 if (ch->cm_id == cm_id) {
2082 found = true;
2083 break;
2084 }
2085 }
2086 spin_unlock_irq(&sdev->spinlock);
2087
2088 return found ? ch : NULL;
2089}
2090
2091/**
2092 * srpt_release_channel() - Release channel resources.
2093 *
2094 * Schedules the actual release because:
2095 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2096 * trigger a deadlock.
2097 * - It is not safe to call TCM transport_* functions from interrupt context.
2098 */
2099static void srpt_release_channel(struct srpt_rdma_ch *ch)
2100{
2101 schedule_work(&ch->release_work);
2102}
2103
2104static void srpt_release_channel_work(struct work_struct *w)
2105{
2106 struct srpt_rdma_ch *ch;
2107 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002108 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002109
2110 ch = container_of(w, struct srpt_rdma_ch, release_work);
2111 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2112 ch->release_done);
2113
2114 sdev = ch->sport->sdev;
2115 BUG_ON(!sdev);
2116
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002117 se_sess = ch->sess;
2118 BUG_ON(!se_sess);
2119
Joern Engelbe646c2d2013-05-15 00:44:07 -07002120 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002121
2122 transport_deregister_session_configfs(se_sess);
2123 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002124 ch->sess = NULL;
2125
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002126 ib_destroy_cm_id(ch->cm_id);
2127
Bart Van Asschea42d9852011-10-14 01:30:46 +00002128 srpt_destroy_ch_ib(ch);
2129
2130 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2131 ch->sport->sdev, ch->rq_size,
2132 ch->rsp_size, DMA_TO_DEVICE);
2133
2134 spin_lock_irq(&sdev->spinlock);
2135 list_del(&ch->list);
2136 spin_unlock_irq(&sdev->spinlock);
2137
Bart Van Asschea42d9852011-10-14 01:30:46 +00002138 if (ch->release_done)
2139 complete(ch->release_done);
2140
2141 wake_up(&sdev->ch_releaseQ);
2142
2143 kfree(ch);
2144}
2145
Bart Van Asschea42d9852011-10-14 01:30:46 +00002146/**
2147 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2148 *
2149 * Ownership of the cm_id is transferred to the target session if this
2150 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2151 */
2152static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2153 struct ib_cm_req_event_param *param,
2154 void *private_data)
2155{
2156 struct srpt_device *sdev = cm_id->context;
2157 struct srpt_port *sport = &sdev->port[param->port - 1];
2158 struct srp_login_req *req;
2159 struct srp_login_rsp *rsp;
2160 struct srp_login_rej *rej;
2161 struct ib_cm_rep_param *rep_param;
2162 struct srpt_rdma_ch *ch, *tmp_ch;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002163 struct se_node_acl *se_acl;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002164 u32 it_iu_len;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002165 int i, ret = 0;
2166 unsigned char *p;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002167
2168 WARN_ON_ONCE(irqs_disabled());
2169
2170 if (WARN_ON(!sdev || !private_data))
2171 return -EINVAL;
2172
2173 req = (struct srp_login_req *)private_data;
2174
2175 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2176
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002177 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2178 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2179 " (guid=0x%llx:0x%llx)\n",
2180 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2181 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2182 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2183 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2184 it_iu_len,
2185 param->port,
2186 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2187 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002188
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002189 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2190 rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2191 rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002192
2193 if (!rsp || !rej || !rep_param) {
2194 ret = -ENOMEM;
2195 goto out;
2196 }
2197
2198 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302199 rej->reason = cpu_to_be32(
2200 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002201 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002202 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002203 " length (%d bytes) is out of range (%d .. %d)\n",
2204 it_iu_len, 64, srp_max_req_size);
2205 goto reject;
2206 }
2207
2208 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302209 rej->reason = cpu_to_be32(
2210 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002211 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002212 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002213 " has not yet been enabled\n");
2214 goto reject;
2215 }
2216
2217 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2218 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2219
2220 spin_lock_irq(&sdev->spinlock);
2221
2222 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2223 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2224 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2225 && param->port == ch->sport->port
2226 && param->listen_id == ch->sport->sdev->cm_id
2227 && ch->cm_id) {
Bart Van Assche33912d72016-02-11 11:04:43 -08002228 if (ch->state != CH_CONNECTING
2229 && ch->state != CH_LIVE)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002230 continue;
2231
2232 /* found an existing channel */
2233 pr_debug("Found existing channel %s"
2234 " cm_id= %p state= %d\n",
Bart Van Assche33912d72016-02-11 11:04:43 -08002235 ch->sess_name, ch->cm_id, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002236
2237 __srpt_close_ch(ch);
2238
2239 rsp->rsp_flags =
2240 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2241 }
2242 }
2243
2244 spin_unlock_irq(&sdev->spinlock);
2245
2246 } else
2247 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2248
2249 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2250 || *(__be64 *)(req->target_port_id + 8) !=
2251 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302252 rej->reason = cpu_to_be32(
2253 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002254 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002255 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002256 " has an invalid target port identifier.\n");
2257 goto reject;
2258 }
2259
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002260 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002261 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302262 rej->reason = cpu_to_be32(
2263 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002264 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002265 ret = -ENOMEM;
2266 goto reject;
2267 }
2268
2269 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2270 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2271 memcpy(ch->t_port_id, req->target_port_id, 16);
2272 ch->sport = &sdev->port[param->port - 1];
2273 ch->cm_id = cm_id;
2274 /*
2275 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2276 * for the SRP protocol to the command queue size.
2277 */
2278 ch->rq_size = SRPT_RQ_SIZE;
2279 spin_lock_init(&ch->spinlock);
2280 ch->state = CH_CONNECTING;
2281 INIT_LIST_HEAD(&ch->cmd_wait_list);
2282 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2283
2284 ch->ioctx_ring = (struct srpt_send_ioctx **)
2285 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2286 sizeof(*ch->ioctx_ring[0]),
2287 ch->rsp_size, DMA_TO_DEVICE);
2288 if (!ch->ioctx_ring)
2289 goto free_ch;
2290
2291 INIT_LIST_HEAD(&ch->free_list);
2292 for (i = 0; i < ch->rq_size; i++) {
2293 ch->ioctx_ring[i]->ch = ch;
2294 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2295 }
2296
2297 ret = srpt_create_ch_ib(ch);
2298 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302299 rej->reason = cpu_to_be32(
2300 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002301 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002302 " a new RDMA channel failed.\n");
2303 goto free_ring;
2304 }
2305
2306 ret = srpt_ch_qp_rtr(ch, ch->qp);
2307 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302308 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002309 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002310 " RTR failed (error code = %d)\n", ret);
2311 goto destroy_ib;
2312 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002313
Bart Van Asschea42d9852011-10-14 01:30:46 +00002314 /*
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002315 * Use the initator port identifier as the session name, when
2316 * checking against se_node_acl->initiatorname[] this can be
2317 * with or without preceeding '0x'.
Bart Van Asschea42d9852011-10-14 01:30:46 +00002318 */
2319 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2320 be64_to_cpu(*(__be64 *)ch->i_port_id),
2321 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2322
2323 pr_debug("registering session %s\n", ch->sess_name);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002324 p = &ch->sess_name[0];
Bart Van Asschea42d9852011-10-14 01:30:46 +00002325
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002326 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002327 if (IS_ERR(ch->sess)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302328 rej->reason = cpu_to_be32(
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002329 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002330 pr_debug("Failed to create session\n");
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002331 goto destroy_ib;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002332 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002333
2334try_again:
2335 se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p);
2336 if (!se_acl) {
2337 pr_info("Rejected login because no ACL has been"
2338 " configured yet for initiator %s.\n", ch->sess_name);
2339 /*
2340 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2341 */
2342 if (p == &ch->sess_name[0]) {
2343 p += 2;
2344 goto try_again;
2345 }
2346 rej->reason = cpu_to_be32(
2347 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2348 transport_free_session(ch->sess);
2349 goto destroy_ib;
2350 }
2351 ch->sess->se_node_acl = se_acl;
2352
2353 transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002354
2355 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2356 ch->sess_name, ch->cm_id);
2357
2358 /* create srp_login_response */
2359 rsp->opcode = SRP_LOGIN_RSP;
2360 rsp->tag = req->tag;
2361 rsp->max_it_iu_len = req->req_it_iu_len;
2362 rsp->max_ti_iu_len = req->req_it_iu_len;
2363 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302364 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2365 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002366 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2367 atomic_set(&ch->req_lim, ch->rq_size);
2368 atomic_set(&ch->req_lim_delta, 0);
2369
2370 /* create cm reply */
2371 rep_param->qp_num = ch->qp->qp_num;
2372 rep_param->private_data = (void *)rsp;
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002373 rep_param->private_data_len = sizeof(*rsp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002374 rep_param->rnr_retry_count = 7;
2375 rep_param->flow_control = 1;
2376 rep_param->failover_accepted = 0;
2377 rep_param->srq = 1;
2378 rep_param->responder_resources = 4;
2379 rep_param->initiator_depth = 4;
2380
2381 ret = ib_send_cm_rep(cm_id, rep_param);
2382 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002383 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002384 " (error code = %d)\n", ret);
2385 goto release_channel;
2386 }
2387
2388 spin_lock_irq(&sdev->spinlock);
2389 list_add_tail(&ch->list, &sdev->rch_list);
2390 spin_unlock_irq(&sdev->spinlock);
2391
2392 goto out;
2393
2394release_channel:
2395 srpt_set_ch_state(ch, CH_RELEASING);
2396 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002397 transport_deregister_session(ch->sess);
2398 ch->sess = NULL;
2399
2400destroy_ib:
2401 srpt_destroy_ch_ib(ch);
2402
2403free_ring:
2404 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2405 ch->sport->sdev, ch->rq_size,
2406 ch->rsp_size, DMA_TO_DEVICE);
2407free_ch:
2408 kfree(ch);
2409
2410reject:
2411 rej->opcode = SRP_LOGIN_REJ;
2412 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302413 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2414 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002415
2416 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002417 (void *)rej, sizeof(*rej));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002418
2419out:
2420 kfree(rep_param);
2421 kfree(rsp);
2422 kfree(rej);
2423
2424 return ret;
2425}
2426
2427static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2428{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002429 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002430 srpt_drain_channel(cm_id);
2431}
2432
2433/**
2434 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2435 *
2436 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2437 * and that the recipient may begin transmitting (RTU = ready to use).
2438 */
2439static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2440{
2441 struct srpt_rdma_ch *ch;
2442 int ret;
2443
2444 ch = srpt_find_channel(cm_id->context, cm_id);
2445 BUG_ON(!ch);
2446
2447 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2448 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2449
2450 ret = srpt_ch_qp_rts(ch, ch->qp);
2451
2452 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2453 wait_list) {
2454 list_del(&ioctx->wait_list);
2455 srpt_handle_new_iu(ch, ioctx, NULL);
2456 }
2457 if (ret)
2458 srpt_close_ch(ch);
2459 }
2460}
2461
2462static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2463{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002464 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002465 srpt_drain_channel(cm_id);
2466}
2467
2468static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2469{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002470 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002471 srpt_drain_channel(cm_id);
2472}
2473
2474/**
2475 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2476 */
2477static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2478{
2479 struct srpt_rdma_ch *ch;
2480 unsigned long flags;
2481 bool send_drep = false;
2482
2483 ch = srpt_find_channel(cm_id->context, cm_id);
2484 BUG_ON(!ch);
2485
Bart Van Assche33912d72016-02-11 11:04:43 -08002486 pr_debug("cm_id= %p ch->state= %d\n", cm_id, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002487
2488 spin_lock_irqsave(&ch->spinlock, flags);
2489 switch (ch->state) {
2490 case CH_CONNECTING:
2491 case CH_LIVE:
2492 send_drep = true;
2493 ch->state = CH_DISCONNECTING;
2494 break;
2495 case CH_DISCONNECTING:
2496 case CH_DRAINING:
2497 case CH_RELEASING:
2498 WARN(true, "unexpected channel state %d\n", ch->state);
2499 break;
2500 }
2501 spin_unlock_irqrestore(&ch->spinlock, flags);
2502
2503 if (send_drep) {
2504 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002505 pr_err("Sending IB DREP failed.\n");
2506 pr_info("Received DREQ and sent DREP for session %s.\n",
2507 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002508 }
2509}
2510
2511/**
2512 * srpt_cm_drep_recv() - Process reception of a DREP message.
2513 */
2514static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2515{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002516 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002517 srpt_drain_channel(cm_id);
2518}
2519
2520/**
2521 * srpt_cm_handler() - IB connection manager callback function.
2522 *
2523 * A non-zero return value will cause the caller destroy the CM ID.
2524 *
2525 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2526 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2527 * a non-zero value in any other case will trigger a race with the
2528 * ib_destroy_cm_id() call in srpt_release_channel().
2529 */
2530static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2531{
2532 int ret;
2533
2534 ret = 0;
2535 switch (event->event) {
2536 case IB_CM_REQ_RECEIVED:
2537 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2538 event->private_data);
2539 break;
2540 case IB_CM_REJ_RECEIVED:
2541 srpt_cm_rej_recv(cm_id);
2542 break;
2543 case IB_CM_RTU_RECEIVED:
2544 case IB_CM_USER_ESTABLISHED:
2545 srpt_cm_rtu_recv(cm_id);
2546 break;
2547 case IB_CM_DREQ_RECEIVED:
2548 srpt_cm_dreq_recv(cm_id);
2549 break;
2550 case IB_CM_DREP_RECEIVED:
2551 srpt_cm_drep_recv(cm_id);
2552 break;
2553 case IB_CM_TIMEWAIT_EXIT:
2554 srpt_cm_timewait_exit(cm_id);
2555 break;
2556 case IB_CM_REP_ERROR:
2557 srpt_cm_rep_error(cm_id);
2558 break;
2559 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002560 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002561 break;
2562 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002563 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002564 break;
2565 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002566 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002567 break;
2568 }
2569
2570 return ret;
2571}
2572
2573/**
2574 * srpt_perform_rdmas() - Perform IB RDMA.
2575 *
2576 * Returns zero upon success or a negative number upon failure.
2577 */
2578static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2579 struct srpt_send_ioctx *ioctx)
2580{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002581 struct ib_send_wr *bad_wr;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002582 int sq_wr_avail, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002583 enum dma_data_direction dir;
2584 const int n_rdma = ioctx->n_rdma;
2585
2586 dir = ioctx->cmd.data_direction;
2587 if (dir == DMA_TO_DEVICE) {
2588 /* write */
2589 ret = -ENOMEM;
2590 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2591 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002592 pr_warn("IB send queue full (needed %d)\n",
2593 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002594 goto out;
2595 }
2596 }
2597
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002598 for (i = 0; i < n_rdma; i++) {
2599 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002600
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002601 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2602 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2603
2604 if (i == n_rdma - 1) {
2605 /* only get completion event for the last rdma read */
2606 if (dir == DMA_TO_DEVICE) {
2607 wr->send_flags = IB_SEND_SIGNALED;
2608 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2609 } else {
2610 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2611 }
2612 wr->wr_cqe = &ioctx->rdma_cqe;
2613 wr->next = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002614 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002615 wr->wr_cqe = NULL;
2616 wr->next = &ioctx->rdma_wrs[i + 1].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002617 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002618 }
2619
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002620 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002621 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002622 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002623 __func__, __LINE__, ret, i, n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002624out:
2625 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2626 atomic_add(n_rdma, &ch->sq_wr_avail);
2627 return ret;
2628}
2629
2630/**
2631 * srpt_xfer_data() - Start data transfer from initiator to target.
2632 */
2633static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2634 struct srpt_send_ioctx *ioctx)
2635{
2636 int ret;
2637
2638 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2639 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002640 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002641 goto out;
2642 }
2643
2644 ret = srpt_perform_rdmas(ch, ioctx);
2645 if (ret) {
2646 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002647 pr_info("%s[%d] queue full -- ret=%d\n",
2648 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002649 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002650 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002651 __func__, __LINE__, ret);
2652 goto out_unmap;
2653 }
2654
2655out:
2656 return ret;
2657out_unmap:
2658 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2659 goto out;
2660}
2661
2662static int srpt_write_pending_status(struct se_cmd *se_cmd)
2663{
2664 struct srpt_send_ioctx *ioctx;
2665
2666 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2667 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2668}
2669
2670/*
2671 * srpt_write_pending() - Start data transfer from initiator to target (write).
2672 */
2673static int srpt_write_pending(struct se_cmd *se_cmd)
2674{
2675 struct srpt_rdma_ch *ch;
2676 struct srpt_send_ioctx *ioctx;
2677 enum srpt_command_state new_state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002678 int ret;
2679
2680 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2681
2682 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2683 WARN_ON(new_state == SRPT_STATE_DONE);
2684
2685 ch = ioctx->ch;
2686 BUG_ON(!ch);
2687
Bart Van Assche33912d72016-02-11 11:04:43 -08002688 switch (ch->state) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002689 case CH_CONNECTING:
Bart Van Assche33912d72016-02-11 11:04:43 -08002690 WARN(true, "unexpected channel state %d\n", ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002691 ret = -EINVAL;
2692 goto out;
2693 case CH_LIVE:
2694 break;
2695 case CH_DISCONNECTING:
2696 case CH_DRAINING:
2697 case CH_RELEASING:
2698 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002699 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002700 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2701 ret = -EINVAL;
2702 goto out;
2703 }
2704 ret = srpt_xfer_data(ch, ioctx);
2705
2706out:
2707 return ret;
2708}
2709
2710static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2711{
2712 switch (tcm_mgmt_status) {
2713 case TMR_FUNCTION_COMPLETE:
2714 return SRP_TSK_MGMT_SUCCESS;
2715 case TMR_FUNCTION_REJECTED:
2716 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2717 }
2718 return SRP_TSK_MGMT_FAILED;
2719}
2720
2721/**
2722 * srpt_queue_response() - Transmits the response to a SCSI command.
2723 *
2724 * Callback function called by the TCM core. Must not block since it can be
2725 * invoked on the context of the IB completion handler.
2726 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002727static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002728{
2729 struct srpt_rdma_ch *ch;
2730 struct srpt_send_ioctx *ioctx;
2731 enum srpt_command_state state;
2732 unsigned long flags;
2733 int ret;
2734 enum dma_data_direction dir;
2735 int resp_len;
2736 u8 srp_tm_status;
2737
Bart Van Asschea42d9852011-10-14 01:30:46 +00002738 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2739 ch = ioctx->ch;
2740 BUG_ON(!ch);
2741
2742 spin_lock_irqsave(&ioctx->spinlock, flags);
2743 state = ioctx->state;
2744 switch (state) {
2745 case SRPT_STATE_NEW:
2746 case SRPT_STATE_DATA_IN:
2747 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2748 break;
2749 case SRPT_STATE_MGMT:
2750 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2751 break;
2752 default:
2753 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2754 ch, ioctx->ioctx.index, ioctx->state);
2755 break;
2756 }
2757 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2758
2759 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2760 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2761 atomic_inc(&ch->req_lim_delta);
2762 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04002763 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002764 }
2765
2766 dir = ioctx->cmd.data_direction;
2767
2768 /* For read commands, transfer the data to the initiator. */
2769 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2770 !ioctx->queue_status_only) {
2771 ret = srpt_xfer_data(ch, ioctx);
2772 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002773 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002774 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04002775 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002776 }
2777 }
2778
2779 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002780 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002781 cmd->scsi_status);
2782 else {
2783 srp_tm_status
2784 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2785 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002786 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002787 }
2788 ret = srpt_post_send(ch, ioctx, resp_len);
2789 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002790 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002791 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002792 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2793 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02002794 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002795 }
Joern Engelb79fafa2013-07-03 11:22:17 -04002796}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002797
Joern Engelb79fafa2013-07-03 11:22:17 -04002798static int srpt_queue_data_in(struct se_cmd *cmd)
2799{
2800 srpt_queue_response(cmd);
2801 return 0;
2802}
2803
2804static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2805{
2806 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002807}
2808
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002809static void srpt_aborted_task(struct se_cmd *cmd)
2810{
2811 struct srpt_send_ioctx *ioctx = container_of(cmd,
2812 struct srpt_send_ioctx, cmd);
2813
2814 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2815}
2816
Bart Van Asschea42d9852011-10-14 01:30:46 +00002817static int srpt_queue_status(struct se_cmd *cmd)
2818{
2819 struct srpt_send_ioctx *ioctx;
2820
2821 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2822 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2823 if (cmd->se_cmd_flags &
2824 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2825 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2826 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002827 srpt_queue_response(cmd);
2828 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002829}
2830
2831static void srpt_refresh_port_work(struct work_struct *work)
2832{
2833 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2834
2835 srpt_refresh_port(sport);
2836}
2837
2838static int srpt_ch_list_empty(struct srpt_device *sdev)
2839{
2840 int res;
2841
2842 spin_lock_irq(&sdev->spinlock);
2843 res = list_empty(&sdev->rch_list);
2844 spin_unlock_irq(&sdev->spinlock);
2845
2846 return res;
2847}
2848
2849/**
2850 * srpt_release_sdev() - Free the channel resources associated with a target.
2851 */
2852static int srpt_release_sdev(struct srpt_device *sdev)
2853{
2854 struct srpt_rdma_ch *ch, *tmp_ch;
2855 int res;
2856
2857 WARN_ON_ONCE(irqs_disabled());
2858
2859 BUG_ON(!sdev);
2860
2861 spin_lock_irq(&sdev->spinlock);
2862 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2863 __srpt_close_ch(ch);
2864 spin_unlock_irq(&sdev->spinlock);
2865
2866 res = wait_event_interruptible(sdev->ch_releaseQ,
2867 srpt_ch_list_empty(sdev));
2868 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002869 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002870
2871 return 0;
2872}
2873
2874static struct srpt_port *__srpt_lookup_port(const char *name)
2875{
2876 struct ib_device *dev;
2877 struct srpt_device *sdev;
2878 struct srpt_port *sport;
2879 int i;
2880
2881 list_for_each_entry(sdev, &srpt_dev_list, list) {
2882 dev = sdev->device;
2883 if (!dev)
2884 continue;
2885
2886 for (i = 0; i < dev->phys_port_cnt; i++) {
2887 sport = &sdev->port[i];
2888
2889 if (!strcmp(sport->port_guid, name))
2890 return sport;
2891 }
2892 }
2893
2894 return NULL;
2895}
2896
2897static struct srpt_port *srpt_lookup_port(const char *name)
2898{
2899 struct srpt_port *sport;
2900
2901 spin_lock(&srpt_dev_lock);
2902 sport = __srpt_lookup_port(name);
2903 spin_unlock(&srpt_dev_lock);
2904
2905 return sport;
2906}
2907
2908/**
2909 * srpt_add_one() - Infiniband device addition callback function.
2910 */
2911static void srpt_add_one(struct ib_device *device)
2912{
2913 struct srpt_device *sdev;
2914 struct srpt_port *sport;
2915 struct ib_srq_init_attr srq_attr;
2916 int i;
2917
2918 pr_debug("device = %p, device->dma_ops = %p\n", device,
2919 device->dma_ops);
2920
Bart Van Assche9d2aa2b42016-02-11 11:03:31 -08002921 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002922 if (!sdev)
2923 goto err;
2924
2925 sdev->device = device;
2926 INIT_LIST_HEAD(&sdev->rch_list);
2927 init_waitqueue_head(&sdev->ch_releaseQ);
2928 spin_lock_init(&sdev->spinlock);
2929
Bart Van Asschea42d9852011-10-14 01:30:46 +00002930 sdev->pd = ib_alloc_pd(device);
2931 if (IS_ERR(sdev->pd))
2932 goto free_dev;
2933
Or Gerlitz4a061b22015-12-18 10:59:46 +02002934 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002935
2936 srq_attr.event_handler = srpt_srq_event;
2937 srq_attr.srq_context = (void *)sdev;
2938 srq_attr.attr.max_wr = sdev->srq_size;
2939 srq_attr.attr.max_sge = 1;
2940 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07002941 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002942
2943 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
2944 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06002945 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002946
2947 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
Or Gerlitz4a061b22015-12-18 10:59:46 +02002948 __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002949 device->name);
2950
2951 if (!srpt_service_guid)
2952 srpt_service_guid = be64_to_cpu(device->node_guid);
2953
2954 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
2955 if (IS_ERR(sdev->cm_id))
2956 goto err_srq;
2957
2958 /* print out target login information */
2959 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
2960 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
2961 srpt_service_guid, srpt_service_guid);
2962
2963 /*
2964 * We do not have a consistent service_id (ie. also id_ext of target_id)
2965 * to identify this target. We currently use the guid of the first HCA
2966 * in the system as service_id; therefore, the target_id will change
2967 * if this HCA is gone bad and replaced by different HCA
2968 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03002969 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00002970 goto err_cm;
2971
2972 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
2973 srpt_event_handler);
2974 if (ib_register_event_handler(&sdev->event_handler))
2975 goto err_cm;
2976
2977 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
2978 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
2979 sizeof(*sdev->ioctx_ring[0]),
2980 srp_max_req_size, DMA_FROM_DEVICE);
2981 if (!sdev->ioctx_ring)
2982 goto err_event;
2983
2984 for (i = 0; i < sdev->srq_size; ++i)
2985 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
2986
Roland Dreierf2250662012-02-02 12:55:58 -08002987 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002988
2989 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
2990 sport = &sdev->port[i - 1];
2991 sport->sdev = sdev;
2992 sport->port = i;
2993 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
2994 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
2995 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
2996 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002997
2998 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002999 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschef68cba4e92016-02-11 11:04:20 -08003000 sdev->device->name, i);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003001 goto err_ring;
3002 }
3003 snprintf(sport->port_guid, sizeof(sport->port_guid),
3004 "0x%016llx%016llx",
3005 be64_to_cpu(sport->gid.global.subnet_prefix),
3006 be64_to_cpu(sport->gid.global.interface_id));
3007 }
3008
3009 spin_lock(&srpt_dev_lock);
3010 list_add_tail(&sdev->list, &srpt_dev_list);
3011 spin_unlock(&srpt_dev_lock);
3012
3013out:
3014 ib_set_client_data(device, &srpt_client, sdev);
3015 pr_debug("added %s.\n", device->name);
3016 return;
3017
3018err_ring:
3019 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3020 sdev->srq_size, srp_max_req_size,
3021 DMA_FROM_DEVICE);
3022err_event:
3023 ib_unregister_event_handler(&sdev->event_handler);
3024err_cm:
3025 ib_destroy_cm_id(sdev->cm_id);
3026err_srq:
3027 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003028err_pd:
3029 ib_dealloc_pd(sdev->pd);
3030free_dev:
3031 kfree(sdev);
3032err:
3033 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003034 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003035 goto out;
3036}
3037
3038/**
3039 * srpt_remove_one() - InfiniBand device removal callback function.
3040 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03003041static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003042{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003043 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003044 int i;
3045
Bart Van Asschea42d9852011-10-14 01:30:46 +00003046 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003047 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003048 return;
3049 }
3050
3051 srpt_unregister_mad_agent(sdev);
3052
3053 ib_unregister_event_handler(&sdev->event_handler);
3054
3055 /* Cancel any work queued by the just unregistered IB event handler. */
3056 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3057 cancel_work_sync(&sdev->port[i].work);
3058
3059 ib_destroy_cm_id(sdev->cm_id);
3060
3061 /*
3062 * Unregistering a target must happen after destroying sdev->cm_id
3063 * such that no new SRP_LOGIN_REQ information units can arrive while
3064 * destroying the target.
3065 */
3066 spin_lock(&srpt_dev_lock);
3067 list_del(&sdev->list);
3068 spin_unlock(&srpt_dev_lock);
3069 srpt_release_sdev(sdev);
3070
3071 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003072 ib_dealloc_pd(sdev->pd);
3073
3074 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3075 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3076 sdev->ioctx_ring = NULL;
3077 kfree(sdev);
3078}
3079
3080static struct ib_client srpt_client = {
3081 .name = DRV_NAME,
3082 .add = srpt_add_one,
3083 .remove = srpt_remove_one
3084};
3085
3086static int srpt_check_true(struct se_portal_group *se_tpg)
3087{
3088 return 1;
3089}
3090
3091static int srpt_check_false(struct se_portal_group *se_tpg)
3092{
3093 return 0;
3094}
3095
3096static char *srpt_get_fabric_name(void)
3097{
3098 return "srpt";
3099}
3100
Bart Van Asschea42d9852011-10-14 01:30:46 +00003101static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3102{
3103 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3104
3105 return sport->port_guid;
3106}
3107
3108static u16 srpt_get_tag(struct se_portal_group *tpg)
3109{
3110 return 1;
3111}
3112
Bart Van Asschea42d9852011-10-14 01:30:46 +00003113static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3114{
3115 return 1;
3116}
3117
3118static void srpt_release_cmd(struct se_cmd *se_cmd)
3119{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003120 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3121 struct srpt_send_ioctx, cmd);
3122 struct srpt_rdma_ch *ch = ioctx->ch;
3123 unsigned long flags;
3124
3125 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3126 WARN_ON(ioctx->mapped_sg_count != 0);
3127
3128 if (ioctx->n_rbuf > 1) {
3129 kfree(ioctx->rbufs);
3130 ioctx->rbufs = NULL;
3131 ioctx->n_rbuf = 0;
3132 }
3133
3134 spin_lock_irqsave(&ch->spinlock, flags);
3135 list_add(&ioctx->free_list, &ch->free_list);
3136 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003137}
3138
3139/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003140 * srpt_close_session() - Forcibly close a session.
3141 *
3142 * Callback function invoked by the TCM core to clean up sessions associated
3143 * with a node ACL when the user invokes
3144 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3145 */
3146static void srpt_close_session(struct se_session *se_sess)
3147{
3148 DECLARE_COMPLETION_ONSTACK(release_done);
3149 struct srpt_rdma_ch *ch;
3150 struct srpt_device *sdev;
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003151 unsigned long res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003152
3153 ch = se_sess->fabric_sess_ptr;
3154 WARN_ON(ch->sess != se_sess);
3155
Bart Van Assche33912d72016-02-11 11:04:43 -08003156 pr_debug("ch %p state %d\n", ch, ch->state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003157
3158 sdev = ch->sport->sdev;
3159 spin_lock_irq(&sdev->spinlock);
3160 BUG_ON(ch->release_done);
3161 ch->release_done = &release_done;
3162 __srpt_close_ch(ch);
3163 spin_unlock_irq(&sdev->spinlock);
3164
3165 res = wait_for_completion_timeout(&release_done, 60 * HZ);
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003166 WARN_ON(res == 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003167}
3168
3169/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003170 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3171 *
3172 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3173 * This object represents an arbitrary integer used to uniquely identify a
3174 * particular attached remote initiator port to a particular SCSI target port
3175 * within a particular SCSI target device within a particular SCSI instance.
3176 */
3177static u32 srpt_sess_get_index(struct se_session *se_sess)
3178{
3179 return 0;
3180}
3181
3182static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3183{
3184}
3185
Bart Van Asschea42d9852011-10-14 01:30:46 +00003186/* Note: only used from inside debug printk's by the TCM core. */
3187static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3188{
3189 struct srpt_send_ioctx *ioctx;
3190
3191 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3192 return srpt_get_cmd_state(ioctx);
3193}
3194
Bart Van Asschea42d9852011-10-14 01:30:46 +00003195/**
3196 * srpt_parse_i_port_id() - Parse an initiator port ID.
3197 * @name: ASCII representation of a 128-bit initiator port ID.
3198 * @i_port_id: Binary 128-bit port ID.
3199 */
3200static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3201{
3202 const char *p;
3203 unsigned len, count, leading_zero_bytes;
3204 int ret, rc;
3205
3206 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003207 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003208 p += 2;
3209 ret = -EINVAL;
3210 len = strlen(p);
3211 if (len % 2)
3212 goto out;
3213 count = min(len / 2, 16U);
3214 leading_zero_bytes = 16 - count;
3215 memset(i_port_id, 0, leading_zero_bytes);
3216 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3217 if (rc < 0)
3218 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3219 ret = 0;
3220out:
3221 return ret;
3222}
3223
3224/*
3225 * configfs callback function invoked for
3226 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3227 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003228static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003229{
Bart Van Asschea42d9852011-10-14 01:30:46 +00003230 u8 i_port_id[16];
3231
3232 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003233 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003234 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003235 }
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003236 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003237}
3238
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003239static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3240 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003241{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003242 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003243 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3244
3245 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3246}
3247
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003248static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3249 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003250{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003251 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003252 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3253 unsigned long val;
3254 int ret;
3255
Jingoo Han9d8abf42014-02-05 11:22:05 +09003256 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003257 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003258 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003259 return -EINVAL;
3260 }
3261 if (val > MAX_SRPT_RDMA_SIZE) {
3262 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3263 MAX_SRPT_RDMA_SIZE);
3264 return -EINVAL;
3265 }
3266 if (val < DEFAULT_MAX_RDMA_SIZE) {
3267 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3268 val, DEFAULT_MAX_RDMA_SIZE);
3269 return -EINVAL;
3270 }
3271 sport->port_attrib.srp_max_rdma_size = val;
3272
3273 return count;
3274}
3275
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003276static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3277 char *page)
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
3282 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3283}
3284
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003285static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3286 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003287{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003288 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003289 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3290 unsigned long val;
3291 int ret;
3292
Jingoo Han9d8abf42014-02-05 11:22:05 +09003293 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003294 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003295 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003296 return -EINVAL;
3297 }
3298 if (val > MAX_SRPT_RSP_SIZE) {
3299 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3300 MAX_SRPT_RSP_SIZE);
3301 return -EINVAL;
3302 }
3303 if (val < MIN_MAX_RSP_SIZE) {
3304 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3305 MIN_MAX_RSP_SIZE);
3306 return -EINVAL;
3307 }
3308 sport->port_attrib.srp_max_rsp_size = val;
3309
3310 return count;
3311}
3312
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003313static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3314 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003315{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003316 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003317 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3318
3319 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3320}
3321
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003322static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3323 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003324{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003325 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003326 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3327 unsigned long val;
3328 int ret;
3329
Jingoo Han9d8abf42014-02-05 11:22:05 +09003330 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003331 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003332 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003333 return -EINVAL;
3334 }
3335 if (val > MAX_SRPT_SRQ_SIZE) {
3336 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3337 MAX_SRPT_SRQ_SIZE);
3338 return -EINVAL;
3339 }
3340 if (val < MIN_SRPT_SRQ_SIZE) {
3341 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3342 MIN_SRPT_SRQ_SIZE);
3343 return -EINVAL;
3344 }
3345 sport->port_attrib.srp_sq_size = val;
3346
3347 return count;
3348}
3349
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003350CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3351CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3352CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003353
3354static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003355 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3356 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3357 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003358 NULL,
3359};
3360
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003361static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003362{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003363 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003364 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3365
3366 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3367}
3368
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003369static ssize_t srpt_tpg_enable_store(struct config_item *item,
3370 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003371{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003372 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003373 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3374 unsigned long tmp;
3375 int ret;
3376
Jingoo Han9d8abf42014-02-05 11:22:05 +09003377 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003378 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003379 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003380 return -EINVAL;
3381 }
3382
3383 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003384 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003385 return -EINVAL;
3386 }
3387 if (tmp == 1)
3388 sport->enabled = true;
3389 else
3390 sport->enabled = false;
3391
3392 return count;
3393}
3394
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003395CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003396
3397static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003398 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003399 NULL,
3400};
3401
3402/**
3403 * configfs callback invoked for
3404 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3405 */
3406static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3407 struct config_group *group,
3408 const char *name)
3409{
3410 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3411 int res;
3412
3413 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003414 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003415 if (res)
3416 return ERR_PTR(res);
3417
3418 return &sport->port_tpg_1;
3419}
3420
3421/**
3422 * configfs callback invoked for
3423 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3424 */
3425static void srpt_drop_tpg(struct se_portal_group *tpg)
3426{
3427 struct srpt_port *sport = container_of(tpg,
3428 struct srpt_port, port_tpg_1);
3429
3430 sport->enabled = false;
3431 core_tpg_deregister(&sport->port_tpg_1);
3432}
3433
3434/**
3435 * configfs callback invoked for
3436 * mkdir /sys/kernel/config/target/$driver/$port
3437 */
3438static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3439 struct config_group *group,
3440 const char *name)
3441{
3442 struct srpt_port *sport;
3443 int ret;
3444
3445 sport = srpt_lookup_port(name);
3446 pr_debug("make_tport(%s)\n", name);
3447 ret = -EINVAL;
3448 if (!sport)
3449 goto err;
3450
3451 return &sport->port_wwn;
3452
3453err:
3454 return ERR_PTR(ret);
3455}
3456
3457/**
3458 * configfs callback invoked for
3459 * rmdir /sys/kernel/config/target/$driver/$port
3460 */
3461static void srpt_drop_tport(struct se_wwn *wwn)
3462{
3463 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3464
3465 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3466}
3467
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003468static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003469{
3470 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3471}
3472
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003473CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003474
3475static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003476 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003477 NULL,
3478};
3479
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003480static const struct target_core_fabric_ops srpt_template = {
3481 .module = THIS_MODULE,
3482 .name = "srpt",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003483 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003484 .tpg_get_wwn = srpt_get_fabric_wwn,
3485 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003486 .tpg_check_demo_mode = srpt_check_false,
3487 .tpg_check_demo_mode_cache = srpt_check_true,
3488 .tpg_check_demo_mode_write_protect = srpt_check_true,
3489 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003490 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3491 .release_cmd = srpt_release_cmd,
3492 .check_stop_free = srpt_check_stop_free,
3493 .shutdown_session = srpt_shutdown_session,
3494 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003495 .sess_get_index = srpt_sess_get_index,
3496 .sess_get_initiator_sid = NULL,
3497 .write_pending = srpt_write_pending,
3498 .write_pending_status = srpt_write_pending_status,
3499 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003500 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003501 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003502 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003503 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003504 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003505 /*
3506 * Setup function pointers for generic logic in
3507 * target_core_fabric_configfs.c
3508 */
3509 .fabric_make_wwn = srpt_make_tport,
3510 .fabric_drop_wwn = srpt_drop_tport,
3511 .fabric_make_tpg = srpt_make_tpg,
3512 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003513 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003514
3515 .tfc_wwn_attrs = srpt_wwn_attrs,
3516 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3517 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003518};
3519
3520/**
3521 * srpt_init_module() - Kernel module initialization.
3522 *
3523 * Note: Since ib_register_client() registers callback functions, and since at
3524 * least one of these callback functions (srpt_add_one()) calls target core
3525 * functions, this driver must be registered with the target core before
3526 * ib_register_client() is called.
3527 */
3528static int __init srpt_init_module(void)
3529{
3530 int ret;
3531
3532 ret = -EINVAL;
3533 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003534 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003535 " srp_max_req_size -- must be at least %d.\n",
3536 srp_max_req_size, MIN_MAX_REQ_SIZE);
3537 goto out;
3538 }
3539
3540 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3541 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003542 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003543 " srpt_srq_size -- must be in the range [%d..%d].\n",
3544 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3545 goto out;
3546 }
3547
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003548 ret = target_register_template(&srpt_template);
3549 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003550 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003551
3552 ret = ib_register_client(&srpt_client);
3553 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003554 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003555 goto out_unregister_target;
3556 }
3557
3558 return 0;
3559
3560out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003561 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003562out:
3563 return ret;
3564}
3565
3566static void __exit srpt_cleanup_module(void)
3567{
3568 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003569 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003570}
3571
3572module_init(srpt_init_module);
3573module_exit(srpt_cleanup_module);