blob: 367614f0e34f825274b3c56aa95c8c219e702198 [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 * Maximum number of zones to get with one report zones command.
109 */
110#define SD_ZBC_REPORT_MAX_ZONES 8192U
111
112/**
113 * Allocate a buffer for report zones reply.
114 * @sdkp: The target disk
115 * @nr_zones: Maximum number of zones to report
116 * @buflen: Size of the buffer allocated
117 *
118 * Try to allocate a reply buffer for the number of requested zones.
119 * The size of the buffer allocated may be smaller than requested to
120 * satify the device constraint (max_hw_sectors, max_segments, etc).
121 *
122 * Return the address of the allocated buffer and update @buflen with
123 * the size of the allocated buffer.
124 */
125static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
126 unsigned int nr_zones, size_t *buflen)
127{
128 struct request_queue *q = sdkp->disk->queue;
129 size_t bufsize;
130 void *buf;
131
132 /*
133 * Report zone buffer size should be at most 64B times the number of
134 * zones requested plus the 64B reply header, but should be at least
135 * SECTOR_SIZE for ATA devices.
136 * Make sure that this size does not exceed the hardware capabilities.
137 * Furthermore, since the report zone command cannot be split, make
138 * sure that the allocated buffer can always be mapped by limiting the
139 * number of pages allocated to the HBA max segments limit.
140 */
141 nr_zones = min(nr_zones, SD_ZBC_REPORT_MAX_ZONES);
142 bufsize = roundup((nr_zones + 1) * 64, 512);
143 bufsize = min_t(size_t, bufsize,
144 queue_max_hw_sectors(q) << SECTOR_SHIFT);
145 bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
146
147 buf = vzalloc(bufsize);
148 if (buf)
149 *buflen = bufsize;
150
151 return buf;
152}
153
Damien Le Moale98f42b2017-10-11 05:54:22 +0900154/**
Christoph Hellwige76239a2018-10-12 19:08:49 +0900155 * sd_zbc_report_zones - Disk report zones operation.
156 * @disk: The target disk
157 * @sector: Start 512B sector of the report
158 * @zones: Array of zone descriptors
159 * @nr_zones: Number of descriptors in the array
Damien Le Moale98f42b2017-10-11 05:54:22 +0900160 *
Christoph Hellwige76239a2018-10-12 19:08:49 +0900161 * Execute a report zones command on the target disk.
Damien Le Moale98f42b2017-10-11 05:54:22 +0900162 */
Christoph Hellwige76239a2018-10-12 19:08:49 +0900163int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
Damien Le Moalbd976e52019-07-01 14:09:16 +0900164 struct blk_zone *zones, unsigned int *nr_zones)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900165{
Christoph Hellwige76239a2018-10-12 19:08:49 +0900166 struct scsi_disk *sdkp = scsi_disk(disk);
Damien Le Moalb091ac62019-07-01 14:09:17 +0900167 unsigned int i, nrz = *nr_zones;
Christoph Hellwige76239a2018-10-12 19:08:49 +0900168 unsigned char *buf;
Damien Le Moalb091ac62019-07-01 14:09:17 +0900169 size_t buflen = 0, offset = 0;
Christoph Hellwige76239a2018-10-12 19:08:49 +0900170 int ret = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900171
172 if (!sd_is_zoned(sdkp))
173 /* Not a zoned device */
Christoph Hellwige76239a2018-10-12 19:08:49 +0900174 return -EOPNOTSUPP;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900175
Damien Le Moalb091ac62019-07-01 14:09:17 +0900176 buf = sd_zbc_alloc_report_buffer(sdkp, nrz, &buflen);
Christoph Hellwige76239a2018-10-12 19:08:49 +0900177 if (!buf)
178 return -ENOMEM;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900179
Christoph Hellwige76239a2018-10-12 19:08:49 +0900180 ret = sd_zbc_do_report_zones(sdkp, buf, buflen,
181 sectors_to_logical(sdkp->device, sector), true);
182 if (ret)
Damien Le Moalb091ac62019-07-01 14:09:17 +0900183 goto out;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900184
Christoph Hellwige76239a2018-10-12 19:08:49 +0900185 nrz = min(nrz, get_unaligned_be32(&buf[0]) / 64);
186 for (i = 0; i < nrz; i++) {
187 offset += 64;
188 sd_zbc_parse_report(sdkp, buf + offset, zones);
189 zones++;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900190 }
Christoph Hellwige76239a2018-10-12 19:08:49 +0900191
192 *nr_zones = nrz;
193
Damien Le Moalb091ac62019-07-01 14:09:17 +0900194out:
195 kvfree(buf);
Christoph Hellwige76239a2018-10-12 19:08:49 +0900196
197 return ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900198}
199
Damien Le Moale98f42b2017-10-11 05:54:22 +0900200/**
201 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
202 * @sdkp: The target disk
203 */
Hannes Reinecke89d94752016-10-18 15:40:34 +0900204static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
205{
206 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
207}
208
Damien Le Moale98f42b2017-10-11 05:54:22 +0900209/**
Damien Le Moale98f42b2017-10-11 05:54:22 +0900210 * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command.
211 * @cmd: the command to setup
Chaitanya Kulkarnid81e9d42019-08-01 10:26:37 -0700212 * @all: Reset all zones control.
Damien Le Moale98f42b2017-10-11 05:54:22 +0900213 *
214 * Called from sd_init_command() for a REQ_OP_ZONE_RESET request.
215 */
Chaitanya Kulkarnid81e9d42019-08-01 10:26:37 -0700216blk_status_t sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd, bool all)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900217{
218 struct request *rq = cmd->request;
219 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
220 sector_t sector = blk_rq_pos(rq);
221 sector_t block = sectors_to_logical(sdkp->device, sector);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900222
223 if (!sd_is_zoned(sdkp))
224 /* Not a zoned device */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100225 return BLK_STS_IOERR;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900226
227 if (sdkp->device->changed)
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100228 return BLK_STS_IOERR;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900229
230 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
231 /* Unaligned request */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100232 return BLK_STS_IOERR;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900233
Hannes Reinecke89d94752016-10-18 15:40:34 +0900234 cmd->cmd_len = 16;
235 memset(cmd->cmnd, 0, cmd->cmd_len);
236 cmd->cmnd[0] = ZBC_OUT;
237 cmd->cmnd[1] = ZO_RESET_WRITE_POINTER;
Chaitanya Kulkarnid81e9d42019-08-01 10:26:37 -0700238 if (all)
239 cmd->cmnd[14] = 0x1;
240 else
241 put_unaligned_be64(block, &cmd->cmnd[2]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900242
243 rq->timeout = SD_TIMEOUT;
244 cmd->sc_data_direction = DMA_NONE;
245 cmd->transfersize = 0;
246 cmd->allowed = 0;
247
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100248 return BLK_STS_OK;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900249}
250
Damien Le Moale98f42b2017-10-11 05:54:22 +0900251/**
Damien Le Moale98f42b2017-10-11 05:54:22 +0900252 * sd_zbc_complete - ZBC command post processing.
253 * @cmd: Completed command
254 * @good_bytes: Command reply bytes
255 * @sshdr: command sense header
256 *
257 * Called from sd_done(). Process report zones reply and handle reset zone
258 * and write commands errors.
259 */
260void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
Hannes Reinecke89d94752016-10-18 15:40:34 +0900261 struct scsi_sense_hdr *sshdr)
262{
263 int result = cmd->result;
264 struct request *rq = cmd->request;
265
266 switch (req_op(rq)) {
Damien Le Moal868ed5a2017-04-24 16:51:15 +0900267 case REQ_OP_ZONE_RESET:
Chaitanya Kulkarnid81e9d42019-08-01 10:26:37 -0700268 case REQ_OP_ZONE_RESET_ALL:
Damien Le Moal868ed5a2017-04-24 16:51:15 +0900269
270 if (result &&
271 sshdr->sense_key == ILLEGAL_REQUEST &&
272 sshdr->asc == 0x24)
273 /*
274 * INVALID FIELD IN CDB error: reset of a conventional
275 * zone was attempted. Nothing to worry about, so be
276 * quiet about the error.
277 */
278 rq->rq_flags |= RQF_QUIET;
279 break;
280
Hannes Reinecke89d94752016-10-18 15:40:34 +0900281 case REQ_OP_WRITE:
Christoph Hellwig02d26102017-04-05 19:21:02 +0200282 case REQ_OP_WRITE_ZEROES:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900283 case REQ_OP_WRITE_SAME:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900284 break;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900285 }
286}
287
288/**
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900289 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
Damien Le Moale98f42b2017-10-11 05:54:22 +0900290 * @sdkp: Target disk
291 * @buf: Buffer where to store the VPD page data
292 *
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900293 * Read VPD page B6, get information and check that reads are unconstrained.
Hannes Reinecke89d94752016-10-18 15:40:34 +0900294 */
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900295static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
296 unsigned char *buf)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900297{
298
299 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
300 sd_printk(KERN_NOTICE, sdkp,
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900301 "Read zoned characteristics VPD page failed\n");
Hannes Reinecke89d94752016-10-18 15:40:34 +0900302 return -ENODEV;
303 }
304
305 if (sdkp->device->type != TYPE_ZBC) {
306 /* Host-aware */
307 sdkp->urswrz = 1;
Damien Le Moal4a109032017-10-11 05:54:25 +0900308 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
309 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900310 sdkp->zones_max_open = 0;
311 } else {
312 /* Host-managed */
313 sdkp->urswrz = buf[4] & 1;
314 sdkp->zones_optimal_open = 0;
315 sdkp->zones_optimal_nonseq = 0;
Damien Le Moal4a109032017-10-11 05:54:25 +0900316 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900317 }
318
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900319 /*
320 * Check for unconstrained reads: host-managed devices with
321 * constrained reads (drives failing read after write pointer)
322 * are not supported.
323 */
324 if (!sdkp->urswrz) {
325 if (sdkp->first_scan)
326 sd_printk(KERN_NOTICE, sdkp,
327 "constrained reads devices are not supported\n");
328 return -ENODEV;
329 }
330
Hannes Reinecke89d94752016-10-18 15:40:34 +0900331 return 0;
332}
333
Damien Le Moale98f42b2017-10-11 05:54:22 +0900334/**
Damien Le Moald2e428e2018-10-12 19:08:41 +0900335 * sd_zbc_check_zones - Check the device capacity and zone sizes
Damien Le Moale98f42b2017-10-11 05:54:22 +0900336 * @sdkp: Target disk
337 *
Damien Le Moald2e428e2018-10-12 19:08:41 +0900338 * Check that the device capacity as reported by READ CAPACITY matches the
339 * max_lba value (plus one)of the report zones command reply. Also check that
340 * all zones of the device have an equal size, only allowing the last zone of
341 * the disk to have a smaller size (runt zone). The zone size must also be a
342 * power of two.
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700343 *
Damien Le Moalf13cff62018-07-03 15:23:58 +0900344 * Returns the zone size in number of blocks upon success or an error code
345 * upon failure.
Damien Le Moale98f42b2017-10-11 05:54:22 +0900346 */
Damien Le Moal5f832a32018-10-12 19:08:42 +0900347static int sd_zbc_check_zones(struct scsi_disk *sdkp, u32 *zblocks)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900348{
Damien Le Moalb091ac62019-07-01 14:09:17 +0900349 size_t bufsize, buflen;
350 unsigned int noio_flag;
Damien Le Moal4b433922018-03-02 07:19:28 +0900351 u64 zone_blocks = 0;
Damien Le Moald2e428e2018-10-12 19:08:41 +0900352 sector_t max_lba, block = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900353 unsigned char *buf;
354 unsigned char *rec;
Damien Le Moal5f832a32018-10-12 19:08:42 +0900355 int ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900356 u8 same;
357
Damien Le Moalb091ac62019-07-01 14:09:17 +0900358 /* Do all memory allocations as if GFP_NOIO was specified */
359 noio_flag = memalloc_noio_save();
360
Hannes Reinecke89d94752016-10-18 15:40:34 +0900361 /* Get a buffer */
Damien Le Moalb091ac62019-07-01 14:09:17 +0900362 buf = sd_zbc_alloc_report_buffer(sdkp, SD_ZBC_REPORT_MAX_ZONES,
363 &bufsize);
364 if (!buf) {
365 ret = -ENOMEM;
366 goto out;
367 }
Hannes Reinecke89d94752016-10-18 15:40:34 +0900368
Damien Le Moald2e428e2018-10-12 19:08:41 +0900369 /* Do a report zone to get max_lba and the same field */
Damien Le Moalb091ac62019-07-01 14:09:17 +0900370 ret = sd_zbc_do_report_zones(sdkp, buf, bufsize, 0, false);
Damien Le Moal4b433922018-03-02 07:19:28 +0900371 if (ret)
372 goto out_free;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900373
Damien Le Moald2e428e2018-10-12 19:08:41 +0900374 if (sdkp->rc_basis == 0) {
375 /* The max_lba field is the capacity of this device */
376 max_lba = get_unaligned_be64(&buf[8]);
377 if (sdkp->capacity != max_lba + 1) {
378 if (sdkp->first_scan)
379 sd_printk(KERN_WARNING, sdkp,
380 "Changing capacity from %llu to max LBA+1 %llu\n",
381 (unsigned long long)sdkp->capacity,
382 (unsigned long long)max_lba + 1);
383 sdkp->capacity = max_lba + 1;
384 }
385 }
386
387 /*
388 * Check same field: for any value other than 0, we know that all zones
389 * have the same size.
390 */
Hannes Reinecke89d94752016-10-18 15:40:34 +0900391 same = buf[4] & 0x0f;
392 if (same > 0) {
393 rec = &buf[64];
394 zone_blocks = get_unaligned_be64(&rec[8]);
395 goto out;
396 }
397
398 /*
399 * Check the size of all zones: all zones must be of
400 * equal size, except the last zone which can be smaller
401 * than other zones.
402 */
403 do {
404
405 /* Parse REPORT ZONES header */
Damien Le Moalb091ac62019-07-01 14:09:17 +0900406 buflen = min_t(size_t, get_unaligned_be32(&buf[0]) + 64,
407 bufsize);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900408 rec = buf + 64;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900409
410 /* Parse zone descriptors */
Damien Le Moalb091ac62019-07-01 14:09:17 +0900411 while (rec < buf + buflen) {
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700412 u64 this_zone_blocks = get_unaligned_be64(&rec[8]);
413
414 if (zone_blocks == 0) {
415 zone_blocks = this_zone_blocks;
416 } else if (this_zone_blocks != zone_blocks &&
417 (block + this_zone_blocks < sdkp->capacity
418 || this_zone_blocks > zone_blocks)) {
Damien Le Moal3aadbe22018-05-31 17:42:40 +0900419 zone_blocks = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900420 goto out;
421 }
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700422 block += this_zone_blocks;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900423 rec += 64;
424 }
425
426 if (block < sdkp->capacity) {
Damien Le Moalb091ac62019-07-01 14:09:17 +0900427 ret = sd_zbc_do_report_zones(sdkp, buf, bufsize, block,
428 true);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900429 if (ret)
Damien Le Moal4b433922018-03-02 07:19:28 +0900430 goto out_free;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900431 }
432
433 } while (block < sdkp->capacity);
434
Hannes Reinecke89d94752016-10-18 15:40:34 +0900435out:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900436 if (!zone_blocks) {
437 if (sdkp->first_scan)
438 sd_printk(KERN_NOTICE, sdkp,
439 "Devices with non constant zone "
440 "size are not supported\n");
Damien Le Moal4b433922018-03-02 07:19:28 +0900441 ret = -ENODEV;
442 } else if (!is_power_of_2(zone_blocks)) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900443 if (sdkp->first_scan)
444 sd_printk(KERN_NOTICE, sdkp,
445 "Devices with non power of 2 zone "
446 "size are not supported\n");
Damien Le Moal4b433922018-03-02 07:19:28 +0900447 ret = -ENODEV;
448 } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900449 if (sdkp->first_scan)
450 sd_printk(KERN_NOTICE, sdkp,
451 "Zone size too large\n");
Damien Le Moal5f832a32018-10-12 19:08:42 +0900452 ret = -EFBIG;
Damien Le Moal4b433922018-03-02 07:19:28 +0900453 } else {
Damien Le Moal5f832a32018-10-12 19:08:42 +0900454 *zblocks = zone_blocks;
455 ret = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900456 }
457
Damien Le Moal4b433922018-03-02 07:19:28 +0900458out_free:
Damien Le Moalb091ac62019-07-01 14:09:17 +0900459 memalloc_noio_restore(noio_flag);
460 kvfree(buf);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900461
Damien Le Moal4b433922018-03-02 07:19:28 +0900462 return ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900463}
464
Damien Le Moale98f42b2017-10-11 05:54:22 +0900465int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900466{
Damien Le Moalbf505452018-10-12 19:08:50 +0900467 struct gendisk *disk = sdkp->disk;
468 unsigned int nr_zones;
Damien Le Moal0cdc5852019-07-17 10:51:49 +0900469 u32 zone_blocks = 0;
Bart Van Asschef7053242017-04-24 16:51:14 +0900470 int ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900471
472 if (!sd_is_zoned(sdkp))
473 /*
474 * Device managed or normal SCSI disk,
475 * no special handling required
476 */
477 return 0;
478
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900479 /* Check zoned block device characteristics (unconstrained reads) */
480 ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900481 if (ret)
482 goto err;
483
Hannes Reinecke89d94752016-10-18 15:40:34 +0900484 /*
485 * Check zone size: only devices with a constant zone size (except
486 * an eventual last runt zone) that is a power of 2 are supported.
487 */
Damien Le Moal5f832a32018-10-12 19:08:42 +0900488 ret = sd_zbc_check_zones(sdkp, &zone_blocks);
489 if (ret != 0)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900490 goto err;
491
492 /* The drive satisfies the kernel restrictions: set it up */
Damien Le Moalbf505452018-10-12 19:08:50 +0900493 blk_queue_chunk_sectors(sdkp->disk->queue,
494 logical_to_sectors(sdkp->device, zone_blocks));
Chaitanya Kulkarnid81e9d42019-08-01 10:26:37 -0700495 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, sdkp->disk->queue);
Damien Le Moalbf505452018-10-12 19:08:50 +0900496 nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
497
498 /* READ16/WRITE16 is mandatory for ZBC disks */
499 sdkp->device->use_16_for_rw = 1;
500 sdkp->device->use_10_for_rw = 0;
501
502 /*
Damien Le Moal88fc41c2019-01-30 15:54:58 +0900503 * Revalidate the disk zone bitmaps once the block device capacity is
504 * set on the second revalidate execution during disk scan and if
505 * something changed when executing a normal revalidate.
Damien Le Moalbf505452018-10-12 19:08:50 +0900506 */
Damien Le Moal88fc41c2019-01-30 15:54:58 +0900507 if (sdkp->first_scan) {
508 sdkp->zone_blocks = zone_blocks;
509 sdkp->nr_zones = nr_zones;
Damien Le Moalbf505452018-10-12 19:08:50 +0900510 return 0;
Damien Le Moal88fc41c2019-01-30 15:54:58 +0900511 }
512
Damien Le Moalbf505452018-10-12 19:08:50 +0900513 if (sdkp->zone_blocks != zone_blocks ||
514 sdkp->nr_zones != nr_zones ||
515 disk->queue->nr_zones != nr_zones) {
516 ret = blk_revalidate_disk_zones(disk);
517 if (ret != 0)
518 goto err;
519 sdkp->zone_blocks = zone_blocks;
520 sdkp->nr_zones = nr_zones;
521 }
Hannes Reinecke89d94752016-10-18 15:40:34 +0900522
Hannes Reinecke89d94752016-10-18 15:40:34 +0900523 return 0;
524
525err:
526 sdkp->capacity = 0;
527
528 return ret;
529}
530
Hannes Reinecke89d94752016-10-18 15:40:34 +0900531void sd_zbc_print_zones(struct scsi_disk *sdkp)
532{
533 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
534 return;
535
536 if (sdkp->capacity & (sdkp->zone_blocks - 1))
537 sd_printk(KERN_NOTICE, sdkp,
538 "%u zones of %u logical blocks + 1 runt zone\n",
539 sdkp->nr_zones - 1,
540 sdkp->zone_blocks);
541 else
542 sd_printk(KERN_NOTICE, sdkp,
543 "%u zones of %u logical blocks\n",
544 sdkp->nr_zones,
545 sdkp->zone_blocks);
546}