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