4b09147f438f5d44a93d46eba3ccf6de6ed345bc
[linux-3.10.git] / drivers / infiniband / ulp / srp / ib_srp.c
1 /*
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
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>
43
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
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");
64
65 static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
66 static int srp_max_iu_len;
67
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)");
71
72 static int topspin_workarounds = 1;
73
74 module_param(topspin_workarounds, int, 0444);
75 MODULE_PARM_DESC(topspin_workarounds,
76                  "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
77
78 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
79
80 static int mellanox_workarounds = 1;
81
82 module_param(mellanox_workarounds, int, 0444);
83 MODULE_PARM_DESC(mellanox_workarounds,
84                  "Enable workarounds for Mellanox SRP target bugs if != 0");
85
86 static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
87
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);
92
93 static struct ib_client srp_client = {
94         .name   = "srp",
95         .add    = srp_add_one,
96         .remove = srp_remove_one
97 };
98
99 static struct ib_sa_client srp_sa_client;
100
101 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
102 {
103         return (struct srp_target_port *) host->hostdata;
104 }
105
106 static const char *srp_target_info(struct Scsi_Host *host)
107 {
108         return host_to_target(host)->target_name;
109 }
110
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)
114 {
115         struct srp_iu *iu;
116
117         iu = kmalloc(sizeof *iu, gfp_mask);
118         if (!iu)
119                 goto out;
120
121         iu->buf = kzalloc(size, gfp_mask);
122         if (!iu->buf)
123                 goto out_free_iu;
124
125         iu->dma = dma_map_single(host->dev->dev->dma_device,
126                                  iu->buf, size, direction);
127         if (dma_mapping_error(iu->dma))
128                 goto out_free_buf;
129
130         iu->size      = size;
131         iu->direction = direction;
132
133         return iu;
134
135 out_free_buf:
136         kfree(iu->buf);
137 out_free_iu:
138         kfree(iu);
139 out:
140         return NULL;
141 }
142
143 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
144 {
145         if (!iu)
146                 return;
147
148         dma_unmap_single(host->dev->dev->dma_device,
149                          iu->dma, iu->size, iu->direction);
150         kfree(iu->buf);
151         kfree(iu);
152 }
153
154 static void srp_qp_event(struct ib_event *event, void *context)
155 {
156         printk(KERN_ERR PFX "QP event %d\n", event->event);
157 }
158
159 static int srp_init_qp(struct srp_target_port *target,
160                        struct ib_qp *qp)
161 {
162         struct ib_qp_attr *attr;
163         int ret;
164
165         attr = kmalloc(sizeof *attr, GFP_KERNEL);
166         if (!attr)
167                 return -ENOMEM;
168
169         ret = ib_find_cached_pkey(target->srp_host->dev->dev,
170                                   target->srp_host->port,
171                                   be16_to_cpu(target->path.pkey),
172                                   &attr->pkey_index);
173         if (ret)
174                 goto out;
175
176         attr->qp_state        = IB_QPS_INIT;
177         attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
178                                     IB_ACCESS_REMOTE_WRITE);
179         attr->port_num        = target->srp_host->port;
180
181         ret = ib_modify_qp(qp, attr,
182                            IB_QP_STATE          |
183                            IB_QP_PKEY_INDEX     |
184                            IB_QP_ACCESS_FLAGS   |
185                            IB_QP_PORT);
186
187 out:
188         kfree(attr);
189         return ret;
190 }
191
192 static int srp_create_target_ib(struct srp_target_port *target)
193 {
194         struct ib_qp_init_attr *init_attr;
195         int ret;
196
197         init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
198         if (!init_attr)
199                 return -ENOMEM;
200
201         target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
202                                   NULL, target, SRP_CQ_SIZE);
203         if (IS_ERR(target->cq)) {
204                 ret = PTR_ERR(target->cq);
205                 goto out;
206         }
207
208         ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
209
210         init_attr->event_handler       = srp_qp_event;
211         init_attr->cap.max_send_wr     = SRP_SQ_SIZE;
212         init_attr->cap.max_recv_wr     = SRP_RQ_SIZE;
213         init_attr->cap.max_recv_sge    = 1;
214         init_attr->cap.max_send_sge    = 1;
215         init_attr->sq_sig_type         = IB_SIGNAL_ALL_WR;
216         init_attr->qp_type             = IB_QPT_RC;
217         init_attr->send_cq             = target->cq;
218         init_attr->recv_cq             = target->cq;
219
220         target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
221         if (IS_ERR(target->qp)) {
222                 ret = PTR_ERR(target->qp);
223                 ib_destroy_cq(target->cq);
224                 goto out;
225         }
226
227         ret = srp_init_qp(target, target->qp);
228         if (ret) {
229                 ib_destroy_qp(target->qp);
230                 ib_destroy_cq(target->cq);
231                 goto out;
232         }
233
234 out:
235         kfree(init_attr);
236         return ret;
237 }
238
239 static void srp_free_target_ib(struct srp_target_port *target)
240 {
241         int i;
242
243         ib_destroy_qp(target->qp);
244         ib_destroy_cq(target->cq);
245
246         for (i = 0; i < SRP_RQ_SIZE; ++i)
247                 srp_free_iu(target->srp_host, target->rx_ring[i]);
248         for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
249                 srp_free_iu(target->srp_host, target->tx_ring[i]);
250 }
251
252 static void srp_path_rec_completion(int status,
253                                     struct ib_sa_path_rec *pathrec,
254                                     void *target_ptr)
255 {
256         struct srp_target_port *target = target_ptr;
257
258         target->status = status;
259         if (status)
260                 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
261         else
262                 target->path = *pathrec;
263         complete(&target->done);
264 }
265
266 static int srp_lookup_path(struct srp_target_port *target)
267 {
268         target->path.numb_path = 1;
269
270         init_completion(&target->done);
271
272         target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
273                                                    target->srp_host->dev->dev,
274                                                    target->srp_host->port,
275                                                    &target->path,
276                                                    IB_SA_PATH_REC_DGID          |
277                                                    IB_SA_PATH_REC_SGID          |
278                                                    IB_SA_PATH_REC_NUMB_PATH     |
279                                                    IB_SA_PATH_REC_PKEY,
280                                                    SRP_PATH_REC_TIMEOUT_MS,
281                                                    GFP_KERNEL,
282                                                    srp_path_rec_completion,
283                                                    target, &target->path_query);
284         if (target->path_query_id < 0)
285                 return target->path_query_id;
286
287         wait_for_completion(&target->done);
288
289         if (target->status < 0)
290                 printk(KERN_WARNING PFX "Path record query failed\n");
291
292         return target->status;
293 }
294
295 static int srp_send_req(struct srp_target_port *target)
296 {
297         struct {
298                 struct ib_cm_req_param param;
299                 struct srp_login_req   priv;
300         } *req = NULL;
301         int status;
302
303         req = kzalloc(sizeof *req, GFP_KERNEL);
304         if (!req)
305                 return -ENOMEM;
306
307         req->param.primary_path               = &target->path;
308         req->param.alternate_path             = NULL;
309         req->param.service_id                 = target->service_id;
310         req->param.qp_num                     = target->qp->qp_num;
311         req->param.qp_type                    = target->qp->qp_type;
312         req->param.private_data               = &req->priv;
313         req->param.private_data_len           = sizeof req->priv;
314         req->param.flow_control               = 1;
315
316         get_random_bytes(&req->param.starting_psn, 4);
317         req->param.starting_psn              &= 0xffffff;
318
319         /*
320          * Pick some arbitrary defaults here; we could make these
321          * module parameters if anyone cared about setting them.
322          */
323         req->param.responder_resources        = 4;
324         req->param.remote_cm_response_timeout = 20;
325         req->param.local_cm_response_timeout  = 20;
326         req->param.retry_count                = 7;
327         req->param.rnr_retry_count            = 7;
328         req->param.max_cm_retries             = 15;
329
330         req->priv.opcode        = SRP_LOGIN_REQ;
331         req->priv.tag           = 0;
332         req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
333         req->priv.req_buf_fmt   = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
334                                               SRP_BUF_FORMAT_INDIRECT);
335         /*
336          * In the published SRP specification (draft rev. 16a), the
337          * port identifier format is 8 bytes of ID extension followed
338          * by 8 bytes of GUID.  Older drafts put the two halves in the
339          * opposite order, so that the GUID comes first.
340          *
341          * Targets conforming to these obsolete drafts can be
342          * recognized by the I/O Class they report.
343          */
344         if (target->io_class == SRP_REV10_IB_IO_CLASS) {
345                 memcpy(req->priv.initiator_port_id,
346                        &target->path.sgid.global.interface_id, 8);
347                 memcpy(req->priv.initiator_port_id + 8,
348                        &target->initiator_ext, 8);
349                 memcpy(req->priv.target_port_id,     &target->ioc_guid, 8);
350                 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
351         } else {
352                 memcpy(req->priv.initiator_port_id,
353                        &target->initiator_ext, 8);
354                 memcpy(req->priv.initiator_port_id + 8,
355                        &target->path.sgid.global.interface_id, 8);
356                 memcpy(req->priv.target_port_id,     &target->id_ext, 8);
357                 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
358         }
359
360         /*
361          * Topspin/Cisco SRP targets will reject our login unless we
362          * zero out the first 8 bytes of our initiator port ID and set
363          * the second 8 bytes to the local node GUID.
364          */
365         if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
366                 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
367                        "activated for target GUID %016llx\n",
368                        (unsigned long long) be64_to_cpu(target->ioc_guid));
369                 memset(req->priv.initiator_port_id, 0, 8);
370                 memcpy(req->priv.initiator_port_id + 8,
371                        &target->srp_host->dev->dev->node_guid, 8);
372         }
373
374         status = ib_send_cm_req(target->cm_id, &req->param);
375
376         kfree(req);
377
378         return status;
379 }
380
381 static void srp_disconnect_target(struct srp_target_port *target)
382 {
383         /* XXX should send SRP_I_LOGOUT request */
384
385         init_completion(&target->done);
386         if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
387                 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
388                 return;
389         }
390         wait_for_completion(&target->done);
391 }
392
393 static void srp_remove_work(void *target_ptr)
394 {
395         struct srp_target_port *target = target_ptr;
396
397         spin_lock_irq(target->scsi_host->host_lock);
398         if (target->state != SRP_TARGET_DEAD) {
399                 spin_unlock_irq(target->scsi_host->host_lock);
400                 return;
401         }
402         target->state = SRP_TARGET_REMOVED;
403         spin_unlock_irq(target->scsi_host->host_lock);
404
405         spin_lock(&target->srp_host->target_lock);
406         list_del(&target->list);
407         spin_unlock(&target->srp_host->target_lock);
408
409         scsi_remove_host(target->scsi_host);
410         ib_destroy_cm_id(target->cm_id);
411         srp_free_target_ib(target);
412         scsi_host_put(target->scsi_host);
413 }
414
415 static int srp_connect_target(struct srp_target_port *target)
416 {
417         int ret;
418
419         ret = srp_lookup_path(target);
420         if (ret)
421                 return ret;
422
423         while (1) {
424                 init_completion(&target->done);
425                 ret = srp_send_req(target);
426                 if (ret)
427                         return ret;
428                 wait_for_completion(&target->done);
429
430                 /*
431                  * The CM event handling code will set status to
432                  * SRP_PORT_REDIRECT if we get a port redirect REJ
433                  * back, or SRP_DLID_REDIRECT if we get a lid/qp
434                  * redirect REJ back.
435                  */
436                 switch (target->status) {
437                 case 0:
438                         return 0;
439
440                 case SRP_PORT_REDIRECT:
441                         ret = srp_lookup_path(target);
442                         if (ret)
443                                 return ret;
444                         break;
445
446                 case SRP_DLID_REDIRECT:
447                         break;
448
449                 default:
450                         return target->status;
451                 }
452         }
453 }
454
455 static void srp_unmap_data(struct scsi_cmnd *scmnd,
456                            struct srp_target_port *target,
457                            struct srp_request *req)
458 {
459         struct scatterlist *scat;
460         int nents;
461
462         if (!scmnd->request_buffer ||
463             (scmnd->sc_data_direction != DMA_TO_DEVICE &&
464              scmnd->sc_data_direction != DMA_FROM_DEVICE))
465                 return;
466
467         if (req->fmr) {
468                 ib_fmr_pool_unmap(req->fmr);
469                 req->fmr = NULL;
470         }
471
472         /*
473          * This handling of non-SG commands can be killed when the
474          * SCSI midlayer no longer generates non-SG commands.
475          */
476         if (likely(scmnd->use_sg)) {
477                 nents = scmnd->use_sg;
478                 scat  = scmnd->request_buffer;
479         } else {
480                 nents = 1;
481                 scat  = &req->fake_sg;
482         }
483
484         dma_unmap_sg(target->srp_host->dev->dev->dma_device, scat, nents,
485                      scmnd->sc_data_direction);
486 }
487
488 static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
489 {
490         srp_unmap_data(req->scmnd, target, req);
491         list_move_tail(&req->list, &target->free_reqs);
492 }
493
494 static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
495 {
496         req->scmnd->result = DID_RESET << 16;
497         req->scmnd->scsi_done(req->scmnd);
498         srp_remove_req(target, req);
499 }
500
501 static int srp_reconnect_target(struct srp_target_port *target)
502 {
503         struct ib_cm_id *new_cm_id;
504         struct ib_qp_attr qp_attr;
505         struct srp_request *req, *tmp;
506         struct ib_wc wc;
507         int ret;
508
509         spin_lock_irq(target->scsi_host->host_lock);
510         if (target->state != SRP_TARGET_LIVE) {
511                 spin_unlock_irq(target->scsi_host->host_lock);
512                 return -EAGAIN;
513         }
514         target->state = SRP_TARGET_CONNECTING;
515         spin_unlock_irq(target->scsi_host->host_lock);
516
517         srp_disconnect_target(target);
518         /*
519          * Now get a new local CM ID so that we avoid confusing the
520          * target in case things are really fouled up.
521          */
522         new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
523                                     srp_cm_handler, target);
524         if (IS_ERR(new_cm_id)) {
525                 ret = PTR_ERR(new_cm_id);
526                 goto err;
527         }
528         ib_destroy_cm_id(target->cm_id);
529         target->cm_id = new_cm_id;
530
531         qp_attr.qp_state = IB_QPS_RESET;
532         ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
533         if (ret)
534                 goto err;
535
536         ret = srp_init_qp(target, target->qp);
537         if (ret)
538                 goto err;
539
540         while (ib_poll_cq(target->cq, 1, &wc) > 0)
541                 ; /* nothing */
542
543         spin_lock_irq(target->scsi_host->host_lock);
544         list_for_each_entry_safe(req, tmp, &target->req_queue, list)
545                 srp_reset_req(target, req);
546         spin_unlock_irq(target->scsi_host->host_lock);
547
548         target->rx_head  = 0;
549         target->tx_head  = 0;
550         target->tx_tail  = 0;
551
552         ret = srp_connect_target(target);
553         if (ret)
554                 goto err;
555
556         spin_lock_irq(target->scsi_host->host_lock);
557         if (target->state == SRP_TARGET_CONNECTING) {
558                 ret = 0;
559                 target->state = SRP_TARGET_LIVE;
560         } else
561                 ret = -EAGAIN;
562         spin_unlock_irq(target->scsi_host->host_lock);
563
564         return ret;
565
566 err:
567         printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
568
569         /*
570          * We couldn't reconnect, so kill our target port off.
571          * However, we have to defer the real removal because we might
572          * be in the context of the SCSI error handler now, which
573          * would deadlock if we call scsi_remove_host().
574          */
575         spin_lock_irq(target->scsi_host->host_lock);
576         if (target->state == SRP_TARGET_CONNECTING) {
577                 target->state = SRP_TARGET_DEAD;
578                 INIT_WORK(&target->work, srp_remove_work, target);
579                 schedule_work(&target->work);
580         }
581         spin_unlock_irq(target->scsi_host->host_lock);
582
583         return ret;
584 }
585
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)
589 {
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
598         if (!dev->fmr_pool)
599                 return -ENODEV;
600
601         if ((sg_dma_address(&scat[0]) & ~dev->fmr_page_mask) &&
602             mellanox_workarounds && !memcmp(&target->ioc_guid, mellanox_oui, 3))
603                 return -EINVAL;
604
605         len = page_cnt = 0;
606         for (i = 0; i < sg_cnt; ++i) {
607                 if (sg_dma_address(&scat[i]) & ~dev->fmr_page_mask) {
608                         if (i > 0)
609                                 return -EINVAL;
610                         else
611                                 ++page_cnt;
612                 }
613                 if ((sg_dma_address(&scat[i]) + sg_dma_len(&scat[i])) &
614                     ~dev->fmr_page_mask) {
615                         if (i < sg_cnt - 1)
616                                 return -EINVAL;
617                         else
618                                 ++page_cnt;
619                 }
620
621                 len += sg_dma_len(&scat[i]);
622         }
623
624         page_cnt += len >> dev->fmr_page_shift;
625         if (page_cnt > SRP_FMR_SIZE)
626                 return -ENOMEM;
627
628         dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
629         if (!dma_pages)
630                 return -ENOMEM;
631
632         page_cnt = 0;
633         for (i = 0; i < sg_cnt; ++i)
634                 for (j = 0; j < sg_dma_len(&scat[i]); j += dev->fmr_page_size)
635                         dma_pages[page_cnt++] =
636                                 (sg_dma_address(&scat[i]) & dev->fmr_page_mask) + j;
637
638         req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
639                                         dma_pages, page_cnt, io_addr);
640         if (IS_ERR(req->fmr)) {
641                 ret = PTR_ERR(req->fmr);
642                 req->fmr = NULL;
643                 goto out;
644         }
645
646         buf->va  = cpu_to_be64(sg_dma_address(&scat[0]) & ~dev->fmr_page_mask);
647         buf->key = cpu_to_be32(req->fmr->fmr->rkey);
648         buf->len = cpu_to_be32(len);
649
650         ret = 0;
651
652 out:
653         kfree(dma_pages);
654
655         return ret;
656 }
657
658 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
659                         struct srp_request *req)
660 {
661         struct scatterlist *scat;
662         struct srp_cmd *cmd = req->cmd->buf;
663         int len, nents, count;
664         u8 fmt = SRP_DATA_DESC_DIRECT;
665
666         if (!scmnd->request_buffer || scmnd->sc_data_direction == DMA_NONE)
667                 return sizeof (struct srp_cmd);
668
669         if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
670             scmnd->sc_data_direction != DMA_TO_DEVICE) {
671                 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
672                        scmnd->sc_data_direction);
673                 return -EINVAL;
674         }
675
676         /*
677          * This handling of non-SG commands can be killed when the
678          * SCSI midlayer no longer generates non-SG commands.
679          */
680         if (likely(scmnd->use_sg)) {
681                 nents = scmnd->use_sg;
682                 scat  = scmnd->request_buffer;
683         } else {
684                 nents = 1;
685                 scat  = &req->fake_sg;
686                 sg_init_one(scat, scmnd->request_buffer, scmnd->request_bufflen);
687         }
688
689         count = dma_map_sg(target->srp_host->dev->dev->dma_device,
690                            scat, nents, scmnd->sc_data_direction);
691
692         fmt = SRP_DATA_DESC_DIRECT;
693         len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
694
695         if (count == 1) {
696                 /*
697                  * The midlayer only generated a single gather/scatter
698                  * entry, or DMA mapping coalesced everything to a
699                  * single entry.  So a direct descriptor along with
700                  * the DMA MR suffices.
701                  */
702                 struct srp_direct_buf *buf = (void *) cmd->add_data;
703
704                 buf->va  = cpu_to_be64(sg_dma_address(scat));
705                 buf->key = cpu_to_be32(target->srp_host->dev->mr->rkey);
706                 buf->len = cpu_to_be32(sg_dma_len(scat));
707         } else if (srp_map_fmr(target, scat, count, req,
708                                (void *) cmd->add_data)) {
709                 /*
710                  * FMR mapping failed, and the scatterlist has more
711                  * than one entry.  Generate an indirect memory
712                  * descriptor.
713                  */
714                 struct srp_indirect_buf *buf = (void *) cmd->add_data;
715                 u32 datalen = 0;
716                 int i;
717
718                 fmt = SRP_DATA_DESC_INDIRECT;
719                 len = sizeof (struct srp_cmd) +
720                         sizeof (struct srp_indirect_buf) +
721                         count * sizeof (struct srp_direct_buf);
722
723                 for (i = 0; i < count; ++i) {
724                         buf->desc_list[i].va  =
725                                 cpu_to_be64(sg_dma_address(&scat[i]));
726                         buf->desc_list[i].key =
727                                 cpu_to_be32(target->srp_host->dev->mr->rkey);
728                         buf->desc_list[i].len =
729                                 cpu_to_be32(sg_dma_len(&scat[i]));
730                         datalen += sg_dma_len(&scat[i]);
731                 }
732
733                 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
734                         cmd->data_out_desc_cnt = count;
735                 else
736                         cmd->data_in_desc_cnt = count;
737
738                 buf->table_desc.va  =
739                         cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
740                 buf->table_desc.key =
741                         cpu_to_be32(target->srp_host->dev->mr->rkey);
742                 buf->table_desc.len =
743                         cpu_to_be32(count * sizeof (struct srp_direct_buf));
744
745                 buf->len = cpu_to_be32(datalen);
746         }
747
748         if (scmnd->sc_data_direction == DMA_TO_DEVICE)
749                 cmd->buf_fmt = fmt << 4;
750         else
751                 cmd->buf_fmt = fmt;
752
753         return len;
754 }
755
756 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
757 {
758         struct srp_request *req;
759         struct scsi_cmnd *scmnd;
760         unsigned long flags;
761         s32 delta;
762
763         delta = (s32) be32_to_cpu(rsp->req_lim_delta);
764
765         spin_lock_irqsave(target->scsi_host->host_lock, flags);
766
767         target->req_lim += delta;
768
769         req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
770
771         if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
772                 if (be32_to_cpu(rsp->resp_data_len) < 4)
773                         req->tsk_status = -1;
774                 else
775                         req->tsk_status = rsp->data[3];
776                 complete(&req->done);
777         } else {
778                 scmnd = req->scmnd;
779                 if (!scmnd)
780                         printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
781                                (unsigned long long) rsp->tag);
782                 scmnd->result = rsp->status;
783
784                 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
785                         memcpy(scmnd->sense_buffer, rsp->data +
786                                be32_to_cpu(rsp->resp_data_len),
787                                min_t(int, be32_to_cpu(rsp->sense_data_len),
788                                      SCSI_SENSE_BUFFERSIZE));
789                 }
790
791                 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
792                         scmnd->resid = be32_to_cpu(rsp->data_out_res_cnt);
793                 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
794                         scmnd->resid = be32_to_cpu(rsp->data_in_res_cnt);
795
796                 if (!req->tsk_mgmt) {
797                         scmnd->host_scribble = (void *) -1L;
798                         scmnd->scsi_done(scmnd);
799
800                         srp_remove_req(target, req);
801                 } else
802                         req->cmd_done = 1;
803         }
804
805         spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
806 }
807
808 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
809 {
810         struct srp_iu *iu;
811         u8 opcode;
812
813         iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
814
815         dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
816                                 target->max_ti_iu_len, DMA_FROM_DEVICE);
817
818         opcode = *(u8 *) iu->buf;
819
820         if (0) {
821                 int i;
822
823                 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
824
825                 for (i = 0; i < wc->byte_len; ++i) {
826                         if (i % 8 == 0)
827                                 printk(KERN_ERR "  [%02x] ", i);
828                         printk(" %02x", ((u8 *) iu->buf)[i]);
829                         if ((i + 1) % 8 == 0)
830                                 printk("\n");
831                 }
832
833                 if (wc->byte_len % 8)
834                         printk("\n");
835         }
836
837         switch (opcode) {
838         case SRP_RSP:
839                 srp_process_rsp(target, iu->buf);
840                 break;
841
842         case SRP_T_LOGOUT:
843                 /* XXX Handle target logout */
844                 printk(KERN_WARNING PFX "Got target logout request\n");
845                 break;
846
847         default:
848                 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
849                 break;
850         }
851
852         dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
853                                    target->max_ti_iu_len, DMA_FROM_DEVICE);
854 }
855
856 static void srp_completion(struct ib_cq *cq, void *target_ptr)
857 {
858         struct srp_target_port *target = target_ptr;
859         struct ib_wc wc;
860
861         ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
862         while (ib_poll_cq(cq, 1, &wc) > 0) {
863                 if (wc.status) {
864                         printk(KERN_ERR PFX "failed %s status %d\n",
865                                wc.wr_id & SRP_OP_RECV ? "receive" : "send",
866                                wc.status);
867                         break;
868                 }
869
870                 if (wc.wr_id & SRP_OP_RECV)
871                         srp_handle_recv(target, &wc);
872                 else
873                         ++target->tx_tail;
874         }
875 }
876
877 static int __srp_post_recv(struct srp_target_port *target)
878 {
879         struct srp_iu *iu;
880         struct ib_sge list;
881         struct ib_recv_wr wr, *bad_wr;
882         unsigned int next;
883         int ret;
884
885         next     = target->rx_head & (SRP_RQ_SIZE - 1);
886         wr.wr_id = next | SRP_OP_RECV;
887         iu       = target->rx_ring[next];
888
889         list.addr   = iu->dma;
890         list.length = iu->size;
891         list.lkey   = target->srp_host->dev->mr->lkey;
892
893         wr.next     = NULL;
894         wr.sg_list  = &list;
895         wr.num_sge  = 1;
896
897         ret = ib_post_recv(target->qp, &wr, &bad_wr);
898         if (!ret)
899                 ++target->rx_head;
900
901         return ret;
902 }
903
904 static int srp_post_recv(struct srp_target_port *target)
905 {
906         unsigned long flags;
907         int ret;
908
909         spin_lock_irqsave(target->scsi_host->host_lock, flags);
910         ret = __srp_post_recv(target);
911         spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
912
913         return ret;
914 }
915
916 /*
917  * Must be called with target->scsi_host->host_lock held to protect
918  * req_lim and tx_head.  Lock cannot be dropped between call here and
919  * call to __srp_post_send().
920  */
921 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
922 {
923         if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
924                 return NULL;
925
926         if (unlikely(target->req_lim < 1))
927                 ++target->zero_req_lim;
928
929         return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
930 }
931
932 /*
933  * Must be called with target->scsi_host->host_lock held to protect
934  * req_lim and tx_head.
935  */
936 static int __srp_post_send(struct srp_target_port *target,
937                            struct srp_iu *iu, int len)
938 {
939         struct ib_sge list;
940         struct ib_send_wr wr, *bad_wr;
941         int ret = 0;
942
943         list.addr   = iu->dma;
944         list.length = len;
945         list.lkey   = target->srp_host->dev->mr->lkey;
946
947         wr.next       = NULL;
948         wr.wr_id      = target->tx_head & SRP_SQ_SIZE;
949         wr.sg_list    = &list;
950         wr.num_sge    = 1;
951         wr.opcode     = IB_WR_SEND;
952         wr.send_flags = IB_SEND_SIGNALED;
953
954         ret = ib_post_send(target->qp, &wr, &bad_wr);
955
956         if (!ret) {
957                 ++target->tx_head;
958                 --target->req_lim;
959         }
960
961         return ret;
962 }
963
964 static int srp_queuecommand(struct scsi_cmnd *scmnd,
965                             void (*done)(struct scsi_cmnd *))
966 {
967         struct srp_target_port *target = host_to_target(scmnd->device->host);
968         struct srp_request *req;
969         struct srp_iu *iu;
970         struct srp_cmd *cmd;
971         int len;
972
973         if (target->state == SRP_TARGET_CONNECTING)
974                 goto err;
975
976         if (target->state == SRP_TARGET_DEAD ||
977             target->state == SRP_TARGET_REMOVED) {
978                 scmnd->result = DID_BAD_TARGET << 16;
979                 done(scmnd);
980                 return 0;
981         }
982
983         iu = __srp_get_tx_iu(target);
984         if (!iu)
985                 goto err;
986
987         dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
988                                 srp_max_iu_len, DMA_TO_DEVICE);
989
990         req = list_entry(target->free_reqs.next, struct srp_request, list);
991
992         scmnd->scsi_done     = done;
993         scmnd->result        = 0;
994         scmnd->host_scribble = (void *) (long) req->index;
995
996         cmd = iu->buf;
997         memset(cmd, 0, sizeof *cmd);
998
999         cmd->opcode = SRP_CMD;
1000         cmd->lun    = cpu_to_be64((u64) scmnd->device->lun << 48);
1001         cmd->tag    = req->index;
1002         memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1003
1004         req->scmnd    = scmnd;
1005         req->cmd      = iu;
1006         req->cmd_done = 0;
1007         req->tsk_mgmt = NULL;
1008
1009         len = srp_map_data(scmnd, target, req);
1010         if (len < 0) {
1011                 printk(KERN_ERR PFX "Failed to map data\n");
1012                 goto err;
1013         }
1014
1015         if (__srp_post_recv(target)) {
1016                 printk(KERN_ERR PFX "Recv failed\n");
1017                 goto err_unmap;
1018         }
1019
1020         dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
1021                                    srp_max_iu_len, DMA_TO_DEVICE);
1022
1023         if (__srp_post_send(target, iu, len)) {
1024                 printk(KERN_ERR PFX "Send failed\n");
1025                 goto err_unmap;
1026         }
1027
1028         list_move_tail(&req->list, &target->req_queue);
1029
1030         return 0;
1031
1032 err_unmap:
1033         srp_unmap_data(scmnd, target, req);
1034
1035 err:
1036         return SCSI_MLQUEUE_HOST_BUSY;
1037 }
1038
1039 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1040 {
1041         int i;
1042
1043         for (i = 0; i < SRP_RQ_SIZE; ++i) {
1044                 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1045                                                   target->max_ti_iu_len,
1046                                                   GFP_KERNEL, DMA_FROM_DEVICE);
1047                 if (!target->rx_ring[i])
1048                         goto err;
1049         }
1050
1051         for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1052                 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1053                                                   srp_max_iu_len,
1054                                                   GFP_KERNEL, DMA_TO_DEVICE);
1055                 if (!target->tx_ring[i])
1056                         goto err;
1057         }
1058
1059         return 0;
1060
1061 err:
1062         for (i = 0; i < SRP_RQ_SIZE; ++i) {
1063                 srp_free_iu(target->srp_host, target->rx_ring[i]);
1064                 target->rx_ring[i] = NULL;
1065         }
1066
1067         for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1068                 srp_free_iu(target->srp_host, target->tx_ring[i]);
1069                 target->tx_ring[i] = NULL;
1070         }
1071
1072         return -ENOMEM;
1073 }
1074
1075 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1076                                struct ib_cm_event *event,
1077                                struct srp_target_port *target)
1078 {
1079         struct ib_class_port_info *cpi;
1080         int opcode;
1081
1082         switch (event->param.rej_rcvd.reason) {
1083         case IB_CM_REJ_PORT_CM_REDIRECT:
1084                 cpi = event->param.rej_rcvd.ari;
1085                 target->path.dlid = cpi->redirect_lid;
1086                 target->path.pkey = cpi->redirect_pkey;
1087                 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1088                 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1089
1090                 target->status = target->path.dlid ?
1091                         SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1092                 break;
1093
1094         case IB_CM_REJ_PORT_REDIRECT:
1095                 if (topspin_workarounds &&
1096                     !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1097                         /*
1098                          * Topspin/Cisco SRP gateways incorrectly send
1099                          * reject reason code 25 when they mean 24
1100                          * (port redirect).
1101                          */
1102                         memcpy(target->path.dgid.raw,
1103                                event->param.rej_rcvd.ari, 16);
1104
1105                         printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1106                                (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1107                                (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1108
1109                         target->status = SRP_PORT_REDIRECT;
1110                 } else {
1111                         printk(KERN_WARNING "  REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1112                         target->status = -ECONNRESET;
1113                 }
1114                 break;
1115
1116         case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1117                 printk(KERN_WARNING "  REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1118                 target->status = -ECONNRESET;
1119                 break;
1120
1121         case IB_CM_REJ_CONSUMER_DEFINED:
1122                 opcode = *(u8 *) event->private_data;
1123                 if (opcode == SRP_LOGIN_REJ) {
1124                         struct srp_login_rej *rej = event->private_data;
1125                         u32 reason = be32_to_cpu(rej->reason);
1126
1127                         if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1128                                 printk(KERN_WARNING PFX
1129                                        "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1130                         else
1131                                 printk(KERN_WARNING PFX
1132                                        "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1133                 } else
1134                         printk(KERN_WARNING "  REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1135                                " opcode 0x%02x\n", opcode);
1136                 target->status = -ECONNRESET;
1137                 break;
1138
1139         default:
1140                 printk(KERN_WARNING "  REJ reason 0x%x\n",
1141                        event->param.rej_rcvd.reason);
1142                 target->status = -ECONNRESET;
1143         }
1144 }
1145
1146 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1147 {
1148         struct srp_target_port *target = cm_id->context;
1149         struct ib_qp_attr *qp_attr = NULL;
1150         int attr_mask = 0;
1151         int comp = 0;
1152         int opcode = 0;
1153
1154         switch (event->event) {
1155         case IB_CM_REQ_ERROR:
1156                 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1157                 comp = 1;
1158                 target->status = -ECONNRESET;
1159                 break;
1160
1161         case IB_CM_REP_RECEIVED:
1162                 comp = 1;
1163                 opcode = *(u8 *) event->private_data;
1164
1165                 if (opcode == SRP_LOGIN_RSP) {
1166                         struct srp_login_rsp *rsp = event->private_data;
1167
1168                         target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1169                         target->req_lim       = be32_to_cpu(rsp->req_lim_delta);
1170
1171                         target->scsi_host->can_queue = min(target->req_lim,
1172                                                            target->scsi_host->can_queue);
1173                 } else {
1174                         printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1175                         target->status = -ECONNRESET;
1176                         break;
1177                 }
1178
1179                 target->status = srp_alloc_iu_bufs(target);
1180                 if (target->status)
1181                         break;
1182
1183                 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1184                 if (!qp_attr) {
1185                         target->status = -ENOMEM;
1186                         break;
1187                 }
1188
1189                 qp_attr->qp_state = IB_QPS_RTR;
1190                 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1191                 if (target->status)
1192                         break;
1193
1194                 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1195                 if (target->status)
1196                         break;
1197
1198                 target->status = srp_post_recv(target);
1199                 if (target->status)
1200                         break;
1201
1202                 qp_attr->qp_state = IB_QPS_RTS;
1203                 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1204                 if (target->status)
1205                         break;
1206
1207                 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1208                 if (target->status)
1209                         break;
1210
1211                 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1212                 if (target->status)
1213                         break;
1214
1215                 break;
1216
1217         case IB_CM_REJ_RECEIVED:
1218                 printk(KERN_DEBUG PFX "REJ received\n");
1219                 comp = 1;
1220
1221                 srp_cm_rej_handler(cm_id, event, target);
1222                 break;
1223
1224         case IB_CM_DREQ_RECEIVED:
1225                 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1226                 if (ib_send_cm_drep(cm_id, NULL, 0))
1227                         printk(KERN_ERR PFX "Sending CM DREP failed\n");
1228                 break;
1229
1230         case IB_CM_TIMEWAIT_EXIT:
1231                 printk(KERN_ERR PFX "connection closed\n");
1232
1233                 comp = 1;
1234                 target->status = 0;
1235                 break;
1236
1237         case IB_CM_MRA_RECEIVED:
1238         case IB_CM_DREQ_ERROR:
1239         case IB_CM_DREP_RECEIVED:
1240                 break;
1241
1242         default:
1243                 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1244                 break;
1245         }
1246
1247         if (comp)
1248                 complete(&target->done);
1249
1250         kfree(qp_attr);
1251
1252         return 0;
1253 }
1254
1255 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1256                              struct srp_request *req, u8 func)
1257 {
1258         struct srp_iu *iu;
1259         struct srp_tsk_mgmt *tsk_mgmt;
1260
1261         spin_lock_irq(target->scsi_host->host_lock);
1262
1263         if (target->state == SRP_TARGET_DEAD ||
1264             target->state == SRP_TARGET_REMOVED) {
1265                 req->scmnd->result = DID_BAD_TARGET << 16;
1266                 goto out;
1267         }
1268
1269         init_completion(&req->done);
1270
1271         iu = __srp_get_tx_iu(target);
1272         if (!iu)
1273                 goto out;
1274
1275         tsk_mgmt = iu->buf;
1276         memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1277
1278         tsk_mgmt->opcode        = SRP_TSK_MGMT;
1279         tsk_mgmt->lun           = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1280         tsk_mgmt->tag           = req->index | SRP_TAG_TSK_MGMT;
1281         tsk_mgmt->tsk_mgmt_func = func;
1282         tsk_mgmt->task_tag      = req->index;
1283
1284         if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1285                 goto out;
1286
1287         req->tsk_mgmt = iu;
1288
1289         spin_unlock_irq(target->scsi_host->host_lock);
1290
1291         if (!wait_for_completion_timeout(&req->done,
1292                                          msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1293                 return -1;
1294
1295         return 0;
1296
1297 out:
1298         spin_unlock_irq(target->scsi_host->host_lock);
1299         return -1;
1300 }
1301
1302 static int srp_find_req(struct srp_target_port *target,
1303                         struct scsi_cmnd *scmnd,
1304                         struct srp_request **req)
1305 {
1306         if (scmnd->host_scribble == (void *) -1L)
1307                 return -1;
1308
1309         *req = &target->req_ring[(long) scmnd->host_scribble];
1310
1311         return 0;
1312 }
1313
1314 static int srp_abort(struct scsi_cmnd *scmnd)
1315 {
1316         struct srp_target_port *target = host_to_target(scmnd->device->host);
1317         struct srp_request *req;
1318         int ret = SUCCESS;
1319
1320         printk(KERN_ERR "SRP abort called\n");
1321
1322         if (srp_find_req(target, scmnd, &req))
1323                 return FAILED;
1324         if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1325                 return FAILED;
1326
1327         spin_lock_irq(target->scsi_host->host_lock);
1328
1329         if (req->cmd_done) {
1330                 srp_remove_req(target, req);
1331                 scmnd->scsi_done(scmnd);
1332         } else if (!req->tsk_status) {
1333                 srp_remove_req(target, req);
1334                 scmnd->result = DID_ABORT << 16;
1335         } else
1336                 ret = FAILED;
1337
1338         spin_unlock_irq(target->scsi_host->host_lock);
1339
1340         return ret;
1341 }
1342
1343 static int srp_reset_device(struct scsi_cmnd *scmnd)
1344 {
1345         struct srp_target_port *target = host_to_target(scmnd->device->host);
1346         struct srp_request *req, *tmp;
1347
1348         printk(KERN_ERR "SRP reset_device called\n");
1349
1350         if (srp_find_req(target, scmnd, &req))
1351                 return FAILED;
1352         if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1353                 return FAILED;
1354         if (req->tsk_status)
1355                 return FAILED;
1356
1357         spin_lock_irq(target->scsi_host->host_lock);
1358
1359         list_for_each_entry_safe(req, tmp, &target->req_queue, list)
1360                 if (req->scmnd->device == scmnd->device)
1361                         srp_reset_req(target, req);
1362
1363         spin_unlock_irq(target->scsi_host->host_lock);
1364
1365         return SUCCESS;
1366 }
1367
1368 static int srp_reset_host(struct scsi_cmnd *scmnd)
1369 {
1370         struct srp_target_port *target = host_to_target(scmnd->device->host);
1371         int ret = FAILED;
1372
1373         printk(KERN_ERR PFX "SRP reset_host called\n");
1374
1375         if (!srp_reconnect_target(target))
1376                 ret = SUCCESS;
1377
1378         return ret;
1379 }
1380
1381 static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1382 {
1383         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1384
1385         if (target->state == SRP_TARGET_DEAD ||
1386             target->state == SRP_TARGET_REMOVED)
1387                 return -ENODEV;
1388
1389         return sprintf(buf, "0x%016llx\n",
1390                        (unsigned long long) be64_to_cpu(target->id_ext));
1391 }
1392
1393 static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1394 {
1395         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1396
1397         if (target->state == SRP_TARGET_DEAD ||
1398             target->state == SRP_TARGET_REMOVED)
1399                 return -ENODEV;
1400
1401         return sprintf(buf, "0x%016llx\n",
1402                        (unsigned long long) be64_to_cpu(target->ioc_guid));
1403 }
1404
1405 static ssize_t show_service_id(struct class_device *cdev, char *buf)
1406 {
1407         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1408
1409         if (target->state == SRP_TARGET_DEAD ||
1410             target->state == SRP_TARGET_REMOVED)
1411                 return -ENODEV;
1412
1413         return sprintf(buf, "0x%016llx\n",
1414                        (unsigned long long) be64_to_cpu(target->service_id));
1415 }
1416
1417 static ssize_t show_pkey(struct class_device *cdev, char *buf)
1418 {
1419         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1420
1421         if (target->state == SRP_TARGET_DEAD ||
1422             target->state == SRP_TARGET_REMOVED)
1423                 return -ENODEV;
1424
1425         return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1426 }
1427
1428 static ssize_t show_dgid(struct class_device *cdev, char *buf)
1429 {
1430         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1431
1432         if (target->state == SRP_TARGET_DEAD ||
1433             target->state == SRP_TARGET_REMOVED)
1434                 return -ENODEV;
1435
1436         return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1437                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1438                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1439                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1440                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1441                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1442                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1443                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1444                        be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1445 }
1446
1447 static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1448 {
1449         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1450
1451         if (target->state == SRP_TARGET_DEAD ||
1452             target->state == SRP_TARGET_REMOVED)
1453                 return -ENODEV;
1454
1455         return sprintf(buf, "%d\n", target->zero_req_lim);
1456 }
1457
1458 static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1459 {
1460         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1461
1462         return sprintf(buf, "%d\n", target->srp_host->port);
1463 }
1464
1465 static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1466 {
1467         struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1468
1469         return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1470 }
1471
1472 static CLASS_DEVICE_ATTR(id_ext,          S_IRUGO, show_id_ext,          NULL);
1473 static CLASS_DEVICE_ATTR(ioc_guid,        S_IRUGO, show_ioc_guid,        NULL);
1474 static CLASS_DEVICE_ATTR(service_id,      S_IRUGO, show_service_id,      NULL);
1475 static CLASS_DEVICE_ATTR(pkey,            S_IRUGO, show_pkey,            NULL);
1476 static CLASS_DEVICE_ATTR(dgid,            S_IRUGO, show_dgid,            NULL);
1477 static CLASS_DEVICE_ATTR(zero_req_lim,    S_IRUGO, show_zero_req_lim,    NULL);
1478 static CLASS_DEVICE_ATTR(local_ib_port,   S_IRUGO, show_local_ib_port,   NULL);
1479 static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
1480
1481 static struct class_device_attribute *srp_host_attrs[] = {
1482         &class_device_attr_id_ext,
1483         &class_device_attr_ioc_guid,
1484         &class_device_attr_service_id,
1485         &class_device_attr_pkey,
1486         &class_device_attr_dgid,
1487         &class_device_attr_zero_req_lim,
1488         &class_device_attr_local_ib_port,
1489         &class_device_attr_local_ib_device,
1490         NULL
1491 };
1492
1493 static struct scsi_host_template srp_template = {
1494         .module                         = THIS_MODULE,
1495         .name                           = DRV_NAME,
1496         .info                           = srp_target_info,
1497         .queuecommand                   = srp_queuecommand,
1498         .eh_abort_handler               = srp_abort,
1499         .eh_device_reset_handler        = srp_reset_device,
1500         .eh_host_reset_handler          = srp_reset_host,
1501         .can_queue                      = SRP_SQ_SIZE,
1502         .this_id                        = -1,
1503         .cmd_per_lun                    = SRP_SQ_SIZE,
1504         .use_clustering                 = ENABLE_CLUSTERING,
1505         .shost_attrs                    = srp_host_attrs
1506 };
1507
1508 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1509 {
1510         sprintf(target->target_name, "SRP.T10:%016llX",
1511                  (unsigned long long) be64_to_cpu(target->id_ext));
1512
1513         if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
1514                 return -ENODEV;
1515
1516         spin_lock(&host->target_lock);
1517         list_add_tail(&target->list, &host->target_list);
1518         spin_unlock(&host->target_lock);
1519
1520         target->state = SRP_TARGET_LIVE;
1521
1522         scsi_scan_target(&target->scsi_host->shost_gendev,
1523                          0, target->scsi_id, SCAN_WILD_CARD, 0);
1524
1525         return 0;
1526 }
1527
1528 static void srp_release_class_dev(struct class_device *class_dev)
1529 {
1530         struct srp_host *host =
1531                 container_of(class_dev, struct srp_host, class_dev);
1532
1533         complete(&host->released);
1534 }
1535
1536 static struct class srp_class = {
1537         .name    = "infiniband_srp",
1538         .release = srp_release_class_dev
1539 };
1540
1541 /*
1542  * Target ports are added by writing
1543  *
1544  *     id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1545  *     pkey=<P_Key>,service_id=<service ID>
1546  *
1547  * to the add_target sysfs attribute.
1548  */
1549 enum {
1550         SRP_OPT_ERR             = 0,
1551         SRP_OPT_ID_EXT          = 1 << 0,
1552         SRP_OPT_IOC_GUID        = 1 << 1,
1553         SRP_OPT_DGID            = 1 << 2,
1554         SRP_OPT_PKEY            = 1 << 3,
1555         SRP_OPT_SERVICE_ID      = 1 << 4,
1556         SRP_OPT_MAX_SECT        = 1 << 5,
1557         SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
1558         SRP_OPT_IO_CLASS        = 1 << 7,
1559         SRP_OPT_INITIATOR_EXT   = 1 << 8,
1560         SRP_OPT_ALL             = (SRP_OPT_ID_EXT       |
1561                                    SRP_OPT_IOC_GUID     |
1562                                    SRP_OPT_DGID         |
1563                                    SRP_OPT_PKEY         |
1564                                    SRP_OPT_SERVICE_ID),
1565 };
1566
1567 static match_table_t srp_opt_tokens = {
1568         { SRP_OPT_ID_EXT,               "id_ext=%s"             },
1569         { SRP_OPT_IOC_GUID,             "ioc_guid=%s"           },
1570         { SRP_OPT_DGID,                 "dgid=%s"               },
1571         { SRP_OPT_PKEY,                 "pkey=%x"               },
1572         { SRP_OPT_SERVICE_ID,           "service_id=%s"         },
1573         { SRP_OPT_MAX_SECT,             "max_sect=%d"           },
1574         { SRP_OPT_MAX_CMD_PER_LUN,      "max_cmd_per_lun=%d"    },
1575         { SRP_OPT_IO_CLASS,             "io_class=%x"           },
1576         { SRP_OPT_INITIATOR_EXT,        "initiator_ext=%s"      },
1577         { SRP_OPT_ERR,                  NULL                    }
1578 };
1579
1580 static int srp_parse_options(const char *buf, struct srp_target_port *target)
1581 {
1582         char *options, *sep_opt;
1583         char *p;
1584         char dgid[3];
1585         substring_t args[MAX_OPT_ARGS];
1586         int opt_mask = 0;
1587         int token;
1588         int ret = -EINVAL;
1589         int i;
1590
1591         options = kstrdup(buf, GFP_KERNEL);
1592         if (!options)
1593                 return -ENOMEM;
1594
1595         sep_opt = options;
1596         while ((p = strsep(&sep_opt, ",")) != NULL) {
1597                 if (!*p)
1598                         continue;
1599
1600                 token = match_token(p, srp_opt_tokens, args);
1601                 opt_mask |= token;
1602
1603                 switch (token) {
1604                 case SRP_OPT_ID_EXT:
1605                         p = match_strdup(args);
1606                         target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1607                         kfree(p);
1608                         break;
1609
1610                 case SRP_OPT_IOC_GUID:
1611                         p = match_strdup(args);
1612                         target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1613                         kfree(p);
1614                         break;
1615
1616                 case SRP_OPT_DGID:
1617                         p = match_strdup(args);
1618                         if (strlen(p) != 32) {
1619                                 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
1620                                 kfree(p);
1621                                 goto out;
1622                         }
1623
1624                         for (i = 0; i < 16; ++i) {
1625                                 strlcpy(dgid, p + i * 2, 3);
1626                                 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1627                         }
1628                         kfree(p);
1629                         break;
1630
1631                 case SRP_OPT_PKEY:
1632                         if (match_hex(args, &token)) {
1633                                 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1634                                 goto out;
1635                         }
1636                         target->path.pkey = cpu_to_be16(token);
1637                         break;
1638
1639                 case SRP_OPT_SERVICE_ID:
1640                         p = match_strdup(args);
1641                         target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1642                         kfree(p);
1643                         break;
1644
1645                 case SRP_OPT_MAX_SECT:
1646                         if (match_int(args, &token)) {
1647                                 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1648                                 goto out;
1649                         }
1650                         target->scsi_host->max_sectors = token;
1651                         break;
1652
1653                 case SRP_OPT_MAX_CMD_PER_LUN:
1654                         if (match_int(args, &token)) {
1655                                 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1656                                 goto out;
1657                         }
1658                         target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1659                         break;
1660
1661                 case SRP_OPT_IO_CLASS:
1662                         if (match_hex(args, &token)) {
1663                                 printk(KERN_WARNING PFX "bad  IO class parameter '%s' \n", p);
1664                                 goto out;
1665                         }
1666                         if (token != SRP_REV10_IB_IO_CLASS &&
1667                             token != SRP_REV16A_IB_IO_CLASS) {
1668                                 printk(KERN_WARNING PFX "unknown IO class parameter value"
1669                                        " %x specified (use %x or %x).\n",
1670                                        token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1671                                 goto out;
1672                         }
1673                         target->io_class = token;
1674                         break;
1675
1676                 case SRP_OPT_INITIATOR_EXT:
1677                         p = match_strdup(args);
1678                         target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1679                         kfree(p);
1680                         break;
1681
1682                 default:
1683                         printk(KERN_WARNING PFX "unknown parameter or missing value "
1684                                "'%s' in target creation request\n", p);
1685                         goto out;
1686                 }
1687         }
1688
1689         if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1690                 ret = 0;
1691         else
1692                 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1693                         if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1694                             !(srp_opt_tokens[i].token & opt_mask))
1695                                 printk(KERN_WARNING PFX "target creation request is "
1696                                        "missing parameter '%s'\n",
1697                                        srp_opt_tokens[i].pattern);
1698
1699 out:
1700         kfree(options);
1701         return ret;
1702 }
1703
1704 static ssize_t srp_create_target(struct class_device *class_dev,
1705                                  const char *buf, size_t count)
1706 {
1707         struct srp_host *host =
1708                 container_of(class_dev, struct srp_host, class_dev);
1709         struct Scsi_Host *target_host;
1710         struct srp_target_port *target;
1711         int ret;
1712         int i;
1713
1714         target_host = scsi_host_alloc(&srp_template,
1715                                       sizeof (struct srp_target_port));
1716         if (!target_host)
1717                 return -ENOMEM;
1718
1719         target_host->max_lun = SRP_MAX_LUN;
1720
1721         target = host_to_target(target_host);
1722
1723         target->io_class   = SRP_REV16A_IB_IO_CLASS;
1724         target->scsi_host  = target_host;
1725         target->srp_host   = host;
1726
1727         INIT_LIST_HEAD(&target->free_reqs);
1728         INIT_LIST_HEAD(&target->req_queue);
1729         for (i = 0; i < SRP_SQ_SIZE; ++i) {
1730                 target->req_ring[i].index = i;
1731                 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1732         }
1733
1734         ret = srp_parse_options(buf, target);
1735         if (ret)
1736                 goto err;
1737
1738         ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
1739
1740         printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1741                "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1742                (unsigned long long) be64_to_cpu(target->id_ext),
1743                (unsigned long long) be64_to_cpu(target->ioc_guid),
1744                be16_to_cpu(target->path.pkey),
1745                (unsigned long long) be64_to_cpu(target->service_id),
1746                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1747                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1748                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1749                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1750                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1751                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1752                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1753                (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1754
1755         ret = srp_create_target_ib(target);
1756         if (ret)
1757                 goto err;
1758
1759         target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
1760         if (IS_ERR(target->cm_id)) {
1761                 ret = PTR_ERR(target->cm_id);
1762                 goto err_free;
1763         }
1764
1765         ret = srp_connect_target(target);
1766         if (ret) {
1767                 printk(KERN_ERR PFX "Connection failed\n");
1768                 goto err_cm_id;
1769         }
1770
1771         ret = srp_add_target(host, target);
1772         if (ret)
1773                 goto err_disconnect;
1774
1775         return count;
1776
1777 err_disconnect:
1778         srp_disconnect_target(target);
1779
1780 err_cm_id:
1781         ib_destroy_cm_id(target->cm_id);
1782
1783 err_free:
1784         srp_free_target_ib(target);
1785
1786 err:
1787         scsi_host_put(target_host);
1788
1789         return ret;
1790 }
1791
1792 static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1793
1794 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1795 {
1796         struct srp_host *host =
1797                 container_of(class_dev, struct srp_host, class_dev);
1798
1799         return sprintf(buf, "%s\n", host->dev->dev->name);
1800 }
1801
1802 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1803
1804 static ssize_t show_port(struct class_device *class_dev, char *buf)
1805 {
1806         struct srp_host *host =
1807                 container_of(class_dev, struct srp_host, class_dev);
1808
1809         return sprintf(buf, "%d\n", host->port);
1810 }
1811
1812 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1813
1814 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
1815 {
1816         struct srp_host *host;
1817
1818         host = kzalloc(sizeof *host, GFP_KERNEL);
1819         if (!host)
1820                 return NULL;
1821
1822         INIT_LIST_HEAD(&host->target_list);
1823         spin_lock_init(&host->target_lock);
1824         init_completion(&host->released);
1825         host->dev  = device;
1826         host->port = port;
1827
1828         host->class_dev.class = &srp_class;
1829         host->class_dev.dev   = device->dev->dma_device;
1830         snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
1831                  device->dev->name, port);
1832
1833         if (class_device_register(&host->class_dev))
1834                 goto free_host;
1835         if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1836                 goto err_class;
1837         if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1838                 goto err_class;
1839         if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1840                 goto err_class;
1841
1842         return host;
1843
1844 err_class:
1845         class_device_unregister(&host->class_dev);
1846
1847 free_host:
1848         kfree(host);
1849
1850         return NULL;
1851 }
1852
1853 static void srp_add_one(struct ib_device *device)
1854 {
1855         struct srp_device *srp_dev;
1856         struct ib_device_attr *dev_attr;
1857         struct ib_fmr_pool_param fmr_param;
1858         struct srp_host *host;
1859         int s, e, p;
1860
1861         dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1862         if (!dev_attr)
1863                 return;
1864
1865         if (ib_query_device(device, dev_attr)) {
1866                 printk(KERN_WARNING PFX "Query device failed for %s\n",
1867                        device->name);
1868                 goto free_attr;
1869         }
1870
1871         srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1872         if (!srp_dev)
1873                 goto free_attr;
1874
1875         /*
1876          * Use the smallest page size supported by the HCA, down to a
1877          * minimum of 512 bytes (which is the smallest sector that a
1878          * SCSI command will ever carry).
1879          */
1880         srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1881         srp_dev->fmr_page_size  = 1 << srp_dev->fmr_page_shift;
1882         srp_dev->fmr_page_mask  = ~((unsigned long) srp_dev->fmr_page_size - 1);
1883
1884         INIT_LIST_HEAD(&srp_dev->dev_list);
1885
1886         srp_dev->dev = device;
1887         srp_dev->pd  = ib_alloc_pd(device);
1888         if (IS_ERR(srp_dev->pd))
1889                 goto free_dev;
1890
1891         srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1892                                     IB_ACCESS_LOCAL_WRITE |
1893                                     IB_ACCESS_REMOTE_READ |
1894                                     IB_ACCESS_REMOTE_WRITE);
1895         if (IS_ERR(srp_dev->mr))
1896                 goto err_pd;
1897
1898         memset(&fmr_param, 0, sizeof fmr_param);
1899         fmr_param.pool_size         = SRP_FMR_POOL_SIZE;
1900         fmr_param.dirty_watermark   = SRP_FMR_DIRTY_SIZE;
1901         fmr_param.cache             = 1;
1902         fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1903         fmr_param.page_shift        = srp_dev->fmr_page_shift;
1904         fmr_param.access            = (IB_ACCESS_LOCAL_WRITE |
1905                                        IB_ACCESS_REMOTE_WRITE |
1906                                        IB_ACCESS_REMOTE_READ);
1907
1908         srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1909         if (IS_ERR(srp_dev->fmr_pool))
1910                 srp_dev->fmr_pool = NULL;
1911
1912         if (device->node_type == RDMA_NODE_IB_SWITCH) {
1913                 s = 0;
1914                 e = 0;
1915         } else {
1916                 s = 1;
1917                 e = device->phys_port_cnt;
1918         }
1919
1920         for (p = s; p <= e; ++p) {
1921                 host = srp_add_port(srp_dev, p);
1922                 if (host)
1923                         list_add_tail(&host->list, &srp_dev->dev_list);
1924         }
1925
1926         ib_set_client_data(device, &srp_client, srp_dev);
1927
1928         goto free_attr;
1929
1930 err_pd:
1931         ib_dealloc_pd(srp_dev->pd);
1932
1933 free_dev:
1934         kfree(srp_dev);
1935
1936 free_attr:
1937         kfree(dev_attr);
1938 }
1939
1940 static void srp_remove_one(struct ib_device *device)
1941 {
1942         struct srp_device *srp_dev;
1943         struct srp_host *host, *tmp_host;
1944         LIST_HEAD(target_list);
1945         struct srp_target_port *target, *tmp_target;
1946
1947         srp_dev = ib_get_client_data(device, &srp_client);
1948
1949         list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
1950                 class_device_unregister(&host->class_dev);
1951                 /*
1952                  * Wait for the sysfs entry to go away, so that no new
1953                  * target ports can be created.
1954                  */
1955                 wait_for_completion(&host->released);
1956
1957                 /*
1958                  * Mark all target ports as removed, so we stop queueing
1959                  * commands and don't try to reconnect.
1960                  */
1961                 spin_lock(&host->target_lock);
1962                 list_for_each_entry(target, &host->target_list, list) {
1963                         spin_lock_irq(target->scsi_host->host_lock);
1964                         target->state = SRP_TARGET_REMOVED;
1965                         spin_unlock_irq(target->scsi_host->host_lock);
1966                 }
1967                 spin_unlock(&host->target_lock);
1968
1969                 /*
1970                  * Wait for any reconnection tasks that may have
1971                  * started before we marked our target ports as
1972                  * removed, and any target port removal tasks.
1973                  */
1974                 flush_scheduled_work();
1975
1976                 list_for_each_entry_safe(target, tmp_target,
1977                                          &host->target_list, list) {
1978                         scsi_remove_host(target->scsi_host);
1979                         srp_disconnect_target(target);
1980                         ib_destroy_cm_id(target->cm_id);
1981                         srp_free_target_ib(target);
1982                         scsi_host_put(target->scsi_host);
1983                 }
1984
1985                 kfree(host);
1986         }
1987
1988         if (srp_dev->fmr_pool)
1989                 ib_destroy_fmr_pool(srp_dev->fmr_pool);
1990         ib_dereg_mr(srp_dev->mr);
1991         ib_dealloc_pd(srp_dev->pd);
1992
1993         kfree(srp_dev);
1994 }
1995
1996 static int __init srp_init_module(void)
1997 {
1998         int ret;
1999
2000         srp_template.sg_tablesize = srp_sg_tablesize;
2001         srp_max_iu_len = (sizeof (struct srp_cmd) +
2002                           sizeof (struct srp_indirect_buf) +
2003                           srp_sg_tablesize * 16);
2004
2005         ret = class_register(&srp_class);
2006         if (ret) {
2007                 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
2008                 return ret;
2009         }
2010
2011         ib_sa_register_client(&srp_sa_client);
2012
2013         ret = ib_register_client(&srp_client);
2014         if (ret) {
2015                 printk(KERN_ERR PFX "couldn't register IB client\n");
2016                 ib_sa_unregister_client(&srp_sa_client);
2017                 class_unregister(&srp_class);
2018                 return ret;
2019         }
2020
2021         return 0;
2022 }
2023
2024 static void __exit srp_cleanup_module(void)
2025 {
2026         ib_unregister_client(&srp_client);
2027         ib_sa_unregister_client(&srp_sa_client);
2028         class_unregister(&srp_class);
2029 }
2030
2031 module_init(srp_init_module);
2032 module_exit(srp_cleanup_module);