blob: 441ea7c2e7c4d077e9ee5aeb1eb5054a79788168 [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 Dreieraef9ec32005-11-02 14:07:13 -0800235 goto out;
236 }
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);
242 ib_destroy_cq(target->recv_cq);
243 goto out;
244 }
245
246 ib_req_notify_cq(target->recv_cq, IB_CQ_NEXT_COMP);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800247
248 init_attr->event_handler = srp_qp_event;
249 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
250 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
251 init_attr->cap.max_recv_sge = 1;
252 init_attr->cap.max_send_sge = 1;
253 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
254 init_attr->qp_type = IB_QPT_RC;
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000255 init_attr->send_cq = target->send_cq;
256 init_attr->recv_cq = target->recv_cq;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800257
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100258 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800259 if (IS_ERR(target->qp)) {
260 ret = PTR_ERR(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000261 ib_destroy_cq(target->send_cq);
262 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800263 goto out;
264 }
265
266 ret = srp_init_qp(target, target->qp);
267 if (ret) {
268 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000269 ib_destroy_cq(target->send_cq);
270 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800271 goto out;
272 }
273
274out:
275 kfree(init_attr);
276 return ret;
277}
278
279static void srp_free_target_ib(struct srp_target_port *target)
280{
281 int i;
282
283 ib_destroy_qp(target->qp);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000284 ib_destroy_cq(target->send_cq);
285 ib_destroy_cq(target->recv_cq);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800286
287 for (i = 0; i < SRP_RQ_SIZE; ++i)
288 srp_free_iu(target->srp_host, target->rx_ring[i]);
289 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
290 srp_free_iu(target->srp_host, target->tx_ring[i]);
291}
292
293static void srp_path_rec_completion(int status,
294 struct ib_sa_path_rec *pathrec,
295 void *target_ptr)
296{
297 struct srp_target_port *target = target_ptr;
298
299 target->status = status;
300 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500301 shost_printk(KERN_ERR, target->scsi_host,
302 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800303 else
304 target->path = *pathrec;
305 complete(&target->done);
306}
307
308static int srp_lookup_path(struct srp_target_port *target)
309{
310 target->path.numb_path = 1;
311
312 init_completion(&target->done);
313
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700314 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100315 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800316 target->srp_host->port,
317 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700318 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800319 IB_SA_PATH_REC_DGID |
320 IB_SA_PATH_REC_SGID |
321 IB_SA_PATH_REC_NUMB_PATH |
322 IB_SA_PATH_REC_PKEY,
323 SRP_PATH_REC_TIMEOUT_MS,
324 GFP_KERNEL,
325 srp_path_rec_completion,
326 target, &target->path_query);
327 if (target->path_query_id < 0)
328 return target->path_query_id;
329
330 wait_for_completion(&target->done);
331
332 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500333 shost_printk(KERN_WARNING, target->scsi_host,
334 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800335
336 return target->status;
337}
338
339static int srp_send_req(struct srp_target_port *target)
340{
341 struct {
342 struct ib_cm_req_param param;
343 struct srp_login_req priv;
344 } *req = NULL;
345 int status;
346
347 req = kzalloc(sizeof *req, GFP_KERNEL);
348 if (!req)
349 return -ENOMEM;
350
351 req->param.primary_path = &target->path;
352 req->param.alternate_path = NULL;
353 req->param.service_id = target->service_id;
354 req->param.qp_num = target->qp->qp_num;
355 req->param.qp_type = target->qp->qp_type;
356 req->param.private_data = &req->priv;
357 req->param.private_data_len = sizeof req->priv;
358 req->param.flow_control = 1;
359
360 get_random_bytes(&req->param.starting_psn, 4);
361 req->param.starting_psn &= 0xffffff;
362
363 /*
364 * Pick some arbitrary defaults here; we could make these
365 * module parameters if anyone cared about setting them.
366 */
367 req->param.responder_resources = 4;
368 req->param.remote_cm_response_timeout = 20;
369 req->param.local_cm_response_timeout = 20;
370 req->param.retry_count = 7;
371 req->param.rnr_retry_count = 7;
372 req->param.max_cm_retries = 15;
373
374 req->priv.opcode = SRP_LOGIN_REQ;
375 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700376 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800377 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
378 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700379 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700380 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700381 * port identifier format is 8 bytes of ID extension followed
382 * by 8 bytes of GUID. Older drafts put the two halves in the
383 * opposite order, so that the GUID comes first.
384 *
385 * Targets conforming to these obsolete drafts can be
386 * recognized by the I/O Class they report.
387 */
388 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
389 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200390 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700391 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200392 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700393 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
394 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
395 } else {
396 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200397 &target->initiator_ext, 8);
398 memcpy(req->priv.initiator_port_id + 8,
399 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700400 memcpy(req->priv.target_port_id, &target->id_ext, 8);
401 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
402 }
403
Roland Dreieraef9ec32005-11-02 14:07:13 -0800404 /*
405 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200406 * zero out the first 8 bytes of our initiator port ID and set
407 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800408 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700409 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500410 shost_printk(KERN_DEBUG, target->scsi_host,
411 PFX "Topspin/Cisco initiator port ID workaround "
412 "activated for target GUID %016llx\n",
413 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800414 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200415 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100416 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800417 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800418
419 status = ib_send_cm_req(target->cm_id, &req->param);
420
421 kfree(req);
422
423 return status;
424}
425
426static void srp_disconnect_target(struct srp_target_port *target)
427{
428 /* XXX should send SRP_I_LOGOUT request */
429
430 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700431 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500432 shost_printk(KERN_DEBUG, target->scsi_host,
433 PFX "Sending CM DREQ failed\n");
Roland Dreiere6581052006-05-17 09:13:21 -0700434 return;
435 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800436 wait_for_completion(&target->done);
437}
438
David Howellsc4028952006-11-22 14:57:56 +0000439static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800440{
David Howellsc4028952006-11-22 14:57:56 +0000441 struct srp_target_port *target =
442 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800443
444 spin_lock_irq(target->scsi_host->host_lock);
445 if (target->state != SRP_TARGET_DEAD) {
446 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800447 return;
448 }
449 target->state = SRP_TARGET_REMOVED;
450 spin_unlock_irq(target->scsi_host->host_lock);
451
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700452 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800453 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700454 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800455
FUJITA Tomonori32368222007-06-27 16:33:12 +0900456 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800457 scsi_remove_host(target->scsi_host);
458 ib_destroy_cm_id(target->cm_id);
459 srp_free_target_ib(target);
460 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800461}
462
463static int srp_connect_target(struct srp_target_port *target)
464{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500465 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800466 int ret;
467
468 ret = srp_lookup_path(target);
469 if (ret)
470 return ret;
471
472 while (1) {
473 init_completion(&target->done);
474 ret = srp_send_req(target);
475 if (ret)
476 return ret;
477 wait_for_completion(&target->done);
478
479 /*
480 * The CM event handling code will set status to
481 * SRP_PORT_REDIRECT if we get a port redirect REJ
482 * back, or SRP_DLID_REDIRECT if we get a lid/qp
483 * redirect REJ back.
484 */
485 switch (target->status) {
486 case 0:
487 return 0;
488
489 case SRP_PORT_REDIRECT:
490 ret = srp_lookup_path(target);
491 if (ret)
492 return ret;
493 break;
494
495 case SRP_DLID_REDIRECT:
496 break;
497
David Dillow9fe4bcf2008-01-08 17:08:52 -0500498 case SRP_STALE_CONN:
499 /* Our current CM id was stale, and is now in timewait.
500 * Try to reconnect with a new one.
501 */
502 if (!retries-- || srp_new_cm_id(target)) {
503 shost_printk(KERN_ERR, target->scsi_host, PFX
504 "giving up on stale connection\n");
505 target->status = -ECONNRESET;
506 return target->status;
507 }
508
509 shost_printk(KERN_ERR, target->scsi_host, PFX
510 "retrying stale connection\n");
511 break;
512
Roland Dreieraef9ec32005-11-02 14:07:13 -0800513 default:
514 return target->status;
515 }
516 }
517}
518
Roland Dreierd945e1d2006-05-09 10:50:28 -0700519static void srp_unmap_data(struct scsi_cmnd *scmnd,
520 struct srp_target_port *target,
521 struct srp_request *req)
522{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900523 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700524 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
525 scmnd->sc_data_direction != DMA_FROM_DEVICE))
526 return;
527
Roland Dreierf5358a12006-06-17 20:37:29 -0700528 if (req->fmr) {
529 ib_fmr_pool_unmap(req->fmr);
530 req->fmr = NULL;
531 }
532
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100533 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900534 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700535}
536
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700537static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
538{
539 srp_unmap_data(req->scmnd, target, req);
540 list_move_tail(&req->list, &target->free_reqs);
541}
542
543static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
544{
545 req->scmnd->result = DID_RESET << 16;
546 req->scmnd->scsi_done(req->scmnd);
547 srp_remove_req(target, req);
548}
549
Roland Dreieraef9ec32005-11-02 14:07:13 -0800550static int srp_reconnect_target(struct srp_target_port *target)
551{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800552 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700553 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800554 struct ib_wc wc;
555 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800556
557 spin_lock_irq(target->scsi_host->host_lock);
558 if (target->state != SRP_TARGET_LIVE) {
559 spin_unlock_irq(target->scsi_host->host_lock);
560 return -EAGAIN;
561 }
562 target->state = SRP_TARGET_CONNECTING;
563 spin_unlock_irq(target->scsi_host->host_lock);
564
565 srp_disconnect_target(target);
566 /*
567 * Now get a new local CM ID so that we avoid confusing the
568 * target in case things are really fouled up.
569 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500570 ret = srp_new_cm_id(target);
571 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800572 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800573
574 qp_attr.qp_state = IB_QPS_RESET;
575 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
576 if (ret)
577 goto err;
578
579 ret = srp_init_qp(target, target->qp);
580 if (ret)
581 goto err;
582
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000583 while (ib_poll_cq(target->recv_cq, 1, &wc) > 0)
584 ; /* nothing */
585 while (ib_poll_cq(target->send_cq, 1, &wc) > 0)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800586 ; /* nothing */
587
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300588 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700589 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
590 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300591 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800592
593 target->rx_head = 0;
594 target->tx_head = 0;
595 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800596
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200597 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800598 ret = srp_connect_target(target);
599 if (ret)
600 goto err;
601
602 spin_lock_irq(target->scsi_host->host_lock);
603 if (target->state == SRP_TARGET_CONNECTING) {
604 ret = 0;
605 target->state = SRP_TARGET_LIVE;
606 } else
607 ret = -EAGAIN;
608 spin_unlock_irq(target->scsi_host->host_lock);
609
610 return ret;
611
612err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500613 shost_printk(KERN_ERR, target->scsi_host,
614 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800615
616 /*
617 * We couldn't reconnect, so kill our target port off.
618 * However, we have to defer the real removal because we might
619 * be in the context of the SCSI error handler now, which
620 * would deadlock if we call scsi_remove_host().
621 */
622 spin_lock_irq(target->scsi_host->host_lock);
623 if (target->state == SRP_TARGET_CONNECTING) {
624 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000625 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800626 schedule_work(&target->work);
627 }
628 spin_unlock_irq(target->scsi_host->host_lock);
629
630 return ret;
631}
632
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700633static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700634 int sg_cnt, struct srp_request *req,
635 struct srp_direct_buf *buf)
636{
637 u64 io_addr = 0;
638 u64 *dma_pages;
639 u32 len;
640 int page_cnt;
641 int i, j;
642 int ret;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100643 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800644 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900645 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700646
647 if (!dev->fmr_pool)
648 return -ENODEV;
649
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700650 if (srp_target_is_mellanox(target) &&
651 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700652 return -EINVAL;
653
Roland Dreierf5358a12006-06-17 20:37:29 -0700654 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900655 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
656 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800657
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900658 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700659 if (i > 0)
660 return -EINVAL;
661 else
662 ++page_cnt;
663 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900664 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700665 ~dev->fmr_page_mask) {
666 if (i < sg_cnt - 1)
667 return -EINVAL;
668 else
669 ++page_cnt;
670 }
671
Ralph Campbell85507bc2006-12-12 14:30:55 -0800672 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700673 }
674
675 page_cnt += len >> dev->fmr_page_shift;
676 if (page_cnt > SRP_FMR_SIZE)
677 return -ENOMEM;
678
679 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
680 if (!dma_pages)
681 return -ENOMEM;
682
683 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900684 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
685 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800686
687 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700688 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900689 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800690 dev->fmr_page_mask) + j;
691 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700692
693 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700694 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700695 if (IS_ERR(req->fmr)) {
696 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700697 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700698 goto out;
699 }
700
Ralph Campbell85507bc2006-12-12 14:30:55 -0800701 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
702 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700703 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
704 buf->len = cpu_to_be32(len);
705
706 ret = 0;
707
708out:
709 kfree(dma_pages);
710
711 return ret;
712}
713
Roland Dreieraef9ec32005-11-02 14:07:13 -0800714static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
715 struct srp_request *req)
716{
Roland Dreiercf368712006-03-24 15:47:26 -0800717 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800718 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800719 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700720 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800721 struct srp_device *dev;
722 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800723
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900724 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800725 return sizeof (struct srp_cmd);
726
727 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
728 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500729 shost_printk(KERN_WARNING, target->scsi_host,
730 PFX "Unhandled data direction %d\n",
731 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800732 return -EINVAL;
733 }
734
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900735 nents = scsi_sg_count(scmnd);
736 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800737
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100738 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800739 ibdev = dev->dev;
740
741 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700742
743 fmt = SRP_DATA_DESC_DIRECT;
744 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800745
746 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700747 /*
748 * The midlayer only generated a single gather/scatter
749 * entry, or DMA mapping coalesced everything to a
750 * single entry. So a direct descriptor along with
751 * the DMA MR suffices.
752 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800753 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800754
Ralph Campbell85507bc2006-12-12 14:30:55 -0800755 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
756 buf->key = cpu_to_be32(dev->mr->rkey);
757 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700758 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700759 (void *) cmd->add_data)) {
760 /*
761 * FMR mapping failed, and the scatterlist has more
762 * than one entry. Generate an indirect memory
763 * descriptor.
764 */
Roland Dreiercf368712006-03-24 15:47:26 -0800765 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900766 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800767 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700768 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800769
770 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700771 len = sizeof (struct srp_cmd) +
772 sizeof (struct srp_indirect_buf) +
773 count * sizeof (struct srp_direct_buf);
774
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900775 scsi_for_each_sg(scmnd, sg, count, i) {
776 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800777
Roland Dreierf5358a12006-06-17 20:37:29 -0700778 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900779 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700780 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800781 cpu_to_be32(dev->mr->rkey);
782 buf->desc_list[i].len = cpu_to_be32(dma_len);
783 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700784 }
Roland Dreiercf368712006-03-24 15:47:26 -0800785
786 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
787 cmd->data_out_desc_cnt = count;
788 else
789 cmd->data_in_desc_cnt = count;
790
Roland Dreierf5358a12006-06-17 20:37:29 -0700791 buf->table_desc.va =
792 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800793 buf->table_desc.key =
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100794 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800795 buf->table_desc.len =
796 cpu_to_be32(count * sizeof (struct srp_direct_buf));
797
Roland Dreiercf368712006-03-24 15:47:26 -0800798 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800799 }
800
801 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
802 cmd->buf_fmt = fmt << 4;
803 else
804 cmd->buf_fmt = fmt;
805
Roland Dreieraef9ec32005-11-02 14:07:13 -0800806 return len;
807}
808
Roland Dreieraef9ec32005-11-02 14:07:13 -0800809static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
810{
811 struct srp_request *req;
812 struct scsi_cmnd *scmnd;
813 unsigned long flags;
814 s32 delta;
815
816 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
817
818 spin_lock_irqsave(target->scsi_host->host_lock, flags);
819
820 target->req_lim += delta;
821
822 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
823
824 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
825 if (be32_to_cpu(rsp->resp_data_len) < 4)
826 req->tsk_status = -1;
827 else
828 req->tsk_status = rsp->data[3];
829 complete(&req->done);
830 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700831 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800832 if (!scmnd)
David Dillow7aa54bd2008-01-07 18:23:41 -0500833 shost_printk(KERN_ERR, target->scsi_host,
834 "Null scmnd for RSP w/tag %016llx\n",
835 (unsigned long long) rsp->tag);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800836 scmnd->result = rsp->status;
837
838 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
839 memcpy(scmnd->sense_buffer, rsp->data +
840 be32_to_cpu(rsp->resp_data_len),
841 min_t(int, be32_to_cpu(rsp->sense_data_len),
842 SCSI_SENSE_BUFFERSIZE));
843 }
844
845 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900846 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800847 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900848 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800849
Roland Dreieraef9ec32005-11-02 14:07:13 -0800850 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800851 scmnd->host_scribble = (void *) -1L;
852 scmnd->scsi_done(scmnd);
853
Roland Dreierd945e1d2006-05-09 10:50:28 -0700854 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800855 } else
856 req->cmd_done = 1;
857 }
858
859 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
860}
861
Roland Dreieraef9ec32005-11-02 14:07:13 -0800862static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
863{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800864 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800865 struct srp_iu *iu;
866 u8 opcode;
867
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000868 iu = target->rx_ring[wc->wr_id];
Roland Dreieraef9ec32005-11-02 14:07:13 -0800869
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100870 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800871 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
872 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800873
874 opcode = *(u8 *) iu->buf;
875
876 if (0) {
877 int i;
878
David Dillow7aa54bd2008-01-07 18:23:41 -0500879 shost_printk(KERN_ERR, target->scsi_host,
880 PFX "recv completion, opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800881
882 for (i = 0; i < wc->byte_len; ++i) {
883 if (i % 8 == 0)
884 printk(KERN_ERR " [%02x] ", i);
885 printk(" %02x", ((u8 *) iu->buf)[i]);
886 if ((i + 1) % 8 == 0)
887 printk("\n");
888 }
889
890 if (wc->byte_len % 8)
891 printk("\n");
892 }
893
894 switch (opcode) {
895 case SRP_RSP:
896 srp_process_rsp(target, iu->buf);
897 break;
898
899 case SRP_T_LOGOUT:
900 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -0500901 shost_printk(KERN_WARNING, target->scsi_host,
902 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800903 break;
904
905 default:
David Dillow7aa54bd2008-01-07 18:23:41 -0500906 shost_printk(KERN_WARNING, target->scsi_host,
907 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800908 break;
909 }
910
Ralph Campbell85507bc2006-12-12 14:30:55 -0800911 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
912 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800913}
914
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000915static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800916{
917 struct srp_target_port *target = target_ptr;
918 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800919
920 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
921 while (ib_poll_cq(cq, 1, &wc) > 0) {
922 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500923 shost_printk(KERN_ERR, target->scsi_host,
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000924 PFX "failed receive status %d\n",
David Dillow7aa54bd2008-01-07 18:23:41 -0500925 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200926 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800927 break;
928 }
929
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000930 srp_handle_recv(target, &wc);
931 }
932}
933
934static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
935{
936 struct srp_target_port *target = target_ptr;
937 struct ib_wc wc;
938
939 while (ib_poll_cq(cq, 1, &wc) > 0) {
940 if (wc.status) {
941 shost_printk(KERN_ERR, target->scsi_host,
942 PFX "failed send status %d\n",
943 wc.status);
944 target->qp_in_error = 1;
945 break;
946 }
947
948 ++target->tx_tail;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800949 }
950}
951
952static int __srp_post_recv(struct srp_target_port *target)
953{
954 struct srp_iu *iu;
955 struct ib_sge list;
956 struct ib_recv_wr wr, *bad_wr;
957 unsigned int next;
958 int ret;
959
960 next = target->rx_head & (SRP_RQ_SIZE - 1);
Bart Van Assche9c03dc92010-02-02 19:23:54 +0000961 wr.wr_id = next;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800962 iu = target->rx_ring[next];
963
964 list.addr = iu->dma;
965 list.length = iu->size;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100966 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800967
968 wr.next = NULL;
969 wr.sg_list = &list;
970 wr.num_sge = 1;
971
972 ret = ib_post_recv(target->qp, &wr, &bad_wr);
973 if (!ret)
974 ++target->rx_head;
975
976 return ret;
977}
978
979static int srp_post_recv(struct srp_target_port *target)
980{
981 unsigned long flags;
982 int ret;
983
984 spin_lock_irqsave(target->scsi_host->host_lock, flags);
985 ret = __srp_post_recv(target);
986 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
987
988 return ret;
989}
990
991/*
992 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800993 * req_lim and tx_head. Lock cannot be dropped between call here and
994 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800995 */
David Dillow8cba2072007-12-19 17:08:43 -0500996static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
997 enum srp_request_type req_type)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800998{
David Dillow8cba2072007-12-19 17:08:43 -0500999 s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
1000
Bart Van Assche9c03dc92010-02-02 19:23:54 +00001001 srp_send_completion(target->send_cq, target);
1002
Roland Dreieraef9ec32005-11-02 14:07:13 -08001003 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
1004 return NULL;
1005
David Dillow8cba2072007-12-19 17:08:43 -05001006 if (target->req_lim < min) {
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001007 ++target->zero_req_lim;
David Dillow8cba2072007-12-19 17:08:43 -05001008 return NULL;
1009 }
Roland Dreier47f2bce2005-11-15 00:19:21 -08001010
Roland Dreieraef9ec32005-11-02 14:07:13 -08001011 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
1012}
1013
1014/*
1015 * Must be called with target->scsi_host->host_lock held to protect
1016 * req_lim and tx_head.
1017 */
1018static int __srp_post_send(struct srp_target_port *target,
1019 struct srp_iu *iu, int len)
1020{
1021 struct ib_sge list;
1022 struct ib_send_wr wr, *bad_wr;
1023 int ret = 0;
1024
Roland Dreieraef9ec32005-11-02 14:07:13 -08001025 list.addr = iu->dma;
1026 list.length = len;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001027 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001028
1029 wr.next = NULL;
1030 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
1031 wr.sg_list = &list;
1032 wr.num_sge = 1;
1033 wr.opcode = IB_WR_SEND;
1034 wr.send_flags = IB_SEND_SIGNALED;
1035
1036 ret = ib_post_send(target->qp, &wr, &bad_wr);
1037
1038 if (!ret) {
1039 ++target->tx_head;
1040 --target->req_lim;
1041 }
1042
1043 return ret;
1044}
1045
1046static int srp_queuecommand(struct scsi_cmnd *scmnd,
1047 void (*done)(struct scsi_cmnd *))
1048{
1049 struct srp_target_port *target = host_to_target(scmnd->device->host);
1050 struct srp_request *req;
1051 struct srp_iu *iu;
1052 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001053 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001054 int len;
1055
1056 if (target->state == SRP_TARGET_CONNECTING)
1057 goto err;
1058
1059 if (target->state == SRP_TARGET_DEAD ||
1060 target->state == SRP_TARGET_REMOVED) {
1061 scmnd->result = DID_BAD_TARGET << 16;
1062 done(scmnd);
1063 return 0;
1064 }
1065
David Dillow8cba2072007-12-19 17:08:43 -05001066 iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001067 if (!iu)
1068 goto err;
1069
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001070 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001071 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1072 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001073
Roland Dreierd945e1d2006-05-09 10:50:28 -07001074 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001075
1076 scmnd->scsi_done = done;
1077 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001078 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001079
1080 cmd = iu->buf;
1081 memset(cmd, 0, sizeof *cmd);
1082
1083 cmd->opcode = SRP_CMD;
1084 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001085 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001086 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1087
Roland Dreieraef9ec32005-11-02 14:07:13 -08001088 req->scmnd = scmnd;
1089 req->cmd = iu;
1090 req->cmd_done = 0;
1091 req->tsk_mgmt = NULL;
1092
1093 len = srp_map_data(scmnd, target, req);
1094 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001095 shost_printk(KERN_ERR, target->scsi_host,
1096 PFX "Failed to map data\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001097 goto err;
1098 }
1099
1100 if (__srp_post_recv(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001101 shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001102 goto err_unmap;
1103 }
1104
Ralph Campbell85507bc2006-12-12 14:30:55 -08001105 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1106 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001107
1108 if (__srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001109 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001110 goto err_unmap;
1111 }
1112
Roland Dreierd945e1d2006-05-09 10:50:28 -07001113 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001114
1115 return 0;
1116
1117err_unmap:
1118 srp_unmap_data(scmnd, target, req);
1119
1120err:
1121 return SCSI_MLQUEUE_HOST_BUSY;
1122}
1123
1124static int srp_alloc_iu_bufs(struct srp_target_port *target)
1125{
1126 int i;
1127
1128 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1129 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1130 target->max_ti_iu_len,
1131 GFP_KERNEL, DMA_FROM_DEVICE);
1132 if (!target->rx_ring[i])
1133 goto err;
1134 }
1135
1136 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1137 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001138 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001139 GFP_KERNEL, DMA_TO_DEVICE);
1140 if (!target->tx_ring[i])
1141 goto err;
1142 }
1143
1144 return 0;
1145
1146err:
1147 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1148 srp_free_iu(target->srp_host, target->rx_ring[i]);
1149 target->rx_ring[i] = NULL;
1150 }
1151
1152 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1153 srp_free_iu(target->srp_host, target->tx_ring[i]);
1154 target->tx_ring[i] = NULL;
1155 }
1156
1157 return -ENOMEM;
1158}
1159
1160static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1161 struct ib_cm_event *event,
1162 struct srp_target_port *target)
1163{
David Dillow7aa54bd2008-01-07 18:23:41 -05001164 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001165 struct ib_class_port_info *cpi;
1166 int opcode;
1167
1168 switch (event->param.rej_rcvd.reason) {
1169 case IB_CM_REJ_PORT_CM_REDIRECT:
1170 cpi = event->param.rej_rcvd.ari;
1171 target->path.dlid = cpi->redirect_lid;
1172 target->path.pkey = cpi->redirect_pkey;
1173 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1174 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1175
1176 target->status = target->path.dlid ?
1177 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1178 break;
1179
1180 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001181 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001182 /*
1183 * Topspin/Cisco SRP gateways incorrectly send
1184 * reject reason code 25 when they mean 24
1185 * (port redirect).
1186 */
1187 memcpy(target->path.dgid.raw,
1188 event->param.rej_rcvd.ari, 16);
1189
David Dillow7aa54bd2008-01-07 18:23:41 -05001190 shost_printk(KERN_DEBUG, shost,
1191 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1192 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1193 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001194
1195 target->status = SRP_PORT_REDIRECT;
1196 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001197 shost_printk(KERN_WARNING, shost,
1198 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001199 target->status = -ECONNRESET;
1200 }
1201 break;
1202
1203 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001204 shost_printk(KERN_WARNING, shost,
1205 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001206 target->status = -ECONNRESET;
1207 break;
1208
1209 case IB_CM_REJ_CONSUMER_DEFINED:
1210 opcode = *(u8 *) event->private_data;
1211 if (opcode == SRP_LOGIN_REJ) {
1212 struct srp_login_rej *rej = event->private_data;
1213 u32 reason = be32_to_cpu(rej->reason);
1214
1215 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001216 shost_printk(KERN_WARNING, shost,
1217 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001218 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001219 shost_printk(KERN_WARNING, shost,
1220 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001221 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001222 shost_printk(KERN_WARNING, shost,
1223 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1224 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001225 target->status = -ECONNRESET;
1226 break;
1227
David Dillow9fe4bcf2008-01-08 17:08:52 -05001228 case IB_CM_REJ_STALE_CONN:
1229 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1230 target->status = SRP_STALE_CONN;
1231 break;
1232
Roland Dreieraef9ec32005-11-02 14:07:13 -08001233 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001234 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1235 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001236 target->status = -ECONNRESET;
1237 }
1238}
1239
1240static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1241{
1242 struct srp_target_port *target = cm_id->context;
1243 struct ib_qp_attr *qp_attr = NULL;
1244 int attr_mask = 0;
1245 int comp = 0;
1246 int opcode = 0;
1247
1248 switch (event->event) {
1249 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001250 shost_printk(KERN_DEBUG, target->scsi_host,
1251 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001252 comp = 1;
1253 target->status = -ECONNRESET;
1254 break;
1255
1256 case IB_CM_REP_RECEIVED:
1257 comp = 1;
1258 opcode = *(u8 *) event->private_data;
1259
1260 if (opcode == SRP_LOGIN_RSP) {
1261 struct srp_login_rsp *rsp = event->private_data;
1262
1263 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1264 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1265
1266 target->scsi_host->can_queue = min(target->req_lim,
1267 target->scsi_host->can_queue);
1268 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001269 shost_printk(KERN_WARNING, target->scsi_host,
1270 PFX "Unhandled RSP opcode %#x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001271 target->status = -ECONNRESET;
1272 break;
1273 }
1274
Vu Phamd2fcea72006-11-21 14:14:10 -08001275 if (!target->rx_ring[0]) {
1276 target->status = srp_alloc_iu_bufs(target);
1277 if (target->status)
1278 break;
1279 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001280
1281 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1282 if (!qp_attr) {
1283 target->status = -ENOMEM;
1284 break;
1285 }
1286
1287 qp_attr->qp_state = IB_QPS_RTR;
1288 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1289 if (target->status)
1290 break;
1291
1292 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1293 if (target->status)
1294 break;
1295
1296 target->status = srp_post_recv(target);
1297 if (target->status)
1298 break;
1299
1300 qp_attr->qp_state = IB_QPS_RTS;
1301 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1302 if (target->status)
1303 break;
1304
1305 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1306 if (target->status)
1307 break;
1308
1309 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1310 if (target->status)
1311 break;
1312
1313 break;
1314
1315 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001316 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001317 comp = 1;
1318
1319 srp_cm_rej_handler(cm_id, event, target);
1320 break;
1321
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001322 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001323 shost_printk(KERN_WARNING, target->scsi_host,
1324 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001325 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001326 shost_printk(KERN_ERR, target->scsi_host,
1327 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001328 break;
1329
1330 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001331 shost_printk(KERN_ERR, target->scsi_host,
1332 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001333
1334 comp = 1;
1335 target->status = 0;
1336 break;
1337
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001338 case IB_CM_MRA_RECEIVED:
1339 case IB_CM_DREQ_ERROR:
1340 case IB_CM_DREP_RECEIVED:
1341 break;
1342
Roland Dreieraef9ec32005-11-02 14:07:13 -08001343 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001344 shost_printk(KERN_WARNING, target->scsi_host,
1345 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001346 break;
1347 }
1348
1349 if (comp)
1350 complete(&target->done);
1351
1352 kfree(qp_attr);
1353
1354 return 0;
1355}
1356
Roland Dreierd945e1d2006-05-09 10:50:28 -07001357static int srp_send_tsk_mgmt(struct srp_target_port *target,
1358 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001359{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001360 struct srp_iu *iu;
1361 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001362
1363 spin_lock_irq(target->scsi_host->host_lock);
1364
Roland Dreier1285b3a2006-03-03 15:47:25 -08001365 if (target->state == SRP_TARGET_DEAD ||
1366 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001367 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001368 goto out;
1369 }
1370
Roland Dreieraef9ec32005-11-02 14:07:13 -08001371 init_completion(&req->done);
1372
David Dillow8cba2072007-12-19 17:08:43 -05001373 iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001374 if (!iu)
1375 goto out;
1376
1377 tsk_mgmt = iu->buf;
1378 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1379
1380 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001381 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1382 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001383 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001384 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001385
1386 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1387 goto out;
1388
1389 req->tsk_mgmt = iu;
1390
1391 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001392
Roland Dreieraef9ec32005-11-02 14:07:13 -08001393 if (!wait_for_completion_timeout(&req->done,
1394 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001395 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001396
Roland Dreierd945e1d2006-05-09 10:50:28 -07001397 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001398
1399out:
1400 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001401 return -1;
1402}
1403
1404static int srp_find_req(struct srp_target_port *target,
1405 struct scsi_cmnd *scmnd,
1406 struct srp_request **req)
1407{
1408 if (scmnd->host_scribble == (void *) -1L)
1409 return -1;
1410
1411 *req = &target->req_ring[(long) scmnd->host_scribble];
1412
1413 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001414}
1415
1416static int srp_abort(struct scsi_cmnd *scmnd)
1417{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001418 struct srp_target_port *target = host_to_target(scmnd->device->host);
1419 struct srp_request *req;
1420 int ret = SUCCESS;
1421
David Dillow7aa54bd2008-01-07 18:23:41 -05001422 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001423
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001424 if (target->qp_in_error)
1425 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001426 if (srp_find_req(target, scmnd, &req))
1427 return FAILED;
1428 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1429 return FAILED;
1430
1431 spin_lock_irq(target->scsi_host->host_lock);
1432
1433 if (req->cmd_done) {
1434 srp_remove_req(target, req);
1435 scmnd->scsi_done(scmnd);
1436 } else if (!req->tsk_status) {
1437 srp_remove_req(target, req);
1438 scmnd->result = DID_ABORT << 16;
1439 } else
1440 ret = FAILED;
1441
1442 spin_unlock_irq(target->scsi_host->host_lock);
1443
1444 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001445}
1446
1447static int srp_reset_device(struct scsi_cmnd *scmnd)
1448{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001449 struct srp_target_port *target = host_to_target(scmnd->device->host);
1450 struct srp_request *req, *tmp;
1451
David Dillow7aa54bd2008-01-07 18:23:41 -05001452 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001453
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001454 if (target->qp_in_error)
1455 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001456 if (srp_find_req(target, scmnd, &req))
1457 return FAILED;
1458 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1459 return FAILED;
1460 if (req->tsk_status)
1461 return FAILED;
1462
1463 spin_lock_irq(target->scsi_host->host_lock);
1464
1465 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001466 if (req->scmnd->device == scmnd->device)
1467 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001468
1469 spin_unlock_irq(target->scsi_host->host_lock);
1470
1471 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001472}
1473
1474static int srp_reset_host(struct scsi_cmnd *scmnd)
1475{
1476 struct srp_target_port *target = host_to_target(scmnd->device->host);
1477 int ret = FAILED;
1478
David Dillow7aa54bd2008-01-07 18:23:41 -05001479 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001480
1481 if (!srp_reconnect_target(target))
1482 ret = SUCCESS;
1483
1484 return ret;
1485}
1486
Tony Jonesee959b02008-02-22 00:13:36 +01001487static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1488 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001489{
Tony Jonesee959b02008-02-22 00:13:36 +01001490 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001491
1492 if (target->state == SRP_TARGET_DEAD ||
1493 target->state == SRP_TARGET_REMOVED)
1494 return -ENODEV;
1495
1496 return sprintf(buf, "0x%016llx\n",
1497 (unsigned long long) be64_to_cpu(target->id_ext));
1498}
1499
Tony Jonesee959b02008-02-22 00:13:36 +01001500static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1501 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001502{
Tony Jonesee959b02008-02-22 00:13:36 +01001503 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001504
1505 if (target->state == SRP_TARGET_DEAD ||
1506 target->state == SRP_TARGET_REMOVED)
1507 return -ENODEV;
1508
1509 return sprintf(buf, "0x%016llx\n",
1510 (unsigned long long) be64_to_cpu(target->ioc_guid));
1511}
1512
Tony Jonesee959b02008-02-22 00:13:36 +01001513static ssize_t show_service_id(struct device *dev,
1514 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001515{
Tony Jonesee959b02008-02-22 00:13:36 +01001516 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001517
1518 if (target->state == SRP_TARGET_DEAD ||
1519 target->state == SRP_TARGET_REMOVED)
1520 return -ENODEV;
1521
1522 return sprintf(buf, "0x%016llx\n",
1523 (unsigned long long) be64_to_cpu(target->service_id));
1524}
1525
Tony Jonesee959b02008-02-22 00:13:36 +01001526static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1527 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001528{
Tony Jonesee959b02008-02-22 00:13:36 +01001529 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001530
1531 if (target->state == SRP_TARGET_DEAD ||
1532 target->state == SRP_TARGET_REMOVED)
1533 return -ENODEV;
1534
1535 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1536}
1537
Tony Jonesee959b02008-02-22 00:13:36 +01001538static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1539 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001540{
Tony Jonesee959b02008-02-22 00:13:36 +01001541 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001542
1543 if (target->state == SRP_TARGET_DEAD ||
1544 target->state == SRP_TARGET_REMOVED)
1545 return -ENODEV;
1546
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001547 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001548}
1549
Tony Jonesee959b02008-02-22 00:13:36 +01001550static ssize_t show_orig_dgid(struct device *dev,
1551 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001552{
Tony Jonesee959b02008-02-22 00:13:36 +01001553 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001554
1555 if (target->state == SRP_TARGET_DEAD ||
1556 target->state == SRP_TARGET_REMOVED)
1557 return -ENODEV;
1558
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001559 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001560}
1561
Tony Jonesee959b02008-02-22 00:13:36 +01001562static ssize_t show_zero_req_lim(struct device *dev,
1563 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001564{
Tony Jonesee959b02008-02-22 00:13:36 +01001565 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001566
1567 if (target->state == SRP_TARGET_DEAD ||
1568 target->state == SRP_TARGET_REMOVED)
1569 return -ENODEV;
1570
1571 return sprintf(buf, "%d\n", target->zero_req_lim);
1572}
1573
Tony Jonesee959b02008-02-22 00:13:36 +01001574static ssize_t show_local_ib_port(struct device *dev,
1575 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001576{
Tony Jonesee959b02008-02-22 00:13:36 +01001577 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001578
1579 return sprintf(buf, "%d\n", target->srp_host->port);
1580}
1581
Tony Jonesee959b02008-02-22 00:13:36 +01001582static ssize_t show_local_ib_device(struct device *dev,
1583 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001584{
Tony Jonesee959b02008-02-22 00:13:36 +01001585 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001586
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001587 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001588}
1589
Tony Jonesee959b02008-02-22 00:13:36 +01001590static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1591static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1592static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1593static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1594static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1595static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1596static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1597static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1598static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001599
Tony Jonesee959b02008-02-22 00:13:36 +01001600static struct device_attribute *srp_host_attrs[] = {
1601 &dev_attr_id_ext,
1602 &dev_attr_ioc_guid,
1603 &dev_attr_service_id,
1604 &dev_attr_pkey,
1605 &dev_attr_dgid,
1606 &dev_attr_orig_dgid,
1607 &dev_attr_zero_req_lim,
1608 &dev_attr_local_ib_port,
1609 &dev_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001610 NULL
1611};
1612
Roland Dreieraef9ec32005-11-02 14:07:13 -08001613static struct scsi_host_template srp_template = {
1614 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001615 .name = "InfiniBand SRP initiator",
1616 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001617 .info = srp_target_info,
1618 .queuecommand = srp_queuecommand,
1619 .eh_abort_handler = srp_abort,
1620 .eh_device_reset_handler = srp_reset_device,
1621 .eh_host_reset_handler = srp_reset_host,
1622 .can_queue = SRP_SQ_SIZE,
1623 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001624 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001625 .use_clustering = ENABLE_CLUSTERING,
1626 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001627};
1628
1629static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1630{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001631 struct srp_rport_identifiers ids;
1632 struct srp_rport *rport;
1633
Roland Dreieraef9ec32005-11-02 14:07:13 -08001634 sprintf(target->target_name, "SRP.T10:%016llX",
1635 (unsigned long long) be64_to_cpu(target->id_ext));
1636
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001637 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001638 return -ENODEV;
1639
FUJITA Tomonori32368222007-06-27 16:33:12 +09001640 memcpy(ids.port_id, &target->id_ext, 8);
1641 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001642 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001643 rport = srp_rport_add(target->scsi_host, &ids);
1644 if (IS_ERR(rport)) {
1645 scsi_remove_host(target->scsi_host);
1646 return PTR_ERR(rport);
1647 }
1648
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001649 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001650 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001651 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001652
1653 target->state = SRP_TARGET_LIVE;
1654
Roland Dreieraef9ec32005-11-02 14:07:13 -08001655 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001656 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001657
1658 return 0;
1659}
1660
Tony Jonesee959b02008-02-22 00:13:36 +01001661static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001662{
1663 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001664 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001665
1666 complete(&host->released);
1667}
1668
1669static struct class srp_class = {
1670 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01001671 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08001672};
1673
1674/*
1675 * Target ports are added by writing
1676 *
1677 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1678 * pkey=<P_Key>,service_id=<service ID>
1679 *
1680 * to the add_target sysfs attribute.
1681 */
1682enum {
1683 SRP_OPT_ERR = 0,
1684 SRP_OPT_ID_EXT = 1 << 0,
1685 SRP_OPT_IOC_GUID = 1 << 1,
1686 SRP_OPT_DGID = 1 << 2,
1687 SRP_OPT_PKEY = 1 << 3,
1688 SRP_OPT_SERVICE_ID = 1 << 4,
1689 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001690 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001691 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001692 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001693 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1694 SRP_OPT_IOC_GUID |
1695 SRP_OPT_DGID |
1696 SRP_OPT_PKEY |
1697 SRP_OPT_SERVICE_ID),
1698};
1699
Steven Whitehousea447c092008-10-13 10:46:57 +01001700static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001701 { SRP_OPT_ID_EXT, "id_ext=%s" },
1702 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1703 { SRP_OPT_DGID, "dgid=%s" },
1704 { SRP_OPT_PKEY, "pkey=%x" },
1705 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1706 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1707 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001708 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001709 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001710 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001711};
1712
1713static int srp_parse_options(const char *buf, struct srp_target_port *target)
1714{
1715 char *options, *sep_opt;
1716 char *p;
1717 char dgid[3];
1718 substring_t args[MAX_OPT_ARGS];
1719 int opt_mask = 0;
1720 int token;
1721 int ret = -EINVAL;
1722 int i;
1723
1724 options = kstrdup(buf, GFP_KERNEL);
1725 if (!options)
1726 return -ENOMEM;
1727
1728 sep_opt = options;
1729 while ((p = strsep(&sep_opt, ",")) != NULL) {
1730 if (!*p)
1731 continue;
1732
1733 token = match_token(p, srp_opt_tokens, args);
1734 opt_mask |= token;
1735
1736 switch (token) {
1737 case SRP_OPT_ID_EXT:
1738 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001739 if (!p) {
1740 ret = -ENOMEM;
1741 goto out;
1742 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001743 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1744 kfree(p);
1745 break;
1746
1747 case SRP_OPT_IOC_GUID:
1748 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001749 if (!p) {
1750 ret = -ENOMEM;
1751 goto out;
1752 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001753 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1754 kfree(p);
1755 break;
1756
1757 case SRP_OPT_DGID:
1758 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001759 if (!p) {
1760 ret = -ENOMEM;
1761 goto out;
1762 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001763 if (strlen(p) != 32) {
1764 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001765 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001766 goto out;
1767 }
1768
1769 for (i = 0; i < 16; ++i) {
1770 strlcpy(dgid, p + i * 2, 3);
1771 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1772 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001773 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001774 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001775 break;
1776
1777 case SRP_OPT_PKEY:
1778 if (match_hex(args, &token)) {
1779 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1780 goto out;
1781 }
1782 target->path.pkey = cpu_to_be16(token);
1783 break;
1784
1785 case SRP_OPT_SERVICE_ID:
1786 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001787 if (!p) {
1788 ret = -ENOMEM;
1789 goto out;
1790 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001791 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07001792 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001793 kfree(p);
1794 break;
1795
1796 case SRP_OPT_MAX_SECT:
1797 if (match_int(args, &token)) {
1798 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1799 goto out;
1800 }
1801 target->scsi_host->max_sectors = token;
1802 break;
1803
Vu Pham52fb2b502006-06-17 20:37:31 -07001804 case SRP_OPT_MAX_CMD_PER_LUN:
1805 if (match_int(args, &token)) {
1806 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1807 goto out;
1808 }
1809 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1810 break;
1811
Ramachandra K0c0450db2006-06-17 20:37:38 -07001812 case SRP_OPT_IO_CLASS:
1813 if (match_hex(args, &token)) {
1814 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1815 goto out;
1816 }
1817 if (token != SRP_REV10_IB_IO_CLASS &&
1818 token != SRP_REV16A_IB_IO_CLASS) {
1819 printk(KERN_WARNING PFX "unknown IO class parameter value"
1820 " %x specified (use %x or %x).\n",
1821 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1822 goto out;
1823 }
1824 target->io_class = token;
1825 break;
1826
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001827 case SRP_OPT_INITIATOR_EXT:
1828 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001829 if (!p) {
1830 ret = -ENOMEM;
1831 goto out;
1832 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001833 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1834 kfree(p);
1835 break;
1836
Roland Dreieraef9ec32005-11-02 14:07:13 -08001837 default:
1838 printk(KERN_WARNING PFX "unknown parameter or missing value "
1839 "'%s' in target creation request\n", p);
1840 goto out;
1841 }
1842 }
1843
1844 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1845 ret = 0;
1846 else
1847 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1848 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1849 !(srp_opt_tokens[i].token & opt_mask))
1850 printk(KERN_WARNING PFX "target creation request is "
1851 "missing parameter '%s'\n",
1852 srp_opt_tokens[i].pattern);
1853
1854out:
1855 kfree(options);
1856 return ret;
1857}
1858
Tony Jonesee959b02008-02-22 00:13:36 +01001859static ssize_t srp_create_target(struct device *dev,
1860 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001861 const char *buf, size_t count)
1862{
1863 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001864 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001865 struct Scsi_Host *target_host;
1866 struct srp_target_port *target;
1867 int ret;
1868 int i;
1869
1870 target_host = scsi_host_alloc(&srp_template,
1871 sizeof (struct srp_target_port));
1872 if (!target_host)
1873 return -ENOMEM;
1874
FUJITA Tomonori32368222007-06-27 16:33:12 +09001875 target_host->transportt = ib_srp_transport_template;
Arne Redlich3c8edf02006-11-15 12:43:00 +01001876 target_host->max_lun = SRP_MAX_LUN;
1877 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001878
Roland Dreieraef9ec32005-11-02 14:07:13 -08001879 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001880
Ramachandra K0c0450db2006-06-17 20:37:38 -07001881 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001882 target->scsi_host = target_host;
1883 target->srp_host = host;
1884
Roland Dreierd945e1d2006-05-09 10:50:28 -07001885 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001886 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001887 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1888 target->req_ring[i].index = i;
1889 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1890 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001891
1892 ret = srp_parse_options(buf, target);
1893 if (ret)
1894 goto err;
1895
Roland Dreier969a60f2008-07-14 23:48:43 -07001896 ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001897
David Dillow7aa54bd2008-01-07 18:23:41 -05001898 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1899 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001900 "service_id %016llx dgid %pI6\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08001901 (unsigned long long) be64_to_cpu(target->id_ext),
1902 (unsigned long long) be64_to_cpu(target->ioc_guid),
1903 be16_to_cpu(target->path.pkey),
1904 (unsigned long long) be64_to_cpu(target->service_id),
Harvey Harrison8867cd72008-10-28 22:36:33 -07001905 target->path.dgid.raw);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001906
1907 ret = srp_create_target_ib(target);
1908 if (ret)
1909 goto err;
1910
David Dillow9fe4bcf2008-01-08 17:08:52 -05001911 ret = srp_new_cm_id(target);
1912 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001913 goto err_free;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001914
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001915 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001916 ret = srp_connect_target(target);
1917 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001918 shost_printk(KERN_ERR, target->scsi_host,
1919 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001920 goto err_cm_id;
1921 }
1922
1923 ret = srp_add_target(host, target);
1924 if (ret)
1925 goto err_disconnect;
1926
1927 return count;
1928
1929err_disconnect:
1930 srp_disconnect_target(target);
1931
1932err_cm_id:
1933 ib_destroy_cm_id(target->cm_id);
1934
1935err_free:
1936 srp_free_target_ib(target);
1937
1938err:
1939 scsi_host_put(target_host);
1940
1941 return ret;
1942}
1943
Tony Jonesee959b02008-02-22 00:13:36 +01001944static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001945
Tony Jonesee959b02008-02-22 00:13:36 +01001946static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1947 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001948{
Tony Jonesee959b02008-02-22 00:13:36 +01001949 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001950
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001951 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001952}
1953
Tony Jonesee959b02008-02-22 00:13:36 +01001954static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001955
Tony Jonesee959b02008-02-22 00:13:36 +01001956static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1957 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001958{
Tony Jonesee959b02008-02-22 00:13:36 +01001959 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001960
1961 return sprintf(buf, "%d\n", host->port);
1962}
1963
Tony Jonesee959b02008-02-22 00:13:36 +01001964static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001965
Roland Dreierf5358a12006-06-17 20:37:29 -07001966static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001967{
1968 struct srp_host *host;
1969
1970 host = kzalloc(sizeof *host, GFP_KERNEL);
1971 if (!host)
1972 return NULL;
1973
1974 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001975 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001976 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001977 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001978 host->port = port;
1979
Tony Jonesee959b02008-02-22 00:13:36 +01001980 host->dev.class = &srp_class;
1981 host->dev.parent = device->dev->dma_device;
Kay Sieversd927e382009-01-06 10:44:39 -08001982 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001983
Tony Jonesee959b02008-02-22 00:13:36 +01001984 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001985 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01001986 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001987 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001988 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001989 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001990 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001991 goto err_class;
1992
1993 return host;
1994
1995err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01001996 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001997
Roland Dreierf5358a12006-06-17 20:37:29 -07001998free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001999 kfree(host);
2000
2001 return NULL;
2002}
2003
2004static void srp_add_one(struct ib_device *device)
2005{
Roland Dreierf5358a12006-06-17 20:37:29 -07002006 struct srp_device *srp_dev;
2007 struct ib_device_attr *dev_attr;
2008 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002009 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002010 int s, e, p;
2011
Roland Dreierf5358a12006-06-17 20:37:29 -07002012 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2013 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002014 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002015
Roland Dreierf5358a12006-06-17 20:37:29 -07002016 if (ib_query_device(device, dev_attr)) {
2017 printk(KERN_WARNING PFX "Query device failed for %s\n",
2018 device->name);
2019 goto free_attr;
2020 }
2021
2022 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2023 if (!srp_dev)
2024 goto free_attr;
2025
2026 /*
2027 * Use the smallest page size supported by the HCA, down to a
2028 * minimum of 512 bytes (which is the smallest sector that a
2029 * SCSI command will ever carry).
2030 */
2031 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2032 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08002033 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07002034
2035 INIT_LIST_HEAD(&srp_dev->dev_list);
2036
2037 srp_dev->dev = device;
2038 srp_dev->pd = ib_alloc_pd(device);
2039 if (IS_ERR(srp_dev->pd))
2040 goto free_dev;
2041
2042 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2043 IB_ACCESS_LOCAL_WRITE |
2044 IB_ACCESS_REMOTE_READ |
2045 IB_ACCESS_REMOTE_WRITE);
2046 if (IS_ERR(srp_dev->mr))
2047 goto err_pd;
2048
2049 memset(&fmr_param, 0, sizeof fmr_param);
2050 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2051 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2052 fmr_param.cache = 1;
2053 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2054 fmr_param.page_shift = srp_dev->fmr_page_shift;
2055 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2056 IB_ACCESS_REMOTE_WRITE |
2057 IB_ACCESS_REMOTE_READ);
2058
2059 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2060 if (IS_ERR(srp_dev->fmr_pool))
2061 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002062
Tom Tucker07ebafb2006-08-03 16:02:42 -05002063 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002064 s = 0;
2065 e = 0;
2066 } else {
2067 s = 1;
2068 e = device->phys_port_cnt;
2069 }
2070
2071 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002072 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002073 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002074 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002075 }
2076
Roland Dreierf5358a12006-06-17 20:37:29 -07002077 ib_set_client_data(device, &srp_client, srp_dev);
2078
2079 goto free_attr;
2080
2081err_pd:
2082 ib_dealloc_pd(srp_dev->pd);
2083
2084free_dev:
2085 kfree(srp_dev);
2086
2087free_attr:
2088 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002089}
2090
2091static void srp_remove_one(struct ib_device *device)
2092{
Roland Dreierf5358a12006-06-17 20:37:29 -07002093 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002094 struct srp_host *host, *tmp_host;
2095 LIST_HEAD(target_list);
2096 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002097
Roland Dreierf5358a12006-06-17 20:37:29 -07002098 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002099
Roland Dreierf5358a12006-06-17 20:37:29 -07002100 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002101 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002102 /*
2103 * Wait for the sysfs entry to go away, so that no new
2104 * target ports can be created.
2105 */
2106 wait_for_completion(&host->released);
2107
2108 /*
2109 * Mark all target ports as removed, so we stop queueing
2110 * commands and don't try to reconnect.
2111 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002112 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002113 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002114 spin_lock_irq(target->scsi_host->host_lock);
2115 target->state = SRP_TARGET_REMOVED;
2116 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002117 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002118 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002119
2120 /*
2121 * Wait for any reconnection tasks that may have
2122 * started before we marked our target ports as
2123 * removed, and any target port removal tasks.
2124 */
2125 flush_scheduled_work();
2126
2127 list_for_each_entry_safe(target, tmp_target,
2128 &host->target_list, list) {
David Dillowb0e47c82008-01-03 10:25:27 -08002129 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002130 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002131 srp_disconnect_target(target);
2132 ib_destroy_cm_id(target->cm_id);
2133 srp_free_target_ib(target);
2134 scsi_host_put(target->scsi_host);
2135 }
2136
Roland Dreieraef9ec32005-11-02 14:07:13 -08002137 kfree(host);
2138 }
2139
Roland Dreierf5358a12006-06-17 20:37:29 -07002140 if (srp_dev->fmr_pool)
2141 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2142 ib_dereg_mr(srp_dev->mr);
2143 ib_dealloc_pd(srp_dev->pd);
2144
2145 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002146}
2147
FUJITA Tomonori32368222007-06-27 16:33:12 +09002148static struct srp_function_template ib_srp_transport_functions = {
2149};
2150
Roland Dreieraef9ec32005-11-02 14:07:13 -08002151static int __init srp_init_module(void)
2152{
2153 int ret;
2154
David Dillow1e89a192008-04-16 21:01:12 -07002155 if (srp_sg_tablesize > 255) {
2156 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2157 srp_sg_tablesize = 255;
2158 }
2159
FUJITA Tomonori32368222007-06-27 16:33:12 +09002160 ib_srp_transport_template =
2161 srp_attach_transport(&ib_srp_transport_functions);
2162 if (!ib_srp_transport_template)
2163 return -ENOMEM;
2164
Vu Pham74b0a152006-06-17 20:37:32 -07002165 srp_template.sg_tablesize = srp_sg_tablesize;
2166 srp_max_iu_len = (sizeof (struct srp_cmd) +
2167 sizeof (struct srp_indirect_buf) +
2168 srp_sg_tablesize * 16);
2169
Roland Dreieraef9ec32005-11-02 14:07:13 -08002170 ret = class_register(&srp_class);
2171 if (ret) {
2172 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002173 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002174 return ret;
2175 }
2176
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002177 ib_sa_register_client(&srp_sa_client);
2178
Roland Dreieraef9ec32005-11-02 14:07:13 -08002179 ret = ib_register_client(&srp_client);
2180 if (ret) {
2181 printk(KERN_ERR PFX "couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002182 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002183 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002184 class_unregister(&srp_class);
2185 return ret;
2186 }
2187
2188 return 0;
2189}
2190
2191static void __exit srp_cleanup_module(void)
2192{
2193 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002194 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002195 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002196 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002197}
2198
2199module_init(srp_init_module);
2200module_exit(srp_cleanup_module);