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