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