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