blob: 4e22afef72062571c12b3489bc9e26fa998fce43 [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
80static void srp_add_one(struct ib_device *device);
81static void srp_remove_one(struct ib_device *device);
82static void srp_completion(struct ib_cq *cq, void *target_ptr);
83static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
84
85static struct ib_client srp_client = {
86 .name = "srp",
87 .add = srp_add_one,
88 .remove = srp_remove_one
89};
90
91static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
92{
93 return (struct srp_target_port *) host->hostdata;
94}
95
96static const char *srp_target_info(struct Scsi_Host *host)
97{
98 return host_to_target(host)->target_name;
99}
100
101static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
102 gfp_t gfp_mask,
103 enum dma_data_direction direction)
104{
105 struct srp_iu *iu;
106
107 iu = kmalloc(sizeof *iu, gfp_mask);
108 if (!iu)
109 goto out;
110
111 iu->buf = kzalloc(size, gfp_mask);
112 if (!iu->buf)
113 goto out_free_iu;
114
Roland Dreierf5358a12006-06-17 20:37:29 -0700115 iu->dma = dma_map_single(host->dev->dev->dma_device,
116 iu->buf, size, direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800117 if (dma_mapping_error(iu->dma))
118 goto out_free_buf;
119
120 iu->size = size;
121 iu->direction = direction;
122
123 return iu;
124
125out_free_buf:
126 kfree(iu->buf);
127out_free_iu:
128 kfree(iu);
129out:
130 return NULL;
131}
132
133static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
134{
135 if (!iu)
136 return;
137
Roland Dreierf5358a12006-06-17 20:37:29 -0700138 dma_unmap_single(host->dev->dev->dma_device,
139 iu->dma, iu->size, iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800140 kfree(iu->buf);
141 kfree(iu);
142}
143
144static void srp_qp_event(struct ib_event *event, void *context)
145{
146 printk(KERN_ERR PFX "QP event %d\n", event->event);
147}
148
149static int srp_init_qp(struct srp_target_port *target,
150 struct ib_qp *qp)
151{
152 struct ib_qp_attr *attr;
153 int ret;
154
155 attr = kmalloc(sizeof *attr, GFP_KERNEL);
156 if (!attr)
157 return -ENOMEM;
158
Roland Dreierf5358a12006-06-17 20:37:29 -0700159 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800160 target->srp_host->port,
161 be16_to_cpu(target->path.pkey),
162 &attr->pkey_index);
163 if (ret)
164 goto out;
165
166 attr->qp_state = IB_QPS_INIT;
167 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
168 IB_ACCESS_REMOTE_WRITE);
169 attr->port_num = target->srp_host->port;
170
171 ret = ib_modify_qp(qp, attr,
172 IB_QP_STATE |
173 IB_QP_PKEY_INDEX |
174 IB_QP_ACCESS_FLAGS |
175 IB_QP_PORT);
176
177out:
178 kfree(attr);
179 return ret;
180}
181
182static int srp_create_target_ib(struct srp_target_port *target)
183{
184 struct ib_qp_init_attr *init_attr;
185 int ret;
186
187 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
188 if (!init_attr)
189 return -ENOMEM;
190
Roland Dreierf5358a12006-06-17 20:37:29 -0700191 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800192 NULL, target, SRP_CQ_SIZE);
193 if (IS_ERR(target->cq)) {
194 ret = PTR_ERR(target->cq);
195 goto out;
196 }
197
198 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
199
200 init_attr->event_handler = srp_qp_event;
201 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
202 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
203 init_attr->cap.max_recv_sge = 1;
204 init_attr->cap.max_send_sge = 1;
205 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
206 init_attr->qp_type = IB_QPT_RC;
207 init_attr->send_cq = target->cq;
208 init_attr->recv_cq = target->cq;
209
Roland Dreierf5358a12006-06-17 20:37:29 -0700210 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800211 if (IS_ERR(target->qp)) {
212 ret = PTR_ERR(target->qp);
213 ib_destroy_cq(target->cq);
214 goto out;
215 }
216
217 ret = srp_init_qp(target, target->qp);
218 if (ret) {
219 ib_destroy_qp(target->qp);
220 ib_destroy_cq(target->cq);
221 goto out;
222 }
223
224out:
225 kfree(init_attr);
226 return ret;
227}
228
229static void srp_free_target_ib(struct srp_target_port *target)
230{
231 int i;
232
233 ib_destroy_qp(target->qp);
234 ib_destroy_cq(target->cq);
235
236 for (i = 0; i < SRP_RQ_SIZE; ++i)
237 srp_free_iu(target->srp_host, target->rx_ring[i]);
238 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
239 srp_free_iu(target->srp_host, target->tx_ring[i]);
240}
241
242static void srp_path_rec_completion(int status,
243 struct ib_sa_path_rec *pathrec,
244 void *target_ptr)
245{
246 struct srp_target_port *target = target_ptr;
247
248 target->status = status;
249 if (status)
250 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
251 else
252 target->path = *pathrec;
253 complete(&target->done);
254}
255
256static int srp_lookup_path(struct srp_target_port *target)
257{
258 target->path.numb_path = 1;
259
260 init_completion(&target->done);
261
Roland Dreierf5358a12006-06-17 20:37:29 -0700262 target->path_query_id = ib_sa_path_rec_get(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800263 target->srp_host->port,
264 &target->path,
265 IB_SA_PATH_REC_DGID |
266 IB_SA_PATH_REC_SGID |
267 IB_SA_PATH_REC_NUMB_PATH |
268 IB_SA_PATH_REC_PKEY,
269 SRP_PATH_REC_TIMEOUT_MS,
270 GFP_KERNEL,
271 srp_path_rec_completion,
272 target, &target->path_query);
273 if (target->path_query_id < 0)
274 return target->path_query_id;
275
276 wait_for_completion(&target->done);
277
278 if (target->status < 0)
279 printk(KERN_WARNING PFX "Path record query failed\n");
280
281 return target->status;
282}
283
284static int srp_send_req(struct srp_target_port *target)
285{
286 struct {
287 struct ib_cm_req_param param;
288 struct srp_login_req priv;
289 } *req = NULL;
290 int status;
291
292 req = kzalloc(sizeof *req, GFP_KERNEL);
293 if (!req)
294 return -ENOMEM;
295
296 req->param.primary_path = &target->path;
297 req->param.alternate_path = NULL;
298 req->param.service_id = target->service_id;
299 req->param.qp_num = target->qp->qp_num;
300 req->param.qp_type = target->qp->qp_type;
301 req->param.private_data = &req->priv;
302 req->param.private_data_len = sizeof req->priv;
303 req->param.flow_control = 1;
304
305 get_random_bytes(&req->param.starting_psn, 4);
306 req->param.starting_psn &= 0xffffff;
307
308 /*
309 * Pick some arbitrary defaults here; we could make these
310 * module parameters if anyone cared about setting them.
311 */
312 req->param.responder_resources = 4;
313 req->param.remote_cm_response_timeout = 20;
314 req->param.local_cm_response_timeout = 20;
315 req->param.retry_count = 7;
316 req->param.rnr_retry_count = 7;
317 req->param.max_cm_retries = 15;
318
319 req->priv.opcode = SRP_LOGIN_REQ;
320 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700321 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800322 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
323 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700324 /*
325 * In the published SRP specification (draft rev. 16a), the
326 * port identifier format is 8 bytes of ID extension followed
327 * by 8 bytes of GUID. Older drafts put the two halves in the
328 * opposite order, so that the GUID comes first.
329 *
330 * Targets conforming to these obsolete drafts can be
331 * recognized by the I/O Class they report.
332 */
333 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
334 memcpy(req->priv.initiator_port_id,
335 target->srp_host->initiator_port_id + 8, 8);
336 memcpy(req->priv.initiator_port_id + 8,
337 target->srp_host->initiator_port_id, 8);
338 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
339 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
340 } else {
341 memcpy(req->priv.initiator_port_id,
342 target->srp_host->initiator_port_id, 16);
343 memcpy(req->priv.target_port_id, &target->id_ext, 8);
344 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
345 }
346
Roland Dreieraef9ec32005-11-02 14:07:13 -0800347 /*
348 * Topspin/Cisco SRP targets will reject our login unless we
349 * zero out the first 8 bytes of our initiator port ID. The
350 * second 8 bytes must be our local node GUID, but we always
351 * use that anyway.
352 */
353 if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
354 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
355 "activated for target GUID %016llx\n",
356 (unsigned long long) be64_to_cpu(target->ioc_guid));
357 memset(req->priv.initiator_port_id, 0, 8);
358 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800359
360 status = ib_send_cm_req(target->cm_id, &req->param);
361
362 kfree(req);
363
364 return status;
365}
366
367static void srp_disconnect_target(struct srp_target_port *target)
368{
369 /* XXX should send SRP_I_LOGOUT request */
370
371 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700372 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
373 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
374 return;
375 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800376 wait_for_completion(&target->done);
377}
378
379static void srp_remove_work(void *target_ptr)
380{
381 struct srp_target_port *target = target_ptr;
382
383 spin_lock_irq(target->scsi_host->host_lock);
384 if (target->state != SRP_TARGET_DEAD) {
385 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800386 return;
387 }
388 target->state = SRP_TARGET_REMOVED;
389 spin_unlock_irq(target->scsi_host->host_lock);
390
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700391 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800392 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700393 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800394
395 scsi_remove_host(target->scsi_host);
396 ib_destroy_cm_id(target->cm_id);
397 srp_free_target_ib(target);
398 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800399}
400
401static int srp_connect_target(struct srp_target_port *target)
402{
403 int ret;
404
405 ret = srp_lookup_path(target);
406 if (ret)
407 return ret;
408
409 while (1) {
410 init_completion(&target->done);
411 ret = srp_send_req(target);
412 if (ret)
413 return ret;
414 wait_for_completion(&target->done);
415
416 /*
417 * The CM event handling code will set status to
418 * SRP_PORT_REDIRECT if we get a port redirect REJ
419 * back, or SRP_DLID_REDIRECT if we get a lid/qp
420 * redirect REJ back.
421 */
422 switch (target->status) {
423 case 0:
424 return 0;
425
426 case SRP_PORT_REDIRECT:
427 ret = srp_lookup_path(target);
428 if (ret)
429 return ret;
430 break;
431
432 case SRP_DLID_REDIRECT:
433 break;
434
435 default:
436 return target->status;
437 }
438 }
439}
440
Roland Dreierd945e1d2006-05-09 10:50:28 -0700441static void srp_unmap_data(struct scsi_cmnd *scmnd,
442 struct srp_target_port *target,
443 struct srp_request *req)
444{
445 struct scatterlist *scat;
446 int nents;
447
448 if (!scmnd->request_buffer ||
449 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
450 scmnd->sc_data_direction != DMA_FROM_DEVICE))
451 return;
452
Roland Dreierf5358a12006-06-17 20:37:29 -0700453 if (req->fmr) {
454 ib_fmr_pool_unmap(req->fmr);
455 req->fmr = NULL;
456 }
457
Roland Dreierd945e1d2006-05-09 10:50:28 -0700458 /*
459 * This handling of non-SG commands can be killed when the
460 * SCSI midlayer no longer generates non-SG commands.
461 */
462 if (likely(scmnd->use_sg)) {
463 nents = scmnd->use_sg;
464 scat = scmnd->request_buffer;
465 } else {
466 nents = 1;
467 scat = &req->fake_sg;
468 }
469
Roland Dreierf5358a12006-06-17 20:37:29 -0700470 dma_unmap_sg(target->srp_host->dev->dev->dma_device, scat, nents,
Roland Dreierd945e1d2006-05-09 10:50:28 -0700471 scmnd->sc_data_direction);
472}
473
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700474static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
475{
476 srp_unmap_data(req->scmnd, target, req);
477 list_move_tail(&req->list, &target->free_reqs);
478}
479
480static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
481{
482 req->scmnd->result = DID_RESET << 16;
483 req->scmnd->scsi_done(req->scmnd);
484 srp_remove_req(target, req);
485}
486
Roland Dreieraef9ec32005-11-02 14:07:13 -0800487static int srp_reconnect_target(struct srp_target_port *target)
488{
489 struct ib_cm_id *new_cm_id;
490 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700491 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800492 struct ib_wc wc;
493 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800494
495 spin_lock_irq(target->scsi_host->host_lock);
496 if (target->state != SRP_TARGET_LIVE) {
497 spin_unlock_irq(target->scsi_host->host_lock);
498 return -EAGAIN;
499 }
500 target->state = SRP_TARGET_CONNECTING;
501 spin_unlock_irq(target->scsi_host->host_lock);
502
503 srp_disconnect_target(target);
504 /*
505 * Now get a new local CM ID so that we avoid confusing the
506 * target in case things are really fouled up.
507 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700508 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800509 srp_cm_handler, target);
510 if (IS_ERR(new_cm_id)) {
511 ret = PTR_ERR(new_cm_id);
512 goto err;
513 }
514 ib_destroy_cm_id(target->cm_id);
515 target->cm_id = new_cm_id;
516
517 qp_attr.qp_state = IB_QPS_RESET;
518 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
519 if (ret)
520 goto err;
521
522 ret = srp_init_qp(target, target->qp);
523 if (ret)
524 goto err;
525
526 while (ib_poll_cq(target->cq, 1, &wc) > 0)
527 ; /* nothing */
528
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700529 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
530 srp_reset_req(target, req);
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
536 ret = srp_connect_target(target);
537 if (ret)
538 goto err;
539
540 spin_lock_irq(target->scsi_host->host_lock);
541 if (target->state == SRP_TARGET_CONNECTING) {
542 ret = 0;
543 target->state = SRP_TARGET_LIVE;
544 } else
545 ret = -EAGAIN;
546 spin_unlock_irq(target->scsi_host->host_lock);
547
548 return ret;
549
550err:
551 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
552
553 /*
554 * We couldn't reconnect, so kill our target port off.
555 * However, we have to defer the real removal because we might
556 * be in the context of the SCSI error handler now, which
557 * would deadlock if we call scsi_remove_host().
558 */
559 spin_lock_irq(target->scsi_host->host_lock);
560 if (target->state == SRP_TARGET_CONNECTING) {
561 target->state = SRP_TARGET_DEAD;
562 INIT_WORK(&target->work, srp_remove_work, target);
563 schedule_work(&target->work);
564 }
565 spin_unlock_irq(target->scsi_host->host_lock);
566
567 return ret;
568}
569
Roland Dreierf5358a12006-06-17 20:37:29 -0700570static int srp_map_fmr(struct srp_device *dev, struct scatterlist *scat,
571 int sg_cnt, struct srp_request *req,
572 struct srp_direct_buf *buf)
573{
574 u64 io_addr = 0;
575 u64 *dma_pages;
576 u32 len;
577 int page_cnt;
578 int i, j;
579 int ret;
580
581 if (!dev->fmr_pool)
582 return -ENODEV;
583
584 len = page_cnt = 0;
585 for (i = 0; i < sg_cnt; ++i) {
586 if (sg_dma_address(&scat[i]) & ~dev->fmr_page_mask) {
587 if (i > 0)
588 return -EINVAL;
589 else
590 ++page_cnt;
591 }
592 if ((sg_dma_address(&scat[i]) + sg_dma_len(&scat[i])) &
593 ~dev->fmr_page_mask) {
594 if (i < sg_cnt - 1)
595 return -EINVAL;
596 else
597 ++page_cnt;
598 }
599
600 len += sg_dma_len(&scat[i]);
601 }
602
603 page_cnt += len >> dev->fmr_page_shift;
604 if (page_cnt > SRP_FMR_SIZE)
605 return -ENOMEM;
606
607 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
608 if (!dma_pages)
609 return -ENOMEM;
610
611 page_cnt = 0;
612 for (i = 0; i < sg_cnt; ++i)
613 for (j = 0; j < sg_dma_len(&scat[i]); j += dev->fmr_page_size)
614 dma_pages[page_cnt++] =
615 (sg_dma_address(&scat[i]) & dev->fmr_page_mask) + j;
616
617 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
618 dma_pages, page_cnt, &io_addr);
619 if (IS_ERR(req->fmr)) {
620 ret = PTR_ERR(req->fmr);
621 goto out;
622 }
623
624 buf->va = cpu_to_be64(sg_dma_address(&scat[0]) & ~dev->fmr_page_mask);
625 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
626 buf->len = cpu_to_be32(len);
627
628 ret = 0;
629
630out:
631 kfree(dma_pages);
632
633 return ret;
634}
635
Roland Dreieraef9ec32005-11-02 14:07:13 -0800636static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
637 struct srp_request *req)
638{
Roland Dreiercf368712006-03-24 15:47:26 -0800639 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800640 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800641 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700642 u8 fmt = SRP_DATA_DESC_DIRECT;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800643
644 if (!scmnd->request_buffer || scmnd->sc_data_direction == DMA_NONE)
645 return sizeof (struct srp_cmd);
646
647 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
648 scmnd->sc_data_direction != DMA_TO_DEVICE) {
649 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
650 scmnd->sc_data_direction);
651 return -EINVAL;
652 }
653
Roland Dreiercf368712006-03-24 15:47:26 -0800654 /*
655 * This handling of non-SG commands can be killed when the
656 * SCSI midlayer no longer generates non-SG commands.
657 */
658 if (likely(scmnd->use_sg)) {
659 nents = scmnd->use_sg;
660 scat = scmnd->request_buffer;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800661 } else {
Roland Dreiercf368712006-03-24 15:47:26 -0800662 nents = 1;
663 scat = &req->fake_sg;
664 sg_init_one(scat, scmnd->request_buffer, scmnd->request_bufflen);
665 }
666
Roland Dreierf5358a12006-06-17 20:37:29 -0700667 count = dma_map_sg(target->srp_host->dev->dev->dma_device,
668 scat, nents, scmnd->sc_data_direction);
669
670 fmt = SRP_DATA_DESC_DIRECT;
671 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800672
673 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700674 /*
675 * The midlayer only generated a single gather/scatter
676 * entry, or DMA mapping coalesced everything to a
677 * single entry. So a direct descriptor along with
678 * the DMA MR suffices.
679 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800680 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800681
Roland Dreiercf368712006-03-24 15:47:26 -0800682 buf->va = cpu_to_be64(sg_dma_address(scat));
Roland Dreierf5358a12006-06-17 20:37:29 -0700683 buf->key = cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800684 buf->len = cpu_to_be32(sg_dma_len(scat));
Roland Dreierf5358a12006-06-17 20:37:29 -0700685 } else if (srp_map_fmr(target->srp_host->dev, scat, count, req,
686 (void *) cmd->add_data)) {
687 /*
688 * FMR mapping failed, and the scatterlist has more
689 * than one entry. Generate an indirect memory
690 * descriptor.
691 */
Roland Dreiercf368712006-03-24 15:47:26 -0800692 struct srp_indirect_buf *buf = (void *) cmd->add_data;
693 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700694 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800695
696 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700697 len = sizeof (struct srp_cmd) +
698 sizeof (struct srp_indirect_buf) +
699 count * sizeof (struct srp_direct_buf);
700
701 for (i = 0; i < count; ++i) {
702 buf->desc_list[i].va =
703 cpu_to_be64(sg_dma_address(&scat[i]));
704 buf->desc_list[i].key =
705 cpu_to_be32(target->srp_host->dev->mr->rkey);
706 buf->desc_list[i].len =
707 cpu_to_be32(sg_dma_len(&scat[i]));
708 datalen += sg_dma_len(&scat[i]);
709 }
Roland Dreiercf368712006-03-24 15:47:26 -0800710
711 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
712 cmd->data_out_desc_cnt = count;
713 else
714 cmd->data_in_desc_cnt = count;
715
Roland Dreierf5358a12006-06-17 20:37:29 -0700716 buf->table_desc.va =
717 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800718 buf->table_desc.key =
Roland Dreierf5358a12006-06-17 20:37:29 -0700719 cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800720 buf->table_desc.len =
721 cpu_to_be32(count * sizeof (struct srp_direct_buf));
722
Roland Dreiercf368712006-03-24 15:47:26 -0800723 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800724 }
725
726 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
727 cmd->buf_fmt = fmt << 4;
728 else
729 cmd->buf_fmt = fmt;
730
Roland Dreieraef9ec32005-11-02 14:07:13 -0800731 return len;
732}
733
Roland Dreieraef9ec32005-11-02 14:07:13 -0800734static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
735{
736 struct srp_request *req;
737 struct scsi_cmnd *scmnd;
738 unsigned long flags;
739 s32 delta;
740
741 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
742
743 spin_lock_irqsave(target->scsi_host->host_lock, flags);
744
745 target->req_lim += delta;
746
747 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
748
749 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
750 if (be32_to_cpu(rsp->resp_data_len) < 4)
751 req->tsk_status = -1;
752 else
753 req->tsk_status = rsp->data[3];
754 complete(&req->done);
755 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700756 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800757 if (!scmnd)
758 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
759 (unsigned long long) rsp->tag);
760 scmnd->result = rsp->status;
761
762 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
763 memcpy(scmnd->sense_buffer, rsp->data +
764 be32_to_cpu(rsp->resp_data_len),
765 min_t(int, be32_to_cpu(rsp->sense_data_len),
766 SCSI_SENSE_BUFFERSIZE));
767 }
768
769 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
770 scmnd->resid = be32_to_cpu(rsp->data_out_res_cnt);
771 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
772 scmnd->resid = be32_to_cpu(rsp->data_in_res_cnt);
773
Roland Dreieraef9ec32005-11-02 14:07:13 -0800774 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800775 scmnd->host_scribble = (void *) -1L;
776 scmnd->scsi_done(scmnd);
777
Roland Dreierd945e1d2006-05-09 10:50:28 -0700778 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800779 } else
780 req->cmd_done = 1;
781 }
782
783 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
784}
785
786static void srp_reconnect_work(void *target_ptr)
787{
788 struct srp_target_port *target = target_ptr;
789
790 srp_reconnect_target(target);
791}
792
793static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
794{
795 struct srp_iu *iu;
796 u8 opcode;
797
798 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
799
Roland Dreierf5358a12006-06-17 20:37:29 -0700800 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800801 target->max_ti_iu_len, DMA_FROM_DEVICE);
802
803 opcode = *(u8 *) iu->buf;
804
805 if (0) {
806 int i;
807
808 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
809
810 for (i = 0; i < wc->byte_len; ++i) {
811 if (i % 8 == 0)
812 printk(KERN_ERR " [%02x] ", i);
813 printk(" %02x", ((u8 *) iu->buf)[i]);
814 if ((i + 1) % 8 == 0)
815 printk("\n");
816 }
817
818 if (wc->byte_len % 8)
819 printk("\n");
820 }
821
822 switch (opcode) {
823 case SRP_RSP:
824 srp_process_rsp(target, iu->buf);
825 break;
826
827 case SRP_T_LOGOUT:
828 /* XXX Handle target logout */
829 printk(KERN_WARNING PFX "Got target logout request\n");
830 break;
831
832 default:
833 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
834 break;
835 }
836
Roland Dreierf5358a12006-06-17 20:37:29 -0700837 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800838 target->max_ti_iu_len, DMA_FROM_DEVICE);
839}
840
841static void srp_completion(struct ib_cq *cq, void *target_ptr)
842{
843 struct srp_target_port *target = target_ptr;
844 struct ib_wc wc;
845 unsigned long flags;
846
847 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
848 while (ib_poll_cq(cq, 1, &wc) > 0) {
849 if (wc.status) {
850 printk(KERN_ERR PFX "failed %s status %d\n",
851 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
852 wc.status);
853 spin_lock_irqsave(target->scsi_host->host_lock, flags);
854 if (target->state == SRP_TARGET_LIVE)
855 schedule_work(&target->work);
856 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
857 break;
858 }
859
860 if (wc.wr_id & SRP_OP_RECV)
861 srp_handle_recv(target, &wc);
862 else
863 ++target->tx_tail;
864 }
865}
866
867static int __srp_post_recv(struct srp_target_port *target)
868{
869 struct srp_iu *iu;
870 struct ib_sge list;
871 struct ib_recv_wr wr, *bad_wr;
872 unsigned int next;
873 int ret;
874
875 next = target->rx_head & (SRP_RQ_SIZE - 1);
876 wr.wr_id = next | SRP_OP_RECV;
877 iu = target->rx_ring[next];
878
879 list.addr = iu->dma;
880 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700881 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800882
883 wr.next = NULL;
884 wr.sg_list = &list;
885 wr.num_sge = 1;
886
887 ret = ib_post_recv(target->qp, &wr, &bad_wr);
888 if (!ret)
889 ++target->rx_head;
890
891 return ret;
892}
893
894static int srp_post_recv(struct srp_target_port *target)
895{
896 unsigned long flags;
897 int ret;
898
899 spin_lock_irqsave(target->scsi_host->host_lock, flags);
900 ret = __srp_post_recv(target);
901 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
902
903 return ret;
904}
905
906/*
907 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800908 * req_lim and tx_head. Lock cannot be dropped between call here and
909 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800910 */
911static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
912{
913 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
914 return NULL;
915
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700916 if (unlikely(target->req_lim < 1))
917 ++target->zero_req_lim;
Roland Dreier47f2bce2005-11-15 00:19:21 -0800918
Roland Dreieraef9ec32005-11-02 14:07:13 -0800919 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
920}
921
922/*
923 * Must be called with target->scsi_host->host_lock held to protect
924 * req_lim and tx_head.
925 */
926static int __srp_post_send(struct srp_target_port *target,
927 struct srp_iu *iu, int len)
928{
929 struct ib_sge list;
930 struct ib_send_wr wr, *bad_wr;
931 int ret = 0;
932
Roland Dreieraef9ec32005-11-02 14:07:13 -0800933 list.addr = iu->dma;
934 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700935 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800936
937 wr.next = NULL;
938 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
939 wr.sg_list = &list;
940 wr.num_sge = 1;
941 wr.opcode = IB_WR_SEND;
942 wr.send_flags = IB_SEND_SIGNALED;
943
944 ret = ib_post_send(target->qp, &wr, &bad_wr);
945
946 if (!ret) {
947 ++target->tx_head;
948 --target->req_lim;
949 }
950
951 return ret;
952}
953
954static int srp_queuecommand(struct scsi_cmnd *scmnd,
955 void (*done)(struct scsi_cmnd *))
956{
957 struct srp_target_port *target = host_to_target(scmnd->device->host);
958 struct srp_request *req;
959 struct srp_iu *iu;
960 struct srp_cmd *cmd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800961 int len;
962
963 if (target->state == SRP_TARGET_CONNECTING)
964 goto err;
965
966 if (target->state == SRP_TARGET_DEAD ||
967 target->state == SRP_TARGET_REMOVED) {
968 scmnd->result = DID_BAD_TARGET << 16;
969 done(scmnd);
970 return 0;
971 }
972
973 iu = __srp_get_tx_iu(target);
974 if (!iu)
975 goto err;
976
Roland Dreierf5358a12006-06-17 20:37:29 -0700977 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -0700978 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800979
Roland Dreierd945e1d2006-05-09 10:50:28 -0700980 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800981
982 scmnd->scsi_done = done;
983 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -0700984 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800985
986 cmd = iu->buf;
987 memset(cmd, 0, sizeof *cmd);
988
989 cmd->opcode = SRP_CMD;
990 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700991 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800992 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
993
Roland Dreieraef9ec32005-11-02 14:07:13 -0800994 req->scmnd = scmnd;
995 req->cmd = iu;
996 req->cmd_done = 0;
997 req->tsk_mgmt = NULL;
998
999 len = srp_map_data(scmnd, target, req);
1000 if (len < 0) {
1001 printk(KERN_ERR PFX "Failed to map data\n");
1002 goto err;
1003 }
1004
1005 if (__srp_post_recv(target)) {
1006 printk(KERN_ERR PFX "Recv failed\n");
1007 goto err_unmap;
1008 }
1009
Roland Dreierf5358a12006-06-17 20:37:29 -07001010 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -07001011 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001012
1013 if (__srp_post_send(target, iu, len)) {
1014 printk(KERN_ERR PFX "Send failed\n");
1015 goto err_unmap;
1016 }
1017
Roland Dreierd945e1d2006-05-09 10:50:28 -07001018 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001019
1020 return 0;
1021
1022err_unmap:
1023 srp_unmap_data(scmnd, target, req);
1024
1025err:
1026 return SCSI_MLQUEUE_HOST_BUSY;
1027}
1028
1029static int srp_alloc_iu_bufs(struct srp_target_port *target)
1030{
1031 int i;
1032
1033 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1034 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1035 target->max_ti_iu_len,
1036 GFP_KERNEL, DMA_FROM_DEVICE);
1037 if (!target->rx_ring[i])
1038 goto err;
1039 }
1040
1041 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1042 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001043 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001044 GFP_KERNEL, DMA_TO_DEVICE);
1045 if (!target->tx_ring[i])
1046 goto err;
1047 }
1048
1049 return 0;
1050
1051err:
1052 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1053 srp_free_iu(target->srp_host, target->rx_ring[i]);
1054 target->rx_ring[i] = NULL;
1055 }
1056
1057 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1058 srp_free_iu(target->srp_host, target->tx_ring[i]);
1059 target->tx_ring[i] = NULL;
1060 }
1061
1062 return -ENOMEM;
1063}
1064
1065static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1066 struct ib_cm_event *event,
1067 struct srp_target_port *target)
1068{
1069 struct ib_class_port_info *cpi;
1070 int opcode;
1071
1072 switch (event->param.rej_rcvd.reason) {
1073 case IB_CM_REJ_PORT_CM_REDIRECT:
1074 cpi = event->param.rej_rcvd.ari;
1075 target->path.dlid = cpi->redirect_lid;
1076 target->path.pkey = cpi->redirect_pkey;
1077 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1078 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1079
1080 target->status = target->path.dlid ?
1081 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1082 break;
1083
1084 case IB_CM_REJ_PORT_REDIRECT:
1085 if (topspin_workarounds &&
1086 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1087 /*
1088 * Topspin/Cisco SRP gateways incorrectly send
1089 * reject reason code 25 when they mean 24
1090 * (port redirect).
1091 */
1092 memcpy(target->path.dgid.raw,
1093 event->param.rej_rcvd.ari, 16);
1094
1095 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1096 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1097 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1098
1099 target->status = SRP_PORT_REDIRECT;
1100 } else {
1101 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1102 target->status = -ECONNRESET;
1103 }
1104 break;
1105
1106 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1107 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1108 target->status = -ECONNRESET;
1109 break;
1110
1111 case IB_CM_REJ_CONSUMER_DEFINED:
1112 opcode = *(u8 *) event->private_data;
1113 if (opcode == SRP_LOGIN_REJ) {
1114 struct srp_login_rej *rej = event->private_data;
1115 u32 reason = be32_to_cpu(rej->reason);
1116
1117 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1118 printk(KERN_WARNING PFX
1119 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1120 else
1121 printk(KERN_WARNING PFX
1122 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1123 } else
1124 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1125 " opcode 0x%02x\n", opcode);
1126 target->status = -ECONNRESET;
1127 break;
1128
1129 default:
1130 printk(KERN_WARNING " REJ reason 0x%x\n",
1131 event->param.rej_rcvd.reason);
1132 target->status = -ECONNRESET;
1133 }
1134}
1135
1136static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1137{
1138 struct srp_target_port *target = cm_id->context;
1139 struct ib_qp_attr *qp_attr = NULL;
1140 int attr_mask = 0;
1141 int comp = 0;
1142 int opcode = 0;
1143
1144 switch (event->event) {
1145 case IB_CM_REQ_ERROR:
1146 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1147 comp = 1;
1148 target->status = -ECONNRESET;
1149 break;
1150
1151 case IB_CM_REP_RECEIVED:
1152 comp = 1;
1153 opcode = *(u8 *) event->private_data;
1154
1155 if (opcode == SRP_LOGIN_RSP) {
1156 struct srp_login_rsp *rsp = event->private_data;
1157
1158 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1159 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1160
1161 target->scsi_host->can_queue = min(target->req_lim,
1162 target->scsi_host->can_queue);
1163 } else {
1164 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1165 target->status = -ECONNRESET;
1166 break;
1167 }
1168
1169 target->status = srp_alloc_iu_bufs(target);
1170 if (target->status)
1171 break;
1172
1173 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1174 if (!qp_attr) {
1175 target->status = -ENOMEM;
1176 break;
1177 }
1178
1179 qp_attr->qp_state = IB_QPS_RTR;
1180 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1181 if (target->status)
1182 break;
1183
1184 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1185 if (target->status)
1186 break;
1187
1188 target->status = srp_post_recv(target);
1189 if (target->status)
1190 break;
1191
1192 qp_attr->qp_state = IB_QPS_RTS;
1193 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1194 if (target->status)
1195 break;
1196
1197 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1198 if (target->status)
1199 break;
1200
1201 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1202 if (target->status)
1203 break;
1204
1205 break;
1206
1207 case IB_CM_REJ_RECEIVED:
1208 printk(KERN_DEBUG PFX "REJ received\n");
1209 comp = 1;
1210
1211 srp_cm_rej_handler(cm_id, event, target);
1212 break;
1213
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001214 case IB_CM_DREQ_RECEIVED:
1215 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1216 if (ib_send_cm_drep(cm_id, NULL, 0))
1217 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001218 break;
1219
1220 case IB_CM_TIMEWAIT_EXIT:
1221 printk(KERN_ERR PFX "connection closed\n");
1222
1223 comp = 1;
1224 target->status = 0;
1225 break;
1226
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001227 case IB_CM_MRA_RECEIVED:
1228 case IB_CM_DREQ_ERROR:
1229 case IB_CM_DREP_RECEIVED:
1230 break;
1231
Roland Dreieraef9ec32005-11-02 14:07:13 -08001232 default:
1233 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1234 break;
1235 }
1236
1237 if (comp)
1238 complete(&target->done);
1239
1240 kfree(qp_attr);
1241
1242 return 0;
1243}
1244
Roland Dreierd945e1d2006-05-09 10:50:28 -07001245static int srp_send_tsk_mgmt(struct srp_target_port *target,
1246 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001247{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001248 struct srp_iu *iu;
1249 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001250
1251 spin_lock_irq(target->scsi_host->host_lock);
1252
Roland Dreier1285b3a2006-03-03 15:47:25 -08001253 if (target->state == SRP_TARGET_DEAD ||
1254 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001255 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001256 goto out;
1257 }
1258
Roland Dreieraef9ec32005-11-02 14:07:13 -08001259 init_completion(&req->done);
1260
1261 iu = __srp_get_tx_iu(target);
1262 if (!iu)
1263 goto out;
1264
1265 tsk_mgmt = iu->buf;
1266 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1267
1268 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001269 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1270 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001271 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001272 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001273
1274 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1275 goto out;
1276
1277 req->tsk_mgmt = iu;
1278
1279 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001280
Roland Dreieraef9ec32005-11-02 14:07:13 -08001281 if (!wait_for_completion_timeout(&req->done,
1282 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001283 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001284
Roland Dreierd945e1d2006-05-09 10:50:28 -07001285 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001286
1287out:
1288 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001289 return -1;
1290}
1291
1292static int srp_find_req(struct srp_target_port *target,
1293 struct scsi_cmnd *scmnd,
1294 struct srp_request **req)
1295{
1296 if (scmnd->host_scribble == (void *) -1L)
1297 return -1;
1298
1299 *req = &target->req_ring[(long) scmnd->host_scribble];
1300
1301 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001302}
1303
1304static int srp_abort(struct scsi_cmnd *scmnd)
1305{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001306 struct srp_target_port *target = host_to_target(scmnd->device->host);
1307 struct srp_request *req;
1308 int ret = SUCCESS;
1309
Roland Dreieraef9ec32005-11-02 14:07:13 -08001310 printk(KERN_ERR "SRP abort called\n");
1311
Roland Dreierd945e1d2006-05-09 10:50:28 -07001312 if (srp_find_req(target, scmnd, &req))
1313 return FAILED;
1314 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1315 return FAILED;
1316
1317 spin_lock_irq(target->scsi_host->host_lock);
1318
1319 if (req->cmd_done) {
1320 srp_remove_req(target, req);
1321 scmnd->scsi_done(scmnd);
1322 } else if (!req->tsk_status) {
1323 srp_remove_req(target, req);
1324 scmnd->result = DID_ABORT << 16;
1325 } else
1326 ret = FAILED;
1327
1328 spin_unlock_irq(target->scsi_host->host_lock);
1329
1330 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001331}
1332
1333static int srp_reset_device(struct scsi_cmnd *scmnd)
1334{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001335 struct srp_target_port *target = host_to_target(scmnd->device->host);
1336 struct srp_request *req, *tmp;
1337
Roland Dreieraef9ec32005-11-02 14:07:13 -08001338 printk(KERN_ERR "SRP reset_device called\n");
1339
Roland Dreierd945e1d2006-05-09 10:50:28 -07001340 if (srp_find_req(target, scmnd, &req))
1341 return FAILED;
1342 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1343 return FAILED;
1344 if (req->tsk_status)
1345 return FAILED;
1346
1347 spin_lock_irq(target->scsi_host->host_lock);
1348
1349 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001350 if (req->scmnd->device == scmnd->device)
1351 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001352
1353 spin_unlock_irq(target->scsi_host->host_lock);
1354
1355 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001356}
1357
1358static int srp_reset_host(struct scsi_cmnd *scmnd)
1359{
1360 struct srp_target_port *target = host_to_target(scmnd->device->host);
1361 int ret = FAILED;
1362
1363 printk(KERN_ERR PFX "SRP reset_host called\n");
1364
1365 if (!srp_reconnect_target(target))
1366 ret = SUCCESS;
1367
1368 return ret;
1369}
1370
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001371static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1372{
1373 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1374
1375 if (target->state == SRP_TARGET_DEAD ||
1376 target->state == SRP_TARGET_REMOVED)
1377 return -ENODEV;
1378
1379 return sprintf(buf, "0x%016llx\n",
1380 (unsigned long long) be64_to_cpu(target->id_ext));
1381}
1382
1383static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1384{
1385 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1386
1387 if (target->state == SRP_TARGET_DEAD ||
1388 target->state == SRP_TARGET_REMOVED)
1389 return -ENODEV;
1390
1391 return sprintf(buf, "0x%016llx\n",
1392 (unsigned long long) be64_to_cpu(target->ioc_guid));
1393}
1394
1395static ssize_t show_service_id(struct class_device *cdev, char *buf)
1396{
1397 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1398
1399 if (target->state == SRP_TARGET_DEAD ||
1400 target->state == SRP_TARGET_REMOVED)
1401 return -ENODEV;
1402
1403 return sprintf(buf, "0x%016llx\n",
1404 (unsigned long long) be64_to_cpu(target->service_id));
1405}
1406
1407static ssize_t show_pkey(struct class_device *cdev, char *buf)
1408{
1409 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1410
1411 if (target->state == SRP_TARGET_DEAD ||
1412 target->state == SRP_TARGET_REMOVED)
1413 return -ENODEV;
1414
1415 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1416}
1417
1418static ssize_t show_dgid(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, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1427 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1428 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1429 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1430 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1431 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1432 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1433 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1434 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1435}
1436
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001437static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1438{
1439 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1440
1441 if (target->state == SRP_TARGET_DEAD ||
1442 target->state == SRP_TARGET_REMOVED)
1443 return -ENODEV;
1444
1445 return sprintf(buf, "%d\n", target->zero_req_lim);
1446}
1447
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001448static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1449static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1450static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1451static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1452static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001453static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001454
1455static struct class_device_attribute *srp_host_attrs[] = {
1456 &class_device_attr_id_ext,
1457 &class_device_attr_ioc_guid,
1458 &class_device_attr_service_id,
1459 &class_device_attr_pkey,
1460 &class_device_attr_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001461 &class_device_attr_zero_req_lim,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001462 NULL
1463};
1464
Roland Dreieraef9ec32005-11-02 14:07:13 -08001465static struct scsi_host_template srp_template = {
1466 .module = THIS_MODULE,
1467 .name = DRV_NAME,
1468 .info = srp_target_info,
1469 .queuecommand = srp_queuecommand,
1470 .eh_abort_handler = srp_abort,
1471 .eh_device_reset_handler = srp_reset_device,
1472 .eh_host_reset_handler = srp_reset_host,
1473 .can_queue = SRP_SQ_SIZE,
1474 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001475 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001476 .use_clustering = ENABLE_CLUSTERING,
1477 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001478};
1479
1480static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1481{
1482 sprintf(target->target_name, "SRP.T10:%016llX",
1483 (unsigned long long) be64_to_cpu(target->id_ext));
1484
Roland Dreierf5358a12006-06-17 20:37:29 -07001485 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001486 return -ENODEV;
1487
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001488 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001489 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001490 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001491
1492 target->state = SRP_TARGET_LIVE;
1493
Roland Dreieraef9ec32005-11-02 14:07:13 -08001494 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001495 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001496
1497 return 0;
1498}
1499
1500static void srp_release_class_dev(struct class_device *class_dev)
1501{
1502 struct srp_host *host =
1503 container_of(class_dev, struct srp_host, class_dev);
1504
1505 complete(&host->released);
1506}
1507
1508static struct class srp_class = {
1509 .name = "infiniband_srp",
1510 .release = srp_release_class_dev
1511};
1512
1513/*
1514 * Target ports are added by writing
1515 *
1516 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1517 * pkey=<P_Key>,service_id=<service ID>
1518 *
1519 * to the add_target sysfs attribute.
1520 */
1521enum {
1522 SRP_OPT_ERR = 0,
1523 SRP_OPT_ID_EXT = 1 << 0,
1524 SRP_OPT_IOC_GUID = 1 << 1,
1525 SRP_OPT_DGID = 1 << 2,
1526 SRP_OPT_PKEY = 1 << 3,
1527 SRP_OPT_SERVICE_ID = 1 << 4,
1528 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001529 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001530 SRP_OPT_IO_CLASS = 1 << 7,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001531 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1532 SRP_OPT_IOC_GUID |
1533 SRP_OPT_DGID |
1534 SRP_OPT_PKEY |
1535 SRP_OPT_SERVICE_ID),
1536};
1537
1538static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001539 { SRP_OPT_ID_EXT, "id_ext=%s" },
1540 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1541 { SRP_OPT_DGID, "dgid=%s" },
1542 { SRP_OPT_PKEY, "pkey=%x" },
1543 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1544 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1545 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001546 { SRP_OPT_IO_CLASS, "io_class=%x" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001547 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001548};
1549
1550static int srp_parse_options(const char *buf, struct srp_target_port *target)
1551{
1552 char *options, *sep_opt;
1553 char *p;
1554 char dgid[3];
1555 substring_t args[MAX_OPT_ARGS];
1556 int opt_mask = 0;
1557 int token;
1558 int ret = -EINVAL;
1559 int i;
1560
1561 options = kstrdup(buf, GFP_KERNEL);
1562 if (!options)
1563 return -ENOMEM;
1564
1565 sep_opt = options;
1566 while ((p = strsep(&sep_opt, ",")) != NULL) {
1567 if (!*p)
1568 continue;
1569
1570 token = match_token(p, srp_opt_tokens, args);
1571 opt_mask |= token;
1572
1573 switch (token) {
1574 case SRP_OPT_ID_EXT:
1575 p = match_strdup(args);
1576 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1577 kfree(p);
1578 break;
1579
1580 case SRP_OPT_IOC_GUID:
1581 p = match_strdup(args);
1582 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1583 kfree(p);
1584 break;
1585
1586 case SRP_OPT_DGID:
1587 p = match_strdup(args);
1588 if (strlen(p) != 32) {
1589 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001590 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001591 goto out;
1592 }
1593
1594 for (i = 0; i < 16; ++i) {
1595 strlcpy(dgid, p + i * 2, 3);
1596 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1597 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001598 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001599 break;
1600
1601 case SRP_OPT_PKEY:
1602 if (match_hex(args, &token)) {
1603 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1604 goto out;
1605 }
1606 target->path.pkey = cpu_to_be16(token);
1607 break;
1608
1609 case SRP_OPT_SERVICE_ID:
1610 p = match_strdup(args);
1611 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1612 kfree(p);
1613 break;
1614
1615 case SRP_OPT_MAX_SECT:
1616 if (match_int(args, &token)) {
1617 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1618 goto out;
1619 }
1620 target->scsi_host->max_sectors = token;
1621 break;
1622
Vu Pham52fb2b502006-06-17 20:37:31 -07001623 case SRP_OPT_MAX_CMD_PER_LUN:
1624 if (match_int(args, &token)) {
1625 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1626 goto out;
1627 }
1628 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1629 break;
1630
Ramachandra K0c0450db2006-06-17 20:37:38 -07001631 case SRP_OPT_IO_CLASS:
1632 if (match_hex(args, &token)) {
1633 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1634 goto out;
1635 }
1636 if (token != SRP_REV10_IB_IO_CLASS &&
1637 token != SRP_REV16A_IB_IO_CLASS) {
1638 printk(KERN_WARNING PFX "unknown IO class parameter value"
1639 " %x specified (use %x or %x).\n",
1640 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1641 goto out;
1642 }
1643 target->io_class = token;
1644 break;
1645
Roland Dreieraef9ec32005-11-02 14:07:13 -08001646 default:
1647 printk(KERN_WARNING PFX "unknown parameter or missing value "
1648 "'%s' in target creation request\n", p);
1649 goto out;
1650 }
1651 }
1652
1653 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1654 ret = 0;
1655 else
1656 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1657 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1658 !(srp_opt_tokens[i].token & opt_mask))
1659 printk(KERN_WARNING PFX "target creation request is "
1660 "missing parameter '%s'\n",
1661 srp_opt_tokens[i].pattern);
1662
1663out:
1664 kfree(options);
1665 return ret;
1666}
1667
1668static ssize_t srp_create_target(struct class_device *class_dev,
1669 const char *buf, size_t count)
1670{
1671 struct srp_host *host =
1672 container_of(class_dev, struct srp_host, class_dev);
1673 struct Scsi_Host *target_host;
1674 struct srp_target_port *target;
1675 int ret;
1676 int i;
1677
1678 target_host = scsi_host_alloc(&srp_template,
1679 sizeof (struct srp_target_port));
1680 if (!target_host)
1681 return -ENOMEM;
1682
Roland Dreier5f068992005-11-11 14:06:01 -08001683 target_host->max_lun = SRP_MAX_LUN;
1684
Roland Dreieraef9ec32005-11-02 14:07:13 -08001685 target = host_to_target(target_host);
1686 memset(target, 0, sizeof *target);
1687
Ramachandra K0c0450db2006-06-17 20:37:38 -07001688 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001689 target->scsi_host = target_host;
1690 target->srp_host = host;
1691
1692 INIT_WORK(&target->work, srp_reconnect_work, target);
1693
Roland Dreierd945e1d2006-05-09 10:50:28 -07001694 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001695 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001696 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1697 target->req_ring[i].index = i;
1698 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1699 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001700
1701 ret = srp_parse_options(buf, target);
1702 if (ret)
1703 goto err;
1704
Roland Dreierf5358a12006-06-17 20:37:29 -07001705 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001706
1707 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1708 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1709 (unsigned long long) be64_to_cpu(target->id_ext),
1710 (unsigned long long) be64_to_cpu(target->ioc_guid),
1711 be16_to_cpu(target->path.pkey),
1712 (unsigned long long) be64_to_cpu(target->service_id),
1713 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1714 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1715 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1716 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1717 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1718 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1719 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1720 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1721
1722 ret = srp_create_target_ib(target);
1723 if (ret)
1724 goto err;
1725
Roland Dreierf5358a12006-06-17 20:37:29 -07001726 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001727 if (IS_ERR(target->cm_id)) {
1728 ret = PTR_ERR(target->cm_id);
1729 goto err_free;
1730 }
1731
1732 ret = srp_connect_target(target);
1733 if (ret) {
1734 printk(KERN_ERR PFX "Connection failed\n");
1735 goto err_cm_id;
1736 }
1737
1738 ret = srp_add_target(host, target);
1739 if (ret)
1740 goto err_disconnect;
1741
1742 return count;
1743
1744err_disconnect:
1745 srp_disconnect_target(target);
1746
1747err_cm_id:
1748 ib_destroy_cm_id(target->cm_id);
1749
1750err_free:
1751 srp_free_target_ib(target);
1752
1753err:
1754 scsi_host_put(target_host);
1755
1756 return ret;
1757}
1758
1759static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1760
1761static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1762{
1763 struct srp_host *host =
1764 container_of(class_dev, struct srp_host, class_dev);
1765
Roland Dreierf5358a12006-06-17 20:37:29 -07001766 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001767}
1768
1769static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1770
1771static ssize_t show_port(struct class_device *class_dev, char *buf)
1772{
1773 struct srp_host *host =
1774 container_of(class_dev, struct srp_host, class_dev);
1775
1776 return sprintf(buf, "%d\n", host->port);
1777}
1778
1779static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1780
Roland Dreierf5358a12006-06-17 20:37:29 -07001781static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001782{
1783 struct srp_host *host;
1784
1785 host = kzalloc(sizeof *host, GFP_KERNEL);
1786 if (!host)
1787 return NULL;
1788
1789 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001790 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001791 init_completion(&host->released);
1792 host->dev = device;
1793 host->port = port;
1794
1795 host->initiator_port_id[7] = port;
Roland Dreierf5358a12006-06-17 20:37:29 -07001796 memcpy(host->initiator_port_id + 8, &device->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001797
1798 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001799 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001800 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001801 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001802
1803 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001804 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001805 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1806 goto err_class;
1807 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1808 goto err_class;
1809 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1810 goto err_class;
1811
1812 return host;
1813
1814err_class:
1815 class_device_unregister(&host->class_dev);
1816
Roland Dreierf5358a12006-06-17 20:37:29 -07001817free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001818 kfree(host);
1819
1820 return NULL;
1821}
1822
1823static void srp_add_one(struct ib_device *device)
1824{
Roland Dreierf5358a12006-06-17 20:37:29 -07001825 struct srp_device *srp_dev;
1826 struct ib_device_attr *dev_attr;
1827 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001828 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001829 int s, e, p;
1830
Roland Dreierf5358a12006-06-17 20:37:29 -07001831 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1832 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001833 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001834
Roland Dreierf5358a12006-06-17 20:37:29 -07001835 if (ib_query_device(device, dev_attr)) {
1836 printk(KERN_WARNING PFX "Query device failed for %s\n",
1837 device->name);
1838 goto free_attr;
1839 }
1840
1841 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1842 if (!srp_dev)
1843 goto free_attr;
1844
1845 /*
1846 * Use the smallest page size supported by the HCA, down to a
1847 * minimum of 512 bytes (which is the smallest sector that a
1848 * SCSI command will ever carry).
1849 */
1850 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1851 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
1852 srp_dev->fmr_page_mask = ~((unsigned long) srp_dev->fmr_page_size - 1);
1853
1854 INIT_LIST_HEAD(&srp_dev->dev_list);
1855
1856 srp_dev->dev = device;
1857 srp_dev->pd = ib_alloc_pd(device);
1858 if (IS_ERR(srp_dev->pd))
1859 goto free_dev;
1860
1861 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1862 IB_ACCESS_LOCAL_WRITE |
1863 IB_ACCESS_REMOTE_READ |
1864 IB_ACCESS_REMOTE_WRITE);
1865 if (IS_ERR(srp_dev->mr))
1866 goto err_pd;
1867
1868 memset(&fmr_param, 0, sizeof fmr_param);
1869 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1870 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1871 fmr_param.cache = 1;
1872 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1873 fmr_param.page_shift = srp_dev->fmr_page_shift;
1874 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1875 IB_ACCESS_REMOTE_WRITE |
1876 IB_ACCESS_REMOTE_READ);
1877
1878 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1879 if (IS_ERR(srp_dev->fmr_pool))
1880 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001881
1882 if (device->node_type == IB_NODE_SWITCH) {
1883 s = 0;
1884 e = 0;
1885 } else {
1886 s = 1;
1887 e = device->phys_port_cnt;
1888 }
1889
1890 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001891 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001892 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001893 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001894 }
1895
Roland Dreierf5358a12006-06-17 20:37:29 -07001896 ib_set_client_data(device, &srp_client, srp_dev);
1897
1898 goto free_attr;
1899
1900err_pd:
1901 ib_dealloc_pd(srp_dev->pd);
1902
1903free_dev:
1904 kfree(srp_dev);
1905
1906free_attr:
1907 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001908}
1909
1910static void srp_remove_one(struct ib_device *device)
1911{
Roland Dreierf5358a12006-06-17 20:37:29 -07001912 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001913 struct srp_host *host, *tmp_host;
1914 LIST_HEAD(target_list);
1915 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001916
Roland Dreierf5358a12006-06-17 20:37:29 -07001917 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001918
Roland Dreierf5358a12006-06-17 20:37:29 -07001919 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001920 class_device_unregister(&host->class_dev);
1921 /*
1922 * Wait for the sysfs entry to go away, so that no new
1923 * target ports can be created.
1924 */
1925 wait_for_completion(&host->released);
1926
1927 /*
1928 * Mark all target ports as removed, so we stop queueing
1929 * commands and don't try to reconnect.
1930 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001931 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07001932 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07001933 spin_lock_irq(target->scsi_host->host_lock);
1934 target->state = SRP_TARGET_REMOVED;
1935 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001936 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001937 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001938
1939 /*
1940 * Wait for any reconnection tasks that may have
1941 * started before we marked our target ports as
1942 * removed, and any target port removal tasks.
1943 */
1944 flush_scheduled_work();
1945
1946 list_for_each_entry_safe(target, tmp_target,
1947 &host->target_list, list) {
1948 scsi_remove_host(target->scsi_host);
1949 srp_disconnect_target(target);
1950 ib_destroy_cm_id(target->cm_id);
1951 srp_free_target_ib(target);
1952 scsi_host_put(target->scsi_host);
1953 }
1954
Roland Dreieraef9ec32005-11-02 14:07:13 -08001955 kfree(host);
1956 }
1957
Roland Dreierf5358a12006-06-17 20:37:29 -07001958 if (srp_dev->fmr_pool)
1959 ib_destroy_fmr_pool(srp_dev->fmr_pool);
1960 ib_dereg_mr(srp_dev->mr);
1961 ib_dealloc_pd(srp_dev->pd);
1962
1963 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001964}
1965
1966static int __init srp_init_module(void)
1967{
1968 int ret;
1969
Vu Pham74b0a152006-06-17 20:37:32 -07001970 srp_template.sg_tablesize = srp_sg_tablesize;
1971 srp_max_iu_len = (sizeof (struct srp_cmd) +
1972 sizeof (struct srp_indirect_buf) +
1973 srp_sg_tablesize * 16);
1974
Roland Dreieraef9ec32005-11-02 14:07:13 -08001975 ret = class_register(&srp_class);
1976 if (ret) {
1977 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
1978 return ret;
1979 }
1980
1981 ret = ib_register_client(&srp_client);
1982 if (ret) {
1983 printk(KERN_ERR PFX "couldn't register IB client\n");
1984 class_unregister(&srp_class);
1985 return ret;
1986 }
1987
1988 return 0;
1989}
1990
1991static void __exit srp_cleanup_module(void)
1992{
1993 ib_unregister_client(&srp_client);
1994 class_unregister(&srp_class);
1995}
1996
1997module_init(srp_init_module);
1998module_exit(srp_cleanup_module);