Commit | Line | Data |
---|---|---|
6dad38d3 MB |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | #ifndef __BLK_NULL_BLK_H | |
3 | #define __BLK_NULL_BLK_H | |
4 | ||
9c7eddf1 AA |
5 | #undef pr_fmt |
6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
7 | ||
6dad38d3 MB |
8 | #include <linux/blkdev.h> |
9 | #include <linux/slab.h> | |
10 | #include <linux/blk-mq.h> | |
11 | #include <linux/hrtimer.h> | |
12 | #include <linux/configfs.h> | |
13 | #include <linux/badblocks.h> | |
14 | #include <linux/fault-inject.h> | |
2b8b7ed7 DLM |
15 | #include <linux/spinlock.h> |
16 | #include <linux/mutex.h> | |
6dad38d3 MB |
17 | |
18 | struct nullb_cmd { | |
6dad38d3 | 19 | blk_status_t error; |
8ba816b2 | 20 | bool fake_timeout; |
6dad38d3 MB |
21 | struct nullb_queue *nq; |
22 | struct hrtimer timer; | |
23 | }; | |
24 | ||
25 | struct nullb_queue { | |
6dad38d3 MB |
26 | struct nullb_device *dev; |
27 | unsigned int requeue_selection; | |
28 | ||
0a593fbb JA |
29 | struct list_head poll_list; |
30 | spinlock_t poll_lock; | |
6dad38d3 MB |
31 | }; |
32 | ||
2b8b7ed7 DLM |
33 | struct nullb_zone { |
34 | /* | |
35 | * Zone lock to prevent concurrent modification of a zone write | |
36 | * pointer position and condition: with memory backing, a write | |
37 | * command execution may sleep on memory allocation. For this case, | |
38 | * use mutex as the zone lock. Otherwise, use the spinlock for | |
39 | * locking the zone. | |
40 | */ | |
41 | union { | |
42 | spinlock_t spinlock; | |
43 | struct mutex mutex; | |
44 | }; | |
45 | enum blk_zone_type type; | |
46 | enum blk_zone_cond cond; | |
47 | sector_t start; | |
48 | sector_t wp; | |
49 | unsigned int len; | |
50 | unsigned int capacity; | |
51 | }; | |
52 | ||
6dad38d3 MB |
53 | struct nullb_device { |
54 | struct nullb *nullb; | |
bb4c19e0 AM |
55 | struct config_group group; |
56 | #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION | |
57 | struct fault_config timeout_config; | |
58 | struct fault_config requeue_config; | |
59 | struct fault_config init_hctx_fault_config; | |
60 | #endif | |
6dad38d3 MB |
61 | struct radix_tree_root data; /* data stored in the disk */ |
62 | struct radix_tree_root cache; /* disk cache data */ | |
63 | unsigned long flags; /* device flags */ | |
64 | unsigned int curr_cache; | |
65 | struct badblocks badblocks; | |
66 | ||
ca4b2a01 | 67 | unsigned int nr_zones; |
dc4d137e NC |
68 | unsigned int nr_zones_imp_open; |
69 | unsigned int nr_zones_exp_open; | |
70 | unsigned int nr_zones_closed; | |
2e8c6e0e | 71 | unsigned int imp_close_zone_no; |
2b8b7ed7 | 72 | struct nullb_zone *zones; |
ca4b2a01 | 73 | sector_t zone_size_sects; |
2b8b7ed7 DLM |
74 | bool need_zone_res_mgmt; |
75 | spinlock_t zone_res_lock; | |
ca4b2a01 | 76 | |
6dad38d3 MB |
77 | unsigned long size; /* device size in MB */ |
78 | unsigned long completion_nsec; /* time in ns to complete a request */ | |
79 | unsigned long cache_size; /* disk cache size in MB */ | |
ca4b2a01 | 80 | unsigned long zone_size; /* zone size in MB if device is zoned */ |
089565fb | 81 | unsigned long zone_capacity; /* zone capacity in MB if device is zoned */ |
ea2c18e1 | 82 | unsigned int zone_nr_conv; /* number of conventional zones */ |
dc4d137e NC |
83 | unsigned int zone_max_open; /* max number of open zones */ |
84 | unsigned int zone_max_active; /* max number of active zones */ | |
997a1f08 | 85 | unsigned int zone_append_max_sectors; /* Max sectors per zone append command */ |
6dad38d3 | 86 | unsigned int submit_queues; /* number of submission queues */ |
15dfc662 | 87 | unsigned int prev_submit_queues; /* number of submission queues before change */ |
0a593fbb | 88 | unsigned int poll_queues; /* number of IOPOLL submission queues */ |
15dfc662 | 89 | unsigned int prev_poll_queues; /* number of IOPOLL submission queues before change */ |
6dad38d3 MB |
90 | unsigned int home_node; /* home node for the device */ |
91 | unsigned int queue_mode; /* block interface */ | |
92 | unsigned int blocksize; /* block size */ | |
ea17fd35 | 93 | unsigned int max_sectors; /* Max sectors per command */ |
6dad38d3 MB |
94 | unsigned int irqmode; /* IRQ completion handler */ |
95 | unsigned int hw_queue_depth; /* queue depth */ | |
96 | unsigned int index; /* index of the disk, only valid with a disk */ | |
97 | unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */ | |
98 | bool blocking; /* blocking blk-mq device */ | |
99 | bool use_per_node_hctx; /* use per-node allocation for hardware context */ | |
100 | bool power; /* power on/off the device */ | |
101 | bool memory_backed; /* if data is stored in memory */ | |
102 | bool discard; /* if support discard */ | |
ca4b2a01 | 103 | bool zoned; /* if device is zoned */ |
f4d5dc33 | 104 | bool zone_full; /* Initialize zones to be full */ |
cee1b215 | 105 | bool virt_boundary; /* virtual boundary on/off for the device */ |
7012eef5 | 106 | bool no_sched; /* no IO scheduler for the device */ |
14509b74 | 107 | bool shared_tags; /* share tag set between devices for blk-mq */ |
7012eef5 | 108 | bool shared_tag_bitmap; /* use hostwide shared tags */ |
f4f84586 | 109 | bool fua; /* Support FUA */ |
6dad38d3 MB |
110 | }; |
111 | ||
112 | struct nullb { | |
113 | struct nullb_device *dev; | |
114 | struct list_head list; | |
115 | unsigned int index; | |
116 | struct request_queue *q; | |
117 | struct gendisk *disk; | |
118 | struct blk_mq_tag_set *tag_set; | |
119 | struct blk_mq_tag_set __tag_set; | |
6dad38d3 MB |
120 | atomic_long_t cur_bytes; |
121 | struct hrtimer bw_timer; | |
122 | unsigned long cache_flush_pos; | |
123 | spinlock_t lock; | |
124 | ||
125 | struct nullb_queue *queues; | |
6dad38d3 MB |
126 | char disk_name[DISK_NAME_LEN]; |
127 | }; | |
ca4b2a01 | 128 | |
0ec4d913 DLM |
129 | blk_status_t null_handle_discard(struct nullb_device *dev, sector_t sector, |
130 | sector_t nr_sectors); | |
ff07a02e BVA |
131 | blk_status_t null_process_cmd(struct nullb_cmd *cmd, enum req_op op, |
132 | sector_t sector, unsigned int nr_sectors); | |
9dd44c7e | 133 | |
ca4b2a01 | 134 | #ifdef CONFIG_BLK_DEV_ZONED |
e440626b | 135 | int null_init_zoned_dev(struct nullb_device *dev, struct queue_limits *lim); |
d205bde7 DLM |
136 | int null_register_zoned_dev(struct nullb *nullb); |
137 | void null_free_zoned_dev(struct nullb_device *dev); | |
7fc8fb51 | 138 | int null_report_zones(struct gendisk *disk, sector_t sector, |
d4100351 | 139 | unsigned int nr_zones, report_zones_cb cb, void *data); |
ff07a02e BVA |
140 | blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd, enum req_op op, |
141 | sector_t sector, sector_t nr_sectors); | |
dd85b492 AJ |
142 | size_t null_zone_valid_read_len(struct nullb *nullb, |
143 | sector_t sector, unsigned int len); | |
d3a57388 SK |
144 | ssize_t zone_cond_store(struct nullb_device *dev, const char *page, |
145 | size_t count, enum blk_zone_cond cond); | |
ca4b2a01 | 146 | #else |
d205bde7 | 147 | static inline int null_init_zoned_dev(struct nullb_device *dev, |
e440626b | 148 | struct queue_limits *lim) |
ca4b2a01 | 149 | { |
9c7eddf1 | 150 | pr_err("CONFIG_BLK_DEV_ZONED not enabled\n"); |
ca4b2a01 MB |
151 | return -EINVAL; |
152 | } | |
d205bde7 DLM |
153 | static inline int null_register_zoned_dev(struct nullb *nullb) |
154 | { | |
155 | return -ENODEV; | |
156 | } | |
157 | static inline void null_free_zoned_dev(struct nullb_device *dev) {} | |
9dd44c7e | 158 | static inline blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd, |
ff07a02e | 159 | enum req_op op, sector_t sector, sector_t nr_sectors) |
b228ba1c | 160 | { |
fceb5d1b | 161 | return BLK_STS_NOTSUPP; |
b228ba1c | 162 | } |
dd85b492 AJ |
163 | static inline size_t null_zone_valid_read_len(struct nullb *nullb, |
164 | sector_t sector, | |
165 | unsigned int len) | |
166 | { | |
167 | return len; | |
168 | } | |
d3a57388 SK |
169 | static inline ssize_t zone_cond_store(struct nullb_device *dev, |
170 | const char *page, size_t count, | |
171 | enum blk_zone_cond cond) | |
172 | { | |
173 | return -EOPNOTSUPP; | |
174 | } | |
7fc8fb51 | 175 | #define null_report_zones NULL |
ca4b2a01 | 176 | #endif /* CONFIG_BLK_DEV_ZONED */ |
6dad38d3 | 177 | #endif /* __NULL_BLK_H */ |