2 * Copyright (c) 2017-2018 Christoph Hellwig.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include <linux/moduleparam.h>
15 #include <trace/events/block.h>
18 static bool multipath = true;
19 module_param(multipath, bool, 0444);
20 MODULE_PARM_DESC(multipath,
21 "turn on native support for multiple controllers per subsystem");
24 * If multipathing is enabled we need to always use the subsystem instance
25 * number for numbering our devices to avoid conflicts between subsystems that
26 * have multiple controllers and thus use the multipath-aware subsystem node
27 * and those that have a single controller and use the controller node
30 void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
31 struct nvme_ctrl *ctrl, int *flags)
34 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
35 } else if (ns->head->disk) {
36 sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
37 ctrl->cntlid, ns->head->instance);
38 *flags = GENHD_FL_HIDDEN;
40 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
45 void nvme_failover_req(struct request *req)
47 struct nvme_ns *ns = req->q->queuedata;
48 u16 status = nvme_req(req)->status;
51 spin_lock_irqsave(&ns->head->requeue_lock, flags);
52 blk_steal_bios(&ns->head->requeue_list, req);
53 spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
54 blk_mq_end_request(req, 0);
56 switch (status & 0x7ff) {
57 case NVME_SC_ANA_TRANSITION:
58 case NVME_SC_ANA_INACCESSIBLE:
59 case NVME_SC_ANA_PERSISTENT_LOSS:
61 * If we got back an ANA error we know the controller is alive,
62 * but not ready to serve this namespaces. The spec suggests
63 * we should update our general state here, but due to the fact
64 * that the admin and I/O queues are not serialized that is
65 * fundamentally racy. So instead just clear the current path,
66 * mark the the path as pending and kick of a re-read of the ANA
69 nvme_mpath_clear_current_path(ns);
70 if (ns->ctrl->ana_log_buf) {
71 set_bit(NVME_NS_ANA_PENDING, &ns->flags);
72 queue_work(nvme_wq, &ns->ctrl->ana_work);
75 case NVME_SC_HOST_PATH_ERROR:
77 * Temporary transport disruption in talking to the controller.
78 * Try to send on a new path.
80 nvme_mpath_clear_current_path(ns);
84 * Reset the controller for any non-ANA error as we don't know
85 * what caused the error.
87 nvme_reset_ctrl(ns->ctrl);
91 kblockd_schedule_work(&ns->head->requeue_work);
94 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
98 down_read(&ctrl->namespaces_rwsem);
99 list_for_each_entry(ns, &ctrl->namespaces, list) {
101 kblockd_schedule_work(&ns->head->requeue_work);
103 up_read(&ctrl->namespaces_rwsem);
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",
115 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head)
117 struct nvme_ns *ns, *fallback = NULL;
119 list_for_each_entry_rcu(ns, &head->list, siblings) {
120 if (ns->ctrl->state != NVME_CTRL_LIVE ||
121 test_bit(NVME_NS_ANA_PENDING, &ns->flags))
123 switch (ns->ana_state) {
124 case NVME_ANA_OPTIMIZED:
125 rcu_assign_pointer(head->current_path, ns);
127 case NVME_ANA_NONOPTIMIZED:
136 rcu_assign_pointer(head->current_path, fallback);
140 static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
142 return ns->ctrl->state == NVME_CTRL_LIVE &&
143 ns->ana_state == NVME_ANA_OPTIMIZED;
146 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
148 struct nvme_ns *ns = srcu_dereference(head->current_path, &head->srcu);
150 if (unlikely(!ns || !nvme_path_is_optimized(ns)))
151 ns = __nvme_find_path(head);
155 static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
158 struct nvme_ns_head *head = q->queuedata;
159 struct device *dev = disk_to_dev(head->disk);
161 blk_qc_t ret = BLK_QC_T_NONE;
164 srcu_idx = srcu_read_lock(&head->srcu);
165 ns = nvme_find_path(head);
167 bio->bi_disk = ns->disk;
168 bio->bi_opf |= REQ_NVME_MPATH;
169 trace_block_bio_remap(bio->bi_disk->queue, bio,
170 disk_devt(ns->head->disk),
171 bio->bi_iter.bi_sector);
172 ret = direct_make_request(bio);
173 } else if (!list_empty_careful(&head->list)) {
174 dev_warn_ratelimited(dev, "no path available - requeuing I/O\n");
176 spin_lock_irq(&head->requeue_lock);
177 bio_list_add(&head->requeue_list, bio);
178 spin_unlock_irq(&head->requeue_lock);
180 dev_warn_ratelimited(dev, "no path - failing I/O\n");
182 bio->bi_status = BLK_STS_IOERR;
186 srcu_read_unlock(&head->srcu, srcu_idx);
190 static bool nvme_ns_head_poll(struct request_queue *q, blk_qc_t qc)
192 struct nvme_ns_head *head = q->queuedata;
197 srcu_idx = srcu_read_lock(&head->srcu);
198 ns = srcu_dereference(head->current_path, &head->srcu);
199 if (likely(ns && nvme_path_is_optimized(ns)))
200 found = ns->queue->poll_fn(q, qc);
201 srcu_read_unlock(&head->srcu, srcu_idx);
205 static void nvme_requeue_work(struct work_struct *work)
207 struct nvme_ns_head *head =
208 container_of(work, struct nvme_ns_head, requeue_work);
209 struct bio *bio, *next;
211 spin_lock_irq(&head->requeue_lock);
212 next = bio_list_get(&head->requeue_list);
213 spin_unlock_irq(&head->requeue_lock);
215 while ((bio = next) != NULL) {
220 * Reset disk to the mpath node and resubmit to select a new
223 bio->bi_disk = head->disk;
224 generic_make_request(bio);
228 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
230 struct request_queue *q;
233 mutex_init(&head->lock);
234 bio_list_init(&head->requeue_list);
235 spin_lock_init(&head->requeue_lock);
236 INIT_WORK(&head->requeue_work, nvme_requeue_work);
239 * Add a multipath node if the subsystems supports multiple controllers.
240 * We also do this for private namespaces as the namespace sharing data could
241 * change after a rescan.
243 if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
246 q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE, NULL);
250 blk_queue_make_request(q, nvme_ns_head_make_request);
251 q->poll_fn = nvme_ns_head_poll;
252 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
253 /* set to a default value for 512 until disk is validated */
254 blk_queue_logical_block_size(q, 512);
255 blk_set_stacking_limits(&q->limits);
257 /* we need to propagate up the VMC settings */
258 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
260 blk_queue_write_cache(q, vwc, vwc);
262 head->disk = alloc_disk(0);
264 goto out_cleanup_queue;
265 head->disk->fops = &nvme_ns_head_ops;
266 head->disk->private_data = head;
267 head->disk->queue = q;
268 head->disk->flags = GENHD_FL_EXT_DEVT;
269 sprintf(head->disk->disk_name, "nvme%dn%d",
270 ctrl->subsys->instance, head->instance);
274 blk_cleanup_queue(q);
279 static void nvme_mpath_set_live(struct nvme_ns *ns)
281 struct nvme_ns_head *head = ns->head;
283 lockdep_assert_held(&ns->head->lock);
288 if (!(head->disk->flags & GENHD_FL_UP)) {
289 device_add_disk(&head->subsys->dev, head->disk);
290 if (sysfs_create_group(&disk_to_dev(head->disk)->kobj,
291 &nvme_ns_id_attr_group))
292 dev_warn(&head->subsys->dev,
293 "failed to create id group.\n");
296 kblockd_schedule_work(&ns->head->requeue_work);
299 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
300 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
303 void *base = ctrl->ana_log_buf;
304 size_t offset = sizeof(struct nvme_ana_rsp_hdr);
307 lockdep_assert_held(&ctrl->ana_lock);
309 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
310 struct nvme_ana_group_desc *desc = base + offset;
311 u32 nr_nsids = le32_to_cpu(desc->nnsids);
312 size_t nsid_buf_size = nr_nsids * sizeof(__le32);
314 if (WARN_ON_ONCE(desc->grpid == 0))
316 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
318 if (WARN_ON_ONCE(desc->state == 0))
320 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
323 offset += sizeof(*desc);
324 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
327 error = cb(ctrl, desc, data);
331 offset += nsid_buf_size;
332 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
339 static inline bool nvme_state_is_live(enum nvme_ana_state state)
341 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
344 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
347 mutex_lock(&ns->head->lock);
348 ns->ana_grpid = le32_to_cpu(desc->grpid);
349 ns->ana_state = desc->state;
350 clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
352 if (nvme_state_is_live(ns->ana_state))
353 nvme_mpath_set_live(ns);
354 mutex_unlock(&ns->head->lock);
357 static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
358 struct nvme_ana_group_desc *desc, void *data)
360 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
361 unsigned *nr_change_groups = data;
364 dev_info(ctrl->device, "ANA group %d: %s.\n",
365 le32_to_cpu(desc->grpid),
366 nvme_ana_state_names[desc->state]);
368 if (desc->state == NVME_ANA_CHANGE)
369 (*nr_change_groups)++;
374 down_write(&ctrl->namespaces_rwsem);
375 list_for_each_entry(ns, &ctrl->namespaces, list) {
376 if (ns->head->ns_id != le32_to_cpu(desc->nsids[n]))
378 nvme_update_ns_ana_state(desc, ns);
382 up_write(&ctrl->namespaces_rwsem);
383 WARN_ON_ONCE(n < nr_nsids);
387 static int nvme_read_ana_log(struct nvme_ctrl *ctrl, bool groups_only)
389 u32 nr_change_groups = 0;
392 mutex_lock(&ctrl->ana_lock);
393 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA,
394 groups_only ? NVME_ANA_LOG_RGO : 0,
395 ctrl->ana_log_buf, ctrl->ana_log_size, 0);
397 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
401 error = nvme_parse_ana_log(ctrl, &nr_change_groups,
402 nvme_update_ana_state);
407 * In theory we should have an ANATT timer per group as they might enter
408 * the change state at different times. But that is a lot of overhead
409 * just to protect against a target that keeps entering new changes
410 * states while never finishing previous ones. But we'll still
411 * eventually time out once all groups are in change state, so this
414 * We also double the ANATT value to provide some slack for transports
415 * or AEN processing overhead.
417 if (nr_change_groups)
418 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
420 del_timer_sync(&ctrl->anatt_timer);
422 mutex_unlock(&ctrl->ana_lock);
426 static void nvme_ana_work(struct work_struct *work)
428 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
430 nvme_read_ana_log(ctrl, false);
433 static void nvme_anatt_timeout(struct timer_list *t)
435 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
437 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
438 nvme_reset_ctrl(ctrl);
441 void nvme_mpath_stop(struct nvme_ctrl *ctrl)
443 if (!nvme_ctrl_use_ana(ctrl))
445 del_timer_sync(&ctrl->anatt_timer);
446 cancel_work_sync(&ctrl->ana_work);
449 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
452 return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
454 DEVICE_ATTR_RO(ana_grpid);
456 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
459 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
461 return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
463 DEVICE_ATTR_RO(ana_state);
465 static int nvme_set_ns_ana_state(struct nvme_ctrl *ctrl,
466 struct nvme_ana_group_desc *desc, void *data)
468 struct nvme_ns *ns = data;
470 if (ns->ana_grpid == le32_to_cpu(desc->grpid)) {
471 nvme_update_ns_ana_state(desc, ns);
472 return -ENXIO; /* just break out of the loop */
478 void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
480 if (nvme_ctrl_use_ana(ns->ctrl)) {
481 mutex_lock(&ns->ctrl->ana_lock);
482 ns->ana_grpid = le32_to_cpu(id->anagrpid);
483 nvme_parse_ana_log(ns->ctrl, ns, nvme_set_ns_ana_state);
484 mutex_unlock(&ns->ctrl->ana_lock);
486 mutex_lock(&ns->head->lock);
487 ns->ana_state = NVME_ANA_OPTIMIZED;
488 nvme_mpath_set_live(ns);
489 mutex_unlock(&ns->head->lock);
493 void nvme_mpath_remove_disk(struct nvme_ns_head *head)
497 if (head->disk->flags & GENHD_FL_UP) {
498 sysfs_remove_group(&disk_to_dev(head->disk)->kobj,
499 &nvme_ns_id_attr_group);
500 del_gendisk(head->disk);
502 blk_set_queue_dying(head->disk->queue);
503 /* make sure all pending bios are cleaned up */
504 kblockd_schedule_work(&head->requeue_work);
505 flush_work(&head->requeue_work);
506 blk_cleanup_queue(head->disk->queue);
507 put_disk(head->disk);
510 int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
514 /* check if multipath is enabled and we have the capability */
515 if (!multipath || !ctrl->subsys || !(ctrl->subsys->cmic & (1 << 3)))
518 ctrl->anacap = id->anacap;
519 ctrl->anatt = id->anatt;
520 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
521 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
523 mutex_init(&ctrl->ana_lock);
524 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
525 ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
526 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc);
527 ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32);
529 if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) {
530 dev_err(ctrl->device,
531 "ANA log page size (%zd) larger than MDTS (%d).\n",
533 ctrl->max_hw_sectors << SECTOR_SHIFT);
534 dev_err(ctrl->device, "disabling ANA support.\n");
538 INIT_WORK(&ctrl->ana_work, nvme_ana_work);
539 ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
540 if (!ctrl->ana_log_buf) {
545 error = nvme_read_ana_log(ctrl, true);
547 goto out_free_ana_log_buf;
549 out_free_ana_log_buf:
550 kfree(ctrl->ana_log_buf);
551 ctrl->ana_log_buf = NULL;
556 void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
558 kfree(ctrl->ana_log_buf);
559 ctrl->ana_log_buf = NULL;