blob: 7c13db885bf67defc836d8d60552865ff0041a62 [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);
83static void srp_completion(struct ib_cq *cq, void *target_ptr);
84static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
85
FUJITA Tomonori32368222007-06-27 16:33:12 +090086static struct scsi_transport_template *ib_srp_transport_template;
87
Roland Dreieraef9ec32005-11-02 14:07:13 -080088static struct ib_client srp_client = {
89 .name = "srp",
90 .add = srp_add_one,
91 .remove = srp_remove_one
92};
93
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070094static struct ib_sa_client srp_sa_client;
95
Roland Dreieraef9ec32005-11-02 14:07:13 -080096static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
97{
98 return (struct srp_target_port *) host->hostdata;
99}
100
101static const char *srp_target_info(struct Scsi_Host *host)
102{
103 return host_to_target(host)->target_name;
104}
105
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700106static int srp_target_is_topspin(struct srp_target_port *target)
107{
108 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700109 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700110
111 return topspin_workarounds &&
Raghava Kondapalli3d1ff482007-08-03 10:45:18 -0700112 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
113 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700114}
115
116static int srp_target_is_mellanox(struct srp_target_port *target)
117{
118 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
119
120 return mellanox_workarounds &&
121 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
122}
123
Roland Dreieraef9ec32005-11-02 14:07:13 -0800124static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
125 gfp_t gfp_mask,
126 enum dma_data_direction direction)
127{
128 struct srp_iu *iu;
129
130 iu = kmalloc(sizeof *iu, gfp_mask);
131 if (!iu)
132 goto out;
133
134 iu->buf = kzalloc(size, gfp_mask);
135 if (!iu->buf)
136 goto out_free_iu;
137
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100138 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
139 direction);
140 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800141 goto out_free_buf;
142
143 iu->size = size;
144 iu->direction = direction;
145
146 return iu;
147
148out_free_buf:
149 kfree(iu->buf);
150out_free_iu:
151 kfree(iu);
152out:
153 return NULL;
154}
155
156static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
157{
158 if (!iu)
159 return;
160
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100161 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
162 iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800163 kfree(iu->buf);
164 kfree(iu);
165}
166
167static void srp_qp_event(struct ib_event *event, void *context)
168{
169 printk(KERN_ERR PFX "QP event %d\n", event->event);
170}
171
172static int srp_init_qp(struct srp_target_port *target,
173 struct ib_qp *qp)
174{
175 struct ib_qp_attr *attr;
176 int ret;
177
178 attr = kmalloc(sizeof *attr, GFP_KERNEL);
179 if (!attr)
180 return -ENOMEM;
181
Roland Dreier969a60f2008-07-14 23:48:43 -0700182 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
183 target->srp_host->port,
184 be16_to_cpu(target->path.pkey),
185 &attr->pkey_index);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800186 if (ret)
187 goto out;
188
189 attr->qp_state = IB_QPS_INIT;
190 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
191 IB_ACCESS_REMOTE_WRITE);
192 attr->port_num = target->srp_host->port;
193
194 ret = ib_modify_qp(qp, attr,
195 IB_QP_STATE |
196 IB_QP_PKEY_INDEX |
197 IB_QP_ACCESS_FLAGS |
198 IB_QP_PORT);
199
200out:
201 kfree(attr);
202 return ret;
203}
204
David Dillow9fe4bcf2008-01-08 17:08:52 -0500205static int srp_new_cm_id(struct srp_target_port *target)
206{
207 struct ib_cm_id *new_cm_id;
208
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100209 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
David Dillow9fe4bcf2008-01-08 17:08:52 -0500210 srp_cm_handler, target);
211 if (IS_ERR(new_cm_id))
212 return PTR_ERR(new_cm_id);
213
214 if (target->cm_id)
215 ib_destroy_cm_id(target->cm_id);
216 target->cm_id = new_cm_id;
217
218 return 0;
219}
220
Roland Dreieraef9ec32005-11-02 14:07:13 -0800221static int srp_create_target_ib(struct srp_target_port *target)
222{
223 struct ib_qp_init_attr *init_attr;
224 int ret;
225
226 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
227 if (!init_attr)
228 return -ENOMEM;
229
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100230 target->cq = ib_create_cq(target->srp_host->srp_dev->dev,
231 srp_completion, NULL, target, SRP_CQ_SIZE, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800232 if (IS_ERR(target->cq)) {
233 ret = PTR_ERR(target->cq);
234 goto out;
235 }
236
237 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
238
239 init_attr->event_handler = srp_qp_event;
240 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
241 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
242 init_attr->cap.max_recv_sge = 1;
243 init_attr->cap.max_send_sge = 1;
244 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
245 init_attr->qp_type = IB_QPT_RC;
246 init_attr->send_cq = target->cq;
247 init_attr->recv_cq = target->cq;
248
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100249 target->qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800250 if (IS_ERR(target->qp)) {
251 ret = PTR_ERR(target->qp);
252 ib_destroy_cq(target->cq);
253 goto out;
254 }
255
256 ret = srp_init_qp(target, target->qp);
257 if (ret) {
258 ib_destroy_qp(target->qp);
259 ib_destroy_cq(target->cq);
260 goto out;
261 }
262
263out:
264 kfree(init_attr);
265 return ret;
266}
267
268static void srp_free_target_ib(struct srp_target_port *target)
269{
270 int i;
271
272 ib_destroy_qp(target->qp);
273 ib_destroy_cq(target->cq);
274
275 for (i = 0; i < SRP_RQ_SIZE; ++i)
276 srp_free_iu(target->srp_host, target->rx_ring[i]);
277 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
278 srp_free_iu(target->srp_host, target->tx_ring[i]);
279}
280
281static void srp_path_rec_completion(int status,
282 struct ib_sa_path_rec *pathrec,
283 void *target_ptr)
284{
285 struct srp_target_port *target = target_ptr;
286
287 target->status = status;
288 if (status)
David Dillow7aa54bd2008-01-07 18:23:41 -0500289 shost_printk(KERN_ERR, target->scsi_host,
290 PFX "Got failed path rec status %d\n", status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800291 else
292 target->path = *pathrec;
293 complete(&target->done);
294}
295
296static int srp_lookup_path(struct srp_target_port *target)
297{
298 target->path.numb_path = 1;
299
300 init_completion(&target->done);
301
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700302 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100303 target->srp_host->srp_dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800304 target->srp_host->port,
305 &target->path,
Sean Hefty247e0202007-08-08 15:51:18 -0700306 IB_SA_PATH_REC_SERVICE_ID |
Roland Dreieraef9ec32005-11-02 14:07:13 -0800307 IB_SA_PATH_REC_DGID |
308 IB_SA_PATH_REC_SGID |
309 IB_SA_PATH_REC_NUMB_PATH |
310 IB_SA_PATH_REC_PKEY,
311 SRP_PATH_REC_TIMEOUT_MS,
312 GFP_KERNEL,
313 srp_path_rec_completion,
314 target, &target->path_query);
315 if (target->path_query_id < 0)
316 return target->path_query_id;
317
318 wait_for_completion(&target->done);
319
320 if (target->status < 0)
David Dillow7aa54bd2008-01-07 18:23:41 -0500321 shost_printk(KERN_WARNING, target->scsi_host,
322 PFX "Path record query failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800323
324 return target->status;
325}
326
327static int srp_send_req(struct srp_target_port *target)
328{
329 struct {
330 struct ib_cm_req_param param;
331 struct srp_login_req priv;
332 } *req = NULL;
333 int status;
334
335 req = kzalloc(sizeof *req, GFP_KERNEL);
336 if (!req)
337 return -ENOMEM;
338
339 req->param.primary_path = &target->path;
340 req->param.alternate_path = NULL;
341 req->param.service_id = target->service_id;
342 req->param.qp_num = target->qp->qp_num;
343 req->param.qp_type = target->qp->qp_type;
344 req->param.private_data = &req->priv;
345 req->param.private_data_len = sizeof req->priv;
346 req->param.flow_control = 1;
347
348 get_random_bytes(&req->param.starting_psn, 4);
349 req->param.starting_psn &= 0xffffff;
350
351 /*
352 * Pick some arbitrary defaults here; we could make these
353 * module parameters if anyone cared about setting them.
354 */
355 req->param.responder_resources = 4;
356 req->param.remote_cm_response_timeout = 20;
357 req->param.local_cm_response_timeout = 20;
358 req->param.retry_count = 7;
359 req->param.rnr_retry_count = 7;
360 req->param.max_cm_retries = 15;
361
362 req->priv.opcode = SRP_LOGIN_REQ;
363 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700364 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800365 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
366 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700367 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700368 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700369 * port identifier format is 8 bytes of ID extension followed
370 * by 8 bytes of GUID. Older drafts put the two halves in the
371 * opposite order, so that the GUID comes first.
372 *
373 * Targets conforming to these obsolete drafts can be
374 * recognized by the I/O Class they report.
375 */
376 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
377 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200378 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700379 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200380 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700381 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
382 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
383 } else {
384 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200385 &target->initiator_ext, 8);
386 memcpy(req->priv.initiator_port_id + 8,
387 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700388 memcpy(req->priv.target_port_id, &target->id_ext, 8);
389 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
390 }
391
Roland Dreieraef9ec32005-11-02 14:07:13 -0800392 /*
393 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200394 * zero out the first 8 bytes of our initiator port ID and set
395 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800396 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700397 if (srp_target_is_topspin(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500398 shost_printk(KERN_DEBUG, target->scsi_host,
399 PFX "Topspin/Cisco initiator port ID workaround "
400 "activated for target GUID %016llx\n",
401 (unsigned long long) be64_to_cpu(target->ioc_guid));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800402 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200403 memcpy(req->priv.initiator_port_id + 8,
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100404 &target->srp_host->srp_dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800405 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800406
407 status = ib_send_cm_req(target->cm_id, &req->param);
408
409 kfree(req);
410
411 return status;
412}
413
414static void srp_disconnect_target(struct srp_target_port *target)
415{
416 /* XXX should send SRP_I_LOGOUT request */
417
418 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700419 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500420 shost_printk(KERN_DEBUG, target->scsi_host,
421 PFX "Sending CM DREQ failed\n");
Roland Dreiere6581052006-05-17 09:13:21 -0700422 return;
423 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800424 wait_for_completion(&target->done);
425}
426
David Howellsc4028952006-11-22 14:57:56 +0000427static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800428{
David Howellsc4028952006-11-22 14:57:56 +0000429 struct srp_target_port *target =
430 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800431
432 spin_lock_irq(target->scsi_host->host_lock);
433 if (target->state != SRP_TARGET_DEAD) {
434 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800435 return;
436 }
437 target->state = SRP_TARGET_REMOVED;
438 spin_unlock_irq(target->scsi_host->host_lock);
439
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700440 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800441 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700442 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800443
FUJITA Tomonori32368222007-06-27 16:33:12 +0900444 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800445 scsi_remove_host(target->scsi_host);
446 ib_destroy_cm_id(target->cm_id);
447 srp_free_target_ib(target);
448 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800449}
450
451static int srp_connect_target(struct srp_target_port *target)
452{
David Dillow9fe4bcf2008-01-08 17:08:52 -0500453 int retries = 3;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800454 int ret;
455
456 ret = srp_lookup_path(target);
457 if (ret)
458 return ret;
459
460 while (1) {
461 init_completion(&target->done);
462 ret = srp_send_req(target);
463 if (ret)
464 return ret;
465 wait_for_completion(&target->done);
466
467 /*
468 * The CM event handling code will set status to
469 * SRP_PORT_REDIRECT if we get a port redirect REJ
470 * back, or SRP_DLID_REDIRECT if we get a lid/qp
471 * redirect REJ back.
472 */
473 switch (target->status) {
474 case 0:
475 return 0;
476
477 case SRP_PORT_REDIRECT:
478 ret = srp_lookup_path(target);
479 if (ret)
480 return ret;
481 break;
482
483 case SRP_DLID_REDIRECT:
484 break;
485
David Dillow9fe4bcf2008-01-08 17:08:52 -0500486 case SRP_STALE_CONN:
487 /* Our current CM id was stale, and is now in timewait.
488 * Try to reconnect with a new one.
489 */
490 if (!retries-- || srp_new_cm_id(target)) {
491 shost_printk(KERN_ERR, target->scsi_host, PFX
492 "giving up on stale connection\n");
493 target->status = -ECONNRESET;
494 return target->status;
495 }
496
497 shost_printk(KERN_ERR, target->scsi_host, PFX
498 "retrying stale connection\n");
499 break;
500
Roland Dreieraef9ec32005-11-02 14:07:13 -0800501 default:
502 return target->status;
503 }
504 }
505}
506
Roland Dreierd945e1d2006-05-09 10:50:28 -0700507static void srp_unmap_data(struct scsi_cmnd *scmnd,
508 struct srp_target_port *target,
509 struct srp_request *req)
510{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900511 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700512 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
513 scmnd->sc_data_direction != DMA_FROM_DEVICE))
514 return;
515
Roland Dreierf5358a12006-06-17 20:37:29 -0700516 if (req->fmr) {
517 ib_fmr_pool_unmap(req->fmr);
518 req->fmr = NULL;
519 }
520
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100521 ib_dma_unmap_sg(target->srp_host->srp_dev->dev, scsi_sglist(scmnd),
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900522 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700523}
524
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700525static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
526{
527 srp_unmap_data(req->scmnd, target, req);
528 list_move_tail(&req->list, &target->free_reqs);
529}
530
531static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
532{
533 req->scmnd->result = DID_RESET << 16;
534 req->scmnd->scsi_done(req->scmnd);
535 srp_remove_req(target, req);
536}
537
Roland Dreieraef9ec32005-11-02 14:07:13 -0800538static int srp_reconnect_target(struct srp_target_port *target)
539{
Roland Dreieraef9ec32005-11-02 14:07:13 -0800540 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700541 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800542 struct ib_wc wc;
543 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800544
545 spin_lock_irq(target->scsi_host->host_lock);
546 if (target->state != SRP_TARGET_LIVE) {
547 spin_unlock_irq(target->scsi_host->host_lock);
548 return -EAGAIN;
549 }
550 target->state = SRP_TARGET_CONNECTING;
551 spin_unlock_irq(target->scsi_host->host_lock);
552
553 srp_disconnect_target(target);
554 /*
555 * Now get a new local CM ID so that we avoid confusing the
556 * target in case things are really fouled up.
557 */
David Dillow9fe4bcf2008-01-08 17:08:52 -0500558 ret = srp_new_cm_id(target);
559 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800560 goto err;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800561
562 qp_attr.qp_state = IB_QPS_RESET;
563 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
564 if (ret)
565 goto err;
566
567 ret = srp_init_qp(target, target->qp);
568 if (ret)
569 goto err;
570
571 while (ib_poll_cq(target->cq, 1, &wc) > 0)
572 ; /* nothing */
573
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300574 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700575 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
576 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300577 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800578
579 target->rx_head = 0;
580 target->tx_head = 0;
581 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800582
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200583 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800584 ret = srp_connect_target(target);
585 if (ret)
586 goto err;
587
588 spin_lock_irq(target->scsi_host->host_lock);
589 if (target->state == SRP_TARGET_CONNECTING) {
590 ret = 0;
591 target->state = SRP_TARGET_LIVE;
592 } else
593 ret = -EAGAIN;
594 spin_unlock_irq(target->scsi_host->host_lock);
595
596 return ret;
597
598err:
David Dillow7aa54bd2008-01-07 18:23:41 -0500599 shost_printk(KERN_ERR, target->scsi_host,
600 PFX "reconnect failed (%d), removing target port.\n", ret);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800601
602 /*
603 * We couldn't reconnect, so kill our target port off.
604 * However, we have to defer the real removal because we might
605 * be in the context of the SCSI error handler now, which
606 * would deadlock if we call scsi_remove_host().
607 */
608 spin_lock_irq(target->scsi_host->host_lock);
609 if (target->state == SRP_TARGET_CONNECTING) {
610 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000611 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800612 schedule_work(&target->work);
613 }
614 spin_unlock_irq(target->scsi_host->host_lock);
615
616 return ret;
617}
618
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700619static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700620 int sg_cnt, struct srp_request *req,
621 struct srp_direct_buf *buf)
622{
623 u64 io_addr = 0;
624 u64 *dma_pages;
625 u32 len;
626 int page_cnt;
627 int i, j;
628 int ret;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100629 struct srp_device *dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800630 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900631 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700632
633 if (!dev->fmr_pool)
634 return -ENODEV;
635
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700636 if (srp_target_is_mellanox(target) &&
637 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700638 return -EINVAL;
639
Roland Dreierf5358a12006-06-17 20:37:29 -0700640 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900641 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
642 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800643
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900644 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700645 if (i > 0)
646 return -EINVAL;
647 else
648 ++page_cnt;
649 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900650 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700651 ~dev->fmr_page_mask) {
652 if (i < sg_cnt - 1)
653 return -EINVAL;
654 else
655 ++page_cnt;
656 }
657
Ralph Campbell85507bc2006-12-12 14:30:55 -0800658 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700659 }
660
661 page_cnt += len >> dev->fmr_page_shift;
662 if (page_cnt > SRP_FMR_SIZE)
663 return -ENOMEM;
664
665 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
666 if (!dma_pages)
667 return -ENOMEM;
668
669 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900670 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
671 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800672
673 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700674 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900675 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800676 dev->fmr_page_mask) + j;
677 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700678
679 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700680 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700681 if (IS_ERR(req->fmr)) {
682 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700683 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700684 goto out;
685 }
686
Ralph Campbell85507bc2006-12-12 14:30:55 -0800687 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
688 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700689 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
690 buf->len = cpu_to_be32(len);
691
692 ret = 0;
693
694out:
695 kfree(dma_pages);
696
697 return ret;
698}
699
Roland Dreieraef9ec32005-11-02 14:07:13 -0800700static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
701 struct srp_request *req)
702{
Roland Dreiercf368712006-03-24 15:47:26 -0800703 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800704 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800705 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700706 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800707 struct srp_device *dev;
708 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800709
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900710 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800711 return sizeof (struct srp_cmd);
712
713 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
714 scmnd->sc_data_direction != DMA_TO_DEVICE) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500715 shost_printk(KERN_WARNING, target->scsi_host,
716 PFX "Unhandled data direction %d\n",
717 scmnd->sc_data_direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800718 return -EINVAL;
719 }
720
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900721 nents = scsi_sg_count(scmnd);
722 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800723
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100724 dev = target->srp_host->srp_dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800725 ibdev = dev->dev;
726
727 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700728
729 fmt = SRP_DATA_DESC_DIRECT;
730 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800731
732 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700733 /*
734 * The midlayer only generated a single gather/scatter
735 * entry, or DMA mapping coalesced everything to a
736 * single entry. So a direct descriptor along with
737 * the DMA MR suffices.
738 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800739 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800740
Ralph Campbell85507bc2006-12-12 14:30:55 -0800741 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
742 buf->key = cpu_to_be32(dev->mr->rkey);
743 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700744 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700745 (void *) cmd->add_data)) {
746 /*
747 * FMR mapping failed, and the scatterlist has more
748 * than one entry. Generate an indirect memory
749 * descriptor.
750 */
Roland Dreiercf368712006-03-24 15:47:26 -0800751 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900752 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800753 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700754 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800755
756 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700757 len = sizeof (struct srp_cmd) +
758 sizeof (struct srp_indirect_buf) +
759 count * sizeof (struct srp_direct_buf);
760
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900761 scsi_for_each_sg(scmnd, sg, count, i) {
762 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800763
Roland Dreierf5358a12006-06-17 20:37:29 -0700764 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900765 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700766 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800767 cpu_to_be32(dev->mr->rkey);
768 buf->desc_list[i].len = cpu_to_be32(dma_len);
769 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700770 }
Roland Dreiercf368712006-03-24 15:47:26 -0800771
772 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
773 cmd->data_out_desc_cnt = count;
774 else
775 cmd->data_in_desc_cnt = count;
776
Roland Dreierf5358a12006-06-17 20:37:29 -0700777 buf->table_desc.va =
778 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800779 buf->table_desc.key =
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100780 cpu_to_be32(target->srp_host->srp_dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800781 buf->table_desc.len =
782 cpu_to_be32(count * sizeof (struct srp_direct_buf));
783
Roland Dreiercf368712006-03-24 15:47:26 -0800784 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800785 }
786
787 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
788 cmd->buf_fmt = fmt << 4;
789 else
790 cmd->buf_fmt = fmt;
791
Roland Dreieraef9ec32005-11-02 14:07:13 -0800792 return len;
793}
794
Roland Dreieraef9ec32005-11-02 14:07:13 -0800795static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
796{
797 struct srp_request *req;
798 struct scsi_cmnd *scmnd;
799 unsigned long flags;
800 s32 delta;
801
802 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
803
804 spin_lock_irqsave(target->scsi_host->host_lock, flags);
805
806 target->req_lim += delta;
807
808 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
809
810 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
811 if (be32_to_cpu(rsp->resp_data_len) < 4)
812 req->tsk_status = -1;
813 else
814 req->tsk_status = rsp->data[3];
815 complete(&req->done);
816 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700817 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800818 if (!scmnd)
David Dillow7aa54bd2008-01-07 18:23:41 -0500819 shost_printk(KERN_ERR, target->scsi_host,
820 "Null scmnd for RSP w/tag %016llx\n",
821 (unsigned long long) rsp->tag);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800822 scmnd->result = rsp->status;
823
824 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
825 memcpy(scmnd->sense_buffer, rsp->data +
826 be32_to_cpu(rsp->resp_data_len),
827 min_t(int, be32_to_cpu(rsp->sense_data_len),
828 SCSI_SENSE_BUFFERSIZE));
829 }
830
831 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900832 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800833 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900834 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800835
Roland Dreieraef9ec32005-11-02 14:07:13 -0800836 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800837 scmnd->host_scribble = (void *) -1L;
838 scmnd->scsi_done(scmnd);
839
Roland Dreierd945e1d2006-05-09 10:50:28 -0700840 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800841 } else
842 req->cmd_done = 1;
843 }
844
845 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
846}
847
Roland Dreieraef9ec32005-11-02 14:07:13 -0800848static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
849{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800850 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800851 struct srp_iu *iu;
852 u8 opcode;
853
854 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
855
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100856 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800857 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
858 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800859
860 opcode = *(u8 *) iu->buf;
861
862 if (0) {
863 int i;
864
David Dillow7aa54bd2008-01-07 18:23:41 -0500865 shost_printk(KERN_ERR, target->scsi_host,
866 PFX "recv completion, opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800867
868 for (i = 0; i < wc->byte_len; ++i) {
869 if (i % 8 == 0)
870 printk(KERN_ERR " [%02x] ", i);
871 printk(" %02x", ((u8 *) iu->buf)[i]);
872 if ((i + 1) % 8 == 0)
873 printk("\n");
874 }
875
876 if (wc->byte_len % 8)
877 printk("\n");
878 }
879
880 switch (opcode) {
881 case SRP_RSP:
882 srp_process_rsp(target, iu->buf);
883 break;
884
885 case SRP_T_LOGOUT:
886 /* XXX Handle target logout */
David Dillow7aa54bd2008-01-07 18:23:41 -0500887 shost_printk(KERN_WARNING, target->scsi_host,
888 PFX "Got target logout request\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -0800889 break;
890
891 default:
David Dillow7aa54bd2008-01-07 18:23:41 -0500892 shost_printk(KERN_WARNING, target->scsi_host,
893 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800894 break;
895 }
896
Ralph Campbell85507bc2006-12-12 14:30:55 -0800897 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
898 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800899}
900
901static void srp_completion(struct ib_cq *cq, void *target_ptr)
902{
903 struct srp_target_port *target = target_ptr;
904 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800905
906 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
907 while (ib_poll_cq(cq, 1, &wc) > 0) {
908 if (wc.status) {
David Dillow7aa54bd2008-01-07 18:23:41 -0500909 shost_printk(KERN_ERR, target->scsi_host,
910 PFX "failed %s status %d\n",
911 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
912 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200913 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800914 break;
915 }
916
917 if (wc.wr_id & SRP_OP_RECV)
918 srp_handle_recv(target, &wc);
919 else
920 ++target->tx_tail;
921 }
922}
923
924static int __srp_post_recv(struct srp_target_port *target)
925{
926 struct srp_iu *iu;
927 struct ib_sge list;
928 struct ib_recv_wr wr, *bad_wr;
929 unsigned int next;
930 int ret;
931
932 next = target->rx_head & (SRP_RQ_SIZE - 1);
933 wr.wr_id = next | SRP_OP_RECV;
934 iu = target->rx_ring[next];
935
936 list.addr = iu->dma;
937 list.length = iu->size;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100938 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800939
940 wr.next = NULL;
941 wr.sg_list = &list;
942 wr.num_sge = 1;
943
944 ret = ib_post_recv(target->qp, &wr, &bad_wr);
945 if (!ret)
946 ++target->rx_head;
947
948 return ret;
949}
950
951static int srp_post_recv(struct srp_target_port *target)
952{
953 unsigned long flags;
954 int ret;
955
956 spin_lock_irqsave(target->scsi_host->host_lock, flags);
957 ret = __srp_post_recv(target);
958 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
959
960 return ret;
961}
962
963/*
964 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800965 * req_lim and tx_head. Lock cannot be dropped between call here and
966 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800967 */
David Dillow8cba2072007-12-19 17:08:43 -0500968static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
969 enum srp_request_type req_type)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800970{
David Dillow8cba2072007-12-19 17:08:43 -0500971 s32 min = (req_type == SRP_REQ_TASK_MGMT) ? 1 : 2;
972
Roland Dreieraef9ec32005-11-02 14:07:13 -0800973 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
974 return NULL;
975
David Dillow8cba2072007-12-19 17:08:43 -0500976 if (target->req_lim < min) {
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700977 ++target->zero_req_lim;
David Dillow8cba2072007-12-19 17:08:43 -0500978 return NULL;
979 }
Roland Dreier47f2bce2005-11-15 00:19:21 -0800980
Roland Dreieraef9ec32005-11-02 14:07:13 -0800981 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
982}
983
984/*
985 * Must be called with target->scsi_host->host_lock held to protect
986 * req_lim and tx_head.
987 */
988static int __srp_post_send(struct srp_target_port *target,
989 struct srp_iu *iu, int len)
990{
991 struct ib_sge list;
992 struct ib_send_wr wr, *bad_wr;
993 int ret = 0;
994
Roland Dreieraef9ec32005-11-02 14:07:13 -0800995 list.addr = iu->dma;
996 list.length = len;
Greg Kroah-Hartman05321932008-03-06 00:13:36 +0100997 list.lkey = target->srp_host->srp_dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800998
999 wr.next = NULL;
1000 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
1001 wr.sg_list = &list;
1002 wr.num_sge = 1;
1003 wr.opcode = IB_WR_SEND;
1004 wr.send_flags = IB_SEND_SIGNALED;
1005
1006 ret = ib_post_send(target->qp, &wr, &bad_wr);
1007
1008 if (!ret) {
1009 ++target->tx_head;
1010 --target->req_lim;
1011 }
1012
1013 return ret;
1014}
1015
1016static int srp_queuecommand(struct scsi_cmnd *scmnd,
1017 void (*done)(struct scsi_cmnd *))
1018{
1019 struct srp_target_port *target = host_to_target(scmnd->device->host);
1020 struct srp_request *req;
1021 struct srp_iu *iu;
1022 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001023 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001024 int len;
1025
1026 if (target->state == SRP_TARGET_CONNECTING)
1027 goto err;
1028
1029 if (target->state == SRP_TARGET_DEAD ||
1030 target->state == SRP_TARGET_REMOVED) {
1031 scmnd->result = DID_BAD_TARGET << 16;
1032 done(scmnd);
1033 return 0;
1034 }
1035
David Dillow8cba2072007-12-19 17:08:43 -05001036 iu = __srp_get_tx_iu(target, SRP_REQ_NORMAL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001037 if (!iu)
1038 goto err;
1039
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001040 dev = target->srp_host->srp_dev->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -08001041 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1042 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001043
Roland Dreierd945e1d2006-05-09 10:50:28 -07001044 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001045
1046 scmnd->scsi_done = done;
1047 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001048 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001049
1050 cmd = iu->buf;
1051 memset(cmd, 0, sizeof *cmd);
1052
1053 cmd->opcode = SRP_CMD;
1054 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001055 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001056 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1057
Roland Dreieraef9ec32005-11-02 14:07:13 -08001058 req->scmnd = scmnd;
1059 req->cmd = iu;
1060 req->cmd_done = 0;
1061 req->tsk_mgmt = NULL;
1062
1063 len = srp_map_data(scmnd, target, req);
1064 if (len < 0) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001065 shost_printk(KERN_ERR, target->scsi_host,
1066 PFX "Failed to map data\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001067 goto err;
1068 }
1069
1070 if (__srp_post_recv(target)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001071 shost_printk(KERN_ERR, target->scsi_host, PFX "Recv failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001072 goto err_unmap;
1073 }
1074
Ralph Campbell85507bc2006-12-12 14:30:55 -08001075 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1076 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001077
1078 if (__srp_post_send(target, iu, len)) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001079 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001080 goto err_unmap;
1081 }
1082
Roland Dreierd945e1d2006-05-09 10:50:28 -07001083 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001084
1085 return 0;
1086
1087err_unmap:
1088 srp_unmap_data(scmnd, target, req);
1089
1090err:
1091 return SCSI_MLQUEUE_HOST_BUSY;
1092}
1093
1094static int srp_alloc_iu_bufs(struct srp_target_port *target)
1095{
1096 int i;
1097
1098 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1099 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1100 target->max_ti_iu_len,
1101 GFP_KERNEL, DMA_FROM_DEVICE);
1102 if (!target->rx_ring[i])
1103 goto err;
1104 }
1105
1106 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1107 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001108 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001109 GFP_KERNEL, DMA_TO_DEVICE);
1110 if (!target->tx_ring[i])
1111 goto err;
1112 }
1113
1114 return 0;
1115
1116err:
1117 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1118 srp_free_iu(target->srp_host, target->rx_ring[i]);
1119 target->rx_ring[i] = NULL;
1120 }
1121
1122 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1123 srp_free_iu(target->srp_host, target->tx_ring[i]);
1124 target->tx_ring[i] = NULL;
1125 }
1126
1127 return -ENOMEM;
1128}
1129
1130static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1131 struct ib_cm_event *event,
1132 struct srp_target_port *target)
1133{
David Dillow7aa54bd2008-01-07 18:23:41 -05001134 struct Scsi_Host *shost = target->scsi_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001135 struct ib_class_port_info *cpi;
1136 int opcode;
1137
1138 switch (event->param.rej_rcvd.reason) {
1139 case IB_CM_REJ_PORT_CM_REDIRECT:
1140 cpi = event->param.rej_rcvd.ari;
1141 target->path.dlid = cpi->redirect_lid;
1142 target->path.pkey = cpi->redirect_pkey;
1143 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1144 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1145
1146 target->status = target->path.dlid ?
1147 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1148 break;
1149
1150 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001151 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001152 /*
1153 * Topspin/Cisco SRP gateways incorrectly send
1154 * reject reason code 25 when they mean 24
1155 * (port redirect).
1156 */
1157 memcpy(target->path.dgid.raw,
1158 event->param.rej_rcvd.ari, 16);
1159
David Dillow7aa54bd2008-01-07 18:23:41 -05001160 shost_printk(KERN_DEBUG, shost,
1161 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1162 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1163 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
Roland Dreieraef9ec32005-11-02 14:07:13 -08001164
1165 target->status = SRP_PORT_REDIRECT;
1166 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001167 shost_printk(KERN_WARNING, shost,
1168 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001169 target->status = -ECONNRESET;
1170 }
1171 break;
1172
1173 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
David Dillow7aa54bd2008-01-07 18:23:41 -05001174 shost_printk(KERN_WARNING, shost,
1175 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001176 target->status = -ECONNRESET;
1177 break;
1178
1179 case IB_CM_REJ_CONSUMER_DEFINED:
1180 opcode = *(u8 *) event->private_data;
1181 if (opcode == SRP_LOGIN_REJ) {
1182 struct srp_login_rej *rej = event->private_data;
1183 u32 reason = be32_to_cpu(rej->reason);
1184
1185 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
David Dillow7aa54bd2008-01-07 18:23:41 -05001186 shost_printk(KERN_WARNING, shost,
1187 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001188 else
David Dillow7aa54bd2008-01-07 18:23:41 -05001189 shost_printk(KERN_WARNING, shost,
1190 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001191 } else
David Dillow7aa54bd2008-01-07 18:23:41 -05001192 shost_printk(KERN_WARNING, shost,
1193 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1194 " opcode 0x%02x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001195 target->status = -ECONNRESET;
1196 break;
1197
David Dillow9fe4bcf2008-01-08 17:08:52 -05001198 case IB_CM_REJ_STALE_CONN:
1199 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1200 target->status = SRP_STALE_CONN;
1201 break;
1202
Roland Dreieraef9ec32005-11-02 14:07:13 -08001203 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001204 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1205 event->param.rej_rcvd.reason);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001206 target->status = -ECONNRESET;
1207 }
1208}
1209
1210static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1211{
1212 struct srp_target_port *target = cm_id->context;
1213 struct ib_qp_attr *qp_attr = NULL;
1214 int attr_mask = 0;
1215 int comp = 0;
1216 int opcode = 0;
1217
1218 switch (event->event) {
1219 case IB_CM_REQ_ERROR:
David Dillow7aa54bd2008-01-07 18:23:41 -05001220 shost_printk(KERN_DEBUG, target->scsi_host,
1221 PFX "Sending CM REQ failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001222 comp = 1;
1223 target->status = -ECONNRESET;
1224 break;
1225
1226 case IB_CM_REP_RECEIVED:
1227 comp = 1;
1228 opcode = *(u8 *) event->private_data;
1229
1230 if (opcode == SRP_LOGIN_RSP) {
1231 struct srp_login_rsp *rsp = event->private_data;
1232
1233 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1234 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1235
1236 target->scsi_host->can_queue = min(target->req_lim,
1237 target->scsi_host->can_queue);
1238 } else {
David Dillow7aa54bd2008-01-07 18:23:41 -05001239 shost_printk(KERN_WARNING, target->scsi_host,
1240 PFX "Unhandled RSP opcode %#x\n", opcode);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001241 target->status = -ECONNRESET;
1242 break;
1243 }
1244
Vu Phamd2fcea72006-11-21 14:14:10 -08001245 if (!target->rx_ring[0]) {
1246 target->status = srp_alloc_iu_bufs(target);
1247 if (target->status)
1248 break;
1249 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001250
1251 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1252 if (!qp_attr) {
1253 target->status = -ENOMEM;
1254 break;
1255 }
1256
1257 qp_attr->qp_state = IB_QPS_RTR;
1258 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1259 if (target->status)
1260 break;
1261
1262 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1263 if (target->status)
1264 break;
1265
1266 target->status = srp_post_recv(target);
1267 if (target->status)
1268 break;
1269
1270 qp_attr->qp_state = IB_QPS_RTS;
1271 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1272 if (target->status)
1273 break;
1274
1275 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1276 if (target->status)
1277 break;
1278
1279 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1280 if (target->status)
1281 break;
1282
1283 break;
1284
1285 case IB_CM_REJ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001286 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001287 comp = 1;
1288
1289 srp_cm_rej_handler(cm_id, event, target);
1290 break;
1291
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001292 case IB_CM_DREQ_RECEIVED:
David Dillow7aa54bd2008-01-07 18:23:41 -05001293 shost_printk(KERN_WARNING, target->scsi_host,
1294 PFX "DREQ received - connection closed\n");
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001295 if (ib_send_cm_drep(cm_id, NULL, 0))
David Dillow7aa54bd2008-01-07 18:23:41 -05001296 shost_printk(KERN_ERR, target->scsi_host,
1297 PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001298 break;
1299
1300 case IB_CM_TIMEWAIT_EXIT:
David Dillow7aa54bd2008-01-07 18:23:41 -05001301 shost_printk(KERN_ERR, target->scsi_host,
1302 PFX "connection closed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001303
1304 comp = 1;
1305 target->status = 0;
1306 break;
1307
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001308 case IB_CM_MRA_RECEIVED:
1309 case IB_CM_DREQ_ERROR:
1310 case IB_CM_DREP_RECEIVED:
1311 break;
1312
Roland Dreieraef9ec32005-11-02 14:07:13 -08001313 default:
David Dillow7aa54bd2008-01-07 18:23:41 -05001314 shost_printk(KERN_WARNING, target->scsi_host,
1315 PFX "Unhandled CM event %d\n", event->event);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001316 break;
1317 }
1318
1319 if (comp)
1320 complete(&target->done);
1321
1322 kfree(qp_attr);
1323
1324 return 0;
1325}
1326
Roland Dreierd945e1d2006-05-09 10:50:28 -07001327static int srp_send_tsk_mgmt(struct srp_target_port *target,
1328 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001329{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001330 struct srp_iu *iu;
1331 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001332
1333 spin_lock_irq(target->scsi_host->host_lock);
1334
Roland Dreier1285b3a2006-03-03 15:47:25 -08001335 if (target->state == SRP_TARGET_DEAD ||
1336 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001337 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001338 goto out;
1339 }
1340
Roland Dreieraef9ec32005-11-02 14:07:13 -08001341 init_completion(&req->done);
1342
David Dillow8cba2072007-12-19 17:08:43 -05001343 iu = __srp_get_tx_iu(target, SRP_REQ_TASK_MGMT);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001344 if (!iu)
1345 goto out;
1346
1347 tsk_mgmt = iu->buf;
1348 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1349
1350 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001351 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1352 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001353 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001354 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001355
1356 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1357 goto out;
1358
1359 req->tsk_mgmt = iu;
1360
1361 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001362
Roland Dreieraef9ec32005-11-02 14:07:13 -08001363 if (!wait_for_completion_timeout(&req->done,
1364 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001365 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001366
Roland Dreierd945e1d2006-05-09 10:50:28 -07001367 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001368
1369out:
1370 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001371 return -1;
1372}
1373
1374static int srp_find_req(struct srp_target_port *target,
1375 struct scsi_cmnd *scmnd,
1376 struct srp_request **req)
1377{
1378 if (scmnd->host_scribble == (void *) -1L)
1379 return -1;
1380
1381 *req = &target->req_ring[(long) scmnd->host_scribble];
1382
1383 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001384}
1385
1386static int srp_abort(struct scsi_cmnd *scmnd)
1387{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001388 struct srp_target_port *target = host_to_target(scmnd->device->host);
1389 struct srp_request *req;
1390 int ret = SUCCESS;
1391
David Dillow7aa54bd2008-01-07 18:23:41 -05001392 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001393
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001394 if (target->qp_in_error)
1395 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001396 if (srp_find_req(target, scmnd, &req))
1397 return FAILED;
1398 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1399 return FAILED;
1400
1401 spin_lock_irq(target->scsi_host->host_lock);
1402
1403 if (req->cmd_done) {
1404 srp_remove_req(target, req);
1405 scmnd->scsi_done(scmnd);
1406 } else if (!req->tsk_status) {
1407 srp_remove_req(target, req);
1408 scmnd->result = DID_ABORT << 16;
1409 } else
1410 ret = FAILED;
1411
1412 spin_unlock_irq(target->scsi_host->host_lock);
1413
1414 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001415}
1416
1417static int srp_reset_device(struct scsi_cmnd *scmnd)
1418{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001419 struct srp_target_port *target = host_to_target(scmnd->device->host);
1420 struct srp_request *req, *tmp;
1421
David Dillow7aa54bd2008-01-07 18:23:41 -05001422 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device 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_LUN_RESET))
1429 return FAILED;
1430 if (req->tsk_status)
1431 return FAILED;
1432
1433 spin_lock_irq(target->scsi_host->host_lock);
1434
1435 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001436 if (req->scmnd->device == scmnd->device)
1437 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001438
1439 spin_unlock_irq(target->scsi_host->host_lock);
1440
1441 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001442}
1443
1444static int srp_reset_host(struct scsi_cmnd *scmnd)
1445{
1446 struct srp_target_port *target = host_to_target(scmnd->device->host);
1447 int ret = FAILED;
1448
David Dillow7aa54bd2008-01-07 18:23:41 -05001449 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001450
1451 if (!srp_reconnect_target(target))
1452 ret = SUCCESS;
1453
1454 return ret;
1455}
1456
Tony Jonesee959b02008-02-22 00:13:36 +01001457static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1458 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001459{
Tony Jonesee959b02008-02-22 00:13:36 +01001460 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001461
1462 if (target->state == SRP_TARGET_DEAD ||
1463 target->state == SRP_TARGET_REMOVED)
1464 return -ENODEV;
1465
1466 return sprintf(buf, "0x%016llx\n",
1467 (unsigned long long) be64_to_cpu(target->id_ext));
1468}
1469
Tony Jonesee959b02008-02-22 00:13:36 +01001470static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1471 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001472{
Tony Jonesee959b02008-02-22 00:13:36 +01001473 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001474
1475 if (target->state == SRP_TARGET_DEAD ||
1476 target->state == SRP_TARGET_REMOVED)
1477 return -ENODEV;
1478
1479 return sprintf(buf, "0x%016llx\n",
1480 (unsigned long long) be64_to_cpu(target->ioc_guid));
1481}
1482
Tony Jonesee959b02008-02-22 00:13:36 +01001483static ssize_t show_service_id(struct device *dev,
1484 struct device_attribute *attr, char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001485{
Tony Jonesee959b02008-02-22 00:13:36 +01001486 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001487
1488 if (target->state == SRP_TARGET_DEAD ||
1489 target->state == SRP_TARGET_REMOVED)
1490 return -ENODEV;
1491
1492 return sprintf(buf, "0x%016llx\n",
1493 (unsigned long long) be64_to_cpu(target->service_id));
1494}
1495
Tony Jonesee959b02008-02-22 00:13:36 +01001496static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1497 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001498{
Tony Jonesee959b02008-02-22 00:13:36 +01001499 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001500
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
Tony Jonesee959b02008-02-22 00:13:36 +01001508static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1509 char *buf)
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001510{
Tony Jonesee959b02008-02-22 00:13:36 +01001511 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001512
1513 if (target->state == SRP_TARGET_DEAD ||
1514 target->state == SRP_TARGET_REMOVED)
1515 return -ENODEV;
1516
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001517 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001518}
1519
Tony Jonesee959b02008-02-22 00:13:36 +01001520static ssize_t show_orig_dgid(struct device *dev,
1521 struct device_attribute *attr, char *buf)
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001522{
Tony Jonesee959b02008-02-22 00:13:36 +01001523 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001524
1525 if (target->state == SRP_TARGET_DEAD ||
1526 target->state == SRP_TARGET_REMOVED)
1527 return -ENODEV;
1528
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001529 return sprintf(buf, "%pI6\n", target->orig_dgid);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001530}
1531
Tony Jonesee959b02008-02-22 00:13:36 +01001532static ssize_t show_zero_req_lim(struct device *dev,
1533 struct device_attribute *attr, char *buf)
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001534{
Tony Jonesee959b02008-02-22 00:13:36 +01001535 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001536
1537 if (target->state == SRP_TARGET_DEAD ||
1538 target->state == SRP_TARGET_REMOVED)
1539 return -ENODEV;
1540
1541 return sprintf(buf, "%d\n", target->zero_req_lim);
1542}
1543
Tony Jonesee959b02008-02-22 00:13:36 +01001544static ssize_t show_local_ib_port(struct device *dev,
1545 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001546{
Tony Jonesee959b02008-02-22 00:13:36 +01001547 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001548
1549 return sprintf(buf, "%d\n", target->srp_host->port);
1550}
1551
Tony Jonesee959b02008-02-22 00:13:36 +01001552static ssize_t show_local_ib_device(struct device *dev,
1553 struct device_attribute *attr, char *buf)
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001554{
Tony Jonesee959b02008-02-22 00:13:36 +01001555 struct srp_target_port *target = host_to_target(class_to_shost(dev));
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001556
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001557 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001558}
1559
Tony Jonesee959b02008-02-22 00:13:36 +01001560static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1561static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1562static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1563static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1564static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1565static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1566static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1567static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1568static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001569
Tony Jonesee959b02008-02-22 00:13:36 +01001570static struct device_attribute *srp_host_attrs[] = {
1571 &dev_attr_id_ext,
1572 &dev_attr_ioc_guid,
1573 &dev_attr_service_id,
1574 &dev_attr_pkey,
1575 &dev_attr_dgid,
1576 &dev_attr_orig_dgid,
1577 &dev_attr_zero_req_lim,
1578 &dev_attr_local_ib_port,
1579 &dev_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001580 NULL
1581};
1582
Roland Dreieraef9ec32005-11-02 14:07:13 -08001583static struct scsi_host_template srp_template = {
1584 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001585 .name = "InfiniBand SRP initiator",
1586 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001587 .info = srp_target_info,
1588 .queuecommand = srp_queuecommand,
1589 .eh_abort_handler = srp_abort,
1590 .eh_device_reset_handler = srp_reset_device,
1591 .eh_host_reset_handler = srp_reset_host,
1592 .can_queue = SRP_SQ_SIZE,
1593 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001594 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001595 .use_clustering = ENABLE_CLUSTERING,
1596 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001597};
1598
1599static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1600{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001601 struct srp_rport_identifiers ids;
1602 struct srp_rport *rport;
1603
Roland Dreieraef9ec32005-11-02 14:07:13 -08001604 sprintf(target->target_name, "SRP.T10:%016llX",
1605 (unsigned long long) be64_to_cpu(target->id_ext));
1606
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001607 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001608 return -ENODEV;
1609
FUJITA Tomonori32368222007-06-27 16:33:12 +09001610 memcpy(ids.port_id, &target->id_ext, 8);
1611 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001612 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001613 rport = srp_rport_add(target->scsi_host, &ids);
1614 if (IS_ERR(rport)) {
1615 scsi_remove_host(target->scsi_host);
1616 return PTR_ERR(rport);
1617 }
1618
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001619 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001620 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001621 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001622
1623 target->state = SRP_TARGET_LIVE;
1624
Roland Dreieraef9ec32005-11-02 14:07:13 -08001625 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001626 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001627
1628 return 0;
1629}
1630
Tony Jonesee959b02008-02-22 00:13:36 +01001631static void srp_release_dev(struct device *dev)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001632{
1633 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001634 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001635
1636 complete(&host->released);
1637}
1638
1639static struct class srp_class = {
1640 .name = "infiniband_srp",
Tony Jonesee959b02008-02-22 00:13:36 +01001641 .dev_release = srp_release_dev
Roland Dreieraef9ec32005-11-02 14:07:13 -08001642};
1643
1644/*
1645 * Target ports are added by writing
1646 *
1647 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1648 * pkey=<P_Key>,service_id=<service ID>
1649 *
1650 * to the add_target sysfs attribute.
1651 */
1652enum {
1653 SRP_OPT_ERR = 0,
1654 SRP_OPT_ID_EXT = 1 << 0,
1655 SRP_OPT_IOC_GUID = 1 << 1,
1656 SRP_OPT_DGID = 1 << 2,
1657 SRP_OPT_PKEY = 1 << 3,
1658 SRP_OPT_SERVICE_ID = 1 << 4,
1659 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001660 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001661 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001662 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001663 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1664 SRP_OPT_IOC_GUID |
1665 SRP_OPT_DGID |
1666 SRP_OPT_PKEY |
1667 SRP_OPT_SERVICE_ID),
1668};
1669
Steven Whitehousea447c092008-10-13 10:46:57 +01001670static const match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001671 { SRP_OPT_ID_EXT, "id_ext=%s" },
1672 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1673 { SRP_OPT_DGID, "dgid=%s" },
1674 { SRP_OPT_PKEY, "pkey=%x" },
1675 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1676 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1677 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001678 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001679 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001680 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001681};
1682
1683static int srp_parse_options(const char *buf, struct srp_target_port *target)
1684{
1685 char *options, *sep_opt;
1686 char *p;
1687 char dgid[3];
1688 substring_t args[MAX_OPT_ARGS];
1689 int opt_mask = 0;
1690 int token;
1691 int ret = -EINVAL;
1692 int i;
1693
1694 options = kstrdup(buf, GFP_KERNEL);
1695 if (!options)
1696 return -ENOMEM;
1697
1698 sep_opt = options;
1699 while ((p = strsep(&sep_opt, ",")) != NULL) {
1700 if (!*p)
1701 continue;
1702
1703 token = match_token(p, srp_opt_tokens, args);
1704 opt_mask |= token;
1705
1706 switch (token) {
1707 case SRP_OPT_ID_EXT:
1708 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001709 if (!p) {
1710 ret = -ENOMEM;
1711 goto out;
1712 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001713 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1714 kfree(p);
1715 break;
1716
1717 case SRP_OPT_IOC_GUID:
1718 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001719 if (!p) {
1720 ret = -ENOMEM;
1721 goto out;
1722 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001723 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1724 kfree(p);
1725 break;
1726
1727 case SRP_OPT_DGID:
1728 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001729 if (!p) {
1730 ret = -ENOMEM;
1731 goto out;
1732 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001733 if (strlen(p) != 32) {
1734 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001735 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001736 goto out;
1737 }
1738
1739 for (i = 0; i < 16; ++i) {
1740 strlcpy(dgid, p + i * 2, 3);
1741 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1742 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001743 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001744 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001745 break;
1746
1747 case SRP_OPT_PKEY:
1748 if (match_hex(args, &token)) {
1749 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1750 goto out;
1751 }
1752 target->path.pkey = cpu_to_be16(token);
1753 break;
1754
1755 case SRP_OPT_SERVICE_ID:
1756 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001757 if (!p) {
1758 ret = -ENOMEM;
1759 goto out;
1760 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001761 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
Sean Hefty247e0202007-08-08 15:51:18 -07001762 target->path.service_id = target->service_id;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001763 kfree(p);
1764 break;
1765
1766 case SRP_OPT_MAX_SECT:
1767 if (match_int(args, &token)) {
1768 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1769 goto out;
1770 }
1771 target->scsi_host->max_sectors = token;
1772 break;
1773
Vu Pham52fb2b502006-06-17 20:37:31 -07001774 case SRP_OPT_MAX_CMD_PER_LUN:
1775 if (match_int(args, &token)) {
1776 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1777 goto out;
1778 }
1779 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1780 break;
1781
Ramachandra K0c0450db2006-06-17 20:37:38 -07001782 case SRP_OPT_IO_CLASS:
1783 if (match_hex(args, &token)) {
1784 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1785 goto out;
1786 }
1787 if (token != SRP_REV10_IB_IO_CLASS &&
1788 token != SRP_REV16A_IB_IO_CLASS) {
1789 printk(KERN_WARNING PFX "unknown IO class parameter value"
1790 " %x specified (use %x or %x).\n",
1791 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1792 goto out;
1793 }
1794 target->io_class = token;
1795 break;
1796
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001797 case SRP_OPT_INITIATOR_EXT:
1798 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001799 if (!p) {
1800 ret = -ENOMEM;
1801 goto out;
1802 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001803 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1804 kfree(p);
1805 break;
1806
Roland Dreieraef9ec32005-11-02 14:07:13 -08001807 default:
1808 printk(KERN_WARNING PFX "unknown parameter or missing value "
1809 "'%s' in target creation request\n", p);
1810 goto out;
1811 }
1812 }
1813
1814 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1815 ret = 0;
1816 else
1817 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1818 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1819 !(srp_opt_tokens[i].token & opt_mask))
1820 printk(KERN_WARNING PFX "target creation request is "
1821 "missing parameter '%s'\n",
1822 srp_opt_tokens[i].pattern);
1823
1824out:
1825 kfree(options);
1826 return ret;
1827}
1828
Tony Jonesee959b02008-02-22 00:13:36 +01001829static ssize_t srp_create_target(struct device *dev,
1830 struct device_attribute *attr,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001831 const char *buf, size_t count)
1832{
1833 struct srp_host *host =
Tony Jonesee959b02008-02-22 00:13:36 +01001834 container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001835 struct Scsi_Host *target_host;
1836 struct srp_target_port *target;
1837 int ret;
1838 int i;
1839
1840 target_host = scsi_host_alloc(&srp_template,
1841 sizeof (struct srp_target_port));
1842 if (!target_host)
1843 return -ENOMEM;
1844
FUJITA Tomonori32368222007-06-27 16:33:12 +09001845 target_host->transportt = ib_srp_transport_template;
Arne Redlich3c8edf02006-11-15 12:43:00 +01001846 target_host->max_lun = SRP_MAX_LUN;
1847 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001848
Roland Dreieraef9ec32005-11-02 14:07:13 -08001849 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001850
Ramachandra K0c0450db2006-06-17 20:37:38 -07001851 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001852 target->scsi_host = target_host;
1853 target->srp_host = host;
1854
Roland Dreierd945e1d2006-05-09 10:50:28 -07001855 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001856 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001857 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1858 target->req_ring[i].index = i;
1859 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1860 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001861
1862 ret = srp_parse_options(buf, target);
1863 if (ret)
1864 goto err;
1865
Roland Dreier969a60f2008-07-14 23:48:43 -07001866 ib_query_gid(host->srp_dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001867
David Dillow7aa54bd2008-01-07 18:23:41 -05001868 shost_printk(KERN_DEBUG, target->scsi_host, PFX
1869 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001870 "service_id %016llx dgid %pI6\n",
Roland Dreieraef9ec32005-11-02 14:07:13 -08001871 (unsigned long long) be64_to_cpu(target->id_ext),
1872 (unsigned long long) be64_to_cpu(target->ioc_guid),
1873 be16_to_cpu(target->path.pkey),
1874 (unsigned long long) be64_to_cpu(target->service_id),
Harvey Harrison8867cd72008-10-28 22:36:33 -07001875 target->path.dgid.raw);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001876
1877 ret = srp_create_target_ib(target);
1878 if (ret)
1879 goto err;
1880
David Dillow9fe4bcf2008-01-08 17:08:52 -05001881 ret = srp_new_cm_id(target);
1882 if (ret)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001883 goto err_free;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001884
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001885 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001886 ret = srp_connect_target(target);
1887 if (ret) {
David Dillow7aa54bd2008-01-07 18:23:41 -05001888 shost_printk(KERN_ERR, target->scsi_host,
1889 PFX "Connection failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001890 goto err_cm_id;
1891 }
1892
1893 ret = srp_add_target(host, target);
1894 if (ret)
1895 goto err_disconnect;
1896
1897 return count;
1898
1899err_disconnect:
1900 srp_disconnect_target(target);
1901
1902err_cm_id:
1903 ib_destroy_cm_id(target->cm_id);
1904
1905err_free:
1906 srp_free_target_ib(target);
1907
1908err:
1909 scsi_host_put(target_host);
1910
1911 return ret;
1912}
1913
Tony Jonesee959b02008-02-22 00:13:36 +01001914static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001915
Tony Jonesee959b02008-02-22 00:13:36 +01001916static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1917 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001918{
Tony Jonesee959b02008-02-22 00:13:36 +01001919 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001920
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001921 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001922}
1923
Tony Jonesee959b02008-02-22 00:13:36 +01001924static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001925
Tony Jonesee959b02008-02-22 00:13:36 +01001926static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1927 char *buf)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001928{
Tony Jonesee959b02008-02-22 00:13:36 +01001929 struct srp_host *host = container_of(dev, struct srp_host, dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001930
1931 return sprintf(buf, "%d\n", host->port);
1932}
1933
Tony Jonesee959b02008-02-22 00:13:36 +01001934static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001935
Roland Dreierf5358a12006-06-17 20:37:29 -07001936static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001937{
1938 struct srp_host *host;
1939
1940 host = kzalloc(sizeof *host, GFP_KERNEL);
1941 if (!host)
1942 return NULL;
1943
1944 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001945 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001946 init_completion(&host->released);
Greg Kroah-Hartman05321932008-03-06 00:13:36 +01001947 host->srp_dev = device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001948 host->port = port;
1949
Tony Jonesee959b02008-02-22 00:13:36 +01001950 host->dev.class = &srp_class;
1951 host->dev.parent = device->dev->dma_device;
1952 snprintf(host->dev.bus_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001953 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001954
Tony Jonesee959b02008-02-22 00:13:36 +01001955 if (device_register(&host->dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001956 goto free_host;
Tony Jonesee959b02008-02-22 00:13:36 +01001957 if (device_create_file(&host->dev, &dev_attr_add_target))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001958 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001959 if (device_create_file(&host->dev, &dev_attr_ibdev))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001960 goto err_class;
Tony Jonesee959b02008-02-22 00:13:36 +01001961 if (device_create_file(&host->dev, &dev_attr_port))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001962 goto err_class;
1963
1964 return host;
1965
1966err_class:
Tony Jonesee959b02008-02-22 00:13:36 +01001967 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001968
Roland Dreierf5358a12006-06-17 20:37:29 -07001969free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001970 kfree(host);
1971
1972 return NULL;
1973}
1974
1975static void srp_add_one(struct ib_device *device)
1976{
Roland Dreierf5358a12006-06-17 20:37:29 -07001977 struct srp_device *srp_dev;
1978 struct ib_device_attr *dev_attr;
1979 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001980 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001981 int s, e, p;
1982
Roland Dreierf5358a12006-06-17 20:37:29 -07001983 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1984 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001985 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001986
Roland Dreierf5358a12006-06-17 20:37:29 -07001987 if (ib_query_device(device, dev_attr)) {
1988 printk(KERN_WARNING PFX "Query device failed for %s\n",
1989 device->name);
1990 goto free_attr;
1991 }
1992
1993 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1994 if (!srp_dev)
1995 goto free_attr;
1996
1997 /*
1998 * Use the smallest page size supported by the HCA, down to a
1999 * minimum of 512 bytes (which is the smallest sector that a
2000 * SCSI command will ever carry).
2001 */
2002 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
2003 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08002004 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07002005
2006 INIT_LIST_HEAD(&srp_dev->dev_list);
2007
2008 srp_dev->dev = device;
2009 srp_dev->pd = ib_alloc_pd(device);
2010 if (IS_ERR(srp_dev->pd))
2011 goto free_dev;
2012
2013 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2014 IB_ACCESS_LOCAL_WRITE |
2015 IB_ACCESS_REMOTE_READ |
2016 IB_ACCESS_REMOTE_WRITE);
2017 if (IS_ERR(srp_dev->mr))
2018 goto err_pd;
2019
2020 memset(&fmr_param, 0, sizeof fmr_param);
2021 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2022 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2023 fmr_param.cache = 1;
2024 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
2025 fmr_param.page_shift = srp_dev->fmr_page_shift;
2026 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2027 IB_ACCESS_REMOTE_WRITE |
2028 IB_ACCESS_REMOTE_READ);
2029
2030 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2031 if (IS_ERR(srp_dev->fmr_pool))
2032 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002033
Tom Tucker07ebafb2006-08-03 16:02:42 -05002034 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002035 s = 0;
2036 e = 0;
2037 } else {
2038 s = 1;
2039 e = device->phys_port_cnt;
2040 }
2041
2042 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07002043 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002044 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07002045 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002046 }
2047
Roland Dreierf5358a12006-06-17 20:37:29 -07002048 ib_set_client_data(device, &srp_client, srp_dev);
2049
2050 goto free_attr;
2051
2052err_pd:
2053 ib_dealloc_pd(srp_dev->pd);
2054
2055free_dev:
2056 kfree(srp_dev);
2057
2058free_attr:
2059 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002060}
2061
2062static void srp_remove_one(struct ib_device *device)
2063{
Roland Dreierf5358a12006-06-17 20:37:29 -07002064 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002065 struct srp_host *host, *tmp_host;
2066 LIST_HEAD(target_list);
2067 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002068
Roland Dreierf5358a12006-06-17 20:37:29 -07002069 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002070
Roland Dreierf5358a12006-06-17 20:37:29 -07002071 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Tony Jonesee959b02008-02-22 00:13:36 +01002072 device_unregister(&host->dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002073 /*
2074 * Wait for the sysfs entry to go away, so that no new
2075 * target ports can be created.
2076 */
2077 wait_for_completion(&host->released);
2078
2079 /*
2080 * Mark all target ports as removed, so we stop queueing
2081 * commands and don't try to reconnect.
2082 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002083 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002084 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002085 spin_lock_irq(target->scsi_host->host_lock);
2086 target->state = SRP_TARGET_REMOVED;
2087 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002088 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002089 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002090
2091 /*
2092 * Wait for any reconnection tasks that may have
2093 * started before we marked our target ports as
2094 * removed, and any target port removal tasks.
2095 */
2096 flush_scheduled_work();
2097
2098 list_for_each_entry_safe(target, tmp_target,
2099 &host->target_list, list) {
David Dillowb0e47c82008-01-03 10:25:27 -08002100 srp_remove_host(target->scsi_host);
Dave Dillowad696982008-01-03 22:35:41 -05002101 scsi_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002102 srp_disconnect_target(target);
2103 ib_destroy_cm_id(target->cm_id);
2104 srp_free_target_ib(target);
2105 scsi_host_put(target->scsi_host);
2106 }
2107
Roland Dreieraef9ec32005-11-02 14:07:13 -08002108 kfree(host);
2109 }
2110
Roland Dreierf5358a12006-06-17 20:37:29 -07002111 if (srp_dev->fmr_pool)
2112 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2113 ib_dereg_mr(srp_dev->mr);
2114 ib_dealloc_pd(srp_dev->pd);
2115
2116 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002117}
2118
FUJITA Tomonori32368222007-06-27 16:33:12 +09002119static struct srp_function_template ib_srp_transport_functions = {
2120};
2121
Roland Dreieraef9ec32005-11-02 14:07:13 -08002122static int __init srp_init_module(void)
2123{
2124 int ret;
2125
David Dillow1e89a192008-04-16 21:01:12 -07002126 if (srp_sg_tablesize > 255) {
2127 printk(KERN_WARNING PFX "Clamping srp_sg_tablesize to 255\n");
2128 srp_sg_tablesize = 255;
2129 }
2130
FUJITA Tomonori32368222007-06-27 16:33:12 +09002131 ib_srp_transport_template =
2132 srp_attach_transport(&ib_srp_transport_functions);
2133 if (!ib_srp_transport_template)
2134 return -ENOMEM;
2135
Vu Pham74b0a152006-06-17 20:37:32 -07002136 srp_template.sg_tablesize = srp_sg_tablesize;
2137 srp_max_iu_len = (sizeof (struct srp_cmd) +
2138 sizeof (struct srp_indirect_buf) +
2139 srp_sg_tablesize * 16);
2140
Roland Dreieraef9ec32005-11-02 14:07:13 -08002141 ret = class_register(&srp_class);
2142 if (ret) {
2143 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002144 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002145 return ret;
2146 }
2147
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002148 ib_sa_register_client(&srp_sa_client);
2149
Roland Dreieraef9ec32005-11-02 14:07:13 -08002150 ret = ib_register_client(&srp_client);
2151 if (ret) {
2152 printk(KERN_ERR PFX "couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002153 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002154 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002155 class_unregister(&srp_class);
2156 return ret;
2157 }
2158
2159 return 0;
2160}
2161
2162static void __exit srp_cleanup_module(void)
2163{
2164 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002165 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002166 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002167 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002168}
2169
2170module_init(srp_init_module);
2171module_exit(srp_cleanup_module);