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