blob: 77b6368fec3d06b439ebb7a6d99e05fc2ebf4380 [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>
44#include <scsi/scsi_tcq.h>
45#include <target/configfs_macros.h>
46#include <target/target_core_base.h>
47#include <target/target_core_fabric_configfs.h>
48#include <target/target_core_fabric.h>
49#include <target/target_core_configfs.h>
50#include "ib_srpt.h"
51
52/* Name of this kernel module. */
53#define DRV_NAME "ib_srpt"
54#define DRV_VERSION "2.0.0"
55#define DRV_RELDATE "2011-02-14"
56
57#define SRPT_ID_STRING "Linux SRP target"
58
59#undef pr_fmt
60#define pr_fmt(fmt) DRV_NAME " " fmt
61
62MODULE_AUTHOR("Vu Pham and Bart Van Assche");
63MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
64 "v" DRV_VERSION " (" DRV_RELDATE ")");
65MODULE_LICENSE("Dual BSD/GPL");
66
67/*
68 * Global Variables
69 */
70
71static u64 srpt_service_guid;
Roland Dreier486d8b92012-02-02 12:55:58 -080072static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
73static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
Bart Van Asschea42d9852011-10-14 01:30:46 +000074
75static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
76module_param(srp_max_req_size, int, 0444);
77MODULE_PARM_DESC(srp_max_req_size,
78 "Maximum size of SRP request messages in bytes.");
79
80static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
81module_param(srpt_srq_size, int, 0444);
82MODULE_PARM_DESC(srpt_srq_size,
83 "Shared receive queue (SRQ) size.");
84
85static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
86{
87 return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
88}
89module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
90 0444);
91MODULE_PARM_DESC(srpt_service_guid,
92 "Using this value for ioc_guid, id_ext, and cm_listen_id"
93 " instead of using the node_guid of the first HCA.");
94
95static struct ib_client srpt_client;
96static struct target_fabric_configfs *srpt_target;
97static void srpt_release_channel(struct srpt_rdma_ch *ch);
98static int srpt_queue_status(struct se_cmd *cmd);
99
100/**
101 * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
102 */
103static inline
104enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
105{
106 switch (dir) {
107 case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
108 case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
109 default: return dir;
110 }
111}
112
113/**
114 * srpt_sdev_name() - Return the name associated with the HCA.
115 *
116 * Examples are ib0, ib1, ...
117 */
118static inline const char *srpt_sdev_name(struct srpt_device *sdev)
119{
120 return sdev->device->name;
121}
122
123static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
124{
125 unsigned long flags;
126 enum rdma_ch_state state;
127
128 spin_lock_irqsave(&ch->spinlock, flags);
129 state = ch->state;
130 spin_unlock_irqrestore(&ch->spinlock, flags);
131 return state;
132}
133
134static enum rdma_ch_state
135srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
136{
137 unsigned long flags;
138 enum rdma_ch_state prev;
139
140 spin_lock_irqsave(&ch->spinlock, flags);
141 prev = ch->state;
142 ch->state = new_state;
143 spin_unlock_irqrestore(&ch->spinlock, flags);
144 return prev;
145}
146
147/**
148 * srpt_test_and_set_ch_state() - Test and set the channel state.
149 *
150 * Returns true if and only if the channel state has been set to the new state.
151 */
152static bool
153srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
154 enum rdma_ch_state new)
155{
156 unsigned long flags;
157 enum rdma_ch_state prev;
158
159 spin_lock_irqsave(&ch->spinlock, flags);
160 prev = ch->state;
161 if (prev == old)
162 ch->state = new;
163 spin_unlock_irqrestore(&ch->spinlock, flags);
164 return prev == old;
165}
166
167/**
168 * srpt_event_handler() - Asynchronous IB event callback function.
169 *
170 * Callback function called by the InfiniBand core when an asynchronous IB
171 * event occurs. This callback may occur in interrupt context. See also
172 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
173 * Architecture Specification.
174 */
175static void srpt_event_handler(struct ib_event_handler *handler,
176 struct ib_event *event)
177{
178 struct srpt_device *sdev;
179 struct srpt_port *sport;
180
181 sdev = ib_get_client_data(event->device, &srpt_client);
182 if (!sdev || sdev->device != event->device)
183 return;
184
185 pr_debug("ASYNC event= %d on device= %s\n", event->event,
186 srpt_sdev_name(sdev));
187
188 switch (event->event) {
189 case IB_EVENT_PORT_ERR:
190 if (event->element.port_num <= sdev->device->phys_port_cnt) {
191 sport = &sdev->port[event->element.port_num - 1];
192 sport->lid = 0;
193 sport->sm_lid = 0;
194 }
195 break;
196 case IB_EVENT_PORT_ACTIVE:
197 case IB_EVENT_LID_CHANGE:
198 case IB_EVENT_PKEY_CHANGE:
199 case IB_EVENT_SM_CHANGE:
200 case IB_EVENT_CLIENT_REREGISTER:
201 /* 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:
209 printk(KERN_ERR "received unrecognized IB event %d\n",
210 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{
220 printk(KERN_INFO "SRQ event %d\n", event->event);
221}
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:
244 printk(KERN_ERR "received unrecognized IB QP event %d\n",
245 event->event);
246 break;
247 }
248}
249
250/**
251 * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
252 *
253 * @slot: one-based slot number.
254 * @value: four-bit value.
255 *
256 * Copies the lowest four bits of value in element slot of the array of four
257 * bit elements called c_list (controller list). The index slot is one-based.
258 */
259static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
260{
261 u16 id;
262 u8 tmp;
263
264 id = (slot - 1) / 2;
265 if (slot & 0x1) {
266 tmp = c_list[id] & 0xf;
267 c_list[id] = (value << 4) | tmp;
268 } else {
269 tmp = c_list[id] & 0xf0;
270 c_list[id] = (value & 0xf) | tmp;
271 }
272}
273
274/**
275 * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
276 *
277 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
278 * Specification.
279 */
280static void srpt_get_class_port_info(struct ib_dm_mad *mad)
281{
282 struct ib_class_port_info *cif;
283
284 cif = (struct ib_class_port_info *)mad->data;
285 memset(cif, 0, sizeof *cif);
286 cif->base_version = 1;
287 cif->class_version = 1;
288 cif->resp_time_value = 20;
289
290 mad->mad_hdr.status = 0;
291}
292
293/**
294 * srpt_get_iou() - Write IOUnitInfo to a management datagram.
295 *
296 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
297 * Specification. See also section B.7, table B.6 in the SRP r16a document.
298 */
299static void srpt_get_iou(struct ib_dm_mad *mad)
300{
301 struct ib_dm_iou_info *ioui;
302 u8 slot;
303 int i;
304
305 ioui = (struct ib_dm_iou_info *)mad->data;
306 ioui->change_id = __constant_cpu_to_be16(1);
307 ioui->max_controllers = 16;
308
309 /* set present for slot 1 and empty for the rest */
310 srpt_set_ioc(ioui->controller_list, 1, 1);
311 for (i = 1, slot = 2; i < 16; i++, slot++)
312 srpt_set_ioc(ioui->controller_list, slot, 0);
313
314 mad->mad_hdr.status = 0;
315}
316
317/**
318 * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
319 *
320 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
321 * Architecture Specification. See also section B.7, table B.7 in the SRP
322 * r16a document.
323 */
324static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
325 struct ib_dm_mad *mad)
326{
327 struct srpt_device *sdev = sport->sdev;
328 struct ib_dm_ioc_profile *iocp;
329
330 iocp = (struct ib_dm_ioc_profile *)mad->data;
331
332 if (!slot || slot > 16) {
333 mad->mad_hdr.status
334 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
335 return;
336 }
337
338 if (slot > 2) {
339 mad->mad_hdr.status
340 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
341 return;
342 }
343
344 memset(iocp, 0, sizeof *iocp);
345 strcpy(iocp->id_string, SRPT_ID_STRING);
346 iocp->guid = cpu_to_be64(srpt_service_guid);
347 iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
348 iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
349 iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
350 iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
351 iocp->subsys_device_id = 0x0;
352 iocp->io_class = __constant_cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
353 iocp->io_subclass = __constant_cpu_to_be16(SRP_IO_SUBCLASS);
354 iocp->protocol = __constant_cpu_to_be16(SRP_PROTOCOL);
355 iocp->protocol_version = __constant_cpu_to_be16(SRP_PROTOCOL_VERSION);
356 iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
357 iocp->rdma_read_depth = 4;
358 iocp->send_size = cpu_to_be32(srp_max_req_size);
359 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
360 1U << 24));
361 iocp->num_svc_entries = 1;
362 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
363 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
364
365 mad->mad_hdr.status = 0;
366}
367
368/**
369 * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
370 *
371 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
372 * Specification. See also section B.7, table B.8 in the SRP r16a document.
373 */
374static void srpt_get_svc_entries(u64 ioc_guid,
375 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
376{
377 struct ib_dm_svc_entries *svc_entries;
378
379 WARN_ON(!ioc_guid);
380
381 if (!slot || slot > 16) {
382 mad->mad_hdr.status
383 = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
384 return;
385 }
386
387 if (slot > 2 || lo > hi || hi > 1) {
388 mad->mad_hdr.status
389 = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
390 return;
391 }
392
393 svc_entries = (struct ib_dm_svc_entries *)mad->data;
394 memset(svc_entries, 0, sizeof *svc_entries);
395 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
396 snprintf(svc_entries->service_entries[0].name,
397 sizeof(svc_entries->service_entries[0].name),
398 "%s%016llx",
399 SRP_SERVICE_NAME_PREFIX,
400 ioc_guid);
401
402 mad->mad_hdr.status = 0;
403}
404
405/**
406 * srpt_mgmt_method_get() - Process a received management datagram.
407 * @sp: source port through which the MAD has been received.
408 * @rq_mad: received MAD.
409 * @rsp_mad: response MAD.
410 */
411static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
412 struct ib_dm_mad *rsp_mad)
413{
414 u16 attr_id;
415 u32 slot;
416 u8 hi, lo;
417
418 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
419 switch (attr_id) {
420 case DM_ATTR_CLASS_PORT_INFO:
421 srpt_get_class_port_info(rsp_mad);
422 break;
423 case DM_ATTR_IOU_INFO:
424 srpt_get_iou(rsp_mad);
425 break;
426 case DM_ATTR_IOC_PROFILE:
427 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
428 srpt_get_ioc(sp, slot, rsp_mad);
429 break;
430 case DM_ATTR_SVC_ENTRIES:
431 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
432 hi = (u8) ((slot >> 8) & 0xff);
433 lo = (u8) (slot & 0xff);
434 slot = (u16) ((slot >> 16) & 0xffff);
435 srpt_get_svc_entries(srpt_service_guid,
436 slot, hi, lo, rsp_mad);
437 break;
438 default:
439 rsp_mad->mad_hdr.status =
440 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
441 break;
442 }
443}
444
445/**
446 * srpt_mad_send_handler() - Post MAD-send callback function.
447 */
448static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
449 struct ib_mad_send_wc *mad_wc)
450{
451 ib_destroy_ah(mad_wc->send_buf->ah);
452 ib_free_send_mad(mad_wc->send_buf);
453}
454
455/**
456 * srpt_mad_recv_handler() - MAD reception callback function.
457 */
458static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
459 struct ib_mad_recv_wc *mad_wc)
460{
461 struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
462 struct ib_ah *ah;
463 struct ib_mad_send_buf *rsp;
464 struct ib_dm_mad *dm_mad;
465
466 if (!mad_wc || !mad_wc->recv_buf.mad)
467 return;
468
469 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
470 mad_wc->recv_buf.grh, mad_agent->port_num);
471 if (IS_ERR(ah))
472 goto err;
473
474 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
475
476 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
477 mad_wc->wc->pkey_index, 0,
478 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
479 GFP_KERNEL);
480 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 =
496 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
497 break;
498 default:
499 dm_mad->mad_hdr.status =
500 __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
501 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
549 ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
550 if (ret)
551 goto err_query_port;
552
553 if (!sport->mad_agent) {
554 memset(&reg_req, 0, sizeof reg_req);
555 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
556 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
557 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
558 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
559
560 sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
561 sport->port,
562 IB_QPT_GSI,
563 &reg_req, 0,
564 srpt_mad_send_handler,
565 srpt_mad_recv_handler,
566 sport);
567 if (IS_ERR(sport->mad_agent)) {
568 ret = PTR_ERR(sport->mad_agent);
569 sport->mad_agent = NULL;
570 goto err_query_port;
571 }
572 }
573
574 return 0;
575
576err_query_port:
577
578 port_modify.set_port_cap_mask = 0;
579 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
580 ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
581
582err_mod_port:
583
584 return ret;
585}
586
587/**
588 * srpt_unregister_mad_agent() - Unregister MAD callback functions.
589 *
590 * Note: It is safe to call this function more than once for the same device.
591 */
592static void srpt_unregister_mad_agent(struct srpt_device *sdev)
593{
594 struct ib_port_modify port_modify = {
595 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
596 };
597 struct srpt_port *sport;
598 int i;
599
600 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
601 sport = &sdev->port[i - 1];
602 WARN_ON(sport->port != i);
603 if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
604 printk(KERN_ERR "disabling MAD processing failed.\n");
605 if (sport->mad_agent) {
606 ib_unregister_mad_agent(sport->mad_agent);
607 sport->mad_agent = NULL;
608 }
609 }
610}
611
612/**
613 * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
614 */
615static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
616 int ioctx_size, int dma_size,
617 enum dma_data_direction dir)
618{
619 struct srpt_ioctx *ioctx;
620
621 ioctx = kmalloc(ioctx_size, GFP_KERNEL);
622 if (!ioctx)
623 goto err;
624
625 ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
626 if (!ioctx->buf)
627 goto err_free_ioctx;
628
629 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
630 if (ib_dma_mapping_error(sdev->device, ioctx->dma))
631 goto err_free_buf;
632
633 return ioctx;
634
635err_free_buf:
636 kfree(ioctx->buf);
637err_free_ioctx:
638 kfree(ioctx);
639err:
640 return NULL;
641}
642
643/**
644 * srpt_free_ioctx() - Free an SRPT I/O context structure.
645 */
646static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
647 int dma_size, enum dma_data_direction dir)
648{
649 if (!ioctx)
650 return;
651
652 ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
653 kfree(ioctx->buf);
654 kfree(ioctx);
655}
656
657/**
658 * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
659 * @sdev: Device to allocate the I/O context ring for.
660 * @ring_size: Number of elements in the I/O context ring.
661 * @ioctx_size: I/O context size.
662 * @dma_size: DMA buffer size.
663 * @dir: DMA data direction.
664 */
665static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
666 int ring_size, int ioctx_size,
667 int dma_size, enum dma_data_direction dir)
668{
669 struct srpt_ioctx **ring;
670 int i;
671
672 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
673 && ioctx_size != sizeof(struct srpt_send_ioctx));
674
675 ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
676 if (!ring)
677 goto out;
678 for (i = 0; i < ring_size; ++i) {
679 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
680 if (!ring[i])
681 goto err;
682 ring[i]->index = i;
683 }
684 goto out;
685
686err:
687 while (--i >= 0)
688 srpt_free_ioctx(sdev, ring[i], dma_size, dir);
689 kfree(ring);
Jesper Juhl715252d2012-02-04 23:49:40 +0100690 ring = NULL;
Bart Van Asschea42d9852011-10-14 01:30:46 +0000691out:
692 return ring;
693}
694
695/**
696 * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
697 */
698static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
699 struct srpt_device *sdev, int ring_size,
700 int dma_size, enum dma_data_direction dir)
701{
702 int i;
703
704 for (i = 0; i < ring_size; ++i)
705 srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
706 kfree(ioctx_ring);
707}
708
709/**
710 * srpt_get_cmd_state() - Get the state of a SCSI command.
711 */
712static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
713{
714 enum srpt_command_state state;
715 unsigned long flags;
716
717 BUG_ON(!ioctx);
718
719 spin_lock_irqsave(&ioctx->spinlock, flags);
720 state = ioctx->state;
721 spin_unlock_irqrestore(&ioctx->spinlock, flags);
722 return state;
723}
724
725/**
726 * srpt_set_cmd_state() - Set the state of a SCSI command.
727 *
728 * Does not modify the state of aborted commands. Returns the previous command
729 * state.
730 */
731static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
732 enum srpt_command_state new)
733{
734 enum srpt_command_state previous;
735 unsigned long flags;
736
737 BUG_ON(!ioctx);
738
739 spin_lock_irqsave(&ioctx->spinlock, flags);
740 previous = ioctx->state;
741 if (previous != SRPT_STATE_DONE)
742 ioctx->state = new;
743 spin_unlock_irqrestore(&ioctx->spinlock, flags);
744
745 return previous;
746}
747
748/**
749 * srpt_test_and_set_cmd_state() - Test and set the state of a command.
750 *
751 * Returns true if and only if the previous command state was equal to 'old'.
752 */
753static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
754 enum srpt_command_state old,
755 enum srpt_command_state new)
756{
757 enum srpt_command_state previous;
758 unsigned long flags;
759
760 WARN_ON(!ioctx);
761 WARN_ON(old == SRPT_STATE_DONE);
762 WARN_ON(new == SRPT_STATE_NEW);
763
764 spin_lock_irqsave(&ioctx->spinlock, flags);
765 previous = ioctx->state;
766 if (previous == old)
767 ioctx->state = new;
768 spin_unlock_irqrestore(&ioctx->spinlock, flags);
769 return previous == old;
770}
771
772/**
773 * srpt_post_recv() - Post an IB receive request.
774 */
775static int srpt_post_recv(struct srpt_device *sdev,
776 struct srpt_recv_ioctx *ioctx)
777{
778 struct ib_sge list;
779 struct ib_recv_wr wr, *bad_wr;
780
781 BUG_ON(!sdev);
782 wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
783
784 list.addr = ioctx->ioctx.dma;
785 list.length = srp_max_req_size;
786 list.lkey = sdev->mr->lkey;
787
788 wr.next = NULL;
789 wr.sg_list = &list;
790 wr.num_sge = 1;
791
792 return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
793}
794
795/**
796 * srpt_post_send() - Post an IB send request.
797 *
798 * Returns zero upon success and a non-zero value upon failure.
799 */
800static int srpt_post_send(struct srpt_rdma_ch *ch,
801 struct srpt_send_ioctx *ioctx, int len)
802{
803 struct ib_sge list;
804 struct ib_send_wr wr, *bad_wr;
805 struct srpt_device *sdev = ch->sport->sdev;
806 int ret;
807
808 atomic_inc(&ch->req_lim);
809
810 ret = -ENOMEM;
811 if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
812 printk(KERN_WARNING "IB send queue full (needed 1)\n");
813 goto out;
814 }
815
816 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
817 DMA_TO_DEVICE);
818
819 list.addr = ioctx->ioctx.dma;
820 list.length = len;
821 list.lkey = sdev->mr->lkey;
822
823 wr.next = NULL;
824 wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
825 wr.sg_list = &list;
826 wr.num_sge = 1;
827 wr.opcode = IB_WR_SEND;
828 wr.send_flags = IB_SEND_SIGNALED;
829
830 ret = ib_post_send(ch->qp, &wr, &bad_wr);
831
832out:
833 if (ret < 0) {
834 atomic_inc(&ch->sq_wr_avail);
835 atomic_dec(&ch->req_lim);
836 }
837 return ret;
838}
839
840/**
841 * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
842 * @ioctx: Pointer to the I/O context associated with the request.
843 * @srp_cmd: Pointer to the SRP_CMD request data.
844 * @dir: Pointer to the variable to which the transfer direction will be
845 * written.
846 * @data_len: Pointer to the variable to which the total data length of all
847 * descriptors in the SRP_CMD request will be written.
848 *
849 * This function initializes ioctx->nrbuf and ioctx->r_bufs.
850 *
851 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
852 * -ENOMEM when memory allocation fails and zero upon success.
853 */
854static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
855 struct srp_cmd *srp_cmd,
856 enum dma_data_direction *dir, u64 *data_len)
857{
858 struct srp_indirect_buf *idb;
859 struct srp_direct_buf *db;
860 unsigned add_cdb_offset;
861 int ret;
862
863 /*
864 * The pointer computations below will only be compiled correctly
865 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
866 * whether srp_cmd::add_data has been declared as a byte pointer.
867 */
868 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
869 && !__same_type(srp_cmd->add_data[0], (u8)0));
870
871 BUG_ON(!dir);
872 BUG_ON(!data_len);
873
874 ret = 0;
875 *data_len = 0;
876
877 /*
878 * The lower four bits of the buffer format field contain the DATA-IN
879 * buffer descriptor format, and the highest four bits contain the
880 * DATA-OUT buffer descriptor format.
881 */
882 *dir = DMA_NONE;
883 if (srp_cmd->buf_fmt & 0xf)
884 /* DATA-IN: transfer data from target to initiator (read). */
885 *dir = DMA_FROM_DEVICE;
886 else if (srp_cmd->buf_fmt >> 4)
887 /* DATA-OUT: transfer data from initiator to target (write). */
888 *dir = DMA_TO_DEVICE;
889
890 /*
891 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
892 * CDB LENGTH' field are reserved and the size in bytes of this field
893 * is four times the value specified in bits 3..7. Hence the "& ~3".
894 */
895 add_cdb_offset = srp_cmd->add_cdb_len & ~3;
896 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
897 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
898 ioctx->n_rbuf = 1;
899 ioctx->rbufs = &ioctx->single_rbuf;
900
901 db = (struct srp_direct_buf *)(srp_cmd->add_data
902 + add_cdb_offset);
903 memcpy(ioctx->rbufs, db, sizeof *db);
904 *data_len = be32_to_cpu(db->len);
905 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
906 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
907 idb = (struct srp_indirect_buf *)(srp_cmd->add_data
908 + add_cdb_offset);
909
910 ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
911
912 if (ioctx->n_rbuf >
913 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
914 printk(KERN_ERR "received unsupported SRP_CMD request"
915 " type (%u out + %u in != %u / %zu)\n",
916 srp_cmd->data_out_desc_cnt,
917 srp_cmd->data_in_desc_cnt,
918 be32_to_cpu(idb->table_desc.len),
919 sizeof(*db));
920 ioctx->n_rbuf = 0;
921 ret = -EINVAL;
922 goto out;
923 }
924
925 if (ioctx->n_rbuf == 1)
926 ioctx->rbufs = &ioctx->single_rbuf;
927 else {
928 ioctx->rbufs =
929 kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
930 if (!ioctx->rbufs) {
931 ioctx->n_rbuf = 0;
932 ret = -ENOMEM;
933 goto out;
934 }
935 }
936
937 db = idb->desc_list;
938 memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
939 *data_len = be32_to_cpu(idb->len);
940 }
941out:
942 return ret;
943}
944
945/**
946 * srpt_init_ch_qp() - Initialize queue pair attributes.
947 *
948 * Initialized the attributes of queue pair 'qp' by allowing local write,
949 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
950 */
951static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
952{
953 struct ib_qp_attr *attr;
954 int ret;
955
956 attr = kzalloc(sizeof *attr, GFP_KERNEL);
957 if (!attr)
958 return -ENOMEM;
959
960 attr->qp_state = IB_QPS_INIT;
961 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
962 IB_ACCESS_REMOTE_WRITE;
963 attr->port_num = ch->sport->port;
964 attr->pkey_index = 0;
965
966 ret = ib_modify_qp(qp, attr,
967 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
968 IB_QP_PKEY_INDEX);
969
970 kfree(attr);
971 return ret;
972}
973
974/**
975 * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
976 * @ch: channel of the queue pair.
977 * @qp: queue pair to change the state of.
978 *
979 * Returns zero upon success and a negative value upon failure.
980 *
981 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
982 * If this structure ever becomes larger, it might be necessary to allocate
983 * it dynamically instead of on the stack.
984 */
985static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
986{
987 struct ib_qp_attr qp_attr;
988 int attr_mask;
989 int ret;
990
991 qp_attr.qp_state = IB_QPS_RTR;
992 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
993 if (ret)
994 goto out;
995
996 qp_attr.max_dest_rd_atomic = 4;
997
998 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
999
1000out:
1001 return ret;
1002}
1003
1004/**
1005 * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
1006 * @ch: channel of the queue pair.
1007 * @qp: queue pair to change the state of.
1008 *
1009 * Returns zero upon success and a negative value upon failure.
1010 *
1011 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1012 * If this structure ever becomes larger, it might be necessary to allocate
1013 * it dynamically instead of on the stack.
1014 */
1015static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1016{
1017 struct ib_qp_attr qp_attr;
1018 int attr_mask;
1019 int ret;
1020
1021 qp_attr.qp_state = IB_QPS_RTS;
1022 ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
1023 if (ret)
1024 goto out;
1025
1026 qp_attr.max_rd_atomic = 4;
1027
1028 ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1029
1030out:
1031 return ret;
1032}
1033
1034/**
1035 * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1036 */
1037static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1038{
1039 struct ib_qp_attr qp_attr;
1040
1041 qp_attr.qp_state = IB_QPS_ERR;
1042 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1043}
1044
1045/**
1046 * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
1047 */
1048static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1049 struct srpt_send_ioctx *ioctx)
1050{
1051 struct scatterlist *sg;
1052 enum dma_data_direction dir;
1053
1054 BUG_ON(!ch);
1055 BUG_ON(!ioctx);
1056 BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
1057
1058 while (ioctx->n_rdma)
1059 kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
1060
1061 kfree(ioctx->rdma_ius);
1062 ioctx->rdma_ius = NULL;
1063
1064 if (ioctx->mapped_sg_count) {
1065 sg = ioctx->sg;
1066 WARN_ON(!sg);
1067 dir = ioctx->cmd.data_direction;
1068 BUG_ON(dir == DMA_NONE);
1069 ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
1070 opposite_dma_dir(dir));
1071 ioctx->mapped_sg_count = 0;
1072 }
1073}
1074
1075/**
1076 * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
1077 */
1078static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1079 struct srpt_send_ioctx *ioctx)
1080{
1081 struct se_cmd *cmd;
1082 struct scatterlist *sg, *sg_orig;
1083 int sg_cnt;
1084 enum dma_data_direction dir;
1085 struct rdma_iu *riu;
1086 struct srp_direct_buf *db;
1087 dma_addr_t dma_addr;
1088 struct ib_sge *sge;
1089 u64 raddr;
1090 u32 rsize;
1091 u32 tsize;
1092 u32 dma_len;
1093 int count, nrdma;
1094 int i, j, k;
1095
1096 BUG_ON(!ch);
1097 BUG_ON(!ioctx);
1098 cmd = &ioctx->cmd;
1099 dir = cmd->data_direction;
1100 BUG_ON(dir == DMA_NONE);
1101
Roland Dreier6f9e7f02012-03-30 11:29:12 -07001102 ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1103 ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001104
1105 count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1106 opposite_dma_dir(dir));
1107 if (unlikely(!count))
1108 return -EAGAIN;
1109
1110 ioctx->mapped_sg_count = count;
1111
1112 if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1113 nrdma = ioctx->n_rdma_ius;
1114 else {
1115 nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1116 + ioctx->n_rbuf;
1117
1118 ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
1119 if (!ioctx->rdma_ius)
1120 goto free_mem;
1121
1122 ioctx->n_rdma_ius = nrdma;
1123 }
1124
1125 db = ioctx->rbufs;
1126 tsize = cmd->data_length;
1127 dma_len = sg_dma_len(&sg[0]);
1128 riu = ioctx->rdma_ius;
1129
1130 /*
1131 * For each remote desc - calculate the #ib_sge.
1132 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1133 * each remote desc rdma_iu is required a rdma wr;
1134 * else
1135 * we need to allocate extra rdma_iu to carry extra #ib_sge in
1136 * another rdma wr
1137 */
1138 for (i = 0, j = 0;
1139 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1140 rsize = be32_to_cpu(db->len);
1141 raddr = be64_to_cpu(db->va);
1142 riu->raddr = raddr;
1143 riu->rkey = be32_to_cpu(db->key);
1144 riu->sge_cnt = 0;
1145
1146 /* calculate how many sge required for this remote_buf */
1147 while (rsize > 0 && tsize > 0) {
1148
1149 if (rsize >= dma_len) {
1150 tsize -= dma_len;
1151 rsize -= dma_len;
1152 raddr += dma_len;
1153
1154 if (tsize > 0) {
1155 ++j;
1156 if (j < count) {
1157 sg = sg_next(sg);
1158 dma_len = sg_dma_len(sg);
1159 }
1160 }
1161 } else {
1162 tsize -= rsize;
1163 dma_len -= rsize;
1164 rsize = 0;
1165 }
1166
1167 ++riu->sge_cnt;
1168
1169 if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1170 ++ioctx->n_rdma;
1171 riu->sge =
1172 kmalloc(riu->sge_cnt * sizeof *riu->sge,
1173 GFP_KERNEL);
1174 if (!riu->sge)
1175 goto free_mem;
1176
1177 ++riu;
1178 riu->sge_cnt = 0;
1179 riu->raddr = raddr;
1180 riu->rkey = be32_to_cpu(db->key);
1181 }
1182 }
1183
1184 ++ioctx->n_rdma;
1185 riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1186 GFP_KERNEL);
1187 if (!riu->sge)
1188 goto free_mem;
1189 }
1190
1191 db = ioctx->rbufs;
1192 tsize = cmd->data_length;
1193 riu = ioctx->rdma_ius;
1194 sg = sg_orig;
1195 dma_len = sg_dma_len(&sg[0]);
1196 dma_addr = sg_dma_address(&sg[0]);
1197
1198 /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1199 for (i = 0, j = 0;
1200 j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1201 rsize = be32_to_cpu(db->len);
1202 sge = riu->sge;
1203 k = 0;
1204
1205 while (rsize > 0 && tsize > 0) {
1206 sge->addr = dma_addr;
1207 sge->lkey = ch->sport->sdev->mr->lkey;
1208
1209 if (rsize >= dma_len) {
1210 sge->length =
1211 (tsize < dma_len) ? tsize : dma_len;
1212 tsize -= dma_len;
1213 rsize -= dma_len;
1214
1215 if (tsize > 0) {
1216 ++j;
1217 if (j < count) {
1218 sg = sg_next(sg);
1219 dma_len = sg_dma_len(sg);
1220 dma_addr = sg_dma_address(sg);
1221 }
1222 }
1223 } else {
1224 sge->length = (tsize < rsize) ? tsize : rsize;
1225 tsize -= rsize;
1226 dma_len -= rsize;
1227 dma_addr += rsize;
1228 rsize = 0;
1229 }
1230
1231 ++k;
1232 if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1233 ++riu;
1234 sge = riu->sge;
1235 k = 0;
1236 } else if (rsize > 0 && tsize > 0)
1237 ++sge;
1238 }
1239 }
1240
1241 return 0;
1242
1243free_mem:
1244 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1245
1246 return -ENOMEM;
1247}
1248
1249/**
1250 * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1251 */
1252static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1253{
1254 struct srpt_send_ioctx *ioctx;
1255 unsigned long flags;
1256
1257 BUG_ON(!ch);
1258
1259 ioctx = NULL;
1260 spin_lock_irqsave(&ch->spinlock, flags);
1261 if (!list_empty(&ch->free_list)) {
1262 ioctx = list_first_entry(&ch->free_list,
1263 struct srpt_send_ioctx, free_list);
1264 list_del(&ioctx->free_list);
1265 }
1266 spin_unlock_irqrestore(&ch->spinlock, flags);
1267
1268 if (!ioctx)
1269 return ioctx;
1270
1271 BUG_ON(ioctx->ch != ch);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001272 spin_lock_init(&ioctx->spinlock);
1273 ioctx->state = SRPT_STATE_NEW;
1274 ioctx->n_rbuf = 0;
1275 ioctx->rbufs = NULL;
1276 ioctx->n_rdma = 0;
1277 ioctx->n_rdma_ius = 0;
1278 ioctx->rdma_ius = NULL;
1279 ioctx->mapped_sg_count = 0;
1280 init_completion(&ioctx->tx_done);
1281 ioctx->queue_status_only = false;
1282 /*
1283 * transport_init_se_cmd() does not initialize all fields, so do it
1284 * here.
1285 */
1286 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1287 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1288
1289 return ioctx;
1290}
1291
1292/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00001293 * srpt_abort_cmd() - Abort a SCSI command.
1294 * @ioctx: I/O context associated with the SCSI command.
1295 * @context: Preferred execution context.
1296 */
1297static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1298{
1299 enum srpt_command_state state;
1300 unsigned long flags;
1301
1302 BUG_ON(!ioctx);
1303
1304 /*
1305 * If the command is in a state where the target core is waiting for
1306 * the ib_srpt driver, change the state to the next state. Changing
1307 * the state of the command from SRPT_STATE_NEED_DATA to
1308 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1309 * function a second time.
1310 */
1311
1312 spin_lock_irqsave(&ioctx->spinlock, flags);
1313 state = ioctx->state;
1314 switch (state) {
1315 case SRPT_STATE_NEED_DATA:
1316 ioctx->state = SRPT_STATE_DATA_IN;
1317 break;
1318 case SRPT_STATE_DATA_IN:
1319 case SRPT_STATE_CMD_RSP_SENT:
1320 case SRPT_STATE_MGMT_RSP_SENT:
1321 ioctx->state = SRPT_STATE_DONE;
1322 break;
1323 default:
1324 break;
1325 }
1326 spin_unlock_irqrestore(&ioctx->spinlock, flags);
1327
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001328 if (state == SRPT_STATE_DONE) {
1329 struct srpt_rdma_ch *ch = ioctx->ch;
1330
1331 BUG_ON(ch->sess == NULL);
1332
1333 target_put_sess_cmd(ch->sess, &ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001334 goto out;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001335 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001336
1337 pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1338 ioctx->tag);
1339
1340 switch (state) {
1341 case SRPT_STATE_NEW:
1342 case SRPT_STATE_DATA_IN:
1343 case SRPT_STATE_MGMT:
1344 /*
1345 * Do nothing - defer abort processing until
1346 * srpt_queue_response() is invoked.
1347 */
1348 WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1349 break;
1350 case SRPT_STATE_NEED_DATA:
1351 /* DMA_TO_DEVICE (write) - RDMA read error. */
Christoph Hellwige672a472012-07-08 15:58:43 -04001352
1353 /* XXX(hch): this is a horrible layering violation.. */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001354 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1355 ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
Christoph Hellwige672a472012-07-08 15:58:43 -04001356 ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001357 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Christoph Hellwige672a472012-07-08 15:58:43 -04001358
1359 complete(&ioctx->cmd.transport_lun_stop_comp);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001360 break;
1361 case SRPT_STATE_CMD_RSP_SENT:
1362 /*
1363 * SRP_RSP sending failed or the SRP_RSP send completion has
1364 * not been received in time.
1365 */
1366 srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001367 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1368 ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
1369 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001370 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001371 break;
1372 case SRPT_STATE_MGMT_RSP_SENT:
1373 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001374 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001375 break;
1376 default:
1377 WARN_ON("ERROR: unexpected command state");
1378 break;
1379 }
1380
1381out:
1382 return state;
1383}
1384
1385/**
1386 * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1387 */
1388static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1389{
1390 struct srpt_send_ioctx *ioctx;
1391 enum srpt_command_state state;
1392 struct se_cmd *cmd;
1393 u32 index;
1394
1395 atomic_inc(&ch->sq_wr_avail);
1396
1397 index = idx_from_wr_id(wr_id);
1398 ioctx = ch->ioctx_ring[index];
1399 state = srpt_get_cmd_state(ioctx);
1400 cmd = &ioctx->cmd;
1401
1402 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1403 && state != SRPT_STATE_MGMT_RSP_SENT
1404 && state != SRPT_STATE_NEED_DATA
1405 && state != SRPT_STATE_DONE);
1406
1407 /* If SRP_RSP sending failed, undo the ch->req_lim change. */
1408 if (state == SRPT_STATE_CMD_RSP_SENT
1409 || state == SRPT_STATE_MGMT_RSP_SENT)
1410 atomic_dec(&ch->req_lim);
1411
1412 srpt_abort_cmd(ioctx);
1413}
1414
1415/**
1416 * srpt_handle_send_comp() - Process an IB send completion notification.
1417 */
1418static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1419 struct srpt_send_ioctx *ioctx)
1420{
1421 enum srpt_command_state state;
1422
1423 atomic_inc(&ch->sq_wr_avail);
1424
1425 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1426
1427 if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1428 && state != SRPT_STATE_MGMT_RSP_SENT
1429 && state != SRPT_STATE_DONE))
1430 pr_debug("state = %d\n", state);
1431
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001432 if (state != SRPT_STATE_DONE) {
1433 srpt_unmap_sg_to_ib_sge(ch, ioctx);
1434 transport_generic_free_cmd(&ioctx->cmd, 0);
1435 } else {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001436 printk(KERN_ERR "IB completion has been received too late for"
1437 " wr_id = %u.\n", ioctx->ioctx.index);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001438 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001439}
1440
1441/**
1442 * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1443 *
Christoph Hellwige672a472012-07-08 15:58:43 -04001444 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1445 * the data that has been transferred via IB RDMA had to be postponed until the
Masanari Iida142ad5d2012-08-10 00:07:58 +00001446 * check_stop_free() callback. None of this is necessary anymore and needs to
Christoph Hellwige672a472012-07-08 15:58:43 -04001447 * be cleaned up.
Bart Van Asschea42d9852011-10-14 01:30:46 +00001448 */
1449static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1450 struct srpt_send_ioctx *ioctx,
1451 enum srpt_opcode opcode)
1452{
1453 WARN_ON(ioctx->n_rdma <= 0);
1454 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1455
1456 if (opcode == SRPT_RDMA_READ_LAST) {
1457 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1458 SRPT_STATE_DATA_IN))
Christoph Hellwige672a472012-07-08 15:58:43 -04001459 target_execute_cmd(&ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001460 else
1461 printk(KERN_ERR "%s[%d]: wrong state = %d\n", __func__,
1462 __LINE__, srpt_get_cmd_state(ioctx));
1463 } else if (opcode == SRPT_RDMA_ABORT) {
1464 ioctx->rdma_aborted = true;
1465 } else {
1466 WARN(true, "unexpected opcode %d\n", opcode);
1467 }
1468}
1469
1470/**
1471 * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1472 */
1473static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1474 struct srpt_send_ioctx *ioctx,
1475 enum srpt_opcode opcode)
1476{
1477 struct se_cmd *cmd;
1478 enum srpt_command_state state;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001479 unsigned long flags;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001480
1481 cmd = &ioctx->cmd;
1482 state = srpt_get_cmd_state(ioctx);
1483 switch (opcode) {
1484 case SRPT_RDMA_READ_LAST:
1485 if (ioctx->n_rdma <= 0) {
1486 printk(KERN_ERR "Received invalid RDMA read"
1487 " error completion with idx %d\n",
1488 ioctx->ioctx.index);
1489 break;
1490 }
1491 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1492 if (state == SRPT_STATE_NEED_DATA)
1493 srpt_abort_cmd(ioctx);
1494 else
1495 printk(KERN_ERR "%s[%d]: wrong state = %d\n",
1496 __func__, __LINE__, state);
1497 break;
1498 case SRPT_RDMA_WRITE_LAST:
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001499 spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1500 ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
1501 spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001502 break;
1503 default:
1504 printk(KERN_ERR "%s[%d]: opcode = %u\n", __func__,
1505 __LINE__, opcode);
1506 break;
1507 }
1508}
1509
1510/**
1511 * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1512 * @ch: RDMA channel through which the request has been received.
1513 * @ioctx: I/O context associated with the SRP_CMD request. The response will
1514 * be built in the buffer ioctx->buf points at and hence this function will
1515 * overwrite the request data.
1516 * @tag: tag of the request for which this response is being generated.
1517 * @status: value for the STATUS field of the SRP_RSP information unit.
1518 *
1519 * Returns the size in bytes of the SRP_RSP response.
1520 *
1521 * An SRP_RSP response contains a SCSI status or service response. See also
1522 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1523 * response. See also SPC-2 for more information about sense data.
1524 */
1525static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1526 struct srpt_send_ioctx *ioctx, u64 tag,
1527 int status)
1528{
1529 struct srp_rsp *srp_rsp;
1530 const u8 *sense_data;
1531 int sense_data_len, max_sense_len;
1532
1533 /*
1534 * The lowest bit of all SAM-3 status codes is zero (see also
1535 * paragraph 5.3 in SAM-3).
1536 */
1537 WARN_ON(status & 1);
1538
1539 srp_rsp = ioctx->ioctx.buf;
1540 BUG_ON(!srp_rsp);
1541
1542 sense_data = ioctx->sense_data;
1543 sense_data_len = ioctx->cmd.scsi_sense_length;
1544 WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1545
1546 memset(srp_rsp, 0, sizeof *srp_rsp);
1547 srp_rsp->opcode = SRP_RSP;
1548 srp_rsp->req_lim_delta =
1549 __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1550 srp_rsp->tag = tag;
1551 srp_rsp->status = status;
1552
1553 if (sense_data_len) {
1554 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1555 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1556 if (sense_data_len > max_sense_len) {
1557 printk(KERN_WARNING "truncated sense data from %d to %d"
1558 " bytes\n", sense_data_len, max_sense_len);
1559 sense_data_len = max_sense_len;
1560 }
1561
1562 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1563 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1564 memcpy(srp_rsp + 1, sense_data, sense_data_len);
1565 }
1566
1567 return sizeof(*srp_rsp) + sense_data_len;
1568}
1569
1570/**
1571 * srpt_build_tskmgmt_rsp() - Build a task management response.
1572 * @ch: RDMA channel through which the request has been received.
1573 * @ioctx: I/O context in which the SRP_RSP response will be built.
1574 * @rsp_code: RSP_CODE that will be stored in the response.
1575 * @tag: Tag of the request for which this response is being generated.
1576 *
1577 * Returns the size in bytes of the SRP_RSP response.
1578 *
1579 * An SRP_RSP response contains a SCSI status or service response. See also
1580 * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1581 * response.
1582 */
1583static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1584 struct srpt_send_ioctx *ioctx,
1585 u8 rsp_code, u64 tag)
1586{
1587 struct srp_rsp *srp_rsp;
1588 int resp_data_len;
1589 int resp_len;
1590
1591 resp_data_len = (rsp_code == SRP_TSK_MGMT_SUCCESS) ? 0 : 4;
1592 resp_len = sizeof(*srp_rsp) + resp_data_len;
1593
1594 srp_rsp = ioctx->ioctx.buf;
1595 BUG_ON(!srp_rsp);
1596 memset(srp_rsp, 0, sizeof *srp_rsp);
1597
1598 srp_rsp->opcode = SRP_RSP;
1599 srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
1600 + atomic_xchg(&ch->req_lim_delta, 0));
1601 srp_rsp->tag = tag;
1602
1603 if (rsp_code != SRP_TSK_MGMT_SUCCESS) {
1604 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1605 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1606 srp_rsp->data[3] = rsp_code;
1607 }
1608
1609 return resp_len;
1610}
1611
1612#define NO_SUCH_LUN ((uint64_t)-1LL)
1613
1614/*
1615 * SCSI LUN addressing method. See also SAM-2 and the section about
1616 * eight byte LUNs.
1617 */
1618enum scsi_lun_addr_method {
1619 SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
1620 SCSI_LUN_ADDR_METHOD_FLAT = 1,
1621 SCSI_LUN_ADDR_METHOD_LUN = 2,
1622 SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1623};
1624
1625/*
1626 * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1627 *
1628 * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1629 * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1630 * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1631 */
1632static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1633{
1634 uint64_t res = NO_SUCH_LUN;
1635 int addressing_method;
1636
1637 if (unlikely(len < 2)) {
1638 printk(KERN_ERR "Illegal LUN length %d, expected 2 bytes or "
1639 "more", len);
1640 goto out;
1641 }
1642
1643 switch (len) {
1644 case 8:
1645 if ((*((__be64 *)lun) &
1646 __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1647 goto out_err;
1648 break;
1649 case 4:
1650 if (*((__be16 *)&lun[2]) != 0)
1651 goto out_err;
1652 break;
1653 case 6:
1654 if (*((__be32 *)&lun[2]) != 0)
1655 goto out_err;
1656 break;
1657 case 2:
1658 break;
1659 default:
1660 goto out_err;
1661 }
1662
1663 addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1664 switch (addressing_method) {
1665 case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1666 case SCSI_LUN_ADDR_METHOD_FLAT:
1667 case SCSI_LUN_ADDR_METHOD_LUN:
1668 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1669 break;
1670
1671 case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1672 default:
1673 printk(KERN_ERR "Unimplemented LUN addressing method %u",
1674 addressing_method);
1675 break;
1676 }
1677
1678out:
1679 return res;
1680
1681out_err:
1682 printk(KERN_ERR "Support for multi-level LUNs has not yet been"
1683 " implemented");
1684 goto out;
1685}
1686
1687static int srpt_check_stop_free(struct se_cmd *cmd)
1688{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001689 struct srpt_send_ioctx *ioctx = container_of(cmd,
1690 struct srpt_send_ioctx, cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001691
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001692 return target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001693}
1694
1695/**
1696 * srpt_handle_cmd() - Process SRP_CMD.
1697 */
1698static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1699 struct srpt_recv_ioctx *recv_ioctx,
1700 struct srpt_send_ioctx *send_ioctx)
1701{
1702 struct se_cmd *cmd;
1703 struct srp_cmd *srp_cmd;
1704 uint64_t unpacked_lun;
1705 u64 data_len;
1706 enum dma_data_direction dir;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001707 sense_reason_t ret;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001708 int rc;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001709
1710 BUG_ON(!send_ioctx);
1711
1712 srp_cmd = recv_ioctx->ioctx.buf;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001713 cmd = &send_ioctx->cmd;
1714 send_ioctx->tag = srp_cmd->tag;
1715
1716 switch (srp_cmd->task_attr) {
1717 case SRP_CMD_SIMPLE_Q:
1718 cmd->sam_task_attr = MSG_SIMPLE_TAG;
1719 break;
1720 case SRP_CMD_ORDERED_Q:
1721 default:
1722 cmd->sam_task_attr = MSG_ORDERED_TAG;
1723 break;
1724 case SRP_CMD_HEAD_OF_Q:
1725 cmd->sam_task_attr = MSG_HEAD_TAG;
1726 break;
1727 case SRP_CMD_ACA:
1728 cmd->sam_task_attr = MSG_ACA_TAG;
1729 break;
1730 }
1731
Christoph Hellwigde103c92012-11-06 12:24:09 -08001732 if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001733 printk(KERN_ERR "0x%llx: parsing SRP descriptor table failed.\n",
1734 srp_cmd->tag);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001735 ret = TCM_INVALID_CDB_FIELD;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001736 goto send_sense;
1737 }
1738
Bart Van Asschea42d9852011-10-14 01:30:46 +00001739 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1740 sizeof(srp_cmd->lun));
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001741 rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1742 &send_ioctx->sense_data[0], unpacked_lun, data_len,
1743 MSG_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
1744 if (rc != 0) {
1745 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001746 goto send_sense;
Nicholas Bellinger187e70a2012-03-17 20:12:36 -07001747 }
Bart Van Asschea42d9852011-10-14 01:30:46 +00001748 return 0;
1749
1750send_sense:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001751 transport_send_check_condition_and_sense(cmd, ret, 0);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001752 return -1;
1753}
1754
1755/**
1756 * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
1757 * @ch: RDMA channel of the task management request.
1758 * @fn: Task management function to perform.
1759 * @req_tag: Tag of the SRP task management request.
1760 * @mgmt_ioctx: I/O context of the task management request.
1761 *
1762 * Returns zero if the target core will process the task management
1763 * request asynchronously.
1764 *
1765 * Note: It is assumed that the initiator serializes tag-based task management
1766 * requests.
1767 */
1768static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
1769{
1770 struct srpt_device *sdev;
1771 struct srpt_rdma_ch *ch;
1772 struct srpt_send_ioctx *target;
1773 int ret, i;
1774
1775 ret = -EINVAL;
1776 ch = ioctx->ch;
1777 BUG_ON(!ch);
1778 BUG_ON(!ch->sport);
1779 sdev = ch->sport->sdev;
1780 BUG_ON(!sdev);
1781 spin_lock_irq(&sdev->spinlock);
1782 for (i = 0; i < ch->rq_size; ++i) {
1783 target = ch->ioctx_ring[i];
1784 if (target->cmd.se_lun == ioctx->cmd.se_lun &&
1785 target->tag == tag &&
1786 srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
1787 ret = 0;
1788 /* now let the target core abort &target->cmd; */
1789 break;
1790 }
1791 }
1792 spin_unlock_irq(&sdev->spinlock);
1793 return ret;
1794}
1795
1796static int srp_tmr_to_tcm(int fn)
1797{
1798 switch (fn) {
1799 case SRP_TSK_ABORT_TASK:
1800 return TMR_ABORT_TASK;
1801 case SRP_TSK_ABORT_TASK_SET:
1802 return TMR_ABORT_TASK_SET;
1803 case SRP_TSK_CLEAR_TASK_SET:
1804 return TMR_CLEAR_TASK_SET;
1805 case SRP_TSK_LUN_RESET:
1806 return TMR_LUN_RESET;
1807 case SRP_TSK_CLEAR_ACA:
1808 return TMR_CLEAR_ACA;
1809 default:
1810 return -1;
1811 }
1812}
1813
1814/**
1815 * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1816 *
1817 * Returns 0 if and only if the request will be processed by the target core.
1818 *
1819 * For more information about SRP_TSK_MGMT information units, see also section
1820 * 6.7 in the SRP r16a document.
1821 */
1822static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1823 struct srpt_recv_ioctx *recv_ioctx,
1824 struct srpt_send_ioctx *send_ioctx)
1825{
1826 struct srp_tsk_mgmt *srp_tsk;
1827 struct se_cmd *cmd;
1828 uint64_t unpacked_lun;
1829 int tcm_tmr;
1830 int res;
1831
1832 BUG_ON(!send_ioctx);
1833
1834 srp_tsk = recv_ioctx->ioctx.buf;
1835 cmd = &send_ioctx->cmd;
1836
1837 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1838 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1839 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1840
1841 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1842 send_ioctx->tag = srp_tsk->tag;
1843 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1844 if (tcm_tmr < 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001845 send_ioctx->cmd.se_tmr_req->response =
1846 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001847 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001848 }
Nicholas Bellinger9474b042012-11-27 23:55:57 -08001849 transport_init_se_cmd(&send_ioctx->cmd, &srpt_target->tf_ops, ch->sess,
1850 0, DMA_NONE, MSG_SIMPLE_TAG, send_ioctx->sense_data);
1851
Andy Groverc8e31f22012-01-19 13:39:17 -08001852 res = core_tmr_alloc_req(cmd, NULL, tcm_tmr, GFP_KERNEL);
1853 if (res < 0) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00001854 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001855 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001856 }
1857
1858 unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1859 sizeof(srp_tsk->lun));
1860 res = transport_lookup_tmr_lun(&send_ioctx->cmd, unpacked_lun);
1861 if (res) {
1862 pr_debug("rejecting TMR for LUN %lld\n", unpacked_lun);
Bart Van Asschea42d9852011-10-14 01:30:46 +00001863 send_ioctx->cmd.se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001864 goto fail;
Bart Van Asschea42d9852011-10-14 01:30:46 +00001865 }
1866
1867 if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK)
1868 srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
1869
Christoph Hellwigde103c92012-11-06 12:24:09 -08001870 transport_generic_handle_tmr(&send_ioctx->cmd);
1871 return;
1872fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -08001873 transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
Bart Van Asschea42d9852011-10-14 01:30:46 +00001874}
1875
1876/**
1877 * srpt_handle_new_iu() - Process a newly received information unit.
1878 * @ch: RDMA channel through which the information unit has been received.
1879 * @ioctx: SRPT I/O context associated with the information unit.
1880 */
1881static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1882 struct srpt_recv_ioctx *recv_ioctx,
1883 struct srpt_send_ioctx *send_ioctx)
1884{
1885 struct srp_cmd *srp_cmd;
1886 enum rdma_ch_state ch_state;
1887
1888 BUG_ON(!ch);
1889 BUG_ON(!recv_ioctx);
1890
1891 ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1892 recv_ioctx->ioctx.dma, srp_max_req_size,
1893 DMA_FROM_DEVICE);
1894
1895 ch_state = srpt_get_ch_state(ch);
1896 if (unlikely(ch_state == CH_CONNECTING)) {
1897 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1898 goto out;
1899 }
1900
1901 if (unlikely(ch_state != CH_LIVE))
1902 goto out;
1903
1904 srp_cmd = recv_ioctx->ioctx.buf;
1905 if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1906 if (!send_ioctx)
1907 send_ioctx = srpt_get_send_ioctx(ch);
1908 if (unlikely(!send_ioctx)) {
1909 list_add_tail(&recv_ioctx->wait_list,
1910 &ch->cmd_wait_list);
1911 goto out;
1912 }
1913 }
1914
Bart Van Asschea42d9852011-10-14 01:30:46 +00001915 switch (srp_cmd->opcode) {
1916 case SRP_CMD:
1917 srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1918 break;
1919 case SRP_TSK_MGMT:
1920 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1921 break;
1922 case SRP_I_LOGOUT:
1923 printk(KERN_ERR "Not yet implemented: SRP_I_LOGOUT\n");
1924 break;
1925 case SRP_CRED_RSP:
1926 pr_debug("received SRP_CRED_RSP\n");
1927 break;
1928 case SRP_AER_RSP:
1929 pr_debug("received SRP_AER_RSP\n");
1930 break;
1931 case SRP_RSP:
1932 printk(KERN_ERR "Received SRP_RSP\n");
1933 break;
1934 default:
1935 printk(KERN_ERR "received IU with unknown opcode 0x%x\n",
1936 srp_cmd->opcode);
1937 break;
1938 }
1939
1940 srpt_post_recv(ch->sport->sdev, recv_ioctx);
1941out:
1942 return;
1943}
1944
1945static void srpt_process_rcv_completion(struct ib_cq *cq,
1946 struct srpt_rdma_ch *ch,
1947 struct ib_wc *wc)
1948{
1949 struct srpt_device *sdev = ch->sport->sdev;
1950 struct srpt_recv_ioctx *ioctx;
1951 u32 index;
1952
1953 index = idx_from_wr_id(wc->wr_id);
1954 if (wc->status == IB_WC_SUCCESS) {
1955 int req_lim;
1956
1957 req_lim = atomic_dec_return(&ch->req_lim);
1958 if (unlikely(req_lim < 0))
1959 printk(KERN_ERR "req_lim = %d < 0\n", req_lim);
1960 ioctx = sdev->ioctx_ring[index];
1961 srpt_handle_new_iu(ch, ioctx, NULL);
1962 } else {
1963 printk(KERN_INFO "receiving failed for idx %u with status %d\n",
1964 index, wc->status);
1965 }
1966}
1967
1968/**
1969 * srpt_process_send_completion() - Process an IB send completion.
1970 *
1971 * Note: Although this has not yet been observed during tests, at least in
1972 * theory it is possible that the srpt_get_send_ioctx() call invoked by
1973 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1974 * value in each response is set to one, and it is possible that this response
1975 * makes the initiator send a new request before the send completion for that
1976 * response has been processed. This could e.g. happen if the call to
1977 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1978 * if IB retransmission causes generation of the send completion to be
1979 * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1980 * are queued on cmd_wait_list. The code below processes these delayed
1981 * requests one at a time.
1982 */
1983static void srpt_process_send_completion(struct ib_cq *cq,
1984 struct srpt_rdma_ch *ch,
1985 struct ib_wc *wc)
1986{
1987 struct srpt_send_ioctx *send_ioctx;
1988 uint32_t index;
1989 enum srpt_opcode opcode;
1990
1991 index = idx_from_wr_id(wc->wr_id);
1992 opcode = opcode_from_wr_id(wc->wr_id);
1993 send_ioctx = ch->ioctx_ring[index];
1994 if (wc->status == IB_WC_SUCCESS) {
1995 if (opcode == SRPT_SEND)
1996 srpt_handle_send_comp(ch, send_ioctx);
1997 else {
1998 WARN_ON(opcode != SRPT_RDMA_ABORT &&
1999 wc->opcode != IB_WC_RDMA_READ);
2000 srpt_handle_rdma_comp(ch, send_ioctx, opcode);
2001 }
2002 } else {
2003 if (opcode == SRPT_SEND) {
2004 printk(KERN_INFO "sending response for idx %u failed"
2005 " with status %d\n", index, wc->status);
2006 srpt_handle_send_err_comp(ch, wc->wr_id);
2007 } else if (opcode != SRPT_RDMA_MID) {
2008 printk(KERN_INFO "RDMA t %d for idx %u failed with"
2009 " status %d", opcode, index, wc->status);
2010 srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
2011 }
2012 }
2013
2014 while (unlikely(opcode == SRPT_SEND
2015 && !list_empty(&ch->cmd_wait_list)
2016 && srpt_get_ch_state(ch) == CH_LIVE
2017 && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
2018 struct srpt_recv_ioctx *recv_ioctx;
2019
2020 recv_ioctx = list_first_entry(&ch->cmd_wait_list,
2021 struct srpt_recv_ioctx,
2022 wait_list);
2023 list_del(&recv_ioctx->wait_list);
2024 srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
2025 }
2026}
2027
2028static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
2029{
2030 struct ib_wc *const wc = ch->wc;
2031 int i, n;
2032
2033 WARN_ON(cq != ch->cq);
2034
2035 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
2036 while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
2037 for (i = 0; i < n; i++) {
2038 if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
2039 srpt_process_rcv_completion(cq, ch, &wc[i]);
2040 else
2041 srpt_process_send_completion(cq, ch, &wc[i]);
2042 }
2043 }
2044}
2045
2046/**
2047 * srpt_completion() - IB completion queue callback function.
2048 *
2049 * Notes:
2050 * - It is guaranteed that a completion handler will never be invoked
2051 * concurrently on two different CPUs for the same completion queue. See also
2052 * Documentation/infiniband/core_locking.txt and the implementation of
2053 * handle_edge_irq() in kernel/irq/chip.c.
2054 * - When threaded IRQs are enabled, completion handlers are invoked in thread
2055 * context instead of interrupt context.
2056 */
2057static void srpt_completion(struct ib_cq *cq, void *ctx)
2058{
2059 struct srpt_rdma_ch *ch = ctx;
2060
2061 wake_up_interruptible(&ch->wait_queue);
2062}
2063
2064static int srpt_compl_thread(void *arg)
2065{
2066 struct srpt_rdma_ch *ch;
2067
2068 /* Hibernation / freezing of the SRPT kernel thread is not supported. */
2069 current->flags |= PF_NOFREEZE;
2070
2071 ch = arg;
2072 BUG_ON(!ch);
2073 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) started\n",
2074 ch->sess_name, ch->thread->comm, current->pid);
2075 while (!kthread_should_stop()) {
2076 wait_event_interruptible(ch->wait_queue,
2077 (srpt_process_completion(ch->cq, ch),
2078 kthread_should_stop()));
2079 }
2080 printk(KERN_INFO "Session %s: kernel thread %s (PID %d) stopped\n",
2081 ch->sess_name, ch->thread->comm, current->pid);
2082 return 0;
2083}
2084
2085/**
2086 * srpt_create_ch_ib() - Create receive and send completion queues.
2087 */
2088static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2089{
2090 struct ib_qp_init_attr *qp_init;
2091 struct srpt_port *sport = ch->sport;
2092 struct srpt_device *sdev = sport->sdev;
2093 u32 srp_sq_size = sport->port_attrib.srp_sq_size;
2094 int ret;
2095
2096 WARN_ON(ch->rq_size < 1);
2097
2098 ret = -ENOMEM;
2099 qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2100 if (!qp_init)
2101 goto out;
2102
2103 ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
2104 ch->rq_size + srp_sq_size, 0);
2105 if (IS_ERR(ch->cq)) {
2106 ret = PTR_ERR(ch->cq);
2107 printk(KERN_ERR "failed to create CQ cqe= %d ret= %d\n",
2108 ch->rq_size + srp_sq_size, ret);
2109 goto out;
2110 }
2111
2112 qp_init->qp_context = (void *)ch;
2113 qp_init->event_handler
2114 = (void(*)(struct ib_event *, void*))srpt_qp_event;
2115 qp_init->send_cq = ch->cq;
2116 qp_init->recv_cq = ch->cq;
2117 qp_init->srq = sdev->srq;
2118 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2119 qp_init->qp_type = IB_QPT_RC;
2120 qp_init->cap.max_send_wr = srp_sq_size;
2121 qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2122
2123 ch->qp = ib_create_qp(sdev->pd, qp_init);
2124 if (IS_ERR(ch->qp)) {
2125 ret = PTR_ERR(ch->qp);
2126 printk(KERN_ERR "failed to create_qp ret= %d\n", ret);
2127 goto err_destroy_cq;
2128 }
2129
2130 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2131
2132 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2133 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2134 qp_init->cap.max_send_wr, ch->cm_id);
2135
2136 ret = srpt_init_ch_qp(ch, ch->qp);
2137 if (ret)
2138 goto err_destroy_qp;
2139
2140 init_waitqueue_head(&ch->wait_queue);
2141
2142 pr_debug("creating thread for session %s\n", ch->sess_name);
2143
2144 ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2145 if (IS_ERR(ch->thread)) {
2146 printk(KERN_ERR "failed to create kernel thread %ld\n",
2147 PTR_ERR(ch->thread));
2148 ch->thread = NULL;
2149 goto err_destroy_qp;
2150 }
2151
2152out:
2153 kfree(qp_init);
2154 return ret;
2155
2156err_destroy_qp:
2157 ib_destroy_qp(ch->qp);
2158err_destroy_cq:
2159 ib_destroy_cq(ch->cq);
2160 goto out;
2161}
2162
2163static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2164{
2165 if (ch->thread)
2166 kthread_stop(ch->thread);
2167
2168 ib_destroy_qp(ch->qp);
2169 ib_destroy_cq(ch->cq);
2170}
2171
2172/**
2173 * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2174 *
2175 * Reset the QP and make sure all resources associated with the channel will
2176 * be deallocated at an appropriate time.
2177 *
2178 * Note: The caller must hold ch->sport->sdev->spinlock.
2179 */
2180static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2181{
2182 struct srpt_device *sdev;
2183 enum rdma_ch_state prev_state;
2184 unsigned long flags;
2185
2186 sdev = ch->sport->sdev;
2187
2188 spin_lock_irqsave(&ch->spinlock, flags);
2189 prev_state = ch->state;
2190 switch (prev_state) {
2191 case CH_CONNECTING:
2192 case CH_LIVE:
2193 ch->state = CH_DISCONNECTING;
2194 break;
2195 default:
2196 break;
2197 }
2198 spin_unlock_irqrestore(&ch->spinlock, flags);
2199
2200 switch (prev_state) {
2201 case CH_CONNECTING:
2202 ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2203 NULL, 0);
2204 /* fall through */
2205 case CH_LIVE:
2206 if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
2207 printk(KERN_ERR "sending CM DREQ failed.\n");
2208 break;
2209 case CH_DISCONNECTING:
2210 break;
2211 case CH_DRAINING:
2212 case CH_RELEASING:
2213 break;
2214 }
2215}
2216
2217/**
2218 * srpt_close_ch() - Close an RDMA channel.
2219 */
2220static void srpt_close_ch(struct srpt_rdma_ch *ch)
2221{
2222 struct srpt_device *sdev;
2223
2224 sdev = ch->sport->sdev;
2225 spin_lock_irq(&sdev->spinlock);
2226 __srpt_close_ch(ch);
2227 spin_unlock_irq(&sdev->spinlock);
2228}
2229
2230/**
2231 * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2232 * @cm_id: Pointer to the CM ID of the channel to be drained.
2233 *
2234 * Note: Must be called from inside srpt_cm_handler to avoid a race between
2235 * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2236 * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2237 * waits until all target sessions for the associated IB device have been
2238 * unregistered and target session registration involves a call to
2239 * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2240 * this function has finished).
2241 */
2242static void srpt_drain_channel(struct ib_cm_id *cm_id)
2243{
2244 struct srpt_device *sdev;
2245 struct srpt_rdma_ch *ch;
2246 int ret;
2247 bool do_reset = false;
2248
2249 WARN_ON_ONCE(irqs_disabled());
2250
2251 sdev = cm_id->context;
2252 BUG_ON(!sdev);
2253 spin_lock_irq(&sdev->spinlock);
2254 list_for_each_entry(ch, &sdev->rch_list, list) {
2255 if (ch->cm_id == cm_id) {
2256 do_reset = srpt_test_and_set_ch_state(ch,
2257 CH_CONNECTING, CH_DRAINING) ||
2258 srpt_test_and_set_ch_state(ch,
2259 CH_LIVE, CH_DRAINING) ||
2260 srpt_test_and_set_ch_state(ch,
2261 CH_DISCONNECTING, CH_DRAINING);
2262 break;
2263 }
2264 }
2265 spin_unlock_irq(&sdev->spinlock);
2266
2267 if (do_reset) {
2268 ret = srpt_ch_qp_err(ch);
2269 if (ret < 0)
2270 printk(KERN_ERR "Setting queue pair in error state"
2271 " failed: %d\n", ret);
2272 }
2273}
2274
2275/**
2276 * srpt_find_channel() - Look up an RDMA channel.
2277 * @cm_id: Pointer to the CM ID of the channel to be looked up.
2278 *
2279 * Return NULL if no matching RDMA channel has been found.
2280 */
2281static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2282 struct ib_cm_id *cm_id)
2283{
2284 struct srpt_rdma_ch *ch;
2285 bool found;
2286
2287 WARN_ON_ONCE(irqs_disabled());
2288 BUG_ON(!sdev);
2289
2290 found = false;
2291 spin_lock_irq(&sdev->spinlock);
2292 list_for_each_entry(ch, &sdev->rch_list, list) {
2293 if (ch->cm_id == cm_id) {
2294 found = true;
2295 break;
2296 }
2297 }
2298 spin_unlock_irq(&sdev->spinlock);
2299
2300 return found ? ch : NULL;
2301}
2302
2303/**
2304 * srpt_release_channel() - Release channel resources.
2305 *
2306 * Schedules the actual release because:
2307 * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2308 * trigger a deadlock.
2309 * - It is not safe to call TCM transport_* functions from interrupt context.
2310 */
2311static void srpt_release_channel(struct srpt_rdma_ch *ch)
2312{
2313 schedule_work(&ch->release_work);
2314}
2315
2316static void srpt_release_channel_work(struct work_struct *w)
2317{
2318 struct srpt_rdma_ch *ch;
2319 struct srpt_device *sdev;
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002320 struct se_session *se_sess;
Bart Van Asschea42d9852011-10-14 01:30:46 +00002321
2322 ch = container_of(w, struct srpt_rdma_ch, release_work);
2323 pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2324 ch->release_done);
2325
2326 sdev = ch->sport->sdev;
2327 BUG_ON(!sdev);
2328
Nicholas Bellinger9474b042012-11-27 23:55:57 -08002329 se_sess = ch->sess;
2330 BUG_ON(!se_sess);
2331
2332 target_wait_for_sess_cmds(se_sess, 0);
2333
2334 transport_deregister_session_configfs(se_sess);
2335 transport_deregister_session(se_sess);
Bart Van Asschea42d9852011-10-14 01:30:46 +00002336 ch->sess = NULL;
2337
2338 srpt_destroy_ch_ib(ch);
2339
2340 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2341 ch->sport->sdev, ch->rq_size,
2342 ch->rsp_size, DMA_TO_DEVICE);
2343
2344 spin_lock_irq(&sdev->spinlock);
2345 list_del(&ch->list);
2346 spin_unlock_irq(&sdev->spinlock);
2347
2348 ib_destroy_cm_id(ch->cm_id);
2349
2350 if (ch->release_done)
2351 complete(ch->release_done);
2352
2353 wake_up(&sdev->ch_releaseQ);
2354
2355 kfree(ch);
2356}
2357
2358static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2359 u8 i_port_id[16])
2360{
2361 struct srpt_node_acl *nacl;
2362
2363 list_for_each_entry(nacl, &sport->port_acl_list, list)
2364 if (memcmp(nacl->i_port_id, i_port_id,
2365 sizeof(nacl->i_port_id)) == 0)
2366 return nacl;
2367
2368 return NULL;
2369}
2370
2371static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2372 u8 i_port_id[16])
2373{
2374 struct srpt_node_acl *nacl;
2375
2376 spin_lock_irq(&sport->port_acl_lock);
2377 nacl = __srpt_lookup_acl(sport, i_port_id);
2378 spin_unlock_irq(&sport->port_acl_lock);
2379
2380 return nacl;
2381}
2382
2383/**
2384 * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2385 *
2386 * Ownership of the cm_id is transferred to the target session if this
2387 * functions returns zero. Otherwise the caller remains the owner of cm_id.
2388 */
2389static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2390 struct ib_cm_req_event_param *param,
2391 void *private_data)
2392{
2393 struct srpt_device *sdev = cm_id->context;
2394 struct srpt_port *sport = &sdev->port[param->port - 1];
2395 struct srp_login_req *req;
2396 struct srp_login_rsp *rsp;
2397 struct srp_login_rej *rej;
2398 struct ib_cm_rep_param *rep_param;
2399 struct srpt_rdma_ch *ch, *tmp_ch;
2400 struct srpt_node_acl *nacl;
2401 u32 it_iu_len;
2402 int i;
2403 int ret = 0;
2404
2405 WARN_ON_ONCE(irqs_disabled());
2406
2407 if (WARN_ON(!sdev || !private_data))
2408 return -EINVAL;
2409
2410 req = (struct srp_login_req *)private_data;
2411
2412 it_iu_len = be32_to_cpu(req->req_it_iu_len);
2413
2414 printk(KERN_INFO "Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2415 " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2416 " (guid=0x%llx:0x%llx)\n",
2417 be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2418 be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2419 be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2420 be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2421 it_iu_len,
2422 param->port,
2423 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2424 be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
2425
2426 rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2427 rej = kzalloc(sizeof *rej, GFP_KERNEL);
2428 rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2429
2430 if (!rsp || !rej || !rep_param) {
2431 ret = -ENOMEM;
2432 goto out;
2433 }
2434
2435 if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2436 rej->reason = __constant_cpu_to_be32(
2437 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2438 ret = -EINVAL;
2439 printk(KERN_ERR "rejected SRP_LOGIN_REQ because its"
2440 " length (%d bytes) is out of range (%d .. %d)\n",
2441 it_iu_len, 64, srp_max_req_size);
2442 goto reject;
2443 }
2444
2445 if (!sport->enabled) {
2446 rej->reason = __constant_cpu_to_be32(
2447 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2448 ret = -EINVAL;
2449 printk(KERN_ERR "rejected SRP_LOGIN_REQ because the target port"
2450 " has not yet been enabled\n");
2451 goto reject;
2452 }
2453
2454 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2455 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2456
2457 spin_lock_irq(&sdev->spinlock);
2458
2459 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2460 if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2461 && !memcmp(ch->t_port_id, req->target_port_id, 16)
2462 && param->port == ch->sport->port
2463 && param->listen_id == ch->sport->sdev->cm_id
2464 && ch->cm_id) {
2465 enum rdma_ch_state ch_state;
2466
2467 ch_state = srpt_get_ch_state(ch);
2468 if (ch_state != CH_CONNECTING
2469 && ch_state != CH_LIVE)
2470 continue;
2471
2472 /* found an existing channel */
2473 pr_debug("Found existing channel %s"
2474 " cm_id= %p state= %d\n",
2475 ch->sess_name, ch->cm_id, ch_state);
2476
2477 __srpt_close_ch(ch);
2478
2479 rsp->rsp_flags =
2480 SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2481 }
2482 }
2483
2484 spin_unlock_irq(&sdev->spinlock);
2485
2486 } else
2487 rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2488
2489 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2490 || *(__be64 *)(req->target_port_id + 8) !=
2491 cpu_to_be64(srpt_service_guid)) {
2492 rej->reason = __constant_cpu_to_be32(
2493 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2494 ret = -ENOMEM;
2495 printk(KERN_ERR "rejected SRP_LOGIN_REQ because it"
2496 " has an invalid target port identifier.\n");
2497 goto reject;
2498 }
2499
2500 ch = kzalloc(sizeof *ch, GFP_KERNEL);
2501 if (!ch) {
2502 rej->reason = __constant_cpu_to_be32(
2503 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2504 printk(KERN_ERR "rejected SRP_LOGIN_REQ because no memory.\n");
2505 ret = -ENOMEM;
2506 goto reject;
2507 }
2508
2509 INIT_WORK(&ch->release_work, srpt_release_channel_work);
2510 memcpy(ch->i_port_id, req->initiator_port_id, 16);
2511 memcpy(ch->t_port_id, req->target_port_id, 16);
2512 ch->sport = &sdev->port[param->port - 1];
2513 ch->cm_id = cm_id;
2514 /*
2515 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2516 * for the SRP protocol to the command queue size.
2517 */
2518 ch->rq_size = SRPT_RQ_SIZE;
2519 spin_lock_init(&ch->spinlock);
2520 ch->state = CH_CONNECTING;
2521 INIT_LIST_HEAD(&ch->cmd_wait_list);
2522 ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2523
2524 ch->ioctx_ring = (struct srpt_send_ioctx **)
2525 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2526 sizeof(*ch->ioctx_ring[0]),
2527 ch->rsp_size, DMA_TO_DEVICE);
2528 if (!ch->ioctx_ring)
2529 goto free_ch;
2530
2531 INIT_LIST_HEAD(&ch->free_list);
2532 for (i = 0; i < ch->rq_size; i++) {
2533 ch->ioctx_ring[i]->ch = ch;
2534 list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2535 }
2536
2537 ret = srpt_create_ch_ib(ch);
2538 if (ret) {
2539 rej->reason = __constant_cpu_to_be32(
2540 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2541 printk(KERN_ERR "rejected SRP_LOGIN_REQ because creating"
2542 " a new RDMA channel failed.\n");
2543 goto free_ring;
2544 }
2545
2546 ret = srpt_ch_qp_rtr(ch, ch->qp);
2547 if (ret) {
2548 rej->reason = __constant_cpu_to_be32(
2549 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2550 printk(KERN_ERR "rejected SRP_LOGIN_REQ because enabling"
2551 " RTR failed (error code = %d)\n", ret);
2552 goto destroy_ib;
2553 }
2554 /*
2555 * Use the initator port identifier as the session name.
2556 */
2557 snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2558 be64_to_cpu(*(__be64 *)ch->i_port_id),
2559 be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2560
2561 pr_debug("registering session %s\n", ch->sess_name);
2562
2563 nacl = srpt_lookup_acl(sport, ch->i_port_id);
2564 if (!nacl) {
2565 printk(KERN_INFO "Rejected login because no ACL has been"
2566 " configured yet for initiator %s.\n", ch->sess_name);
2567 rej->reason = __constant_cpu_to_be32(
2568 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2569 goto destroy_ib;
2570 }
2571
2572 ch->sess = transport_init_session();
Dan Carpenter3af33632011-11-04 21:27:32 +03002573 if (IS_ERR(ch->sess)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00002574 rej->reason = __constant_cpu_to_be32(
2575 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2576 pr_debug("Failed to create session\n");
2577 goto deregister_session;
2578 }
2579 ch->sess->se_node_acl = &nacl->nacl;
2580 transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2581
2582 pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2583 ch->sess_name, ch->cm_id);
2584
2585 /* create srp_login_response */
2586 rsp->opcode = SRP_LOGIN_RSP;
2587 rsp->tag = req->tag;
2588 rsp->max_it_iu_len = req->req_it_iu_len;
2589 rsp->max_ti_iu_len = req->req_it_iu_len;
2590 ch->max_ti_iu_len = it_iu_len;
2591 rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2592 | SRP_BUF_FORMAT_INDIRECT);
2593 rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2594 atomic_set(&ch->req_lim, ch->rq_size);
2595 atomic_set(&ch->req_lim_delta, 0);
2596
2597 /* create cm reply */
2598 rep_param->qp_num = ch->qp->qp_num;
2599 rep_param->private_data = (void *)rsp;
2600 rep_param->private_data_len = sizeof *rsp;
2601 rep_param->rnr_retry_count = 7;
2602 rep_param->flow_control = 1;
2603 rep_param->failover_accepted = 0;
2604 rep_param->srq = 1;
2605 rep_param->responder_resources = 4;
2606 rep_param->initiator_depth = 4;
2607
2608 ret = ib_send_cm_rep(cm_id, rep_param);
2609 if (ret) {
2610 printk(KERN_ERR "sending SRP_LOGIN_REQ response failed"
2611 " (error code = %d)\n", ret);
2612 goto release_channel;
2613 }
2614
2615 spin_lock_irq(&sdev->spinlock);
2616 list_add_tail(&ch->list, &sdev->rch_list);
2617 spin_unlock_irq(&sdev->spinlock);
2618
2619 goto out;
2620
2621release_channel:
2622 srpt_set_ch_state(ch, CH_RELEASING);
2623 transport_deregister_session_configfs(ch->sess);
2624
2625deregister_session:
2626 transport_deregister_session(ch->sess);
2627 ch->sess = NULL;
2628
2629destroy_ib:
2630 srpt_destroy_ch_ib(ch);
2631
2632free_ring:
2633 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2634 ch->sport->sdev, ch->rq_size,
2635 ch->rsp_size, DMA_TO_DEVICE);
2636free_ch:
2637 kfree(ch);
2638
2639reject:
2640 rej->opcode = SRP_LOGIN_REJ;
2641 rej->tag = req->tag;
2642 rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2643 | SRP_BUF_FORMAT_INDIRECT);
2644
2645 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2646 (void *)rej, sizeof *rej);
2647
2648out:
2649 kfree(rep_param);
2650 kfree(rsp);
2651 kfree(rej);
2652
2653 return ret;
2654}
2655
2656static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2657{
2658 printk(KERN_INFO "Received IB REJ for cm_id %p.\n", cm_id);
2659 srpt_drain_channel(cm_id);
2660}
2661
2662/**
2663 * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2664 *
2665 * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2666 * and that the recipient may begin transmitting (RTU = ready to use).
2667 */
2668static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2669{
2670 struct srpt_rdma_ch *ch;
2671 int ret;
2672
2673 ch = srpt_find_channel(cm_id->context, cm_id);
2674 BUG_ON(!ch);
2675
2676 if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2677 struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2678
2679 ret = srpt_ch_qp_rts(ch, ch->qp);
2680
2681 list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2682 wait_list) {
2683 list_del(&ioctx->wait_list);
2684 srpt_handle_new_iu(ch, ioctx, NULL);
2685 }
2686 if (ret)
2687 srpt_close_ch(ch);
2688 }
2689}
2690
2691static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2692{
2693 printk(KERN_INFO "Received IB TimeWait exit for cm_id %p.\n", cm_id);
2694 srpt_drain_channel(cm_id);
2695}
2696
2697static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2698{
2699 printk(KERN_INFO "Received IB REP error for cm_id %p.\n", cm_id);
2700 srpt_drain_channel(cm_id);
2701}
2702
2703/**
2704 * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2705 */
2706static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2707{
2708 struct srpt_rdma_ch *ch;
2709 unsigned long flags;
2710 bool send_drep = false;
2711
2712 ch = srpt_find_channel(cm_id->context, cm_id);
2713 BUG_ON(!ch);
2714
2715 pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2716
2717 spin_lock_irqsave(&ch->spinlock, flags);
2718 switch (ch->state) {
2719 case CH_CONNECTING:
2720 case CH_LIVE:
2721 send_drep = true;
2722 ch->state = CH_DISCONNECTING;
2723 break;
2724 case CH_DISCONNECTING:
2725 case CH_DRAINING:
2726 case CH_RELEASING:
2727 WARN(true, "unexpected channel state %d\n", ch->state);
2728 break;
2729 }
2730 spin_unlock_irqrestore(&ch->spinlock, flags);
2731
2732 if (send_drep) {
2733 if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
2734 printk(KERN_ERR "Sending IB DREP failed.\n");
2735 printk(KERN_INFO "Received DREQ and sent DREP for session %s.\n",
2736 ch->sess_name);
2737 }
2738}
2739
2740/**
2741 * srpt_cm_drep_recv() - Process reception of a DREP message.
2742 */
2743static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2744{
2745 printk(KERN_INFO "Received InfiniBand DREP message for cm_id %p.\n",
2746 cm_id);
2747 srpt_drain_channel(cm_id);
2748}
2749
2750/**
2751 * srpt_cm_handler() - IB connection manager callback function.
2752 *
2753 * A non-zero return value will cause the caller destroy the CM ID.
2754 *
2755 * Note: srpt_cm_handler() must only return a non-zero value when transferring
2756 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2757 * a non-zero value in any other case will trigger a race with the
2758 * ib_destroy_cm_id() call in srpt_release_channel().
2759 */
2760static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2761{
2762 int ret;
2763
2764 ret = 0;
2765 switch (event->event) {
2766 case IB_CM_REQ_RECEIVED:
2767 ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2768 event->private_data);
2769 break;
2770 case IB_CM_REJ_RECEIVED:
2771 srpt_cm_rej_recv(cm_id);
2772 break;
2773 case IB_CM_RTU_RECEIVED:
2774 case IB_CM_USER_ESTABLISHED:
2775 srpt_cm_rtu_recv(cm_id);
2776 break;
2777 case IB_CM_DREQ_RECEIVED:
2778 srpt_cm_dreq_recv(cm_id);
2779 break;
2780 case IB_CM_DREP_RECEIVED:
2781 srpt_cm_drep_recv(cm_id);
2782 break;
2783 case IB_CM_TIMEWAIT_EXIT:
2784 srpt_cm_timewait_exit(cm_id);
2785 break;
2786 case IB_CM_REP_ERROR:
2787 srpt_cm_rep_error(cm_id);
2788 break;
2789 case IB_CM_DREQ_ERROR:
2790 printk(KERN_INFO "Received IB DREQ ERROR event.\n");
2791 break;
2792 case IB_CM_MRA_RECEIVED:
2793 printk(KERN_INFO "Received IB MRA event\n");
2794 break;
2795 default:
2796 printk(KERN_ERR "received unrecognized IB CM event %d\n",
2797 event->event);
2798 break;
2799 }
2800
2801 return ret;
2802}
2803
2804/**
2805 * srpt_perform_rdmas() - Perform IB RDMA.
2806 *
2807 * Returns zero upon success or a negative number upon failure.
2808 */
2809static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2810 struct srpt_send_ioctx *ioctx)
2811{
2812 struct ib_send_wr wr;
2813 struct ib_send_wr *bad_wr;
2814 struct rdma_iu *riu;
2815 int i;
2816 int ret;
2817 int sq_wr_avail;
2818 enum dma_data_direction dir;
2819 const int n_rdma = ioctx->n_rdma;
2820
2821 dir = ioctx->cmd.data_direction;
2822 if (dir == DMA_TO_DEVICE) {
2823 /* write */
2824 ret = -ENOMEM;
2825 sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2826 if (sq_wr_avail < 0) {
2827 printk(KERN_WARNING "IB send queue full (needed %d)\n",
2828 n_rdma);
2829 goto out;
2830 }
2831 }
2832
2833 ioctx->rdma_aborted = false;
2834 ret = 0;
2835 riu = ioctx->rdma_ius;
2836 memset(&wr, 0, sizeof wr);
2837
2838 for (i = 0; i < n_rdma; ++i, ++riu) {
2839 if (dir == DMA_FROM_DEVICE) {
2840 wr.opcode = IB_WR_RDMA_WRITE;
2841 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2842 SRPT_RDMA_WRITE_LAST :
2843 SRPT_RDMA_MID,
2844 ioctx->ioctx.index);
2845 } else {
2846 wr.opcode = IB_WR_RDMA_READ;
2847 wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2848 SRPT_RDMA_READ_LAST :
2849 SRPT_RDMA_MID,
2850 ioctx->ioctx.index);
2851 }
2852 wr.next = NULL;
2853 wr.wr.rdma.remote_addr = riu->raddr;
2854 wr.wr.rdma.rkey = riu->rkey;
2855 wr.num_sge = riu->sge_cnt;
2856 wr.sg_list = riu->sge;
2857
2858 /* only get completion event for the last rdma write */
2859 if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
2860 wr.send_flags = IB_SEND_SIGNALED;
2861
2862 ret = ib_post_send(ch->qp, &wr, &bad_wr);
2863 if (ret)
2864 break;
2865 }
2866
2867 if (ret)
2868 printk(KERN_ERR "%s[%d]: ib_post_send() returned %d for %d/%d",
2869 __func__, __LINE__, ret, i, n_rdma);
2870 if (ret && i > 0) {
2871 wr.num_sge = 0;
2872 wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2873 wr.send_flags = IB_SEND_SIGNALED;
2874 while (ch->state == CH_LIVE &&
2875 ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
2876 printk(KERN_INFO "Trying to abort failed RDMA transfer [%d]",
2877 ioctx->ioctx.index);
2878 msleep(1000);
2879 }
2880 while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
2881 printk(KERN_INFO "Waiting until RDMA abort finished [%d]",
2882 ioctx->ioctx.index);
2883 msleep(1000);
2884 }
2885 }
2886out:
2887 if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2888 atomic_add(n_rdma, &ch->sq_wr_avail);
2889 return ret;
2890}
2891
2892/**
2893 * srpt_xfer_data() - Start data transfer from initiator to target.
2894 */
2895static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2896 struct srpt_send_ioctx *ioctx)
2897{
2898 int ret;
2899
2900 ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2901 if (ret) {
2902 printk(KERN_ERR "%s[%d] ret=%d\n", __func__, __LINE__, ret);
2903 goto out;
2904 }
2905
2906 ret = srpt_perform_rdmas(ch, ioctx);
2907 if (ret) {
2908 if (ret == -EAGAIN || ret == -ENOMEM)
2909 printk(KERN_INFO "%s[%d] queue full -- ret=%d\n",
2910 __func__, __LINE__, ret);
2911 else
2912 printk(KERN_ERR "%s[%d] fatal error -- ret=%d\n",
2913 __func__, __LINE__, ret);
2914 goto out_unmap;
2915 }
2916
2917out:
2918 return ret;
2919out_unmap:
2920 srpt_unmap_sg_to_ib_sge(ch, ioctx);
2921 goto out;
2922}
2923
2924static int srpt_write_pending_status(struct se_cmd *se_cmd)
2925{
2926 struct srpt_send_ioctx *ioctx;
2927
2928 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2929 return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2930}
2931
2932/*
2933 * srpt_write_pending() - Start data transfer from initiator to target (write).
2934 */
2935static int srpt_write_pending(struct se_cmd *se_cmd)
2936{
2937 struct srpt_rdma_ch *ch;
2938 struct srpt_send_ioctx *ioctx;
2939 enum srpt_command_state new_state;
2940 enum rdma_ch_state ch_state;
2941 int ret;
2942
2943 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2944
2945 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2946 WARN_ON(new_state == SRPT_STATE_DONE);
2947
2948 ch = ioctx->ch;
2949 BUG_ON(!ch);
2950
2951 ch_state = srpt_get_ch_state(ch);
2952 switch (ch_state) {
2953 case CH_CONNECTING:
2954 WARN(true, "unexpected channel state %d\n", ch_state);
2955 ret = -EINVAL;
2956 goto out;
2957 case CH_LIVE:
2958 break;
2959 case CH_DISCONNECTING:
2960 case CH_DRAINING:
2961 case CH_RELEASING:
2962 pr_debug("cmd with tag %lld: channel disconnecting\n",
2963 ioctx->tag);
2964 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2965 ret = -EINVAL;
2966 goto out;
2967 }
2968 ret = srpt_xfer_data(ch, ioctx);
2969
2970out:
2971 return ret;
2972}
2973
2974static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2975{
2976 switch (tcm_mgmt_status) {
2977 case TMR_FUNCTION_COMPLETE:
2978 return SRP_TSK_MGMT_SUCCESS;
2979 case TMR_FUNCTION_REJECTED:
2980 return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2981 }
2982 return SRP_TSK_MGMT_FAILED;
2983}
2984
2985/**
2986 * srpt_queue_response() - Transmits the response to a SCSI command.
2987 *
2988 * Callback function called by the TCM core. Must not block since it can be
2989 * invoked on the context of the IB completion handler.
2990 */
2991static int srpt_queue_response(struct se_cmd *cmd)
2992{
2993 struct srpt_rdma_ch *ch;
2994 struct srpt_send_ioctx *ioctx;
2995 enum srpt_command_state state;
2996 unsigned long flags;
2997 int ret;
2998 enum dma_data_direction dir;
2999 int resp_len;
3000 u8 srp_tm_status;
3001
3002 ret = 0;
3003
3004 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3005 ch = ioctx->ch;
3006 BUG_ON(!ch);
3007
3008 spin_lock_irqsave(&ioctx->spinlock, flags);
3009 state = ioctx->state;
3010 switch (state) {
3011 case SRPT_STATE_NEW:
3012 case SRPT_STATE_DATA_IN:
3013 ioctx->state = SRPT_STATE_CMD_RSP_SENT;
3014 break;
3015 case SRPT_STATE_MGMT:
3016 ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
3017 break;
3018 default:
3019 WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
3020 ch, ioctx->ioctx.index, ioctx->state);
3021 break;
3022 }
3023 spin_unlock_irqrestore(&ioctx->spinlock, flags);
3024
3025 if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
3026 || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
3027 atomic_inc(&ch->req_lim_delta);
3028 srpt_abort_cmd(ioctx);
3029 goto out;
3030 }
3031
3032 dir = ioctx->cmd.data_direction;
3033
3034 /* For read commands, transfer the data to the initiator. */
3035 if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
3036 !ioctx->queue_status_only) {
3037 ret = srpt_xfer_data(ch, ioctx);
3038 if (ret) {
3039 printk(KERN_ERR "xfer_data failed for tag %llu\n",
3040 ioctx->tag);
3041 goto out;
3042 }
3043 }
3044
3045 if (state != SRPT_STATE_MGMT)
3046 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
3047 cmd->scsi_status);
3048 else {
3049 srp_tm_status
3050 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3051 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
3052 ioctx->tag);
3053 }
3054 ret = srpt_post_send(ch, ioctx, resp_len);
3055 if (ret) {
3056 printk(KERN_ERR "sending cmd response failed for tag %llu\n",
3057 ioctx->tag);
3058 srpt_unmap_sg_to_ib_sge(ch, ioctx);
3059 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003060 target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003061 }
3062
3063out:
3064 return ret;
3065}
3066
3067static int srpt_queue_status(struct se_cmd *cmd)
3068{
3069 struct srpt_send_ioctx *ioctx;
3070
3071 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3072 BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3073 if (cmd->se_cmd_flags &
3074 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3075 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3076 ioctx->queue_status_only = true;
3077 return srpt_queue_response(cmd);
3078}
3079
3080static void srpt_refresh_port_work(struct work_struct *work)
3081{
3082 struct srpt_port *sport = container_of(work, struct srpt_port, work);
3083
3084 srpt_refresh_port(sport);
3085}
3086
3087static int srpt_ch_list_empty(struct srpt_device *sdev)
3088{
3089 int res;
3090
3091 spin_lock_irq(&sdev->spinlock);
3092 res = list_empty(&sdev->rch_list);
3093 spin_unlock_irq(&sdev->spinlock);
3094
3095 return res;
3096}
3097
3098/**
3099 * srpt_release_sdev() - Free the channel resources associated with a target.
3100 */
3101static int srpt_release_sdev(struct srpt_device *sdev)
3102{
3103 struct srpt_rdma_ch *ch, *tmp_ch;
3104 int res;
3105
3106 WARN_ON_ONCE(irqs_disabled());
3107
3108 BUG_ON(!sdev);
3109
3110 spin_lock_irq(&sdev->spinlock);
3111 list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3112 __srpt_close_ch(ch);
3113 spin_unlock_irq(&sdev->spinlock);
3114
3115 res = wait_event_interruptible(sdev->ch_releaseQ,
3116 srpt_ch_list_empty(sdev));
3117 if (res)
3118 printk(KERN_ERR "%s: interrupted.\n", __func__);
3119
3120 return 0;
3121}
3122
3123static struct srpt_port *__srpt_lookup_port(const char *name)
3124{
3125 struct ib_device *dev;
3126 struct srpt_device *sdev;
3127 struct srpt_port *sport;
3128 int i;
3129
3130 list_for_each_entry(sdev, &srpt_dev_list, list) {
3131 dev = sdev->device;
3132 if (!dev)
3133 continue;
3134
3135 for (i = 0; i < dev->phys_port_cnt; i++) {
3136 sport = &sdev->port[i];
3137
3138 if (!strcmp(sport->port_guid, name))
3139 return sport;
3140 }
3141 }
3142
3143 return NULL;
3144}
3145
3146static struct srpt_port *srpt_lookup_port(const char *name)
3147{
3148 struct srpt_port *sport;
3149
3150 spin_lock(&srpt_dev_lock);
3151 sport = __srpt_lookup_port(name);
3152 spin_unlock(&srpt_dev_lock);
3153
3154 return sport;
3155}
3156
3157/**
3158 * srpt_add_one() - Infiniband device addition callback function.
3159 */
3160static void srpt_add_one(struct ib_device *device)
3161{
3162 struct srpt_device *sdev;
3163 struct srpt_port *sport;
3164 struct ib_srq_init_attr srq_attr;
3165 int i;
3166
3167 pr_debug("device = %p, device->dma_ops = %p\n", device,
3168 device->dma_ops);
3169
3170 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3171 if (!sdev)
3172 goto err;
3173
3174 sdev->device = device;
3175 INIT_LIST_HEAD(&sdev->rch_list);
3176 init_waitqueue_head(&sdev->ch_releaseQ);
3177 spin_lock_init(&sdev->spinlock);
3178
3179 if (ib_query_device(device, &sdev->dev_attr))
3180 goto free_dev;
3181
3182 sdev->pd = ib_alloc_pd(device);
3183 if (IS_ERR(sdev->pd))
3184 goto free_dev;
3185
3186 sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
3187 if (IS_ERR(sdev->mr))
3188 goto err_pd;
3189
3190 sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3191
3192 srq_attr.event_handler = srpt_srq_event;
3193 srq_attr.srq_context = (void *)sdev;
3194 srq_attr.attr.max_wr = sdev->srq_size;
3195 srq_attr.attr.max_sge = 1;
3196 srq_attr.attr.srq_limit = 0;
Roland Dreier6f360332012-04-12 07:51:08 -07003197 srq_attr.srq_type = IB_SRQT_BASIC;
Bart Van Asschea42d9852011-10-14 01:30:46 +00003198
3199 sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3200 if (IS_ERR(sdev->srq))
3201 goto err_mr;
3202
3203 pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3204 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3205 device->name);
3206
3207 if (!srpt_service_guid)
3208 srpt_service_guid = be64_to_cpu(device->node_guid);
3209
3210 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3211 if (IS_ERR(sdev->cm_id))
3212 goto err_srq;
3213
3214 /* print out target login information */
3215 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3216 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3217 srpt_service_guid, srpt_service_guid);
3218
3219 /*
3220 * We do not have a consistent service_id (ie. also id_ext of target_id)
3221 * to identify this target. We currently use the guid of the first HCA
3222 * in the system as service_id; therefore, the target_id will change
3223 * if this HCA is gone bad and replaced by different HCA
3224 */
3225 if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
3226 goto err_cm;
3227
3228 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3229 srpt_event_handler);
3230 if (ib_register_event_handler(&sdev->event_handler))
3231 goto err_cm;
3232
3233 sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3234 srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3235 sizeof(*sdev->ioctx_ring[0]),
3236 srp_max_req_size, DMA_FROM_DEVICE);
3237 if (!sdev->ioctx_ring)
3238 goto err_event;
3239
3240 for (i = 0; i < sdev->srq_size; ++i)
3241 srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3242
Roland Dreierf2250662012-02-02 12:55:58 -08003243 WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
Bart Van Asschea42d9852011-10-14 01:30:46 +00003244
3245 for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3246 sport = &sdev->port[i - 1];
3247 sport->sdev = sdev;
3248 sport->port = i;
3249 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3250 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3251 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3252 INIT_WORK(&sport->work, srpt_refresh_port_work);
3253 INIT_LIST_HEAD(&sport->port_acl_list);
3254 spin_lock_init(&sport->port_acl_lock);
3255
3256 if (srpt_refresh_port(sport)) {
3257 printk(KERN_ERR "MAD registration failed for %s-%d.\n",
3258 srpt_sdev_name(sdev), i);
3259 goto err_ring;
3260 }
3261 snprintf(sport->port_guid, sizeof(sport->port_guid),
3262 "0x%016llx%016llx",
3263 be64_to_cpu(sport->gid.global.subnet_prefix),
3264 be64_to_cpu(sport->gid.global.interface_id));
3265 }
3266
3267 spin_lock(&srpt_dev_lock);
3268 list_add_tail(&sdev->list, &srpt_dev_list);
3269 spin_unlock(&srpt_dev_lock);
3270
3271out:
3272 ib_set_client_data(device, &srpt_client, sdev);
3273 pr_debug("added %s.\n", device->name);
3274 return;
3275
3276err_ring:
3277 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3278 sdev->srq_size, srp_max_req_size,
3279 DMA_FROM_DEVICE);
3280err_event:
3281 ib_unregister_event_handler(&sdev->event_handler);
3282err_cm:
3283 ib_destroy_cm_id(sdev->cm_id);
3284err_srq:
3285 ib_destroy_srq(sdev->srq);
3286err_mr:
3287 ib_dereg_mr(sdev->mr);
3288err_pd:
3289 ib_dealloc_pd(sdev->pd);
3290free_dev:
3291 kfree(sdev);
3292err:
3293 sdev = NULL;
3294 printk(KERN_INFO "%s(%s) failed.\n", __func__, device->name);
3295 goto out;
3296}
3297
3298/**
3299 * srpt_remove_one() - InfiniBand device removal callback function.
3300 */
3301static void srpt_remove_one(struct ib_device *device)
3302{
3303 struct srpt_device *sdev;
3304 int i;
3305
3306 sdev = ib_get_client_data(device, &srpt_client);
3307 if (!sdev) {
3308 printk(KERN_INFO "%s(%s): nothing to do.\n", __func__,
3309 device->name);
3310 return;
3311 }
3312
3313 srpt_unregister_mad_agent(sdev);
3314
3315 ib_unregister_event_handler(&sdev->event_handler);
3316
3317 /* Cancel any work queued by the just unregistered IB event handler. */
3318 for (i = 0; i < sdev->device->phys_port_cnt; i++)
3319 cancel_work_sync(&sdev->port[i].work);
3320
3321 ib_destroy_cm_id(sdev->cm_id);
3322
3323 /*
3324 * Unregistering a target must happen after destroying sdev->cm_id
3325 * such that no new SRP_LOGIN_REQ information units can arrive while
3326 * destroying the target.
3327 */
3328 spin_lock(&srpt_dev_lock);
3329 list_del(&sdev->list);
3330 spin_unlock(&srpt_dev_lock);
3331 srpt_release_sdev(sdev);
3332
3333 ib_destroy_srq(sdev->srq);
3334 ib_dereg_mr(sdev->mr);
3335 ib_dealloc_pd(sdev->pd);
3336
3337 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3338 sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3339 sdev->ioctx_ring = NULL;
3340 kfree(sdev);
3341}
3342
3343static struct ib_client srpt_client = {
3344 .name = DRV_NAME,
3345 .add = srpt_add_one,
3346 .remove = srpt_remove_one
3347};
3348
3349static int srpt_check_true(struct se_portal_group *se_tpg)
3350{
3351 return 1;
3352}
3353
3354static int srpt_check_false(struct se_portal_group *se_tpg)
3355{
3356 return 0;
3357}
3358
3359static char *srpt_get_fabric_name(void)
3360{
3361 return "srpt";
3362}
3363
3364static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
3365{
3366 return SCSI_TRANSPORTID_PROTOCOLID_SRP;
3367}
3368
3369static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3370{
3371 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3372
3373 return sport->port_guid;
3374}
3375
3376static u16 srpt_get_tag(struct se_portal_group *tpg)
3377{
3378 return 1;
3379}
3380
3381static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
3382{
3383 return 1;
3384}
3385
3386static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
3387 struct se_node_acl *se_nacl,
3388 struct t10_pr_registration *pr_reg,
3389 int *format_code, unsigned char *buf)
3390{
3391 struct srpt_node_acl *nacl;
3392 struct spc_rdma_transport_id *tr_id;
3393
3394 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3395 tr_id = (void *)buf;
3396 tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
3397 memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
3398 return sizeof(*tr_id);
3399}
3400
3401static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
3402 struct se_node_acl *se_nacl,
3403 struct t10_pr_registration *pr_reg,
3404 int *format_code)
3405{
3406 *format_code = 0;
3407 return sizeof(struct spc_rdma_transport_id);
3408}
3409
3410static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
3411 const char *buf, u32 *out_tid_len,
3412 char **port_nexus_ptr)
3413{
3414 struct spc_rdma_transport_id *tr_id;
3415
3416 *port_nexus_ptr = NULL;
3417 *out_tid_len = sizeof(struct spc_rdma_transport_id);
3418 tr_id = (void *)buf;
3419 return (char *)tr_id->i_port_id;
3420}
3421
3422static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
3423{
3424 struct srpt_node_acl *nacl;
3425
3426 nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
3427 if (!nacl) {
Masanari Iida7367d992012-02-09 23:37:43 +09003428 printk(KERN_ERR "Unable to allocate struct srpt_node_acl\n");
Bart Van Asschea42d9852011-10-14 01:30:46 +00003429 return NULL;
3430 }
3431
3432 return &nacl->nacl;
3433}
3434
3435static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
3436 struct se_node_acl *se_nacl)
3437{
3438 struct srpt_node_acl *nacl;
3439
3440 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3441 kfree(nacl);
3442}
3443
3444static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3445{
3446 return 1;
3447}
3448
3449static void srpt_release_cmd(struct se_cmd *se_cmd)
3450{
Nicholas Bellinger9474b042012-11-27 23:55:57 -08003451 struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3452 struct srpt_send_ioctx, cmd);
3453 struct srpt_rdma_ch *ch = ioctx->ch;
3454 unsigned long flags;
3455
3456 WARN_ON(ioctx->state != SRPT_STATE_DONE);
3457 WARN_ON(ioctx->mapped_sg_count != 0);
3458
3459 if (ioctx->n_rbuf > 1) {
3460 kfree(ioctx->rbufs);
3461 ioctx->rbufs = NULL;
3462 ioctx->n_rbuf = 0;
3463 }
3464
3465 spin_lock_irqsave(&ch->spinlock, flags);
3466 list_add(&ioctx->free_list, &ch->free_list);
3467 spin_unlock_irqrestore(&ch->spinlock, flags);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003468}
3469
3470/**
3471 * srpt_shutdown_session() - Whether or not a session may be shut down.
3472 */
3473static int srpt_shutdown_session(struct se_session *se_sess)
3474{
3475 return true;
3476}
3477
3478/**
3479 * srpt_close_session() - Forcibly close a session.
3480 *
3481 * Callback function invoked by the TCM core to clean up sessions associated
3482 * with a node ACL when the user invokes
3483 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3484 */
3485static void srpt_close_session(struct se_session *se_sess)
3486{
3487 DECLARE_COMPLETION_ONSTACK(release_done);
3488 struct srpt_rdma_ch *ch;
3489 struct srpt_device *sdev;
3490 int res;
3491
3492 ch = se_sess->fabric_sess_ptr;
3493 WARN_ON(ch->sess != se_sess);
3494
3495 pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3496
3497 sdev = ch->sport->sdev;
3498 spin_lock_irq(&sdev->spinlock);
3499 BUG_ON(ch->release_done);
3500 ch->release_done = &release_done;
3501 __srpt_close_ch(ch);
3502 spin_unlock_irq(&sdev->spinlock);
3503
3504 res = wait_for_completion_timeout(&release_done, 60 * HZ);
3505 WARN_ON(res <= 0);
3506}
3507
3508/**
Bart Van Asschea42d9852011-10-14 01:30:46 +00003509 * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3510 *
3511 * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3512 * This object represents an arbitrary integer used to uniquely identify a
3513 * particular attached remote initiator port to a particular SCSI target port
3514 * within a particular SCSI target device within a particular SCSI instance.
3515 */
3516static u32 srpt_sess_get_index(struct se_session *se_sess)
3517{
3518 return 0;
3519}
3520
3521static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3522{
3523}
3524
3525static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
3526{
3527 struct srpt_send_ioctx *ioctx;
3528
3529 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3530 return ioctx->tag;
3531}
3532
3533/* Note: only used from inside debug printk's by the TCM core. */
3534static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3535{
3536 struct srpt_send_ioctx *ioctx;
3537
3538 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3539 return srpt_get_cmd_state(ioctx);
3540}
3541
Bart Van Asschea42d9852011-10-14 01:30:46 +00003542/**
3543 * srpt_parse_i_port_id() - Parse an initiator port ID.
3544 * @name: ASCII representation of a 128-bit initiator port ID.
3545 * @i_port_id: Binary 128-bit port ID.
3546 */
3547static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3548{
3549 const char *p;
3550 unsigned len, count, leading_zero_bytes;
3551 int ret, rc;
3552
3553 p = name;
3554 if (strnicmp(p, "0x", 2) == 0)
3555 p += 2;
3556 ret = -EINVAL;
3557 len = strlen(p);
3558 if (len % 2)
3559 goto out;
3560 count = min(len / 2, 16U);
3561 leading_zero_bytes = 16 - count;
3562 memset(i_port_id, 0, leading_zero_bytes);
3563 rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3564 if (rc < 0)
3565 pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3566 ret = 0;
3567out:
3568 return ret;
3569}
3570
3571/*
3572 * configfs callback function invoked for
3573 * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3574 */
3575static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
3576 struct config_group *group,
3577 const char *name)
3578{
3579 struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3580 struct se_node_acl *se_nacl, *se_nacl_new;
3581 struct srpt_node_acl *nacl;
3582 int ret = 0;
3583 u32 nexus_depth = 1;
3584 u8 i_port_id[16];
3585
3586 if (srpt_parse_i_port_id(i_port_id, name) < 0) {
3587 printk(KERN_ERR "invalid initiator port ID %s\n", name);
3588 ret = -EINVAL;
3589 goto err;
3590 }
3591
3592 se_nacl_new = srpt_alloc_fabric_acl(tpg);
3593 if (!se_nacl_new) {
3594 ret = -ENOMEM;
3595 goto err;
3596 }
3597 /*
3598 * nacl_new may be released by core_tpg_add_initiator_node_acl()
3599 * when converting a node ACL from demo mode to explict
3600 */
3601 se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
3602 nexus_depth);
3603 if (IS_ERR(se_nacl)) {
3604 ret = PTR_ERR(se_nacl);
3605 goto err;
3606 }
3607 /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
3608 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3609 memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3610 nacl->sport = sport;
3611
3612 spin_lock_irq(&sport->port_acl_lock);
3613 list_add_tail(&nacl->list, &sport->port_acl_list);
3614 spin_unlock_irq(&sport->port_acl_lock);
3615
3616 return se_nacl;
3617err:
3618 return ERR_PTR(ret);
3619}
3620
3621/*
3622 * configfs callback function invoked for
3623 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3624 */
3625static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
3626{
3627 struct srpt_node_acl *nacl;
3628 struct srpt_device *sdev;
3629 struct srpt_port *sport;
3630
3631 nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3632 sport = nacl->sport;
3633 sdev = sport->sdev;
3634 spin_lock_irq(&sport->port_acl_lock);
3635 list_del(&nacl->list);
3636 spin_unlock_irq(&sport->port_acl_lock);
3637 core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
3638 srpt_release_fabric_acl(NULL, se_nacl);
3639}
3640
3641static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
3642 struct se_portal_group *se_tpg,
3643 char *page)
3644{
3645 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3646
3647 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3648}
3649
3650static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
3651 struct se_portal_group *se_tpg,
3652 const char *page,
3653 size_t count)
3654{
3655 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3656 unsigned long val;
3657 int ret;
3658
3659 ret = strict_strtoul(page, 0, &val);
3660 if (ret < 0) {
3661 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3662 return -EINVAL;
3663 }
3664 if (val > MAX_SRPT_RDMA_SIZE) {
3665 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3666 MAX_SRPT_RDMA_SIZE);
3667 return -EINVAL;
3668 }
3669 if (val < DEFAULT_MAX_RDMA_SIZE) {
3670 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3671 val, DEFAULT_MAX_RDMA_SIZE);
3672 return -EINVAL;
3673 }
3674 sport->port_attrib.srp_max_rdma_size = val;
3675
3676 return count;
3677}
3678
3679TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
3680
3681static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
3682 struct se_portal_group *se_tpg,
3683 char *page)
3684{
3685 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3686
3687 return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3688}
3689
3690static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
3691 struct se_portal_group *se_tpg,
3692 const char *page,
3693 size_t count)
3694{
3695 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3696 unsigned long val;
3697 int ret;
3698
3699 ret = strict_strtoul(page, 0, &val);
3700 if (ret < 0) {
3701 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3702 return -EINVAL;
3703 }
3704 if (val > MAX_SRPT_RSP_SIZE) {
3705 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3706 MAX_SRPT_RSP_SIZE);
3707 return -EINVAL;
3708 }
3709 if (val < MIN_MAX_RSP_SIZE) {
3710 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3711 MIN_MAX_RSP_SIZE);
3712 return -EINVAL;
3713 }
3714 sport->port_attrib.srp_max_rsp_size = val;
3715
3716 return count;
3717}
3718
3719TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
3720
3721static ssize_t srpt_tpg_attrib_show_srp_sq_size(
3722 struct se_portal_group *se_tpg,
3723 char *page)
3724{
3725 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3726
3727 return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3728}
3729
3730static ssize_t srpt_tpg_attrib_store_srp_sq_size(
3731 struct se_portal_group *se_tpg,
3732 const char *page,
3733 size_t count)
3734{
3735 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3736 unsigned long val;
3737 int ret;
3738
3739 ret = strict_strtoul(page, 0, &val);
3740 if (ret < 0) {
3741 pr_err("strict_strtoul() failed with ret: %d\n", ret);
3742 return -EINVAL;
3743 }
3744 if (val > MAX_SRPT_SRQ_SIZE) {
3745 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3746 MAX_SRPT_SRQ_SIZE);
3747 return -EINVAL;
3748 }
3749 if (val < MIN_SRPT_SRQ_SIZE) {
3750 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3751 MIN_SRPT_SRQ_SIZE);
3752 return -EINVAL;
3753 }
3754 sport->port_attrib.srp_sq_size = val;
3755
3756 return count;
3757}
3758
3759TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
3760
3761static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3762 &srpt_tpg_attrib_srp_max_rdma_size.attr,
3763 &srpt_tpg_attrib_srp_max_rsp_size.attr,
3764 &srpt_tpg_attrib_srp_sq_size.attr,
3765 NULL,
3766};
3767
3768static ssize_t srpt_tpg_show_enable(
3769 struct se_portal_group *se_tpg,
3770 char *page)
3771{
3772 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3773
3774 return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3775}
3776
3777static ssize_t srpt_tpg_store_enable(
3778 struct se_portal_group *se_tpg,
3779 const char *page,
3780 size_t count)
3781{
3782 struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3783 unsigned long tmp;
3784 int ret;
3785
3786 ret = strict_strtoul(page, 0, &tmp);
3787 if (ret < 0) {
3788 printk(KERN_ERR "Unable to extract srpt_tpg_store_enable\n");
3789 return -EINVAL;
3790 }
3791
3792 if ((tmp != 0) && (tmp != 1)) {
3793 printk(KERN_ERR "Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3794 return -EINVAL;
3795 }
3796 if (tmp == 1)
3797 sport->enabled = true;
3798 else
3799 sport->enabled = false;
3800
3801 return count;
3802}
3803
3804TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
3805
3806static struct configfs_attribute *srpt_tpg_attrs[] = {
3807 &srpt_tpg_enable.attr,
3808 NULL,
3809};
3810
3811/**
3812 * configfs callback invoked for
3813 * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3814 */
3815static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3816 struct config_group *group,
3817 const char *name)
3818{
3819 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3820 int res;
3821
3822 /* Initialize sport->port_wwn and sport->port_tpg_1 */
3823 res = core_tpg_register(&srpt_target->tf_ops, &sport->port_wwn,
3824 &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
3825 if (res)
3826 return ERR_PTR(res);
3827
3828 return &sport->port_tpg_1;
3829}
3830
3831/**
3832 * configfs callback invoked for
3833 * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3834 */
3835static void srpt_drop_tpg(struct se_portal_group *tpg)
3836{
3837 struct srpt_port *sport = container_of(tpg,
3838 struct srpt_port, port_tpg_1);
3839
3840 sport->enabled = false;
3841 core_tpg_deregister(&sport->port_tpg_1);
3842}
3843
3844/**
3845 * configfs callback invoked for
3846 * mkdir /sys/kernel/config/target/$driver/$port
3847 */
3848static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3849 struct config_group *group,
3850 const char *name)
3851{
3852 struct srpt_port *sport;
3853 int ret;
3854
3855 sport = srpt_lookup_port(name);
3856 pr_debug("make_tport(%s)\n", name);
3857 ret = -EINVAL;
3858 if (!sport)
3859 goto err;
3860
3861 return &sport->port_wwn;
3862
3863err:
3864 return ERR_PTR(ret);
3865}
3866
3867/**
3868 * configfs callback invoked for
3869 * rmdir /sys/kernel/config/target/$driver/$port
3870 */
3871static void srpt_drop_tport(struct se_wwn *wwn)
3872{
3873 struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3874
3875 pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3876}
3877
3878static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
3879 char *buf)
3880{
3881 return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3882}
3883
3884TF_WWN_ATTR_RO(srpt, version);
3885
3886static struct configfs_attribute *srpt_wwn_attrs[] = {
3887 &srpt_wwn_version.attr,
3888 NULL,
3889};
3890
3891static struct target_core_fabric_ops srpt_template = {
3892 .get_fabric_name = srpt_get_fabric_name,
3893 .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
3894 .tpg_get_wwn = srpt_get_fabric_wwn,
3895 .tpg_get_tag = srpt_get_tag,
3896 .tpg_get_default_depth = srpt_get_default_depth,
3897 .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
3898 .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
3899 .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
3900 .tpg_check_demo_mode = srpt_check_false,
3901 .tpg_check_demo_mode_cache = srpt_check_true,
3902 .tpg_check_demo_mode_write_protect = srpt_check_true,
3903 .tpg_check_prod_mode_write_protect = srpt_check_false,
3904 .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
3905 .tpg_release_fabric_acl = srpt_release_fabric_acl,
3906 .tpg_get_inst_index = srpt_tpg_get_inst_index,
3907 .release_cmd = srpt_release_cmd,
3908 .check_stop_free = srpt_check_stop_free,
3909 .shutdown_session = srpt_shutdown_session,
3910 .close_session = srpt_close_session,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003911 .sess_get_index = srpt_sess_get_index,
3912 .sess_get_initiator_sid = NULL,
3913 .write_pending = srpt_write_pending,
3914 .write_pending_status = srpt_write_pending_status,
3915 .set_default_node_attributes = srpt_set_default_node_attrs,
3916 .get_task_tag = srpt_get_task_tag,
3917 .get_cmd_state = srpt_get_tcm_cmd_state,
3918 .queue_data_in = srpt_queue_response,
3919 .queue_status = srpt_queue_status,
3920 .queue_tm_rsp = srpt_queue_response,
Bart Van Asschea42d9852011-10-14 01:30:46 +00003921 /*
3922 * Setup function pointers for generic logic in
3923 * target_core_fabric_configfs.c
3924 */
3925 .fabric_make_wwn = srpt_make_tport,
3926 .fabric_drop_wwn = srpt_drop_tport,
3927 .fabric_make_tpg = srpt_make_tpg,
3928 .fabric_drop_tpg = srpt_drop_tpg,
3929 .fabric_post_link = NULL,
3930 .fabric_pre_unlink = NULL,
3931 .fabric_make_np = NULL,
3932 .fabric_drop_np = NULL,
3933 .fabric_make_nodeacl = srpt_make_nodeacl,
3934 .fabric_drop_nodeacl = srpt_drop_nodeacl,
3935};
3936
3937/**
3938 * srpt_init_module() - Kernel module initialization.
3939 *
3940 * Note: Since ib_register_client() registers callback functions, and since at
3941 * least one of these callback functions (srpt_add_one()) calls target core
3942 * functions, this driver must be registered with the target core before
3943 * ib_register_client() is called.
3944 */
3945static int __init srpt_init_module(void)
3946{
3947 int ret;
3948
3949 ret = -EINVAL;
3950 if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3951 printk(KERN_ERR "invalid value %d for kernel module parameter"
3952 " srp_max_req_size -- must be at least %d.\n",
3953 srp_max_req_size, MIN_MAX_REQ_SIZE);
3954 goto out;
3955 }
3956
3957 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3958 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3959 printk(KERN_ERR "invalid value %d for kernel module parameter"
3960 " srpt_srq_size -- must be in the range [%d..%d].\n",
3961 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3962 goto out;
3963 }
3964
Bart Van Asschea42d9852011-10-14 01:30:46 +00003965 srpt_target = target_fabric_configfs_init(THIS_MODULE, "srpt");
Dan Carpenter3af33632011-11-04 21:27:32 +03003966 if (IS_ERR(srpt_target)) {
Bart Van Asschea42d9852011-10-14 01:30:46 +00003967 printk(KERN_ERR "couldn't register\n");
Dan Carpenter3af33632011-11-04 21:27:32 +03003968 ret = PTR_ERR(srpt_target);
Bart Van Asschea42d9852011-10-14 01:30:46 +00003969 goto out;
3970 }
3971
3972 srpt_target->tf_ops = srpt_template;
3973
Bart Van Asschea42d9852011-10-14 01:30:46 +00003974 /*
3975 * Set up default attribute lists.
3976 */
3977 srpt_target->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = srpt_wwn_attrs;
3978 srpt_target->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = srpt_tpg_attrs;
3979 srpt_target->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = srpt_tpg_attrib_attrs;
3980 srpt_target->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
3981 srpt_target->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
3982 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
3983 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
3984 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
3985 srpt_target->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
3986
3987 ret = target_fabric_configfs_register(srpt_target);
3988 if (ret < 0) {
3989 printk(KERN_ERR "couldn't register\n");
3990 goto out_free_target;
3991 }
3992
3993 ret = ib_register_client(&srpt_client);
3994 if (ret) {
3995 printk(KERN_ERR "couldn't register IB client\n");
3996 goto out_unregister_target;
3997 }
3998
3999 return 0;
4000
4001out_unregister_target:
4002 target_fabric_configfs_deregister(srpt_target);
4003 srpt_target = NULL;
4004out_free_target:
4005 if (srpt_target)
4006 target_fabric_configfs_free(srpt_target);
4007out:
4008 return ret;
4009}
4010
4011static void __exit srpt_cleanup_module(void)
4012{
4013 ib_unregister_client(&srpt_client);
4014 target_fabric_configfs_deregister(srpt_target);
4015 srpt_target = NULL;
4016}
4017
4018module_init(srpt_init_module);
4019module_exit(srpt_cleanup_module);