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