blob: 8068affe25b520290882b0d77d4c427b50e2d25d [file] [log] [blame]
Bart Van Asschea42d9852011-10-14 01:30:46 +00001/*
2 * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
3 * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 */
34
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/err.h>
39#include <linux/ctype.h>
40#include <linux/kthread.h>
41#include <linux/string.h>
42#include <linux/delay.h>
43#include <linux/atomic.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020044#include <scsi/scsi_proto.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000045#include <scsi/scsi_tcq.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000046#include <target/target_core_base.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000047#include <target/target_core_fabric.h>
Bart Van Asschea42d9852011-10-14 01:30:46 +000048#include "ib_srpt.h"
49
50/* Name of this kernel module. */
51#define DRV_NAME "ib_srpt"
52#define DRV_VERSION "2.0.0"
53#define DRV_RELDATE "2011-02-14"
54
55#define SRPT_ID_STRING "Linux SRP target"
56
57#undef pr_fmt
58#define pr_fmt(fmt) DRV_NAME " " fmt
59
60MODULE_AUTHOR("Vu Pham and Bart Van Assche");
61MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63MODULE_LICENSE("Dual BSD/GPL");
64
65/*
66 * Global Variables
67 */
68
69static u64 srpt_service_guid;
Roland Dreier486d8b92012-02-02 12:55:58 -080070static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
71static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
Bart Van Asschea42d9852011-10-14 01:30:46 +000072
73static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
74module_param(srp_max_req_size, int, 0444);
75MODULE_PARM_DESC(srp_max_req_size,
76 "Maximum size of SRP request messages in bytes.");
77
78static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
79module_param(srpt_srq_size, int, 0444);
80MODULE_PARM_DESC(srpt_srq_size,
81 "Shared receive queue (SRQ) size.");
82
83static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
84{
85 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
86}
87module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
88 0444);
89MODULE_PARM_DESC(srpt_service_guid,
90 "Using this value for ioc_guid, id_ext, and cm_listen_id"
91 " instead of using the node_guid of the first HCA.");
92
93static struct ib_client srpt_client;
Bart Van Asschea42d9852011-10-14 01:30:46 +000094static void srpt_release_channel(struct srpt_rdma_ch *ch);
95static int srpt_queue_status(struct se_cmd *cmd);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +020096static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
97static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
Bart Van Asschea42d9852011-10-14 01:30:46 +000098
99/**
100 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
101 */
102static inline
103enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
104{
105 switch (dir) {
106 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
107 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
108 default: return dir;
109 }
110}
111
112/**
113 * srpt_sdev_name() - Return the name associated with the HCA.
114 *
115 * Examples are ib0, ib1, ...
116 */
117static inline const char *srpt_sdev_name(struct srpt_device *sdev)
118{
119 return sdev->device->name;
120}
121
122static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
123{
124 unsigned long flags;
125 enum rdma_ch_state state;
126
127 spin_lock_irqsave(&ch->spinlock, flags);
128 state = ch->state;
129 spin_unlock_irqrestore(&ch->spinlock, flags);
130 return state;
131}
132
133static enum rdma_ch_state
134srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
135{
136 unsigned long flags;
137 enum rdma_ch_state prev;
138
139 spin_lock_irqsave(&ch->spinlock, flags);
140 prev = ch->state;
141 ch->state = new_state;
142 spin_unlock_irqrestore(&ch->spinlock, flags);
143 return prev;
144}
145
146/**
147 * srpt_test_and_set_ch_state() - Test and set the channel state.
148 *
149 * Returns true if and only if the channel state has been set to the new state.
150 */
151static bool
152srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
153 enum rdma_ch_state new)
154{
155 unsigned long flags;
156 enum rdma_ch_state prev;
157
158 spin_lock_irqsave(&ch->spinlock, flags);
159 prev = ch->state;
160 if (prev == old)
161 ch->state = new;
162 spin_unlock_irqrestore(&ch->spinlock, flags);
163 return prev == old;
164}
165
166/**
167 * srpt_event_handler() - Asynchronous IB event callback function.
168 *
169 * Callback function called by the InfiniBand core when an asynchronous IB
170 * event occurs. This callback may occur in interrupt context. See also
171 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
172 * Architecture Specification.
173 */
174static void srpt_event_handler(struct ib_event_handler *handler,
175 struct ib_event *event)
176{
177 struct srpt_device *sdev;
178 struct srpt_port *sport;
179
180 sdev = ib_get_client_data(event->device, &srpt_client);
181 if (!sdev || sdev->device != event->device)
182 return;
183
184 pr_debug("ASYNC event= %d on device= %s\n", event->event,
185 srpt_sdev_name(sdev));
186
187 switch (event->event) {
188 case IB_EVENT_PORT_ERR:
189 if (event->element.port_num <= sdev->device->phys_port_cnt) {
190 sport = &sdev->port[event->element.port_num - 1];
191 sport->lid = 0;
192 sport->sm_lid = 0;
193 }
194 break;
195 case IB_EVENT_PORT_ACTIVE:
196 case IB_EVENT_LID_CHANGE:
197 case IB_EVENT_PKEY_CHANGE:
198 case IB_EVENT_SM_CHANGE:
199 case IB_EVENT_CLIENT_REREGISTER:
Doug Ledford2aa1cf62014-08-12 19:20:10 -0400200 case IB_EVENT_GID_CHANGE:
Bart Van Asschea42d9852011-10-14 01:30:46 +0000201 /* Refresh port data asynchronously. */
202 if (event->element.port_num <= sdev->device->phys_port_cnt) {
203 sport = &sdev->port[event->element.port_num - 1];
204 if (!sport->lid && !sport->sm_lid)
205 schedule_work(&sport->work);
206 }
207 break;
208 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400209 pr_err("received unrecognized IB event %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +0000210 event->event);
211 break;
212 }
213}
214
215/**
216 * srpt_srq_event() - SRQ event callback function.
217 */
218static void srpt_srq_event(struct ib_event *event, void *ctx)
219{
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400220 pr_info("SRQ event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000221}
222
223/**
224 * srpt_qp_event() - QP event callback function.
225 */
226static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
227{
228 pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
229 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
230
231 switch (event->event) {
232 case IB_EVENT_COMM_EST:
233 ib_cm_notify(ch->cm_id, event->event);
234 break;
235 case IB_EVENT_QP_LAST_WQE_REACHED:
236 if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
237 CH_RELEASING))
238 srpt_release_channel(ch);
239 else
240 pr_debug("%s: state %d - ignored LAST_WQE.\n",
241 ch->sess_name, srpt_get_ch_state(ch));
242 break;
243 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400244 pr_err("received unrecognized IB QP event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000245 break;
246 }
247}
248
249/**
250 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
251 *
252 * @slot: one-based slot number.
253 * @value: four-bit value.
254 *
255 * Copies the lowest four bits of value in element slot of the array of four
256 * bit elements called c_list (controller list). The index slot is one-based.
257 */
258static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
259{
260 u16 id;
261 u8 tmp;
262
263 id = (slot - 1) / 2;
264 if (slot & 0x1) {
265 tmp = c_list[id] & 0xf;
266 c_list[id] = (value << 4) | tmp;
267 } else {
268 tmp = c_list[id] & 0xf0;
269 c_list[id] = (value & 0xf) | tmp;
270 }
271}
272
273/**
274 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
275 *
276 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
277 * Specification.
278 */
279static void srpt_get_class_port_info(struct ib_dm_mad *mad)
280{
281 struct ib_class_port_info *cif;
282
283 cif = (struct ib_class_port_info *)mad->data;
284 memset(cif, 0, sizeof *cif);
285 cif->base_version = 1;
286 cif->class_version = 1;
287 cif->resp_time_value = 20;
288
289 mad->mad_hdr.status = 0;
290}
291
292/**
293 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
294 *
295 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
296 * Specification. See also section B.7, table B.6 in the SRP r16a document.
297 */
298static void srpt_get_iou(struct ib_dm_mad *mad)
299{
300 struct ib_dm_iou_info *ioui;
301 u8 slot;
302 int i;
303
304 ioui = (struct ib_dm_iou_info *)mad->data;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530305 ioui->change_id = cpu_to_be16(1);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000306 ioui->max_controllers = 16;
307
308 /* set present for slot 1 and empty for the rest */
309 srpt_set_ioc(ioui->controller_list, 1, 1);
310 for (i = 1, slot = 2; i < 16; i++, slot++)
311 srpt_set_ioc(ioui->controller_list, slot, 0);
312
313 mad->mad_hdr.status = 0;
314}
315
316/**
317 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
318 *
319 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
320 * Architecture Specification. See also section B.7, table B.7 in the SRP
321 * r16a document.
322 */
323static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
324 struct ib_dm_mad *mad)
325{
326 struct srpt_device *sdev = sport->sdev;
327 struct ib_dm_ioc_profile *iocp;
328
329 iocp = (struct ib_dm_ioc_profile *)mad->data;
330
331 if (!slot || slot > 16) {
332 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530333 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000334 return;
335 }
336
337 if (slot > 2) {
338 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530339 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000340 return;
341 }
342
343 memset(iocp, 0, sizeof *iocp);
344 strcpy(iocp->id_string, SRPT_ID_STRING);
345 iocp->guid = cpu_to_be64(srpt_service_guid);
346 iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
347 iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
348 iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
349 iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
350 iocp->subsys_device_id = 0x0;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530351 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
352 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
353 iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
354 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000355 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
356 iocp->rdma_read_depth = 4;
357 iocp->send_size = cpu_to_be32(srp_max_req_size);
358 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
359 1U << 24));
360 iocp->num_svc_entries = 1;
361 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
362 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
363
364 mad->mad_hdr.status = 0;
365}
366
367/**
368 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
369 *
370 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
371 * Specification. See also section B.7, table B.8 in the SRP r16a document.
372 */
373static void srpt_get_svc_entries(u64 ioc_guid,
374 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
375{
376 struct ib_dm_svc_entries *svc_entries;
377
378 WARN_ON(!ioc_guid);
379
380 if (!slot || slot > 16) {
381 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530382 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000383 return;
384 }
385
386 if (slot > 2 || lo > hi || hi > 1) {
387 mad->mad_hdr.status
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530388 = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000389 return;
390 }
391
392 svc_entries = (struct ib_dm_svc_entries *)mad->data;
393 memset(svc_entries, 0, sizeof *svc_entries);
394 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
395 snprintf(svc_entries->service_entries[0].name,
396 sizeof(svc_entries->service_entries[0].name),
397 "%s%016llx",
398 SRP_SERVICE_NAME_PREFIX,
399 ioc_guid);
400
401 mad->mad_hdr.status = 0;
402}
403
404/**
405 * srpt_mgmt_method_get() - Process a received management datagram.
406 * @sp: source port through which the MAD has been received.
407 * @rq_mad: received MAD.
408 * @rsp_mad: response MAD.
409 */
410static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
411 struct ib_dm_mad *rsp_mad)
412{
413 u16 attr_id;
414 u32 slot;
415 u8 hi, lo;
416
417 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
418 switch (attr_id) {
419 case DM_ATTR_CLASS_PORT_INFO:
420 srpt_get_class_port_info(rsp_mad);
421 break;
422 case DM_ATTR_IOU_INFO:
423 srpt_get_iou(rsp_mad);
424 break;
425 case DM_ATTR_IOC_PROFILE:
426 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
427 srpt_get_ioc(sp, slot, rsp_mad);
428 break;
429 case DM_ATTR_SVC_ENTRIES:
430 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
431 hi = (u8) ((slot >> 8) & 0xff);
432 lo = (u8) (slot & 0xff);
433 slot = (u16) ((slot >> 16) & 0xffff);
434 srpt_get_svc_entries(srpt_service_guid,
435 slot, hi, lo, rsp_mad);
436 break;
437 default:
438 rsp_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530439 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000440 break;
441 }
442}
443
444/**
445 * srpt_mad_send_handler() - Post MAD-send callback function.
446 */
447static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
448 struct ib_mad_send_wc *mad_wc)
449{
450 ib_destroy_ah(mad_wc->send_buf->ah);
451 ib_free_send_mad(mad_wc->send_buf);
452}
453
454/**
455 * srpt_mad_recv_handler() - MAD reception callback function.
456 */
457static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
458 struct ib_mad_recv_wc *mad_wc)
459{
460 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
461 struct ib_ah *ah;
462 struct ib_mad_send_buf *rsp;
463 struct ib_dm_mad *dm_mad;
464
465 if (!mad_wc || !mad_wc->recv_buf.mad)
466 return;
467
468 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
469 mad_wc->recv_buf.grh, mad_agent->port_num);
470 if (IS_ERR(ah))
471 goto err;
472
473 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
474
475 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
476 mad_wc->wc->pkey_index, 0,
477 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400478 GFP_KERNEL,
479 IB_MGMT_BASE_VERSION);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000480 if (IS_ERR(rsp))
481 goto err_rsp;
482
483 rsp->ah = ah;
484
485 dm_mad = rsp->mad;
486 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
487 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
488 dm_mad->mad_hdr.status = 0;
489
490 switch (mad_wc->recv_buf.mad->mad_hdr.method) {
491 case IB_MGMT_METHOD_GET:
492 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
493 break;
494 case IB_MGMT_METHOD_SET:
495 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530496 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000497 break;
498 default:
499 dm_mad->mad_hdr.status =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +0530500 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000501 break;
502 }
503
504 if (!ib_post_send_mad(rsp, NULL)) {
505 ib_free_recv_mad(mad_wc);
506 /* will destroy_ah & free_send_mad in send completion */
507 return;
508 }
509
510 ib_free_send_mad(rsp);
511
512err_rsp:
513 ib_destroy_ah(ah);
514err:
515 ib_free_recv_mad(mad_wc);
516}
517
518/**
519 * srpt_refresh_port() - Configure a HCA port.
520 *
521 * Enable InfiniBand management datagram processing, update the cached sm_lid,
522 * lid and gid values, and register a callback function for processing MADs
523 * on the specified port.
524 *
525 * Note: It is safe to call this function more than once for the same port.
526 */
527static int srpt_refresh_port(struct srpt_port *sport)
528{
529 struct ib_mad_reg_req reg_req;
530 struct ib_port_modify port_modify;
531 struct ib_port_attr port_attr;
532 int ret;
533
534 memset(&port_modify, 0, sizeof port_modify);
535 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
536 port_modify.clr_port_cap_mask = 0;
537
538 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
539 if (ret)
540 goto err_mod_port;
541
542 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
543 if (ret)
544 goto err_query_port;
545
546 sport->sm_lid = port_attr.sm_lid;
547 sport->lid = port_attr.lid;
548
Matan Barak55ee3ab2015-10-15 18:38:45 +0300549 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
550 NULL);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000551 if (ret)
552 goto err_query_port;
553
554 if (!sport->mad_agent) {
555 memset(&reg_req, 0, sizeof reg_req);
556 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
557 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
558 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
559 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
560
561 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
562 sport->port,
563 IB_QPT_GSI,
564 &reg_req, 0,
565 srpt_mad_send_handler,
566 srpt_mad_recv_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400567 sport, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000568 if (IS_ERR(sport->mad_agent)) {
569 ret = PTR_ERR(sport->mad_agent);
570 sport->mad_agent = NULL;
571 goto err_query_port;
572 }
573 }
574
575 return 0;
576
577err_query_port:
578
579 port_modify.set_port_cap_mask = 0;
580 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
581 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
582
583err_mod_port:
584
585 return ret;
586}
587
588/**
589 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
590 *
591 * Note: It is safe to call this function more than once for the same device.
592 */
593static void srpt_unregister_mad_agent(struct srpt_device *sdev)
594{
595 struct ib_port_modify port_modify = {
596 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
597 };
598 struct srpt_port *sport;
599 int i;
600
601 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
602 sport = &sdev->port[i - 1];
603 WARN_ON(sport->port != i);
604 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400605 pr_err("disabling MAD processing failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000606 if (sport->mad_agent) {
607 ib_unregister_mad_agent(sport->mad_agent);
608 sport->mad_agent = NULL;
609 }
610 }
611}
612
613/**
614 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
615 */
616static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
617 int ioctx_size, int dma_size,
618 enum dma_data_direction dir)
619{
620 struct srpt_ioctx *ioctx;
621
622 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
623 if (!ioctx)
624 goto err;
625
626 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
627 if (!ioctx->buf)
628 goto err_free_ioctx;
629
630 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
631 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
632 goto err_free_buf;
633
634 return ioctx;
635
636err_free_buf:
637 kfree(ioctx->buf);
638err_free_ioctx:
639 kfree(ioctx);
640err:
641 return NULL;
642}
643
644/**
645 * srpt_free_ioctx() - Free an SRPT I/O context structure.
646 */
647static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
648 int dma_size, enum dma_data_direction dir)
649{
650 if (!ioctx)
651 return;
652
653 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
654 kfree(ioctx->buf);
655 kfree(ioctx);
656}
657
658/**
659 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
660 * @sdev: Device to allocate the I/O context ring for.
661 * @ring_size: Number of elements in the I/O context ring.
662 * @ioctx_size: I/O context size.
663 * @dma_size: DMA buffer size.
664 * @dir: DMA data direction.
665 */
666static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
667 int ring_size, int ioctx_size,
668 int dma_size, enum dma_data_direction dir)
669{
670 struct srpt_ioctx **ring;
671 int i;
672
673 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
674 && ioctx_size != sizeof(struct srpt_send_ioctx));
675
676 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
677 if (!ring)
678 goto out;
679 for (i = 0; i < ring_size; ++i) {
680 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
681 if (!ring[i])
682 goto err;
683 ring[i]->index = i;
684 }
685 goto out;
686
687err:
688 while (--i >= 0)
689 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
690 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100691 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000692out:
693 return ring;
694}
695
696/**
697 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
698 */
699static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
700 struct srpt_device *sdev, int ring_size,
701 int dma_size, enum dma_data_direction dir)
702{
703 int i;
704
705 for (i = 0; i < ring_size; ++i)
706 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
707 kfree(ioctx_ring);
708}
709
710/**
711 * srpt_get_cmd_state() - Get the state of a SCSI command.
712 */
713static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
714{
715 enum srpt_command_state state;
716 unsigned long flags;
717
718 BUG_ON(!ioctx);
719
720 spin_lock_irqsave(&ioctx->spinlock, flags);
721 state = ioctx->state;
722 spin_unlock_irqrestore(&ioctx->spinlock, flags);
723 return state;
724}
725
726/**
727 * srpt_set_cmd_state() - Set the state of a SCSI command.
728 *
729 * Does not modify the state of aborted commands. Returns the previous command
730 * state.
731 */
732static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
733 enum srpt_command_state new)
734{
735 enum srpt_command_state previous;
736 unsigned long flags;
737
738 BUG_ON(!ioctx);
739
740 spin_lock_irqsave(&ioctx->spinlock, flags);
741 previous = ioctx->state;
742 if (previous != SRPT_STATE_DONE)
743 ioctx->state = new;
744 spin_unlock_irqrestore(&ioctx->spinlock, flags);
745
746 return previous;
747}
748
749/**
750 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
751 *
752 * Returns true if and only if the previous command state was equal to 'old'.
753 */
754static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
755 enum srpt_command_state old,
756 enum srpt_command_state new)
757{
758 enum srpt_command_state previous;
759 unsigned long flags;
760
761 WARN_ON(!ioctx);
762 WARN_ON(old == SRPT_STATE_DONE);
763 WARN_ON(new == SRPT_STATE_NEW);
764
765 spin_lock_irqsave(&ioctx->spinlock, flags);
766 previous = ioctx->state;
767 if (previous == old)
768 ioctx->state = new;
769 spin_unlock_irqrestore(&ioctx->spinlock, flags);
770 return previous == old;
771}
772
773/**
774 * srpt_post_recv() - Post an IB receive request.
775 */
776static int srpt_post_recv(struct srpt_device *sdev,
777 struct srpt_recv_ioctx *ioctx)
778{
779 struct ib_sge list;
780 struct ib_recv_wr wr, *bad_wr;
781
782 BUG_ON(!sdev);
Bart Van Asschea42d9852011-10-14 01:30:46 +0000783 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
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200787 ioctx->ioctx.cqe.done = srpt_recv_done;
788 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000789 wr.next = NULL;
790 wr.sg_list = &list;
791 wr.num_sge = 1;
792
793 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
794}
795
796/**
797 * srpt_post_send() - Post an IB send request.
798 *
799 * Returns zero upon success and a non-zero value upon failure.
800 */
801static int srpt_post_send(struct srpt_rdma_ch *ch,
802 struct srpt_send_ioctx *ioctx, int len)
803{
804 struct ib_sge list;
805 struct ib_send_wr wr, *bad_wr;
806 struct srpt_device *sdev = ch->sport->sdev;
807 int ret;
808
809 atomic_inc(&ch->req_lim);
810
811 ret = -ENOMEM;
812 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400813 pr_warn("IB send queue full (needed 1)\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +0000814 goto out;
815 }
816
817 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
818 DMA_TO_DEVICE);
819
820 list.addr = ioctx->ioctx.dma;
821 list.length = len;
Jason Gunthorpe5a783952015-07-30 17:22:24 -0600822 list.lkey = sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000823
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200824 ioctx->ioctx.cqe.done = srpt_send_done;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000825 wr.next = NULL;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +0200826 wr.wr_cqe = &ioctx->ioctx.cqe;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000827 wr.sg_list = &list;
828 wr.num_sge = 1;
829 wr.opcode = IB_WR_SEND;
830 wr.send_flags = IB_SEND_SIGNALED;
831
832 ret = ib_post_send(ch->qp, &wr, &bad_wr);
833
834out:
835 if (ret < 0) {
836 atomic_inc(&ch->sq_wr_avail);
837 atomic_dec(&ch->req_lim);
838 }
839 return ret;
840}
841
842/**
843 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
844 * @ioctx: Pointer to the I/O context associated with the request.
845 * @srp_cmd: Pointer to the SRP_CMD request data.
846 * @dir: Pointer to the variable to which the transfer direction will be
847 * written.
848 * @data_len: Pointer to the variable to which the total data length of all
849 * descriptors in the SRP_CMD request will be written.
850 *
851 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
852 *
853 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
854 * -ENOMEM when memory allocation fails and zero upon success.
855 */
856static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
857 struct srp_cmd *srp_cmd,
858 enum dma_data_direction *dir, u64 *data_len)
859{
860 struct srp_indirect_buf *idb;
861 struct srp_direct_buf *db;
862 unsigned add_cdb_offset;
863 int ret;
864
865 /*
866 * The pointer computations below will only be compiled correctly
867 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
868 * whether srp_cmd::add_data has been declared as a byte pointer.
869 */
870 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
871 && !__same_type(srp_cmd->add_data[0], (u8)0));
872
873 BUG_ON(!dir);
874 BUG_ON(!data_len);
875
876 ret = 0;
877 *data_len = 0;
878
879 /*
880 * The lower four bits of the buffer format field contain the DATA-IN
881 * buffer descriptor format, and the highest four bits contain the
882 * DATA-OUT buffer descriptor format.
883 */
884 *dir = DMA_NONE;
885 if (srp_cmd->buf_fmt & 0xf)
886 /* DATA-IN: transfer data from target to initiator (read). */
887 *dir = DMA_FROM_DEVICE;
888 else if (srp_cmd->buf_fmt >> 4)
889 /* DATA-OUT: transfer data from initiator to target (write). */
890 *dir = DMA_TO_DEVICE;
891
892 /*
893 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
894 * CDB LENGTH' field are reserved and the size in bytes of this field
895 * is four times the value specified in bits 3..7. Hence the "& ~3".
896 */
897 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
898 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
899 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
900 ioctx->n_rbuf = 1;
901 ioctx->rbufs = &ioctx->single_rbuf;
902
903 db = (struct srp_direct_buf *)(srp_cmd->add_data
904 + add_cdb_offset);
905 memcpy(ioctx->rbufs, db, sizeof *db);
906 *data_len = be32_to_cpu(db->len);
907 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
908 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
909 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
910 + add_cdb_offset);
911
912 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
913
914 if (ioctx->n_rbuf >
915 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -0400916 pr_err("received unsupported SRP_CMD request"
Bart Van Asschea42d9852011-10-14 01:30:46 +0000917 " type (%u out + %u in != %u / %zu)\n",
918 srp_cmd->data_out_desc_cnt,
919 srp_cmd->data_in_desc_cnt,
920 be32_to_cpu(idb->table_desc.len),
921 sizeof(*db));
922 ioctx->n_rbuf = 0;
923 ret = -EINVAL;
924 goto out;
925 }
926
927 if (ioctx->n_rbuf == 1)
928 ioctx->rbufs = &ioctx->single_rbuf;
929 else {
930 ioctx->rbufs =
931 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
932 if (!ioctx->rbufs) {
933 ioctx->n_rbuf = 0;
934 ret = -ENOMEM;
935 goto out;
936 }
937 }
938
939 db = idb->desc_list;
940 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
941 *data_len = be32_to_cpu(idb->len);
942 }
943out:
944 return ret;
945}
946
947/**
948 * srpt_init_ch_qp() - Initialize queue pair attributes.
949 *
950 * Initialized the attributes of queue pair 'qp' by allowing local write,
951 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
952 */
953static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
954{
955 struct ib_qp_attr *attr;
956 int ret;
957
958 attr = kzalloc(sizeof *attr, GFP_KERNEL);
959 if (!attr)
960 return -ENOMEM;
961
962 attr->qp_state = IB_QPS_INIT;
963 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
964 IB_ACCESS_REMOTE_WRITE;
965 attr->port_num = ch->sport->port;
966 attr->pkey_index = 0;
967
968 ret = ib_modify_qp(qp, attr,
969 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
970 IB_QP_PKEY_INDEX);
971
972 kfree(attr);
973 return ret;
974}
975
976/**
977 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
978 * @ch: channel of the queue pair.
979 * @qp: queue pair to change the state of.
980 *
981 * Returns zero upon success and a negative value upon failure.
982 *
983 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
984 * If this structure ever becomes larger, it might be necessary to allocate
985 * it dynamically instead of on the stack.
986 */
987static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
988{
989 struct ib_qp_attr qp_attr;
990 int attr_mask;
991 int ret;
992
993 qp_attr.qp_state = IB_QPS_RTR;
994 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
995 if (ret)
996 goto out;
997
998 qp_attr.max_dest_rd_atomic = 4;
999
1000 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1001
1002out:
1003 return ret;
1004}
1005
1006/**
1007 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1008 * @ch: channel of the queue pair.
1009 * @qp: queue pair to change the state of.
1010 *
1011 * Returns zero upon success and a negative value upon failure.
1012 *
1013 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1014 * If this structure ever becomes larger, it might be necessary to allocate
1015 * it dynamically instead of on the stack.
1016 */
1017static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1018{
1019 struct ib_qp_attr qp_attr;
1020 int attr_mask;
1021 int ret;
1022
1023 qp_attr.qp_state = IB_QPS_RTS;
1024 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1025 if (ret)
1026 goto out;
1027
1028 qp_attr.max_rd_atomic = 4;
1029
1030 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1031
1032out:
1033 return ret;
1034}
1035
1036/**
1037 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1038 */
1039static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1040{
1041 struct ib_qp_attr qp_attr;
1042
1043 qp_attr.qp_state = IB_QPS_ERR;
1044 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1045}
1046
1047/**
1048 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1049 */
1050static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1051 struct srpt_send_ioctx *ioctx)
1052{
1053 struct scatterlist *sg;
1054 enum dma_data_direction dir;
1055
1056 BUG_ON(!ch);
1057 BUG_ON(!ioctx);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001058 BUG_ON(ioctx->n_rdma && !ioctx->rdma_wrs);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001059
1060 while (ioctx->n_rdma)
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001061 kfree(ioctx->rdma_wrs[--ioctx->n_rdma].wr.sg_list);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001062
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001063 kfree(ioctx->rdma_wrs);
1064 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001065
1066 if (ioctx->mapped_sg_count) {
1067 sg = ioctx->sg;
1068 WARN_ON(!sg);
1069 dir = ioctx->cmd.data_direction;
1070 BUG_ON(dir == DMA_NONE);
1071 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1072 opposite_dma_dir(dir));
1073 ioctx->mapped_sg_count = 0;
1074 }
1075}
1076
1077/**
1078 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1079 */
1080static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1081 struct srpt_send_ioctx *ioctx)
1082{
Mike Marciniszynb0768082014-04-07 13:58:35 -04001083 struct ib_device *dev = ch->sport->sdev->device;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001084 struct se_cmd *cmd;
1085 struct scatterlist *sg, *sg_orig;
1086 int sg_cnt;
1087 enum dma_data_direction dir;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001088 struct ib_rdma_wr *riu;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001089 struct srp_direct_buf *db;
1090 dma_addr_t dma_addr;
1091 struct ib_sge *sge;
1092 u64 raddr;
1093 u32 rsize;
1094 u32 tsize;
1095 u32 dma_len;
1096 int count, nrdma;
1097 int i, j, k;
1098
1099 BUG_ON(!ch);
1100 BUG_ON(!ioctx);
1101 cmd = &ioctx->cmd;
1102 dir = cmd->data_direction;
1103 BUG_ON(dir == DMA_NONE);
1104
Roland Dreier6f9e7f02012-03-30 11:29:12 -07001105 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1106 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001107
1108 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1109 opposite_dma_dir(dir));
1110 if (unlikely(!count))
1111 return -EAGAIN;
1112
1113 ioctx->mapped_sg_count = count;
1114
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001115 if (ioctx->rdma_wrs && ioctx->n_rdma_wrs)
1116 nrdma = ioctx->n_rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001117 else {
1118 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1119 + ioctx->n_rbuf;
1120
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001121 ioctx->rdma_wrs = kcalloc(nrdma, sizeof(*ioctx->rdma_wrs),
1122 GFP_KERNEL);
1123 if (!ioctx->rdma_wrs)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001124 goto free_mem;
1125
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001126 ioctx->n_rdma_wrs = nrdma;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001127 }
1128
1129 db = ioctx->rbufs;
1130 tsize = cmd->data_length;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001131 dma_len = ib_sg_dma_len(dev, &sg[0]);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001132 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001133
1134 /*
1135 * For each remote desc - calculate the #ib_sge.
1136 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1137 * each remote desc rdma_iu is required a rdma wr;
1138 * else
1139 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1140 * another rdma wr
1141 */
1142 for (i = 0, j = 0;
1143 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1144 rsize = be32_to_cpu(db->len);
1145 raddr = be64_to_cpu(db->va);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001146 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001147 riu->rkey = be32_to_cpu(db->key);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001148 riu->wr.num_sge = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001149
1150 /* calculate how many sge required for this remote_buf */
1151 while (rsize > 0 && tsize > 0) {
1152
1153 if (rsize >= dma_len) {
1154 tsize -= dma_len;
1155 rsize -= dma_len;
1156 raddr += dma_len;
1157
1158 if (tsize > 0) {
1159 ++j;
1160 if (j < count) {
1161 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001162 dma_len = ib_sg_dma_len(
1163 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001164 }
1165 }
1166 } else {
1167 tsize -= rsize;
1168 dma_len -= rsize;
1169 rsize = 0;
1170 }
1171
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001172 ++riu->wr.num_sge;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001173
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001174 if (rsize > 0 &&
1175 riu->wr.num_sge == SRPT_DEF_SG_PER_WQE) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001176 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001177 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1178 sizeof(*riu->wr.sg_list),
1179 GFP_KERNEL);
1180 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001181 goto free_mem;
1182
1183 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001184 riu->wr.num_sge = 0;
1185 riu->remote_addr = raddr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001186 riu->rkey = be32_to_cpu(db->key);
1187 }
1188 }
1189
1190 ++ioctx->n_rdma;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001191 riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
1192 sizeof(*riu->wr.sg_list),
1193 GFP_KERNEL);
1194 if (!riu->wr.sg_list)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001195 goto free_mem;
1196 }
1197
1198 db = ioctx->rbufs;
1199 tsize = cmd->data_length;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001200 riu = ioctx->rdma_wrs;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001201 sg = sg_orig;
Mike Marciniszynb0768082014-04-07 13:58:35 -04001202 dma_len = ib_sg_dma_len(dev, &sg[0]);
1203 dma_addr = ib_sg_dma_address(dev, &sg[0]);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001204
1205 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1206 for (i = 0, j = 0;
1207 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1208 rsize = be32_to_cpu(db->len);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001209 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001210 k = 0;
1211
1212 while (rsize > 0 && tsize > 0) {
1213 sge->addr = dma_addr;
Jason Gunthorpe5a783952015-07-30 17:22:24 -06001214 sge->lkey = ch->sport->sdev->pd->local_dma_lkey;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001215
1216 if (rsize >= dma_len) {
1217 sge->length =
1218 (tsize < dma_len) ? tsize : dma_len;
1219 tsize -= dma_len;
1220 rsize -= dma_len;
1221
1222 if (tsize > 0) {
1223 ++j;
1224 if (j < count) {
1225 sg = sg_next(sg);
Mike Marciniszynb0768082014-04-07 13:58:35 -04001226 dma_len = ib_sg_dma_len(
1227 dev, sg);
1228 dma_addr = ib_sg_dma_address(
1229 dev, sg);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001230 }
1231 }
1232 } else {
1233 sge->length = (tsize < rsize) ? tsize : rsize;
1234 tsize -= rsize;
1235 dma_len -= rsize;
1236 dma_addr += rsize;
1237 rsize = 0;
1238 }
1239
1240 ++k;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001241 if (k == riu->wr.num_sge && rsize > 0 && tsize > 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001242 ++riu;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001243 sge = riu->wr.sg_list;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001244 k = 0;
1245 } else if (rsize > 0 && tsize > 0)
1246 ++sge;
1247 }
1248 }
1249
1250 return 0;
1251
1252free_mem:
1253 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1254
1255 return -ENOMEM;
1256}
1257
1258/**
1259 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1260 */
1261static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1262{
1263 struct srpt_send_ioctx *ioctx;
1264 unsigned long flags;
1265
1266 BUG_ON(!ch);
1267
1268 ioctx = NULL;
1269 spin_lock_irqsave(&ch->spinlock, flags);
1270 if (!list_empty(&ch->free_list)) {
1271 ioctx = list_first_entry(&ch->free_list,
1272 struct srpt_send_ioctx, free_list);
1273 list_del(&ioctx->free_list);
1274 }
1275 spin_unlock_irqrestore(&ch->spinlock, flags);
1276
1277 if (!ioctx)
1278 return ioctx;
1279
1280 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001281 spin_lock_init(&ioctx->spinlock);
1282 ioctx->state = SRPT_STATE_NEW;
1283 ioctx->n_rbuf = 0;
1284 ioctx->rbufs = NULL;
1285 ioctx->n_rdma = 0;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001286 ioctx->n_rdma_wrs = 0;
1287 ioctx->rdma_wrs = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001288 ioctx->mapped_sg_count = 0;
1289 init_completion(&ioctx->tx_done);
1290 ioctx->queue_status_only = false;
1291 /*
1292 * transport_init_se_cmd() does not initialize all fields, so do it
1293 * here.
1294 */
1295 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1296 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1297
1298 return ioctx;
1299}
1300
1301/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001302 * srpt_abort_cmd() - Abort a SCSI command.
1303 * @ioctx: I/O context associated with the SCSI command.
1304 * @context: Preferred execution context.
1305 */
1306static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1307{
1308 enum srpt_command_state state;
1309 unsigned long flags;
1310
1311 BUG_ON(!ioctx);
1312
1313 /*
1314 * If the command is in a state where the target core is waiting for
1315 * the ib_srpt driver, change the state to the next state. Changing
1316 * the state of the command from SRPT_STATE_NEED_DATA to
1317 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1318 * function a second time.
1319 */
1320
1321 spin_lock_irqsave(&ioctx->spinlock, flags);
1322 state = ioctx->state;
1323 switch (state) {
1324 case SRPT_STATE_NEED_DATA:
1325 ioctx->state = SRPT_STATE_DATA_IN;
1326 break;
1327 case SRPT_STATE_DATA_IN:
1328 case SRPT_STATE_CMD_RSP_SENT:
1329 case SRPT_STATE_MGMT_RSP_SENT:
1330 ioctx->state = SRPT_STATE_DONE;
1331 break;
1332 default:
1333 break;
1334 }
1335 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1336
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001337 if (state == SRPT_STATE_DONE) {
1338 struct srpt_rdma_ch *ch = ioctx->ch;
1339
1340 BUG_ON(ch->sess == NULL);
1341
Bart Van Asscheafc16602015-04-27 13:52:36 +02001342 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001343 goto out;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001344 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001345
1346 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
Bart Van Assche649ee052015-04-14 13:26:44 +02001347 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001348
1349 switch (state) {
1350 case SRPT_STATE_NEW:
1351 case SRPT_STATE_DATA_IN:
1352 case SRPT_STATE_MGMT:
1353 /*
1354 * Do nothing - defer abort processing until
1355 * srpt_queue_response() is invoked.
1356 */
1357 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1358 break;
1359 case SRPT_STATE_NEED_DATA:
1360 /* DMA_TO_DEVICE (write) - RDMA read error. */
Christoph Hellwige672a472012-07-08 15:58:43 -04001361
1362 /* XXX(hch): this is a horrible layering violation.. */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001363 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
Christoph Hellwige672a472012-07-08 15:58:43 -04001364 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001365 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001366 break;
1367 case SRPT_STATE_CMD_RSP_SENT:
1368 /*
1369 * SRP_RSP sending failed or the SRP_RSP send completion has
1370 * not been received in time.
1371 */
1372 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001373 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001374 break;
1375 case SRPT_STATE_MGMT_RSP_SENT:
1376 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001377 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001378 break;
1379 default:
Grant Grundler532ec6f2013-03-26 21:48:28 +00001380 WARN(1, "Unexpected command state (%d)", state);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001381 break;
1382 }
1383
1384out:
1385 return state;
1386}
1387
1388/**
Christoph Hellwige672a472012-07-08 15:58:43 -04001389 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1390 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001391 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001392 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001393 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001394static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001395{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001396 struct srpt_rdma_ch *ch = cq->cq_context;
1397 struct srpt_send_ioctx *ioctx =
1398 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1399
Bart Van Asschea42d9852011-10-14 01:30:46 +00001400 WARN_ON(ioctx->n_rdma <= 0);
1401 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1402
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001403 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1404 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1405 ioctx, wc->status);
1406 srpt_abort_cmd(ioctx);
1407 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001408 }
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001409
1410 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1411 SRPT_STATE_DATA_IN))
1412 target_execute_cmd(&ioctx->cmd);
1413 else
1414 pr_err("%s[%d]: wrong state = %d\n", __func__,
1415 __LINE__, srpt_get_cmd_state(ioctx));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001416}
1417
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001418static void srpt_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001419{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001420 struct srpt_send_ioctx *ioctx =
1421 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001422
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001423 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1424 pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n",
1425 ioctx, wc->status);
1426 srpt_abort_cmd(ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001427 }
1428}
1429
1430/**
1431 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1432 * @ch: RDMA channel through which the request has been received.
1433 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1434 * be built in the buffer ioctx->buf points at and hence this function will
1435 * overwrite the request data.
1436 * @tag: tag of the request for which this response is being generated.
1437 * @status: value for the STATUS field of the SRP_RSP information unit.
1438 *
1439 * Returns the size in bytes of the SRP_RSP response.
1440 *
1441 * An SRP_RSP response contains a SCSI status or service response. See also
1442 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1443 * response. See also SPC-2 for more information about sense data.
1444 */
1445static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1446 struct srpt_send_ioctx *ioctx, u64 tag,
1447 int status)
1448{
1449 struct srp_rsp *srp_rsp;
1450 const u8 *sense_data;
1451 int sense_data_len, max_sense_len;
1452
1453 /*
1454 * The lowest bit of all SAM-3 status codes is zero (see also
1455 * paragraph 5.3 in SAM-3).
1456 */
1457 WARN_ON(status & 1);
1458
1459 srp_rsp = ioctx->ioctx.buf;
1460 BUG_ON(!srp_rsp);
1461
1462 sense_data = ioctx->sense_data;
1463 sense_data_len = ioctx->cmd.scsi_sense_length;
1464 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1465
1466 memset(srp_rsp, 0, sizeof *srp_rsp);
1467 srp_rsp->opcode = SRP_RSP;
1468 srp_rsp->req_lim_delta =
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301469 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001470 srp_rsp->tag = tag;
1471 srp_rsp->status = status;
1472
1473 if (sense_data_len) {
1474 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1475 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1476 if (sense_data_len > max_sense_len) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001477 pr_warn("truncated sense data from %d to %d"
1478 " bytes\n", sense_data_len, max_sense_len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001479 sense_data_len = max_sense_len;
1480 }
1481
1482 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1483 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1484 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1485 }
1486
1487 return sizeof(*srp_rsp) + sense_data_len;
1488}
1489
1490/**
1491 * srpt_build_tskmgmt_rsp() - Build a task management response.
1492 * @ch: RDMA channel through which the request has been received.
1493 * @ioctx: I/O context in which the SRP_RSP response will be built.
1494 * @rsp_code: RSP_CODE that will be stored in the response.
1495 * @tag: Tag of the request for which this response is being generated.
1496 *
1497 * Returns the size in bytes of the SRP_RSP response.
1498 *
1499 * An SRP_RSP response contains a SCSI status or service response. See also
1500 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1501 * response.
1502 */
1503static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1504 struct srpt_send_ioctx *ioctx,
1505 u8 rsp_code, u64 tag)
1506{
1507 struct srp_rsp *srp_rsp;
1508 int resp_data_len;
1509 int resp_len;
1510
Jack Wangc807f642013-09-30 10:09:05 +02001511 resp_data_len = 4;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001512 resp_len = sizeof(*srp_rsp) + resp_data_len;
1513
1514 srp_rsp = ioctx->ioctx.buf;
1515 BUG_ON(!srp_rsp);
1516 memset(srp_rsp, 0, sizeof *srp_rsp);
1517
1518 srp_rsp->opcode = SRP_RSP;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301519 srp_rsp->req_lim_delta =
1520 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
Bart Van Asschea42d9852011-10-14 01:30:46 +00001521 srp_rsp->tag = tag;
1522
Jack Wangc807f642013-09-30 10:09:05 +02001523 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1524 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1525 srp_rsp->data[3] = rsp_code;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001526
1527 return resp_len;
1528}
1529
1530#define NO_SUCH_LUN ((uint64_t)-1LL)
1531
1532/*
1533 * SCSI LUN addressing method. See also SAM-2 and the section about
1534 * eight byte LUNs.
1535 */
1536enum scsi_lun_addr_method {
1537 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1538 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1539 SCSI_LUN_ADDR_METHOD_LUN = 2,
1540 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1541};
1542
1543/*
1544 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1545 *
1546 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1547 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1548 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1549 */
1550static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1551{
1552 uint64_t res = NO_SUCH_LUN;
1553 int addressing_method;
1554
1555 if (unlikely(len < 2)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001556 pr_err("Illegal LUN length %d, expected 2 bytes or more\n",
1557 len);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001558 goto out;
1559 }
1560
1561 switch (len) {
1562 case 8:
1563 if ((*((__be64 *)lun) &
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05301564 cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001565 goto out_err;
1566 break;
1567 case 4:
1568 if (*((__be16 *)&lun[2]) != 0)
1569 goto out_err;
1570 break;
1571 case 6:
1572 if (*((__be32 *)&lun[2]) != 0)
1573 goto out_err;
1574 break;
1575 case 2:
1576 break;
1577 default:
1578 goto out_err;
1579 }
1580
1581 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1582 switch (addressing_method) {
1583 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1584 case SCSI_LUN_ADDR_METHOD_FLAT:
1585 case SCSI_LUN_ADDR_METHOD_LUN:
1586 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1587 break;
1588
1589 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1590 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001591 pr_err("Unimplemented LUN addressing method %u\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001592 addressing_method);
1593 break;
1594 }
1595
1596out:
1597 return res;
1598
1599out_err:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001600 pr_err("Support for multi-level LUNs has not yet been implemented\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001601 goto out;
1602}
1603
1604static int srpt_check_stop_free(struct se_cmd *cmd)
1605{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001606 struct srpt_send_ioctx *ioctx = container_of(cmd,
1607 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001608
Bart Van Asscheafc16602015-04-27 13:52:36 +02001609 return target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001610}
1611
1612/**
1613 * srpt_handle_cmd() - Process SRP_CMD.
1614 */
1615static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1616 struct srpt_recv_ioctx *recv_ioctx,
1617 struct srpt_send_ioctx *send_ioctx)
1618{
1619 struct se_cmd *cmd;
1620 struct srp_cmd *srp_cmd;
1621 uint64_t unpacked_lun;
1622 u64 data_len;
1623 enum dma_data_direction dir;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001624 sense_reason_t ret;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001625 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001626
1627 BUG_ON(!send_ioctx);
1628
1629 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001630 cmd = &send_ioctx->cmd;
Bart Van Assche649ee052015-04-14 13:26:44 +02001631 cmd->tag = srp_cmd->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001632
1633 switch (srp_cmd->task_attr) {
1634 case SRP_CMD_SIMPLE_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001635 cmd->sam_task_attr = TCM_SIMPLE_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001636 break;
1637 case SRP_CMD_ORDERED_Q:
1638 default:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001639 cmd->sam_task_attr = TCM_ORDERED_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001640 break;
1641 case SRP_CMD_HEAD_OF_Q:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001642 cmd->sam_task_attr = TCM_HEAD_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001643 break;
1644 case SRP_CMD_ACA:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001645 cmd->sam_task_attr = TCM_ACA_TAG;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001646 break;
1647 }
1648
Christoph Hellwigde103c92012-11-06 12:24:09 -08001649 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001650 pr_err("0x%llx: parsing SRP descriptor table failed.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001651 srp_cmd->tag);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001652 ret = TCM_INVALID_CDB_FIELD;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001653 goto send_sense;
1654 }
1655
Bart Van Asschea42d9852011-10-14 01:30:46 +00001656 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1657 sizeof(srp_cmd->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001658 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1659 &send_ioctx->sense_data[0], unpacked_lun, data_len,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001660 TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001661 if (rc != 0) {
1662 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001663 goto send_sense;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001664 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001665 return 0;
1666
1667send_sense:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001668 transport_send_check_condition_and_sense(cmd, ret, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001669 return -1;
1670}
1671
1672/**
1673 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1674 * @ch: RDMA channel of the task management request.
1675 * @fn: Task management function to perform.
1676 * @req_tag: Tag of the SRP task management request.
1677 * @mgmt_ioctx: I/O context of the task management request.
1678 *
1679 * Returns zero if the target core will process the task management
1680 * request asynchronously.
1681 *
1682 * Note: It is assumed that the initiator serializes tag-based task management
1683 * requests.
1684 */
1685static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1686{
1687 struct srpt_device *sdev;
1688 struct srpt_rdma_ch *ch;
1689 struct srpt_send_ioctx *target;
1690 int ret, i;
1691
1692 ret = -EINVAL;
1693 ch = ioctx->ch;
1694 BUG_ON(!ch);
1695 BUG_ON(!ch->sport);
1696 sdev = ch->sport->sdev;
1697 BUG_ON(!sdev);
1698 spin_lock_irq(&sdev->spinlock);
1699 for (i = 0; i < ch->rq_size; ++i) {
1700 target = ch->ioctx_ring[i];
1701 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
Bart Van Assche649ee052015-04-14 13:26:44 +02001702 target->cmd.tag == tag &&
Bart Van Asschea42d9852011-10-14 01:30:46 +00001703 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1704 ret = 0;
1705 /* now let the target core abort &target->cmd; */
1706 break;
1707 }
1708 }
1709 spin_unlock_irq(&sdev->spinlock);
1710 return ret;
1711}
1712
1713static int srp_tmr_to_tcm(int fn)
1714{
1715 switch (fn) {
1716 case SRP_TSK_ABORT_TASK:
1717 return TMR_ABORT_TASK;
1718 case SRP_TSK_ABORT_TASK_SET:
1719 return TMR_ABORT_TASK_SET;
1720 case SRP_TSK_CLEAR_TASK_SET:
1721 return TMR_CLEAR_TASK_SET;
1722 case SRP_TSK_LUN_RESET:
1723 return TMR_LUN_RESET;
1724 case SRP_TSK_CLEAR_ACA:
1725 return TMR_CLEAR_ACA;
1726 default:
1727 return -1;
1728 }
1729}
1730
1731/**
1732 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1733 *
1734 * Returns 0 if and only if the request will be processed by the target core.
1735 *
1736 * For more information about SRP_TSK_MGMT information units, see also section
1737 * 6.7 in the SRP r16a document.
1738 */
1739static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1740 struct srpt_recv_ioctx *recv_ioctx,
1741 struct srpt_send_ioctx *send_ioctx)
1742{
1743 struct srp_tsk_mgmt *srp_tsk;
1744 struct se_cmd *cmd;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001745 struct se_session *sess = ch->sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001746 uint64_t unpacked_lun;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001747 uint32_t tag = 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001748 int tcm_tmr;
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001749 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001750
1751 BUG_ON(!send_ioctx);
1752
1753 srp_tsk = recv_ioctx->ioctx.buf;
1754 cmd = &send_ioctx->cmd;
1755
1756 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1757 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1758 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1759
1760 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
Bart Van Assche649ee052015-04-14 13:26:44 +02001761 send_ioctx->cmd.tag = srp_tsk->tag;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001762 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1763 if (tcm_tmr < 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001764 send_ioctx->cmd.se_tmr_req->response =
1765 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001766 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001767 }
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001768 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1769 sizeof(srp_tsk->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001770
Nicholas Bellinger3e4f5742012-11-28 01:38:04 -08001771 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK) {
1772 rc = srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1773 if (rc < 0) {
1774 send_ioctx->cmd.se_tmr_req->response =
1775 TMR_TASK_DOES_NOT_EXIST;
1776 goto fail;
1777 }
1778 tag = srp_tsk->task_tag;
1779 }
1780 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
1781 srp_tsk, tcm_tmr, GFP_KERNEL, tag,
1782 TARGET_SCF_ACK_KREF);
1783 if (rc != 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001784 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001785 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001786 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001787 return;
1788fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001789 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001790}
1791
1792/**
1793 * srpt_handle_new_iu() - Process a newly received information unit.
1794 * @ch: RDMA channel through which the information unit has been received.
1795 * @ioctx: SRPT I/O context associated with the information unit.
1796 */
1797static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1798 struct srpt_recv_ioctx *recv_ioctx,
1799 struct srpt_send_ioctx *send_ioctx)
1800{
1801 struct srp_cmd *srp_cmd;
1802 enum rdma_ch_state ch_state;
1803
1804 BUG_ON(!ch);
1805 BUG_ON(!recv_ioctx);
1806
1807 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1808 recv_ioctx->ioctx.dma, srp_max_req_size,
1809 DMA_FROM_DEVICE);
1810
1811 ch_state = srpt_get_ch_state(ch);
1812 if (unlikely(ch_state == CH_CONNECTING)) {
1813 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1814 goto out;
1815 }
1816
1817 if (unlikely(ch_state != CH_LIVE))
1818 goto out;
1819
1820 srp_cmd = recv_ioctx->ioctx.buf;
1821 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1822 if (!send_ioctx)
1823 send_ioctx = srpt_get_send_ioctx(ch);
1824 if (unlikely(!send_ioctx)) {
1825 list_add_tail(&recv_ioctx->wait_list,
1826 &ch->cmd_wait_list);
1827 goto out;
1828 }
1829 }
1830
Bart Van Asschea42d9852011-10-14 01:30:46 +00001831 switch (srp_cmd->opcode) {
1832 case SRP_CMD:
1833 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1834 break;
1835 case SRP_TSK_MGMT:
1836 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1837 break;
1838 case SRP_I_LOGOUT:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001839 pr_err("Not yet implemented: SRP_I_LOGOUT\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001840 break;
1841 case SRP_CRED_RSP:
1842 pr_debug("received SRP_CRED_RSP\n");
1843 break;
1844 case SRP_AER_RSP:
1845 pr_debug("received SRP_AER_RSP\n");
1846 break;
1847 case SRP_RSP:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001848 pr_err("Received SRP_RSP\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00001849 break;
1850 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001851 pr_err("received IU with unknown opcode 0x%x\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001852 srp_cmd->opcode);
1853 break;
1854 }
1855
1856 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1857out:
1858 return;
1859}
1860
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001861static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001862{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001863 struct srpt_rdma_ch *ch = cq->cq_context;
1864 struct srpt_recv_ioctx *ioctx =
1865 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001866
Bart Van Asschea42d9852011-10-14 01:30:46 +00001867 if (wc->status == IB_WC_SUCCESS) {
1868 int req_lim;
1869
1870 req_lim = atomic_dec_return(&ch->req_lim);
1871 if (unlikely(req_lim < 0))
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001872 pr_err("req_lim = %d < 0\n", req_lim);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001873 srpt_handle_new_iu(ch, ioctx, NULL);
1874 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001875 pr_info("receiving failed for ioctx %p with status %d\n",
1876 ioctx, wc->status);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001877 }
1878}
1879
1880/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001881 * Note: Although this has not yet been observed during tests, at least in
1882 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1883 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1884 * value in each response is set to one, and it is possible that this response
1885 * makes the initiator send a new request before the send completion for that
1886 * response has been processed. This could e.g. happen if the call to
1887 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1888 * if IB retransmission causes generation of the send completion to be
1889 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1890 * are queued on cmd_wait_list. The code below processes these delayed
1891 * requests one at a time.
1892 */
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001893static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
Bart Van Asschea42d9852011-10-14 01:30:46 +00001894{
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001895 struct srpt_rdma_ch *ch = cq->cq_context;
1896 struct srpt_send_ioctx *ioctx =
1897 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1898 enum srpt_command_state state;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001899
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001900 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1901
1902 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1903 state != SRPT_STATE_MGMT_RSP_SENT);
1904
1905 atomic_inc(&ch->sq_wr_avail);
1906
1907 if (wc->status != IB_WC_SUCCESS) {
1908 pr_info("sending response for ioctx 0x%p failed"
1909 " with status %d\n", ioctx, wc->status);
1910
1911 atomic_dec(&ch->req_lim);
1912 srpt_abort_cmd(ioctx);
1913 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001914 }
1915
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001916 if (state != SRPT_STATE_DONE) {
1917 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1918 transport_generic_free_cmd(&ioctx->cmd, 0);
1919 } else {
1920 pr_err("IB completion has been received too late for"
1921 " wr_id = %u.\n", ioctx->ioctx.index);
1922 }
1923
1924out:
1925 while (!list_empty(&ch->cmd_wait_list) &&
1926 srpt_get_ch_state(ch) == CH_LIVE &&
1927 (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001928 struct srpt_recv_ioctx *recv_ioctx;
1929
1930 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1931 struct srpt_recv_ioctx,
1932 wait_list);
1933 list_del(&recv_ioctx->wait_list);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001934 srpt_handle_new_iu(ch, recv_ioctx, ioctx);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001935 }
1936}
1937
Bart Van Asschea42d9852011-10-14 01:30:46 +00001938/**
1939 * srpt_create_ch_ib() - Create receive and send completion queues.
1940 */
1941static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1942{
1943 struct ib_qp_init_attr *qp_init;
1944 struct srpt_port *sport = ch->sport;
1945 struct srpt_device *sdev = sport->sdev;
1946 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
1947 int ret;
1948
1949 WARN_ON(ch->rq_size < 1);
1950
1951 ret = -ENOMEM;
1952 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
1953 if (!qp_init)
1954 goto out;
1955
Bart Van Asscheab477c12014-10-19 18:05:33 +03001956retry:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02001957 ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
1958 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001959 if (IS_ERR(ch->cq)) {
1960 ret = PTR_ERR(ch->cq);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001961 pr_err("failed to create CQ cqe= %d ret= %d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00001962 ch->rq_size + srp_sq_size, ret);
1963 goto out;
1964 }
1965
1966 qp_init->qp_context = (void *)ch;
1967 qp_init->event_handler
1968 = (void(*)(struct ib_event *, void*))srpt_qp_event;
1969 qp_init->send_cq = ch->cq;
1970 qp_init->recv_cq = ch->cq;
1971 qp_init->srq = sdev->srq;
1972 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1973 qp_init->qp_type = IB_QPT_RC;
1974 qp_init->cap.max_send_wr = srp_sq_size;
1975 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
1976
1977 ch->qp = ib_create_qp(sdev->pd, qp_init);
1978 if (IS_ERR(ch->qp)) {
1979 ret = PTR_ERR(ch->qp);
Bart Van Asscheab477c12014-10-19 18:05:33 +03001980 if (ret == -ENOMEM) {
1981 srp_sq_size /= 2;
1982 if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
1983 ib_destroy_cq(ch->cq);
1984 goto retry;
1985 }
1986 }
Doug Ledford9f5d32a2014-10-20 18:25:15 -04001987 pr_err("failed to create_qp ret= %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001988 goto err_destroy_cq;
1989 }
1990
1991 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1992
1993 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1994 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1995 qp_init->cap.max_send_wr, ch->cm_id);
1996
1997 ret = srpt_init_ch_qp(ch, ch->qp);
1998 if (ret)
1999 goto err_destroy_qp;
2000
Bart Van Asschea42d9852011-10-14 01:30:46 +00002001out:
2002 kfree(qp_init);
2003 return ret;
2004
2005err_destroy_qp:
2006 ib_destroy_qp(ch->qp);
2007err_destroy_cq:
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002008 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002009 goto out;
2010}
2011
2012static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2013{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002014 ib_destroy_qp(ch->qp);
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002015 ib_free_cq(ch->cq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002016}
2017
2018/**
2019 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2020 *
2021 * Reset the QP and make sure all resources associated with the channel will
2022 * be deallocated at an appropriate time.
2023 *
2024 * Note: The caller must hold ch->sport->sdev->spinlock.
2025 */
2026static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2027{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002028 enum rdma_ch_state prev_state;
2029 unsigned long flags;
2030
Bart Van Asschea42d9852011-10-14 01:30:46 +00002031 spin_lock_irqsave(&ch->spinlock, flags);
2032 prev_state = ch->state;
2033 switch (prev_state) {
2034 case CH_CONNECTING:
2035 case CH_LIVE:
2036 ch->state = CH_DISCONNECTING;
2037 break;
2038 default:
2039 break;
2040 }
2041 spin_unlock_irqrestore(&ch->spinlock, flags);
2042
2043 switch (prev_state) {
2044 case CH_CONNECTING:
2045 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2046 NULL, 0);
2047 /* fall through */
2048 case CH_LIVE:
2049 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002050 pr_err("sending CM DREQ failed.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002051 break;
2052 case CH_DISCONNECTING:
2053 break;
2054 case CH_DRAINING:
2055 case CH_RELEASING:
2056 break;
2057 }
2058}
2059
2060/**
2061 * srpt_close_ch() - Close an RDMA channel.
2062 */
2063static void srpt_close_ch(struct srpt_rdma_ch *ch)
2064{
2065 struct srpt_device *sdev;
2066
2067 sdev = ch->sport->sdev;
2068 spin_lock_irq(&sdev->spinlock);
2069 __srpt_close_ch(ch);
2070 spin_unlock_irq(&sdev->spinlock);
2071}
2072
2073/**
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002074 * srpt_shutdown_session() - Whether or not a session may be shut down.
2075 */
2076static int srpt_shutdown_session(struct se_session *se_sess)
2077{
2078 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2079 unsigned long flags;
2080
2081 spin_lock_irqsave(&ch->spinlock, flags);
2082 if (ch->in_shutdown) {
2083 spin_unlock_irqrestore(&ch->spinlock, flags);
2084 return true;
2085 }
2086
2087 ch->in_shutdown = true;
2088 target_sess_cmd_list_set_waiting(se_sess);
2089 spin_unlock_irqrestore(&ch->spinlock, flags);
2090
2091 return true;
2092}
2093
2094/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00002095 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2096 * @cm_id: Pointer to the CM ID of the channel to be drained.
2097 *
2098 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2099 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2100 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2101 * waits until all target sessions for the associated IB device have been
2102 * unregistered and target session registration involves a call to
2103 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2104 * this function has finished).
2105 */
2106static void srpt_drain_channel(struct ib_cm_id *cm_id)
2107{
2108 struct srpt_device *sdev;
2109 struct srpt_rdma_ch *ch;
2110 int ret;
2111 bool do_reset = false;
2112
2113 WARN_ON_ONCE(irqs_disabled());
2114
2115 sdev = cm_id->context;
2116 BUG_ON(!sdev);
2117 spin_lock_irq(&sdev->spinlock);
2118 list_for_each_entry(ch, &sdev->rch_list, list) {
2119 if (ch->cm_id == cm_id) {
2120 do_reset = srpt_test_and_set_ch_state(ch,
2121 CH_CONNECTING, CH_DRAINING) ||
2122 srpt_test_and_set_ch_state(ch,
2123 CH_LIVE, CH_DRAINING) ||
2124 srpt_test_and_set_ch_state(ch,
2125 CH_DISCONNECTING, CH_DRAINING);
2126 break;
2127 }
2128 }
2129 spin_unlock_irq(&sdev->spinlock);
2130
2131 if (do_reset) {
Nicholas Bellinger1d19f782013-05-15 01:30:01 -07002132 if (ch->sess)
2133 srpt_shutdown_session(ch->sess);
2134
Bart Van Asschea42d9852011-10-14 01:30:46 +00002135 ret = srpt_ch_qp_err(ch);
2136 if (ret < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002137 pr_err("Setting queue pair in error state"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002138 " failed: %d\n", ret);
2139 }
2140}
2141
2142/**
2143 * srpt_find_channel() - Look up an RDMA channel.
2144 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2145 *
2146 * Return NULL if no matching RDMA channel has been found.
2147 */
2148static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2149 struct ib_cm_id *cm_id)
2150{
2151 struct srpt_rdma_ch *ch;
2152 bool found;
2153
2154 WARN_ON_ONCE(irqs_disabled());
2155 BUG_ON(!sdev);
2156
2157 found = false;
2158 spin_lock_irq(&sdev->spinlock);
2159 list_for_each_entry(ch, &sdev->rch_list, list) {
2160 if (ch->cm_id == cm_id) {
2161 found = true;
2162 break;
2163 }
2164 }
2165 spin_unlock_irq(&sdev->spinlock);
2166
2167 return found ? ch : NULL;
2168}
2169
2170/**
2171 * srpt_release_channel() - Release channel resources.
2172 *
2173 * Schedules the actual release because:
2174 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2175 * trigger a deadlock.
2176 * - It is not safe to call TCM transport_* functions from interrupt context.
2177 */
2178static void srpt_release_channel(struct srpt_rdma_ch *ch)
2179{
2180 schedule_work(&ch->release_work);
2181}
2182
2183static void srpt_release_channel_work(struct work_struct *w)
2184{
2185 struct srpt_rdma_ch *ch;
2186 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002187 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002188
2189 ch = container_of(w, struct srpt_rdma_ch, release_work);
2190 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2191 ch->release_done);
2192
2193 sdev = ch->sport->sdev;
2194 BUG_ON(!sdev);
2195
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002196 se_sess = ch->sess;
2197 BUG_ON(!se_sess);
2198
Joern Engelbe646c2d2013-05-15 00:44:07 -07002199 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002200
2201 transport_deregister_session_configfs(se_sess);
2202 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002203 ch->sess = NULL;
2204
Nicholas Bellinger0b41d6c2013-09-18 12:48:27 -07002205 ib_destroy_cm_id(ch->cm_id);
2206
Bart Van Asschea42d9852011-10-14 01:30:46 +00002207 srpt_destroy_ch_ib(ch);
2208
2209 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2210 ch->sport->sdev, ch->rq_size,
2211 ch->rsp_size, DMA_TO_DEVICE);
2212
2213 spin_lock_irq(&sdev->spinlock);
2214 list_del(&ch->list);
2215 spin_unlock_irq(&sdev->spinlock);
2216
Bart Van Asschea42d9852011-10-14 01:30:46 +00002217 if (ch->release_done)
2218 complete(ch->release_done);
2219
2220 wake_up(&sdev->ch_releaseQ);
2221
2222 kfree(ch);
2223}
2224
2225static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2226 u8 i_port_id[16])
2227{
2228 struct srpt_node_acl *nacl;
2229
2230 list_for_each_entry(nacl, &sport->port_acl_list, list)
2231 if (memcmp(nacl->i_port_id, i_port_id,
2232 sizeof(nacl->i_port_id)) == 0)
2233 return nacl;
2234
2235 return NULL;
2236}
2237
2238static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2239 u8 i_port_id[16])
2240{
2241 struct srpt_node_acl *nacl;
2242
2243 spin_lock_irq(&sport->port_acl_lock);
2244 nacl = __srpt_lookup_acl(sport, i_port_id);
2245 spin_unlock_irq(&sport->port_acl_lock);
2246
2247 return nacl;
2248}
2249
2250/**
2251 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2252 *
2253 * Ownership of the cm_id is transferred to the target session if this
2254 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2255 */
2256static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2257 struct ib_cm_req_event_param *param,
2258 void *private_data)
2259{
2260 struct srpt_device *sdev = cm_id->context;
2261 struct srpt_port *sport = &sdev->port[param->port - 1];
2262 struct srp_login_req *req;
2263 struct srp_login_rsp *rsp;
2264 struct srp_login_rej *rej;
2265 struct ib_cm_rep_param *rep_param;
2266 struct srpt_rdma_ch *ch, *tmp_ch;
2267 struct srpt_node_acl *nacl;
2268 u32 it_iu_len;
2269 int i;
2270 int ret = 0;
2271
2272 WARN_ON_ONCE(irqs_disabled());
2273
2274 if (WARN_ON(!sdev || !private_data))
2275 return -EINVAL;
2276
2277 req = (struct srp_login_req *)private_data;
2278
2279 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2280
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002281 pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2282 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2283 " (guid=0x%llx:0x%llx)\n",
2284 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2285 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2286 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2287 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2288 it_iu_len,
2289 param->port,
2290 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2291 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
Bart Van Asschea42d9852011-10-14 01:30:46 +00002292
2293 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2294 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2295 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2296
2297 if (!rsp || !rej || !rep_param) {
2298 ret = -ENOMEM;
2299 goto out;
2300 }
2301
2302 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302303 rej->reason = cpu_to_be32(
2304 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002305 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002306 pr_err("rejected SRP_LOGIN_REQ because its"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002307 " length (%d bytes) is out of range (%d .. %d)\n",
2308 it_iu_len, 64, srp_max_req_size);
2309 goto reject;
2310 }
2311
2312 if (!sport->enabled) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302313 rej->reason = cpu_to_be32(
2314 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002315 ret = -EINVAL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002316 pr_err("rejected SRP_LOGIN_REQ because the target port"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002317 " has not yet been enabled\n");
2318 goto reject;
2319 }
2320
2321 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2322 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2323
2324 spin_lock_irq(&sdev->spinlock);
2325
2326 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2327 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2328 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2329 && param->port == ch->sport->port
2330 && param->listen_id == ch->sport->sdev->cm_id
2331 && ch->cm_id) {
2332 enum rdma_ch_state ch_state;
2333
2334 ch_state = srpt_get_ch_state(ch);
2335 if (ch_state != CH_CONNECTING
2336 && ch_state != CH_LIVE)
2337 continue;
2338
2339 /* found an existing channel */
2340 pr_debug("Found existing channel %s"
2341 " cm_id= %p state= %d\n",
2342 ch->sess_name, ch->cm_id, ch_state);
2343
2344 __srpt_close_ch(ch);
2345
2346 rsp->rsp_flags =
2347 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2348 }
2349 }
2350
2351 spin_unlock_irq(&sdev->spinlock);
2352
2353 } else
2354 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2355
2356 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2357 || *(__be64 *)(req->target_port_id + 8) !=
2358 cpu_to_be64(srpt_service_guid)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302359 rej->reason = cpu_to_be32(
2360 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002361 ret = -ENOMEM;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002362 pr_err("rejected SRP_LOGIN_REQ because it"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002363 " has an invalid target port identifier.\n");
2364 goto reject;
2365 }
2366
2367 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2368 if (!ch) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302369 rej->reason = cpu_to_be32(
2370 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002371 pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002372 ret = -ENOMEM;
2373 goto reject;
2374 }
2375
2376 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2377 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2378 memcpy(ch->t_port_id, req->target_port_id, 16);
2379 ch->sport = &sdev->port[param->port - 1];
2380 ch->cm_id = cm_id;
2381 /*
2382 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2383 * for the SRP protocol to the command queue size.
2384 */
2385 ch->rq_size = SRPT_RQ_SIZE;
2386 spin_lock_init(&ch->spinlock);
2387 ch->state = CH_CONNECTING;
2388 INIT_LIST_HEAD(&ch->cmd_wait_list);
2389 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2390
2391 ch->ioctx_ring = (struct srpt_send_ioctx **)
2392 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2393 sizeof(*ch->ioctx_ring[0]),
2394 ch->rsp_size, DMA_TO_DEVICE);
2395 if (!ch->ioctx_ring)
2396 goto free_ch;
2397
2398 INIT_LIST_HEAD(&ch->free_list);
2399 for (i = 0; i < ch->rq_size; i++) {
2400 ch->ioctx_ring[i]->ch = ch;
2401 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2402 }
2403
2404 ret = srpt_create_ch_ib(ch);
2405 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302406 rej->reason = cpu_to_be32(
2407 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002408 pr_err("rejected SRP_LOGIN_REQ because creating"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002409 " a new RDMA channel failed.\n");
2410 goto free_ring;
2411 }
2412
2413 ret = srpt_ch_qp_rtr(ch, ch->qp);
2414 if (ret) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302415 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002416 pr_err("rejected SRP_LOGIN_REQ because enabling"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002417 " RTR failed (error code = %d)\n", ret);
2418 goto destroy_ib;
2419 }
2420 /*
2421 * Use the initator port identifier as the session name.
2422 */
2423 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2424 be64_to_cpu(*(__be64 *)ch->i_port_id),
2425 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2426
2427 pr_debug("registering session %s\n", ch->sess_name);
2428
2429 nacl = srpt_lookup_acl(sport, ch->i_port_id);
2430 if (!nacl) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002431 pr_info("Rejected login because no ACL has been"
2432 " configured yet for initiator %s.\n", ch->sess_name);
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302433 rej->reason = cpu_to_be32(
2434 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002435 goto destroy_ib;
2436 }
2437
Nicholas Bellingere70beee2014-04-02 12:52:38 -07002438 ch->sess = transport_init_session(TARGET_PROT_NORMAL);
Dan Carpenter3af33632011-11-04 21:27:32 +03002439 if (IS_ERR(ch->sess)) {
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302440 rej->reason = cpu_to_be32(
2441 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002442 pr_debug("Failed to create session\n");
2443 goto deregister_session;
2444 }
2445 ch->sess->se_node_acl = &nacl->nacl;
2446 transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2447
2448 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2449 ch->sess_name, ch->cm_id);
2450
2451 /* create srp_login_response */
2452 rsp->opcode = SRP_LOGIN_RSP;
2453 rsp->tag = req->tag;
2454 rsp->max_it_iu_len = req->req_it_iu_len;
2455 rsp->max_ti_iu_len = req->req_it_iu_len;
2456 ch->max_ti_iu_len = it_iu_len;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302457 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2458 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002459 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2460 atomic_set(&ch->req_lim, ch->rq_size);
2461 atomic_set(&ch->req_lim_delta, 0);
2462
2463 /* create cm reply */
2464 rep_param->qp_num = ch->qp->qp_num;
2465 rep_param->private_data = (void *)rsp;
2466 rep_param->private_data_len = sizeof *rsp;
2467 rep_param->rnr_retry_count = 7;
2468 rep_param->flow_control = 1;
2469 rep_param->failover_accepted = 0;
2470 rep_param->srq = 1;
2471 rep_param->responder_resources = 4;
2472 rep_param->initiator_depth = 4;
2473
2474 ret = ib_send_cm_rep(cm_id, rep_param);
2475 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002476 pr_err("sending SRP_LOGIN_REQ response failed"
Bart Van Asschea42d9852011-10-14 01:30:46 +00002477 " (error code = %d)\n", ret);
2478 goto release_channel;
2479 }
2480
2481 spin_lock_irq(&sdev->spinlock);
2482 list_add_tail(&ch->list, &sdev->rch_list);
2483 spin_unlock_irq(&sdev->spinlock);
2484
2485 goto out;
2486
2487release_channel:
2488 srpt_set_ch_state(ch, CH_RELEASING);
2489 transport_deregister_session_configfs(ch->sess);
2490
2491deregister_session:
2492 transport_deregister_session(ch->sess);
2493 ch->sess = NULL;
2494
2495destroy_ib:
2496 srpt_destroy_ch_ib(ch);
2497
2498free_ring:
2499 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2500 ch->sport->sdev, ch->rq_size,
2501 ch->rsp_size, DMA_TO_DEVICE);
2502free_ch:
2503 kfree(ch);
2504
2505reject:
2506 rej->opcode = SRP_LOGIN_REJ;
2507 rej->tag = req->tag;
Vaishali Thakkarb356c1c2015-06-24 10:12:13 +05302508 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2509 | SRP_BUF_FORMAT_INDIRECT);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002510
2511 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2512 (void *)rej, sizeof *rej);
2513
2514out:
2515 kfree(rep_param);
2516 kfree(rsp);
2517 kfree(rej);
2518
2519 return ret;
2520}
2521
2522static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2523{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002524 pr_info("Received IB REJ for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002525 srpt_drain_channel(cm_id);
2526}
2527
2528/**
2529 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2530 *
2531 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2532 * and that the recipient may begin transmitting (RTU = ready to use).
2533 */
2534static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2535{
2536 struct srpt_rdma_ch *ch;
2537 int ret;
2538
2539 ch = srpt_find_channel(cm_id->context, cm_id);
2540 BUG_ON(!ch);
2541
2542 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2543 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2544
2545 ret = srpt_ch_qp_rts(ch, ch->qp);
2546
2547 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2548 wait_list) {
2549 list_del(&ioctx->wait_list);
2550 srpt_handle_new_iu(ch, ioctx, NULL);
2551 }
2552 if (ret)
2553 srpt_close_ch(ch);
2554 }
2555}
2556
2557static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2558{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002559 pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002560 srpt_drain_channel(cm_id);
2561}
2562
2563static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2564{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002565 pr_info("Received IB REP error for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002566 srpt_drain_channel(cm_id);
2567}
2568
2569/**
2570 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2571 */
2572static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2573{
2574 struct srpt_rdma_ch *ch;
2575 unsigned long flags;
2576 bool send_drep = false;
2577
2578 ch = srpt_find_channel(cm_id->context, cm_id);
2579 BUG_ON(!ch);
2580
2581 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2582
2583 spin_lock_irqsave(&ch->spinlock, flags);
2584 switch (ch->state) {
2585 case CH_CONNECTING:
2586 case CH_LIVE:
2587 send_drep = true;
2588 ch->state = CH_DISCONNECTING;
2589 break;
2590 case CH_DISCONNECTING:
2591 case CH_DRAINING:
2592 case CH_RELEASING:
2593 WARN(true, "unexpected channel state %d\n", ch->state);
2594 break;
2595 }
2596 spin_unlock_irqrestore(&ch->spinlock, flags);
2597
2598 if (send_drep) {
2599 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002600 pr_err("Sending IB DREP failed.\n");
2601 pr_info("Received DREQ and sent DREP for session %s.\n",
2602 ch->sess_name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002603 }
2604}
2605
2606/**
2607 * srpt_cm_drep_recv() - Process reception of a DREP message.
2608 */
2609static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2610{
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002611 pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002612 srpt_drain_channel(cm_id);
2613}
2614
2615/**
2616 * srpt_cm_handler() - IB connection manager callback function.
2617 *
2618 * A non-zero return value will cause the caller destroy the CM ID.
2619 *
2620 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2621 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2622 * a non-zero value in any other case will trigger a race with the
2623 * ib_destroy_cm_id() call in srpt_release_channel().
2624 */
2625static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2626{
2627 int ret;
2628
2629 ret = 0;
2630 switch (event->event) {
2631 case IB_CM_REQ_RECEIVED:
2632 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2633 event->private_data);
2634 break;
2635 case IB_CM_REJ_RECEIVED:
2636 srpt_cm_rej_recv(cm_id);
2637 break;
2638 case IB_CM_RTU_RECEIVED:
2639 case IB_CM_USER_ESTABLISHED:
2640 srpt_cm_rtu_recv(cm_id);
2641 break;
2642 case IB_CM_DREQ_RECEIVED:
2643 srpt_cm_dreq_recv(cm_id);
2644 break;
2645 case IB_CM_DREP_RECEIVED:
2646 srpt_cm_drep_recv(cm_id);
2647 break;
2648 case IB_CM_TIMEWAIT_EXIT:
2649 srpt_cm_timewait_exit(cm_id);
2650 break;
2651 case IB_CM_REP_ERROR:
2652 srpt_cm_rep_error(cm_id);
2653 break;
2654 case IB_CM_DREQ_ERROR:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002655 pr_info("Received IB DREQ ERROR event.\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002656 break;
2657 case IB_CM_MRA_RECEIVED:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002658 pr_info("Received IB MRA event\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00002659 break;
2660 default:
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002661 pr_err("received unrecognized IB CM event %d\n", event->event);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002662 break;
2663 }
2664
2665 return ret;
2666}
2667
2668/**
2669 * srpt_perform_rdmas() - Perform IB RDMA.
2670 *
2671 * Returns zero upon success or a negative number upon failure.
2672 */
2673static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2674 struct srpt_send_ioctx *ioctx)
2675{
Bart Van Asschea42d9852011-10-14 01:30:46 +00002676 struct ib_send_wr *bad_wr;
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002677 int sq_wr_avail, ret, i;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002678 enum dma_data_direction dir;
2679 const int n_rdma = ioctx->n_rdma;
2680
2681 dir = ioctx->cmd.data_direction;
2682 if (dir == DMA_TO_DEVICE) {
2683 /* write */
2684 ret = -ENOMEM;
2685 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2686 if (sq_wr_avail < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002687 pr_warn("IB send queue full (needed %d)\n",
2688 n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002689 goto out;
2690 }
2691 }
2692
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002693 for (i = 0; i < n_rdma; i++) {
2694 struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002695
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002696 wr->opcode = (dir == DMA_FROM_DEVICE) ?
2697 IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
2698
2699 if (i == n_rdma - 1) {
2700 /* only get completion event for the last rdma read */
2701 if (dir == DMA_TO_DEVICE) {
2702 wr->send_flags = IB_SEND_SIGNALED;
2703 ioctx->rdma_cqe.done = srpt_rdma_read_done;
2704 } else {
2705 ioctx->rdma_cqe.done = srpt_rdma_write_done;
2706 }
2707 wr->wr_cqe = &ioctx->rdma_cqe;
2708 wr->next = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002709 } else {
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002710 wr->wr_cqe = NULL;
2711 wr->next = &ioctx->rdma_wrs[i + 1].wr;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002712 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00002713 }
2714
Christoph Hellwig59fae4d2015-09-29 13:00:44 +02002715 ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002716 if (ret)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002717 pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002718 __func__, __LINE__, ret, i, n_rdma);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002719out:
2720 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2721 atomic_add(n_rdma, &ch->sq_wr_avail);
2722 return ret;
2723}
2724
2725/**
2726 * srpt_xfer_data() - Start data transfer from initiator to target.
2727 */
2728static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2729 struct srpt_send_ioctx *ioctx)
2730{
2731 int ret;
2732
2733 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2734 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002735 pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002736 goto out;
2737 }
2738
2739 ret = srpt_perform_rdmas(ch, ioctx);
2740 if (ret) {
2741 if (ret == -EAGAIN || ret == -ENOMEM)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002742 pr_info("%s[%d] queue full -- ret=%d\n",
2743 __func__, __LINE__, ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002744 else
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002745 pr_err("%s[%d] fatal error -- ret=%d\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00002746 __func__, __LINE__, ret);
2747 goto out_unmap;
2748 }
2749
2750out:
2751 return ret;
2752out_unmap:
2753 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2754 goto out;
2755}
2756
2757static int srpt_write_pending_status(struct se_cmd *se_cmd)
2758{
2759 struct srpt_send_ioctx *ioctx;
2760
2761 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2762 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2763}
2764
2765/*
2766 * srpt_write_pending() - Start data transfer from initiator to target (write).
2767 */
2768static int srpt_write_pending(struct se_cmd *se_cmd)
2769{
2770 struct srpt_rdma_ch *ch;
2771 struct srpt_send_ioctx *ioctx;
2772 enum srpt_command_state new_state;
2773 enum rdma_ch_state ch_state;
2774 int ret;
2775
2776 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2777
2778 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2779 WARN_ON(new_state == SRPT_STATE_DONE);
2780
2781 ch = ioctx->ch;
2782 BUG_ON(!ch);
2783
2784 ch_state = srpt_get_ch_state(ch);
2785 switch (ch_state) {
2786 case CH_CONNECTING:
2787 WARN(true, "unexpected channel state %d\n", ch_state);
2788 ret = -EINVAL;
2789 goto out;
2790 case CH_LIVE:
2791 break;
2792 case CH_DISCONNECTING:
2793 case CH_DRAINING:
2794 case CH_RELEASING:
2795 pr_debug("cmd with tag %lld: channel disconnecting\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002796 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002797 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2798 ret = -EINVAL;
2799 goto out;
2800 }
2801 ret = srpt_xfer_data(ch, ioctx);
2802
2803out:
2804 return ret;
2805}
2806
2807static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2808{
2809 switch (tcm_mgmt_status) {
2810 case TMR_FUNCTION_COMPLETE:
2811 return SRP_TSK_MGMT_SUCCESS;
2812 case TMR_FUNCTION_REJECTED:
2813 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2814 }
2815 return SRP_TSK_MGMT_FAILED;
2816}
2817
2818/**
2819 * srpt_queue_response() - Transmits the response to a SCSI command.
2820 *
2821 * Callback function called by the TCM core. Must not block since it can be
2822 * invoked on the context of the IB completion handler.
2823 */
Joern Engelb79fafa2013-07-03 11:22:17 -04002824static void srpt_queue_response(struct se_cmd *cmd)
Bart Van Asschea42d9852011-10-14 01:30:46 +00002825{
2826 struct srpt_rdma_ch *ch;
2827 struct srpt_send_ioctx *ioctx;
2828 enum srpt_command_state state;
2829 unsigned long flags;
2830 int ret;
2831 enum dma_data_direction dir;
2832 int resp_len;
2833 u8 srp_tm_status;
2834
Bart Van Asschea42d9852011-10-14 01:30:46 +00002835 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2836 ch = ioctx->ch;
2837 BUG_ON(!ch);
2838
2839 spin_lock_irqsave(&ioctx->spinlock, flags);
2840 state = ioctx->state;
2841 switch (state) {
2842 case SRPT_STATE_NEW:
2843 case SRPT_STATE_DATA_IN:
2844 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2845 break;
2846 case SRPT_STATE_MGMT:
2847 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2848 break;
2849 default:
2850 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2851 ch, ioctx->ioctx.index, ioctx->state);
2852 break;
2853 }
2854 spin_unlock_irqrestore(&ioctx->spinlock, flags);
2855
2856 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2857 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2858 atomic_inc(&ch->req_lim_delta);
2859 srpt_abort_cmd(ioctx);
Joern Engelb79fafa2013-07-03 11:22:17 -04002860 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002861 }
2862
2863 dir = ioctx->cmd.data_direction;
2864
2865 /* For read commands, transfer the data to the initiator. */
2866 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2867 !ioctx->queue_status_only) {
2868 ret = srpt_xfer_data(ch, ioctx);
2869 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002870 pr_err("xfer_data failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002871 ioctx->cmd.tag);
Joern Engelb79fafa2013-07-03 11:22:17 -04002872 return;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002873 }
2874 }
2875
2876 if (state != SRPT_STATE_MGMT)
Bart Van Assche649ee052015-04-14 13:26:44 +02002877 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00002878 cmd->scsi_status);
2879 else {
2880 srp_tm_status
2881 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2882 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
Bart Van Assche649ee052015-04-14 13:26:44 +02002883 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002884 }
2885 ret = srpt_post_send(ch, ioctx, resp_len);
2886 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002887 pr_err("sending cmd response failed for tag %llu\n",
Bart Van Assche649ee052015-04-14 13:26:44 +02002888 ioctx->cmd.tag);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002889 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2890 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Bart Van Asscheafc16602015-04-27 13:52:36 +02002891 target_put_sess_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002892 }
Joern Engelb79fafa2013-07-03 11:22:17 -04002893}
Bart Van Asschea42d9852011-10-14 01:30:46 +00002894
Joern Engelb79fafa2013-07-03 11:22:17 -04002895static int srpt_queue_data_in(struct se_cmd *cmd)
2896{
2897 srpt_queue_response(cmd);
2898 return 0;
2899}
2900
2901static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2902{
2903 srpt_queue_response(cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002904}
2905
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002906static void srpt_aborted_task(struct se_cmd *cmd)
2907{
2908 struct srpt_send_ioctx *ioctx = container_of(cmd,
2909 struct srpt_send_ioctx, cmd);
2910
2911 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
2912}
2913
Bart Van Asschea42d9852011-10-14 01:30:46 +00002914static int srpt_queue_status(struct se_cmd *cmd)
2915{
2916 struct srpt_send_ioctx *ioctx;
2917
2918 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2919 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2920 if (cmd->se_cmd_flags &
2921 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2922 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2923 ioctx->queue_status_only = true;
Joern Engelb79fafa2013-07-03 11:22:17 -04002924 srpt_queue_response(cmd);
2925 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002926}
2927
2928static void srpt_refresh_port_work(struct work_struct *work)
2929{
2930 struct srpt_port *sport = container_of(work, struct srpt_port, work);
2931
2932 srpt_refresh_port(sport);
2933}
2934
2935static int srpt_ch_list_empty(struct srpt_device *sdev)
2936{
2937 int res;
2938
2939 spin_lock_irq(&sdev->spinlock);
2940 res = list_empty(&sdev->rch_list);
2941 spin_unlock_irq(&sdev->spinlock);
2942
2943 return res;
2944}
2945
2946/**
2947 * srpt_release_sdev() - Free the channel resources associated with a target.
2948 */
2949static int srpt_release_sdev(struct srpt_device *sdev)
2950{
2951 struct srpt_rdma_ch *ch, *tmp_ch;
2952 int res;
2953
2954 WARN_ON_ONCE(irqs_disabled());
2955
2956 BUG_ON(!sdev);
2957
2958 spin_lock_irq(&sdev->spinlock);
2959 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
2960 __srpt_close_ch(ch);
2961 spin_unlock_irq(&sdev->spinlock);
2962
2963 res = wait_event_interruptible(sdev->ch_releaseQ,
2964 srpt_ch_list_empty(sdev));
2965 if (res)
Doug Ledford9f5d32a2014-10-20 18:25:15 -04002966 pr_err("%s: interrupted.\n", __func__);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002967
2968 return 0;
2969}
2970
2971static struct srpt_port *__srpt_lookup_port(const char *name)
2972{
2973 struct ib_device *dev;
2974 struct srpt_device *sdev;
2975 struct srpt_port *sport;
2976 int i;
2977
2978 list_for_each_entry(sdev, &srpt_dev_list, list) {
2979 dev = sdev->device;
2980 if (!dev)
2981 continue;
2982
2983 for (i = 0; i < dev->phys_port_cnt; i++) {
2984 sport = &sdev->port[i];
2985
2986 if (!strcmp(sport->port_guid, name))
2987 return sport;
2988 }
2989 }
2990
2991 return NULL;
2992}
2993
2994static struct srpt_port *srpt_lookup_port(const char *name)
2995{
2996 struct srpt_port *sport;
2997
2998 spin_lock(&srpt_dev_lock);
2999 sport = __srpt_lookup_port(name);
3000 spin_unlock(&srpt_dev_lock);
3001
3002 return sport;
3003}
3004
3005/**
3006 * srpt_add_one() - Infiniband device addition callback function.
3007 */
3008static void srpt_add_one(struct ib_device *device)
3009{
3010 struct srpt_device *sdev;
3011 struct srpt_port *sport;
3012 struct ib_srq_init_attr srq_attr;
3013 int i;
3014
3015 pr_debug("device = %p, device->dma_ops = %p\n", device,
3016 device->dma_ops);
3017
3018 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3019 if (!sdev)
3020 goto err;
3021
3022 sdev->device = device;
3023 INIT_LIST_HEAD(&sdev->rch_list);
3024 init_waitqueue_head(&sdev->ch_releaseQ);
3025 spin_lock_init(&sdev->spinlock);
3026
3027 if (ib_query_device(device, &sdev->dev_attr))
3028 goto free_dev;
3029
3030 sdev->pd = ib_alloc_pd(device);
3031 if (IS_ERR(sdev->pd))
3032 goto free_dev;
3033
Bart Van Asschea42d9852011-10-14 01:30:46 +00003034 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3035
3036 srq_attr.event_handler = srpt_srq_event;
3037 srq_attr.srq_context = (void *)sdev;
3038 srq_attr.attr.max_wr = sdev->srq_size;
3039 srq_attr.attr.max_sge = 1;
3040 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07003041 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003042
3043 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3044 if (IS_ERR(sdev->srq))
Jason Gunthorpe5a783952015-07-30 17:22:24 -06003045 goto err_pd;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003046
3047 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3048 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3049 device->name);
3050
3051 if (!srpt_service_guid)
3052 srpt_service_guid = be64_to_cpu(device->node_guid);
3053
3054 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3055 if (IS_ERR(sdev->cm_id))
3056 goto err_srq;
3057
3058 /* print out target login information */
3059 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3060 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3061 srpt_service_guid, srpt_service_guid);
3062
3063 /*
3064 * We do not have a consistent service_id (ie. also id_ext of target_id)
3065 * to identify this target. We currently use the guid of the first HCA
3066 * in the system as service_id; therefore, the target_id will change
3067 * if this HCA is gone bad and replaced by different HCA
3068 */
Haggai Eran73fec7f2015-07-30 17:50:26 +03003069 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
Bart Van Asschea42d9852011-10-14 01:30:46 +00003070 goto err_cm;
3071
3072 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3073 srpt_event_handler);
3074 if (ib_register_event_handler(&sdev->event_handler))
3075 goto err_cm;
3076
3077 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3078 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3079 sizeof(*sdev->ioctx_ring[0]),
3080 srp_max_req_size, DMA_FROM_DEVICE);
3081 if (!sdev->ioctx_ring)
3082 goto err_event;
3083
3084 for (i = 0; i < sdev->srq_size; ++i)
3085 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3086
Roland Dreierf2250662012-02-02 12:55:58 -08003087 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00003088
3089 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3090 sport = &sdev->port[i - 1];
3091 sport->sdev = sdev;
3092 sport->port = i;
3093 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3094 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3095 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3096 INIT_WORK(&sport->work, srpt_refresh_port_work);
3097 INIT_LIST_HEAD(&sport->port_acl_list);
3098 spin_lock_init(&sport->port_acl_lock);
3099
3100 if (srpt_refresh_port(sport)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003101 pr_err("MAD registration failed for %s-%d.\n",
Bart Van Asschea42d9852011-10-14 01:30:46 +00003102 srpt_sdev_name(sdev), i);
3103 goto err_ring;
3104 }
3105 snprintf(sport->port_guid, sizeof(sport->port_guid),
3106 "0x%016llx%016llx",
3107 be64_to_cpu(sport->gid.global.subnet_prefix),
3108 be64_to_cpu(sport->gid.global.interface_id));
3109 }
3110
3111 spin_lock(&srpt_dev_lock);
3112 list_add_tail(&sdev->list, &srpt_dev_list);
3113 spin_unlock(&srpt_dev_lock);
3114
3115out:
3116 ib_set_client_data(device, &srpt_client, sdev);
3117 pr_debug("added %s.\n", device->name);
3118 return;
3119
3120err_ring:
3121 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3122 sdev->srq_size, srp_max_req_size,
3123 DMA_FROM_DEVICE);
3124err_event:
3125 ib_unregister_event_handler(&sdev->event_handler);
3126err_cm:
3127 ib_destroy_cm_id(sdev->cm_id);
3128err_srq:
3129 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003130err_pd:
3131 ib_dealloc_pd(sdev->pd);
3132free_dev:
3133 kfree(sdev);
3134err:
3135 sdev = NULL;
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003136 pr_info("%s(%s) failed.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003137 goto out;
3138}
3139
3140/**
3141 * srpt_remove_one() - InfiniBand device removal callback function.
3142 */
Haggai Eran7c1eb452015-07-30 17:50:14 +03003143static void srpt_remove_one(struct ib_device *device, void *client_data)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003144{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003145 struct srpt_device *sdev = client_data;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003146 int i;
3147
Bart Van Asschea42d9852011-10-14 01:30:46 +00003148 if (!sdev) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003149 pr_info("%s(%s): nothing to do.\n", __func__, device->name);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003150 return;
3151 }
3152
3153 srpt_unregister_mad_agent(sdev);
3154
3155 ib_unregister_event_handler(&sdev->event_handler);
3156
3157 /* Cancel any work queued by the just unregistered IB event handler. */
3158 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3159 cancel_work_sync(&sdev->port[i].work);
3160
3161 ib_destroy_cm_id(sdev->cm_id);
3162
3163 /*
3164 * Unregistering a target must happen after destroying sdev->cm_id
3165 * such that no new SRP_LOGIN_REQ information units can arrive while
3166 * destroying the target.
3167 */
3168 spin_lock(&srpt_dev_lock);
3169 list_del(&sdev->list);
3170 spin_unlock(&srpt_dev_lock);
3171 srpt_release_sdev(sdev);
3172
3173 ib_destroy_srq(sdev->srq);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003174 ib_dealloc_pd(sdev->pd);
3175
3176 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3177 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3178 sdev->ioctx_ring = NULL;
3179 kfree(sdev);
3180}
3181
3182static struct ib_client srpt_client = {
3183 .name = DRV_NAME,
3184 .add = srpt_add_one,
3185 .remove = srpt_remove_one
3186};
3187
3188static int srpt_check_true(struct se_portal_group *se_tpg)
3189{
3190 return 1;
3191}
3192
3193static int srpt_check_false(struct se_portal_group *se_tpg)
3194{
3195 return 0;
3196}
3197
3198static char *srpt_get_fabric_name(void)
3199{
3200 return "srpt";
3201}
3202
Bart Van Asschea42d9852011-10-14 01:30:46 +00003203static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3204{
3205 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3206
3207 return sport->port_guid;
3208}
3209
3210static u16 srpt_get_tag(struct se_portal_group *tpg)
3211{
3212 return 1;
3213}
3214
Bart Van Asschea42d9852011-10-14 01:30:46 +00003215static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3216{
3217 return 1;
3218}
3219
3220static void srpt_release_cmd(struct se_cmd *se_cmd)
3221{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003222 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3223 struct srpt_send_ioctx, cmd);
3224 struct srpt_rdma_ch *ch = ioctx->ch;
3225 unsigned long flags;
3226
3227 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3228 WARN_ON(ioctx->mapped_sg_count != 0);
3229
3230 if (ioctx->n_rbuf > 1) {
3231 kfree(ioctx->rbufs);
3232 ioctx->rbufs = NULL;
3233 ioctx->n_rbuf = 0;
3234 }
3235
3236 spin_lock_irqsave(&ch->spinlock, flags);
3237 list_add(&ioctx->free_list, &ch->free_list);
3238 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003239}
3240
3241/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003242 * srpt_close_session() - Forcibly close a session.
3243 *
3244 * Callback function invoked by the TCM core to clean up sessions associated
3245 * with a node ACL when the user invokes
3246 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3247 */
3248static void srpt_close_session(struct se_session *se_sess)
3249{
3250 DECLARE_COMPLETION_ONSTACK(release_done);
3251 struct srpt_rdma_ch *ch;
3252 struct srpt_device *sdev;
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003253 unsigned long res;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003254
3255 ch = se_sess->fabric_sess_ptr;
3256 WARN_ON(ch->sess != se_sess);
3257
3258 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3259
3260 sdev = ch->sport->sdev;
3261 spin_lock_irq(&sdev->spinlock);
3262 BUG_ON(ch->release_done);
3263 ch->release_done = &release_done;
3264 __srpt_close_ch(ch);
3265 spin_unlock_irq(&sdev->spinlock);
3266
3267 res = wait_for_completion_timeout(&release_done, 60 * HZ);
Nicholas Mc Guireecc3f3e2015-01-16 12:20:17 +01003268 WARN_ON(res == 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003269}
3270
3271/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003272 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3273 *
3274 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3275 * This object represents an arbitrary integer used to uniquely identify a
3276 * particular attached remote initiator port to a particular SCSI target port
3277 * within a particular SCSI target device within a particular SCSI instance.
3278 */
3279static u32 srpt_sess_get_index(struct se_session *se_sess)
3280{
3281 return 0;
3282}
3283
3284static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3285{
3286}
3287
Bart Van Asschea42d9852011-10-14 01:30:46 +00003288/* Note: only used from inside debug printk's by the TCM core. */
3289static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3290{
3291 struct srpt_send_ioctx *ioctx;
3292
3293 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3294 return srpt_get_cmd_state(ioctx);
3295}
3296
Bart Van Asschea42d9852011-10-14 01:30:46 +00003297/**
3298 * srpt_parse_i_port_id() - Parse an initiator port ID.
3299 * @name: ASCII representation of a 128-bit initiator port ID.
3300 * @i_port_id: Binary 128-bit port ID.
3301 */
3302static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3303{
3304 const char *p;
3305 unsigned len, count, leading_zero_bytes;
3306 int ret, rc;
3307
3308 p = name;
Rasmus Villemoesb60459f2014-10-13 15:54:46 -07003309 if (strncasecmp(p, "0x", 2) == 0)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003310 p += 2;
3311 ret = -EINVAL;
3312 len = strlen(p);
3313 if (len % 2)
3314 goto out;
3315 count = min(len / 2, 16U);
3316 leading_zero_bytes = 16 - count;
3317 memset(i_port_id, 0, leading_zero_bytes);
3318 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3319 if (rc < 0)
3320 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3321 ret = 0;
3322out:
3323 return ret;
3324}
3325
3326/*
3327 * configfs callback function invoked for
3328 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3329 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003330static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003331{
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003332 struct srpt_port *sport =
3333 container_of(se_nacl->se_tpg, struct srpt_port, port_tpg_1);
3334 struct srpt_node_acl *nacl =
3335 container_of(se_nacl, struct srpt_node_acl, nacl);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003336 u8 i_port_id[16];
3337
3338 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003339 pr_err("invalid initiator port ID %s\n", name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003340 return -EINVAL;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003341 }
3342
Bart Van Asschea42d9852011-10-14 01:30:46 +00003343 memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3344 nacl->sport = sport;
3345
3346 spin_lock_irq(&sport->port_acl_lock);
3347 list_add_tail(&nacl->list, &sport->port_acl_list);
3348 spin_unlock_irq(&sport->port_acl_lock);
3349
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003350 return 0;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003351}
3352
3353/*
3354 * configfs callback function invoked for
3355 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3356 */
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003357static void srpt_cleanup_nodeacl(struct se_node_acl *se_nacl)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003358{
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003359 struct srpt_node_acl *nacl =
3360 container_of(se_nacl, struct srpt_node_acl, nacl);
3361 struct srpt_port *sport = nacl->sport;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003362
Bart Van Asschea42d9852011-10-14 01:30:46 +00003363 spin_lock_irq(&sport->port_acl_lock);
3364 list_del(&nacl->list);
3365 spin_unlock_irq(&sport->port_acl_lock);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003366}
3367
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003368static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3369 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003370{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003371 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003372 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3373
3374 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3375}
3376
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003377static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3378 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003379{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003380 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003381 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3382 unsigned long val;
3383 int ret;
3384
Jingoo Han9d8abf42014-02-05 11:22:05 +09003385 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003386 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003387 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003388 return -EINVAL;
3389 }
3390 if (val > MAX_SRPT_RDMA_SIZE) {
3391 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3392 MAX_SRPT_RDMA_SIZE);
3393 return -EINVAL;
3394 }
3395 if (val < DEFAULT_MAX_RDMA_SIZE) {
3396 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3397 val, DEFAULT_MAX_RDMA_SIZE);
3398 return -EINVAL;
3399 }
3400 sport->port_attrib.srp_max_rdma_size = val;
3401
3402 return count;
3403}
3404
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003405static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3406 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003407{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003408 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003409 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3410
3411 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3412}
3413
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003414static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3415 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003416{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003417 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003418 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3419 unsigned long val;
3420 int ret;
3421
Jingoo Han9d8abf42014-02-05 11:22:05 +09003422 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003423 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003424 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003425 return -EINVAL;
3426 }
3427 if (val > MAX_SRPT_RSP_SIZE) {
3428 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3429 MAX_SRPT_RSP_SIZE);
3430 return -EINVAL;
3431 }
3432 if (val < MIN_MAX_RSP_SIZE) {
3433 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3434 MIN_MAX_RSP_SIZE);
3435 return -EINVAL;
3436 }
3437 sport->port_attrib.srp_max_rsp_size = val;
3438
3439 return count;
3440}
3441
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003442static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3443 char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003444{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003445 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003446 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3447
3448 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3449}
3450
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003451static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3452 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003453{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003454 struct se_portal_group *se_tpg = attrib_to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003455 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3456 unsigned long val;
3457 int ret;
3458
Jingoo Han9d8abf42014-02-05 11:22:05 +09003459 ret = kstrtoul(page, 0, &val);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003460 if (ret < 0) {
Jingoo Han9d8abf42014-02-05 11:22:05 +09003461 pr_err("kstrtoul() failed with ret: %d\n", ret);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003462 return -EINVAL;
3463 }
3464 if (val > MAX_SRPT_SRQ_SIZE) {
3465 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3466 MAX_SRPT_SRQ_SIZE);
3467 return -EINVAL;
3468 }
3469 if (val < MIN_SRPT_SRQ_SIZE) {
3470 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3471 MIN_SRPT_SRQ_SIZE);
3472 return -EINVAL;
3473 }
3474 sport->port_attrib.srp_sq_size = val;
3475
3476 return count;
3477}
3478
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003479CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
3480CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
3481CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003482
3483static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003484 &srpt_tpg_attrib_attr_srp_max_rdma_size,
3485 &srpt_tpg_attrib_attr_srp_max_rsp_size,
3486 &srpt_tpg_attrib_attr_srp_sq_size,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003487 NULL,
3488};
3489
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003490static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003491{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003492 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003493 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3494
3495 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3496}
3497
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003498static ssize_t srpt_tpg_enable_store(struct config_item *item,
3499 const char *page, size_t count)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003500{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003501 struct se_portal_group *se_tpg = to_tpg(item);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003502 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3503 unsigned long tmp;
3504 int ret;
3505
Jingoo Han9d8abf42014-02-05 11:22:05 +09003506 ret = kstrtoul(page, 0, &tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003507 if (ret < 0) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003508 pr_err("Unable to extract srpt_tpg_store_enable\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003509 return -EINVAL;
3510 }
3511
3512 if ((tmp != 0) && (tmp != 1)) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003513 pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003514 return -EINVAL;
3515 }
3516 if (tmp == 1)
3517 sport->enabled = true;
3518 else
3519 sport->enabled = false;
3520
3521 return count;
3522}
3523
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003524CONFIGFS_ATTR(srpt_tpg_, enable);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003525
3526static struct configfs_attribute *srpt_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003527 &srpt_tpg_attr_enable,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003528 NULL,
3529};
3530
3531/**
3532 * configfs callback invoked for
3533 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3534 */
3535static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3536 struct config_group *group,
3537 const char *name)
3538{
3539 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3540 int res;
3541
3542 /* Initialize sport->port_wwn and sport->port_tpg_1 */
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07003543 res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003544 if (res)
3545 return ERR_PTR(res);
3546
3547 return &sport->port_tpg_1;
3548}
3549
3550/**
3551 * configfs callback invoked for
3552 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3553 */
3554static void srpt_drop_tpg(struct se_portal_group *tpg)
3555{
3556 struct srpt_port *sport = container_of(tpg,
3557 struct srpt_port, port_tpg_1);
3558
3559 sport->enabled = false;
3560 core_tpg_deregister(&sport->port_tpg_1);
3561}
3562
3563/**
3564 * configfs callback invoked for
3565 * mkdir /sys/kernel/config/target/$driver/$port
3566 */
3567static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3568 struct config_group *group,
3569 const char *name)
3570{
3571 struct srpt_port *sport;
3572 int ret;
3573
3574 sport = srpt_lookup_port(name);
3575 pr_debug("make_tport(%s)\n", name);
3576 ret = -EINVAL;
3577 if (!sport)
3578 goto err;
3579
3580 return &sport->port_wwn;
3581
3582err:
3583 return ERR_PTR(ret);
3584}
3585
3586/**
3587 * configfs callback invoked for
3588 * rmdir /sys/kernel/config/target/$driver/$port
3589 */
3590static void srpt_drop_tport(struct se_wwn *wwn)
3591{
3592 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3593
3594 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3595}
3596
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003597static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003598{
3599 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3600}
3601
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003602CONFIGFS_ATTR_RO(srpt_wwn_, version);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003603
3604static struct configfs_attribute *srpt_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003605 &srpt_wwn_attr_version,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003606 NULL,
3607};
3608
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003609static const struct target_core_fabric_ops srpt_template = {
3610 .module = THIS_MODULE,
3611 .name = "srpt",
Christoph Hellwig144bc4c2015-04-13 19:51:16 +02003612 .node_acl_size = sizeof(struct srpt_node_acl),
Bart Van Asschea42d9852011-10-14 01:30:46 +00003613 .get_fabric_name = srpt_get_fabric_name,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003614 .tpg_get_wwn = srpt_get_fabric_wwn,
3615 .tpg_get_tag = srpt_get_tag,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003616 .tpg_check_demo_mode = srpt_check_false,
3617 .tpg_check_demo_mode_cache = srpt_check_true,
3618 .tpg_check_demo_mode_write_protect = srpt_check_true,
3619 .tpg_check_prod_mode_write_protect = srpt_check_false,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003620 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3621 .release_cmd = srpt_release_cmd,
3622 .check_stop_free = srpt_check_stop_free,
3623 .shutdown_session = srpt_shutdown_session,
3624 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003625 .sess_get_index = srpt_sess_get_index,
3626 .sess_get_initiator_sid = NULL,
3627 .write_pending = srpt_write_pending,
3628 .write_pending_status = srpt_write_pending_status,
3629 .set_default_node_attributes = srpt_set_default_node_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003630 .get_cmd_state = srpt_get_tcm_cmd_state,
Joern Engelb79fafa2013-07-03 11:22:17 -04003631 .queue_data_in = srpt_queue_data_in,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003632 .queue_status = srpt_queue_status,
Joern Engelb79fafa2013-07-03 11:22:17 -04003633 .queue_tm_rsp = srpt_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07003634 .aborted_task = srpt_aborted_task,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003635 /*
3636 * Setup function pointers for generic logic in
3637 * target_core_fabric_configfs.c
3638 */
3639 .fabric_make_wwn = srpt_make_tport,
3640 .fabric_drop_wwn = srpt_drop_tport,
3641 .fabric_make_tpg = srpt_make_tpg,
3642 .fabric_drop_tpg = srpt_drop_tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +02003643 .fabric_init_nodeacl = srpt_init_nodeacl,
3644 .fabric_cleanup_nodeacl = srpt_cleanup_nodeacl,
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003645
3646 .tfc_wwn_attrs = srpt_wwn_attrs,
3647 .tfc_tpg_base_attrs = srpt_tpg_attrs,
3648 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003649};
3650
3651/**
3652 * srpt_init_module() - Kernel module initialization.
3653 *
3654 * Note: Since ib_register_client() registers callback functions, and since at
3655 * least one of these callback functions (srpt_add_one()) calls target core
3656 * functions, this driver must be registered with the target core before
3657 * ib_register_client() is called.
3658 */
3659static int __init srpt_init_module(void)
3660{
3661 int ret;
3662
3663 ret = -EINVAL;
3664 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003665 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003666 " srp_max_req_size -- must be at least %d.\n",
3667 srp_max_req_size, MIN_MAX_REQ_SIZE);
3668 goto out;
3669 }
3670
3671 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3672 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003673 pr_err("invalid value %d for kernel module parameter"
Bart Van Asschea42d9852011-10-14 01:30:46 +00003674 " srpt_srq_size -- must be in the range [%d..%d].\n",
3675 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3676 goto out;
3677 }
3678
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003679 ret = target_register_template(&srpt_template);
3680 if (ret)
Bart Van Asschea42d9852011-10-14 01:30:46 +00003681 goto out;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003682
3683 ret = ib_register_client(&srpt_client);
3684 if (ret) {
Doug Ledford9f5d32a2014-10-20 18:25:15 -04003685 pr_err("couldn't register IB client\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003686 goto out_unregister_target;
3687 }
3688
3689 return 0;
3690
3691out_unregister_target:
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003692 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003693out:
3694 return ret;
3695}
3696
3697static void __exit srpt_cleanup_module(void)
3698{
3699 ib_unregister_client(&srpt_client);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003700 target_unregister_template(&srpt_template);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003701}
3702
3703module_init(srpt_init_module);
3704module_exit(srpt_cleanup_module);