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 | |
Keith Busch | b2ce4d9 | 2020-04-09 09:09:04 -0700 | [diff] [blame] | 6 | #include <linux/backing-dev.h> |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 7 | #include <linux/moduleparam.h> |
Hannes Reinecke | 2796b56 | 2018-06-07 10:38:47 +0200 | [diff] [blame] | 8 | #include <trace/events/block.h> |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 9 | #include "nvme.h" |
| 10 | |
| 11 | static bool multipath = true; |
Keith Busch | 5cadde8 | 2018-04-26 14:24:29 -0600 | [diff] [blame] | 12 | module_param(multipath, bool, 0444); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 13 | MODULE_PARM_DESC(multipath, |
| 14 | "turn on native support for multiple controllers per subsystem"); |
| 15 | |
Sagi Grimberg | b9156da | 2019-07-31 11:00:26 -0700 | [diff] [blame] | 16 | void nvme_mpath_unfreeze(struct nvme_subsystem *subsys) |
| 17 | { |
| 18 | struct nvme_ns_head *h; |
| 19 | |
| 20 | lockdep_assert_held(&subsys->lock); |
| 21 | list_for_each_entry(h, &subsys->nsheads, entry) |
| 22 | if (h->disk) |
| 23 | blk_mq_unfreeze_queue(h->disk->queue); |
| 24 | } |
| 25 | |
| 26 | void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys) |
| 27 | { |
| 28 | struct nvme_ns_head *h; |
| 29 | |
| 30 | lockdep_assert_held(&subsys->lock); |
| 31 | list_for_each_entry(h, &subsys->nsheads, entry) |
| 32 | if (h->disk) |
| 33 | blk_mq_freeze_queue_wait(h->disk->queue); |
| 34 | } |
| 35 | |
| 36 | void nvme_mpath_start_freeze(struct nvme_subsystem *subsys) |
| 37 | { |
| 38 | struct nvme_ns_head *h; |
| 39 | |
| 40 | lockdep_assert_held(&subsys->lock); |
| 41 | list_for_each_entry(h, &subsys->nsheads, entry) |
| 42 | if (h->disk) |
| 43 | blk_freeze_queue_start(h->disk->queue); |
| 44 | } |
| 45 | |
Keith Busch | a785dbc | 2018-04-26 14:22:41 -0600 | [diff] [blame] | 46 | /* |
| 47 | * If multipathing is enabled we need to always use the subsystem instance |
| 48 | * number for numbering our devices to avoid conflicts between subsystems that |
| 49 | * have multiple controllers and thus use the multipath-aware subsystem node |
| 50 | * and those that have a single controller and use the controller node |
| 51 | * directly. |
| 52 | */ |
Christoph Hellwig | 9953ab0 | 2021-04-07 12:46:46 +0200 | [diff] [blame] | 53 | bool nvme_mpath_set_disk_name(struct nvme_ns *ns, char *disk_name, int *flags) |
Keith Busch | a785dbc | 2018-04-26 14:22:41 -0600 | [diff] [blame] | 54 | { |
Christoph Hellwig | 9953ab0 | 2021-04-07 12:46:46 +0200 | [diff] [blame] | 55 | if (!multipath) |
| 56 | return false; |
| 57 | if (!ns->head->disk) { |
| 58 | sprintf(disk_name, "nvme%dn%d", ns->ctrl->subsys->instance, |
| 59 | ns->head->instance); |
| 60 | return true; |
Keith Busch | a785dbc | 2018-04-26 14:22:41 -0600 | [diff] [blame] | 61 | } |
Christoph Hellwig | 9953ab0 | 2021-04-07 12:46:46 +0200 | [diff] [blame] | 62 | sprintf(disk_name, "nvme%dc%dn%d", ns->ctrl->subsys->instance, |
| 63 | ns->ctrl->instance, ns->head->instance); |
| 64 | *flags = GENHD_FL_HIDDEN; |
| 65 | return true; |
Keith Busch | a785dbc | 2018-04-26 14:22:41 -0600 | [diff] [blame] | 66 | } |
| 67 | |
Christoph Hellwig | 5ddaabe | 2020-08-18 09:11:30 +0200 | [diff] [blame] | 68 | void nvme_failover_req(struct request *req) |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 69 | { |
| 70 | struct nvme_ns *ns = req->q->queuedata; |
Christoph Hellwig | 5ddaabe | 2020-08-18 09:11:30 +0200 | [diff] [blame] | 71 | u16 status = nvme_req(req)->status & 0x7ff; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 72 | unsigned long flags; |
Daniel Wagner | ce86dad | 2021-05-03 19:03:03 +0200 | [diff] [blame] | 73 | struct bio *bio; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 74 | |
Christoph Hellwig | 5ddaabe | 2020-08-18 09:11:30 +0200 | [diff] [blame] | 75 | nvme_mpath_clear_current_path(ns); |
| 76 | |
| 77 | /* |
| 78 | * If we got back an ANA error, we know the controller is alive but not |
| 79 | * ready to serve this namespace. Kick of a re-read of the ANA |
| 80 | * information page, and just try any other available path for now. |
| 81 | */ |
| 82 | if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) { |
| 83 | set_bit(NVME_NS_ANA_PENDING, &ns->flags); |
| 84 | queue_work(nvme_wq, &ns->ctrl->ana_work); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 85 | } |
| 86 | |
John Meneghini | 764e933 | 2020-02-20 10:05:38 +0900 | [diff] [blame] | 87 | spin_lock_irqsave(&ns->head->requeue_lock, flags); |
Daniel Wagner | ce86dad | 2021-05-03 19:03:03 +0200 | [diff] [blame] | 88 | for (bio = req->bio; bio; bio = bio->bi_next) |
| 89 | bio_set_dev(bio, ns->head->disk->part0); |
John Meneghini | 764e933 | 2020-02-20 10:05:38 +0900 | [diff] [blame] | 90 | blk_steal_bios(&ns->head->requeue_list, req); |
| 91 | spin_unlock_irqrestore(&ns->head->requeue_lock, flags); |
John Meneghini | 764e933 | 2020-02-20 10:05:38 +0900 | [diff] [blame] | 92 | |
Christoph Hellwig | 5ddaabe | 2020-08-18 09:11:30 +0200 | [diff] [blame] | 93 | blk_mq_end_request(req, 0); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 94 | kblockd_schedule_work(&ns->head->requeue_work); |
| 95 | } |
| 96 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 97 | void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl) |
| 98 | { |
| 99 | struct nvme_ns *ns; |
| 100 | |
Jianchao Wang | 765cc031 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 101 | down_read(&ctrl->namespaces_rwsem); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 102 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
| 103 | if (ns->head->disk) |
| 104 | kblockd_schedule_work(&ns->head->requeue_work); |
| 105 | } |
Jianchao Wang | 765cc031 | 2018-02-12 20:54:46 +0800 | [diff] [blame] | 106 | up_read(&ctrl->namespaces_rwsem); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 109 | static const char *nvme_ana_state_names[] = { |
| 110 | [0] = "invalid state", |
| 111 | [NVME_ANA_OPTIMIZED] = "optimized", |
| 112 | [NVME_ANA_NONOPTIMIZED] = "non-optimized", |
| 113 | [NVME_ANA_INACCESSIBLE] = "inaccessible", |
| 114 | [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss", |
| 115 | [NVME_ANA_CHANGE] = "change", |
| 116 | }; |
| 117 | |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 118 | bool nvme_mpath_clear_current_path(struct nvme_ns *ns) |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 119 | { |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 120 | struct nvme_ns_head *head = ns->head; |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 121 | bool changed = false; |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 122 | int node; |
| 123 | |
| 124 | if (!head) |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 125 | goto out; |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 126 | |
| 127 | for_each_node(node) { |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 128 | if (ns == rcu_access_pointer(head->current_path[node])) { |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 129 | rcu_assign_pointer(head->current_path[node], NULL); |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 130 | changed = true; |
| 131 | } |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 132 | } |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 133 | out: |
| 134 | return changed; |
| 135 | } |
| 136 | |
| 137 | void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl) |
| 138 | { |
| 139 | struct nvme_ns *ns; |
| 140 | |
| 141 | mutex_lock(&ctrl->scan_lock); |
Anton Eidelman | 763303a | 2019-11-01 17:27:55 -0700 | [diff] [blame] | 142 | down_read(&ctrl->namespaces_rwsem); |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 143 | list_for_each_entry(ns, &ctrl->namespaces, list) |
| 144 | if (nvme_mpath_clear_current_path(ns)) |
| 145 | kblockd_schedule_work(&ns->head->requeue_work); |
Anton Eidelman | 763303a | 2019-11-01 17:27:55 -0700 | [diff] [blame] | 146 | up_read(&ctrl->namespaces_rwsem); |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 147 | mutex_unlock(&ctrl->scan_lock); |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Hannes Reinecke | ca7ae5c | 2019-07-04 08:10:46 +0200 | [diff] [blame] | 150 | static bool nvme_path_is_disabled(struct nvme_ns *ns) |
| 151 | { |
Sagi Grimberg | ecca390e | 2020-07-22 16:32:19 -0700 | [diff] [blame] | 152 | /* |
| 153 | * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should |
| 154 | * still be able to complete assuming that the controller is connected. |
| 155 | * Otherwise it will fail immediately and return to the requeue list. |
| 156 | */ |
| 157 | if (ns->ctrl->state != NVME_CTRL_LIVE && |
| 158 | ns->ctrl->state != NVME_CTRL_DELETING) |
| 159 | return true; |
| 160 | if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) || |
| 161 | test_bit(NVME_NS_REMOVING, &ns->flags)) |
| 162 | return true; |
| 163 | return false; |
Hannes Reinecke | ca7ae5c | 2019-07-04 08:10:46 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 166 | static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node) |
| 167 | { |
| 168 | int found_distance = INT_MAX, fallback_distance = INT_MAX, distance; |
| 169 | struct nvme_ns *found = NULL, *fallback = NULL, *ns; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 170 | |
| 171 | list_for_each_entry_rcu(ns, &head->list, siblings) { |
Hannes Reinecke | ca7ae5c | 2019-07-04 08:10:46 +0200 | [diff] [blame] | 172 | if (nvme_path_is_disabled(ns)) |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 173 | continue; |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 174 | |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 175 | if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA) |
| 176 | distance = node_distance(node, ns->ctrl->numa_node); |
| 177 | else |
| 178 | distance = LOCAL_DISTANCE; |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 179 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 180 | switch (ns->ana_state) { |
| 181 | case NVME_ANA_OPTIMIZED: |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 182 | if (distance < found_distance) { |
| 183 | found_distance = distance; |
| 184 | found = ns; |
| 185 | } |
| 186 | break; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 187 | case NVME_ANA_NONOPTIMIZED: |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 188 | if (distance < fallback_distance) { |
| 189 | fallback_distance = distance; |
| 190 | fallback = ns; |
| 191 | } |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 192 | break; |
| 193 | default: |
| 194 | break; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 198 | if (!found) |
| 199 | found = fallback; |
| 200 | if (found) |
| 201 | rcu_assign_pointer(head->current_path[node], found); |
| 202 | return found; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 205 | static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head, |
| 206 | struct nvme_ns *ns) |
| 207 | { |
| 208 | ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns, |
| 209 | siblings); |
| 210 | if (ns) |
| 211 | return ns; |
| 212 | return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings); |
| 213 | } |
| 214 | |
| 215 | static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head, |
| 216 | int node, struct nvme_ns *old) |
| 217 | { |
Martin Wilck | e398863 | 2020-08-06 15:19:32 +0200 | [diff] [blame] | 218 | struct nvme_ns *ns, *found = NULL; |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 219 | |
Hannes Reinecke | 2032d07 | 2019-07-04 08:10:46 +0200 | [diff] [blame] | 220 | if (list_is_singular(&head->list)) { |
| 221 | if (nvme_path_is_disabled(old)) |
| 222 | return NULL; |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 223 | return old; |
Hannes Reinecke | 2032d07 | 2019-07-04 08:10:46 +0200 | [diff] [blame] | 224 | } |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 225 | |
| 226 | for (ns = nvme_next_ns(head, old); |
Daniel Wagner | d1bcf00 | 2021-01-27 11:30:33 +0100 | [diff] [blame] | 227 | ns && ns != old; |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 228 | ns = nvme_next_ns(head, ns)) { |
Hannes Reinecke | ca7ae5c | 2019-07-04 08:10:46 +0200 | [diff] [blame] | 229 | if (nvme_path_is_disabled(ns)) |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 230 | continue; |
| 231 | |
| 232 | if (ns->ana_state == NVME_ANA_OPTIMIZED) { |
| 233 | found = ns; |
| 234 | goto out; |
| 235 | } |
| 236 | if (ns->ana_state == NVME_ANA_NONOPTIMIZED) |
Martin Wilck | e398863 | 2020-08-06 15:19:32 +0200 | [diff] [blame] | 237 | found = ns; |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Martin Wilck | 93eb038 | 2020-08-06 15:19:31 +0200 | [diff] [blame] | 240 | /* |
| 241 | * The loop above skips the current path for round-robin semantics. |
| 242 | * Fall back to the current path if either: |
| 243 | * - no other optimized path found and current is optimized, |
| 244 | * - no other usable path found and current is usable. |
| 245 | */ |
Martin Wilck | 3f6e324 | 2020-07-27 18:08:02 +0200 | [diff] [blame] | 246 | if (!nvme_path_is_disabled(old) && |
Martin Wilck | 93eb038 | 2020-08-06 15:19:31 +0200 | [diff] [blame] | 247 | (old->ana_state == NVME_ANA_OPTIMIZED || |
Martin Wilck | e398863 | 2020-08-06 15:19:32 +0200 | [diff] [blame] | 248 | (!found && old->ana_state == NVME_ANA_NONOPTIMIZED))) |
Martin Wilck | 93eb038 | 2020-08-06 15:19:31 +0200 | [diff] [blame] | 249 | return old; |
| 250 | |
Martin Wilck | e398863 | 2020-08-06 15:19:32 +0200 | [diff] [blame] | 251 | if (!found) |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 252 | return NULL; |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 253 | out: |
| 254 | rcu_assign_pointer(head->current_path[node], found); |
| 255 | return found; |
| 256 | } |
| 257 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 258 | static inline bool nvme_path_is_optimized(struct nvme_ns *ns) |
| 259 | { |
| 260 | return ns->ctrl->state == NVME_CTRL_LIVE && |
| 261 | ns->ana_state == NVME_ANA_OPTIMIZED; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head) |
| 265 | { |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 266 | int node = numa_node_id(); |
| 267 | struct nvme_ns *ns; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 268 | |
Christoph Hellwig | f333444 | 2018-09-11 09:51:29 +0200 | [diff] [blame] | 269 | ns = srcu_dereference(head->current_path[node], &head->srcu); |
Hannes Reinecke | fbd6a42 | 2020-07-27 18:08:03 +0200 | [diff] [blame] | 270 | if (unlikely(!ns)) |
| 271 | return __nvme_find_path(head, node); |
| 272 | |
| 273 | if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR) |
| 274 | return nvme_round_robin_path(head, node, ns); |
| 275 | if (unlikely(!nvme_path_is_optimized(ns))) |
| 276 | return __nvme_find_path(head, node); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 277 | return ns; |
| 278 | } |
| 279 | |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 280 | static bool nvme_available_path(struct nvme_ns_head *head) |
| 281 | { |
| 282 | struct nvme_ns *ns; |
| 283 | |
| 284 | list_for_each_entry_rcu(ns, &head->list, siblings) { |
Victor Gladkov | 8c4dfea | 2020-11-24 18:34:59 +0000 | [diff] [blame] | 285 | if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags)) |
| 286 | continue; |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 287 | switch (ns->ctrl->state) { |
| 288 | case NVME_CTRL_LIVE: |
| 289 | case NVME_CTRL_RESETTING: |
| 290 | case NVME_CTRL_CONNECTING: |
| 291 | /* fallthru */ |
| 292 | return true; |
| 293 | default: |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | return false; |
| 298 | } |
| 299 | |
Christoph Hellwig | 1496bd4 | 2021-04-07 14:22:12 +0200 | [diff] [blame] | 300 | static blk_qc_t nvme_ns_head_submit_bio(struct bio *bio) |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 301 | { |
Christoph Hellwig | 309dca30 | 2021-01-24 11:02:34 +0100 | [diff] [blame] | 302 | struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 303 | struct device *dev = disk_to_dev(head->disk); |
| 304 | struct nvme_ns *ns; |
| 305 | blk_qc_t ret = BLK_QC_T_NONE; |
| 306 | int srcu_idx; |
| 307 | |
Hannes Reinecke | 525aa5a | 2019-04-30 18:57:09 +0200 | [diff] [blame] | 308 | /* |
Christoph Hellwig | f695ca3 | 2020-07-01 10:59:39 +0200 | [diff] [blame] | 309 | * The namespace might be going away and the bio might be moved to a |
| 310 | * different queue via blk_steal_bios(), so we need to use the bio_split |
| 311 | * pool from the original queue to allocate the bvecs from. |
Hannes Reinecke | 525aa5a | 2019-04-30 18:57:09 +0200 | [diff] [blame] | 312 | */ |
Christoph Hellwig | f695ca3 | 2020-07-01 10:59:39 +0200 | [diff] [blame] | 313 | blk_queue_split(&bio); |
Hannes Reinecke | 525aa5a | 2019-04-30 18:57:09 +0200 | [diff] [blame] | 314 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 315 | srcu_idx = srcu_read_lock(&head->srcu); |
| 316 | ns = nvme_find_path(head); |
| 317 | if (likely(ns)) { |
Christoph Hellwig | a7c7f7b | 2021-01-26 15:33:06 +0100 | [diff] [blame] | 318 | bio_set_dev(bio, ns->disk->part0); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 319 | bio->bi_opf |= REQ_NVME_MPATH; |
Christoph Hellwig | 1c02fca | 2020-12-03 17:21:38 +0100 | [diff] [blame] | 320 | trace_block_bio_remap(bio, disk_devt(ns->head->disk), |
Hannes Reinecke | 2796b56 | 2018-06-07 10:38:47 +0200 | [diff] [blame] | 321 | bio->bi_iter.bi_sector); |
Christoph Hellwig | 5a6c35f | 2020-07-01 10:59:47 +0200 | [diff] [blame] | 322 | ret = submit_bio_noacct(bio); |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 323 | } else if (nvme_available_path(head)) { |
| 324 | dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n"); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 325 | |
| 326 | spin_lock_irq(&head->requeue_lock); |
| 327 | bio_list_add(&head->requeue_list, bio); |
| 328 | spin_unlock_irq(&head->requeue_lock); |
| 329 | } else { |
Sagi Grimberg | 0157ec8 | 2019-07-25 11:56:57 -0700 | [diff] [blame] | 330 | dev_warn_ratelimited(dev, "no available path - failing I/O\n"); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 331 | |
| 332 | bio->bi_status = BLK_STS_IOERR; |
| 333 | bio_endio(bio); |
| 334 | } |
| 335 | |
| 336 | srcu_read_unlock(&head->srcu, srcu_idx); |
| 337 | return ret; |
| 338 | } |
| 339 | |
Christoph Hellwig | 1496bd4 | 2021-04-07 14:22:12 +0200 | [diff] [blame] | 340 | static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode) |
| 341 | { |
| 342 | if (!nvme_tryget_ns_head(bdev->bd_disk->private_data)) |
| 343 | return -ENXIO; |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode) |
| 348 | { |
| 349 | nvme_put_ns_head(disk->private_data); |
| 350 | } |
| 351 | |
Christoph Hellwig | 8b4fb0f | 2021-05-19 09:17:06 +0200 | [diff] [blame] | 352 | #ifdef CONFIG_BLK_DEV_ZONED |
| 353 | static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector, |
| 354 | unsigned int nr_zones, report_zones_cb cb, void *data) |
| 355 | { |
| 356 | struct nvme_ns_head *head = disk->private_data; |
| 357 | struct nvme_ns *ns; |
| 358 | int srcu_idx, ret = -EWOULDBLOCK; |
| 359 | |
| 360 | srcu_idx = srcu_read_lock(&head->srcu); |
| 361 | ns = nvme_find_path(head); |
| 362 | if (ns) |
| 363 | ret = nvme_ns_report_zones(ns, sector, nr_zones, cb, data); |
| 364 | srcu_read_unlock(&head->srcu, srcu_idx); |
| 365 | return ret; |
| 366 | } |
| 367 | #else |
| 368 | #define nvme_ns_head_report_zones NULL |
| 369 | #endif /* CONFIG_BLK_DEV_ZONED */ |
| 370 | |
Christoph Hellwig | 1496bd4 | 2021-04-07 14:22:12 +0200 | [diff] [blame] | 371 | const struct block_device_operations nvme_ns_head_ops = { |
| 372 | .owner = THIS_MODULE, |
| 373 | .submit_bio = nvme_ns_head_submit_bio, |
| 374 | .open = nvme_ns_head_open, |
| 375 | .release = nvme_ns_head_release, |
| 376 | .ioctl = nvme_ns_head_ioctl, |
| 377 | .getgeo = nvme_getgeo, |
Christoph Hellwig | 8b4fb0f | 2021-05-19 09:17:06 +0200 | [diff] [blame] | 378 | .report_zones = nvme_ns_head_report_zones, |
Christoph Hellwig | 1496bd4 | 2021-04-07 14:22:12 +0200 | [diff] [blame] | 379 | .pr_ops = &nvme_pr_ops, |
| 380 | }; |
| 381 | |
Minwoo Im | 2637bae | 2021-04-21 16:45:04 +0900 | [diff] [blame] | 382 | static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev) |
| 383 | { |
| 384 | return container_of(cdev, struct nvme_ns_head, cdev); |
| 385 | } |
| 386 | |
| 387 | static int nvme_ns_head_chr_open(struct inode *inode, struct file *file) |
| 388 | { |
| 389 | if (!nvme_tryget_ns_head(cdev_to_ns_head(inode->i_cdev))) |
| 390 | return -ENXIO; |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static int nvme_ns_head_chr_release(struct inode *inode, struct file *file) |
| 395 | { |
| 396 | nvme_put_ns_head(cdev_to_ns_head(inode->i_cdev)); |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static const struct file_operations nvme_ns_head_chr_fops = { |
| 401 | .owner = THIS_MODULE, |
| 402 | .open = nvme_ns_head_chr_open, |
| 403 | .release = nvme_ns_head_chr_release, |
| 404 | .unlocked_ioctl = nvme_ns_head_chr_ioctl, |
| 405 | .compat_ioctl = compat_ptr_ioctl, |
| 406 | }; |
| 407 | |
| 408 | static int nvme_add_ns_head_cdev(struct nvme_ns_head *head) |
| 409 | { |
| 410 | int ret; |
| 411 | |
| 412 | head->cdev_device.parent = &head->subsys->dev; |
| 413 | ret = dev_set_name(&head->cdev_device, "ng%dn%d", |
| 414 | head->subsys->instance, head->instance); |
| 415 | if (ret) |
| 416 | return ret; |
| 417 | ret = nvme_cdev_add(&head->cdev, &head->cdev_device, |
| 418 | &nvme_ns_head_chr_fops, THIS_MODULE); |
| 419 | if (ret) |
| 420 | kfree_const(head->cdev_device.kobj.name); |
| 421 | return ret; |
| 422 | } |
| 423 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 424 | static void nvme_requeue_work(struct work_struct *work) |
| 425 | { |
| 426 | struct nvme_ns_head *head = |
| 427 | container_of(work, struct nvme_ns_head, requeue_work); |
| 428 | struct bio *bio, *next; |
| 429 | |
| 430 | spin_lock_irq(&head->requeue_lock); |
| 431 | next = bio_list_get(&head->requeue_list); |
| 432 | spin_unlock_irq(&head->requeue_lock); |
| 433 | |
| 434 | while ((bio = next) != NULL) { |
| 435 | next = bio->bi_next; |
| 436 | bio->bi_next = NULL; |
| 437 | |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 438 | submit_bio_noacct(bio); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head) |
| 443 | { |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 444 | bool vwc = false; |
| 445 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 446 | mutex_init(&head->lock); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 447 | bio_list_init(&head->requeue_list); |
| 448 | spin_lock_init(&head->requeue_lock); |
| 449 | INIT_WORK(&head->requeue_work, nvme_requeue_work); |
| 450 | |
| 451 | /* |
| 452 | * Add a multipath node if the subsystems supports multiple controllers. |
| 453 | * We also do this for private namespaces as the namespace sharing data could |
| 454 | * change after a rescan. |
| 455 | */ |
Keith Busch | 92decf1 | 2020-04-03 10:53:46 -0700 | [diff] [blame] | 456 | if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || !multipath) |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 457 | return 0; |
| 458 | |
Christoph Hellwig | f165fb8 | 2021-05-21 07:51:08 +0200 | [diff] [blame] | 459 | head->disk = blk_alloc_disk(ctrl->numa_node); |
| 460 | if (!head->disk) |
| 461 | return -ENOMEM; |
| 462 | head->disk->fops = &nvme_ns_head_ops; |
| 463 | head->disk->private_data = head; |
| 464 | sprintf(head->disk->disk_name, "nvme%dn%d", |
| 465 | ctrl->subsys->instance, head->instance); |
| 466 | |
| 467 | blk_queue_flag_set(QUEUE_FLAG_NONROT, head->disk->queue); |
| 468 | /* set to a default value of 512 until the disk is validated */ |
| 469 | blk_queue_logical_block_size(head->disk->queue, 512); |
| 470 | blk_set_stacking_limits(&head->disk->queue->limits); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 471 | |
| 472 | /* we need to propagate up the VMC settings */ |
| 473 | if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) |
| 474 | vwc = true; |
Christoph Hellwig | f165fb8 | 2021-05-21 07:51:08 +0200 | [diff] [blame] | 475 | blk_queue_write_cache(head->disk->queue, vwc, vwc); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 476 | return 0; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 479 | static void nvme_mpath_set_live(struct nvme_ns *ns) |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 480 | { |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 481 | struct nvme_ns_head *head = ns->head; |
| 482 | |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 483 | if (!head->disk) |
| 484 | return; |
Baegjae Sung | 9bd82b1 | 2018-02-28 16:06:04 +0900 | [diff] [blame] | 485 | |
Minwoo Im | 2637bae | 2021-04-21 16:45:04 +0900 | [diff] [blame] | 486 | if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { |
Hannes Reinecke | 33b14f67 | 2018-09-28 08:17:20 +0200 | [diff] [blame] | 487 | device_add_disk(&head->subsys->dev, head->disk, |
| 488 | nvme_ns_id_attr_groups); |
Minwoo Im | 2637bae | 2021-04-21 16:45:04 +0900 | [diff] [blame] | 489 | nvme_add_ns_head_cdev(head); |
| 490 | } |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 491 | |
Anton Eidelman | d8a22f8 | 2020-06-24 01:53:11 -0700 | [diff] [blame] | 492 | mutex_lock(&head->lock); |
Keith Busch | 886fabf | 2018-10-05 09:49:37 -0600 | [diff] [blame] | 493 | if (nvme_path_is_optimized(ns)) { |
| 494 | int node, srcu_idx; |
| 495 | |
| 496 | srcu_idx = srcu_read_lock(&head->srcu); |
| 497 | for_each_node(node) |
| 498 | __nvme_find_path(head, node); |
| 499 | srcu_read_unlock(&head->srcu, srcu_idx); |
| 500 | } |
Sagi Grimberg | e164471 | 2020-06-24 01:53:10 -0700 | [diff] [blame] | 501 | mutex_unlock(&head->lock); |
Keith Busch | 886fabf | 2018-10-05 09:49:37 -0600 | [diff] [blame] | 502 | |
Sagi Grimberg | e164471 | 2020-06-24 01:53:10 -0700 | [diff] [blame] | 503 | synchronize_srcu(&head->srcu); |
| 504 | kblockd_schedule_work(&head->requeue_work); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data, |
| 508 | int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *, |
| 509 | void *)) |
| 510 | { |
| 511 | void *base = ctrl->ana_log_buf; |
| 512 | size_t offset = sizeof(struct nvme_ana_rsp_hdr); |
| 513 | int error, i; |
| 514 | |
| 515 | lockdep_assert_held(&ctrl->ana_lock); |
| 516 | |
| 517 | for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) { |
| 518 | struct nvme_ana_group_desc *desc = base + offset; |
Prabhath Sajeepa | 64fab72 | 2019-10-28 16:56:48 -0600 | [diff] [blame] | 519 | u32 nr_nsids; |
| 520 | size_t nsid_buf_size; |
| 521 | |
| 522 | if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc))) |
| 523 | return -EINVAL; |
| 524 | |
| 525 | nr_nsids = le32_to_cpu(desc->nnsids); |
| 526 | nsid_buf_size = nr_nsids * sizeof(__le32); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 527 | |
| 528 | if (WARN_ON_ONCE(desc->grpid == 0)) |
| 529 | return -EINVAL; |
| 530 | if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax)) |
| 531 | return -EINVAL; |
| 532 | if (WARN_ON_ONCE(desc->state == 0)) |
| 533 | return -EINVAL; |
| 534 | if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE)) |
| 535 | return -EINVAL; |
| 536 | |
| 537 | offset += sizeof(*desc); |
| 538 | if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size)) |
| 539 | return -EINVAL; |
| 540 | |
| 541 | error = cb(ctrl, desc, data); |
| 542 | if (error) |
| 543 | return error; |
| 544 | |
| 545 | offset += nsid_buf_size; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static inline bool nvme_state_is_live(enum nvme_ana_state state) |
| 552 | { |
| 553 | return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED; |
| 554 | } |
| 555 | |
| 556 | static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc, |
| 557 | struct nvme_ns *ns) |
| 558 | { |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 559 | ns->ana_grpid = le32_to_cpu(desc->grpid); |
| 560 | ns->ana_state = desc->state; |
| 561 | clear_bit(NVME_NS_ANA_PENDING, &ns->flags); |
| 562 | |
Martin George | cc2278c | 2019-03-27 09:52:56 +0100 | [diff] [blame] | 563 | if (nvme_state_is_live(ns->ana_state)) |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 564 | nvme_mpath_set_live(ns); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | static int nvme_update_ana_state(struct nvme_ctrl *ctrl, |
| 568 | struct nvme_ana_group_desc *desc, void *data) |
| 569 | { |
| 570 | u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0; |
| 571 | unsigned *nr_change_groups = data; |
| 572 | struct nvme_ns *ns; |
| 573 | |
Hannes Reinecke | 592b6e7 | 2019-04-28 20:24:42 -0700 | [diff] [blame] | 574 | dev_dbg(ctrl->device, "ANA group %d: %s.\n", |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 575 | le32_to_cpu(desc->grpid), |
| 576 | nvme_ana_state_names[desc->state]); |
| 577 | |
| 578 | if (desc->state == NVME_ANA_CHANGE) |
| 579 | (*nr_change_groups)++; |
| 580 | |
| 581 | if (!nr_nsids) |
| 582 | return 0; |
| 583 | |
Sagi Grimberg | 657f197 | 2020-04-02 09:34:54 -0700 | [diff] [blame] | 584 | down_read(&ctrl->namespaces_rwsem); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 585 | list_for_each_entry(ns, &ctrl->namespaces, list) { |
Anton Eidelman | e01f91d | 2019-08-16 13:00:10 -0700 | [diff] [blame] | 586 | unsigned nsid = le32_to_cpu(desc->nsids[n]); |
| 587 | |
| 588 | if (ns->head->ns_id < nsid) |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 589 | continue; |
Anton Eidelman | e01f91d | 2019-08-16 13:00:10 -0700 | [diff] [blame] | 590 | if (ns->head->ns_id == nsid) |
| 591 | nvme_update_ns_ana_state(desc, ns); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 592 | if (++n == nr_nsids) |
| 593 | break; |
| 594 | } |
Sagi Grimberg | 657f197 | 2020-04-02 09:34:54 -0700 | [diff] [blame] | 595 | up_read(&ctrl->namespaces_rwsem); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
Anton Eidelman | 86cccfb | 2019-10-18 11:32:51 -0700 | [diff] [blame] | 599 | static int nvme_read_ana_log(struct nvme_ctrl *ctrl) |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 600 | { |
| 601 | u32 nr_change_groups = 0; |
| 602 | int error; |
| 603 | |
| 604 | mutex_lock(&ctrl->ana_lock); |
Keith Busch | be93e87 | 2020-06-29 12:06:40 -0700 | [diff] [blame] | 605 | error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM, |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 606 | ctrl->ana_log_buf, ctrl->ana_log_size, 0); |
| 607 | if (error) { |
| 608 | dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error); |
| 609 | goto out_unlock; |
| 610 | } |
| 611 | |
| 612 | error = nvme_parse_ana_log(ctrl, &nr_change_groups, |
| 613 | nvme_update_ana_state); |
| 614 | if (error) |
| 615 | goto out_unlock; |
| 616 | |
| 617 | /* |
| 618 | * In theory we should have an ANATT timer per group as they might enter |
| 619 | * the change state at different times. But that is a lot of overhead |
| 620 | * just to protect against a target that keeps entering new changes |
| 621 | * states while never finishing previous ones. But we'll still |
| 622 | * eventually time out once all groups are in change state, so this |
| 623 | * isn't a big deal. |
| 624 | * |
| 625 | * We also double the ANATT value to provide some slack for transports |
| 626 | * or AEN processing overhead. |
| 627 | */ |
| 628 | if (nr_change_groups) |
| 629 | mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies); |
| 630 | else |
| 631 | del_timer_sync(&ctrl->anatt_timer); |
| 632 | out_unlock: |
| 633 | mutex_unlock(&ctrl->ana_lock); |
| 634 | return error; |
| 635 | } |
| 636 | |
| 637 | static void nvme_ana_work(struct work_struct *work) |
| 638 | { |
| 639 | struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work); |
| 640 | |
Sagi Grimberg | ecca390e | 2020-07-22 16:32:19 -0700 | [diff] [blame] | 641 | if (ctrl->state != NVME_CTRL_LIVE) |
| 642 | return; |
| 643 | |
Anton Eidelman | 86cccfb | 2019-10-18 11:32:51 -0700 | [diff] [blame] | 644 | nvme_read_ana_log(ctrl); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static void nvme_anatt_timeout(struct timer_list *t) |
| 648 | { |
| 649 | struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer); |
| 650 | |
| 651 | dev_info(ctrl->device, "ANATT timeout, resetting controller.\n"); |
| 652 | nvme_reset_ctrl(ctrl); |
| 653 | } |
| 654 | |
| 655 | void nvme_mpath_stop(struct nvme_ctrl *ctrl) |
| 656 | { |
| 657 | if (!nvme_ctrl_use_ana(ctrl)) |
| 658 | return; |
| 659 | del_timer_sync(&ctrl->anatt_timer); |
| 660 | cancel_work_sync(&ctrl->ana_work); |
| 661 | } |
| 662 | |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 663 | #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \ |
| 664 | struct device_attribute subsys_attr_##_name = \ |
| 665 | __ATTR(_name, _mode, _show, _store) |
| 666 | |
| 667 | static const char *nvme_iopolicy_names[] = { |
| 668 | [NVME_IOPOLICY_NUMA] = "numa", |
| 669 | [NVME_IOPOLICY_RR] = "round-robin", |
| 670 | }; |
| 671 | |
| 672 | static ssize_t nvme_subsys_iopolicy_show(struct device *dev, |
| 673 | struct device_attribute *attr, char *buf) |
| 674 | { |
| 675 | struct nvme_subsystem *subsys = |
| 676 | container_of(dev, struct nvme_subsystem, dev); |
| 677 | |
Daniel Wagner | bff4bcf | 2021-04-01 11:54:10 +0200 | [diff] [blame] | 678 | return sysfs_emit(buf, "%s\n", |
| 679 | nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]); |
Hannes Reinecke | 75c10e7 | 2019-02-18 11:43:26 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static ssize_t nvme_subsys_iopolicy_store(struct device *dev, |
| 683 | struct device_attribute *attr, const char *buf, size_t count) |
| 684 | { |
| 685 | struct nvme_subsystem *subsys = |
| 686 | container_of(dev, struct nvme_subsystem, dev); |
| 687 | int i; |
| 688 | |
| 689 | for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) { |
| 690 | if (sysfs_streq(buf, nvme_iopolicy_names[i])) { |
| 691 | WRITE_ONCE(subsys->iopolicy, i); |
| 692 | return count; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | return -EINVAL; |
| 697 | } |
| 698 | SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR, |
| 699 | nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store); |
| 700 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 701 | static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr, |
| 702 | char *buf) |
| 703 | { |
Daniel Wagner | bff4bcf | 2021-04-01 11:54:10 +0200 | [diff] [blame] | 704 | return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 705 | } |
| 706 | DEVICE_ATTR_RO(ana_grpid); |
| 707 | |
| 708 | static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr, |
| 709 | char *buf) |
| 710 | { |
| 711 | struct nvme_ns *ns = nvme_get_ns_from_dev(dev); |
| 712 | |
Daniel Wagner | bff4bcf | 2021-04-01 11:54:10 +0200 | [diff] [blame] | 713 | return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 714 | } |
| 715 | DEVICE_ATTR_RO(ana_state); |
| 716 | |
Anton Eidelman | 489dd10 | 2020-06-24 01:53:09 -0700 | [diff] [blame] | 717 | static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl, |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 718 | struct nvme_ana_group_desc *desc, void *data) |
| 719 | { |
Anton Eidelman | 489dd10 | 2020-06-24 01:53:09 -0700 | [diff] [blame] | 720 | struct nvme_ana_group_desc *dst = data; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 721 | |
Anton Eidelman | 489dd10 | 2020-06-24 01:53:09 -0700 | [diff] [blame] | 722 | if (desc->grpid != dst->grpid) |
| 723 | return 0; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 724 | |
Anton Eidelman | 489dd10 | 2020-06-24 01:53:09 -0700 | [diff] [blame] | 725 | *dst = *desc; |
| 726 | return -ENXIO; /* just break out of the loop */ |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id) |
| 730 | { |
| 731 | if (nvme_ctrl_use_ana(ns->ctrl)) { |
Anton Eidelman | 489dd10 | 2020-06-24 01:53:09 -0700 | [diff] [blame] | 732 | struct nvme_ana_group_desc desc = { |
| 733 | .grpid = id->anagrpid, |
| 734 | .state = 0, |
| 735 | }; |
| 736 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 737 | mutex_lock(&ns->ctrl->ana_lock); |
| 738 | ns->ana_grpid = le32_to_cpu(id->anagrpid); |
Anton Eidelman | 489dd10 | 2020-06-24 01:53:09 -0700 | [diff] [blame] | 739 | nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 740 | mutex_unlock(&ns->ctrl->ana_lock); |
Anton Eidelman | 489dd10 | 2020-06-24 01:53:09 -0700 | [diff] [blame] | 741 | if (desc.state) { |
| 742 | /* found the group desc: update */ |
| 743 | nvme_update_ns_ana_state(&desc, ns); |
Hannes Reinecke | dd8f7fa | 2020-12-05 16:29:01 +0100 | [diff] [blame] | 744 | } else { |
| 745 | /* group desc not found: trigger a re-read */ |
| 746 | set_bit(NVME_NS_ANA_PENDING, &ns->flags); |
| 747 | queue_work(nvme_wq, &ns->ctrl->ana_work); |
Anton Eidelman | 489dd10 | 2020-06-24 01:53:09 -0700 | [diff] [blame] | 748 | } |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 749 | } else { |
Niklas Cassel | e234f1f | 2021-04-10 20:15:45 +0000 | [diff] [blame] | 750 | ns->ana_state = NVME_ANA_OPTIMIZED; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 751 | nvme_mpath_set_live(ns); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 752 | } |
Keith Busch | b2ce4d9 | 2020-04-09 09:09:04 -0700 | [diff] [blame] | 753 | |
Christoph Hellwig | 1cb039f | 2020-09-24 08:51:38 +0200 | [diff] [blame] | 754 | if (blk_queue_stable_writes(ns->queue) && ns->head->disk) |
| 755 | blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, |
| 756 | ns->head->disk->queue); |
Keith Busch | 73a1a22 | 2021-02-05 11:50:02 -0800 | [diff] [blame] | 757 | #ifdef CONFIG_BLK_DEV_ZONED |
| 758 | if (blk_queue_is_zoned(ns->queue) && ns->head->disk) |
| 759 | ns->head->disk->queue->nr_zones = ns->queue->nr_zones; |
| 760 | #endif |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Hannes Reinecke | 5396fda | 2021-07-16 13:30:35 +0200 | [diff] [blame] | 763 | void nvme_mpath_shutdown_disk(struct nvme_ns_head *head) |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 764 | { |
| 765 | if (!head->disk) |
| 766 | return; |
Hannes Reinecke | 5396fda | 2021-07-16 13:30:35 +0200 | [diff] [blame] | 767 | kblockd_schedule_work(&head->requeue_work); |
Christoph Hellwig | 916a470 | 2021-08-09 08:40:24 +0200 | [diff] [blame^] | 768 | if (test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { |
Minwoo Im | 2637bae | 2021-04-21 16:45:04 +0900 | [diff] [blame] | 769 | nvme_cdev_del(&head->cdev, &head->cdev_device); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 770 | del_gendisk(head->disk); |
Minwoo Im | 2637bae | 2021-04-21 16:45:04 +0900 | [diff] [blame] | 771 | } |
Hannes Reinecke | 5396fda | 2021-07-16 13:30:35 +0200 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | void nvme_mpath_remove_disk(struct nvme_ns_head *head) |
| 775 | { |
| 776 | if (!head->disk) |
| 777 | return; |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 778 | blk_set_queue_dying(head->disk->queue); |
| 779 | /* make sure all pending bios are cleaned up */ |
| 780 | kblockd_schedule_work(&head->requeue_work); |
| 781 | flush_work(&head->requeue_work); |
Christoph Hellwig | f165fb8 | 2021-05-21 07:51:08 +0200 | [diff] [blame] | 782 | blk_cleanup_disk(head->disk); |
Christoph Hellwig | 32acab3 | 2017-11-02 12:59:30 +0100 | [diff] [blame] | 783 | } |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 784 | |
Christoph Hellwig | 5e1f689 | 2021-04-29 14:18:53 +0200 | [diff] [blame] | 785 | void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl) |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 786 | { |
Christoph Hellwig | 5e1f689 | 2021-04-29 14:18:53 +0200 | [diff] [blame] | 787 | mutex_init(&ctrl->ana_lock); |
| 788 | timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0); |
| 789 | INIT_WORK(&ctrl->ana_work, nvme_ana_work); |
| 790 | } |
| 791 | |
| 792 | int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) |
| 793 | { |
| 794 | size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT; |
| 795 | size_t ana_log_size; |
| 796 | int error = 0; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 797 | |
Marta Rybczynska | 66b20ac | 2019-07-23 07:41:20 +0200 | [diff] [blame] | 798 | /* check if multipath is enabled and we have the capability */ |
Keith Busch | 92decf1 | 2020-04-03 10:53:46 -0700 | [diff] [blame] | 799 | if (!multipath || !ctrl->subsys || |
| 800 | !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA)) |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 801 | return 0; |
| 802 | |
Daniel Wagner | 120bb36 | 2021-06-07 10:56:56 +0200 | [diff] [blame] | 803 | if (!ctrl->max_namespaces || |
| 804 | ctrl->max_namespaces > le32_to_cpu(id->nn)) { |
| 805 | dev_err(ctrl->device, |
| 806 | "Invalid MNAN value %u\n", ctrl->max_namespaces); |
| 807 | return -EINVAL; |
| 808 | } |
| 809 | |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 810 | ctrl->anacap = id->anacap; |
| 811 | ctrl->anatt = id->anatt; |
| 812 | ctrl->nanagrpid = le32_to_cpu(id->nanagrpid); |
| 813 | ctrl->anagrpmax = le32_to_cpu(id->anagrpmax); |
| 814 | |
Christoph Hellwig | 5e1f689 | 2021-04-29 14:18:53 +0200 | [diff] [blame] | 815 | ana_log_size = sizeof(struct nvme_ana_rsp_hdr) + |
| 816 | ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) + |
| 817 | ctrl->max_namespaces * sizeof(__le32); |
| 818 | if (ana_log_size > max_transfer_size) { |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 819 | dev_err(ctrl->device, |
Christoph Hellwig | 5e1f689 | 2021-04-29 14:18:53 +0200 | [diff] [blame] | 820 | "ANA log page size (%zd) larger than MDTS (%zd).\n", |
| 821 | ana_log_size, max_transfer_size); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 822 | dev_err(ctrl->device, "disabling ANA support.\n"); |
Christoph Hellwig | 5e1f689 | 2021-04-29 14:18:53 +0200 | [diff] [blame] | 823 | goto out_uninit; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 824 | } |
Christoph Hellwig | 5e1f689 | 2021-04-29 14:18:53 +0200 | [diff] [blame] | 825 | if (ana_log_size > ctrl->ana_log_size) { |
| 826 | nvme_mpath_stop(ctrl); |
| 827 | kfree(ctrl->ana_log_buf); |
Hou Pu | e181811 | 2021-05-13 21:04:10 +0800 | [diff] [blame] | 828 | ctrl->ana_log_buf = kmalloc(ana_log_size, GFP_KERNEL); |
Christoph Hellwig | 5e1f689 | 2021-04-29 14:18:53 +0200 | [diff] [blame] | 829 | if (!ctrl->ana_log_buf) |
| 830 | return -ENOMEM; |
Susobhan Dey | bb830ad | 2018-09-25 12:29:15 -0700 | [diff] [blame] | 831 | } |
Christoph Hellwig | 5e1f689 | 2021-04-29 14:18:53 +0200 | [diff] [blame] | 832 | ctrl->ana_log_size = ana_log_size; |
Anton Eidelman | 86cccfb | 2019-10-18 11:32:51 -0700 | [diff] [blame] | 833 | error = nvme_read_ana_log(ctrl); |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 834 | if (error) |
Christoph Hellwig | 5e1f689 | 2021-04-29 14:18:53 +0200 | [diff] [blame] | 835 | goto out_uninit; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 836 | return 0; |
Christoph Hellwig | 5e1f689 | 2021-04-29 14:18:53 +0200 | [diff] [blame] | 837 | |
| 838 | out_uninit: |
| 839 | nvme_mpath_uninit(ctrl); |
Susobhan Dey | bb830ad | 2018-09-25 12:29:15 -0700 | [diff] [blame] | 840 | return error; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | void nvme_mpath_uninit(struct nvme_ctrl *ctrl) |
| 844 | { |
| 845 | kfree(ctrl->ana_log_buf); |
Hannes Reinecke | c7055fd | 2019-01-08 12:46:58 +0100 | [diff] [blame] | 846 | ctrl->ana_log_buf = NULL; |
Christoph Hellwig | 0d0b660 | 2018-05-14 08:48:54 +0200 | [diff] [blame] | 847 | } |