1 /*
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 
62 MODULE_AUTHOR("Vu Pham and Bart Van Assche");
63 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
64 		   "v" DRV_VERSION " (" DRV_RELDATE ")");
65 MODULE_LICENSE("Dual BSD/GPL");
66 
67 /*
68  * Global Variables
69  */
70 
71 static u64 srpt_service_guid;
72 static DEFINE_SPINLOCK(srpt_dev_lock);	/* Protects srpt_dev_list. */
73 static LIST_HEAD(srpt_dev_list);	/* List of srpt_device structures. */
74 
75 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
76 module_param(srp_max_req_size, int, 0444);
77 MODULE_PARM_DESC(srp_max_req_size,
78 		 "Maximum size of SRP request messages in bytes.");
79 
80 static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
81 module_param(srpt_srq_size, int, 0444);
82 MODULE_PARM_DESC(srpt_srq_size,
83 		 "Shared receive queue (SRQ) size.");
84 
srpt_get_u64_x(char * buffer,struct kernel_param * kp)85 static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
86 {
87 	return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
88 }
89 module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
90 		  0444);
91 MODULE_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 
95 static struct ib_client srpt_client;
96 static const struct target_core_fabric_ops srpt_template;
97 static void srpt_release_channel(struct srpt_rdma_ch *ch);
98 static int srpt_queue_status(struct se_cmd *cmd);
99 
100 /**
101  * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
102  */
103 static inline
opposite_dma_dir(enum dma_data_direction dir)104 enum 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  */
srpt_sdev_name(struct srpt_device * sdev)118 static inline const char *srpt_sdev_name(struct srpt_device *sdev)
119 {
120 	return sdev->device->name;
121 }
122 
srpt_get_ch_state(struct srpt_rdma_ch * ch)123 static 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 
134 static enum rdma_ch_state
srpt_set_ch_state(struct srpt_rdma_ch * ch,enum rdma_ch_state new_state)135 srpt_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  */
152 static bool
srpt_test_and_set_ch_state(struct srpt_rdma_ch * ch,enum rdma_ch_state old,enum rdma_ch_state new)153 srpt_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  */
srpt_event_handler(struct ib_event_handler * handler,struct ib_event * event)175 static 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 	case IB_EVENT_GID_CHANGE:
202 		/* Refresh port data asynchronously. */
203 		if (event->element.port_num <= sdev->device->phys_port_cnt) {
204 			sport = &sdev->port[event->element.port_num - 1];
205 			if (!sport->lid && !sport->sm_lid)
206 				schedule_work(&sport->work);
207 		}
208 		break;
209 	default:
210 		pr_err("received unrecognized IB event %d\n",
211 		       event->event);
212 		break;
213 	}
214 }
215 
216 /**
217  * srpt_srq_event() - SRQ event callback function.
218  */
srpt_srq_event(struct ib_event * event,void * ctx)219 static void srpt_srq_event(struct ib_event *event, void *ctx)
220 {
221 	pr_info("SRQ event %d\n", event->event);
222 }
223 
224 /**
225  * srpt_qp_event() - QP event callback function.
226  */
srpt_qp_event(struct ib_event * event,struct srpt_rdma_ch * ch)227 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
228 {
229 	pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
230 		 event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
231 
232 	switch (event->event) {
233 	case IB_EVENT_COMM_EST:
234 		ib_cm_notify(ch->cm_id, event->event);
235 		break;
236 	case IB_EVENT_QP_LAST_WQE_REACHED:
237 		if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
238 					       CH_RELEASING))
239 			srpt_release_channel(ch);
240 		else
241 			pr_debug("%s: state %d - ignored LAST_WQE.\n",
242 				 ch->sess_name, srpt_get_ch_state(ch));
243 		break;
244 	default:
245 		pr_err("received unrecognized IB QP event %d\n", 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  */
srpt_set_ioc(u8 * c_list,u32 slot,u8 value)259 static 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  */
srpt_get_class_port_info(struct ib_dm_mad * mad)280 static 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  */
srpt_get_iou(struct ib_dm_mad * mad)299 static 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  */
srpt_get_ioc(struct srpt_port * sport,u32 slot,struct ib_dm_mad * mad)324 static 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  */
srpt_get_svc_entries(u64 ioc_guid,u16 slot,u8 hi,u8 lo,struct ib_dm_mad * mad)374 static 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  */
srpt_mgmt_method_get(struct srpt_port * sp,struct ib_mad * rq_mad,struct ib_dm_mad * rsp_mad)411 static 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  */
srpt_mad_send_handler(struct ib_mad_agent * mad_agent,struct ib_mad_send_wc * mad_wc)448 static 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  */
srpt_mad_recv_handler(struct ib_mad_agent * mad_agent,struct ib_mad_recv_wc * mad_wc)458 static 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 
512 err_rsp:
513 	ib_destroy_ah(ah);
514 err:
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  */
srpt_refresh_port(struct srpt_port * sport)527 static 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, 0);
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 
576 err_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 
582 err_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  */
srpt_unregister_mad_agent(struct srpt_device * sdev)592 static 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 			pr_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  */
srpt_alloc_ioctx(struct srpt_device * sdev,int ioctx_size,int dma_size,enum dma_data_direction dir)615 static 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 
635 err_free_buf:
636 	kfree(ioctx->buf);
637 err_free_ioctx:
638 	kfree(ioctx);
639 err:
640 	return NULL;
641 }
642 
643 /**
644  * srpt_free_ioctx() - Free an SRPT I/O context structure.
645  */
srpt_free_ioctx(struct srpt_device * sdev,struct srpt_ioctx * ioctx,int dma_size,enum dma_data_direction dir)646 static 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  */
srpt_alloc_ioctx_ring(struct srpt_device * sdev,int ring_size,int ioctx_size,int dma_size,enum dma_data_direction dir)665 static 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 
686 err:
687 	while (--i >= 0)
688 		srpt_free_ioctx(sdev, ring[i], dma_size, dir);
689 	kfree(ring);
690 	ring = NULL;
691 out:
692 	return ring;
693 }
694 
695 /**
696  * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
697  */
srpt_free_ioctx_ring(struct srpt_ioctx ** ioctx_ring,struct srpt_device * sdev,int ring_size,int dma_size,enum dma_data_direction dir)698 static 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  */
srpt_get_cmd_state(struct srpt_send_ioctx * ioctx)712 static 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  */
srpt_set_cmd_state(struct srpt_send_ioctx * ioctx,enum srpt_command_state new)731 static 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  */
srpt_test_and_set_cmd_state(struct srpt_send_ioctx * ioctx,enum srpt_command_state old,enum srpt_command_state new)753 static 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  */
srpt_post_recv(struct srpt_device * sdev,struct srpt_recv_ioctx * ioctx)775 static 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  */
srpt_post_send(struct srpt_rdma_ch * ch,struct srpt_send_ioctx * ioctx,int len)800 static 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 		pr_warn("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 
832 out:
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  */
srpt_get_desc_tbl(struct srpt_send_ioctx * ioctx,struct srp_cmd * srp_cmd,enum dma_data_direction * dir,u64 * data_len)854 static 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 			pr_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 	}
941 out:
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  */
srpt_init_ch_qp(struct srpt_rdma_ch * ch,struct ib_qp * qp)951 static 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  */
srpt_ch_qp_rtr(struct srpt_rdma_ch * ch,struct ib_qp * qp)985 static 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 
1000 out:
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  */
srpt_ch_qp_rts(struct srpt_rdma_ch * ch,struct ib_qp * qp)1015 static 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 
1030 out:
1031 	return ret;
1032 }
1033 
1034 /**
1035  * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
1036  */
srpt_ch_qp_err(struct srpt_rdma_ch * ch)1037 static 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  */
srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch * ch,struct srpt_send_ioctx * ioctx)1048 static 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  */
srpt_map_sg_to_ib_sge(struct srpt_rdma_ch * ch,struct srpt_send_ioctx * ioctx)1078 static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
1079 				 struct srpt_send_ioctx *ioctx)
1080 {
1081 	struct ib_device *dev = ch->sport->sdev->device;
1082 	struct se_cmd *cmd;
1083 	struct scatterlist *sg, *sg_orig;
1084 	int sg_cnt;
1085 	enum dma_data_direction dir;
1086 	struct rdma_iu *riu;
1087 	struct srp_direct_buf *db;
1088 	dma_addr_t dma_addr;
1089 	struct ib_sge *sge;
1090 	u64 raddr;
1091 	u32 rsize;
1092 	u32 tsize;
1093 	u32 dma_len;
1094 	int count, nrdma;
1095 	int i, j, k;
1096 
1097 	BUG_ON(!ch);
1098 	BUG_ON(!ioctx);
1099 	cmd = &ioctx->cmd;
1100 	dir = cmd->data_direction;
1101 	BUG_ON(dir == DMA_NONE);
1102 
1103 	ioctx->sg = sg = sg_orig = cmd->t_data_sg;
1104 	ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
1105 
1106 	count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
1107 			      opposite_dma_dir(dir));
1108 	if (unlikely(!count))
1109 		return -EAGAIN;
1110 
1111 	ioctx->mapped_sg_count = count;
1112 
1113 	if (ioctx->rdma_ius && ioctx->n_rdma_ius)
1114 		nrdma = ioctx->n_rdma_ius;
1115 	else {
1116 		nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
1117 			+ ioctx->n_rbuf;
1118 
1119 		ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
1120 		if (!ioctx->rdma_ius)
1121 			goto free_mem;
1122 
1123 		ioctx->n_rdma_ius = nrdma;
1124 	}
1125 
1126 	db = ioctx->rbufs;
1127 	tsize = cmd->data_length;
1128 	dma_len = ib_sg_dma_len(dev, &sg[0]);
1129 	riu = ioctx->rdma_ius;
1130 
1131 	/*
1132 	 * For each remote desc - calculate the #ib_sge.
1133 	 * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
1134 	 *      each remote desc rdma_iu is required a rdma wr;
1135 	 * else
1136 	 *      we need to allocate extra rdma_iu to carry extra #ib_sge in
1137 	 *      another rdma wr
1138 	 */
1139 	for (i = 0, j = 0;
1140 	     j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1141 		rsize = be32_to_cpu(db->len);
1142 		raddr = be64_to_cpu(db->va);
1143 		riu->raddr = raddr;
1144 		riu->rkey = be32_to_cpu(db->key);
1145 		riu->sge_cnt = 0;
1146 
1147 		/* calculate how many sge required for this remote_buf */
1148 		while (rsize > 0 && tsize > 0) {
1149 
1150 			if (rsize >= dma_len) {
1151 				tsize -= dma_len;
1152 				rsize -= dma_len;
1153 				raddr += dma_len;
1154 
1155 				if (tsize > 0) {
1156 					++j;
1157 					if (j < count) {
1158 						sg = sg_next(sg);
1159 						dma_len = ib_sg_dma_len(
1160 								dev, sg);
1161 					}
1162 				}
1163 			} else {
1164 				tsize -= rsize;
1165 				dma_len -= rsize;
1166 				rsize = 0;
1167 			}
1168 
1169 			++riu->sge_cnt;
1170 
1171 			if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
1172 				++ioctx->n_rdma;
1173 				riu->sge =
1174 				    kmalloc(riu->sge_cnt * sizeof *riu->sge,
1175 					    GFP_KERNEL);
1176 				if (!riu->sge)
1177 					goto free_mem;
1178 
1179 				++riu;
1180 				riu->sge_cnt = 0;
1181 				riu->raddr = raddr;
1182 				riu->rkey = be32_to_cpu(db->key);
1183 			}
1184 		}
1185 
1186 		++ioctx->n_rdma;
1187 		riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
1188 				   GFP_KERNEL);
1189 		if (!riu->sge)
1190 			goto free_mem;
1191 	}
1192 
1193 	db = ioctx->rbufs;
1194 	tsize = cmd->data_length;
1195 	riu = ioctx->rdma_ius;
1196 	sg = sg_orig;
1197 	dma_len = ib_sg_dma_len(dev, &sg[0]);
1198 	dma_addr = ib_sg_dma_address(dev, &sg[0]);
1199 
1200 	/* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
1201 	for (i = 0, j = 0;
1202 	     j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
1203 		rsize = be32_to_cpu(db->len);
1204 		sge = riu->sge;
1205 		k = 0;
1206 
1207 		while (rsize > 0 && tsize > 0) {
1208 			sge->addr = dma_addr;
1209 			sge->lkey = ch->sport->sdev->mr->lkey;
1210 
1211 			if (rsize >= dma_len) {
1212 				sge->length =
1213 					(tsize < dma_len) ? tsize : dma_len;
1214 				tsize -= dma_len;
1215 				rsize -= dma_len;
1216 
1217 				if (tsize > 0) {
1218 					++j;
1219 					if (j < count) {
1220 						sg = sg_next(sg);
1221 						dma_len = ib_sg_dma_len(
1222 								dev, sg);
1223 						dma_addr = ib_sg_dma_address(
1224 								dev, sg);
1225 					}
1226 				}
1227 			} else {
1228 				sge->length = (tsize < rsize) ? tsize : rsize;
1229 				tsize -= rsize;
1230 				dma_len -= rsize;
1231 				dma_addr += rsize;
1232 				rsize = 0;
1233 			}
1234 
1235 			++k;
1236 			if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
1237 				++riu;
1238 				sge = riu->sge;
1239 				k = 0;
1240 			} else if (rsize > 0 && tsize > 0)
1241 				++sge;
1242 		}
1243 	}
1244 
1245 	return 0;
1246 
1247 free_mem:
1248 	srpt_unmap_sg_to_ib_sge(ch, ioctx);
1249 
1250 	return -ENOMEM;
1251 }
1252 
1253 /**
1254  * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
1255  */
srpt_get_send_ioctx(struct srpt_rdma_ch * ch)1256 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1257 {
1258 	struct srpt_send_ioctx *ioctx;
1259 	unsigned long flags;
1260 
1261 	BUG_ON(!ch);
1262 
1263 	ioctx = NULL;
1264 	spin_lock_irqsave(&ch->spinlock, flags);
1265 	if (!list_empty(&ch->free_list)) {
1266 		ioctx = list_first_entry(&ch->free_list,
1267 					 struct srpt_send_ioctx, free_list);
1268 		list_del(&ioctx->free_list);
1269 	}
1270 	spin_unlock_irqrestore(&ch->spinlock, flags);
1271 
1272 	if (!ioctx)
1273 		return ioctx;
1274 
1275 	BUG_ON(ioctx->ch != ch);
1276 	spin_lock_init(&ioctx->spinlock);
1277 	ioctx->state = SRPT_STATE_NEW;
1278 	ioctx->n_rbuf = 0;
1279 	ioctx->rbufs = NULL;
1280 	ioctx->n_rdma = 0;
1281 	ioctx->n_rdma_ius = 0;
1282 	ioctx->rdma_ius = NULL;
1283 	ioctx->mapped_sg_count = 0;
1284 	init_completion(&ioctx->tx_done);
1285 	ioctx->queue_status_only = false;
1286 	/*
1287 	 * transport_init_se_cmd() does not initialize all fields, so do it
1288 	 * here.
1289 	 */
1290 	memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1291 	memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1292 
1293 	return ioctx;
1294 }
1295 
1296 /**
1297  * srpt_abort_cmd() - Abort a SCSI command.
1298  * @ioctx:   I/O context associated with the SCSI command.
1299  * @context: Preferred execution context.
1300  */
srpt_abort_cmd(struct srpt_send_ioctx * ioctx)1301 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1302 {
1303 	enum srpt_command_state state;
1304 	unsigned long flags;
1305 
1306 	BUG_ON(!ioctx);
1307 
1308 	/*
1309 	 * If the command is in a state where the target core is waiting for
1310 	 * the ib_srpt driver, change the state to the next state. Changing
1311 	 * the state of the command from SRPT_STATE_NEED_DATA to
1312 	 * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
1313 	 * function a second time.
1314 	 */
1315 
1316 	spin_lock_irqsave(&ioctx->spinlock, flags);
1317 	state = ioctx->state;
1318 	switch (state) {
1319 	case SRPT_STATE_NEED_DATA:
1320 		ioctx->state = SRPT_STATE_DATA_IN;
1321 		break;
1322 	case SRPT_STATE_DATA_IN:
1323 	case SRPT_STATE_CMD_RSP_SENT:
1324 	case SRPT_STATE_MGMT_RSP_SENT:
1325 		ioctx->state = SRPT_STATE_DONE;
1326 		break;
1327 	default:
1328 		break;
1329 	}
1330 	spin_unlock_irqrestore(&ioctx->spinlock, flags);
1331 
1332 	if (state == SRPT_STATE_DONE) {
1333 		struct srpt_rdma_ch *ch = ioctx->ch;
1334 
1335 		BUG_ON(ch->sess == NULL);
1336 
1337 		target_put_sess_cmd(&ioctx->cmd);
1338 		goto out;
1339 	}
1340 
1341 	pr_debug("Aborting cmd with state %d and tag %lld\n", state,
1342 		 ioctx->tag);
1343 
1344 	switch (state) {
1345 	case SRPT_STATE_NEW:
1346 	case SRPT_STATE_DATA_IN:
1347 	case SRPT_STATE_MGMT:
1348 		/*
1349 		 * Do nothing - defer abort processing until
1350 		 * srpt_queue_response() is invoked.
1351 		 */
1352 		WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
1353 		break;
1354 	case SRPT_STATE_NEED_DATA:
1355 		/* DMA_TO_DEVICE (write) - RDMA read error. */
1356 
1357 		/* XXX(hch): this is a horrible layering violation.. */
1358 		spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
1359 		ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
1360 		spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
1361 		break;
1362 	case SRPT_STATE_CMD_RSP_SENT:
1363 		/*
1364 		 * SRP_RSP sending failed or the SRP_RSP send completion has
1365 		 * not been received in time.
1366 		 */
1367 		srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
1368 		target_put_sess_cmd(&ioctx->cmd);
1369 		break;
1370 	case SRPT_STATE_MGMT_RSP_SENT:
1371 		srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1372 		target_put_sess_cmd(&ioctx->cmd);
1373 		break;
1374 	default:
1375 		WARN(1, "Unexpected command state (%d)", state);
1376 		break;
1377 	}
1378 
1379 out:
1380 	return state;
1381 }
1382 
1383 /**
1384  * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
1385  */
srpt_handle_send_err_comp(struct srpt_rdma_ch * ch,u64 wr_id)1386 static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
1387 {
1388 	struct srpt_send_ioctx *ioctx;
1389 	enum srpt_command_state state;
1390 	struct se_cmd *cmd;
1391 	u32 index;
1392 
1393 	atomic_inc(&ch->sq_wr_avail);
1394 
1395 	index = idx_from_wr_id(wr_id);
1396 	ioctx = ch->ioctx_ring[index];
1397 	state = srpt_get_cmd_state(ioctx);
1398 	cmd = &ioctx->cmd;
1399 
1400 	WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1401 		&& state != SRPT_STATE_MGMT_RSP_SENT
1402 		&& state != SRPT_STATE_NEED_DATA
1403 		&& state != SRPT_STATE_DONE);
1404 
1405 	/* If SRP_RSP sending failed, undo the ch->req_lim change. */
1406 	if (state == SRPT_STATE_CMD_RSP_SENT
1407 	    || state == SRPT_STATE_MGMT_RSP_SENT)
1408 		atomic_dec(&ch->req_lim);
1409 
1410 	srpt_abort_cmd(ioctx);
1411 }
1412 
1413 /**
1414  * srpt_handle_send_comp() - Process an IB send completion notification.
1415  */
srpt_handle_send_comp(struct srpt_rdma_ch * ch,struct srpt_send_ioctx * ioctx)1416 static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
1417 				  struct srpt_send_ioctx *ioctx)
1418 {
1419 	enum srpt_command_state state;
1420 
1421 	atomic_inc(&ch->sq_wr_avail);
1422 
1423 	state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1424 
1425 	if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
1426 		    && state != SRPT_STATE_MGMT_RSP_SENT
1427 		    && state != SRPT_STATE_DONE))
1428 		pr_debug("state = %d\n", state);
1429 
1430 	if (state != SRPT_STATE_DONE) {
1431 		srpt_unmap_sg_to_ib_sge(ch, ioctx);
1432 		transport_generic_free_cmd(&ioctx->cmd, 0);
1433 	} else {
1434 		pr_err("IB completion has been received too late for"
1435 		       " wr_id = %u.\n", ioctx->ioctx.index);
1436 	}
1437 }
1438 
1439 /**
1440  * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
1441  *
1442  * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1443  * the data that has been transferred via IB RDMA had to be postponed until the
1444  * check_stop_free() callback.  None of this is necessary anymore and needs to
1445  * be cleaned up.
1446  */
srpt_handle_rdma_comp(struct srpt_rdma_ch * ch,struct srpt_send_ioctx * ioctx,enum srpt_opcode opcode)1447 static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
1448 				  struct srpt_send_ioctx *ioctx,
1449 				  enum srpt_opcode opcode)
1450 {
1451 	WARN_ON(ioctx->n_rdma <= 0);
1452 	atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1453 
1454 	if (opcode == SRPT_RDMA_READ_LAST) {
1455 		if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1456 						SRPT_STATE_DATA_IN))
1457 			target_execute_cmd(&ioctx->cmd);
1458 		else
1459 			pr_err("%s[%d]: wrong state = %d\n", __func__,
1460 			       __LINE__, srpt_get_cmd_state(ioctx));
1461 	} else if (opcode == SRPT_RDMA_ABORT) {
1462 		ioctx->rdma_aborted = true;
1463 	} else {
1464 		WARN(true, "unexpected opcode %d\n", opcode);
1465 	}
1466 }
1467 
1468 /**
1469  * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
1470  */
srpt_handle_rdma_err_comp(struct srpt_rdma_ch * ch,struct srpt_send_ioctx * ioctx,enum srpt_opcode opcode)1471 static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
1472 				      struct srpt_send_ioctx *ioctx,
1473 				      enum srpt_opcode opcode)
1474 {
1475 	struct se_cmd *cmd;
1476 	enum srpt_command_state state;
1477 
1478 	cmd = &ioctx->cmd;
1479 	state = srpt_get_cmd_state(ioctx);
1480 	switch (opcode) {
1481 	case SRPT_RDMA_READ_LAST:
1482 		if (ioctx->n_rdma <= 0) {
1483 			pr_err("Received invalid RDMA read"
1484 			       " error completion with idx %d\n",
1485 			       ioctx->ioctx.index);
1486 			break;
1487 		}
1488 		atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1489 		if (state == SRPT_STATE_NEED_DATA)
1490 			srpt_abort_cmd(ioctx);
1491 		else
1492 			pr_err("%s[%d]: wrong state = %d\n",
1493 			       __func__, __LINE__, state);
1494 		break;
1495 	case SRPT_RDMA_WRITE_LAST:
1496 		break;
1497 	default:
1498 		pr_err("%s[%d]: opcode = %u\n", __func__, __LINE__, opcode);
1499 		break;
1500 	}
1501 }
1502 
1503 /**
1504  * srpt_build_cmd_rsp() - Build an SRP_RSP response.
1505  * @ch: RDMA channel through which the request has been received.
1506  * @ioctx: I/O context associated with the SRP_CMD request. The response will
1507  *   be built in the buffer ioctx->buf points at and hence this function will
1508  *   overwrite the request data.
1509  * @tag: tag of the request for which this response is being generated.
1510  * @status: value for the STATUS field of the SRP_RSP information unit.
1511  *
1512  * Returns the size in bytes of the SRP_RSP response.
1513  *
1514  * An SRP_RSP response contains a SCSI status or service response. See also
1515  * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1516  * response. See also SPC-2 for more information about sense data.
1517  */
srpt_build_cmd_rsp(struct srpt_rdma_ch * ch,struct srpt_send_ioctx * ioctx,u64 tag,int status)1518 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1519 			      struct srpt_send_ioctx *ioctx, u64 tag,
1520 			      int status)
1521 {
1522 	struct srp_rsp *srp_rsp;
1523 	const u8 *sense_data;
1524 	int sense_data_len, max_sense_len;
1525 
1526 	/*
1527 	 * The lowest bit of all SAM-3 status codes is zero (see also
1528 	 * paragraph 5.3 in SAM-3).
1529 	 */
1530 	WARN_ON(status & 1);
1531 
1532 	srp_rsp = ioctx->ioctx.buf;
1533 	BUG_ON(!srp_rsp);
1534 
1535 	sense_data = ioctx->sense_data;
1536 	sense_data_len = ioctx->cmd.scsi_sense_length;
1537 	WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1538 
1539 	memset(srp_rsp, 0, sizeof *srp_rsp);
1540 	srp_rsp->opcode = SRP_RSP;
1541 	srp_rsp->req_lim_delta =
1542 		__constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1543 	srp_rsp->tag = tag;
1544 	srp_rsp->status = status;
1545 
1546 	if (sense_data_len) {
1547 		BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1548 		max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1549 		if (sense_data_len > max_sense_len) {
1550 			pr_warn("truncated sense data from %d to %d"
1551 				" bytes\n", sense_data_len, max_sense_len);
1552 			sense_data_len = max_sense_len;
1553 		}
1554 
1555 		srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1556 		srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1557 		memcpy(srp_rsp + 1, sense_data, sense_data_len);
1558 	}
1559 
1560 	return sizeof(*srp_rsp) + sense_data_len;
1561 }
1562 
1563 /**
1564  * srpt_build_tskmgmt_rsp() - Build a task management response.
1565  * @ch:       RDMA channel through which the request has been received.
1566  * @ioctx:    I/O context in which the SRP_RSP response will be built.
1567  * @rsp_code: RSP_CODE that will be stored in the response.
1568  * @tag:      Tag of the request for which this response is being generated.
1569  *
1570  * Returns the size in bytes of the SRP_RSP response.
1571  *
1572  * An SRP_RSP response contains a SCSI status or service response. See also
1573  * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1574  * response.
1575  */
srpt_build_tskmgmt_rsp(struct srpt_rdma_ch * ch,struct srpt_send_ioctx * ioctx,u8 rsp_code,u64 tag)1576 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1577 				  struct srpt_send_ioctx *ioctx,
1578 				  u8 rsp_code, u64 tag)
1579 {
1580 	struct srp_rsp *srp_rsp;
1581 	int resp_data_len;
1582 	int resp_len;
1583 
1584 	resp_data_len = 4;
1585 	resp_len = sizeof(*srp_rsp) + resp_data_len;
1586 
1587 	srp_rsp = ioctx->ioctx.buf;
1588 	BUG_ON(!srp_rsp);
1589 	memset(srp_rsp, 0, sizeof *srp_rsp);
1590 
1591 	srp_rsp->opcode = SRP_RSP;
1592 	srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
1593 				    + atomic_xchg(&ch->req_lim_delta, 0));
1594 	srp_rsp->tag = tag;
1595 
1596 	srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1597 	srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1598 	srp_rsp->data[3] = rsp_code;
1599 
1600 	return resp_len;
1601 }
1602 
1603 #define NO_SUCH_LUN ((uint64_t)-1LL)
1604 
1605 /*
1606  * SCSI LUN addressing method. See also SAM-2 and the section about
1607  * eight byte LUNs.
1608  */
1609 enum scsi_lun_addr_method {
1610 	SCSI_LUN_ADDR_METHOD_PERIPHERAL   = 0,
1611 	SCSI_LUN_ADDR_METHOD_FLAT         = 1,
1612 	SCSI_LUN_ADDR_METHOD_LUN          = 2,
1613 	SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
1614 };
1615 
1616 /*
1617  * srpt_unpack_lun() - Convert from network LUN to linear LUN.
1618  *
1619  * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
1620  * order (big endian) to a linear LUN. Supports three LUN addressing methods:
1621  * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
1622  */
srpt_unpack_lun(const uint8_t * lun,int len)1623 static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
1624 {
1625 	uint64_t res = NO_SUCH_LUN;
1626 	int addressing_method;
1627 
1628 	if (unlikely(len < 2)) {
1629 		pr_err("Illegal LUN length %d, expected 2 bytes or more\n",
1630 		       len);
1631 		goto out;
1632 	}
1633 
1634 	switch (len) {
1635 	case 8:
1636 		if ((*((__be64 *)lun) &
1637 		     __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1638 			goto out_err;
1639 		break;
1640 	case 4:
1641 		if (*((__be16 *)&lun[2]) != 0)
1642 			goto out_err;
1643 		break;
1644 	case 6:
1645 		if (*((__be32 *)&lun[2]) != 0)
1646 			goto out_err;
1647 		break;
1648 	case 2:
1649 		break;
1650 	default:
1651 		goto out_err;
1652 	}
1653 
1654 	addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
1655 	switch (addressing_method) {
1656 	case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
1657 	case SCSI_LUN_ADDR_METHOD_FLAT:
1658 	case SCSI_LUN_ADDR_METHOD_LUN:
1659 		res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1660 		break;
1661 
1662 	case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
1663 	default:
1664 		pr_err("Unimplemented LUN addressing method %u\n",
1665 		       addressing_method);
1666 		break;
1667 	}
1668 
1669 out:
1670 	return res;
1671 
1672 out_err:
1673 	pr_err("Support for multi-level LUNs has not yet been implemented\n");
1674 	goto out;
1675 }
1676 
srpt_check_stop_free(struct se_cmd * cmd)1677 static int srpt_check_stop_free(struct se_cmd *cmd)
1678 {
1679 	struct srpt_send_ioctx *ioctx = container_of(cmd,
1680 				struct srpt_send_ioctx, cmd);
1681 
1682 	return target_put_sess_cmd(&ioctx->cmd);
1683 }
1684 
1685 /**
1686  * srpt_handle_cmd() - Process SRP_CMD.
1687  */
srpt_handle_cmd(struct srpt_rdma_ch * ch,struct srpt_recv_ioctx * recv_ioctx,struct srpt_send_ioctx * send_ioctx)1688 static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
1689 			   struct srpt_recv_ioctx *recv_ioctx,
1690 			   struct srpt_send_ioctx *send_ioctx)
1691 {
1692 	struct se_cmd *cmd;
1693 	struct srp_cmd *srp_cmd;
1694 	uint64_t unpacked_lun;
1695 	u64 data_len;
1696 	enum dma_data_direction dir;
1697 	sense_reason_t ret;
1698 	int rc;
1699 
1700 	BUG_ON(!send_ioctx);
1701 
1702 	srp_cmd = recv_ioctx->ioctx.buf;
1703 	cmd = &send_ioctx->cmd;
1704 	send_ioctx->tag = srp_cmd->tag;
1705 
1706 	switch (srp_cmd->task_attr) {
1707 	case SRP_CMD_SIMPLE_Q:
1708 		cmd->sam_task_attr = TCM_SIMPLE_TAG;
1709 		break;
1710 	case SRP_CMD_ORDERED_Q:
1711 	default:
1712 		cmd->sam_task_attr = TCM_ORDERED_TAG;
1713 		break;
1714 	case SRP_CMD_HEAD_OF_Q:
1715 		cmd->sam_task_attr = TCM_HEAD_TAG;
1716 		break;
1717 	case SRP_CMD_ACA:
1718 		cmd->sam_task_attr = TCM_ACA_TAG;
1719 		break;
1720 	}
1721 
1722 	if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
1723 		pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1724 		       srp_cmd->tag);
1725 		ret = TCM_INVALID_CDB_FIELD;
1726 		goto send_sense;
1727 	}
1728 
1729 	unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
1730 				       sizeof(srp_cmd->lun));
1731 	rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
1732 			&send_ioctx->sense_data[0], unpacked_lun, data_len,
1733 			TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
1734 	if (rc != 0) {
1735 		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1736 		goto send_sense;
1737 	}
1738 	return 0;
1739 
1740 send_sense:
1741 	transport_send_check_condition_and_sense(cmd, ret, 0);
1742 	return -1;
1743 }
1744 
srp_tmr_to_tcm(int fn)1745 static int srp_tmr_to_tcm(int fn)
1746 {
1747 	switch (fn) {
1748 	case SRP_TSK_ABORT_TASK:
1749 		return TMR_ABORT_TASK;
1750 	case SRP_TSK_ABORT_TASK_SET:
1751 		return TMR_ABORT_TASK_SET;
1752 	case SRP_TSK_CLEAR_TASK_SET:
1753 		return TMR_CLEAR_TASK_SET;
1754 	case SRP_TSK_LUN_RESET:
1755 		return TMR_LUN_RESET;
1756 	case SRP_TSK_CLEAR_ACA:
1757 		return TMR_CLEAR_ACA;
1758 	default:
1759 		return -1;
1760 	}
1761 }
1762 
1763 /**
1764  * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
1765  *
1766  * Returns 0 if and only if the request will be processed by the target core.
1767  *
1768  * For more information about SRP_TSK_MGMT information units, see also section
1769  * 6.7 in the SRP r16a document.
1770  */
srpt_handle_tsk_mgmt(struct srpt_rdma_ch * ch,struct srpt_recv_ioctx * recv_ioctx,struct srpt_send_ioctx * send_ioctx)1771 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1772 				 struct srpt_recv_ioctx *recv_ioctx,
1773 				 struct srpt_send_ioctx *send_ioctx)
1774 {
1775 	struct srp_tsk_mgmt *srp_tsk;
1776 	struct se_cmd *cmd;
1777 	struct se_session *sess = ch->sess;
1778 	uint64_t unpacked_lun;
1779 	int tcm_tmr;
1780 	int rc;
1781 
1782 	BUG_ON(!send_ioctx);
1783 
1784 	srp_tsk = recv_ioctx->ioctx.buf;
1785 	cmd = &send_ioctx->cmd;
1786 
1787 	pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
1788 		 " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
1789 		 srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
1790 
1791 	srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1792 	send_ioctx->tag = srp_tsk->tag;
1793 	tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1794 	unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
1795 				       sizeof(srp_tsk->lun));
1796 	rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
1797 				srp_tsk, tcm_tmr, GFP_KERNEL, srp_tsk->task_tag,
1798 				TARGET_SCF_ACK_KREF);
1799 	if (rc != 0) {
1800 		send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1801 		goto fail;
1802 	}
1803 	return;
1804 fail:
1805 	transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
1806 }
1807 
1808 /**
1809  * srpt_handle_new_iu() - Process a newly received information unit.
1810  * @ch:    RDMA channel through which the information unit has been received.
1811  * @ioctx: SRPT I/O context associated with the information unit.
1812  */
srpt_handle_new_iu(struct srpt_rdma_ch * ch,struct srpt_recv_ioctx * recv_ioctx,struct srpt_send_ioctx * send_ioctx)1813 static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
1814 			       struct srpt_recv_ioctx *recv_ioctx,
1815 			       struct srpt_send_ioctx *send_ioctx)
1816 {
1817 	struct srp_cmd *srp_cmd;
1818 	enum rdma_ch_state ch_state;
1819 
1820 	BUG_ON(!ch);
1821 	BUG_ON(!recv_ioctx);
1822 
1823 	ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1824 				   recv_ioctx->ioctx.dma, srp_max_req_size,
1825 				   DMA_FROM_DEVICE);
1826 
1827 	ch_state = srpt_get_ch_state(ch);
1828 	if (unlikely(ch_state == CH_CONNECTING)) {
1829 		list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1830 		goto out;
1831 	}
1832 
1833 	if (unlikely(ch_state != CH_LIVE))
1834 		goto out;
1835 
1836 	srp_cmd = recv_ioctx->ioctx.buf;
1837 	if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
1838 		if (!send_ioctx)
1839 			send_ioctx = srpt_get_send_ioctx(ch);
1840 		if (unlikely(!send_ioctx)) {
1841 			list_add_tail(&recv_ioctx->wait_list,
1842 				      &ch->cmd_wait_list);
1843 			goto out;
1844 		}
1845 	}
1846 
1847 	switch (srp_cmd->opcode) {
1848 	case SRP_CMD:
1849 		srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1850 		break;
1851 	case SRP_TSK_MGMT:
1852 		srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1853 		break;
1854 	case SRP_I_LOGOUT:
1855 		pr_err("Not yet implemented: SRP_I_LOGOUT\n");
1856 		break;
1857 	case SRP_CRED_RSP:
1858 		pr_debug("received SRP_CRED_RSP\n");
1859 		break;
1860 	case SRP_AER_RSP:
1861 		pr_debug("received SRP_AER_RSP\n");
1862 		break;
1863 	case SRP_RSP:
1864 		pr_err("Received SRP_RSP\n");
1865 		break;
1866 	default:
1867 		pr_err("received IU with unknown opcode 0x%x\n",
1868 		       srp_cmd->opcode);
1869 		break;
1870 	}
1871 
1872 	srpt_post_recv(ch->sport->sdev, recv_ioctx);
1873 out:
1874 	return;
1875 }
1876 
srpt_process_rcv_completion(struct ib_cq * cq,struct srpt_rdma_ch * ch,struct ib_wc * wc)1877 static void srpt_process_rcv_completion(struct ib_cq *cq,
1878 					struct srpt_rdma_ch *ch,
1879 					struct ib_wc *wc)
1880 {
1881 	struct srpt_device *sdev = ch->sport->sdev;
1882 	struct srpt_recv_ioctx *ioctx;
1883 	u32 index;
1884 
1885 	index = idx_from_wr_id(wc->wr_id);
1886 	if (wc->status == IB_WC_SUCCESS) {
1887 		int req_lim;
1888 
1889 		req_lim = atomic_dec_return(&ch->req_lim);
1890 		if (unlikely(req_lim < 0))
1891 			pr_err("req_lim = %d < 0\n", req_lim);
1892 		ioctx = sdev->ioctx_ring[index];
1893 		srpt_handle_new_iu(ch, ioctx, NULL);
1894 	} else {
1895 		pr_info("receiving failed for idx %u with status %d\n",
1896 			index, wc->status);
1897 	}
1898 }
1899 
1900 /**
1901  * srpt_process_send_completion() - Process an IB send completion.
1902  *
1903  * Note: Although this has not yet been observed during tests, at least in
1904  * theory it is possible that the srpt_get_send_ioctx() call invoked by
1905  * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1906  * value in each response is set to one, and it is possible that this response
1907  * makes the initiator send a new request before the send completion for that
1908  * response has been processed. This could e.g. happen if the call to
1909  * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1910  * if IB retransmission causes generation of the send completion to be
1911  * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1912  * are queued on cmd_wait_list. The code below processes these delayed
1913  * requests one at a time.
1914  */
srpt_process_send_completion(struct ib_cq * cq,struct srpt_rdma_ch * ch,struct ib_wc * wc)1915 static void srpt_process_send_completion(struct ib_cq *cq,
1916 					 struct srpt_rdma_ch *ch,
1917 					 struct ib_wc *wc)
1918 {
1919 	struct srpt_send_ioctx *send_ioctx;
1920 	uint32_t index;
1921 	enum srpt_opcode opcode;
1922 
1923 	index = idx_from_wr_id(wc->wr_id);
1924 	opcode = opcode_from_wr_id(wc->wr_id);
1925 	send_ioctx = ch->ioctx_ring[index];
1926 	if (wc->status == IB_WC_SUCCESS) {
1927 		if (opcode == SRPT_SEND)
1928 			srpt_handle_send_comp(ch, send_ioctx);
1929 		else {
1930 			WARN_ON(opcode != SRPT_RDMA_ABORT &&
1931 				wc->opcode != IB_WC_RDMA_READ);
1932 			srpt_handle_rdma_comp(ch, send_ioctx, opcode);
1933 		}
1934 	} else {
1935 		if (opcode == SRPT_SEND) {
1936 			pr_info("sending response for idx %u failed"
1937 				" with status %d\n", index, wc->status);
1938 			srpt_handle_send_err_comp(ch, wc->wr_id);
1939 		} else if (opcode != SRPT_RDMA_MID) {
1940 			pr_info("RDMA t %d for idx %u failed with"
1941 				" status %d\n", opcode, index, wc->status);
1942 			srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
1943 		}
1944 	}
1945 
1946 	while (unlikely(opcode == SRPT_SEND
1947 			&& !list_empty(&ch->cmd_wait_list)
1948 			&& srpt_get_ch_state(ch) == CH_LIVE
1949 			&& (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
1950 		struct srpt_recv_ioctx *recv_ioctx;
1951 
1952 		recv_ioctx = list_first_entry(&ch->cmd_wait_list,
1953 					      struct srpt_recv_ioctx,
1954 					      wait_list);
1955 		list_del(&recv_ioctx->wait_list);
1956 		srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
1957 	}
1958 }
1959 
srpt_process_completion(struct ib_cq * cq,struct srpt_rdma_ch * ch)1960 static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
1961 {
1962 	struct ib_wc *const wc = ch->wc;
1963 	int i, n;
1964 
1965 	WARN_ON(cq != ch->cq);
1966 
1967 	ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1968 	while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
1969 		for (i = 0; i < n; i++) {
1970 			if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
1971 				srpt_process_rcv_completion(cq, ch, &wc[i]);
1972 			else
1973 				srpt_process_send_completion(cq, ch, &wc[i]);
1974 		}
1975 	}
1976 }
1977 
1978 /**
1979  * srpt_completion() - IB completion queue callback function.
1980  *
1981  * Notes:
1982  * - It is guaranteed that a completion handler will never be invoked
1983  *   concurrently on two different CPUs for the same completion queue. See also
1984  *   Documentation/infiniband/core_locking.txt and the implementation of
1985  *   handle_edge_irq() in kernel/irq/chip.c.
1986  * - When threaded IRQs are enabled, completion handlers are invoked in thread
1987  *   context instead of interrupt context.
1988  */
srpt_completion(struct ib_cq * cq,void * ctx)1989 static void srpt_completion(struct ib_cq *cq, void *ctx)
1990 {
1991 	struct srpt_rdma_ch *ch = ctx;
1992 
1993 	wake_up_interruptible(&ch->wait_queue);
1994 }
1995 
srpt_compl_thread(void * arg)1996 static int srpt_compl_thread(void *arg)
1997 {
1998 	struct srpt_rdma_ch *ch;
1999 
2000 	/* Hibernation / freezing of the SRPT kernel thread is not supported. */
2001 	current->flags |= PF_NOFREEZE;
2002 
2003 	ch = arg;
2004 	BUG_ON(!ch);
2005 	pr_info("Session %s: kernel thread %s (PID %d) started\n",
2006 		ch->sess_name, ch->thread->comm, current->pid);
2007 	while (!kthread_should_stop()) {
2008 		wait_event_interruptible(ch->wait_queue,
2009 			(srpt_process_completion(ch->cq, ch),
2010 			 kthread_should_stop()));
2011 	}
2012 	pr_info("Session %s: kernel thread %s (PID %d) stopped\n",
2013 		ch->sess_name, ch->thread->comm, current->pid);
2014 	return 0;
2015 }
2016 
2017 /**
2018  * srpt_create_ch_ib() - Create receive and send completion queues.
2019  */
srpt_create_ch_ib(struct srpt_rdma_ch * ch)2020 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
2021 {
2022 	struct ib_qp_init_attr *qp_init;
2023 	struct srpt_port *sport = ch->sport;
2024 	struct srpt_device *sdev = sport->sdev;
2025 	u32 srp_sq_size = sport->port_attrib.srp_sq_size;
2026 	int ret;
2027 
2028 	WARN_ON(ch->rq_size < 1);
2029 
2030 	ret = -ENOMEM;
2031 	qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
2032 	if (!qp_init)
2033 		goto out;
2034 
2035 retry:
2036 	ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
2037 			      ch->rq_size + srp_sq_size, 0);
2038 	if (IS_ERR(ch->cq)) {
2039 		ret = PTR_ERR(ch->cq);
2040 		pr_err("failed to create CQ cqe= %d ret= %d\n",
2041 		       ch->rq_size + srp_sq_size, ret);
2042 		goto out;
2043 	}
2044 
2045 	qp_init->qp_context = (void *)ch;
2046 	qp_init->event_handler
2047 		= (void(*)(struct ib_event *, void*))srpt_qp_event;
2048 	qp_init->send_cq = ch->cq;
2049 	qp_init->recv_cq = ch->cq;
2050 	qp_init->srq = sdev->srq;
2051 	qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
2052 	qp_init->qp_type = IB_QPT_RC;
2053 	qp_init->cap.max_send_wr = srp_sq_size;
2054 	qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
2055 
2056 	ch->qp = ib_create_qp(sdev->pd, qp_init);
2057 	if (IS_ERR(ch->qp)) {
2058 		ret = PTR_ERR(ch->qp);
2059 		if (ret == -ENOMEM) {
2060 			srp_sq_size /= 2;
2061 			if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
2062 				ib_destroy_cq(ch->cq);
2063 				goto retry;
2064 			}
2065 		}
2066 		pr_err("failed to create_qp ret= %d\n", ret);
2067 		goto err_destroy_cq;
2068 	}
2069 
2070 	atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
2071 
2072 	pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
2073 		 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
2074 		 qp_init->cap.max_send_wr, ch->cm_id);
2075 
2076 	ret = srpt_init_ch_qp(ch, ch->qp);
2077 	if (ret)
2078 		goto err_destroy_qp;
2079 
2080 	init_waitqueue_head(&ch->wait_queue);
2081 
2082 	pr_debug("creating thread for session %s\n", ch->sess_name);
2083 
2084 	ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
2085 	if (IS_ERR(ch->thread)) {
2086 		pr_err("failed to create kernel thread %ld\n",
2087 		       PTR_ERR(ch->thread));
2088 		ch->thread = NULL;
2089 		goto err_destroy_qp;
2090 	}
2091 
2092 out:
2093 	kfree(qp_init);
2094 	return ret;
2095 
2096 err_destroy_qp:
2097 	ib_destroy_qp(ch->qp);
2098 err_destroy_cq:
2099 	ib_destroy_cq(ch->cq);
2100 	goto out;
2101 }
2102 
srpt_destroy_ch_ib(struct srpt_rdma_ch * ch)2103 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
2104 {
2105 	if (ch->thread)
2106 		kthread_stop(ch->thread);
2107 
2108 	ib_destroy_qp(ch->qp);
2109 	ib_destroy_cq(ch->cq);
2110 }
2111 
2112 /**
2113  * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
2114  *
2115  * Reset the QP and make sure all resources associated with the channel will
2116  * be deallocated at an appropriate time.
2117  *
2118  * Note: The caller must hold ch->sport->sdev->spinlock.
2119  */
__srpt_close_ch(struct srpt_rdma_ch * ch)2120 static void __srpt_close_ch(struct srpt_rdma_ch *ch)
2121 {
2122 	struct srpt_device *sdev;
2123 	enum rdma_ch_state prev_state;
2124 	unsigned long flags;
2125 
2126 	sdev = ch->sport->sdev;
2127 
2128 	spin_lock_irqsave(&ch->spinlock, flags);
2129 	prev_state = ch->state;
2130 	switch (prev_state) {
2131 	case CH_CONNECTING:
2132 	case CH_LIVE:
2133 		ch->state = CH_DISCONNECTING;
2134 		break;
2135 	default:
2136 		break;
2137 	}
2138 	spin_unlock_irqrestore(&ch->spinlock, flags);
2139 
2140 	switch (prev_state) {
2141 	case CH_CONNECTING:
2142 		ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
2143 			       NULL, 0);
2144 		/* fall through */
2145 	case CH_LIVE:
2146 		if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
2147 			pr_err("sending CM DREQ failed.\n");
2148 		break;
2149 	case CH_DISCONNECTING:
2150 		break;
2151 	case CH_DRAINING:
2152 	case CH_RELEASING:
2153 		break;
2154 	}
2155 }
2156 
2157 /**
2158  * srpt_close_ch() - Close an RDMA channel.
2159  */
srpt_close_ch(struct srpt_rdma_ch * ch)2160 static void srpt_close_ch(struct srpt_rdma_ch *ch)
2161 {
2162 	struct srpt_device *sdev;
2163 
2164 	sdev = ch->sport->sdev;
2165 	spin_lock_irq(&sdev->spinlock);
2166 	__srpt_close_ch(ch);
2167 	spin_unlock_irq(&sdev->spinlock);
2168 }
2169 
2170 /**
2171  * srpt_shutdown_session() - Whether or not a session may be shut down.
2172  */
srpt_shutdown_session(struct se_session * se_sess)2173 static int srpt_shutdown_session(struct se_session *se_sess)
2174 {
2175 	struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
2176 	unsigned long flags;
2177 
2178 	spin_lock_irqsave(&ch->spinlock, flags);
2179 	if (ch->in_shutdown) {
2180 		spin_unlock_irqrestore(&ch->spinlock, flags);
2181 		return true;
2182 	}
2183 
2184 	ch->in_shutdown = true;
2185 	target_sess_cmd_list_set_waiting(se_sess);
2186 	spin_unlock_irqrestore(&ch->spinlock, flags);
2187 
2188 	return true;
2189 }
2190 
2191 /**
2192  * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
2193  * @cm_id: Pointer to the CM ID of the channel to be drained.
2194  *
2195  * Note: Must be called from inside srpt_cm_handler to avoid a race between
2196  * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
2197  * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
2198  * waits until all target sessions for the associated IB device have been
2199  * unregistered and target session registration involves a call to
2200  * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
2201  * this function has finished).
2202  */
srpt_drain_channel(struct ib_cm_id * cm_id)2203 static void srpt_drain_channel(struct ib_cm_id *cm_id)
2204 {
2205 	struct srpt_device *sdev;
2206 	struct srpt_rdma_ch *ch;
2207 	int ret;
2208 	bool do_reset = false;
2209 
2210 	WARN_ON_ONCE(irqs_disabled());
2211 
2212 	sdev = cm_id->context;
2213 	BUG_ON(!sdev);
2214 	spin_lock_irq(&sdev->spinlock);
2215 	list_for_each_entry(ch, &sdev->rch_list, list) {
2216 		if (ch->cm_id == cm_id) {
2217 			do_reset = srpt_test_and_set_ch_state(ch,
2218 					CH_CONNECTING, CH_DRAINING) ||
2219 				   srpt_test_and_set_ch_state(ch,
2220 					CH_LIVE, CH_DRAINING) ||
2221 				   srpt_test_and_set_ch_state(ch,
2222 					CH_DISCONNECTING, CH_DRAINING);
2223 			break;
2224 		}
2225 	}
2226 	spin_unlock_irq(&sdev->spinlock);
2227 
2228 	if (do_reset) {
2229 		if (ch->sess)
2230 			srpt_shutdown_session(ch->sess);
2231 
2232 		ret = srpt_ch_qp_err(ch);
2233 		if (ret < 0)
2234 			pr_err("Setting queue pair in error state"
2235 			       " failed: %d\n", ret);
2236 	}
2237 }
2238 
2239 /**
2240  * srpt_find_channel() - Look up an RDMA channel.
2241  * @cm_id: Pointer to the CM ID of the channel to be looked up.
2242  *
2243  * Return NULL if no matching RDMA channel has been found.
2244  */
srpt_find_channel(struct srpt_device * sdev,struct ib_cm_id * cm_id)2245 static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
2246 					      struct ib_cm_id *cm_id)
2247 {
2248 	struct srpt_rdma_ch *ch;
2249 	bool found;
2250 
2251 	WARN_ON_ONCE(irqs_disabled());
2252 	BUG_ON(!sdev);
2253 
2254 	found = false;
2255 	spin_lock_irq(&sdev->spinlock);
2256 	list_for_each_entry(ch, &sdev->rch_list, list) {
2257 		if (ch->cm_id == cm_id) {
2258 			found = true;
2259 			break;
2260 		}
2261 	}
2262 	spin_unlock_irq(&sdev->spinlock);
2263 
2264 	return found ? ch : NULL;
2265 }
2266 
2267 /**
2268  * srpt_release_channel() - Release channel resources.
2269  *
2270  * Schedules the actual release because:
2271  * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
2272  *   trigger a deadlock.
2273  * - It is not safe to call TCM transport_* functions from interrupt context.
2274  */
srpt_release_channel(struct srpt_rdma_ch * ch)2275 static void srpt_release_channel(struct srpt_rdma_ch *ch)
2276 {
2277 	schedule_work(&ch->release_work);
2278 }
2279 
srpt_release_channel_work(struct work_struct * w)2280 static void srpt_release_channel_work(struct work_struct *w)
2281 {
2282 	struct srpt_rdma_ch *ch;
2283 	struct srpt_device *sdev;
2284 	struct se_session *se_sess;
2285 
2286 	ch = container_of(w, struct srpt_rdma_ch, release_work);
2287 	pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
2288 		 ch->release_done);
2289 
2290 	sdev = ch->sport->sdev;
2291 	BUG_ON(!sdev);
2292 
2293 	se_sess = ch->sess;
2294 	BUG_ON(!se_sess);
2295 
2296 	target_wait_for_sess_cmds(se_sess);
2297 
2298 	transport_deregister_session_configfs(se_sess);
2299 	transport_deregister_session(se_sess);
2300 	ch->sess = NULL;
2301 
2302 	ib_destroy_cm_id(ch->cm_id);
2303 
2304 	srpt_destroy_ch_ib(ch);
2305 
2306 	srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2307 			     ch->sport->sdev, ch->rq_size,
2308 			     ch->rsp_size, DMA_TO_DEVICE);
2309 
2310 	spin_lock_irq(&sdev->spinlock);
2311 	list_del(&ch->list);
2312 	spin_unlock_irq(&sdev->spinlock);
2313 
2314 	if (ch->release_done)
2315 		complete(ch->release_done);
2316 
2317 	wake_up(&sdev->ch_releaseQ);
2318 
2319 	kfree(ch);
2320 }
2321 
__srpt_lookup_acl(struct srpt_port * sport,u8 i_port_id[16])2322 static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
2323 					       u8 i_port_id[16])
2324 {
2325 	struct srpt_node_acl *nacl;
2326 
2327 	list_for_each_entry(nacl, &sport->port_acl_list, list)
2328 		if (memcmp(nacl->i_port_id, i_port_id,
2329 			   sizeof(nacl->i_port_id)) == 0)
2330 			return nacl;
2331 
2332 	return NULL;
2333 }
2334 
srpt_lookup_acl(struct srpt_port * sport,u8 i_port_id[16])2335 static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
2336 					     u8 i_port_id[16])
2337 {
2338 	struct srpt_node_acl *nacl;
2339 
2340 	spin_lock_irq(&sport->port_acl_lock);
2341 	nacl = __srpt_lookup_acl(sport, i_port_id);
2342 	spin_unlock_irq(&sport->port_acl_lock);
2343 
2344 	return nacl;
2345 }
2346 
2347 /**
2348  * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
2349  *
2350  * Ownership of the cm_id is transferred to the target session if this
2351  * functions returns zero. Otherwise the caller remains the owner of cm_id.
2352  */
srpt_cm_req_recv(struct ib_cm_id * cm_id,struct ib_cm_req_event_param * param,void * private_data)2353 static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
2354 			    struct ib_cm_req_event_param *param,
2355 			    void *private_data)
2356 {
2357 	struct srpt_device *sdev = cm_id->context;
2358 	struct srpt_port *sport = &sdev->port[param->port - 1];
2359 	struct srp_login_req *req;
2360 	struct srp_login_rsp *rsp;
2361 	struct srp_login_rej *rej;
2362 	struct ib_cm_rep_param *rep_param;
2363 	struct srpt_rdma_ch *ch, *tmp_ch;
2364 	struct srpt_node_acl *nacl;
2365 	u32 it_iu_len;
2366 	int i;
2367 	int ret = 0;
2368 
2369 	WARN_ON_ONCE(irqs_disabled());
2370 
2371 	if (WARN_ON(!sdev || !private_data))
2372 		return -EINVAL;
2373 
2374 	req = (struct srp_login_req *)private_data;
2375 
2376 	it_iu_len = be32_to_cpu(req->req_it_iu_len);
2377 
2378 	pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
2379 		" t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
2380 		" (guid=0x%llx:0x%llx)\n",
2381 		be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
2382 		be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
2383 		be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
2384 		be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
2385 		it_iu_len,
2386 		param->port,
2387 		be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
2388 		be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
2389 
2390 	rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
2391 	rej = kzalloc(sizeof *rej, GFP_KERNEL);
2392 	rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
2393 
2394 	if (!rsp || !rej || !rep_param) {
2395 		ret = -ENOMEM;
2396 		goto out;
2397 	}
2398 
2399 	if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2400 		rej->reason = __constant_cpu_to_be32(
2401 				SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2402 		ret = -EINVAL;
2403 		pr_err("rejected SRP_LOGIN_REQ because its"
2404 		       " length (%d bytes) is out of range (%d .. %d)\n",
2405 		       it_iu_len, 64, srp_max_req_size);
2406 		goto reject;
2407 	}
2408 
2409 	if (!sport->enabled) {
2410 		rej->reason = __constant_cpu_to_be32(
2411 			     SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2412 		ret = -EINVAL;
2413 		pr_err("rejected SRP_LOGIN_REQ because the target port"
2414 		       " has not yet been enabled\n");
2415 		goto reject;
2416 	}
2417 
2418 	if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2419 		rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
2420 
2421 		spin_lock_irq(&sdev->spinlock);
2422 
2423 		list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
2424 			if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
2425 			    && !memcmp(ch->t_port_id, req->target_port_id, 16)
2426 			    && param->port == ch->sport->port
2427 			    && param->listen_id == ch->sport->sdev->cm_id
2428 			    && ch->cm_id) {
2429 				enum rdma_ch_state ch_state;
2430 
2431 				ch_state = srpt_get_ch_state(ch);
2432 				if (ch_state != CH_CONNECTING
2433 				    && ch_state != CH_LIVE)
2434 					continue;
2435 
2436 				/* found an existing channel */
2437 				pr_debug("Found existing channel %s"
2438 					 " cm_id= %p state= %d\n",
2439 					 ch->sess_name, ch->cm_id, ch_state);
2440 
2441 				__srpt_close_ch(ch);
2442 
2443 				rsp->rsp_flags =
2444 					SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2445 			}
2446 		}
2447 
2448 		spin_unlock_irq(&sdev->spinlock);
2449 
2450 	} else
2451 		rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2452 
2453 	if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2454 	    || *(__be64 *)(req->target_port_id + 8) !=
2455 	       cpu_to_be64(srpt_service_guid)) {
2456 		rej->reason = __constant_cpu_to_be32(
2457 				SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2458 		ret = -ENOMEM;
2459 		pr_err("rejected SRP_LOGIN_REQ because it"
2460 		       " has an invalid target port identifier.\n");
2461 		goto reject;
2462 	}
2463 
2464 	ch = kzalloc(sizeof *ch, GFP_KERNEL);
2465 	if (!ch) {
2466 		rej->reason = __constant_cpu_to_be32(
2467 					SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2468 		pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
2469 		ret = -ENOMEM;
2470 		goto reject;
2471 	}
2472 
2473 	INIT_WORK(&ch->release_work, srpt_release_channel_work);
2474 	memcpy(ch->i_port_id, req->initiator_port_id, 16);
2475 	memcpy(ch->t_port_id, req->target_port_id, 16);
2476 	ch->sport = &sdev->port[param->port - 1];
2477 	ch->cm_id = cm_id;
2478 	/*
2479 	 * Avoid QUEUE_FULL conditions by limiting the number of buffers used
2480 	 * for the SRP protocol to the command queue size.
2481 	 */
2482 	ch->rq_size = SRPT_RQ_SIZE;
2483 	spin_lock_init(&ch->spinlock);
2484 	ch->state = CH_CONNECTING;
2485 	INIT_LIST_HEAD(&ch->cmd_wait_list);
2486 	ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2487 
2488 	ch->ioctx_ring = (struct srpt_send_ioctx **)
2489 		srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2490 				      sizeof(*ch->ioctx_ring[0]),
2491 				      ch->rsp_size, DMA_TO_DEVICE);
2492 	if (!ch->ioctx_ring)
2493 		goto free_ch;
2494 
2495 	INIT_LIST_HEAD(&ch->free_list);
2496 	for (i = 0; i < ch->rq_size; i++) {
2497 		ch->ioctx_ring[i]->ch = ch;
2498 		list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
2499 	}
2500 
2501 	ret = srpt_create_ch_ib(ch);
2502 	if (ret) {
2503 		rej->reason = __constant_cpu_to_be32(
2504 				SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2505 		pr_err("rejected SRP_LOGIN_REQ because creating"
2506 		       " a new RDMA channel failed.\n");
2507 		goto free_ring;
2508 	}
2509 
2510 	ret = srpt_ch_qp_rtr(ch, ch->qp);
2511 	if (ret) {
2512 		rej->reason = __constant_cpu_to_be32(
2513 				SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2514 		pr_err("rejected SRP_LOGIN_REQ because enabling"
2515 		       " RTR failed (error code = %d)\n", ret);
2516 		goto destroy_ib;
2517 	}
2518 	/*
2519 	 * Use the initator port identifier as the session name.
2520 	 */
2521 	snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
2522 			be64_to_cpu(*(__be64 *)ch->i_port_id),
2523 			be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
2524 
2525 	pr_debug("registering session %s\n", ch->sess_name);
2526 
2527 	nacl = srpt_lookup_acl(sport, ch->i_port_id);
2528 	if (!nacl) {
2529 		pr_info("Rejected login because no ACL has been"
2530 			" configured yet for initiator %s.\n", ch->sess_name);
2531 		rej->reason = __constant_cpu_to_be32(
2532 				SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2533 		goto destroy_ib;
2534 	}
2535 
2536 	ch->sess = transport_init_session(TARGET_PROT_NORMAL);
2537 	if (IS_ERR(ch->sess)) {
2538 		rej->reason = __constant_cpu_to_be32(
2539 				SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2540 		pr_debug("Failed to create session\n");
2541 		goto deregister_session;
2542 	}
2543 	ch->sess->se_node_acl = &nacl->nacl;
2544 	transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
2545 
2546 	pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
2547 		 ch->sess_name, ch->cm_id);
2548 
2549 	/* create srp_login_response */
2550 	rsp->opcode = SRP_LOGIN_RSP;
2551 	rsp->tag = req->tag;
2552 	rsp->max_it_iu_len = req->req_it_iu_len;
2553 	rsp->max_ti_iu_len = req->req_it_iu_len;
2554 	ch->max_ti_iu_len = it_iu_len;
2555 	rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2556 					      | SRP_BUF_FORMAT_INDIRECT);
2557 	rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2558 	atomic_set(&ch->req_lim, ch->rq_size);
2559 	atomic_set(&ch->req_lim_delta, 0);
2560 
2561 	/* create cm reply */
2562 	rep_param->qp_num = ch->qp->qp_num;
2563 	rep_param->private_data = (void *)rsp;
2564 	rep_param->private_data_len = sizeof *rsp;
2565 	rep_param->rnr_retry_count = 7;
2566 	rep_param->flow_control = 1;
2567 	rep_param->failover_accepted = 0;
2568 	rep_param->srq = 1;
2569 	rep_param->responder_resources = 4;
2570 	rep_param->initiator_depth = 4;
2571 
2572 	ret = ib_send_cm_rep(cm_id, rep_param);
2573 	if (ret) {
2574 		pr_err("sending SRP_LOGIN_REQ response failed"
2575 		       " (error code = %d)\n", ret);
2576 		goto release_channel;
2577 	}
2578 
2579 	spin_lock_irq(&sdev->spinlock);
2580 	list_add_tail(&ch->list, &sdev->rch_list);
2581 	spin_unlock_irq(&sdev->spinlock);
2582 
2583 	goto out;
2584 
2585 release_channel:
2586 	srpt_set_ch_state(ch, CH_RELEASING);
2587 	transport_deregister_session_configfs(ch->sess);
2588 
2589 deregister_session:
2590 	transport_deregister_session(ch->sess);
2591 	ch->sess = NULL;
2592 
2593 destroy_ib:
2594 	srpt_destroy_ch_ib(ch);
2595 
2596 free_ring:
2597 	srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2598 			     ch->sport->sdev, ch->rq_size,
2599 			     ch->rsp_size, DMA_TO_DEVICE);
2600 free_ch:
2601 	kfree(ch);
2602 
2603 reject:
2604 	rej->opcode = SRP_LOGIN_REJ;
2605 	rej->tag = req->tag;
2606 	rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
2607 					      | SRP_BUF_FORMAT_INDIRECT);
2608 
2609 	ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2610 			     (void *)rej, sizeof *rej);
2611 
2612 out:
2613 	kfree(rep_param);
2614 	kfree(rsp);
2615 	kfree(rej);
2616 
2617 	return ret;
2618 }
2619 
srpt_cm_rej_recv(struct ib_cm_id * cm_id)2620 static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
2621 {
2622 	pr_info("Received IB REJ for cm_id %p.\n", cm_id);
2623 	srpt_drain_channel(cm_id);
2624 }
2625 
2626 /**
2627  * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
2628  *
2629  * An IB_CM_RTU_RECEIVED message indicates that the connection is established
2630  * and that the recipient may begin transmitting (RTU = ready to use).
2631  */
srpt_cm_rtu_recv(struct ib_cm_id * cm_id)2632 static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
2633 {
2634 	struct srpt_rdma_ch *ch;
2635 	int ret;
2636 
2637 	ch = srpt_find_channel(cm_id->context, cm_id);
2638 	BUG_ON(!ch);
2639 
2640 	if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
2641 		struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
2642 
2643 		ret = srpt_ch_qp_rts(ch, ch->qp);
2644 
2645 		list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
2646 					 wait_list) {
2647 			list_del(&ioctx->wait_list);
2648 			srpt_handle_new_iu(ch, ioctx, NULL);
2649 		}
2650 		if (ret)
2651 			srpt_close_ch(ch);
2652 	}
2653 }
2654 
srpt_cm_timewait_exit(struct ib_cm_id * cm_id)2655 static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
2656 {
2657 	pr_info("Received IB TimeWait exit for cm_id %p.\n", cm_id);
2658 	srpt_drain_channel(cm_id);
2659 }
2660 
srpt_cm_rep_error(struct ib_cm_id * cm_id)2661 static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
2662 {
2663 	pr_info("Received IB REP error for cm_id %p.\n", cm_id);
2664 	srpt_drain_channel(cm_id);
2665 }
2666 
2667 /**
2668  * srpt_cm_dreq_recv() - Process reception of a DREQ message.
2669  */
srpt_cm_dreq_recv(struct ib_cm_id * cm_id)2670 static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
2671 {
2672 	struct srpt_rdma_ch *ch;
2673 	unsigned long flags;
2674 	bool send_drep = false;
2675 
2676 	ch = srpt_find_channel(cm_id->context, cm_id);
2677 	BUG_ON(!ch);
2678 
2679 	pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
2680 
2681 	spin_lock_irqsave(&ch->spinlock, flags);
2682 	switch (ch->state) {
2683 	case CH_CONNECTING:
2684 	case CH_LIVE:
2685 		send_drep = true;
2686 		ch->state = CH_DISCONNECTING;
2687 		break;
2688 	case CH_DISCONNECTING:
2689 	case CH_DRAINING:
2690 	case CH_RELEASING:
2691 		WARN(true, "unexpected channel state %d\n", ch->state);
2692 		break;
2693 	}
2694 	spin_unlock_irqrestore(&ch->spinlock, flags);
2695 
2696 	if (send_drep) {
2697 		if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
2698 			pr_err("Sending IB DREP failed.\n");
2699 		pr_info("Received DREQ and sent DREP for session %s.\n",
2700 			ch->sess_name);
2701 	}
2702 }
2703 
2704 /**
2705  * srpt_cm_drep_recv() - Process reception of a DREP message.
2706  */
srpt_cm_drep_recv(struct ib_cm_id * cm_id)2707 static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
2708 {
2709 	pr_info("Received InfiniBand DREP message for cm_id %p.\n", cm_id);
2710 	srpt_drain_channel(cm_id);
2711 }
2712 
2713 /**
2714  * srpt_cm_handler() - IB connection manager callback function.
2715  *
2716  * A non-zero return value will cause the caller destroy the CM ID.
2717  *
2718  * Note: srpt_cm_handler() must only return a non-zero value when transferring
2719  * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2720  * a non-zero value in any other case will trigger a race with the
2721  * ib_destroy_cm_id() call in srpt_release_channel().
2722  */
srpt_cm_handler(struct ib_cm_id * cm_id,struct ib_cm_event * event)2723 static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
2724 {
2725 	int ret;
2726 
2727 	ret = 0;
2728 	switch (event->event) {
2729 	case IB_CM_REQ_RECEIVED:
2730 		ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
2731 				       event->private_data);
2732 		break;
2733 	case IB_CM_REJ_RECEIVED:
2734 		srpt_cm_rej_recv(cm_id);
2735 		break;
2736 	case IB_CM_RTU_RECEIVED:
2737 	case IB_CM_USER_ESTABLISHED:
2738 		srpt_cm_rtu_recv(cm_id);
2739 		break;
2740 	case IB_CM_DREQ_RECEIVED:
2741 		srpt_cm_dreq_recv(cm_id);
2742 		break;
2743 	case IB_CM_DREP_RECEIVED:
2744 		srpt_cm_drep_recv(cm_id);
2745 		break;
2746 	case IB_CM_TIMEWAIT_EXIT:
2747 		srpt_cm_timewait_exit(cm_id);
2748 		break;
2749 	case IB_CM_REP_ERROR:
2750 		srpt_cm_rep_error(cm_id);
2751 		break;
2752 	case IB_CM_DREQ_ERROR:
2753 		pr_info("Received IB DREQ ERROR event.\n");
2754 		break;
2755 	case IB_CM_MRA_RECEIVED:
2756 		pr_info("Received IB MRA event\n");
2757 		break;
2758 	default:
2759 		pr_err("received unrecognized IB CM event %d\n", event->event);
2760 		break;
2761 	}
2762 
2763 	return ret;
2764 }
2765 
2766 /**
2767  * srpt_perform_rdmas() - Perform IB RDMA.
2768  *
2769  * Returns zero upon success or a negative number upon failure.
2770  */
srpt_perform_rdmas(struct srpt_rdma_ch * ch,struct srpt_send_ioctx * ioctx)2771 static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
2772 			      struct srpt_send_ioctx *ioctx)
2773 {
2774 	struct ib_send_wr wr;
2775 	struct ib_send_wr *bad_wr;
2776 	struct rdma_iu *riu;
2777 	int i;
2778 	int ret;
2779 	int sq_wr_avail;
2780 	enum dma_data_direction dir;
2781 	const int n_rdma = ioctx->n_rdma;
2782 
2783 	dir = ioctx->cmd.data_direction;
2784 	if (dir == DMA_TO_DEVICE) {
2785 		/* write */
2786 		ret = -ENOMEM;
2787 		sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
2788 		if (sq_wr_avail < 0) {
2789 			pr_warn("IB send queue full (needed %d)\n",
2790 				n_rdma);
2791 			goto out;
2792 		}
2793 	}
2794 
2795 	ioctx->rdma_aborted = false;
2796 	ret = 0;
2797 	riu = ioctx->rdma_ius;
2798 	memset(&wr, 0, sizeof wr);
2799 
2800 	for (i = 0; i < n_rdma; ++i, ++riu) {
2801 		if (dir == DMA_FROM_DEVICE) {
2802 			wr.opcode = IB_WR_RDMA_WRITE;
2803 			wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2804 						SRPT_RDMA_WRITE_LAST :
2805 						SRPT_RDMA_MID,
2806 						ioctx->ioctx.index);
2807 		} else {
2808 			wr.opcode = IB_WR_RDMA_READ;
2809 			wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
2810 						SRPT_RDMA_READ_LAST :
2811 						SRPT_RDMA_MID,
2812 						ioctx->ioctx.index);
2813 		}
2814 		wr.next = NULL;
2815 		wr.wr.rdma.remote_addr = riu->raddr;
2816 		wr.wr.rdma.rkey = riu->rkey;
2817 		wr.num_sge = riu->sge_cnt;
2818 		wr.sg_list = riu->sge;
2819 
2820 		/* only get completion event for the last rdma write */
2821 		if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
2822 			wr.send_flags = IB_SEND_SIGNALED;
2823 
2824 		ret = ib_post_send(ch->qp, &wr, &bad_wr);
2825 		if (ret)
2826 			break;
2827 	}
2828 
2829 	if (ret)
2830 		pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
2831 				 __func__, __LINE__, ret, i, n_rdma);
2832 	if (ret && i > 0) {
2833 		wr.num_sge = 0;
2834 		wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
2835 		wr.send_flags = IB_SEND_SIGNALED;
2836 		while (ch->state == CH_LIVE &&
2837 			ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
2838 			pr_info("Trying to abort failed RDMA transfer [%d]\n",
2839 				ioctx->ioctx.index);
2840 			msleep(1000);
2841 		}
2842 		while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
2843 			pr_info("Waiting until RDMA abort finished [%d]\n",
2844 				ioctx->ioctx.index);
2845 			msleep(1000);
2846 		}
2847 	}
2848 out:
2849 	if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
2850 		atomic_add(n_rdma, &ch->sq_wr_avail);
2851 	return ret;
2852 }
2853 
2854 /**
2855  * srpt_xfer_data() - Start data transfer from initiator to target.
2856  */
srpt_xfer_data(struct srpt_rdma_ch * ch,struct srpt_send_ioctx * ioctx)2857 static int srpt_xfer_data(struct srpt_rdma_ch *ch,
2858 			  struct srpt_send_ioctx *ioctx)
2859 {
2860 	int ret;
2861 
2862 	ret = srpt_map_sg_to_ib_sge(ch, ioctx);
2863 	if (ret) {
2864 		pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
2865 		goto out;
2866 	}
2867 
2868 	ret = srpt_perform_rdmas(ch, ioctx);
2869 	if (ret) {
2870 		if (ret == -EAGAIN || ret == -ENOMEM)
2871 			pr_info("%s[%d] queue full -- ret=%d\n",
2872 				__func__, __LINE__, ret);
2873 		else
2874 			pr_err("%s[%d] fatal error -- ret=%d\n",
2875 			       __func__, __LINE__, ret);
2876 		goto out_unmap;
2877 	}
2878 
2879 out:
2880 	return ret;
2881 out_unmap:
2882 	srpt_unmap_sg_to_ib_sge(ch, ioctx);
2883 	goto out;
2884 }
2885 
srpt_write_pending_status(struct se_cmd * se_cmd)2886 static int srpt_write_pending_status(struct se_cmd *se_cmd)
2887 {
2888 	struct srpt_send_ioctx *ioctx;
2889 
2890 	ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2891 	return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
2892 }
2893 
2894 /*
2895  * srpt_write_pending() - Start data transfer from initiator to target (write).
2896  */
srpt_write_pending(struct se_cmd * se_cmd)2897 static int srpt_write_pending(struct se_cmd *se_cmd)
2898 {
2899 	struct srpt_rdma_ch *ch;
2900 	struct srpt_send_ioctx *ioctx;
2901 	enum srpt_command_state new_state;
2902 	enum rdma_ch_state ch_state;
2903 	int ret;
2904 
2905 	ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
2906 
2907 	new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2908 	WARN_ON(new_state == SRPT_STATE_DONE);
2909 
2910 	ch = ioctx->ch;
2911 	BUG_ON(!ch);
2912 
2913 	ch_state = srpt_get_ch_state(ch);
2914 	switch (ch_state) {
2915 	case CH_CONNECTING:
2916 		WARN(true, "unexpected channel state %d\n", ch_state);
2917 		ret = -EINVAL;
2918 		goto out;
2919 	case CH_LIVE:
2920 		break;
2921 	case CH_DISCONNECTING:
2922 	case CH_DRAINING:
2923 	case CH_RELEASING:
2924 		pr_debug("cmd with tag %lld: channel disconnecting\n",
2925 			 ioctx->tag);
2926 		srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2927 		ret = -EINVAL;
2928 		goto out;
2929 	}
2930 	ret = srpt_xfer_data(ch, ioctx);
2931 
2932 out:
2933 	return ret;
2934 }
2935 
tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)2936 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2937 {
2938 	switch (tcm_mgmt_status) {
2939 	case TMR_FUNCTION_COMPLETE:
2940 		return SRP_TSK_MGMT_SUCCESS;
2941 	case TMR_FUNCTION_REJECTED:
2942 		return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2943 	}
2944 	return SRP_TSK_MGMT_FAILED;
2945 }
2946 
2947 /**
2948  * srpt_queue_response() - Transmits the response to a SCSI command.
2949  *
2950  * Callback function called by the TCM core. Must not block since it can be
2951  * invoked on the context of the IB completion handler.
2952  */
srpt_queue_response(struct se_cmd * cmd)2953 static void srpt_queue_response(struct se_cmd *cmd)
2954 {
2955 	struct srpt_rdma_ch *ch;
2956 	struct srpt_send_ioctx *ioctx;
2957 	enum srpt_command_state state;
2958 	unsigned long flags;
2959 	int ret;
2960 	enum dma_data_direction dir;
2961 	int resp_len;
2962 	u8 srp_tm_status;
2963 
2964 	ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2965 	ch = ioctx->ch;
2966 	BUG_ON(!ch);
2967 
2968 	spin_lock_irqsave(&ioctx->spinlock, flags);
2969 	state = ioctx->state;
2970 	switch (state) {
2971 	case SRPT_STATE_NEW:
2972 	case SRPT_STATE_DATA_IN:
2973 		ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2974 		break;
2975 	case SRPT_STATE_MGMT:
2976 		ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2977 		break;
2978 	default:
2979 		WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2980 			ch, ioctx->ioctx.index, ioctx->state);
2981 		break;
2982 	}
2983 	spin_unlock_irqrestore(&ioctx->spinlock, flags);
2984 
2985 	if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
2986 		     || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
2987 		atomic_inc(&ch->req_lim_delta);
2988 		srpt_abort_cmd(ioctx);
2989 		return;
2990 	}
2991 
2992 	dir = ioctx->cmd.data_direction;
2993 
2994 	/* For read commands, transfer the data to the initiator. */
2995 	if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
2996 	    !ioctx->queue_status_only) {
2997 		ret = srpt_xfer_data(ch, ioctx);
2998 		if (ret) {
2999 			pr_err("xfer_data failed for tag %llu\n",
3000 			       ioctx->tag);
3001 			return;
3002 		}
3003 	}
3004 
3005 	if (state != SRPT_STATE_MGMT)
3006 		resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
3007 					      cmd->scsi_status);
3008 	else {
3009 		srp_tm_status
3010 			= tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
3011 		resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
3012 						 ioctx->tag);
3013 	}
3014 	ret = srpt_post_send(ch, ioctx, resp_len);
3015 	if (ret) {
3016 		pr_err("sending cmd response failed for tag %llu\n",
3017 		       ioctx->tag);
3018 		srpt_unmap_sg_to_ib_sge(ch, ioctx);
3019 		srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
3020 		target_put_sess_cmd(&ioctx->cmd);
3021 	}
3022 }
3023 
srpt_queue_data_in(struct se_cmd * cmd)3024 static int srpt_queue_data_in(struct se_cmd *cmd)
3025 {
3026 	srpt_queue_response(cmd);
3027 	return 0;
3028 }
3029 
srpt_queue_tm_rsp(struct se_cmd * cmd)3030 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
3031 {
3032 	srpt_queue_response(cmd);
3033 }
3034 
srpt_aborted_task(struct se_cmd * cmd)3035 static void srpt_aborted_task(struct se_cmd *cmd)
3036 {
3037 	struct srpt_send_ioctx *ioctx = container_of(cmd,
3038 				struct srpt_send_ioctx, cmd);
3039 
3040 	srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
3041 }
3042 
srpt_queue_status(struct se_cmd * cmd)3043 static int srpt_queue_status(struct se_cmd *cmd)
3044 {
3045 	struct srpt_send_ioctx *ioctx;
3046 
3047 	ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
3048 	BUG_ON(ioctx->sense_data != cmd->sense_buffer);
3049 	if (cmd->se_cmd_flags &
3050 	    (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
3051 		WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
3052 	ioctx->queue_status_only = true;
3053 	srpt_queue_response(cmd);
3054 	return 0;
3055 }
3056 
srpt_refresh_port_work(struct work_struct * work)3057 static void srpt_refresh_port_work(struct work_struct *work)
3058 {
3059 	struct srpt_port *sport = container_of(work, struct srpt_port, work);
3060 
3061 	srpt_refresh_port(sport);
3062 }
3063 
srpt_ch_list_empty(struct srpt_device * sdev)3064 static int srpt_ch_list_empty(struct srpt_device *sdev)
3065 {
3066 	int res;
3067 
3068 	spin_lock_irq(&sdev->spinlock);
3069 	res = list_empty(&sdev->rch_list);
3070 	spin_unlock_irq(&sdev->spinlock);
3071 
3072 	return res;
3073 }
3074 
3075 /**
3076  * srpt_release_sdev() - Free the channel resources associated with a target.
3077  */
srpt_release_sdev(struct srpt_device * sdev)3078 static int srpt_release_sdev(struct srpt_device *sdev)
3079 {
3080 	struct srpt_rdma_ch *ch, *tmp_ch;
3081 	int res;
3082 
3083 	WARN_ON_ONCE(irqs_disabled());
3084 
3085 	BUG_ON(!sdev);
3086 
3087 	spin_lock_irq(&sdev->spinlock);
3088 	list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
3089 		__srpt_close_ch(ch);
3090 	spin_unlock_irq(&sdev->spinlock);
3091 
3092 	res = wait_event_interruptible(sdev->ch_releaseQ,
3093 				       srpt_ch_list_empty(sdev));
3094 	if (res)
3095 		pr_err("%s: interrupted.\n", __func__);
3096 
3097 	return 0;
3098 }
3099 
__srpt_lookup_port(const char * name)3100 static struct srpt_port *__srpt_lookup_port(const char *name)
3101 {
3102 	struct ib_device *dev;
3103 	struct srpt_device *sdev;
3104 	struct srpt_port *sport;
3105 	int i;
3106 
3107 	list_for_each_entry(sdev, &srpt_dev_list, list) {
3108 		dev = sdev->device;
3109 		if (!dev)
3110 			continue;
3111 
3112 		for (i = 0; i < dev->phys_port_cnt; i++) {
3113 			sport = &sdev->port[i];
3114 
3115 			if (!strcmp(sport->port_guid, name))
3116 				return sport;
3117 		}
3118 	}
3119 
3120 	return NULL;
3121 }
3122 
srpt_lookup_port(const char * name)3123 static struct srpt_port *srpt_lookup_port(const char *name)
3124 {
3125 	struct srpt_port *sport;
3126 
3127 	spin_lock(&srpt_dev_lock);
3128 	sport = __srpt_lookup_port(name);
3129 	spin_unlock(&srpt_dev_lock);
3130 
3131 	return sport;
3132 }
3133 
3134 /**
3135  * srpt_add_one() - Infiniband device addition callback function.
3136  */
srpt_add_one(struct ib_device * device)3137 static void srpt_add_one(struct ib_device *device)
3138 {
3139 	struct srpt_device *sdev;
3140 	struct srpt_port *sport;
3141 	struct ib_srq_init_attr srq_attr;
3142 	int i;
3143 
3144 	pr_debug("device = %p, device->dma_ops = %p\n", device,
3145 		 device->dma_ops);
3146 
3147 	sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
3148 	if (!sdev)
3149 		goto err;
3150 
3151 	sdev->device = device;
3152 	INIT_LIST_HEAD(&sdev->rch_list);
3153 	init_waitqueue_head(&sdev->ch_releaseQ);
3154 	spin_lock_init(&sdev->spinlock);
3155 
3156 	if (ib_query_device(device, &sdev->dev_attr))
3157 		goto free_dev;
3158 
3159 	sdev->pd = ib_alloc_pd(device);
3160 	if (IS_ERR(sdev->pd))
3161 		goto free_dev;
3162 
3163 	sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
3164 	if (IS_ERR(sdev->mr))
3165 		goto err_pd;
3166 
3167 	sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
3168 
3169 	srq_attr.event_handler = srpt_srq_event;
3170 	srq_attr.srq_context = (void *)sdev;
3171 	srq_attr.attr.max_wr = sdev->srq_size;
3172 	srq_attr.attr.max_sge = 1;
3173 	srq_attr.attr.srq_limit = 0;
3174 	srq_attr.srq_type = IB_SRQT_BASIC;
3175 
3176 	sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
3177 	if (IS_ERR(sdev->srq))
3178 		goto err_mr;
3179 
3180 	pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
3181 		 __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
3182 		 device->name);
3183 
3184 	if (!srpt_service_guid)
3185 		srpt_service_guid = be64_to_cpu(device->node_guid);
3186 
3187 	sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3188 	if (IS_ERR(sdev->cm_id))
3189 		goto err_srq;
3190 
3191 	/* print out target login information */
3192 	pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
3193 		 "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
3194 		 srpt_service_guid, srpt_service_guid);
3195 
3196 	/*
3197 	 * We do not have a consistent service_id (ie. also id_ext of target_id)
3198 	 * to identify this target. We currently use the guid of the first HCA
3199 	 * in the system as service_id; therefore, the target_id will change
3200 	 * if this HCA is gone bad and replaced by different HCA
3201 	 */
3202 	if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
3203 		goto err_cm;
3204 
3205 	INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3206 			      srpt_event_handler);
3207 	if (ib_register_event_handler(&sdev->event_handler))
3208 		goto err_cm;
3209 
3210 	sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3211 		srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3212 				      sizeof(*sdev->ioctx_ring[0]),
3213 				      srp_max_req_size, DMA_FROM_DEVICE);
3214 	if (!sdev->ioctx_ring)
3215 		goto err_event;
3216 
3217 	for (i = 0; i < sdev->srq_size; ++i)
3218 		srpt_post_recv(sdev, sdev->ioctx_ring[i]);
3219 
3220 	WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
3221 
3222 	for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3223 		sport = &sdev->port[i - 1];
3224 		sport->sdev = sdev;
3225 		sport->port = i;
3226 		sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3227 		sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3228 		sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3229 		INIT_WORK(&sport->work, srpt_refresh_port_work);
3230 		INIT_LIST_HEAD(&sport->port_acl_list);
3231 		spin_lock_init(&sport->port_acl_lock);
3232 
3233 		if (srpt_refresh_port(sport)) {
3234 			pr_err("MAD registration failed for %s-%d.\n",
3235 			       srpt_sdev_name(sdev), i);
3236 			goto err_ring;
3237 		}
3238 		snprintf(sport->port_guid, sizeof(sport->port_guid),
3239 			"0x%016llx%016llx",
3240 			be64_to_cpu(sport->gid.global.subnet_prefix),
3241 			be64_to_cpu(sport->gid.global.interface_id));
3242 	}
3243 
3244 	spin_lock(&srpt_dev_lock);
3245 	list_add_tail(&sdev->list, &srpt_dev_list);
3246 	spin_unlock(&srpt_dev_lock);
3247 
3248 out:
3249 	ib_set_client_data(device, &srpt_client, sdev);
3250 	pr_debug("added %s.\n", device->name);
3251 	return;
3252 
3253 err_ring:
3254 	srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3255 			     sdev->srq_size, srp_max_req_size,
3256 			     DMA_FROM_DEVICE);
3257 err_event:
3258 	ib_unregister_event_handler(&sdev->event_handler);
3259 err_cm:
3260 	ib_destroy_cm_id(sdev->cm_id);
3261 err_srq:
3262 	ib_destroy_srq(sdev->srq);
3263 err_mr:
3264 	ib_dereg_mr(sdev->mr);
3265 err_pd:
3266 	ib_dealloc_pd(sdev->pd);
3267 free_dev:
3268 	kfree(sdev);
3269 err:
3270 	sdev = NULL;
3271 	pr_info("%s(%s) failed.\n", __func__, device->name);
3272 	goto out;
3273 }
3274 
3275 /**
3276  * srpt_remove_one() - InfiniBand device removal callback function.
3277  */
srpt_remove_one(struct ib_device * device)3278 static void srpt_remove_one(struct ib_device *device)
3279 {
3280 	struct srpt_device *sdev;
3281 	int i;
3282 
3283 	sdev = ib_get_client_data(device, &srpt_client);
3284 	if (!sdev) {
3285 		pr_info("%s(%s): nothing to do.\n", __func__, device->name);
3286 		return;
3287 	}
3288 
3289 	srpt_unregister_mad_agent(sdev);
3290 
3291 	ib_unregister_event_handler(&sdev->event_handler);
3292 
3293 	/* Cancel any work queued by the just unregistered IB event handler. */
3294 	for (i = 0; i < sdev->device->phys_port_cnt; i++)
3295 		cancel_work_sync(&sdev->port[i].work);
3296 
3297 	ib_destroy_cm_id(sdev->cm_id);
3298 
3299 	/*
3300 	 * Unregistering a target must happen after destroying sdev->cm_id
3301 	 * such that no new SRP_LOGIN_REQ information units can arrive while
3302 	 * destroying the target.
3303 	 */
3304 	spin_lock(&srpt_dev_lock);
3305 	list_del(&sdev->list);
3306 	spin_unlock(&srpt_dev_lock);
3307 	srpt_release_sdev(sdev);
3308 
3309 	ib_destroy_srq(sdev->srq);
3310 	ib_dereg_mr(sdev->mr);
3311 	ib_dealloc_pd(sdev->pd);
3312 
3313 	srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3314 			     sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
3315 	sdev->ioctx_ring = NULL;
3316 	kfree(sdev);
3317 }
3318 
3319 static struct ib_client srpt_client = {
3320 	.name = DRV_NAME,
3321 	.add = srpt_add_one,
3322 	.remove = srpt_remove_one
3323 };
3324 
srpt_check_true(struct se_portal_group * se_tpg)3325 static int srpt_check_true(struct se_portal_group *se_tpg)
3326 {
3327 	return 1;
3328 }
3329 
srpt_check_false(struct se_portal_group * se_tpg)3330 static int srpt_check_false(struct se_portal_group *se_tpg)
3331 {
3332 	return 0;
3333 }
3334 
srpt_get_fabric_name(void)3335 static char *srpt_get_fabric_name(void)
3336 {
3337 	return "srpt";
3338 }
3339 
srpt_get_fabric_proto_ident(struct se_portal_group * se_tpg)3340 static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
3341 {
3342 	return SCSI_TRANSPORTID_PROTOCOLID_SRP;
3343 }
3344 
srpt_get_fabric_wwn(struct se_portal_group * tpg)3345 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3346 {
3347 	struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3348 
3349 	return sport->port_guid;
3350 }
3351 
srpt_get_tag(struct se_portal_group * tpg)3352 static u16 srpt_get_tag(struct se_portal_group *tpg)
3353 {
3354 	return 1;
3355 }
3356 
srpt_get_default_depth(struct se_portal_group * se_tpg)3357 static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
3358 {
3359 	return 1;
3360 }
3361 
srpt_get_pr_transport_id(struct se_portal_group * se_tpg,struct se_node_acl * se_nacl,struct t10_pr_registration * pr_reg,int * format_code,unsigned char * buf)3362 static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
3363 				    struct se_node_acl *se_nacl,
3364 				    struct t10_pr_registration *pr_reg,
3365 				    int *format_code, unsigned char *buf)
3366 {
3367 	struct srpt_node_acl *nacl;
3368 	struct spc_rdma_transport_id *tr_id;
3369 
3370 	nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3371 	tr_id = (void *)buf;
3372 	tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
3373 	memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
3374 	return sizeof(*tr_id);
3375 }
3376 
srpt_get_pr_transport_id_len(struct se_portal_group * se_tpg,struct se_node_acl * se_nacl,struct t10_pr_registration * pr_reg,int * format_code)3377 static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
3378 					struct se_node_acl *se_nacl,
3379 					struct t10_pr_registration *pr_reg,
3380 					int *format_code)
3381 {
3382 	*format_code = 0;
3383 	return sizeof(struct spc_rdma_transport_id);
3384 }
3385 
srpt_parse_pr_out_transport_id(struct se_portal_group * se_tpg,const char * buf,u32 * out_tid_len,char ** port_nexus_ptr)3386 static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
3387 					    const char *buf, u32 *out_tid_len,
3388 					    char **port_nexus_ptr)
3389 {
3390 	struct spc_rdma_transport_id *tr_id;
3391 
3392 	*port_nexus_ptr = NULL;
3393 	*out_tid_len = sizeof(struct spc_rdma_transport_id);
3394 	tr_id = (void *)buf;
3395 	return (char *)tr_id->i_port_id;
3396 }
3397 
srpt_alloc_fabric_acl(struct se_portal_group * se_tpg)3398 static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
3399 {
3400 	struct srpt_node_acl *nacl;
3401 
3402 	nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
3403 	if (!nacl) {
3404 		pr_err("Unable to allocate struct srpt_node_acl\n");
3405 		return NULL;
3406 	}
3407 
3408 	return &nacl->nacl;
3409 }
3410 
srpt_release_fabric_acl(struct se_portal_group * se_tpg,struct se_node_acl * se_nacl)3411 static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
3412 				    struct se_node_acl *se_nacl)
3413 {
3414 	struct srpt_node_acl *nacl;
3415 
3416 	nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3417 	kfree(nacl);
3418 }
3419 
srpt_tpg_get_inst_index(struct se_portal_group * se_tpg)3420 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3421 {
3422 	return 1;
3423 }
3424 
srpt_release_cmd(struct se_cmd * se_cmd)3425 static void srpt_release_cmd(struct se_cmd *se_cmd)
3426 {
3427 	struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3428 				struct srpt_send_ioctx, cmd);
3429 	struct srpt_rdma_ch *ch = ioctx->ch;
3430 	unsigned long flags;
3431 
3432 	WARN_ON(ioctx->state != SRPT_STATE_DONE);
3433 	WARN_ON(ioctx->mapped_sg_count != 0);
3434 
3435 	if (ioctx->n_rbuf > 1) {
3436 		kfree(ioctx->rbufs);
3437 		ioctx->rbufs = NULL;
3438 		ioctx->n_rbuf = 0;
3439 	}
3440 
3441 	spin_lock_irqsave(&ch->spinlock, flags);
3442 	list_add(&ioctx->free_list, &ch->free_list);
3443 	spin_unlock_irqrestore(&ch->spinlock, flags);
3444 }
3445 
3446 /**
3447  * srpt_close_session() - Forcibly close a session.
3448  *
3449  * Callback function invoked by the TCM core to clean up sessions associated
3450  * with a node ACL when the user invokes
3451  * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3452  */
srpt_close_session(struct se_session * se_sess)3453 static void srpt_close_session(struct se_session *se_sess)
3454 {
3455 	DECLARE_COMPLETION_ONSTACK(release_done);
3456 	struct srpt_rdma_ch *ch;
3457 	struct srpt_device *sdev;
3458 	unsigned long res;
3459 
3460 	ch = se_sess->fabric_sess_ptr;
3461 	WARN_ON(ch->sess != se_sess);
3462 
3463 	pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
3464 
3465 	sdev = ch->sport->sdev;
3466 	spin_lock_irq(&sdev->spinlock);
3467 	BUG_ON(ch->release_done);
3468 	ch->release_done = &release_done;
3469 	__srpt_close_ch(ch);
3470 	spin_unlock_irq(&sdev->spinlock);
3471 
3472 	res = wait_for_completion_timeout(&release_done, 60 * HZ);
3473 	WARN_ON(res == 0);
3474 }
3475 
3476 /**
3477  * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
3478  *
3479  * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3480  * This object represents an arbitrary integer used to uniquely identify a
3481  * particular attached remote initiator port to a particular SCSI target port
3482  * within a particular SCSI target device within a particular SCSI instance.
3483  */
srpt_sess_get_index(struct se_session * se_sess)3484 static u32 srpt_sess_get_index(struct se_session *se_sess)
3485 {
3486 	return 0;
3487 }
3488 
srpt_set_default_node_attrs(struct se_node_acl * nacl)3489 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3490 {
3491 }
3492 
srpt_get_task_tag(struct se_cmd * se_cmd)3493 static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
3494 {
3495 	struct srpt_send_ioctx *ioctx;
3496 
3497 	ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3498 	return ioctx->tag;
3499 }
3500 
3501 /* Note: only used from inside debug printk's by the TCM core. */
srpt_get_tcm_cmd_state(struct se_cmd * se_cmd)3502 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3503 {
3504 	struct srpt_send_ioctx *ioctx;
3505 
3506 	ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3507 	return srpt_get_cmd_state(ioctx);
3508 }
3509 
3510 /**
3511  * srpt_parse_i_port_id() - Parse an initiator port ID.
3512  * @name: ASCII representation of a 128-bit initiator port ID.
3513  * @i_port_id: Binary 128-bit port ID.
3514  */
srpt_parse_i_port_id(u8 i_port_id[16],const char * name)3515 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3516 {
3517 	const char *p;
3518 	unsigned len, count, leading_zero_bytes;
3519 	int ret, rc;
3520 
3521 	p = name;
3522 	if (strncasecmp(p, "0x", 2) == 0)
3523 		p += 2;
3524 	ret = -EINVAL;
3525 	len = strlen(p);
3526 	if (len % 2)
3527 		goto out;
3528 	count = min(len / 2, 16U);
3529 	leading_zero_bytes = 16 - count;
3530 	memset(i_port_id, 0, leading_zero_bytes);
3531 	rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
3532 	if (rc < 0)
3533 		pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
3534 	ret = 0;
3535 out:
3536 	return ret;
3537 }
3538 
3539 /*
3540  * configfs callback function invoked for
3541  * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3542  */
srpt_make_nodeacl(struct se_portal_group * tpg,struct config_group * group,const char * name)3543 static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
3544 					     struct config_group *group,
3545 					     const char *name)
3546 {
3547 	struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
3548 	struct se_node_acl *se_nacl, *se_nacl_new;
3549 	struct srpt_node_acl *nacl;
3550 	int ret = 0;
3551 	u32 nexus_depth = 1;
3552 	u8 i_port_id[16];
3553 
3554 	if (srpt_parse_i_port_id(i_port_id, name) < 0) {
3555 		pr_err("invalid initiator port ID %s\n", name);
3556 		ret = -EINVAL;
3557 		goto err;
3558 	}
3559 
3560 	se_nacl_new = srpt_alloc_fabric_acl(tpg);
3561 	if (!se_nacl_new) {
3562 		ret = -ENOMEM;
3563 		goto err;
3564 	}
3565 	/*
3566 	 * nacl_new may be released by core_tpg_add_initiator_node_acl()
3567 	 * when converting a node ACL from demo mode to explict
3568 	 */
3569 	se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
3570 						  nexus_depth);
3571 	if (IS_ERR(se_nacl)) {
3572 		ret = PTR_ERR(se_nacl);
3573 		goto err;
3574 	}
3575 	/* Locate our struct srpt_node_acl and set sdev and i_port_id. */
3576 	nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3577 	memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
3578 	nacl->sport = sport;
3579 
3580 	spin_lock_irq(&sport->port_acl_lock);
3581 	list_add_tail(&nacl->list, &sport->port_acl_list);
3582 	spin_unlock_irq(&sport->port_acl_lock);
3583 
3584 	return se_nacl;
3585 err:
3586 	return ERR_PTR(ret);
3587 }
3588 
3589 /*
3590  * configfs callback function invoked for
3591  * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3592  */
srpt_drop_nodeacl(struct se_node_acl * se_nacl)3593 static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
3594 {
3595 	struct srpt_node_acl *nacl;
3596 	struct srpt_device *sdev;
3597 	struct srpt_port *sport;
3598 
3599 	nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
3600 	sport = nacl->sport;
3601 	sdev = sport->sdev;
3602 	spin_lock_irq(&sport->port_acl_lock);
3603 	list_del(&nacl->list);
3604 	spin_unlock_irq(&sport->port_acl_lock);
3605 	core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
3606 	srpt_release_fabric_acl(NULL, se_nacl);
3607 }
3608 
srpt_tpg_attrib_show_srp_max_rdma_size(struct se_portal_group * se_tpg,char * page)3609 static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
3610 	struct se_portal_group *se_tpg,
3611 	char *page)
3612 {
3613 	struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3614 
3615 	return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3616 }
3617 
srpt_tpg_attrib_store_srp_max_rdma_size(struct se_portal_group * se_tpg,const char * page,size_t count)3618 static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
3619 	struct se_portal_group *se_tpg,
3620 	const char *page,
3621 	size_t count)
3622 {
3623 	struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3624 	unsigned long val;
3625 	int ret;
3626 
3627 	ret = kstrtoul(page, 0, &val);
3628 	if (ret < 0) {
3629 		pr_err("kstrtoul() failed with ret: %d\n", ret);
3630 		return -EINVAL;
3631 	}
3632 	if (val > MAX_SRPT_RDMA_SIZE) {
3633 		pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3634 			MAX_SRPT_RDMA_SIZE);
3635 		return -EINVAL;
3636 	}
3637 	if (val < DEFAULT_MAX_RDMA_SIZE) {
3638 		pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3639 			val, DEFAULT_MAX_RDMA_SIZE);
3640 		return -EINVAL;
3641 	}
3642 	sport->port_attrib.srp_max_rdma_size = val;
3643 
3644 	return count;
3645 }
3646 
3647 TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
3648 
srpt_tpg_attrib_show_srp_max_rsp_size(struct se_portal_group * se_tpg,char * page)3649 static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
3650 	struct se_portal_group *se_tpg,
3651 	char *page)
3652 {
3653 	struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3654 
3655 	return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3656 }
3657 
srpt_tpg_attrib_store_srp_max_rsp_size(struct se_portal_group * se_tpg,const char * page,size_t count)3658 static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
3659 	struct se_portal_group *se_tpg,
3660 	const char *page,
3661 	size_t count)
3662 {
3663 	struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3664 	unsigned long val;
3665 	int ret;
3666 
3667 	ret = kstrtoul(page, 0, &val);
3668 	if (ret < 0) {
3669 		pr_err("kstrtoul() failed with ret: %d\n", ret);
3670 		return -EINVAL;
3671 	}
3672 	if (val > MAX_SRPT_RSP_SIZE) {
3673 		pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3674 			MAX_SRPT_RSP_SIZE);
3675 		return -EINVAL;
3676 	}
3677 	if (val < MIN_MAX_RSP_SIZE) {
3678 		pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3679 			MIN_MAX_RSP_SIZE);
3680 		return -EINVAL;
3681 	}
3682 	sport->port_attrib.srp_max_rsp_size = val;
3683 
3684 	return count;
3685 }
3686 
3687 TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
3688 
srpt_tpg_attrib_show_srp_sq_size(struct se_portal_group * se_tpg,char * page)3689 static ssize_t srpt_tpg_attrib_show_srp_sq_size(
3690 	struct se_portal_group *se_tpg,
3691 	char *page)
3692 {
3693 	struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3694 
3695 	return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3696 }
3697 
srpt_tpg_attrib_store_srp_sq_size(struct se_portal_group * se_tpg,const char * page,size_t count)3698 static ssize_t srpt_tpg_attrib_store_srp_sq_size(
3699 	struct se_portal_group *se_tpg,
3700 	const char *page,
3701 	size_t count)
3702 {
3703 	struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3704 	unsigned long val;
3705 	int ret;
3706 
3707 	ret = kstrtoul(page, 0, &val);
3708 	if (ret < 0) {
3709 		pr_err("kstrtoul() failed with ret: %d\n", ret);
3710 		return -EINVAL;
3711 	}
3712 	if (val > MAX_SRPT_SRQ_SIZE) {
3713 		pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3714 			MAX_SRPT_SRQ_SIZE);
3715 		return -EINVAL;
3716 	}
3717 	if (val < MIN_SRPT_SRQ_SIZE) {
3718 		pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3719 			MIN_SRPT_SRQ_SIZE);
3720 		return -EINVAL;
3721 	}
3722 	sport->port_attrib.srp_sq_size = val;
3723 
3724 	return count;
3725 }
3726 
3727 TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
3728 
3729 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3730 	&srpt_tpg_attrib_srp_max_rdma_size.attr,
3731 	&srpt_tpg_attrib_srp_max_rsp_size.attr,
3732 	&srpt_tpg_attrib_srp_sq_size.attr,
3733 	NULL,
3734 };
3735 
srpt_tpg_show_enable(struct se_portal_group * se_tpg,char * page)3736 static ssize_t srpt_tpg_show_enable(
3737 	struct se_portal_group *se_tpg,
3738 	char *page)
3739 {
3740 	struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3741 
3742 	return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
3743 }
3744 
srpt_tpg_store_enable(struct se_portal_group * se_tpg,const char * page,size_t count)3745 static ssize_t srpt_tpg_store_enable(
3746 	struct se_portal_group *se_tpg,
3747 	const char *page,
3748 	size_t count)
3749 {
3750 	struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
3751 	unsigned long tmp;
3752         int ret;
3753 
3754 	ret = kstrtoul(page, 0, &tmp);
3755 	if (ret < 0) {
3756 		pr_err("Unable to extract srpt_tpg_store_enable\n");
3757 		return -EINVAL;
3758 	}
3759 
3760 	if ((tmp != 0) && (tmp != 1)) {
3761 		pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3762 		return -EINVAL;
3763 	}
3764 	if (tmp == 1)
3765 		sport->enabled = true;
3766 	else
3767 		sport->enabled = false;
3768 
3769 	return count;
3770 }
3771 
3772 TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
3773 
3774 static struct configfs_attribute *srpt_tpg_attrs[] = {
3775 	&srpt_tpg_enable.attr,
3776 	NULL,
3777 };
3778 
3779 /**
3780  * configfs callback invoked for
3781  * mkdir /sys/kernel/config/target/$driver/$port/$tpg
3782  */
srpt_make_tpg(struct se_wwn * wwn,struct config_group * group,const char * name)3783 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3784 					     struct config_group *group,
3785 					     const char *name)
3786 {
3787 	struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3788 	int res;
3789 
3790 	/* Initialize sport->port_wwn and sport->port_tpg_1 */
3791 	res = core_tpg_register(&srpt_template, &sport->port_wwn,
3792 			&sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
3793 	if (res)
3794 		return ERR_PTR(res);
3795 
3796 	return &sport->port_tpg_1;
3797 }
3798 
3799 /**
3800  * configfs callback invoked for
3801  * rmdir /sys/kernel/config/target/$driver/$port/$tpg
3802  */
srpt_drop_tpg(struct se_portal_group * tpg)3803 static void srpt_drop_tpg(struct se_portal_group *tpg)
3804 {
3805 	struct srpt_port *sport = container_of(tpg,
3806 				struct srpt_port, port_tpg_1);
3807 
3808 	sport->enabled = false;
3809 	core_tpg_deregister(&sport->port_tpg_1);
3810 }
3811 
3812 /**
3813  * configfs callback invoked for
3814  * mkdir /sys/kernel/config/target/$driver/$port
3815  */
srpt_make_tport(struct target_fabric_configfs * tf,struct config_group * group,const char * name)3816 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3817 				      struct config_group *group,
3818 				      const char *name)
3819 {
3820 	struct srpt_port *sport;
3821 	int ret;
3822 
3823 	sport = srpt_lookup_port(name);
3824 	pr_debug("make_tport(%s)\n", name);
3825 	ret = -EINVAL;
3826 	if (!sport)
3827 		goto err;
3828 
3829 	return &sport->port_wwn;
3830 
3831 err:
3832 	return ERR_PTR(ret);
3833 }
3834 
3835 /**
3836  * configfs callback invoked for
3837  * rmdir /sys/kernel/config/target/$driver/$port
3838  */
srpt_drop_tport(struct se_wwn * wwn)3839 static void srpt_drop_tport(struct se_wwn *wwn)
3840 {
3841 	struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
3842 
3843 	pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
3844 }
3845 
srpt_wwn_show_attr_version(struct target_fabric_configfs * tf,char * buf)3846 static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
3847 					      char *buf)
3848 {
3849 	return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
3850 }
3851 
3852 TF_WWN_ATTR_RO(srpt, version);
3853 
3854 static struct configfs_attribute *srpt_wwn_attrs[] = {
3855 	&srpt_wwn_version.attr,
3856 	NULL,
3857 };
3858 
3859 static const struct target_core_fabric_ops srpt_template = {
3860 	.module				= THIS_MODULE,
3861 	.name				= "srpt",
3862 	.get_fabric_name		= srpt_get_fabric_name,
3863 	.get_fabric_proto_ident		= srpt_get_fabric_proto_ident,
3864 	.tpg_get_wwn			= srpt_get_fabric_wwn,
3865 	.tpg_get_tag			= srpt_get_tag,
3866 	.tpg_get_default_depth		= srpt_get_default_depth,
3867 	.tpg_get_pr_transport_id	= srpt_get_pr_transport_id,
3868 	.tpg_get_pr_transport_id_len	= srpt_get_pr_transport_id_len,
3869 	.tpg_parse_pr_out_transport_id	= srpt_parse_pr_out_transport_id,
3870 	.tpg_check_demo_mode		= srpt_check_false,
3871 	.tpg_check_demo_mode_cache	= srpt_check_true,
3872 	.tpg_check_demo_mode_write_protect = srpt_check_true,
3873 	.tpg_check_prod_mode_write_protect = srpt_check_false,
3874 	.tpg_alloc_fabric_acl		= srpt_alloc_fabric_acl,
3875 	.tpg_release_fabric_acl		= srpt_release_fabric_acl,
3876 	.tpg_get_inst_index		= srpt_tpg_get_inst_index,
3877 	.release_cmd			= srpt_release_cmd,
3878 	.check_stop_free		= srpt_check_stop_free,
3879 	.shutdown_session		= srpt_shutdown_session,
3880 	.close_session			= srpt_close_session,
3881 	.sess_get_index			= srpt_sess_get_index,
3882 	.sess_get_initiator_sid		= NULL,
3883 	.write_pending			= srpt_write_pending,
3884 	.write_pending_status		= srpt_write_pending_status,
3885 	.set_default_node_attributes	= srpt_set_default_node_attrs,
3886 	.get_task_tag			= srpt_get_task_tag,
3887 	.get_cmd_state			= srpt_get_tcm_cmd_state,
3888 	.queue_data_in			= srpt_queue_data_in,
3889 	.queue_status			= srpt_queue_status,
3890 	.queue_tm_rsp			= srpt_queue_tm_rsp,
3891 	.aborted_task			= srpt_aborted_task,
3892 	/*
3893 	 * Setup function pointers for generic logic in
3894 	 * target_core_fabric_configfs.c
3895 	 */
3896 	.fabric_make_wwn		= srpt_make_tport,
3897 	.fabric_drop_wwn		= srpt_drop_tport,
3898 	.fabric_make_tpg		= srpt_make_tpg,
3899 	.fabric_drop_tpg		= srpt_drop_tpg,
3900 	.fabric_post_link		= NULL,
3901 	.fabric_pre_unlink		= NULL,
3902 	.fabric_make_np			= NULL,
3903 	.fabric_drop_np			= NULL,
3904 	.fabric_make_nodeacl		= srpt_make_nodeacl,
3905 	.fabric_drop_nodeacl		= srpt_drop_nodeacl,
3906 
3907 	.tfc_wwn_attrs			= srpt_wwn_attrs,
3908 	.tfc_tpg_base_attrs		= srpt_tpg_attrs,
3909 	.tfc_tpg_attrib_attrs		= srpt_tpg_attrib_attrs,
3910 };
3911 
3912 /**
3913  * srpt_init_module() - Kernel module initialization.
3914  *
3915  * Note: Since ib_register_client() registers callback functions, and since at
3916  * least one of these callback functions (srpt_add_one()) calls target core
3917  * functions, this driver must be registered with the target core before
3918  * ib_register_client() is called.
3919  */
srpt_init_module(void)3920 static int __init srpt_init_module(void)
3921 {
3922 	int ret;
3923 
3924 	ret = -EINVAL;
3925 	if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3926 		pr_err("invalid value %d for kernel module parameter"
3927 		       " srp_max_req_size -- must be at least %d.\n",
3928 		       srp_max_req_size, MIN_MAX_REQ_SIZE);
3929 		goto out;
3930 	}
3931 
3932 	if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3933 	    || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3934 		pr_err("invalid value %d for kernel module parameter"
3935 		       " srpt_srq_size -- must be in the range [%d..%d].\n",
3936 		       srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3937 		goto out;
3938 	}
3939 
3940 	ret = target_register_template(&srpt_template);
3941 	if (ret)
3942 		goto out;
3943 
3944 	ret = ib_register_client(&srpt_client);
3945 	if (ret) {
3946 		pr_err("couldn't register IB client\n");
3947 		goto out_unregister_target;
3948 	}
3949 
3950 	return 0;
3951 
3952 out_unregister_target:
3953 	target_unregister_template(&srpt_template);
3954 out:
3955 	return ret;
3956 }
3957 
srpt_cleanup_module(void)3958 static void __exit srpt_cleanup_module(void)
3959 {
3960 	ib_unregister_client(&srpt_client);
3961 	target_unregister_template(&srpt_template);
3962 }
3963 
3964 module_init(srpt_init_module);
3965 module_exit(srpt_cleanup_module);
3966