blob: 99a110660040961efdb08f366db0cac094653481 [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.
31 *
32 * $Id: ib_srp.c 3932 2005-11-01 17:19:29Z roland $
33 */
34
Roland Dreieraef9ec32005-11-02 14:07:13 -080035#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/err.h>
39#include <linux/string.h>
40#include <linux/parser.h>
41#include <linux/random.h>
Tim Schmielaude259682006-01-08 01:02:05 -080042#include <linux/jiffies.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080043
44#include <asm/atomic.h>
45
46#include <scsi/scsi.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_dbg.h>
49#include <scsi/srp.h>
FUJITA Tomonori32368222007-06-27 16:33:12 +090050#include <scsi/scsi_transport_srp.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080051
52#include <rdma/ib_cache.h>
53
54#include "ib_srp.h"
55
56#define DRV_NAME "ib_srp"
57#define PFX DRV_NAME ": "
58#define DRV_VERSION "0.2"
59#define DRV_RELDATE "November 1, 2005"
60
61MODULE_AUTHOR("Roland Dreier");
62MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
63 "v" DRV_VERSION " (" DRV_RELDATE ")");
64MODULE_LICENSE("Dual BSD/GPL");
65
Vu Pham74b0a152006-06-17 20:37:32 -070066static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
67static int srp_max_iu_len;
68
69module_param(srp_sg_tablesize, int, 0444);
70MODULE_PARM_DESC(srp_sg_tablesize,
David Dillow1e89a192008-04-16 21:01:12 -070071 "Max number of gather/scatter entries per I/O (default is 12, max 255)");
Vu Pham74b0a152006-06-17 20:37:32 -070072
Roland Dreieraef9ec32005-11-02 14:07:13 -080073static int topspin_workarounds = 1;
74
75module_param(topspin_workarounds, int, 0444);
76MODULE_PARM_DESC(topspin_workarounds,
77 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
78
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070079static int mellanox_workarounds = 1;
80
81module_param(mellanox_workarounds, int, 0444);
82MODULE_PARM_DESC(mellanox_workarounds,
83 "Enable workarounds for Mellanox SRP target bugs if != 0");
84
Roland Dreieraef9ec32005-11-02 14:07:13 -080085static void srp_add_one(struct ib_device *device);
86static void srp_remove_one(struct ib_device *device);
87static void srp_completion(struct ib_cq *cq, void *target_ptr);
88static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
89
FUJITA Tomonori32368222007-06-27 16:33:12 +090090static struct scsi_transport_template *ib_srp_transport_template;
91
Roland Dreieraef9ec32005-11-02 14:07:13 -080092static struct ib_client srp_client = {
93 .name = "srp",
94 .add = srp_add_one,
95 .remove = srp_remove_one
96};
97
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070098static struct ib_sa_client srp_sa_client;
99
Roland Dreieraef9ec32005-11-02 14:07:13 -0800100static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
101{
102 return (struct srp_target_port *) host->hostdata;
103}
104
105static const char *srp_target_info(struct Scsi_Host *host)
106{
107 return host_to_target(host)->target_name;
108}
109
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700110static int srp_target_is_topspin(struct srp_target_port *target)
111{
112 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700113 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700114
115 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700116 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
117 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700118}
119
120static int srp_target_is_mellanox(struct srp_target_port *target)
121{
122 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
123
124 return mellanox_workarounds &&
125 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
126}
127
Roland Dreieraef9ec32005-11-02 14:07:13 -0800128static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
129 gfp_t gfp_mask,
130 enum dma_data_direction direction)
131{
132 struct srp_iu *iu;
133
134 iu = kmalloc(sizeof *iu, gfp_mask);
135 if (!iu)
136 goto out;
137
138 iu->buf = kzalloc(size, gfp_mask);
139 if (!iu->buf)
140 goto out_free_iu;
141
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100142 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
143 direction);
144 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800145 goto out_free_buf;
146
147 iu->size = size;
148 iu->direction = direction;
149
150 return iu;
151
152out_free_buf:
153 kfree(iu->buf);
154out_free_iu:
155 kfree(iu);
156out:
157 return NULL;
158}
159
160static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
161{
162 if (!iu)
163 return;
164
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100165 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
166 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800167 kfree(iu->buf);
168 kfree(iu);
169}
170
171static void srp_qp_event(struct ib_event *event, void *context)
172{
173 printk(KERN_ERR PFX "QP event %d\n", event->event);
174}
175
176static int srp_init_qp(struct srp_target_port *target,
177 struct ib_qp *qp)
178{
179 struct ib_qp_attr *attr;
180 int ret;
181
182 attr = kmalloc(sizeof *attr, GFP_KERNEL);
183 if (!attr)
184 return -ENOMEM;
185
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100186 ret = ib_find_cached_pkey(target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800187 target->srp_host->port,
188 be16_to_cpu(target->path.pkey),
189 &attr->pkey_index);
190 if (ret)
191 goto out;
192
193 attr->qp_state = IB_QPS_INIT;
194 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
195 IB_ACCESS_REMOTE_WRITE);
196 attr->port_num = target->srp_host->port;
197
198 ret = ib_modify_qp(qp, attr,
199 IB_QP_STATE |
200 IB_QP_PKEY_INDEX |
201 IB_QP_ACCESS_FLAGS |
202 IB_QP_PORT);
203
204out:
205 kfree(attr);
206 return ret;
207}
208
David Dillow9fe4bcf2008-01-08 17:08:52 -0500209static int srp_new_cm_id(struct srp_target_port *target)
210{
211 struct ib_cm_id *new_cm_id;
212
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100213 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500214 srp_cm_handler, target);
215 if (IS_ERR(new_cm_id))
216 return PTR_ERR(new_cm_id);
217
218 if (target->cm_id)
219 ib_destroy_cm_id(target->cm_id);
220 target->cm_id = new_cm_id;
221
222 return 0;
223}
224
Roland Dreieraef9ec32005-11-02 14:07:13 -0800225static int srp_create_target_ib(struct srp_target_port *target)
226{
227 struct ib_qp_init_attr *init_attr;
228 int ret;
229
230 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
231 if (!init_attr)
232 return -ENOMEM;
233
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100234 target->cq = ib_create_cq(target->srp_host->srp_dev->dev,
235 srp_completion, NULL, target, SRP_CQ_SIZE, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800236 if (IS_ERR(target->cq)) {
237 ret = PTR_ERR(target->cq);
238 goto out;
239 }
240
241 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
242
243 init_attr->event_handler = srp_qp_event;
244 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
245 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
246 init_attr->cap.max_recv_sge = 1;
247 init_attr->cap.max_send_sge = 1;
248 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
249 init_attr->qp_type = IB_QPT_RC;
250 init_attr->send_cq = target->cq;
251 init_attr->recv_cq = target->cq;
252
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100253 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800254 if (IS_ERR(target->qp)) {
255 ret = PTR_ERR(target->qp);
256 ib_destroy_cq(target->cq);
257 goto out;
258 }
259
260 ret = srp_init_qp(target, target->qp);
261 if (ret) {
262 ib_destroy_qp(target->qp);
263 ib_destroy_cq(target->cq);
264 goto out;
265 }
266
267out:
268 kfree(init_attr);
269 return ret;
270}
271
272static void srp_free_target_ib(struct srp_target_port *target)
273{
274 int i;
275
276 ib_destroy_qp(target->qp);
277 ib_destroy_cq(target->cq);
278
279 for (i = 0; i < SRP_RQ_SIZE; ++i)
280 srp_free_iu(target->srp_host, target->rx_ring[i]);
281 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
282 srp_free_iu(target->srp_host, target->tx_ring[i]);
283}
284
285static void srp_path_rec_completion(int status,
286 struct ib_sa_path_rec *pathrec,
287 void *target_ptr)
288{
289 struct srp_target_port *target = target_ptr;
290
291 target->status = status;
292 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500293 shost_printk(KERN_ERR, target->scsi_host,
294 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800295 else
296 target->path = *pathrec;
297 complete(&target->done);
298}
299
300static int srp_lookup_path(struct srp_target_port *target)
301{
302 target->path.numb_path = 1;
303
304 init_completion(&target->done);
305
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700306 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100307 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800308 target->srp_host->port,
309 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700310 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800311 IB_SA_PATH_REC_DGID |
312 IB_SA_PATH_REC_SGID |
313 IB_SA_PATH_REC_NUMB_PATH |
314 IB_SA_PATH_REC_PKEY,
315 SRP_PATH_REC_TIMEOUT_MS,
316 GFP_KERNEL,
317 srp_path_rec_completion,
318 target, &target->path_query);
319 if (target->path_query_id < 0)
320 return target->path_query_id;
321
322 wait_for_completion(&target->done);
323
324 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500325 shost_printk(KERN_WARNING, target->scsi_host,
326 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800327
328 return target->status;
329}
330
331static int srp_send_req(struct srp_target_port *target)
332{
333 struct {
334 struct ib_cm_req_param param;
335 struct srp_login_req priv;
336 } *req = NULL;
337 int status;
338
339 req = kzalloc(sizeof *req, GFP_KERNEL);
340 if (!req)
341 return -ENOMEM;
342
343 req->param.primary_path = &target->path;
344 req->param.alternate_path = NULL;
345 req->param.service_id = target->service_id;
346 req->param.qp_num = target->qp->qp_num;
347 req->param.qp_type = target->qp->qp_type;
348 req->param.private_data = &req->priv;
349 req->param.private_data_len = sizeof req->priv;
350 req->param.flow_control = 1;
351
352 get_random_bytes(&req->param.starting_psn, 4);
353 req->param.starting_psn &= 0xffffff;
354
355 /*
356 * Pick some arbitrary defaults here; we could make these
357 * module parameters if anyone cared about setting them.
358 */
359 req->param.responder_resources = 4;
360 req->param.remote_cm_response_timeout = 20;
361 req->param.local_cm_response_timeout = 20;
362 req->param.retry_count = 7;
363 req->param.rnr_retry_count = 7;
364 req->param.max_cm_retries = 15;
365
366 req->priv.opcode = SRP_LOGIN_REQ;
367 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700368 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800369 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
370 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700371 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700372 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700373 * port identifier format is 8 bytes of ID extension followed
374 * by 8 bytes of GUID. Older drafts put the two halves in the
375 * opposite order, so that the GUID comes first.
376 *
377 * Targets conforming to these obsolete drafts can be
378 * recognized by the I/O Class they report.
379 */
380 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
381 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200382 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700383 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200384 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700385 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
386 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
387 } else {
388 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200389 &target->initiator_ext, 8);
390 memcpy(req->priv.initiator_port_id + 8,
391 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700392 memcpy(req->priv.target_port_id, &target->id_ext, 8);
393 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
394 }
395
Roland Dreieraef9ec32005-11-02 14:07:13 -0800396 /*
397 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200398 * zero out the first 8 bytes of our initiator port ID and set
399 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800400 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700401 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500402 shost_printk(KERN_DEBUG, target->scsi_host,
403 PFX "Topspin/Cisco initiator port ID workaround "
404 "activated for target GUID %016llx\n",
405 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800406 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200407 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100408 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800409 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800410
411 status = ib_send_cm_req(target->cm_id, &req->param);
412
413 kfree(req);
414
415 return status;
416}
417
418static void srp_disconnect_target(struct srp_target_port *target)
419{
420 /* XXX should send SRP_I_LOGOUT request */
421
422 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700423 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500424 shost_printk(KERN_DEBUG, target->scsi_host,
425 PFX "Sending CM DREQ failed\n");
Roland Dreiere6581052006-05-17 09:13:21 -0700426 return;
427 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800428 wait_for_completion(&target->done);
429}
430
David Howellsc4028952006-11-22 14:57:56 +0000431static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800432{
David Howellsc4028952006-11-22 14:57:56 +0000433 struct srp_target_port *target =
434 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800435
436 spin_lock_irq(target->scsi_host->host_lock);
437 if (target->state != SRP_TARGET_DEAD) {
438 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800439 return;
440 }
441 target->state = SRP_TARGET_REMOVED;
442 spin_unlock_irq(target->scsi_host->host_lock);
443
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700444 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800445 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700446 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800447
FUJITA Tomonori32368222007-06-27 16:33:12 +0900448 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800449 scsi_remove_host(target->scsi_host);
450 ib_destroy_cm_id(target->cm_id);
451 srp_free_target_ib(target);
452 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800453}
454
455static int srp_connect_target(struct srp_target_port *target)
456{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500457 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800458 int ret;
459
460 ret = srp_lookup_path(target);
461 if (ret)
462 return ret;
463
464 while (1) {
465 init_completion(&target->done);
466 ret = srp_send_req(target);
467 if (ret)
468 return ret;
469 wait_for_completion(&target->done);
470
471 /*
472 * The CM event handling code will set status to
473 * SRP_PORT_REDIRECT if we get a port redirect REJ
474 * back, or SRP_DLID_REDIRECT if we get a lid/qp
475 * redirect REJ back.
476 */
477 switch (target->status) {
478 case 0:
479 return 0;
480
481 case SRP_PORT_REDIRECT:
482 ret = srp_lookup_path(target);
483 if (ret)
484 return ret;
485 break;
486
487 case SRP_DLID_REDIRECT:
488 break;
489
David Dillow9fe4bcf2008-01-08 17:08:52 -0500490 case SRP_STALE_CONN:
491 /* Our current CM id was stale, and is now in timewait.
492 * Try to reconnect with a new one.
493 */
494 if (!retries-- || srp_new_cm_id(target)) {
495 shost_printk(KERN_ERR, target->scsi_host, PFX
496 "giving up on stale connection\n");
497 target->status = -ECONNRESET;
498 return target->status;
499 }
500
501 shost_printk(KERN_ERR, target->scsi_host, PFX
502 "retrying stale connection\n");
503 break;
504
Roland Dreieraef9ec32005-11-02 14:07:13 -0800505 default:
506 return target->status;
507 }
508 }
509}
510
Roland Dreierd945e1d2006-05-09 10:50:28 -0700511static void srp_unmap_data(struct scsi_cmnd *scmnd,
512 struct srp_target_port *target,
513 struct srp_request *req)
514{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900515 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700516 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
517 scmnd->sc_data_direction != DMA_FROM_DEVICE))
518 return;
519
Roland Dreierf5358a12006-06-17 20:37:29 -0700520 if (req->fmr) {
521 ib_fmr_pool_unmap(req->fmr);
522 req->fmr = NULL;
523 }
524
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100525 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900526 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700527}
528
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700529static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
530{
531 srp_unmap_data(req->scmnd, target, req);
532 list_move_tail(&req->list, &target->free_reqs);
533}
534
535static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
536{
537 req->scmnd->result = DID_RESET << 16;
538 req->scmnd->scsi_done(req->scmnd);
539 srp_remove_req(target, req);
540}
541
Roland Dreieraef9ec32005-11-02 14:07:13 -0800542static int srp_reconnect_target(struct srp_target_port *target)
543{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800544 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700545 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800546 struct ib_wc wc;
547 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800548
549 spin_lock_irq(target->scsi_host->host_lock);
550 if (target->state != SRP_TARGET_LIVE) {
551 spin_unlock_irq(target->scsi_host->host_lock);
552 return -EAGAIN;
553 }
554 target->state = SRP_TARGET_CONNECTING;
555 spin_unlock_irq(target->scsi_host->host_lock);
556
557 srp_disconnect_target(target);
558 /*
559 * Now get a new local CM ID so that we avoid confusing the
560 * target in case things are really fouled up.
561 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500562 ret = srp_new_cm_id(target);
563 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800564 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800565
566 qp_attr.qp_state = IB_QPS_RESET;
567 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
568 if (ret)
569 goto err;
570
571 ret = srp_init_qp(target, target->qp);
572 if (ret)
573 goto err;
574
575 while (ib_poll_cq(target->cq, 1, &wc) > 0)
576 ; /* nothing */
577
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300578 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700579 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
580 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300581 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800582
583 target->rx_head = 0;
584 target->tx_head = 0;
585 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800586
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200587 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800588 ret = srp_connect_target(target);
589 if (ret)
590 goto err;
591
592 spin_lock_irq(target->scsi_host->host_lock);
593 if (target->state == SRP_TARGET_CONNECTING) {
594 ret = 0;
595 target->state = SRP_TARGET_LIVE;
596 } else
597 ret = -EAGAIN;
598 spin_unlock_irq(target->scsi_host->host_lock);
599
600 return ret;
601
602err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500603 shost_printk(KERN_ERR, target->scsi_host,
604 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800605
606 /*
607 * We couldn't reconnect, so kill our target port off.
608 * However, we have to defer the real removal because we might
609 * be in the context of the SCSI error handler now, which
610 * would deadlock if we call scsi_remove_host().
611 */
612 spin_lock_irq(target->scsi_host->host_lock);
613 if (target->state == SRP_TARGET_CONNECTING) {
614 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000615 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800616 schedule_work(&target->work);
617 }
618 spin_unlock_irq(target->scsi_host->host_lock);
619
620 return ret;
621}
622
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700623static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700624 int sg_cnt, struct srp_request *req,
625 struct srp_direct_buf *buf)
626{
627 u64 io_addr = 0;
628 u64 *dma_pages;
629 u32 len;
630 int page_cnt;
631 int i, j;
632 int ret;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100633 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800634 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900635 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700636
637 if (!dev->fmr_pool)
638 return -ENODEV;
639
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700640 if (srp_target_is_mellanox(target) &&
641 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700642 return -EINVAL;
643
Roland Dreierf5358a12006-06-17 20:37:29 -0700644 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900645 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
646 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800647
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900648 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700649 if (i > 0)
650 return -EINVAL;
651 else
652 ++page_cnt;
653 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900654 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700655 ~dev->fmr_page_mask) {
656 if (i < sg_cnt - 1)
657 return -EINVAL;
658 else
659 ++page_cnt;
660 }
661
Ralph Campbell85507bc2006-12-12 14:30:55 -0800662 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700663 }
664
665 page_cnt += len >> dev->fmr_page_shift;
666 if (page_cnt > SRP_FMR_SIZE)
667 return -ENOMEM;
668
669 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
670 if (!dma_pages)
671 return -ENOMEM;
672
673 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900674 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
675 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800676
677 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700678 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900679 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800680 dev->fmr_page_mask) + j;
681 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700682
683 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700684 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700685 if (IS_ERR(req->fmr)) {
686 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700687 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700688 goto out;
689 }
690
Ralph Campbell85507bc2006-12-12 14:30:55 -0800691 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
692 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700693 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
694 buf->len = cpu_to_be32(len);
695
696 ret = 0;
697
698out:
699 kfree(dma_pages);
700
701 return ret;
702}
703
Roland Dreieraef9ec32005-11-02 14:07:13 -0800704static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
705 struct srp_request *req)
706{
Roland Dreiercf368712006-03-24 15:47:26 -0800707 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800708 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800709 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700710 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800711 struct srp_device *dev;
712 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800713
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900714 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800715 return sizeof (struct srp_cmd);
716
717 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
718 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500719 shost_printk(KERN_WARNING, target->scsi_host,
720 PFX "Unhandled data direction %d\n",
721 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800722 return -EINVAL;
723 }
724
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900725 nents = scsi_sg_count(scmnd);
726 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800727
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100728 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800729 ibdev = dev->dev;
730
731 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700732
733 fmt = SRP_DATA_DESC_DIRECT;
734 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800735
736 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700737 /*
738 * The midlayer only generated a single gather/scatter
739 * entry, or DMA mapping coalesced everything to a
740 * single entry. So a direct descriptor along with
741 * the DMA MR suffices.
742 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800743 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800744
Ralph Campbell85507bc2006-12-12 14:30:55 -0800745 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
746 buf->key = cpu_to_be32(dev->mr->rkey);
747 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700748 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700749 (void *) cmd->add_data)) {
750 /*
751 * FMR mapping failed, and the scatterlist has more
752 * than one entry. Generate an indirect memory
753 * descriptor.
754 */
Roland Dreiercf368712006-03-24 15:47:26 -0800755 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900756 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800757 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700758 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800759
760 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700761 len = sizeof (struct srp_cmd) +
762 sizeof (struct srp_indirect_buf) +
763 count * sizeof (struct srp_direct_buf);
764
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900765 scsi_for_each_sg(scmnd, sg, count, i) {
766 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800767
Roland Dreierf5358a12006-06-17 20:37:29 -0700768 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900769 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700770 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800771 cpu_to_be32(dev->mr->rkey);
772 buf->desc_list[i].len = cpu_to_be32(dma_len);
773 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700774 }
Roland Dreiercf368712006-03-24 15:47:26 -0800775
776 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
777 cmd->data_out_desc_cnt = count;
778 else
779 cmd->data_in_desc_cnt = count;
780
Roland Dreierf5358a12006-06-17 20:37:29 -0700781 buf->table_desc.va =
782 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800783 buf->table_desc.key =
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100784 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800785 buf->table_desc.len =
786 cpu_to_be32(count * sizeof (struct srp_direct_buf));
787
Roland Dreiercf368712006-03-24 15:47:26 -0800788 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800789 }
790
791 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
792 cmd->buf_fmt = fmt << 4;
793 else
794 cmd->buf_fmt = fmt;
795
Roland Dreieraef9ec32005-11-02 14:07:13 -0800796 return len;
797}
798
Roland Dreieraef9ec32005-11-02 14:07:13 -0800799static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
800{
801 struct srp_request *req;
802 struct scsi_cmnd *scmnd;
803 unsigned long flags;
804 s32 delta;
805
806 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
807
808 spin_lock_irqsave(target->scsi_host->host_lock, flags);
809
810 target->req_lim += delta;
811
812 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
813
814 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
815 if (be32_to_cpu(rsp->resp_data_len) < 4)
816 req->tsk_status = -1;
817 else
818 req->tsk_status = rsp->data[3];
819 complete(&req->done);
820 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700821 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800822 if (!scmnd)
David Dillow7aa54bd2008-01-07 18:23:41 -0500823 shost_printk(KERN_ERR, target->scsi_host,
824 "Null scmnd for RSP w/tag %016llx\n",
825 (unsigned long long) rsp->tag);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800826 scmnd->result = rsp->status;
827
828 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
829 memcpy(scmnd->sense_buffer, rsp->data +
830 be32_to_cpu(rsp->resp_data_len),
831 min_t(int, be32_to_cpu(rsp->sense_data_len),
832 SCSI_SENSE_BUFFERSIZE));
833 }
834
835 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900836 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800837 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900838 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800839
Roland Dreieraef9ec32005-11-02 14:07:13 -0800840 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800841 scmnd->host_scribble = (void *) -1L;
842 scmnd->scsi_done(scmnd);
843
Roland Dreierd945e1d2006-05-09 10:50:28 -0700844 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800845 } else
846 req->cmd_done = 1;
847 }
848
849 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
850}
851
Roland Dreieraef9ec32005-11-02 14:07:13 -0800852static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
853{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800854 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800855 struct srp_iu *iu;
856 u8 opcode;
857
858 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
859
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100860 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800861 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
862 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800863
864 opcode = *(u8 *) iu->buf;
865
866 if (0) {
867 int i;
868
David Dillow7aa54bd2008-01-07 18:23:41 -0500869 shost_printk(KERN_ERR, target->scsi_host,
870 PFX "recv completion, opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800871
872 for (i = 0; i < wc->byte_len; ++i) {
873 if (i % 8 == 0)
874 printk(KERN_ERR " [%02x] ", i);
875 printk(" %02x", ((u8 *) iu->buf)[i]);
876 if ((i + 1) % 8 == 0)
877 printk("\n");
878 }
879
880 if (wc->byte_len % 8)
881 printk("\n");
882 }
883
884 switch (opcode) {
885 case SRP_RSP:
886 srp_process_rsp(target, iu->buf);
887 break;
888
889 case SRP_T_LOGOUT:
890 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -0500891 shost_printk(KERN_WARNING, target->scsi_host,
892 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800893 break;
894
895 default:
David Dillow7aa54bd2008-01-07 18:23:41 -0500896 shost_printk(KERN_WARNING, target->scsi_host,
897 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800898 break;
899 }
900
Ralph Campbell85507bc2006-12-12 14:30:55 -0800901 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
902 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800903}
904
905static void srp_completion(struct ib_cq *cq, void *target_ptr)
906{
907 struct srp_target_port *target = target_ptr;
908 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800909
910 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
911 while (ib_poll_cq(cq, 1, &wc) > 0) {
912 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500913 shost_printk(KERN_ERR, target->scsi_host,
914 PFX "failed %s status %d\n",
915 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
916 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200917 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800918 break;
919 }
920
921 if (wc.wr_id & SRP_OP_RECV)
922 srp_handle_recv(target, &wc);
923 else
924 ++target->tx_tail;
925 }
926}
927
928static int __srp_post_recv(struct srp_target_port *target)
929{
930 struct srp_iu *iu;
931 struct ib_sge list;
932 struct ib_recv_wr wr, *bad_wr;
933 unsigned int next;
934 int ret;
935
936 next = target->rx_head & (SRP_RQ_SIZE - 1);
937 wr.wr_id = next | SRP_OP_RECV;
938 iu = target->rx_ring[next];
939
940 list.addr = iu->dma;
941 list.length = iu->size;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100942 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800943
944 wr.next = NULL;
945 wr.sg_list = &list;
946 wr.num_sge = 1;
947
948 ret = ib_post_recv(target->qp, &wr, &bad_wr);
949 if (!ret)
950 ++target->rx_head;
951
952 return ret;
953}
954
955static int srp_post_recv(struct srp_target_port *target)
956{
957 unsigned long flags;
958 int ret;
959
960 spin_lock_irqsave(target->scsi_host->host_lock, flags);
961 ret = __srp_post_recv(target);
962 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
963
964 return ret;
965}
966
967/*
968 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800969 * req_lim and tx_head. Lock cannot be dropped between call here and
970 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800971 */
David Dillow8cba2072007-12-19 17:08:43 -0500972static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
973 enum srp_request_type req_type)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800974{
David Dillow8cba2072007-12-19 17:08:43 -0500975 s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
976
Roland Dreieraef9ec32005-11-02 14:07:13 -0800977 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
978 return NULL;
979
David Dillow8cba2072007-12-19 17:08:43 -0500980 if (target->req_lim < min) {
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700981 ++target->zero_req_lim;
David Dillow8cba2072007-12-19 17:08:43 -0500982 return NULL;
983 }
Roland Dreier47f2bce2005-11-15 00:19:21 -0800984
Roland Dreieraef9ec32005-11-02 14:07:13 -0800985 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
986}
987
988/*
989 * Must be called with target->scsi_host->host_lock held to protect
990 * req_lim and tx_head.
991 */
992static int __srp_post_send(struct srp_target_port *target,
993 struct srp_iu *iu, int len)
994{
995 struct ib_sge list;
996 struct ib_send_wr wr, *bad_wr;
997 int ret = 0;
998
Roland Dreieraef9ec32005-11-02 14:07:13 -0800999 list.addr = iu->dma;
1000 list.length = len;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001001 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001002
1003 wr.next = NULL;
1004 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
1005 wr.sg_list = &list;
1006 wr.num_sge = 1;
1007 wr.opcode = IB_WR_SEND;
1008 wr.send_flags = IB_SEND_SIGNALED;
1009
1010 ret = ib_post_send(target->qp, &wr, &bad_wr);
1011
1012 if (!ret) {
1013 ++target->tx_head;
1014 --target->req_lim;
1015 }
1016
1017 return ret;
1018}
1019
1020static int srp_queuecommand(struct scsi_cmnd *scmnd,
1021 void (*done)(struct scsi_cmnd *))
1022{
1023 struct srp_target_port *target = host_to_target(scmnd->device->host);
1024 struct srp_request *req;
1025 struct srp_iu *iu;
1026 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001027 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001028 int len;
1029
1030 if (target->state == SRP_TARGET_CONNECTING)
1031 goto err;
1032
1033 if (target->state == SRP_TARGET_DEAD ||
1034 target->state == SRP_TARGET_REMOVED) {
1035 scmnd->result = DID_BAD_TARGET << 16;
1036 done(scmnd);
1037 return 0;
1038 }
1039
David Dillow8cba2072007-12-19 17:08:43 -05001040 iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001041 if (!iu)
1042 goto err;
1043
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001044 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001045 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1046 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001047
Roland Dreierd945e1d2006-05-09 10:50:28 -07001048 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001049
1050 scmnd->scsi_done = done;
1051 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001052 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001053
1054 cmd = iu->buf;
1055 memset(cmd, 0, sizeof *cmd);
1056
1057 cmd->opcode = SRP_CMD;
1058 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001059 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001060 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1061
Roland Dreieraef9ec32005-11-02 14:07:13 -08001062 req->scmnd = scmnd;
1063 req->cmd = iu;
1064 req->cmd_done = 0;
1065 req->tsk_mgmt = NULL;
1066
1067 len = srp_map_data(scmnd, target, req);
1068 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001069 shost_printk(KERN_ERR, target->scsi_host,
1070 PFX "Failed to map data\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001071 goto err;
1072 }
1073
1074 if (__srp_post_recv(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001075 shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001076 goto err_unmap;
1077 }
1078
Ralph Campbell85507bc2006-12-12 14:30:55 -08001079 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1080 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001081
1082 if (__srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001083 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001084 goto err_unmap;
1085 }
1086
Roland Dreierd945e1d2006-05-09 10:50:28 -07001087 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001088
1089 return 0;
1090
1091err_unmap:
1092 srp_unmap_data(scmnd, target, req);
1093
1094err:
1095 return SCSI_MLQUEUE_HOST_BUSY;
1096}
1097
1098static int srp_alloc_iu_bufs(struct srp_target_port *target)
1099{
1100 int i;
1101
1102 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1103 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1104 target->max_ti_iu_len,
1105 GFP_KERNEL, DMA_FROM_DEVICE);
1106 if (!target->rx_ring[i])
1107 goto err;
1108 }
1109
1110 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1111 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001112 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001113 GFP_KERNEL, DMA_TO_DEVICE);
1114 if (!target->tx_ring[i])
1115 goto err;
1116 }
1117
1118 return 0;
1119
1120err:
1121 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1122 srp_free_iu(target->srp_host, target->rx_ring[i]);
1123 target->rx_ring[i] = NULL;
1124 }
1125
1126 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1127 srp_free_iu(target->srp_host, target->tx_ring[i]);
1128 target->tx_ring[i] = NULL;
1129 }
1130
1131 return -ENOMEM;
1132}
1133
1134static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1135 struct ib_cm_event *event,
1136 struct srp_target_port *target)
1137{
David Dillow7aa54bd2008-01-07 18:23:41 -05001138 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001139 struct ib_class_port_info *cpi;
1140 int opcode;
1141
1142 switch (event->param.rej_rcvd.reason) {
1143 case IB_CM_REJ_PORT_CM_REDIRECT:
1144 cpi = event->param.rej_rcvd.ari;
1145 target->path.dlid = cpi->redirect_lid;
1146 target->path.pkey = cpi->redirect_pkey;
1147 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1148 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1149
1150 target->status = target->path.dlid ?
1151 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1152 break;
1153
1154 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001155 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001156 /*
1157 * Topspin/Cisco SRP gateways incorrectly send
1158 * reject reason code 25 when they mean 24
1159 * (port redirect).
1160 */
1161 memcpy(target->path.dgid.raw,
1162 event->param.rej_rcvd.ari, 16);
1163
David Dillow7aa54bd2008-01-07 18:23:41 -05001164 shost_printk(KERN_DEBUG, shost,
1165 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1166 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1167 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001168
1169 target->status = SRP_PORT_REDIRECT;
1170 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001171 shost_printk(KERN_WARNING, shost,
1172 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001173 target->status = -ECONNRESET;
1174 }
1175 break;
1176
1177 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001178 shost_printk(KERN_WARNING, shost,
1179 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001180 target->status = -ECONNRESET;
1181 break;
1182
1183 case IB_CM_REJ_CONSUMER_DEFINED:
1184 opcode = *(u8 *) event->private_data;
1185 if (opcode == SRP_LOGIN_REJ) {
1186 struct srp_login_rej *rej = event->private_data;
1187 u32 reason = be32_to_cpu(rej->reason);
1188
1189 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001190 shost_printk(KERN_WARNING, shost,
1191 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001192 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001193 shost_printk(KERN_WARNING, shost,
1194 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001195 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001196 shost_printk(KERN_WARNING, shost,
1197 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1198 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001199 target->status = -ECONNRESET;
1200 break;
1201
David Dillow9fe4bcf2008-01-08 17:08:52 -05001202 case IB_CM_REJ_STALE_CONN:
1203 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1204 target->status = SRP_STALE_CONN;
1205 break;
1206
Roland Dreieraef9ec32005-11-02 14:07:13 -08001207 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001208 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1209 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001210 target->status = -ECONNRESET;
1211 }
1212}
1213
1214static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1215{
1216 struct srp_target_port *target = cm_id->context;
1217 struct ib_qp_attr *qp_attr = NULL;
1218 int attr_mask = 0;
1219 int comp = 0;
1220 int opcode = 0;
1221
1222 switch (event->event) {
1223 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001224 shost_printk(KERN_DEBUG, target->scsi_host,
1225 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001226 comp = 1;
1227 target->status = -ECONNRESET;
1228 break;
1229
1230 case IB_CM_REP_RECEIVED:
1231 comp = 1;
1232 opcode = *(u8 *) event->private_data;
1233
1234 if (opcode == SRP_LOGIN_RSP) {
1235 struct srp_login_rsp *rsp = event->private_data;
1236
1237 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1238 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1239
1240 target->scsi_host->can_queue = min(target->req_lim,
1241 target->scsi_host->can_queue);
1242 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001243 shost_printk(KERN_WARNING, target->scsi_host,
1244 PFX "Unhandled RSP opcode %#x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001245 target->status = -ECONNRESET;
1246 break;
1247 }
1248
Vu Phamd2fcea72006-11-21 14:14:10 -08001249 if (!target->rx_ring[0]) {
1250 target->status = srp_alloc_iu_bufs(target);
1251 if (target->status)
1252 break;
1253 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001254
1255 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1256 if (!qp_attr) {
1257 target->status = -ENOMEM;
1258 break;
1259 }
1260
1261 qp_attr->qp_state = IB_QPS_RTR;
1262 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1263 if (target->status)
1264 break;
1265
1266 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1267 if (target->status)
1268 break;
1269
1270 target->status = srp_post_recv(target);
1271 if (target->status)
1272 break;
1273
1274 qp_attr->qp_state = IB_QPS_RTS;
1275 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1276 if (target->status)
1277 break;
1278
1279 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1280 if (target->status)
1281 break;
1282
1283 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1284 if (target->status)
1285 break;
1286
1287 break;
1288
1289 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001290 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001291 comp = 1;
1292
1293 srp_cm_rej_handler(cm_id, event, target);
1294 break;
1295
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001296 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001297 shost_printk(KERN_WARNING, target->scsi_host,
1298 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001299 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001300 shost_printk(KERN_ERR, target->scsi_host,
1301 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001302 break;
1303
1304 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001305 shost_printk(KERN_ERR, target->scsi_host,
1306 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001307
1308 comp = 1;
1309 target->status = 0;
1310 break;
1311
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001312 case IB_CM_MRA_RECEIVED:
1313 case IB_CM_DREQ_ERROR:
1314 case IB_CM_DREP_RECEIVED:
1315 break;
1316
Roland Dreieraef9ec32005-11-02 14:07:13 -08001317 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001318 shost_printk(KERN_WARNING, target->scsi_host,
1319 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001320 break;
1321 }
1322
1323 if (comp)
1324 complete(&target->done);
1325
1326 kfree(qp_attr);
1327
1328 return 0;
1329}
1330
Roland Dreierd945e1d2006-05-09 10:50:28 -07001331static int srp_send_tsk_mgmt(struct srp_target_port *target,
1332 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001333{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001334 struct srp_iu *iu;
1335 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001336
1337 spin_lock_irq(target->scsi_host->host_lock);
1338
Roland Dreier1285b3a2006-03-03 15:47:25 -08001339 if (target->state == SRP_TARGET_DEAD ||
1340 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001341 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001342 goto out;
1343 }
1344
Roland Dreieraef9ec32005-11-02 14:07:13 -08001345 init_completion(&req->done);
1346
David Dillow8cba2072007-12-19 17:08:43 -05001347 iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001348 if (!iu)
1349 goto out;
1350
1351 tsk_mgmt = iu->buf;
1352 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1353
1354 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001355 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1356 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001357 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001358 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001359
1360 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1361 goto out;
1362
1363 req->tsk_mgmt = iu;
1364
1365 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001366
Roland Dreieraef9ec32005-11-02 14:07:13 -08001367 if (!wait_for_completion_timeout(&req->done,
1368 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001369 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001370
Roland Dreierd945e1d2006-05-09 10:50:28 -07001371 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001372
1373out:
1374 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001375 return -1;
1376}
1377
1378static int srp_find_req(struct srp_target_port *target,
1379 struct scsi_cmnd *scmnd,
1380 struct srp_request **req)
1381{
1382 if (scmnd->host_scribble == (void *) -1L)
1383 return -1;
1384
1385 *req = &target->req_ring[(long) scmnd->host_scribble];
1386
1387 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001388}
1389
1390static int srp_abort(struct scsi_cmnd *scmnd)
1391{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001392 struct srp_target_port *target = host_to_target(scmnd->device->host);
1393 struct srp_request *req;
1394 int ret = SUCCESS;
1395
David Dillow7aa54bd2008-01-07 18:23:41 -05001396 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001397
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001398 if (target->qp_in_error)
1399 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001400 if (srp_find_req(target, scmnd, &req))
1401 return FAILED;
1402 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1403 return FAILED;
1404
1405 spin_lock_irq(target->scsi_host->host_lock);
1406
1407 if (req->cmd_done) {
1408 srp_remove_req(target, req);
1409 scmnd->scsi_done(scmnd);
1410 } else if (!req->tsk_status) {
1411 srp_remove_req(target, req);
1412 scmnd->result = DID_ABORT << 16;
1413 } else
1414 ret = FAILED;
1415
1416 spin_unlock_irq(target->scsi_host->host_lock);
1417
1418 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001419}
1420
1421static int srp_reset_device(struct scsi_cmnd *scmnd)
1422{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001423 struct srp_target_port *target = host_to_target(scmnd->device->host);
1424 struct srp_request *req, *tmp;
1425
David Dillow7aa54bd2008-01-07 18:23:41 -05001426 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001427
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001428 if (target->qp_in_error)
1429 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001430 if (srp_find_req(target, scmnd, &req))
1431 return FAILED;
1432 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1433 return FAILED;
1434 if (req->tsk_status)
1435 return FAILED;
1436
1437 spin_lock_irq(target->scsi_host->host_lock);
1438
1439 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001440 if (req->scmnd->device == scmnd->device)
1441 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001442
1443 spin_unlock_irq(target->scsi_host->host_lock);
1444
1445 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001446}
1447
1448static int srp_reset_host(struct scsi_cmnd *scmnd)
1449{
1450 struct srp_target_port *target = host_to_target(scmnd->device->host);
1451 int ret = FAILED;
1452
David Dillow7aa54bd2008-01-07 18:23:41 -05001453 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001454
1455 if (!srp_reconnect_target(target))
1456 ret = SUCCESS;
1457
1458 return ret;
1459}
1460
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001461static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1462{
1463 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1464
1465 if (target->state == SRP_TARGET_DEAD ||
1466 target->state == SRP_TARGET_REMOVED)
1467 return -ENODEV;
1468
1469 return sprintf(buf, "0x%016llx\n",
1470 (unsigned long long) be64_to_cpu(target->id_ext));
1471}
1472
1473static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1474{
1475 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1476
1477 if (target->state == SRP_TARGET_DEAD ||
1478 target->state == SRP_TARGET_REMOVED)
1479 return -ENODEV;
1480
1481 return sprintf(buf, "0x%016llx\n",
1482 (unsigned long long) be64_to_cpu(target->ioc_guid));
1483}
1484
1485static ssize_t show_service_id(struct class_device *cdev, char *buf)
1486{
1487 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1488
1489 if (target->state == SRP_TARGET_DEAD ||
1490 target->state == SRP_TARGET_REMOVED)
1491 return -ENODEV;
1492
1493 return sprintf(buf, "0x%016llx\n",
1494 (unsigned long long) be64_to_cpu(target->service_id));
1495}
1496
1497static ssize_t show_pkey(struct class_device *cdev, char *buf)
1498{
1499 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1500
1501 if (target->state == SRP_TARGET_DEAD ||
1502 target->state == SRP_TARGET_REMOVED)
1503 return -ENODEV;
1504
1505 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1506}
1507
1508static ssize_t show_dgid(struct class_device *cdev, char *buf)
1509{
1510 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1511
1512 if (target->state == SRP_TARGET_DEAD ||
1513 target->state == SRP_TARGET_REMOVED)
1514 return -ENODEV;
1515
1516 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1517 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1518 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1519 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1520 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1521 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1522 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1523 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1524 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1525}
1526
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001527static ssize_t show_orig_dgid(struct class_device *cdev, char *buf)
1528{
1529 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1530
1531 if (target->state == SRP_TARGET_DEAD ||
1532 target->state == SRP_TARGET_REMOVED)
1533 return -ENODEV;
1534
1535 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1536 be16_to_cpu(target->orig_dgid[0]),
1537 be16_to_cpu(target->orig_dgid[1]),
1538 be16_to_cpu(target->orig_dgid[2]),
1539 be16_to_cpu(target->orig_dgid[3]),
1540 be16_to_cpu(target->orig_dgid[4]),
1541 be16_to_cpu(target->orig_dgid[5]),
1542 be16_to_cpu(target->orig_dgid[6]),
1543 be16_to_cpu(target->orig_dgid[7]));
1544}
1545
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001546static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1547{
1548 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1549
1550 if (target->state == SRP_TARGET_DEAD ||
1551 target->state == SRP_TARGET_REMOVED)
1552 return -ENODEV;
1553
1554 return sprintf(buf, "%d\n", target->zero_req_lim);
1555}
1556
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001557static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1558{
1559 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1560
1561 return sprintf(buf, "%d\n", target->srp_host->port);
1562}
1563
1564static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1565{
1566 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1567
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001568 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001569}
1570
1571static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1572static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1573static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1574static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1575static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001576static CLASS_DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001577static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1578static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1579static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001580
1581static struct class_device_attribute *srp_host_attrs[] = {
1582 &class_device_attr_id_ext,
1583 &class_device_attr_ioc_guid,
1584 &class_device_attr_service_id,
1585 &class_device_attr_pkey,
1586 &class_device_attr_dgid,
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001587 &class_device_attr_orig_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001588 &class_device_attr_zero_req_lim,
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001589 &class_device_attr_local_ib_port,
1590 &class_device_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001591 NULL
1592};
1593
Roland Dreieraef9ec32005-11-02 14:07:13 -08001594static struct scsi_host_template srp_template = {
1595 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001596 .name = "InfiniBand SRP initiator",
1597 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001598 .info = srp_target_info,
1599 .queuecommand = srp_queuecommand,
1600 .eh_abort_handler = srp_abort,
1601 .eh_device_reset_handler = srp_reset_device,
1602 .eh_host_reset_handler = srp_reset_host,
1603 .can_queue = SRP_SQ_SIZE,
1604 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001605 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001606 .use_clustering = ENABLE_CLUSTERING,
1607 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001608};
1609
1610static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1611{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001612 struct srp_rport_identifiers ids;
1613 struct srp_rport *rport;
1614
Roland Dreieraef9ec32005-11-02 14:07:13 -08001615 sprintf(target->target_name, "SRP.T10:%016llX",
1616 (unsigned long long) be64_to_cpu(target->id_ext));
1617
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001618 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001619 return -ENODEV;
1620
FUJITA Tomonori32368222007-06-27 16:33:12 +09001621 memcpy(ids.port_id, &target->id_ext, 8);
1622 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001623 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001624 rport = srp_rport_add(target->scsi_host, &ids);
1625 if (IS_ERR(rport)) {
1626 scsi_remove_host(target->scsi_host);
1627 return PTR_ERR(rport);
1628 }
1629
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001630 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001631 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001632 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001633
1634 target->state = SRP_TARGET_LIVE;
1635
Roland Dreieraef9ec32005-11-02 14:07:13 -08001636 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001637 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001638
1639 return 0;
1640}
1641
1642static void srp_release_class_dev(struct class_device *class_dev)
1643{
1644 struct srp_host *host =
1645 container_of(class_dev, struct srp_host, class_dev);
1646
1647 complete(&host->released);
1648}
1649
1650static struct class srp_class = {
1651 .name = "infiniband_srp",
1652 .release = srp_release_class_dev
1653};
1654
1655/*
1656 * Target ports are added by writing
1657 *
1658 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1659 * pkey=<P_Key>,service_id=<service ID>
1660 *
1661 * to the add_target sysfs attribute.
1662 */
1663enum {
1664 SRP_OPT_ERR = 0,
1665 SRP_OPT_ID_EXT = 1 << 0,
1666 SRP_OPT_IOC_GUID = 1 << 1,
1667 SRP_OPT_DGID = 1 << 2,
1668 SRP_OPT_PKEY = 1 << 3,
1669 SRP_OPT_SERVICE_ID = 1 << 4,
1670 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001671 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001672 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001673 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001674 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1675 SRP_OPT_IOC_GUID |
1676 SRP_OPT_DGID |
1677 SRP_OPT_PKEY |
1678 SRP_OPT_SERVICE_ID),
1679};
1680
1681static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001682 { SRP_OPT_ID_EXT, "id_ext=%s" },
1683 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1684 { SRP_OPT_DGID, "dgid=%s" },
1685 { SRP_OPT_PKEY, "pkey=%x" },
1686 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1687 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1688 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001689 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001690 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001691 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001692};
1693
1694static int srp_parse_options(const char *buf, struct srp_target_port *target)
1695{
1696 char *options, *sep_opt;
1697 char *p;
1698 char dgid[3];
1699 substring_t args[MAX_OPT_ARGS];
1700 int opt_mask = 0;
1701 int token;
1702 int ret = -EINVAL;
1703 int i;
1704
1705 options = kstrdup(buf, GFP_KERNEL);
1706 if (!options)
1707 return -ENOMEM;
1708
1709 sep_opt = options;
1710 while ((p = strsep(&sep_opt, ",")) != NULL) {
1711 if (!*p)
1712 continue;
1713
1714 token = match_token(p, srp_opt_tokens, args);
1715 opt_mask |= token;
1716
1717 switch (token) {
1718 case SRP_OPT_ID_EXT:
1719 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001720 if (!p) {
1721 ret = -ENOMEM;
1722 goto out;
1723 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001724 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1725 kfree(p);
1726 break;
1727
1728 case SRP_OPT_IOC_GUID:
1729 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001730 if (!p) {
1731 ret = -ENOMEM;
1732 goto out;
1733 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001734 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1735 kfree(p);
1736 break;
1737
1738 case SRP_OPT_DGID:
1739 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001740 if (!p) {
1741 ret = -ENOMEM;
1742 goto out;
1743 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001744 if (strlen(p) != 32) {
1745 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001746 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001747 goto out;
1748 }
1749
1750 for (i = 0; i < 16; ++i) {
1751 strlcpy(dgid, p + i * 2, 3);
1752 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1753 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001754 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001755 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001756 break;
1757
1758 case SRP_OPT_PKEY:
1759 if (match_hex(args, &token)) {
1760 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1761 goto out;
1762 }
1763 target->path.pkey = cpu_to_be16(token);
1764 break;
1765
1766 case SRP_OPT_SERVICE_ID:
1767 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001768 if (!p) {
1769 ret = -ENOMEM;
1770 goto out;
1771 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001772 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07001773 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001774 kfree(p);
1775 break;
1776
1777 case SRP_OPT_MAX_SECT:
1778 if (match_int(args, &token)) {
1779 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1780 goto out;
1781 }
1782 target->scsi_host->max_sectors = token;
1783 break;
1784
Vu Pham52fb2b502006-06-17 20:37:31 -07001785 case SRP_OPT_MAX_CMD_PER_LUN:
1786 if (match_int(args, &token)) {
1787 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1788 goto out;
1789 }
1790 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1791 break;
1792
Ramachandra K0c0450db2006-06-17 20:37:38 -07001793 case SRP_OPT_IO_CLASS:
1794 if (match_hex(args, &token)) {
1795 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1796 goto out;
1797 }
1798 if (token != SRP_REV10_IB_IO_CLASS &&
1799 token != SRP_REV16A_IB_IO_CLASS) {
1800 printk(KERN_WARNING PFX "unknown IO class parameter value"
1801 " %x specified (use %x or %x).\n",
1802 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1803 goto out;
1804 }
1805 target->io_class = token;
1806 break;
1807
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001808 case SRP_OPT_INITIATOR_EXT:
1809 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001810 if (!p) {
1811 ret = -ENOMEM;
1812 goto out;
1813 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001814 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1815 kfree(p);
1816 break;
1817
Roland Dreieraef9ec32005-11-02 14:07:13 -08001818 default:
1819 printk(KERN_WARNING PFX "unknown parameter or missing value "
1820 "'%s' in target creation request\n", p);
1821 goto out;
1822 }
1823 }
1824
1825 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1826 ret = 0;
1827 else
1828 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1829 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1830 !(srp_opt_tokens[i].token & opt_mask))
1831 printk(KERN_WARNING PFX "target creation request is "
1832 "missing parameter '%s'\n",
1833 srp_opt_tokens[i].pattern);
1834
1835out:
1836 kfree(options);
1837 return ret;
1838}
1839
1840static ssize_t srp_create_target(struct class_device *class_dev,
1841 const char *buf, size_t count)
1842{
1843 struct srp_host *host =
1844 container_of(class_dev, struct srp_host, class_dev);
1845 struct Scsi_Host *target_host;
1846 struct srp_target_port *target;
1847 int ret;
1848 int i;
1849
1850 target_host = scsi_host_alloc(&srp_template,
1851 sizeof (struct srp_target_port));
1852 if (!target_host)
1853 return -ENOMEM;
1854
FUJITA Tomonori32368222007-06-27 16:33:12 +09001855 target_host->transportt = ib_srp_transport_template;
Arne Redlich3c8edf02006-11-15 12:43:00 +01001856 target_host->max_lun = SRP_MAX_LUN;
1857 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001858
Roland Dreieraef9ec32005-11-02 14:07:13 -08001859 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001860
Ramachandra K0c0450db2006-06-17 20:37:38 -07001861 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001862 target->scsi_host = target_host;
1863 target->srp_host = host;
1864
Roland Dreierd945e1d2006-05-09 10:50:28 -07001865 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001866 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001867 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1868 target->req_ring[i].index = i;
1869 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1870 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001871
1872 ret = srp_parse_options(buf, target);
1873 if (ret)
1874 goto err;
1875
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001876 ib_get_cached_gid(host->srp_dev->dev, host->port, 0,
1877 &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001878
David Dillow7aa54bd2008-01-07 18:23:41 -05001879 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1880 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1881 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08001882 (unsigned long long) be64_to_cpu(target->id_ext),
1883 (unsigned long long) be64_to_cpu(target->ioc_guid),
1884 be16_to_cpu(target->path.pkey),
1885 (unsigned long long) be64_to_cpu(target->service_id),
1886 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1887 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1888 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1889 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1890 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1891 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1892 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1893 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1894
1895 ret = srp_create_target_ib(target);
1896 if (ret)
1897 goto err;
1898
David Dillow9fe4bcf2008-01-08 17:08:52 -05001899 ret = srp_new_cm_id(target);
1900 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001901 goto err_free;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001902
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001903 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001904 ret = srp_connect_target(target);
1905 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001906 shost_printk(KERN_ERR, target->scsi_host,
1907 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001908 goto err_cm_id;
1909 }
1910
1911 ret = srp_add_target(host, target);
1912 if (ret)
1913 goto err_disconnect;
1914
1915 return count;
1916
1917err_disconnect:
1918 srp_disconnect_target(target);
1919
1920err_cm_id:
1921 ib_destroy_cm_id(target->cm_id);
1922
1923err_free:
1924 srp_free_target_ib(target);
1925
1926err:
1927 scsi_host_put(target_host);
1928
1929 return ret;
1930}
1931
1932static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1933
1934static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1935{
1936 struct srp_host *host =
1937 container_of(class_dev, struct srp_host, class_dev);
1938
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001939 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001940}
1941
1942static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1943
1944static ssize_t show_port(struct class_device *class_dev, char *buf)
1945{
1946 struct srp_host *host =
1947 container_of(class_dev, struct srp_host, class_dev);
1948
1949 return sprintf(buf, "%d\n", host->port);
1950}
1951
1952static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1953
Roland Dreierf5358a12006-06-17 20:37:29 -07001954static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001955{
1956 struct srp_host *host;
1957
1958 host = kzalloc(sizeof *host, GFP_KERNEL);
1959 if (!host)
1960 return NULL;
1961
1962 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001963 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001964 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001965 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001966 host->port = port;
1967
Roland Dreieraef9ec32005-11-02 14:07:13 -08001968 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001969 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001970 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001971 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001972
1973 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001974 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001975 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1976 goto err_class;
1977 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1978 goto err_class;
1979 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1980 goto err_class;
1981
1982 return host;
1983
1984err_class:
1985 class_device_unregister(&host->class_dev);
1986
Roland Dreierf5358a12006-06-17 20:37:29 -07001987free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001988 kfree(host);
1989
1990 return NULL;
1991}
1992
1993static void srp_add_one(struct ib_device *device)
1994{
Roland Dreierf5358a12006-06-17 20:37:29 -07001995 struct srp_device *srp_dev;
1996 struct ib_device_attr *dev_attr;
1997 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001998 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001999 int s, e, p;
2000
Roland Dreierf5358a12006-06-17 20:37:29 -07002001 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2002 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08002003 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002004
Roland Dreierf5358a12006-06-17 20:37:29 -07002005 if (ib_query_device(device, dev_attr)) {
2006 printk(KERN_WARNING PFX "Query device failed for %s\n",
2007 device->name);
2008 goto free_attr;
2009 }
2010
2011 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2012 if (!srp_dev)
2013 goto free_attr;
2014
2015 /*
2016 * Use the smallest page size supported by the HCA, down to a
2017 * minimum of 512 bytes (which is the smallest sector that a
2018 * SCSI command will ever carry).
2019 */
2020 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2021 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08002022 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07002023
2024 INIT_LIST_HEAD(&srp_dev->dev_list);
2025
2026 srp_dev->dev = device;
2027 srp_dev->pd = ib_alloc_pd(device);
2028 if (IS_ERR(srp_dev->pd))
2029 goto free_dev;
2030
2031 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2032 IB_ACCESS_LOCAL_WRITE |
2033 IB_ACCESS_REMOTE_READ |
2034 IB_ACCESS_REMOTE_WRITE);
2035 if (IS_ERR(srp_dev->mr))
2036 goto err_pd;
2037
2038 memset(&fmr_param, 0, sizeof fmr_param);
2039 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2040 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2041 fmr_param.cache = 1;
2042 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2043 fmr_param.page_shift = srp_dev->fmr_page_shift;
2044 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2045 IB_ACCESS_REMOTE_WRITE |
2046 IB_ACCESS_REMOTE_READ);
2047
2048 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2049 if (IS_ERR(srp_dev->fmr_pool))
2050 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002051
Tom Tucker07ebafb2006-08-03 16:02:42 -05002052 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002053 s = 0;
2054 e = 0;
2055 } else {
2056 s = 1;
2057 e = device->phys_port_cnt;
2058 }
2059
2060 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002061 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002062 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002063 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002064 }
2065
Roland Dreierf5358a12006-06-17 20:37:29 -07002066 ib_set_client_data(device, &srp_client, srp_dev);
2067
2068 goto free_attr;
2069
2070err_pd:
2071 ib_dealloc_pd(srp_dev->pd);
2072
2073free_dev:
2074 kfree(srp_dev);
2075
2076free_attr:
2077 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002078}
2079
2080static void srp_remove_one(struct ib_device *device)
2081{
Roland Dreierf5358a12006-06-17 20:37:29 -07002082 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002083 struct srp_host *host, *tmp_host;
2084 LIST_HEAD(target_list);
2085 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002086
Roland Dreierf5358a12006-06-17 20:37:29 -07002087 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002088
Roland Dreierf5358a12006-06-17 20:37:29 -07002089 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002090 class_device_unregister(&host->class_dev);
2091 /*
2092 * Wait for the sysfs entry to go away, so that no new
2093 * target ports can be created.
2094 */
2095 wait_for_completion(&host->released);
2096
2097 /*
2098 * Mark all target ports as removed, so we stop queueing
2099 * commands and don't try to reconnect.
2100 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002101 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002102 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002103 spin_lock_irq(target->scsi_host->host_lock);
2104 target->state = SRP_TARGET_REMOVED;
2105 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002106 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002107 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002108
2109 /*
2110 * Wait for any reconnection tasks that may have
2111 * started before we marked our target ports as
2112 * removed, and any target port removal tasks.
2113 */
2114 flush_scheduled_work();
2115
2116 list_for_each_entry_safe(target, tmp_target,
2117 &host->target_list, list) {
David Dillowb0e47c82008-01-03 10:25:27 -08002118 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002119 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002120 srp_disconnect_target(target);
2121 ib_destroy_cm_id(target->cm_id);
2122 srp_free_target_ib(target);
2123 scsi_host_put(target->scsi_host);
2124 }
2125
Roland Dreieraef9ec32005-11-02 14:07:13 -08002126 kfree(host);
2127 }
2128
Roland Dreierf5358a12006-06-17 20:37:29 -07002129 if (srp_dev->fmr_pool)
2130 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2131 ib_dereg_mr(srp_dev->mr);
2132 ib_dealloc_pd(srp_dev->pd);
2133
2134 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002135}
2136
FUJITA Tomonori32368222007-06-27 16:33:12 +09002137static struct srp_function_template ib_srp_transport_functions = {
2138};
2139
Roland Dreieraef9ec32005-11-02 14:07:13 -08002140static int __init srp_init_module(void)
2141{
2142 int ret;
2143
David Dillow1e89a192008-04-16 21:01:12 -07002144 if (srp_sg_tablesize > 255) {
2145 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2146 srp_sg_tablesize = 255;
2147 }
2148
FUJITA Tomonori32368222007-06-27 16:33:12 +09002149 ib_srp_transport_template =
2150 srp_attach_transport(&ib_srp_transport_functions);
2151 if (!ib_srp_transport_template)
2152 return -ENOMEM;
2153
Vu Pham74b0a152006-06-17 20:37:32 -07002154 srp_template.sg_tablesize = srp_sg_tablesize;
2155 srp_max_iu_len = (sizeof (struct srp_cmd) +
2156 sizeof (struct srp_indirect_buf) +
2157 srp_sg_tablesize * 16);
2158
Roland Dreieraef9ec32005-11-02 14:07:13 -08002159 ret = class_register(&srp_class);
2160 if (ret) {
2161 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002162 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002163 return ret;
2164 }
2165
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002166 ib_sa_register_client(&srp_sa_client);
2167
Roland Dreieraef9ec32005-11-02 14:07:13 -08002168 ret = ib_register_client(&srp_client);
2169 if (ret) {
2170 printk(KERN_ERR PFX "couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002171 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002172 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002173 class_unregister(&srp_class);
2174 return ret;
2175 }
2176
2177 return 0;
2178}
2179
2180static void __exit srp_cleanup_module(void)
2181{
2182 ib_unregister_client(&srp_client);
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);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002185 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002186}
2187
2188module_init(srp_init_module);
2189module_exit(srp_cleanup_module);