blob: be7646205b8c4aa76cae304c46ae038fed2a37fe [file] [log] [blame]
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001// SPDX-License-Identifier: GPL-2.0
2#include <linux/vmalloc.h>
3#include "null_blk.h"
4
5/* zone_size in MBs to sectors. */
6#define ZONE_SIZE_SHIFT 11
7
8static inline unsigned int null_zone_no(struct nullb_device *dev, sector_t sect)
9{
10 return sect >> ilog2(dev->zone_size_sects);
11}
12
13int null_zone_init(struct nullb_device *dev)
14{
15 sector_t dev_size = (sector_t)dev->size * 1024 * 1024;
16 sector_t sector = 0;
17 unsigned int i;
18
19 if (!is_power_of_2(dev->zone_size)) {
André Almeida9c7eddf2019-09-16 11:07:59 -030020 pr_err("zone_size must be power-of-two\n");
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020021 return -EINVAL;
22 }
23
24 dev->zone_size_sects = dev->zone_size << ZONE_SIZE_SHIFT;
25 dev->nr_zones = dev_size >>
26 (SECTOR_SHIFT + ilog2(dev->zone_size_sects));
27 dev->zones = kvmalloc_array(dev->nr_zones, sizeof(struct blk_zone),
28 GFP_KERNEL | __GFP_ZERO);
29 if (!dev->zones)
30 return -ENOMEM;
31
Masato Suzukiea2c18e2018-10-30 16:14:05 +090032 if (dev->zone_nr_conv >= dev->nr_zones) {
33 dev->zone_nr_conv = dev->nr_zones - 1;
André Almeida9c7eddf2019-09-16 11:07:59 -030034 pr_info("changed the number of conventional zones to %u",
Masato Suzukiea2c18e2018-10-30 16:14:05 +090035 dev->zone_nr_conv);
36 }
37
38 for (i = 0; i < dev->zone_nr_conv; i++) {
39 struct blk_zone *zone = &dev->zones[i];
40
41 zone->start = sector;
42 zone->len = dev->zone_size_sects;
43 zone->wp = zone->start + zone->len;
44 zone->type = BLK_ZONE_TYPE_CONVENTIONAL;
45 zone->cond = BLK_ZONE_COND_NOT_WP;
46
47 sector += dev->zone_size_sects;
48 }
49
50 for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) {
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020051 struct blk_zone *zone = &dev->zones[i];
52
53 zone->start = zone->wp = sector;
54 zone->len = dev->zone_size_sects;
55 zone->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
56 zone->cond = BLK_ZONE_COND_EMPTY;
57
58 sector += dev->zone_size_sects;
59 }
60
61 return 0;
62}
63
64void null_zone_exit(struct nullb_device *dev)
65{
66 kvfree(dev->zones);
67}
68
Christoph Hellwige76239a2018-10-12 19:08:49 +090069int null_zone_report(struct gendisk *disk, sector_t sector,
Damien Le Moalbd976e52019-07-01 14:09:16 +090070 struct blk_zone *zones, unsigned int *nr_zones)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020071{
Christoph Hellwige76239a2018-10-12 19:08:49 +090072 struct nullb *nullb = disk->private_data;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020073 struct nullb_device *dev = nullb->dev;
Christoph Hellwige76239a2018-10-12 19:08:49 +090074 unsigned int zno, nrz = 0;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020075
Christoph Hellwige76239a2018-10-12 19:08:49 +090076 zno = null_zone_no(dev, sector);
77 if (zno < dev->nr_zones) {
78 nrz = min_t(unsigned int, *nr_zones, dev->nr_zones - zno);
79 memcpy(zones, &dev->zones[zno], nrz * sizeof(struct blk_zone));
80 }
81
82 *nr_zones = nrz;
83
84 return 0;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020085}
86
Ajay Joshidd85b492019-10-17 14:19:43 -070087size_t null_zone_valid_read_len(struct nullb *nullb,
88 sector_t sector, unsigned int len)
89{
90 struct nullb_device *dev = nullb->dev;
91 struct blk_zone *zone = &dev->zones[null_zone_no(dev, sector)];
92 unsigned int nr_sectors = len >> SECTOR_SHIFT;
93
94 /* Read must be below the write pointer position */
95 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL ||
96 sector + nr_sectors <= zone->wp)
97 return len;
98
99 if (sector > zone->wp)
100 return 0;
101
102 return (zone->wp - sector) << SECTOR_SHIFT;
103}
104
Chaitanya Kulkarnifceb5d12019-08-22 21:45:18 -0700105static blk_status_t null_zone_write(struct nullb_cmd *cmd, sector_t sector,
Jens Axboeb228ba12018-09-12 18:21:11 -0600106 unsigned int nr_sectors)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200107{
108 struct nullb_device *dev = cmd->nq->dev;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200109 unsigned int zno = null_zone_no(dev, sector);
110 struct blk_zone *zone = &dev->zones[zno];
111
112 switch (zone->cond) {
113 case BLK_ZONE_COND_FULL:
114 /* Cannot write to a full zone */
115 cmd->error = BLK_STS_IOERR;
Chaitanya Kulkarnifceb5d12019-08-22 21:45:18 -0700116 return BLK_STS_IOERR;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200117 case BLK_ZONE_COND_EMPTY:
118 case BLK_ZONE_COND_IMP_OPEN:
119 /* Writes must be at the write pointer position */
Chaitanya Kulkarnifceb5d12019-08-22 21:45:18 -0700120 if (sector != zone->wp)
121 return BLK_STS_IOERR;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200122
123 if (zone->cond == BLK_ZONE_COND_EMPTY)
124 zone->cond = BLK_ZONE_COND_IMP_OPEN;
125
Jens Axboeb228ba12018-09-12 18:21:11 -0600126 zone->wp += nr_sectors;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200127 if (zone->wp == zone->start + zone->len)
128 zone->cond = BLK_ZONE_COND_FULL;
129 break;
Masato Suzukiea2c18e2018-10-30 16:14:05 +0900130 case BLK_ZONE_COND_NOT_WP:
131 break;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200132 default:
133 /* Invalid zone condition */
Chaitanya Kulkarnifceb5d12019-08-22 21:45:18 -0700134 return BLK_STS_IOERR;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200135 }
Chaitanya Kulkarnifceb5d12019-08-22 21:45:18 -0700136 return BLK_STS_OK;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200137}
138
Ajay Joshida644b22019-10-27 23:05:49 +0900139static blk_status_t null_zone_mgmt(struct nullb_cmd *cmd, enum req_opf op,
140 sector_t sector)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200141{
142 struct nullb_device *dev = cmd->nq->dev;
Ajay Joshidd85b492019-10-17 14:19:43 -0700143 struct blk_zone *zone = &dev->zones[null_zone_no(dev, sector)];
Chaitanya Kulkarnia61dbfb2019-08-01 10:26:38 -0700144 size_t i;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200145
Ajay Joshida644b22019-10-27 23:05:49 +0900146 switch (op) {
Chaitanya Kulkarnia61dbfb2019-08-01 10:26:38 -0700147 case REQ_OP_ZONE_RESET_ALL:
148 for (i = 0; i < dev->nr_zones; i++) {
149 if (zone[i].type == BLK_ZONE_TYPE_CONVENTIONAL)
150 continue;
151 zone[i].cond = BLK_ZONE_COND_EMPTY;
152 zone[i].wp = zone[i].start;
153 }
154 break;
155 case REQ_OP_ZONE_RESET:
Chaitanya Kulkarnifceb5d12019-08-22 21:45:18 -0700156 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
157 return BLK_STS_IOERR;
Chaitanya Kulkarnia61dbfb2019-08-01 10:26:38 -0700158
159 zone->cond = BLK_ZONE_COND_EMPTY;
160 zone->wp = zone->start;
161 break;
Ajay Joshida644b22019-10-27 23:05:49 +0900162 case REQ_OP_ZONE_OPEN:
163 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
164 return BLK_STS_IOERR;
165 if (zone->cond == BLK_ZONE_COND_FULL)
166 return BLK_STS_IOERR;
167
168 zone->cond = BLK_ZONE_COND_EXP_OPEN;
169 break;
170 case REQ_OP_ZONE_CLOSE:
171 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
172 return BLK_STS_IOERR;
173 if (zone->cond == BLK_ZONE_COND_FULL)
174 return BLK_STS_IOERR;
175
176 zone->cond = BLK_ZONE_COND_CLOSED;
177 break;
178 case REQ_OP_ZONE_FINISH:
179 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
180 return BLK_STS_IOERR;
181
182 zone->cond = BLK_ZONE_COND_FULL;
183 zone->wp = zone->start + zone->len;
184 break;
Chaitanya Kulkarnia61dbfb2019-08-01 10:26:38 -0700185 default:
186 cmd->error = BLK_STS_NOTSUPP;
187 break;
Masato Suzukiea2c18e2018-10-30 16:14:05 +0900188 }
Chaitanya Kulkarnifceb5d12019-08-22 21:45:18 -0700189 return BLK_STS_OK;
190}
191
Jens Axboe38b4e092019-08-23 12:49:00 -0600192blk_status_t null_handle_zoned(struct nullb_cmd *cmd, enum req_opf op,
193 sector_t sector, sector_t nr_sectors)
Chaitanya Kulkarnifceb5d12019-08-22 21:45:18 -0700194{
195 switch (op) {
196 case REQ_OP_WRITE:
197 return null_zone_write(cmd, sector, nr_sectors);
198 case REQ_OP_ZONE_RESET:
199 case REQ_OP_ZONE_RESET_ALL:
Ajay Joshida644b22019-10-27 23:05:49 +0900200 case REQ_OP_ZONE_OPEN:
201 case REQ_OP_ZONE_CLOSE:
202 case REQ_OP_ZONE_FINISH:
203 return null_zone_mgmt(cmd, op, sector);
Chaitanya Kulkarnifceb5d12019-08-22 21:45:18 -0700204 default:
205 return BLK_STS_OK;
206 }
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200207}