blob: e616c4fc6ff0046262fecf288c71ddf438787825 [file] [log] [blame]
Roland Dreieraef9ec32005-11-02 14:07:13 -08001/*
2 * Copyright (c) 2005 Cisco Systems. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 * $Id: ib_srp.c 3932 2005-11-01 17:19:29Z roland $
33 */
34
Roland Dreieraef9ec32005-11-02 14:07:13 -080035#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/err.h>
39#include <linux/string.h>
40#include <linux/parser.h>
41#include <linux/random.h>
Tim Schmielaude259682006-01-08 01:02:05 -080042#include <linux/jiffies.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080043
44#include <asm/atomic.h>
45
46#include <scsi/scsi.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_dbg.h>
49#include <scsi/srp.h>
FUJITA Tomonori32368222007-06-27 16:33:12 +090050#include <scsi/scsi_transport_srp.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080051
52#include <rdma/ib_cache.h>
53
54#include "ib_srp.h"
55
56#define DRV_NAME "ib_srp"
57#define PFX DRV_NAME ": "
58#define DRV_VERSION "0.2"
59#define DRV_RELDATE "November 1, 2005"
60
61MODULE_AUTHOR("Roland Dreier");
62MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
63 "v" DRV_VERSION " (" DRV_RELDATE ")");
64MODULE_LICENSE("Dual BSD/GPL");
65
Vu Pham74b0a152006-06-17 20:37:32 -070066static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
67static int srp_max_iu_len;
68
69module_param(srp_sg_tablesize, int, 0444);
70MODULE_PARM_DESC(srp_sg_tablesize,
71 "Max number of gather/scatter entries per I/O (default is 12)");
72
Roland Dreieraef9ec32005-11-02 14:07:13 -080073static int topspin_workarounds = 1;
74
75module_param(topspin_workarounds, int, 0444);
76MODULE_PARM_DESC(topspin_workarounds,
77 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
78
79static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
80
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070081static int mellanox_workarounds = 1;
82
83module_param(mellanox_workarounds, int, 0444);
84MODULE_PARM_DESC(mellanox_workarounds,
85 "Enable workarounds for Mellanox SRP target bugs if != 0");
86
87static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
88
Roland Dreieraef9ec32005-11-02 14:07:13 -080089static void srp_add_one(struct ib_device *device);
90static void srp_remove_one(struct ib_device *device);
91static void srp_completion(struct ib_cq *cq, void *target_ptr);
92static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
93
FUJITA Tomonori32368222007-06-27 16:33:12 +090094static struct scsi_transport_template *ib_srp_transport_template;
95
Roland Dreieraef9ec32005-11-02 14:07:13 -080096static struct ib_client srp_client = {
97 .name = "srp",
98 .add = srp_add_one,
99 .remove = srp_remove_one
100};
101
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700102static struct ib_sa_client srp_sa_client;
103
Roland Dreieraef9ec32005-11-02 14:07:13 -0800104static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
105{
106 return (struct srp_target_port *) host->hostdata;
107}
108
109static const char *srp_target_info(struct Scsi_Host *host)
110{
111 return host_to_target(host)->target_name;
112}
113
114static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
115 gfp_t gfp_mask,
116 enum dma_data_direction direction)
117{
118 struct srp_iu *iu;
119
120 iu = kmalloc(sizeof *iu, gfp_mask);
121 if (!iu)
122 goto out;
123
124 iu->buf = kzalloc(size, gfp_mask);
125 if (!iu->buf)
126 goto out_free_iu;
127
Ralph Campbell85507bc2006-12-12 14:30:55 -0800128 iu->dma = ib_dma_map_single(host->dev->dev, iu->buf, size, direction);
129 if (ib_dma_mapping_error(host->dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800130 goto out_free_buf;
131
132 iu->size = size;
133 iu->direction = direction;
134
135 return iu;
136
137out_free_buf:
138 kfree(iu->buf);
139out_free_iu:
140 kfree(iu);
141out:
142 return NULL;
143}
144
145static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
146{
147 if (!iu)
148 return;
149
Ralph Campbell85507bc2006-12-12 14:30:55 -0800150 ib_dma_unmap_single(host->dev->dev, iu->dma, iu->size, iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800151 kfree(iu->buf);
152 kfree(iu);
153}
154
155static void srp_qp_event(struct ib_event *event, void *context)
156{
157 printk(KERN_ERR PFX "QP event %d\n", event->event);
158}
159
160static int srp_init_qp(struct srp_target_port *target,
161 struct ib_qp *qp)
162{
163 struct ib_qp_attr *attr;
164 int ret;
165
166 attr = kmalloc(sizeof *attr, GFP_KERNEL);
167 if (!attr)
168 return -ENOMEM;
169
Roland Dreierf5358a12006-06-17 20:37:29 -0700170 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800171 target->srp_host->port,
172 be16_to_cpu(target->path.pkey),
173 &attr->pkey_index);
174 if (ret)
175 goto out;
176
177 attr->qp_state = IB_QPS_INIT;
178 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
179 IB_ACCESS_REMOTE_WRITE);
180 attr->port_num = target->srp_host->port;
181
182 ret = ib_modify_qp(qp, attr,
183 IB_QP_STATE |
184 IB_QP_PKEY_INDEX |
185 IB_QP_ACCESS_FLAGS |
186 IB_QP_PORT);
187
188out:
189 kfree(attr);
190 return ret;
191}
192
193static int srp_create_target_ib(struct srp_target_port *target)
194{
195 struct ib_qp_init_attr *init_attr;
196 int ret;
197
198 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
199 if (!init_attr)
200 return -ENOMEM;
201
Roland Dreierf5358a12006-06-17 20:37:29 -0700202 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +0300203 NULL, target, SRP_CQ_SIZE, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800204 if (IS_ERR(target->cq)) {
205 ret = PTR_ERR(target->cq);
206 goto out;
207 }
208
209 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
210
211 init_attr->event_handler = srp_qp_event;
212 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
213 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
214 init_attr->cap.max_recv_sge = 1;
215 init_attr->cap.max_send_sge = 1;
216 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
217 init_attr->qp_type = IB_QPT_RC;
218 init_attr->send_cq = target->cq;
219 init_attr->recv_cq = target->cq;
220
Roland Dreierf5358a12006-06-17 20:37:29 -0700221 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800222 if (IS_ERR(target->qp)) {
223 ret = PTR_ERR(target->qp);
224 ib_destroy_cq(target->cq);
225 goto out;
226 }
227
228 ret = srp_init_qp(target, target->qp);
229 if (ret) {
230 ib_destroy_qp(target->qp);
231 ib_destroy_cq(target->cq);
232 goto out;
233 }
234
235out:
236 kfree(init_attr);
237 return ret;
238}
239
240static void srp_free_target_ib(struct srp_target_port *target)
241{
242 int i;
243
244 ib_destroy_qp(target->qp);
245 ib_destroy_cq(target->cq);
246
247 for (i = 0; i < SRP_RQ_SIZE; ++i)
248 srp_free_iu(target->srp_host, target->rx_ring[i]);
249 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
250 srp_free_iu(target->srp_host, target->tx_ring[i]);
251}
252
253static void srp_path_rec_completion(int status,
254 struct ib_sa_path_rec *pathrec,
255 void *target_ptr)
256{
257 struct srp_target_port *target = target_ptr;
258
259 target->status = status;
260 if (status)
261 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
262 else
263 target->path = *pathrec;
264 complete(&target->done);
265}
266
267static int srp_lookup_path(struct srp_target_port *target)
268{
269 target->path.numb_path = 1;
270
271 init_completion(&target->done);
272
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700273 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
274 target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800275 target->srp_host->port,
276 &target->path,
277 IB_SA_PATH_REC_DGID |
278 IB_SA_PATH_REC_SGID |
279 IB_SA_PATH_REC_NUMB_PATH |
280 IB_SA_PATH_REC_PKEY,
281 SRP_PATH_REC_TIMEOUT_MS,
282 GFP_KERNEL,
283 srp_path_rec_completion,
284 target, &target->path_query);
285 if (target->path_query_id < 0)
286 return target->path_query_id;
287
288 wait_for_completion(&target->done);
289
290 if (target->status < 0)
291 printk(KERN_WARNING PFX "Path record query failed\n");
292
293 return target->status;
294}
295
296static int srp_send_req(struct srp_target_port *target)
297{
298 struct {
299 struct ib_cm_req_param param;
300 struct srp_login_req priv;
301 } *req = NULL;
302 int status;
303
304 req = kzalloc(sizeof *req, GFP_KERNEL);
305 if (!req)
306 return -ENOMEM;
307
308 req->param.primary_path = &target->path;
309 req->param.alternate_path = NULL;
310 req->param.service_id = target->service_id;
311 req->param.qp_num = target->qp->qp_num;
312 req->param.qp_type = target->qp->qp_type;
313 req->param.private_data = &req->priv;
314 req->param.private_data_len = sizeof req->priv;
315 req->param.flow_control = 1;
316
317 get_random_bytes(&req->param.starting_psn, 4);
318 req->param.starting_psn &= 0xffffff;
319
320 /*
321 * Pick some arbitrary defaults here; we could make these
322 * module parameters if anyone cared about setting them.
323 */
324 req->param.responder_resources = 4;
325 req->param.remote_cm_response_timeout = 20;
326 req->param.local_cm_response_timeout = 20;
327 req->param.retry_count = 7;
328 req->param.rnr_retry_count = 7;
329 req->param.max_cm_retries = 15;
330
331 req->priv.opcode = SRP_LOGIN_REQ;
332 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700333 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800334 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
335 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700336 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700337 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700338 * port identifier format is 8 bytes of ID extension followed
339 * by 8 bytes of GUID. Older drafts put the two halves in the
340 * opposite order, so that the GUID comes first.
341 *
342 * Targets conforming to these obsolete drafts can be
343 * recognized by the I/O Class they report.
344 */
345 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
346 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200347 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700348 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200349 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700350 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
351 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
352 } else {
353 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200354 &target->initiator_ext, 8);
355 memcpy(req->priv.initiator_port_id + 8,
356 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700357 memcpy(req->priv.target_port_id, &target->id_ext, 8);
358 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
359 }
360
Roland Dreieraef9ec32005-11-02 14:07:13 -0800361 /*
362 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200363 * zero out the first 8 bytes of our initiator port ID and set
364 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800365 */
366 if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
367 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
368 "activated for target GUID %016llx\n",
369 (unsigned long long) be64_to_cpu(target->ioc_guid));
370 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200371 memcpy(req->priv.initiator_port_id + 8,
372 &target->srp_host->dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800373 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800374
375 status = ib_send_cm_req(target->cm_id, &req->param);
376
377 kfree(req);
378
379 return status;
380}
381
382static void srp_disconnect_target(struct srp_target_port *target)
383{
384 /* XXX should send SRP_I_LOGOUT request */
385
386 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700387 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
388 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
389 return;
390 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800391 wait_for_completion(&target->done);
392}
393
David Howellsc4028952006-11-22 14:57:56 +0000394static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800395{
David Howellsc4028952006-11-22 14:57:56 +0000396 struct srp_target_port *target =
397 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800398
399 spin_lock_irq(target->scsi_host->host_lock);
400 if (target->state != SRP_TARGET_DEAD) {
401 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800402 return;
403 }
404 target->state = SRP_TARGET_REMOVED;
405 spin_unlock_irq(target->scsi_host->host_lock);
406
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700407 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800408 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700409 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800410
FUJITA Tomonori32368222007-06-27 16:33:12 +0900411 srp_remove_host(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800412 scsi_remove_host(target->scsi_host);
413 ib_destroy_cm_id(target->cm_id);
414 srp_free_target_ib(target);
415 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800416}
417
418static int srp_connect_target(struct srp_target_port *target)
419{
420 int ret;
421
422 ret = srp_lookup_path(target);
423 if (ret)
424 return ret;
425
426 while (1) {
427 init_completion(&target->done);
428 ret = srp_send_req(target);
429 if (ret)
430 return ret;
431 wait_for_completion(&target->done);
432
433 /*
434 * The CM event handling code will set status to
435 * SRP_PORT_REDIRECT if we get a port redirect REJ
436 * back, or SRP_DLID_REDIRECT if we get a lid/qp
437 * redirect REJ back.
438 */
439 switch (target->status) {
440 case 0:
441 return 0;
442
443 case SRP_PORT_REDIRECT:
444 ret = srp_lookup_path(target);
445 if (ret)
446 return ret;
447 break;
448
449 case SRP_DLID_REDIRECT:
450 break;
451
452 default:
453 return target->status;
454 }
455 }
456}
457
Roland Dreierd945e1d2006-05-09 10:50:28 -0700458static void srp_unmap_data(struct scsi_cmnd *scmnd,
459 struct srp_target_port *target,
460 struct srp_request *req)
461{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900462 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700463 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
464 scmnd->sc_data_direction != DMA_FROM_DEVICE))
465 return;
466
Roland Dreierf5358a12006-06-17 20:37:29 -0700467 if (req->fmr) {
468 ib_fmr_pool_unmap(req->fmr);
469 req->fmr = NULL;
470 }
471
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900472 ib_dma_unmap_sg(target->srp_host->dev->dev, scsi_sglist(scmnd),
473 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700474}
475
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700476static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
477{
478 srp_unmap_data(req->scmnd, target, req);
479 list_move_tail(&req->list, &target->free_reqs);
480}
481
482static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
483{
484 req->scmnd->result = DID_RESET << 16;
485 req->scmnd->scsi_done(req->scmnd);
486 srp_remove_req(target, req);
487}
488
Roland Dreieraef9ec32005-11-02 14:07:13 -0800489static int srp_reconnect_target(struct srp_target_port *target)
490{
491 struct ib_cm_id *new_cm_id;
492 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700493 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800494 struct ib_wc wc;
495 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800496
497 spin_lock_irq(target->scsi_host->host_lock);
498 if (target->state != SRP_TARGET_LIVE) {
499 spin_unlock_irq(target->scsi_host->host_lock);
500 return -EAGAIN;
501 }
502 target->state = SRP_TARGET_CONNECTING;
503 spin_unlock_irq(target->scsi_host->host_lock);
504
505 srp_disconnect_target(target);
506 /*
507 * Now get a new local CM ID so that we avoid confusing the
508 * target in case things are really fouled up.
509 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700510 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800511 srp_cm_handler, target);
512 if (IS_ERR(new_cm_id)) {
513 ret = PTR_ERR(new_cm_id);
514 goto err;
515 }
516 ib_destroy_cm_id(target->cm_id);
517 target->cm_id = new_cm_id;
518
519 qp_attr.qp_state = IB_QPS_RESET;
520 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
521 if (ret)
522 goto err;
523
524 ret = srp_init_qp(target, target->qp);
525 if (ret)
526 goto err;
527
528 while (ib_poll_cq(target->cq, 1, &wc) > 0)
529 ; /* nothing */
530
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300531 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700532 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
533 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300534 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800535
536 target->rx_head = 0;
537 target->tx_head = 0;
538 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800539
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200540 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800541 ret = srp_connect_target(target);
542 if (ret)
543 goto err;
544
545 spin_lock_irq(target->scsi_host->host_lock);
546 if (target->state == SRP_TARGET_CONNECTING) {
547 ret = 0;
548 target->state = SRP_TARGET_LIVE;
549 } else
550 ret = -EAGAIN;
551 spin_unlock_irq(target->scsi_host->host_lock);
552
553 return ret;
554
555err:
556 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
557
558 /*
559 * We couldn't reconnect, so kill our target port off.
560 * However, we have to defer the real removal because we might
561 * be in the context of the SCSI error handler now, which
562 * would deadlock if we call scsi_remove_host().
563 */
564 spin_lock_irq(target->scsi_host->host_lock);
565 if (target->state == SRP_TARGET_CONNECTING) {
566 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000567 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800568 schedule_work(&target->work);
569 }
570 spin_unlock_irq(target->scsi_host->host_lock);
571
572 return ret;
573}
574
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700575static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700576 int sg_cnt, struct srp_request *req,
577 struct srp_direct_buf *buf)
578{
579 u64 io_addr = 0;
580 u64 *dma_pages;
581 u32 len;
582 int page_cnt;
583 int i, j;
584 int ret;
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700585 struct srp_device *dev = target->srp_host->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800586 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900587 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700588
589 if (!dev->fmr_pool)
590 return -ENODEV;
591
Ralph Campbell85507bc2006-12-12 14:30:55 -0800592 if ((ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask) &&
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700593 mellanox_workarounds && !memcmp(&target->ioc_guid, mellanox_oui, 3))
594 return -EINVAL;
595
Roland Dreierf5358a12006-06-17 20:37:29 -0700596 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900597 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
598 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800599
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900600 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700601 if (i > 0)
602 return -EINVAL;
603 else
604 ++page_cnt;
605 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900606 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700607 ~dev->fmr_page_mask) {
608 if (i < sg_cnt - 1)
609 return -EINVAL;
610 else
611 ++page_cnt;
612 }
613
Ralph Campbell85507bc2006-12-12 14:30:55 -0800614 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700615 }
616
617 page_cnt += len >> dev->fmr_page_shift;
618 if (page_cnt > SRP_FMR_SIZE)
619 return -ENOMEM;
620
621 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
622 if (!dma_pages)
623 return -ENOMEM;
624
625 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900626 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
627 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800628
629 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700630 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900631 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800632 dev->fmr_page_mask) + j;
633 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700634
635 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700636 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700637 if (IS_ERR(req->fmr)) {
638 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700639 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700640 goto out;
641 }
642
Ralph Campbell85507bc2006-12-12 14:30:55 -0800643 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
644 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700645 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
646 buf->len = cpu_to_be32(len);
647
648 ret = 0;
649
650out:
651 kfree(dma_pages);
652
653 return ret;
654}
655
Roland Dreieraef9ec32005-11-02 14:07:13 -0800656static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
657 struct srp_request *req)
658{
Roland Dreiercf368712006-03-24 15:47:26 -0800659 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800660 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800661 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700662 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800663 struct srp_device *dev;
664 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800665
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900666 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800667 return sizeof (struct srp_cmd);
668
669 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
670 scmnd->sc_data_direction != DMA_TO_DEVICE) {
671 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
672 scmnd->sc_data_direction);
673 return -EINVAL;
674 }
675
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900676 nents = scsi_sg_count(scmnd);
677 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800678
Ralph Campbell85507bc2006-12-12 14:30:55 -0800679 dev = target->srp_host->dev;
680 ibdev = dev->dev;
681
682 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700683
684 fmt = SRP_DATA_DESC_DIRECT;
685 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800686
687 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700688 /*
689 * The midlayer only generated a single gather/scatter
690 * entry, or DMA mapping coalesced everything to a
691 * single entry. So a direct descriptor along with
692 * the DMA MR suffices.
693 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800694 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800695
Ralph Campbell85507bc2006-12-12 14:30:55 -0800696 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
697 buf->key = cpu_to_be32(dev->mr->rkey);
698 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700699 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700700 (void *) cmd->add_data)) {
701 /*
702 * FMR mapping failed, and the scatterlist has more
703 * than one entry. Generate an indirect memory
704 * descriptor.
705 */
Roland Dreiercf368712006-03-24 15:47:26 -0800706 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900707 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800708 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700709 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800710
711 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700712 len = sizeof (struct srp_cmd) +
713 sizeof (struct srp_indirect_buf) +
714 count * sizeof (struct srp_direct_buf);
715
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900716 scsi_for_each_sg(scmnd, sg, count, i) {
717 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800718
Roland Dreierf5358a12006-06-17 20:37:29 -0700719 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900720 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700721 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800722 cpu_to_be32(dev->mr->rkey);
723 buf->desc_list[i].len = cpu_to_be32(dma_len);
724 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700725 }
Roland Dreiercf368712006-03-24 15:47:26 -0800726
727 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
728 cmd->data_out_desc_cnt = count;
729 else
730 cmd->data_in_desc_cnt = count;
731
Roland Dreierf5358a12006-06-17 20:37:29 -0700732 buf->table_desc.va =
733 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800734 buf->table_desc.key =
Roland Dreierf5358a12006-06-17 20:37:29 -0700735 cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800736 buf->table_desc.len =
737 cpu_to_be32(count * sizeof (struct srp_direct_buf));
738
Roland Dreiercf368712006-03-24 15:47:26 -0800739 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800740 }
741
742 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
743 cmd->buf_fmt = fmt << 4;
744 else
745 cmd->buf_fmt = fmt;
746
Roland Dreieraef9ec32005-11-02 14:07:13 -0800747 return len;
748}
749
Roland Dreieraef9ec32005-11-02 14:07:13 -0800750static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
751{
752 struct srp_request *req;
753 struct scsi_cmnd *scmnd;
754 unsigned long flags;
755 s32 delta;
756
757 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
758
759 spin_lock_irqsave(target->scsi_host->host_lock, flags);
760
761 target->req_lim += delta;
762
763 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
764
765 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
766 if (be32_to_cpu(rsp->resp_data_len) < 4)
767 req->tsk_status = -1;
768 else
769 req->tsk_status = rsp->data[3];
770 complete(&req->done);
771 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700772 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800773 if (!scmnd)
774 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
775 (unsigned long long) rsp->tag);
776 scmnd->result = rsp->status;
777
778 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
779 memcpy(scmnd->sense_buffer, rsp->data +
780 be32_to_cpu(rsp->resp_data_len),
781 min_t(int, be32_to_cpu(rsp->sense_data_len),
782 SCSI_SENSE_BUFFERSIZE));
783 }
784
785 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900786 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800787 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900788 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800789
Roland Dreieraef9ec32005-11-02 14:07:13 -0800790 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800791 scmnd->host_scribble = (void *) -1L;
792 scmnd->scsi_done(scmnd);
793
Roland Dreierd945e1d2006-05-09 10:50:28 -0700794 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800795 } else
796 req->cmd_done = 1;
797 }
798
799 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
800}
801
Roland Dreieraef9ec32005-11-02 14:07:13 -0800802static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
803{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800804 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800805 struct srp_iu *iu;
806 u8 opcode;
807
808 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
809
Ralph Campbell85507bc2006-12-12 14:30:55 -0800810 dev = target->srp_host->dev->dev;
811 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
812 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800813
814 opcode = *(u8 *) iu->buf;
815
816 if (0) {
817 int i;
818
819 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
820
821 for (i = 0; i < wc->byte_len; ++i) {
822 if (i % 8 == 0)
823 printk(KERN_ERR " [%02x] ", i);
824 printk(" %02x", ((u8 *) iu->buf)[i]);
825 if ((i + 1) % 8 == 0)
826 printk("\n");
827 }
828
829 if (wc->byte_len % 8)
830 printk("\n");
831 }
832
833 switch (opcode) {
834 case SRP_RSP:
835 srp_process_rsp(target, iu->buf);
836 break;
837
838 case SRP_T_LOGOUT:
839 /* XXX Handle target logout */
840 printk(KERN_WARNING PFX "Got target logout request\n");
841 break;
842
843 default:
844 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
845 break;
846 }
847
Ralph Campbell85507bc2006-12-12 14:30:55 -0800848 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
849 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800850}
851
852static void srp_completion(struct ib_cq *cq, void *target_ptr)
853{
854 struct srp_target_port *target = target_ptr;
855 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800856
857 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
858 while (ib_poll_cq(cq, 1, &wc) > 0) {
859 if (wc.status) {
860 printk(KERN_ERR PFX "failed %s status %d\n",
861 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
862 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200863 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800864 break;
865 }
866
867 if (wc.wr_id & SRP_OP_RECV)
868 srp_handle_recv(target, &wc);
869 else
870 ++target->tx_tail;
871 }
872}
873
874static int __srp_post_recv(struct srp_target_port *target)
875{
876 struct srp_iu *iu;
877 struct ib_sge list;
878 struct ib_recv_wr wr, *bad_wr;
879 unsigned int next;
880 int ret;
881
882 next = target->rx_head & (SRP_RQ_SIZE - 1);
883 wr.wr_id = next | SRP_OP_RECV;
884 iu = target->rx_ring[next];
885
886 list.addr = iu->dma;
887 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700888 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800889
890 wr.next = NULL;
891 wr.sg_list = &list;
892 wr.num_sge = 1;
893
894 ret = ib_post_recv(target->qp, &wr, &bad_wr);
895 if (!ret)
896 ++target->rx_head;
897
898 return ret;
899}
900
901static int srp_post_recv(struct srp_target_port *target)
902{
903 unsigned long flags;
904 int ret;
905
906 spin_lock_irqsave(target->scsi_host->host_lock, flags);
907 ret = __srp_post_recv(target);
908 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
909
910 return ret;
911}
912
913/*
914 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800915 * req_lim and tx_head. Lock cannot be dropped between call here and
916 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800917 */
918static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
919{
920 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
921 return NULL;
922
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700923 if (unlikely(target->req_lim < 1))
924 ++target->zero_req_lim;
Roland Dreier47f2bce2005-11-15 00:19:21 -0800925
Roland Dreieraef9ec32005-11-02 14:07:13 -0800926 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
927}
928
929/*
930 * Must be called with target->scsi_host->host_lock held to protect
931 * req_lim and tx_head.
932 */
933static int __srp_post_send(struct srp_target_port *target,
934 struct srp_iu *iu, int len)
935{
936 struct ib_sge list;
937 struct ib_send_wr wr, *bad_wr;
938 int ret = 0;
939
Roland Dreieraef9ec32005-11-02 14:07:13 -0800940 list.addr = iu->dma;
941 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700942 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800943
944 wr.next = NULL;
945 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
946 wr.sg_list = &list;
947 wr.num_sge = 1;
948 wr.opcode = IB_WR_SEND;
949 wr.send_flags = IB_SEND_SIGNALED;
950
951 ret = ib_post_send(target->qp, &wr, &bad_wr);
952
953 if (!ret) {
954 ++target->tx_head;
955 --target->req_lim;
956 }
957
958 return ret;
959}
960
961static int srp_queuecommand(struct scsi_cmnd *scmnd,
962 void (*done)(struct scsi_cmnd *))
963{
964 struct srp_target_port *target = host_to_target(scmnd->device->host);
965 struct srp_request *req;
966 struct srp_iu *iu;
967 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800968 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800969 int len;
970
971 if (target->state == SRP_TARGET_CONNECTING)
972 goto err;
973
974 if (target->state == SRP_TARGET_DEAD ||
975 target->state == SRP_TARGET_REMOVED) {
976 scmnd->result = DID_BAD_TARGET << 16;
977 done(scmnd);
978 return 0;
979 }
980
981 iu = __srp_get_tx_iu(target);
982 if (!iu)
983 goto err;
984
Ralph Campbell85507bc2006-12-12 14:30:55 -0800985 dev = target->srp_host->dev->dev;
986 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
987 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800988
Roland Dreierd945e1d2006-05-09 10:50:28 -0700989 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800990
991 scmnd->scsi_done = done;
992 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -0700993 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800994
995 cmd = iu->buf;
996 memset(cmd, 0, sizeof *cmd);
997
998 cmd->opcode = SRP_CMD;
999 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001000 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001001 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1002
Roland Dreieraef9ec32005-11-02 14:07:13 -08001003 req->scmnd = scmnd;
1004 req->cmd = iu;
1005 req->cmd_done = 0;
1006 req->tsk_mgmt = NULL;
1007
1008 len = srp_map_data(scmnd, target, req);
1009 if (len < 0) {
1010 printk(KERN_ERR PFX "Failed to map data\n");
1011 goto err;
1012 }
1013
1014 if (__srp_post_recv(target)) {
1015 printk(KERN_ERR PFX "Recv failed\n");
1016 goto err_unmap;
1017 }
1018
Ralph Campbell85507bc2006-12-12 14:30:55 -08001019 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1020 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001021
1022 if (__srp_post_send(target, iu, len)) {
1023 printk(KERN_ERR PFX "Send failed\n");
1024 goto err_unmap;
1025 }
1026
Roland Dreierd945e1d2006-05-09 10:50:28 -07001027 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001028
1029 return 0;
1030
1031err_unmap:
1032 srp_unmap_data(scmnd, target, req);
1033
1034err:
1035 return SCSI_MLQUEUE_HOST_BUSY;
1036}
1037
1038static int srp_alloc_iu_bufs(struct srp_target_port *target)
1039{
1040 int i;
1041
1042 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1043 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1044 target->max_ti_iu_len,
1045 GFP_KERNEL, DMA_FROM_DEVICE);
1046 if (!target->rx_ring[i])
1047 goto err;
1048 }
1049
1050 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1051 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001052 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001053 GFP_KERNEL, DMA_TO_DEVICE);
1054 if (!target->tx_ring[i])
1055 goto err;
1056 }
1057
1058 return 0;
1059
1060err:
1061 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1062 srp_free_iu(target->srp_host, target->rx_ring[i]);
1063 target->rx_ring[i] = NULL;
1064 }
1065
1066 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1067 srp_free_iu(target->srp_host, target->tx_ring[i]);
1068 target->tx_ring[i] = NULL;
1069 }
1070
1071 return -ENOMEM;
1072}
1073
1074static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1075 struct ib_cm_event *event,
1076 struct srp_target_port *target)
1077{
1078 struct ib_class_port_info *cpi;
1079 int opcode;
1080
1081 switch (event->param.rej_rcvd.reason) {
1082 case IB_CM_REJ_PORT_CM_REDIRECT:
1083 cpi = event->param.rej_rcvd.ari;
1084 target->path.dlid = cpi->redirect_lid;
1085 target->path.pkey = cpi->redirect_pkey;
1086 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1087 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1088
1089 target->status = target->path.dlid ?
1090 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1091 break;
1092
1093 case IB_CM_REJ_PORT_REDIRECT:
1094 if (topspin_workarounds &&
1095 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1096 /*
1097 * Topspin/Cisco SRP gateways incorrectly send
1098 * reject reason code 25 when they mean 24
1099 * (port redirect).
1100 */
1101 memcpy(target->path.dgid.raw,
1102 event->param.rej_rcvd.ari, 16);
1103
1104 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1105 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1106 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1107
1108 target->status = SRP_PORT_REDIRECT;
1109 } else {
1110 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1111 target->status = -ECONNRESET;
1112 }
1113 break;
1114
1115 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1116 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1117 target->status = -ECONNRESET;
1118 break;
1119
1120 case IB_CM_REJ_CONSUMER_DEFINED:
1121 opcode = *(u8 *) event->private_data;
1122 if (opcode == SRP_LOGIN_REJ) {
1123 struct srp_login_rej *rej = event->private_data;
1124 u32 reason = be32_to_cpu(rej->reason);
1125
1126 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1127 printk(KERN_WARNING PFX
1128 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1129 else
1130 printk(KERN_WARNING PFX
1131 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1132 } else
1133 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1134 " opcode 0x%02x\n", opcode);
1135 target->status = -ECONNRESET;
1136 break;
1137
1138 default:
1139 printk(KERN_WARNING " REJ reason 0x%x\n",
1140 event->param.rej_rcvd.reason);
1141 target->status = -ECONNRESET;
1142 }
1143}
1144
1145static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1146{
1147 struct srp_target_port *target = cm_id->context;
1148 struct ib_qp_attr *qp_attr = NULL;
1149 int attr_mask = 0;
1150 int comp = 0;
1151 int opcode = 0;
1152
1153 switch (event->event) {
1154 case IB_CM_REQ_ERROR:
1155 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1156 comp = 1;
1157 target->status = -ECONNRESET;
1158 break;
1159
1160 case IB_CM_REP_RECEIVED:
1161 comp = 1;
1162 opcode = *(u8 *) event->private_data;
1163
1164 if (opcode == SRP_LOGIN_RSP) {
1165 struct srp_login_rsp *rsp = event->private_data;
1166
1167 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1168 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1169
1170 target->scsi_host->can_queue = min(target->req_lim,
1171 target->scsi_host->can_queue);
1172 } else {
1173 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1174 target->status = -ECONNRESET;
1175 break;
1176 }
1177
Vu Phamd2fcea72006-11-21 14:14:10 -08001178 if (!target->rx_ring[0]) {
1179 target->status = srp_alloc_iu_bufs(target);
1180 if (target->status)
1181 break;
1182 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001183
1184 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1185 if (!qp_attr) {
1186 target->status = -ENOMEM;
1187 break;
1188 }
1189
1190 qp_attr->qp_state = IB_QPS_RTR;
1191 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1192 if (target->status)
1193 break;
1194
1195 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1196 if (target->status)
1197 break;
1198
1199 target->status = srp_post_recv(target);
1200 if (target->status)
1201 break;
1202
1203 qp_attr->qp_state = IB_QPS_RTS;
1204 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1205 if (target->status)
1206 break;
1207
1208 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1209 if (target->status)
1210 break;
1211
1212 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1213 if (target->status)
1214 break;
1215
1216 break;
1217
1218 case IB_CM_REJ_RECEIVED:
1219 printk(KERN_DEBUG PFX "REJ received\n");
1220 comp = 1;
1221
1222 srp_cm_rej_handler(cm_id, event, target);
1223 break;
1224
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001225 case IB_CM_DREQ_RECEIVED:
1226 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1227 if (ib_send_cm_drep(cm_id, NULL, 0))
1228 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001229 break;
1230
1231 case IB_CM_TIMEWAIT_EXIT:
1232 printk(KERN_ERR PFX "connection closed\n");
1233
1234 comp = 1;
1235 target->status = 0;
1236 break;
1237
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001238 case IB_CM_MRA_RECEIVED:
1239 case IB_CM_DREQ_ERROR:
1240 case IB_CM_DREP_RECEIVED:
1241 break;
1242
Roland Dreieraef9ec32005-11-02 14:07:13 -08001243 default:
1244 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1245 break;
1246 }
1247
1248 if (comp)
1249 complete(&target->done);
1250
1251 kfree(qp_attr);
1252
1253 return 0;
1254}
1255
Roland Dreierd945e1d2006-05-09 10:50:28 -07001256static int srp_send_tsk_mgmt(struct srp_target_port *target,
1257 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001258{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001259 struct srp_iu *iu;
1260 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001261
1262 spin_lock_irq(target->scsi_host->host_lock);
1263
Roland Dreier1285b3a2006-03-03 15:47:25 -08001264 if (target->state == SRP_TARGET_DEAD ||
1265 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001266 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001267 goto out;
1268 }
1269
Roland Dreieraef9ec32005-11-02 14:07:13 -08001270 init_completion(&req->done);
1271
1272 iu = __srp_get_tx_iu(target);
1273 if (!iu)
1274 goto out;
1275
1276 tsk_mgmt = iu->buf;
1277 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1278
1279 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001280 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1281 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001282 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001283 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001284
1285 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1286 goto out;
1287
1288 req->tsk_mgmt = iu;
1289
1290 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001291
Roland Dreieraef9ec32005-11-02 14:07:13 -08001292 if (!wait_for_completion_timeout(&req->done,
1293 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001294 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001295
Roland Dreierd945e1d2006-05-09 10:50:28 -07001296 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001297
1298out:
1299 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001300 return -1;
1301}
1302
1303static int srp_find_req(struct srp_target_port *target,
1304 struct scsi_cmnd *scmnd,
1305 struct srp_request **req)
1306{
1307 if (scmnd->host_scribble == (void *) -1L)
1308 return -1;
1309
1310 *req = &target->req_ring[(long) scmnd->host_scribble];
1311
1312 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001313}
1314
1315static int srp_abort(struct scsi_cmnd *scmnd)
1316{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001317 struct srp_target_port *target = host_to_target(scmnd->device->host);
1318 struct srp_request *req;
1319 int ret = SUCCESS;
1320
Roland Dreieraef9ec32005-11-02 14:07:13 -08001321 printk(KERN_ERR "SRP abort called\n");
1322
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001323 if (target->qp_in_error)
1324 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001325 if (srp_find_req(target, scmnd, &req))
1326 return FAILED;
1327 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1328 return FAILED;
1329
1330 spin_lock_irq(target->scsi_host->host_lock);
1331
1332 if (req->cmd_done) {
1333 srp_remove_req(target, req);
1334 scmnd->scsi_done(scmnd);
1335 } else if (!req->tsk_status) {
1336 srp_remove_req(target, req);
1337 scmnd->result = DID_ABORT << 16;
1338 } else
1339 ret = FAILED;
1340
1341 spin_unlock_irq(target->scsi_host->host_lock);
1342
1343 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001344}
1345
1346static int srp_reset_device(struct scsi_cmnd *scmnd)
1347{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001348 struct srp_target_port *target = host_to_target(scmnd->device->host);
1349 struct srp_request *req, *tmp;
1350
Roland Dreieraef9ec32005-11-02 14:07:13 -08001351 printk(KERN_ERR "SRP reset_device called\n");
1352
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001353 if (target->qp_in_error)
1354 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001355 if (srp_find_req(target, scmnd, &req))
1356 return FAILED;
1357 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1358 return FAILED;
1359 if (req->tsk_status)
1360 return FAILED;
1361
1362 spin_lock_irq(target->scsi_host->host_lock);
1363
1364 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001365 if (req->scmnd->device == scmnd->device)
1366 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001367
1368 spin_unlock_irq(target->scsi_host->host_lock);
1369
1370 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001371}
1372
1373static int srp_reset_host(struct scsi_cmnd *scmnd)
1374{
1375 struct srp_target_port *target = host_to_target(scmnd->device->host);
1376 int ret = FAILED;
1377
1378 printk(KERN_ERR PFX "SRP reset_host called\n");
1379
1380 if (!srp_reconnect_target(target))
1381 ret = SUCCESS;
1382
1383 return ret;
1384}
1385
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001386static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1387{
1388 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1389
1390 if (target->state == SRP_TARGET_DEAD ||
1391 target->state == SRP_TARGET_REMOVED)
1392 return -ENODEV;
1393
1394 return sprintf(buf, "0x%016llx\n",
1395 (unsigned long long) be64_to_cpu(target->id_ext));
1396}
1397
1398static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1399{
1400 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1401
1402 if (target->state == SRP_TARGET_DEAD ||
1403 target->state == SRP_TARGET_REMOVED)
1404 return -ENODEV;
1405
1406 return sprintf(buf, "0x%016llx\n",
1407 (unsigned long long) be64_to_cpu(target->ioc_guid));
1408}
1409
1410static ssize_t show_service_id(struct class_device *cdev, char *buf)
1411{
1412 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1413
1414 if (target->state == SRP_TARGET_DEAD ||
1415 target->state == SRP_TARGET_REMOVED)
1416 return -ENODEV;
1417
1418 return sprintf(buf, "0x%016llx\n",
1419 (unsigned long long) be64_to_cpu(target->service_id));
1420}
1421
1422static ssize_t show_pkey(struct class_device *cdev, char *buf)
1423{
1424 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1425
1426 if (target->state == SRP_TARGET_DEAD ||
1427 target->state == SRP_TARGET_REMOVED)
1428 return -ENODEV;
1429
1430 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1431}
1432
1433static ssize_t show_dgid(struct class_device *cdev, char *buf)
1434{
1435 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1436
1437 if (target->state == SRP_TARGET_DEAD ||
1438 target->state == SRP_TARGET_REMOVED)
1439 return -ENODEV;
1440
1441 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1442 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1443 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1444 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1445 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1446 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1447 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1448 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1449 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1450}
1451
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001452static ssize_t show_orig_dgid(struct class_device *cdev, char *buf)
1453{
1454 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1455
1456 if (target->state == SRP_TARGET_DEAD ||
1457 target->state == SRP_TARGET_REMOVED)
1458 return -ENODEV;
1459
1460 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1461 be16_to_cpu(target->orig_dgid[0]),
1462 be16_to_cpu(target->orig_dgid[1]),
1463 be16_to_cpu(target->orig_dgid[2]),
1464 be16_to_cpu(target->orig_dgid[3]),
1465 be16_to_cpu(target->orig_dgid[4]),
1466 be16_to_cpu(target->orig_dgid[5]),
1467 be16_to_cpu(target->orig_dgid[6]),
1468 be16_to_cpu(target->orig_dgid[7]));
1469}
1470
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001471static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1472{
1473 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1474
1475 if (target->state == SRP_TARGET_DEAD ||
1476 target->state == SRP_TARGET_REMOVED)
1477 return -ENODEV;
1478
1479 return sprintf(buf, "%d\n", target->zero_req_lim);
1480}
1481
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001482static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1483{
1484 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1485
1486 return sprintf(buf, "%d\n", target->srp_host->port);
1487}
1488
1489static ssize_t show_local_ib_device(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, "%s\n", target->srp_host->dev->dev->name);
1494}
1495
1496static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1497static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1498static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1499static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1500static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001501static CLASS_DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001502static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1503static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1504static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001505
1506static struct class_device_attribute *srp_host_attrs[] = {
1507 &class_device_attr_id_ext,
1508 &class_device_attr_ioc_guid,
1509 &class_device_attr_service_id,
1510 &class_device_attr_pkey,
1511 &class_device_attr_dgid,
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001512 &class_device_attr_orig_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001513 &class_device_attr_zero_req_lim,
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001514 &class_device_attr_local_ib_port,
1515 &class_device_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001516 NULL
1517};
1518
Roland Dreieraef9ec32005-11-02 14:07:13 -08001519static struct scsi_host_template srp_template = {
1520 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001521 .name = "InfiniBand SRP initiator",
1522 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001523 .info = srp_target_info,
1524 .queuecommand = srp_queuecommand,
1525 .eh_abort_handler = srp_abort,
1526 .eh_device_reset_handler = srp_reset_device,
1527 .eh_host_reset_handler = srp_reset_host,
1528 .can_queue = SRP_SQ_SIZE,
1529 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001530 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001531 .use_clustering = ENABLE_CLUSTERING,
1532 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001533};
1534
1535static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1536{
FUJITA Tomonori32368222007-06-27 16:33:12 +09001537 struct srp_rport_identifiers ids;
1538 struct srp_rport *rport;
1539
Roland Dreieraef9ec32005-11-02 14:07:13 -08001540 sprintf(target->target_name, "SRP.T10:%016llX",
1541 (unsigned long long) be64_to_cpu(target->id_ext));
1542
Roland Dreierf5358a12006-06-17 20:37:29 -07001543 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001544 return -ENODEV;
1545
FUJITA Tomonori32368222007-06-27 16:33:12 +09001546 memcpy(ids.port_id, &target->id_ext, 8);
1547 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +09001548 ids.roles = SRP_RPORT_ROLE_TARGET;
FUJITA Tomonori32368222007-06-27 16:33:12 +09001549 rport = srp_rport_add(target->scsi_host, &ids);
1550 if (IS_ERR(rport)) {
1551 scsi_remove_host(target->scsi_host);
1552 return PTR_ERR(rport);
1553 }
1554
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001555 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001556 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001557 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001558
1559 target->state = SRP_TARGET_LIVE;
1560
Roland Dreieraef9ec32005-11-02 14:07:13 -08001561 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001562 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001563
1564 return 0;
1565}
1566
1567static void srp_release_class_dev(struct class_device *class_dev)
1568{
1569 struct srp_host *host =
1570 container_of(class_dev, struct srp_host, class_dev);
1571
1572 complete(&host->released);
1573}
1574
1575static struct class srp_class = {
1576 .name = "infiniband_srp",
1577 .release = srp_release_class_dev
1578};
1579
1580/*
1581 * Target ports are added by writing
1582 *
1583 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1584 * pkey=<P_Key>,service_id=<service ID>
1585 *
1586 * to the add_target sysfs attribute.
1587 */
1588enum {
1589 SRP_OPT_ERR = 0,
1590 SRP_OPT_ID_EXT = 1 << 0,
1591 SRP_OPT_IOC_GUID = 1 << 1,
1592 SRP_OPT_DGID = 1 << 2,
1593 SRP_OPT_PKEY = 1 << 3,
1594 SRP_OPT_SERVICE_ID = 1 << 4,
1595 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001596 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001597 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001598 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001599 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1600 SRP_OPT_IOC_GUID |
1601 SRP_OPT_DGID |
1602 SRP_OPT_PKEY |
1603 SRP_OPT_SERVICE_ID),
1604};
1605
1606static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001607 { SRP_OPT_ID_EXT, "id_ext=%s" },
1608 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1609 { SRP_OPT_DGID, "dgid=%s" },
1610 { SRP_OPT_PKEY, "pkey=%x" },
1611 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1612 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1613 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001614 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001615 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001616 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001617};
1618
1619static int srp_parse_options(const char *buf, struct srp_target_port *target)
1620{
1621 char *options, *sep_opt;
1622 char *p;
1623 char dgid[3];
1624 substring_t args[MAX_OPT_ARGS];
1625 int opt_mask = 0;
1626 int token;
1627 int ret = -EINVAL;
1628 int i;
1629
1630 options = kstrdup(buf, GFP_KERNEL);
1631 if (!options)
1632 return -ENOMEM;
1633
1634 sep_opt = options;
1635 while ((p = strsep(&sep_opt, ",")) != NULL) {
1636 if (!*p)
1637 continue;
1638
1639 token = match_token(p, srp_opt_tokens, args);
1640 opt_mask |= token;
1641
1642 switch (token) {
1643 case SRP_OPT_ID_EXT:
1644 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001645 if (!p) {
1646 ret = -ENOMEM;
1647 goto out;
1648 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001649 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1650 kfree(p);
1651 break;
1652
1653 case SRP_OPT_IOC_GUID:
1654 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001655 if (!p) {
1656 ret = -ENOMEM;
1657 goto out;
1658 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001659 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1660 kfree(p);
1661 break;
1662
1663 case SRP_OPT_DGID:
1664 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001665 if (!p) {
1666 ret = -ENOMEM;
1667 goto out;
1668 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001669 if (strlen(p) != 32) {
1670 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001671 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001672 goto out;
1673 }
1674
1675 for (i = 0; i < 16; ++i) {
1676 strlcpy(dgid, p + i * 2, 3);
1677 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1678 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001679 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001680 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001681 break;
1682
1683 case SRP_OPT_PKEY:
1684 if (match_hex(args, &token)) {
1685 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1686 goto out;
1687 }
1688 target->path.pkey = cpu_to_be16(token);
1689 break;
1690
1691 case SRP_OPT_SERVICE_ID:
1692 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001693 if (!p) {
1694 ret = -ENOMEM;
1695 goto out;
1696 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001697 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1698 kfree(p);
1699 break;
1700
1701 case SRP_OPT_MAX_SECT:
1702 if (match_int(args, &token)) {
1703 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1704 goto out;
1705 }
1706 target->scsi_host->max_sectors = token;
1707 break;
1708
Vu Pham52fb2b502006-06-17 20:37:31 -07001709 case SRP_OPT_MAX_CMD_PER_LUN:
1710 if (match_int(args, &token)) {
1711 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1712 goto out;
1713 }
1714 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1715 break;
1716
Ramachandra K0c0450db2006-06-17 20:37:38 -07001717 case SRP_OPT_IO_CLASS:
1718 if (match_hex(args, &token)) {
1719 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1720 goto out;
1721 }
1722 if (token != SRP_REV10_IB_IO_CLASS &&
1723 token != SRP_REV16A_IB_IO_CLASS) {
1724 printk(KERN_WARNING PFX "unknown IO class parameter value"
1725 " %x specified (use %x or %x).\n",
1726 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1727 goto out;
1728 }
1729 target->io_class = token;
1730 break;
1731
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001732 case SRP_OPT_INITIATOR_EXT:
1733 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001734 if (!p) {
1735 ret = -ENOMEM;
1736 goto out;
1737 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001738 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1739 kfree(p);
1740 break;
1741
Roland Dreieraef9ec32005-11-02 14:07:13 -08001742 default:
1743 printk(KERN_WARNING PFX "unknown parameter or missing value "
1744 "'%s' in target creation request\n", p);
1745 goto out;
1746 }
1747 }
1748
1749 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1750 ret = 0;
1751 else
1752 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1753 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1754 !(srp_opt_tokens[i].token & opt_mask))
1755 printk(KERN_WARNING PFX "target creation request is "
1756 "missing parameter '%s'\n",
1757 srp_opt_tokens[i].pattern);
1758
1759out:
1760 kfree(options);
1761 return ret;
1762}
1763
1764static ssize_t srp_create_target(struct class_device *class_dev,
1765 const char *buf, size_t count)
1766{
1767 struct srp_host *host =
1768 container_of(class_dev, struct srp_host, class_dev);
1769 struct Scsi_Host *target_host;
1770 struct srp_target_port *target;
1771 int ret;
1772 int i;
1773
1774 target_host = scsi_host_alloc(&srp_template,
1775 sizeof (struct srp_target_port));
1776 if (!target_host)
1777 return -ENOMEM;
1778
FUJITA Tomonori32368222007-06-27 16:33:12 +09001779 target_host->transportt = ib_srp_transport_template;
Arne Redlich3c8edf02006-11-15 12:43:00 +01001780 target_host->max_lun = SRP_MAX_LUN;
1781 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001782
Roland Dreieraef9ec32005-11-02 14:07:13 -08001783 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001784
Ramachandra K0c0450db2006-06-17 20:37:38 -07001785 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001786 target->scsi_host = target_host;
1787 target->srp_host = host;
1788
Roland Dreierd945e1d2006-05-09 10:50:28 -07001789 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001790 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001791 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1792 target->req_ring[i].index = i;
1793 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1794 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001795
1796 ret = srp_parse_options(buf, target);
1797 if (ret)
1798 goto err;
1799
Roland Dreierf5358a12006-06-17 20:37:29 -07001800 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001801
1802 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1803 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1804 (unsigned long long) be64_to_cpu(target->id_ext),
1805 (unsigned long long) be64_to_cpu(target->ioc_guid),
1806 be16_to_cpu(target->path.pkey),
1807 (unsigned long long) be64_to_cpu(target->service_id),
1808 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1809 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1810 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1811 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1812 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1813 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1814 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1815 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1816
1817 ret = srp_create_target_ib(target);
1818 if (ret)
1819 goto err;
1820
Roland Dreierf5358a12006-06-17 20:37:29 -07001821 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001822 if (IS_ERR(target->cm_id)) {
1823 ret = PTR_ERR(target->cm_id);
1824 goto err_free;
1825 }
1826
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001827 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001828 ret = srp_connect_target(target);
1829 if (ret) {
1830 printk(KERN_ERR PFX "Connection failed\n");
1831 goto err_cm_id;
1832 }
1833
1834 ret = srp_add_target(host, target);
1835 if (ret)
1836 goto err_disconnect;
1837
1838 return count;
1839
1840err_disconnect:
1841 srp_disconnect_target(target);
1842
1843err_cm_id:
1844 ib_destroy_cm_id(target->cm_id);
1845
1846err_free:
1847 srp_free_target_ib(target);
1848
1849err:
1850 scsi_host_put(target_host);
1851
1852 return ret;
1853}
1854
1855static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1856
1857static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1858{
1859 struct srp_host *host =
1860 container_of(class_dev, struct srp_host, class_dev);
1861
Roland Dreierf5358a12006-06-17 20:37:29 -07001862 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001863}
1864
1865static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1866
1867static ssize_t show_port(struct class_device *class_dev, char *buf)
1868{
1869 struct srp_host *host =
1870 container_of(class_dev, struct srp_host, class_dev);
1871
1872 return sprintf(buf, "%d\n", host->port);
1873}
1874
1875static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1876
Roland Dreierf5358a12006-06-17 20:37:29 -07001877static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001878{
1879 struct srp_host *host;
1880
1881 host = kzalloc(sizeof *host, GFP_KERNEL);
1882 if (!host)
1883 return NULL;
1884
1885 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001886 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001887 init_completion(&host->released);
1888 host->dev = device;
1889 host->port = port;
1890
Roland Dreieraef9ec32005-11-02 14:07:13 -08001891 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001892 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001893 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001894 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001895
1896 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001897 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001898 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1899 goto err_class;
1900 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1901 goto err_class;
1902 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1903 goto err_class;
1904
1905 return host;
1906
1907err_class:
1908 class_device_unregister(&host->class_dev);
1909
Roland Dreierf5358a12006-06-17 20:37:29 -07001910free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001911 kfree(host);
1912
1913 return NULL;
1914}
1915
1916static void srp_add_one(struct ib_device *device)
1917{
Roland Dreierf5358a12006-06-17 20:37:29 -07001918 struct srp_device *srp_dev;
1919 struct ib_device_attr *dev_attr;
1920 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001921 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001922 int s, e, p;
1923
Roland Dreierf5358a12006-06-17 20:37:29 -07001924 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1925 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001926 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001927
Roland Dreierf5358a12006-06-17 20:37:29 -07001928 if (ib_query_device(device, dev_attr)) {
1929 printk(KERN_WARNING PFX "Query device failed for %s\n",
1930 device->name);
1931 goto free_attr;
1932 }
1933
1934 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1935 if (!srp_dev)
1936 goto free_attr;
1937
1938 /*
1939 * Use the smallest page size supported by the HCA, down to a
1940 * minimum of 512 bytes (which is the smallest sector that a
1941 * SCSI command will ever carry).
1942 */
1943 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1944 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08001945 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07001946
1947 INIT_LIST_HEAD(&srp_dev->dev_list);
1948
1949 srp_dev->dev = device;
1950 srp_dev->pd = ib_alloc_pd(device);
1951 if (IS_ERR(srp_dev->pd))
1952 goto free_dev;
1953
1954 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1955 IB_ACCESS_LOCAL_WRITE |
1956 IB_ACCESS_REMOTE_READ |
1957 IB_ACCESS_REMOTE_WRITE);
1958 if (IS_ERR(srp_dev->mr))
1959 goto err_pd;
1960
1961 memset(&fmr_param, 0, sizeof fmr_param);
1962 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1963 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1964 fmr_param.cache = 1;
1965 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1966 fmr_param.page_shift = srp_dev->fmr_page_shift;
1967 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1968 IB_ACCESS_REMOTE_WRITE |
1969 IB_ACCESS_REMOTE_READ);
1970
1971 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1972 if (IS_ERR(srp_dev->fmr_pool))
1973 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001974
Tom Tucker07ebafb2006-08-03 16:02:42 -05001975 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001976 s = 0;
1977 e = 0;
1978 } else {
1979 s = 1;
1980 e = device->phys_port_cnt;
1981 }
1982
1983 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001984 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001985 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001986 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001987 }
1988
Roland Dreierf5358a12006-06-17 20:37:29 -07001989 ib_set_client_data(device, &srp_client, srp_dev);
1990
1991 goto free_attr;
1992
1993err_pd:
1994 ib_dealloc_pd(srp_dev->pd);
1995
1996free_dev:
1997 kfree(srp_dev);
1998
1999free_attr:
2000 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002001}
2002
2003static void srp_remove_one(struct ib_device *device)
2004{
Roland Dreierf5358a12006-06-17 20:37:29 -07002005 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002006 struct srp_host *host, *tmp_host;
2007 LIST_HEAD(target_list);
2008 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002009
Roland Dreierf5358a12006-06-17 20:37:29 -07002010 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002011
Roland Dreierf5358a12006-06-17 20:37:29 -07002012 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002013 class_device_unregister(&host->class_dev);
2014 /*
2015 * Wait for the sysfs entry to go away, so that no new
2016 * target ports can be created.
2017 */
2018 wait_for_completion(&host->released);
2019
2020 /*
2021 * Mark all target ports as removed, so we stop queueing
2022 * commands and don't try to reconnect.
2023 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002024 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002025 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002026 spin_lock_irq(target->scsi_host->host_lock);
2027 target->state = SRP_TARGET_REMOVED;
2028 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002029 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002030 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002031
2032 /*
2033 * Wait for any reconnection tasks that may have
2034 * started before we marked our target ports as
2035 * removed, and any target port removal tasks.
2036 */
2037 flush_scheduled_work();
2038
2039 list_for_each_entry_safe(target, tmp_target,
2040 &host->target_list, list) {
2041 scsi_remove_host(target->scsi_host);
2042 srp_disconnect_target(target);
2043 ib_destroy_cm_id(target->cm_id);
2044 srp_free_target_ib(target);
2045 scsi_host_put(target->scsi_host);
2046 }
2047
Roland Dreieraef9ec32005-11-02 14:07:13 -08002048 kfree(host);
2049 }
2050
Roland Dreierf5358a12006-06-17 20:37:29 -07002051 if (srp_dev->fmr_pool)
2052 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2053 ib_dereg_mr(srp_dev->mr);
2054 ib_dealloc_pd(srp_dev->pd);
2055
2056 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002057}
2058
FUJITA Tomonori32368222007-06-27 16:33:12 +09002059static struct srp_function_template ib_srp_transport_functions = {
2060};
2061
Roland Dreieraef9ec32005-11-02 14:07:13 -08002062static int __init srp_init_module(void)
2063{
2064 int ret;
2065
FUJITA Tomonori32368222007-06-27 16:33:12 +09002066 ib_srp_transport_template =
2067 srp_attach_transport(&ib_srp_transport_functions);
2068 if (!ib_srp_transport_template)
2069 return -ENOMEM;
2070
Vu Pham74b0a152006-06-17 20:37:32 -07002071 srp_template.sg_tablesize = srp_sg_tablesize;
2072 srp_max_iu_len = (sizeof (struct srp_cmd) +
2073 sizeof (struct srp_indirect_buf) +
2074 srp_sg_tablesize * 16);
2075
Roland Dreieraef9ec32005-11-02 14:07:13 -08002076 ret = class_register(&srp_class);
2077 if (ret) {
2078 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002079 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002080 return ret;
2081 }
2082
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002083 ib_sa_register_client(&srp_sa_client);
2084
Roland Dreieraef9ec32005-11-02 14:07:13 -08002085 ret = ib_register_client(&srp_client);
2086 if (ret) {
2087 printk(KERN_ERR PFX "couldn't register IB client\n");
FUJITA Tomonori32368222007-06-27 16:33:12 +09002088 srp_release_transport(ib_srp_transport_template);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002089 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002090 class_unregister(&srp_class);
2091 return ret;
2092 }
2093
2094 return 0;
2095}
2096
2097static void __exit srp_cleanup_module(void)
2098{
2099 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002100 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002101 class_unregister(&srp_class);
FUJITA Tomonori32368222007-06-27 16:33:12 +09002102 srp_release_transport(ib_srp_transport_template);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002103}
2104
2105module_init(srp_init_module);
2106module_exit(srp_cleanup_module);