1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2011-2014, Intel Corporation.
9 #include <linux/nvme.h>
10 #include <linux/cdev.h>
11 #include <linux/pci.h>
12 #include <linux/kref.h>
13 #include <linux/blk-mq.h>
14 #include <linux/lightnvm.h>
15 #include <linux/sed-opal.h>
16 #include <linux/fault-inject.h>
17 #include <linux/rcupdate.h>
18 #include <linux/wait.h>
19 #include <linux/t10-pi.h>
21 #include <trace/events/block.h>
23 extern unsigned int nvme_io_timeout;
24 #define NVME_IO_TIMEOUT (nvme_io_timeout * HZ)
26 extern unsigned int admin_timeout;
27 #define ADMIN_TIMEOUT (admin_timeout * HZ)
29 #define NVME_DEFAULT_KATO 5
30 #define NVME_KATO_GRACE 10
32 #ifdef CONFIG_ARCH_NO_SG_CHAIN
33 #define NVME_INLINE_SG_CNT 0
34 #define NVME_INLINE_METADATA_SG_CNT 0
36 #define NVME_INLINE_SG_CNT 2
37 #define NVME_INLINE_METADATA_SG_CNT 1
40 extern struct workqueue_struct *nvme_wq;
41 extern struct workqueue_struct *nvme_reset_wq;
42 extern struct workqueue_struct *nvme_delete_wq;
50 * List of workarounds for devices that required behavior not specified in
55 * Prefers I/O aligned to a stripe size specified in a vendor
56 * specific Identify field.
58 NVME_QUIRK_STRIPE_SIZE = (1 << 0),
61 * The controller doesn't handle Identify value others than 0 or 1
64 NVME_QUIRK_IDENTIFY_CNS = (1 << 1),
67 * The controller deterministically returns O's on reads to
68 * logical blocks that deallocate was called on.
70 NVME_QUIRK_DEALLOCATE_ZEROES = (1 << 2),
73 * The controller needs a delay before starts checking the device
74 * readiness, which is done by reading the NVME_CSTS_RDY bit.
76 NVME_QUIRK_DELAY_BEFORE_CHK_RDY = (1 << 3),
79 * APST should not be used.
81 NVME_QUIRK_NO_APST = (1 << 4),
84 * The deepest sleep state should not be used.
86 NVME_QUIRK_NO_DEEPEST_PS = (1 << 5),
89 * Supports the LighNVM command set if indicated in vs[1].
91 NVME_QUIRK_LIGHTNVM = (1 << 6),
94 * Set MEDIUM priority on SQ creation
96 NVME_QUIRK_MEDIUM_PRIO_SQ = (1 << 7),
99 * Ignore device provided subnqn.
101 NVME_QUIRK_IGNORE_DEV_SUBNQN = (1 << 8),
104 * Broken Write Zeroes.
106 NVME_QUIRK_DISABLE_WRITE_ZEROES = (1 << 9),
109 * Force simple suspend/resume path.
111 NVME_QUIRK_SIMPLE_SUSPEND = (1 << 10),
114 * Use only one interrupt vector for all queues
116 NVME_QUIRK_SINGLE_VECTOR = (1 << 11),
119 * Use non-standard 128 bytes SQEs.
121 NVME_QUIRK_128_BYTES_SQES = (1 << 12),
124 * Prevent tag overlap between queues
126 NVME_QUIRK_SHARED_TAGS = (1 << 13),
129 * Don't change the value of the temperature threshold feature
131 NVME_QUIRK_NO_TEMP_THRESH_CHANGE = (1 << 14),
135 * Common request structure for NVMe passthrough. All drivers must have
136 * this structure as the first member of their request-private data.
138 struct nvme_request {
139 struct nvme_command *cmd;
140 union nvme_result result;
144 struct nvme_ctrl *ctrl;
148 * Mark a bio as coming in through the mpath node.
150 #define REQ_NVME_MPATH REQ_DRV
153 NVME_REQ_CANCELLED = (1 << 0),
154 NVME_REQ_USERCMD = (1 << 1),
157 static inline struct nvme_request *nvme_req(struct request *req)
159 return blk_mq_rq_to_pdu(req);
162 static inline u16 nvme_req_qid(struct request *req)
166 return blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(req)) + 1;
169 /* The below value is the specific amount of delay needed before checking
170 * readiness in case of the PCI_DEVICE(0x1c58, 0x0003), which needs the
171 * NVME_QUIRK_DELAY_BEFORE_CHK_RDY quirk enabled. The value (in ms) was
174 #define NVME_QUIRK_DELAY_AMOUNT 2300
176 enum nvme_ctrl_state {
180 NVME_CTRL_CONNECTING,
185 struct nvme_fault_inject {
186 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
187 struct fault_attr attr;
188 struct dentry *parent;
189 bool dont_retry; /* DNR, do not retry */
190 u16 status; /* status code */
196 enum nvme_ctrl_state state;
199 struct mutex scan_lock;
200 const struct nvme_ctrl_ops *ops;
201 struct request_queue *admin_q;
202 struct request_queue *connect_q;
203 struct request_queue *fabrics_q;
207 struct blk_mq_tag_set *tagset;
208 struct blk_mq_tag_set *admin_tagset;
209 struct list_head namespaces;
210 struct rw_semaphore namespaces_rwsem;
211 struct device ctrl_device;
212 struct device *device; /* char device */
214 struct work_struct reset_work;
215 struct work_struct delete_work;
216 wait_queue_head_t state_wq;
218 struct nvme_subsystem *subsys;
219 struct list_head subsys_entry;
221 struct opal_dev *opal_dev;
234 u32 max_integrity_segments;
242 atomic_t abort_limit;
254 unsigned int shutdown_timeout;
257 unsigned long quirks;
258 struct nvme_id_power_state psd[32];
259 struct nvme_effects_log *effects;
260 struct work_struct scan_work;
261 struct work_struct async_event_work;
262 struct delayed_work ka_work;
263 struct nvme_command ka_cmd;
264 struct work_struct fw_act_work;
265 unsigned long events;
268 #ifdef CONFIG_NVME_MULTIPATH
269 /* asymmetric namespace access: */
274 struct mutex ana_lock;
275 struct nvme_ana_rsp_hdr *ana_log_buf;
277 struct timer_list anatt_timer;
278 struct work_struct ana_work;
281 /* Power saving configuration */
282 u64 ps_max_latency_us;
297 struct nvmf_ctrl_options *opts;
299 struct page *discard_page;
300 unsigned long discard_page_busy;
302 struct nvme_fault_inject fault_inject;
310 struct nvme_subsystem {
314 * Because we unregister the device on the last put we need
315 * a separate refcount.
318 struct list_head entry;
320 struct list_head ctrls;
321 struct list_head nsheads;
322 char subnqn[NVMF_NQN_SIZE];
325 char firmware_rev[8];
328 u16 awupf; /* 0's based awupf value. */
330 #ifdef CONFIG_NVME_MULTIPATH
331 enum nvme_iopolicy iopolicy;
336 * Container structure for uniqueue namespace identifiers.
345 * Anchor structure for namespaces. There is one for each namespace in a
346 * NVMe subsystem that any of our controllers can see, and the namespace
347 * structure for each controller is chained of it. For private namespaces
348 * there is a 1:1 relation to our namespace structures, that is ->list
349 * only ever has a single entry for private namespaces.
351 struct nvme_ns_head {
352 struct list_head list;
353 struct srcu_struct srcu;
354 struct nvme_subsystem *subsys;
356 struct nvme_ns_ids ids;
357 struct list_head entry;
361 #ifdef CONFIG_NVME_MULTIPATH
362 struct gendisk *disk;
363 struct bio_list requeue_list;
364 spinlock_t requeue_lock;
365 struct work_struct requeue_work;
367 struct nvme_ns __rcu *current_path[];
371 enum nvme_ns_features {
372 NVME_NS_EXT_LBAS = 1 << 0, /* support extended LBA format */
373 NVME_NS_METADATA_SUPPORTED = 1 << 1, /* support getting generated md */
377 struct list_head list;
379 struct nvme_ctrl *ctrl;
380 struct request_queue *queue;
381 struct gendisk *disk;
382 #ifdef CONFIG_NVME_MULTIPATH
383 enum nvme_ana_state ana_state;
386 struct list_head siblings;
387 struct nvm_dev *ndev;
389 struct nvme_ns_head *head;
396 unsigned long features;
398 #define NVME_NS_REMOVING 0
399 #define NVME_NS_DEAD 1
400 #define NVME_NS_ANA_PENDING 2
402 struct nvme_fault_inject fault_inject;
406 /* NVMe ns supports metadata actions by the controller (generate/strip) */
407 static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
409 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
412 struct nvme_ctrl_ops {
414 struct module *module;
416 #define NVME_F_FABRICS (1 << 0)
417 #define NVME_F_METADATA_SUPPORTED (1 << 1)
418 #define NVME_F_PCI_P2PDMA (1 << 2)
419 int (*reg_read32)(struct nvme_ctrl *ctrl, u32 off, u32 *val);
420 int (*reg_write32)(struct nvme_ctrl *ctrl, u32 off, u32 val);
421 int (*reg_read64)(struct nvme_ctrl *ctrl, u32 off, u64 *val);
422 void (*free_ctrl)(struct nvme_ctrl *ctrl);
423 void (*submit_async_event)(struct nvme_ctrl *ctrl);
424 void (*delete_ctrl)(struct nvme_ctrl *ctrl);
425 int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
428 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
429 void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
430 const char *dev_name);
431 void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inject);
432 void nvme_should_fail(struct request *req);
434 static inline void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
435 const char *dev_name)
438 static inline void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inj)
441 static inline void nvme_should_fail(struct request *req) {}
444 static inline int nvme_reset_subsystem(struct nvme_ctrl *ctrl)
446 if (!ctrl->subsystem)
448 return ctrl->ops->reg_write32(ctrl, NVME_REG_NSSR, 0x4E564D65);
452 * Convert a 512B sector number to a device logical block number.
454 static inline u64 nvme_sect_to_lba(struct nvme_ns *ns, sector_t sector)
456 return sector >> (ns->lba_shift - SECTOR_SHIFT);
460 * Convert a device logical block number to a 512B sector number.
462 static inline sector_t nvme_lba_to_sect(struct nvme_ns *ns, u64 lba)
464 return lba << (ns->lba_shift - SECTOR_SHIFT);
468 * Convert byte length to nvme's 0-based num dwords
470 static inline u32 nvme_bytes_to_numd(size_t len)
472 return (len >> 2) - 1;
475 static inline void nvme_end_request(struct request *req, __le16 status,
476 union nvme_result result)
478 struct nvme_request *rq = nvme_req(req);
480 rq->status = le16_to_cpu(status) >> 1;
482 /* inject error when permitted by fault injection framework */
483 nvme_should_fail(req);
484 blk_mq_complete_request(req);
487 static inline void nvme_get_ctrl(struct nvme_ctrl *ctrl)
489 get_device(ctrl->device);
492 static inline void nvme_put_ctrl(struct nvme_ctrl *ctrl)
494 put_device(ctrl->device);
497 static inline bool nvme_is_aen_req(u16 qid, __u16 command_id)
499 return !qid && command_id >= NVME_AQ_BLK_MQ_DEPTH;
502 void nvme_complete_rq(struct request *req);
503 bool nvme_cancel_request(struct request *req, void *data, bool reserved);
504 bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
505 enum nvme_ctrl_state new_state);
506 bool nvme_wait_reset(struct nvme_ctrl *ctrl);
507 int nvme_disable_ctrl(struct nvme_ctrl *ctrl);
508 int nvme_enable_ctrl(struct nvme_ctrl *ctrl);
509 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl);
510 int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
511 const struct nvme_ctrl_ops *ops, unsigned long quirks);
512 void nvme_uninit_ctrl(struct nvme_ctrl *ctrl);
513 void nvme_start_ctrl(struct nvme_ctrl *ctrl);
514 void nvme_stop_ctrl(struct nvme_ctrl *ctrl);
515 int nvme_init_identify(struct nvme_ctrl *ctrl);
517 void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
519 int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
522 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
523 volatile union nvme_result *res);
525 void nvme_stop_queues(struct nvme_ctrl *ctrl);
526 void nvme_start_queues(struct nvme_ctrl *ctrl);
527 void nvme_kill_queues(struct nvme_ctrl *ctrl);
528 void nvme_sync_queues(struct nvme_ctrl *ctrl);
529 void nvme_unfreeze(struct nvme_ctrl *ctrl);
530 void nvme_wait_freeze(struct nvme_ctrl *ctrl);
531 void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout);
532 void nvme_start_freeze(struct nvme_ctrl *ctrl);
534 #define NVME_QID_ANY -1
535 struct request *nvme_alloc_request(struct request_queue *q,
536 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid);
537 void nvme_cleanup_cmd(struct request *req);
538 blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
539 struct nvme_command *cmd);
540 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
541 void *buf, unsigned bufflen);
542 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
543 union nvme_result *result, void *buffer, unsigned bufflen,
544 unsigned timeout, int qid, int at_head,
545 blk_mq_req_flags_t flags, bool poll);
546 int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid,
547 unsigned int dword11, void *buffer, size_t buflen,
549 int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid,
550 unsigned int dword11, void *buffer, size_t buflen,
552 int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
553 void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
554 int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
555 int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl);
556 int nvme_try_sched_reset(struct nvme_ctrl *ctrl);
557 int nvme_delete_ctrl(struct nvme_ctrl *ctrl);
559 int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp,
560 void *log, size_t size, u64 offset);
562 extern const struct attribute_group *nvme_ns_id_attr_groups[];
563 extern const struct block_device_operations nvme_ns_head_ops;
565 #ifdef CONFIG_NVME_MULTIPATH
566 static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
568 return ctrl->ana_log_buf != NULL;
571 void nvme_mpath_unfreeze(struct nvme_subsystem *subsys);
572 void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys);
573 void nvme_mpath_start_freeze(struct nvme_subsystem *subsys);
574 void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
575 struct nvme_ctrl *ctrl, int *flags);
576 bool nvme_failover_req(struct request *req);
577 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl);
578 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,struct nvme_ns_head *head);
579 void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id);
580 void nvme_mpath_remove_disk(struct nvme_ns_head *head);
581 int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id);
582 void nvme_mpath_uninit(struct nvme_ctrl *ctrl);
583 void nvme_mpath_stop(struct nvme_ctrl *ctrl);
584 bool nvme_mpath_clear_current_path(struct nvme_ns *ns);
585 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl);
586 struct nvme_ns *nvme_find_path(struct nvme_ns_head *head);
588 static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
590 struct nvme_ns_head *head = ns->head;
592 if (head->disk && list_empty(&head->list))
593 kblockd_schedule_work(&head->requeue_work);
596 static inline void nvme_trace_bio_complete(struct request *req,
599 struct nvme_ns *ns = req->q->queuedata;
601 if (req->cmd_flags & REQ_NVME_MPATH)
602 trace_block_bio_complete(ns->head->disk->queue,
606 extern struct device_attribute dev_attr_ana_grpid;
607 extern struct device_attribute dev_attr_ana_state;
608 extern struct device_attribute subsys_attr_iopolicy;
611 static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
616 * Without the multipath code enabled, multiple controller per subsystems are
617 * visible as devices and thus we cannot use the subsystem instance.
619 static inline void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
620 struct nvme_ctrl *ctrl, int *flags)
622 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
625 static inline bool nvme_failover_req(struct request *req)
629 static inline void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
632 static inline int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,
633 struct nvme_ns_head *head)
637 static inline void nvme_mpath_add_disk(struct nvme_ns *ns,
638 struct nvme_id_ns *id)
641 static inline void nvme_mpath_remove_disk(struct nvme_ns_head *head)
644 static inline bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
648 static inline void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
651 static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
654 static inline void nvme_trace_bio_complete(struct request *req,
658 static inline int nvme_mpath_init(struct nvme_ctrl *ctrl,
659 struct nvme_id_ctrl *id)
661 if (ctrl->subsys->cmic & (1 << 3))
662 dev_warn(ctrl->device,
663 "Please enable CONFIG_NVME_MULTIPATH for full support of multi-port devices.\n");
666 static inline void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
669 static inline void nvme_mpath_stop(struct nvme_ctrl *ctrl)
672 static inline void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
675 static inline void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
678 static inline void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
681 #endif /* CONFIG_NVME_MULTIPATH */
684 int nvme_nvm_register(struct nvme_ns *ns, char *disk_name, int node);
685 void nvme_nvm_unregister(struct nvme_ns *ns);
686 extern const struct attribute_group nvme_nvm_attr_group;
687 int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd, unsigned long arg);
689 static inline int nvme_nvm_register(struct nvme_ns *ns, char *disk_name,
695 static inline void nvme_nvm_unregister(struct nvme_ns *ns) {};
696 static inline int nvme_nvm_ioctl(struct nvme_ns *ns, unsigned int cmd,
701 #endif /* CONFIG_NVM */
703 static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
705 return dev_to_disk(dev)->private_data;
708 #ifdef CONFIG_NVME_HWMON
709 void nvme_hwmon_init(struct nvme_ctrl *ctrl);
711 static inline void nvme_hwmon_init(struct nvme_ctrl *ctrl) { }