blob: df526cb9e796acc2b95d8efcf98934f4c53bb0a9 [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,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800200 NULL, target, SRP_CQ_SIZE);
201 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{
458 struct scatterlist *scat;
459 int nents;
460
461 if (!scmnd->request_buffer ||
462 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
463 scmnd->sc_data_direction != DMA_FROM_DEVICE))
464 return;
465
Roland Dreierf5358a12006-06-17 20:37:29 -0700466 if (req->fmr) {
467 ib_fmr_pool_unmap(req->fmr);
468 req->fmr = NULL;
469 }
470
Roland Dreierd945e1d2006-05-09 10:50:28 -0700471 /*
472 * This handling of non-SG commands can be killed when the
473 * SCSI midlayer no longer generates non-SG commands.
474 */
475 if (likely(scmnd->use_sg)) {
476 nents = scmnd->use_sg;
477 scat = scmnd->request_buffer;
478 } else {
479 nents = 1;
480 scat = &req->fake_sg;
481 }
482
Ralph Campbell85507bc2006-12-12 14:30:55 -0800483 ib_dma_unmap_sg(target->srp_host->dev->dev, scat, nents,
484 scmnd->sc_data_direction);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700485}
486
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700487static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
488{
489 srp_unmap_data(req->scmnd, target, req);
490 list_move_tail(&req->list, &target->free_reqs);
491}
492
493static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
494{
495 req->scmnd->result = DID_RESET << 16;
496 req->scmnd->scsi_done(req->scmnd);
497 srp_remove_req(target, req);
498}
499
Roland Dreieraef9ec32005-11-02 14:07:13 -0800500static int srp_reconnect_target(struct srp_target_port *target)
501{
502 struct ib_cm_id *new_cm_id;
503 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700504 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800505 struct ib_wc wc;
506 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800507
508 spin_lock_irq(target->scsi_host->host_lock);
509 if (target->state != SRP_TARGET_LIVE) {
510 spin_unlock_irq(target->scsi_host->host_lock);
511 return -EAGAIN;
512 }
513 target->state = SRP_TARGET_CONNECTING;
514 spin_unlock_irq(target->scsi_host->host_lock);
515
516 srp_disconnect_target(target);
517 /*
518 * Now get a new local CM ID so that we avoid confusing the
519 * target in case things are really fouled up.
520 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700521 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800522 srp_cm_handler, target);
523 if (IS_ERR(new_cm_id)) {
524 ret = PTR_ERR(new_cm_id);
525 goto err;
526 }
527 ib_destroy_cm_id(target->cm_id);
528 target->cm_id = new_cm_id;
529
530 qp_attr.qp_state = IB_QPS_RESET;
531 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
532 if (ret)
533 goto err;
534
535 ret = srp_init_qp(target, target->qp);
536 if (ret)
537 goto err;
538
539 while (ib_poll_cq(target->cq, 1, &wc) > 0)
540 ; /* nothing */
541
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300542 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700543 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
544 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300545 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800546
547 target->rx_head = 0;
548 target->tx_head = 0;
549 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800550
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200551 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800552 ret = srp_connect_target(target);
553 if (ret)
554 goto err;
555
556 spin_lock_irq(target->scsi_host->host_lock);
557 if (target->state == SRP_TARGET_CONNECTING) {
558 ret = 0;
559 target->state = SRP_TARGET_LIVE;
560 } else
561 ret = -EAGAIN;
562 spin_unlock_irq(target->scsi_host->host_lock);
563
564 return ret;
565
566err:
567 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
568
569 /*
570 * We couldn't reconnect, so kill our target port off.
571 * However, we have to defer the real removal because we might
572 * be in the context of the SCSI error handler now, which
573 * would deadlock if we call scsi_remove_host().
574 */
575 spin_lock_irq(target->scsi_host->host_lock);
576 if (target->state == SRP_TARGET_CONNECTING) {
577 target->state = SRP_TARGET_DEAD;
David Howellsc4028952006-11-22 14:57:56 +0000578 INIT_WORK(&target->work, srp_remove_work);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800579 schedule_work(&target->work);
580 }
581 spin_unlock_irq(target->scsi_host->host_lock);
582
583 return ret;
584}
585
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700586static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700587 int sg_cnt, struct srp_request *req,
588 struct srp_direct_buf *buf)
589{
590 u64 io_addr = 0;
591 u64 *dma_pages;
592 u32 len;
593 int page_cnt;
594 int i, j;
595 int ret;
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700596 struct srp_device *dev = target->srp_host->dev;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800597 struct ib_device *ibdev = dev->dev;
Roland Dreierf5358a12006-06-17 20:37:29 -0700598
599 if (!dev->fmr_pool)
600 return -ENODEV;
601
Ralph Campbell85507bc2006-12-12 14:30:55 -0800602 if ((ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask) &&
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700603 mellanox_workarounds && !memcmp(&target->ioc_guid, mellanox_oui, 3))
604 return -EINVAL;
605
Roland Dreierf5358a12006-06-17 20:37:29 -0700606 len = page_cnt = 0;
607 for (i = 0; i < sg_cnt; ++i) {
Ralph Campbell85507bc2006-12-12 14:30:55 -0800608 unsigned int dma_len = ib_sg_dma_len(ibdev, &scat[i]);
609
610 if (ib_sg_dma_address(ibdev, &scat[i]) & ~dev->fmr_page_mask) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700611 if (i > 0)
612 return -EINVAL;
613 else
614 ++page_cnt;
615 }
Ralph Campbell85507bc2006-12-12 14:30:55 -0800616 if ((ib_sg_dma_address(ibdev, &scat[i]) + dma_len) &
Roland Dreierf5358a12006-06-17 20:37:29 -0700617 ~dev->fmr_page_mask) {
618 if (i < sg_cnt - 1)
619 return -EINVAL;
620 else
621 ++page_cnt;
622 }
623
Ralph Campbell85507bc2006-12-12 14:30:55 -0800624 len += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700625 }
626
627 page_cnt += len >> dev->fmr_page_shift;
628 if (page_cnt > SRP_FMR_SIZE)
629 return -ENOMEM;
630
631 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
632 if (!dma_pages)
633 return -ENOMEM;
634
635 page_cnt = 0;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800636 for (i = 0; i < sg_cnt; ++i) {
637 unsigned int dma_len = ib_sg_dma_len(ibdev, &scat[i]);
638
639 for (j = 0; j < dma_len; j += dev->fmr_page_size)
Roland Dreierf5358a12006-06-17 20:37:29 -0700640 dma_pages[page_cnt++] =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800641 (ib_sg_dma_address(ibdev, &scat[i]) &
642 dev->fmr_page_mask) + j;
643 }
Roland Dreierf5358a12006-06-17 20:37:29 -0700644
645 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700646 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700647 if (IS_ERR(req->fmr)) {
648 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700649 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700650 goto out;
651 }
652
Ralph Campbell85507bc2006-12-12 14:30:55 -0800653 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
654 ~dev->fmr_page_mask);
Roland Dreierf5358a12006-06-17 20:37:29 -0700655 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
656 buf->len = cpu_to_be32(len);
657
658 ret = 0;
659
660out:
661 kfree(dma_pages);
662
663 return ret;
664}
665
Roland Dreieraef9ec32005-11-02 14:07:13 -0800666static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
667 struct srp_request *req)
668{
Roland Dreiercf368712006-03-24 15:47:26 -0800669 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800670 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800671 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700672 u8 fmt = SRP_DATA_DESC_DIRECT;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800673 struct srp_device *dev;
674 struct ib_device *ibdev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800675
676 if (!scmnd->request_buffer || scmnd->sc_data_direction == DMA_NONE)
677 return sizeof (struct srp_cmd);
678
679 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
680 scmnd->sc_data_direction != DMA_TO_DEVICE) {
681 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
682 scmnd->sc_data_direction);
683 return -EINVAL;
684 }
685
Roland Dreiercf368712006-03-24 15:47:26 -0800686 /*
687 * This handling of non-SG commands can be killed when the
688 * SCSI midlayer no longer generates non-SG commands.
689 */
690 if (likely(scmnd->use_sg)) {
691 nents = scmnd->use_sg;
692 scat = scmnd->request_buffer;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800693 } else {
Roland Dreiercf368712006-03-24 15:47:26 -0800694 nents = 1;
695 scat = &req->fake_sg;
696 sg_init_one(scat, scmnd->request_buffer, scmnd->request_bufflen);
697 }
698
Ralph Campbell85507bc2006-12-12 14:30:55 -0800699 dev = target->srp_host->dev;
700 ibdev = dev->dev;
701
702 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
Roland Dreierf5358a12006-06-17 20:37:29 -0700703
704 fmt = SRP_DATA_DESC_DIRECT;
705 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800706
707 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700708 /*
709 * The midlayer only generated a single gather/scatter
710 * entry, or DMA mapping coalesced everything to a
711 * single entry. So a direct descriptor along with
712 * the DMA MR suffices.
713 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800714 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800715
Ralph Campbell85507bc2006-12-12 14:30:55 -0800716 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
717 buf->key = cpu_to_be32(dev->mr->rkey);
718 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700719 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700720 (void *) cmd->add_data)) {
721 /*
722 * FMR mapping failed, and the scatterlist has more
723 * than one entry. Generate an indirect memory
724 * descriptor.
725 */
Roland Dreiercf368712006-03-24 15:47:26 -0800726 struct srp_indirect_buf *buf = (void *) cmd->add_data;
727 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700728 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800729
730 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700731 len = sizeof (struct srp_cmd) +
732 sizeof (struct srp_indirect_buf) +
733 count * sizeof (struct srp_direct_buf);
734
735 for (i = 0; i < count; ++i) {
Ralph Campbell85507bc2006-12-12 14:30:55 -0800736 unsigned int dma_len = ib_sg_dma_len(ibdev, &scat[i]);
737
Roland Dreierf5358a12006-06-17 20:37:29 -0700738 buf->desc_list[i].va =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800739 cpu_to_be64(ib_sg_dma_address(ibdev, &scat[i]));
Roland Dreierf5358a12006-06-17 20:37:29 -0700740 buf->desc_list[i].key =
Ralph Campbell85507bc2006-12-12 14:30:55 -0800741 cpu_to_be32(dev->mr->rkey);
742 buf->desc_list[i].len = cpu_to_be32(dma_len);
743 datalen += dma_len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700744 }
Roland Dreiercf368712006-03-24 15:47:26 -0800745
746 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
747 cmd->data_out_desc_cnt = count;
748 else
749 cmd->data_in_desc_cnt = count;
750
Roland Dreierf5358a12006-06-17 20:37:29 -0700751 buf->table_desc.va =
752 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800753 buf->table_desc.key =
Roland Dreierf5358a12006-06-17 20:37:29 -0700754 cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800755 buf->table_desc.len =
756 cpu_to_be32(count * sizeof (struct srp_direct_buf));
757
Roland Dreiercf368712006-03-24 15:47:26 -0800758 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800759 }
760
761 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
762 cmd->buf_fmt = fmt << 4;
763 else
764 cmd->buf_fmt = fmt;
765
Roland Dreieraef9ec32005-11-02 14:07:13 -0800766 return len;
767}
768
Roland Dreieraef9ec32005-11-02 14:07:13 -0800769static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
770{
771 struct srp_request *req;
772 struct scsi_cmnd *scmnd;
773 unsigned long flags;
774 s32 delta;
775
776 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
777
778 spin_lock_irqsave(target->scsi_host->host_lock, flags);
779
780 target->req_lim += delta;
781
782 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
783
784 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
785 if (be32_to_cpu(rsp->resp_data_len) < 4)
786 req->tsk_status = -1;
787 else
788 req->tsk_status = rsp->data[3];
789 complete(&req->done);
790 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700791 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800792 if (!scmnd)
793 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
794 (unsigned long long) rsp->tag);
795 scmnd->result = rsp->status;
796
797 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
798 memcpy(scmnd->sense_buffer, rsp->data +
799 be32_to_cpu(rsp->resp_data_len),
800 min_t(int, be32_to_cpu(rsp->sense_data_len),
801 SCSI_SENSE_BUFFERSIZE));
802 }
803
804 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
805 scmnd->resid = be32_to_cpu(rsp->data_out_res_cnt);
806 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
807 scmnd->resid = be32_to_cpu(rsp->data_in_res_cnt);
808
Roland Dreieraef9ec32005-11-02 14:07:13 -0800809 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800810 scmnd->host_scribble = (void *) -1L;
811 scmnd->scsi_done(scmnd);
812
Roland Dreierd945e1d2006-05-09 10:50:28 -0700813 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800814 } else
815 req->cmd_done = 1;
816 }
817
818 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
819}
820
Roland Dreieraef9ec32005-11-02 14:07:13 -0800821static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
822{
Ralph Campbell85507bc2006-12-12 14:30:55 -0800823 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800824 struct srp_iu *iu;
825 u8 opcode;
826
827 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
828
Ralph Campbell85507bc2006-12-12 14:30:55 -0800829 dev = target->srp_host->dev->dev;
830 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
831 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800832
833 opcode = *(u8 *) iu->buf;
834
835 if (0) {
836 int i;
837
838 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
839
840 for (i = 0; i < wc->byte_len; ++i) {
841 if (i % 8 == 0)
842 printk(KERN_ERR " [%02x] ", i);
843 printk(" %02x", ((u8 *) iu->buf)[i]);
844 if ((i + 1) % 8 == 0)
845 printk("\n");
846 }
847
848 if (wc->byte_len % 8)
849 printk("\n");
850 }
851
852 switch (opcode) {
853 case SRP_RSP:
854 srp_process_rsp(target, iu->buf);
855 break;
856
857 case SRP_T_LOGOUT:
858 /* XXX Handle target logout */
859 printk(KERN_WARNING PFX "Got target logout request\n");
860 break;
861
862 default:
863 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
864 break;
865 }
866
Ralph Campbell85507bc2006-12-12 14:30:55 -0800867 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
868 DMA_FROM_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800869}
870
871static void srp_completion(struct ib_cq *cq, void *target_ptr)
872{
873 struct srp_target_port *target = target_ptr;
874 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800875
876 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
877 while (ib_poll_cq(cq, 1, &wc) > 0) {
878 if (wc.status) {
879 printk(KERN_ERR PFX "failed %s status %d\n",
880 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
881 wc.status);
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +0200882 target->qp_in_error = 1;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800883 break;
884 }
885
886 if (wc.wr_id & SRP_OP_RECV)
887 srp_handle_recv(target, &wc);
888 else
889 ++target->tx_tail;
890 }
891}
892
893static int __srp_post_recv(struct srp_target_port *target)
894{
895 struct srp_iu *iu;
896 struct ib_sge list;
897 struct ib_recv_wr wr, *bad_wr;
898 unsigned int next;
899 int ret;
900
901 next = target->rx_head & (SRP_RQ_SIZE - 1);
902 wr.wr_id = next | SRP_OP_RECV;
903 iu = target->rx_ring[next];
904
905 list.addr = iu->dma;
906 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700907 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800908
909 wr.next = NULL;
910 wr.sg_list = &list;
911 wr.num_sge = 1;
912
913 ret = ib_post_recv(target->qp, &wr, &bad_wr);
914 if (!ret)
915 ++target->rx_head;
916
917 return ret;
918}
919
920static int srp_post_recv(struct srp_target_port *target)
921{
922 unsigned long flags;
923 int ret;
924
925 spin_lock_irqsave(target->scsi_host->host_lock, flags);
926 ret = __srp_post_recv(target);
927 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
928
929 return ret;
930}
931
932/*
933 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800934 * req_lim and tx_head. Lock cannot be dropped between call here and
935 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800936 */
937static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
938{
939 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
940 return NULL;
941
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700942 if (unlikely(target->req_lim < 1))
943 ++target->zero_req_lim;
Roland Dreier47f2bce2005-11-15 00:19:21 -0800944
Roland Dreieraef9ec32005-11-02 14:07:13 -0800945 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
946}
947
948/*
949 * Must be called with target->scsi_host->host_lock held to protect
950 * req_lim and tx_head.
951 */
952static int __srp_post_send(struct srp_target_port *target,
953 struct srp_iu *iu, int len)
954{
955 struct ib_sge list;
956 struct ib_send_wr wr, *bad_wr;
957 int ret = 0;
958
Roland Dreieraef9ec32005-11-02 14:07:13 -0800959 list.addr = iu->dma;
960 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700961 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800962
963 wr.next = NULL;
964 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
965 wr.sg_list = &list;
966 wr.num_sge = 1;
967 wr.opcode = IB_WR_SEND;
968 wr.send_flags = IB_SEND_SIGNALED;
969
970 ret = ib_post_send(target->qp, &wr, &bad_wr);
971
972 if (!ret) {
973 ++target->tx_head;
974 --target->req_lim;
975 }
976
977 return ret;
978}
979
980static int srp_queuecommand(struct scsi_cmnd *scmnd,
981 void (*done)(struct scsi_cmnd *))
982{
983 struct srp_target_port *target = host_to_target(scmnd->device->host);
984 struct srp_request *req;
985 struct srp_iu *iu;
986 struct srp_cmd *cmd;
Ralph Campbell85507bc2006-12-12 14:30:55 -0800987 struct ib_device *dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800988 int len;
989
990 if (target->state == SRP_TARGET_CONNECTING)
991 goto err;
992
993 if (target->state == SRP_TARGET_DEAD ||
994 target->state == SRP_TARGET_REMOVED) {
995 scmnd->result = DID_BAD_TARGET << 16;
996 done(scmnd);
997 return 0;
998 }
999
1000 iu = __srp_get_tx_iu(target);
1001 if (!iu)
1002 goto err;
1003
Ralph Campbell85507bc2006-12-12 14:30:55 -08001004 dev = target->srp_host->dev->dev;
1005 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1006 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001007
Roland Dreierd945e1d2006-05-09 10:50:28 -07001008 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001009
1010 scmnd->scsi_done = done;
1011 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001012 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001013
1014 cmd = iu->buf;
1015 memset(cmd, 0, sizeof *cmd);
1016
1017 cmd->opcode = SRP_CMD;
1018 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001019 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001020 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1021
Roland Dreieraef9ec32005-11-02 14:07:13 -08001022 req->scmnd = scmnd;
1023 req->cmd = iu;
1024 req->cmd_done = 0;
1025 req->tsk_mgmt = NULL;
1026
1027 len = srp_map_data(scmnd, target, req);
1028 if (len < 0) {
1029 printk(KERN_ERR PFX "Failed to map data\n");
1030 goto err;
1031 }
1032
1033 if (__srp_post_recv(target)) {
1034 printk(KERN_ERR PFX "Recv failed\n");
1035 goto err_unmap;
1036 }
1037
Ralph Campbell85507bc2006-12-12 14:30:55 -08001038 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1039 DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001040
1041 if (__srp_post_send(target, iu, len)) {
1042 printk(KERN_ERR PFX "Send failed\n");
1043 goto err_unmap;
1044 }
1045
Roland Dreierd945e1d2006-05-09 10:50:28 -07001046 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001047
1048 return 0;
1049
1050err_unmap:
1051 srp_unmap_data(scmnd, target, req);
1052
1053err:
1054 return SCSI_MLQUEUE_HOST_BUSY;
1055}
1056
1057static int srp_alloc_iu_bufs(struct srp_target_port *target)
1058{
1059 int i;
1060
1061 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1062 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1063 target->max_ti_iu_len,
1064 GFP_KERNEL, DMA_FROM_DEVICE);
1065 if (!target->rx_ring[i])
1066 goto err;
1067 }
1068
1069 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1070 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001071 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001072 GFP_KERNEL, DMA_TO_DEVICE);
1073 if (!target->tx_ring[i])
1074 goto err;
1075 }
1076
1077 return 0;
1078
1079err:
1080 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1081 srp_free_iu(target->srp_host, target->rx_ring[i]);
1082 target->rx_ring[i] = NULL;
1083 }
1084
1085 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1086 srp_free_iu(target->srp_host, target->tx_ring[i]);
1087 target->tx_ring[i] = NULL;
1088 }
1089
1090 return -ENOMEM;
1091}
1092
1093static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1094 struct ib_cm_event *event,
1095 struct srp_target_port *target)
1096{
1097 struct ib_class_port_info *cpi;
1098 int opcode;
1099
1100 switch (event->param.rej_rcvd.reason) {
1101 case IB_CM_REJ_PORT_CM_REDIRECT:
1102 cpi = event->param.rej_rcvd.ari;
1103 target->path.dlid = cpi->redirect_lid;
1104 target->path.pkey = cpi->redirect_pkey;
1105 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1106 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1107
1108 target->status = target->path.dlid ?
1109 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1110 break;
1111
1112 case IB_CM_REJ_PORT_REDIRECT:
1113 if (topspin_workarounds &&
1114 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1115 /*
1116 * Topspin/Cisco SRP gateways incorrectly send
1117 * reject reason code 25 when they mean 24
1118 * (port redirect).
1119 */
1120 memcpy(target->path.dgid.raw,
1121 event->param.rej_rcvd.ari, 16);
1122
1123 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1124 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1125 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1126
1127 target->status = SRP_PORT_REDIRECT;
1128 } else {
1129 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1130 target->status = -ECONNRESET;
1131 }
1132 break;
1133
1134 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1135 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1136 target->status = -ECONNRESET;
1137 break;
1138
1139 case IB_CM_REJ_CONSUMER_DEFINED:
1140 opcode = *(u8 *) event->private_data;
1141 if (opcode == SRP_LOGIN_REJ) {
1142 struct srp_login_rej *rej = event->private_data;
1143 u32 reason = be32_to_cpu(rej->reason);
1144
1145 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1146 printk(KERN_WARNING PFX
1147 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1148 else
1149 printk(KERN_WARNING PFX
1150 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1151 } else
1152 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1153 " opcode 0x%02x\n", opcode);
1154 target->status = -ECONNRESET;
1155 break;
1156
1157 default:
1158 printk(KERN_WARNING " REJ reason 0x%x\n",
1159 event->param.rej_rcvd.reason);
1160 target->status = -ECONNRESET;
1161 }
1162}
1163
1164static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1165{
1166 struct srp_target_port *target = cm_id->context;
1167 struct ib_qp_attr *qp_attr = NULL;
1168 int attr_mask = 0;
1169 int comp = 0;
1170 int opcode = 0;
1171
1172 switch (event->event) {
1173 case IB_CM_REQ_ERROR:
1174 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1175 comp = 1;
1176 target->status = -ECONNRESET;
1177 break;
1178
1179 case IB_CM_REP_RECEIVED:
1180 comp = 1;
1181 opcode = *(u8 *) event->private_data;
1182
1183 if (opcode == SRP_LOGIN_RSP) {
1184 struct srp_login_rsp *rsp = event->private_data;
1185
1186 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1187 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1188
1189 target->scsi_host->can_queue = min(target->req_lim,
1190 target->scsi_host->can_queue);
1191 } else {
1192 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1193 target->status = -ECONNRESET;
1194 break;
1195 }
1196
Vu Phamd2fcea72006-11-21 14:14:10 -08001197 if (!target->rx_ring[0]) {
1198 target->status = srp_alloc_iu_bufs(target);
1199 if (target->status)
1200 break;
1201 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001202
1203 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1204 if (!qp_attr) {
1205 target->status = -ENOMEM;
1206 break;
1207 }
1208
1209 qp_attr->qp_state = IB_QPS_RTR;
1210 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1211 if (target->status)
1212 break;
1213
1214 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1215 if (target->status)
1216 break;
1217
1218 target->status = srp_post_recv(target);
1219 if (target->status)
1220 break;
1221
1222 qp_attr->qp_state = IB_QPS_RTS;
1223 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1224 if (target->status)
1225 break;
1226
1227 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1228 if (target->status)
1229 break;
1230
1231 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1232 if (target->status)
1233 break;
1234
1235 break;
1236
1237 case IB_CM_REJ_RECEIVED:
1238 printk(KERN_DEBUG PFX "REJ received\n");
1239 comp = 1;
1240
1241 srp_cm_rej_handler(cm_id, event, target);
1242 break;
1243
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001244 case IB_CM_DREQ_RECEIVED:
1245 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1246 if (ib_send_cm_drep(cm_id, NULL, 0))
1247 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001248 break;
1249
1250 case IB_CM_TIMEWAIT_EXIT:
1251 printk(KERN_ERR PFX "connection closed\n");
1252
1253 comp = 1;
1254 target->status = 0;
1255 break;
1256
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001257 case IB_CM_MRA_RECEIVED:
1258 case IB_CM_DREQ_ERROR:
1259 case IB_CM_DREP_RECEIVED:
1260 break;
1261
Roland Dreieraef9ec32005-11-02 14:07:13 -08001262 default:
1263 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1264 break;
1265 }
1266
1267 if (comp)
1268 complete(&target->done);
1269
1270 kfree(qp_attr);
1271
1272 return 0;
1273}
1274
Roland Dreierd945e1d2006-05-09 10:50:28 -07001275static int srp_send_tsk_mgmt(struct srp_target_port *target,
1276 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001277{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001278 struct srp_iu *iu;
1279 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001280
1281 spin_lock_irq(target->scsi_host->host_lock);
1282
Roland Dreier1285b3a2006-03-03 15:47:25 -08001283 if (target->state == SRP_TARGET_DEAD ||
1284 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001285 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001286 goto out;
1287 }
1288
Roland Dreieraef9ec32005-11-02 14:07:13 -08001289 init_completion(&req->done);
1290
1291 iu = __srp_get_tx_iu(target);
1292 if (!iu)
1293 goto out;
1294
1295 tsk_mgmt = iu->buf;
1296 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1297
1298 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001299 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1300 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001301 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001302 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001303
1304 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1305 goto out;
1306
1307 req->tsk_mgmt = iu;
1308
1309 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001310
Roland Dreieraef9ec32005-11-02 14:07:13 -08001311 if (!wait_for_completion_timeout(&req->done,
1312 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001313 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001314
Roland Dreierd945e1d2006-05-09 10:50:28 -07001315 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001316
1317out:
1318 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001319 return -1;
1320}
1321
1322static int srp_find_req(struct srp_target_port *target,
1323 struct scsi_cmnd *scmnd,
1324 struct srp_request **req)
1325{
1326 if (scmnd->host_scribble == (void *) -1L)
1327 return -1;
1328
1329 *req = &target->req_ring[(long) scmnd->host_scribble];
1330
1331 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001332}
1333
1334static int srp_abort(struct scsi_cmnd *scmnd)
1335{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001336 struct srp_target_port *target = host_to_target(scmnd->device->host);
1337 struct srp_request *req;
1338 int ret = SUCCESS;
1339
Roland Dreieraef9ec32005-11-02 14:07:13 -08001340 printk(KERN_ERR "SRP abort called\n");
1341
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001342 if (target->qp_in_error)
1343 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001344 if (srp_find_req(target, scmnd, &req))
1345 return FAILED;
1346 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1347 return FAILED;
1348
1349 spin_lock_irq(target->scsi_host->host_lock);
1350
1351 if (req->cmd_done) {
1352 srp_remove_req(target, req);
1353 scmnd->scsi_done(scmnd);
1354 } else if (!req->tsk_status) {
1355 srp_remove_req(target, req);
1356 scmnd->result = DID_ABORT << 16;
1357 } else
1358 ret = FAILED;
1359
1360 spin_unlock_irq(target->scsi_host->host_lock);
1361
1362 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001363}
1364
1365static int srp_reset_device(struct scsi_cmnd *scmnd)
1366{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001367 struct srp_target_port *target = host_to_target(scmnd->device->host);
1368 struct srp_request *req, *tmp;
1369
Roland Dreieraef9ec32005-11-02 14:07:13 -08001370 printk(KERN_ERR "SRP reset_device called\n");
1371
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001372 if (target->qp_in_error)
1373 return FAILED;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001374 if (srp_find_req(target, scmnd, &req))
1375 return FAILED;
1376 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1377 return FAILED;
1378 if (req->tsk_status)
1379 return FAILED;
1380
1381 spin_lock_irq(target->scsi_host->host_lock);
1382
1383 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001384 if (req->scmnd->device == scmnd->device)
1385 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001386
1387 spin_unlock_irq(target->scsi_host->host_lock);
1388
1389 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001390}
1391
1392static int srp_reset_host(struct scsi_cmnd *scmnd)
1393{
1394 struct srp_target_port *target = host_to_target(scmnd->device->host);
1395 int ret = FAILED;
1396
1397 printk(KERN_ERR PFX "SRP reset_host called\n");
1398
1399 if (!srp_reconnect_target(target))
1400 ret = SUCCESS;
1401
1402 return ret;
1403}
1404
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001405static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1406{
1407 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1408
1409 if (target->state == SRP_TARGET_DEAD ||
1410 target->state == SRP_TARGET_REMOVED)
1411 return -ENODEV;
1412
1413 return sprintf(buf, "0x%016llx\n",
1414 (unsigned long long) be64_to_cpu(target->id_ext));
1415}
1416
1417static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1418{
1419 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1420
1421 if (target->state == SRP_TARGET_DEAD ||
1422 target->state == SRP_TARGET_REMOVED)
1423 return -ENODEV;
1424
1425 return sprintf(buf, "0x%016llx\n",
1426 (unsigned long long) be64_to_cpu(target->ioc_guid));
1427}
1428
1429static ssize_t show_service_id(struct class_device *cdev, char *buf)
1430{
1431 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1432
1433 if (target->state == SRP_TARGET_DEAD ||
1434 target->state == SRP_TARGET_REMOVED)
1435 return -ENODEV;
1436
1437 return sprintf(buf, "0x%016llx\n",
1438 (unsigned long long) be64_to_cpu(target->service_id));
1439}
1440
1441static ssize_t show_pkey(struct class_device *cdev, char *buf)
1442{
1443 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1444
1445 if (target->state == SRP_TARGET_DEAD ||
1446 target->state == SRP_TARGET_REMOVED)
1447 return -ENODEV;
1448
1449 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1450}
1451
1452static ssize_t show_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(((__be16 *) target->path.dgid.raw)[0]),
1462 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1463 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1464 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1465 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1466 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1467 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1468 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1469}
1470
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001471static ssize_t show_orig_dgid(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, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1480 be16_to_cpu(target->orig_dgid[0]),
1481 be16_to_cpu(target->orig_dgid[1]),
1482 be16_to_cpu(target->orig_dgid[2]),
1483 be16_to_cpu(target->orig_dgid[3]),
1484 be16_to_cpu(target->orig_dgid[4]),
1485 be16_to_cpu(target->orig_dgid[5]),
1486 be16_to_cpu(target->orig_dgid[6]),
1487 be16_to_cpu(target->orig_dgid[7]));
1488}
1489
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001490static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1491{
1492 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1493
1494 if (target->state == SRP_TARGET_DEAD ||
1495 target->state == SRP_TARGET_REMOVED)
1496 return -ENODEV;
1497
1498 return sprintf(buf, "%d\n", target->zero_req_lim);
1499}
1500
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001501static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1502{
1503 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1504
1505 return sprintf(buf, "%d\n", target->srp_host->port);
1506}
1507
1508static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1509{
1510 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1511
1512 return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1513}
1514
1515static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1516static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1517static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1518static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1519static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001520static CLASS_DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001521static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1522static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1523static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001524
1525static struct class_device_attribute *srp_host_attrs[] = {
1526 &class_device_attr_id_ext,
1527 &class_device_attr_ioc_guid,
1528 &class_device_attr_service_id,
1529 &class_device_attr_pkey,
1530 &class_device_attr_dgid,
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001531 &class_device_attr_orig_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001532 &class_device_attr_zero_req_lim,
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001533 &class_device_attr_local_ib_port,
1534 &class_device_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001535 NULL
1536};
1537
Roland Dreieraef9ec32005-11-02 14:07:13 -08001538static struct scsi_host_template srp_template = {
1539 .module = THIS_MODULE,
1540 .name = DRV_NAME,
1541 .info = srp_target_info,
1542 .queuecommand = srp_queuecommand,
1543 .eh_abort_handler = srp_abort,
1544 .eh_device_reset_handler = srp_reset_device,
1545 .eh_host_reset_handler = srp_reset_host,
1546 .can_queue = SRP_SQ_SIZE,
1547 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001548 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001549 .use_clustering = ENABLE_CLUSTERING,
1550 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001551};
1552
1553static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1554{
1555 sprintf(target->target_name, "SRP.T10:%016llX",
1556 (unsigned long long) be64_to_cpu(target->id_ext));
1557
Roland Dreierf5358a12006-06-17 20:37:29 -07001558 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001559 return -ENODEV;
1560
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001561 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001562 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001563 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001564
1565 target->state = SRP_TARGET_LIVE;
1566
Roland Dreieraef9ec32005-11-02 14:07:13 -08001567 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001568 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001569
1570 return 0;
1571}
1572
1573static void srp_release_class_dev(struct class_device *class_dev)
1574{
1575 struct srp_host *host =
1576 container_of(class_dev, struct srp_host, class_dev);
1577
1578 complete(&host->released);
1579}
1580
1581static struct class srp_class = {
1582 .name = "infiniband_srp",
1583 .release = srp_release_class_dev
1584};
1585
1586/*
1587 * Target ports are added by writing
1588 *
1589 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1590 * pkey=<P_Key>,service_id=<service ID>
1591 *
1592 * to the add_target sysfs attribute.
1593 */
1594enum {
1595 SRP_OPT_ERR = 0,
1596 SRP_OPT_ID_EXT = 1 << 0,
1597 SRP_OPT_IOC_GUID = 1 << 1,
1598 SRP_OPT_DGID = 1 << 2,
1599 SRP_OPT_PKEY = 1 << 3,
1600 SRP_OPT_SERVICE_ID = 1 << 4,
1601 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001602 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001603 SRP_OPT_IO_CLASS = 1 << 7,
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001604 SRP_OPT_INITIATOR_EXT = 1 << 8,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001605 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1606 SRP_OPT_IOC_GUID |
1607 SRP_OPT_DGID |
1608 SRP_OPT_PKEY |
1609 SRP_OPT_SERVICE_ID),
1610};
1611
1612static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001613 { SRP_OPT_ID_EXT, "id_ext=%s" },
1614 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1615 { SRP_OPT_DGID, "dgid=%s" },
1616 { SRP_OPT_PKEY, "pkey=%x" },
1617 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1618 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1619 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001620 { SRP_OPT_IO_CLASS, "io_class=%x" },
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001621 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001622 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001623};
1624
1625static int srp_parse_options(const char *buf, struct srp_target_port *target)
1626{
1627 char *options, *sep_opt;
1628 char *p;
1629 char dgid[3];
1630 substring_t args[MAX_OPT_ARGS];
1631 int opt_mask = 0;
1632 int token;
1633 int ret = -EINVAL;
1634 int i;
1635
1636 options = kstrdup(buf, GFP_KERNEL);
1637 if (!options)
1638 return -ENOMEM;
1639
1640 sep_opt = options;
1641 while ((p = strsep(&sep_opt, ",")) != NULL) {
1642 if (!*p)
1643 continue;
1644
1645 token = match_token(p, srp_opt_tokens, args);
1646 opt_mask |= token;
1647
1648 switch (token) {
1649 case SRP_OPT_ID_EXT:
1650 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001651 if (!p) {
1652 ret = -ENOMEM;
1653 goto out;
1654 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001655 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1656 kfree(p);
1657 break;
1658
1659 case SRP_OPT_IOC_GUID:
1660 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001661 if (!p) {
1662 ret = -ENOMEM;
1663 goto out;
1664 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001665 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1666 kfree(p);
1667 break;
1668
1669 case SRP_OPT_DGID:
1670 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001671 if (!p) {
1672 ret = -ENOMEM;
1673 goto out;
1674 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001675 if (strlen(p) != 32) {
1676 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001677 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001678 goto out;
1679 }
1680
1681 for (i = 0; i < 16; ++i) {
1682 strlcpy(dgid, p + i * 2, 3);
1683 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1684 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001685 kfree(p);
Ishai Rabinovitz3633b3d2007-05-06 21:18:11 -07001686 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001687 break;
1688
1689 case SRP_OPT_PKEY:
1690 if (match_hex(args, &token)) {
1691 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1692 goto out;
1693 }
1694 target->path.pkey = cpu_to_be16(token);
1695 break;
1696
1697 case SRP_OPT_SERVICE_ID:
1698 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001699 if (!p) {
1700 ret = -ENOMEM;
1701 goto out;
1702 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001703 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1704 kfree(p);
1705 break;
1706
1707 case SRP_OPT_MAX_SECT:
1708 if (match_int(args, &token)) {
1709 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1710 goto out;
1711 }
1712 target->scsi_host->max_sectors = token;
1713 break;
1714
Vu Pham52fb2b502006-06-17 20:37:31 -07001715 case SRP_OPT_MAX_CMD_PER_LUN:
1716 if (match_int(args, &token)) {
1717 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1718 goto out;
1719 }
1720 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1721 break;
1722
Ramachandra K0c0450db2006-06-17 20:37:38 -07001723 case SRP_OPT_IO_CLASS:
1724 if (match_hex(args, &token)) {
1725 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1726 goto out;
1727 }
1728 if (token != SRP_REV10_IB_IO_CLASS &&
1729 token != SRP_REV16A_IB_IO_CLASS) {
1730 printk(KERN_WARNING PFX "unknown IO class parameter value"
1731 " %x specified (use %x or %x).\n",
1732 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1733 goto out;
1734 }
1735 target->io_class = token;
1736 break;
1737
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001738 case SRP_OPT_INITIATOR_EXT:
1739 p = match_strdup(args);
Ishai Rabinovitza20f3a62007-01-16 17:20:25 +02001740 if (!p) {
1741 ret = -ENOMEM;
1742 goto out;
1743 }
Ishai Rabinovitz01cb9bc2006-10-04 15:28:56 +02001744 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1745 kfree(p);
1746 break;
1747
Roland Dreieraef9ec32005-11-02 14:07:13 -08001748 default:
1749 printk(KERN_WARNING PFX "unknown parameter or missing value "
1750 "'%s' in target creation request\n", p);
1751 goto out;
1752 }
1753 }
1754
1755 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1756 ret = 0;
1757 else
1758 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1759 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1760 !(srp_opt_tokens[i].token & opt_mask))
1761 printk(KERN_WARNING PFX "target creation request is "
1762 "missing parameter '%s'\n",
1763 srp_opt_tokens[i].pattern);
1764
1765out:
1766 kfree(options);
1767 return ret;
1768}
1769
1770static ssize_t srp_create_target(struct class_device *class_dev,
1771 const char *buf, size_t count)
1772{
1773 struct srp_host *host =
1774 container_of(class_dev, struct srp_host, class_dev);
1775 struct Scsi_Host *target_host;
1776 struct srp_target_port *target;
1777 int ret;
1778 int i;
1779
1780 target_host = scsi_host_alloc(&srp_template,
1781 sizeof (struct srp_target_port));
1782 if (!target_host)
1783 return -ENOMEM;
1784
Arne Redlich3c8edf02006-11-15 12:43:00 +01001785 target_host->max_lun = SRP_MAX_LUN;
1786 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
Roland Dreier5f068992005-11-11 14:06:01 -08001787
Roland Dreieraef9ec32005-11-02 14:07:13 -08001788 target = host_to_target(target_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001789
Ramachandra K0c0450db2006-06-17 20:37:38 -07001790 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001791 target->scsi_host = target_host;
1792 target->srp_host = host;
1793
Roland Dreierd945e1d2006-05-09 10:50:28 -07001794 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001795 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001796 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1797 target->req_ring[i].index = i;
1798 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1799 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001800
1801 ret = srp_parse_options(buf, target);
1802 if (ret)
1803 goto err;
1804
Roland Dreierf5358a12006-06-17 20:37:29 -07001805 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001806
1807 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1808 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1809 (unsigned long long) be64_to_cpu(target->id_ext),
1810 (unsigned long long) be64_to_cpu(target->ioc_guid),
1811 be16_to_cpu(target->path.pkey),
1812 (unsigned long long) be64_to_cpu(target->service_id),
1813 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1814 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1815 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1816 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1817 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1818 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1819 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1820 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1821
1822 ret = srp_create_target_ib(target);
1823 if (ret)
1824 goto err;
1825
Roland Dreierf5358a12006-06-17 20:37:29 -07001826 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001827 if (IS_ERR(target->cm_id)) {
1828 ret = PTR_ERR(target->cm_id);
1829 goto err_free;
1830 }
1831
Ishai Rabinovitz1033ff62007-01-16 17:26:22 +02001832 target->qp_in_error = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001833 ret = srp_connect_target(target);
1834 if (ret) {
1835 printk(KERN_ERR PFX "Connection failed\n");
1836 goto err_cm_id;
1837 }
1838
1839 ret = srp_add_target(host, target);
1840 if (ret)
1841 goto err_disconnect;
1842
1843 return count;
1844
1845err_disconnect:
1846 srp_disconnect_target(target);
1847
1848err_cm_id:
1849 ib_destroy_cm_id(target->cm_id);
1850
1851err_free:
1852 srp_free_target_ib(target);
1853
1854err:
1855 scsi_host_put(target_host);
1856
1857 return ret;
1858}
1859
1860static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1861
1862static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1863{
1864 struct srp_host *host =
1865 container_of(class_dev, struct srp_host, class_dev);
1866
Roland Dreierf5358a12006-06-17 20:37:29 -07001867 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001868}
1869
1870static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1871
1872static ssize_t show_port(struct class_device *class_dev, char *buf)
1873{
1874 struct srp_host *host =
1875 container_of(class_dev, struct srp_host, class_dev);
1876
1877 return sprintf(buf, "%d\n", host->port);
1878}
1879
1880static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1881
Roland Dreierf5358a12006-06-17 20:37:29 -07001882static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001883{
1884 struct srp_host *host;
1885
1886 host = kzalloc(sizeof *host, GFP_KERNEL);
1887 if (!host)
1888 return NULL;
1889
1890 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001891 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001892 init_completion(&host->released);
1893 host->dev = device;
1894 host->port = port;
1895
Roland Dreieraef9ec32005-11-02 14:07:13 -08001896 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001897 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001898 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001899 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001900
1901 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001902 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001903 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1904 goto err_class;
1905 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1906 goto err_class;
1907 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1908 goto err_class;
1909
1910 return host;
1911
1912err_class:
1913 class_device_unregister(&host->class_dev);
1914
Roland Dreierf5358a12006-06-17 20:37:29 -07001915free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001916 kfree(host);
1917
1918 return NULL;
1919}
1920
1921static void srp_add_one(struct ib_device *device)
1922{
Roland Dreierf5358a12006-06-17 20:37:29 -07001923 struct srp_device *srp_dev;
1924 struct ib_device_attr *dev_attr;
1925 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001926 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001927 int s, e, p;
1928
Roland Dreierf5358a12006-06-17 20:37:29 -07001929 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1930 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001931 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001932
Roland Dreierf5358a12006-06-17 20:37:29 -07001933 if (ib_query_device(device, dev_attr)) {
1934 printk(KERN_WARNING PFX "Query device failed for %s\n",
1935 device->name);
1936 goto free_attr;
1937 }
1938
1939 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1940 if (!srp_dev)
1941 goto free_attr;
1942
1943 /*
1944 * Use the smallest page size supported by the HCA, down to a
1945 * minimum of 512 bytes (which is the smallest sector that a
1946 * SCSI command will ever carry).
1947 */
1948 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1949 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
Roland Dreierbf628dc2006-12-15 14:01:49 -08001950 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
Roland Dreierf5358a12006-06-17 20:37:29 -07001951
1952 INIT_LIST_HEAD(&srp_dev->dev_list);
1953
1954 srp_dev->dev = device;
1955 srp_dev->pd = ib_alloc_pd(device);
1956 if (IS_ERR(srp_dev->pd))
1957 goto free_dev;
1958
1959 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1960 IB_ACCESS_LOCAL_WRITE |
1961 IB_ACCESS_REMOTE_READ |
1962 IB_ACCESS_REMOTE_WRITE);
1963 if (IS_ERR(srp_dev->mr))
1964 goto err_pd;
1965
1966 memset(&fmr_param, 0, sizeof fmr_param);
1967 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1968 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1969 fmr_param.cache = 1;
1970 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1971 fmr_param.page_shift = srp_dev->fmr_page_shift;
1972 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1973 IB_ACCESS_REMOTE_WRITE |
1974 IB_ACCESS_REMOTE_READ);
1975
1976 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1977 if (IS_ERR(srp_dev->fmr_pool))
1978 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001979
Tom Tucker07ebafb2006-08-03 16:02:42 -05001980 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001981 s = 0;
1982 e = 0;
1983 } else {
1984 s = 1;
1985 e = device->phys_port_cnt;
1986 }
1987
1988 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001989 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001990 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001991 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001992 }
1993
Roland Dreierf5358a12006-06-17 20:37:29 -07001994 ib_set_client_data(device, &srp_client, srp_dev);
1995
1996 goto free_attr;
1997
1998err_pd:
1999 ib_dealloc_pd(srp_dev->pd);
2000
2001free_dev:
2002 kfree(srp_dev);
2003
2004free_attr:
2005 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002006}
2007
2008static void srp_remove_one(struct ib_device *device)
2009{
Roland Dreierf5358a12006-06-17 20:37:29 -07002010 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002011 struct srp_host *host, *tmp_host;
2012 LIST_HEAD(target_list);
2013 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08002014
Roland Dreierf5358a12006-06-17 20:37:29 -07002015 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002016
Roland Dreierf5358a12006-06-17 20:37:29 -07002017 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08002018 class_device_unregister(&host->class_dev);
2019 /*
2020 * Wait for the sysfs entry to go away, so that no new
2021 * target ports can be created.
2022 */
2023 wait_for_completion(&host->released);
2024
2025 /*
2026 * Mark all target ports as removed, so we stop queueing
2027 * commands and don't try to reconnect.
2028 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002029 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07002030 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07002031 spin_lock_irq(target->scsi_host->host_lock);
2032 target->state = SRP_TARGET_REMOVED;
2033 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002034 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07002035 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002036
2037 /*
2038 * Wait for any reconnection tasks that may have
2039 * started before we marked our target ports as
2040 * removed, and any target port removal tasks.
2041 */
2042 flush_scheduled_work();
2043
2044 list_for_each_entry_safe(target, tmp_target,
2045 &host->target_list, list) {
2046 scsi_remove_host(target->scsi_host);
2047 srp_disconnect_target(target);
2048 ib_destroy_cm_id(target->cm_id);
2049 srp_free_target_ib(target);
2050 scsi_host_put(target->scsi_host);
2051 }
2052
Roland Dreieraef9ec32005-11-02 14:07:13 -08002053 kfree(host);
2054 }
2055
Roland Dreierf5358a12006-06-17 20:37:29 -07002056 if (srp_dev->fmr_pool)
2057 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2058 ib_dereg_mr(srp_dev->mr);
2059 ib_dealloc_pd(srp_dev->pd);
2060
2061 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002062}
2063
2064static int __init srp_init_module(void)
2065{
2066 int ret;
2067
Vu Pham74b0a152006-06-17 20:37:32 -07002068 srp_template.sg_tablesize = srp_sg_tablesize;
2069 srp_max_iu_len = (sizeof (struct srp_cmd) +
2070 sizeof (struct srp_indirect_buf) +
2071 srp_sg_tablesize * 16);
2072
Roland Dreieraef9ec32005-11-02 14:07:13 -08002073 ret = class_register(&srp_class);
2074 if (ret) {
2075 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2076 return ret;
2077 }
2078
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002079 ib_sa_register_client(&srp_sa_client);
2080
Roland Dreieraef9ec32005-11-02 14:07:13 -08002081 ret = ib_register_client(&srp_client);
2082 if (ret) {
2083 printk(KERN_ERR PFX "couldn't register IB client\n");
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002084 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002085 class_unregister(&srp_class);
2086 return ret;
2087 }
2088
2089 return 0;
2090}
2091
2092static void __exit srp_cleanup_module(void)
2093{
2094 ib_unregister_client(&srp_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002095 ib_sa_unregister_client(&srp_sa_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08002096 class_unregister(&srp_class);
2097}
2098
2099module_init(srp_init_module);
2100module_exit(srp_cleanup_module);