blob: 663608d1003b142ceec83a76031f868353129a29 [file] [log] [blame]
Thomas Gleixnera98c5b12019-06-01 10:08:25 +02001// SPDX-License-Identifier: GPL-2.0-only
Hannes Reinecke89d94752016-10-18 15:40:34 +09002/*
3 * SCSI Zoned Block commands
4 *
5 * Copyright (C) 2014-2015 SUSE Linux GmbH
6 * Written by: Hannes Reinecke <hare@suse.de>
7 * Modified by: Damien Le Moal <damien.lemoal@hgst.com>
8 * Modified by: Shaun Tancheff <shaun.tancheff@seagate.com>
Hannes Reinecke89d94752016-10-18 15:40:34 +09009 */
10
11#include <linux/blkdev.h>
Damien Le Moalb091ac62019-07-01 14:09:17 +090012#include <linux/vmalloc.h>
13#include <linux/sched/mm.h>
Hannes Reinecke89d94752016-10-18 15:40:34 +090014
15#include <asm/unaligned.h>
16
17#include <scsi/scsi.h>
18#include <scsi/scsi_cmnd.h>
Hannes Reinecke89d94752016-10-18 15:40:34 +090019
20#include "sd.h"
Hannes Reinecke89d94752016-10-18 15:40:34 +090021
22/**
Damien Le Moale98f42b2017-10-11 05:54:22 +090023 * sd_zbc_parse_report - Convert a zone descriptor to a struct blk_zone,
24 * @sdkp: The disk the report originated from
25 * @buf: Address of the report zone descriptor
26 * @zone: the destination zone structure
27 *
28 * All LBA sized values are converted to 512B sectors unit.
Hannes Reinecke89d94752016-10-18 15:40:34 +090029 */
Damien Le Moale98f42b2017-10-11 05:54:22 +090030static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
Hannes Reinecke89d94752016-10-18 15:40:34 +090031 struct blk_zone *zone)
32{
33 struct scsi_device *sdp = sdkp->device;
34
35 memset(zone, 0, sizeof(struct blk_zone));
36
37 zone->type = buf[0] & 0x0f;
38 zone->cond = (buf[1] >> 4) & 0xf;
39 if (buf[1] & 0x01)
40 zone->reset = 1;
41 if (buf[1] & 0x02)
42 zone->non_seq = 1;
43
44 zone->len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
45 zone->start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
46 zone->wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
47 if (zone->type != ZBC_ZONE_TYPE_CONV &&
48 zone->cond == ZBC_ZONE_COND_FULL)
49 zone->wp = zone->start + zone->len;
50}
51
52/**
Christoph Hellwige76239a2018-10-12 19:08:49 +090053 * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
Damien Le Moale98f42b2017-10-11 05:54:22 +090054 * @sdkp: The target disk
Damien Le Moalb091ac62019-07-01 14:09:17 +090055 * @buf: vmalloc-ed buffer to use for the reply
Damien Le Moale98f42b2017-10-11 05:54:22 +090056 * @buflen: the buffer size
57 * @lba: Start LBA of the report
Damien Le Moald2e428e2018-10-12 19:08:41 +090058 * @partial: Do partial report
Damien Le Moale98f42b2017-10-11 05:54:22 +090059 *
60 * For internal use during device validation.
Damien Le Moald2e428e2018-10-12 19:08:41 +090061 * Using partial=true can significantly speed up execution of a report zones
62 * command because the disk does not have to count all possible report matching
63 * zones and will only report the count of zones fitting in the command reply
64 * buffer.
Hannes Reinecke89d94752016-10-18 15:40:34 +090065 */
Christoph Hellwige76239a2018-10-12 19:08:49 +090066static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
67 unsigned int buflen, sector_t lba,
68 bool partial)
Hannes Reinecke89d94752016-10-18 15:40:34 +090069{
70 struct scsi_device *sdp = sdkp->device;
71 const int timeout = sdp->request_queue->rq_timeout;
72 struct scsi_sense_hdr sshdr;
73 unsigned char cmd[16];
74 unsigned int rep_len;
75 int result;
76
77 memset(cmd, 0, 16);
78 cmd[0] = ZBC_IN;
79 cmd[1] = ZI_REPORT_ZONES;
80 put_unaligned_be64(lba, &cmd[2]);
81 put_unaligned_be32(buflen, &cmd[10]);
Damien Le Moald2e428e2018-10-12 19:08:41 +090082 if (partial)
83 cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
Hannes Reinecke89d94752016-10-18 15:40:34 +090084
85 result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
86 buf, buflen, &sshdr,
87 timeout, SD_MAX_RETRIES, NULL);
88 if (result) {
89 sd_printk(KERN_ERR, sdkp,
90 "REPORT ZONES lba %llu failed with %d/%d\n",
91 (unsigned long long)lba,
92 host_byte(result), driver_byte(result));
93 return -EIO;
94 }
95
96 rep_len = get_unaligned_be32(&buf[0]);
97 if (rep_len < 64) {
98 sd_printk(KERN_ERR, sdkp,
99 "REPORT ZONES report invalid length %u\n",
100 rep_len);
101 return -EIO;
102 }
103
104 return 0;
105}
106
Damien Le Moalb091ac62019-07-01 14:09:17 +0900107/**
108 * Allocate a buffer for report zones reply.
109 * @sdkp: The target disk
110 * @nr_zones: Maximum number of zones to report
111 * @buflen: Size of the buffer allocated
112 *
113 * Try to allocate a reply buffer for the number of requested zones.
114 * The size of the buffer allocated may be smaller than requested to
115 * satify the device constraint (max_hw_sectors, max_segments, etc).
116 *
117 * Return the address of the allocated buffer and update @buflen with
118 * the size of the allocated buffer.
119 */
120static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
121 unsigned int nr_zones, size_t *buflen)
122{
123 struct request_queue *q = sdkp->disk->queue;
124 size_t bufsize;
125 void *buf;
126
127 /*
128 * Report zone buffer size should be at most 64B times the number of
129 * zones requested plus the 64B reply header, but should be at least
130 * SECTOR_SIZE for ATA devices.
131 * Make sure that this size does not exceed the hardware capabilities.
132 * Furthermore, since the report zone command cannot be split, make
133 * sure that the allocated buffer can always be mapped by limiting the
134 * number of pages allocated to the HBA max segments limit.
135 */
Damien Le Moal23a50862019-11-11 11:39:29 +0900136 nr_zones = min(nr_zones, sdkp->nr_zones);
137 bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE);
Damien Le Moalb091ac62019-07-01 14:09:17 +0900138 bufsize = min_t(size_t, bufsize,
139 queue_max_hw_sectors(q) << SECTOR_SHIFT);
140 bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
141
Damien Le Moal23a50862019-11-11 11:39:29 +0900142 while (bufsize >= SECTOR_SIZE) {
143 buf = __vmalloc(bufsize,
144 GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY,
145 PAGE_KERNEL);
146 if (buf) {
147 *buflen = bufsize;
148 return buf;
149 }
150 bufsize >>= 1;
151 }
Damien Le Moalb091ac62019-07-01 14:09:17 +0900152
Damien Le Moal23a50862019-11-11 11:39:29 +0900153 return NULL;
Damien Le Moalb091ac62019-07-01 14:09:17 +0900154}
155
Damien Le Moale98f42b2017-10-11 05:54:22 +0900156/**
Christoph Hellwige76239a2018-10-12 19:08:49 +0900157 * sd_zbc_report_zones - Disk report zones operation.
158 * @disk: The target disk
159 * @sector: Start 512B sector of the report
160 * @zones: Array of zone descriptors
161 * @nr_zones: Number of descriptors in the array
Damien Le Moale98f42b2017-10-11 05:54:22 +0900162 *
Christoph Hellwige76239a2018-10-12 19:08:49 +0900163 * Execute a report zones command on the target disk.
Damien Le Moale98f42b2017-10-11 05:54:22 +0900164 */
Christoph Hellwige76239a2018-10-12 19:08:49 +0900165int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
Damien Le Moalbd976e52019-07-01 14:09:16 +0900166 struct blk_zone *zones, unsigned int *nr_zones)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900167{
Christoph Hellwige76239a2018-10-12 19:08:49 +0900168 struct scsi_disk *sdkp = scsi_disk(disk);
Damien Le Moalb091ac62019-07-01 14:09:17 +0900169 unsigned int i, nrz = *nr_zones;
Christoph Hellwige76239a2018-10-12 19:08:49 +0900170 unsigned char *buf;
Damien Le Moalb091ac62019-07-01 14:09:17 +0900171 size_t buflen = 0, offset = 0;
Christoph Hellwige76239a2018-10-12 19:08:49 +0900172 int ret = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900173
174 if (!sd_is_zoned(sdkp))
175 /* Not a zoned device */
Christoph Hellwige76239a2018-10-12 19:08:49 +0900176 return -EOPNOTSUPP;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900177
Damien Le Moalb091ac62019-07-01 14:09:17 +0900178 buf = sd_zbc_alloc_report_buffer(sdkp, nrz, &buflen);
Christoph Hellwige76239a2018-10-12 19:08:49 +0900179 if (!buf)
180 return -ENOMEM;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900181
Christoph Hellwige76239a2018-10-12 19:08:49 +0900182 ret = sd_zbc_do_report_zones(sdkp, buf, buflen,
183 sectors_to_logical(sdkp->device, sector), true);
184 if (ret)
Damien Le Moalb091ac62019-07-01 14:09:17 +0900185 goto out;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900186
Christoph Hellwige76239a2018-10-12 19:08:49 +0900187 nrz = min(nrz, get_unaligned_be32(&buf[0]) / 64);
188 for (i = 0; i < nrz; i++) {
189 offset += 64;
190 sd_zbc_parse_report(sdkp, buf + offset, zones);
191 zones++;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900192 }
Christoph Hellwige76239a2018-10-12 19:08:49 +0900193
194 *nr_zones = nrz;
195
Damien Le Moalb091ac62019-07-01 14:09:17 +0900196out:
197 kvfree(buf);
Christoph Hellwige76239a2018-10-12 19:08:49 +0900198
199 return ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900200}
201
Damien Le Moale98f42b2017-10-11 05:54:22 +0900202/**
203 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
204 * @sdkp: The target disk
205 */
Hannes Reinecke89d94752016-10-18 15:40:34 +0900206static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
207{
208 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
209}
210
Damien Le Moale98f42b2017-10-11 05:54:22 +0900211/**
Ajay Joshiad512f22019-10-27 23:05:47 +0900212 * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations
213 * can be RESET WRITE POINTER, OPEN, CLOSE or FINISH.
Damien Le Moale98f42b2017-10-11 05:54:22 +0900214 * @cmd: the command to setup
Ajay Joshiad512f22019-10-27 23:05:47 +0900215 * @op: Operation to be performed
216 * @all: All zones control
Damien Le Moale98f42b2017-10-11 05:54:22 +0900217 *
Ajay Joshiad512f22019-10-27 23:05:47 +0900218 * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL,
219 * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests.
Damien Le Moale98f42b2017-10-11 05:54:22 +0900220 */
Ajay Joshiad512f22019-10-27 23:05:47 +0900221blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
222 unsigned char op, bool all)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900223{
224 struct request *rq = cmd->request;
225 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
226 sector_t sector = blk_rq_pos(rq);
227 sector_t block = sectors_to_logical(sdkp->device, sector);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900228
229 if (!sd_is_zoned(sdkp))
230 /* Not a zoned device */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100231 return BLK_STS_IOERR;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900232
233 if (sdkp->device->changed)
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100234 return BLK_STS_IOERR;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900235
236 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
237 /* Unaligned request */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100238 return BLK_STS_IOERR;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900239
Hannes Reinecke89d94752016-10-18 15:40:34 +0900240 cmd->cmd_len = 16;
241 memset(cmd->cmnd, 0, cmd->cmd_len);
242 cmd->cmnd[0] = ZBC_OUT;
Ajay Joshiad512f22019-10-27 23:05:47 +0900243 cmd->cmnd[1] = op;
Chaitanya Kulkarnid81e9d42019-08-01 10:26:37 -0700244 if (all)
245 cmd->cmnd[14] = 0x1;
246 else
247 put_unaligned_be64(block, &cmd->cmnd[2]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900248
249 rq->timeout = SD_TIMEOUT;
250 cmd->sc_data_direction = DMA_NONE;
251 cmd->transfersize = 0;
252 cmd->allowed = 0;
253
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100254 return BLK_STS_OK;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900255}
256
Damien Le Moale98f42b2017-10-11 05:54:22 +0900257/**
Damien Le Moale98f42b2017-10-11 05:54:22 +0900258 * sd_zbc_complete - ZBC command post processing.
259 * @cmd: Completed command
260 * @good_bytes: Command reply bytes
261 * @sshdr: command sense header
262 *
263 * Called from sd_done(). Process report zones reply and handle reset zone
264 * and write commands errors.
265 */
266void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
Hannes Reinecke89d94752016-10-18 15:40:34 +0900267 struct scsi_sense_hdr *sshdr)
268{
269 int result = cmd->result;
270 struct request *rq = cmd->request;
271
Ajay Joshiad512f22019-10-27 23:05:47 +0900272 if (op_is_zone_mgmt(req_op(rq)) &&
Damien Le Moaledc1f542019-10-27 23:05:44 +0900273 result &&
274 sshdr->sense_key == ILLEGAL_REQUEST &&
275 sshdr->asc == 0x24) {
276 /*
Ajay Joshiad512f22019-10-27 23:05:47 +0900277 * INVALID FIELD IN CDB error: a zone management command was
278 * attempted on a conventional zone. Nothing to worry about,
279 * so be quiet about the error.
Damien Le Moaledc1f542019-10-27 23:05:44 +0900280 */
281 rq->rq_flags |= RQF_QUIET;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900282 }
283}
284
285/**
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900286 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
Damien Le Moale98f42b2017-10-11 05:54:22 +0900287 * @sdkp: Target disk
288 * @buf: Buffer where to store the VPD page data
289 *
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900290 * Read VPD page B6, get information and check that reads are unconstrained.
Hannes Reinecke89d94752016-10-18 15:40:34 +0900291 */
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900292static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
293 unsigned char *buf)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900294{
295
296 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
297 sd_printk(KERN_NOTICE, sdkp,
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900298 "Read zoned characteristics VPD page failed\n");
Hannes Reinecke89d94752016-10-18 15:40:34 +0900299 return -ENODEV;
300 }
301
302 if (sdkp->device->type != TYPE_ZBC) {
303 /* Host-aware */
304 sdkp->urswrz = 1;
Damien Le Moal4a109032017-10-11 05:54:25 +0900305 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
306 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900307 sdkp->zones_max_open = 0;
308 } else {
309 /* Host-managed */
310 sdkp->urswrz = buf[4] & 1;
311 sdkp->zones_optimal_open = 0;
312 sdkp->zones_optimal_nonseq = 0;
Damien Le Moal4a109032017-10-11 05:54:25 +0900313 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900314 }
315
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900316 /*
317 * Check for unconstrained reads: host-managed devices with
318 * constrained reads (drives failing read after write pointer)
319 * are not supported.
320 */
321 if (!sdkp->urswrz) {
322 if (sdkp->first_scan)
323 sd_printk(KERN_NOTICE, sdkp,
324 "constrained reads devices are not supported\n");
325 return -ENODEV;
326 }
327
Hannes Reinecke89d94752016-10-18 15:40:34 +0900328 return 0;
329}
330
Damien Le Moale98f42b2017-10-11 05:54:22 +0900331/**
Damien Le Moald2e428e2018-10-12 19:08:41 +0900332 * sd_zbc_check_zones - Check the device capacity and zone sizes
Damien Le Moale98f42b2017-10-11 05:54:22 +0900333 * @sdkp: Target disk
334 *
Damien Le Moald2e428e2018-10-12 19:08:41 +0900335 * Check that the device capacity as reported by READ CAPACITY matches the
336 * max_lba value (plus one)of the report zones command reply. Also check that
337 * all zones of the device have an equal size, only allowing the last zone of
338 * the disk to have a smaller size (runt zone). The zone size must also be a
339 * power of two.
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700340 *
Damien Le Moalf13cff62018-07-03 15:23:58 +0900341 * Returns the zone size in number of blocks upon success or an error code
342 * upon failure.
Damien Le Moale98f42b2017-10-11 05:54:22 +0900343 */
Damien Le Moald9dd7302019-11-11 11:39:22 +0900344static int sd_zbc_check_zones(struct scsi_disk *sdkp, unsigned char *buf,
345 u32 *zblocks)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900346{
Damien Le Moald9dd7302019-11-11 11:39:22 +0900347 size_t buflen;
Damien Le Moal4b433922018-03-02 07:19:28 +0900348 u64 zone_blocks = 0;
Damien Le Moald9dd7302019-11-11 11:39:22 +0900349 sector_t max_lba;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900350 unsigned char *rec;
Damien Le Moal5f832a32018-10-12 19:08:42 +0900351 int ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900352
Damien Le Moald9dd7302019-11-11 11:39:22 +0900353 /* Do a report zone to get max_lba and the size of the first zone */
354 ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);
Damien Le Moal4b433922018-03-02 07:19:28 +0900355 if (ret)
Damien Le Moald9dd7302019-11-11 11:39:22 +0900356 return ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900357
Damien Le Moald2e428e2018-10-12 19:08:41 +0900358 if (sdkp->rc_basis == 0) {
359 /* The max_lba field is the capacity of this device */
360 max_lba = get_unaligned_be64(&buf[8]);
361 if (sdkp->capacity != max_lba + 1) {
362 if (sdkp->first_scan)
363 sd_printk(KERN_WARNING, sdkp,
364 "Changing capacity from %llu to max LBA+1 %llu\n",
365 (unsigned long long)sdkp->capacity,
366 (unsigned long long)max_lba + 1);
367 sdkp->capacity = max_lba + 1;
368 }
369 }
370
Damien Le Moald9dd7302019-11-11 11:39:22 +0900371 /* Parse REPORT ZONES header */
372 buflen = min_t(size_t, get_unaligned_be32(&buf[0]) + 64, SD_BUF_SIZE);
373 rec = buf + 64;
374 zone_blocks = get_unaligned_be64(&rec[8]);
375 if (!zone_blocks || !is_power_of_2(zone_blocks)) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900376 if (sdkp->first_scan)
377 sd_printk(KERN_NOTICE, sdkp,
378 "Devices with non power of 2 zone "
379 "size are not supported\n");
Damien Le Moald9dd7302019-11-11 11:39:22 +0900380 return -ENODEV;
381 }
382
383 if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900384 if (sdkp->first_scan)
385 sd_printk(KERN_NOTICE, sdkp,
386 "Zone size too large\n");
Damien Le Moald9dd7302019-11-11 11:39:22 +0900387 return -EFBIG;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900388 }
389
Damien Le Moald9dd7302019-11-11 11:39:22 +0900390 *zblocks = zone_blocks;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900391
Damien Le Moald9dd7302019-11-11 11:39:22 +0900392 return 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900393}
394
Damien Le Moale98f42b2017-10-11 05:54:22 +0900395int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900396{
Damien Le Moalbf505452018-10-12 19:08:50 +0900397 struct gendisk *disk = sdkp->disk;
398 unsigned int nr_zones;
Damien Le Moal0cdc5852019-07-17 10:51:49 +0900399 u32 zone_blocks = 0;
Bart Van Asschef7053242017-04-24 16:51:14 +0900400 int ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900401
402 if (!sd_is_zoned(sdkp))
403 /*
404 * Device managed or normal SCSI disk,
405 * no special handling required
406 */
407 return 0;
408
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900409 /* Check zoned block device characteristics (unconstrained reads) */
410 ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900411 if (ret)
412 goto err;
413
Hannes Reinecke89d94752016-10-18 15:40:34 +0900414 /*
415 * Check zone size: only devices with a constant zone size (except
416 * an eventual last runt zone) that is a power of 2 are supported.
417 */
Damien Le Moald9dd7302019-11-11 11:39:22 +0900418 ret = sd_zbc_check_zones(sdkp, buf, &zone_blocks);
Damien Le Moal5f832a32018-10-12 19:08:42 +0900419 if (ret != 0)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900420 goto err;
421
422 /* The drive satisfies the kernel restrictions: set it up */
Damien Le Moalbf505452018-10-12 19:08:50 +0900423 blk_queue_chunk_sectors(sdkp->disk->queue,
424 logical_to_sectors(sdkp->device, zone_blocks));
Chaitanya Kulkarnid81e9d42019-08-01 10:26:37 -0700425 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, sdkp->disk->queue);
Damien Le Moalebddd2a2019-09-05 18:51:35 +0900426 blk_queue_required_elevator_features(sdkp->disk->queue,
427 ELEVATOR_F_ZBD_SEQ_WRITE);
Damien Le Moalbf505452018-10-12 19:08:50 +0900428 nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
429
430 /* READ16/WRITE16 is mandatory for ZBC disks */
431 sdkp->device->use_16_for_rw = 1;
432 sdkp->device->use_10_for_rw = 0;
433
434 /*
Damien Le Moal88fc41c2019-01-30 15:54:58 +0900435 * Revalidate the disk zone bitmaps once the block device capacity is
436 * set on the second revalidate execution during disk scan and if
437 * something changed when executing a normal revalidate.
Damien Le Moalbf505452018-10-12 19:08:50 +0900438 */
Damien Le Moal88fc41c2019-01-30 15:54:58 +0900439 if (sdkp->first_scan) {
440 sdkp->zone_blocks = zone_blocks;
441 sdkp->nr_zones = nr_zones;
Damien Le Moalbf505452018-10-12 19:08:50 +0900442 return 0;
Damien Le Moal88fc41c2019-01-30 15:54:58 +0900443 }
444
Damien Le Moalbf505452018-10-12 19:08:50 +0900445 if (sdkp->zone_blocks != zone_blocks ||
446 sdkp->nr_zones != nr_zones ||
447 disk->queue->nr_zones != nr_zones) {
448 ret = blk_revalidate_disk_zones(disk);
449 if (ret != 0)
450 goto err;
451 sdkp->zone_blocks = zone_blocks;
452 sdkp->nr_zones = nr_zones;
453 }
Hannes Reinecke89d94752016-10-18 15:40:34 +0900454
Hannes Reinecke89d94752016-10-18 15:40:34 +0900455 return 0;
456
457err:
458 sdkp->capacity = 0;
459
460 return ret;
461}
462
Hannes Reinecke89d94752016-10-18 15:40:34 +0900463void sd_zbc_print_zones(struct scsi_disk *sdkp)
464{
465 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
466 return;
467
468 if (sdkp->capacity & (sdkp->zone_blocks - 1))
469 sd_printk(KERN_NOTICE, sdkp,
470 "%u zones of %u logical blocks + 1 runt zone\n",
471 sdkp->nr_zones - 1,
472 sdkp->zone_blocks);
473 else
474 sd_printk(KERN_NOTICE, sdkp,
475 "%u zones of %u logical blocks\n",
476 sdkp->nr_zones,
477 sdkp->zone_blocks);
478}