1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/vmalloc.h>
5 /* zone_size in MBs to sectors. */
6 #define ZONE_SIZE_SHIFT 11
8 static inline unsigned int null_zone_no(struct nullb_device
*dev
, sector_t sect
)
10 return sect
>> ilog2(dev
->zone_size_sects
);
13 int null_zone_init(struct nullb_device
*dev
)
15 sector_t dev_size
= (sector_t
)dev
->size
* 1024 * 1024;
19 if (!is_power_of_2(dev
->zone_size
)) {
20 pr_err("zone_size must be power-of-two\n");
23 if (dev
->zone_size
> dev
->size
) {
24 pr_err("Zone size larger than device capacity\n");
28 dev
->zone_size_sects
= dev
->zone_size
<< ZONE_SIZE_SHIFT
;
29 dev
->nr_zones
= dev_size
>>
30 (SECTOR_SHIFT
+ ilog2(dev
->zone_size_sects
));
31 dev
->zones
= kvmalloc_array(dev
->nr_zones
, sizeof(struct blk_zone
),
32 GFP_KERNEL
| __GFP_ZERO
);
36 if (dev
->zone_nr_conv
>= dev
->nr_zones
) {
37 dev
->zone_nr_conv
= dev
->nr_zones
- 1;
38 pr_info("changed the number of conventional zones to %u",
42 for (i
= 0; i
< dev
->zone_nr_conv
; i
++) {
43 struct blk_zone
*zone
= &dev
->zones
[i
];
46 zone
->len
= dev
->zone_size_sects
;
47 zone
->wp
= zone
->start
+ zone
->len
;
48 zone
->type
= BLK_ZONE_TYPE_CONVENTIONAL
;
49 zone
->cond
= BLK_ZONE_COND_NOT_WP
;
51 sector
+= dev
->zone_size_sects
;
54 for (i
= dev
->zone_nr_conv
; i
< dev
->nr_zones
; i
++) {
55 struct blk_zone
*zone
= &dev
->zones
[i
];
57 zone
->start
= zone
->wp
= sector
;
58 zone
->len
= dev
->zone_size_sects
;
59 zone
->type
= BLK_ZONE_TYPE_SEQWRITE_REQ
;
60 zone
->cond
= BLK_ZONE_COND_EMPTY
;
62 sector
+= dev
->zone_size_sects
;
68 void null_zone_exit(struct nullb_device
*dev
)
73 int null_zone_report(struct gendisk
*disk
, sector_t sector
,
74 struct blk_zone
*zones
, unsigned int *nr_zones
)
76 struct nullb
*nullb
= disk
->private_data
;
77 struct nullb_device
*dev
= nullb
->dev
;
78 unsigned int zno
, nrz
= 0;
80 zno
= null_zone_no(dev
, sector
);
81 if (zno
< dev
->nr_zones
) {
82 nrz
= min_t(unsigned int, *nr_zones
, dev
->nr_zones
- zno
);
83 memcpy(zones
, &dev
->zones
[zno
], nrz
* sizeof(struct blk_zone
));
91 static blk_status_t
null_zone_write(struct nullb_cmd
*cmd
, sector_t sector
,
92 unsigned int nr_sectors
)
94 struct nullb_device
*dev
= cmd
->nq
->dev
;
95 unsigned int zno
= null_zone_no(dev
, sector
);
96 struct blk_zone
*zone
= &dev
->zones
[zno
];
99 case BLK_ZONE_COND_FULL
:
100 /* Cannot write to a full zone */
101 cmd
->error
= BLK_STS_IOERR
;
102 return BLK_STS_IOERR
;
103 case BLK_ZONE_COND_EMPTY
:
104 case BLK_ZONE_COND_IMP_OPEN
:
105 /* Writes must be at the write pointer position */
106 if (sector
!= zone
->wp
)
107 return BLK_STS_IOERR
;
109 if (zone
->cond
== BLK_ZONE_COND_EMPTY
)
110 zone
->cond
= BLK_ZONE_COND_IMP_OPEN
;
112 zone
->wp
+= nr_sectors
;
113 if (zone
->wp
== zone
->start
+ zone
->len
)
114 zone
->cond
= BLK_ZONE_COND_FULL
;
116 case BLK_ZONE_COND_NOT_WP
:
119 /* Invalid zone condition */
120 return BLK_STS_IOERR
;
125 static blk_status_t
null_zone_reset(struct nullb_cmd
*cmd
, sector_t sector
)
127 struct nullb_device
*dev
= cmd
->nq
->dev
;
128 unsigned int zno
= null_zone_no(dev
, sector
);
129 struct blk_zone
*zone
= &dev
->zones
[zno
];
132 switch (req_op(cmd
->rq
)) {
133 case REQ_OP_ZONE_RESET_ALL
:
134 for (i
= 0; i
< dev
->nr_zones
; i
++) {
135 if (zone
[i
].type
== BLK_ZONE_TYPE_CONVENTIONAL
)
137 zone
[i
].cond
= BLK_ZONE_COND_EMPTY
;
138 zone
[i
].wp
= zone
[i
].start
;
141 case REQ_OP_ZONE_RESET
:
142 if (zone
->type
== BLK_ZONE_TYPE_CONVENTIONAL
)
143 return BLK_STS_IOERR
;
145 zone
->cond
= BLK_ZONE_COND_EMPTY
;
146 zone
->wp
= zone
->start
;
149 return BLK_STS_NOTSUPP
;
154 blk_status_t
null_handle_zoned(struct nullb_cmd
*cmd
, enum req_opf op
,
155 sector_t sector
, sector_t nr_sectors
)
159 return null_zone_write(cmd
, sector
, nr_sectors
);
160 case REQ_OP_ZONE_RESET
:
161 case REQ_OP_ZONE_RESET_ALL
:
162 return null_zone_reset(cmd
, sector
);