18638b246SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0 2e399441dSJames Smart /* 3e399441dSJames Smart * Copyright (c) 2016 Avago Technologies. All rights reserved. 4e399441dSJames Smart */ 5e399441dSJames Smart #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 6e399441dSJames Smart #include <linux/module.h> 7e399441dSJames Smart #include <linux/parser.h> 8e399441dSJames Smart #include <uapi/scsi/fc/fc_fs.h> 9e399441dSJames Smart #include <uapi/scsi/fc/fc_els.h> 1061bff8efSJames Smart #include <linux/delay.h> 11d3d0bc78SBart Van Assche #include <linux/overflow.h> 12e399441dSJames Smart 13e399441dSJames Smart #include "nvme.h" 14e399441dSJames Smart #include "fabrics.h" 15e399441dSJames Smart #include <linux/nvme-fc-driver.h> 16e399441dSJames Smart #include <linux/nvme-fc.h> 17a6a6d058SHannes Reinecke #include <scsi/scsi_transport_fc.h> 18e399441dSJames Smart 19e399441dSJames Smart /* *************************** Data Structures/Defines ****************** */ 20e399441dSJames Smart 21e399441dSJames Smart 22e399441dSJames Smart enum nvme_fc_queue_flags { 2326c0a26dSJens Axboe NVME_FC_Q_CONNECTED = 0, 2426c0a26dSJens Axboe NVME_FC_Q_LIVE, 25e399441dSJames Smart }; 26e399441dSJames Smart 27ac7fe82bSJames Smart #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */ 28ac7fe82bSJames Smart 29e399441dSJames Smart struct nvme_fc_queue { 30e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 31e399441dSJames Smart struct device *dev; 32e399441dSJames Smart struct blk_mq_hw_ctx *hctx; 33e399441dSJames Smart void *lldd_handle; 34e399441dSJames Smart size_t cmnd_capsule_len; 35e399441dSJames Smart u32 qnum; 36e399441dSJames Smart u32 rqcnt; 37e399441dSJames Smart u32 seqno; 38e399441dSJames Smart 39e399441dSJames Smart u64 connection_id; 40e399441dSJames Smart atomic_t csn; 41e399441dSJames Smart 42e399441dSJames Smart unsigned long flags; 43e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 44e399441dSJames Smart 458d64daf7SJames Smart enum nvme_fcop_flags { 468d64daf7SJames Smart FCOP_FLAGS_TERMIO = (1 << 0), 47c3aedd22SJames Smart FCOP_FLAGS_AEN = (1 << 1), 488d64daf7SJames Smart }; 498d64daf7SJames Smart 50e399441dSJames Smart struct nvmefc_ls_req_op { 51e399441dSJames Smart struct nvmefc_ls_req ls_req; 52e399441dSJames Smart 53c913a8b0SJames Smart struct nvme_fc_rport *rport; 54e399441dSJames Smart struct nvme_fc_queue *queue; 55e399441dSJames Smart struct request *rq; 568d64daf7SJames Smart u32 flags; 57e399441dSJames Smart 58e399441dSJames Smart int ls_error; 59e399441dSJames Smart struct completion ls_done; 60c913a8b0SJames Smart struct list_head lsreq_list; /* rport->ls_req_list */ 61e399441dSJames Smart bool req_queued; 62e399441dSJames Smart }; 63e399441dSJames Smart 64e399441dSJames Smart enum nvme_fcpop_state { 65e399441dSJames Smart FCPOP_STATE_UNINIT = 0, 66e399441dSJames Smart FCPOP_STATE_IDLE = 1, 67e399441dSJames Smart FCPOP_STATE_ACTIVE = 2, 68e399441dSJames Smart FCPOP_STATE_ABORTED = 3, 6978a7ac26SJames Smart FCPOP_STATE_COMPLETE = 4, 70e399441dSJames Smart }; 71e399441dSJames Smart 72e399441dSJames Smart struct nvme_fc_fcp_op { 73e399441dSJames Smart struct nvme_request nreq; /* 74e399441dSJames Smart * nvme/host/core.c 75e399441dSJames Smart * requires this to be 76e399441dSJames Smart * the 1st element in the 77e399441dSJames Smart * private structure 78e399441dSJames Smart * associated with the 79e399441dSJames Smart * request. 80e399441dSJames Smart */ 81e399441dSJames Smart struct nvmefc_fcp_req fcp_req; 82e399441dSJames Smart 83e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 84e399441dSJames Smart struct nvme_fc_queue *queue; 85e399441dSJames Smart struct request *rq; 86e399441dSJames Smart 87e399441dSJames Smart atomic_t state; 8878a7ac26SJames Smart u32 flags; 89e399441dSJames Smart u32 rqno; 90e399441dSJames Smart u32 nents; 91e399441dSJames Smart 92e399441dSJames Smart struct nvme_fc_cmd_iu cmd_iu; 93e399441dSJames Smart struct nvme_fc_ersp_iu rsp_iu; 94e399441dSJames Smart }; 95e399441dSJames Smart 96d3d0bc78SBart Van Assche struct nvme_fcp_op_w_sgl { 97d3d0bc78SBart Van Assche struct nvme_fc_fcp_op op; 98b1ae1a23SIsrael Rukshin struct scatterlist sgl[NVME_INLINE_SG_CNT]; 99d3d0bc78SBart Van Assche uint8_t priv[0]; 100d3d0bc78SBart Van Assche }; 101d3d0bc78SBart Van Assche 102e399441dSJames Smart struct nvme_fc_lport { 103e399441dSJames Smart struct nvme_fc_local_port localport; 104e399441dSJames Smart 105e399441dSJames Smart struct ida endp_cnt; 106e399441dSJames Smart struct list_head port_list; /* nvme_fc_port_list */ 107e399441dSJames Smart struct list_head endp_list; 108e399441dSJames Smart struct device *dev; /* physical device for dma */ 109e399441dSJames Smart struct nvme_fc_port_template *ops; 110e399441dSJames Smart struct kref ref; 111158bfb88SJames Smart atomic_t act_rport_cnt; 112e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 113e399441dSJames Smart 114e399441dSJames Smart struct nvme_fc_rport { 115e399441dSJames Smart struct nvme_fc_remote_port remoteport; 116e399441dSJames Smart 117e399441dSJames Smart struct list_head endp_list; /* for lport->endp_list */ 118e399441dSJames Smart struct list_head ctrl_list; 119c913a8b0SJames Smart struct list_head ls_req_list; 12097faec53SJames Smart struct list_head disc_list; 121c913a8b0SJames Smart struct device *dev; /* physical device for dma */ 122c913a8b0SJames Smart struct nvme_fc_lport *lport; 123e399441dSJames Smart spinlock_t lock; 124e399441dSJames Smart struct kref ref; 125158bfb88SJames Smart atomic_t act_ctrl_cnt; 1262b632970SJames Smart unsigned long dev_loss_end; 127e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 128e399441dSJames Smart 12961bff8efSJames Smart enum nvme_fcctrl_flags { 13061bff8efSJames Smart FCCTRL_TERMIO = (1 << 0), 131e399441dSJames Smart }; 132e399441dSJames Smart 133e399441dSJames Smart struct nvme_fc_ctrl { 134e399441dSJames Smart spinlock_t lock; 135e399441dSJames Smart struct nvme_fc_queue *queues; 136e399441dSJames Smart struct device *dev; 137e399441dSJames Smart struct nvme_fc_lport *lport; 138e399441dSJames Smart struct nvme_fc_rport *rport; 139e399441dSJames Smart u32 cnum; 140e399441dSJames Smart 1414c984154SJames Smart bool ioq_live; 142158bfb88SJames Smart bool assoc_active; 1434cff280aSJames Smart atomic_t err_work_active; 144e399441dSJames Smart u64 association_id; 145e399441dSJames Smart 146e399441dSJames Smart struct list_head ctrl_list; /* rport->ctrl_list */ 147e399441dSJames Smart 148e399441dSJames Smart struct blk_mq_tag_set admin_tag_set; 149e399441dSJames Smart struct blk_mq_tag_set tag_set; 150e399441dSJames Smart 15161bff8efSJames Smart struct delayed_work connect_work; 1524cff280aSJames Smart struct work_struct err_work; 15361bff8efSJames Smart 154e399441dSJames Smart struct kref ref; 15561bff8efSJames Smart u32 flags; 15661bff8efSJames Smart u32 iocnt; 15736715cf4SJames Smart wait_queue_head_t ioabort_wait; 158e399441dSJames Smart 15938dabe21SKeith Busch struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS]; 160e399441dSJames Smart 161e399441dSJames Smart struct nvme_ctrl ctrl; 162e399441dSJames Smart }; 163e399441dSJames Smart 164e399441dSJames Smart static inline struct nvme_fc_ctrl * 165e399441dSJames Smart to_fc_ctrl(struct nvme_ctrl *ctrl) 166e399441dSJames Smart { 167e399441dSJames Smart return container_of(ctrl, struct nvme_fc_ctrl, ctrl); 168e399441dSJames Smart } 169e399441dSJames Smart 170e399441dSJames Smart static inline struct nvme_fc_lport * 171e399441dSJames Smart localport_to_lport(struct nvme_fc_local_port *portptr) 172e399441dSJames Smart { 173e399441dSJames Smart return container_of(portptr, struct nvme_fc_lport, localport); 174e399441dSJames Smart } 175e399441dSJames Smart 176e399441dSJames Smart static inline struct nvme_fc_rport * 177e399441dSJames Smart remoteport_to_rport(struct nvme_fc_remote_port *portptr) 178e399441dSJames Smart { 179e399441dSJames Smart return container_of(portptr, struct nvme_fc_rport, remoteport); 180e399441dSJames Smart } 181e399441dSJames Smart 182e399441dSJames Smart static inline struct nvmefc_ls_req_op * 183e399441dSJames Smart ls_req_to_lsop(struct nvmefc_ls_req *lsreq) 184e399441dSJames Smart { 185e399441dSJames Smart return container_of(lsreq, struct nvmefc_ls_req_op, ls_req); 186e399441dSJames Smart } 187e399441dSJames Smart 188e399441dSJames Smart static inline struct nvme_fc_fcp_op * 189e399441dSJames Smart fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq) 190e399441dSJames Smart { 191e399441dSJames Smart return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req); 192e399441dSJames Smart } 193e399441dSJames Smart 194e399441dSJames Smart 195e399441dSJames Smart 196e399441dSJames Smart /* *************************** Globals **************************** */ 197e399441dSJames Smart 198e399441dSJames Smart 199e399441dSJames Smart static DEFINE_SPINLOCK(nvme_fc_lock); 200e399441dSJames Smart 201e399441dSJames Smart static LIST_HEAD(nvme_fc_lport_list); 202e399441dSJames Smart static DEFINE_IDA(nvme_fc_local_port_cnt); 203e399441dSJames Smart static DEFINE_IDA(nvme_fc_ctrl_cnt); 204e399441dSJames Smart 2058730c1ddSHannes Reinecke static struct workqueue_struct *nvme_fc_wq; 206e399441dSJames Smart 2074c73cbdfSJames Smart static bool nvme_fc_waiting_to_unload; 2084c73cbdfSJames Smart static DECLARE_COMPLETION(nvme_fc_unload_proceed); 2094c73cbdfSJames Smart 2105f568556SJames Smart /* 2115f568556SJames Smart * These items are short-term. They will eventually be moved into 2125f568556SJames Smart * a generic FC class. See comments in module init. 2135f568556SJames Smart */ 2145f568556SJames Smart static struct device *fc_udev_device; 2155f568556SJames Smart 216e399441dSJames Smart 217e399441dSJames Smart /* *********************** FC-NVME Port Management ************************ */ 218e399441dSJames Smart 219e399441dSJames Smart static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *, 220e399441dSJames Smart struct nvme_fc_queue *, unsigned int); 221e399441dSJames Smart 2225533d424SJames Smart static void 2235533d424SJames Smart nvme_fc_free_lport(struct kref *ref) 2245533d424SJames Smart { 2255533d424SJames Smart struct nvme_fc_lport *lport = 2265533d424SJames Smart container_of(ref, struct nvme_fc_lport, ref); 2275533d424SJames Smart unsigned long flags; 2285533d424SJames Smart 2295533d424SJames Smart WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED); 2305533d424SJames Smart WARN_ON(!list_empty(&lport->endp_list)); 2315533d424SJames Smart 2325533d424SJames Smart /* remove from transport list */ 2335533d424SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 2345533d424SJames Smart list_del(&lport->port_list); 2354c73cbdfSJames Smart if (nvme_fc_waiting_to_unload && list_empty(&nvme_fc_lport_list)) 2364c73cbdfSJames Smart complete(&nvme_fc_unload_proceed); 2375533d424SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 2385533d424SJames Smart 2395533d424SJames Smart ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num); 2405533d424SJames Smart ida_destroy(&lport->endp_cnt); 2415533d424SJames Smart 2425533d424SJames Smart put_device(lport->dev); 2435533d424SJames Smart 2445533d424SJames Smart kfree(lport); 2455533d424SJames Smart } 2465533d424SJames Smart 2475533d424SJames Smart static void 2485533d424SJames Smart nvme_fc_lport_put(struct nvme_fc_lport *lport) 2495533d424SJames Smart { 2505533d424SJames Smart kref_put(&lport->ref, nvme_fc_free_lport); 2515533d424SJames Smart } 2525533d424SJames Smart 2535533d424SJames Smart static int 2545533d424SJames Smart nvme_fc_lport_get(struct nvme_fc_lport *lport) 2555533d424SJames Smart { 2565533d424SJames Smart return kref_get_unless_zero(&lport->ref); 2575533d424SJames Smart } 2585533d424SJames Smart 2595533d424SJames Smart 2605533d424SJames Smart static struct nvme_fc_lport * 261c5760f30SJames Smart nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo, 262c5760f30SJames Smart struct nvme_fc_port_template *ops, 263c5760f30SJames Smart struct device *dev) 2645533d424SJames Smart { 2655533d424SJames Smart struct nvme_fc_lport *lport; 2665533d424SJames Smart unsigned long flags; 2675533d424SJames Smart 2685533d424SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 2695533d424SJames Smart 2705533d424SJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { 2715533d424SJames Smart if (lport->localport.node_name != pinfo->node_name || 2725533d424SJames Smart lport->localport.port_name != pinfo->port_name) 2735533d424SJames Smart continue; 2745533d424SJames Smart 275c5760f30SJames Smart if (lport->dev != dev) { 276c5760f30SJames Smart lport = ERR_PTR(-EXDEV); 277c5760f30SJames Smart goto out_done; 278c5760f30SJames Smart } 279c5760f30SJames Smart 2805533d424SJames Smart if (lport->localport.port_state != FC_OBJSTATE_DELETED) { 2815533d424SJames Smart lport = ERR_PTR(-EEXIST); 2825533d424SJames Smart goto out_done; 2835533d424SJames Smart } 2845533d424SJames Smart 2855533d424SJames Smart if (!nvme_fc_lport_get(lport)) { 2865533d424SJames Smart /* 2875533d424SJames Smart * fails if ref cnt already 0. If so, 2885533d424SJames Smart * act as if lport already deleted 2895533d424SJames Smart */ 2905533d424SJames Smart lport = NULL; 2915533d424SJames Smart goto out_done; 2925533d424SJames Smart } 2935533d424SJames Smart 2945533d424SJames Smart /* resume the lport */ 2955533d424SJames Smart 296c5760f30SJames Smart lport->ops = ops; 2975533d424SJames Smart lport->localport.port_role = pinfo->port_role; 2985533d424SJames Smart lport->localport.port_id = pinfo->port_id; 2995533d424SJames Smart lport->localport.port_state = FC_OBJSTATE_ONLINE; 3005533d424SJames Smart 3015533d424SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 3025533d424SJames Smart 3035533d424SJames Smart return lport; 3045533d424SJames Smart } 3055533d424SJames Smart 3065533d424SJames Smart lport = NULL; 3075533d424SJames Smart 3085533d424SJames Smart out_done: 3095533d424SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 3105533d424SJames Smart 3115533d424SJames Smart return lport; 3125533d424SJames Smart } 313e399441dSJames Smart 314e399441dSJames Smart /** 315e399441dSJames Smart * nvme_fc_register_localport - transport entry point called by an 316e399441dSJames Smart * LLDD to register the existence of a NVME 317e399441dSJames Smart * host FC port. 318e399441dSJames Smart * @pinfo: pointer to information about the port to be registered 319e399441dSJames Smart * @template: LLDD entrypoints and operational parameters for the port 320e399441dSJames Smart * @dev: physical hardware device node port corresponds to. Will be 321e399441dSJames Smart * used for DMA mappings 32276c910c7SBart Van Assche * @portptr: pointer to a local port pointer. Upon success, the routine 323e399441dSJames Smart * will allocate a nvme_fc_local_port structure and place its 324e399441dSJames Smart * address in the local port pointer. Upon failure, local port 325e399441dSJames Smart * pointer will be set to 0. 326e399441dSJames Smart * 327e399441dSJames Smart * Returns: 328e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 329e399441dSJames Smart * (ex: -ENXIO) upon failure. 330e399441dSJames Smart */ 331e399441dSJames Smart int 332e399441dSJames Smart nvme_fc_register_localport(struct nvme_fc_port_info *pinfo, 333e399441dSJames Smart struct nvme_fc_port_template *template, 334e399441dSJames Smart struct device *dev, 335e399441dSJames Smart struct nvme_fc_local_port **portptr) 336e399441dSJames Smart { 337e399441dSJames Smart struct nvme_fc_lport *newrec; 338e399441dSJames Smart unsigned long flags; 339e399441dSJames Smart int ret, idx; 340e399441dSJames Smart 341e399441dSJames Smart if (!template->localport_delete || !template->remoteport_delete || 342e399441dSJames Smart !template->ls_req || !template->fcp_io || 343e399441dSJames Smart !template->ls_abort || !template->fcp_abort || 344e399441dSJames Smart !template->max_hw_queues || !template->max_sgl_segments || 3458c5c6605SJames Smart !template->max_dif_sgl_segments || !template->dma_boundary) { 346e399441dSJames Smart ret = -EINVAL; 347e399441dSJames Smart goto out_reghost_failed; 348e399441dSJames Smart } 349e399441dSJames Smart 3505533d424SJames Smart /* 3515533d424SJames Smart * look to see if there is already a localport that had been 3525533d424SJames Smart * deregistered and in the process of waiting for all the 3535533d424SJames Smart * references to fully be removed. If the references haven't 3545533d424SJames Smart * expired, we can simply re-enable the localport. Remoteports 3555533d424SJames Smart * and controller reconnections should resume naturally. 3565533d424SJames Smart */ 357c5760f30SJames Smart newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev); 3585533d424SJames Smart 3595533d424SJames Smart /* found an lport, but something about its state is bad */ 3605533d424SJames Smart if (IS_ERR(newrec)) { 3615533d424SJames Smart ret = PTR_ERR(newrec); 3625533d424SJames Smart goto out_reghost_failed; 3635533d424SJames Smart 3645533d424SJames Smart /* found existing lport, which was resumed */ 3655533d424SJames Smart } else if (newrec) { 3665533d424SJames Smart *portptr = &newrec->localport; 3675533d424SJames Smart return 0; 3685533d424SJames Smart } 3695533d424SJames Smart 3705533d424SJames Smart /* nothing found - allocate a new localport struct */ 3715533d424SJames Smart 372e399441dSJames Smart newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz), 373e399441dSJames Smart GFP_KERNEL); 374e399441dSJames Smart if (!newrec) { 375e399441dSJames Smart ret = -ENOMEM; 376e399441dSJames Smart goto out_reghost_failed; 377e399441dSJames Smart } 378e399441dSJames Smart 379e399441dSJames Smart idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL); 380e399441dSJames Smart if (idx < 0) { 381e399441dSJames Smart ret = -ENOSPC; 382e399441dSJames Smart goto out_fail_kfree; 383e399441dSJames Smart } 384e399441dSJames Smart 385e399441dSJames Smart if (!get_device(dev) && dev) { 386e399441dSJames Smart ret = -ENODEV; 387e399441dSJames Smart goto out_ida_put; 388e399441dSJames Smart } 389e399441dSJames Smart 390e399441dSJames Smart INIT_LIST_HEAD(&newrec->port_list); 391e399441dSJames Smart INIT_LIST_HEAD(&newrec->endp_list); 392e399441dSJames Smart kref_init(&newrec->ref); 393158bfb88SJames Smart atomic_set(&newrec->act_rport_cnt, 0); 394e399441dSJames Smart newrec->ops = template; 395e399441dSJames Smart newrec->dev = dev; 396e399441dSJames Smart ida_init(&newrec->endp_cnt); 397e399441dSJames Smart newrec->localport.private = &newrec[1]; 398e399441dSJames Smart newrec->localport.node_name = pinfo->node_name; 399e399441dSJames Smart newrec->localport.port_name = pinfo->port_name; 400e399441dSJames Smart newrec->localport.port_role = pinfo->port_role; 401e399441dSJames Smart newrec->localport.port_id = pinfo->port_id; 402e399441dSJames Smart newrec->localport.port_state = FC_OBJSTATE_ONLINE; 403e399441dSJames Smart newrec->localport.port_num = idx; 404e399441dSJames Smart 405e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 406e399441dSJames Smart list_add_tail(&newrec->port_list, &nvme_fc_lport_list); 407e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 408e399441dSJames Smart 409e399441dSJames Smart if (dev) 410e399441dSJames Smart dma_set_seg_boundary(dev, template->dma_boundary); 411e399441dSJames Smart 412e399441dSJames Smart *portptr = &newrec->localport; 413e399441dSJames Smart return 0; 414e399441dSJames Smart 415e399441dSJames Smart out_ida_put: 416e399441dSJames Smart ida_simple_remove(&nvme_fc_local_port_cnt, idx); 417e399441dSJames Smart out_fail_kfree: 418e399441dSJames Smart kfree(newrec); 419e399441dSJames Smart out_reghost_failed: 420e399441dSJames Smart *portptr = NULL; 421e399441dSJames Smart 422e399441dSJames Smart return ret; 423e399441dSJames Smart } 424e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_localport); 425e399441dSJames Smart 426e399441dSJames Smart /** 427e399441dSJames Smart * nvme_fc_unregister_localport - transport entry point called by an 428e399441dSJames Smart * LLDD to deregister/remove a previously 429e399441dSJames Smart * registered a NVME host FC port. 43076c910c7SBart Van Assche * @portptr: pointer to the (registered) local port that is to be deregistered. 431e399441dSJames Smart * 432e399441dSJames Smart * Returns: 433e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 434e399441dSJames Smart * (ex: -ENXIO) upon failure. 435e399441dSJames Smart */ 436e399441dSJames Smart int 437e399441dSJames Smart nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr) 438e399441dSJames Smart { 439e399441dSJames Smart struct nvme_fc_lport *lport = localport_to_lport(portptr); 440e399441dSJames Smart unsigned long flags; 441e399441dSJames Smart 442e399441dSJames Smart if (!portptr) 443e399441dSJames Smart return -EINVAL; 444e399441dSJames Smart 445e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 446e399441dSJames Smart 447e399441dSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) { 448e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 449e399441dSJames Smart return -EINVAL; 450e399441dSJames Smart } 451e399441dSJames Smart portptr->port_state = FC_OBJSTATE_DELETED; 452e399441dSJames Smart 453e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 454e399441dSJames Smart 455158bfb88SJames Smart if (atomic_read(&lport->act_rport_cnt) == 0) 456158bfb88SJames Smart lport->ops->localport_delete(&lport->localport); 457158bfb88SJames Smart 458e399441dSJames Smart nvme_fc_lport_put(lport); 459e399441dSJames Smart 460e399441dSJames Smart return 0; 461e399441dSJames Smart } 462e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport); 463e399441dSJames Smart 464eaefd5abSJames Smart /* 465eaefd5abSJames Smart * TRADDR strings, per FC-NVME are fixed format: 466eaefd5abSJames Smart * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters 467eaefd5abSJames Smart * udev event will only differ by prefix of what field is 468eaefd5abSJames Smart * being specified: 469eaefd5abSJames Smart * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters 470eaefd5abSJames Smart * 19 + 43 + null_fudge = 64 characters 471eaefd5abSJames Smart */ 472eaefd5abSJames Smart #define FCNVME_TRADDR_LENGTH 64 473eaefd5abSJames Smart 474eaefd5abSJames Smart static void 475eaefd5abSJames Smart nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport, 476eaefd5abSJames Smart struct nvme_fc_rport *rport) 477eaefd5abSJames Smart { 478eaefd5abSJames Smart char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/ 479eaefd5abSJames Smart char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/ 480eaefd5abSJames Smart char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL }; 481eaefd5abSJames Smart 482eaefd5abSJames Smart if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY)) 483eaefd5abSJames Smart return; 484eaefd5abSJames Smart 485eaefd5abSJames Smart snprintf(hostaddr, sizeof(hostaddr), 486eaefd5abSJames Smart "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx", 487eaefd5abSJames Smart lport->localport.node_name, lport->localport.port_name); 488eaefd5abSJames Smart snprintf(tgtaddr, sizeof(tgtaddr), 489eaefd5abSJames Smart "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx", 490eaefd5abSJames Smart rport->remoteport.node_name, rport->remoteport.port_name); 491eaefd5abSJames Smart kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp); 492eaefd5abSJames Smart } 493eaefd5abSJames Smart 494469d0ef0SJames Smart static void 495469d0ef0SJames Smart nvme_fc_free_rport(struct kref *ref) 496469d0ef0SJames Smart { 497469d0ef0SJames Smart struct nvme_fc_rport *rport = 498469d0ef0SJames Smart container_of(ref, struct nvme_fc_rport, ref); 499469d0ef0SJames Smart struct nvme_fc_lport *lport = 500469d0ef0SJames Smart localport_to_lport(rport->remoteport.localport); 501469d0ef0SJames Smart unsigned long flags; 502469d0ef0SJames Smart 503469d0ef0SJames Smart WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED); 504469d0ef0SJames Smart WARN_ON(!list_empty(&rport->ctrl_list)); 505469d0ef0SJames Smart 506469d0ef0SJames Smart /* remove from lport list */ 507469d0ef0SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 508469d0ef0SJames Smart list_del(&rport->endp_list); 509469d0ef0SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 510469d0ef0SJames Smart 51197faec53SJames Smart WARN_ON(!list_empty(&rport->disc_list)); 512469d0ef0SJames Smart ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num); 513469d0ef0SJames Smart 514469d0ef0SJames Smart kfree(rport); 515469d0ef0SJames Smart 516469d0ef0SJames Smart nvme_fc_lport_put(lport); 517469d0ef0SJames Smart } 518469d0ef0SJames Smart 519469d0ef0SJames Smart static void 520469d0ef0SJames Smart nvme_fc_rport_put(struct nvme_fc_rport *rport) 521469d0ef0SJames Smart { 522469d0ef0SJames Smart kref_put(&rport->ref, nvme_fc_free_rport); 523469d0ef0SJames Smart } 524469d0ef0SJames Smart 525469d0ef0SJames Smart static int 526469d0ef0SJames Smart nvme_fc_rport_get(struct nvme_fc_rport *rport) 527469d0ef0SJames Smart { 528469d0ef0SJames Smart return kref_get_unless_zero(&rport->ref); 529469d0ef0SJames Smart } 530469d0ef0SJames Smart 5312b632970SJames Smart static void 5322b632970SJames Smart nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl) 5332b632970SJames Smart { 5342b632970SJames Smart switch (ctrl->ctrl.state) { 5352b632970SJames Smart case NVME_CTRL_NEW: 536ad6a0a52SMax Gurtovoy case NVME_CTRL_CONNECTING: 5372b632970SJames Smart /* 5382b632970SJames Smart * As all reconnects were suppressed, schedule a 5392b632970SJames Smart * connect. 5402b632970SJames Smart */ 5412b632970SJames Smart dev_info(ctrl->ctrl.device, 5422b632970SJames Smart "NVME-FC{%d}: connectivity re-established. " 5432b632970SJames Smart "Attempting reconnect\n", ctrl->cnum); 5442b632970SJames Smart 5452b632970SJames Smart queue_delayed_work(nvme_wq, &ctrl->connect_work, 0); 5462b632970SJames Smart break; 5472b632970SJames Smart 5482b632970SJames Smart case NVME_CTRL_RESETTING: 5492b632970SJames Smart /* 5502b632970SJames Smart * Controller is already in the process of terminating the 5512b632970SJames Smart * association. No need to do anything further. The reconnect 5522b632970SJames Smart * step will naturally occur after the reset completes. 5532b632970SJames Smart */ 5542b632970SJames Smart break; 5552b632970SJames Smart 5562b632970SJames Smart default: 5572b632970SJames Smart /* no action to take - let it delete */ 5582b632970SJames Smart break; 5592b632970SJames Smart } 5602b632970SJames Smart } 5612b632970SJames Smart 5622b632970SJames Smart static struct nvme_fc_rport * 5632b632970SJames Smart nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport, 5642b632970SJames Smart struct nvme_fc_port_info *pinfo) 5652b632970SJames Smart { 5662b632970SJames Smart struct nvme_fc_rport *rport; 5672b632970SJames Smart struct nvme_fc_ctrl *ctrl; 5682b632970SJames Smart unsigned long flags; 5692b632970SJames Smart 5702b632970SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 5712b632970SJames Smart 5722b632970SJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) { 5732b632970SJames Smart if (rport->remoteport.node_name != pinfo->node_name || 5742b632970SJames Smart rport->remoteport.port_name != pinfo->port_name) 5752b632970SJames Smart continue; 5762b632970SJames Smart 5772b632970SJames Smart if (!nvme_fc_rport_get(rport)) { 5782b632970SJames Smart rport = ERR_PTR(-ENOLCK); 5792b632970SJames Smart goto out_done; 5802b632970SJames Smart } 5812b632970SJames Smart 5822b632970SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 5832b632970SJames Smart 5842b632970SJames Smart spin_lock_irqsave(&rport->lock, flags); 5852b632970SJames Smart 5862b632970SJames Smart /* has it been unregistered */ 5872b632970SJames Smart if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) { 5882b632970SJames Smart /* means lldd called us twice */ 5892b632970SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 5902b632970SJames Smart nvme_fc_rport_put(rport); 5912b632970SJames Smart return ERR_PTR(-ESTALE); 5922b632970SJames Smart } 5932b632970SJames Smart 5940cdd5fcaSJames Smart rport->remoteport.port_role = pinfo->port_role; 5950cdd5fcaSJames Smart rport->remoteport.port_id = pinfo->port_id; 5962b632970SJames Smart rport->remoteport.port_state = FC_OBJSTATE_ONLINE; 5972b632970SJames Smart rport->dev_loss_end = 0; 5982b632970SJames Smart 5992b632970SJames Smart /* 6002b632970SJames Smart * kick off a reconnect attempt on all associations to the 6012b632970SJames Smart * remote port. A successful reconnects will resume i/o. 6022b632970SJames Smart */ 6032b632970SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) 6042b632970SJames Smart nvme_fc_resume_controller(ctrl); 6052b632970SJames Smart 6062b632970SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 6072b632970SJames Smart 6082b632970SJames Smart return rport; 6092b632970SJames Smart } 6102b632970SJames Smart 6112b632970SJames Smart rport = NULL; 6122b632970SJames Smart 6132b632970SJames Smart out_done: 6142b632970SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 6152b632970SJames Smart 6162b632970SJames Smart return rport; 6172b632970SJames Smart } 6182b632970SJames Smart 6192b632970SJames Smart static inline void 6202b632970SJames Smart __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport, 6212b632970SJames Smart struct nvme_fc_port_info *pinfo) 6222b632970SJames Smart { 6232b632970SJames Smart if (pinfo->dev_loss_tmo) 6242b632970SJames Smart rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo; 6252b632970SJames Smart else 6262b632970SJames Smart rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO; 6272b632970SJames Smart } 6282b632970SJames Smart 629e399441dSJames Smart /** 630e399441dSJames Smart * nvme_fc_register_remoteport - transport entry point called by an 631e399441dSJames Smart * LLDD to register the existence of a NVME 632e399441dSJames Smart * subsystem FC port on its fabric. 633e399441dSJames Smart * @localport: pointer to the (registered) local port that the remote 634e399441dSJames Smart * subsystem port is connected to. 635e399441dSJames Smart * @pinfo: pointer to information about the port to be registered 63676c910c7SBart Van Assche * @portptr: pointer to a remote port pointer. Upon success, the routine 637e399441dSJames Smart * will allocate a nvme_fc_remote_port structure and place its 638e399441dSJames Smart * address in the remote port pointer. Upon failure, remote port 639e399441dSJames Smart * pointer will be set to 0. 640e399441dSJames Smart * 641e399441dSJames Smart * Returns: 642e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 643e399441dSJames Smart * (ex: -ENXIO) upon failure. 644e399441dSJames Smart */ 645e399441dSJames Smart int 646e399441dSJames Smart nvme_fc_register_remoteport(struct nvme_fc_local_port *localport, 647e399441dSJames Smart struct nvme_fc_port_info *pinfo, 648e399441dSJames Smart struct nvme_fc_remote_port **portptr) 649e399441dSJames Smart { 650e399441dSJames Smart struct nvme_fc_lport *lport = localport_to_lport(localport); 651e399441dSJames Smart struct nvme_fc_rport *newrec; 652e399441dSJames Smart unsigned long flags; 653e399441dSJames Smart int ret, idx; 654e399441dSJames Smart 6552b632970SJames Smart if (!nvme_fc_lport_get(lport)) { 6562b632970SJames Smart ret = -ESHUTDOWN; 6572b632970SJames Smart goto out_reghost_failed; 6582b632970SJames Smart } 6592b632970SJames Smart 6602b632970SJames Smart /* 6612b632970SJames Smart * look to see if there is already a remoteport that is waiting 6622b632970SJames Smart * for a reconnect (within dev_loss_tmo) with the same WWN's. 6632b632970SJames Smart * If so, transition to it and reconnect. 6642b632970SJames Smart */ 6652b632970SJames Smart newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo); 6662b632970SJames Smart 6672b632970SJames Smart /* found an rport, but something about its state is bad */ 6682b632970SJames Smart if (IS_ERR(newrec)) { 6692b632970SJames Smart ret = PTR_ERR(newrec); 6702b632970SJames Smart goto out_lport_put; 6712b632970SJames Smart 6722b632970SJames Smart /* found existing rport, which was resumed */ 6732b632970SJames Smart } else if (newrec) { 6742b632970SJames Smart nvme_fc_lport_put(lport); 6752b632970SJames Smart __nvme_fc_set_dev_loss_tmo(newrec, pinfo); 6762b632970SJames Smart nvme_fc_signal_discovery_scan(lport, newrec); 6772b632970SJames Smart *portptr = &newrec->remoteport; 6782b632970SJames Smart return 0; 6792b632970SJames Smart } 6802b632970SJames Smart 6812b632970SJames Smart /* nothing found - allocate a new remoteport struct */ 6822b632970SJames Smart 683e399441dSJames Smart newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz), 684e399441dSJames Smart GFP_KERNEL); 685e399441dSJames Smart if (!newrec) { 686e399441dSJames Smart ret = -ENOMEM; 6872b632970SJames Smart goto out_lport_put; 688e399441dSJames Smart } 689e399441dSJames Smart 690e399441dSJames Smart idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL); 691e399441dSJames Smart if (idx < 0) { 692e399441dSJames Smart ret = -ENOSPC; 6932b632970SJames Smart goto out_kfree_rport; 694e399441dSJames Smart } 695e399441dSJames Smart 696e399441dSJames Smart INIT_LIST_HEAD(&newrec->endp_list); 697e399441dSJames Smart INIT_LIST_HEAD(&newrec->ctrl_list); 698c913a8b0SJames Smart INIT_LIST_HEAD(&newrec->ls_req_list); 69997faec53SJames Smart INIT_LIST_HEAD(&newrec->disc_list); 700e399441dSJames Smart kref_init(&newrec->ref); 701158bfb88SJames Smart atomic_set(&newrec->act_ctrl_cnt, 0); 702e399441dSJames Smart spin_lock_init(&newrec->lock); 703e399441dSJames Smart newrec->remoteport.localport = &lport->localport; 704c913a8b0SJames Smart newrec->dev = lport->dev; 705c913a8b0SJames Smart newrec->lport = lport; 706e399441dSJames Smart newrec->remoteport.private = &newrec[1]; 707e399441dSJames Smart newrec->remoteport.port_role = pinfo->port_role; 708e399441dSJames Smart newrec->remoteport.node_name = pinfo->node_name; 709e399441dSJames Smart newrec->remoteport.port_name = pinfo->port_name; 710e399441dSJames Smart newrec->remoteport.port_id = pinfo->port_id; 711e399441dSJames Smart newrec->remoteport.port_state = FC_OBJSTATE_ONLINE; 712e399441dSJames Smart newrec->remoteport.port_num = idx; 7132b632970SJames Smart __nvme_fc_set_dev_loss_tmo(newrec, pinfo); 714e399441dSJames Smart 715e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 716e399441dSJames Smart list_add_tail(&newrec->endp_list, &lport->endp_list); 717e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 718e399441dSJames Smart 719eaefd5abSJames Smart nvme_fc_signal_discovery_scan(lport, newrec); 720eaefd5abSJames Smart 721e399441dSJames Smart *portptr = &newrec->remoteport; 722e399441dSJames Smart return 0; 723e399441dSJames Smart 724e399441dSJames Smart out_kfree_rport: 725e399441dSJames Smart kfree(newrec); 7262b632970SJames Smart out_lport_put: 7272b632970SJames Smart nvme_fc_lport_put(lport); 728e399441dSJames Smart out_reghost_failed: 729e399441dSJames Smart *portptr = NULL; 730e399441dSJames Smart return ret; 731e399441dSJames Smart } 732e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport); 733e399441dSJames Smart 7348d64daf7SJames Smart static int 7358d64daf7SJames Smart nvme_fc_abort_lsops(struct nvme_fc_rport *rport) 7368d64daf7SJames Smart { 7378d64daf7SJames Smart struct nvmefc_ls_req_op *lsop; 7388d64daf7SJames Smart unsigned long flags; 7398d64daf7SJames Smart 7408d64daf7SJames Smart restart: 7418d64daf7SJames Smart spin_lock_irqsave(&rport->lock, flags); 7428d64daf7SJames Smart 7438d64daf7SJames Smart list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) { 7448d64daf7SJames Smart if (!(lsop->flags & FCOP_FLAGS_TERMIO)) { 7458d64daf7SJames Smart lsop->flags |= FCOP_FLAGS_TERMIO; 7468d64daf7SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 7478d64daf7SJames Smart rport->lport->ops->ls_abort(&rport->lport->localport, 7488d64daf7SJames Smart &rport->remoteport, 7498d64daf7SJames Smart &lsop->ls_req); 7508d64daf7SJames Smart goto restart; 7518d64daf7SJames Smart } 7528d64daf7SJames Smart } 7538d64daf7SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 7548d64daf7SJames Smart 7558d64daf7SJames Smart return 0; 7568d64daf7SJames Smart } 7578d64daf7SJames Smart 7582b632970SJames Smart static void 7592b632970SJames Smart nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl) 7602b632970SJames Smart { 7612b632970SJames Smart dev_info(ctrl->ctrl.device, 7622b632970SJames Smart "NVME-FC{%d}: controller connectivity lost. Awaiting " 7632b632970SJames Smart "Reconnect", ctrl->cnum); 7642b632970SJames Smart 7652b632970SJames Smart switch (ctrl->ctrl.state) { 7662b632970SJames Smart case NVME_CTRL_NEW: 7672b632970SJames Smart case NVME_CTRL_LIVE: 7682b632970SJames Smart /* 7692b632970SJames Smart * Schedule a controller reset. The reset will terminate the 7702b632970SJames Smart * association and schedule the reconnect timer. Reconnects 7712b632970SJames Smart * will be attempted until either the ctlr_loss_tmo 7722b632970SJames Smart * (max_retries * connect_delay) expires or the remoteport's 7732b632970SJames Smart * dev_loss_tmo expires. 7742b632970SJames Smart */ 7752b632970SJames Smart if (nvme_reset_ctrl(&ctrl->ctrl)) { 7762b632970SJames Smart dev_warn(ctrl->ctrl.device, 77777d0612dSMax Gurtovoy "NVME-FC{%d}: Couldn't schedule reset.\n", 7782b632970SJames Smart ctrl->cnum); 7792b632970SJames Smart nvme_delete_ctrl(&ctrl->ctrl); 7802b632970SJames Smart } 7812b632970SJames Smart break; 7822b632970SJames Smart 783ad6a0a52SMax Gurtovoy case NVME_CTRL_CONNECTING: 7842b632970SJames Smart /* 7852b632970SJames Smart * The association has already been terminated and the 7862b632970SJames Smart * controller is attempting reconnects. No need to do anything 7872b632970SJames Smart * futher. Reconnects will be attempted until either the 7882b632970SJames Smart * ctlr_loss_tmo (max_retries * connect_delay) expires or the 7892b632970SJames Smart * remoteport's dev_loss_tmo expires. 7902b632970SJames Smart */ 7912b632970SJames Smart break; 7922b632970SJames Smart 7932b632970SJames Smart case NVME_CTRL_RESETTING: 7942b632970SJames Smart /* 7952b632970SJames Smart * Controller is already in the process of terminating the 7962b632970SJames Smart * association. No need to do anything further. The reconnect 7972b632970SJames Smart * step will kick in naturally after the association is 7982b632970SJames Smart * terminated. 7992b632970SJames Smart */ 8002b632970SJames Smart break; 8012b632970SJames Smart 8022b632970SJames Smart case NVME_CTRL_DELETING: 8032b632970SJames Smart default: 8042b632970SJames Smart /* no action to take - let it delete */ 8052b632970SJames Smart break; 8062b632970SJames Smart } 8072b632970SJames Smart } 8082b632970SJames Smart 809e399441dSJames Smart /** 810e399441dSJames Smart * nvme_fc_unregister_remoteport - transport entry point called by an 811e399441dSJames Smart * LLDD to deregister/remove a previously 812e399441dSJames Smart * registered a NVME subsystem FC port. 81376c910c7SBart Van Assche * @portptr: pointer to the (registered) remote port that is to be 814e399441dSJames Smart * deregistered. 815e399441dSJames Smart * 816e399441dSJames Smart * Returns: 817e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 818e399441dSJames Smart * (ex: -ENXIO) upon failure. 819e399441dSJames Smart */ 820e399441dSJames Smart int 821e399441dSJames Smart nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr) 822e399441dSJames Smart { 823e399441dSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(portptr); 824e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 825e399441dSJames Smart unsigned long flags; 826e399441dSJames Smart 827e399441dSJames Smart if (!portptr) 828e399441dSJames Smart return -EINVAL; 829e399441dSJames Smart 830e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags); 831e399441dSJames Smart 832e399441dSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) { 833e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 834e399441dSJames Smart return -EINVAL; 835e399441dSJames Smart } 836e399441dSJames Smart portptr->port_state = FC_OBJSTATE_DELETED; 837e399441dSJames Smart 8382b632970SJames Smart rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ); 8392b632970SJames Smart 8402b632970SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) { 8412b632970SJames Smart /* if dev_loss_tmo==0, dev loss is immediate */ 8422b632970SJames Smart if (!portptr->dev_loss_tmo) { 8432b632970SJames Smart dev_warn(ctrl->ctrl.device, 84477d0612dSMax Gurtovoy "NVME-FC{%d}: controller connectivity lost.\n", 8452b632970SJames Smart ctrl->cnum); 846c5017e85SChristoph Hellwig nvme_delete_ctrl(&ctrl->ctrl); 8472b632970SJames Smart } else 8482b632970SJames Smart nvme_fc_ctrl_connectivity_loss(ctrl); 8492b632970SJames Smart } 850e399441dSJames Smart 851e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 852e399441dSJames Smart 8538d64daf7SJames Smart nvme_fc_abort_lsops(rport); 8548d64daf7SJames Smart 855158bfb88SJames Smart if (atomic_read(&rport->act_ctrl_cnt) == 0) 856158bfb88SJames Smart rport->lport->ops->remoteport_delete(portptr); 857158bfb88SJames Smart 8582b632970SJames Smart /* 8592b632970SJames Smart * release the reference, which will allow, if all controllers 8602b632970SJames Smart * go away, which should only occur after dev_loss_tmo occurs, 8612b632970SJames Smart * for the rport to be torn down. 8622b632970SJames Smart */ 863e399441dSJames Smart nvme_fc_rport_put(rport); 8642b632970SJames Smart 865e399441dSJames Smart return 0; 866e399441dSJames Smart } 867e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport); 868e399441dSJames Smart 869eaefd5abSJames Smart /** 870eaefd5abSJames Smart * nvme_fc_rescan_remoteport - transport entry point called by an 871eaefd5abSJames Smart * LLDD to request a nvme device rescan. 872eaefd5abSJames Smart * @remoteport: pointer to the (registered) remote port that is to be 873eaefd5abSJames Smart * rescanned. 874eaefd5abSJames Smart * 875eaefd5abSJames Smart * Returns: N/A 876eaefd5abSJames Smart */ 877eaefd5abSJames Smart void 878eaefd5abSJames Smart nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport) 879eaefd5abSJames Smart { 880eaefd5abSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(remoteport); 881eaefd5abSJames Smart 882eaefd5abSJames Smart nvme_fc_signal_discovery_scan(rport->lport, rport); 883eaefd5abSJames Smart } 884eaefd5abSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport); 885eaefd5abSJames Smart 886ac7fe82bSJames Smart int 887ac7fe82bSJames Smart nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr, 888ac7fe82bSJames Smart u32 dev_loss_tmo) 889ac7fe82bSJames Smart { 890ac7fe82bSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(portptr); 891ac7fe82bSJames Smart unsigned long flags; 892ac7fe82bSJames Smart 893ac7fe82bSJames Smart spin_lock_irqsave(&rport->lock, flags); 894ac7fe82bSJames Smart 895ac7fe82bSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) { 896ac7fe82bSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 897ac7fe82bSJames Smart return -EINVAL; 898ac7fe82bSJames Smart } 899ac7fe82bSJames Smart 900ac7fe82bSJames Smart /* a dev_loss_tmo of 0 (immediate) is allowed to be set */ 901ac7fe82bSJames Smart rport->remoteport.dev_loss_tmo = dev_loss_tmo; 902ac7fe82bSJames Smart 903ac7fe82bSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 904ac7fe82bSJames Smart 905ac7fe82bSJames Smart return 0; 906ac7fe82bSJames Smart } 907ac7fe82bSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss); 908ac7fe82bSJames Smart 909e399441dSJames Smart 910e399441dSJames Smart /* *********************** FC-NVME DMA Handling **************************** */ 911e399441dSJames Smart 912e399441dSJames Smart /* 913e399441dSJames Smart * The fcloop device passes in a NULL device pointer. Real LLD's will 914e399441dSJames Smart * pass in a valid device pointer. If NULL is passed to the dma mapping 915e399441dSJames Smart * routines, depending on the platform, it may or may not succeed, and 916e399441dSJames Smart * may crash. 917e399441dSJames Smart * 918e399441dSJames Smart * As such: 919e399441dSJames Smart * Wrapper all the dma routines and check the dev pointer. 920e399441dSJames Smart * 921e399441dSJames Smart * If simple mappings (return just a dma address, we'll noop them, 922e399441dSJames Smart * returning a dma address of 0. 923e399441dSJames Smart * 924e399441dSJames Smart * On more complex mappings (dma_map_sg), a pseudo routine fills 925e399441dSJames Smart * in the scatter list, setting all dma addresses to 0. 926e399441dSJames Smart */ 927e399441dSJames Smart 928e399441dSJames Smart static inline dma_addr_t 929e399441dSJames Smart fc_dma_map_single(struct device *dev, void *ptr, size_t size, 930e399441dSJames Smart enum dma_data_direction dir) 931e399441dSJames Smart { 932e399441dSJames Smart return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L; 933e399441dSJames Smart } 934e399441dSJames Smart 935e399441dSJames Smart static inline int 936e399441dSJames Smart fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 937e399441dSJames Smart { 938e399441dSJames Smart return dev ? dma_mapping_error(dev, dma_addr) : 0; 939e399441dSJames Smart } 940e399441dSJames Smart 941e399441dSJames Smart static inline void 942e399441dSJames Smart fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size, 943e399441dSJames Smart enum dma_data_direction dir) 944e399441dSJames Smart { 945e399441dSJames Smart if (dev) 946e399441dSJames Smart dma_unmap_single(dev, addr, size, dir); 947e399441dSJames Smart } 948e399441dSJames Smart 949e399441dSJames Smart static inline void 950e399441dSJames Smart fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size, 951e399441dSJames Smart enum dma_data_direction dir) 952e399441dSJames Smart { 953e399441dSJames Smart if (dev) 954e399441dSJames Smart dma_sync_single_for_cpu(dev, addr, size, dir); 955e399441dSJames Smart } 956e399441dSJames Smart 957e399441dSJames Smart static inline void 958e399441dSJames Smart fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size, 959e399441dSJames Smart enum dma_data_direction dir) 960e399441dSJames Smart { 961e399441dSJames Smart if (dev) 962e399441dSJames Smart dma_sync_single_for_device(dev, addr, size, dir); 963e399441dSJames Smart } 964e399441dSJames Smart 965e399441dSJames Smart /* pseudo dma_map_sg call */ 966e399441dSJames Smart static int 967e399441dSJames Smart fc_map_sg(struct scatterlist *sg, int nents) 968e399441dSJames Smart { 969e399441dSJames Smart struct scatterlist *s; 970e399441dSJames Smart int i; 971e399441dSJames Smart 972e399441dSJames Smart WARN_ON(nents == 0 || sg[0].length == 0); 973e399441dSJames Smart 974e399441dSJames Smart for_each_sg(sg, s, nents, i) { 975e399441dSJames Smart s->dma_address = 0L; 976e399441dSJames Smart #ifdef CONFIG_NEED_SG_DMA_LENGTH 977e399441dSJames Smart s->dma_length = s->length; 978e399441dSJames Smart #endif 979e399441dSJames Smart } 980e399441dSJames Smart return nents; 981e399441dSJames Smart } 982e399441dSJames Smart 983e399441dSJames Smart static inline int 984e399441dSJames Smart fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, 985e399441dSJames Smart enum dma_data_direction dir) 986e399441dSJames Smart { 987e399441dSJames Smart return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents); 988e399441dSJames Smart } 989e399441dSJames Smart 990e399441dSJames Smart static inline void 991e399441dSJames Smart fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, 992e399441dSJames Smart enum dma_data_direction dir) 993e399441dSJames Smart { 994e399441dSJames Smart if (dev) 995e399441dSJames Smart dma_unmap_sg(dev, sg, nents, dir); 996e399441dSJames Smart } 997e399441dSJames Smart 998e399441dSJames Smart /* *********************** FC-NVME LS Handling **************************** */ 999e399441dSJames Smart 1000e399441dSJames Smart static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *); 1001e399441dSJames Smart static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *); 1002e399441dSJames Smart 1003e399441dSJames Smart 1004e399441dSJames Smart static void 1005c913a8b0SJames Smart __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop) 1006e399441dSJames Smart { 1007c913a8b0SJames Smart struct nvme_fc_rport *rport = lsop->rport; 1008e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req; 1009e399441dSJames Smart unsigned long flags; 1010e399441dSJames Smart 1011c913a8b0SJames Smart spin_lock_irqsave(&rport->lock, flags); 1012e399441dSJames Smart 1013e399441dSJames Smart if (!lsop->req_queued) { 1014c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 1015e399441dSJames Smart return; 1016e399441dSJames Smart } 1017e399441dSJames Smart 1018e399441dSJames Smart list_del(&lsop->lsreq_list); 1019e399441dSJames Smart 1020e399441dSJames Smart lsop->req_queued = false; 1021e399441dSJames Smart 1022c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 1023e399441dSJames Smart 1024c913a8b0SJames Smart fc_dma_unmap_single(rport->dev, lsreq->rqstdma, 1025e399441dSJames Smart (lsreq->rqstlen + lsreq->rsplen), 1026e399441dSJames Smart DMA_BIDIRECTIONAL); 1027e399441dSJames Smart 1028c913a8b0SJames Smart nvme_fc_rport_put(rport); 1029e399441dSJames Smart } 1030e399441dSJames Smart 1031e399441dSJames Smart static int 1032c913a8b0SJames Smart __nvme_fc_send_ls_req(struct nvme_fc_rport *rport, 1033e399441dSJames Smart struct nvmefc_ls_req_op *lsop, 1034e399441dSJames Smart void (*done)(struct nvmefc_ls_req *req, int status)) 1035e399441dSJames Smart { 1036e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req; 1037e399441dSJames Smart unsigned long flags; 1038c913a8b0SJames Smart int ret = 0; 1039e399441dSJames Smart 1040c913a8b0SJames Smart if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) 1041c913a8b0SJames Smart return -ECONNREFUSED; 1042c913a8b0SJames Smart 1043c913a8b0SJames Smart if (!nvme_fc_rport_get(rport)) 1044e399441dSJames Smart return -ESHUTDOWN; 1045e399441dSJames Smart 1046e399441dSJames Smart lsreq->done = done; 1047c913a8b0SJames Smart lsop->rport = rport; 1048e399441dSJames Smart lsop->req_queued = false; 1049e399441dSJames Smart INIT_LIST_HEAD(&lsop->lsreq_list); 1050e399441dSJames Smart init_completion(&lsop->ls_done); 1051e399441dSJames Smart 1052c913a8b0SJames Smart lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr, 1053e399441dSJames Smart lsreq->rqstlen + lsreq->rsplen, 1054e399441dSJames Smart DMA_BIDIRECTIONAL); 1055c913a8b0SJames Smart if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) { 1056c913a8b0SJames Smart ret = -EFAULT; 1057c913a8b0SJames Smart goto out_putrport; 1058e399441dSJames Smart } 1059e399441dSJames Smart lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen; 1060e399441dSJames Smart 1061c913a8b0SJames Smart spin_lock_irqsave(&rport->lock, flags); 1062e399441dSJames Smart 1063c913a8b0SJames Smart list_add_tail(&lsop->lsreq_list, &rport->ls_req_list); 1064e399441dSJames Smart 1065e399441dSJames Smart lsop->req_queued = true; 1066e399441dSJames Smart 1067c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 1068e399441dSJames Smart 1069c913a8b0SJames Smart ret = rport->lport->ops->ls_req(&rport->lport->localport, 1070c913a8b0SJames Smart &rport->remoteport, lsreq); 1071e399441dSJames Smart if (ret) 1072c913a8b0SJames Smart goto out_unlink; 1073c913a8b0SJames Smart 1074c913a8b0SJames Smart return 0; 1075c913a8b0SJames Smart 1076c913a8b0SJames Smart out_unlink: 1077e399441dSJames Smart lsop->ls_error = ret; 1078c913a8b0SJames Smart spin_lock_irqsave(&rport->lock, flags); 1079c913a8b0SJames Smart lsop->req_queued = false; 1080c913a8b0SJames Smart list_del(&lsop->lsreq_list); 1081c913a8b0SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 1082c913a8b0SJames Smart fc_dma_unmap_single(rport->dev, lsreq->rqstdma, 1083c913a8b0SJames Smart (lsreq->rqstlen + lsreq->rsplen), 1084c913a8b0SJames Smart DMA_BIDIRECTIONAL); 1085c913a8b0SJames Smart out_putrport: 1086c913a8b0SJames Smart nvme_fc_rport_put(rport); 1087e399441dSJames Smart 1088e399441dSJames Smart return ret; 1089e399441dSJames Smart } 1090e399441dSJames Smart 1091e399441dSJames Smart static void 1092e399441dSJames Smart nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status) 1093e399441dSJames Smart { 1094e399441dSJames Smart struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq); 1095e399441dSJames Smart 1096e399441dSJames Smart lsop->ls_error = status; 1097e399441dSJames Smart complete(&lsop->ls_done); 1098e399441dSJames Smart } 1099e399441dSJames Smart 1100e399441dSJames Smart static int 1101c913a8b0SJames Smart nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop) 1102e399441dSJames Smart { 1103e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req; 1104e399441dSJames Smart struct fcnvme_ls_rjt *rjt = lsreq->rspaddr; 1105e399441dSJames Smart int ret; 1106e399441dSJames Smart 1107c913a8b0SJames Smart ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done); 1108e399441dSJames Smart 1109c913a8b0SJames Smart if (!ret) { 1110e399441dSJames Smart /* 1111e399441dSJames Smart * No timeout/not interruptible as we need the struct 1112e399441dSJames Smart * to exist until the lldd calls us back. Thus mandate 1113e399441dSJames Smart * wait until driver calls back. lldd responsible for 1114e399441dSJames Smart * the timeout action 1115e399441dSJames Smart */ 1116e399441dSJames Smart wait_for_completion(&lsop->ls_done); 1117e399441dSJames Smart 1118c913a8b0SJames Smart __nvme_fc_finish_ls_req(lsop); 1119e399441dSJames Smart 1120c913a8b0SJames Smart ret = lsop->ls_error; 1121e399441dSJames Smart } 1122e399441dSJames Smart 1123c913a8b0SJames Smart if (ret) 1124c913a8b0SJames Smart return ret; 1125c913a8b0SJames Smart 1126e399441dSJames Smart /* ACC or RJT payload ? */ 1127e399441dSJames Smart if (rjt->w0.ls_cmd == FCNVME_LS_RJT) 1128e399441dSJames Smart return -ENXIO; 1129e399441dSJames Smart 1130e399441dSJames Smart return 0; 1131e399441dSJames Smart } 1132e399441dSJames Smart 1133c913a8b0SJames Smart static int 1134c913a8b0SJames Smart nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport, 1135e399441dSJames Smart struct nvmefc_ls_req_op *lsop, 1136e399441dSJames Smart void (*done)(struct nvmefc_ls_req *req, int status)) 1137e399441dSJames Smart { 1138e399441dSJames Smart /* don't wait for completion */ 1139e399441dSJames Smart 1140c913a8b0SJames Smart return __nvme_fc_send_ls_req(rport, lsop, done); 1141e399441dSJames Smart } 1142e399441dSJames Smart 1143e399441dSJames Smart /* Validation Error indexes into the string table below */ 1144e399441dSJames Smart enum { 1145e399441dSJames Smart VERR_NO_ERROR = 0, 1146e399441dSJames Smart VERR_LSACC = 1, 1147e399441dSJames Smart VERR_LSDESC_RQST = 2, 1148e399441dSJames Smart VERR_LSDESC_RQST_LEN = 3, 1149e399441dSJames Smart VERR_ASSOC_ID = 4, 1150e399441dSJames Smart VERR_ASSOC_ID_LEN = 5, 1151e399441dSJames Smart VERR_CONN_ID = 6, 1152e399441dSJames Smart VERR_CONN_ID_LEN = 7, 1153e399441dSJames Smart VERR_CR_ASSOC = 8, 1154e399441dSJames Smart VERR_CR_ASSOC_ACC_LEN = 9, 1155e399441dSJames Smart VERR_CR_CONN = 10, 1156e399441dSJames Smart VERR_CR_CONN_ACC_LEN = 11, 1157e399441dSJames Smart VERR_DISCONN = 12, 1158e399441dSJames Smart VERR_DISCONN_ACC_LEN = 13, 1159e399441dSJames Smart }; 1160e399441dSJames Smart 1161e399441dSJames Smart static char *validation_errors[] = { 1162e399441dSJames Smart "OK", 1163e399441dSJames Smart "Not LS_ACC", 1164e399441dSJames Smart "Not LSDESC_RQST", 1165e399441dSJames Smart "Bad LSDESC_RQST Length", 1166e399441dSJames Smart "Not Association ID", 1167e399441dSJames Smart "Bad Association ID Length", 1168e399441dSJames Smart "Not Connection ID", 1169e399441dSJames Smart "Bad Connection ID Length", 1170e399441dSJames Smart "Not CR_ASSOC Rqst", 1171e399441dSJames Smart "Bad CR_ASSOC ACC Length", 1172e399441dSJames Smart "Not CR_CONN Rqst", 1173e399441dSJames Smart "Bad CR_CONN ACC Length", 1174e399441dSJames Smart "Not Disconnect Rqst", 1175e399441dSJames Smart "Bad Disconnect ACC Length", 1176e399441dSJames Smart }; 1177e399441dSJames Smart 1178e399441dSJames Smart static int 1179e399441dSJames Smart nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl, 1180e399441dSJames Smart struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio) 1181e399441dSJames Smart { 1182e399441dSJames Smart struct nvmefc_ls_req_op *lsop; 1183e399441dSJames Smart struct nvmefc_ls_req *lsreq; 1184e399441dSJames Smart struct fcnvme_ls_cr_assoc_rqst *assoc_rqst; 1185e399441dSJames Smart struct fcnvme_ls_cr_assoc_acc *assoc_acc; 1186e399441dSJames Smart int ret, fcret = 0; 1187e399441dSJames Smart 1188e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) + 1189e399441dSJames Smart ctrl->lport->ops->lsrqst_priv_sz + 1190e399441dSJames Smart sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL); 1191e399441dSJames Smart if (!lsop) { 1192e399441dSJames Smart ret = -ENOMEM; 1193e399441dSJames Smart goto out_no_memory; 1194e399441dSJames Smart } 1195e399441dSJames Smart lsreq = &lsop->ls_req; 1196e399441dSJames Smart 1197e399441dSJames Smart lsreq->private = (void *)&lsop[1]; 1198e399441dSJames Smart assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *) 1199e399441dSJames Smart (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz); 1200e399441dSJames Smart assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1]; 1201e399441dSJames Smart 1202e399441dSJames Smart assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION; 1203e399441dSJames Smart assoc_rqst->desc_list_len = 1204e399441dSJames Smart cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd)); 1205e399441dSJames Smart 1206e399441dSJames Smart assoc_rqst->assoc_cmd.desc_tag = 1207e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD); 1208e399441dSJames Smart assoc_rqst->assoc_cmd.desc_len = 1209e399441dSJames Smart fcnvme_lsdesc_len( 1210e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_assoc_cmd)); 1211e399441dSJames Smart 1212e399441dSJames Smart assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio); 1213d157e534SJames Smart assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize - 1); 1214e399441dSJames Smart /* Linux supports only Dynamic controllers */ 1215e399441dSJames Smart assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff); 12168e412263SChristoph Hellwig uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id); 1217e399441dSJames Smart strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn, 1218e399441dSJames Smart min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE)); 1219e399441dSJames Smart strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn, 1220e399441dSJames Smart min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE)); 1221e399441dSJames Smart 1222e399441dSJames Smart lsop->queue = queue; 1223e399441dSJames Smart lsreq->rqstaddr = assoc_rqst; 1224e399441dSJames Smart lsreq->rqstlen = sizeof(*assoc_rqst); 1225e399441dSJames Smart lsreq->rspaddr = assoc_acc; 1226e399441dSJames Smart lsreq->rsplen = sizeof(*assoc_acc); 122753b2b2f5SJames Smart lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC; 1228e399441dSJames Smart 1229c913a8b0SJames Smart ret = nvme_fc_send_ls_req(ctrl->rport, lsop); 1230e399441dSJames Smart if (ret) 1231e399441dSJames Smart goto out_free_buffer; 1232e399441dSJames Smart 1233e399441dSJames Smart /* process connect LS completion */ 1234e399441dSJames Smart 1235e399441dSJames Smart /* validate the ACC response */ 1236e399441dSJames Smart if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC) 1237e399441dSJames Smart fcret = VERR_LSACC; 1238f77fc87cSJames Smart else if (assoc_acc->hdr.desc_list_len != 1239e399441dSJames Smart fcnvme_lsdesc_len( 1240e399441dSJames Smart sizeof(struct fcnvme_ls_cr_assoc_acc))) 1241e399441dSJames Smart fcret = VERR_CR_ASSOC_ACC_LEN; 1242f77fc87cSJames Smart else if (assoc_acc->hdr.rqst.desc_tag != 1243f77fc87cSJames Smart cpu_to_be32(FCNVME_LSDESC_RQST)) 1244e399441dSJames Smart fcret = VERR_LSDESC_RQST; 1245e399441dSJames Smart else if (assoc_acc->hdr.rqst.desc_len != 1246e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst))) 1247e399441dSJames Smart fcret = VERR_LSDESC_RQST_LEN; 1248e399441dSJames Smart else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION) 1249e399441dSJames Smart fcret = VERR_CR_ASSOC; 1250e399441dSJames Smart else if (assoc_acc->associd.desc_tag != 1251e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_ASSOC_ID)) 1252e399441dSJames Smart fcret = VERR_ASSOC_ID; 1253e399441dSJames Smart else if (assoc_acc->associd.desc_len != 1254e399441dSJames Smart fcnvme_lsdesc_len( 1255e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id))) 1256e399441dSJames Smart fcret = VERR_ASSOC_ID_LEN; 1257e399441dSJames Smart else if (assoc_acc->connectid.desc_tag != 1258e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CONN_ID)) 1259e399441dSJames Smart fcret = VERR_CONN_ID; 1260e399441dSJames Smart else if (assoc_acc->connectid.desc_len != 1261e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id))) 1262e399441dSJames Smart fcret = VERR_CONN_ID_LEN; 1263e399441dSJames Smart 1264e399441dSJames Smart if (fcret) { 1265e399441dSJames Smart ret = -EBADF; 1266e399441dSJames Smart dev_err(ctrl->dev, 12677db39484SJames Smart "q %d Create Association LS failed: %s\n", 1268e399441dSJames Smart queue->qnum, validation_errors[fcret]); 1269e399441dSJames Smart } else { 1270e399441dSJames Smart ctrl->association_id = 1271e399441dSJames Smart be64_to_cpu(assoc_acc->associd.association_id); 1272e399441dSJames Smart queue->connection_id = 1273e399441dSJames Smart be64_to_cpu(assoc_acc->connectid.connection_id); 1274e399441dSJames Smart set_bit(NVME_FC_Q_CONNECTED, &queue->flags); 1275e399441dSJames Smart } 1276e399441dSJames Smart 1277e399441dSJames Smart out_free_buffer: 1278e399441dSJames Smart kfree(lsop); 1279e399441dSJames Smart out_no_memory: 1280e399441dSJames Smart if (ret) 1281e399441dSJames Smart dev_err(ctrl->dev, 1282e399441dSJames Smart "queue %d connect admin queue failed (%d).\n", 1283e399441dSJames Smart queue->qnum, ret); 1284e399441dSJames Smart return ret; 1285e399441dSJames Smart } 1286e399441dSJames Smart 1287e399441dSJames Smart static int 1288e399441dSJames Smart nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue, 1289e399441dSJames Smart u16 qsize, u16 ersp_ratio) 1290e399441dSJames Smart { 1291e399441dSJames Smart struct nvmefc_ls_req_op *lsop; 1292e399441dSJames Smart struct nvmefc_ls_req *lsreq; 1293e399441dSJames Smart struct fcnvme_ls_cr_conn_rqst *conn_rqst; 1294e399441dSJames Smart struct fcnvme_ls_cr_conn_acc *conn_acc; 1295e399441dSJames Smart int ret, fcret = 0; 1296e399441dSJames Smart 1297e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) + 1298e399441dSJames Smart ctrl->lport->ops->lsrqst_priv_sz + 1299e399441dSJames Smart sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL); 1300e399441dSJames Smart if (!lsop) { 1301e399441dSJames Smart ret = -ENOMEM; 1302e399441dSJames Smart goto out_no_memory; 1303e399441dSJames Smart } 1304e399441dSJames Smart lsreq = &lsop->ls_req; 1305e399441dSJames Smart 1306e399441dSJames Smart lsreq->private = (void *)&lsop[1]; 1307e399441dSJames Smart conn_rqst = (struct fcnvme_ls_cr_conn_rqst *) 1308e399441dSJames Smart (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz); 1309e399441dSJames Smart conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1]; 1310e399441dSJames Smart 1311e399441dSJames Smart conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION; 1312e399441dSJames Smart conn_rqst->desc_list_len = cpu_to_be32( 1313e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id) + 1314e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_conn_cmd)); 1315e399441dSJames Smart 1316e399441dSJames Smart conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID); 1317e399441dSJames Smart conn_rqst->associd.desc_len = 1318e399441dSJames Smart fcnvme_lsdesc_len( 1319e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id)); 1320e399441dSJames Smart conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id); 1321e399441dSJames Smart conn_rqst->connect_cmd.desc_tag = 1322e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD); 1323e399441dSJames Smart conn_rqst->connect_cmd.desc_len = 1324e399441dSJames Smart fcnvme_lsdesc_len( 1325e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_conn_cmd)); 1326e399441dSJames Smart conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio); 1327e399441dSJames Smart conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum); 1328d157e534SJames Smart conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize - 1); 1329e399441dSJames Smart 1330e399441dSJames Smart lsop->queue = queue; 1331e399441dSJames Smart lsreq->rqstaddr = conn_rqst; 1332e399441dSJames Smart lsreq->rqstlen = sizeof(*conn_rqst); 1333e399441dSJames Smart lsreq->rspaddr = conn_acc; 1334e399441dSJames Smart lsreq->rsplen = sizeof(*conn_acc); 133553b2b2f5SJames Smart lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC; 1336e399441dSJames Smart 1337c913a8b0SJames Smart ret = nvme_fc_send_ls_req(ctrl->rport, lsop); 1338e399441dSJames Smart if (ret) 1339e399441dSJames Smart goto out_free_buffer; 1340e399441dSJames Smart 1341e399441dSJames Smart /* process connect LS completion */ 1342e399441dSJames Smart 1343e399441dSJames Smart /* validate the ACC response */ 1344e399441dSJames Smart if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC) 1345e399441dSJames Smart fcret = VERR_LSACC; 1346f77fc87cSJames Smart else if (conn_acc->hdr.desc_list_len != 1347e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc))) 1348e399441dSJames Smart fcret = VERR_CR_CONN_ACC_LEN; 1349f77fc87cSJames Smart else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST)) 1350e399441dSJames Smart fcret = VERR_LSDESC_RQST; 1351e399441dSJames Smart else if (conn_acc->hdr.rqst.desc_len != 1352e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst))) 1353e399441dSJames Smart fcret = VERR_LSDESC_RQST_LEN; 1354e399441dSJames Smart else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION) 1355e399441dSJames Smart fcret = VERR_CR_CONN; 1356e399441dSJames Smart else if (conn_acc->connectid.desc_tag != 1357e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CONN_ID)) 1358e399441dSJames Smart fcret = VERR_CONN_ID; 1359e399441dSJames Smart else if (conn_acc->connectid.desc_len != 1360e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id))) 1361e399441dSJames Smart fcret = VERR_CONN_ID_LEN; 1362e399441dSJames Smart 1363e399441dSJames Smart if (fcret) { 1364e399441dSJames Smart ret = -EBADF; 1365e399441dSJames Smart dev_err(ctrl->dev, 13667db39484SJames Smart "q %d Create I/O Connection LS failed: %s\n", 1367e399441dSJames Smart queue->qnum, validation_errors[fcret]); 1368e399441dSJames Smart } else { 1369e399441dSJames Smart queue->connection_id = 1370e399441dSJames Smart be64_to_cpu(conn_acc->connectid.connection_id); 1371e399441dSJames Smart set_bit(NVME_FC_Q_CONNECTED, &queue->flags); 1372e399441dSJames Smart } 1373e399441dSJames Smart 1374e399441dSJames Smart out_free_buffer: 1375e399441dSJames Smart kfree(lsop); 1376e399441dSJames Smart out_no_memory: 1377e399441dSJames Smart if (ret) 1378e399441dSJames Smart dev_err(ctrl->dev, 13797db39484SJames Smart "queue %d connect I/O queue failed (%d).\n", 1380e399441dSJames Smart queue->qnum, ret); 1381e399441dSJames Smart return ret; 1382e399441dSJames Smart } 1383e399441dSJames Smart 1384e399441dSJames Smart static void 1385e399441dSJames Smart nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status) 1386e399441dSJames Smart { 1387e399441dSJames Smart struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq); 1388e399441dSJames Smart 1389c913a8b0SJames Smart __nvme_fc_finish_ls_req(lsop); 1390e399441dSJames Smart 1391d4e4230cSMilan P. Gandhi /* fc-nvme initiator doesn't care about success or failure of cmd */ 1392e399441dSJames Smart 1393e399441dSJames Smart kfree(lsop); 1394e399441dSJames Smart } 1395e399441dSJames Smart 1396e399441dSJames Smart /* 1397e399441dSJames Smart * This routine sends a FC-NVME LS to disconnect (aka terminate) 1398e399441dSJames Smart * the FC-NVME Association. Terminating the association also 1399e399441dSJames Smart * terminates the FC-NVME connections (per queue, both admin and io 1400e399441dSJames Smart * queues) that are part of the association. E.g. things are torn 1401e399441dSJames Smart * down, and the related FC-NVME Association ID and Connection IDs 1402e399441dSJames Smart * become invalid. 1403e399441dSJames Smart * 1404e399441dSJames Smart * The behavior of the fc-nvme initiator is such that it's 1405e399441dSJames Smart * understanding of the association and connections will implicitly 1406e399441dSJames Smart * be torn down. The action is implicit as it may be due to a loss of 1407e399441dSJames Smart * connectivity with the fc-nvme target, so you may never get a 1408e399441dSJames Smart * response even if you tried. As such, the action of this routine 1409e399441dSJames Smart * is to asynchronously send the LS, ignore any results of the LS, and 1410e399441dSJames Smart * continue on with terminating the association. If the fc-nvme target 1411e399441dSJames Smart * is present and receives the LS, it too can tear down. 1412e399441dSJames Smart */ 1413e399441dSJames Smart static void 1414e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl) 1415e399441dSJames Smart { 141653b2b2f5SJames Smart struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst; 141753b2b2f5SJames Smart struct fcnvme_ls_disconnect_assoc_acc *discon_acc; 1418e399441dSJames Smart struct nvmefc_ls_req_op *lsop; 1419e399441dSJames Smart struct nvmefc_ls_req *lsreq; 1420c913a8b0SJames Smart int ret; 1421e399441dSJames Smart 1422e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) + 1423e399441dSJames Smart ctrl->lport->ops->lsrqst_priv_sz + 1424e399441dSJames Smart sizeof(*discon_rqst) + sizeof(*discon_acc)), 1425e399441dSJames Smart GFP_KERNEL); 1426e399441dSJames Smart if (!lsop) 1427e399441dSJames Smart /* couldn't sent it... too bad */ 1428e399441dSJames Smart return; 1429e399441dSJames Smart 1430e399441dSJames Smart lsreq = &lsop->ls_req; 1431e399441dSJames Smart 1432e399441dSJames Smart lsreq->private = (void *)&lsop[1]; 143353b2b2f5SJames Smart discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *) 1434e399441dSJames Smart (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz); 143553b2b2f5SJames Smart discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1]; 1436e399441dSJames Smart 143753b2b2f5SJames Smart discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT_ASSOC; 1438e399441dSJames Smart discon_rqst->desc_list_len = cpu_to_be32( 1439e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id) + 1440e399441dSJames Smart sizeof(struct fcnvme_lsdesc_disconn_cmd)); 1441e399441dSJames Smart 1442e399441dSJames Smart discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID); 1443e399441dSJames Smart discon_rqst->associd.desc_len = 1444e399441dSJames Smart fcnvme_lsdesc_len( 1445e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id)); 1446e399441dSJames Smart 1447e399441dSJames Smart discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id); 1448e399441dSJames Smart 1449e399441dSJames Smart discon_rqst->discon_cmd.desc_tag = cpu_to_be32( 1450e399441dSJames Smart FCNVME_LSDESC_DISCONN_CMD); 1451e399441dSJames Smart discon_rqst->discon_cmd.desc_len = 1452e399441dSJames Smart fcnvme_lsdesc_len( 1453e399441dSJames Smart sizeof(struct fcnvme_lsdesc_disconn_cmd)); 1454e399441dSJames Smart 1455e399441dSJames Smart lsreq->rqstaddr = discon_rqst; 1456e399441dSJames Smart lsreq->rqstlen = sizeof(*discon_rqst); 1457e399441dSJames Smart lsreq->rspaddr = discon_acc; 1458e399441dSJames Smart lsreq->rsplen = sizeof(*discon_acc); 145953b2b2f5SJames Smart lsreq->timeout = NVME_FC_LS_TIMEOUT_SEC; 1460e399441dSJames Smart 1461c913a8b0SJames Smart ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop, 1462c913a8b0SJames Smart nvme_fc_disconnect_assoc_done); 1463c913a8b0SJames Smart if (ret) 1464c913a8b0SJames Smart kfree(lsop); 1465e399441dSJames Smart } 1466e399441dSJames Smart 1467e399441dSJames Smart 1468e399441dSJames Smart /* *********************** NVME Ctrl Routines **************************** */ 1469e399441dSJames Smart 1470f874d5d0SJames Smart static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg); 1471e399441dSJames Smart 1472e399441dSJames Smart static void 1473e399441dSJames Smart __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl, 1474e399441dSJames Smart struct nvme_fc_fcp_op *op) 1475e399441dSJames Smart { 1476e399441dSJames Smart fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma, 1477e399441dSJames Smart sizeof(op->rsp_iu), DMA_FROM_DEVICE); 1478e399441dSJames Smart fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma, 1479e399441dSJames Smart sizeof(op->cmd_iu), DMA_TO_DEVICE); 1480e399441dSJames Smart 1481e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_UNINIT); 1482e399441dSJames Smart } 1483e399441dSJames Smart 1484e399441dSJames Smart static void 1485d6296d39SChristoph Hellwig nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq, 1486d6296d39SChristoph Hellwig unsigned int hctx_idx) 1487e399441dSJames Smart { 1488e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1489e399441dSJames Smart 1490d6296d39SChristoph Hellwig return __nvme_fc_exit_request(set->driver_data, op); 1491e399441dSJames Smart } 1492e399441dSJames Smart 149378a7ac26SJames Smart static int 149478a7ac26SJames Smart __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op) 149578a7ac26SJames Smart { 14963efd6e8eSJames Smart unsigned long flags; 14973efd6e8eSJames Smart int opstate; 149878a7ac26SJames Smart 14993efd6e8eSJames Smart spin_lock_irqsave(&ctrl->lock, flags); 15003efd6e8eSJames Smart opstate = atomic_xchg(&op->state, FCPOP_STATE_ABORTED); 15013efd6e8eSJames Smart if (opstate != FCPOP_STATE_ACTIVE) 15023efd6e8eSJames Smart atomic_set(&op->state, opstate); 15033efd6e8eSJames Smart else if (ctrl->flags & FCCTRL_TERMIO) 15043efd6e8eSJames Smart ctrl->iocnt++; 15053efd6e8eSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 15063efd6e8eSJames Smart 15073efd6e8eSJames Smart if (opstate != FCPOP_STATE_ACTIVE) 150878a7ac26SJames Smart return -ECANCELED; 150978a7ac26SJames Smart 151078a7ac26SJames Smart ctrl->lport->ops->fcp_abort(&ctrl->lport->localport, 151178a7ac26SJames Smart &ctrl->rport->remoteport, 151278a7ac26SJames Smart op->queue->lldd_handle, 151378a7ac26SJames Smart &op->fcp_req); 151478a7ac26SJames Smart 151578a7ac26SJames Smart return 0; 151678a7ac26SJames Smart } 151778a7ac26SJames Smart 1518e399441dSJames Smart static void 151978a7ac26SJames Smart nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl) 1520e399441dSJames Smart { 1521e399441dSJames Smart struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops; 15223efd6e8eSJames Smart int i; 1523e399441dSJames Smart 15244cff280aSJames Smart /* ensure we've initialized the ops once */ 15254cff280aSJames Smart if (!(aen_op->flags & FCOP_FLAGS_AEN)) 15264cff280aSJames Smart return; 15274cff280aSJames Smart 15283efd6e8eSJames Smart for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) 15293efd6e8eSJames Smart __nvme_fc_abort_op(ctrl, aen_op); 153078a7ac26SJames Smart } 153178a7ac26SJames Smart 1532c3aedd22SJames Smart static inline void 153378a7ac26SJames Smart __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl, 15343efd6e8eSJames Smart struct nvme_fc_fcp_op *op, int opstate) 153578a7ac26SJames Smart { 153678a7ac26SJames Smart unsigned long flags; 153778a7ac26SJames Smart 1538c3aedd22SJames Smart if (opstate == FCPOP_STATE_ABORTED) { 153978a7ac26SJames Smart spin_lock_irqsave(&ctrl->lock, flags); 1540c3aedd22SJames Smart if (ctrl->flags & FCCTRL_TERMIO) { 154136715cf4SJames Smart if (!--ctrl->iocnt) 154236715cf4SJames Smart wake_up(&ctrl->ioabort_wait); 154336715cf4SJames Smart } 154478a7ac26SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 1545c3aedd22SJames Smart } 154678a7ac26SJames Smart } 1547e399441dSJames Smart 1548baee29acSChristoph Hellwig static void 1549e399441dSJames Smart nvme_fc_fcpio_done(struct nvmefc_fcp_req *req) 1550e399441dSJames Smart { 1551e399441dSJames Smart struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req); 1552e399441dSJames Smart struct request *rq = op->rq; 1553e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req; 1554e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl; 1555e399441dSJames Smart struct nvme_fc_queue *queue = op->queue; 1556e399441dSJames Smart struct nvme_completion *cqe = &op->rsp_iu.cqe; 1557458f280dSJames Smart struct nvme_command *sqe = &op->cmd_iu.sqe; 1558d663b69fSChristoph Hellwig __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1); 155927fa9bc5SChristoph Hellwig union nvme_result result; 15600a02e39fSJames Smart bool terminate_assoc = true; 15613efd6e8eSJames Smart int opstate; 1562e399441dSJames Smart 1563e399441dSJames Smart /* 1564e399441dSJames Smart * WARNING: 1565e399441dSJames Smart * The current linux implementation of a nvme controller 1566e399441dSJames Smart * allocates a single tag set for all io queues and sizes 1567e399441dSJames Smart * the io queues to fully hold all possible tags. Thus, the 1568e399441dSJames Smart * implementation does not reference or care about the sqhd 1569e399441dSJames Smart * value as it never needs to use the sqhd/sqtail pointers 1570e399441dSJames Smart * for submission pacing. 1571e399441dSJames Smart * 1572e399441dSJames Smart * This affects the FC-NVME implementation in two ways: 1573e399441dSJames Smart * 1) As the value doesn't matter, we don't need to waste 1574e399441dSJames Smart * cycles extracting it from ERSPs and stamping it in the 1575e399441dSJames Smart * cases where the transport fabricates CQEs on successful 1576e399441dSJames Smart * completions. 1577e399441dSJames Smart * 2) The FC-NVME implementation requires that delivery of 1578e399441dSJames Smart * ERSP completions are to go back to the nvme layer in order 1579e399441dSJames Smart * relative to the rsn, such that the sqhd value will always 1580e399441dSJames Smart * be "in order" for the nvme layer. As the nvme layer in 1581e399441dSJames Smart * linux doesn't care about sqhd, there's no need to return 1582e399441dSJames Smart * them in order. 1583e399441dSJames Smart * 1584e399441dSJames Smart * Additionally: 1585e399441dSJames Smart * As the core nvme layer in linux currently does not look at 1586e399441dSJames Smart * every field in the cqe - in cases where the FC transport must 1587e399441dSJames Smart * fabricate a CQE, the following fields will not be set as they 1588e399441dSJames Smart * are not referenced: 1589e399441dSJames Smart * cqe.sqid, cqe.sqhd, cqe.command_id 1590f874d5d0SJames Smart * 1591f874d5d0SJames Smart * Failure or error of an individual i/o, in a transport 1592f874d5d0SJames Smart * detected fashion unrelated to the nvme completion status, 1593f874d5d0SJames Smart * potentially cause the initiator and target sides to get out 1594f874d5d0SJames Smart * of sync on SQ head/tail (aka outstanding io count allowed). 1595f874d5d0SJames Smart * Per FC-NVME spec, failure of an individual command requires 1596f874d5d0SJames Smart * the connection to be terminated, which in turn requires the 1597f874d5d0SJames Smart * association to be terminated. 1598e399441dSJames Smart */ 1599e399441dSJames Smart 16003efd6e8eSJames Smart opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE); 16013efd6e8eSJames Smart 1602e399441dSJames Smart fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma, 1603e399441dSJames Smart sizeof(op->rsp_iu), DMA_FROM_DEVICE); 1604e399441dSJames Smart 16053efd6e8eSJames Smart if (opstate == FCPOP_STATE_ABORTED) 160674bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1); 160774bd8cbeSJames Smart else if (freq->status) { 160874bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1); 160974bd8cbeSJames Smart dev_info(ctrl->ctrl.device, 161074bd8cbeSJames Smart "NVME-FC{%d}: io failed due to lldd error %d\n", 161174bd8cbeSJames Smart ctrl->cnum, freq->status); 161274bd8cbeSJames Smart } 1613e399441dSJames Smart 1614e399441dSJames Smart /* 1615e399441dSJames Smart * For the linux implementation, if we have an unsuccesful 1616e399441dSJames Smart * status, they blk-mq layer can typically be called with the 1617e399441dSJames Smart * non-zero status and the content of the cqe isn't important. 1618e399441dSJames Smart */ 1619e399441dSJames Smart if (status) 1620e399441dSJames Smart goto done; 1621e399441dSJames Smart 1622e399441dSJames Smart /* 1623e399441dSJames Smart * command completed successfully relative to the wire 1624e399441dSJames Smart * protocol. However, validate anything received and 1625e399441dSJames Smart * extract the status and result from the cqe (create it 1626e399441dSJames Smart * where necessary). 1627e399441dSJames Smart */ 1628e399441dSJames Smart 1629e399441dSJames Smart switch (freq->rcv_rsplen) { 1630e399441dSJames Smart 1631e399441dSJames Smart case 0: 1632e399441dSJames Smart case NVME_FC_SIZEOF_ZEROS_RSP: 1633e399441dSJames Smart /* 1634e399441dSJames Smart * No response payload or 12 bytes of payload (which 1635e399441dSJames Smart * should all be zeros) are considered successful and 1636e399441dSJames Smart * no payload in the CQE by the transport. 1637e399441dSJames Smart */ 1638e399441dSJames Smart if (freq->transferred_length != 1639e399441dSJames Smart be32_to_cpu(op->cmd_iu.data_len)) { 164074bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1); 164174bd8cbeSJames Smart dev_info(ctrl->ctrl.device, 164274bd8cbeSJames Smart "NVME-FC{%d}: io failed due to bad transfer " 164374bd8cbeSJames Smart "length: %d vs expected %d\n", 164474bd8cbeSJames Smart ctrl->cnum, freq->transferred_length, 164574bd8cbeSJames Smart be32_to_cpu(op->cmd_iu.data_len)); 1646e399441dSJames Smart goto done; 1647e399441dSJames Smart } 164827fa9bc5SChristoph Hellwig result.u64 = 0; 1649e399441dSJames Smart break; 1650e399441dSJames Smart 1651e399441dSJames Smart case sizeof(struct nvme_fc_ersp_iu): 1652e399441dSJames Smart /* 1653e399441dSJames Smart * The ERSP IU contains a full completion with CQE. 1654e399441dSJames Smart * Validate ERSP IU and look at cqe. 1655e399441dSJames Smart */ 1656e399441dSJames Smart if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) != 1657e399441dSJames Smart (freq->rcv_rsplen / 4) || 1658e399441dSJames Smart be32_to_cpu(op->rsp_iu.xfrd_len) != 1659e399441dSJames Smart freq->transferred_length || 166053b2b2f5SJames Smart op->rsp_iu.ersp_result || 1661458f280dSJames Smart sqe->common.command_id != cqe->command_id)) { 166274bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1); 166374bd8cbeSJames Smart dev_info(ctrl->ctrl.device, 166474bd8cbeSJames Smart "NVME-FC{%d}: io failed due to bad NVMe_ERSP: " 166574bd8cbeSJames Smart "iu len %d, xfr len %d vs %d, status code " 166674bd8cbeSJames Smart "%d, cmdid %d vs %d\n", 166774bd8cbeSJames Smart ctrl->cnum, be16_to_cpu(op->rsp_iu.iu_len), 166874bd8cbeSJames Smart be32_to_cpu(op->rsp_iu.xfrd_len), 166974bd8cbeSJames Smart freq->transferred_length, 167053b2b2f5SJames Smart op->rsp_iu.ersp_result, 167174bd8cbeSJames Smart sqe->common.command_id, 167274bd8cbeSJames Smart cqe->command_id); 1673e399441dSJames Smart goto done; 1674e399441dSJames Smart } 167527fa9bc5SChristoph Hellwig result = cqe->result; 1676d663b69fSChristoph Hellwig status = cqe->status; 1677e399441dSJames Smart break; 1678e399441dSJames Smart 1679e399441dSJames Smart default: 168074bd8cbeSJames Smart status = cpu_to_le16(NVME_SC_HOST_PATH_ERROR << 1); 168174bd8cbeSJames Smart dev_info(ctrl->ctrl.device, 168274bd8cbeSJames Smart "NVME-FC{%d}: io failed due to odd NVMe_xRSP iu " 168374bd8cbeSJames Smart "len %d\n", 168474bd8cbeSJames Smart ctrl->cnum, freq->rcv_rsplen); 1685e399441dSJames Smart goto done; 1686e399441dSJames Smart } 1687e399441dSJames Smart 1688f874d5d0SJames Smart terminate_assoc = false; 1689f874d5d0SJames Smart 1690e399441dSJames Smart done: 169178a7ac26SJames Smart if (op->flags & FCOP_FLAGS_AEN) { 169227fa9bc5SChristoph Hellwig nvme_complete_async_event(&queue->ctrl->ctrl, status, &result); 16933efd6e8eSJames Smart __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate); 169478a7ac26SJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE); 169578a7ac26SJames Smart op->flags = FCOP_FLAGS_AEN; /* clear other flags */ 1696e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 1697f874d5d0SJames Smart goto check_error; 1698e399441dSJames Smart } 1699e399441dSJames Smart 1700c3aedd22SJames Smart __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate); 17010a02e39fSJames Smart nvme_end_request(rq, status, result); 1702f874d5d0SJames Smart 1703f874d5d0SJames Smart check_error: 1704f874d5d0SJames Smart if (terminate_assoc) 1705f874d5d0SJames Smart nvme_fc_error_recovery(ctrl, "transport detected io error"); 1706e399441dSJames Smart } 1707e399441dSJames Smart 1708e399441dSJames Smart static int 1709e399441dSJames Smart __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl, 1710e399441dSJames Smart struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op, 1711e399441dSJames Smart struct request *rq, u32 rqno) 1712e399441dSJames Smart { 1713d3d0bc78SBart Van Assche struct nvme_fcp_op_w_sgl *op_w_sgl = 1714d3d0bc78SBart Van Assche container_of(op, typeof(*op_w_sgl), op); 1715e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 1716e399441dSJames Smart int ret = 0; 1717e399441dSJames Smart 1718e399441dSJames Smart memset(op, 0, sizeof(*op)); 1719e399441dSJames Smart op->fcp_req.cmdaddr = &op->cmd_iu; 1720e399441dSJames Smart op->fcp_req.cmdlen = sizeof(op->cmd_iu); 1721e399441dSJames Smart op->fcp_req.rspaddr = &op->rsp_iu; 1722e399441dSJames Smart op->fcp_req.rsplen = sizeof(op->rsp_iu); 1723e399441dSJames Smart op->fcp_req.done = nvme_fc_fcpio_done; 1724e399441dSJames Smart op->ctrl = ctrl; 1725e399441dSJames Smart op->queue = queue; 1726e399441dSJames Smart op->rq = rq; 1727e399441dSJames Smart op->rqno = rqno; 1728e399441dSJames Smart 172953b2b2f5SJames Smart cmdiu->format_id = NVME_CMD_FORMAT_ID; 1730e399441dSJames Smart cmdiu->fc_id = NVME_CMD_FC_ID; 1731e399441dSJames Smart cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32)); 173244fbf3bbSJames Smart if (queue->qnum) 173344fbf3bbSJames Smart cmdiu->rsv_cat = fccmnd_set_cat_css(0, 173444fbf3bbSJames Smart (NVME_CC_CSS_NVM >> NVME_CC_CSS_SHIFT)); 173544fbf3bbSJames Smart else 173644fbf3bbSJames Smart cmdiu->rsv_cat = fccmnd_set_cat_admin(0); 1737e399441dSJames Smart 1738e399441dSJames Smart op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev, 1739e399441dSJames Smart &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE); 1740e399441dSJames Smart if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) { 1741e399441dSJames Smart dev_err(ctrl->dev, 1742e399441dSJames Smart "FCP Op failed - cmdiu dma mapping failed.\n"); 1743e399441dSJames Smart ret = EFAULT; 1744e399441dSJames Smart goto out_on_error; 1745e399441dSJames Smart } 1746e399441dSJames Smart 1747e399441dSJames Smart op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev, 1748e399441dSJames Smart &op->rsp_iu, sizeof(op->rsp_iu), 1749e399441dSJames Smart DMA_FROM_DEVICE); 1750e399441dSJames Smart if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) { 1751e399441dSJames Smart dev_err(ctrl->dev, 1752e399441dSJames Smart "FCP Op failed - rspiu dma mapping failed.\n"); 1753e399441dSJames Smart ret = EFAULT; 1754e399441dSJames Smart } 1755e399441dSJames Smart 1756e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE); 1757e399441dSJames Smart out_on_error: 1758e399441dSJames Smart return ret; 1759e399441dSJames Smart } 1760e399441dSJames Smart 1761e399441dSJames Smart static int 1762d6296d39SChristoph Hellwig nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq, 1763d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node) 1764e399441dSJames Smart { 1765d6296d39SChristoph Hellwig struct nvme_fc_ctrl *ctrl = set->driver_data; 1766d3d0bc78SBart Van Assche struct nvme_fcp_op_w_sgl *op = blk_mq_rq_to_pdu(rq); 176776f983cbSChristoph Hellwig int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0; 176876f983cbSChristoph Hellwig struct nvme_fc_queue *queue = &ctrl->queues[queue_idx]; 17690d2bdf9fSBart Van Assche int res; 1770e399441dSJames Smart 17710d2bdf9fSBart Van Assche res = __nvme_fc_init_request(ctrl, queue, &op->op, rq, queue->rqcnt++); 17720d2bdf9fSBart Van Assche if (res) 17730d2bdf9fSBart Van Assche return res; 17740d2bdf9fSBart Van Assche op->op.fcp_req.first_sgl = &op->sgl[0]; 1775d19b8bc8SJames Smart op->op.fcp_req.private = &op->priv[0]; 1776dfa74422SEwan D. Milne nvme_req(rq)->ctrl = &ctrl->ctrl; 17770d2bdf9fSBart Van Assche return res; 1778e399441dSJames Smart } 1779e399441dSJames Smart 1780e399441dSJames Smart static int 1781e399441dSJames Smart nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl) 1782e399441dSJames Smart { 1783e399441dSJames Smart struct nvme_fc_fcp_op *aen_op; 1784e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu; 1785e399441dSJames Smart struct nvme_command *sqe; 178661bff8efSJames Smart void *private; 1787e399441dSJames Smart int i, ret; 1788e399441dSJames Smart 1789e399441dSJames Smart aen_op = ctrl->aen_ops; 179038dabe21SKeith Busch for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) { 179161bff8efSJames Smart private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz, 179261bff8efSJames Smart GFP_KERNEL); 179361bff8efSJames Smart if (!private) 179461bff8efSJames Smart return -ENOMEM; 179561bff8efSJames Smart 1796e399441dSJames Smart cmdiu = &aen_op->cmd_iu; 1797e399441dSJames Smart sqe = &cmdiu->sqe; 1798e399441dSJames Smart ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0], 1799e399441dSJames Smart aen_op, (struct request *)NULL, 180038dabe21SKeith Busch (NVME_AQ_BLK_MQ_DEPTH + i)); 180161bff8efSJames Smart if (ret) { 180261bff8efSJames Smart kfree(private); 1803e399441dSJames Smart return ret; 180461bff8efSJames Smart } 1805e399441dSJames Smart 180678a7ac26SJames Smart aen_op->flags = FCOP_FLAGS_AEN; 180761bff8efSJames Smart aen_op->fcp_req.private = private; 180878a7ac26SJames Smart 1809e399441dSJames Smart memset(sqe, 0, sizeof(*sqe)); 1810e399441dSJames Smart sqe->common.opcode = nvme_admin_async_event; 181178a7ac26SJames Smart /* Note: core layer may overwrite the sqe.command_id value */ 181238dabe21SKeith Busch sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i; 1813e399441dSJames Smart } 1814e399441dSJames Smart return 0; 1815e399441dSJames Smart } 1816e399441dSJames Smart 181761bff8efSJames Smart static void 181861bff8efSJames Smart nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl) 181961bff8efSJames Smart { 182061bff8efSJames Smart struct nvme_fc_fcp_op *aen_op; 182161bff8efSJames Smart int i; 182261bff8efSJames Smart 182361bff8efSJames Smart aen_op = ctrl->aen_ops; 182438dabe21SKeith Busch for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) { 182561bff8efSJames Smart if (!aen_op->fcp_req.private) 182661bff8efSJames Smart continue; 182761bff8efSJames Smart 182861bff8efSJames Smart __nvme_fc_exit_request(ctrl, aen_op); 182961bff8efSJames Smart 183061bff8efSJames Smart kfree(aen_op->fcp_req.private); 183161bff8efSJames Smart aen_op->fcp_req.private = NULL; 183261bff8efSJames Smart } 183361bff8efSJames Smart } 1834e399441dSJames Smart 1835e399441dSJames Smart static inline void 1836e399441dSJames Smart __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl, 1837e399441dSJames Smart unsigned int qidx) 1838e399441dSJames Smart { 1839e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[qidx]; 1840e399441dSJames Smart 1841e399441dSJames Smart hctx->driver_data = queue; 1842e399441dSJames Smart queue->hctx = hctx; 1843e399441dSJames Smart } 1844e399441dSJames Smart 1845e399441dSJames Smart static int 1846e399441dSJames Smart nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, 1847e399441dSJames Smart unsigned int hctx_idx) 1848e399441dSJames Smart { 1849e399441dSJames Smart struct nvme_fc_ctrl *ctrl = data; 1850e399441dSJames Smart 1851e399441dSJames Smart __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1); 1852e399441dSJames Smart 1853e399441dSJames Smart return 0; 1854e399441dSJames Smart } 1855e399441dSJames Smart 1856e399441dSJames Smart static int 1857e399441dSJames Smart nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, 1858e399441dSJames Smart unsigned int hctx_idx) 1859e399441dSJames Smart { 1860e399441dSJames Smart struct nvme_fc_ctrl *ctrl = data; 1861e399441dSJames Smart 1862e399441dSJames Smart __nvme_fc_init_hctx(hctx, ctrl, hctx_idx); 1863e399441dSJames Smart 1864e399441dSJames Smart return 0; 1865e399441dSJames Smart } 1866e399441dSJames Smart 1867e399441dSJames Smart static void 186808e15075SKeith Busch nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx) 1869e399441dSJames Smart { 1870e399441dSJames Smart struct nvme_fc_queue *queue; 1871e399441dSJames Smart 1872e399441dSJames Smart queue = &ctrl->queues[idx]; 1873e399441dSJames Smart memset(queue, 0, sizeof(*queue)); 1874e399441dSJames Smart queue->ctrl = ctrl; 1875e399441dSJames Smart queue->qnum = idx; 187667f471b6SJames Smart atomic_set(&queue->csn, 0); 1877e399441dSJames Smart queue->dev = ctrl->dev; 1878e399441dSJames Smart 1879e399441dSJames Smart if (idx > 0) 1880e399441dSJames Smart queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; 1881e399441dSJames Smart else 1882e399441dSJames Smart queue->cmnd_capsule_len = sizeof(struct nvme_command); 1883e399441dSJames Smart 1884e399441dSJames Smart /* 1885e399441dSJames Smart * Considered whether we should allocate buffers for all SQEs 1886e399441dSJames Smart * and CQEs and dma map them - mapping their respective entries 1887e399441dSJames Smart * into the request structures (kernel vm addr and dma address) 1888e399441dSJames Smart * thus the driver could use the buffers/mappings directly. 1889e399441dSJames Smart * It only makes sense if the LLDD would use them for its 1890e399441dSJames Smart * messaging api. It's very unlikely most adapter api's would use 1891e399441dSJames Smart * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload 1892e399441dSJames Smart * structures were used instead. 1893e399441dSJames Smart */ 1894e399441dSJames Smart } 1895e399441dSJames Smart 1896e399441dSJames Smart /* 1897e399441dSJames Smart * This routine terminates a queue at the transport level. 1898e399441dSJames Smart * The transport has already ensured that all outstanding ios on 1899e399441dSJames Smart * the queue have been terminated. 1900e399441dSJames Smart * The transport will send a Disconnect LS request to terminate 1901e399441dSJames Smart * the queue's connection. Termination of the admin queue will also 1902e399441dSJames Smart * terminate the association at the target. 1903e399441dSJames Smart */ 1904e399441dSJames Smart static void 1905e399441dSJames Smart nvme_fc_free_queue(struct nvme_fc_queue *queue) 1906e399441dSJames Smart { 1907e399441dSJames Smart if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags)) 1908e399441dSJames Smart return; 1909e399441dSJames Smart 19109e0ed16aSSagi Grimberg clear_bit(NVME_FC_Q_LIVE, &queue->flags); 1911e399441dSJames Smart /* 1912e399441dSJames Smart * Current implementation never disconnects a single queue. 1913e399441dSJames Smart * It always terminates a whole association. So there is never 1914e399441dSJames Smart * a disconnect(queue) LS sent to the target. 1915e399441dSJames Smart */ 1916e399441dSJames Smart 1917e399441dSJames Smart queue->connection_id = 0; 191867f471b6SJames Smart atomic_set(&queue->csn, 0); 1919e399441dSJames Smart } 1920e399441dSJames Smart 1921e399441dSJames Smart static void 1922e399441dSJames Smart __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl, 1923e399441dSJames Smart struct nvme_fc_queue *queue, unsigned int qidx) 1924e399441dSJames Smart { 1925e399441dSJames Smart if (ctrl->lport->ops->delete_queue) 1926e399441dSJames Smart ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx, 1927e399441dSJames Smart queue->lldd_handle); 1928e399441dSJames Smart queue->lldd_handle = NULL; 1929e399441dSJames Smart } 1930e399441dSJames Smart 1931e399441dSJames Smart static void 1932e399441dSJames Smart nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl) 1933e399441dSJames Smart { 1934e399441dSJames Smart int i; 1935e399441dSJames Smart 1936d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++) 1937e399441dSJames Smart nvme_fc_free_queue(&ctrl->queues[i]); 1938e399441dSJames Smart } 1939e399441dSJames Smart 1940e399441dSJames Smart static int 1941e399441dSJames Smart __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl, 1942e399441dSJames Smart struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize) 1943e399441dSJames Smart { 1944e399441dSJames Smart int ret = 0; 1945e399441dSJames Smart 1946e399441dSJames Smart queue->lldd_handle = NULL; 1947e399441dSJames Smart if (ctrl->lport->ops->create_queue) 1948e399441dSJames Smart ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport, 1949e399441dSJames Smart qidx, qsize, &queue->lldd_handle); 1950e399441dSJames Smart 1951e399441dSJames Smart return ret; 1952e399441dSJames Smart } 1953e399441dSJames Smart 1954e399441dSJames Smart static void 1955e399441dSJames Smart nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl) 1956e399441dSJames Smart { 1957d858e5f0SSagi Grimberg struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1]; 1958e399441dSJames Smart int i; 1959e399441dSJames Smart 1960d858e5f0SSagi Grimberg for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--) 1961e399441dSJames Smart __nvme_fc_delete_hw_queue(ctrl, queue, i); 1962e399441dSJames Smart } 1963e399441dSJames Smart 1964e399441dSJames Smart static int 1965e399441dSJames Smart nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize) 1966e399441dSJames Smart { 1967e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[1]; 196817a1ec08SJohannes Thumshirn int i, ret; 1969e399441dSJames Smart 1970d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) { 1971e399441dSJames Smart ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize); 197217a1ec08SJohannes Thumshirn if (ret) 197317a1ec08SJohannes Thumshirn goto delete_queues; 1974e399441dSJames Smart } 1975e399441dSJames Smart 1976e399441dSJames Smart return 0; 197717a1ec08SJohannes Thumshirn 197817a1ec08SJohannes Thumshirn delete_queues: 197917a1ec08SJohannes Thumshirn for (; i >= 0; i--) 198017a1ec08SJohannes Thumshirn __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i); 198117a1ec08SJohannes Thumshirn return ret; 1982e399441dSJames Smart } 1983e399441dSJames Smart 1984e399441dSJames Smart static int 1985e399441dSJames Smart nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize) 1986e399441dSJames Smart { 1987e399441dSJames Smart int i, ret = 0; 1988e399441dSJames Smart 1989d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++) { 1990e399441dSJames Smart ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize, 1991e399441dSJames Smart (qsize / 5)); 1992e399441dSJames Smart if (ret) 1993e399441dSJames Smart break; 199426c68227SSagi Grimberg ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false); 1995e399441dSJames Smart if (ret) 1996e399441dSJames Smart break; 19979e0ed16aSSagi Grimberg 19989e0ed16aSSagi Grimberg set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags); 1999e399441dSJames Smart } 2000e399441dSJames Smart 2001e399441dSJames Smart return ret; 2002e399441dSJames Smart } 2003e399441dSJames Smart 2004e399441dSJames Smart static void 2005e399441dSJames Smart nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl) 2006e399441dSJames Smart { 2007e399441dSJames Smart int i; 2008e399441dSJames Smart 2009d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++) 201008e15075SKeith Busch nvme_fc_init_queue(ctrl, i); 2011e399441dSJames Smart } 2012e399441dSJames Smart 2013e399441dSJames Smart static void 2014e399441dSJames Smart nvme_fc_ctrl_free(struct kref *ref) 2015e399441dSJames Smart { 2016e399441dSJames Smart struct nvme_fc_ctrl *ctrl = 2017e399441dSJames Smart container_of(ref, struct nvme_fc_ctrl, ref); 2018e399441dSJames Smart unsigned long flags; 2019e399441dSJames Smart 202061bff8efSJames Smart if (ctrl->ctrl.tagset) { 202161bff8efSJames Smart blk_cleanup_queue(ctrl->ctrl.connect_q); 202261bff8efSJames Smart blk_mq_free_tag_set(&ctrl->tag_set); 202361bff8efSJames Smart } 202461bff8efSJames Smart 2025e399441dSJames Smart /* remove from rport list */ 2026e399441dSJames Smart spin_lock_irqsave(&ctrl->rport->lock, flags); 2027e399441dSJames Smart list_del(&ctrl->ctrl_list); 2028e399441dSJames Smart spin_unlock_irqrestore(&ctrl->rport->lock, flags); 202961bff8efSJames Smart 2030f9c5af5fSSagi Grimberg blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); 203161bff8efSJames Smart blk_cleanup_queue(ctrl->ctrl.admin_q); 2032e7832cb4SSagi Grimberg blk_cleanup_queue(ctrl->ctrl.fabrics_q); 203361bff8efSJames Smart blk_mq_free_tag_set(&ctrl->admin_tag_set); 203461bff8efSJames Smart 203561bff8efSJames Smart kfree(ctrl->queues); 2036e399441dSJames Smart 2037e399441dSJames Smart put_device(ctrl->dev); 2038e399441dSJames Smart nvme_fc_rport_put(ctrl->rport); 2039e399441dSJames Smart 2040e399441dSJames Smart ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum); 2041de41447aSEwan D. Milne if (ctrl->ctrl.opts) 2042e399441dSJames Smart nvmf_free_options(ctrl->ctrl.opts); 2043e399441dSJames Smart kfree(ctrl); 2044e399441dSJames Smart } 2045e399441dSJames Smart 2046e399441dSJames Smart static void 2047e399441dSJames Smart nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl) 2048e399441dSJames Smart { 2049e399441dSJames Smart kref_put(&ctrl->ref, nvme_fc_ctrl_free); 2050e399441dSJames Smart } 2051e399441dSJames Smart 2052e399441dSJames Smart static int 2053e399441dSJames Smart nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl) 2054e399441dSJames Smart { 2055e399441dSJames Smart return kref_get_unless_zero(&ctrl->ref); 2056e399441dSJames Smart } 2057e399441dSJames Smart 2058e399441dSJames Smart /* 2059e399441dSJames Smart * All accesses from nvme core layer done - can now free the 2060e399441dSJames Smart * controller. Called after last nvme_put_ctrl() call 2061e399441dSJames Smart */ 2062e399441dSJames Smart static void 206361bff8efSJames Smart nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl) 2064e399441dSJames Smart { 2065e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 2066e399441dSJames Smart 2067e399441dSJames Smart WARN_ON(nctrl != &ctrl->ctrl); 2068e399441dSJames Smart 2069e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 2070e399441dSJames Smart } 2071e399441dSJames Smart 207261bff8efSJames Smart static void 207361bff8efSJames Smart nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg) 207461bff8efSJames Smart { 20754cff280aSJames Smart int active; 20764cff280aSJames Smart 20774cff280aSJames Smart /* 20784cff280aSJames Smart * if an error (io timeout, etc) while (re)connecting, 20794cff280aSJames Smart * it's an error on creating the new association. 20804cff280aSJames Smart * Start the error recovery thread if it hasn't already 20814cff280aSJames Smart * been started. It is expected there could be multiple 20824cff280aSJames Smart * ios hitting this path before things are cleaned up. 20834cff280aSJames Smart */ 20844cff280aSJames Smart if (ctrl->ctrl.state == NVME_CTRL_CONNECTING) { 20854cff280aSJames Smart active = atomic_xchg(&ctrl->err_work_active, 1); 20868730c1ddSHannes Reinecke if (!active && !queue_work(nvme_fc_wq, &ctrl->err_work)) { 20874cff280aSJames Smart atomic_set(&ctrl->err_work_active, 0); 20884cff280aSJames Smart WARN_ON(1); 20894cff280aSJames Smart } 20904cff280aSJames Smart return; 20914cff280aSJames Smart } 20924cff280aSJames Smart 20934cff280aSJames Smart /* Otherwise, only proceed if in LIVE state - e.g. on first error */ 209469fa9646SJames Smart if (ctrl->ctrl.state != NVME_CTRL_LIVE) 209569fa9646SJames Smart return; 209669fa9646SJames Smart 209761bff8efSJames Smart dev_warn(ctrl->ctrl.device, 209861bff8efSJames Smart "NVME-FC{%d}: transport association error detected: %s\n", 209961bff8efSJames Smart ctrl->cnum, errmsg); 2100589ff775SJames Smart dev_warn(ctrl->ctrl.device, 210161bff8efSJames Smart "NVME-FC{%d}: resetting controller\n", ctrl->cnum); 210261bff8efSJames Smart 2103d86c4d8eSChristoph Hellwig nvme_reset_ctrl(&ctrl->ctrl); 210461bff8efSJames Smart } 210561bff8efSJames Smart 2106baee29acSChristoph Hellwig static enum blk_eh_timer_return 2107e399441dSJames Smart nvme_fc_timeout(struct request *rq, bool reserved) 2108e399441dSJames Smart { 2109e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 2110e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl; 2111e399441dSJames Smart 2112e399441dSJames Smart /* 211361bff8efSJames Smart * we can't individually ABTS an io without affecting the queue, 2114041018c6SJames Smart * thus killing the queue, and thus the association. 211561bff8efSJames Smart * So resolve by performing a controller reset, which will stop 211661bff8efSJames Smart * the host/io stack, terminate the association on the link, 211761bff8efSJames Smart * and recreate an association on the link. 2118e399441dSJames Smart */ 211961bff8efSJames Smart nvme_fc_error_recovery(ctrl, "io timeout error"); 2120e399441dSJames Smart 2121134aedc9SJames Smart /* 2122134aedc9SJames Smart * the io abort has been initiated. Have the reset timer 2123134aedc9SJames Smart * restarted and the abort completion will complete the io 2124134aedc9SJames Smart * shortly. Avoids a synchronous wait while the abort finishes. 2125134aedc9SJames Smart */ 2126134aedc9SJames Smart return BLK_EH_RESET_TIMER; 2127e399441dSJames Smart } 2128e399441dSJames Smart 2129e399441dSJames Smart static int 2130e399441dSJames Smart nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq, 2131e399441dSJames Smart struct nvme_fc_fcp_op *op) 2132e399441dSJames Smart { 2133e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req; 2134e399441dSJames Smart int ret; 2135e399441dSJames Smart 2136e399441dSJames Smart freq->sg_cnt = 0; 2137e399441dSJames Smart 21389f7d8ae2SJames Smart if (!blk_rq_nr_phys_segments(rq)) 2139e399441dSJames Smart return 0; 2140e399441dSJames Smart 2141e399441dSJames Smart freq->sg_table.sgl = freq->first_sgl; 214219e420bbSChristoph Hellwig ret = sg_alloc_table_chained(&freq->sg_table, 21434635873cSMing Lei blk_rq_nr_phys_segments(rq), freq->sg_table.sgl, 2144b1ae1a23SIsrael Rukshin NVME_INLINE_SG_CNT); 2145e399441dSJames Smart if (ret) 2146e399441dSJames Smart return -ENOMEM; 2147e399441dSJames Smart 2148e399441dSJames Smart op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl); 214919e420bbSChristoph Hellwig WARN_ON(op->nents > blk_rq_nr_phys_segments(rq)); 2150e399441dSJames Smart freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl, 2151f15872c5SIsrael Rukshin op->nents, rq_dma_dir(rq)); 2152e399441dSJames Smart if (unlikely(freq->sg_cnt <= 0)) { 2153b1ae1a23SIsrael Rukshin sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT); 2154e399441dSJames Smart freq->sg_cnt = 0; 2155e399441dSJames Smart return -EFAULT; 2156e399441dSJames Smart } 2157e399441dSJames Smart 2158e399441dSJames Smart /* 2159e399441dSJames Smart * TODO: blk_integrity_rq(rq) for DIF 2160e399441dSJames Smart */ 2161e399441dSJames Smart return 0; 2162e399441dSJames Smart } 2163e399441dSJames Smart 2164e399441dSJames Smart static void 2165e399441dSJames Smart nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq, 2166e399441dSJames Smart struct nvme_fc_fcp_op *op) 2167e399441dSJames Smart { 2168e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req; 2169e399441dSJames Smart 2170e399441dSJames Smart if (!freq->sg_cnt) 2171e399441dSJames Smart return; 2172e399441dSJames Smart 2173e399441dSJames Smart fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents, 2174f15872c5SIsrael Rukshin rq_dma_dir(rq)); 2175e399441dSJames Smart 2176b1ae1a23SIsrael Rukshin sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT); 2177e399441dSJames Smart 2178e399441dSJames Smart freq->sg_cnt = 0; 2179e399441dSJames Smart } 2180e399441dSJames Smart 2181e399441dSJames Smart /* 2182e399441dSJames Smart * In FC, the queue is a logical thing. At transport connect, the target 2183e399441dSJames Smart * creates its "queue" and returns a handle that is to be given to the 2184e399441dSJames Smart * target whenever it posts something to the corresponding SQ. When an 2185e399441dSJames Smart * SQE is sent on a SQ, FC effectively considers the SQE, or rather the 2186e399441dSJames Smart * command contained within the SQE, an io, and assigns a FC exchange 2187e399441dSJames Smart * to it. The SQE and the associated SQ handle are sent in the initial 2188e399441dSJames Smart * CMD IU sents on the exchange. All transfers relative to the io occur 2189e399441dSJames Smart * as part of the exchange. The CQE is the last thing for the io, 2190e399441dSJames Smart * which is transferred (explicitly or implicitly) with the RSP IU 2191e399441dSJames Smart * sent on the exchange. After the CQE is received, the FC exchange is 2192e399441dSJames Smart * terminaed and the Exchange may be used on a different io. 2193e399441dSJames Smart * 2194e399441dSJames Smart * The transport to LLDD api has the transport making a request for a 2195e399441dSJames Smart * new fcp io request to the LLDD. The LLDD then allocates a FC exchange 2196e399441dSJames Smart * resource and transfers the command. The LLDD will then process all 2197e399441dSJames Smart * steps to complete the io. Upon completion, the transport done routine 2198e399441dSJames Smart * is called. 2199e399441dSJames Smart * 2200e399441dSJames Smart * So - while the operation is outstanding to the LLDD, there is a link 2201e399441dSJames Smart * level FC exchange resource that is also outstanding. This must be 2202e399441dSJames Smart * considered in all cleanup operations. 2203e399441dSJames Smart */ 2204fc17b653SChristoph Hellwig static blk_status_t 2205e399441dSJames Smart nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue, 2206e399441dSJames Smart struct nvme_fc_fcp_op *op, u32 data_len, 2207e399441dSJames Smart enum nvmefc_fcp_datadir io_dir) 2208e399441dSJames Smart { 2209e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 2210e399441dSJames Smart struct nvme_command *sqe = &cmdiu->sqe; 2211b12740d3SJames Smart int ret, opstate; 2212e399441dSJames Smart 221361bff8efSJames Smart /* 221461bff8efSJames Smart * before attempting to send the io, check to see if we believe 221561bff8efSJames Smart * the target device is present 221661bff8efSJames Smart */ 221761bff8efSJames Smart if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE) 221886ff7c2aSMing Lei return BLK_STS_RESOURCE; 221961bff8efSJames Smart 2220e399441dSJames Smart if (!nvme_fc_ctrl_get(ctrl)) 2221fc17b653SChristoph Hellwig return BLK_STS_IOERR; 2222e399441dSJames Smart 2223e399441dSJames Smart /* format the FC-NVME CMD IU and fcp_req */ 2224e399441dSJames Smart cmdiu->connection_id = cpu_to_be64(queue->connection_id); 2225e399441dSJames Smart cmdiu->data_len = cpu_to_be32(data_len); 2226e399441dSJames Smart switch (io_dir) { 2227e399441dSJames Smart case NVMEFC_FCP_WRITE: 2228e399441dSJames Smart cmdiu->flags = FCNVME_CMD_FLAGS_WRITE; 2229e399441dSJames Smart break; 2230e399441dSJames Smart case NVMEFC_FCP_READ: 2231e399441dSJames Smart cmdiu->flags = FCNVME_CMD_FLAGS_READ; 2232e399441dSJames Smart break; 2233e399441dSJames Smart case NVMEFC_FCP_NODATA: 2234e399441dSJames Smart cmdiu->flags = 0; 2235e399441dSJames Smart break; 2236e399441dSJames Smart } 2237e399441dSJames Smart op->fcp_req.payload_length = data_len; 2238e399441dSJames Smart op->fcp_req.io_dir = io_dir; 2239e399441dSJames Smart op->fcp_req.transferred_length = 0; 2240e399441dSJames Smart op->fcp_req.rcv_rsplen = 0; 224162eeacb0SJames Smart op->fcp_req.status = NVME_SC_SUCCESS; 2242e399441dSJames Smart op->fcp_req.sqid = cpu_to_le16(queue->qnum); 2243e399441dSJames Smart 2244e399441dSJames Smart /* 2245e399441dSJames Smart * validate per fabric rules, set fields mandated by fabric spec 2246e399441dSJames Smart * as well as those by FC-NVME spec. 2247e399441dSJames Smart */ 2248e399441dSJames Smart WARN_ON_ONCE(sqe->common.metadata); 2249e399441dSJames Smart sqe->common.flags |= NVME_CMD_SGL_METABUF; 2250e399441dSJames Smart 2251e399441dSJames Smart /* 2252d9d34c0bSJames Smart * format SQE DPTR field per FC-NVME rules: 2253d9d34c0bSJames Smart * type=0x5 Transport SGL Data Block Descriptor 2254d9d34c0bSJames Smart * subtype=0xA Transport-specific value 2255d9d34c0bSJames Smart * address=0 2256d9d34c0bSJames Smart * length=length of the data series 2257e399441dSJames Smart */ 2258d9d34c0bSJames Smart sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) | 2259d9d34c0bSJames Smart NVME_SGL_FMT_TRANSPORT_A; 2260e399441dSJames Smart sqe->rw.dptr.sgl.length = cpu_to_le32(data_len); 2261e399441dSJames Smart sqe->rw.dptr.sgl.addr = 0; 2262e399441dSJames Smart 226378a7ac26SJames Smart if (!(op->flags & FCOP_FLAGS_AEN)) { 2264e399441dSJames Smart ret = nvme_fc_map_data(ctrl, op->rq, op); 2265e399441dSJames Smart if (ret < 0) { 2266e399441dSJames Smart nvme_cleanup_cmd(op->rq); 2267e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 2268fc17b653SChristoph Hellwig if (ret == -ENOMEM || ret == -EAGAIN) 2269fc17b653SChristoph Hellwig return BLK_STS_RESOURCE; 2270fc17b653SChristoph Hellwig return BLK_STS_IOERR; 2271e399441dSJames Smart } 2272e399441dSJames Smart } 2273e399441dSJames Smart 2274e399441dSJames Smart fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma, 2275e399441dSJames Smart sizeof(op->cmd_iu), DMA_TO_DEVICE); 2276e399441dSJames Smart 2277e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_ACTIVE); 2278e399441dSJames Smart 227978a7ac26SJames Smart if (!(op->flags & FCOP_FLAGS_AEN)) 2280e399441dSJames Smart blk_mq_start_request(op->rq); 2281e399441dSJames Smart 228267f471b6SJames Smart cmdiu->csn = cpu_to_be32(atomic_inc_return(&queue->csn)); 2283e399441dSJames Smart ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport, 2284e399441dSJames Smart &ctrl->rport->remoteport, 2285e399441dSJames Smart queue->lldd_handle, &op->fcp_req); 2286e399441dSJames Smart 2287e399441dSJames Smart if (ret) { 228867f471b6SJames Smart /* 228967f471b6SJames Smart * If the lld fails to send the command is there an issue with 229067f471b6SJames Smart * the csn value? If the command that fails is the Connect, 229167f471b6SJames Smart * no - as the connection won't be live. If it is a command 229267f471b6SJames Smart * post-connect, it's possible a gap in csn may be created. 229367f471b6SJames Smart * Does this matter? As Linux initiators don't send fused 229467f471b6SJames Smart * commands, no. The gap would exist, but as there's nothing 229567f471b6SJames Smart * that depends on csn order to be delivered on the target 229667f471b6SJames Smart * side, it shouldn't hurt. It would be difficult for a 229767f471b6SJames Smart * target to even detect the csn gap as it has no idea when the 229867f471b6SJames Smart * cmd with the csn was supposed to arrive. 229967f471b6SJames Smart */ 2300b12740d3SJames Smart opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE); 2301b12740d3SJames Smart __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate); 2302b12740d3SJames Smart 23038b25f351SJames Smart if (!(op->flags & FCOP_FLAGS_AEN)) 2304e399441dSJames Smart nvme_fc_unmap_data(ctrl, op->rq, op); 2305e399441dSJames Smart 230616686f3aSMax Gurtovoy nvme_cleanup_cmd(op->rq); 2307e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 2308e399441dSJames Smart 23098b25f351SJames Smart if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE && 23108b25f351SJames Smart ret != -EBUSY) 2311fc17b653SChristoph Hellwig return BLK_STS_IOERR; 2312e399441dSJames Smart 231386ff7c2aSMing Lei return BLK_STS_RESOURCE; 2314e399441dSJames Smart } 2315e399441dSJames Smart 2316fc17b653SChristoph Hellwig return BLK_STS_OK; 2317e399441dSJames Smart } 2318e399441dSJames Smart 2319fc17b653SChristoph Hellwig static blk_status_t 2320e399441dSJames Smart nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx, 2321e399441dSJames Smart const struct blk_mq_queue_data *bd) 2322e399441dSJames Smart { 2323e399441dSJames Smart struct nvme_ns *ns = hctx->queue->queuedata; 2324e399441dSJames Smart struct nvme_fc_queue *queue = hctx->driver_data; 2325e399441dSJames Smart struct nvme_fc_ctrl *ctrl = queue->ctrl; 2326e399441dSJames Smart struct request *rq = bd->rq; 2327e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 2328e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 2329e399441dSJames Smart struct nvme_command *sqe = &cmdiu->sqe; 2330e399441dSJames Smart enum nvmefc_fcp_datadir io_dir; 23313bc32bb1SChristoph Hellwig bool queue_ready = test_bit(NVME_FC_Q_LIVE, &queue->flags); 2332e399441dSJames Smart u32 data_len; 2333fc17b653SChristoph Hellwig blk_status_t ret; 2334e399441dSJames Smart 23353bc32bb1SChristoph Hellwig if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE || 23363bc32bb1SChristoph Hellwig !nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready)) 23376cdefc6eSJames Smart return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq); 23389e0ed16aSSagi Grimberg 2339e399441dSJames Smart ret = nvme_setup_cmd(ns, rq, sqe); 2340e399441dSJames Smart if (ret) 2341e399441dSJames Smart return ret; 2342e399441dSJames Smart 23439f7d8ae2SJames Smart /* 23449f7d8ae2SJames Smart * nvme core doesn't quite treat the rq opaquely. Commands such 23459f7d8ae2SJames Smart * as WRITE ZEROES will return a non-zero rq payload_bytes yet 23469f7d8ae2SJames Smart * there is no actual payload to be transferred. 23479f7d8ae2SJames Smart * To get it right, key data transmission on there being 1 or 23489f7d8ae2SJames Smart * more physical segments in the sg list. If there is no 23499f7d8ae2SJames Smart * physical segments, there is no payload. 23509f7d8ae2SJames Smart */ 23519f7d8ae2SJames Smart if (blk_rq_nr_phys_segments(rq)) { 2352b131c61dSChristoph Hellwig data_len = blk_rq_payload_bytes(rq); 2353e399441dSJames Smart io_dir = ((rq_data_dir(rq) == WRITE) ? 2354e399441dSJames Smart NVMEFC_FCP_WRITE : NVMEFC_FCP_READ); 23559f7d8ae2SJames Smart } else { 23569f7d8ae2SJames Smart data_len = 0; 2357e399441dSJames Smart io_dir = NVMEFC_FCP_NODATA; 23589f7d8ae2SJames Smart } 23599f7d8ae2SJames Smart 2360e399441dSJames Smart 2361e399441dSJames Smart return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir); 2362e399441dSJames Smart } 2363e399441dSJames Smart 2364e399441dSJames Smart static void 2365ad22c355SKeith Busch nvme_fc_submit_async_event(struct nvme_ctrl *arg) 2366e399441dSJames Smart { 2367e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg); 2368e399441dSJames Smart struct nvme_fc_fcp_op *aen_op; 236961bff8efSJames Smart unsigned long flags; 237061bff8efSJames Smart bool terminating = false; 2371fc17b653SChristoph Hellwig blk_status_t ret; 2372e399441dSJames Smart 237361bff8efSJames Smart spin_lock_irqsave(&ctrl->lock, flags); 237461bff8efSJames Smart if (ctrl->flags & FCCTRL_TERMIO) 237561bff8efSJames Smart terminating = true; 237661bff8efSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 237761bff8efSJames Smart 237861bff8efSJames Smart if (terminating) 237961bff8efSJames Smart return; 238061bff8efSJames Smart 2381ad22c355SKeith Busch aen_op = &ctrl->aen_ops[0]; 2382e399441dSJames Smart 2383e399441dSJames Smart ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0, 2384e399441dSJames Smart NVMEFC_FCP_NODATA); 2385e399441dSJames Smart if (ret) 2386e399441dSJames Smart dev_err(ctrl->ctrl.device, 2387ad22c355SKeith Busch "failed async event work\n"); 2388e399441dSJames Smart } 2389e399441dSJames Smart 2390e399441dSJames Smart static void 2391c3aedd22SJames Smart nvme_fc_complete_rq(struct request *rq) 2392e399441dSJames Smart { 2393e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 2394e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl; 2395e399441dSJames Smart 239678a7ac26SJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE); 2397e399441dSJames Smart 2398e399441dSJames Smart nvme_fc_unmap_data(ctrl, rq, op); 239977f02a7aSChristoph Hellwig nvme_complete_rq(rq); 2400e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 240178a7ac26SJames Smart } 240278a7ac26SJames Smart 2403e399441dSJames Smart /* 2404e399441dSJames Smart * This routine is used by the transport when it needs to find active 2405e399441dSJames Smart * io on a queue that is to be terminated. The transport uses 2406e399441dSJames Smart * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke 2407e399441dSJames Smart * this routine to kill them on a 1 by 1 basis. 2408e399441dSJames Smart * 2409e399441dSJames Smart * As FC allocates FC exchange for each io, the transport must contact 2410e399441dSJames Smart * the LLDD to terminate the exchange, thus releasing the FC exchange. 2411e399441dSJames Smart * After terminating the exchange the LLDD will call the transport's 2412e399441dSJames Smart * normal io done path for the request, but it will have an aborted 2413e399441dSJames Smart * status. The done path will return the io request back to the block 2414e399441dSJames Smart * layer with an error status. 2415e399441dSJames Smart */ 24167baa8572SJens Axboe static bool 2417e399441dSJames Smart nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved) 2418e399441dSJames Smart { 2419e399441dSJames Smart struct nvme_ctrl *nctrl = data; 2420e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 2421e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req); 2422e399441dSJames Smart 24233efd6e8eSJames Smart __nvme_fc_abort_op(ctrl, op); 24247baa8572SJens Axboe return true; 242578a7ac26SJames Smart } 2426e399441dSJames Smart 2427e399441dSJames Smart 242861bff8efSJames Smart static const struct blk_mq_ops nvme_fc_mq_ops = { 242961bff8efSJames Smart .queue_rq = nvme_fc_queue_rq, 243061bff8efSJames Smart .complete = nvme_fc_complete_rq, 243161bff8efSJames Smart .init_request = nvme_fc_init_request, 243261bff8efSJames Smart .exit_request = nvme_fc_exit_request, 243361bff8efSJames Smart .init_hctx = nvme_fc_init_hctx, 243461bff8efSJames Smart .timeout = nvme_fc_timeout, 2435e399441dSJames Smart }; 2436e399441dSJames Smart 2437e399441dSJames Smart static int 2438e399441dSJames Smart nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl) 2439e399441dSJames Smart { 2440e399441dSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 24417314183dSSagi Grimberg unsigned int nr_io_queues; 2442e399441dSJames Smart int ret; 2443e399441dSJames Smart 24447314183dSSagi Grimberg nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()), 24457314183dSSagi Grimberg ctrl->lport->ops->max_hw_queues); 24467314183dSSagi Grimberg ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); 2447e399441dSJames Smart if (ret) { 2448e399441dSJames Smart dev_info(ctrl->ctrl.device, 2449e399441dSJames Smart "set_queue_count failed: %d\n", ret); 2450e399441dSJames Smart return ret; 2451e399441dSJames Smart } 2452e399441dSJames Smart 24537314183dSSagi Grimberg ctrl->ctrl.queue_count = nr_io_queues + 1; 24547314183dSSagi Grimberg if (!nr_io_queues) 2455e399441dSJames Smart return 0; 2456e399441dSJames Smart 2457e399441dSJames Smart nvme_fc_init_io_queues(ctrl); 2458e399441dSJames Smart 2459e399441dSJames Smart memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); 2460e399441dSJames Smart ctrl->tag_set.ops = &nvme_fc_mq_ops; 2461e399441dSJames Smart ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; 2462e399441dSJames Smart ctrl->tag_set.reserved_tags = 1; /* fabric connect */ 2463103e515eSHannes Reinecke ctrl->tag_set.numa_node = ctrl->ctrl.numa_node; 2464e399441dSJames Smart ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 2465d3d0bc78SBart Van Assche ctrl->tag_set.cmd_size = 2466d3d0bc78SBart Van Assche struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv, 2467d3d0bc78SBart Van Assche ctrl->lport->ops->fcprqst_priv_sz); 2468e399441dSJames Smart ctrl->tag_set.driver_data = ctrl; 2469d858e5f0SSagi Grimberg ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1; 2470e399441dSJames Smart ctrl->tag_set.timeout = NVME_IO_TIMEOUT; 2471e399441dSJames Smart 2472e399441dSJames Smart ret = blk_mq_alloc_tag_set(&ctrl->tag_set); 2473e399441dSJames Smart if (ret) 2474e399441dSJames Smart return ret; 2475e399441dSJames Smart 2476e399441dSJames Smart ctrl->ctrl.tagset = &ctrl->tag_set; 2477e399441dSJames Smart 2478e399441dSJames Smart ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); 2479e399441dSJames Smart if (IS_ERR(ctrl->ctrl.connect_q)) { 2480e399441dSJames Smart ret = PTR_ERR(ctrl->ctrl.connect_q); 2481e399441dSJames Smart goto out_free_tag_set; 2482e399441dSJames Smart } 2483e399441dSJames Smart 2484d157e534SJames Smart ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1); 2485e399441dSJames Smart if (ret) 2486e399441dSJames Smart goto out_cleanup_blk_queue; 2487e399441dSJames Smart 2488d157e534SJames Smart ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1); 2489e399441dSJames Smart if (ret) 2490e399441dSJames Smart goto out_delete_hw_queues; 2491e399441dSJames Smart 24924c984154SJames Smart ctrl->ioq_live = true; 24934c984154SJames Smart 2494e399441dSJames Smart return 0; 2495e399441dSJames Smart 2496e399441dSJames Smart out_delete_hw_queues: 2497e399441dSJames Smart nvme_fc_delete_hw_io_queues(ctrl); 2498e399441dSJames Smart out_cleanup_blk_queue: 2499e399441dSJames Smart blk_cleanup_queue(ctrl->ctrl.connect_q); 2500e399441dSJames Smart out_free_tag_set: 2501e399441dSJames Smart blk_mq_free_tag_set(&ctrl->tag_set); 2502e399441dSJames Smart nvme_fc_free_io_queues(ctrl); 2503e399441dSJames Smart 2504e399441dSJames Smart /* force put free routine to ignore io queues */ 2505e399441dSJames Smart ctrl->ctrl.tagset = NULL; 2506e399441dSJames Smart 2507e399441dSJames Smart return ret; 2508e399441dSJames Smart } 2509e399441dSJames Smart 251061bff8efSJames Smart static int 25113e493c00SJames Smart nvme_fc_recreate_io_queues(struct nvme_fc_ctrl *ctrl) 251261bff8efSJames Smart { 251361bff8efSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 2514834d3710SJames Smart u32 prior_ioq_cnt = ctrl->ctrl.queue_count - 1; 25157314183dSSagi Grimberg unsigned int nr_io_queues; 251661bff8efSJames Smart int ret; 251761bff8efSJames Smart 25187314183dSSagi Grimberg nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()), 25197314183dSSagi Grimberg ctrl->lport->ops->max_hw_queues); 25207314183dSSagi Grimberg ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); 252161bff8efSJames Smart if (ret) { 252261bff8efSJames Smart dev_info(ctrl->ctrl.device, 252361bff8efSJames Smart "set_queue_count failed: %d\n", ret); 252461bff8efSJames Smart return ret; 252561bff8efSJames Smart } 252661bff8efSJames Smart 2527834d3710SJames Smart if (!nr_io_queues && prior_ioq_cnt) { 2528834d3710SJames Smart dev_info(ctrl->ctrl.device, 2529834d3710SJames Smart "Fail Reconnect: At least 1 io queue " 2530834d3710SJames Smart "required (was %d)\n", prior_ioq_cnt); 2531834d3710SJames Smart return -ENOSPC; 2532834d3710SJames Smart } 2533834d3710SJames Smart 25347314183dSSagi Grimberg ctrl->ctrl.queue_count = nr_io_queues + 1; 253561bff8efSJames Smart /* check for io queues existing */ 2536d858e5f0SSagi Grimberg if (ctrl->ctrl.queue_count == 1) 253761bff8efSJames Smart return 0; 253861bff8efSJames Smart 2539d157e534SJames Smart ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.sqsize + 1); 254061bff8efSJames Smart if (ret) 254161bff8efSJames Smart goto out_free_io_queues; 254261bff8efSJames Smart 2543d157e534SJames Smart ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.sqsize + 1); 254461bff8efSJames Smart if (ret) 254561bff8efSJames Smart goto out_delete_hw_queues; 254661bff8efSJames Smart 2547834d3710SJames Smart if (prior_ioq_cnt != nr_io_queues) 2548834d3710SJames Smart dev_info(ctrl->ctrl.device, 2549834d3710SJames Smart "reconnect: revising io queue count from %d to %d\n", 2550834d3710SJames Smart prior_ioq_cnt, nr_io_queues); 2551cda5fd1aSSagi Grimberg blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues); 2552cda5fd1aSSagi Grimberg 255361bff8efSJames Smart return 0; 255461bff8efSJames Smart 255561bff8efSJames Smart out_delete_hw_queues: 255661bff8efSJames Smart nvme_fc_delete_hw_io_queues(ctrl); 255761bff8efSJames Smart out_free_io_queues: 255861bff8efSJames Smart nvme_fc_free_io_queues(ctrl); 255961bff8efSJames Smart return ret; 256061bff8efSJames Smart } 256161bff8efSJames Smart 2562158bfb88SJames Smart static void 2563158bfb88SJames Smart nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport) 2564158bfb88SJames Smart { 2565158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport; 2566158bfb88SJames Smart 2567158bfb88SJames Smart atomic_inc(&lport->act_rport_cnt); 2568158bfb88SJames Smart } 2569158bfb88SJames Smart 2570158bfb88SJames Smart static void 2571158bfb88SJames Smart nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport) 2572158bfb88SJames Smart { 2573158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport; 2574158bfb88SJames Smart u32 cnt; 2575158bfb88SJames Smart 2576158bfb88SJames Smart cnt = atomic_dec_return(&lport->act_rport_cnt); 2577158bfb88SJames Smart if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED) 2578158bfb88SJames Smart lport->ops->localport_delete(&lport->localport); 2579158bfb88SJames Smart } 2580158bfb88SJames Smart 2581158bfb88SJames Smart static int 2582158bfb88SJames Smart nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl) 2583158bfb88SJames Smart { 2584158bfb88SJames Smart struct nvme_fc_rport *rport = ctrl->rport; 2585158bfb88SJames Smart u32 cnt; 2586158bfb88SJames Smart 2587158bfb88SJames Smart if (ctrl->assoc_active) 2588158bfb88SJames Smart return 1; 2589158bfb88SJames Smart 2590158bfb88SJames Smart ctrl->assoc_active = true; 2591158bfb88SJames Smart cnt = atomic_inc_return(&rport->act_ctrl_cnt); 2592158bfb88SJames Smart if (cnt == 1) 2593158bfb88SJames Smart nvme_fc_rport_active_on_lport(rport); 2594158bfb88SJames Smart 2595158bfb88SJames Smart return 0; 2596158bfb88SJames Smart } 2597158bfb88SJames Smart 2598158bfb88SJames Smart static int 2599158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl) 2600158bfb88SJames Smart { 2601158bfb88SJames Smart struct nvme_fc_rport *rport = ctrl->rport; 2602158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport; 2603158bfb88SJames Smart u32 cnt; 2604158bfb88SJames Smart 2605158bfb88SJames Smart /* ctrl->assoc_active=false will be set independently */ 2606158bfb88SJames Smart 2607158bfb88SJames Smart cnt = atomic_dec_return(&rport->act_ctrl_cnt); 2608158bfb88SJames Smart if (cnt == 0) { 2609158bfb88SJames Smart if (rport->remoteport.port_state == FC_OBJSTATE_DELETED) 2610158bfb88SJames Smart lport->ops->remoteport_delete(&rport->remoteport); 2611158bfb88SJames Smart nvme_fc_rport_inactive_on_lport(rport); 2612158bfb88SJames Smart } 2613158bfb88SJames Smart 2614158bfb88SJames Smart return 0; 2615158bfb88SJames Smart } 2616158bfb88SJames Smart 261761bff8efSJames Smart /* 261861bff8efSJames Smart * This routine restarts the controller on the host side, and 261961bff8efSJames Smart * on the link side, recreates the controller association. 262061bff8efSJames Smart */ 262161bff8efSJames Smart static int 262261bff8efSJames Smart nvme_fc_create_association(struct nvme_fc_ctrl *ctrl) 262361bff8efSJames Smart { 262461bff8efSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 262561bff8efSJames Smart int ret; 262661bff8efSJames Smart bool changed; 262761bff8efSJames Smart 2628fdf9dfa8SSagi Grimberg ++ctrl->ctrl.nr_reconnects; 262961bff8efSJames Smart 263096e24801SJames Smart if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE) 263196e24801SJames Smart return -ENODEV; 263296e24801SJames Smart 2633158bfb88SJames Smart if (nvme_fc_ctlr_active_on_rport(ctrl)) 2634158bfb88SJames Smart return -ENOTUNIQ; 2635158bfb88SJames Smart 26364bea364fSJames Smart dev_info(ctrl->ctrl.device, 26374bea364fSJames Smart "NVME-FC{%d}: create association : host wwpn 0x%016llx " 26384bea364fSJames Smart " rport wwpn 0x%016llx: NQN \"%s\"\n", 26394bea364fSJames Smart ctrl->cnum, ctrl->lport->localport.port_name, 26404bea364fSJames Smart ctrl->rport->remoteport.port_name, ctrl->ctrl.opts->subsysnqn); 26414bea364fSJames Smart 264261bff8efSJames Smart /* 264361bff8efSJames Smart * Create the admin queue 264461bff8efSJames Smart */ 264561bff8efSJames Smart 264661bff8efSJames Smart ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0, 2647d157e534SJames Smart NVME_AQ_DEPTH); 264861bff8efSJames Smart if (ret) 264961bff8efSJames Smart goto out_free_queue; 265061bff8efSJames Smart 265161bff8efSJames Smart ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0], 2652d157e534SJames Smart NVME_AQ_DEPTH, (NVME_AQ_DEPTH / 4)); 265361bff8efSJames Smart if (ret) 265461bff8efSJames Smart goto out_delete_hw_queue; 265561bff8efSJames Smart 265661bff8efSJames Smart ret = nvmf_connect_admin_queue(&ctrl->ctrl); 265761bff8efSJames Smart if (ret) 265861bff8efSJames Smart goto out_disconnect_admin_queue; 265961bff8efSJames Smart 26609e0ed16aSSagi Grimberg set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags); 26619e0ed16aSSagi Grimberg 266261bff8efSJames Smart /* 266361bff8efSJames Smart * Check controller capabilities 266461bff8efSJames Smart * 266561bff8efSJames Smart * todo:- add code to check if ctrl attributes changed from 266661bff8efSJames Smart * prior connection values 266761bff8efSJames Smart */ 266861bff8efSJames Smart 2669c0f2f45bSSagi Grimberg ret = nvme_enable_ctrl(&ctrl->ctrl); 267061bff8efSJames Smart if (ret) 267161bff8efSJames Smart goto out_disconnect_admin_queue; 267261bff8efSJames Smart 2673ecad0d2cSJames Smart ctrl->ctrl.max_hw_sectors = 2674ecad0d2cSJames Smart (ctrl->lport->ops->max_sgl_segments - 1) << (PAGE_SHIFT - 9); 267561bff8efSJames Smart 2676e7832cb4SSagi Grimberg blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); 2677e7832cb4SSagi Grimberg 267861bff8efSJames Smart ret = nvme_init_identify(&ctrl->ctrl); 267961bff8efSJames Smart if (ret) 268061bff8efSJames Smart goto out_disconnect_admin_queue; 268161bff8efSJames Smart 268261bff8efSJames Smart /* sanity checks */ 268361bff8efSJames Smart 268461bff8efSJames Smart /* FC-NVME does not have other data in the capsule */ 268561bff8efSJames Smart if (ctrl->ctrl.icdoff) { 268661bff8efSJames Smart dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n", 268761bff8efSJames Smart ctrl->ctrl.icdoff); 268861bff8efSJames Smart goto out_disconnect_admin_queue; 268961bff8efSJames Smart } 269061bff8efSJames Smart 269161bff8efSJames Smart /* FC-NVME supports normal SGL Data Block Descriptors */ 269261bff8efSJames Smart 269361bff8efSJames Smart if (opts->queue_size > ctrl->ctrl.maxcmd) { 269461bff8efSJames Smart /* warn if maxcmd is lower than queue_size */ 269561bff8efSJames Smart dev_warn(ctrl->ctrl.device, 269661bff8efSJames Smart "queue_size %zu > ctrl maxcmd %u, reducing " 26977db39484SJames Smart "to maxcmd\n", 269861bff8efSJames Smart opts->queue_size, ctrl->ctrl.maxcmd); 269961bff8efSJames Smart opts->queue_size = ctrl->ctrl.maxcmd; 270061bff8efSJames Smart } 270161bff8efSJames Smart 2702d157e534SJames Smart if (opts->queue_size > ctrl->ctrl.sqsize + 1) { 2703d157e534SJames Smart /* warn if sqsize is lower than queue_size */ 2704d157e534SJames Smart dev_warn(ctrl->ctrl.device, 27057db39484SJames Smart "queue_size %zu > ctrl sqsize %u, reducing " 27067db39484SJames Smart "to sqsize\n", 2707d157e534SJames Smart opts->queue_size, ctrl->ctrl.sqsize + 1); 2708d157e534SJames Smart opts->queue_size = ctrl->ctrl.sqsize + 1; 2709d157e534SJames Smart } 2710d157e534SJames Smart 271161bff8efSJames Smart ret = nvme_fc_init_aen_ops(ctrl); 271261bff8efSJames Smart if (ret) 271361bff8efSJames Smart goto out_term_aen_ops; 271461bff8efSJames Smart 271561bff8efSJames Smart /* 271661bff8efSJames Smart * Create the io queues 271761bff8efSJames Smart */ 271861bff8efSJames Smart 2719d858e5f0SSagi Grimberg if (ctrl->ctrl.queue_count > 1) { 27204c984154SJames Smart if (!ctrl->ioq_live) 272161bff8efSJames Smart ret = nvme_fc_create_io_queues(ctrl); 272261bff8efSJames Smart else 27233e493c00SJames Smart ret = nvme_fc_recreate_io_queues(ctrl); 272461bff8efSJames Smart if (ret) 272561bff8efSJames Smart goto out_term_aen_ops; 272661bff8efSJames Smart } 272761bff8efSJames Smart 272861bff8efSJames Smart changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 272961bff8efSJames Smart 2730fdf9dfa8SSagi Grimberg ctrl->ctrl.nr_reconnects = 0; 273161bff8efSJames Smart 273244c6ec77SJames Smart if (changed) 2733d09f2b45SSagi Grimberg nvme_start_ctrl(&ctrl->ctrl); 273461bff8efSJames Smart 273561bff8efSJames Smart return 0; /* Success */ 273661bff8efSJames Smart 273761bff8efSJames Smart out_term_aen_ops: 273861bff8efSJames Smart nvme_fc_term_aen_ops(ctrl); 273961bff8efSJames Smart out_disconnect_admin_queue: 274061bff8efSJames Smart /* send a Disconnect(association) LS to fc-nvme target */ 274161bff8efSJames Smart nvme_fc_xmt_disconnect_assoc(ctrl); 2742bcde5f0fSJames Smart ctrl->association_id = 0; 274361bff8efSJames Smart out_delete_hw_queue: 274461bff8efSJames Smart __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0); 274561bff8efSJames Smart out_free_queue: 274661bff8efSJames Smart nvme_fc_free_queue(&ctrl->queues[0]); 2747158bfb88SJames Smart ctrl->assoc_active = false; 2748158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(ctrl); 274961bff8efSJames Smart 275061bff8efSJames Smart return ret; 275161bff8efSJames Smart } 275261bff8efSJames Smart 275361bff8efSJames Smart /* 275461bff8efSJames Smart * This routine stops operation of the controller on the host side. 275561bff8efSJames Smart * On the host os stack side: Admin and IO queues are stopped, 275661bff8efSJames Smart * outstanding ios on them terminated via FC ABTS. 275761bff8efSJames Smart * On the link side: the association is terminated. 275861bff8efSJames Smart */ 275961bff8efSJames Smart static void 276061bff8efSJames Smart nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl) 276161bff8efSJames Smart { 276261bff8efSJames Smart unsigned long flags; 276361bff8efSJames Smart 2764158bfb88SJames Smart if (!ctrl->assoc_active) 2765158bfb88SJames Smart return; 2766158bfb88SJames Smart ctrl->assoc_active = false; 2767158bfb88SJames Smart 276861bff8efSJames Smart spin_lock_irqsave(&ctrl->lock, flags); 276961bff8efSJames Smart ctrl->flags |= FCCTRL_TERMIO; 277061bff8efSJames Smart ctrl->iocnt = 0; 277161bff8efSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 277261bff8efSJames Smart 277361bff8efSJames Smart /* 277461bff8efSJames Smart * If io queues are present, stop them and terminate all outstanding 277561bff8efSJames Smart * ios on them. As FC allocates FC exchange for each io, the 277661bff8efSJames Smart * transport must contact the LLDD to terminate the exchange, 277761bff8efSJames Smart * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr() 277861bff8efSJames Smart * to tell us what io's are busy and invoke a transport routine 277961bff8efSJames Smart * to kill them with the LLDD. After terminating the exchange 278061bff8efSJames Smart * the LLDD will call the transport's normal io done path, but it 278161bff8efSJames Smart * will have an aborted status. The done path will return the 278261bff8efSJames Smart * io requests back to the block layer as part of normal completions 278361bff8efSJames Smart * (but with error status). 278461bff8efSJames Smart */ 2785d858e5f0SSagi Grimberg if (ctrl->ctrl.queue_count > 1) { 278661bff8efSJames Smart nvme_stop_queues(&ctrl->ctrl); 278761bff8efSJames Smart blk_mq_tagset_busy_iter(&ctrl->tag_set, 278861bff8efSJames Smart nvme_fc_terminate_exchange, &ctrl->ctrl); 2789622b8b68SMing Lei blk_mq_tagset_wait_completed_request(&ctrl->tag_set); 279061bff8efSJames Smart } 279161bff8efSJames Smart 279261bff8efSJames Smart /* 279361bff8efSJames Smart * Other transports, which don't have link-level contexts bound 279461bff8efSJames Smart * to sqe's, would try to gracefully shutdown the controller by 279561bff8efSJames Smart * writing the registers for shutdown and polling (call 279661bff8efSJames Smart * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially 279761bff8efSJames Smart * just aborted and we will wait on those contexts, and given 279861bff8efSJames Smart * there was no indication of how live the controlelr is on the 279961bff8efSJames Smart * link, don't send more io to create more contexts for the 280061bff8efSJames Smart * shutdown. Let the controller fail via keepalive failure if 280161bff8efSJames Smart * its still present. 280261bff8efSJames Smart */ 280361bff8efSJames Smart 280461bff8efSJames Smart /* 280561bff8efSJames Smart * clean up the admin queue. Same thing as above. 280661bff8efSJames Smart * use blk_mq_tagset_busy_itr() and the transport routine to 280761bff8efSJames Smart * terminate the exchanges. 280861bff8efSJames Smart */ 2809f9c5af5fSSagi Grimberg blk_mq_quiesce_queue(ctrl->ctrl.admin_q); 281061bff8efSJames Smart blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, 281161bff8efSJames Smart nvme_fc_terminate_exchange, &ctrl->ctrl); 2812622b8b68SMing Lei blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set); 281361bff8efSJames Smart 281461bff8efSJames Smart /* kill the aens as they are a separate path */ 281561bff8efSJames Smart nvme_fc_abort_aen_ops(ctrl); 281661bff8efSJames Smart 281761bff8efSJames Smart /* wait for all io that had to be aborted */ 28188a82dbf1SJames Smart spin_lock_irq(&ctrl->lock); 281936715cf4SJames Smart wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock); 282061bff8efSJames Smart ctrl->flags &= ~FCCTRL_TERMIO; 28218a82dbf1SJames Smart spin_unlock_irq(&ctrl->lock); 282261bff8efSJames Smart 282361bff8efSJames Smart nvme_fc_term_aen_ops(ctrl); 282461bff8efSJames Smart 282561bff8efSJames Smart /* 282661bff8efSJames Smart * send a Disconnect(association) LS to fc-nvme target 282761bff8efSJames Smart * Note: could have been sent at top of process, but 282861bff8efSJames Smart * cleaner on link traffic if after the aborts complete. 282961bff8efSJames Smart * Note: if association doesn't exist, association_id will be 0 283061bff8efSJames Smart */ 283161bff8efSJames Smart if (ctrl->association_id) 283261bff8efSJames Smart nvme_fc_xmt_disconnect_assoc(ctrl); 283361bff8efSJames Smart 2834bcde5f0fSJames Smart ctrl->association_id = 0; 2835bcde5f0fSJames Smart 283661bff8efSJames Smart if (ctrl->ctrl.tagset) { 283761bff8efSJames Smart nvme_fc_delete_hw_io_queues(ctrl); 283861bff8efSJames Smart nvme_fc_free_io_queues(ctrl); 283961bff8efSJames Smart } 284061bff8efSJames Smart 284161bff8efSJames Smart __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0); 284261bff8efSJames Smart nvme_fc_free_queue(&ctrl->queues[0]); 2843158bfb88SJames Smart 2844d625d05eSJames Smart /* re-enable the admin_q so anything new can fast fail */ 2845d625d05eSJames Smart blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); 2846d625d05eSJames Smart 284702d62a8bSJames Smart /* resume the io queues so that things will fast fail */ 284802d62a8bSJames Smart nvme_start_queues(&ctrl->ctrl); 284902d62a8bSJames Smart 2850158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(ctrl); 285161bff8efSJames Smart } 285261bff8efSJames Smart 285361bff8efSJames Smart static void 2854c5017e85SChristoph Hellwig nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl) 285561bff8efSJames Smart { 2856c5017e85SChristoph Hellwig struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 285761bff8efSJames Smart 28584cff280aSJames Smart cancel_work_sync(&ctrl->err_work); 285961bff8efSJames Smart cancel_delayed_work_sync(&ctrl->connect_work); 286061bff8efSJames Smart /* 286161bff8efSJames Smart * kill the association on the link side. this will block 286261bff8efSJames Smart * waiting for io to terminate 286361bff8efSJames Smart */ 286461bff8efSJames Smart nvme_fc_delete_association(ctrl); 286561bff8efSJames Smart } 286661bff8efSJames Smart 286761bff8efSJames Smart static void 28685bbecdbcSJames Smart nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status) 28695bbecdbcSJames Smart { 28702b632970SJames Smart struct nvme_fc_rport *rport = ctrl->rport; 28712b632970SJames Smart struct nvme_fc_remote_port *portptr = &rport->remoteport; 28722b632970SJames Smart unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ; 28732b632970SJames Smart bool recon = true; 28745bbecdbcSJames Smart 2875ad6a0a52SMax Gurtovoy if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) 28765bbecdbcSJames Smart return; 28775bbecdbcSJames Smart 28782b632970SJames Smart if (portptr->port_state == FC_OBJSTATE_ONLINE) 2879589ff775SJames Smart dev_info(ctrl->ctrl.device, 28805bbecdbcSJames Smart "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n", 28815bbecdbcSJames Smart ctrl->cnum, status); 28822b632970SJames Smart else if (time_after_eq(jiffies, rport->dev_loss_end)) 28832b632970SJames Smart recon = false; 28845bbecdbcSJames Smart 28852b632970SJames Smart if (recon && nvmf_should_reconnect(&ctrl->ctrl)) { 28862b632970SJames Smart if (portptr->port_state == FC_OBJSTATE_ONLINE) 28875bbecdbcSJames Smart dev_info(ctrl->ctrl.device, 28882b632970SJames Smart "NVME-FC{%d}: Reconnect attempt in %ld " 28892b632970SJames Smart "seconds\n", 28902b632970SJames Smart ctrl->cnum, recon_delay / HZ); 28912b632970SJames Smart else if (time_after(jiffies + recon_delay, rport->dev_loss_end)) 28922b632970SJames Smart recon_delay = rport->dev_loss_end - jiffies; 28932b632970SJames Smart 28942b632970SJames Smart queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay); 28955bbecdbcSJames Smart } else { 28962b632970SJames Smart if (portptr->port_state == FC_OBJSTATE_ONLINE) 2897589ff775SJames Smart dev_warn(ctrl->ctrl.device, 28985bbecdbcSJames Smart "NVME-FC{%d}: Max reconnect attempts (%d) " 289977d0612dSMax Gurtovoy "reached.\n", 2900fdf9dfa8SSagi Grimberg ctrl->cnum, ctrl->ctrl.nr_reconnects); 29012b632970SJames Smart else 29022b632970SJames Smart dev_warn(ctrl->ctrl.device, 29032b632970SJames Smart "NVME-FC{%d}: dev_loss_tmo (%d) expired " 290477d0612dSMax Gurtovoy "while waiting for remoteport connectivity.\n", 290577d0612dSMax Gurtovoy ctrl->cnum, portptr->dev_loss_tmo); 2906c5017e85SChristoph Hellwig WARN_ON(nvme_delete_ctrl(&ctrl->ctrl)); 29075bbecdbcSJames Smart } 29085bbecdbcSJames Smart } 29095bbecdbcSJames Smart 29105bbecdbcSJames Smart static void 29114cff280aSJames Smart __nvme_fc_terminate_io(struct nvme_fc_ctrl *ctrl) 29124cff280aSJames Smart { 2913c869e494SJames Smart /* 2914c869e494SJames Smart * if state is connecting - the error occurred as part of a 2915c869e494SJames Smart * reconnect attempt. The create_association error paths will 2916c869e494SJames Smart * clean up any outstanding io. 2917c869e494SJames Smart * 2918c869e494SJames Smart * if it's a different state - ensure all pending io is 2919c869e494SJames Smart * terminated. Given this can delay while waiting for the 2920c869e494SJames Smart * aborted io to return, we recheck adapter state below 2921c869e494SJames Smart * before changing state. 2922c869e494SJames Smart */ 2923c869e494SJames Smart if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) { 29244cff280aSJames Smart nvme_stop_keep_alive(&ctrl->ctrl); 29254cff280aSJames Smart 29264cff280aSJames Smart /* will block will waiting for io to terminate */ 29274cff280aSJames Smart nvme_fc_delete_association(ctrl); 2928c869e494SJames Smart } 29294cff280aSJames Smart 29304cff280aSJames Smart if (ctrl->ctrl.state != NVME_CTRL_CONNECTING && 29314cff280aSJames Smart !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) 29324cff280aSJames Smart dev_err(ctrl->ctrl.device, 29334cff280aSJames Smart "NVME-FC{%d}: error_recovery: Couldn't change state " 29344cff280aSJames Smart "to CONNECTING\n", ctrl->cnum); 29354cff280aSJames Smart } 29364cff280aSJames Smart 29374cff280aSJames Smart static void 293861bff8efSJames Smart nvme_fc_reset_ctrl_work(struct work_struct *work) 293961bff8efSJames Smart { 294061bff8efSJames Smart struct nvme_fc_ctrl *ctrl = 2941d86c4d8eSChristoph Hellwig container_of(work, struct nvme_fc_ctrl, ctrl.reset_work); 294261bff8efSJames Smart int ret; 294361bff8efSJames Smart 29444cff280aSJames Smart __nvme_fc_terminate_io(ctrl); 29454cff280aSJames Smart 2946d09f2b45SSagi Grimberg nvme_stop_ctrl(&ctrl->ctrl); 294744c6ec77SJames Smart 29482b632970SJames Smart if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) 294961bff8efSJames Smart ret = nvme_fc_create_association(ctrl); 29502b632970SJames Smart else 29512b632970SJames Smart ret = -ENOTCONN; 29522b632970SJames Smart 29535bbecdbcSJames Smart if (ret) 29545bbecdbcSJames Smart nvme_fc_reconnect_or_delete(ctrl, ret); 29555bbecdbcSJames Smart else 295661bff8efSJames Smart dev_info(ctrl->ctrl.device, 295796e24801SJames Smart "NVME-FC{%d}: controller reset complete\n", 295896e24801SJames Smart ctrl->cnum); 295961bff8efSJames Smart } 296061bff8efSJames Smart 29614cff280aSJames Smart static void 29624cff280aSJames Smart nvme_fc_connect_err_work(struct work_struct *work) 29634cff280aSJames Smart { 29644cff280aSJames Smart struct nvme_fc_ctrl *ctrl = 29654cff280aSJames Smart container_of(work, struct nvme_fc_ctrl, err_work); 29664cff280aSJames Smart 29674cff280aSJames Smart __nvme_fc_terminate_io(ctrl); 29684cff280aSJames Smart 29694cff280aSJames Smart atomic_set(&ctrl->err_work_active, 0); 29704cff280aSJames Smart 29714cff280aSJames Smart /* 29724cff280aSJames Smart * Rescheduling the connection after recovering 29734cff280aSJames Smart * from the io error is left to the reconnect work 29744cff280aSJames Smart * item, which is what should have stalled waiting on 29754cff280aSJames Smart * the io that had the error that scheduled this work. 29764cff280aSJames Smart */ 29774cff280aSJames Smart } 29784cff280aSJames Smart 297961bff8efSJames Smart static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = { 298061bff8efSJames Smart .name = "fc", 298161bff8efSJames Smart .module = THIS_MODULE, 2982d3d5b87dSChristoph Hellwig .flags = NVME_F_FABRICS, 298361bff8efSJames Smart .reg_read32 = nvmf_reg_read32, 298461bff8efSJames Smart .reg_read64 = nvmf_reg_read64, 298561bff8efSJames Smart .reg_write32 = nvmf_reg_write32, 298661bff8efSJames Smart .free_ctrl = nvme_fc_nvme_ctrl_freed, 298761bff8efSJames Smart .submit_async_event = nvme_fc_submit_async_event, 2988c5017e85SChristoph Hellwig .delete_ctrl = nvme_fc_delete_ctrl, 298961bff8efSJames Smart .get_address = nvmf_get_address, 299061bff8efSJames Smart }; 299161bff8efSJames Smart 299261bff8efSJames Smart static void 299361bff8efSJames Smart nvme_fc_connect_ctrl_work(struct work_struct *work) 299461bff8efSJames Smart { 299561bff8efSJames Smart int ret; 299661bff8efSJames Smart 299761bff8efSJames Smart struct nvme_fc_ctrl *ctrl = 299861bff8efSJames Smart container_of(to_delayed_work(work), 299961bff8efSJames Smart struct nvme_fc_ctrl, connect_work); 300061bff8efSJames Smart 300161bff8efSJames Smart ret = nvme_fc_create_association(ctrl); 30025bbecdbcSJames Smart if (ret) 30035bbecdbcSJames Smart nvme_fc_reconnect_or_delete(ctrl, ret); 30045bbecdbcSJames Smart else 300561bff8efSJames Smart dev_info(ctrl->ctrl.device, 30064c984154SJames Smart "NVME-FC{%d}: controller connect complete\n", 300761bff8efSJames Smart ctrl->cnum); 300861bff8efSJames Smart } 300961bff8efSJames Smart 301061bff8efSJames Smart 301161bff8efSJames Smart static const struct blk_mq_ops nvme_fc_admin_mq_ops = { 301261bff8efSJames Smart .queue_rq = nvme_fc_queue_rq, 301361bff8efSJames Smart .complete = nvme_fc_complete_rq, 301476f983cbSChristoph Hellwig .init_request = nvme_fc_init_request, 301561bff8efSJames Smart .exit_request = nvme_fc_exit_request, 301661bff8efSJames Smart .init_hctx = nvme_fc_init_admin_hctx, 301761bff8efSJames Smart .timeout = nvme_fc_timeout, 301861bff8efSJames Smart }; 301961bff8efSJames Smart 3020e399441dSJames Smart 302156d5f4f1SJames Smart /* 302256d5f4f1SJames Smart * Fails a controller request if it matches an existing controller 302356d5f4f1SJames Smart * (association) with the same tuple: 302456d5f4f1SJames Smart * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN> 302556d5f4f1SJames Smart * 302656d5f4f1SJames Smart * The ports don't need to be compared as they are intrinsically 302756d5f4f1SJames Smart * already matched by the port pointers supplied. 302856d5f4f1SJames Smart */ 302956d5f4f1SJames Smart static bool 303056d5f4f1SJames Smart nvme_fc_existing_controller(struct nvme_fc_rport *rport, 303156d5f4f1SJames Smart struct nvmf_ctrl_options *opts) 303256d5f4f1SJames Smart { 303356d5f4f1SJames Smart struct nvme_fc_ctrl *ctrl; 303456d5f4f1SJames Smart unsigned long flags; 303556d5f4f1SJames Smart bool found = false; 303656d5f4f1SJames Smart 303756d5f4f1SJames Smart spin_lock_irqsave(&rport->lock, flags); 303856d5f4f1SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) { 303956d5f4f1SJames Smart found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts); 304056d5f4f1SJames Smart if (found) 304156d5f4f1SJames Smart break; 304256d5f4f1SJames Smart } 304356d5f4f1SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 304456d5f4f1SJames Smart 304556d5f4f1SJames Smart return found; 304656d5f4f1SJames Smart } 304756d5f4f1SJames Smart 3048e399441dSJames Smart static struct nvme_ctrl * 304961bff8efSJames Smart nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts, 3050e399441dSJames Smart struct nvme_fc_lport *lport, struct nvme_fc_rport *rport) 3051e399441dSJames Smart { 3052e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 3053e399441dSJames Smart unsigned long flags; 30544c984154SJames Smart int ret, idx; 3055e399441dSJames Smart 305685e6a6adSJames Smart if (!(rport->remoteport.port_role & 305785e6a6adSJames Smart (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) { 305885e6a6adSJames Smart ret = -EBADR; 305985e6a6adSJames Smart goto out_fail; 306085e6a6adSJames Smart } 306185e6a6adSJames Smart 306256d5f4f1SJames Smart if (!opts->duplicate_connect && 306356d5f4f1SJames Smart nvme_fc_existing_controller(rport, opts)) { 306456d5f4f1SJames Smart ret = -EALREADY; 306556d5f4f1SJames Smart goto out_fail; 306656d5f4f1SJames Smart } 306756d5f4f1SJames Smart 3068e399441dSJames Smart ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 3069e399441dSJames Smart if (!ctrl) { 3070e399441dSJames Smart ret = -ENOMEM; 3071e399441dSJames Smart goto out_fail; 3072e399441dSJames Smart } 3073e399441dSJames Smart 3074e399441dSJames Smart idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL); 3075e399441dSJames Smart if (idx < 0) { 3076e399441dSJames Smart ret = -ENOSPC; 30778c5c6605SJames Smart goto out_free_ctrl; 3078e399441dSJames Smart } 3079e399441dSJames Smart 3080e399441dSJames Smart ctrl->ctrl.opts = opts; 30814c984154SJames Smart ctrl->ctrl.nr_reconnects = 0; 308206f3d71eSJames Smart if (lport->dev) 3083103e515eSHannes Reinecke ctrl->ctrl.numa_node = dev_to_node(lport->dev); 308406f3d71eSJames Smart else 308506f3d71eSJames Smart ctrl->ctrl.numa_node = NUMA_NO_NODE; 3086e399441dSJames Smart INIT_LIST_HEAD(&ctrl->ctrl_list); 3087e399441dSJames Smart ctrl->lport = lport; 3088e399441dSJames Smart ctrl->rport = rport; 3089e399441dSJames Smart ctrl->dev = lport->dev; 3090e399441dSJames Smart ctrl->cnum = idx; 30914c984154SJames Smart ctrl->ioq_live = false; 3092158bfb88SJames Smart ctrl->assoc_active = false; 30934cff280aSJames Smart atomic_set(&ctrl->err_work_active, 0); 30948a82dbf1SJames Smart init_waitqueue_head(&ctrl->ioabort_wait); 3095e399441dSJames Smart 3096e399441dSJames Smart get_device(ctrl->dev); 3097e399441dSJames Smart kref_init(&ctrl->ref); 3098e399441dSJames Smart 3099d86c4d8eSChristoph Hellwig INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work); 310061bff8efSJames Smart INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work); 31014cff280aSJames Smart INIT_WORK(&ctrl->err_work, nvme_fc_connect_err_work); 3102e399441dSJames Smart spin_lock_init(&ctrl->lock); 3103e399441dSJames Smart 3104e399441dSJames Smart /* io queue count */ 3105d858e5f0SSagi Grimberg ctrl->ctrl.queue_count = min_t(unsigned int, 3106e399441dSJames Smart opts->nr_io_queues, 3107e399441dSJames Smart lport->ops->max_hw_queues); 3108d858e5f0SSagi Grimberg ctrl->ctrl.queue_count++; /* +1 for admin queue */ 3109e399441dSJames Smart 3110e399441dSJames Smart ctrl->ctrl.sqsize = opts->queue_size - 1; 3111e399441dSJames Smart ctrl->ctrl.kato = opts->kato; 31124c984154SJames Smart ctrl->ctrl.cntlid = 0xffff; 3113e399441dSJames Smart 3114e399441dSJames Smart ret = -ENOMEM; 3115d858e5f0SSagi Grimberg ctrl->queues = kcalloc(ctrl->ctrl.queue_count, 3116d858e5f0SSagi Grimberg sizeof(struct nvme_fc_queue), GFP_KERNEL); 3117e399441dSJames Smart if (!ctrl->queues) 311861bff8efSJames Smart goto out_free_ida; 3119e399441dSJames Smart 31203e493c00SJames Smart nvme_fc_init_queue(ctrl, 0); 31213e493c00SJames Smart 312261bff8efSJames Smart memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); 312361bff8efSJames Smart ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops; 312438dabe21SKeith Busch ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH; 312561bff8efSJames Smart ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */ 3126103e515eSHannes Reinecke ctrl->admin_tag_set.numa_node = ctrl->ctrl.numa_node; 3127d3d0bc78SBart Van Assche ctrl->admin_tag_set.cmd_size = 3128d3d0bc78SBart Van Assche struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv, 3129d3d0bc78SBart Van Assche ctrl->lport->ops->fcprqst_priv_sz); 313061bff8efSJames Smart ctrl->admin_tag_set.driver_data = ctrl; 313161bff8efSJames Smart ctrl->admin_tag_set.nr_hw_queues = 1; 313261bff8efSJames Smart ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; 31335a22e2bfSIsrael Rukshin ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED; 313461bff8efSJames Smart 313561bff8efSJames Smart ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); 3136e399441dSJames Smart if (ret) 313761bff8efSJames Smart goto out_free_queues; 313834b6c231SSagi Grimberg ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set; 3139e399441dSJames Smart 3140e7832cb4SSagi Grimberg ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set); 3141e7832cb4SSagi Grimberg if (IS_ERR(ctrl->ctrl.fabrics_q)) { 3142e7832cb4SSagi Grimberg ret = PTR_ERR(ctrl->ctrl.fabrics_q); 3143e7832cb4SSagi Grimberg goto out_free_admin_tag_set; 3144e7832cb4SSagi Grimberg } 3145e7832cb4SSagi Grimberg 314661bff8efSJames Smart ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); 314761bff8efSJames Smart if (IS_ERR(ctrl->ctrl.admin_q)) { 314861bff8efSJames Smart ret = PTR_ERR(ctrl->ctrl.admin_q); 3149e7832cb4SSagi Grimberg goto out_cleanup_fabrics_q; 3150e399441dSJames Smart } 3151e399441dSJames Smart 315261bff8efSJames Smart /* 315361bff8efSJames Smart * Would have been nice to init io queues tag set as well. 315461bff8efSJames Smart * However, we require interaction from the controller 315561bff8efSJames Smart * for max io queue count before we can do so. 315661bff8efSJames Smart * Defer this to the connect path. 315761bff8efSJames Smart */ 3158e399441dSJames Smart 315961bff8efSJames Smart ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0); 3160e399441dSJames Smart if (ret) 316161bff8efSJames Smart goto out_cleanup_admin_q; 3162e399441dSJames Smart 316361bff8efSJames Smart /* at this point, teardown path changes to ref counting on nvme ctrl */ 3164e399441dSJames Smart 3165e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags); 3166e399441dSJames Smart list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list); 3167e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 3168e399441dSJames Smart 31694c984154SJames Smart if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING) || 31704c984154SJames Smart !nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { 31714c984154SJames Smart dev_err(ctrl->ctrl.device, 31724c984154SJames Smart "NVME-FC{%d}: failed to init ctrl state\n", ctrl->cnum); 31734c984154SJames Smart goto fail_ctrl; 317417c4dc6eSJames Smart } 317517c4dc6eSJames Smart 31764c984154SJames Smart if (!queue_delayed_work(nvme_wq, &ctrl->connect_work, 0)) { 31774c984154SJames Smart dev_err(ctrl->ctrl.device, 31784c984154SJames Smart "NVME-FC{%d}: failed to schedule initial connect\n", 31794c984154SJames Smart ctrl->cnum); 31804c984154SJames Smart goto fail_ctrl; 31814c984154SJames Smart } 31824c984154SJames Smart 31834c984154SJames Smart flush_delayed_work(&ctrl->connect_work); 31844c984154SJames Smart 31854c984154SJames Smart dev_info(ctrl->ctrl.device, 31864c984154SJames Smart "NVME-FC{%d}: new ctrl: NQN \"%s\"\n", 31874c984154SJames Smart ctrl->cnum, ctrl->ctrl.opts->subsysnqn); 31884c984154SJames Smart 31894c984154SJames Smart return &ctrl->ctrl; 31904c984154SJames Smart 31914c984154SJames Smart fail_ctrl: 3192cf25809bSJames Smart nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING); 3193cf25809bSJames Smart cancel_work_sync(&ctrl->ctrl.reset_work); 31944cff280aSJames Smart cancel_work_sync(&ctrl->err_work); 3195cf25809bSJames Smart cancel_delayed_work_sync(&ctrl->connect_work); 3196cf25809bSJames Smart 3197de41447aSEwan D. Milne ctrl->ctrl.opts = NULL; 319817c4dc6eSJames Smart 319961bff8efSJames Smart /* initiate nvme ctrl ref counting teardown */ 320061bff8efSJames Smart nvme_uninit_ctrl(&ctrl->ctrl); 320161bff8efSJames Smart 32020b5a7669SJames Smart /* Remove core ctrl ref. */ 32030b5a7669SJames Smart nvme_put_ctrl(&ctrl->ctrl); 32040b5a7669SJames Smart 320561bff8efSJames Smart /* as we're past the point where we transition to the ref 320661bff8efSJames Smart * counting teardown path, if we return a bad pointer here, 320761bff8efSJames Smart * the calling routine, thinking it's prior to the 320861bff8efSJames Smart * transition, will do an rport put. Since the teardown 320961bff8efSJames Smart * path also does a rport put, we do an extra get here to 321061bff8efSJames Smart * so proper order/teardown happens. 321161bff8efSJames Smart */ 321261bff8efSJames Smart nvme_fc_rport_get(rport); 321361bff8efSJames Smart 32144c984154SJames Smart return ERR_PTR(-EIO); 3215e399441dSJames Smart 321661bff8efSJames Smart out_cleanup_admin_q: 321761bff8efSJames Smart blk_cleanup_queue(ctrl->ctrl.admin_q); 3218e7832cb4SSagi Grimberg out_cleanup_fabrics_q: 3219e7832cb4SSagi Grimberg blk_cleanup_queue(ctrl->ctrl.fabrics_q); 322061bff8efSJames Smart out_free_admin_tag_set: 322161bff8efSJames Smart blk_mq_free_tag_set(&ctrl->admin_tag_set); 322261bff8efSJames Smart out_free_queues: 322361bff8efSJames Smart kfree(ctrl->queues); 3224e399441dSJames Smart out_free_ida: 322561bff8efSJames Smart put_device(ctrl->dev); 3226e399441dSJames Smart ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum); 3227e399441dSJames Smart out_free_ctrl: 3228e399441dSJames Smart kfree(ctrl); 3229e399441dSJames Smart out_fail: 3230e399441dSJames Smart /* exit via here doesn't follow ctlr ref points */ 3231e399441dSJames Smart return ERR_PTR(ret); 3232e399441dSJames Smart } 3233e399441dSJames Smart 3234e399441dSJames Smart 3235e399441dSJames Smart struct nvmet_fc_traddr { 3236e399441dSJames Smart u64 nn; 3237e399441dSJames Smart u64 pn; 3238e399441dSJames Smart }; 3239e399441dSJames Smart 3240e399441dSJames Smart static int 32419c5358e1SJames Smart __nvme_fc_parse_u64(substring_t *sstr, u64 *val) 3242e399441dSJames Smart { 3243e399441dSJames Smart u64 token64; 3244e399441dSJames Smart 32459c5358e1SJames Smart if (match_u64(sstr, &token64)) 32469c5358e1SJames Smart return -EINVAL; 32479c5358e1SJames Smart *val = token64; 3248e399441dSJames Smart 32499c5358e1SJames Smart return 0; 3250e399441dSJames Smart } 3251e399441dSJames Smart 32529c5358e1SJames Smart /* 32539c5358e1SJames Smart * This routine validates and extracts the WWN's from the TRADDR string. 32549c5358e1SJames Smart * As kernel parsers need the 0x to determine number base, universally 32559c5358e1SJames Smart * build string to parse with 0x prefix before parsing name strings. 32569c5358e1SJames Smart */ 32579c5358e1SJames Smart static int 32589c5358e1SJames Smart nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen) 32599c5358e1SJames Smart { 32609c5358e1SJames Smart char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1]; 32619c5358e1SJames Smart substring_t wwn = { name, &name[sizeof(name)-1] }; 32629c5358e1SJames Smart int nnoffset, pnoffset; 32639c5358e1SJames Smart 3264d4e4230cSMilan P. Gandhi /* validate if string is one of the 2 allowed formats */ 32659c5358e1SJames Smart if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH && 32669c5358e1SJames Smart !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) && 32679c5358e1SJames Smart !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET], 32689c5358e1SJames Smart "pn-0x", NVME_FC_TRADDR_OXNNLEN)) { 32699c5358e1SJames Smart nnoffset = NVME_FC_TRADDR_OXNNLEN; 32709c5358e1SJames Smart pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET + 32719c5358e1SJames Smart NVME_FC_TRADDR_OXNNLEN; 32729c5358e1SJames Smart } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH && 32739c5358e1SJames Smart !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) && 32749c5358e1SJames Smart !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET], 32759c5358e1SJames Smart "pn-", NVME_FC_TRADDR_NNLEN))) { 32769c5358e1SJames Smart nnoffset = NVME_FC_TRADDR_NNLEN; 32779c5358e1SJames Smart pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN; 32789c5358e1SJames Smart } else 32799c5358e1SJames Smart goto out_einval; 32809c5358e1SJames Smart 32819c5358e1SJames Smart name[0] = '0'; 32829c5358e1SJames Smart name[1] = 'x'; 32839c5358e1SJames Smart name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0; 32849c5358e1SJames Smart 32859c5358e1SJames Smart memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN); 32869c5358e1SJames Smart if (__nvme_fc_parse_u64(&wwn, &traddr->nn)) 32879c5358e1SJames Smart goto out_einval; 32889c5358e1SJames Smart 32899c5358e1SJames Smart memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN); 32909c5358e1SJames Smart if (__nvme_fc_parse_u64(&wwn, &traddr->pn)) 32919c5358e1SJames Smart goto out_einval; 32929c5358e1SJames Smart 32939c5358e1SJames Smart return 0; 32949c5358e1SJames Smart 32959c5358e1SJames Smart out_einval: 32969c5358e1SJames Smart pr_warn("%s: bad traddr string\n", __func__); 32979c5358e1SJames Smart return -EINVAL; 3298e399441dSJames Smart } 3299e399441dSJames Smart 3300e399441dSJames Smart static struct nvme_ctrl * 3301e399441dSJames Smart nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts) 3302e399441dSJames Smart { 3303e399441dSJames Smart struct nvme_fc_lport *lport; 3304e399441dSJames Smart struct nvme_fc_rport *rport; 330561bff8efSJames Smart struct nvme_ctrl *ctrl; 3306e399441dSJames Smart struct nvmet_fc_traddr laddr = { 0L, 0L }; 3307e399441dSJames Smart struct nvmet_fc_traddr raddr = { 0L, 0L }; 3308e399441dSJames Smart unsigned long flags; 3309e399441dSJames Smart int ret; 3310e399441dSJames Smart 33119c5358e1SJames Smart ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE); 3312e399441dSJames Smart if (ret || !raddr.nn || !raddr.pn) 3313e399441dSJames Smart return ERR_PTR(-EINVAL); 3314e399441dSJames Smart 33159c5358e1SJames Smart ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE); 3316e399441dSJames Smart if (ret || !laddr.nn || !laddr.pn) 3317e399441dSJames Smart return ERR_PTR(-EINVAL); 3318e399441dSJames Smart 3319e399441dSJames Smart /* find the host and remote ports to connect together */ 3320e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 3321e399441dSJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { 3322e399441dSJames Smart if (lport->localport.node_name != laddr.nn || 3323e399441dSJames Smart lport->localport.port_name != laddr.pn) 3324e399441dSJames Smart continue; 3325e399441dSJames Smart 3326e399441dSJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) { 3327e399441dSJames Smart if (rport->remoteport.node_name != raddr.nn || 3328e399441dSJames Smart rport->remoteport.port_name != raddr.pn) 3329e399441dSJames Smart continue; 3330e399441dSJames Smart 3331e399441dSJames Smart /* if fail to get reference fall through. Will error */ 3332e399441dSJames Smart if (!nvme_fc_rport_get(rport)) 3333e399441dSJames Smart break; 3334e399441dSJames Smart 3335e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 3336e399441dSJames Smart 333761bff8efSJames Smart ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport); 333861bff8efSJames Smart if (IS_ERR(ctrl)) 333961bff8efSJames Smart nvme_fc_rport_put(rport); 334061bff8efSJames Smart return ctrl; 3341e399441dSJames Smart } 3342e399441dSJames Smart } 3343e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 3344e399441dSJames Smart 33454fb135adSJohannes Thumshirn pr_warn("%s: %s - %s combination not found\n", 33464fb135adSJohannes Thumshirn __func__, opts->traddr, opts->host_traddr); 3347e399441dSJames Smart return ERR_PTR(-ENOENT); 3348e399441dSJames Smart } 3349e399441dSJames Smart 3350e399441dSJames Smart 3351e399441dSJames Smart static struct nvmf_transport_ops nvme_fc_transport = { 3352e399441dSJames Smart .name = "fc", 33530de5cd36SRoy Shterman .module = THIS_MODULE, 3354e399441dSJames Smart .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR, 33555bbecdbcSJames Smart .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO, 3356e399441dSJames Smart .create_ctrl = nvme_fc_create_ctrl, 3357e399441dSJames Smart }; 3358e399441dSJames Smart 335997faec53SJames Smart /* Arbitrary successive failures max. With lots of subsystems could be high */ 336097faec53SJames Smart #define DISCOVERY_MAX_FAIL 20 336197faec53SJames Smart 336297faec53SJames Smart static ssize_t nvme_fc_nvme_discovery_store(struct device *dev, 336397faec53SJames Smart struct device_attribute *attr, const char *buf, size_t count) 336497faec53SJames Smart { 336597faec53SJames Smart unsigned long flags; 336697faec53SJames Smart LIST_HEAD(local_disc_list); 336797faec53SJames Smart struct nvme_fc_lport *lport; 336897faec53SJames Smart struct nvme_fc_rport *rport; 336997faec53SJames Smart int failcnt = 0; 337097faec53SJames Smart 337197faec53SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 337297faec53SJames Smart restart: 337397faec53SJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { 337497faec53SJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) { 337597faec53SJames Smart if (!nvme_fc_lport_get(lport)) 337697faec53SJames Smart continue; 337797faec53SJames Smart if (!nvme_fc_rport_get(rport)) { 337897faec53SJames Smart /* 337997faec53SJames Smart * This is a temporary condition. Upon restart 338097faec53SJames Smart * this rport will be gone from the list. 338197faec53SJames Smart * 338297faec53SJames Smart * Revert the lport put and retry. Anything 338397faec53SJames Smart * added to the list already will be skipped (as 338497faec53SJames Smart * they are no longer list_empty). Loops should 338597faec53SJames Smart * resume at rports that were not yet seen. 338697faec53SJames Smart */ 338797faec53SJames Smart nvme_fc_lport_put(lport); 338897faec53SJames Smart 338997faec53SJames Smart if (failcnt++ < DISCOVERY_MAX_FAIL) 339097faec53SJames Smart goto restart; 339197faec53SJames Smart 339297faec53SJames Smart pr_err("nvme_discovery: too many reference " 339397faec53SJames Smart "failures\n"); 339497faec53SJames Smart goto process_local_list; 339597faec53SJames Smart } 339697faec53SJames Smart if (list_empty(&rport->disc_list)) 339797faec53SJames Smart list_add_tail(&rport->disc_list, 339897faec53SJames Smart &local_disc_list); 339997faec53SJames Smart } 340097faec53SJames Smart } 340197faec53SJames Smart 340297faec53SJames Smart process_local_list: 340397faec53SJames Smart while (!list_empty(&local_disc_list)) { 340497faec53SJames Smart rport = list_first_entry(&local_disc_list, 340597faec53SJames Smart struct nvme_fc_rport, disc_list); 340697faec53SJames Smart list_del_init(&rport->disc_list); 340797faec53SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 340897faec53SJames Smart 340997faec53SJames Smart lport = rport->lport; 341097faec53SJames Smart /* signal discovery. Won't hurt if it repeats */ 341197faec53SJames Smart nvme_fc_signal_discovery_scan(lport, rport); 341297faec53SJames Smart nvme_fc_rport_put(rport); 341397faec53SJames Smart nvme_fc_lport_put(lport); 341497faec53SJames Smart 341597faec53SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 341697faec53SJames Smart } 341797faec53SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 341897faec53SJames Smart 341997faec53SJames Smart return count; 342097faec53SJames Smart } 342197faec53SJames Smart static DEVICE_ATTR(nvme_discovery, 0200, NULL, nvme_fc_nvme_discovery_store); 342297faec53SJames Smart 342397faec53SJames Smart static struct attribute *nvme_fc_attrs[] = { 342497faec53SJames Smart &dev_attr_nvme_discovery.attr, 342597faec53SJames Smart NULL 342697faec53SJames Smart }; 342797faec53SJames Smart 342897faec53SJames Smart static struct attribute_group nvme_fc_attr_group = { 342997faec53SJames Smart .attrs = nvme_fc_attrs, 343097faec53SJames Smart }; 343197faec53SJames Smart 343297faec53SJames Smart static const struct attribute_group *nvme_fc_attr_groups[] = { 343397faec53SJames Smart &nvme_fc_attr_group, 343497faec53SJames Smart NULL 343597faec53SJames Smart }; 343697faec53SJames Smart 343797faec53SJames Smart static struct class fc_class = { 343897faec53SJames Smart .name = "fc", 343997faec53SJames Smart .dev_groups = nvme_fc_attr_groups, 344097faec53SJames Smart .owner = THIS_MODULE, 344197faec53SJames Smart }; 344297faec53SJames Smart 3443e399441dSJames Smart static int __init nvme_fc_init_module(void) 3444e399441dSJames Smart { 34455f568556SJames Smart int ret; 34465f568556SJames Smart 34478730c1ddSHannes Reinecke nvme_fc_wq = alloc_workqueue("nvme_fc_wq", WQ_MEM_RECLAIM, 0); 34488730c1ddSHannes Reinecke if (!nvme_fc_wq) 34498730c1ddSHannes Reinecke return -ENOMEM; 34508730c1ddSHannes Reinecke 34515f568556SJames Smart /* 34525f568556SJames Smart * NOTE: 34535f568556SJames Smart * It is expected that in the future the kernel will combine 34545f568556SJames Smart * the FC-isms that are currently under scsi and now being 34555f568556SJames Smart * added to by NVME into a new standalone FC class. The SCSI 34565f568556SJames Smart * and NVME protocols and their devices would be under this 34575f568556SJames Smart * new FC class. 34585f568556SJames Smart * 34595f568556SJames Smart * As we need something to post FC-specific udev events to, 34605f568556SJames Smart * specifically for nvme probe events, start by creating the 34615f568556SJames Smart * new device class. When the new standalone FC class is 34625f568556SJames Smart * put in place, this code will move to a more generic 34635f568556SJames Smart * location for the class. 34645f568556SJames Smart */ 346597faec53SJames Smart ret = class_register(&fc_class); 346697faec53SJames Smart if (ret) { 34675f568556SJames Smart pr_err("couldn't register class fc\n"); 34688730c1ddSHannes Reinecke goto out_destroy_wq; 34695f568556SJames Smart } 34705f568556SJames Smart 34715f568556SJames Smart /* 34725f568556SJames Smart * Create a device for the FC-centric udev events 34735f568556SJames Smart */ 347497faec53SJames Smart fc_udev_device = device_create(&fc_class, NULL, MKDEV(0, 0), NULL, 34755f568556SJames Smart "fc_udev_device"); 34765f568556SJames Smart if (IS_ERR(fc_udev_device)) { 34775f568556SJames Smart pr_err("couldn't create fc_udev device!\n"); 34785f568556SJames Smart ret = PTR_ERR(fc_udev_device); 34795f568556SJames Smart goto out_destroy_class; 34805f568556SJames Smart } 34815f568556SJames Smart 34825f568556SJames Smart ret = nvmf_register_transport(&nvme_fc_transport); 34835f568556SJames Smart if (ret) 34845f568556SJames Smart goto out_destroy_device; 34855f568556SJames Smart 34865f568556SJames Smart return 0; 34875f568556SJames Smart 34885f568556SJames Smart out_destroy_device: 348997faec53SJames Smart device_destroy(&fc_class, MKDEV(0, 0)); 34905f568556SJames Smart out_destroy_class: 349197faec53SJames Smart class_unregister(&fc_class); 34928730c1ddSHannes Reinecke out_destroy_wq: 34938730c1ddSHannes Reinecke destroy_workqueue(nvme_fc_wq); 34948730c1ddSHannes Reinecke 34955f568556SJames Smart return ret; 3496e399441dSJames Smart } 3497e399441dSJames Smart 34984c73cbdfSJames Smart static void 34994c73cbdfSJames Smart nvme_fc_delete_controllers(struct nvme_fc_rport *rport) 35004c73cbdfSJames Smart { 35014c73cbdfSJames Smart struct nvme_fc_ctrl *ctrl; 35024c73cbdfSJames Smart 35034c73cbdfSJames Smart spin_lock(&rport->lock); 35044c73cbdfSJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) { 35054c73cbdfSJames Smart dev_warn(ctrl->ctrl.device, 35064c73cbdfSJames Smart "NVME-FC{%d}: transport unloading: deleting ctrl\n", 35074c73cbdfSJames Smart ctrl->cnum); 35084c73cbdfSJames Smart nvme_delete_ctrl(&ctrl->ctrl); 35094c73cbdfSJames Smart } 35104c73cbdfSJames Smart spin_unlock(&rport->lock); 35114c73cbdfSJames Smart } 35124c73cbdfSJames Smart 35134c73cbdfSJames Smart static void 35144c73cbdfSJames Smart nvme_fc_cleanup_for_unload(void) 35154c73cbdfSJames Smart { 35164c73cbdfSJames Smart struct nvme_fc_lport *lport; 35174c73cbdfSJames Smart struct nvme_fc_rport *rport; 35184c73cbdfSJames Smart 35194c73cbdfSJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { 35204c73cbdfSJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) { 35214c73cbdfSJames Smart nvme_fc_delete_controllers(rport); 35224c73cbdfSJames Smart } 35234c73cbdfSJames Smart } 35244c73cbdfSJames Smart } 35254c73cbdfSJames Smart 3526e399441dSJames Smart static void __exit nvme_fc_exit_module(void) 3527e399441dSJames Smart { 35284c73cbdfSJames Smart unsigned long flags; 35294c73cbdfSJames Smart bool need_cleanup = false; 35304c73cbdfSJames Smart 35314c73cbdfSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 35324c73cbdfSJames Smart nvme_fc_waiting_to_unload = true; 35334c73cbdfSJames Smart if (!list_empty(&nvme_fc_lport_list)) { 35344c73cbdfSJames Smart need_cleanup = true; 35354c73cbdfSJames Smart nvme_fc_cleanup_for_unload(); 35364c73cbdfSJames Smart } 35374c73cbdfSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 35384c73cbdfSJames Smart if (need_cleanup) { 35394c73cbdfSJames Smart pr_info("%s: waiting for ctlr deletes\n", __func__); 35404c73cbdfSJames Smart wait_for_completion(&nvme_fc_unload_proceed); 35414c73cbdfSJames Smart pr_info("%s: ctrl deletes complete\n", __func__); 35424c73cbdfSJames Smart } 3543e399441dSJames Smart 3544e399441dSJames Smart nvmf_unregister_transport(&nvme_fc_transport); 3545e399441dSJames Smart 3546e399441dSJames Smart ida_destroy(&nvme_fc_local_port_cnt); 3547e399441dSJames Smart ida_destroy(&nvme_fc_ctrl_cnt); 35485f568556SJames Smart 354997faec53SJames Smart device_destroy(&fc_class, MKDEV(0, 0)); 355097faec53SJames Smart class_unregister(&fc_class); 35518730c1ddSHannes Reinecke destroy_workqueue(nvme_fc_wq); 3552e399441dSJames Smart } 3553e399441dSJames Smart 3554e399441dSJames Smart module_init(nvme_fc_init_module); 3555e399441dSJames Smart module_exit(nvme_fc_exit_module); 3556e399441dSJames Smart 3557e399441dSJames Smart MODULE_LICENSE("GPL v2"); 3558