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