1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2016 Avago Technologies. All rights reserved. 4 */ 5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 6 #include <linux/module.h> 7 #include <linux/parser.h> 8 #include <uapi/scsi/fc/fc_fs.h> 9 #include <uapi/scsi/fc/fc_els.h> 10 #include <linux/delay.h> 11 #include <linux/overflow.h> 12 #include <linux/blk-cgroup.h> 13 #include "nvme.h" 14 #include "fabrics.h" 15 #include <linux/nvme-fc-driver.h> 16 #include <linux/nvme-fc.h> 17 #include "fc.h" 18 #include <scsi/scsi_transport_fc.h> 19 20 /* *************************** Data Structures/Defines ****************** */ 21 22 23 enum nvme_fc_queue_flags { 24 NVME_FC_Q_CONNECTED = 0, 25 NVME_FC_Q_LIVE, 26 }; 27 28 #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */ 29 #define NVME_FC_DEFAULT_RECONNECT_TMO 2 /* delay between reconnects 30 * when connected and a 31 * connection failure. 32 */ 33 34 struct nvme_fc_queue { 35 struct nvme_fc_ctrl *ctrl; 36 struct device *dev; 37 struct blk_mq_hw_ctx *hctx; 38 void *lldd_handle; 39 size_t cmnd_capsule_len; 40 u32 qnum; 41 u32 rqcnt; 42 43 u64 connection_id; 44 atomic_t csn; 45 46 unsigned long flags; 47 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 48 49 enum nvme_fcop_flags { 50 FCOP_FLAGS_TERMIO = (1 << 0), 51 FCOP_FLAGS_AEN = (1 << 1), 52 }; 53 54 struct nvmefc_ls_req_op { 55 struct nvmefc_ls_req ls_req; 56 57 struct nvme_fc_rport *rport; 58 struct nvme_fc_queue *queue; 59 struct request *rq; 60 u32 flags; 61 62 int ls_error; 63 struct completion ls_done; 64 struct list_head lsreq_list; /* rport->ls_req_list */ 65 bool req_queued; 66 }; 67 68 struct nvmefc_ls_rcv_op { 69 struct nvme_fc_rport *rport; 70 struct nvmefc_ls_rsp *lsrsp; 71 union nvmefc_ls_requests *rqstbuf; 72 union nvmefc_ls_responses *rspbuf; 73 u16 rqstdatalen; 74 bool handled; 75 dma_addr_t rspdma; 76 struct list_head lsrcv_list; /* rport->ls_rcv_list */ 77 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 78 79 enum nvme_fcpop_state { 80 FCPOP_STATE_UNINIT = 0, 81 FCPOP_STATE_IDLE = 1, 82 FCPOP_STATE_ACTIVE = 2, 83 FCPOP_STATE_ABORTED = 3, 84 FCPOP_STATE_COMPLETE = 4, 85 }; 86 87 struct nvme_fc_fcp_op { 88 struct nvme_request nreq; /* 89 * nvme/host/core.c 90 * requires this to be 91 * the 1st element in the 92 * private structure 93 * associated with the 94 * request. 95 */ 96 struct nvmefc_fcp_req fcp_req; 97 98 struct nvme_fc_ctrl *ctrl; 99 struct nvme_fc_queue *queue; 100 struct request *rq; 101 102 atomic_t state; 103 u32 flags; 104 u32 rqno; 105 u32 nents; 106 107 struct nvme_fc_cmd_iu cmd_iu; 108 struct nvme_fc_ersp_iu rsp_iu; 109 }; 110 111 struct nvme_fcp_op_w_sgl { 112 struct nvme_fc_fcp_op op; 113 struct scatterlist sgl[NVME_INLINE_SG_CNT]; 114 uint8_t priv[]; 115 }; 116 117 struct nvme_fc_lport { 118 struct nvme_fc_local_port localport; 119 120 struct ida endp_cnt; 121 struct list_head port_list; /* nvme_fc_port_list */ 122 struct list_head endp_list; 123 struct device *dev; /* physical device for dma */ 124 struct nvme_fc_port_template *ops; 125 struct kref ref; 126 atomic_t act_rport_cnt; 127 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 128 129 struct nvme_fc_rport { 130 struct nvme_fc_remote_port remoteport; 131 132 struct list_head endp_list; /* for lport->endp_list */ 133 struct list_head ctrl_list; 134 struct list_head ls_req_list; 135 struct list_head ls_rcv_list; 136 struct list_head disc_list; 137 struct device *dev; /* physical device for dma */ 138 struct nvme_fc_lport *lport; 139 spinlock_t lock; 140 struct kref ref; 141 atomic_t act_ctrl_cnt; 142 unsigned long dev_loss_end; 143 struct work_struct lsrcv_work; 144 } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 145 146 /* fc_ctrl flags values - specified as bit positions */ 147 #define ASSOC_ACTIVE 0 148 #define ASSOC_FAILED 1 149 #define FCCTRL_TERMIO 2 150 151 struct nvme_fc_ctrl { 152 spinlock_t lock; 153 struct nvme_fc_queue *queues; 154 struct device *dev; 155 struct nvme_fc_lport *lport; 156 struct nvme_fc_rport *rport; 157 u32 cnum; 158 159 bool ioq_live; 160 u64 association_id; 161 struct nvmefc_ls_rcv_op *rcv_disconn; 162 163 struct list_head ctrl_list; /* rport->ctrl_list */ 164 165 struct blk_mq_tag_set admin_tag_set; 166 struct blk_mq_tag_set tag_set; 167 168 struct work_struct ioerr_work; 169 struct delayed_work connect_work; 170 171 struct kref ref; 172 unsigned long flags; 173 u32 iocnt; 174 wait_queue_head_t ioabort_wait; 175 176 struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS]; 177 178 struct nvme_ctrl ctrl; 179 }; 180 181 static inline struct nvme_fc_ctrl * 182 to_fc_ctrl(struct nvme_ctrl *ctrl) 183 { 184 return container_of(ctrl, struct nvme_fc_ctrl, ctrl); 185 } 186 187 static inline struct nvme_fc_lport * 188 localport_to_lport(struct nvme_fc_local_port *portptr) 189 { 190 return container_of(portptr, struct nvme_fc_lport, localport); 191 } 192 193 static inline struct nvme_fc_rport * 194 remoteport_to_rport(struct nvme_fc_remote_port *portptr) 195 { 196 return container_of(portptr, struct nvme_fc_rport, remoteport); 197 } 198 199 static inline struct nvmefc_ls_req_op * 200 ls_req_to_lsop(struct nvmefc_ls_req *lsreq) 201 { 202 return container_of(lsreq, struct nvmefc_ls_req_op, ls_req); 203 } 204 205 static inline struct nvme_fc_fcp_op * 206 fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq) 207 { 208 return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req); 209 } 210 211 212 213 /* *************************** Globals **************************** */ 214 215 216 static DEFINE_SPINLOCK(nvme_fc_lock); 217 218 static LIST_HEAD(nvme_fc_lport_list); 219 static DEFINE_IDA(nvme_fc_local_port_cnt); 220 static DEFINE_IDA(nvme_fc_ctrl_cnt); 221 222 /* 223 * These items are short-term. They will eventually be moved into 224 * a generic FC class. See comments in module init. 225 */ 226 static struct device *fc_udev_device; 227 228 static void nvme_fc_complete_rq(struct request *rq); 229 230 /* *********************** FC-NVME Port Management ************************ */ 231 232 static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *, 233 struct nvme_fc_queue *, unsigned int); 234 235 static void nvme_fc_handle_ls_rqst_work(struct work_struct *work); 236 237 238 static void 239 nvme_fc_free_lport(struct kref *ref) 240 { 241 struct nvme_fc_lport *lport = 242 container_of(ref, struct nvme_fc_lport, ref); 243 unsigned long flags; 244 245 WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED); 246 WARN_ON(!list_empty(&lport->endp_list)); 247 248 /* remove from transport list */ 249 spin_lock_irqsave(&nvme_fc_lock, flags); 250 list_del(&lport->port_list); 251 spin_unlock_irqrestore(&nvme_fc_lock, flags); 252 253 ida_free(&nvme_fc_local_port_cnt, lport->localport.port_num); 254 ida_destroy(&lport->endp_cnt); 255 256 put_device(lport->dev); 257 258 kfree(lport); 259 } 260 261 static void 262 nvme_fc_lport_put(struct nvme_fc_lport *lport) 263 { 264 kref_put(&lport->ref, nvme_fc_free_lport); 265 } 266 267 static int 268 nvme_fc_lport_get(struct nvme_fc_lport *lport) 269 { 270 return kref_get_unless_zero(&lport->ref); 271 } 272 273 274 static struct nvme_fc_lport * 275 nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo, 276 struct nvme_fc_port_template *ops, 277 struct device *dev) 278 { 279 struct nvme_fc_lport *lport; 280 unsigned long flags; 281 282 spin_lock_irqsave(&nvme_fc_lock, flags); 283 284 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { 285 if (lport->localport.node_name != pinfo->node_name || 286 lport->localport.port_name != pinfo->port_name) 287 continue; 288 289 if (lport->dev != dev) { 290 lport = ERR_PTR(-EXDEV); 291 goto out_done; 292 } 293 294 if (lport->localport.port_state != FC_OBJSTATE_DELETED) { 295 lport = ERR_PTR(-EEXIST); 296 goto out_done; 297 } 298 299 if (!nvme_fc_lport_get(lport)) { 300 /* 301 * fails if ref cnt already 0. If so, 302 * act as if lport already deleted 303 */ 304 lport = NULL; 305 goto out_done; 306 } 307 308 /* resume the lport */ 309 310 lport->ops = ops; 311 lport->localport.port_role = pinfo->port_role; 312 lport->localport.port_id = pinfo->port_id; 313 lport->localport.port_state = FC_OBJSTATE_ONLINE; 314 315 spin_unlock_irqrestore(&nvme_fc_lock, flags); 316 317 return lport; 318 } 319 320 lport = NULL; 321 322 out_done: 323 spin_unlock_irqrestore(&nvme_fc_lock, flags); 324 325 return lport; 326 } 327 328 /** 329 * nvme_fc_register_localport - transport entry point called by an 330 * LLDD to register the existence of a NVME 331 * host FC port. 332 * @pinfo: pointer to information about the port to be registered 333 * @template: LLDD entrypoints and operational parameters for the port 334 * @dev: physical hardware device node port corresponds to. Will be 335 * used for DMA mappings 336 * @portptr: pointer to a local port pointer. Upon success, the routine 337 * will allocate a nvme_fc_local_port structure and place its 338 * address in the local port pointer. Upon failure, local port 339 * pointer will be set to 0. 340 * 341 * Returns: 342 * a completion status. Must be 0 upon success; a negative errno 343 * (ex: -ENXIO) upon failure. 344 */ 345 int 346 nvme_fc_register_localport(struct nvme_fc_port_info *pinfo, 347 struct nvme_fc_port_template *template, 348 struct device *dev, 349 struct nvme_fc_local_port **portptr) 350 { 351 struct nvme_fc_lport *newrec; 352 unsigned long flags; 353 int ret, idx; 354 355 if (!template->localport_delete || !template->remoteport_delete || 356 !template->ls_req || !template->fcp_io || 357 !template->ls_abort || !template->fcp_abort || 358 !template->max_hw_queues || !template->max_sgl_segments || 359 !template->max_dif_sgl_segments || !template->dma_boundary) { 360 ret = -EINVAL; 361 goto out_reghost_failed; 362 } 363 364 /* 365 * look to see if there is already a localport that had been 366 * deregistered and in the process of waiting for all the 367 * references to fully be removed. If the references haven't 368 * expired, we can simply re-enable the localport. Remoteports 369 * and controller reconnections should resume naturally. 370 */ 371 newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev); 372 373 /* found an lport, but something about its state is bad */ 374 if (IS_ERR(newrec)) { 375 ret = PTR_ERR(newrec); 376 goto out_reghost_failed; 377 378 /* found existing lport, which was resumed */ 379 } else if (newrec) { 380 *portptr = &newrec->localport; 381 return 0; 382 } 383 384 /* nothing found - allocate a new localport struct */ 385 386 newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz), 387 GFP_KERNEL); 388 if (!newrec) { 389 ret = -ENOMEM; 390 goto out_reghost_failed; 391 } 392 393 idx = ida_alloc(&nvme_fc_local_port_cnt, GFP_KERNEL); 394 if (idx < 0) { 395 ret = -ENOSPC; 396 goto out_fail_kfree; 397 } 398 399 if (!get_device(dev) && dev) { 400 ret = -ENODEV; 401 goto out_ida_put; 402 } 403 404 INIT_LIST_HEAD(&newrec->port_list); 405 INIT_LIST_HEAD(&newrec->endp_list); 406 kref_init(&newrec->ref); 407 atomic_set(&newrec->act_rport_cnt, 0); 408 newrec->ops = template; 409 newrec->dev = dev; 410 ida_init(&newrec->endp_cnt); 411 if (template->local_priv_sz) 412 newrec->localport.private = &newrec[1]; 413 else 414 newrec->localport.private = NULL; 415 newrec->localport.node_name = pinfo->node_name; 416 newrec->localport.port_name = pinfo->port_name; 417 newrec->localport.port_role = pinfo->port_role; 418 newrec->localport.port_id = pinfo->port_id; 419 newrec->localport.port_state = FC_OBJSTATE_ONLINE; 420 newrec->localport.port_num = idx; 421 422 spin_lock_irqsave(&nvme_fc_lock, flags); 423 list_add_tail(&newrec->port_list, &nvme_fc_lport_list); 424 spin_unlock_irqrestore(&nvme_fc_lock, flags); 425 426 if (dev) 427 dma_set_seg_boundary(dev, template->dma_boundary); 428 429 *portptr = &newrec->localport; 430 return 0; 431 432 out_ida_put: 433 ida_free(&nvme_fc_local_port_cnt, idx); 434 out_fail_kfree: 435 kfree(newrec); 436 out_reghost_failed: 437 *portptr = NULL; 438 439 return ret; 440 } 441 EXPORT_SYMBOL_GPL(nvme_fc_register_localport); 442 443 /** 444 * nvme_fc_unregister_localport - transport entry point called by an 445 * LLDD to deregister/remove a previously 446 * registered a NVME host FC port. 447 * @portptr: pointer to the (registered) local port that is to be deregistered. 448 * 449 * Returns: 450 * a completion status. Must be 0 upon success; a negative errno 451 * (ex: -ENXIO) upon failure. 452 */ 453 int 454 nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr) 455 { 456 struct nvme_fc_lport *lport = localport_to_lport(portptr); 457 unsigned long flags; 458 459 if (!portptr) 460 return -EINVAL; 461 462 spin_lock_irqsave(&nvme_fc_lock, flags); 463 464 if (portptr->port_state != FC_OBJSTATE_ONLINE) { 465 spin_unlock_irqrestore(&nvme_fc_lock, flags); 466 return -EINVAL; 467 } 468 portptr->port_state = FC_OBJSTATE_DELETED; 469 470 spin_unlock_irqrestore(&nvme_fc_lock, flags); 471 472 if (atomic_read(&lport->act_rport_cnt) == 0) 473 lport->ops->localport_delete(&lport->localport); 474 475 nvme_fc_lport_put(lport); 476 477 return 0; 478 } 479 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport); 480 481 /* 482 * TRADDR strings, per FC-NVME are fixed format: 483 * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters 484 * udev event will only differ by prefix of what field is 485 * being specified: 486 * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters 487 * 19 + 43 + null_fudge = 64 characters 488 */ 489 #define FCNVME_TRADDR_LENGTH 64 490 491 static void 492 nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport, 493 struct nvme_fc_rport *rport) 494 { 495 char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/ 496 char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/ 497 char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL }; 498 499 if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY)) 500 return; 501 502 snprintf(hostaddr, sizeof(hostaddr), 503 "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx", 504 lport->localport.node_name, lport->localport.port_name); 505 snprintf(tgtaddr, sizeof(tgtaddr), 506 "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx", 507 rport->remoteport.node_name, rport->remoteport.port_name); 508 kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp); 509 } 510 511 static void 512 nvme_fc_free_rport(struct kref *ref) 513 { 514 struct nvme_fc_rport *rport = 515 container_of(ref, struct nvme_fc_rport, ref); 516 struct nvme_fc_lport *lport = 517 localport_to_lport(rport->remoteport.localport); 518 unsigned long flags; 519 520 WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED); 521 WARN_ON(!list_empty(&rport->ctrl_list)); 522 WARN_ON(!list_empty(&rport->ls_req_list)); 523 WARN_ON(!list_empty(&rport->ls_rcv_list)); 524 525 /* remove from lport list */ 526 spin_lock_irqsave(&nvme_fc_lock, flags); 527 list_del(&rport->endp_list); 528 spin_unlock_irqrestore(&nvme_fc_lock, flags); 529 530 WARN_ON(!list_empty(&rport->disc_list)); 531 ida_free(&lport->endp_cnt, rport->remoteport.port_num); 532 533 kfree(rport); 534 535 nvme_fc_lport_put(lport); 536 } 537 538 static void 539 nvme_fc_rport_put(struct nvme_fc_rport *rport) 540 { 541 kref_put(&rport->ref, nvme_fc_free_rport); 542 } 543 544 static int 545 nvme_fc_rport_get(struct nvme_fc_rport *rport) 546 { 547 return kref_get_unless_zero(&rport->ref); 548 } 549 550 static void 551 nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl) 552 { 553 switch (nvme_ctrl_state(&ctrl->ctrl)) { 554 case NVME_CTRL_NEW: 555 case NVME_CTRL_CONNECTING: 556 /* 557 * As all reconnects were suppressed, schedule a 558 * connect. 559 */ 560 dev_info(ctrl->ctrl.device, 561 "NVME-FC{%d}: connectivity re-established. " 562 "Attempting reconnect\n", ctrl->cnum); 563 564 queue_delayed_work(nvme_wq, &ctrl->connect_work, 0); 565 break; 566 567 case NVME_CTRL_RESETTING: 568 /* 569 * Controller is already in the process of terminating the 570 * association. No need to do anything further. The reconnect 571 * step will naturally occur after the reset completes. 572 */ 573 break; 574 575 default: 576 /* no action to take - let it delete */ 577 break; 578 } 579 } 580 581 static struct nvme_fc_rport * 582 nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport, 583 struct nvme_fc_port_info *pinfo) 584 { 585 struct nvme_fc_rport *rport; 586 struct nvme_fc_ctrl *ctrl; 587 unsigned long flags; 588 589 spin_lock_irqsave(&nvme_fc_lock, flags); 590 591 list_for_each_entry(rport, &lport->endp_list, endp_list) { 592 if (rport->remoteport.node_name != pinfo->node_name || 593 rport->remoteport.port_name != pinfo->port_name) 594 continue; 595 596 if (!nvme_fc_rport_get(rport)) { 597 rport = ERR_PTR(-ENOLCK); 598 goto out_done; 599 } 600 601 spin_unlock_irqrestore(&nvme_fc_lock, flags); 602 603 spin_lock_irqsave(&rport->lock, flags); 604 605 /* has it been unregistered */ 606 if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) { 607 /* means lldd called us twice */ 608 spin_unlock_irqrestore(&rport->lock, flags); 609 nvme_fc_rport_put(rport); 610 return ERR_PTR(-ESTALE); 611 } 612 613 rport->remoteport.port_role = pinfo->port_role; 614 rport->remoteport.port_id = pinfo->port_id; 615 rport->remoteport.port_state = FC_OBJSTATE_ONLINE; 616 rport->dev_loss_end = 0; 617 618 /* 619 * kick off a reconnect attempt on all associations to the 620 * remote port. A successful reconnects will resume i/o. 621 */ 622 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) 623 nvme_fc_resume_controller(ctrl); 624 625 spin_unlock_irqrestore(&rport->lock, flags); 626 627 return rport; 628 } 629 630 rport = NULL; 631 632 out_done: 633 spin_unlock_irqrestore(&nvme_fc_lock, flags); 634 635 return rport; 636 } 637 638 static inline void 639 __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport, 640 struct nvme_fc_port_info *pinfo) 641 { 642 if (pinfo->dev_loss_tmo) 643 rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo; 644 else 645 rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO; 646 } 647 648 /** 649 * nvme_fc_register_remoteport - transport entry point called by an 650 * LLDD to register the existence of a NVME 651 * subsystem FC port on its fabric. 652 * @localport: pointer to the (registered) local port that the remote 653 * subsystem port is connected to. 654 * @pinfo: pointer to information about the port to be registered 655 * @portptr: pointer to a remote port pointer. Upon success, the routine 656 * will allocate a nvme_fc_remote_port structure and place its 657 * address in the remote port pointer. Upon failure, remote port 658 * pointer will be set to 0. 659 * 660 * Returns: 661 * a completion status. Must be 0 upon success; a negative errno 662 * (ex: -ENXIO) upon failure. 663 */ 664 int 665 nvme_fc_register_remoteport(struct nvme_fc_local_port *localport, 666 struct nvme_fc_port_info *pinfo, 667 struct nvme_fc_remote_port **portptr) 668 { 669 struct nvme_fc_lport *lport = localport_to_lport(localport); 670 struct nvme_fc_rport *newrec; 671 unsigned long flags; 672 int ret, idx; 673 674 if (!nvme_fc_lport_get(lport)) { 675 ret = -ESHUTDOWN; 676 goto out_reghost_failed; 677 } 678 679 /* 680 * look to see if there is already a remoteport that is waiting 681 * for a reconnect (within dev_loss_tmo) with the same WWN's. 682 * If so, transition to it and reconnect. 683 */ 684 newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo); 685 686 /* found an rport, but something about its state is bad */ 687 if (IS_ERR(newrec)) { 688 ret = PTR_ERR(newrec); 689 goto out_lport_put; 690 691 /* found existing rport, which was resumed */ 692 } else if (newrec) { 693 nvme_fc_lport_put(lport); 694 __nvme_fc_set_dev_loss_tmo(newrec, pinfo); 695 nvme_fc_signal_discovery_scan(lport, newrec); 696 *portptr = &newrec->remoteport; 697 return 0; 698 } 699 700 /* nothing found - allocate a new remoteport struct */ 701 702 newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz), 703 GFP_KERNEL); 704 if (!newrec) { 705 ret = -ENOMEM; 706 goto out_lport_put; 707 } 708 709 idx = ida_alloc(&lport->endp_cnt, GFP_KERNEL); 710 if (idx < 0) { 711 ret = -ENOSPC; 712 goto out_kfree_rport; 713 } 714 715 INIT_LIST_HEAD(&newrec->endp_list); 716 INIT_LIST_HEAD(&newrec->ctrl_list); 717 INIT_LIST_HEAD(&newrec->ls_req_list); 718 INIT_LIST_HEAD(&newrec->disc_list); 719 kref_init(&newrec->ref); 720 atomic_set(&newrec->act_ctrl_cnt, 0); 721 spin_lock_init(&newrec->lock); 722 newrec->remoteport.localport = &lport->localport; 723 INIT_LIST_HEAD(&newrec->ls_rcv_list); 724 newrec->dev = lport->dev; 725 newrec->lport = lport; 726 if (lport->ops->remote_priv_sz) 727 newrec->remoteport.private = &newrec[1]; 728 else 729 newrec->remoteport.private = NULL; 730 newrec->remoteport.port_role = pinfo->port_role; 731 newrec->remoteport.node_name = pinfo->node_name; 732 newrec->remoteport.port_name = pinfo->port_name; 733 newrec->remoteport.port_id = pinfo->port_id; 734 newrec->remoteport.port_state = FC_OBJSTATE_ONLINE; 735 newrec->remoteport.port_num = idx; 736 __nvme_fc_set_dev_loss_tmo(newrec, pinfo); 737 INIT_WORK(&newrec->lsrcv_work, nvme_fc_handle_ls_rqst_work); 738 739 spin_lock_irqsave(&nvme_fc_lock, flags); 740 list_add_tail(&newrec->endp_list, &lport->endp_list); 741 spin_unlock_irqrestore(&nvme_fc_lock, flags); 742 743 nvme_fc_signal_discovery_scan(lport, newrec); 744 745 *portptr = &newrec->remoteport; 746 return 0; 747 748 out_kfree_rport: 749 kfree(newrec); 750 out_lport_put: 751 nvme_fc_lport_put(lport); 752 out_reghost_failed: 753 *portptr = NULL; 754 return ret; 755 } 756 EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport); 757 758 static int 759 nvme_fc_abort_lsops(struct nvme_fc_rport *rport) 760 { 761 struct nvmefc_ls_req_op *lsop; 762 unsigned long flags; 763 764 restart: 765 spin_lock_irqsave(&rport->lock, flags); 766 767 list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) { 768 if (!(lsop->flags & FCOP_FLAGS_TERMIO)) { 769 lsop->flags |= FCOP_FLAGS_TERMIO; 770 spin_unlock_irqrestore(&rport->lock, flags); 771 rport->lport->ops->ls_abort(&rport->lport->localport, 772 &rport->remoteport, 773 &lsop->ls_req); 774 goto restart; 775 } 776 } 777 spin_unlock_irqrestore(&rport->lock, flags); 778 779 return 0; 780 } 781 782 static void 783 nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl) 784 { 785 dev_info(ctrl->ctrl.device, 786 "NVME-FC{%d}: controller connectivity lost. Awaiting " 787 "Reconnect", ctrl->cnum); 788 789 set_bit(ASSOC_FAILED, &ctrl->flags); 790 nvme_reset_ctrl(&ctrl->ctrl); 791 } 792 793 /** 794 * nvme_fc_unregister_remoteport - transport entry point called by an 795 * LLDD to deregister/remove a previously 796 * registered a NVME subsystem FC port. 797 * @portptr: pointer to the (registered) remote port that is to be 798 * deregistered. 799 * 800 * Returns: 801 * a completion status. Must be 0 upon success; a negative errno 802 * (ex: -ENXIO) upon failure. 803 */ 804 int 805 nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr) 806 { 807 struct nvme_fc_rport *rport = remoteport_to_rport(portptr); 808 struct nvme_fc_ctrl *ctrl; 809 unsigned long flags; 810 811 if (!portptr) 812 return -EINVAL; 813 814 spin_lock_irqsave(&rport->lock, flags); 815 816 if (portptr->port_state != FC_OBJSTATE_ONLINE) { 817 spin_unlock_irqrestore(&rport->lock, flags); 818 return -EINVAL; 819 } 820 portptr->port_state = FC_OBJSTATE_DELETED; 821 822 rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ); 823 824 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) { 825 /* if dev_loss_tmo==0, dev loss is immediate */ 826 if (!portptr->dev_loss_tmo) { 827 dev_warn(ctrl->ctrl.device, 828 "NVME-FC{%d}: controller connectivity lost.\n", 829 ctrl->cnum); 830 nvme_delete_ctrl(&ctrl->ctrl); 831 } else 832 nvme_fc_ctrl_connectivity_loss(ctrl); 833 } 834 835 spin_unlock_irqrestore(&rport->lock, flags); 836 837 nvme_fc_abort_lsops(rport); 838 839 if (atomic_read(&rport->act_ctrl_cnt) == 0) 840 rport->lport->ops->remoteport_delete(portptr); 841 842 /* 843 * release the reference, which will allow, if all controllers 844 * go away, which should only occur after dev_loss_tmo occurs, 845 * for the rport to be torn down. 846 */ 847 nvme_fc_rport_put(rport); 848 849 return 0; 850 } 851 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport); 852 853 /** 854 * nvme_fc_rescan_remoteport - transport entry point called by an 855 * LLDD to request a nvme device rescan. 856 * @remoteport: pointer to the (registered) remote port that is to be 857 * rescanned. 858 * 859 * Returns: N/A 860 */ 861 void 862 nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport) 863 { 864 struct nvme_fc_rport *rport = remoteport_to_rport(remoteport); 865 866 nvme_fc_signal_discovery_scan(rport->lport, rport); 867 } 868 EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport); 869 870 int 871 nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr, 872 u32 dev_loss_tmo) 873 { 874 struct nvme_fc_rport *rport = remoteport_to_rport(portptr); 875 unsigned long flags; 876 877 spin_lock_irqsave(&rport->lock, flags); 878 879 if (portptr->port_state != FC_OBJSTATE_ONLINE) { 880 spin_unlock_irqrestore(&rport->lock, flags); 881 return -EINVAL; 882 } 883 884 /* a dev_loss_tmo of 0 (immediate) is allowed to be set */ 885 rport->remoteport.dev_loss_tmo = dev_loss_tmo; 886 887 spin_unlock_irqrestore(&rport->lock, flags); 888 889 return 0; 890 } 891 EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss); 892 893 894 /* *********************** FC-NVME DMA Handling **************************** */ 895 896 /* 897 * The fcloop device passes in a NULL device pointer. Real LLD's will 898 * pass in a valid device pointer. If NULL is passed to the dma mapping 899 * routines, depending on the platform, it may or may not succeed, and 900 * may crash. 901 * 902 * As such: 903 * Wrap all the dma routines and check the dev pointer. 904 * 905 * If simple mappings (return just a dma address, we'll noop them, 906 * returning a dma address of 0. 907 * 908 * On more complex mappings (dma_map_sg), a pseudo routine fills 909 * in the scatter list, setting all dma addresses to 0. 910 */ 911 912 static inline dma_addr_t 913 fc_dma_map_single(struct device *dev, void *ptr, size_t size, 914 enum dma_data_direction dir) 915 { 916 return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L; 917 } 918 919 static inline int 920 fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 921 { 922 return dev ? dma_mapping_error(dev, dma_addr) : 0; 923 } 924 925 static inline void 926 fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size, 927 enum dma_data_direction dir) 928 { 929 if (dev) 930 dma_unmap_single(dev, addr, size, dir); 931 } 932 933 static inline void 934 fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size, 935 enum dma_data_direction dir) 936 { 937 if (dev) 938 dma_sync_single_for_cpu(dev, addr, size, dir); 939 } 940 941 static inline void 942 fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size, 943 enum dma_data_direction dir) 944 { 945 if (dev) 946 dma_sync_single_for_device(dev, addr, size, dir); 947 } 948 949 /* pseudo dma_map_sg call */ 950 static int 951 fc_map_sg(struct scatterlist *sg, int nents) 952 { 953 struct scatterlist *s; 954 int i; 955 956 WARN_ON(nents == 0 || sg[0].length == 0); 957 958 for_each_sg(sg, s, nents, i) { 959 s->dma_address = 0L; 960 #ifdef CONFIG_NEED_SG_DMA_LENGTH 961 s->dma_length = s->length; 962 #endif 963 } 964 return nents; 965 } 966 967 static inline int 968 fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, 969 enum dma_data_direction dir) 970 { 971 return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents); 972 } 973 974 static inline void 975 fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, 976 enum dma_data_direction dir) 977 { 978 if (dev) 979 dma_unmap_sg(dev, sg, nents, dir); 980 } 981 982 /* *********************** FC-NVME LS Handling **************************** */ 983 984 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *); 985 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *); 986 987 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg); 988 989 static void 990 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop) 991 { 992 struct nvme_fc_rport *rport = lsop->rport; 993 struct nvmefc_ls_req *lsreq = &lsop->ls_req; 994 unsigned long flags; 995 996 spin_lock_irqsave(&rport->lock, flags); 997 998 if (!lsop->req_queued) { 999 spin_unlock_irqrestore(&rport->lock, flags); 1000 return; 1001 } 1002 1003 list_del(&lsop->lsreq_list); 1004 1005 lsop->req_queued = false; 1006 1007 spin_unlock_irqrestore(&rport->lock, flags); 1008 1009 fc_dma_unmap_single(rport->dev, lsreq->rqstdma, 1010 (lsreq->rqstlen + lsreq->rsplen), 1011 DMA_BIDIRECTIONAL); 1012 1013 nvme_fc_rport_put(rport); 1014 } 1015 1016 static int 1017 __nvme_fc_send_ls_req(struct nvme_fc_rport *rport, 1018 struct nvmefc_ls_req_op *lsop, 1019 void (*done)(struct nvmefc_ls_req *req, int status)) 1020 { 1021 struct nvmefc_ls_req *lsreq = &lsop->ls_req; 1022 unsigned long flags; 1023 int ret = 0; 1024 1025 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) 1026 return -ECONNREFUSED; 1027 1028 if (!nvme_fc_rport_get(rport)) 1029 return -ESHUTDOWN; 1030 1031 lsreq->done = done; 1032 lsop->rport = rport; 1033 lsop->req_queued = false; 1034 INIT_LIST_HEAD(&lsop->lsreq_list); 1035 init_completion(&lsop->ls_done); 1036 1037 lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr, 1038 lsreq->rqstlen + lsreq->rsplen, 1039 DMA_BIDIRECTIONAL); 1040 if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) { 1041 ret = -EFAULT; 1042 goto out_putrport; 1043 } 1044 lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen; 1045 1046 spin_lock_irqsave(&rport->lock, flags); 1047 1048 list_add_tail(&lsop->lsreq_list, &rport->ls_req_list); 1049 1050 lsop->req_queued = true; 1051 1052 spin_unlock_irqrestore(&rport->lock, flags); 1053 1054 ret = rport->lport->ops->ls_req(&rport->lport->localport, 1055 &rport->remoteport, lsreq); 1056 if (ret) 1057 goto out_unlink; 1058 1059 return 0; 1060 1061 out_unlink: 1062 lsop->ls_error = ret; 1063 spin_lock_irqsave(&rport->lock, flags); 1064 lsop->req_queued = false; 1065 list_del(&lsop->lsreq_list); 1066 spin_unlock_irqrestore(&rport->lock, flags); 1067 fc_dma_unmap_single(rport->dev, lsreq->rqstdma, 1068 (lsreq->rqstlen + lsreq->rsplen), 1069 DMA_BIDIRECTIONAL); 1070 out_putrport: 1071 nvme_fc_rport_put(rport); 1072 1073 return ret; 1074 } 1075 1076 static void 1077 nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status) 1078 { 1079 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq); 1080 1081 lsop->ls_error = status; 1082 complete(&lsop->ls_done); 1083 } 1084 1085 static int 1086 nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop) 1087 { 1088 struct nvmefc_ls_req *lsreq = &lsop->ls_req; 1089 struct fcnvme_ls_rjt *rjt = lsreq->rspaddr; 1090 int ret; 1091 1092 ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done); 1093 1094 if (!ret) { 1095 /* 1096 * No timeout/not interruptible as we need the struct 1097 * to exist until the lldd calls us back. Thus mandate 1098 * wait until driver calls back. lldd responsible for 1099 * the timeout action 1100 */ 1101 wait_for_completion(&lsop->ls_done); 1102 1103 __nvme_fc_finish_ls_req(lsop); 1104 1105 ret = lsop->ls_error; 1106 } 1107 1108 if (ret) 1109 return ret; 1110 1111 /* ACC or RJT payload ? */ 1112 if (rjt->w0.ls_cmd == FCNVME_LS_RJT) 1113 return -ENXIO; 1114 1115 return 0; 1116 } 1117 1118 static int 1119 nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport, 1120 struct nvmefc_ls_req_op *lsop, 1121 void (*done)(struct nvmefc_ls_req *req, int status)) 1122 { 1123 /* don't wait for completion */ 1124 1125 return __nvme_fc_send_ls_req(rport, lsop, done); 1126 } 1127 1128 static int 1129 nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl, 1130 struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio) 1131 { 1132 struct nvmefc_ls_req_op *lsop; 1133 struct nvmefc_ls_req *lsreq; 1134 struct fcnvme_ls_cr_assoc_rqst *assoc_rqst; 1135 struct fcnvme_ls_cr_assoc_acc *assoc_acc; 1136 unsigned long flags; 1137 int ret, fcret = 0; 1138 1139 lsop = kzalloc((sizeof(*lsop) + 1140 sizeof(*assoc_rqst) + sizeof(*assoc_acc) + 1141 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL); 1142 if (!lsop) { 1143 dev_info(ctrl->ctrl.device, 1144 "NVME-FC{%d}: send Create Association failed: ENOMEM\n", 1145 ctrl->cnum); 1146 ret = -ENOMEM; 1147 goto out_no_memory; 1148 } 1149 1150 assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1]; 1151 assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1]; 1152 lsreq = &lsop->ls_req; 1153 if (ctrl->lport->ops->lsrqst_priv_sz) 1154 lsreq->private = &assoc_acc[1]; 1155 else 1156 lsreq->private = NULL; 1157 1158 assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION; 1159 assoc_rqst->desc_list_len = 1160 cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd)); 1161 1162 assoc_rqst->assoc_cmd.desc_tag = 1163 cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD); 1164 assoc_rqst->assoc_cmd.desc_len = 1165 fcnvme_lsdesc_len( 1166 sizeof(struct fcnvme_lsdesc_cr_assoc_cmd)); 1167 1168 assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio); 1169 assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1); 1170 /* Linux supports only Dynamic controllers */ 1171 assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff); 1172 uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id); 1173 strscpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn, 1174 sizeof(assoc_rqst->assoc_cmd.hostnqn)); 1175 strscpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn, 1176 sizeof(assoc_rqst->assoc_cmd.subnqn)); 1177 1178 lsop->queue = queue; 1179 lsreq->rqstaddr = assoc_rqst; 1180 lsreq->rqstlen = sizeof(*assoc_rqst); 1181 lsreq->rspaddr = assoc_acc; 1182 lsreq->rsplen = sizeof(*assoc_acc); 1183 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC; 1184 1185 ret = nvme_fc_send_ls_req(ctrl->rport, lsop); 1186 if (ret) 1187 goto out_free_buffer; 1188 1189 /* process connect LS completion */ 1190 1191 /* validate the ACC response */ 1192 if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC) 1193 fcret = VERR_LSACC; 1194 else if (assoc_acc->hdr.desc_list_len != 1195 fcnvme_lsdesc_len( 1196 sizeof(struct fcnvme_ls_cr_assoc_acc))) 1197 fcret = VERR_CR_ASSOC_ACC_LEN; 1198 else if (assoc_acc->hdr.rqst.desc_tag != 1199 cpu_to_be32(FCNVME_LSDESC_RQST)) 1200 fcret = VERR_LSDESC_RQST; 1201 else if (assoc_acc->hdr.rqst.desc_len != 1202 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst))) 1203 fcret = VERR_LSDESC_RQST_LEN; 1204 else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION) 1205 fcret = VERR_CR_ASSOC; 1206 else if (assoc_acc->associd.desc_tag != 1207 cpu_to_be32(FCNVME_LSDESC_ASSOC_ID)) 1208 fcret = VERR_ASSOC_ID; 1209 else if (assoc_acc->associd.desc_len != 1210 fcnvme_lsdesc_len( 1211 sizeof(struct fcnvme_lsdesc_assoc_id))) 1212 fcret = VERR_ASSOC_ID_LEN; 1213 else if (assoc_acc->connectid.desc_tag != 1214 cpu_to_be32(FCNVME_LSDESC_CONN_ID)) 1215 fcret = VERR_CONN_ID; 1216 else if (assoc_acc->connectid.desc_len != 1217 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id))) 1218 fcret = VERR_CONN_ID_LEN; 1219 1220 if (fcret) { 1221 ret = -EBADF; 1222 dev_err(ctrl->dev, 1223 "q %d Create Association LS failed: %s\n", 1224 queue->qnum, validation_errors[fcret]); 1225 } else { 1226 spin_lock_irqsave(&ctrl->lock, flags); 1227 ctrl->association_id = 1228 be64_to_cpu(assoc_acc->associd.association_id); 1229 queue->connection_id = 1230 be64_to_cpu(assoc_acc->connectid.connection_id); 1231 set_bit(NVME_FC_Q_CONNECTED, &queue->flags); 1232 spin_unlock_irqrestore(&ctrl->lock, flags); 1233 } 1234 1235 out_free_buffer: 1236 kfree(lsop); 1237 out_no_memory: 1238 if (ret) 1239 dev_err(ctrl->dev, 1240 "queue %d connect admin queue failed (%d).\n", 1241 queue->qnum, ret); 1242 return ret; 1243 } 1244 1245 static int 1246 nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue, 1247 u16 qsize, u16 ersp_ratio) 1248 { 1249 struct nvmefc_ls_req_op *lsop; 1250 struct nvmefc_ls_req *lsreq; 1251 struct fcnvme_ls_cr_conn_rqst *conn_rqst; 1252 struct fcnvme_ls_cr_conn_acc *conn_acc; 1253 int ret, fcret = 0; 1254 1255 lsop = kzalloc((sizeof(*lsop) + 1256 sizeof(*conn_rqst) + sizeof(*conn_acc) + 1257 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL); 1258 if (!lsop) { 1259 dev_info(ctrl->ctrl.device, 1260 "NVME-FC{%d}: send Create Connection failed: ENOMEM\n", 1261 ctrl->cnum); 1262 ret = -ENOMEM; 1263 goto out_no_memory; 1264 } 1265 1266 conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)&lsop[1]; 1267 conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1]; 1268 lsreq = &lsop->ls_req; 1269 if (ctrl->lport->ops->lsrqst_priv_sz) 1270 lsreq->private = (void *)&conn_acc[1]; 1271 else 1272 lsreq->private = NULL; 1273 1274 conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION; 1275 conn_rqst->desc_list_len = cpu_to_be32( 1276 sizeof(struct fcnvme_lsdesc_assoc_id) + 1277 sizeof(struct fcnvme_lsdesc_cr_conn_cmd)); 1278 1279 conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID); 1280 conn_rqst->associd.desc_len = 1281 fcnvme_lsdesc_len( 1282 sizeof(struct fcnvme_lsdesc_assoc_id)); 1283 conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id); 1284 conn_rqst->connect_cmd.desc_tag = 1285 cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD); 1286 conn_rqst->connect_cmd.desc_len = 1287 fcnvme_lsdesc_len( 1288 sizeof(struct fcnvme_lsdesc_cr_conn_cmd)); 1289 conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio); 1290 conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum); 1291 conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1); 1292 1293 lsop->queue = queue; 1294 lsreq->rqstaddr = conn_rqst; 1295 lsreq->rqstlen = sizeof(*conn_rqst); 1296 lsreq->rspaddr = conn_acc; 1297 lsreq->rsplen = sizeof(*conn_acc); 1298 lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC; 1299 1300 ret = nvme_fc_send_ls_req(ctrl->rport, lsop); 1301 if (ret) 1302 goto out_free_buffer; 1303 1304 /* process connect LS completion */ 1305 1306 /* validate the ACC response */ 1307 if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC) 1308 fcret = VERR_LSACC; 1309 else if (conn_acc->hdr.desc_list_len != 1310 fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc))) 1311 fcret = VERR_CR_CONN_ACC_LEN; 1312 else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST)) 1313 fcret = VERR_LSDESC_RQST; 1314 else if (conn_acc->hdr.rqst.desc_len != 1315 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst))) 1316 fcret = VERR_LSDESC_RQST_LEN; 1317 else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION) 1318 fcret = VERR_CR_CONN; 1319 else if (conn_acc->connectid.desc_tag != 1320 cpu_to_be32(FCNVME_LSDESC_CONN_ID)) 1321 fcret = VERR_CONN_ID; 1322 else if (conn_acc->connectid.desc_len != 1323 fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id))) 1324 fcret = VERR_CONN_ID_LEN; 1325 1326 if (fcret) { 1327 ret = -EBADF; 1328 dev_err(ctrl->dev, 1329 "q %d Create I/O Connection LS failed: %s\n", 1330 queue->qnum, validation_errors[fcret]); 1331 } else { 1332 queue->connection_id = 1333 be64_to_cpu(conn_acc->connectid.connection_id); 1334 set_bit(NVME_FC_Q_CONNECTED, &queue->flags); 1335 } 1336 1337 out_free_buffer: 1338 kfree(lsop); 1339 out_no_memory: 1340 if (ret) 1341 dev_err(ctrl->dev, 1342 "queue %d connect I/O queue failed (%d).\n", 1343 queue->qnum, ret); 1344 return ret; 1345 } 1346 1347 static void 1348 nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status) 1349 { 1350 struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq); 1351 1352 __nvme_fc_finish_ls_req(lsop); 1353 1354 /* fc-nvme initiator doesn't care about success or failure of cmd */ 1355 1356 kfree(lsop); 1357 } 1358 1359 /* 1360 * This routine sends a FC-NVME LS to disconnect (aka terminate) 1361 * the FC-NVME Association. Terminating the association also 1362 * terminates the FC-NVME connections (per queue, both admin and io 1363 * queues) that are part of the association. E.g. things are torn 1364 * down, and the related FC-NVME Association ID and Connection IDs 1365 * become invalid. 1366 * 1367 * The behavior of the fc-nvme initiator is such that its 1368 * understanding of the association and connections will implicitly 1369 * be torn down. The action is implicit as it may be due to a loss of 1370 * connectivity with the fc-nvme target, so you may never get a 1371 * response even if you tried. As such, the action of this routine 1372 * is to asynchronously send the LS, ignore any results of the LS, and 1373 * continue on with terminating the association. If the fc-nvme target 1374 * is present and receives the LS, it too can tear down. 1375 */ 1376 static void 1377 nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl) 1378 { 1379 struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst; 1380 struct fcnvme_ls_disconnect_assoc_acc *discon_acc; 1381 struct nvmefc_ls_req_op *lsop; 1382 struct nvmefc_ls_req *lsreq; 1383 int ret; 1384 1385 lsop = kzalloc((sizeof(*lsop) + 1386 sizeof(*discon_rqst) + sizeof(*discon_acc) + 1387 ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL); 1388 if (!lsop) { 1389 dev_info(ctrl->ctrl.device, 1390 "NVME-FC{%d}: send Disconnect Association " 1391 "failed: ENOMEM\n", 1392 ctrl->cnum); 1393 return; 1394 } 1395 1396 discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1]; 1397 discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1]; 1398 lsreq = &lsop->ls_req; 1399 if (ctrl->lport->ops->lsrqst_priv_sz) 1400 lsreq->private = (void *)&discon_acc[1]; 1401 else 1402 lsreq->private = NULL; 1403 1404 nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc, 1405 ctrl->association_id); 1406 1407 ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop, 1408 nvme_fc_disconnect_assoc_done); 1409 if (ret) 1410 kfree(lsop); 1411 } 1412 1413 static void 1414 nvme_fc_xmt_ls_rsp_free(struct nvmefc_ls_rcv_op *lsop) 1415 { 1416 struct nvme_fc_rport *rport = lsop->rport; 1417 struct nvme_fc_lport *lport = rport->lport; 1418 unsigned long flags; 1419 1420 spin_lock_irqsave(&rport->lock, flags); 1421 list_del(&lsop->lsrcv_list); 1422 spin_unlock_irqrestore(&rport->lock, flags); 1423 1424 fc_dma_sync_single_for_cpu(lport->dev, lsop->rspdma, 1425 sizeof(*lsop->rspbuf), DMA_TO_DEVICE); 1426 fc_dma_unmap_single(lport->dev, lsop->rspdma, 1427 sizeof(*lsop->rspbuf), DMA_TO_DEVICE); 1428 1429 kfree(lsop->rspbuf); 1430 kfree(lsop->rqstbuf); 1431 kfree(lsop); 1432 1433 nvme_fc_rport_put(rport); 1434 } 1435 1436 static void 1437 nvme_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp) 1438 { 1439 struct nvmefc_ls_rcv_op *lsop = lsrsp->nvme_fc_private; 1440 1441 nvme_fc_xmt_ls_rsp_free(lsop); 1442 } 1443 1444 static void 1445 nvme_fc_xmt_ls_rsp(struct nvmefc_ls_rcv_op *lsop) 1446 { 1447 struct nvme_fc_rport *rport = lsop->rport; 1448 struct nvme_fc_lport *lport = rport->lport; 1449 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0; 1450 int ret; 1451 1452 fc_dma_sync_single_for_device(lport->dev, lsop->rspdma, 1453 sizeof(*lsop->rspbuf), DMA_TO_DEVICE); 1454 1455 ret = lport->ops->xmt_ls_rsp(&lport->localport, &rport->remoteport, 1456 lsop->lsrsp); 1457 if (ret) { 1458 dev_warn(lport->dev, 1459 "LLDD rejected LS RSP xmt: LS %d status %d\n", 1460 w0->ls_cmd, ret); 1461 nvme_fc_xmt_ls_rsp_free(lsop); 1462 return; 1463 } 1464 } 1465 1466 static struct nvme_fc_ctrl * 1467 nvme_fc_match_disconn_ls(struct nvme_fc_rport *rport, 1468 struct nvmefc_ls_rcv_op *lsop) 1469 { 1470 struct fcnvme_ls_disconnect_assoc_rqst *rqst = 1471 &lsop->rqstbuf->rq_dis_assoc; 1472 struct nvme_fc_ctrl *ctrl, *tmp, *ret = NULL; 1473 struct nvmefc_ls_rcv_op *oldls = NULL; 1474 u64 association_id = be64_to_cpu(rqst->associd.association_id); 1475 unsigned long flags; 1476 1477 spin_lock_irqsave(&rport->lock, flags); 1478 1479 list_for_each_entry_safe(ctrl, tmp, &rport->ctrl_list, ctrl_list) { 1480 if (!nvme_fc_ctrl_get(ctrl)) 1481 continue; 1482 spin_lock(&ctrl->lock); 1483 if (association_id == ctrl->association_id) { 1484 oldls = ctrl->rcv_disconn; 1485 ctrl->rcv_disconn = lsop; 1486 ret = ctrl; 1487 } 1488 spin_unlock(&ctrl->lock); 1489 if (ret) 1490 /* leave the ctrl get reference */ 1491 break; 1492 spin_unlock_irqrestore(&rport->lock, flags); 1493 nvme_fc_ctrl_put(ctrl); 1494 spin_lock_irqsave(&rport->lock, flags); 1495 } 1496 1497 spin_unlock_irqrestore(&rport->lock, flags); 1498 1499 /* transmit a response for anything that was pending */ 1500 if (oldls) { 1501 dev_info(rport->lport->dev, 1502 "NVME-FC{%d}: Multiple Disconnect Association " 1503 "LS's received\n", ctrl->cnum); 1504 /* overwrite good response with bogus failure */ 1505 oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf, 1506 sizeof(*oldls->rspbuf), 1507 rqst->w0.ls_cmd, 1508 FCNVME_RJT_RC_UNAB, 1509 FCNVME_RJT_EXP_NONE, 0); 1510 nvme_fc_xmt_ls_rsp(oldls); 1511 } 1512 1513 return ret; 1514 } 1515 1516 /* 1517 * returns true to mean LS handled and ls_rsp can be sent 1518 * returns false to defer ls_rsp xmt (will be done as part of 1519 * association termination) 1520 */ 1521 static bool 1522 nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop) 1523 { 1524 struct nvme_fc_rport *rport = lsop->rport; 1525 struct fcnvme_ls_disconnect_assoc_rqst *rqst = 1526 &lsop->rqstbuf->rq_dis_assoc; 1527 struct fcnvme_ls_disconnect_assoc_acc *acc = 1528 &lsop->rspbuf->rsp_dis_assoc; 1529 struct nvme_fc_ctrl *ctrl = NULL; 1530 int ret = 0; 1531 1532 memset(acc, 0, sizeof(*acc)); 1533 1534 ret = nvmefc_vldt_lsreq_discon_assoc(lsop->rqstdatalen, rqst); 1535 if (!ret) { 1536 /* match an active association */ 1537 ctrl = nvme_fc_match_disconn_ls(rport, lsop); 1538 if (!ctrl) 1539 ret = VERR_NO_ASSOC; 1540 } 1541 1542 if (ret) { 1543 dev_info(rport->lport->dev, 1544 "Disconnect LS failed: %s\n", 1545 validation_errors[ret]); 1546 lsop->lsrsp->rsplen = nvme_fc_format_rjt(acc, 1547 sizeof(*acc), rqst->w0.ls_cmd, 1548 (ret == VERR_NO_ASSOC) ? 1549 FCNVME_RJT_RC_INV_ASSOC : 1550 FCNVME_RJT_RC_LOGIC, 1551 FCNVME_RJT_EXP_NONE, 0); 1552 return true; 1553 } 1554 1555 /* format an ACCept response */ 1556 1557 lsop->lsrsp->rsplen = sizeof(*acc); 1558 1559 nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC, 1560 fcnvme_lsdesc_len( 1561 sizeof(struct fcnvme_ls_disconnect_assoc_acc)), 1562 FCNVME_LS_DISCONNECT_ASSOC); 1563 1564 /* 1565 * the transmit of the response will occur after the exchanges 1566 * for the association have been ABTS'd by 1567 * nvme_fc_delete_association(). 1568 */ 1569 1570 /* fail the association */ 1571 nvme_fc_error_recovery(ctrl, "Disconnect Association LS received"); 1572 1573 /* release the reference taken by nvme_fc_match_disconn_ls() */ 1574 nvme_fc_ctrl_put(ctrl); 1575 1576 return false; 1577 } 1578 1579 /* 1580 * Actual Processing routine for received FC-NVME LS Requests from the LLD 1581 * returns true if a response should be sent afterward, false if rsp will 1582 * be sent asynchronously. 1583 */ 1584 static bool 1585 nvme_fc_handle_ls_rqst(struct nvmefc_ls_rcv_op *lsop) 1586 { 1587 struct fcnvme_ls_rqst_w0 *w0 = &lsop->rqstbuf->w0; 1588 bool ret = true; 1589 1590 lsop->lsrsp->nvme_fc_private = lsop; 1591 lsop->lsrsp->rspbuf = lsop->rspbuf; 1592 lsop->lsrsp->rspdma = lsop->rspdma; 1593 lsop->lsrsp->done = nvme_fc_xmt_ls_rsp_done; 1594 /* Be preventative. handlers will later set to valid length */ 1595 lsop->lsrsp->rsplen = 0; 1596 1597 /* 1598 * handlers: 1599 * parse request input, execute the request, and format the 1600 * LS response 1601 */ 1602 switch (w0->ls_cmd) { 1603 case FCNVME_LS_DISCONNECT_ASSOC: 1604 ret = nvme_fc_ls_disconnect_assoc(lsop); 1605 break; 1606 case FCNVME_LS_DISCONNECT_CONN: 1607 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf, 1608 sizeof(*lsop->rspbuf), w0->ls_cmd, 1609 FCNVME_RJT_RC_UNSUP, FCNVME_RJT_EXP_NONE, 0); 1610 break; 1611 case FCNVME_LS_CREATE_ASSOCIATION: 1612 case FCNVME_LS_CREATE_CONNECTION: 1613 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf, 1614 sizeof(*lsop->rspbuf), w0->ls_cmd, 1615 FCNVME_RJT_RC_LOGIC, FCNVME_RJT_EXP_NONE, 0); 1616 break; 1617 default: 1618 lsop->lsrsp->rsplen = nvme_fc_format_rjt(lsop->rspbuf, 1619 sizeof(*lsop->rspbuf), w0->ls_cmd, 1620 FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0); 1621 break; 1622 } 1623 1624 return(ret); 1625 } 1626 1627 static void 1628 nvme_fc_handle_ls_rqst_work(struct work_struct *work) 1629 { 1630 struct nvme_fc_rport *rport = 1631 container_of(work, struct nvme_fc_rport, lsrcv_work); 1632 struct fcnvme_ls_rqst_w0 *w0; 1633 struct nvmefc_ls_rcv_op *lsop; 1634 unsigned long flags; 1635 bool sendrsp; 1636 1637 restart: 1638 sendrsp = true; 1639 spin_lock_irqsave(&rport->lock, flags); 1640 list_for_each_entry(lsop, &rport->ls_rcv_list, lsrcv_list) { 1641 if (lsop->handled) 1642 continue; 1643 1644 lsop->handled = true; 1645 if (rport->remoteport.port_state == FC_OBJSTATE_ONLINE) { 1646 spin_unlock_irqrestore(&rport->lock, flags); 1647 sendrsp = nvme_fc_handle_ls_rqst(lsop); 1648 } else { 1649 spin_unlock_irqrestore(&rport->lock, flags); 1650 w0 = &lsop->rqstbuf->w0; 1651 lsop->lsrsp->rsplen = nvme_fc_format_rjt( 1652 lsop->rspbuf, 1653 sizeof(*lsop->rspbuf), 1654 w0->ls_cmd, 1655 FCNVME_RJT_RC_UNAB, 1656 FCNVME_RJT_EXP_NONE, 0); 1657 } 1658 if (sendrsp) 1659 nvme_fc_xmt_ls_rsp(lsop); 1660 goto restart; 1661 } 1662 spin_unlock_irqrestore(&rport->lock, flags); 1663 } 1664 1665 static 1666 void nvme_fc_rcv_ls_req_err_msg(struct nvme_fc_lport *lport, 1667 struct fcnvme_ls_rqst_w0 *w0) 1668 { 1669 dev_info(lport->dev, "RCV %s LS failed: No memory\n", 1670 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ? 1671 nvmefc_ls_names[w0->ls_cmd] : ""); 1672 } 1673 1674 /** 1675 * nvme_fc_rcv_ls_req - transport entry point called by an LLDD 1676 * upon the reception of a NVME LS request. 1677 * 1678 * The nvme-fc layer will copy payload to an internal structure for 1679 * processing. As such, upon completion of the routine, the LLDD may 1680 * immediately free/reuse the LS request buffer passed in the call. 1681 * 1682 * If this routine returns error, the LLDD should abort the exchange. 1683 * 1684 * @portptr: pointer to the (registered) remote port that the LS 1685 * was received from. The remoteport is associated with 1686 * a specific localport. 1687 * @lsrsp: pointer to a nvmefc_ls_rsp response structure to be 1688 * used to reference the exchange corresponding to the LS 1689 * when issuing an ls response. 1690 * @lsreqbuf: pointer to the buffer containing the LS Request 1691 * @lsreqbuf_len: length, in bytes, of the received LS request 1692 */ 1693 int 1694 nvme_fc_rcv_ls_req(struct nvme_fc_remote_port *portptr, 1695 struct nvmefc_ls_rsp *lsrsp, 1696 void *lsreqbuf, u32 lsreqbuf_len) 1697 { 1698 struct nvme_fc_rport *rport = remoteport_to_rport(portptr); 1699 struct nvme_fc_lport *lport = rport->lport; 1700 struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf; 1701 struct nvmefc_ls_rcv_op *lsop; 1702 unsigned long flags; 1703 int ret; 1704 1705 nvme_fc_rport_get(rport); 1706 1707 /* validate there's a routine to transmit a response */ 1708 if (!lport->ops->xmt_ls_rsp) { 1709 dev_info(lport->dev, 1710 "RCV %s LS failed: no LLDD xmt_ls_rsp\n", 1711 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ? 1712 nvmefc_ls_names[w0->ls_cmd] : ""); 1713 ret = -EINVAL; 1714 goto out_put; 1715 } 1716 1717 if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) { 1718 dev_info(lport->dev, 1719 "RCV %s LS failed: payload too large\n", 1720 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ? 1721 nvmefc_ls_names[w0->ls_cmd] : ""); 1722 ret = -E2BIG; 1723 goto out_put; 1724 } 1725 1726 lsop = kzalloc_obj(*lsop); 1727 if (!lsop) { 1728 nvme_fc_rcv_ls_req_err_msg(lport, w0); 1729 ret = -ENOMEM; 1730 goto out_put; 1731 } 1732 1733 lsop->rqstbuf = kzalloc_obj(*lsop->rqstbuf); 1734 lsop->rspbuf = kzalloc_obj(*lsop->rspbuf); 1735 if (!lsop->rqstbuf || !lsop->rspbuf) { 1736 nvme_fc_rcv_ls_req_err_msg(lport, w0); 1737 ret = -ENOMEM; 1738 goto out_free; 1739 } 1740 1741 lsop->rspdma = fc_dma_map_single(lport->dev, lsop->rspbuf, 1742 sizeof(*lsop->rspbuf), 1743 DMA_TO_DEVICE); 1744 if (fc_dma_mapping_error(lport->dev, lsop->rspdma)) { 1745 dev_info(lport->dev, 1746 "RCV %s LS failed: DMA mapping failure\n", 1747 (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ? 1748 nvmefc_ls_names[w0->ls_cmd] : ""); 1749 ret = -EFAULT; 1750 goto out_free; 1751 } 1752 1753 lsop->rport = rport; 1754 lsop->lsrsp = lsrsp; 1755 1756 memcpy(lsop->rqstbuf, lsreqbuf, lsreqbuf_len); 1757 lsop->rqstdatalen = lsreqbuf_len; 1758 1759 spin_lock_irqsave(&rport->lock, flags); 1760 if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) { 1761 spin_unlock_irqrestore(&rport->lock, flags); 1762 ret = -ENOTCONN; 1763 goto out_unmap; 1764 } 1765 list_add_tail(&lsop->lsrcv_list, &rport->ls_rcv_list); 1766 spin_unlock_irqrestore(&rport->lock, flags); 1767 1768 schedule_work(&rport->lsrcv_work); 1769 1770 return 0; 1771 1772 out_unmap: 1773 fc_dma_unmap_single(lport->dev, lsop->rspdma, 1774 sizeof(*lsop->rspbuf), DMA_TO_DEVICE); 1775 out_free: 1776 kfree(lsop->rspbuf); 1777 kfree(lsop->rqstbuf); 1778 kfree(lsop); 1779 out_put: 1780 nvme_fc_rport_put(rport); 1781 return ret; 1782 } 1783 EXPORT_SYMBOL_GPL(nvme_fc_rcv_ls_req); 1784 1785 1786 /* *********************** NVME Ctrl Routines **************************** */ 1787 1788 static void 1789 __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl, 1790 struct nvme_fc_fcp_op *op) 1791 { 1792 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma, 1793 sizeof(op->rsp_iu), DMA_FROM_DEVICE); 1794 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma, 1795 sizeof(op->cmd_iu), DMA_TO_DEVICE); 1796 1797 atomic_set(&op->state, FCPOP_STATE_UNINIT); 1798 } 1799 1800 static void 1801 nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq, 1802 unsigned int hctx_idx) 1803 { 1804 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1805 1806 return __nvme_fc_exit_request(to_fc_ctrl(set->driver_data), op); 1807 } 1808 1809 static int 1810 __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op) 1811 { 1812 unsigned long flags; 1813 int opstate; 1814 1815 spin_lock_irqsave(&ctrl->lock, flags); 1816 opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED); 1817 if (opstate != FCPOP_STATE_ACTIVE) 1818 atomic_set(&op->state, opstate); 1819 else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) { 1820 op->flags |= FCOP_FLAGS_TERMIO; 1821 ctrl->iocnt++; 1822 } 1823 spin_unlock_irqrestore(&ctrl->lock, flags); 1824 1825 if (opstate != FCPOP_STATE_ACTIVE) 1826 return -ECANCELED; 1827 1828 ctrl->lport->ops->fcp_abort(&ctrl->lport->localport, 1829 &ctrl->rport->remoteport, 1830 op->queue->lldd_handle, 1831 &op->fcp_req); 1832 1833 return 0; 1834 } 1835 1836 static void 1837 nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl) 1838 { 1839 struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops; 1840 int i; 1841 1842 /* ensure we've initialized the ops once */ 1843 if (!(aen_op->flags & FCOP_FLAGS_AEN)) 1844 return; 1845 1846 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) 1847 __nvme_fc_abort_op(ctrl, aen_op); 1848 } 1849 1850 static inline void 1851 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl, 1852 struct nvme_fc_fcp_op *op, int opstate) 1853 { 1854 unsigned long flags; 1855 1856 if (opstate == FCPOP_STATE_ABORTED) { 1857 spin_lock_irqsave(&ctrl->lock, flags); 1858 if (test_bit(FCCTRL_TERMIO, &ctrl->flags) && 1859 op->flags & FCOP_FLAGS_TERMIO) { 1860 if (!--ctrl->iocnt) 1861 wake_up(&ctrl->ioabort_wait); 1862 } 1863 spin_unlock_irqrestore(&ctrl->lock, flags); 1864 } 1865 } 1866 1867 static void 1868 nvme_fc_ctrl_ioerr_work(struct work_struct *work) 1869 { 1870 struct nvme_fc_ctrl *ctrl = 1871 container_of(work, struct nvme_fc_ctrl, ioerr_work); 1872 1873 nvme_fc_error_recovery(ctrl, "transport detected io error"); 1874 } 1875 1876 /* 1877 * nvme_fc_io_getuuid - Routine called to get the appid field 1878 * associated with request by the lldd 1879 * @req:IO request from nvme fc to driver 1880 * Returns: UUID if there is an appid associated with VM or 1881 * NULL if the user/libvirt has not set the appid to VM 1882 */ 1883 char *nvme_fc_io_getuuid(struct nvmefc_fcp_req *req) 1884 { 1885 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req); 1886 struct request *rq = op->rq; 1887 1888 if (!IS_ENABLED(CONFIG_BLK_CGROUP_FC_APPID) || !rq || !rq->bio) 1889 return NULL; 1890 return blkcg_get_fc_appid(rq->bio); 1891 } 1892 EXPORT_SYMBOL_GPL(nvme_fc_io_getuuid); 1893 1894 static void 1895 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req) 1896 { 1897 struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req); 1898 struct request *rq = op->rq; 1899 struct nvmefc_fcp_req *freq = &op->fcp_req; 1900 struct nvme_fc_ctrl *ctrl = op->ctrl; 1901 struct nvme_fc_queue *queue = op->queue; 1902 struct nvme_completion *cqe = &op->rsp_iu.cqe; 1903 struct nvme_command *sqe = &op->cmd_iu.sqe; 1904 __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1); 1905 union nvme_result result; 1906 bool terminate_assoc = true; 1907 int opstate; 1908 1909 /* 1910 * WARNING: 1911 * The current linux implementation of a nvme controller 1912 * allocates a single tag set for all io queues and sizes 1913 * the io queues to fully hold all possible tags. Thus, the 1914 * implementation does not reference or care about the sqhd 1915 * value as it never needs to use the sqhd/sqtail pointers 1916 * for submission pacing. 1917 * 1918 * This affects the FC-NVME implementation in two ways: 1919 * 1) As the value doesn't matter, we don't need to waste 1920 * cycles extracting it from ERSPs and stamping it in the 1921 * cases where the transport fabricates CQEs on successful 1922 * completions. 1923 * 2) The FC-NVME implementation requires that delivery of 1924 * ERSP completions are to go back to the nvme layer in order 1925 * relative to the rsn, such that the sqhd value will always 1926 * be "in order" for the nvme layer. As the nvme layer in 1927 * linux doesn't care about sqhd, there's no need to return 1928 * them in order. 1929 * 1930 * Additionally: 1931 * As the core nvme layer in linux currently does not look at 1932 * every field in the cqe - in cases where the FC transport must 1933 * fabricate a CQE, the following fields will not be set as they 1934 * are not referenced: 1935 * cqe.sqid, cqe.sqhd, cqe.command_id 1936 * 1937 * Failure or error of an individual i/o, in a transport 1938 * detected fashion unrelated to the nvme completion status, 1939 * potentially cause the initiator and target sides to get out 1940 * of sync on SQ head/tail (aka outstanding io count allowed). 1941 * Per FC-NVME spec, failure of an individual command requires 1942 * the connection to be terminated, which in turn requires the 1943 * association to be terminated. 1944 */ 1945 1946 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE); 1947 1948 fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma, 1949 sizeof(op->rsp_iu), DMA_FROM_DEVICE); 1950 1951 if (opstate == FCPOP_STATE_ABORTED) 1952 status = cpu_to_le16(NVME_SC_HOST_ABORTED_CMD << 1); 1953 else if (freq->status) { 1954 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1); 1955 dev_info(ctrl->ctrl.device, 1956 "NVME-FC{%d}: io failed due to lldd error %d\n", 1957 ctrl->cnum, freq->status); 1958 } 1959 1960 /* 1961 * For the linux implementation, if we have an unsuccessful 1962 * status, the blk-mq layer can typically be called with the 1963 * non-zero status and the content of the cqe isn't important. 1964 */ 1965 if (status) 1966 goto done; 1967 1968 /* 1969 * command completed successfully relative to the wire 1970 * protocol. However, validate anything received and 1971 * extract the status and result from the cqe (create it 1972 * where necessary). 1973 */ 1974 1975 switch (freq->rcv_rsplen) { 1976 1977 case 0: 1978 case NVME_FC_SIZEOF_ZEROS_RSP: 1979 /* 1980 * No response payload or 12 bytes of payload (which 1981 * should all be zeros) are considered successful and 1982 * no payload in the CQE by the transport. 1983 */ 1984 if (freq->transferred_length != 1985 be32_to_cpu(op->cmd_iu.data_len)) { 1986 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1); 1987 dev_info(ctrl->ctrl.device, 1988 "NVME-FC{%d}: io failed due to bad transfer " 1989 "length: %d vs expected %d\n", 1990 ctrl->cnum, freq->transferred_length, 1991 be32_to_cpu(op->cmd_iu.data_len)); 1992 goto done; 1993 } 1994 result.u64 = 0; 1995 break; 1996 1997 case sizeof(struct nvme_fc_ersp_iu): 1998 /* 1999 * The ERSP IU contains a full completion with CQE. 2000 * Validate ERSP IU and look at cqe. 2001 */ 2002 if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) != 2003 (freq->rcv_rsplen / 4) || 2004 be32_to_cpu(op->rsp_iu.xfrd_len) != 2005 freq->transferred_length || 2006 op->rsp_iu.ersp_result || 2007 sqe->common.command_id != cqe->command_id)) { 2008 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1); 2009 dev_info(ctrl->ctrl.device, 2010 "NVME-FC{%d}: io failed due to bad NVMe_ERSP: " 2011 "iu len %d, xfr len %d vs %d, status code " 2012 "%d, cmdid %d vs %d\n", 2013 ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len), 2014 be32_to_cpu(op->rsp_iu.xfrd_len), 2015 freq->transferred_length, 2016 op->rsp_iu.ersp_result, 2017 sqe->common.command_id, 2018 cqe->command_id); 2019 goto done; 2020 } 2021 result = cqe->result; 2022 status = cqe->status; 2023 break; 2024 2025 default: 2026 status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1); 2027 dev_info(ctrl->ctrl.device, 2028 "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu " 2029 "len %d\n", 2030 ctrl->cnum, freq->rcv_rsplen); 2031 goto done; 2032 } 2033 2034 terminate_assoc = false; 2035 2036 done: 2037 if (op->flags & FCOP_FLAGS_AEN) { 2038 nvme_complete_async_event(&queue->ctrl->ctrl, status, &result); 2039 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate); 2040 atomic_set(&op->state, FCPOP_STATE_IDLE); 2041 op->flags = FCOP_FLAGS_AEN; /* clear other flags */ 2042 nvme_fc_ctrl_put(ctrl); 2043 goto check_error; 2044 } 2045 2046 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate); 2047 if (!nvme_try_complete_req(rq, status, result)) 2048 nvme_fc_complete_rq(rq); 2049 2050 check_error: 2051 if (terminate_assoc && 2052 nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_RESETTING) 2053 queue_work(nvme_reset_wq, &ctrl->ioerr_work); 2054 } 2055 2056 static int 2057 __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl, 2058 struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op, 2059 struct request *rq, u32 rqno) 2060 { 2061 struct nvme_fcp_op_w_sgl *op_w_sgl = 2062 container_of(op, typeof(*op_w_sgl), op); 2063 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 2064 int ret = 0; 2065 2066 memset(op, 0, sizeof(*op)); 2067 op->fcp_req.cmdaddr = &op->cmd_iu; 2068 op->fcp_req.cmdlen = sizeof(op->cmd_iu); 2069 op->fcp_req.rspaddr = &op->rsp_iu; 2070 op->fcp_req.rsplen = sizeof(op->rsp_iu); 2071 op->fcp_req.done = nvme_fc_fcpio_done; 2072 op->ctrl = ctrl; 2073 op->queue = queue; 2074 op->rq = rq; 2075 op->rqno = rqno; 2076 2077 cmdiu->format_id = NVME_CMD_FORMAT_ID; 2078 cmdiu->fc_id = NVME_CMD_FC_ID; 2079 cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32)); 2080 if (queue->qnum) 2081 cmdiu->rsv_cat = fccmnd_set_cat_css(0, 2082 (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT)); 2083 else 2084 cmdiu->rsv_cat = fccmnd_set_cat_admin(0); 2085 2086 op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev, 2087 &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE); 2088 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) { 2089 dev_err(ctrl->dev, 2090 "FCP Op failed - cmdiu dma mapping failed.\n"); 2091 ret = -EFAULT; 2092 goto out_on_error; 2093 } 2094 2095 op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev, 2096 &op->rsp_iu, sizeof(op->rsp_iu), 2097 DMA_FROM_DEVICE); 2098 if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) { 2099 dev_err(ctrl->dev, 2100 "FCP Op failed - rspiu dma mapping failed.\n"); 2101 ret = -EFAULT; 2102 goto out_unmap; 2103 } 2104 2105 atomic_set(&op->state, FCPOP_STATE_IDLE); 2106 return 0; 2107 2108 out_unmap: 2109 fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma, 2110 sizeof(op->cmd_iu), DMA_TO_DEVICE); 2111 out_on_error: 2112 return ret; 2113 } 2114 2115 static int 2116 nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq, 2117 unsigned int hctx_idx, int numa_node) 2118 { 2119 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data); 2120 struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq); 2121 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0; 2122 struct nvme_fc_queue *queue = &ctrl->queues[queue_idx]; 2123 int res; 2124 2125 res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++); 2126 if (res) 2127 return res; 2128 op->op.fcp_req.first_sgl = op->sgl; 2129 op->op.fcp_req.private = &op->priv[0]; 2130 nvme_req(rq)->ctrl = &ctrl->ctrl; 2131 nvme_req(rq)->cmd = &op->op.cmd_iu.sqe; 2132 return res; 2133 } 2134 2135 static int 2136 nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl) 2137 { 2138 struct nvme_fc_fcp_op *aen_op; 2139 struct nvme_fc_cmd_iu *cmdiu; 2140 struct nvme_command *sqe; 2141 void *private = NULL; 2142 int i, ret; 2143 2144 aen_op = ctrl->aen_ops; 2145 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) { 2146 if (ctrl->lport->ops->fcprqst_priv_sz) { 2147 private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz, 2148 GFP_KERNEL); 2149 if (!private) 2150 return -ENOMEM; 2151 } 2152 2153 cmdiu = &aen_op->cmd_iu; 2154 sqe = &cmdiu->sqe; 2155 ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0], 2156 aen_op, (struct request *)NULL, 2157 (NVME_AQ_BLK_MQ_DEPTH + i)); 2158 if (ret) { 2159 kfree(private); 2160 return ret; 2161 } 2162 2163 aen_op->flags = FCOP_FLAGS_AEN; 2164 aen_op->fcp_req.private = private; 2165 2166 memset(sqe, 0, sizeof(*sqe)); 2167 sqe->common.opcode = nvme_admin_async_event; 2168 /* Note: core layer may overwrite the sqe.command_id value */ 2169 sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i; 2170 } 2171 return 0; 2172 } 2173 2174 static void 2175 nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl) 2176 { 2177 struct nvme_fc_fcp_op *aen_op; 2178 int i; 2179 2180 cancel_work_sync(&ctrl->ctrl.async_event_work); 2181 aen_op = ctrl->aen_ops; 2182 for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) { 2183 __nvme_fc_exit_request(ctrl, aen_op); 2184 2185 kfree(aen_op->fcp_req.private); 2186 aen_op->fcp_req.private = NULL; 2187 } 2188 } 2189 2190 static inline int 2191 __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int qidx) 2192 { 2193 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(data); 2194 struct nvme_fc_queue *queue = &ctrl->queues[qidx]; 2195 2196 hctx->driver_data = queue; 2197 queue->hctx = hctx; 2198 return 0; 2199 } 2200 2201 static int 2202 nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, unsigned int hctx_idx) 2203 { 2204 return __nvme_fc_init_hctx(hctx, data, hctx_idx + 1); 2205 } 2206 2207 static int 2208 nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, 2209 unsigned int hctx_idx) 2210 { 2211 return __nvme_fc_init_hctx(hctx, data, hctx_idx); 2212 } 2213 2214 static void 2215 nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx) 2216 { 2217 struct nvme_fc_queue *queue; 2218 2219 queue = &ctrl->queues[idx]; 2220 memset(queue, 0, sizeof(*queue)); 2221 queue->ctrl = ctrl; 2222 queue->qnum = idx; 2223 atomic_set(&queue->csn, 0); 2224 queue->dev = ctrl->dev; 2225 2226 if (idx > 0) 2227 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; 2228 else 2229 queue->cmnd_capsule_len = sizeof(struct nvme_command); 2230 2231 /* 2232 * Considered whether we should allocate buffers for all SQEs 2233 * and CQEs and dma map them - mapping their respective entries 2234 * into the request structures (kernel vm addr and dma address) 2235 * thus the driver could use the buffers/mappings directly. 2236 * It only makes sense if the LLDD would use them for its 2237 * messaging api. It's very unlikely most adapter api's would use 2238 * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload 2239 * structures were used instead. 2240 */ 2241 } 2242 2243 /* 2244 * This routine terminates a queue at the transport level. 2245 * The transport has already ensured that all outstanding ios on 2246 * the queue have been terminated. 2247 * The transport will send a Disconnect LS request to terminate 2248 * the queue's connection. Termination of the admin queue will also 2249 * terminate the association at the target. 2250 */ 2251 static void 2252 nvme_fc_free_queue(struct nvme_fc_queue *queue) 2253 { 2254 if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags)) 2255 return; 2256 2257 clear_bit(NVME_FC_Q_LIVE, &queue->flags); 2258 /* 2259 * Current implementation never disconnects a single queue. 2260 * It always terminates a whole association. So there is never 2261 * a disconnect(queue) LS sent to the target. 2262 */ 2263 2264 queue->connection_id = 0; 2265 atomic_set(&queue->csn, 0); 2266 } 2267 2268 static void 2269 __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl, 2270 struct nvme_fc_queue *queue, unsigned int qidx) 2271 { 2272 if (ctrl->lport->ops->delete_queue) 2273 ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx, 2274 queue->lldd_handle); 2275 queue->lldd_handle = NULL; 2276 } 2277 2278 static void 2279 nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl) 2280 { 2281 int i; 2282 2283 for (i = 1; i < ctrl->ctrl.queue_count; i++) 2284 nvme_fc_free_queue(&ctrl->queues[i]); 2285 } 2286 2287 static int 2288 __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl, 2289 struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize) 2290 { 2291 int ret = 0; 2292 2293 queue->lldd_handle = NULL; 2294 if (ctrl->lport->ops->create_queue) 2295 ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport, 2296 qidx, qsize, &queue->lldd_handle); 2297 2298 return ret; 2299 } 2300 2301 static void 2302 nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl) 2303 { 2304 struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1]; 2305 int i; 2306 2307 for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--) 2308 __nvme_fc_delete_hw_queue(ctrl, queue, i); 2309 } 2310 2311 static int 2312 nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize) 2313 { 2314 struct nvme_fc_queue *queue = &ctrl->queues[1]; 2315 int i, ret; 2316 2317 for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) { 2318 ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize); 2319 if (ret) 2320 goto delete_queues; 2321 } 2322 2323 return 0; 2324 2325 delete_queues: 2326 for (--i; i > 0; i--) 2327 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i); 2328 return ret; 2329 } 2330 2331 static int 2332 nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize) 2333 { 2334 int i, ret = 0; 2335 2336 for (i = 1; i < ctrl->ctrl.queue_count; i++) { 2337 ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize, 2338 (qsize / 5)); 2339 if (ret) 2340 break; 2341 ret = nvmf_connect_io_queue(&ctrl->ctrl, i); 2342 if (ret) 2343 break; 2344 2345 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags); 2346 } 2347 2348 return ret; 2349 } 2350 2351 static void 2352 nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl) 2353 { 2354 int i; 2355 2356 for (i = 1; i < ctrl->ctrl.queue_count; i++) 2357 nvme_fc_init_queue(ctrl, i); 2358 } 2359 2360 static void 2361 nvme_fc_ctrl_free(struct kref *ref) 2362 { 2363 struct nvme_fc_ctrl *ctrl = 2364 container_of(ref, struct nvme_fc_ctrl, ref); 2365 unsigned long flags; 2366 bool owns_opts; 2367 2368 /* 2369 * Presence on the rport list means nvme_fc_init_ctrl() completed, 2370 * and with it ownership of the fabrics options passed to it. If it 2371 * failed instead, the options still belong to nvmf_create_ctrl(). 2372 */ 2373 spin_lock_irqsave(&ctrl->rport->lock, flags); 2374 owns_opts = !list_empty(&ctrl->ctrl_list); 2375 list_del(&ctrl->ctrl_list); 2376 spin_unlock_irqrestore(&ctrl->rport->lock, flags); 2377 2378 kfree(ctrl->queues); 2379 2380 put_device(ctrl->dev); 2381 nvme_fc_rport_put(ctrl->rport); 2382 2383 ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum); 2384 if (owns_opts) 2385 nvmf_free_options(ctrl->ctrl.opts); 2386 kfree(ctrl); 2387 } 2388 2389 static void 2390 nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl) 2391 { 2392 kref_put(&ctrl->ref, nvme_fc_ctrl_free); 2393 } 2394 2395 static int 2396 nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl) 2397 { 2398 return kref_get_unless_zero(&ctrl->ref); 2399 } 2400 2401 /* 2402 * All accesses from nvme core layer done - can now free the 2403 * controller. Called after last nvme_put_ctrl() call 2404 */ 2405 static void 2406 nvme_fc_free_ctrl(struct nvme_ctrl *nctrl) 2407 { 2408 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 2409 2410 WARN_ON(nctrl != &ctrl->ctrl); 2411 2412 nvme_fc_ctrl_put(ctrl); 2413 } 2414 2415 /* 2416 * This routine is used by the transport when it needs to find active 2417 * io on a queue that is to be terminated. The transport uses 2418 * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke 2419 * this routine to kill them on a 1 by 1 basis. 2420 * 2421 * As FC allocates FC exchange for each io, the transport must contact 2422 * the LLDD to terminate the exchange, thus releasing the FC exchange. 2423 * After terminating the exchange the LLDD will call the transport's 2424 * normal io done path for the request, but it will have an aborted 2425 * status. The done path will return the io request back to the block 2426 * layer with an error status. 2427 */ 2428 static bool nvme_fc_terminate_exchange(struct request *req, void *data) 2429 { 2430 struct nvme_ctrl *nctrl = data; 2431 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 2432 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req); 2433 2434 op->nreq.flags |= NVME_REQ_CANCELLED; 2435 __nvme_fc_abort_op(ctrl, op); 2436 return true; 2437 } 2438 2439 /* 2440 * This routine runs through all outstanding commands on the association 2441 * and aborts them. This routine is typically called by the 2442 * delete_association routine. It is also called due to an error during 2443 * reconnect. In that scenario, it is most likely a command that initializes 2444 * the controller, including fabric Connect commands on io queues, that 2445 * may have timed out or failed thus the io must be killed for the connect 2446 * thread to see the error. 2447 */ 2448 static void 2449 __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues) 2450 { 2451 int q; 2452 2453 /* 2454 * if aborting io, the queues are no longer good, mark them 2455 * all as not live. 2456 */ 2457 if (ctrl->ctrl.queue_count > 1) { 2458 for (q = 1; q < ctrl->ctrl.queue_count; q++) 2459 clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[q].flags); 2460 } 2461 clear_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags); 2462 2463 /* 2464 * If io queues are present, stop them and terminate all outstanding 2465 * ios on them. As FC allocates FC exchange for each io, the 2466 * transport must contact the LLDD to terminate the exchange, 2467 * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr() 2468 * to tell us what io's are busy and invoke a transport routine 2469 * to kill them with the LLDD. After terminating the exchange 2470 * the LLDD will call the transport's normal io done path, but it 2471 * will have an aborted status. The done path will return the 2472 * io requests back to the block layer as part of normal completions 2473 * (but with error status). 2474 */ 2475 if (ctrl->ctrl.queue_count > 1 && ctrl->ctrl.tagset) { 2476 nvme_quiesce_io_queues(&ctrl->ctrl); 2477 nvme_sync_io_queues(&ctrl->ctrl); 2478 blk_mq_tagset_busy_iter(&ctrl->tag_set, 2479 nvme_fc_terminate_exchange, &ctrl->ctrl); 2480 blk_mq_tagset_wait_completed_request(&ctrl->tag_set); 2481 if (start_queues) 2482 nvme_unquiesce_io_queues(&ctrl->ctrl); 2483 } 2484 2485 /* 2486 * Other transports, which don't have link-level contexts bound 2487 * to sqe's, would try to gracefully shutdown the controller by 2488 * writing the registers for shutdown and polling (call 2489 * nvme_disable_ctrl()). Given a bunch of i/o was potentially 2490 * just aborted and we will wait on those contexts, and given 2491 * there was no indication of how live the controller is on the 2492 * link, don't send more io to create more contexts for the 2493 * shutdown. Let the controller fail via keepalive failure if 2494 * its still present. 2495 */ 2496 2497 /* 2498 * clean up the admin queue. Same thing as above. 2499 */ 2500 nvme_quiesce_admin_queue(&ctrl->ctrl); 2501 blk_sync_queue(ctrl->ctrl.admin_q); 2502 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, 2503 nvme_fc_terminate_exchange, &ctrl->ctrl); 2504 blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set); 2505 if (start_queues) 2506 nvme_unquiesce_admin_queue(&ctrl->ctrl); 2507 } 2508 2509 static void 2510 nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg) 2511 { 2512 enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl); 2513 2514 /* 2515 * if an error (io timeout, etc) while (re)connecting, the remote 2516 * port requested terminating of the association (disconnect_ls) 2517 * or an error (timeout or abort) occurred on an io while creating 2518 * the controller. Abort any ios on the association and let the 2519 * create_association error path resolve things. 2520 */ 2521 if (state == NVME_CTRL_CONNECTING) { 2522 __nvme_fc_abort_outstanding_ios(ctrl, true); 2523 dev_warn(ctrl->ctrl.device, 2524 "NVME-FC{%d}: transport error during (re)connect\n", 2525 ctrl->cnum); 2526 return; 2527 } 2528 2529 /* Otherwise, only proceed if in LIVE state - e.g. on first error */ 2530 if (state != NVME_CTRL_LIVE) 2531 return; 2532 2533 dev_warn(ctrl->ctrl.device, 2534 "NVME-FC{%d}: transport association event: %s\n", 2535 ctrl->cnum, errmsg); 2536 dev_warn(ctrl->ctrl.device, 2537 "NVME-FC{%d}: resetting controller\n", ctrl->cnum); 2538 2539 nvme_reset_ctrl(&ctrl->ctrl); 2540 } 2541 2542 static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq) 2543 { 2544 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 2545 struct nvme_fc_ctrl *ctrl = op->ctrl; 2546 u16 qnum = op->queue->qnum; 2547 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 2548 struct nvme_command *sqe = &cmdiu->sqe; 2549 2550 /* 2551 * Attempt to abort the offending command. Command completion 2552 * will detect the aborted io and will fail the connection. 2553 */ 2554 dev_info(ctrl->ctrl.device, 2555 "NVME-FC{%d.%d}: io timeout: opcode %d fctype %d (%s) w10/11: " 2556 "x%08x/x%08x\n", 2557 ctrl->cnum, qnum, sqe->common.opcode, sqe->fabrics.fctype, 2558 nvme_fabrics_opcode_str(qnum, sqe), 2559 sqe->common.cdw10, sqe->common.cdw11); 2560 if (__nvme_fc_abort_op(ctrl, op)) 2561 nvme_fc_error_recovery(ctrl, "io timeout abort failed"); 2562 2563 /* 2564 * the io abort has been initiated. Have the reset timer 2565 * restarted and the abort completion will complete the io 2566 * shortly. Avoids a synchronous wait while the abort finishes. 2567 */ 2568 return BLK_EH_RESET_TIMER; 2569 } 2570 2571 static int 2572 nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq, 2573 struct nvme_fc_fcp_op *op) 2574 { 2575 struct nvmefc_fcp_req *freq = &op->fcp_req; 2576 int ret; 2577 2578 freq->sg_cnt = 0; 2579 2580 if (!blk_rq_nr_phys_segments(rq)) 2581 return 0; 2582 2583 freq->sg_table.sgl = freq->first_sgl; 2584 ret = sg_alloc_table_chained(&freq->sg_table, 2585 blk_rq_nr_phys_segments(rq), freq->sg_table.sgl, 2586 NVME_INLINE_SG_CNT); 2587 if (ret) 2588 return -ENOMEM; 2589 2590 op->nents = blk_rq_map_sg(rq, freq->sg_table.sgl); 2591 WARN_ON(op->nents > blk_rq_nr_phys_segments(rq)); 2592 freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl, 2593 op->nents, rq_dma_dir(rq)); 2594 if (unlikely(freq->sg_cnt <= 0)) { 2595 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT); 2596 freq->sg_cnt = 0; 2597 return -EFAULT; 2598 } 2599 2600 /* 2601 * TODO: blk_integrity_rq(rq) for DIF 2602 */ 2603 return 0; 2604 } 2605 2606 static void 2607 nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq, 2608 struct nvme_fc_fcp_op *op) 2609 { 2610 struct nvmefc_fcp_req *freq = &op->fcp_req; 2611 2612 if (!freq->sg_cnt) 2613 return; 2614 2615 fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents, 2616 rq_dma_dir(rq)); 2617 2618 sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT); 2619 2620 freq->sg_cnt = 0; 2621 } 2622 2623 /* 2624 * In FC, the queue is a logical thing. At transport connect, the target 2625 * creates its "queue" and returns a handle that is to be given to the 2626 * target whenever it posts something to the corresponding SQ. When an 2627 * SQE is sent on a SQ, FC effectively considers the SQE, or rather the 2628 * command contained within the SQE, an io, and assigns a FC exchange 2629 * to it. The SQE and the associated SQ handle are sent in the initial 2630 * CMD IU sents on the exchange. All transfers relative to the io occur 2631 * as part of the exchange. The CQE is the last thing for the io, 2632 * which is transferred (explicitly or implicitly) with the RSP IU 2633 * sent on the exchange. After the CQE is received, the FC exchange is 2634 * terminated and the Exchange may be used on a different io. 2635 * 2636 * The transport to LLDD api has the transport making a request for a 2637 * new fcp io request to the LLDD. The LLDD then allocates a FC exchange 2638 * resource and transfers the command. The LLDD will then process all 2639 * steps to complete the io. Upon completion, the transport done routine 2640 * is called. 2641 * 2642 * So - while the operation is outstanding to the LLDD, there is a link 2643 * level FC exchange resource that is also outstanding. This must be 2644 * considered in all cleanup operations. 2645 */ 2646 static blk_status_t 2647 nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue, 2648 struct nvme_fc_fcp_op *op, u32 data_len, 2649 enum nvmefc_fcp_datadir io_dir) 2650 { 2651 struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 2652 struct nvme_command *sqe = &cmdiu->sqe; 2653 int ret, opstate; 2654 2655 /* 2656 * before attempting to send the io, check to see if we believe 2657 * the target device is present 2658 */ 2659 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE) 2660 return BLK_STS_RESOURCE; 2661 2662 if (!nvme_fc_ctrl_get(ctrl)) 2663 return BLK_STS_IOERR; 2664 2665 /* format the FC-NVME CMD IU and fcp_req */ 2666 cmdiu->connection_id = cpu_to_be64(queue->connection_id); 2667 cmdiu->data_len = cpu_to_be32(data_len); 2668 switch (io_dir) { 2669 case NVMEFC_FCP_WRITE: 2670 cmdiu->flags = FCNVME_CMD_FLAGS_WRITE; 2671 break; 2672 case NVMEFC_FCP_READ: 2673 cmdiu->flags = FCNVME_CMD_FLAGS_READ; 2674 break; 2675 case NVMEFC_FCP_NODATA: 2676 cmdiu->flags = 0; 2677 break; 2678 } 2679 op->fcp_req.payload_length = data_len; 2680 op->fcp_req.io_dir = io_dir; 2681 op->fcp_req.transferred_length = 0; 2682 op->fcp_req.rcv_rsplen = 0; 2683 op->fcp_req.status = NVME_SC_SUCCESS; 2684 op->fcp_req.sqid = cpu_to_le16(queue->qnum); 2685 2686 /* 2687 * validate per fabric rules, set fields mandated by fabric spec 2688 * as well as those by FC-NVME spec. 2689 */ 2690 WARN_ON_ONCE(sqe->common.metadata); 2691 sqe->common.flags |= NVME_CMD_SGL_METABUF; 2692 2693 /* 2694 * format SQE DPTR field per FC-NVME rules: 2695 * type=0x5 Transport SGL Data Block Descriptor 2696 * subtype=0xA Transport-specific value 2697 * address=0 2698 * length=length of the data series 2699 */ 2700 sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) | 2701 NVME_SGL_FMT_TRANSPORT_A; 2702 sqe->rw.dptr.sgl.length = cpu_to_le32(data_len); 2703 sqe->rw.dptr.sgl.addr = 0; 2704 2705 if (!(op->flags & FCOP_FLAGS_AEN)) { 2706 ret = nvme_fc_map_data(ctrl, op->rq, op); 2707 if (ret < 0) { 2708 nvme_cleanup_cmd(op->rq); 2709 nvme_fc_ctrl_put(ctrl); 2710 if (ret == -ENOMEM || ret == -EAGAIN) 2711 return BLK_STS_RESOURCE; 2712 return BLK_STS_IOERR; 2713 } 2714 } 2715 2716 fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma, 2717 sizeof(op->cmd_iu), DMA_TO_DEVICE); 2718 2719 atomic_set(&op->state, FCPOP_STATE_ACTIVE); 2720 2721 if (!(op->flags & FCOP_FLAGS_AEN)) 2722 nvme_start_request(op->rq); 2723 2724 cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn)); 2725 ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport, 2726 &ctrl->rport->remoteport, 2727 queue->lldd_handle, &op->fcp_req); 2728 2729 if (ret) { 2730 /* 2731 * If the lld fails to send the command is there an issue with 2732 * the csn value? If the command that fails is the Connect, 2733 * no - as the connection won't be live. If it is a command 2734 * post-connect, it's possible a gap in csn may be created. 2735 * Does this matter? As Linux initiators don't send fused 2736 * commands, no. The gap would exist, but as there's nothing 2737 * that depends on csn order to be delivered on the target 2738 * side, it shouldn't hurt. It would be difficult for a 2739 * target to even detect the csn gap as it has no idea when the 2740 * cmd with the csn was supposed to arrive. 2741 */ 2742 opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE); 2743 __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate); 2744 2745 if (!(op->flags & FCOP_FLAGS_AEN)) { 2746 nvme_fc_unmap_data(ctrl, op->rq, op); 2747 nvme_cleanup_cmd(op->rq); 2748 } 2749 2750 nvme_fc_ctrl_put(ctrl); 2751 2752 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE && 2753 ret != -EBUSY) 2754 return BLK_STS_IOERR; 2755 2756 return BLK_STS_RESOURCE; 2757 } 2758 2759 return BLK_STS_OK; 2760 } 2761 2762 static blk_status_t 2763 nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx, 2764 const struct blk_mq_queue_data *bd) 2765 { 2766 struct nvme_ns *ns = hctx->queue->queuedata; 2767 struct nvme_fc_queue *queue = hctx->driver_data; 2768 struct nvme_fc_ctrl *ctrl = queue->ctrl; 2769 struct request *rq = bd->rq; 2770 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 2771 enum nvmefc_fcp_datadir io_dir; 2772 bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags); 2773 u32 data_len; 2774 blk_status_t ret; 2775 2776 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE || 2777 !nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready)) 2778 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq); 2779 2780 ret = nvme_setup_cmd(ns, rq); 2781 if (ret) 2782 return ret; 2783 2784 /* 2785 * nvme core doesn't quite treat the rq opaquely. Commands such 2786 * as WRITE ZEROES will return a non-zero rq payload_bytes yet 2787 * there is no actual payload to be transferred. 2788 * To get it right, key data transmission on there being 1 or 2789 * more physical segments in the sg list. If there are no 2790 * physical segments, there is no payload. 2791 */ 2792 if (blk_rq_nr_phys_segments(rq)) { 2793 data_len = blk_rq_payload_bytes(rq); 2794 io_dir = ((rq_data_dir(rq) == WRITE) ? 2795 NVMEFC_FCP_WRITE : NVMEFC_FCP_READ); 2796 } else { 2797 data_len = 0; 2798 io_dir = NVMEFC_FCP_NODATA; 2799 } 2800 2801 2802 return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir); 2803 } 2804 2805 static void 2806 nvme_fc_submit_async_event(struct nvme_ctrl *arg) 2807 { 2808 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg); 2809 struct nvme_fc_fcp_op *aen_op; 2810 blk_status_t ret; 2811 2812 if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) 2813 return; 2814 2815 aen_op = &ctrl->aen_ops[0]; 2816 2817 ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0, 2818 NVMEFC_FCP_NODATA); 2819 if (ret) 2820 dev_err(ctrl->ctrl.device, 2821 "failed async event work\n"); 2822 } 2823 2824 static void 2825 nvme_fc_complete_rq(struct request *rq) 2826 { 2827 struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 2828 struct nvme_fc_ctrl *ctrl = op->ctrl; 2829 2830 atomic_set(&op->state, FCPOP_STATE_IDLE); 2831 op->flags &= ~FCOP_FLAGS_TERMIO; 2832 2833 nvme_fc_unmap_data(ctrl, rq, op); 2834 nvme_complete_rq(rq); 2835 nvme_fc_ctrl_put(ctrl); 2836 } 2837 2838 static void nvme_fc_map_queues(struct blk_mq_tag_set *set) 2839 { 2840 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(set->driver_data); 2841 int i; 2842 2843 for (i = 0; i < set->nr_maps; i++) { 2844 struct blk_mq_queue_map *map = &set->map[i]; 2845 2846 if (!map->nr_queues) { 2847 WARN_ON(i == HCTX_TYPE_DEFAULT); 2848 continue; 2849 } 2850 2851 /* Call LLDD map queue functionality if defined */ 2852 if (ctrl->lport->ops->map_queues) 2853 ctrl->lport->ops->map_queues(&ctrl->lport->localport, 2854 map); 2855 else 2856 blk_mq_map_queues(map); 2857 } 2858 } 2859 2860 static const struct blk_mq_ops nvme_fc_mq_ops = { 2861 .queue_rq = nvme_fc_queue_rq, 2862 .complete = nvme_fc_complete_rq, 2863 .init_request = nvme_fc_init_request, 2864 .exit_request = nvme_fc_exit_request, 2865 .init_hctx = nvme_fc_init_hctx, 2866 .timeout = nvme_fc_timeout, 2867 .map_queues = nvme_fc_map_queues, 2868 }; 2869 2870 static int 2871 nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl) 2872 { 2873 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 2874 unsigned int nr_io_queues; 2875 int ret; 2876 2877 nr_io_queues = min3(opts->nr_io_queues, num_online_cpus(), 2878 ctrl->lport->ops->max_hw_queues); 2879 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); 2880 if (ret) { 2881 dev_info(ctrl->ctrl.device, 2882 "set_queue_count failed: %d\n", ret); 2883 return ret; 2884 } 2885 2886 ctrl->ctrl.queue_count = nr_io_queues + 1; 2887 if (!nr_io_queues) 2888 return 0; 2889 2890 nvme_fc_init_io_queues(ctrl); 2891 2892 ret = nvme_alloc_io_tag_set(&ctrl->ctrl, &ctrl->tag_set, 2893 &nvme_fc_mq_ops, 1, 2894 struct_size_t(struct nvme_fcp_op_w_sgl, priv, 2895 ctrl->lport->ops->fcprqst_priv_sz)); 2896 if (ret) 2897 return ret; 2898 2899 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1); 2900 if (ret) 2901 goto out_cleanup_tagset; 2902 2903 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1); 2904 if (ret) 2905 goto out_delete_hw_queues; 2906 2907 ctrl->ioq_live = true; 2908 2909 return 0; 2910 2911 out_delete_hw_queues: 2912 nvme_fc_delete_hw_io_queues(ctrl); 2913 out_cleanup_tagset: 2914 /* 2915 * In CONNECTING state ctrl->ioerr_work will abort both admin 2916 * and io tagsets. Cancel it first before removing io tagset. 2917 */ 2918 cancel_work_sync(&ctrl->ioerr_work); 2919 nvme_remove_io_tag_set(&ctrl->ctrl); 2920 nvme_fc_free_io_queues(ctrl); 2921 2922 /* force put free routine to ignore io queues */ 2923 ctrl->ctrl.tagset = NULL; 2924 2925 return ret; 2926 } 2927 2928 static int 2929 nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl) 2930 { 2931 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 2932 u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1; 2933 unsigned int nr_io_queues; 2934 int ret; 2935 2936 nr_io_queues = min3(opts->nr_io_queues, num_online_cpus(), 2937 ctrl->lport->ops->max_hw_queues); 2938 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); 2939 if (ret) { 2940 dev_info(ctrl->ctrl.device, 2941 "set_queue_count failed: %d\n", ret); 2942 return ret; 2943 } 2944 2945 if (!nr_io_queues && prior_ioq_cnt) { 2946 dev_info(ctrl->ctrl.device, 2947 "Fail Reconnect: At least 1 io queue " 2948 "required (was %d)\n", prior_ioq_cnt); 2949 return -ENOSPC; 2950 } 2951 2952 ctrl->ctrl.queue_count = nr_io_queues + 1; 2953 /* check for io queues existing */ 2954 if (ctrl->ctrl.queue_count == 1) 2955 return 0; 2956 2957 if (prior_ioq_cnt != nr_io_queues) { 2958 dev_info(ctrl->ctrl.device, 2959 "reconnect: revising io queue count from %d to %d\n", 2960 prior_ioq_cnt, nr_io_queues); 2961 blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues); 2962 } 2963 2964 ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1); 2965 if (ret) 2966 goto out_free_io_queues; 2967 2968 ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1); 2969 if (ret) 2970 goto out_delete_hw_queues; 2971 2972 return 0; 2973 2974 out_delete_hw_queues: 2975 nvme_fc_delete_hw_io_queues(ctrl); 2976 out_free_io_queues: 2977 nvme_fc_free_io_queues(ctrl); 2978 return ret; 2979 } 2980 2981 static void 2982 nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport) 2983 { 2984 struct nvme_fc_lport *lport = rport->lport; 2985 2986 atomic_inc(&lport->act_rport_cnt); 2987 } 2988 2989 static void 2990 nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport) 2991 { 2992 struct nvme_fc_lport *lport = rport->lport; 2993 u32 cnt; 2994 2995 cnt = atomic_dec_return(&lport->act_rport_cnt); 2996 if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED) 2997 lport->ops->localport_delete(&lport->localport); 2998 } 2999 3000 static int 3001 nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl) 3002 { 3003 struct nvme_fc_rport *rport = ctrl->rport; 3004 u32 cnt; 3005 3006 if (test_and_set_bit(ASSOC_ACTIVE, &ctrl->flags)) 3007 return 1; 3008 3009 cnt = atomic_inc_return(&rport->act_ctrl_cnt); 3010 if (cnt == 1) 3011 nvme_fc_rport_active_on_lport(rport); 3012 3013 return 0; 3014 } 3015 3016 static int 3017 nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl) 3018 { 3019 struct nvme_fc_rport *rport = ctrl->rport; 3020 struct nvme_fc_lport *lport = rport->lport; 3021 u32 cnt; 3022 3023 /* clearing of ctrl->flags ASSOC_ACTIVE bit is in association delete */ 3024 3025 cnt = atomic_dec_return(&rport->act_ctrl_cnt); 3026 if (cnt == 0) { 3027 if (rport->remoteport.port_state == FC_OBJSTATE_DELETED) 3028 lport->ops->remoteport_delete(&rport->remoteport); 3029 nvme_fc_rport_inactive_on_lport(rport); 3030 } 3031 3032 return 0; 3033 } 3034 3035 /* 3036 * This routine restarts the controller on the host side, and 3037 * on the link side, recreates the controller association. 3038 */ 3039 static int 3040 nvme_fc_create_association(struct nvme_fc_ctrl *ctrl) 3041 { 3042 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 3043 struct nvmefc_ls_rcv_op *disls = NULL; 3044 unsigned long flags; 3045 int ret; 3046 3047 ++ctrl->ctrl.nr_reconnects; 3048 3049 spin_lock_irqsave(&ctrl->rport->lock, flags); 3050 if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE) { 3051 spin_unlock_irqrestore(&ctrl->rport->lock, flags); 3052 return -ENODEV; 3053 } 3054 3055 if (nvme_fc_ctlr_active_on_rport(ctrl)) { 3056 spin_unlock_irqrestore(&ctrl->rport->lock, flags); 3057 return -ENOTUNIQ; 3058 } 3059 spin_unlock_irqrestore(&ctrl->rport->lock, flags); 3060 3061 dev_info(ctrl->ctrl.device, 3062 "NVME-FC{%d}: create association : host wwpn 0x%016llx " 3063 " rport wwpn 0x%016llx: NQN \"%s\"\n", 3064 ctrl->cnum, ctrl->lport->localport.port_name, 3065 ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn); 3066 3067 clear_bit(ASSOC_FAILED, &ctrl->flags); 3068 3069 /* 3070 * Create the admin queue 3071 */ 3072 3073 ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0, 3074 NVME_AQ_DEPTH); 3075 if (ret) 3076 goto out_free_queue; 3077 3078 ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0], 3079 NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4)); 3080 if (ret) 3081 goto out_delete_hw_queue; 3082 3083 ret = nvmf_connect_admin_queue(&ctrl->ctrl); 3084 if (ret) 3085 goto out_disconnect_admin_queue; 3086 3087 set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags); 3088 3089 /* 3090 * Check controller capabilities 3091 * 3092 * todo:- add code to check if ctrl attributes changed from 3093 * prior connection values 3094 */ 3095 3096 ret = nvme_enable_ctrl(&ctrl->ctrl); 3097 if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags)) 3098 ret = -EIO; 3099 if (ret) 3100 goto out_disconnect_admin_queue; 3101 3102 ctrl->ctrl.max_segments = ctrl->lport->ops->max_sgl_segments; 3103 ctrl->ctrl.max_hw_sectors = ctrl->ctrl.max_segments << 3104 (ilog2(SZ_4K) - 9); 3105 3106 nvme_unquiesce_admin_queue(&ctrl->ctrl); 3107 3108 ret = nvme_init_ctrl_finish(&ctrl->ctrl, false); 3109 if (ret) 3110 goto out_disconnect_admin_queue; 3111 if (test_bit(ASSOC_FAILED, &ctrl->flags)) { 3112 ret = -EIO; 3113 goto out_stop_keep_alive; 3114 } 3115 /* sanity checks */ 3116 3117 /* FC-NVME does not have other data in the capsule */ 3118 if (ctrl->ctrl.icdoff) { 3119 dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n", 3120 ctrl->ctrl.icdoff); 3121 ret = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 3122 goto out_stop_keep_alive; 3123 } 3124 3125 /* FC-NVME supports normal SGL Data Block Descriptors */ 3126 if (!nvme_ctrl_sgl_supported(&ctrl->ctrl)) { 3127 dev_err(ctrl->ctrl.device, 3128 "Mandatory sgls are not supported!\n"); 3129 ret = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 3130 goto out_stop_keep_alive; 3131 } 3132 3133 if (opts->queue_size > ctrl->ctrl.maxcmd) { 3134 /* warn if maxcmd is lower than queue_size */ 3135 dev_warn(ctrl->ctrl.device, 3136 "queue_size %zu > ctrl maxcmd %u, reducing " 3137 "to maxcmd\n", 3138 opts->queue_size, ctrl->ctrl.maxcmd); 3139 opts->queue_size = ctrl->ctrl.maxcmd; 3140 ctrl->ctrl.sqsize = opts->queue_size - 1; 3141 } 3142 3143 ret = nvme_fc_init_aen_ops(ctrl); 3144 if (ret) 3145 goto out_term_aen_ops; 3146 3147 /* 3148 * Create the io queues 3149 */ 3150 3151 if (ctrl->ctrl.queue_count > 1) { 3152 if (!ctrl->ioq_live) 3153 ret = nvme_fc_create_io_queues(ctrl); 3154 else 3155 ret = nvme_fc_recreate_io_queues(ctrl); 3156 } 3157 if (!ret && test_bit(ASSOC_FAILED, &ctrl->flags)) 3158 ret = -EIO; 3159 if (ret) 3160 goto out_term_aen_ops; 3161 3162 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE)) { 3163 ret = -EIO; 3164 goto out_term_aen_ops; 3165 } 3166 3167 /* accumulate reconnect attempts before resetting it to zero */ 3168 atomic_long_add(ctrl->ctrl.nr_reconnects, &ctrl->ctrl.acc_reconnects); 3169 ctrl->ctrl.nr_reconnects = 0; 3170 nvme_start_ctrl(&ctrl->ctrl); 3171 3172 return 0; /* Success */ 3173 3174 out_term_aen_ops: 3175 nvme_fc_term_aen_ops(ctrl); 3176 out_stop_keep_alive: 3177 nvme_stop_keep_alive(&ctrl->ctrl); 3178 out_disconnect_admin_queue: 3179 dev_warn(ctrl->ctrl.device, 3180 "NVME-FC{%d}: create_assoc failed, assoc_id %llx ret %d\n", 3181 ctrl->cnum, ctrl->association_id, ret); 3182 /* send a Disconnect(association) LS to fc-nvme target */ 3183 nvme_fc_xmt_disconnect_assoc(ctrl); 3184 spin_lock_irqsave(&ctrl->lock, flags); 3185 ctrl->association_id = 0; 3186 disls = ctrl->rcv_disconn; 3187 ctrl->rcv_disconn = NULL; 3188 spin_unlock_irqrestore(&ctrl->lock, flags); 3189 if (disls) 3190 nvme_fc_xmt_ls_rsp(disls); 3191 out_delete_hw_queue: 3192 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0); 3193 out_free_queue: 3194 nvme_fc_free_queue(&ctrl->queues[0]); 3195 clear_bit(ASSOC_ACTIVE, &ctrl->flags); 3196 nvme_fc_ctlr_inactive_on_rport(ctrl); 3197 3198 return ret; 3199 } 3200 3201 3202 /* 3203 * This routine stops operation of the controller on the host side. 3204 * On the host os stack side: Admin and IO queues are stopped, 3205 * outstanding ios on them terminated via FC ABTS. 3206 * On the link side: the association is terminated. 3207 */ 3208 static void 3209 nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl) 3210 { 3211 struct nvmefc_ls_rcv_op *disls = NULL; 3212 unsigned long flags; 3213 3214 if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags)) 3215 return; 3216 3217 spin_lock_irqsave(&ctrl->lock, flags); 3218 set_bit(FCCTRL_TERMIO, &ctrl->flags); 3219 ctrl->iocnt = 0; 3220 spin_unlock_irqrestore(&ctrl->lock, flags); 3221 3222 __nvme_fc_abort_outstanding_ios(ctrl, false); 3223 3224 /* kill the aens as they are a separate path */ 3225 nvme_fc_abort_aen_ops(ctrl); 3226 3227 /* wait for all io that had to be aborted */ 3228 spin_lock_irq(&ctrl->lock); 3229 wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock); 3230 clear_bit(FCCTRL_TERMIO, &ctrl->flags); 3231 spin_unlock_irq(&ctrl->lock); 3232 3233 nvme_fc_term_aen_ops(ctrl); 3234 3235 /* 3236 * send a Disconnect(association) LS to fc-nvme target 3237 * Note: could have been sent at top of process, but 3238 * cleaner on link traffic if after the aborts complete. 3239 * Note: if association doesn't exist, association_id will be 0 3240 */ 3241 if (ctrl->association_id) 3242 nvme_fc_xmt_disconnect_assoc(ctrl); 3243 3244 spin_lock_irqsave(&ctrl->lock, flags); 3245 ctrl->association_id = 0; 3246 disls = ctrl->rcv_disconn; 3247 ctrl->rcv_disconn = NULL; 3248 spin_unlock_irqrestore(&ctrl->lock, flags); 3249 if (disls) 3250 /* 3251 * if a Disconnect Request was waiting for a response, send 3252 * now that all ABTS's have been issued (and are complete). 3253 */ 3254 nvme_fc_xmt_ls_rsp(disls); 3255 3256 if (ctrl->ctrl.tagset) { 3257 nvme_fc_delete_hw_io_queues(ctrl); 3258 nvme_fc_free_io_queues(ctrl); 3259 } 3260 3261 __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0); 3262 nvme_fc_free_queue(&ctrl->queues[0]); 3263 3264 /* re-enable the admin_q so anything new can fast fail */ 3265 nvme_unquiesce_admin_queue(&ctrl->ctrl); 3266 3267 /* resume the io queues so that things will fast fail */ 3268 nvme_unquiesce_io_queues(&ctrl->ctrl); 3269 3270 nvme_fc_ctlr_inactive_on_rport(ctrl); 3271 } 3272 3273 static void 3274 nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl) 3275 { 3276 struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 3277 3278 cancel_delayed_work_sync(&ctrl->connect_work); 3279 3280 /* 3281 * kill the association on the link side. this will block 3282 * waiting for io to terminate 3283 */ 3284 nvme_fc_delete_association(ctrl); 3285 cancel_work_sync(&ctrl->ioerr_work); 3286 3287 if (ctrl->ctrl.tagset) 3288 nvme_remove_io_tag_set(&ctrl->ctrl); 3289 3290 nvme_unquiesce_admin_queue(&ctrl->ctrl); 3291 nvme_remove_admin_tag_set(&ctrl->ctrl); 3292 } 3293 3294 static void 3295 nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status) 3296 { 3297 struct nvme_fc_rport *rport = ctrl->rport; 3298 struct nvme_fc_remote_port *portptr = &rport->remoteport; 3299 unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ; 3300 bool recon = true; 3301 3302 if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_CONNECTING) 3303 return; 3304 3305 if (portptr->port_state == FC_OBJSTATE_ONLINE) { 3306 dev_info(ctrl->ctrl.device, 3307 "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n", 3308 ctrl->cnum, status); 3309 } else if (time_after_eq(jiffies, rport->dev_loss_end)) 3310 recon = false; 3311 3312 if (recon && nvmf_should_reconnect(&ctrl->ctrl, status)) { 3313 if (portptr->port_state == FC_OBJSTATE_ONLINE) 3314 dev_info(ctrl->ctrl.device, 3315 "NVME-FC{%d}: Reconnect attempt in %ld " 3316 "seconds\n", 3317 ctrl->cnum, recon_delay / HZ); 3318 else if (time_after(jiffies + recon_delay, rport->dev_loss_end)) 3319 recon_delay = rport->dev_loss_end - jiffies; 3320 3321 queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay); 3322 } else { 3323 if (portptr->port_state == FC_OBJSTATE_ONLINE) { 3324 if (status > 0 && (status & NVME_STATUS_DNR)) 3325 dev_warn(ctrl->ctrl.device, 3326 "NVME-FC{%d}: reconnect failure\n", 3327 ctrl->cnum); 3328 else 3329 dev_warn(ctrl->ctrl.device, 3330 "NVME-FC{%d}: Max reconnect attempts " 3331 "(%d) reached.\n", 3332 ctrl->cnum, ctrl->ctrl.nr_reconnects); 3333 } else 3334 dev_warn(ctrl->ctrl.device, 3335 "NVME-FC{%d}: dev_loss_tmo (%d) expired " 3336 "while waiting for remoteport connectivity.\n", 3337 ctrl->cnum, min_t(int, portptr->dev_loss_tmo, 3338 (ctrl->ctrl.opts->max_reconnects * 3339 ctrl->ctrl.opts->reconnect_delay))); 3340 nvme_delete_ctrl(&ctrl->ctrl); 3341 } 3342 } 3343 3344 static void 3345 nvme_fc_reset_ctrl_work(struct work_struct *work) 3346 { 3347 struct nvme_fc_ctrl *ctrl = 3348 container_of(work, struct nvme_fc_ctrl, ctrl.reset_work); 3349 3350 nvme_stop_ctrl(&ctrl->ctrl); 3351 3352 /* will block will waiting for io to terminate */ 3353 nvme_fc_delete_association(ctrl); 3354 3355 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) 3356 dev_err(ctrl->ctrl.device, 3357 "NVME-FC{%d}: error_recovery: Couldn't change state " 3358 "to CONNECTING\n", ctrl->cnum); 3359 3360 if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) { 3361 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) { 3362 dev_err(ctrl->ctrl.device, 3363 "NVME-FC{%d}: failed to schedule connect " 3364 "after reset\n", ctrl->cnum); 3365 } else { 3366 flush_delayed_work(&ctrl->connect_work); 3367 } 3368 } else { 3369 nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN); 3370 } 3371 } 3372 3373 3374 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = { 3375 .name = "fc", 3376 .module = THIS_MODULE, 3377 .flags = NVME_F_FABRICS, 3378 .reg_read32 = nvmf_reg_read32, 3379 .reg_read64 = nvmf_reg_read64, 3380 .reg_write32 = nvmf_reg_write32, 3381 .subsystem_reset = nvmf_subsystem_reset, 3382 .free_ctrl = nvme_fc_free_ctrl, 3383 .submit_async_event = nvme_fc_submit_async_event, 3384 .delete_ctrl = nvme_fc_delete_ctrl, 3385 .get_address = nvmf_get_address, 3386 .get_virt_boundary = nvmf_get_virt_boundary, 3387 }; 3388 3389 static void 3390 nvme_fc_connect_ctrl_work(struct work_struct *work) 3391 { 3392 int ret; 3393 3394 struct nvme_fc_ctrl *ctrl = 3395 container_of(to_delayed_work(work), 3396 struct nvme_fc_ctrl, connect_work); 3397 3398 ret = nvme_fc_create_association(ctrl); 3399 if (ret) 3400 nvme_fc_reconnect_or_delete(ctrl, ret); 3401 else 3402 dev_info(ctrl->ctrl.device, 3403 "NVME-FC{%d}: controller connect complete\n", 3404 ctrl->cnum); 3405 } 3406 3407 3408 static const struct blk_mq_ops nvme_fc_admin_mq_ops = { 3409 .queue_rq = nvme_fc_queue_rq, 3410 .complete = nvme_fc_complete_rq, 3411 .init_request = nvme_fc_init_request, 3412 .exit_request = nvme_fc_exit_request, 3413 .init_hctx = nvme_fc_init_admin_hctx, 3414 .timeout = nvme_fc_timeout, 3415 }; 3416 3417 3418 /* 3419 * Fails a controller request if it matches an existing controller 3420 * (association) with the same tuple: 3421 * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN> 3422 * 3423 * The ports don't need to be compared as they are intrinsically 3424 * already matched by the port pointers supplied. 3425 */ 3426 static bool 3427 nvme_fc_existing_controller(struct nvme_fc_rport *rport, 3428 struct nvmf_ctrl_options *opts) 3429 { 3430 struct nvme_fc_ctrl *ctrl; 3431 unsigned long flags; 3432 bool found = false; 3433 3434 spin_lock_irqsave(&rport->lock, flags); 3435 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) { 3436 found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts); 3437 if (found) 3438 break; 3439 } 3440 spin_unlock_irqrestore(&rport->lock, flags); 3441 3442 return found; 3443 } 3444 3445 static struct nvme_fc_ctrl * 3446 nvme_fc_alloc_ctrl(struct device *dev, struct nvmf_ctrl_options *opts, 3447 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport) 3448 { 3449 struct nvme_fc_ctrl *ctrl; 3450 int ret, idx, ctrl_loss_tmo; 3451 3452 if (!(rport->remoteport.port_role & 3453 (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) { 3454 ret = -EBADR; 3455 goto out_fail; 3456 } 3457 3458 if (!opts->duplicate_connect && 3459 nvme_fc_existing_controller(rport, opts)) { 3460 ret = -EALREADY; 3461 goto out_fail; 3462 } 3463 3464 ctrl = kzalloc_obj(*ctrl); 3465 if (!ctrl) { 3466 ret = -ENOMEM; 3467 goto out_fail; 3468 } 3469 3470 idx = ida_alloc(&nvme_fc_ctrl_cnt, GFP_KERNEL); 3471 if (idx < 0) { 3472 ret = -ENOSPC; 3473 goto out_free_ctrl; 3474 } 3475 3476 /* 3477 * if ctrl_loss_tmo is being enforced and the default reconnect delay 3478 * is being used, change to a shorter reconnect delay for FC. 3479 */ 3480 if (opts->max_reconnects != -1 && 3481 opts->reconnect_delay == NVMF_DEF_RECONNECT_DELAY && 3482 opts->reconnect_delay > NVME_FC_DEFAULT_RECONNECT_TMO) { 3483 ctrl_loss_tmo = opts->max_reconnects * opts->reconnect_delay; 3484 opts->reconnect_delay = NVME_FC_DEFAULT_RECONNECT_TMO; 3485 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo, 3486 opts->reconnect_delay); 3487 } 3488 3489 ctrl->ctrl.opts = opts; 3490 ctrl->ctrl.nr_reconnects = 0; 3491 atomic_long_set(&ctrl->ctrl.acc_reconnects, 0); 3492 INIT_LIST_HEAD(&ctrl->ctrl_list); 3493 ctrl->lport = lport; 3494 ctrl->rport = rport; 3495 ctrl->dev = lport->dev; 3496 ctrl->cnum = idx; 3497 ctrl->ioq_live = false; 3498 init_waitqueue_head(&ctrl->ioabort_wait); 3499 3500 get_device(ctrl->dev); 3501 kref_init(&ctrl->ref); 3502 3503 INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work); 3504 INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work); 3505 INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work); 3506 spin_lock_init(&ctrl->lock); 3507 3508 /* io queue count */ 3509 ctrl->ctrl.queue_count = min_t(unsigned int, 3510 opts->nr_io_queues, 3511 lport->ops->max_hw_queues); 3512 ctrl->ctrl.queue_count++; /* +1 for admin queue */ 3513 3514 ctrl->ctrl.sqsize = opts->queue_size - 1; 3515 ctrl->ctrl.kato = opts->kato; 3516 ctrl->ctrl.cntlid = 0xffff; 3517 3518 ret = -ENOMEM; 3519 ctrl->queues = kzalloc_objs(struct nvme_fc_queue, 3520 ctrl->ctrl.queue_count); 3521 if (!ctrl->queues) 3522 goto out_free_ida; 3523 3524 nvme_fc_init_queue(ctrl, 0); 3525 3526 /* 3527 * Would have been nice to init io queues tag set as well. 3528 * However, we require interaction from the controller 3529 * for max io queue count before we can do so. 3530 * Defer this to the connect path. 3531 */ 3532 3533 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0); 3534 if (ret) 3535 goto out_free_queues; 3536 if (lport->dev) 3537 ctrl->ctrl.numa_node = dev_to_node(lport->dev); 3538 3539 return ctrl; 3540 3541 out_free_queues: 3542 kfree(ctrl->queues); 3543 out_free_ida: 3544 put_device(ctrl->dev); 3545 ida_free(&nvme_fc_ctrl_cnt, ctrl->cnum); 3546 out_free_ctrl: 3547 kfree(ctrl); 3548 out_fail: 3549 /* exit via here doesn't follow ctlr ref points */ 3550 return ERR_PTR(ret); 3551 } 3552 3553 static struct nvme_ctrl * 3554 nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts, 3555 struct nvme_fc_lport *lport, struct nvme_fc_rport *rport) 3556 { 3557 struct nvme_fc_ctrl *ctrl; 3558 unsigned long flags; 3559 int ret; 3560 3561 ctrl = nvme_fc_alloc_ctrl(dev, opts, lport, rport); 3562 if (IS_ERR(ctrl)) 3563 return ERR_CAST(ctrl); 3564 3565 ret = nvme_add_ctrl(&ctrl->ctrl); 3566 if (ret) 3567 goto out_put_ctrl; 3568 3569 ret = nvme_alloc_admin_tag_set(&ctrl->ctrl, &ctrl->admin_tag_set, 3570 &nvme_fc_admin_mq_ops, 3571 struct_size_t(struct nvme_fcp_op_w_sgl, priv, 3572 ctrl->lport->ops->fcprqst_priv_sz)); 3573 if (ret) 3574 goto fail_ctrl; 3575 3576 spin_lock_irqsave(&rport->lock, flags); 3577 list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list); 3578 spin_unlock_irqrestore(&rport->lock, flags); 3579 3580 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { 3581 dev_err(ctrl->ctrl.device, 3582 "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum); 3583 goto fail_unlist; 3584 } 3585 3586 if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) { 3587 dev_err(ctrl->ctrl.device, 3588 "NVME-FC{%d}: failed to schedule initial connect\n", 3589 ctrl->cnum); 3590 goto fail_unlist; 3591 } 3592 3593 flush_delayed_work(&ctrl->connect_work); 3594 3595 dev_info(ctrl->ctrl.device, 3596 "NVME-FC{%d}: new ctrl: NQN \"%s\", hostnqn: %s\n", 3597 ctrl->cnum, nvmf_ctrl_subsysnqn(&ctrl->ctrl), opts->host->nqn); 3598 3599 return &ctrl->ctrl; 3600 3601 fail_unlist: 3602 /* 3603 * Leaving the list hands the options back to nvmf_create_ctrl(); 3604 * see nvme_fc_ctrl_free(). Re-init so that list_empty() there 3605 * reports the controller as unlisted. 3606 */ 3607 spin_lock_irqsave(&rport->lock, flags); 3608 list_del_init(&ctrl->ctrl_list); 3609 spin_unlock_irqrestore(&rport->lock, flags); 3610 3611 fail_ctrl: 3612 nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING); 3613 cancel_work_sync(&ctrl->ioerr_work); 3614 cancel_work_sync(&ctrl->ctrl.reset_work); 3615 cancel_delayed_work_sync(&ctrl->connect_work); 3616 3617 if (ctrl->ctrl.admin_tagset) 3618 nvme_remove_admin_tag_set(&ctrl->ctrl); 3619 /* initiate nvme ctrl ref counting teardown */ 3620 nvme_uninit_ctrl(&ctrl->ctrl); 3621 3622 out_put_ctrl: 3623 /* Remove core ctrl ref. */ 3624 nvme_put_ctrl(&ctrl->ctrl); 3625 3626 /* as we're past the point where we transition to the ref 3627 * counting teardown path, if we return a bad pointer here, 3628 * the calling routine, thinking it's prior to the 3629 * transition, will do an rport put. Since the teardown 3630 * path also does a rport put, we do an extra get here to 3631 * so proper order/teardown happens. 3632 */ 3633 nvme_fc_rport_get(rport); 3634 3635 return ERR_PTR(-EIO); 3636 } 3637 3638 struct nvmet_fc_traddr { 3639 u64 nn; 3640 u64 pn; 3641 }; 3642 3643 static int 3644 __nvme_fc_parse_u64(substring_t *sstr, u64 *val) 3645 { 3646 u64 token64; 3647 3648 if (match_u64(sstr, &token64)) 3649 return -EINVAL; 3650 *val = token64; 3651 3652 return 0; 3653 } 3654 3655 /* 3656 * This routine validates and extracts the WWN's from the TRADDR string. 3657 * As kernel parsers need the 0x to determine number base, universally 3658 * build string to parse with 0x prefix before parsing name strings. 3659 */ 3660 static int 3661 nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen) 3662 { 3663 char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1]; 3664 substring_t wwn = { name, &name[sizeof(name)-1] }; 3665 int nnoffset, pnoffset; 3666 3667 /* validate if string is one of the 2 allowed formats */ 3668 if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH && 3669 !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) && 3670 !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET], 3671 "pn-0x", NVME_FC_TRADDR_OXNNLEN)) { 3672 nnoffset = NVME_FC_TRADDR_OXNNLEN; 3673 pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET + 3674 NVME_FC_TRADDR_OXNNLEN; 3675 } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH && 3676 !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) && 3677 !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET], 3678 "pn-", NVME_FC_TRADDR_NNLEN))) { 3679 nnoffset = NVME_FC_TRADDR_NNLEN; 3680 pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN; 3681 } else 3682 goto out_einval; 3683 3684 name[0] = '0'; 3685 name[1] = 'x'; 3686 name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0; 3687 3688 memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN); 3689 if (__nvme_fc_parse_u64(&wwn, &traddr->nn)) 3690 goto out_einval; 3691 3692 memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN); 3693 if (__nvme_fc_parse_u64(&wwn, &traddr->pn)) 3694 goto out_einval; 3695 3696 return 0; 3697 3698 out_einval: 3699 pr_warn("%s: bad traddr string\n", __func__); 3700 return -EINVAL; 3701 } 3702 3703 static struct nvme_ctrl * 3704 nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts) 3705 { 3706 struct nvme_fc_lport *lport; 3707 struct nvme_fc_rport *rport; 3708 struct nvme_ctrl *ctrl; 3709 struct nvmet_fc_traddr laddr = { 0L, 0L }; 3710 struct nvmet_fc_traddr raddr = { 0L, 0L }; 3711 unsigned long flags; 3712 int ret; 3713 3714 ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE); 3715 if (ret || !raddr.nn || !raddr.pn) 3716 return ERR_PTR(-EINVAL); 3717 3718 ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE); 3719 if (ret || !laddr.nn || !laddr.pn) 3720 return ERR_PTR(-EINVAL); 3721 3722 /* find the host and remote ports to connect together */ 3723 spin_lock_irqsave(&nvme_fc_lock, flags); 3724 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { 3725 if (lport->localport.node_name != laddr.nn || 3726 lport->localport.port_name != laddr.pn || 3727 lport->localport.port_state != FC_OBJSTATE_ONLINE) 3728 continue; 3729 3730 list_for_each_entry(rport, &lport->endp_list, endp_list) { 3731 if (rport->remoteport.node_name != raddr.nn || 3732 rport->remoteport.port_name != raddr.pn || 3733 rport->remoteport.port_state != FC_OBJSTATE_ONLINE) 3734 continue; 3735 3736 /* if fail to get reference fall through. Will error */ 3737 if (!nvme_fc_rport_get(rport)) 3738 break; 3739 3740 spin_unlock_irqrestore(&nvme_fc_lock, flags); 3741 3742 ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport); 3743 if (IS_ERR(ctrl)) 3744 nvme_fc_rport_put(rport); 3745 return ctrl; 3746 } 3747 } 3748 spin_unlock_irqrestore(&nvme_fc_lock, flags); 3749 3750 pr_warn("%s: %s - %s combination not found\n", 3751 __func__, opts->traddr, opts->host_traddr); 3752 return ERR_PTR(-ENOENT); 3753 } 3754 3755 3756 static struct nvmf_transport_ops nvme_fc_transport = { 3757 .name = "fc", 3758 .module = THIS_MODULE, 3759 .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR, 3760 .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO, 3761 .create_ctrl = nvme_fc_create_ctrl, 3762 }; 3763 3764 /* Arbitrary successive failures max. With lots of subsystems could be high */ 3765 #define DISCOVERY_MAX_FAIL 20 3766 3767 static ssize_t nvme_fc_nvme_discovery_store(struct device *dev, 3768 struct device_attribute *attr, const char *buf, size_t count) 3769 { 3770 unsigned long flags; 3771 LIST_HEAD(local_disc_list); 3772 struct nvme_fc_lport *lport; 3773 struct nvme_fc_rport *rport; 3774 int failcnt = 0; 3775 3776 spin_lock_irqsave(&nvme_fc_lock, flags); 3777 restart: 3778 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { 3779 list_for_each_entry(rport, &lport->endp_list, endp_list) { 3780 /* skip rports already queued for discovery */ 3781 if (!list_empty(&rport->disc_list)) 3782 continue; 3783 if (!nvme_fc_lport_get(lport)) 3784 continue; 3785 if (!nvme_fc_rport_get(rport)) { 3786 /* 3787 * This is a temporary condition. Upon restart 3788 * this rport will be gone from the list. 3789 * 3790 * Revert the lport put and retry. Anything 3791 * added to the list already will be skipped (as 3792 * they are no longer list_empty). Loops should 3793 * resume at rports that were not yet seen. 3794 */ 3795 nvme_fc_lport_put(lport); 3796 3797 if (failcnt++ < DISCOVERY_MAX_FAIL) 3798 goto restart; 3799 3800 pr_err("nvme_discovery: too many reference " 3801 "failures\n"); 3802 goto process_local_list; 3803 } 3804 list_add_tail(&rport->disc_list, &local_disc_list); 3805 } 3806 } 3807 3808 process_local_list: 3809 while (!list_empty(&local_disc_list)) { 3810 rport = list_first_entry(&local_disc_list, 3811 struct nvme_fc_rport, disc_list); 3812 list_del_init(&rport->disc_list); 3813 spin_unlock_irqrestore(&nvme_fc_lock, flags); 3814 3815 lport = rport->lport; 3816 /* signal discovery. Won't hurt if it repeats */ 3817 nvme_fc_signal_discovery_scan(lport, rport); 3818 nvme_fc_rport_put(rport); 3819 nvme_fc_lport_put(lport); 3820 3821 spin_lock_irqsave(&nvme_fc_lock, flags); 3822 } 3823 spin_unlock_irqrestore(&nvme_fc_lock, flags); 3824 3825 return count; 3826 } 3827 3828 static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store); 3829 3830 #ifdef CONFIG_BLK_CGROUP_FC_APPID 3831 /* Parse the cgroup id from a buf and return the length of cgrpid */ 3832 static int fc_parse_cgrpid(const char *buf, u64 *id) 3833 { 3834 char cgrp_id[16+1]; 3835 int cgrpid_len, j; 3836 3837 memset(cgrp_id, 0x0, sizeof(cgrp_id)); 3838 for (cgrpid_len = 0, j = 0; cgrpid_len < 17; cgrpid_len++) { 3839 if (buf[cgrpid_len] != ':') 3840 cgrp_id[cgrpid_len] = buf[cgrpid_len]; 3841 else { 3842 j = 1; 3843 break; 3844 } 3845 } 3846 if (!j) 3847 return -EINVAL; 3848 if (kstrtou64(cgrp_id, 16, id) < 0) 3849 return -EINVAL; 3850 return cgrpid_len; 3851 } 3852 3853 /* 3854 * Parse and update the appid in the blkcg associated with the cgroupid. 3855 */ 3856 static ssize_t fc_appid_store(struct device *dev, 3857 struct device_attribute *attr, const char *buf, size_t count) 3858 { 3859 size_t orig_count = count; 3860 u64 cgrp_id; 3861 int appid_len = 0; 3862 int cgrpid_len = 0; 3863 char app_id[FC_APPID_LEN]; 3864 int ret = 0; 3865 3866 if (buf[count-1] == '\n') 3867 count--; 3868 3869 if ((count > (16+1+FC_APPID_LEN)) || (!strchr(buf, ':'))) 3870 return -EINVAL; 3871 3872 cgrpid_len = fc_parse_cgrpid(buf, &cgrp_id); 3873 if (cgrpid_len < 0) 3874 return -EINVAL; 3875 appid_len = count - cgrpid_len - 1; 3876 if (appid_len > FC_APPID_LEN) 3877 return -EINVAL; 3878 3879 memset(app_id, 0x0, sizeof(app_id)); 3880 memcpy(app_id, &buf[cgrpid_len+1], appid_len); 3881 ret = blkcg_set_fc_appid(app_id, cgrp_id, sizeof(app_id)); 3882 if (ret < 0) 3883 return ret; 3884 return orig_count; 3885 } 3886 static DEVICE_ATTR(appid_store, 0200, NULL, fc_appid_store); 3887 #endif /* CONFIG_BLK_CGROUP_FC_APPID */ 3888 3889 static struct attribute *nvme_fc_attrs[] = { 3890 &dev_attr_nvme_discovery.attr, 3891 #ifdef CONFIG_BLK_CGROUP_FC_APPID 3892 &dev_attr_appid_store.attr, 3893 #endif 3894 NULL 3895 }; 3896 3897 static const struct attribute_group nvme_fc_attr_group = { 3898 .attrs = nvme_fc_attrs, 3899 }; 3900 3901 static const struct attribute_group *nvme_fc_attr_groups[] = { 3902 &nvme_fc_attr_group, 3903 NULL 3904 }; 3905 3906 static struct class fc_class = { 3907 .name = "fc", 3908 .dev_groups = nvme_fc_attr_groups, 3909 }; 3910 3911 static int __init nvme_fc_init_module(void) 3912 { 3913 int ret; 3914 3915 /* 3916 * NOTE: 3917 * It is expected that in the future the kernel will combine 3918 * the FC-isms that are currently under scsi and now being 3919 * added to by NVME into a new standalone FC class. The SCSI 3920 * and NVME protocols and their devices would be under this 3921 * new FC class. 3922 * 3923 * As we need something to post FC-specific udev events to, 3924 * specifically for nvme probe events, start by creating the 3925 * new device class. When the new standalone FC class is 3926 * put in place, this code will move to a more generic 3927 * location for the class. 3928 */ 3929 ret = class_register(&fc_class); 3930 if (ret) { 3931 pr_err("couldn't register class fc\n"); 3932 return ret; 3933 } 3934 3935 /* 3936 * Create a device for the FC-centric udev events 3937 */ 3938 fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL, 3939 "fc_udev_device"); 3940 if (IS_ERR(fc_udev_device)) { 3941 pr_err("couldn't create fc_udev device!\n"); 3942 ret = PTR_ERR(fc_udev_device); 3943 goto out_destroy_class; 3944 } 3945 3946 ret = nvmf_register_transport(&nvme_fc_transport); 3947 if (ret) 3948 goto out_destroy_device; 3949 3950 return 0; 3951 3952 out_destroy_device: 3953 device_destroy(&fc_class, MKDEV(0, 0)); 3954 out_destroy_class: 3955 class_unregister(&fc_class); 3956 3957 return ret; 3958 } 3959 3960 static void 3961 nvme_fc_delete_controllers(struct nvme_fc_rport *rport) 3962 { 3963 struct nvme_fc_ctrl *ctrl; 3964 3965 spin_lock(&rport->lock); 3966 list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) { 3967 dev_warn(ctrl->ctrl.device, 3968 "NVME-FC{%d}: transport unloading: deleting ctrl\n", 3969 ctrl->cnum); 3970 nvme_delete_ctrl(&ctrl->ctrl); 3971 } 3972 spin_unlock(&rport->lock); 3973 } 3974 3975 static void __exit nvme_fc_exit_module(void) 3976 { 3977 struct nvme_fc_lport *lport; 3978 struct nvme_fc_rport *rport; 3979 unsigned long flags; 3980 3981 spin_lock_irqsave(&nvme_fc_lock, flags); 3982 list_for_each_entry(lport, &nvme_fc_lport_list, port_list) 3983 list_for_each_entry(rport, &lport->endp_list, endp_list) 3984 nvme_fc_delete_controllers(rport); 3985 spin_unlock_irqrestore(&nvme_fc_lock, flags); 3986 flush_workqueue(nvme_delete_wq); 3987 3988 nvmf_unregister_transport(&nvme_fc_transport); 3989 3990 device_destroy(&fc_class, MKDEV(0, 0)); 3991 class_unregister(&fc_class); 3992 } 3993 3994 module_init(nvme_fc_init_module); 3995 module_exit(nvme_fc_exit_module); 3996 3997 MODULE_DESCRIPTION("NVMe host FC transport driver"); 3998 MODULE_LICENSE("GPL v2"); 3999 MODULE_ALIAS("nvme-fc"); 4000