xref: /linux/drivers/infiniband/ulp/srp/ib_srp.c (revision 62154b2e892807b8373a107c277f1fa8338f4333)
1aef9ec39SRoland Dreier /*
2aef9ec39SRoland Dreier  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
3aef9ec39SRoland Dreier  *
4aef9ec39SRoland Dreier  * This software is available to you under a choice of one of two
5aef9ec39SRoland Dreier  * licenses.  You may choose to be licensed under the terms of the GNU
6aef9ec39SRoland Dreier  * General Public License (GPL) Version 2, available from the file
7aef9ec39SRoland Dreier  * COPYING in the main directory of this source tree, or the
8aef9ec39SRoland Dreier  * OpenIB.org BSD license below:
9aef9ec39SRoland Dreier  *
10aef9ec39SRoland Dreier  *     Redistribution and use in source and binary forms, with or
11aef9ec39SRoland Dreier  *     without modification, are permitted provided that the following
12aef9ec39SRoland Dreier  *     conditions are met:
13aef9ec39SRoland Dreier  *
14aef9ec39SRoland Dreier  *      - Redistributions of source code must retain the above
15aef9ec39SRoland Dreier  *        copyright notice, this list of conditions and the following
16aef9ec39SRoland Dreier  *        disclaimer.
17aef9ec39SRoland Dreier  *
18aef9ec39SRoland Dreier  *      - Redistributions in binary form must reproduce the above
19aef9ec39SRoland Dreier  *        copyright notice, this list of conditions and the following
20aef9ec39SRoland Dreier  *        disclaimer in the documentation and/or other materials
21aef9ec39SRoland Dreier  *        provided with the distribution.
22aef9ec39SRoland Dreier  *
23aef9ec39SRoland Dreier  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24aef9ec39SRoland Dreier  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25aef9ec39SRoland Dreier  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26aef9ec39SRoland Dreier  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27aef9ec39SRoland Dreier  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28aef9ec39SRoland Dreier  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29aef9ec39SRoland Dreier  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30aef9ec39SRoland Dreier  * SOFTWARE.
31aef9ec39SRoland Dreier  */
32aef9ec39SRoland Dreier 
33e0bda7d8SBart Van Assche #define pr_fmt(fmt) PFX fmt
34e0bda7d8SBart Van Assche 
35aef9ec39SRoland Dreier #include <linux/module.h>
36aef9ec39SRoland Dreier #include <linux/init.h>
37aef9ec39SRoland Dreier #include <linux/slab.h>
38aef9ec39SRoland Dreier #include <linux/err.h>
39aef9ec39SRoland Dreier #include <linux/string.h>
40aef9ec39SRoland Dreier #include <linux/parser.h>
41aef9ec39SRoland Dreier #include <linux/random.h>
42de25968cSTim Schmielau #include <linux/jiffies.h>
43aef9ec39SRoland Dreier 
4460063497SArun Sharma #include <linux/atomic.h>
45aef9ec39SRoland Dreier 
46aef9ec39SRoland Dreier #include <scsi/scsi.h>
47aef9ec39SRoland Dreier #include <scsi/scsi_device.h>
48aef9ec39SRoland Dreier #include <scsi/scsi_dbg.h>
4971444b97SJack Wang #include <scsi/scsi_tcq.h>
50aef9ec39SRoland Dreier #include <scsi/srp.h>
513236822bSFUJITA Tomonori #include <scsi/scsi_transport_srp.h>
52aef9ec39SRoland Dreier 
53aef9ec39SRoland Dreier #include "ib_srp.h"
54aef9ec39SRoland Dreier 
55aef9ec39SRoland Dreier #define DRV_NAME	"ib_srp"
56aef9ec39SRoland Dreier #define PFX		DRV_NAME ": "
57e8ca4135SVu Pham #define DRV_VERSION	"1.0"
58e8ca4135SVu Pham #define DRV_RELDATE	"July 1, 2013"
59aef9ec39SRoland Dreier 
60aef9ec39SRoland Dreier MODULE_AUTHOR("Roland Dreier");
61aef9ec39SRoland Dreier MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
62aef9ec39SRoland Dreier 		   "v" DRV_VERSION " (" DRV_RELDATE ")");
63aef9ec39SRoland Dreier MODULE_LICENSE("Dual BSD/GPL");
64aef9ec39SRoland Dreier 
6549248644SDavid Dillow static unsigned int srp_sg_tablesize;
6649248644SDavid Dillow static unsigned int cmd_sg_entries;
67c07d424dSDavid Dillow static unsigned int indirect_sg_entries;
68c07d424dSDavid Dillow static bool allow_ext_sg;
69aef9ec39SRoland Dreier static int topspin_workarounds = 1;
70aef9ec39SRoland Dreier 
7149248644SDavid Dillow module_param(srp_sg_tablesize, uint, 0444);
7249248644SDavid Dillow MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
7349248644SDavid Dillow 
7449248644SDavid Dillow module_param(cmd_sg_entries, uint, 0444);
7549248644SDavid Dillow MODULE_PARM_DESC(cmd_sg_entries,
7649248644SDavid Dillow 		 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
7749248644SDavid Dillow 
78c07d424dSDavid Dillow module_param(indirect_sg_entries, uint, 0444);
79c07d424dSDavid Dillow MODULE_PARM_DESC(indirect_sg_entries,
80c07d424dSDavid Dillow 		 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
81c07d424dSDavid Dillow 
82c07d424dSDavid Dillow module_param(allow_ext_sg, bool, 0444);
83c07d424dSDavid Dillow MODULE_PARM_DESC(allow_ext_sg,
84c07d424dSDavid Dillow 		  "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
85c07d424dSDavid Dillow 
86aef9ec39SRoland Dreier module_param(topspin_workarounds, int, 0444);
87aef9ec39SRoland Dreier MODULE_PARM_DESC(topspin_workarounds,
88aef9ec39SRoland Dreier 		 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
89aef9ec39SRoland Dreier 
90ed9b2264SBart Van Assche static struct kernel_param_ops srp_tmo_ops;
91ed9b2264SBart Van Assche 
92a95cadb9SBart Van Assche static int srp_reconnect_delay = 10;
93a95cadb9SBart Van Assche module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
94a95cadb9SBart Van Assche 		S_IRUGO | S_IWUSR);
95a95cadb9SBart Van Assche MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
96a95cadb9SBart Van Assche 
97ed9b2264SBart Van Assche static int srp_fast_io_fail_tmo = 15;
98ed9b2264SBart Van Assche module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
99ed9b2264SBart Van Assche 		S_IRUGO | S_IWUSR);
100ed9b2264SBart Van Assche MODULE_PARM_DESC(fast_io_fail_tmo,
101ed9b2264SBart Van Assche 		 "Number of seconds between the observation of a transport"
102ed9b2264SBart Van Assche 		 " layer error and failing all I/O. \"off\" means that this"
103ed9b2264SBart Van Assche 		 " functionality is disabled.");
104ed9b2264SBart Van Assche 
105a95cadb9SBart Van Assche static int srp_dev_loss_tmo = 600;
106ed9b2264SBart Van Assche module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
107ed9b2264SBart Van Assche 		S_IRUGO | S_IWUSR);
108ed9b2264SBart Van Assche MODULE_PARM_DESC(dev_loss_tmo,
109ed9b2264SBart Van Assche 		 "Maximum number of seconds that the SRP transport should"
110ed9b2264SBart Van Assche 		 " insulate transport layer errors. After this time has been"
111ed9b2264SBart Van Assche 		 " exceeded the SCSI host is removed. Should be"
112ed9b2264SBart Van Assche 		 " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
113ed9b2264SBart Van Assche 		 " if fast_io_fail_tmo has not been set. \"off\" means that"
114ed9b2264SBart Van Assche 		 " this functionality is disabled.");
115ed9b2264SBart Van Assche 
116aef9ec39SRoland Dreier static void srp_add_one(struct ib_device *device);
117aef9ec39SRoland Dreier static void srp_remove_one(struct ib_device *device);
1189c03dc9fSBart Van Assche static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
1199c03dc9fSBart Van Assche static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
120aef9ec39SRoland Dreier static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
121aef9ec39SRoland Dreier 
1223236822bSFUJITA Tomonori static struct scsi_transport_template *ib_srp_transport_template;
1233236822bSFUJITA Tomonori 
124aef9ec39SRoland Dreier static struct ib_client srp_client = {
125aef9ec39SRoland Dreier 	.name   = "srp",
126aef9ec39SRoland Dreier 	.add    = srp_add_one,
127aef9ec39SRoland Dreier 	.remove = srp_remove_one
128aef9ec39SRoland Dreier };
129aef9ec39SRoland Dreier 
130c1a0b23bSMichael S. Tsirkin static struct ib_sa_client srp_sa_client;
131c1a0b23bSMichael S. Tsirkin 
132ed9b2264SBart Van Assche static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
133ed9b2264SBart Van Assche {
134ed9b2264SBart Van Assche 	int tmo = *(int *)kp->arg;
135ed9b2264SBart Van Assche 
136ed9b2264SBart Van Assche 	if (tmo >= 0)
137ed9b2264SBart Van Assche 		return sprintf(buffer, "%d", tmo);
138ed9b2264SBart Van Assche 	else
139ed9b2264SBart Van Assche 		return sprintf(buffer, "off");
140ed9b2264SBart Van Assche }
141ed9b2264SBart Van Assche 
142ed9b2264SBart Van Assche static int srp_tmo_set(const char *val, const struct kernel_param *kp)
143ed9b2264SBart Van Assche {
144ed9b2264SBart Van Assche 	int tmo, res;
145ed9b2264SBart Van Assche 
146ed9b2264SBart Van Assche 	if (strncmp(val, "off", 3) != 0) {
147ed9b2264SBart Van Assche 		res = kstrtoint(val, 0, &tmo);
148ed9b2264SBart Van Assche 		if (res)
149ed9b2264SBart Van Assche 			goto out;
150ed9b2264SBart Van Assche 	} else {
151ed9b2264SBart Van Assche 		tmo = -1;
152ed9b2264SBart Van Assche 	}
153a95cadb9SBart Van Assche 	if (kp->arg == &srp_reconnect_delay)
154a95cadb9SBart Van Assche 		res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
155a95cadb9SBart Van Assche 				    srp_dev_loss_tmo);
156a95cadb9SBart Van Assche 	else if (kp->arg == &srp_fast_io_fail_tmo)
157a95cadb9SBart Van Assche 		res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
158ed9b2264SBart Van Assche 	else
159a95cadb9SBart Van Assche 		res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
160a95cadb9SBart Van Assche 				    tmo);
161ed9b2264SBart Van Assche 	if (res)
162ed9b2264SBart Van Assche 		goto out;
163ed9b2264SBart Van Assche 	*(int *)kp->arg = tmo;
164ed9b2264SBart Van Assche 
165ed9b2264SBart Van Assche out:
166ed9b2264SBart Van Assche 	return res;
167ed9b2264SBart Van Assche }
168ed9b2264SBart Van Assche 
169ed9b2264SBart Van Assche static struct kernel_param_ops srp_tmo_ops = {
170ed9b2264SBart Van Assche 	.get = srp_tmo_get,
171ed9b2264SBart Van Assche 	.set = srp_tmo_set,
172ed9b2264SBart Van Assche };
173ed9b2264SBart Van Assche 
174aef9ec39SRoland Dreier static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
175aef9ec39SRoland Dreier {
176aef9ec39SRoland Dreier 	return (struct srp_target_port *) host->hostdata;
177aef9ec39SRoland Dreier }
178aef9ec39SRoland Dreier 
179aef9ec39SRoland Dreier static const char *srp_target_info(struct Scsi_Host *host)
180aef9ec39SRoland Dreier {
181aef9ec39SRoland Dreier 	return host_to_target(host)->target_name;
182aef9ec39SRoland Dreier }
183aef9ec39SRoland Dreier 
1845d7cbfd6SRoland Dreier static int srp_target_is_topspin(struct srp_target_port *target)
1855d7cbfd6SRoland Dreier {
1865d7cbfd6SRoland Dreier 	static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
1873d1ff48dSRaghava Kondapalli 	static const u8 cisco_oui[3]   = { 0x00, 0x1b, 0x0d };
1885d7cbfd6SRoland Dreier 
1895d7cbfd6SRoland Dreier 	return topspin_workarounds &&
1903d1ff48dSRaghava Kondapalli 		(!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
1913d1ff48dSRaghava Kondapalli 		 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
1925d7cbfd6SRoland Dreier }
1935d7cbfd6SRoland Dreier 
194aef9ec39SRoland Dreier static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
195aef9ec39SRoland Dreier 				   gfp_t gfp_mask,
196aef9ec39SRoland Dreier 				   enum dma_data_direction direction)
197aef9ec39SRoland Dreier {
198aef9ec39SRoland Dreier 	struct srp_iu *iu;
199aef9ec39SRoland Dreier 
200aef9ec39SRoland Dreier 	iu = kmalloc(sizeof *iu, gfp_mask);
201aef9ec39SRoland Dreier 	if (!iu)
202aef9ec39SRoland Dreier 		goto out;
203aef9ec39SRoland Dreier 
204aef9ec39SRoland Dreier 	iu->buf = kzalloc(size, gfp_mask);
205aef9ec39SRoland Dreier 	if (!iu->buf)
206aef9ec39SRoland Dreier 		goto out_free_iu;
207aef9ec39SRoland Dreier 
20805321937SGreg Kroah-Hartman 	iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
20905321937SGreg Kroah-Hartman 				    direction);
21005321937SGreg Kroah-Hartman 	if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
211aef9ec39SRoland Dreier 		goto out_free_buf;
212aef9ec39SRoland Dreier 
213aef9ec39SRoland Dreier 	iu->size      = size;
214aef9ec39SRoland Dreier 	iu->direction = direction;
215aef9ec39SRoland Dreier 
216aef9ec39SRoland Dreier 	return iu;
217aef9ec39SRoland Dreier 
218aef9ec39SRoland Dreier out_free_buf:
219aef9ec39SRoland Dreier 	kfree(iu->buf);
220aef9ec39SRoland Dreier out_free_iu:
221aef9ec39SRoland Dreier 	kfree(iu);
222aef9ec39SRoland Dreier out:
223aef9ec39SRoland Dreier 	return NULL;
224aef9ec39SRoland Dreier }
225aef9ec39SRoland Dreier 
226aef9ec39SRoland Dreier static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
227aef9ec39SRoland Dreier {
228aef9ec39SRoland Dreier 	if (!iu)
229aef9ec39SRoland Dreier 		return;
230aef9ec39SRoland Dreier 
23105321937SGreg Kroah-Hartman 	ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
23205321937SGreg Kroah-Hartman 			    iu->direction);
233aef9ec39SRoland Dreier 	kfree(iu->buf);
234aef9ec39SRoland Dreier 	kfree(iu);
235aef9ec39SRoland Dreier }
236aef9ec39SRoland Dreier 
237aef9ec39SRoland Dreier static void srp_qp_event(struct ib_event *event, void *context)
238aef9ec39SRoland Dreier {
239e0bda7d8SBart Van Assche 	pr_debug("QP event %d\n", event->event);
240aef9ec39SRoland Dreier }
241aef9ec39SRoland Dreier 
242aef9ec39SRoland Dreier static int srp_init_qp(struct srp_target_port *target,
243aef9ec39SRoland Dreier 		       struct ib_qp *qp)
244aef9ec39SRoland Dreier {
245aef9ec39SRoland Dreier 	struct ib_qp_attr *attr;
246aef9ec39SRoland Dreier 	int ret;
247aef9ec39SRoland Dreier 
248aef9ec39SRoland Dreier 	attr = kmalloc(sizeof *attr, GFP_KERNEL);
249aef9ec39SRoland Dreier 	if (!attr)
250aef9ec39SRoland Dreier 		return -ENOMEM;
251aef9ec39SRoland Dreier 
252969a60f9SRoland Dreier 	ret = ib_find_pkey(target->srp_host->srp_dev->dev,
253aef9ec39SRoland Dreier 			   target->srp_host->port,
254aef9ec39SRoland Dreier 			   be16_to_cpu(target->path.pkey),
255aef9ec39SRoland Dreier 			   &attr->pkey_index);
256aef9ec39SRoland Dreier 	if (ret)
257aef9ec39SRoland Dreier 		goto out;
258aef9ec39SRoland Dreier 
259aef9ec39SRoland Dreier 	attr->qp_state        = IB_QPS_INIT;
260aef9ec39SRoland Dreier 	attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
261aef9ec39SRoland Dreier 				    IB_ACCESS_REMOTE_WRITE);
262aef9ec39SRoland Dreier 	attr->port_num        = target->srp_host->port;
263aef9ec39SRoland Dreier 
264aef9ec39SRoland Dreier 	ret = ib_modify_qp(qp, attr,
265aef9ec39SRoland Dreier 			   IB_QP_STATE		|
266aef9ec39SRoland Dreier 			   IB_QP_PKEY_INDEX	|
267aef9ec39SRoland Dreier 			   IB_QP_ACCESS_FLAGS	|
268aef9ec39SRoland Dreier 			   IB_QP_PORT);
269aef9ec39SRoland Dreier 
270aef9ec39SRoland Dreier out:
271aef9ec39SRoland Dreier 	kfree(attr);
272aef9ec39SRoland Dreier 	return ret;
273aef9ec39SRoland Dreier }
274aef9ec39SRoland Dreier 
2759fe4bcf4SDavid Dillow static int srp_new_cm_id(struct srp_target_port *target)
2769fe4bcf4SDavid Dillow {
2779fe4bcf4SDavid Dillow 	struct ib_cm_id *new_cm_id;
2789fe4bcf4SDavid Dillow 
27905321937SGreg Kroah-Hartman 	new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
2809fe4bcf4SDavid Dillow 				    srp_cm_handler, target);
2819fe4bcf4SDavid Dillow 	if (IS_ERR(new_cm_id))
2829fe4bcf4SDavid Dillow 		return PTR_ERR(new_cm_id);
2839fe4bcf4SDavid Dillow 
2849fe4bcf4SDavid Dillow 	if (target->cm_id)
2859fe4bcf4SDavid Dillow 		ib_destroy_cm_id(target->cm_id);
2869fe4bcf4SDavid Dillow 	target->cm_id = new_cm_id;
2879fe4bcf4SDavid Dillow 
2889fe4bcf4SDavid Dillow 	return 0;
2899fe4bcf4SDavid Dillow }
2909fe4bcf4SDavid Dillow 
291aef9ec39SRoland Dreier static int srp_create_target_ib(struct srp_target_port *target)
292aef9ec39SRoland Dreier {
293*62154b2eSBart Van Assche 	struct srp_device *dev = target->srp_host->srp_dev;
294aef9ec39SRoland Dreier 	struct ib_qp_init_attr *init_attr;
29573aa89edSIshai Rabinovitz 	struct ib_cq *recv_cq, *send_cq;
29673aa89edSIshai Rabinovitz 	struct ib_qp *qp;
297aef9ec39SRoland Dreier 	int ret;
298aef9ec39SRoland Dreier 
299aef9ec39SRoland Dreier 	init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
300aef9ec39SRoland Dreier 	if (!init_attr)
301aef9ec39SRoland Dreier 		return -ENOMEM;
302aef9ec39SRoland Dreier 
303*62154b2eSBart Van Assche 	recv_cq = ib_create_cq(dev->dev, srp_recv_completion, NULL, target,
3044d73f95fSBart Van Assche 			       target->queue_size, target->comp_vector);
30573aa89edSIshai Rabinovitz 	if (IS_ERR(recv_cq)) {
30673aa89edSIshai Rabinovitz 		ret = PTR_ERR(recv_cq);
307da9d2f07SRoland Dreier 		goto err;
308aef9ec39SRoland Dreier 	}
309aef9ec39SRoland Dreier 
310*62154b2eSBart Van Assche 	send_cq = ib_create_cq(dev->dev, srp_send_completion, NULL, target,
3114d73f95fSBart Van Assche 			       target->queue_size, target->comp_vector);
31273aa89edSIshai Rabinovitz 	if (IS_ERR(send_cq)) {
31373aa89edSIshai Rabinovitz 		ret = PTR_ERR(send_cq);
314da9d2f07SRoland Dreier 		goto err_recv_cq;
3159c03dc9fSBart Van Assche 	}
3169c03dc9fSBart Van Assche 
31773aa89edSIshai Rabinovitz 	ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
318aef9ec39SRoland Dreier 
319aef9ec39SRoland Dreier 	init_attr->event_handler       = srp_qp_event;
3204d73f95fSBart Van Assche 	init_attr->cap.max_send_wr     = target->queue_size;
3214d73f95fSBart Van Assche 	init_attr->cap.max_recv_wr     = target->queue_size;
322aef9ec39SRoland Dreier 	init_attr->cap.max_recv_sge    = 1;
323aef9ec39SRoland Dreier 	init_attr->cap.max_send_sge    = 1;
324aef9ec39SRoland Dreier 	init_attr->sq_sig_type         = IB_SIGNAL_ALL_WR;
325aef9ec39SRoland Dreier 	init_attr->qp_type             = IB_QPT_RC;
32673aa89edSIshai Rabinovitz 	init_attr->send_cq             = send_cq;
32773aa89edSIshai Rabinovitz 	init_attr->recv_cq             = recv_cq;
328aef9ec39SRoland Dreier 
329*62154b2eSBart Van Assche 	qp = ib_create_qp(dev->pd, init_attr);
33073aa89edSIshai Rabinovitz 	if (IS_ERR(qp)) {
33173aa89edSIshai Rabinovitz 		ret = PTR_ERR(qp);
332da9d2f07SRoland Dreier 		goto err_send_cq;
333aef9ec39SRoland Dreier 	}
334aef9ec39SRoland Dreier 
33573aa89edSIshai Rabinovitz 	ret = srp_init_qp(target, qp);
336da9d2f07SRoland Dreier 	if (ret)
337da9d2f07SRoland Dreier 		goto err_qp;
338aef9ec39SRoland Dreier 
33973aa89edSIshai Rabinovitz 	if (target->qp)
34073aa89edSIshai Rabinovitz 		ib_destroy_qp(target->qp);
34173aa89edSIshai Rabinovitz 	if (target->recv_cq)
34273aa89edSIshai Rabinovitz 		ib_destroy_cq(target->recv_cq);
34373aa89edSIshai Rabinovitz 	if (target->send_cq)
34473aa89edSIshai Rabinovitz 		ib_destroy_cq(target->send_cq);
34573aa89edSIshai Rabinovitz 
34673aa89edSIshai Rabinovitz 	target->qp = qp;
34773aa89edSIshai Rabinovitz 	target->recv_cq = recv_cq;
34873aa89edSIshai Rabinovitz 	target->send_cq = send_cq;
34973aa89edSIshai Rabinovitz 
350da9d2f07SRoland Dreier 	kfree(init_attr);
351da9d2f07SRoland Dreier 	return 0;
352da9d2f07SRoland Dreier 
353da9d2f07SRoland Dreier err_qp:
35473aa89edSIshai Rabinovitz 	ib_destroy_qp(qp);
355da9d2f07SRoland Dreier 
356da9d2f07SRoland Dreier err_send_cq:
35773aa89edSIshai Rabinovitz 	ib_destroy_cq(send_cq);
358da9d2f07SRoland Dreier 
359da9d2f07SRoland Dreier err_recv_cq:
36073aa89edSIshai Rabinovitz 	ib_destroy_cq(recv_cq);
361da9d2f07SRoland Dreier 
362da9d2f07SRoland Dreier err:
363aef9ec39SRoland Dreier 	kfree(init_attr);
364aef9ec39SRoland Dreier 	return ret;
365aef9ec39SRoland Dreier }
366aef9ec39SRoland Dreier 
3674d73f95fSBart Van Assche /*
3684d73f95fSBart Van Assche  * Note: this function may be called without srp_alloc_iu_bufs() having been
3694d73f95fSBart Van Assche  * invoked. Hence the target->[rt]x_ring checks.
3704d73f95fSBart Van Assche  */
371aef9ec39SRoland Dreier static void srp_free_target_ib(struct srp_target_port *target)
372aef9ec39SRoland Dreier {
373aef9ec39SRoland Dreier 	int i;
374aef9ec39SRoland Dreier 
375aef9ec39SRoland Dreier 	ib_destroy_qp(target->qp);
3769c03dc9fSBart Van Assche 	ib_destroy_cq(target->send_cq);
3779c03dc9fSBart Van Assche 	ib_destroy_cq(target->recv_cq);
378aef9ec39SRoland Dreier 
37973aa89edSIshai Rabinovitz 	target->qp = NULL;
38073aa89edSIshai Rabinovitz 	target->send_cq = target->recv_cq = NULL;
38173aa89edSIshai Rabinovitz 
3824d73f95fSBart Van Assche 	if (target->rx_ring) {
3834d73f95fSBart Van Assche 		for (i = 0; i < target->queue_size; ++i)
384aef9ec39SRoland Dreier 			srp_free_iu(target->srp_host, target->rx_ring[i]);
3854d73f95fSBart Van Assche 		kfree(target->rx_ring);
3864d73f95fSBart Van Assche 		target->rx_ring = NULL;
3874d73f95fSBart Van Assche 	}
3884d73f95fSBart Van Assche 	if (target->tx_ring) {
3894d73f95fSBart Van Assche 		for (i = 0; i < target->queue_size; ++i)
390aef9ec39SRoland Dreier 			srp_free_iu(target->srp_host, target->tx_ring[i]);
3914d73f95fSBart Van Assche 		kfree(target->tx_ring);
3924d73f95fSBart Van Assche 		target->tx_ring = NULL;
3934d73f95fSBart Van Assche 	}
394aef9ec39SRoland Dreier }
395aef9ec39SRoland Dreier 
396aef9ec39SRoland Dreier static void srp_path_rec_completion(int status,
397aef9ec39SRoland Dreier 				    struct ib_sa_path_rec *pathrec,
398aef9ec39SRoland Dreier 				    void *target_ptr)
399aef9ec39SRoland Dreier {
400aef9ec39SRoland Dreier 	struct srp_target_port *target = target_ptr;
401aef9ec39SRoland Dreier 
402aef9ec39SRoland Dreier 	target->status = status;
403aef9ec39SRoland Dreier 	if (status)
4047aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
4057aa54bd7SDavid Dillow 			     PFX "Got failed path rec status %d\n", status);
406aef9ec39SRoland Dreier 	else
407aef9ec39SRoland Dreier 		target->path = *pathrec;
408aef9ec39SRoland Dreier 	complete(&target->done);
409aef9ec39SRoland Dreier }
410aef9ec39SRoland Dreier 
411aef9ec39SRoland Dreier static int srp_lookup_path(struct srp_target_port *target)
412aef9ec39SRoland Dreier {
413a702adceSBart Van Assche 	int ret;
414a702adceSBart Van Assche 
415aef9ec39SRoland Dreier 	target->path.numb_path = 1;
416aef9ec39SRoland Dreier 
417aef9ec39SRoland Dreier 	init_completion(&target->done);
418aef9ec39SRoland Dreier 
419c1a0b23bSMichael S. Tsirkin 	target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
42005321937SGreg Kroah-Hartman 						   target->srp_host->srp_dev->dev,
421aef9ec39SRoland Dreier 						   target->srp_host->port,
422aef9ec39SRoland Dreier 						   &target->path,
423247e020eSSean Hefty 						   IB_SA_PATH_REC_SERVICE_ID	|
424aef9ec39SRoland Dreier 						   IB_SA_PATH_REC_DGID		|
425aef9ec39SRoland Dreier 						   IB_SA_PATH_REC_SGID		|
426aef9ec39SRoland Dreier 						   IB_SA_PATH_REC_NUMB_PATH	|
427aef9ec39SRoland Dreier 						   IB_SA_PATH_REC_PKEY,
428aef9ec39SRoland Dreier 						   SRP_PATH_REC_TIMEOUT_MS,
429aef9ec39SRoland Dreier 						   GFP_KERNEL,
430aef9ec39SRoland Dreier 						   srp_path_rec_completion,
431aef9ec39SRoland Dreier 						   target, &target->path_query);
432aef9ec39SRoland Dreier 	if (target->path_query_id < 0)
433aef9ec39SRoland Dreier 		return target->path_query_id;
434aef9ec39SRoland Dreier 
435a702adceSBart Van Assche 	ret = wait_for_completion_interruptible(&target->done);
436a702adceSBart Van Assche 	if (ret < 0)
437a702adceSBart Van Assche 		return ret;
438aef9ec39SRoland Dreier 
439aef9ec39SRoland Dreier 	if (target->status < 0)
4407aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
4417aa54bd7SDavid Dillow 			     PFX "Path record query failed\n");
442aef9ec39SRoland Dreier 
443aef9ec39SRoland Dreier 	return target->status;
444aef9ec39SRoland Dreier }
445aef9ec39SRoland Dreier 
446aef9ec39SRoland Dreier static int srp_send_req(struct srp_target_port *target)
447aef9ec39SRoland Dreier {
448aef9ec39SRoland Dreier 	struct {
449aef9ec39SRoland Dreier 		struct ib_cm_req_param param;
450aef9ec39SRoland Dreier 		struct srp_login_req   priv;
451aef9ec39SRoland Dreier 	} *req = NULL;
452aef9ec39SRoland Dreier 	int status;
453aef9ec39SRoland Dreier 
454aef9ec39SRoland Dreier 	req = kzalloc(sizeof *req, GFP_KERNEL);
455aef9ec39SRoland Dreier 	if (!req)
456aef9ec39SRoland Dreier 		return -ENOMEM;
457aef9ec39SRoland Dreier 
458aef9ec39SRoland Dreier 	req->param.primary_path 	      = &target->path;
459aef9ec39SRoland Dreier 	req->param.alternate_path 	      = NULL;
460aef9ec39SRoland Dreier 	req->param.service_id 		      = target->service_id;
461aef9ec39SRoland Dreier 	req->param.qp_num 		      = target->qp->qp_num;
462aef9ec39SRoland Dreier 	req->param.qp_type 		      = target->qp->qp_type;
463aef9ec39SRoland Dreier 	req->param.private_data 	      = &req->priv;
464aef9ec39SRoland Dreier 	req->param.private_data_len 	      = sizeof req->priv;
465aef9ec39SRoland Dreier 	req->param.flow_control 	      = 1;
466aef9ec39SRoland Dreier 
467aef9ec39SRoland Dreier 	get_random_bytes(&req->param.starting_psn, 4);
468aef9ec39SRoland Dreier 	req->param.starting_psn 	     &= 0xffffff;
469aef9ec39SRoland Dreier 
470aef9ec39SRoland Dreier 	/*
471aef9ec39SRoland Dreier 	 * Pick some arbitrary defaults here; we could make these
472aef9ec39SRoland Dreier 	 * module parameters if anyone cared about setting them.
473aef9ec39SRoland Dreier 	 */
474aef9ec39SRoland Dreier 	req->param.responder_resources	      = 4;
475aef9ec39SRoland Dreier 	req->param.remote_cm_response_timeout = 20;
476aef9ec39SRoland Dreier 	req->param.local_cm_response_timeout  = 20;
4777bb312e4SVu Pham 	req->param.retry_count                = target->tl_retry_count;
478aef9ec39SRoland Dreier 	req->param.rnr_retry_count 	      = 7;
479aef9ec39SRoland Dreier 	req->param.max_cm_retries 	      = 15;
480aef9ec39SRoland Dreier 
481aef9ec39SRoland Dreier 	req->priv.opcode     	= SRP_LOGIN_REQ;
482aef9ec39SRoland Dreier 	req->priv.tag        	= 0;
48349248644SDavid Dillow 	req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
484aef9ec39SRoland Dreier 	req->priv.req_buf_fmt 	= cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
485aef9ec39SRoland Dreier 					      SRP_BUF_FORMAT_INDIRECT);
4860c0450dbSRamachandra K 	/*
4870c0450dbSRamachandra K 	 * In the published SRP specification (draft rev. 16a), the
4880c0450dbSRamachandra K 	 * port identifier format is 8 bytes of ID extension followed
4890c0450dbSRamachandra K 	 * by 8 bytes of GUID.  Older drafts put the two halves in the
4900c0450dbSRamachandra K 	 * opposite order, so that the GUID comes first.
4910c0450dbSRamachandra K 	 *
4920c0450dbSRamachandra K 	 * Targets conforming to these obsolete drafts can be
4930c0450dbSRamachandra K 	 * recognized by the I/O Class they report.
4940c0450dbSRamachandra K 	 */
4950c0450dbSRamachandra K 	if (target->io_class == SRP_REV10_IB_IO_CLASS) {
4960c0450dbSRamachandra K 		memcpy(req->priv.initiator_port_id,
49701cb9bcbSIshai Rabinovitz 		       &target->path.sgid.global.interface_id, 8);
4980c0450dbSRamachandra K 		memcpy(req->priv.initiator_port_id + 8,
49901cb9bcbSIshai Rabinovitz 		       &target->initiator_ext, 8);
5000c0450dbSRamachandra K 		memcpy(req->priv.target_port_id,     &target->ioc_guid, 8);
5010c0450dbSRamachandra K 		memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
5020c0450dbSRamachandra K 	} else {
5030c0450dbSRamachandra K 		memcpy(req->priv.initiator_port_id,
50401cb9bcbSIshai Rabinovitz 		       &target->initiator_ext, 8);
50501cb9bcbSIshai Rabinovitz 		memcpy(req->priv.initiator_port_id + 8,
50601cb9bcbSIshai Rabinovitz 		       &target->path.sgid.global.interface_id, 8);
5070c0450dbSRamachandra K 		memcpy(req->priv.target_port_id,     &target->id_ext, 8);
5080c0450dbSRamachandra K 		memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
5090c0450dbSRamachandra K 	}
5100c0450dbSRamachandra K 
511aef9ec39SRoland Dreier 	/*
512aef9ec39SRoland Dreier 	 * Topspin/Cisco SRP targets will reject our login unless we
51301cb9bcbSIshai Rabinovitz 	 * zero out the first 8 bytes of our initiator port ID and set
51401cb9bcbSIshai Rabinovitz 	 * the second 8 bytes to the local node GUID.
515aef9ec39SRoland Dreier 	 */
5165d7cbfd6SRoland Dreier 	if (srp_target_is_topspin(target)) {
5177aa54bd7SDavid Dillow 		shost_printk(KERN_DEBUG, target->scsi_host,
5187aa54bd7SDavid Dillow 			     PFX "Topspin/Cisco initiator port ID workaround "
519aef9ec39SRoland Dreier 			     "activated for target GUID %016llx\n",
520aef9ec39SRoland Dreier 			     (unsigned long long) be64_to_cpu(target->ioc_guid));
521aef9ec39SRoland Dreier 		memset(req->priv.initiator_port_id, 0, 8);
52201cb9bcbSIshai Rabinovitz 		memcpy(req->priv.initiator_port_id + 8,
52305321937SGreg Kroah-Hartman 		       &target->srp_host->srp_dev->dev->node_guid, 8);
524aef9ec39SRoland Dreier 	}
525aef9ec39SRoland Dreier 
526aef9ec39SRoland Dreier 	status = ib_send_cm_req(target->cm_id, &req->param);
527aef9ec39SRoland Dreier 
528aef9ec39SRoland Dreier 	kfree(req);
529aef9ec39SRoland Dreier 
530aef9ec39SRoland Dreier 	return status;
531aef9ec39SRoland Dreier }
532aef9ec39SRoland Dreier 
533ef6c49d8SBart Van Assche static bool srp_queue_remove_work(struct srp_target_port *target)
534ef6c49d8SBart Van Assche {
535ef6c49d8SBart Van Assche 	bool changed = false;
536ef6c49d8SBart Van Assche 
537ef6c49d8SBart Van Assche 	spin_lock_irq(&target->lock);
538ef6c49d8SBart Van Assche 	if (target->state != SRP_TARGET_REMOVED) {
539ef6c49d8SBart Van Assche 		target->state = SRP_TARGET_REMOVED;
540ef6c49d8SBart Van Assche 		changed = true;
541ef6c49d8SBart Van Assche 	}
542ef6c49d8SBart Van Assche 	spin_unlock_irq(&target->lock);
543ef6c49d8SBart Van Assche 
544ef6c49d8SBart Van Assche 	if (changed)
545ef6c49d8SBart Van Assche 		queue_work(system_long_wq, &target->remove_work);
546ef6c49d8SBart Van Assche 
547ef6c49d8SBart Van Assche 	return changed;
548ef6c49d8SBart Van Assche }
549ef6c49d8SBart Van Assche 
550294c875aSBart Van Assche static bool srp_change_conn_state(struct srp_target_port *target,
551294c875aSBart Van Assche 				  bool connected)
552294c875aSBart Van Assche {
553294c875aSBart Van Assche 	bool changed = false;
554294c875aSBart Van Assche 
555294c875aSBart Van Assche 	spin_lock_irq(&target->lock);
556294c875aSBart Van Assche 	if (target->connected != connected) {
557294c875aSBart Van Assche 		target->connected = connected;
558294c875aSBart Van Assche 		changed = true;
559294c875aSBart Van Assche 	}
560294c875aSBart Van Assche 	spin_unlock_irq(&target->lock);
561294c875aSBart Van Assche 
562294c875aSBart Van Assche 	return changed;
563294c875aSBart Van Assche }
564294c875aSBart Van Assche 
565aef9ec39SRoland Dreier static void srp_disconnect_target(struct srp_target_port *target)
566aef9ec39SRoland Dreier {
567294c875aSBart Van Assche 	if (srp_change_conn_state(target, false)) {
568aef9ec39SRoland Dreier 		/* XXX should send SRP_I_LOGOUT request */
569aef9ec39SRoland Dreier 
570e6581056SRoland Dreier 		if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
5717aa54bd7SDavid Dillow 			shost_printk(KERN_DEBUG, target->scsi_host,
5727aa54bd7SDavid Dillow 				     PFX "Sending CM DREQ failed\n");
573aef9ec39SRoland Dreier 		}
574294c875aSBart Van Assche 	}
575294c875aSBart Van Assche }
576aef9ec39SRoland Dreier 
5778f26c9ffSDavid Dillow static void srp_free_req_data(struct srp_target_port *target)
5788f26c9ffSDavid Dillow {
579c07d424dSDavid Dillow 	struct ib_device *ibdev = target->srp_host->srp_dev->dev;
5808f26c9ffSDavid Dillow 	struct srp_request *req;
5818f26c9ffSDavid Dillow 	int i;
5828f26c9ffSDavid Dillow 
5834d73f95fSBart Van Assche 	if (!target->req_ring)
5844d73f95fSBart Van Assche 		return;
5854d73f95fSBart Van Assche 
5864d73f95fSBart Van Assche 	for (i = 0; i < target->req_ring_size; ++i) {
5874d73f95fSBart Van Assche 		req = &target->req_ring[i];
5888f26c9ffSDavid Dillow 		kfree(req->fmr_list);
5898f26c9ffSDavid Dillow 		kfree(req->map_page);
590c07d424dSDavid Dillow 		if (req->indirect_dma_addr) {
591c07d424dSDavid Dillow 			ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
592c07d424dSDavid Dillow 					    target->indirect_size,
593c07d424dSDavid Dillow 					    DMA_TO_DEVICE);
594c07d424dSDavid Dillow 		}
595c07d424dSDavid Dillow 		kfree(req->indirect_desc);
5968f26c9ffSDavid Dillow 	}
5974d73f95fSBart Van Assche 
5984d73f95fSBart Van Assche 	kfree(target->req_ring);
5994d73f95fSBart Van Assche 	target->req_ring = NULL;
6008f26c9ffSDavid Dillow }
6018f26c9ffSDavid Dillow 
602b81d00bdSBart Van Assche static int srp_alloc_req_data(struct srp_target_port *target)
603b81d00bdSBart Van Assche {
604b81d00bdSBart Van Assche 	struct srp_device *srp_dev = target->srp_host->srp_dev;
605b81d00bdSBart Van Assche 	struct ib_device *ibdev = srp_dev->dev;
606b81d00bdSBart Van Assche 	struct srp_request *req;
607b81d00bdSBart Van Assche 	dma_addr_t dma_addr;
608b81d00bdSBart Van Assche 	int i, ret = -ENOMEM;
609b81d00bdSBart Van Assche 
610b81d00bdSBart Van Assche 	INIT_LIST_HEAD(&target->free_reqs);
611b81d00bdSBart Van Assche 
6124d73f95fSBart Van Assche 	target->req_ring = kzalloc(target->req_ring_size *
6134d73f95fSBart Van Assche 				   sizeof(*target->req_ring), GFP_KERNEL);
6144d73f95fSBart Van Assche 	if (!target->req_ring)
6154d73f95fSBart Van Assche 		goto out;
6164d73f95fSBart Van Assche 
6174d73f95fSBart Van Assche 	for (i = 0; i < target->req_ring_size; ++i) {
618b81d00bdSBart Van Assche 		req = &target->req_ring[i];
619b81d00bdSBart Van Assche 		req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *),
620b81d00bdSBart Van Assche 					GFP_KERNEL);
621b81d00bdSBart Van Assche 		req->map_page = kmalloc(SRP_FMR_SIZE * sizeof(void *),
622b81d00bdSBart Van Assche 					GFP_KERNEL);
623b81d00bdSBart Van Assche 		req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
624b81d00bdSBart Van Assche 		if (!req->fmr_list || !req->map_page || !req->indirect_desc)
625b81d00bdSBart Van Assche 			goto out;
626b81d00bdSBart Van Assche 
627b81d00bdSBart Van Assche 		dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
628b81d00bdSBart Van Assche 					     target->indirect_size,
629b81d00bdSBart Van Assche 					     DMA_TO_DEVICE);
630b81d00bdSBart Van Assche 		if (ib_dma_mapping_error(ibdev, dma_addr))
631b81d00bdSBart Van Assche 			goto out;
632b81d00bdSBart Van Assche 
633b81d00bdSBart Van Assche 		req->indirect_dma_addr = dma_addr;
634b81d00bdSBart Van Assche 		req->index = i;
635b81d00bdSBart Van Assche 		list_add_tail(&req->list, &target->free_reqs);
636b81d00bdSBart Van Assche 	}
637b81d00bdSBart Van Assche 	ret = 0;
638b81d00bdSBart Van Assche 
639b81d00bdSBart Van Assche out:
640b81d00bdSBart Van Assche 	return ret;
641b81d00bdSBart Van Assche }
642b81d00bdSBart Van Assche 
643683b159aSBart Van Assche /**
644683b159aSBart Van Assche  * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
645683b159aSBart Van Assche  * @shost: SCSI host whose attributes to remove from sysfs.
646683b159aSBart Van Assche  *
647683b159aSBart Van Assche  * Note: Any attributes defined in the host template and that did not exist
648683b159aSBart Van Assche  * before invocation of this function will be ignored.
649683b159aSBart Van Assche  */
650683b159aSBart Van Assche static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
651683b159aSBart Van Assche {
652683b159aSBart Van Assche 	struct device_attribute **attr;
653683b159aSBart Van Assche 
654683b159aSBart Van Assche 	for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
655683b159aSBart Van Assche 		device_remove_file(&shost->shost_dev, *attr);
656683b159aSBart Van Assche }
657683b159aSBart Van Assche 
658ee12d6a8SBart Van Assche static void srp_remove_target(struct srp_target_port *target)
659ee12d6a8SBart Van Assche {
660ef6c49d8SBart Van Assche 	WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
661ef6c49d8SBart Van Assche 
662ee12d6a8SBart Van Assche 	srp_del_scsi_host_attr(target->scsi_host);
6639dd69a60SBart Van Assche 	srp_rport_get(target->rport);
664ee12d6a8SBart Van Assche 	srp_remove_host(target->scsi_host);
665ee12d6a8SBart Van Assche 	scsi_remove_host(target->scsi_host);
66693079162SBart Van Assche 	srp_stop_rport_timers(target->rport);
667ef6c49d8SBart Van Assche 	srp_disconnect_target(target);
668ee12d6a8SBart Van Assche 	ib_destroy_cm_id(target->cm_id);
669ee12d6a8SBart Van Assche 	srp_free_target_ib(target);
670c1120f89SBart Van Assche 	cancel_work_sync(&target->tl_err_work);
6719dd69a60SBart Van Assche 	srp_rport_put(target->rport);
672ee12d6a8SBart Van Assche 	srp_free_req_data(target);
67365d7dd2fSVu Pham 
67465d7dd2fSVu Pham 	spin_lock(&target->srp_host->target_lock);
67565d7dd2fSVu Pham 	list_del(&target->list);
67665d7dd2fSVu Pham 	spin_unlock(&target->srp_host->target_lock);
67765d7dd2fSVu Pham 
678ee12d6a8SBart Van Assche 	scsi_host_put(target->scsi_host);
679ee12d6a8SBart Van Assche }
680ee12d6a8SBart Van Assche 
681c4028958SDavid Howells static void srp_remove_work(struct work_struct *work)
682aef9ec39SRoland Dreier {
683c4028958SDavid Howells 	struct srp_target_port *target =
684ef6c49d8SBart Van Assche 		container_of(work, struct srp_target_port, remove_work);
685aef9ec39SRoland Dreier 
686ef6c49d8SBart Van Assche 	WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
687aef9ec39SRoland Dreier 
68896fc248aSBart Van Assche 	srp_remove_target(target);
689aef9ec39SRoland Dreier }
690aef9ec39SRoland Dreier 
691dc1bdbd9SBart Van Assche static void srp_rport_delete(struct srp_rport *rport)
692dc1bdbd9SBart Van Assche {
693dc1bdbd9SBart Van Assche 	struct srp_target_port *target = rport->lld_data;
694dc1bdbd9SBart Van Assche 
695dc1bdbd9SBart Van Assche 	srp_queue_remove_work(target);
696dc1bdbd9SBart Van Assche }
697dc1bdbd9SBart Van Assche 
698aef9ec39SRoland Dreier static int srp_connect_target(struct srp_target_port *target)
699aef9ec39SRoland Dreier {
7009fe4bcf4SDavid Dillow 	int retries = 3;
701aef9ec39SRoland Dreier 	int ret;
702aef9ec39SRoland Dreier 
703294c875aSBart Van Assche 	WARN_ON_ONCE(target->connected);
704294c875aSBart Van Assche 
705948d1e88SBart Van Assche 	target->qp_in_error = false;
706948d1e88SBart Van Assche 
707aef9ec39SRoland Dreier 	ret = srp_lookup_path(target);
708aef9ec39SRoland Dreier 	if (ret)
709aef9ec39SRoland Dreier 		return ret;
710aef9ec39SRoland Dreier 
711aef9ec39SRoland Dreier 	while (1) {
712aef9ec39SRoland Dreier 		init_completion(&target->done);
713aef9ec39SRoland Dreier 		ret = srp_send_req(target);
714aef9ec39SRoland Dreier 		if (ret)
715aef9ec39SRoland Dreier 			return ret;
716a702adceSBart Van Assche 		ret = wait_for_completion_interruptible(&target->done);
717a702adceSBart Van Assche 		if (ret < 0)
718a702adceSBart Van Assche 			return ret;
719aef9ec39SRoland Dreier 
720aef9ec39SRoland Dreier 		/*
721aef9ec39SRoland Dreier 		 * The CM event handling code will set status to
722aef9ec39SRoland Dreier 		 * SRP_PORT_REDIRECT if we get a port redirect REJ
723aef9ec39SRoland Dreier 		 * back, or SRP_DLID_REDIRECT if we get a lid/qp
724aef9ec39SRoland Dreier 		 * redirect REJ back.
725aef9ec39SRoland Dreier 		 */
726aef9ec39SRoland Dreier 		switch (target->status) {
727aef9ec39SRoland Dreier 		case 0:
728294c875aSBart Van Assche 			srp_change_conn_state(target, true);
729aef9ec39SRoland Dreier 			return 0;
730aef9ec39SRoland Dreier 
731aef9ec39SRoland Dreier 		case SRP_PORT_REDIRECT:
732aef9ec39SRoland Dreier 			ret = srp_lookup_path(target);
733aef9ec39SRoland Dreier 			if (ret)
734aef9ec39SRoland Dreier 				return ret;
735aef9ec39SRoland Dreier 			break;
736aef9ec39SRoland Dreier 
737aef9ec39SRoland Dreier 		case SRP_DLID_REDIRECT:
738aef9ec39SRoland Dreier 			break;
739aef9ec39SRoland Dreier 
7409fe4bcf4SDavid Dillow 		case SRP_STALE_CONN:
7419fe4bcf4SDavid Dillow 			/* Our current CM id was stale, and is now in timewait.
7429fe4bcf4SDavid Dillow 			 * Try to reconnect with a new one.
7439fe4bcf4SDavid Dillow 			 */
7449fe4bcf4SDavid Dillow 			if (!retries-- || srp_new_cm_id(target)) {
7459fe4bcf4SDavid Dillow 				shost_printk(KERN_ERR, target->scsi_host, PFX
7469fe4bcf4SDavid Dillow 					     "giving up on stale connection\n");
7479fe4bcf4SDavid Dillow 				target->status = -ECONNRESET;
7489fe4bcf4SDavid Dillow 				return target->status;
7499fe4bcf4SDavid Dillow 			}
7509fe4bcf4SDavid Dillow 
7519fe4bcf4SDavid Dillow 			shost_printk(KERN_ERR, target->scsi_host, PFX
7529fe4bcf4SDavid Dillow 				     "retrying stale connection\n");
7539fe4bcf4SDavid Dillow 			break;
7549fe4bcf4SDavid Dillow 
755aef9ec39SRoland Dreier 		default:
756aef9ec39SRoland Dreier 			return target->status;
757aef9ec39SRoland Dreier 		}
758aef9ec39SRoland Dreier 	}
759aef9ec39SRoland Dreier }
760aef9ec39SRoland Dreier 
761d945e1dfSRoland Dreier static void srp_unmap_data(struct scsi_cmnd *scmnd,
762d945e1dfSRoland Dreier 			   struct srp_target_port *target,
763d945e1dfSRoland Dreier 			   struct srp_request *req)
764d945e1dfSRoland Dreier {
7658f26c9ffSDavid Dillow 	struct ib_device *ibdev = target->srp_host->srp_dev->dev;
7668f26c9ffSDavid Dillow 	struct ib_pool_fmr **pfmr;
7678f26c9ffSDavid Dillow 
768bb350d1dSFUJITA Tomonori 	if (!scsi_sglist(scmnd) ||
769d945e1dfSRoland Dreier 	    (scmnd->sc_data_direction != DMA_TO_DEVICE &&
770d945e1dfSRoland Dreier 	     scmnd->sc_data_direction != DMA_FROM_DEVICE))
771d945e1dfSRoland Dreier 		return;
772d945e1dfSRoland Dreier 
7738f26c9ffSDavid Dillow 	pfmr = req->fmr_list;
7748f26c9ffSDavid Dillow 	while (req->nfmr--)
7758f26c9ffSDavid Dillow 		ib_fmr_pool_unmap(*pfmr++);
776f5358a17SRoland Dreier 
7778f26c9ffSDavid Dillow 	ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
7788f26c9ffSDavid Dillow 			scmnd->sc_data_direction);
779d945e1dfSRoland Dreier }
780d945e1dfSRoland Dreier 
78122032991SBart Van Assche /**
78222032991SBart Van Assche  * srp_claim_req - Take ownership of the scmnd associated with a request.
78322032991SBart Van Assche  * @target: SRP target port.
78422032991SBart Van Assche  * @req: SRP request.
785b3fe628dSBart Van Assche  * @sdev: If not NULL, only take ownership for this SCSI device.
78622032991SBart Van Assche  * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
78722032991SBart Van Assche  *         ownership of @req->scmnd if it equals @scmnd.
78822032991SBart Van Assche  *
78922032991SBart Van Assche  * Return value:
79022032991SBart Van Assche  * Either NULL or a pointer to the SCSI command the caller became owner of.
79122032991SBart Van Assche  */
79222032991SBart Van Assche static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
79322032991SBart Van Assche 				       struct srp_request *req,
794b3fe628dSBart Van Assche 				       struct scsi_device *sdev,
79522032991SBart Van Assche 				       struct scsi_cmnd *scmnd)
796526b4caaSIshai Rabinovitz {
79794a9174cSBart Van Assche 	unsigned long flags;
79894a9174cSBart Van Assche 
79922032991SBart Van Assche 	spin_lock_irqsave(&target->lock, flags);
800b3fe628dSBart Van Assche 	if (req->scmnd &&
801b3fe628dSBart Van Assche 	    (!sdev || req->scmnd->device == sdev) &&
802b3fe628dSBart Van Assche 	    (!scmnd || req->scmnd == scmnd)) {
80322032991SBart Van Assche 		scmnd = req->scmnd;
80422032991SBart Van Assche 		req->scmnd = NULL;
80522032991SBart Van Assche 	} else {
80622032991SBart Van Assche 		scmnd = NULL;
80722032991SBart Van Assche 	}
80822032991SBart Van Assche 	spin_unlock_irqrestore(&target->lock, flags);
80922032991SBart Van Assche 
81022032991SBart Van Assche 	return scmnd;
81122032991SBart Van Assche }
81222032991SBart Van Assche 
81322032991SBart Van Assche /**
81422032991SBart Van Assche  * srp_free_req() - Unmap data and add request to the free request list.
815af24663bSBart Van Assche  * @target: SRP target port.
816af24663bSBart Van Assche  * @req:    Request to be freed.
817af24663bSBart Van Assche  * @scmnd:  SCSI command associated with @req.
818af24663bSBart Van Assche  * @req_lim_delta: Amount to be added to @target->req_lim.
81922032991SBart Van Assche  */
82022032991SBart Van Assche static void srp_free_req(struct srp_target_port *target,
82122032991SBart Van Assche 			 struct srp_request *req, struct scsi_cmnd *scmnd,
82222032991SBart Van Assche 			 s32 req_lim_delta)
82322032991SBart Van Assche {
82422032991SBart Van Assche 	unsigned long flags;
82522032991SBart Van Assche 
82622032991SBart Van Assche 	srp_unmap_data(scmnd, target, req);
82722032991SBart Van Assche 
828e9684678SBart Van Assche 	spin_lock_irqsave(&target->lock, flags);
82994a9174cSBart Van Assche 	target->req_lim += req_lim_delta;
830536ae14eSBart Van Assche 	list_add_tail(&req->list, &target->free_reqs);
831e9684678SBart Van Assche 	spin_unlock_irqrestore(&target->lock, flags);
832526b4caaSIshai Rabinovitz }
833526b4caaSIshai Rabinovitz 
834ed9b2264SBart Van Assche static void srp_finish_req(struct srp_target_port *target,
835b3fe628dSBart Van Assche 			   struct srp_request *req, struct scsi_device *sdev,
836b3fe628dSBart Van Assche 			   int result)
837526b4caaSIshai Rabinovitz {
838b3fe628dSBart Van Assche 	struct scsi_cmnd *scmnd = srp_claim_req(target, req, sdev, NULL);
83922032991SBart Van Assche 
84022032991SBart Van Assche 	if (scmnd) {
8419b796d06SBart Van Assche 		srp_free_req(target, req, scmnd, 0);
842ed9b2264SBart Van Assche 		scmnd->result = result;
84322032991SBart Van Assche 		scmnd->scsi_done(scmnd);
84422032991SBart Van Assche 	}
845526b4caaSIshai Rabinovitz }
846526b4caaSIshai Rabinovitz 
847ed9b2264SBart Van Assche static void srp_terminate_io(struct srp_rport *rport)
848aef9ec39SRoland Dreier {
849ed9b2264SBart Van Assche 	struct srp_target_port *target = rport->lld_data;
850b3fe628dSBart Van Assche 	struct Scsi_Host *shost = target->scsi_host;
851b3fe628dSBart Van Assche 	struct scsi_device *sdev;
852ed9b2264SBart Van Assche 	int i;
853aef9ec39SRoland Dreier 
854b3fe628dSBart Van Assche 	/*
855b3fe628dSBart Van Assche 	 * Invoking srp_terminate_io() while srp_queuecommand() is running
856b3fe628dSBart Van Assche 	 * is not safe. Hence the warning statement below.
857b3fe628dSBart Van Assche 	 */
858b3fe628dSBart Van Assche 	shost_for_each_device(sdev, shost)
859b3fe628dSBart Van Assche 		WARN_ON_ONCE(sdev->request_queue->request_fn_active);
860b3fe628dSBart Van Assche 
8614d73f95fSBart Van Assche 	for (i = 0; i < target->req_ring_size; ++i) {
862ed9b2264SBart Van Assche 		struct srp_request *req = &target->req_ring[i];
863b3fe628dSBart Van Assche 		srp_finish_req(target, req, NULL, DID_TRANSPORT_FAILFAST << 16);
864ed9b2264SBart Van Assche 	}
865ed9b2264SBart Van Assche }
866ed9b2264SBart Van Assche 
867ed9b2264SBart Van Assche /*
868ed9b2264SBart Van Assche  * It is up to the caller to ensure that srp_rport_reconnect() calls are
869ed9b2264SBart Van Assche  * serialized and that no concurrent srp_queuecommand(), srp_abort(),
870ed9b2264SBart Van Assche  * srp_reset_device() or srp_reset_host() calls will occur while this function
871ed9b2264SBart Van Assche  * is in progress. One way to realize that is not to call this function
872ed9b2264SBart Van Assche  * directly but to call srp_reconnect_rport() instead since that last function
873ed9b2264SBart Van Assche  * serializes calls of this function via rport->mutex and also blocks
874ed9b2264SBart Van Assche  * srp_queuecommand() calls before invoking this function.
875ed9b2264SBart Van Assche  */
876ed9b2264SBart Van Assche static int srp_rport_reconnect(struct srp_rport *rport)
877ed9b2264SBart Van Assche {
878ed9b2264SBart Van Assche 	struct srp_target_port *target = rport->lld_data;
879ed9b2264SBart Van Assche 	int i, ret;
88009be70a2SBart Van Assche 
881aef9ec39SRoland Dreier 	srp_disconnect_target(target);
882aef9ec39SRoland Dreier 	/*
883c7c4e7ffSBart Van Assche 	 * Now get a new local CM ID so that we avoid confusing the target in
884c7c4e7ffSBart Van Assche 	 * case things are really fouled up. Doing so also ensures that all CM
885c7c4e7ffSBart Van Assche 	 * callbacks will have finished before a new QP is allocated.
886aef9ec39SRoland Dreier 	 */
8879fe4bcf4SDavid Dillow 	ret = srp_new_cm_id(target);
888c7c4e7ffSBart Van Assche 	/*
889c7c4e7ffSBart Van Assche 	 * Whether or not creating a new CM ID succeeded, create a new
890c7c4e7ffSBart Van Assche 	 * QP. This guarantees that all completion callback function
891c7c4e7ffSBart Van Assche 	 * invocations have finished before request resetting starts.
892c7c4e7ffSBart Van Assche 	 */
893c7c4e7ffSBart Van Assche 	if (ret == 0)
89473aa89edSIshai Rabinovitz 		ret = srp_create_target_ib(target);
895c7c4e7ffSBart Van Assche 	else
896c7c4e7ffSBart Van Assche 		srp_create_target_ib(target);
897aef9ec39SRoland Dreier 
8984d73f95fSBart Van Assche 	for (i = 0; i < target->req_ring_size; ++i) {
899536ae14eSBart Van Assche 		struct srp_request *req = &target->req_ring[i];
900b3fe628dSBart Van Assche 		srp_finish_req(target, req, NULL, DID_RESET << 16);
901536ae14eSBart Van Assche 	}
902aef9ec39SRoland Dreier 
903536ae14eSBart Van Assche 	INIT_LIST_HEAD(&target->free_tx);
9044d73f95fSBart Van Assche 	for (i = 0; i < target->queue_size; ++i)
905536ae14eSBart Van Assche 		list_add(&target->tx_ring[i]->list, &target->free_tx);
906aef9ec39SRoland Dreier 
907c7c4e7ffSBart Van Assche 	if (ret == 0)
908aef9ec39SRoland Dreier 		ret = srp_connect_target(target);
90909be70a2SBart Van Assche 
910ed9b2264SBart Van Assche 	if (ret == 0)
911ed9b2264SBart Van Assche 		shost_printk(KERN_INFO, target->scsi_host,
912ed9b2264SBart Van Assche 			     PFX "reconnect succeeded\n");
913aef9ec39SRoland Dreier 
914aef9ec39SRoland Dreier 	return ret;
915aef9ec39SRoland Dreier }
916aef9ec39SRoland Dreier 
9178f26c9ffSDavid Dillow static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
9188f26c9ffSDavid Dillow 			 unsigned int dma_len, u32 rkey)
919f5358a17SRoland Dreier {
9208f26c9ffSDavid Dillow 	struct srp_direct_buf *desc = state->desc;
9218f26c9ffSDavid Dillow 
9228f26c9ffSDavid Dillow 	desc->va = cpu_to_be64(dma_addr);
9238f26c9ffSDavid Dillow 	desc->key = cpu_to_be32(rkey);
9248f26c9ffSDavid Dillow 	desc->len = cpu_to_be32(dma_len);
9258f26c9ffSDavid Dillow 
9268f26c9ffSDavid Dillow 	state->total_len += dma_len;
9278f26c9ffSDavid Dillow 	state->desc++;
9288f26c9ffSDavid Dillow 	state->ndesc++;
9298f26c9ffSDavid Dillow }
9308f26c9ffSDavid Dillow 
9318f26c9ffSDavid Dillow static int srp_map_finish_fmr(struct srp_map_state *state,
9328f26c9ffSDavid Dillow 			      struct srp_target_port *target)
9338f26c9ffSDavid Dillow {
9348f26c9ffSDavid Dillow 	struct srp_device *dev = target->srp_host->srp_dev;
9358f26c9ffSDavid Dillow 	struct ib_pool_fmr *fmr;
936f5358a17SRoland Dreier 	u64 io_addr = 0;
9378f26c9ffSDavid Dillow 
9388f26c9ffSDavid Dillow 	if (!state->npages)
9398f26c9ffSDavid Dillow 		return 0;
9408f26c9ffSDavid Dillow 
9418f26c9ffSDavid Dillow 	if (state->npages == 1) {
9428f26c9ffSDavid Dillow 		srp_map_desc(state, state->base_dma_addr, state->fmr_len,
9438f26c9ffSDavid Dillow 			     target->rkey);
9448f26c9ffSDavid Dillow 		state->npages = state->fmr_len = 0;
9458f26c9ffSDavid Dillow 		return 0;
9468f26c9ffSDavid Dillow 	}
9478f26c9ffSDavid Dillow 
9488f26c9ffSDavid Dillow 	fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
9498f26c9ffSDavid Dillow 				   state->npages, io_addr);
9508f26c9ffSDavid Dillow 	if (IS_ERR(fmr))
9518f26c9ffSDavid Dillow 		return PTR_ERR(fmr);
9528f26c9ffSDavid Dillow 
9538f26c9ffSDavid Dillow 	*state->next_fmr++ = fmr;
9548f26c9ffSDavid Dillow 	state->nfmr++;
9558f26c9ffSDavid Dillow 
9568f26c9ffSDavid Dillow 	srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
9578f26c9ffSDavid Dillow 	state->npages = state->fmr_len = 0;
9588f26c9ffSDavid Dillow 	return 0;
9598f26c9ffSDavid Dillow }
9608f26c9ffSDavid Dillow 
9618f26c9ffSDavid Dillow static void srp_map_update_start(struct srp_map_state *state,
9628f26c9ffSDavid Dillow 				 struct scatterlist *sg, int sg_index,
9638f26c9ffSDavid Dillow 				 dma_addr_t dma_addr)
9648f26c9ffSDavid Dillow {
9658f26c9ffSDavid Dillow 	state->unmapped_sg = sg;
9668f26c9ffSDavid Dillow 	state->unmapped_index = sg_index;
9678f26c9ffSDavid Dillow 	state->unmapped_addr = dma_addr;
9688f26c9ffSDavid Dillow }
9698f26c9ffSDavid Dillow 
9708f26c9ffSDavid Dillow static int srp_map_sg_entry(struct srp_map_state *state,
9718f26c9ffSDavid Dillow 			    struct srp_target_port *target,
9728f26c9ffSDavid Dillow 			    struct scatterlist *sg, int sg_index,
9738f26c9ffSDavid Dillow 			    int use_fmr)
9748f26c9ffSDavid Dillow {
97505321937SGreg Kroah-Hartman 	struct srp_device *dev = target->srp_host->srp_dev;
97685507bccSRalph Campbell 	struct ib_device *ibdev = dev->dev;
9778f26c9ffSDavid Dillow 	dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
978bb350d1dSFUJITA Tomonori 	unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
9798f26c9ffSDavid Dillow 	unsigned int len;
9808f26c9ffSDavid Dillow 	int ret;
98185507bccSRalph Campbell 
9828f26c9ffSDavid Dillow 	if (!dma_len)
9838f26c9ffSDavid Dillow 		return 0;
9848f26c9ffSDavid Dillow 
9858f26c9ffSDavid Dillow 	if (use_fmr == SRP_MAP_NO_FMR) {
9868f26c9ffSDavid Dillow 		/* Once we're in direct map mode for a request, we don't
9878f26c9ffSDavid Dillow 		 * go back to FMR mode, so no need to update anything
9888f26c9ffSDavid Dillow 		 * other than the descriptor.
9898f26c9ffSDavid Dillow 		 */
9908f26c9ffSDavid Dillow 		srp_map_desc(state, dma_addr, dma_len, target->rkey);
9918f26c9ffSDavid Dillow 		return 0;
992f5358a17SRoland Dreier 	}
993f5358a17SRoland Dreier 
9948f26c9ffSDavid Dillow 	/* If we start at an offset into the FMR page, don't merge into
9958f26c9ffSDavid Dillow 	 * the current FMR. Finish it out, and use the kernel's MR for this
9968f26c9ffSDavid Dillow 	 * sg entry. This is to avoid potential bugs on some SRP targets
9978f26c9ffSDavid Dillow 	 * that were never quite defined, but went away when the initiator
9988f26c9ffSDavid Dillow 	 * avoided using FMR on such page fragments.
9998f26c9ffSDavid Dillow 	 */
10008f26c9ffSDavid Dillow 	if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
10018f26c9ffSDavid Dillow 		ret = srp_map_finish_fmr(state, target);
10028f26c9ffSDavid Dillow 		if (ret)
10038f26c9ffSDavid Dillow 			return ret;
10048f26c9ffSDavid Dillow 
10058f26c9ffSDavid Dillow 		srp_map_desc(state, dma_addr, dma_len, target->rkey);
10068f26c9ffSDavid Dillow 		srp_map_update_start(state, NULL, 0, 0);
10078f26c9ffSDavid Dillow 		return 0;
1008f5358a17SRoland Dreier 	}
1009f5358a17SRoland Dreier 
10108f26c9ffSDavid Dillow 	/* If this is the first sg to go into the FMR, save our position.
10118f26c9ffSDavid Dillow 	 * We need to know the first unmapped entry, its index, and the
10128f26c9ffSDavid Dillow 	 * first unmapped address within that entry to be able to restart
10138f26c9ffSDavid Dillow 	 * mapping after an error.
10148f26c9ffSDavid Dillow 	 */
10158f26c9ffSDavid Dillow 	if (!state->unmapped_sg)
10168f26c9ffSDavid Dillow 		srp_map_update_start(state, sg, sg_index, dma_addr);
1017f5358a17SRoland Dreier 
10188f26c9ffSDavid Dillow 	while (dma_len) {
10198f26c9ffSDavid Dillow 		if (state->npages == SRP_FMR_SIZE) {
10208f26c9ffSDavid Dillow 			ret = srp_map_finish_fmr(state, target);
10218f26c9ffSDavid Dillow 			if (ret)
10228f26c9ffSDavid Dillow 				return ret;
1023f5358a17SRoland Dreier 
10248f26c9ffSDavid Dillow 			srp_map_update_start(state, sg, sg_index, dma_addr);
102585507bccSRalph Campbell 		}
1026f5358a17SRoland Dreier 
10278f26c9ffSDavid Dillow 		len = min_t(unsigned int, dma_len, dev->fmr_page_size);
10288f26c9ffSDavid Dillow 
10298f26c9ffSDavid Dillow 		if (!state->npages)
10308f26c9ffSDavid Dillow 			state->base_dma_addr = dma_addr;
10318f26c9ffSDavid Dillow 		state->pages[state->npages++] = dma_addr;
10328f26c9ffSDavid Dillow 		state->fmr_len += len;
10338f26c9ffSDavid Dillow 		dma_addr += len;
10348f26c9ffSDavid Dillow 		dma_len -= len;
1035f5358a17SRoland Dreier 	}
1036f5358a17SRoland Dreier 
10378f26c9ffSDavid Dillow 	/* If the last entry of the FMR wasn't a full page, then we need to
10388f26c9ffSDavid Dillow 	 * close it out and start a new one -- we can only merge at page
10398f26c9ffSDavid Dillow 	 * boundries.
10408f26c9ffSDavid Dillow 	 */
1041f5358a17SRoland Dreier 	ret = 0;
10428f26c9ffSDavid Dillow 	if (len != dev->fmr_page_size) {
10438f26c9ffSDavid Dillow 		ret = srp_map_finish_fmr(state, target);
10448f26c9ffSDavid Dillow 		if (!ret)
10458f26c9ffSDavid Dillow 			srp_map_update_start(state, NULL, 0, 0);
10468f26c9ffSDavid Dillow 	}
1047f5358a17SRoland Dreier 	return ret;
1048f5358a17SRoland Dreier }
1049f5358a17SRoland Dreier 
1050aef9ec39SRoland Dreier static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
1051aef9ec39SRoland Dreier 			struct srp_request *req)
1052aef9ec39SRoland Dreier {
10538f26c9ffSDavid Dillow 	struct scatterlist *scat, *sg;
1054aef9ec39SRoland Dreier 	struct srp_cmd *cmd = req->cmd->buf;
10558f26c9ffSDavid Dillow 	int i, len, nents, count, use_fmr;
105685507bccSRalph Campbell 	struct srp_device *dev;
105785507bccSRalph Campbell 	struct ib_device *ibdev;
10588f26c9ffSDavid Dillow 	struct srp_map_state state;
10598f26c9ffSDavid Dillow 	struct srp_indirect_buf *indirect_hdr;
10608f26c9ffSDavid Dillow 	u32 table_len;
10618f26c9ffSDavid Dillow 	u8 fmt;
1062aef9ec39SRoland Dreier 
1063bb350d1dSFUJITA Tomonori 	if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
1064aef9ec39SRoland Dreier 		return sizeof (struct srp_cmd);
1065aef9ec39SRoland Dreier 
1066aef9ec39SRoland Dreier 	if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
1067aef9ec39SRoland Dreier 	    scmnd->sc_data_direction != DMA_TO_DEVICE) {
10687aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
10697aa54bd7SDavid Dillow 			     PFX "Unhandled data direction %d\n",
1070aef9ec39SRoland Dreier 			     scmnd->sc_data_direction);
1071aef9ec39SRoland Dreier 		return -EINVAL;
1072aef9ec39SRoland Dreier 	}
1073aef9ec39SRoland Dreier 
1074bb350d1dSFUJITA Tomonori 	nents = scsi_sg_count(scmnd);
1075bb350d1dSFUJITA Tomonori 	scat  = scsi_sglist(scmnd);
1076aef9ec39SRoland Dreier 
107705321937SGreg Kroah-Hartman 	dev = target->srp_host->srp_dev;
107885507bccSRalph Campbell 	ibdev = dev->dev;
107985507bccSRalph Campbell 
108085507bccSRalph Campbell 	count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
10818f26c9ffSDavid Dillow 	if (unlikely(count == 0))
10828f26c9ffSDavid Dillow 		return -EIO;
1083aef9ec39SRoland Dreier 
1084aef9ec39SRoland Dreier 	fmt = SRP_DATA_DESC_DIRECT;
1085f5358a17SRoland Dreier 	len = sizeof (struct srp_cmd) +	sizeof (struct srp_direct_buf);
1086f5358a17SRoland Dreier 
1087f5358a17SRoland Dreier 	if (count == 1) {
1088f5358a17SRoland Dreier 		/*
1089f5358a17SRoland Dreier 		 * The midlayer only generated a single gather/scatter
1090f5358a17SRoland Dreier 		 * entry, or DMA mapping coalesced everything to a
1091f5358a17SRoland Dreier 		 * single entry.  So a direct descriptor along with
1092f5358a17SRoland Dreier 		 * the DMA MR suffices.
1093f5358a17SRoland Dreier 		 */
1094f5358a17SRoland Dreier 		struct srp_direct_buf *buf = (void *) cmd->add_data;
1095aef9ec39SRoland Dreier 
109685507bccSRalph Campbell 		buf->va  = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
10979af76271SDavid Dillow 		buf->key = cpu_to_be32(target->rkey);
109885507bccSRalph Campbell 		buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
10998f26c9ffSDavid Dillow 
11008f26c9ffSDavid Dillow 		req->nfmr = 0;
11018f26c9ffSDavid Dillow 		goto map_complete;
11028f26c9ffSDavid Dillow 	}
11038f26c9ffSDavid Dillow 
11048f26c9ffSDavid Dillow 	/* We have more than one scatter/gather entry, so build our indirect
11058f26c9ffSDavid Dillow 	 * descriptor table, trying to merge as many entries with FMR as we
11068f26c9ffSDavid Dillow 	 * can.
1107f5358a17SRoland Dreier 	 */
11088f26c9ffSDavid Dillow 	indirect_hdr = (void *) cmd->add_data;
11098f26c9ffSDavid Dillow 
1110c07d424dSDavid Dillow 	ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
1111c07d424dSDavid Dillow 				   target->indirect_size, DMA_TO_DEVICE);
1112c07d424dSDavid Dillow 
11138f26c9ffSDavid Dillow 	memset(&state, 0, sizeof(state));
1114c07d424dSDavid Dillow 	state.desc	= req->indirect_desc;
11158f26c9ffSDavid Dillow 	state.pages	= req->map_page;
11168f26c9ffSDavid Dillow 	state.next_fmr	= req->fmr_list;
11178f26c9ffSDavid Dillow 
11188f26c9ffSDavid Dillow 	use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
11198f26c9ffSDavid Dillow 
11208f26c9ffSDavid Dillow 	for_each_sg(scat, sg, count, i) {
11218f26c9ffSDavid Dillow 		if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
11228f26c9ffSDavid Dillow 			/* FMR mapping failed, so backtrack to the first
11238f26c9ffSDavid Dillow 			 * unmapped entry and continue on without using FMR.
11248f26c9ffSDavid Dillow 			 */
11258f26c9ffSDavid Dillow 			dma_addr_t dma_addr;
11268f26c9ffSDavid Dillow 			unsigned int dma_len;
11278f26c9ffSDavid Dillow 
11288f26c9ffSDavid Dillow backtrack:
11298f26c9ffSDavid Dillow 			sg = state.unmapped_sg;
11308f26c9ffSDavid Dillow 			i = state.unmapped_index;
11318f26c9ffSDavid Dillow 
11328f26c9ffSDavid Dillow 			dma_addr = ib_sg_dma_address(ibdev, sg);
11338f26c9ffSDavid Dillow 			dma_len = ib_sg_dma_len(ibdev, sg);
11348f26c9ffSDavid Dillow 			dma_len -= (state.unmapped_addr - dma_addr);
11358f26c9ffSDavid Dillow 			dma_addr = state.unmapped_addr;
11368f26c9ffSDavid Dillow 			use_fmr = SRP_MAP_NO_FMR;
11378f26c9ffSDavid Dillow 			srp_map_desc(&state, dma_addr, dma_len, target->rkey);
11388f26c9ffSDavid Dillow 		}
11398f26c9ffSDavid Dillow 	}
11408f26c9ffSDavid Dillow 
11418f26c9ffSDavid Dillow 	if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
11428f26c9ffSDavid Dillow 		goto backtrack;
11438f26c9ffSDavid Dillow 
1144c07d424dSDavid Dillow 	/* We've mapped the request, now pull as much of the indirect
1145c07d424dSDavid Dillow 	 * descriptor table as we can into the command buffer. If this
1146c07d424dSDavid Dillow 	 * target is not using an external indirect table, we are
1147c07d424dSDavid Dillow 	 * guaranteed to fit into the command, as the SCSI layer won't
1148c07d424dSDavid Dillow 	 * give us more S/G entries than we allow.
11498f26c9ffSDavid Dillow 	 */
11508f26c9ffSDavid Dillow 	req->nfmr = state.nfmr;
11518f26c9ffSDavid Dillow 	if (state.ndesc == 1) {
11528f26c9ffSDavid Dillow 		/* FMR mapping was able to collapse this to one entry,
11538f26c9ffSDavid Dillow 		 * so use a direct descriptor.
11548f26c9ffSDavid Dillow 		 */
11558f26c9ffSDavid Dillow 		struct srp_direct_buf *buf = (void *) cmd->add_data;
11568f26c9ffSDavid Dillow 
1157c07d424dSDavid Dillow 		*buf = req->indirect_desc[0];
11588f26c9ffSDavid Dillow 		goto map_complete;
11598f26c9ffSDavid Dillow 	}
11608f26c9ffSDavid Dillow 
1161c07d424dSDavid Dillow 	if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1162c07d424dSDavid Dillow 						!target->allow_ext_sg)) {
1163c07d424dSDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
1164c07d424dSDavid Dillow 			     "Could not fit S/G list into SRP_CMD\n");
1165c07d424dSDavid Dillow 		return -EIO;
1166c07d424dSDavid Dillow 	}
1167c07d424dSDavid Dillow 
1168c07d424dSDavid Dillow 	count = min(state.ndesc, target->cmd_sg_cnt);
11698f26c9ffSDavid Dillow 	table_len = state.ndesc * sizeof (struct srp_direct_buf);
1170aef9ec39SRoland Dreier 
1171aef9ec39SRoland Dreier 	fmt = SRP_DATA_DESC_INDIRECT;
11728f26c9ffSDavid Dillow 	len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
1173c07d424dSDavid Dillow 	len += count * sizeof (struct srp_direct_buf);
1174f5358a17SRoland Dreier 
1175c07d424dSDavid Dillow 	memcpy(indirect_hdr->desc_list, req->indirect_desc,
1176c07d424dSDavid Dillow 	       count * sizeof (struct srp_direct_buf));
117785507bccSRalph Campbell 
1178c07d424dSDavid Dillow 	indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
11798f26c9ffSDavid Dillow 	indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
11808f26c9ffSDavid Dillow 	indirect_hdr->table_desc.len = cpu_to_be32(table_len);
11818f26c9ffSDavid Dillow 	indirect_hdr->len = cpu_to_be32(state.total_len);
1182aef9ec39SRoland Dreier 
1183aef9ec39SRoland Dreier 	if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1184c07d424dSDavid Dillow 		cmd->data_out_desc_cnt = count;
1185aef9ec39SRoland Dreier 	else
1186c07d424dSDavid Dillow 		cmd->data_in_desc_cnt = count;
1187c07d424dSDavid Dillow 
1188c07d424dSDavid Dillow 	ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1189c07d424dSDavid Dillow 				      DMA_TO_DEVICE);
1190aef9ec39SRoland Dreier 
11918f26c9ffSDavid Dillow map_complete:
1192aef9ec39SRoland Dreier 	if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1193aef9ec39SRoland Dreier 		cmd->buf_fmt = fmt << 4;
1194aef9ec39SRoland Dreier 	else
1195aef9ec39SRoland Dreier 		cmd->buf_fmt = fmt;
1196aef9ec39SRoland Dreier 
1197aef9ec39SRoland Dreier 	return len;
1198aef9ec39SRoland Dreier }
1199aef9ec39SRoland Dreier 
120005a1d750SDavid Dillow /*
120176c75b25SBart Van Assche  * Return an IU and possible credit to the free pool
120276c75b25SBart Van Assche  */
120376c75b25SBart Van Assche static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
120476c75b25SBart Van Assche 			  enum srp_iu_type iu_type)
120576c75b25SBart Van Assche {
120676c75b25SBart Van Assche 	unsigned long flags;
120776c75b25SBart Van Assche 
1208e9684678SBart Van Assche 	spin_lock_irqsave(&target->lock, flags);
120976c75b25SBart Van Assche 	list_add(&iu->list, &target->free_tx);
121076c75b25SBart Van Assche 	if (iu_type != SRP_IU_RSP)
121176c75b25SBart Van Assche 		++target->req_lim;
1212e9684678SBart Van Assche 	spin_unlock_irqrestore(&target->lock, flags);
121376c75b25SBart Van Assche }
121476c75b25SBart Van Assche 
121576c75b25SBart Van Assche /*
1216e9684678SBart Van Assche  * Must be called with target->lock held to protect req_lim and free_tx.
1217e9684678SBart Van Assche  * If IU is not sent, it must be returned using srp_put_tx_iu().
121805a1d750SDavid Dillow  *
121905a1d750SDavid Dillow  * Note:
122005a1d750SDavid Dillow  * An upper limit for the number of allocated information units for each
122105a1d750SDavid Dillow  * request type is:
122205a1d750SDavid Dillow  * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
122305a1d750SDavid Dillow  *   more than Scsi_Host.can_queue requests.
122405a1d750SDavid Dillow  * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
122505a1d750SDavid Dillow  * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
122605a1d750SDavid Dillow  *   one unanswered SRP request to an initiator.
122705a1d750SDavid Dillow  */
122805a1d750SDavid Dillow static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
122905a1d750SDavid Dillow 				      enum srp_iu_type iu_type)
123005a1d750SDavid Dillow {
123105a1d750SDavid Dillow 	s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
123205a1d750SDavid Dillow 	struct srp_iu *iu;
123305a1d750SDavid Dillow 
123405a1d750SDavid Dillow 	srp_send_completion(target->send_cq, target);
123505a1d750SDavid Dillow 
1236dcb4cb85SBart Van Assche 	if (list_empty(&target->free_tx))
123705a1d750SDavid Dillow 		return NULL;
123805a1d750SDavid Dillow 
123905a1d750SDavid Dillow 	/* Initiator responses to target requests do not consume credits */
124076c75b25SBart Van Assche 	if (iu_type != SRP_IU_RSP) {
124176c75b25SBart Van Assche 		if (target->req_lim <= rsv) {
124205a1d750SDavid Dillow 			++target->zero_req_lim;
124305a1d750SDavid Dillow 			return NULL;
124405a1d750SDavid Dillow 		}
124505a1d750SDavid Dillow 
124676c75b25SBart Van Assche 		--target->req_lim;
124776c75b25SBart Van Assche 	}
124876c75b25SBart Van Assche 
1249dcb4cb85SBart Van Assche 	iu = list_first_entry(&target->free_tx, struct srp_iu, list);
125076c75b25SBart Van Assche 	list_del(&iu->list);
125105a1d750SDavid Dillow 	return iu;
125205a1d750SDavid Dillow }
125305a1d750SDavid Dillow 
125476c75b25SBart Van Assche static int srp_post_send(struct srp_target_port *target,
125505a1d750SDavid Dillow 			 struct srp_iu *iu, int len)
125605a1d750SDavid Dillow {
125705a1d750SDavid Dillow 	struct ib_sge list;
125805a1d750SDavid Dillow 	struct ib_send_wr wr, *bad_wr;
125905a1d750SDavid Dillow 
126005a1d750SDavid Dillow 	list.addr   = iu->dma;
126105a1d750SDavid Dillow 	list.length = len;
12629af76271SDavid Dillow 	list.lkey   = target->lkey;
126305a1d750SDavid Dillow 
126405a1d750SDavid Dillow 	wr.next       = NULL;
1265dcb4cb85SBart Van Assche 	wr.wr_id      = (uintptr_t) iu;
126605a1d750SDavid Dillow 	wr.sg_list    = &list;
126705a1d750SDavid Dillow 	wr.num_sge    = 1;
126805a1d750SDavid Dillow 	wr.opcode     = IB_WR_SEND;
126905a1d750SDavid Dillow 	wr.send_flags = IB_SEND_SIGNALED;
127005a1d750SDavid Dillow 
127176c75b25SBart Van Assche 	return ib_post_send(target->qp, &wr, &bad_wr);
127205a1d750SDavid Dillow }
127305a1d750SDavid Dillow 
1274dcb4cb85SBart Van Assche static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
1275c996bb47SBart Van Assche {
1276c996bb47SBart Van Assche 	struct ib_recv_wr wr, *bad_wr;
1277dcb4cb85SBart Van Assche 	struct ib_sge list;
1278c996bb47SBart Van Assche 
1279c996bb47SBart Van Assche 	list.addr   = iu->dma;
1280c996bb47SBart Van Assche 	list.length = iu->size;
12819af76271SDavid Dillow 	list.lkey   = target->lkey;
1282c996bb47SBart Van Assche 
1283c996bb47SBart Van Assche 	wr.next     = NULL;
1284dcb4cb85SBart Van Assche 	wr.wr_id    = (uintptr_t) iu;
1285c996bb47SBart Van Assche 	wr.sg_list  = &list;
1286c996bb47SBart Van Assche 	wr.num_sge  = 1;
1287c996bb47SBart Van Assche 
1288dcb4cb85SBart Van Assche 	return ib_post_recv(target->qp, &wr, &bad_wr);
1289c996bb47SBart Van Assche }
1290c996bb47SBart Van Assche 
1291aef9ec39SRoland Dreier static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1292aef9ec39SRoland Dreier {
1293aef9ec39SRoland Dreier 	struct srp_request *req;
1294aef9ec39SRoland Dreier 	struct scsi_cmnd *scmnd;
1295aef9ec39SRoland Dreier 	unsigned long flags;
1296aef9ec39SRoland Dreier 
1297aef9ec39SRoland Dreier 	if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
1298e9684678SBart Van Assche 		spin_lock_irqsave(&target->lock, flags);
129994a9174cSBart Van Assche 		target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1300e9684678SBart Van Assche 		spin_unlock_irqrestore(&target->lock, flags);
130194a9174cSBart Van Assche 
1302f8b6e31eSDavid Dillow 		target->tsk_mgmt_status = -1;
1303f8b6e31eSDavid Dillow 		if (be32_to_cpu(rsp->resp_data_len) >= 4)
1304f8b6e31eSDavid Dillow 			target->tsk_mgmt_status = rsp->data[3];
1305f8b6e31eSDavid Dillow 		complete(&target->tsk_mgmt_done);
1306aef9ec39SRoland Dreier 	} else {
1307f8b6e31eSDavid Dillow 		req = &target->req_ring[rsp->tag];
1308b3fe628dSBart Van Assche 		scmnd = srp_claim_req(target, req, NULL, NULL);
130922032991SBart Van Assche 		if (!scmnd) {
13107aa54bd7SDavid Dillow 			shost_printk(KERN_ERR, target->scsi_host,
13117aa54bd7SDavid Dillow 				     "Null scmnd for RSP w/tag %016llx\n",
1312aef9ec39SRoland Dreier 				     (unsigned long long) rsp->tag);
131322032991SBart Van Assche 
131422032991SBart Van Assche 			spin_lock_irqsave(&target->lock, flags);
131522032991SBart Van Assche 			target->req_lim += be32_to_cpu(rsp->req_lim_delta);
131622032991SBart Van Assche 			spin_unlock_irqrestore(&target->lock, flags);
131722032991SBart Van Assche 
131822032991SBart Van Assche 			return;
131922032991SBart Van Assche 		}
1320aef9ec39SRoland Dreier 		scmnd->result = rsp->status;
1321aef9ec39SRoland Dreier 
1322aef9ec39SRoland Dreier 		if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1323aef9ec39SRoland Dreier 			memcpy(scmnd->sense_buffer, rsp->data +
1324aef9ec39SRoland Dreier 			       be32_to_cpu(rsp->resp_data_len),
1325aef9ec39SRoland Dreier 			       min_t(int, be32_to_cpu(rsp->sense_data_len),
1326aef9ec39SRoland Dreier 				     SCSI_SENSE_BUFFERSIZE));
1327aef9ec39SRoland Dreier 		}
1328aef9ec39SRoland Dreier 
1329aef9ec39SRoland Dreier 		if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
1330bb350d1dSFUJITA Tomonori 			scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
1331aef9ec39SRoland Dreier 		else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
1332bb350d1dSFUJITA Tomonori 			scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
1333aef9ec39SRoland Dreier 
133422032991SBart Van Assche 		srp_free_req(target, req, scmnd,
133522032991SBart Van Assche 			     be32_to_cpu(rsp->req_lim_delta));
133622032991SBart Van Assche 
1337f8b6e31eSDavid Dillow 		scmnd->host_scribble = NULL;
1338aef9ec39SRoland Dreier 		scmnd->scsi_done(scmnd);
1339aef9ec39SRoland Dreier 	}
1340aef9ec39SRoland Dreier }
1341aef9ec39SRoland Dreier 
1342bb12588aSDavid Dillow static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1343bb12588aSDavid Dillow 			       void *rsp, int len)
1344bb12588aSDavid Dillow {
134576c75b25SBart Van Assche 	struct ib_device *dev = target->srp_host->srp_dev->dev;
1346bb12588aSDavid Dillow 	unsigned long flags;
1347bb12588aSDavid Dillow 	struct srp_iu *iu;
134876c75b25SBart Van Assche 	int err;
1349bb12588aSDavid Dillow 
1350e9684678SBart Van Assche 	spin_lock_irqsave(&target->lock, flags);
1351bb12588aSDavid Dillow 	target->req_lim += req_delta;
1352bb12588aSDavid Dillow 	iu = __srp_get_tx_iu(target, SRP_IU_RSP);
1353e9684678SBart Van Assche 	spin_unlock_irqrestore(&target->lock, flags);
135476c75b25SBart Van Assche 
1355bb12588aSDavid Dillow 	if (!iu) {
1356bb12588aSDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host, PFX
1357bb12588aSDavid Dillow 			     "no IU available to send response\n");
135876c75b25SBart Van Assche 		return 1;
1359bb12588aSDavid Dillow 	}
1360bb12588aSDavid Dillow 
1361bb12588aSDavid Dillow 	ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1362bb12588aSDavid Dillow 	memcpy(iu->buf, rsp, len);
1363bb12588aSDavid Dillow 	ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1364bb12588aSDavid Dillow 
136576c75b25SBart Van Assche 	err = srp_post_send(target, iu, len);
136676c75b25SBart Van Assche 	if (err) {
1367bb12588aSDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host, PFX
1368bb12588aSDavid Dillow 			     "unable to post response: %d\n", err);
136976c75b25SBart Van Assche 		srp_put_tx_iu(target, iu, SRP_IU_RSP);
137076c75b25SBart Van Assche 	}
1371bb12588aSDavid Dillow 
1372bb12588aSDavid Dillow 	return err;
1373bb12588aSDavid Dillow }
1374bb12588aSDavid Dillow 
1375bb12588aSDavid Dillow static void srp_process_cred_req(struct srp_target_port *target,
1376bb12588aSDavid Dillow 				 struct srp_cred_req *req)
1377bb12588aSDavid Dillow {
1378bb12588aSDavid Dillow 	struct srp_cred_rsp rsp = {
1379bb12588aSDavid Dillow 		.opcode = SRP_CRED_RSP,
1380bb12588aSDavid Dillow 		.tag = req->tag,
1381bb12588aSDavid Dillow 	};
1382bb12588aSDavid Dillow 	s32 delta = be32_to_cpu(req->req_lim_delta);
1383bb12588aSDavid Dillow 
1384bb12588aSDavid Dillow 	if (srp_response_common(target, delta, &rsp, sizeof rsp))
1385bb12588aSDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host, PFX
1386bb12588aSDavid Dillow 			     "problems processing SRP_CRED_REQ\n");
1387bb12588aSDavid Dillow }
1388bb12588aSDavid Dillow 
1389bb12588aSDavid Dillow static void srp_process_aer_req(struct srp_target_port *target,
1390bb12588aSDavid Dillow 				struct srp_aer_req *req)
1391bb12588aSDavid Dillow {
1392bb12588aSDavid Dillow 	struct srp_aer_rsp rsp = {
1393bb12588aSDavid Dillow 		.opcode = SRP_AER_RSP,
1394bb12588aSDavid Dillow 		.tag = req->tag,
1395bb12588aSDavid Dillow 	};
1396bb12588aSDavid Dillow 	s32 delta = be32_to_cpu(req->req_lim_delta);
1397bb12588aSDavid Dillow 
1398bb12588aSDavid Dillow 	shost_printk(KERN_ERR, target->scsi_host, PFX
1399bb12588aSDavid Dillow 		     "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1400bb12588aSDavid Dillow 
1401bb12588aSDavid Dillow 	if (srp_response_common(target, delta, &rsp, sizeof rsp))
1402bb12588aSDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host, PFX
1403bb12588aSDavid Dillow 			     "problems processing SRP_AER_REQ\n");
1404bb12588aSDavid Dillow }
1405bb12588aSDavid Dillow 
1406aef9ec39SRoland Dreier static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1407aef9ec39SRoland Dreier {
1408dcb4cb85SBart Van Assche 	struct ib_device *dev = target->srp_host->srp_dev->dev;
1409737b94ebSRoland Dreier 	struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
1410c996bb47SBart Van Assche 	int res;
1411aef9ec39SRoland Dreier 	u8 opcode;
1412aef9ec39SRoland Dreier 
141385507bccSRalph Campbell 	ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
141485507bccSRalph Campbell 				   DMA_FROM_DEVICE);
1415aef9ec39SRoland Dreier 
1416aef9ec39SRoland Dreier 	opcode = *(u8 *) iu->buf;
1417aef9ec39SRoland Dreier 
1418aef9ec39SRoland Dreier 	if (0) {
14197aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
14207aa54bd7SDavid Dillow 			     PFX "recv completion, opcode 0x%02x\n", opcode);
14217a700811SBart Van Assche 		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
14227a700811SBart Van Assche 			       iu->buf, wc->byte_len, true);
1423aef9ec39SRoland Dreier 	}
1424aef9ec39SRoland Dreier 
1425aef9ec39SRoland Dreier 	switch (opcode) {
1426aef9ec39SRoland Dreier 	case SRP_RSP:
1427aef9ec39SRoland Dreier 		srp_process_rsp(target, iu->buf);
1428aef9ec39SRoland Dreier 		break;
1429aef9ec39SRoland Dreier 
1430bb12588aSDavid Dillow 	case SRP_CRED_REQ:
1431bb12588aSDavid Dillow 		srp_process_cred_req(target, iu->buf);
1432bb12588aSDavid Dillow 		break;
1433bb12588aSDavid Dillow 
1434bb12588aSDavid Dillow 	case SRP_AER_REQ:
1435bb12588aSDavid Dillow 		srp_process_aer_req(target, iu->buf);
1436bb12588aSDavid Dillow 		break;
1437bb12588aSDavid Dillow 
1438aef9ec39SRoland Dreier 	case SRP_T_LOGOUT:
1439aef9ec39SRoland Dreier 		/* XXX Handle target logout */
14407aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
14417aa54bd7SDavid Dillow 			     PFX "Got target logout request\n");
1442aef9ec39SRoland Dreier 		break;
1443aef9ec39SRoland Dreier 
1444aef9ec39SRoland Dreier 	default:
14457aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
14467aa54bd7SDavid Dillow 			     PFX "Unhandled SRP opcode 0x%02x\n", opcode);
1447aef9ec39SRoland Dreier 		break;
1448aef9ec39SRoland Dreier 	}
1449aef9ec39SRoland Dreier 
145085507bccSRalph Campbell 	ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
145185507bccSRalph Campbell 				      DMA_FROM_DEVICE);
1452c996bb47SBart Van Assche 
1453dcb4cb85SBart Van Assche 	res = srp_post_recv(target, iu);
1454c996bb47SBart Van Assche 	if (res != 0)
1455c996bb47SBart Van Assche 		shost_printk(KERN_ERR, target->scsi_host,
1456c996bb47SBart Van Assche 			     PFX "Recv failed with error code %d\n", res);
1457aef9ec39SRoland Dreier }
1458aef9ec39SRoland Dreier 
1459c1120f89SBart Van Assche /**
1460c1120f89SBart Van Assche  * srp_tl_err_work() - handle a transport layer error
1461af24663bSBart Van Assche  * @work: Work structure embedded in an SRP target port.
1462c1120f89SBart Van Assche  *
1463c1120f89SBart Van Assche  * Note: This function may get invoked before the rport has been created,
1464c1120f89SBart Van Assche  * hence the target->rport test.
1465c1120f89SBart Van Assche  */
1466c1120f89SBart Van Assche static void srp_tl_err_work(struct work_struct *work)
1467c1120f89SBart Van Assche {
1468c1120f89SBart Van Assche 	struct srp_target_port *target;
1469c1120f89SBart Van Assche 
1470c1120f89SBart Van Assche 	target = container_of(work, struct srp_target_port, tl_err_work);
1471c1120f89SBart Van Assche 	if (target->rport)
1472c1120f89SBart Van Assche 		srp_start_tl_fail_timers(target->rport);
1473c1120f89SBart Van Assche }
1474c1120f89SBart Van Assche 
1475cd4e3854SBart Van Assche static void srp_handle_qp_err(enum ib_wc_status wc_status, bool send_err,
1476948d1e88SBart Van Assche 			      struct srp_target_port *target)
1477948d1e88SBart Van Assche {
1478294c875aSBart Van Assche 	if (target->connected && !target->qp_in_error) {
14794f0af697SBart Van Assche 		shost_printk(KERN_ERR, target->scsi_host,
14804f0af697SBart Van Assche 			     PFX "failed %s status %d\n",
1481cd4e3854SBart Van Assche 			     send_err ? "send" : "receive",
14824f0af697SBart Van Assche 			     wc_status);
1483c1120f89SBart Van Assche 		queue_work(system_long_wq, &target->tl_err_work);
14844f0af697SBart Van Assche 	}
1485948d1e88SBart Van Assche 	target->qp_in_error = true;
1486948d1e88SBart Van Assche }
1487948d1e88SBart Van Assche 
14889c03dc9fSBart Van Assche static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
1489aef9ec39SRoland Dreier {
1490aef9ec39SRoland Dreier 	struct srp_target_port *target = target_ptr;
1491aef9ec39SRoland Dreier 	struct ib_wc wc;
1492aef9ec39SRoland Dreier 
1493aef9ec39SRoland Dreier 	ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1494aef9ec39SRoland Dreier 	while (ib_poll_cq(cq, 1, &wc) > 0) {
1495948d1e88SBart Van Assche 		if (likely(wc.status == IB_WC_SUCCESS)) {
1496948d1e88SBart Van Assche 			srp_handle_recv(target, &wc);
1497948d1e88SBart Van Assche 		} else {
1498cd4e3854SBart Van Assche 			srp_handle_qp_err(wc.status, false, target);
1499aef9ec39SRoland Dreier 		}
15009c03dc9fSBart Van Assche 	}
15019c03dc9fSBart Van Assche }
15029c03dc9fSBart Van Assche 
15039c03dc9fSBart Van Assche static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
15049c03dc9fSBart Van Assche {
15059c03dc9fSBart Van Assche 	struct srp_target_port *target = target_ptr;
15069c03dc9fSBart Van Assche 	struct ib_wc wc;
1507dcb4cb85SBart Van Assche 	struct srp_iu *iu;
15089c03dc9fSBart Van Assche 
15099c03dc9fSBart Van Assche 	while (ib_poll_cq(cq, 1, &wc) > 0) {
1510948d1e88SBart Van Assche 		if (likely(wc.status == IB_WC_SUCCESS)) {
1511737b94ebSRoland Dreier 			iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1512dcb4cb85SBart Van Assche 			list_add(&iu->list, &target->free_tx);
1513948d1e88SBart Van Assche 		} else {
1514cd4e3854SBart Van Assche 			srp_handle_qp_err(wc.status, true, target);
1515948d1e88SBart Van Assche 		}
1516aef9ec39SRoland Dreier 	}
1517aef9ec39SRoland Dreier }
1518aef9ec39SRoland Dreier 
151976c75b25SBart Van Assche static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
1520aef9ec39SRoland Dreier {
152176c75b25SBart Van Assche 	struct srp_target_port *target = host_to_target(shost);
1522a95cadb9SBart Van Assche 	struct srp_rport *rport = target->rport;
1523aef9ec39SRoland Dreier 	struct srp_request *req;
1524aef9ec39SRoland Dreier 	struct srp_iu *iu;
1525aef9ec39SRoland Dreier 	struct srp_cmd *cmd;
152685507bccSRalph Campbell 	struct ib_device *dev;
152776c75b25SBart Van Assche 	unsigned long flags;
1528ed9b2264SBart Van Assche 	int len, result;
1529a95cadb9SBart Van Assche 	const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
1530a95cadb9SBart Van Assche 
1531a95cadb9SBart Van Assche 	/*
1532a95cadb9SBart Van Assche 	 * The SCSI EH thread is the only context from which srp_queuecommand()
1533a95cadb9SBart Van Assche 	 * can get invoked for blocked devices (SDEV_BLOCK /
1534a95cadb9SBart Van Assche 	 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
1535a95cadb9SBart Van Assche 	 * locking the rport mutex if invoked from inside the SCSI EH.
1536a95cadb9SBart Van Assche 	 */
1537a95cadb9SBart Van Assche 	if (in_scsi_eh)
1538a95cadb9SBart Van Assche 		mutex_lock(&rport->mutex);
1539aef9ec39SRoland Dreier 
1540ed9b2264SBart Van Assche 	result = srp_chkready(target->rport);
1541ed9b2264SBart Van Assche 	if (unlikely(result)) {
1542ed9b2264SBart Van Assche 		scmnd->result = result;
15432ce19e72SBart Van Assche 		scmnd->scsi_done(scmnd);
1544a95cadb9SBart Van Assche 		goto unlock_rport;
15452ce19e72SBart Van Assche 	}
15462ce19e72SBart Van Assche 
1547e9684678SBart Van Assche 	spin_lock_irqsave(&target->lock, flags);
1548bb12588aSDavid Dillow 	iu = __srp_get_tx_iu(target, SRP_IU_CMD);
1549aef9ec39SRoland Dreier 	if (!iu)
1550695b8349SBart Van Assche 		goto err_unlock;
1551695b8349SBart Van Assche 
1552695b8349SBart Van Assche 	req = list_first_entry(&target->free_reqs, struct srp_request, list);
1553695b8349SBart Van Assche 	list_del(&req->list);
1554695b8349SBart Van Assche 	spin_unlock_irqrestore(&target->lock, flags);
1555aef9ec39SRoland Dreier 
155605321937SGreg Kroah-Hartman 	dev = target->srp_host->srp_dev->dev;
155749248644SDavid Dillow 	ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
155885507bccSRalph Campbell 				   DMA_TO_DEVICE);
1559aef9ec39SRoland Dreier 
1560aef9ec39SRoland Dreier 	scmnd->result        = 0;
1561f8b6e31eSDavid Dillow 	scmnd->host_scribble = (void *) req;
1562aef9ec39SRoland Dreier 
1563aef9ec39SRoland Dreier 	cmd = iu->buf;
1564aef9ec39SRoland Dreier 	memset(cmd, 0, sizeof *cmd);
1565aef9ec39SRoland Dreier 
1566aef9ec39SRoland Dreier 	cmd->opcode = SRP_CMD;
1567aef9ec39SRoland Dreier 	cmd->lun    = cpu_to_be64((u64) scmnd->device->lun << 48);
1568d945e1dfSRoland Dreier 	cmd->tag    = req->index;
1569aef9ec39SRoland Dreier 	memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1570aef9ec39SRoland Dreier 
1571aef9ec39SRoland Dreier 	req->scmnd    = scmnd;
1572aef9ec39SRoland Dreier 	req->cmd      = iu;
1573aef9ec39SRoland Dreier 
1574aef9ec39SRoland Dreier 	len = srp_map_data(scmnd, target, req);
1575aef9ec39SRoland Dreier 	if (len < 0) {
15767aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
15777aa54bd7SDavid Dillow 			     PFX "Failed to map data\n");
157876c75b25SBart Van Assche 		goto err_iu;
1579aef9ec39SRoland Dreier 	}
1580aef9ec39SRoland Dreier 
158149248644SDavid Dillow 	ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
158285507bccSRalph Campbell 				      DMA_TO_DEVICE);
1583aef9ec39SRoland Dreier 
158476c75b25SBart Van Assche 	if (srp_post_send(target, iu, len)) {
15857aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
1586aef9ec39SRoland Dreier 		goto err_unmap;
1587aef9ec39SRoland Dreier 	}
1588aef9ec39SRoland Dreier 
1589a95cadb9SBart Van Assche unlock_rport:
1590a95cadb9SBart Van Assche 	if (in_scsi_eh)
1591a95cadb9SBart Van Assche 		mutex_unlock(&rport->mutex);
1592a95cadb9SBart Van Assche 
1593aef9ec39SRoland Dreier 	return 0;
1594aef9ec39SRoland Dreier 
1595aef9ec39SRoland Dreier err_unmap:
1596aef9ec39SRoland Dreier 	srp_unmap_data(scmnd, target, req);
1597aef9ec39SRoland Dreier 
159876c75b25SBart Van Assche err_iu:
159976c75b25SBart Van Assche 	srp_put_tx_iu(target, iu, SRP_IU_CMD);
160076c75b25SBart Van Assche 
1601024ca901SBart Van Assche 	/*
1602024ca901SBart Van Assche 	 * Avoid that the loops that iterate over the request ring can
1603024ca901SBart Van Assche 	 * encounter a dangling SCSI command pointer.
1604024ca901SBart Van Assche 	 */
1605024ca901SBart Van Assche 	req->scmnd = NULL;
1606024ca901SBart Van Assche 
1607e9684678SBart Van Assche 	spin_lock_irqsave(&target->lock, flags);
160876c75b25SBart Van Assche 	list_add(&req->list, &target->free_reqs);
1609695b8349SBart Van Assche 
1610695b8349SBart Van Assche err_unlock:
1611e9684678SBart Van Assche 	spin_unlock_irqrestore(&target->lock, flags);
161276c75b25SBart Van Assche 
1613a95cadb9SBart Van Assche 	if (in_scsi_eh)
1614a95cadb9SBart Van Assche 		mutex_unlock(&rport->mutex);
1615a95cadb9SBart Van Assche 
1616aef9ec39SRoland Dreier 	return SCSI_MLQUEUE_HOST_BUSY;
1617aef9ec39SRoland Dreier }
1618aef9ec39SRoland Dreier 
16194d73f95fSBart Van Assche /*
16204d73f95fSBart Van Assche  * Note: the resources allocated in this function are freed in
16214d73f95fSBart Van Assche  * srp_free_target_ib().
16224d73f95fSBart Van Assche  */
1623aef9ec39SRoland Dreier static int srp_alloc_iu_bufs(struct srp_target_port *target)
1624aef9ec39SRoland Dreier {
1625aef9ec39SRoland Dreier 	int i;
1626aef9ec39SRoland Dreier 
16274d73f95fSBart Van Assche 	target->rx_ring = kzalloc(target->queue_size * sizeof(*target->rx_ring),
16284d73f95fSBart Van Assche 				  GFP_KERNEL);
16294d73f95fSBart Van Assche 	if (!target->rx_ring)
16304d73f95fSBart Van Assche 		goto err_no_ring;
16314d73f95fSBart Van Assche 	target->tx_ring = kzalloc(target->queue_size * sizeof(*target->tx_ring),
16324d73f95fSBart Van Assche 				  GFP_KERNEL);
16334d73f95fSBart Van Assche 	if (!target->tx_ring)
16344d73f95fSBart Van Assche 		goto err_no_ring;
16354d73f95fSBart Van Assche 
16364d73f95fSBart Van Assche 	for (i = 0; i < target->queue_size; ++i) {
1637aef9ec39SRoland Dreier 		target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1638aef9ec39SRoland Dreier 						  target->max_ti_iu_len,
1639aef9ec39SRoland Dreier 						  GFP_KERNEL, DMA_FROM_DEVICE);
1640aef9ec39SRoland Dreier 		if (!target->rx_ring[i])
1641aef9ec39SRoland Dreier 			goto err;
1642aef9ec39SRoland Dreier 	}
1643aef9ec39SRoland Dreier 
16444d73f95fSBart Van Assche 	for (i = 0; i < target->queue_size; ++i) {
1645aef9ec39SRoland Dreier 		target->tx_ring[i] = srp_alloc_iu(target->srp_host,
164649248644SDavid Dillow 						  target->max_iu_len,
1647aef9ec39SRoland Dreier 						  GFP_KERNEL, DMA_TO_DEVICE);
1648aef9ec39SRoland Dreier 		if (!target->tx_ring[i])
1649aef9ec39SRoland Dreier 			goto err;
1650dcb4cb85SBart Van Assche 
1651dcb4cb85SBart Van Assche 		list_add(&target->tx_ring[i]->list, &target->free_tx);
1652aef9ec39SRoland Dreier 	}
1653aef9ec39SRoland Dreier 
1654aef9ec39SRoland Dreier 	return 0;
1655aef9ec39SRoland Dreier 
1656aef9ec39SRoland Dreier err:
16574d73f95fSBart Van Assche 	for (i = 0; i < target->queue_size; ++i) {
1658aef9ec39SRoland Dreier 		srp_free_iu(target->srp_host, target->rx_ring[i]);
16594d73f95fSBart Van Assche 		srp_free_iu(target->srp_host, target->tx_ring[i]);
1660aef9ec39SRoland Dreier 	}
1661aef9ec39SRoland Dreier 
16624d73f95fSBart Van Assche 
16634d73f95fSBart Van Assche err_no_ring:
16644d73f95fSBart Van Assche 	kfree(target->tx_ring);
16654d73f95fSBart Van Assche 	target->tx_ring = NULL;
16664d73f95fSBart Van Assche 	kfree(target->rx_ring);
16674d73f95fSBart Van Assche 	target->rx_ring = NULL;
1668aef9ec39SRoland Dreier 
1669aef9ec39SRoland Dreier 	return -ENOMEM;
1670aef9ec39SRoland Dreier }
1671aef9ec39SRoland Dreier 
1672c9b03c1aSBart Van Assche static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
1673c9b03c1aSBart Van Assche {
1674c9b03c1aSBart Van Assche 	uint64_t T_tr_ns, max_compl_time_ms;
1675c9b03c1aSBart Van Assche 	uint32_t rq_tmo_jiffies;
1676c9b03c1aSBart Van Assche 
1677c9b03c1aSBart Van Assche 	/*
1678c9b03c1aSBart Van Assche 	 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
1679c9b03c1aSBart Van Assche 	 * table 91), both the QP timeout and the retry count have to be set
1680c9b03c1aSBart Van Assche 	 * for RC QP's during the RTR to RTS transition.
1681c9b03c1aSBart Van Assche 	 */
1682c9b03c1aSBart Van Assche 	WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
1683c9b03c1aSBart Van Assche 		     (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
1684c9b03c1aSBart Van Assche 
1685c9b03c1aSBart Van Assche 	/*
1686c9b03c1aSBart Van Assche 	 * Set target->rq_tmo_jiffies to one second more than the largest time
1687c9b03c1aSBart Van Assche 	 * it can take before an error completion is generated. See also
1688c9b03c1aSBart Van Assche 	 * C9-140..142 in the IBTA spec for more information about how to
1689c9b03c1aSBart Van Assche 	 * convert the QP Local ACK Timeout value to nanoseconds.
1690c9b03c1aSBart Van Assche 	 */
1691c9b03c1aSBart Van Assche 	T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
1692c9b03c1aSBart Van Assche 	max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
1693c9b03c1aSBart Van Assche 	do_div(max_compl_time_ms, NSEC_PER_MSEC);
1694c9b03c1aSBart Van Assche 	rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
1695c9b03c1aSBart Van Assche 
1696c9b03c1aSBart Van Assche 	return rq_tmo_jiffies;
1697c9b03c1aSBart Van Assche }
1698c9b03c1aSBart Van Assche 
1699961e0be8SDavid Dillow static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1700961e0be8SDavid Dillow 			       struct srp_login_rsp *lrsp,
1701961e0be8SDavid Dillow 			       struct srp_target_port *target)
1702961e0be8SDavid Dillow {
1703961e0be8SDavid Dillow 	struct ib_qp_attr *qp_attr = NULL;
1704961e0be8SDavid Dillow 	int attr_mask = 0;
1705961e0be8SDavid Dillow 	int ret;
1706961e0be8SDavid Dillow 	int i;
1707961e0be8SDavid Dillow 
1708961e0be8SDavid Dillow 	if (lrsp->opcode == SRP_LOGIN_RSP) {
1709961e0be8SDavid Dillow 		target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1710961e0be8SDavid Dillow 		target->req_lim       = be32_to_cpu(lrsp->req_lim_delta);
1711961e0be8SDavid Dillow 
1712961e0be8SDavid Dillow 		/*
1713961e0be8SDavid Dillow 		 * Reserve credits for task management so we don't
1714961e0be8SDavid Dillow 		 * bounce requests back to the SCSI mid-layer.
1715961e0be8SDavid Dillow 		 */
1716961e0be8SDavid Dillow 		target->scsi_host->can_queue
1717961e0be8SDavid Dillow 			= min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1718961e0be8SDavid Dillow 			      target->scsi_host->can_queue);
17194d73f95fSBart Van Assche 		target->scsi_host->cmd_per_lun
17204d73f95fSBart Van Assche 			= min_t(int, target->scsi_host->can_queue,
17214d73f95fSBart Van Assche 				target->scsi_host->cmd_per_lun);
1722961e0be8SDavid Dillow 	} else {
1723961e0be8SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
1724961e0be8SDavid Dillow 			     PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1725961e0be8SDavid Dillow 		ret = -ECONNRESET;
1726961e0be8SDavid Dillow 		goto error;
1727961e0be8SDavid Dillow 	}
1728961e0be8SDavid Dillow 
17294d73f95fSBart Van Assche 	if (!target->rx_ring) {
1730961e0be8SDavid Dillow 		ret = srp_alloc_iu_bufs(target);
1731961e0be8SDavid Dillow 		if (ret)
1732961e0be8SDavid Dillow 			goto error;
1733961e0be8SDavid Dillow 	}
1734961e0be8SDavid Dillow 
1735961e0be8SDavid Dillow 	ret = -ENOMEM;
1736961e0be8SDavid Dillow 	qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1737961e0be8SDavid Dillow 	if (!qp_attr)
1738961e0be8SDavid Dillow 		goto error;
1739961e0be8SDavid Dillow 
1740961e0be8SDavid Dillow 	qp_attr->qp_state = IB_QPS_RTR;
1741961e0be8SDavid Dillow 	ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1742961e0be8SDavid Dillow 	if (ret)
1743961e0be8SDavid Dillow 		goto error_free;
1744961e0be8SDavid Dillow 
1745961e0be8SDavid Dillow 	ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1746961e0be8SDavid Dillow 	if (ret)
1747961e0be8SDavid Dillow 		goto error_free;
1748961e0be8SDavid Dillow 
17494d73f95fSBart Van Assche 	for (i = 0; i < target->queue_size; i++) {
1750961e0be8SDavid Dillow 		struct srp_iu *iu = target->rx_ring[i];
1751961e0be8SDavid Dillow 		ret = srp_post_recv(target, iu);
1752961e0be8SDavid Dillow 		if (ret)
1753961e0be8SDavid Dillow 			goto error_free;
1754961e0be8SDavid Dillow 	}
1755961e0be8SDavid Dillow 
1756961e0be8SDavid Dillow 	qp_attr->qp_state = IB_QPS_RTS;
1757961e0be8SDavid Dillow 	ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1758961e0be8SDavid Dillow 	if (ret)
1759961e0be8SDavid Dillow 		goto error_free;
1760961e0be8SDavid Dillow 
1761c9b03c1aSBart Van Assche 	target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
1762c9b03c1aSBart Van Assche 
1763961e0be8SDavid Dillow 	ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1764961e0be8SDavid Dillow 	if (ret)
1765961e0be8SDavid Dillow 		goto error_free;
1766961e0be8SDavid Dillow 
1767961e0be8SDavid Dillow 	ret = ib_send_cm_rtu(cm_id, NULL, 0);
1768961e0be8SDavid Dillow 
1769961e0be8SDavid Dillow error_free:
1770961e0be8SDavid Dillow 	kfree(qp_attr);
1771961e0be8SDavid Dillow 
1772961e0be8SDavid Dillow error:
1773961e0be8SDavid Dillow 	target->status = ret;
1774961e0be8SDavid Dillow }
1775961e0be8SDavid Dillow 
1776aef9ec39SRoland Dreier static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1777aef9ec39SRoland Dreier 			       struct ib_cm_event *event,
1778aef9ec39SRoland Dreier 			       struct srp_target_port *target)
1779aef9ec39SRoland Dreier {
17807aa54bd7SDavid Dillow 	struct Scsi_Host *shost = target->scsi_host;
1781aef9ec39SRoland Dreier 	struct ib_class_port_info *cpi;
1782aef9ec39SRoland Dreier 	int opcode;
1783aef9ec39SRoland Dreier 
1784aef9ec39SRoland Dreier 	switch (event->param.rej_rcvd.reason) {
1785aef9ec39SRoland Dreier 	case IB_CM_REJ_PORT_CM_REDIRECT:
1786aef9ec39SRoland Dreier 		cpi = event->param.rej_rcvd.ari;
1787aef9ec39SRoland Dreier 		target->path.dlid = cpi->redirect_lid;
1788aef9ec39SRoland Dreier 		target->path.pkey = cpi->redirect_pkey;
1789aef9ec39SRoland Dreier 		cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1790aef9ec39SRoland Dreier 		memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1791aef9ec39SRoland Dreier 
1792aef9ec39SRoland Dreier 		target->status = target->path.dlid ?
1793aef9ec39SRoland Dreier 			SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1794aef9ec39SRoland Dreier 		break;
1795aef9ec39SRoland Dreier 
1796aef9ec39SRoland Dreier 	case IB_CM_REJ_PORT_REDIRECT:
17975d7cbfd6SRoland Dreier 		if (srp_target_is_topspin(target)) {
1798aef9ec39SRoland Dreier 			/*
1799aef9ec39SRoland Dreier 			 * Topspin/Cisco SRP gateways incorrectly send
1800aef9ec39SRoland Dreier 			 * reject reason code 25 when they mean 24
1801aef9ec39SRoland Dreier 			 * (port redirect).
1802aef9ec39SRoland Dreier 			 */
1803aef9ec39SRoland Dreier 			memcpy(target->path.dgid.raw,
1804aef9ec39SRoland Dreier 			       event->param.rej_rcvd.ari, 16);
1805aef9ec39SRoland Dreier 
18067aa54bd7SDavid Dillow 			shost_printk(KERN_DEBUG, shost,
18077aa54bd7SDavid Dillow 				     PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1808aef9ec39SRoland Dreier 				     (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1809aef9ec39SRoland Dreier 				     (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1810aef9ec39SRoland Dreier 
1811aef9ec39SRoland Dreier 			target->status = SRP_PORT_REDIRECT;
1812aef9ec39SRoland Dreier 		} else {
18137aa54bd7SDavid Dillow 			shost_printk(KERN_WARNING, shost,
18147aa54bd7SDavid Dillow 				     "  REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1815aef9ec39SRoland Dreier 			target->status = -ECONNRESET;
1816aef9ec39SRoland Dreier 		}
1817aef9ec39SRoland Dreier 		break;
1818aef9ec39SRoland Dreier 
1819aef9ec39SRoland Dreier 	case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
18207aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, shost,
18217aa54bd7SDavid Dillow 			    "  REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1822aef9ec39SRoland Dreier 		target->status = -ECONNRESET;
1823aef9ec39SRoland Dreier 		break;
1824aef9ec39SRoland Dreier 
1825aef9ec39SRoland Dreier 	case IB_CM_REJ_CONSUMER_DEFINED:
1826aef9ec39SRoland Dreier 		opcode = *(u8 *) event->private_data;
1827aef9ec39SRoland Dreier 		if (opcode == SRP_LOGIN_REJ) {
1828aef9ec39SRoland Dreier 			struct srp_login_rej *rej = event->private_data;
1829aef9ec39SRoland Dreier 			u32 reason = be32_to_cpu(rej->reason);
1830aef9ec39SRoland Dreier 
1831aef9ec39SRoland Dreier 			if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
18327aa54bd7SDavid Dillow 				shost_printk(KERN_WARNING, shost,
18337aa54bd7SDavid Dillow 					     PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1834aef9ec39SRoland Dreier 			else
1835e7ffde01SBart Van Assche 				shost_printk(KERN_WARNING, shost, PFX
1836e7ffde01SBart Van Assche 					     "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n",
1837e7ffde01SBart Van Assche 					     target->path.sgid.raw,
1838e7ffde01SBart Van Assche 					     target->orig_dgid, reason);
1839aef9ec39SRoland Dreier 		} else
18407aa54bd7SDavid Dillow 			shost_printk(KERN_WARNING, shost,
18417aa54bd7SDavid Dillow 				     "  REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1842aef9ec39SRoland Dreier 				     " opcode 0x%02x\n", opcode);
1843aef9ec39SRoland Dreier 		target->status = -ECONNRESET;
1844aef9ec39SRoland Dreier 		break;
1845aef9ec39SRoland Dreier 
18469fe4bcf4SDavid Dillow 	case IB_CM_REJ_STALE_CONN:
18479fe4bcf4SDavid Dillow 		shost_printk(KERN_WARNING, shost, "  REJ reason: stale connection\n");
18489fe4bcf4SDavid Dillow 		target->status = SRP_STALE_CONN;
18499fe4bcf4SDavid Dillow 		break;
18509fe4bcf4SDavid Dillow 
1851aef9ec39SRoland Dreier 	default:
18527aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, shost, "  REJ reason 0x%x\n",
1853aef9ec39SRoland Dreier 			     event->param.rej_rcvd.reason);
1854aef9ec39SRoland Dreier 		target->status = -ECONNRESET;
1855aef9ec39SRoland Dreier 	}
1856aef9ec39SRoland Dreier }
1857aef9ec39SRoland Dreier 
1858aef9ec39SRoland Dreier static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1859aef9ec39SRoland Dreier {
1860aef9ec39SRoland Dreier 	struct srp_target_port *target = cm_id->context;
1861aef9ec39SRoland Dreier 	int comp = 0;
1862aef9ec39SRoland Dreier 
1863aef9ec39SRoland Dreier 	switch (event->event) {
1864aef9ec39SRoland Dreier 	case IB_CM_REQ_ERROR:
18657aa54bd7SDavid Dillow 		shost_printk(KERN_DEBUG, target->scsi_host,
18667aa54bd7SDavid Dillow 			     PFX "Sending CM REQ failed\n");
1867aef9ec39SRoland Dreier 		comp = 1;
1868aef9ec39SRoland Dreier 		target->status = -ECONNRESET;
1869aef9ec39SRoland Dreier 		break;
1870aef9ec39SRoland Dreier 
1871aef9ec39SRoland Dreier 	case IB_CM_REP_RECEIVED:
1872aef9ec39SRoland Dreier 		comp = 1;
1873961e0be8SDavid Dillow 		srp_cm_rep_handler(cm_id, event->private_data, target);
1874aef9ec39SRoland Dreier 		break;
1875aef9ec39SRoland Dreier 
1876aef9ec39SRoland Dreier 	case IB_CM_REJ_RECEIVED:
18777aa54bd7SDavid Dillow 		shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
1878aef9ec39SRoland Dreier 		comp = 1;
1879aef9ec39SRoland Dreier 
1880aef9ec39SRoland Dreier 		srp_cm_rej_handler(cm_id, event, target);
1881aef9ec39SRoland Dreier 		break;
1882aef9ec39SRoland Dreier 
1883b7ac4ab4SIshai Rabinovitz 	case IB_CM_DREQ_RECEIVED:
18847aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
18857aa54bd7SDavid Dillow 			     PFX "DREQ received - connection closed\n");
1886294c875aSBart Van Assche 		srp_change_conn_state(target, false);
1887b7ac4ab4SIshai Rabinovitz 		if (ib_send_cm_drep(cm_id, NULL, 0))
18887aa54bd7SDavid Dillow 			shost_printk(KERN_ERR, target->scsi_host,
18897aa54bd7SDavid Dillow 				     PFX "Sending CM DREP failed\n");
1890c1120f89SBart Van Assche 		queue_work(system_long_wq, &target->tl_err_work);
1891aef9ec39SRoland Dreier 		break;
1892aef9ec39SRoland Dreier 
1893aef9ec39SRoland Dreier 	case IB_CM_TIMEWAIT_EXIT:
18947aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
18957aa54bd7SDavid Dillow 			     PFX "connection closed\n");
1896ac72d766SBart Van Assche 		comp = 1;
1897aef9ec39SRoland Dreier 
1898aef9ec39SRoland Dreier 		target->status = 0;
1899aef9ec39SRoland Dreier 		break;
1900aef9ec39SRoland Dreier 
1901b7ac4ab4SIshai Rabinovitz 	case IB_CM_MRA_RECEIVED:
1902b7ac4ab4SIshai Rabinovitz 	case IB_CM_DREQ_ERROR:
1903b7ac4ab4SIshai Rabinovitz 	case IB_CM_DREP_RECEIVED:
1904b7ac4ab4SIshai Rabinovitz 		break;
1905b7ac4ab4SIshai Rabinovitz 
1906aef9ec39SRoland Dreier 	default:
19077aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
19087aa54bd7SDavid Dillow 			     PFX "Unhandled CM event %d\n", event->event);
1909aef9ec39SRoland Dreier 		break;
1910aef9ec39SRoland Dreier 	}
1911aef9ec39SRoland Dreier 
1912aef9ec39SRoland Dreier 	if (comp)
1913aef9ec39SRoland Dreier 		complete(&target->done);
1914aef9ec39SRoland Dreier 
1915aef9ec39SRoland Dreier 	return 0;
1916aef9ec39SRoland Dreier }
1917aef9ec39SRoland Dreier 
191871444b97SJack Wang /**
191971444b97SJack Wang  * srp_change_queue_type - changing device queue tag type
192071444b97SJack Wang  * @sdev: scsi device struct
192171444b97SJack Wang  * @tag_type: requested tag type
192271444b97SJack Wang  *
192371444b97SJack Wang  * Returns queue tag type.
192471444b97SJack Wang  */
192571444b97SJack Wang static int
192671444b97SJack Wang srp_change_queue_type(struct scsi_device *sdev, int tag_type)
192771444b97SJack Wang {
192871444b97SJack Wang 	if (sdev->tagged_supported) {
192971444b97SJack Wang 		scsi_set_tag_type(sdev, tag_type);
193071444b97SJack Wang 		if (tag_type)
193171444b97SJack Wang 			scsi_activate_tcq(sdev, sdev->queue_depth);
193271444b97SJack Wang 		else
193371444b97SJack Wang 			scsi_deactivate_tcq(sdev, sdev->queue_depth);
193471444b97SJack Wang 	} else
193571444b97SJack Wang 		tag_type = 0;
193671444b97SJack Wang 
193771444b97SJack Wang 	return tag_type;
193871444b97SJack Wang }
193971444b97SJack Wang 
194071444b97SJack Wang /**
194171444b97SJack Wang  * srp_change_queue_depth - setting device queue depth
194271444b97SJack Wang  * @sdev: scsi device struct
194371444b97SJack Wang  * @qdepth: requested queue depth
194471444b97SJack Wang  * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
194571444b97SJack Wang  * (see include/scsi/scsi_host.h for definition)
194671444b97SJack Wang  *
194771444b97SJack Wang  * Returns queue depth.
194871444b97SJack Wang  */
194971444b97SJack Wang static int
195071444b97SJack Wang srp_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
195171444b97SJack Wang {
195271444b97SJack Wang 	struct Scsi_Host *shost = sdev->host;
195371444b97SJack Wang 	int max_depth;
195471444b97SJack Wang 	if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) {
195571444b97SJack Wang 		max_depth = shost->can_queue;
195671444b97SJack Wang 		if (!sdev->tagged_supported)
195771444b97SJack Wang 			max_depth = 1;
195871444b97SJack Wang 		if (qdepth > max_depth)
195971444b97SJack Wang 			qdepth = max_depth;
196071444b97SJack Wang 		scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
196171444b97SJack Wang 	} else if (reason == SCSI_QDEPTH_QFULL)
196271444b97SJack Wang 		scsi_track_queue_full(sdev, qdepth);
196371444b97SJack Wang 	else
196471444b97SJack Wang 		return -EOPNOTSUPP;
196571444b97SJack Wang 
196671444b97SJack Wang 	return sdev->queue_depth;
196771444b97SJack Wang }
196871444b97SJack Wang 
1969d945e1dfSRoland Dreier static int srp_send_tsk_mgmt(struct srp_target_port *target,
1970f8b6e31eSDavid Dillow 			     u64 req_tag, unsigned int lun, u8 func)
1971aef9ec39SRoland Dreier {
1972a95cadb9SBart Van Assche 	struct srp_rport *rport = target->rport;
197319081f31SDavid Dillow 	struct ib_device *dev = target->srp_host->srp_dev->dev;
1974aef9ec39SRoland Dreier 	struct srp_iu *iu;
1975aef9ec39SRoland Dreier 	struct srp_tsk_mgmt *tsk_mgmt;
1976aef9ec39SRoland Dreier 
19773780d1f0SBart Van Assche 	if (!target->connected || target->qp_in_error)
19783780d1f0SBart Van Assche 		return -1;
19793780d1f0SBart Van Assche 
1980f8b6e31eSDavid Dillow 	init_completion(&target->tsk_mgmt_done);
1981aef9ec39SRoland Dreier 
1982a95cadb9SBart Van Assche 	/*
1983a95cadb9SBart Van Assche 	 * Lock the rport mutex to avoid that srp_create_target_ib() is
1984a95cadb9SBart Van Assche 	 * invoked while a task management function is being sent.
1985a95cadb9SBart Van Assche 	 */
1986a95cadb9SBart Van Assche 	mutex_lock(&rport->mutex);
1987e9684678SBart Van Assche 	spin_lock_irq(&target->lock);
1988bb12588aSDavid Dillow 	iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
1989e9684678SBart Van Assche 	spin_unlock_irq(&target->lock);
199076c75b25SBart Van Assche 
1991a95cadb9SBart Van Assche 	if (!iu) {
1992a95cadb9SBart Van Assche 		mutex_unlock(&rport->mutex);
1993a95cadb9SBart Van Assche 
199476c75b25SBart Van Assche 		return -1;
1995a95cadb9SBart Van Assche 	}
1996aef9ec39SRoland Dreier 
199719081f31SDavid Dillow 	ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
199819081f31SDavid Dillow 				   DMA_TO_DEVICE);
1999aef9ec39SRoland Dreier 	tsk_mgmt = iu->buf;
2000aef9ec39SRoland Dreier 	memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
2001aef9ec39SRoland Dreier 
2002aef9ec39SRoland Dreier 	tsk_mgmt->opcode 	= SRP_TSK_MGMT;
2003f8b6e31eSDavid Dillow 	tsk_mgmt->lun		= cpu_to_be64((u64) lun << 48);
2004f8b6e31eSDavid Dillow 	tsk_mgmt->tag		= req_tag | SRP_TAG_TSK_MGMT;
2005aef9ec39SRoland Dreier 	tsk_mgmt->tsk_mgmt_func = func;
2006f8b6e31eSDavid Dillow 	tsk_mgmt->task_tag	= req_tag;
2007aef9ec39SRoland Dreier 
200819081f31SDavid Dillow 	ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
200919081f31SDavid Dillow 				      DMA_TO_DEVICE);
201076c75b25SBart Van Assche 	if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
201176c75b25SBart Van Assche 		srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
2012a95cadb9SBart Van Assche 		mutex_unlock(&rport->mutex);
2013a95cadb9SBart Van Assche 
201476c75b25SBart Van Assche 		return -1;
201576c75b25SBart Van Assche 	}
2016a95cadb9SBart Van Assche 	mutex_unlock(&rport->mutex);
2017d945e1dfSRoland Dreier 
2018f8b6e31eSDavid Dillow 	if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
2019aef9ec39SRoland Dreier 					 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
2020d945e1dfSRoland Dreier 		return -1;
2021aef9ec39SRoland Dreier 
2022d945e1dfSRoland Dreier 	return 0;
2023d945e1dfSRoland Dreier }
2024d945e1dfSRoland Dreier 
2025aef9ec39SRoland Dreier static int srp_abort(struct scsi_cmnd *scmnd)
2026aef9ec39SRoland Dreier {
2027d945e1dfSRoland Dreier 	struct srp_target_port *target = host_to_target(scmnd->device->host);
2028f8b6e31eSDavid Dillow 	struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
2029086f44f5SBart Van Assche 	int ret;
2030d945e1dfSRoland Dreier 
20317aa54bd7SDavid Dillow 	shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
2032aef9ec39SRoland Dreier 
2033b3fe628dSBart Van Assche 	if (!req || !srp_claim_req(target, req, NULL, scmnd))
203499b6697aSBart Van Assche 		return SUCCESS;
2035086f44f5SBart Van Assche 	if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
203680d5e8a2SBart Van Assche 			      SRP_TSK_ABORT_TASK) == 0)
2037086f44f5SBart Van Assche 		ret = SUCCESS;
2038ed9b2264SBart Van Assche 	else if (target->rport->state == SRP_RPORT_LOST)
203999e1c139SBart Van Assche 		ret = FAST_IO_FAIL;
2040086f44f5SBart Van Assche 	else
2041086f44f5SBart Van Assche 		ret = FAILED;
204222032991SBart Van Assche 	srp_free_req(target, req, scmnd, 0);
2043d945e1dfSRoland Dreier 	scmnd->result = DID_ABORT << 16;
2044d8536670SBart Van Assche 	scmnd->scsi_done(scmnd);
2045d945e1dfSRoland Dreier 
2046086f44f5SBart Van Assche 	return ret;
2047aef9ec39SRoland Dreier }
2048aef9ec39SRoland Dreier 
2049aef9ec39SRoland Dreier static int srp_reset_device(struct scsi_cmnd *scmnd)
2050aef9ec39SRoland Dreier {
2051d945e1dfSRoland Dreier 	struct srp_target_port *target = host_to_target(scmnd->device->host);
2052536ae14eSBart Van Assche 	int i;
2053d945e1dfSRoland Dreier 
20547aa54bd7SDavid Dillow 	shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
2055aef9ec39SRoland Dreier 
2056f8b6e31eSDavid Dillow 	if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
2057f8b6e31eSDavid Dillow 			      SRP_TSK_LUN_RESET))
2058d945e1dfSRoland Dreier 		return FAILED;
2059f8b6e31eSDavid Dillow 	if (target->tsk_mgmt_status)
2060d945e1dfSRoland Dreier 		return FAILED;
2061d945e1dfSRoland Dreier 
20624d73f95fSBart Van Assche 	for (i = 0; i < target->req_ring_size; ++i) {
2063536ae14eSBart Van Assche 		struct srp_request *req = &target->req_ring[i];
2064b3fe628dSBart Van Assche 		srp_finish_req(target, req, scmnd->device, DID_RESET << 16);
2065536ae14eSBart Van Assche 	}
2066d945e1dfSRoland Dreier 
2067d945e1dfSRoland Dreier 	return SUCCESS;
2068aef9ec39SRoland Dreier }
2069aef9ec39SRoland Dreier 
2070aef9ec39SRoland Dreier static int srp_reset_host(struct scsi_cmnd *scmnd)
2071aef9ec39SRoland Dreier {
2072aef9ec39SRoland Dreier 	struct srp_target_port *target = host_to_target(scmnd->device->host);
2073aef9ec39SRoland Dreier 
20747aa54bd7SDavid Dillow 	shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
2075aef9ec39SRoland Dreier 
2076ed9b2264SBart Van Assche 	return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
2077aef9ec39SRoland Dreier }
2078aef9ec39SRoland Dreier 
2079c9b03c1aSBart Van Assche static int srp_slave_configure(struct scsi_device *sdev)
2080c9b03c1aSBart Van Assche {
2081c9b03c1aSBart Van Assche 	struct Scsi_Host *shost = sdev->host;
2082c9b03c1aSBart Van Assche 	struct srp_target_port *target = host_to_target(shost);
2083c9b03c1aSBart Van Assche 	struct request_queue *q = sdev->request_queue;
2084c9b03c1aSBart Van Assche 	unsigned long timeout;
2085c9b03c1aSBart Van Assche 
2086c9b03c1aSBart Van Assche 	if (sdev->type == TYPE_DISK) {
2087c9b03c1aSBart Van Assche 		timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
2088c9b03c1aSBart Van Assche 		blk_queue_rq_timeout(q, timeout);
2089c9b03c1aSBart Van Assche 	}
2090c9b03c1aSBart Van Assche 
2091c9b03c1aSBart Van Assche 	return 0;
2092c9b03c1aSBart Van Assche }
2093c9b03c1aSBart Van Assche 
2094ee959b00STony Jones static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
2095ee959b00STony Jones 			   char *buf)
20966ecb0c84SRoland Dreier {
2097ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
20986ecb0c84SRoland Dreier 
20996ecb0c84SRoland Dreier 	return sprintf(buf, "0x%016llx\n",
21006ecb0c84SRoland Dreier 		       (unsigned long long) be64_to_cpu(target->id_ext));
21016ecb0c84SRoland Dreier }
21026ecb0c84SRoland Dreier 
2103ee959b00STony Jones static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
2104ee959b00STony Jones 			     char *buf)
21056ecb0c84SRoland Dreier {
2106ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
21076ecb0c84SRoland Dreier 
21086ecb0c84SRoland Dreier 	return sprintf(buf, "0x%016llx\n",
21096ecb0c84SRoland Dreier 		       (unsigned long long) be64_to_cpu(target->ioc_guid));
21106ecb0c84SRoland Dreier }
21116ecb0c84SRoland Dreier 
2112ee959b00STony Jones static ssize_t show_service_id(struct device *dev,
2113ee959b00STony Jones 			       struct device_attribute *attr, char *buf)
21146ecb0c84SRoland Dreier {
2115ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
21166ecb0c84SRoland Dreier 
21176ecb0c84SRoland Dreier 	return sprintf(buf, "0x%016llx\n",
21186ecb0c84SRoland Dreier 		       (unsigned long long) be64_to_cpu(target->service_id));
21196ecb0c84SRoland Dreier }
21206ecb0c84SRoland Dreier 
2121ee959b00STony Jones static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
2122ee959b00STony Jones 			 char *buf)
21236ecb0c84SRoland Dreier {
2124ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
21256ecb0c84SRoland Dreier 
21266ecb0c84SRoland Dreier 	return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
21276ecb0c84SRoland Dreier }
21286ecb0c84SRoland Dreier 
2129848b3082SBart Van Assche static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
2130848b3082SBart Van Assche 			 char *buf)
2131848b3082SBart Van Assche {
2132848b3082SBart Van Assche 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2133848b3082SBart Van Assche 
2134848b3082SBart Van Assche 	return sprintf(buf, "%pI6\n", target->path.sgid.raw);
2135848b3082SBart Van Assche }
2136848b3082SBart Van Assche 
2137ee959b00STony Jones static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
2138ee959b00STony Jones 			 char *buf)
21396ecb0c84SRoland Dreier {
2140ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
21416ecb0c84SRoland Dreier 
21425b095d98SHarvey Harrison 	return sprintf(buf, "%pI6\n", target->path.dgid.raw);
21436ecb0c84SRoland Dreier }
21446ecb0c84SRoland Dreier 
2145ee959b00STony Jones static ssize_t show_orig_dgid(struct device *dev,
2146ee959b00STony Jones 			      struct device_attribute *attr, char *buf)
21473633b3d0SIshai Rabinovitz {
2148ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
21493633b3d0SIshai Rabinovitz 
21505b095d98SHarvey Harrison 	return sprintf(buf, "%pI6\n", target->orig_dgid);
21513633b3d0SIshai Rabinovitz }
21523633b3d0SIshai Rabinovitz 
215389de7486SBart Van Assche static ssize_t show_req_lim(struct device *dev,
215489de7486SBart Van Assche 			    struct device_attribute *attr, char *buf)
215589de7486SBart Van Assche {
215689de7486SBart Van Assche 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
215789de7486SBart Van Assche 
215889de7486SBart Van Assche 	return sprintf(buf, "%d\n", target->req_lim);
215989de7486SBart Van Assche }
216089de7486SBart Van Assche 
2161ee959b00STony Jones static ssize_t show_zero_req_lim(struct device *dev,
2162ee959b00STony Jones 				 struct device_attribute *attr, char *buf)
21636bfa24faSRoland Dreier {
2164ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
21656bfa24faSRoland Dreier 
21666bfa24faSRoland Dreier 	return sprintf(buf, "%d\n", target->zero_req_lim);
21676bfa24faSRoland Dreier }
21686bfa24faSRoland Dreier 
2169ee959b00STony Jones static ssize_t show_local_ib_port(struct device *dev,
2170ee959b00STony Jones 				  struct device_attribute *attr, char *buf)
2171ded7f1a1SIshai Rabinovitz {
2172ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2173ded7f1a1SIshai Rabinovitz 
2174ded7f1a1SIshai Rabinovitz 	return sprintf(buf, "%d\n", target->srp_host->port);
2175ded7f1a1SIshai Rabinovitz }
2176ded7f1a1SIshai Rabinovitz 
2177ee959b00STony Jones static ssize_t show_local_ib_device(struct device *dev,
2178ee959b00STony Jones 				    struct device_attribute *attr, char *buf)
2179ded7f1a1SIshai Rabinovitz {
2180ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2181ded7f1a1SIshai Rabinovitz 
218205321937SGreg Kroah-Hartman 	return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
2183ded7f1a1SIshai Rabinovitz }
2184ded7f1a1SIshai Rabinovitz 
21854b5e5f41SBart Van Assche static ssize_t show_comp_vector(struct device *dev,
21864b5e5f41SBart Van Assche 				struct device_attribute *attr, char *buf)
21874b5e5f41SBart Van Assche {
21884b5e5f41SBart Van Assche 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
21894b5e5f41SBart Van Assche 
21904b5e5f41SBart Van Assche 	return sprintf(buf, "%d\n", target->comp_vector);
21914b5e5f41SBart Van Assche }
21924b5e5f41SBart Van Assche 
21937bb312e4SVu Pham static ssize_t show_tl_retry_count(struct device *dev,
21947bb312e4SVu Pham 				   struct device_attribute *attr, char *buf)
21957bb312e4SVu Pham {
21967bb312e4SVu Pham 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
21977bb312e4SVu Pham 
21987bb312e4SVu Pham 	return sprintf(buf, "%d\n", target->tl_retry_count);
21997bb312e4SVu Pham }
22007bb312e4SVu Pham 
220149248644SDavid Dillow static ssize_t show_cmd_sg_entries(struct device *dev,
220249248644SDavid Dillow 				   struct device_attribute *attr, char *buf)
220349248644SDavid Dillow {
220449248644SDavid Dillow 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
220549248644SDavid Dillow 
220649248644SDavid Dillow 	return sprintf(buf, "%u\n", target->cmd_sg_cnt);
220749248644SDavid Dillow }
220849248644SDavid Dillow 
2209c07d424dSDavid Dillow static ssize_t show_allow_ext_sg(struct device *dev,
2210c07d424dSDavid Dillow 				 struct device_attribute *attr, char *buf)
2211c07d424dSDavid Dillow {
2212c07d424dSDavid Dillow 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
2213c07d424dSDavid Dillow 
2214c07d424dSDavid Dillow 	return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
2215c07d424dSDavid Dillow }
2216c07d424dSDavid Dillow 
2217ee959b00STony Jones static DEVICE_ATTR(id_ext,	    S_IRUGO, show_id_ext,	   NULL);
2218ee959b00STony Jones static DEVICE_ATTR(ioc_guid,	    S_IRUGO, show_ioc_guid,	   NULL);
2219ee959b00STony Jones static DEVICE_ATTR(service_id,	    S_IRUGO, show_service_id,	   NULL);
2220ee959b00STony Jones static DEVICE_ATTR(pkey,	    S_IRUGO, show_pkey,		   NULL);
2221848b3082SBart Van Assche static DEVICE_ATTR(sgid,	    S_IRUGO, show_sgid,		   NULL);
2222ee959b00STony Jones static DEVICE_ATTR(dgid,	    S_IRUGO, show_dgid,		   NULL);
2223ee959b00STony Jones static DEVICE_ATTR(orig_dgid,	    S_IRUGO, show_orig_dgid,	   NULL);
222489de7486SBart Van Assche static DEVICE_ATTR(req_lim,         S_IRUGO, show_req_lim,         NULL);
2225ee959b00STony Jones static DEVICE_ATTR(zero_req_lim,    S_IRUGO, show_zero_req_lim,	   NULL);
2226ee959b00STony Jones static DEVICE_ATTR(local_ib_port,   S_IRUGO, show_local_ib_port,   NULL);
2227ee959b00STony Jones static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
22284b5e5f41SBart Van Assche static DEVICE_ATTR(comp_vector,     S_IRUGO, show_comp_vector,     NULL);
22297bb312e4SVu Pham static DEVICE_ATTR(tl_retry_count,  S_IRUGO, show_tl_retry_count,  NULL);
223049248644SDavid Dillow static DEVICE_ATTR(cmd_sg_entries,  S_IRUGO, show_cmd_sg_entries,  NULL);
2231c07d424dSDavid Dillow static DEVICE_ATTR(allow_ext_sg,    S_IRUGO, show_allow_ext_sg,    NULL);
22326ecb0c84SRoland Dreier 
2233ee959b00STony Jones static struct device_attribute *srp_host_attrs[] = {
2234ee959b00STony Jones 	&dev_attr_id_ext,
2235ee959b00STony Jones 	&dev_attr_ioc_guid,
2236ee959b00STony Jones 	&dev_attr_service_id,
2237ee959b00STony Jones 	&dev_attr_pkey,
2238848b3082SBart Van Assche 	&dev_attr_sgid,
2239ee959b00STony Jones 	&dev_attr_dgid,
2240ee959b00STony Jones 	&dev_attr_orig_dgid,
224189de7486SBart Van Assche 	&dev_attr_req_lim,
2242ee959b00STony Jones 	&dev_attr_zero_req_lim,
2243ee959b00STony Jones 	&dev_attr_local_ib_port,
2244ee959b00STony Jones 	&dev_attr_local_ib_device,
22454b5e5f41SBart Van Assche 	&dev_attr_comp_vector,
22467bb312e4SVu Pham 	&dev_attr_tl_retry_count,
224749248644SDavid Dillow 	&dev_attr_cmd_sg_entries,
2248c07d424dSDavid Dillow 	&dev_attr_allow_ext_sg,
22496ecb0c84SRoland Dreier 	NULL
22506ecb0c84SRoland Dreier };
22516ecb0c84SRoland Dreier 
2252aef9ec39SRoland Dreier static struct scsi_host_template srp_template = {
2253aef9ec39SRoland Dreier 	.module				= THIS_MODULE,
2254b7f008fdSRoland Dreier 	.name				= "InfiniBand SRP initiator",
2255b7f008fdSRoland Dreier 	.proc_name			= DRV_NAME,
2256c9b03c1aSBart Van Assche 	.slave_configure		= srp_slave_configure,
2257aef9ec39SRoland Dreier 	.info				= srp_target_info,
2258aef9ec39SRoland Dreier 	.queuecommand			= srp_queuecommand,
225971444b97SJack Wang 	.change_queue_depth             = srp_change_queue_depth,
226071444b97SJack Wang 	.change_queue_type              = srp_change_queue_type,
2261aef9ec39SRoland Dreier 	.eh_abort_handler		= srp_abort,
2262aef9ec39SRoland Dreier 	.eh_device_reset_handler	= srp_reset_device,
2263aef9ec39SRoland Dreier 	.eh_host_reset_handler		= srp_reset_host,
22642742c1daSBart Van Assche 	.skip_settle_delay		= true,
226549248644SDavid Dillow 	.sg_tablesize			= SRP_DEF_SG_TABLESIZE,
22664d73f95fSBart Van Assche 	.can_queue			= SRP_DEFAULT_CMD_SQ_SIZE,
2267aef9ec39SRoland Dreier 	.this_id			= -1,
22684d73f95fSBart Van Assche 	.cmd_per_lun			= SRP_DEFAULT_CMD_SQ_SIZE,
22696ecb0c84SRoland Dreier 	.use_clustering			= ENABLE_CLUSTERING,
22706ecb0c84SRoland Dreier 	.shost_attrs			= srp_host_attrs
2271aef9ec39SRoland Dreier };
2272aef9ec39SRoland Dreier 
2273aef9ec39SRoland Dreier static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
2274aef9ec39SRoland Dreier {
22753236822bSFUJITA Tomonori 	struct srp_rport_identifiers ids;
22763236822bSFUJITA Tomonori 	struct srp_rport *rport;
22773236822bSFUJITA Tomonori 
2278aef9ec39SRoland Dreier 	sprintf(target->target_name, "SRP.T10:%016llX",
2279aef9ec39SRoland Dreier 		 (unsigned long long) be64_to_cpu(target->id_ext));
2280aef9ec39SRoland Dreier 
228105321937SGreg Kroah-Hartman 	if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
2282aef9ec39SRoland Dreier 		return -ENODEV;
2283aef9ec39SRoland Dreier 
22843236822bSFUJITA Tomonori 	memcpy(ids.port_id, &target->id_ext, 8);
22853236822bSFUJITA Tomonori 	memcpy(ids.port_id + 8, &target->ioc_guid, 8);
2286aebd5e47SFUJITA Tomonori 	ids.roles = SRP_RPORT_ROLE_TARGET;
22873236822bSFUJITA Tomonori 	rport = srp_rport_add(target->scsi_host, &ids);
22883236822bSFUJITA Tomonori 	if (IS_ERR(rport)) {
22893236822bSFUJITA Tomonori 		scsi_remove_host(target->scsi_host);
22903236822bSFUJITA Tomonori 		return PTR_ERR(rport);
22913236822bSFUJITA Tomonori 	}
22923236822bSFUJITA Tomonori 
2293dc1bdbd9SBart Van Assche 	rport->lld_data = target;
22949dd69a60SBart Van Assche 	target->rport = rport;
2295dc1bdbd9SBart Van Assche 
2296b3589fd4SMatthew Wilcox 	spin_lock(&host->target_lock);
2297aef9ec39SRoland Dreier 	list_add_tail(&target->list, &host->target_list);
2298b3589fd4SMatthew Wilcox 	spin_unlock(&host->target_lock);
2299aef9ec39SRoland Dreier 
2300aef9ec39SRoland Dreier 	target->state = SRP_TARGET_LIVE;
2301aef9ec39SRoland Dreier 
2302aef9ec39SRoland Dreier 	scsi_scan_target(&target->scsi_host->shost_gendev,
23031962a4a1SMatthew Wilcox 			 0, target->scsi_id, SCAN_WILD_CARD, 0);
2304aef9ec39SRoland Dreier 
2305aef9ec39SRoland Dreier 	return 0;
2306aef9ec39SRoland Dreier }
2307aef9ec39SRoland Dreier 
2308ee959b00STony Jones static void srp_release_dev(struct device *dev)
2309aef9ec39SRoland Dreier {
2310aef9ec39SRoland Dreier 	struct srp_host *host =
2311ee959b00STony Jones 		container_of(dev, struct srp_host, dev);
2312aef9ec39SRoland Dreier 
2313aef9ec39SRoland Dreier 	complete(&host->released);
2314aef9ec39SRoland Dreier }
2315aef9ec39SRoland Dreier 
2316aef9ec39SRoland Dreier static struct class srp_class = {
2317aef9ec39SRoland Dreier 	.name    = "infiniband_srp",
2318ee959b00STony Jones 	.dev_release = srp_release_dev
2319aef9ec39SRoland Dreier };
2320aef9ec39SRoland Dreier 
232196fc248aSBart Van Assche /**
232296fc248aSBart Van Assche  * srp_conn_unique() - check whether the connection to a target is unique
2323af24663bSBart Van Assche  * @host:   SRP host.
2324af24663bSBart Van Assche  * @target: SRP target port.
232596fc248aSBart Van Assche  */
232696fc248aSBart Van Assche static bool srp_conn_unique(struct srp_host *host,
232796fc248aSBart Van Assche 			    struct srp_target_port *target)
232896fc248aSBart Van Assche {
232996fc248aSBart Van Assche 	struct srp_target_port *t;
233096fc248aSBart Van Assche 	bool ret = false;
233196fc248aSBart Van Assche 
233296fc248aSBart Van Assche 	if (target->state == SRP_TARGET_REMOVED)
233396fc248aSBart Van Assche 		goto out;
233496fc248aSBart Van Assche 
233596fc248aSBart Van Assche 	ret = true;
233696fc248aSBart Van Assche 
233796fc248aSBart Van Assche 	spin_lock(&host->target_lock);
233896fc248aSBart Van Assche 	list_for_each_entry(t, &host->target_list, list) {
233996fc248aSBart Van Assche 		if (t != target &&
234096fc248aSBart Van Assche 		    target->id_ext == t->id_ext &&
234196fc248aSBart Van Assche 		    target->ioc_guid == t->ioc_guid &&
234296fc248aSBart Van Assche 		    target->initiator_ext == t->initiator_ext) {
234396fc248aSBart Van Assche 			ret = false;
234496fc248aSBart Van Assche 			break;
234596fc248aSBart Van Assche 		}
234696fc248aSBart Van Assche 	}
234796fc248aSBart Van Assche 	spin_unlock(&host->target_lock);
234896fc248aSBart Van Assche 
234996fc248aSBart Van Assche out:
235096fc248aSBart Van Assche 	return ret;
235196fc248aSBart Van Assche }
235296fc248aSBart Van Assche 
2353aef9ec39SRoland Dreier /*
2354aef9ec39SRoland Dreier  * Target ports are added by writing
2355aef9ec39SRoland Dreier  *
2356aef9ec39SRoland Dreier  *     id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2357aef9ec39SRoland Dreier  *     pkey=<P_Key>,service_id=<service ID>
2358aef9ec39SRoland Dreier  *
2359aef9ec39SRoland Dreier  * to the add_target sysfs attribute.
2360aef9ec39SRoland Dreier  */
2361aef9ec39SRoland Dreier enum {
2362aef9ec39SRoland Dreier 	SRP_OPT_ERR		= 0,
2363aef9ec39SRoland Dreier 	SRP_OPT_ID_EXT		= 1 << 0,
2364aef9ec39SRoland Dreier 	SRP_OPT_IOC_GUID	= 1 << 1,
2365aef9ec39SRoland Dreier 	SRP_OPT_DGID		= 1 << 2,
2366aef9ec39SRoland Dreier 	SRP_OPT_PKEY		= 1 << 3,
2367aef9ec39SRoland Dreier 	SRP_OPT_SERVICE_ID	= 1 << 4,
2368aef9ec39SRoland Dreier 	SRP_OPT_MAX_SECT	= 1 << 5,
236952fb2b50SVu Pham 	SRP_OPT_MAX_CMD_PER_LUN	= 1 << 6,
23700c0450dbSRamachandra K 	SRP_OPT_IO_CLASS	= 1 << 7,
237101cb9bcbSIshai Rabinovitz 	SRP_OPT_INITIATOR_EXT	= 1 << 8,
237249248644SDavid Dillow 	SRP_OPT_CMD_SG_ENTRIES	= 1 << 9,
2373c07d424dSDavid Dillow 	SRP_OPT_ALLOW_EXT_SG	= 1 << 10,
2374c07d424dSDavid Dillow 	SRP_OPT_SG_TABLESIZE	= 1 << 11,
23754b5e5f41SBart Van Assche 	SRP_OPT_COMP_VECTOR	= 1 << 12,
23767bb312e4SVu Pham 	SRP_OPT_TL_RETRY_COUNT	= 1 << 13,
23774d73f95fSBart Van Assche 	SRP_OPT_QUEUE_SIZE	= 1 << 14,
2378aef9ec39SRoland Dreier 	SRP_OPT_ALL		= (SRP_OPT_ID_EXT	|
2379aef9ec39SRoland Dreier 				   SRP_OPT_IOC_GUID	|
2380aef9ec39SRoland Dreier 				   SRP_OPT_DGID		|
2381aef9ec39SRoland Dreier 				   SRP_OPT_PKEY		|
2382aef9ec39SRoland Dreier 				   SRP_OPT_SERVICE_ID),
2383aef9ec39SRoland Dreier };
2384aef9ec39SRoland Dreier 
2385a447c093SSteven Whitehouse static const match_table_t srp_opt_tokens = {
2386aef9ec39SRoland Dreier 	{ SRP_OPT_ID_EXT,		"id_ext=%s" 		},
2387aef9ec39SRoland Dreier 	{ SRP_OPT_IOC_GUID,		"ioc_guid=%s" 		},
2388aef9ec39SRoland Dreier 	{ SRP_OPT_DGID,			"dgid=%s" 		},
2389aef9ec39SRoland Dreier 	{ SRP_OPT_PKEY,			"pkey=%x" 		},
2390aef9ec39SRoland Dreier 	{ SRP_OPT_SERVICE_ID,		"service_id=%s"		},
2391aef9ec39SRoland Dreier 	{ SRP_OPT_MAX_SECT,		"max_sect=%d" 		},
239252fb2b50SVu Pham 	{ SRP_OPT_MAX_CMD_PER_LUN,	"max_cmd_per_lun=%d" 	},
23930c0450dbSRamachandra K 	{ SRP_OPT_IO_CLASS,		"io_class=%x"		},
239401cb9bcbSIshai Rabinovitz 	{ SRP_OPT_INITIATOR_EXT,	"initiator_ext=%s"	},
239549248644SDavid Dillow 	{ SRP_OPT_CMD_SG_ENTRIES,	"cmd_sg_entries=%u"	},
2396c07d424dSDavid Dillow 	{ SRP_OPT_ALLOW_EXT_SG,		"allow_ext_sg=%u"	},
2397c07d424dSDavid Dillow 	{ SRP_OPT_SG_TABLESIZE,		"sg_tablesize=%u"	},
23984b5e5f41SBart Van Assche 	{ SRP_OPT_COMP_VECTOR,		"comp_vector=%u"	},
23997bb312e4SVu Pham 	{ SRP_OPT_TL_RETRY_COUNT,	"tl_retry_count=%u"	},
24004d73f95fSBart Van Assche 	{ SRP_OPT_QUEUE_SIZE,		"queue_size=%d"		},
2401aef9ec39SRoland Dreier 	{ SRP_OPT_ERR,			NULL 			}
2402aef9ec39SRoland Dreier };
2403aef9ec39SRoland Dreier 
2404aef9ec39SRoland Dreier static int srp_parse_options(const char *buf, struct srp_target_port *target)
2405aef9ec39SRoland Dreier {
2406aef9ec39SRoland Dreier 	char *options, *sep_opt;
2407aef9ec39SRoland Dreier 	char *p;
2408aef9ec39SRoland Dreier 	char dgid[3];
2409aef9ec39SRoland Dreier 	substring_t args[MAX_OPT_ARGS];
2410aef9ec39SRoland Dreier 	int opt_mask = 0;
2411aef9ec39SRoland Dreier 	int token;
2412aef9ec39SRoland Dreier 	int ret = -EINVAL;
2413aef9ec39SRoland Dreier 	int i;
2414aef9ec39SRoland Dreier 
2415aef9ec39SRoland Dreier 	options = kstrdup(buf, GFP_KERNEL);
2416aef9ec39SRoland Dreier 	if (!options)
2417aef9ec39SRoland Dreier 		return -ENOMEM;
2418aef9ec39SRoland Dreier 
2419aef9ec39SRoland Dreier 	sep_opt = options;
2420aef9ec39SRoland Dreier 	while ((p = strsep(&sep_opt, ",")) != NULL) {
2421aef9ec39SRoland Dreier 		if (!*p)
2422aef9ec39SRoland Dreier 			continue;
2423aef9ec39SRoland Dreier 
2424aef9ec39SRoland Dreier 		token = match_token(p, srp_opt_tokens, args);
2425aef9ec39SRoland Dreier 		opt_mask |= token;
2426aef9ec39SRoland Dreier 
2427aef9ec39SRoland Dreier 		switch (token) {
2428aef9ec39SRoland Dreier 		case SRP_OPT_ID_EXT:
2429aef9ec39SRoland Dreier 			p = match_strdup(args);
2430a20f3a6dSIshai Rabinovitz 			if (!p) {
2431a20f3a6dSIshai Rabinovitz 				ret = -ENOMEM;
2432a20f3a6dSIshai Rabinovitz 				goto out;
2433a20f3a6dSIshai Rabinovitz 			}
2434aef9ec39SRoland Dreier 			target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2435aef9ec39SRoland Dreier 			kfree(p);
2436aef9ec39SRoland Dreier 			break;
2437aef9ec39SRoland Dreier 
2438aef9ec39SRoland Dreier 		case SRP_OPT_IOC_GUID:
2439aef9ec39SRoland Dreier 			p = match_strdup(args);
2440a20f3a6dSIshai Rabinovitz 			if (!p) {
2441a20f3a6dSIshai Rabinovitz 				ret = -ENOMEM;
2442a20f3a6dSIshai Rabinovitz 				goto out;
2443a20f3a6dSIshai Rabinovitz 			}
2444aef9ec39SRoland Dreier 			target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2445aef9ec39SRoland Dreier 			kfree(p);
2446aef9ec39SRoland Dreier 			break;
2447aef9ec39SRoland Dreier 
2448aef9ec39SRoland Dreier 		case SRP_OPT_DGID:
2449aef9ec39SRoland Dreier 			p = match_strdup(args);
2450a20f3a6dSIshai Rabinovitz 			if (!p) {
2451a20f3a6dSIshai Rabinovitz 				ret = -ENOMEM;
2452a20f3a6dSIshai Rabinovitz 				goto out;
2453a20f3a6dSIshai Rabinovitz 			}
2454aef9ec39SRoland Dreier 			if (strlen(p) != 32) {
2455e0bda7d8SBart Van Assche 				pr_warn("bad dest GID parameter '%s'\n", p);
2456ce1823f0SRoland Dreier 				kfree(p);
2457aef9ec39SRoland Dreier 				goto out;
2458aef9ec39SRoland Dreier 			}
2459aef9ec39SRoland Dreier 
2460aef9ec39SRoland Dreier 			for (i = 0; i < 16; ++i) {
2461aef9ec39SRoland Dreier 				strlcpy(dgid, p + i * 2, 3);
2462aef9ec39SRoland Dreier 				target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2463aef9ec39SRoland Dreier 			}
2464bf17c1c7SRoland Dreier 			kfree(p);
24653633b3d0SIshai Rabinovitz 			memcpy(target->orig_dgid, target->path.dgid.raw, 16);
2466aef9ec39SRoland Dreier 			break;
2467aef9ec39SRoland Dreier 
2468aef9ec39SRoland Dreier 		case SRP_OPT_PKEY:
2469aef9ec39SRoland Dreier 			if (match_hex(args, &token)) {
2470e0bda7d8SBart Van Assche 				pr_warn("bad P_Key parameter '%s'\n", p);
2471aef9ec39SRoland Dreier 				goto out;
2472aef9ec39SRoland Dreier 			}
2473aef9ec39SRoland Dreier 			target->path.pkey = cpu_to_be16(token);
2474aef9ec39SRoland Dreier 			break;
2475aef9ec39SRoland Dreier 
2476aef9ec39SRoland Dreier 		case SRP_OPT_SERVICE_ID:
2477aef9ec39SRoland Dreier 			p = match_strdup(args);
2478a20f3a6dSIshai Rabinovitz 			if (!p) {
2479a20f3a6dSIshai Rabinovitz 				ret = -ENOMEM;
2480a20f3a6dSIshai Rabinovitz 				goto out;
2481a20f3a6dSIshai Rabinovitz 			}
2482aef9ec39SRoland Dreier 			target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
2483247e020eSSean Hefty 			target->path.service_id = target->service_id;
2484aef9ec39SRoland Dreier 			kfree(p);
2485aef9ec39SRoland Dreier 			break;
2486aef9ec39SRoland Dreier 
2487aef9ec39SRoland Dreier 		case SRP_OPT_MAX_SECT:
2488aef9ec39SRoland Dreier 			if (match_int(args, &token)) {
2489e0bda7d8SBart Van Assche 				pr_warn("bad max sect parameter '%s'\n", p);
2490aef9ec39SRoland Dreier 				goto out;
2491aef9ec39SRoland Dreier 			}
2492aef9ec39SRoland Dreier 			target->scsi_host->max_sectors = token;
2493aef9ec39SRoland Dreier 			break;
2494aef9ec39SRoland Dreier 
24954d73f95fSBart Van Assche 		case SRP_OPT_QUEUE_SIZE:
24964d73f95fSBart Van Assche 			if (match_int(args, &token) || token < 1) {
24974d73f95fSBart Van Assche 				pr_warn("bad queue_size parameter '%s'\n", p);
24984d73f95fSBart Van Assche 				goto out;
24994d73f95fSBart Van Assche 			}
25004d73f95fSBart Van Assche 			target->scsi_host->can_queue = token;
25014d73f95fSBart Van Assche 			target->queue_size = token + SRP_RSP_SQ_SIZE +
25024d73f95fSBart Van Assche 					     SRP_TSK_MGMT_SQ_SIZE;
25034d73f95fSBart Van Assche 			if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
25044d73f95fSBart Van Assche 				target->scsi_host->cmd_per_lun = token;
25054d73f95fSBart Van Assche 			break;
25064d73f95fSBart Van Assche 
250752fb2b50SVu Pham 		case SRP_OPT_MAX_CMD_PER_LUN:
25084d73f95fSBart Van Assche 			if (match_int(args, &token) || token < 1) {
2509e0bda7d8SBart Van Assche 				pr_warn("bad max cmd_per_lun parameter '%s'\n",
2510e0bda7d8SBart Van Assche 					p);
251152fb2b50SVu Pham 				goto out;
251252fb2b50SVu Pham 			}
25134d73f95fSBart Van Assche 			target->scsi_host->cmd_per_lun = token;
251452fb2b50SVu Pham 			break;
251552fb2b50SVu Pham 
25160c0450dbSRamachandra K 		case SRP_OPT_IO_CLASS:
25170c0450dbSRamachandra K 			if (match_hex(args, &token)) {
2518e0bda7d8SBart Van Assche 				pr_warn("bad IO class parameter '%s'\n", p);
25190c0450dbSRamachandra K 				goto out;
25200c0450dbSRamachandra K 			}
25210c0450dbSRamachandra K 			if (token != SRP_REV10_IB_IO_CLASS &&
25220c0450dbSRamachandra K 			    token != SRP_REV16A_IB_IO_CLASS) {
2523e0bda7d8SBart Van Assche 				pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2524e0bda7d8SBart Van Assche 					token, SRP_REV10_IB_IO_CLASS,
2525e0bda7d8SBart Van Assche 					SRP_REV16A_IB_IO_CLASS);
25260c0450dbSRamachandra K 				goto out;
25270c0450dbSRamachandra K 			}
25280c0450dbSRamachandra K 			target->io_class = token;
25290c0450dbSRamachandra K 			break;
25300c0450dbSRamachandra K 
253101cb9bcbSIshai Rabinovitz 		case SRP_OPT_INITIATOR_EXT:
253201cb9bcbSIshai Rabinovitz 			p = match_strdup(args);
2533a20f3a6dSIshai Rabinovitz 			if (!p) {
2534a20f3a6dSIshai Rabinovitz 				ret = -ENOMEM;
2535a20f3a6dSIshai Rabinovitz 				goto out;
2536a20f3a6dSIshai Rabinovitz 			}
253701cb9bcbSIshai Rabinovitz 			target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
253801cb9bcbSIshai Rabinovitz 			kfree(p);
253901cb9bcbSIshai Rabinovitz 			break;
254001cb9bcbSIshai Rabinovitz 
254149248644SDavid Dillow 		case SRP_OPT_CMD_SG_ENTRIES:
254249248644SDavid Dillow 			if (match_int(args, &token) || token < 1 || token > 255) {
2543e0bda7d8SBart Van Assche 				pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2544e0bda7d8SBart Van Assche 					p);
254549248644SDavid Dillow 				goto out;
254649248644SDavid Dillow 			}
254749248644SDavid Dillow 			target->cmd_sg_cnt = token;
254849248644SDavid Dillow 			break;
254949248644SDavid Dillow 
2550c07d424dSDavid Dillow 		case SRP_OPT_ALLOW_EXT_SG:
2551c07d424dSDavid Dillow 			if (match_int(args, &token)) {
2552e0bda7d8SBart Van Assche 				pr_warn("bad allow_ext_sg parameter '%s'\n", p);
2553c07d424dSDavid Dillow 				goto out;
2554c07d424dSDavid Dillow 			}
2555c07d424dSDavid Dillow 			target->allow_ext_sg = !!token;
2556c07d424dSDavid Dillow 			break;
2557c07d424dSDavid Dillow 
2558c07d424dSDavid Dillow 		case SRP_OPT_SG_TABLESIZE:
2559c07d424dSDavid Dillow 			if (match_int(args, &token) || token < 1 ||
2560c07d424dSDavid Dillow 					token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
2561e0bda7d8SBart Van Assche 				pr_warn("bad max sg_tablesize parameter '%s'\n",
2562e0bda7d8SBart Van Assche 					p);
2563c07d424dSDavid Dillow 				goto out;
2564c07d424dSDavid Dillow 			}
2565c07d424dSDavid Dillow 			target->sg_tablesize = token;
2566c07d424dSDavid Dillow 			break;
2567c07d424dSDavid Dillow 
25684b5e5f41SBart Van Assche 		case SRP_OPT_COMP_VECTOR:
25694b5e5f41SBart Van Assche 			if (match_int(args, &token) || token < 0) {
25704b5e5f41SBart Van Assche 				pr_warn("bad comp_vector parameter '%s'\n", p);
25714b5e5f41SBart Van Assche 				goto out;
25724b5e5f41SBart Van Assche 			}
25734b5e5f41SBart Van Assche 			target->comp_vector = token;
25744b5e5f41SBart Van Assche 			break;
25754b5e5f41SBart Van Assche 
25767bb312e4SVu Pham 		case SRP_OPT_TL_RETRY_COUNT:
25777bb312e4SVu Pham 			if (match_int(args, &token) || token < 2 || token > 7) {
25787bb312e4SVu Pham 				pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
25797bb312e4SVu Pham 					p);
25807bb312e4SVu Pham 				goto out;
25817bb312e4SVu Pham 			}
25827bb312e4SVu Pham 			target->tl_retry_count = token;
25837bb312e4SVu Pham 			break;
25847bb312e4SVu Pham 
2585aef9ec39SRoland Dreier 		default:
2586e0bda7d8SBart Van Assche 			pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2587e0bda7d8SBart Van Assche 				p);
2588aef9ec39SRoland Dreier 			goto out;
2589aef9ec39SRoland Dreier 		}
2590aef9ec39SRoland Dreier 	}
2591aef9ec39SRoland Dreier 
2592aef9ec39SRoland Dreier 	if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2593aef9ec39SRoland Dreier 		ret = 0;
2594aef9ec39SRoland Dreier 	else
2595aef9ec39SRoland Dreier 		for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2596aef9ec39SRoland Dreier 			if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2597aef9ec39SRoland Dreier 			    !(srp_opt_tokens[i].token & opt_mask))
2598e0bda7d8SBart Van Assche 				pr_warn("target creation request is missing parameter '%s'\n",
2599aef9ec39SRoland Dreier 					srp_opt_tokens[i].pattern);
2600aef9ec39SRoland Dreier 
26014d73f95fSBart Van Assche 	if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
26024d73f95fSBart Van Assche 	    && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
26034d73f95fSBart Van Assche 		pr_warn("cmd_per_lun = %d > queue_size = %d\n",
26044d73f95fSBart Van Assche 			target->scsi_host->cmd_per_lun,
26054d73f95fSBart Van Assche 			target->scsi_host->can_queue);
26064d73f95fSBart Van Assche 
2607aef9ec39SRoland Dreier out:
2608aef9ec39SRoland Dreier 	kfree(options);
2609aef9ec39SRoland Dreier 	return ret;
2610aef9ec39SRoland Dreier }
2611aef9ec39SRoland Dreier 
2612ee959b00STony Jones static ssize_t srp_create_target(struct device *dev,
2613ee959b00STony Jones 				 struct device_attribute *attr,
2614aef9ec39SRoland Dreier 				 const char *buf, size_t count)
2615aef9ec39SRoland Dreier {
2616aef9ec39SRoland Dreier 	struct srp_host *host =
2617ee959b00STony Jones 		container_of(dev, struct srp_host, dev);
2618aef9ec39SRoland Dreier 	struct Scsi_Host *target_host;
2619aef9ec39SRoland Dreier 	struct srp_target_port *target;
2620c07d424dSDavid Dillow 	struct ib_device *ibdev = host->srp_dev->dev;
2621b81d00bdSBart Van Assche 	int ret;
2622aef9ec39SRoland Dreier 
2623aef9ec39SRoland Dreier 	target_host = scsi_host_alloc(&srp_template,
2624aef9ec39SRoland Dreier 				      sizeof (struct srp_target_port));
2625aef9ec39SRoland Dreier 	if (!target_host)
2626aef9ec39SRoland Dreier 		return -ENOMEM;
2627aef9ec39SRoland Dreier 
26283236822bSFUJITA Tomonori 	target_host->transportt  = ib_srp_transport_template;
2629fd1b6c4aSBart Van Assche 	target_host->max_channel = 0;
2630fd1b6c4aSBart Van Assche 	target_host->max_id      = 1;
26315f068992SRoland Dreier 	target_host->max_lun     = SRP_MAX_LUN;
26323c8edf0eSArne Redlich 	target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
26335f068992SRoland Dreier 
2634aef9ec39SRoland Dreier 	target = host_to_target(target_host);
2635aef9ec39SRoland Dreier 
26360c0450dbSRamachandra K 	target->io_class	= SRP_REV16A_IB_IO_CLASS;
2637aef9ec39SRoland Dreier 	target->scsi_host	= target_host;
2638aef9ec39SRoland Dreier 	target->srp_host	= host;
26399af76271SDavid Dillow 	target->lkey		= host->srp_dev->mr->lkey;
26409af76271SDavid Dillow 	target->rkey		= host->srp_dev->mr->rkey;
264149248644SDavid Dillow 	target->cmd_sg_cnt	= cmd_sg_entries;
2642c07d424dSDavid Dillow 	target->sg_tablesize	= indirect_sg_entries ? : cmd_sg_entries;
2643c07d424dSDavid Dillow 	target->allow_ext_sg	= allow_ext_sg;
26447bb312e4SVu Pham 	target->tl_retry_count	= 7;
26454d73f95fSBart Van Assche 	target->queue_size	= SRP_DEFAULT_QUEUE_SIZE;
2646aef9ec39SRoland Dreier 
26472d7091bcSBart Van Assche 	mutex_lock(&host->add_target_mutex);
26482d7091bcSBart Van Assche 
2649aef9ec39SRoland Dreier 	ret = srp_parse_options(buf, target);
2650aef9ec39SRoland Dreier 	if (ret)
2651aef9ec39SRoland Dreier 		goto err;
2652aef9ec39SRoland Dreier 
26534d73f95fSBart Van Assche 	target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
26544d73f95fSBart Van Assche 
265596fc248aSBart Van Assche 	if (!srp_conn_unique(target->srp_host, target)) {
265696fc248aSBart Van Assche 		shost_printk(KERN_INFO, target->scsi_host,
265796fc248aSBart Van Assche 			     PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
265896fc248aSBart Van Assche 			     be64_to_cpu(target->id_ext),
265996fc248aSBart Van Assche 			     be64_to_cpu(target->ioc_guid),
266096fc248aSBart Van Assche 			     be64_to_cpu(target->initiator_ext));
266196fc248aSBart Van Assche 		ret = -EEXIST;
266296fc248aSBart Van Assche 		goto err;
266396fc248aSBart Van Assche 	}
266496fc248aSBart Van Assche 
2665c07d424dSDavid Dillow 	if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2666c07d424dSDavid Dillow 				target->cmd_sg_cnt < target->sg_tablesize) {
2667e0bda7d8SBart Van Assche 		pr_warn("No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
2668c07d424dSDavid Dillow 		target->sg_tablesize = target->cmd_sg_cnt;
2669c07d424dSDavid Dillow 	}
2670c07d424dSDavid Dillow 
2671c07d424dSDavid Dillow 	target_host->sg_tablesize = target->sg_tablesize;
2672c07d424dSDavid Dillow 	target->indirect_size = target->sg_tablesize *
2673c07d424dSDavid Dillow 				sizeof (struct srp_direct_buf);
267449248644SDavid Dillow 	target->max_iu_len = sizeof (struct srp_cmd) +
267549248644SDavid Dillow 			     sizeof (struct srp_indirect_buf) +
267649248644SDavid Dillow 			     target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
267749248644SDavid Dillow 
2678c1120f89SBart Van Assche 	INIT_WORK(&target->tl_err_work, srp_tl_err_work);
2679ef6c49d8SBart Van Assche 	INIT_WORK(&target->remove_work, srp_remove_work);
26808f26c9ffSDavid Dillow 	spin_lock_init(&target->lock);
26818f26c9ffSDavid Dillow 	INIT_LIST_HEAD(&target->free_tx);
2682b81d00bdSBart Van Assche 	ret = srp_alloc_req_data(target);
2683b81d00bdSBart Van Assche 	if (ret)
26848f26c9ffSDavid Dillow 		goto err_free_mem;
26858f26c9ffSDavid Dillow 
26862088ca66SSagi Grimberg 	ret = ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
26872088ca66SSagi Grimberg 	if (ret)
26882088ca66SSagi Grimberg 		goto err_free_mem;
2689aef9ec39SRoland Dreier 
2690aef9ec39SRoland Dreier 	ret = srp_create_target_ib(target);
2691aef9ec39SRoland Dreier 	if (ret)
26928f26c9ffSDavid Dillow 		goto err_free_mem;
2693aef9ec39SRoland Dreier 
26949fe4bcf4SDavid Dillow 	ret = srp_new_cm_id(target);
26959fe4bcf4SDavid Dillow 	if (ret)
26968f26c9ffSDavid Dillow 		goto err_free_ib;
2697aef9ec39SRoland Dreier 
2698aef9ec39SRoland Dreier 	ret = srp_connect_target(target);
2699aef9ec39SRoland Dreier 	if (ret) {
27007aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
27017aa54bd7SDavid Dillow 			     PFX "Connection failed\n");
2702aef9ec39SRoland Dreier 		goto err_cm_id;
2703aef9ec39SRoland Dreier 	}
2704aef9ec39SRoland Dreier 
2705aef9ec39SRoland Dreier 	ret = srp_add_target(host, target);
2706aef9ec39SRoland Dreier 	if (ret)
2707aef9ec39SRoland Dreier 		goto err_disconnect;
2708aef9ec39SRoland Dreier 
2709e7ffde01SBart Van Assche 	shost_printk(KERN_DEBUG, target->scsi_host, PFX
2710e7ffde01SBart Van Assche 		     "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n",
2711e7ffde01SBart Van Assche 		     be64_to_cpu(target->id_ext),
2712e7ffde01SBart Van Assche 		     be64_to_cpu(target->ioc_guid),
2713e7ffde01SBart Van Assche 		     be16_to_cpu(target->path.pkey),
2714e7ffde01SBart Van Assche 		     be64_to_cpu(target->service_id),
2715e7ffde01SBart Van Assche 		     target->path.sgid.raw, target->path.dgid.raw);
2716e7ffde01SBart Van Assche 
27172d7091bcSBart Van Assche 	ret = count;
27182d7091bcSBart Van Assche 
27192d7091bcSBart Van Assche out:
27202d7091bcSBart Van Assche 	mutex_unlock(&host->add_target_mutex);
27212d7091bcSBart Van Assche 	return ret;
2722aef9ec39SRoland Dreier 
2723aef9ec39SRoland Dreier err_disconnect:
2724aef9ec39SRoland Dreier 	srp_disconnect_target(target);
2725aef9ec39SRoland Dreier 
2726aef9ec39SRoland Dreier err_cm_id:
2727aef9ec39SRoland Dreier 	ib_destroy_cm_id(target->cm_id);
2728aef9ec39SRoland Dreier 
27298f26c9ffSDavid Dillow err_free_ib:
2730aef9ec39SRoland Dreier 	srp_free_target_ib(target);
2731aef9ec39SRoland Dreier 
27328f26c9ffSDavid Dillow err_free_mem:
27338f26c9ffSDavid Dillow 	srp_free_req_data(target);
27348f26c9ffSDavid Dillow 
2735aef9ec39SRoland Dreier err:
2736aef9ec39SRoland Dreier 	scsi_host_put(target_host);
27372d7091bcSBart Van Assche 	goto out;
2738aef9ec39SRoland Dreier }
2739aef9ec39SRoland Dreier 
2740ee959b00STony Jones static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
2741aef9ec39SRoland Dreier 
2742ee959b00STony Jones static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2743ee959b00STony Jones 			  char *buf)
2744aef9ec39SRoland Dreier {
2745ee959b00STony Jones 	struct srp_host *host = container_of(dev, struct srp_host, dev);
2746aef9ec39SRoland Dreier 
274705321937SGreg Kroah-Hartman 	return sprintf(buf, "%s\n", host->srp_dev->dev->name);
2748aef9ec39SRoland Dreier }
2749aef9ec39SRoland Dreier 
2750ee959b00STony Jones static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
2751aef9ec39SRoland Dreier 
2752ee959b00STony Jones static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2753ee959b00STony Jones 			 char *buf)
2754aef9ec39SRoland Dreier {
2755ee959b00STony Jones 	struct srp_host *host = container_of(dev, struct srp_host, dev);
2756aef9ec39SRoland Dreier 
2757aef9ec39SRoland Dreier 	return sprintf(buf, "%d\n", host->port);
2758aef9ec39SRoland Dreier }
2759aef9ec39SRoland Dreier 
2760ee959b00STony Jones static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
2761aef9ec39SRoland Dreier 
2762f5358a17SRoland Dreier static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
2763aef9ec39SRoland Dreier {
2764aef9ec39SRoland Dreier 	struct srp_host *host;
2765aef9ec39SRoland Dreier 
2766aef9ec39SRoland Dreier 	host = kzalloc(sizeof *host, GFP_KERNEL);
2767aef9ec39SRoland Dreier 	if (!host)
2768aef9ec39SRoland Dreier 		return NULL;
2769aef9ec39SRoland Dreier 
2770aef9ec39SRoland Dreier 	INIT_LIST_HEAD(&host->target_list);
2771b3589fd4SMatthew Wilcox 	spin_lock_init(&host->target_lock);
2772aef9ec39SRoland Dreier 	init_completion(&host->released);
27732d7091bcSBart Van Assche 	mutex_init(&host->add_target_mutex);
277405321937SGreg Kroah-Hartman 	host->srp_dev = device;
2775aef9ec39SRoland Dreier 	host->port = port;
2776aef9ec39SRoland Dreier 
2777ee959b00STony Jones 	host->dev.class = &srp_class;
2778ee959b00STony Jones 	host->dev.parent = device->dev->dma_device;
2779d927e38cSKay Sievers 	dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
2780aef9ec39SRoland Dreier 
2781ee959b00STony Jones 	if (device_register(&host->dev))
2782f5358a17SRoland Dreier 		goto free_host;
2783ee959b00STony Jones 	if (device_create_file(&host->dev, &dev_attr_add_target))
2784aef9ec39SRoland Dreier 		goto err_class;
2785ee959b00STony Jones 	if (device_create_file(&host->dev, &dev_attr_ibdev))
2786aef9ec39SRoland Dreier 		goto err_class;
2787ee959b00STony Jones 	if (device_create_file(&host->dev, &dev_attr_port))
2788aef9ec39SRoland Dreier 		goto err_class;
2789aef9ec39SRoland Dreier 
2790aef9ec39SRoland Dreier 	return host;
2791aef9ec39SRoland Dreier 
2792aef9ec39SRoland Dreier err_class:
2793ee959b00STony Jones 	device_unregister(&host->dev);
2794aef9ec39SRoland Dreier 
2795f5358a17SRoland Dreier free_host:
2796aef9ec39SRoland Dreier 	kfree(host);
2797aef9ec39SRoland Dreier 
2798aef9ec39SRoland Dreier 	return NULL;
2799aef9ec39SRoland Dreier }
2800aef9ec39SRoland Dreier 
2801aef9ec39SRoland Dreier static void srp_add_one(struct ib_device *device)
2802aef9ec39SRoland Dreier {
2803f5358a17SRoland Dreier 	struct srp_device *srp_dev;
2804f5358a17SRoland Dreier 	struct ib_device_attr *dev_attr;
2805f5358a17SRoland Dreier 	struct ib_fmr_pool_param fmr_param;
2806aef9ec39SRoland Dreier 	struct srp_host *host;
2807be8b9814SDavid Dillow 	int max_pages_per_fmr, fmr_page_shift, s, e, p;
2808aef9ec39SRoland Dreier 
2809f5358a17SRoland Dreier 	dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2810f5358a17SRoland Dreier 	if (!dev_attr)
2811cf311cd4SSean Hefty 		return;
2812aef9ec39SRoland Dreier 
2813f5358a17SRoland Dreier 	if (ib_query_device(device, dev_attr)) {
2814e0bda7d8SBart Van Assche 		pr_warn("Query device failed for %s\n", device->name);
2815f5358a17SRoland Dreier 		goto free_attr;
2816f5358a17SRoland Dreier 	}
2817f5358a17SRoland Dreier 
2818f5358a17SRoland Dreier 	srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2819f5358a17SRoland Dreier 	if (!srp_dev)
2820f5358a17SRoland Dreier 		goto free_attr;
2821f5358a17SRoland Dreier 
2822f5358a17SRoland Dreier 	/*
2823f5358a17SRoland Dreier 	 * Use the smallest page size supported by the HCA, down to a
28248f26c9ffSDavid Dillow 	 * minimum of 4096 bytes. We're unlikely to build large sglists
28258f26c9ffSDavid Dillow 	 * out of smaller entries.
2826f5358a17SRoland Dreier 	 */
28278f26c9ffSDavid Dillow 	fmr_page_shift		= max(12, ffs(dev_attr->page_size_cap) - 1);
28288f26c9ffSDavid Dillow 	srp_dev->fmr_page_size	= 1 << fmr_page_shift;
2829bf628dc2SRoland Dreier 	srp_dev->fmr_page_mask	= ~((u64) srp_dev->fmr_page_size - 1);
28308f26c9ffSDavid Dillow 	srp_dev->fmr_max_size	= srp_dev->fmr_page_size * SRP_FMR_SIZE;
2831f5358a17SRoland Dreier 
2832f5358a17SRoland Dreier 	INIT_LIST_HEAD(&srp_dev->dev_list);
2833f5358a17SRoland Dreier 
2834f5358a17SRoland Dreier 	srp_dev->dev = device;
2835f5358a17SRoland Dreier 	srp_dev->pd  = ib_alloc_pd(device);
2836f5358a17SRoland Dreier 	if (IS_ERR(srp_dev->pd))
2837f5358a17SRoland Dreier 		goto free_dev;
2838f5358a17SRoland Dreier 
2839f5358a17SRoland Dreier 	srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2840f5358a17SRoland Dreier 				    IB_ACCESS_LOCAL_WRITE |
2841f5358a17SRoland Dreier 				    IB_ACCESS_REMOTE_READ |
2842f5358a17SRoland Dreier 				    IB_ACCESS_REMOTE_WRITE);
2843f5358a17SRoland Dreier 	if (IS_ERR(srp_dev->mr))
2844f5358a17SRoland Dreier 		goto err_pd;
2845f5358a17SRoland Dreier 
2846be8b9814SDavid Dillow 	for (max_pages_per_fmr = SRP_FMR_SIZE;
2847be8b9814SDavid Dillow 			max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2848be8b9814SDavid Dillow 			max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2849f5358a17SRoland Dreier 		memset(&fmr_param, 0, sizeof fmr_param);
2850f5358a17SRoland Dreier 		fmr_param.pool_size	    = SRP_FMR_POOL_SIZE;
2851f5358a17SRoland Dreier 		fmr_param.dirty_watermark   = SRP_FMR_DIRTY_SIZE;
2852f5358a17SRoland Dreier 		fmr_param.cache		    = 1;
2853be8b9814SDavid Dillow 		fmr_param.max_pages_per_fmr = max_pages_per_fmr;
28548f26c9ffSDavid Dillow 		fmr_param.page_shift	    = fmr_page_shift;
2855f5358a17SRoland Dreier 		fmr_param.access	    = (IB_ACCESS_LOCAL_WRITE |
2856f5358a17SRoland Dreier 					       IB_ACCESS_REMOTE_WRITE |
2857f5358a17SRoland Dreier 					       IB_ACCESS_REMOTE_READ);
2858f5358a17SRoland Dreier 
2859f5358a17SRoland Dreier 		srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2860be8b9814SDavid Dillow 		if (!IS_ERR(srp_dev->fmr_pool))
2861be8b9814SDavid Dillow 			break;
2862be8b9814SDavid Dillow 	}
2863be8b9814SDavid Dillow 
2864f5358a17SRoland Dreier 	if (IS_ERR(srp_dev->fmr_pool))
2865f5358a17SRoland Dreier 		srp_dev->fmr_pool = NULL;
2866aef9ec39SRoland Dreier 
286707ebafbaSTom Tucker 	if (device->node_type == RDMA_NODE_IB_SWITCH) {
2868aef9ec39SRoland Dreier 		s = 0;
2869aef9ec39SRoland Dreier 		e = 0;
2870aef9ec39SRoland Dreier 	} else {
2871aef9ec39SRoland Dreier 		s = 1;
2872aef9ec39SRoland Dreier 		e = device->phys_port_cnt;
2873aef9ec39SRoland Dreier 	}
2874aef9ec39SRoland Dreier 
2875aef9ec39SRoland Dreier 	for (p = s; p <= e; ++p) {
2876f5358a17SRoland Dreier 		host = srp_add_port(srp_dev, p);
2877aef9ec39SRoland Dreier 		if (host)
2878f5358a17SRoland Dreier 			list_add_tail(&host->list, &srp_dev->dev_list);
2879aef9ec39SRoland Dreier 	}
2880aef9ec39SRoland Dreier 
2881f5358a17SRoland Dreier 	ib_set_client_data(device, &srp_client, srp_dev);
2882f5358a17SRoland Dreier 
2883f5358a17SRoland Dreier 	goto free_attr;
2884f5358a17SRoland Dreier 
2885f5358a17SRoland Dreier err_pd:
2886f5358a17SRoland Dreier 	ib_dealloc_pd(srp_dev->pd);
2887f5358a17SRoland Dreier 
2888f5358a17SRoland Dreier free_dev:
2889f5358a17SRoland Dreier 	kfree(srp_dev);
2890f5358a17SRoland Dreier 
2891f5358a17SRoland Dreier free_attr:
2892f5358a17SRoland Dreier 	kfree(dev_attr);
2893aef9ec39SRoland Dreier }
2894aef9ec39SRoland Dreier 
2895aef9ec39SRoland Dreier static void srp_remove_one(struct ib_device *device)
2896aef9ec39SRoland Dreier {
2897f5358a17SRoland Dreier 	struct srp_device *srp_dev;
2898aef9ec39SRoland Dreier 	struct srp_host *host, *tmp_host;
2899ef6c49d8SBart Van Assche 	struct srp_target_port *target;
2900aef9ec39SRoland Dreier 
2901f5358a17SRoland Dreier 	srp_dev = ib_get_client_data(device, &srp_client);
29021fe0cb84SDotan Barak 	if (!srp_dev)
29031fe0cb84SDotan Barak 		return;
2904aef9ec39SRoland Dreier 
2905f5358a17SRoland Dreier 	list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2906ee959b00STony Jones 		device_unregister(&host->dev);
2907aef9ec39SRoland Dreier 		/*
2908aef9ec39SRoland Dreier 		 * Wait for the sysfs entry to go away, so that no new
2909aef9ec39SRoland Dreier 		 * target ports can be created.
2910aef9ec39SRoland Dreier 		 */
2911aef9ec39SRoland Dreier 		wait_for_completion(&host->released);
2912aef9ec39SRoland Dreier 
2913aef9ec39SRoland Dreier 		/*
2914ef6c49d8SBart Van Assche 		 * Remove all target ports.
2915aef9ec39SRoland Dreier 		 */
2916b3589fd4SMatthew Wilcox 		spin_lock(&host->target_lock);
2917ef6c49d8SBart Van Assche 		list_for_each_entry(target, &host->target_list, list)
2918ef6c49d8SBart Van Assche 			srp_queue_remove_work(target);
2919b3589fd4SMatthew Wilcox 		spin_unlock(&host->target_lock);
2920aef9ec39SRoland Dreier 
2921aef9ec39SRoland Dreier 		/*
2922ef6c49d8SBart Van Assche 		 * Wait for target port removal tasks.
2923aef9ec39SRoland Dreier 		 */
2924ef6c49d8SBart Van Assche 		flush_workqueue(system_long_wq);
2925aef9ec39SRoland Dreier 
2926aef9ec39SRoland Dreier 		kfree(host);
2927aef9ec39SRoland Dreier 	}
2928aef9ec39SRoland Dreier 
2929f5358a17SRoland Dreier 	if (srp_dev->fmr_pool)
2930f5358a17SRoland Dreier 		ib_destroy_fmr_pool(srp_dev->fmr_pool);
2931f5358a17SRoland Dreier 	ib_dereg_mr(srp_dev->mr);
2932f5358a17SRoland Dreier 	ib_dealloc_pd(srp_dev->pd);
2933f5358a17SRoland Dreier 
2934f5358a17SRoland Dreier 	kfree(srp_dev);
2935aef9ec39SRoland Dreier }
2936aef9ec39SRoland Dreier 
29373236822bSFUJITA Tomonori static struct srp_function_template ib_srp_transport_functions = {
2938ed9b2264SBart Van Assche 	.has_rport_state	 = true,
2939ed9b2264SBart Van Assche 	.reset_timer_if_blocked	 = true,
2940a95cadb9SBart Van Assche 	.reconnect_delay	 = &srp_reconnect_delay,
2941ed9b2264SBart Van Assche 	.fast_io_fail_tmo	 = &srp_fast_io_fail_tmo,
2942ed9b2264SBart Van Assche 	.dev_loss_tmo		 = &srp_dev_loss_tmo,
2943ed9b2264SBart Van Assche 	.reconnect		 = srp_rport_reconnect,
2944dc1bdbd9SBart Van Assche 	.rport_delete		 = srp_rport_delete,
2945ed9b2264SBart Van Assche 	.terminate_rport_io	 = srp_terminate_io,
29463236822bSFUJITA Tomonori };
29473236822bSFUJITA Tomonori 
2948aef9ec39SRoland Dreier static int __init srp_init_module(void)
2949aef9ec39SRoland Dreier {
2950aef9ec39SRoland Dreier 	int ret;
2951aef9ec39SRoland Dreier 
2952dcb4cb85SBart Van Assche 	BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
2953dd5e6e38SBart Van Assche 
295449248644SDavid Dillow 	if (srp_sg_tablesize) {
2955e0bda7d8SBart Van Assche 		pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
295649248644SDavid Dillow 		if (!cmd_sg_entries)
295749248644SDavid Dillow 			cmd_sg_entries = srp_sg_tablesize;
295849248644SDavid Dillow 	}
295949248644SDavid Dillow 
296049248644SDavid Dillow 	if (!cmd_sg_entries)
296149248644SDavid Dillow 		cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
296249248644SDavid Dillow 
296349248644SDavid Dillow 	if (cmd_sg_entries > 255) {
2964e0bda7d8SBart Van Assche 		pr_warn("Clamping cmd_sg_entries to 255\n");
296549248644SDavid Dillow 		cmd_sg_entries = 255;
29661e89a194SDavid Dillow 	}
29671e89a194SDavid Dillow 
2968c07d424dSDavid Dillow 	if (!indirect_sg_entries)
2969c07d424dSDavid Dillow 		indirect_sg_entries = cmd_sg_entries;
2970c07d424dSDavid Dillow 	else if (indirect_sg_entries < cmd_sg_entries) {
2971e0bda7d8SBart Van Assche 		pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2972e0bda7d8SBart Van Assche 			cmd_sg_entries);
2973c07d424dSDavid Dillow 		indirect_sg_entries = cmd_sg_entries;
2974c07d424dSDavid Dillow 	}
2975c07d424dSDavid Dillow 
29763236822bSFUJITA Tomonori 	ib_srp_transport_template =
29773236822bSFUJITA Tomonori 		srp_attach_transport(&ib_srp_transport_functions);
29783236822bSFUJITA Tomonori 	if (!ib_srp_transport_template)
29793236822bSFUJITA Tomonori 		return -ENOMEM;
29803236822bSFUJITA Tomonori 
2981aef9ec39SRoland Dreier 	ret = class_register(&srp_class);
2982aef9ec39SRoland Dreier 	if (ret) {
2983e0bda7d8SBart Van Assche 		pr_err("couldn't register class infiniband_srp\n");
29843236822bSFUJITA Tomonori 		srp_release_transport(ib_srp_transport_template);
2985aef9ec39SRoland Dreier 		return ret;
2986aef9ec39SRoland Dreier 	}
2987aef9ec39SRoland Dreier 
2988c1a0b23bSMichael S. Tsirkin 	ib_sa_register_client(&srp_sa_client);
2989c1a0b23bSMichael S. Tsirkin 
2990aef9ec39SRoland Dreier 	ret = ib_register_client(&srp_client);
2991aef9ec39SRoland Dreier 	if (ret) {
2992e0bda7d8SBart Van Assche 		pr_err("couldn't register IB client\n");
29933236822bSFUJITA Tomonori 		srp_release_transport(ib_srp_transport_template);
2994c1a0b23bSMichael S. Tsirkin 		ib_sa_unregister_client(&srp_sa_client);
2995aef9ec39SRoland Dreier 		class_unregister(&srp_class);
2996aef9ec39SRoland Dreier 		return ret;
2997aef9ec39SRoland Dreier 	}
2998aef9ec39SRoland Dreier 
2999aef9ec39SRoland Dreier 	return 0;
3000aef9ec39SRoland Dreier }
3001aef9ec39SRoland Dreier 
3002aef9ec39SRoland Dreier static void __exit srp_cleanup_module(void)
3003aef9ec39SRoland Dreier {
3004aef9ec39SRoland Dreier 	ib_unregister_client(&srp_client);
3005c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&srp_sa_client);
3006aef9ec39SRoland Dreier 	class_unregister(&srp_class);
30073236822bSFUJITA Tomonori 	srp_release_transport(ib_srp_transport_template);
3008aef9ec39SRoland Dreier }
3009aef9ec39SRoland Dreier 
3010aef9ec39SRoland Dreier module_init(srp_init_module);
3011aef9ec39SRoland Dreier module_exit(srp_cleanup_module);
3012