blob: fb6c5798daf3852cfbbb6ed28590395671d034a4 [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>
50
51#include <rdma/ib_cache.h>
52
53#include "ib_srp.h"
54
55#define DRV_NAME "ib_srp"
56#define PFX DRV_NAME ": "
57#define DRV_VERSION "0.2"
58#define DRV_RELDATE "November 1, 2005"
59
60MODULE_AUTHOR("Roland Dreier");
61MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63MODULE_LICENSE("Dual BSD/GPL");
64
Vu Pham74b0a152006-06-17 20:37:32 -070065static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
66static int srp_max_iu_len;
67
68module_param(srp_sg_tablesize, int, 0444);
69MODULE_PARM_DESC(srp_sg_tablesize,
70 "Max number of gather/scatter entries per I/O (default is 12)");
71
Roland Dreieraef9ec32005-11-02 14:07:13 -080072static int topspin_workarounds = 1;
73
74module_param(topspin_workarounds, int, 0444);
75MODULE_PARM_DESC(topspin_workarounds,
76 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
77
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070078static int mellanox_workarounds = 1;
79
80module_param(mellanox_workarounds, int, 0444);
81MODULE_PARM_DESC(mellanox_workarounds,
82 "Enable workarounds for Mellanox SRP target bugs if != 0");
83
Roland Dreieraef9ec32005-11-02 14:07:13 -080084static void srp_add_one(struct ib_device *device);
85static void srp_remove_one(struct ib_device *device);
86static void srp_completion(struct ib_cq *cq, void *target_ptr);
87static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
88
89static struct ib_client srp_client = {
90 .name = "srp",
91 .add = srp_add_one,
92 .remove = srp_remove_one
93};
94
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070095static struct ib_sa_client srp_sa_client;
96
Roland Dreieraef9ec32005-11-02 14:07:13 -080097static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
98{
99 return (struct srp_target_port *) host->hostdata;
100}
101
102static const char *srp_target_info(struct Scsi_Host *host)
103{
104 return host_to_target(host)->target_name;
105}
106
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700107static int srp_target_is_topspin(struct srp_target_port *target)
108{
109 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
110
111 return topspin_workarounds &&
112 !memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui);
113}
114
115static int srp_target_is_mellanox(struct srp_target_port *target)
116{
117 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
118
119 return mellanox_workarounds &&
120 !memcmp(&target->ioc_guid, mellanox_oui, sizeof mellanox_oui);
121}
122
Roland Dreieraef9ec32005-11-02 14:07:13 -0800123static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
124 gfp_t gfp_mask,
125 enum dma_data_direction direction)
126{
127 struct srp_iu *iu;
128
129 iu = kmalloc(sizeof *iu, gfp_mask);
130 if (!iu)
131 goto out;
132
133 iu->buf = kzalloc(size, gfp_mask);
134 if (!iu->buf)
135 goto out_free_iu;
136
Ralph Campbell85507bc2006-12-12 14:30:55 -0800137 iu->dma = ib_dma_map_single(host->dev->dev, iu->buf, size, direction);
138 if (ib_dma_mapping_error(host->dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800139 goto out_free_buf;
140
141 iu->size = size;
142 iu->direction = direction;
143
144 return iu;
145
146out_free_buf:
147 kfree(iu->buf);
148out_free_iu:
149 kfree(iu);
150out:
151 return NULL;
152}
153
154static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
155{
156 if (!iu)
157 return;
158
Ralph Campbell85507bc2006-12-12 14:30:55 -0800159 ib_dma_unmap_single(host->dev->dev, iu->dma, iu->size, iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800160 kfree(iu->buf);
161 kfree(iu);
162}
163
164static void srp_qp_event(struct ib_event *event, void *context)
165{
166 printk(KERN_ERR PFX "QP event %d\n", event->event);
167}
168
169static int srp_init_qp(struct srp_target_port *target,
170 struct ib_qp *qp)
171{
172 struct ib_qp_attr *attr;
173 int ret;
174
175 attr = kmalloc(sizeof *attr, GFP_KERNEL);
176 if (!attr)
177 return -ENOMEM;
178
Roland Dreierf5358a12006-06-17 20:37:29 -0700179 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800180 target->srp_host->port,
181 be16_to_cpu(target->path.pkey),
182 &attr->pkey_index);
183 if (ret)
184 goto out;
185
186 attr->qp_state = IB_QPS_INIT;
187 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
188 IB_ACCESS_REMOTE_WRITE);
189 attr->port_num = target->srp_host->port;
190
191 ret = ib_modify_qp(qp, attr,
192 IB_QP_STATE |
193 IB_QP_PKEY_INDEX |
194 IB_QP_ACCESS_FLAGS |
195 IB_QP_PORT);
196
197out:
198 kfree(attr);
199 return ret;
200}
201
202static int srp_create_target_ib(struct srp_target_port *target)
203{
204 struct ib_qp_init_attr *init_attr;
205 int ret;
206
207 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
208 if (!init_attr)
209 return -ENOMEM;
210
Roland Dreierf5358a12006-06-17 20:37:29 -0700211 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +0300212 NULL, target, SRP_CQ_SIZE, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800213 if (IS_ERR(target->cq)) {
214 ret = PTR_ERR(target->cq);
215 goto out;
216 }
217
218 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
219
220 init_attr->event_handler = srp_qp_event;
221 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
222 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
223 init_attr->cap.max_recv_sge = 1;
224 init_attr->cap.max_send_sge = 1;
225 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
226 init_attr->qp_type = IB_QPT_RC;
227 init_attr->send_cq = target->cq;
228 init_attr->recv_cq = target->cq;
229
Roland Dreierf5358a12006-06-17 20:37:29 -0700230 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800231 if (IS_ERR(target->qp)) {
232 ret = PTR_ERR(target->qp);
233 ib_destroy_cq(target->cq);
234 goto out;
235 }
236
237 ret = srp_init_qp(target, target->qp);
238 if (ret) {
239 ib_destroy_qp(target->qp);
240 ib_destroy_cq(target->cq);
241 goto out;
242 }
243
244out:
245 kfree(init_attr);
246 return ret;
247}
248
249static void srp_free_target_ib(struct srp_target_port *target)
250{
251 int i;
252
253 ib_destroy_qp(target->qp);
254 ib_destroy_cq(target->cq);
255
256 for (i = 0; i < SRP_RQ_SIZE; ++i)
257 srp_free_iu(target->srp_host, target->rx_ring[i]);
258 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
259 srp_free_iu(target->srp_host, target->tx_ring[i]);
260}
261
262static void srp_path_rec_completion(int status,
263 struct ib_sa_path_rec *pathrec,
264 void *target_ptr)
265{
266 struct srp_target_port *target = target_ptr;
267
268 target->status = status;
269 if (status)
270 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
271 else
272 target->path = *pathrec;
273 complete(&target->done);
274}
275
276static int srp_lookup_path(struct srp_target_port *target)
277{
278 target->path.numb_path = 1;
279
280 init_completion(&target->done);
281
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700282 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
283 target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800284 target->srp_host->port,
285 &target->path,
286 IB_SA_PATH_REC_DGID |
287 IB_SA_PATH_REC_SGID |
288 IB_SA_PATH_REC_NUMB_PATH |
289 IB_SA_PATH_REC_PKEY,
290 SRP_PATH_REC_TIMEOUT_MS,
291 GFP_KERNEL,
292 srp_path_rec_completion,
293 target, &target->path_query);
294 if (target->path_query_id < 0)
295 return target->path_query_id;
296
297 wait_for_completion(&target->done);
298
299 if (target->status < 0)
300 printk(KERN_WARNING PFX "Path record query failed\n");
301
302 return target->status;
303}
304
305static int srp_send_req(struct srp_target_port *target)
306{
307 struct {
308 struct ib_cm_req_param param;
309 struct srp_login_req priv;
310 } *req = NULL;
311 int status;
312
313 req = kzalloc(sizeof *req, GFP_KERNEL);
314 if (!req)
315 return -ENOMEM;
316
317 req->param.primary_path = &target->path;
318 req->param.alternate_path = NULL;
319 req->param.service_id = target->service_id;
320 req->param.qp_num = target->qp->qp_num;
321 req->param.qp_type = target->qp->qp_type;
322 req->param.private_data = &req->priv;
323 req->param.private_data_len = sizeof req->priv;
324 req->param.flow_control = 1;
325
326 get_random_bytes(&req->param.starting_psn, 4);
327 req->param.starting_psn &= 0xffffff;
328
329 /*
330 * Pick some arbitrary defaults here; we could make these
331 * module parameters if anyone cared about setting them.
332 */
333 req->param.responder_resources = 4;
334 req->param.remote_cm_response_timeout = 20;
335 req->param.local_cm_response_timeout = 20;
336 req->param.retry_count = 7;
337 req->param.rnr_retry_count = 7;
338 req->param.max_cm_retries = 15;
339
340 req->priv.opcode = SRP_LOGIN_REQ;
341 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700342 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800343 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
344 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700345 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700346 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700347 * port identifier format is 8 bytes of ID extension followed
348 * by 8 bytes of GUID. Older drafts put the two halves in the
349 * opposite order, so that the GUID comes first.
350 *
351 * Targets conforming to these obsolete drafts can be
352 * recognized by the I/O Class they report.
353 */
354 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
355 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200356 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700357 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200358 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700359 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
360 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
361 } else {
362 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200363 &target->initiator_ext, 8);
364 memcpy(req->priv.initiator_port_id + 8,
365 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700366 memcpy(req->priv.target_port_id, &target->id_ext, 8);
367 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
368 }
369
Roland Dreieraef9ec32005-11-02 14:07:13 -0800370 /*
371 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200372 * zero out the first 8 bytes of our initiator port ID and set
373 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800374 */
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700375 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800376 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
377 "activated for target GUID %016llx\n",
378 (unsigned long long) be64_to_cpu(target->ioc_guid));
379 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200380 memcpy(req->priv.initiator_port_id + 8,
381 &target->srp_host->dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800382 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800383
384 status = ib_send_cm_req(target->cm_id, &req->param);
385
386 kfree(req);
387
388 return status;
389}
390
391static void srp_disconnect_target(struct srp_target_port *target)
392{
393 /* XXX should send SRP_I_LOGOUT request */
394
395 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700396 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
397 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
398 return;
399 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800400 wait_for_completion(&target->done);
401}
402
David Howellsc4028952006-11-22 14:57:56 +0000403static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800404{
David Howellsc4028952006-11-22 14:57:56 +0000405 struct srp_target_port *target =
406 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800407
408 spin_lock_irq(target->scsi_host->host_lock);
409 if (target->state != SRP_TARGET_DEAD) {
410 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800411 return;
412 }
413 target->state = SRP_TARGET_REMOVED;
414 spin_unlock_irq(target->scsi_host->host_lock);
415
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700416 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800417 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700418 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800419
420 scsi_remove_host(target->scsi_host);
421 ib_destroy_cm_id(target->cm_id);
422 srp_free_target_ib(target);
423 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800424}
425
426static int srp_connect_target(struct srp_target_port *target)
427{
428 int ret;
429
430 ret = srp_lookup_path(target);
431 if (ret)
432 return ret;
433
434 while (1) {
435 init_completion(&target->done);
436 ret = srp_send_req(target);
437 if (ret)
438 return ret;
439 wait_for_completion(&target->done);
440
441 /*
442 * The CM event handling code will set status to
443 * SRP_PORT_REDIRECT if we get a port redirect REJ
444 * back, or SRP_DLID_REDIRECT if we get a lid/qp
445 * redirect REJ back.
446 */
447 switch (target->status) {
448 case 0:
449 return 0;
450
451 case SRP_PORT_REDIRECT:
452 ret = srp_lookup_path(target);
453 if (ret)
454 return ret;
455 break;
456
457 case SRP_DLID_REDIRECT:
458 break;
459
460 default:
461 return target->status;
462 }
463 }
464}
465
Roland Dreierd945e1d2006-05-09 10:50:28 -0700466static void srp_unmap_data(struct scsi_cmnd *scmnd,
467 struct srp_target_port *target,
468 struct srp_request *req)
469{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900470 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700471 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
472 scmnd->sc_data_direction != DMA_FROM_DEVICE))
473 return;
474
Roland Dreierf5358a12006-06-17 20:37:29 -0700475 if (req->fmr) {
476 ib_fmr_pool_unmap(req->fmr);
477 req->fmr = NULL;
478 }
479
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900480 ib_dma_unmap_sg(target->srp_host->dev->dev, scsi_sglist(scmnd),
481 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700482}
483
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700484static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
485{
486 srp_unmap_data(req->scmnd, target, req);
487 list_move_tail(&req->list, &target->free_reqs);
488}
489
490static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
491{
492 req->scmnd->result = DID_RESET << 16;
493 req->scmnd->scsi_done(req->scmnd);
494 srp_remove_req(target, req);
495}
496
Roland Dreieraef9ec32005-11-02 14:07:13 -0800497static int srp_reconnect_target(struct srp_target_port *target)
498{
499 struct ib_cm_id *new_cm_id;
500 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700501 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800502 struct ib_wc wc;
503 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800504
505 spin_lock_irq(target->scsi_host->host_lock);
506 if (target->state != SRP_TARGET_LIVE) {
507 spin_unlock_irq(target->scsi_host->host_lock);
508 return -EAGAIN;
509 }
510 target->state = SRP_TARGET_CONNECTING;
511 spin_unlock_irq(target->scsi_host->host_lock);
512
513 srp_disconnect_target(target);
514 /*
515 * Now get a new local CM ID so that we avoid confusing the
516 * target in case things are really fouled up.
517 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700518 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800519 srp_cm_handler, target);
520 if (IS_ERR(new_cm_id)) {
521 ret = PTR_ERR(new_cm_id);
522 goto err;
523 }
524 ib_destroy_cm_id(target->cm_id);
525 target->cm_id = new_cm_id;
526
527 qp_attr.qp_state = IB_QPS_RESET;
528 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
529 if (ret)
530 goto err;
531
532 ret = srp_init_qp(target, target->qp);
533 if (ret)
534 goto err;
535
536 while (ib_poll_cq(target->cq, 1, &wc) > 0)
537 ; /* nothing */
538
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300539 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700540 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
541 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300542 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800543
544 target->rx_head = 0;
545 target->tx_head = 0;
546 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800547
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200548 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800549 ret = srp_connect_target(target);
550 if (ret)
551 goto err;
552
553 spin_lock_irq(target->scsi_host->host_lock);
554 if (target->state == SRP_TARGET_CONNECTING) {
555 ret = 0;
556 target->state = SRP_TARGET_LIVE;
557 } else
558 ret = -EAGAIN;
559 spin_unlock_irq(target->scsi_host->host_lock);
560
561 return ret;
562
563err:
564 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
565
566 /*
567 * We couldn't reconnect, so kill our target port off.
568 * However, we have to defer the real removal because we might
569 * be in the context of the SCSI error handler now, which
570 * would deadlock if we call scsi_remove_host().
571 */
572 spin_lock_irq(target->scsi_host->host_lock);
573 if (target->state == SRP_TARGET_CONNECTING) {
574 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000575 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800576 schedule_work(&target->work);
577 }
578 spin_unlock_irq(target->scsi_host->host_lock);
579
580 return ret;
581}
582
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700583static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700584 int sg_cnt, struct srp_request *req,
585 struct srp_direct_buf *buf)
586{
587 u64 io_addr = 0;
588 u64 *dma_pages;
589 u32 len;
590 int page_cnt;
591 int i, j;
592 int ret;
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700593 struct srp_device *dev = target->srp_host->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800594 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900595 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700596
597 if (!dev->fmr_pool)
598 return -ENODEV;
599
Roland Dreier5d7cbfd2007-08-03 10:45:18 -0700600 if (srp_target_is_mellanox(target) &&
601 (ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask))
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700602 return -EINVAL;
603
Roland Dreierf5358a12006-06-17 20:37:29 -0700604 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900605 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
606 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800607
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900608 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700609 if (i > 0)
610 return -EINVAL;
611 else
612 ++page_cnt;
613 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900614 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700615 ~dev->fmr_page_mask) {
616 if (i < sg_cnt - 1)
617 return -EINVAL;
618 else
619 ++page_cnt;
620 }
621
Ralph Campbell85507bc2006-12-12 14:30:55 -0800622 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700623 }
624
625 page_cnt += len >> dev->fmr_page_shift;
626 if (page_cnt > SRP_FMR_SIZE)
627 return -ENOMEM;
628
629 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
630 if (!dma_pages)
631 return -ENOMEM;
632
633 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900634 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
635 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800636
637 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700638 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900639 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800640 dev->fmr_page_mask) + j;
641 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700642
643 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700644 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700645 if (IS_ERR(req->fmr)) {
646 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700647 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700648 goto out;
649 }
650
Ralph Campbell85507bc2006-12-12 14:30:55 -0800651 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
652 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700653 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
654 buf->len = cpu_to_be32(len);
655
656 ret = 0;
657
658out:
659 kfree(dma_pages);
660
661 return ret;
662}
663
Roland Dreieraef9ec32005-11-02 14:07:13 -0800664static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
665 struct srp_request *req)
666{
Roland Dreiercf368712006-03-24 15:47:26 -0800667 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800668 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800669 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700670 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800671 struct srp_device *dev;
672 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800673
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900674 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800675 return sizeof (struct srp_cmd);
676
677 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
678 scmnd->sc_data_direction != DMA_TO_DEVICE) {
679 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
680 scmnd->sc_data_direction);
681 return -EINVAL;
682 }
683
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900684 nents = scsi_sg_count(scmnd);
685 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800686
Ralph Campbell85507bc2006-12-12 14:30:55 -0800687 dev = target->srp_host->dev;
688 ibdev = dev->dev;
689
690 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700691
692 fmt = SRP_DATA_DESC_DIRECT;
693 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800694
695 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700696 /*
697 * The midlayer only generated a single gather/scatter
698 * entry, or DMA mapping coalesced everything to a
699 * single entry. So a direct descriptor along with
700 * the DMA MR suffices.
701 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800702 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800703
Ralph Campbell85507bc2006-12-12 14:30:55 -0800704 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
705 buf->key = cpu_to_be32(dev->mr->rkey);
706 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700707 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700708 (void *) cmd->add_data)) {
709 /*
710 * FMR mapping failed, and the scatterlist has more
711 * than one entry. Generate an indirect memory
712 * descriptor.
713 */
Roland Dreiercf368712006-03-24 15:47:26 -0800714 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900715 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800716 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700717 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800718
719 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700720 len = sizeof (struct srp_cmd) +
721 sizeof (struct srp_indirect_buf) +
722 count * sizeof (struct srp_direct_buf);
723
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900724 scsi_for_each_sg(scmnd, sg, count, i) {
725 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800726
Roland Dreierf5358a12006-06-17 20:37:29 -0700727 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900728 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700729 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800730 cpu_to_be32(dev->mr->rkey);
731 buf->desc_list[i].len = cpu_to_be32(dma_len);
732 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700733 }
Roland Dreiercf368712006-03-24 15:47:26 -0800734
735 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
736 cmd->data_out_desc_cnt = count;
737 else
738 cmd->data_in_desc_cnt = count;
739
Roland Dreierf5358a12006-06-17 20:37:29 -0700740 buf->table_desc.va =
741 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800742 buf->table_desc.key =
Roland Dreierf5358a12006-06-17 20:37:29 -0700743 cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800744 buf->table_desc.len =
745 cpu_to_be32(count * sizeof (struct srp_direct_buf));
746
Roland Dreiercf368712006-03-24 15:47:26 -0800747 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800748 }
749
750 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
751 cmd->buf_fmt = fmt << 4;
752 else
753 cmd->buf_fmt = fmt;
754
Roland Dreieraef9ec32005-11-02 14:07:13 -0800755 return len;
756}
757
Roland Dreieraef9ec32005-11-02 14:07:13 -0800758static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
759{
760 struct srp_request *req;
761 struct scsi_cmnd *scmnd;
762 unsigned long flags;
763 s32 delta;
764
765 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
766
767 spin_lock_irqsave(target->scsi_host->host_lock, flags);
768
769 target->req_lim += delta;
770
771 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
772
773 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
774 if (be32_to_cpu(rsp->resp_data_len) < 4)
775 req->tsk_status = -1;
776 else
777 req->tsk_status = rsp->data[3];
778 complete(&req->done);
779 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700780 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800781 if (!scmnd)
782 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
783 (unsigned long long) rsp->tag);
784 scmnd->result = rsp->status;
785
786 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
787 memcpy(scmnd->sense_buffer, rsp->data +
788 be32_to_cpu(rsp->resp_data_len),
789 min_t(int, be32_to_cpu(rsp->sense_data_len),
790 SCSI_SENSE_BUFFERSIZE));
791 }
792
793 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900794 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800795 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900796 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800797
Roland Dreieraef9ec32005-11-02 14:07:13 -0800798 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800799 scmnd->host_scribble = (void *) -1L;
800 scmnd->scsi_done(scmnd);
801
Roland Dreierd945e1d2006-05-09 10:50:28 -0700802 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800803 } else
804 req->cmd_done = 1;
805 }
806
807 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
808}
809
Roland Dreieraef9ec32005-11-02 14:07:13 -0800810static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
811{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800812 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800813 struct srp_iu *iu;
814 u8 opcode;
815
816 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
817
Ralph Campbell85507bc2006-12-12 14:30:55 -0800818 dev = target->srp_host->dev->dev;
819 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
820 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800821
822 opcode = *(u8 *) iu->buf;
823
824 if (0) {
825 int i;
826
827 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
828
829 for (i = 0; i < wc->byte_len; ++i) {
830 if (i % 8 == 0)
831 printk(KERN_ERR " [%02x] ", i);
832 printk(" %02x", ((u8 *) iu->buf)[i]);
833 if ((i + 1) % 8 == 0)
834 printk("\n");
835 }
836
837 if (wc->byte_len % 8)
838 printk("\n");
839 }
840
841 switch (opcode) {
842 case SRP_RSP:
843 srp_process_rsp(target, iu->buf);
844 break;
845
846 case SRP_T_LOGOUT:
847 /* XXX Handle target logout */
848 printk(KERN_WARNING PFX "Got target logout request\n");
849 break;
850
851 default:
852 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
853 break;
854 }
855
Ralph Campbell85507bc2006-12-12 14:30:55 -0800856 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
857 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800858}
859
860static void srp_completion(struct ib_cq *cq, void *target_ptr)
861{
862 struct srp_target_port *target = target_ptr;
863 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800864
865 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
866 while (ib_poll_cq(cq, 1, &wc) > 0) {
867 if (wc.status) {
868 printk(KERN_ERR PFX "failed %s status %d\n",
869 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
870 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200871 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800872 break;
873 }
874
875 if (wc.wr_id & SRP_OP_RECV)
876 srp_handle_recv(target, &wc);
877 else
878 ++target->tx_tail;
879 }
880}
881
882static int __srp_post_recv(struct srp_target_port *target)
883{
884 struct srp_iu *iu;
885 struct ib_sge list;
886 struct ib_recv_wr wr, *bad_wr;
887 unsigned int next;
888 int ret;
889
890 next = target->rx_head & (SRP_RQ_SIZE - 1);
891 wr.wr_id = next | SRP_OP_RECV;
892 iu = target->rx_ring[next];
893
894 list.addr = iu->dma;
895 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700896 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800897
898 wr.next = NULL;
899 wr.sg_list = &list;
900 wr.num_sge = 1;
901
902 ret = ib_post_recv(target->qp, &wr, &bad_wr);
903 if (!ret)
904 ++target->rx_head;
905
906 return ret;
907}
908
909static int srp_post_recv(struct srp_target_port *target)
910{
911 unsigned long flags;
912 int ret;
913
914 spin_lock_irqsave(target->scsi_host->host_lock, flags);
915 ret = __srp_post_recv(target);
916 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
917
918 return ret;
919}
920
921/*
922 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800923 * req_lim and tx_head. Lock cannot be dropped between call here and
924 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800925 */
926static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
927{
928 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
929 return NULL;
930
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700931 if (unlikely(target->req_lim < 1))
932 ++target->zero_req_lim;
Roland Dreier47f2bce2005-11-15 00:19:21 -0800933
Roland Dreieraef9ec32005-11-02 14:07:13 -0800934 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
935}
936
937/*
938 * Must be called with target->scsi_host->host_lock held to protect
939 * req_lim and tx_head.
940 */
941static int __srp_post_send(struct srp_target_port *target,
942 struct srp_iu *iu, int len)
943{
944 struct ib_sge list;
945 struct ib_send_wr wr, *bad_wr;
946 int ret = 0;
947
Roland Dreieraef9ec32005-11-02 14:07:13 -0800948 list.addr = iu->dma;
949 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700950 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800951
952 wr.next = NULL;
953 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
954 wr.sg_list = &list;
955 wr.num_sge = 1;
956 wr.opcode = IB_WR_SEND;
957 wr.send_flags = IB_SEND_SIGNALED;
958
959 ret = ib_post_send(target->qp, &wr, &bad_wr);
960
961 if (!ret) {
962 ++target->tx_head;
963 --target->req_lim;
964 }
965
966 return ret;
967}
968
969static int srp_queuecommand(struct scsi_cmnd *scmnd,
970 void (*done)(struct scsi_cmnd *))
971{
972 struct srp_target_port *target = host_to_target(scmnd->device->host);
973 struct srp_request *req;
974 struct srp_iu *iu;
975 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800976 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800977 int len;
978
979 if (target->state == SRP_TARGET_CONNECTING)
980 goto err;
981
982 if (target->state == SRP_TARGET_DEAD ||
983 target->state == SRP_TARGET_REMOVED) {
984 scmnd->result = DID_BAD_TARGET << 16;
985 done(scmnd);
986 return 0;
987 }
988
989 iu = __srp_get_tx_iu(target);
990 if (!iu)
991 goto err;
992
Ralph Campbell85507bc2006-12-12 14:30:55 -0800993 dev = target->srp_host->dev->dev;
994 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
995 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800996
Roland Dreierd945e1d2006-05-09 10:50:28 -0700997 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800998
999 scmnd->scsi_done = done;
1000 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001001 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001002
1003 cmd = iu->buf;
1004 memset(cmd, 0, sizeof *cmd);
1005
1006 cmd->opcode = SRP_CMD;
1007 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001008 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001009 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1010
Roland Dreieraef9ec32005-11-02 14:07:13 -08001011 req->scmnd = scmnd;
1012 req->cmd = iu;
1013 req->cmd_done = 0;
1014 req->tsk_mgmt = NULL;
1015
1016 len = srp_map_data(scmnd, target, req);
1017 if (len < 0) {
1018 printk(KERN_ERR PFX "Failed to map data\n");
1019 goto err;
1020 }
1021
1022 if (__srp_post_recv(target)) {
1023 printk(KERN_ERR PFX "Recv failed\n");
1024 goto err_unmap;
1025 }
1026
Ralph Campbell85507bc2006-12-12 14:30:55 -08001027 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1028 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001029
1030 if (__srp_post_send(target, iu, len)) {
1031 printk(KERN_ERR PFX "Send failed\n");
1032 goto err_unmap;
1033 }
1034
Roland Dreierd945e1d2006-05-09 10:50:28 -07001035 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001036
1037 return 0;
1038
1039err_unmap:
1040 srp_unmap_data(scmnd, target, req);
1041
1042err:
1043 return SCSI_MLQUEUE_HOST_BUSY;
1044}
1045
1046static int srp_alloc_iu_bufs(struct srp_target_port *target)
1047{
1048 int i;
1049
1050 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1051 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1052 target->max_ti_iu_len,
1053 GFP_KERNEL, DMA_FROM_DEVICE);
1054 if (!target->rx_ring[i])
1055 goto err;
1056 }
1057
1058 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1059 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001060 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001061 GFP_KERNEL, DMA_TO_DEVICE);
1062 if (!target->tx_ring[i])
1063 goto err;
1064 }
1065
1066 return 0;
1067
1068err:
1069 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1070 srp_free_iu(target->srp_host, target->rx_ring[i]);
1071 target->rx_ring[i] = NULL;
1072 }
1073
1074 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1075 srp_free_iu(target->srp_host, target->tx_ring[i]);
1076 target->tx_ring[i] = NULL;
1077 }
1078
1079 return -ENOMEM;
1080}
1081
1082static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1083 struct ib_cm_event *event,
1084 struct srp_target_port *target)
1085{
1086 struct ib_class_port_info *cpi;
1087 int opcode;
1088
1089 switch (event->param.rej_rcvd.reason) {
1090 case IB_CM_REJ_PORT_CM_REDIRECT:
1091 cpi = event->param.rej_rcvd.ari;
1092 target->path.dlid = cpi->redirect_lid;
1093 target->path.pkey = cpi->redirect_pkey;
1094 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1095 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1096
1097 target->status = target->path.dlid ?
1098 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1099 break;
1100
1101 case IB_CM_REJ_PORT_REDIRECT:
Roland Dreier5d7cbfd2007-08-03 10:45:18 -07001102 if (srp_target_is_topspin(target)) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001103 /*
1104 * Topspin/Cisco SRP gateways incorrectly send
1105 * reject reason code 25 when they mean 24
1106 * (port redirect).
1107 */
1108 memcpy(target->path.dgid.raw,
1109 event->param.rej_rcvd.ari, 16);
1110
1111 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1112 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1113 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1114
1115 target->status = SRP_PORT_REDIRECT;
1116 } else {
1117 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1118 target->status = -ECONNRESET;
1119 }
1120 break;
1121
1122 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1123 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1124 target->status = -ECONNRESET;
1125 break;
1126
1127 case IB_CM_REJ_CONSUMER_DEFINED:
1128 opcode = *(u8 *) event->private_data;
1129 if (opcode == SRP_LOGIN_REJ) {
1130 struct srp_login_rej *rej = event->private_data;
1131 u32 reason = be32_to_cpu(rej->reason);
1132
1133 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1134 printk(KERN_WARNING PFX
1135 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1136 else
1137 printk(KERN_WARNING PFX
1138 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1139 } else
1140 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1141 " opcode 0x%02x\n", opcode);
1142 target->status = -ECONNRESET;
1143 break;
1144
1145 default:
1146 printk(KERN_WARNING " REJ reason 0x%x\n",
1147 event->param.rej_rcvd.reason);
1148 target->status = -ECONNRESET;
1149 }
1150}
1151
1152static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1153{
1154 struct srp_target_port *target = cm_id->context;
1155 struct ib_qp_attr *qp_attr = NULL;
1156 int attr_mask = 0;
1157 int comp = 0;
1158 int opcode = 0;
1159
1160 switch (event->event) {
1161 case IB_CM_REQ_ERROR:
1162 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1163 comp = 1;
1164 target->status = -ECONNRESET;
1165 break;
1166
1167 case IB_CM_REP_RECEIVED:
1168 comp = 1;
1169 opcode = *(u8 *) event->private_data;
1170
1171 if (opcode == SRP_LOGIN_RSP) {
1172 struct srp_login_rsp *rsp = event->private_data;
1173
1174 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1175 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1176
1177 target->scsi_host->can_queue = min(target->req_lim,
1178 target->scsi_host->can_queue);
1179 } else {
1180 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1181 target->status = -ECONNRESET;
1182 break;
1183 }
1184
Vu Phamd2fcea72006-11-21 14:14:10 -08001185 if (!target->rx_ring[0]) {
1186 target->status = srp_alloc_iu_bufs(target);
1187 if (target->status)
1188 break;
1189 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001190
1191 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1192 if (!qp_attr) {
1193 target->status = -ENOMEM;
1194 break;
1195 }
1196
1197 qp_attr->qp_state = IB_QPS_RTR;
1198 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1199 if (target->status)
1200 break;
1201
1202 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1203 if (target->status)
1204 break;
1205
1206 target->status = srp_post_recv(target);
1207 if (target->status)
1208 break;
1209
1210 qp_attr->qp_state = IB_QPS_RTS;
1211 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1212 if (target->status)
1213 break;
1214
1215 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1216 if (target->status)
1217 break;
1218
1219 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1220 if (target->status)
1221 break;
1222
1223 break;
1224
1225 case IB_CM_REJ_RECEIVED:
1226 printk(KERN_DEBUG PFX "REJ received\n");
1227 comp = 1;
1228
1229 srp_cm_rej_handler(cm_id, event, target);
1230 break;
1231
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001232 case IB_CM_DREQ_RECEIVED:
1233 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1234 if (ib_send_cm_drep(cm_id, NULL, 0))
1235 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001236 break;
1237
1238 case IB_CM_TIMEWAIT_EXIT:
1239 printk(KERN_ERR PFX "connection closed\n");
1240
1241 comp = 1;
1242 target->status = 0;
1243 break;
1244
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001245 case IB_CM_MRA_RECEIVED:
1246 case IB_CM_DREQ_ERROR:
1247 case IB_CM_DREP_RECEIVED:
1248 break;
1249
Roland Dreieraef9ec32005-11-02 14:07:13 -08001250 default:
1251 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1252 break;
1253 }
1254
1255 if (comp)
1256 complete(&target->done);
1257
1258 kfree(qp_attr);
1259
1260 return 0;
1261}
1262
Roland Dreierd945e1d2006-05-09 10:50:28 -07001263static int srp_send_tsk_mgmt(struct srp_target_port *target,
1264 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001265{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001266 struct srp_iu *iu;
1267 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001268
1269 spin_lock_irq(target->scsi_host->host_lock);
1270
Roland Dreier1285b3a2006-03-03 15:47:25 -08001271 if (target->state == SRP_TARGET_DEAD ||
1272 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001273 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001274 goto out;
1275 }
1276
Roland Dreieraef9ec32005-11-02 14:07:13 -08001277 init_completion(&req->done);
1278
1279 iu = __srp_get_tx_iu(target);
1280 if (!iu)
1281 goto out;
1282
1283 tsk_mgmt = iu->buf;
1284 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1285
1286 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001287 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1288 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001289 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001290 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001291
1292 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1293 goto out;
1294
1295 req->tsk_mgmt = iu;
1296
1297 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001298
Roland Dreieraef9ec32005-11-02 14:07:13 -08001299 if (!wait_for_completion_timeout(&req->done,
1300 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001301 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001302
Roland Dreierd945e1d2006-05-09 10:50:28 -07001303 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001304
1305out:
1306 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001307 return -1;
1308}
1309
1310static int srp_find_req(struct srp_target_port *target,
1311 struct scsi_cmnd *scmnd,
1312 struct srp_request **req)
1313{
1314 if (scmnd->host_scribble == (void *) -1L)
1315 return -1;
1316
1317 *req = &target->req_ring[(long) scmnd->host_scribble];
1318
1319 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001320}
1321
1322static int srp_abort(struct scsi_cmnd *scmnd)
1323{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001324 struct srp_target_port *target = host_to_target(scmnd->device->host);
1325 struct srp_request *req;
1326 int ret = SUCCESS;
1327
Roland Dreieraef9ec32005-11-02 14:07:13 -08001328 printk(KERN_ERR "SRP abort called\n");
1329
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001330 if (target->qp_in_error)
1331 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001332 if (srp_find_req(target, scmnd, &req))
1333 return FAILED;
1334 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1335 return FAILED;
1336
1337 spin_lock_irq(target->scsi_host->host_lock);
1338
1339 if (req->cmd_done) {
1340 srp_remove_req(target, req);
1341 scmnd->scsi_done(scmnd);
1342 } else if (!req->tsk_status) {
1343 srp_remove_req(target, req);
1344 scmnd->result = DID_ABORT << 16;
1345 } else
1346 ret = FAILED;
1347
1348 spin_unlock_irq(target->scsi_host->host_lock);
1349
1350 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001351}
1352
1353static int srp_reset_device(struct scsi_cmnd *scmnd)
1354{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001355 struct srp_target_port *target = host_to_target(scmnd->device->host);
1356 struct srp_request *req, *tmp;
1357
Roland Dreieraef9ec32005-11-02 14:07:13 -08001358 printk(KERN_ERR "SRP reset_device called\n");
1359
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001360 if (target->qp_in_error)
1361 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001362 if (srp_find_req(target, scmnd, &req))
1363 return FAILED;
1364 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1365 return FAILED;
1366 if (req->tsk_status)
1367 return FAILED;
1368
1369 spin_lock_irq(target->scsi_host->host_lock);
1370
1371 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001372 if (req->scmnd->device == scmnd->device)
1373 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001374
1375 spin_unlock_irq(target->scsi_host->host_lock);
1376
1377 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001378}
1379
1380static int srp_reset_host(struct scsi_cmnd *scmnd)
1381{
1382 struct srp_target_port *target = host_to_target(scmnd->device->host);
1383 int ret = FAILED;
1384
1385 printk(KERN_ERR PFX "SRP reset_host called\n");
1386
1387 if (!srp_reconnect_target(target))
1388 ret = SUCCESS;
1389
1390 return ret;
1391}
1392
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001393static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1394{
1395 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1396
1397 if (target->state == SRP_TARGET_DEAD ||
1398 target->state == SRP_TARGET_REMOVED)
1399 return -ENODEV;
1400
1401 return sprintf(buf, "0x%016llx\n",
1402 (unsigned long long) be64_to_cpu(target->id_ext));
1403}
1404
1405static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1406{
1407 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1408
1409 if (target->state == SRP_TARGET_DEAD ||
1410 target->state == SRP_TARGET_REMOVED)
1411 return -ENODEV;
1412
1413 return sprintf(buf, "0x%016llx\n",
1414 (unsigned long long) be64_to_cpu(target->ioc_guid));
1415}
1416
1417static ssize_t show_service_id(struct class_device *cdev, char *buf)
1418{
1419 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1420
1421 if (target->state == SRP_TARGET_DEAD ||
1422 target->state == SRP_TARGET_REMOVED)
1423 return -ENODEV;
1424
1425 return sprintf(buf, "0x%016llx\n",
1426 (unsigned long long) be64_to_cpu(target->service_id));
1427}
1428
1429static ssize_t show_pkey(struct class_device *cdev, char *buf)
1430{
1431 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1432
1433 if (target->state == SRP_TARGET_DEAD ||
1434 target->state == SRP_TARGET_REMOVED)
1435 return -ENODEV;
1436
1437 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1438}
1439
1440static ssize_t show_dgid(struct class_device *cdev, char *buf)
1441{
1442 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1443
1444 if (target->state == SRP_TARGET_DEAD ||
1445 target->state == SRP_TARGET_REMOVED)
1446 return -ENODEV;
1447
1448 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1449 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1450 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1451 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1452 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1453 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1454 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1455 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1456 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1457}
1458
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001459static ssize_t show_orig_dgid(struct class_device *cdev, char *buf)
1460{
1461 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1462
1463 if (target->state == SRP_TARGET_DEAD ||
1464 target->state == SRP_TARGET_REMOVED)
1465 return -ENODEV;
1466
1467 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1468 be16_to_cpu(target->orig_dgid[0]),
1469 be16_to_cpu(target->orig_dgid[1]),
1470 be16_to_cpu(target->orig_dgid[2]),
1471 be16_to_cpu(target->orig_dgid[3]),
1472 be16_to_cpu(target->orig_dgid[4]),
1473 be16_to_cpu(target->orig_dgid[5]),
1474 be16_to_cpu(target->orig_dgid[6]),
1475 be16_to_cpu(target->orig_dgid[7]));
1476}
1477
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001478static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1479{
1480 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1481
1482 if (target->state == SRP_TARGET_DEAD ||
1483 target->state == SRP_TARGET_REMOVED)
1484 return -ENODEV;
1485
1486 return sprintf(buf, "%d\n", target->zero_req_lim);
1487}
1488
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001489static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1490{
1491 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1492
1493 return sprintf(buf, "%d\n", target->srp_host->port);
1494}
1495
1496static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1497{
1498 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1499
1500 return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1501}
1502
1503static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1504static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1505static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1506static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1507static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001508static CLASS_DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001509static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1510static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1511static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001512
1513static struct class_device_attribute *srp_host_attrs[] = {
1514 &class_device_attr_id_ext,
1515 &class_device_attr_ioc_guid,
1516 &class_device_attr_service_id,
1517 &class_device_attr_pkey,
1518 &class_device_attr_dgid,
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001519 &class_device_attr_orig_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001520 &class_device_attr_zero_req_lim,
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001521 &class_device_attr_local_ib_port,
1522 &class_device_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001523 NULL
1524};
1525
Roland Dreieraef9ec32005-11-02 14:07:13 -08001526static struct scsi_host_template srp_template = {
1527 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001528 .name = "InfiniBand SRP initiator",
1529 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001530 .info = srp_target_info,
1531 .queuecommand = srp_queuecommand,
1532 .eh_abort_handler = srp_abort,
1533 .eh_device_reset_handler = srp_reset_device,
1534 .eh_host_reset_handler = srp_reset_host,
1535 .can_queue = SRP_SQ_SIZE,
1536 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001537 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001538 .use_clustering = ENABLE_CLUSTERING,
1539 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001540};
1541
1542static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1543{
1544 sprintf(target->target_name, "SRP.T10:%016llX",
1545 (unsigned long long) be64_to_cpu(target->id_ext));
1546
Roland Dreierf5358a12006-06-17 20:37:29 -07001547 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001548 return -ENODEV;
1549
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001550 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001551 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001552 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001553
1554 target->state = SRP_TARGET_LIVE;
1555
Roland Dreieraef9ec32005-11-02 14:07:13 -08001556 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001557 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001558
1559 return 0;
1560}
1561
1562static void srp_release_class_dev(struct class_device *class_dev)
1563{
1564 struct srp_host *host =
1565 container_of(class_dev, struct srp_host, class_dev);
1566
1567 complete(&host->released);
1568}
1569
1570static struct class srp_class = {
1571 .name = "infiniband_srp",
1572 .release = srp_release_class_dev
1573};
1574
1575/*
1576 * Target ports are added by writing
1577 *
1578 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1579 * pkey=<P_Key>,service_id=<service ID>
1580 *
1581 * to the add_target sysfs attribute.
1582 */
1583enum {
1584 SRP_OPT_ERR = 0,
1585 SRP_OPT_ID_EXT = 1 << 0,
1586 SRP_OPT_IOC_GUID = 1 << 1,
1587 SRP_OPT_DGID = 1 << 2,
1588 SRP_OPT_PKEY = 1 << 3,
1589 SRP_OPT_SERVICE_ID = 1 << 4,
1590 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001591 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001592 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001593 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001594 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1595 SRP_OPT_IOC_GUID |
1596 SRP_OPT_DGID |
1597 SRP_OPT_PKEY |
1598 SRP_OPT_SERVICE_ID),
1599};
1600
1601static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001602 { SRP_OPT_ID_EXT, "id_ext=%s" },
1603 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1604 { SRP_OPT_DGID, "dgid=%s" },
1605 { SRP_OPT_PKEY, "pkey=%x" },
1606 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1607 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1608 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001609 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001610 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001611 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001612};
1613
1614static int srp_parse_options(const char *buf, struct srp_target_port *target)
1615{
1616 char *options, *sep_opt;
1617 char *p;
1618 char dgid[3];
1619 substring_t args[MAX_OPT_ARGS];
1620 int opt_mask = 0;
1621 int token;
1622 int ret = -EINVAL;
1623 int i;
1624
1625 options = kstrdup(buf, GFP_KERNEL);
1626 if (!options)
1627 return -ENOMEM;
1628
1629 sep_opt = options;
1630 while ((p = strsep(&sep_opt, ",")) != NULL) {
1631 if (!*p)
1632 continue;
1633
1634 token = match_token(p, srp_opt_tokens, args);
1635 opt_mask |= token;
1636
1637 switch (token) {
1638 case SRP_OPT_ID_EXT:
1639 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001640 if (!p) {
1641 ret = -ENOMEM;
1642 goto out;
1643 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001644 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1645 kfree(p);
1646 break;
1647
1648 case SRP_OPT_IOC_GUID:
1649 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001650 if (!p) {
1651 ret = -ENOMEM;
1652 goto out;
1653 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001654 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1655 kfree(p);
1656 break;
1657
1658 case SRP_OPT_DGID:
1659 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001660 if (!p) {
1661 ret = -ENOMEM;
1662 goto out;
1663 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001664 if (strlen(p) != 32) {
1665 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001666 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001667 goto out;
1668 }
1669
1670 for (i = 0; i < 16; ++i) {
1671 strlcpy(dgid, p + i * 2, 3);
1672 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1673 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001674 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001675 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001676 break;
1677
1678 case SRP_OPT_PKEY:
1679 if (match_hex(args, &token)) {
1680 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1681 goto out;
1682 }
1683 target->path.pkey = cpu_to_be16(token);
1684 break;
1685
1686 case SRP_OPT_SERVICE_ID:
1687 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001688 if (!p) {
1689 ret = -ENOMEM;
1690 goto out;
1691 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001692 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1693 kfree(p);
1694 break;
1695
1696 case SRP_OPT_MAX_SECT:
1697 if (match_int(args, &token)) {
1698 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1699 goto out;
1700 }
1701 target->scsi_host->max_sectors = token;
1702 break;
1703
Vu Pham52fb2b502006-06-17 20:37:31 -07001704 case SRP_OPT_MAX_CMD_PER_LUN:
1705 if (match_int(args, &token)) {
1706 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1707 goto out;
1708 }
1709 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1710 break;
1711
Ramachandra K0c0450db2006-06-17 20:37:38 -07001712 case SRP_OPT_IO_CLASS:
1713 if (match_hex(args, &token)) {
1714 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1715 goto out;
1716 }
1717 if (token != SRP_REV10_IB_IO_CLASS &&
1718 token != SRP_REV16A_IB_IO_CLASS) {
1719 printk(KERN_WARNING PFX "unknown IO class parameter value"
1720 " %x specified (use %x or %x).\n",
1721 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1722 goto out;
1723 }
1724 target->io_class = token;
1725 break;
1726
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001727 case SRP_OPT_INITIATOR_EXT:
1728 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001729 if (!p) {
1730 ret = -ENOMEM;
1731 goto out;
1732 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001733 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1734 kfree(p);
1735 break;
1736
Roland Dreieraef9ec32005-11-02 14:07:13 -08001737 default:
1738 printk(KERN_WARNING PFX "unknown parameter or missing value "
1739 "'%s' in target creation request\n", p);
1740 goto out;
1741 }
1742 }
1743
1744 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1745 ret = 0;
1746 else
1747 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1748 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1749 !(srp_opt_tokens[i].token & opt_mask))
1750 printk(KERN_WARNING PFX "target creation request is "
1751 "missing parameter '%s'\n",
1752 srp_opt_tokens[i].pattern);
1753
1754out:
1755 kfree(options);
1756 return ret;
1757}
1758
1759static ssize_t srp_create_target(struct class_device *class_dev,
1760 const char *buf, size_t count)
1761{
1762 struct srp_host *host =
1763 container_of(class_dev, struct srp_host, class_dev);
1764 struct Scsi_Host *target_host;
1765 struct srp_target_port *target;
1766 int ret;
1767 int i;
1768
1769 target_host = scsi_host_alloc(&srp_template,
1770 sizeof (struct srp_target_port));
1771 if (!target_host)
1772 return -ENOMEM;
1773
Arne Redlich3c8edf02006-11-15 12:43:00 +01001774 target_host->max_lun = SRP_MAX_LUN;
1775 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001776
Roland Dreieraef9ec32005-11-02 14:07:13 -08001777 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001778
Ramachandra K0c0450db2006-06-17 20:37:38 -07001779 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001780 target->scsi_host = target_host;
1781 target->srp_host = host;
1782
Roland Dreierd945e1d2006-05-09 10:50:28 -07001783 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001784 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001785 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1786 target->req_ring[i].index = i;
1787 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1788 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001789
1790 ret = srp_parse_options(buf, target);
1791 if (ret)
1792 goto err;
1793
Roland Dreierf5358a12006-06-17 20:37:29 -07001794 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001795
1796 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1797 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1798 (unsigned long long) be64_to_cpu(target->id_ext),
1799 (unsigned long long) be64_to_cpu(target->ioc_guid),
1800 be16_to_cpu(target->path.pkey),
1801 (unsigned long long) be64_to_cpu(target->service_id),
1802 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1803 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1804 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1805 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1806 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1807 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1808 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1809 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1810
1811 ret = srp_create_target_ib(target);
1812 if (ret)
1813 goto err;
1814
Roland Dreierf5358a12006-06-17 20:37:29 -07001815 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001816 if (IS_ERR(target->cm_id)) {
1817 ret = PTR_ERR(target->cm_id);
1818 goto err_free;
1819 }
1820
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001821 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001822 ret = srp_connect_target(target);
1823 if (ret) {
1824 printk(KERN_ERR PFX "Connection failed\n");
1825 goto err_cm_id;
1826 }
1827
1828 ret = srp_add_target(host, target);
1829 if (ret)
1830 goto err_disconnect;
1831
1832 return count;
1833
1834err_disconnect:
1835 srp_disconnect_target(target);
1836
1837err_cm_id:
1838 ib_destroy_cm_id(target->cm_id);
1839
1840err_free:
1841 srp_free_target_ib(target);
1842
1843err:
1844 scsi_host_put(target_host);
1845
1846 return ret;
1847}
1848
1849static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1850
1851static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1852{
1853 struct srp_host *host =
1854 container_of(class_dev, struct srp_host, class_dev);
1855
Roland Dreierf5358a12006-06-17 20:37:29 -07001856 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001857}
1858
1859static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1860
1861static ssize_t show_port(struct class_device *class_dev, char *buf)
1862{
1863 struct srp_host *host =
1864 container_of(class_dev, struct srp_host, class_dev);
1865
1866 return sprintf(buf, "%d\n", host->port);
1867}
1868
1869static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1870
Roland Dreierf5358a12006-06-17 20:37:29 -07001871static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001872{
1873 struct srp_host *host;
1874
1875 host = kzalloc(sizeof *host, GFP_KERNEL);
1876 if (!host)
1877 return NULL;
1878
1879 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001880 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001881 init_completion(&host->released);
1882 host->dev = device;
1883 host->port = port;
1884
Roland Dreieraef9ec32005-11-02 14:07:13 -08001885 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001886 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001887 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001888 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001889
1890 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001891 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001892 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1893 goto err_class;
1894 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1895 goto err_class;
1896 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1897 goto err_class;
1898
1899 return host;
1900
1901err_class:
1902 class_device_unregister(&host->class_dev);
1903
Roland Dreierf5358a12006-06-17 20:37:29 -07001904free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001905 kfree(host);
1906
1907 return NULL;
1908}
1909
1910static void srp_add_one(struct ib_device *device)
1911{
Roland Dreierf5358a12006-06-17 20:37:29 -07001912 struct srp_device *srp_dev;
1913 struct ib_device_attr *dev_attr;
1914 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001915 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001916 int s, e, p;
1917
Roland Dreierf5358a12006-06-17 20:37:29 -07001918 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1919 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001920 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001921
Roland Dreierf5358a12006-06-17 20:37:29 -07001922 if (ib_query_device(device, dev_attr)) {
1923 printk(KERN_WARNING PFX "Query device failed for %s\n",
1924 device->name);
1925 goto free_attr;
1926 }
1927
1928 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1929 if (!srp_dev)
1930 goto free_attr;
1931
1932 /*
1933 * Use the smallest page size supported by the HCA, down to a
1934 * minimum of 512 bytes (which is the smallest sector that a
1935 * SCSI command will ever carry).
1936 */
1937 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1938 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08001939 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07001940
1941 INIT_LIST_HEAD(&srp_dev->dev_list);
1942
1943 srp_dev->dev = device;
1944 srp_dev->pd = ib_alloc_pd(device);
1945 if (IS_ERR(srp_dev->pd))
1946 goto free_dev;
1947
1948 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1949 IB_ACCESS_LOCAL_WRITE |
1950 IB_ACCESS_REMOTE_READ |
1951 IB_ACCESS_REMOTE_WRITE);
1952 if (IS_ERR(srp_dev->mr))
1953 goto err_pd;
1954
1955 memset(&fmr_param, 0, sizeof fmr_param);
1956 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1957 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1958 fmr_param.cache = 1;
1959 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1960 fmr_param.page_shift = srp_dev->fmr_page_shift;
1961 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1962 IB_ACCESS_REMOTE_WRITE |
1963 IB_ACCESS_REMOTE_READ);
1964
1965 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1966 if (IS_ERR(srp_dev->fmr_pool))
1967 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001968
Tom Tucker07ebafb2006-08-03 16:02:42 -05001969 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001970 s = 0;
1971 e = 0;
1972 } else {
1973 s = 1;
1974 e = device->phys_port_cnt;
1975 }
1976
1977 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001978 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001979 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001980 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001981 }
1982
Roland Dreierf5358a12006-06-17 20:37:29 -07001983 ib_set_client_data(device, &srp_client, srp_dev);
1984
1985 goto free_attr;
1986
1987err_pd:
1988 ib_dealloc_pd(srp_dev->pd);
1989
1990free_dev:
1991 kfree(srp_dev);
1992
1993free_attr:
1994 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001995}
1996
1997static void srp_remove_one(struct ib_device *device)
1998{
Roland Dreierf5358a12006-06-17 20:37:29 -07001999 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002000 struct srp_host *host, *tmp_host;
2001 LIST_HEAD(target_list);
2002 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002003
Roland Dreierf5358a12006-06-17 20:37:29 -07002004 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002005
Roland Dreierf5358a12006-06-17 20:37:29 -07002006 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002007 class_device_unregister(&host->class_dev);
2008 /*
2009 * Wait for the sysfs entry to go away, so that no new
2010 * target ports can be created.
2011 */
2012 wait_for_completion(&host->released);
2013
2014 /*
2015 * Mark all target ports as removed, so we stop queueing
2016 * commands and don't try to reconnect.
2017 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002018 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002019 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002020 spin_lock_irq(target->scsi_host->host_lock);
2021 target->state = SRP_TARGET_REMOVED;
2022 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002023 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002024 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002025
2026 /*
2027 * Wait for any reconnection tasks that may have
2028 * started before we marked our target ports as
2029 * removed, and any target port removal tasks.
2030 */
2031 flush_scheduled_work();
2032
2033 list_for_each_entry_safe(target, tmp_target,
2034 &host->target_list, list) {
2035 scsi_remove_host(target->scsi_host);
2036 srp_disconnect_target(target);
2037 ib_destroy_cm_id(target->cm_id);
2038 srp_free_target_ib(target);
2039 scsi_host_put(target->scsi_host);
2040 }
2041
Roland Dreieraef9ec32005-11-02 14:07:13 -08002042 kfree(host);
2043 }
2044
Roland Dreierf5358a12006-06-17 20:37:29 -07002045 if (srp_dev->fmr_pool)
2046 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2047 ib_dereg_mr(srp_dev->mr);
2048 ib_dealloc_pd(srp_dev->pd);
2049
2050 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002051}
2052
2053static int __init srp_init_module(void)
2054{
2055 int ret;
2056
Vu Pham74b0a152006-06-17 20:37:32 -07002057 srp_template.sg_tablesize = srp_sg_tablesize;
2058 srp_max_iu_len = (sizeof (struct srp_cmd) +
2059 sizeof (struct srp_indirect_buf) +
2060 srp_sg_tablesize * 16);
2061
Roland Dreieraef9ec32005-11-02 14:07:13 -08002062 ret = class_register(&srp_class);
2063 if (ret) {
2064 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2065 return ret;
2066 }
2067
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002068 ib_sa_register_client(&srp_sa_client);
2069
Roland Dreieraef9ec32005-11-02 14:07:13 -08002070 ret = ib_register_client(&srp_client);
2071 if (ret) {
2072 printk(KERN_ERR PFX "couldn't register IB client\n");
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002073 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002074 class_unregister(&srp_class);
2075 return ret;
2076 }
2077
2078 return 0;
2079}
2080
2081static void __exit srp_cleanup_module(void)
2082{
2083 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002084 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002085 class_unregister(&srp_class);
2086}
2087
2088module_init(srp_init_module);
2089module_exit(srp_cleanup_module);