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) KBUILD_MODNAME ": " 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 #include <linux/lockdep.h> 44 #include <linux/inet.h> 45 #include <rdma/ib_cache.h> 46 47 #include <linux/atomic.h> 48 49 #include <scsi/scsi.h> 50 #include <scsi/scsi_device.h> 51 #include <scsi/scsi_dbg.h> 52 #include <scsi/scsi_tcq.h> 53 #include <scsi/srp.h> 54 #include <scsi/scsi_transport_srp.h> 55 56 #include "ib_srp.h" 57 58 #define DRV_NAME "ib_srp" 59 #define PFX DRV_NAME ": " 60 #define DRV_VERSION "2.0" 61 #define DRV_RELDATE "July 26, 2015" 62 63 MODULE_AUTHOR("Roland Dreier"); 64 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator"); 65 MODULE_LICENSE("Dual BSD/GPL"); 66 MODULE_INFO(release_date, DRV_RELDATE); 67 68 #if !defined(CONFIG_DYNAMIC_DEBUG) 69 #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) 70 #define DYNAMIC_DEBUG_BRANCH(descriptor) false 71 #endif 72 73 static unsigned int srp_sg_tablesize; 74 static unsigned int cmd_sg_entries; 75 static unsigned int indirect_sg_entries; 76 static bool allow_ext_sg; 77 static bool prefer_fr = true; 78 static bool register_always = true; 79 static bool never_register; 80 static int topspin_workarounds = 1; 81 82 module_param(srp_sg_tablesize, uint, 0444); 83 MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries"); 84 85 module_param(cmd_sg_entries, uint, 0444); 86 MODULE_PARM_DESC(cmd_sg_entries, 87 "Default number of gather/scatter entries in the SRP command (default is 12, max 255)"); 88 89 module_param(indirect_sg_entries, uint, 0444); 90 MODULE_PARM_DESC(indirect_sg_entries, 91 "Default max number of gather/scatter entries (default is 12, max is " __stringify(SG_MAX_SEGMENTS) ")"); 92 93 module_param(allow_ext_sg, bool, 0444); 94 MODULE_PARM_DESC(allow_ext_sg, 95 "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)"); 96 97 module_param(topspin_workarounds, int, 0444); 98 MODULE_PARM_DESC(topspin_workarounds, 99 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0"); 100 101 module_param(prefer_fr, bool, 0444); 102 MODULE_PARM_DESC(prefer_fr, 103 "Whether to use fast registration if both FMR and fast registration are supported"); 104 105 module_param(register_always, bool, 0444); 106 MODULE_PARM_DESC(register_always, 107 "Use memory registration even for contiguous memory regions"); 108 109 module_param(never_register, bool, 0444); 110 MODULE_PARM_DESC(never_register, "Never register memory"); 111 112 static const struct kernel_param_ops srp_tmo_ops; 113 114 static int srp_reconnect_delay = 10; 115 module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay, 116 S_IRUGO | S_IWUSR); 117 MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts"); 118 119 static int srp_fast_io_fail_tmo = 15; 120 module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo, 121 S_IRUGO | S_IWUSR); 122 MODULE_PARM_DESC(fast_io_fail_tmo, 123 "Number of seconds between the observation of a transport" 124 " layer error and failing all I/O. \"off\" means that this" 125 " functionality is disabled."); 126 127 static int srp_dev_loss_tmo = 600; 128 module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo, 129 S_IRUGO | S_IWUSR); 130 MODULE_PARM_DESC(dev_loss_tmo, 131 "Maximum number of seconds that the SRP transport should" 132 " insulate transport layer errors. After this time has been" 133 " exceeded the SCSI host is removed. Should be" 134 " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT) 135 " if fast_io_fail_tmo has not been set. \"off\" means that" 136 " this functionality is disabled."); 137 138 static unsigned ch_count; 139 module_param(ch_count, uint, 0444); 140 MODULE_PARM_DESC(ch_count, 141 "Number of RDMA channels to use for communication with an SRP target. Using more than one channel improves performance if the HCA supports multiple completion vectors. The default value is the minimum of four times the number of online CPU sockets and the number of completion vectors supported by the HCA."); 142 143 static void srp_add_one(struct ib_device *device); 144 static void srp_remove_one(struct ib_device *device, void *client_data); 145 static void srp_recv_done(struct ib_cq *cq, struct ib_wc *wc); 146 static void srp_handle_qp_err(struct ib_cq *cq, struct ib_wc *wc, 147 const char *opname); 148 static int srp_ib_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event); 149 static int srp_rdma_cm_handler(struct rdma_cm_id *cm_id, 150 struct rdma_cm_event *event); 151 152 static struct scsi_transport_template *ib_srp_transport_template; 153 static struct workqueue_struct *srp_remove_wq; 154 155 static struct ib_client srp_client = { 156 .name = "srp", 157 .add = srp_add_one, 158 .remove = srp_remove_one 159 }; 160 161 static struct ib_sa_client srp_sa_client; 162 163 static int srp_tmo_get(char *buffer, const struct kernel_param *kp) 164 { 165 int tmo = *(int *)kp->arg; 166 167 if (tmo >= 0) 168 return sprintf(buffer, "%d", tmo); 169 else 170 return sprintf(buffer, "off"); 171 } 172 173 static int srp_tmo_set(const char *val, const struct kernel_param *kp) 174 { 175 int tmo, res; 176 177 res = srp_parse_tmo(&tmo, val); 178 if (res) 179 goto out; 180 181 if (kp->arg == &srp_reconnect_delay) 182 res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo, 183 srp_dev_loss_tmo); 184 else if (kp->arg == &srp_fast_io_fail_tmo) 185 res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo); 186 else 187 res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo, 188 tmo); 189 if (res) 190 goto out; 191 *(int *)kp->arg = tmo; 192 193 out: 194 return res; 195 } 196 197 static const struct kernel_param_ops srp_tmo_ops = { 198 .get = srp_tmo_get, 199 .set = srp_tmo_set, 200 }; 201 202 static inline struct srp_target_port *host_to_target(struct Scsi_Host *host) 203 { 204 return (struct srp_target_port *) host->hostdata; 205 } 206 207 static const char *srp_target_info(struct Scsi_Host *host) 208 { 209 return host_to_target(host)->target_name; 210 } 211 212 static int srp_target_is_topspin(struct srp_target_port *target) 213 { 214 static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad }; 215 static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d }; 216 217 return topspin_workarounds && 218 (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) || 219 !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui)); 220 } 221 222 static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size, 223 gfp_t gfp_mask, 224 enum dma_data_direction direction) 225 { 226 struct srp_iu *iu; 227 228 iu = kmalloc(sizeof *iu, gfp_mask); 229 if (!iu) 230 goto out; 231 232 iu->buf = kzalloc(size, gfp_mask); 233 if (!iu->buf) 234 goto out_free_iu; 235 236 iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size, 237 direction); 238 if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma)) 239 goto out_free_buf; 240 241 iu->size = size; 242 iu->direction = direction; 243 244 return iu; 245 246 out_free_buf: 247 kfree(iu->buf); 248 out_free_iu: 249 kfree(iu); 250 out: 251 return NULL; 252 } 253 254 static void srp_free_iu(struct srp_host *host, struct srp_iu *iu) 255 { 256 if (!iu) 257 return; 258 259 ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size, 260 iu->direction); 261 kfree(iu->buf); 262 kfree(iu); 263 } 264 265 static void srp_qp_event(struct ib_event *event, void *context) 266 { 267 pr_debug("QP event %s (%d)\n", 268 ib_event_msg(event->event), event->event); 269 } 270 271 static int srp_init_ib_qp(struct srp_target_port *target, 272 struct ib_qp *qp) 273 { 274 struct ib_qp_attr *attr; 275 int ret; 276 277 attr = kmalloc(sizeof *attr, GFP_KERNEL); 278 if (!attr) 279 return -ENOMEM; 280 281 ret = ib_find_cached_pkey(target->srp_host->srp_dev->dev, 282 target->srp_host->port, 283 be16_to_cpu(target->ib_cm.pkey), 284 &attr->pkey_index); 285 if (ret) 286 goto out; 287 288 attr->qp_state = IB_QPS_INIT; 289 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ | 290 IB_ACCESS_REMOTE_WRITE); 291 attr->port_num = target->srp_host->port; 292 293 ret = ib_modify_qp(qp, attr, 294 IB_QP_STATE | 295 IB_QP_PKEY_INDEX | 296 IB_QP_ACCESS_FLAGS | 297 IB_QP_PORT); 298 299 out: 300 kfree(attr); 301 return ret; 302 } 303 304 static int srp_new_ib_cm_id(struct srp_rdma_ch *ch) 305 { 306 struct srp_target_port *target = ch->target; 307 struct ib_cm_id *new_cm_id; 308 309 new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev, 310 srp_ib_cm_handler, ch); 311 if (IS_ERR(new_cm_id)) 312 return PTR_ERR(new_cm_id); 313 314 if (ch->ib_cm.cm_id) 315 ib_destroy_cm_id(ch->ib_cm.cm_id); 316 ch->ib_cm.cm_id = new_cm_id; 317 if (rdma_cap_opa_ah(target->srp_host->srp_dev->dev, 318 target->srp_host->port)) 319 ch->ib_cm.path.rec_type = SA_PATH_REC_TYPE_OPA; 320 else 321 ch->ib_cm.path.rec_type = SA_PATH_REC_TYPE_IB; 322 ch->ib_cm.path.sgid = target->sgid; 323 ch->ib_cm.path.dgid = target->ib_cm.orig_dgid; 324 ch->ib_cm.path.pkey = target->ib_cm.pkey; 325 ch->ib_cm.path.service_id = target->ib_cm.service_id; 326 327 return 0; 328 } 329 330 static int srp_new_rdma_cm_id(struct srp_rdma_ch *ch) 331 { 332 struct srp_target_port *target = ch->target; 333 struct rdma_cm_id *new_cm_id; 334 int ret; 335 336 new_cm_id = rdma_create_id(target->net, srp_rdma_cm_handler, ch, 337 RDMA_PS_TCP, IB_QPT_RC); 338 if (IS_ERR(new_cm_id)) { 339 ret = PTR_ERR(new_cm_id); 340 new_cm_id = NULL; 341 goto out; 342 } 343 344 init_completion(&ch->done); 345 ret = rdma_resolve_addr(new_cm_id, target->rdma_cm.src_specified ? 346 (struct sockaddr *)&target->rdma_cm.src : NULL, 347 (struct sockaddr *)&target->rdma_cm.dst, 348 SRP_PATH_REC_TIMEOUT_MS); 349 if (ret) { 350 pr_err("No route available from %pIS to %pIS (%d)\n", 351 &target->rdma_cm.src, &target->rdma_cm.dst, ret); 352 goto out; 353 } 354 ret = wait_for_completion_interruptible(&ch->done); 355 if (ret < 0) 356 goto out; 357 358 ret = ch->status; 359 if (ret) { 360 pr_err("Resolving address %pIS failed (%d)\n", 361 &target->rdma_cm.dst, ret); 362 goto out; 363 } 364 365 swap(ch->rdma_cm.cm_id, new_cm_id); 366 367 out: 368 if (new_cm_id) 369 rdma_destroy_id(new_cm_id); 370 371 return ret; 372 } 373 374 static int srp_new_cm_id(struct srp_rdma_ch *ch) 375 { 376 struct srp_target_port *target = ch->target; 377 378 return target->using_rdma_cm ? srp_new_rdma_cm_id(ch) : 379 srp_new_ib_cm_id(ch); 380 } 381 382 static struct ib_fmr_pool *srp_alloc_fmr_pool(struct srp_target_port *target) 383 { 384 struct srp_device *dev = target->srp_host->srp_dev; 385 struct ib_fmr_pool_param fmr_param; 386 387 memset(&fmr_param, 0, sizeof(fmr_param)); 388 fmr_param.pool_size = target->mr_pool_size; 389 fmr_param.dirty_watermark = fmr_param.pool_size / 4; 390 fmr_param.cache = 1; 391 fmr_param.max_pages_per_fmr = dev->max_pages_per_mr; 392 fmr_param.page_shift = ilog2(dev->mr_page_size); 393 fmr_param.access = (IB_ACCESS_LOCAL_WRITE | 394 IB_ACCESS_REMOTE_WRITE | 395 IB_ACCESS_REMOTE_READ); 396 397 return ib_create_fmr_pool(dev->pd, &fmr_param); 398 } 399 400 /** 401 * srp_destroy_fr_pool() - free the resources owned by a pool 402 * @pool: Fast registration pool to be destroyed. 403 */ 404 static void srp_destroy_fr_pool(struct srp_fr_pool *pool) 405 { 406 int i; 407 struct srp_fr_desc *d; 408 409 if (!pool) 410 return; 411 412 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) { 413 if (d->mr) 414 ib_dereg_mr(d->mr); 415 } 416 kfree(pool); 417 } 418 419 /** 420 * srp_create_fr_pool() - allocate and initialize a pool for fast registration 421 * @device: IB device to allocate fast registration descriptors for. 422 * @pd: Protection domain associated with the FR descriptors. 423 * @pool_size: Number of descriptors to allocate. 424 * @max_page_list_len: Maximum fast registration work request page list length. 425 */ 426 static struct srp_fr_pool *srp_create_fr_pool(struct ib_device *device, 427 struct ib_pd *pd, int pool_size, 428 int max_page_list_len) 429 { 430 struct srp_fr_pool *pool; 431 struct srp_fr_desc *d; 432 struct ib_mr *mr; 433 int i, ret = -EINVAL; 434 enum ib_mr_type mr_type; 435 436 if (pool_size <= 0) 437 goto err; 438 ret = -ENOMEM; 439 pool = kzalloc(sizeof(struct srp_fr_pool) + 440 pool_size * sizeof(struct srp_fr_desc), GFP_KERNEL); 441 if (!pool) 442 goto err; 443 pool->size = pool_size; 444 pool->max_page_list_len = max_page_list_len; 445 spin_lock_init(&pool->lock); 446 INIT_LIST_HEAD(&pool->free_list); 447 448 if (device->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG) 449 mr_type = IB_MR_TYPE_SG_GAPS; 450 else 451 mr_type = IB_MR_TYPE_MEM_REG; 452 453 for (i = 0, d = &pool->desc[0]; i < pool->size; i++, d++) { 454 mr = ib_alloc_mr(pd, mr_type, max_page_list_len); 455 if (IS_ERR(mr)) { 456 ret = PTR_ERR(mr); 457 if (ret == -ENOMEM) 458 pr_info("%s: ib_alloc_mr() failed. Try to reduce max_cmd_per_lun, max_sect or ch_count\n", 459 dev_name(&device->dev)); 460 goto destroy_pool; 461 } 462 d->mr = mr; 463 list_add_tail(&d->entry, &pool->free_list); 464 } 465 466 out: 467 return pool; 468 469 destroy_pool: 470 srp_destroy_fr_pool(pool); 471 472 err: 473 pool = ERR_PTR(ret); 474 goto out; 475 } 476 477 /** 478 * srp_fr_pool_get() - obtain a descriptor suitable for fast registration 479 * @pool: Pool to obtain descriptor from. 480 */ 481 static struct srp_fr_desc *srp_fr_pool_get(struct srp_fr_pool *pool) 482 { 483 struct srp_fr_desc *d = NULL; 484 unsigned long flags; 485 486 spin_lock_irqsave(&pool->lock, flags); 487 if (!list_empty(&pool->free_list)) { 488 d = list_first_entry(&pool->free_list, typeof(*d), entry); 489 list_del(&d->entry); 490 } 491 spin_unlock_irqrestore(&pool->lock, flags); 492 493 return d; 494 } 495 496 /** 497 * srp_fr_pool_put() - put an FR descriptor back in the free list 498 * @pool: Pool the descriptor was allocated from. 499 * @desc: Pointer to an array of fast registration descriptor pointers. 500 * @n: Number of descriptors to put back. 501 * 502 * Note: The caller must already have queued an invalidation request for 503 * desc->mr->rkey before calling this function. 504 */ 505 static void srp_fr_pool_put(struct srp_fr_pool *pool, struct srp_fr_desc **desc, 506 int n) 507 { 508 unsigned long flags; 509 int i; 510 511 spin_lock_irqsave(&pool->lock, flags); 512 for (i = 0; i < n; i++) 513 list_add(&desc[i]->entry, &pool->free_list); 514 spin_unlock_irqrestore(&pool->lock, flags); 515 } 516 517 static struct srp_fr_pool *srp_alloc_fr_pool(struct srp_target_port *target) 518 { 519 struct srp_device *dev = target->srp_host->srp_dev; 520 521 return srp_create_fr_pool(dev->dev, dev->pd, target->mr_pool_size, 522 dev->max_pages_per_mr); 523 } 524 525 /** 526 * srp_destroy_qp() - destroy an RDMA queue pair 527 * @ch: SRP RDMA channel. 528 * 529 * Drain the qp before destroying it. This avoids that the receive 530 * completion handler can access the queue pair while it is 531 * being destroyed. 532 */ 533 static void srp_destroy_qp(struct srp_rdma_ch *ch) 534 { 535 spin_lock_irq(&ch->lock); 536 ib_process_cq_direct(ch->send_cq, -1); 537 spin_unlock_irq(&ch->lock); 538 539 ib_drain_qp(ch->qp); 540 ib_destroy_qp(ch->qp); 541 } 542 543 static int srp_create_ch_ib(struct srp_rdma_ch *ch) 544 { 545 struct srp_target_port *target = ch->target; 546 struct srp_device *dev = target->srp_host->srp_dev; 547 struct ib_qp_init_attr *init_attr; 548 struct ib_cq *recv_cq, *send_cq; 549 struct ib_qp *qp; 550 struct ib_fmr_pool *fmr_pool = NULL; 551 struct srp_fr_pool *fr_pool = NULL; 552 const int m = 1 + dev->use_fast_reg * target->mr_per_cmd * 2; 553 int ret; 554 555 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL); 556 if (!init_attr) 557 return -ENOMEM; 558 559 /* queue_size + 1 for ib_drain_rq() */ 560 recv_cq = ib_alloc_cq(dev->dev, ch, target->queue_size + 1, 561 ch->comp_vector, IB_POLL_SOFTIRQ); 562 if (IS_ERR(recv_cq)) { 563 ret = PTR_ERR(recv_cq); 564 goto err; 565 } 566 567 send_cq = ib_alloc_cq(dev->dev, ch, m * target->queue_size, 568 ch->comp_vector, IB_POLL_DIRECT); 569 if (IS_ERR(send_cq)) { 570 ret = PTR_ERR(send_cq); 571 goto err_recv_cq; 572 } 573 574 init_attr->event_handler = srp_qp_event; 575 init_attr->cap.max_send_wr = m * target->queue_size; 576 init_attr->cap.max_recv_wr = target->queue_size + 1; 577 init_attr->cap.max_recv_sge = 1; 578 init_attr->cap.max_send_sge = 1; 579 init_attr->sq_sig_type = IB_SIGNAL_REQ_WR; 580 init_attr->qp_type = IB_QPT_RC; 581 init_attr->send_cq = send_cq; 582 init_attr->recv_cq = recv_cq; 583 584 if (target->using_rdma_cm) { 585 ret = rdma_create_qp(ch->rdma_cm.cm_id, dev->pd, init_attr); 586 qp = ch->rdma_cm.cm_id->qp; 587 } else { 588 qp = ib_create_qp(dev->pd, init_attr); 589 if (!IS_ERR(qp)) { 590 ret = srp_init_ib_qp(target, qp); 591 if (ret) 592 ib_destroy_qp(qp); 593 } else { 594 ret = PTR_ERR(qp); 595 } 596 } 597 if (ret) { 598 pr_err("QP creation failed for dev %s: %d\n", 599 dev_name(&dev->dev->dev), ret); 600 goto err_send_cq; 601 } 602 603 if (dev->use_fast_reg) { 604 fr_pool = srp_alloc_fr_pool(target); 605 if (IS_ERR(fr_pool)) { 606 ret = PTR_ERR(fr_pool); 607 shost_printk(KERN_WARNING, target->scsi_host, PFX 608 "FR pool allocation failed (%d)\n", ret); 609 goto err_qp; 610 } 611 } else if (dev->use_fmr) { 612 fmr_pool = srp_alloc_fmr_pool(target); 613 if (IS_ERR(fmr_pool)) { 614 ret = PTR_ERR(fmr_pool); 615 shost_printk(KERN_WARNING, target->scsi_host, PFX 616 "FMR pool allocation failed (%d)\n", ret); 617 goto err_qp; 618 } 619 } 620 621 if (ch->qp) 622 srp_destroy_qp(ch); 623 if (ch->recv_cq) 624 ib_free_cq(ch->recv_cq); 625 if (ch->send_cq) 626 ib_free_cq(ch->send_cq); 627 628 ch->qp = qp; 629 ch->recv_cq = recv_cq; 630 ch->send_cq = send_cq; 631 632 if (dev->use_fast_reg) { 633 if (ch->fr_pool) 634 srp_destroy_fr_pool(ch->fr_pool); 635 ch->fr_pool = fr_pool; 636 } else if (dev->use_fmr) { 637 if (ch->fmr_pool) 638 ib_destroy_fmr_pool(ch->fmr_pool); 639 ch->fmr_pool = fmr_pool; 640 } 641 642 kfree(init_attr); 643 return 0; 644 645 err_qp: 646 if (target->using_rdma_cm) 647 rdma_destroy_qp(ch->rdma_cm.cm_id); 648 else 649 ib_destroy_qp(qp); 650 651 err_send_cq: 652 ib_free_cq(send_cq); 653 654 err_recv_cq: 655 ib_free_cq(recv_cq); 656 657 err: 658 kfree(init_attr); 659 return ret; 660 } 661 662 /* 663 * Note: this function may be called without srp_alloc_iu_bufs() having been 664 * invoked. Hence the ch->[rt]x_ring checks. 665 */ 666 static void srp_free_ch_ib(struct srp_target_port *target, 667 struct srp_rdma_ch *ch) 668 { 669 struct srp_device *dev = target->srp_host->srp_dev; 670 int i; 671 672 if (!ch->target) 673 return; 674 675 if (target->using_rdma_cm) { 676 if (ch->rdma_cm.cm_id) { 677 rdma_destroy_id(ch->rdma_cm.cm_id); 678 ch->rdma_cm.cm_id = NULL; 679 } 680 } else { 681 if (ch->ib_cm.cm_id) { 682 ib_destroy_cm_id(ch->ib_cm.cm_id); 683 ch->ib_cm.cm_id = NULL; 684 } 685 } 686 687 /* If srp_new_cm_id() succeeded but srp_create_ch_ib() not, return. */ 688 if (!ch->qp) 689 return; 690 691 if (dev->use_fast_reg) { 692 if (ch->fr_pool) 693 srp_destroy_fr_pool(ch->fr_pool); 694 } else if (dev->use_fmr) { 695 if (ch->fmr_pool) 696 ib_destroy_fmr_pool(ch->fmr_pool); 697 } 698 699 srp_destroy_qp(ch); 700 ib_free_cq(ch->send_cq); 701 ib_free_cq(ch->recv_cq); 702 703 /* 704 * Avoid that the SCSI error handler tries to use this channel after 705 * it has been freed. The SCSI error handler can namely continue 706 * trying to perform recovery actions after scsi_remove_host() 707 * returned. 708 */ 709 ch->target = NULL; 710 711 ch->qp = NULL; 712 ch->send_cq = ch->recv_cq = NULL; 713 714 if (ch->rx_ring) { 715 for (i = 0; i < target->queue_size; ++i) 716 srp_free_iu(target->srp_host, ch->rx_ring[i]); 717 kfree(ch->rx_ring); 718 ch->rx_ring = NULL; 719 } 720 if (ch->tx_ring) { 721 for (i = 0; i < target->queue_size; ++i) 722 srp_free_iu(target->srp_host, ch->tx_ring[i]); 723 kfree(ch->tx_ring); 724 ch->tx_ring = NULL; 725 } 726 } 727 728 static void srp_path_rec_completion(int status, 729 struct sa_path_rec *pathrec, 730 void *ch_ptr) 731 { 732 struct srp_rdma_ch *ch = ch_ptr; 733 struct srp_target_port *target = ch->target; 734 735 ch->status = status; 736 if (status) 737 shost_printk(KERN_ERR, target->scsi_host, 738 PFX "Got failed path rec status %d\n", status); 739 else 740 ch->ib_cm.path = *pathrec; 741 complete(&ch->done); 742 } 743 744 static int srp_ib_lookup_path(struct srp_rdma_ch *ch) 745 { 746 struct srp_target_port *target = ch->target; 747 int ret; 748 749 ch->ib_cm.path.numb_path = 1; 750 751 init_completion(&ch->done); 752 753 ch->ib_cm.path_query_id = ib_sa_path_rec_get(&srp_sa_client, 754 target->srp_host->srp_dev->dev, 755 target->srp_host->port, 756 &ch->ib_cm.path, 757 IB_SA_PATH_REC_SERVICE_ID | 758 IB_SA_PATH_REC_DGID | 759 IB_SA_PATH_REC_SGID | 760 IB_SA_PATH_REC_NUMB_PATH | 761 IB_SA_PATH_REC_PKEY, 762 SRP_PATH_REC_TIMEOUT_MS, 763 GFP_KERNEL, 764 srp_path_rec_completion, 765 ch, &ch->ib_cm.path_query); 766 if (ch->ib_cm.path_query_id < 0) 767 return ch->ib_cm.path_query_id; 768 769 ret = wait_for_completion_interruptible(&ch->done); 770 if (ret < 0) 771 return ret; 772 773 if (ch->status < 0) 774 shost_printk(KERN_WARNING, target->scsi_host, 775 PFX "Path record query failed: sgid %pI6, dgid %pI6, pkey %#04x, service_id %#16llx\n", 776 ch->ib_cm.path.sgid.raw, ch->ib_cm.path.dgid.raw, 777 be16_to_cpu(target->ib_cm.pkey), 778 be64_to_cpu(target->ib_cm.service_id)); 779 780 return ch->status; 781 } 782 783 static int srp_rdma_lookup_path(struct srp_rdma_ch *ch) 784 { 785 struct srp_target_port *target = ch->target; 786 int ret; 787 788 init_completion(&ch->done); 789 790 ret = rdma_resolve_route(ch->rdma_cm.cm_id, SRP_PATH_REC_TIMEOUT_MS); 791 if (ret) 792 return ret; 793 794 wait_for_completion_interruptible(&ch->done); 795 796 if (ch->status != 0) 797 shost_printk(KERN_WARNING, target->scsi_host, 798 PFX "Path resolution failed\n"); 799 800 return ch->status; 801 } 802 803 static int srp_lookup_path(struct srp_rdma_ch *ch) 804 { 805 struct srp_target_port *target = ch->target; 806 807 return target->using_rdma_cm ? srp_rdma_lookup_path(ch) : 808 srp_ib_lookup_path(ch); 809 } 810 811 static u8 srp_get_subnet_timeout(struct srp_host *host) 812 { 813 struct ib_port_attr attr; 814 int ret; 815 u8 subnet_timeout = 18; 816 817 ret = ib_query_port(host->srp_dev->dev, host->port, &attr); 818 if (ret == 0) 819 subnet_timeout = attr.subnet_timeout; 820 821 if (unlikely(subnet_timeout < 15)) 822 pr_warn("%s: subnet timeout %d may cause SRP login to fail.\n", 823 dev_name(&host->srp_dev->dev->dev), subnet_timeout); 824 825 return subnet_timeout; 826 } 827 828 static int srp_send_req(struct srp_rdma_ch *ch, bool multich) 829 { 830 struct srp_target_port *target = ch->target; 831 struct { 832 struct rdma_conn_param rdma_param; 833 struct srp_login_req_rdma rdma_req; 834 struct ib_cm_req_param ib_param; 835 struct srp_login_req ib_req; 836 } *req = NULL; 837 char *ipi, *tpi; 838 int status; 839 840 req = kzalloc(sizeof *req, GFP_KERNEL); 841 if (!req) 842 return -ENOMEM; 843 844 req->ib_param.flow_control = 1; 845 req->ib_param.retry_count = target->tl_retry_count; 846 847 /* 848 * Pick some arbitrary defaults here; we could make these 849 * module parameters if anyone cared about setting them. 850 */ 851 req->ib_param.responder_resources = 4; 852 req->ib_param.rnr_retry_count = 7; 853 req->ib_param.max_cm_retries = 15; 854 855 req->ib_req.opcode = SRP_LOGIN_REQ; 856 req->ib_req.tag = 0; 857 req->ib_req.req_it_iu_len = cpu_to_be32(target->max_iu_len); 858 req->ib_req.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | 859 SRP_BUF_FORMAT_INDIRECT); 860 req->ib_req.req_flags = (multich ? SRP_MULTICHAN_MULTI : 861 SRP_MULTICHAN_SINGLE); 862 863 if (target->using_rdma_cm) { 864 req->rdma_param.flow_control = req->ib_param.flow_control; 865 req->rdma_param.responder_resources = 866 req->ib_param.responder_resources; 867 req->rdma_param.initiator_depth = req->ib_param.initiator_depth; 868 req->rdma_param.retry_count = req->ib_param.retry_count; 869 req->rdma_param.rnr_retry_count = req->ib_param.rnr_retry_count; 870 req->rdma_param.private_data = &req->rdma_req; 871 req->rdma_param.private_data_len = sizeof(req->rdma_req); 872 873 req->rdma_req.opcode = req->ib_req.opcode; 874 req->rdma_req.tag = req->ib_req.tag; 875 req->rdma_req.req_it_iu_len = req->ib_req.req_it_iu_len; 876 req->rdma_req.req_buf_fmt = req->ib_req.req_buf_fmt; 877 req->rdma_req.req_flags = req->ib_req.req_flags; 878 879 ipi = req->rdma_req.initiator_port_id; 880 tpi = req->rdma_req.target_port_id; 881 } else { 882 u8 subnet_timeout; 883 884 subnet_timeout = srp_get_subnet_timeout(target->srp_host); 885 886 req->ib_param.primary_path = &ch->ib_cm.path; 887 req->ib_param.alternate_path = NULL; 888 req->ib_param.service_id = target->ib_cm.service_id; 889 get_random_bytes(&req->ib_param.starting_psn, 4); 890 req->ib_param.starting_psn &= 0xffffff; 891 req->ib_param.qp_num = ch->qp->qp_num; 892 req->ib_param.qp_type = ch->qp->qp_type; 893 req->ib_param.local_cm_response_timeout = subnet_timeout + 2; 894 req->ib_param.remote_cm_response_timeout = subnet_timeout + 2; 895 req->ib_param.private_data = &req->ib_req; 896 req->ib_param.private_data_len = sizeof(req->ib_req); 897 898 ipi = req->ib_req.initiator_port_id; 899 tpi = req->ib_req.target_port_id; 900 } 901 902 /* 903 * In the published SRP specification (draft rev. 16a), the 904 * port identifier format is 8 bytes of ID extension followed 905 * by 8 bytes of GUID. Older drafts put the two halves in the 906 * opposite order, so that the GUID comes first. 907 * 908 * Targets conforming to these obsolete drafts can be 909 * recognized by the I/O Class they report. 910 */ 911 if (target->io_class == SRP_REV10_IB_IO_CLASS) { 912 memcpy(ipi, &target->sgid.global.interface_id, 8); 913 memcpy(ipi + 8, &target->initiator_ext, 8); 914 memcpy(tpi, &target->ioc_guid, 8); 915 memcpy(tpi + 8, &target->id_ext, 8); 916 } else { 917 memcpy(ipi, &target->initiator_ext, 8); 918 memcpy(ipi + 8, &target->sgid.global.interface_id, 8); 919 memcpy(tpi, &target->id_ext, 8); 920 memcpy(tpi + 8, &target->ioc_guid, 8); 921 } 922 923 /* 924 * Topspin/Cisco SRP targets will reject our login unless we 925 * zero out the first 8 bytes of our initiator port ID and set 926 * the second 8 bytes to the local node GUID. 927 */ 928 if (srp_target_is_topspin(target)) { 929 shost_printk(KERN_DEBUG, target->scsi_host, 930 PFX "Topspin/Cisco initiator port ID workaround " 931 "activated for target GUID %016llx\n", 932 be64_to_cpu(target->ioc_guid)); 933 memset(ipi, 0, 8); 934 memcpy(ipi + 8, &target->srp_host->srp_dev->dev->node_guid, 8); 935 } 936 937 if (target->using_rdma_cm) 938 status = rdma_connect(ch->rdma_cm.cm_id, &req->rdma_param); 939 else 940 status = ib_send_cm_req(ch->ib_cm.cm_id, &req->ib_param); 941 942 kfree(req); 943 944 return status; 945 } 946 947 static bool srp_queue_remove_work(struct srp_target_port *target) 948 { 949 bool changed = false; 950 951 spin_lock_irq(&target->lock); 952 if (target->state != SRP_TARGET_REMOVED) { 953 target->state = SRP_TARGET_REMOVED; 954 changed = true; 955 } 956 spin_unlock_irq(&target->lock); 957 958 if (changed) 959 queue_work(srp_remove_wq, &target->remove_work); 960 961 return changed; 962 } 963 964 static void srp_disconnect_target(struct srp_target_port *target) 965 { 966 struct srp_rdma_ch *ch; 967 int i, ret; 968 969 /* XXX should send SRP_I_LOGOUT request */ 970 971 for (i = 0; i < target->ch_count; i++) { 972 ch = &target->ch[i]; 973 ch->connected = false; 974 ret = 0; 975 if (target->using_rdma_cm) { 976 if (ch->rdma_cm.cm_id) 977 rdma_disconnect(ch->rdma_cm.cm_id); 978 } else { 979 if (ch->ib_cm.cm_id) 980 ret = ib_send_cm_dreq(ch->ib_cm.cm_id, 981 NULL, 0); 982 } 983 if (ret < 0) { 984 shost_printk(KERN_DEBUG, target->scsi_host, 985 PFX "Sending CM DREQ failed\n"); 986 } 987 } 988 } 989 990 static void srp_free_req_data(struct srp_target_port *target, 991 struct srp_rdma_ch *ch) 992 { 993 struct srp_device *dev = target->srp_host->srp_dev; 994 struct ib_device *ibdev = dev->dev; 995 struct srp_request *req; 996 int i; 997 998 if (!ch->req_ring) 999 return; 1000 1001 for (i = 0; i < target->req_ring_size; ++i) { 1002 req = &ch->req_ring[i]; 1003 if (dev->use_fast_reg) { 1004 kfree(req->fr_list); 1005 } else { 1006 kfree(req->fmr_list); 1007 kfree(req->map_page); 1008 } 1009 if (req->indirect_dma_addr) { 1010 ib_dma_unmap_single(ibdev, req->indirect_dma_addr, 1011 target->indirect_size, 1012 DMA_TO_DEVICE); 1013 } 1014 kfree(req->indirect_desc); 1015 } 1016 1017 kfree(ch->req_ring); 1018 ch->req_ring = NULL; 1019 } 1020 1021 static int srp_alloc_req_data(struct srp_rdma_ch *ch) 1022 { 1023 struct srp_target_port *target = ch->target; 1024 struct srp_device *srp_dev = target->srp_host->srp_dev; 1025 struct ib_device *ibdev = srp_dev->dev; 1026 struct srp_request *req; 1027 void *mr_list; 1028 dma_addr_t dma_addr; 1029 int i, ret = -ENOMEM; 1030 1031 ch->req_ring = kcalloc(target->req_ring_size, sizeof(*ch->req_ring), 1032 GFP_KERNEL); 1033 if (!ch->req_ring) 1034 goto out; 1035 1036 for (i = 0; i < target->req_ring_size; ++i) { 1037 req = &ch->req_ring[i]; 1038 mr_list = kmalloc(target->mr_per_cmd * sizeof(void *), 1039 GFP_KERNEL); 1040 if (!mr_list) 1041 goto out; 1042 if (srp_dev->use_fast_reg) { 1043 req->fr_list = mr_list; 1044 } else { 1045 req->fmr_list = mr_list; 1046 req->map_page = kmalloc(srp_dev->max_pages_per_mr * 1047 sizeof(void *), GFP_KERNEL); 1048 if (!req->map_page) 1049 goto out; 1050 } 1051 req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL); 1052 if (!req->indirect_desc) 1053 goto out; 1054 1055 dma_addr = ib_dma_map_single(ibdev, req->indirect_desc, 1056 target->indirect_size, 1057 DMA_TO_DEVICE); 1058 if (ib_dma_mapping_error(ibdev, dma_addr)) 1059 goto out; 1060 1061 req->indirect_dma_addr = dma_addr; 1062 } 1063 ret = 0; 1064 1065 out: 1066 return ret; 1067 } 1068 1069 /** 1070 * srp_del_scsi_host_attr() - Remove attributes defined in the host template. 1071 * @shost: SCSI host whose attributes to remove from sysfs. 1072 * 1073 * Note: Any attributes defined in the host template and that did not exist 1074 * before invocation of this function will be ignored. 1075 */ 1076 static void srp_del_scsi_host_attr(struct Scsi_Host *shost) 1077 { 1078 struct device_attribute **attr; 1079 1080 for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr) 1081 device_remove_file(&shost->shost_dev, *attr); 1082 } 1083 1084 static void srp_remove_target(struct srp_target_port *target) 1085 { 1086 struct srp_rdma_ch *ch; 1087 int i; 1088 1089 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED); 1090 1091 srp_del_scsi_host_attr(target->scsi_host); 1092 srp_rport_get(target->rport); 1093 srp_remove_host(target->scsi_host); 1094 scsi_remove_host(target->scsi_host); 1095 srp_stop_rport_timers(target->rport); 1096 srp_disconnect_target(target); 1097 kobj_ns_drop(KOBJ_NS_TYPE_NET, target->net); 1098 for (i = 0; i < target->ch_count; i++) { 1099 ch = &target->ch[i]; 1100 srp_free_ch_ib(target, ch); 1101 } 1102 cancel_work_sync(&target->tl_err_work); 1103 srp_rport_put(target->rport); 1104 for (i = 0; i < target->ch_count; i++) { 1105 ch = &target->ch[i]; 1106 srp_free_req_data(target, ch); 1107 } 1108 kfree(target->ch); 1109 target->ch = NULL; 1110 1111 spin_lock(&target->srp_host->target_lock); 1112 list_del(&target->list); 1113 spin_unlock(&target->srp_host->target_lock); 1114 1115 scsi_host_put(target->scsi_host); 1116 } 1117 1118 static void srp_remove_work(struct work_struct *work) 1119 { 1120 struct srp_target_port *target = 1121 container_of(work, struct srp_target_port, remove_work); 1122 1123 WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED); 1124 1125 srp_remove_target(target); 1126 } 1127 1128 static void srp_rport_delete(struct srp_rport *rport) 1129 { 1130 struct srp_target_port *target = rport->lld_data; 1131 1132 srp_queue_remove_work(target); 1133 } 1134 1135 /** 1136 * srp_connected_ch() - number of connected channels 1137 * @target: SRP target port. 1138 */ 1139 static int srp_connected_ch(struct srp_target_port *target) 1140 { 1141 int i, c = 0; 1142 1143 for (i = 0; i < target->ch_count; i++) 1144 c += target->ch[i].connected; 1145 1146 return c; 1147 } 1148 1149 static int srp_connect_ch(struct srp_rdma_ch *ch, bool multich) 1150 { 1151 struct srp_target_port *target = ch->target; 1152 int ret; 1153 1154 WARN_ON_ONCE(!multich && srp_connected_ch(target) > 0); 1155 1156 ret = srp_lookup_path(ch); 1157 if (ret) 1158 goto out; 1159 1160 while (1) { 1161 init_completion(&ch->done); 1162 ret = srp_send_req(ch, multich); 1163 if (ret) 1164 goto out; 1165 ret = wait_for_completion_interruptible(&ch->done); 1166 if (ret < 0) 1167 goto out; 1168 1169 /* 1170 * The CM event handling code will set status to 1171 * SRP_PORT_REDIRECT if we get a port redirect REJ 1172 * back, or SRP_DLID_REDIRECT if we get a lid/qp 1173 * redirect REJ back. 1174 */ 1175 ret = ch->status; 1176 switch (ret) { 1177 case 0: 1178 ch->connected = true; 1179 goto out; 1180 1181 case SRP_PORT_REDIRECT: 1182 ret = srp_lookup_path(ch); 1183 if (ret) 1184 goto out; 1185 break; 1186 1187 case SRP_DLID_REDIRECT: 1188 break; 1189 1190 case SRP_STALE_CONN: 1191 shost_printk(KERN_ERR, target->scsi_host, PFX 1192 "giving up on stale connection\n"); 1193 ret = -ECONNRESET; 1194 goto out; 1195 1196 default: 1197 goto out; 1198 } 1199 } 1200 1201 out: 1202 return ret <= 0 ? ret : -ENODEV; 1203 } 1204 1205 static void srp_inv_rkey_err_done(struct ib_cq *cq, struct ib_wc *wc) 1206 { 1207 srp_handle_qp_err(cq, wc, "INV RKEY"); 1208 } 1209 1210 static int srp_inv_rkey(struct srp_request *req, struct srp_rdma_ch *ch, 1211 u32 rkey) 1212 { 1213 struct ib_send_wr *bad_wr; 1214 struct ib_send_wr wr = { 1215 .opcode = IB_WR_LOCAL_INV, 1216 .next = NULL, 1217 .num_sge = 0, 1218 .send_flags = 0, 1219 .ex.invalidate_rkey = rkey, 1220 }; 1221 1222 wr.wr_cqe = &req->reg_cqe; 1223 req->reg_cqe.done = srp_inv_rkey_err_done; 1224 return ib_post_send(ch->qp, &wr, &bad_wr); 1225 } 1226 1227 static void srp_unmap_data(struct scsi_cmnd *scmnd, 1228 struct srp_rdma_ch *ch, 1229 struct srp_request *req) 1230 { 1231 struct srp_target_port *target = ch->target; 1232 struct srp_device *dev = target->srp_host->srp_dev; 1233 struct ib_device *ibdev = dev->dev; 1234 int i, res; 1235 1236 if (!scsi_sglist(scmnd) || 1237 (scmnd->sc_data_direction != DMA_TO_DEVICE && 1238 scmnd->sc_data_direction != DMA_FROM_DEVICE)) 1239 return; 1240 1241 if (dev->use_fast_reg) { 1242 struct srp_fr_desc **pfr; 1243 1244 for (i = req->nmdesc, pfr = req->fr_list; i > 0; i--, pfr++) { 1245 res = srp_inv_rkey(req, ch, (*pfr)->mr->rkey); 1246 if (res < 0) { 1247 shost_printk(KERN_ERR, target->scsi_host, PFX 1248 "Queueing INV WR for rkey %#x failed (%d)\n", 1249 (*pfr)->mr->rkey, res); 1250 queue_work(system_long_wq, 1251 &target->tl_err_work); 1252 } 1253 } 1254 if (req->nmdesc) 1255 srp_fr_pool_put(ch->fr_pool, req->fr_list, 1256 req->nmdesc); 1257 } else if (dev->use_fmr) { 1258 struct ib_pool_fmr **pfmr; 1259 1260 for (i = req->nmdesc, pfmr = req->fmr_list; i > 0; i--, pfmr++) 1261 ib_fmr_pool_unmap(*pfmr); 1262 } 1263 1264 ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd), 1265 scmnd->sc_data_direction); 1266 } 1267 1268 /** 1269 * srp_claim_req - Take ownership of the scmnd associated with a request. 1270 * @ch: SRP RDMA channel. 1271 * @req: SRP request. 1272 * @sdev: If not NULL, only take ownership for this SCSI device. 1273 * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take 1274 * ownership of @req->scmnd if it equals @scmnd. 1275 * 1276 * Return value: 1277 * Either NULL or a pointer to the SCSI command the caller became owner of. 1278 */ 1279 static struct scsi_cmnd *srp_claim_req(struct srp_rdma_ch *ch, 1280 struct srp_request *req, 1281 struct scsi_device *sdev, 1282 struct scsi_cmnd *scmnd) 1283 { 1284 unsigned long flags; 1285 1286 spin_lock_irqsave(&ch->lock, flags); 1287 if (req->scmnd && 1288 (!sdev || req->scmnd->device == sdev) && 1289 (!scmnd || req->scmnd == scmnd)) { 1290 scmnd = req->scmnd; 1291 req->scmnd = NULL; 1292 } else { 1293 scmnd = NULL; 1294 } 1295 spin_unlock_irqrestore(&ch->lock, flags); 1296 1297 return scmnd; 1298 } 1299 1300 /** 1301 * srp_free_req() - Unmap data and adjust ch->req_lim. 1302 * @ch: SRP RDMA channel. 1303 * @req: Request to be freed. 1304 * @scmnd: SCSI command associated with @req. 1305 * @req_lim_delta: Amount to be added to @target->req_lim. 1306 */ 1307 static void srp_free_req(struct srp_rdma_ch *ch, struct srp_request *req, 1308 struct scsi_cmnd *scmnd, s32 req_lim_delta) 1309 { 1310 unsigned long flags; 1311 1312 srp_unmap_data(scmnd, ch, req); 1313 1314 spin_lock_irqsave(&ch->lock, flags); 1315 ch->req_lim += req_lim_delta; 1316 spin_unlock_irqrestore(&ch->lock, flags); 1317 } 1318 1319 static void srp_finish_req(struct srp_rdma_ch *ch, struct srp_request *req, 1320 struct scsi_device *sdev, int result) 1321 { 1322 struct scsi_cmnd *scmnd = srp_claim_req(ch, req, sdev, NULL); 1323 1324 if (scmnd) { 1325 srp_free_req(ch, req, scmnd, 0); 1326 scmnd->result = result; 1327 scmnd->scsi_done(scmnd); 1328 } 1329 } 1330 1331 static void srp_terminate_io(struct srp_rport *rport) 1332 { 1333 struct srp_target_port *target = rport->lld_data; 1334 struct srp_rdma_ch *ch; 1335 struct Scsi_Host *shost = target->scsi_host; 1336 struct scsi_device *sdev; 1337 int i, j; 1338 1339 /* 1340 * Invoking srp_terminate_io() while srp_queuecommand() is running 1341 * is not safe. Hence the warning statement below. 1342 */ 1343 shost_for_each_device(sdev, shost) 1344 WARN_ON_ONCE(sdev->request_queue->request_fn_active); 1345 1346 for (i = 0; i < target->ch_count; i++) { 1347 ch = &target->ch[i]; 1348 1349 for (j = 0; j < target->req_ring_size; ++j) { 1350 struct srp_request *req = &ch->req_ring[j]; 1351 1352 srp_finish_req(ch, req, NULL, 1353 DID_TRANSPORT_FAILFAST << 16); 1354 } 1355 } 1356 } 1357 1358 /* 1359 * It is up to the caller to ensure that srp_rport_reconnect() calls are 1360 * serialized and that no concurrent srp_queuecommand(), srp_abort(), 1361 * srp_reset_device() or srp_reset_host() calls will occur while this function 1362 * is in progress. One way to realize that is not to call this function 1363 * directly but to call srp_reconnect_rport() instead since that last function 1364 * serializes calls of this function via rport->mutex and also blocks 1365 * srp_queuecommand() calls before invoking this function. 1366 */ 1367 static int srp_rport_reconnect(struct srp_rport *rport) 1368 { 1369 struct srp_target_port *target = rport->lld_data; 1370 struct srp_rdma_ch *ch; 1371 int i, j, ret = 0; 1372 bool multich = false; 1373 1374 srp_disconnect_target(target); 1375 1376 if (target->state == SRP_TARGET_SCANNING) 1377 return -ENODEV; 1378 1379 /* 1380 * Now get a new local CM ID so that we avoid confusing the target in 1381 * case things are really fouled up. Doing so also ensures that all CM 1382 * callbacks will have finished before a new QP is allocated. 1383 */ 1384 for (i = 0; i < target->ch_count; i++) { 1385 ch = &target->ch[i]; 1386 ret += srp_new_cm_id(ch); 1387 } 1388 for (i = 0; i < target->ch_count; i++) { 1389 ch = &target->ch[i]; 1390 for (j = 0; j < target->req_ring_size; ++j) { 1391 struct srp_request *req = &ch->req_ring[j]; 1392 1393 srp_finish_req(ch, req, NULL, DID_RESET << 16); 1394 } 1395 } 1396 for (i = 0; i < target->ch_count; i++) { 1397 ch = &target->ch[i]; 1398 /* 1399 * Whether or not creating a new CM ID succeeded, create a new 1400 * QP. This guarantees that all completion callback function 1401 * invocations have finished before request resetting starts. 1402 */ 1403 ret += srp_create_ch_ib(ch); 1404 1405 INIT_LIST_HEAD(&ch->free_tx); 1406 for (j = 0; j < target->queue_size; ++j) 1407 list_add(&ch->tx_ring[j]->list, &ch->free_tx); 1408 } 1409 1410 target->qp_in_error = false; 1411 1412 for (i = 0; i < target->ch_count; i++) { 1413 ch = &target->ch[i]; 1414 if (ret) 1415 break; 1416 ret = srp_connect_ch(ch, multich); 1417 multich = true; 1418 } 1419 1420 if (ret == 0) 1421 shost_printk(KERN_INFO, target->scsi_host, 1422 PFX "reconnect succeeded\n"); 1423 1424 return ret; 1425 } 1426 1427 static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr, 1428 unsigned int dma_len, u32 rkey) 1429 { 1430 struct srp_direct_buf *desc = state->desc; 1431 1432 WARN_ON_ONCE(!dma_len); 1433 1434 desc->va = cpu_to_be64(dma_addr); 1435 desc->key = cpu_to_be32(rkey); 1436 desc->len = cpu_to_be32(dma_len); 1437 1438 state->total_len += dma_len; 1439 state->desc++; 1440 state->ndesc++; 1441 } 1442 1443 static int srp_map_finish_fmr(struct srp_map_state *state, 1444 struct srp_rdma_ch *ch) 1445 { 1446 struct srp_target_port *target = ch->target; 1447 struct srp_device *dev = target->srp_host->srp_dev; 1448 struct ib_pool_fmr *fmr; 1449 u64 io_addr = 0; 1450 1451 if (state->fmr.next >= state->fmr.end) { 1452 shost_printk(KERN_ERR, ch->target->scsi_host, 1453 PFX "Out of MRs (mr_per_cmd = %d)\n", 1454 ch->target->mr_per_cmd); 1455 return -ENOMEM; 1456 } 1457 1458 WARN_ON_ONCE(!dev->use_fmr); 1459 1460 if (state->npages == 0) 1461 return 0; 1462 1463 if (state->npages == 1 && target->global_rkey) { 1464 srp_map_desc(state, state->base_dma_addr, state->dma_len, 1465 target->global_rkey); 1466 goto reset_state; 1467 } 1468 1469 fmr = ib_fmr_pool_map_phys(ch->fmr_pool, state->pages, 1470 state->npages, io_addr); 1471 if (IS_ERR(fmr)) 1472 return PTR_ERR(fmr); 1473 1474 *state->fmr.next++ = fmr; 1475 state->nmdesc++; 1476 1477 srp_map_desc(state, state->base_dma_addr & ~dev->mr_page_mask, 1478 state->dma_len, fmr->fmr->rkey); 1479 1480 reset_state: 1481 state->npages = 0; 1482 state->dma_len = 0; 1483 1484 return 0; 1485 } 1486 1487 static void srp_reg_mr_err_done(struct ib_cq *cq, struct ib_wc *wc) 1488 { 1489 srp_handle_qp_err(cq, wc, "FAST REG"); 1490 } 1491 1492 /* 1493 * Map up to sg_nents elements of state->sg where *sg_offset_p is the offset 1494 * where to start in the first element. If sg_offset_p != NULL then 1495 * *sg_offset_p is updated to the offset in state->sg[retval] of the first 1496 * byte that has not yet been mapped. 1497 */ 1498 static int srp_map_finish_fr(struct srp_map_state *state, 1499 struct srp_request *req, 1500 struct srp_rdma_ch *ch, int sg_nents, 1501 unsigned int *sg_offset_p) 1502 { 1503 struct srp_target_port *target = ch->target; 1504 struct srp_device *dev = target->srp_host->srp_dev; 1505 struct ib_send_wr *bad_wr; 1506 struct ib_reg_wr wr; 1507 struct srp_fr_desc *desc; 1508 u32 rkey; 1509 int n, err; 1510 1511 if (state->fr.next >= state->fr.end) { 1512 shost_printk(KERN_ERR, ch->target->scsi_host, 1513 PFX "Out of MRs (mr_per_cmd = %d)\n", 1514 ch->target->mr_per_cmd); 1515 return -ENOMEM; 1516 } 1517 1518 WARN_ON_ONCE(!dev->use_fast_reg); 1519 1520 if (sg_nents == 1 && target->global_rkey) { 1521 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0; 1522 1523 srp_map_desc(state, sg_dma_address(state->sg) + sg_offset, 1524 sg_dma_len(state->sg) - sg_offset, 1525 target->global_rkey); 1526 if (sg_offset_p) 1527 *sg_offset_p = 0; 1528 return 1; 1529 } 1530 1531 desc = srp_fr_pool_get(ch->fr_pool); 1532 if (!desc) 1533 return -ENOMEM; 1534 1535 rkey = ib_inc_rkey(desc->mr->rkey); 1536 ib_update_fast_reg_key(desc->mr, rkey); 1537 1538 n = ib_map_mr_sg(desc->mr, state->sg, sg_nents, sg_offset_p, 1539 dev->mr_page_size); 1540 if (unlikely(n < 0)) { 1541 srp_fr_pool_put(ch->fr_pool, &desc, 1); 1542 pr_debug("%s: ib_map_mr_sg(%d, %d) returned %d.\n", 1543 dev_name(&req->scmnd->device->sdev_gendev), sg_nents, 1544 sg_offset_p ? *sg_offset_p : -1, n); 1545 return n; 1546 } 1547 1548 WARN_ON_ONCE(desc->mr->length == 0); 1549 1550 req->reg_cqe.done = srp_reg_mr_err_done; 1551 1552 wr.wr.next = NULL; 1553 wr.wr.opcode = IB_WR_REG_MR; 1554 wr.wr.wr_cqe = &req->reg_cqe; 1555 wr.wr.num_sge = 0; 1556 wr.wr.send_flags = 0; 1557 wr.mr = desc->mr; 1558 wr.key = desc->mr->rkey; 1559 wr.access = (IB_ACCESS_LOCAL_WRITE | 1560 IB_ACCESS_REMOTE_READ | 1561 IB_ACCESS_REMOTE_WRITE); 1562 1563 *state->fr.next++ = desc; 1564 state->nmdesc++; 1565 1566 srp_map_desc(state, desc->mr->iova, 1567 desc->mr->length, desc->mr->rkey); 1568 1569 err = ib_post_send(ch->qp, &wr.wr, &bad_wr); 1570 if (unlikely(err)) { 1571 WARN_ON_ONCE(err == -ENOMEM); 1572 return err; 1573 } 1574 1575 return n; 1576 } 1577 1578 static int srp_map_sg_entry(struct srp_map_state *state, 1579 struct srp_rdma_ch *ch, 1580 struct scatterlist *sg) 1581 { 1582 struct srp_target_port *target = ch->target; 1583 struct srp_device *dev = target->srp_host->srp_dev; 1584 struct ib_device *ibdev = dev->dev; 1585 dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg); 1586 unsigned int dma_len = ib_sg_dma_len(ibdev, sg); 1587 unsigned int len = 0; 1588 int ret; 1589 1590 WARN_ON_ONCE(!dma_len); 1591 1592 while (dma_len) { 1593 unsigned offset = dma_addr & ~dev->mr_page_mask; 1594 1595 if (state->npages == dev->max_pages_per_mr || 1596 (state->npages > 0 && offset != 0)) { 1597 ret = srp_map_finish_fmr(state, ch); 1598 if (ret) 1599 return ret; 1600 } 1601 1602 len = min_t(unsigned int, dma_len, dev->mr_page_size - offset); 1603 1604 if (!state->npages) 1605 state->base_dma_addr = dma_addr; 1606 state->pages[state->npages++] = dma_addr & dev->mr_page_mask; 1607 state->dma_len += len; 1608 dma_addr += len; 1609 dma_len -= len; 1610 } 1611 1612 /* 1613 * If the end of the MR is not on a page boundary then we need to 1614 * close it out and start a new one -- we can only merge at page 1615 * boundaries. 1616 */ 1617 ret = 0; 1618 if ((dma_addr & ~dev->mr_page_mask) != 0) 1619 ret = srp_map_finish_fmr(state, ch); 1620 return ret; 1621 } 1622 1623 static int srp_map_sg_fmr(struct srp_map_state *state, struct srp_rdma_ch *ch, 1624 struct srp_request *req, struct scatterlist *scat, 1625 int count) 1626 { 1627 struct scatterlist *sg; 1628 int i, ret; 1629 1630 state->pages = req->map_page; 1631 state->fmr.next = req->fmr_list; 1632 state->fmr.end = req->fmr_list + ch->target->mr_per_cmd; 1633 1634 for_each_sg(scat, sg, count, i) { 1635 ret = srp_map_sg_entry(state, ch, sg); 1636 if (ret) 1637 return ret; 1638 } 1639 1640 ret = srp_map_finish_fmr(state, ch); 1641 if (ret) 1642 return ret; 1643 1644 return 0; 1645 } 1646 1647 static int srp_map_sg_fr(struct srp_map_state *state, struct srp_rdma_ch *ch, 1648 struct srp_request *req, struct scatterlist *scat, 1649 int count) 1650 { 1651 unsigned int sg_offset = 0; 1652 1653 state->fr.next = req->fr_list; 1654 state->fr.end = req->fr_list + ch->target->mr_per_cmd; 1655 state->sg = scat; 1656 1657 if (count == 0) 1658 return 0; 1659 1660 while (count) { 1661 int i, n; 1662 1663 n = srp_map_finish_fr(state, req, ch, count, &sg_offset); 1664 if (unlikely(n < 0)) 1665 return n; 1666 1667 count -= n; 1668 for (i = 0; i < n; i++) 1669 state->sg = sg_next(state->sg); 1670 } 1671 1672 return 0; 1673 } 1674 1675 static int srp_map_sg_dma(struct srp_map_state *state, struct srp_rdma_ch *ch, 1676 struct srp_request *req, struct scatterlist *scat, 1677 int count) 1678 { 1679 struct srp_target_port *target = ch->target; 1680 struct srp_device *dev = target->srp_host->srp_dev; 1681 struct scatterlist *sg; 1682 int i; 1683 1684 for_each_sg(scat, sg, count, i) { 1685 srp_map_desc(state, ib_sg_dma_address(dev->dev, sg), 1686 ib_sg_dma_len(dev->dev, sg), 1687 target->global_rkey); 1688 } 1689 1690 return 0; 1691 } 1692 1693 /* 1694 * Register the indirect data buffer descriptor with the HCA. 1695 * 1696 * Note: since the indirect data buffer descriptor has been allocated with 1697 * kmalloc() it is guaranteed that this buffer is a physically contiguous 1698 * memory buffer. 1699 */ 1700 static int srp_map_idb(struct srp_rdma_ch *ch, struct srp_request *req, 1701 void **next_mr, void **end_mr, u32 idb_len, 1702 __be32 *idb_rkey) 1703 { 1704 struct srp_target_port *target = ch->target; 1705 struct srp_device *dev = target->srp_host->srp_dev; 1706 struct srp_map_state state; 1707 struct srp_direct_buf idb_desc; 1708 u64 idb_pages[1]; 1709 struct scatterlist idb_sg[1]; 1710 int ret; 1711 1712 memset(&state, 0, sizeof(state)); 1713 memset(&idb_desc, 0, sizeof(idb_desc)); 1714 state.gen.next = next_mr; 1715 state.gen.end = end_mr; 1716 state.desc = &idb_desc; 1717 state.base_dma_addr = req->indirect_dma_addr; 1718 state.dma_len = idb_len; 1719 1720 if (dev->use_fast_reg) { 1721 state.sg = idb_sg; 1722 sg_init_one(idb_sg, req->indirect_desc, idb_len); 1723 idb_sg->dma_address = req->indirect_dma_addr; /* hack! */ 1724 #ifdef CONFIG_NEED_SG_DMA_LENGTH 1725 idb_sg->dma_length = idb_sg->length; /* hack^2 */ 1726 #endif 1727 ret = srp_map_finish_fr(&state, req, ch, 1, NULL); 1728 if (ret < 0) 1729 return ret; 1730 WARN_ON_ONCE(ret < 1); 1731 } else if (dev->use_fmr) { 1732 state.pages = idb_pages; 1733 state.pages[0] = (req->indirect_dma_addr & 1734 dev->mr_page_mask); 1735 state.npages = 1; 1736 ret = srp_map_finish_fmr(&state, ch); 1737 if (ret < 0) 1738 return ret; 1739 } else { 1740 return -EINVAL; 1741 } 1742 1743 *idb_rkey = idb_desc.key; 1744 1745 return 0; 1746 } 1747 1748 static void srp_check_mapping(struct srp_map_state *state, 1749 struct srp_rdma_ch *ch, struct srp_request *req, 1750 struct scatterlist *scat, int count) 1751 { 1752 struct srp_device *dev = ch->target->srp_host->srp_dev; 1753 struct srp_fr_desc **pfr; 1754 u64 desc_len = 0, mr_len = 0; 1755 int i; 1756 1757 for (i = 0; i < state->ndesc; i++) 1758 desc_len += be32_to_cpu(req->indirect_desc[i].len); 1759 if (dev->use_fast_reg) 1760 for (i = 0, pfr = req->fr_list; i < state->nmdesc; i++, pfr++) 1761 mr_len += (*pfr)->mr->length; 1762 else if (dev->use_fmr) 1763 for (i = 0; i < state->nmdesc; i++) 1764 mr_len += be32_to_cpu(req->indirect_desc[i].len); 1765 if (desc_len != scsi_bufflen(req->scmnd) || 1766 mr_len > scsi_bufflen(req->scmnd)) 1767 pr_err("Inconsistent: scsi len %d <> desc len %lld <> mr len %lld; ndesc %d; nmdesc = %d\n", 1768 scsi_bufflen(req->scmnd), desc_len, mr_len, 1769 state->ndesc, state->nmdesc); 1770 } 1771 1772 /** 1773 * srp_map_data() - map SCSI data buffer onto an SRP request 1774 * @scmnd: SCSI command to map 1775 * @ch: SRP RDMA channel 1776 * @req: SRP request 1777 * 1778 * Returns the length in bytes of the SRP_CMD IU or a negative value if 1779 * mapping failed. 1780 */ 1781 static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_rdma_ch *ch, 1782 struct srp_request *req) 1783 { 1784 struct srp_target_port *target = ch->target; 1785 struct scatterlist *scat; 1786 struct srp_cmd *cmd = req->cmd->buf; 1787 int len, nents, count, ret; 1788 struct srp_device *dev; 1789 struct ib_device *ibdev; 1790 struct srp_map_state state; 1791 struct srp_indirect_buf *indirect_hdr; 1792 u32 idb_len, table_len; 1793 __be32 idb_rkey; 1794 u8 fmt; 1795 1796 if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE) 1797 return sizeof (struct srp_cmd); 1798 1799 if (scmnd->sc_data_direction != DMA_FROM_DEVICE && 1800 scmnd->sc_data_direction != DMA_TO_DEVICE) { 1801 shost_printk(KERN_WARNING, target->scsi_host, 1802 PFX "Unhandled data direction %d\n", 1803 scmnd->sc_data_direction); 1804 return -EINVAL; 1805 } 1806 1807 nents = scsi_sg_count(scmnd); 1808 scat = scsi_sglist(scmnd); 1809 1810 dev = target->srp_host->srp_dev; 1811 ibdev = dev->dev; 1812 1813 count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction); 1814 if (unlikely(count == 0)) 1815 return -EIO; 1816 1817 fmt = SRP_DATA_DESC_DIRECT; 1818 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf); 1819 1820 if (count == 1 && target->global_rkey) { 1821 /* 1822 * The midlayer only generated a single gather/scatter 1823 * entry, or DMA mapping coalesced everything to a 1824 * single entry. So a direct descriptor along with 1825 * the DMA MR suffices. 1826 */ 1827 struct srp_direct_buf *buf = (void *) cmd->add_data; 1828 1829 buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat)); 1830 buf->key = cpu_to_be32(target->global_rkey); 1831 buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat)); 1832 1833 req->nmdesc = 0; 1834 goto map_complete; 1835 } 1836 1837 /* 1838 * We have more than one scatter/gather entry, so build our indirect 1839 * descriptor table, trying to merge as many entries as we can. 1840 */ 1841 indirect_hdr = (void *) cmd->add_data; 1842 1843 ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr, 1844 target->indirect_size, DMA_TO_DEVICE); 1845 1846 memset(&state, 0, sizeof(state)); 1847 state.desc = req->indirect_desc; 1848 if (dev->use_fast_reg) 1849 ret = srp_map_sg_fr(&state, ch, req, scat, count); 1850 else if (dev->use_fmr) 1851 ret = srp_map_sg_fmr(&state, ch, req, scat, count); 1852 else 1853 ret = srp_map_sg_dma(&state, ch, req, scat, count); 1854 req->nmdesc = state.nmdesc; 1855 if (ret < 0) 1856 goto unmap; 1857 1858 { 1859 DEFINE_DYNAMIC_DEBUG_METADATA(ddm, 1860 "Memory mapping consistency check"); 1861 if (DYNAMIC_DEBUG_BRANCH(ddm)) 1862 srp_check_mapping(&state, ch, req, scat, count); 1863 } 1864 1865 /* We've mapped the request, now pull as much of the indirect 1866 * descriptor table as we can into the command buffer. If this 1867 * target is not using an external indirect table, we are 1868 * guaranteed to fit into the command, as the SCSI layer won't 1869 * give us more S/G entries than we allow. 1870 */ 1871 if (state.ndesc == 1) { 1872 /* 1873 * Memory registration collapsed the sg-list into one entry, 1874 * so use a direct descriptor. 1875 */ 1876 struct srp_direct_buf *buf = (void *) cmd->add_data; 1877 1878 *buf = req->indirect_desc[0]; 1879 goto map_complete; 1880 } 1881 1882 if (unlikely(target->cmd_sg_cnt < state.ndesc && 1883 !target->allow_ext_sg)) { 1884 shost_printk(KERN_ERR, target->scsi_host, 1885 "Could not fit S/G list into SRP_CMD\n"); 1886 ret = -EIO; 1887 goto unmap; 1888 } 1889 1890 count = min(state.ndesc, target->cmd_sg_cnt); 1891 table_len = state.ndesc * sizeof (struct srp_direct_buf); 1892 idb_len = sizeof(struct srp_indirect_buf) + table_len; 1893 1894 fmt = SRP_DATA_DESC_INDIRECT; 1895 len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf); 1896 len += count * sizeof (struct srp_direct_buf); 1897 1898 memcpy(indirect_hdr->desc_list, req->indirect_desc, 1899 count * sizeof (struct srp_direct_buf)); 1900 1901 if (!target->global_rkey) { 1902 ret = srp_map_idb(ch, req, state.gen.next, state.gen.end, 1903 idb_len, &idb_rkey); 1904 if (ret < 0) 1905 goto unmap; 1906 req->nmdesc++; 1907 } else { 1908 idb_rkey = cpu_to_be32(target->global_rkey); 1909 } 1910 1911 indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr); 1912 indirect_hdr->table_desc.key = idb_rkey; 1913 indirect_hdr->table_desc.len = cpu_to_be32(table_len); 1914 indirect_hdr->len = cpu_to_be32(state.total_len); 1915 1916 if (scmnd->sc_data_direction == DMA_TO_DEVICE) 1917 cmd->data_out_desc_cnt = count; 1918 else 1919 cmd->data_in_desc_cnt = count; 1920 1921 ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len, 1922 DMA_TO_DEVICE); 1923 1924 map_complete: 1925 if (scmnd->sc_data_direction == DMA_TO_DEVICE) 1926 cmd->buf_fmt = fmt << 4; 1927 else 1928 cmd->buf_fmt = fmt; 1929 1930 return len; 1931 1932 unmap: 1933 srp_unmap_data(scmnd, ch, req); 1934 if (ret == -ENOMEM && req->nmdesc >= target->mr_pool_size) 1935 ret = -E2BIG; 1936 return ret; 1937 } 1938 1939 /* 1940 * Return an IU and possible credit to the free pool 1941 */ 1942 static void srp_put_tx_iu(struct srp_rdma_ch *ch, struct srp_iu *iu, 1943 enum srp_iu_type iu_type) 1944 { 1945 unsigned long flags; 1946 1947 spin_lock_irqsave(&ch->lock, flags); 1948 list_add(&iu->list, &ch->free_tx); 1949 if (iu_type != SRP_IU_RSP) 1950 ++ch->req_lim; 1951 spin_unlock_irqrestore(&ch->lock, flags); 1952 } 1953 1954 /* 1955 * Must be called with ch->lock held to protect req_lim and free_tx. 1956 * If IU is not sent, it must be returned using srp_put_tx_iu(). 1957 * 1958 * Note: 1959 * An upper limit for the number of allocated information units for each 1960 * request type is: 1961 * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues 1962 * more than Scsi_Host.can_queue requests. 1963 * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE. 1964 * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than 1965 * one unanswered SRP request to an initiator. 1966 */ 1967 static struct srp_iu *__srp_get_tx_iu(struct srp_rdma_ch *ch, 1968 enum srp_iu_type iu_type) 1969 { 1970 struct srp_target_port *target = ch->target; 1971 s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE; 1972 struct srp_iu *iu; 1973 1974 lockdep_assert_held(&ch->lock); 1975 1976 ib_process_cq_direct(ch->send_cq, -1); 1977 1978 if (list_empty(&ch->free_tx)) 1979 return NULL; 1980 1981 /* Initiator responses to target requests do not consume credits */ 1982 if (iu_type != SRP_IU_RSP) { 1983 if (ch->req_lim <= rsv) { 1984 ++target->zero_req_lim; 1985 return NULL; 1986 } 1987 1988 --ch->req_lim; 1989 } 1990 1991 iu = list_first_entry(&ch->free_tx, struct srp_iu, list); 1992 list_del(&iu->list); 1993 return iu; 1994 } 1995 1996 /* 1997 * Note: if this function is called from inside ib_drain_sq() then it will 1998 * be called without ch->lock being held. If ib_drain_sq() dequeues a WQE 1999 * with status IB_WC_SUCCESS then that's a bug. 2000 */ 2001 static void srp_send_done(struct ib_cq *cq, struct ib_wc *wc) 2002 { 2003 struct srp_iu *iu = container_of(wc->wr_cqe, struct srp_iu, cqe); 2004 struct srp_rdma_ch *ch = cq->cq_context; 2005 2006 if (unlikely(wc->status != IB_WC_SUCCESS)) { 2007 srp_handle_qp_err(cq, wc, "SEND"); 2008 return; 2009 } 2010 2011 lockdep_assert_held(&ch->lock); 2012 2013 list_add(&iu->list, &ch->free_tx); 2014 } 2015 2016 static int srp_post_send(struct srp_rdma_ch *ch, struct srp_iu *iu, int len) 2017 { 2018 struct srp_target_port *target = ch->target; 2019 struct ib_sge list; 2020 struct ib_send_wr wr, *bad_wr; 2021 2022 list.addr = iu->dma; 2023 list.length = len; 2024 list.lkey = target->lkey; 2025 2026 iu->cqe.done = srp_send_done; 2027 2028 wr.next = NULL; 2029 wr.wr_cqe = &iu->cqe; 2030 wr.sg_list = &list; 2031 wr.num_sge = 1; 2032 wr.opcode = IB_WR_SEND; 2033 wr.send_flags = IB_SEND_SIGNALED; 2034 2035 return ib_post_send(ch->qp, &wr, &bad_wr); 2036 } 2037 2038 static int srp_post_recv(struct srp_rdma_ch *ch, struct srp_iu *iu) 2039 { 2040 struct srp_target_port *target = ch->target; 2041 struct ib_recv_wr wr, *bad_wr; 2042 struct ib_sge list; 2043 2044 list.addr = iu->dma; 2045 list.length = iu->size; 2046 list.lkey = target->lkey; 2047 2048 iu->cqe.done = srp_recv_done; 2049 2050 wr.next = NULL; 2051 wr.wr_cqe = &iu->cqe; 2052 wr.sg_list = &list; 2053 wr.num_sge = 1; 2054 2055 return ib_post_recv(ch->qp, &wr, &bad_wr); 2056 } 2057 2058 static void srp_process_rsp(struct srp_rdma_ch *ch, struct srp_rsp *rsp) 2059 { 2060 struct srp_target_port *target = ch->target; 2061 struct srp_request *req; 2062 struct scsi_cmnd *scmnd; 2063 unsigned long flags; 2064 2065 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) { 2066 spin_lock_irqsave(&ch->lock, flags); 2067 ch->req_lim += be32_to_cpu(rsp->req_lim_delta); 2068 if (rsp->tag == ch->tsk_mgmt_tag) { 2069 ch->tsk_mgmt_status = -1; 2070 if (be32_to_cpu(rsp->resp_data_len) >= 4) 2071 ch->tsk_mgmt_status = rsp->data[3]; 2072 complete(&ch->tsk_mgmt_done); 2073 } else { 2074 shost_printk(KERN_ERR, target->scsi_host, 2075 "Received tsk mgmt response too late for tag %#llx\n", 2076 rsp->tag); 2077 } 2078 spin_unlock_irqrestore(&ch->lock, flags); 2079 } else { 2080 scmnd = scsi_host_find_tag(target->scsi_host, rsp->tag); 2081 if (scmnd && scmnd->host_scribble) { 2082 req = (void *)scmnd->host_scribble; 2083 scmnd = srp_claim_req(ch, req, NULL, scmnd); 2084 } else { 2085 scmnd = NULL; 2086 } 2087 if (!scmnd) { 2088 shost_printk(KERN_ERR, target->scsi_host, 2089 "Null scmnd for RSP w/tag %#016llx received on ch %td / QP %#x\n", 2090 rsp->tag, ch - target->ch, ch->qp->qp_num); 2091 2092 spin_lock_irqsave(&ch->lock, flags); 2093 ch->req_lim += be32_to_cpu(rsp->req_lim_delta); 2094 spin_unlock_irqrestore(&ch->lock, flags); 2095 2096 return; 2097 } 2098 scmnd->result = rsp->status; 2099 2100 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) { 2101 memcpy(scmnd->sense_buffer, rsp->data + 2102 be32_to_cpu(rsp->resp_data_len), 2103 min_t(int, be32_to_cpu(rsp->sense_data_len), 2104 SCSI_SENSE_BUFFERSIZE)); 2105 } 2106 2107 if (unlikely(rsp->flags & SRP_RSP_FLAG_DIUNDER)) 2108 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt)); 2109 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DIOVER)) 2110 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_in_res_cnt)); 2111 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOUNDER)) 2112 scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt)); 2113 else if (unlikely(rsp->flags & SRP_RSP_FLAG_DOOVER)) 2114 scsi_set_resid(scmnd, -be32_to_cpu(rsp->data_out_res_cnt)); 2115 2116 srp_free_req(ch, req, scmnd, 2117 be32_to_cpu(rsp->req_lim_delta)); 2118 2119 scmnd->host_scribble = NULL; 2120 scmnd->scsi_done(scmnd); 2121 } 2122 } 2123 2124 static int srp_response_common(struct srp_rdma_ch *ch, s32 req_delta, 2125 void *rsp, int len) 2126 { 2127 struct srp_target_port *target = ch->target; 2128 struct ib_device *dev = target->srp_host->srp_dev->dev; 2129 unsigned long flags; 2130 struct srp_iu *iu; 2131 int err; 2132 2133 spin_lock_irqsave(&ch->lock, flags); 2134 ch->req_lim += req_delta; 2135 iu = __srp_get_tx_iu(ch, SRP_IU_RSP); 2136 spin_unlock_irqrestore(&ch->lock, flags); 2137 2138 if (!iu) { 2139 shost_printk(KERN_ERR, target->scsi_host, PFX 2140 "no IU available to send response\n"); 2141 return 1; 2142 } 2143 2144 ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE); 2145 memcpy(iu->buf, rsp, len); 2146 ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE); 2147 2148 err = srp_post_send(ch, iu, len); 2149 if (err) { 2150 shost_printk(KERN_ERR, target->scsi_host, PFX 2151 "unable to post response: %d\n", err); 2152 srp_put_tx_iu(ch, iu, SRP_IU_RSP); 2153 } 2154 2155 return err; 2156 } 2157 2158 static void srp_process_cred_req(struct srp_rdma_ch *ch, 2159 struct srp_cred_req *req) 2160 { 2161 struct srp_cred_rsp rsp = { 2162 .opcode = SRP_CRED_RSP, 2163 .tag = req->tag, 2164 }; 2165 s32 delta = be32_to_cpu(req->req_lim_delta); 2166 2167 if (srp_response_common(ch, delta, &rsp, sizeof(rsp))) 2168 shost_printk(KERN_ERR, ch->target->scsi_host, PFX 2169 "problems processing SRP_CRED_REQ\n"); 2170 } 2171 2172 static void srp_process_aer_req(struct srp_rdma_ch *ch, 2173 struct srp_aer_req *req) 2174 { 2175 struct srp_target_port *target = ch->target; 2176 struct srp_aer_rsp rsp = { 2177 .opcode = SRP_AER_RSP, 2178 .tag = req->tag, 2179 }; 2180 s32 delta = be32_to_cpu(req->req_lim_delta); 2181 2182 shost_printk(KERN_ERR, target->scsi_host, PFX 2183 "ignoring AER for LUN %llu\n", scsilun_to_int(&req->lun)); 2184 2185 if (srp_response_common(ch, delta, &rsp, sizeof(rsp))) 2186 shost_printk(KERN_ERR, target->scsi_host, PFX 2187 "problems processing SRP_AER_REQ\n"); 2188 } 2189 2190 static void srp_recv_done(struct ib_cq *cq, struct ib_wc *wc) 2191 { 2192 struct srp_iu *iu = container_of(wc->wr_cqe, struct srp_iu, cqe); 2193 struct srp_rdma_ch *ch = cq->cq_context; 2194 struct srp_target_port *target = ch->target; 2195 struct ib_device *dev = target->srp_host->srp_dev->dev; 2196 int res; 2197 u8 opcode; 2198 2199 if (unlikely(wc->status != IB_WC_SUCCESS)) { 2200 srp_handle_qp_err(cq, wc, "RECV"); 2201 return; 2202 } 2203 2204 ib_dma_sync_single_for_cpu(dev, iu->dma, ch->max_ti_iu_len, 2205 DMA_FROM_DEVICE); 2206 2207 opcode = *(u8 *) iu->buf; 2208 2209 if (0) { 2210 shost_printk(KERN_ERR, target->scsi_host, 2211 PFX "recv completion, opcode 0x%02x\n", opcode); 2212 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1, 2213 iu->buf, wc->byte_len, true); 2214 } 2215 2216 switch (opcode) { 2217 case SRP_RSP: 2218 srp_process_rsp(ch, iu->buf); 2219 break; 2220 2221 case SRP_CRED_REQ: 2222 srp_process_cred_req(ch, iu->buf); 2223 break; 2224 2225 case SRP_AER_REQ: 2226 srp_process_aer_req(ch, iu->buf); 2227 break; 2228 2229 case SRP_T_LOGOUT: 2230 /* XXX Handle target logout */ 2231 shost_printk(KERN_WARNING, target->scsi_host, 2232 PFX "Got target logout request\n"); 2233 break; 2234 2235 default: 2236 shost_printk(KERN_WARNING, target->scsi_host, 2237 PFX "Unhandled SRP opcode 0x%02x\n", opcode); 2238 break; 2239 } 2240 2241 ib_dma_sync_single_for_device(dev, iu->dma, ch->max_ti_iu_len, 2242 DMA_FROM_DEVICE); 2243 2244 res = srp_post_recv(ch, iu); 2245 if (res != 0) 2246 shost_printk(KERN_ERR, target->scsi_host, 2247 PFX "Recv failed with error code %d\n", res); 2248 } 2249 2250 /** 2251 * srp_tl_err_work() - handle a transport layer error 2252 * @work: Work structure embedded in an SRP target port. 2253 * 2254 * Note: This function may get invoked before the rport has been created, 2255 * hence the target->rport test. 2256 */ 2257 static void srp_tl_err_work(struct work_struct *work) 2258 { 2259 struct srp_target_port *target; 2260 2261 target = container_of(work, struct srp_target_port, tl_err_work); 2262 if (target->rport) 2263 srp_start_tl_fail_timers(target->rport); 2264 } 2265 2266 static void srp_handle_qp_err(struct ib_cq *cq, struct ib_wc *wc, 2267 const char *opname) 2268 { 2269 struct srp_rdma_ch *ch = cq->cq_context; 2270 struct srp_target_port *target = ch->target; 2271 2272 if (ch->connected && !target->qp_in_error) { 2273 shost_printk(KERN_ERR, target->scsi_host, 2274 PFX "failed %s status %s (%d) for CQE %p\n", 2275 opname, ib_wc_status_msg(wc->status), wc->status, 2276 wc->wr_cqe); 2277 queue_work(system_long_wq, &target->tl_err_work); 2278 } 2279 target->qp_in_error = true; 2280 } 2281 2282 static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd) 2283 { 2284 struct srp_target_port *target = host_to_target(shost); 2285 struct srp_rport *rport = target->rport; 2286 struct srp_rdma_ch *ch; 2287 struct srp_request *req; 2288 struct srp_iu *iu; 2289 struct srp_cmd *cmd; 2290 struct ib_device *dev; 2291 unsigned long flags; 2292 u32 tag; 2293 u16 idx; 2294 int len, ret; 2295 const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler; 2296 2297 /* 2298 * The SCSI EH thread is the only context from which srp_queuecommand() 2299 * can get invoked for blocked devices (SDEV_BLOCK / 2300 * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by 2301 * locking the rport mutex if invoked from inside the SCSI EH. 2302 */ 2303 if (in_scsi_eh) 2304 mutex_lock(&rport->mutex); 2305 2306 scmnd->result = srp_chkready(target->rport); 2307 if (unlikely(scmnd->result)) 2308 goto err; 2309 2310 WARN_ON_ONCE(scmnd->request->tag < 0); 2311 tag = blk_mq_unique_tag(scmnd->request); 2312 ch = &target->ch[blk_mq_unique_tag_to_hwq(tag)]; 2313 idx = blk_mq_unique_tag_to_tag(tag); 2314 WARN_ONCE(idx >= target->req_ring_size, "%s: tag %#x: idx %d >= %d\n", 2315 dev_name(&shost->shost_gendev), tag, idx, 2316 target->req_ring_size); 2317 2318 spin_lock_irqsave(&ch->lock, flags); 2319 iu = __srp_get_tx_iu(ch, SRP_IU_CMD); 2320 spin_unlock_irqrestore(&ch->lock, flags); 2321 2322 if (!iu) 2323 goto err; 2324 2325 req = &ch->req_ring[idx]; 2326 dev = target->srp_host->srp_dev->dev; 2327 ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len, 2328 DMA_TO_DEVICE); 2329 2330 scmnd->host_scribble = (void *) req; 2331 2332 cmd = iu->buf; 2333 memset(cmd, 0, sizeof *cmd); 2334 2335 cmd->opcode = SRP_CMD; 2336 int_to_scsilun(scmnd->device->lun, &cmd->lun); 2337 cmd->tag = tag; 2338 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len); 2339 2340 req->scmnd = scmnd; 2341 req->cmd = iu; 2342 2343 len = srp_map_data(scmnd, ch, req); 2344 if (len < 0) { 2345 shost_printk(KERN_ERR, target->scsi_host, 2346 PFX "Failed to map data (%d)\n", len); 2347 /* 2348 * If we ran out of memory descriptors (-ENOMEM) because an 2349 * application is queuing many requests with more than 2350 * max_pages_per_mr sg-list elements, tell the SCSI mid-layer 2351 * to reduce queue depth temporarily. 2352 */ 2353 scmnd->result = len == -ENOMEM ? 2354 DID_OK << 16 | QUEUE_FULL << 1 : DID_ERROR << 16; 2355 goto err_iu; 2356 } 2357 2358 ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len, 2359 DMA_TO_DEVICE); 2360 2361 if (srp_post_send(ch, iu, len)) { 2362 shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n"); 2363 goto err_unmap; 2364 } 2365 2366 ret = 0; 2367 2368 unlock_rport: 2369 if (in_scsi_eh) 2370 mutex_unlock(&rport->mutex); 2371 2372 return ret; 2373 2374 err_unmap: 2375 srp_unmap_data(scmnd, ch, req); 2376 2377 err_iu: 2378 srp_put_tx_iu(ch, iu, SRP_IU_CMD); 2379 2380 /* 2381 * Avoid that the loops that iterate over the request ring can 2382 * encounter a dangling SCSI command pointer. 2383 */ 2384 req->scmnd = NULL; 2385 2386 err: 2387 if (scmnd->result) { 2388 scmnd->scsi_done(scmnd); 2389 ret = 0; 2390 } else { 2391 ret = SCSI_MLQUEUE_HOST_BUSY; 2392 } 2393 2394 goto unlock_rport; 2395 } 2396 2397 /* 2398 * Note: the resources allocated in this function are freed in 2399 * srp_free_ch_ib(). 2400 */ 2401 static int srp_alloc_iu_bufs(struct srp_rdma_ch *ch) 2402 { 2403 struct srp_target_port *target = ch->target; 2404 int i; 2405 2406 ch->rx_ring = kcalloc(target->queue_size, sizeof(*ch->rx_ring), 2407 GFP_KERNEL); 2408 if (!ch->rx_ring) 2409 goto err_no_ring; 2410 ch->tx_ring = kcalloc(target->queue_size, sizeof(*ch->tx_ring), 2411 GFP_KERNEL); 2412 if (!ch->tx_ring) 2413 goto err_no_ring; 2414 2415 for (i = 0; i < target->queue_size; ++i) { 2416 ch->rx_ring[i] = srp_alloc_iu(target->srp_host, 2417 ch->max_ti_iu_len, 2418 GFP_KERNEL, DMA_FROM_DEVICE); 2419 if (!ch->rx_ring[i]) 2420 goto err; 2421 } 2422 2423 for (i = 0; i < target->queue_size; ++i) { 2424 ch->tx_ring[i] = srp_alloc_iu(target->srp_host, 2425 target->max_iu_len, 2426 GFP_KERNEL, DMA_TO_DEVICE); 2427 if (!ch->tx_ring[i]) 2428 goto err; 2429 2430 list_add(&ch->tx_ring[i]->list, &ch->free_tx); 2431 } 2432 2433 return 0; 2434 2435 err: 2436 for (i = 0; i < target->queue_size; ++i) { 2437 srp_free_iu(target->srp_host, ch->rx_ring[i]); 2438 srp_free_iu(target->srp_host, ch->tx_ring[i]); 2439 } 2440 2441 2442 err_no_ring: 2443 kfree(ch->tx_ring); 2444 ch->tx_ring = NULL; 2445 kfree(ch->rx_ring); 2446 ch->rx_ring = NULL; 2447 2448 return -ENOMEM; 2449 } 2450 2451 static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask) 2452 { 2453 uint64_t T_tr_ns, max_compl_time_ms; 2454 uint32_t rq_tmo_jiffies; 2455 2456 /* 2457 * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair, 2458 * table 91), both the QP timeout and the retry count have to be set 2459 * for RC QP's during the RTR to RTS transition. 2460 */ 2461 WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) != 2462 (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)); 2463 2464 /* 2465 * Set target->rq_tmo_jiffies to one second more than the largest time 2466 * it can take before an error completion is generated. See also 2467 * C9-140..142 in the IBTA spec for more information about how to 2468 * convert the QP Local ACK Timeout value to nanoseconds. 2469 */ 2470 T_tr_ns = 4096 * (1ULL << qp_attr->timeout); 2471 max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns; 2472 do_div(max_compl_time_ms, NSEC_PER_MSEC); 2473 rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000); 2474 2475 return rq_tmo_jiffies; 2476 } 2477 2478 static void srp_cm_rep_handler(struct ib_cm_id *cm_id, 2479 const struct srp_login_rsp *lrsp, 2480 struct srp_rdma_ch *ch) 2481 { 2482 struct srp_target_port *target = ch->target; 2483 struct ib_qp_attr *qp_attr = NULL; 2484 int attr_mask = 0; 2485 int ret = 0; 2486 int i; 2487 2488 if (lrsp->opcode == SRP_LOGIN_RSP) { 2489 ch->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len); 2490 ch->req_lim = be32_to_cpu(lrsp->req_lim_delta); 2491 2492 /* 2493 * Reserve credits for task management so we don't 2494 * bounce requests back to the SCSI mid-layer. 2495 */ 2496 target->scsi_host->can_queue 2497 = min(ch->req_lim - SRP_TSK_MGMT_SQ_SIZE, 2498 target->scsi_host->can_queue); 2499 target->scsi_host->cmd_per_lun 2500 = min_t(int, target->scsi_host->can_queue, 2501 target->scsi_host->cmd_per_lun); 2502 } else { 2503 shost_printk(KERN_WARNING, target->scsi_host, 2504 PFX "Unhandled RSP opcode %#x\n", lrsp->opcode); 2505 ret = -ECONNRESET; 2506 goto error; 2507 } 2508 2509 if (!ch->rx_ring) { 2510 ret = srp_alloc_iu_bufs(ch); 2511 if (ret) 2512 goto error; 2513 } 2514 2515 for (i = 0; i < target->queue_size; i++) { 2516 struct srp_iu *iu = ch->rx_ring[i]; 2517 2518 ret = srp_post_recv(ch, iu); 2519 if (ret) 2520 goto error; 2521 } 2522 2523 if (!target->using_rdma_cm) { 2524 ret = -ENOMEM; 2525 qp_attr = kmalloc(sizeof(*qp_attr), GFP_KERNEL); 2526 if (!qp_attr) 2527 goto error; 2528 2529 qp_attr->qp_state = IB_QPS_RTR; 2530 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); 2531 if (ret) 2532 goto error_free; 2533 2534 ret = ib_modify_qp(ch->qp, qp_attr, attr_mask); 2535 if (ret) 2536 goto error_free; 2537 2538 qp_attr->qp_state = IB_QPS_RTS; 2539 ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); 2540 if (ret) 2541 goto error_free; 2542 2543 target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask); 2544 2545 ret = ib_modify_qp(ch->qp, qp_attr, attr_mask); 2546 if (ret) 2547 goto error_free; 2548 2549 ret = ib_send_cm_rtu(cm_id, NULL, 0); 2550 } 2551 2552 error_free: 2553 kfree(qp_attr); 2554 2555 error: 2556 ch->status = ret; 2557 } 2558 2559 static void srp_ib_cm_rej_handler(struct ib_cm_id *cm_id, 2560 struct ib_cm_event *event, 2561 struct srp_rdma_ch *ch) 2562 { 2563 struct srp_target_port *target = ch->target; 2564 struct Scsi_Host *shost = target->scsi_host; 2565 struct ib_class_port_info *cpi; 2566 int opcode; 2567 u16 dlid; 2568 2569 switch (event->param.rej_rcvd.reason) { 2570 case IB_CM_REJ_PORT_CM_REDIRECT: 2571 cpi = event->param.rej_rcvd.ari; 2572 dlid = be16_to_cpu(cpi->redirect_lid); 2573 sa_path_set_dlid(&ch->ib_cm.path, dlid); 2574 ch->ib_cm.path.pkey = cpi->redirect_pkey; 2575 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff; 2576 memcpy(ch->ib_cm.path.dgid.raw, cpi->redirect_gid, 16); 2577 2578 ch->status = dlid ? SRP_DLID_REDIRECT : SRP_PORT_REDIRECT; 2579 break; 2580 2581 case IB_CM_REJ_PORT_REDIRECT: 2582 if (srp_target_is_topspin(target)) { 2583 union ib_gid *dgid = &ch->ib_cm.path.dgid; 2584 2585 /* 2586 * Topspin/Cisco SRP gateways incorrectly send 2587 * reject reason code 25 when they mean 24 2588 * (port redirect). 2589 */ 2590 memcpy(dgid->raw, event->param.rej_rcvd.ari, 16); 2591 2592 shost_printk(KERN_DEBUG, shost, 2593 PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n", 2594 be64_to_cpu(dgid->global.subnet_prefix), 2595 be64_to_cpu(dgid->global.interface_id)); 2596 2597 ch->status = SRP_PORT_REDIRECT; 2598 } else { 2599 shost_printk(KERN_WARNING, shost, 2600 " REJ reason: IB_CM_REJ_PORT_REDIRECT\n"); 2601 ch->status = -ECONNRESET; 2602 } 2603 break; 2604 2605 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID: 2606 shost_printk(KERN_WARNING, shost, 2607 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n"); 2608 ch->status = -ECONNRESET; 2609 break; 2610 2611 case IB_CM_REJ_CONSUMER_DEFINED: 2612 opcode = *(u8 *) event->private_data; 2613 if (opcode == SRP_LOGIN_REJ) { 2614 struct srp_login_rej *rej = event->private_data; 2615 u32 reason = be32_to_cpu(rej->reason); 2616 2617 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE) 2618 shost_printk(KERN_WARNING, shost, 2619 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n"); 2620 else 2621 shost_printk(KERN_WARNING, shost, PFX 2622 "SRP LOGIN from %pI6 to %pI6 REJECTED, reason 0x%08x\n", 2623 target->sgid.raw, 2624 target->ib_cm.orig_dgid.raw, 2625 reason); 2626 } else 2627 shost_printk(KERN_WARNING, shost, 2628 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED," 2629 " opcode 0x%02x\n", opcode); 2630 ch->status = -ECONNRESET; 2631 break; 2632 2633 case IB_CM_REJ_STALE_CONN: 2634 shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n"); 2635 ch->status = SRP_STALE_CONN; 2636 break; 2637 2638 default: 2639 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n", 2640 event->param.rej_rcvd.reason); 2641 ch->status = -ECONNRESET; 2642 } 2643 } 2644 2645 static int srp_ib_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) 2646 { 2647 struct srp_rdma_ch *ch = cm_id->context; 2648 struct srp_target_port *target = ch->target; 2649 int comp = 0; 2650 2651 switch (event->event) { 2652 case IB_CM_REQ_ERROR: 2653 shost_printk(KERN_DEBUG, target->scsi_host, 2654 PFX "Sending CM REQ failed\n"); 2655 comp = 1; 2656 ch->status = -ECONNRESET; 2657 break; 2658 2659 case IB_CM_REP_RECEIVED: 2660 comp = 1; 2661 srp_cm_rep_handler(cm_id, event->private_data, ch); 2662 break; 2663 2664 case IB_CM_REJ_RECEIVED: 2665 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n"); 2666 comp = 1; 2667 2668 srp_ib_cm_rej_handler(cm_id, event, ch); 2669 break; 2670 2671 case IB_CM_DREQ_RECEIVED: 2672 shost_printk(KERN_WARNING, target->scsi_host, 2673 PFX "DREQ received - connection closed\n"); 2674 ch->connected = false; 2675 if (ib_send_cm_drep(cm_id, NULL, 0)) 2676 shost_printk(KERN_ERR, target->scsi_host, 2677 PFX "Sending CM DREP failed\n"); 2678 queue_work(system_long_wq, &target->tl_err_work); 2679 break; 2680 2681 case IB_CM_TIMEWAIT_EXIT: 2682 shost_printk(KERN_ERR, target->scsi_host, 2683 PFX "connection closed\n"); 2684 comp = 1; 2685 2686 ch->status = 0; 2687 break; 2688 2689 case IB_CM_MRA_RECEIVED: 2690 case IB_CM_DREQ_ERROR: 2691 case IB_CM_DREP_RECEIVED: 2692 break; 2693 2694 default: 2695 shost_printk(KERN_WARNING, target->scsi_host, 2696 PFX "Unhandled CM event %d\n", event->event); 2697 break; 2698 } 2699 2700 if (comp) 2701 complete(&ch->done); 2702 2703 return 0; 2704 } 2705 2706 static void srp_rdma_cm_rej_handler(struct srp_rdma_ch *ch, 2707 struct rdma_cm_event *event) 2708 { 2709 struct srp_target_port *target = ch->target; 2710 struct Scsi_Host *shost = target->scsi_host; 2711 int opcode; 2712 2713 switch (event->status) { 2714 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID: 2715 shost_printk(KERN_WARNING, shost, 2716 " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n"); 2717 ch->status = -ECONNRESET; 2718 break; 2719 2720 case IB_CM_REJ_CONSUMER_DEFINED: 2721 opcode = *(u8 *) event->param.conn.private_data; 2722 if (opcode == SRP_LOGIN_REJ) { 2723 struct srp_login_rej *rej = 2724 (struct srp_login_rej *) 2725 event->param.conn.private_data; 2726 u32 reason = be32_to_cpu(rej->reason); 2727 2728 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE) 2729 shost_printk(KERN_WARNING, shost, 2730 PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n"); 2731 else 2732 shost_printk(KERN_WARNING, shost, 2733 PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason); 2734 } else { 2735 shost_printk(KERN_WARNING, shost, 2736 " REJ reason: IB_CM_REJ_CONSUMER_DEFINED, opcode 0x%02x\n", 2737 opcode); 2738 } 2739 ch->status = -ECONNRESET; 2740 break; 2741 2742 case IB_CM_REJ_STALE_CONN: 2743 shost_printk(KERN_WARNING, shost, 2744 " REJ reason: stale connection\n"); 2745 ch->status = SRP_STALE_CONN; 2746 break; 2747 2748 default: 2749 shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n", 2750 event->status); 2751 ch->status = -ECONNRESET; 2752 break; 2753 } 2754 } 2755 2756 static int srp_rdma_cm_handler(struct rdma_cm_id *cm_id, 2757 struct rdma_cm_event *event) 2758 { 2759 struct srp_rdma_ch *ch = cm_id->context; 2760 struct srp_target_port *target = ch->target; 2761 int comp = 0; 2762 2763 switch (event->event) { 2764 case RDMA_CM_EVENT_ADDR_RESOLVED: 2765 ch->status = 0; 2766 comp = 1; 2767 break; 2768 2769 case RDMA_CM_EVENT_ADDR_ERROR: 2770 ch->status = -ENXIO; 2771 comp = 1; 2772 break; 2773 2774 case RDMA_CM_EVENT_ROUTE_RESOLVED: 2775 ch->status = 0; 2776 comp = 1; 2777 break; 2778 2779 case RDMA_CM_EVENT_ROUTE_ERROR: 2780 case RDMA_CM_EVENT_UNREACHABLE: 2781 ch->status = -EHOSTUNREACH; 2782 comp = 1; 2783 break; 2784 2785 case RDMA_CM_EVENT_CONNECT_ERROR: 2786 shost_printk(KERN_DEBUG, target->scsi_host, 2787 PFX "Sending CM REQ failed\n"); 2788 comp = 1; 2789 ch->status = -ECONNRESET; 2790 break; 2791 2792 case RDMA_CM_EVENT_ESTABLISHED: 2793 comp = 1; 2794 srp_cm_rep_handler(NULL, event->param.conn.private_data, ch); 2795 break; 2796 2797 case RDMA_CM_EVENT_REJECTED: 2798 shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n"); 2799 comp = 1; 2800 2801 srp_rdma_cm_rej_handler(ch, event); 2802 break; 2803 2804 case RDMA_CM_EVENT_DISCONNECTED: 2805 if (ch->connected) { 2806 shost_printk(KERN_WARNING, target->scsi_host, 2807 PFX "received DREQ\n"); 2808 rdma_disconnect(ch->rdma_cm.cm_id); 2809 comp = 1; 2810 ch->status = 0; 2811 queue_work(system_long_wq, &target->tl_err_work); 2812 } 2813 break; 2814 2815 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 2816 shost_printk(KERN_ERR, target->scsi_host, 2817 PFX "connection closed\n"); 2818 2819 comp = 1; 2820 ch->status = 0; 2821 break; 2822 2823 default: 2824 shost_printk(KERN_WARNING, target->scsi_host, 2825 PFX "Unhandled CM event %d\n", event->event); 2826 break; 2827 } 2828 2829 if (comp) 2830 complete(&ch->done); 2831 2832 return 0; 2833 } 2834 2835 /** 2836 * srp_change_queue_depth - setting device queue depth 2837 * @sdev: scsi device struct 2838 * @qdepth: requested queue depth 2839 * 2840 * Returns queue depth. 2841 */ 2842 static int 2843 srp_change_queue_depth(struct scsi_device *sdev, int qdepth) 2844 { 2845 if (!sdev->tagged_supported) 2846 qdepth = 1; 2847 return scsi_change_queue_depth(sdev, qdepth); 2848 } 2849 2850 static int srp_send_tsk_mgmt(struct srp_rdma_ch *ch, u64 req_tag, u64 lun, 2851 u8 func, u8 *status) 2852 { 2853 struct srp_target_port *target = ch->target; 2854 struct srp_rport *rport = target->rport; 2855 struct ib_device *dev = target->srp_host->srp_dev->dev; 2856 struct srp_iu *iu; 2857 struct srp_tsk_mgmt *tsk_mgmt; 2858 int res; 2859 2860 if (!ch->connected || target->qp_in_error) 2861 return -1; 2862 2863 /* 2864 * Lock the rport mutex to avoid that srp_create_ch_ib() is 2865 * invoked while a task management function is being sent. 2866 */ 2867 mutex_lock(&rport->mutex); 2868 spin_lock_irq(&ch->lock); 2869 iu = __srp_get_tx_iu(ch, SRP_IU_TSK_MGMT); 2870 spin_unlock_irq(&ch->lock); 2871 2872 if (!iu) { 2873 mutex_unlock(&rport->mutex); 2874 2875 return -1; 2876 } 2877 2878 ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt, 2879 DMA_TO_DEVICE); 2880 tsk_mgmt = iu->buf; 2881 memset(tsk_mgmt, 0, sizeof *tsk_mgmt); 2882 2883 tsk_mgmt->opcode = SRP_TSK_MGMT; 2884 int_to_scsilun(lun, &tsk_mgmt->lun); 2885 tsk_mgmt->tsk_mgmt_func = func; 2886 tsk_mgmt->task_tag = req_tag; 2887 2888 spin_lock_irq(&ch->lock); 2889 ch->tsk_mgmt_tag = (ch->tsk_mgmt_tag + 1) | SRP_TAG_TSK_MGMT; 2890 tsk_mgmt->tag = ch->tsk_mgmt_tag; 2891 spin_unlock_irq(&ch->lock); 2892 2893 init_completion(&ch->tsk_mgmt_done); 2894 2895 ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt, 2896 DMA_TO_DEVICE); 2897 if (srp_post_send(ch, iu, sizeof(*tsk_mgmt))) { 2898 srp_put_tx_iu(ch, iu, SRP_IU_TSK_MGMT); 2899 mutex_unlock(&rport->mutex); 2900 2901 return -1; 2902 } 2903 res = wait_for_completion_timeout(&ch->tsk_mgmt_done, 2904 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)); 2905 if (res > 0 && status) 2906 *status = ch->tsk_mgmt_status; 2907 mutex_unlock(&rport->mutex); 2908 2909 WARN_ON_ONCE(res < 0); 2910 2911 return res > 0 ? 0 : -1; 2912 } 2913 2914 static int srp_abort(struct scsi_cmnd *scmnd) 2915 { 2916 struct srp_target_port *target = host_to_target(scmnd->device->host); 2917 struct srp_request *req = (struct srp_request *) scmnd->host_scribble; 2918 u32 tag; 2919 u16 ch_idx; 2920 struct srp_rdma_ch *ch; 2921 int ret; 2922 2923 shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n"); 2924 2925 if (!req) 2926 return SUCCESS; 2927 tag = blk_mq_unique_tag(scmnd->request); 2928 ch_idx = blk_mq_unique_tag_to_hwq(tag); 2929 if (WARN_ON_ONCE(ch_idx >= target->ch_count)) 2930 return SUCCESS; 2931 ch = &target->ch[ch_idx]; 2932 if (!srp_claim_req(ch, req, NULL, scmnd)) 2933 return SUCCESS; 2934 shost_printk(KERN_ERR, target->scsi_host, 2935 "Sending SRP abort for tag %#x\n", tag); 2936 if (srp_send_tsk_mgmt(ch, tag, scmnd->device->lun, 2937 SRP_TSK_ABORT_TASK, NULL) == 0) 2938 ret = SUCCESS; 2939 else if (target->rport->state == SRP_RPORT_LOST) 2940 ret = FAST_IO_FAIL; 2941 else 2942 ret = FAILED; 2943 if (ret == SUCCESS) { 2944 srp_free_req(ch, req, scmnd, 0); 2945 scmnd->result = DID_ABORT << 16; 2946 scmnd->scsi_done(scmnd); 2947 } 2948 2949 return ret; 2950 } 2951 2952 static int srp_reset_device(struct scsi_cmnd *scmnd) 2953 { 2954 struct srp_target_port *target = host_to_target(scmnd->device->host); 2955 struct srp_rdma_ch *ch; 2956 int i; 2957 u8 status; 2958 2959 shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n"); 2960 2961 ch = &target->ch[0]; 2962 if (srp_send_tsk_mgmt(ch, SRP_TAG_NO_REQ, scmnd->device->lun, 2963 SRP_TSK_LUN_RESET, &status)) 2964 return FAILED; 2965 if (status) 2966 return FAILED; 2967 2968 for (i = 0; i < target->ch_count; i++) { 2969 ch = &target->ch[i]; 2970 for (i = 0; i < target->req_ring_size; ++i) { 2971 struct srp_request *req = &ch->req_ring[i]; 2972 2973 srp_finish_req(ch, req, scmnd->device, DID_RESET << 16); 2974 } 2975 } 2976 2977 return SUCCESS; 2978 } 2979 2980 static int srp_reset_host(struct scsi_cmnd *scmnd) 2981 { 2982 struct srp_target_port *target = host_to_target(scmnd->device->host); 2983 2984 shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n"); 2985 2986 return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED; 2987 } 2988 2989 static int srp_target_alloc(struct scsi_target *starget) 2990 { 2991 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 2992 struct srp_target_port *target = host_to_target(shost); 2993 2994 if (target->target_can_queue) 2995 starget->can_queue = target->target_can_queue; 2996 return 0; 2997 } 2998 2999 static int srp_slave_alloc(struct scsi_device *sdev) 3000 { 3001 struct Scsi_Host *shost = sdev->host; 3002 struct srp_target_port *target = host_to_target(shost); 3003 struct srp_device *srp_dev = target->srp_host->srp_dev; 3004 struct ib_device *ibdev = srp_dev->dev; 3005 3006 if (!(ibdev->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)) 3007 blk_queue_virt_boundary(sdev->request_queue, 3008 ~srp_dev->mr_page_mask); 3009 3010 return 0; 3011 } 3012 3013 static int srp_slave_configure(struct scsi_device *sdev) 3014 { 3015 struct Scsi_Host *shost = sdev->host; 3016 struct srp_target_port *target = host_to_target(shost); 3017 struct request_queue *q = sdev->request_queue; 3018 unsigned long timeout; 3019 3020 if (sdev->type == TYPE_DISK) { 3021 timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies); 3022 blk_queue_rq_timeout(q, timeout); 3023 } 3024 3025 return 0; 3026 } 3027 3028 static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr, 3029 char *buf) 3030 { 3031 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3032 3033 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->id_ext)); 3034 } 3035 3036 static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr, 3037 char *buf) 3038 { 3039 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3040 3041 return sprintf(buf, "0x%016llx\n", be64_to_cpu(target->ioc_guid)); 3042 } 3043 3044 static ssize_t show_service_id(struct device *dev, 3045 struct device_attribute *attr, char *buf) 3046 { 3047 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3048 3049 if (target->using_rdma_cm) 3050 return -ENOENT; 3051 return sprintf(buf, "0x%016llx\n", 3052 be64_to_cpu(target->ib_cm.service_id)); 3053 } 3054 3055 static ssize_t show_pkey(struct device *dev, struct device_attribute *attr, 3056 char *buf) 3057 { 3058 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3059 3060 if (target->using_rdma_cm) 3061 return -ENOENT; 3062 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->ib_cm.pkey)); 3063 } 3064 3065 static ssize_t show_sgid(struct device *dev, struct device_attribute *attr, 3066 char *buf) 3067 { 3068 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3069 3070 return sprintf(buf, "%pI6\n", target->sgid.raw); 3071 } 3072 3073 static ssize_t show_dgid(struct device *dev, struct device_attribute *attr, 3074 char *buf) 3075 { 3076 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3077 struct srp_rdma_ch *ch = &target->ch[0]; 3078 3079 if (target->using_rdma_cm) 3080 return -ENOENT; 3081 return sprintf(buf, "%pI6\n", ch->ib_cm.path.dgid.raw); 3082 } 3083 3084 static ssize_t show_orig_dgid(struct device *dev, 3085 struct device_attribute *attr, char *buf) 3086 { 3087 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3088 3089 if (target->using_rdma_cm) 3090 return -ENOENT; 3091 return sprintf(buf, "%pI6\n", target->ib_cm.orig_dgid.raw); 3092 } 3093 3094 static ssize_t show_req_lim(struct device *dev, 3095 struct device_attribute *attr, char *buf) 3096 { 3097 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3098 struct srp_rdma_ch *ch; 3099 int i, req_lim = INT_MAX; 3100 3101 for (i = 0; i < target->ch_count; i++) { 3102 ch = &target->ch[i]; 3103 req_lim = min(req_lim, ch->req_lim); 3104 } 3105 return sprintf(buf, "%d\n", req_lim); 3106 } 3107 3108 static ssize_t show_zero_req_lim(struct device *dev, 3109 struct device_attribute *attr, char *buf) 3110 { 3111 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3112 3113 return sprintf(buf, "%d\n", target->zero_req_lim); 3114 } 3115 3116 static ssize_t show_local_ib_port(struct device *dev, 3117 struct device_attribute *attr, char *buf) 3118 { 3119 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3120 3121 return sprintf(buf, "%d\n", target->srp_host->port); 3122 } 3123 3124 static ssize_t show_local_ib_device(struct device *dev, 3125 struct device_attribute *attr, char *buf) 3126 { 3127 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3128 3129 return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name); 3130 } 3131 3132 static ssize_t show_ch_count(struct device *dev, struct device_attribute *attr, 3133 char *buf) 3134 { 3135 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3136 3137 return sprintf(buf, "%d\n", target->ch_count); 3138 } 3139 3140 static ssize_t show_comp_vector(struct device *dev, 3141 struct device_attribute *attr, char *buf) 3142 { 3143 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3144 3145 return sprintf(buf, "%d\n", target->comp_vector); 3146 } 3147 3148 static ssize_t show_tl_retry_count(struct device *dev, 3149 struct device_attribute *attr, char *buf) 3150 { 3151 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3152 3153 return sprintf(buf, "%d\n", target->tl_retry_count); 3154 } 3155 3156 static ssize_t show_cmd_sg_entries(struct device *dev, 3157 struct device_attribute *attr, char *buf) 3158 { 3159 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3160 3161 return sprintf(buf, "%u\n", target->cmd_sg_cnt); 3162 } 3163 3164 static ssize_t show_allow_ext_sg(struct device *dev, 3165 struct device_attribute *attr, char *buf) 3166 { 3167 struct srp_target_port *target = host_to_target(class_to_shost(dev)); 3168 3169 return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false"); 3170 } 3171 3172 static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL); 3173 static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL); 3174 static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL); 3175 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL); 3176 static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL); 3177 static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL); 3178 static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL); 3179 static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL); 3180 static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL); 3181 static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL); 3182 static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL); 3183 static DEVICE_ATTR(ch_count, S_IRUGO, show_ch_count, NULL); 3184 static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL); 3185 static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL); 3186 static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL); 3187 static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL); 3188 3189 static struct device_attribute *srp_host_attrs[] = { 3190 &dev_attr_id_ext, 3191 &dev_attr_ioc_guid, 3192 &dev_attr_service_id, 3193 &dev_attr_pkey, 3194 &dev_attr_sgid, 3195 &dev_attr_dgid, 3196 &dev_attr_orig_dgid, 3197 &dev_attr_req_lim, 3198 &dev_attr_zero_req_lim, 3199 &dev_attr_local_ib_port, 3200 &dev_attr_local_ib_device, 3201 &dev_attr_ch_count, 3202 &dev_attr_comp_vector, 3203 &dev_attr_tl_retry_count, 3204 &dev_attr_cmd_sg_entries, 3205 &dev_attr_allow_ext_sg, 3206 NULL 3207 }; 3208 3209 static struct scsi_host_template srp_template = { 3210 .module = THIS_MODULE, 3211 .name = "InfiniBand SRP initiator", 3212 .proc_name = DRV_NAME, 3213 .target_alloc = srp_target_alloc, 3214 .slave_alloc = srp_slave_alloc, 3215 .slave_configure = srp_slave_configure, 3216 .info = srp_target_info, 3217 .queuecommand = srp_queuecommand, 3218 .change_queue_depth = srp_change_queue_depth, 3219 .eh_timed_out = srp_timed_out, 3220 .eh_abort_handler = srp_abort, 3221 .eh_device_reset_handler = srp_reset_device, 3222 .eh_host_reset_handler = srp_reset_host, 3223 .skip_settle_delay = true, 3224 .sg_tablesize = SRP_DEF_SG_TABLESIZE, 3225 .can_queue = SRP_DEFAULT_CMD_SQ_SIZE, 3226 .this_id = -1, 3227 .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE, 3228 .use_clustering = ENABLE_CLUSTERING, 3229 .shost_attrs = srp_host_attrs, 3230 .track_queue_depth = 1, 3231 }; 3232 3233 static int srp_sdev_count(struct Scsi_Host *host) 3234 { 3235 struct scsi_device *sdev; 3236 int c = 0; 3237 3238 shost_for_each_device(sdev, host) 3239 c++; 3240 3241 return c; 3242 } 3243 3244 /* 3245 * Return values: 3246 * < 0 upon failure. Caller is responsible for SRP target port cleanup. 3247 * 0 and target->state == SRP_TARGET_REMOVED if asynchronous target port 3248 * removal has been scheduled. 3249 * 0 and target->state != SRP_TARGET_REMOVED upon success. 3250 */ 3251 static int srp_add_target(struct srp_host *host, struct srp_target_port *target) 3252 { 3253 struct srp_rport_identifiers ids; 3254 struct srp_rport *rport; 3255 3256 target->state = SRP_TARGET_SCANNING; 3257 sprintf(target->target_name, "SRP.T10:%016llX", 3258 be64_to_cpu(target->id_ext)); 3259 3260 if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dev.parent)) 3261 return -ENODEV; 3262 3263 memcpy(ids.port_id, &target->id_ext, 8); 3264 memcpy(ids.port_id + 8, &target->ioc_guid, 8); 3265 ids.roles = SRP_RPORT_ROLE_TARGET; 3266 rport = srp_rport_add(target->scsi_host, &ids); 3267 if (IS_ERR(rport)) { 3268 scsi_remove_host(target->scsi_host); 3269 return PTR_ERR(rport); 3270 } 3271 3272 rport->lld_data = target; 3273 target->rport = rport; 3274 3275 spin_lock(&host->target_lock); 3276 list_add_tail(&target->list, &host->target_list); 3277 spin_unlock(&host->target_lock); 3278 3279 scsi_scan_target(&target->scsi_host->shost_gendev, 3280 0, target->scsi_id, SCAN_WILD_CARD, SCSI_SCAN_INITIAL); 3281 3282 if (srp_connected_ch(target) < target->ch_count || 3283 target->qp_in_error) { 3284 shost_printk(KERN_INFO, target->scsi_host, 3285 PFX "SCSI scan failed - removing SCSI host\n"); 3286 srp_queue_remove_work(target); 3287 goto out; 3288 } 3289 3290 pr_debug("%s: SCSI scan succeeded - detected %d LUNs\n", 3291 dev_name(&target->scsi_host->shost_gendev), 3292 srp_sdev_count(target->scsi_host)); 3293 3294 spin_lock_irq(&target->lock); 3295 if (target->state == SRP_TARGET_SCANNING) 3296 target->state = SRP_TARGET_LIVE; 3297 spin_unlock_irq(&target->lock); 3298 3299 out: 3300 return 0; 3301 } 3302 3303 static void srp_release_dev(struct device *dev) 3304 { 3305 struct srp_host *host = 3306 container_of(dev, struct srp_host, dev); 3307 3308 complete(&host->released); 3309 } 3310 3311 static struct class srp_class = { 3312 .name = "infiniband_srp", 3313 .dev_release = srp_release_dev 3314 }; 3315 3316 /** 3317 * srp_conn_unique() - check whether the connection to a target is unique 3318 * @host: SRP host. 3319 * @target: SRP target port. 3320 */ 3321 static bool srp_conn_unique(struct srp_host *host, 3322 struct srp_target_port *target) 3323 { 3324 struct srp_target_port *t; 3325 bool ret = false; 3326 3327 if (target->state == SRP_TARGET_REMOVED) 3328 goto out; 3329 3330 ret = true; 3331 3332 spin_lock(&host->target_lock); 3333 list_for_each_entry(t, &host->target_list, list) { 3334 if (t != target && 3335 target->id_ext == t->id_ext && 3336 target->ioc_guid == t->ioc_guid && 3337 target->initiator_ext == t->initiator_ext) { 3338 ret = false; 3339 break; 3340 } 3341 } 3342 spin_unlock(&host->target_lock); 3343 3344 out: 3345 return ret; 3346 } 3347 3348 /* 3349 * Target ports are added by writing 3350 * 3351 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>, 3352 * pkey=<P_Key>,service_id=<service ID> 3353 * or 3354 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>, 3355 * [src=<IPv4 address>,]dest=<IPv4 address>:<port number> 3356 * 3357 * to the add_target sysfs attribute. 3358 */ 3359 enum { 3360 SRP_OPT_ERR = 0, 3361 SRP_OPT_ID_EXT = 1 << 0, 3362 SRP_OPT_IOC_GUID = 1 << 1, 3363 SRP_OPT_DGID = 1 << 2, 3364 SRP_OPT_PKEY = 1 << 3, 3365 SRP_OPT_SERVICE_ID = 1 << 4, 3366 SRP_OPT_MAX_SECT = 1 << 5, 3367 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6, 3368 SRP_OPT_IO_CLASS = 1 << 7, 3369 SRP_OPT_INITIATOR_EXT = 1 << 8, 3370 SRP_OPT_CMD_SG_ENTRIES = 1 << 9, 3371 SRP_OPT_ALLOW_EXT_SG = 1 << 10, 3372 SRP_OPT_SG_TABLESIZE = 1 << 11, 3373 SRP_OPT_COMP_VECTOR = 1 << 12, 3374 SRP_OPT_TL_RETRY_COUNT = 1 << 13, 3375 SRP_OPT_QUEUE_SIZE = 1 << 14, 3376 SRP_OPT_IP_SRC = 1 << 15, 3377 SRP_OPT_IP_DEST = 1 << 16, 3378 SRP_OPT_TARGET_CAN_QUEUE= 1 << 17, 3379 }; 3380 3381 static unsigned int srp_opt_mandatory[] = { 3382 SRP_OPT_ID_EXT | 3383 SRP_OPT_IOC_GUID | 3384 SRP_OPT_DGID | 3385 SRP_OPT_PKEY | 3386 SRP_OPT_SERVICE_ID, 3387 SRP_OPT_ID_EXT | 3388 SRP_OPT_IOC_GUID | 3389 SRP_OPT_IP_DEST, 3390 }; 3391 3392 static const match_table_t srp_opt_tokens = { 3393 { SRP_OPT_ID_EXT, "id_ext=%s" }, 3394 { SRP_OPT_IOC_GUID, "ioc_guid=%s" }, 3395 { SRP_OPT_DGID, "dgid=%s" }, 3396 { SRP_OPT_PKEY, "pkey=%x" }, 3397 { SRP_OPT_SERVICE_ID, "service_id=%s" }, 3398 { SRP_OPT_MAX_SECT, "max_sect=%d" }, 3399 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" }, 3400 { SRP_OPT_TARGET_CAN_QUEUE, "target_can_queue=%d" }, 3401 { SRP_OPT_IO_CLASS, "io_class=%x" }, 3402 { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" }, 3403 { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" }, 3404 { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" }, 3405 { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" }, 3406 { SRP_OPT_COMP_VECTOR, "comp_vector=%u" }, 3407 { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" }, 3408 { SRP_OPT_QUEUE_SIZE, "queue_size=%d" }, 3409 { SRP_OPT_IP_SRC, "src=%s" }, 3410 { SRP_OPT_IP_DEST, "dest=%s" }, 3411 { SRP_OPT_ERR, NULL } 3412 }; 3413 3414 /** 3415 * srp_parse_in - parse an IP address and port number combination 3416 * 3417 * Parse the following address formats: 3418 * - IPv4: <ip_address>:<port>, e.g. 1.2.3.4:5. 3419 * - IPv6: \[<ipv6_address>\]:<port>, e.g. [1::2:3%4]:5. 3420 */ 3421 static int srp_parse_in(struct net *net, struct sockaddr_storage *sa, 3422 const char *addr_port_str) 3423 { 3424 char *addr_end, *addr = kstrdup(addr_port_str, GFP_KERNEL); 3425 char *port_str; 3426 int ret; 3427 3428 if (!addr) 3429 return -ENOMEM; 3430 port_str = strrchr(addr, ':'); 3431 if (!port_str) 3432 return -EINVAL; 3433 *port_str++ = '\0'; 3434 ret = inet_pton_with_scope(net, AF_INET, addr, port_str, sa); 3435 if (ret && addr[0]) { 3436 addr_end = addr + strlen(addr) - 1; 3437 if (addr[0] == '[' && *addr_end == ']') { 3438 *addr_end = '\0'; 3439 ret = inet_pton_with_scope(net, AF_INET6, addr + 1, 3440 port_str, sa); 3441 } 3442 } 3443 kfree(addr); 3444 pr_debug("%s -> %pISpfsc\n", addr_port_str, sa); 3445 return ret; 3446 } 3447 3448 static int srp_parse_options(struct net *net, const char *buf, 3449 struct srp_target_port *target) 3450 { 3451 char *options, *sep_opt; 3452 char *p; 3453 substring_t args[MAX_OPT_ARGS]; 3454 unsigned long long ull; 3455 int opt_mask = 0; 3456 int token; 3457 int ret = -EINVAL; 3458 int i; 3459 3460 options = kstrdup(buf, GFP_KERNEL); 3461 if (!options) 3462 return -ENOMEM; 3463 3464 sep_opt = options; 3465 while ((p = strsep(&sep_opt, ",\n")) != NULL) { 3466 if (!*p) 3467 continue; 3468 3469 token = match_token(p, srp_opt_tokens, args); 3470 opt_mask |= token; 3471 3472 switch (token) { 3473 case SRP_OPT_ID_EXT: 3474 p = match_strdup(args); 3475 if (!p) { 3476 ret = -ENOMEM; 3477 goto out; 3478 } 3479 ret = kstrtoull(p, 16, &ull); 3480 if (ret) { 3481 pr_warn("invalid id_ext parameter '%s'\n", p); 3482 kfree(p); 3483 goto out; 3484 } 3485 target->id_ext = cpu_to_be64(ull); 3486 kfree(p); 3487 break; 3488 3489 case SRP_OPT_IOC_GUID: 3490 p = match_strdup(args); 3491 if (!p) { 3492 ret = -ENOMEM; 3493 goto out; 3494 } 3495 ret = kstrtoull(p, 16, &ull); 3496 if (ret) { 3497 pr_warn("invalid ioc_guid parameter '%s'\n", p); 3498 kfree(p); 3499 goto out; 3500 } 3501 target->ioc_guid = cpu_to_be64(ull); 3502 kfree(p); 3503 break; 3504 3505 case SRP_OPT_DGID: 3506 p = match_strdup(args); 3507 if (!p) { 3508 ret = -ENOMEM; 3509 goto out; 3510 } 3511 if (strlen(p) != 32) { 3512 pr_warn("bad dest GID parameter '%s'\n", p); 3513 kfree(p); 3514 goto out; 3515 } 3516 3517 ret = hex2bin(target->ib_cm.orig_dgid.raw, p, 16); 3518 kfree(p); 3519 if (ret < 0) 3520 goto out; 3521 break; 3522 3523 case SRP_OPT_PKEY: 3524 if (match_hex(args, &token)) { 3525 pr_warn("bad P_Key parameter '%s'\n", p); 3526 goto out; 3527 } 3528 target->ib_cm.pkey = cpu_to_be16(token); 3529 break; 3530 3531 case SRP_OPT_SERVICE_ID: 3532 p = match_strdup(args); 3533 if (!p) { 3534 ret = -ENOMEM; 3535 goto out; 3536 } 3537 ret = kstrtoull(p, 16, &ull); 3538 if (ret) { 3539 pr_warn("bad service_id parameter '%s'\n", p); 3540 kfree(p); 3541 goto out; 3542 } 3543 target->ib_cm.service_id = cpu_to_be64(ull); 3544 kfree(p); 3545 break; 3546 3547 case SRP_OPT_IP_SRC: 3548 p = match_strdup(args); 3549 if (!p) { 3550 ret = -ENOMEM; 3551 goto out; 3552 } 3553 ret = srp_parse_in(net, &target->rdma_cm.src.ss, p); 3554 if (ret < 0) { 3555 pr_warn("bad source parameter '%s'\n", p); 3556 kfree(p); 3557 goto out; 3558 } 3559 target->rdma_cm.src_specified = true; 3560 kfree(p); 3561 break; 3562 3563 case SRP_OPT_IP_DEST: 3564 p = match_strdup(args); 3565 if (!p) { 3566 ret = -ENOMEM; 3567 goto out; 3568 } 3569 ret = srp_parse_in(net, &target->rdma_cm.dst.ss, p); 3570 if (ret < 0) { 3571 pr_warn("bad dest parameter '%s'\n", p); 3572 kfree(p); 3573 goto out; 3574 } 3575 target->using_rdma_cm = true; 3576 kfree(p); 3577 break; 3578 3579 case SRP_OPT_MAX_SECT: 3580 if (match_int(args, &token)) { 3581 pr_warn("bad max sect parameter '%s'\n", p); 3582 goto out; 3583 } 3584 target->scsi_host->max_sectors = token; 3585 break; 3586 3587 case SRP_OPT_QUEUE_SIZE: 3588 if (match_int(args, &token) || token < 1) { 3589 pr_warn("bad queue_size parameter '%s'\n", p); 3590 goto out; 3591 } 3592 target->scsi_host->can_queue = token; 3593 target->queue_size = token + SRP_RSP_SQ_SIZE + 3594 SRP_TSK_MGMT_SQ_SIZE; 3595 if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN)) 3596 target->scsi_host->cmd_per_lun = token; 3597 break; 3598 3599 case SRP_OPT_MAX_CMD_PER_LUN: 3600 if (match_int(args, &token) || token < 1) { 3601 pr_warn("bad max cmd_per_lun parameter '%s'\n", 3602 p); 3603 goto out; 3604 } 3605 target->scsi_host->cmd_per_lun = token; 3606 break; 3607 3608 case SRP_OPT_TARGET_CAN_QUEUE: 3609 if (match_int(args, &token) || token < 1) { 3610 pr_warn("bad max target_can_queue parameter '%s'\n", 3611 p); 3612 goto out; 3613 } 3614 target->target_can_queue = token; 3615 break; 3616 3617 case SRP_OPT_IO_CLASS: 3618 if (match_hex(args, &token)) { 3619 pr_warn("bad IO class parameter '%s'\n", p); 3620 goto out; 3621 } 3622 if (token != SRP_REV10_IB_IO_CLASS && 3623 token != SRP_REV16A_IB_IO_CLASS) { 3624 pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n", 3625 token, SRP_REV10_IB_IO_CLASS, 3626 SRP_REV16A_IB_IO_CLASS); 3627 goto out; 3628 } 3629 target->io_class = token; 3630 break; 3631 3632 case SRP_OPT_INITIATOR_EXT: 3633 p = match_strdup(args); 3634 if (!p) { 3635 ret = -ENOMEM; 3636 goto out; 3637 } 3638 ret = kstrtoull(p, 16, &ull); 3639 if (ret) { 3640 pr_warn("bad initiator_ext value '%s'\n", p); 3641 kfree(p); 3642 goto out; 3643 } 3644 target->initiator_ext = cpu_to_be64(ull); 3645 kfree(p); 3646 break; 3647 3648 case SRP_OPT_CMD_SG_ENTRIES: 3649 if (match_int(args, &token) || token < 1 || token > 255) { 3650 pr_warn("bad max cmd_sg_entries parameter '%s'\n", 3651 p); 3652 goto out; 3653 } 3654 target->cmd_sg_cnt = token; 3655 break; 3656 3657 case SRP_OPT_ALLOW_EXT_SG: 3658 if (match_int(args, &token)) { 3659 pr_warn("bad allow_ext_sg parameter '%s'\n", p); 3660 goto out; 3661 } 3662 target->allow_ext_sg = !!token; 3663 break; 3664 3665 case SRP_OPT_SG_TABLESIZE: 3666 if (match_int(args, &token) || token < 1 || 3667 token > SG_MAX_SEGMENTS) { 3668 pr_warn("bad max sg_tablesize parameter '%s'\n", 3669 p); 3670 goto out; 3671 } 3672 target->sg_tablesize = token; 3673 break; 3674 3675 case SRP_OPT_COMP_VECTOR: 3676 if (match_int(args, &token) || token < 0) { 3677 pr_warn("bad comp_vector parameter '%s'\n", p); 3678 goto out; 3679 } 3680 target->comp_vector = token; 3681 break; 3682 3683 case SRP_OPT_TL_RETRY_COUNT: 3684 if (match_int(args, &token) || token < 2 || token > 7) { 3685 pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n", 3686 p); 3687 goto out; 3688 } 3689 target->tl_retry_count = token; 3690 break; 3691 3692 default: 3693 pr_warn("unknown parameter or missing value '%s' in target creation request\n", 3694 p); 3695 goto out; 3696 } 3697 } 3698 3699 for (i = 0; i < ARRAY_SIZE(srp_opt_mandatory); i++) { 3700 if ((opt_mask & srp_opt_mandatory[i]) == srp_opt_mandatory[i]) { 3701 ret = 0; 3702 break; 3703 } 3704 } 3705 if (ret) 3706 pr_warn("target creation request is missing one or more parameters\n"); 3707 3708 if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue 3709 && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN)) 3710 pr_warn("cmd_per_lun = %d > queue_size = %d\n", 3711 target->scsi_host->cmd_per_lun, 3712 target->scsi_host->can_queue); 3713 3714 out: 3715 kfree(options); 3716 return ret; 3717 } 3718 3719 static ssize_t srp_create_target(struct device *dev, 3720 struct device_attribute *attr, 3721 const char *buf, size_t count) 3722 { 3723 struct srp_host *host = 3724 container_of(dev, struct srp_host, dev); 3725 struct Scsi_Host *target_host; 3726 struct srp_target_port *target; 3727 struct srp_rdma_ch *ch; 3728 struct srp_device *srp_dev = host->srp_dev; 3729 struct ib_device *ibdev = srp_dev->dev; 3730 int ret, node_idx, node, cpu, i; 3731 unsigned int max_sectors_per_mr, mr_per_cmd = 0; 3732 bool multich = false; 3733 3734 target_host = scsi_host_alloc(&srp_template, 3735 sizeof (struct srp_target_port)); 3736 if (!target_host) 3737 return -ENOMEM; 3738 3739 target_host->transportt = ib_srp_transport_template; 3740 target_host->max_channel = 0; 3741 target_host->max_id = 1; 3742 target_host->max_lun = -1LL; 3743 target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb; 3744 3745 target = host_to_target(target_host); 3746 3747 target->net = kobj_ns_grab_current(KOBJ_NS_TYPE_NET); 3748 target->io_class = SRP_REV16A_IB_IO_CLASS; 3749 target->scsi_host = target_host; 3750 target->srp_host = host; 3751 target->lkey = host->srp_dev->pd->local_dma_lkey; 3752 target->global_rkey = host->srp_dev->global_rkey; 3753 target->cmd_sg_cnt = cmd_sg_entries; 3754 target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries; 3755 target->allow_ext_sg = allow_ext_sg; 3756 target->tl_retry_count = 7; 3757 target->queue_size = SRP_DEFAULT_QUEUE_SIZE; 3758 3759 /* 3760 * Avoid that the SCSI host can be removed by srp_remove_target() 3761 * before this function returns. 3762 */ 3763 scsi_host_get(target->scsi_host); 3764 3765 ret = mutex_lock_interruptible(&host->add_target_mutex); 3766 if (ret < 0) 3767 goto put; 3768 3769 ret = srp_parse_options(target->net, buf, target); 3770 if (ret) 3771 goto out; 3772 3773 target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE; 3774 3775 if (!srp_conn_unique(target->srp_host, target)) { 3776 if (target->using_rdma_cm) { 3777 shost_printk(KERN_INFO, target->scsi_host, 3778 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;dest=%pIS\n", 3779 be64_to_cpu(target->id_ext), 3780 be64_to_cpu(target->ioc_guid), 3781 &target->rdma_cm.dst); 3782 } else { 3783 shost_printk(KERN_INFO, target->scsi_host, 3784 PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n", 3785 be64_to_cpu(target->id_ext), 3786 be64_to_cpu(target->ioc_guid), 3787 be64_to_cpu(target->initiator_ext)); 3788 } 3789 ret = -EEXIST; 3790 goto out; 3791 } 3792 3793 if (!srp_dev->has_fmr && !srp_dev->has_fr && !target->allow_ext_sg && 3794 target->cmd_sg_cnt < target->sg_tablesize) { 3795 pr_warn("No MR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n"); 3796 target->sg_tablesize = target->cmd_sg_cnt; 3797 } 3798 3799 if (srp_dev->use_fast_reg || srp_dev->use_fmr) { 3800 bool gaps_reg = (ibdev->attrs.device_cap_flags & 3801 IB_DEVICE_SG_GAPS_REG); 3802 3803 max_sectors_per_mr = srp_dev->max_pages_per_mr << 3804 (ilog2(srp_dev->mr_page_size) - 9); 3805 if (!gaps_reg) { 3806 /* 3807 * FR and FMR can only map one HCA page per entry. If 3808 * the start address is not aligned on a HCA page 3809 * boundary two entries will be used for the head and 3810 * the tail although these two entries combined 3811 * contain at most one HCA page of data. Hence the "+ 3812 * 1" in the calculation below. 3813 * 3814 * The indirect data buffer descriptor is contiguous 3815 * so the memory for that buffer will only be 3816 * registered if register_always is true. Hence add 3817 * one to mr_per_cmd if register_always has been set. 3818 */ 3819 mr_per_cmd = register_always + 3820 (target->scsi_host->max_sectors + 1 + 3821 max_sectors_per_mr - 1) / max_sectors_per_mr; 3822 } else { 3823 mr_per_cmd = register_always + 3824 (target->sg_tablesize + 3825 srp_dev->max_pages_per_mr - 1) / 3826 srp_dev->max_pages_per_mr; 3827 } 3828 pr_debug("max_sectors = %u; max_pages_per_mr = %u; mr_page_size = %u; max_sectors_per_mr = %u; mr_per_cmd = %u\n", 3829 target->scsi_host->max_sectors, srp_dev->max_pages_per_mr, srp_dev->mr_page_size, 3830 max_sectors_per_mr, mr_per_cmd); 3831 } 3832 3833 target_host->sg_tablesize = target->sg_tablesize; 3834 target->mr_pool_size = target->scsi_host->can_queue * mr_per_cmd; 3835 target->mr_per_cmd = mr_per_cmd; 3836 target->indirect_size = target->sg_tablesize * 3837 sizeof (struct srp_direct_buf); 3838 target->max_iu_len = sizeof (struct srp_cmd) + 3839 sizeof (struct srp_indirect_buf) + 3840 target->cmd_sg_cnt * sizeof (struct srp_direct_buf); 3841 3842 INIT_WORK(&target->tl_err_work, srp_tl_err_work); 3843 INIT_WORK(&target->remove_work, srp_remove_work); 3844 spin_lock_init(&target->lock); 3845 ret = ib_query_gid(ibdev, host->port, 0, &target->sgid, NULL); 3846 if (ret) 3847 goto out; 3848 3849 ret = -ENOMEM; 3850 target->ch_count = max_t(unsigned, num_online_nodes(), 3851 min(ch_count ? : 3852 min(4 * num_online_nodes(), 3853 ibdev->num_comp_vectors), 3854 num_online_cpus())); 3855 target->ch = kcalloc(target->ch_count, sizeof(*target->ch), 3856 GFP_KERNEL); 3857 if (!target->ch) 3858 goto out; 3859 3860 node_idx = 0; 3861 for_each_online_node(node) { 3862 const int ch_start = (node_idx * target->ch_count / 3863 num_online_nodes()); 3864 const int ch_end = ((node_idx + 1) * target->ch_count / 3865 num_online_nodes()); 3866 const int cv_start = node_idx * ibdev->num_comp_vectors / 3867 num_online_nodes(); 3868 const int cv_end = (node_idx + 1) * ibdev->num_comp_vectors / 3869 num_online_nodes(); 3870 int cpu_idx = 0; 3871 3872 for_each_online_cpu(cpu) { 3873 if (cpu_to_node(cpu) != node) 3874 continue; 3875 if (ch_start + cpu_idx >= ch_end) 3876 continue; 3877 ch = &target->ch[ch_start + cpu_idx]; 3878 ch->target = target; 3879 ch->comp_vector = cv_start == cv_end ? cv_start : 3880 cv_start + cpu_idx % (cv_end - cv_start); 3881 spin_lock_init(&ch->lock); 3882 INIT_LIST_HEAD(&ch->free_tx); 3883 ret = srp_new_cm_id(ch); 3884 if (ret) 3885 goto err_disconnect; 3886 3887 ret = srp_create_ch_ib(ch); 3888 if (ret) 3889 goto err_disconnect; 3890 3891 ret = srp_alloc_req_data(ch); 3892 if (ret) 3893 goto err_disconnect; 3894 3895 ret = srp_connect_ch(ch, multich); 3896 if (ret) { 3897 char dst[64]; 3898 3899 if (target->using_rdma_cm) 3900 snprintf(dst, sizeof(dst), "%pIS", 3901 &target->rdma_cm.dst); 3902 else 3903 snprintf(dst, sizeof(dst), "%pI6", 3904 target->ib_cm.orig_dgid.raw); 3905 shost_printk(KERN_ERR, target->scsi_host, 3906 PFX "Connection %d/%d to %s failed\n", 3907 ch_start + cpu_idx, 3908 target->ch_count, dst); 3909 if (node_idx == 0 && cpu_idx == 0) { 3910 goto free_ch; 3911 } else { 3912 srp_free_ch_ib(target, ch); 3913 srp_free_req_data(target, ch); 3914 target->ch_count = ch - target->ch; 3915 goto connected; 3916 } 3917 } 3918 3919 multich = true; 3920 cpu_idx++; 3921 } 3922 node_idx++; 3923 } 3924 3925 connected: 3926 target->scsi_host->nr_hw_queues = target->ch_count; 3927 3928 ret = srp_add_target(host, target); 3929 if (ret) 3930 goto err_disconnect; 3931 3932 if (target->state != SRP_TARGET_REMOVED) { 3933 if (target->using_rdma_cm) { 3934 shost_printk(KERN_DEBUG, target->scsi_host, PFX 3935 "new target: id_ext %016llx ioc_guid %016llx sgid %pI6 dest %pIS\n", 3936 be64_to_cpu(target->id_ext), 3937 be64_to_cpu(target->ioc_guid), 3938 target->sgid.raw, &target->rdma_cm.dst); 3939 } else { 3940 shost_printk(KERN_DEBUG, target->scsi_host, PFX 3941 "new target: id_ext %016llx ioc_guid %016llx pkey %04x service_id %016llx sgid %pI6 dgid %pI6\n", 3942 be64_to_cpu(target->id_ext), 3943 be64_to_cpu(target->ioc_guid), 3944 be16_to_cpu(target->ib_cm.pkey), 3945 be64_to_cpu(target->ib_cm.service_id), 3946 target->sgid.raw, 3947 target->ib_cm.orig_dgid.raw); 3948 } 3949 } 3950 3951 ret = count; 3952 3953 out: 3954 mutex_unlock(&host->add_target_mutex); 3955 3956 put: 3957 scsi_host_put(target->scsi_host); 3958 if (ret < 0) { 3959 /* 3960 * If a call to srp_remove_target() has not been scheduled, 3961 * drop the network namespace reference now that was obtained 3962 * earlier in this function. 3963 */ 3964 if (target->state != SRP_TARGET_REMOVED) 3965 kobj_ns_drop(KOBJ_NS_TYPE_NET, target->net); 3966 scsi_host_put(target->scsi_host); 3967 } 3968 3969 return ret; 3970 3971 err_disconnect: 3972 srp_disconnect_target(target); 3973 3974 free_ch: 3975 for (i = 0; i < target->ch_count; i++) { 3976 ch = &target->ch[i]; 3977 srp_free_ch_ib(target, ch); 3978 srp_free_req_data(target, ch); 3979 } 3980 3981 kfree(target->ch); 3982 goto out; 3983 } 3984 3985 static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target); 3986 3987 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr, 3988 char *buf) 3989 { 3990 struct srp_host *host = container_of(dev, struct srp_host, dev); 3991 3992 return sprintf(buf, "%s\n", host->srp_dev->dev->name); 3993 } 3994 3995 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); 3996 3997 static ssize_t show_port(struct device *dev, struct device_attribute *attr, 3998 char *buf) 3999 { 4000 struct srp_host *host = container_of(dev, struct srp_host, dev); 4001 4002 return sprintf(buf, "%d\n", host->port); 4003 } 4004 4005 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL); 4006 4007 static struct srp_host *srp_add_port(struct srp_device *device, u8 port) 4008 { 4009 struct srp_host *host; 4010 4011 host = kzalloc(sizeof *host, GFP_KERNEL); 4012 if (!host) 4013 return NULL; 4014 4015 INIT_LIST_HEAD(&host->target_list); 4016 spin_lock_init(&host->target_lock); 4017 init_completion(&host->released); 4018 mutex_init(&host->add_target_mutex); 4019 host->srp_dev = device; 4020 host->port = port; 4021 4022 host->dev.class = &srp_class; 4023 host->dev.parent = device->dev->dev.parent; 4024 dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port); 4025 4026 if (device_register(&host->dev)) 4027 goto free_host; 4028 if (device_create_file(&host->dev, &dev_attr_add_target)) 4029 goto err_class; 4030 if (device_create_file(&host->dev, &dev_attr_ibdev)) 4031 goto err_class; 4032 if (device_create_file(&host->dev, &dev_attr_port)) 4033 goto err_class; 4034 4035 return host; 4036 4037 err_class: 4038 device_unregister(&host->dev); 4039 4040 free_host: 4041 kfree(host); 4042 4043 return NULL; 4044 } 4045 4046 static void srp_add_one(struct ib_device *device) 4047 { 4048 struct srp_device *srp_dev; 4049 struct ib_device_attr *attr = &device->attrs; 4050 struct srp_host *host; 4051 int mr_page_shift, p; 4052 u64 max_pages_per_mr; 4053 unsigned int flags = 0; 4054 4055 srp_dev = kzalloc(sizeof(*srp_dev), GFP_KERNEL); 4056 if (!srp_dev) 4057 return; 4058 4059 /* 4060 * Use the smallest page size supported by the HCA, down to a 4061 * minimum of 4096 bytes. We're unlikely to build large sglists 4062 * out of smaller entries. 4063 */ 4064 mr_page_shift = max(12, ffs(attr->page_size_cap) - 1); 4065 srp_dev->mr_page_size = 1 << mr_page_shift; 4066 srp_dev->mr_page_mask = ~((u64) srp_dev->mr_page_size - 1); 4067 max_pages_per_mr = attr->max_mr_size; 4068 do_div(max_pages_per_mr, srp_dev->mr_page_size); 4069 pr_debug("%s: %llu / %u = %llu <> %u\n", __func__, 4070 attr->max_mr_size, srp_dev->mr_page_size, 4071 max_pages_per_mr, SRP_MAX_PAGES_PER_MR); 4072 srp_dev->max_pages_per_mr = min_t(u64, SRP_MAX_PAGES_PER_MR, 4073 max_pages_per_mr); 4074 4075 srp_dev->has_fmr = (device->alloc_fmr && device->dealloc_fmr && 4076 device->map_phys_fmr && device->unmap_fmr); 4077 srp_dev->has_fr = (attr->device_cap_flags & 4078 IB_DEVICE_MEM_MGT_EXTENSIONS); 4079 if (!never_register && !srp_dev->has_fmr && !srp_dev->has_fr) { 4080 dev_warn(&device->dev, "neither FMR nor FR is supported\n"); 4081 } else if (!never_register && 4082 attr->max_mr_size >= 2 * srp_dev->mr_page_size) { 4083 srp_dev->use_fast_reg = (srp_dev->has_fr && 4084 (!srp_dev->has_fmr || prefer_fr)); 4085 srp_dev->use_fmr = !srp_dev->use_fast_reg && srp_dev->has_fmr; 4086 } 4087 4088 if (never_register || !register_always || 4089 (!srp_dev->has_fmr && !srp_dev->has_fr)) 4090 flags |= IB_PD_UNSAFE_GLOBAL_RKEY; 4091 4092 if (srp_dev->use_fast_reg) { 4093 srp_dev->max_pages_per_mr = 4094 min_t(u32, srp_dev->max_pages_per_mr, 4095 attr->max_fast_reg_page_list_len); 4096 } 4097 srp_dev->mr_max_size = srp_dev->mr_page_size * 4098 srp_dev->max_pages_per_mr; 4099 pr_debug("%s: mr_page_shift = %d, device->max_mr_size = %#llx, device->max_fast_reg_page_list_len = %u, max_pages_per_mr = %d, mr_max_size = %#x\n", 4100 device->name, mr_page_shift, attr->max_mr_size, 4101 attr->max_fast_reg_page_list_len, 4102 srp_dev->max_pages_per_mr, srp_dev->mr_max_size); 4103 4104 INIT_LIST_HEAD(&srp_dev->dev_list); 4105 4106 srp_dev->dev = device; 4107 srp_dev->pd = ib_alloc_pd(device, flags); 4108 if (IS_ERR(srp_dev->pd)) 4109 goto free_dev; 4110 4111 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) { 4112 srp_dev->global_rkey = srp_dev->pd->unsafe_global_rkey; 4113 WARN_ON_ONCE(srp_dev->global_rkey == 0); 4114 } 4115 4116 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) { 4117 host = srp_add_port(srp_dev, p); 4118 if (host) 4119 list_add_tail(&host->list, &srp_dev->dev_list); 4120 } 4121 4122 ib_set_client_data(device, &srp_client, srp_dev); 4123 return; 4124 4125 free_dev: 4126 kfree(srp_dev); 4127 } 4128 4129 static void srp_remove_one(struct ib_device *device, void *client_data) 4130 { 4131 struct srp_device *srp_dev; 4132 struct srp_host *host, *tmp_host; 4133 struct srp_target_port *target; 4134 4135 srp_dev = client_data; 4136 if (!srp_dev) 4137 return; 4138 4139 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) { 4140 device_unregister(&host->dev); 4141 /* 4142 * Wait for the sysfs entry to go away, so that no new 4143 * target ports can be created. 4144 */ 4145 wait_for_completion(&host->released); 4146 4147 /* 4148 * Remove all target ports. 4149 */ 4150 spin_lock(&host->target_lock); 4151 list_for_each_entry(target, &host->target_list, list) 4152 srp_queue_remove_work(target); 4153 spin_unlock(&host->target_lock); 4154 4155 /* 4156 * Wait for tl_err and target port removal tasks. 4157 */ 4158 flush_workqueue(system_long_wq); 4159 flush_workqueue(srp_remove_wq); 4160 4161 kfree(host); 4162 } 4163 4164 ib_dealloc_pd(srp_dev->pd); 4165 4166 kfree(srp_dev); 4167 } 4168 4169 static struct srp_function_template ib_srp_transport_functions = { 4170 .has_rport_state = true, 4171 .reset_timer_if_blocked = true, 4172 .reconnect_delay = &srp_reconnect_delay, 4173 .fast_io_fail_tmo = &srp_fast_io_fail_tmo, 4174 .dev_loss_tmo = &srp_dev_loss_tmo, 4175 .reconnect = srp_rport_reconnect, 4176 .rport_delete = srp_rport_delete, 4177 .terminate_rport_io = srp_terminate_io, 4178 }; 4179 4180 static int __init srp_init_module(void) 4181 { 4182 int ret; 4183 4184 if (srp_sg_tablesize) { 4185 pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n"); 4186 if (!cmd_sg_entries) 4187 cmd_sg_entries = srp_sg_tablesize; 4188 } 4189 4190 if (!cmd_sg_entries) 4191 cmd_sg_entries = SRP_DEF_SG_TABLESIZE; 4192 4193 if (cmd_sg_entries > 255) { 4194 pr_warn("Clamping cmd_sg_entries to 255\n"); 4195 cmd_sg_entries = 255; 4196 } 4197 4198 if (!indirect_sg_entries) 4199 indirect_sg_entries = cmd_sg_entries; 4200 else if (indirect_sg_entries < cmd_sg_entries) { 4201 pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n", 4202 cmd_sg_entries); 4203 indirect_sg_entries = cmd_sg_entries; 4204 } 4205 4206 if (indirect_sg_entries > SG_MAX_SEGMENTS) { 4207 pr_warn("Clamping indirect_sg_entries to %u\n", 4208 SG_MAX_SEGMENTS); 4209 indirect_sg_entries = SG_MAX_SEGMENTS; 4210 } 4211 4212 srp_remove_wq = create_workqueue("srp_remove"); 4213 if (!srp_remove_wq) { 4214 ret = -ENOMEM; 4215 goto out; 4216 } 4217 4218 ret = -ENOMEM; 4219 ib_srp_transport_template = 4220 srp_attach_transport(&ib_srp_transport_functions); 4221 if (!ib_srp_transport_template) 4222 goto destroy_wq; 4223 4224 ret = class_register(&srp_class); 4225 if (ret) { 4226 pr_err("couldn't register class infiniband_srp\n"); 4227 goto release_tr; 4228 } 4229 4230 ib_sa_register_client(&srp_sa_client); 4231 4232 ret = ib_register_client(&srp_client); 4233 if (ret) { 4234 pr_err("couldn't register IB client\n"); 4235 goto unreg_sa; 4236 } 4237 4238 out: 4239 return ret; 4240 4241 unreg_sa: 4242 ib_sa_unregister_client(&srp_sa_client); 4243 class_unregister(&srp_class); 4244 4245 release_tr: 4246 srp_release_transport(ib_srp_transport_template); 4247 4248 destroy_wq: 4249 destroy_workqueue(srp_remove_wq); 4250 goto out; 4251 } 4252 4253 static void __exit srp_cleanup_module(void) 4254 { 4255 ib_unregister_client(&srp_client); 4256 ib_sa_unregister_client(&srp_sa_client); 4257 class_unregister(&srp_class); 4258 srp_release_transport(ib_srp_transport_template); 4259 destroy_workqueue(srp_remove_wq); 4260 } 4261 4262 module_init(srp_init_module); 4263 module_exit(srp_cleanup_module); 4264