James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Avago Technologies. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful. |
| 9 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, |
| 10 | * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
| 11 | * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO |
| 12 | * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID. |
| 13 | * See the GNU General Public License for more details, a copy of which |
| 14 | * can be found in the file COPYING included with this package |
| 15 | * |
| 16 | */ |
| 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/parser.h> |
| 20 | #include <uapi/scsi/fc/fc_fs.h> |
| 21 | #include <uapi/scsi/fc/fc_els.h> |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 22 | #include <linux/delay.h> |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 23 | |
| 24 | #include "nvme.h" |
| 25 | #include "fabrics.h" |
| 26 | #include <linux/nvme-fc-driver.h> |
| 27 | #include <linux/nvme-fc.h> |
| 28 | |
| 29 | |
| 30 | /* *************************** Data Structures/Defines ****************** */ |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | * We handle AEN commands ourselves and don't even let the |
| 35 | * block layer know about them. |
| 36 | */ |
| 37 | #define NVME_FC_NR_AEN_COMMANDS 1 |
| 38 | #define NVME_FC_AQ_BLKMQ_DEPTH \ |
Sagi Grimberg | 7aa1f42 | 2017-06-18 16:15:59 +0300 | [diff] [blame] | 39 | (NVME_AQ_DEPTH - NVME_FC_NR_AEN_COMMANDS) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 40 | #define AEN_CMDID_BASE (NVME_FC_AQ_BLKMQ_DEPTH + 1) |
| 41 | |
| 42 | enum nvme_fc_queue_flags { |
| 43 | NVME_FC_Q_CONNECTED = (1 << 0), |
| 44 | }; |
| 45 | |
| 46 | #define NVMEFC_QUEUE_DELAY 3 /* ms units */ |
| 47 | |
James Smart | ac7fe82 | 2017-10-25 16:43:15 -0700 | [diff] [blame] | 48 | #define NVME_FC_DEFAULT_DEV_LOSS_TMO 60 /* seconds */ |
| 49 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 50 | struct nvme_fc_queue { |
| 51 | struct nvme_fc_ctrl *ctrl; |
| 52 | struct device *dev; |
| 53 | struct blk_mq_hw_ctx *hctx; |
| 54 | void *lldd_handle; |
| 55 | int queue_size; |
| 56 | size_t cmnd_capsule_len; |
| 57 | u32 qnum; |
| 58 | u32 rqcnt; |
| 59 | u32 seqno; |
| 60 | |
| 61 | u64 connection_id; |
| 62 | atomic_t csn; |
| 63 | |
| 64 | unsigned long flags; |
| 65 | } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ |
| 66 | |
James Smart | 8d64daf | 2017-04-11 11:35:09 -0700 | [diff] [blame] | 67 | enum nvme_fcop_flags { |
| 68 | FCOP_FLAGS_TERMIO = (1 << 0), |
| 69 | FCOP_FLAGS_RELEASED = (1 << 1), |
| 70 | FCOP_FLAGS_COMPLETE = (1 << 2), |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 71 | FCOP_FLAGS_AEN = (1 << 3), |
James Smart | 8d64daf | 2017-04-11 11:35:09 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 74 | struct nvmefc_ls_req_op { |
| 75 | struct nvmefc_ls_req ls_req; |
| 76 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 77 | struct nvme_fc_rport *rport; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 78 | struct nvme_fc_queue *queue; |
| 79 | struct request *rq; |
James Smart | 8d64daf | 2017-04-11 11:35:09 -0700 | [diff] [blame] | 80 | u32 flags; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 81 | |
| 82 | int ls_error; |
| 83 | struct completion ls_done; |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 84 | struct list_head lsreq_list; /* rport->ls_req_list */ |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 85 | bool req_queued; |
| 86 | }; |
| 87 | |
| 88 | enum nvme_fcpop_state { |
| 89 | FCPOP_STATE_UNINIT = 0, |
| 90 | FCPOP_STATE_IDLE = 1, |
| 91 | FCPOP_STATE_ACTIVE = 2, |
| 92 | FCPOP_STATE_ABORTED = 3, |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 93 | FCPOP_STATE_COMPLETE = 4, |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | struct nvme_fc_fcp_op { |
| 97 | struct nvme_request nreq; /* |
| 98 | * nvme/host/core.c |
| 99 | * requires this to be |
| 100 | * the 1st element in the |
| 101 | * private structure |
| 102 | * associated with the |
| 103 | * request. |
| 104 | */ |
| 105 | struct nvmefc_fcp_req fcp_req; |
| 106 | |
| 107 | struct nvme_fc_ctrl *ctrl; |
| 108 | struct nvme_fc_queue *queue; |
| 109 | struct request *rq; |
| 110 | |
| 111 | atomic_t state; |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 112 | u32 flags; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 113 | u32 rqno; |
| 114 | u32 nents; |
| 115 | |
| 116 | struct nvme_fc_cmd_iu cmd_iu; |
| 117 | struct nvme_fc_ersp_iu rsp_iu; |
| 118 | }; |
| 119 | |
| 120 | struct nvme_fc_lport { |
| 121 | struct nvme_fc_local_port localport; |
| 122 | |
| 123 | struct ida endp_cnt; |
| 124 | struct list_head port_list; /* nvme_fc_port_list */ |
| 125 | struct list_head endp_list; |
| 126 | struct device *dev; /* physical device for dma */ |
| 127 | struct nvme_fc_port_template *ops; |
| 128 | struct kref ref; |
| 129 | } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ |
| 130 | |
| 131 | struct nvme_fc_rport { |
| 132 | struct nvme_fc_remote_port remoteport; |
| 133 | |
| 134 | struct list_head endp_list; /* for lport->endp_list */ |
| 135 | struct list_head ctrl_list; |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 136 | struct list_head ls_req_list; |
| 137 | struct device *dev; /* physical device for dma */ |
| 138 | struct nvme_fc_lport *lport; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 139 | spinlock_t lock; |
| 140 | struct kref ref; |
| 141 | } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */ |
| 142 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 143 | enum nvme_fcctrl_flags { |
| 144 | FCCTRL_TERMIO = (1 << 0), |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | struct nvme_fc_ctrl { |
| 148 | spinlock_t lock; |
| 149 | struct nvme_fc_queue *queues; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 150 | struct device *dev; |
| 151 | struct nvme_fc_lport *lport; |
| 152 | struct nvme_fc_rport *rport; |
| 153 | u32 cnum; |
| 154 | |
| 155 | u64 association_id; |
| 156 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 157 | struct list_head ctrl_list; /* rport->ctrl_list */ |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 158 | |
| 159 | struct blk_mq_tag_set admin_tag_set; |
| 160 | struct blk_mq_tag_set tag_set; |
| 161 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 162 | struct delayed_work connect_work; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 163 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 164 | struct kref ref; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 165 | u32 flags; |
| 166 | u32 iocnt; |
James Smart | 36715cf | 2017-05-22 15:28:42 -0700 | [diff] [blame] | 167 | wait_queue_head_t ioabort_wait; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 168 | |
| 169 | struct nvme_fc_fcp_op aen_ops[NVME_FC_NR_AEN_COMMANDS]; |
| 170 | |
| 171 | struct nvme_ctrl ctrl; |
| 172 | }; |
| 173 | |
| 174 | static inline struct nvme_fc_ctrl * |
| 175 | to_fc_ctrl(struct nvme_ctrl *ctrl) |
| 176 | { |
| 177 | return container_of(ctrl, struct nvme_fc_ctrl, ctrl); |
| 178 | } |
| 179 | |
| 180 | static inline struct nvme_fc_lport * |
| 181 | localport_to_lport(struct nvme_fc_local_port *portptr) |
| 182 | { |
| 183 | return container_of(portptr, struct nvme_fc_lport, localport); |
| 184 | } |
| 185 | |
| 186 | static inline struct nvme_fc_rport * |
| 187 | remoteport_to_rport(struct nvme_fc_remote_port *portptr) |
| 188 | { |
| 189 | return container_of(portptr, struct nvme_fc_rport, remoteport); |
| 190 | } |
| 191 | |
| 192 | static inline struct nvmefc_ls_req_op * |
| 193 | ls_req_to_lsop(struct nvmefc_ls_req *lsreq) |
| 194 | { |
| 195 | return container_of(lsreq, struct nvmefc_ls_req_op, ls_req); |
| 196 | } |
| 197 | |
| 198 | static inline struct nvme_fc_fcp_op * |
| 199 | fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq) |
| 200 | { |
| 201 | return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | /* *************************** Globals **************************** */ |
| 207 | |
| 208 | |
| 209 | static DEFINE_SPINLOCK(nvme_fc_lock); |
| 210 | |
| 211 | static LIST_HEAD(nvme_fc_lport_list); |
| 212 | static DEFINE_IDA(nvme_fc_local_port_cnt); |
| 213 | static DEFINE_IDA(nvme_fc_ctrl_cnt); |
| 214 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 215 | |
| 216 | |
James Smart | 5f56855 | 2017-09-14 10:38:41 -0700 | [diff] [blame] | 217 | /* |
| 218 | * These items are short-term. They will eventually be moved into |
| 219 | * a generic FC class. See comments in module init. |
| 220 | */ |
| 221 | static struct class *fc_class; |
| 222 | static struct device *fc_udev_device; |
| 223 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 224 | |
| 225 | /* *********************** FC-NVME Port Management ************************ */ |
| 226 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 227 | static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *, |
| 228 | struct nvme_fc_queue *, unsigned int); |
| 229 | |
James Smart | 5533d42 | 2017-07-31 13:20:30 -0700 | [diff] [blame] | 230 | static void |
| 231 | nvme_fc_free_lport(struct kref *ref) |
| 232 | { |
| 233 | struct nvme_fc_lport *lport = |
| 234 | container_of(ref, struct nvme_fc_lport, ref); |
| 235 | unsigned long flags; |
| 236 | |
| 237 | WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED); |
| 238 | WARN_ON(!list_empty(&lport->endp_list)); |
| 239 | |
| 240 | /* remove from transport list */ |
| 241 | spin_lock_irqsave(&nvme_fc_lock, flags); |
| 242 | list_del(&lport->port_list); |
| 243 | spin_unlock_irqrestore(&nvme_fc_lock, flags); |
| 244 | |
| 245 | /* let the LLDD know we've finished tearing it down */ |
| 246 | lport->ops->localport_delete(&lport->localport); |
| 247 | |
| 248 | ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num); |
| 249 | ida_destroy(&lport->endp_cnt); |
| 250 | |
| 251 | put_device(lport->dev); |
| 252 | |
| 253 | kfree(lport); |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | nvme_fc_lport_put(struct nvme_fc_lport *lport) |
| 258 | { |
| 259 | kref_put(&lport->ref, nvme_fc_free_lport); |
| 260 | } |
| 261 | |
| 262 | static int |
| 263 | nvme_fc_lport_get(struct nvme_fc_lport *lport) |
| 264 | { |
| 265 | return kref_get_unless_zero(&lport->ref); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | static struct nvme_fc_lport * |
| 270 | nvme_fc_attach_to_unreg_lport(struct nvme_fc_port_info *pinfo) |
| 271 | { |
| 272 | struct nvme_fc_lport *lport; |
| 273 | unsigned long flags; |
| 274 | |
| 275 | spin_lock_irqsave(&nvme_fc_lock, flags); |
| 276 | |
| 277 | list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { |
| 278 | if (lport->localport.node_name != pinfo->node_name || |
| 279 | lport->localport.port_name != pinfo->port_name) |
| 280 | continue; |
| 281 | |
| 282 | if (lport->localport.port_state != FC_OBJSTATE_DELETED) { |
| 283 | lport = ERR_PTR(-EEXIST); |
| 284 | goto out_done; |
| 285 | } |
| 286 | |
| 287 | if (!nvme_fc_lport_get(lport)) { |
| 288 | /* |
| 289 | * fails if ref cnt already 0. If so, |
| 290 | * act as if lport already deleted |
| 291 | */ |
| 292 | lport = NULL; |
| 293 | goto out_done; |
| 294 | } |
| 295 | |
| 296 | /* resume the lport */ |
| 297 | |
| 298 | lport->localport.port_role = pinfo->port_role; |
| 299 | lport->localport.port_id = pinfo->port_id; |
| 300 | lport->localport.port_state = FC_OBJSTATE_ONLINE; |
| 301 | |
| 302 | spin_unlock_irqrestore(&nvme_fc_lock, flags); |
| 303 | |
| 304 | return lport; |
| 305 | } |
| 306 | |
| 307 | lport = NULL; |
| 308 | |
| 309 | out_done: |
| 310 | spin_unlock_irqrestore(&nvme_fc_lock, flags); |
| 311 | |
| 312 | return lport; |
| 313 | } |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * nvme_fc_register_localport - transport entry point called by an |
| 317 | * LLDD to register the existence of a NVME |
| 318 | * host FC port. |
| 319 | * @pinfo: pointer to information about the port to be registered |
| 320 | * @template: LLDD entrypoints and operational parameters for the port |
| 321 | * @dev: physical hardware device node port corresponds to. Will be |
| 322 | * used for DMA mappings |
| 323 | * @lport_p: pointer to a local port pointer. Upon success, the routine |
| 324 | * will allocate a nvme_fc_local_port structure and place its |
| 325 | * address in the local port pointer. Upon failure, local port |
| 326 | * pointer will be set to 0. |
| 327 | * |
| 328 | * Returns: |
| 329 | * a completion status. Must be 0 upon success; a negative errno |
| 330 | * (ex: -ENXIO) upon failure. |
| 331 | */ |
| 332 | int |
| 333 | nvme_fc_register_localport(struct nvme_fc_port_info *pinfo, |
| 334 | struct nvme_fc_port_template *template, |
| 335 | struct device *dev, |
| 336 | struct nvme_fc_local_port **portptr) |
| 337 | { |
| 338 | struct nvme_fc_lport *newrec; |
| 339 | unsigned long flags; |
| 340 | int ret, idx; |
| 341 | |
| 342 | if (!template->localport_delete || !template->remoteport_delete || |
| 343 | !template->ls_req || !template->fcp_io || |
| 344 | !template->ls_abort || !template->fcp_abort || |
| 345 | !template->max_hw_queues || !template->max_sgl_segments || |
| 346 | !template->max_dif_sgl_segments || !template->dma_boundary) { |
| 347 | ret = -EINVAL; |
| 348 | goto out_reghost_failed; |
| 349 | } |
| 350 | |
James Smart | 5533d42 | 2017-07-31 13:20:30 -0700 | [diff] [blame] | 351 | /* |
| 352 | * look to see if there is already a localport that had been |
| 353 | * deregistered and in the process of waiting for all the |
| 354 | * references to fully be removed. If the references haven't |
| 355 | * expired, we can simply re-enable the localport. Remoteports |
| 356 | * and controller reconnections should resume naturally. |
| 357 | */ |
| 358 | newrec = nvme_fc_attach_to_unreg_lport(pinfo); |
| 359 | |
| 360 | /* found an lport, but something about its state is bad */ |
| 361 | if (IS_ERR(newrec)) { |
| 362 | ret = PTR_ERR(newrec); |
| 363 | goto out_reghost_failed; |
| 364 | |
| 365 | /* found existing lport, which was resumed */ |
| 366 | } else if (newrec) { |
| 367 | *portptr = &newrec->localport; |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /* nothing found - allocate a new localport struct */ |
| 372 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 373 | newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz), |
| 374 | GFP_KERNEL); |
| 375 | if (!newrec) { |
| 376 | ret = -ENOMEM; |
| 377 | goto out_reghost_failed; |
| 378 | } |
| 379 | |
| 380 | idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL); |
| 381 | if (idx < 0) { |
| 382 | ret = -ENOSPC; |
| 383 | goto out_fail_kfree; |
| 384 | } |
| 385 | |
| 386 | if (!get_device(dev) && dev) { |
| 387 | ret = -ENODEV; |
| 388 | goto out_ida_put; |
| 389 | } |
| 390 | |
| 391 | INIT_LIST_HEAD(&newrec->port_list); |
| 392 | INIT_LIST_HEAD(&newrec->endp_list); |
| 393 | kref_init(&newrec->ref); |
| 394 | newrec->ops = template; |
| 395 | newrec->dev = dev; |
| 396 | ida_init(&newrec->endp_cnt); |
| 397 | newrec->localport.private = &newrec[1]; |
| 398 | newrec->localport.node_name = pinfo->node_name; |
| 399 | newrec->localport.port_name = pinfo->port_name; |
| 400 | newrec->localport.port_role = pinfo->port_role; |
| 401 | newrec->localport.port_id = pinfo->port_id; |
| 402 | newrec->localport.port_state = FC_OBJSTATE_ONLINE; |
| 403 | newrec->localport.port_num = idx; |
| 404 | |
| 405 | spin_lock_irqsave(&nvme_fc_lock, flags); |
| 406 | list_add_tail(&newrec->port_list, &nvme_fc_lport_list); |
| 407 | spin_unlock_irqrestore(&nvme_fc_lock, flags); |
| 408 | |
| 409 | if (dev) |
| 410 | dma_set_seg_boundary(dev, template->dma_boundary); |
| 411 | |
| 412 | *portptr = &newrec->localport; |
| 413 | return 0; |
| 414 | |
| 415 | out_ida_put: |
| 416 | ida_simple_remove(&nvme_fc_local_port_cnt, idx); |
| 417 | out_fail_kfree: |
| 418 | kfree(newrec); |
| 419 | out_reghost_failed: |
| 420 | *portptr = NULL; |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | EXPORT_SYMBOL_GPL(nvme_fc_register_localport); |
| 425 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 426 | /** |
| 427 | * nvme_fc_unregister_localport - transport entry point called by an |
| 428 | * LLDD to deregister/remove a previously |
| 429 | * registered a NVME host FC port. |
| 430 | * @localport: pointer to the (registered) local port that is to be |
| 431 | * deregistered. |
| 432 | * |
| 433 | * Returns: |
| 434 | * a completion status. Must be 0 upon success; a negative errno |
| 435 | * (ex: -ENXIO) upon failure. |
| 436 | */ |
| 437 | int |
| 438 | nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr) |
| 439 | { |
| 440 | struct nvme_fc_lport *lport = localport_to_lport(portptr); |
| 441 | unsigned long flags; |
| 442 | |
| 443 | if (!portptr) |
| 444 | return -EINVAL; |
| 445 | |
| 446 | spin_lock_irqsave(&nvme_fc_lock, flags); |
| 447 | |
| 448 | if (portptr->port_state != FC_OBJSTATE_ONLINE) { |
| 449 | spin_unlock_irqrestore(&nvme_fc_lock, flags); |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | portptr->port_state = FC_OBJSTATE_DELETED; |
| 453 | |
| 454 | spin_unlock_irqrestore(&nvme_fc_lock, flags); |
| 455 | |
| 456 | nvme_fc_lport_put(lport); |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport); |
| 461 | |
James Smart | eaefd5a | 2017-09-14 10:38:42 -0700 | [diff] [blame] | 462 | /* |
| 463 | * TRADDR strings, per FC-NVME are fixed format: |
| 464 | * "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters |
| 465 | * udev event will only differ by prefix of what field is |
| 466 | * being specified: |
| 467 | * "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters |
| 468 | * 19 + 43 + null_fudge = 64 characters |
| 469 | */ |
| 470 | #define FCNVME_TRADDR_LENGTH 64 |
| 471 | |
| 472 | static void |
| 473 | nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport, |
| 474 | struct nvme_fc_rport *rport) |
| 475 | { |
| 476 | char hostaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_HOST_TRADDR=...*/ |
| 477 | char tgtaddr[FCNVME_TRADDR_LENGTH]; /* NVMEFC_TRADDR=...*/ |
| 478 | char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL }; |
| 479 | |
| 480 | if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY)) |
| 481 | return; |
| 482 | |
| 483 | snprintf(hostaddr, sizeof(hostaddr), |
| 484 | "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx", |
| 485 | lport->localport.node_name, lport->localport.port_name); |
| 486 | snprintf(tgtaddr, sizeof(tgtaddr), |
| 487 | "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx", |
| 488 | rport->remoteport.node_name, rport->remoteport.port_name); |
| 489 | kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp); |
| 490 | } |
| 491 | |
James Smart | 469d0ef | 2017-09-26 21:50:45 -0700 | [diff] [blame] | 492 | static void |
| 493 | nvme_fc_free_rport(struct kref *ref) |
| 494 | { |
| 495 | struct nvme_fc_rport *rport = |
| 496 | container_of(ref, struct nvme_fc_rport, ref); |
| 497 | struct nvme_fc_lport *lport = |
| 498 | localport_to_lport(rport->remoteport.localport); |
| 499 | unsigned long flags; |
| 500 | |
| 501 | WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED); |
| 502 | WARN_ON(!list_empty(&rport->ctrl_list)); |
| 503 | |
| 504 | /* remove from lport list */ |
| 505 | spin_lock_irqsave(&nvme_fc_lock, flags); |
| 506 | list_del(&rport->endp_list); |
| 507 | spin_unlock_irqrestore(&nvme_fc_lock, flags); |
| 508 | |
| 509 | /* let the LLDD know we've finished tearing it down */ |
| 510 | lport->ops->remoteport_delete(&rport->remoteport); |
| 511 | |
| 512 | ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num); |
| 513 | |
| 514 | kfree(rport); |
| 515 | |
| 516 | nvme_fc_lport_put(lport); |
| 517 | } |
| 518 | |
| 519 | static void |
| 520 | nvme_fc_rport_put(struct nvme_fc_rport *rport) |
| 521 | { |
| 522 | kref_put(&rport->ref, nvme_fc_free_rport); |
| 523 | } |
| 524 | |
| 525 | static int |
| 526 | nvme_fc_rport_get(struct nvme_fc_rport *rport) |
| 527 | { |
| 528 | return kref_get_unless_zero(&rport->ref); |
| 529 | } |
| 530 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 531 | /** |
| 532 | * nvme_fc_register_remoteport - transport entry point called by an |
| 533 | * LLDD to register the existence of a NVME |
| 534 | * subsystem FC port on its fabric. |
| 535 | * @localport: pointer to the (registered) local port that the remote |
| 536 | * subsystem port is connected to. |
| 537 | * @pinfo: pointer to information about the port to be registered |
| 538 | * @rport_p: pointer to a remote port pointer. Upon success, the routine |
| 539 | * will allocate a nvme_fc_remote_port structure and place its |
| 540 | * address in the remote port pointer. Upon failure, remote port |
| 541 | * pointer will be set to 0. |
| 542 | * |
| 543 | * Returns: |
| 544 | * a completion status. Must be 0 upon success; a negative errno |
| 545 | * (ex: -ENXIO) upon failure. |
| 546 | */ |
| 547 | int |
| 548 | nvme_fc_register_remoteport(struct nvme_fc_local_port *localport, |
| 549 | struct nvme_fc_port_info *pinfo, |
| 550 | struct nvme_fc_remote_port **portptr) |
| 551 | { |
| 552 | struct nvme_fc_lport *lport = localport_to_lport(localport); |
| 553 | struct nvme_fc_rport *newrec; |
| 554 | unsigned long flags; |
| 555 | int ret, idx; |
| 556 | |
| 557 | newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz), |
| 558 | GFP_KERNEL); |
| 559 | if (!newrec) { |
| 560 | ret = -ENOMEM; |
| 561 | goto out_reghost_failed; |
| 562 | } |
| 563 | |
| 564 | if (!nvme_fc_lport_get(lport)) { |
| 565 | ret = -ESHUTDOWN; |
| 566 | goto out_kfree_rport; |
| 567 | } |
| 568 | |
| 569 | idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL); |
| 570 | if (idx < 0) { |
| 571 | ret = -ENOSPC; |
| 572 | goto out_lport_put; |
| 573 | } |
| 574 | |
| 575 | INIT_LIST_HEAD(&newrec->endp_list); |
| 576 | INIT_LIST_HEAD(&newrec->ctrl_list); |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 577 | INIT_LIST_HEAD(&newrec->ls_req_list); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 578 | kref_init(&newrec->ref); |
| 579 | spin_lock_init(&newrec->lock); |
| 580 | newrec->remoteport.localport = &lport->localport; |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 581 | newrec->dev = lport->dev; |
| 582 | newrec->lport = lport; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 583 | newrec->remoteport.private = &newrec[1]; |
| 584 | newrec->remoteport.port_role = pinfo->port_role; |
| 585 | newrec->remoteport.node_name = pinfo->node_name; |
| 586 | newrec->remoteport.port_name = pinfo->port_name; |
| 587 | newrec->remoteport.port_id = pinfo->port_id; |
| 588 | newrec->remoteport.port_state = FC_OBJSTATE_ONLINE; |
| 589 | newrec->remoteport.port_num = idx; |
James Smart | ac7fe82 | 2017-10-25 16:43:15 -0700 | [diff] [blame] | 590 | /* a registration value of dev_loss_tmo=0 results in the default */ |
| 591 | if (pinfo->dev_loss_tmo) |
| 592 | newrec->remoteport.dev_loss_tmo = pinfo->dev_loss_tmo; |
| 593 | else |
| 594 | newrec->remoteport.dev_loss_tmo = NVME_FC_DEFAULT_DEV_LOSS_TMO; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 595 | |
| 596 | spin_lock_irqsave(&nvme_fc_lock, flags); |
| 597 | list_add_tail(&newrec->endp_list, &lport->endp_list); |
| 598 | spin_unlock_irqrestore(&nvme_fc_lock, flags); |
| 599 | |
James Smart | eaefd5a | 2017-09-14 10:38:42 -0700 | [diff] [blame] | 600 | nvme_fc_signal_discovery_scan(lport, newrec); |
| 601 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 602 | *portptr = &newrec->remoteport; |
| 603 | return 0; |
| 604 | |
| 605 | out_lport_put: |
| 606 | nvme_fc_lport_put(lport); |
| 607 | out_kfree_rport: |
| 608 | kfree(newrec); |
| 609 | out_reghost_failed: |
| 610 | *portptr = NULL; |
| 611 | return ret; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 612 | } |
| 613 | EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport); |
| 614 | |
James Smart | 8d64daf | 2017-04-11 11:35:09 -0700 | [diff] [blame] | 615 | static int |
| 616 | nvme_fc_abort_lsops(struct nvme_fc_rport *rport) |
| 617 | { |
| 618 | struct nvmefc_ls_req_op *lsop; |
| 619 | unsigned long flags; |
| 620 | |
| 621 | restart: |
| 622 | spin_lock_irqsave(&rport->lock, flags); |
| 623 | |
| 624 | list_for_each_entry(lsop, &rport->ls_req_list, lsreq_list) { |
| 625 | if (!(lsop->flags & FCOP_FLAGS_TERMIO)) { |
| 626 | lsop->flags |= FCOP_FLAGS_TERMIO; |
| 627 | spin_unlock_irqrestore(&rport->lock, flags); |
| 628 | rport->lport->ops->ls_abort(&rport->lport->localport, |
| 629 | &rport->remoteport, |
| 630 | &lsop->ls_req); |
| 631 | goto restart; |
| 632 | } |
| 633 | } |
| 634 | spin_unlock_irqrestore(&rport->lock, flags); |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 639 | /** |
| 640 | * nvme_fc_unregister_remoteport - transport entry point called by an |
| 641 | * LLDD to deregister/remove a previously |
| 642 | * registered a NVME subsystem FC port. |
| 643 | * @remoteport: pointer to the (registered) remote port that is to be |
| 644 | * deregistered. |
| 645 | * |
| 646 | * Returns: |
| 647 | * a completion status. Must be 0 upon success; a negative errno |
| 648 | * (ex: -ENXIO) upon failure. |
| 649 | */ |
| 650 | int |
| 651 | nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr) |
| 652 | { |
| 653 | struct nvme_fc_rport *rport = remoteport_to_rport(portptr); |
| 654 | struct nvme_fc_ctrl *ctrl; |
| 655 | unsigned long flags; |
| 656 | |
| 657 | if (!portptr) |
| 658 | return -EINVAL; |
| 659 | |
| 660 | spin_lock_irqsave(&rport->lock, flags); |
| 661 | |
| 662 | if (portptr->port_state != FC_OBJSTATE_ONLINE) { |
| 663 | spin_unlock_irqrestore(&rport->lock, flags); |
| 664 | return -EINVAL; |
| 665 | } |
| 666 | portptr->port_state = FC_OBJSTATE_DELETED; |
| 667 | |
| 668 | /* tear down all associations to the remote port */ |
| 669 | list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 670 | nvme_delete_ctrl(&ctrl->ctrl); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 671 | |
| 672 | spin_unlock_irqrestore(&rport->lock, flags); |
| 673 | |
James Smart | 8d64daf | 2017-04-11 11:35:09 -0700 | [diff] [blame] | 674 | nvme_fc_abort_lsops(rport); |
| 675 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 676 | nvme_fc_rport_put(rport); |
| 677 | return 0; |
| 678 | } |
| 679 | EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport); |
| 680 | |
James Smart | eaefd5a | 2017-09-14 10:38:42 -0700 | [diff] [blame] | 681 | /** |
| 682 | * nvme_fc_rescan_remoteport - transport entry point called by an |
| 683 | * LLDD to request a nvme device rescan. |
| 684 | * @remoteport: pointer to the (registered) remote port that is to be |
| 685 | * rescanned. |
| 686 | * |
| 687 | * Returns: N/A |
| 688 | */ |
| 689 | void |
| 690 | nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport) |
| 691 | { |
| 692 | struct nvme_fc_rport *rport = remoteport_to_rport(remoteport); |
| 693 | |
| 694 | nvme_fc_signal_discovery_scan(rport->lport, rport); |
| 695 | } |
| 696 | EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport); |
| 697 | |
James Smart | ac7fe82 | 2017-10-25 16:43:15 -0700 | [diff] [blame] | 698 | int |
| 699 | nvme_fc_set_remoteport_devloss(struct nvme_fc_remote_port *portptr, |
| 700 | u32 dev_loss_tmo) |
| 701 | { |
| 702 | struct nvme_fc_rport *rport = remoteport_to_rport(portptr); |
| 703 | struct nvme_fc_ctrl *ctrl; |
| 704 | unsigned long flags; |
| 705 | |
| 706 | spin_lock_irqsave(&rport->lock, flags); |
| 707 | |
| 708 | if (portptr->port_state != FC_OBJSTATE_ONLINE) { |
| 709 | spin_unlock_irqrestore(&rport->lock, flags); |
| 710 | return -EINVAL; |
| 711 | } |
| 712 | |
| 713 | /* a dev_loss_tmo of 0 (immediate) is allowed to be set */ |
| 714 | rport->remoteport.dev_loss_tmo = dev_loss_tmo; |
| 715 | |
| 716 | spin_unlock_irqrestore(&rport->lock, flags); |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | EXPORT_SYMBOL_GPL(nvme_fc_set_remoteport_devloss); |
| 721 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 722 | |
| 723 | /* *********************** FC-NVME DMA Handling **************************** */ |
| 724 | |
| 725 | /* |
| 726 | * The fcloop device passes in a NULL device pointer. Real LLD's will |
| 727 | * pass in a valid device pointer. If NULL is passed to the dma mapping |
| 728 | * routines, depending on the platform, it may or may not succeed, and |
| 729 | * may crash. |
| 730 | * |
| 731 | * As such: |
| 732 | * Wrapper all the dma routines and check the dev pointer. |
| 733 | * |
| 734 | * If simple mappings (return just a dma address, we'll noop them, |
| 735 | * returning a dma address of 0. |
| 736 | * |
| 737 | * On more complex mappings (dma_map_sg), a pseudo routine fills |
| 738 | * in the scatter list, setting all dma addresses to 0. |
| 739 | */ |
| 740 | |
| 741 | static inline dma_addr_t |
| 742 | fc_dma_map_single(struct device *dev, void *ptr, size_t size, |
| 743 | enum dma_data_direction dir) |
| 744 | { |
| 745 | return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L; |
| 746 | } |
| 747 | |
| 748 | static inline int |
| 749 | fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr) |
| 750 | { |
| 751 | return dev ? dma_mapping_error(dev, dma_addr) : 0; |
| 752 | } |
| 753 | |
| 754 | static inline void |
| 755 | fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size, |
| 756 | enum dma_data_direction dir) |
| 757 | { |
| 758 | if (dev) |
| 759 | dma_unmap_single(dev, addr, size, dir); |
| 760 | } |
| 761 | |
| 762 | static inline void |
| 763 | fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size, |
| 764 | enum dma_data_direction dir) |
| 765 | { |
| 766 | if (dev) |
| 767 | dma_sync_single_for_cpu(dev, addr, size, dir); |
| 768 | } |
| 769 | |
| 770 | static inline void |
| 771 | fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size, |
| 772 | enum dma_data_direction dir) |
| 773 | { |
| 774 | if (dev) |
| 775 | dma_sync_single_for_device(dev, addr, size, dir); |
| 776 | } |
| 777 | |
| 778 | /* pseudo dma_map_sg call */ |
| 779 | static int |
| 780 | fc_map_sg(struct scatterlist *sg, int nents) |
| 781 | { |
| 782 | struct scatterlist *s; |
| 783 | int i; |
| 784 | |
| 785 | WARN_ON(nents == 0 || sg[0].length == 0); |
| 786 | |
| 787 | for_each_sg(sg, s, nents, i) { |
| 788 | s->dma_address = 0L; |
| 789 | #ifdef CONFIG_NEED_SG_DMA_LENGTH |
| 790 | s->dma_length = s->length; |
| 791 | #endif |
| 792 | } |
| 793 | return nents; |
| 794 | } |
| 795 | |
| 796 | static inline int |
| 797 | fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 798 | enum dma_data_direction dir) |
| 799 | { |
| 800 | return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents); |
| 801 | } |
| 802 | |
| 803 | static inline void |
| 804 | fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 805 | enum dma_data_direction dir) |
| 806 | { |
| 807 | if (dev) |
| 808 | dma_unmap_sg(dev, sg, nents, dir); |
| 809 | } |
| 810 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 811 | /* *********************** FC-NVME LS Handling **************************** */ |
| 812 | |
| 813 | static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *); |
| 814 | static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *); |
| 815 | |
| 816 | |
| 817 | static void |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 818 | __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 819 | { |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 820 | struct nvme_fc_rport *rport = lsop->rport; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 821 | struct nvmefc_ls_req *lsreq = &lsop->ls_req; |
| 822 | unsigned long flags; |
| 823 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 824 | spin_lock_irqsave(&rport->lock, flags); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 825 | |
| 826 | if (!lsop->req_queued) { |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 827 | spin_unlock_irqrestore(&rport->lock, flags); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 828 | return; |
| 829 | } |
| 830 | |
| 831 | list_del(&lsop->lsreq_list); |
| 832 | |
| 833 | lsop->req_queued = false; |
| 834 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 835 | spin_unlock_irqrestore(&rport->lock, flags); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 836 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 837 | fc_dma_unmap_single(rport->dev, lsreq->rqstdma, |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 838 | (lsreq->rqstlen + lsreq->rsplen), |
| 839 | DMA_BIDIRECTIONAL); |
| 840 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 841 | nvme_fc_rport_put(rport); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | static int |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 845 | __nvme_fc_send_ls_req(struct nvme_fc_rport *rport, |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 846 | struct nvmefc_ls_req_op *lsop, |
| 847 | void (*done)(struct nvmefc_ls_req *req, int status)) |
| 848 | { |
| 849 | struct nvmefc_ls_req *lsreq = &lsop->ls_req; |
| 850 | unsigned long flags; |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 851 | int ret = 0; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 852 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 853 | if (rport->remoteport.port_state != FC_OBJSTATE_ONLINE) |
| 854 | return -ECONNREFUSED; |
| 855 | |
| 856 | if (!nvme_fc_rport_get(rport)) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 857 | return -ESHUTDOWN; |
| 858 | |
| 859 | lsreq->done = done; |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 860 | lsop->rport = rport; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 861 | lsop->req_queued = false; |
| 862 | INIT_LIST_HEAD(&lsop->lsreq_list); |
| 863 | init_completion(&lsop->ls_done); |
| 864 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 865 | lsreq->rqstdma = fc_dma_map_single(rport->dev, lsreq->rqstaddr, |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 866 | lsreq->rqstlen + lsreq->rsplen, |
| 867 | DMA_BIDIRECTIONAL); |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 868 | if (fc_dma_mapping_error(rport->dev, lsreq->rqstdma)) { |
| 869 | ret = -EFAULT; |
| 870 | goto out_putrport; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 871 | } |
| 872 | lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen; |
| 873 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 874 | spin_lock_irqsave(&rport->lock, flags); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 875 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 876 | list_add_tail(&lsop->lsreq_list, &rport->ls_req_list); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 877 | |
| 878 | lsop->req_queued = true; |
| 879 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 880 | spin_unlock_irqrestore(&rport->lock, flags); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 881 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 882 | ret = rport->lport->ops->ls_req(&rport->lport->localport, |
| 883 | &rport->remoteport, lsreq); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 884 | if (ret) |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 885 | goto out_unlink; |
| 886 | |
| 887 | return 0; |
| 888 | |
| 889 | out_unlink: |
| 890 | lsop->ls_error = ret; |
| 891 | spin_lock_irqsave(&rport->lock, flags); |
| 892 | lsop->req_queued = false; |
| 893 | list_del(&lsop->lsreq_list); |
| 894 | spin_unlock_irqrestore(&rport->lock, flags); |
| 895 | fc_dma_unmap_single(rport->dev, lsreq->rqstdma, |
| 896 | (lsreq->rqstlen + lsreq->rsplen), |
| 897 | DMA_BIDIRECTIONAL); |
| 898 | out_putrport: |
| 899 | nvme_fc_rport_put(rport); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 900 | |
| 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | static void |
| 905 | nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status) |
| 906 | { |
| 907 | struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq); |
| 908 | |
| 909 | lsop->ls_error = status; |
| 910 | complete(&lsop->ls_done); |
| 911 | } |
| 912 | |
| 913 | static int |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 914 | nvme_fc_send_ls_req(struct nvme_fc_rport *rport, struct nvmefc_ls_req_op *lsop) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 915 | { |
| 916 | struct nvmefc_ls_req *lsreq = &lsop->ls_req; |
| 917 | struct fcnvme_ls_rjt *rjt = lsreq->rspaddr; |
| 918 | int ret; |
| 919 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 920 | ret = __nvme_fc_send_ls_req(rport, lsop, nvme_fc_send_ls_req_done); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 921 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 922 | if (!ret) { |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 923 | /* |
| 924 | * No timeout/not interruptible as we need the struct |
| 925 | * to exist until the lldd calls us back. Thus mandate |
| 926 | * wait until driver calls back. lldd responsible for |
| 927 | * the timeout action |
| 928 | */ |
| 929 | wait_for_completion(&lsop->ls_done); |
| 930 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 931 | __nvme_fc_finish_ls_req(lsop); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 932 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 933 | ret = lsop->ls_error; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 934 | } |
| 935 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 936 | if (ret) |
| 937 | return ret; |
| 938 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 939 | /* ACC or RJT payload ? */ |
| 940 | if (rjt->w0.ls_cmd == FCNVME_LS_RJT) |
| 941 | return -ENXIO; |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 946 | static int |
| 947 | nvme_fc_send_ls_req_async(struct nvme_fc_rport *rport, |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 948 | struct nvmefc_ls_req_op *lsop, |
| 949 | void (*done)(struct nvmefc_ls_req *req, int status)) |
| 950 | { |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 951 | /* don't wait for completion */ |
| 952 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 953 | return __nvme_fc_send_ls_req(rport, lsop, done); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* Validation Error indexes into the string table below */ |
| 957 | enum { |
| 958 | VERR_NO_ERROR = 0, |
| 959 | VERR_LSACC = 1, |
| 960 | VERR_LSDESC_RQST = 2, |
| 961 | VERR_LSDESC_RQST_LEN = 3, |
| 962 | VERR_ASSOC_ID = 4, |
| 963 | VERR_ASSOC_ID_LEN = 5, |
| 964 | VERR_CONN_ID = 6, |
| 965 | VERR_CONN_ID_LEN = 7, |
| 966 | VERR_CR_ASSOC = 8, |
| 967 | VERR_CR_ASSOC_ACC_LEN = 9, |
| 968 | VERR_CR_CONN = 10, |
| 969 | VERR_CR_CONN_ACC_LEN = 11, |
| 970 | VERR_DISCONN = 12, |
| 971 | VERR_DISCONN_ACC_LEN = 13, |
| 972 | }; |
| 973 | |
| 974 | static char *validation_errors[] = { |
| 975 | "OK", |
| 976 | "Not LS_ACC", |
| 977 | "Not LSDESC_RQST", |
| 978 | "Bad LSDESC_RQST Length", |
| 979 | "Not Association ID", |
| 980 | "Bad Association ID Length", |
| 981 | "Not Connection ID", |
| 982 | "Bad Connection ID Length", |
| 983 | "Not CR_ASSOC Rqst", |
| 984 | "Bad CR_ASSOC ACC Length", |
| 985 | "Not CR_CONN Rqst", |
| 986 | "Bad CR_CONN ACC Length", |
| 987 | "Not Disconnect Rqst", |
| 988 | "Bad Disconnect ACC Length", |
| 989 | }; |
| 990 | |
| 991 | static int |
| 992 | nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl, |
| 993 | struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio) |
| 994 | { |
| 995 | struct nvmefc_ls_req_op *lsop; |
| 996 | struct nvmefc_ls_req *lsreq; |
| 997 | struct fcnvme_ls_cr_assoc_rqst *assoc_rqst; |
| 998 | struct fcnvme_ls_cr_assoc_acc *assoc_acc; |
| 999 | int ret, fcret = 0; |
| 1000 | |
| 1001 | lsop = kzalloc((sizeof(*lsop) + |
| 1002 | ctrl->lport->ops->lsrqst_priv_sz + |
| 1003 | sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL); |
| 1004 | if (!lsop) { |
| 1005 | ret = -ENOMEM; |
| 1006 | goto out_no_memory; |
| 1007 | } |
| 1008 | lsreq = &lsop->ls_req; |
| 1009 | |
| 1010 | lsreq->private = (void *)&lsop[1]; |
| 1011 | assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *) |
| 1012 | (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz); |
| 1013 | assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1]; |
| 1014 | |
| 1015 | assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION; |
| 1016 | assoc_rqst->desc_list_len = |
| 1017 | cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd)); |
| 1018 | |
| 1019 | assoc_rqst->assoc_cmd.desc_tag = |
| 1020 | cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD); |
| 1021 | assoc_rqst->assoc_cmd.desc_len = |
| 1022 | fcnvme_lsdesc_len( |
| 1023 | sizeof(struct fcnvme_lsdesc_cr_assoc_cmd)); |
| 1024 | |
| 1025 | assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio); |
| 1026 | assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize); |
| 1027 | /* Linux supports only Dynamic controllers */ |
| 1028 | assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff); |
Christoph Hellwig | 8e41226 | 2017-05-17 09:54:27 +0200 | [diff] [blame] | 1029 | uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1030 | strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn, |
| 1031 | min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE)); |
| 1032 | strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn, |
| 1033 | min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE)); |
| 1034 | |
| 1035 | lsop->queue = queue; |
| 1036 | lsreq->rqstaddr = assoc_rqst; |
| 1037 | lsreq->rqstlen = sizeof(*assoc_rqst); |
| 1038 | lsreq->rspaddr = assoc_acc; |
| 1039 | lsreq->rsplen = sizeof(*assoc_acc); |
| 1040 | lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC; |
| 1041 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 1042 | ret = nvme_fc_send_ls_req(ctrl->rport, lsop); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1043 | if (ret) |
| 1044 | goto out_free_buffer; |
| 1045 | |
| 1046 | /* process connect LS completion */ |
| 1047 | |
| 1048 | /* validate the ACC response */ |
| 1049 | if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC) |
| 1050 | fcret = VERR_LSACC; |
James Smart | f77fc87 | 2017-03-23 20:41:25 -0700 | [diff] [blame] | 1051 | else if (assoc_acc->hdr.desc_list_len != |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1052 | fcnvme_lsdesc_len( |
| 1053 | sizeof(struct fcnvme_ls_cr_assoc_acc))) |
| 1054 | fcret = VERR_CR_ASSOC_ACC_LEN; |
James Smart | f77fc87 | 2017-03-23 20:41:25 -0700 | [diff] [blame] | 1055 | else if (assoc_acc->hdr.rqst.desc_tag != |
| 1056 | cpu_to_be32(FCNVME_LSDESC_RQST)) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1057 | fcret = VERR_LSDESC_RQST; |
| 1058 | else if (assoc_acc->hdr.rqst.desc_len != |
| 1059 | fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst))) |
| 1060 | fcret = VERR_LSDESC_RQST_LEN; |
| 1061 | else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION) |
| 1062 | fcret = VERR_CR_ASSOC; |
| 1063 | else if (assoc_acc->associd.desc_tag != |
| 1064 | cpu_to_be32(FCNVME_LSDESC_ASSOC_ID)) |
| 1065 | fcret = VERR_ASSOC_ID; |
| 1066 | else if (assoc_acc->associd.desc_len != |
| 1067 | fcnvme_lsdesc_len( |
| 1068 | sizeof(struct fcnvme_lsdesc_assoc_id))) |
| 1069 | fcret = VERR_ASSOC_ID_LEN; |
| 1070 | else if (assoc_acc->connectid.desc_tag != |
| 1071 | cpu_to_be32(FCNVME_LSDESC_CONN_ID)) |
| 1072 | fcret = VERR_CONN_ID; |
| 1073 | else if (assoc_acc->connectid.desc_len != |
| 1074 | fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id))) |
| 1075 | fcret = VERR_CONN_ID_LEN; |
| 1076 | |
| 1077 | if (fcret) { |
| 1078 | ret = -EBADF; |
| 1079 | dev_err(ctrl->dev, |
| 1080 | "q %d connect failed: %s\n", |
| 1081 | queue->qnum, validation_errors[fcret]); |
| 1082 | } else { |
| 1083 | ctrl->association_id = |
| 1084 | be64_to_cpu(assoc_acc->associd.association_id); |
| 1085 | queue->connection_id = |
| 1086 | be64_to_cpu(assoc_acc->connectid.connection_id); |
| 1087 | set_bit(NVME_FC_Q_CONNECTED, &queue->flags); |
| 1088 | } |
| 1089 | |
| 1090 | out_free_buffer: |
| 1091 | kfree(lsop); |
| 1092 | out_no_memory: |
| 1093 | if (ret) |
| 1094 | dev_err(ctrl->dev, |
| 1095 | "queue %d connect admin queue failed (%d).\n", |
| 1096 | queue->qnum, ret); |
| 1097 | return ret; |
| 1098 | } |
| 1099 | |
| 1100 | static int |
| 1101 | nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue, |
| 1102 | u16 qsize, u16 ersp_ratio) |
| 1103 | { |
| 1104 | struct nvmefc_ls_req_op *lsop; |
| 1105 | struct nvmefc_ls_req *lsreq; |
| 1106 | struct fcnvme_ls_cr_conn_rqst *conn_rqst; |
| 1107 | struct fcnvme_ls_cr_conn_acc *conn_acc; |
| 1108 | int ret, fcret = 0; |
| 1109 | |
| 1110 | lsop = kzalloc((sizeof(*lsop) + |
| 1111 | ctrl->lport->ops->lsrqst_priv_sz + |
| 1112 | sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL); |
| 1113 | if (!lsop) { |
| 1114 | ret = -ENOMEM; |
| 1115 | goto out_no_memory; |
| 1116 | } |
| 1117 | lsreq = &lsop->ls_req; |
| 1118 | |
| 1119 | lsreq->private = (void *)&lsop[1]; |
| 1120 | conn_rqst = (struct fcnvme_ls_cr_conn_rqst *) |
| 1121 | (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz); |
| 1122 | conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1]; |
| 1123 | |
| 1124 | conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION; |
| 1125 | conn_rqst->desc_list_len = cpu_to_be32( |
| 1126 | sizeof(struct fcnvme_lsdesc_assoc_id) + |
| 1127 | sizeof(struct fcnvme_lsdesc_cr_conn_cmd)); |
| 1128 | |
| 1129 | conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID); |
| 1130 | conn_rqst->associd.desc_len = |
| 1131 | fcnvme_lsdesc_len( |
| 1132 | sizeof(struct fcnvme_lsdesc_assoc_id)); |
| 1133 | conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id); |
| 1134 | conn_rqst->connect_cmd.desc_tag = |
| 1135 | cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD); |
| 1136 | conn_rqst->connect_cmd.desc_len = |
| 1137 | fcnvme_lsdesc_len( |
| 1138 | sizeof(struct fcnvme_lsdesc_cr_conn_cmd)); |
| 1139 | conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio); |
| 1140 | conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum); |
| 1141 | conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize); |
| 1142 | |
| 1143 | lsop->queue = queue; |
| 1144 | lsreq->rqstaddr = conn_rqst; |
| 1145 | lsreq->rqstlen = sizeof(*conn_rqst); |
| 1146 | lsreq->rspaddr = conn_acc; |
| 1147 | lsreq->rsplen = sizeof(*conn_acc); |
| 1148 | lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC; |
| 1149 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 1150 | ret = nvme_fc_send_ls_req(ctrl->rport, lsop); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1151 | if (ret) |
| 1152 | goto out_free_buffer; |
| 1153 | |
| 1154 | /* process connect LS completion */ |
| 1155 | |
| 1156 | /* validate the ACC response */ |
| 1157 | if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC) |
| 1158 | fcret = VERR_LSACC; |
James Smart | f77fc87 | 2017-03-23 20:41:25 -0700 | [diff] [blame] | 1159 | else if (conn_acc->hdr.desc_list_len != |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1160 | fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc))) |
| 1161 | fcret = VERR_CR_CONN_ACC_LEN; |
James Smart | f77fc87 | 2017-03-23 20:41:25 -0700 | [diff] [blame] | 1162 | else if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST)) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1163 | fcret = VERR_LSDESC_RQST; |
| 1164 | else if (conn_acc->hdr.rqst.desc_len != |
| 1165 | fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst))) |
| 1166 | fcret = VERR_LSDESC_RQST_LEN; |
| 1167 | else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION) |
| 1168 | fcret = VERR_CR_CONN; |
| 1169 | else if (conn_acc->connectid.desc_tag != |
| 1170 | cpu_to_be32(FCNVME_LSDESC_CONN_ID)) |
| 1171 | fcret = VERR_CONN_ID; |
| 1172 | else if (conn_acc->connectid.desc_len != |
| 1173 | fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id))) |
| 1174 | fcret = VERR_CONN_ID_LEN; |
| 1175 | |
| 1176 | if (fcret) { |
| 1177 | ret = -EBADF; |
| 1178 | dev_err(ctrl->dev, |
| 1179 | "q %d connect failed: %s\n", |
| 1180 | queue->qnum, validation_errors[fcret]); |
| 1181 | } else { |
| 1182 | queue->connection_id = |
| 1183 | be64_to_cpu(conn_acc->connectid.connection_id); |
| 1184 | set_bit(NVME_FC_Q_CONNECTED, &queue->flags); |
| 1185 | } |
| 1186 | |
| 1187 | out_free_buffer: |
| 1188 | kfree(lsop); |
| 1189 | out_no_memory: |
| 1190 | if (ret) |
| 1191 | dev_err(ctrl->dev, |
| 1192 | "queue %d connect command failed (%d).\n", |
| 1193 | queue->qnum, ret); |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
| 1197 | static void |
| 1198 | nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status) |
| 1199 | { |
| 1200 | struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1201 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 1202 | __nvme_fc_finish_ls_req(lsop); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1203 | |
| 1204 | /* fc-nvme iniator doesn't care about success or failure of cmd */ |
| 1205 | |
| 1206 | kfree(lsop); |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * This routine sends a FC-NVME LS to disconnect (aka terminate) |
| 1211 | * the FC-NVME Association. Terminating the association also |
| 1212 | * terminates the FC-NVME connections (per queue, both admin and io |
| 1213 | * queues) that are part of the association. E.g. things are torn |
| 1214 | * down, and the related FC-NVME Association ID and Connection IDs |
| 1215 | * become invalid. |
| 1216 | * |
| 1217 | * The behavior of the fc-nvme initiator is such that it's |
| 1218 | * understanding of the association and connections will implicitly |
| 1219 | * be torn down. The action is implicit as it may be due to a loss of |
| 1220 | * connectivity with the fc-nvme target, so you may never get a |
| 1221 | * response even if you tried. As such, the action of this routine |
| 1222 | * is to asynchronously send the LS, ignore any results of the LS, and |
| 1223 | * continue on with terminating the association. If the fc-nvme target |
| 1224 | * is present and receives the LS, it too can tear down. |
| 1225 | */ |
| 1226 | static void |
| 1227 | nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl) |
| 1228 | { |
| 1229 | struct fcnvme_ls_disconnect_rqst *discon_rqst; |
| 1230 | struct fcnvme_ls_disconnect_acc *discon_acc; |
| 1231 | struct nvmefc_ls_req_op *lsop; |
| 1232 | struct nvmefc_ls_req *lsreq; |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 1233 | int ret; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1234 | |
| 1235 | lsop = kzalloc((sizeof(*lsop) + |
| 1236 | ctrl->lport->ops->lsrqst_priv_sz + |
| 1237 | sizeof(*discon_rqst) + sizeof(*discon_acc)), |
| 1238 | GFP_KERNEL); |
| 1239 | if (!lsop) |
| 1240 | /* couldn't sent it... too bad */ |
| 1241 | return; |
| 1242 | |
| 1243 | lsreq = &lsop->ls_req; |
| 1244 | |
| 1245 | lsreq->private = (void *)&lsop[1]; |
| 1246 | discon_rqst = (struct fcnvme_ls_disconnect_rqst *) |
| 1247 | (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz); |
| 1248 | discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1]; |
| 1249 | |
| 1250 | discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT; |
| 1251 | discon_rqst->desc_list_len = cpu_to_be32( |
| 1252 | sizeof(struct fcnvme_lsdesc_assoc_id) + |
| 1253 | sizeof(struct fcnvme_lsdesc_disconn_cmd)); |
| 1254 | |
| 1255 | discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID); |
| 1256 | discon_rqst->associd.desc_len = |
| 1257 | fcnvme_lsdesc_len( |
| 1258 | sizeof(struct fcnvme_lsdesc_assoc_id)); |
| 1259 | |
| 1260 | discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id); |
| 1261 | |
| 1262 | discon_rqst->discon_cmd.desc_tag = cpu_to_be32( |
| 1263 | FCNVME_LSDESC_DISCONN_CMD); |
| 1264 | discon_rqst->discon_cmd.desc_len = |
| 1265 | fcnvme_lsdesc_len( |
| 1266 | sizeof(struct fcnvme_lsdesc_disconn_cmd)); |
| 1267 | discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION; |
| 1268 | discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id); |
| 1269 | |
| 1270 | lsreq->rqstaddr = discon_rqst; |
| 1271 | lsreq->rqstlen = sizeof(*discon_rqst); |
| 1272 | lsreq->rspaddr = discon_acc; |
| 1273 | lsreq->rsplen = sizeof(*discon_acc); |
| 1274 | lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC; |
| 1275 | |
James Smart | c913a8b | 2017-04-11 11:35:08 -0700 | [diff] [blame] | 1276 | ret = nvme_fc_send_ls_req_async(ctrl->rport, lsop, |
| 1277 | nvme_fc_disconnect_assoc_done); |
| 1278 | if (ret) |
| 1279 | kfree(lsop); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1280 | |
| 1281 | /* only meaningful part to terminating the association */ |
| 1282 | ctrl->association_id = 0; |
| 1283 | } |
| 1284 | |
| 1285 | |
| 1286 | /* *********************** NVME Ctrl Routines **************************** */ |
| 1287 | |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1288 | static void __nvme_fc_final_op_cleanup(struct request *rq); |
James Smart | f874d5d | 2017-06-01 22:54:21 -0700 | [diff] [blame] | 1289 | static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1290 | |
| 1291 | static int |
| 1292 | nvme_fc_reinit_request(void *data, struct request *rq) |
| 1293 | { |
| 1294 | struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); |
| 1295 | struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; |
| 1296 | |
| 1297 | memset(cmdiu, 0, sizeof(*cmdiu)); |
| 1298 | cmdiu->scsi_id = NVME_CMD_SCSI_ID; |
| 1299 | cmdiu->fc_id = NVME_CMD_FC_ID; |
| 1300 | cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32)); |
| 1301 | memset(&op->rsp_iu, 0, sizeof(op->rsp_iu)); |
| 1302 | |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | static void |
| 1307 | __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl, |
| 1308 | struct nvme_fc_fcp_op *op) |
| 1309 | { |
| 1310 | fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma, |
| 1311 | sizeof(op->rsp_iu), DMA_FROM_DEVICE); |
| 1312 | fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma, |
| 1313 | sizeof(op->cmd_iu), DMA_TO_DEVICE); |
| 1314 | |
| 1315 | atomic_set(&op->state, FCPOP_STATE_UNINIT); |
| 1316 | } |
| 1317 | |
| 1318 | static void |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 1319 | nvme_fc_exit_request(struct blk_mq_tag_set *set, struct request *rq, |
| 1320 | unsigned int hctx_idx) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1321 | { |
| 1322 | struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); |
| 1323 | |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 1324 | return __nvme_fc_exit_request(set->driver_data, op); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1325 | } |
| 1326 | |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1327 | static int |
| 1328 | __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op) |
| 1329 | { |
| 1330 | int state; |
| 1331 | |
| 1332 | state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED); |
| 1333 | if (state != FCPOP_STATE_ACTIVE) { |
| 1334 | atomic_set(&op->state, state); |
| 1335 | return -ECANCELED; |
| 1336 | } |
| 1337 | |
| 1338 | ctrl->lport->ops->fcp_abort(&ctrl->lport->localport, |
| 1339 | &ctrl->rport->remoteport, |
| 1340 | op->queue->lldd_handle, |
| 1341 | &op->fcp_req); |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1346 | static void |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1347 | nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1348 | { |
| 1349 | struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops; |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1350 | unsigned long flags; |
| 1351 | int i, ret; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1352 | |
| 1353 | for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) { |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1354 | if (atomic_read(&aen_op->state) != FCPOP_STATE_ACTIVE) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1355 | continue; |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1356 | |
| 1357 | spin_lock_irqsave(&ctrl->lock, flags); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1358 | if (ctrl->flags & FCCTRL_TERMIO) { |
| 1359 | ctrl->iocnt++; |
| 1360 | aen_op->flags |= FCOP_FLAGS_TERMIO; |
| 1361 | } |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1362 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 1363 | |
| 1364 | ret = __nvme_fc_abort_op(ctrl, aen_op); |
| 1365 | if (ret) { |
| 1366 | /* |
| 1367 | * if __nvme_fc_abort_op failed the io wasn't |
| 1368 | * active. Thus this call path is running in |
| 1369 | * parallel to the io complete. Treat as non-error. |
| 1370 | */ |
| 1371 | |
| 1372 | /* back out the flags/counters */ |
| 1373 | spin_lock_irqsave(&ctrl->lock, flags); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1374 | if (ctrl->flags & FCCTRL_TERMIO) |
| 1375 | ctrl->iocnt--; |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1376 | aen_op->flags &= ~FCOP_FLAGS_TERMIO; |
| 1377 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 1378 | return; |
| 1379 | } |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1383 | static inline int |
| 1384 | __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl, |
| 1385 | struct nvme_fc_fcp_op *op) |
| 1386 | { |
| 1387 | unsigned long flags; |
| 1388 | bool complete_rq = false; |
| 1389 | |
| 1390 | spin_lock_irqsave(&ctrl->lock, flags); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1391 | if (unlikely(op->flags & FCOP_FLAGS_TERMIO)) { |
James Smart | 36715cf | 2017-05-22 15:28:42 -0700 | [diff] [blame] | 1392 | if (ctrl->flags & FCCTRL_TERMIO) { |
| 1393 | if (!--ctrl->iocnt) |
| 1394 | wake_up(&ctrl->ioabort_wait); |
| 1395 | } |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1396 | } |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1397 | if (op->flags & FCOP_FLAGS_RELEASED) |
| 1398 | complete_rq = true; |
| 1399 | else |
| 1400 | op->flags |= FCOP_FLAGS_COMPLETE; |
| 1401 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 1402 | |
| 1403 | return complete_rq; |
| 1404 | } |
| 1405 | |
Christoph Hellwig | baee29a | 2017-04-21 10:44:06 +0200 | [diff] [blame] | 1406 | static void |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1407 | nvme_fc_fcpio_done(struct nvmefc_fcp_req *req) |
| 1408 | { |
| 1409 | struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req); |
| 1410 | struct request *rq = op->rq; |
| 1411 | struct nvmefc_fcp_req *freq = &op->fcp_req; |
| 1412 | struct nvme_fc_ctrl *ctrl = op->ctrl; |
| 1413 | struct nvme_fc_queue *queue = op->queue; |
| 1414 | struct nvme_completion *cqe = &op->rsp_iu.cqe; |
James Smart | 458f280 | 2017-04-23 08:30:06 -0700 | [diff] [blame] | 1415 | struct nvme_command *sqe = &op->cmd_iu.sqe; |
Christoph Hellwig | d663b69 | 2017-04-20 16:02:56 +0200 | [diff] [blame] | 1416 | __le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1); |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 1417 | union nvme_result result; |
James Smart | 0a02e39 | 2017-10-19 16:11:38 -0700 | [diff] [blame] | 1418 | bool terminate_assoc = true; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1419 | |
| 1420 | /* |
| 1421 | * WARNING: |
| 1422 | * The current linux implementation of a nvme controller |
| 1423 | * allocates a single tag set for all io queues and sizes |
| 1424 | * the io queues to fully hold all possible tags. Thus, the |
| 1425 | * implementation does not reference or care about the sqhd |
| 1426 | * value as it never needs to use the sqhd/sqtail pointers |
| 1427 | * for submission pacing. |
| 1428 | * |
| 1429 | * This affects the FC-NVME implementation in two ways: |
| 1430 | * 1) As the value doesn't matter, we don't need to waste |
| 1431 | * cycles extracting it from ERSPs and stamping it in the |
| 1432 | * cases where the transport fabricates CQEs on successful |
| 1433 | * completions. |
| 1434 | * 2) The FC-NVME implementation requires that delivery of |
| 1435 | * ERSP completions are to go back to the nvme layer in order |
| 1436 | * relative to the rsn, such that the sqhd value will always |
| 1437 | * be "in order" for the nvme layer. As the nvme layer in |
| 1438 | * linux doesn't care about sqhd, there's no need to return |
| 1439 | * them in order. |
| 1440 | * |
| 1441 | * Additionally: |
| 1442 | * As the core nvme layer in linux currently does not look at |
| 1443 | * every field in the cqe - in cases where the FC transport must |
| 1444 | * fabricate a CQE, the following fields will not be set as they |
| 1445 | * are not referenced: |
| 1446 | * cqe.sqid, cqe.sqhd, cqe.command_id |
James Smart | f874d5d | 2017-06-01 22:54:21 -0700 | [diff] [blame] | 1447 | * |
| 1448 | * Failure or error of an individual i/o, in a transport |
| 1449 | * detected fashion unrelated to the nvme completion status, |
| 1450 | * potentially cause the initiator and target sides to get out |
| 1451 | * of sync on SQ head/tail (aka outstanding io count allowed). |
| 1452 | * Per FC-NVME spec, failure of an individual command requires |
| 1453 | * the connection to be terminated, which in turn requires the |
| 1454 | * association to be terminated. |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1455 | */ |
| 1456 | |
| 1457 | fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma, |
| 1458 | sizeof(op->rsp_iu), DMA_FROM_DEVICE); |
| 1459 | |
James Smart | 0a02e39 | 2017-10-19 16:11:38 -0700 | [diff] [blame] | 1460 | if (atomic_read(&op->state) == FCPOP_STATE_ABORTED || |
| 1461 | op->flags & FCOP_FLAGS_TERMIO) |
| 1462 | status = cpu_to_le16(NVME_SC_ABORT_REQ << 1); |
James Smart | 62eeacb | 2017-03-23 20:41:27 -0700 | [diff] [blame] | 1463 | else if (freq->status) |
James Smart | 56b7103a | 2017-09-07 16:27:26 -0700 | [diff] [blame] | 1464 | status = cpu_to_le16(NVME_SC_INTERNAL << 1); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1465 | |
| 1466 | /* |
| 1467 | * For the linux implementation, if we have an unsuccesful |
| 1468 | * status, they blk-mq layer can typically be called with the |
| 1469 | * non-zero status and the content of the cqe isn't important. |
| 1470 | */ |
| 1471 | if (status) |
| 1472 | goto done; |
| 1473 | |
| 1474 | /* |
| 1475 | * command completed successfully relative to the wire |
| 1476 | * protocol. However, validate anything received and |
| 1477 | * extract the status and result from the cqe (create it |
| 1478 | * where necessary). |
| 1479 | */ |
| 1480 | |
| 1481 | switch (freq->rcv_rsplen) { |
| 1482 | |
| 1483 | case 0: |
| 1484 | case NVME_FC_SIZEOF_ZEROS_RSP: |
| 1485 | /* |
| 1486 | * No response payload or 12 bytes of payload (which |
| 1487 | * should all be zeros) are considered successful and |
| 1488 | * no payload in the CQE by the transport. |
| 1489 | */ |
| 1490 | if (freq->transferred_length != |
| 1491 | be32_to_cpu(op->cmd_iu.data_len)) { |
James Smart | 56b7103a | 2017-09-07 16:27:26 -0700 | [diff] [blame] | 1492 | status = cpu_to_le16(NVME_SC_INTERNAL << 1); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1493 | goto done; |
| 1494 | } |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 1495 | result.u64 = 0; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1496 | break; |
| 1497 | |
| 1498 | case sizeof(struct nvme_fc_ersp_iu): |
| 1499 | /* |
| 1500 | * The ERSP IU contains a full completion with CQE. |
| 1501 | * Validate ERSP IU and look at cqe. |
| 1502 | */ |
| 1503 | if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) != |
| 1504 | (freq->rcv_rsplen / 4) || |
| 1505 | be32_to_cpu(op->rsp_iu.xfrd_len) != |
| 1506 | freq->transferred_length || |
James Smart | 726a108 | 2017-03-23 20:41:23 -0700 | [diff] [blame] | 1507 | op->rsp_iu.status_code || |
James Smart | 458f280 | 2017-04-23 08:30:06 -0700 | [diff] [blame] | 1508 | sqe->common.command_id != cqe->command_id)) { |
James Smart | 56b7103a | 2017-09-07 16:27:26 -0700 | [diff] [blame] | 1509 | status = cpu_to_le16(NVME_SC_INTERNAL << 1); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1510 | goto done; |
| 1511 | } |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 1512 | result = cqe->result; |
Christoph Hellwig | d663b69 | 2017-04-20 16:02:56 +0200 | [diff] [blame] | 1513 | status = cqe->status; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1514 | break; |
| 1515 | |
| 1516 | default: |
James Smart | 56b7103a | 2017-09-07 16:27:26 -0700 | [diff] [blame] | 1517 | status = cpu_to_le16(NVME_SC_INTERNAL << 1); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1518 | goto done; |
| 1519 | } |
| 1520 | |
James Smart | f874d5d | 2017-06-01 22:54:21 -0700 | [diff] [blame] | 1521 | terminate_assoc = false; |
| 1522 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1523 | done: |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1524 | if (op->flags & FCOP_FLAGS_AEN) { |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 1525 | nvme_complete_async_event(&queue->ctrl->ctrl, status, &result); |
James Smart | 0a02e39 | 2017-10-19 16:11:38 -0700 | [diff] [blame] | 1526 | __nvme_fc_fcpop_chk_teardowns(ctrl, op); |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1527 | atomic_set(&op->state, FCPOP_STATE_IDLE); |
| 1528 | op->flags = FCOP_FLAGS_AEN; /* clear other flags */ |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1529 | nvme_fc_ctrl_put(ctrl); |
James Smart | f874d5d | 2017-06-01 22:54:21 -0700 | [diff] [blame] | 1530 | goto check_error; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1531 | } |
| 1532 | |
James Smart | 0a02e39 | 2017-10-19 16:11:38 -0700 | [diff] [blame] | 1533 | /* |
| 1534 | * Force failures of commands if we're killing the controller |
| 1535 | * or have an error on a command used to create an new association |
| 1536 | */ |
| 1537 | if (status && |
| 1538 | (blk_queue_dying(rq->q) || |
| 1539 | ctrl->ctrl.state == NVME_CTRL_NEW || |
| 1540 | ctrl->ctrl.state == NVME_CTRL_RECONNECTING)) |
| 1541 | status |= cpu_to_le16(NVME_SC_DNR << 1); |
| 1542 | |
| 1543 | if (__nvme_fc_fcpop_chk_teardowns(ctrl, op)) |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1544 | __nvme_fc_final_op_cleanup(rq); |
James Smart | 0a02e39 | 2017-10-19 16:11:38 -0700 | [diff] [blame] | 1545 | else |
| 1546 | nvme_end_request(rq, status, result); |
James Smart | f874d5d | 2017-06-01 22:54:21 -0700 | [diff] [blame] | 1547 | |
| 1548 | check_error: |
| 1549 | if (terminate_assoc) |
| 1550 | nvme_fc_error_recovery(ctrl, "transport detected io error"); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | static int |
| 1554 | __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl, |
| 1555 | struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op, |
| 1556 | struct request *rq, u32 rqno) |
| 1557 | { |
| 1558 | struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; |
| 1559 | int ret = 0; |
| 1560 | |
| 1561 | memset(op, 0, sizeof(*op)); |
| 1562 | op->fcp_req.cmdaddr = &op->cmd_iu; |
| 1563 | op->fcp_req.cmdlen = sizeof(op->cmd_iu); |
| 1564 | op->fcp_req.rspaddr = &op->rsp_iu; |
| 1565 | op->fcp_req.rsplen = sizeof(op->rsp_iu); |
| 1566 | op->fcp_req.done = nvme_fc_fcpio_done; |
| 1567 | op->fcp_req.first_sgl = (struct scatterlist *)&op[1]; |
| 1568 | op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE]; |
| 1569 | op->ctrl = ctrl; |
| 1570 | op->queue = queue; |
| 1571 | op->rq = rq; |
| 1572 | op->rqno = rqno; |
| 1573 | |
| 1574 | cmdiu->scsi_id = NVME_CMD_SCSI_ID; |
| 1575 | cmdiu->fc_id = NVME_CMD_FC_ID; |
| 1576 | cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32)); |
| 1577 | |
| 1578 | op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev, |
| 1579 | &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE); |
| 1580 | if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) { |
| 1581 | dev_err(ctrl->dev, |
| 1582 | "FCP Op failed - cmdiu dma mapping failed.\n"); |
| 1583 | ret = EFAULT; |
| 1584 | goto out_on_error; |
| 1585 | } |
| 1586 | |
| 1587 | op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev, |
| 1588 | &op->rsp_iu, sizeof(op->rsp_iu), |
| 1589 | DMA_FROM_DEVICE); |
| 1590 | if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) { |
| 1591 | dev_err(ctrl->dev, |
| 1592 | "FCP Op failed - rspiu dma mapping failed.\n"); |
| 1593 | ret = EFAULT; |
| 1594 | } |
| 1595 | |
| 1596 | atomic_set(&op->state, FCPOP_STATE_IDLE); |
| 1597 | out_on_error: |
| 1598 | return ret; |
| 1599 | } |
| 1600 | |
| 1601 | static int |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 1602 | nvme_fc_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 1603 | unsigned int hctx_idx, unsigned int numa_node) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1604 | { |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 1605 | struct nvme_fc_ctrl *ctrl = set->driver_data; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1606 | struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | 76f983c | 2017-06-13 09:15:20 +0200 | [diff] [blame] | 1607 | int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0; |
| 1608 | struct nvme_fc_queue *queue = &ctrl->queues[queue_idx]; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1609 | |
| 1610 | return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++); |
| 1611 | } |
| 1612 | |
| 1613 | static int |
| 1614 | nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl) |
| 1615 | { |
| 1616 | struct nvme_fc_fcp_op *aen_op; |
| 1617 | struct nvme_fc_cmd_iu *cmdiu; |
| 1618 | struct nvme_command *sqe; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1619 | void *private; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1620 | int i, ret; |
| 1621 | |
| 1622 | aen_op = ctrl->aen_ops; |
| 1623 | for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) { |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1624 | private = kzalloc(ctrl->lport->ops->fcprqst_priv_sz, |
| 1625 | GFP_KERNEL); |
| 1626 | if (!private) |
| 1627 | return -ENOMEM; |
| 1628 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1629 | cmdiu = &aen_op->cmd_iu; |
| 1630 | sqe = &cmdiu->sqe; |
| 1631 | ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0], |
| 1632 | aen_op, (struct request *)NULL, |
| 1633 | (AEN_CMDID_BASE + i)); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1634 | if (ret) { |
| 1635 | kfree(private); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1636 | return ret; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1637 | } |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1638 | |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1639 | aen_op->flags = FCOP_FLAGS_AEN; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1640 | aen_op->fcp_req.first_sgl = NULL; /* no sg list */ |
| 1641 | aen_op->fcp_req.private = private; |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1642 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1643 | memset(sqe, 0, sizeof(*sqe)); |
| 1644 | sqe->common.opcode = nvme_admin_async_event; |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 1645 | /* Note: core layer may overwrite the sqe.command_id value */ |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1646 | sqe->common.command_id = AEN_CMDID_BASE + i; |
| 1647 | } |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1651 | static void |
| 1652 | nvme_fc_term_aen_ops(struct nvme_fc_ctrl *ctrl) |
| 1653 | { |
| 1654 | struct nvme_fc_fcp_op *aen_op; |
| 1655 | int i; |
| 1656 | |
| 1657 | aen_op = ctrl->aen_ops; |
| 1658 | for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) { |
| 1659 | if (!aen_op->fcp_req.private) |
| 1660 | continue; |
| 1661 | |
| 1662 | __nvme_fc_exit_request(ctrl, aen_op); |
| 1663 | |
| 1664 | kfree(aen_op->fcp_req.private); |
| 1665 | aen_op->fcp_req.private = NULL; |
| 1666 | } |
| 1667 | } |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1668 | |
| 1669 | static inline void |
| 1670 | __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl, |
| 1671 | unsigned int qidx) |
| 1672 | { |
| 1673 | struct nvme_fc_queue *queue = &ctrl->queues[qidx]; |
| 1674 | |
| 1675 | hctx->driver_data = queue; |
| 1676 | queue->hctx = hctx; |
| 1677 | } |
| 1678 | |
| 1679 | static int |
| 1680 | nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 1681 | unsigned int hctx_idx) |
| 1682 | { |
| 1683 | struct nvme_fc_ctrl *ctrl = data; |
| 1684 | |
| 1685 | __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1); |
| 1686 | |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | static int |
| 1691 | nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 1692 | unsigned int hctx_idx) |
| 1693 | { |
| 1694 | struct nvme_fc_ctrl *ctrl = data; |
| 1695 | |
| 1696 | __nvme_fc_init_hctx(hctx, ctrl, hctx_idx); |
| 1697 | |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | static void |
| 1702 | nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size) |
| 1703 | { |
| 1704 | struct nvme_fc_queue *queue; |
| 1705 | |
| 1706 | queue = &ctrl->queues[idx]; |
| 1707 | memset(queue, 0, sizeof(*queue)); |
| 1708 | queue->ctrl = ctrl; |
| 1709 | queue->qnum = idx; |
| 1710 | atomic_set(&queue->csn, 1); |
| 1711 | queue->dev = ctrl->dev; |
| 1712 | |
| 1713 | if (idx > 0) |
| 1714 | queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; |
| 1715 | else |
| 1716 | queue->cmnd_capsule_len = sizeof(struct nvme_command); |
| 1717 | |
| 1718 | queue->queue_size = queue_size; |
| 1719 | |
| 1720 | /* |
| 1721 | * Considered whether we should allocate buffers for all SQEs |
| 1722 | * and CQEs and dma map them - mapping their respective entries |
| 1723 | * into the request structures (kernel vm addr and dma address) |
| 1724 | * thus the driver could use the buffers/mappings directly. |
| 1725 | * It only makes sense if the LLDD would use them for its |
| 1726 | * messaging api. It's very unlikely most adapter api's would use |
| 1727 | * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload |
| 1728 | * structures were used instead. |
| 1729 | */ |
| 1730 | } |
| 1731 | |
| 1732 | /* |
| 1733 | * This routine terminates a queue at the transport level. |
| 1734 | * The transport has already ensured that all outstanding ios on |
| 1735 | * the queue have been terminated. |
| 1736 | * The transport will send a Disconnect LS request to terminate |
| 1737 | * the queue's connection. Termination of the admin queue will also |
| 1738 | * terminate the association at the target. |
| 1739 | */ |
| 1740 | static void |
| 1741 | nvme_fc_free_queue(struct nvme_fc_queue *queue) |
| 1742 | { |
| 1743 | if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags)) |
| 1744 | return; |
| 1745 | |
| 1746 | /* |
| 1747 | * Current implementation never disconnects a single queue. |
| 1748 | * It always terminates a whole association. So there is never |
| 1749 | * a disconnect(queue) LS sent to the target. |
| 1750 | */ |
| 1751 | |
| 1752 | queue->connection_id = 0; |
| 1753 | clear_bit(NVME_FC_Q_CONNECTED, &queue->flags); |
| 1754 | } |
| 1755 | |
| 1756 | static void |
| 1757 | __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl, |
| 1758 | struct nvme_fc_queue *queue, unsigned int qidx) |
| 1759 | { |
| 1760 | if (ctrl->lport->ops->delete_queue) |
| 1761 | ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx, |
| 1762 | queue->lldd_handle); |
| 1763 | queue->lldd_handle = NULL; |
| 1764 | } |
| 1765 | |
| 1766 | static void |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1767 | nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl) |
| 1768 | { |
| 1769 | int i; |
| 1770 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1771 | for (i = 1; i < ctrl->ctrl.queue_count; i++) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1772 | nvme_fc_free_queue(&ctrl->queues[i]); |
| 1773 | } |
| 1774 | |
| 1775 | static int |
| 1776 | __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl, |
| 1777 | struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize) |
| 1778 | { |
| 1779 | int ret = 0; |
| 1780 | |
| 1781 | queue->lldd_handle = NULL; |
| 1782 | if (ctrl->lport->ops->create_queue) |
| 1783 | ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport, |
| 1784 | qidx, qsize, &queue->lldd_handle); |
| 1785 | |
| 1786 | return ret; |
| 1787 | } |
| 1788 | |
| 1789 | static void |
| 1790 | nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl) |
| 1791 | { |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1792 | struct nvme_fc_queue *queue = &ctrl->queues[ctrl->ctrl.queue_count - 1]; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1793 | int i; |
| 1794 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1795 | for (i = ctrl->ctrl.queue_count - 1; i >= 1; i--, queue--) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1796 | __nvme_fc_delete_hw_queue(ctrl, queue, i); |
| 1797 | } |
| 1798 | |
| 1799 | static int |
| 1800 | nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize) |
| 1801 | { |
| 1802 | struct nvme_fc_queue *queue = &ctrl->queues[1]; |
Johannes Thumshirn | 17a1ec0 | 2016-12-15 14:20:48 +0100 | [diff] [blame] | 1803 | int i, ret; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1804 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1805 | for (i = 1; i < ctrl->ctrl.queue_count; i++, queue++) { |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1806 | ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize); |
Johannes Thumshirn | 17a1ec0 | 2016-12-15 14:20:48 +0100 | [diff] [blame] | 1807 | if (ret) |
| 1808 | goto delete_queues; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | return 0; |
Johannes Thumshirn | 17a1ec0 | 2016-12-15 14:20:48 +0100 | [diff] [blame] | 1812 | |
| 1813 | delete_queues: |
| 1814 | for (; i >= 0; i--) |
| 1815 | __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i); |
| 1816 | return ret; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | static int |
| 1820 | nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize) |
| 1821 | { |
| 1822 | int i, ret = 0; |
| 1823 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1824 | for (i = 1; i < ctrl->ctrl.queue_count; i++) { |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1825 | ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize, |
| 1826 | (qsize / 5)); |
| 1827 | if (ret) |
| 1828 | break; |
| 1829 | ret = nvmf_connect_io_queue(&ctrl->ctrl, i); |
| 1830 | if (ret) |
| 1831 | break; |
| 1832 | } |
| 1833 | |
| 1834 | return ret; |
| 1835 | } |
| 1836 | |
| 1837 | static void |
| 1838 | nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl) |
| 1839 | { |
| 1840 | int i; |
| 1841 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1842 | for (i = 1; i < ctrl->ctrl.queue_count; i++) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1843 | nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize); |
| 1844 | } |
| 1845 | |
| 1846 | static void |
| 1847 | nvme_fc_ctrl_free(struct kref *ref) |
| 1848 | { |
| 1849 | struct nvme_fc_ctrl *ctrl = |
| 1850 | container_of(ref, struct nvme_fc_ctrl, ref); |
| 1851 | unsigned long flags; |
| 1852 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1853 | if (ctrl->ctrl.tagset) { |
| 1854 | blk_cleanup_queue(ctrl->ctrl.connect_q); |
| 1855 | blk_mq_free_tag_set(&ctrl->tag_set); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1856 | } |
| 1857 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1858 | /* remove from rport list */ |
| 1859 | spin_lock_irqsave(&ctrl->rport->lock, flags); |
| 1860 | list_del(&ctrl->ctrl_list); |
| 1861 | spin_unlock_irqrestore(&ctrl->rport->lock, flags); |
| 1862 | |
Sagi Grimberg | f9c5af5 | 2017-07-02 15:39:34 +0300 | [diff] [blame] | 1863 | blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1864 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
| 1865 | blk_mq_free_tag_set(&ctrl->admin_tag_set); |
| 1866 | |
| 1867 | kfree(ctrl->queues); |
| 1868 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1869 | put_device(ctrl->dev); |
| 1870 | nvme_fc_rport_put(ctrl->rport); |
| 1871 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1872 | ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum); |
Ewan D. Milne | de41447 | 2017-04-24 13:24:16 -0400 | [diff] [blame] | 1873 | if (ctrl->ctrl.opts) |
| 1874 | nvmf_free_options(ctrl->ctrl.opts); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1875 | kfree(ctrl); |
| 1876 | } |
| 1877 | |
| 1878 | static void |
| 1879 | nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl) |
| 1880 | { |
| 1881 | kref_put(&ctrl->ref, nvme_fc_ctrl_free); |
| 1882 | } |
| 1883 | |
| 1884 | static int |
| 1885 | nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl) |
| 1886 | { |
| 1887 | return kref_get_unless_zero(&ctrl->ref); |
| 1888 | } |
| 1889 | |
| 1890 | /* |
| 1891 | * All accesses from nvme core layer done - can now free the |
| 1892 | * controller. Called after last nvme_put_ctrl() call |
| 1893 | */ |
| 1894 | static void |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1895 | nvme_fc_nvme_ctrl_freed(struct nvme_ctrl *nctrl) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1896 | { |
| 1897 | struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); |
| 1898 | |
| 1899 | WARN_ON(nctrl != &ctrl->ctrl); |
| 1900 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1901 | nvme_fc_ctrl_put(ctrl); |
| 1902 | } |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1903 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1904 | static void |
| 1905 | nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg) |
| 1906 | { |
James Smart | 69fa964 | 2017-06-21 17:43:21 -0700 | [diff] [blame] | 1907 | /* only proceed if in LIVE state - e.g. on first error */ |
| 1908 | if (ctrl->ctrl.state != NVME_CTRL_LIVE) |
| 1909 | return; |
| 1910 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1911 | dev_warn(ctrl->ctrl.device, |
| 1912 | "NVME-FC{%d}: transport association error detected: %s\n", |
| 1913 | ctrl->cnum, errmsg); |
James Smart | 589ff77 | 2017-05-15 17:10:19 -0700 | [diff] [blame] | 1914 | dev_warn(ctrl->ctrl.device, |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1915 | "NVME-FC{%d}: resetting controller\n", ctrl->cnum); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1916 | |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 1917 | nvme_reset_ctrl(&ctrl->ctrl); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
Christoph Hellwig | baee29a | 2017-04-21 10:44:06 +0200 | [diff] [blame] | 1920 | static enum blk_eh_timer_return |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1921 | nvme_fc_timeout(struct request *rq, bool reserved) |
| 1922 | { |
| 1923 | struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); |
| 1924 | struct nvme_fc_ctrl *ctrl = op->ctrl; |
| 1925 | int ret; |
| 1926 | |
James Smart | 134aedc | 2017-10-19 16:11:39 -0700 | [diff] [blame] | 1927 | if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE || |
| 1928 | atomic_read(&op->state) == FCPOP_STATE_ABORTED) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1929 | return BLK_EH_RESET_TIMER; |
| 1930 | |
| 1931 | ret = __nvme_fc_abort_op(ctrl, op); |
| 1932 | if (ret) |
James Smart | 134aedc | 2017-10-19 16:11:39 -0700 | [diff] [blame] | 1933 | /* io wasn't active to abort */ |
| 1934 | return BLK_EH_NOT_HANDLED; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1935 | |
| 1936 | /* |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1937 | * we can't individually ABTS an io without affecting the queue, |
| 1938 | * thus killing the queue, adn thus the association. |
| 1939 | * So resolve by performing a controller reset, which will stop |
| 1940 | * the host/io stack, terminate the association on the link, |
| 1941 | * and recreate an association on the link. |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1942 | */ |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 1943 | nvme_fc_error_recovery(ctrl, "io timeout error"); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1944 | |
James Smart | 134aedc | 2017-10-19 16:11:39 -0700 | [diff] [blame] | 1945 | /* |
| 1946 | * the io abort has been initiated. Have the reset timer |
| 1947 | * restarted and the abort completion will complete the io |
| 1948 | * shortly. Avoids a synchronous wait while the abort finishes. |
| 1949 | */ |
| 1950 | return BLK_EH_RESET_TIMER; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | static int |
| 1954 | nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq, |
| 1955 | struct nvme_fc_fcp_op *op) |
| 1956 | { |
| 1957 | struct nvmefc_fcp_req *freq = &op->fcp_req; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1958 | enum dma_data_direction dir; |
| 1959 | int ret; |
| 1960 | |
| 1961 | freq->sg_cnt = 0; |
| 1962 | |
Christoph Hellwig | b131c61 | 2017-01-13 12:29:12 +0100 | [diff] [blame] | 1963 | if (!blk_rq_payload_bytes(rq)) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1964 | return 0; |
| 1965 | |
| 1966 | freq->sg_table.sgl = freq->first_sgl; |
Christoph Hellwig | 19e420b | 2017-01-19 16:55:57 +0100 | [diff] [blame] | 1967 | ret = sg_alloc_table_chained(&freq->sg_table, |
| 1968 | blk_rq_nr_phys_segments(rq), freq->sg_table.sgl); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1969 | if (ret) |
| 1970 | return -ENOMEM; |
| 1971 | |
| 1972 | op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl); |
Christoph Hellwig | 19e420b | 2017-01-19 16:55:57 +0100 | [diff] [blame] | 1973 | WARN_ON(op->nents > blk_rq_nr_phys_segments(rq)); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 1974 | dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE; |
| 1975 | freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl, |
| 1976 | op->nents, dir); |
| 1977 | if (unlikely(freq->sg_cnt <= 0)) { |
| 1978 | sg_free_table_chained(&freq->sg_table, true); |
| 1979 | freq->sg_cnt = 0; |
| 1980 | return -EFAULT; |
| 1981 | } |
| 1982 | |
| 1983 | /* |
| 1984 | * TODO: blk_integrity_rq(rq) for DIF |
| 1985 | */ |
| 1986 | return 0; |
| 1987 | } |
| 1988 | |
| 1989 | static void |
| 1990 | nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq, |
| 1991 | struct nvme_fc_fcp_op *op) |
| 1992 | { |
| 1993 | struct nvmefc_fcp_req *freq = &op->fcp_req; |
| 1994 | |
| 1995 | if (!freq->sg_cnt) |
| 1996 | return; |
| 1997 | |
| 1998 | fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents, |
| 1999 | ((rq_data_dir(rq) == WRITE) ? |
| 2000 | DMA_TO_DEVICE : DMA_FROM_DEVICE)); |
| 2001 | |
| 2002 | nvme_cleanup_cmd(rq); |
| 2003 | |
| 2004 | sg_free_table_chained(&freq->sg_table, true); |
| 2005 | |
| 2006 | freq->sg_cnt = 0; |
| 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | * In FC, the queue is a logical thing. At transport connect, the target |
| 2011 | * creates its "queue" and returns a handle that is to be given to the |
| 2012 | * target whenever it posts something to the corresponding SQ. When an |
| 2013 | * SQE is sent on a SQ, FC effectively considers the SQE, or rather the |
| 2014 | * command contained within the SQE, an io, and assigns a FC exchange |
| 2015 | * to it. The SQE and the associated SQ handle are sent in the initial |
| 2016 | * CMD IU sents on the exchange. All transfers relative to the io occur |
| 2017 | * as part of the exchange. The CQE is the last thing for the io, |
| 2018 | * which is transferred (explicitly or implicitly) with the RSP IU |
| 2019 | * sent on the exchange. After the CQE is received, the FC exchange is |
| 2020 | * terminaed and the Exchange may be used on a different io. |
| 2021 | * |
| 2022 | * The transport to LLDD api has the transport making a request for a |
| 2023 | * new fcp io request to the LLDD. The LLDD then allocates a FC exchange |
| 2024 | * resource and transfers the command. The LLDD will then process all |
| 2025 | * steps to complete the io. Upon completion, the transport done routine |
| 2026 | * is called. |
| 2027 | * |
| 2028 | * So - while the operation is outstanding to the LLDD, there is a link |
| 2029 | * level FC exchange resource that is also outstanding. This must be |
| 2030 | * considered in all cleanup operations. |
| 2031 | */ |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2032 | static blk_status_t |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2033 | nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue, |
| 2034 | struct nvme_fc_fcp_op *op, u32 data_len, |
| 2035 | enum nvmefc_fcp_datadir io_dir) |
| 2036 | { |
| 2037 | struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; |
| 2038 | struct nvme_command *sqe = &cmdiu->sqe; |
| 2039 | u32 csn; |
| 2040 | int ret; |
| 2041 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2042 | /* |
| 2043 | * before attempting to send the io, check to see if we believe |
| 2044 | * the target device is present |
| 2045 | */ |
| 2046 | if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE) |
James Smart | 8b25f35 | 2017-07-18 14:29:34 -0700 | [diff] [blame] | 2047 | goto busy; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2048 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2049 | if (!nvme_fc_ctrl_get(ctrl)) |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2050 | return BLK_STS_IOERR; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2051 | |
| 2052 | /* format the FC-NVME CMD IU and fcp_req */ |
| 2053 | cmdiu->connection_id = cpu_to_be64(queue->connection_id); |
| 2054 | csn = atomic_inc_return(&queue->csn); |
| 2055 | cmdiu->csn = cpu_to_be32(csn); |
| 2056 | cmdiu->data_len = cpu_to_be32(data_len); |
| 2057 | switch (io_dir) { |
| 2058 | case NVMEFC_FCP_WRITE: |
| 2059 | cmdiu->flags = FCNVME_CMD_FLAGS_WRITE; |
| 2060 | break; |
| 2061 | case NVMEFC_FCP_READ: |
| 2062 | cmdiu->flags = FCNVME_CMD_FLAGS_READ; |
| 2063 | break; |
| 2064 | case NVMEFC_FCP_NODATA: |
| 2065 | cmdiu->flags = 0; |
| 2066 | break; |
| 2067 | } |
| 2068 | op->fcp_req.payload_length = data_len; |
| 2069 | op->fcp_req.io_dir = io_dir; |
| 2070 | op->fcp_req.transferred_length = 0; |
| 2071 | op->fcp_req.rcv_rsplen = 0; |
James Smart | 62eeacb | 2017-03-23 20:41:27 -0700 | [diff] [blame] | 2072 | op->fcp_req.status = NVME_SC_SUCCESS; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2073 | op->fcp_req.sqid = cpu_to_le16(queue->qnum); |
| 2074 | |
| 2075 | /* |
| 2076 | * validate per fabric rules, set fields mandated by fabric spec |
| 2077 | * as well as those by FC-NVME spec. |
| 2078 | */ |
| 2079 | WARN_ON_ONCE(sqe->common.metadata); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2080 | sqe->common.flags |= NVME_CMD_SGL_METABUF; |
| 2081 | |
| 2082 | /* |
James Smart | d9d34c0 | 2017-09-07 13:20:24 -0700 | [diff] [blame] | 2083 | * format SQE DPTR field per FC-NVME rules: |
| 2084 | * type=0x5 Transport SGL Data Block Descriptor |
| 2085 | * subtype=0xA Transport-specific value |
| 2086 | * address=0 |
| 2087 | * length=length of the data series |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2088 | */ |
James Smart | d9d34c0 | 2017-09-07 13:20:24 -0700 | [diff] [blame] | 2089 | sqe->rw.dptr.sgl.type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) | |
| 2090 | NVME_SGL_FMT_TRANSPORT_A; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2091 | sqe->rw.dptr.sgl.length = cpu_to_le32(data_len); |
| 2092 | sqe->rw.dptr.sgl.addr = 0; |
| 2093 | |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 2094 | if (!(op->flags & FCOP_FLAGS_AEN)) { |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2095 | ret = nvme_fc_map_data(ctrl, op->rq, op); |
| 2096 | if (ret < 0) { |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2097 | nvme_cleanup_cmd(op->rq); |
| 2098 | nvme_fc_ctrl_put(ctrl); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2099 | if (ret == -ENOMEM || ret == -EAGAIN) |
| 2100 | return BLK_STS_RESOURCE; |
| 2101 | return BLK_STS_IOERR; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma, |
| 2106 | sizeof(op->cmd_iu), DMA_TO_DEVICE); |
| 2107 | |
| 2108 | atomic_set(&op->state, FCPOP_STATE_ACTIVE); |
| 2109 | |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 2110 | if (!(op->flags & FCOP_FLAGS_AEN)) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2111 | blk_mq_start_request(op->rq); |
| 2112 | |
| 2113 | ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport, |
| 2114 | &ctrl->rport->remoteport, |
| 2115 | queue->lldd_handle, &op->fcp_req); |
| 2116 | |
| 2117 | if (ret) { |
James Smart | 8b25f35 | 2017-07-18 14:29:34 -0700 | [diff] [blame] | 2118 | if (!(op->flags & FCOP_FLAGS_AEN)) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2119 | nvme_fc_unmap_data(ctrl, op->rq, op); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2120 | |
| 2121 | nvme_fc_ctrl_put(ctrl); |
| 2122 | |
James Smart | 8b25f35 | 2017-07-18 14:29:34 -0700 | [diff] [blame] | 2123 | if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE && |
| 2124 | ret != -EBUSY) |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2125 | return BLK_STS_IOERR; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2126 | |
James Smart | 8b25f35 | 2017-07-18 14:29:34 -0700 | [diff] [blame] | 2127 | goto busy; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2128 | } |
| 2129 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2130 | return BLK_STS_OK; |
James Smart | 8b25f35 | 2017-07-18 14:29:34 -0700 | [diff] [blame] | 2131 | |
| 2132 | busy: |
| 2133 | if (!(op->flags & FCOP_FLAGS_AEN) && queue->hctx) |
| 2134 | blk_mq_delay_run_hw_queue(queue->hctx, NVMEFC_QUEUE_DELAY); |
| 2135 | |
| 2136 | return BLK_STS_RESOURCE; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2137 | } |
| 2138 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2139 | static blk_status_t |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2140 | nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 2141 | const struct blk_mq_queue_data *bd) |
| 2142 | { |
| 2143 | struct nvme_ns *ns = hctx->queue->queuedata; |
| 2144 | struct nvme_fc_queue *queue = hctx->driver_data; |
| 2145 | struct nvme_fc_ctrl *ctrl = queue->ctrl; |
| 2146 | struct request *rq = bd->rq; |
| 2147 | struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); |
| 2148 | struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu; |
| 2149 | struct nvme_command *sqe = &cmdiu->sqe; |
| 2150 | enum nvmefc_fcp_datadir io_dir; |
| 2151 | u32 data_len; |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2152 | blk_status_t ret; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2153 | |
| 2154 | ret = nvme_setup_cmd(ns, rq, sqe); |
| 2155 | if (ret) |
| 2156 | return ret; |
| 2157 | |
Christoph Hellwig | b131c61 | 2017-01-13 12:29:12 +0100 | [diff] [blame] | 2158 | data_len = blk_rq_payload_bytes(rq); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2159 | if (data_len) |
| 2160 | io_dir = ((rq_data_dir(rq) == WRITE) ? |
| 2161 | NVMEFC_FCP_WRITE : NVMEFC_FCP_READ); |
| 2162 | else |
| 2163 | io_dir = NVMEFC_FCP_NODATA; |
| 2164 | |
| 2165 | return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir); |
| 2166 | } |
| 2167 | |
| 2168 | static struct blk_mq_tags * |
| 2169 | nvme_fc_tagset(struct nvme_fc_queue *queue) |
| 2170 | { |
| 2171 | if (queue->qnum == 0) |
| 2172 | return queue->ctrl->admin_tag_set.tags[queue->qnum]; |
| 2173 | |
| 2174 | return queue->ctrl->tag_set.tags[queue->qnum - 1]; |
| 2175 | } |
| 2176 | |
| 2177 | static int |
| 2178 | nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag) |
| 2179 | |
| 2180 | { |
| 2181 | struct nvme_fc_queue *queue = hctx->driver_data; |
| 2182 | struct nvme_fc_ctrl *ctrl = queue->ctrl; |
| 2183 | struct request *req; |
| 2184 | struct nvme_fc_fcp_op *op; |
| 2185 | |
| 2186 | req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2187 | if (!req) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2188 | return 0; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2189 | |
| 2190 | op = blk_mq_rq_to_pdu(req); |
| 2191 | |
| 2192 | if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) && |
| 2193 | (ctrl->lport->ops->poll_queue)) |
| 2194 | ctrl->lport->ops->poll_queue(&ctrl->lport->localport, |
| 2195 | queue->lldd_handle); |
| 2196 | |
| 2197 | return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE)); |
| 2198 | } |
| 2199 | |
| 2200 | static void |
| 2201 | nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx) |
| 2202 | { |
| 2203 | struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg); |
| 2204 | struct nvme_fc_fcp_op *aen_op; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2205 | unsigned long flags; |
| 2206 | bool terminating = false; |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 2207 | blk_status_t ret; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2208 | |
| 2209 | if (aer_idx > NVME_FC_NR_AEN_COMMANDS) |
| 2210 | return; |
| 2211 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2212 | spin_lock_irqsave(&ctrl->lock, flags); |
| 2213 | if (ctrl->flags & FCCTRL_TERMIO) |
| 2214 | terminating = true; |
| 2215 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 2216 | |
| 2217 | if (terminating) |
| 2218 | return; |
| 2219 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2220 | aen_op = &ctrl->aen_ops[aer_idx]; |
| 2221 | |
| 2222 | ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0, |
| 2223 | NVMEFC_FCP_NODATA); |
| 2224 | if (ret) |
| 2225 | dev_err(ctrl->ctrl.device, |
| 2226 | "failed async event work [%d]\n", aer_idx); |
| 2227 | } |
| 2228 | |
| 2229 | static void |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 2230 | __nvme_fc_final_op_cleanup(struct request *rq) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2231 | { |
| 2232 | struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); |
| 2233 | struct nvme_fc_ctrl *ctrl = op->ctrl; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2234 | |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 2235 | atomic_set(&op->state, FCPOP_STATE_IDLE); |
| 2236 | op->flags &= ~(FCOP_FLAGS_TERMIO | FCOP_FLAGS_RELEASED | |
| 2237 | FCOP_FLAGS_COMPLETE); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2238 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2239 | nvme_fc_unmap_data(ctrl, rq, op); |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 2240 | nvme_complete_rq(rq); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2241 | nvme_fc_ctrl_put(ctrl); |
| 2242 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2243 | } |
| 2244 | |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 2245 | static void |
| 2246 | nvme_fc_complete_rq(struct request *rq) |
| 2247 | { |
| 2248 | struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq); |
| 2249 | struct nvme_fc_ctrl *ctrl = op->ctrl; |
| 2250 | unsigned long flags; |
| 2251 | bool completed = false; |
| 2252 | |
| 2253 | /* |
| 2254 | * the core layer, on controller resets after calling |
| 2255 | * nvme_shutdown_ctrl(), calls complete_rq without our |
| 2256 | * calling blk_mq_complete_request(), thus there may still |
| 2257 | * be live i/o outstanding with the LLDD. Means transport has |
| 2258 | * to track complete calls vs fcpio_done calls to know what |
| 2259 | * path to take on completes and dones. |
| 2260 | */ |
| 2261 | spin_lock_irqsave(&ctrl->lock, flags); |
| 2262 | if (op->flags & FCOP_FLAGS_COMPLETE) |
| 2263 | completed = true; |
| 2264 | else |
| 2265 | op->flags |= FCOP_FLAGS_RELEASED; |
| 2266 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 2267 | |
| 2268 | if (completed) |
| 2269 | __nvme_fc_final_op_cleanup(rq); |
| 2270 | } |
| 2271 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2272 | /* |
| 2273 | * This routine is used by the transport when it needs to find active |
| 2274 | * io on a queue that is to be terminated. The transport uses |
| 2275 | * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke |
| 2276 | * this routine to kill them on a 1 by 1 basis. |
| 2277 | * |
| 2278 | * As FC allocates FC exchange for each io, the transport must contact |
| 2279 | * the LLDD to terminate the exchange, thus releasing the FC exchange. |
| 2280 | * After terminating the exchange the LLDD will call the transport's |
| 2281 | * normal io done path for the request, but it will have an aborted |
| 2282 | * status. The done path will return the io request back to the block |
| 2283 | * layer with an error status. |
| 2284 | */ |
| 2285 | static void |
| 2286 | nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved) |
| 2287 | { |
| 2288 | struct nvme_ctrl *nctrl = data; |
| 2289 | struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); |
| 2290 | struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req); |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 2291 | unsigned long flags; |
| 2292 | int status; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2293 | |
| 2294 | if (!blk_mq_request_started(req)) |
| 2295 | return; |
| 2296 | |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 2297 | spin_lock_irqsave(&ctrl->lock, flags); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2298 | if (ctrl->flags & FCCTRL_TERMIO) { |
| 2299 | ctrl->iocnt++; |
| 2300 | op->flags |= FCOP_FLAGS_TERMIO; |
| 2301 | } |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 2302 | spin_unlock_irqrestore(&ctrl->lock, flags); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2303 | |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 2304 | status = __nvme_fc_abort_op(ctrl, op); |
| 2305 | if (status) { |
| 2306 | /* |
| 2307 | * if __nvme_fc_abort_op failed the io wasn't |
| 2308 | * active. Thus this call path is running in |
| 2309 | * parallel to the io complete. Treat as non-error. |
| 2310 | */ |
| 2311 | |
| 2312 | /* back out the flags/counters */ |
| 2313 | spin_lock_irqsave(&ctrl->lock, flags); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2314 | if (ctrl->flags & FCCTRL_TERMIO) |
| 2315 | ctrl->iocnt--; |
James Smart | 78a7ac2 | 2017-04-23 08:30:07 -0700 | [diff] [blame] | 2316 | op->flags &= ~FCOP_FLAGS_TERMIO; |
| 2317 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 2318 | return; |
| 2319 | } |
| 2320 | } |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2321 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2322 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2323 | static const struct blk_mq_ops nvme_fc_mq_ops = { |
| 2324 | .queue_rq = nvme_fc_queue_rq, |
| 2325 | .complete = nvme_fc_complete_rq, |
| 2326 | .init_request = nvme_fc_init_request, |
| 2327 | .exit_request = nvme_fc_exit_request, |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2328 | .init_hctx = nvme_fc_init_hctx, |
| 2329 | .poll = nvme_fc_poll, |
| 2330 | .timeout = nvme_fc_timeout, |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2331 | }; |
| 2332 | |
| 2333 | static int |
| 2334 | nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl) |
| 2335 | { |
| 2336 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; |
Sagi Grimberg | 7314183 | 2017-06-29 11:16:49 +0300 | [diff] [blame] | 2337 | unsigned int nr_io_queues; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2338 | int ret; |
| 2339 | |
Sagi Grimberg | 7314183 | 2017-06-29 11:16:49 +0300 | [diff] [blame] | 2340 | nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()), |
| 2341 | ctrl->lport->ops->max_hw_queues); |
| 2342 | ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2343 | if (ret) { |
| 2344 | dev_info(ctrl->ctrl.device, |
| 2345 | "set_queue_count failed: %d\n", ret); |
| 2346 | return ret; |
| 2347 | } |
| 2348 | |
Sagi Grimberg | 7314183 | 2017-06-29 11:16:49 +0300 | [diff] [blame] | 2349 | ctrl->ctrl.queue_count = nr_io_queues + 1; |
| 2350 | if (!nr_io_queues) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2351 | return 0; |
| 2352 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2353 | nvme_fc_init_io_queues(ctrl); |
| 2354 | |
| 2355 | memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); |
| 2356 | ctrl->tag_set.ops = &nvme_fc_mq_ops; |
| 2357 | ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; |
| 2358 | ctrl->tag_set.reserved_tags = 1; /* fabric connect */ |
| 2359 | ctrl->tag_set.numa_node = NUMA_NO_NODE; |
| 2360 | ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
| 2361 | ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) + |
| 2362 | (SG_CHUNK_SIZE * |
| 2363 | sizeof(struct scatterlist)) + |
| 2364 | ctrl->lport->ops->fcprqst_priv_sz; |
| 2365 | ctrl->tag_set.driver_data = ctrl; |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 2366 | ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2367 | ctrl->tag_set.timeout = NVME_IO_TIMEOUT; |
| 2368 | |
| 2369 | ret = blk_mq_alloc_tag_set(&ctrl->tag_set); |
| 2370 | if (ret) |
| 2371 | return ret; |
| 2372 | |
| 2373 | ctrl->ctrl.tagset = &ctrl->tag_set; |
| 2374 | |
| 2375 | ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); |
| 2376 | if (IS_ERR(ctrl->ctrl.connect_q)) { |
| 2377 | ret = PTR_ERR(ctrl->ctrl.connect_q); |
| 2378 | goto out_free_tag_set; |
| 2379 | } |
| 2380 | |
| 2381 | ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size); |
| 2382 | if (ret) |
| 2383 | goto out_cleanup_blk_queue; |
| 2384 | |
| 2385 | ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size); |
| 2386 | if (ret) |
| 2387 | goto out_delete_hw_queues; |
| 2388 | |
| 2389 | return 0; |
| 2390 | |
| 2391 | out_delete_hw_queues: |
| 2392 | nvme_fc_delete_hw_io_queues(ctrl); |
| 2393 | out_cleanup_blk_queue: |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2394 | blk_cleanup_queue(ctrl->ctrl.connect_q); |
| 2395 | out_free_tag_set: |
| 2396 | blk_mq_free_tag_set(&ctrl->tag_set); |
| 2397 | nvme_fc_free_io_queues(ctrl); |
| 2398 | |
| 2399 | /* force put free routine to ignore io queues */ |
| 2400 | ctrl->ctrl.tagset = NULL; |
| 2401 | |
| 2402 | return ret; |
| 2403 | } |
| 2404 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2405 | static int |
| 2406 | nvme_fc_reinit_io_queues(struct nvme_fc_ctrl *ctrl) |
| 2407 | { |
| 2408 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; |
Sagi Grimberg | 7314183 | 2017-06-29 11:16:49 +0300 | [diff] [blame] | 2409 | unsigned int nr_io_queues; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2410 | int ret; |
| 2411 | |
Sagi Grimberg | 7314183 | 2017-06-29 11:16:49 +0300 | [diff] [blame] | 2412 | nr_io_queues = min(min(opts->nr_io_queues, num_online_cpus()), |
| 2413 | ctrl->lport->ops->max_hw_queues); |
| 2414 | ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2415 | if (ret) { |
| 2416 | dev_info(ctrl->ctrl.device, |
| 2417 | "set_queue_count failed: %d\n", ret); |
| 2418 | return ret; |
| 2419 | } |
| 2420 | |
Sagi Grimberg | 7314183 | 2017-06-29 11:16:49 +0300 | [diff] [blame] | 2421 | ctrl->ctrl.queue_count = nr_io_queues + 1; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2422 | /* check for io queues existing */ |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 2423 | if (ctrl->ctrl.queue_count == 1) |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2424 | return 0; |
| 2425 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2426 | nvme_fc_init_io_queues(ctrl); |
| 2427 | |
Sagi Grimberg | 31b8446 | 2017-10-11 12:53:07 +0300 | [diff] [blame] | 2428 | ret = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.tagset); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2429 | if (ret) |
| 2430 | goto out_free_io_queues; |
| 2431 | |
| 2432 | ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size); |
| 2433 | if (ret) |
| 2434 | goto out_free_io_queues; |
| 2435 | |
| 2436 | ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size); |
| 2437 | if (ret) |
| 2438 | goto out_delete_hw_queues; |
| 2439 | |
Sagi Grimberg | cda5fd1 | 2017-06-29 11:20:10 +0300 | [diff] [blame] | 2440 | blk_mq_update_nr_hw_queues(&ctrl->tag_set, nr_io_queues); |
| 2441 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2442 | return 0; |
| 2443 | |
| 2444 | out_delete_hw_queues: |
| 2445 | nvme_fc_delete_hw_io_queues(ctrl); |
| 2446 | out_free_io_queues: |
| 2447 | nvme_fc_free_io_queues(ctrl); |
| 2448 | return ret; |
| 2449 | } |
| 2450 | |
| 2451 | /* |
| 2452 | * This routine restarts the controller on the host side, and |
| 2453 | * on the link side, recreates the controller association. |
| 2454 | */ |
| 2455 | static int |
| 2456 | nvme_fc_create_association(struct nvme_fc_ctrl *ctrl) |
| 2457 | { |
| 2458 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2459 | int ret; |
| 2460 | bool changed; |
| 2461 | |
Sagi Grimberg | fdf9dfa | 2017-05-04 13:33:15 +0300 | [diff] [blame] | 2462 | ++ctrl->ctrl.nr_reconnects; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2463 | |
James Smart | 96e2480 | 2017-10-25 16:43:16 -0700 | [diff] [blame^] | 2464 | if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE) |
| 2465 | return -ENODEV; |
| 2466 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2467 | /* |
| 2468 | * Create the admin queue |
| 2469 | */ |
| 2470 | |
| 2471 | nvme_fc_init_queue(ctrl, 0, NVME_FC_AQ_BLKMQ_DEPTH); |
| 2472 | |
| 2473 | ret = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0, |
| 2474 | NVME_FC_AQ_BLKMQ_DEPTH); |
| 2475 | if (ret) |
| 2476 | goto out_free_queue; |
| 2477 | |
| 2478 | ret = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0], |
| 2479 | NVME_FC_AQ_BLKMQ_DEPTH, |
| 2480 | (NVME_FC_AQ_BLKMQ_DEPTH / 4)); |
| 2481 | if (ret) |
| 2482 | goto out_delete_hw_queue; |
| 2483 | |
| 2484 | if (ctrl->ctrl.state != NVME_CTRL_NEW) |
Sagi Grimberg | f9c5af5 | 2017-07-02 15:39:34 +0300 | [diff] [blame] | 2485 | blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2486 | |
| 2487 | ret = nvmf_connect_admin_queue(&ctrl->ctrl); |
| 2488 | if (ret) |
| 2489 | goto out_disconnect_admin_queue; |
| 2490 | |
| 2491 | /* |
| 2492 | * Check controller capabilities |
| 2493 | * |
| 2494 | * todo:- add code to check if ctrl attributes changed from |
| 2495 | * prior connection values |
| 2496 | */ |
| 2497 | |
Sagi Grimberg | 20d0dfe | 2017-06-27 22:16:38 +0300 | [diff] [blame] | 2498 | ret = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2499 | if (ret) { |
| 2500 | dev_err(ctrl->ctrl.device, |
| 2501 | "prop_get NVME_REG_CAP failed\n"); |
| 2502 | goto out_disconnect_admin_queue; |
| 2503 | } |
| 2504 | |
| 2505 | ctrl->ctrl.sqsize = |
Sagi Grimberg | 20d0dfe | 2017-06-27 22:16:38 +0300 | [diff] [blame] | 2506 | min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap) + 1, ctrl->ctrl.sqsize); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2507 | |
Sagi Grimberg | 20d0dfe | 2017-06-27 22:16:38 +0300 | [diff] [blame] | 2508 | ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2509 | if (ret) |
| 2510 | goto out_disconnect_admin_queue; |
| 2511 | |
James Smart | ecad0d2 | 2017-10-23 15:11:36 -0700 | [diff] [blame] | 2512 | ctrl->ctrl.max_hw_sectors = |
| 2513 | (ctrl->lport->ops->max_sgl_segments - 1) << (PAGE_SHIFT - 9); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2514 | |
| 2515 | ret = nvme_init_identify(&ctrl->ctrl); |
| 2516 | if (ret) |
| 2517 | goto out_disconnect_admin_queue; |
| 2518 | |
| 2519 | /* sanity checks */ |
| 2520 | |
| 2521 | /* FC-NVME does not have other data in the capsule */ |
| 2522 | if (ctrl->ctrl.icdoff) { |
| 2523 | dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n", |
| 2524 | ctrl->ctrl.icdoff); |
| 2525 | goto out_disconnect_admin_queue; |
| 2526 | } |
| 2527 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2528 | /* FC-NVME supports normal SGL Data Block Descriptors */ |
| 2529 | |
| 2530 | if (opts->queue_size > ctrl->ctrl.maxcmd) { |
| 2531 | /* warn if maxcmd is lower than queue_size */ |
| 2532 | dev_warn(ctrl->ctrl.device, |
| 2533 | "queue_size %zu > ctrl maxcmd %u, reducing " |
| 2534 | "to queue_size\n", |
| 2535 | opts->queue_size, ctrl->ctrl.maxcmd); |
| 2536 | opts->queue_size = ctrl->ctrl.maxcmd; |
| 2537 | } |
| 2538 | |
| 2539 | ret = nvme_fc_init_aen_ops(ctrl); |
| 2540 | if (ret) |
| 2541 | goto out_term_aen_ops; |
| 2542 | |
| 2543 | /* |
| 2544 | * Create the io queues |
| 2545 | */ |
| 2546 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 2547 | if (ctrl->ctrl.queue_count > 1) { |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2548 | if (ctrl->ctrl.state == NVME_CTRL_NEW) |
| 2549 | ret = nvme_fc_create_io_queues(ctrl); |
| 2550 | else |
| 2551 | ret = nvme_fc_reinit_io_queues(ctrl); |
| 2552 | if (ret) |
| 2553 | goto out_term_aen_ops; |
| 2554 | } |
| 2555 | |
| 2556 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2557 | |
Sagi Grimberg | fdf9dfa | 2017-05-04 13:33:15 +0300 | [diff] [blame] | 2558 | ctrl->ctrl.nr_reconnects = 0; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2559 | |
James Smart | 44c6ec7 | 2017-10-25 16:43:14 -0700 | [diff] [blame] | 2560 | if (changed) |
| 2561 | nvme_start_ctrl(&ctrl->ctrl); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2562 | |
| 2563 | return 0; /* Success */ |
| 2564 | |
| 2565 | out_term_aen_ops: |
| 2566 | nvme_fc_term_aen_ops(ctrl); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2567 | out_disconnect_admin_queue: |
| 2568 | /* send a Disconnect(association) LS to fc-nvme target */ |
| 2569 | nvme_fc_xmt_disconnect_assoc(ctrl); |
| 2570 | out_delete_hw_queue: |
| 2571 | __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0); |
| 2572 | out_free_queue: |
| 2573 | nvme_fc_free_queue(&ctrl->queues[0]); |
| 2574 | |
| 2575 | return ret; |
| 2576 | } |
| 2577 | |
| 2578 | /* |
| 2579 | * This routine stops operation of the controller on the host side. |
| 2580 | * On the host os stack side: Admin and IO queues are stopped, |
| 2581 | * outstanding ios on them terminated via FC ABTS. |
| 2582 | * On the link side: the association is terminated. |
| 2583 | */ |
| 2584 | static void |
| 2585 | nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl) |
| 2586 | { |
| 2587 | unsigned long flags; |
| 2588 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2589 | spin_lock_irqsave(&ctrl->lock, flags); |
| 2590 | ctrl->flags |= FCCTRL_TERMIO; |
| 2591 | ctrl->iocnt = 0; |
| 2592 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 2593 | |
| 2594 | /* |
| 2595 | * If io queues are present, stop them and terminate all outstanding |
| 2596 | * ios on them. As FC allocates FC exchange for each io, the |
| 2597 | * transport must contact the LLDD to terminate the exchange, |
| 2598 | * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr() |
| 2599 | * to tell us what io's are busy and invoke a transport routine |
| 2600 | * to kill them with the LLDD. After terminating the exchange |
| 2601 | * the LLDD will call the transport's normal io done path, but it |
| 2602 | * will have an aborted status. The done path will return the |
| 2603 | * io requests back to the block layer as part of normal completions |
| 2604 | * (but with error status). |
| 2605 | */ |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 2606 | if (ctrl->ctrl.queue_count > 1) { |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2607 | nvme_stop_queues(&ctrl->ctrl); |
| 2608 | blk_mq_tagset_busy_iter(&ctrl->tag_set, |
| 2609 | nvme_fc_terminate_exchange, &ctrl->ctrl); |
| 2610 | } |
| 2611 | |
| 2612 | /* |
| 2613 | * Other transports, which don't have link-level contexts bound |
| 2614 | * to sqe's, would try to gracefully shutdown the controller by |
| 2615 | * writing the registers for shutdown and polling (call |
| 2616 | * nvme_shutdown_ctrl()). Given a bunch of i/o was potentially |
| 2617 | * just aborted and we will wait on those contexts, and given |
| 2618 | * there was no indication of how live the controlelr is on the |
| 2619 | * link, don't send more io to create more contexts for the |
| 2620 | * shutdown. Let the controller fail via keepalive failure if |
| 2621 | * its still present. |
| 2622 | */ |
| 2623 | |
| 2624 | /* |
| 2625 | * clean up the admin queue. Same thing as above. |
| 2626 | * use blk_mq_tagset_busy_itr() and the transport routine to |
| 2627 | * terminate the exchanges. |
| 2628 | */ |
James Smart | 44c6ec7 | 2017-10-25 16:43:14 -0700 | [diff] [blame] | 2629 | if (ctrl->ctrl.state != NVME_CTRL_NEW) |
| 2630 | blk_mq_quiesce_queue(ctrl->ctrl.admin_q); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2631 | blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, |
| 2632 | nvme_fc_terminate_exchange, &ctrl->ctrl); |
| 2633 | |
| 2634 | /* kill the aens as they are a separate path */ |
| 2635 | nvme_fc_abort_aen_ops(ctrl); |
| 2636 | |
| 2637 | /* wait for all io that had to be aborted */ |
| 2638 | spin_lock_irqsave(&ctrl->lock, flags); |
James Smart | 36715cf | 2017-05-22 15:28:42 -0700 | [diff] [blame] | 2639 | wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2640 | ctrl->flags &= ~FCCTRL_TERMIO; |
| 2641 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 2642 | |
| 2643 | nvme_fc_term_aen_ops(ctrl); |
| 2644 | |
| 2645 | /* |
| 2646 | * send a Disconnect(association) LS to fc-nvme target |
| 2647 | * Note: could have been sent at top of process, but |
| 2648 | * cleaner on link traffic if after the aborts complete. |
| 2649 | * Note: if association doesn't exist, association_id will be 0 |
| 2650 | */ |
| 2651 | if (ctrl->association_id) |
| 2652 | nvme_fc_xmt_disconnect_assoc(ctrl); |
| 2653 | |
| 2654 | if (ctrl->ctrl.tagset) { |
| 2655 | nvme_fc_delete_hw_io_queues(ctrl); |
| 2656 | nvme_fc_free_io_queues(ctrl); |
| 2657 | } |
| 2658 | |
| 2659 | __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0); |
| 2660 | nvme_fc_free_queue(&ctrl->queues[0]); |
| 2661 | } |
| 2662 | |
| 2663 | static void |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 2664 | nvme_fc_delete_ctrl(struct nvme_ctrl *nctrl) |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2665 | { |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 2666 | struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2667 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2668 | cancel_delayed_work_sync(&ctrl->connect_work); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2669 | /* |
| 2670 | * kill the association on the link side. this will block |
| 2671 | * waiting for io to terminate |
| 2672 | */ |
| 2673 | nvme_fc_delete_association(ctrl); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2674 | } |
| 2675 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2676 | static void |
James Smart | 5bbecdb | 2017-05-15 17:10:16 -0700 | [diff] [blame] | 2677 | nvme_fc_reconnect_or_delete(struct nvme_fc_ctrl *ctrl, int status) |
| 2678 | { |
James Smart | 44c6ec7 | 2017-10-25 16:43:14 -0700 | [diff] [blame] | 2679 | if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) |
James Smart | 5bbecdb | 2017-05-15 17:10:16 -0700 | [diff] [blame] | 2680 | return; |
James Smart | 5bbecdb | 2017-05-15 17:10:16 -0700 | [diff] [blame] | 2681 | |
James Smart | 589ff77 | 2017-05-15 17:10:19 -0700 | [diff] [blame] | 2682 | dev_info(ctrl->ctrl.device, |
James Smart | 5bbecdb | 2017-05-15 17:10:16 -0700 | [diff] [blame] | 2683 | "NVME-FC{%d}: reset: Reconnect attempt failed (%d)\n", |
| 2684 | ctrl->cnum, status); |
| 2685 | |
| 2686 | if (nvmf_should_reconnect(&ctrl->ctrl)) { |
James Smart | 96e2480 | 2017-10-25 16:43:16 -0700 | [diff] [blame^] | 2687 | /* Only schedule the reconnect if the remote port is online */ |
| 2688 | if (ctrl->rport->remoteport.port_state != FC_OBJSTATE_ONLINE) |
| 2689 | return; |
| 2690 | |
James Smart | 5bbecdb | 2017-05-15 17:10:16 -0700 | [diff] [blame] | 2691 | dev_info(ctrl->ctrl.device, |
| 2692 | "NVME-FC{%d}: Reconnect attempt in %d seconds.\n", |
| 2693 | ctrl->cnum, ctrl->ctrl.opts->reconnect_delay); |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 2694 | queue_delayed_work(nvme_wq, &ctrl->connect_work, |
James Smart | 5bbecdb | 2017-05-15 17:10:16 -0700 | [diff] [blame] | 2695 | ctrl->ctrl.opts->reconnect_delay * HZ); |
| 2696 | } else { |
James Smart | 589ff77 | 2017-05-15 17:10:19 -0700 | [diff] [blame] | 2697 | dev_warn(ctrl->ctrl.device, |
James Smart | 5bbecdb | 2017-05-15 17:10:16 -0700 | [diff] [blame] | 2698 | "NVME-FC{%d}: Max reconnect attempts (%d) " |
| 2699 | "reached. Removing controller\n", |
Sagi Grimberg | fdf9dfa | 2017-05-04 13:33:15 +0300 | [diff] [blame] | 2700 | ctrl->cnum, ctrl->ctrl.nr_reconnects); |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 2701 | WARN_ON(nvme_delete_ctrl(&ctrl->ctrl)); |
James Smart | 5bbecdb | 2017-05-15 17:10:16 -0700 | [diff] [blame] | 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | static void |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2706 | nvme_fc_reset_ctrl_work(struct work_struct *work) |
| 2707 | { |
| 2708 | struct nvme_fc_ctrl *ctrl = |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 2709 | container_of(work, struct nvme_fc_ctrl, ctrl.reset_work); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2710 | int ret; |
| 2711 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 2712 | nvme_stop_ctrl(&ctrl->ctrl); |
James Smart | 44c6ec7 | 2017-10-25 16:43:14 -0700 | [diff] [blame] | 2713 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2714 | /* will block will waiting for io to terminate */ |
| 2715 | nvme_fc_delete_association(ctrl); |
| 2716 | |
James Smart | 44c6ec7 | 2017-10-25 16:43:14 -0700 | [diff] [blame] | 2717 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) { |
| 2718 | dev_err(ctrl->ctrl.device, |
| 2719 | "NVME-FC{%d}: error_recovery: Couldn't change state " |
| 2720 | "to RECONNECTING\n", ctrl->cnum); |
| 2721 | return; |
| 2722 | } |
| 2723 | |
James Smart | 96e2480 | 2017-10-25 16:43:16 -0700 | [diff] [blame^] | 2724 | if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) { |
| 2725 | ret = nvme_fc_create_association(ctrl); |
| 2726 | if (ret) |
| 2727 | nvme_fc_reconnect_or_delete(ctrl, ret); |
| 2728 | else |
| 2729 | dev_info(ctrl->ctrl.device, |
| 2730 | "NVME-FC{%d}: controller reset complete\n", |
| 2731 | ctrl->cnum); |
| 2732 | } |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2733 | } |
| 2734 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2735 | static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = { |
| 2736 | .name = "fc", |
| 2737 | .module = THIS_MODULE, |
Christoph Hellwig | d3d5b87 | 2017-05-20 15:14:44 +0200 | [diff] [blame] | 2738 | .flags = NVME_F_FABRICS, |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2739 | .reg_read32 = nvmf_reg_read32, |
| 2740 | .reg_read64 = nvmf_reg_read64, |
| 2741 | .reg_write32 = nvmf_reg_write32, |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2742 | .free_ctrl = nvme_fc_nvme_ctrl_freed, |
| 2743 | .submit_async_event = nvme_fc_submit_async_event, |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 2744 | .delete_ctrl = nvme_fc_delete_ctrl, |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2745 | .get_address = nvmf_get_address, |
Sagi Grimberg | 31b8446 | 2017-10-11 12:53:07 +0300 | [diff] [blame] | 2746 | .reinit_request = nvme_fc_reinit_request, |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2747 | }; |
| 2748 | |
| 2749 | static void |
| 2750 | nvme_fc_connect_ctrl_work(struct work_struct *work) |
| 2751 | { |
| 2752 | int ret; |
| 2753 | |
| 2754 | struct nvme_fc_ctrl *ctrl = |
| 2755 | container_of(to_delayed_work(work), |
| 2756 | struct nvme_fc_ctrl, connect_work); |
| 2757 | |
| 2758 | ret = nvme_fc_create_association(ctrl); |
James Smart | 5bbecdb | 2017-05-15 17:10:16 -0700 | [diff] [blame] | 2759 | if (ret) |
| 2760 | nvme_fc_reconnect_or_delete(ctrl, ret); |
| 2761 | else |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2762 | dev_info(ctrl->ctrl.device, |
| 2763 | "NVME-FC{%d}: controller reconnect complete\n", |
| 2764 | ctrl->cnum); |
| 2765 | } |
| 2766 | |
| 2767 | |
| 2768 | static const struct blk_mq_ops nvme_fc_admin_mq_ops = { |
| 2769 | .queue_rq = nvme_fc_queue_rq, |
| 2770 | .complete = nvme_fc_complete_rq, |
Christoph Hellwig | 76f983c | 2017-06-13 09:15:20 +0200 | [diff] [blame] | 2771 | .init_request = nvme_fc_init_request, |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2772 | .exit_request = nvme_fc_exit_request, |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2773 | .init_hctx = nvme_fc_init_admin_hctx, |
| 2774 | .timeout = nvme_fc_timeout, |
| 2775 | }; |
| 2776 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2777 | |
James Smart | 56d5f4f | 2017-10-20 16:17:08 -0700 | [diff] [blame] | 2778 | /* |
| 2779 | * Fails a controller request if it matches an existing controller |
| 2780 | * (association) with the same tuple: |
| 2781 | * <Host NQN, Host ID, local FC port, remote FC port, SUBSYS NQN> |
| 2782 | * |
| 2783 | * The ports don't need to be compared as they are intrinsically |
| 2784 | * already matched by the port pointers supplied. |
| 2785 | */ |
| 2786 | static bool |
| 2787 | nvme_fc_existing_controller(struct nvme_fc_rport *rport, |
| 2788 | struct nvmf_ctrl_options *opts) |
| 2789 | { |
| 2790 | struct nvme_fc_ctrl *ctrl; |
| 2791 | unsigned long flags; |
| 2792 | bool found = false; |
| 2793 | |
| 2794 | spin_lock_irqsave(&rport->lock, flags); |
| 2795 | list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list) { |
| 2796 | found = nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts); |
| 2797 | if (found) |
| 2798 | break; |
| 2799 | } |
| 2800 | spin_unlock_irqrestore(&rport->lock, flags); |
| 2801 | |
| 2802 | return found; |
| 2803 | } |
| 2804 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2805 | static struct nvme_ctrl * |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2806 | nvme_fc_init_ctrl(struct device *dev, struct nvmf_ctrl_options *opts, |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2807 | struct nvme_fc_lport *lport, struct nvme_fc_rport *rport) |
| 2808 | { |
| 2809 | struct nvme_fc_ctrl *ctrl; |
| 2810 | unsigned long flags; |
| 2811 | int ret, idx; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2812 | |
James Smart | 85e6a6a | 2017-05-05 16:13:15 -0700 | [diff] [blame] | 2813 | if (!(rport->remoteport.port_role & |
| 2814 | (FC_PORT_ROLE_NVME_DISCOVERY | FC_PORT_ROLE_NVME_TARGET))) { |
| 2815 | ret = -EBADR; |
| 2816 | goto out_fail; |
| 2817 | } |
| 2818 | |
James Smart | 56d5f4f | 2017-10-20 16:17:08 -0700 | [diff] [blame] | 2819 | if (!opts->duplicate_connect && |
| 2820 | nvme_fc_existing_controller(rport, opts)) { |
| 2821 | ret = -EALREADY; |
| 2822 | goto out_fail; |
| 2823 | } |
| 2824 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2825 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); |
| 2826 | if (!ctrl) { |
| 2827 | ret = -ENOMEM; |
| 2828 | goto out_fail; |
| 2829 | } |
| 2830 | |
| 2831 | idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL); |
| 2832 | if (idx < 0) { |
| 2833 | ret = -ENOSPC; |
| 2834 | goto out_free_ctrl; |
| 2835 | } |
| 2836 | |
| 2837 | ctrl->ctrl.opts = opts; |
| 2838 | INIT_LIST_HEAD(&ctrl->ctrl_list); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2839 | ctrl->lport = lport; |
| 2840 | ctrl->rport = rport; |
| 2841 | ctrl->dev = lport->dev; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2842 | ctrl->cnum = idx; |
| 2843 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2844 | get_device(ctrl->dev); |
| 2845 | kref_init(&ctrl->ref); |
| 2846 | |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 2847 | INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2848 | INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2849 | spin_lock_init(&ctrl->lock); |
| 2850 | |
| 2851 | /* io queue count */ |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 2852 | ctrl->ctrl.queue_count = min_t(unsigned int, |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2853 | opts->nr_io_queues, |
| 2854 | lport->ops->max_hw_queues); |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 2855 | ctrl->ctrl.queue_count++; /* +1 for admin queue */ |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2856 | |
| 2857 | ctrl->ctrl.sqsize = opts->queue_size - 1; |
| 2858 | ctrl->ctrl.kato = opts->kato; |
| 2859 | |
| 2860 | ret = -ENOMEM; |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 2861 | ctrl->queues = kcalloc(ctrl->ctrl.queue_count, |
| 2862 | sizeof(struct nvme_fc_queue), GFP_KERNEL); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2863 | if (!ctrl->queues) |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2864 | goto out_free_ida; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2865 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2866 | memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); |
| 2867 | ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops; |
| 2868 | ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH; |
| 2869 | ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */ |
| 2870 | ctrl->admin_tag_set.numa_node = NUMA_NO_NODE; |
| 2871 | ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) + |
| 2872 | (SG_CHUNK_SIZE * |
| 2873 | sizeof(struct scatterlist)) + |
| 2874 | ctrl->lport->ops->fcprqst_priv_sz; |
| 2875 | ctrl->admin_tag_set.driver_data = ctrl; |
| 2876 | ctrl->admin_tag_set.nr_hw_queues = 1; |
| 2877 | ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; |
Israel Rukshin | 5a22e2b | 2017-10-18 12:38:25 +0000 | [diff] [blame] | 2878 | ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2879 | |
| 2880 | ret = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2881 | if (ret) |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2882 | goto out_free_queues; |
Sagi Grimberg | 34b6c23 | 2017-07-10 09:22:29 +0300 | [diff] [blame] | 2883 | ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2884 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2885 | ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); |
| 2886 | if (IS_ERR(ctrl->ctrl.admin_q)) { |
| 2887 | ret = PTR_ERR(ctrl->ctrl.admin_q); |
| 2888 | goto out_free_admin_tag_set; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2889 | } |
| 2890 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2891 | /* |
| 2892 | * Would have been nice to init io queues tag set as well. |
| 2893 | * However, we require interaction from the controller |
| 2894 | * for max io queue count before we can do so. |
| 2895 | * Defer this to the connect path. |
| 2896 | */ |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2897 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2898 | ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2899 | if (ret) |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2900 | goto out_cleanup_admin_q; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2901 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2902 | /* at this point, teardown path changes to ref counting on nvme ctrl */ |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2903 | |
| 2904 | spin_lock_irqsave(&rport->lock, flags); |
| 2905 | list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list); |
| 2906 | spin_unlock_irqrestore(&rport->lock, flags); |
| 2907 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2908 | ret = nvme_fc_create_association(ctrl); |
| 2909 | if (ret) { |
Ewan D. Milne | de41447 | 2017-04-24 13:24:16 -0400 | [diff] [blame] | 2910 | ctrl->ctrl.opts = NULL; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2911 | /* initiate nvme ctrl ref counting teardown */ |
| 2912 | nvme_uninit_ctrl(&ctrl->ctrl); |
James Smart | 24b7f05 | 2017-06-05 15:03:42 -0700 | [diff] [blame] | 2913 | nvme_put_ctrl(&ctrl->ctrl); |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2914 | |
James Smart | 0b5a766 | 2017-06-15 23:40:54 -0700 | [diff] [blame] | 2915 | /* Remove core ctrl ref. */ |
| 2916 | nvme_put_ctrl(&ctrl->ctrl); |
| 2917 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2918 | /* as we're past the point where we transition to the ref |
| 2919 | * counting teardown path, if we return a bad pointer here, |
| 2920 | * the calling routine, thinking it's prior to the |
| 2921 | * transition, will do an rport put. Since the teardown |
| 2922 | * path also does a rport put, we do an extra get here to |
| 2923 | * so proper order/teardown happens. |
| 2924 | */ |
| 2925 | nvme_fc_rport_get(rport); |
| 2926 | |
| 2927 | if (ret > 0) |
| 2928 | ret = -EIO; |
| 2929 | return ERR_PTR(ret); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2930 | } |
| 2931 | |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 2932 | nvme_get_ctrl(&ctrl->ctrl); |
James Smart | 2cb657b | 2017-05-15 17:10:22 -0700 | [diff] [blame] | 2933 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2934 | dev_info(ctrl->ctrl.device, |
| 2935 | "NVME-FC{%d}: new ctrl: NQN \"%s\"\n", |
| 2936 | ctrl->cnum, ctrl->ctrl.opts->subsysnqn); |
| 2937 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2938 | return &ctrl->ctrl; |
| 2939 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2940 | out_cleanup_admin_q: |
| 2941 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
| 2942 | out_free_admin_tag_set: |
| 2943 | blk_mq_free_tag_set(&ctrl->admin_tag_set); |
| 2944 | out_free_queues: |
| 2945 | kfree(ctrl->queues); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2946 | out_free_ida: |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 2947 | put_device(ctrl->dev); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2948 | ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum); |
| 2949 | out_free_ctrl: |
| 2950 | kfree(ctrl); |
| 2951 | out_fail: |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2952 | /* exit via here doesn't follow ctlr ref points */ |
| 2953 | return ERR_PTR(ret); |
| 2954 | } |
| 2955 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2956 | |
| 2957 | struct nvmet_fc_traddr { |
| 2958 | u64 nn; |
| 2959 | u64 pn; |
| 2960 | }; |
| 2961 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2962 | static int |
James Smart | 9c5358e | 2017-07-17 13:59:39 -0700 | [diff] [blame] | 2963 | __nvme_fc_parse_u64(substring_t *sstr, u64 *val) |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2964 | { |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2965 | u64 token64; |
| 2966 | |
James Smart | 9c5358e | 2017-07-17 13:59:39 -0700 | [diff] [blame] | 2967 | if (match_u64(sstr, &token64)) |
| 2968 | return -EINVAL; |
| 2969 | *val = token64; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2970 | |
James Smart | 9c5358e | 2017-07-17 13:59:39 -0700 | [diff] [blame] | 2971 | return 0; |
| 2972 | } |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2973 | |
James Smart | 9c5358e | 2017-07-17 13:59:39 -0700 | [diff] [blame] | 2974 | /* |
| 2975 | * This routine validates and extracts the WWN's from the TRADDR string. |
| 2976 | * As kernel parsers need the 0x to determine number base, universally |
| 2977 | * build string to parse with 0x prefix before parsing name strings. |
| 2978 | */ |
| 2979 | static int |
| 2980 | nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen) |
| 2981 | { |
| 2982 | char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1]; |
| 2983 | substring_t wwn = { name, &name[sizeof(name)-1] }; |
| 2984 | int nnoffset, pnoffset; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 2985 | |
James Smart | 9c5358e | 2017-07-17 13:59:39 -0700 | [diff] [blame] | 2986 | /* validate it string one of the 2 allowed formats */ |
| 2987 | if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH && |
| 2988 | !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) && |
| 2989 | !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET], |
| 2990 | "pn-0x", NVME_FC_TRADDR_OXNNLEN)) { |
| 2991 | nnoffset = NVME_FC_TRADDR_OXNNLEN; |
| 2992 | pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET + |
| 2993 | NVME_FC_TRADDR_OXNNLEN; |
| 2994 | } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH && |
| 2995 | !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) && |
| 2996 | !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET], |
| 2997 | "pn-", NVME_FC_TRADDR_NNLEN))) { |
| 2998 | nnoffset = NVME_FC_TRADDR_NNLEN; |
| 2999 | pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN; |
| 3000 | } else |
| 3001 | goto out_einval; |
| 3002 | |
| 3003 | name[0] = '0'; |
| 3004 | name[1] = 'x'; |
| 3005 | name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0; |
| 3006 | |
| 3007 | memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN); |
| 3008 | if (__nvme_fc_parse_u64(&wwn, &traddr->nn)) |
| 3009 | goto out_einval; |
| 3010 | |
| 3011 | memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN); |
| 3012 | if (__nvme_fc_parse_u64(&wwn, &traddr->pn)) |
| 3013 | goto out_einval; |
| 3014 | |
| 3015 | return 0; |
| 3016 | |
| 3017 | out_einval: |
| 3018 | pr_warn("%s: bad traddr string\n", __func__); |
| 3019 | return -EINVAL; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | static struct nvme_ctrl * |
| 3023 | nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts) |
| 3024 | { |
| 3025 | struct nvme_fc_lport *lport; |
| 3026 | struct nvme_fc_rport *rport; |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 3027 | struct nvme_ctrl *ctrl; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 3028 | struct nvmet_fc_traddr laddr = { 0L, 0L }; |
| 3029 | struct nvmet_fc_traddr raddr = { 0L, 0L }; |
| 3030 | unsigned long flags; |
| 3031 | int ret; |
| 3032 | |
James Smart | 9c5358e | 2017-07-17 13:59:39 -0700 | [diff] [blame] | 3033 | ret = nvme_fc_parse_traddr(&raddr, opts->traddr, NVMF_TRADDR_SIZE); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 3034 | if (ret || !raddr.nn || !raddr.pn) |
| 3035 | return ERR_PTR(-EINVAL); |
| 3036 | |
James Smart | 9c5358e | 2017-07-17 13:59:39 -0700 | [diff] [blame] | 3037 | ret = nvme_fc_parse_traddr(&laddr, opts->host_traddr, NVMF_TRADDR_SIZE); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 3038 | if (ret || !laddr.nn || !laddr.pn) |
| 3039 | return ERR_PTR(-EINVAL); |
| 3040 | |
| 3041 | /* find the host and remote ports to connect together */ |
| 3042 | spin_lock_irqsave(&nvme_fc_lock, flags); |
| 3043 | list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { |
| 3044 | if (lport->localport.node_name != laddr.nn || |
| 3045 | lport->localport.port_name != laddr.pn) |
| 3046 | continue; |
| 3047 | |
| 3048 | list_for_each_entry(rport, &lport->endp_list, endp_list) { |
| 3049 | if (rport->remoteport.node_name != raddr.nn || |
| 3050 | rport->remoteport.port_name != raddr.pn) |
| 3051 | continue; |
| 3052 | |
| 3053 | /* if fail to get reference fall through. Will error */ |
| 3054 | if (!nvme_fc_rport_get(rport)) |
| 3055 | break; |
| 3056 | |
| 3057 | spin_unlock_irqrestore(&nvme_fc_lock, flags); |
| 3058 | |
James Smart | 61bff8e | 2017-04-23 08:30:08 -0700 | [diff] [blame] | 3059 | ctrl = nvme_fc_init_ctrl(dev, opts, lport, rport); |
| 3060 | if (IS_ERR(ctrl)) |
| 3061 | nvme_fc_rport_put(rport); |
| 3062 | return ctrl; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 3063 | } |
| 3064 | } |
| 3065 | spin_unlock_irqrestore(&nvme_fc_lock, flags); |
| 3066 | |
| 3067 | return ERR_PTR(-ENOENT); |
| 3068 | } |
| 3069 | |
| 3070 | |
| 3071 | static struct nvmf_transport_ops nvme_fc_transport = { |
| 3072 | .name = "fc", |
| 3073 | .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR, |
James Smart | 5bbecdb | 2017-05-15 17:10:16 -0700 | [diff] [blame] | 3074 | .allowed_opts = NVMF_OPT_RECONNECT_DELAY | NVMF_OPT_CTRL_LOSS_TMO, |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 3075 | .create_ctrl = nvme_fc_create_ctrl, |
| 3076 | }; |
| 3077 | |
| 3078 | static int __init nvme_fc_init_module(void) |
| 3079 | { |
James Smart | 5f56855 | 2017-09-14 10:38:41 -0700 | [diff] [blame] | 3080 | int ret; |
| 3081 | |
| 3082 | /* |
| 3083 | * NOTE: |
| 3084 | * It is expected that in the future the kernel will combine |
| 3085 | * the FC-isms that are currently under scsi and now being |
| 3086 | * added to by NVME into a new standalone FC class. The SCSI |
| 3087 | * and NVME protocols and their devices would be under this |
| 3088 | * new FC class. |
| 3089 | * |
| 3090 | * As we need something to post FC-specific udev events to, |
| 3091 | * specifically for nvme probe events, start by creating the |
| 3092 | * new device class. When the new standalone FC class is |
| 3093 | * put in place, this code will move to a more generic |
| 3094 | * location for the class. |
| 3095 | */ |
| 3096 | fc_class = class_create(THIS_MODULE, "fc"); |
| 3097 | if (IS_ERR(fc_class)) { |
| 3098 | pr_err("couldn't register class fc\n"); |
| 3099 | return PTR_ERR(fc_class); |
| 3100 | } |
| 3101 | |
| 3102 | /* |
| 3103 | * Create a device for the FC-centric udev events |
| 3104 | */ |
| 3105 | fc_udev_device = device_create(fc_class, NULL, MKDEV(0, 0), NULL, |
| 3106 | "fc_udev_device"); |
| 3107 | if (IS_ERR(fc_udev_device)) { |
| 3108 | pr_err("couldn't create fc_udev device!\n"); |
| 3109 | ret = PTR_ERR(fc_udev_device); |
| 3110 | goto out_destroy_class; |
| 3111 | } |
| 3112 | |
| 3113 | ret = nvmf_register_transport(&nvme_fc_transport); |
| 3114 | if (ret) |
| 3115 | goto out_destroy_device; |
| 3116 | |
| 3117 | return 0; |
| 3118 | |
| 3119 | out_destroy_device: |
| 3120 | device_destroy(fc_class, MKDEV(0, 0)); |
| 3121 | out_destroy_class: |
| 3122 | class_destroy(fc_class); |
| 3123 | return ret; |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | static void __exit nvme_fc_exit_module(void) |
| 3127 | { |
| 3128 | /* sanity check - all lports should be removed */ |
| 3129 | if (!list_empty(&nvme_fc_lport_list)) |
| 3130 | pr_warn("%s: localport list not empty\n", __func__); |
| 3131 | |
| 3132 | nvmf_unregister_transport(&nvme_fc_transport); |
| 3133 | |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 3134 | ida_destroy(&nvme_fc_local_port_cnt); |
| 3135 | ida_destroy(&nvme_fc_ctrl_cnt); |
James Smart | 5f56855 | 2017-09-14 10:38:41 -0700 | [diff] [blame] | 3136 | |
| 3137 | device_destroy(fc_class, MKDEV(0, 0)); |
| 3138 | class_destroy(fc_class); |
James Smart | e399441 | 2016-12-02 00:28:42 -0800 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | module_init(nvme_fc_init_module); |
| 3142 | module_exit(nvme_fc_exit_module); |
| 3143 | |
| 3144 | MODULE_LICENSE("GPL v2"); |