Christoph Hellwig | bc50ad7 | 2019-02-18 09:36:29 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 2 | /* |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 3 | * Copyright (c) 2017-2018 Christoph Hellwig. |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/moduleparam.h> |
Hannes Reinecke | 2796b56 | 2018-06-07 10:38:47 +0200 | [diff] [blame] | 7 | #include <trace/events/block.h> |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 8 | #include "nvme.h" |
| 9 | |
| 10 | static bool multipath = true; |
Keith Busch | 5cadde8 | 2018-04-26 14:24:29 -0600 | [diff] [blame] | 11 | module_param(multipath, bool, 0444); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 12 | MODULE_PARM_DESC(multipath, |
| 13 | "turn on native support for multiple controllers per subsystem"); |
| 14 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 15 | inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl) |
| 16 | { |
Hannes Reinecke | 8f220c4 | 2018-08-07 12:43:42 +0200 | [diff] [blame] | 17 | return multipath && ctrl->subsys && (ctrl->subsys->cmic & (1 << 3)); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 18 | } |
| 19 | |
Keith Busch | a785dbc | 2018-04-26 14:22:41 -0600 | [diff] [blame] | 20 | /* |
| 21 | * If multipathing is enabled we need to always use the subsystem instance |
| 22 | * number for numbering our devices to avoid conflicts between subsystems that |
| 23 | * have multiple controllers and thus use the multipath-aware subsystem node |
| 24 | * and those that have a single controller and use the controller node |
| 25 | * directly. |
| 26 | */ |
| 27 | void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns, |
| 28 | struct nvme_ctrl *ctrl, int *flags) |
| 29 | { |
| 30 | if (!multipath) { |
| 31 | sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance); |
| 32 | } else if (ns->head->disk) { |
| 33 | sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance, |
| 34 | ctrl->cntlid, ns->head->instance); |
| 35 | *flags = GENHD_FL_HIDDEN; |
| 36 | } else { |
| 37 | sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance, |
| 38 | ns->head->instance); |
| 39 | } |
| 40 | } |
| 41 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 42 | void nvme_failover_req(struct request *req) |
| 43 | { |
| 44 | struct nvme_ns *ns = req->q->queuedata; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 45 | u16 status = nvme_req(req)->status; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 46 | unsigned long flags; |
| 47 | |
| 48 | spin_lock_irqsave(&ns->head->requeue_lock, flags); |
| 49 | blk_steal_bios(&ns->head->requeue_list, req); |
| 50 | spin_unlock_irqrestore(&ns->head->requeue_lock, flags); |
| 51 | blk_mq_end_request(req, 0); |
| 52 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 53 | switch (status & 0x7ff) { |
| 54 | case NVME_SC_ANA_TRANSITION: |
| 55 | case NVME_SC_ANA_INACCESSIBLE: |
| 56 | case NVME_SC_ANA_PERSISTENT_LOSS: |
| 57 | /* |
| 58 | * If we got back an ANA error we know the controller is alive, |
| 59 | * but not ready to serve this namespaces. The spec suggests |
| 60 | * we should update our general state here, but due to the fact |
| 61 | * that the admin and I/O queues are not serialized that is |
| 62 | * fundamentally racy. So instead just clear the current path, |
| 63 | * mark the the path as pending and kick of a re-read of the ANA |
| 64 | * log page ASAP. |
| 65 | */ |
| 66 | nvme_mpath_clear_current_path(ns); |
| 67 | if (ns->ctrl->ana_log_buf) { |
| 68 | set_bit(NVME_NS_ANA_PENDING, &ns->flags); |
| 69 | queue_work(nvme_wq, &ns->ctrl->ana_work); |
| 70 | } |
| 71 | break; |
James Smart | 783f4a4 | 2018-09-27 16:58:54 -0700 | [diff] [blame] | 72 | case NVME_SC_HOST_PATH_ERROR: |
| 73 | /* |
| 74 | * Temporary transport disruption in talking to the controller. |
| 75 | * Try to send on a new path. |
| 76 | */ |
| 77 | nvme_mpath_clear_current_path(ns); |
| 78 | break; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 79 | default: |
| 80 | /* |
| 81 | * Reset the controller for any non-ANA error as we don't know |
| 82 | * what caused the error. |
| 83 | */ |
| 84 | nvme_reset_ctrl(ns->ctrl); |
| 85 | break; |
| 86 | } |
| 87 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 88 | kblockd_schedule_work(&ns->head->requeue_work); |
| 89 | } |
| 90 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 91 | void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl) |
| 92 | { |
| 93 | struct nvme_ns *ns; |
| 94 | |
Jianchao Wang | 765cc031 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 95 | down_read(&ctrl->namespaces_rwsem); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 96 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
| 97 | if (ns->head->disk) |
| 98 | kblockd_schedule_work(&ns->head->requeue_work); |
| 99 | } |
Jianchao Wang | 765cc031 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 100 | up_read(&ctrl->namespaces_rwsem); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 103 | static const char *nvme_ana_state_names[] = { |
| 104 | [0] = "invalid state", |
| 105 | [NVME_ANA_OPTIMIZED] = "optimized", |
| 106 | [NVME_ANA_NONOPTIMIZED] = "non-optimized", |
| 107 | [NVME_ANA_INACCESSIBLE] = "inaccessible", |
| 108 | [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss", |
| 109 | [NVME_ANA_CHANGE] = "change", |
| 110 | }; |
| 111 | |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 112 | void nvme_mpath_clear_current_path(struct nvme_ns *ns) |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 113 | { |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 114 | struct nvme_ns_head *head = ns->head; |
| 115 | int node; |
| 116 | |
| 117 | if (!head) |
| 118 | return; |
| 119 | |
| 120 | for_each_node(node) { |
| 121 | if (ns == rcu_access_pointer(head->current_path[node])) |
| 122 | rcu_assign_pointer(head->current_path[node], NULL); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node) |
| 127 | { |
| 128 | int found_distance = INT_MAX, fallback_distance = INT_MAX, distance; |
| 129 | struct nvme_ns *found = NULL, *fallback = NULL, *ns; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 130 | |
| 131 | list_for_each_entry_rcu(ns, &head->list, siblings) { |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 132 | if (ns->ctrl->state != NVME_CTRL_LIVE || |
| 133 | test_bit(NVME_NS_ANA_PENDING, &ns->flags)) |
| 134 | continue; |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 135 | |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 136 | if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA) |
| 137 | distance = node_distance(node, ns->ctrl->numa_node); |
| 138 | else |
| 139 | distance = LOCAL_DISTANCE; |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 140 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 141 | switch (ns->ana_state) { |
| 142 | case NVME_ANA_OPTIMIZED: |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 143 | if (distance < found_distance) { |
| 144 | found_distance = distance; |
| 145 | found = ns; |
| 146 | } |
| 147 | break; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 148 | case NVME_ANA_NONOPTIMIZED: |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 149 | if (distance < fallback_distance) { |
| 150 | fallback_distance = distance; |
| 151 | fallback = ns; |
| 152 | } |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 153 | break; |
| 154 | default: |
| 155 | break; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 159 | if (!found) |
| 160 | found = fallback; |
| 161 | if (found) |
| 162 | rcu_assign_pointer(head->current_path[node], found); |
| 163 | return found; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 166 | static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head, |
| 167 | struct nvme_ns *ns) |
| 168 | { |
| 169 | ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns, |
| 170 | siblings); |
| 171 | if (ns) |
| 172 | return ns; |
| 173 | return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings); |
| 174 | } |
| 175 | |
| 176 | static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head, |
| 177 | int node, struct nvme_ns *old) |
| 178 | { |
| 179 | struct nvme_ns *ns, *found, *fallback = NULL; |
| 180 | |
| 181 | if (list_is_singular(&head->list)) |
| 182 | return old; |
| 183 | |
| 184 | for (ns = nvme_next_ns(head, old); |
| 185 | ns != old; |
| 186 | ns = nvme_next_ns(head, ns)) { |
| 187 | if (ns->ctrl->state != NVME_CTRL_LIVE || |
| 188 | test_bit(NVME_NS_ANA_PENDING, &ns->flags)) |
| 189 | continue; |
| 190 | |
| 191 | if (ns->ana_state == NVME_ANA_OPTIMIZED) { |
| 192 | found = ns; |
| 193 | goto out; |
| 194 | } |
| 195 | if (ns->ana_state == NVME_ANA_NONOPTIMIZED) |
| 196 | fallback = ns; |
| 197 | } |
| 198 | |
| 199 | if (!fallback) |
| 200 | return NULL; |
| 201 | found = fallback; |
| 202 | out: |
| 203 | rcu_assign_pointer(head->current_path[node], found); |
| 204 | return found; |
| 205 | } |
| 206 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 207 | static inline bool nvme_path_is_optimized(struct nvme_ns *ns) |
| 208 | { |
| 209 | return ns->ctrl->state == NVME_CTRL_LIVE && |
| 210 | ns->ana_state == NVME_ANA_OPTIMIZED; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head) |
| 214 | { |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 215 | int node = numa_node_id(); |
| 216 | struct nvme_ns *ns; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 217 | |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 218 | ns = srcu_dereference(head->current_path[node], &head->srcu); |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 219 | if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR && ns) |
| 220 | ns = nvme_round_robin_path(head, node, ns); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 221 | if (unlikely(!ns || !nvme_path_is_optimized(ns))) |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 222 | ns = __nvme_find_path(head, node); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 223 | return ns; |
| 224 | } |
| 225 | |
| 226 | static blk_qc_t nvme_ns_head_make_request(struct request_queue *q, |
| 227 | struct bio *bio) |
| 228 | { |
| 229 | struct nvme_ns_head *head = q->queuedata; |
| 230 | struct device *dev = disk_to_dev(head->disk); |
| 231 | struct nvme_ns *ns; |
| 232 | blk_qc_t ret = BLK_QC_T_NONE; |
| 233 | int srcu_idx; |
| 234 | |
| 235 | srcu_idx = srcu_read_lock(&head->srcu); |
| 236 | ns = nvme_find_path(head); |
| 237 | if (likely(ns)) { |
| 238 | bio->bi_disk = ns->disk; |
| 239 | bio->bi_opf |= REQ_NVME_MPATH; |
Hannes Reinecke | 2796b56 | 2018-06-07 10:38:47 +0200 | [diff] [blame] | 240 | trace_block_bio_remap(bio->bi_disk->queue, bio, |
| 241 | disk_devt(ns->head->disk), |
| 242 | bio->bi_iter.bi_sector); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 243 | ret = direct_make_request(bio); |
| 244 | } else if (!list_empty_careful(&head->list)) { |
Colin Ian King | 89c4aff | 2017-11-14 14:26:27 +0000 | [diff] [blame] | 245 | dev_warn_ratelimited(dev, "no path available - requeuing I/O\n"); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 246 | |
| 247 | spin_lock_irq(&head->requeue_lock); |
| 248 | bio_list_add(&head->requeue_list, bio); |
| 249 | spin_unlock_irq(&head->requeue_lock); |
| 250 | } else { |
| 251 | dev_warn_ratelimited(dev, "no path - failing I/O\n"); |
| 252 | |
| 253 | bio->bi_status = BLK_STS_IOERR; |
| 254 | bio_endio(bio); |
| 255 | } |
| 256 | |
| 257 | srcu_read_unlock(&head->srcu, srcu_idx); |
| 258 | return ret; |
| 259 | } |
| 260 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 261 | static void nvme_requeue_work(struct work_struct *work) |
| 262 | { |
| 263 | struct nvme_ns_head *head = |
| 264 | container_of(work, struct nvme_ns_head, requeue_work); |
| 265 | struct bio *bio, *next; |
| 266 | |
| 267 | spin_lock_irq(&head->requeue_lock); |
| 268 | next = bio_list_get(&head->requeue_list); |
| 269 | spin_unlock_irq(&head->requeue_lock); |
| 270 | |
| 271 | while ((bio = next) != NULL) { |
| 272 | next = bio->bi_next; |
| 273 | bio->bi_next = NULL; |
| 274 | |
| 275 | /* |
| 276 | * Reset disk to the mpath node and resubmit to select a new |
| 277 | * path. |
| 278 | */ |
| 279 | bio->bi_disk = head->disk; |
| 280 | generic_make_request(bio); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head) |
| 285 | { |
| 286 | struct request_queue *q; |
| 287 | bool vwc = false; |
| 288 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 289 | mutex_init(&head->lock); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 290 | bio_list_init(&head->requeue_list); |
| 291 | spin_lock_init(&head->requeue_lock); |
| 292 | INIT_WORK(&head->requeue_work, nvme_requeue_work); |
| 293 | |
| 294 | /* |
| 295 | * Add a multipath node if the subsystems supports multiple controllers. |
| 296 | * We also do this for private namespaces as the namespace sharing data could |
| 297 | * change after a rescan. |
| 298 | */ |
| 299 | if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath) |
| 300 | return 0; |
| 301 | |
Hannes Reinecke | 103e515 | 2018-11-16 09:22:29 +0100 | [diff] [blame] | 302 | q = blk_alloc_queue_node(GFP_KERNEL, ctrl->numa_node); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 303 | if (!q) |
| 304 | goto out; |
| 305 | q->queuedata = head; |
| 306 | blk_queue_make_request(q, nvme_ns_head_make_request); |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 307 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 308 | /* set to a default value for 512 until disk is validated */ |
| 309 | blk_queue_logical_block_size(q, 512); |
Sagi Grimberg | 8f676b85 | 2018-11-02 11:22:13 -0700 | [diff] [blame] | 310 | blk_set_stacking_limits(&q->limits); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 311 | |
| 312 | /* we need to propagate up the VMC settings */ |
| 313 | if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) |
| 314 | vwc = true; |
| 315 | blk_queue_write_cache(q, vwc, vwc); |
| 316 | |
| 317 | head->disk = alloc_disk(0); |
| 318 | if (!head->disk) |
| 319 | goto out_cleanup_queue; |
| 320 | head->disk->fops = &nvme_ns_head_ops; |
| 321 | head->disk->private_data = head; |
| 322 | head->disk->queue = q; |
| 323 | head->disk->flags = GENHD_FL_EXT_DEVT; |
| 324 | sprintf(head->disk->disk_name, "nvme%dn%d", |
| 325 | ctrl->subsys->instance, head->instance); |
| 326 | return 0; |
| 327 | |
| 328 | out_cleanup_queue: |
| 329 | blk_cleanup_queue(q); |
| 330 | out: |
| 331 | return -ENOMEM; |
| 332 | } |
| 333 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 334 | static void nvme_mpath_set_live(struct nvme_ns *ns) |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 335 | { |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 336 | struct nvme_ns_head *head = ns->head; |
| 337 | |
| 338 | lockdep_assert_held(&ns->head->lock); |
| 339 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 340 | if (!head->disk) |
| 341 | return; |
Baegjae Sung | 9bd82b1 | 2018-02-28 16:06:04 +0900 | [diff] [blame] | 342 | |
Hannes Reinecke | 33b14f6 | 2018-09-28 08:17:20 +0200 | [diff] [blame] | 343 | if (!(head->disk->flags & GENHD_FL_UP)) |
| 344 | device_add_disk(&head->subsys->dev, head->disk, |
| 345 | nvme_ns_id_attr_groups); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 346 | |
Keith Busch | 886fabf | 2018-10-05 09:49:37 -0600 | [diff] [blame] | 347 | if (nvme_path_is_optimized(ns)) { |
| 348 | int node, srcu_idx; |
| 349 | |
| 350 | srcu_idx = srcu_read_lock(&head->srcu); |
| 351 | for_each_node(node) |
| 352 | __nvme_find_path(head, node); |
| 353 | srcu_read_unlock(&head->srcu, srcu_idx); |
| 354 | } |
| 355 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 356 | kblockd_schedule_work(&ns->head->requeue_work); |
| 357 | } |
| 358 | |
| 359 | static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data, |
| 360 | int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *, |
| 361 | void *)) |
| 362 | { |
| 363 | void *base = ctrl->ana_log_buf; |
| 364 | size_t offset = sizeof(struct nvme_ana_rsp_hdr); |
| 365 | int error, i; |
| 366 | |
| 367 | lockdep_assert_held(&ctrl->ana_lock); |
| 368 | |
| 369 | for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) { |
| 370 | struct nvme_ana_group_desc *desc = base + offset; |
| 371 | u32 nr_nsids = le32_to_cpu(desc->nnsids); |
| 372 | size_t nsid_buf_size = nr_nsids * sizeof(__le32); |
| 373 | |
| 374 | if (WARN_ON_ONCE(desc->grpid == 0)) |
| 375 | return -EINVAL; |
| 376 | if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax)) |
| 377 | return -EINVAL; |
| 378 | if (WARN_ON_ONCE(desc->state == 0)) |
| 379 | return -EINVAL; |
| 380 | if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE)) |
| 381 | return -EINVAL; |
| 382 | |
| 383 | offset += sizeof(*desc); |
| 384 | if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size)) |
| 385 | return -EINVAL; |
| 386 | |
| 387 | error = cb(ctrl, desc, data); |
| 388 | if (error) |
| 389 | return error; |
| 390 | |
| 391 | offset += nsid_buf_size; |
| 392 | if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc))) |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static inline bool nvme_state_is_live(enum nvme_ana_state state) |
| 400 | { |
| 401 | return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED; |
| 402 | } |
| 403 | |
| 404 | static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc, |
| 405 | struct nvme_ns *ns) |
| 406 | { |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 407 | mutex_lock(&ns->head->lock); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 408 | ns->ana_grpid = le32_to_cpu(desc->grpid); |
| 409 | ns->ana_state = desc->state; |
| 410 | clear_bit(NVME_NS_ANA_PENDING, &ns->flags); |
| 411 | |
Martin George | cc2278c | 2019-03-27 09:52:56 +0100 | [diff] [blame] | 412 | if (nvme_state_is_live(ns->ana_state)) |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 413 | nvme_mpath_set_live(ns); |
| 414 | mutex_unlock(&ns->head->lock); |
| 415 | } |
| 416 | |
| 417 | static int nvme_update_ana_state(struct nvme_ctrl *ctrl, |
| 418 | struct nvme_ana_group_desc *desc, void *data) |
| 419 | { |
| 420 | u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0; |
| 421 | unsigned *nr_change_groups = data; |
| 422 | struct nvme_ns *ns; |
| 423 | |
| 424 | dev_info(ctrl->device, "ANA group %d: %s.\n", |
| 425 | le32_to_cpu(desc->grpid), |
| 426 | nvme_ana_state_names[desc->state]); |
| 427 | |
| 428 | if (desc->state == NVME_ANA_CHANGE) |
| 429 | (*nr_change_groups)++; |
| 430 | |
| 431 | if (!nr_nsids) |
| 432 | return 0; |
| 433 | |
| 434 | down_write(&ctrl->namespaces_rwsem); |
| 435 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
| 436 | if (ns->head->ns_id != le32_to_cpu(desc->nsids[n])) |
| 437 | continue; |
| 438 | nvme_update_ns_ana_state(desc, ns); |
| 439 | if (++n == nr_nsids) |
| 440 | break; |
| 441 | } |
| 442 | up_write(&ctrl->namespaces_rwsem); |
| 443 | WARN_ON_ONCE(n < nr_nsids); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int nvme_read_ana_log(struct nvme_ctrl *ctrl, bool groups_only) |
| 448 | { |
| 449 | u32 nr_change_groups = 0; |
| 450 | int error; |
| 451 | |
| 452 | mutex_lock(&ctrl->ana_lock); |
| 453 | error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, |
| 454 | groups_only ? NVME_ANA_LOG_RGO : 0, |
| 455 | ctrl->ana_log_buf, ctrl->ana_log_size, 0); |
| 456 | if (error) { |
| 457 | dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error); |
| 458 | goto out_unlock; |
| 459 | } |
| 460 | |
| 461 | error = nvme_parse_ana_log(ctrl, &nr_change_groups, |
| 462 | nvme_update_ana_state); |
| 463 | if (error) |
| 464 | goto out_unlock; |
| 465 | |
| 466 | /* |
| 467 | * In theory we should have an ANATT timer per group as they might enter |
| 468 | * the change state at different times. But that is a lot of overhead |
| 469 | * just to protect against a target that keeps entering new changes |
| 470 | * states while never finishing previous ones. But we'll still |
| 471 | * eventually time out once all groups are in change state, so this |
| 472 | * isn't a big deal. |
| 473 | * |
| 474 | * We also double the ANATT value to provide some slack for transports |
| 475 | * or AEN processing overhead. |
| 476 | */ |
| 477 | if (nr_change_groups) |
| 478 | mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies); |
| 479 | else |
| 480 | del_timer_sync(&ctrl->anatt_timer); |
| 481 | out_unlock: |
| 482 | mutex_unlock(&ctrl->ana_lock); |
| 483 | return error; |
| 484 | } |
| 485 | |
| 486 | static void nvme_ana_work(struct work_struct *work) |
| 487 | { |
| 488 | struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work); |
| 489 | |
| 490 | nvme_read_ana_log(ctrl, false); |
| 491 | } |
| 492 | |
| 493 | static void nvme_anatt_timeout(struct timer_list *t) |
| 494 | { |
| 495 | struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer); |
| 496 | |
| 497 | dev_info(ctrl->device, "ANATT timeout, resetting controller.\n"); |
| 498 | nvme_reset_ctrl(ctrl); |
| 499 | } |
| 500 | |
| 501 | void nvme_mpath_stop(struct nvme_ctrl *ctrl) |
| 502 | { |
| 503 | if (!nvme_ctrl_use_ana(ctrl)) |
| 504 | return; |
| 505 | del_timer_sync(&ctrl->anatt_timer); |
| 506 | cancel_work_sync(&ctrl->ana_work); |
| 507 | } |
| 508 | |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 509 | #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \ |
| 510 | struct device_attribute subsys_attr_##_name = \ |
| 511 | __ATTR(_name, _mode, _show, _store) |
| 512 | |
| 513 | static const char *nvme_iopolicy_names[] = { |
| 514 | [NVME_IOPOLICY_NUMA] = "numa", |
| 515 | [NVME_IOPOLICY_RR] = "round-robin", |
| 516 | }; |
| 517 | |
| 518 | static ssize_t nvme_subsys_iopolicy_show(struct device *dev, |
| 519 | struct device_attribute *attr, char *buf) |
| 520 | { |
| 521 | struct nvme_subsystem *subsys = |
| 522 | container_of(dev, struct nvme_subsystem, dev); |
| 523 | |
| 524 | return sprintf(buf, "%s\n", |
| 525 | nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]); |
| 526 | } |
| 527 | |
| 528 | static ssize_t nvme_subsys_iopolicy_store(struct device *dev, |
| 529 | struct device_attribute *attr, const char *buf, size_t count) |
| 530 | { |
| 531 | struct nvme_subsystem *subsys = |
| 532 | container_of(dev, struct nvme_subsystem, dev); |
| 533 | int i; |
| 534 | |
| 535 | for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) { |
| 536 | if (sysfs_streq(buf, nvme_iopolicy_names[i])) { |
| 537 | WRITE_ONCE(subsys->iopolicy, i); |
| 538 | return count; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | return -EINVAL; |
| 543 | } |
| 544 | SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR, |
| 545 | nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store); |
| 546 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 547 | static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr, |
| 548 | char *buf) |
| 549 | { |
| 550 | return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid); |
| 551 | } |
| 552 | DEVICE_ATTR_RO(ana_grpid); |
| 553 | |
| 554 | static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr, |
| 555 | char *buf) |
| 556 | { |
| 557 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
| 558 | |
| 559 | return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]); |
| 560 | } |
| 561 | DEVICE_ATTR_RO(ana_state); |
| 562 | |
| 563 | static int nvme_set_ns_ana_state(struct nvme_ctrl *ctrl, |
| 564 | struct nvme_ana_group_desc *desc, void *data) |
| 565 | { |
| 566 | struct nvme_ns *ns = data; |
| 567 | |
| 568 | if (ns->ana_grpid == le32_to_cpu(desc->grpid)) { |
| 569 | nvme_update_ns_ana_state(desc, ns); |
| 570 | return -ENXIO; /* just break out of the loop */ |
| 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id) |
| 577 | { |
| 578 | if (nvme_ctrl_use_ana(ns->ctrl)) { |
| 579 | mutex_lock(&ns->ctrl->ana_lock); |
| 580 | ns->ana_grpid = le32_to_cpu(id->anagrpid); |
| 581 | nvme_parse_ana_log(ns->ctrl, ns, nvme_set_ns_ana_state); |
| 582 | mutex_unlock(&ns->ctrl->ana_lock); |
| 583 | } else { |
| 584 | mutex_lock(&ns->head->lock); |
| 585 | ns->ana_state = NVME_ANA_OPTIMIZED; |
| 586 | nvme_mpath_set_live(ns); |
| 587 | mutex_unlock(&ns->head->lock); |
| 588 | } |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void nvme_mpath_remove_disk(struct nvme_ns_head *head) |
| 592 | { |
| 593 | if (!head->disk) |
| 594 | return; |
Hannes Reinecke | 33b14f6 | 2018-09-28 08:17:20 +0200 | [diff] [blame] | 595 | if (head->disk->flags & GENHD_FL_UP) |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 596 | del_gendisk(head->disk); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 597 | blk_set_queue_dying(head->disk->queue); |
| 598 | /* make sure all pending bios are cleaned up */ |
| 599 | kblockd_schedule_work(&head->requeue_work); |
| 600 | flush_work(&head->requeue_work); |
| 601 | blk_cleanup_queue(head->disk->queue); |
| 602 | put_disk(head->disk); |
| 603 | } |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 604 | |
| 605 | int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) |
| 606 | { |
| 607 | int error; |
| 608 | |
| 609 | if (!nvme_ctrl_use_ana(ctrl)) |
| 610 | return 0; |
| 611 | |
| 612 | ctrl->anacap = id->anacap; |
| 613 | ctrl->anatt = id->anatt; |
| 614 | ctrl->nanagrpid = le32_to_cpu(id->nanagrpid); |
| 615 | ctrl->anagrpmax = le32_to_cpu(id->anagrpmax); |
| 616 | |
| 617 | mutex_init(&ctrl->ana_lock); |
| 618 | timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0); |
| 619 | ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) + |
| 620 | ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc); |
Hannes Reinecke | 78a61cd | 2019-01-09 09:45:15 +0100 | [diff] [blame] | 621 | ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 622 | |
| 623 | if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) { |
| 624 | dev_err(ctrl->device, |
| 625 | "ANA log page size (%zd) larger than MDTS (%d).\n", |
| 626 | ctrl->ana_log_size, |
| 627 | ctrl->max_hw_sectors << SECTOR_SHIFT); |
| 628 | dev_err(ctrl->device, "disabling ANA support.\n"); |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | INIT_WORK(&ctrl->ana_work, nvme_ana_work); |
| 633 | ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL); |
Susobhan Dey | bb830ad | 2018-09-25 12:29:15 -0700 | [diff] [blame] | 634 | if (!ctrl->ana_log_buf) { |
| 635 | error = -ENOMEM; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 636 | goto out; |
Susobhan Dey | bb830ad | 2018-09-25 12:29:15 -0700 | [diff] [blame] | 637 | } |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 638 | |
| 639 | error = nvme_read_ana_log(ctrl, true); |
| 640 | if (error) |
| 641 | goto out_free_ana_log_buf; |
| 642 | return 0; |
| 643 | out_free_ana_log_buf: |
| 644 | kfree(ctrl->ana_log_buf); |
Hannes Reinecke | c7055fd | 2019-01-08 12:46:58 +0100 | [diff] [blame] | 645 | ctrl->ana_log_buf = NULL; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 646 | out: |
Susobhan Dey | bb830ad | 2018-09-25 12:29:15 -0700 | [diff] [blame] | 647 | return error; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void nvme_mpath_uninit(struct nvme_ctrl *ctrl) |
| 651 | { |
| 652 | kfree(ctrl->ana_log_buf); |
Hannes Reinecke | c7055fd | 2019-01-08 12:46:58 +0100 | [diff] [blame] | 653 | ctrl->ana_log_buf = NULL; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 654 | } |
| 655 | |