1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __BLK_NULL_BLK_H
3 #define __BLK_NULL_BLK_H
4 
5 #undef pr_fmt
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
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>
15 #include <linux/spinlock.h>
16 #include <linux/mutex.h>
17 
18 struct nullb_cmd {
19 	struct request *rq;
20 	struct bio *bio;
21 	unsigned int tag;
22 	blk_status_t error;
23 	struct nullb_queue *nq;
24 	struct hrtimer timer;
25 	bool fake_timeout;
26 };
27 
28 struct nullb_queue {
29 	unsigned long *tag_map;
30 	wait_queue_head_t wait;
31 	unsigned int queue_depth;
32 	struct nullb_device *dev;
33 	unsigned int requeue_selection;
34 
35 	struct nullb_cmd *cmds;
36 };
37 
38 struct nullb_zone {
39 	/*
40 	 * Zone lock to prevent concurrent modification of a zone write
41 	 * pointer position and condition: with memory backing, a write
42 	 * command execution may sleep on memory allocation. For this case,
43 	 * use mutex as the zone lock. Otherwise, use the spinlock for
44 	 * locking the zone.
45 	 */
46 	union {
47 		spinlock_t spinlock;
48 		struct mutex mutex;
49 	};
50 	enum blk_zone_type type;
51 	enum blk_zone_cond cond;
52 	sector_t start;
53 	sector_t wp;
54 	unsigned int len;
55 	unsigned int capacity;
56 };
57 
58 struct nullb_device {
59 	struct nullb *nullb;
60 	struct config_item item;
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 
67 	unsigned int nr_zones;
68 	unsigned int nr_zones_imp_open;
69 	unsigned int nr_zones_exp_open;
70 	unsigned int nr_zones_closed;
71 	unsigned int imp_close_zone_no;
72 	struct nullb_zone *zones;
73 	sector_t zone_size_sects;
74 	bool need_zone_res_mgmt;
75 	spinlock_t zone_res_lock;
76 
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 */
80 	unsigned long zone_size; /* zone size in MB if device is zoned */
81 	unsigned long zone_capacity; /* zone capacity in MB if device is zoned */
82 	unsigned int zone_nr_conv; /* number of conventional zones */
83 	unsigned int zone_max_open; /* max number of open zones */
84 	unsigned int zone_max_active; /* max number of active zones */
85 	unsigned int submit_queues; /* number of submission queues */
86 	unsigned int home_node; /* home node for the device */
87 	unsigned int queue_mode; /* block interface */
88 	unsigned int blocksize; /* block size */
89 	unsigned int max_sectors; /* Max sectors per command */
90 	unsigned int irqmode; /* IRQ completion handler */
91 	unsigned int hw_queue_depth; /* queue depth */
92 	unsigned int index; /* index of the disk, only valid with a disk */
93 	unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */
94 	bool blocking; /* blocking blk-mq device */
95 	bool use_per_node_hctx; /* use per-node allocation for hardware context */
96 	bool power; /* power on/off the device */
97 	bool memory_backed; /* if data is stored in memory */
98 	bool discard; /* if support discard */
99 	bool zoned; /* if device is zoned */
100 	bool virt_boundary; /* virtual boundary on/off for the device */
101 };
102 
103 struct nullb {
104 	struct nullb_device *dev;
105 	struct list_head list;
106 	unsigned int index;
107 	struct request_queue *q;
108 	struct gendisk *disk;
109 	struct blk_mq_tag_set *tag_set;
110 	struct blk_mq_tag_set __tag_set;
111 	unsigned int queue_depth;
112 	atomic_long_t cur_bytes;
113 	struct hrtimer bw_timer;
114 	unsigned long cache_flush_pos;
115 	spinlock_t lock;
116 
117 	struct nullb_queue *queues;
118 	unsigned int nr_queues;
119 	char disk_name[DISK_NAME_LEN];
120 };
121 
122 blk_status_t null_handle_discard(struct nullb_device *dev, sector_t sector,
123 				 sector_t nr_sectors);
124 blk_status_t null_process_cmd(struct nullb_cmd *cmd,
125 			      enum req_opf op, sector_t sector,
126 			      unsigned int nr_sectors);
127 
128 #ifdef CONFIG_BLK_DEV_ZONED
129 int null_init_zoned_dev(struct nullb_device *dev, struct request_queue *q);
130 int null_register_zoned_dev(struct nullb *nullb);
131 void null_free_zoned_dev(struct nullb_device *dev);
132 int null_report_zones(struct gendisk *disk, sector_t sector,
133 		      unsigned int nr_zones, report_zones_cb cb, void *data);
134 blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd,
135 				    enum req_opf op, sector_t sector,
136 				    sector_t nr_sectors);
137 size_t null_zone_valid_read_len(struct nullb *nullb,
138 				sector_t sector, unsigned int len);
139 #else
140 static inline int null_init_zoned_dev(struct nullb_device *dev,
141 				      struct request_queue *q)
142 {
143 	pr_err("CONFIG_BLK_DEV_ZONED not enabled\n");
144 	return -EINVAL;
145 }
146 static inline int null_register_zoned_dev(struct nullb *nullb)
147 {
148 	return -ENODEV;
149 }
150 static inline void null_free_zoned_dev(struct nullb_device *dev) {}
151 static inline blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd,
152 			enum req_opf op, sector_t sector, sector_t nr_sectors)
153 {
154 	return BLK_STS_NOTSUPP;
155 }
156 static inline size_t null_zone_valid_read_len(struct nullb *nullb,
157 					      sector_t sector,
158 					      unsigned int len)
159 {
160 	return len;
161 }
162 #define null_report_zones	NULL
163 #endif /* CONFIG_BLK_DEV_ZONED */
164 #endif /* __NULL_BLK_H */
165