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
16 struct nullb_cmd {
17 struct request *rq;
18 struct bio *bio;
19 unsigned int tag;
20 blk_status_t error;
21 struct nullb_queue *nq;
22 struct hrtimer timer;
23 bool fake_timeout;
24 };
25
26 struct nullb_queue {
27 unsigned long *tag_map;
28 wait_queue_head_t wait;
29 unsigned int queue_depth;
30 struct nullb_device *dev;
31 unsigned int requeue_selection;
32
33 struct nullb_cmd *cmds;
34 };
35
36 struct nullb_device {
37 struct nullb *nullb;
38 struct config_item item;
39 struct radix_tree_root data; /* data stored in the disk */
40 struct radix_tree_root cache; /* disk cache data */
41 unsigned long flags; /* device flags */
42 unsigned int curr_cache;
43 struct badblocks badblocks;
44
45 unsigned int nr_zones;
46 unsigned int nr_zones_imp_open;
47 unsigned int nr_zones_exp_open;
48 unsigned int nr_zones_closed;
49 struct blk_zone *zones;
50 sector_t zone_size_sects;
51 spinlock_t zone_lock;
52 unsigned long *zone_locks;
53
54 unsigned long size; /* device size in MB */
55 unsigned long completion_nsec; /* time in ns to complete a request */
56 unsigned long cache_size; /* disk cache size in MB */
57 unsigned long zone_size; /* zone size in MB if device is zoned */
58 unsigned long zone_capacity; /* zone capacity in MB if device is zoned */
59 unsigned int zone_nr_conv; /* number of conventional zones */
60 unsigned int zone_max_open; /* max number of open zones */
61 unsigned int zone_max_active; /* max number of active zones */
62 unsigned int submit_queues; /* number of submission queues */
63 unsigned int home_node; /* home node for the device */
64 unsigned int queue_mode; /* block interface */
65 unsigned int blocksize; /* block size */
66 unsigned int irqmode; /* IRQ completion handler */
67 unsigned int hw_queue_depth; /* queue depth */
68 unsigned int index; /* index of the disk, only valid with a disk */
69 unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */
70 bool blocking; /* blocking blk-mq device */
71 bool use_per_node_hctx; /* use per-node allocation for hardware context */
72 bool power; /* power on/off the device */
73 bool memory_backed; /* if data is stored in memory */
74 bool discard; /* if support discard */
75 bool zoned; /* if device is zoned */
76 };
77
78 struct nullb {
79 struct nullb_device *dev;
80 struct list_head list;
81 unsigned int index;
82 struct request_queue *q;
83 struct gendisk *disk;
84 struct blk_mq_tag_set *tag_set;
85 struct blk_mq_tag_set __tag_set;
86 unsigned int queue_depth;
87 atomic_long_t cur_bytes;
88 struct hrtimer bw_timer;
89 unsigned long cache_flush_pos;
90 spinlock_t lock;
91
92 struct nullb_queue *queues;
93 unsigned int nr_queues;
94 char disk_name[DISK_NAME_LEN];
95 };
96
97 blk_status_t null_process_cmd(struct nullb_cmd *cmd,
98 enum req_opf op, sector_t sector,
99 unsigned int nr_sectors);
100
101 #ifdef CONFIG_BLK_DEV_ZONED
102 int null_init_zoned_dev(struct nullb_device *dev, struct request_queue *q);
103 int null_register_zoned_dev(struct nullb *nullb);
104 void null_free_zoned_dev(struct nullb_device *dev);
105 int null_report_zones(struct gendisk *disk, sector_t sector,
106 unsigned int nr_zones, report_zones_cb cb, void *data);
107 blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd,
108 enum req_opf op, sector_t sector,
109 sector_t nr_sectors);
110 size_t null_zone_valid_read_len(struct nullb *nullb,
111 sector_t sector, unsigned int len);
112 #else
null_init_zoned_dev(struct nullb_device * dev,struct request_queue * q)113 static inline int null_init_zoned_dev(struct nullb_device *dev,
114 struct request_queue *q)
115 {
116 pr_err("CONFIG_BLK_DEV_ZONED not enabled\n");
117 return -EINVAL;
118 }
null_register_zoned_dev(struct nullb * nullb)119 static inline int null_register_zoned_dev(struct nullb *nullb)
120 {
121 return -ENODEV;
122 }
null_free_zoned_dev(struct nullb_device * dev)123 static inline void null_free_zoned_dev(struct nullb_device *dev) {}
null_process_zoned_cmd(struct nullb_cmd * cmd,enum req_opf op,sector_t sector,sector_t nr_sectors)124 static inline blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd,
125 enum req_opf op, sector_t sector, sector_t nr_sectors)
126 {
127 return BLK_STS_NOTSUPP;
128 }
null_zone_valid_read_len(struct nullb * nullb,sector_t sector,unsigned int len)129 static inline size_t null_zone_valid_read_len(struct nullb *nullb,
130 sector_t sector,
131 unsigned int len)
132 {
133 return len;
134 }
135 #define null_report_zones NULL
136 #endif /* CONFIG_BLK_DEV_ZONED */
137 #endif /* __NULL_BLK_H */
138