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> 22e399441dSJames Smart 23e399441dSJames Smart #include "nvme.h" 24e399441dSJames Smart #include "fabrics.h" 25e399441dSJames Smart #include <linux/nvme-fc-driver.h> 26e399441dSJames Smart #include <linux/nvme-fc.h> 27e399441dSJames Smart 28e399441dSJames Smart 29e399441dSJames Smart /* *************************** Data Structures/Defines ****************** */ 30e399441dSJames Smart 31e399441dSJames Smart 32e399441dSJames Smart /* 33e399441dSJames Smart * We handle AEN commands ourselves and don't even let the 34e399441dSJames Smart * block layer know about them. 35e399441dSJames Smart */ 36e399441dSJames Smart #define NVME_FC_NR_AEN_COMMANDS 1 37e399441dSJames Smart #define NVME_FC_AQ_BLKMQ_DEPTH \ 38e399441dSJames Smart (NVMF_AQ_DEPTH - NVME_FC_NR_AEN_COMMANDS) 39e399441dSJames Smart #define AEN_CMDID_BASE (NVME_FC_AQ_BLKMQ_DEPTH + 1) 40e399441dSJames Smart 41e399441dSJames Smart enum nvme_fc_queue_flags { 42e399441dSJames Smart NVME_FC_Q_CONNECTED = (1 << 0), 43e399441dSJames Smart }; 44e399441dSJames Smart 45e399441dSJames Smart #define NVMEFC_QUEUE_DELAY 3 /* ms units */ 46e399441dSJames Smart 47e399441dSJames Smart struct nvme_fc_queue { 48e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 49e399441dSJames Smart struct device *dev; 50e399441dSJames Smart struct blk_mq_hw_ctx *hctx; 51e399441dSJames Smart void *lldd_handle; 52e399441dSJames Smart int queue_size; 53e399441dSJames Smart size_t cmnd_capsule_len; 54e399441dSJames Smart u32 qnum; 55e399441dSJames Smart u32 rqcnt; 56e399441dSJames Smart u32 seqno; 57e399441dSJames Smart 58e399441dSJames Smart u64 connection_id; 59e399441dSJames Smart atomic_t csn; 60e399441dSJames Smart 61e399441dSJames Smart unsigned long flags; 62e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 63e399441dSJames Smart 64e399441dSJames Smart struct nvmefc_ls_req_op { 65e399441dSJames Smart struct nvmefc_ls_req ls_req; 66e399441dSJames Smart 67e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 68e399441dSJames Smart struct nvme_fc_queue *queue; 69e399441dSJames Smart struct request *rq; 70e399441dSJames Smart 71e399441dSJames Smart int ls_error; 72e399441dSJames Smart struct completion ls_done; 73e399441dSJames Smart struct list_head lsreq_list; /* ctrl->ls_req_list */ 74e399441dSJames Smart bool req_queued; 75e399441dSJames Smart }; 76e399441dSJames Smart 77e399441dSJames Smart enum nvme_fcpop_state { 78e399441dSJames Smart FCPOP_STATE_UNINIT = 0, 79e399441dSJames Smart FCPOP_STATE_IDLE = 1, 80e399441dSJames Smart FCPOP_STATE_ACTIVE = 2, 81e399441dSJames Smart FCPOP_STATE_ABORTED = 3, 82e399441dSJames Smart }; 83e399441dSJames Smart 84e399441dSJames Smart struct nvme_fc_fcp_op { 85e399441dSJames Smart struct nvme_request nreq; /* 86e399441dSJames Smart * nvme/host/core.c 87e399441dSJames Smart * requires this to be 88e399441dSJames Smart * the 1st element in the 89e399441dSJames Smart * private structure 90e399441dSJames Smart * associated with the 91e399441dSJames Smart * request. 92e399441dSJames Smart */ 93e399441dSJames Smart struct nvmefc_fcp_req fcp_req; 94e399441dSJames Smart 95e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 96e399441dSJames Smart struct nvme_fc_queue *queue; 97e399441dSJames Smart struct request *rq; 98e399441dSJames Smart 99e399441dSJames Smart atomic_t state; 100e399441dSJames Smart u32 rqno; 101e399441dSJames Smart u32 nents; 102e399441dSJames Smart 103e399441dSJames Smart struct nvme_fc_cmd_iu cmd_iu; 104e399441dSJames Smart struct nvme_fc_ersp_iu rsp_iu; 105e399441dSJames Smart }; 106e399441dSJames Smart 107e399441dSJames Smart struct nvme_fc_lport { 108e399441dSJames Smart struct nvme_fc_local_port localport; 109e399441dSJames Smart 110e399441dSJames Smart struct ida endp_cnt; 111e399441dSJames Smart struct list_head port_list; /* nvme_fc_port_list */ 112e399441dSJames Smart struct list_head endp_list; 113e399441dSJames Smart struct device *dev; /* physical device for dma */ 114e399441dSJames Smart struct nvme_fc_port_template *ops; 115e399441dSJames Smart struct kref ref; 116e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 117e399441dSJames Smart 118e399441dSJames Smart struct nvme_fc_rport { 119e399441dSJames Smart struct nvme_fc_remote_port remoteport; 120e399441dSJames Smart 121e399441dSJames Smart struct list_head endp_list; /* for lport->endp_list */ 122e399441dSJames Smart struct list_head ctrl_list; 123e399441dSJames Smart spinlock_t lock; 124e399441dSJames Smart struct kref ref; 125e399441dSJames Smart } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ 126e399441dSJames Smart 127e399441dSJames Smart enum nvme_fcctrl_state { 128e399441dSJames Smart FCCTRL_INIT = 0, 129e399441dSJames Smart FCCTRL_ACTIVE = 1, 130e399441dSJames Smart }; 131e399441dSJames Smart 132e399441dSJames Smart struct nvme_fc_ctrl { 133e399441dSJames Smart spinlock_t lock; 134e399441dSJames Smart struct nvme_fc_queue *queues; 135e399441dSJames Smart u32 queue_count; 136e399441dSJames Smart 137e399441dSJames Smart struct device *dev; 138e399441dSJames Smart struct nvme_fc_lport *lport; 139e399441dSJames Smart struct nvme_fc_rport *rport; 140e399441dSJames Smart u32 cnum; 141e399441dSJames Smart 142e399441dSJames Smart u64 association_id; 143e399441dSJames Smart 144e399441dSJames Smart u64 cap; 145e399441dSJames Smart 146e399441dSJames Smart struct list_head ctrl_list; /* rport->ctrl_list */ 147e399441dSJames Smart struct list_head ls_req_list; 148e399441dSJames Smart 149e399441dSJames Smart struct blk_mq_tag_set admin_tag_set; 150e399441dSJames Smart struct blk_mq_tag_set tag_set; 151e399441dSJames Smart 152e399441dSJames Smart struct work_struct delete_work; 153e399441dSJames Smart struct kref ref; 154e399441dSJames Smart int state; 155e399441dSJames Smart 156e399441dSJames Smart struct nvme_fc_fcp_op aen_ops[NVME_FC_NR_AEN_COMMANDS]; 157e399441dSJames Smart 158e399441dSJames Smart struct nvme_ctrl ctrl; 159e399441dSJames Smart }; 160e399441dSJames Smart 161e399441dSJames Smart static inline struct nvme_fc_ctrl * 162e399441dSJames Smart to_fc_ctrl(struct nvme_ctrl *ctrl) 163e399441dSJames Smart { 164e399441dSJames Smart return container_of(ctrl, struct nvme_fc_ctrl, ctrl); 165e399441dSJames Smart } 166e399441dSJames Smart 167e399441dSJames Smart static inline struct nvme_fc_lport * 168e399441dSJames Smart localport_to_lport(struct nvme_fc_local_port *portptr) 169e399441dSJames Smart { 170e399441dSJames Smart return container_of(portptr, struct nvme_fc_lport, localport); 171e399441dSJames Smart } 172e399441dSJames Smart 173e399441dSJames Smart static inline struct nvme_fc_rport * 174e399441dSJames Smart remoteport_to_rport(struct nvme_fc_remote_port *portptr) 175e399441dSJames Smart { 176e399441dSJames Smart return container_of(portptr, struct nvme_fc_rport, remoteport); 177e399441dSJames Smart } 178e399441dSJames Smart 179e399441dSJames Smart static inline struct nvmefc_ls_req_op * 180e399441dSJames Smart ls_req_to_lsop(struct nvmefc_ls_req *lsreq) 181e399441dSJames Smart { 182e399441dSJames Smart return container_of(lsreq, struct nvmefc_ls_req_op, ls_req); 183e399441dSJames Smart } 184e399441dSJames Smart 185e399441dSJames Smart static inline struct nvme_fc_fcp_op * 186e399441dSJames Smart fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq) 187e399441dSJames Smart { 188e399441dSJames Smart return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req); 189e399441dSJames Smart } 190e399441dSJames Smart 191e399441dSJames Smart 192e399441dSJames Smart 193e399441dSJames Smart /* *************************** Globals **************************** */ 194e399441dSJames Smart 195e399441dSJames Smart 196e399441dSJames Smart static DEFINE_SPINLOCK(nvme_fc_lock); 197e399441dSJames Smart 198e399441dSJames Smart static LIST_HEAD(nvme_fc_lport_list); 199e399441dSJames Smart static DEFINE_IDA(nvme_fc_local_port_cnt); 200e399441dSJames Smart static DEFINE_IDA(nvme_fc_ctrl_cnt); 201e399441dSJames Smart 202e399441dSJames Smart static struct workqueue_struct *nvme_fc_wq; 203e399441dSJames Smart 204e399441dSJames Smart 205e399441dSJames Smart 206e399441dSJames Smart /* *********************** FC-NVME Port Management ************************ */ 207e399441dSJames Smart 208e399441dSJames Smart static int __nvme_fc_del_ctrl(struct nvme_fc_ctrl *); 209e399441dSJames Smart static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *, 210e399441dSJames Smart struct nvme_fc_queue *, unsigned int); 211e399441dSJames Smart 212e399441dSJames Smart 213e399441dSJames Smart /** 214e399441dSJames Smart * nvme_fc_register_localport - transport entry point called by an 215e399441dSJames Smart * LLDD to register the existence of a NVME 216e399441dSJames Smart * host FC port. 217e399441dSJames Smart * @pinfo: pointer to information about the port to be registered 218e399441dSJames Smart * @template: LLDD entrypoints and operational parameters for the port 219e399441dSJames Smart * @dev: physical hardware device node port corresponds to. Will be 220e399441dSJames Smart * used for DMA mappings 221e399441dSJames Smart * @lport_p: pointer to a local port pointer. Upon success, the routine 222e399441dSJames Smart * will allocate a nvme_fc_local_port structure and place its 223e399441dSJames Smart * address in the local port pointer. Upon failure, local port 224e399441dSJames Smart * pointer will be set to 0. 225e399441dSJames Smart * 226e399441dSJames Smart * Returns: 227e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 228e399441dSJames Smart * (ex: -ENXIO) upon failure. 229e399441dSJames Smart */ 230e399441dSJames Smart int 231e399441dSJames Smart nvme_fc_register_localport(struct nvme_fc_port_info *pinfo, 232e399441dSJames Smart struct nvme_fc_port_template *template, 233e399441dSJames Smart struct device *dev, 234e399441dSJames Smart struct nvme_fc_local_port **portptr) 235e399441dSJames Smart { 236e399441dSJames Smart struct nvme_fc_lport *newrec; 237e399441dSJames Smart unsigned long flags; 238e399441dSJames Smart int ret, idx; 239e399441dSJames Smart 240e399441dSJames Smart if (!template->localport_delete || !template->remoteport_delete || 241e399441dSJames Smart !template->ls_req || !template->fcp_io || 242e399441dSJames Smart !template->ls_abort || !template->fcp_abort || 243e399441dSJames Smart !template->max_hw_queues || !template->max_sgl_segments || 244e399441dSJames Smart !template->max_dif_sgl_segments || !template->dma_boundary) { 245e399441dSJames Smart ret = -EINVAL; 246e399441dSJames Smart goto out_reghost_failed; 247e399441dSJames Smart } 248e399441dSJames Smart 249e399441dSJames Smart newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz), 250e399441dSJames Smart GFP_KERNEL); 251e399441dSJames Smart if (!newrec) { 252e399441dSJames Smart ret = -ENOMEM; 253e399441dSJames Smart goto out_reghost_failed; 254e399441dSJames Smart } 255e399441dSJames Smart 256e399441dSJames Smart idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL); 257e399441dSJames Smart if (idx < 0) { 258e399441dSJames Smart ret = -ENOSPC; 259e399441dSJames Smart goto out_fail_kfree; 260e399441dSJames Smart } 261e399441dSJames Smart 262e399441dSJames Smart if (!get_device(dev) && dev) { 263e399441dSJames Smart ret = -ENODEV; 264e399441dSJames Smart goto out_ida_put; 265e399441dSJames Smart } 266e399441dSJames Smart 267e399441dSJames Smart INIT_LIST_HEAD(&newrec->port_list); 268e399441dSJames Smart INIT_LIST_HEAD(&newrec->endp_list); 269e399441dSJames Smart kref_init(&newrec->ref); 270e399441dSJames Smart newrec->ops = template; 271e399441dSJames Smart newrec->dev = dev; 272e399441dSJames Smart ida_init(&newrec->endp_cnt); 273e399441dSJames Smart newrec->localport.private = &newrec[1]; 274e399441dSJames Smart newrec->localport.node_name = pinfo->node_name; 275e399441dSJames Smart newrec->localport.port_name = pinfo->port_name; 276e399441dSJames Smart newrec->localport.port_role = pinfo->port_role; 277e399441dSJames Smart newrec->localport.port_id = pinfo->port_id; 278e399441dSJames Smart newrec->localport.port_state = FC_OBJSTATE_ONLINE; 279e399441dSJames Smart newrec->localport.port_num = idx; 280e399441dSJames Smart 281e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 282e399441dSJames Smart list_add_tail(&newrec->port_list, &nvme_fc_lport_list); 283e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 284e399441dSJames Smart 285e399441dSJames Smart if (dev) 286e399441dSJames Smart dma_set_seg_boundary(dev, template->dma_boundary); 287e399441dSJames Smart 288e399441dSJames Smart *portptr = &newrec->localport; 289e399441dSJames Smart return 0; 290e399441dSJames Smart 291e399441dSJames Smart out_ida_put: 292e399441dSJames Smart ida_simple_remove(&nvme_fc_local_port_cnt, idx); 293e399441dSJames Smart out_fail_kfree: 294e399441dSJames Smart kfree(newrec); 295e399441dSJames Smart out_reghost_failed: 296e399441dSJames Smart *portptr = NULL; 297e399441dSJames Smart 298e399441dSJames Smart return ret; 299e399441dSJames Smart } 300e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_localport); 301e399441dSJames Smart 302e399441dSJames Smart static void 303e399441dSJames Smart nvme_fc_free_lport(struct kref *ref) 304e399441dSJames Smart { 305e399441dSJames Smart struct nvme_fc_lport *lport = 306e399441dSJames Smart container_of(ref, struct nvme_fc_lport, ref); 307e399441dSJames Smart unsigned long flags; 308e399441dSJames Smart 309e399441dSJames Smart WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED); 310e399441dSJames Smart WARN_ON(!list_empty(&lport->endp_list)); 311e399441dSJames Smart 312e399441dSJames Smart /* remove from transport list */ 313e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 314e399441dSJames Smart list_del(&lport->port_list); 315e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 316e399441dSJames Smart 317e399441dSJames Smart /* let the LLDD know we've finished tearing it down */ 318e399441dSJames Smart lport->ops->localport_delete(&lport->localport); 319e399441dSJames Smart 320e399441dSJames Smart ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num); 321e399441dSJames Smart ida_destroy(&lport->endp_cnt); 322e399441dSJames Smart 323e399441dSJames Smart put_device(lport->dev); 324e399441dSJames Smart 325e399441dSJames Smart kfree(lport); 326e399441dSJames Smart } 327e399441dSJames Smart 328e399441dSJames Smart static void 329e399441dSJames Smart nvme_fc_lport_put(struct nvme_fc_lport *lport) 330e399441dSJames Smart { 331e399441dSJames Smart kref_put(&lport->ref, nvme_fc_free_lport); 332e399441dSJames Smart } 333e399441dSJames Smart 334e399441dSJames Smart static int 335e399441dSJames Smart nvme_fc_lport_get(struct nvme_fc_lport *lport) 336e399441dSJames Smart { 337e399441dSJames Smart return kref_get_unless_zero(&lport->ref); 338e399441dSJames Smart } 339e399441dSJames Smart 340e399441dSJames Smart /** 341e399441dSJames Smart * nvme_fc_unregister_localport - transport entry point called by an 342e399441dSJames Smart * LLDD to deregister/remove a previously 343e399441dSJames Smart * registered a NVME host FC port. 344e399441dSJames Smart * @localport: pointer to the (registered) local port that is to be 345e399441dSJames Smart * deregistered. 346e399441dSJames Smart * 347e399441dSJames Smart * Returns: 348e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 349e399441dSJames Smart * (ex: -ENXIO) upon failure. 350e399441dSJames Smart */ 351e399441dSJames Smart int 352e399441dSJames Smart nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr) 353e399441dSJames Smart { 354e399441dSJames Smart struct nvme_fc_lport *lport = localport_to_lport(portptr); 355e399441dSJames Smart unsigned long flags; 356e399441dSJames Smart 357e399441dSJames Smart if (!portptr) 358e399441dSJames Smart return -EINVAL; 359e399441dSJames Smart 360e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 361e399441dSJames Smart 362e399441dSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) { 363e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 364e399441dSJames Smart return -EINVAL; 365e399441dSJames Smart } 366e399441dSJames Smart portptr->port_state = FC_OBJSTATE_DELETED; 367e399441dSJames Smart 368e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 369e399441dSJames Smart 370e399441dSJames Smart nvme_fc_lport_put(lport); 371e399441dSJames Smart 372e399441dSJames Smart return 0; 373e399441dSJames Smart } 374e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport); 375e399441dSJames Smart 376e399441dSJames Smart /** 377e399441dSJames Smart * nvme_fc_register_remoteport - transport entry point called by an 378e399441dSJames Smart * LLDD to register the existence of a NVME 379e399441dSJames Smart * subsystem FC port on its fabric. 380e399441dSJames Smart * @localport: pointer to the (registered) local port that the remote 381e399441dSJames Smart * subsystem port is connected to. 382e399441dSJames Smart * @pinfo: pointer to information about the port to be registered 383e399441dSJames Smart * @rport_p: pointer to a remote port pointer. Upon success, the routine 384e399441dSJames Smart * will allocate a nvme_fc_remote_port structure and place its 385e399441dSJames Smart * address in the remote port pointer. Upon failure, remote port 386e399441dSJames Smart * pointer will be set to 0. 387e399441dSJames Smart * 388e399441dSJames Smart * Returns: 389e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 390e399441dSJames Smart * (ex: -ENXIO) upon failure. 391e399441dSJames Smart */ 392e399441dSJames Smart int 393e399441dSJames Smart nvme_fc_register_remoteport(struct nvme_fc_local_port *localport, 394e399441dSJames Smart struct nvme_fc_port_info *pinfo, 395e399441dSJames Smart struct nvme_fc_remote_port **portptr) 396e399441dSJames Smart { 397e399441dSJames Smart struct nvme_fc_lport *lport = localport_to_lport(localport); 398e399441dSJames Smart struct nvme_fc_rport *newrec; 399e399441dSJames Smart unsigned long flags; 400e399441dSJames Smart int ret, idx; 401e399441dSJames Smart 402e399441dSJames Smart newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz), 403e399441dSJames Smart GFP_KERNEL); 404e399441dSJames Smart if (!newrec) { 405e399441dSJames Smart ret = -ENOMEM; 406e399441dSJames Smart goto out_reghost_failed; 407e399441dSJames Smart } 408e399441dSJames Smart 409e399441dSJames Smart if (!nvme_fc_lport_get(lport)) { 410e399441dSJames Smart ret = -ESHUTDOWN; 411e399441dSJames Smart goto out_kfree_rport; 412e399441dSJames Smart } 413e399441dSJames Smart 414e399441dSJames Smart idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL); 415e399441dSJames Smart if (idx < 0) { 416e399441dSJames Smart ret = -ENOSPC; 417e399441dSJames Smart goto out_lport_put; 418e399441dSJames Smart } 419e399441dSJames Smart 420e399441dSJames Smart INIT_LIST_HEAD(&newrec->endp_list); 421e399441dSJames Smart INIT_LIST_HEAD(&newrec->ctrl_list); 422e399441dSJames Smart kref_init(&newrec->ref); 423e399441dSJames Smart spin_lock_init(&newrec->lock); 424e399441dSJames Smart newrec->remoteport.localport = &lport->localport; 425e399441dSJames Smart newrec->remoteport.private = &newrec[1]; 426e399441dSJames Smart newrec->remoteport.port_role = pinfo->port_role; 427e399441dSJames Smart newrec->remoteport.node_name = pinfo->node_name; 428e399441dSJames Smart newrec->remoteport.port_name = pinfo->port_name; 429e399441dSJames Smart newrec->remoteport.port_id = pinfo->port_id; 430e399441dSJames Smart newrec->remoteport.port_state = FC_OBJSTATE_ONLINE; 431e399441dSJames Smart newrec->remoteport.port_num = idx; 432e399441dSJames Smart 433e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 434e399441dSJames Smart list_add_tail(&newrec->endp_list, &lport->endp_list); 435e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 436e399441dSJames Smart 437e399441dSJames Smart *portptr = &newrec->remoteport; 438e399441dSJames Smart return 0; 439e399441dSJames Smart 440e399441dSJames Smart out_lport_put: 441e399441dSJames Smart nvme_fc_lport_put(lport); 442e399441dSJames Smart out_kfree_rport: 443e399441dSJames Smart kfree(newrec); 444e399441dSJames Smart out_reghost_failed: 445e399441dSJames Smart *portptr = NULL; 446e399441dSJames Smart return ret; 447e399441dSJames Smart 448e399441dSJames Smart } 449e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport); 450e399441dSJames Smart 451e399441dSJames Smart static void 452e399441dSJames Smart nvme_fc_free_rport(struct kref *ref) 453e399441dSJames Smart { 454e399441dSJames Smart struct nvme_fc_rport *rport = 455e399441dSJames Smart container_of(ref, struct nvme_fc_rport, ref); 456e399441dSJames Smart struct nvme_fc_lport *lport = 457e399441dSJames Smart localport_to_lport(rport->remoteport.localport); 458e399441dSJames Smart unsigned long flags; 459e399441dSJames Smart 460e399441dSJames Smart WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED); 461e399441dSJames Smart WARN_ON(!list_empty(&rport->ctrl_list)); 462e399441dSJames Smart 463e399441dSJames Smart /* remove from lport list */ 464e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 465e399441dSJames Smart list_del(&rport->endp_list); 466e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 467e399441dSJames Smart 468e399441dSJames Smart /* let the LLDD know we've finished tearing it down */ 469e399441dSJames Smart lport->ops->remoteport_delete(&rport->remoteport); 470e399441dSJames Smart 471e399441dSJames Smart ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num); 472e399441dSJames Smart 473e399441dSJames Smart kfree(rport); 474e399441dSJames Smart 475e399441dSJames Smart nvme_fc_lport_put(lport); 476e399441dSJames Smart } 477e399441dSJames Smart 478e399441dSJames Smart static void 479e399441dSJames Smart nvme_fc_rport_put(struct nvme_fc_rport *rport) 480e399441dSJames Smart { 481e399441dSJames Smart kref_put(&rport->ref, nvme_fc_free_rport); 482e399441dSJames Smart } 483e399441dSJames Smart 484e399441dSJames Smart static int 485e399441dSJames Smart nvme_fc_rport_get(struct nvme_fc_rport *rport) 486e399441dSJames Smart { 487e399441dSJames Smart return kref_get_unless_zero(&rport->ref); 488e399441dSJames Smart } 489e399441dSJames Smart 490e399441dSJames Smart /** 491e399441dSJames Smart * nvme_fc_unregister_remoteport - transport entry point called by an 492e399441dSJames Smart * LLDD to deregister/remove a previously 493e399441dSJames Smart * registered a NVME subsystem FC port. 494e399441dSJames Smart * @remoteport: pointer to the (registered) remote port that is to be 495e399441dSJames Smart * deregistered. 496e399441dSJames Smart * 497e399441dSJames Smart * Returns: 498e399441dSJames Smart * a completion status. Must be 0 upon success; a negative errno 499e399441dSJames Smart * (ex: -ENXIO) upon failure. 500e399441dSJames Smart */ 501e399441dSJames Smart int 502e399441dSJames Smart nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr) 503e399441dSJames Smart { 504e399441dSJames Smart struct nvme_fc_rport *rport = remoteport_to_rport(portptr); 505e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 506e399441dSJames Smart unsigned long flags; 507e399441dSJames Smart 508e399441dSJames Smart if (!portptr) 509e399441dSJames Smart return -EINVAL; 510e399441dSJames Smart 511e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags); 512e399441dSJames Smart 513e399441dSJames Smart if (portptr->port_state != FC_OBJSTATE_ONLINE) { 514e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 515e399441dSJames Smart return -EINVAL; 516e399441dSJames Smart } 517e399441dSJames Smart portptr->port_state = FC_OBJSTATE_DELETED; 518e399441dSJames Smart 519e399441dSJames Smart /* tear down all associations to the remote port */ 520e399441dSJames Smart list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) 521e399441dSJames Smart __nvme_fc_del_ctrl(ctrl); 522e399441dSJames Smart 523e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 524e399441dSJames Smart 525e399441dSJames Smart nvme_fc_rport_put(rport); 526e399441dSJames Smart return 0; 527e399441dSJames Smart } 528e399441dSJames Smart EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport); 529e399441dSJames Smart 530e399441dSJames Smart 531e399441dSJames Smart /* *********************** FC-NVME DMA Handling **************************** */ 532e399441dSJames Smart 533e399441dSJames Smart /* 534e399441dSJames Smart * The fcloop device passes in a NULL device pointer. Real LLD's will 535e399441dSJames Smart * pass in a valid device pointer. If NULL is passed to the dma mapping 536e399441dSJames Smart * routines, depending on the platform, it may or may not succeed, and 537e399441dSJames Smart * may crash. 538e399441dSJames Smart * 539e399441dSJames Smart * As such: 540e399441dSJames Smart * Wrapper all the dma routines and check the dev pointer. 541e399441dSJames Smart * 542e399441dSJames Smart * If simple mappings (return just a dma address, we'll noop them, 543e399441dSJames Smart * returning a dma address of 0. 544e399441dSJames Smart * 545e399441dSJames Smart * On more complex mappings (dma_map_sg), a pseudo routine fills 546e399441dSJames Smart * in the scatter list, setting all dma addresses to 0. 547e399441dSJames Smart */ 548e399441dSJames Smart 549e399441dSJames Smart static inline dma_addr_t 550e399441dSJames Smart fc_dma_map_single(struct device *dev, void *ptr, size_t size, 551e399441dSJames Smart enum dma_data_direction dir) 552e399441dSJames Smart { 553e399441dSJames Smart return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L; 554e399441dSJames Smart } 555e399441dSJames Smart 556e399441dSJames Smart static inline int 557e399441dSJames Smart fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 558e399441dSJames Smart { 559e399441dSJames Smart return dev ? dma_mapping_error(dev, dma_addr) : 0; 560e399441dSJames Smart } 561e399441dSJames Smart 562e399441dSJames Smart static inline void 563e399441dSJames Smart fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size, 564e399441dSJames Smart enum dma_data_direction dir) 565e399441dSJames Smart { 566e399441dSJames Smart if (dev) 567e399441dSJames Smart dma_unmap_single(dev, addr, size, dir); 568e399441dSJames Smart } 569e399441dSJames Smart 570e399441dSJames Smart static inline void 571e399441dSJames Smart fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size, 572e399441dSJames Smart enum dma_data_direction dir) 573e399441dSJames Smart { 574e399441dSJames Smart if (dev) 575e399441dSJames Smart dma_sync_single_for_cpu(dev, addr, size, dir); 576e399441dSJames Smart } 577e399441dSJames Smart 578e399441dSJames Smart static inline void 579e399441dSJames Smart fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size, 580e399441dSJames Smart enum dma_data_direction dir) 581e399441dSJames Smart { 582e399441dSJames Smart if (dev) 583e399441dSJames Smart dma_sync_single_for_device(dev, addr, size, dir); 584e399441dSJames Smart } 585e399441dSJames Smart 586e399441dSJames Smart /* pseudo dma_map_sg call */ 587e399441dSJames Smart static int 588e399441dSJames Smart fc_map_sg(struct scatterlist *sg, int nents) 589e399441dSJames Smart { 590e399441dSJames Smart struct scatterlist *s; 591e399441dSJames Smart int i; 592e399441dSJames Smart 593e399441dSJames Smart WARN_ON(nents == 0 || sg[0].length == 0); 594e399441dSJames Smart 595e399441dSJames Smart for_each_sg(sg, s, nents, i) { 596e399441dSJames Smart s->dma_address = 0L; 597e399441dSJames Smart #ifdef CONFIG_NEED_SG_DMA_LENGTH 598e399441dSJames Smart s->dma_length = s->length; 599e399441dSJames Smart #endif 600e399441dSJames Smart } 601e399441dSJames Smart return nents; 602e399441dSJames Smart } 603e399441dSJames Smart 604e399441dSJames Smart static inline int 605e399441dSJames Smart fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, 606e399441dSJames Smart enum dma_data_direction dir) 607e399441dSJames Smart { 608e399441dSJames Smart return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents); 609e399441dSJames Smart } 610e399441dSJames Smart 611e399441dSJames Smart static inline void 612e399441dSJames Smart fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, 613e399441dSJames Smart enum dma_data_direction dir) 614e399441dSJames Smart { 615e399441dSJames Smart if (dev) 616e399441dSJames Smart dma_unmap_sg(dev, sg, nents, dir); 617e399441dSJames Smart } 618e399441dSJames Smart 619e399441dSJames Smart 620e399441dSJames Smart /* *********************** FC-NVME LS Handling **************************** */ 621e399441dSJames Smart 622e399441dSJames Smart static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *); 623e399441dSJames Smart static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *); 624e399441dSJames Smart 625e399441dSJames Smart 626e399441dSJames Smart static void 627e399441dSJames Smart __nvme_fc_finish_ls_req(struct nvme_fc_ctrl *ctrl, 628e399441dSJames Smart struct nvmefc_ls_req_op *lsop) 629e399441dSJames Smart { 630e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req; 631e399441dSJames Smart unsigned long flags; 632e399441dSJames Smart 633e399441dSJames Smart spin_lock_irqsave(&ctrl->lock, flags); 634e399441dSJames Smart 635e399441dSJames Smart if (!lsop->req_queued) { 636e399441dSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 637e399441dSJames Smart return; 638e399441dSJames Smart } 639e399441dSJames Smart 640e399441dSJames Smart list_del(&lsop->lsreq_list); 641e399441dSJames Smart 642e399441dSJames Smart lsop->req_queued = false; 643e399441dSJames Smart 644e399441dSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 645e399441dSJames Smart 646e399441dSJames Smart fc_dma_unmap_single(ctrl->dev, lsreq->rqstdma, 647e399441dSJames Smart (lsreq->rqstlen + lsreq->rsplen), 648e399441dSJames Smart DMA_BIDIRECTIONAL); 649e399441dSJames Smart 650e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 651e399441dSJames Smart } 652e399441dSJames Smart 653e399441dSJames Smart static int 654e399441dSJames Smart __nvme_fc_send_ls_req(struct nvme_fc_ctrl *ctrl, 655e399441dSJames Smart struct nvmefc_ls_req_op *lsop, 656e399441dSJames Smart void (*done)(struct nvmefc_ls_req *req, int status)) 657e399441dSJames Smart { 658e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req; 659e399441dSJames Smart unsigned long flags; 660e399441dSJames Smart int ret; 661e399441dSJames Smart 662e399441dSJames Smart if (!nvme_fc_ctrl_get(ctrl)) 663e399441dSJames Smart return -ESHUTDOWN; 664e399441dSJames Smart 665e399441dSJames Smart lsreq->done = done; 666e399441dSJames Smart lsop->ctrl = ctrl; 667e399441dSJames Smart lsop->req_queued = false; 668e399441dSJames Smart INIT_LIST_HEAD(&lsop->lsreq_list); 669e399441dSJames Smart init_completion(&lsop->ls_done); 670e399441dSJames Smart 671e399441dSJames Smart lsreq->rqstdma = fc_dma_map_single(ctrl->dev, lsreq->rqstaddr, 672e399441dSJames Smart lsreq->rqstlen + lsreq->rsplen, 673e399441dSJames Smart DMA_BIDIRECTIONAL); 674e399441dSJames Smart if (fc_dma_mapping_error(ctrl->dev, lsreq->rqstdma)) { 675e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 676e399441dSJames Smart dev_err(ctrl->dev, 677e399441dSJames Smart "els request command failed EFAULT.\n"); 678e399441dSJames Smart return -EFAULT; 679e399441dSJames Smart } 680e399441dSJames Smart lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen; 681e399441dSJames Smart 682e399441dSJames Smart spin_lock_irqsave(&ctrl->lock, flags); 683e399441dSJames Smart 684e399441dSJames Smart list_add_tail(&lsop->lsreq_list, &ctrl->ls_req_list); 685e399441dSJames Smart 686e399441dSJames Smart lsop->req_queued = true; 687e399441dSJames Smart 688e399441dSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 689e399441dSJames Smart 690e399441dSJames Smart ret = ctrl->lport->ops->ls_req(&ctrl->lport->localport, 691e399441dSJames Smart &ctrl->rport->remoteport, lsreq); 692e399441dSJames Smart if (ret) 693e399441dSJames Smart lsop->ls_error = ret; 694e399441dSJames Smart 695e399441dSJames Smart return ret; 696e399441dSJames Smart } 697e399441dSJames Smart 698e399441dSJames Smart static void 699e399441dSJames Smart nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status) 700e399441dSJames Smart { 701e399441dSJames Smart struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq); 702e399441dSJames Smart 703e399441dSJames Smart lsop->ls_error = status; 704e399441dSJames Smart complete(&lsop->ls_done); 705e399441dSJames Smart } 706e399441dSJames Smart 707e399441dSJames Smart static int 708e399441dSJames Smart nvme_fc_send_ls_req(struct nvme_fc_ctrl *ctrl, struct nvmefc_ls_req_op *lsop) 709e399441dSJames Smart { 710e399441dSJames Smart struct nvmefc_ls_req *lsreq = &lsop->ls_req; 711e399441dSJames Smart struct fcnvme_ls_rjt *rjt = lsreq->rspaddr; 712e399441dSJames Smart int ret; 713e399441dSJames Smart 714e399441dSJames Smart ret = __nvme_fc_send_ls_req(ctrl, lsop, nvme_fc_send_ls_req_done); 715e399441dSJames Smart 716e399441dSJames Smart if (!ret) 717e399441dSJames Smart /* 718e399441dSJames Smart * No timeout/not interruptible as we need the struct 719e399441dSJames Smart * to exist until the lldd calls us back. Thus mandate 720e399441dSJames Smart * wait until driver calls back. lldd responsible for 721e399441dSJames Smart * the timeout action 722e399441dSJames Smart */ 723e399441dSJames Smart wait_for_completion(&lsop->ls_done); 724e399441dSJames Smart 725e399441dSJames Smart __nvme_fc_finish_ls_req(ctrl, lsop); 726e399441dSJames Smart 727e399441dSJames Smart if (ret) { 728e399441dSJames Smart dev_err(ctrl->dev, 729e399441dSJames Smart "ls request command failed (%d).\n", ret); 730e399441dSJames Smart return ret; 731e399441dSJames Smart } 732e399441dSJames Smart 733e399441dSJames Smart /* ACC or RJT payload ? */ 734e399441dSJames Smart if (rjt->w0.ls_cmd == FCNVME_LS_RJT) 735e399441dSJames Smart return -ENXIO; 736e399441dSJames Smart 737e399441dSJames Smart return 0; 738e399441dSJames Smart } 739e399441dSJames Smart 740e399441dSJames Smart static void 741e399441dSJames Smart nvme_fc_send_ls_req_async(struct nvme_fc_ctrl *ctrl, 742e399441dSJames Smart struct nvmefc_ls_req_op *lsop, 743e399441dSJames Smart void (*done)(struct nvmefc_ls_req *req, int status)) 744e399441dSJames Smart { 745e399441dSJames Smart int ret; 746e399441dSJames Smart 747e399441dSJames Smart ret = __nvme_fc_send_ls_req(ctrl, lsop, done); 748e399441dSJames Smart 749e399441dSJames Smart /* don't wait for completion */ 750e399441dSJames Smart 751e399441dSJames Smart if (ret) 752e399441dSJames Smart done(&lsop->ls_req, ret); 753e399441dSJames Smart } 754e399441dSJames Smart 755e399441dSJames Smart /* Validation Error indexes into the string table below */ 756e399441dSJames Smart enum { 757e399441dSJames Smart VERR_NO_ERROR = 0, 758e399441dSJames Smart VERR_LSACC = 1, 759e399441dSJames Smart VERR_LSDESC_RQST = 2, 760e399441dSJames Smart VERR_LSDESC_RQST_LEN = 3, 761e399441dSJames Smart VERR_ASSOC_ID = 4, 762e399441dSJames Smart VERR_ASSOC_ID_LEN = 5, 763e399441dSJames Smart VERR_CONN_ID = 6, 764e399441dSJames Smart VERR_CONN_ID_LEN = 7, 765e399441dSJames Smart VERR_CR_ASSOC = 8, 766e399441dSJames Smart VERR_CR_ASSOC_ACC_LEN = 9, 767e399441dSJames Smart VERR_CR_CONN = 10, 768e399441dSJames Smart VERR_CR_CONN_ACC_LEN = 11, 769e399441dSJames Smart VERR_DISCONN = 12, 770e399441dSJames Smart VERR_DISCONN_ACC_LEN = 13, 771e399441dSJames Smart }; 772e399441dSJames Smart 773e399441dSJames Smart static char *validation_errors[] = { 774e399441dSJames Smart "OK", 775e399441dSJames Smart "Not LS_ACC", 776e399441dSJames Smart "Not LSDESC_RQST", 777e399441dSJames Smart "Bad LSDESC_RQST Length", 778e399441dSJames Smart "Not Association ID", 779e399441dSJames Smart "Bad Association ID Length", 780e399441dSJames Smart "Not Connection ID", 781e399441dSJames Smart "Bad Connection ID Length", 782e399441dSJames Smart "Not CR_ASSOC Rqst", 783e399441dSJames Smart "Bad CR_ASSOC ACC Length", 784e399441dSJames Smart "Not CR_CONN Rqst", 785e399441dSJames Smart "Bad CR_CONN ACC Length", 786e399441dSJames Smart "Not Disconnect Rqst", 787e399441dSJames Smart "Bad Disconnect ACC Length", 788e399441dSJames Smart }; 789e399441dSJames Smart 790e399441dSJames Smart static int 791e399441dSJames Smart nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl, 792e399441dSJames Smart struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio) 793e399441dSJames Smart { 794e399441dSJames Smart struct nvmefc_ls_req_op *lsop; 795e399441dSJames Smart struct nvmefc_ls_req *lsreq; 796e399441dSJames Smart struct fcnvme_ls_cr_assoc_rqst *assoc_rqst; 797e399441dSJames Smart struct fcnvme_ls_cr_assoc_acc *assoc_acc; 798e399441dSJames Smart int ret, fcret = 0; 799e399441dSJames Smart 800e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) + 801e399441dSJames Smart ctrl->lport->ops->lsrqst_priv_sz + 802e399441dSJames Smart sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL); 803e399441dSJames Smart if (!lsop) { 804e399441dSJames Smart ret = -ENOMEM; 805e399441dSJames Smart goto out_no_memory; 806e399441dSJames Smart } 807e399441dSJames Smart lsreq = &lsop->ls_req; 808e399441dSJames Smart 809e399441dSJames Smart lsreq->private = (void *)&lsop[1]; 810e399441dSJames Smart assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *) 811e399441dSJames Smart (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz); 812e399441dSJames Smart assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1]; 813e399441dSJames Smart 814e399441dSJames Smart assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION; 815e399441dSJames Smart assoc_rqst->desc_list_len = 816e399441dSJames Smart cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd)); 817e399441dSJames Smart 818e399441dSJames Smart assoc_rqst->assoc_cmd.desc_tag = 819e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD); 820e399441dSJames Smart assoc_rqst->assoc_cmd.desc_len = 821e399441dSJames Smart fcnvme_lsdesc_len( 822e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_assoc_cmd)); 823e399441dSJames Smart 824e399441dSJames Smart assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio); 825e399441dSJames Smart assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize); 826e399441dSJames Smart /* Linux supports only Dynamic controllers */ 827e399441dSJames Smart assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff); 828e399441dSJames Smart memcpy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id, 829e399441dSJames Smart min_t(size_t, FCNVME_ASSOC_HOSTID_LEN, sizeof(uuid_be))); 830e399441dSJames Smart strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn, 831e399441dSJames Smart min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE)); 832e399441dSJames Smart strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn, 833e399441dSJames Smart min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE)); 834e399441dSJames Smart 835e399441dSJames Smart lsop->queue = queue; 836e399441dSJames Smart lsreq->rqstaddr = assoc_rqst; 837e399441dSJames Smart lsreq->rqstlen = sizeof(*assoc_rqst); 838e399441dSJames Smart lsreq->rspaddr = assoc_acc; 839e399441dSJames Smart lsreq->rsplen = sizeof(*assoc_acc); 840e399441dSJames Smart lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC; 841e399441dSJames Smart 842e399441dSJames Smart ret = nvme_fc_send_ls_req(ctrl, lsop); 843e399441dSJames Smart if (ret) 844e399441dSJames Smart goto out_free_buffer; 845e399441dSJames Smart 846e399441dSJames Smart /* process connect LS completion */ 847e399441dSJames Smart 848e399441dSJames Smart /* validate the ACC response */ 849e399441dSJames Smart if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC) 850e399441dSJames Smart fcret = VERR_LSACC; 851e399441dSJames Smart if (assoc_acc->hdr.desc_list_len != 852e399441dSJames Smart fcnvme_lsdesc_len( 853e399441dSJames Smart sizeof(struct fcnvme_ls_cr_assoc_acc))) 854e399441dSJames Smart fcret = VERR_CR_ASSOC_ACC_LEN; 855e399441dSJames Smart if (assoc_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST)) 856e399441dSJames Smart fcret = VERR_LSDESC_RQST; 857e399441dSJames Smart else if (assoc_acc->hdr.rqst.desc_len != 858e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst))) 859e399441dSJames Smart fcret = VERR_LSDESC_RQST_LEN; 860e399441dSJames Smart else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION) 861e399441dSJames Smart fcret = VERR_CR_ASSOC; 862e399441dSJames Smart else if (assoc_acc->associd.desc_tag != 863e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_ASSOC_ID)) 864e399441dSJames Smart fcret = VERR_ASSOC_ID; 865e399441dSJames Smart else if (assoc_acc->associd.desc_len != 866e399441dSJames Smart fcnvme_lsdesc_len( 867e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id))) 868e399441dSJames Smart fcret = VERR_ASSOC_ID_LEN; 869e399441dSJames Smart else if (assoc_acc->connectid.desc_tag != 870e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CONN_ID)) 871e399441dSJames Smart fcret = VERR_CONN_ID; 872e399441dSJames Smart else if (assoc_acc->connectid.desc_len != 873e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id))) 874e399441dSJames Smart fcret = VERR_CONN_ID_LEN; 875e399441dSJames Smart 876e399441dSJames Smart if (fcret) { 877e399441dSJames Smart ret = -EBADF; 878e399441dSJames Smart dev_err(ctrl->dev, 879e399441dSJames Smart "q %d connect failed: %s\n", 880e399441dSJames Smart queue->qnum, validation_errors[fcret]); 881e399441dSJames Smart } else { 882e399441dSJames Smart ctrl->association_id = 883e399441dSJames Smart be64_to_cpu(assoc_acc->associd.association_id); 884e399441dSJames Smart queue->connection_id = 885e399441dSJames Smart be64_to_cpu(assoc_acc->connectid.connection_id); 886e399441dSJames Smart set_bit(NVME_FC_Q_CONNECTED, &queue->flags); 887e399441dSJames Smart } 888e399441dSJames Smart 889e399441dSJames Smart out_free_buffer: 890e399441dSJames Smart kfree(lsop); 891e399441dSJames Smart out_no_memory: 892e399441dSJames Smart if (ret) 893e399441dSJames Smart dev_err(ctrl->dev, 894e399441dSJames Smart "queue %d connect admin queue failed (%d).\n", 895e399441dSJames Smart queue->qnum, ret); 896e399441dSJames Smart return ret; 897e399441dSJames Smart } 898e399441dSJames Smart 899e399441dSJames Smart static int 900e399441dSJames Smart nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue, 901e399441dSJames Smart u16 qsize, u16 ersp_ratio) 902e399441dSJames Smart { 903e399441dSJames Smart struct nvmefc_ls_req_op *lsop; 904e399441dSJames Smart struct nvmefc_ls_req *lsreq; 905e399441dSJames Smart struct fcnvme_ls_cr_conn_rqst *conn_rqst; 906e399441dSJames Smart struct fcnvme_ls_cr_conn_acc *conn_acc; 907e399441dSJames Smart int ret, fcret = 0; 908e399441dSJames Smart 909e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) + 910e399441dSJames Smart ctrl->lport->ops->lsrqst_priv_sz + 911e399441dSJames Smart sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL); 912e399441dSJames Smart if (!lsop) { 913e399441dSJames Smart ret = -ENOMEM; 914e399441dSJames Smart goto out_no_memory; 915e399441dSJames Smart } 916e399441dSJames Smart lsreq = &lsop->ls_req; 917e399441dSJames Smart 918e399441dSJames Smart lsreq->private = (void *)&lsop[1]; 919e399441dSJames Smart conn_rqst = (struct fcnvme_ls_cr_conn_rqst *) 920e399441dSJames Smart (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz); 921e399441dSJames Smart conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1]; 922e399441dSJames Smart 923e399441dSJames Smart conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION; 924e399441dSJames Smart conn_rqst->desc_list_len = cpu_to_be32( 925e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id) + 926e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_conn_cmd)); 927e399441dSJames Smart 928e399441dSJames Smart conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID); 929e399441dSJames Smart conn_rqst->associd.desc_len = 930e399441dSJames Smart fcnvme_lsdesc_len( 931e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id)); 932e399441dSJames Smart conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id); 933e399441dSJames Smart conn_rqst->connect_cmd.desc_tag = 934e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD); 935e399441dSJames Smart conn_rqst->connect_cmd.desc_len = 936e399441dSJames Smart fcnvme_lsdesc_len( 937e399441dSJames Smart sizeof(struct fcnvme_lsdesc_cr_conn_cmd)); 938e399441dSJames Smart conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio); 939e399441dSJames Smart conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum); 940e399441dSJames Smart conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize); 941e399441dSJames Smart 942e399441dSJames Smart lsop->queue = queue; 943e399441dSJames Smart lsreq->rqstaddr = conn_rqst; 944e399441dSJames Smart lsreq->rqstlen = sizeof(*conn_rqst); 945e399441dSJames Smart lsreq->rspaddr = conn_acc; 946e399441dSJames Smart lsreq->rsplen = sizeof(*conn_acc); 947e399441dSJames Smart lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC; 948e399441dSJames Smart 949e399441dSJames Smart ret = nvme_fc_send_ls_req(ctrl, lsop); 950e399441dSJames Smart if (ret) 951e399441dSJames Smart goto out_free_buffer; 952e399441dSJames Smart 953e399441dSJames Smart /* process connect LS completion */ 954e399441dSJames Smart 955e399441dSJames Smart /* validate the ACC response */ 956e399441dSJames Smart if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC) 957e399441dSJames Smart fcret = VERR_LSACC; 958e399441dSJames Smart if (conn_acc->hdr.desc_list_len != 959e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc))) 960e399441dSJames Smart fcret = VERR_CR_CONN_ACC_LEN; 961e399441dSJames Smart if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST)) 962e399441dSJames Smart fcret = VERR_LSDESC_RQST; 963e399441dSJames Smart else if (conn_acc->hdr.rqst.desc_len != 964e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst))) 965e399441dSJames Smart fcret = VERR_LSDESC_RQST_LEN; 966e399441dSJames Smart else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION) 967e399441dSJames Smart fcret = VERR_CR_CONN; 968e399441dSJames Smart else if (conn_acc->connectid.desc_tag != 969e399441dSJames Smart cpu_to_be32(FCNVME_LSDESC_CONN_ID)) 970e399441dSJames Smart fcret = VERR_CONN_ID; 971e399441dSJames Smart else if (conn_acc->connectid.desc_len != 972e399441dSJames Smart fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id))) 973e399441dSJames Smart fcret = VERR_CONN_ID_LEN; 974e399441dSJames Smart 975e399441dSJames Smart if (fcret) { 976e399441dSJames Smart ret = -EBADF; 977e399441dSJames Smart dev_err(ctrl->dev, 978e399441dSJames Smart "q %d connect failed: %s\n", 979e399441dSJames Smart queue->qnum, validation_errors[fcret]); 980e399441dSJames Smart } else { 981e399441dSJames Smart queue->connection_id = 982e399441dSJames Smart be64_to_cpu(conn_acc->connectid.connection_id); 983e399441dSJames Smart set_bit(NVME_FC_Q_CONNECTED, &queue->flags); 984e399441dSJames Smart } 985e399441dSJames Smart 986e399441dSJames Smart out_free_buffer: 987e399441dSJames Smart kfree(lsop); 988e399441dSJames Smart out_no_memory: 989e399441dSJames Smart if (ret) 990e399441dSJames Smart dev_err(ctrl->dev, 991e399441dSJames Smart "queue %d connect command failed (%d).\n", 992e399441dSJames Smart queue->qnum, ret); 993e399441dSJames Smart return ret; 994e399441dSJames Smart } 995e399441dSJames Smart 996e399441dSJames Smart static void 997e399441dSJames Smart nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status) 998e399441dSJames Smart { 999e399441dSJames Smart struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq); 1000e399441dSJames Smart struct nvme_fc_ctrl *ctrl = lsop->ctrl; 1001e399441dSJames Smart 1002e399441dSJames Smart __nvme_fc_finish_ls_req(ctrl, lsop); 1003e399441dSJames Smart 1004e399441dSJames Smart if (status) 1005e399441dSJames Smart dev_err(ctrl->dev, 1006e399441dSJames Smart "disconnect assoc ls request command failed (%d).\n", 1007e399441dSJames Smart status); 1008e399441dSJames Smart 1009e399441dSJames Smart /* fc-nvme iniator doesn't care about success or failure of cmd */ 1010e399441dSJames Smart 1011e399441dSJames Smart kfree(lsop); 1012e399441dSJames Smart } 1013e399441dSJames Smart 1014e399441dSJames Smart /* 1015e399441dSJames Smart * This routine sends a FC-NVME LS to disconnect (aka terminate) 1016e399441dSJames Smart * the FC-NVME Association. Terminating the association also 1017e399441dSJames Smart * terminates the FC-NVME connections (per queue, both admin and io 1018e399441dSJames Smart * queues) that are part of the association. E.g. things are torn 1019e399441dSJames Smart * down, and the related FC-NVME Association ID and Connection IDs 1020e399441dSJames Smart * become invalid. 1021e399441dSJames Smart * 1022e399441dSJames Smart * The behavior of the fc-nvme initiator is such that it's 1023e399441dSJames Smart * understanding of the association and connections will implicitly 1024e399441dSJames Smart * be torn down. The action is implicit as it may be due to a loss of 1025e399441dSJames Smart * connectivity with the fc-nvme target, so you may never get a 1026e399441dSJames Smart * response even if you tried. As such, the action of this routine 1027e399441dSJames Smart * is to asynchronously send the LS, ignore any results of the LS, and 1028e399441dSJames Smart * continue on with terminating the association. If the fc-nvme target 1029e399441dSJames Smart * is present and receives the LS, it too can tear down. 1030e399441dSJames Smart */ 1031e399441dSJames Smart static void 1032e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl) 1033e399441dSJames Smart { 1034e399441dSJames Smart struct fcnvme_ls_disconnect_rqst *discon_rqst; 1035e399441dSJames Smart struct fcnvme_ls_disconnect_acc *discon_acc; 1036e399441dSJames Smart struct nvmefc_ls_req_op *lsop; 1037e399441dSJames Smart struct nvmefc_ls_req *lsreq; 1038e399441dSJames Smart 1039e399441dSJames Smart lsop = kzalloc((sizeof(*lsop) + 1040e399441dSJames Smart ctrl->lport->ops->lsrqst_priv_sz + 1041e399441dSJames Smart sizeof(*discon_rqst) + sizeof(*discon_acc)), 1042e399441dSJames Smart GFP_KERNEL); 1043e399441dSJames Smart if (!lsop) 1044e399441dSJames Smart /* couldn't sent it... too bad */ 1045e399441dSJames Smart return; 1046e399441dSJames Smart 1047e399441dSJames Smart lsreq = &lsop->ls_req; 1048e399441dSJames Smart 1049e399441dSJames Smart lsreq->private = (void *)&lsop[1]; 1050e399441dSJames Smart discon_rqst = (struct fcnvme_ls_disconnect_rqst *) 1051e399441dSJames Smart (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz); 1052e399441dSJames Smart discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1]; 1053e399441dSJames Smart 1054e399441dSJames Smart discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT; 1055e399441dSJames Smart discon_rqst->desc_list_len = cpu_to_be32( 1056e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id) + 1057e399441dSJames Smart sizeof(struct fcnvme_lsdesc_disconn_cmd)); 1058e399441dSJames Smart 1059e399441dSJames Smart discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID); 1060e399441dSJames Smart discon_rqst->associd.desc_len = 1061e399441dSJames Smart fcnvme_lsdesc_len( 1062e399441dSJames Smart sizeof(struct fcnvme_lsdesc_assoc_id)); 1063e399441dSJames Smart 1064e399441dSJames Smart discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id); 1065e399441dSJames Smart 1066e399441dSJames Smart discon_rqst->discon_cmd.desc_tag = cpu_to_be32( 1067e399441dSJames Smart FCNVME_LSDESC_DISCONN_CMD); 1068e399441dSJames Smart discon_rqst->discon_cmd.desc_len = 1069e399441dSJames Smart fcnvme_lsdesc_len( 1070e399441dSJames Smart sizeof(struct fcnvme_lsdesc_disconn_cmd)); 1071e399441dSJames Smart discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION; 1072e399441dSJames Smart discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id); 1073e399441dSJames Smart 1074e399441dSJames Smart lsreq->rqstaddr = discon_rqst; 1075e399441dSJames Smart lsreq->rqstlen = sizeof(*discon_rqst); 1076e399441dSJames Smart lsreq->rspaddr = discon_acc; 1077e399441dSJames Smart lsreq->rsplen = sizeof(*discon_acc); 1078e399441dSJames Smart lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC; 1079e399441dSJames Smart 1080e399441dSJames Smart nvme_fc_send_ls_req_async(ctrl, lsop, nvme_fc_disconnect_assoc_done); 1081e399441dSJames Smart 1082e399441dSJames Smart /* only meaningful part to terminating the association */ 1083e399441dSJames Smart ctrl->association_id = 0; 1084e399441dSJames Smart } 1085e399441dSJames Smart 1086e399441dSJames Smart 1087e399441dSJames Smart /* *********************** NVME Ctrl Routines **************************** */ 1088e399441dSJames Smart 1089e399441dSJames Smart 1090e399441dSJames Smart static int 1091e399441dSJames Smart nvme_fc_reinit_request(void *data, struct request *rq) 1092e399441dSJames Smart { 1093e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1094e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 1095e399441dSJames Smart 1096e399441dSJames Smart memset(cmdiu, 0, sizeof(*cmdiu)); 1097e399441dSJames Smart cmdiu->scsi_id = NVME_CMD_SCSI_ID; 1098e399441dSJames Smart cmdiu->fc_id = NVME_CMD_FC_ID; 1099e399441dSJames Smart cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32)); 1100e399441dSJames Smart memset(&op->rsp_iu, 0, sizeof(op->rsp_iu)); 1101e399441dSJames Smart 1102e399441dSJames Smart return 0; 1103e399441dSJames Smart } 1104e399441dSJames Smart 1105e399441dSJames Smart static void 1106e399441dSJames Smart __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl, 1107e399441dSJames Smart struct nvme_fc_fcp_op *op) 1108e399441dSJames Smart { 1109e399441dSJames Smart fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma, 1110e399441dSJames Smart sizeof(op->rsp_iu), DMA_FROM_DEVICE); 1111e399441dSJames Smart fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma, 1112e399441dSJames Smart sizeof(op->cmd_iu), DMA_TO_DEVICE); 1113e399441dSJames Smart 1114e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_UNINIT); 1115e399441dSJames Smart } 1116e399441dSJames Smart 1117e399441dSJames Smart static void 1118e399441dSJames Smart nvme_fc_exit_request(void *data, struct request *rq, 1119e399441dSJames Smart unsigned int hctx_idx, unsigned int rq_idx) 1120e399441dSJames Smart { 1121e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1122e399441dSJames Smart 1123e399441dSJames Smart return __nvme_fc_exit_request(data, op); 1124e399441dSJames Smart } 1125e399441dSJames Smart 1126e399441dSJames Smart static void 1127e399441dSJames Smart nvme_fc_exit_aen_ops(struct nvme_fc_ctrl *ctrl) 1128e399441dSJames Smart { 1129e399441dSJames Smart struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops; 1130e399441dSJames Smart int i; 1131e399441dSJames Smart 1132e399441dSJames Smart for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) { 1133e399441dSJames Smart if (atomic_read(&aen_op->state) == FCPOP_STATE_UNINIT) 1134e399441dSJames Smart continue; 1135e399441dSJames Smart __nvme_fc_exit_request(ctrl, aen_op); 1136e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 1137e399441dSJames Smart } 1138e399441dSJames Smart } 1139e399441dSJames Smart 1140e399441dSJames Smart void 1141e399441dSJames Smart nvme_fc_fcpio_done(struct nvmefc_fcp_req *req) 1142e399441dSJames Smart { 1143e399441dSJames Smart struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req); 1144e399441dSJames Smart struct request *rq = op->rq; 1145e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req; 1146e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl; 1147e399441dSJames Smart struct nvme_fc_queue *queue = op->queue; 1148e399441dSJames Smart struct nvme_completion *cqe = &op->rsp_iu.cqe; 1149e399441dSJames Smart u16 status; 1150e399441dSJames Smart 1151e399441dSJames Smart /* 1152e399441dSJames Smart * WARNING: 1153e399441dSJames Smart * The current linux implementation of a nvme controller 1154e399441dSJames Smart * allocates a single tag set for all io queues and sizes 1155e399441dSJames Smart * the io queues to fully hold all possible tags. Thus, the 1156e399441dSJames Smart * implementation does not reference or care about the sqhd 1157e399441dSJames Smart * value as it never needs to use the sqhd/sqtail pointers 1158e399441dSJames Smart * for submission pacing. 1159e399441dSJames Smart * 1160e399441dSJames Smart * This affects the FC-NVME implementation in two ways: 1161e399441dSJames Smart * 1) As the value doesn't matter, we don't need to waste 1162e399441dSJames Smart * cycles extracting it from ERSPs and stamping it in the 1163e399441dSJames Smart * cases where the transport fabricates CQEs on successful 1164e399441dSJames Smart * completions. 1165e399441dSJames Smart * 2) The FC-NVME implementation requires that delivery of 1166e399441dSJames Smart * ERSP completions are to go back to the nvme layer in order 1167e399441dSJames Smart * relative to the rsn, such that the sqhd value will always 1168e399441dSJames Smart * be "in order" for the nvme layer. As the nvme layer in 1169e399441dSJames Smart * linux doesn't care about sqhd, there's no need to return 1170e399441dSJames Smart * them in order. 1171e399441dSJames Smart * 1172e399441dSJames Smart * Additionally: 1173e399441dSJames Smart * As the core nvme layer in linux currently does not look at 1174e399441dSJames Smart * every field in the cqe - in cases where the FC transport must 1175e399441dSJames Smart * fabricate a CQE, the following fields will not be set as they 1176e399441dSJames Smart * are not referenced: 1177e399441dSJames Smart * cqe.sqid, cqe.sqhd, cqe.command_id 1178e399441dSJames Smart */ 1179e399441dSJames Smart 1180e399441dSJames Smart fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma, 1181e399441dSJames Smart sizeof(op->rsp_iu), DMA_FROM_DEVICE); 1182e399441dSJames Smart 1183e399441dSJames Smart if (atomic_read(&op->state) == FCPOP_STATE_ABORTED) 1184e399441dSJames Smart status = NVME_SC_ABORT_REQ | NVME_SC_DNR; 1185e399441dSJames Smart else 1186e399441dSJames Smart status = freq->status; 1187e399441dSJames Smart 1188e399441dSJames Smart /* 1189e399441dSJames Smart * For the linux implementation, if we have an unsuccesful 1190e399441dSJames Smart * status, they blk-mq layer can typically be called with the 1191e399441dSJames Smart * non-zero status and the content of the cqe isn't important. 1192e399441dSJames Smart */ 1193e399441dSJames Smart if (status) 1194e399441dSJames Smart goto done; 1195e399441dSJames Smart 1196e399441dSJames Smart /* 1197e399441dSJames Smart * command completed successfully relative to the wire 1198e399441dSJames Smart * protocol. However, validate anything received and 1199e399441dSJames Smart * extract the status and result from the cqe (create it 1200e399441dSJames Smart * where necessary). 1201e399441dSJames Smart */ 1202e399441dSJames Smart 1203e399441dSJames Smart switch (freq->rcv_rsplen) { 1204e399441dSJames Smart 1205e399441dSJames Smart case 0: 1206e399441dSJames Smart case NVME_FC_SIZEOF_ZEROS_RSP: 1207e399441dSJames Smart /* 1208e399441dSJames Smart * No response payload or 12 bytes of payload (which 1209e399441dSJames Smart * should all be zeros) are considered successful and 1210e399441dSJames Smart * no payload in the CQE by the transport. 1211e399441dSJames Smart */ 1212e399441dSJames Smart if (freq->transferred_length != 1213e399441dSJames Smart be32_to_cpu(op->cmd_iu.data_len)) { 1214e399441dSJames Smart status = -EIO; 1215e399441dSJames Smart goto done; 1216e399441dSJames Smart } 1217e399441dSJames Smart op->nreq.result.u64 = 0; 1218e399441dSJames Smart break; 1219e399441dSJames Smart 1220e399441dSJames Smart case sizeof(struct nvme_fc_ersp_iu): 1221e399441dSJames Smart /* 1222e399441dSJames Smart * The ERSP IU contains a full completion with CQE. 1223e399441dSJames Smart * Validate ERSP IU and look at cqe. 1224e399441dSJames Smart */ 1225e399441dSJames Smart if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) != 1226e399441dSJames Smart (freq->rcv_rsplen / 4) || 1227e399441dSJames Smart be32_to_cpu(op->rsp_iu.xfrd_len) != 1228e399441dSJames Smart freq->transferred_length || 1229e399441dSJames Smart op->rqno != le16_to_cpu(cqe->command_id))) { 1230e399441dSJames Smart status = -EIO; 1231e399441dSJames Smart goto done; 1232e399441dSJames Smart } 1233e399441dSJames Smart op->nreq.result = cqe->result; 1234e399441dSJames Smart status = le16_to_cpu(cqe->status) >> 1; 1235e399441dSJames Smart break; 1236e399441dSJames Smart 1237e399441dSJames Smart default: 1238e399441dSJames Smart status = -EIO; 1239e399441dSJames Smart goto done; 1240e399441dSJames Smart } 1241e399441dSJames Smart 1242e399441dSJames Smart done: 1243e399441dSJames Smart if (!queue->qnum && op->rqno >= AEN_CMDID_BASE) { 1244e399441dSJames Smart nvme_complete_async_event(&queue->ctrl->ctrl, status, 1245e399441dSJames Smart &op->nreq.result); 1246e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 1247e399441dSJames Smart return; 1248e399441dSJames Smart } 1249e399441dSJames Smart 1250e399441dSJames Smart blk_mq_complete_request(rq, status); 1251e399441dSJames Smart } 1252e399441dSJames Smart 1253e399441dSJames Smart static int 1254e399441dSJames Smart __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl, 1255e399441dSJames Smart struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op, 1256e399441dSJames Smart struct request *rq, u32 rqno) 1257e399441dSJames Smart { 1258e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 1259e399441dSJames Smart int ret = 0; 1260e399441dSJames Smart 1261e399441dSJames Smart memset(op, 0, sizeof(*op)); 1262e399441dSJames Smart op->fcp_req.cmdaddr = &op->cmd_iu; 1263e399441dSJames Smart op->fcp_req.cmdlen = sizeof(op->cmd_iu); 1264e399441dSJames Smart op->fcp_req.rspaddr = &op->rsp_iu; 1265e399441dSJames Smart op->fcp_req.rsplen = sizeof(op->rsp_iu); 1266e399441dSJames Smart op->fcp_req.done = nvme_fc_fcpio_done; 1267e399441dSJames Smart op->fcp_req.first_sgl = (struct scatterlist *)&op[1]; 1268e399441dSJames Smart op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE]; 1269e399441dSJames Smart op->ctrl = ctrl; 1270e399441dSJames Smart op->queue = queue; 1271e399441dSJames Smart op->rq = rq; 1272e399441dSJames Smart op->rqno = rqno; 1273e399441dSJames Smart 1274e399441dSJames Smart cmdiu->scsi_id = NVME_CMD_SCSI_ID; 1275e399441dSJames Smart cmdiu->fc_id = NVME_CMD_FC_ID; 1276e399441dSJames Smart cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32)); 1277e399441dSJames Smart 1278e399441dSJames Smart op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev, 1279e399441dSJames Smart &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE); 1280e399441dSJames Smart if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) { 1281e399441dSJames Smart dev_err(ctrl->dev, 1282e399441dSJames Smart "FCP Op failed - cmdiu dma mapping failed.\n"); 1283e399441dSJames Smart ret = EFAULT; 1284e399441dSJames Smart goto out_on_error; 1285e399441dSJames Smart } 1286e399441dSJames Smart 1287e399441dSJames Smart op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev, 1288e399441dSJames Smart &op->rsp_iu, sizeof(op->rsp_iu), 1289e399441dSJames Smart DMA_FROM_DEVICE); 1290e399441dSJames Smart if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) { 1291e399441dSJames Smart dev_err(ctrl->dev, 1292e399441dSJames Smart "FCP Op failed - rspiu dma mapping failed.\n"); 1293e399441dSJames Smart ret = EFAULT; 1294e399441dSJames Smart } 1295e399441dSJames Smart 1296e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_IDLE); 1297e399441dSJames Smart out_on_error: 1298e399441dSJames Smart return ret; 1299e399441dSJames Smart } 1300e399441dSJames Smart 1301e399441dSJames Smart static int 1302e399441dSJames Smart nvme_fc_init_request(void *data, struct request *rq, 1303e399441dSJames Smart unsigned int hctx_idx, unsigned int rq_idx, 1304e399441dSJames Smart unsigned int numa_node) 1305e399441dSJames Smart { 1306e399441dSJames Smart struct nvme_fc_ctrl *ctrl = data; 1307e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1308e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[hctx_idx+1]; 1309e399441dSJames Smart 1310e399441dSJames Smart return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++); 1311e399441dSJames Smart } 1312e399441dSJames Smart 1313e399441dSJames Smart static int 1314e399441dSJames Smart nvme_fc_init_admin_request(void *data, struct request *rq, 1315e399441dSJames Smart unsigned int hctx_idx, unsigned int rq_idx, 1316e399441dSJames Smart unsigned int numa_node) 1317e399441dSJames Smart { 1318e399441dSJames Smart struct nvme_fc_ctrl *ctrl = data; 1319e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1320e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[0]; 1321e399441dSJames Smart 1322e399441dSJames Smart return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++); 1323e399441dSJames Smart } 1324e399441dSJames Smart 1325e399441dSJames Smart static int 1326e399441dSJames Smart nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl) 1327e399441dSJames Smart { 1328e399441dSJames Smart struct nvme_fc_fcp_op *aen_op; 1329e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu; 1330e399441dSJames Smart struct nvme_command *sqe; 1331e399441dSJames Smart int i, ret; 1332e399441dSJames Smart 1333e399441dSJames Smart aen_op = ctrl->aen_ops; 1334e399441dSJames Smart for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) { 1335e399441dSJames Smart cmdiu = &aen_op->cmd_iu; 1336e399441dSJames Smart sqe = &cmdiu->sqe; 1337e399441dSJames Smart ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0], 1338e399441dSJames Smart aen_op, (struct request *)NULL, 1339e399441dSJames Smart (AEN_CMDID_BASE + i)); 1340e399441dSJames Smart if (ret) 1341e399441dSJames Smart return ret; 1342e399441dSJames Smart 1343e399441dSJames Smart memset(sqe, 0, sizeof(*sqe)); 1344e399441dSJames Smart sqe->common.opcode = nvme_admin_async_event; 1345e399441dSJames Smart sqe->common.command_id = AEN_CMDID_BASE + i; 1346e399441dSJames Smart } 1347e399441dSJames Smart return 0; 1348e399441dSJames Smart } 1349e399441dSJames Smart 1350e399441dSJames Smart 1351e399441dSJames Smart static inline void 1352e399441dSJames Smart __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl, 1353e399441dSJames Smart unsigned int qidx) 1354e399441dSJames Smart { 1355e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[qidx]; 1356e399441dSJames Smart 1357e399441dSJames Smart hctx->driver_data = queue; 1358e399441dSJames Smart queue->hctx = hctx; 1359e399441dSJames Smart } 1360e399441dSJames Smart 1361e399441dSJames Smart static int 1362e399441dSJames Smart nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, 1363e399441dSJames Smart unsigned int hctx_idx) 1364e399441dSJames Smart { 1365e399441dSJames Smart struct nvme_fc_ctrl *ctrl = data; 1366e399441dSJames Smart 1367e399441dSJames Smart __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1); 1368e399441dSJames Smart 1369e399441dSJames Smart return 0; 1370e399441dSJames Smart } 1371e399441dSJames Smart 1372e399441dSJames Smart static int 1373e399441dSJames Smart nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, 1374e399441dSJames Smart unsigned int hctx_idx) 1375e399441dSJames Smart { 1376e399441dSJames Smart struct nvme_fc_ctrl *ctrl = data; 1377e399441dSJames Smart 1378e399441dSJames Smart __nvme_fc_init_hctx(hctx, ctrl, hctx_idx); 1379e399441dSJames Smart 1380e399441dSJames Smart return 0; 1381e399441dSJames Smart } 1382e399441dSJames Smart 1383e399441dSJames Smart static void 1384e399441dSJames Smart nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size) 1385e399441dSJames Smart { 1386e399441dSJames Smart struct nvme_fc_queue *queue; 1387e399441dSJames Smart 1388e399441dSJames Smart queue = &ctrl->queues[idx]; 1389e399441dSJames Smart memset(queue, 0, sizeof(*queue)); 1390e399441dSJames Smart queue->ctrl = ctrl; 1391e399441dSJames Smart queue->qnum = idx; 1392e399441dSJames Smart atomic_set(&queue->csn, 1); 1393e399441dSJames Smart queue->dev = ctrl->dev; 1394e399441dSJames Smart 1395e399441dSJames Smart if (idx > 0) 1396e399441dSJames Smart queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; 1397e399441dSJames Smart else 1398e399441dSJames Smart queue->cmnd_capsule_len = sizeof(struct nvme_command); 1399e399441dSJames Smart 1400e399441dSJames Smart queue->queue_size = queue_size; 1401e399441dSJames Smart 1402e399441dSJames Smart /* 1403e399441dSJames Smart * Considered whether we should allocate buffers for all SQEs 1404e399441dSJames Smart * and CQEs and dma map them - mapping their respective entries 1405e399441dSJames Smart * into the request structures (kernel vm addr and dma address) 1406e399441dSJames Smart * thus the driver could use the buffers/mappings directly. 1407e399441dSJames Smart * It only makes sense if the LLDD would use them for its 1408e399441dSJames Smart * messaging api. It's very unlikely most adapter api's would use 1409e399441dSJames Smart * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload 1410e399441dSJames Smart * structures were used instead. 1411e399441dSJames Smart */ 1412e399441dSJames Smart } 1413e399441dSJames Smart 1414e399441dSJames Smart /* 1415e399441dSJames Smart * This routine terminates a queue at the transport level. 1416e399441dSJames Smart * The transport has already ensured that all outstanding ios on 1417e399441dSJames Smart * the queue have been terminated. 1418e399441dSJames Smart * The transport will send a Disconnect LS request to terminate 1419e399441dSJames Smart * the queue's connection. Termination of the admin queue will also 1420e399441dSJames Smart * terminate the association at the target. 1421e399441dSJames Smart */ 1422e399441dSJames Smart static void 1423e399441dSJames Smart nvme_fc_free_queue(struct nvme_fc_queue *queue) 1424e399441dSJames Smart { 1425e399441dSJames Smart if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags)) 1426e399441dSJames Smart return; 1427e399441dSJames Smart 1428e399441dSJames Smart /* 1429e399441dSJames Smart * Current implementation never disconnects a single queue. 1430e399441dSJames Smart * It always terminates a whole association. So there is never 1431e399441dSJames Smart * a disconnect(queue) LS sent to the target. 1432e399441dSJames Smart */ 1433e399441dSJames Smart 1434e399441dSJames Smart queue->connection_id = 0; 1435e399441dSJames Smart clear_bit(NVME_FC_Q_CONNECTED, &queue->flags); 1436e399441dSJames Smart } 1437e399441dSJames Smart 1438e399441dSJames Smart static void 1439e399441dSJames Smart __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl, 1440e399441dSJames Smart struct nvme_fc_queue *queue, unsigned int qidx) 1441e399441dSJames Smart { 1442e399441dSJames Smart if (ctrl->lport->ops->delete_queue) 1443e399441dSJames Smart ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx, 1444e399441dSJames Smart queue->lldd_handle); 1445e399441dSJames Smart queue->lldd_handle = NULL; 1446e399441dSJames Smart } 1447e399441dSJames Smart 1448e399441dSJames Smart static void 1449e399441dSJames Smart nvme_fc_destroy_admin_queue(struct nvme_fc_ctrl *ctrl) 1450e399441dSJames Smart { 1451e399441dSJames Smart __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0); 1452e399441dSJames Smart blk_cleanup_queue(ctrl->ctrl.admin_q); 1453e399441dSJames Smart blk_mq_free_tag_set(&ctrl->admin_tag_set); 1454e399441dSJames Smart nvme_fc_free_queue(&ctrl->queues[0]); 1455e399441dSJames Smart } 1456e399441dSJames Smart 1457e399441dSJames Smart static void 1458e399441dSJames Smart nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl) 1459e399441dSJames Smart { 1460e399441dSJames Smart int i; 1461e399441dSJames Smart 1462e399441dSJames Smart for (i = 1; i < ctrl->queue_count; i++) 1463e399441dSJames Smart nvme_fc_free_queue(&ctrl->queues[i]); 1464e399441dSJames Smart } 1465e399441dSJames Smart 1466e399441dSJames Smart static int 1467e399441dSJames Smart __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl, 1468e399441dSJames Smart struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize) 1469e399441dSJames Smart { 1470e399441dSJames Smart int ret = 0; 1471e399441dSJames Smart 1472e399441dSJames Smart queue->lldd_handle = NULL; 1473e399441dSJames Smart if (ctrl->lport->ops->create_queue) 1474e399441dSJames Smart ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport, 1475e399441dSJames Smart qidx, qsize, &queue->lldd_handle); 1476e399441dSJames Smart 1477e399441dSJames Smart return ret; 1478e399441dSJames Smart } 1479e399441dSJames Smart 1480e399441dSJames Smart static void 1481e399441dSJames Smart nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl) 1482e399441dSJames Smart { 1483e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[ctrl->queue_count - 1]; 1484e399441dSJames Smart int i; 1485e399441dSJames Smart 1486e399441dSJames Smart for (i = ctrl->queue_count - 1; i >= 1; i--, queue--) 1487e399441dSJames Smart __nvme_fc_delete_hw_queue(ctrl, queue, i); 1488e399441dSJames Smart } 1489e399441dSJames Smart 1490e399441dSJames Smart static int 1491e399441dSJames Smart nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize) 1492e399441dSJames Smart { 1493e399441dSJames Smart struct nvme_fc_queue *queue = &ctrl->queues[1]; 149417a1ec08SJohannes Thumshirn int i, ret; 1495e399441dSJames Smart 1496e399441dSJames Smart for (i = 1; i < ctrl->queue_count; i++, queue++) { 1497e399441dSJames Smart ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize); 149817a1ec08SJohannes Thumshirn if (ret) 149917a1ec08SJohannes Thumshirn goto delete_queues; 1500e399441dSJames Smart } 1501e399441dSJames Smart 1502e399441dSJames Smart return 0; 150317a1ec08SJohannes Thumshirn 150417a1ec08SJohannes Thumshirn delete_queues: 150517a1ec08SJohannes Thumshirn for (; i >= 0; i--) 150617a1ec08SJohannes Thumshirn __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i); 150717a1ec08SJohannes Thumshirn return ret; 1508e399441dSJames Smart } 1509e399441dSJames Smart 1510e399441dSJames Smart static int 1511e399441dSJames Smart nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize) 1512e399441dSJames Smart { 1513e399441dSJames Smart int i, ret = 0; 1514e399441dSJames Smart 1515e399441dSJames Smart for (i = 1; i < ctrl->queue_count; i++) { 1516e399441dSJames Smart ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize, 1517e399441dSJames Smart (qsize / 5)); 1518e399441dSJames Smart if (ret) 1519e399441dSJames Smart break; 1520e399441dSJames Smart ret = nvmf_connect_io_queue(&ctrl->ctrl, i); 1521e399441dSJames Smart if (ret) 1522e399441dSJames Smart break; 1523e399441dSJames Smart } 1524e399441dSJames Smart 1525e399441dSJames Smart return ret; 1526e399441dSJames Smart } 1527e399441dSJames Smart 1528e399441dSJames Smart static void 1529e399441dSJames Smart nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl) 1530e399441dSJames Smart { 1531e399441dSJames Smart int i; 1532e399441dSJames Smart 1533e399441dSJames Smart for (i = 1; i < ctrl->queue_count; i++) 1534e399441dSJames Smart nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize); 1535e399441dSJames Smart } 1536e399441dSJames Smart 1537e399441dSJames Smart static void 1538e399441dSJames Smart nvme_fc_ctrl_free(struct kref *ref) 1539e399441dSJames Smart { 1540e399441dSJames Smart struct nvme_fc_ctrl *ctrl = 1541e399441dSJames Smart container_of(ref, struct nvme_fc_ctrl, ref); 1542e399441dSJames Smart unsigned long flags; 1543e399441dSJames Smart 1544e399441dSJames Smart if (ctrl->state != FCCTRL_INIT) { 1545e399441dSJames Smart /* remove from rport list */ 1546e399441dSJames Smart spin_lock_irqsave(&ctrl->rport->lock, flags); 1547e399441dSJames Smart list_del(&ctrl->ctrl_list); 1548e399441dSJames Smart spin_unlock_irqrestore(&ctrl->rport->lock, flags); 1549e399441dSJames Smart } 1550e399441dSJames Smart 1551e399441dSJames Smart put_device(ctrl->dev); 1552e399441dSJames Smart nvme_fc_rport_put(ctrl->rport); 1553e399441dSJames Smart 1554e399441dSJames Smart kfree(ctrl->queues); 1555e399441dSJames Smart ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum); 1556e399441dSJames Smart nvmf_free_options(ctrl->ctrl.opts); 1557e399441dSJames Smart kfree(ctrl); 1558e399441dSJames Smart } 1559e399441dSJames Smart 1560e399441dSJames Smart static void 1561e399441dSJames Smart nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl) 1562e399441dSJames Smart { 1563e399441dSJames Smart kref_put(&ctrl->ref, nvme_fc_ctrl_free); 1564e399441dSJames Smart } 1565e399441dSJames Smart 1566e399441dSJames Smart static int 1567e399441dSJames Smart nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl) 1568e399441dSJames Smart { 1569e399441dSJames Smart return kref_get_unless_zero(&ctrl->ref); 1570e399441dSJames Smart } 1571e399441dSJames Smart 1572e399441dSJames Smart /* 1573e399441dSJames Smart * All accesses from nvme core layer done - can now free the 1574e399441dSJames Smart * controller. Called after last nvme_put_ctrl() call 1575e399441dSJames Smart */ 1576e399441dSJames Smart static void 1577e399441dSJames Smart nvme_fc_free_nvme_ctrl(struct nvme_ctrl *nctrl) 1578e399441dSJames Smart { 1579e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 1580e399441dSJames Smart 1581e399441dSJames Smart WARN_ON(nctrl != &ctrl->ctrl); 1582e399441dSJames Smart 1583e399441dSJames Smart /* 1584e399441dSJames Smart * Tear down the association, which will generate link 1585e399441dSJames Smart * traffic to terminate connections 1586e399441dSJames Smart */ 1587e399441dSJames Smart 1588e399441dSJames Smart if (ctrl->state != FCCTRL_INIT) { 1589e399441dSJames Smart /* send a Disconnect(association) LS to fc-nvme target */ 1590e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(ctrl); 1591e399441dSJames Smart 1592e399441dSJames Smart if (ctrl->ctrl.tagset) { 1593e399441dSJames Smart blk_cleanup_queue(ctrl->ctrl.connect_q); 1594e399441dSJames Smart blk_mq_free_tag_set(&ctrl->tag_set); 1595e399441dSJames Smart nvme_fc_delete_hw_io_queues(ctrl); 1596e399441dSJames Smart nvme_fc_free_io_queues(ctrl); 1597e399441dSJames Smart } 1598e399441dSJames Smart 1599e399441dSJames Smart nvme_fc_exit_aen_ops(ctrl); 1600e399441dSJames Smart 1601e399441dSJames Smart nvme_fc_destroy_admin_queue(ctrl); 1602e399441dSJames Smart } 1603e399441dSJames Smart 1604e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 1605e399441dSJames Smart } 1606e399441dSJames Smart 1607e399441dSJames Smart 1608e399441dSJames Smart static int 1609e399441dSJames Smart __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op) 1610e399441dSJames Smart { 1611e399441dSJames Smart int state; 1612e399441dSJames Smart 1613e399441dSJames Smart state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED); 1614e399441dSJames Smart if (state != FCPOP_STATE_ACTIVE) { 1615e399441dSJames Smart atomic_set(&op->state, state); 1616e399441dSJames Smart return -ECANCELED; /* fail */ 1617e399441dSJames Smart } 1618e399441dSJames Smart 1619e399441dSJames Smart ctrl->lport->ops->fcp_abort(&ctrl->lport->localport, 1620e399441dSJames Smart &ctrl->rport->remoteport, 1621e399441dSJames Smart op->queue->lldd_handle, 1622e399441dSJames Smart &op->fcp_req); 1623e399441dSJames Smart 1624e399441dSJames Smart return 0; 1625e399441dSJames Smart } 1626e399441dSJames Smart 1627e399441dSJames Smart enum blk_eh_timer_return 1628e399441dSJames Smart nvme_fc_timeout(struct request *rq, bool reserved) 1629e399441dSJames Smart { 1630e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1631e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl; 1632e399441dSJames Smart int ret; 1633e399441dSJames Smart 1634e399441dSJames Smart if (reserved) 1635e399441dSJames Smart return BLK_EH_RESET_TIMER; 1636e399441dSJames Smart 1637e399441dSJames Smart ret = __nvme_fc_abort_op(ctrl, op); 1638e399441dSJames Smart if (ret) 1639e399441dSJames Smart /* io wasn't active to abort consider it done */ 1640e399441dSJames Smart return BLK_EH_HANDLED; 1641e399441dSJames Smart 1642e399441dSJames Smart /* 1643e399441dSJames Smart * TODO: force a controller reset 1644e399441dSJames Smart * when that happens, queues will be torn down and outstanding 1645e399441dSJames Smart * ios will be terminated, and the above abort, on a single io 1646e399441dSJames Smart * will no longer be needed. 1647e399441dSJames Smart */ 1648e399441dSJames Smart 1649e399441dSJames Smart return BLK_EH_HANDLED; 1650e399441dSJames Smart } 1651e399441dSJames Smart 1652e399441dSJames Smart static int 1653e399441dSJames Smart nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq, 1654e399441dSJames Smart struct nvme_fc_fcp_op *op) 1655e399441dSJames Smart { 1656e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req; 1657e399441dSJames Smart enum dma_data_direction dir; 1658e399441dSJames Smart int ret; 1659e399441dSJames Smart 1660e399441dSJames Smart freq->sg_cnt = 0; 1661e399441dSJames Smart 1662b131c61dSChristoph Hellwig if (!blk_rq_payload_bytes(rq)) 1663e399441dSJames Smart return 0; 1664e399441dSJames Smart 1665e399441dSJames Smart freq->sg_table.sgl = freq->first_sgl; 1666e399441dSJames Smart ret = sg_alloc_table_chained(&freq->sg_table, rq->nr_phys_segments, 1667e399441dSJames Smart freq->sg_table.sgl); 1668e399441dSJames Smart if (ret) 1669e399441dSJames Smart return -ENOMEM; 1670e399441dSJames Smart 1671e399441dSJames Smart op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl); 1672e399441dSJames Smart WARN_ON(op->nents > rq->nr_phys_segments); 1673e399441dSJames Smart dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE; 1674e399441dSJames Smart freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl, 1675e399441dSJames Smart op->nents, dir); 1676e399441dSJames Smart if (unlikely(freq->sg_cnt <= 0)) { 1677e399441dSJames Smart sg_free_table_chained(&freq->sg_table, true); 1678e399441dSJames Smart freq->sg_cnt = 0; 1679e399441dSJames Smart return -EFAULT; 1680e399441dSJames Smart } 1681e399441dSJames Smart 1682e399441dSJames Smart /* 1683e399441dSJames Smart * TODO: blk_integrity_rq(rq) for DIF 1684e399441dSJames Smart */ 1685e399441dSJames Smart return 0; 1686e399441dSJames Smart } 1687e399441dSJames Smart 1688e399441dSJames Smart static void 1689e399441dSJames Smart nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq, 1690e399441dSJames Smart struct nvme_fc_fcp_op *op) 1691e399441dSJames Smart { 1692e399441dSJames Smart struct nvmefc_fcp_req *freq = &op->fcp_req; 1693e399441dSJames Smart 1694e399441dSJames Smart if (!freq->sg_cnt) 1695e399441dSJames Smart return; 1696e399441dSJames Smart 1697e399441dSJames Smart fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents, 1698e399441dSJames Smart ((rq_data_dir(rq) == WRITE) ? 1699e399441dSJames Smart DMA_TO_DEVICE : DMA_FROM_DEVICE)); 1700e399441dSJames Smart 1701e399441dSJames Smart nvme_cleanup_cmd(rq); 1702e399441dSJames Smart 1703e399441dSJames Smart sg_free_table_chained(&freq->sg_table, true); 1704e399441dSJames Smart 1705e399441dSJames Smart freq->sg_cnt = 0; 1706e399441dSJames Smart } 1707e399441dSJames Smart 1708e399441dSJames Smart /* 1709e399441dSJames Smart * In FC, the queue is a logical thing. At transport connect, the target 1710e399441dSJames Smart * creates its "queue" and returns a handle that is to be given to the 1711e399441dSJames Smart * target whenever it posts something to the corresponding SQ. When an 1712e399441dSJames Smart * SQE is sent on a SQ, FC effectively considers the SQE, or rather the 1713e399441dSJames Smart * command contained within the SQE, an io, and assigns a FC exchange 1714e399441dSJames Smart * to it. The SQE and the associated SQ handle are sent in the initial 1715e399441dSJames Smart * CMD IU sents on the exchange. All transfers relative to the io occur 1716e399441dSJames Smart * as part of the exchange. The CQE is the last thing for the io, 1717e399441dSJames Smart * which is transferred (explicitly or implicitly) with the RSP IU 1718e399441dSJames Smart * sent on the exchange. After the CQE is received, the FC exchange is 1719e399441dSJames Smart * terminaed and the Exchange may be used on a different io. 1720e399441dSJames Smart * 1721e399441dSJames Smart * The transport to LLDD api has the transport making a request for a 1722e399441dSJames Smart * new fcp io request to the LLDD. The LLDD then allocates a FC exchange 1723e399441dSJames Smart * resource and transfers the command. The LLDD will then process all 1724e399441dSJames Smart * steps to complete the io. Upon completion, the transport done routine 1725e399441dSJames Smart * is called. 1726e399441dSJames Smart * 1727e399441dSJames Smart * So - while the operation is outstanding to the LLDD, there is a link 1728e399441dSJames Smart * level FC exchange resource that is also outstanding. This must be 1729e399441dSJames Smart * considered in all cleanup operations. 1730e399441dSJames Smart */ 1731e399441dSJames Smart static int 1732e399441dSJames Smart nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue, 1733e399441dSJames Smart struct nvme_fc_fcp_op *op, u32 data_len, 1734e399441dSJames Smart enum nvmefc_fcp_datadir io_dir) 1735e399441dSJames Smart { 1736e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 1737e399441dSJames Smart struct nvme_command *sqe = &cmdiu->sqe; 1738e399441dSJames Smart u32 csn; 1739e399441dSJames Smart int ret; 1740e399441dSJames Smart 1741e399441dSJames Smart if (!nvme_fc_ctrl_get(ctrl)) 1742e399441dSJames Smart return BLK_MQ_RQ_QUEUE_ERROR; 1743e399441dSJames Smart 1744e399441dSJames Smart /* format the FC-NVME CMD IU and fcp_req */ 1745e399441dSJames Smart cmdiu->connection_id = cpu_to_be64(queue->connection_id); 1746e399441dSJames Smart csn = atomic_inc_return(&queue->csn); 1747e399441dSJames Smart cmdiu->csn = cpu_to_be32(csn); 1748e399441dSJames Smart cmdiu->data_len = cpu_to_be32(data_len); 1749e399441dSJames Smart switch (io_dir) { 1750e399441dSJames Smart case NVMEFC_FCP_WRITE: 1751e399441dSJames Smart cmdiu->flags = FCNVME_CMD_FLAGS_WRITE; 1752e399441dSJames Smart break; 1753e399441dSJames Smart case NVMEFC_FCP_READ: 1754e399441dSJames Smart cmdiu->flags = FCNVME_CMD_FLAGS_READ; 1755e399441dSJames Smart break; 1756e399441dSJames Smart case NVMEFC_FCP_NODATA: 1757e399441dSJames Smart cmdiu->flags = 0; 1758e399441dSJames Smart break; 1759e399441dSJames Smart } 1760e399441dSJames Smart op->fcp_req.payload_length = data_len; 1761e399441dSJames Smart op->fcp_req.io_dir = io_dir; 1762e399441dSJames Smart op->fcp_req.transferred_length = 0; 1763e399441dSJames Smart op->fcp_req.rcv_rsplen = 0; 1764e399441dSJames Smart op->fcp_req.status = 0; 1765e399441dSJames Smart op->fcp_req.sqid = cpu_to_le16(queue->qnum); 1766e399441dSJames Smart 1767e399441dSJames Smart /* 1768e399441dSJames Smart * validate per fabric rules, set fields mandated by fabric spec 1769e399441dSJames Smart * as well as those by FC-NVME spec. 1770e399441dSJames Smart */ 1771e399441dSJames Smart WARN_ON_ONCE(sqe->common.metadata); 1772e399441dSJames Smart WARN_ON_ONCE(sqe->common.dptr.prp1); 1773e399441dSJames Smart WARN_ON_ONCE(sqe->common.dptr.prp2); 1774e399441dSJames Smart sqe->common.flags |= NVME_CMD_SGL_METABUF; 1775e399441dSJames Smart 1776e399441dSJames Smart /* 1777e399441dSJames Smart * format SQE DPTR field per FC-NVME rules 1778e399441dSJames Smart * type=data block descr; subtype=offset; 1779e399441dSJames Smart * offset is currently 0. 1780e399441dSJames Smart */ 1781e399441dSJames Smart sqe->rw.dptr.sgl.type = NVME_SGL_FMT_OFFSET; 1782e399441dSJames Smart sqe->rw.dptr.sgl.length = cpu_to_le32(data_len); 1783e399441dSJames Smart sqe->rw.dptr.sgl.addr = 0; 1784e399441dSJames Smart 1785e399441dSJames Smart /* odd that we set the command_id - should come from nvme-fabrics */ 1786e399441dSJames Smart WARN_ON_ONCE(sqe->common.command_id != cpu_to_le16(op->rqno)); 1787e399441dSJames Smart 1788e399441dSJames Smart if (op->rq) { /* skipped on aens */ 1789e399441dSJames Smart ret = nvme_fc_map_data(ctrl, op->rq, op); 1790e399441dSJames Smart if (ret < 0) { 1791e399441dSJames Smart dev_err(queue->ctrl->ctrl.device, 1792e399441dSJames Smart "Failed to map data (%d)\n", ret); 1793e399441dSJames Smart nvme_cleanup_cmd(op->rq); 1794e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 1795e399441dSJames Smart return (ret == -ENOMEM || ret == -EAGAIN) ? 1796e399441dSJames Smart BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR; 1797e399441dSJames Smart } 1798e399441dSJames Smart } 1799e399441dSJames Smart 1800e399441dSJames Smart fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma, 1801e399441dSJames Smart sizeof(op->cmd_iu), DMA_TO_DEVICE); 1802e399441dSJames Smart 1803e399441dSJames Smart atomic_set(&op->state, FCPOP_STATE_ACTIVE); 1804e399441dSJames Smart 1805e399441dSJames Smart if (op->rq) 1806e399441dSJames Smart blk_mq_start_request(op->rq); 1807e399441dSJames Smart 1808e399441dSJames Smart ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport, 1809e399441dSJames Smart &ctrl->rport->remoteport, 1810e399441dSJames Smart queue->lldd_handle, &op->fcp_req); 1811e399441dSJames Smart 1812e399441dSJames Smart if (ret) { 1813e399441dSJames Smart dev_err(ctrl->dev, 1814e399441dSJames Smart "Send nvme command failed - lldd returned %d.\n", ret); 1815e399441dSJames Smart 1816e399441dSJames Smart if (op->rq) { /* normal request */ 1817e399441dSJames Smart nvme_fc_unmap_data(ctrl, op->rq, op); 1818e399441dSJames Smart nvme_cleanup_cmd(op->rq); 1819e399441dSJames Smart } 1820e399441dSJames Smart /* else - aen. no cleanup needed */ 1821e399441dSJames Smart 1822e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 1823e399441dSJames Smart 1824e399441dSJames Smart if (ret != -EBUSY) 1825e399441dSJames Smart return BLK_MQ_RQ_QUEUE_ERROR; 1826e399441dSJames Smart 1827e399441dSJames Smart if (op->rq) { 1828e399441dSJames Smart blk_mq_stop_hw_queues(op->rq->q); 1829e399441dSJames Smart blk_mq_delay_queue(queue->hctx, NVMEFC_QUEUE_DELAY); 1830e399441dSJames Smart } 1831e399441dSJames Smart return BLK_MQ_RQ_QUEUE_BUSY; 1832e399441dSJames Smart } 1833e399441dSJames Smart 1834e399441dSJames Smart return BLK_MQ_RQ_QUEUE_OK; 1835e399441dSJames Smart } 1836e399441dSJames Smart 1837e399441dSJames Smart static int 1838e399441dSJames Smart nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx, 1839e399441dSJames Smart const struct blk_mq_queue_data *bd) 1840e399441dSJames Smart { 1841e399441dSJames Smart struct nvme_ns *ns = hctx->queue->queuedata; 1842e399441dSJames Smart struct nvme_fc_queue *queue = hctx->driver_data; 1843e399441dSJames Smart struct nvme_fc_ctrl *ctrl = queue->ctrl; 1844e399441dSJames Smart struct request *rq = bd->rq; 1845e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1846e399441dSJames Smart struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; 1847e399441dSJames Smart struct nvme_command *sqe = &cmdiu->sqe; 1848e399441dSJames Smart enum nvmefc_fcp_datadir io_dir; 1849e399441dSJames Smart u32 data_len; 1850e399441dSJames Smart int ret; 1851e399441dSJames Smart 1852e399441dSJames Smart ret = nvme_setup_cmd(ns, rq, sqe); 1853e399441dSJames Smart if (ret) 1854e399441dSJames Smart return ret; 1855e399441dSJames Smart 1856b131c61dSChristoph Hellwig data_len = blk_rq_payload_bytes(rq); 1857e399441dSJames Smart if (data_len) 1858e399441dSJames Smart io_dir = ((rq_data_dir(rq) == WRITE) ? 1859e399441dSJames Smart NVMEFC_FCP_WRITE : NVMEFC_FCP_READ); 1860e399441dSJames Smart else 1861e399441dSJames Smart io_dir = NVMEFC_FCP_NODATA; 1862e399441dSJames Smart 1863e399441dSJames Smart return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir); 1864e399441dSJames Smart } 1865e399441dSJames Smart 1866e399441dSJames Smart static struct blk_mq_tags * 1867e399441dSJames Smart nvme_fc_tagset(struct nvme_fc_queue *queue) 1868e399441dSJames Smart { 1869e399441dSJames Smart if (queue->qnum == 0) 1870e399441dSJames Smart return queue->ctrl->admin_tag_set.tags[queue->qnum]; 1871e399441dSJames Smart 1872e399441dSJames Smart return queue->ctrl->tag_set.tags[queue->qnum - 1]; 1873e399441dSJames Smart } 1874e399441dSJames Smart 1875e399441dSJames Smart static int 1876e399441dSJames Smart nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag) 1877e399441dSJames Smart 1878e399441dSJames Smart { 1879e399441dSJames Smart struct nvme_fc_queue *queue = hctx->driver_data; 1880e399441dSJames Smart struct nvme_fc_ctrl *ctrl = queue->ctrl; 1881e399441dSJames Smart struct request *req; 1882e399441dSJames Smart struct nvme_fc_fcp_op *op; 1883e399441dSJames Smart 1884e399441dSJames Smart req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag); 1885e399441dSJames Smart if (!req) { 1886e399441dSJames Smart dev_err(queue->ctrl->ctrl.device, 1887e399441dSJames Smart "tag 0x%x on QNum %#x not found\n", 1888e399441dSJames Smart tag, queue->qnum); 1889e399441dSJames Smart return 0; 1890e399441dSJames Smart } 1891e399441dSJames Smart 1892e399441dSJames Smart op = blk_mq_rq_to_pdu(req); 1893e399441dSJames Smart 1894e399441dSJames Smart if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) && 1895e399441dSJames Smart (ctrl->lport->ops->poll_queue)) 1896e399441dSJames Smart ctrl->lport->ops->poll_queue(&ctrl->lport->localport, 1897e399441dSJames Smart queue->lldd_handle); 1898e399441dSJames Smart 1899e399441dSJames Smart return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE)); 1900e399441dSJames Smart } 1901e399441dSJames Smart 1902e399441dSJames Smart static void 1903e399441dSJames Smart nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx) 1904e399441dSJames Smart { 1905e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg); 1906e399441dSJames Smart struct nvme_fc_fcp_op *aen_op; 1907e399441dSJames Smart int ret; 1908e399441dSJames Smart 1909e399441dSJames Smart if (aer_idx > NVME_FC_NR_AEN_COMMANDS) 1910e399441dSJames Smart return; 1911e399441dSJames Smart 1912e399441dSJames Smart aen_op = &ctrl->aen_ops[aer_idx]; 1913e399441dSJames Smart 1914e399441dSJames Smart ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0, 1915e399441dSJames Smart NVMEFC_FCP_NODATA); 1916e399441dSJames Smart if (ret) 1917e399441dSJames Smart dev_err(ctrl->ctrl.device, 1918e399441dSJames Smart "failed async event work [%d]\n", aer_idx); 1919e399441dSJames Smart } 1920e399441dSJames Smart 1921e399441dSJames Smart static void 1922e399441dSJames Smart nvme_fc_complete_rq(struct request *rq) 1923e399441dSJames Smart { 1924e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); 1925e399441dSJames Smart struct nvme_fc_ctrl *ctrl = op->ctrl; 1926e399441dSJames Smart int error = 0, state; 1927e399441dSJames Smart 1928e399441dSJames Smart state = atomic_xchg(&op->state, FCPOP_STATE_IDLE); 1929e399441dSJames Smart 1930e399441dSJames Smart nvme_cleanup_cmd(rq); 1931e399441dSJames Smart 1932e399441dSJames Smart nvme_fc_unmap_data(ctrl, rq, op); 1933e399441dSJames Smart 1934e399441dSJames Smart if (unlikely(rq->errors)) { 1935e399441dSJames Smart if (nvme_req_needs_retry(rq, rq->errors)) { 1936e399441dSJames Smart nvme_requeue_req(rq); 1937e399441dSJames Smart return; 1938e399441dSJames Smart } 1939e399441dSJames Smart 1940e399441dSJames Smart if (rq->cmd_type == REQ_TYPE_DRV_PRIV) 1941e399441dSJames Smart error = rq->errors; 1942e399441dSJames Smart else 1943e399441dSJames Smart error = nvme_error_status(rq->errors); 1944e399441dSJames Smart } 1945e399441dSJames Smart 1946e399441dSJames Smart nvme_fc_ctrl_put(ctrl); 1947e399441dSJames Smart 1948e399441dSJames Smart blk_mq_end_request(rq, error); 1949e399441dSJames Smart } 1950e399441dSJames Smart 1951e399441dSJames Smart static struct blk_mq_ops nvme_fc_mq_ops = { 1952e399441dSJames Smart .queue_rq = nvme_fc_queue_rq, 1953e399441dSJames Smart .complete = nvme_fc_complete_rq, 1954e399441dSJames Smart .init_request = nvme_fc_init_request, 1955e399441dSJames Smart .exit_request = nvme_fc_exit_request, 1956e399441dSJames Smart .reinit_request = nvme_fc_reinit_request, 1957e399441dSJames Smart .init_hctx = nvme_fc_init_hctx, 1958e399441dSJames Smart .poll = nvme_fc_poll, 1959e399441dSJames Smart .timeout = nvme_fc_timeout, 1960e399441dSJames Smart }; 1961e399441dSJames Smart 1962e399441dSJames Smart static struct blk_mq_ops nvme_fc_admin_mq_ops = { 1963e399441dSJames Smart .queue_rq = nvme_fc_queue_rq, 1964e399441dSJames Smart .complete = nvme_fc_complete_rq, 1965e399441dSJames Smart .init_request = nvme_fc_init_admin_request, 1966e399441dSJames Smart .exit_request = nvme_fc_exit_request, 1967e399441dSJames Smart .reinit_request = nvme_fc_reinit_request, 1968e399441dSJames Smart .init_hctx = nvme_fc_init_admin_hctx, 1969e399441dSJames Smart .timeout = nvme_fc_timeout, 1970e399441dSJames Smart }; 1971e399441dSJames Smart 1972e399441dSJames Smart static int 1973e399441dSJames Smart nvme_fc_configure_admin_queue(struct nvme_fc_ctrl *ctrl) 1974e399441dSJames Smart { 1975e399441dSJames Smart u32 segs; 1976e399441dSJames Smart int error; 1977e399441dSJames Smart 1978e399441dSJames Smart nvme_fc_init_queue(ctrl, 0, NVME_FC_AQ_BLKMQ_DEPTH); 1979e399441dSJames Smart 1980e399441dSJames Smart error = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0], 1981e399441dSJames Smart NVME_FC_AQ_BLKMQ_DEPTH, 1982e399441dSJames Smart (NVME_FC_AQ_BLKMQ_DEPTH / 4)); 1983e399441dSJames Smart if (error) 1984e399441dSJames Smart return error; 1985e399441dSJames Smart 1986e399441dSJames Smart memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); 1987e399441dSJames Smart ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops; 1988e399441dSJames Smart ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH; 1989e399441dSJames Smart ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */ 1990e399441dSJames Smart ctrl->admin_tag_set.numa_node = NUMA_NO_NODE; 1991e399441dSJames Smart ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) + 1992e399441dSJames Smart (SG_CHUNK_SIZE * 1993e399441dSJames Smart sizeof(struct scatterlist)) + 1994e399441dSJames Smart ctrl->lport->ops->fcprqst_priv_sz; 1995e399441dSJames Smart ctrl->admin_tag_set.driver_data = ctrl; 1996e399441dSJames Smart ctrl->admin_tag_set.nr_hw_queues = 1; 1997e399441dSJames Smart ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; 1998e399441dSJames Smart 1999e399441dSJames Smart error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); 2000e399441dSJames Smart if (error) 2001e399441dSJames Smart goto out_free_queue; 2002e399441dSJames Smart 2003e399441dSJames Smart ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); 2004e399441dSJames Smart if (IS_ERR(ctrl->ctrl.admin_q)) { 2005e399441dSJames Smart error = PTR_ERR(ctrl->ctrl.admin_q); 2006e399441dSJames Smart goto out_free_tagset; 2007e399441dSJames Smart } 2008e399441dSJames Smart 2009e399441dSJames Smart error = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0, 2010e399441dSJames Smart NVME_FC_AQ_BLKMQ_DEPTH); 2011e399441dSJames Smart if (error) 2012e399441dSJames Smart goto out_cleanup_queue; 2013e399441dSJames Smart 2014e399441dSJames Smart error = nvmf_connect_admin_queue(&ctrl->ctrl); 2015e399441dSJames Smart if (error) 2016e399441dSJames Smart goto out_delete_hw_queue; 2017e399441dSJames Smart 2018e399441dSJames Smart error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap); 2019e399441dSJames Smart if (error) { 2020e399441dSJames Smart dev_err(ctrl->ctrl.device, 2021e399441dSJames Smart "prop_get NVME_REG_CAP failed\n"); 2022e399441dSJames Smart goto out_delete_hw_queue; 2023e399441dSJames Smart } 2024e399441dSJames Smart 2025e399441dSJames Smart ctrl->ctrl.sqsize = 2026e399441dSJames Smart min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize); 2027e399441dSJames Smart 2028e399441dSJames Smart error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap); 2029e399441dSJames Smart if (error) 2030e399441dSJames Smart goto out_delete_hw_queue; 2031e399441dSJames Smart 2032e399441dSJames Smart segs = min_t(u32, NVME_FC_MAX_SEGMENTS, 2033e399441dSJames Smart ctrl->lport->ops->max_sgl_segments); 2034e399441dSJames Smart ctrl->ctrl.max_hw_sectors = (segs - 1) << (PAGE_SHIFT - 9); 2035e399441dSJames Smart 2036e399441dSJames Smart error = nvme_init_identify(&ctrl->ctrl); 2037e399441dSJames Smart if (error) 2038e399441dSJames Smart goto out_delete_hw_queue; 2039e399441dSJames Smart 2040e399441dSJames Smart nvme_start_keep_alive(&ctrl->ctrl); 2041e399441dSJames Smart 2042e399441dSJames Smart return 0; 2043e399441dSJames Smart 2044e399441dSJames Smart out_delete_hw_queue: 2045e399441dSJames Smart __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0); 2046e399441dSJames Smart out_cleanup_queue: 2047e399441dSJames Smart blk_cleanup_queue(ctrl->ctrl.admin_q); 2048e399441dSJames Smart out_free_tagset: 2049e399441dSJames Smart blk_mq_free_tag_set(&ctrl->admin_tag_set); 2050e399441dSJames Smart out_free_queue: 2051e399441dSJames Smart nvme_fc_free_queue(&ctrl->queues[0]); 2052e399441dSJames Smart return error; 2053e399441dSJames Smart } 2054e399441dSJames Smart 2055e399441dSJames Smart /* 2056e399441dSJames Smart * This routine is used by the transport when it needs to find active 2057e399441dSJames Smart * io on a queue that is to be terminated. The transport uses 2058e399441dSJames Smart * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke 2059e399441dSJames Smart * this routine to kill them on a 1 by 1 basis. 2060e399441dSJames Smart * 2061e399441dSJames Smart * As FC allocates FC exchange for each io, the transport must contact 2062e399441dSJames Smart * the LLDD to terminate the exchange, thus releasing the FC exchange. 2063e399441dSJames Smart * After terminating the exchange the LLDD will call the transport's 2064e399441dSJames Smart * normal io done path for the request, but it will have an aborted 2065e399441dSJames Smart * status. The done path will return the io request back to the block 2066e399441dSJames Smart * layer with an error status. 2067e399441dSJames Smart */ 2068e399441dSJames Smart static void 2069e399441dSJames Smart nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved) 2070e399441dSJames Smart { 2071e399441dSJames Smart struct nvme_ctrl *nctrl = data; 2072e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 2073e399441dSJames Smart struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req); 2074e399441dSJames Smart int status; 2075e399441dSJames Smart 2076e399441dSJames Smart if (!blk_mq_request_started(req)) 2077e399441dSJames Smart return; 2078e399441dSJames Smart 2079e399441dSJames Smart /* this performs an ABTS-LS on the FC exchange for the io */ 2080e399441dSJames Smart status = __nvme_fc_abort_op(ctrl, op); 2081e399441dSJames Smart /* 2082e399441dSJames Smart * if __nvme_fc_abort_op failed: io wasn't active to abort 2083e399441dSJames Smart * consider it done. Assume completion path already completing 2084e399441dSJames Smart * in parallel 2085e399441dSJames Smart */ 2086e399441dSJames Smart if (status) 2087e399441dSJames Smart /* io wasn't active to abort consider it done */ 2088e399441dSJames Smart /* assume completion path already completing in parallel */ 2089e399441dSJames Smart return; 2090e399441dSJames Smart } 2091e399441dSJames Smart 2092e399441dSJames Smart 2093e399441dSJames Smart /* 2094e399441dSJames Smart * This routine stops operation of the controller. Admin and IO queues 2095e399441dSJames Smart * are stopped, outstanding ios on them terminated, and the nvme ctrl 2096e399441dSJames Smart * is shutdown. 2097e399441dSJames Smart */ 2098e399441dSJames Smart static void 2099e399441dSJames Smart nvme_fc_shutdown_ctrl(struct nvme_fc_ctrl *ctrl) 2100e399441dSJames Smart { 2101e399441dSJames Smart /* 2102e399441dSJames Smart * If io queues are present, stop them and terminate all outstanding 2103e399441dSJames Smart * ios on them. As FC allocates FC exchange for each io, the 2104e399441dSJames Smart * transport must contact the LLDD to terminate the exchange, 2105e399441dSJames Smart * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr() 2106e399441dSJames Smart * to tell us what io's are busy and invoke a transport routine 2107e399441dSJames Smart * to kill them with the LLDD. After terminating the exchange 2108e399441dSJames Smart * the LLDD will call the transport's normal io done path, but it 2109e399441dSJames Smart * will have an aborted status. The done path will return the 2110e399441dSJames Smart * io requests back to the block layer as part of normal completions 2111e399441dSJames Smart * (but with error status). 2112e399441dSJames Smart */ 2113e399441dSJames Smart if (ctrl->queue_count > 1) { 2114e399441dSJames Smart nvme_stop_queues(&ctrl->ctrl); 2115e399441dSJames Smart blk_mq_tagset_busy_iter(&ctrl->tag_set, 2116e399441dSJames Smart nvme_fc_terminate_exchange, &ctrl->ctrl); 2117e399441dSJames Smart } 2118e399441dSJames Smart 2119e399441dSJames Smart if (ctrl->ctrl.state == NVME_CTRL_LIVE) 2120e399441dSJames Smart nvme_shutdown_ctrl(&ctrl->ctrl); 2121e399441dSJames Smart 2122e399441dSJames Smart /* 2123e399441dSJames Smart * now clean up the admin queue. Same thing as above. 2124e399441dSJames Smart * use blk_mq_tagset_busy_itr() and the transport routine to 2125e399441dSJames Smart * terminate the exchanges. 2126e399441dSJames Smart */ 2127e399441dSJames Smart blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); 2128e399441dSJames Smart blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, 2129e399441dSJames Smart nvme_fc_terminate_exchange, &ctrl->ctrl); 2130e399441dSJames Smart } 2131e399441dSJames Smart 2132e399441dSJames Smart /* 2133e399441dSJames Smart * Called to teardown an association. 2134e399441dSJames Smart * May be called with association fully in place or partially in place. 2135e399441dSJames Smart */ 2136e399441dSJames Smart static void 2137e399441dSJames Smart __nvme_fc_remove_ctrl(struct nvme_fc_ctrl *ctrl) 2138e399441dSJames Smart { 2139e399441dSJames Smart nvme_stop_keep_alive(&ctrl->ctrl); 2140e399441dSJames Smart 2141e399441dSJames Smart /* stop and terminate ios on admin and io queues */ 2142e399441dSJames Smart nvme_fc_shutdown_ctrl(ctrl); 2143e399441dSJames Smart 2144e399441dSJames Smart /* 2145e399441dSJames Smart * tear down the controller 2146e399441dSJames Smart * This will result in the last reference on the nvme ctrl to 2147e399441dSJames Smart * expire, calling the transport nvme_fc_free_nvme_ctrl() callback. 2148e399441dSJames Smart * From there, the transport will tear down it's logical queues and 2149e399441dSJames Smart * association. 2150e399441dSJames Smart */ 2151e399441dSJames Smart nvme_uninit_ctrl(&ctrl->ctrl); 2152e399441dSJames Smart 2153e399441dSJames Smart nvme_put_ctrl(&ctrl->ctrl); 2154e399441dSJames Smart } 2155e399441dSJames Smart 2156e399441dSJames Smart static void 2157e399441dSJames Smart nvme_fc_del_ctrl_work(struct work_struct *work) 2158e399441dSJames Smart { 2159e399441dSJames Smart struct nvme_fc_ctrl *ctrl = 2160e399441dSJames Smart container_of(work, struct nvme_fc_ctrl, delete_work); 2161e399441dSJames Smart 2162e399441dSJames Smart __nvme_fc_remove_ctrl(ctrl); 2163e399441dSJames Smart } 2164e399441dSJames Smart 2165e399441dSJames Smart static int 2166e399441dSJames Smart __nvme_fc_del_ctrl(struct nvme_fc_ctrl *ctrl) 2167e399441dSJames Smart { 2168e399441dSJames Smart if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING)) 2169e399441dSJames Smart return -EBUSY; 2170e399441dSJames Smart 2171e399441dSJames Smart if (!queue_work(nvme_fc_wq, &ctrl->delete_work)) 2172e399441dSJames Smart return -EBUSY; 2173e399441dSJames Smart 2174e399441dSJames Smart return 0; 2175e399441dSJames Smart } 2176e399441dSJames Smart 2177e399441dSJames Smart /* 2178e399441dSJames Smart * Request from nvme core layer to delete the controller 2179e399441dSJames Smart */ 2180e399441dSJames Smart static int 2181e399441dSJames Smart nvme_fc_del_nvme_ctrl(struct nvme_ctrl *nctrl) 2182e399441dSJames Smart { 2183e399441dSJames Smart struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); 2184e399441dSJames Smart struct nvme_fc_rport *rport = ctrl->rport; 2185e399441dSJames Smart unsigned long flags; 2186e399441dSJames Smart int ret; 2187e399441dSJames Smart 2188e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags); 2189e399441dSJames Smart ret = __nvme_fc_del_ctrl(ctrl); 2190e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 2191e399441dSJames Smart if (ret) 2192e399441dSJames Smart return ret; 2193e399441dSJames Smart 2194e399441dSJames Smart flush_work(&ctrl->delete_work); 2195e399441dSJames Smart 2196e399441dSJames Smart return 0; 2197e399441dSJames Smart } 2198e399441dSJames Smart 2199e399441dSJames Smart static int 2200e399441dSJames Smart nvme_fc_reset_nvme_ctrl(struct nvme_ctrl *nctrl) 2201e399441dSJames Smart { 2202e399441dSJames Smart return -EIO; 2203e399441dSJames Smart } 2204e399441dSJames Smart 2205e399441dSJames Smart static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = { 2206e399441dSJames Smart .name = "fc", 2207e399441dSJames Smart .module = THIS_MODULE, 2208e399441dSJames Smart .is_fabrics = true, 2209e399441dSJames Smart .reg_read32 = nvmf_reg_read32, 2210e399441dSJames Smart .reg_read64 = nvmf_reg_read64, 2211e399441dSJames Smart .reg_write32 = nvmf_reg_write32, 2212e399441dSJames Smart .reset_ctrl = nvme_fc_reset_nvme_ctrl, 2213e399441dSJames Smart .free_ctrl = nvme_fc_free_nvme_ctrl, 2214e399441dSJames Smart .submit_async_event = nvme_fc_submit_async_event, 2215e399441dSJames Smart .delete_ctrl = nvme_fc_del_nvme_ctrl, 2216e399441dSJames Smart .get_subsysnqn = nvmf_get_subsysnqn, 2217e399441dSJames Smart .get_address = nvmf_get_address, 2218e399441dSJames Smart }; 2219e399441dSJames Smart 2220e399441dSJames Smart static int 2221e399441dSJames Smart nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl) 2222e399441dSJames Smart { 2223e399441dSJames Smart struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 2224e399441dSJames Smart int ret; 2225e399441dSJames Smart 2226e399441dSJames Smart ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues); 2227e399441dSJames Smart if (ret) { 2228e399441dSJames Smart dev_info(ctrl->ctrl.device, 2229e399441dSJames Smart "set_queue_count failed: %d\n", ret); 2230e399441dSJames Smart return ret; 2231e399441dSJames Smart } 2232e399441dSJames Smart 2233e399441dSJames Smart ctrl->queue_count = opts->nr_io_queues + 1; 2234e399441dSJames Smart if (!opts->nr_io_queues) 2235e399441dSJames Smart return 0; 2236e399441dSJames Smart 2237e399441dSJames Smart dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", 2238e399441dSJames Smart opts->nr_io_queues); 2239e399441dSJames Smart 2240e399441dSJames Smart nvme_fc_init_io_queues(ctrl); 2241e399441dSJames Smart 2242e399441dSJames Smart memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); 2243e399441dSJames Smart ctrl->tag_set.ops = &nvme_fc_mq_ops; 2244e399441dSJames Smart ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; 2245e399441dSJames Smart ctrl->tag_set.reserved_tags = 1; /* fabric connect */ 2246e399441dSJames Smart ctrl->tag_set.numa_node = NUMA_NO_NODE; 2247e399441dSJames Smart ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 2248e399441dSJames Smart ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) + 2249e399441dSJames Smart (SG_CHUNK_SIZE * 2250e399441dSJames Smart sizeof(struct scatterlist)) + 2251e399441dSJames Smart ctrl->lport->ops->fcprqst_priv_sz; 2252e399441dSJames Smart ctrl->tag_set.driver_data = ctrl; 2253e399441dSJames Smart ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1; 2254e399441dSJames Smart ctrl->tag_set.timeout = NVME_IO_TIMEOUT; 2255e399441dSJames Smart 2256e399441dSJames Smart ret = blk_mq_alloc_tag_set(&ctrl->tag_set); 2257e399441dSJames Smart if (ret) 2258e399441dSJames Smart return ret; 2259e399441dSJames Smart 2260e399441dSJames Smart ctrl->ctrl.tagset = &ctrl->tag_set; 2261e399441dSJames Smart 2262e399441dSJames Smart ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); 2263e399441dSJames Smart if (IS_ERR(ctrl->ctrl.connect_q)) { 2264e399441dSJames Smart ret = PTR_ERR(ctrl->ctrl.connect_q); 2265e399441dSJames Smart goto out_free_tag_set; 2266e399441dSJames Smart } 2267e399441dSJames Smart 2268e399441dSJames Smart ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size); 2269e399441dSJames Smart if (ret) 2270e399441dSJames Smart goto out_cleanup_blk_queue; 2271e399441dSJames Smart 2272e399441dSJames Smart ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size); 2273e399441dSJames Smart if (ret) 2274e399441dSJames Smart goto out_delete_hw_queues; 2275e399441dSJames Smart 2276e399441dSJames Smart return 0; 2277e399441dSJames Smart 2278e399441dSJames Smart out_delete_hw_queues: 2279e399441dSJames Smart nvme_fc_delete_hw_io_queues(ctrl); 2280e399441dSJames Smart out_cleanup_blk_queue: 2281e399441dSJames Smart nvme_stop_keep_alive(&ctrl->ctrl); 2282e399441dSJames Smart blk_cleanup_queue(ctrl->ctrl.connect_q); 2283e399441dSJames Smart out_free_tag_set: 2284e399441dSJames Smart blk_mq_free_tag_set(&ctrl->tag_set); 2285e399441dSJames Smart nvme_fc_free_io_queues(ctrl); 2286e399441dSJames Smart 2287e399441dSJames Smart /* force put free routine to ignore io queues */ 2288e399441dSJames Smart ctrl->ctrl.tagset = NULL; 2289e399441dSJames Smart 2290e399441dSJames Smart return ret; 2291e399441dSJames Smart } 2292e399441dSJames Smart 2293e399441dSJames Smart 2294e399441dSJames Smart static struct nvme_ctrl * 2295e399441dSJames Smart __nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts, 2296e399441dSJames Smart struct nvme_fc_lport *lport, struct nvme_fc_rport *rport) 2297e399441dSJames Smart { 2298e399441dSJames Smart struct nvme_fc_ctrl *ctrl; 2299e399441dSJames Smart unsigned long flags; 2300e399441dSJames Smart int ret, idx; 2301e399441dSJames Smart bool changed; 2302e399441dSJames Smart 2303e399441dSJames Smart ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 2304e399441dSJames Smart if (!ctrl) { 2305e399441dSJames Smart ret = -ENOMEM; 2306e399441dSJames Smart goto out_fail; 2307e399441dSJames Smart } 2308e399441dSJames Smart 2309e399441dSJames Smart idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL); 2310e399441dSJames Smart if (idx < 0) { 2311e399441dSJames Smart ret = -ENOSPC; 2312e399441dSJames Smart goto out_free_ctrl; 2313e399441dSJames Smart } 2314e399441dSJames Smart 2315e399441dSJames Smart ctrl->ctrl.opts = opts; 2316e399441dSJames Smart INIT_LIST_HEAD(&ctrl->ctrl_list); 2317e399441dSJames Smart INIT_LIST_HEAD(&ctrl->ls_req_list); 2318e399441dSJames Smart ctrl->lport = lport; 2319e399441dSJames Smart ctrl->rport = rport; 2320e399441dSJames Smart ctrl->dev = lport->dev; 2321e399441dSJames Smart ctrl->state = FCCTRL_INIT; 2322e399441dSJames Smart ctrl->cnum = idx; 2323e399441dSJames Smart 2324e399441dSJames Smart ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0); 2325e399441dSJames Smart if (ret) 2326e399441dSJames Smart goto out_free_ida; 2327e399441dSJames Smart 2328e399441dSJames Smart get_device(ctrl->dev); 2329e399441dSJames Smart kref_init(&ctrl->ref); 2330e399441dSJames Smart 2331e399441dSJames Smart INIT_WORK(&ctrl->delete_work, nvme_fc_del_ctrl_work); 2332e399441dSJames Smart spin_lock_init(&ctrl->lock); 2333e399441dSJames Smart 2334e399441dSJames Smart /* io queue count */ 2335e399441dSJames Smart ctrl->queue_count = min_t(unsigned int, 2336e399441dSJames Smart opts->nr_io_queues, 2337e399441dSJames Smart lport->ops->max_hw_queues); 2338e399441dSJames Smart opts->nr_io_queues = ctrl->queue_count; /* so opts has valid value */ 2339e399441dSJames Smart ctrl->queue_count++; /* +1 for admin queue */ 2340e399441dSJames Smart 2341e399441dSJames Smart ctrl->ctrl.sqsize = opts->queue_size - 1; 2342e399441dSJames Smart ctrl->ctrl.kato = opts->kato; 2343e399441dSJames Smart 2344e399441dSJames Smart ret = -ENOMEM; 2345e399441dSJames Smart ctrl->queues = kcalloc(ctrl->queue_count, sizeof(struct nvme_fc_queue), 2346e399441dSJames Smart GFP_KERNEL); 2347e399441dSJames Smart if (!ctrl->queues) 2348e399441dSJames Smart goto out_uninit_ctrl; 2349e399441dSJames Smart 2350e399441dSJames Smart ret = nvme_fc_configure_admin_queue(ctrl); 2351e399441dSJames Smart if (ret) 2352e399441dSJames Smart goto out_uninit_ctrl; 2353e399441dSJames Smart 2354e399441dSJames Smart /* sanity checks */ 2355e399441dSJames Smart 2356e399441dSJames Smart /* FC-NVME supports 64-byte SQE only */ 2357e399441dSJames Smart if (ctrl->ctrl.ioccsz != 4) { 2358e399441dSJames Smart dev_err(ctrl->ctrl.device, "ioccsz %d is not supported!\n", 2359e399441dSJames Smart ctrl->ctrl.ioccsz); 2360e399441dSJames Smart goto out_remove_admin_queue; 2361e399441dSJames Smart } 2362e399441dSJames Smart /* FC-NVME supports 16-byte CQE only */ 2363e399441dSJames Smart if (ctrl->ctrl.iorcsz != 1) { 2364e399441dSJames Smart dev_err(ctrl->ctrl.device, "iorcsz %d is not supported!\n", 2365e399441dSJames Smart ctrl->ctrl.iorcsz); 2366e399441dSJames Smart goto out_remove_admin_queue; 2367e399441dSJames Smart } 2368e399441dSJames Smart /* FC-NVME does not have other data in the capsule */ 2369e399441dSJames Smart if (ctrl->ctrl.icdoff) { 2370e399441dSJames Smart dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n", 2371e399441dSJames Smart ctrl->ctrl.icdoff); 2372e399441dSJames Smart goto out_remove_admin_queue; 2373e399441dSJames Smart } 2374e399441dSJames Smart 2375e399441dSJames Smart /* FC-NVME supports normal SGL Data Block Descriptors */ 2376e399441dSJames Smart 2377e399441dSJames Smart if (opts->queue_size > ctrl->ctrl.maxcmd) { 2378e399441dSJames Smart /* warn if maxcmd is lower than queue_size */ 2379e399441dSJames Smart dev_warn(ctrl->ctrl.device, 2380e399441dSJames Smart "queue_size %zu > ctrl maxcmd %u, reducing " 2381e399441dSJames Smart "to queue_size\n", 2382e399441dSJames Smart opts->queue_size, ctrl->ctrl.maxcmd); 2383e399441dSJames Smart opts->queue_size = ctrl->ctrl.maxcmd; 2384e399441dSJames Smart } 2385e399441dSJames Smart 2386e399441dSJames Smart ret = nvme_fc_init_aen_ops(ctrl); 2387e399441dSJames Smart if (ret) 2388e399441dSJames Smart goto out_exit_aen_ops; 2389e399441dSJames Smart 2390e399441dSJames Smart if (ctrl->queue_count > 1) { 2391e399441dSJames Smart ret = nvme_fc_create_io_queues(ctrl); 2392e399441dSJames Smart if (ret) 2393e399441dSJames Smart goto out_exit_aen_ops; 2394e399441dSJames Smart } 2395e399441dSJames Smart 2396e399441dSJames Smart spin_lock_irqsave(&ctrl->lock, flags); 2397e399441dSJames Smart ctrl->state = FCCTRL_ACTIVE; 2398e399441dSJames Smart spin_unlock_irqrestore(&ctrl->lock, flags); 2399e399441dSJames Smart 2400e399441dSJames Smart changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 2401e399441dSJames Smart WARN_ON_ONCE(!changed); 2402e399441dSJames Smart 2403e399441dSJames Smart dev_info(ctrl->ctrl.device, 2404c7034898SJames Smart "NVME-FC{%d}: new ctrl: NQN \"%s\"\n", 2405c7034898SJames Smart ctrl->cnum, ctrl->ctrl.opts->subsysnqn); 2406e399441dSJames Smart 2407e399441dSJames Smart kref_get(&ctrl->ctrl.kref); 2408e399441dSJames Smart 2409e399441dSJames Smart spin_lock_irqsave(&rport->lock, flags); 2410e399441dSJames Smart list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list); 2411e399441dSJames Smart spin_unlock_irqrestore(&rport->lock, flags); 2412e399441dSJames Smart 2413e399441dSJames Smart if (opts->nr_io_queues) { 2414e399441dSJames Smart nvme_queue_scan(&ctrl->ctrl); 2415e399441dSJames Smart nvme_queue_async_events(&ctrl->ctrl); 2416e399441dSJames Smart } 2417e399441dSJames Smart 2418e399441dSJames Smart return &ctrl->ctrl; 2419e399441dSJames Smart 2420e399441dSJames Smart out_exit_aen_ops: 2421e399441dSJames Smart nvme_fc_exit_aen_ops(ctrl); 2422e399441dSJames Smart out_remove_admin_queue: 2423e399441dSJames Smart /* send a Disconnect(association) LS to fc-nvme target */ 2424e399441dSJames Smart nvme_fc_xmt_disconnect_assoc(ctrl); 2425e399441dSJames Smart nvme_stop_keep_alive(&ctrl->ctrl); 2426e399441dSJames Smart nvme_fc_destroy_admin_queue(ctrl); 2427e399441dSJames Smart out_uninit_ctrl: 2428e399441dSJames Smart nvme_uninit_ctrl(&ctrl->ctrl); 2429e399441dSJames Smart nvme_put_ctrl(&ctrl->ctrl); 2430e399441dSJames Smart if (ret > 0) 2431e399441dSJames Smart ret = -EIO; 2432e399441dSJames Smart /* exit via here will follow ctlr ref point callbacks to free */ 2433e399441dSJames Smart return ERR_PTR(ret); 2434e399441dSJames Smart 2435e399441dSJames Smart out_free_ida: 2436e399441dSJames Smart ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum); 2437e399441dSJames Smart out_free_ctrl: 2438e399441dSJames Smart kfree(ctrl); 2439e399441dSJames Smart out_fail: 2440e399441dSJames Smart nvme_fc_rport_put(rport); 2441e399441dSJames Smart /* exit via here doesn't follow ctlr ref points */ 2442e399441dSJames Smart return ERR_PTR(ret); 2443e399441dSJames Smart } 2444e399441dSJames Smart 2445e399441dSJames Smart enum { 2446e399441dSJames Smart FCT_TRADDR_ERR = 0, 2447e399441dSJames Smart FCT_TRADDR_WWNN = 1 << 0, 2448e399441dSJames Smart FCT_TRADDR_WWPN = 1 << 1, 2449e399441dSJames Smart }; 2450e399441dSJames Smart 2451e399441dSJames Smart struct nvmet_fc_traddr { 2452e399441dSJames Smart u64 nn; 2453e399441dSJames Smart u64 pn; 2454e399441dSJames Smart }; 2455e399441dSJames Smart 2456e399441dSJames Smart static const match_table_t traddr_opt_tokens = { 2457e399441dSJames Smart { FCT_TRADDR_WWNN, "nn-%s" }, 2458e399441dSJames Smart { FCT_TRADDR_WWPN, "pn-%s" }, 2459e399441dSJames Smart { FCT_TRADDR_ERR, NULL } 2460e399441dSJames Smart }; 2461e399441dSJames Smart 2462e399441dSJames Smart static int 2463e399441dSJames Smart nvme_fc_parse_address(struct nvmet_fc_traddr *traddr, char *buf) 2464e399441dSJames Smart { 2465e399441dSJames Smart substring_t args[MAX_OPT_ARGS]; 2466e399441dSJames Smart char *options, *o, *p; 2467e399441dSJames Smart int token, ret = 0; 2468e399441dSJames Smart u64 token64; 2469e399441dSJames Smart 2470e399441dSJames Smart options = o = kstrdup(buf, GFP_KERNEL); 2471e399441dSJames Smart if (!options) 2472e399441dSJames Smart return -ENOMEM; 2473e399441dSJames Smart 2474e399441dSJames Smart while ((p = strsep(&o, ":\n")) != NULL) { 2475e399441dSJames Smart if (!*p) 2476e399441dSJames Smart continue; 2477e399441dSJames Smart 2478e399441dSJames Smart token = match_token(p, traddr_opt_tokens, args); 2479e399441dSJames Smart switch (token) { 2480e399441dSJames Smart case FCT_TRADDR_WWNN: 2481e399441dSJames Smart if (match_u64(args, &token64)) { 2482e399441dSJames Smart ret = -EINVAL; 2483e399441dSJames Smart goto out; 2484e399441dSJames Smart } 2485e399441dSJames Smart traddr->nn = token64; 2486e399441dSJames Smart break; 2487e399441dSJames Smart case FCT_TRADDR_WWPN: 2488e399441dSJames Smart if (match_u64(args, &token64)) { 2489e399441dSJames Smart ret = -EINVAL; 2490e399441dSJames Smart goto out; 2491e399441dSJames Smart } 2492e399441dSJames Smart traddr->pn = token64; 2493e399441dSJames Smart break; 2494e399441dSJames Smart default: 2495e399441dSJames Smart pr_warn("unknown traddr token or missing value '%s'\n", 2496e399441dSJames Smart p); 2497e399441dSJames Smart ret = -EINVAL; 2498e399441dSJames Smart goto out; 2499e399441dSJames Smart } 2500e399441dSJames Smart } 2501e399441dSJames Smart 2502e399441dSJames Smart out: 2503e399441dSJames Smart kfree(options); 2504e399441dSJames Smart return ret; 2505e399441dSJames Smart } 2506e399441dSJames Smart 2507e399441dSJames Smart static struct nvme_ctrl * 2508e399441dSJames Smart nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts) 2509e399441dSJames Smart { 2510e399441dSJames Smart struct nvme_fc_lport *lport; 2511e399441dSJames Smart struct nvme_fc_rport *rport; 2512e399441dSJames Smart struct nvmet_fc_traddr laddr = { 0L, 0L }; 2513e399441dSJames Smart struct nvmet_fc_traddr raddr = { 0L, 0L }; 2514e399441dSJames Smart unsigned long flags; 2515e399441dSJames Smart int ret; 2516e399441dSJames Smart 2517e399441dSJames Smart ret = nvme_fc_parse_address(&raddr, opts->traddr); 2518e399441dSJames Smart if (ret || !raddr.nn || !raddr.pn) 2519e399441dSJames Smart return ERR_PTR(-EINVAL); 2520e399441dSJames Smart 2521e399441dSJames Smart ret = nvme_fc_parse_address(&laddr, opts->host_traddr); 2522e399441dSJames Smart if (ret || !laddr.nn || !laddr.pn) 2523e399441dSJames Smart return ERR_PTR(-EINVAL); 2524e399441dSJames Smart 2525e399441dSJames Smart /* find the host and remote ports to connect together */ 2526e399441dSJames Smart spin_lock_irqsave(&nvme_fc_lock, flags); 2527e399441dSJames Smart list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { 2528e399441dSJames Smart if (lport->localport.node_name != laddr.nn || 2529e399441dSJames Smart lport->localport.port_name != laddr.pn) 2530e399441dSJames Smart continue; 2531e399441dSJames Smart 2532e399441dSJames Smart list_for_each_entry(rport, &lport->endp_list, endp_list) { 2533e399441dSJames Smart if (rport->remoteport.node_name != raddr.nn || 2534e399441dSJames Smart rport->remoteport.port_name != raddr.pn) 2535e399441dSJames Smart continue; 2536e399441dSJames Smart 2537e399441dSJames Smart /* if fail to get reference fall through. Will error */ 2538e399441dSJames Smart if (!nvme_fc_rport_get(rport)) 2539e399441dSJames Smart break; 2540e399441dSJames Smart 2541e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 2542e399441dSJames Smart 2543e399441dSJames Smart return __nvme_fc_create_ctrl(dev, opts, lport, rport); 2544e399441dSJames Smart } 2545e399441dSJames Smart } 2546e399441dSJames Smart spin_unlock_irqrestore(&nvme_fc_lock, flags); 2547e399441dSJames Smart 2548e399441dSJames Smart return ERR_PTR(-ENOENT); 2549e399441dSJames Smart } 2550e399441dSJames Smart 2551e399441dSJames Smart 2552e399441dSJames Smart static struct nvmf_transport_ops nvme_fc_transport = { 2553e399441dSJames Smart .name = "fc", 2554e399441dSJames Smart .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR, 2555e399441dSJames Smart .allowed_opts = NVMF_OPT_RECONNECT_DELAY, 2556e399441dSJames Smart .create_ctrl = nvme_fc_create_ctrl, 2557e399441dSJames Smart }; 2558e399441dSJames Smart 2559e399441dSJames Smart static int __init nvme_fc_init_module(void) 2560e399441dSJames Smart { 2561e399441dSJames Smart nvme_fc_wq = create_workqueue("nvme_fc_wq"); 2562e399441dSJames Smart if (!nvme_fc_wq) 2563e399441dSJames Smart return -ENOMEM; 2564e399441dSJames Smart 2565e399441dSJames Smart nvmf_register_transport(&nvme_fc_transport); 2566e399441dSJames Smart return 0; 2567e399441dSJames Smart } 2568e399441dSJames Smart 2569e399441dSJames Smart static void __exit nvme_fc_exit_module(void) 2570e399441dSJames Smart { 2571e399441dSJames Smart /* sanity check - all lports should be removed */ 2572e399441dSJames Smart if (!list_empty(&nvme_fc_lport_list)) 2573e399441dSJames Smart pr_warn("%s: localport list not empty\n", __func__); 2574e399441dSJames Smart 2575e399441dSJames Smart nvmf_unregister_transport(&nvme_fc_transport); 2576e399441dSJames Smart 2577e399441dSJames Smart destroy_workqueue(nvme_fc_wq); 2578e399441dSJames Smart 2579e399441dSJames Smart ida_destroy(&nvme_fc_local_port_cnt); 2580e399441dSJames Smart ida_destroy(&nvme_fc_ctrl_cnt); 2581e399441dSJames Smart } 2582e399441dSJames Smart 2583e399441dSJames Smart module_init(nvme_fc_init_module); 2584e399441dSJames Smart module_exit(nvme_fc_exit_module); 2585e399441dSJames Smart 2586e399441dSJames Smart MODULE_LICENSE("GPL v2"); 2587