blob: f01ca182f226a5df9c1d21adf195458ca95325e6 [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
78static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
79
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070080static int mellanox_workarounds = 1;
81
82module_param(mellanox_workarounds, int, 0444);
83MODULE_PARM_DESC(mellanox_workarounds,
84 "Enable workarounds for Mellanox SRP target bugs if != 0");
85
86static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
87
Roland Dreieraef9ec32005-11-02 14:07:13 -080088static void srp_add_one(struct ib_device *device);
89static void srp_remove_one(struct ib_device *device);
90static void srp_completion(struct ib_cq *cq, void *target_ptr);
91static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
92
93static struct ib_client srp_client = {
94 .name = "srp",
95 .add = srp_add_one,
96 .remove = srp_remove_one
97};
98
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070099static struct ib_sa_client srp_sa_client;
100
Roland Dreieraef9ec32005-11-02 14:07:13 -0800101static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
102{
103 return (struct srp_target_port *) host->hostdata;
104}
105
106static const char *srp_target_info(struct Scsi_Host *host)
107{
108 return host_to_target(host)->target_name;
109}
110
111static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
112 gfp_t gfp_mask,
113 enum dma_data_direction direction)
114{
115 struct srp_iu *iu;
116
117 iu = kmalloc(sizeof *iu, gfp_mask);
118 if (!iu)
119 goto out;
120
121 iu->buf = kzalloc(size, gfp_mask);
122 if (!iu->buf)
123 goto out_free_iu;
124
Ralph Campbell85507bc2006-12-12 14:30:55 -0800125 iu->dma = ib_dma_map_single(host->dev->dev, iu->buf, size, direction);
126 if (ib_dma_mapping_error(host->dev->dev, iu->dma))
Roland Dreieraef9ec32005-11-02 14:07:13 -0800127 goto out_free_buf;
128
129 iu->size = size;
130 iu->direction = direction;
131
132 return iu;
133
134out_free_buf:
135 kfree(iu->buf);
136out_free_iu:
137 kfree(iu);
138out:
139 return NULL;
140}
141
142static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
143{
144 if (!iu)
145 return;
146
Ralph Campbell85507bc2006-12-12 14:30:55 -0800147 ib_dma_unmap_single(host->dev->dev, iu->dma, iu->size, iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800148 kfree(iu->buf);
149 kfree(iu);
150}
151
152static void srp_qp_event(struct ib_event *event, void *context)
153{
154 printk(KERN_ERR PFX "QP event %d\n", event->event);
155}
156
157static int srp_init_qp(struct srp_target_port *target,
158 struct ib_qp *qp)
159{
160 struct ib_qp_attr *attr;
161 int ret;
162
163 attr = kmalloc(sizeof *attr, GFP_KERNEL);
164 if (!attr)
165 return -ENOMEM;
166
Roland Dreierf5358a12006-06-17 20:37:29 -0700167 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800168 target->srp_host->port,
169 be16_to_cpu(target->path.pkey),
170 &attr->pkey_index);
171 if (ret)
172 goto out;
173
174 attr->qp_state = IB_QPS_INIT;
175 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
176 IB_ACCESS_REMOTE_WRITE);
177 attr->port_num = target->srp_host->port;
178
179 ret = ib_modify_qp(qp, attr,
180 IB_QP_STATE |
181 IB_QP_PKEY_INDEX |
182 IB_QP_ACCESS_FLAGS |
183 IB_QP_PORT);
184
185out:
186 kfree(attr);
187 return ret;
188}
189
190static int srp_create_target_ib(struct srp_target_port *target)
191{
192 struct ib_qp_init_attr *init_attr;
193 int ret;
194
195 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
196 if (!init_attr)
197 return -ENOMEM;
198
Roland Dreierf5358a12006-06-17 20:37:29 -0700199 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +0300200 NULL, target, SRP_CQ_SIZE, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800201 if (IS_ERR(target->cq)) {
202 ret = PTR_ERR(target->cq);
203 goto out;
204 }
205
206 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
207
208 init_attr->event_handler = srp_qp_event;
209 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
210 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
211 init_attr->cap.max_recv_sge = 1;
212 init_attr->cap.max_send_sge = 1;
213 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
214 init_attr->qp_type = IB_QPT_RC;
215 init_attr->send_cq = target->cq;
216 init_attr->recv_cq = target->cq;
217
Roland Dreierf5358a12006-06-17 20:37:29 -0700218 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800219 if (IS_ERR(target->qp)) {
220 ret = PTR_ERR(target->qp);
221 ib_destroy_cq(target->cq);
222 goto out;
223 }
224
225 ret = srp_init_qp(target, target->qp);
226 if (ret) {
227 ib_destroy_qp(target->qp);
228 ib_destroy_cq(target->cq);
229 goto out;
230 }
231
232out:
233 kfree(init_attr);
234 return ret;
235}
236
237static void srp_free_target_ib(struct srp_target_port *target)
238{
239 int i;
240
241 ib_destroy_qp(target->qp);
242 ib_destroy_cq(target->cq);
243
244 for (i = 0; i < SRP_RQ_SIZE; ++i)
245 srp_free_iu(target->srp_host, target->rx_ring[i]);
246 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
247 srp_free_iu(target->srp_host, target->tx_ring[i]);
248}
249
250static void srp_path_rec_completion(int status,
251 struct ib_sa_path_rec *pathrec,
252 void *target_ptr)
253{
254 struct srp_target_port *target = target_ptr;
255
256 target->status = status;
257 if (status)
258 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
259 else
260 target->path = *pathrec;
261 complete(&target->done);
262}
263
264static int srp_lookup_path(struct srp_target_port *target)
265{
266 target->path.numb_path = 1;
267
268 init_completion(&target->done);
269
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700270 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
271 target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800272 target->srp_host->port,
273 &target->path,
274 IB_SA_PATH_REC_DGID |
275 IB_SA_PATH_REC_SGID |
276 IB_SA_PATH_REC_NUMB_PATH |
277 IB_SA_PATH_REC_PKEY,
278 SRP_PATH_REC_TIMEOUT_MS,
279 GFP_KERNEL,
280 srp_path_rec_completion,
281 target, &target->path_query);
282 if (target->path_query_id < 0)
283 return target->path_query_id;
284
285 wait_for_completion(&target->done);
286
287 if (target->status < 0)
288 printk(KERN_WARNING PFX "Path record query failed\n");
289
290 return target->status;
291}
292
293static int srp_send_req(struct srp_target_port *target)
294{
295 struct {
296 struct ib_cm_req_param param;
297 struct srp_login_req priv;
298 } *req = NULL;
299 int status;
300
301 req = kzalloc(sizeof *req, GFP_KERNEL);
302 if (!req)
303 return -ENOMEM;
304
305 req->param.primary_path = &target->path;
306 req->param.alternate_path = NULL;
307 req->param.service_id = target->service_id;
308 req->param.qp_num = target->qp->qp_num;
309 req->param.qp_type = target->qp->qp_type;
310 req->param.private_data = &req->priv;
311 req->param.private_data_len = sizeof req->priv;
312 req->param.flow_control = 1;
313
314 get_random_bytes(&req->param.starting_psn, 4);
315 req->param.starting_psn &= 0xffffff;
316
317 /*
318 * Pick some arbitrary defaults here; we could make these
319 * module parameters if anyone cared about setting them.
320 */
321 req->param.responder_resources = 4;
322 req->param.remote_cm_response_timeout = 20;
323 req->param.local_cm_response_timeout = 20;
324 req->param.retry_count = 7;
325 req->param.rnr_retry_count = 7;
326 req->param.max_cm_retries = 15;
327
328 req->priv.opcode = SRP_LOGIN_REQ;
329 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700330 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800331 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
332 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700333 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700334 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700335 * port identifier format is 8 bytes of ID extension followed
336 * by 8 bytes of GUID. Older drafts put the two halves in the
337 * opposite order, so that the GUID comes first.
338 *
339 * Targets conforming to these obsolete drafts can be
340 * recognized by the I/O Class they report.
341 */
342 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
343 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200344 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700345 memcpy(req->priv.initiator_port_id + 8,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200346 &target->initiator_ext, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700347 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
348 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
349 } else {
350 memcpy(req->priv.initiator_port_id,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200351 &target->initiator_ext, 8);
352 memcpy(req->priv.initiator_port_id + 8,
353 &target->path.sgid.global.interface_id, 8);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700354 memcpy(req->priv.target_port_id, &target->id_ext, 8);
355 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
356 }
357
Roland Dreieraef9ec32005-11-02 14:07:13 -0800358 /*
359 * Topspin/Cisco SRP targets will reject our login unless we
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200360 * zero out the first 8 bytes of our initiator port ID and set
361 * the second 8 bytes to the local node GUID.
Roland Dreieraef9ec32005-11-02 14:07:13 -0800362 */
363 if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
364 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
365 "activated for target GUID %016llx\n",
366 (unsigned long long) be64_to_cpu(target->ioc_guid));
367 memset(req->priv.initiator_port_id, 0, 8);
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +0200368 memcpy(req->priv.initiator_port_id + 8,
369 &target->srp_host->dev->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800370 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800371
372 status = ib_send_cm_req(target->cm_id, &req->param);
373
374 kfree(req);
375
376 return status;
377}
378
379static void srp_disconnect_target(struct srp_target_port *target)
380{
381 /* XXX should send SRP_I_LOGOUT request */
382
383 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700384 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
385 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
386 return;
387 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800388 wait_for_completion(&target->done);
389}
390
David Howellsc4028952006-11-22 14:57:56 +0000391static void srp_remove_work(struct work_struct *work)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800392{
David Howellsc4028952006-11-22 14:57:56 +0000393 struct srp_target_port *target =
394 container_of(work, struct srp_target_port, work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800395
396 spin_lock_irq(target->scsi_host->host_lock);
397 if (target->state != SRP_TARGET_DEAD) {
398 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800399 return;
400 }
401 target->state = SRP_TARGET_REMOVED;
402 spin_unlock_irq(target->scsi_host->host_lock);
403
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700404 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800405 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700406 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800407
408 scsi_remove_host(target->scsi_host);
409 ib_destroy_cm_id(target->cm_id);
410 srp_free_target_ib(target);
411 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800412}
413
414static int srp_connect_target(struct srp_target_port *target)
415{
416 int ret;
417
418 ret = srp_lookup_path(target);
419 if (ret)
420 return ret;
421
422 while (1) {
423 init_completion(&target->done);
424 ret = srp_send_req(target);
425 if (ret)
426 return ret;
427 wait_for_completion(&target->done);
428
429 /*
430 * The CM event handling code will set status to
431 * SRP_PORT_REDIRECT if we get a port redirect REJ
432 * back, or SRP_DLID_REDIRECT if we get a lid/qp
433 * redirect REJ back.
434 */
435 switch (target->status) {
436 case 0:
437 return 0;
438
439 case SRP_PORT_REDIRECT:
440 ret = srp_lookup_path(target);
441 if (ret)
442 return ret;
443 break;
444
445 case SRP_DLID_REDIRECT:
446 break;
447
448 default:
449 return target->status;
450 }
451 }
452}
453
Roland Dreierd945e1d2006-05-09 10:50:28 -0700454static void srp_unmap_data(struct scsi_cmnd *scmnd,
455 struct srp_target_port *target,
456 struct srp_request *req)
457{
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900458 if (!scsi_sglist(scmnd) ||
Roland Dreierd945e1d2006-05-09 10:50:28 -0700459 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
460 scmnd->sc_data_direction != DMA_FROM_DEVICE))
461 return;
462
Roland Dreierf5358a12006-06-17 20:37:29 -0700463 if (req->fmr) {
464 ib_fmr_pool_unmap(req->fmr);
465 req->fmr = NULL;
466 }
467
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900468 ib_dma_unmap_sg(target->srp_host->dev->dev, scsi_sglist(scmnd),
469 scsi_sg_count(scmnd), scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700470}
471
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700472static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
473{
474 srp_unmap_data(req->scmnd, target, req);
475 list_move_tail(&req->list, &target->free_reqs);
476}
477
478static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
479{
480 req->scmnd->result = DID_RESET << 16;
481 req->scmnd->scsi_done(req->scmnd);
482 srp_remove_req(target, req);
483}
484
Roland Dreieraef9ec32005-11-02 14:07:13 -0800485static int srp_reconnect_target(struct srp_target_port *target)
486{
487 struct ib_cm_id *new_cm_id;
488 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700489 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800490 struct ib_wc wc;
491 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800492
493 spin_lock_irq(target->scsi_host->host_lock);
494 if (target->state != SRP_TARGET_LIVE) {
495 spin_unlock_irq(target->scsi_host->host_lock);
496 return -EAGAIN;
497 }
498 target->state = SRP_TARGET_CONNECTING;
499 spin_unlock_irq(target->scsi_host->host_lock);
500
501 srp_disconnect_target(target);
502 /*
503 * Now get a new local CM ID so that we avoid confusing the
504 * target in case things are really fouled up.
505 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700506 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800507 srp_cm_handler, target);
508 if (IS_ERR(new_cm_id)) {
509 ret = PTR_ERR(new_cm_id);
510 goto err;
511 }
512 ib_destroy_cm_id(target->cm_id);
513 target->cm_id = new_cm_id;
514
515 qp_attr.qp_state = IB_QPS_RESET;
516 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
517 if (ret)
518 goto err;
519
520 ret = srp_init_qp(target, target->qp);
521 if (ret)
522 goto err;
523
524 while (ib_poll_cq(target->cq, 1, &wc) > 0)
525 ; /* nothing */
526
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300527 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700528 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
529 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300530 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800531
532 target->rx_head = 0;
533 target->tx_head = 0;
534 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800535
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200536 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800537 ret = srp_connect_target(target);
538 if (ret)
539 goto err;
540
541 spin_lock_irq(target->scsi_host->host_lock);
542 if (target->state == SRP_TARGET_CONNECTING) {
543 ret = 0;
544 target->state = SRP_TARGET_LIVE;
545 } else
546 ret = -EAGAIN;
547 spin_unlock_irq(target->scsi_host->host_lock);
548
549 return ret;
550
551err:
552 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
553
554 /*
555 * We couldn't reconnect, so kill our target port off.
556 * However, we have to defer the real removal because we might
557 * be in the context of the SCSI error handler now, which
558 * would deadlock if we call scsi_remove_host().
559 */
560 spin_lock_irq(target->scsi_host->host_lock);
561 if (target->state == SRP_TARGET_CONNECTING) {
562 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000563 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800564 schedule_work(&target->work);
565 }
566 spin_unlock_irq(target->scsi_host->host_lock);
567
568 return ret;
569}
570
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700571static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700572 int sg_cnt, struct srp_request *req,
573 struct srp_direct_buf *buf)
574{
575 u64 io_addr = 0;
576 u64 *dma_pages;
577 u32 len;
578 int page_cnt;
579 int i, j;
580 int ret;
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700581 struct srp_device *dev = target->srp_host->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800582 struct ib_device *ibdev = dev->dev;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900583 struct scatterlist *sg;
Roland Dreierf5358a12006-06-17 20:37:29 -0700584
585 if (!dev->fmr_pool)
586 return -ENODEV;
587
Ralph Campbell85507bc2006-12-12 14:30:55 -0800588 if ((ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask) &&
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700589 mellanox_workarounds && !memcmp(&target->ioc_guid, mellanox_oui, 3))
590 return -EINVAL;
591
Roland Dreierf5358a12006-06-17 20:37:29 -0700592 len = page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900593 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
594 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800595
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900596 if (ib_sg_dma_address(ibdev, sg) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700597 if (i > 0)
598 return -EINVAL;
599 else
600 ++page_cnt;
601 }
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900602 if ((ib_sg_dma_address(ibdev, sg) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700603 ~dev->fmr_page_mask) {
604 if (i < sg_cnt - 1)
605 return -EINVAL;
606 else
607 ++page_cnt;
608 }
609
Ralph Campbell85507bc2006-12-12 14:30:55 -0800610 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700611 }
612
613 page_cnt += len >> dev->fmr_page_shift;
614 if (page_cnt > SRP_FMR_SIZE)
615 return -ENOMEM;
616
617 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
618 if (!dma_pages)
619 return -ENOMEM;
620
621 page_cnt = 0;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900622 scsi_for_each_sg(req->scmnd, sg, sg_cnt, i) {
623 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800624
625 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700626 dma_pages[page_cnt++] =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900627 (ib_sg_dma_address(ibdev, sg) &
Ralph Campbell85507bc2006-12-12 14:30:55 -0800628 dev->fmr_page_mask) + j;
629 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700630
631 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700632 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700633 if (IS_ERR(req->fmr)) {
634 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700635 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700636 goto out;
637 }
638
Ralph Campbell85507bc2006-12-12 14:30:55 -0800639 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
640 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700641 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
642 buf->len = cpu_to_be32(len);
643
644 ret = 0;
645
646out:
647 kfree(dma_pages);
648
649 return ret;
650}
651
Roland Dreieraef9ec32005-11-02 14:07:13 -0800652static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
653 struct srp_request *req)
654{
Roland Dreiercf368712006-03-24 15:47:26 -0800655 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800656 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800657 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700658 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800659 struct srp_device *dev;
660 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800661
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900662 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
Roland Dreieraef9ec32005-11-02 14:07:13 -0800663 return sizeof (struct srp_cmd);
664
665 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
666 scmnd->sc_data_direction != DMA_TO_DEVICE) {
667 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
668 scmnd->sc_data_direction);
669 return -EINVAL;
670 }
671
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900672 nents = scsi_sg_count(scmnd);
673 scat = scsi_sglist(scmnd);
Roland Dreiercf368712006-03-24 15:47:26 -0800674
Ralph Campbell85507bc2006-12-12 14:30:55 -0800675 dev = target->srp_host->dev;
676 ibdev = dev->dev;
677
678 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700679
680 fmt = SRP_DATA_DESC_DIRECT;
681 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800682
683 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700684 /*
685 * The midlayer only generated a single gather/scatter
686 * entry, or DMA mapping coalesced everything to a
687 * single entry. So a direct descriptor along with
688 * the DMA MR suffices.
689 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800690 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800691
Ralph Campbell85507bc2006-12-12 14:30:55 -0800692 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
693 buf->key = cpu_to_be32(dev->mr->rkey);
694 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700695 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700696 (void *) cmd->add_data)) {
697 /*
698 * FMR mapping failed, and the scatterlist has more
699 * than one entry. Generate an indirect memory
700 * descriptor.
701 */
Roland Dreiercf368712006-03-24 15:47:26 -0800702 struct srp_indirect_buf *buf = (void *) cmd->add_data;
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900703 struct scatterlist *sg;
Roland Dreiercf368712006-03-24 15:47:26 -0800704 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700705 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800706
707 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700708 len = sizeof (struct srp_cmd) +
709 sizeof (struct srp_indirect_buf) +
710 count * sizeof (struct srp_direct_buf);
711
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900712 scsi_for_each_sg(scmnd, sg, count, i) {
713 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
Ralph Campbell85507bc2006-12-12 14:30:55 -0800714
Roland Dreierf5358a12006-06-17 20:37:29 -0700715 buf->desc_list[i].va =
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900716 cpu_to_be64(ib_sg_dma_address(ibdev, sg));
Roland Dreierf5358a12006-06-17 20:37:29 -0700717 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800718 cpu_to_be32(dev->mr->rkey);
719 buf->desc_list[i].len = cpu_to_be32(dma_len);
720 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700721 }
Roland Dreiercf368712006-03-24 15:47:26 -0800722
723 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
724 cmd->data_out_desc_cnt = count;
725 else
726 cmd->data_in_desc_cnt = count;
727
Roland Dreierf5358a12006-06-17 20:37:29 -0700728 buf->table_desc.va =
729 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800730 buf->table_desc.key =
Roland Dreierf5358a12006-06-17 20:37:29 -0700731 cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800732 buf->table_desc.len =
733 cpu_to_be32(count * sizeof (struct srp_direct_buf));
734
Roland Dreiercf368712006-03-24 15:47:26 -0800735 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800736 }
737
738 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
739 cmd->buf_fmt = fmt << 4;
740 else
741 cmd->buf_fmt = fmt;
742
Roland Dreieraef9ec32005-11-02 14:07:13 -0800743 return len;
744}
745
Roland Dreieraef9ec32005-11-02 14:07:13 -0800746static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
747{
748 struct srp_request *req;
749 struct scsi_cmnd *scmnd;
750 unsigned long flags;
751 s32 delta;
752
753 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
754
755 spin_lock_irqsave(target->scsi_host->host_lock, flags);
756
757 target->req_lim += delta;
758
759 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
760
761 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
762 if (be32_to_cpu(rsp->resp_data_len) < 4)
763 req->tsk_status = -1;
764 else
765 req->tsk_status = rsp->data[3];
766 complete(&req->done);
767 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700768 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800769 if (!scmnd)
770 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
771 (unsigned long long) rsp->tag);
772 scmnd->result = rsp->status;
773
774 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
775 memcpy(scmnd->sense_buffer, rsp->data +
776 be32_to_cpu(rsp->resp_data_len),
777 min_t(int, be32_to_cpu(rsp->sense_data_len),
778 SCSI_SENSE_BUFFERSIZE));
779 }
780
781 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900782 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800783 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
FUJITA Tomonoribb350d12007-05-26 02:28:25 +0900784 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
Roland Dreieraef9ec32005-11-02 14:07:13 -0800785
Roland Dreieraef9ec32005-11-02 14:07:13 -0800786 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800787 scmnd->host_scribble = (void *) -1L;
788 scmnd->scsi_done(scmnd);
789
Roland Dreierd945e1d2006-05-09 10:50:28 -0700790 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800791 } else
792 req->cmd_done = 1;
793 }
794
795 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
796}
797
Roland Dreieraef9ec32005-11-02 14:07:13 -0800798static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
799{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800800 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800801 struct srp_iu *iu;
802 u8 opcode;
803
804 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
805
Ralph Campbell85507bc2006-12-12 14:30:55 -0800806 dev = target->srp_host->dev->dev;
807 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
808 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800809
810 opcode = *(u8 *) iu->buf;
811
812 if (0) {
813 int i;
814
815 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
816
817 for (i = 0; i < wc->byte_len; ++i) {
818 if (i % 8 == 0)
819 printk(KERN_ERR " [%02x] ", i);
820 printk(" %02x", ((u8 *) iu->buf)[i]);
821 if ((i + 1) % 8 == 0)
822 printk("\n");
823 }
824
825 if (wc->byte_len % 8)
826 printk("\n");
827 }
828
829 switch (opcode) {
830 case SRP_RSP:
831 srp_process_rsp(target, iu->buf);
832 break;
833
834 case SRP_T_LOGOUT:
835 /* XXX Handle target logout */
836 printk(KERN_WARNING PFX "Got target logout request\n");
837 break;
838
839 default:
840 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
841 break;
842 }
843
Ralph Campbell85507bc2006-12-12 14:30:55 -0800844 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
845 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800846}
847
848static void srp_completion(struct ib_cq *cq, void *target_ptr)
849{
850 struct srp_target_port *target = target_ptr;
851 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800852
853 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
854 while (ib_poll_cq(cq, 1, &wc) > 0) {
855 if (wc.status) {
856 printk(KERN_ERR PFX "failed %s status %d\n",
857 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
858 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200859 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800860 break;
861 }
862
863 if (wc.wr_id & SRP_OP_RECV)
864 srp_handle_recv(target, &wc);
865 else
866 ++target->tx_tail;
867 }
868}
869
870static int __srp_post_recv(struct srp_target_port *target)
871{
872 struct srp_iu *iu;
873 struct ib_sge list;
874 struct ib_recv_wr wr, *bad_wr;
875 unsigned int next;
876 int ret;
877
878 next = target->rx_head & (SRP_RQ_SIZE - 1);
879 wr.wr_id = next | SRP_OP_RECV;
880 iu = target->rx_ring[next];
881
882 list.addr = iu->dma;
883 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700884 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800885
886 wr.next = NULL;
887 wr.sg_list = &list;
888 wr.num_sge = 1;
889
890 ret = ib_post_recv(target->qp, &wr, &bad_wr);
891 if (!ret)
892 ++target->rx_head;
893
894 return ret;
895}
896
897static int srp_post_recv(struct srp_target_port *target)
898{
899 unsigned long flags;
900 int ret;
901
902 spin_lock_irqsave(target->scsi_host->host_lock, flags);
903 ret = __srp_post_recv(target);
904 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
905
906 return ret;
907}
908
909/*
910 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800911 * req_lim and tx_head. Lock cannot be dropped between call here and
912 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800913 */
914static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
915{
916 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
917 return NULL;
918
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700919 if (unlikely(target->req_lim < 1))
920 ++target->zero_req_lim;
Roland Dreier47f2bce2005-11-15 00:19:21 -0800921
Roland Dreieraef9ec32005-11-02 14:07:13 -0800922 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
923}
924
925/*
926 * Must be called with target->scsi_host->host_lock held to protect
927 * req_lim and tx_head.
928 */
929static int __srp_post_send(struct srp_target_port *target,
930 struct srp_iu *iu, int len)
931{
932 struct ib_sge list;
933 struct ib_send_wr wr, *bad_wr;
934 int ret = 0;
935
Roland Dreieraef9ec32005-11-02 14:07:13 -0800936 list.addr = iu->dma;
937 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700938 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800939
940 wr.next = NULL;
941 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
942 wr.sg_list = &list;
943 wr.num_sge = 1;
944 wr.opcode = IB_WR_SEND;
945 wr.send_flags = IB_SEND_SIGNALED;
946
947 ret = ib_post_send(target->qp, &wr, &bad_wr);
948
949 if (!ret) {
950 ++target->tx_head;
951 --target->req_lim;
952 }
953
954 return ret;
955}
956
957static int srp_queuecommand(struct scsi_cmnd *scmnd,
958 void (*done)(struct scsi_cmnd *))
959{
960 struct srp_target_port *target = host_to_target(scmnd->device->host);
961 struct srp_request *req;
962 struct srp_iu *iu;
963 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800964 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800965 int len;
966
967 if (target->state == SRP_TARGET_CONNECTING)
968 goto err;
969
970 if (target->state == SRP_TARGET_DEAD ||
971 target->state == SRP_TARGET_REMOVED) {
972 scmnd->result = DID_BAD_TARGET << 16;
973 done(scmnd);
974 return 0;
975 }
976
977 iu = __srp_get_tx_iu(target);
978 if (!iu)
979 goto err;
980
Ralph Campbell85507bc2006-12-12 14:30:55 -0800981 dev = target->srp_host->dev->dev;
982 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
983 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800984
Roland Dreierd945e1d2006-05-09 10:50:28 -0700985 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800986
987 scmnd->scsi_done = done;
988 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -0700989 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800990
991 cmd = iu->buf;
992 memset(cmd, 0, sizeof *cmd);
993
994 cmd->opcode = SRP_CMD;
995 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700996 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800997 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
998
Roland Dreieraef9ec32005-11-02 14:07:13 -0800999 req->scmnd = scmnd;
1000 req->cmd = iu;
1001 req->cmd_done = 0;
1002 req->tsk_mgmt = NULL;
1003
1004 len = srp_map_data(scmnd, target, req);
1005 if (len < 0) {
1006 printk(KERN_ERR PFX "Failed to map data\n");
1007 goto err;
1008 }
1009
1010 if (__srp_post_recv(target)) {
1011 printk(KERN_ERR PFX "Recv failed\n");
1012 goto err_unmap;
1013 }
1014
Ralph Campbell85507bc2006-12-12 14:30:55 -08001015 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1016 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001017
1018 if (__srp_post_send(target, iu, len)) {
1019 printk(KERN_ERR PFX "Send failed\n");
1020 goto err_unmap;
1021 }
1022
Roland Dreierd945e1d2006-05-09 10:50:28 -07001023 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001024
1025 return 0;
1026
1027err_unmap:
1028 srp_unmap_data(scmnd, target, req);
1029
1030err:
1031 return SCSI_MLQUEUE_HOST_BUSY;
1032}
1033
1034static int srp_alloc_iu_bufs(struct srp_target_port *target)
1035{
1036 int i;
1037
1038 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1039 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1040 target->max_ti_iu_len,
1041 GFP_KERNEL, DMA_FROM_DEVICE);
1042 if (!target->rx_ring[i])
1043 goto err;
1044 }
1045
1046 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1047 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001048 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001049 GFP_KERNEL, DMA_TO_DEVICE);
1050 if (!target->tx_ring[i])
1051 goto err;
1052 }
1053
1054 return 0;
1055
1056err:
1057 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1058 srp_free_iu(target->srp_host, target->rx_ring[i]);
1059 target->rx_ring[i] = NULL;
1060 }
1061
1062 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1063 srp_free_iu(target->srp_host, target->tx_ring[i]);
1064 target->tx_ring[i] = NULL;
1065 }
1066
1067 return -ENOMEM;
1068}
1069
1070static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1071 struct ib_cm_event *event,
1072 struct srp_target_port *target)
1073{
1074 struct ib_class_port_info *cpi;
1075 int opcode;
1076
1077 switch (event->param.rej_rcvd.reason) {
1078 case IB_CM_REJ_PORT_CM_REDIRECT:
1079 cpi = event->param.rej_rcvd.ari;
1080 target->path.dlid = cpi->redirect_lid;
1081 target->path.pkey = cpi->redirect_pkey;
1082 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1083 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1084
1085 target->status = target->path.dlid ?
1086 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1087 break;
1088
1089 case IB_CM_REJ_PORT_REDIRECT:
1090 if (topspin_workarounds &&
1091 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1092 /*
1093 * Topspin/Cisco SRP gateways incorrectly send
1094 * reject reason code 25 when they mean 24
1095 * (port redirect).
1096 */
1097 memcpy(target->path.dgid.raw,
1098 event->param.rej_rcvd.ari, 16);
1099
1100 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1101 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1102 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1103
1104 target->status = SRP_PORT_REDIRECT;
1105 } else {
1106 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1107 target->status = -ECONNRESET;
1108 }
1109 break;
1110
1111 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1112 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1113 target->status = -ECONNRESET;
1114 break;
1115
1116 case IB_CM_REJ_CONSUMER_DEFINED:
1117 opcode = *(u8 *) event->private_data;
1118 if (opcode == SRP_LOGIN_REJ) {
1119 struct srp_login_rej *rej = event->private_data;
1120 u32 reason = be32_to_cpu(rej->reason);
1121
1122 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1123 printk(KERN_WARNING PFX
1124 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1125 else
1126 printk(KERN_WARNING PFX
1127 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1128 } else
1129 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1130 " opcode 0x%02x\n", opcode);
1131 target->status = -ECONNRESET;
1132 break;
1133
1134 default:
1135 printk(KERN_WARNING " REJ reason 0x%x\n",
1136 event->param.rej_rcvd.reason);
1137 target->status = -ECONNRESET;
1138 }
1139}
1140
1141static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1142{
1143 struct srp_target_port *target = cm_id->context;
1144 struct ib_qp_attr *qp_attr = NULL;
1145 int attr_mask = 0;
1146 int comp = 0;
1147 int opcode = 0;
1148
1149 switch (event->event) {
1150 case IB_CM_REQ_ERROR:
1151 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1152 comp = 1;
1153 target->status = -ECONNRESET;
1154 break;
1155
1156 case IB_CM_REP_RECEIVED:
1157 comp = 1;
1158 opcode = *(u8 *) event->private_data;
1159
1160 if (opcode == SRP_LOGIN_RSP) {
1161 struct srp_login_rsp *rsp = event->private_data;
1162
1163 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1164 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1165
1166 target->scsi_host->can_queue = min(target->req_lim,
1167 target->scsi_host->can_queue);
1168 } else {
1169 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1170 target->status = -ECONNRESET;
1171 break;
1172 }
1173
Vu Phamd2fcea72006-11-21 14:14:10 -08001174 if (!target->rx_ring[0]) {
1175 target->status = srp_alloc_iu_bufs(target);
1176 if (target->status)
1177 break;
1178 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001179
1180 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1181 if (!qp_attr) {
1182 target->status = -ENOMEM;
1183 break;
1184 }
1185
1186 qp_attr->qp_state = IB_QPS_RTR;
1187 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1188 if (target->status)
1189 break;
1190
1191 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1192 if (target->status)
1193 break;
1194
1195 target->status = srp_post_recv(target);
1196 if (target->status)
1197 break;
1198
1199 qp_attr->qp_state = IB_QPS_RTS;
1200 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1201 if (target->status)
1202 break;
1203
1204 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1205 if (target->status)
1206 break;
1207
1208 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1209 if (target->status)
1210 break;
1211
1212 break;
1213
1214 case IB_CM_REJ_RECEIVED:
1215 printk(KERN_DEBUG PFX "REJ received\n");
1216 comp = 1;
1217
1218 srp_cm_rej_handler(cm_id, event, target);
1219 break;
1220
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001221 case IB_CM_DREQ_RECEIVED:
1222 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1223 if (ib_send_cm_drep(cm_id, NULL, 0))
1224 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001225 break;
1226
1227 case IB_CM_TIMEWAIT_EXIT:
1228 printk(KERN_ERR PFX "connection closed\n");
1229
1230 comp = 1;
1231 target->status = 0;
1232 break;
1233
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001234 case IB_CM_MRA_RECEIVED:
1235 case IB_CM_DREQ_ERROR:
1236 case IB_CM_DREP_RECEIVED:
1237 break;
1238
Roland Dreieraef9ec32005-11-02 14:07:13 -08001239 default:
1240 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1241 break;
1242 }
1243
1244 if (comp)
1245 complete(&target->done);
1246
1247 kfree(qp_attr);
1248
1249 return 0;
1250}
1251
Roland Dreierd945e1d2006-05-09 10:50:28 -07001252static int srp_send_tsk_mgmt(struct srp_target_port *target,
1253 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001254{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001255 struct srp_iu *iu;
1256 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001257
1258 spin_lock_irq(target->scsi_host->host_lock);
1259
Roland Dreier1285b3a2006-03-03 15:47:25 -08001260 if (target->state == SRP_TARGET_DEAD ||
1261 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001262 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001263 goto out;
1264 }
1265
Roland Dreieraef9ec32005-11-02 14:07:13 -08001266 init_completion(&req->done);
1267
1268 iu = __srp_get_tx_iu(target);
1269 if (!iu)
1270 goto out;
1271
1272 tsk_mgmt = iu->buf;
1273 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1274
1275 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001276 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1277 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001278 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001279 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001280
1281 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1282 goto out;
1283
1284 req->tsk_mgmt = iu;
1285
1286 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001287
Roland Dreieraef9ec32005-11-02 14:07:13 -08001288 if (!wait_for_completion_timeout(&req->done,
1289 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001290 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001291
Roland Dreierd945e1d2006-05-09 10:50:28 -07001292 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001293
1294out:
1295 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001296 return -1;
1297}
1298
1299static int srp_find_req(struct srp_target_port *target,
1300 struct scsi_cmnd *scmnd,
1301 struct srp_request **req)
1302{
1303 if (scmnd->host_scribble == (void *) -1L)
1304 return -1;
1305
1306 *req = &target->req_ring[(long) scmnd->host_scribble];
1307
1308 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001309}
1310
1311static int srp_abort(struct scsi_cmnd *scmnd)
1312{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001313 struct srp_target_port *target = host_to_target(scmnd->device->host);
1314 struct srp_request *req;
1315 int ret = SUCCESS;
1316
Roland Dreieraef9ec32005-11-02 14:07:13 -08001317 printk(KERN_ERR "SRP abort called\n");
1318
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001319 if (target->qp_in_error)
1320 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001321 if (srp_find_req(target, scmnd, &req))
1322 return FAILED;
1323 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1324 return FAILED;
1325
1326 spin_lock_irq(target->scsi_host->host_lock);
1327
1328 if (req->cmd_done) {
1329 srp_remove_req(target, req);
1330 scmnd->scsi_done(scmnd);
1331 } else if (!req->tsk_status) {
1332 srp_remove_req(target, req);
1333 scmnd->result = DID_ABORT << 16;
1334 } else
1335 ret = FAILED;
1336
1337 spin_unlock_irq(target->scsi_host->host_lock);
1338
1339 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001340}
1341
1342static int srp_reset_device(struct scsi_cmnd *scmnd)
1343{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001344 struct srp_target_port *target = host_to_target(scmnd->device->host);
1345 struct srp_request *req, *tmp;
1346
Roland Dreieraef9ec32005-11-02 14:07:13 -08001347 printk(KERN_ERR "SRP reset_device called\n");
1348
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001349 if (target->qp_in_error)
1350 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001351 if (srp_find_req(target, scmnd, &req))
1352 return FAILED;
1353 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1354 return FAILED;
1355 if (req->tsk_status)
1356 return FAILED;
1357
1358 spin_lock_irq(target->scsi_host->host_lock);
1359
1360 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001361 if (req->scmnd->device == scmnd->device)
1362 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001363
1364 spin_unlock_irq(target->scsi_host->host_lock);
1365
1366 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001367}
1368
1369static int srp_reset_host(struct scsi_cmnd *scmnd)
1370{
1371 struct srp_target_port *target = host_to_target(scmnd->device->host);
1372 int ret = FAILED;
1373
1374 printk(KERN_ERR PFX "SRP reset_host called\n");
1375
1376 if (!srp_reconnect_target(target))
1377 ret = SUCCESS;
1378
1379 return ret;
1380}
1381
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001382static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1383{
1384 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1385
1386 if (target->state == SRP_TARGET_DEAD ||
1387 target->state == SRP_TARGET_REMOVED)
1388 return -ENODEV;
1389
1390 return sprintf(buf, "0x%016llx\n",
1391 (unsigned long long) be64_to_cpu(target->id_ext));
1392}
1393
1394static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1395{
1396 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1397
1398 if (target->state == SRP_TARGET_DEAD ||
1399 target->state == SRP_TARGET_REMOVED)
1400 return -ENODEV;
1401
1402 return sprintf(buf, "0x%016llx\n",
1403 (unsigned long long) be64_to_cpu(target->ioc_guid));
1404}
1405
1406static ssize_t show_service_id(struct class_device *cdev, char *buf)
1407{
1408 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1409
1410 if (target->state == SRP_TARGET_DEAD ||
1411 target->state == SRP_TARGET_REMOVED)
1412 return -ENODEV;
1413
1414 return sprintf(buf, "0x%016llx\n",
1415 (unsigned long long) be64_to_cpu(target->service_id));
1416}
1417
1418static ssize_t show_pkey(struct class_device *cdev, char *buf)
1419{
1420 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1421
1422 if (target->state == SRP_TARGET_DEAD ||
1423 target->state == SRP_TARGET_REMOVED)
1424 return -ENODEV;
1425
1426 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1427}
1428
1429static ssize_t show_dgid(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, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1438 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1439 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1440 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1441 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1442 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1443 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1444 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1445 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1446}
1447
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001448static ssize_t show_orig_dgid(struct class_device *cdev, char *buf)
1449{
1450 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1451
1452 if (target->state == SRP_TARGET_DEAD ||
1453 target->state == SRP_TARGET_REMOVED)
1454 return -ENODEV;
1455
1456 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1457 be16_to_cpu(target->orig_dgid[0]),
1458 be16_to_cpu(target->orig_dgid[1]),
1459 be16_to_cpu(target->orig_dgid[2]),
1460 be16_to_cpu(target->orig_dgid[3]),
1461 be16_to_cpu(target->orig_dgid[4]),
1462 be16_to_cpu(target->orig_dgid[5]),
1463 be16_to_cpu(target->orig_dgid[6]),
1464 be16_to_cpu(target->orig_dgid[7]));
1465}
1466
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001467static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1468{
1469 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1470
1471 if (target->state == SRP_TARGET_DEAD ||
1472 target->state == SRP_TARGET_REMOVED)
1473 return -ENODEV;
1474
1475 return sprintf(buf, "%d\n", target->zero_req_lim);
1476}
1477
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001478static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1479{
1480 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1481
1482 return sprintf(buf, "%d\n", target->srp_host->port);
1483}
1484
1485static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1486{
1487 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1488
1489 return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1490}
1491
1492static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1493static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1494static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1495static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1496static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001497static CLASS_DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001498static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1499static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1500static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001501
1502static struct class_device_attribute *srp_host_attrs[] = {
1503 &class_device_attr_id_ext,
1504 &class_device_attr_ioc_guid,
1505 &class_device_attr_service_id,
1506 &class_device_attr_pkey,
1507 &class_device_attr_dgid,
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001508 &class_device_attr_orig_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001509 &class_device_attr_zero_req_lim,
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001510 &class_device_attr_local_ib_port,
1511 &class_device_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001512 NULL
1513};
1514
Roland Dreieraef9ec32005-11-02 14:07:13 -08001515static struct scsi_host_template srp_template = {
1516 .module = THIS_MODULE,
Roland Dreierb7f008f2007-05-06 21:18:11 -07001517 .name = "InfiniBand SRP initiator",
1518 .proc_name = DRV_NAME,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001519 .info = srp_target_info,
1520 .queuecommand = srp_queuecommand,
1521 .eh_abort_handler = srp_abort,
1522 .eh_device_reset_handler = srp_reset_device,
1523 .eh_host_reset_handler = srp_reset_host,
1524 .can_queue = SRP_SQ_SIZE,
1525 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001526 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001527 .use_clustering = ENABLE_CLUSTERING,
1528 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001529};
1530
1531static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1532{
1533 sprintf(target->target_name, "SRP.T10:%016llX",
1534 (unsigned long long) be64_to_cpu(target->id_ext));
1535
Roland Dreierf5358a12006-06-17 20:37:29 -07001536 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001537 return -ENODEV;
1538
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001539 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001540 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001541 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001542
1543 target->state = SRP_TARGET_LIVE;
1544
Roland Dreieraef9ec32005-11-02 14:07:13 -08001545 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001546 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001547
1548 return 0;
1549}
1550
1551static void srp_release_class_dev(struct class_device *class_dev)
1552{
1553 struct srp_host *host =
1554 container_of(class_dev, struct srp_host, class_dev);
1555
1556 complete(&host->released);
1557}
1558
1559static struct class srp_class = {
1560 .name = "infiniband_srp",
1561 .release = srp_release_class_dev
1562};
1563
1564/*
1565 * Target ports are added by writing
1566 *
1567 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1568 * pkey=<P_Key>,service_id=<service ID>
1569 *
1570 * to the add_target sysfs attribute.
1571 */
1572enum {
1573 SRP_OPT_ERR = 0,
1574 SRP_OPT_ID_EXT = 1 << 0,
1575 SRP_OPT_IOC_GUID = 1 << 1,
1576 SRP_OPT_DGID = 1 << 2,
1577 SRP_OPT_PKEY = 1 << 3,
1578 SRP_OPT_SERVICE_ID = 1 << 4,
1579 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001580 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001581 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001582 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001583 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1584 SRP_OPT_IOC_GUID |
1585 SRP_OPT_DGID |
1586 SRP_OPT_PKEY |
1587 SRP_OPT_SERVICE_ID),
1588};
1589
1590static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001591 { SRP_OPT_ID_EXT, "id_ext=%s" },
1592 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1593 { SRP_OPT_DGID, "dgid=%s" },
1594 { SRP_OPT_PKEY, "pkey=%x" },
1595 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1596 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1597 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001598 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001599 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001600 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001601};
1602
1603static int srp_parse_options(const char *buf, struct srp_target_port *target)
1604{
1605 char *options, *sep_opt;
1606 char *p;
1607 char dgid[3];
1608 substring_t args[MAX_OPT_ARGS];
1609 int opt_mask = 0;
1610 int token;
1611 int ret = -EINVAL;
1612 int i;
1613
1614 options = kstrdup(buf, GFP_KERNEL);
1615 if (!options)
1616 return -ENOMEM;
1617
1618 sep_opt = options;
1619 while ((p = strsep(&sep_opt, ",")) != NULL) {
1620 if (!*p)
1621 continue;
1622
1623 token = match_token(p, srp_opt_tokens, args);
1624 opt_mask |= token;
1625
1626 switch (token) {
1627 case SRP_OPT_ID_EXT:
1628 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001629 if (!p) {
1630 ret = -ENOMEM;
1631 goto out;
1632 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001633 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1634 kfree(p);
1635 break;
1636
1637 case SRP_OPT_IOC_GUID:
1638 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001639 if (!p) {
1640 ret = -ENOMEM;
1641 goto out;
1642 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001643 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1644 kfree(p);
1645 break;
1646
1647 case SRP_OPT_DGID:
1648 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001649 if (!p) {
1650 ret = -ENOMEM;
1651 goto out;
1652 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001653 if (strlen(p) != 32) {
1654 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001655 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001656 goto out;
1657 }
1658
1659 for (i = 0; i < 16; ++i) {
1660 strlcpy(dgid, p + i * 2, 3);
1661 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1662 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001663 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001664 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001665 break;
1666
1667 case SRP_OPT_PKEY:
1668 if (match_hex(args, &token)) {
1669 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1670 goto out;
1671 }
1672 target->path.pkey = cpu_to_be16(token);
1673 break;
1674
1675 case SRP_OPT_SERVICE_ID:
1676 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001677 if (!p) {
1678 ret = -ENOMEM;
1679 goto out;
1680 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001681 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1682 kfree(p);
1683 break;
1684
1685 case SRP_OPT_MAX_SECT:
1686 if (match_int(args, &token)) {
1687 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1688 goto out;
1689 }
1690 target->scsi_host->max_sectors = token;
1691 break;
1692
Vu Pham52fb2b502006-06-17 20:37:31 -07001693 case SRP_OPT_MAX_CMD_PER_LUN:
1694 if (match_int(args, &token)) {
1695 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1696 goto out;
1697 }
1698 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1699 break;
1700
Ramachandra K0c0450db2006-06-17 20:37:38 -07001701 case SRP_OPT_IO_CLASS:
1702 if (match_hex(args, &token)) {
1703 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1704 goto out;
1705 }
1706 if (token != SRP_REV10_IB_IO_CLASS &&
1707 token != SRP_REV16A_IB_IO_CLASS) {
1708 printk(KERN_WARNING PFX "unknown IO class parameter value"
1709 " %x specified (use %x or %x).\n",
1710 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1711 goto out;
1712 }
1713 target->io_class = token;
1714 break;
1715
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001716 case SRP_OPT_INITIATOR_EXT:
1717 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001718 if (!p) {
1719 ret = -ENOMEM;
1720 goto out;
1721 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001722 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1723 kfree(p);
1724 break;
1725
Roland Dreieraef9ec32005-11-02 14:07:13 -08001726 default:
1727 printk(KERN_WARNING PFX "unknown parameter or missing value "
1728 "'%s' in target creation request\n", p);
1729 goto out;
1730 }
1731 }
1732
1733 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1734 ret = 0;
1735 else
1736 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1737 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1738 !(srp_opt_tokens[i].token & opt_mask))
1739 printk(KERN_WARNING PFX "target creation request is "
1740 "missing parameter '%s'\n",
1741 srp_opt_tokens[i].pattern);
1742
1743out:
1744 kfree(options);
1745 return ret;
1746}
1747
1748static ssize_t srp_create_target(struct class_device *class_dev,
1749 const char *buf, size_t count)
1750{
1751 struct srp_host *host =
1752 container_of(class_dev, struct srp_host, class_dev);
1753 struct Scsi_Host *target_host;
1754 struct srp_target_port *target;
1755 int ret;
1756 int i;
1757
1758 target_host = scsi_host_alloc(&srp_template,
1759 sizeof (struct srp_target_port));
1760 if (!target_host)
1761 return -ENOMEM;
1762
Arne Redlich3c8edf02006-11-15 12:43:00 +01001763 target_host->max_lun = SRP_MAX_LUN;
1764 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001765
Roland Dreieraef9ec32005-11-02 14:07:13 -08001766 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001767
Ramachandra K0c0450db2006-06-17 20:37:38 -07001768 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001769 target->scsi_host = target_host;
1770 target->srp_host = host;
1771
Roland Dreierd945e1d2006-05-09 10:50:28 -07001772 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001773 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001774 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1775 target->req_ring[i].index = i;
1776 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1777 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001778
1779 ret = srp_parse_options(buf, target);
1780 if (ret)
1781 goto err;
1782
Roland Dreierf5358a12006-06-17 20:37:29 -07001783 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001784
1785 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1786 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1787 (unsigned long long) be64_to_cpu(target->id_ext),
1788 (unsigned long long) be64_to_cpu(target->ioc_guid),
1789 be16_to_cpu(target->path.pkey),
1790 (unsigned long long) be64_to_cpu(target->service_id),
1791 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1792 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1793 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1794 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1795 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1796 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1797 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1798 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1799
1800 ret = srp_create_target_ib(target);
1801 if (ret)
1802 goto err;
1803
Roland Dreierf5358a12006-06-17 20:37:29 -07001804 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001805 if (IS_ERR(target->cm_id)) {
1806 ret = PTR_ERR(target->cm_id);
1807 goto err_free;
1808 }
1809
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001810 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001811 ret = srp_connect_target(target);
1812 if (ret) {
1813 printk(KERN_ERR PFX "Connection failed\n");
1814 goto err_cm_id;
1815 }
1816
1817 ret = srp_add_target(host, target);
1818 if (ret)
1819 goto err_disconnect;
1820
1821 return count;
1822
1823err_disconnect:
1824 srp_disconnect_target(target);
1825
1826err_cm_id:
1827 ib_destroy_cm_id(target->cm_id);
1828
1829err_free:
1830 srp_free_target_ib(target);
1831
1832err:
1833 scsi_host_put(target_host);
1834
1835 return ret;
1836}
1837
1838static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1839
1840static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1841{
1842 struct srp_host *host =
1843 container_of(class_dev, struct srp_host, class_dev);
1844
Roland Dreierf5358a12006-06-17 20:37:29 -07001845 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001846}
1847
1848static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1849
1850static ssize_t show_port(struct class_device *class_dev, char *buf)
1851{
1852 struct srp_host *host =
1853 container_of(class_dev, struct srp_host, class_dev);
1854
1855 return sprintf(buf, "%d\n", host->port);
1856}
1857
1858static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1859
Roland Dreierf5358a12006-06-17 20:37:29 -07001860static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001861{
1862 struct srp_host *host;
1863
1864 host = kzalloc(sizeof *host, GFP_KERNEL);
1865 if (!host)
1866 return NULL;
1867
1868 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001869 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001870 init_completion(&host->released);
1871 host->dev = device;
1872 host->port = port;
1873
Roland Dreieraef9ec32005-11-02 14:07:13 -08001874 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001875 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001876 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001877 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001878
1879 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001880 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001881 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1882 goto err_class;
1883 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1884 goto err_class;
1885 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1886 goto err_class;
1887
1888 return host;
1889
1890err_class:
1891 class_device_unregister(&host->class_dev);
1892
Roland Dreierf5358a12006-06-17 20:37:29 -07001893free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001894 kfree(host);
1895
1896 return NULL;
1897}
1898
1899static void srp_add_one(struct ib_device *device)
1900{
Roland Dreierf5358a12006-06-17 20:37:29 -07001901 struct srp_device *srp_dev;
1902 struct ib_device_attr *dev_attr;
1903 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001904 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001905 int s, e, p;
1906
Roland Dreierf5358a12006-06-17 20:37:29 -07001907 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1908 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001909 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001910
Roland Dreierf5358a12006-06-17 20:37:29 -07001911 if (ib_query_device(device, dev_attr)) {
1912 printk(KERN_WARNING PFX "Query device failed for %s\n",
1913 device->name);
1914 goto free_attr;
1915 }
1916
1917 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1918 if (!srp_dev)
1919 goto free_attr;
1920
1921 /*
1922 * Use the smallest page size supported by the HCA, down to a
1923 * minimum of 512 bytes (which is the smallest sector that a
1924 * SCSI command will ever carry).
1925 */
1926 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1927 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08001928 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07001929
1930 INIT_LIST_HEAD(&srp_dev->dev_list);
1931
1932 srp_dev->dev = device;
1933 srp_dev->pd = ib_alloc_pd(device);
1934 if (IS_ERR(srp_dev->pd))
1935 goto free_dev;
1936
1937 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1938 IB_ACCESS_LOCAL_WRITE |
1939 IB_ACCESS_REMOTE_READ |
1940 IB_ACCESS_REMOTE_WRITE);
1941 if (IS_ERR(srp_dev->mr))
1942 goto err_pd;
1943
1944 memset(&fmr_param, 0, sizeof fmr_param);
1945 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1946 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1947 fmr_param.cache = 1;
1948 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1949 fmr_param.page_shift = srp_dev->fmr_page_shift;
1950 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1951 IB_ACCESS_REMOTE_WRITE |
1952 IB_ACCESS_REMOTE_READ);
1953
1954 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1955 if (IS_ERR(srp_dev->fmr_pool))
1956 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001957
Tom Tucker07ebafb2006-08-03 16:02:42 -05001958 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001959 s = 0;
1960 e = 0;
1961 } else {
1962 s = 1;
1963 e = device->phys_port_cnt;
1964 }
1965
1966 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001967 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001968 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001969 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001970 }
1971
Roland Dreierf5358a12006-06-17 20:37:29 -07001972 ib_set_client_data(device, &srp_client, srp_dev);
1973
1974 goto free_attr;
1975
1976err_pd:
1977 ib_dealloc_pd(srp_dev->pd);
1978
1979free_dev:
1980 kfree(srp_dev);
1981
1982free_attr:
1983 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001984}
1985
1986static void srp_remove_one(struct ib_device *device)
1987{
Roland Dreierf5358a12006-06-17 20:37:29 -07001988 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001989 struct srp_host *host, *tmp_host;
1990 LIST_HEAD(target_list);
1991 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001992
Roland Dreierf5358a12006-06-17 20:37:29 -07001993 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001994
Roland Dreierf5358a12006-06-17 20:37:29 -07001995 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001996 class_device_unregister(&host->class_dev);
1997 /*
1998 * Wait for the sysfs entry to go away, so that no new
1999 * target ports can be created.
2000 */
2001 wait_for_completion(&host->released);
2002
2003 /*
2004 * Mark all target ports as removed, so we stop queueing
2005 * commands and don't try to reconnect.
2006 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002007 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002008 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002009 spin_lock_irq(target->scsi_host->host_lock);
2010 target->state = SRP_TARGET_REMOVED;
2011 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002012 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002013 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002014
2015 /*
2016 * Wait for any reconnection tasks that may have
2017 * started before we marked our target ports as
2018 * removed, and any target port removal tasks.
2019 */
2020 flush_scheduled_work();
2021
2022 list_for_each_entry_safe(target, tmp_target,
2023 &host->target_list, list) {
2024 scsi_remove_host(target->scsi_host);
2025 srp_disconnect_target(target);
2026 ib_destroy_cm_id(target->cm_id);
2027 srp_free_target_ib(target);
2028 scsi_host_put(target->scsi_host);
2029 }
2030
Roland Dreieraef9ec32005-11-02 14:07:13 -08002031 kfree(host);
2032 }
2033
Roland Dreierf5358a12006-06-17 20:37:29 -07002034 if (srp_dev->fmr_pool)
2035 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2036 ib_dereg_mr(srp_dev->mr);
2037 ib_dealloc_pd(srp_dev->pd);
2038
2039 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002040}
2041
2042static int __init srp_init_module(void)
2043{
2044 int ret;
2045
Vu Pham74b0a152006-06-17 20:37:32 -07002046 srp_template.sg_tablesize = srp_sg_tablesize;
2047 srp_max_iu_len = (sizeof (struct srp_cmd) +
2048 sizeof (struct srp_indirect_buf) +
2049 srp_sg_tablesize * 16);
2050
Roland Dreieraef9ec32005-11-02 14:07:13 -08002051 ret = class_register(&srp_class);
2052 if (ret) {
2053 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2054 return ret;
2055 }
2056
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002057 ib_sa_register_client(&srp_sa_client);
2058
Roland Dreieraef9ec32005-11-02 14:07:13 -08002059 ret = ib_register_client(&srp_client);
2060 if (ret) {
2061 printk(KERN_ERR PFX "couldn't register IB client\n");
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002062 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002063 class_unregister(&srp_class);
2064 return ret;
2065 }
2066
2067 return 0;
2068}
2069
2070static void __exit srp_cleanup_module(void)
2071{
2072 ib_unregister_client(&srp_client);
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}
2076
2077module_init(srp_init_module);
2078module_exit(srp_cleanup_module);