xref: /linux/drivers/infiniband/ulp/srp/ib_srp.c (revision da9d2f07306fc29a2f10885c2b0a463f3863c365)
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 
33aef9ec39SRoland Dreier #include <linux/module.h>
34aef9ec39SRoland Dreier #include <linux/init.h>
35aef9ec39SRoland Dreier #include <linux/slab.h>
36aef9ec39SRoland Dreier #include <linux/err.h>
37aef9ec39SRoland Dreier #include <linux/string.h>
38aef9ec39SRoland Dreier #include <linux/parser.h>
39aef9ec39SRoland Dreier #include <linux/random.h>
40de25968cSTim Schmielau #include <linux/jiffies.h>
41aef9ec39SRoland Dreier 
42aef9ec39SRoland Dreier #include <asm/atomic.h>
43aef9ec39SRoland Dreier 
44aef9ec39SRoland Dreier #include <scsi/scsi.h>
45aef9ec39SRoland Dreier #include <scsi/scsi_device.h>
46aef9ec39SRoland Dreier #include <scsi/scsi_dbg.h>
47aef9ec39SRoland Dreier #include <scsi/srp.h>
483236822bSFUJITA Tomonori #include <scsi/scsi_transport_srp.h>
49aef9ec39SRoland Dreier 
50aef9ec39SRoland Dreier #include "ib_srp.h"
51aef9ec39SRoland Dreier 
52aef9ec39SRoland Dreier #define DRV_NAME	"ib_srp"
53aef9ec39SRoland Dreier #define PFX		DRV_NAME ": "
54aef9ec39SRoland Dreier #define DRV_VERSION	"0.2"
55aef9ec39SRoland Dreier #define DRV_RELDATE	"November 1, 2005"
56aef9ec39SRoland Dreier 
57aef9ec39SRoland Dreier MODULE_AUTHOR("Roland Dreier");
58aef9ec39SRoland Dreier MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
59aef9ec39SRoland Dreier 		   "v" DRV_VERSION " (" DRV_RELDATE ")");
60aef9ec39SRoland Dreier MODULE_LICENSE("Dual BSD/GPL");
61aef9ec39SRoland Dreier 
6274b0a15bSVu Pham static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
6374b0a15bSVu Pham static int srp_max_iu_len;
6474b0a15bSVu Pham 
6574b0a15bSVu Pham module_param(srp_sg_tablesize, int, 0444);
6674b0a15bSVu Pham MODULE_PARM_DESC(srp_sg_tablesize,
671e89a194SDavid Dillow 		 "Max number of gather/scatter entries per I/O (default is 12, max 255)");
6874b0a15bSVu Pham 
69aef9ec39SRoland Dreier static int topspin_workarounds = 1;
70aef9ec39SRoland Dreier 
71aef9ec39SRoland Dreier module_param(topspin_workarounds, int, 0444);
72aef9ec39SRoland Dreier MODULE_PARM_DESC(topspin_workarounds,
73aef9ec39SRoland Dreier 		 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
74aef9ec39SRoland Dreier 
75559ce8f1SIshai Rabinovitz static int mellanox_workarounds = 1;
76559ce8f1SIshai Rabinovitz 
77559ce8f1SIshai Rabinovitz module_param(mellanox_workarounds, int, 0444);
78559ce8f1SIshai Rabinovitz MODULE_PARM_DESC(mellanox_workarounds,
79559ce8f1SIshai Rabinovitz 		 "Enable workarounds for Mellanox SRP target bugs if != 0");
80559ce8f1SIshai Rabinovitz 
81aef9ec39SRoland Dreier static void srp_add_one(struct ib_device *device);
82aef9ec39SRoland Dreier static void srp_remove_one(struct ib_device *device);
839c03dc9fSBart Van Assche static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
849c03dc9fSBart Van Assche static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
85aef9ec39SRoland Dreier static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
86aef9ec39SRoland Dreier 
873236822bSFUJITA Tomonori static struct scsi_transport_template *ib_srp_transport_template;
883236822bSFUJITA Tomonori 
89aef9ec39SRoland Dreier static struct ib_client srp_client = {
90aef9ec39SRoland Dreier 	.name   = "srp",
91aef9ec39SRoland Dreier 	.add    = srp_add_one,
92aef9ec39SRoland Dreier 	.remove = srp_remove_one
93aef9ec39SRoland Dreier };
94aef9ec39SRoland Dreier 
95c1a0b23bSMichael S. Tsirkin static struct ib_sa_client srp_sa_client;
96c1a0b23bSMichael S. Tsirkin 
97aef9ec39SRoland Dreier static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
98aef9ec39SRoland Dreier {
99aef9ec39SRoland Dreier 	return (struct srp_target_port *) host->hostdata;
100aef9ec39SRoland Dreier }
101aef9ec39SRoland Dreier 
102aef9ec39SRoland Dreier static const char *srp_target_info(struct Scsi_Host *host)
103aef9ec39SRoland Dreier {
104aef9ec39SRoland Dreier 	return host_to_target(host)->target_name;
105aef9ec39SRoland Dreier }
106aef9ec39SRoland Dreier 
1075d7cbfd6SRoland Dreier static int srp_target_is_topspin(struct srp_target_port *target)
1085d7cbfd6SRoland Dreier {
1095d7cbfd6SRoland Dreier 	static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
1103d1ff48dSRaghava Kondapalli 	static const u8 cisco_oui[3]   = { 0x00, 0x1b, 0x0d };
1115d7cbfd6SRoland Dreier 
1125d7cbfd6SRoland Dreier 	return topspin_workarounds &&
1133d1ff48dSRaghava Kondapalli 		(!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
1143d1ff48dSRaghava Kondapalli 		 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
1155d7cbfd6SRoland Dreier }
1165d7cbfd6SRoland Dreier 
1175d7cbfd6SRoland Dreier static int srp_target_is_mellanox(struct srp_target_port *target)
1185d7cbfd6SRoland Dreier {
1195d7cbfd6SRoland Dreier 	static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
1205d7cbfd6SRoland Dreier 
1215d7cbfd6SRoland Dreier 	return mellanox_workarounds &&
1225d7cbfd6SRoland Dreier 		!memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
1235d7cbfd6SRoland Dreier }
1245d7cbfd6SRoland Dreier 
125aef9ec39SRoland Dreier static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
126aef9ec39SRoland Dreier 				   gfp_t gfp_mask,
127aef9ec39SRoland Dreier 				   enum dma_data_direction direction)
128aef9ec39SRoland Dreier {
129aef9ec39SRoland Dreier 	struct srp_iu *iu;
130aef9ec39SRoland Dreier 
131aef9ec39SRoland Dreier 	iu = kmalloc(sizeof *iu, gfp_mask);
132aef9ec39SRoland Dreier 	if (!iu)
133aef9ec39SRoland Dreier 		goto out;
134aef9ec39SRoland Dreier 
135aef9ec39SRoland Dreier 	iu->buf = kzalloc(size, gfp_mask);
136aef9ec39SRoland Dreier 	if (!iu->buf)
137aef9ec39SRoland Dreier 		goto out_free_iu;
138aef9ec39SRoland Dreier 
13905321937SGreg Kroah-Hartman 	iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
14005321937SGreg Kroah-Hartman 				    direction);
14105321937SGreg Kroah-Hartman 	if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
142aef9ec39SRoland Dreier 		goto out_free_buf;
143aef9ec39SRoland Dreier 
144aef9ec39SRoland Dreier 	iu->size      = size;
145aef9ec39SRoland Dreier 	iu->direction = direction;
146aef9ec39SRoland Dreier 
147aef9ec39SRoland Dreier 	return iu;
148aef9ec39SRoland Dreier 
149aef9ec39SRoland Dreier out_free_buf:
150aef9ec39SRoland Dreier 	kfree(iu->buf);
151aef9ec39SRoland Dreier out_free_iu:
152aef9ec39SRoland Dreier 	kfree(iu);
153aef9ec39SRoland Dreier out:
154aef9ec39SRoland Dreier 	return NULL;
155aef9ec39SRoland Dreier }
156aef9ec39SRoland Dreier 
157aef9ec39SRoland Dreier static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
158aef9ec39SRoland Dreier {
159aef9ec39SRoland Dreier 	if (!iu)
160aef9ec39SRoland Dreier 		return;
161aef9ec39SRoland Dreier 
16205321937SGreg Kroah-Hartman 	ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
16305321937SGreg Kroah-Hartman 			    iu->direction);
164aef9ec39SRoland Dreier 	kfree(iu->buf);
165aef9ec39SRoland Dreier 	kfree(iu);
166aef9ec39SRoland Dreier }
167aef9ec39SRoland Dreier 
168aef9ec39SRoland Dreier static void srp_qp_event(struct ib_event *event, void *context)
169aef9ec39SRoland Dreier {
170aef9ec39SRoland Dreier 	printk(KERN_ERR PFX "QP event %d\n", event->event);
171aef9ec39SRoland Dreier }
172aef9ec39SRoland Dreier 
173aef9ec39SRoland Dreier static int srp_init_qp(struct srp_target_port *target,
174aef9ec39SRoland Dreier 		       struct ib_qp *qp)
175aef9ec39SRoland Dreier {
176aef9ec39SRoland Dreier 	struct ib_qp_attr *attr;
177aef9ec39SRoland Dreier 	int ret;
178aef9ec39SRoland Dreier 
179aef9ec39SRoland Dreier 	attr = kmalloc(sizeof *attr, GFP_KERNEL);
180aef9ec39SRoland Dreier 	if (!attr)
181aef9ec39SRoland Dreier 		return -ENOMEM;
182aef9ec39SRoland Dreier 
183969a60f9SRoland Dreier 	ret = ib_find_pkey(target->srp_host->srp_dev->dev,
184aef9ec39SRoland Dreier 			   target->srp_host->port,
185aef9ec39SRoland Dreier 			   be16_to_cpu(target->path.pkey),
186aef9ec39SRoland Dreier 			   &attr->pkey_index);
187aef9ec39SRoland Dreier 	if (ret)
188aef9ec39SRoland Dreier 		goto out;
189aef9ec39SRoland Dreier 
190aef9ec39SRoland Dreier 	attr->qp_state        = IB_QPS_INIT;
191aef9ec39SRoland Dreier 	attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
192aef9ec39SRoland Dreier 				    IB_ACCESS_REMOTE_WRITE);
193aef9ec39SRoland Dreier 	attr->port_num        = target->srp_host->port;
194aef9ec39SRoland Dreier 
195aef9ec39SRoland Dreier 	ret = ib_modify_qp(qp, attr,
196aef9ec39SRoland Dreier 			   IB_QP_STATE		|
197aef9ec39SRoland Dreier 			   IB_QP_PKEY_INDEX	|
198aef9ec39SRoland Dreier 			   IB_QP_ACCESS_FLAGS	|
199aef9ec39SRoland Dreier 			   IB_QP_PORT);
200aef9ec39SRoland Dreier 
201aef9ec39SRoland Dreier out:
202aef9ec39SRoland Dreier 	kfree(attr);
203aef9ec39SRoland Dreier 	return ret;
204aef9ec39SRoland Dreier }
205aef9ec39SRoland Dreier 
2069fe4bcf4SDavid Dillow static int srp_new_cm_id(struct srp_target_port *target)
2079fe4bcf4SDavid Dillow {
2089fe4bcf4SDavid Dillow 	struct ib_cm_id *new_cm_id;
2099fe4bcf4SDavid Dillow 
21005321937SGreg Kroah-Hartman 	new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
2119fe4bcf4SDavid Dillow 				    srp_cm_handler, target);
2129fe4bcf4SDavid Dillow 	if (IS_ERR(new_cm_id))
2139fe4bcf4SDavid Dillow 		return PTR_ERR(new_cm_id);
2149fe4bcf4SDavid Dillow 
2159fe4bcf4SDavid Dillow 	if (target->cm_id)
2169fe4bcf4SDavid Dillow 		ib_destroy_cm_id(target->cm_id);
2179fe4bcf4SDavid Dillow 	target->cm_id = new_cm_id;
2189fe4bcf4SDavid Dillow 
2199fe4bcf4SDavid Dillow 	return 0;
2209fe4bcf4SDavid Dillow }
2219fe4bcf4SDavid Dillow 
222aef9ec39SRoland Dreier static int srp_create_target_ib(struct srp_target_port *target)
223aef9ec39SRoland Dreier {
224aef9ec39SRoland Dreier 	struct ib_qp_init_attr *init_attr;
225aef9ec39SRoland Dreier 	int ret;
226aef9ec39SRoland Dreier 
227aef9ec39SRoland Dreier 	init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
228aef9ec39SRoland Dreier 	if (!init_attr)
229aef9ec39SRoland Dreier 		return -ENOMEM;
230aef9ec39SRoland Dreier 
2319c03dc9fSBart Van Assche 	target->recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
2329c03dc9fSBart Van Assche 				       srp_recv_completion, NULL, target, SRP_RQ_SIZE, 0);
2339c03dc9fSBart Van Assche 	if (IS_ERR(target->recv_cq)) {
2349c03dc9fSBart Van Assche 		ret = PTR_ERR(target->recv_cq);
235*da9d2f07SRoland Dreier 		goto err;
236aef9ec39SRoland Dreier 	}
237aef9ec39SRoland Dreier 
2389c03dc9fSBart Van Assche 	target->send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
2399c03dc9fSBart Van Assche 				       srp_send_completion, NULL, target, SRP_SQ_SIZE, 0);
2409c03dc9fSBart Van Assche 	if (IS_ERR(target->send_cq)) {
2419c03dc9fSBart Van Assche 		ret = PTR_ERR(target->send_cq);
242*da9d2f07SRoland Dreier 		goto err_recv_cq;
2439c03dc9fSBart Van Assche 	}
2449c03dc9fSBart Van Assche 
2459c03dc9fSBart Van Assche 	ib_req_notify_cq(target->recv_cq, IB_CQ_NEXT_COMP);
246aef9ec39SRoland Dreier 
247aef9ec39SRoland Dreier 	init_attr->event_handler       = srp_qp_event;
248aef9ec39SRoland Dreier 	init_attr->cap.max_send_wr     = SRP_SQ_SIZE;
249aef9ec39SRoland Dreier 	init_attr->cap.max_recv_wr     = SRP_RQ_SIZE;
250aef9ec39SRoland Dreier 	init_attr->cap.max_recv_sge    = 1;
251aef9ec39SRoland Dreier 	init_attr->cap.max_send_sge    = 1;
252aef9ec39SRoland Dreier 	init_attr->sq_sig_type         = IB_SIGNAL_ALL_WR;
253aef9ec39SRoland Dreier 	init_attr->qp_type             = IB_QPT_RC;
2549c03dc9fSBart Van Assche 	init_attr->send_cq             = target->send_cq;
2559c03dc9fSBart Van Assche 	init_attr->recv_cq             = target->recv_cq;
256aef9ec39SRoland Dreier 
25705321937SGreg Kroah-Hartman 	target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
258aef9ec39SRoland Dreier 	if (IS_ERR(target->qp)) {
259aef9ec39SRoland Dreier 		ret = PTR_ERR(target->qp);
260*da9d2f07SRoland Dreier 		goto err_send_cq;
261aef9ec39SRoland Dreier 	}
262aef9ec39SRoland Dreier 
263aef9ec39SRoland Dreier 	ret = srp_init_qp(target, target->qp);
264*da9d2f07SRoland Dreier 	if (ret)
265*da9d2f07SRoland Dreier 		goto err_qp;
266aef9ec39SRoland Dreier 
267*da9d2f07SRoland Dreier 	kfree(init_attr);
268*da9d2f07SRoland Dreier 	return 0;
269*da9d2f07SRoland Dreier 
270*da9d2f07SRoland Dreier err_qp:
271*da9d2f07SRoland Dreier 	ib_destroy_qp(target->qp);
272*da9d2f07SRoland Dreier 
273*da9d2f07SRoland Dreier err_send_cq:
274*da9d2f07SRoland Dreier 	ib_destroy_cq(target->send_cq);
275*da9d2f07SRoland Dreier 
276*da9d2f07SRoland Dreier err_recv_cq:
277*da9d2f07SRoland Dreier 	ib_destroy_cq(target->recv_cq);
278*da9d2f07SRoland Dreier 
279*da9d2f07SRoland Dreier err:
280aef9ec39SRoland Dreier 	kfree(init_attr);
281aef9ec39SRoland Dreier 	return ret;
282aef9ec39SRoland Dreier }
283aef9ec39SRoland Dreier 
284aef9ec39SRoland Dreier static void srp_free_target_ib(struct srp_target_port *target)
285aef9ec39SRoland Dreier {
286aef9ec39SRoland Dreier 	int i;
287aef9ec39SRoland Dreier 
288aef9ec39SRoland Dreier 	ib_destroy_qp(target->qp);
2899c03dc9fSBart Van Assche 	ib_destroy_cq(target->send_cq);
2909c03dc9fSBart Van Assche 	ib_destroy_cq(target->recv_cq);
291aef9ec39SRoland Dreier 
292aef9ec39SRoland Dreier 	for (i = 0; i < SRP_RQ_SIZE; ++i)
293aef9ec39SRoland Dreier 		srp_free_iu(target->srp_host, target->rx_ring[i]);
294aef9ec39SRoland Dreier 	for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
295aef9ec39SRoland Dreier 		srp_free_iu(target->srp_host, target->tx_ring[i]);
296aef9ec39SRoland Dreier }
297aef9ec39SRoland Dreier 
298aef9ec39SRoland Dreier static void srp_path_rec_completion(int status,
299aef9ec39SRoland Dreier 				    struct ib_sa_path_rec *pathrec,
300aef9ec39SRoland Dreier 				    void *target_ptr)
301aef9ec39SRoland Dreier {
302aef9ec39SRoland Dreier 	struct srp_target_port *target = target_ptr;
303aef9ec39SRoland Dreier 
304aef9ec39SRoland Dreier 	target->status = status;
305aef9ec39SRoland Dreier 	if (status)
3067aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
3077aa54bd7SDavid Dillow 			     PFX "Got failed path rec status %d\n", status);
308aef9ec39SRoland Dreier 	else
309aef9ec39SRoland Dreier 		target->path = *pathrec;
310aef9ec39SRoland Dreier 	complete(&target->done);
311aef9ec39SRoland Dreier }
312aef9ec39SRoland Dreier 
313aef9ec39SRoland Dreier static int srp_lookup_path(struct srp_target_port *target)
314aef9ec39SRoland Dreier {
315aef9ec39SRoland Dreier 	target->path.numb_path = 1;
316aef9ec39SRoland Dreier 
317aef9ec39SRoland Dreier 	init_completion(&target->done);
318aef9ec39SRoland Dreier 
319c1a0b23bSMichael S. Tsirkin 	target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
32005321937SGreg Kroah-Hartman 						   target->srp_host->srp_dev->dev,
321aef9ec39SRoland Dreier 						   target->srp_host->port,
322aef9ec39SRoland Dreier 						   &target->path,
323247e020eSSean Hefty 						   IB_SA_PATH_REC_SERVICE_ID	|
324aef9ec39SRoland Dreier 						   IB_SA_PATH_REC_DGID		|
325aef9ec39SRoland Dreier 						   IB_SA_PATH_REC_SGID		|
326aef9ec39SRoland Dreier 						   IB_SA_PATH_REC_NUMB_PATH	|
327aef9ec39SRoland Dreier 						   IB_SA_PATH_REC_PKEY,
328aef9ec39SRoland Dreier 						   SRP_PATH_REC_TIMEOUT_MS,
329aef9ec39SRoland Dreier 						   GFP_KERNEL,
330aef9ec39SRoland Dreier 						   srp_path_rec_completion,
331aef9ec39SRoland Dreier 						   target, &target->path_query);
332aef9ec39SRoland Dreier 	if (target->path_query_id < 0)
333aef9ec39SRoland Dreier 		return target->path_query_id;
334aef9ec39SRoland Dreier 
335aef9ec39SRoland Dreier 	wait_for_completion(&target->done);
336aef9ec39SRoland Dreier 
337aef9ec39SRoland Dreier 	if (target->status < 0)
3387aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
3397aa54bd7SDavid Dillow 			     PFX "Path record query failed\n");
340aef9ec39SRoland Dreier 
341aef9ec39SRoland Dreier 	return target->status;
342aef9ec39SRoland Dreier }
343aef9ec39SRoland Dreier 
344aef9ec39SRoland Dreier static int srp_send_req(struct srp_target_port *target)
345aef9ec39SRoland Dreier {
346aef9ec39SRoland Dreier 	struct {
347aef9ec39SRoland Dreier 		struct ib_cm_req_param param;
348aef9ec39SRoland Dreier 		struct srp_login_req   priv;
349aef9ec39SRoland Dreier 	} *req = NULL;
350aef9ec39SRoland Dreier 	int status;
351aef9ec39SRoland Dreier 
352aef9ec39SRoland Dreier 	req = kzalloc(sizeof *req, GFP_KERNEL);
353aef9ec39SRoland Dreier 	if (!req)
354aef9ec39SRoland Dreier 		return -ENOMEM;
355aef9ec39SRoland Dreier 
356aef9ec39SRoland Dreier 	req->param.primary_path 	      = &target->path;
357aef9ec39SRoland Dreier 	req->param.alternate_path 	      = NULL;
358aef9ec39SRoland Dreier 	req->param.service_id 		      = target->service_id;
359aef9ec39SRoland Dreier 	req->param.qp_num 		      = target->qp->qp_num;
360aef9ec39SRoland Dreier 	req->param.qp_type 		      = target->qp->qp_type;
361aef9ec39SRoland Dreier 	req->param.private_data 	      = &req->priv;
362aef9ec39SRoland Dreier 	req->param.private_data_len 	      = sizeof req->priv;
363aef9ec39SRoland Dreier 	req->param.flow_control 	      = 1;
364aef9ec39SRoland Dreier 
365aef9ec39SRoland Dreier 	get_random_bytes(&req->param.starting_psn, 4);
366aef9ec39SRoland Dreier 	req->param.starting_psn 	     &= 0xffffff;
367aef9ec39SRoland Dreier 
368aef9ec39SRoland Dreier 	/*
369aef9ec39SRoland Dreier 	 * Pick some arbitrary defaults here; we could make these
370aef9ec39SRoland Dreier 	 * module parameters if anyone cared about setting them.
371aef9ec39SRoland Dreier 	 */
372aef9ec39SRoland Dreier 	req->param.responder_resources	      = 4;
373aef9ec39SRoland Dreier 	req->param.remote_cm_response_timeout = 20;
374aef9ec39SRoland Dreier 	req->param.local_cm_response_timeout  = 20;
375aef9ec39SRoland Dreier 	req->param.retry_count 		      = 7;
376aef9ec39SRoland Dreier 	req->param.rnr_retry_count 	      = 7;
377aef9ec39SRoland Dreier 	req->param.max_cm_retries 	      = 15;
378aef9ec39SRoland Dreier 
379aef9ec39SRoland Dreier 	req->priv.opcode     	= SRP_LOGIN_REQ;
380aef9ec39SRoland Dreier 	req->priv.tag        	= 0;
38174b0a15bSVu Pham 	req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
382aef9ec39SRoland Dreier 	req->priv.req_buf_fmt 	= cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
383aef9ec39SRoland Dreier 					      SRP_BUF_FORMAT_INDIRECT);
3840c0450dbSRamachandra K 	/*
3850c0450dbSRamachandra K 	 * In the published SRP specification (draft rev. 16a), the
3860c0450dbSRamachandra K 	 * port identifier format is 8 bytes of ID extension followed
3870c0450dbSRamachandra K 	 * by 8 bytes of GUID.  Older drafts put the two halves in the
3880c0450dbSRamachandra K 	 * opposite order, so that the GUID comes first.
3890c0450dbSRamachandra K 	 *
3900c0450dbSRamachandra K 	 * Targets conforming to these obsolete drafts can be
3910c0450dbSRamachandra K 	 * recognized by the I/O Class they report.
3920c0450dbSRamachandra K 	 */
3930c0450dbSRamachandra K 	if (target->io_class == SRP_REV10_IB_IO_CLASS) {
3940c0450dbSRamachandra K 		memcpy(req->priv.initiator_port_id,
39501cb9bcbSIshai Rabinovitz 		       &target->path.sgid.global.interface_id, 8);
3960c0450dbSRamachandra K 		memcpy(req->priv.initiator_port_id + 8,
39701cb9bcbSIshai Rabinovitz 		       &target->initiator_ext, 8);
3980c0450dbSRamachandra K 		memcpy(req->priv.target_port_id,     &target->ioc_guid, 8);
3990c0450dbSRamachandra K 		memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
4000c0450dbSRamachandra K 	} else {
4010c0450dbSRamachandra K 		memcpy(req->priv.initiator_port_id,
40201cb9bcbSIshai Rabinovitz 		       &target->initiator_ext, 8);
40301cb9bcbSIshai Rabinovitz 		memcpy(req->priv.initiator_port_id + 8,
40401cb9bcbSIshai Rabinovitz 		       &target->path.sgid.global.interface_id, 8);
4050c0450dbSRamachandra K 		memcpy(req->priv.target_port_id,     &target->id_ext, 8);
4060c0450dbSRamachandra K 		memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
4070c0450dbSRamachandra K 	}
4080c0450dbSRamachandra K 
409aef9ec39SRoland Dreier 	/*
410aef9ec39SRoland Dreier 	 * Topspin/Cisco SRP targets will reject our login unless we
41101cb9bcbSIshai Rabinovitz 	 * zero out the first 8 bytes of our initiator port ID and set
41201cb9bcbSIshai Rabinovitz 	 * the second 8 bytes to the local node GUID.
413aef9ec39SRoland Dreier 	 */
4145d7cbfd6SRoland Dreier 	if (srp_target_is_topspin(target)) {
4157aa54bd7SDavid Dillow 		shost_printk(KERN_DEBUG, target->scsi_host,
4167aa54bd7SDavid Dillow 			     PFX "Topspin/Cisco initiator port ID workaround "
417aef9ec39SRoland Dreier 			     "activated for target GUID %016llx\n",
418aef9ec39SRoland Dreier 			     (unsigned long long) be64_to_cpu(target->ioc_guid));
419aef9ec39SRoland Dreier 		memset(req->priv.initiator_port_id, 0, 8);
42001cb9bcbSIshai Rabinovitz 		memcpy(req->priv.initiator_port_id + 8,
42105321937SGreg Kroah-Hartman 		       &target->srp_host->srp_dev->dev->node_guid, 8);
422aef9ec39SRoland Dreier 	}
423aef9ec39SRoland Dreier 
424aef9ec39SRoland Dreier 	status = ib_send_cm_req(target->cm_id, &req->param);
425aef9ec39SRoland Dreier 
426aef9ec39SRoland Dreier 	kfree(req);
427aef9ec39SRoland Dreier 
428aef9ec39SRoland Dreier 	return status;
429aef9ec39SRoland Dreier }
430aef9ec39SRoland Dreier 
431aef9ec39SRoland Dreier static void srp_disconnect_target(struct srp_target_port *target)
432aef9ec39SRoland Dreier {
433aef9ec39SRoland Dreier 	/* XXX should send SRP_I_LOGOUT request */
434aef9ec39SRoland Dreier 
435aef9ec39SRoland Dreier 	init_completion(&target->done);
436e6581056SRoland Dreier 	if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
4377aa54bd7SDavid Dillow 		shost_printk(KERN_DEBUG, target->scsi_host,
4387aa54bd7SDavid Dillow 			     PFX "Sending CM DREQ failed\n");
439e6581056SRoland Dreier 		return;
440e6581056SRoland Dreier 	}
441aef9ec39SRoland Dreier 	wait_for_completion(&target->done);
442aef9ec39SRoland Dreier }
443aef9ec39SRoland Dreier 
444c4028958SDavid Howells static void srp_remove_work(struct work_struct *work)
445aef9ec39SRoland Dreier {
446c4028958SDavid Howells 	struct srp_target_port *target =
447c4028958SDavid Howells 		container_of(work, struct srp_target_port, work);
448aef9ec39SRoland Dreier 
449aef9ec39SRoland Dreier 	spin_lock_irq(target->scsi_host->host_lock);
450aef9ec39SRoland Dreier 	if (target->state != SRP_TARGET_DEAD) {
451aef9ec39SRoland Dreier 		spin_unlock_irq(target->scsi_host->host_lock);
452aef9ec39SRoland Dreier 		return;
453aef9ec39SRoland Dreier 	}
454aef9ec39SRoland Dreier 	target->state = SRP_TARGET_REMOVED;
455aef9ec39SRoland Dreier 	spin_unlock_irq(target->scsi_host->host_lock);
456aef9ec39SRoland Dreier 
457b3589fd4SMatthew Wilcox 	spin_lock(&target->srp_host->target_lock);
458aef9ec39SRoland Dreier 	list_del(&target->list);
459b3589fd4SMatthew Wilcox 	spin_unlock(&target->srp_host->target_lock);
460aef9ec39SRoland Dreier 
4613236822bSFUJITA Tomonori 	srp_remove_host(target->scsi_host);
462aef9ec39SRoland Dreier 	scsi_remove_host(target->scsi_host);
463aef9ec39SRoland Dreier 	ib_destroy_cm_id(target->cm_id);
464aef9ec39SRoland Dreier 	srp_free_target_ib(target);
465aef9ec39SRoland Dreier 	scsi_host_put(target->scsi_host);
466aef9ec39SRoland Dreier }
467aef9ec39SRoland Dreier 
468aef9ec39SRoland Dreier static int srp_connect_target(struct srp_target_port *target)
469aef9ec39SRoland Dreier {
4709fe4bcf4SDavid Dillow 	int retries = 3;
471aef9ec39SRoland Dreier 	int ret;
472aef9ec39SRoland Dreier 
473aef9ec39SRoland Dreier 	ret = srp_lookup_path(target);
474aef9ec39SRoland Dreier 	if (ret)
475aef9ec39SRoland Dreier 		return ret;
476aef9ec39SRoland Dreier 
477aef9ec39SRoland Dreier 	while (1) {
478aef9ec39SRoland Dreier 		init_completion(&target->done);
479aef9ec39SRoland Dreier 		ret = srp_send_req(target);
480aef9ec39SRoland Dreier 		if (ret)
481aef9ec39SRoland Dreier 			return ret;
482aef9ec39SRoland Dreier 		wait_for_completion(&target->done);
483aef9ec39SRoland Dreier 
484aef9ec39SRoland Dreier 		/*
485aef9ec39SRoland Dreier 		 * The CM event handling code will set status to
486aef9ec39SRoland Dreier 		 * SRP_PORT_REDIRECT if we get a port redirect REJ
487aef9ec39SRoland Dreier 		 * back, or SRP_DLID_REDIRECT if we get a lid/qp
488aef9ec39SRoland Dreier 		 * redirect REJ back.
489aef9ec39SRoland Dreier 		 */
490aef9ec39SRoland Dreier 		switch (target->status) {
491aef9ec39SRoland Dreier 		case 0:
492aef9ec39SRoland Dreier 			return 0;
493aef9ec39SRoland Dreier 
494aef9ec39SRoland Dreier 		case SRP_PORT_REDIRECT:
495aef9ec39SRoland Dreier 			ret = srp_lookup_path(target);
496aef9ec39SRoland Dreier 			if (ret)
497aef9ec39SRoland Dreier 				return ret;
498aef9ec39SRoland Dreier 			break;
499aef9ec39SRoland Dreier 
500aef9ec39SRoland Dreier 		case SRP_DLID_REDIRECT:
501aef9ec39SRoland Dreier 			break;
502aef9ec39SRoland Dreier 
5039fe4bcf4SDavid Dillow 		case SRP_STALE_CONN:
5049fe4bcf4SDavid Dillow 			/* Our current CM id was stale, and is now in timewait.
5059fe4bcf4SDavid Dillow 			 * Try to reconnect with a new one.
5069fe4bcf4SDavid Dillow 			 */
5079fe4bcf4SDavid Dillow 			if (!retries-- || srp_new_cm_id(target)) {
5089fe4bcf4SDavid Dillow 				shost_printk(KERN_ERR, target->scsi_host, PFX
5099fe4bcf4SDavid Dillow 					     "giving up on stale connection\n");
5109fe4bcf4SDavid Dillow 				target->status = -ECONNRESET;
5119fe4bcf4SDavid Dillow 				return target->status;
5129fe4bcf4SDavid Dillow 			}
5139fe4bcf4SDavid Dillow 
5149fe4bcf4SDavid Dillow 			shost_printk(KERN_ERR, target->scsi_host, PFX
5159fe4bcf4SDavid Dillow 				     "retrying stale connection\n");
5169fe4bcf4SDavid Dillow 			break;
5179fe4bcf4SDavid Dillow 
518aef9ec39SRoland Dreier 		default:
519aef9ec39SRoland Dreier 			return target->status;
520aef9ec39SRoland Dreier 		}
521aef9ec39SRoland Dreier 	}
522aef9ec39SRoland Dreier }
523aef9ec39SRoland Dreier 
524d945e1dfSRoland Dreier static void srp_unmap_data(struct scsi_cmnd *scmnd,
525d945e1dfSRoland Dreier 			   struct srp_target_port *target,
526d945e1dfSRoland Dreier 			   struct srp_request *req)
527d945e1dfSRoland Dreier {
528bb350d1dSFUJITA Tomonori 	if (!scsi_sglist(scmnd) ||
529d945e1dfSRoland Dreier 	    (scmnd->sc_data_direction != DMA_TO_DEVICE &&
530d945e1dfSRoland Dreier 	     scmnd->sc_data_direction != DMA_FROM_DEVICE))
531d945e1dfSRoland Dreier 		return;
532d945e1dfSRoland Dreier 
533f5358a17SRoland Dreier 	if (req->fmr) {
534f5358a17SRoland Dreier 		ib_fmr_pool_unmap(req->fmr);
535f5358a17SRoland Dreier 		req->fmr = NULL;
536f5358a17SRoland Dreier 	}
537f5358a17SRoland Dreier 
53805321937SGreg Kroah-Hartman 	ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
539bb350d1dSFUJITA Tomonori 			scsi_sg_count(scmnd), scmnd->sc_data_direction);
540d945e1dfSRoland Dreier }
541d945e1dfSRoland Dreier 
542526b4caaSIshai Rabinovitz static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
543526b4caaSIshai Rabinovitz {
544526b4caaSIshai Rabinovitz 	srp_unmap_data(req->scmnd, target, req);
545526b4caaSIshai Rabinovitz 	list_move_tail(&req->list, &target->free_reqs);
546526b4caaSIshai Rabinovitz }
547526b4caaSIshai Rabinovitz 
548526b4caaSIshai Rabinovitz static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
549526b4caaSIshai Rabinovitz {
550526b4caaSIshai Rabinovitz 	req->scmnd->result = DID_RESET << 16;
551526b4caaSIshai Rabinovitz 	req->scmnd->scsi_done(req->scmnd);
552526b4caaSIshai Rabinovitz 	srp_remove_req(target, req);
553526b4caaSIshai Rabinovitz }
554526b4caaSIshai Rabinovitz 
555aef9ec39SRoland Dreier static int srp_reconnect_target(struct srp_target_port *target)
556aef9ec39SRoland Dreier {
557aef9ec39SRoland Dreier 	struct ib_qp_attr qp_attr;
558526b4caaSIshai Rabinovitz 	struct srp_request *req, *tmp;
559aef9ec39SRoland Dreier 	struct ib_wc wc;
560aef9ec39SRoland Dreier 	int ret;
561aef9ec39SRoland Dreier 
562aef9ec39SRoland Dreier 	spin_lock_irq(target->scsi_host->host_lock);
563aef9ec39SRoland Dreier 	if (target->state != SRP_TARGET_LIVE) {
564aef9ec39SRoland Dreier 		spin_unlock_irq(target->scsi_host->host_lock);
565aef9ec39SRoland Dreier 		return -EAGAIN;
566aef9ec39SRoland Dreier 	}
567aef9ec39SRoland Dreier 	target->state = SRP_TARGET_CONNECTING;
568aef9ec39SRoland Dreier 	spin_unlock_irq(target->scsi_host->host_lock);
569aef9ec39SRoland Dreier 
570aef9ec39SRoland Dreier 	srp_disconnect_target(target);
571aef9ec39SRoland Dreier 	/*
572aef9ec39SRoland Dreier 	 * Now get a new local CM ID so that we avoid confusing the
573aef9ec39SRoland Dreier 	 * target in case things are really fouled up.
574aef9ec39SRoland Dreier 	 */
5759fe4bcf4SDavid Dillow 	ret = srp_new_cm_id(target);
5769fe4bcf4SDavid Dillow 	if (ret)
577aef9ec39SRoland Dreier 		goto err;
578aef9ec39SRoland Dreier 
579aef9ec39SRoland Dreier 	qp_attr.qp_state = IB_QPS_RESET;
580aef9ec39SRoland Dreier 	ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
581aef9ec39SRoland Dreier 	if (ret)
582aef9ec39SRoland Dreier 		goto err;
583aef9ec39SRoland Dreier 
584aef9ec39SRoland Dreier 	ret = srp_init_qp(target, target->qp);
585aef9ec39SRoland Dreier 	if (ret)
586aef9ec39SRoland Dreier 		goto err;
587aef9ec39SRoland Dreier 
5889c03dc9fSBart Van Assche 	while (ib_poll_cq(target->recv_cq, 1, &wc) > 0)
5899c03dc9fSBart Van Assche 		; /* nothing */
5909c03dc9fSBart Van Assche 	while (ib_poll_cq(target->send_cq, 1, &wc) > 0)
591aef9ec39SRoland Dreier 		; /* nothing */
592aef9ec39SRoland Dreier 
593d916a8f1SIshai Rabinovitz 	spin_lock_irq(target->scsi_host->host_lock);
594526b4caaSIshai Rabinovitz 	list_for_each_entry_safe(req, tmp, &target->req_queue, list)
595526b4caaSIshai Rabinovitz 		srp_reset_req(target, req);
596d916a8f1SIshai Rabinovitz 	spin_unlock_irq(target->scsi_host->host_lock);
597aef9ec39SRoland Dreier 
598aef9ec39SRoland Dreier 	target->rx_head	 = 0;
599aef9ec39SRoland Dreier 	target->tx_head	 = 0;
600aef9ec39SRoland Dreier 	target->tx_tail  = 0;
601aef9ec39SRoland Dreier 
6021033ff67SIshai Rabinovitz 	target->qp_in_error = 0;
603aef9ec39SRoland Dreier 	ret = srp_connect_target(target);
604aef9ec39SRoland Dreier 	if (ret)
605aef9ec39SRoland Dreier 		goto err;
606aef9ec39SRoland Dreier 
607aef9ec39SRoland Dreier 	spin_lock_irq(target->scsi_host->host_lock);
608aef9ec39SRoland Dreier 	if (target->state == SRP_TARGET_CONNECTING) {
609aef9ec39SRoland Dreier 		ret = 0;
610aef9ec39SRoland Dreier 		target->state = SRP_TARGET_LIVE;
611aef9ec39SRoland Dreier 	} else
612aef9ec39SRoland Dreier 		ret = -EAGAIN;
613aef9ec39SRoland Dreier 	spin_unlock_irq(target->scsi_host->host_lock);
614aef9ec39SRoland Dreier 
615aef9ec39SRoland Dreier 	return ret;
616aef9ec39SRoland Dreier 
617aef9ec39SRoland Dreier err:
6187aa54bd7SDavid Dillow 	shost_printk(KERN_ERR, target->scsi_host,
6197aa54bd7SDavid Dillow 		     PFX "reconnect failed (%d), removing target port.\n", ret);
620aef9ec39SRoland Dreier 
621aef9ec39SRoland Dreier 	/*
622aef9ec39SRoland Dreier 	 * We couldn't reconnect, so kill our target port off.
623aef9ec39SRoland Dreier 	 * However, we have to defer the real removal because we might
624aef9ec39SRoland Dreier 	 * be in the context of the SCSI error handler now, which
625aef9ec39SRoland Dreier 	 * would deadlock if we call scsi_remove_host().
626aef9ec39SRoland Dreier 	 */
627aef9ec39SRoland Dreier 	spin_lock_irq(target->scsi_host->host_lock);
628aef9ec39SRoland Dreier 	if (target->state == SRP_TARGET_CONNECTING) {
629aef9ec39SRoland Dreier 		target->state = SRP_TARGET_DEAD;
630c4028958SDavid Howells 		INIT_WORK(&target->work, srp_remove_work);
631aef9ec39SRoland Dreier 		schedule_work(&target->work);
632aef9ec39SRoland Dreier 	}
633aef9ec39SRoland Dreier 	spin_unlock_irq(target->scsi_host->host_lock);
634aef9ec39SRoland Dreier 
635aef9ec39SRoland Dreier 	return ret;
636aef9ec39SRoland Dreier }
637aef9ec39SRoland Dreier 
638559ce8f1SIshai Rabinovitz static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
639f5358a17SRoland Dreier 		       int sg_cnt, struct srp_request *req,
640f5358a17SRoland Dreier 		       struct srp_direct_buf *buf)
641f5358a17SRoland Dreier {
642f5358a17SRoland Dreier 	u64 io_addr = 0;
643f5358a17SRoland Dreier 	u64 *dma_pages;
644f5358a17SRoland Dreier 	u32 len;
645f5358a17SRoland Dreier 	int page_cnt;
646f5358a17SRoland Dreier 	int i, j;
647f5358a17SRoland Dreier 	int ret;
64805321937SGreg Kroah-Hartman 	struct srp_device *dev = target->srp_host->srp_dev;
64985507bccSRalph Campbell 	struct ib_device *ibdev = dev->dev;
650bb350d1dSFUJITA Tomonori 	struct scatterlist *sg;
651f5358a17SRoland Dreier 
652f5358a17SRoland Dreier 	if (!dev->fmr_pool)
653f5358a17SRoland Dreier 		return -ENODEV;
654f5358a17SRoland Dreier 
6555d7cbfd6SRoland Dreier 	if (srp_target_is_mellanox(target) &&
6565d7cbfd6SRoland Dreier 	    (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
657559ce8f1SIshai Rabinovitz 		return -EINVAL;
658559ce8f1SIshai Rabinovitz 
659f5358a17SRoland Dreier 	len = page_cnt = 0;
660bb350d1dSFUJITA Tomonori 	scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
661bb350d1dSFUJITA Tomonori 		unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
66285507bccSRalph Campbell 
663bb350d1dSFUJITA Tomonori 		if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
664f5358a17SRoland Dreier 			if (i > 0)
665f5358a17SRoland Dreier 				return -EINVAL;
666f5358a17SRoland Dreier 			else
667f5358a17SRoland Dreier 				++page_cnt;
668f5358a17SRoland Dreier 		}
669bb350d1dSFUJITA Tomonori 		if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
670f5358a17SRoland Dreier 		    ~dev->fmr_page_mask) {
671f5358a17SRoland Dreier 			if (i < sg_cnt - 1)
672f5358a17SRoland Dreier 				return -EINVAL;
673f5358a17SRoland Dreier 			else
674f5358a17SRoland Dreier 				++page_cnt;
675f5358a17SRoland Dreier 		}
676f5358a17SRoland Dreier 
67785507bccSRalph Campbell 		len += dma_len;
678f5358a17SRoland Dreier 	}
679f5358a17SRoland Dreier 
680f5358a17SRoland Dreier 	page_cnt += len >> dev->fmr_page_shift;
681f5358a17SRoland Dreier 	if (page_cnt > SRP_FMR_SIZE)
682f5358a17SRoland Dreier 		return -ENOMEM;
683f5358a17SRoland Dreier 
684f5358a17SRoland Dreier 	dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
685f5358a17SRoland Dreier 	if (!dma_pages)
686f5358a17SRoland Dreier 		return -ENOMEM;
687f5358a17SRoland Dreier 
688f5358a17SRoland Dreier 	page_cnt = 0;
689bb350d1dSFUJITA Tomonori 	scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
690bb350d1dSFUJITA Tomonori 		unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
69185507bccSRalph Campbell 
69285507bccSRalph Campbell 		for (j = 0; j < dma_len; j += dev->fmr_page_size)
693f5358a17SRoland Dreier 			dma_pages[page_cnt++] =
694bb350d1dSFUJITA Tomonori 				(ib_sg_dma_address(ibdev, sg) &
69585507bccSRalph Campbell 				 dev->fmr_page_mask) + j;
69685507bccSRalph Campbell 	}
697f5358a17SRoland Dreier 
698f5358a17SRoland Dreier 	req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
699adfaa888SMichael S. Tsirkin 					dma_pages, page_cnt, io_addr);
700f5358a17SRoland Dreier 	if (IS_ERR(req->fmr)) {
701f5358a17SRoland Dreier 		ret = PTR_ERR(req->fmr);
7026583eb3dSVu Pham 		req->fmr = NULL;
703f5358a17SRoland Dreier 		goto out;
704f5358a17SRoland Dreier 	}
705f5358a17SRoland Dreier 
70685507bccSRalph Campbell 	buf->va  = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
70785507bccSRalph Campbell 			       ~dev->fmr_page_mask);
708f5358a17SRoland Dreier 	buf->key = cpu_to_be32(req->fmr->fmr->rkey);
709f5358a17SRoland Dreier 	buf->len = cpu_to_be32(len);
710f5358a17SRoland Dreier 
711f5358a17SRoland Dreier 	ret = 0;
712f5358a17SRoland Dreier 
713f5358a17SRoland Dreier out:
714f5358a17SRoland Dreier 	kfree(dma_pages);
715f5358a17SRoland Dreier 
716f5358a17SRoland Dreier 	return ret;
717f5358a17SRoland Dreier }
718f5358a17SRoland Dreier 
719aef9ec39SRoland Dreier static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
720aef9ec39SRoland Dreier 			struct srp_request *req)
721aef9ec39SRoland Dreier {
722cf368713SRoland Dreier 	struct scatterlist *scat;
723aef9ec39SRoland Dreier 	struct srp_cmd *cmd = req->cmd->buf;
724cf368713SRoland Dreier 	int len, nents, count;
725f5358a17SRoland Dreier 	u8 fmt = SRP_DATA_DESC_DIRECT;
72685507bccSRalph Campbell 	struct srp_device *dev;
72785507bccSRalph Campbell 	struct ib_device *ibdev;
728aef9ec39SRoland Dreier 
729bb350d1dSFUJITA Tomonori 	if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
730aef9ec39SRoland Dreier 		return sizeof (struct srp_cmd);
731aef9ec39SRoland Dreier 
732aef9ec39SRoland Dreier 	if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
733aef9ec39SRoland Dreier 	    scmnd->sc_data_direction != DMA_TO_DEVICE) {
7347aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
7357aa54bd7SDavid Dillow 			     PFX "Unhandled data direction %d\n",
736aef9ec39SRoland Dreier 			     scmnd->sc_data_direction);
737aef9ec39SRoland Dreier 		return -EINVAL;
738aef9ec39SRoland Dreier 	}
739aef9ec39SRoland Dreier 
740bb350d1dSFUJITA Tomonori 	nents = scsi_sg_count(scmnd);
741bb350d1dSFUJITA Tomonori 	scat  = scsi_sglist(scmnd);
742aef9ec39SRoland Dreier 
74305321937SGreg Kroah-Hartman 	dev = target->srp_host->srp_dev;
74485507bccSRalph Campbell 	ibdev = dev->dev;
74585507bccSRalph Campbell 
74685507bccSRalph Campbell 	count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
747aef9ec39SRoland Dreier 
748aef9ec39SRoland Dreier 	fmt = SRP_DATA_DESC_DIRECT;
749f5358a17SRoland Dreier 	len = sizeof (struct srp_cmd) +	sizeof (struct srp_direct_buf);
750f5358a17SRoland Dreier 
751f5358a17SRoland Dreier 	if (count == 1) {
752f5358a17SRoland Dreier 		/*
753f5358a17SRoland Dreier 		 * The midlayer only generated a single gather/scatter
754f5358a17SRoland Dreier 		 * entry, or DMA mapping coalesced everything to a
755f5358a17SRoland Dreier 		 * single entry.  So a direct descriptor along with
756f5358a17SRoland Dreier 		 * the DMA MR suffices.
757f5358a17SRoland Dreier 		 */
758f5358a17SRoland Dreier 		struct srp_direct_buf *buf = (void *) cmd->add_data;
759aef9ec39SRoland Dreier 
76085507bccSRalph Campbell 		buf->va  = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
76185507bccSRalph Campbell 		buf->key = cpu_to_be32(dev->mr->rkey);
76285507bccSRalph Campbell 		buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
763559ce8f1SIshai Rabinovitz 	} else if (srp_map_fmr(target, scat, count, req,
764f5358a17SRoland Dreier 			       (void *) cmd->add_data)) {
765f5358a17SRoland Dreier 		/*
766f5358a17SRoland Dreier 		 * FMR mapping failed, and the scatterlist has more
767f5358a17SRoland Dreier 		 * than one entry.  Generate an indirect memory
768f5358a17SRoland Dreier 		 * descriptor.
769f5358a17SRoland Dreier 		 */
770aef9ec39SRoland Dreier 		struct srp_indirect_buf *buf = (void *) cmd->add_data;
771bb350d1dSFUJITA Tomonori 		struct scatterlist *sg;
772aef9ec39SRoland Dreier 		u32 datalen = 0;
773f5358a17SRoland Dreier 		int i;
774aef9ec39SRoland Dreier 
775aef9ec39SRoland Dreier 		fmt = SRP_DATA_DESC_INDIRECT;
776f5358a17SRoland Dreier 		len = sizeof (struct srp_cmd) +
777f5358a17SRoland Dreier 			sizeof (struct srp_indirect_buf) +
778f5358a17SRoland Dreier 			count * sizeof (struct srp_direct_buf);
779f5358a17SRoland Dreier 
780bb350d1dSFUJITA Tomonori 		scsi_for_each_sg(scmnd, sg, count, i) {
781bb350d1dSFUJITA Tomonori 			unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
78285507bccSRalph Campbell 
783f5358a17SRoland Dreier 			buf->desc_list[i].va  =
784bb350d1dSFUJITA Tomonori 				cpu_to_be64(ib_sg_dma_address(ibdev, sg));
785f5358a17SRoland Dreier 			buf->desc_list[i].key =
78685507bccSRalph Campbell 				cpu_to_be32(dev->mr->rkey);
78785507bccSRalph Campbell 			buf->desc_list[i].len = cpu_to_be32(dma_len);
78885507bccSRalph Campbell 			datalen += dma_len;
789f5358a17SRoland Dreier 		}
790aef9ec39SRoland Dreier 
791aef9ec39SRoland Dreier 		if (scmnd->sc_data_direction == DMA_TO_DEVICE)
792cf368713SRoland Dreier 			cmd->data_out_desc_cnt = count;
793aef9ec39SRoland Dreier 		else
794cf368713SRoland Dreier 			cmd->data_in_desc_cnt = count;
795aef9ec39SRoland Dreier 
796f5358a17SRoland Dreier 		buf->table_desc.va  =
797f5358a17SRoland Dreier 			cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
798aef9ec39SRoland Dreier 		buf->table_desc.key =
79905321937SGreg Kroah-Hartman 			cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
800aef9ec39SRoland Dreier 		buf->table_desc.len =
801cf368713SRoland Dreier 			cpu_to_be32(count * sizeof (struct srp_direct_buf));
802aef9ec39SRoland Dreier 
803aef9ec39SRoland Dreier 		buf->len = cpu_to_be32(datalen);
804aef9ec39SRoland Dreier 	}
805aef9ec39SRoland Dreier 
806aef9ec39SRoland Dreier 	if (scmnd->sc_data_direction == DMA_TO_DEVICE)
807aef9ec39SRoland Dreier 		cmd->buf_fmt = fmt << 4;
808aef9ec39SRoland Dreier 	else
809aef9ec39SRoland Dreier 		cmd->buf_fmt = fmt;
810aef9ec39SRoland Dreier 
811aef9ec39SRoland Dreier 	return len;
812aef9ec39SRoland Dreier }
813aef9ec39SRoland Dreier 
814aef9ec39SRoland Dreier static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
815aef9ec39SRoland Dreier {
816aef9ec39SRoland Dreier 	struct srp_request *req;
817aef9ec39SRoland Dreier 	struct scsi_cmnd *scmnd;
818aef9ec39SRoland Dreier 	unsigned long flags;
819aef9ec39SRoland Dreier 	s32 delta;
820aef9ec39SRoland Dreier 
821aef9ec39SRoland Dreier 	delta = (s32) be32_to_cpu(rsp->req_lim_delta);
822aef9ec39SRoland Dreier 
823aef9ec39SRoland Dreier 	spin_lock_irqsave(target->scsi_host->host_lock, flags);
824aef9ec39SRoland Dreier 
825aef9ec39SRoland Dreier 	target->req_lim += delta;
826aef9ec39SRoland Dreier 
827aef9ec39SRoland Dreier 	req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
828aef9ec39SRoland Dreier 
829aef9ec39SRoland Dreier 	if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
830aef9ec39SRoland Dreier 		if (be32_to_cpu(rsp->resp_data_len) < 4)
831aef9ec39SRoland Dreier 			req->tsk_status = -1;
832aef9ec39SRoland Dreier 		else
833aef9ec39SRoland Dreier 			req->tsk_status = rsp->data[3];
834aef9ec39SRoland Dreier 		complete(&req->done);
835aef9ec39SRoland Dreier 	} else {
836aef9ec39SRoland Dreier 		scmnd = req->scmnd;
837aef9ec39SRoland Dreier 		if (!scmnd)
8387aa54bd7SDavid Dillow 			shost_printk(KERN_ERR, target->scsi_host,
8397aa54bd7SDavid Dillow 				     "Null scmnd for RSP w/tag %016llx\n",
840aef9ec39SRoland Dreier 				     (unsigned long long) rsp->tag);
841aef9ec39SRoland Dreier 		scmnd->result = rsp->status;
842aef9ec39SRoland Dreier 
843aef9ec39SRoland Dreier 		if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
844aef9ec39SRoland Dreier 			memcpy(scmnd->sense_buffer, rsp->data +
845aef9ec39SRoland Dreier 			       be32_to_cpu(rsp->resp_data_len),
846aef9ec39SRoland Dreier 			       min_t(int, be32_to_cpu(rsp->sense_data_len),
847aef9ec39SRoland Dreier 				     SCSI_SENSE_BUFFERSIZE));
848aef9ec39SRoland Dreier 		}
849aef9ec39SRoland Dreier 
850aef9ec39SRoland Dreier 		if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
851bb350d1dSFUJITA Tomonori 			scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
852aef9ec39SRoland Dreier 		else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
853bb350d1dSFUJITA Tomonori 			scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
854aef9ec39SRoland Dreier 
855aef9ec39SRoland Dreier 		if (!req->tsk_mgmt) {
856aef9ec39SRoland Dreier 			scmnd->host_scribble = (void *) -1L;
857aef9ec39SRoland Dreier 			scmnd->scsi_done(scmnd);
858aef9ec39SRoland Dreier 
859d945e1dfSRoland Dreier 			srp_remove_req(target, req);
860aef9ec39SRoland Dreier 		} else
861aef9ec39SRoland Dreier 			req->cmd_done = 1;
862aef9ec39SRoland Dreier 	}
863aef9ec39SRoland Dreier 
864aef9ec39SRoland Dreier 	spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
865aef9ec39SRoland Dreier }
866aef9ec39SRoland Dreier 
867aef9ec39SRoland Dreier static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
868aef9ec39SRoland Dreier {
86985507bccSRalph Campbell 	struct ib_device *dev;
870aef9ec39SRoland Dreier 	struct srp_iu *iu;
871aef9ec39SRoland Dreier 	u8 opcode;
872aef9ec39SRoland Dreier 
8739c03dc9fSBart Van Assche 	iu = target->rx_ring[wc->wr_id];
874aef9ec39SRoland Dreier 
87505321937SGreg Kroah-Hartman 	dev = target->srp_host->srp_dev->dev;
87685507bccSRalph Campbell 	ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
87785507bccSRalph Campbell 				   DMA_FROM_DEVICE);
878aef9ec39SRoland Dreier 
879aef9ec39SRoland Dreier 	opcode = *(u8 *) iu->buf;
880aef9ec39SRoland Dreier 
881aef9ec39SRoland Dreier 	if (0) {
882aef9ec39SRoland Dreier 		int i;
883aef9ec39SRoland Dreier 
8847aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
8857aa54bd7SDavid Dillow 			     PFX "recv completion, opcode 0x%02x\n", opcode);
886aef9ec39SRoland Dreier 
887aef9ec39SRoland Dreier 		for (i = 0; i < wc->byte_len; ++i) {
888aef9ec39SRoland Dreier 			if (i % 8 == 0)
889aef9ec39SRoland Dreier 				printk(KERN_ERR "  [%02x] ", i);
890aef9ec39SRoland Dreier 			printk(" %02x", ((u8 *) iu->buf)[i]);
891aef9ec39SRoland Dreier 			if ((i + 1) % 8 == 0)
892aef9ec39SRoland Dreier 				printk("\n");
893aef9ec39SRoland Dreier 		}
894aef9ec39SRoland Dreier 
895aef9ec39SRoland Dreier 		if (wc->byte_len % 8)
896aef9ec39SRoland Dreier 			printk("\n");
897aef9ec39SRoland Dreier 	}
898aef9ec39SRoland Dreier 
899aef9ec39SRoland Dreier 	switch (opcode) {
900aef9ec39SRoland Dreier 	case SRP_RSP:
901aef9ec39SRoland Dreier 		srp_process_rsp(target, iu->buf);
902aef9ec39SRoland Dreier 		break;
903aef9ec39SRoland Dreier 
904aef9ec39SRoland Dreier 	case SRP_T_LOGOUT:
905aef9ec39SRoland Dreier 		/* XXX Handle target logout */
9067aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
9077aa54bd7SDavid Dillow 			     PFX "Got target logout request\n");
908aef9ec39SRoland Dreier 		break;
909aef9ec39SRoland Dreier 
910aef9ec39SRoland Dreier 	default:
9117aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
9127aa54bd7SDavid Dillow 			     PFX "Unhandled SRP opcode 0x%02x\n", opcode);
913aef9ec39SRoland Dreier 		break;
914aef9ec39SRoland Dreier 	}
915aef9ec39SRoland Dreier 
91685507bccSRalph Campbell 	ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
91785507bccSRalph Campbell 				      DMA_FROM_DEVICE);
918aef9ec39SRoland Dreier }
919aef9ec39SRoland Dreier 
9209c03dc9fSBart Van Assche static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
921aef9ec39SRoland Dreier {
922aef9ec39SRoland Dreier 	struct srp_target_port *target = target_ptr;
923aef9ec39SRoland Dreier 	struct ib_wc wc;
924aef9ec39SRoland Dreier 
925aef9ec39SRoland Dreier 	ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
926aef9ec39SRoland Dreier 	while (ib_poll_cq(cq, 1, &wc) > 0) {
927aef9ec39SRoland Dreier 		if (wc.status) {
9287aa54bd7SDavid Dillow 			shost_printk(KERN_ERR, target->scsi_host,
9299c03dc9fSBart Van Assche 				     PFX "failed receive status %d\n",
930aef9ec39SRoland Dreier 				     wc.status);
9311033ff67SIshai Rabinovitz 			target->qp_in_error = 1;
932aef9ec39SRoland Dreier 			break;
933aef9ec39SRoland Dreier 		}
934aef9ec39SRoland Dreier 
935aef9ec39SRoland Dreier 		srp_handle_recv(target, &wc);
9369c03dc9fSBart Van Assche 	}
9379c03dc9fSBart Van Assche }
9389c03dc9fSBart Van Assche 
9399c03dc9fSBart Van Assche static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
9409c03dc9fSBart Van Assche {
9419c03dc9fSBart Van Assche 	struct srp_target_port *target = target_ptr;
9429c03dc9fSBart Van Assche 	struct ib_wc wc;
9439c03dc9fSBart Van Assche 
9449c03dc9fSBart Van Assche 	while (ib_poll_cq(cq, 1, &wc) > 0) {
9459c03dc9fSBart Van Assche 		if (wc.status) {
9469c03dc9fSBart Van Assche 			shost_printk(KERN_ERR, target->scsi_host,
9479c03dc9fSBart Van Assche 				     PFX "failed send status %d\n",
9489c03dc9fSBart Van Assche 				     wc.status);
9499c03dc9fSBart Van Assche 			target->qp_in_error = 1;
9509c03dc9fSBart Van Assche 			break;
9519c03dc9fSBart Van Assche 		}
9529c03dc9fSBart Van Assche 
953aef9ec39SRoland Dreier 		++target->tx_tail;
954aef9ec39SRoland Dreier 	}
955aef9ec39SRoland Dreier }
956aef9ec39SRoland Dreier 
957aef9ec39SRoland Dreier static int __srp_post_recv(struct srp_target_port *target)
958aef9ec39SRoland Dreier {
959aef9ec39SRoland Dreier 	struct srp_iu *iu;
960aef9ec39SRoland Dreier 	struct ib_sge list;
961aef9ec39SRoland Dreier 	struct ib_recv_wr wr, *bad_wr;
962aef9ec39SRoland Dreier 	unsigned int next;
963aef9ec39SRoland Dreier 	int ret;
964aef9ec39SRoland Dreier 
965aef9ec39SRoland Dreier 	next 	 = target->rx_head & (SRP_RQ_SIZE - 1);
9669c03dc9fSBart Van Assche 	wr.wr_id = next;
967aef9ec39SRoland Dreier 	iu 	 = target->rx_ring[next];
968aef9ec39SRoland Dreier 
969aef9ec39SRoland Dreier 	list.addr   = iu->dma;
970aef9ec39SRoland Dreier 	list.length = iu->size;
97105321937SGreg Kroah-Hartman 	list.lkey   = target->srp_host->srp_dev->mr->lkey;
972aef9ec39SRoland Dreier 
973aef9ec39SRoland Dreier 	wr.next     = NULL;
974aef9ec39SRoland Dreier 	wr.sg_list  = &list;
975aef9ec39SRoland Dreier 	wr.num_sge  = 1;
976aef9ec39SRoland Dreier 
977aef9ec39SRoland Dreier 	ret = ib_post_recv(target->qp, &wr, &bad_wr);
978aef9ec39SRoland Dreier 	if (!ret)
979aef9ec39SRoland Dreier 		++target->rx_head;
980aef9ec39SRoland Dreier 
981aef9ec39SRoland Dreier 	return ret;
982aef9ec39SRoland Dreier }
983aef9ec39SRoland Dreier 
984aef9ec39SRoland Dreier static int srp_post_recv(struct srp_target_port *target)
985aef9ec39SRoland Dreier {
986aef9ec39SRoland Dreier 	unsigned long flags;
987aef9ec39SRoland Dreier 	int ret;
988aef9ec39SRoland Dreier 
989aef9ec39SRoland Dreier 	spin_lock_irqsave(target->scsi_host->host_lock, flags);
990aef9ec39SRoland Dreier 	ret = __srp_post_recv(target);
991aef9ec39SRoland Dreier 	spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
992aef9ec39SRoland Dreier 
993aef9ec39SRoland Dreier 	return ret;
994aef9ec39SRoland Dreier }
995aef9ec39SRoland Dreier 
996aef9ec39SRoland Dreier /*
997aef9ec39SRoland Dreier  * Must be called with target->scsi_host->host_lock held to protect
99847f2bce9SRoland Dreier  * req_lim and tx_head.  Lock cannot be dropped between call here and
99947f2bce9SRoland Dreier  * call to __srp_post_send().
1000aef9ec39SRoland Dreier  */
10018cba2077SDavid Dillow static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
10028cba2077SDavid Dillow 					enum srp_request_type req_type)
1003aef9ec39SRoland Dreier {
10048cba2077SDavid Dillow 	s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
10058cba2077SDavid Dillow 
10069c03dc9fSBart Van Assche 	srp_send_completion(target->send_cq, target);
10079c03dc9fSBart Van Assche 
1008aef9ec39SRoland Dreier 	if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
1009aef9ec39SRoland Dreier 		return NULL;
1010aef9ec39SRoland Dreier 
10118cba2077SDavid Dillow 	if (target->req_lim < min) {
10126bfa24faSRoland Dreier 		++target->zero_req_lim;
10138cba2077SDavid Dillow 		return NULL;
10148cba2077SDavid Dillow 	}
101547f2bce9SRoland Dreier 
1016aef9ec39SRoland Dreier 	return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
1017aef9ec39SRoland Dreier }
1018aef9ec39SRoland Dreier 
1019aef9ec39SRoland Dreier /*
1020aef9ec39SRoland Dreier  * Must be called with target->scsi_host->host_lock held to protect
1021aef9ec39SRoland Dreier  * req_lim and tx_head.
1022aef9ec39SRoland Dreier  */
1023aef9ec39SRoland Dreier static int __srp_post_send(struct srp_target_port *target,
1024aef9ec39SRoland Dreier 			   struct srp_iu *iu, int len)
1025aef9ec39SRoland Dreier {
1026aef9ec39SRoland Dreier 	struct ib_sge list;
1027aef9ec39SRoland Dreier 	struct ib_send_wr wr, *bad_wr;
1028aef9ec39SRoland Dreier 	int ret = 0;
1029aef9ec39SRoland Dreier 
1030aef9ec39SRoland Dreier 	list.addr   = iu->dma;
1031aef9ec39SRoland Dreier 	list.length = len;
103205321937SGreg Kroah-Hartman 	list.lkey   = target->srp_host->srp_dev->mr->lkey;
1033aef9ec39SRoland Dreier 
1034aef9ec39SRoland Dreier 	wr.next       = NULL;
1035aef9ec39SRoland Dreier 	wr.wr_id      = target->tx_head & SRP_SQ_SIZE;
1036aef9ec39SRoland Dreier 	wr.sg_list    = &list;
1037aef9ec39SRoland Dreier 	wr.num_sge    = 1;
1038aef9ec39SRoland Dreier 	wr.opcode     = IB_WR_SEND;
1039aef9ec39SRoland Dreier 	wr.send_flags = IB_SEND_SIGNALED;
1040aef9ec39SRoland Dreier 
1041aef9ec39SRoland Dreier 	ret = ib_post_send(target->qp, &wr, &bad_wr);
1042aef9ec39SRoland Dreier 
1043aef9ec39SRoland Dreier 	if (!ret) {
1044aef9ec39SRoland Dreier 		++target->tx_head;
1045aef9ec39SRoland Dreier 		--target->req_lim;
1046aef9ec39SRoland Dreier 	}
1047aef9ec39SRoland Dreier 
1048aef9ec39SRoland Dreier 	return ret;
1049aef9ec39SRoland Dreier }
1050aef9ec39SRoland Dreier 
1051aef9ec39SRoland Dreier static int srp_queuecommand(struct scsi_cmnd *scmnd,
1052aef9ec39SRoland Dreier 			    void (*done)(struct scsi_cmnd *))
1053aef9ec39SRoland Dreier {
1054aef9ec39SRoland Dreier 	struct srp_target_port *target = host_to_target(scmnd->device->host);
1055aef9ec39SRoland Dreier 	struct srp_request *req;
1056aef9ec39SRoland Dreier 	struct srp_iu *iu;
1057aef9ec39SRoland Dreier 	struct srp_cmd *cmd;
105885507bccSRalph Campbell 	struct ib_device *dev;
1059aef9ec39SRoland Dreier 	int len;
1060aef9ec39SRoland Dreier 
1061aef9ec39SRoland Dreier 	if (target->state == SRP_TARGET_CONNECTING)
1062aef9ec39SRoland Dreier 		goto err;
1063aef9ec39SRoland Dreier 
1064aef9ec39SRoland Dreier 	if (target->state == SRP_TARGET_DEAD ||
1065aef9ec39SRoland Dreier 	    target->state == SRP_TARGET_REMOVED) {
1066aef9ec39SRoland Dreier 		scmnd->result = DID_BAD_TARGET << 16;
1067aef9ec39SRoland Dreier 		done(scmnd);
1068aef9ec39SRoland Dreier 		return 0;
1069aef9ec39SRoland Dreier 	}
1070aef9ec39SRoland Dreier 
10718cba2077SDavid Dillow 	iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
1072aef9ec39SRoland Dreier 	if (!iu)
1073aef9ec39SRoland Dreier 		goto err;
1074aef9ec39SRoland Dreier 
107505321937SGreg Kroah-Hartman 	dev = target->srp_host->srp_dev->dev;
107685507bccSRalph Campbell 	ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
107785507bccSRalph Campbell 				   DMA_TO_DEVICE);
1078aef9ec39SRoland Dreier 
1079d945e1dfSRoland Dreier 	req = list_entry(target->free_reqs.next, struct srp_request, list);
1080aef9ec39SRoland Dreier 
1081aef9ec39SRoland Dreier 	scmnd->scsi_done     = done;
1082aef9ec39SRoland Dreier 	scmnd->result        = 0;
1083d945e1dfSRoland Dreier 	scmnd->host_scribble = (void *) (long) req->index;
1084aef9ec39SRoland Dreier 
1085aef9ec39SRoland Dreier 	cmd = iu->buf;
1086aef9ec39SRoland Dreier 	memset(cmd, 0, sizeof *cmd);
1087aef9ec39SRoland Dreier 
1088aef9ec39SRoland Dreier 	cmd->opcode = SRP_CMD;
1089aef9ec39SRoland Dreier 	cmd->lun    = cpu_to_be64((u64) scmnd->device->lun << 48);
1090d945e1dfSRoland Dreier 	cmd->tag    = req->index;
1091aef9ec39SRoland Dreier 	memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1092aef9ec39SRoland Dreier 
1093aef9ec39SRoland Dreier 	req->scmnd    = scmnd;
1094aef9ec39SRoland Dreier 	req->cmd      = iu;
1095aef9ec39SRoland Dreier 	req->cmd_done = 0;
1096aef9ec39SRoland Dreier 	req->tsk_mgmt = NULL;
1097aef9ec39SRoland Dreier 
1098aef9ec39SRoland Dreier 	len = srp_map_data(scmnd, target, req);
1099aef9ec39SRoland Dreier 	if (len < 0) {
11007aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
11017aa54bd7SDavid Dillow 			     PFX "Failed to map data\n");
1102aef9ec39SRoland Dreier 		goto err;
1103aef9ec39SRoland Dreier 	}
1104aef9ec39SRoland Dreier 
1105aef9ec39SRoland Dreier 	if (__srp_post_recv(target)) {
11067aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
1107aef9ec39SRoland Dreier 		goto err_unmap;
1108aef9ec39SRoland Dreier 	}
1109aef9ec39SRoland Dreier 
111085507bccSRalph Campbell 	ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
111185507bccSRalph Campbell 				      DMA_TO_DEVICE);
1112aef9ec39SRoland Dreier 
1113aef9ec39SRoland Dreier 	if (__srp_post_send(target, iu, len)) {
11147aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
1115aef9ec39SRoland Dreier 		goto err_unmap;
1116aef9ec39SRoland Dreier 	}
1117aef9ec39SRoland Dreier 
1118d945e1dfSRoland Dreier 	list_move_tail(&req->list, &target->req_queue);
1119aef9ec39SRoland Dreier 
1120aef9ec39SRoland Dreier 	return 0;
1121aef9ec39SRoland Dreier 
1122aef9ec39SRoland Dreier err_unmap:
1123aef9ec39SRoland Dreier 	srp_unmap_data(scmnd, target, req);
1124aef9ec39SRoland Dreier 
1125aef9ec39SRoland Dreier err:
1126aef9ec39SRoland Dreier 	return SCSI_MLQUEUE_HOST_BUSY;
1127aef9ec39SRoland Dreier }
1128aef9ec39SRoland Dreier 
1129aef9ec39SRoland Dreier static int srp_alloc_iu_bufs(struct srp_target_port *target)
1130aef9ec39SRoland Dreier {
1131aef9ec39SRoland Dreier 	int i;
1132aef9ec39SRoland Dreier 
1133aef9ec39SRoland Dreier 	for (i = 0; i < SRP_RQ_SIZE; ++i) {
1134aef9ec39SRoland Dreier 		target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1135aef9ec39SRoland Dreier 						  target->max_ti_iu_len,
1136aef9ec39SRoland Dreier 						  GFP_KERNEL, DMA_FROM_DEVICE);
1137aef9ec39SRoland Dreier 		if (!target->rx_ring[i])
1138aef9ec39SRoland Dreier 			goto err;
1139aef9ec39SRoland Dreier 	}
1140aef9ec39SRoland Dreier 
1141aef9ec39SRoland Dreier 	for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1142aef9ec39SRoland Dreier 		target->tx_ring[i] = srp_alloc_iu(target->srp_host,
114374b0a15bSVu Pham 						  srp_max_iu_len,
1144aef9ec39SRoland Dreier 						  GFP_KERNEL, DMA_TO_DEVICE);
1145aef9ec39SRoland Dreier 		if (!target->tx_ring[i])
1146aef9ec39SRoland Dreier 			goto err;
1147aef9ec39SRoland Dreier 	}
1148aef9ec39SRoland Dreier 
1149aef9ec39SRoland Dreier 	return 0;
1150aef9ec39SRoland Dreier 
1151aef9ec39SRoland Dreier err:
1152aef9ec39SRoland Dreier 	for (i = 0; i < SRP_RQ_SIZE; ++i) {
1153aef9ec39SRoland Dreier 		srp_free_iu(target->srp_host, target->rx_ring[i]);
1154aef9ec39SRoland Dreier 		target->rx_ring[i] = NULL;
1155aef9ec39SRoland Dreier 	}
1156aef9ec39SRoland Dreier 
1157aef9ec39SRoland Dreier 	for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1158aef9ec39SRoland Dreier 		srp_free_iu(target->srp_host, target->tx_ring[i]);
1159aef9ec39SRoland Dreier 		target->tx_ring[i] = NULL;
1160aef9ec39SRoland Dreier 	}
1161aef9ec39SRoland Dreier 
1162aef9ec39SRoland Dreier 	return -ENOMEM;
1163aef9ec39SRoland Dreier }
1164aef9ec39SRoland Dreier 
1165aef9ec39SRoland Dreier static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1166aef9ec39SRoland Dreier 			       struct ib_cm_event *event,
1167aef9ec39SRoland Dreier 			       struct srp_target_port *target)
1168aef9ec39SRoland Dreier {
11697aa54bd7SDavid Dillow 	struct Scsi_Host *shost = target->scsi_host;
1170aef9ec39SRoland Dreier 	struct ib_class_port_info *cpi;
1171aef9ec39SRoland Dreier 	int opcode;
1172aef9ec39SRoland Dreier 
1173aef9ec39SRoland Dreier 	switch (event->param.rej_rcvd.reason) {
1174aef9ec39SRoland Dreier 	case IB_CM_REJ_PORT_CM_REDIRECT:
1175aef9ec39SRoland Dreier 		cpi = event->param.rej_rcvd.ari;
1176aef9ec39SRoland Dreier 		target->path.dlid = cpi->redirect_lid;
1177aef9ec39SRoland Dreier 		target->path.pkey = cpi->redirect_pkey;
1178aef9ec39SRoland Dreier 		cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1179aef9ec39SRoland Dreier 		memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1180aef9ec39SRoland Dreier 
1181aef9ec39SRoland Dreier 		target->status = target->path.dlid ?
1182aef9ec39SRoland Dreier 			SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1183aef9ec39SRoland Dreier 		break;
1184aef9ec39SRoland Dreier 
1185aef9ec39SRoland Dreier 	case IB_CM_REJ_PORT_REDIRECT:
11865d7cbfd6SRoland Dreier 		if (srp_target_is_topspin(target)) {
1187aef9ec39SRoland Dreier 			/*
1188aef9ec39SRoland Dreier 			 * Topspin/Cisco SRP gateways incorrectly send
1189aef9ec39SRoland Dreier 			 * reject reason code 25 when they mean 24
1190aef9ec39SRoland Dreier 			 * (port redirect).
1191aef9ec39SRoland Dreier 			 */
1192aef9ec39SRoland Dreier 			memcpy(target->path.dgid.raw,
1193aef9ec39SRoland Dreier 			       event->param.rej_rcvd.ari, 16);
1194aef9ec39SRoland Dreier 
11957aa54bd7SDavid Dillow 			shost_printk(KERN_DEBUG, shost,
11967aa54bd7SDavid Dillow 				     PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1197aef9ec39SRoland Dreier 				     (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1198aef9ec39SRoland Dreier 				     (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1199aef9ec39SRoland Dreier 
1200aef9ec39SRoland Dreier 			target->status = SRP_PORT_REDIRECT;
1201aef9ec39SRoland Dreier 		} else {
12027aa54bd7SDavid Dillow 			shost_printk(KERN_WARNING, shost,
12037aa54bd7SDavid Dillow 				     "  REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1204aef9ec39SRoland Dreier 			target->status = -ECONNRESET;
1205aef9ec39SRoland Dreier 		}
1206aef9ec39SRoland Dreier 		break;
1207aef9ec39SRoland Dreier 
1208aef9ec39SRoland Dreier 	case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
12097aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, shost,
12107aa54bd7SDavid Dillow 			    "  REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1211aef9ec39SRoland Dreier 		target->status = -ECONNRESET;
1212aef9ec39SRoland Dreier 		break;
1213aef9ec39SRoland Dreier 
1214aef9ec39SRoland Dreier 	case IB_CM_REJ_CONSUMER_DEFINED:
1215aef9ec39SRoland Dreier 		opcode = *(u8 *) event->private_data;
1216aef9ec39SRoland Dreier 		if (opcode == SRP_LOGIN_REJ) {
1217aef9ec39SRoland Dreier 			struct srp_login_rej *rej = event->private_data;
1218aef9ec39SRoland Dreier 			u32 reason = be32_to_cpu(rej->reason);
1219aef9ec39SRoland Dreier 
1220aef9ec39SRoland Dreier 			if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
12217aa54bd7SDavid Dillow 				shost_printk(KERN_WARNING, shost,
12227aa54bd7SDavid Dillow 					     PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1223aef9ec39SRoland Dreier 			else
12247aa54bd7SDavid Dillow 				shost_printk(KERN_WARNING, shost,
12257aa54bd7SDavid Dillow 					    PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1226aef9ec39SRoland Dreier 		} else
12277aa54bd7SDavid Dillow 			shost_printk(KERN_WARNING, shost,
12287aa54bd7SDavid Dillow 				     "  REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1229aef9ec39SRoland Dreier 				     " opcode 0x%02x\n", opcode);
1230aef9ec39SRoland Dreier 		target->status = -ECONNRESET;
1231aef9ec39SRoland Dreier 		break;
1232aef9ec39SRoland Dreier 
12339fe4bcf4SDavid Dillow 	case IB_CM_REJ_STALE_CONN:
12349fe4bcf4SDavid Dillow 		shost_printk(KERN_WARNING, shost, "  REJ reason: stale connection\n");
12359fe4bcf4SDavid Dillow 		target->status = SRP_STALE_CONN;
12369fe4bcf4SDavid Dillow 		break;
12379fe4bcf4SDavid Dillow 
1238aef9ec39SRoland Dreier 	default:
12397aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, shost, "  REJ reason 0x%x\n",
1240aef9ec39SRoland Dreier 			     event->param.rej_rcvd.reason);
1241aef9ec39SRoland Dreier 		target->status = -ECONNRESET;
1242aef9ec39SRoland Dreier 	}
1243aef9ec39SRoland Dreier }
1244aef9ec39SRoland Dreier 
1245aef9ec39SRoland Dreier static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1246aef9ec39SRoland Dreier {
1247aef9ec39SRoland Dreier 	struct srp_target_port *target = cm_id->context;
1248aef9ec39SRoland Dreier 	struct ib_qp_attr *qp_attr = NULL;
1249aef9ec39SRoland Dreier 	int attr_mask = 0;
1250aef9ec39SRoland Dreier 	int comp = 0;
1251aef9ec39SRoland Dreier 	int opcode = 0;
1252aef9ec39SRoland Dreier 
1253aef9ec39SRoland Dreier 	switch (event->event) {
1254aef9ec39SRoland Dreier 	case IB_CM_REQ_ERROR:
12557aa54bd7SDavid Dillow 		shost_printk(KERN_DEBUG, target->scsi_host,
12567aa54bd7SDavid Dillow 			     PFX "Sending CM REQ failed\n");
1257aef9ec39SRoland Dreier 		comp = 1;
1258aef9ec39SRoland Dreier 		target->status = -ECONNRESET;
1259aef9ec39SRoland Dreier 		break;
1260aef9ec39SRoland Dreier 
1261aef9ec39SRoland Dreier 	case IB_CM_REP_RECEIVED:
1262aef9ec39SRoland Dreier 		comp = 1;
1263aef9ec39SRoland Dreier 		opcode = *(u8 *) event->private_data;
1264aef9ec39SRoland Dreier 
1265aef9ec39SRoland Dreier 		if (opcode == SRP_LOGIN_RSP) {
1266aef9ec39SRoland Dreier 			struct srp_login_rsp *rsp = event->private_data;
1267aef9ec39SRoland Dreier 
1268aef9ec39SRoland Dreier 			target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1269aef9ec39SRoland Dreier 			target->req_lim       = be32_to_cpu(rsp->req_lim_delta);
1270aef9ec39SRoland Dreier 
1271aef9ec39SRoland Dreier 			target->scsi_host->can_queue = min(target->req_lim,
1272aef9ec39SRoland Dreier 							   target->scsi_host->can_queue);
1273aef9ec39SRoland Dreier 		} else {
12747aa54bd7SDavid Dillow 			shost_printk(KERN_WARNING, target->scsi_host,
12757aa54bd7SDavid Dillow 				    PFX "Unhandled RSP opcode %#x\n", opcode);
1276aef9ec39SRoland Dreier 			target->status = -ECONNRESET;
1277aef9ec39SRoland Dreier 			break;
1278aef9ec39SRoland Dreier 		}
1279aef9ec39SRoland Dreier 
1280d2fcea7dSVu Pham 		if (!target->rx_ring[0]) {
1281aef9ec39SRoland Dreier 			target->status = srp_alloc_iu_bufs(target);
1282aef9ec39SRoland Dreier 			if (target->status)
1283aef9ec39SRoland Dreier 				break;
1284d2fcea7dSVu Pham 		}
1285aef9ec39SRoland Dreier 
1286aef9ec39SRoland Dreier 		qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1287aef9ec39SRoland Dreier 		if (!qp_attr) {
1288aef9ec39SRoland Dreier 			target->status = -ENOMEM;
1289aef9ec39SRoland Dreier 			break;
1290aef9ec39SRoland Dreier 		}
1291aef9ec39SRoland Dreier 
1292aef9ec39SRoland Dreier 		qp_attr->qp_state = IB_QPS_RTR;
1293aef9ec39SRoland Dreier 		target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1294aef9ec39SRoland Dreier 		if (target->status)
1295aef9ec39SRoland Dreier 			break;
1296aef9ec39SRoland Dreier 
1297aef9ec39SRoland Dreier 		target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1298aef9ec39SRoland Dreier 		if (target->status)
1299aef9ec39SRoland Dreier 			break;
1300aef9ec39SRoland Dreier 
1301aef9ec39SRoland Dreier 		target->status = srp_post_recv(target);
1302aef9ec39SRoland Dreier 		if (target->status)
1303aef9ec39SRoland Dreier 			break;
1304aef9ec39SRoland Dreier 
1305aef9ec39SRoland Dreier 		qp_attr->qp_state = IB_QPS_RTS;
1306aef9ec39SRoland Dreier 		target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1307aef9ec39SRoland Dreier 		if (target->status)
1308aef9ec39SRoland Dreier 			break;
1309aef9ec39SRoland Dreier 
1310aef9ec39SRoland Dreier 		target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1311aef9ec39SRoland Dreier 		if (target->status)
1312aef9ec39SRoland Dreier 			break;
1313aef9ec39SRoland Dreier 
1314aef9ec39SRoland Dreier 		target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1315aef9ec39SRoland Dreier 		if (target->status)
1316aef9ec39SRoland Dreier 			break;
1317aef9ec39SRoland Dreier 
1318aef9ec39SRoland Dreier 		break;
1319aef9ec39SRoland Dreier 
1320aef9ec39SRoland Dreier 	case IB_CM_REJ_RECEIVED:
13217aa54bd7SDavid Dillow 		shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
1322aef9ec39SRoland Dreier 		comp = 1;
1323aef9ec39SRoland Dreier 
1324aef9ec39SRoland Dreier 		srp_cm_rej_handler(cm_id, event, target);
1325aef9ec39SRoland Dreier 		break;
1326aef9ec39SRoland Dreier 
1327b7ac4ab4SIshai Rabinovitz 	case IB_CM_DREQ_RECEIVED:
13287aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
13297aa54bd7SDavid Dillow 			     PFX "DREQ received - connection closed\n");
1330b7ac4ab4SIshai Rabinovitz 		if (ib_send_cm_drep(cm_id, NULL, 0))
13317aa54bd7SDavid Dillow 			shost_printk(KERN_ERR, target->scsi_host,
13327aa54bd7SDavid Dillow 				     PFX "Sending CM DREP failed\n");
1333aef9ec39SRoland Dreier 		break;
1334aef9ec39SRoland Dreier 
1335aef9ec39SRoland Dreier 	case IB_CM_TIMEWAIT_EXIT:
13367aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
13377aa54bd7SDavid Dillow 			     PFX "connection closed\n");
1338aef9ec39SRoland Dreier 
1339aef9ec39SRoland Dreier 		comp = 1;
1340aef9ec39SRoland Dreier 		target->status = 0;
1341aef9ec39SRoland Dreier 		break;
1342aef9ec39SRoland Dreier 
1343b7ac4ab4SIshai Rabinovitz 	case IB_CM_MRA_RECEIVED:
1344b7ac4ab4SIshai Rabinovitz 	case IB_CM_DREQ_ERROR:
1345b7ac4ab4SIshai Rabinovitz 	case IB_CM_DREP_RECEIVED:
1346b7ac4ab4SIshai Rabinovitz 		break;
1347b7ac4ab4SIshai Rabinovitz 
1348aef9ec39SRoland Dreier 	default:
13497aa54bd7SDavid Dillow 		shost_printk(KERN_WARNING, target->scsi_host,
13507aa54bd7SDavid Dillow 			     PFX "Unhandled CM event %d\n", event->event);
1351aef9ec39SRoland Dreier 		break;
1352aef9ec39SRoland Dreier 	}
1353aef9ec39SRoland Dreier 
1354aef9ec39SRoland Dreier 	if (comp)
1355aef9ec39SRoland Dreier 		complete(&target->done);
1356aef9ec39SRoland Dreier 
1357aef9ec39SRoland Dreier 	kfree(qp_attr);
1358aef9ec39SRoland Dreier 
1359aef9ec39SRoland Dreier 	return 0;
1360aef9ec39SRoland Dreier }
1361aef9ec39SRoland Dreier 
1362d945e1dfSRoland Dreier static int srp_send_tsk_mgmt(struct srp_target_port *target,
1363d945e1dfSRoland Dreier 			     struct srp_request *req, u8 func)
1364aef9ec39SRoland Dreier {
1365aef9ec39SRoland Dreier 	struct srp_iu *iu;
1366aef9ec39SRoland Dreier 	struct srp_tsk_mgmt *tsk_mgmt;
1367aef9ec39SRoland Dreier 
1368aef9ec39SRoland Dreier 	spin_lock_irq(target->scsi_host->host_lock);
1369aef9ec39SRoland Dreier 
13701285b3a0SRoland Dreier 	if (target->state == SRP_TARGET_DEAD ||
13711285b3a0SRoland Dreier 	    target->state == SRP_TARGET_REMOVED) {
1372d945e1dfSRoland Dreier 		req->scmnd->result = DID_BAD_TARGET << 16;
13731285b3a0SRoland Dreier 		goto out;
13741285b3a0SRoland Dreier 	}
13751285b3a0SRoland Dreier 
1376aef9ec39SRoland Dreier 	init_completion(&req->done);
1377aef9ec39SRoland Dreier 
13788cba2077SDavid Dillow 	iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
1379aef9ec39SRoland Dreier 	if (!iu)
1380aef9ec39SRoland Dreier 		goto out;
1381aef9ec39SRoland Dreier 
1382aef9ec39SRoland Dreier 	tsk_mgmt = iu->buf;
1383aef9ec39SRoland Dreier 	memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1384aef9ec39SRoland Dreier 
1385aef9ec39SRoland Dreier 	tsk_mgmt->opcode 	= SRP_TSK_MGMT;
1386d945e1dfSRoland Dreier 	tsk_mgmt->lun 		= cpu_to_be64((u64) req->scmnd->device->lun << 48);
1387d945e1dfSRoland Dreier 	tsk_mgmt->tag 		= req->index | SRP_TAG_TSK_MGMT;
1388aef9ec39SRoland Dreier 	tsk_mgmt->tsk_mgmt_func = func;
1389d945e1dfSRoland Dreier 	tsk_mgmt->task_tag 	= req->index;
1390aef9ec39SRoland Dreier 
1391aef9ec39SRoland Dreier 	if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1392aef9ec39SRoland Dreier 		goto out;
1393aef9ec39SRoland Dreier 
1394aef9ec39SRoland Dreier 	req->tsk_mgmt = iu;
1395aef9ec39SRoland Dreier 
1396aef9ec39SRoland Dreier 	spin_unlock_irq(target->scsi_host->host_lock);
1397d945e1dfSRoland Dreier 
1398aef9ec39SRoland Dreier 	if (!wait_for_completion_timeout(&req->done,
1399aef9ec39SRoland Dreier 					 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1400d945e1dfSRoland Dreier 		return -1;
1401aef9ec39SRoland Dreier 
1402d945e1dfSRoland Dreier 	return 0;
1403aef9ec39SRoland Dreier 
1404aef9ec39SRoland Dreier out:
1405aef9ec39SRoland Dreier 	spin_unlock_irq(target->scsi_host->host_lock);
1406d945e1dfSRoland Dreier 	return -1;
1407d945e1dfSRoland Dreier }
1408d945e1dfSRoland Dreier 
1409d945e1dfSRoland Dreier static int srp_find_req(struct srp_target_port *target,
1410d945e1dfSRoland Dreier 			struct scsi_cmnd *scmnd,
1411d945e1dfSRoland Dreier 			struct srp_request **req)
1412d945e1dfSRoland Dreier {
1413d945e1dfSRoland Dreier 	if (scmnd->host_scribble == (void *) -1L)
1414d945e1dfSRoland Dreier 		return -1;
1415d945e1dfSRoland Dreier 
1416d945e1dfSRoland Dreier 	*req = &target->req_ring[(long) scmnd->host_scribble];
1417d945e1dfSRoland Dreier 
1418d945e1dfSRoland Dreier 	return 0;
1419aef9ec39SRoland Dreier }
1420aef9ec39SRoland Dreier 
1421aef9ec39SRoland Dreier static int srp_abort(struct scsi_cmnd *scmnd)
1422aef9ec39SRoland Dreier {
1423d945e1dfSRoland Dreier 	struct srp_target_port *target = host_to_target(scmnd->device->host);
1424d945e1dfSRoland Dreier 	struct srp_request *req;
1425d945e1dfSRoland Dreier 	int ret = SUCCESS;
1426d945e1dfSRoland Dreier 
14277aa54bd7SDavid Dillow 	shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
1428aef9ec39SRoland Dreier 
14291033ff67SIshai Rabinovitz 	if (target->qp_in_error)
14301033ff67SIshai Rabinovitz 		return FAILED;
1431d945e1dfSRoland Dreier 	if (srp_find_req(target, scmnd, &req))
1432d945e1dfSRoland Dreier 		return FAILED;
1433d945e1dfSRoland Dreier 	if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1434d945e1dfSRoland Dreier 		return FAILED;
1435d945e1dfSRoland Dreier 
1436d945e1dfSRoland Dreier 	spin_lock_irq(target->scsi_host->host_lock);
1437d945e1dfSRoland Dreier 
1438d945e1dfSRoland Dreier 	if (req->cmd_done) {
1439d945e1dfSRoland Dreier 		srp_remove_req(target, req);
1440d945e1dfSRoland Dreier 		scmnd->scsi_done(scmnd);
1441d945e1dfSRoland Dreier 	} else if (!req->tsk_status) {
1442d945e1dfSRoland Dreier 		srp_remove_req(target, req);
1443d945e1dfSRoland Dreier 		scmnd->result = DID_ABORT << 16;
1444d945e1dfSRoland Dreier 	} else
1445d945e1dfSRoland Dreier 		ret = FAILED;
1446d945e1dfSRoland Dreier 
1447d945e1dfSRoland Dreier 	spin_unlock_irq(target->scsi_host->host_lock);
1448d945e1dfSRoland Dreier 
1449d945e1dfSRoland Dreier 	return ret;
1450aef9ec39SRoland Dreier }
1451aef9ec39SRoland Dreier 
1452aef9ec39SRoland Dreier static int srp_reset_device(struct scsi_cmnd *scmnd)
1453aef9ec39SRoland Dreier {
1454d945e1dfSRoland Dreier 	struct srp_target_port *target = host_to_target(scmnd->device->host);
1455d945e1dfSRoland Dreier 	struct srp_request *req, *tmp;
1456d945e1dfSRoland Dreier 
14577aa54bd7SDavid Dillow 	shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
1458aef9ec39SRoland Dreier 
14591033ff67SIshai Rabinovitz 	if (target->qp_in_error)
14601033ff67SIshai Rabinovitz 		return FAILED;
1461d945e1dfSRoland Dreier 	if (srp_find_req(target, scmnd, &req))
1462d945e1dfSRoland Dreier 		return FAILED;
1463d945e1dfSRoland Dreier 	if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1464d945e1dfSRoland Dreier 		return FAILED;
1465d945e1dfSRoland Dreier 	if (req->tsk_status)
1466d945e1dfSRoland Dreier 		return FAILED;
1467d945e1dfSRoland Dreier 
1468d945e1dfSRoland Dreier 	spin_lock_irq(target->scsi_host->host_lock);
1469d945e1dfSRoland Dreier 
1470d945e1dfSRoland Dreier 	list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1471526b4caaSIshai Rabinovitz 		if (req->scmnd->device == scmnd->device)
1472526b4caaSIshai Rabinovitz 			srp_reset_req(target, req);
1473d945e1dfSRoland Dreier 
1474d945e1dfSRoland Dreier 	spin_unlock_irq(target->scsi_host->host_lock);
1475d945e1dfSRoland Dreier 
1476d945e1dfSRoland Dreier 	return SUCCESS;
1477aef9ec39SRoland Dreier }
1478aef9ec39SRoland Dreier 
1479aef9ec39SRoland Dreier static int srp_reset_host(struct scsi_cmnd *scmnd)
1480aef9ec39SRoland Dreier {
1481aef9ec39SRoland Dreier 	struct srp_target_port *target = host_to_target(scmnd->device->host);
1482aef9ec39SRoland Dreier 	int ret = FAILED;
1483aef9ec39SRoland Dreier 
14847aa54bd7SDavid Dillow 	shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
1485aef9ec39SRoland Dreier 
1486aef9ec39SRoland Dreier 	if (!srp_reconnect_target(target))
1487aef9ec39SRoland Dreier 		ret = SUCCESS;
1488aef9ec39SRoland Dreier 
1489aef9ec39SRoland Dreier 	return ret;
1490aef9ec39SRoland Dreier }
1491aef9ec39SRoland Dreier 
1492ee959b00STony Jones static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1493ee959b00STony Jones 			   char *buf)
14946ecb0c84SRoland Dreier {
1495ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
14966ecb0c84SRoland Dreier 
14976ecb0c84SRoland Dreier 	if (target->state == SRP_TARGET_DEAD ||
14986ecb0c84SRoland Dreier 	    target->state == SRP_TARGET_REMOVED)
14996ecb0c84SRoland Dreier 		return -ENODEV;
15006ecb0c84SRoland Dreier 
15016ecb0c84SRoland Dreier 	return sprintf(buf, "0x%016llx\n",
15026ecb0c84SRoland Dreier 		       (unsigned long long) be64_to_cpu(target->id_ext));
15036ecb0c84SRoland Dreier }
15046ecb0c84SRoland Dreier 
1505ee959b00STony Jones static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1506ee959b00STony Jones 			     char *buf)
15076ecb0c84SRoland Dreier {
1508ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
15096ecb0c84SRoland Dreier 
15106ecb0c84SRoland Dreier 	if (target->state == SRP_TARGET_DEAD ||
15116ecb0c84SRoland Dreier 	    target->state == SRP_TARGET_REMOVED)
15126ecb0c84SRoland Dreier 		return -ENODEV;
15136ecb0c84SRoland Dreier 
15146ecb0c84SRoland Dreier 	return sprintf(buf, "0x%016llx\n",
15156ecb0c84SRoland Dreier 		       (unsigned long long) be64_to_cpu(target->ioc_guid));
15166ecb0c84SRoland Dreier }
15176ecb0c84SRoland Dreier 
1518ee959b00STony Jones static ssize_t show_service_id(struct device *dev,
1519ee959b00STony Jones 			       struct device_attribute *attr, char *buf)
15206ecb0c84SRoland Dreier {
1521ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
15226ecb0c84SRoland Dreier 
15236ecb0c84SRoland Dreier 	if (target->state == SRP_TARGET_DEAD ||
15246ecb0c84SRoland Dreier 	    target->state == SRP_TARGET_REMOVED)
15256ecb0c84SRoland Dreier 		return -ENODEV;
15266ecb0c84SRoland Dreier 
15276ecb0c84SRoland Dreier 	return sprintf(buf, "0x%016llx\n",
15286ecb0c84SRoland Dreier 		       (unsigned long long) be64_to_cpu(target->service_id));
15296ecb0c84SRoland Dreier }
15306ecb0c84SRoland Dreier 
1531ee959b00STony Jones static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1532ee959b00STony Jones 			 char *buf)
15336ecb0c84SRoland Dreier {
1534ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
15356ecb0c84SRoland Dreier 
15366ecb0c84SRoland Dreier 	if (target->state == SRP_TARGET_DEAD ||
15376ecb0c84SRoland Dreier 	    target->state == SRP_TARGET_REMOVED)
15386ecb0c84SRoland Dreier 		return -ENODEV;
15396ecb0c84SRoland Dreier 
15406ecb0c84SRoland Dreier 	return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
15416ecb0c84SRoland Dreier }
15426ecb0c84SRoland Dreier 
1543ee959b00STony Jones static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1544ee959b00STony Jones 			 char *buf)
15456ecb0c84SRoland Dreier {
1546ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
15476ecb0c84SRoland Dreier 
15486ecb0c84SRoland Dreier 	if (target->state == SRP_TARGET_DEAD ||
15496ecb0c84SRoland Dreier 	    target->state == SRP_TARGET_REMOVED)
15506ecb0c84SRoland Dreier 		return -ENODEV;
15516ecb0c84SRoland Dreier 
15525b095d98SHarvey Harrison 	return sprintf(buf, "%pI6\n", target->path.dgid.raw);
15536ecb0c84SRoland Dreier }
15546ecb0c84SRoland Dreier 
1555ee959b00STony Jones static ssize_t show_orig_dgid(struct device *dev,
1556ee959b00STony Jones 			      struct device_attribute *attr, char *buf)
15573633b3d0SIshai Rabinovitz {
1558ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
15593633b3d0SIshai Rabinovitz 
15603633b3d0SIshai Rabinovitz 	if (target->state == SRP_TARGET_DEAD ||
15613633b3d0SIshai Rabinovitz 	    target->state == SRP_TARGET_REMOVED)
15623633b3d0SIshai Rabinovitz 		return -ENODEV;
15633633b3d0SIshai Rabinovitz 
15645b095d98SHarvey Harrison 	return sprintf(buf, "%pI6\n", target->orig_dgid);
15653633b3d0SIshai Rabinovitz }
15663633b3d0SIshai Rabinovitz 
1567ee959b00STony Jones static ssize_t show_zero_req_lim(struct device *dev,
1568ee959b00STony Jones 				 struct device_attribute *attr, char *buf)
15696bfa24faSRoland Dreier {
1570ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
15716bfa24faSRoland Dreier 
15726bfa24faSRoland Dreier 	if (target->state == SRP_TARGET_DEAD ||
15736bfa24faSRoland Dreier 	    target->state == SRP_TARGET_REMOVED)
15746bfa24faSRoland Dreier 		return -ENODEV;
15756bfa24faSRoland Dreier 
15766bfa24faSRoland Dreier 	return sprintf(buf, "%d\n", target->zero_req_lim);
15776bfa24faSRoland Dreier }
15786bfa24faSRoland Dreier 
1579ee959b00STony Jones static ssize_t show_local_ib_port(struct device *dev,
1580ee959b00STony Jones 				  struct device_attribute *attr, char *buf)
1581ded7f1a1SIshai Rabinovitz {
1582ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
1583ded7f1a1SIshai Rabinovitz 
1584ded7f1a1SIshai Rabinovitz 	return sprintf(buf, "%d\n", target->srp_host->port);
1585ded7f1a1SIshai Rabinovitz }
1586ded7f1a1SIshai Rabinovitz 
1587ee959b00STony Jones static ssize_t show_local_ib_device(struct device *dev,
1588ee959b00STony Jones 				    struct device_attribute *attr, char *buf)
1589ded7f1a1SIshai Rabinovitz {
1590ee959b00STony Jones 	struct srp_target_port *target = host_to_target(class_to_shost(dev));
1591ded7f1a1SIshai Rabinovitz 
159205321937SGreg Kroah-Hartman 	return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
1593ded7f1a1SIshai Rabinovitz }
1594ded7f1a1SIshai Rabinovitz 
1595ee959b00STony Jones static DEVICE_ATTR(id_ext,	    S_IRUGO, show_id_ext,	   NULL);
1596ee959b00STony Jones static DEVICE_ATTR(ioc_guid,	    S_IRUGO, show_ioc_guid,	   NULL);
1597ee959b00STony Jones static DEVICE_ATTR(service_id,	    S_IRUGO, show_service_id,	   NULL);
1598ee959b00STony Jones static DEVICE_ATTR(pkey,	    S_IRUGO, show_pkey,		   NULL);
1599ee959b00STony Jones static DEVICE_ATTR(dgid,	    S_IRUGO, show_dgid,		   NULL);
1600ee959b00STony Jones static DEVICE_ATTR(orig_dgid,	    S_IRUGO, show_orig_dgid,	   NULL);
1601ee959b00STony Jones static DEVICE_ATTR(zero_req_lim,    S_IRUGO, show_zero_req_lim,	   NULL);
1602ee959b00STony Jones static DEVICE_ATTR(local_ib_port,   S_IRUGO, show_local_ib_port,   NULL);
1603ee959b00STony Jones static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
16046ecb0c84SRoland Dreier 
1605ee959b00STony Jones static struct device_attribute *srp_host_attrs[] = {
1606ee959b00STony Jones 	&dev_attr_id_ext,
1607ee959b00STony Jones 	&dev_attr_ioc_guid,
1608ee959b00STony Jones 	&dev_attr_service_id,
1609ee959b00STony Jones 	&dev_attr_pkey,
1610ee959b00STony Jones 	&dev_attr_dgid,
1611ee959b00STony Jones 	&dev_attr_orig_dgid,
1612ee959b00STony Jones 	&dev_attr_zero_req_lim,
1613ee959b00STony Jones 	&dev_attr_local_ib_port,
1614ee959b00STony Jones 	&dev_attr_local_ib_device,
16156ecb0c84SRoland Dreier 	NULL
16166ecb0c84SRoland Dreier };
16176ecb0c84SRoland Dreier 
1618aef9ec39SRoland Dreier static struct scsi_host_template srp_template = {
1619aef9ec39SRoland Dreier 	.module				= THIS_MODULE,
1620b7f008fdSRoland Dreier 	.name				= "InfiniBand SRP initiator",
1621b7f008fdSRoland Dreier 	.proc_name			= DRV_NAME,
1622aef9ec39SRoland Dreier 	.info				= srp_target_info,
1623aef9ec39SRoland Dreier 	.queuecommand			= srp_queuecommand,
1624aef9ec39SRoland Dreier 	.eh_abort_handler		= srp_abort,
1625aef9ec39SRoland Dreier 	.eh_device_reset_handler	= srp_reset_device,
1626aef9ec39SRoland Dreier 	.eh_host_reset_handler		= srp_reset_host,
1627aef9ec39SRoland Dreier 	.can_queue			= SRP_SQ_SIZE,
1628aef9ec39SRoland Dreier 	.this_id			= -1,
1629aef9ec39SRoland Dreier 	.cmd_per_lun			= SRP_SQ_SIZE,
16306ecb0c84SRoland Dreier 	.use_clustering			= ENABLE_CLUSTERING,
16316ecb0c84SRoland Dreier 	.shost_attrs			= srp_host_attrs
1632aef9ec39SRoland Dreier };
1633aef9ec39SRoland Dreier 
1634aef9ec39SRoland Dreier static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1635aef9ec39SRoland Dreier {
16363236822bSFUJITA Tomonori 	struct srp_rport_identifiers ids;
16373236822bSFUJITA Tomonori 	struct srp_rport *rport;
16383236822bSFUJITA Tomonori 
1639aef9ec39SRoland Dreier 	sprintf(target->target_name, "SRP.T10:%016llX",
1640aef9ec39SRoland Dreier 		 (unsigned long long) be64_to_cpu(target->id_ext));
1641aef9ec39SRoland Dreier 
164205321937SGreg Kroah-Hartman 	if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
1643aef9ec39SRoland Dreier 		return -ENODEV;
1644aef9ec39SRoland Dreier 
16453236822bSFUJITA Tomonori 	memcpy(ids.port_id, &target->id_ext, 8);
16463236822bSFUJITA Tomonori 	memcpy(ids.port_id + 8, &target->ioc_guid, 8);
1647aebd5e47SFUJITA Tomonori 	ids.roles = SRP_RPORT_ROLE_TARGET;
16483236822bSFUJITA Tomonori 	rport = srp_rport_add(target->scsi_host, &ids);
16493236822bSFUJITA Tomonori 	if (IS_ERR(rport)) {
16503236822bSFUJITA Tomonori 		scsi_remove_host(target->scsi_host);
16513236822bSFUJITA Tomonori 		return PTR_ERR(rport);
16523236822bSFUJITA Tomonori 	}
16533236822bSFUJITA Tomonori 
1654b3589fd4SMatthew Wilcox 	spin_lock(&host->target_lock);
1655aef9ec39SRoland Dreier 	list_add_tail(&target->list, &host->target_list);
1656b3589fd4SMatthew Wilcox 	spin_unlock(&host->target_lock);
1657aef9ec39SRoland Dreier 
1658aef9ec39SRoland Dreier 	target->state = SRP_TARGET_LIVE;
1659aef9ec39SRoland Dreier 
1660aef9ec39SRoland Dreier 	scsi_scan_target(&target->scsi_host->shost_gendev,
16611962a4a1SMatthew Wilcox 			 0, target->scsi_id, SCAN_WILD_CARD, 0);
1662aef9ec39SRoland Dreier 
1663aef9ec39SRoland Dreier 	return 0;
1664aef9ec39SRoland Dreier }
1665aef9ec39SRoland Dreier 
1666ee959b00STony Jones static void srp_release_dev(struct device *dev)
1667aef9ec39SRoland Dreier {
1668aef9ec39SRoland Dreier 	struct srp_host *host =
1669ee959b00STony Jones 		container_of(dev, struct srp_host, dev);
1670aef9ec39SRoland Dreier 
1671aef9ec39SRoland Dreier 	complete(&host->released);
1672aef9ec39SRoland Dreier }
1673aef9ec39SRoland Dreier 
1674aef9ec39SRoland Dreier static struct class srp_class = {
1675aef9ec39SRoland Dreier 	.name    = "infiniband_srp",
1676ee959b00STony Jones 	.dev_release = srp_release_dev
1677aef9ec39SRoland Dreier };
1678aef9ec39SRoland Dreier 
1679aef9ec39SRoland Dreier /*
1680aef9ec39SRoland Dreier  * Target ports are added by writing
1681aef9ec39SRoland Dreier  *
1682aef9ec39SRoland Dreier  *     id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1683aef9ec39SRoland Dreier  *     pkey=<P_Key>,service_id=<service ID>
1684aef9ec39SRoland Dreier  *
1685aef9ec39SRoland Dreier  * to the add_target sysfs attribute.
1686aef9ec39SRoland Dreier  */
1687aef9ec39SRoland Dreier enum {
1688aef9ec39SRoland Dreier 	SRP_OPT_ERR		= 0,
1689aef9ec39SRoland Dreier 	SRP_OPT_ID_EXT		= 1 << 0,
1690aef9ec39SRoland Dreier 	SRP_OPT_IOC_GUID	= 1 << 1,
1691aef9ec39SRoland Dreier 	SRP_OPT_DGID		= 1 << 2,
1692aef9ec39SRoland Dreier 	SRP_OPT_PKEY		= 1 << 3,
1693aef9ec39SRoland Dreier 	SRP_OPT_SERVICE_ID	= 1 << 4,
1694aef9ec39SRoland Dreier 	SRP_OPT_MAX_SECT	= 1 << 5,
169552fb2b50SVu Pham 	SRP_OPT_MAX_CMD_PER_LUN	= 1 << 6,
16960c0450dbSRamachandra K 	SRP_OPT_IO_CLASS	= 1 << 7,
169701cb9bcbSIshai Rabinovitz 	SRP_OPT_INITIATOR_EXT	= 1 << 8,
1698aef9ec39SRoland Dreier 	SRP_OPT_ALL		= (SRP_OPT_ID_EXT	|
1699aef9ec39SRoland Dreier 				   SRP_OPT_IOC_GUID	|
1700aef9ec39SRoland Dreier 				   SRP_OPT_DGID		|
1701aef9ec39SRoland Dreier 				   SRP_OPT_PKEY		|
1702aef9ec39SRoland Dreier 				   SRP_OPT_SERVICE_ID),
1703aef9ec39SRoland Dreier };
1704aef9ec39SRoland Dreier 
1705a447c093SSteven Whitehouse static const match_table_t srp_opt_tokens = {
1706aef9ec39SRoland Dreier 	{ SRP_OPT_ID_EXT,		"id_ext=%s" 		},
1707aef9ec39SRoland Dreier 	{ SRP_OPT_IOC_GUID,		"ioc_guid=%s" 		},
1708aef9ec39SRoland Dreier 	{ SRP_OPT_DGID,			"dgid=%s" 		},
1709aef9ec39SRoland Dreier 	{ SRP_OPT_PKEY,			"pkey=%x" 		},
1710aef9ec39SRoland Dreier 	{ SRP_OPT_SERVICE_ID,		"service_id=%s"		},
1711aef9ec39SRoland Dreier 	{ SRP_OPT_MAX_SECT,		"max_sect=%d" 		},
171252fb2b50SVu Pham 	{ SRP_OPT_MAX_CMD_PER_LUN,	"max_cmd_per_lun=%d" 	},
17130c0450dbSRamachandra K 	{ SRP_OPT_IO_CLASS,		"io_class=%x"		},
171401cb9bcbSIshai Rabinovitz 	{ SRP_OPT_INITIATOR_EXT,	"initiator_ext=%s"	},
1715aef9ec39SRoland Dreier 	{ SRP_OPT_ERR,			NULL 			}
1716aef9ec39SRoland Dreier };
1717aef9ec39SRoland Dreier 
1718aef9ec39SRoland Dreier static int srp_parse_options(const char *buf, struct srp_target_port *target)
1719aef9ec39SRoland Dreier {
1720aef9ec39SRoland Dreier 	char *options, *sep_opt;
1721aef9ec39SRoland Dreier 	char *p;
1722aef9ec39SRoland Dreier 	char dgid[3];
1723aef9ec39SRoland Dreier 	substring_t args[MAX_OPT_ARGS];
1724aef9ec39SRoland Dreier 	int opt_mask = 0;
1725aef9ec39SRoland Dreier 	int token;
1726aef9ec39SRoland Dreier 	int ret = -EINVAL;
1727aef9ec39SRoland Dreier 	int i;
1728aef9ec39SRoland Dreier 
1729aef9ec39SRoland Dreier 	options = kstrdup(buf, GFP_KERNEL);
1730aef9ec39SRoland Dreier 	if (!options)
1731aef9ec39SRoland Dreier 		return -ENOMEM;
1732aef9ec39SRoland Dreier 
1733aef9ec39SRoland Dreier 	sep_opt = options;
1734aef9ec39SRoland Dreier 	while ((p = strsep(&sep_opt, ",")) != NULL) {
1735aef9ec39SRoland Dreier 		if (!*p)
1736aef9ec39SRoland Dreier 			continue;
1737aef9ec39SRoland Dreier 
1738aef9ec39SRoland Dreier 		token = match_token(p, srp_opt_tokens, args);
1739aef9ec39SRoland Dreier 		opt_mask |= token;
1740aef9ec39SRoland Dreier 
1741aef9ec39SRoland Dreier 		switch (token) {
1742aef9ec39SRoland Dreier 		case SRP_OPT_ID_EXT:
1743aef9ec39SRoland Dreier 			p = match_strdup(args);
1744a20f3a6dSIshai Rabinovitz 			if (!p) {
1745a20f3a6dSIshai Rabinovitz 				ret = -ENOMEM;
1746a20f3a6dSIshai Rabinovitz 				goto out;
1747a20f3a6dSIshai Rabinovitz 			}
1748aef9ec39SRoland Dreier 			target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1749aef9ec39SRoland Dreier 			kfree(p);
1750aef9ec39SRoland Dreier 			break;
1751aef9ec39SRoland Dreier 
1752aef9ec39SRoland Dreier 		case SRP_OPT_IOC_GUID:
1753aef9ec39SRoland Dreier 			p = match_strdup(args);
1754a20f3a6dSIshai Rabinovitz 			if (!p) {
1755a20f3a6dSIshai Rabinovitz 				ret = -ENOMEM;
1756a20f3a6dSIshai Rabinovitz 				goto out;
1757a20f3a6dSIshai Rabinovitz 			}
1758aef9ec39SRoland Dreier 			target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1759aef9ec39SRoland Dreier 			kfree(p);
1760aef9ec39SRoland Dreier 			break;
1761aef9ec39SRoland Dreier 
1762aef9ec39SRoland Dreier 		case SRP_OPT_DGID:
1763aef9ec39SRoland Dreier 			p = match_strdup(args);
1764a20f3a6dSIshai Rabinovitz 			if (!p) {
1765a20f3a6dSIshai Rabinovitz 				ret = -ENOMEM;
1766a20f3a6dSIshai Rabinovitz 				goto out;
1767a20f3a6dSIshai Rabinovitz 			}
1768aef9ec39SRoland Dreier 			if (strlen(p) != 32) {
1769aef9ec39SRoland Dreier 				printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
1770ce1823f0SRoland Dreier 				kfree(p);
1771aef9ec39SRoland Dreier 				goto out;
1772aef9ec39SRoland Dreier 			}
1773aef9ec39SRoland Dreier 
1774aef9ec39SRoland Dreier 			for (i = 0; i < 16; ++i) {
1775aef9ec39SRoland Dreier 				strlcpy(dgid, p + i * 2, 3);
1776aef9ec39SRoland Dreier 				target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1777aef9ec39SRoland Dreier 			}
1778bf17c1c7SRoland Dreier 			kfree(p);
17793633b3d0SIshai Rabinovitz 			memcpy(target->orig_dgid, target->path.dgid.raw, 16);
1780aef9ec39SRoland Dreier 			break;
1781aef9ec39SRoland Dreier 
1782aef9ec39SRoland Dreier 		case SRP_OPT_PKEY:
1783aef9ec39SRoland Dreier 			if (match_hex(args, &token)) {
1784aef9ec39SRoland Dreier 				printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1785aef9ec39SRoland Dreier 				goto out;
1786aef9ec39SRoland Dreier 			}
1787aef9ec39SRoland Dreier 			target->path.pkey = cpu_to_be16(token);
1788aef9ec39SRoland Dreier 			break;
1789aef9ec39SRoland Dreier 
1790aef9ec39SRoland Dreier 		case SRP_OPT_SERVICE_ID:
1791aef9ec39SRoland Dreier 			p = match_strdup(args);
1792a20f3a6dSIshai Rabinovitz 			if (!p) {
1793a20f3a6dSIshai Rabinovitz 				ret = -ENOMEM;
1794a20f3a6dSIshai Rabinovitz 				goto out;
1795a20f3a6dSIshai Rabinovitz 			}
1796aef9ec39SRoland Dreier 			target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1797247e020eSSean Hefty 			target->path.service_id = target->service_id;
1798aef9ec39SRoland Dreier 			kfree(p);
1799aef9ec39SRoland Dreier 			break;
1800aef9ec39SRoland Dreier 
1801aef9ec39SRoland Dreier 		case SRP_OPT_MAX_SECT:
1802aef9ec39SRoland Dreier 			if (match_int(args, &token)) {
1803aef9ec39SRoland Dreier 				printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1804aef9ec39SRoland Dreier 				goto out;
1805aef9ec39SRoland Dreier 			}
1806aef9ec39SRoland Dreier 			target->scsi_host->max_sectors = token;
1807aef9ec39SRoland Dreier 			break;
1808aef9ec39SRoland Dreier 
180952fb2b50SVu Pham 		case SRP_OPT_MAX_CMD_PER_LUN:
181052fb2b50SVu Pham 			if (match_int(args, &token)) {
181152fb2b50SVu Pham 				printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
181252fb2b50SVu Pham 				goto out;
181352fb2b50SVu Pham 			}
181452fb2b50SVu Pham 			target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
181552fb2b50SVu Pham 			break;
181652fb2b50SVu Pham 
18170c0450dbSRamachandra K 		case SRP_OPT_IO_CLASS:
18180c0450dbSRamachandra K 			if (match_hex(args, &token)) {
18190c0450dbSRamachandra K 				printk(KERN_WARNING PFX "bad  IO class parameter '%s' \n", p);
18200c0450dbSRamachandra K 				goto out;
18210c0450dbSRamachandra K 			}
18220c0450dbSRamachandra K 			if (token != SRP_REV10_IB_IO_CLASS &&
18230c0450dbSRamachandra K 			    token != SRP_REV16A_IB_IO_CLASS) {
18240c0450dbSRamachandra K 				printk(KERN_WARNING PFX "unknown IO class parameter value"
18250c0450dbSRamachandra K 				       " %x specified (use %x or %x).\n",
18260c0450dbSRamachandra K 				       token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
18270c0450dbSRamachandra K 				goto out;
18280c0450dbSRamachandra K 			}
18290c0450dbSRamachandra K 			target->io_class = token;
18300c0450dbSRamachandra K 			break;
18310c0450dbSRamachandra K 
183201cb9bcbSIshai Rabinovitz 		case SRP_OPT_INITIATOR_EXT:
183301cb9bcbSIshai Rabinovitz 			p = match_strdup(args);
1834a20f3a6dSIshai Rabinovitz 			if (!p) {
1835a20f3a6dSIshai Rabinovitz 				ret = -ENOMEM;
1836a20f3a6dSIshai Rabinovitz 				goto out;
1837a20f3a6dSIshai Rabinovitz 			}
183801cb9bcbSIshai Rabinovitz 			target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
183901cb9bcbSIshai Rabinovitz 			kfree(p);
184001cb9bcbSIshai Rabinovitz 			break;
184101cb9bcbSIshai Rabinovitz 
1842aef9ec39SRoland Dreier 		default:
1843aef9ec39SRoland Dreier 			printk(KERN_WARNING PFX "unknown parameter or missing value "
1844aef9ec39SRoland Dreier 			       "'%s' in target creation request\n", p);
1845aef9ec39SRoland Dreier 			goto out;
1846aef9ec39SRoland Dreier 		}
1847aef9ec39SRoland Dreier 	}
1848aef9ec39SRoland Dreier 
1849aef9ec39SRoland Dreier 	if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1850aef9ec39SRoland Dreier 		ret = 0;
1851aef9ec39SRoland Dreier 	else
1852aef9ec39SRoland Dreier 		for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1853aef9ec39SRoland Dreier 			if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1854aef9ec39SRoland Dreier 			    !(srp_opt_tokens[i].token & opt_mask))
1855aef9ec39SRoland Dreier 				printk(KERN_WARNING PFX "target creation request is "
1856aef9ec39SRoland Dreier 				       "missing parameter '%s'\n",
1857aef9ec39SRoland Dreier 				       srp_opt_tokens[i].pattern);
1858aef9ec39SRoland Dreier 
1859aef9ec39SRoland Dreier out:
1860aef9ec39SRoland Dreier 	kfree(options);
1861aef9ec39SRoland Dreier 	return ret;
1862aef9ec39SRoland Dreier }
1863aef9ec39SRoland Dreier 
1864ee959b00STony Jones static ssize_t srp_create_target(struct device *dev,
1865ee959b00STony Jones 				 struct device_attribute *attr,
1866aef9ec39SRoland Dreier 				 const char *buf, size_t count)
1867aef9ec39SRoland Dreier {
1868aef9ec39SRoland Dreier 	struct srp_host *host =
1869ee959b00STony Jones 		container_of(dev, struct srp_host, dev);
1870aef9ec39SRoland Dreier 	struct Scsi_Host *target_host;
1871aef9ec39SRoland Dreier 	struct srp_target_port *target;
1872aef9ec39SRoland Dreier 	int ret;
1873aef9ec39SRoland Dreier 	int i;
1874aef9ec39SRoland Dreier 
1875aef9ec39SRoland Dreier 	target_host = scsi_host_alloc(&srp_template,
1876aef9ec39SRoland Dreier 				      sizeof (struct srp_target_port));
1877aef9ec39SRoland Dreier 	if (!target_host)
1878aef9ec39SRoland Dreier 		return -ENOMEM;
1879aef9ec39SRoland Dreier 
18803236822bSFUJITA Tomonori 	target_host->transportt = ib_srp_transport_template;
18815f068992SRoland Dreier 	target_host->max_lun     = SRP_MAX_LUN;
18823c8edf0eSArne Redlich 	target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
18835f068992SRoland Dreier 
1884aef9ec39SRoland Dreier 	target = host_to_target(target_host);
1885aef9ec39SRoland Dreier 
18860c0450dbSRamachandra K 	target->io_class   = SRP_REV16A_IB_IO_CLASS;
1887aef9ec39SRoland Dreier 	target->scsi_host  = target_host;
1888aef9ec39SRoland Dreier 	target->srp_host   = host;
1889aef9ec39SRoland Dreier 
1890d945e1dfSRoland Dreier 	INIT_LIST_HEAD(&target->free_reqs);
1891aef9ec39SRoland Dreier 	INIT_LIST_HEAD(&target->req_queue);
1892d945e1dfSRoland Dreier 	for (i = 0; i < SRP_SQ_SIZE; ++i) {
1893d945e1dfSRoland Dreier 		target->req_ring[i].index = i;
1894d945e1dfSRoland Dreier 		list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1895d945e1dfSRoland Dreier 	}
1896aef9ec39SRoland Dreier 
1897aef9ec39SRoland Dreier 	ret = srp_parse_options(buf, target);
1898aef9ec39SRoland Dreier 	if (ret)
1899aef9ec39SRoland Dreier 		goto err;
1900aef9ec39SRoland Dreier 
1901969a60f9SRoland Dreier 	ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid);
1902aef9ec39SRoland Dreier 
19037aa54bd7SDavid Dillow 	shost_printk(KERN_DEBUG, target->scsi_host, PFX
19047aa54bd7SDavid Dillow 		     "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
19055b095d98SHarvey Harrison 		     "service_id %016llx dgid %pI6\n",
1906aef9ec39SRoland Dreier 	       (unsigned long long) be64_to_cpu(target->id_ext),
1907aef9ec39SRoland Dreier 	       (unsigned long long) be64_to_cpu(target->ioc_guid),
1908aef9ec39SRoland Dreier 	       be16_to_cpu(target->path.pkey),
1909aef9ec39SRoland Dreier 	       (unsigned long long) be64_to_cpu(target->service_id),
19108867cd7cSHarvey Harrison 	       target->path.dgid.raw);
1911aef9ec39SRoland Dreier 
1912aef9ec39SRoland Dreier 	ret = srp_create_target_ib(target);
1913aef9ec39SRoland Dreier 	if (ret)
1914aef9ec39SRoland Dreier 		goto err;
1915aef9ec39SRoland Dreier 
19169fe4bcf4SDavid Dillow 	ret = srp_new_cm_id(target);
19179fe4bcf4SDavid Dillow 	if (ret)
1918aef9ec39SRoland Dreier 		goto err_free;
1919aef9ec39SRoland Dreier 
19201033ff67SIshai Rabinovitz 	target->qp_in_error = 0;
1921aef9ec39SRoland Dreier 	ret = srp_connect_target(target);
1922aef9ec39SRoland Dreier 	if (ret) {
19237aa54bd7SDavid Dillow 		shost_printk(KERN_ERR, target->scsi_host,
19247aa54bd7SDavid Dillow 			     PFX "Connection failed\n");
1925aef9ec39SRoland Dreier 		goto err_cm_id;
1926aef9ec39SRoland Dreier 	}
1927aef9ec39SRoland Dreier 
1928aef9ec39SRoland Dreier 	ret = srp_add_target(host, target);
1929aef9ec39SRoland Dreier 	if (ret)
1930aef9ec39SRoland Dreier 		goto err_disconnect;
1931aef9ec39SRoland Dreier 
1932aef9ec39SRoland Dreier 	return count;
1933aef9ec39SRoland Dreier 
1934aef9ec39SRoland Dreier err_disconnect:
1935aef9ec39SRoland Dreier 	srp_disconnect_target(target);
1936aef9ec39SRoland Dreier 
1937aef9ec39SRoland Dreier err_cm_id:
1938aef9ec39SRoland Dreier 	ib_destroy_cm_id(target->cm_id);
1939aef9ec39SRoland Dreier 
1940aef9ec39SRoland Dreier err_free:
1941aef9ec39SRoland Dreier 	srp_free_target_ib(target);
1942aef9ec39SRoland Dreier 
1943aef9ec39SRoland Dreier err:
1944aef9ec39SRoland Dreier 	scsi_host_put(target_host);
1945aef9ec39SRoland Dreier 
1946aef9ec39SRoland Dreier 	return ret;
1947aef9ec39SRoland Dreier }
1948aef9ec39SRoland Dreier 
1949ee959b00STony Jones static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1950aef9ec39SRoland Dreier 
1951ee959b00STony Jones static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1952ee959b00STony Jones 			  char *buf)
1953aef9ec39SRoland Dreier {
1954ee959b00STony Jones 	struct srp_host *host = container_of(dev, struct srp_host, dev);
1955aef9ec39SRoland Dreier 
195605321937SGreg Kroah-Hartman 	return sprintf(buf, "%s\n", host->srp_dev->dev->name);
1957aef9ec39SRoland Dreier }
1958aef9ec39SRoland Dreier 
1959ee959b00STony Jones static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1960aef9ec39SRoland Dreier 
1961ee959b00STony Jones static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1962ee959b00STony Jones 			 char *buf)
1963aef9ec39SRoland Dreier {
1964ee959b00STony Jones 	struct srp_host *host = container_of(dev, struct srp_host, dev);
1965aef9ec39SRoland Dreier 
1966aef9ec39SRoland Dreier 	return sprintf(buf, "%d\n", host->port);
1967aef9ec39SRoland Dreier }
1968aef9ec39SRoland Dreier 
1969ee959b00STony Jones static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1970aef9ec39SRoland Dreier 
1971f5358a17SRoland Dreier static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
1972aef9ec39SRoland Dreier {
1973aef9ec39SRoland Dreier 	struct srp_host *host;
1974aef9ec39SRoland Dreier 
1975aef9ec39SRoland Dreier 	host = kzalloc(sizeof *host, GFP_KERNEL);
1976aef9ec39SRoland Dreier 	if (!host)
1977aef9ec39SRoland Dreier 		return NULL;
1978aef9ec39SRoland Dreier 
1979aef9ec39SRoland Dreier 	INIT_LIST_HEAD(&host->target_list);
1980b3589fd4SMatthew Wilcox 	spin_lock_init(&host->target_lock);
1981aef9ec39SRoland Dreier 	init_completion(&host->released);
198205321937SGreg Kroah-Hartman 	host->srp_dev = device;
1983aef9ec39SRoland Dreier 	host->port = port;
1984aef9ec39SRoland Dreier 
1985ee959b00STony Jones 	host->dev.class = &srp_class;
1986ee959b00STony Jones 	host->dev.parent = device->dev->dma_device;
1987d927e38cSKay Sievers 	dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
1988aef9ec39SRoland Dreier 
1989ee959b00STony Jones 	if (device_register(&host->dev))
1990f5358a17SRoland Dreier 		goto free_host;
1991ee959b00STony Jones 	if (device_create_file(&host->dev, &dev_attr_add_target))
1992aef9ec39SRoland Dreier 		goto err_class;
1993ee959b00STony Jones 	if (device_create_file(&host->dev, &dev_attr_ibdev))
1994aef9ec39SRoland Dreier 		goto err_class;
1995ee959b00STony Jones 	if (device_create_file(&host->dev, &dev_attr_port))
1996aef9ec39SRoland Dreier 		goto err_class;
1997aef9ec39SRoland Dreier 
1998aef9ec39SRoland Dreier 	return host;
1999aef9ec39SRoland Dreier 
2000aef9ec39SRoland Dreier err_class:
2001ee959b00STony Jones 	device_unregister(&host->dev);
2002aef9ec39SRoland Dreier 
2003f5358a17SRoland Dreier free_host:
2004aef9ec39SRoland Dreier 	kfree(host);
2005aef9ec39SRoland Dreier 
2006aef9ec39SRoland Dreier 	return NULL;
2007aef9ec39SRoland Dreier }
2008aef9ec39SRoland Dreier 
2009aef9ec39SRoland Dreier static void srp_add_one(struct ib_device *device)
2010aef9ec39SRoland Dreier {
2011f5358a17SRoland Dreier 	struct srp_device *srp_dev;
2012f5358a17SRoland Dreier 	struct ib_device_attr *dev_attr;
2013f5358a17SRoland Dreier 	struct ib_fmr_pool_param fmr_param;
2014aef9ec39SRoland Dreier 	struct srp_host *host;
2015aef9ec39SRoland Dreier 	int s, e, p;
2016aef9ec39SRoland Dreier 
2017f5358a17SRoland Dreier 	dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2018f5358a17SRoland Dreier 	if (!dev_attr)
2019cf311cd4SSean Hefty 		return;
2020aef9ec39SRoland Dreier 
2021f5358a17SRoland Dreier 	if (ib_query_device(device, dev_attr)) {
2022f5358a17SRoland Dreier 		printk(KERN_WARNING PFX "Query device failed for %s\n",
2023f5358a17SRoland Dreier 		       device->name);
2024f5358a17SRoland Dreier 		goto free_attr;
2025f5358a17SRoland Dreier 	}
2026f5358a17SRoland Dreier 
2027f5358a17SRoland Dreier 	srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2028f5358a17SRoland Dreier 	if (!srp_dev)
2029f5358a17SRoland Dreier 		goto free_attr;
2030f5358a17SRoland Dreier 
2031f5358a17SRoland Dreier 	/*
2032f5358a17SRoland Dreier 	 * Use the smallest page size supported by the HCA, down to a
2033f5358a17SRoland Dreier 	 * minimum of 512 bytes (which is the smallest sector that a
2034f5358a17SRoland Dreier 	 * SCSI command will ever carry).
2035f5358a17SRoland Dreier 	 */
2036f5358a17SRoland Dreier 	srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2037f5358a17SRoland Dreier 	srp_dev->fmr_page_size  = 1 << srp_dev->fmr_page_shift;
2038bf628dc2SRoland Dreier 	srp_dev->fmr_page_mask  = ~((u64) srp_dev->fmr_page_size - 1);
2039f5358a17SRoland Dreier 
2040f5358a17SRoland Dreier 	INIT_LIST_HEAD(&srp_dev->dev_list);
2041f5358a17SRoland Dreier 
2042f5358a17SRoland Dreier 	srp_dev->dev = device;
2043f5358a17SRoland Dreier 	srp_dev->pd  = ib_alloc_pd(device);
2044f5358a17SRoland Dreier 	if (IS_ERR(srp_dev->pd))
2045f5358a17SRoland Dreier 		goto free_dev;
2046f5358a17SRoland Dreier 
2047f5358a17SRoland Dreier 	srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2048f5358a17SRoland Dreier 				    IB_ACCESS_LOCAL_WRITE |
2049f5358a17SRoland Dreier 				    IB_ACCESS_REMOTE_READ |
2050f5358a17SRoland Dreier 				    IB_ACCESS_REMOTE_WRITE);
2051f5358a17SRoland Dreier 	if (IS_ERR(srp_dev->mr))
2052f5358a17SRoland Dreier 		goto err_pd;
2053f5358a17SRoland Dreier 
2054f5358a17SRoland Dreier 	memset(&fmr_param, 0, sizeof fmr_param);
2055f5358a17SRoland Dreier 	fmr_param.pool_size	    = SRP_FMR_POOL_SIZE;
2056f5358a17SRoland Dreier 	fmr_param.dirty_watermark   = SRP_FMR_DIRTY_SIZE;
2057f5358a17SRoland Dreier 	fmr_param.cache		    = 1;
2058f5358a17SRoland Dreier 	fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2059f5358a17SRoland Dreier 	fmr_param.page_shift	    = srp_dev->fmr_page_shift;
2060f5358a17SRoland Dreier 	fmr_param.access	    = (IB_ACCESS_LOCAL_WRITE |
2061f5358a17SRoland Dreier 				       IB_ACCESS_REMOTE_WRITE |
2062f5358a17SRoland Dreier 				       IB_ACCESS_REMOTE_READ);
2063f5358a17SRoland Dreier 
2064f5358a17SRoland Dreier 	srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2065f5358a17SRoland Dreier 	if (IS_ERR(srp_dev->fmr_pool))
2066f5358a17SRoland Dreier 		srp_dev->fmr_pool = NULL;
2067aef9ec39SRoland Dreier 
206807ebafbaSTom Tucker 	if (device->node_type == RDMA_NODE_IB_SWITCH) {
2069aef9ec39SRoland Dreier 		s = 0;
2070aef9ec39SRoland Dreier 		e = 0;
2071aef9ec39SRoland Dreier 	} else {
2072aef9ec39SRoland Dreier 		s = 1;
2073aef9ec39SRoland Dreier 		e = device->phys_port_cnt;
2074aef9ec39SRoland Dreier 	}
2075aef9ec39SRoland Dreier 
2076aef9ec39SRoland Dreier 	for (p = s; p <= e; ++p) {
2077f5358a17SRoland Dreier 		host = srp_add_port(srp_dev, p);
2078aef9ec39SRoland Dreier 		if (host)
2079f5358a17SRoland Dreier 			list_add_tail(&host->list, &srp_dev->dev_list);
2080aef9ec39SRoland Dreier 	}
2081aef9ec39SRoland Dreier 
2082f5358a17SRoland Dreier 	ib_set_client_data(device, &srp_client, srp_dev);
2083f5358a17SRoland Dreier 
2084f5358a17SRoland Dreier 	goto free_attr;
2085f5358a17SRoland Dreier 
2086f5358a17SRoland Dreier err_pd:
2087f5358a17SRoland Dreier 	ib_dealloc_pd(srp_dev->pd);
2088f5358a17SRoland Dreier 
2089f5358a17SRoland Dreier free_dev:
2090f5358a17SRoland Dreier 	kfree(srp_dev);
2091f5358a17SRoland Dreier 
2092f5358a17SRoland Dreier free_attr:
2093f5358a17SRoland Dreier 	kfree(dev_attr);
2094aef9ec39SRoland Dreier }
2095aef9ec39SRoland Dreier 
2096aef9ec39SRoland Dreier static void srp_remove_one(struct ib_device *device)
2097aef9ec39SRoland Dreier {
2098f5358a17SRoland Dreier 	struct srp_device *srp_dev;
2099aef9ec39SRoland Dreier 	struct srp_host *host, *tmp_host;
2100aef9ec39SRoland Dreier 	LIST_HEAD(target_list);
2101aef9ec39SRoland Dreier 	struct srp_target_port *target, *tmp_target;
2102aef9ec39SRoland Dreier 
2103f5358a17SRoland Dreier 	srp_dev = ib_get_client_data(device, &srp_client);
2104aef9ec39SRoland Dreier 
2105f5358a17SRoland Dreier 	list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2106ee959b00STony Jones 		device_unregister(&host->dev);
2107aef9ec39SRoland Dreier 		/*
2108aef9ec39SRoland Dreier 		 * Wait for the sysfs entry to go away, so that no new
2109aef9ec39SRoland Dreier 		 * target ports can be created.
2110aef9ec39SRoland Dreier 		 */
2111aef9ec39SRoland Dreier 		wait_for_completion(&host->released);
2112aef9ec39SRoland Dreier 
2113aef9ec39SRoland Dreier 		/*
2114aef9ec39SRoland Dreier 		 * Mark all target ports as removed, so we stop queueing
2115aef9ec39SRoland Dreier 		 * commands and don't try to reconnect.
2116aef9ec39SRoland Dreier 		 */
2117b3589fd4SMatthew Wilcox 		spin_lock(&host->target_lock);
2118549c5fc2SMatthew Wilcox 		list_for_each_entry(target, &host->target_list, list) {
21190c5b3952SIshai Rabinovitz 			spin_lock_irq(target->scsi_host->host_lock);
2120aef9ec39SRoland Dreier 			target->state = SRP_TARGET_REMOVED;
21210c5b3952SIshai Rabinovitz 			spin_unlock_irq(target->scsi_host->host_lock);
2122aef9ec39SRoland Dreier 		}
2123b3589fd4SMatthew Wilcox 		spin_unlock(&host->target_lock);
2124aef9ec39SRoland Dreier 
2125aef9ec39SRoland Dreier 		/*
2126aef9ec39SRoland Dreier 		 * Wait for any reconnection tasks that may have
2127aef9ec39SRoland Dreier 		 * started before we marked our target ports as
2128aef9ec39SRoland Dreier 		 * removed, and any target port removal tasks.
2129aef9ec39SRoland Dreier 		 */
2130aef9ec39SRoland Dreier 		flush_scheduled_work();
2131aef9ec39SRoland Dreier 
2132aef9ec39SRoland Dreier 		list_for_each_entry_safe(target, tmp_target,
2133aef9ec39SRoland Dreier 					 &host->target_list, list) {
2134b0e47c8bSDavid Dillow 			srp_remove_host(target->scsi_host);
2135ad696989SDave Dillow 			scsi_remove_host(target->scsi_host);
2136aef9ec39SRoland Dreier 			srp_disconnect_target(target);
2137aef9ec39SRoland Dreier 			ib_destroy_cm_id(target->cm_id);
2138aef9ec39SRoland Dreier 			srp_free_target_ib(target);
2139aef9ec39SRoland Dreier 			scsi_host_put(target->scsi_host);
2140aef9ec39SRoland Dreier 		}
2141aef9ec39SRoland Dreier 
2142aef9ec39SRoland Dreier 		kfree(host);
2143aef9ec39SRoland Dreier 	}
2144aef9ec39SRoland Dreier 
2145f5358a17SRoland Dreier 	if (srp_dev->fmr_pool)
2146f5358a17SRoland Dreier 		ib_destroy_fmr_pool(srp_dev->fmr_pool);
2147f5358a17SRoland Dreier 	ib_dereg_mr(srp_dev->mr);
2148f5358a17SRoland Dreier 	ib_dealloc_pd(srp_dev->pd);
2149f5358a17SRoland Dreier 
2150f5358a17SRoland Dreier 	kfree(srp_dev);
2151aef9ec39SRoland Dreier }
2152aef9ec39SRoland Dreier 
21533236822bSFUJITA Tomonori static struct srp_function_template ib_srp_transport_functions = {
21543236822bSFUJITA Tomonori };
21553236822bSFUJITA Tomonori 
2156aef9ec39SRoland Dreier static int __init srp_init_module(void)
2157aef9ec39SRoland Dreier {
2158aef9ec39SRoland Dreier 	int ret;
2159aef9ec39SRoland Dreier 
21601e89a194SDavid Dillow 	if (srp_sg_tablesize > 255) {
21611e89a194SDavid Dillow 		printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
21621e89a194SDavid Dillow 		srp_sg_tablesize = 255;
21631e89a194SDavid Dillow 	}
21641e89a194SDavid Dillow 
21653236822bSFUJITA Tomonori 	ib_srp_transport_template =
21663236822bSFUJITA Tomonori 		srp_attach_transport(&ib_srp_transport_functions);
21673236822bSFUJITA Tomonori 	if (!ib_srp_transport_template)
21683236822bSFUJITA Tomonori 		return -ENOMEM;
21693236822bSFUJITA Tomonori 
217074b0a15bSVu Pham 	srp_template.sg_tablesize = srp_sg_tablesize;
217174b0a15bSVu Pham 	srp_max_iu_len = (sizeof (struct srp_cmd) +
217274b0a15bSVu Pham 			  sizeof (struct srp_indirect_buf) +
217374b0a15bSVu Pham 			  srp_sg_tablesize * 16);
217474b0a15bSVu Pham 
2175aef9ec39SRoland Dreier 	ret = class_register(&srp_class);
2176aef9ec39SRoland Dreier 	if (ret) {
2177aef9ec39SRoland Dreier 		printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
21783236822bSFUJITA Tomonori 		srp_release_transport(ib_srp_transport_template);
2179aef9ec39SRoland Dreier 		return ret;
2180aef9ec39SRoland Dreier 	}
2181aef9ec39SRoland Dreier 
2182c1a0b23bSMichael S. Tsirkin 	ib_sa_register_client(&srp_sa_client);
2183c1a0b23bSMichael S. Tsirkin 
2184aef9ec39SRoland Dreier 	ret = ib_register_client(&srp_client);
2185aef9ec39SRoland Dreier 	if (ret) {
2186aef9ec39SRoland Dreier 		printk(KERN_ERR PFX "couldn't register IB client\n");
21873236822bSFUJITA Tomonori 		srp_release_transport(ib_srp_transport_template);
2188c1a0b23bSMichael S. Tsirkin 		ib_sa_unregister_client(&srp_sa_client);
2189aef9ec39SRoland Dreier 		class_unregister(&srp_class);
2190aef9ec39SRoland Dreier 		return ret;
2191aef9ec39SRoland Dreier 	}
2192aef9ec39SRoland Dreier 
2193aef9ec39SRoland Dreier 	return 0;
2194aef9ec39SRoland Dreier }
2195aef9ec39SRoland Dreier 
2196aef9ec39SRoland Dreier static void __exit srp_cleanup_module(void)
2197aef9ec39SRoland Dreier {
2198aef9ec39SRoland Dreier 	ib_unregister_client(&srp_client);
2199c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&srp_sa_client);
2200aef9ec39SRoland Dreier 	class_unregister(&srp_class);
22013236822bSFUJITA Tomonori 	srp_release_transport(ib_srp_transport_template);
2202aef9ec39SRoland Dreier }
2203aef9ec39SRoland Dreier 
2204aef9ec39SRoland Dreier module_init(srp_init_module);
2205aef9ec39SRoland Dreier module_exit(srp_cleanup_module);
2206