blob: bc5470c43d26080c5fa0f9d0e6427e848ae2771a [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);
96
97/**
98 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
99 */
100static inline
101enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
102{
103 switch (dir) {
104 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
105 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
106 default: return dir;
107 }
108}
109
110/**
111 * srpt_sdev_name() - Return the name associated with the HCA.
112 *
113 * Examples are ib0, ib1, ...
114 */
115static inline const char *srpt_sdev_name(struct srpt_device *sdev)
116{
117 return sdev->device->name;
118}
119
120static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
121{
122 unsigned long flags;
123 enum rdma_ch_state state;
124
125 spin_lock_irqsave(&ch->spinlock, flags);
126 state = ch->state;
127 spin_unlock_irqrestore(&ch->spinlock, flags);
128 return state;
129}
130
131static enum rdma_ch_state
132srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
133{
134 unsigned long flags;
135 enum rdma_ch_state prev;
136
137 spin_lock_irqsave(&ch->spinlock, flags);
138 prev = ch->state;
139 ch->state = new_state;
140 spin_unlock_irqrestore(&ch->spinlock, flags);
141 return prev;
142}
143
144/**
145 * srpt_test_and_set_ch_state() - Test and set the channel state.
146 *
147 * Returns true if and only if the channel state has been set to the new state.
148 */
149static bool
150srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
151 enum rdma_ch_state new)
152{
153 unsigned long flags;
154 enum rdma_ch_state prev;
155
156 spin_lock_irqsave(&ch->spinlock, flags);
157 prev = ch->state;
158 if (prev == old)
159 ch->state = new;
160 spin_unlock_irqrestore(&ch->spinlock, flags);
161 return prev == old;
162}
163
164/**
165 * srpt_event_handler() - Asynchronous IB event callback function.
166 *
167 * Callback function called by the InfiniBand core when an asynchronous IB
168 * event occurs. This callback may occur in interrupt context. See also
169 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
170 * Architecture Specification.
171 */
172static void srpt_event_handler(struct ib_event_handler *handler,
173 struct ib_event *event)
174{
175 struct srpt_device *sdev;
176 struct srpt_port *sport;
177
178 sdev = ib_get_client_data(event->device, &srpt_client);
179 if (!sdev || sdev->device != event->device)
180 return;
181
182 pr_debug("ASYNC event= %d on device= %s\n", event->event,
183 srpt_sdev_name(sdev));
184
185 switch (event->event) {
186 case IB_EVENT_PORT_ERR:
187 if (event->element.port_num <= sdev->device->phys_port_cnt) {
188 sport = &sdev->port[event->element.port_num - 1];
189 sport->lid = 0;
190 sport->sm_lid = 0;
191 }
192 break;
193 case IB_EVENT_PORT_ACTIVE:
194 case IB_EVENT_LID_CHANGE:
195 case IB_EVENT_PKEY_CHANGE:
196 case IB_EVENT_SM_CHANGE:
197 case IB_EVENT_CLIENT_REREGISTER:
Doug Ledford2aa1cf62014-08-12 19:20:10 -0400198 case IB_EVENT_GID_CHANGE:
Bart Van Asschea42d9852011-10-14 01:30:46 +0000199 /* Refresh port data asynchronously. */
200 if (event->element.port_num <= sdev->device->phys_port_cnt) {
201 sport = &sdev->port[event->element.port_num - 1];
202 if (!sport->lid && !sport->sm_lid)
203 schedule_work(&sport->work);
204 }
205 break;
206 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400207 pr_err("received unrecognized IB event %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +0000208 event->event);
209 break;
210 }
211}
212
213/**
214 * srpt_srq_event() - SRQ event callback function.
215 */
216static void srpt_srq_event(struct ib_event *event, void *ctx)
217{
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400218 pr_info("SRQ event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000219}
220
221/**
222 * srpt_qp_event() - QP event callback function.
223 */
224static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
225{
226 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
227 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
228
229 switch (event->event) {
230 case IB_EVENT_COMM_EST:
231 ib_cm_notify(ch->cm_id, event->event);
232 break;
233 case IB_EVENT_QP_LAST_WQE_REACHED:
234 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
235 CH_RELEASING))
236 srpt_release_channel(ch);
237 else
238 pr_debug("%s: state %d - ignored LAST_WQE.\n",
239 ch->sess_name, srpt_get_ch_state(ch));
240 break;
241 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400242 pr_err("received unrecognized IB QP event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000243 break;
244 }
245}
246
247/**
248 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
249 *
250 * @slot: one-based slot number.
251 * @value: four-bit value.
252 *
253 * Copies the lowest four bits of value in element slot of the array of four
254 * bit elements called c_list (controller list). The index slot is one-based.
255 */
256static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
257{
258 u16 id;
259 u8 tmp;
260
261 id = (slot - 1) / 2;
262 if (slot & 0x1) {
263 tmp = c_list[id] & 0xf;
264 c_list[id] = (value << 4) | tmp;
265 } else {
266 tmp = c_list[id] & 0xf0;
267 c_list[id] = (value & 0xf) | tmp;
268 }
269}
270
271/**
272 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
273 *
274 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
275 * Specification.
276 */
277static void srpt_get_class_port_info(struct ib_dm_mad *mad)
278{
279 struct ib_class_port_info *cif;
280
281 cif = (struct ib_class_port_info *)mad->data;
282 memset(cif, 0, sizeof *cif);
283 cif->base_version = 1;
284 cif->class_version = 1;
285 cif->resp_time_value = 20;
286
287 mad->mad_hdr.status = 0;
288}
289
290/**
291 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
292 *
293 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
294 * Specification. See also section B.7, table B.6 in the SRP r16a document.
295 */
296static void srpt_get_iou(struct ib_dm_mad *mad)
297{
298 struct ib_dm_iou_info *ioui;
299 u8 slot;
300 int i;
301
302 ioui = (struct ib_dm_iou_info *)mad->data;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530303 ioui->change_id = cpu_to_be16(1);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000304 ioui->max_controllers = 16;
305
306 /* set present for slot 1 and empty for the rest */
307 srpt_set_ioc(ioui->controller_list, 1, 1);
308 for (i = 1, slot = 2; i < 16; i++, slot++)
309 srpt_set_ioc(ioui->controller_list, slot, 0);
310
311 mad->mad_hdr.status = 0;
312}
313
314/**
315 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
316 *
317 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
318 * Architecture Specification. See also section B.7, table B.7 in the SRP
319 * r16a document.
320 */
321static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
322 struct ib_dm_mad *mad)
323{
324 struct srpt_device *sdev = sport->sdev;
325 struct ib_dm_ioc_profile *iocp;
326
327 iocp = (struct ib_dm_ioc_profile *)mad->data;
328
329 if (!slot || slot > 16) {
330 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530331 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000332 return;
333 }
334
335 if (slot > 2) {
336 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530337 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000338 return;
339 }
340
341 memset(iocp, 0, sizeof *iocp);
342 strcpy(iocp->id_string, SRPT_ID_STRING);
343 iocp->guid = cpu_to_be64(srpt_service_guid);
344 iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
345 iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
346 iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
347 iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
348 iocp->subsys_device_id = 0x0;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530349 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
350 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
351 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
352 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000353 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
354 iocp->rdma_read_depth = 4;
355 iocp->send_size = cpu_to_be32(srp_max_req_size);
356 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
357 1U << 24));
358 iocp->num_svc_entries = 1;
359 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
360 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
361
362 mad->mad_hdr.status = 0;
363}
364
365/**
366 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
367 *
368 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
369 * Specification. See also section B.7, table B.8 in the SRP r16a document.
370 */
371static void srpt_get_svc_entries(u64 ioc_guid,
372 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
373{
374 struct ib_dm_svc_entries *svc_entries;
375
376 WARN_ON(!ioc_guid);
377
378 if (!slot || slot > 16) {
379 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530380 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000381 return;
382 }
383
384 if (slot > 2 || lo > hi || hi > 1) {
385 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530386 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000387 return;
388 }
389
390 svc_entries = (struct ib_dm_svc_entries *)mad->data;
391 memset(svc_entries, 0, sizeof *svc_entries);
392 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
393 snprintf(svc_entries->service_entries[0].name,
394 sizeof(svc_entries->service_entries[0].name),
395 "%s%016llx",
396 SRP_SERVICE_NAME_PREFIX,
397 ioc_guid);
398
399 mad->mad_hdr.status = 0;
400}
401
402/**
403 * srpt_mgmt_method_get() - Process a received management datagram.
404 * @sp: source port through which the MAD has been received.
405 * @rq_mad: received MAD.
406 * @rsp_mad: response MAD.
407 */
408static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
409 struct ib_dm_mad *rsp_mad)
410{
411 u16 attr_id;
412 u32 slot;
413 u8 hi, lo;
414
415 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
416 switch (attr_id) {
417 case DM_ATTR_CLASS_PORT_INFO:
418 srpt_get_class_port_info(rsp_mad);
419 break;
420 case DM_ATTR_IOU_INFO:
421 srpt_get_iou(rsp_mad);
422 break;
423 case DM_ATTR_IOC_PROFILE:
424 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
425 srpt_get_ioc(sp, slot, rsp_mad);
426 break;
427 case DM_ATTR_SVC_ENTRIES:
428 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
429 hi = (u8) ((slot >> 8) & 0xff);
430 lo = (u8) (slot & 0xff);
431 slot = (u16) ((slot >> 16) & 0xffff);
432 srpt_get_svc_entries(srpt_service_guid,
433 slot, hi, lo, rsp_mad);
434 break;
435 default:
436 rsp_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530437 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000438 break;
439 }
440}
441
442/**
443 * srpt_mad_send_handler() - Post MAD-send callback function.
444 */
445static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
446 struct ib_mad_send_wc *mad_wc)
447{
448 ib_destroy_ah(mad_wc->send_buf->ah);
449 ib_free_send_mad(mad_wc->send_buf);
450}
451
452/**
453 * srpt_mad_recv_handler() - MAD reception callback function.
454 */
455static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
456 struct ib_mad_recv_wc *mad_wc)
457{
458 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
459 struct ib_ah *ah;
460 struct ib_mad_send_buf *rsp;
461 struct ib_dm_mad *dm_mad;
462
463 if (!mad_wc || !mad_wc->recv_buf.mad)
464 return;
465
466 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
467 mad_wc->recv_buf.grh, mad_agent->port_num);
468 if (IS_ERR(ah))
469 goto err;
470
471 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
472
473 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
474 mad_wc->wc->pkey_index, 0,
475 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400476 GFP_KERNEL,
477 IB_MGMT_BASE_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000478 if (IS_ERR(rsp))
479 goto err_rsp;
480
481 rsp->ah = ah;
482
483 dm_mad = rsp->mad;
484 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
485 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
486 dm_mad->mad_hdr.status = 0;
487
488 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
489 case IB_MGMT_METHOD_GET:
490 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
491 break;
492 case IB_MGMT_METHOD_SET:
493 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530494 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000495 break;
496 default:
497 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530498 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000499 break;
500 }
501
502 if (!ib_post_send_mad(rsp, NULL)) {
503 ib_free_recv_mad(mad_wc);
504 /* will destroy_ah & free_send_mad in send completion */
505 return;
506 }
507
508 ib_free_send_mad(rsp);
509
510err_rsp:
511 ib_destroy_ah(ah);
512err:
513 ib_free_recv_mad(mad_wc);
514}
515
516/**
517 * srpt_refresh_port() - Configure a HCA port.
518 *
519 * Enable InfiniBand management datagram processing, update the cached sm_lid,
520 * lid and gid values, and register a callback function for processing MADs
521 * on the specified port.
522 *
523 * Note: It is safe to call this function more than once for the same port.
524 */
525static int srpt_refresh_port(struct srpt_port *sport)
526{
527 struct ib_mad_reg_req reg_req;
528 struct ib_port_modify port_modify;
529 struct ib_port_attr port_attr;
530 int ret;
531
532 memset(&port_modify, 0, sizeof port_modify);
533 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
534 port_modify.clr_port_cap_mask = 0;
535
536 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
537 if (ret)
538 goto err_mod_port;
539
540 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
541 if (ret)
542 goto err_query_port;
543
544 sport->sm_lid = port_attr.sm_lid;
545 sport->lid = port_attr.lid;
546
Matan Barak55ee3ab2015-10-15 18:38:45 +0300547 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
548 NULL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000549 if (ret)
550 goto err_query_port;
551
552 if (!sport->mad_agent) {
553 memset(&reg_req, 0, sizeof reg_req);
554 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
555 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
556 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
557 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
558
559 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
560 sport->port,
561 IB_QPT_GSI,
562 &reg_req, 0,
563 srpt_mad_send_handler,
564 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400565 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000566 if (IS_ERR(sport->mad_agent)) {
567 ret = PTR_ERR(sport->mad_agent);
568 sport->mad_agent = NULL;
569 goto err_query_port;
570 }
571 }
572
573 return 0;
574
575err_query_port:
576
577 port_modify.set_port_cap_mask = 0;
578 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
579 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
580
581err_mod_port:
582
583 return ret;
584}
585
586/**
587 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
588 *
589 * Note: It is safe to call this function more than once for the same device.
590 */
591static void srpt_unregister_mad_agent(struct srpt_device *sdev)
592{
593 struct ib_port_modify port_modify = {
594 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
595 };
596 struct srpt_port *sport;
597 int i;
598
599 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
600 sport = &sdev->port[i - 1];
601 WARN_ON(sport->port != i);
602 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400603 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000604 if (sport->mad_agent) {
605 ib_unregister_mad_agent(sport->mad_agent);
606 sport->mad_agent = NULL;
607 }
608 }
609}
610
611/**
612 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
613 */
614static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
615 int ioctx_size, int dma_size,
616 enum dma_data_direction dir)
617{
618 struct srpt_ioctx *ioctx;
619
620 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
621 if (!ioctx)
622 goto err;
623
624 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
625 if (!ioctx->buf)
626 goto err_free_ioctx;
627
628 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
629 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
630 goto err_free_buf;
631
632 return ioctx;
633
634err_free_buf:
635 kfree(ioctx->buf);
636err_free_ioctx:
637 kfree(ioctx);
638err:
639 return NULL;
640}
641
642/**
643 * srpt_free_ioctx() - Free an SRPT I/O context structure.
644 */
645static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
646 int dma_size, enum dma_data_direction dir)
647{
648 if (!ioctx)
649 return;
650
651 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
652 kfree(ioctx->buf);
653 kfree(ioctx);
654}
655
656/**
657 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
658 * @sdev: Device to allocate the I/O context ring for.
659 * @ring_size: Number of elements in the I/O context ring.
660 * @ioctx_size: I/O context size.
661 * @dma_size: DMA buffer size.
662 * @dir: DMA data direction.
663 */
664static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
665 int ring_size, int ioctx_size,
666 int dma_size, enum dma_data_direction dir)
667{
668 struct srpt_ioctx **ring;
669 int i;
670
671 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
672 && ioctx_size != sizeof(struct srpt_send_ioctx));
673
674 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
675 if (!ring)
676 goto out;
677 for (i = 0; i < ring_size; ++i) {
678 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
679 if (!ring[i])
680 goto err;
681 ring[i]->index = i;
682 }
683 goto out;
684
685err:
686 while (--i >= 0)
687 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
688 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100689 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000690out:
691 return ring;
692}
693
694/**
695 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
696 */
697static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
698 struct srpt_device *sdev, int ring_size,
699 int dma_size, enum dma_data_direction dir)
700{
701 int i;
702
703 for (i = 0; i < ring_size; ++i)
704 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
705 kfree(ioctx_ring);
706}
707
708/**
709 * srpt_get_cmd_state() - Get the state of a SCSI command.
710 */
711static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
712{
713 enum srpt_command_state state;
714 unsigned long flags;
715
716 BUG_ON(!ioctx);
717
718 spin_lock_irqsave(&ioctx->spinlock, flags);
719 state = ioctx->state;
720 spin_unlock_irqrestore(&ioctx->spinlock, flags);
721 return state;
722}
723
724/**
725 * srpt_set_cmd_state() - Set the state of a SCSI command.
726 *
727 * Does not modify the state of aborted commands. Returns the previous command
728 * state.
729 */
730static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
731 enum srpt_command_state new)
732{
733 enum srpt_command_state previous;
734 unsigned long flags;
735
736 BUG_ON(!ioctx);
737
738 spin_lock_irqsave(&ioctx->spinlock, flags);
739 previous = ioctx->state;
740 if (previous != SRPT_STATE_DONE)
741 ioctx->state = new;
742 spin_unlock_irqrestore(&ioctx->spinlock, flags);
743
744 return previous;
745}
746
747/**
748 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
749 *
750 * Returns true if and only if the previous command state was equal to 'old'.
751 */
752static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
753 enum srpt_command_state old,
754 enum srpt_command_state new)
755{
756 enum srpt_command_state previous;
757 unsigned long flags;
758
759 WARN_ON(!ioctx);
760 WARN_ON(old == SRPT_STATE_DONE);
761 WARN_ON(new == SRPT_STATE_NEW);
762
763 spin_lock_irqsave(&ioctx->spinlock, flags);
764 previous = ioctx->state;
765 if (previous == old)
766 ioctx->state = new;
767 spin_unlock_irqrestore(&ioctx->spinlock, flags);
768 return previous == old;
769}
770
771/**
772 * srpt_post_recv() - Post an IB receive request.
773 */
774static int srpt_post_recv(struct srpt_device *sdev,
775 struct srpt_recv_ioctx *ioctx)
776{
777 struct ib_sge list;
778 struct ib_recv_wr wr, *bad_wr;
779
780 BUG_ON(!sdev);
781 wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
782
783 list.addr = ioctx->ioctx.dma;
784 list.length = srp_max_req_size;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600785 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000786
787 wr.next = NULL;
788 wr.sg_list = &list;
789 wr.num_sge = 1;
790
791 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
792}
793
794/**
795 * srpt_post_send() - Post an IB send request.
796 *
797 * Returns zero upon success and a non-zero value upon failure.
798 */
799static int srpt_post_send(struct srpt_rdma_ch *ch,
800 struct srpt_send_ioctx *ioctx, int len)
801{
802 struct ib_sge list;
803 struct ib_send_wr wr, *bad_wr;
804 struct srpt_device *sdev = ch->sport->sdev;
805 int ret;
806
807 atomic_inc(&ch->req_lim);
808
809 ret = -ENOMEM;
810 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400811 pr_warn("IB send queue full (needed 1)\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000812 goto out;
813 }
814
815 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
816 DMA_TO_DEVICE);
817
818 list.addr = ioctx->ioctx.dma;
819 list.length = len;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600820 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000821
822 wr.next = NULL;
823 wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
824 wr.sg_list = &list;
825 wr.num_sge = 1;
826 wr.opcode = IB_WR_SEND;
827 wr.send_flags = IB_SEND_SIGNALED;
828
829 ret = ib_post_send(ch->qp, &wr, &bad_wr);
830
831out:
832 if (ret < 0) {
833 atomic_inc(&ch->sq_wr_avail);
834 atomic_dec(&ch->req_lim);
835 }
836 return ret;
837}
838
839/**
840 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
841 * @ioctx: Pointer to the I/O context associated with the request.
842 * @srp_cmd: Pointer to the SRP_CMD request data.
843 * @dir: Pointer to the variable to which the transfer direction will be
844 * written.
845 * @data_len: Pointer to the variable to which the total data length of all
846 * descriptors in the SRP_CMD request will be written.
847 *
848 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
849 *
850 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
851 * -ENOMEM when memory allocation fails and zero upon success.
852 */
853static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
854 struct srp_cmd *srp_cmd,
855 enum dma_data_direction *dir, u64 *data_len)
856{
857 struct srp_indirect_buf *idb;
858 struct srp_direct_buf *db;
859 unsigned add_cdb_offset;
860 int ret;
861
862 /*
863 * The pointer computations below will only be compiled correctly
864 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
865 * whether srp_cmd::add_data has been declared as a byte pointer.
866 */
867 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
868 && !__same_type(srp_cmd->add_data[0], (u8)0));
869
870 BUG_ON(!dir);
871 BUG_ON(!data_len);
872
873 ret = 0;
874 *data_len = 0;
875
876 /*
877 * The lower four bits of the buffer format field contain the DATA-IN
878 * buffer descriptor format, and the highest four bits contain the
879 * DATA-OUT buffer descriptor format.
880 */
881 *dir = DMA_NONE;
882 if (srp_cmd->buf_fmt & 0xf)
883 /* DATA-IN: transfer data from target to initiator (read). */
884 *dir = DMA_FROM_DEVICE;
885 else if (srp_cmd->buf_fmt >> 4)
886 /* DATA-OUT: transfer data from initiator to target (write). */
887 *dir = DMA_TO_DEVICE;
888
889 /*
890 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
891 * CDB LENGTH' field are reserved and the size in bytes of this field
892 * is four times the value specified in bits 3..7. Hence the "& ~3".
893 */
894 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
895 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
896 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
897 ioctx->n_rbuf = 1;
898 ioctx->rbufs = &ioctx->single_rbuf;
899
900 db = (struct srp_direct_buf *)(srp_cmd->add_data
901 + add_cdb_offset);
902 memcpy(ioctx->rbufs, db, sizeof *db);
903 *data_len = be32_to_cpu(db->len);
904 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
905 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
906 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
907 + add_cdb_offset);
908
909 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
910
911 if (ioctx->n_rbuf >
912 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400913 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000914 " type (%u out + %u in != %u / %zu)\n",
915 srp_cmd->data_out_desc_cnt,
916 srp_cmd->data_in_desc_cnt,
917 be32_to_cpu(idb->table_desc.len),
918 sizeof(*db));
919 ioctx->n_rbuf = 0;
920 ret = -EINVAL;
921 goto out;
922 }
923
924 if (ioctx->n_rbuf == 1)
925 ioctx->rbufs = &ioctx->single_rbuf;
926 else {
927 ioctx->rbufs =
928 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
929 if (!ioctx->rbufs) {
930 ioctx->n_rbuf = 0;
931 ret = -ENOMEM;
932 goto out;
933 }
934 }
935
936 db = idb->desc_list;
937 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
938 *data_len = be32_to_cpu(idb->len);
939 }
940out:
941 return ret;
942}
943
944/**
945 * srpt_init_ch_qp() - Initialize queue pair attributes.
946 *
947 * Initialized the attributes of queue pair 'qp' by allowing local write,
948 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
949 */
950static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
951{
952 struct ib_qp_attr *attr;
953 int ret;
954
955 attr = kzalloc(sizeof *attr, GFP_KERNEL);
956 if (!attr)
957 return -ENOMEM;
958
959 attr->qp_state = IB_QPS_INIT;
960 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
961 IB_ACCESS_REMOTE_WRITE;
962 attr->port_num = ch->sport->port;
963 attr->pkey_index = 0;
964
965 ret = ib_modify_qp(qp, attr,
966 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
967 IB_QP_PKEY_INDEX);
968
969 kfree(attr);
970 return ret;
971}
972
973/**
974 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
975 * @ch: channel of the queue pair.
976 * @qp: queue pair to change the state of.
977 *
978 * Returns zero upon success and a negative value upon failure.
979 *
980 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
981 * If this structure ever becomes larger, it might be necessary to allocate
982 * it dynamically instead of on the stack.
983 */
984static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
985{
986 struct ib_qp_attr qp_attr;
987 int attr_mask;
988 int ret;
989
990 qp_attr.qp_state = IB_QPS_RTR;
991 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
992 if (ret)
993 goto out;
994
995 qp_attr.max_dest_rd_atomic = 4;
996
997 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
998
999out:
1000 return ret;
1001}
1002
1003/**
1004 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1005 * @ch: channel of the queue pair.
1006 * @qp: queue pair to change the state of.
1007 *
1008 * Returns zero upon success and a negative value upon failure.
1009 *
1010 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1011 * If this structure ever becomes larger, it might be necessary to allocate
1012 * it dynamically instead of on the stack.
1013 */
1014static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1015{
1016 struct ib_qp_attr qp_attr;
1017 int attr_mask;
1018 int ret;
1019
1020 qp_attr.qp_state = IB_QPS_RTS;
1021 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1022 if (ret)
1023 goto out;
1024
1025 qp_attr.max_rd_atomic = 4;
1026
1027 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1028
1029out:
1030 return ret;
1031}
1032
1033/**
1034 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1035 */
1036static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1037{
1038 struct ib_qp_attr qp_attr;
1039
1040 qp_attr.qp_state = IB_QPS_ERR;
1041 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1042}
1043
1044/**
1045 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1046 */
1047static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1048 struct srpt_send_ioctx *ioctx)
1049{
1050 struct scatterlist *sg;
1051 enum dma_data_direction dir;
1052
1053 BUG_ON(!ch);
1054 BUG_ON(!ioctx);
1055 BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
1056
1057 while (ioctx->n_rdma)
1058 kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
1059
1060 kfree(ioctx->rdma_ius);
1061 ioctx->rdma_ius = NULL;
1062
1063 if (ioctx->mapped_sg_count) {
1064 sg = ioctx->sg;
1065 WARN_ON(!sg);
1066 dir = ioctx->cmd.data_direction;
1067 BUG_ON(dir == DMA_NONE);
1068 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1069 opposite_dma_dir(dir));
1070 ioctx->mapped_sg_count = 0;
1071 }
1072}
1073
1074/**
1075 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1076 */
1077static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1078 struct srpt_send_ioctx *ioctx)
1079{
Mike Marciniszynb0768082014-04-07 13:58:35 -04001080 struct ib_device *dev = ch->sport->sdev->device;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001081 struct se_cmd *cmd;
1082 struct scatterlist *sg, *sg_orig;
1083 int sg_cnt;
1084 enum dma_data_direction dir;
1085 struct rdma_iu *riu;
1086 struct srp_direct_buf *db;
1087 dma_addr_t dma_addr;
1088 struct ib_sge *sge;
1089 u64 raddr;
1090 u32 rsize;
1091 u32 tsize;
1092 u32 dma_len;
1093 int count, nrdma;
1094 int i, j, k;
1095
1096 BUG_ON(!ch);
1097 BUG_ON(!ioctx);
1098 cmd = &ioctx->cmd;
1099 dir = cmd->data_direction;
1100 BUG_ON(dir == DMA_NONE);
1101
Roland Dreier6f9e7f02012-03-30 11:29:12 -07001102 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1103 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001104
1105 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1106 opposite_dma_dir(dir));
1107 if (unlikely(!count))
1108 return -EAGAIN;
1109
1110 ioctx->mapped_sg_count = count;
1111
1112 if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1113 nrdma = ioctx->n_rdma_ius;
1114 else {
1115 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1116 + ioctx->n_rbuf;
1117
1118 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
1119 if (!ioctx->rdma_ius)
1120 goto free_mem;
1121
1122 ioctx->n_rdma_ius = nrdma;
1123 }
1124
1125 db = ioctx->rbufs;
1126 tsize = cmd->data_length;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001127 dma_len = ib_sg_dma_len(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001128 riu = ioctx->rdma_ius;
1129
1130 /*
1131 * For each remote desc - calculate the #ib_sge.
1132 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1133 * each remote desc rdma_iu is required a rdma wr;
1134 * else
1135 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1136 * another rdma wr
1137 */
1138 for (i = 0, j = 0;
1139 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1140 rsize = be32_to_cpu(db->len);
1141 raddr = be64_to_cpu(db->va);
1142 riu->raddr = raddr;
1143 riu->rkey = be32_to_cpu(db->key);
1144 riu->sge_cnt = 0;
1145
1146 /* calculate how many sge required for this remote_buf */
1147 while (rsize > 0 && tsize > 0) {
1148
1149 if (rsize >= dma_len) {
1150 tsize -= dma_len;
1151 rsize -= dma_len;
1152 raddr += dma_len;
1153
1154 if (tsize > 0) {
1155 ++j;
1156 if (j < count) {
1157 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001158 dma_len = ib_sg_dma_len(
1159 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001160 }
1161 }
1162 } else {
1163 tsize -= rsize;
1164 dma_len -= rsize;
1165 rsize = 0;
1166 }
1167
1168 ++riu->sge_cnt;
1169
1170 if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1171 ++ioctx->n_rdma;
1172 riu->sge =
1173 kmalloc(riu->sge_cnt * sizeof *riu->sge,
1174 GFP_KERNEL);
1175 if (!riu->sge)
1176 goto free_mem;
1177
1178 ++riu;
1179 riu->sge_cnt = 0;
1180 riu->raddr = raddr;
1181 riu->rkey = be32_to_cpu(db->key);
1182 }
1183 }
1184
1185 ++ioctx->n_rdma;
1186 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1187 GFP_KERNEL);
1188 if (!riu->sge)
1189 goto free_mem;
1190 }
1191
1192 db = ioctx->rbufs;
1193 tsize = cmd->data_length;
1194 riu = ioctx->rdma_ius;
1195 sg = sg_orig;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001196 dma_len = ib_sg_dma_len(dev, &sg[0]);
1197 dma_addr = ib_sg_dma_address(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001198
1199 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1200 for (i = 0, j = 0;
1201 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1202 rsize = be32_to_cpu(db->len);
1203 sge = riu->sge;
1204 k = 0;
1205
1206 while (rsize > 0 && tsize > 0) {
1207 sge->addr = dma_addr;
Jason Gunthorpe5a783952015-07-30 17:22:24 -06001208 sge->lkey = ch->sport->sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001209
1210 if (rsize >= dma_len) {
1211 sge->length =
1212 (tsize < dma_len) ? tsize : dma_len;
1213 tsize -= dma_len;
1214 rsize -= dma_len;
1215
1216 if (tsize > 0) {
1217 ++j;
1218 if (j < count) {
1219 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001220 dma_len = ib_sg_dma_len(
1221 dev, sg);
1222 dma_addr = ib_sg_dma_address(
1223 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001224 }
1225 }
1226 } else {
1227 sge->length = (tsize < rsize) ? tsize : rsize;
1228 tsize -= rsize;
1229 dma_len -= rsize;
1230 dma_addr += rsize;
1231 rsize = 0;
1232 }
1233
1234 ++k;
1235 if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1236 ++riu;
1237 sge = riu->sge;
1238 k = 0;
1239 } else if (rsize > 0 && tsize > 0)
1240 ++sge;
1241 }
1242 }
1243
1244 return 0;
1245
1246free_mem:
1247 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1248
1249 return -ENOMEM;
1250}
1251
1252/**
1253 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1254 */
1255static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1256{
1257 struct srpt_send_ioctx *ioctx;
1258 unsigned long flags;
1259
1260 BUG_ON(!ch);
1261
1262 ioctx = NULL;
1263 spin_lock_irqsave(&ch->spinlock, flags);
1264 if (!list_empty(&ch->free_list)) {
1265 ioctx = list_first_entry(&ch->free_list,
1266 struct srpt_send_ioctx, free_list);
1267 list_del(&ioctx->free_list);
1268 }
1269 spin_unlock_irqrestore(&ch->spinlock, flags);
1270
1271 if (!ioctx)
1272 return ioctx;
1273
1274 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001275 spin_lock_init(&ioctx->spinlock);
1276 ioctx->state = SRPT_STATE_NEW;
1277 ioctx->n_rbuf = 0;
1278 ioctx->rbufs = NULL;
1279 ioctx->n_rdma = 0;
1280 ioctx->n_rdma_ius = 0;
1281 ioctx->rdma_ius = NULL;
1282 ioctx->mapped_sg_count = 0;
1283 init_completion(&ioctx->tx_done);
1284 ioctx->queue_status_only = false;
1285 /*
1286 * transport_init_se_cmd() does not initialize all fields, so do it
1287 * here.
1288 */
1289 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1290 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1291
1292 return ioctx;
1293}
1294
1295/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001296 * srpt_abort_cmd() - Abort a SCSI command.
1297 * @ioctx: I/O context associated with the SCSI command.
1298 * @context: Preferred execution context.
1299 */
1300static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1301{
1302 enum srpt_command_state state;
1303 unsigned long flags;
1304
1305 BUG_ON(!ioctx);
1306
1307 /*
1308 * If the command is in a state where the target core is waiting for
1309 * the ib_srpt driver, change the state to the next state. Changing
1310 * the state of the command from SRPT_STATE_NEED_DATA to
1311 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1312 * function a second time.
1313 */
1314
1315 spin_lock_irqsave(&ioctx->spinlock, flags);
1316 state = ioctx->state;
1317 switch (state) {
1318 case SRPT_STATE_NEED_DATA:
1319 ioctx->state = SRPT_STATE_DATA_IN;
1320 break;
1321 case SRPT_STATE_DATA_IN:
1322 case SRPT_STATE_CMD_RSP_SENT:
1323 case SRPT_STATE_MGMT_RSP_SENT:
1324 ioctx->state = SRPT_STATE_DONE;
1325 break;
1326 default:
1327 break;
1328 }
1329 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1330
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001331 if (state == SRPT_STATE_DONE) {
1332 struct srpt_rdma_ch *ch = ioctx->ch;
1333
1334 BUG_ON(ch->sess == NULL);
1335
Bart Van Asscheafc16602015-04-27 13:52:36 +02001336 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001337 goto out;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001338 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001339
1340 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
Bart Van Assche649ee052015-04-14 13:26:44 +02001341 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001342
1343 switch (state) {
1344 case SRPT_STATE_NEW:
1345 case SRPT_STATE_DATA_IN:
1346 case SRPT_STATE_MGMT:
1347 /*
1348 * Do nothing - defer abort processing until
1349 * srpt_queue_response() is invoked.
1350 */
1351 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1352 break;
1353 case SRPT_STATE_NEED_DATA:
1354 /* DMA_TO_DEVICE (write) - RDMA read error. */
Christoph Hellwige672a472012-07-08 15:58:43 -04001355
1356 /* XXX(hch): this is a horrible layering violation.. */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001357 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
Christoph Hellwige672a472012-07-08 15:58:43 -04001358 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001359 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001360 break;
1361 case SRPT_STATE_CMD_RSP_SENT:
1362 /*
1363 * SRP_RSP sending failed or the SRP_RSP send completion has
1364 * not been received in time.
1365 */
1366 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001367 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001368 break;
1369 case SRPT_STATE_MGMT_RSP_SENT:
1370 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001371 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001372 break;
1373 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001374 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001375 break;
1376 }
1377
1378out:
1379 return state;
1380}
1381
1382/**
1383 * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1384 */
1385static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1386{
1387 struct srpt_send_ioctx *ioctx;
1388 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001389 u32 index;
1390
1391 atomic_inc(&ch->sq_wr_avail);
1392
1393 index = idx_from_wr_id(wr_id);
1394 ioctx = ch->ioctx_ring[index];
1395 state = srpt_get_cmd_state(ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001396
1397 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1398 && state != SRPT_STATE_MGMT_RSP_SENT
1399 && state != SRPT_STATE_NEED_DATA
1400 && state != SRPT_STATE_DONE);
1401
1402 /* If SRP_RSP sending failed, undo the ch->req_lim change. */
1403 if (state == SRPT_STATE_CMD_RSP_SENT
1404 || state == SRPT_STATE_MGMT_RSP_SENT)
1405 atomic_dec(&ch->req_lim);
1406
1407 srpt_abort_cmd(ioctx);
1408}
1409
1410/**
1411 * srpt_handle_send_comp() - Process an IB send completion notification.
1412 */
1413static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1414 struct srpt_send_ioctx *ioctx)
1415{
1416 enum srpt_command_state state;
1417
1418 atomic_inc(&ch->sq_wr_avail);
1419
1420 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1421
1422 if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1423 && state != SRPT_STATE_MGMT_RSP_SENT
1424 && state != SRPT_STATE_DONE))
1425 pr_debug("state = %d\n", state);
1426
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001427 if (state != SRPT_STATE_DONE) {
1428 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1429 transport_generic_free_cmd(&ioctx->cmd, 0);
1430 } else {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001431 pr_err("IB completion has been received too late for"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001432 " wr_id = %u.\n", ioctx->ioctx.index);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001433 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001434}
1435
1436/**
1437 * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1438 *
Christoph Hellwige672a472012-07-08 15:58:43 -04001439 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1440 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001441 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001442 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001443 */
1444static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1445 struct srpt_send_ioctx *ioctx,
1446 enum srpt_opcode opcode)
1447{
1448 WARN_ON(ioctx->n_rdma <= 0);
1449 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1450
1451 if (opcode == SRPT_RDMA_READ_LAST) {
1452 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1453 SRPT_STATE_DATA_IN))
Christoph Hellwige672a472012-07-08 15:58:43 -04001454 target_execute_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001455 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001456 pr_err("%s[%d]: wrong state = %d\n", __func__,
Bart Van Asschea42d9852011-10-14 01:30:46 +00001457 __LINE__, srpt_get_cmd_state(ioctx));
1458 } else if (opcode == SRPT_RDMA_ABORT) {
1459 ioctx->rdma_aborted = true;
1460 } else {
1461 WARN(true, "unexpected opcode %d\n", opcode);
1462 }
1463}
1464
1465/**
1466 * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1467 */
1468static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1469 struct srpt_send_ioctx *ioctx,
1470 enum srpt_opcode opcode)
1471{
Bart Van Asschea42d9852011-10-14 01:30:46 +00001472 enum srpt_command_state state;
1473
Bart Van Asschea42d9852011-10-14 01:30:46 +00001474 state = srpt_get_cmd_state(ioctx);
1475 switch (opcode) {
1476 case SRPT_RDMA_READ_LAST:
1477 if (ioctx->n_rdma <= 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001478 pr_err("Received invalid RDMA read"
Bart Van Asschea42d9852011-10-14 01:30:46 +00001479 " error completion with idx %d\n",
1480 ioctx->ioctx.index);
1481 break;
1482 }
1483 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1484 if (state == SRPT_STATE_NEED_DATA)
1485 srpt_abort_cmd(ioctx);
1486 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001487 pr_err("%s[%d]: wrong state = %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001488 __func__, __LINE__, state);
1489 break;
1490 case SRPT_RDMA_WRITE_LAST:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001491 break;
1492 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001493 pr_err("%s[%d]: opcode = %u\n", __func__, __LINE__, opcode);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001494 break;
1495 }
1496}
1497
1498/**
1499 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1500 * @ch: RDMA channel through which the request has been received.
1501 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1502 * be built in the buffer ioctx->buf points at and hence this function will
1503 * overwrite the request data.
1504 * @tag: tag of the request for which this response is being generated.
1505 * @status: value for the STATUS field of the SRP_RSP information unit.
1506 *
1507 * Returns the size in bytes of the SRP_RSP response.
1508 *
1509 * An SRP_RSP response contains a SCSI status or service response. See also
1510 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1511 * response. See also SPC-2 for more information about sense data.
1512 */
1513static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1514 struct srpt_send_ioctx *ioctx, u64 tag,
1515 int status)
1516{
1517 struct srp_rsp *srp_rsp;
1518 const u8 *sense_data;
1519 int sense_data_len, max_sense_len;
1520
1521 /*
1522 * The lowest bit of all SAM-3 status codes is zero (see also
1523 * paragraph 5.3 in SAM-3).
1524 */
1525 WARN_ON(status & 1);
1526
1527 srp_rsp = ioctx->ioctx.buf;
1528 BUG_ON(!srp_rsp);
1529
1530 sense_data = ioctx->sense_data;
1531 sense_data_len = ioctx->cmd.scsi_sense_length;
1532 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1533
1534 memset(srp_rsp, 0, sizeof *srp_rsp);
1535 srp_rsp->opcode = SRP_RSP;
1536 srp_rsp->req_lim_delta =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301537 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001538 srp_rsp->tag = tag;
1539 srp_rsp->status = status;
1540
1541 if (sense_data_len) {
1542 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1543 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1544 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001545 pr_warn("truncated sense data from %d to %d"
1546 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001547 sense_data_len = max_sense_len;
1548 }
1549
1550 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1551 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1552 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1553 }
1554
1555 return sizeof(*srp_rsp) + sense_data_len;
1556}
1557
1558/**
1559 * srpt_build_tskmgmt_rsp() - Build a task management response.
1560 * @ch: RDMA channel through which the request has been received.
1561 * @ioctx: I/O context in which the SRP_RSP response will be built.
1562 * @rsp_code: RSP_CODE that will be stored in the response.
1563 * @tag: Tag of the request for which this response is being generated.
1564 *
1565 * Returns the size in bytes of the SRP_RSP response.
1566 *
1567 * An SRP_RSP response contains a SCSI status or service response. See also
1568 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1569 * response.
1570 */
1571static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1572 struct srpt_send_ioctx *ioctx,
1573 u8 rsp_code, u64 tag)
1574{
1575 struct srp_rsp *srp_rsp;
1576 int resp_data_len;
1577 int resp_len;
1578
Jack Wangc807f642013-09-30 10:09:05 +02001579 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001580 resp_len = sizeof(*srp_rsp) + resp_data_len;
1581
1582 srp_rsp = ioctx->ioctx.buf;
1583 BUG_ON(!srp_rsp);
1584 memset(srp_rsp, 0, sizeof *srp_rsp);
1585
1586 srp_rsp->opcode = SRP_RSP;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301587 srp_rsp->req_lim_delta =
1588 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001589 srp_rsp->tag = tag;
1590
Jack Wangc807f642013-09-30 10:09:05 +02001591 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1592 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1593 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001594
1595 return resp_len;
1596}
1597
1598#define NO_SUCH_LUN ((uint64_t)-1LL)
1599
1600/*
1601 * SCSI LUN addressing method. See also SAM-2 and the section about
1602 * eight byte LUNs.
1603 */
1604enum scsi_lun_addr_method {
1605 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1606 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1607 SCSI_LUN_ADDR_METHOD_LUN = 2,
1608 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1609};
1610
1611/*
1612 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1613 *
1614 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1615 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1616 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1617 */
1618static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1619{
1620 uint64_t res = NO_SUCH_LUN;
1621 int addressing_method;
1622
1623 if (unlikely(len < 2)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001624 pr_err("Illegal LUN length %d, expected 2 bytes or more\n",
1625 len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001626 goto out;
1627 }
1628
1629 switch (len) {
1630 case 8:
1631 if ((*((__be64 *)lun) &
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301632 cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001633 goto out_err;
1634 break;
1635 case 4:
1636 if (*((__be16 *)&lun[2]) != 0)
1637 goto out_err;
1638 break;
1639 case 6:
1640 if (*((__be32 *)&lun[2]) != 0)
1641 goto out_err;
1642 break;
1643 case 2:
1644 break;
1645 default:
1646 goto out_err;
1647 }
1648
1649 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1650 switch (addressing_method) {
1651 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1652 case SCSI_LUN_ADDR_METHOD_FLAT:
1653 case SCSI_LUN_ADDR_METHOD_LUN:
1654 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1655 break;
1656
1657 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1658 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001659 pr_err("Unimplemented LUN addressing method %u\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001660 addressing_method);
1661 break;
1662 }
1663
1664out:
1665 return res;
1666
1667out_err:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001668 pr_err("Support for multi-level LUNs has not yet been implemented\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001669 goto out;
1670}
1671
1672static int srpt_check_stop_free(struct se_cmd *cmd)
1673{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001674 struct srpt_send_ioctx *ioctx = container_of(cmd,
1675 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001676
Bart Van Asscheafc16602015-04-27 13:52:36 +02001677 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001678}
1679
1680/**
1681 * srpt_handle_cmd() - Process SRP_CMD.
1682 */
1683static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1684 struct srpt_recv_ioctx *recv_ioctx,
1685 struct srpt_send_ioctx *send_ioctx)
1686{
1687 struct se_cmd *cmd;
1688 struct srp_cmd *srp_cmd;
1689 uint64_t unpacked_lun;
1690 u64 data_len;
1691 enum dma_data_direction dir;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001692 sense_reason_t ret;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001693 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001694
1695 BUG_ON(!send_ioctx);
1696
1697 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001698 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001699 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001700
1701 switch (srp_cmd->task_attr) {
1702 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001703 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001704 break;
1705 case SRP_CMD_ORDERED_Q:
1706 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001707 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001708 break;
1709 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001710 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001711 break;
1712 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001713 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001714 break;
1715 }
1716
Christoph Hellwigde103c92012-11-06 12:24:09 -08001717 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001718 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001719 srp_cmd->tag);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001720 ret = TCM_INVALID_CDB_FIELD;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001721 goto send_sense;
1722 }
1723
Bart Van Asschea42d9852011-10-14 01:30:46 +00001724 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1725 sizeof(srp_cmd->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001726 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1727 &send_ioctx->sense_data[0], unpacked_lun, data_len,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001728 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001729 if (rc != 0) {
1730 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001731 goto send_sense;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001732 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001733 return 0;
1734
1735send_sense:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001736 transport_send_check_condition_and_sense(cmd, ret, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001737 return -1;
1738}
1739
1740/**
1741 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1742 * @ch: RDMA channel of the task management request.
1743 * @fn: Task management function to perform.
1744 * @req_tag: Tag of the SRP task management request.
1745 * @mgmt_ioctx: I/O context of the task management request.
1746 *
1747 * Returns zero if the target core will process the task management
1748 * request asynchronously.
1749 *
1750 * Note: It is assumed that the initiator serializes tag-based task management
1751 * requests.
1752 */
1753static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1754{
1755 struct srpt_device *sdev;
1756 struct srpt_rdma_ch *ch;
1757 struct srpt_send_ioctx *target;
1758 int ret, i;
1759
1760 ret = -EINVAL;
1761 ch = ioctx->ch;
1762 BUG_ON(!ch);
1763 BUG_ON(!ch->sport);
1764 sdev = ch->sport->sdev;
1765 BUG_ON(!sdev);
1766 spin_lock_irq(&sdev->spinlock);
1767 for (i = 0; i < ch->rq_size; ++i) {
1768 target = ch->ioctx_ring[i];
1769 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
Bart Van Assche649ee052015-04-14 13:26:44 +02001770 target->cmd.tag == tag &&
Bart Van Asschea42d9852011-10-14 01:30:46 +00001771 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1772 ret = 0;
1773 /* now let the target core abort &target->cmd; */
1774 break;
1775 }
1776 }
1777 spin_unlock_irq(&sdev->spinlock);
1778 return ret;
1779}
1780
1781static int srp_tmr_to_tcm(int fn)
1782{
1783 switch (fn) {
1784 case SRP_TSK_ABORT_TASK:
1785 return TMR_ABORT_TASK;
1786 case SRP_TSK_ABORT_TASK_SET:
1787 return TMR_ABORT_TASK_SET;
1788 case SRP_TSK_CLEAR_TASK_SET:
1789 return TMR_CLEAR_TASK_SET;
1790 case SRP_TSK_LUN_RESET:
1791 return TMR_LUN_RESET;
1792 case SRP_TSK_CLEAR_ACA:
1793 return TMR_CLEAR_ACA;
1794 default:
1795 return -1;
1796 }
1797}
1798
1799/**
1800 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1801 *
1802 * Returns 0 if and only if the request will be processed by the target core.
1803 *
1804 * For more information about SRP_TSK_MGMT information units, see also section
1805 * 6.7 in the SRP r16a document.
1806 */
1807static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1808 struct srpt_recv_ioctx *recv_ioctx,
1809 struct srpt_send_ioctx *send_ioctx)
1810{
1811 struct srp_tsk_mgmt *srp_tsk;
1812 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001813 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001814 uint64_t unpacked_lun;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001815 uint32_t tag = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001816 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001817 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001818
1819 BUG_ON(!send_ioctx);
1820
1821 srp_tsk = recv_ioctx->ioctx.buf;
1822 cmd = &send_ioctx->cmd;
1823
1824 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1825 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1826 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1827
1828 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001829 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001830 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1831 if (tcm_tmr < 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001832 send_ioctx->cmd.se_tmr_req->response =
1833 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001834 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001835 }
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001836 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1837 sizeof(srp_tsk->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001838
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001839 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK) {
1840 rc = srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1841 if (rc < 0) {
1842 send_ioctx->cmd.se_tmr_req->response =
1843 TMR_TASK_DOES_NOT_EXIST;
1844 goto fail;
1845 }
1846 tag = srp_tsk->task_tag;
1847 }
1848 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
1849 srp_tsk, tcm_tmr, GFP_KERNEL, tag,
1850 TARGET_SCF_ACK_KREF);
1851 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001852 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001853 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001854 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001855 return;
1856fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001857 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001858}
1859
1860/**
1861 * srpt_handle_new_iu() - Process a newly received information unit.
1862 * @ch: RDMA channel through which the information unit has been received.
1863 * @ioctx: SRPT I/O context associated with the information unit.
1864 */
1865static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1866 struct srpt_recv_ioctx *recv_ioctx,
1867 struct srpt_send_ioctx *send_ioctx)
1868{
1869 struct srp_cmd *srp_cmd;
1870 enum rdma_ch_state ch_state;
1871
1872 BUG_ON(!ch);
1873 BUG_ON(!recv_ioctx);
1874
1875 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1876 recv_ioctx->ioctx.dma, srp_max_req_size,
1877 DMA_FROM_DEVICE);
1878
1879 ch_state = srpt_get_ch_state(ch);
1880 if (unlikely(ch_state == CH_CONNECTING)) {
1881 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1882 goto out;
1883 }
1884
1885 if (unlikely(ch_state != CH_LIVE))
1886 goto out;
1887
1888 srp_cmd = recv_ioctx->ioctx.buf;
1889 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1890 if (!send_ioctx)
1891 send_ioctx = srpt_get_send_ioctx(ch);
1892 if (unlikely(!send_ioctx)) {
1893 list_add_tail(&recv_ioctx->wait_list,
1894 &ch->cmd_wait_list);
1895 goto out;
1896 }
1897 }
1898
Bart Van Asschea42d9852011-10-14 01:30:46 +00001899 switch (srp_cmd->opcode) {
1900 case SRP_CMD:
1901 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1902 break;
1903 case SRP_TSK_MGMT:
1904 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1905 break;
1906 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001907 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001908 break;
1909 case SRP_CRED_RSP:
1910 pr_debug("received SRP_CRED_RSP\n");
1911 break;
1912 case SRP_AER_RSP:
1913 pr_debug("received SRP_AER_RSP\n");
1914 break;
1915 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001916 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001917 break;
1918 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001919 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001920 srp_cmd->opcode);
1921 break;
1922 }
1923
1924 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1925out:
1926 return;
1927}
1928
1929static void srpt_process_rcv_completion(struct ib_cq *cq,
1930 struct srpt_rdma_ch *ch,
1931 struct ib_wc *wc)
1932{
1933 struct srpt_device *sdev = ch->sport->sdev;
1934 struct srpt_recv_ioctx *ioctx;
1935 u32 index;
1936
1937 index = idx_from_wr_id(wc->wr_id);
1938 if (wc->status == IB_WC_SUCCESS) {
1939 int req_lim;
1940
1941 req_lim = atomic_dec_return(&ch->req_lim);
1942 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001943 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001944 ioctx = sdev->ioctx_ring[index];
1945 srpt_handle_new_iu(ch, ioctx, NULL);
1946 } else {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001947 pr_info("receiving failed for idx %u with status %d\n",
1948 index, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001949 }
1950}
1951
1952/**
1953 * srpt_process_send_completion() - Process an IB send completion.
1954 *
1955 * Note: Although this has not yet been observed during tests, at least in
1956 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1957 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1958 * value in each response is set to one, and it is possible that this response
1959 * makes the initiator send a new request before the send completion for that
1960 * response has been processed. This could e.g. happen if the call to
1961 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1962 * if IB retransmission causes generation of the send completion to be
1963 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1964 * are queued on cmd_wait_list. The code below processes these delayed
1965 * requests one at a time.
1966 */
1967static void srpt_process_send_completion(struct ib_cq *cq,
1968 struct srpt_rdma_ch *ch,
1969 struct ib_wc *wc)
1970{
1971 struct srpt_send_ioctx *send_ioctx;
1972 uint32_t index;
1973 enum srpt_opcode opcode;
1974
1975 index = idx_from_wr_id(wc->wr_id);
1976 opcode = opcode_from_wr_id(wc->wr_id);
1977 send_ioctx = ch->ioctx_ring[index];
1978 if (wc->status == IB_WC_SUCCESS) {
1979 if (opcode == SRPT_SEND)
1980 srpt_handle_send_comp(ch, send_ioctx);
1981 else {
1982 WARN_ON(opcode != SRPT_RDMA_ABORT &&
1983 wc->opcode != IB_WC_RDMA_READ);
1984 srpt_handle_rdma_comp(ch, send_ioctx, opcode);
1985 }
1986 } else {
1987 if (opcode == SRPT_SEND) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001988 pr_info("sending response for idx %u failed"
1989 " with status %d\n", index, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001990 srpt_handle_send_err_comp(ch, wc->wr_id);
1991 } else if (opcode != SRPT_RDMA_MID) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001992 pr_info("RDMA t %d for idx %u failed with"
1993 " status %d\n", opcode, index, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001994 srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
1995 }
1996 }
1997
1998 while (unlikely(opcode == SRPT_SEND
1999 && !list_empty(&ch->cmd_wait_list)
2000 && srpt_get_ch_state(ch) == CH_LIVE
2001 && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
2002 struct srpt_recv_ioctx *recv_ioctx;
2003
2004 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
2005 struct srpt_recv_ioctx,
2006 wait_list);
2007 list_del(&recv_ioctx->wait_list);
2008 srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
2009 }
2010}
2011
2012static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
2013{
2014 struct ib_wc *const wc = ch->wc;
2015 int i, n;
2016
2017 WARN_ON(cq != ch->cq);
2018
2019 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
2020 while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
2021 for (i = 0; i < n; i++) {
2022 if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
2023 srpt_process_rcv_completion(cq, ch, &wc[i]);
2024 else
2025 srpt_process_send_completion(cq, ch, &wc[i]);
2026 }
2027 }
2028}
2029
2030/**
2031 * srpt_completion() - IB completion queue callback function.
2032 *
2033 * Notes:
2034 * - It is guaranteed that a completion handler will never be invoked
2035 * concurrently on two different CPUs for the same completion queue. See also
2036 * Documentation/infiniband/core_locking.txt and the implementation of
2037 * handle_edge_irq() in kernel/irq/chip.c.
2038 * - When threaded IRQs are enabled, completion handlers are invoked in thread
2039 * context instead of interrupt context.
2040 */
2041static void srpt_completion(struct ib_cq *cq, void *ctx)
2042{
2043 struct srpt_rdma_ch *ch = ctx;
2044
2045 wake_up_interruptible(&ch->wait_queue);
2046}
2047
2048static int srpt_compl_thread(void *arg)
2049{
2050 struct srpt_rdma_ch *ch;
2051
2052 /* Hibernation / freezing of the SRPT kernel thread is not supported. */
2053 current->flags |= PF_NOFREEZE;
2054
2055 ch = arg;
2056 BUG_ON(!ch);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002057 pr_info("Session %s: kernel thread %s (PID %d) started\n",
2058 ch->sess_name, ch->thread->comm, current->pid);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002059 while (!kthread_should_stop()) {
2060 wait_event_interruptible(ch->wait_queue,
2061 (srpt_process_completion(ch->cq, ch),
2062 kthread_should_stop()));
2063 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002064 pr_info("Session %s: kernel thread %s (PID %d) stopped\n",
2065 ch->sess_name, ch->thread->comm, current->pid);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002066 return 0;
2067}
2068
2069/**
2070 * srpt_create_ch_ib() - Create receive and send completion queues.
2071 */
2072static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2073{
2074 struct ib_qp_init_attr *qp_init;
2075 struct srpt_port *sport = ch->sport;
2076 struct srpt_device *sdev = sport->sdev;
2077 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
Matan Barak8e372102015-06-11 16:35:21 +03002078 struct ib_cq_init_attr cq_attr = {};
Bart Van Asschea42d9852011-10-14 01:30:46 +00002079 int ret;
2080
2081 WARN_ON(ch->rq_size < 1);
2082
2083 ret = -ENOMEM;
2084 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2085 if (!qp_init)
2086 goto out;
2087
Bart Van Asscheab477c12014-10-19 18:05:33 +03002088retry:
Matan Barak8e372102015-06-11 16:35:21 +03002089 cq_attr.cqe = ch->rq_size + srp_sq_size;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002090 ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
Matan Barak8e372102015-06-11 16:35:21 +03002091 &cq_attr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002092 if (IS_ERR(ch->cq)) {
2093 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002094 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002095 ch->rq_size + srp_sq_size, ret);
2096 goto out;
2097 }
2098
2099 qp_init->qp_context = (void *)ch;
2100 qp_init->event_handler
2101 = (void(*)(struct ib_event *, void*))srpt_qp_event;
2102 qp_init->send_cq = ch->cq;
2103 qp_init->recv_cq = ch->cq;
2104 qp_init->srq = sdev->srq;
2105 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2106 qp_init->qp_type = IB_QPT_RC;
2107 qp_init->cap.max_send_wr = srp_sq_size;
2108 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2109
2110 ch->qp = ib_create_qp(sdev->pd, qp_init);
2111 if (IS_ERR(ch->qp)) {
2112 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03002113 if (ret == -ENOMEM) {
2114 srp_sq_size /= 2;
2115 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
2116 ib_destroy_cq(ch->cq);
2117 goto retry;
2118 }
2119 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002120 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002121 goto err_destroy_cq;
2122 }
2123
2124 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2125
2126 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2127 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2128 qp_init->cap.max_send_wr, ch->cm_id);
2129
2130 ret = srpt_init_ch_qp(ch, ch->qp);
2131 if (ret)
2132 goto err_destroy_qp;
2133
2134 init_waitqueue_head(&ch->wait_queue);
2135
2136 pr_debug("creating thread for session %s\n", ch->sess_name);
2137
2138 ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2139 if (IS_ERR(ch->thread)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002140 pr_err("failed to create kernel thread %ld\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002141 PTR_ERR(ch->thread));
2142 ch->thread = NULL;
2143 goto err_destroy_qp;
2144 }
2145
2146out:
2147 kfree(qp_init);
2148 return ret;
2149
2150err_destroy_qp:
2151 ib_destroy_qp(ch->qp);
2152err_destroy_cq:
2153 ib_destroy_cq(ch->cq);
2154 goto out;
2155}
2156
2157static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2158{
2159 if (ch->thread)
2160 kthread_stop(ch->thread);
2161
2162 ib_destroy_qp(ch->qp);
2163 ib_destroy_cq(ch->cq);
2164}
2165
2166/**
2167 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2168 *
2169 * Reset the QP and make sure all resources associated with the channel will
2170 * be deallocated at an appropriate time.
2171 *
2172 * Note: The caller must hold ch->sport->sdev->spinlock.
2173 */
2174static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2175{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002176 enum rdma_ch_state prev_state;
2177 unsigned long flags;
2178
Bart Van Asschea42d9852011-10-14 01:30:46 +00002179 spin_lock_irqsave(&ch->spinlock, flags);
2180 prev_state = ch->state;
2181 switch (prev_state) {
2182 case CH_CONNECTING:
2183 case CH_LIVE:
2184 ch->state = CH_DISCONNECTING;
2185 break;
2186 default:
2187 break;
2188 }
2189 spin_unlock_irqrestore(&ch->spinlock, flags);
2190
2191 switch (prev_state) {
2192 case CH_CONNECTING:
2193 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2194 NULL, 0);
2195 /* fall through */
2196 case CH_LIVE:
2197 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002198 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002199 break;
2200 case CH_DISCONNECTING:
2201 break;
2202 case CH_DRAINING:
2203 case CH_RELEASING:
2204 break;
2205 }
2206}
2207
2208/**
2209 * srpt_close_ch() - Close an RDMA channel.
2210 */
2211static void srpt_close_ch(struct srpt_rdma_ch *ch)
2212{
2213 struct srpt_device *sdev;
2214
2215 sdev = ch->sport->sdev;
2216 spin_lock_irq(&sdev->spinlock);
2217 __srpt_close_ch(ch);
2218 spin_unlock_irq(&sdev->spinlock);
2219}
2220
2221/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002222 * srpt_shutdown_session() - Whether or not a session may be shut down.
2223 */
2224static int srpt_shutdown_session(struct se_session *se_sess)
2225{
2226 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2227 unsigned long flags;
2228
2229 spin_lock_irqsave(&ch->spinlock, flags);
2230 if (ch->in_shutdown) {
2231 spin_unlock_irqrestore(&ch->spinlock, flags);
2232 return true;
2233 }
2234
2235 ch->in_shutdown = true;
2236 target_sess_cmd_list_set_waiting(se_sess);
2237 spin_unlock_irqrestore(&ch->spinlock, flags);
2238
2239 return true;
2240}
2241
2242/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002243 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2244 * @cm_id: Pointer to the CM ID of the channel to be drained.
2245 *
2246 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2247 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2248 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2249 * waits until all target sessions for the associated IB device have been
2250 * unregistered and target session registration involves a call to
2251 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2252 * this function has finished).
2253 */
2254static void srpt_drain_channel(struct ib_cm_id *cm_id)
2255{
2256 struct srpt_device *sdev;
2257 struct srpt_rdma_ch *ch;
2258 int ret;
2259 bool do_reset = false;
2260
2261 WARN_ON_ONCE(irqs_disabled());
2262
2263 sdev = cm_id->context;
2264 BUG_ON(!sdev);
2265 spin_lock_irq(&sdev->spinlock);
2266 list_for_each_entry(ch, &sdev->rch_list, list) {
2267 if (ch->cm_id == cm_id) {
2268 do_reset = srpt_test_and_set_ch_state(ch,
2269 CH_CONNECTING, CH_DRAINING) ||
2270 srpt_test_and_set_ch_state(ch,
2271 CH_LIVE, CH_DRAINING) ||
2272 srpt_test_and_set_ch_state(ch,
2273 CH_DISCONNECTING, CH_DRAINING);
2274 break;
2275 }
2276 }
2277 spin_unlock_irq(&sdev->spinlock);
2278
2279 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002280 if (ch->sess)
2281 srpt_shutdown_session(ch->sess);
2282
Bart Van Asschea42d9852011-10-14 01:30:46 +00002283 ret = srpt_ch_qp_err(ch);
2284 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002285 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002286 " failed: %d\n", ret);
2287 }
2288}
2289
2290/**
2291 * srpt_find_channel() - Look up an RDMA channel.
2292 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2293 *
2294 * Return NULL if no matching RDMA channel has been found.
2295 */
2296static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2297 struct ib_cm_id *cm_id)
2298{
2299 struct srpt_rdma_ch *ch;
2300 bool found;
2301
2302 WARN_ON_ONCE(irqs_disabled());
2303 BUG_ON(!sdev);
2304
2305 found = false;
2306 spin_lock_irq(&sdev->spinlock);
2307 list_for_each_entry(ch, &sdev->rch_list, list) {
2308 if (ch->cm_id == cm_id) {
2309 found = true;
2310 break;
2311 }
2312 }
2313 spin_unlock_irq(&sdev->spinlock);
2314
2315 return found ? ch : NULL;
2316}
2317
2318/**
2319 * srpt_release_channel() - Release channel resources.
2320 *
2321 * Schedules the actual release because:
2322 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2323 * trigger a deadlock.
2324 * - It is not safe to call TCM transport_* functions from interrupt context.
2325 */
2326static void srpt_release_channel(struct srpt_rdma_ch *ch)
2327{
2328 schedule_work(&ch->release_work);
2329}
2330
2331static void srpt_release_channel_work(struct work_struct *w)
2332{
2333 struct srpt_rdma_ch *ch;
2334 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002335 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002336
2337 ch = container_of(w, struct srpt_rdma_ch, release_work);
2338 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2339 ch->release_done);
2340
2341 sdev = ch->sport->sdev;
2342 BUG_ON(!sdev);
2343
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002344 se_sess = ch->sess;
2345 BUG_ON(!se_sess);
2346
Joern Engelbe646c2d2013-05-15 00:44:07 -07002347 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002348
2349 transport_deregister_session_configfs(se_sess);
2350 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002351 ch->sess = NULL;
2352
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002353 ib_destroy_cm_id(ch->cm_id);
2354
Bart Van Asschea42d9852011-10-14 01:30:46 +00002355 srpt_destroy_ch_ib(ch);
2356
2357 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2358 ch->sport->sdev, ch->rq_size,
2359 ch->rsp_size, DMA_TO_DEVICE);
2360
2361 spin_lock_irq(&sdev->spinlock);
2362 list_del(&ch->list);
2363 spin_unlock_irq(&sdev->spinlock);
2364
Bart Van Asschea42d9852011-10-14 01:30:46 +00002365 if (ch->release_done)
2366 complete(ch->release_done);
2367
2368 wake_up(&sdev->ch_releaseQ);
2369
2370 kfree(ch);
2371}
2372
Bart Van Asschea42d9852011-10-14 01:30:46 +00002373/**
2374 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2375 *
2376 * Ownership of the cm_id is transferred to the target session if this
2377 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2378 */
2379static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2380 struct ib_cm_req_event_param *param,
2381 void *private_data)
2382{
2383 struct srpt_device *sdev = cm_id->context;
2384 struct srpt_port *sport = &sdev->port[param->port - 1];
2385 struct srp_login_req *req;
2386 struct srp_login_rsp *rsp;
2387 struct srp_login_rej *rej;
2388 struct ib_cm_rep_param *rep_param;
2389 struct srpt_rdma_ch *ch, *tmp_ch;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002390 struct se_node_acl *se_acl;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002391 u32 it_iu_len;
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002392 int i, ret = 0;
2393 unsigned char *p;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002394
2395 WARN_ON_ONCE(irqs_disabled());
2396
2397 if (WARN_ON(!sdev || !private_data))
2398 return -EINVAL;
2399
2400 req = (struct srp_login_req *)private_data;
2401
2402 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2403
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002404 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2405 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2406 " (guid=0x%llx:0x%llx)\n",
2407 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2408 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2409 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2410 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2411 it_iu_len,
2412 param->port,
2413 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2414 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002415
2416 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2417 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2418 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2419
2420 if (!rsp || !rej || !rep_param) {
2421 ret = -ENOMEM;
2422 goto out;
2423 }
2424
2425 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302426 rej->reason = cpu_to_be32(
2427 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002428 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002429 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002430 " length (%d bytes) is out of range (%d .. %d)\n",
2431 it_iu_len, 64, srp_max_req_size);
2432 goto reject;
2433 }
2434
2435 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302436 rej->reason = cpu_to_be32(
2437 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002438 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002439 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002440 " has not yet been enabled\n");
2441 goto reject;
2442 }
2443
2444 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2445 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2446
2447 spin_lock_irq(&sdev->spinlock);
2448
2449 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2450 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2451 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2452 && param->port == ch->sport->port
2453 && param->listen_id == ch->sport->sdev->cm_id
2454 && ch->cm_id) {
2455 enum rdma_ch_state ch_state;
2456
2457 ch_state = srpt_get_ch_state(ch);
2458 if (ch_state != CH_CONNECTING
2459 && ch_state != CH_LIVE)
2460 continue;
2461
2462 /* found an existing channel */
2463 pr_debug("Found existing channel %s"
2464 " cm_id= %p state= %d\n",
2465 ch->sess_name, ch->cm_id, ch_state);
2466
2467 __srpt_close_ch(ch);
2468
2469 rsp->rsp_flags =
2470 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2471 }
2472 }
2473
2474 spin_unlock_irq(&sdev->spinlock);
2475
2476 } else
2477 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2478
2479 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2480 || *(__be64 *)(req->target_port_id + 8) !=
2481 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302482 rej->reason = cpu_to_be32(
2483 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002484 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002485 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002486 " has an invalid target port identifier.\n");
2487 goto reject;
2488 }
2489
2490 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2491 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302492 rej->reason = cpu_to_be32(
2493 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002494 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002495 ret = -ENOMEM;
2496 goto reject;
2497 }
2498
2499 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2500 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2501 memcpy(ch->t_port_id, req->target_port_id, 16);
2502 ch->sport = &sdev->port[param->port - 1];
2503 ch->cm_id = cm_id;
2504 /*
2505 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2506 * for the SRP protocol to the command queue size.
2507 */
2508 ch->rq_size = SRPT_RQ_SIZE;
2509 spin_lock_init(&ch->spinlock);
2510 ch->state = CH_CONNECTING;
2511 INIT_LIST_HEAD(&ch->cmd_wait_list);
2512 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2513
2514 ch->ioctx_ring = (struct srpt_send_ioctx **)
2515 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2516 sizeof(*ch->ioctx_ring[0]),
2517 ch->rsp_size, DMA_TO_DEVICE);
2518 if (!ch->ioctx_ring)
2519 goto free_ch;
2520
2521 INIT_LIST_HEAD(&ch->free_list);
2522 for (i = 0; i < ch->rq_size; i++) {
2523 ch->ioctx_ring[i]->ch = ch;
2524 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2525 }
2526
2527 ret = srpt_create_ch_ib(ch);
2528 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302529 rej->reason = cpu_to_be32(
2530 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002531 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002532 " a new RDMA channel failed.\n");
2533 goto free_ring;
2534 }
2535
2536 ret = srpt_ch_qp_rtr(ch, ch->qp);
2537 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302538 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002539 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002540 " RTR failed (error code = %d)\n", ret);
2541 goto destroy_ib;
2542 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002543
Bart Van Asschea42d9852011-10-14 01:30:46 +00002544 /*
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002545 * Use the initator port identifier as the session name, when
2546 * checking against se_node_acl->initiatorname[] this can be
2547 * with or without preceeding '0x'.
Bart Van Asschea42d9852011-10-14 01:30:46 +00002548 */
2549 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2550 be64_to_cpu(*(__be64 *)ch->i_port_id),
2551 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2552
2553 pr_debug("registering session %s\n", ch->sess_name);
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002554 p = &ch->sess_name[0];
Bart Van Asschea42d9852011-10-14 01:30:46 +00002555
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002556 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002557 if (IS_ERR(ch->sess)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302558 rej->reason = cpu_to_be32(
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002559 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002560 pr_debug("Failed to create session\n");
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002561 goto destroy_ib;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002562 }
Nicholas Bellingerf246c942016-01-07 22:19:21 -08002563
2564try_again:
2565 se_acl = core_tpg_get_initiator_node_acl(&sport->port_tpg_1, p);
2566 if (!se_acl) {
2567 pr_info("Rejected login because no ACL has been"
2568 " configured yet for initiator %s.\n", ch->sess_name);
2569 /*
2570 * XXX: Hack to retry of ch->i_port_id without leading '0x'
2571 */
2572 if (p == &ch->sess_name[0]) {
2573 p += 2;
2574 goto try_again;
2575 }
2576 rej->reason = cpu_to_be32(
2577 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2578 transport_free_session(ch->sess);
2579 goto destroy_ib;
2580 }
2581 ch->sess->se_node_acl = se_acl;
2582
2583 transport_register_session(&sport->port_tpg_1, se_acl, ch->sess, ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002584
2585 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2586 ch->sess_name, ch->cm_id);
2587
2588 /* create srp_login_response */
2589 rsp->opcode = SRP_LOGIN_RSP;
2590 rsp->tag = req->tag;
2591 rsp->max_it_iu_len = req->req_it_iu_len;
2592 rsp->max_ti_iu_len = req->req_it_iu_len;
2593 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302594 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2595 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002596 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2597 atomic_set(&ch->req_lim, ch->rq_size);
2598 atomic_set(&ch->req_lim_delta, 0);
2599
2600 /* create cm reply */
2601 rep_param->qp_num = ch->qp->qp_num;
2602 rep_param->private_data = (void *)rsp;
2603 rep_param->private_data_len = sizeof *rsp;
2604 rep_param->rnr_retry_count = 7;
2605 rep_param->flow_control = 1;
2606 rep_param->failover_accepted = 0;
2607 rep_param->srq = 1;
2608 rep_param->responder_resources = 4;
2609 rep_param->initiator_depth = 4;
2610
2611 ret = ib_send_cm_rep(cm_id, rep_param);
2612 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002613 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002614 " (error code = %d)\n", ret);
2615 goto release_channel;
2616 }
2617
2618 spin_lock_irq(&sdev->spinlock);
2619 list_add_tail(&ch->list, &sdev->rch_list);
2620 spin_unlock_irq(&sdev->spinlock);
2621
2622 goto out;
2623
2624release_channel:
2625 srpt_set_ch_state(ch, CH_RELEASING);
2626 transport_deregister_session_configfs(ch->sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002627 transport_deregister_session(ch->sess);
2628 ch->sess = NULL;
2629
2630destroy_ib:
2631 srpt_destroy_ch_ib(ch);
2632
2633free_ring:
2634 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2635 ch->sport->sdev, ch->rq_size,
2636 ch->rsp_size, DMA_TO_DEVICE);
2637free_ch:
2638 kfree(ch);
2639
2640reject:
2641 rej->opcode = SRP_LOGIN_REJ;
2642 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302643 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2644 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002645
2646 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2647 (void *)rej, sizeof *rej);
2648
2649out:
2650 kfree(rep_param);
2651 kfree(rsp);
2652 kfree(rej);
2653
2654 return ret;
2655}
2656
2657static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2658{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002659 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002660 srpt_drain_channel(cm_id);
2661}
2662
2663/**
2664 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2665 *
2666 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2667 * and that the recipient may begin transmitting (RTU = ready to use).
2668 */
2669static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2670{
2671 struct srpt_rdma_ch *ch;
2672 int ret;
2673
2674 ch = srpt_find_channel(cm_id->context, cm_id);
2675 BUG_ON(!ch);
2676
2677 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2678 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2679
2680 ret = srpt_ch_qp_rts(ch, ch->qp);
2681
2682 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2683 wait_list) {
2684 list_del(&ioctx->wait_list);
2685 srpt_handle_new_iu(ch, ioctx, NULL);
2686 }
2687 if (ret)
2688 srpt_close_ch(ch);
2689 }
2690}
2691
2692static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2693{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002694 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002695 srpt_drain_channel(cm_id);
2696}
2697
2698static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2699{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002700 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002701 srpt_drain_channel(cm_id);
2702}
2703
2704/**
2705 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2706 */
2707static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2708{
2709 struct srpt_rdma_ch *ch;
2710 unsigned long flags;
2711 bool send_drep = false;
2712
2713 ch = srpt_find_channel(cm_id->context, cm_id);
2714 BUG_ON(!ch);
2715
2716 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2717
2718 spin_lock_irqsave(&ch->spinlock, flags);
2719 switch (ch->state) {
2720 case CH_CONNECTING:
2721 case CH_LIVE:
2722 send_drep = true;
2723 ch->state = CH_DISCONNECTING;
2724 break;
2725 case CH_DISCONNECTING:
2726 case CH_DRAINING:
2727 case CH_RELEASING:
2728 WARN(true, "unexpected channel state %d\n", ch->state);
2729 break;
2730 }
2731 spin_unlock_irqrestore(&ch->spinlock, flags);
2732
2733 if (send_drep) {
2734 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002735 pr_err("Sending IB DREP failed.\n");
2736 pr_info("Received DREQ and sent DREP for session %s.\n",
2737 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002738 }
2739}
2740
2741/**
2742 * srpt_cm_drep_recv() - Process reception of a DREP message.
2743 */
2744static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2745{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002746 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002747 srpt_drain_channel(cm_id);
2748}
2749
2750/**
2751 * srpt_cm_handler() - IB connection manager callback function.
2752 *
2753 * A non-zero return value will cause the caller destroy the CM ID.
2754 *
2755 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2756 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2757 * a non-zero value in any other case will trigger a race with the
2758 * ib_destroy_cm_id() call in srpt_release_channel().
2759 */
2760static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2761{
2762 int ret;
2763
2764 ret = 0;
2765 switch (event->event) {
2766 case IB_CM_REQ_RECEIVED:
2767 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2768 event->private_data);
2769 break;
2770 case IB_CM_REJ_RECEIVED:
2771 srpt_cm_rej_recv(cm_id);
2772 break;
2773 case IB_CM_RTU_RECEIVED:
2774 case IB_CM_USER_ESTABLISHED:
2775 srpt_cm_rtu_recv(cm_id);
2776 break;
2777 case IB_CM_DREQ_RECEIVED:
2778 srpt_cm_dreq_recv(cm_id);
2779 break;
2780 case IB_CM_DREP_RECEIVED:
2781 srpt_cm_drep_recv(cm_id);
2782 break;
2783 case IB_CM_TIMEWAIT_EXIT:
2784 srpt_cm_timewait_exit(cm_id);
2785 break;
2786 case IB_CM_REP_ERROR:
2787 srpt_cm_rep_error(cm_id);
2788 break;
2789 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002790 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002791 break;
2792 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002793 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002794 break;
2795 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002796 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002797 break;
2798 }
2799
2800 return ret;
2801}
2802
2803/**
2804 * srpt_perform_rdmas() - Perform IB RDMA.
2805 *
2806 * Returns zero upon success or a negative number upon failure.
2807 */
2808static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2809 struct srpt_send_ioctx *ioctx)
2810{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002811 struct ib_rdma_wr wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002812 struct ib_send_wr *bad_wr;
2813 struct rdma_iu *riu;
2814 int i;
2815 int ret;
2816 int sq_wr_avail;
2817 enum dma_data_direction dir;
2818 const int n_rdma = ioctx->n_rdma;
2819
2820 dir = ioctx->cmd.data_direction;
2821 if (dir == DMA_TO_DEVICE) {
2822 /* write */
2823 ret = -ENOMEM;
2824 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2825 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002826 pr_warn("IB send queue full (needed %d)\n",
2827 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002828 goto out;
2829 }
2830 }
2831
2832 ioctx->rdma_aborted = false;
2833 ret = 0;
2834 riu = ioctx->rdma_ius;
2835 memset(&wr, 0, sizeof wr);
2836
2837 for (i = 0; i < n_rdma; ++i, ++riu) {
2838 if (dir == DMA_FROM_DEVICE) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002839 wr.wr.opcode = IB_WR_RDMA_WRITE;
2840 wr.wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
Bart Van Asschea42d9852011-10-14 01:30:46 +00002841 SRPT_RDMA_WRITE_LAST :
2842 SRPT_RDMA_MID,
2843 ioctx->ioctx.index);
2844 } else {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002845 wr.wr.opcode = IB_WR_RDMA_READ;
2846 wr.wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
Bart Van Asschea42d9852011-10-14 01:30:46 +00002847 SRPT_RDMA_READ_LAST :
2848 SRPT_RDMA_MID,
2849 ioctx->ioctx.index);
2850 }
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002851 wr.wr.next = NULL;
2852 wr.remote_addr = riu->raddr;
2853 wr.rkey = riu->rkey;
2854 wr.wr.num_sge = riu->sge_cnt;
2855 wr.wr.sg_list = riu->sge;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002856
2857 /* only get completion event for the last rdma write */
2858 if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002859 wr.wr.send_flags = IB_SEND_SIGNALED;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002860
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002861 ret = ib_post_send(ch->qp, &wr.wr, &bad_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002862 if (ret)
2863 break;
2864 }
2865
2866 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002867 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002868 __func__, __LINE__, ret, i, n_rdma);
2869 if (ret && i > 0) {
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002870 wr.wr.num_sge = 0;
2871 wr.wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2872 wr.wr.send_flags = IB_SEND_SIGNALED;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002873 while (ch->state == CH_LIVE &&
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002874 ib_post_send(ch->qp, &wr.wr, &bad_wr) != 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002875 pr_info("Trying to abort failed RDMA transfer [%d]\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002876 ioctx->ioctx.index);
2877 msleep(1000);
2878 }
2879 while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002880 pr_info("Waiting until RDMA abort finished [%d]\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002881 ioctx->ioctx.index);
2882 msleep(1000);
2883 }
2884 }
2885out:
2886 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2887 atomic_add(n_rdma, &ch->sq_wr_avail);
2888 return ret;
2889}
2890
2891/**
2892 * srpt_xfer_data() - Start data transfer from initiator to target.
2893 */
2894static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2895 struct srpt_send_ioctx *ioctx)
2896{
2897 int ret;
2898
2899 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2900 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002901 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002902 goto out;
2903 }
2904
2905 ret = srpt_perform_rdmas(ch, ioctx);
2906 if (ret) {
2907 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002908 pr_info("%s[%d] queue full -- ret=%d\n",
2909 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002910 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002911 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002912 __func__, __LINE__, ret);
2913 goto out_unmap;
2914 }
2915
2916out:
2917 return ret;
2918out_unmap:
2919 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2920 goto out;
2921}
2922
2923static int srpt_write_pending_status(struct se_cmd *se_cmd)
2924{
2925 struct srpt_send_ioctx *ioctx;
2926
2927 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2928 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2929}
2930
2931/*
2932 * srpt_write_pending() - Start data transfer from initiator to target (write).
2933 */
2934static int srpt_write_pending(struct se_cmd *se_cmd)
2935{
2936 struct srpt_rdma_ch *ch;
2937 struct srpt_send_ioctx *ioctx;
2938 enum srpt_command_state new_state;
2939 enum rdma_ch_state ch_state;
2940 int ret;
2941
2942 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2943
2944 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2945 WARN_ON(new_state == SRPT_STATE_DONE);
2946
2947 ch = ioctx->ch;
2948 BUG_ON(!ch);
2949
2950 ch_state = srpt_get_ch_state(ch);
2951 switch (ch_state) {
2952 case CH_CONNECTING:
2953 WARN(true, "unexpected channel state %d\n", ch_state);
2954 ret = -EINVAL;
2955 goto out;
2956 case CH_LIVE:
2957 break;
2958 case CH_DISCONNECTING:
2959 case CH_DRAINING:
2960 case CH_RELEASING:
2961 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002962 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002963 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2964 ret = -EINVAL;
2965 goto out;
2966 }
2967 ret = srpt_xfer_data(ch, ioctx);
2968
2969out:
2970 return ret;
2971}
2972
2973static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2974{
2975 switch (tcm_mgmt_status) {
2976 case TMR_FUNCTION_COMPLETE:
2977 return SRP_TSK_MGMT_SUCCESS;
2978 case TMR_FUNCTION_REJECTED:
2979 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2980 }
2981 return SRP_TSK_MGMT_FAILED;
2982}
2983
2984/**
2985 * srpt_queue_response() - Transmits the response to a SCSI command.
2986 *
2987 * Callback function called by the TCM core. Must not block since it can be
2988 * invoked on the context of the IB completion handler.
2989 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002990static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002991{
2992 struct srpt_rdma_ch *ch;
2993 struct srpt_send_ioctx *ioctx;
2994 enum srpt_command_state state;
2995 unsigned long flags;
2996 int ret;
2997 enum dma_data_direction dir;
2998 int resp_len;
2999 u8 srp_tm_status;
3000
Bart Van Asschea42d9852011-10-14 01:30:46 +00003001 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3002 ch = ioctx->ch;
3003 BUG_ON(!ch);
3004
3005 spin_lock_irqsave(&ioctx->spinlock, flags);
3006 state = ioctx->state;
3007 switch (state) {
3008 case SRPT_STATE_NEW:
3009 case SRPT_STATE_DATA_IN:
3010 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
3011 break;
3012 case SRPT_STATE_MGMT:
3013 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
3014 break;
3015 default:
3016 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
3017 ch, ioctx->ioctx.index, ioctx->state);
3018 break;
3019 }
3020 spin_unlock_irqrestore(&ioctx->spinlock, flags);
3021
3022 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
3023 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
3024 atomic_inc(&ch->req_lim_delta);
3025 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04003026 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003027 }
3028
3029 dir = ioctx->cmd.data_direction;
3030
3031 /* For read commands, transfer the data to the initiator. */
3032 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
3033 !ioctx->queue_status_only) {
3034 ret = srpt_xfer_data(ch, ioctx);
3035 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003036 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02003037 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04003038 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003039 }
3040 }
3041
3042 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02003043 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003044 cmd->scsi_status);
3045 else {
3046 srp_tm_status
3047 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3048 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02003049 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003050 }
3051 ret = srpt_post_send(ch, ioctx, resp_len);
3052 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003053 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02003054 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003055 srpt_unmap_sg_to_ib_sge(ch, ioctx);
3056 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02003057 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003058 }
Joern Engelb79fafa2013-07-03 11:22:17 -04003059}
Bart Van Asschea42d9852011-10-14 01:30:46 +00003060
Joern Engelb79fafa2013-07-03 11:22:17 -04003061static int srpt_queue_data_in(struct se_cmd *cmd)
3062{
3063 srpt_queue_response(cmd);
3064 return 0;
3065}
3066
3067static void srpt_queue_tm_rsp(struct se_cmd *cmd)
3068{
3069 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003070}
3071
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003072static void srpt_aborted_task(struct se_cmd *cmd)
3073{
3074 struct srpt_send_ioctx *ioctx = container_of(cmd,
3075 struct srpt_send_ioctx, cmd);
3076
3077 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
3078}
3079
Bart Van Asschea42d9852011-10-14 01:30:46 +00003080static int srpt_queue_status(struct se_cmd *cmd)
3081{
3082 struct srpt_send_ioctx *ioctx;
3083
3084 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3085 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3086 if (cmd->se_cmd_flags &
3087 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3088 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3089 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04003090 srpt_queue_response(cmd);
3091 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003092}
3093
3094static void srpt_refresh_port_work(struct work_struct *work)
3095{
3096 struct srpt_port *sport = container_of(work, struct srpt_port, work);
3097
3098 srpt_refresh_port(sport);
3099}
3100
3101static int srpt_ch_list_empty(struct srpt_device *sdev)
3102{
3103 int res;
3104
3105 spin_lock_irq(&sdev->spinlock);
3106 res = list_empty(&sdev->rch_list);
3107 spin_unlock_irq(&sdev->spinlock);
3108
3109 return res;
3110}
3111
3112/**
3113 * srpt_release_sdev() - Free the channel resources associated with a target.
3114 */
3115static int srpt_release_sdev(struct srpt_device *sdev)
3116{
3117 struct srpt_rdma_ch *ch, *tmp_ch;
3118 int res;
3119
3120 WARN_ON_ONCE(irqs_disabled());
3121
3122 BUG_ON(!sdev);
3123
3124 spin_lock_irq(&sdev->spinlock);
3125 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3126 __srpt_close_ch(ch);
3127 spin_unlock_irq(&sdev->spinlock);
3128
3129 res = wait_event_interruptible(sdev->ch_releaseQ,
3130 srpt_ch_list_empty(sdev));
3131 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003132 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003133
3134 return 0;
3135}
3136
3137static struct srpt_port *__srpt_lookup_port(const char *name)
3138{
3139 struct ib_device *dev;
3140 struct srpt_device *sdev;
3141 struct srpt_port *sport;
3142 int i;
3143
3144 list_for_each_entry(sdev, &srpt_dev_list, list) {
3145 dev = sdev->device;
3146 if (!dev)
3147 continue;
3148
3149 for (i = 0; i < dev->phys_port_cnt; i++) {
3150 sport = &sdev->port[i];
3151
3152 if (!strcmp(sport->port_guid, name))
3153 return sport;
3154 }
3155 }
3156
3157 return NULL;
3158}
3159
3160static struct srpt_port *srpt_lookup_port(const char *name)
3161{
3162 struct srpt_port *sport;
3163
3164 spin_lock(&srpt_dev_lock);
3165 sport = __srpt_lookup_port(name);
3166 spin_unlock(&srpt_dev_lock);
3167
3168 return sport;
3169}
3170
3171/**
3172 * srpt_add_one() - Infiniband device addition callback function.
3173 */
3174static void srpt_add_one(struct ib_device *device)
3175{
3176 struct srpt_device *sdev;
3177 struct srpt_port *sport;
3178 struct ib_srq_init_attr srq_attr;
3179 int i;
3180
3181 pr_debug("device = %p, device->dma_ops = %p\n", device,
3182 device->dma_ops);
3183
3184 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3185 if (!sdev)
3186 goto err;
3187
3188 sdev->device = device;
3189 INIT_LIST_HEAD(&sdev->rch_list);
3190 init_waitqueue_head(&sdev->ch_releaseQ);
3191 spin_lock_init(&sdev->spinlock);
3192
3193 if (ib_query_device(device, &sdev->dev_attr))
3194 goto free_dev;
3195
3196 sdev->pd = ib_alloc_pd(device);
3197 if (IS_ERR(sdev->pd))
3198 goto free_dev;
3199
Bart Van Asschea42d9852011-10-14 01:30:46 +00003200 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3201
3202 srq_attr.event_handler = srpt_srq_event;
3203 srq_attr.srq_context = (void *)sdev;
3204 srq_attr.attr.max_wr = sdev->srq_size;
3205 srq_attr.attr.max_sge = 1;
3206 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07003207 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003208
3209 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3210 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06003211 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003212
3213 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3214 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3215 device->name);
3216
3217 if (!srpt_service_guid)
3218 srpt_service_guid = be64_to_cpu(device->node_guid);
3219
3220 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3221 if (IS_ERR(sdev->cm_id))
3222 goto err_srq;
3223
3224 /* print out target login information */
3225 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3226 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3227 srpt_service_guid, srpt_service_guid);
3228
3229 /*
3230 * We do not have a consistent service_id (ie. also id_ext of target_id)
3231 * to identify this target. We currently use the guid of the first HCA
3232 * in the system as service_id; therefore, the target_id will change
3233 * if this HCA is gone bad and replaced by different HCA
3234 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03003235 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00003236 goto err_cm;
3237
3238 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3239 srpt_event_handler);
3240 if (ib_register_event_handler(&sdev->event_handler))
3241 goto err_cm;
3242
3243 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3244 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3245 sizeof(*sdev->ioctx_ring[0]),
3246 srp_max_req_size, DMA_FROM_DEVICE);
3247 if (!sdev->ioctx_ring)
3248 goto err_event;
3249
3250 for (i = 0; i < sdev->srq_size; ++i)
3251 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3252
Roland Dreierf2250662012-02-02 12:55:58 -08003253 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00003254
3255 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3256 sport = &sdev->port[i - 1];
3257 sport->sdev = sdev;
3258 sport->port = i;
3259 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3260 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3261 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3262 INIT_WORK(&sport->work, srpt_refresh_port_work);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003263
3264 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003265 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003266 srpt_sdev_name(sdev), i);
3267 goto err_ring;
3268 }
3269 snprintf(sport->port_guid, sizeof(sport->port_guid),
3270 "0x%016llx%016llx",
3271 be64_to_cpu(sport->gid.global.subnet_prefix),
3272 be64_to_cpu(sport->gid.global.interface_id));
3273 }
3274
3275 spin_lock(&srpt_dev_lock);
3276 list_add_tail(&sdev->list, &srpt_dev_list);
3277 spin_unlock(&srpt_dev_lock);
3278
3279out:
3280 ib_set_client_data(device, &srpt_client, sdev);
3281 pr_debug("added %s.\n", device->name);
3282 return;
3283
3284err_ring:
3285 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3286 sdev->srq_size, srp_max_req_size,
3287 DMA_FROM_DEVICE);
3288err_event:
3289 ib_unregister_event_handler(&sdev->event_handler);
3290err_cm:
3291 ib_destroy_cm_id(sdev->cm_id);
3292err_srq:
3293 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003294err_pd:
3295 ib_dealloc_pd(sdev->pd);
3296free_dev:
3297 kfree(sdev);
3298err:
3299 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003300 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003301 goto out;
3302}
3303
3304/**
3305 * srpt_remove_one() - InfiniBand device removal callback function.
3306 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03003307static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003308{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003309 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003310 int i;
3311
Bart Van Asschea42d9852011-10-14 01:30:46 +00003312 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003313 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003314 return;
3315 }
3316
3317 srpt_unregister_mad_agent(sdev);
3318
3319 ib_unregister_event_handler(&sdev->event_handler);
3320
3321 /* Cancel any work queued by the just unregistered IB event handler. */
3322 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3323 cancel_work_sync(&sdev->port[i].work);
3324
3325 ib_destroy_cm_id(sdev->cm_id);
3326
3327 /*
3328 * Unregistering a target must happen after destroying sdev->cm_id
3329 * such that no new SRP_LOGIN_REQ information units can arrive while
3330 * destroying the target.
3331 */
3332 spin_lock(&srpt_dev_lock);
3333 list_del(&sdev->list);
3334 spin_unlock(&srpt_dev_lock);
3335 srpt_release_sdev(sdev);
3336
3337 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003338 ib_dealloc_pd(sdev->pd);
3339
3340 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3341 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3342 sdev->ioctx_ring = NULL;
3343 kfree(sdev);
3344}
3345
3346static struct ib_client srpt_client = {
3347 .name = DRV_NAME,
3348 .add = srpt_add_one,
3349 .remove = srpt_remove_one
3350};
3351
3352static int srpt_check_true(struct se_portal_group *se_tpg)
3353{
3354 return 1;
3355}
3356
3357static int srpt_check_false(struct se_portal_group *se_tpg)
3358{
3359 return 0;
3360}
3361
3362static char *srpt_get_fabric_name(void)
3363{
3364 return "srpt";
3365}
3366
Bart Van Asschea42d9852011-10-14 01:30:46 +00003367static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3368{
3369 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3370
3371 return sport->port_guid;
3372}
3373
3374static u16 srpt_get_tag(struct se_portal_group *tpg)
3375{
3376 return 1;
3377}
3378
Bart Van Asschea42d9852011-10-14 01:30:46 +00003379static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3380{
3381 return 1;
3382}
3383
3384static void srpt_release_cmd(struct se_cmd *se_cmd)
3385{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003386 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3387 struct srpt_send_ioctx, cmd);
3388 struct srpt_rdma_ch *ch = ioctx->ch;
3389 unsigned long flags;
3390
3391 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3392 WARN_ON(ioctx->mapped_sg_count != 0);
3393
3394 if (ioctx->n_rbuf > 1) {
3395 kfree(ioctx->rbufs);
3396 ioctx->rbufs = NULL;
3397 ioctx->n_rbuf = 0;
3398 }
3399
3400 spin_lock_irqsave(&ch->spinlock, flags);
3401 list_add(&ioctx->free_list, &ch->free_list);
3402 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003403}
3404
3405/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003406 * srpt_close_session() - Forcibly close a session.
3407 *
3408 * Callback function invoked by the TCM core to clean up sessions associated
3409 * with a node ACL when the user invokes
3410 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3411 */
3412static void srpt_close_session(struct se_session *se_sess)
3413{
3414 DECLARE_COMPLETION_ONSTACK(release_done);
3415 struct srpt_rdma_ch *ch;
3416 struct srpt_device *sdev;
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003417 unsigned long res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003418
3419 ch = se_sess->fabric_sess_ptr;
3420 WARN_ON(ch->sess != se_sess);
3421
3422 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3423
3424 sdev = ch->sport->sdev;
3425 spin_lock_irq(&sdev->spinlock);
3426 BUG_ON(ch->release_done);
3427 ch->release_done = &release_done;
3428 __srpt_close_ch(ch);
3429 spin_unlock_irq(&sdev->spinlock);
3430
3431 res = wait_for_completion_timeout(&release_done, 60 * HZ);
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003432 WARN_ON(res == 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003433}
3434
3435/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003436 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3437 *
3438 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3439 * This object represents an arbitrary integer used to uniquely identify a
3440 * particular attached remote initiator port to a particular SCSI target port
3441 * within a particular SCSI target device within a particular SCSI instance.
3442 */
3443static u32 srpt_sess_get_index(struct se_session *se_sess)
3444{
3445 return 0;
3446}
3447
3448static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3449{
3450}
3451
Bart Van Asschea42d9852011-10-14 01:30:46 +00003452/* Note: only used from inside debug printk's by the TCM core. */
3453static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3454{
3455 struct srpt_send_ioctx *ioctx;
3456
3457 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3458 return srpt_get_cmd_state(ioctx);
3459}
3460
Bart Van Asschea42d9852011-10-14 01:30:46 +00003461/**
3462 * srpt_parse_i_port_id() - Parse an initiator port ID.
3463 * @name: ASCII representation of a 128-bit initiator port ID.
3464 * @i_port_id: Binary 128-bit port ID.
3465 */
3466static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3467{
3468 const char *p;
3469 unsigned len, count, leading_zero_bytes;
3470 int ret, rc;
3471
3472 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003473 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003474 p += 2;
3475 ret = -EINVAL;
3476 len = strlen(p);
3477 if (len % 2)
3478 goto out;
3479 count = min(len / 2, 16U);
3480 leading_zero_bytes = 16 - count;
3481 memset(i_port_id, 0, leading_zero_bytes);
3482 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3483 if (rc < 0)
3484 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3485 ret = 0;
3486out:
3487 return ret;
3488}
3489
3490/*
3491 * configfs callback function invoked for
3492 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3493 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003494static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003495{
Bart Van Asschea42d9852011-10-14 01:30:46 +00003496 u8 i_port_id[16];
3497
3498 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003499 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003500 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003501 }
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003502 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003503}
3504
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003505static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3506 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003507{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003508 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003509 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3510
3511 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3512}
3513
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003514static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3515 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003516{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003517 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003518 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3519 unsigned long val;
3520 int ret;
3521
Jingoo Han9d8abf42014-02-05 11:22:05 +09003522 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003523 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003524 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003525 return -EINVAL;
3526 }
3527 if (val > MAX_SRPT_RDMA_SIZE) {
3528 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3529 MAX_SRPT_RDMA_SIZE);
3530 return -EINVAL;
3531 }
3532 if (val < DEFAULT_MAX_RDMA_SIZE) {
3533 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3534 val, DEFAULT_MAX_RDMA_SIZE);
3535 return -EINVAL;
3536 }
3537 sport->port_attrib.srp_max_rdma_size = val;
3538
3539 return count;
3540}
3541
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003542static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3543 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003544{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003545 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003546 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3547
3548 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3549}
3550
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003551static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3552 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003553{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003554 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003555 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3556 unsigned long val;
3557 int ret;
3558
Jingoo Han9d8abf42014-02-05 11:22:05 +09003559 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003560 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003561 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003562 return -EINVAL;
3563 }
3564 if (val > MAX_SRPT_RSP_SIZE) {
3565 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3566 MAX_SRPT_RSP_SIZE);
3567 return -EINVAL;
3568 }
3569 if (val < MIN_MAX_RSP_SIZE) {
3570 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3571 MIN_MAX_RSP_SIZE);
3572 return -EINVAL;
3573 }
3574 sport->port_attrib.srp_max_rsp_size = val;
3575
3576 return count;
3577}
3578
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003579static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3580 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003581{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003582 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003583 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3584
3585 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3586}
3587
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003588static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3589 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003590{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003591 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003592 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3593 unsigned long val;
3594 int ret;
3595
Jingoo Han9d8abf42014-02-05 11:22:05 +09003596 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003597 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003598 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003599 return -EINVAL;
3600 }
3601 if (val > MAX_SRPT_SRQ_SIZE) {
3602 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3603 MAX_SRPT_SRQ_SIZE);
3604 return -EINVAL;
3605 }
3606 if (val < MIN_SRPT_SRQ_SIZE) {
3607 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3608 MIN_SRPT_SRQ_SIZE);
3609 return -EINVAL;
3610 }
3611 sport->port_attrib.srp_sq_size = val;
3612
3613 return count;
3614}
3615
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003616CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3617CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3618CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003619
3620static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003621 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3622 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3623 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003624 NULL,
3625};
3626
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003627static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003628{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003629 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003630 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3631
3632 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3633}
3634
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003635static ssize_t srpt_tpg_enable_store(struct config_item *item,
3636 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003637{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003638 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003639 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3640 unsigned long tmp;
3641 int ret;
3642
Jingoo Han9d8abf42014-02-05 11:22:05 +09003643 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003644 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003645 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003646 return -EINVAL;
3647 }
3648
3649 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003650 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003651 return -EINVAL;
3652 }
3653 if (tmp == 1)
3654 sport->enabled = true;
3655 else
3656 sport->enabled = false;
3657
3658 return count;
3659}
3660
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003661CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003662
3663static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003664 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003665 NULL,
3666};
3667
3668/**
3669 * configfs callback invoked for
3670 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3671 */
3672static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3673 struct config_group *group,
3674 const char *name)
3675{
3676 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3677 int res;
3678
3679 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003680 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003681 if (res)
3682 return ERR_PTR(res);
3683
3684 return &sport->port_tpg_1;
3685}
3686
3687/**
3688 * configfs callback invoked for
3689 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3690 */
3691static void srpt_drop_tpg(struct se_portal_group *tpg)
3692{
3693 struct srpt_port *sport = container_of(tpg,
3694 struct srpt_port, port_tpg_1);
3695
3696 sport->enabled = false;
3697 core_tpg_deregister(&sport->port_tpg_1);
3698}
3699
3700/**
3701 * configfs callback invoked for
3702 * mkdir /sys/kernel/config/target/$driver/$port
3703 */
3704static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3705 struct config_group *group,
3706 const char *name)
3707{
3708 struct srpt_port *sport;
3709 int ret;
3710
3711 sport = srpt_lookup_port(name);
3712 pr_debug("make_tport(%s)\n", name);
3713 ret = -EINVAL;
3714 if (!sport)
3715 goto err;
3716
3717 return &sport->port_wwn;
3718
3719err:
3720 return ERR_PTR(ret);
3721}
3722
3723/**
3724 * configfs callback invoked for
3725 * rmdir /sys/kernel/config/target/$driver/$port
3726 */
3727static void srpt_drop_tport(struct se_wwn *wwn)
3728{
3729 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3730
3731 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3732}
3733
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003734static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003735{
3736 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3737}
3738
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003739CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003740
3741static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003742 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003743 NULL,
3744};
3745
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003746static const struct target_core_fabric_ops srpt_template = {
3747 .module = THIS_MODULE,
3748 .name = "srpt",
Christoph Hellwig144bc4c2015-04-13 19:51:16 +02003749 .node_acl_size = sizeof(struct srpt_node_acl),
Bart Van Asschea42d9852011-10-14 01:30:46 +00003750 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003751 .tpg_get_wwn = srpt_get_fabric_wwn,
3752 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003753 .tpg_check_demo_mode = srpt_check_false,
3754 .tpg_check_demo_mode_cache = srpt_check_true,
3755 .tpg_check_demo_mode_write_protect = srpt_check_true,
3756 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003757 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3758 .release_cmd = srpt_release_cmd,
3759 .check_stop_free = srpt_check_stop_free,
3760 .shutdown_session = srpt_shutdown_session,
3761 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003762 .sess_get_index = srpt_sess_get_index,
3763 .sess_get_initiator_sid = NULL,
3764 .write_pending = srpt_write_pending,
3765 .write_pending_status = srpt_write_pending_status,
3766 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003767 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003768 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003769 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003770 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003771 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003772 /*
3773 * Setup function pointers for generic logic in
3774 * target_core_fabric_configfs.c
3775 */
3776 .fabric_make_wwn = srpt_make_tport,
3777 .fabric_drop_wwn = srpt_drop_tport,
3778 .fabric_make_tpg = srpt_make_tpg,
3779 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003780 .fabric_init_nodeacl = srpt_init_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003781
3782 .tfc_wwn_attrs = srpt_wwn_attrs,
3783 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3784 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003785};
3786
3787/**
3788 * srpt_init_module() - Kernel module initialization.
3789 *
3790 * Note: Since ib_register_client() registers callback functions, and since at
3791 * least one of these callback functions (srpt_add_one()) calls target core
3792 * functions, this driver must be registered with the target core before
3793 * ib_register_client() is called.
3794 */
3795static int __init srpt_init_module(void)
3796{
3797 int ret;
3798
3799 ret = -EINVAL;
3800 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003801 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003802 " srp_max_req_size -- must be at least %d.\n",
3803 srp_max_req_size, MIN_MAX_REQ_SIZE);
3804 goto out;
3805 }
3806
3807 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3808 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003809 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003810 " srpt_srq_size -- must be in the range [%d..%d].\n",
3811 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3812 goto out;
3813 }
3814
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003815 ret = target_register_template(&srpt_template);
3816 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003817 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003818
3819 ret = ib_register_client(&srpt_client);
3820 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003821 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003822 goto out_unregister_target;
3823 }
3824
3825 return 0;
3826
3827out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003828 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003829out:
3830 return ret;
3831}
3832
3833static void __exit srpt_cleanup_module(void)
3834{
3835 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003836 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003837}
3838
3839module_init(srpt_init_module);
3840module_exit(srpt_cleanup_module);