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 <linux/inet.h>
45 #include <rdma/ib_cache.h>
46 #include <scsi/scsi_proto.h>
47 #include <scsi/scsi_tcq.h>
48 #include <target/target_core_base.h>
49 #include <target/target_core_fabric.h>
50 #include "ib_srpt.h"
51 
52 /* Name of this kernel module. */
53 #define DRV_NAME		"ib_srpt"
54 
55 #define SRPT_ID_STRING	"Linux SRP target"
56 
57 #undef pr_fmt
58 #define pr_fmt(fmt) DRV_NAME " " fmt
59 
60 MODULE_AUTHOR("Vu Pham and Bart Van Assche");
61 MODULE_DESCRIPTION("SCSI RDMA Protocol target driver");
62 MODULE_LICENSE("Dual BSD/GPL");
63 
64 /*
65  * Global Variables
66  */
67 
68 static u64 srpt_service_guid;
69 static DEFINE_SPINLOCK(srpt_dev_lock);	/* Protects srpt_dev_list. */
70 static LIST_HEAD(srpt_dev_list);	/* List of srpt_device structures. */
71 
72 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
73 module_param(srp_max_req_size, int, 0444);
74 MODULE_PARM_DESC(srp_max_req_size,
75 		 "Maximum size of SRP request messages in bytes.");
76 
77 static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
78 module_param(srpt_srq_size, int, 0444);
79 MODULE_PARM_DESC(srpt_srq_size,
80 		 "Shared receive queue (SRQ) size.");
81 
82 static int srpt_get_u64_x(char *buffer, const struct kernel_param *kp)
83 {
84 	return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
85 }
86 module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
87 		  0444);
88 MODULE_PARM_DESC(srpt_service_guid,
89 		 "Using this value for ioc_guid, id_ext, and cm_listen_id instead of using the node_guid of the first HCA.");
90 
91 static struct ib_client srpt_client;
92 /* Protects both rdma_cm_port and rdma_cm_id. */
93 static DEFINE_MUTEX(rdma_cm_mutex);
94 /* Port number RDMA/CM will bind to. */
95 static u16 rdma_cm_port;
96 static struct rdma_cm_id *rdma_cm_id;
97 static void srpt_release_cmd(struct se_cmd *se_cmd);
98 static void srpt_free_ch(struct kref *kref);
99 static int srpt_queue_status(struct se_cmd *cmd);
100 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
101 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
102 static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
103 
104 /*
105  * The only allowed channel state changes are those that change the channel
106  * state into a state with a higher numerical value. Hence the new > prev test.
107  */
108 static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
109 {
110 	unsigned long flags;
111 	enum rdma_ch_state prev;
112 	bool changed = false;
113 
114 	spin_lock_irqsave(&ch->spinlock, flags);
115 	prev = ch->state;
116 	if (new > prev) {
117 		ch->state = new;
118 		changed = true;
119 	}
120 	spin_unlock_irqrestore(&ch->spinlock, flags);
121 
122 	return changed;
123 }
124 
125 /**
126  * srpt_event_handler - asynchronous IB event callback function
127  * @handler: IB event handler registered by ib_register_event_handler().
128  * @event: Description of the event that occurred.
129  *
130  * Callback function called by the InfiniBand core when an asynchronous IB
131  * event occurs. This callback may occur in interrupt context. See also
132  * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
133  * Architecture Specification.
134  */
135 static void srpt_event_handler(struct ib_event_handler *handler,
136 			       struct ib_event *event)
137 {
138 	struct srpt_device *sdev;
139 	struct srpt_port *sport;
140 	u8 port_num;
141 
142 	sdev = ib_get_client_data(event->device, &srpt_client);
143 	if (!sdev || sdev->device != event->device)
144 		return;
145 
146 	pr_debug("ASYNC event= %d on device= %s\n", event->event,
147 		 dev_name(&sdev->device->dev));
148 
149 	switch (event->event) {
150 	case IB_EVENT_PORT_ERR:
151 		port_num = event->element.port_num - 1;
152 		if (port_num < sdev->device->phys_port_cnt) {
153 			sport = &sdev->port[port_num];
154 			sport->lid = 0;
155 			sport->sm_lid = 0;
156 		} else {
157 			WARN(true, "event %d: port_num %d out of range 1..%d\n",
158 			     event->event, port_num + 1,
159 			     sdev->device->phys_port_cnt);
160 		}
161 		break;
162 	case IB_EVENT_PORT_ACTIVE:
163 	case IB_EVENT_LID_CHANGE:
164 	case IB_EVENT_PKEY_CHANGE:
165 	case IB_EVENT_SM_CHANGE:
166 	case IB_EVENT_CLIENT_REREGISTER:
167 	case IB_EVENT_GID_CHANGE:
168 		/* Refresh port data asynchronously. */
169 		port_num = event->element.port_num - 1;
170 		if (port_num < sdev->device->phys_port_cnt) {
171 			sport = &sdev->port[port_num];
172 			if (!sport->lid && !sport->sm_lid)
173 				schedule_work(&sport->work);
174 		} else {
175 			WARN(true, "event %d: port_num %d out of range 1..%d\n",
176 			     event->event, port_num + 1,
177 			     sdev->device->phys_port_cnt);
178 		}
179 		break;
180 	default:
181 		pr_err("received unrecognized IB event %d\n", event->event);
182 		break;
183 	}
184 }
185 
186 /**
187  * srpt_srq_event - SRQ event callback function
188  * @event: Description of the event that occurred.
189  * @ctx: Context pointer specified at SRQ creation time.
190  */
191 static void srpt_srq_event(struct ib_event *event, void *ctx)
192 {
193 	pr_debug("SRQ event %d\n", event->event);
194 }
195 
196 static const char *get_ch_state_name(enum rdma_ch_state s)
197 {
198 	switch (s) {
199 	case CH_CONNECTING:
200 		return "connecting";
201 	case CH_LIVE:
202 		return "live";
203 	case CH_DISCONNECTING:
204 		return "disconnecting";
205 	case CH_DRAINING:
206 		return "draining";
207 	case CH_DISCONNECTED:
208 		return "disconnected";
209 	}
210 	return "???";
211 }
212 
213 /**
214  * srpt_qp_event - QP event callback function
215  * @event: Description of the event that occurred.
216  * @ch: SRPT RDMA channel.
217  */
218 static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
219 {
220 	pr_debug("QP event %d on ch=%p sess_name=%s state=%d\n",
221 		 event->event, ch, ch->sess_name, ch->state);
222 
223 	switch (event->event) {
224 	case IB_EVENT_COMM_EST:
225 		if (ch->using_rdma_cm)
226 			rdma_notify(ch->rdma_cm.cm_id, event->event);
227 		else
228 			ib_cm_notify(ch->ib_cm.cm_id, event->event);
229 		break;
230 	case IB_EVENT_QP_LAST_WQE_REACHED:
231 		pr_debug("%s-%d, state %s: received Last WQE event.\n",
232 			 ch->sess_name, ch->qp->qp_num,
233 			 get_ch_state_name(ch->state));
234 		break;
235 	default:
236 		pr_err("received unrecognized IB QP event %d\n", event->event);
237 		break;
238 	}
239 }
240 
241 /**
242  * srpt_set_ioc - initialize a IOUnitInfo structure
243  * @c_list: controller list.
244  * @slot: one-based slot number.
245  * @value: four-bit value.
246  *
247  * Copies the lowest four bits of value in element slot of the array of four
248  * bit elements called c_list (controller list). The index slot is one-based.
249  */
250 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
251 {
252 	u16 id;
253 	u8 tmp;
254 
255 	id = (slot - 1) / 2;
256 	if (slot & 0x1) {
257 		tmp = c_list[id] & 0xf;
258 		c_list[id] = (value << 4) | tmp;
259 	} else {
260 		tmp = c_list[id] & 0xf0;
261 		c_list[id] = (value & 0xf) | tmp;
262 	}
263 }
264 
265 /**
266  * srpt_get_class_port_info - copy ClassPortInfo to a management datagram
267  * @mad: Datagram that will be sent as response to DM_ATTR_CLASS_PORT_INFO.
268  *
269  * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
270  * Specification.
271  */
272 static void srpt_get_class_port_info(struct ib_dm_mad *mad)
273 {
274 	struct ib_class_port_info *cif;
275 
276 	cif = (struct ib_class_port_info *)mad->data;
277 	memset(cif, 0, sizeof(*cif));
278 	cif->base_version = 1;
279 	cif->class_version = 1;
280 
281 	ib_set_cpi_resp_time(cif, 20);
282 	mad->mad_hdr.status = 0;
283 }
284 
285 /**
286  * srpt_get_iou - write IOUnitInfo to a management datagram
287  * @mad: Datagram that will be sent as response to DM_ATTR_IOU_INFO.
288  *
289  * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
290  * Specification. See also section B.7, table B.6 in the SRP r16a document.
291  */
292 static void srpt_get_iou(struct ib_dm_mad *mad)
293 {
294 	struct ib_dm_iou_info *ioui;
295 	u8 slot;
296 	int i;
297 
298 	ioui = (struct ib_dm_iou_info *)mad->data;
299 	ioui->change_id = cpu_to_be16(1);
300 	ioui->max_controllers = 16;
301 
302 	/* set present for slot 1 and empty for the rest */
303 	srpt_set_ioc(ioui->controller_list, 1, 1);
304 	for (i = 1, slot = 2; i < 16; i++, slot++)
305 		srpt_set_ioc(ioui->controller_list, slot, 0);
306 
307 	mad->mad_hdr.status = 0;
308 }
309 
310 /**
311  * srpt_get_ioc - write IOControllerprofile to a management datagram
312  * @sport: HCA port through which the MAD has been received.
313  * @slot: Slot number specified in DM_ATTR_IOC_PROFILE query.
314  * @mad: Datagram that will be sent as response to DM_ATTR_IOC_PROFILE.
315  *
316  * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
317  * Architecture Specification. See also section B.7, table B.7 in the SRP
318  * r16a document.
319  */
320 static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
321 			 struct ib_dm_mad *mad)
322 {
323 	struct srpt_device *sdev = sport->sdev;
324 	struct ib_dm_ioc_profile *iocp;
325 	int send_queue_depth;
326 
327 	iocp = (struct ib_dm_ioc_profile *)mad->data;
328 
329 	if (!slot || slot > 16) {
330 		mad->mad_hdr.status
331 			= cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
332 		return;
333 	}
334 
335 	if (slot > 2) {
336 		mad->mad_hdr.status
337 			= cpu_to_be16(DM_MAD_STATUS_NO_IOC);
338 		return;
339 	}
340 
341 	if (sdev->use_srq)
342 		send_queue_depth = sdev->srq_size;
343 	else
344 		send_queue_depth = min(MAX_SRPT_RQ_SIZE,
345 				       sdev->device->attrs.max_qp_wr);
346 
347 	memset(iocp, 0, sizeof(*iocp));
348 	strcpy(iocp->id_string, SRPT_ID_STRING);
349 	iocp->guid = cpu_to_be64(srpt_service_guid);
350 	iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
351 	iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
352 	iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
353 	iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
354 	iocp->subsys_device_id = 0x0;
355 	iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
356 	iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
357 	iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
358 	iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
359 	iocp->send_queue_depth = cpu_to_be16(send_queue_depth);
360 	iocp->rdma_read_depth = 4;
361 	iocp->send_size = cpu_to_be32(srp_max_req_size);
362 	iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
363 					  1U << 24));
364 	iocp->num_svc_entries = 1;
365 	iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
366 		SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
367 
368 	mad->mad_hdr.status = 0;
369 }
370 
371 /**
372  * srpt_get_svc_entries - write ServiceEntries to a management datagram
373  * @ioc_guid: I/O controller GUID to use in reply.
374  * @slot: I/O controller number.
375  * @hi: End of the range of service entries to be specified in the reply.
376  * @lo: Start of the range of service entries to be specified in the reply..
377  * @mad: Datagram that will be sent as response to DM_ATTR_SVC_ENTRIES.
378  *
379  * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
380  * Specification. See also section B.7, table B.8 in the SRP r16a document.
381  */
382 static void srpt_get_svc_entries(u64 ioc_guid,
383 				 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
384 {
385 	struct ib_dm_svc_entries *svc_entries;
386 
387 	WARN_ON(!ioc_guid);
388 
389 	if (!slot || slot > 16) {
390 		mad->mad_hdr.status
391 			= cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
392 		return;
393 	}
394 
395 	if (slot > 2 || lo > hi || hi > 1) {
396 		mad->mad_hdr.status
397 			= cpu_to_be16(DM_MAD_STATUS_NO_IOC);
398 		return;
399 	}
400 
401 	svc_entries = (struct ib_dm_svc_entries *)mad->data;
402 	memset(svc_entries, 0, sizeof(*svc_entries));
403 	svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
404 	snprintf(svc_entries->service_entries[0].name,
405 		 sizeof(svc_entries->service_entries[0].name),
406 		 "%s%016llx",
407 		 SRP_SERVICE_NAME_PREFIX,
408 		 ioc_guid);
409 
410 	mad->mad_hdr.status = 0;
411 }
412 
413 /**
414  * srpt_mgmt_method_get - process a received management datagram
415  * @sp:      HCA port through which the MAD has been received.
416  * @rq_mad:  received MAD.
417  * @rsp_mad: response MAD.
418  */
419 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
420 				 struct ib_dm_mad *rsp_mad)
421 {
422 	u16 attr_id;
423 	u32 slot;
424 	u8 hi, lo;
425 
426 	attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
427 	switch (attr_id) {
428 	case DM_ATTR_CLASS_PORT_INFO:
429 		srpt_get_class_port_info(rsp_mad);
430 		break;
431 	case DM_ATTR_IOU_INFO:
432 		srpt_get_iou(rsp_mad);
433 		break;
434 	case DM_ATTR_IOC_PROFILE:
435 		slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
436 		srpt_get_ioc(sp, slot, rsp_mad);
437 		break;
438 	case DM_ATTR_SVC_ENTRIES:
439 		slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
440 		hi = (u8) ((slot >> 8) & 0xff);
441 		lo = (u8) (slot & 0xff);
442 		slot = (u16) ((slot >> 16) & 0xffff);
443 		srpt_get_svc_entries(srpt_service_guid,
444 				     slot, hi, lo, rsp_mad);
445 		break;
446 	default:
447 		rsp_mad->mad_hdr.status =
448 		    cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
449 		break;
450 	}
451 }
452 
453 /**
454  * srpt_mad_send_handler - MAD send completion callback
455  * @mad_agent: Return value of ib_register_mad_agent().
456  * @mad_wc: Work completion reporting that the MAD has been sent.
457  */
458 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
459 				  struct ib_mad_send_wc *mad_wc)
460 {
461 	rdma_destroy_ah(mad_wc->send_buf->ah, RDMA_DESTROY_AH_SLEEPABLE);
462 	ib_free_send_mad(mad_wc->send_buf);
463 }
464 
465 /**
466  * srpt_mad_recv_handler - MAD reception callback function
467  * @mad_agent: Return value of ib_register_mad_agent().
468  * @send_buf: Not used.
469  * @mad_wc: Work completion reporting that a MAD has been received.
470  */
471 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
472 				  struct ib_mad_send_buf *send_buf,
473 				  struct ib_mad_recv_wc *mad_wc)
474 {
475 	struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
476 	struct ib_ah *ah;
477 	struct ib_mad_send_buf *rsp;
478 	struct ib_dm_mad *dm_mad;
479 
480 	if (!mad_wc || !mad_wc->recv_buf.mad)
481 		return;
482 
483 	ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
484 				  mad_wc->recv_buf.grh, mad_agent->port_num);
485 	if (IS_ERR(ah))
486 		goto err;
487 
488 	BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
489 
490 	rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
491 				 mad_wc->wc->pkey_index, 0,
492 				 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
493 				 GFP_KERNEL,
494 				 IB_MGMT_BASE_VERSION);
495 	if (IS_ERR(rsp))
496 		goto err_rsp;
497 
498 	rsp->ah = ah;
499 
500 	dm_mad = rsp->mad;
501 	memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
502 	dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
503 	dm_mad->mad_hdr.status = 0;
504 
505 	switch (mad_wc->recv_buf.mad->mad_hdr.method) {
506 	case IB_MGMT_METHOD_GET:
507 		srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
508 		break;
509 	case IB_MGMT_METHOD_SET:
510 		dm_mad->mad_hdr.status =
511 		    cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
512 		break;
513 	default:
514 		dm_mad->mad_hdr.status =
515 		    cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
516 		break;
517 	}
518 
519 	if (!ib_post_send_mad(rsp, NULL)) {
520 		ib_free_recv_mad(mad_wc);
521 		/* will destroy_ah & free_send_mad in send completion */
522 		return;
523 	}
524 
525 	ib_free_send_mad(rsp);
526 
527 err_rsp:
528 	rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE);
529 err:
530 	ib_free_recv_mad(mad_wc);
531 }
532 
533 static int srpt_format_guid(char *buf, unsigned int size, const __be64 *guid)
534 {
535 	const __be16 *g = (const __be16 *)guid;
536 
537 	return snprintf(buf, size, "%04x:%04x:%04x:%04x",
538 			be16_to_cpu(g[0]), be16_to_cpu(g[1]),
539 			be16_to_cpu(g[2]), be16_to_cpu(g[3]));
540 }
541 
542 /**
543  * srpt_refresh_port - configure a HCA port
544  * @sport: SRPT HCA port.
545  *
546  * Enable InfiniBand management datagram processing, update the cached sm_lid,
547  * lid and gid values, and register a callback function for processing MADs
548  * on the specified port.
549  *
550  * Note: It is safe to call this function more than once for the same port.
551  */
552 static int srpt_refresh_port(struct srpt_port *sport)
553 {
554 	struct ib_mad_reg_req reg_req;
555 	struct ib_port_modify port_modify;
556 	struct ib_port_attr port_attr;
557 	int ret;
558 
559 	ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
560 	if (ret)
561 		return ret;
562 
563 	sport->sm_lid = port_attr.sm_lid;
564 	sport->lid = port_attr.lid;
565 
566 	ret = rdma_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
567 	if (ret)
568 		return ret;
569 
570 	sport->port_guid_id.wwn.priv = sport;
571 	srpt_format_guid(sport->port_guid_id.name,
572 			 sizeof(sport->port_guid_id.name),
573 			 &sport->gid.global.interface_id);
574 	sport->port_gid_id.wwn.priv = sport;
575 	snprintf(sport->port_gid_id.name, sizeof(sport->port_gid_id.name),
576 		 "0x%016llx%016llx",
577 		 be64_to_cpu(sport->gid.global.subnet_prefix),
578 		 be64_to_cpu(sport->gid.global.interface_id));
579 
580 	if (rdma_protocol_iwarp(sport->sdev->device, sport->port))
581 		return 0;
582 
583 	memset(&port_modify, 0, sizeof(port_modify));
584 	port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
585 	port_modify.clr_port_cap_mask = 0;
586 
587 	ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
588 	if (ret) {
589 		pr_warn("%s-%d: enabling device management failed (%d). Note: this is expected if SR-IOV is enabled.\n",
590 			dev_name(&sport->sdev->device->dev), sport->port, ret);
591 		return 0;
592 	}
593 
594 	if (!sport->mad_agent) {
595 		memset(&reg_req, 0, sizeof(reg_req));
596 		reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
597 		reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
598 		set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
599 		set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
600 
601 		sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
602 							 sport->port,
603 							 IB_QPT_GSI,
604 							 &reg_req, 0,
605 							 srpt_mad_send_handler,
606 							 srpt_mad_recv_handler,
607 							 sport, 0);
608 		if (IS_ERR(sport->mad_agent)) {
609 			pr_err("%s-%d: MAD agent registration failed (%ld). Note: this is expected if SR-IOV is enabled.\n",
610 			       dev_name(&sport->sdev->device->dev), sport->port,
611 			       PTR_ERR(sport->mad_agent));
612 			sport->mad_agent = NULL;
613 		}
614 	}
615 
616 	return 0;
617 }
618 
619 /**
620  * srpt_unregister_mad_agent - unregister MAD callback functions
621  * @sdev: SRPT HCA pointer.
622  *
623  * Note: It is safe to call this function more than once for the same device.
624  */
625 static void srpt_unregister_mad_agent(struct srpt_device *sdev)
626 {
627 	struct ib_port_modify port_modify = {
628 		.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
629 	};
630 	struct srpt_port *sport;
631 	int i;
632 
633 	for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
634 		sport = &sdev->port[i - 1];
635 		WARN_ON(sport->port != i);
636 		if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
637 			pr_err("disabling MAD processing failed.\n");
638 		if (sport->mad_agent) {
639 			ib_unregister_mad_agent(sport->mad_agent);
640 			sport->mad_agent = NULL;
641 		}
642 	}
643 }
644 
645 /**
646  * srpt_alloc_ioctx - allocate a SRPT I/O context structure
647  * @sdev: SRPT HCA pointer.
648  * @ioctx_size: I/O context size.
649  * @buf_cache: I/O buffer cache.
650  * @dir: DMA data direction.
651  */
652 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
653 					   int ioctx_size,
654 					   struct kmem_cache *buf_cache,
655 					   enum dma_data_direction dir)
656 {
657 	struct srpt_ioctx *ioctx;
658 
659 	ioctx = kzalloc(ioctx_size, GFP_KERNEL);
660 	if (!ioctx)
661 		goto err;
662 
663 	ioctx->buf = kmem_cache_alloc(buf_cache, GFP_KERNEL);
664 	if (!ioctx->buf)
665 		goto err_free_ioctx;
666 
667 	ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf,
668 				       kmem_cache_size(buf_cache), dir);
669 	if (ib_dma_mapping_error(sdev->device, ioctx->dma))
670 		goto err_free_buf;
671 
672 	return ioctx;
673 
674 err_free_buf:
675 	kmem_cache_free(buf_cache, ioctx->buf);
676 err_free_ioctx:
677 	kfree(ioctx);
678 err:
679 	return NULL;
680 }
681 
682 /**
683  * srpt_free_ioctx - free a SRPT I/O context structure
684  * @sdev: SRPT HCA pointer.
685  * @ioctx: I/O context pointer.
686  * @buf_cache: I/O buffer cache.
687  * @dir: DMA data direction.
688  */
689 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
690 			    struct kmem_cache *buf_cache,
691 			    enum dma_data_direction dir)
692 {
693 	if (!ioctx)
694 		return;
695 
696 	ib_dma_unmap_single(sdev->device, ioctx->dma,
697 			    kmem_cache_size(buf_cache), dir);
698 	kmem_cache_free(buf_cache, ioctx->buf);
699 	kfree(ioctx);
700 }
701 
702 /**
703  * srpt_alloc_ioctx_ring - allocate a ring of SRPT I/O context structures
704  * @sdev:       Device to allocate the I/O context ring for.
705  * @ring_size:  Number of elements in the I/O context ring.
706  * @ioctx_size: I/O context size.
707  * @buf_cache:  I/O buffer cache.
708  * @alignment_offset: Offset in each ring buffer at which the SRP information
709  *		unit starts.
710  * @dir:        DMA data direction.
711  */
712 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
713 				int ring_size, int ioctx_size,
714 				struct kmem_cache *buf_cache,
715 				int alignment_offset,
716 				enum dma_data_direction dir)
717 {
718 	struct srpt_ioctx **ring;
719 	int i;
720 
721 	WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx) &&
722 		ioctx_size != sizeof(struct srpt_send_ioctx));
723 
724 	ring = kvmalloc_array(ring_size, sizeof(ring[0]), GFP_KERNEL);
725 	if (!ring)
726 		goto out;
727 	for (i = 0; i < ring_size; ++i) {
728 		ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, buf_cache, dir);
729 		if (!ring[i])
730 			goto err;
731 		ring[i]->index = i;
732 		ring[i]->offset = alignment_offset;
733 	}
734 	goto out;
735 
736 err:
737 	while (--i >= 0)
738 		srpt_free_ioctx(sdev, ring[i], buf_cache, dir);
739 	kvfree(ring);
740 	ring = NULL;
741 out:
742 	return ring;
743 }
744 
745 /**
746  * srpt_free_ioctx_ring - free the ring of SRPT I/O context structures
747  * @ioctx_ring: I/O context ring to be freed.
748  * @sdev: SRPT HCA pointer.
749  * @ring_size: Number of ring elements.
750  * @buf_cache: I/O buffer cache.
751  * @dir: DMA data direction.
752  */
753 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
754 				 struct srpt_device *sdev, int ring_size,
755 				 struct kmem_cache *buf_cache,
756 				 enum dma_data_direction dir)
757 {
758 	int i;
759 
760 	if (!ioctx_ring)
761 		return;
762 
763 	for (i = 0; i < ring_size; ++i)
764 		srpt_free_ioctx(sdev, ioctx_ring[i], buf_cache, dir);
765 	kvfree(ioctx_ring);
766 }
767 
768 /**
769  * srpt_set_cmd_state - set the state of a SCSI command
770  * @ioctx: Send I/O context.
771  * @new: New I/O context state.
772  *
773  * Does not modify the state of aborted commands. Returns the previous command
774  * state.
775  */
776 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
777 						  enum srpt_command_state new)
778 {
779 	enum srpt_command_state previous;
780 
781 	previous = ioctx->state;
782 	if (previous != SRPT_STATE_DONE)
783 		ioctx->state = new;
784 
785 	return previous;
786 }
787 
788 /**
789  * srpt_test_and_set_cmd_state - test and set the state of a command
790  * @ioctx: Send I/O context.
791  * @old: Current I/O context state.
792  * @new: New I/O context state.
793  *
794  * Returns true if and only if the previous command state was equal to 'old'.
795  */
796 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
797 					enum srpt_command_state old,
798 					enum srpt_command_state new)
799 {
800 	enum srpt_command_state previous;
801 
802 	WARN_ON(!ioctx);
803 	WARN_ON(old == SRPT_STATE_DONE);
804 	WARN_ON(new == SRPT_STATE_NEW);
805 
806 	previous = ioctx->state;
807 	if (previous == old)
808 		ioctx->state = new;
809 
810 	return previous == old;
811 }
812 
813 /**
814  * srpt_post_recv - post an IB receive request
815  * @sdev: SRPT HCA pointer.
816  * @ch: SRPT RDMA channel.
817  * @ioctx: Receive I/O context pointer.
818  */
819 static int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch,
820 			  struct srpt_recv_ioctx *ioctx)
821 {
822 	struct ib_sge list;
823 	struct ib_recv_wr wr;
824 
825 	BUG_ON(!sdev);
826 	list.addr = ioctx->ioctx.dma + ioctx->ioctx.offset;
827 	list.length = srp_max_req_size;
828 	list.lkey = sdev->lkey;
829 
830 	ioctx->ioctx.cqe.done = srpt_recv_done;
831 	wr.wr_cqe = &ioctx->ioctx.cqe;
832 	wr.next = NULL;
833 	wr.sg_list = &list;
834 	wr.num_sge = 1;
835 
836 	if (sdev->use_srq)
837 		return ib_post_srq_recv(sdev->srq, &wr, NULL);
838 	else
839 		return ib_post_recv(ch->qp, &wr, NULL);
840 }
841 
842 /**
843  * srpt_zerolength_write - perform a zero-length RDMA write
844  * @ch: SRPT RDMA channel.
845  *
846  * A quote from the InfiniBand specification: C9-88: For an HCA responder
847  * using Reliable Connection service, for each zero-length RDMA READ or WRITE
848  * request, the R_Key shall not be validated, even if the request includes
849  * Immediate data.
850  */
851 static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
852 {
853 	struct ib_rdma_wr wr = {
854 		.wr = {
855 			.next		= NULL,
856 			{ .wr_cqe	= &ch->zw_cqe, },
857 			.opcode		= IB_WR_RDMA_WRITE,
858 			.send_flags	= IB_SEND_SIGNALED,
859 		}
860 	};
861 
862 	pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
863 		 ch->qp->qp_num);
864 
865 	return ib_post_send(ch->qp, &wr.wr, NULL);
866 }
867 
868 static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
869 {
870 	struct srpt_rdma_ch *ch = cq->cq_context;
871 
872 	pr_debug("%s-%d wc->status %d\n", ch->sess_name, ch->qp->qp_num,
873 		 wc->status);
874 
875 	if (wc->status == IB_WC_SUCCESS) {
876 		srpt_process_wait_list(ch);
877 	} else {
878 		if (srpt_set_ch_state(ch, CH_DISCONNECTED))
879 			schedule_work(&ch->release_work);
880 		else
881 			pr_debug("%s-%d: already disconnected.\n",
882 				 ch->sess_name, ch->qp->qp_num);
883 	}
884 }
885 
886 static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
887 		struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
888 		unsigned *sg_cnt)
889 {
890 	enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
891 	struct srpt_rdma_ch *ch = ioctx->ch;
892 	struct scatterlist *prev = NULL;
893 	unsigned prev_nents;
894 	int ret, i;
895 
896 	if (nbufs == 1) {
897 		ioctx->rw_ctxs = &ioctx->s_rw_ctx;
898 	} else {
899 		ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
900 			GFP_KERNEL);
901 		if (!ioctx->rw_ctxs)
902 			return -ENOMEM;
903 	}
904 
905 	for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
906 		struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
907 		u64 remote_addr = be64_to_cpu(db->va);
908 		u32 size = be32_to_cpu(db->len);
909 		u32 rkey = be32_to_cpu(db->key);
910 
911 		ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
912 				i < nbufs - 1);
913 		if (ret)
914 			goto unwind;
915 
916 		ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
917 				ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
918 		if (ret < 0) {
919 			target_free_sgl(ctx->sg, ctx->nents);
920 			goto unwind;
921 		}
922 
923 		ioctx->n_rdma += ret;
924 		ioctx->n_rw_ctx++;
925 
926 		if (prev) {
927 			sg_unmark_end(&prev[prev_nents - 1]);
928 			sg_chain(prev, prev_nents + 1, ctx->sg);
929 		} else {
930 			*sg = ctx->sg;
931 		}
932 
933 		prev = ctx->sg;
934 		prev_nents = ctx->nents;
935 
936 		*sg_cnt += ctx->nents;
937 	}
938 
939 	return 0;
940 
941 unwind:
942 	while (--i >= 0) {
943 		struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
944 
945 		rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
946 				ctx->sg, ctx->nents, dir);
947 		target_free_sgl(ctx->sg, ctx->nents);
948 	}
949 	if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
950 		kfree(ioctx->rw_ctxs);
951 	return ret;
952 }
953 
954 static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
955 				    struct srpt_send_ioctx *ioctx)
956 {
957 	enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
958 	int i;
959 
960 	for (i = 0; i < ioctx->n_rw_ctx; i++) {
961 		struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
962 
963 		rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
964 				ctx->sg, ctx->nents, dir);
965 		target_free_sgl(ctx->sg, ctx->nents);
966 	}
967 
968 	if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
969 		kfree(ioctx->rw_ctxs);
970 }
971 
972 static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
973 {
974 	/*
975 	 * The pointer computations below will only be compiled correctly
976 	 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
977 	 * whether srp_cmd::add_data has been declared as a byte pointer.
978 	 */
979 	BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
980 		     !__same_type(srp_cmd->add_data[0], (u8)0));
981 
982 	/*
983 	 * According to the SRP spec, the lower two bits of the 'ADDITIONAL
984 	 * CDB LENGTH' field are reserved and the size in bytes of this field
985 	 * is four times the value specified in bits 3..7. Hence the "& ~3".
986 	 */
987 	return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
988 }
989 
990 /**
991  * srpt_get_desc_tbl - parse the data descriptors of a SRP_CMD request
992  * @recv_ioctx: I/O context associated with the received command @srp_cmd.
993  * @ioctx: I/O context that will be used for responding to the initiator.
994  * @srp_cmd: Pointer to the SRP_CMD request data.
995  * @dir: Pointer to the variable to which the transfer direction will be
996  *   written.
997  * @sg: [out] scatterlist for the parsed SRP_CMD.
998  * @sg_cnt: [out] length of @sg.
999  * @data_len: Pointer to the variable to which the total data length of all
1000  *   descriptors in the SRP_CMD request will be written.
1001  * @imm_data_offset: [in] Offset in SRP_CMD requests at which immediate data
1002  *   starts.
1003  *
1004  * This function initializes ioctx->nrbuf and ioctx->r_bufs.
1005  *
1006  * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
1007  * -ENOMEM when memory allocation fails and zero upon success.
1008  */
1009 static int srpt_get_desc_tbl(struct srpt_recv_ioctx *recv_ioctx,
1010 		struct srpt_send_ioctx *ioctx,
1011 		struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
1012 		struct scatterlist **sg, unsigned int *sg_cnt, u64 *data_len,
1013 		u16 imm_data_offset)
1014 {
1015 	BUG_ON(!dir);
1016 	BUG_ON(!data_len);
1017 
1018 	/*
1019 	 * The lower four bits of the buffer format field contain the DATA-IN
1020 	 * buffer descriptor format, and the highest four bits contain the
1021 	 * DATA-OUT buffer descriptor format.
1022 	 */
1023 	if (srp_cmd->buf_fmt & 0xf)
1024 		/* DATA-IN: transfer data from target to initiator (read). */
1025 		*dir = DMA_FROM_DEVICE;
1026 	else if (srp_cmd->buf_fmt >> 4)
1027 		/* DATA-OUT: transfer data from initiator to target (write). */
1028 		*dir = DMA_TO_DEVICE;
1029 	else
1030 		*dir = DMA_NONE;
1031 
1032 	/* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
1033 	ioctx->cmd.data_direction = *dir;
1034 
1035 	if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
1036 	    ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
1037 		struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
1038 
1039 		*data_len = be32_to_cpu(db->len);
1040 		return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
1041 	} else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
1042 		   ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
1043 		struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
1044 		int nbufs = be32_to_cpu(idb->table_desc.len) /
1045 				sizeof(struct srp_direct_buf);
1046 
1047 		if (nbufs >
1048 		    (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
1049 			pr_err("received unsupported SRP_CMD request type (%u out + %u in != %u / %zu)\n",
1050 			       srp_cmd->data_out_desc_cnt,
1051 			       srp_cmd->data_in_desc_cnt,
1052 			       be32_to_cpu(idb->table_desc.len),
1053 			       sizeof(struct srp_direct_buf));
1054 			return -EINVAL;
1055 		}
1056 
1057 		*data_len = be32_to_cpu(idb->len);
1058 		return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
1059 				sg, sg_cnt);
1060 	} else if ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_IMM) {
1061 		struct srp_imm_buf *imm_buf = srpt_get_desc_buf(srp_cmd);
1062 		void *data = (void *)srp_cmd + imm_data_offset;
1063 		uint32_t len = be32_to_cpu(imm_buf->len);
1064 		uint32_t req_size = imm_data_offset + len;
1065 
1066 		if (req_size > srp_max_req_size) {
1067 			pr_err("Immediate data (length %d + %d) exceeds request size %d\n",
1068 			       imm_data_offset, len, srp_max_req_size);
1069 			return -EINVAL;
1070 		}
1071 		if (recv_ioctx->byte_len < req_size) {
1072 			pr_err("Received too few data - %d < %d\n",
1073 			       recv_ioctx->byte_len, req_size);
1074 			return -EIO;
1075 		}
1076 		/*
1077 		 * The immediate data buffer descriptor must occur before the
1078 		 * immediate data itself.
1079 		 */
1080 		if ((void *)(imm_buf + 1) > (void *)data) {
1081 			pr_err("Received invalid write request\n");
1082 			return -EINVAL;
1083 		}
1084 		*data_len = len;
1085 		ioctx->recv_ioctx = recv_ioctx;
1086 		if ((uintptr_t)data & 511) {
1087 			pr_warn_once("Internal error - the receive buffers are not aligned properly.\n");
1088 			return -EINVAL;
1089 		}
1090 		sg_init_one(&ioctx->imm_sg, data, len);
1091 		*sg = &ioctx->imm_sg;
1092 		*sg_cnt = 1;
1093 		return 0;
1094 	} else {
1095 		*data_len = 0;
1096 		return 0;
1097 	}
1098 }
1099 
1100 /**
1101  * srpt_init_ch_qp - initialize queue pair attributes
1102  * @ch: SRPT RDMA channel.
1103  * @qp: Queue pair pointer.
1104  *
1105  * Initialized the attributes of queue pair 'qp' by allowing local write,
1106  * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
1107  */
1108 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1109 {
1110 	struct ib_qp_attr *attr;
1111 	int ret;
1112 
1113 	WARN_ON_ONCE(ch->using_rdma_cm);
1114 
1115 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1116 	if (!attr)
1117 		return -ENOMEM;
1118 
1119 	attr->qp_state = IB_QPS_INIT;
1120 	attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1121 	attr->port_num = ch->sport->port;
1122 
1123 	ret = ib_find_cached_pkey(ch->sport->sdev->device, ch->sport->port,
1124 				  ch->pkey, &attr->pkey_index);
1125 	if (ret < 0)
1126 		pr_err("Translating pkey %#x failed (%d) - using index 0\n",
1127 		       ch->pkey, ret);
1128 
1129 	ret = ib_modify_qp(qp, attr,
1130 			   IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
1131 			   IB_QP_PKEY_INDEX);
1132 
1133 	kfree(attr);
1134 	return ret;
1135 }
1136 
1137 /**
1138  * srpt_ch_qp_rtr - change the state of a channel to 'ready to receive' (RTR)
1139  * @ch: channel of the queue pair.
1140  * @qp: queue pair to change the state of.
1141  *
1142  * Returns zero upon success and a negative value upon failure.
1143  *
1144  * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1145  * If this structure ever becomes larger, it might be necessary to allocate
1146  * it dynamically instead of on the stack.
1147  */
1148 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1149 {
1150 	struct ib_qp_attr qp_attr;
1151 	int attr_mask;
1152 	int ret;
1153 
1154 	WARN_ON_ONCE(ch->using_rdma_cm);
1155 
1156 	qp_attr.qp_state = IB_QPS_RTR;
1157 	ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
1158 	if (ret)
1159 		goto out;
1160 
1161 	qp_attr.max_dest_rd_atomic = 4;
1162 
1163 	ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1164 
1165 out:
1166 	return ret;
1167 }
1168 
1169 /**
1170  * srpt_ch_qp_rts - change the state of a channel to 'ready to send' (RTS)
1171  * @ch: channel of the queue pair.
1172  * @qp: queue pair to change the state of.
1173  *
1174  * Returns zero upon success and a negative value upon failure.
1175  *
1176  * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
1177  * If this structure ever becomes larger, it might be necessary to allocate
1178  * it dynamically instead of on the stack.
1179  */
1180 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
1181 {
1182 	struct ib_qp_attr qp_attr;
1183 	int attr_mask;
1184 	int ret;
1185 
1186 	qp_attr.qp_state = IB_QPS_RTS;
1187 	ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
1188 	if (ret)
1189 		goto out;
1190 
1191 	qp_attr.max_rd_atomic = 4;
1192 
1193 	ret = ib_modify_qp(qp, &qp_attr, attr_mask);
1194 
1195 out:
1196 	return ret;
1197 }
1198 
1199 /**
1200  * srpt_ch_qp_err - set the channel queue pair state to 'error'
1201  * @ch: SRPT RDMA channel.
1202  */
1203 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
1204 {
1205 	struct ib_qp_attr qp_attr;
1206 
1207 	qp_attr.qp_state = IB_QPS_ERR;
1208 	return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
1209 }
1210 
1211 /**
1212  * srpt_get_send_ioctx - obtain an I/O context for sending to the initiator
1213  * @ch: SRPT RDMA channel.
1214  */
1215 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
1216 {
1217 	struct srpt_send_ioctx *ioctx;
1218 	int tag, cpu;
1219 
1220 	BUG_ON(!ch);
1221 
1222 	tag = sbitmap_queue_get(&ch->sess->sess_tag_pool, &cpu);
1223 	if (tag < 0)
1224 		return NULL;
1225 
1226 	ioctx = ch->ioctx_ring[tag];
1227 	BUG_ON(ioctx->ch != ch);
1228 	ioctx->state = SRPT_STATE_NEW;
1229 	WARN_ON_ONCE(ioctx->recv_ioctx);
1230 	ioctx->n_rdma = 0;
1231 	ioctx->n_rw_ctx = 0;
1232 	ioctx->queue_status_only = false;
1233 	/*
1234 	 * transport_init_se_cmd() does not initialize all fields, so do it
1235 	 * here.
1236 	 */
1237 	memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
1238 	memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
1239 	ioctx->cmd.map_tag = tag;
1240 	ioctx->cmd.map_cpu = cpu;
1241 
1242 	return ioctx;
1243 }
1244 
1245 /**
1246  * srpt_abort_cmd - abort a SCSI command
1247  * @ioctx:   I/O context associated with the SCSI command.
1248  */
1249 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
1250 {
1251 	enum srpt_command_state state;
1252 
1253 	BUG_ON(!ioctx);
1254 
1255 	/*
1256 	 * If the command is in a state where the target core is waiting for
1257 	 * the ib_srpt driver, change the state to the next state.
1258 	 */
1259 
1260 	state = ioctx->state;
1261 	switch (state) {
1262 	case SRPT_STATE_NEED_DATA:
1263 		ioctx->state = SRPT_STATE_DATA_IN;
1264 		break;
1265 	case SRPT_STATE_CMD_RSP_SENT:
1266 	case SRPT_STATE_MGMT_RSP_SENT:
1267 		ioctx->state = SRPT_STATE_DONE;
1268 		break;
1269 	default:
1270 		WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
1271 			  __func__, state);
1272 		break;
1273 	}
1274 
1275 	pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state,
1276 		 ioctx->state, ioctx->cmd.tag);
1277 
1278 	switch (state) {
1279 	case SRPT_STATE_NEW:
1280 	case SRPT_STATE_DATA_IN:
1281 	case SRPT_STATE_MGMT:
1282 	case SRPT_STATE_DONE:
1283 		/*
1284 		 * Do nothing - defer abort processing until
1285 		 * srpt_queue_response() is invoked.
1286 		 */
1287 		break;
1288 	case SRPT_STATE_NEED_DATA:
1289 		pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
1290 		transport_generic_request_failure(&ioctx->cmd,
1291 					TCM_CHECK_CONDITION_ABORT_CMD);
1292 		break;
1293 	case SRPT_STATE_CMD_RSP_SENT:
1294 		/*
1295 		 * SRP_RSP sending failed or the SRP_RSP send completion has
1296 		 * not been received in time.
1297 		 */
1298 		transport_generic_free_cmd(&ioctx->cmd, 0);
1299 		break;
1300 	case SRPT_STATE_MGMT_RSP_SENT:
1301 		transport_generic_free_cmd(&ioctx->cmd, 0);
1302 		break;
1303 	default:
1304 		WARN(1, "Unexpected command state (%d)", state);
1305 		break;
1306 	}
1307 
1308 	return state;
1309 }
1310 
1311 /**
1312  * srpt_rdma_read_done - RDMA read completion callback
1313  * @cq: Completion queue.
1314  * @wc: Work completion.
1315  *
1316  * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
1317  * the data that has been transferred via IB RDMA had to be postponed until the
1318  * check_stop_free() callback.  None of this is necessary anymore and needs to
1319  * be cleaned up.
1320  */
1321 static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
1322 {
1323 	struct srpt_rdma_ch *ch = cq->cq_context;
1324 	struct srpt_send_ioctx *ioctx =
1325 		container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
1326 
1327 	WARN_ON(ioctx->n_rdma <= 0);
1328 	atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
1329 	ioctx->n_rdma = 0;
1330 
1331 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
1332 		pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
1333 			ioctx, wc->status);
1334 		srpt_abort_cmd(ioctx);
1335 		return;
1336 	}
1337 
1338 	if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
1339 					SRPT_STATE_DATA_IN))
1340 		target_execute_cmd(&ioctx->cmd);
1341 	else
1342 		pr_err("%s[%d]: wrong state = %d\n", __func__,
1343 		       __LINE__, ioctx->state);
1344 }
1345 
1346 /**
1347  * srpt_build_cmd_rsp - build a SRP_RSP response
1348  * @ch: RDMA channel through which the request has been received.
1349  * @ioctx: I/O context associated with the SRP_CMD request. The response will
1350  *   be built in the buffer ioctx->buf points at and hence this function will
1351  *   overwrite the request data.
1352  * @tag: tag of the request for which this response is being generated.
1353  * @status: value for the STATUS field of the SRP_RSP information unit.
1354  *
1355  * Returns the size in bytes of the SRP_RSP response.
1356  *
1357  * An SRP_RSP response contains a SCSI status or service response. See also
1358  * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1359  * response. See also SPC-2 for more information about sense data.
1360  */
1361 static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
1362 			      struct srpt_send_ioctx *ioctx, u64 tag,
1363 			      int status)
1364 {
1365 	struct se_cmd *cmd = &ioctx->cmd;
1366 	struct srp_rsp *srp_rsp;
1367 	const u8 *sense_data;
1368 	int sense_data_len, max_sense_len;
1369 	u32 resid = cmd->residual_count;
1370 
1371 	/*
1372 	 * The lowest bit of all SAM-3 status codes is zero (see also
1373 	 * paragraph 5.3 in SAM-3).
1374 	 */
1375 	WARN_ON(status & 1);
1376 
1377 	srp_rsp = ioctx->ioctx.buf;
1378 	BUG_ON(!srp_rsp);
1379 
1380 	sense_data = ioctx->sense_data;
1381 	sense_data_len = ioctx->cmd.scsi_sense_length;
1382 	WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
1383 
1384 	memset(srp_rsp, 0, sizeof(*srp_rsp));
1385 	srp_rsp->opcode = SRP_RSP;
1386 	srp_rsp->req_lim_delta =
1387 		cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1388 	srp_rsp->tag = tag;
1389 	srp_rsp->status = status;
1390 
1391 	if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
1392 		if (cmd->data_direction == DMA_TO_DEVICE) {
1393 			/* residual data from an underflow write */
1394 			srp_rsp->flags = SRP_RSP_FLAG_DOUNDER;
1395 			srp_rsp->data_out_res_cnt = cpu_to_be32(resid);
1396 		} else if (cmd->data_direction == DMA_FROM_DEVICE) {
1397 			/* residual data from an underflow read */
1398 			srp_rsp->flags = SRP_RSP_FLAG_DIUNDER;
1399 			srp_rsp->data_in_res_cnt = cpu_to_be32(resid);
1400 		}
1401 	} else if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1402 		if (cmd->data_direction == DMA_TO_DEVICE) {
1403 			/* residual data from an overflow write */
1404 			srp_rsp->flags = SRP_RSP_FLAG_DOOVER;
1405 			srp_rsp->data_out_res_cnt = cpu_to_be32(resid);
1406 		} else if (cmd->data_direction == DMA_FROM_DEVICE) {
1407 			/* residual data from an overflow read */
1408 			srp_rsp->flags = SRP_RSP_FLAG_DIOVER;
1409 			srp_rsp->data_in_res_cnt = cpu_to_be32(resid);
1410 		}
1411 	}
1412 
1413 	if (sense_data_len) {
1414 		BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
1415 		max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
1416 		if (sense_data_len > max_sense_len) {
1417 			pr_warn("truncated sense data from %d to %d bytes\n",
1418 				sense_data_len, max_sense_len);
1419 			sense_data_len = max_sense_len;
1420 		}
1421 
1422 		srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
1423 		srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
1424 		memcpy(srp_rsp + 1, sense_data, sense_data_len);
1425 	}
1426 
1427 	return sizeof(*srp_rsp) + sense_data_len;
1428 }
1429 
1430 /**
1431  * srpt_build_tskmgmt_rsp - build a task management response
1432  * @ch:       RDMA channel through which the request has been received.
1433  * @ioctx:    I/O context in which the SRP_RSP response will be built.
1434  * @rsp_code: RSP_CODE that will be stored in the response.
1435  * @tag:      Tag of the request for which this response is being generated.
1436  *
1437  * Returns the size in bytes of the SRP_RSP response.
1438  *
1439  * An SRP_RSP response contains a SCSI status or service response. See also
1440  * section 6.9 in the SRP r16a document for the format of an SRP_RSP
1441  * response.
1442  */
1443 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
1444 				  struct srpt_send_ioctx *ioctx,
1445 				  u8 rsp_code, u64 tag)
1446 {
1447 	struct srp_rsp *srp_rsp;
1448 	int resp_data_len;
1449 	int resp_len;
1450 
1451 	resp_data_len = 4;
1452 	resp_len = sizeof(*srp_rsp) + resp_data_len;
1453 
1454 	srp_rsp = ioctx->ioctx.buf;
1455 	BUG_ON(!srp_rsp);
1456 	memset(srp_rsp, 0, sizeof(*srp_rsp));
1457 
1458 	srp_rsp->opcode = SRP_RSP;
1459 	srp_rsp->req_lim_delta =
1460 		cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
1461 	srp_rsp->tag = tag;
1462 
1463 	srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
1464 	srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
1465 	srp_rsp->data[3] = rsp_code;
1466 
1467 	return resp_len;
1468 }
1469 
1470 static int srpt_check_stop_free(struct se_cmd *cmd)
1471 {
1472 	struct srpt_send_ioctx *ioctx = container_of(cmd,
1473 				struct srpt_send_ioctx, cmd);
1474 
1475 	return target_put_sess_cmd(&ioctx->cmd);
1476 }
1477 
1478 /**
1479  * srpt_handle_cmd - process a SRP_CMD information unit
1480  * @ch: SRPT RDMA channel.
1481  * @recv_ioctx: Receive I/O context.
1482  * @send_ioctx: Send I/O context.
1483  */
1484 static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
1485 			    struct srpt_recv_ioctx *recv_ioctx,
1486 			    struct srpt_send_ioctx *send_ioctx)
1487 {
1488 	struct se_cmd *cmd;
1489 	struct srp_cmd *srp_cmd;
1490 	struct scatterlist *sg = NULL;
1491 	unsigned sg_cnt = 0;
1492 	u64 data_len;
1493 	enum dma_data_direction dir;
1494 	int rc;
1495 
1496 	BUG_ON(!send_ioctx);
1497 
1498 	srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset;
1499 	cmd = &send_ioctx->cmd;
1500 	cmd->tag = srp_cmd->tag;
1501 
1502 	switch (srp_cmd->task_attr) {
1503 	case SRP_CMD_SIMPLE_Q:
1504 		cmd->sam_task_attr = TCM_SIMPLE_TAG;
1505 		break;
1506 	case SRP_CMD_ORDERED_Q:
1507 	default:
1508 		cmd->sam_task_attr = TCM_ORDERED_TAG;
1509 		break;
1510 	case SRP_CMD_HEAD_OF_Q:
1511 		cmd->sam_task_attr = TCM_HEAD_TAG;
1512 		break;
1513 	case SRP_CMD_ACA:
1514 		cmd->sam_task_attr = TCM_ACA_TAG;
1515 		break;
1516 	}
1517 
1518 	rc = srpt_get_desc_tbl(recv_ioctx, send_ioctx, srp_cmd, &dir,
1519 			       &sg, &sg_cnt, &data_len, ch->imm_data_offset);
1520 	if (rc) {
1521 		if (rc != -EAGAIN) {
1522 			pr_err("0x%llx: parsing SRP descriptor table failed.\n",
1523 			       srp_cmd->tag);
1524 		}
1525 		goto busy;
1526 	}
1527 
1528 	rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
1529 			       &send_ioctx->sense_data[0],
1530 			       scsilun_to_int(&srp_cmd->lun), data_len,
1531 			       TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
1532 			       sg, sg_cnt, NULL, 0, NULL, 0);
1533 	if (rc != 0) {
1534 		pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
1535 			 srp_cmd->tag);
1536 		goto busy;
1537 	}
1538 	return;
1539 
1540 busy:
1541 	target_send_busy(cmd);
1542 }
1543 
1544 static int srp_tmr_to_tcm(int fn)
1545 {
1546 	switch (fn) {
1547 	case SRP_TSK_ABORT_TASK:
1548 		return TMR_ABORT_TASK;
1549 	case SRP_TSK_ABORT_TASK_SET:
1550 		return TMR_ABORT_TASK_SET;
1551 	case SRP_TSK_CLEAR_TASK_SET:
1552 		return TMR_CLEAR_TASK_SET;
1553 	case SRP_TSK_LUN_RESET:
1554 		return TMR_LUN_RESET;
1555 	case SRP_TSK_CLEAR_ACA:
1556 		return TMR_CLEAR_ACA;
1557 	default:
1558 		return -1;
1559 	}
1560 }
1561 
1562 /**
1563  * srpt_handle_tsk_mgmt - process a SRP_TSK_MGMT information unit
1564  * @ch: SRPT RDMA channel.
1565  * @recv_ioctx: Receive I/O context.
1566  * @send_ioctx: Send I/O context.
1567  *
1568  * Returns 0 if and only if the request will be processed by the target core.
1569  *
1570  * For more information about SRP_TSK_MGMT information units, see also section
1571  * 6.7 in the SRP r16a document.
1572  */
1573 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
1574 				 struct srpt_recv_ioctx *recv_ioctx,
1575 				 struct srpt_send_ioctx *send_ioctx)
1576 {
1577 	struct srp_tsk_mgmt *srp_tsk;
1578 	struct se_cmd *cmd;
1579 	struct se_session *sess = ch->sess;
1580 	int tcm_tmr;
1581 	int rc;
1582 
1583 	BUG_ON(!send_ioctx);
1584 
1585 	srp_tsk = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset;
1586 	cmd = &send_ioctx->cmd;
1587 
1588 	pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld ch %p sess %p\n",
1589 		 srp_tsk->tsk_mgmt_func, srp_tsk->task_tag, srp_tsk->tag, ch,
1590 		 ch->sess);
1591 
1592 	srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
1593 	send_ioctx->cmd.tag = srp_tsk->tag;
1594 	tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
1595 	rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
1596 			       scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
1597 			       GFP_KERNEL, srp_tsk->task_tag,
1598 			       TARGET_SCF_ACK_KREF);
1599 	if (rc != 0) {
1600 		send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
1601 		cmd->se_tfo->queue_tm_rsp(cmd);
1602 	}
1603 	return;
1604 }
1605 
1606 /**
1607  * srpt_handle_new_iu - process a newly received information unit
1608  * @ch:    RDMA channel through which the information unit has been received.
1609  * @recv_ioctx: Receive I/O context associated with the information unit.
1610  */
1611 static bool
1612 srpt_handle_new_iu(struct srpt_rdma_ch *ch, struct srpt_recv_ioctx *recv_ioctx)
1613 {
1614 	struct srpt_send_ioctx *send_ioctx = NULL;
1615 	struct srp_cmd *srp_cmd;
1616 	bool res = false;
1617 	u8 opcode;
1618 
1619 	BUG_ON(!ch);
1620 	BUG_ON(!recv_ioctx);
1621 
1622 	if (unlikely(ch->state == CH_CONNECTING))
1623 		goto push;
1624 
1625 	ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
1626 				   recv_ioctx->ioctx.dma,
1627 				   recv_ioctx->ioctx.offset + srp_max_req_size,
1628 				   DMA_FROM_DEVICE);
1629 
1630 	srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset;
1631 	opcode = srp_cmd->opcode;
1632 	if (opcode == SRP_CMD || opcode == SRP_TSK_MGMT) {
1633 		send_ioctx = srpt_get_send_ioctx(ch);
1634 		if (unlikely(!send_ioctx))
1635 			goto push;
1636 	}
1637 
1638 	if (!list_empty(&recv_ioctx->wait_list)) {
1639 		WARN_ON_ONCE(!ch->processing_wait_list);
1640 		list_del_init(&recv_ioctx->wait_list);
1641 	}
1642 
1643 	switch (opcode) {
1644 	case SRP_CMD:
1645 		srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
1646 		break;
1647 	case SRP_TSK_MGMT:
1648 		srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
1649 		break;
1650 	case SRP_I_LOGOUT:
1651 		pr_err("Not yet implemented: SRP_I_LOGOUT\n");
1652 		break;
1653 	case SRP_CRED_RSP:
1654 		pr_debug("received SRP_CRED_RSP\n");
1655 		break;
1656 	case SRP_AER_RSP:
1657 		pr_debug("received SRP_AER_RSP\n");
1658 		break;
1659 	case SRP_RSP:
1660 		pr_err("Received SRP_RSP\n");
1661 		break;
1662 	default:
1663 		pr_err("received IU with unknown opcode 0x%x\n", opcode);
1664 		break;
1665 	}
1666 
1667 	if (!send_ioctx || !send_ioctx->recv_ioctx)
1668 		srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
1669 	res = true;
1670 
1671 out:
1672 	return res;
1673 
1674 push:
1675 	if (list_empty(&recv_ioctx->wait_list)) {
1676 		WARN_ON_ONCE(ch->processing_wait_list);
1677 		list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
1678 	}
1679 	goto out;
1680 }
1681 
1682 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1683 {
1684 	struct srpt_rdma_ch *ch = cq->cq_context;
1685 	struct srpt_recv_ioctx *ioctx =
1686 		container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
1687 
1688 	if (wc->status == IB_WC_SUCCESS) {
1689 		int req_lim;
1690 
1691 		req_lim = atomic_dec_return(&ch->req_lim);
1692 		if (unlikely(req_lim < 0))
1693 			pr_err("req_lim = %d < 0\n", req_lim);
1694 		ioctx->byte_len = wc->byte_len;
1695 		srpt_handle_new_iu(ch, ioctx);
1696 	} else {
1697 		pr_info_ratelimited("receiving failed for ioctx %p with status %d\n",
1698 				    ioctx, wc->status);
1699 	}
1700 }
1701 
1702 /*
1703  * This function must be called from the context in which RDMA completions are
1704  * processed because it accesses the wait list without protection against
1705  * access from other threads.
1706  */
1707 static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
1708 {
1709 	struct srpt_recv_ioctx *recv_ioctx, *tmp;
1710 
1711 	WARN_ON_ONCE(ch->state == CH_CONNECTING);
1712 
1713 	if (list_empty(&ch->cmd_wait_list))
1714 		return;
1715 
1716 	WARN_ON_ONCE(ch->processing_wait_list);
1717 	ch->processing_wait_list = true;
1718 	list_for_each_entry_safe(recv_ioctx, tmp, &ch->cmd_wait_list,
1719 				 wait_list) {
1720 		if (!srpt_handle_new_iu(ch, recv_ioctx))
1721 			break;
1722 	}
1723 	ch->processing_wait_list = false;
1724 }
1725 
1726 /**
1727  * srpt_send_done - send completion callback
1728  * @cq: Completion queue.
1729  * @wc: Work completion.
1730  *
1731  * Note: Although this has not yet been observed during tests, at least in
1732  * theory it is possible that the srpt_get_send_ioctx() call invoked by
1733  * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
1734  * value in each response is set to one, and it is possible that this response
1735  * makes the initiator send a new request before the send completion for that
1736  * response has been processed. This could e.g. happen if the call to
1737  * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
1738  * if IB retransmission causes generation of the send completion to be
1739  * delayed. Incoming information units for which srpt_get_send_ioctx() fails
1740  * are queued on cmd_wait_list. The code below processes these delayed
1741  * requests one at a time.
1742  */
1743 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
1744 {
1745 	struct srpt_rdma_ch *ch = cq->cq_context;
1746 	struct srpt_send_ioctx *ioctx =
1747 		container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
1748 	enum srpt_command_state state;
1749 
1750 	state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
1751 
1752 	WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
1753 		state != SRPT_STATE_MGMT_RSP_SENT);
1754 
1755 	atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
1756 
1757 	if (wc->status != IB_WC_SUCCESS)
1758 		pr_info("sending response for ioctx 0x%p failed with status %d\n",
1759 			ioctx, wc->status);
1760 
1761 	if (state != SRPT_STATE_DONE) {
1762 		transport_generic_free_cmd(&ioctx->cmd, 0);
1763 	} else {
1764 		pr_err("IB completion has been received too late for wr_id = %u.\n",
1765 		       ioctx->ioctx.index);
1766 	}
1767 
1768 	srpt_process_wait_list(ch);
1769 }
1770 
1771 /**
1772  * srpt_create_ch_ib - create receive and send completion queues
1773  * @ch: SRPT RDMA channel.
1774  */
1775 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
1776 {
1777 	struct ib_qp_init_attr *qp_init;
1778 	struct srpt_port *sport = ch->sport;
1779 	struct srpt_device *sdev = sport->sdev;
1780 	const struct ib_device_attr *attrs = &sdev->device->attrs;
1781 	int sq_size = sport->port_attrib.srp_sq_size;
1782 	int i, ret;
1783 
1784 	WARN_ON(ch->rq_size < 1);
1785 
1786 	ret = -ENOMEM;
1787 	qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
1788 	if (!qp_init)
1789 		goto out;
1790 
1791 retry:
1792 	ch->cq = ib_alloc_cq_any(sdev->device, ch, ch->rq_size + sq_size,
1793 				 IB_POLL_WORKQUEUE);
1794 	if (IS_ERR(ch->cq)) {
1795 		ret = PTR_ERR(ch->cq);
1796 		pr_err("failed to create CQ cqe= %d ret= %d\n",
1797 		       ch->rq_size + sq_size, ret);
1798 		goto out;
1799 	}
1800 
1801 	qp_init->qp_context = (void *)ch;
1802 	qp_init->event_handler
1803 		= (void(*)(struct ib_event *, void*))srpt_qp_event;
1804 	qp_init->send_cq = ch->cq;
1805 	qp_init->recv_cq = ch->cq;
1806 	qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
1807 	qp_init->qp_type = IB_QPT_RC;
1808 	/*
1809 	 * We divide up our send queue size into half SEND WRs to send the
1810 	 * completions, and half R/W contexts to actually do the RDMA
1811 	 * READ/WRITE transfers.  Note that we need to allocate CQ slots for
1812 	 * both both, as RDMA contexts will also post completions for the
1813 	 * RDMA READ case.
1814 	 */
1815 	qp_init->cap.max_send_wr = min(sq_size / 2, attrs->max_qp_wr);
1816 	qp_init->cap.max_rdma_ctxs = sq_size / 2;
1817 	qp_init->cap.max_send_sge = min(attrs->max_send_sge,
1818 					SRPT_MAX_SG_PER_WQE);
1819 	qp_init->cap.max_recv_sge = min(attrs->max_recv_sge,
1820 					SRPT_MAX_SG_PER_WQE);
1821 	qp_init->port_num = ch->sport->port;
1822 	if (sdev->use_srq) {
1823 		qp_init->srq = sdev->srq;
1824 	} else {
1825 		qp_init->cap.max_recv_wr = ch->rq_size;
1826 		qp_init->cap.max_recv_sge = min(attrs->max_recv_sge,
1827 						SRPT_MAX_SG_PER_WQE);
1828 	}
1829 
1830 	if (ch->using_rdma_cm) {
1831 		ret = rdma_create_qp(ch->rdma_cm.cm_id, sdev->pd, qp_init);
1832 		ch->qp = ch->rdma_cm.cm_id->qp;
1833 	} else {
1834 		ch->qp = ib_create_qp(sdev->pd, qp_init);
1835 		if (!IS_ERR(ch->qp)) {
1836 			ret = srpt_init_ch_qp(ch, ch->qp);
1837 			if (ret)
1838 				ib_destroy_qp(ch->qp);
1839 		} else {
1840 			ret = PTR_ERR(ch->qp);
1841 		}
1842 	}
1843 	if (ret) {
1844 		bool retry = sq_size > MIN_SRPT_SQ_SIZE;
1845 
1846 		if (retry) {
1847 			pr_debug("failed to create queue pair with sq_size = %d (%d) - retrying\n",
1848 				 sq_size, ret);
1849 			ib_free_cq(ch->cq);
1850 			sq_size = max(sq_size / 2, MIN_SRPT_SQ_SIZE);
1851 			goto retry;
1852 		} else {
1853 			pr_err("failed to create queue pair with sq_size = %d (%d)\n",
1854 			       sq_size, ret);
1855 			goto err_destroy_cq;
1856 		}
1857 	}
1858 
1859 	atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
1860 
1861 	pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d ch= %p\n",
1862 		 __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
1863 		 qp_init->cap.max_send_wr, ch);
1864 
1865 	if (!sdev->use_srq)
1866 		for (i = 0; i < ch->rq_size; i++)
1867 			srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]);
1868 
1869 out:
1870 	kfree(qp_init);
1871 	return ret;
1872 
1873 err_destroy_cq:
1874 	ch->qp = NULL;
1875 	ib_free_cq(ch->cq);
1876 	goto out;
1877 }
1878 
1879 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
1880 {
1881 	ib_destroy_qp(ch->qp);
1882 	ib_free_cq(ch->cq);
1883 }
1884 
1885 /**
1886  * srpt_close_ch - close a RDMA channel
1887  * @ch: SRPT RDMA channel.
1888  *
1889  * Make sure all resources associated with the channel will be deallocated at
1890  * an appropriate time.
1891  *
1892  * Returns true if and only if the channel state has been modified into
1893  * CH_DRAINING.
1894  */
1895 static bool srpt_close_ch(struct srpt_rdma_ch *ch)
1896 {
1897 	int ret;
1898 
1899 	if (!srpt_set_ch_state(ch, CH_DRAINING)) {
1900 		pr_debug("%s: already closed\n", ch->sess_name);
1901 		return false;
1902 	}
1903 
1904 	kref_get(&ch->kref);
1905 
1906 	ret = srpt_ch_qp_err(ch);
1907 	if (ret < 0)
1908 		pr_err("%s-%d: changing queue pair into error state failed: %d\n",
1909 		       ch->sess_name, ch->qp->qp_num, ret);
1910 
1911 	ret = srpt_zerolength_write(ch);
1912 	if (ret < 0) {
1913 		pr_err("%s-%d: queuing zero-length write failed: %d\n",
1914 		       ch->sess_name, ch->qp->qp_num, ret);
1915 		if (srpt_set_ch_state(ch, CH_DISCONNECTED))
1916 			schedule_work(&ch->release_work);
1917 		else
1918 			WARN_ON_ONCE(true);
1919 	}
1920 
1921 	kref_put(&ch->kref, srpt_free_ch);
1922 
1923 	return true;
1924 }
1925 
1926 /*
1927  * Change the channel state into CH_DISCONNECTING. If a channel has not yet
1928  * reached the connected state, close it. If a channel is in the connected
1929  * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
1930  * the responsibility of the caller to ensure that this function is not
1931  * invoked concurrently with the code that accepts a connection. This means
1932  * that this function must either be invoked from inside a CM callback
1933  * function or that it must be invoked with the srpt_port.mutex held.
1934  */
1935 static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
1936 {
1937 	int ret;
1938 
1939 	if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
1940 		return -ENOTCONN;
1941 
1942 	if (ch->using_rdma_cm) {
1943 		ret = rdma_disconnect(ch->rdma_cm.cm_id);
1944 	} else {
1945 		ret = ib_send_cm_dreq(ch->ib_cm.cm_id, NULL, 0);
1946 		if (ret < 0)
1947 			ret = ib_send_cm_drep(ch->ib_cm.cm_id, NULL, 0);
1948 	}
1949 
1950 	if (ret < 0 && srpt_close_ch(ch))
1951 		ret = 0;
1952 
1953 	return ret;
1954 }
1955 
1956 /* Send DREQ and wait for DREP. */
1957 static void srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch)
1958 {
1959 	DECLARE_COMPLETION_ONSTACK(closed);
1960 	struct srpt_port *sport = ch->sport;
1961 
1962 	pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
1963 		 ch->state);
1964 
1965 	ch->closed = &closed;
1966 
1967 	mutex_lock(&sport->mutex);
1968 	srpt_disconnect_ch(ch);
1969 	mutex_unlock(&sport->mutex);
1970 
1971 	while (wait_for_completion_timeout(&closed, 5 * HZ) == 0)
1972 		pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
1973 			ch->sess_name, ch->qp->qp_num, ch->state);
1974 
1975 }
1976 
1977 static void __srpt_close_all_ch(struct srpt_port *sport)
1978 {
1979 	struct srpt_nexus *nexus;
1980 	struct srpt_rdma_ch *ch;
1981 
1982 	lockdep_assert_held(&sport->mutex);
1983 
1984 	list_for_each_entry(nexus, &sport->nexus_list, entry) {
1985 		list_for_each_entry(ch, &nexus->ch_list, list) {
1986 			if (srpt_disconnect_ch(ch) >= 0)
1987 				pr_info("Closing channel %s because target %s_%d has been disabled\n",
1988 					ch->sess_name,
1989 					dev_name(&sport->sdev->device->dev),
1990 					sport->port);
1991 			srpt_close_ch(ch);
1992 		}
1993 	}
1994 }
1995 
1996 /*
1997  * Look up (i_port_id, t_port_id) in sport->nexus_list. Create an entry if
1998  * it does not yet exist.
1999  */
2000 static struct srpt_nexus *srpt_get_nexus(struct srpt_port *sport,
2001 					 const u8 i_port_id[16],
2002 					 const u8 t_port_id[16])
2003 {
2004 	struct srpt_nexus *nexus = NULL, *tmp_nexus = NULL, *n;
2005 
2006 	for (;;) {
2007 		mutex_lock(&sport->mutex);
2008 		list_for_each_entry(n, &sport->nexus_list, entry) {
2009 			if (memcmp(n->i_port_id, i_port_id, 16) == 0 &&
2010 			    memcmp(n->t_port_id, t_port_id, 16) == 0) {
2011 				nexus = n;
2012 				break;
2013 			}
2014 		}
2015 		if (!nexus && tmp_nexus) {
2016 			list_add_tail_rcu(&tmp_nexus->entry,
2017 					  &sport->nexus_list);
2018 			swap(nexus, tmp_nexus);
2019 		}
2020 		mutex_unlock(&sport->mutex);
2021 
2022 		if (nexus)
2023 			break;
2024 		tmp_nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
2025 		if (!tmp_nexus) {
2026 			nexus = ERR_PTR(-ENOMEM);
2027 			break;
2028 		}
2029 		INIT_LIST_HEAD(&tmp_nexus->ch_list);
2030 		memcpy(tmp_nexus->i_port_id, i_port_id, 16);
2031 		memcpy(tmp_nexus->t_port_id, t_port_id, 16);
2032 	}
2033 
2034 	kfree(tmp_nexus);
2035 
2036 	return nexus;
2037 }
2038 
2039 static void srpt_set_enabled(struct srpt_port *sport, bool enabled)
2040 	__must_hold(&sport->mutex)
2041 {
2042 	lockdep_assert_held(&sport->mutex);
2043 
2044 	if (sport->enabled == enabled)
2045 		return;
2046 	sport->enabled = enabled;
2047 	if (!enabled)
2048 		__srpt_close_all_ch(sport);
2049 }
2050 
2051 static void srpt_drop_sport_ref(struct srpt_port *sport)
2052 {
2053 	if (atomic_dec_return(&sport->refcount) == 0 && sport->freed_channels)
2054 		complete(sport->freed_channels);
2055 }
2056 
2057 static void srpt_free_ch(struct kref *kref)
2058 {
2059 	struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
2060 
2061 	srpt_drop_sport_ref(ch->sport);
2062 	kfree_rcu(ch, rcu);
2063 }
2064 
2065 /*
2066  * Shut down the SCSI target session, tell the connection manager to
2067  * disconnect the associated RDMA channel, transition the QP to the error
2068  * state and remove the channel from the channel list. This function is
2069  * typically called from inside srpt_zerolength_write_done(). Concurrent
2070  * srpt_zerolength_write() calls from inside srpt_close_ch() are possible
2071  * as long as the channel is on sport->nexus_list.
2072  */
2073 static void srpt_release_channel_work(struct work_struct *w)
2074 {
2075 	struct srpt_rdma_ch *ch;
2076 	struct srpt_device *sdev;
2077 	struct srpt_port *sport;
2078 	struct se_session *se_sess;
2079 
2080 	ch = container_of(w, struct srpt_rdma_ch, release_work);
2081 	pr_debug("%s-%d\n", ch->sess_name, ch->qp->qp_num);
2082 
2083 	sdev = ch->sport->sdev;
2084 	BUG_ON(!sdev);
2085 
2086 	se_sess = ch->sess;
2087 	BUG_ON(!se_sess);
2088 
2089 	target_sess_cmd_list_set_waiting(se_sess);
2090 	target_wait_for_sess_cmds(se_sess);
2091 
2092 	target_remove_session(se_sess);
2093 	ch->sess = NULL;
2094 
2095 	if (ch->using_rdma_cm)
2096 		rdma_destroy_id(ch->rdma_cm.cm_id);
2097 	else
2098 		ib_destroy_cm_id(ch->ib_cm.cm_id);
2099 
2100 	sport = ch->sport;
2101 	mutex_lock(&sport->mutex);
2102 	list_del_rcu(&ch->list);
2103 	mutex_unlock(&sport->mutex);
2104 
2105 	if (ch->closed)
2106 		complete(ch->closed);
2107 
2108 	srpt_destroy_ch_ib(ch);
2109 
2110 	srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2111 			     ch->sport->sdev, ch->rq_size,
2112 			     ch->rsp_buf_cache, DMA_TO_DEVICE);
2113 
2114 	kmem_cache_destroy(ch->rsp_buf_cache);
2115 
2116 	srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2117 			     sdev, ch->rq_size,
2118 			     ch->req_buf_cache, DMA_FROM_DEVICE);
2119 
2120 	kmem_cache_destroy(ch->req_buf_cache);
2121 
2122 	kref_put(&ch->kref, srpt_free_ch);
2123 }
2124 
2125 /**
2126  * srpt_cm_req_recv - process the event IB_CM_REQ_RECEIVED
2127  * @sdev: HCA through which the login request was received.
2128  * @ib_cm_id: IB/CM connection identifier in case of IB/CM.
2129  * @rdma_cm_id: RDMA/CM connection identifier in case of RDMA/CM.
2130  * @port_num: Port through which the REQ message was received.
2131  * @pkey: P_Key of the incoming connection.
2132  * @req: SRP login request.
2133  * @src_addr: GID (IB/CM) or IP address (RDMA/CM) of the port that submitted
2134  * the login request.
2135  *
2136  * Ownership of the cm_id is transferred to the target session if this
2137  * function returns zero. Otherwise the caller remains the owner of cm_id.
2138  */
2139 static int srpt_cm_req_recv(struct srpt_device *const sdev,
2140 			    struct ib_cm_id *ib_cm_id,
2141 			    struct rdma_cm_id *rdma_cm_id,
2142 			    u8 port_num, __be16 pkey,
2143 			    const struct srp_login_req *req,
2144 			    const char *src_addr)
2145 {
2146 	struct srpt_port *sport = &sdev->port[port_num - 1];
2147 	struct srpt_nexus *nexus;
2148 	struct srp_login_rsp *rsp = NULL;
2149 	struct srp_login_rej *rej = NULL;
2150 	union {
2151 		struct rdma_conn_param rdma_cm;
2152 		struct ib_cm_rep_param ib_cm;
2153 	} *rep_param = NULL;
2154 	struct srpt_rdma_ch *ch = NULL;
2155 	char i_port_id[36];
2156 	u32 it_iu_len;
2157 	int i, tag_num, tag_size, ret;
2158 	struct srpt_tpg *stpg;
2159 
2160 	WARN_ON_ONCE(irqs_disabled());
2161 
2162 	if (WARN_ON(!sdev || !req))
2163 		return -EINVAL;
2164 
2165 	it_iu_len = be32_to_cpu(req->req_it_iu_len);
2166 
2167 	pr_info("Received SRP_LOGIN_REQ with i_port_id %pI6, t_port_id %pI6 and it_iu_len %d on port %d (guid=%pI6); pkey %#04x\n",
2168 		req->initiator_port_id, req->target_port_id, it_iu_len,
2169 		port_num, &sport->gid, be16_to_cpu(pkey));
2170 
2171 	nexus = srpt_get_nexus(sport, req->initiator_port_id,
2172 			       req->target_port_id);
2173 	if (IS_ERR(nexus)) {
2174 		ret = PTR_ERR(nexus);
2175 		goto out;
2176 	}
2177 
2178 	ret = -ENOMEM;
2179 	rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
2180 	rej = kzalloc(sizeof(*rej), GFP_KERNEL);
2181 	rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
2182 	if (!rsp || !rej || !rep_param)
2183 		goto out;
2184 
2185 	ret = -EINVAL;
2186 	if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
2187 		rej->reason = cpu_to_be32(
2188 				SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
2189 		pr_err("rejected SRP_LOGIN_REQ because its length (%d bytes) is out of range (%d .. %d)\n",
2190 		       it_iu_len, 64, srp_max_req_size);
2191 		goto reject;
2192 	}
2193 
2194 	if (!sport->enabled) {
2195 		rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2196 		pr_info("rejected SRP_LOGIN_REQ because target port %s_%d has not yet been enabled\n",
2197 			dev_name(&sport->sdev->device->dev), port_num);
2198 		goto reject;
2199 	}
2200 
2201 	if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
2202 	    || *(__be64 *)(req->target_port_id + 8) !=
2203 	       cpu_to_be64(srpt_service_guid)) {
2204 		rej->reason = cpu_to_be32(
2205 				SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
2206 		pr_err("rejected SRP_LOGIN_REQ because it has an invalid target port identifier.\n");
2207 		goto reject;
2208 	}
2209 
2210 	ret = -ENOMEM;
2211 	ch = kzalloc(sizeof(*ch), GFP_KERNEL);
2212 	if (!ch) {
2213 		rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2214 		pr_err("rejected SRP_LOGIN_REQ because out of memory.\n");
2215 		goto reject;
2216 	}
2217 
2218 	kref_init(&ch->kref);
2219 	ch->pkey = be16_to_cpu(pkey);
2220 	ch->nexus = nexus;
2221 	ch->zw_cqe.done = srpt_zerolength_write_done;
2222 	INIT_WORK(&ch->release_work, srpt_release_channel_work);
2223 	ch->sport = sport;
2224 	if (ib_cm_id) {
2225 		ch->ib_cm.cm_id = ib_cm_id;
2226 		ib_cm_id->context = ch;
2227 	} else {
2228 		ch->using_rdma_cm = true;
2229 		ch->rdma_cm.cm_id = rdma_cm_id;
2230 		rdma_cm_id->context = ch;
2231 	}
2232 	/*
2233 	 * ch->rq_size should be at least as large as the initiator queue
2234 	 * depth to avoid that the initiator driver has to report QUEUE_FULL
2235 	 * to the SCSI mid-layer.
2236 	 */
2237 	ch->rq_size = min(MAX_SRPT_RQ_SIZE, sdev->device->attrs.max_qp_wr);
2238 	spin_lock_init(&ch->spinlock);
2239 	ch->state = CH_CONNECTING;
2240 	INIT_LIST_HEAD(&ch->cmd_wait_list);
2241 	ch->max_rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
2242 
2243 	ch->rsp_buf_cache = kmem_cache_create("srpt-rsp-buf", ch->max_rsp_size,
2244 					      512, 0, NULL);
2245 	if (!ch->rsp_buf_cache)
2246 		goto free_ch;
2247 
2248 	ch->ioctx_ring = (struct srpt_send_ioctx **)
2249 		srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2250 				      sizeof(*ch->ioctx_ring[0]),
2251 				      ch->rsp_buf_cache, 0, DMA_TO_DEVICE);
2252 	if (!ch->ioctx_ring) {
2253 		pr_err("rejected SRP_LOGIN_REQ because creating a new QP SQ ring failed.\n");
2254 		rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2255 		goto free_rsp_cache;
2256 	}
2257 
2258 	for (i = 0; i < ch->rq_size; i++)
2259 		ch->ioctx_ring[i]->ch = ch;
2260 	if (!sdev->use_srq) {
2261 		u16 imm_data_offset = req->req_flags & SRP_IMMED_REQUESTED ?
2262 			be16_to_cpu(req->imm_data_offset) : 0;
2263 		u16 alignment_offset;
2264 		u32 req_sz;
2265 
2266 		if (req->req_flags & SRP_IMMED_REQUESTED)
2267 			pr_debug("imm_data_offset = %d\n",
2268 				 be16_to_cpu(req->imm_data_offset));
2269 		if (imm_data_offset >= sizeof(struct srp_cmd)) {
2270 			ch->imm_data_offset = imm_data_offset;
2271 			rsp->rsp_flags |= SRP_LOGIN_RSP_IMMED_SUPP;
2272 		} else {
2273 			ch->imm_data_offset = 0;
2274 		}
2275 		alignment_offset = round_up(imm_data_offset, 512) -
2276 			imm_data_offset;
2277 		req_sz = alignment_offset + imm_data_offset + srp_max_req_size;
2278 		ch->req_buf_cache = kmem_cache_create("srpt-req-buf", req_sz,
2279 						      512, 0, NULL);
2280 		if (!ch->req_buf_cache)
2281 			goto free_rsp_ring;
2282 
2283 		ch->ioctx_recv_ring = (struct srpt_recv_ioctx **)
2284 			srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
2285 					      sizeof(*ch->ioctx_recv_ring[0]),
2286 					      ch->req_buf_cache,
2287 					      alignment_offset,
2288 					      DMA_FROM_DEVICE);
2289 		if (!ch->ioctx_recv_ring) {
2290 			pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n");
2291 			rej->reason =
2292 			    cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2293 			goto free_recv_cache;
2294 		}
2295 		for (i = 0; i < ch->rq_size; i++)
2296 			INIT_LIST_HEAD(&ch->ioctx_recv_ring[i]->wait_list);
2297 	}
2298 
2299 	ret = srpt_create_ch_ib(ch);
2300 	if (ret) {
2301 		rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2302 		pr_err("rejected SRP_LOGIN_REQ because creating a new RDMA channel failed.\n");
2303 		goto free_recv_ring;
2304 	}
2305 
2306 	strlcpy(ch->sess_name, src_addr, sizeof(ch->sess_name));
2307 	snprintf(i_port_id, sizeof(i_port_id), "0x%016llx%016llx",
2308 			be64_to_cpu(*(__be64 *)nexus->i_port_id),
2309 			be64_to_cpu(*(__be64 *)(nexus->i_port_id + 8)));
2310 
2311 	pr_debug("registering src addr %s or i_port_id %s\n", ch->sess_name,
2312 		 i_port_id);
2313 
2314 	tag_num = ch->rq_size;
2315 	tag_size = 1; /* ib_srpt does not use se_sess->sess_cmd_map */
2316 
2317 	mutex_lock(&sport->port_guid_id.mutex);
2318 	list_for_each_entry(stpg, &sport->port_guid_id.tpg_list, entry) {
2319 		if (!IS_ERR_OR_NULL(ch->sess))
2320 			break;
2321 		ch->sess = target_setup_session(&stpg->tpg, tag_num,
2322 						tag_size, TARGET_PROT_NORMAL,
2323 						ch->sess_name, ch, NULL);
2324 	}
2325 	mutex_unlock(&sport->port_guid_id.mutex);
2326 
2327 	mutex_lock(&sport->port_gid_id.mutex);
2328 	list_for_each_entry(stpg, &sport->port_gid_id.tpg_list, entry) {
2329 		if (!IS_ERR_OR_NULL(ch->sess))
2330 			break;
2331 		ch->sess = target_setup_session(&stpg->tpg, tag_num,
2332 					tag_size, TARGET_PROT_NORMAL, i_port_id,
2333 					ch, NULL);
2334 		if (!IS_ERR_OR_NULL(ch->sess))
2335 			break;
2336 		/* Retry without leading "0x" */
2337 		ch->sess = target_setup_session(&stpg->tpg, tag_num,
2338 						tag_size, TARGET_PROT_NORMAL,
2339 						i_port_id + 2, ch, NULL);
2340 	}
2341 	mutex_unlock(&sport->port_gid_id.mutex);
2342 
2343 	if (IS_ERR_OR_NULL(ch->sess)) {
2344 		WARN_ON_ONCE(ch->sess == NULL);
2345 		ret = PTR_ERR(ch->sess);
2346 		ch->sess = NULL;
2347 		pr_info("Rejected login for initiator %s: ret = %d.\n",
2348 			ch->sess_name, ret);
2349 		rej->reason = cpu_to_be32(ret == -ENOMEM ?
2350 				SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
2351 				SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
2352 		goto destroy_ib;
2353 	}
2354 
2355 	/*
2356 	 * Once a session has been created destruction of srpt_rdma_ch objects
2357 	 * will decrement sport->refcount. Hence increment sport->refcount now.
2358 	 */
2359 	atomic_inc(&sport->refcount);
2360 
2361 	mutex_lock(&sport->mutex);
2362 
2363 	if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
2364 		struct srpt_rdma_ch *ch2;
2365 
2366 		list_for_each_entry(ch2, &nexus->ch_list, list) {
2367 			if (srpt_disconnect_ch(ch2) < 0)
2368 				continue;
2369 			pr_info("Relogin - closed existing channel %s\n",
2370 				ch2->sess_name);
2371 			rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
2372 		}
2373 	} else {
2374 		rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
2375 	}
2376 
2377 	list_add_tail_rcu(&ch->list, &nexus->ch_list);
2378 
2379 	if (!sport->enabled) {
2380 		rej->reason = cpu_to_be32(
2381 				SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2382 		pr_info("rejected SRP_LOGIN_REQ because target %s_%d is not enabled\n",
2383 			dev_name(&sdev->device->dev), port_num);
2384 		mutex_unlock(&sport->mutex);
2385 		goto reject;
2386 	}
2387 
2388 	mutex_unlock(&sport->mutex);
2389 
2390 	ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rtr(ch, ch->qp);
2391 	if (ret) {
2392 		rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2393 		pr_err("rejected SRP_LOGIN_REQ because enabling RTR failed (error code = %d)\n",
2394 		       ret);
2395 		goto reject;
2396 	}
2397 
2398 	pr_debug("Establish connection sess=%p name=%s ch=%p\n", ch->sess,
2399 		 ch->sess_name, ch);
2400 
2401 	/* create srp_login_response */
2402 	rsp->opcode = SRP_LOGIN_RSP;
2403 	rsp->tag = req->tag;
2404 	rsp->max_it_iu_len = cpu_to_be32(srp_max_req_size);
2405 	rsp->max_ti_iu_len = req->req_it_iu_len;
2406 	ch->max_ti_iu_len = it_iu_len;
2407 	rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
2408 				   SRP_BUF_FORMAT_INDIRECT);
2409 	rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
2410 	atomic_set(&ch->req_lim, ch->rq_size);
2411 	atomic_set(&ch->req_lim_delta, 0);
2412 
2413 	/* create cm reply */
2414 	if (ch->using_rdma_cm) {
2415 		rep_param->rdma_cm.private_data = (void *)rsp;
2416 		rep_param->rdma_cm.private_data_len = sizeof(*rsp);
2417 		rep_param->rdma_cm.rnr_retry_count = 7;
2418 		rep_param->rdma_cm.flow_control = 1;
2419 		rep_param->rdma_cm.responder_resources = 4;
2420 		rep_param->rdma_cm.initiator_depth = 4;
2421 	} else {
2422 		rep_param->ib_cm.qp_num = ch->qp->qp_num;
2423 		rep_param->ib_cm.private_data = (void *)rsp;
2424 		rep_param->ib_cm.private_data_len = sizeof(*rsp);
2425 		rep_param->ib_cm.rnr_retry_count = 7;
2426 		rep_param->ib_cm.flow_control = 1;
2427 		rep_param->ib_cm.failover_accepted = 0;
2428 		rep_param->ib_cm.srq = 1;
2429 		rep_param->ib_cm.responder_resources = 4;
2430 		rep_param->ib_cm.initiator_depth = 4;
2431 	}
2432 
2433 	/*
2434 	 * Hold the sport mutex while accepting a connection to avoid that
2435 	 * srpt_disconnect_ch() is invoked concurrently with this code.
2436 	 */
2437 	mutex_lock(&sport->mutex);
2438 	if (sport->enabled && ch->state == CH_CONNECTING) {
2439 		if (ch->using_rdma_cm)
2440 			ret = rdma_accept(rdma_cm_id, &rep_param->rdma_cm);
2441 		else
2442 			ret = ib_send_cm_rep(ib_cm_id, &rep_param->ib_cm);
2443 	} else {
2444 		ret = -EINVAL;
2445 	}
2446 	mutex_unlock(&sport->mutex);
2447 
2448 	switch (ret) {
2449 	case 0:
2450 		break;
2451 	case -EINVAL:
2452 		goto reject;
2453 	default:
2454 		rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
2455 		pr_err("sending SRP_LOGIN_REQ response failed (error code = %d)\n",
2456 		       ret);
2457 		goto reject;
2458 	}
2459 
2460 	goto out;
2461 
2462 destroy_ib:
2463 	srpt_destroy_ch_ib(ch);
2464 
2465 free_recv_ring:
2466 	srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
2467 			     ch->sport->sdev, ch->rq_size,
2468 			     ch->req_buf_cache, DMA_FROM_DEVICE);
2469 
2470 free_recv_cache:
2471 	kmem_cache_destroy(ch->req_buf_cache);
2472 
2473 free_rsp_ring:
2474 	srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
2475 			     ch->sport->sdev, ch->rq_size,
2476 			     ch->rsp_buf_cache, DMA_TO_DEVICE);
2477 
2478 free_rsp_cache:
2479 	kmem_cache_destroy(ch->rsp_buf_cache);
2480 
2481 free_ch:
2482 	if (rdma_cm_id)
2483 		rdma_cm_id->context = NULL;
2484 	else
2485 		ib_cm_id->context = NULL;
2486 	kfree(ch);
2487 	ch = NULL;
2488 
2489 	WARN_ON_ONCE(ret == 0);
2490 
2491 reject:
2492 	pr_info("Rejecting login with reason %#x\n", be32_to_cpu(rej->reason));
2493 	rej->opcode = SRP_LOGIN_REJ;
2494 	rej->tag = req->tag;
2495 	rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
2496 				   SRP_BUF_FORMAT_INDIRECT);
2497 
2498 	if (rdma_cm_id)
2499 		rdma_reject(rdma_cm_id, rej, sizeof(*rej));
2500 	else
2501 		ib_send_cm_rej(ib_cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
2502 			       rej, sizeof(*rej));
2503 
2504 	if (ch && ch->sess) {
2505 		srpt_close_ch(ch);
2506 		/*
2507 		 * Tell the caller not to free cm_id since
2508 		 * srpt_release_channel_work() will do that.
2509 		 */
2510 		ret = 0;
2511 	}
2512 
2513 out:
2514 	kfree(rep_param);
2515 	kfree(rsp);
2516 	kfree(rej);
2517 
2518 	return ret;
2519 }
2520 
2521 static int srpt_ib_cm_req_recv(struct ib_cm_id *cm_id,
2522 			       const struct ib_cm_req_event_param *param,
2523 			       void *private_data)
2524 {
2525 	char sguid[40];
2526 
2527 	srpt_format_guid(sguid, sizeof(sguid),
2528 			 &param->primary_path->dgid.global.interface_id);
2529 
2530 	return srpt_cm_req_recv(cm_id->context, cm_id, NULL, param->port,
2531 				param->primary_path->pkey,
2532 				private_data, sguid);
2533 }
2534 
2535 static int srpt_rdma_cm_req_recv(struct rdma_cm_id *cm_id,
2536 				 struct rdma_cm_event *event)
2537 {
2538 	struct srpt_device *sdev;
2539 	struct srp_login_req req;
2540 	const struct srp_login_req_rdma *req_rdma;
2541 	struct sa_path_rec *path_rec = cm_id->route.path_rec;
2542 	char src_addr[40];
2543 
2544 	sdev = ib_get_client_data(cm_id->device, &srpt_client);
2545 	if (!sdev)
2546 		return -ECONNREFUSED;
2547 
2548 	if (event->param.conn.private_data_len < sizeof(*req_rdma))
2549 		return -EINVAL;
2550 
2551 	/* Transform srp_login_req_rdma into srp_login_req. */
2552 	req_rdma = event->param.conn.private_data;
2553 	memset(&req, 0, sizeof(req));
2554 	req.opcode		= req_rdma->opcode;
2555 	req.tag			= req_rdma->tag;
2556 	req.req_it_iu_len	= req_rdma->req_it_iu_len;
2557 	req.req_buf_fmt		= req_rdma->req_buf_fmt;
2558 	req.req_flags		= req_rdma->req_flags;
2559 	memcpy(req.initiator_port_id, req_rdma->initiator_port_id, 16);
2560 	memcpy(req.target_port_id, req_rdma->target_port_id, 16);
2561 	req.imm_data_offset	= req_rdma->imm_data_offset;
2562 
2563 	snprintf(src_addr, sizeof(src_addr), "%pIS",
2564 		 &cm_id->route.addr.src_addr);
2565 
2566 	return srpt_cm_req_recv(sdev, NULL, cm_id, cm_id->port_num,
2567 				path_rec ? path_rec->pkey : 0, &req, src_addr);
2568 }
2569 
2570 static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
2571 			     enum ib_cm_rej_reason reason,
2572 			     const u8 *private_data,
2573 			     u8 private_data_len)
2574 {
2575 	char *priv = NULL;
2576 	int i;
2577 
2578 	if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
2579 						GFP_KERNEL))) {
2580 		for (i = 0; i < private_data_len; i++)
2581 			sprintf(priv + 3 * i, " %02x", private_data[i]);
2582 	}
2583 	pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
2584 		ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
2585 		"; private data" : "", priv ? priv : " (?)");
2586 	kfree(priv);
2587 }
2588 
2589 /**
2590  * srpt_cm_rtu_recv - process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event
2591  * @ch: SRPT RDMA channel.
2592  *
2593  * An RTU (ready to use) message indicates that the connection has been
2594  * established and that the recipient may begin transmitting.
2595  */
2596 static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
2597 {
2598 	int ret;
2599 
2600 	ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rts(ch, ch->qp);
2601 	if (ret < 0) {
2602 		pr_err("%s-%d: QP transition to RTS failed\n", ch->sess_name,
2603 		       ch->qp->qp_num);
2604 		srpt_close_ch(ch);
2605 		return;
2606 	}
2607 
2608 	/*
2609 	 * Note: calling srpt_close_ch() if the transition to the LIVE state
2610 	 * fails is not necessary since that means that that function has
2611 	 * already been invoked from another thread.
2612 	 */
2613 	if (!srpt_set_ch_state(ch, CH_LIVE)) {
2614 		pr_err("%s-%d: channel transition to LIVE state failed\n",
2615 		       ch->sess_name, ch->qp->qp_num);
2616 		return;
2617 	}
2618 
2619 	/* Trigger wait list processing. */
2620 	ret = srpt_zerolength_write(ch);
2621 	WARN_ONCE(ret < 0, "%d\n", ret);
2622 }
2623 
2624 /**
2625  * srpt_cm_handler - IB connection manager callback function
2626  * @cm_id: IB/CM connection identifier.
2627  * @event: IB/CM event.
2628  *
2629  * A non-zero return value will cause the caller destroy the CM ID.
2630  *
2631  * Note: srpt_cm_handler() must only return a non-zero value when transferring
2632  * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
2633  * a non-zero value in any other case will trigger a race with the
2634  * ib_destroy_cm_id() call in srpt_release_channel().
2635  */
2636 static int srpt_cm_handler(struct ib_cm_id *cm_id,
2637 			   const struct ib_cm_event *event)
2638 {
2639 	struct srpt_rdma_ch *ch = cm_id->context;
2640 	int ret;
2641 
2642 	ret = 0;
2643 	switch (event->event) {
2644 	case IB_CM_REQ_RECEIVED:
2645 		ret = srpt_ib_cm_req_recv(cm_id, &event->param.req_rcvd,
2646 					  event->private_data);
2647 		break;
2648 	case IB_CM_REJ_RECEIVED:
2649 		srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
2650 				 event->private_data,
2651 				 IB_CM_REJ_PRIVATE_DATA_SIZE);
2652 		break;
2653 	case IB_CM_RTU_RECEIVED:
2654 	case IB_CM_USER_ESTABLISHED:
2655 		srpt_cm_rtu_recv(ch);
2656 		break;
2657 	case IB_CM_DREQ_RECEIVED:
2658 		srpt_disconnect_ch(ch);
2659 		break;
2660 	case IB_CM_DREP_RECEIVED:
2661 		pr_info("Received CM DREP message for ch %s-%d.\n",
2662 			ch->sess_name, ch->qp->qp_num);
2663 		srpt_close_ch(ch);
2664 		break;
2665 	case IB_CM_TIMEWAIT_EXIT:
2666 		pr_info("Received CM TimeWait exit for ch %s-%d.\n",
2667 			ch->sess_name, ch->qp->qp_num);
2668 		srpt_close_ch(ch);
2669 		break;
2670 	case IB_CM_REP_ERROR:
2671 		pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2672 			ch->qp->qp_num);
2673 		break;
2674 	case IB_CM_DREQ_ERROR:
2675 		pr_info("Received CM DREQ ERROR event.\n");
2676 		break;
2677 	case IB_CM_MRA_RECEIVED:
2678 		pr_info("Received CM MRA event\n");
2679 		break;
2680 	default:
2681 		pr_err("received unrecognized CM event %d\n", event->event);
2682 		break;
2683 	}
2684 
2685 	return ret;
2686 }
2687 
2688 static int srpt_rdma_cm_handler(struct rdma_cm_id *cm_id,
2689 				struct rdma_cm_event *event)
2690 {
2691 	struct srpt_rdma_ch *ch = cm_id->context;
2692 	int ret = 0;
2693 
2694 	switch (event->event) {
2695 	case RDMA_CM_EVENT_CONNECT_REQUEST:
2696 		ret = srpt_rdma_cm_req_recv(cm_id, event);
2697 		break;
2698 	case RDMA_CM_EVENT_REJECTED:
2699 		srpt_cm_rej_recv(ch, event->status,
2700 				 event->param.conn.private_data,
2701 				 event->param.conn.private_data_len);
2702 		break;
2703 	case RDMA_CM_EVENT_ESTABLISHED:
2704 		srpt_cm_rtu_recv(ch);
2705 		break;
2706 	case RDMA_CM_EVENT_DISCONNECTED:
2707 		if (ch->state < CH_DISCONNECTING)
2708 			srpt_disconnect_ch(ch);
2709 		else
2710 			srpt_close_ch(ch);
2711 		break;
2712 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2713 		srpt_close_ch(ch);
2714 		break;
2715 	case RDMA_CM_EVENT_UNREACHABLE:
2716 		pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
2717 			ch->qp->qp_num);
2718 		break;
2719 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
2720 	case RDMA_CM_EVENT_ADDR_CHANGE:
2721 		break;
2722 	default:
2723 		pr_err("received unrecognized RDMA CM event %d\n",
2724 		       event->event);
2725 		break;
2726 	}
2727 
2728 	return ret;
2729 }
2730 
2731 /*
2732  * srpt_write_pending - Start data transfer from initiator to target (write).
2733  */
2734 static int srpt_write_pending(struct se_cmd *se_cmd)
2735 {
2736 	struct srpt_send_ioctx *ioctx =
2737 		container_of(se_cmd, struct srpt_send_ioctx, cmd);
2738 	struct srpt_rdma_ch *ch = ioctx->ch;
2739 	struct ib_send_wr *first_wr = NULL;
2740 	struct ib_cqe *cqe = &ioctx->rdma_cqe;
2741 	enum srpt_command_state new_state;
2742 	int ret, i;
2743 
2744 	if (ioctx->recv_ioctx) {
2745 		srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
2746 		target_execute_cmd(&ioctx->cmd);
2747 		return 0;
2748 	}
2749 
2750 	new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
2751 	WARN_ON(new_state == SRPT_STATE_DONE);
2752 
2753 	if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
2754 		pr_warn("%s: IB send queue full (needed %d)\n",
2755 				__func__, ioctx->n_rdma);
2756 		ret = -ENOMEM;
2757 		goto out_undo;
2758 	}
2759 
2760 	cqe->done = srpt_rdma_read_done;
2761 	for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2762 		struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2763 
2764 		first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
2765 				cqe, first_wr);
2766 		cqe = NULL;
2767 	}
2768 
2769 	ret = ib_post_send(ch->qp, first_wr, NULL);
2770 	if (ret) {
2771 		pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
2772 			 __func__, ret, ioctx->n_rdma,
2773 			 atomic_read(&ch->sq_wr_avail));
2774 		goto out_undo;
2775 	}
2776 
2777 	return 0;
2778 out_undo:
2779 	atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
2780 	return ret;
2781 }
2782 
2783 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
2784 {
2785 	switch (tcm_mgmt_status) {
2786 	case TMR_FUNCTION_COMPLETE:
2787 		return SRP_TSK_MGMT_SUCCESS;
2788 	case TMR_FUNCTION_REJECTED:
2789 		return SRP_TSK_MGMT_FUNC_NOT_SUPP;
2790 	}
2791 	return SRP_TSK_MGMT_FAILED;
2792 }
2793 
2794 /**
2795  * srpt_queue_response - transmit the response to a SCSI command
2796  * @cmd: SCSI target command.
2797  *
2798  * Callback function called by the TCM core. Must not block since it can be
2799  * invoked on the context of the IB completion handler.
2800  */
2801 static void srpt_queue_response(struct se_cmd *cmd)
2802 {
2803 	struct srpt_send_ioctx *ioctx =
2804 		container_of(cmd, struct srpt_send_ioctx, cmd);
2805 	struct srpt_rdma_ch *ch = ioctx->ch;
2806 	struct srpt_device *sdev = ch->sport->sdev;
2807 	struct ib_send_wr send_wr, *first_wr = &send_wr;
2808 	struct ib_sge sge;
2809 	enum srpt_command_state state;
2810 	int resp_len, ret, i;
2811 	u8 srp_tm_status;
2812 
2813 	state = ioctx->state;
2814 	switch (state) {
2815 	case SRPT_STATE_NEW:
2816 	case SRPT_STATE_DATA_IN:
2817 		ioctx->state = SRPT_STATE_CMD_RSP_SENT;
2818 		break;
2819 	case SRPT_STATE_MGMT:
2820 		ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
2821 		break;
2822 	default:
2823 		WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
2824 			ch, ioctx->ioctx.index, ioctx->state);
2825 		break;
2826 	}
2827 
2828 	if (WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))
2829 		return;
2830 
2831 	/* For read commands, transfer the data to the initiator. */
2832 	if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
2833 	    ioctx->cmd.data_length &&
2834 	    !ioctx->queue_status_only) {
2835 		for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
2836 			struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
2837 
2838 			first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
2839 					ch->sport->port, NULL, first_wr);
2840 		}
2841 	}
2842 
2843 	if (state != SRPT_STATE_MGMT)
2844 		resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
2845 					      cmd->scsi_status);
2846 	else {
2847 		srp_tm_status
2848 			= tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
2849 		resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
2850 						 ioctx->cmd.tag);
2851 	}
2852 
2853 	atomic_inc(&ch->req_lim);
2854 
2855 	if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
2856 			&ch->sq_wr_avail) < 0)) {
2857 		pr_warn("%s: IB send queue full (needed %d)\n",
2858 				__func__, ioctx->n_rdma);
2859 		ret = -ENOMEM;
2860 		goto out;
2861 	}
2862 
2863 	ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
2864 				      DMA_TO_DEVICE);
2865 
2866 	sge.addr = ioctx->ioctx.dma;
2867 	sge.length = resp_len;
2868 	sge.lkey = sdev->lkey;
2869 
2870 	ioctx->ioctx.cqe.done = srpt_send_done;
2871 	send_wr.next = NULL;
2872 	send_wr.wr_cqe = &ioctx->ioctx.cqe;
2873 	send_wr.sg_list = &sge;
2874 	send_wr.num_sge = 1;
2875 	send_wr.opcode = IB_WR_SEND;
2876 	send_wr.send_flags = IB_SEND_SIGNALED;
2877 
2878 	ret = ib_post_send(ch->qp, first_wr, NULL);
2879 	if (ret < 0) {
2880 		pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
2881 			__func__, ioctx->cmd.tag, ret);
2882 		goto out;
2883 	}
2884 
2885 	return;
2886 
2887 out:
2888 	atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
2889 	atomic_dec(&ch->req_lim);
2890 	srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
2891 	target_put_sess_cmd(&ioctx->cmd);
2892 }
2893 
2894 static int srpt_queue_data_in(struct se_cmd *cmd)
2895 {
2896 	srpt_queue_response(cmd);
2897 	return 0;
2898 }
2899 
2900 static void srpt_queue_tm_rsp(struct se_cmd *cmd)
2901 {
2902 	srpt_queue_response(cmd);
2903 }
2904 
2905 /*
2906  * This function is called for aborted commands if no response is sent to the
2907  * initiator. Make sure that the credits freed by aborting a command are
2908  * returned to the initiator the next time a response is sent by incrementing
2909  * ch->req_lim_delta.
2910  */
2911 static void srpt_aborted_task(struct se_cmd *cmd)
2912 {
2913 	struct srpt_send_ioctx *ioctx = container_of(cmd,
2914 				struct srpt_send_ioctx, cmd);
2915 	struct srpt_rdma_ch *ch = ioctx->ch;
2916 
2917 	atomic_inc(&ch->req_lim_delta);
2918 }
2919 
2920 static int srpt_queue_status(struct se_cmd *cmd)
2921 {
2922 	struct srpt_send_ioctx *ioctx;
2923 
2924 	ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
2925 	BUG_ON(ioctx->sense_data != cmd->sense_buffer);
2926 	if (cmd->se_cmd_flags &
2927 	    (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
2928 		WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
2929 	ioctx->queue_status_only = true;
2930 	srpt_queue_response(cmd);
2931 	return 0;
2932 }
2933 
2934 static void srpt_refresh_port_work(struct work_struct *work)
2935 {
2936 	struct srpt_port *sport = container_of(work, struct srpt_port, work);
2937 
2938 	srpt_refresh_port(sport);
2939 }
2940 
2941 /**
2942  * srpt_release_sport - disable login and wait for associated channels
2943  * @sport: SRPT HCA port.
2944  */
2945 static int srpt_release_sport(struct srpt_port *sport)
2946 {
2947 	DECLARE_COMPLETION_ONSTACK(c);
2948 	struct srpt_nexus *nexus, *next_n;
2949 	struct srpt_rdma_ch *ch;
2950 
2951 	WARN_ON_ONCE(irqs_disabled());
2952 
2953 	sport->freed_channels = &c;
2954 
2955 	mutex_lock(&sport->mutex);
2956 	srpt_set_enabled(sport, false);
2957 	mutex_unlock(&sport->mutex);
2958 
2959 	while (atomic_read(&sport->refcount) > 0 &&
2960 	       wait_for_completion_timeout(&c, 5 * HZ) <= 0) {
2961 		pr_info("%s_%d: waiting for unregistration of %d sessions ...\n",
2962 			dev_name(&sport->sdev->device->dev), sport->port,
2963 			atomic_read(&sport->refcount));
2964 		rcu_read_lock();
2965 		list_for_each_entry(nexus, &sport->nexus_list, entry) {
2966 			list_for_each_entry(ch, &nexus->ch_list, list) {
2967 				pr_info("%s-%d: state %s\n",
2968 					ch->sess_name, ch->qp->qp_num,
2969 					get_ch_state_name(ch->state));
2970 			}
2971 		}
2972 		rcu_read_unlock();
2973 	}
2974 
2975 	mutex_lock(&sport->mutex);
2976 	list_for_each_entry_safe(nexus, next_n, &sport->nexus_list, entry) {
2977 		list_del(&nexus->entry);
2978 		kfree_rcu(nexus, rcu);
2979 	}
2980 	mutex_unlock(&sport->mutex);
2981 
2982 	return 0;
2983 }
2984 
2985 static struct se_wwn *__srpt_lookup_wwn(const char *name)
2986 {
2987 	struct ib_device *dev;
2988 	struct srpt_device *sdev;
2989 	struct srpt_port *sport;
2990 	int i;
2991 
2992 	list_for_each_entry(sdev, &srpt_dev_list, list) {
2993 		dev = sdev->device;
2994 		if (!dev)
2995 			continue;
2996 
2997 		for (i = 0; i < dev->phys_port_cnt; i++) {
2998 			sport = &sdev->port[i];
2999 
3000 			if (strcmp(sport->port_guid_id.name, name) == 0)
3001 				return &sport->port_guid_id.wwn;
3002 			if (strcmp(sport->port_gid_id.name, name) == 0)
3003 				return &sport->port_gid_id.wwn;
3004 		}
3005 	}
3006 
3007 	return NULL;
3008 }
3009 
3010 static struct se_wwn *srpt_lookup_wwn(const char *name)
3011 {
3012 	struct se_wwn *wwn;
3013 
3014 	spin_lock(&srpt_dev_lock);
3015 	wwn = __srpt_lookup_wwn(name);
3016 	spin_unlock(&srpt_dev_lock);
3017 
3018 	return wwn;
3019 }
3020 
3021 static void srpt_free_srq(struct srpt_device *sdev)
3022 {
3023 	if (!sdev->srq)
3024 		return;
3025 
3026 	ib_destroy_srq(sdev->srq);
3027 	srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
3028 			     sdev->srq_size, sdev->req_buf_cache,
3029 			     DMA_FROM_DEVICE);
3030 	kmem_cache_destroy(sdev->req_buf_cache);
3031 	sdev->srq = NULL;
3032 }
3033 
3034 static int srpt_alloc_srq(struct srpt_device *sdev)
3035 {
3036 	struct ib_srq_init_attr srq_attr = {
3037 		.event_handler = srpt_srq_event,
3038 		.srq_context = (void *)sdev,
3039 		.attr.max_wr = sdev->srq_size,
3040 		.attr.max_sge = 1,
3041 		.srq_type = IB_SRQT_BASIC,
3042 	};
3043 	struct ib_device *device = sdev->device;
3044 	struct ib_srq *srq;
3045 	int i;
3046 
3047 	WARN_ON_ONCE(sdev->srq);
3048 	srq = ib_create_srq(sdev->pd, &srq_attr);
3049 	if (IS_ERR(srq)) {
3050 		pr_debug("ib_create_srq() failed: %ld\n", PTR_ERR(srq));
3051 		return PTR_ERR(srq);
3052 	}
3053 
3054 	pr_debug("create SRQ #wr= %d max_allow=%d dev= %s\n", sdev->srq_size,
3055 		 sdev->device->attrs.max_srq_wr, dev_name(&device->dev));
3056 
3057 	sdev->req_buf_cache = kmem_cache_create("srpt-srq-req-buf",
3058 						srp_max_req_size, 0, 0, NULL);
3059 	if (!sdev->req_buf_cache)
3060 		goto free_srq;
3061 
3062 	sdev->ioctx_ring = (struct srpt_recv_ioctx **)
3063 		srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
3064 				      sizeof(*sdev->ioctx_ring[0]),
3065 				      sdev->req_buf_cache, 0, DMA_FROM_DEVICE);
3066 	if (!sdev->ioctx_ring)
3067 		goto free_cache;
3068 
3069 	sdev->use_srq = true;
3070 	sdev->srq = srq;
3071 
3072 	for (i = 0; i < sdev->srq_size; ++i) {
3073 		INIT_LIST_HEAD(&sdev->ioctx_ring[i]->wait_list);
3074 		srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]);
3075 	}
3076 
3077 	return 0;
3078 
3079 free_cache:
3080 	kmem_cache_destroy(sdev->req_buf_cache);
3081 
3082 free_srq:
3083 	ib_destroy_srq(srq);
3084 	return -ENOMEM;
3085 }
3086 
3087 static int srpt_use_srq(struct srpt_device *sdev, bool use_srq)
3088 {
3089 	struct ib_device *device = sdev->device;
3090 	int ret = 0;
3091 
3092 	if (!use_srq) {
3093 		srpt_free_srq(sdev);
3094 		sdev->use_srq = false;
3095 	} else if (use_srq && !sdev->srq) {
3096 		ret = srpt_alloc_srq(sdev);
3097 	}
3098 	pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__,
3099 		 dev_name(&device->dev), sdev->use_srq, ret);
3100 	return ret;
3101 }
3102 
3103 /**
3104  * srpt_add_one - InfiniBand device addition callback function
3105  * @device: Describes a HCA.
3106  */
3107 static void srpt_add_one(struct ib_device *device)
3108 {
3109 	struct srpt_device *sdev;
3110 	struct srpt_port *sport;
3111 	int i, ret;
3112 
3113 	pr_debug("device = %p\n", device);
3114 
3115 	sdev = kzalloc(struct_size(sdev, port, device->phys_port_cnt),
3116 		       GFP_KERNEL);
3117 	if (!sdev)
3118 		goto err;
3119 
3120 	sdev->device = device;
3121 	mutex_init(&sdev->sdev_mutex);
3122 
3123 	sdev->pd = ib_alloc_pd(device, 0);
3124 	if (IS_ERR(sdev->pd))
3125 		goto free_dev;
3126 
3127 	sdev->lkey = sdev->pd->local_dma_lkey;
3128 
3129 	sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
3130 
3131 	srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq);
3132 
3133 	if (!srpt_service_guid)
3134 		srpt_service_guid = be64_to_cpu(device->node_guid);
3135 
3136 	if (rdma_port_get_link_layer(device, 1) == IB_LINK_LAYER_INFINIBAND)
3137 		sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
3138 	if (IS_ERR(sdev->cm_id)) {
3139 		pr_info("ib_create_cm_id() failed: %ld\n",
3140 			PTR_ERR(sdev->cm_id));
3141 		sdev->cm_id = NULL;
3142 		if (!rdma_cm_id)
3143 			goto err_ring;
3144 	}
3145 
3146 	/* print out target login information */
3147 	pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,pkey=ffff,service_id=%016llx\n",
3148 		 srpt_service_guid, srpt_service_guid, srpt_service_guid);
3149 
3150 	/*
3151 	 * We do not have a consistent service_id (ie. also id_ext of target_id)
3152 	 * to identify this target. We currently use the guid of the first HCA
3153 	 * in the system as service_id; therefore, the target_id will change
3154 	 * if this HCA is gone bad and replaced by different HCA
3155 	 */
3156 	ret = sdev->cm_id ?
3157 		ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0) :
3158 		0;
3159 	if (ret < 0) {
3160 		pr_err("ib_cm_listen() failed: %d (cm_id state = %d)\n", ret,
3161 		       sdev->cm_id->state);
3162 		goto err_cm;
3163 	}
3164 
3165 	INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
3166 			      srpt_event_handler);
3167 	ib_register_event_handler(&sdev->event_handler);
3168 
3169 	for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
3170 		sport = &sdev->port[i - 1];
3171 		INIT_LIST_HEAD(&sport->nexus_list);
3172 		mutex_init(&sport->mutex);
3173 		sport->sdev = sdev;
3174 		sport->port = i;
3175 		sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
3176 		sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
3177 		sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
3178 		sport->port_attrib.use_srq = false;
3179 		INIT_WORK(&sport->work, srpt_refresh_port_work);
3180 		mutex_init(&sport->port_guid_id.mutex);
3181 		INIT_LIST_HEAD(&sport->port_guid_id.tpg_list);
3182 		mutex_init(&sport->port_gid_id.mutex);
3183 		INIT_LIST_HEAD(&sport->port_gid_id.tpg_list);
3184 
3185 		if (srpt_refresh_port(sport)) {
3186 			pr_err("MAD registration failed for %s-%d.\n",
3187 			       dev_name(&sdev->device->dev), i);
3188 			goto err_event;
3189 		}
3190 	}
3191 
3192 	spin_lock(&srpt_dev_lock);
3193 	list_add_tail(&sdev->list, &srpt_dev_list);
3194 	spin_unlock(&srpt_dev_lock);
3195 
3196 out:
3197 	ib_set_client_data(device, &srpt_client, sdev);
3198 	pr_debug("added %s.\n", dev_name(&device->dev));
3199 	return;
3200 
3201 err_event:
3202 	ib_unregister_event_handler(&sdev->event_handler);
3203 err_cm:
3204 	if (sdev->cm_id)
3205 		ib_destroy_cm_id(sdev->cm_id);
3206 err_ring:
3207 	srpt_free_srq(sdev);
3208 	ib_dealloc_pd(sdev->pd);
3209 free_dev:
3210 	kfree(sdev);
3211 err:
3212 	sdev = NULL;
3213 	pr_info("%s(%s) failed.\n", __func__, dev_name(&device->dev));
3214 	goto out;
3215 }
3216 
3217 /**
3218  * srpt_remove_one - InfiniBand device removal callback function
3219  * @device: Describes a HCA.
3220  * @client_data: The value passed as the third argument to ib_set_client_data().
3221  */
3222 static void srpt_remove_one(struct ib_device *device, void *client_data)
3223 {
3224 	struct srpt_device *sdev = client_data;
3225 	int i;
3226 
3227 	if (!sdev) {
3228 		pr_info("%s(%s): nothing to do.\n", __func__,
3229 			dev_name(&device->dev));
3230 		return;
3231 	}
3232 
3233 	srpt_unregister_mad_agent(sdev);
3234 
3235 	ib_unregister_event_handler(&sdev->event_handler);
3236 
3237 	/* Cancel any work queued by the just unregistered IB event handler. */
3238 	for (i = 0; i < sdev->device->phys_port_cnt; i++)
3239 		cancel_work_sync(&sdev->port[i].work);
3240 
3241 	if (sdev->cm_id)
3242 		ib_destroy_cm_id(sdev->cm_id);
3243 
3244 	ib_set_client_data(device, &srpt_client, NULL);
3245 
3246 	/*
3247 	 * Unregistering a target must happen after destroying sdev->cm_id
3248 	 * such that no new SRP_LOGIN_REQ information units can arrive while
3249 	 * destroying the target.
3250 	 */
3251 	spin_lock(&srpt_dev_lock);
3252 	list_del(&sdev->list);
3253 	spin_unlock(&srpt_dev_lock);
3254 
3255 	for (i = 0; i < sdev->device->phys_port_cnt; i++)
3256 		srpt_release_sport(&sdev->port[i]);
3257 
3258 	srpt_free_srq(sdev);
3259 
3260 	ib_dealloc_pd(sdev->pd);
3261 
3262 	kfree(sdev);
3263 }
3264 
3265 static struct ib_client srpt_client = {
3266 	.name = DRV_NAME,
3267 	.add = srpt_add_one,
3268 	.remove = srpt_remove_one
3269 };
3270 
3271 static int srpt_check_true(struct se_portal_group *se_tpg)
3272 {
3273 	return 1;
3274 }
3275 
3276 static int srpt_check_false(struct se_portal_group *se_tpg)
3277 {
3278 	return 0;
3279 }
3280 
3281 static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg)
3282 {
3283 	return tpg->se_tpg_wwn->priv;
3284 }
3285 
3286 static struct srpt_port_id *srpt_wwn_to_sport_id(struct se_wwn *wwn)
3287 {
3288 	struct srpt_port *sport = wwn->priv;
3289 
3290 	if (wwn == &sport->port_guid_id.wwn)
3291 		return &sport->port_guid_id;
3292 	if (wwn == &sport->port_gid_id.wwn)
3293 		return &sport->port_gid_id;
3294 	WARN_ON_ONCE(true);
3295 	return NULL;
3296 }
3297 
3298 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
3299 {
3300 	struct srpt_tpg *stpg = container_of(tpg, typeof(*stpg), tpg);
3301 
3302 	return stpg->sport_id->name;
3303 }
3304 
3305 static u16 srpt_get_tag(struct se_portal_group *tpg)
3306 {
3307 	return 1;
3308 }
3309 
3310 static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
3311 {
3312 	return 1;
3313 }
3314 
3315 static void srpt_release_cmd(struct se_cmd *se_cmd)
3316 {
3317 	struct srpt_send_ioctx *ioctx = container_of(se_cmd,
3318 				struct srpt_send_ioctx, cmd);
3319 	struct srpt_rdma_ch *ch = ioctx->ch;
3320 	struct srpt_recv_ioctx *recv_ioctx = ioctx->recv_ioctx;
3321 
3322 	WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
3323 		     !(ioctx->cmd.transport_state & CMD_T_ABORTED));
3324 
3325 	if (recv_ioctx) {
3326 		WARN_ON_ONCE(!list_empty(&recv_ioctx->wait_list));
3327 		ioctx->recv_ioctx = NULL;
3328 		srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
3329 	}
3330 
3331 	if (ioctx->n_rw_ctx) {
3332 		srpt_free_rw_ctxs(ch, ioctx);
3333 		ioctx->n_rw_ctx = 0;
3334 	}
3335 
3336 	target_free_tag(se_cmd->se_sess, se_cmd);
3337 }
3338 
3339 /**
3340  * srpt_close_session - forcibly close a session
3341  * @se_sess: SCSI target session.
3342  *
3343  * Callback function invoked by the TCM core to clean up sessions associated
3344  * with a node ACL when the user invokes
3345  * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3346  */
3347 static void srpt_close_session(struct se_session *se_sess)
3348 {
3349 	struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
3350 
3351 	srpt_disconnect_ch_sync(ch);
3352 }
3353 
3354 /**
3355  * srpt_sess_get_index - return the value of scsiAttIntrPortIndex (SCSI-MIB)
3356  * @se_sess: SCSI target session.
3357  *
3358  * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
3359  * This object represents an arbitrary integer used to uniquely identify a
3360  * particular attached remote initiator port to a particular SCSI target port
3361  * within a particular SCSI target device within a particular SCSI instance.
3362  */
3363 static u32 srpt_sess_get_index(struct se_session *se_sess)
3364 {
3365 	return 0;
3366 }
3367 
3368 static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
3369 {
3370 }
3371 
3372 /* Note: only used from inside debug printk's by the TCM core. */
3373 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
3374 {
3375 	struct srpt_send_ioctx *ioctx;
3376 
3377 	ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
3378 	return ioctx->state;
3379 }
3380 
3381 static int srpt_parse_guid(u64 *guid, const char *name)
3382 {
3383 	u16 w[4];
3384 	int ret = -EINVAL;
3385 
3386 	if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4)
3387 		goto out;
3388 	*guid = get_unaligned_be64(w);
3389 	ret = 0;
3390 out:
3391 	return ret;
3392 }
3393 
3394 /**
3395  * srpt_parse_i_port_id - parse an initiator port ID
3396  * @name: ASCII representation of a 128-bit initiator port ID.
3397  * @i_port_id: Binary 128-bit port ID.
3398  */
3399 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
3400 {
3401 	const char *p;
3402 	unsigned len, count, leading_zero_bytes;
3403 	int ret;
3404 
3405 	p = name;
3406 	if (strncasecmp(p, "0x", 2) == 0)
3407 		p += 2;
3408 	ret = -EINVAL;
3409 	len = strlen(p);
3410 	if (len % 2)
3411 		goto out;
3412 	count = min(len / 2, 16U);
3413 	leading_zero_bytes = 16 - count;
3414 	memset(i_port_id, 0, leading_zero_bytes);
3415 	ret = hex2bin(i_port_id + leading_zero_bytes, p, count);
3416 
3417 out:
3418 	return ret;
3419 }
3420 
3421 /*
3422  * configfs callback function invoked for mkdir
3423  * /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
3424  *
3425  * i_port_id must be an initiator port GUID, GID or IP address. See also the
3426  * target_alloc_session() calls in this driver. Examples of valid initiator
3427  * port IDs:
3428  * 0x0000000000000000505400fffe4a0b7b
3429  * 0000000000000000505400fffe4a0b7b
3430  * 5054:00ff:fe4a:0b7b
3431  * 192.168.122.76
3432  */
3433 static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
3434 {
3435 	struct sockaddr_storage sa;
3436 	u64 guid;
3437 	u8 i_port_id[16];
3438 	int ret;
3439 
3440 	ret = srpt_parse_guid(&guid, name);
3441 	if (ret < 0)
3442 		ret = srpt_parse_i_port_id(i_port_id, name);
3443 	if (ret < 0)
3444 		ret = inet_pton_with_scope(&init_net, AF_UNSPEC, name, NULL,
3445 					   &sa);
3446 	if (ret < 0)
3447 		pr_err("invalid initiator port ID %s\n", name);
3448 	return ret;
3449 }
3450 
3451 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
3452 		char *page)
3453 {
3454 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
3455 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3456 
3457 	return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
3458 }
3459 
3460 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
3461 		const char *page, size_t count)
3462 {
3463 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
3464 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3465 	unsigned long val;
3466 	int ret;
3467 
3468 	ret = kstrtoul(page, 0, &val);
3469 	if (ret < 0) {
3470 		pr_err("kstrtoul() failed with ret: %d\n", ret);
3471 		return -EINVAL;
3472 	}
3473 	if (val > MAX_SRPT_RDMA_SIZE) {
3474 		pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
3475 			MAX_SRPT_RDMA_SIZE);
3476 		return -EINVAL;
3477 	}
3478 	if (val < DEFAULT_MAX_RDMA_SIZE) {
3479 		pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
3480 			val, DEFAULT_MAX_RDMA_SIZE);
3481 		return -EINVAL;
3482 	}
3483 	sport->port_attrib.srp_max_rdma_size = val;
3484 
3485 	return count;
3486 }
3487 
3488 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
3489 		char *page)
3490 {
3491 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
3492 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3493 
3494 	return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
3495 }
3496 
3497 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
3498 		const char *page, size_t count)
3499 {
3500 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
3501 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3502 	unsigned long val;
3503 	int ret;
3504 
3505 	ret = kstrtoul(page, 0, &val);
3506 	if (ret < 0) {
3507 		pr_err("kstrtoul() failed with ret: %d\n", ret);
3508 		return -EINVAL;
3509 	}
3510 	if (val > MAX_SRPT_RSP_SIZE) {
3511 		pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
3512 			MAX_SRPT_RSP_SIZE);
3513 		return -EINVAL;
3514 	}
3515 	if (val < MIN_MAX_RSP_SIZE) {
3516 		pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
3517 			MIN_MAX_RSP_SIZE);
3518 		return -EINVAL;
3519 	}
3520 	sport->port_attrib.srp_max_rsp_size = val;
3521 
3522 	return count;
3523 }
3524 
3525 static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
3526 		char *page)
3527 {
3528 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
3529 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3530 
3531 	return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
3532 }
3533 
3534 static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
3535 		const char *page, size_t count)
3536 {
3537 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
3538 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3539 	unsigned long val;
3540 	int ret;
3541 
3542 	ret = kstrtoul(page, 0, &val);
3543 	if (ret < 0) {
3544 		pr_err("kstrtoul() failed with ret: %d\n", ret);
3545 		return -EINVAL;
3546 	}
3547 	if (val > MAX_SRPT_SRQ_SIZE) {
3548 		pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
3549 			MAX_SRPT_SRQ_SIZE);
3550 		return -EINVAL;
3551 	}
3552 	if (val < MIN_SRPT_SRQ_SIZE) {
3553 		pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
3554 			MIN_SRPT_SRQ_SIZE);
3555 		return -EINVAL;
3556 	}
3557 	sport->port_attrib.srp_sq_size = val;
3558 
3559 	return count;
3560 }
3561 
3562 static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item,
3563 					    char *page)
3564 {
3565 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
3566 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3567 
3568 	return sprintf(page, "%d\n", sport->port_attrib.use_srq);
3569 }
3570 
3571 static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item,
3572 					     const char *page, size_t count)
3573 {
3574 	struct se_portal_group *se_tpg = attrib_to_tpg(item);
3575 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3576 	struct srpt_device *sdev = sport->sdev;
3577 	unsigned long val;
3578 	bool enabled;
3579 	int ret;
3580 
3581 	ret = kstrtoul(page, 0, &val);
3582 	if (ret < 0)
3583 		return ret;
3584 	if (val != !!val)
3585 		return -EINVAL;
3586 
3587 	ret = mutex_lock_interruptible(&sdev->sdev_mutex);
3588 	if (ret < 0)
3589 		return ret;
3590 	ret = mutex_lock_interruptible(&sport->mutex);
3591 	if (ret < 0)
3592 		goto unlock_sdev;
3593 	enabled = sport->enabled;
3594 	/* Log out all initiator systems before changing 'use_srq'. */
3595 	srpt_set_enabled(sport, false);
3596 	sport->port_attrib.use_srq = val;
3597 	srpt_use_srq(sdev, sport->port_attrib.use_srq);
3598 	srpt_set_enabled(sport, enabled);
3599 	ret = count;
3600 	mutex_unlock(&sport->mutex);
3601 unlock_sdev:
3602 	mutex_unlock(&sdev->sdev_mutex);
3603 
3604 	return ret;
3605 }
3606 
3607 CONFIGFS_ATTR(srpt_tpg_attrib_,  srp_max_rdma_size);
3608 CONFIGFS_ATTR(srpt_tpg_attrib_,  srp_max_rsp_size);
3609 CONFIGFS_ATTR(srpt_tpg_attrib_,  srp_sq_size);
3610 CONFIGFS_ATTR(srpt_tpg_attrib_,  use_srq);
3611 
3612 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
3613 	&srpt_tpg_attrib_attr_srp_max_rdma_size,
3614 	&srpt_tpg_attrib_attr_srp_max_rsp_size,
3615 	&srpt_tpg_attrib_attr_srp_sq_size,
3616 	&srpt_tpg_attrib_attr_use_srq,
3617 	NULL,
3618 };
3619 
3620 static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr)
3621 {
3622 	struct rdma_cm_id *rdma_cm_id;
3623 	int ret;
3624 
3625 	rdma_cm_id = rdma_create_id(&init_net, srpt_rdma_cm_handler,
3626 				    NULL, RDMA_PS_TCP, IB_QPT_RC);
3627 	if (IS_ERR(rdma_cm_id)) {
3628 		pr_err("RDMA/CM ID creation failed: %ld\n",
3629 		       PTR_ERR(rdma_cm_id));
3630 		goto out;
3631 	}
3632 
3633 	ret = rdma_bind_addr(rdma_cm_id, listen_addr);
3634 	if (ret) {
3635 		char addr_str[64];
3636 
3637 		snprintf(addr_str, sizeof(addr_str), "%pISp", listen_addr);
3638 		pr_err("Binding RDMA/CM ID to address %s failed: %d\n",
3639 		       addr_str, ret);
3640 		rdma_destroy_id(rdma_cm_id);
3641 		rdma_cm_id = ERR_PTR(ret);
3642 		goto out;
3643 	}
3644 
3645 	ret = rdma_listen(rdma_cm_id, 128);
3646 	if (ret) {
3647 		pr_err("rdma_listen() failed: %d\n", ret);
3648 		rdma_destroy_id(rdma_cm_id);
3649 		rdma_cm_id = ERR_PTR(ret);
3650 	}
3651 
3652 out:
3653 	return rdma_cm_id;
3654 }
3655 
3656 static ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page)
3657 {
3658 	return sprintf(page, "%d\n", rdma_cm_port);
3659 }
3660 
3661 static ssize_t srpt_rdma_cm_port_store(struct config_item *item,
3662 				       const char *page, size_t count)
3663 {
3664 	struct sockaddr_in  addr4 = { .sin_family  = AF_INET  };
3665 	struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6 };
3666 	struct rdma_cm_id *new_id = NULL;
3667 	u16 val;
3668 	int ret;
3669 
3670 	ret = kstrtou16(page, 0, &val);
3671 	if (ret < 0)
3672 		return ret;
3673 	ret = count;
3674 	if (rdma_cm_port == val)
3675 		goto out;
3676 
3677 	if (val) {
3678 		addr6.sin6_port = cpu_to_be16(val);
3679 		new_id = srpt_create_rdma_id((struct sockaddr *)&addr6);
3680 		if (IS_ERR(new_id)) {
3681 			addr4.sin_port = cpu_to_be16(val);
3682 			new_id = srpt_create_rdma_id((struct sockaddr *)&addr4);
3683 			if (IS_ERR(new_id)) {
3684 				ret = PTR_ERR(new_id);
3685 				goto out;
3686 			}
3687 		}
3688 	}
3689 
3690 	mutex_lock(&rdma_cm_mutex);
3691 	rdma_cm_port = val;
3692 	swap(rdma_cm_id, new_id);
3693 	mutex_unlock(&rdma_cm_mutex);
3694 
3695 	if (new_id)
3696 		rdma_destroy_id(new_id);
3697 	ret = count;
3698 out:
3699 	return ret;
3700 }
3701 
3702 CONFIGFS_ATTR(srpt_, rdma_cm_port);
3703 
3704 static struct configfs_attribute *srpt_da_attrs[] = {
3705 	&srpt_attr_rdma_cm_port,
3706 	NULL,
3707 };
3708 
3709 static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
3710 {
3711 	struct se_portal_group *se_tpg = to_tpg(item);
3712 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3713 
3714 	return snprintf(page, PAGE_SIZE, "%d\n", sport->enabled);
3715 }
3716 
3717 static ssize_t srpt_tpg_enable_store(struct config_item *item,
3718 		const char *page, size_t count)
3719 {
3720 	struct se_portal_group *se_tpg = to_tpg(item);
3721 	struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
3722 	unsigned long tmp;
3723 	int ret;
3724 
3725 	ret = kstrtoul(page, 0, &tmp);
3726 	if (ret < 0) {
3727 		pr_err("Unable to extract srpt_tpg_store_enable\n");
3728 		return -EINVAL;
3729 	}
3730 
3731 	if ((tmp != 0) && (tmp != 1)) {
3732 		pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
3733 		return -EINVAL;
3734 	}
3735 
3736 	mutex_lock(&sport->mutex);
3737 	srpt_set_enabled(sport, tmp);
3738 	mutex_unlock(&sport->mutex);
3739 
3740 	return count;
3741 }
3742 
3743 CONFIGFS_ATTR(srpt_tpg_, enable);
3744 
3745 static struct configfs_attribute *srpt_tpg_attrs[] = {
3746 	&srpt_tpg_attr_enable,
3747 	NULL,
3748 };
3749 
3750 /**
3751  * srpt_make_tpg - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port/$tpg
3752  * @wwn: Corresponds to $driver/$port.
3753  * @name: $tpg.
3754  */
3755 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
3756 					     const char *name)
3757 {
3758 	struct srpt_port_id *sport_id = srpt_wwn_to_sport_id(wwn);
3759 	struct srpt_tpg *stpg;
3760 	int res = -ENOMEM;
3761 
3762 	stpg = kzalloc(sizeof(*stpg), GFP_KERNEL);
3763 	if (!stpg)
3764 		return ERR_PTR(res);
3765 	stpg->sport_id = sport_id;
3766 	res = core_tpg_register(wwn, &stpg->tpg, SCSI_PROTOCOL_SRP);
3767 	if (res) {
3768 		kfree(stpg);
3769 		return ERR_PTR(res);
3770 	}
3771 
3772 	mutex_lock(&sport_id->mutex);
3773 	list_add_tail(&stpg->entry, &sport_id->tpg_list);
3774 	mutex_unlock(&sport_id->mutex);
3775 
3776 	return &stpg->tpg;
3777 }
3778 
3779 /**
3780  * srpt_drop_tpg - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port/$tpg
3781  * @tpg: Target portal group to deregister.
3782  */
3783 static void srpt_drop_tpg(struct se_portal_group *tpg)
3784 {
3785 	struct srpt_tpg *stpg = container_of(tpg, typeof(*stpg), tpg);
3786 	struct srpt_port_id *sport_id = stpg->sport_id;
3787 	struct srpt_port *sport = srpt_tpg_to_sport(tpg);
3788 
3789 	mutex_lock(&sport_id->mutex);
3790 	list_del(&stpg->entry);
3791 	mutex_unlock(&sport_id->mutex);
3792 
3793 	sport->enabled = false;
3794 	core_tpg_deregister(tpg);
3795 	kfree(stpg);
3796 }
3797 
3798 /**
3799  * srpt_make_tport - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port
3800  * @tf: Not used.
3801  * @group: Not used.
3802  * @name: $port.
3803  */
3804 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
3805 				      struct config_group *group,
3806 				      const char *name)
3807 {
3808 	return srpt_lookup_wwn(name) ? : ERR_PTR(-EINVAL);
3809 }
3810 
3811 /**
3812  * srpt_drop_tport - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port
3813  * @wwn: $port.
3814  */
3815 static void srpt_drop_tport(struct se_wwn *wwn)
3816 {
3817 }
3818 
3819 static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
3820 {
3821 	return scnprintf(buf, PAGE_SIZE, "\n");
3822 }
3823 
3824 CONFIGFS_ATTR_RO(srpt_wwn_, version);
3825 
3826 static struct configfs_attribute *srpt_wwn_attrs[] = {
3827 	&srpt_wwn_attr_version,
3828 	NULL,
3829 };
3830 
3831 static const struct target_core_fabric_ops srpt_template = {
3832 	.module				= THIS_MODULE,
3833 	.fabric_name			= "srpt",
3834 	.tpg_get_wwn			= srpt_get_fabric_wwn,
3835 	.tpg_get_tag			= srpt_get_tag,
3836 	.tpg_check_demo_mode		= srpt_check_false,
3837 	.tpg_check_demo_mode_cache	= srpt_check_true,
3838 	.tpg_check_demo_mode_write_protect = srpt_check_true,
3839 	.tpg_check_prod_mode_write_protect = srpt_check_false,
3840 	.tpg_get_inst_index		= srpt_tpg_get_inst_index,
3841 	.release_cmd			= srpt_release_cmd,
3842 	.check_stop_free		= srpt_check_stop_free,
3843 	.close_session			= srpt_close_session,
3844 	.sess_get_index			= srpt_sess_get_index,
3845 	.sess_get_initiator_sid		= NULL,
3846 	.write_pending			= srpt_write_pending,
3847 	.set_default_node_attributes	= srpt_set_default_node_attrs,
3848 	.get_cmd_state			= srpt_get_tcm_cmd_state,
3849 	.queue_data_in			= srpt_queue_data_in,
3850 	.queue_status			= srpt_queue_status,
3851 	.queue_tm_rsp			= srpt_queue_tm_rsp,
3852 	.aborted_task			= srpt_aborted_task,
3853 	/*
3854 	 * Setup function pointers for generic logic in
3855 	 * target_core_fabric_configfs.c
3856 	 */
3857 	.fabric_make_wwn		= srpt_make_tport,
3858 	.fabric_drop_wwn		= srpt_drop_tport,
3859 	.fabric_make_tpg		= srpt_make_tpg,
3860 	.fabric_drop_tpg		= srpt_drop_tpg,
3861 	.fabric_init_nodeacl		= srpt_init_nodeacl,
3862 
3863 	.tfc_discovery_attrs		= srpt_da_attrs,
3864 	.tfc_wwn_attrs			= srpt_wwn_attrs,
3865 	.tfc_tpg_base_attrs		= srpt_tpg_attrs,
3866 	.tfc_tpg_attrib_attrs		= srpt_tpg_attrib_attrs,
3867 };
3868 
3869 /**
3870  * srpt_init_module - kernel module initialization
3871  *
3872  * Note: Since ib_register_client() registers callback functions, and since at
3873  * least one of these callback functions (srpt_add_one()) calls target core
3874  * functions, this driver must be registered with the target core before
3875  * ib_register_client() is called.
3876  */
3877 static int __init srpt_init_module(void)
3878 {
3879 	int ret;
3880 
3881 	ret = -EINVAL;
3882 	if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
3883 		pr_err("invalid value %d for kernel module parameter srp_max_req_size -- must be at least %d.\n",
3884 		       srp_max_req_size, MIN_MAX_REQ_SIZE);
3885 		goto out;
3886 	}
3887 
3888 	if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
3889 	    || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
3890 		pr_err("invalid value %d for kernel module parameter srpt_srq_size -- must be in the range [%d..%d].\n",
3891 		       srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
3892 		goto out;
3893 	}
3894 
3895 	ret = target_register_template(&srpt_template);
3896 	if (ret)
3897 		goto out;
3898 
3899 	ret = ib_register_client(&srpt_client);
3900 	if (ret) {
3901 		pr_err("couldn't register IB client\n");
3902 		goto out_unregister_target;
3903 	}
3904 
3905 	return 0;
3906 
3907 out_unregister_target:
3908 	target_unregister_template(&srpt_template);
3909 out:
3910 	return ret;
3911 }
3912 
3913 static void __exit srpt_cleanup_module(void)
3914 {
3915 	if (rdma_cm_id)
3916 		rdma_destroy_id(rdma_cm_id);
3917 	ib_unregister_client(&srpt_client);
3918 	target_unregister_template(&srpt_template);
3919 }
3920 
3921 module_init(srpt_init_module);
3922 module_exit(srpt_cleanup_module);
3923