Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / drivers / infiniband / ulp / srp / ib_srp.c
blob39bf057fbc4306c46112d3ac5e9433578a21f4ba
1 /*
2 * Copyright (c) 2005 Cisco Systems. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
32 * $Id: ib_srp.c 3932 2005-11-01 17:19:29Z roland $
35 #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>
42 #include <linux/jiffies.h>
44 #include <asm/atomic.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_dbg.h>
49 #include <scsi/srp.h>
51 #include <rdma/ib_cache.h>
53 #include "ib_srp.h"
55 #define DRV_NAME "ib_srp"
56 #define PFX DRV_NAME ": "
57 #define DRV_VERSION "0.2"
58 #define DRV_RELDATE "November 1, 2005"
60 MODULE_AUTHOR("Roland Dreier");
61 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63 MODULE_LICENSE("Dual BSD/GPL");
65 static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
66 static int srp_max_iu_len;
68 module_param(srp_sg_tablesize, int, 0444);
69 MODULE_PARM_DESC(srp_sg_tablesize,
70 "Max number of gather/scatter entries per I/O (default is 12)");
72 static int topspin_workarounds = 1;
74 module_param(topspin_workarounds, int, 0444);
75 MODULE_PARM_DESC(topspin_workarounds,
76 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
78 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
80 static int mellanox_workarounds = 1;
82 module_param(mellanox_workarounds, int, 0444);
83 MODULE_PARM_DESC(mellanox_workarounds,
84 "Enable workarounds for Mellanox SRP target bugs if != 0");
86 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
88 static void srp_add_one(struct ib_device *device);
89 static void srp_remove_one(struct ib_device *device);
90 static void srp_completion(struct ib_cq *cq, void *target_ptr);
91 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
93 static struct ib_client srp_client = {
94 .name = "srp",
95 .add = srp_add_one,
96 .remove = srp_remove_one
99 static struct ib_sa_client srp_sa_client;
101 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
103 return (struct srp_target_port *) host->hostdata;
106 static const char *srp_target_info(struct Scsi_Host *host)
108 return host_to_target(host)->target_name;
111 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
112 gfp_t gfp_mask,
113 enum dma_data_direction direction)
115 struct srp_iu *iu;
117 iu = kmalloc(sizeof *iu, gfp_mask);
118 if (!iu)
119 goto out;
121 iu->buf = kzalloc(size, gfp_mask);
122 if (!iu->buf)
123 goto out_free_iu;
125 iu->dma = ib_dma_map_single(host->dev->dev, iu->buf, size, direction);
126 if (ib_dma_mapping_error(host->dev->dev, iu->dma))
127 goto out_free_buf;
129 iu->size = size;
130 iu->direction = direction;
132 return iu;
134 out_free_buf:
135 kfree(iu->buf);
136 out_free_iu:
137 kfree(iu);
138 out:
139 return NULL;
142 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
144 if (!iu)
145 return;
147 ib_dma_unmap_single(host->dev->dev, iu->dma, iu->size, iu->direction);
148 kfree(iu->buf);
149 kfree(iu);
152 static void srp_qp_event(struct ib_event *event, void *context)
154 printk(KERN_ERR PFX "QP event %d\n", event->event);
157 static int srp_init_qp(struct srp_target_port *target,
158 struct ib_qp *qp)
160 struct ib_qp_attr *attr;
161 int ret;
163 attr = kmalloc(sizeof *attr, GFP_KERNEL);
164 if (!attr)
165 return -ENOMEM;
167 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
168 target->srp_host->port,
169 be16_to_cpu(target->path.pkey),
170 &attr->pkey_index);
171 if (ret)
172 goto out;
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;
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);
185 out:
186 kfree(attr);
187 return ret;
190 static int srp_create_target_ib(struct srp_target_port *target)
192 struct ib_qp_init_attr *init_attr;
193 int ret;
195 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
196 if (!init_attr)
197 return -ENOMEM;
199 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
200 NULL, target, SRP_CQ_SIZE, 0);
201 if (IS_ERR(target->cq)) {
202 ret = PTR_ERR(target->cq);
203 goto out;
206 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
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;
218 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
219 if (IS_ERR(target->qp)) {
220 ret = PTR_ERR(target->qp);
221 ib_destroy_cq(target->cq);
222 goto out;
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;
232 out:
233 kfree(init_attr);
234 return ret;
237 static void srp_free_target_ib(struct srp_target_port *target)
239 int i;
241 ib_destroy_qp(target->qp);
242 ib_destroy_cq(target->cq);
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]);
250 static void srp_path_rec_completion(int status,
251 struct ib_sa_path_rec *pathrec,
252 void *target_ptr)
254 struct srp_target_port *target = target_ptr;
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);
264 static int srp_lookup_path(struct srp_target_port *target)
266 target->path.numb_path = 1;
268 init_completion(&target->done);
270 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
271 target->srp_host->dev->dev,
272 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;
285 wait_for_completion(&target->done);
287 if (target->status < 0)
288 printk(KERN_WARNING PFX "Path record query failed\n");
290 return target->status;
293 static int srp_send_req(struct srp_target_port *target)
295 struct {
296 struct ib_cm_req_param param;
297 struct srp_login_req priv;
298 } *req = NULL;
299 int status;
301 req = kzalloc(sizeof *req, GFP_KERNEL);
302 if (!req)
303 return -ENOMEM;
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;
314 get_random_bytes(&req->param.starting_psn, 4);
315 req->param.starting_psn &= 0xffffff;
318 * Pick some arbitrary defaults here; we could make these
319 * module parameters if anyone cared about setting them.
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;
328 req->priv.opcode = SRP_LOGIN_REQ;
329 req->priv.tag = 0;
330 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
331 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
332 SRP_BUF_FORMAT_INDIRECT);
334 * In the published SRP specification (draft rev. 16a), the
335 * 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.
339 * Targets conforming to these obsolete drafts can be
340 * recognized by the I/O Class they report.
342 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
343 memcpy(req->priv.initiator_port_id,
344 &target->path.sgid.global.interface_id, 8);
345 memcpy(req->priv.initiator_port_id + 8,
346 &target->initiator_ext, 8);
347 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,
351 &target->initiator_ext, 8);
352 memcpy(req->priv.initiator_port_id + 8,
353 &target->path.sgid.global.interface_id, 8);
354 memcpy(req->priv.target_port_id, &target->id_ext, 8);
355 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
359 * Topspin/Cisco SRP targets will reject our login unless we
360 * zero out the first 8 bytes of our initiator port ID and set
361 * the second 8 bytes to the local node GUID.
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);
368 memcpy(req->priv.initiator_port_id + 8,
369 &target->srp_host->dev->dev->node_guid, 8);
372 status = ib_send_cm_req(target->cm_id, &req->param);
374 kfree(req);
376 return status;
379 static void srp_disconnect_target(struct srp_target_port *target)
381 /* XXX should send SRP_I_LOGOUT request */
383 init_completion(&target->done);
384 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
385 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
386 return;
388 wait_for_completion(&target->done);
391 static void srp_remove_work(struct work_struct *work)
393 struct srp_target_port *target =
394 container_of(work, struct srp_target_port, work);
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);
399 return;
401 target->state = SRP_TARGET_REMOVED;
402 spin_unlock_irq(target->scsi_host->host_lock);
404 spin_lock(&target->srp_host->target_lock);
405 list_del(&target->list);
406 spin_unlock(&target->srp_host->target_lock);
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);
414 static int srp_connect_target(struct srp_target_port *target)
416 int ret;
418 ret = srp_lookup_path(target);
419 if (ret)
420 return ret;
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);
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.
435 switch (target->status) {
436 case 0:
437 return 0;
439 case SRP_PORT_REDIRECT:
440 ret = srp_lookup_path(target);
441 if (ret)
442 return ret;
443 break;
445 case SRP_DLID_REDIRECT:
446 break;
448 default:
449 return target->status;
454 static void srp_unmap_data(struct scsi_cmnd *scmnd,
455 struct srp_target_port *target,
456 struct srp_request *req)
458 struct scatterlist *scat;
459 int nents;
461 if (!scmnd->request_buffer ||
462 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
463 scmnd->sc_data_direction != DMA_FROM_DEVICE))
464 return;
466 if (req->fmr) {
467 ib_fmr_pool_unmap(req->fmr);
468 req->fmr = NULL;
472 * This handling of non-SG commands can be killed when the
473 * SCSI midlayer no longer generates non-SG commands.
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;
483 ib_dma_unmap_sg(target->srp_host->dev->dev, scat, nents,
484 scmnd->sc_data_direction);
487 static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
489 srp_unmap_data(req->scmnd, target, req);
490 list_move_tail(&req->list, &target->free_reqs);
493 static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
495 req->scmnd->result = DID_RESET << 16;
496 req->scmnd->scsi_done(req->scmnd);
497 srp_remove_req(target, req);
500 static int srp_reconnect_target(struct srp_target_port *target)
502 struct ib_cm_id *new_cm_id;
503 struct ib_qp_attr qp_attr;
504 struct srp_request *req, *tmp;
505 struct ib_wc wc;
506 int ret;
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;
513 target->state = SRP_TARGET_CONNECTING;
514 spin_unlock_irq(target->scsi_host->host_lock);
516 srp_disconnect_target(target);
518 * Now get a new local CM ID so that we avoid confusing the
519 * target in case things are really fouled up.
521 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
522 srp_cm_handler, target);
523 if (IS_ERR(new_cm_id)) {
524 ret = PTR_ERR(new_cm_id);
525 goto err;
527 ib_destroy_cm_id(target->cm_id);
528 target->cm_id = new_cm_id;
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;
535 ret = srp_init_qp(target, target->qp);
536 if (ret)
537 goto err;
539 while (ib_poll_cq(target->cq, 1, &wc) > 0)
540 ; /* nothing */
542 spin_lock_irq(target->scsi_host->host_lock);
543 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
544 srp_reset_req(target, req);
545 spin_unlock_irq(target->scsi_host->host_lock);
547 target->rx_head = 0;
548 target->tx_head = 0;
549 target->tx_tail = 0;
551 target->qp_in_error = 0;
552 ret = srp_connect_target(target);
553 if (ret)
554 goto err;
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);
564 return ret;
566 err:
567 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
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().
575 spin_lock_irq(target->scsi_host->host_lock);
576 if (target->state == SRP_TARGET_CONNECTING) {
577 target->state = SRP_TARGET_DEAD;
578 INIT_WORK(&target->work, srp_remove_work);
579 schedule_work(&target->work);
581 spin_unlock_irq(target->scsi_host->host_lock);
583 return ret;
586 static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
587 int sg_cnt, struct srp_request *req,
588 struct srp_direct_buf *buf)
590 u64 io_addr = 0;
591 u64 *dma_pages;
592 u32 len;
593 int page_cnt;
594 int i, j;
595 int ret;
596 struct srp_device *dev = target->srp_host->dev;
597 struct ib_device *ibdev = dev->dev;
599 if (!dev->fmr_pool)
600 return -ENODEV;
602 if ((ib_sg_dma_address(ibdev, &scat[0]) & ~dev->fmr_page_mask) &&
603 mellanox_workarounds && !memcmp(&target->ioc_guid, mellanox_oui, 3))
604 return -EINVAL;
606 len = page_cnt = 0;
607 for (i = 0; i < sg_cnt; ++i) {
608 unsigned int dma_len = ib_sg_dma_len(ibdev, &scat[i]);
610 if (ib_sg_dma_address(ibdev, &scat[i]) & ~dev->fmr_page_mask) {
611 if (i > 0)
612 return -EINVAL;
613 else
614 ++page_cnt;
616 if ((ib_sg_dma_address(ibdev, &scat[i]) + dma_len) &
617 ~dev->fmr_page_mask) {
618 if (i < sg_cnt - 1)
619 return -EINVAL;
620 else
621 ++page_cnt;
624 len += dma_len;
627 page_cnt += len >> dev->fmr_page_shift;
628 if (page_cnt > SRP_FMR_SIZE)
629 return -ENOMEM;
631 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
632 if (!dma_pages)
633 return -ENOMEM;
635 page_cnt = 0;
636 for (i = 0; i < sg_cnt; ++i) {
637 unsigned int dma_len = ib_sg_dma_len(ibdev, &scat[i]);
639 for (j = 0; j < dma_len; j += dev->fmr_page_size)
640 dma_pages[page_cnt++] =
641 (ib_sg_dma_address(ibdev, &scat[i]) &
642 dev->fmr_page_mask) + j;
645 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
646 dma_pages, page_cnt, io_addr);
647 if (IS_ERR(req->fmr)) {
648 ret = PTR_ERR(req->fmr);
649 req->fmr = NULL;
650 goto out;
653 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, &scat[0]) &
654 ~dev->fmr_page_mask);
655 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
656 buf->len = cpu_to_be32(len);
658 ret = 0;
660 out:
661 kfree(dma_pages);
663 return ret;
666 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
667 struct srp_request *req)
669 struct scatterlist *scat;
670 struct srp_cmd *cmd = req->cmd->buf;
671 int len, nents, count;
672 u8 fmt = SRP_DATA_DESC_DIRECT;
673 struct srp_device *dev;
674 struct ib_device *ibdev;
676 if (!scmnd->request_buffer || scmnd->sc_data_direction == DMA_NONE)
677 return sizeof (struct srp_cmd);
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;
687 * This handling of non-SG commands can be killed when the
688 * SCSI midlayer no longer generates non-SG commands.
690 if (likely(scmnd->use_sg)) {
691 nents = scmnd->use_sg;
692 scat = scmnd->request_buffer;
693 } else {
694 nents = 1;
695 scat = &req->fake_sg;
696 sg_init_one(scat, scmnd->request_buffer, scmnd->request_bufflen);
699 dev = target->srp_host->dev;
700 ibdev = dev->dev;
702 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
704 fmt = SRP_DATA_DESC_DIRECT;
705 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
707 if (count == 1) {
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.
714 struct srp_direct_buf *buf = (void *) cmd->add_data;
716 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));
719 } else if (srp_map_fmr(target, scat, count, req,
720 (void *) cmd->add_data)) {
722 * FMR mapping failed, and the scatterlist has more
723 * than one entry. Generate an indirect memory
724 * descriptor.
726 struct srp_indirect_buf *buf = (void *) cmd->add_data;
727 u32 datalen = 0;
728 int i;
730 fmt = SRP_DATA_DESC_INDIRECT;
731 len = sizeof (struct srp_cmd) +
732 sizeof (struct srp_indirect_buf) +
733 count * sizeof (struct srp_direct_buf);
735 for (i = 0; i < count; ++i) {
736 unsigned int dma_len = ib_sg_dma_len(ibdev, &scat[i]);
738 buf->desc_list[i].va =
739 cpu_to_be64(ib_sg_dma_address(ibdev, &scat[i]));
740 buf->desc_list[i].key =
741 cpu_to_be32(dev->mr->rkey);
742 buf->desc_list[i].len = cpu_to_be32(dma_len);
743 datalen += dma_len;
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;
751 buf->table_desc.va =
752 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
753 buf->table_desc.key =
754 cpu_to_be32(target->srp_host->dev->mr->rkey);
755 buf->table_desc.len =
756 cpu_to_be32(count * sizeof (struct srp_direct_buf));
758 buf->len = cpu_to_be32(datalen);
761 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
762 cmd->buf_fmt = fmt << 4;
763 else
764 cmd->buf_fmt = fmt;
766 return len;
769 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
771 struct srp_request *req;
772 struct scsi_cmnd *scmnd;
773 unsigned long flags;
774 s32 delta;
776 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
778 spin_lock_irqsave(target->scsi_host->host_lock, flags);
780 target->req_lim += delta;
782 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
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 {
791 scmnd = req->scmnd;
792 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;
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));
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);
809 if (!req->tsk_mgmt) {
810 scmnd->host_scribble = (void *) -1L;
811 scmnd->scsi_done(scmnd);
813 srp_remove_req(target, req);
814 } else
815 req->cmd_done = 1;
818 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
821 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
823 struct ib_device *dev;
824 struct srp_iu *iu;
825 u8 opcode;
827 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
829 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);
833 opcode = *(u8 *) iu->buf;
835 if (0) {
836 int i;
838 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
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");
848 if (wc->byte_len % 8)
849 printk("\n");
852 switch (opcode) {
853 case SRP_RSP:
854 srp_process_rsp(target, iu->buf);
855 break;
857 case SRP_T_LOGOUT:
858 /* XXX Handle target logout */
859 printk(KERN_WARNING PFX "Got target logout request\n");
860 break;
862 default:
863 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
864 break;
867 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
868 DMA_FROM_DEVICE);
871 static void srp_completion(struct ib_cq *cq, void *target_ptr)
873 struct srp_target_port *target = target_ptr;
874 struct ib_wc wc;
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);
882 target->qp_in_error = 1;
883 break;
886 if (wc.wr_id & SRP_OP_RECV)
887 srp_handle_recv(target, &wc);
888 else
889 ++target->tx_tail;
893 static int __srp_post_recv(struct srp_target_port *target)
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;
901 next = target->rx_head & (SRP_RQ_SIZE - 1);
902 wr.wr_id = next | SRP_OP_RECV;
903 iu = target->rx_ring[next];
905 list.addr = iu->dma;
906 list.length = iu->size;
907 list.lkey = target->srp_host->dev->mr->lkey;
909 wr.next = NULL;
910 wr.sg_list = &list;
911 wr.num_sge = 1;
913 ret = ib_post_recv(target->qp, &wr, &bad_wr);
914 if (!ret)
915 ++target->rx_head;
917 return ret;
920 static int srp_post_recv(struct srp_target_port *target)
922 unsigned long flags;
923 int ret;
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);
929 return ret;
933 * Must be called with target->scsi_host->host_lock held to protect
934 * req_lim and tx_head. Lock cannot be dropped between call here and
935 * call to __srp_post_send().
937 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
939 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
940 return NULL;
942 if (unlikely(target->req_lim < 1))
943 ++target->zero_req_lim;
945 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
949 * Must be called with target->scsi_host->host_lock held to protect
950 * req_lim and tx_head.
952 static int __srp_post_send(struct srp_target_port *target,
953 struct srp_iu *iu, int len)
955 struct ib_sge list;
956 struct ib_send_wr wr, *bad_wr;
957 int ret = 0;
959 list.addr = iu->dma;
960 list.length = len;
961 list.lkey = target->srp_host->dev->mr->lkey;
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;
970 ret = ib_post_send(target->qp, &wr, &bad_wr);
972 if (!ret) {
973 ++target->tx_head;
974 --target->req_lim;
977 return ret;
980 static int srp_queuecommand(struct scsi_cmnd *scmnd,
981 void (*done)(struct scsi_cmnd *))
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;
987 struct ib_device *dev;
988 int len;
990 if (target->state == SRP_TARGET_CONNECTING)
991 goto err;
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;
1000 iu = __srp_get_tx_iu(target);
1001 if (!iu)
1002 goto err;
1004 dev = target->srp_host->dev->dev;
1005 ib_dma_sync_single_for_cpu(dev, iu->dma, srp_max_iu_len,
1006 DMA_TO_DEVICE);
1008 req = list_entry(target->free_reqs.next, struct srp_request, list);
1010 scmnd->scsi_done = done;
1011 scmnd->result = 0;
1012 scmnd->host_scribble = (void *) (long) req->index;
1014 cmd = iu->buf;
1015 memset(cmd, 0, sizeof *cmd);
1017 cmd->opcode = SRP_CMD;
1018 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
1019 cmd->tag = req->index;
1020 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1022 req->scmnd = scmnd;
1023 req->cmd = iu;
1024 req->cmd_done = 0;
1025 req->tsk_mgmt = NULL;
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;
1033 if (__srp_post_recv(target)) {
1034 printk(KERN_ERR PFX "Recv failed\n");
1035 goto err_unmap;
1038 ib_dma_sync_single_for_device(dev, iu->dma, srp_max_iu_len,
1039 DMA_TO_DEVICE);
1041 if (__srp_post_send(target, iu, len)) {
1042 printk(KERN_ERR PFX "Send failed\n");
1043 goto err_unmap;
1046 list_move_tail(&req->list, &target->req_queue);
1048 return 0;
1050 err_unmap:
1051 srp_unmap_data(scmnd, target, req);
1053 err:
1054 return SCSI_MLQUEUE_HOST_BUSY;
1057 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1059 int i;
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;
1069 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1070 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1071 srp_max_iu_len,
1072 GFP_KERNEL, DMA_TO_DEVICE);
1073 if (!target->tx_ring[i])
1074 goto err;
1077 return 0;
1079 err:
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;
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;
1090 return -ENOMEM;
1093 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1094 struct ib_cm_event *event,
1095 struct srp_target_port *target)
1097 struct ib_class_port_info *cpi;
1098 int opcode;
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);
1108 target->status = target->path.dlid ?
1109 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1110 break;
1112 case IB_CM_REJ_PORT_REDIRECT:
1113 if (topspin_workarounds &&
1114 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1116 * Topspin/Cisco SRP gateways incorrectly send
1117 * reject reason code 25 when they mean 24
1118 * (port redirect).
1120 memcpy(target->path.dgid.raw,
1121 event->param.rej_rcvd.ari, 16);
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));
1127 target->status = SRP_PORT_REDIRECT;
1128 } else {
1129 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1130 target->status = -ECONNRESET;
1132 break;
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;
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);
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;
1157 default:
1158 printk(KERN_WARNING " REJ reason 0x%x\n",
1159 event->param.rej_rcvd.reason);
1160 target->status = -ECONNRESET;
1164 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
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;
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;
1179 case IB_CM_REP_RECEIVED:
1180 comp = 1;
1181 opcode = *(u8 *) event->private_data;
1183 if (opcode == SRP_LOGIN_RSP) {
1184 struct srp_login_rsp *rsp = event->private_data;
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);
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;
1197 if (!target->rx_ring[0]) {
1198 target->status = srp_alloc_iu_bufs(target);
1199 if (target->status)
1200 break;
1203 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1204 if (!qp_attr) {
1205 target->status = -ENOMEM;
1206 break;
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;
1214 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1215 if (target->status)
1216 break;
1218 target->status = srp_post_recv(target);
1219 if (target->status)
1220 break;
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;
1227 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1228 if (target->status)
1229 break;
1231 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1232 if (target->status)
1233 break;
1235 break;
1237 case IB_CM_REJ_RECEIVED:
1238 printk(KERN_DEBUG PFX "REJ received\n");
1239 comp = 1;
1241 srp_cm_rej_handler(cm_id, event, target);
1242 break;
1244 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");
1248 break;
1250 case IB_CM_TIMEWAIT_EXIT:
1251 printk(KERN_ERR PFX "connection closed\n");
1253 comp = 1;
1254 target->status = 0;
1255 break;
1257 case IB_CM_MRA_RECEIVED:
1258 case IB_CM_DREQ_ERROR:
1259 case IB_CM_DREP_RECEIVED:
1260 break;
1262 default:
1263 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1264 break;
1267 if (comp)
1268 complete(&target->done);
1270 kfree(qp_attr);
1272 return 0;
1275 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1276 struct srp_request *req, u8 func)
1278 struct srp_iu *iu;
1279 struct srp_tsk_mgmt *tsk_mgmt;
1281 spin_lock_irq(target->scsi_host->host_lock);
1283 if (target->state == SRP_TARGET_DEAD ||
1284 target->state == SRP_TARGET_REMOVED) {
1285 req->scmnd->result = DID_BAD_TARGET << 16;
1286 goto out;
1289 init_completion(&req->done);
1291 iu = __srp_get_tx_iu(target);
1292 if (!iu)
1293 goto out;
1295 tsk_mgmt = iu->buf;
1296 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1298 tsk_mgmt->opcode = SRP_TSK_MGMT;
1299 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1300 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
1301 tsk_mgmt->tsk_mgmt_func = func;
1302 tsk_mgmt->task_tag = req->index;
1304 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1305 goto out;
1307 req->tsk_mgmt = iu;
1309 spin_unlock_irq(target->scsi_host->host_lock);
1311 if (!wait_for_completion_timeout(&req->done,
1312 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1313 return -1;
1315 return 0;
1317 out:
1318 spin_unlock_irq(target->scsi_host->host_lock);
1319 return -1;
1322 static int srp_find_req(struct srp_target_port *target,
1323 struct scsi_cmnd *scmnd,
1324 struct srp_request **req)
1326 if (scmnd->host_scribble == (void *) -1L)
1327 return -1;
1329 *req = &target->req_ring[(long) scmnd->host_scribble];
1331 return 0;
1334 static int srp_abort(struct scsi_cmnd *scmnd)
1336 struct srp_target_port *target = host_to_target(scmnd->device->host);
1337 struct srp_request *req;
1338 int ret = SUCCESS;
1340 printk(KERN_ERR "SRP abort called\n");
1342 if (target->qp_in_error)
1343 return FAILED;
1344 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;
1349 spin_lock_irq(target->scsi_host->host_lock);
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;
1360 spin_unlock_irq(target->scsi_host->host_lock);
1362 return ret;
1365 static int srp_reset_device(struct scsi_cmnd *scmnd)
1367 struct srp_target_port *target = host_to_target(scmnd->device->host);
1368 struct srp_request *req, *tmp;
1370 printk(KERN_ERR "SRP reset_device called\n");
1372 if (target->qp_in_error)
1373 return FAILED;
1374 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;
1381 spin_lock_irq(target->scsi_host->host_lock);
1383 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1384 if (req->scmnd->device == scmnd->device)
1385 srp_reset_req(target, req);
1387 spin_unlock_irq(target->scsi_host->host_lock);
1389 return SUCCESS;
1392 static int srp_reset_host(struct scsi_cmnd *scmnd)
1394 struct srp_target_port *target = host_to_target(scmnd->device->host);
1395 int ret = FAILED;
1397 printk(KERN_ERR PFX "SRP reset_host called\n");
1399 if (!srp_reconnect_target(target))
1400 ret = SUCCESS;
1402 return ret;
1405 static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1407 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1409 if (target->state == SRP_TARGET_DEAD ||
1410 target->state == SRP_TARGET_REMOVED)
1411 return -ENODEV;
1413 return sprintf(buf, "0x%016llx\n",
1414 (unsigned long long) be64_to_cpu(target->id_ext));
1417 static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1419 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1421 if (target->state == SRP_TARGET_DEAD ||
1422 target->state == SRP_TARGET_REMOVED)
1423 return -ENODEV;
1425 return sprintf(buf, "0x%016llx\n",
1426 (unsigned long long) be64_to_cpu(target->ioc_guid));
1429 static ssize_t show_service_id(struct class_device *cdev, char *buf)
1431 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1433 if (target->state == SRP_TARGET_DEAD ||
1434 target->state == SRP_TARGET_REMOVED)
1435 return -ENODEV;
1437 return sprintf(buf, "0x%016llx\n",
1438 (unsigned long long) be64_to_cpu(target->service_id));
1441 static ssize_t show_pkey(struct class_device *cdev, char *buf)
1443 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1445 if (target->state == SRP_TARGET_DEAD ||
1446 target->state == SRP_TARGET_REMOVED)
1447 return -ENODEV;
1449 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1452 static ssize_t show_dgid(struct class_device *cdev, char *buf)
1454 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1456 if (target->state == SRP_TARGET_DEAD ||
1457 target->state == SRP_TARGET_REMOVED)
1458 return -ENODEV;
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]));
1471 static ssize_t show_orig_dgid(struct class_device *cdev, char *buf)
1473 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1475 if (target->state == SRP_TARGET_DEAD ||
1476 target->state == SRP_TARGET_REMOVED)
1477 return -ENODEV;
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]));
1490 static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1492 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1494 if (target->state == SRP_TARGET_DEAD ||
1495 target->state == SRP_TARGET_REMOVED)
1496 return -ENODEV;
1498 return sprintf(buf, "%d\n", target->zero_req_lim);
1501 static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1503 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1505 return sprintf(buf, "%d\n", target->srp_host->port);
1508 static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1510 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1512 return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1515 static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1516 static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1517 static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1518 static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1519 static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1520 static CLASS_DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1521 static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1522 static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1523 static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
1525 static 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,
1531 &class_device_attr_orig_dgid,
1532 &class_device_attr_zero_req_lim,
1533 &class_device_attr_local_ib_port,
1534 &class_device_attr_local_ib_device,
1535 NULL
1538 static struct scsi_host_template srp_template = {
1539 .module = THIS_MODULE,
1540 .name = "InfiniBand SRP initiator",
1541 .proc_name = DRV_NAME,
1542 .info = srp_target_info,
1543 .queuecommand = srp_queuecommand,
1544 .eh_abort_handler = srp_abort,
1545 .eh_device_reset_handler = srp_reset_device,
1546 .eh_host_reset_handler = srp_reset_host,
1547 .can_queue = SRP_SQ_SIZE,
1548 .this_id = -1,
1549 .cmd_per_lun = SRP_SQ_SIZE,
1550 .use_clustering = ENABLE_CLUSTERING,
1551 .shost_attrs = srp_host_attrs
1554 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1556 sprintf(target->target_name, "SRP.T10:%016llX",
1557 (unsigned long long) be64_to_cpu(target->id_ext));
1559 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
1560 return -ENODEV;
1562 spin_lock(&host->target_lock);
1563 list_add_tail(&target->list, &host->target_list);
1564 spin_unlock(&host->target_lock);
1566 target->state = SRP_TARGET_LIVE;
1568 scsi_scan_target(&target->scsi_host->shost_gendev,
1569 0, target->scsi_id, SCAN_WILD_CARD, 0);
1571 return 0;
1574 static void srp_release_class_dev(struct class_device *class_dev)
1576 struct srp_host *host =
1577 container_of(class_dev, struct srp_host, class_dev);
1579 complete(&host->released);
1582 static struct class srp_class = {
1583 .name = "infiniband_srp",
1584 .release = srp_release_class_dev
1588 * Target ports are added by writing
1590 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1591 * pkey=<P_Key>,service_id=<service ID>
1593 * to the add_target sysfs attribute.
1595 enum {
1596 SRP_OPT_ERR = 0,
1597 SRP_OPT_ID_EXT = 1 << 0,
1598 SRP_OPT_IOC_GUID = 1 << 1,
1599 SRP_OPT_DGID = 1 << 2,
1600 SRP_OPT_PKEY = 1 << 3,
1601 SRP_OPT_SERVICE_ID = 1 << 4,
1602 SRP_OPT_MAX_SECT = 1 << 5,
1603 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
1604 SRP_OPT_IO_CLASS = 1 << 7,
1605 SRP_OPT_INITIATOR_EXT = 1 << 8,
1606 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1607 SRP_OPT_IOC_GUID |
1608 SRP_OPT_DGID |
1609 SRP_OPT_PKEY |
1610 SRP_OPT_SERVICE_ID),
1613 static match_table_t srp_opt_tokens = {
1614 { SRP_OPT_ID_EXT, "id_ext=%s" },
1615 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1616 { SRP_OPT_DGID, "dgid=%s" },
1617 { SRP_OPT_PKEY, "pkey=%x" },
1618 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1619 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1620 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
1621 { SRP_OPT_IO_CLASS, "io_class=%x" },
1622 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
1623 { SRP_OPT_ERR, NULL }
1626 static int srp_parse_options(const char *buf, struct srp_target_port *target)
1628 char *options, *sep_opt;
1629 char *p;
1630 char dgid[3];
1631 substring_t args[MAX_OPT_ARGS];
1632 int opt_mask = 0;
1633 int token;
1634 int ret = -EINVAL;
1635 int i;
1637 options = kstrdup(buf, GFP_KERNEL);
1638 if (!options)
1639 return -ENOMEM;
1641 sep_opt = options;
1642 while ((p = strsep(&sep_opt, ",")) != NULL) {
1643 if (!*p)
1644 continue;
1646 token = match_token(p, srp_opt_tokens, args);
1647 opt_mask |= token;
1649 switch (token) {
1650 case SRP_OPT_ID_EXT:
1651 p = match_strdup(args);
1652 if (!p) {
1653 ret = -ENOMEM;
1654 goto out;
1656 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1657 kfree(p);
1658 break;
1660 case SRP_OPT_IOC_GUID:
1661 p = match_strdup(args);
1662 if (!p) {
1663 ret = -ENOMEM;
1664 goto out;
1666 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1667 kfree(p);
1668 break;
1670 case SRP_OPT_DGID:
1671 p = match_strdup(args);
1672 if (!p) {
1673 ret = -ENOMEM;
1674 goto out;
1676 if (strlen(p) != 32) {
1677 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
1678 kfree(p);
1679 goto out;
1682 for (i = 0; i < 16; ++i) {
1683 strlcpy(dgid, p + i * 2, 3);
1684 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1686 kfree(p);
1687 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
1688 break;
1690 case SRP_OPT_PKEY:
1691 if (match_hex(args, &token)) {
1692 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1693 goto out;
1695 target->path.pkey = cpu_to_be16(token);
1696 break;
1698 case SRP_OPT_SERVICE_ID:
1699 p = match_strdup(args);
1700 if (!p) {
1701 ret = -ENOMEM;
1702 goto out;
1704 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1705 kfree(p);
1706 break;
1708 case SRP_OPT_MAX_SECT:
1709 if (match_int(args, &token)) {
1710 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1711 goto out;
1713 target->scsi_host->max_sectors = token;
1714 break;
1716 case SRP_OPT_MAX_CMD_PER_LUN:
1717 if (match_int(args, &token)) {
1718 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1719 goto out;
1721 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1722 break;
1724 case SRP_OPT_IO_CLASS:
1725 if (match_hex(args, &token)) {
1726 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1727 goto out;
1729 if (token != SRP_REV10_IB_IO_CLASS &&
1730 token != SRP_REV16A_IB_IO_CLASS) {
1731 printk(KERN_WARNING PFX "unknown IO class parameter value"
1732 " %x specified (use %x or %x).\n",
1733 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1734 goto out;
1736 target->io_class = token;
1737 break;
1739 case SRP_OPT_INITIATOR_EXT:
1740 p = match_strdup(args);
1741 if (!p) {
1742 ret = -ENOMEM;
1743 goto out;
1745 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1746 kfree(p);
1747 break;
1749 default:
1750 printk(KERN_WARNING PFX "unknown parameter or missing value "
1751 "'%s' in target creation request\n", p);
1752 goto out;
1756 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1757 ret = 0;
1758 else
1759 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1760 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1761 !(srp_opt_tokens[i].token & opt_mask))
1762 printk(KERN_WARNING PFX "target creation request is "
1763 "missing parameter '%s'\n",
1764 srp_opt_tokens[i].pattern);
1766 out:
1767 kfree(options);
1768 return ret;
1771 static ssize_t srp_create_target(struct class_device *class_dev,
1772 const char *buf, size_t count)
1774 struct srp_host *host =
1775 container_of(class_dev, struct srp_host, class_dev);
1776 struct Scsi_Host *target_host;
1777 struct srp_target_port *target;
1778 int ret;
1779 int i;
1781 target_host = scsi_host_alloc(&srp_template,
1782 sizeof (struct srp_target_port));
1783 if (!target_host)
1784 return -ENOMEM;
1786 target_host->max_lun = SRP_MAX_LUN;
1787 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
1789 target = host_to_target(target_host);
1791 target->io_class = SRP_REV16A_IB_IO_CLASS;
1792 target->scsi_host = target_host;
1793 target->srp_host = host;
1795 INIT_LIST_HEAD(&target->free_reqs);
1796 INIT_LIST_HEAD(&target->req_queue);
1797 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1798 target->req_ring[i].index = i;
1799 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1802 ret = srp_parse_options(buf, target);
1803 if (ret)
1804 goto err;
1806 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
1808 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1809 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1810 (unsigned long long) be64_to_cpu(target->id_ext),
1811 (unsigned long long) be64_to_cpu(target->ioc_guid),
1812 be16_to_cpu(target->path.pkey),
1813 (unsigned long long) be64_to_cpu(target->service_id),
1814 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1815 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1816 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1817 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1818 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1819 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1820 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1821 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1823 ret = srp_create_target_ib(target);
1824 if (ret)
1825 goto err;
1827 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
1828 if (IS_ERR(target->cm_id)) {
1829 ret = PTR_ERR(target->cm_id);
1830 goto err_free;
1833 target->qp_in_error = 0;
1834 ret = srp_connect_target(target);
1835 if (ret) {
1836 printk(KERN_ERR PFX "Connection failed\n");
1837 goto err_cm_id;
1840 ret = srp_add_target(host, target);
1841 if (ret)
1842 goto err_disconnect;
1844 return count;
1846 err_disconnect:
1847 srp_disconnect_target(target);
1849 err_cm_id:
1850 ib_destroy_cm_id(target->cm_id);
1852 err_free:
1853 srp_free_target_ib(target);
1855 err:
1856 scsi_host_put(target_host);
1858 return ret;
1861 static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1863 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1865 struct srp_host *host =
1866 container_of(class_dev, struct srp_host, class_dev);
1868 return sprintf(buf, "%s\n", host->dev->dev->name);
1871 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1873 static ssize_t show_port(struct class_device *class_dev, char *buf)
1875 struct srp_host *host =
1876 container_of(class_dev, struct srp_host, class_dev);
1878 return sprintf(buf, "%d\n", host->port);
1881 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1883 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
1885 struct srp_host *host;
1887 host = kzalloc(sizeof *host, GFP_KERNEL);
1888 if (!host)
1889 return NULL;
1891 INIT_LIST_HEAD(&host->target_list);
1892 spin_lock_init(&host->target_lock);
1893 init_completion(&host->released);
1894 host->dev = device;
1895 host->port = port;
1897 host->class_dev.class = &srp_class;
1898 host->class_dev.dev = device->dev->dma_device;
1899 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
1900 device->dev->name, port);
1902 if (class_device_register(&host->class_dev))
1903 goto free_host;
1904 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1905 goto err_class;
1906 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1907 goto err_class;
1908 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1909 goto err_class;
1911 return host;
1913 err_class:
1914 class_device_unregister(&host->class_dev);
1916 free_host:
1917 kfree(host);
1919 return NULL;
1922 static void srp_add_one(struct ib_device *device)
1924 struct srp_device *srp_dev;
1925 struct ib_device_attr *dev_attr;
1926 struct ib_fmr_pool_param fmr_param;
1927 struct srp_host *host;
1928 int s, e, p;
1930 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1931 if (!dev_attr)
1932 return;
1934 if (ib_query_device(device, dev_attr)) {
1935 printk(KERN_WARNING PFX "Query device failed for %s\n",
1936 device->name);
1937 goto free_attr;
1940 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1941 if (!srp_dev)
1942 goto free_attr;
1945 * Use the smallest page size supported by the HCA, down to a
1946 * minimum of 512 bytes (which is the smallest sector that a
1947 * SCSI command will ever carry).
1949 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1950 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
1951 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
1953 INIT_LIST_HEAD(&srp_dev->dev_list);
1955 srp_dev->dev = device;
1956 srp_dev->pd = ib_alloc_pd(device);
1957 if (IS_ERR(srp_dev->pd))
1958 goto free_dev;
1960 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1961 IB_ACCESS_LOCAL_WRITE |
1962 IB_ACCESS_REMOTE_READ |
1963 IB_ACCESS_REMOTE_WRITE);
1964 if (IS_ERR(srp_dev->mr))
1965 goto err_pd;
1967 memset(&fmr_param, 0, sizeof fmr_param);
1968 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1969 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1970 fmr_param.cache = 1;
1971 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1972 fmr_param.page_shift = srp_dev->fmr_page_shift;
1973 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1974 IB_ACCESS_REMOTE_WRITE |
1975 IB_ACCESS_REMOTE_READ);
1977 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1978 if (IS_ERR(srp_dev->fmr_pool))
1979 srp_dev->fmr_pool = NULL;
1981 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1982 s = 0;
1983 e = 0;
1984 } else {
1985 s = 1;
1986 e = device->phys_port_cnt;
1989 for (p = s; p <= e; ++p) {
1990 host = srp_add_port(srp_dev, p);
1991 if (host)
1992 list_add_tail(&host->list, &srp_dev->dev_list);
1995 ib_set_client_data(device, &srp_client, srp_dev);
1997 goto free_attr;
1999 err_pd:
2000 ib_dealloc_pd(srp_dev->pd);
2002 free_dev:
2003 kfree(srp_dev);
2005 free_attr:
2006 kfree(dev_attr);
2009 static void srp_remove_one(struct ib_device *device)
2011 struct srp_device *srp_dev;
2012 struct srp_host *host, *tmp_host;
2013 LIST_HEAD(target_list);
2014 struct srp_target_port *target, *tmp_target;
2016 srp_dev = ib_get_client_data(device, &srp_client);
2018 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2019 class_device_unregister(&host->class_dev);
2021 * Wait for the sysfs entry to go away, so that no new
2022 * target ports can be created.
2024 wait_for_completion(&host->released);
2027 * Mark all target ports as removed, so we stop queueing
2028 * commands and don't try to reconnect.
2030 spin_lock(&host->target_lock);
2031 list_for_each_entry(target, &host->target_list, list) {
2032 spin_lock_irq(target->scsi_host->host_lock);
2033 target->state = SRP_TARGET_REMOVED;
2034 spin_unlock_irq(target->scsi_host->host_lock);
2036 spin_unlock(&host->target_lock);
2039 * Wait for any reconnection tasks that may have
2040 * started before we marked our target ports as
2041 * removed, and any target port removal tasks.
2043 flush_scheduled_work();
2045 list_for_each_entry_safe(target, tmp_target,
2046 &host->target_list, list) {
2047 scsi_remove_host(target->scsi_host);
2048 srp_disconnect_target(target);
2049 ib_destroy_cm_id(target->cm_id);
2050 srp_free_target_ib(target);
2051 scsi_host_put(target->scsi_host);
2054 kfree(host);
2057 if (srp_dev->fmr_pool)
2058 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2059 ib_dereg_mr(srp_dev->mr);
2060 ib_dealloc_pd(srp_dev->pd);
2062 kfree(srp_dev);
2065 static int __init srp_init_module(void)
2067 int ret;
2069 srp_template.sg_tablesize = srp_sg_tablesize;
2070 srp_max_iu_len = (sizeof (struct srp_cmd) +
2071 sizeof (struct srp_indirect_buf) +
2072 srp_sg_tablesize * 16);
2074 ret = class_register(&srp_class);
2075 if (ret) {
2076 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2077 return ret;
2080 ib_sa_register_client(&srp_sa_client);
2082 ret = ib_register_client(&srp_client);
2083 if (ret) {
2084 printk(KERN_ERR PFX "couldn't register IB client\n");
2085 ib_sa_unregister_client(&srp_sa_client);
2086 class_unregister(&srp_class);
2087 return ret;
2090 return 0;
2093 static void __exit srp_cleanup_module(void)
2095 ib_unregister_client(&srp_client);
2096 ib_sa_unregister_client(&srp_sa_client);
2097 class_unregister(&srp_class);
2100 module_init(srp_init_module);
2101 module_exit(srp_cleanup_module);