blob: 4675defb374c22b0e4cf4e3341ce7e24ac90e841 [file] [log] [blame]
Roland Dreieraef9ec32005-11-02 14:07:13 -08001/*
2 * Copyright (c) 2005 Cisco Systems. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
Roland Dreieraef9ec32005-11-02 14:07:13 -080031 */
32
Roland Dreieraef9ec32005-11-02 14:07:13 -080033#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/slab.h>
36#include <linux/err.h>
37#include <linux/string.h>
38#include <linux/parser.h>
39#include <linux/random.h>
Tim Schmielaude259682006-01-08 01:02:05 -080040#include <linux/jiffies.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080041
42#include <asm/atomic.h>
43
44#include <scsi/scsi.h>
45#include <scsi/scsi_device.h>
46#include <scsi/scsi_dbg.h>
47#include <scsi/srp.h>
FUJITA Tomonori32368222007-06-27 16:33:12 +090048#include <scsi/scsi_transport_srp.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080049
Roland Dreieraef9ec32005-11-02 14:07:13 -080050#include "ib_srp.h"
51
52#define DRV_NAME "ib_srp"
53#define PFX DRV_NAME ": "
54#define DRV_VERSION "0.2"
55#define DRV_RELDATE "November 1, 2005"
56
57MODULE_AUTHOR("Roland Dreier");
58MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
59 "v" DRV_VERSION " (" DRV_RELDATE ")");
60MODULE_LICENSE("Dual BSD/GPL");
61
Vu Pham74b0a152006-06-17 20:37:32 -070062static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
63static int srp_max_iu_len;
64
65module_param(srp_sg_tablesize, int, 0444);
66MODULE_PARM_DESC(srp_sg_tablesize,
David Dillow1e89a192008-04-16 21:01:12 -070067 "Max number of gather/scatter entries per I/O (default is 12, max 255)");
Vu Pham74b0a152006-06-17 20:37:32 -070068
Roland Dreieraef9ec32005-11-02 14:07:13 -080069static int topspin_workarounds = 1;
70
71module_param(topspin_workarounds, int, 0444);
72MODULE_PARM_DESC(topspin_workarounds,
73 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
74
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070075static int mellanox_workarounds = 1;
76
77module_param(mellanox_workarounds, int, 0444);
78MODULE_PARM_DESC(mellanox_workarounds,
79 "Enable workarounds for Mellanox SRP target bugs if != 0");
80
Roland Dreieraef9ec32005-11-02 14:07:13 -080081static void srp_add_one(struct ib_device *device);
82static void srp_remove_one(struct ib_device *device);
Bart Van Assche9c03dc92010-02-02 19:23:54 +000083static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
84static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
Roland Dreieraef9ec32005-11-02 14:07:13 -080085static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
86
FUJITA Tomonori32368222007-06-27 16:33:12 +090087static struct scsi_transport_template *ib_srp_transport_template;
88
Roland Dreieraef9ec32005-11-02 14:07:13 -080089static struct ib_client srp_client = {
90 .name = "srp",
91 .add = srp_add_one,
92 .remove = srp_remove_one
93};
94
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070095static struct ib_sa_client srp_sa_client;
96
Roland Dreieraef9ec32005-11-02 14:07:13 -080097static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
98{
99 return (struct srp_target_port *) host->hostdata;
100}
101
102static const char *srp_target_info(struct Scsi_Host *host)
103{
104 return host_to_target(host)->target_name;
105}
106
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700107static int srp_target_is_topspin(struct srp_target_port *target)
108{
109 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700110 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700111
112 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700113 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
114 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700115}
116
117static int srp_target_is_mellanox(struct srp_target_port *target)
118{
119 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
120
121 return mellanox_workarounds &&
122 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
123}
124
Roland Dreieraef9ec32005-11-02 14:07:13 -0800125static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
126 gfp_t gfp_mask,
127 enum dma_data_direction direction)
128{
129 struct srp_iu *iu;
130
131 iu = kmalloc(sizeof *iu, gfp_mask);
132 if (!iu)
133 goto out;
134
135 iu->buf = kzalloc(size, gfp_mask);
136 if (!iu->buf)
137 goto out_free_iu;
138
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100139 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
140 direction);
141 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800142 goto out_free_buf;
143
144 iu->size = size;
145 iu->direction = direction;
146
147 return iu;
148
149out_free_buf:
150 kfree(iu->buf);
151out_free_iu:
152 kfree(iu);
153out:
154 return NULL;
155}
156
157static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
158{
159 if (!iu)
160 return;
161
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100162 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
163 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800164 kfree(iu->buf);
165 kfree(iu);
166}
167
168static void srp_qp_event(struct ib_event *event, void *context)
169{
170 printk(KERN_ERR PFX "QP event %d\n", event->event);
171}
172
173static int srp_init_qp(struct srp_target_port *target,
174 struct ib_qp *qp)
175{
176 struct ib_qp_attr *attr;
177 int ret;
178
179 attr = kmalloc(sizeof *attr, GFP_KERNEL);
180 if (!attr)
181 return -ENOMEM;
182
Roland Dreier969a60f2008-07-14 23:48:43 -0700183 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
184 target->srp_host->port,
185 be16_to_cpu(target->path.pkey),
186 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800187 if (ret)
188 goto out;
189
190 attr->qp_state = IB_QPS_INIT;
191 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
192 IB_ACCESS_REMOTE_WRITE);
193 attr->port_num = target->srp_host->port;
194
195 ret = ib_modify_qp(qp, attr,
196 IB_QP_STATE |
197 IB_QP_PKEY_INDEX |
198 IB_QP_ACCESS_FLAGS |
199 IB_QP_PORT);
200
201out:
202 kfree(attr);
203 return ret;
204}
205
David Dillow9fe4bcf2008-01-08 17:08:52 -0500206static int srp_new_cm_id(struct srp_target_port *target)
207{
208 struct ib_cm_id *new_cm_id;
209
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100210 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500211 srp_cm_handler, target);
212 if (IS_ERR(new_cm_id))
213 return PTR_ERR(new_cm_id);
214
215 if (target->cm_id)
216 ib_destroy_cm_id(target->cm_id);
217 target->cm_id = new_cm_id;
218
219 return 0;
220}
221
Roland Dreieraef9ec32005-11-02 14:07:13 -0800222static int srp_create_target_ib(struct srp_target_port *target)
223{
224 struct ib_qp_init_attr *init_attr;
225 int ret;
226
227 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
228 if (!init_attr)
229 return -ENOMEM;
230
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000231 target->recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
232 srp_recv_completion, NULL, target, SRP_RQ_SIZE, 0);
233 if (IS_ERR(target->recv_cq)) {
234 ret = PTR_ERR(target->recv_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800235 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800236 }
237
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000238 target->send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
239 srp_send_completion, NULL, target, SRP_SQ_SIZE, 0);
240 if (IS_ERR(target->send_cq)) {
241 ret = PTR_ERR(target->send_cq);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800242 goto err_recv_cq;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000243 }
244
245 ib_req_notify_cq(target->recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800246
247 init_attr->event_handler = srp_qp_event;
248 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
249 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
250 init_attr->cap.max_recv_sge = 1;
251 init_attr->cap.max_send_sge = 1;
252 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
253 init_attr->qp_type = IB_QPT_RC;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000254 init_attr->send_cq = target->send_cq;
255 init_attr->recv_cq = target->recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800256
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100257 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800258 if (IS_ERR(target->qp)) {
259 ret = PTR_ERR(target->qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800260 goto err_send_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800261 }
262
263 ret = srp_init_qp(target, target->qp);
Roland Dreierda9d2f02010-02-24 15:07:59 -0800264 if (ret)
265 goto err_qp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800266
Roland Dreierda9d2f02010-02-24 15:07:59 -0800267 kfree(init_attr);
268 return 0;
269
270err_qp:
271 ib_destroy_qp(target->qp);
272
273err_send_cq:
274 ib_destroy_cq(target->send_cq);
275
276err_recv_cq:
277 ib_destroy_cq(target->recv_cq);
278
279err:
Roland Dreieraef9ec32005-11-02 14:07:13 -0800280 kfree(init_attr);
281 return ret;
282}
283
284static void srp_free_target_ib(struct srp_target_port *target)
285{
286 int i;
287
288 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000289 ib_destroy_cq(target->send_cq);
290 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800291
292 for (i = 0; i < SRP_RQ_SIZE; ++i)
293 srp_free_iu(target->srp_host, target->rx_ring[i]);
294 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
295 srp_free_iu(target->srp_host, target->tx_ring[i]);
296}
297
298static void srp_path_rec_completion(int status,
299 struct ib_sa_path_rec *pathrec,
300 void *target_ptr)
301{
302 struct srp_target_port *target = target_ptr;
303
304 target->status = status;
305 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500306 shost_printk(KERN_ERR, target->scsi_host,
307 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800308 else
309 target->path = *pathrec;
310 complete(&target->done);
311}
312
313static int srp_lookup_path(struct srp_target_port *target)
314{
315 target->path.numb_path = 1;
316
317 init_completion(&target->done);
318
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700319 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100320 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800321 target->srp_host->port,
322 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700323 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800324 IB_SA_PATH_REC_DGID |
325 IB_SA_PATH_REC_SGID |
326 IB_SA_PATH_REC_NUMB_PATH |
327 IB_SA_PATH_REC_PKEY,
328 SRP_PATH_REC_TIMEOUT_MS,
329 GFP_KERNEL,
330 srp_path_rec_completion,
331 target, &target->path_query);
332 if (target->path_query_id < 0)
333 return target->path_query_id;
334
335 wait_for_completion(&target->done);
336
337 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500338 shost_printk(KERN_WARNING, target->scsi_host,
339 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800340
341 return target->status;
342}
343
344static int srp_send_req(struct srp_target_port *target)
345{
346 struct {
347 struct ib_cm_req_param param;
348 struct srp_login_req priv;
349 } *req = NULL;
350 int status;
351
352 req = kzalloc(sizeof *req, GFP_KERNEL);
353 if (!req)
354 return -ENOMEM;
355
356 req->param.primary_path = &target->path;
357 req->param.alternate_path = NULL;
358 req->param.service_id = target->service_id;
359 req->param.qp_num = target->qp->qp_num;
360 req->param.qp_type = target->qp->qp_type;
361 req->param.private_data = &req->priv;
362 req->param.private_data_len = sizeof req->priv;
363 req->param.flow_control = 1;
364
365 get_random_bytes(&req->param.starting_psn, 4);
366 req->param.starting_psn &= 0xffffff;
367
368 /*
369 * Pick some arbitrary defaults here; we could make these
370 * module parameters if anyone cared about setting them.
371 */
372 req->param.responder_resources = 4;
373 req->param.remote_cm_response_timeout = 20;
374 req->param.local_cm_response_timeout = 20;
375 req->param.retry_count = 7;
376 req->param.rnr_retry_count = 7;
377 req->param.max_cm_retries = 15;
378
379 req->priv.opcode = SRP_LOGIN_REQ;
380 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700381 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800382 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
383 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700384 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700385 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700386 * port identifier format is 8 bytes of ID extension followed
387 * by 8 bytes of GUID. Older drafts put the two halves in the
388 * opposite order, so that the GUID comes first.
389 *
390 * Targets conforming to these obsolete drafts can be
391 * recognized by the I/O Class they report.
392 */
393 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
394 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200395 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700396 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200397 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700398 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
399 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
400 } else {
401 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200402 &target->initiator_ext, 8);
403 memcpy(req->priv.initiator_port_id + 8,
404 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700405 memcpy(req->priv.target_port_id, &target->id_ext, 8);
406 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
407 }
408
Roland Dreieraef9ec32005-11-02 14:07:13 -0800409 /*
410 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200411 * zero out the first 8 bytes of our initiator port ID and set
412 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800413 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700414 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500415 shost_printk(KERN_DEBUG, target->scsi_host,
416 PFX "Topspin/Cisco initiator port ID workaround "
417 "activated for target GUID %016llx\n",
418 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800419 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200420 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100421 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800422 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800423
424 status = ib_send_cm_req(target->cm_id, &req->param);
425
426 kfree(req);
427
428 return status;
429}
430
431static void srp_disconnect_target(struct srp_target_port *target)
432{
433 /* XXX should send SRP_I_LOGOUT request */
434
435 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700436 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500437 shost_printk(KERN_DEBUG, target->scsi_host,
438 PFX "Sending CM DREQ failed\n");
Roland Dreiere6581052006-05-17 09:13:21 -0700439 return;
440 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800441 wait_for_completion(&target->done);
442}
443
David Howellsc4028952006-11-22 14:57:56 +0000444static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800445{
David Howellsc4028952006-11-22 14:57:56 +0000446 struct srp_target_port *target =
447 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800448
449 spin_lock_irq(target->scsi_host->host_lock);
450 if (target->state != SRP_TARGET_DEAD) {
451 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800452 return;
453 }
454 target->state = SRP_TARGET_REMOVED;
455 spin_unlock_irq(target->scsi_host->host_lock);
456
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700457 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800458 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700459 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800460
FUJITA Tomonori32368222007-06-27 16:33:12 +0900461 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800462 scsi_remove_host(target->scsi_host);
463 ib_destroy_cm_id(target->cm_id);
464 srp_free_target_ib(target);
465 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800466}
467
468static int srp_connect_target(struct srp_target_port *target)
469{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500470 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800471 int ret;
472
473 ret = srp_lookup_path(target);
474 if (ret)
475 return ret;
476
477 while (1) {
478 init_completion(&target->done);
479 ret = srp_send_req(target);
480 if (ret)
481 return ret;
482 wait_for_completion(&target->done);
483
484 /*
485 * The CM event handling code will set status to
486 * SRP_PORT_REDIRECT if we get a port redirect REJ
487 * back, or SRP_DLID_REDIRECT if we get a lid/qp
488 * redirect REJ back.
489 */
490 switch (target->status) {
491 case 0:
492 return 0;
493
494 case SRP_PORT_REDIRECT:
495 ret = srp_lookup_path(target);
496 if (ret)
497 return ret;
498 break;
499
500 case SRP_DLID_REDIRECT:
501 break;
502
David Dillow9fe4bcf2008-01-08 17:08:52 -0500503 case SRP_STALE_CONN:
504 /* Our current CM id was stale, and is now in timewait.
505 * Try to reconnect with a new one.
506 */
507 if (!retries-- || srp_new_cm_id(target)) {
508 shost_printk(KERN_ERR, target->scsi_host, PFX
509 "giving up on stale connection\n");
510 target->status = -ECONNRESET;
511 return target->status;
512 }
513
514 shost_printk(KERN_ERR, target->scsi_host, PFX
515 "retrying stale connection\n");
516 break;
517
Roland Dreieraef9ec32005-11-02 14:07:13 -0800518 default:
519 return target->status;
520 }
521 }
522}
523
Roland Dreierd945e1d2006-05-09 10:50:28 -0700524static void srp_unmap_data(struct scsi_cmnd *scmnd,
525 struct srp_target_port *target,
526 struct srp_request *req)
527{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900528 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700529 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
530 scmnd->sc_data_direction != DMA_FROM_DEVICE))
531 return;
532
Roland Dreierf5358a12006-06-17 20:37:29 -0700533 if (req->fmr) {
534 ib_fmr_pool_unmap(req->fmr);
535 req->fmr = NULL;
536 }
537
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100538 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900539 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700540}
541
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700542static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
543{
544 srp_unmap_data(req->scmnd, target, req);
545 list_move_tail(&req->list, &target->free_reqs);
546}
547
548static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
549{
550 req->scmnd->result = DID_RESET << 16;
551 req->scmnd->scsi_done(req->scmnd);
552 srp_remove_req(target, req);
553}
554
Roland Dreieraef9ec32005-11-02 14:07:13 -0800555static int srp_reconnect_target(struct srp_target_port *target)
556{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800557 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700558 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800559 struct ib_wc wc;
560 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800561
562 spin_lock_irq(target->scsi_host->host_lock);
563 if (target->state != SRP_TARGET_LIVE) {
564 spin_unlock_irq(target->scsi_host->host_lock);
565 return -EAGAIN;
566 }
567 target->state = SRP_TARGET_CONNECTING;
568 spin_unlock_irq(target->scsi_host->host_lock);
569
570 srp_disconnect_target(target);
571 /*
572 * Now get a new local CM ID so that we avoid confusing the
573 * target in case things are really fouled up.
574 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500575 ret = srp_new_cm_id(target);
576 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800577 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800578
579 qp_attr.qp_state = IB_QPS_RESET;
580 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
581 if (ret)
582 goto err;
583
584 ret = srp_init_qp(target, target->qp);
585 if (ret)
586 goto err;
587
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000588 while (ib_poll_cq(target->recv_cq, 1, &wc) > 0)
589 ; /* nothing */
590 while (ib_poll_cq(target->send_cq, 1, &wc) > 0)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800591 ; /* nothing */
592
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300593 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700594 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
595 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300596 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800597
598 target->rx_head = 0;
599 target->tx_head = 0;
600 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800601
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200602 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800603 ret = srp_connect_target(target);
604 if (ret)
605 goto err;
606
607 spin_lock_irq(target->scsi_host->host_lock);
608 if (target->state == SRP_TARGET_CONNECTING) {
609 ret = 0;
610 target->state = SRP_TARGET_LIVE;
611 } else
612 ret = -EAGAIN;
613 spin_unlock_irq(target->scsi_host->host_lock);
614
615 return ret;
616
617err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500618 shost_printk(KERN_ERR, target->scsi_host,
619 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800620
621 /*
622 * We couldn't reconnect, so kill our target port off.
623 * However, we have to defer the real removal because we might
624 * be in the context of the SCSI error handler now, which
625 * would deadlock if we call scsi_remove_host().
626 */
627 spin_lock_irq(target->scsi_host->host_lock);
628 if (target->state == SRP_TARGET_CONNECTING) {
629 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000630 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800631 schedule_work(&target->work);
632 }
633 spin_unlock_irq(target->scsi_host->host_lock);
634
635 return ret;
636}
637
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700638static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700639 int sg_cnt, struct srp_request *req,
640 struct srp_direct_buf *buf)
641{
642 u64 io_addr = 0;
643 u64 *dma_pages;
644 u32 len;
645 int page_cnt;
646 int i, j;
647 int ret;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100648 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800649 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900650 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700651
652 if (!dev->fmr_pool)
653 return -ENODEV;
654
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700655 if (srp_target_is_mellanox(target) &&
656 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700657 return -EINVAL;
658
Roland Dreierf5358a12006-06-17 20:37:29 -0700659 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900660 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
661 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800662
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900663 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700664 if (i > 0)
665 return -EINVAL;
666 else
667 ++page_cnt;
668 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900669 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700670 ~dev->fmr_page_mask) {
671 if (i < sg_cnt - 1)
672 return -EINVAL;
673 else
674 ++page_cnt;
675 }
676
Ralph Campbell85507bc2006-12-12 14:30:55 -0800677 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700678 }
679
680 page_cnt += len >> dev->fmr_page_shift;
681 if (page_cnt > SRP_FMR_SIZE)
682 return -ENOMEM;
683
684 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
685 if (!dma_pages)
686 return -ENOMEM;
687
688 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900689 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
690 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800691
692 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700693 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900694 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800695 dev->fmr_page_mask) + j;
696 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700697
698 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700699 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700700 if (IS_ERR(req->fmr)) {
701 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700702 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700703 goto out;
704 }
705
Ralph Campbell85507bc2006-12-12 14:30:55 -0800706 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
707 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700708 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
709 buf->len = cpu_to_be32(len);
710
711 ret = 0;
712
713out:
714 kfree(dma_pages);
715
716 return ret;
717}
718
Roland Dreieraef9ec32005-11-02 14:07:13 -0800719static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
720 struct srp_request *req)
721{
Roland Dreiercf368712006-03-24 15:47:26 -0800722 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800723 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800724 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700725 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800726 struct srp_device *dev;
727 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800728
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900729 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800730 return sizeof (struct srp_cmd);
731
732 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
733 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500734 shost_printk(KERN_WARNING, target->scsi_host,
735 PFX "Unhandled data direction %d\n",
736 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800737 return -EINVAL;
738 }
739
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900740 nents = scsi_sg_count(scmnd);
741 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800742
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100743 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800744 ibdev = dev->dev;
745
746 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700747
748 fmt = SRP_DATA_DESC_DIRECT;
749 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800750
751 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700752 /*
753 * The midlayer only generated a single gather/scatter
754 * entry, or DMA mapping coalesced everything to a
755 * single entry. So a direct descriptor along with
756 * the DMA MR suffices.
757 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800758 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800759
Ralph Campbell85507bc2006-12-12 14:30:55 -0800760 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
761 buf->key = cpu_to_be32(dev->mr->rkey);
762 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700763 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700764 (void *) cmd->add_data)) {
765 /*
766 * FMR mapping failed, and the scatterlist has more
767 * than one entry. Generate an indirect memory
768 * descriptor.
769 */
Roland Dreiercf368712006-03-24 15:47:26 -0800770 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900771 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800772 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700773 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800774
775 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700776 len = sizeof (struct srp_cmd) +
777 sizeof (struct srp_indirect_buf) +
778 count * sizeof (struct srp_direct_buf);
779
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900780 scsi_for_each_sg(scmnd, sg, count, i) {
781 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800782
Roland Dreierf5358a12006-06-17 20:37:29 -0700783 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900784 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700785 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800786 cpu_to_be32(dev->mr->rkey);
787 buf->desc_list[i].len = cpu_to_be32(dma_len);
788 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700789 }
Roland Dreiercf368712006-03-24 15:47:26 -0800790
791 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
792 cmd->data_out_desc_cnt = count;
793 else
794 cmd->data_in_desc_cnt = count;
795
Roland Dreierf5358a12006-06-17 20:37:29 -0700796 buf->table_desc.va =
797 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800798 buf->table_desc.key =
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100799 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800800 buf->table_desc.len =
801 cpu_to_be32(count * sizeof (struct srp_direct_buf));
802
Roland Dreiercf368712006-03-24 15:47:26 -0800803 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800804 }
805
806 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
807 cmd->buf_fmt = fmt << 4;
808 else
809 cmd->buf_fmt = fmt;
810
Roland Dreieraef9ec32005-11-02 14:07:13 -0800811 return len;
812}
813
Roland Dreieraef9ec32005-11-02 14:07:13 -0800814static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
815{
816 struct srp_request *req;
817 struct scsi_cmnd *scmnd;
818 unsigned long flags;
819 s32 delta;
820
821 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
822
823 spin_lock_irqsave(target->scsi_host->host_lock, flags);
824
825 target->req_lim += delta;
826
827 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
828
829 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
830 if (be32_to_cpu(rsp->resp_data_len) < 4)
831 req->tsk_status = -1;
832 else
833 req->tsk_status = rsp->data[3];
834 complete(&req->done);
835 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700836 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800837 if (!scmnd)
David Dillow7aa54bd2008-01-07 18:23:41 -0500838 shost_printk(KERN_ERR, target->scsi_host,
839 "Null scmnd for RSP w/tag %016llx\n",
840 (unsigned long long) rsp->tag);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800841 scmnd->result = rsp->status;
842
843 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
844 memcpy(scmnd->sense_buffer, rsp->data +
845 be32_to_cpu(rsp->resp_data_len),
846 min_t(int, be32_to_cpu(rsp->sense_data_len),
847 SCSI_SENSE_BUFFERSIZE));
848 }
849
850 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900851 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800852 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900853 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800854
Roland Dreieraef9ec32005-11-02 14:07:13 -0800855 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800856 scmnd->host_scribble = (void *) -1L;
857 scmnd->scsi_done(scmnd);
858
Roland Dreierd945e1d2006-05-09 10:50:28 -0700859 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800860 } else
861 req->cmd_done = 1;
862 }
863
864 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
865}
866
Roland Dreieraef9ec32005-11-02 14:07:13 -0800867static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
868{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800869 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800870 struct srp_iu *iu;
871 u8 opcode;
872
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000873 iu = target->rx_ring[wc->wr_id];
Roland Dreieraef9ec32005-11-02 14:07:13 -0800874
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100875 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800876 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
877 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800878
879 opcode = *(u8 *) iu->buf;
880
881 if (0) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500882 shost_printk(KERN_ERR, target->scsi_host,
883 PFX "recv completion, opcode 0x%02x\n", opcode);
Bart Van Assche7a700812010-07-29 15:56:37 +0000884 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
885 iu->buf, wc->byte_len, true);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800886 }
887
888 switch (opcode) {
889 case SRP_RSP:
890 srp_process_rsp(target, iu->buf);
891 break;
892
893 case SRP_T_LOGOUT:
894 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -0500895 shost_printk(KERN_WARNING, target->scsi_host,
896 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800897 break;
898
899 default:
David Dillow7aa54bd2008-01-07 18:23:41 -0500900 shost_printk(KERN_WARNING, target->scsi_host,
901 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800902 break;
903 }
904
Ralph Campbell85507bc2006-12-12 14:30:55 -0800905 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
906 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800907}
908
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000909static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800910{
911 struct srp_target_port *target = target_ptr;
912 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800913
914 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
915 while (ib_poll_cq(cq, 1, &wc) > 0) {
916 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500917 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000918 PFX "failed receive status %d\n",
David Dillow7aa54bd2008-01-07 18:23:41 -0500919 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200920 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800921 break;
922 }
923
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000924 srp_handle_recv(target, &wc);
925 }
926}
927
928static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
929{
930 struct srp_target_port *target = target_ptr;
931 struct ib_wc wc;
932
933 while (ib_poll_cq(cq, 1, &wc) > 0) {
934 if (wc.status) {
935 shost_printk(KERN_ERR, target->scsi_host,
936 PFX "failed send status %d\n",
937 wc.status);
938 target->qp_in_error = 1;
939 break;
940 }
941
942 ++target->tx_tail;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800943 }
944}
945
946static int __srp_post_recv(struct srp_target_port *target)
947{
948 struct srp_iu *iu;
949 struct ib_sge list;
950 struct ib_recv_wr wr, *bad_wr;
951 unsigned int next;
952 int ret;
953
954 next = target->rx_head & (SRP_RQ_SIZE - 1);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000955 wr.wr_id = next;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800956 iu = target->rx_ring[next];
957
958 list.addr = iu->dma;
959 list.length = iu->size;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100960 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800961
962 wr.next = NULL;
963 wr.sg_list = &list;
964 wr.num_sge = 1;
965
966 ret = ib_post_recv(target->qp, &wr, &bad_wr);
967 if (!ret)
968 ++target->rx_head;
969
970 return ret;
971}
972
973static int srp_post_recv(struct srp_target_port *target)
974{
975 unsigned long flags;
976 int ret;
977
978 spin_lock_irqsave(target->scsi_host->host_lock, flags);
979 ret = __srp_post_recv(target);
980 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
981
982 return ret;
983}
984
985/*
986 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800987 * req_lim and tx_head. Lock cannot be dropped between call here and
988 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800989 */
David Dillow8cba2072007-12-19 17:08:43 -0500990static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
991 enum srp_request_type req_type)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800992{
David Dillow8cba2072007-12-19 17:08:43 -0500993 s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
994
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000995 srp_send_completion(target->send_cq, target);
996
Roland Dreieraef9ec32005-11-02 14:07:13 -0800997 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
998 return NULL;
999
David Dillow8cba2072007-12-19 17:08:43 -05001000 if (target->req_lim < min) {
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001001 ++target->zero_req_lim;
David Dillow8cba2072007-12-19 17:08:43 -05001002 return NULL;
1003 }
Roland Dreier47f2bce2005-11-15 00:19:21 -08001004
Roland Dreieraef9ec32005-11-02 14:07:13 -08001005 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
1006}
1007
1008/*
1009 * Must be called with target->scsi_host->host_lock held to protect
1010 * req_lim and tx_head.
1011 */
1012static int __srp_post_send(struct srp_target_port *target,
1013 struct srp_iu *iu, int len)
1014{
1015 struct ib_sge list;
1016 struct ib_send_wr wr, *bad_wr;
1017 int ret = 0;
1018
Roland Dreieraef9ec32005-11-02 14:07:13 -08001019 list.addr = iu->dma;
1020 list.length = len;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001021 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001022
1023 wr.next = NULL;
1024 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
1025 wr.sg_list = &list;
1026 wr.num_sge = 1;
1027 wr.opcode = IB_WR_SEND;
1028 wr.send_flags = IB_SEND_SIGNALED;
1029
1030 ret = ib_post_send(target->qp, &wr, &bad_wr);
1031
1032 if (!ret) {
1033 ++target->tx_head;
1034 --target->req_lim;
1035 }
1036
1037 return ret;
1038}
1039
1040static int srp_queuecommand(struct scsi_cmnd *scmnd,
1041 void (*done)(struct scsi_cmnd *))
1042{
1043 struct srp_target_port *target = host_to_target(scmnd->device->host);
1044 struct srp_request *req;
1045 struct srp_iu *iu;
1046 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001047 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001048 int len;
1049
1050 if (target->state == SRP_TARGET_CONNECTING)
1051 goto err;
1052
1053 if (target->state == SRP_TARGET_DEAD ||
1054 target->state == SRP_TARGET_REMOVED) {
1055 scmnd->result = DID_BAD_TARGET << 16;
1056 done(scmnd);
1057 return 0;
1058 }
1059
David Dillow8cba2072007-12-19 17:08:43 -05001060 iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001061 if (!iu)
1062 goto err;
1063
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001064 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001065 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1066 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001067
Roland Dreierd945e1d2006-05-09 10:50:28 -07001068 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001069
1070 scmnd->scsi_done = done;
1071 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001072 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001073
1074 cmd = iu->buf;
1075 memset(cmd, 0, sizeof *cmd);
1076
1077 cmd->opcode = SRP_CMD;
1078 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001079 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001080 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1081
Roland Dreieraef9ec32005-11-02 14:07:13 -08001082 req->scmnd = scmnd;
1083 req->cmd = iu;
1084 req->cmd_done = 0;
1085 req->tsk_mgmt = NULL;
1086
1087 len = srp_map_data(scmnd, target, req);
1088 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001089 shost_printk(KERN_ERR, target->scsi_host,
1090 PFX "Failed to map data\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001091 goto err;
1092 }
1093
1094 if (__srp_post_recv(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001095 shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001096 goto err_unmap;
1097 }
1098
Ralph Campbell85507bc2006-12-12 14:30:55 -08001099 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1100 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001101
1102 if (__srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001103 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001104 goto err_unmap;
1105 }
1106
Roland Dreierd945e1d2006-05-09 10:50:28 -07001107 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001108
1109 return 0;
1110
1111err_unmap:
1112 srp_unmap_data(scmnd, target, req);
1113
1114err:
1115 return SCSI_MLQUEUE_HOST_BUSY;
1116}
1117
1118static int srp_alloc_iu_bufs(struct srp_target_port *target)
1119{
1120 int i;
1121
1122 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1123 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1124 target->max_ti_iu_len,
1125 GFP_KERNEL, DMA_FROM_DEVICE);
1126 if (!target->rx_ring[i])
1127 goto err;
1128 }
1129
1130 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1131 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001132 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001133 GFP_KERNEL, DMA_TO_DEVICE);
1134 if (!target->tx_ring[i])
1135 goto err;
1136 }
1137
1138 return 0;
1139
1140err:
1141 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1142 srp_free_iu(target->srp_host, target->rx_ring[i]);
1143 target->rx_ring[i] = NULL;
1144 }
1145
1146 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1147 srp_free_iu(target->srp_host, target->tx_ring[i]);
1148 target->tx_ring[i] = NULL;
1149 }
1150
1151 return -ENOMEM;
1152}
1153
1154static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1155 struct ib_cm_event *event,
1156 struct srp_target_port *target)
1157{
David Dillow7aa54bd2008-01-07 18:23:41 -05001158 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001159 struct ib_class_port_info *cpi;
1160 int opcode;
1161
1162 switch (event->param.rej_rcvd.reason) {
1163 case IB_CM_REJ_PORT_CM_REDIRECT:
1164 cpi = event->param.rej_rcvd.ari;
1165 target->path.dlid = cpi->redirect_lid;
1166 target->path.pkey = cpi->redirect_pkey;
1167 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1168 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1169
1170 target->status = target->path.dlid ?
1171 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1172 break;
1173
1174 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001175 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001176 /*
1177 * Topspin/Cisco SRP gateways incorrectly send
1178 * reject reason code 25 when they mean 24
1179 * (port redirect).
1180 */
1181 memcpy(target->path.dgid.raw,
1182 event->param.rej_rcvd.ari, 16);
1183
David Dillow7aa54bd2008-01-07 18:23:41 -05001184 shost_printk(KERN_DEBUG, shost,
1185 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1186 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1187 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001188
1189 target->status = SRP_PORT_REDIRECT;
1190 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001191 shost_printk(KERN_WARNING, shost,
1192 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001193 target->status = -ECONNRESET;
1194 }
1195 break;
1196
1197 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001198 shost_printk(KERN_WARNING, shost,
1199 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001200 target->status = -ECONNRESET;
1201 break;
1202
1203 case IB_CM_REJ_CONSUMER_DEFINED:
1204 opcode = *(u8 *) event->private_data;
1205 if (opcode == SRP_LOGIN_REJ) {
1206 struct srp_login_rej *rej = event->private_data;
1207 u32 reason = be32_to_cpu(rej->reason);
1208
1209 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001210 shost_printk(KERN_WARNING, shost,
1211 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001212 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001213 shost_printk(KERN_WARNING, shost,
1214 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001215 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001216 shost_printk(KERN_WARNING, shost,
1217 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1218 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001219 target->status = -ECONNRESET;
1220 break;
1221
David Dillow9fe4bcf2008-01-08 17:08:52 -05001222 case IB_CM_REJ_STALE_CONN:
1223 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1224 target->status = SRP_STALE_CONN;
1225 break;
1226
Roland Dreieraef9ec32005-11-02 14:07:13 -08001227 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001228 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1229 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001230 target->status = -ECONNRESET;
1231 }
1232}
1233
1234static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1235{
1236 struct srp_target_port *target = cm_id->context;
1237 struct ib_qp_attr *qp_attr = NULL;
1238 int attr_mask = 0;
1239 int comp = 0;
1240 int opcode = 0;
1241
1242 switch (event->event) {
1243 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001244 shost_printk(KERN_DEBUG, target->scsi_host,
1245 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001246 comp = 1;
1247 target->status = -ECONNRESET;
1248 break;
1249
1250 case IB_CM_REP_RECEIVED:
1251 comp = 1;
1252 opcode = *(u8 *) event->private_data;
1253
1254 if (opcode == SRP_LOGIN_RSP) {
1255 struct srp_login_rsp *rsp = event->private_data;
1256
1257 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1258 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1259
1260 target->scsi_host->can_queue = min(target->req_lim,
1261 target->scsi_host->can_queue);
1262 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001263 shost_printk(KERN_WARNING, target->scsi_host,
1264 PFX "Unhandled RSP opcode %#x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001265 target->status = -ECONNRESET;
1266 break;
1267 }
1268
Vu Phamd2fcea72006-11-21 14:14:10 -08001269 if (!target->rx_ring[0]) {
1270 target->status = srp_alloc_iu_bufs(target);
1271 if (target->status)
1272 break;
1273 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001274
1275 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1276 if (!qp_attr) {
1277 target->status = -ENOMEM;
1278 break;
1279 }
1280
1281 qp_attr->qp_state = IB_QPS_RTR;
1282 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1283 if (target->status)
1284 break;
1285
1286 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1287 if (target->status)
1288 break;
1289
1290 target->status = srp_post_recv(target);
1291 if (target->status)
1292 break;
1293
1294 qp_attr->qp_state = IB_QPS_RTS;
1295 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1296 if (target->status)
1297 break;
1298
1299 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1300 if (target->status)
1301 break;
1302
1303 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1304 if (target->status)
1305 break;
1306
1307 break;
1308
1309 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001310 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001311 comp = 1;
1312
1313 srp_cm_rej_handler(cm_id, event, target);
1314 break;
1315
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001316 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001317 shost_printk(KERN_WARNING, target->scsi_host,
1318 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001319 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001320 shost_printk(KERN_ERR, target->scsi_host,
1321 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001322 break;
1323
1324 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001325 shost_printk(KERN_ERR, target->scsi_host,
1326 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001327
1328 comp = 1;
1329 target->status = 0;
1330 break;
1331
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001332 case IB_CM_MRA_RECEIVED:
1333 case IB_CM_DREQ_ERROR:
1334 case IB_CM_DREP_RECEIVED:
1335 break;
1336
Roland Dreieraef9ec32005-11-02 14:07:13 -08001337 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001338 shost_printk(KERN_WARNING, target->scsi_host,
1339 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001340 break;
1341 }
1342
1343 if (comp)
1344 complete(&target->done);
1345
1346 kfree(qp_attr);
1347
1348 return 0;
1349}
1350
Roland Dreierd945e1d2006-05-09 10:50:28 -07001351static int srp_send_tsk_mgmt(struct srp_target_port *target,
1352 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001353{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001354 struct srp_iu *iu;
1355 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001356
1357 spin_lock_irq(target->scsi_host->host_lock);
1358
Roland Dreier1285b3a2006-03-03 15:47:25 -08001359 if (target->state == SRP_TARGET_DEAD ||
1360 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001361 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001362 goto out;
1363 }
1364
Roland Dreieraef9ec32005-11-02 14:07:13 -08001365 init_completion(&req->done);
1366
David Dillow8cba2072007-12-19 17:08:43 -05001367 iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001368 if (!iu)
1369 goto out;
1370
1371 tsk_mgmt = iu->buf;
1372 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1373
1374 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001375 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1376 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001377 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001378 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001379
1380 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1381 goto out;
1382
1383 req->tsk_mgmt = iu;
1384
1385 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001386
Roland Dreieraef9ec32005-11-02 14:07:13 -08001387 if (!wait_for_completion_timeout(&req->done,
1388 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001389 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001390
Roland Dreierd945e1d2006-05-09 10:50:28 -07001391 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001392
1393out:
1394 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001395 return -1;
1396}
1397
1398static int srp_find_req(struct srp_target_port *target,
1399 struct scsi_cmnd *scmnd,
1400 struct srp_request **req)
1401{
1402 if (scmnd->host_scribble == (void *) -1L)
1403 return -1;
1404
1405 *req = &target->req_ring[(long) scmnd->host_scribble];
1406
1407 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001408}
1409
1410static int srp_abort(struct scsi_cmnd *scmnd)
1411{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001412 struct srp_target_port *target = host_to_target(scmnd->device->host);
1413 struct srp_request *req;
1414 int ret = SUCCESS;
1415
David Dillow7aa54bd2008-01-07 18:23:41 -05001416 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001417
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001418 if (target->qp_in_error)
1419 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001420 if (srp_find_req(target, scmnd, &req))
1421 return FAILED;
1422 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1423 return FAILED;
1424
1425 spin_lock_irq(target->scsi_host->host_lock);
1426
1427 if (req->cmd_done) {
1428 srp_remove_req(target, req);
1429 scmnd->scsi_done(scmnd);
1430 } else if (!req->tsk_status) {
1431 srp_remove_req(target, req);
1432 scmnd->result = DID_ABORT << 16;
1433 } else
1434 ret = FAILED;
1435
1436 spin_unlock_irq(target->scsi_host->host_lock);
1437
1438 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001439}
1440
1441static int srp_reset_device(struct scsi_cmnd *scmnd)
1442{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001443 struct srp_target_port *target = host_to_target(scmnd->device->host);
1444 struct srp_request *req, *tmp;
1445
David Dillow7aa54bd2008-01-07 18:23:41 -05001446 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001447
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001448 if (target->qp_in_error)
1449 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001450 if (srp_find_req(target, scmnd, &req))
1451 return FAILED;
1452 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1453 return FAILED;
1454 if (req->tsk_status)
1455 return FAILED;
1456
1457 spin_lock_irq(target->scsi_host->host_lock);
1458
1459 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001460 if (req->scmnd->device == scmnd->device)
1461 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001462
1463 spin_unlock_irq(target->scsi_host->host_lock);
1464
1465 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001466}
1467
1468static int srp_reset_host(struct scsi_cmnd *scmnd)
1469{
1470 struct srp_target_port *target = host_to_target(scmnd->device->host);
1471 int ret = FAILED;
1472
David Dillow7aa54bd2008-01-07 18:23:41 -05001473 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001474
1475 if (!srp_reconnect_target(target))
1476 ret = SUCCESS;
1477
1478 return ret;
1479}
1480
Tony Jonesee959b02008-02-22 00:13:36 +01001481static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1482 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001483{
Tony Jonesee959b02008-02-22 00:13:36 +01001484 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001485
1486 if (target->state == SRP_TARGET_DEAD ||
1487 target->state == SRP_TARGET_REMOVED)
1488 return -ENODEV;
1489
1490 return sprintf(buf, "0x%016llx\n",
1491 (unsigned long long) be64_to_cpu(target->id_ext));
1492}
1493
Tony Jonesee959b02008-02-22 00:13:36 +01001494static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1495 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001496{
Tony Jonesee959b02008-02-22 00:13:36 +01001497 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001498
1499 if (target->state == SRP_TARGET_DEAD ||
1500 target->state == SRP_TARGET_REMOVED)
1501 return -ENODEV;
1502
1503 return sprintf(buf, "0x%016llx\n",
1504 (unsigned long long) be64_to_cpu(target->ioc_guid));
1505}
1506
Tony Jonesee959b02008-02-22 00:13:36 +01001507static ssize_t show_service_id(struct device *dev,
1508 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001509{
Tony Jonesee959b02008-02-22 00:13:36 +01001510 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001511
1512 if (target->state == SRP_TARGET_DEAD ||
1513 target->state == SRP_TARGET_REMOVED)
1514 return -ENODEV;
1515
1516 return sprintf(buf, "0x%016llx\n",
1517 (unsigned long long) be64_to_cpu(target->service_id));
1518}
1519
Tony Jonesee959b02008-02-22 00:13:36 +01001520static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1521 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001522{
Tony Jonesee959b02008-02-22 00:13:36 +01001523 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001524
1525 if (target->state == SRP_TARGET_DEAD ||
1526 target->state == SRP_TARGET_REMOVED)
1527 return -ENODEV;
1528
1529 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1530}
1531
Tony Jonesee959b02008-02-22 00:13:36 +01001532static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1533 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001534{
Tony Jonesee959b02008-02-22 00:13:36 +01001535 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001536
1537 if (target->state == SRP_TARGET_DEAD ||
1538 target->state == SRP_TARGET_REMOVED)
1539 return -ENODEV;
1540
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001541 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001542}
1543
Tony Jonesee959b02008-02-22 00:13:36 +01001544static ssize_t show_orig_dgid(struct device *dev,
1545 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001546{
Tony Jonesee959b02008-02-22 00:13:36 +01001547 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001548
1549 if (target->state == SRP_TARGET_DEAD ||
1550 target->state == SRP_TARGET_REMOVED)
1551 return -ENODEV;
1552
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001553 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001554}
1555
Tony Jonesee959b02008-02-22 00:13:36 +01001556static ssize_t show_zero_req_lim(struct device *dev,
1557 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001558{
Tony Jonesee959b02008-02-22 00:13:36 +01001559 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001560
1561 if (target->state == SRP_TARGET_DEAD ||
1562 target->state == SRP_TARGET_REMOVED)
1563 return -ENODEV;
1564
1565 return sprintf(buf, "%d\n", target->zero_req_lim);
1566}
1567
Tony Jonesee959b02008-02-22 00:13:36 +01001568static ssize_t show_local_ib_port(struct device *dev,
1569 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001570{
Tony Jonesee959b02008-02-22 00:13:36 +01001571 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001572
1573 return sprintf(buf, "%d\n", target->srp_host->port);
1574}
1575
Tony Jonesee959b02008-02-22 00:13:36 +01001576static ssize_t show_local_ib_device(struct device *dev,
1577 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001578{
Tony Jonesee959b02008-02-22 00:13:36 +01001579 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001580
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001581 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001582}
1583
Tony Jonesee959b02008-02-22 00:13:36 +01001584static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1585static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1586static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1587static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1588static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1589static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1590static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1591static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1592static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001593
Tony Jonesee959b02008-02-22 00:13:36 +01001594static struct device_attribute *srp_host_attrs[] = {
1595 &dev_attr_id_ext,
1596 &dev_attr_ioc_guid,
1597 &dev_attr_service_id,
1598 &dev_attr_pkey,
1599 &dev_attr_dgid,
1600 &dev_attr_orig_dgid,
1601 &dev_attr_zero_req_lim,
1602 &dev_attr_local_ib_port,
1603 &dev_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001604 NULL
1605};
1606
Roland Dreieraef9ec32005-11-02 14:07:13 -08001607static struct scsi_host_template srp_template = {
1608 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001609 .name = "InfiniBand SRP initiator",
1610 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001611 .info = srp_target_info,
1612 .queuecommand = srp_queuecommand,
1613 .eh_abort_handler = srp_abort,
1614 .eh_device_reset_handler = srp_reset_device,
1615 .eh_host_reset_handler = srp_reset_host,
1616 .can_queue = SRP_SQ_SIZE,
1617 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001618 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001619 .use_clustering = ENABLE_CLUSTERING,
1620 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001621};
1622
1623static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1624{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001625 struct srp_rport_identifiers ids;
1626 struct srp_rport *rport;
1627
Roland Dreieraef9ec32005-11-02 14:07:13 -08001628 sprintf(target->target_name, "SRP.T10:%016llX",
1629 (unsigned long long) be64_to_cpu(target->id_ext));
1630
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001631 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001632 return -ENODEV;
1633
FUJITA Tomonori32368222007-06-27 16:33:12 +09001634 memcpy(ids.port_id, &target->id_ext, 8);
1635 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001636 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001637 rport = srp_rport_add(target->scsi_host, &ids);
1638 if (IS_ERR(rport)) {
1639 scsi_remove_host(target->scsi_host);
1640 return PTR_ERR(rport);
1641 }
1642
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001643 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001644 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001645 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001646
1647 target->state = SRP_TARGET_LIVE;
1648
Roland Dreieraef9ec32005-11-02 14:07:13 -08001649 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001650 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001651
1652 return 0;
1653}
1654
Tony Jonesee959b02008-02-22 00:13:36 +01001655static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001656{
1657 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001658 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001659
1660 complete(&host->released);
1661}
1662
1663static struct class srp_class = {
1664 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01001665 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08001666};
1667
1668/*
1669 * Target ports are added by writing
1670 *
1671 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1672 * pkey=<P_Key>,service_id=<service ID>
1673 *
1674 * to the add_target sysfs attribute.
1675 */
1676enum {
1677 SRP_OPT_ERR = 0,
1678 SRP_OPT_ID_EXT = 1 << 0,
1679 SRP_OPT_IOC_GUID = 1 << 1,
1680 SRP_OPT_DGID = 1 << 2,
1681 SRP_OPT_PKEY = 1 << 3,
1682 SRP_OPT_SERVICE_ID = 1 << 4,
1683 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001684 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001685 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001686 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001687 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1688 SRP_OPT_IOC_GUID |
1689 SRP_OPT_DGID |
1690 SRP_OPT_PKEY |
1691 SRP_OPT_SERVICE_ID),
1692};
1693
Steven Whitehousea447c092008-10-13 10:46:57 +01001694static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001695 { SRP_OPT_ID_EXT, "id_ext=%s" },
1696 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1697 { SRP_OPT_DGID, "dgid=%s" },
1698 { SRP_OPT_PKEY, "pkey=%x" },
1699 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1700 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1701 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001702 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001703 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001704 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001705};
1706
1707static int srp_parse_options(const char *buf, struct srp_target_port *target)
1708{
1709 char *options, *sep_opt;
1710 char *p;
1711 char dgid[3];
1712 substring_t args[MAX_OPT_ARGS];
1713 int opt_mask = 0;
1714 int token;
1715 int ret = -EINVAL;
1716 int i;
1717
1718 options = kstrdup(buf, GFP_KERNEL);
1719 if (!options)
1720 return -ENOMEM;
1721
1722 sep_opt = options;
1723 while ((p = strsep(&sep_opt, ",")) != NULL) {
1724 if (!*p)
1725 continue;
1726
1727 token = match_token(p, srp_opt_tokens, args);
1728 opt_mask |= token;
1729
1730 switch (token) {
1731 case SRP_OPT_ID_EXT:
1732 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001733 if (!p) {
1734 ret = -ENOMEM;
1735 goto out;
1736 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001737 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1738 kfree(p);
1739 break;
1740
1741 case SRP_OPT_IOC_GUID:
1742 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001743 if (!p) {
1744 ret = -ENOMEM;
1745 goto out;
1746 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001747 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1748 kfree(p);
1749 break;
1750
1751 case SRP_OPT_DGID:
1752 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001753 if (!p) {
1754 ret = -ENOMEM;
1755 goto out;
1756 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001757 if (strlen(p) != 32) {
1758 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001759 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001760 goto out;
1761 }
1762
1763 for (i = 0; i < 16; ++i) {
1764 strlcpy(dgid, p + i * 2, 3);
1765 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1766 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001767 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001768 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001769 break;
1770
1771 case SRP_OPT_PKEY:
1772 if (match_hex(args, &token)) {
1773 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1774 goto out;
1775 }
1776 target->path.pkey = cpu_to_be16(token);
1777 break;
1778
1779 case SRP_OPT_SERVICE_ID:
1780 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001781 if (!p) {
1782 ret = -ENOMEM;
1783 goto out;
1784 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001785 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07001786 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001787 kfree(p);
1788 break;
1789
1790 case SRP_OPT_MAX_SECT:
1791 if (match_int(args, &token)) {
1792 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1793 goto out;
1794 }
1795 target->scsi_host->max_sectors = token;
1796 break;
1797
Vu Pham52fb2b502006-06-17 20:37:31 -07001798 case SRP_OPT_MAX_CMD_PER_LUN:
1799 if (match_int(args, &token)) {
1800 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1801 goto out;
1802 }
1803 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1804 break;
1805
Ramachandra K0c0450db2006-06-17 20:37:38 -07001806 case SRP_OPT_IO_CLASS:
1807 if (match_hex(args, &token)) {
1808 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1809 goto out;
1810 }
1811 if (token != SRP_REV10_IB_IO_CLASS &&
1812 token != SRP_REV16A_IB_IO_CLASS) {
1813 printk(KERN_WARNING PFX "unknown IO class parameter value"
1814 " %x specified (use %x or %x).\n",
1815 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1816 goto out;
1817 }
1818 target->io_class = token;
1819 break;
1820
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001821 case SRP_OPT_INITIATOR_EXT:
1822 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001823 if (!p) {
1824 ret = -ENOMEM;
1825 goto out;
1826 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001827 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1828 kfree(p);
1829 break;
1830
Roland Dreieraef9ec32005-11-02 14:07:13 -08001831 default:
1832 printk(KERN_WARNING PFX "unknown parameter or missing value "
1833 "'%s' in target creation request\n", p);
1834 goto out;
1835 }
1836 }
1837
1838 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1839 ret = 0;
1840 else
1841 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1842 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1843 !(srp_opt_tokens[i].token & opt_mask))
1844 printk(KERN_WARNING PFX "target creation request is "
1845 "missing parameter '%s'\n",
1846 srp_opt_tokens[i].pattern);
1847
1848out:
1849 kfree(options);
1850 return ret;
1851}
1852
Tony Jonesee959b02008-02-22 00:13:36 +01001853static ssize_t srp_create_target(struct device *dev,
1854 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001855 const char *buf, size_t count)
1856{
1857 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001858 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001859 struct Scsi_Host *target_host;
1860 struct srp_target_port *target;
1861 int ret;
1862 int i;
1863
1864 target_host = scsi_host_alloc(&srp_template,
1865 sizeof (struct srp_target_port));
1866 if (!target_host)
1867 return -ENOMEM;
1868
FUJITA Tomonori32368222007-06-27 16:33:12 +09001869 target_host->transportt = ib_srp_transport_template;
Arne Redlich3c8edf02006-11-15 12:43:00 +01001870 target_host->max_lun = SRP_MAX_LUN;
1871 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001872
Roland Dreieraef9ec32005-11-02 14:07:13 -08001873 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001874
Ramachandra K0c0450db2006-06-17 20:37:38 -07001875 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001876 target->scsi_host = target_host;
1877 target->srp_host = host;
1878
Roland Dreierd945e1d2006-05-09 10:50:28 -07001879 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001880 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001881 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1882 target->req_ring[i].index = i;
1883 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1884 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001885
1886 ret = srp_parse_options(buf, target);
1887 if (ret)
1888 goto err;
1889
Roland Dreier969a60f2008-07-14 23:48:43 -07001890 ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001891
David Dillow7aa54bd2008-01-07 18:23:41 -05001892 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1893 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001894 "service_id %016llx dgid %pI6\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08001895 (unsigned long long) be64_to_cpu(target->id_ext),
1896 (unsigned long long) be64_to_cpu(target->ioc_guid),
1897 be16_to_cpu(target->path.pkey),
1898 (unsigned long long) be64_to_cpu(target->service_id),
Harvey Harrison8867cd72008-10-28 22:36:33 -07001899 target->path.dgid.raw);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001900
1901 ret = srp_create_target_ib(target);
1902 if (ret)
1903 goto err;
1904
David Dillow9fe4bcf2008-01-08 17:08:52 -05001905 ret = srp_new_cm_id(target);
1906 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001907 goto err_free;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001908
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001909 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001910 ret = srp_connect_target(target);
1911 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001912 shost_printk(KERN_ERR, target->scsi_host,
1913 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001914 goto err_cm_id;
1915 }
1916
1917 ret = srp_add_target(host, target);
1918 if (ret)
1919 goto err_disconnect;
1920
1921 return count;
1922
1923err_disconnect:
1924 srp_disconnect_target(target);
1925
1926err_cm_id:
1927 ib_destroy_cm_id(target->cm_id);
1928
1929err_free:
1930 srp_free_target_ib(target);
1931
1932err:
1933 scsi_host_put(target_host);
1934
1935 return ret;
1936}
1937
Tony Jonesee959b02008-02-22 00:13:36 +01001938static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001939
Tony Jonesee959b02008-02-22 00:13:36 +01001940static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1941 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001942{
Tony Jonesee959b02008-02-22 00:13:36 +01001943 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001944
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001945 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001946}
1947
Tony Jonesee959b02008-02-22 00:13:36 +01001948static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001949
Tony Jonesee959b02008-02-22 00:13:36 +01001950static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1951 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001952{
Tony Jonesee959b02008-02-22 00:13:36 +01001953 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001954
1955 return sprintf(buf, "%d\n", host->port);
1956}
1957
Tony Jonesee959b02008-02-22 00:13:36 +01001958static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001959
Roland Dreierf5358a12006-06-17 20:37:29 -07001960static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001961{
1962 struct srp_host *host;
1963
1964 host = kzalloc(sizeof *host, GFP_KERNEL);
1965 if (!host)
1966 return NULL;
1967
1968 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001969 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001970 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001971 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001972 host->port = port;
1973
Tony Jonesee959b02008-02-22 00:13:36 +01001974 host->dev.class = &srp_class;
1975 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08001976 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001977
Tony Jonesee959b02008-02-22 00:13:36 +01001978 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001979 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01001980 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001981 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001982 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001983 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001984 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001985 goto err_class;
1986
1987 return host;
1988
1989err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01001990 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001991
Roland Dreierf5358a12006-06-17 20:37:29 -07001992free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001993 kfree(host);
1994
1995 return NULL;
1996}
1997
1998static void srp_add_one(struct ib_device *device)
1999{
Roland Dreierf5358a12006-06-17 20:37:29 -07002000 struct srp_device *srp_dev;
2001 struct ib_device_attr *dev_attr;
2002 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002003 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002004 int s, e, p;
2005
Roland Dreierf5358a12006-06-17 20:37:29 -07002006 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2007 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002008 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002009
Roland Dreierf5358a12006-06-17 20:37:29 -07002010 if (ib_query_device(device, dev_attr)) {
2011 printk(KERN_WARNING PFX "Query device failed for %s\n",
2012 device->name);
2013 goto free_attr;
2014 }
2015
2016 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2017 if (!srp_dev)
2018 goto free_attr;
2019
2020 /*
2021 * Use the smallest page size supported by the HCA, down to a
2022 * minimum of 512 bytes (which is the smallest sector that a
2023 * SCSI command will ever carry).
2024 */
2025 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2026 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08002027 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07002028
2029 INIT_LIST_HEAD(&srp_dev->dev_list);
2030
2031 srp_dev->dev = device;
2032 srp_dev->pd = ib_alloc_pd(device);
2033 if (IS_ERR(srp_dev->pd))
2034 goto free_dev;
2035
2036 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2037 IB_ACCESS_LOCAL_WRITE |
2038 IB_ACCESS_REMOTE_READ |
2039 IB_ACCESS_REMOTE_WRITE);
2040 if (IS_ERR(srp_dev->mr))
2041 goto err_pd;
2042
2043 memset(&fmr_param, 0, sizeof fmr_param);
2044 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2045 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2046 fmr_param.cache = 1;
2047 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2048 fmr_param.page_shift = srp_dev->fmr_page_shift;
2049 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2050 IB_ACCESS_REMOTE_WRITE |
2051 IB_ACCESS_REMOTE_READ);
2052
2053 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2054 if (IS_ERR(srp_dev->fmr_pool))
2055 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002056
Tom Tucker07ebafb2006-08-03 16:02:42 -05002057 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002058 s = 0;
2059 e = 0;
2060 } else {
2061 s = 1;
2062 e = device->phys_port_cnt;
2063 }
2064
2065 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002066 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002067 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002068 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002069 }
2070
Roland Dreierf5358a12006-06-17 20:37:29 -07002071 ib_set_client_data(device, &srp_client, srp_dev);
2072
2073 goto free_attr;
2074
2075err_pd:
2076 ib_dealloc_pd(srp_dev->pd);
2077
2078free_dev:
2079 kfree(srp_dev);
2080
2081free_attr:
2082 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002083}
2084
2085static void srp_remove_one(struct ib_device *device)
2086{
Roland Dreierf5358a12006-06-17 20:37:29 -07002087 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002088 struct srp_host *host, *tmp_host;
2089 LIST_HEAD(target_list);
2090 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002091
Roland Dreierf5358a12006-06-17 20:37:29 -07002092 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002093
Roland Dreierf5358a12006-06-17 20:37:29 -07002094 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002095 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002096 /*
2097 * Wait for the sysfs entry to go away, so that no new
2098 * target ports can be created.
2099 */
2100 wait_for_completion(&host->released);
2101
2102 /*
2103 * Mark all target ports as removed, so we stop queueing
2104 * commands and don't try to reconnect.
2105 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002106 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002107 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002108 spin_lock_irq(target->scsi_host->host_lock);
2109 target->state = SRP_TARGET_REMOVED;
2110 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002111 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002112 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002113
2114 /*
2115 * Wait for any reconnection tasks that may have
2116 * started before we marked our target ports as
2117 * removed, and any target port removal tasks.
2118 */
2119 flush_scheduled_work();
2120
2121 list_for_each_entry_safe(target, tmp_target,
2122 &host->target_list, list) {
David Dillowb0e47c82008-01-03 10:25:27 -08002123 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002124 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002125 srp_disconnect_target(target);
2126 ib_destroy_cm_id(target->cm_id);
2127 srp_free_target_ib(target);
2128 scsi_host_put(target->scsi_host);
2129 }
2130
Roland Dreieraef9ec32005-11-02 14:07:13 -08002131 kfree(host);
2132 }
2133
Roland Dreierf5358a12006-06-17 20:37:29 -07002134 if (srp_dev->fmr_pool)
2135 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2136 ib_dereg_mr(srp_dev->mr);
2137 ib_dealloc_pd(srp_dev->pd);
2138
2139 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002140}
2141
FUJITA Tomonori32368222007-06-27 16:33:12 +09002142static struct srp_function_template ib_srp_transport_functions = {
2143};
2144
Roland Dreieraef9ec32005-11-02 14:07:13 -08002145static int __init srp_init_module(void)
2146{
2147 int ret;
2148
David Dillow1e89a192008-04-16 21:01:12 -07002149 if (srp_sg_tablesize > 255) {
2150 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2151 srp_sg_tablesize = 255;
2152 }
2153
FUJITA Tomonori32368222007-06-27 16:33:12 +09002154 ib_srp_transport_template =
2155 srp_attach_transport(&ib_srp_transport_functions);
2156 if (!ib_srp_transport_template)
2157 return -ENOMEM;
2158
Vu Pham74b0a152006-06-17 20:37:32 -07002159 srp_template.sg_tablesize = srp_sg_tablesize;
2160 srp_max_iu_len = (sizeof (struct srp_cmd) +
2161 sizeof (struct srp_indirect_buf) +
2162 srp_sg_tablesize * 16);
2163
Roland Dreieraef9ec32005-11-02 14:07:13 -08002164 ret = class_register(&srp_class);
2165 if (ret) {
2166 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002167 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002168 return ret;
2169 }
2170
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002171 ib_sa_register_client(&srp_sa_client);
2172
Roland Dreieraef9ec32005-11-02 14:07:13 -08002173 ret = ib_register_client(&srp_client);
2174 if (ret) {
2175 printk(KERN_ERR PFX "couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002176 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002177 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002178 class_unregister(&srp_class);
2179 return ret;
2180 }
2181
2182 return 0;
2183}
2184
2185static void __exit srp_cleanup_module(void)
2186{
2187 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002188 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002189 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002190 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002191}
2192
2193module_init(srp_init_module);
2194module_exit(srp_cleanup_module);