1e399441dSJames Smart /* 2e399441dSJames Smart * Copyright (c) 2016 Avago Technologies. All rights reserved. 3e399441dSJames Smart * 4e399441dSJames Smart * This program is free software; you can redistribute it and/or modify 5e399441dSJames Smart * it under the terms of version 2 of the GNU General Public License as 6e399441dSJames Smart * published by the Free Software Foundation. 7e399441dSJames Smart * 8e399441dSJames Smart * This program is distributed in the hope that it will be useful. 9e399441dSJames Smart * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 10e399441dSJames Smart * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 11e399441dSJames Smart * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO 12e399441dSJames Smart * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID. 13e399441dSJames Smart * See the GNU General Public License for more details, a copy of which 14e399441dSJames Smart * can be found in the file COPYING included with this package 15e399441dSJames Smart * 16e399441dSJames Smart */ 17e399441dSJames Smart #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 18e399441dSJames Smart #include <linux/module.h> 19e399441dSJames Smart #include <linux/parser.h> 20e399441dSJames Smart #include <uapi/scsi/fc/fc_fs.h> 21e399441dSJames Smart #include <uapi/scsi/fc/fc_els.h> 2261bff8efSJames Smart #include <linux/delay.h> 23e399441dSJames Smart 24e399441dSJames Smart #include "nvme.h" 25e399441dSJames Smart #include "fabrics.h" 26e399441dSJames Smart #include <linux/nvme-fc-driver.h> 27e399441dSJames Smart #include <linux/nvme-fc.h> 28e399441dSJames Smart 29e399441dSJames Smart 30e399441dSJames Smart /* *************************** Data Structures/Defines ****************** */ 31e399441dSJames Smart 32e399441dSJames Smart 33e399441dSJames Smart enum nvme_fc_queue_flags { 3426c0a26dSJens Axboe NVME_FC_Q_CONNECTED = 0, 3526c0a26dSJens Axboe NVME_FC_Q_LIVE, 36e399441dSJames Smart }; 37e399441dSJames Smart 38e399441dSJames Smart #define NVMEFC_QUEUE_DELAY 3 /* ms units */ 39e399441dSJames Smart 40ac7fe82bSJames Smart #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */ 41ac7fe82bSJames Smart 42e399441dSJames Smart struct nvme_fc_queue { 43e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 44e399441dSJames Smart struct device *dev; 45e399441dSJames Smart struct blk_mq_hw_ctx *hctx; 46e399441dSJames Smart void *lldd_handle; 47e399441dSJames Smart size_t cmnd_capsule_len; 48e399441dSJames Smart u32 qnum; 49e399441dSJames Smart u32 rqcnt; 50e399441dSJames Smart u32 seqno; 51e399441dSJames Smart 52e399441dSJames Smart u64 connection_id; 53e399441dSJames Smart atomic_t csn; 54e399441dSJames Smart 55e399441dSJames Smart unsigned long flags; 56e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 57e399441dSJames Smart 588d64daf7SJames Smart enum nvme_fcop_flags { 598d64daf7SJames Smart FCOP_FLAGS_TERMIO = (1 << 0), 608d64daf7SJames Smart FCOP_FLAGS_RELEASED = (1 << 1), 618d64daf7SJames Smart FCOP_FLAGS_COMPLETE = (1 << 2), 6278a7ac26SJames Smart FCOP_FLAGS_AEN = (1 << 3), 638d64daf7SJames Smart }; 648d64daf7SJames Smart 65e399441dSJames Smart struct nvmefc_ls_req_op { 66e399441dSJames Smart struct nvmefc_ls_req ls_req; 67e399441dSJames Smart 68c913a8b0SJames Smart struct nvme_fc_rport *rport; 69e399441dSJames Smart struct nvme_fc_queue *queue; 70e399441dSJames Smart struct request *rq; 718d64daf7SJames Smart u32 flags; 72e399441dSJames Smart 73e399441dSJames Smart int ls_error; 74e399441dSJames Smart struct completion ls_done; 75c913a8b0SJames Smart struct list_head lsreq_list; /* rport->ls_req_list */ 76e399441dSJames Smart bool req_queued; 77e399441dSJames Smart }; 78e399441dSJames Smart 79e399441dSJames Smart enum nvme_fcpop_state { 80e399441dSJames Smart FCPOP_STATE_UNINIT = 0, 81e399441dSJames Smart FCPOP_STATE_IDLE = 1, 82e399441dSJames Smart FCPOP_STATE_ACTIVE = 2, 83e399441dSJames Smart FCPOP_STATE_ABORTED = 3, 8478a7ac26SJames Smart FCPOP_STATE_COMPLETE = 4, 85e399441dSJames Smart }; 86e399441dSJames Smart 87e399441dSJames Smart struct nvme_fc_fcp_op { 88e399441dSJames Smart struct nvme_request nreq; /* 89e399441dSJames Smart * nvme/host/core.c 90e399441dSJames Smart * requires this to be 91e399441dSJames Smart * the 1st element in the 92e399441dSJames Smart * private structure 93e399441dSJames Smart * associated with the 94e399441dSJames Smart * request. 95e399441dSJames Smart */ 96e399441dSJames Smart struct nvmefc_fcp_req fcp_req; 97e399441dSJames Smart 98e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 99e399441dSJames Smart struct nvme_fc_queue *queue; 100e399441dSJames Smart struct request *rq; 101e399441dSJames Smart 102e399441dSJames Smart atomic_t state; 10378a7ac26SJames Smart u32 flags; 104e399441dSJames Smart u32 rqno; 105e399441dSJames Smart u32 nents; 106e399441dSJames Smart 107e399441dSJames Smart struct nvme_fc_cmd_iu cmd_iu; 108e399441dSJames Smart struct nvme_fc_ersp_iu rsp_iu; 109e399441dSJames Smart }; 110e399441dSJames Smart 111e399441dSJames Smart struct nvme_fc_lport { 112e399441dSJames Smart struct nvme_fc_local_port localport; 113e399441dSJames Smart 114e399441dSJames Smart struct ida endp_cnt; 115e399441dSJames Smart struct list_head port_list; /* nvme_fc_port_list */ 116e399441dSJames Smart struct list_head endp_list; 117e399441dSJames Smart struct device *dev; /* physical device for dma */ 118e399441dSJames Smart struct nvme_fc_port_template *ops; 119e399441dSJames Smart struct kref ref; 120158bfb88SJames Smart atomic_t act_rport_cnt; 121e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 122e399441dSJames Smart 123e399441dSJames Smart struct nvme_fc_rport { 124e399441dSJames Smart struct nvme_fc_remote_port remoteport; 125e399441dSJames Smart 126e399441dSJames Smart struct list_head endp_list; /* for lport->endp_list */ 127e399441dSJames Smart struct list_head ctrl_list; 128c913a8b0SJames Smart struct list_head ls_req_list; 129c913a8b0SJames Smart struct device *dev; /* physical device for dma */ 130c913a8b0SJames Smart struct nvme_fc_lport *lport; 131e399441dSJames Smart spinlock_t lock; 132e399441dSJames Smart struct kref ref; 133158bfb88SJames Smart atomic_t act_ctrl_cnt; 1342b632970SJames Smart unsigned long dev_loss_end; 135e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 136e399441dSJames Smart 13761bff8efSJames Smart enum nvme_fcctrl_flags { 13861bff8efSJames Smart FCCTRL_TERMIO = (1 << 0), 139e399441dSJames Smart }; 140e399441dSJames Smart 141e399441dSJames Smart struct nvme_fc_ctrl { 142e399441dSJames Smart spinlock_t lock; 143e399441dSJames Smart struct nvme_fc_queue *queues; 144e399441dSJames Smart struct device *dev; 145e399441dSJames Smart struct nvme_fc_lport *lport; 146e399441dSJames Smart struct nvme_fc_rport *rport; 147e399441dSJames Smart u32 cnum; 148e399441dSJames Smart 149158bfb88SJames Smart bool assoc_active; 150e399441dSJames Smart u64 association_id; 151e399441dSJames Smart 152e399441dSJames Smart struct list_head ctrl_list; /* rport->ctrl_list */ 153e399441dSJames Smart 154e399441dSJames Smart struct blk_mq_tag_set admin_tag_set; 155e399441dSJames Smart struct blk_mq_tag_set tag_set; 156e399441dSJames Smart 15761bff8efSJames Smart struct delayed_work connect_work; 15861bff8efSJames Smart 159e399441dSJames Smart struct kref ref; 16061bff8efSJames Smart u32 flags; 16161bff8efSJames Smart u32 iocnt; 16236715cf4SJames Smart wait_queue_head_t ioabort_wait; 163e399441dSJames Smart 16438dabe21SKeith Busch struct nvme_fc_fcp_op aen_ops[NVME_NR_AEN_COMMANDS]; 165e399441dSJames Smart 166e399441dSJames Smart struct nvme_ctrl ctrl; 167e399441dSJames Smart }; 168e399441dSJames Smart 169e399441dSJames Smart static inline struct nvme_fc_ctrl * 170e399441dSJames Smart to_fc_ctrl(struct nvme_ctrl *ctrl) 171e399441dSJames Smart { 172e399441dSJames Smart return container_of(ctrl, struct nvme_fc_ctrl, ctrl); 173e399441dSJames Smart } 174e399441dSJames Smart 175e399441dSJames Smart static inline struct nvme_fc_lport * 176e399441dSJames Smart localport_to_lport(struct nvme_fc_local_port *portptr) 177e399441dSJames Smart { 178e399441dSJames Smart return container_of(portptr, struct nvme_fc_lport, localport); 179e399441dSJames Smart } 180e399441dSJames Smart 181e399441dSJames Smart static inline struct nvme_fc_rport * 182e399441dSJames Smart remoteport_to_rport(struct nvme_fc_remote_port *portptr) 183e399441dSJames Smart { 184e399441dSJames Smart return container_of(portptr, struct nvme_fc_rport, remoteport); 185e399441dSJames Smart } 186e399441dSJames Smart 187e399441dSJames Smart static inline struct nvmefc_ls_req_op * 188e399441dSJames Smart ls_req_to_lsop(struct nvmefc_ls_req *lsreq) 189e399441dSJames Smart { 190e399441dSJames Smart return container_of(lsreq, struct nvmefc_ls_req_op, ls_req); 191e399441dSJames Smart } 192e399441dSJames Smart 193e399441dSJames Smart static inline struct nvme_fc_fcp_op * 194e399441dSJames Smart fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq) 195e399441dSJames Smart { 196e399441dSJames Smart return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req); 197e399441dSJames Smart } 198e399441dSJames Smart 199e399441dSJames Smart 200e399441dSJames Smart 201e399441dSJames Smart /* *************************** Globals **************************** */ 202e399441dSJames Smart 203e399441dSJames Smart 204e399441dSJames Smart static DEFINE_SPINLOCK(nvme_fc_lock); 205e399441dSJames Smart 206e399441dSJames Smart static LIST_HEAD(nvme_fc_lport_list); 207e399441dSJames Smart static DEFINE_IDA(nvme_fc_local_port_cnt); 208e399441dSJames Smart static DEFINE_IDA(nvme_fc_ctrl_cnt); 209e399441dSJames Smart 210e399441dSJames Smart 211e399441dSJames Smart 2125f568556SJames Smart /* 2135f568556SJames Smart * These items are short-term. They will eventually be moved into 2145f568556SJames Smart * a generic FC class. See comments in module init. 2155f568556SJames Smart */ 2165f568556SJames Smart static struct class *fc_class; 2175f568556SJames Smart static struct device *fc_udev_device; 2185f568556SJames Smart 219e399441dSJames Smart 220e399441dSJames Smart /* *********************** FC-NVME Port Management ************************ */ 221e399441dSJames Smart 222e399441dSJames Smart static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *, 223e399441dSJames Smart struct nvme_fc_queue *, unsigned int); 224e399441dSJames Smart 2255533d424SJames Smart static void 2265533d424SJames Smart nvme_fc_free_lport(struct kref *ref) 2275533d424SJames Smart { 2285533d424SJames Smart struct nvme_fc_lport *lport = 2295533d424SJames Smart container_of(ref, struct nvme_fc_lport, ref); 2305533d424SJames Smart unsigned long flags; 2315533d424SJames Smart 2325533d424SJames Smart WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED); 2335533d424SJames Smart WARN_ON(!list_empty(&lport->endp_list)); 2345533d424SJames Smart 2355533d424SJames Smart /* remove from transport list */ 2365533d424SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 2375533d424SJames Smart list_del(&lport->port_list); 2385533d424SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 2395533d424SJames Smart 2405533d424SJames Smart ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num); 2415533d424SJames Smart ida_destroy(&lport->endp_cnt); 2425533d424SJames Smart 2435533d424SJames Smart put_device(lport->dev); 2445533d424SJames Smart 2455533d424SJames Smart kfree(lport); 2465533d424SJames Smart } 2475533d424SJames Smart 2485533d424SJames Smart static void 2495533d424SJames Smart nvme_fc_lport_put(struct nvme_fc_lport *lport) 2505533d424SJames Smart { 2515533d424SJames Smart kref_put(&lport->ref, nvme_fc_free_lport); 2525533d424SJames Smart } 2535533d424SJames Smart 2545533d424SJames Smart static int 2555533d424SJames Smart nvme_fc_lport_get(struct nvme_fc_lport *lport) 2565533d424SJames Smart { 2575533d424SJames Smart return kref_get_unless_zero(&lport->ref); 2585533d424SJames Smart } 2595533d424SJames Smart 2605533d424SJames Smart 2615533d424SJames Smart static struct nvme_fc_lport * 262c5760f30SJames Smart nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo, 263c5760f30SJames Smart struct nvme_fc_port_template *ops, 264c5760f30SJames Smart struct device *dev) 2655533d424SJames Smart { 2665533d424SJames Smart struct nvme_fc_lport *lport; 2675533d424SJames Smart unsigned long flags; 2685533d424SJames Smart 2695533d424SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 2705533d424SJames Smart 2715533d424SJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { 2725533d424SJames Smart if (lport->localport.node_name != pinfo->node_name || 2735533d424SJames Smart lport->localport.port_name != pinfo->port_name) 2745533d424SJames Smart continue; 2755533d424SJames Smart 276c5760f30SJames Smart if (lport->dev != dev) { 277c5760f30SJames Smart lport = ERR_PTR(-EXDEV); 278c5760f30SJames Smart goto out_done; 279c5760f30SJames Smart } 280c5760f30SJames Smart 2815533d424SJames Smart if (lport->localport.port_state != FC_OBJSTATE_DELETED) { 2825533d424SJames Smart lport = ERR_PTR(-EEXIST); 2835533d424SJames Smart goto out_done; 2845533d424SJames Smart } 2855533d424SJames Smart 2865533d424SJames Smart if (!nvme_fc_lport_get(lport)) { 2875533d424SJames Smart /* 2885533d424SJames Smart * fails if ref cnt already 0. If so, 2895533d424SJames Smart * act as if lport already deleted 2905533d424SJames Smart */ 2915533d424SJames Smart lport = NULL; 2925533d424SJames Smart goto out_done; 2935533d424SJames Smart } 2945533d424SJames Smart 2955533d424SJames Smart /* resume the lport */ 2965533d424SJames Smart 297c5760f30SJames Smart lport->ops = ops; 2985533d424SJames Smart lport->localport.port_role = pinfo->port_role; 2995533d424SJames Smart lport->localport.port_id = pinfo->port_id; 3005533d424SJames Smart lport->localport.port_state = FC_OBJSTATE_ONLINE; 3015533d424SJames Smart 3025533d424SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 3035533d424SJames Smart 3045533d424SJames Smart return lport; 3055533d424SJames Smart } 3065533d424SJames Smart 3075533d424SJames Smart lport = NULL; 3085533d424SJames Smart 3095533d424SJames Smart out_done: 3105533d424SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 3115533d424SJames Smart 3125533d424SJames Smart return lport; 3135533d424SJames Smart } 314e399441dSJames Smart 315e399441dSJames Smart /** 316e399441dSJames Smart * nvme_fc_register_localport - transport entry point called by an 317e399441dSJames Smart * LLDD to register the existence of a NVME 318e399441dSJames Smart * host FC port. 319e399441dSJames Smart * @pinfo: pointer to information about the port to be registered 320e399441dSJames Smart * @template: LLDD entrypoints and operational parameters for the port 321e399441dSJames Smart * @dev: physical hardware device node port corresponds to. Will be 322e399441dSJames Smart * used for DMA mappings 323e399441dSJames Smart * @lport_p: pointer to a local port pointer. Upon success, the routine 324e399441dSJames Smart * will allocate a nvme_fc_local_port structure and place its 325e399441dSJames Smart * address in the local port pointer. Upon failure, local port 326e399441dSJames Smart * pointer will be set to 0. 327e399441dSJames Smart * 328e399441dSJames Smart * Returns: 329e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 330e399441dSJames Smart * (ex: -ENXIO) upon failure. 331e399441dSJames Smart */ 332e399441dSJames Smart int 333e399441dSJames Smart nvme_fc_register_localport(struct nvme_fc_port_info *pinfo, 334e399441dSJames Smart struct nvme_fc_port_template *template, 335e399441dSJames Smart struct device *dev, 336e399441dSJames Smart struct nvme_fc_local_port **portptr) 337e399441dSJames Smart { 338e399441dSJames Smart struct nvme_fc_lport *newrec; 339e399441dSJames Smart unsigned long flags; 340e399441dSJames Smart int ret, idx; 341e399441dSJames Smart 342e399441dSJames Smart if (!template->localport_delete || !template->remoteport_delete || 343e399441dSJames Smart !template->ls_req || !template->fcp_io || 344e399441dSJames Smart !template->ls_abort || !template->fcp_abort || 345e399441dSJames Smart !template->max_hw_queues || !template->max_sgl_segments || 346e399441dSJames Smart !template->max_dif_sgl_segments || !template->dma_boundary) { 347e399441dSJames Smart ret = -EINVAL; 348e399441dSJames Smart goto out_reghost_failed; 349e399441dSJames Smart } 350e399441dSJames Smart 3515533d424SJames Smart /* 3525533d424SJames Smart * look to see if there is already a localport that had been 3535533d424SJames Smart * deregistered and in the process of waiting for all the 3545533d424SJames Smart * references to fully be removed. If the references haven't 3555533d424SJames Smart * expired, we can simply re-enable the localport. Remoteports 3565533d424SJames Smart * and controller reconnections should resume naturally. 3575533d424SJames Smart */ 358c5760f30SJames Smart newrec = nvme_fc_attach_to_unreg_lport(pinfo, template, dev); 3595533d424SJames Smart 3605533d424SJames Smart /* found an lport, but something about its state is bad */ 3615533d424SJames Smart if (IS_ERR(newrec)) { 3625533d424SJames Smart ret = PTR_ERR(newrec); 3635533d424SJames Smart goto out_reghost_failed; 3645533d424SJames Smart 3655533d424SJames Smart /* found existing lport, which was resumed */ 3665533d424SJames Smart } else if (newrec) { 3675533d424SJames Smart *portptr = &newrec->localport; 3685533d424SJames Smart return 0; 3695533d424SJames Smart } 3705533d424SJames Smart 3715533d424SJames Smart /* nothing found - allocate a new localport struct */ 3725533d424SJames Smart 373e399441dSJames Smart newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz), 374e399441dSJames Smart GFP_KERNEL); 375e399441dSJames Smart if (!newrec) { 376e399441dSJames Smart ret = -ENOMEM; 377e399441dSJames Smart goto out_reghost_failed; 378e399441dSJames Smart } 379e399441dSJames Smart 380e399441dSJames Smart idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL); 381e399441dSJames Smart if (idx < 0) { 382e399441dSJames Smart ret = -ENOSPC; 383e399441dSJames Smart goto out_fail_kfree; 384e399441dSJames Smart } 385e399441dSJames Smart 386e399441dSJames Smart if (!get_device(dev) && dev) { 387e399441dSJames Smart ret = -ENODEV; 388e399441dSJames Smart goto out_ida_put; 389e399441dSJames Smart } 390e399441dSJames Smart 391e399441dSJames Smart INIT_LIST_HEAD(&newrec->port_list); 392e399441dSJames Smart INIT_LIST_HEAD(&newrec->endp_list); 393e399441dSJames Smart kref_init(&newrec->ref); 394158bfb88SJames Smart atomic_set(&newrec->act_rport_cnt, 0); 395e399441dSJames Smart newrec->ops = template; 396e399441dSJames Smart newrec->dev = dev; 397e399441dSJames Smart ida_init(&newrec->endp_cnt); 398e399441dSJames Smart newrec->localport.private = &newrec[1]; 399e399441dSJames Smart newrec->localport.node_name = pinfo->node_name; 400e399441dSJames Smart newrec->localport.port_name = pinfo->port_name; 401e399441dSJames Smart newrec->localport.port_role = pinfo->port_role; 402e399441dSJames Smart newrec->localport.port_id = pinfo->port_id; 403e399441dSJames Smart newrec->localport.port_state = FC_OBJSTATE_ONLINE; 404e399441dSJames Smart newrec->localport.port_num = idx; 405e399441dSJames Smart 406e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 407e399441dSJames Smart list_add_tail(&newrec->port_list, &nvme_fc_lport_list); 408e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 409e399441dSJames Smart 410e399441dSJames Smart if (dev) 411e399441dSJames Smart dma_set_seg_boundary(dev, template->dma_boundary); 412e399441dSJames Smart 413e399441dSJames Smart *portptr = &newrec->localport; 414e399441dSJames Smart return 0; 415e399441dSJames Smart 416e399441dSJames Smart out_ida_put: 417e399441dSJames Smart ida_simple_remove(&nvme_fc_local_port_cnt, idx); 418e399441dSJames Smart out_fail_kfree: 419e399441dSJames Smart kfree(newrec); 420e399441dSJames Smart out_reghost_failed: 421e399441dSJames Smart *portptr = NULL; 422e399441dSJames Smart 423e399441dSJames Smart return ret; 424e399441dSJames Smart } 425e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_localport); 426e399441dSJames Smart 427e399441dSJames Smart /** 428e399441dSJames Smart * nvme_fc_unregister_localport - transport entry point called by an 429e399441dSJames Smart * LLDD to deregister/remove a previously 430e399441dSJames Smart * registered a NVME host FC port. 431e399441dSJames Smart * @localport: pointer to the (registered) local port that is to be 432e399441dSJames Smart * deregistered. 433e399441dSJames Smart * 434e399441dSJames Smart * Returns: 435e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 436e399441dSJames Smart * (ex: -ENXIO) upon failure. 437e399441dSJames Smart */ 438e399441dSJames Smart int 439e399441dSJames Smart nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr) 440e399441dSJames Smart { 441e399441dSJames Smart struct nvme_fc_lport *lport = localport_to_lport(portptr); 442e399441dSJames Smart unsigned long flags; 443e399441dSJames Smart 444e399441dSJames Smart if (!portptr) 445e399441dSJames Smart return -EINVAL; 446e399441dSJames Smart 447e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 448e399441dSJames Smart 449e399441dSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) { 450e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 451e399441dSJames Smart return -EINVAL; 452e399441dSJames Smart } 453e399441dSJames Smart portptr->port_state = FC_OBJSTATE_DELETED; 454e399441dSJames Smart 455e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 456e399441dSJames Smart 457158bfb88SJames Smart if (atomic_read(&lport->act_rport_cnt) == 0) 458158bfb88SJames Smart lport->ops->localport_delete(&lport->localport); 459158bfb88SJames Smart 460e399441dSJames Smart nvme_fc_lport_put(lport); 461e399441dSJames Smart 462e399441dSJames Smart return 0; 463e399441dSJames Smart } 464e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport); 465e399441dSJames Smart 466eaefd5abSJames Smart /* 467eaefd5abSJames Smart * TRADDR strings, per FC-NVME are fixed format: 468eaefd5abSJames Smart * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters 469eaefd5abSJames Smart * udev event will only differ by prefix of what field is 470eaefd5abSJames Smart * being specified: 471eaefd5abSJames Smart * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters 472eaefd5abSJames Smart * 19 + 43 + null_fudge = 64 characters 473eaefd5abSJames Smart */ 474eaefd5abSJames Smart #define FCNVME_TRADDR_LENGTH 64 475eaefd5abSJames Smart 476eaefd5abSJames Smart static void 477eaefd5abSJames Smart nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport, 478eaefd5abSJames Smart struct nvme_fc_rport *rport) 479eaefd5abSJames Smart { 480eaefd5abSJames Smart char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/ 481eaefd5abSJames Smart char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/ 482eaefd5abSJames Smart char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL }; 483eaefd5abSJames Smart 484eaefd5abSJames Smart if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY)) 485eaefd5abSJames Smart return; 486eaefd5abSJames Smart 487eaefd5abSJames Smart snprintf(hostaddr, sizeof(hostaddr), 488eaefd5abSJames Smart "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx", 489eaefd5abSJames Smart lport->localport.node_name, lport->localport.port_name); 490eaefd5abSJames Smart snprintf(tgtaddr, sizeof(tgtaddr), 491eaefd5abSJames Smart "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx", 492eaefd5abSJames Smart rport->remoteport.node_name, rport->remoteport.port_name); 493eaefd5abSJames Smart kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp); 494eaefd5abSJames Smart } 495eaefd5abSJames Smart 496469d0ef0SJames Smart static void 497469d0ef0SJames Smart nvme_fc_free_rport(struct kref *ref) 498469d0ef0SJames Smart { 499469d0ef0SJames Smart struct nvme_fc_rport *rport = 500469d0ef0SJames Smart container_of(ref, struct nvme_fc_rport, ref); 501469d0ef0SJames Smart struct nvme_fc_lport *lport = 502469d0ef0SJames Smart localport_to_lport(rport->remoteport.localport); 503469d0ef0SJames Smart unsigned long flags; 504469d0ef0SJames Smart 505469d0ef0SJames Smart WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED); 506469d0ef0SJames Smart WARN_ON(!list_empty(&rport->ctrl_list)); 507469d0ef0SJames Smart 508469d0ef0SJames Smart /* remove from lport list */ 509469d0ef0SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 510469d0ef0SJames Smart list_del(&rport->endp_list); 511469d0ef0SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 512469d0ef0SJames Smart 513469d0ef0SJames Smart ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num); 514469d0ef0SJames Smart 515469d0ef0SJames Smart kfree(rport); 516469d0ef0SJames Smart 517469d0ef0SJames Smart nvme_fc_lport_put(lport); 518469d0ef0SJames Smart } 519469d0ef0SJames Smart 520469d0ef0SJames Smart static void 521469d0ef0SJames Smart nvme_fc_rport_put(struct nvme_fc_rport *rport) 522469d0ef0SJames Smart { 523469d0ef0SJames Smart kref_put(&rport->ref, nvme_fc_free_rport); 524469d0ef0SJames Smart } 525469d0ef0SJames Smart 526469d0ef0SJames Smart static int 527469d0ef0SJames Smart nvme_fc_rport_get(struct nvme_fc_rport *rport) 528469d0ef0SJames Smart { 529469d0ef0SJames Smart return kref_get_unless_zero(&rport->ref); 530469d0ef0SJames Smart } 531469d0ef0SJames Smart 5322b632970SJames Smart static void 5332b632970SJames Smart nvme_fc_resume_controller(struct nvme_fc_ctrl *ctrl) 5342b632970SJames Smart { 5352b632970SJames Smart switch (ctrl->ctrl.state) { 5362b632970SJames Smart case NVME_CTRL_NEW: 5372b632970SJames Smart case NVME_CTRL_RECONNECTING: 5382b632970SJames Smart /* 5392b632970SJames Smart * As all reconnects were suppressed, schedule a 5402b632970SJames Smart * connect. 5412b632970SJames Smart */ 5422b632970SJames Smart dev_info(ctrl->ctrl.device, 5432b632970SJames Smart "NVME-FC{%d}: connectivity re-established. " 5442b632970SJames Smart "Attempting reconnect\n", ctrl->cnum); 5452b632970SJames Smart 5462b632970SJames Smart queue_delayed_work(nvme_wq, &ctrl->connect_work, 0); 5472b632970SJames Smart break; 5482b632970SJames Smart 5492b632970SJames Smart case NVME_CTRL_RESETTING: 5502b632970SJames Smart /* 5512b632970SJames Smart * Controller is already in the process of terminating the 5522b632970SJames Smart * association. No need to do anything further. The reconnect 5532b632970SJames Smart * step will naturally occur after the reset completes. 5542b632970SJames Smart */ 5552b632970SJames Smart break; 5562b632970SJames Smart 5572b632970SJames Smart default: 5582b632970SJames Smart /* no action to take - let it delete */ 5592b632970SJames Smart break; 5602b632970SJames Smart } 5612b632970SJames Smart } 5622b632970SJames Smart 5632b632970SJames Smart static struct nvme_fc_rport * 5642b632970SJames Smart nvme_fc_attach_to_suspended_rport(struct nvme_fc_lport *lport, 5652b632970SJames Smart struct nvme_fc_port_info *pinfo) 5662b632970SJames Smart { 5672b632970SJames Smart struct nvme_fc_rport *rport; 5682b632970SJames Smart struct nvme_fc_ctrl *ctrl; 5692b632970SJames Smart unsigned long flags; 5702b632970SJames Smart 5712b632970SJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 5722b632970SJames Smart 5732b632970SJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) { 5742b632970SJames Smart if (rport->remoteport.node_name != pinfo->node_name || 5752b632970SJames Smart rport->remoteport.port_name != pinfo->port_name) 5762b632970SJames Smart continue; 5772b632970SJames Smart 5782b632970SJames Smart if (!nvme_fc_rport_get(rport)) { 5792b632970SJames Smart rport = ERR_PTR(-ENOLCK); 5802b632970SJames Smart goto out_done; 5812b632970SJames Smart } 5822b632970SJames Smart 5832b632970SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 5842b632970SJames Smart 5852b632970SJames Smart spin_lock_irqsave(&rport->lock, flags); 5862b632970SJames Smart 5872b632970SJames Smart /* has it been unregistered */ 5882b632970SJames Smart if (rport->remoteport.port_state != FC_OBJSTATE_DELETED) { 5892b632970SJames Smart /* means lldd called us twice */ 5902b632970SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 5912b632970SJames Smart nvme_fc_rport_put(rport); 5922b632970SJames Smart return ERR_PTR(-ESTALE); 5932b632970SJames Smart } 5942b632970SJames Smart 5952b632970SJames Smart rport->remoteport.port_state = FC_OBJSTATE_ONLINE; 5962b632970SJames Smart rport->dev_loss_end = 0; 5972b632970SJames Smart 5982b632970SJames Smart /* 5992b632970SJames Smart * kick off a reconnect attempt on all associations to the 6002b632970SJames Smart * remote port. A successful reconnects will resume i/o. 6012b632970SJames Smart */ 6022b632970SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) 6032b632970SJames Smart nvme_fc_resume_controller(ctrl); 6042b632970SJames Smart 6052b632970SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 6062b632970SJames Smart 6072b632970SJames Smart return rport; 6082b632970SJames Smart } 6092b632970SJames Smart 6102b632970SJames Smart rport = NULL; 6112b632970SJames Smart 6122b632970SJames Smart out_done: 6132b632970SJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 6142b632970SJames Smart 6152b632970SJames Smart return rport; 6162b632970SJames Smart } 6172b632970SJames Smart 6182b632970SJames Smart static inline void 6192b632970SJames Smart __nvme_fc_set_dev_loss_tmo(struct nvme_fc_rport *rport, 6202b632970SJames Smart struct nvme_fc_port_info *pinfo) 6212b632970SJames Smart { 6222b632970SJames Smart if (pinfo->dev_loss_tmo) 6232b632970SJames Smart rport->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo; 6242b632970SJames Smart else 6252b632970SJames Smart rport->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO; 6262b632970SJames Smart } 6272b632970SJames Smart 628e399441dSJames Smart /** 629e399441dSJames Smart * nvme_fc_register_remoteport - transport entry point called by an 630e399441dSJames Smart * LLDD to register the existence of a NVME 631e399441dSJames Smart * subsystem FC port on its fabric. 632e399441dSJames Smart * @localport: pointer to the (registered) local port that the remote 633e399441dSJames Smart * subsystem port is connected to. 634e399441dSJames Smart * @pinfo: pointer to information about the port to be registered 635e399441dSJames Smart * @rport_p: pointer to a remote port pointer. Upon success, the routine 636e399441dSJames Smart * will allocate a nvme_fc_remote_port structure and place its 637e399441dSJames Smart * address in the remote port pointer. Upon failure, remote port 638e399441dSJames Smart * pointer will be set to 0. 639e399441dSJames Smart * 640e399441dSJames Smart * Returns: 641e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 642e399441dSJames Smart * (ex: -ENXIO) upon failure. 643e399441dSJames Smart */ 644e399441dSJames Smart int 645e399441dSJames Smart nvme_fc_register_remoteport(struct nvme_fc_local_port *localport, 646e399441dSJames Smart struct nvme_fc_port_info *pinfo, 647e399441dSJames Smart struct nvme_fc_remote_port **portptr) 648e399441dSJames Smart { 649e399441dSJames Smart struct nvme_fc_lport *lport = localport_to_lport(localport); 650e399441dSJames Smart struct nvme_fc_rport *newrec; 651e399441dSJames Smart unsigned long flags; 652e399441dSJames Smart int ret, idx; 653e399441dSJames Smart 6542b632970SJames Smart if (!nvme_fc_lport_get(lport)) { 6552b632970SJames Smart ret = -ESHUTDOWN; 6562b632970SJames Smart goto out_reghost_failed; 6572b632970SJames Smart } 6582b632970SJames Smart 6592b632970SJames Smart /* 6602b632970SJames Smart * look to see if there is already a remoteport that is waiting 6612b632970SJames Smart * for a reconnect (within dev_loss_tmo) with the same WWN's. 6622b632970SJames Smart * If so, transition to it and reconnect. 6632b632970SJames Smart */ 6642b632970SJames Smart newrec = nvme_fc_attach_to_suspended_rport(lport, pinfo); 6652b632970SJames Smart 6662b632970SJames Smart /* found an rport, but something about its state is bad */ 6672b632970SJames Smart if (IS_ERR(newrec)) { 6682b632970SJames Smart ret = PTR_ERR(newrec); 6692b632970SJames Smart goto out_lport_put; 6702b632970SJames Smart 6712b632970SJames Smart /* found existing rport, which was resumed */ 6722b632970SJames Smart } else if (newrec) { 6732b632970SJames Smart nvme_fc_lport_put(lport); 6742b632970SJames Smart __nvme_fc_set_dev_loss_tmo(newrec, pinfo); 6752b632970SJames Smart nvme_fc_signal_discovery_scan(lport, newrec); 6762b632970SJames Smart *portptr = &newrec->remoteport; 6772b632970SJames Smart return 0; 6782b632970SJames Smart } 6792b632970SJames Smart 6802b632970SJames Smart /* nothing found - allocate a new remoteport struct */ 6812b632970SJames Smart 682e399441dSJames Smart newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz), 683e399441dSJames Smart GFP_KERNEL); 684e399441dSJames Smart if (!newrec) { 685e399441dSJames Smart ret = -ENOMEM; 6862b632970SJames Smart goto out_lport_put; 687e399441dSJames Smart } 688e399441dSJames Smart 689e399441dSJames Smart idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL); 690e399441dSJames Smart if (idx < 0) { 691e399441dSJames Smart ret = -ENOSPC; 6922b632970SJames Smart goto out_kfree_rport; 693e399441dSJames Smart } 694e399441dSJames Smart 695e399441dSJames Smart INIT_LIST_HEAD(&newrec->endp_list); 696e399441dSJames Smart INIT_LIST_HEAD(&newrec->ctrl_list); 697c913a8b0SJames Smart INIT_LIST_HEAD(&newrec->ls_req_list); 698e399441dSJames Smart kref_init(&newrec->ref); 699158bfb88SJames Smart atomic_set(&newrec->act_ctrl_cnt, 0); 700e399441dSJames Smart spin_lock_init(&newrec->lock); 701e399441dSJames Smart newrec->remoteport.localport = &lport->localport; 702c913a8b0SJames Smart newrec->dev = lport->dev; 703c913a8b0SJames Smart newrec->lport = lport; 704e399441dSJames Smart newrec->remoteport.private = &newrec[1]; 705e399441dSJames Smart newrec->remoteport.port_role = pinfo->port_role; 706e399441dSJames Smart newrec->remoteport.node_name = pinfo->node_name; 707e399441dSJames Smart newrec->remoteport.port_name = pinfo->port_name; 708e399441dSJames Smart newrec->remoteport.port_id = pinfo->port_id; 709e399441dSJames Smart newrec->remoteport.port_state = FC_OBJSTATE_ONLINE; 710e399441dSJames Smart newrec->remoteport.port_num = idx; 7112b632970SJames Smart __nvme_fc_set_dev_loss_tmo(newrec, pinfo); 712e399441dSJames Smart 713e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 714e399441dSJames Smart list_add_tail(&newrec->endp_list, &lport->endp_list); 715e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 716e399441dSJames Smart 717eaefd5abSJames Smart nvme_fc_signal_discovery_scan(lport, newrec); 718eaefd5abSJames Smart 719e399441dSJames Smart *portptr = &newrec->remoteport; 720e399441dSJames Smart return 0; 721e399441dSJames Smart 722e399441dSJames Smart out_kfree_rport: 723e399441dSJames Smart kfree(newrec); 7242b632970SJames Smart out_lport_put: 7252b632970SJames Smart nvme_fc_lport_put(lport); 726e399441dSJames Smart out_reghost_failed: 727e399441dSJames Smart *portptr = NULL; 728e399441dSJames Smart return ret; 729e399441dSJames Smart } 730e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport); 731e399441dSJames Smart 7328d64daf7SJames Smart static int 7338d64daf7SJames Smart nvme_fc_abort_lsops(struct nvme_fc_rport *rport) 7348d64daf7SJames Smart { 7358d64daf7SJames Smart struct nvmefc_ls_req_op *lsop; 7368d64daf7SJames Smart unsigned long flags; 7378d64daf7SJames Smart 7388d64daf7SJames Smart restart: 7398d64daf7SJames Smart spin_lock_irqsave(&rport->lock, flags); 7408d64daf7SJames Smart 7418d64daf7SJames Smart list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) { 7428d64daf7SJames Smart if (!(lsop->flags & FCOP_FLAGS_TERMIO)) { 7438d64daf7SJames Smart lsop->flags |= FCOP_FLAGS_TERMIO; 7448d64daf7SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 7458d64daf7SJames Smart rport->lport->ops->ls_abort(&rport->lport->localport, 7468d64daf7SJames Smart &rport->remoteport, 7478d64daf7SJames Smart &lsop->ls_req); 7488d64daf7SJames Smart goto restart; 7498d64daf7SJames Smart } 7508d64daf7SJames Smart } 7518d64daf7SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 7528d64daf7SJames Smart 7538d64daf7SJames Smart return 0; 7548d64daf7SJames Smart } 7558d64daf7SJames Smart 7562b632970SJames Smart static void 7572b632970SJames Smart nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl) 7582b632970SJames Smart { 7592b632970SJames Smart dev_info(ctrl->ctrl.device, 7602b632970SJames Smart "NVME-FC{%d}: controller connectivity lost. Awaiting " 7612b632970SJames Smart "Reconnect", ctrl->cnum); 7622b632970SJames Smart 7632b632970SJames Smart switch (ctrl->ctrl.state) { 7642b632970SJames Smart case NVME_CTRL_NEW: 7652b632970SJames Smart case NVME_CTRL_LIVE: 7662b632970SJames Smart /* 7672b632970SJames Smart * Schedule a controller reset. The reset will terminate the 7682b632970SJames Smart * association and schedule the reconnect timer. Reconnects 7692b632970SJames Smart * will be attempted until either the ctlr_loss_tmo 7702b632970SJames Smart * (max_retries * connect_delay) expires or the remoteport's 7712b632970SJames Smart * dev_loss_tmo expires. 7722b632970SJames Smart */ 7732b632970SJames Smart if (nvme_reset_ctrl(&ctrl->ctrl)) { 7742b632970SJames Smart dev_warn(ctrl->ctrl.device, 7752b632970SJames Smart "NVME-FC{%d}: Couldn't schedule reset. " 7762b632970SJames Smart "Deleting controller.\n", 7772b632970SJames Smart ctrl->cnum); 7782b632970SJames Smart nvme_delete_ctrl(&ctrl->ctrl); 7792b632970SJames Smart } 7802b632970SJames Smart break; 7812b632970SJames Smart 7822b632970SJames Smart case NVME_CTRL_RECONNECTING: 7832b632970SJames Smart /* 7842b632970SJames Smart * The association has already been terminated and the 7852b632970SJames Smart * controller is attempting reconnects. No need to do anything 7862b632970SJames Smart * futher. Reconnects will be attempted until either the 7872b632970SJames Smart * ctlr_loss_tmo (max_retries * connect_delay) expires or the 7882b632970SJames Smart * remoteport's dev_loss_tmo expires. 7892b632970SJames Smart */ 7902b632970SJames Smart break; 7912b632970SJames Smart 7922b632970SJames Smart case NVME_CTRL_RESETTING: 7932b632970SJames Smart /* 7942b632970SJames Smart * Controller is already in the process of terminating the 7952b632970SJames Smart * association. No need to do anything further. The reconnect 7962b632970SJames Smart * step will kick in naturally after the association is 7972b632970SJames Smart * terminated. 7982b632970SJames Smart */ 7992b632970SJames Smart break; 8002b632970SJames Smart 8012b632970SJames Smart case NVME_CTRL_DELETING: 8022b632970SJames Smart default: 8032b632970SJames Smart /* no action to take - let it delete */ 8042b632970SJames Smart break; 8052b632970SJames Smart } 8062b632970SJames Smart } 8072b632970SJames Smart 808e399441dSJames Smart /** 809e399441dSJames Smart * nvme_fc_unregister_remoteport - transport entry point called by an 810e399441dSJames Smart * LLDD to deregister/remove a previously 811e399441dSJames Smart * registered a NVME subsystem FC port. 812e399441dSJames Smart * @remoteport: pointer to the (registered) remote port that is to be 813e399441dSJames Smart * deregistered. 814e399441dSJames Smart * 815e399441dSJames Smart * Returns: 816e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 817e399441dSJames Smart * (ex: -ENXIO) upon failure. 818e399441dSJames Smart */ 819e399441dSJames Smart int 820e399441dSJames Smart nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr) 821e399441dSJames Smart { 822e399441dSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(portptr); 823e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 824e399441dSJames Smart unsigned long flags; 825e399441dSJames Smart 826e399441dSJames Smart if (!portptr) 827e399441dSJames Smart return -EINVAL; 828e399441dSJames Smart 829e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags); 830e399441dSJames Smart 831e399441dSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) { 832e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 833e399441dSJames Smart return -EINVAL; 834e399441dSJames Smart } 835e399441dSJames Smart portptr->port_state = FC_OBJSTATE_DELETED; 836e399441dSJames Smart 8372b632970SJames Smart rport->dev_loss_end = jiffies + (portptr->dev_loss_tmo * HZ); 8382b632970SJames Smart 8392b632970SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) { 8402b632970SJames Smart /* if dev_loss_tmo==0, dev loss is immediate */ 8412b632970SJames Smart if (!portptr->dev_loss_tmo) { 8422b632970SJames Smart dev_warn(ctrl->ctrl.device, 8432b632970SJames Smart "NVME-FC{%d}: controller connectivity lost. " 8442b632970SJames Smart "Deleting controller.\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); 1213e399441dSJames Smart assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize); 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); 1227e399441dSJames Smart lsreq->timeout = NVME_FC_CONNECT_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, 1267e399441dSJames Smart "q %d connect 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); 1328e399441dSJames Smart conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize); 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); 1335e399441dSJames Smart lsreq->timeout = NVME_FC_CONNECT_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, 1366e399441dSJames Smart "q %d connect 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, 1379e399441dSJames Smart "queue %d connect command 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 1391e399441dSJames Smart /* fc-nvme iniator 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 { 1416e399441dSJames Smart struct fcnvme_ls_disconnect_rqst *discon_rqst; 1417e399441dSJames Smart struct fcnvme_ls_disconnect_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]; 1433e399441dSJames Smart discon_rqst = (struct fcnvme_ls_disconnect_rqst *) 1434e399441dSJames Smart (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz); 1435e399441dSJames Smart discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1]; 1436e399441dSJames Smart 1437e399441dSJames Smart discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT; 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 discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION; 1455e399441dSJames Smart discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id); 1456e399441dSJames Smart 1457e399441dSJames Smart lsreq->rqstaddr = discon_rqst; 1458e399441dSJames Smart lsreq->rqstlen = sizeof(*discon_rqst); 1459e399441dSJames Smart lsreq->rspaddr = discon_acc; 1460e399441dSJames Smart lsreq->rsplen = sizeof(*discon_acc); 1461e399441dSJames Smart lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC; 1462e399441dSJames Smart 1463c913a8b0SJames Smart ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop, 1464c913a8b0SJames Smart nvme_fc_disconnect_assoc_done); 1465c913a8b0SJames Smart if (ret) 1466c913a8b0SJames Smart kfree(lsop); 1467e399441dSJames Smart 1468e399441dSJames Smart /* only meaningful part to terminating the association */ 1469e399441dSJames Smart ctrl->association_id = 0; 1470e399441dSJames Smart } 1471e399441dSJames Smart 1472e399441dSJames Smart 1473e399441dSJames Smart /* *********************** NVME Ctrl Routines **************************** */ 1474e399441dSJames Smart 147578a7ac26SJames Smart static void __nvme_fc_final_op_cleanup(struct request *rq); 1476f874d5d0SJames Smart static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg); 1477e399441dSJames Smart 1478e399441dSJames Smart static int 1479e399441dSJames Smart nvme_fc_reinit_request(void *data, struct request *rq) 1480e399441dSJames Smart { 1481e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1482e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 1483e399441dSJames Smart 1484e399441dSJames Smart memset(cmdiu, 0, sizeof(*cmdiu)); 1485e399441dSJames Smart cmdiu->scsi_id = NVME_CMD_SCSI_ID; 1486e399441dSJames Smart cmdiu->fc_id = NVME_CMD_FC_ID; 1487e399441dSJames Smart cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32)); 1488e399441dSJames Smart memset(&op->rsp_iu, 0, sizeof(op->rsp_iu)); 1489e399441dSJames Smart 1490e399441dSJames Smart return 0; 1491e399441dSJames Smart } 1492e399441dSJames Smart 1493e399441dSJames Smart static void 1494e399441dSJames Smart __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl, 1495e399441dSJames Smart struct nvme_fc_fcp_op *op) 1496e399441dSJames Smart { 1497e399441dSJames Smart fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma, 1498e399441dSJames Smart sizeof(op->rsp_iu), DMA_FROM_DEVICE); 1499e399441dSJames Smart fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma, 1500e399441dSJames Smart sizeof(op->cmd_iu), DMA_TO_DEVICE); 1501e399441dSJames Smart 1502e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_UNINIT); 1503e399441dSJames Smart } 1504e399441dSJames Smart 1505e399441dSJames Smart static void 1506d6296d39SChristoph Hellwig nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq, 1507d6296d39SChristoph Hellwig unsigned int hctx_idx) 1508e399441dSJames Smart { 1509e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1510e399441dSJames Smart 1511d6296d39SChristoph Hellwig return __nvme_fc_exit_request(set->driver_data, op); 1512e399441dSJames Smart } 1513e399441dSJames Smart 151478a7ac26SJames Smart static int 151578a7ac26SJames Smart __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op) 151678a7ac26SJames Smart { 151778a7ac26SJames Smart int state; 151878a7ac26SJames Smart 151978a7ac26SJames Smart state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED); 152078a7ac26SJames Smart if (state != FCPOP_STATE_ACTIVE) { 152178a7ac26SJames Smart atomic_set(&op->state, state); 152278a7ac26SJames Smart return -ECANCELED; 152378a7ac26SJames Smart } 152478a7ac26SJames Smart 152578a7ac26SJames Smart ctrl->lport->ops->fcp_abort(&ctrl->lport->localport, 152678a7ac26SJames Smart &ctrl->rport->remoteport, 152778a7ac26SJames Smart op->queue->lldd_handle, 152878a7ac26SJames Smart &op->fcp_req); 152978a7ac26SJames Smart 153078a7ac26SJames Smart return 0; 153178a7ac26SJames Smart } 153278a7ac26SJames Smart 1533e399441dSJames Smart static void 153478a7ac26SJames Smart nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl) 1535e399441dSJames Smart { 1536e399441dSJames Smart struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops; 153778a7ac26SJames Smart unsigned long flags; 153878a7ac26SJames Smart int i, ret; 1539e399441dSJames Smart 154038dabe21SKeith Busch for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) { 154178a7ac26SJames Smart if (atomic_read(&aen_op->state) != FCPOP_STATE_ACTIVE) 1542e399441dSJames Smart continue; 154378a7ac26SJames Smart 154478a7ac26SJames Smart spin_lock_irqsave(&ctrl->lock, flags); 154561bff8efSJames Smart if (ctrl->flags & FCCTRL_TERMIO) { 154661bff8efSJames Smart ctrl->iocnt++; 154778a7ac26SJames Smart aen_op->flags |= FCOP_FLAGS_TERMIO; 154861bff8efSJames Smart } 154978a7ac26SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 155078a7ac26SJames Smart 155178a7ac26SJames Smart ret = __nvme_fc_abort_op(ctrl, aen_op); 155278a7ac26SJames Smart if (ret) { 155378a7ac26SJames Smart /* 155478a7ac26SJames Smart * if __nvme_fc_abort_op failed the io wasn't 155578a7ac26SJames Smart * active. Thus this call path is running in 155678a7ac26SJames Smart * parallel to the io complete. Treat as non-error. 155778a7ac26SJames Smart */ 155878a7ac26SJames Smart 155978a7ac26SJames Smart /* back out the flags/counters */ 156078a7ac26SJames Smart spin_lock_irqsave(&ctrl->lock, flags); 156161bff8efSJames Smart if (ctrl->flags & FCCTRL_TERMIO) 156261bff8efSJames Smart ctrl->iocnt--; 156378a7ac26SJames Smart aen_op->flags &= ~FCOP_FLAGS_TERMIO; 156478a7ac26SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 156578a7ac26SJames Smart return; 1566e399441dSJames Smart } 1567e399441dSJames Smart } 156878a7ac26SJames Smart } 156978a7ac26SJames Smart 157078a7ac26SJames Smart static inline int 157178a7ac26SJames Smart __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl, 157278a7ac26SJames Smart struct nvme_fc_fcp_op *op) 157378a7ac26SJames Smart { 157478a7ac26SJames Smart unsigned long flags; 157578a7ac26SJames Smart bool complete_rq = false; 157678a7ac26SJames Smart 157778a7ac26SJames Smart spin_lock_irqsave(&ctrl->lock, flags); 157861bff8efSJames Smart if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) { 157936715cf4SJames Smart if (ctrl->flags & FCCTRL_TERMIO) { 158036715cf4SJames Smart if (!--ctrl->iocnt) 158136715cf4SJames Smart wake_up(&ctrl->ioabort_wait); 158236715cf4SJames Smart } 158361bff8efSJames Smart } 158478a7ac26SJames Smart if (op->flags & FCOP_FLAGS_RELEASED) 158578a7ac26SJames Smart complete_rq = true; 158678a7ac26SJames Smart else 158778a7ac26SJames Smart op->flags |= FCOP_FLAGS_COMPLETE; 158878a7ac26SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 158978a7ac26SJames Smart 159078a7ac26SJames Smart return complete_rq; 159178a7ac26SJames Smart } 1592e399441dSJames Smart 1593baee29acSChristoph Hellwig static void 1594e399441dSJames Smart nvme_fc_fcpio_done(struct nvmefc_fcp_req *req) 1595e399441dSJames Smart { 1596e399441dSJames Smart struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req); 1597e399441dSJames Smart struct request *rq = op->rq; 1598e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req; 1599e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl; 1600e399441dSJames Smart struct nvme_fc_queue *queue = op->queue; 1601e399441dSJames Smart struct nvme_completion *cqe = &op->rsp_iu.cqe; 1602458f280dSJames Smart struct nvme_command *sqe = &op->cmd_iu.sqe; 1603d663b69fSChristoph Hellwig __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1); 160427fa9bc5SChristoph Hellwig union nvme_result result; 16050a02e39fSJames Smart bool terminate_assoc = true; 1606e399441dSJames Smart 1607e399441dSJames Smart /* 1608e399441dSJames Smart * WARNING: 1609e399441dSJames Smart * The current linux implementation of a nvme controller 1610e399441dSJames Smart * allocates a single tag set for all io queues and sizes 1611e399441dSJames Smart * the io queues to fully hold all possible tags. Thus, the 1612e399441dSJames Smart * implementation does not reference or care about the sqhd 1613e399441dSJames Smart * value as it never needs to use the sqhd/sqtail pointers 1614e399441dSJames Smart * for submission pacing. 1615e399441dSJames Smart * 1616e399441dSJames Smart * This affects the FC-NVME implementation in two ways: 1617e399441dSJames Smart * 1) As the value doesn't matter, we don't need to waste 1618e399441dSJames Smart * cycles extracting it from ERSPs and stamping it in the 1619e399441dSJames Smart * cases where the transport fabricates CQEs on successful 1620e399441dSJames Smart * completions. 1621e399441dSJames Smart * 2) The FC-NVME implementation requires that delivery of 1622e399441dSJames Smart * ERSP completions are to go back to the nvme layer in order 1623e399441dSJames Smart * relative to the rsn, such that the sqhd value will always 1624e399441dSJames Smart * be "in order" for the nvme layer. As the nvme layer in 1625e399441dSJames Smart * linux doesn't care about sqhd, there's no need to return 1626e399441dSJames Smart * them in order. 1627e399441dSJames Smart * 1628e399441dSJames Smart * Additionally: 1629e399441dSJames Smart * As the core nvme layer in linux currently does not look at 1630e399441dSJames Smart * every field in the cqe - in cases where the FC transport must 1631e399441dSJames Smart * fabricate a CQE, the following fields will not be set as they 1632e399441dSJames Smart * are not referenced: 1633e399441dSJames Smart * cqe.sqid, cqe.sqhd, cqe.command_id 1634f874d5d0SJames Smart * 1635f874d5d0SJames Smart * Failure or error of an individual i/o, in a transport 1636f874d5d0SJames Smart * detected fashion unrelated to the nvme completion status, 1637f874d5d0SJames Smart * potentially cause the initiator and target sides to get out 1638f874d5d0SJames Smart * of sync on SQ head/tail (aka outstanding io count allowed). 1639f874d5d0SJames Smart * Per FC-NVME spec, failure of an individual command requires 1640f874d5d0SJames Smart * the connection to be terminated, which in turn requires the 1641f874d5d0SJames Smart * association to be terminated. 1642e399441dSJames Smart */ 1643e399441dSJames Smart 1644e399441dSJames Smart fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma, 1645e399441dSJames Smart sizeof(op->rsp_iu), DMA_FROM_DEVICE); 1646e399441dSJames Smart 16470a02e39fSJames Smart if (atomic_read(&op->state) == FCPOP_STATE_ABORTED || 16480a02e39fSJames Smart op->flags & FCOP_FLAGS_TERMIO) 16490a02e39fSJames Smart status = cpu_to_le16(NVME_SC_ABORT_REQ << 1); 165062eeacb0SJames Smart else if (freq->status) 165156b7103aSJames Smart status = cpu_to_le16(NVME_SC_INTERNAL << 1); 1652e399441dSJames Smart 1653e399441dSJames Smart /* 1654e399441dSJames Smart * For the linux implementation, if we have an unsuccesful 1655e399441dSJames Smart * status, they blk-mq layer can typically be called with the 1656e399441dSJames Smart * non-zero status and the content of the cqe isn't important. 1657e399441dSJames Smart */ 1658e399441dSJames Smart if (status) 1659e399441dSJames Smart goto done; 1660e399441dSJames Smart 1661e399441dSJames Smart /* 1662e399441dSJames Smart * command completed successfully relative to the wire 1663e399441dSJames Smart * protocol. However, validate anything received and 1664e399441dSJames Smart * extract the status and result from the cqe (create it 1665e399441dSJames Smart * where necessary). 1666e399441dSJames Smart */ 1667e399441dSJames Smart 1668e399441dSJames Smart switch (freq->rcv_rsplen) { 1669e399441dSJames Smart 1670e399441dSJames Smart case 0: 1671e399441dSJames Smart case NVME_FC_SIZEOF_ZEROS_RSP: 1672e399441dSJames Smart /* 1673e399441dSJames Smart * No response payload or 12 bytes of payload (which 1674e399441dSJames Smart * should all be zeros) are considered successful and 1675e399441dSJames Smart * no payload in the CQE by the transport. 1676e399441dSJames Smart */ 1677e399441dSJames Smart if (freq->transferred_length != 1678e399441dSJames Smart be32_to_cpu(op->cmd_iu.data_len)) { 167956b7103aSJames Smart status = cpu_to_le16(NVME_SC_INTERNAL << 1); 1680e399441dSJames Smart goto done; 1681e399441dSJames Smart } 168227fa9bc5SChristoph Hellwig result.u64 = 0; 1683e399441dSJames Smart break; 1684e399441dSJames Smart 1685e399441dSJames Smart case sizeof(struct nvme_fc_ersp_iu): 1686e399441dSJames Smart /* 1687e399441dSJames Smart * The ERSP IU contains a full completion with CQE. 1688e399441dSJames Smart * Validate ERSP IU and look at cqe. 1689e399441dSJames Smart */ 1690e399441dSJames Smart if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) != 1691e399441dSJames Smart (freq->rcv_rsplen / 4) || 1692e399441dSJames Smart be32_to_cpu(op->rsp_iu.xfrd_len) != 1693e399441dSJames Smart freq->transferred_length || 1694726a1080SJames Smart op->rsp_iu.status_code || 1695458f280dSJames Smart sqe->common.command_id != cqe->command_id)) { 169656b7103aSJames Smart status = cpu_to_le16(NVME_SC_INTERNAL << 1); 1697e399441dSJames Smart goto done; 1698e399441dSJames Smart } 169927fa9bc5SChristoph Hellwig result = cqe->result; 1700d663b69fSChristoph Hellwig status = cqe->status; 1701e399441dSJames Smart break; 1702e399441dSJames Smart 1703e399441dSJames Smart default: 170456b7103aSJames Smart status = cpu_to_le16(NVME_SC_INTERNAL << 1); 1705e399441dSJames Smart goto done; 1706e399441dSJames Smart } 1707e399441dSJames Smart 1708f874d5d0SJames Smart terminate_assoc = false; 1709f874d5d0SJames Smart 1710e399441dSJames Smart done: 171178a7ac26SJames Smart if (op->flags & FCOP_FLAGS_AEN) { 171227fa9bc5SChristoph Hellwig nvme_complete_async_event(&queue->ctrl->ctrl, status, &result); 17130a02e39fSJames Smart __nvme_fc_fcpop_chk_teardowns(ctrl, op); 171478a7ac26SJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE); 171578a7ac26SJames Smart op->flags = FCOP_FLAGS_AEN; /* clear other flags */ 1716e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 1717f874d5d0SJames Smart goto check_error; 1718e399441dSJames Smart } 1719e399441dSJames Smart 17200a02e39fSJames Smart /* 17210a02e39fSJames Smart * Force failures of commands if we're killing the controller 17220a02e39fSJames Smart * or have an error on a command used to create an new association 17230a02e39fSJames Smart */ 17240a02e39fSJames Smart if (status && 17250a02e39fSJames Smart (blk_queue_dying(rq->q) || 17260a02e39fSJames Smart ctrl->ctrl.state == NVME_CTRL_NEW || 17270a02e39fSJames Smart ctrl->ctrl.state == NVME_CTRL_RECONNECTING)) 1728e392e1f1SJames Smart status |= cpu_to_le16(NVME_SC_DNR << 1); 17290a02e39fSJames Smart 17300a02e39fSJames Smart if (__nvme_fc_fcpop_chk_teardowns(ctrl, op)) 173178a7ac26SJames Smart __nvme_fc_final_op_cleanup(rq); 17320a02e39fSJames Smart else 17330a02e39fSJames Smart nvme_end_request(rq, status, result); 1734f874d5d0SJames Smart 1735f874d5d0SJames Smart check_error: 1736f874d5d0SJames Smart if (terminate_assoc) 1737f874d5d0SJames Smart nvme_fc_error_recovery(ctrl, "transport detected io error"); 1738e399441dSJames Smart } 1739e399441dSJames Smart 1740e399441dSJames Smart static int 1741e399441dSJames Smart __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl, 1742e399441dSJames Smart struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op, 1743e399441dSJames Smart struct request *rq, u32 rqno) 1744e399441dSJames Smart { 1745e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 1746e399441dSJames Smart int ret = 0; 1747e399441dSJames Smart 1748e399441dSJames Smart memset(op, 0, sizeof(*op)); 1749e399441dSJames Smart op->fcp_req.cmdaddr = &op->cmd_iu; 1750e399441dSJames Smart op->fcp_req.cmdlen = sizeof(op->cmd_iu); 1751e399441dSJames Smart op->fcp_req.rspaddr = &op->rsp_iu; 1752e399441dSJames Smart op->fcp_req.rsplen = sizeof(op->rsp_iu); 1753e399441dSJames Smart op->fcp_req.done = nvme_fc_fcpio_done; 1754e399441dSJames Smart op->fcp_req.first_sgl = (struct scatterlist *)&op[1]; 1755e399441dSJames Smart op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE]; 1756e399441dSJames Smart op->ctrl = ctrl; 1757e399441dSJames Smart op->queue = queue; 1758e399441dSJames Smart op->rq = rq; 1759e399441dSJames Smart op->rqno = rqno; 1760e399441dSJames Smart 1761e399441dSJames Smart cmdiu->scsi_id = NVME_CMD_SCSI_ID; 1762e399441dSJames Smart cmdiu->fc_id = NVME_CMD_FC_ID; 1763e399441dSJames Smart cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32)); 1764e399441dSJames Smart 1765e399441dSJames Smart op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev, 1766e399441dSJames Smart &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE); 1767e399441dSJames Smart if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) { 1768e399441dSJames Smart dev_err(ctrl->dev, 1769e399441dSJames Smart "FCP Op failed - cmdiu dma mapping failed.\n"); 1770e399441dSJames Smart ret = EFAULT; 1771e399441dSJames Smart goto out_on_error; 1772e399441dSJames Smart } 1773e399441dSJames Smart 1774e399441dSJames Smart op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev, 1775e399441dSJames Smart &op->rsp_iu, sizeof(op->rsp_iu), 1776e399441dSJames Smart DMA_FROM_DEVICE); 1777e399441dSJames Smart if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) { 1778e399441dSJames Smart dev_err(ctrl->dev, 1779e399441dSJames Smart "FCP Op failed - rspiu dma mapping failed.\n"); 1780e399441dSJames Smart ret = EFAULT; 1781e399441dSJames Smart } 1782e399441dSJames Smart 1783e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE); 1784e399441dSJames Smart out_on_error: 1785e399441dSJames Smart return ret; 1786e399441dSJames Smart } 1787e399441dSJames Smart 1788e399441dSJames Smart static int 1789d6296d39SChristoph Hellwig nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq, 1790d6296d39SChristoph Hellwig unsigned int hctx_idx, unsigned int numa_node) 1791e399441dSJames Smart { 1792d6296d39SChristoph Hellwig struct nvme_fc_ctrl *ctrl = set->driver_data; 1793e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 179476f983cbSChristoph Hellwig int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0; 179576f983cbSChristoph Hellwig struct nvme_fc_queue *queue = &ctrl->queues[queue_idx]; 1796e399441dSJames Smart 1797e399441dSJames Smart return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++); 1798e399441dSJames Smart } 1799e399441dSJames Smart 1800e399441dSJames Smart static int 1801e399441dSJames Smart nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl) 1802e399441dSJames Smart { 1803e399441dSJames Smart struct nvme_fc_fcp_op *aen_op; 1804e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu; 1805e399441dSJames Smart struct nvme_command *sqe; 180661bff8efSJames Smart void *private; 1807e399441dSJames Smart int i, ret; 1808e399441dSJames Smart 1809e399441dSJames Smart aen_op = ctrl->aen_ops; 181038dabe21SKeith Busch for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) { 181161bff8efSJames Smart private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz, 181261bff8efSJames Smart GFP_KERNEL); 181361bff8efSJames Smart if (!private) 181461bff8efSJames Smart return -ENOMEM; 181561bff8efSJames Smart 1816e399441dSJames Smart cmdiu = &aen_op->cmd_iu; 1817e399441dSJames Smart sqe = &cmdiu->sqe; 1818e399441dSJames Smart ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0], 1819e399441dSJames Smart aen_op, (struct request *)NULL, 182038dabe21SKeith Busch (NVME_AQ_BLK_MQ_DEPTH + i)); 182161bff8efSJames Smart if (ret) { 182261bff8efSJames Smart kfree(private); 1823e399441dSJames Smart return ret; 182461bff8efSJames Smart } 1825e399441dSJames Smart 182678a7ac26SJames Smart aen_op->flags = FCOP_FLAGS_AEN; 182761bff8efSJames Smart aen_op->fcp_req.first_sgl = NULL; /* no sg list */ 182861bff8efSJames Smart aen_op->fcp_req.private = private; 182978a7ac26SJames Smart 1830e399441dSJames Smart memset(sqe, 0, sizeof(*sqe)); 1831e399441dSJames Smart sqe->common.opcode = nvme_admin_async_event; 183278a7ac26SJames Smart /* Note: core layer may overwrite the sqe.command_id value */ 183338dabe21SKeith Busch sqe->common.command_id = NVME_AQ_BLK_MQ_DEPTH + i; 1834e399441dSJames Smart } 1835e399441dSJames Smart return 0; 1836e399441dSJames Smart } 1837e399441dSJames Smart 183861bff8efSJames Smart static void 183961bff8efSJames Smart nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl) 184061bff8efSJames Smart { 184161bff8efSJames Smart struct nvme_fc_fcp_op *aen_op; 184261bff8efSJames Smart int i; 184361bff8efSJames Smart 184461bff8efSJames Smart aen_op = ctrl->aen_ops; 184538dabe21SKeith Busch for (i = 0; i < NVME_NR_AEN_COMMANDS; i++, aen_op++) { 184661bff8efSJames Smart if (!aen_op->fcp_req.private) 184761bff8efSJames Smart continue; 184861bff8efSJames Smart 184961bff8efSJames Smart __nvme_fc_exit_request(ctrl, aen_op); 185061bff8efSJames Smart 185161bff8efSJames Smart kfree(aen_op->fcp_req.private); 185261bff8efSJames Smart aen_op->fcp_req.private = NULL; 185361bff8efSJames Smart } 185461bff8efSJames Smart } 1855e399441dSJames Smart 1856e399441dSJames Smart static inline void 1857e399441dSJames Smart __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl, 1858e399441dSJames Smart unsigned int qidx) 1859e399441dSJames Smart { 1860e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[qidx]; 1861e399441dSJames Smart 1862e399441dSJames Smart hctx->driver_data = queue; 1863e399441dSJames Smart queue->hctx = hctx; 1864e399441dSJames Smart } 1865e399441dSJames Smart 1866e399441dSJames Smart static int 1867e399441dSJames Smart nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, 1868e399441dSJames Smart unsigned int hctx_idx) 1869e399441dSJames Smart { 1870e399441dSJames Smart struct nvme_fc_ctrl *ctrl = data; 1871e399441dSJames Smart 1872e399441dSJames Smart __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1); 1873e399441dSJames Smart 1874e399441dSJames Smart return 0; 1875e399441dSJames Smart } 1876e399441dSJames Smart 1877e399441dSJames Smart static int 1878e399441dSJames Smart nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, 1879e399441dSJames Smart unsigned int hctx_idx) 1880e399441dSJames Smart { 1881e399441dSJames Smart struct nvme_fc_ctrl *ctrl = data; 1882e399441dSJames Smart 1883e399441dSJames Smart __nvme_fc_init_hctx(hctx, ctrl, hctx_idx); 1884e399441dSJames Smart 1885e399441dSJames Smart return 0; 1886e399441dSJames Smart } 1887e399441dSJames Smart 1888e399441dSJames Smart static void 188908e15075SKeith Busch nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx) 1890e399441dSJames Smart { 1891e399441dSJames Smart struct nvme_fc_queue *queue; 1892e399441dSJames Smart 1893e399441dSJames Smart queue = &ctrl->queues[idx]; 1894e399441dSJames Smart memset(queue, 0, sizeof(*queue)); 1895e399441dSJames Smart queue->ctrl = ctrl; 1896e399441dSJames Smart queue->qnum = idx; 1897e399441dSJames Smart atomic_set(&queue->csn, 1); 1898e399441dSJames Smart queue->dev = ctrl->dev; 1899e399441dSJames Smart 1900e399441dSJames Smart if (idx > 0) 1901e399441dSJames Smart queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; 1902e399441dSJames Smart else 1903e399441dSJames Smart queue->cmnd_capsule_len = sizeof(struct nvme_command); 1904e399441dSJames Smart 1905e399441dSJames Smart /* 1906e399441dSJames Smart * Considered whether we should allocate buffers for all SQEs 1907e399441dSJames Smart * and CQEs and dma map them - mapping their respective entries 1908e399441dSJames Smart * into the request structures (kernel vm addr and dma address) 1909e399441dSJames Smart * thus the driver could use the buffers/mappings directly. 1910e399441dSJames Smart * It only makes sense if the LLDD would use them for its 1911e399441dSJames Smart * messaging api. It's very unlikely most adapter api's would use 1912e399441dSJames Smart * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload 1913e399441dSJames Smart * structures were used instead. 1914e399441dSJames Smart */ 1915e399441dSJames Smart } 1916e399441dSJames Smart 1917e399441dSJames Smart /* 1918e399441dSJames Smart * This routine terminates a queue at the transport level. 1919e399441dSJames Smart * The transport has already ensured that all outstanding ios on 1920e399441dSJames Smart * the queue have been terminated. 1921e399441dSJames Smart * The transport will send a Disconnect LS request to terminate 1922e399441dSJames Smart * the queue's connection. Termination of the admin queue will also 1923e399441dSJames Smart * terminate the association at the target. 1924e399441dSJames Smart */ 1925e399441dSJames Smart static void 1926e399441dSJames Smart nvme_fc_free_queue(struct nvme_fc_queue *queue) 1927e399441dSJames Smart { 1928e399441dSJames Smart if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags)) 1929e399441dSJames Smart return; 1930e399441dSJames Smart 19319e0ed16aSSagi Grimberg clear_bit(NVME_FC_Q_LIVE, &queue->flags); 1932e399441dSJames Smart /* 1933e399441dSJames Smart * Current implementation never disconnects a single queue. 1934e399441dSJames Smart * It always terminates a whole association. So there is never 1935e399441dSJames Smart * a disconnect(queue) LS sent to the target. 1936e399441dSJames Smart */ 1937e399441dSJames Smart 1938e399441dSJames Smart queue->connection_id = 0; 1939e399441dSJames Smart } 1940e399441dSJames Smart 1941e399441dSJames Smart static void 1942e399441dSJames Smart __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl, 1943e399441dSJames Smart struct nvme_fc_queue *queue, unsigned int qidx) 1944e399441dSJames Smart { 1945e399441dSJames Smart if (ctrl->lport->ops->delete_queue) 1946e399441dSJames Smart ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx, 1947e399441dSJames Smart queue->lldd_handle); 1948e399441dSJames Smart queue->lldd_handle = NULL; 1949e399441dSJames Smart } 1950e399441dSJames Smart 1951e399441dSJames Smart static void 1952e399441dSJames Smart nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl) 1953e399441dSJames Smart { 1954e399441dSJames Smart int i; 1955e399441dSJames Smart 1956d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++) 1957e399441dSJames Smart nvme_fc_free_queue(&ctrl->queues[i]); 1958e399441dSJames Smart } 1959e399441dSJames Smart 1960e399441dSJames Smart static int 1961e399441dSJames Smart __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl, 1962e399441dSJames Smart struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize) 1963e399441dSJames Smart { 1964e399441dSJames Smart int ret = 0; 1965e399441dSJames Smart 1966e399441dSJames Smart queue->lldd_handle = NULL; 1967e399441dSJames Smart if (ctrl->lport->ops->create_queue) 1968e399441dSJames Smart ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport, 1969e399441dSJames Smart qidx, qsize, &queue->lldd_handle); 1970e399441dSJames Smart 1971e399441dSJames Smart return ret; 1972e399441dSJames Smart } 1973e399441dSJames Smart 1974e399441dSJames Smart static void 1975e399441dSJames Smart nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl) 1976e399441dSJames Smart { 1977d858e5f0SSagi Grimberg struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1]; 1978e399441dSJames Smart int i; 1979e399441dSJames Smart 1980d858e5f0SSagi Grimberg for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--) 1981e399441dSJames Smart __nvme_fc_delete_hw_queue(ctrl, queue, i); 1982e399441dSJames Smart } 1983e399441dSJames Smart 1984e399441dSJames Smart static int 1985e399441dSJames Smart nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize) 1986e399441dSJames Smart { 1987e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[1]; 198817a1ec08SJohannes Thumshirn int i, ret; 1989e399441dSJames Smart 1990d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) { 1991e399441dSJames Smart ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize); 199217a1ec08SJohannes Thumshirn if (ret) 199317a1ec08SJohannes Thumshirn goto delete_queues; 1994e399441dSJames Smart } 1995e399441dSJames Smart 1996e399441dSJames Smart return 0; 199717a1ec08SJohannes Thumshirn 199817a1ec08SJohannes Thumshirn delete_queues: 199917a1ec08SJohannes Thumshirn for (; i >= 0; i--) 200017a1ec08SJohannes Thumshirn __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i); 200117a1ec08SJohannes Thumshirn return ret; 2002e399441dSJames Smart } 2003e399441dSJames Smart 2004e399441dSJames Smart static int 2005e399441dSJames Smart nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize) 2006e399441dSJames Smart { 2007e399441dSJames Smart int i, ret = 0; 2008e399441dSJames Smart 2009d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++) { 2010e399441dSJames Smart ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize, 2011e399441dSJames Smart (qsize / 5)); 2012e399441dSJames Smart if (ret) 2013e399441dSJames Smart break; 2014e399441dSJames Smart ret = nvmf_connect_io_queue(&ctrl->ctrl, i); 2015e399441dSJames Smart if (ret) 2016e399441dSJames Smart break; 20179e0ed16aSSagi Grimberg 20189e0ed16aSSagi Grimberg set_bit(NVME_FC_Q_LIVE, &ctrl->queues[i].flags); 2019e399441dSJames Smart } 2020e399441dSJames Smart 2021e399441dSJames Smart return ret; 2022e399441dSJames Smart } 2023e399441dSJames Smart 2024e399441dSJames Smart static void 2025e399441dSJames Smart nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl) 2026e399441dSJames Smart { 2027e399441dSJames Smart int i; 2028e399441dSJames Smart 2029d858e5f0SSagi Grimberg for (i = 1; i < ctrl->ctrl.queue_count; i++) 203008e15075SKeith Busch nvme_fc_init_queue(ctrl, i); 2031e399441dSJames Smart } 2032e399441dSJames Smart 2033e399441dSJames Smart static void 2034e399441dSJames Smart nvme_fc_ctrl_free(struct kref *ref) 2035e399441dSJames Smart { 2036e399441dSJames Smart struct nvme_fc_ctrl *ctrl = 2037e399441dSJames Smart container_of(ref, struct nvme_fc_ctrl, ref); 2038e399441dSJames Smart unsigned long flags; 2039e399441dSJames Smart 204061bff8efSJames Smart if (ctrl->ctrl.tagset) { 204161bff8efSJames Smart blk_cleanup_queue(ctrl->ctrl.connect_q); 204261bff8efSJames Smart blk_mq_free_tag_set(&ctrl->tag_set); 204361bff8efSJames Smart } 204461bff8efSJames Smart 2045e399441dSJames Smart /* remove from rport list */ 2046e399441dSJames Smart spin_lock_irqsave(&ctrl->rport->lock, flags); 2047e399441dSJames Smart list_del(&ctrl->ctrl_list); 2048e399441dSJames Smart spin_unlock_irqrestore(&ctrl->rport->lock, flags); 204961bff8efSJames Smart 2050f9c5af5fSSagi Grimberg blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); 205161bff8efSJames Smart blk_cleanup_queue(ctrl->ctrl.admin_q); 205261bff8efSJames Smart blk_mq_free_tag_set(&ctrl->admin_tag_set); 205361bff8efSJames Smart 205461bff8efSJames Smart kfree(ctrl->queues); 2055e399441dSJames Smart 2056e399441dSJames Smart put_device(ctrl->dev); 2057e399441dSJames Smart nvme_fc_rport_put(ctrl->rport); 2058e399441dSJames Smart 2059e399441dSJames Smart ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum); 2060de41447aSEwan D. Milne if (ctrl->ctrl.opts) 2061e399441dSJames Smart nvmf_free_options(ctrl->ctrl.opts); 2062e399441dSJames Smart kfree(ctrl); 2063e399441dSJames Smart } 2064e399441dSJames Smart 2065e399441dSJames Smart static void 2066e399441dSJames Smart nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl) 2067e399441dSJames Smart { 2068e399441dSJames Smart kref_put(&ctrl->ref, nvme_fc_ctrl_free); 2069e399441dSJames Smart } 2070e399441dSJames Smart 2071e399441dSJames Smart static int 2072e399441dSJames Smart nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl) 2073e399441dSJames Smart { 2074e399441dSJames Smart return kref_get_unless_zero(&ctrl->ref); 2075e399441dSJames Smart } 2076e399441dSJames Smart 2077e399441dSJames Smart /* 2078e399441dSJames Smart * All accesses from nvme core layer done - can now free the 2079e399441dSJames Smart * controller. Called after last nvme_put_ctrl() call 2080e399441dSJames Smart */ 2081e399441dSJames Smart static void 208261bff8efSJames Smart nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl) 2083e399441dSJames Smart { 2084e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 2085e399441dSJames Smart 2086e399441dSJames Smart WARN_ON(nctrl != &ctrl->ctrl); 2087e399441dSJames Smart 2088e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 2089e399441dSJames Smart } 2090e399441dSJames Smart 209161bff8efSJames Smart static void 209261bff8efSJames Smart nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg) 209361bff8efSJames Smart { 209469fa9646SJames Smart /* only proceed if in LIVE state - e.g. on first error */ 209569fa9646SJames Smart if (ctrl->ctrl.state != NVME_CTRL_LIVE) 209669fa9646SJames Smart return; 209769fa9646SJames Smart 209861bff8efSJames Smart dev_warn(ctrl->ctrl.device, 209961bff8efSJames Smart "NVME-FC{%d}: transport association error detected: %s\n", 210061bff8efSJames Smart ctrl->cnum, errmsg); 2101589ff775SJames Smart dev_warn(ctrl->ctrl.device, 210261bff8efSJames Smart "NVME-FC{%d}: resetting controller\n", ctrl->cnum); 210361bff8efSJames Smart 2104d86c4d8eSChristoph Hellwig nvme_reset_ctrl(&ctrl->ctrl); 210561bff8efSJames Smart } 210661bff8efSJames Smart 2107baee29acSChristoph Hellwig static enum blk_eh_timer_return 2108e399441dSJames Smart nvme_fc_timeout(struct request *rq, bool reserved) 2109e399441dSJames Smart { 2110e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 2111e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl; 2112e399441dSJames Smart int ret; 2113e399441dSJames Smart 2114134aedc9SJames Smart if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE || 2115134aedc9SJames Smart atomic_read(&op->state) == FCPOP_STATE_ABORTED) 2116e399441dSJames Smart return BLK_EH_RESET_TIMER; 2117e399441dSJames Smart 2118e399441dSJames Smart ret = __nvme_fc_abort_op(ctrl, op); 2119e399441dSJames Smart if (ret) 2120134aedc9SJames Smart /* io wasn't active to abort */ 2121134aedc9SJames Smart return BLK_EH_NOT_HANDLED; 2122e399441dSJames Smart 2123e399441dSJames Smart /* 212461bff8efSJames Smart * we can't individually ABTS an io without affecting the queue, 212561bff8efSJames Smart * thus killing the queue, adn thus the association. 212661bff8efSJames Smart * So resolve by performing a controller reset, which will stop 212761bff8efSJames Smart * the host/io stack, terminate the association on the link, 212861bff8efSJames Smart * and recreate an association on the link. 2129e399441dSJames Smart */ 213061bff8efSJames Smart nvme_fc_error_recovery(ctrl, "io timeout error"); 2131e399441dSJames Smart 2132134aedc9SJames Smart /* 2133134aedc9SJames Smart * the io abort has been initiated. Have the reset timer 2134134aedc9SJames Smart * restarted and the abort completion will complete the io 2135134aedc9SJames Smart * shortly. Avoids a synchronous wait while the abort finishes. 2136134aedc9SJames Smart */ 2137134aedc9SJames Smart return BLK_EH_RESET_TIMER; 2138e399441dSJames Smart } 2139e399441dSJames Smart 2140e399441dSJames Smart static int 2141e399441dSJames Smart nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq, 2142e399441dSJames Smart struct nvme_fc_fcp_op *op) 2143e399441dSJames Smart { 2144e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req; 2145e399441dSJames Smart enum dma_data_direction dir; 2146e399441dSJames Smart int ret; 2147e399441dSJames Smart 2148e399441dSJames Smart freq->sg_cnt = 0; 2149e399441dSJames Smart 2150b131c61dSChristoph Hellwig if (!blk_rq_payload_bytes(rq)) 2151e399441dSJames Smart return 0; 2152e399441dSJames Smart 2153e399441dSJames Smart freq->sg_table.sgl = freq->first_sgl; 215419e420bbSChristoph Hellwig ret = sg_alloc_table_chained(&freq->sg_table, 215519e420bbSChristoph Hellwig blk_rq_nr_phys_segments(rq), freq->sg_table.sgl); 2156e399441dSJames Smart if (ret) 2157e399441dSJames Smart return -ENOMEM; 2158e399441dSJames Smart 2159e399441dSJames Smart op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl); 216019e420bbSChristoph Hellwig WARN_ON(op->nents > blk_rq_nr_phys_segments(rq)); 2161e399441dSJames Smart dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE; 2162e399441dSJames Smart freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl, 2163e399441dSJames Smart op->nents, dir); 2164e399441dSJames Smart if (unlikely(freq->sg_cnt <= 0)) { 2165e399441dSJames Smart sg_free_table_chained(&freq->sg_table, true); 2166e399441dSJames Smart freq->sg_cnt = 0; 2167e399441dSJames Smart return -EFAULT; 2168e399441dSJames Smart } 2169e399441dSJames Smart 2170e399441dSJames Smart /* 2171e399441dSJames Smart * TODO: blk_integrity_rq(rq) for DIF 2172e399441dSJames Smart */ 2173e399441dSJames Smart return 0; 2174e399441dSJames Smart } 2175e399441dSJames Smart 2176e399441dSJames Smart static void 2177e399441dSJames Smart nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq, 2178e399441dSJames Smart struct nvme_fc_fcp_op *op) 2179e399441dSJames Smart { 2180e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req; 2181e399441dSJames Smart 2182e399441dSJames Smart if (!freq->sg_cnt) 2183e399441dSJames Smart return; 2184e399441dSJames Smart 2185e399441dSJames Smart fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents, 2186e399441dSJames Smart ((rq_data_dir(rq) == WRITE) ? 2187e399441dSJames Smart DMA_TO_DEVICE : DMA_FROM_DEVICE)); 2188e399441dSJames Smart 2189e399441dSJames Smart nvme_cleanup_cmd(rq); 2190e399441dSJames Smart 2191e399441dSJames Smart sg_free_table_chained(&freq->sg_table, true); 2192e399441dSJames Smart 2193e399441dSJames Smart freq->sg_cnt = 0; 2194e399441dSJames Smart } 2195e399441dSJames Smart 2196e399441dSJames Smart /* 2197e399441dSJames Smart * In FC, the queue is a logical thing. At transport connect, the target 2198e399441dSJames Smart * creates its "queue" and returns a handle that is to be given to the 2199e399441dSJames Smart * target whenever it posts something to the corresponding SQ. When an 2200e399441dSJames Smart * SQE is sent on a SQ, FC effectively considers the SQE, or rather the 2201e399441dSJames Smart * command contained within the SQE, an io, and assigns a FC exchange 2202e399441dSJames Smart * to it. The SQE and the associated SQ handle are sent in the initial 2203e399441dSJames Smart * CMD IU sents on the exchange. All transfers relative to the io occur 2204e399441dSJames Smart * as part of the exchange. The CQE is the last thing for the io, 2205e399441dSJames Smart * which is transferred (explicitly or implicitly) with the RSP IU 2206e399441dSJames Smart * sent on the exchange. After the CQE is received, the FC exchange is 2207e399441dSJames Smart * terminaed and the Exchange may be used on a different io. 2208e399441dSJames Smart * 2209e399441dSJames Smart * The transport to LLDD api has the transport making a request for a 2210e399441dSJames Smart * new fcp io request to the LLDD. The LLDD then allocates a FC exchange 2211e399441dSJames Smart * resource and transfers the command. The LLDD will then process all 2212e399441dSJames Smart * steps to complete the io. Upon completion, the transport done routine 2213e399441dSJames Smart * is called. 2214e399441dSJames Smart * 2215e399441dSJames Smart * So - while the operation is outstanding to the LLDD, there is a link 2216e399441dSJames Smart * level FC exchange resource that is also outstanding. This must be 2217e399441dSJames Smart * considered in all cleanup operations. 2218e399441dSJames Smart */ 2219fc17b653SChristoph Hellwig static blk_status_t 2220e399441dSJames Smart nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue, 2221e399441dSJames Smart struct nvme_fc_fcp_op *op, u32 data_len, 2222e399441dSJames Smart enum nvmefc_fcp_datadir io_dir) 2223e399441dSJames Smart { 2224e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 2225e399441dSJames Smart struct nvme_command *sqe = &cmdiu->sqe; 2226e399441dSJames Smart u32 csn; 2227e399441dSJames Smart int ret; 2228e399441dSJames Smart 222961bff8efSJames Smart /* 223061bff8efSJames Smart * before attempting to send the io, check to see if we believe 223161bff8efSJames Smart * the target device is present 223261bff8efSJames Smart */ 223361bff8efSJames Smart if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE) 22348b25f351SJames Smart goto busy; 223561bff8efSJames Smart 2236e399441dSJames Smart if (!nvme_fc_ctrl_get(ctrl)) 2237fc17b653SChristoph Hellwig return BLK_STS_IOERR; 2238e399441dSJames Smart 2239e399441dSJames Smart /* format the FC-NVME CMD IU and fcp_req */ 2240e399441dSJames Smart cmdiu->connection_id = cpu_to_be64(queue->connection_id); 2241e399441dSJames Smart csn = atomic_inc_return(&queue->csn); 2242e399441dSJames Smart cmdiu->csn = cpu_to_be32(csn); 2243e399441dSJames Smart cmdiu->data_len = cpu_to_be32(data_len); 2244e399441dSJames Smart switch (io_dir) { 2245e399441dSJames Smart case NVMEFC_FCP_WRITE: 2246e399441dSJames Smart cmdiu->flags = FCNVME_CMD_FLAGS_WRITE; 2247e399441dSJames Smart break; 2248e399441dSJames Smart case NVMEFC_FCP_READ: 2249e399441dSJames Smart cmdiu->flags = FCNVME_CMD_FLAGS_READ; 2250e399441dSJames Smart break; 2251e399441dSJames Smart case NVMEFC_FCP_NODATA: 2252e399441dSJames Smart cmdiu->flags = 0; 2253e399441dSJames Smart break; 2254e399441dSJames Smart } 2255e399441dSJames Smart op->fcp_req.payload_length = data_len; 2256e399441dSJames Smart op->fcp_req.io_dir = io_dir; 2257e399441dSJames Smart op->fcp_req.transferred_length = 0; 2258e399441dSJames Smart op->fcp_req.rcv_rsplen = 0; 225962eeacb0SJames Smart op->fcp_req.status = NVME_SC_SUCCESS; 2260e399441dSJames Smart op->fcp_req.sqid = cpu_to_le16(queue->qnum); 2261e399441dSJames Smart 2262e399441dSJames Smart /* 2263e399441dSJames Smart * validate per fabric rules, set fields mandated by fabric spec 2264e399441dSJames Smart * as well as those by FC-NVME spec. 2265e399441dSJames Smart */ 2266e399441dSJames Smart WARN_ON_ONCE(sqe->common.metadata); 2267e399441dSJames Smart sqe->common.flags |= NVME_CMD_SGL_METABUF; 2268e399441dSJames Smart 2269e399441dSJames Smart /* 2270d9d34c0bSJames Smart * format SQE DPTR field per FC-NVME rules: 2271d9d34c0bSJames Smart * type=0x5 Transport SGL Data Block Descriptor 2272d9d34c0bSJames Smart * subtype=0xA Transport-specific value 2273d9d34c0bSJames Smart * address=0 2274d9d34c0bSJames Smart * length=length of the data series 2275e399441dSJames Smart */ 2276d9d34c0bSJames Smart sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) | 2277d9d34c0bSJames Smart NVME_SGL_FMT_TRANSPORT_A; 2278e399441dSJames Smart sqe->rw.dptr.sgl.length = cpu_to_le32(data_len); 2279e399441dSJames Smart sqe->rw.dptr.sgl.addr = 0; 2280e399441dSJames Smart 228178a7ac26SJames Smart if (!(op->flags & FCOP_FLAGS_AEN)) { 2282e399441dSJames Smart ret = nvme_fc_map_data(ctrl, op->rq, op); 2283e399441dSJames Smart if (ret < 0) { 2284e399441dSJames Smart nvme_cleanup_cmd(op->rq); 2285e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 2286fc17b653SChristoph Hellwig if (ret == -ENOMEM || ret == -EAGAIN) 2287fc17b653SChristoph Hellwig return BLK_STS_RESOURCE; 2288fc17b653SChristoph Hellwig return BLK_STS_IOERR; 2289e399441dSJames Smart } 2290e399441dSJames Smart } 2291e399441dSJames Smart 2292e399441dSJames Smart fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma, 2293e399441dSJames Smart sizeof(op->cmd_iu), DMA_TO_DEVICE); 2294e399441dSJames Smart 2295e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_ACTIVE); 2296e399441dSJames Smart 229778a7ac26SJames Smart if (!(op->flags & FCOP_FLAGS_AEN)) 2298e399441dSJames Smart blk_mq_start_request(op->rq); 2299e399441dSJames Smart 2300e399441dSJames Smart ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport, 2301e399441dSJames Smart &ctrl->rport->remoteport, 2302e399441dSJames Smart queue->lldd_handle, &op->fcp_req); 2303e399441dSJames Smart 2304e399441dSJames Smart if (ret) { 23058b25f351SJames Smart if (!(op->flags & FCOP_FLAGS_AEN)) 2306e399441dSJames Smart nvme_fc_unmap_data(ctrl, op->rq, op); 2307e399441dSJames Smart 2308e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 2309e399441dSJames Smart 23108b25f351SJames Smart if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE && 23118b25f351SJames Smart ret != -EBUSY) 2312fc17b653SChristoph Hellwig return BLK_STS_IOERR; 2313e399441dSJames Smart 23148b25f351SJames Smart goto busy; 2315e399441dSJames Smart } 2316e399441dSJames Smart 2317fc17b653SChristoph Hellwig return BLK_STS_OK; 23188b25f351SJames Smart 23198b25f351SJames Smart busy: 23208b25f351SJames Smart if (!(op->flags & FCOP_FLAGS_AEN) && queue->hctx) 23218b25f351SJames Smart blk_mq_delay_run_hw_queue(queue->hctx, NVMEFC_QUEUE_DELAY); 23228b25f351SJames Smart 23238b25f351SJames Smart return BLK_STS_RESOURCE; 2324e399441dSJames Smart } 2325e399441dSJames Smart 23269e0ed16aSSagi Grimberg static inline blk_status_t nvme_fc_is_ready(struct nvme_fc_queue *queue, 23279e0ed16aSSagi Grimberg struct request *rq) 23289e0ed16aSSagi Grimberg { 23299e0ed16aSSagi Grimberg if (unlikely(!test_bit(NVME_FC_Q_LIVE, &queue->flags))) 23309e0ed16aSSagi Grimberg return nvmf_check_init_req(&queue->ctrl->ctrl, rq); 23319e0ed16aSSagi Grimberg return BLK_STS_OK; 23329e0ed16aSSagi Grimberg } 23339e0ed16aSSagi Grimberg 2334fc17b653SChristoph Hellwig static blk_status_t 2335e399441dSJames Smart nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx, 2336e399441dSJames Smart const struct blk_mq_queue_data *bd) 2337e399441dSJames Smart { 2338e399441dSJames Smart struct nvme_ns *ns = hctx->queue->queuedata; 2339e399441dSJames Smart struct nvme_fc_queue *queue = hctx->driver_data; 2340e399441dSJames Smart struct nvme_fc_ctrl *ctrl = queue->ctrl; 2341e399441dSJames Smart struct request *rq = bd->rq; 2342e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 2343e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 2344e399441dSJames Smart struct nvme_command *sqe = &cmdiu->sqe; 2345e399441dSJames Smart enum nvmefc_fcp_datadir io_dir; 2346e399441dSJames Smart u32 data_len; 2347fc17b653SChristoph Hellwig blk_status_t ret; 2348e399441dSJames Smart 23499e0ed16aSSagi Grimberg ret = nvme_fc_is_ready(queue, rq); 23509e0ed16aSSagi Grimberg if (unlikely(ret)) 23519e0ed16aSSagi Grimberg return ret; 23529e0ed16aSSagi Grimberg 2353e399441dSJames Smart ret = nvme_setup_cmd(ns, rq, sqe); 2354e399441dSJames Smart if (ret) 2355e399441dSJames Smart return ret; 2356e399441dSJames Smart 2357b131c61dSChristoph Hellwig data_len = blk_rq_payload_bytes(rq); 2358e399441dSJames Smart if (data_len) 2359e399441dSJames Smart io_dir = ((rq_data_dir(rq) == WRITE) ? 2360e399441dSJames Smart NVMEFC_FCP_WRITE : NVMEFC_FCP_READ); 2361e399441dSJames Smart else 2362e399441dSJames Smart io_dir = NVMEFC_FCP_NODATA; 2363e399441dSJames Smart 2364e399441dSJames Smart return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir); 2365e399441dSJames Smart } 2366e399441dSJames Smart 2367e399441dSJames Smart static struct blk_mq_tags * 2368e399441dSJames Smart nvme_fc_tagset(struct nvme_fc_queue *queue) 2369e399441dSJames Smart { 2370e399441dSJames Smart if (queue->qnum == 0) 2371e399441dSJames Smart return queue->ctrl->admin_tag_set.tags[queue->qnum]; 2372e399441dSJames Smart 2373e399441dSJames Smart return queue->ctrl->tag_set.tags[queue->qnum - 1]; 2374e399441dSJames Smart } 2375e399441dSJames Smart 2376e399441dSJames Smart static int 2377e399441dSJames Smart nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag) 2378e399441dSJames Smart 2379e399441dSJames Smart { 2380e399441dSJames Smart struct nvme_fc_queue *queue = hctx->driver_data; 2381e399441dSJames Smart struct nvme_fc_ctrl *ctrl = queue->ctrl; 2382e399441dSJames Smart struct request *req; 2383e399441dSJames Smart struct nvme_fc_fcp_op *op; 2384e399441dSJames Smart 2385e399441dSJames Smart req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag); 238661bff8efSJames Smart if (!req) 2387e399441dSJames Smart return 0; 2388e399441dSJames Smart 2389e399441dSJames Smart op = blk_mq_rq_to_pdu(req); 2390e399441dSJames Smart 2391e399441dSJames Smart if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) && 2392e399441dSJames Smart (ctrl->lport->ops->poll_queue)) 2393e399441dSJames Smart ctrl->lport->ops->poll_queue(&ctrl->lport->localport, 2394e399441dSJames Smart queue->lldd_handle); 2395e399441dSJames Smart 2396e399441dSJames Smart return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE)); 2397e399441dSJames Smart } 2398e399441dSJames Smart 2399e399441dSJames Smart static void 2400ad22c355SKeith Busch nvme_fc_submit_async_event(struct nvme_ctrl *arg) 2401e399441dSJames Smart { 2402e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg); 2403e399441dSJames Smart struct nvme_fc_fcp_op *aen_op; 240461bff8efSJames Smart unsigned long flags; 240561bff8efSJames Smart bool terminating = false; 2406fc17b653SChristoph Hellwig blk_status_t ret; 2407e399441dSJames Smart 240861bff8efSJames Smart spin_lock_irqsave(&ctrl->lock, flags); 240961bff8efSJames Smart if (ctrl->flags & FCCTRL_TERMIO) 241061bff8efSJames Smart terminating = true; 241161bff8efSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 241261bff8efSJames Smart 241361bff8efSJames Smart if (terminating) 241461bff8efSJames Smart return; 241561bff8efSJames Smart 2416ad22c355SKeith Busch aen_op = &ctrl->aen_ops[0]; 2417e399441dSJames Smart 2418e399441dSJames Smart ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0, 2419e399441dSJames Smart NVMEFC_FCP_NODATA); 2420e399441dSJames Smart if (ret) 2421e399441dSJames Smart dev_err(ctrl->ctrl.device, 2422ad22c355SKeith Busch "failed async event work\n"); 2423e399441dSJames Smart } 2424e399441dSJames Smart 2425e399441dSJames Smart static void 242678a7ac26SJames Smart __nvme_fc_final_op_cleanup(struct request *rq) 2427e399441dSJames Smart { 2428e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 2429e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl; 2430e399441dSJames Smart 243178a7ac26SJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE); 243278a7ac26SJames Smart op->flags &= ~(FCOP_FLAGS_TERMIO | FCOP_FLAGS_RELEASED | 243378a7ac26SJames Smart FCOP_FLAGS_COMPLETE); 2434e399441dSJames Smart 2435e399441dSJames Smart nvme_fc_unmap_data(ctrl, rq, op); 243677f02a7aSChristoph Hellwig nvme_complete_rq(rq); 2437e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 2438e399441dSJames Smart 2439e399441dSJames Smart } 2440e399441dSJames Smart 244178a7ac26SJames Smart static void 244278a7ac26SJames Smart nvme_fc_complete_rq(struct request *rq) 244378a7ac26SJames Smart { 244478a7ac26SJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 244578a7ac26SJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl; 244678a7ac26SJames Smart unsigned long flags; 244778a7ac26SJames Smart bool completed = false; 244878a7ac26SJames Smart 244978a7ac26SJames Smart /* 245078a7ac26SJames Smart * the core layer, on controller resets after calling 245178a7ac26SJames Smart * nvme_shutdown_ctrl(), calls complete_rq without our 245278a7ac26SJames Smart * calling blk_mq_complete_request(), thus there may still 245378a7ac26SJames Smart * be live i/o outstanding with the LLDD. Means transport has 245478a7ac26SJames Smart * to track complete calls vs fcpio_done calls to know what 245578a7ac26SJames Smart * path to take on completes and dones. 245678a7ac26SJames Smart */ 245778a7ac26SJames Smart spin_lock_irqsave(&ctrl->lock, flags); 245878a7ac26SJames Smart if (op->flags & FCOP_FLAGS_COMPLETE) 245978a7ac26SJames Smart completed = true; 246078a7ac26SJames Smart else 246178a7ac26SJames Smart op->flags |= FCOP_FLAGS_RELEASED; 246278a7ac26SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 246378a7ac26SJames Smart 246478a7ac26SJames Smart if (completed) 246578a7ac26SJames Smart __nvme_fc_final_op_cleanup(rq); 246678a7ac26SJames Smart } 246778a7ac26SJames Smart 2468e399441dSJames Smart /* 2469e399441dSJames Smart * This routine is used by the transport when it needs to find active 2470e399441dSJames Smart * io on a queue that is to be terminated. The transport uses 2471e399441dSJames Smart * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke 2472e399441dSJames Smart * this routine to kill them on a 1 by 1 basis. 2473e399441dSJames Smart * 2474e399441dSJames Smart * As FC allocates FC exchange for each io, the transport must contact 2475e399441dSJames Smart * the LLDD to terminate the exchange, thus releasing the FC exchange. 2476e399441dSJames Smart * After terminating the exchange the LLDD will call the transport's 2477e399441dSJames Smart * normal io done path for the request, but it will have an aborted 2478e399441dSJames Smart * status. The done path will return the io request back to the block 2479e399441dSJames Smart * layer with an error status. 2480e399441dSJames Smart */ 2481e399441dSJames Smart static void 2482e399441dSJames Smart nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved) 2483e399441dSJames Smart { 2484e399441dSJames Smart struct nvme_ctrl *nctrl = data; 2485e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 2486e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req); 248778a7ac26SJames Smart unsigned long flags; 2488e399441dSJames Smart int status; 2489e399441dSJames Smart 2490e399441dSJames Smart if (!blk_mq_request_started(req)) 2491e399441dSJames Smart return; 2492e399441dSJames Smart 249378a7ac26SJames Smart spin_lock_irqsave(&ctrl->lock, flags); 249461bff8efSJames Smart if (ctrl->flags & FCCTRL_TERMIO) { 249561bff8efSJames Smart ctrl->iocnt++; 249678a7ac26SJames Smart op->flags |= FCOP_FLAGS_TERMIO; 249761bff8efSJames Smart } 249878a7ac26SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 249978a7ac26SJames Smart 2500e399441dSJames Smart status = __nvme_fc_abort_op(ctrl, op); 250178a7ac26SJames Smart if (status) { 2502e399441dSJames Smart /* 250378a7ac26SJames Smart * if __nvme_fc_abort_op failed the io wasn't 250478a7ac26SJames Smart * active. Thus this call path is running in 250578a7ac26SJames Smart * parallel to the io complete. Treat as non-error. 2506e399441dSJames Smart */ 250778a7ac26SJames Smart 250878a7ac26SJames Smart /* back out the flags/counters */ 250978a7ac26SJames Smart spin_lock_irqsave(&ctrl->lock, flags); 251061bff8efSJames Smart if (ctrl->flags & FCCTRL_TERMIO) 251161bff8efSJames Smart ctrl->iocnt--; 251278a7ac26SJames Smart op->flags &= ~FCOP_FLAGS_TERMIO; 251378a7ac26SJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 2514e399441dSJames Smart return; 2515e399441dSJames Smart } 251678a7ac26SJames Smart } 2517e399441dSJames Smart 2518e399441dSJames Smart 251961bff8efSJames Smart static const struct blk_mq_ops nvme_fc_mq_ops = { 252061bff8efSJames Smart .queue_rq = nvme_fc_queue_rq, 252161bff8efSJames Smart .complete = nvme_fc_complete_rq, 252261bff8efSJames Smart .init_request = nvme_fc_init_request, 252361bff8efSJames Smart .exit_request = nvme_fc_exit_request, 252461bff8efSJames Smart .init_hctx = nvme_fc_init_hctx, 252561bff8efSJames Smart .poll = nvme_fc_poll, 252661bff8efSJames Smart .timeout = nvme_fc_timeout, 2527e399441dSJames Smart }; 2528e399441dSJames Smart 2529e399441dSJames Smart static int 2530e399441dSJames Smart nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl) 2531e399441dSJames Smart { 2532e399441dSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 25337314183dSSagi Grimberg unsigned int nr_io_queues; 2534e399441dSJames Smart int ret; 2535e399441dSJames Smart 25367314183dSSagi Grimberg nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()), 25377314183dSSagi Grimberg ctrl->lport->ops->max_hw_queues); 25387314183dSSagi Grimberg ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); 2539e399441dSJames Smart if (ret) { 2540e399441dSJames Smart dev_info(ctrl->ctrl.device, 2541e399441dSJames Smart "set_queue_count failed: %d\n", ret); 2542e399441dSJames Smart return ret; 2543e399441dSJames Smart } 2544e399441dSJames Smart 25457314183dSSagi Grimberg ctrl->ctrl.queue_count = nr_io_queues + 1; 25467314183dSSagi Grimberg if (!nr_io_queues) 2547e399441dSJames Smart return 0; 2548e399441dSJames Smart 2549e399441dSJames Smart nvme_fc_init_io_queues(ctrl); 2550e399441dSJames Smart 2551e399441dSJames Smart memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); 2552e399441dSJames Smart ctrl->tag_set.ops = &nvme_fc_mq_ops; 2553e399441dSJames Smart ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; 2554e399441dSJames Smart ctrl->tag_set.reserved_tags = 1; /* fabric connect */ 2555e399441dSJames Smart ctrl->tag_set.numa_node = NUMA_NO_NODE; 2556e399441dSJames Smart ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 2557e399441dSJames Smart ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) + 2558e399441dSJames Smart (SG_CHUNK_SIZE * 2559e399441dSJames Smart sizeof(struct scatterlist)) + 2560e399441dSJames Smart ctrl->lport->ops->fcprqst_priv_sz; 2561e399441dSJames Smart ctrl->tag_set.driver_data = ctrl; 2562d858e5f0SSagi Grimberg ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1; 2563e399441dSJames Smart ctrl->tag_set.timeout = NVME_IO_TIMEOUT; 2564e399441dSJames Smart 2565e399441dSJames Smart ret = blk_mq_alloc_tag_set(&ctrl->tag_set); 2566e399441dSJames Smart if (ret) 2567e399441dSJames Smart return ret; 2568e399441dSJames Smart 2569e399441dSJames Smart ctrl->ctrl.tagset = &ctrl->tag_set; 2570e399441dSJames Smart 2571e399441dSJames Smart ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); 2572e399441dSJames Smart if (IS_ERR(ctrl->ctrl.connect_q)) { 2573e399441dSJames Smart ret = PTR_ERR(ctrl->ctrl.connect_q); 2574e399441dSJames Smart goto out_free_tag_set; 2575e399441dSJames Smart } 2576e399441dSJames Smart 2577e399441dSJames Smart ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size); 2578e399441dSJames Smart if (ret) 2579e399441dSJames Smart goto out_cleanup_blk_queue; 2580e399441dSJames Smart 2581e399441dSJames Smart ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size); 2582e399441dSJames Smart if (ret) 2583e399441dSJames Smart goto out_delete_hw_queues; 2584e399441dSJames Smart 2585e399441dSJames Smart return 0; 2586e399441dSJames Smart 2587e399441dSJames Smart out_delete_hw_queues: 2588e399441dSJames Smart nvme_fc_delete_hw_io_queues(ctrl); 2589e399441dSJames Smart out_cleanup_blk_queue: 2590e399441dSJames Smart blk_cleanup_queue(ctrl->ctrl.connect_q); 2591e399441dSJames Smart out_free_tag_set: 2592e399441dSJames Smart blk_mq_free_tag_set(&ctrl->tag_set); 2593e399441dSJames Smart nvme_fc_free_io_queues(ctrl); 2594e399441dSJames Smart 2595e399441dSJames Smart /* force put free routine to ignore io queues */ 2596e399441dSJames Smart ctrl->ctrl.tagset = NULL; 2597e399441dSJames Smart 2598e399441dSJames Smart return ret; 2599e399441dSJames Smart } 2600e399441dSJames Smart 260161bff8efSJames Smart static int 260261bff8efSJames Smart nvme_fc_reinit_io_queues(struct nvme_fc_ctrl *ctrl) 260361bff8efSJames Smart { 260461bff8efSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 26057314183dSSagi Grimberg unsigned int nr_io_queues; 260661bff8efSJames Smart int ret; 260761bff8efSJames Smart 26087314183dSSagi Grimberg nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()), 26097314183dSSagi Grimberg ctrl->lport->ops->max_hw_queues); 26107314183dSSagi Grimberg ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); 261161bff8efSJames Smart if (ret) { 261261bff8efSJames Smart dev_info(ctrl->ctrl.device, 261361bff8efSJames Smart "set_queue_count failed: %d\n", ret); 261461bff8efSJames Smart return ret; 261561bff8efSJames Smart } 261661bff8efSJames Smart 26177314183dSSagi Grimberg ctrl->ctrl.queue_count = nr_io_queues + 1; 261861bff8efSJames Smart /* check for io queues existing */ 2619d858e5f0SSagi Grimberg if (ctrl->ctrl.queue_count == 1) 262061bff8efSJames Smart return 0; 262161bff8efSJames Smart 262261bff8efSJames Smart nvme_fc_init_io_queues(ctrl); 262361bff8efSJames Smart 262431b84460SSagi Grimberg ret = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.tagset); 262561bff8efSJames Smart if (ret) 262661bff8efSJames Smart goto out_free_io_queues; 262761bff8efSJames Smart 262861bff8efSJames Smart ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size); 262961bff8efSJames Smart if (ret) 263061bff8efSJames Smart goto out_free_io_queues; 263161bff8efSJames Smart 263261bff8efSJames Smart ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size); 263361bff8efSJames Smart if (ret) 263461bff8efSJames Smart goto out_delete_hw_queues; 263561bff8efSJames Smart 2636cda5fd1aSSagi Grimberg blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues); 2637cda5fd1aSSagi Grimberg 263861bff8efSJames Smart return 0; 263961bff8efSJames Smart 264061bff8efSJames Smart out_delete_hw_queues: 264161bff8efSJames Smart nvme_fc_delete_hw_io_queues(ctrl); 264261bff8efSJames Smart out_free_io_queues: 264361bff8efSJames Smart nvme_fc_free_io_queues(ctrl); 264461bff8efSJames Smart return ret; 264561bff8efSJames Smart } 264661bff8efSJames Smart 2647158bfb88SJames Smart static void 2648158bfb88SJames Smart nvme_fc_rport_active_on_lport(struct nvme_fc_rport *rport) 2649158bfb88SJames Smart { 2650158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport; 2651158bfb88SJames Smart 2652158bfb88SJames Smart atomic_inc(&lport->act_rport_cnt); 2653158bfb88SJames Smart } 2654158bfb88SJames Smart 2655158bfb88SJames Smart static void 2656158bfb88SJames Smart nvme_fc_rport_inactive_on_lport(struct nvme_fc_rport *rport) 2657158bfb88SJames Smart { 2658158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport; 2659158bfb88SJames Smart u32 cnt; 2660158bfb88SJames Smart 2661158bfb88SJames Smart cnt = atomic_dec_return(&lport->act_rport_cnt); 2662158bfb88SJames Smart if (cnt == 0 && lport->localport.port_state == FC_OBJSTATE_DELETED) 2663158bfb88SJames Smart lport->ops->localport_delete(&lport->localport); 2664158bfb88SJames Smart } 2665158bfb88SJames Smart 2666158bfb88SJames Smart static int 2667158bfb88SJames Smart nvme_fc_ctlr_active_on_rport(struct nvme_fc_ctrl *ctrl) 2668158bfb88SJames Smart { 2669158bfb88SJames Smart struct nvme_fc_rport *rport = ctrl->rport; 2670158bfb88SJames Smart u32 cnt; 2671158bfb88SJames Smart 2672158bfb88SJames Smart if (ctrl->assoc_active) 2673158bfb88SJames Smart return 1; 2674158bfb88SJames Smart 2675158bfb88SJames Smart ctrl->assoc_active = true; 2676158bfb88SJames Smart cnt = atomic_inc_return(&rport->act_ctrl_cnt); 2677158bfb88SJames Smart if (cnt == 1) 2678158bfb88SJames Smart nvme_fc_rport_active_on_lport(rport); 2679158bfb88SJames Smart 2680158bfb88SJames Smart return 0; 2681158bfb88SJames Smart } 2682158bfb88SJames Smart 2683158bfb88SJames Smart static int 2684158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(struct nvme_fc_ctrl *ctrl) 2685158bfb88SJames Smart { 2686158bfb88SJames Smart struct nvme_fc_rport *rport = ctrl->rport; 2687158bfb88SJames Smart struct nvme_fc_lport *lport = rport->lport; 2688158bfb88SJames Smart u32 cnt; 2689158bfb88SJames Smart 2690158bfb88SJames Smart /* ctrl->assoc_active=false will be set independently */ 2691158bfb88SJames Smart 2692158bfb88SJames Smart cnt = atomic_dec_return(&rport->act_ctrl_cnt); 2693158bfb88SJames Smart if (cnt == 0) { 2694158bfb88SJames Smart if (rport->remoteport.port_state == FC_OBJSTATE_DELETED) 2695158bfb88SJames Smart lport->ops->remoteport_delete(&rport->remoteport); 2696158bfb88SJames Smart nvme_fc_rport_inactive_on_lport(rport); 2697158bfb88SJames Smart } 2698158bfb88SJames Smart 2699158bfb88SJames Smart return 0; 2700158bfb88SJames Smart } 2701158bfb88SJames Smart 270261bff8efSJames Smart /* 270361bff8efSJames Smart * This routine restarts the controller on the host side, and 270461bff8efSJames Smart * on the link side, recreates the controller association. 270561bff8efSJames Smart */ 270661bff8efSJames Smart static int 270761bff8efSJames Smart nvme_fc_create_association(struct nvme_fc_ctrl *ctrl) 270861bff8efSJames Smart { 270961bff8efSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 271061bff8efSJames Smart int ret; 271161bff8efSJames Smart bool changed; 271261bff8efSJames Smart 2713fdf9dfa8SSagi Grimberg ++ctrl->ctrl.nr_reconnects; 271461bff8efSJames Smart 271596e24801SJames Smart if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE) 271696e24801SJames Smart return -ENODEV; 271796e24801SJames Smart 2718158bfb88SJames Smart if (nvme_fc_ctlr_active_on_rport(ctrl)) 2719158bfb88SJames Smart return -ENOTUNIQ; 2720158bfb88SJames Smart 272161bff8efSJames Smart /* 272261bff8efSJames Smart * Create the admin queue 272361bff8efSJames Smart */ 272461bff8efSJames Smart 272508e15075SKeith Busch nvme_fc_init_queue(ctrl, 0); 272661bff8efSJames Smart 272761bff8efSJames Smart ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0, 272838dabe21SKeith Busch NVME_AQ_BLK_MQ_DEPTH); 272961bff8efSJames Smart if (ret) 273061bff8efSJames Smart goto out_free_queue; 273161bff8efSJames Smart 273261bff8efSJames Smart ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0], 273338dabe21SKeith Busch NVME_AQ_BLK_MQ_DEPTH, 273438dabe21SKeith Busch (NVME_AQ_BLK_MQ_DEPTH / 4)); 273561bff8efSJames Smart if (ret) 273661bff8efSJames Smart goto out_delete_hw_queue; 273761bff8efSJames Smart 273861bff8efSJames Smart if (ctrl->ctrl.state != NVME_CTRL_NEW) 2739f9c5af5fSSagi Grimberg blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); 274061bff8efSJames Smart 274161bff8efSJames Smart ret = nvmf_connect_admin_queue(&ctrl->ctrl); 274261bff8efSJames Smart if (ret) 274361bff8efSJames Smart goto out_disconnect_admin_queue; 274461bff8efSJames Smart 27459e0ed16aSSagi Grimberg set_bit(NVME_FC_Q_LIVE, &ctrl->queues[0].flags); 27469e0ed16aSSagi Grimberg 274761bff8efSJames Smart /* 274861bff8efSJames Smart * Check controller capabilities 274961bff8efSJames Smart * 275061bff8efSJames Smart * todo:- add code to check if ctrl attributes changed from 275161bff8efSJames Smart * prior connection values 275261bff8efSJames Smart */ 275361bff8efSJames Smart 275420d0dfe6SSagi Grimberg ret = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap); 275561bff8efSJames Smart if (ret) { 275661bff8efSJames Smart dev_err(ctrl->ctrl.device, 275761bff8efSJames Smart "prop_get NVME_REG_CAP failed\n"); 275861bff8efSJames Smart goto out_disconnect_admin_queue; 275961bff8efSJames Smart } 276061bff8efSJames Smart 276161bff8efSJames Smart ctrl->ctrl.sqsize = 276220d0dfe6SSagi Grimberg min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap) + 1, ctrl->ctrl.sqsize); 276361bff8efSJames Smart 276420d0dfe6SSagi Grimberg ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap); 276561bff8efSJames Smart if (ret) 276661bff8efSJames Smart goto out_disconnect_admin_queue; 276761bff8efSJames Smart 2768ecad0d2cSJames Smart ctrl->ctrl.max_hw_sectors = 2769ecad0d2cSJames Smart (ctrl->lport->ops->max_sgl_segments - 1) << (PAGE_SHIFT - 9); 277061bff8efSJames Smart 277161bff8efSJames Smart ret = nvme_init_identify(&ctrl->ctrl); 277261bff8efSJames Smart if (ret) 277361bff8efSJames Smart goto out_disconnect_admin_queue; 277461bff8efSJames Smart 277561bff8efSJames Smart /* sanity checks */ 277661bff8efSJames Smart 277761bff8efSJames Smart /* FC-NVME does not have other data in the capsule */ 277861bff8efSJames Smart if (ctrl->ctrl.icdoff) { 277961bff8efSJames Smart dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n", 278061bff8efSJames Smart ctrl->ctrl.icdoff); 278161bff8efSJames Smart goto out_disconnect_admin_queue; 278261bff8efSJames Smart } 278361bff8efSJames Smart 278461bff8efSJames Smart /* FC-NVME supports normal SGL Data Block Descriptors */ 278561bff8efSJames Smart 278661bff8efSJames Smart if (opts->queue_size > ctrl->ctrl.maxcmd) { 278761bff8efSJames Smart /* warn if maxcmd is lower than queue_size */ 278861bff8efSJames Smart dev_warn(ctrl->ctrl.device, 278961bff8efSJames Smart "queue_size %zu > ctrl maxcmd %u, reducing " 279061bff8efSJames Smart "to queue_size\n", 279161bff8efSJames Smart opts->queue_size, ctrl->ctrl.maxcmd); 279261bff8efSJames Smart opts->queue_size = ctrl->ctrl.maxcmd; 279361bff8efSJames Smart } 279461bff8efSJames Smart 279561bff8efSJames Smart ret = nvme_fc_init_aen_ops(ctrl); 279661bff8efSJames Smart if (ret) 279761bff8efSJames Smart goto out_term_aen_ops; 279861bff8efSJames Smart 279961bff8efSJames Smart /* 280061bff8efSJames Smart * Create the io queues 280161bff8efSJames Smart */ 280261bff8efSJames Smart 2803d858e5f0SSagi Grimberg if (ctrl->ctrl.queue_count > 1) { 280461bff8efSJames Smart if (ctrl->ctrl.state == NVME_CTRL_NEW) 280561bff8efSJames Smart ret = nvme_fc_create_io_queues(ctrl); 280661bff8efSJames Smart else 280761bff8efSJames Smart ret = nvme_fc_reinit_io_queues(ctrl); 280861bff8efSJames Smart if (ret) 280961bff8efSJames Smart goto out_term_aen_ops; 281061bff8efSJames Smart } 281161bff8efSJames Smart 281261bff8efSJames Smart changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 281361bff8efSJames Smart 2814fdf9dfa8SSagi Grimberg ctrl->ctrl.nr_reconnects = 0; 281561bff8efSJames Smart 281644c6ec77SJames Smart if (changed) 2817d09f2b45SSagi Grimberg nvme_start_ctrl(&ctrl->ctrl); 281861bff8efSJames Smart 281961bff8efSJames Smart return 0; /* Success */ 282061bff8efSJames Smart 282161bff8efSJames Smart out_term_aen_ops: 282261bff8efSJames Smart nvme_fc_term_aen_ops(ctrl); 282361bff8efSJames Smart out_disconnect_admin_queue: 282461bff8efSJames Smart /* send a Disconnect(association) LS to fc-nvme target */ 282561bff8efSJames Smart nvme_fc_xmt_disconnect_assoc(ctrl); 282661bff8efSJames Smart out_delete_hw_queue: 282761bff8efSJames Smart __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0); 282861bff8efSJames Smart out_free_queue: 282961bff8efSJames Smart nvme_fc_free_queue(&ctrl->queues[0]); 2830158bfb88SJames Smart ctrl->assoc_active = false; 2831158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(ctrl); 283261bff8efSJames Smart 283361bff8efSJames Smart return ret; 283461bff8efSJames Smart } 283561bff8efSJames Smart 283661bff8efSJames Smart /* 283761bff8efSJames Smart * This routine stops operation of the controller on the host side. 283861bff8efSJames Smart * On the host os stack side: Admin and IO queues are stopped, 283961bff8efSJames Smart * outstanding ios on them terminated via FC ABTS. 284061bff8efSJames Smart * On the link side: the association is terminated. 284161bff8efSJames Smart */ 284261bff8efSJames Smart static void 284361bff8efSJames Smart nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl) 284461bff8efSJames Smart { 284561bff8efSJames Smart unsigned long flags; 284661bff8efSJames Smart 2847158bfb88SJames Smart if (!ctrl->assoc_active) 2848158bfb88SJames Smart return; 2849158bfb88SJames Smart ctrl->assoc_active = false; 2850158bfb88SJames Smart 285161bff8efSJames Smart spin_lock_irqsave(&ctrl->lock, flags); 285261bff8efSJames Smart ctrl->flags |= FCCTRL_TERMIO; 285361bff8efSJames Smart ctrl->iocnt = 0; 285461bff8efSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 285561bff8efSJames Smart 285661bff8efSJames Smart /* 285761bff8efSJames Smart * If io queues are present, stop them and terminate all outstanding 285861bff8efSJames Smart * ios on them. As FC allocates FC exchange for each io, the 285961bff8efSJames Smart * transport must contact the LLDD to terminate the exchange, 286061bff8efSJames Smart * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr() 286161bff8efSJames Smart * to tell us what io's are busy and invoke a transport routine 286261bff8efSJames Smart * to kill them with the LLDD. After terminating the exchange 286361bff8efSJames Smart * the LLDD will call the transport's normal io done path, but it 286461bff8efSJames Smart * will have an aborted status. The done path will return the 286561bff8efSJames Smart * io requests back to the block layer as part of normal completions 286661bff8efSJames Smart * (but with error status). 286761bff8efSJames Smart */ 2868d858e5f0SSagi Grimberg if (ctrl->ctrl.queue_count > 1) { 286961bff8efSJames Smart nvme_stop_queues(&ctrl->ctrl); 287061bff8efSJames Smart blk_mq_tagset_busy_iter(&ctrl->tag_set, 287161bff8efSJames Smart nvme_fc_terminate_exchange, &ctrl->ctrl); 287261bff8efSJames Smart } 287361bff8efSJames Smart 287461bff8efSJames Smart /* 287561bff8efSJames Smart * Other transports, which don't have link-level contexts bound 287661bff8efSJames Smart * to sqe's, would try to gracefully shutdown the controller by 287761bff8efSJames Smart * writing the registers for shutdown and polling (call 287861bff8efSJames Smart * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially 287961bff8efSJames Smart * just aborted and we will wait on those contexts, and given 288061bff8efSJames Smart * there was no indication of how live the controlelr is on the 288161bff8efSJames Smart * link, don't send more io to create more contexts for the 288261bff8efSJames Smart * shutdown. Let the controller fail via keepalive failure if 288361bff8efSJames Smart * its still present. 288461bff8efSJames Smart */ 288561bff8efSJames Smart 288661bff8efSJames Smart /* 288761bff8efSJames Smart * clean up the admin queue. Same thing as above. 288861bff8efSJames Smart * use blk_mq_tagset_busy_itr() and the transport routine to 288961bff8efSJames Smart * terminate the exchanges. 289061bff8efSJames Smart */ 289144c6ec77SJames Smart if (ctrl->ctrl.state != NVME_CTRL_NEW) 2892f9c5af5fSSagi Grimberg blk_mq_quiesce_queue(ctrl->ctrl.admin_q); 289361bff8efSJames Smart blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, 289461bff8efSJames Smart nvme_fc_terminate_exchange, &ctrl->ctrl); 289561bff8efSJames Smart 289661bff8efSJames Smart /* kill the aens as they are a separate path */ 289761bff8efSJames Smart nvme_fc_abort_aen_ops(ctrl); 289861bff8efSJames Smart 289961bff8efSJames Smart /* wait for all io that had to be aborted */ 29008a82dbf1SJames Smart spin_lock_irq(&ctrl->lock); 290136715cf4SJames Smart wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock); 290261bff8efSJames Smart ctrl->flags &= ~FCCTRL_TERMIO; 29038a82dbf1SJames Smart spin_unlock_irq(&ctrl->lock); 290461bff8efSJames Smart 290561bff8efSJames Smart nvme_fc_term_aen_ops(ctrl); 290661bff8efSJames Smart 290761bff8efSJames Smart /* 290861bff8efSJames Smart * send a Disconnect(association) LS to fc-nvme target 290961bff8efSJames Smart * Note: could have been sent at top of process, but 291061bff8efSJames Smart * cleaner on link traffic if after the aborts complete. 291161bff8efSJames Smart * Note: if association doesn't exist, association_id will be 0 291261bff8efSJames Smart */ 291361bff8efSJames Smart if (ctrl->association_id) 291461bff8efSJames Smart nvme_fc_xmt_disconnect_assoc(ctrl); 291561bff8efSJames Smart 291661bff8efSJames Smart if (ctrl->ctrl.tagset) { 291761bff8efSJames Smart nvme_fc_delete_hw_io_queues(ctrl); 291861bff8efSJames Smart nvme_fc_free_io_queues(ctrl); 291961bff8efSJames Smart } 292061bff8efSJames Smart 292161bff8efSJames Smart __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0); 292261bff8efSJames Smart nvme_fc_free_queue(&ctrl->queues[0]); 2923158bfb88SJames Smart 2924d625d05eSJames Smart /* re-enable the admin_q so anything new can fast fail */ 2925d625d05eSJames Smart blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); 2926d625d05eSJames Smart 2927158bfb88SJames Smart nvme_fc_ctlr_inactive_on_rport(ctrl); 292861bff8efSJames Smart } 292961bff8efSJames Smart 293061bff8efSJames Smart static void 2931c5017e85SChristoph Hellwig nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl) 293261bff8efSJames Smart { 2933c5017e85SChristoph Hellwig struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 293461bff8efSJames Smart 293561bff8efSJames Smart cancel_delayed_work_sync(&ctrl->connect_work); 293661bff8efSJames Smart /* 293761bff8efSJames Smart * kill the association on the link side. this will block 293861bff8efSJames Smart * waiting for io to terminate 293961bff8efSJames Smart */ 294061bff8efSJames Smart nvme_fc_delete_association(ctrl); 294161bff8efSJames Smart } 294261bff8efSJames Smart 294361bff8efSJames Smart static void 29445bbecdbcSJames Smart nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status) 29455bbecdbcSJames Smart { 29462b632970SJames Smart struct nvme_fc_rport *rport = ctrl->rport; 29472b632970SJames Smart struct nvme_fc_remote_port *portptr = &rport->remoteport; 29482b632970SJames Smart unsigned long recon_delay = ctrl->ctrl.opts->reconnect_delay * HZ; 29492b632970SJames Smart bool recon = true; 29505bbecdbcSJames Smart 295144c6ec77SJames Smart if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) 29525bbecdbcSJames Smart return; 29535bbecdbcSJames Smart 29542b632970SJames Smart if (portptr->port_state == FC_OBJSTATE_ONLINE) 2955589ff775SJames Smart dev_info(ctrl->ctrl.device, 29565bbecdbcSJames Smart "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n", 29575bbecdbcSJames Smart ctrl->cnum, status); 29582b632970SJames Smart else if (time_after_eq(jiffies, rport->dev_loss_end)) 29592b632970SJames Smart recon = false; 29605bbecdbcSJames Smart 29612b632970SJames Smart if (recon && nvmf_should_reconnect(&ctrl->ctrl)) { 29622b632970SJames Smart if (portptr->port_state == FC_OBJSTATE_ONLINE) 29635bbecdbcSJames Smart dev_info(ctrl->ctrl.device, 29642b632970SJames Smart "NVME-FC{%d}: Reconnect attempt in %ld " 29652b632970SJames Smart "seconds\n", 29662b632970SJames Smart ctrl->cnum, recon_delay / HZ); 29672b632970SJames Smart else if (time_after(jiffies + recon_delay, rport->dev_loss_end)) 29682b632970SJames Smart recon_delay = rport->dev_loss_end - jiffies; 29692b632970SJames Smart 29702b632970SJames Smart queue_delayed_work(nvme_wq, &ctrl->connect_work, recon_delay); 29715bbecdbcSJames Smart } else { 29722b632970SJames Smart if (portptr->port_state == FC_OBJSTATE_ONLINE) 2973589ff775SJames Smart dev_warn(ctrl->ctrl.device, 29745bbecdbcSJames Smart "NVME-FC{%d}: Max reconnect attempts (%d) " 29755bbecdbcSJames Smart "reached. Removing controller\n", 2976fdf9dfa8SSagi Grimberg ctrl->cnum, ctrl->ctrl.nr_reconnects); 29772b632970SJames Smart else 29782b632970SJames Smart dev_warn(ctrl->ctrl.device, 29792b632970SJames Smart "NVME-FC{%d}: dev_loss_tmo (%d) expired " 29802b632970SJames Smart "while waiting for remoteport connectivity. " 29812b632970SJames Smart "Removing controller\n", ctrl->cnum, 29822b632970SJames Smart portptr->dev_loss_tmo); 2983c5017e85SChristoph Hellwig WARN_ON(nvme_delete_ctrl(&ctrl->ctrl)); 29845bbecdbcSJames Smart } 29855bbecdbcSJames Smart } 29865bbecdbcSJames Smart 29875bbecdbcSJames Smart static void 298861bff8efSJames Smart nvme_fc_reset_ctrl_work(struct work_struct *work) 298961bff8efSJames Smart { 299061bff8efSJames Smart struct nvme_fc_ctrl *ctrl = 2991d86c4d8eSChristoph Hellwig container_of(work, struct nvme_fc_ctrl, ctrl.reset_work); 299261bff8efSJames Smart int ret; 299361bff8efSJames Smart 2994d09f2b45SSagi Grimberg nvme_stop_ctrl(&ctrl->ctrl); 299544c6ec77SJames Smart 299661bff8efSJames Smart /* will block will waiting for io to terminate */ 299761bff8efSJames Smart nvme_fc_delete_association(ctrl); 299861bff8efSJames Smart 299944c6ec77SJames Smart if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) { 300044c6ec77SJames Smart dev_err(ctrl->ctrl.device, 300144c6ec77SJames Smart "NVME-FC{%d}: error_recovery: Couldn't change state " 300244c6ec77SJames Smart "to RECONNECTING\n", ctrl->cnum); 300344c6ec77SJames Smart return; 300444c6ec77SJames Smart } 300544c6ec77SJames Smart 30062b632970SJames Smart if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) 300761bff8efSJames Smart ret = nvme_fc_create_association(ctrl); 30082b632970SJames Smart else 30092b632970SJames Smart ret = -ENOTCONN; 30102b632970SJames Smart 30115bbecdbcSJames Smart if (ret) 30125bbecdbcSJames Smart nvme_fc_reconnect_or_delete(ctrl, ret); 30135bbecdbcSJames Smart else 301461bff8efSJames Smart dev_info(ctrl->ctrl.device, 301596e24801SJames Smart "NVME-FC{%d}: controller reset complete\n", 301696e24801SJames Smart ctrl->cnum); 301761bff8efSJames Smart } 301861bff8efSJames Smart 301961bff8efSJames Smart static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = { 302061bff8efSJames Smart .name = "fc", 302161bff8efSJames Smart .module = THIS_MODULE, 3022d3d5b87dSChristoph Hellwig .flags = NVME_F_FABRICS, 302361bff8efSJames Smart .reg_read32 = nvmf_reg_read32, 302461bff8efSJames Smart .reg_read64 = nvmf_reg_read64, 302561bff8efSJames Smart .reg_write32 = nvmf_reg_write32, 302661bff8efSJames Smart .free_ctrl = nvme_fc_nvme_ctrl_freed, 302761bff8efSJames Smart .submit_async_event = nvme_fc_submit_async_event, 3028c5017e85SChristoph Hellwig .delete_ctrl = nvme_fc_delete_ctrl, 302961bff8efSJames Smart .get_address = nvmf_get_address, 303031b84460SSagi Grimberg .reinit_request = nvme_fc_reinit_request, 303161bff8efSJames Smart }; 303261bff8efSJames Smart 303361bff8efSJames Smart static void 303461bff8efSJames Smart nvme_fc_connect_ctrl_work(struct work_struct *work) 303561bff8efSJames Smart { 303661bff8efSJames Smart int ret; 303761bff8efSJames Smart 303861bff8efSJames Smart struct nvme_fc_ctrl *ctrl = 303961bff8efSJames Smart container_of(to_delayed_work(work), 304061bff8efSJames Smart struct nvme_fc_ctrl, connect_work); 304161bff8efSJames Smart 304261bff8efSJames Smart ret = nvme_fc_create_association(ctrl); 30435bbecdbcSJames Smart if (ret) 30445bbecdbcSJames Smart nvme_fc_reconnect_or_delete(ctrl, ret); 30455bbecdbcSJames Smart else 304661bff8efSJames Smart dev_info(ctrl->ctrl.device, 304761bff8efSJames Smart "NVME-FC{%d}: controller reconnect complete\n", 304861bff8efSJames Smart ctrl->cnum); 304961bff8efSJames Smart } 305061bff8efSJames Smart 305161bff8efSJames Smart 305261bff8efSJames Smart static const struct blk_mq_ops nvme_fc_admin_mq_ops = { 305361bff8efSJames Smart .queue_rq = nvme_fc_queue_rq, 305461bff8efSJames Smart .complete = nvme_fc_complete_rq, 305576f983cbSChristoph Hellwig .init_request = nvme_fc_init_request, 305661bff8efSJames Smart .exit_request = nvme_fc_exit_request, 305761bff8efSJames Smart .init_hctx = nvme_fc_init_admin_hctx, 305861bff8efSJames Smart .timeout = nvme_fc_timeout, 305961bff8efSJames Smart }; 306061bff8efSJames Smart 3061e399441dSJames Smart 306256d5f4f1SJames Smart /* 306356d5f4f1SJames Smart * Fails a controller request if it matches an existing controller 306456d5f4f1SJames Smart * (association) with the same tuple: 306556d5f4f1SJames Smart * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN> 306656d5f4f1SJames Smart * 306756d5f4f1SJames Smart * The ports don't need to be compared as they are intrinsically 306856d5f4f1SJames Smart * already matched by the port pointers supplied. 306956d5f4f1SJames Smart */ 307056d5f4f1SJames Smart static bool 307156d5f4f1SJames Smart nvme_fc_existing_controller(struct nvme_fc_rport *rport, 307256d5f4f1SJames Smart struct nvmf_ctrl_options *opts) 307356d5f4f1SJames Smart { 307456d5f4f1SJames Smart struct nvme_fc_ctrl *ctrl; 307556d5f4f1SJames Smart unsigned long flags; 307656d5f4f1SJames Smart bool found = false; 307756d5f4f1SJames Smart 307856d5f4f1SJames Smart spin_lock_irqsave(&rport->lock, flags); 307956d5f4f1SJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) { 308056d5f4f1SJames Smart found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts); 308156d5f4f1SJames Smart if (found) 308256d5f4f1SJames Smart break; 308356d5f4f1SJames Smart } 308456d5f4f1SJames Smart spin_unlock_irqrestore(&rport->lock, flags); 308556d5f4f1SJames Smart 308656d5f4f1SJames Smart return found; 308756d5f4f1SJames Smart } 308856d5f4f1SJames Smart 3089e399441dSJames Smart static struct nvme_ctrl * 309061bff8efSJames Smart nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts, 3091e399441dSJames Smart struct nvme_fc_lport *lport, struct nvme_fc_rport *rport) 3092e399441dSJames Smart { 3093e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 3094e399441dSJames Smart unsigned long flags; 309517c4dc6eSJames Smart int ret, idx, retry; 3096e399441dSJames Smart 309785e6a6adSJames Smart if (!(rport->remoteport.port_role & 309885e6a6adSJames Smart (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) { 309985e6a6adSJames Smart ret = -EBADR; 310085e6a6adSJames Smart goto out_fail; 310185e6a6adSJames Smart } 310285e6a6adSJames Smart 310356d5f4f1SJames Smart if (!opts->duplicate_connect && 310456d5f4f1SJames Smart nvme_fc_existing_controller(rport, opts)) { 310556d5f4f1SJames Smart ret = -EALREADY; 310656d5f4f1SJames Smart goto out_fail; 310756d5f4f1SJames Smart } 310856d5f4f1SJames Smart 3109e399441dSJames Smart ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 3110e399441dSJames Smart if (!ctrl) { 3111e399441dSJames Smart ret = -ENOMEM; 3112e399441dSJames Smart goto out_fail; 3113e399441dSJames Smart } 3114e399441dSJames Smart 3115e399441dSJames Smart idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL); 3116e399441dSJames Smart if (idx < 0) { 3117e399441dSJames Smart ret = -ENOSPC; 3118e399441dSJames Smart goto out_free_ctrl; 3119e399441dSJames Smart } 3120e399441dSJames Smart 3121e399441dSJames Smart ctrl->ctrl.opts = opts; 3122e399441dSJames Smart INIT_LIST_HEAD(&ctrl->ctrl_list); 3123e399441dSJames Smart ctrl->lport = lport; 3124e399441dSJames Smart ctrl->rport = rport; 3125e399441dSJames Smart ctrl->dev = lport->dev; 3126e399441dSJames Smart ctrl->cnum = idx; 3127158bfb88SJames Smart ctrl->assoc_active = false; 31288a82dbf1SJames Smart init_waitqueue_head(&ctrl->ioabort_wait); 3129e399441dSJames Smart 3130e399441dSJames Smart get_device(ctrl->dev); 3131e399441dSJames Smart kref_init(&ctrl->ref); 3132e399441dSJames Smart 3133d86c4d8eSChristoph Hellwig INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work); 313461bff8efSJames Smart INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work); 3135e399441dSJames Smart spin_lock_init(&ctrl->lock); 3136e399441dSJames Smart 3137e399441dSJames Smart /* io queue count */ 3138d858e5f0SSagi Grimberg ctrl->ctrl.queue_count = min_t(unsigned int, 3139e399441dSJames Smart opts->nr_io_queues, 3140e399441dSJames Smart lport->ops->max_hw_queues); 3141d858e5f0SSagi Grimberg ctrl->ctrl.queue_count++; /* +1 for admin queue */ 3142e399441dSJames Smart 3143e399441dSJames Smart ctrl->ctrl.sqsize = opts->queue_size - 1; 3144e399441dSJames Smart ctrl->ctrl.kato = opts->kato; 3145e399441dSJames Smart 3146e399441dSJames Smart ret = -ENOMEM; 3147d858e5f0SSagi Grimberg ctrl->queues = kcalloc(ctrl->ctrl.queue_count, 3148d858e5f0SSagi Grimberg sizeof(struct nvme_fc_queue), GFP_KERNEL); 3149e399441dSJames Smart if (!ctrl->queues) 315061bff8efSJames Smart goto out_free_ida; 3151e399441dSJames Smart 315261bff8efSJames Smart memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); 315361bff8efSJames Smart ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops; 315438dabe21SKeith Busch ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH; 315561bff8efSJames Smart ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */ 315661bff8efSJames Smart ctrl->admin_tag_set.numa_node = NUMA_NO_NODE; 315761bff8efSJames Smart ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) + 315861bff8efSJames Smart (SG_CHUNK_SIZE * 315961bff8efSJames Smart sizeof(struct scatterlist)) + 316061bff8efSJames Smart ctrl->lport->ops->fcprqst_priv_sz; 316161bff8efSJames Smart ctrl->admin_tag_set.driver_data = ctrl; 316261bff8efSJames Smart ctrl->admin_tag_set.nr_hw_queues = 1; 316361bff8efSJames Smart ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; 31645a22e2bfSIsrael Rukshin ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED; 316561bff8efSJames Smart 316661bff8efSJames Smart ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); 3167e399441dSJames Smart if (ret) 316861bff8efSJames Smart goto out_free_queues; 316934b6c231SSagi Grimberg ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set; 3170e399441dSJames Smart 317161bff8efSJames Smart ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); 317261bff8efSJames Smart if (IS_ERR(ctrl->ctrl.admin_q)) { 317361bff8efSJames Smart ret = PTR_ERR(ctrl->ctrl.admin_q); 317461bff8efSJames Smart goto out_free_admin_tag_set; 3175e399441dSJames Smart } 3176e399441dSJames Smart 317761bff8efSJames Smart /* 317861bff8efSJames Smart * Would have been nice to init io queues tag set as well. 317961bff8efSJames Smart * However, we require interaction from the controller 318061bff8efSJames Smart * for max io queue count before we can do so. 318161bff8efSJames Smart * Defer this to the connect path. 318261bff8efSJames Smart */ 3183e399441dSJames Smart 318461bff8efSJames Smart ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0); 3185e399441dSJames Smart if (ret) 318661bff8efSJames Smart goto out_cleanup_admin_q; 3187e399441dSJames Smart 318861bff8efSJames Smart /* at this point, teardown path changes to ref counting on nvme ctrl */ 3189e399441dSJames Smart 3190e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags); 3191e399441dSJames Smart list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list); 3192e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 3193e399441dSJames Smart 319417c4dc6eSJames Smart /* 319517c4dc6eSJames Smart * It's possible that transactions used to create the association 319617c4dc6eSJames Smart * may fail. Examples: CreateAssociation LS or CreateIOConnection 319717c4dc6eSJames Smart * LS gets dropped/corrupted/fails; or a frame gets dropped or a 319817c4dc6eSJames Smart * command times out for one of the actions to init the controller 319917c4dc6eSJames Smart * (Connect, Get/Set_Property, Set_Features, etc). Many of these 320017c4dc6eSJames Smart * transport errors (frame drop, LS failure) inherently must kill 320117c4dc6eSJames Smart * the association. The transport is coded so that any command used 320217c4dc6eSJames Smart * to create the association (prior to a LIVE state transition 320317c4dc6eSJames Smart * while NEW or RECONNECTING) will fail if it completes in error or 320417c4dc6eSJames Smart * times out. 320517c4dc6eSJames Smart * 320617c4dc6eSJames Smart * As such: as the connect request was mostly likely due to a 320717c4dc6eSJames Smart * udev event that discovered the remote port, meaning there is 320817c4dc6eSJames Smart * not an admin or script there to restart if the connect 320917c4dc6eSJames Smart * request fails, retry the initial connection creation up to 321017c4dc6eSJames Smart * three times before giving up and declaring failure. 321117c4dc6eSJames Smart */ 321217c4dc6eSJames Smart for (retry = 0; retry < 3; retry++) { 321361bff8efSJames Smart ret = nvme_fc_create_association(ctrl); 321417c4dc6eSJames Smart if (!ret) 321517c4dc6eSJames Smart break; 321617c4dc6eSJames Smart } 321717c4dc6eSJames Smart 321861bff8efSJames Smart if (ret) { 321917c4dc6eSJames Smart /* couldn't schedule retry - fail out */ 322017c4dc6eSJames Smart dev_err(ctrl->ctrl.device, 322117c4dc6eSJames Smart "NVME-FC{%d}: Connect retry failed\n", ctrl->cnum); 322217c4dc6eSJames Smart 3223de41447aSEwan D. Milne ctrl->ctrl.opts = NULL; 322417c4dc6eSJames Smart 322561bff8efSJames Smart /* initiate nvme ctrl ref counting teardown */ 322661bff8efSJames Smart nvme_uninit_ctrl(&ctrl->ctrl); 322724b7f059SJames Smart nvme_put_ctrl(&ctrl->ctrl); 322861bff8efSJames Smart 32290b5a7669SJames Smart /* Remove core ctrl ref. */ 32300b5a7669SJames Smart nvme_put_ctrl(&ctrl->ctrl); 32310b5a7669SJames Smart 323261bff8efSJames Smart /* as we're past the point where we transition to the ref 323361bff8efSJames Smart * counting teardown path, if we return a bad pointer here, 323461bff8efSJames Smart * the calling routine, thinking it's prior to the 323561bff8efSJames Smart * transition, will do an rport put. Since the teardown 323661bff8efSJames Smart * path also does a rport put, we do an extra get here to 323761bff8efSJames Smart * so proper order/teardown happens. 323861bff8efSJames Smart */ 323961bff8efSJames Smart nvme_fc_rport_get(rport); 324061bff8efSJames Smart 324161bff8efSJames Smart if (ret > 0) 324261bff8efSJames Smart ret = -EIO; 324361bff8efSJames Smart return ERR_PTR(ret); 3244e399441dSJames Smart } 3245e399441dSJames Smart 3246d22524a4SChristoph Hellwig nvme_get_ctrl(&ctrl->ctrl); 32472cb657bcSJames Smart 324861bff8efSJames Smart dev_info(ctrl->ctrl.device, 324961bff8efSJames Smart "NVME-FC{%d}: new ctrl: NQN \"%s\"\n", 325061bff8efSJames Smart ctrl->cnum, ctrl->ctrl.opts->subsysnqn); 325161bff8efSJames Smart 3252e399441dSJames Smart return &ctrl->ctrl; 3253e399441dSJames Smart 325461bff8efSJames Smart out_cleanup_admin_q: 325561bff8efSJames Smart blk_cleanup_queue(ctrl->ctrl.admin_q); 325661bff8efSJames Smart out_free_admin_tag_set: 325761bff8efSJames Smart blk_mq_free_tag_set(&ctrl->admin_tag_set); 325861bff8efSJames Smart out_free_queues: 325961bff8efSJames Smart kfree(ctrl->queues); 3260e399441dSJames Smart out_free_ida: 326161bff8efSJames Smart put_device(ctrl->dev); 3262e399441dSJames Smart ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum); 3263e399441dSJames Smart out_free_ctrl: 3264e399441dSJames Smart kfree(ctrl); 3265e399441dSJames Smart out_fail: 3266e399441dSJames Smart /* exit via here doesn't follow ctlr ref points */ 3267e399441dSJames Smart return ERR_PTR(ret); 3268e399441dSJames Smart } 3269e399441dSJames Smart 3270e399441dSJames Smart 3271e399441dSJames Smart struct nvmet_fc_traddr { 3272e399441dSJames Smart u64 nn; 3273e399441dSJames Smart u64 pn; 3274e399441dSJames Smart }; 3275e399441dSJames Smart 3276e399441dSJames Smart static int 32779c5358e1SJames Smart __nvme_fc_parse_u64(substring_t *sstr, u64 *val) 3278e399441dSJames Smart { 3279e399441dSJames Smart u64 token64; 3280e399441dSJames Smart 32819c5358e1SJames Smart if (match_u64(sstr, &token64)) 32829c5358e1SJames Smart return -EINVAL; 32839c5358e1SJames Smart *val = token64; 3284e399441dSJames Smart 32859c5358e1SJames Smart return 0; 3286e399441dSJames Smart } 3287e399441dSJames Smart 32889c5358e1SJames Smart /* 32899c5358e1SJames Smart * This routine validates and extracts the WWN's from the TRADDR string. 32909c5358e1SJames Smart * As kernel parsers need the 0x to determine number base, universally 32919c5358e1SJames Smart * build string to parse with 0x prefix before parsing name strings. 32929c5358e1SJames Smart */ 32939c5358e1SJames Smart static int 32949c5358e1SJames Smart nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen) 32959c5358e1SJames Smart { 32969c5358e1SJames Smart char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1]; 32979c5358e1SJames Smart substring_t wwn = { name, &name[sizeof(name)-1] }; 32989c5358e1SJames Smart int nnoffset, pnoffset; 32999c5358e1SJames Smart 33009c5358e1SJames Smart /* validate it string one of the 2 allowed formats */ 33019c5358e1SJames Smart if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH && 33029c5358e1SJames Smart !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) && 33039c5358e1SJames Smart !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET], 33049c5358e1SJames Smart "pn-0x", NVME_FC_TRADDR_OXNNLEN)) { 33059c5358e1SJames Smart nnoffset = NVME_FC_TRADDR_OXNNLEN; 33069c5358e1SJames Smart pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET + 33079c5358e1SJames Smart NVME_FC_TRADDR_OXNNLEN; 33089c5358e1SJames Smart } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH && 33099c5358e1SJames Smart !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) && 33109c5358e1SJames Smart !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET], 33119c5358e1SJames Smart "pn-", NVME_FC_TRADDR_NNLEN))) { 33129c5358e1SJames Smart nnoffset = NVME_FC_TRADDR_NNLEN; 33139c5358e1SJames Smart pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN; 33149c5358e1SJames Smart } else 33159c5358e1SJames Smart goto out_einval; 33169c5358e1SJames Smart 33179c5358e1SJames Smart name[0] = '0'; 33189c5358e1SJames Smart name[1] = 'x'; 33199c5358e1SJames Smart name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0; 33209c5358e1SJames Smart 33219c5358e1SJames Smart memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN); 33229c5358e1SJames Smart if (__nvme_fc_parse_u64(&wwn, &traddr->nn)) 33239c5358e1SJames Smart goto out_einval; 33249c5358e1SJames Smart 33259c5358e1SJames Smart memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN); 33269c5358e1SJames Smart if (__nvme_fc_parse_u64(&wwn, &traddr->pn)) 33279c5358e1SJames Smart goto out_einval; 33289c5358e1SJames Smart 33299c5358e1SJames Smart return 0; 33309c5358e1SJames Smart 33319c5358e1SJames Smart out_einval: 33329c5358e1SJames Smart pr_warn("%s: bad traddr string\n", __func__); 33339c5358e1SJames Smart return -EINVAL; 3334e399441dSJames Smart } 3335e399441dSJames Smart 3336e399441dSJames Smart static struct nvme_ctrl * 3337e399441dSJames Smart nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts) 3338e399441dSJames Smart { 3339e399441dSJames Smart struct nvme_fc_lport *lport; 3340e399441dSJames Smart struct nvme_fc_rport *rport; 334161bff8efSJames Smart struct nvme_ctrl *ctrl; 3342e399441dSJames Smart struct nvmet_fc_traddr laddr = { 0L, 0L }; 3343e399441dSJames Smart struct nvmet_fc_traddr raddr = { 0L, 0L }; 3344e399441dSJames Smart unsigned long flags; 3345e399441dSJames Smart int ret; 3346e399441dSJames Smart 33479c5358e1SJames Smart ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE); 3348e399441dSJames Smart if (ret || !raddr.nn || !raddr.pn) 3349e399441dSJames Smart return ERR_PTR(-EINVAL); 3350e399441dSJames Smart 33519c5358e1SJames Smart ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE); 3352e399441dSJames Smart if (ret || !laddr.nn || !laddr.pn) 3353e399441dSJames Smart return ERR_PTR(-EINVAL); 3354e399441dSJames Smart 3355e399441dSJames Smart /* find the host and remote ports to connect together */ 3356e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 3357e399441dSJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { 3358e399441dSJames Smart if (lport->localport.node_name != laddr.nn || 3359e399441dSJames Smart lport->localport.port_name != laddr.pn) 3360e399441dSJames Smart continue; 3361e399441dSJames Smart 3362e399441dSJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) { 3363e399441dSJames Smart if (rport->remoteport.node_name != raddr.nn || 3364e399441dSJames Smart rport->remoteport.port_name != raddr.pn) 3365e399441dSJames Smart continue; 3366e399441dSJames Smart 3367e399441dSJames Smart /* if fail to get reference fall through. Will error */ 3368e399441dSJames Smart if (!nvme_fc_rport_get(rport)) 3369e399441dSJames Smart break; 3370e399441dSJames Smart 3371e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 3372e399441dSJames Smart 337361bff8efSJames Smart ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport); 337461bff8efSJames Smart if (IS_ERR(ctrl)) 337561bff8efSJames Smart nvme_fc_rport_put(rport); 337661bff8efSJames Smart return ctrl; 3377e399441dSJames Smart } 3378e399441dSJames Smart } 3379e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 3380e399441dSJames Smart 3381e399441dSJames Smart return ERR_PTR(-ENOENT); 3382e399441dSJames Smart } 3383e399441dSJames Smart 3384e399441dSJames Smart 3385e399441dSJames Smart static struct nvmf_transport_ops nvme_fc_transport = { 3386e399441dSJames Smart .name = "fc", 33870de5cd36SRoy Shterman .module = THIS_MODULE, 3388e399441dSJames Smart .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR, 33895bbecdbcSJames Smart .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO, 3390e399441dSJames Smart .create_ctrl = nvme_fc_create_ctrl, 3391e399441dSJames Smart }; 3392e399441dSJames Smart 3393e399441dSJames Smart static int __init nvme_fc_init_module(void) 3394e399441dSJames Smart { 33955f568556SJames Smart int ret; 33965f568556SJames Smart 33975f568556SJames Smart /* 33985f568556SJames Smart * NOTE: 33995f568556SJames Smart * It is expected that in the future the kernel will combine 34005f568556SJames Smart * the FC-isms that are currently under scsi and now being 34015f568556SJames Smart * added to by NVME into a new standalone FC class. The SCSI 34025f568556SJames Smart * and NVME protocols and their devices would be under this 34035f568556SJames Smart * new FC class. 34045f568556SJames Smart * 34055f568556SJames Smart * As we need something to post FC-specific udev events to, 34065f568556SJames Smart * specifically for nvme probe events, start by creating the 34075f568556SJames Smart * new device class. When the new standalone FC class is 34085f568556SJames Smart * put in place, this code will move to a more generic 34095f568556SJames Smart * location for the class. 34105f568556SJames Smart */ 34115f568556SJames Smart fc_class = class_create(THIS_MODULE, "fc"); 34125f568556SJames Smart if (IS_ERR(fc_class)) { 34135f568556SJames Smart pr_err("couldn't register class fc\n"); 34145f568556SJames Smart return PTR_ERR(fc_class); 34155f568556SJames Smart } 34165f568556SJames Smart 34175f568556SJames Smart /* 34185f568556SJames Smart * Create a device for the FC-centric udev events 34195f568556SJames Smart */ 34205f568556SJames Smart fc_udev_device = device_create(fc_class, NULL, MKDEV(0, 0), NULL, 34215f568556SJames Smart "fc_udev_device"); 34225f568556SJames Smart if (IS_ERR(fc_udev_device)) { 34235f568556SJames Smart pr_err("couldn't create fc_udev device!\n"); 34245f568556SJames Smart ret = PTR_ERR(fc_udev_device); 34255f568556SJames Smart goto out_destroy_class; 34265f568556SJames Smart } 34275f568556SJames Smart 34285f568556SJames Smart ret = nvmf_register_transport(&nvme_fc_transport); 34295f568556SJames Smart if (ret) 34305f568556SJames Smart goto out_destroy_device; 34315f568556SJames Smart 34325f568556SJames Smart return 0; 34335f568556SJames Smart 34345f568556SJames Smart out_destroy_device: 34355f568556SJames Smart device_destroy(fc_class, MKDEV(0, 0)); 34365f568556SJames Smart out_destroy_class: 34375f568556SJames Smart class_destroy(fc_class); 34385f568556SJames Smart return ret; 3439e399441dSJames Smart } 3440e399441dSJames Smart 3441e399441dSJames Smart static void __exit nvme_fc_exit_module(void) 3442e399441dSJames Smart { 3443e399441dSJames Smart /* sanity check - all lports should be removed */ 3444e399441dSJames Smart if (!list_empty(&nvme_fc_lport_list)) 3445e399441dSJames Smart pr_warn("%s: localport list not empty\n", __func__); 3446e399441dSJames Smart 3447e399441dSJames Smart nvmf_unregister_transport(&nvme_fc_transport); 3448e399441dSJames Smart 3449e399441dSJames Smart ida_destroy(&nvme_fc_local_port_cnt); 3450e399441dSJames Smart ida_destroy(&nvme_fc_ctrl_cnt); 34515f568556SJames Smart 34525f568556SJames Smart device_destroy(fc_class, MKDEV(0, 0)); 34535f568556SJames Smart class_destroy(fc_class); 3454e399441dSJames Smart } 3455e399441dSJames Smart 3456e399441dSJames Smart module_init(nvme_fc_init_module); 3457e399441dSJames Smart module_exit(nvme_fc_exit_module); 3458e399441dSJames Smart 3459e399441dSJames Smart MODULE_LICENSE("GPL v2"); 3460