1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2017-2018 Christoph Hellwig.
4 */
5
6 #include <linux/backing-dev.h>
7 #include <linux/moduleparam.h>
8 #include <trace/events/block.h>
9 #include "nvme.h"
10
11 static bool multipath = true;
12 module_param(multipath, bool, 0444);
13 MODULE_PARM_DESC(multipath,
14 "turn on native support for multiple controllers per subsystem");
15
nvme_mpath_unfreeze(struct nvme_subsystem * subsys)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
nvme_mpath_wait_freeze(struct nvme_subsystem * subsys)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
nvme_mpath_start_freeze(struct nvme_subsystem * subsys)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
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 */
nvme_set_disk_name(char * disk_name,struct nvme_ns * ns,struct nvme_ctrl * ctrl,int * flags)53 void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
54 struct nvme_ctrl *ctrl, int *flags)
55 {
56 if (!multipath) {
57 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
58 } else if (ns->head->disk) {
59 sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
60 ctrl->instance, ns->head->instance);
61 *flags = GENHD_FL_HIDDEN;
62 } else {
63 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
64 ns->head->instance);
65 }
66 }
67
nvme_failover_req(struct request * req)68 void nvme_failover_req(struct request *req)
69 {
70 struct nvme_ns *ns = req->q->queuedata;
71 u16 status = nvme_req(req)->status & 0x7ff;
72 unsigned long flags;
73
74 nvme_mpath_clear_current_path(ns);
75
76 /*
77 * If we got back an ANA error, we know the controller is alive but not
78 * ready to serve this namespace. Kick of a re-read of the ANA
79 * information page, and just try any other available path for now.
80 */
81 if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) {
82 set_bit(NVME_NS_ANA_PENDING, &ns->flags);
83 queue_work(nvme_wq, &ns->ctrl->ana_work);
84 }
85
86 spin_lock_irqsave(&ns->head->requeue_lock, flags);
87 blk_steal_bios(&ns->head->requeue_list, req);
88 spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
89
90 blk_mq_end_request(req, 0);
91 kblockd_schedule_work(&ns->head->requeue_work);
92 }
93
nvme_kick_requeue_lists(struct nvme_ctrl * ctrl)94 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
95 {
96 struct nvme_ns *ns;
97
98 down_read(&ctrl->namespaces_rwsem);
99 list_for_each_entry(ns, &ctrl->namespaces, list) {
100 if (ns->head->disk)
101 kblockd_schedule_work(&ns->head->requeue_work);
102 }
103 up_read(&ctrl->namespaces_rwsem);
104 }
105
106 static const char *nvme_ana_state_names[] = {
107 [0] = "invalid state",
108 [NVME_ANA_OPTIMIZED] = "optimized",
109 [NVME_ANA_NONOPTIMIZED] = "non-optimized",
110 [NVME_ANA_INACCESSIBLE] = "inaccessible",
111 [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss",
112 [NVME_ANA_CHANGE] = "change",
113 };
114
nvme_mpath_clear_current_path(struct nvme_ns * ns)115 bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
116 {
117 struct nvme_ns_head *head = ns->head;
118 bool changed = false;
119 int node;
120
121 if (!head)
122 goto out;
123
124 for_each_node(node) {
125 if (ns == rcu_access_pointer(head->current_path[node])) {
126 rcu_assign_pointer(head->current_path[node], NULL);
127 changed = true;
128 }
129 }
130 out:
131 return changed;
132 }
133
nvme_mpath_clear_ctrl_paths(struct nvme_ctrl * ctrl)134 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
135 {
136 struct nvme_ns *ns;
137
138 mutex_lock(&ctrl->scan_lock);
139 down_read(&ctrl->namespaces_rwsem);
140 list_for_each_entry(ns, &ctrl->namespaces, list)
141 if (nvme_mpath_clear_current_path(ns))
142 kblockd_schedule_work(&ns->head->requeue_work);
143 up_read(&ctrl->namespaces_rwsem);
144 mutex_unlock(&ctrl->scan_lock);
145 }
146
nvme_path_is_disabled(struct nvme_ns * ns)147 static bool nvme_path_is_disabled(struct nvme_ns *ns)
148 {
149 /*
150 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
151 * still be able to complete assuming that the controller is connected.
152 * Otherwise it will fail immediately and return to the requeue list.
153 */
154 if (ns->ctrl->state != NVME_CTRL_LIVE &&
155 ns->ctrl->state != NVME_CTRL_DELETING)
156 return true;
157 if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
158 test_bit(NVME_NS_REMOVING, &ns->flags))
159 return true;
160 return false;
161 }
162
__nvme_find_path(struct nvme_ns_head * head,int node)163 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
164 {
165 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
166 struct nvme_ns *found = NULL, *fallback = NULL, *ns;
167
168 list_for_each_entry_rcu(ns, &head->list, siblings) {
169 if (nvme_path_is_disabled(ns))
170 continue;
171
172 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
173 distance = node_distance(node, ns->ctrl->numa_node);
174 else
175 distance = LOCAL_DISTANCE;
176
177 switch (ns->ana_state) {
178 case NVME_ANA_OPTIMIZED:
179 if (distance < found_distance) {
180 found_distance = distance;
181 found = ns;
182 }
183 break;
184 case NVME_ANA_NONOPTIMIZED:
185 if (distance < fallback_distance) {
186 fallback_distance = distance;
187 fallback = ns;
188 }
189 break;
190 default:
191 break;
192 }
193 }
194
195 if (!found)
196 found = fallback;
197 if (found)
198 rcu_assign_pointer(head->current_path[node], found);
199 return found;
200 }
201
nvme_next_ns(struct nvme_ns_head * head,struct nvme_ns * ns)202 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
203 struct nvme_ns *ns)
204 {
205 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
206 siblings);
207 if (ns)
208 return ns;
209 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
210 }
211
nvme_round_robin_path(struct nvme_ns_head * head,int node,struct nvme_ns * old)212 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
213 int node, struct nvme_ns *old)
214 {
215 struct nvme_ns *ns, *found = NULL;
216
217 if (list_is_singular(&head->list)) {
218 if (nvme_path_is_disabled(old))
219 return NULL;
220 return old;
221 }
222
223 for (ns = nvme_next_ns(head, old);
224 ns && ns != old;
225 ns = nvme_next_ns(head, ns)) {
226 if (nvme_path_is_disabled(ns))
227 continue;
228
229 if (ns->ana_state == NVME_ANA_OPTIMIZED) {
230 found = ns;
231 goto out;
232 }
233 if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
234 found = ns;
235 }
236
237 /*
238 * The loop above skips the current path for round-robin semantics.
239 * Fall back to the current path if either:
240 * - no other optimized path found and current is optimized,
241 * - no other usable path found and current is usable.
242 */
243 if (!nvme_path_is_disabled(old) &&
244 (old->ana_state == NVME_ANA_OPTIMIZED ||
245 (!found && old->ana_state == NVME_ANA_NONOPTIMIZED)))
246 return old;
247
248 if (!found)
249 return NULL;
250 out:
251 rcu_assign_pointer(head->current_path[node], found);
252 return found;
253 }
254
nvme_path_is_optimized(struct nvme_ns * ns)255 static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
256 {
257 return ns->ctrl->state == NVME_CTRL_LIVE &&
258 ns->ana_state == NVME_ANA_OPTIMIZED;
259 }
260
nvme_find_path(struct nvme_ns_head * head)261 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
262 {
263 int node = numa_node_id();
264 struct nvme_ns *ns;
265
266 ns = srcu_dereference(head->current_path[node], &head->srcu);
267 if (unlikely(!ns))
268 return __nvme_find_path(head, node);
269
270 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR)
271 return nvme_round_robin_path(head, node, ns);
272 if (unlikely(!nvme_path_is_optimized(ns)))
273 return __nvme_find_path(head, node);
274 return ns;
275 }
276
nvme_available_path(struct nvme_ns_head * head)277 static bool nvme_available_path(struct nvme_ns_head *head)
278 {
279 struct nvme_ns *ns;
280
281 list_for_each_entry_rcu(ns, &head->list, siblings) {
282 switch (ns->ctrl->state) {
283 case NVME_CTRL_LIVE:
284 case NVME_CTRL_RESETTING:
285 case NVME_CTRL_CONNECTING:
286 /* fallthru */
287 return true;
288 default:
289 break;
290 }
291 }
292 return false;
293 }
294
nvme_ns_head_submit_bio(struct bio * bio)295 blk_qc_t nvme_ns_head_submit_bio(struct bio *bio)
296 {
297 struct nvme_ns_head *head = bio->bi_disk->private_data;
298 struct device *dev = disk_to_dev(head->disk);
299 struct nvme_ns *ns;
300 blk_qc_t ret = BLK_QC_T_NONE;
301 int srcu_idx;
302
303 /*
304 * The namespace might be going away and the bio might be moved to a
305 * different queue via blk_steal_bios(), so we need to use the bio_split
306 * pool from the original queue to allocate the bvecs from.
307 */
308 blk_queue_split(&bio);
309
310 srcu_idx = srcu_read_lock(&head->srcu);
311 ns = nvme_find_path(head);
312 if (likely(ns)) {
313 bio->bi_disk = ns->disk;
314 bio->bi_opf |= REQ_NVME_MPATH;
315 trace_block_bio_remap(bio->bi_disk->queue, bio,
316 disk_devt(ns->head->disk),
317 bio->bi_iter.bi_sector);
318 ret = submit_bio_noacct(bio);
319 } else if (nvme_available_path(head)) {
320 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
321
322 spin_lock_irq(&head->requeue_lock);
323 bio_list_add(&head->requeue_list, bio);
324 spin_unlock_irq(&head->requeue_lock);
325 } else {
326 dev_warn_ratelimited(dev, "no available path - failing I/O\n");
327
328 bio->bi_status = BLK_STS_IOERR;
329 bio_endio(bio);
330 }
331
332 srcu_read_unlock(&head->srcu, srcu_idx);
333 return ret;
334 }
335
nvme_requeue_work(struct work_struct * work)336 static void nvme_requeue_work(struct work_struct *work)
337 {
338 struct nvme_ns_head *head =
339 container_of(work, struct nvme_ns_head, requeue_work);
340 struct bio *bio, *next;
341
342 spin_lock_irq(&head->requeue_lock);
343 next = bio_list_get(&head->requeue_list);
344 spin_unlock_irq(&head->requeue_lock);
345
346 while ((bio = next) != NULL) {
347 next = bio->bi_next;
348 bio->bi_next = NULL;
349
350 /*
351 * Reset disk to the mpath node and resubmit to select a new
352 * path.
353 */
354 bio->bi_disk = head->disk;
355 submit_bio_noacct(bio);
356 }
357 }
358
nvme_mpath_alloc_disk(struct nvme_ctrl * ctrl,struct nvme_ns_head * head)359 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
360 {
361 struct request_queue *q;
362 bool vwc = false;
363
364 mutex_init(&head->lock);
365 bio_list_init(&head->requeue_list);
366 spin_lock_init(&head->requeue_lock);
367 INIT_WORK(&head->requeue_work, nvme_requeue_work);
368
369 /*
370 * Add a multipath node if the subsystems supports multiple controllers.
371 * We also do this for private namespaces as the namespace sharing data could
372 * change after a rescan.
373 */
374 if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || !multipath)
375 return 0;
376
377 q = blk_alloc_queue(ctrl->numa_node);
378 if (!q)
379 goto out;
380 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
381 /* set to a default value for 512 until disk is validated */
382 blk_queue_logical_block_size(q, 512);
383 blk_set_stacking_limits(&q->limits);
384
385 /* we need to propagate up the VMC settings */
386 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
387 vwc = true;
388 blk_queue_write_cache(q, vwc, vwc);
389
390 head->disk = alloc_disk(0);
391 if (!head->disk)
392 goto out_cleanup_queue;
393 head->disk->fops = &nvme_ns_head_ops;
394 head->disk->private_data = head;
395 head->disk->queue = q;
396 head->disk->flags = GENHD_FL_EXT_DEVT;
397 sprintf(head->disk->disk_name, "nvme%dn%d",
398 ctrl->subsys->instance, head->instance);
399 return 0;
400
401 out_cleanup_queue:
402 blk_cleanup_queue(q);
403 out:
404 return -ENOMEM;
405 }
406
nvme_mpath_set_live(struct nvme_ns * ns)407 static void nvme_mpath_set_live(struct nvme_ns *ns)
408 {
409 struct nvme_ns_head *head = ns->head;
410
411 if (!head->disk)
412 return;
413
414 if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags))
415 device_add_disk(&head->subsys->dev, head->disk,
416 nvme_ns_id_attr_groups);
417
418 mutex_lock(&head->lock);
419 if (nvme_path_is_optimized(ns)) {
420 int node, srcu_idx;
421
422 srcu_idx = srcu_read_lock(&head->srcu);
423 for_each_node(node)
424 __nvme_find_path(head, node);
425 srcu_read_unlock(&head->srcu, srcu_idx);
426 }
427 mutex_unlock(&head->lock);
428
429 synchronize_srcu(&head->srcu);
430 kblockd_schedule_work(&head->requeue_work);
431 }
432
nvme_parse_ana_log(struct nvme_ctrl * ctrl,void * data,int (* cb)(struct nvme_ctrl * ctrl,struct nvme_ana_group_desc *,void *))433 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
434 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
435 void *))
436 {
437 void *base = ctrl->ana_log_buf;
438 size_t offset = sizeof(struct nvme_ana_rsp_hdr);
439 int error, i;
440
441 lockdep_assert_held(&ctrl->ana_lock);
442
443 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
444 struct nvme_ana_group_desc *desc = base + offset;
445 u32 nr_nsids;
446 size_t nsid_buf_size;
447
448 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
449 return -EINVAL;
450
451 nr_nsids = le32_to_cpu(desc->nnsids);
452 nsid_buf_size = nr_nsids * sizeof(__le32);
453
454 if (WARN_ON_ONCE(desc->grpid == 0))
455 return -EINVAL;
456 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
457 return -EINVAL;
458 if (WARN_ON_ONCE(desc->state == 0))
459 return -EINVAL;
460 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
461 return -EINVAL;
462
463 offset += sizeof(*desc);
464 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
465 return -EINVAL;
466
467 error = cb(ctrl, desc, data);
468 if (error)
469 return error;
470
471 offset += nsid_buf_size;
472 }
473
474 return 0;
475 }
476
nvme_state_is_live(enum nvme_ana_state state)477 static inline bool nvme_state_is_live(enum nvme_ana_state state)
478 {
479 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
480 }
481
nvme_update_ns_ana_state(struct nvme_ana_group_desc * desc,struct nvme_ns * ns)482 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
483 struct nvme_ns *ns)
484 {
485 ns->ana_grpid = le32_to_cpu(desc->grpid);
486 ns->ana_state = desc->state;
487 clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
488
489 if (nvme_state_is_live(ns->ana_state))
490 nvme_mpath_set_live(ns);
491 }
492
nvme_update_ana_state(struct nvme_ctrl * ctrl,struct nvme_ana_group_desc * desc,void * data)493 static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
494 struct nvme_ana_group_desc *desc, void *data)
495 {
496 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
497 unsigned *nr_change_groups = data;
498 struct nvme_ns *ns;
499
500 dev_dbg(ctrl->device, "ANA group %d: %s.\n",
501 le32_to_cpu(desc->grpid),
502 nvme_ana_state_names[desc->state]);
503
504 if (desc->state == NVME_ANA_CHANGE)
505 (*nr_change_groups)++;
506
507 if (!nr_nsids)
508 return 0;
509
510 down_read(&ctrl->namespaces_rwsem);
511 list_for_each_entry(ns, &ctrl->namespaces, list) {
512 unsigned nsid;
513 again:
514 nsid = le32_to_cpu(desc->nsids[n]);
515 if (ns->head->ns_id < nsid)
516 continue;
517 if (ns->head->ns_id == nsid)
518 nvme_update_ns_ana_state(desc, ns);
519 if (++n == nr_nsids)
520 break;
521 if (ns->head->ns_id > nsid)
522 goto again;
523 }
524 up_read(&ctrl->namespaces_rwsem);
525 return 0;
526 }
527
nvme_read_ana_log(struct nvme_ctrl * ctrl)528 static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
529 {
530 u32 nr_change_groups = 0;
531 int error;
532
533 mutex_lock(&ctrl->ana_lock);
534 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
535 ctrl->ana_log_buf, ctrl->ana_log_size, 0);
536 if (error) {
537 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
538 goto out_unlock;
539 }
540
541 error = nvme_parse_ana_log(ctrl, &nr_change_groups,
542 nvme_update_ana_state);
543 if (error)
544 goto out_unlock;
545
546 /*
547 * In theory we should have an ANATT timer per group as they might enter
548 * the change state at different times. But that is a lot of overhead
549 * just to protect against a target that keeps entering new changes
550 * states while never finishing previous ones. But we'll still
551 * eventually time out once all groups are in change state, so this
552 * isn't a big deal.
553 *
554 * We also double the ANATT value to provide some slack for transports
555 * or AEN processing overhead.
556 */
557 if (nr_change_groups)
558 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
559 else
560 del_timer_sync(&ctrl->anatt_timer);
561 out_unlock:
562 mutex_unlock(&ctrl->ana_lock);
563 return error;
564 }
565
nvme_ana_work(struct work_struct * work)566 static void nvme_ana_work(struct work_struct *work)
567 {
568 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
569
570 if (ctrl->state != NVME_CTRL_LIVE)
571 return;
572
573 nvme_read_ana_log(ctrl);
574 }
575
nvme_anatt_timeout(struct timer_list * t)576 static void nvme_anatt_timeout(struct timer_list *t)
577 {
578 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
579
580 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
581 nvme_reset_ctrl(ctrl);
582 }
583
nvme_mpath_stop(struct nvme_ctrl * ctrl)584 void nvme_mpath_stop(struct nvme_ctrl *ctrl)
585 {
586 if (!nvme_ctrl_use_ana(ctrl))
587 return;
588 del_timer_sync(&ctrl->anatt_timer);
589 cancel_work_sync(&ctrl->ana_work);
590 }
591
592 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \
593 struct device_attribute subsys_attr_##_name = \
594 __ATTR(_name, _mode, _show, _store)
595
596 static const char *nvme_iopolicy_names[] = {
597 [NVME_IOPOLICY_NUMA] = "numa",
598 [NVME_IOPOLICY_RR] = "round-robin",
599 };
600
nvme_subsys_iopolicy_show(struct device * dev,struct device_attribute * attr,char * buf)601 static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
603 {
604 struct nvme_subsystem *subsys =
605 container_of(dev, struct nvme_subsystem, dev);
606
607 return sprintf(buf, "%s\n",
608 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
609 }
610
nvme_subsys_iopolicy_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)611 static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
612 struct device_attribute *attr, const char *buf, size_t count)
613 {
614 struct nvme_subsystem *subsys =
615 container_of(dev, struct nvme_subsystem, dev);
616 int i;
617
618 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
619 if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
620 WRITE_ONCE(subsys->iopolicy, i);
621 return count;
622 }
623 }
624
625 return -EINVAL;
626 }
627 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
628 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
629
ana_grpid_show(struct device * dev,struct device_attribute * attr,char * buf)630 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
631 char *buf)
632 {
633 return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
634 }
635 DEVICE_ATTR_RO(ana_grpid);
636
ana_state_show(struct device * dev,struct device_attribute * attr,char * buf)637 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
638 char *buf)
639 {
640 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
641
642 return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
643 }
644 DEVICE_ATTR_RO(ana_state);
645
nvme_lookup_ana_group_desc(struct nvme_ctrl * ctrl,struct nvme_ana_group_desc * desc,void * data)646 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
647 struct nvme_ana_group_desc *desc, void *data)
648 {
649 struct nvme_ana_group_desc *dst = data;
650
651 if (desc->grpid != dst->grpid)
652 return 0;
653
654 *dst = *desc;
655 return -ENXIO; /* just break out of the loop */
656 }
657
nvme_mpath_add_disk(struct nvme_ns * ns,struct nvme_id_ns * id)658 void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
659 {
660 if (nvme_ctrl_use_ana(ns->ctrl)) {
661 struct nvme_ana_group_desc desc = {
662 .grpid = id->anagrpid,
663 .state = 0,
664 };
665
666 mutex_lock(&ns->ctrl->ana_lock);
667 ns->ana_grpid = le32_to_cpu(id->anagrpid);
668 nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
669 mutex_unlock(&ns->ctrl->ana_lock);
670 if (desc.state) {
671 /* found the group desc: update */
672 nvme_update_ns_ana_state(&desc, ns);
673 } else {
674 /* group desc not found: trigger a re-read */
675 set_bit(NVME_NS_ANA_PENDING, &ns->flags);
676 queue_work(nvme_wq, &ns->ctrl->ana_work);
677 }
678 } else {
679 ns->ana_state = NVME_ANA_OPTIMIZED;
680 nvme_mpath_set_live(ns);
681 }
682
683 if (blk_queue_stable_writes(ns->queue) && ns->head->disk)
684 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES,
685 ns->head->disk->queue);
686 #ifdef CONFIG_BLK_DEV_ZONED
687 if (blk_queue_is_zoned(ns->queue) && ns->head->disk)
688 ns->head->disk->queue->nr_zones = ns->queue->nr_zones;
689 #endif
690 }
691
nvme_mpath_remove_disk(struct nvme_ns_head * head)692 void nvme_mpath_remove_disk(struct nvme_ns_head *head)
693 {
694 if (!head->disk)
695 return;
696 if (head->disk->flags & GENHD_FL_UP)
697 del_gendisk(head->disk);
698 blk_set_queue_dying(head->disk->queue);
699 /* make sure all pending bios are cleaned up */
700 kblockd_schedule_work(&head->requeue_work);
701 flush_work(&head->requeue_work);
702 blk_cleanup_queue(head->disk->queue);
703 if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
704 /*
705 * if device_add_disk wasn't called, prevent
706 * disk release to put a bogus reference on the
707 * request queue
708 */
709 head->disk->queue = NULL;
710 }
711 put_disk(head->disk);
712 }
713
nvme_mpath_init_ctrl(struct nvme_ctrl * ctrl)714 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
715 {
716 mutex_init(&ctrl->ana_lock);
717 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
718 INIT_WORK(&ctrl->ana_work, nvme_ana_work);
719 }
720
nvme_mpath_init_identify(struct nvme_ctrl * ctrl,struct nvme_id_ctrl * id)721 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
722 {
723 size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT;
724 size_t ana_log_size;
725 int error = 0;
726
727 /* check if multipath is enabled and we have the capability */
728 if (!multipath || !ctrl->subsys ||
729 !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA))
730 return 0;
731
732 ctrl->anacap = id->anacap;
733 ctrl->anatt = id->anatt;
734 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
735 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
736
737 ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
738 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
739 ctrl->max_namespaces * sizeof(__le32);
740 if (ana_log_size > max_transfer_size) {
741 dev_err(ctrl->device,
742 "ANA log page size (%zd) larger than MDTS (%zd).\n",
743 ana_log_size, max_transfer_size);
744 dev_err(ctrl->device, "disabling ANA support.\n");
745 goto out_uninit;
746 }
747 if (ana_log_size > ctrl->ana_log_size) {
748 nvme_mpath_stop(ctrl);
749 kfree(ctrl->ana_log_buf);
750 ctrl->ana_log_buf = kmalloc(ana_log_size, GFP_KERNEL);
751 if (!ctrl->ana_log_buf)
752 return -ENOMEM;
753 }
754 ctrl->ana_log_size = ana_log_size;
755 error = nvme_read_ana_log(ctrl);
756 if (error)
757 goto out_uninit;
758 return 0;
759
760 out_uninit:
761 nvme_mpath_uninit(ctrl);
762 return error;
763 }
764
nvme_mpath_uninit(struct nvme_ctrl * ctrl)765 void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
766 {
767 kfree(ctrl->ana_log_buf);
768 ctrl->ana_log_buf = NULL;
769 }
770
771