Merge tag 'v3.10.55' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.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
33 #define pr_fmt(fmt) PFX fmt
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 <linux/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 #include <scsi/scsi_transport_srp.h>
51
52 #include "ib_srp.h"
53
54 #define DRV_NAME "ib_srp"
55 #define PFX DRV_NAME ": "
56 #define DRV_VERSION "0.2"
57 #define DRV_RELDATE "November 1, 2005"
58
59 MODULE_AUTHOR("Roland Dreier");
60 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
61 "v" DRV_VERSION " (" DRV_RELDATE ")");
62 MODULE_LICENSE("Dual BSD/GPL");
63
64 static unsigned int srp_sg_tablesize;
65 static unsigned int cmd_sg_entries;
66 static unsigned int indirect_sg_entries;
67 static bool allow_ext_sg;
68 static int topspin_workarounds = 1;
69
70 module_param(srp_sg_tablesize, uint, 0444);
71 MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
72
73 module_param(cmd_sg_entries, uint, 0444);
74 MODULE_PARM_DESC(cmd_sg_entries,
75 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
76
77 module_param(indirect_sg_entries, uint, 0444);
78 MODULE_PARM_DESC(indirect_sg_entries,
79 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
80
81 module_param(allow_ext_sg, bool, 0444);
82 MODULE_PARM_DESC(allow_ext_sg,
83 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
84
85 module_param(topspin_workarounds, int, 0444);
86 MODULE_PARM_DESC(topspin_workarounds,
87 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
88
89 static void srp_add_one(struct ib_device *device);
90 static void srp_remove_one(struct ib_device *device);
91 static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
92 static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
93 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
94
95 static struct scsi_transport_template *ib_srp_transport_template;
96 static struct workqueue_struct *srp_remove_wq;
97
98 static struct ib_client srp_client = {
99 .name = "srp",
100 .add = srp_add_one,
101 .remove = srp_remove_one
102 };
103
104 static struct ib_sa_client srp_sa_client;
105
106 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
107 {
108 return (struct srp_target_port *) host->hostdata;
109 }
110
111 static const char *srp_target_info(struct Scsi_Host *host)
112 {
113 return host_to_target(host)->target_name;
114 }
115
116 static int srp_target_is_topspin(struct srp_target_port *target)
117 {
118 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
119 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
120
121 return topspin_workarounds &&
122 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
123 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
124 }
125
126 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
127 gfp_t gfp_mask,
128 enum dma_data_direction direction)
129 {
130 struct srp_iu *iu;
131
132 iu = kmalloc(sizeof *iu, gfp_mask);
133 if (!iu)
134 goto out;
135
136 iu->buf = kzalloc(size, gfp_mask);
137 if (!iu->buf)
138 goto out_free_iu;
139
140 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
141 direction);
142 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
143 goto out_free_buf;
144
145 iu->size = size;
146 iu->direction = direction;
147
148 return iu;
149
150 out_free_buf:
151 kfree(iu->buf);
152 out_free_iu:
153 kfree(iu);
154 out:
155 return NULL;
156 }
157
158 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
159 {
160 if (!iu)
161 return;
162
163 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
164 iu->direction);
165 kfree(iu->buf);
166 kfree(iu);
167 }
168
169 static void srp_qp_event(struct ib_event *event, void *context)
170 {
171 pr_debug("QP event %d\n", event->event);
172 }
173
174 static int srp_init_qp(struct srp_target_port *target,
175 struct ib_qp *qp)
176 {
177 struct ib_qp_attr *attr;
178 int ret;
179
180 attr = kmalloc(sizeof *attr, GFP_KERNEL);
181 if (!attr)
182 return -ENOMEM;
183
184 ret = ib_find_pkey(target->srp_host->srp_dev->dev,
185 target->srp_host->port,
186 be16_to_cpu(target->path.pkey),
187 &attr->pkey_index);
188 if (ret)
189 goto out;
190
191 attr->qp_state = IB_QPS_INIT;
192 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
193 IB_ACCESS_REMOTE_WRITE);
194 attr->port_num = target->srp_host->port;
195
196 ret = ib_modify_qp(qp, attr,
197 IB_QP_STATE |
198 IB_QP_PKEY_INDEX |
199 IB_QP_ACCESS_FLAGS |
200 IB_QP_PORT);
201
202 out:
203 kfree(attr);
204 return ret;
205 }
206
207 static int srp_new_cm_id(struct srp_target_port *target)
208 {
209 struct ib_cm_id *new_cm_id;
210
211 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
212 srp_cm_handler, target);
213 if (IS_ERR(new_cm_id))
214 return PTR_ERR(new_cm_id);
215
216 if (target->cm_id)
217 ib_destroy_cm_id(target->cm_id);
218 target->cm_id = new_cm_id;
219
220 return 0;
221 }
222
223 static int srp_create_target_ib(struct srp_target_port *target)
224 {
225 struct ib_qp_init_attr *init_attr;
226 struct ib_cq *recv_cq, *send_cq;
227 struct ib_qp *qp;
228 int ret;
229
230 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
231 if (!init_attr)
232 return -ENOMEM;
233
234 recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
235 srp_recv_completion, NULL, target, SRP_RQ_SIZE, 0);
236 if (IS_ERR(recv_cq)) {
237 ret = PTR_ERR(recv_cq);
238 goto err;
239 }
240
241 send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
242 srp_send_completion, NULL, target, SRP_SQ_SIZE, 0);
243 if (IS_ERR(send_cq)) {
244 ret = PTR_ERR(send_cq);
245 goto err_recv_cq;
246 }
247
248 ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
249
250 init_attr->event_handler = srp_qp_event;
251 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
252 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
253 init_attr->cap.max_recv_sge = 1;
254 init_attr->cap.max_send_sge = 1;
255 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
256 init_attr->qp_type = IB_QPT_RC;
257 init_attr->send_cq = send_cq;
258 init_attr->recv_cq = recv_cq;
259
260 qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
261 if (IS_ERR(qp)) {
262 ret = PTR_ERR(qp);
263 goto err_send_cq;
264 }
265
266 ret = srp_init_qp(target, qp);
267 if (ret)
268 goto err_qp;
269
270 if (target->qp)
271 ib_destroy_qp(target->qp);
272 if (target->recv_cq)
273 ib_destroy_cq(target->recv_cq);
274 if (target->send_cq)
275 ib_destroy_cq(target->send_cq);
276
277 target->qp = qp;
278 target->recv_cq = recv_cq;
279 target->send_cq = send_cq;
280
281 kfree(init_attr);
282 return 0;
283
284 err_qp:
285 ib_destroy_qp(qp);
286
287 err_send_cq:
288 ib_destroy_cq(send_cq);
289
290 err_recv_cq:
291 ib_destroy_cq(recv_cq);
292
293 err:
294 kfree(init_attr);
295 return ret;
296 }
297
298 static void srp_free_target_ib(struct srp_target_port *target)
299 {
300 int i;
301
302 ib_destroy_qp(target->qp);
303 ib_destroy_cq(target->send_cq);
304 ib_destroy_cq(target->recv_cq);
305
306 target->qp = NULL;
307 target->send_cq = target->recv_cq = NULL;
308
309 for (i = 0; i < SRP_RQ_SIZE; ++i)
310 srp_free_iu(target->srp_host, target->rx_ring[i]);
311 for (i = 0; i < SRP_SQ_SIZE; ++i)
312 srp_free_iu(target->srp_host, target->tx_ring[i]);
313 }
314
315 static void srp_path_rec_completion(int status,
316 struct ib_sa_path_rec *pathrec,
317 void *target_ptr)
318 {
319 struct srp_target_port *target = target_ptr;
320
321 target->status = status;
322 if (status)
323 shost_printk(KERN_ERR, target->scsi_host,
324 PFX "Got failed path rec status %d\n", status);
325 else
326 target->path = *pathrec;
327 complete(&target->done);
328 }
329
330 static int srp_lookup_path(struct srp_target_port *target)
331 {
332 target->path.numb_path = 1;
333
334 init_completion(&target->done);
335
336 target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
337 target->srp_host->srp_dev->dev,
338 target->srp_host->port,
339 &target->path,
340 IB_SA_PATH_REC_SERVICE_ID |
341 IB_SA_PATH_REC_DGID |
342 IB_SA_PATH_REC_SGID |
343 IB_SA_PATH_REC_NUMB_PATH |
344 IB_SA_PATH_REC_PKEY,
345 SRP_PATH_REC_TIMEOUT_MS,
346 GFP_KERNEL,
347 srp_path_rec_completion,
348 target, &target->path_query);
349 if (target->path_query_id < 0)
350 return target->path_query_id;
351
352 wait_for_completion(&target->done);
353
354 if (target->status < 0)
355 shost_printk(KERN_WARNING, target->scsi_host,
356 PFX "Path record query failed\n");
357
358 return target->status;
359 }
360
361 static int srp_send_req(struct srp_target_port *target)
362 {
363 struct {
364 struct ib_cm_req_param param;
365 struct srp_login_req priv;
366 } *req = NULL;
367 int status;
368
369 req = kzalloc(sizeof *req, GFP_KERNEL);
370 if (!req)
371 return -ENOMEM;
372
373 req->param.primary_path = &target->path;
374 req->param.alternate_path = NULL;
375 req->param.service_id = target->service_id;
376 req->param.qp_num = target->qp->qp_num;
377 req->param.qp_type = target->qp->qp_type;
378 req->param.private_data = &req->priv;
379 req->param.private_data_len = sizeof req->priv;
380 req->param.flow_control = 1;
381
382 get_random_bytes(&req->param.starting_psn, 4);
383 req->param.starting_psn &= 0xffffff;
384
385 /*
386 * Pick some arbitrary defaults here; we could make these
387 * module parameters if anyone cared about setting them.
388 */
389 req->param.responder_resources = 4;
390 req->param.remote_cm_response_timeout = 20;
391 req->param.local_cm_response_timeout = 20;
392 req->param.retry_count = 7;
393 req->param.rnr_retry_count = 7;
394 req->param.max_cm_retries = 15;
395
396 req->priv.opcode = SRP_LOGIN_REQ;
397 req->priv.tag = 0;
398 req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
399 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
400 SRP_BUF_FORMAT_INDIRECT);
401 /*
402 * In the published SRP specification (draft rev. 16a), the
403 * port identifier format is 8 bytes of ID extension followed
404 * by 8 bytes of GUID. Older drafts put the two halves in the
405 * opposite order, so that the GUID comes first.
406 *
407 * Targets conforming to these obsolete drafts can be
408 * recognized by the I/O Class they report.
409 */
410 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
411 memcpy(req->priv.initiator_port_id,
412 &target->path.sgid.global.interface_id, 8);
413 memcpy(req->priv.initiator_port_id + 8,
414 &target->initiator_ext, 8);
415 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
416 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
417 } else {
418 memcpy(req->priv.initiator_port_id,
419 &target->initiator_ext, 8);
420 memcpy(req->priv.initiator_port_id + 8,
421 &target->path.sgid.global.interface_id, 8);
422 memcpy(req->priv.target_port_id, &target->id_ext, 8);
423 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
424 }
425
426 /*
427 * Topspin/Cisco SRP targets will reject our login unless we
428 * zero out the first 8 bytes of our initiator port ID and set
429 * the second 8 bytes to the local node GUID.
430 */
431 if (srp_target_is_topspin(target)) {
432 shost_printk(KERN_DEBUG, target->scsi_host,
433 PFX "Topspin/Cisco initiator port ID workaround "
434 "activated for target GUID %016llx\n",
435 (unsigned long long) be64_to_cpu(target->ioc_guid));
436 memset(req->priv.initiator_port_id, 0, 8);
437 memcpy(req->priv.initiator_port_id + 8,
438 &target->srp_host->srp_dev->dev->node_guid, 8);
439 }
440
441 status = ib_send_cm_req(target->cm_id, &req->param);
442
443 kfree(req);
444
445 return status;
446 }
447
448 static bool srp_queue_remove_work(struct srp_target_port *target)
449 {
450 bool changed = false;
451
452 spin_lock_irq(&target->lock);
453 if (target->state != SRP_TARGET_REMOVED) {
454 target->state = SRP_TARGET_REMOVED;
455 changed = true;
456 }
457 spin_unlock_irq(&target->lock);
458
459 if (changed)
460 queue_work(srp_remove_wq, &target->remove_work);
461
462 return changed;
463 }
464
465 static bool srp_change_conn_state(struct srp_target_port *target,
466 bool connected)
467 {
468 bool changed = false;
469
470 spin_lock_irq(&target->lock);
471 if (target->connected != connected) {
472 target->connected = connected;
473 changed = true;
474 }
475 spin_unlock_irq(&target->lock);
476
477 return changed;
478 }
479
480 static void srp_disconnect_target(struct srp_target_port *target)
481 {
482 if (srp_change_conn_state(target, false)) {
483 /* XXX should send SRP_I_LOGOUT request */
484
485 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
486 shost_printk(KERN_DEBUG, target->scsi_host,
487 PFX "Sending CM DREQ failed\n");
488 }
489 }
490 }
491
492 static void srp_free_req_data(struct srp_target_port *target)
493 {
494 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
495 struct srp_request *req;
496 int i;
497
498 for (i = 0, req = target->req_ring; i < SRP_CMD_SQ_SIZE; ++i, ++req) {
499 kfree(req->fmr_list);
500 kfree(req->map_page);
501 if (req->indirect_dma_addr) {
502 ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
503 target->indirect_size,
504 DMA_TO_DEVICE);
505 }
506 kfree(req->indirect_desc);
507 }
508 }
509
510 /**
511 * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
512 * @shost: SCSI host whose attributes to remove from sysfs.
513 *
514 * Note: Any attributes defined in the host template and that did not exist
515 * before invocation of this function will be ignored.
516 */
517 static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
518 {
519 struct device_attribute **attr;
520
521 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
522 device_remove_file(&shost->shost_dev, *attr);
523 }
524
525 static void srp_remove_target(struct srp_target_port *target)
526 {
527 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
528
529 srp_del_scsi_host_attr(target->scsi_host);
530 srp_remove_host(target->scsi_host);
531 scsi_remove_host(target->scsi_host);
532 srp_disconnect_target(target);
533 ib_destroy_cm_id(target->cm_id);
534 srp_free_target_ib(target);
535 srp_free_req_data(target);
536 scsi_host_put(target->scsi_host);
537 }
538
539 static void srp_remove_work(struct work_struct *work)
540 {
541 struct srp_target_port *target =
542 container_of(work, struct srp_target_port, remove_work);
543
544 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
545
546 spin_lock(&target->srp_host->target_lock);
547 list_del(&target->list);
548 spin_unlock(&target->srp_host->target_lock);
549
550 srp_remove_target(target);
551 }
552
553 static void srp_rport_delete(struct srp_rport *rport)
554 {
555 struct srp_target_port *target = rport->lld_data;
556
557 srp_queue_remove_work(target);
558 }
559
560 static int srp_connect_target(struct srp_target_port *target)
561 {
562 int retries = 3;
563 int ret;
564
565 WARN_ON_ONCE(target->connected);
566
567 target->qp_in_error = false;
568
569 ret = srp_lookup_path(target);
570 if (ret)
571 return ret;
572
573 while (1) {
574 init_completion(&target->done);
575 ret = srp_send_req(target);
576 if (ret)
577 return ret;
578 wait_for_completion(&target->done);
579
580 /*
581 * The CM event handling code will set status to
582 * SRP_PORT_REDIRECT if we get a port redirect REJ
583 * back, or SRP_DLID_REDIRECT if we get a lid/qp
584 * redirect REJ back.
585 */
586 switch (target->status) {
587 case 0:
588 srp_change_conn_state(target, true);
589 return 0;
590
591 case SRP_PORT_REDIRECT:
592 ret = srp_lookup_path(target);
593 if (ret)
594 return ret;
595 break;
596
597 case SRP_DLID_REDIRECT:
598 break;
599
600 case SRP_STALE_CONN:
601 /* Our current CM id was stale, and is now in timewait.
602 * Try to reconnect with a new one.
603 */
604 if (!retries-- || srp_new_cm_id(target)) {
605 shost_printk(KERN_ERR, target->scsi_host, PFX
606 "giving up on stale connection\n");
607 target->status = -ECONNRESET;
608 return target->status;
609 }
610
611 shost_printk(KERN_ERR, target->scsi_host, PFX
612 "retrying stale connection\n");
613 break;
614
615 default:
616 return target->status;
617 }
618 }
619 }
620
621 static void srp_unmap_data(struct scsi_cmnd *scmnd,
622 struct srp_target_port *target,
623 struct srp_request *req)
624 {
625 struct ib_device *ibdev = target->srp_host->srp_dev->dev;
626 struct ib_pool_fmr **pfmr;
627
628 if (!scsi_sglist(scmnd) ||
629 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
630 scmnd->sc_data_direction != DMA_FROM_DEVICE))
631 return;
632
633 pfmr = req->fmr_list;
634 while (req->nfmr--)
635 ib_fmr_pool_unmap(*pfmr++);
636
637 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
638 scmnd->sc_data_direction);
639 }
640
641 /**
642 * srp_claim_req - Take ownership of the scmnd associated with a request.
643 * @target: SRP target port.
644 * @req: SRP request.
645 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
646 * ownership of @req->scmnd if it equals @scmnd.
647 *
648 * Return value:
649 * Either NULL or a pointer to the SCSI command the caller became owner of.
650 */
651 static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
652 struct srp_request *req,
653 struct scsi_cmnd *scmnd)
654 {
655 unsigned long flags;
656
657 spin_lock_irqsave(&target->lock, flags);
658 if (!scmnd) {
659 scmnd = req->scmnd;
660 req->scmnd = NULL;
661 } else if (req->scmnd == scmnd) {
662 req->scmnd = NULL;
663 } else {
664 scmnd = NULL;
665 }
666 spin_unlock_irqrestore(&target->lock, flags);
667
668 return scmnd;
669 }
670
671 /**
672 * srp_free_req() - Unmap data and add request to the free request list.
673 */
674 static void srp_free_req(struct srp_target_port *target,
675 struct srp_request *req, struct scsi_cmnd *scmnd,
676 s32 req_lim_delta)
677 {
678 unsigned long flags;
679
680 srp_unmap_data(scmnd, target, req);
681
682 spin_lock_irqsave(&target->lock, flags);
683 target->req_lim += req_lim_delta;
684 list_add_tail(&req->list, &target->free_reqs);
685 spin_unlock_irqrestore(&target->lock, flags);
686 }
687
688 static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
689 {
690 struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL);
691
692 if (scmnd) {
693 srp_free_req(target, req, scmnd, 0);
694 scmnd->result = DID_RESET << 16;
695 scmnd->scsi_done(scmnd);
696 }
697 }
698
699 static int srp_reconnect_target(struct srp_target_port *target)
700 {
701 struct Scsi_Host *shost = target->scsi_host;
702 int i, ret;
703
704 scsi_target_block(&shost->shost_gendev);
705
706 srp_disconnect_target(target);
707 /*
708 * Now get a new local CM ID so that we avoid confusing the target in
709 * case things are really fouled up. Doing so also ensures that all CM
710 * callbacks will have finished before a new QP is allocated.
711 */
712 ret = srp_new_cm_id(target);
713 /*
714 * Whether or not creating a new CM ID succeeded, create a new
715 * QP. This guarantees that all completion callback function
716 * invocations have finished before request resetting starts.
717 */
718 if (ret == 0)
719 ret = srp_create_target_ib(target);
720 else
721 srp_create_target_ib(target);
722
723 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
724 struct srp_request *req = &target->req_ring[i];
725 if (req->scmnd)
726 srp_reset_req(target, req);
727 }
728
729 INIT_LIST_HEAD(&target->free_tx);
730 for (i = 0; i < SRP_SQ_SIZE; ++i)
731 list_add(&target->tx_ring[i]->list, &target->free_tx);
732
733 if (ret == 0)
734 ret = srp_connect_target(target);
735
736 scsi_target_unblock(&shost->shost_gendev, ret == 0 ? SDEV_RUNNING :
737 SDEV_TRANSPORT_OFFLINE);
738 target->transport_offline = !!ret;
739
740 if (ret)
741 goto err;
742
743 shost_printk(KERN_INFO, target->scsi_host, PFX "reconnect succeeded\n");
744
745 return ret;
746
747 err:
748 shost_printk(KERN_ERR, target->scsi_host,
749 PFX "reconnect failed (%d), removing target port.\n", ret);
750
751 /*
752 * We couldn't reconnect, so kill our target port off.
753 * However, we have to defer the real removal because we
754 * are in the context of the SCSI error handler now, which
755 * will deadlock if we call scsi_remove_host().
756 */
757 srp_queue_remove_work(target);
758
759 return ret;
760 }
761
762 static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
763 unsigned int dma_len, u32 rkey)
764 {
765 struct srp_direct_buf *desc = state->desc;
766
767 desc->va = cpu_to_be64(dma_addr);
768 desc->key = cpu_to_be32(rkey);
769 desc->len = cpu_to_be32(dma_len);
770
771 state->total_len += dma_len;
772 state->desc++;
773 state->ndesc++;
774 }
775
776 static int srp_map_finish_fmr(struct srp_map_state *state,
777 struct srp_target_port *target)
778 {
779 struct srp_device *dev = target->srp_host->srp_dev;
780 struct ib_pool_fmr *fmr;
781 u64 io_addr = 0;
782
783 if (!state->npages)
784 return 0;
785
786 if (state->npages == 1) {
787 srp_map_desc(state, state->base_dma_addr, state->fmr_len,
788 target->rkey);
789 state->npages = state->fmr_len = 0;
790 return 0;
791 }
792
793 fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
794 state->npages, io_addr);
795 if (IS_ERR(fmr))
796 return PTR_ERR(fmr);
797
798 *state->next_fmr++ = fmr;
799 state->nfmr++;
800
801 srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
802 state->npages = state->fmr_len = 0;
803 return 0;
804 }
805
806 static void srp_map_update_start(struct srp_map_state *state,
807 struct scatterlist *sg, int sg_index,
808 dma_addr_t dma_addr)
809 {
810 state->unmapped_sg = sg;
811 state->unmapped_index = sg_index;
812 state->unmapped_addr = dma_addr;
813 }
814
815 static int srp_map_sg_entry(struct srp_map_state *state,
816 struct srp_target_port *target,
817 struct scatterlist *sg, int sg_index,
818 int use_fmr)
819 {
820 struct srp_device *dev = target->srp_host->srp_dev;
821 struct ib_device *ibdev = dev->dev;
822 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
823 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
824 unsigned int len;
825 int ret;
826
827 if (!dma_len)
828 return 0;
829
830 if (use_fmr == SRP_MAP_NO_FMR) {
831 /* Once we're in direct map mode for a request, we don't
832 * go back to FMR mode, so no need to update anything
833 * other than the descriptor.
834 */
835 srp_map_desc(state, dma_addr, dma_len, target->rkey);
836 return 0;
837 }
838
839 /* If we start at an offset into the FMR page, don't merge into
840 * the current FMR. Finish it out, and use the kernel's MR for this
841 * sg entry. This is to avoid potential bugs on some SRP targets
842 * that were never quite defined, but went away when the initiator
843 * avoided using FMR on such page fragments.
844 */
845 if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
846 ret = srp_map_finish_fmr(state, target);
847 if (ret)
848 return ret;
849
850 srp_map_desc(state, dma_addr, dma_len, target->rkey);
851 srp_map_update_start(state, NULL, 0, 0);
852 return 0;
853 }
854
855 /* If this is the first sg to go into the FMR, save our position.
856 * We need to know the first unmapped entry, its index, and the
857 * first unmapped address within that entry to be able to restart
858 * mapping after an error.
859 */
860 if (!state->unmapped_sg)
861 srp_map_update_start(state, sg, sg_index, dma_addr);
862
863 while (dma_len) {
864 if (state->npages == SRP_FMR_SIZE) {
865 ret = srp_map_finish_fmr(state, target);
866 if (ret)
867 return ret;
868
869 srp_map_update_start(state, sg, sg_index, dma_addr);
870 }
871
872 len = min_t(unsigned int, dma_len, dev->fmr_page_size);
873
874 if (!state->npages)
875 state->base_dma_addr = dma_addr;
876 state->pages[state->npages++] = dma_addr;
877 state->fmr_len += len;
878 dma_addr += len;
879 dma_len -= len;
880 }
881
882 /* If the last entry of the FMR wasn't a full page, then we need to
883 * close it out and start a new one -- we can only merge at page
884 * boundries.
885 */
886 ret = 0;
887 if (len != dev->fmr_page_size) {
888 ret = srp_map_finish_fmr(state, target);
889 if (!ret)
890 srp_map_update_start(state, NULL, 0, 0);
891 }
892 return ret;
893 }
894
895 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
896 struct srp_request *req)
897 {
898 struct scatterlist *scat, *sg;
899 struct srp_cmd *cmd = req->cmd->buf;
900 int i, len, nents, count, use_fmr;
901 struct srp_device *dev;
902 struct ib_device *ibdev;
903 struct srp_map_state state;
904 struct srp_indirect_buf *indirect_hdr;
905 u32 table_len;
906 u8 fmt;
907
908 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
909 return sizeof (struct srp_cmd);
910
911 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
912 scmnd->sc_data_direction != DMA_TO_DEVICE) {
913 shost_printk(KERN_WARNING, target->scsi_host,
914 PFX "Unhandled data direction %d\n",
915 scmnd->sc_data_direction);
916 return -EINVAL;
917 }
918
919 nents = scsi_sg_count(scmnd);
920 scat = scsi_sglist(scmnd);
921
922 dev = target->srp_host->srp_dev;
923 ibdev = dev->dev;
924
925 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
926 if (unlikely(count == 0))
927 return -EIO;
928
929 fmt = SRP_DATA_DESC_DIRECT;
930 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
931
932 if (count == 1) {
933 /*
934 * The midlayer only generated a single gather/scatter
935 * entry, or DMA mapping coalesced everything to a
936 * single entry. So a direct descriptor along with
937 * the DMA MR suffices.
938 */
939 struct srp_direct_buf *buf = (void *) cmd->add_data;
940
941 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
942 buf->key = cpu_to_be32(target->rkey);
943 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
944
945 req->nfmr = 0;
946 goto map_complete;
947 }
948
949 /* We have more than one scatter/gather entry, so build our indirect
950 * descriptor table, trying to merge as many entries with FMR as we
951 * can.
952 */
953 indirect_hdr = (void *) cmd->add_data;
954
955 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
956 target->indirect_size, DMA_TO_DEVICE);
957
958 memset(&state, 0, sizeof(state));
959 state.desc = req->indirect_desc;
960 state.pages = req->map_page;
961 state.next_fmr = req->fmr_list;
962
963 use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
964
965 for_each_sg(scat, sg, count, i) {
966 if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
967 /* FMR mapping failed, so backtrack to the first
968 * unmapped entry and continue on without using FMR.
969 */
970 dma_addr_t dma_addr;
971 unsigned int dma_len;
972
973 backtrack:
974 sg = state.unmapped_sg;
975 i = state.unmapped_index;
976
977 dma_addr = ib_sg_dma_address(ibdev, sg);
978 dma_len = ib_sg_dma_len(ibdev, sg);
979 dma_len -= (state.unmapped_addr - dma_addr);
980 dma_addr = state.unmapped_addr;
981 use_fmr = SRP_MAP_NO_FMR;
982 srp_map_desc(&state, dma_addr, dma_len, target->rkey);
983 }
984 }
985
986 if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
987 goto backtrack;
988
989 /* We've mapped the request, now pull as much of the indirect
990 * descriptor table as we can into the command buffer. If this
991 * target is not using an external indirect table, we are
992 * guaranteed to fit into the command, as the SCSI layer won't
993 * give us more S/G entries than we allow.
994 */
995 req->nfmr = state.nfmr;
996 if (state.ndesc == 1) {
997 /* FMR mapping was able to collapse this to one entry,
998 * so use a direct descriptor.
999 */
1000 struct srp_direct_buf *buf = (void *) cmd->add_data;
1001
1002 *buf = req->indirect_desc[0];
1003 goto map_complete;
1004 }
1005
1006 if (unlikely(target->cmd_sg_cnt < state.ndesc &&
1007 !target->allow_ext_sg)) {
1008 shost_printk(KERN_ERR, target->scsi_host,
1009 "Could not fit S/G list into SRP_CMD\n");
1010 return -EIO;
1011 }
1012
1013 count = min(state.ndesc, target->cmd_sg_cnt);
1014 table_len = state.ndesc * sizeof (struct srp_direct_buf);
1015
1016 fmt = SRP_DATA_DESC_INDIRECT;
1017 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
1018 len += count * sizeof (struct srp_direct_buf);
1019
1020 memcpy(indirect_hdr->desc_list, req->indirect_desc,
1021 count * sizeof (struct srp_direct_buf));
1022
1023 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
1024 indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
1025 indirect_hdr->table_desc.len = cpu_to_be32(table_len);
1026 indirect_hdr->len = cpu_to_be32(state.total_len);
1027
1028 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1029 cmd->data_out_desc_cnt = count;
1030 else
1031 cmd->data_in_desc_cnt = count;
1032
1033 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
1034 DMA_TO_DEVICE);
1035
1036 map_complete:
1037 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
1038 cmd->buf_fmt = fmt << 4;
1039 else
1040 cmd->buf_fmt = fmt;
1041
1042 return len;
1043 }
1044
1045 /*
1046 * Return an IU and possible credit to the free pool
1047 */
1048 static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
1049 enum srp_iu_type iu_type)
1050 {
1051 unsigned long flags;
1052
1053 spin_lock_irqsave(&target->lock, flags);
1054 list_add(&iu->list, &target->free_tx);
1055 if (iu_type != SRP_IU_RSP)
1056 ++target->req_lim;
1057 spin_unlock_irqrestore(&target->lock, flags);
1058 }
1059
1060 /*
1061 * Must be called with target->lock held to protect req_lim and free_tx.
1062 * If IU is not sent, it must be returned using srp_put_tx_iu().
1063 *
1064 * Note:
1065 * An upper limit for the number of allocated information units for each
1066 * request type is:
1067 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
1068 * more than Scsi_Host.can_queue requests.
1069 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
1070 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
1071 * one unanswered SRP request to an initiator.
1072 */
1073 static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
1074 enum srp_iu_type iu_type)
1075 {
1076 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
1077 struct srp_iu *iu;
1078
1079 srp_send_completion(target->send_cq, target);
1080
1081 if (list_empty(&target->free_tx))
1082 return NULL;
1083
1084 /* Initiator responses to target requests do not consume credits */
1085 if (iu_type != SRP_IU_RSP) {
1086 if (target->req_lim <= rsv) {
1087 ++target->zero_req_lim;
1088 return NULL;
1089 }
1090
1091 --target->req_lim;
1092 }
1093
1094 iu = list_first_entry(&target->free_tx, struct srp_iu, list);
1095 list_del(&iu->list);
1096 return iu;
1097 }
1098
1099 static int srp_post_send(struct srp_target_port *target,
1100 struct srp_iu *iu, int len)
1101 {
1102 struct ib_sge list;
1103 struct ib_send_wr wr, *bad_wr;
1104
1105 list.addr = iu->dma;
1106 list.length = len;
1107 list.lkey = target->lkey;
1108
1109 wr.next = NULL;
1110 wr.wr_id = (uintptr_t) iu;
1111 wr.sg_list = &list;
1112 wr.num_sge = 1;
1113 wr.opcode = IB_WR_SEND;
1114 wr.send_flags = IB_SEND_SIGNALED;
1115
1116 return ib_post_send(target->qp, &wr, &bad_wr);
1117 }
1118
1119 static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
1120 {
1121 struct ib_recv_wr wr, *bad_wr;
1122 struct ib_sge list;
1123
1124 list.addr = iu->dma;
1125 list.length = iu->size;
1126 list.lkey = target->lkey;
1127
1128 wr.next = NULL;
1129 wr.wr_id = (uintptr_t) iu;
1130 wr.sg_list = &list;
1131 wr.num_sge = 1;
1132
1133 return ib_post_recv(target->qp, &wr, &bad_wr);
1134 }
1135
1136 static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
1137 {
1138 struct srp_request *req;
1139 struct scsi_cmnd *scmnd;
1140 unsigned long flags;
1141
1142 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
1143 spin_lock_irqsave(&target->lock, flags);
1144 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1145 spin_unlock_irqrestore(&target->lock, flags);
1146
1147 target->tsk_mgmt_status = -1;
1148 if (be32_to_cpu(rsp->resp_data_len) >= 4)
1149 target->tsk_mgmt_status = rsp->data[3];
1150 complete(&target->tsk_mgmt_done);
1151 } else {
1152 req = &target->req_ring[rsp->tag];
1153 scmnd = srp_claim_req(target, req, NULL);
1154 if (!scmnd) {
1155 shost_printk(KERN_ERR, target->scsi_host,
1156 "Null scmnd for RSP w/tag %016llx\n",
1157 (unsigned long long) rsp->tag);
1158
1159 spin_lock_irqsave(&target->lock, flags);
1160 target->req_lim += be32_to_cpu(rsp->req_lim_delta);
1161 spin_unlock_irqrestore(&target->lock, flags);
1162
1163 return;
1164 }
1165 scmnd->result = rsp->status;
1166
1167 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
1168 memcpy(scmnd->sense_buffer, rsp->data +
1169 be32_to_cpu(rsp->resp_data_len),
1170 min_t(int, be32_to_cpu(rsp->sense_data_len),
1171 SCSI_SENSE_BUFFERSIZE));
1172 }
1173
1174 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
1175 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
1176 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
1177 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
1178
1179 srp_free_req(target, req, scmnd,
1180 be32_to_cpu(rsp->req_lim_delta));
1181
1182 scmnd->host_scribble = NULL;
1183 scmnd->scsi_done(scmnd);
1184 }
1185 }
1186
1187 static int srp_response_common(struct srp_target_port *target, s32 req_delta,
1188 void *rsp, int len)
1189 {
1190 struct ib_device *dev = target->srp_host->srp_dev->dev;
1191 unsigned long flags;
1192 struct srp_iu *iu;
1193 int err;
1194
1195 spin_lock_irqsave(&target->lock, flags);
1196 target->req_lim += req_delta;
1197 iu = __srp_get_tx_iu(target, SRP_IU_RSP);
1198 spin_unlock_irqrestore(&target->lock, flags);
1199
1200 if (!iu) {
1201 shost_printk(KERN_ERR, target->scsi_host, PFX
1202 "no IU available to send response\n");
1203 return 1;
1204 }
1205
1206 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
1207 memcpy(iu->buf, rsp, len);
1208 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
1209
1210 err = srp_post_send(target, iu, len);
1211 if (err) {
1212 shost_printk(KERN_ERR, target->scsi_host, PFX
1213 "unable to post response: %d\n", err);
1214 srp_put_tx_iu(target, iu, SRP_IU_RSP);
1215 }
1216
1217 return err;
1218 }
1219
1220 static void srp_process_cred_req(struct srp_target_port *target,
1221 struct srp_cred_req *req)
1222 {
1223 struct srp_cred_rsp rsp = {
1224 .opcode = SRP_CRED_RSP,
1225 .tag = req->tag,
1226 };
1227 s32 delta = be32_to_cpu(req->req_lim_delta);
1228
1229 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1230 shost_printk(KERN_ERR, target->scsi_host, PFX
1231 "problems processing SRP_CRED_REQ\n");
1232 }
1233
1234 static void srp_process_aer_req(struct srp_target_port *target,
1235 struct srp_aer_req *req)
1236 {
1237 struct srp_aer_rsp rsp = {
1238 .opcode = SRP_AER_RSP,
1239 .tag = req->tag,
1240 };
1241 s32 delta = be32_to_cpu(req->req_lim_delta);
1242
1243 shost_printk(KERN_ERR, target->scsi_host, PFX
1244 "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
1245
1246 if (srp_response_common(target, delta, &rsp, sizeof rsp))
1247 shost_printk(KERN_ERR, target->scsi_host, PFX
1248 "problems processing SRP_AER_REQ\n");
1249 }
1250
1251 static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
1252 {
1253 struct ib_device *dev = target->srp_host->srp_dev->dev;
1254 struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
1255 int res;
1256 u8 opcode;
1257
1258 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
1259 DMA_FROM_DEVICE);
1260
1261 opcode = *(u8 *) iu->buf;
1262
1263 if (0) {
1264 shost_printk(KERN_ERR, target->scsi_host,
1265 PFX "recv completion, opcode 0x%02x\n", opcode);
1266 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
1267 iu->buf, wc->byte_len, true);
1268 }
1269
1270 switch (opcode) {
1271 case SRP_RSP:
1272 srp_process_rsp(target, iu->buf);
1273 break;
1274
1275 case SRP_CRED_REQ:
1276 srp_process_cred_req(target, iu->buf);
1277 break;
1278
1279 case SRP_AER_REQ:
1280 srp_process_aer_req(target, iu->buf);
1281 break;
1282
1283 case SRP_T_LOGOUT:
1284 /* XXX Handle target logout */
1285 shost_printk(KERN_WARNING, target->scsi_host,
1286 PFX "Got target logout request\n");
1287 break;
1288
1289 default:
1290 shost_printk(KERN_WARNING, target->scsi_host,
1291 PFX "Unhandled SRP opcode 0x%02x\n", opcode);
1292 break;
1293 }
1294
1295 ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
1296 DMA_FROM_DEVICE);
1297
1298 res = srp_post_recv(target, iu);
1299 if (res != 0)
1300 shost_printk(KERN_ERR, target->scsi_host,
1301 PFX "Recv failed with error code %d\n", res);
1302 }
1303
1304 static void srp_handle_qp_err(enum ib_wc_status wc_status, bool send_err,
1305 struct srp_target_port *target)
1306 {
1307 if (target->connected && !target->qp_in_error) {
1308 shost_printk(KERN_ERR, target->scsi_host,
1309 PFX "failed %s status %d\n",
1310 send_err ? "send" : "receive",
1311 wc_status);
1312 }
1313 target->qp_in_error = true;
1314 }
1315
1316 static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
1317 {
1318 struct srp_target_port *target = target_ptr;
1319 struct ib_wc wc;
1320
1321 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1322 while (ib_poll_cq(cq, 1, &wc) > 0) {
1323 if (likely(wc.status == IB_WC_SUCCESS)) {
1324 srp_handle_recv(target, &wc);
1325 } else {
1326 srp_handle_qp_err(wc.status, false, target);
1327 }
1328 }
1329 }
1330
1331 static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
1332 {
1333 struct srp_target_port *target = target_ptr;
1334 struct ib_wc wc;
1335 struct srp_iu *iu;
1336
1337 while (ib_poll_cq(cq, 1, &wc) > 0) {
1338 if (likely(wc.status == IB_WC_SUCCESS)) {
1339 iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
1340 list_add(&iu->list, &target->free_tx);
1341 } else {
1342 srp_handle_qp_err(wc.status, true, target);
1343 }
1344 }
1345 }
1346
1347 static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
1348 {
1349 struct srp_target_port *target = host_to_target(shost);
1350 struct srp_request *req;
1351 struct srp_iu *iu;
1352 struct srp_cmd *cmd;
1353 struct ib_device *dev;
1354 unsigned long flags;
1355 int len;
1356
1357 if (unlikely(target->transport_offline)) {
1358 scmnd->result = DID_NO_CONNECT << 16;
1359 scmnd->scsi_done(scmnd);
1360 return 0;
1361 }
1362
1363 spin_lock_irqsave(&target->lock, flags);
1364 iu = __srp_get_tx_iu(target, SRP_IU_CMD);
1365 if (!iu)
1366 goto err_unlock;
1367
1368 req = list_first_entry(&target->free_reqs, struct srp_request, list);
1369 list_del(&req->list);
1370 spin_unlock_irqrestore(&target->lock, flags);
1371
1372 dev = target->srp_host->srp_dev->dev;
1373 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
1374 DMA_TO_DEVICE);
1375
1376 scmnd->result = 0;
1377 scmnd->host_scribble = (void *) req;
1378
1379 cmd = iu->buf;
1380 memset(cmd, 0, sizeof *cmd);
1381
1382 cmd->opcode = SRP_CMD;
1383 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
1384 cmd->tag = req->index;
1385 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1386
1387 req->scmnd = scmnd;
1388 req->cmd = iu;
1389
1390 len = srp_map_data(scmnd, target, req);
1391 if (len < 0) {
1392 shost_printk(KERN_ERR, target->scsi_host,
1393 PFX "Failed to map data\n");
1394 goto err_iu;
1395 }
1396
1397 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
1398 DMA_TO_DEVICE);
1399
1400 if (srp_post_send(target, iu, len)) {
1401 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
1402 goto err_unmap;
1403 }
1404
1405 return 0;
1406
1407 err_unmap:
1408 srp_unmap_data(scmnd, target, req);
1409
1410 err_iu:
1411 srp_put_tx_iu(target, iu, SRP_IU_CMD);
1412
1413 /*
1414 * Avoid that the loops that iterate over the request ring can
1415 * encounter a dangling SCSI command pointer.
1416 */
1417 req->scmnd = NULL;
1418
1419 spin_lock_irqsave(&target->lock, flags);
1420 list_add(&req->list, &target->free_reqs);
1421
1422 err_unlock:
1423 spin_unlock_irqrestore(&target->lock, flags);
1424
1425 return SCSI_MLQUEUE_HOST_BUSY;
1426 }
1427
1428 static int srp_alloc_iu_bufs(struct srp_target_port *target)
1429 {
1430 int i;
1431
1432 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1433 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1434 target->max_ti_iu_len,
1435 GFP_KERNEL, DMA_FROM_DEVICE);
1436 if (!target->rx_ring[i])
1437 goto err;
1438 }
1439
1440 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1441 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
1442 target->max_iu_len,
1443 GFP_KERNEL, DMA_TO_DEVICE);
1444 if (!target->tx_ring[i])
1445 goto err;
1446
1447 list_add(&target->tx_ring[i]->list, &target->free_tx);
1448 }
1449
1450 return 0;
1451
1452 err:
1453 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1454 srp_free_iu(target->srp_host, target->rx_ring[i]);
1455 target->rx_ring[i] = NULL;
1456 }
1457
1458 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1459 srp_free_iu(target->srp_host, target->tx_ring[i]);
1460 target->tx_ring[i] = NULL;
1461 }
1462
1463 return -ENOMEM;
1464 }
1465
1466 static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
1467 {
1468 uint64_t T_tr_ns, max_compl_time_ms;
1469 uint32_t rq_tmo_jiffies;
1470
1471 /*
1472 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
1473 * table 91), both the QP timeout and the retry count have to be set
1474 * for RC QP's during the RTR to RTS transition.
1475 */
1476 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
1477 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
1478
1479 /*
1480 * Set target->rq_tmo_jiffies to one second more than the largest time
1481 * it can take before an error completion is generated. See also
1482 * C9-140..142 in the IBTA spec for more information about how to
1483 * convert the QP Local ACK Timeout value to nanoseconds.
1484 */
1485 T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
1486 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
1487 do_div(max_compl_time_ms, NSEC_PER_MSEC);
1488 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
1489
1490 return rq_tmo_jiffies;
1491 }
1492
1493 static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
1494 struct srp_login_rsp *lrsp,
1495 struct srp_target_port *target)
1496 {
1497 struct ib_qp_attr *qp_attr = NULL;
1498 int attr_mask = 0;
1499 int ret;
1500 int i;
1501
1502 if (lrsp->opcode == SRP_LOGIN_RSP) {
1503 target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
1504 target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
1505
1506 /*
1507 * Reserve credits for task management so we don't
1508 * bounce requests back to the SCSI mid-layer.
1509 */
1510 target->scsi_host->can_queue
1511 = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
1512 target->scsi_host->can_queue);
1513 } else {
1514 shost_printk(KERN_WARNING, target->scsi_host,
1515 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
1516 ret = -ECONNRESET;
1517 goto error;
1518 }
1519
1520 if (!target->rx_ring[0]) {
1521 ret = srp_alloc_iu_bufs(target);
1522 if (ret)
1523 goto error;
1524 }
1525
1526 ret = -ENOMEM;
1527 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1528 if (!qp_attr)
1529 goto error;
1530
1531 qp_attr->qp_state = IB_QPS_RTR;
1532 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1533 if (ret)
1534 goto error_free;
1535
1536 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1537 if (ret)
1538 goto error_free;
1539
1540 for (i = 0; i < SRP_RQ_SIZE; i++) {
1541 struct srp_iu *iu = target->rx_ring[i];
1542 ret = srp_post_recv(target, iu);
1543 if (ret)
1544 goto error_free;
1545 }
1546
1547 qp_attr->qp_state = IB_QPS_RTS;
1548 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1549 if (ret)
1550 goto error_free;
1551
1552 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
1553
1554 ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
1555 if (ret)
1556 goto error_free;
1557
1558 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1559
1560 error_free:
1561 kfree(qp_attr);
1562
1563 error:
1564 target->status = ret;
1565 }
1566
1567 static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1568 struct ib_cm_event *event,
1569 struct srp_target_port *target)
1570 {
1571 struct Scsi_Host *shost = target->scsi_host;
1572 struct ib_class_port_info *cpi;
1573 int opcode;
1574
1575 switch (event->param.rej_rcvd.reason) {
1576 case IB_CM_REJ_PORT_CM_REDIRECT:
1577 cpi = event->param.rej_rcvd.ari;
1578 target->path.dlid = cpi->redirect_lid;
1579 target->path.pkey = cpi->redirect_pkey;
1580 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1581 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1582
1583 target->status = target->path.dlid ?
1584 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1585 break;
1586
1587 case IB_CM_REJ_PORT_REDIRECT:
1588 if (srp_target_is_topspin(target)) {
1589 /*
1590 * Topspin/Cisco SRP gateways incorrectly send
1591 * reject reason code 25 when they mean 24
1592 * (port redirect).
1593 */
1594 memcpy(target->path.dgid.raw,
1595 event->param.rej_rcvd.ari, 16);
1596
1597 shost_printk(KERN_DEBUG, shost,
1598 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1599 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1600 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1601
1602 target->status = SRP_PORT_REDIRECT;
1603 } else {
1604 shost_printk(KERN_WARNING, shost,
1605 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1606 target->status = -ECONNRESET;
1607 }
1608 break;
1609
1610 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1611 shost_printk(KERN_WARNING, shost,
1612 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1613 target->status = -ECONNRESET;
1614 break;
1615
1616 case IB_CM_REJ_CONSUMER_DEFINED:
1617 opcode = *(u8 *) event->private_data;
1618 if (opcode == SRP_LOGIN_REJ) {
1619 struct srp_login_rej *rej = event->private_data;
1620 u32 reason = be32_to_cpu(rej->reason);
1621
1622 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1623 shost_printk(KERN_WARNING, shost,
1624 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1625 else
1626 shost_printk(KERN_WARNING, shost,
1627 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1628 } else
1629 shost_printk(KERN_WARNING, shost,
1630 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1631 " opcode 0x%02x\n", opcode);
1632 target->status = -ECONNRESET;
1633 break;
1634
1635 case IB_CM_REJ_STALE_CONN:
1636 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
1637 target->status = SRP_STALE_CONN;
1638 break;
1639
1640 default:
1641 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
1642 event->param.rej_rcvd.reason);
1643 target->status = -ECONNRESET;
1644 }
1645 }
1646
1647 static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1648 {
1649 struct srp_target_port *target = cm_id->context;
1650 int comp = 0;
1651
1652 switch (event->event) {
1653 case IB_CM_REQ_ERROR:
1654 shost_printk(KERN_DEBUG, target->scsi_host,
1655 PFX "Sending CM REQ failed\n");
1656 comp = 1;
1657 target->status = -ECONNRESET;
1658 break;
1659
1660 case IB_CM_REP_RECEIVED:
1661 comp = 1;
1662 srp_cm_rep_handler(cm_id, event->private_data, target);
1663 break;
1664
1665 case IB_CM_REJ_RECEIVED:
1666 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
1667 comp = 1;
1668
1669 srp_cm_rej_handler(cm_id, event, target);
1670 break;
1671
1672 case IB_CM_DREQ_RECEIVED:
1673 shost_printk(KERN_WARNING, target->scsi_host,
1674 PFX "DREQ received - connection closed\n");
1675 srp_change_conn_state(target, false);
1676 if (ib_send_cm_drep(cm_id, NULL, 0))
1677 shost_printk(KERN_ERR, target->scsi_host,
1678 PFX "Sending CM DREP failed\n");
1679 break;
1680
1681 case IB_CM_TIMEWAIT_EXIT:
1682 shost_printk(KERN_ERR, target->scsi_host,
1683 PFX "connection closed\n");
1684
1685 target->status = 0;
1686 break;
1687
1688 case IB_CM_MRA_RECEIVED:
1689 case IB_CM_DREQ_ERROR:
1690 case IB_CM_DREP_RECEIVED:
1691 break;
1692
1693 default:
1694 shost_printk(KERN_WARNING, target->scsi_host,
1695 PFX "Unhandled CM event %d\n", event->event);
1696 break;
1697 }
1698
1699 if (comp)
1700 complete(&target->done);
1701
1702 return 0;
1703 }
1704
1705 static int srp_send_tsk_mgmt(struct srp_target_port *target,
1706 u64 req_tag, unsigned int lun, u8 func)
1707 {
1708 struct ib_device *dev = target->srp_host->srp_dev->dev;
1709 struct srp_iu *iu;
1710 struct srp_tsk_mgmt *tsk_mgmt;
1711
1712 if (!target->connected || target->qp_in_error)
1713 return -1;
1714
1715 init_completion(&target->tsk_mgmt_done);
1716
1717 spin_lock_irq(&target->lock);
1718 iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
1719 spin_unlock_irq(&target->lock);
1720
1721 if (!iu)
1722 return -1;
1723
1724 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
1725 DMA_TO_DEVICE);
1726 tsk_mgmt = iu->buf;
1727 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1728
1729 tsk_mgmt->opcode = SRP_TSK_MGMT;
1730 tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
1731 tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
1732 tsk_mgmt->tsk_mgmt_func = func;
1733 tsk_mgmt->task_tag = req_tag;
1734
1735 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
1736 DMA_TO_DEVICE);
1737 if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
1738 srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
1739 return -1;
1740 }
1741
1742 if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
1743 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
1744 return -1;
1745
1746 return 0;
1747 }
1748
1749 static int srp_abort(struct scsi_cmnd *scmnd)
1750 {
1751 struct srp_target_port *target = host_to_target(scmnd->device->host);
1752 struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
1753
1754 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
1755
1756 if (!req || !srp_claim_req(target, req, scmnd))
1757 return FAILED;
1758 srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
1759 SRP_TSK_ABORT_TASK);
1760 srp_free_req(target, req, scmnd, 0);
1761 scmnd->result = DID_ABORT << 16;
1762 scmnd->scsi_done(scmnd);
1763
1764 return SUCCESS;
1765 }
1766
1767 static int srp_reset_device(struct scsi_cmnd *scmnd)
1768 {
1769 struct srp_target_port *target = host_to_target(scmnd->device->host);
1770 int i;
1771
1772 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
1773
1774 if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
1775 SRP_TSK_LUN_RESET))
1776 return FAILED;
1777 if (target->tsk_mgmt_status)
1778 return FAILED;
1779
1780 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
1781 struct srp_request *req = &target->req_ring[i];
1782 if (req->scmnd && req->scmnd->device == scmnd->device)
1783 srp_reset_req(target, req);
1784 }
1785
1786 return SUCCESS;
1787 }
1788
1789 static int srp_reset_host(struct scsi_cmnd *scmnd)
1790 {
1791 struct srp_target_port *target = host_to_target(scmnd->device->host);
1792 int ret = FAILED;
1793
1794 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
1795
1796 if (!srp_reconnect_target(target))
1797 ret = SUCCESS;
1798
1799 return ret;
1800 }
1801
1802 static int srp_slave_configure(struct scsi_device *sdev)
1803 {
1804 struct Scsi_Host *shost = sdev->host;
1805 struct srp_target_port *target = host_to_target(shost);
1806 struct request_queue *q = sdev->request_queue;
1807 unsigned long timeout;
1808
1809 if (sdev->type == TYPE_DISK) {
1810 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
1811 blk_queue_rq_timeout(q, timeout);
1812 }
1813
1814 return 0;
1815 }
1816
1817 static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
1818 char *buf)
1819 {
1820 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1821
1822 return sprintf(buf, "0x%016llx\n",
1823 (unsigned long long) be64_to_cpu(target->id_ext));
1824 }
1825
1826 static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
1827 char *buf)
1828 {
1829 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1830
1831 return sprintf(buf, "0x%016llx\n",
1832 (unsigned long long) be64_to_cpu(target->ioc_guid));
1833 }
1834
1835 static ssize_t show_service_id(struct device *dev,
1836 struct device_attribute *attr, char *buf)
1837 {
1838 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1839
1840 return sprintf(buf, "0x%016llx\n",
1841 (unsigned long long) be64_to_cpu(target->service_id));
1842 }
1843
1844 static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
1845 char *buf)
1846 {
1847 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1848
1849 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1850 }
1851
1852 static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
1853 char *buf)
1854 {
1855 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1856
1857 return sprintf(buf, "%pI6\n", target->path.dgid.raw);
1858 }
1859
1860 static ssize_t show_orig_dgid(struct device *dev,
1861 struct device_attribute *attr, char *buf)
1862 {
1863 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1864
1865 return sprintf(buf, "%pI6\n", target->orig_dgid);
1866 }
1867
1868 static ssize_t show_req_lim(struct device *dev,
1869 struct device_attribute *attr, char *buf)
1870 {
1871 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1872
1873 return sprintf(buf, "%d\n", target->req_lim);
1874 }
1875
1876 static ssize_t show_zero_req_lim(struct device *dev,
1877 struct device_attribute *attr, char *buf)
1878 {
1879 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1880
1881 return sprintf(buf, "%d\n", target->zero_req_lim);
1882 }
1883
1884 static ssize_t show_local_ib_port(struct device *dev,
1885 struct device_attribute *attr, char *buf)
1886 {
1887 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1888
1889 return sprintf(buf, "%d\n", target->srp_host->port);
1890 }
1891
1892 static ssize_t show_local_ib_device(struct device *dev,
1893 struct device_attribute *attr, char *buf)
1894 {
1895 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1896
1897 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
1898 }
1899
1900 static ssize_t show_cmd_sg_entries(struct device *dev,
1901 struct device_attribute *attr, char *buf)
1902 {
1903 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1904
1905 return sprintf(buf, "%u\n", target->cmd_sg_cnt);
1906 }
1907
1908 static ssize_t show_allow_ext_sg(struct device *dev,
1909 struct device_attribute *attr, char *buf)
1910 {
1911 struct srp_target_port *target = host_to_target(class_to_shost(dev));
1912
1913 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
1914 }
1915
1916 static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1917 static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1918 static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1919 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1920 static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1921 static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
1922 static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
1923 static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1924 static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1925 static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
1926 static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
1927 static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
1928
1929 static struct device_attribute *srp_host_attrs[] = {
1930 &dev_attr_id_ext,
1931 &dev_attr_ioc_guid,
1932 &dev_attr_service_id,
1933 &dev_attr_pkey,
1934 &dev_attr_dgid,
1935 &dev_attr_orig_dgid,
1936 &dev_attr_req_lim,
1937 &dev_attr_zero_req_lim,
1938 &dev_attr_local_ib_port,
1939 &dev_attr_local_ib_device,
1940 &dev_attr_cmd_sg_entries,
1941 &dev_attr_allow_ext_sg,
1942 NULL
1943 };
1944
1945 static struct scsi_host_template srp_template = {
1946 .module = THIS_MODULE,
1947 .name = "InfiniBand SRP initiator",
1948 .proc_name = DRV_NAME,
1949 .slave_configure = srp_slave_configure,
1950 .info = srp_target_info,
1951 .queuecommand = srp_queuecommand,
1952 .eh_abort_handler = srp_abort,
1953 .eh_device_reset_handler = srp_reset_device,
1954 .eh_host_reset_handler = srp_reset_host,
1955 .sg_tablesize = SRP_DEF_SG_TABLESIZE,
1956 .can_queue = SRP_CMD_SQ_SIZE,
1957 .this_id = -1,
1958 .cmd_per_lun = SRP_CMD_SQ_SIZE,
1959 .use_clustering = ENABLE_CLUSTERING,
1960 .shost_attrs = srp_host_attrs
1961 };
1962
1963 static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1964 {
1965 struct srp_rport_identifiers ids;
1966 struct srp_rport *rport;
1967
1968 sprintf(target->target_name, "SRP.T10:%016llX",
1969 (unsigned long long) be64_to_cpu(target->id_ext));
1970
1971 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
1972 return -ENODEV;
1973
1974 memcpy(ids.port_id, &target->id_ext, 8);
1975 memcpy(ids.port_id + 8, &target->ioc_guid, 8);
1976 ids.roles = SRP_RPORT_ROLE_TARGET;
1977 rport = srp_rport_add(target->scsi_host, &ids);
1978 if (IS_ERR(rport)) {
1979 scsi_remove_host(target->scsi_host);
1980 return PTR_ERR(rport);
1981 }
1982
1983 rport->lld_data = target;
1984
1985 spin_lock(&host->target_lock);
1986 list_add_tail(&target->list, &host->target_list);
1987 spin_unlock(&host->target_lock);
1988
1989 target->state = SRP_TARGET_LIVE;
1990
1991 scsi_scan_target(&target->scsi_host->shost_gendev,
1992 0, target->scsi_id, SCAN_WILD_CARD, 0);
1993
1994 return 0;
1995 }
1996
1997 static void srp_release_dev(struct device *dev)
1998 {
1999 struct srp_host *host =
2000 container_of(dev, struct srp_host, dev);
2001
2002 complete(&host->released);
2003 }
2004
2005 static struct class srp_class = {
2006 .name = "infiniband_srp",
2007 .dev_release = srp_release_dev
2008 };
2009
2010 /*
2011 * Target ports are added by writing
2012 *
2013 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
2014 * pkey=<P_Key>,service_id=<service ID>
2015 *
2016 * to the add_target sysfs attribute.
2017 */
2018 enum {
2019 SRP_OPT_ERR = 0,
2020 SRP_OPT_ID_EXT = 1 << 0,
2021 SRP_OPT_IOC_GUID = 1 << 1,
2022 SRP_OPT_DGID = 1 << 2,
2023 SRP_OPT_PKEY = 1 << 3,
2024 SRP_OPT_SERVICE_ID = 1 << 4,
2025 SRP_OPT_MAX_SECT = 1 << 5,
2026 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
2027 SRP_OPT_IO_CLASS = 1 << 7,
2028 SRP_OPT_INITIATOR_EXT = 1 << 8,
2029 SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
2030 SRP_OPT_ALLOW_EXT_SG = 1 << 10,
2031 SRP_OPT_SG_TABLESIZE = 1 << 11,
2032 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
2033 SRP_OPT_IOC_GUID |
2034 SRP_OPT_DGID |
2035 SRP_OPT_PKEY |
2036 SRP_OPT_SERVICE_ID),
2037 };
2038
2039 static const match_table_t srp_opt_tokens = {
2040 { SRP_OPT_ID_EXT, "id_ext=%s" },
2041 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
2042 { SRP_OPT_DGID, "dgid=%s" },
2043 { SRP_OPT_PKEY, "pkey=%x" },
2044 { SRP_OPT_SERVICE_ID, "service_id=%s" },
2045 { SRP_OPT_MAX_SECT, "max_sect=%d" },
2046 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
2047 { SRP_OPT_IO_CLASS, "io_class=%x" },
2048 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
2049 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
2050 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
2051 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
2052 { SRP_OPT_ERR, NULL }
2053 };
2054
2055 static int srp_parse_options(const char *buf, struct srp_target_port *target)
2056 {
2057 char *options, *sep_opt;
2058 char *p;
2059 char dgid[3];
2060 substring_t args[MAX_OPT_ARGS];
2061 int opt_mask = 0;
2062 int token;
2063 int ret = -EINVAL;
2064 int i;
2065
2066 options = kstrdup(buf, GFP_KERNEL);
2067 if (!options)
2068 return -ENOMEM;
2069
2070 sep_opt = options;
2071 while ((p = strsep(&sep_opt, ",")) != NULL) {
2072 if (!*p)
2073 continue;
2074
2075 token = match_token(p, srp_opt_tokens, args);
2076 opt_mask |= token;
2077
2078 switch (token) {
2079 case SRP_OPT_ID_EXT:
2080 p = match_strdup(args);
2081 if (!p) {
2082 ret = -ENOMEM;
2083 goto out;
2084 }
2085 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2086 kfree(p);
2087 break;
2088
2089 case SRP_OPT_IOC_GUID:
2090 p = match_strdup(args);
2091 if (!p) {
2092 ret = -ENOMEM;
2093 goto out;
2094 }
2095 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
2096 kfree(p);
2097 break;
2098
2099 case SRP_OPT_DGID:
2100 p = match_strdup(args);
2101 if (!p) {
2102 ret = -ENOMEM;
2103 goto out;
2104 }
2105 if (strlen(p) != 32) {
2106 pr_warn("bad dest GID parameter '%s'\n", p);
2107 kfree(p);
2108 goto out;
2109 }
2110
2111 for (i = 0; i < 16; ++i) {
2112 strlcpy(dgid, p + i * 2, 3);
2113 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
2114 }
2115 kfree(p);
2116 memcpy(target->orig_dgid, target->path.dgid.raw, 16);
2117 break;
2118
2119 case SRP_OPT_PKEY:
2120 if (match_hex(args, &token)) {
2121 pr_warn("bad P_Key parameter '%s'\n", p);
2122 goto out;
2123 }
2124 target->path.pkey = cpu_to_be16(token);
2125 break;
2126
2127 case SRP_OPT_SERVICE_ID:
2128 p = match_strdup(args);
2129 if (!p) {
2130 ret = -ENOMEM;
2131 goto out;
2132 }
2133 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
2134 target->path.service_id = target->service_id;
2135 kfree(p);
2136 break;
2137
2138 case SRP_OPT_MAX_SECT:
2139 if (match_int(args, &token)) {
2140 pr_warn("bad max sect parameter '%s'\n", p);
2141 goto out;
2142 }
2143 target->scsi_host->max_sectors = token;
2144 break;
2145
2146 case SRP_OPT_MAX_CMD_PER_LUN:
2147 if (match_int(args, &token)) {
2148 pr_warn("bad max cmd_per_lun parameter '%s'\n",
2149 p);
2150 goto out;
2151 }
2152 target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE);
2153 break;
2154
2155 case SRP_OPT_IO_CLASS:
2156 if (match_hex(args, &token)) {
2157 pr_warn("bad IO class parameter '%s'\n", p);
2158 goto out;
2159 }
2160 if (token != SRP_REV10_IB_IO_CLASS &&
2161 token != SRP_REV16A_IB_IO_CLASS) {
2162 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
2163 token, SRP_REV10_IB_IO_CLASS,
2164 SRP_REV16A_IB_IO_CLASS);
2165 goto out;
2166 }
2167 target->io_class = token;
2168 break;
2169
2170 case SRP_OPT_INITIATOR_EXT:
2171 p = match_strdup(args);
2172 if (!p) {
2173 ret = -ENOMEM;
2174 goto out;
2175 }
2176 target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
2177 kfree(p);
2178 break;
2179
2180 case SRP_OPT_CMD_SG_ENTRIES:
2181 if (match_int(args, &token) || token < 1 || token > 255) {
2182 pr_warn("bad max cmd_sg_entries parameter '%s'\n",
2183 p);
2184 goto out;
2185 }
2186 target->cmd_sg_cnt = token;
2187 break;
2188
2189 case SRP_OPT_ALLOW_EXT_SG:
2190 if (match_int(args, &token)) {
2191 pr_warn("bad allow_ext_sg parameter '%s'\n", p);
2192 goto out;
2193 }
2194 target->allow_ext_sg = !!token;
2195 break;
2196
2197 case SRP_OPT_SG_TABLESIZE:
2198 if (match_int(args, &token) || token < 1 ||
2199 token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
2200 pr_warn("bad max sg_tablesize parameter '%s'\n",
2201 p);
2202 goto out;
2203 }
2204 target->sg_tablesize = token;
2205 break;
2206
2207 default:
2208 pr_warn("unknown parameter or missing value '%s' in target creation request\n",
2209 p);
2210 goto out;
2211 }
2212 }
2213
2214 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
2215 ret = 0;
2216 else
2217 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
2218 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
2219 !(srp_opt_tokens[i].token & opt_mask))
2220 pr_warn("target creation request is missing parameter '%s'\n",
2221 srp_opt_tokens[i].pattern);
2222
2223 out:
2224 kfree(options);
2225 return ret;
2226 }
2227
2228 static ssize_t srp_create_target(struct device *dev,
2229 struct device_attribute *attr,
2230 const char *buf, size_t count)
2231 {
2232 struct srp_host *host =
2233 container_of(dev, struct srp_host, dev);
2234 struct Scsi_Host *target_host;
2235 struct srp_target_port *target;
2236 struct ib_device *ibdev = host->srp_dev->dev;
2237 dma_addr_t dma_addr;
2238 int i, ret;
2239
2240 target_host = scsi_host_alloc(&srp_template,
2241 sizeof (struct srp_target_port));
2242 if (!target_host)
2243 return -ENOMEM;
2244
2245 target_host->transportt = ib_srp_transport_template;
2246 target_host->max_channel = 0;
2247 target_host->max_id = 1;
2248 target_host->max_lun = SRP_MAX_LUN;
2249 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
2250
2251 target = host_to_target(target_host);
2252
2253 target->io_class = SRP_REV16A_IB_IO_CLASS;
2254 target->scsi_host = target_host;
2255 target->srp_host = host;
2256 target->lkey = host->srp_dev->mr->lkey;
2257 target->rkey = host->srp_dev->mr->rkey;
2258 target->cmd_sg_cnt = cmd_sg_entries;
2259 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
2260 target->allow_ext_sg = allow_ext_sg;
2261
2262 ret = srp_parse_options(buf, target);
2263 if (ret)
2264 goto err;
2265
2266 if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
2267 target->cmd_sg_cnt < target->sg_tablesize) {
2268 pr_warn("No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
2269 target->sg_tablesize = target->cmd_sg_cnt;
2270 }
2271
2272 target_host->sg_tablesize = target->sg_tablesize;
2273 target->indirect_size = target->sg_tablesize *
2274 sizeof (struct srp_direct_buf);
2275 target->max_iu_len = sizeof (struct srp_cmd) +
2276 sizeof (struct srp_indirect_buf) +
2277 target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
2278
2279 INIT_WORK(&target->remove_work, srp_remove_work);
2280 spin_lock_init(&target->lock);
2281 INIT_LIST_HEAD(&target->free_tx);
2282 INIT_LIST_HEAD(&target->free_reqs);
2283 for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
2284 struct srp_request *req = &target->req_ring[i];
2285
2286 req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof (void *),
2287 GFP_KERNEL);
2288 req->map_page = kmalloc(SRP_FMR_SIZE * sizeof (void *),
2289 GFP_KERNEL);
2290 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
2291 if (!req->fmr_list || !req->map_page || !req->indirect_desc)
2292 goto err_free_mem;
2293
2294 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
2295 target->indirect_size,
2296 DMA_TO_DEVICE);
2297 if (ib_dma_mapping_error(ibdev, dma_addr))
2298 goto err_free_mem;
2299
2300 req->indirect_dma_addr = dma_addr;
2301 req->index = i;
2302 list_add_tail(&req->list, &target->free_reqs);
2303 }
2304
2305 ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
2306
2307 shost_printk(KERN_DEBUG, target->scsi_host, PFX
2308 "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
2309 "service_id %016llx dgid %pI6\n",
2310 (unsigned long long) be64_to_cpu(target->id_ext),
2311 (unsigned long long) be64_to_cpu(target->ioc_guid),
2312 be16_to_cpu(target->path.pkey),
2313 (unsigned long long) be64_to_cpu(target->service_id),
2314 target->path.dgid.raw);
2315
2316 ret = srp_create_target_ib(target);
2317 if (ret)
2318 goto err_free_mem;
2319
2320 ret = srp_new_cm_id(target);
2321 if (ret)
2322 goto err_free_ib;
2323
2324 ret = srp_connect_target(target);
2325 if (ret) {
2326 shost_printk(KERN_ERR, target->scsi_host,
2327 PFX "Connection failed\n");
2328 goto err_cm_id;
2329 }
2330
2331 ret = srp_add_target(host, target);
2332 if (ret)
2333 goto err_disconnect;
2334
2335 return count;
2336
2337 err_disconnect:
2338 srp_disconnect_target(target);
2339
2340 err_cm_id:
2341 ib_destroy_cm_id(target->cm_id);
2342
2343 err_free_ib:
2344 srp_free_target_ib(target);
2345
2346 err_free_mem:
2347 srp_free_req_data(target);
2348
2349 err:
2350 scsi_host_put(target_host);
2351
2352 return ret;
2353 }
2354
2355 static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
2356
2357 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
2358 char *buf)
2359 {
2360 struct srp_host *host = container_of(dev, struct srp_host, dev);
2361
2362 return sprintf(buf, "%s\n", host->srp_dev->dev->name);
2363 }
2364
2365 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
2366
2367 static ssize_t show_port(struct device *dev, struct device_attribute *attr,
2368 char *buf)
2369 {
2370 struct srp_host *host = container_of(dev, struct srp_host, dev);
2371
2372 return sprintf(buf, "%d\n", host->port);
2373 }
2374
2375 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
2376
2377 static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
2378 {
2379 struct srp_host *host;
2380
2381 host = kzalloc(sizeof *host, GFP_KERNEL);
2382 if (!host)
2383 return NULL;
2384
2385 INIT_LIST_HEAD(&host->target_list);
2386 spin_lock_init(&host->target_lock);
2387 init_completion(&host->released);
2388 host->srp_dev = device;
2389 host->port = port;
2390
2391 host->dev.class = &srp_class;
2392 host->dev.parent = device->dev->dma_device;
2393 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
2394
2395 if (device_register(&host->dev))
2396 goto free_host;
2397 if (device_create_file(&host->dev, &dev_attr_add_target))
2398 goto err_class;
2399 if (device_create_file(&host->dev, &dev_attr_ibdev))
2400 goto err_class;
2401 if (device_create_file(&host->dev, &dev_attr_port))
2402 goto err_class;
2403
2404 return host;
2405
2406 err_class:
2407 device_unregister(&host->dev);
2408
2409 free_host:
2410 kfree(host);
2411
2412 return NULL;
2413 }
2414
2415 static void srp_add_one(struct ib_device *device)
2416 {
2417 struct srp_device *srp_dev;
2418 struct ib_device_attr *dev_attr;
2419 struct ib_fmr_pool_param fmr_param;
2420 struct srp_host *host;
2421 int max_pages_per_fmr, fmr_page_shift, s, e, p;
2422
2423 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
2424 if (!dev_attr)
2425 return;
2426
2427 if (ib_query_device(device, dev_attr)) {
2428 pr_warn("Query device failed for %s\n", device->name);
2429 goto free_attr;
2430 }
2431
2432 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
2433 if (!srp_dev)
2434 goto free_attr;
2435
2436 /*
2437 * Use the smallest page size supported by the HCA, down to a
2438 * minimum of 4096 bytes. We're unlikely to build large sglists
2439 * out of smaller entries.
2440 */
2441 fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
2442 srp_dev->fmr_page_size = 1 << fmr_page_shift;
2443 srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
2444 srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
2445
2446 INIT_LIST_HEAD(&srp_dev->dev_list);
2447
2448 srp_dev->dev = device;
2449 srp_dev->pd = ib_alloc_pd(device);
2450 if (IS_ERR(srp_dev->pd))
2451 goto free_dev;
2452
2453 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
2454 IB_ACCESS_LOCAL_WRITE |
2455 IB_ACCESS_REMOTE_READ |
2456 IB_ACCESS_REMOTE_WRITE);
2457 if (IS_ERR(srp_dev->mr))
2458 goto err_pd;
2459
2460 for (max_pages_per_fmr = SRP_FMR_SIZE;
2461 max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
2462 max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
2463 memset(&fmr_param, 0, sizeof fmr_param);
2464 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
2465 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
2466 fmr_param.cache = 1;
2467 fmr_param.max_pages_per_fmr = max_pages_per_fmr;
2468 fmr_param.page_shift = fmr_page_shift;
2469 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
2470 IB_ACCESS_REMOTE_WRITE |
2471 IB_ACCESS_REMOTE_READ);
2472
2473 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
2474 if (!IS_ERR(srp_dev->fmr_pool))
2475 break;
2476 }
2477
2478 if (IS_ERR(srp_dev->fmr_pool))
2479 srp_dev->fmr_pool = NULL;
2480
2481 if (device->node_type == RDMA_NODE_IB_SWITCH) {
2482 s = 0;
2483 e = 0;
2484 } else {
2485 s = 1;
2486 e = device->phys_port_cnt;
2487 }
2488
2489 for (p = s; p <= e; ++p) {
2490 host = srp_add_port(srp_dev, p);
2491 if (host)
2492 list_add_tail(&host->list, &srp_dev->dev_list);
2493 }
2494
2495 ib_set_client_data(device, &srp_client, srp_dev);
2496
2497 goto free_attr;
2498
2499 err_pd:
2500 ib_dealloc_pd(srp_dev->pd);
2501
2502 free_dev:
2503 kfree(srp_dev);
2504
2505 free_attr:
2506 kfree(dev_attr);
2507 }
2508
2509 static void srp_remove_one(struct ib_device *device)
2510 {
2511 struct srp_device *srp_dev;
2512 struct srp_host *host, *tmp_host;
2513 struct srp_target_port *target;
2514
2515 srp_dev = ib_get_client_data(device, &srp_client);
2516
2517 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
2518 device_unregister(&host->dev);
2519 /*
2520 * Wait for the sysfs entry to go away, so that no new
2521 * target ports can be created.
2522 */
2523 wait_for_completion(&host->released);
2524
2525 /*
2526 * Remove all target ports.
2527 */
2528 spin_lock(&host->target_lock);
2529 list_for_each_entry(target, &host->target_list, list)
2530 srp_queue_remove_work(target);
2531 spin_unlock(&host->target_lock);
2532
2533 /*
2534 * Wait for tl_err and target port removal tasks.
2535 */
2536 flush_workqueue(system_long_wq);
2537 flush_workqueue(srp_remove_wq);
2538
2539 kfree(host);
2540 }
2541
2542 if (srp_dev->fmr_pool)
2543 ib_destroy_fmr_pool(srp_dev->fmr_pool);
2544 ib_dereg_mr(srp_dev->mr);
2545 ib_dealloc_pd(srp_dev->pd);
2546
2547 kfree(srp_dev);
2548 }
2549
2550 static struct srp_function_template ib_srp_transport_functions = {
2551 .rport_delete = srp_rport_delete,
2552 };
2553
2554 static int __init srp_init_module(void)
2555 {
2556 int ret;
2557
2558 BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
2559
2560 if (srp_sg_tablesize) {
2561 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
2562 if (!cmd_sg_entries)
2563 cmd_sg_entries = srp_sg_tablesize;
2564 }
2565
2566 if (!cmd_sg_entries)
2567 cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
2568
2569 if (cmd_sg_entries > 255) {
2570 pr_warn("Clamping cmd_sg_entries to 255\n");
2571 cmd_sg_entries = 255;
2572 }
2573
2574 if (!indirect_sg_entries)
2575 indirect_sg_entries = cmd_sg_entries;
2576 else if (indirect_sg_entries < cmd_sg_entries) {
2577 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
2578 cmd_sg_entries);
2579 indirect_sg_entries = cmd_sg_entries;
2580 }
2581
2582 srp_remove_wq = create_workqueue("srp_remove");
2583 if (IS_ERR(srp_remove_wq)) {
2584 ret = PTR_ERR(srp_remove_wq);
2585 goto out;
2586 }
2587
2588 ret = -ENOMEM;
2589 ib_srp_transport_template =
2590 srp_attach_transport(&ib_srp_transport_functions);
2591 if (!ib_srp_transport_template)
2592 goto destroy_wq;
2593
2594 ret = class_register(&srp_class);
2595 if (ret) {
2596 pr_err("couldn't register class infiniband_srp\n");
2597 goto release_tr;
2598 }
2599
2600 ib_sa_register_client(&srp_sa_client);
2601
2602 ret = ib_register_client(&srp_client);
2603 if (ret) {
2604 pr_err("couldn't register IB client\n");
2605 goto unreg_sa;
2606 }
2607
2608 out:
2609 return ret;
2610
2611 unreg_sa:
2612 ib_sa_unregister_client(&srp_sa_client);
2613 class_unregister(&srp_class);
2614
2615 release_tr:
2616 srp_release_transport(ib_srp_transport_template);
2617
2618 destroy_wq:
2619 destroy_workqueue(srp_remove_wq);
2620 goto out;
2621 }
2622
2623 static void __exit srp_cleanup_module(void)
2624 {
2625 ib_unregister_client(&srp_client);
2626 ib_sa_unregister_client(&srp_sa_client);
2627 class_unregister(&srp_class);
2628 srp_release_transport(ib_srp_transport_template);
2629 destroy_workqueue(srp_remove_wq);
2630 }