blob: 7334024b64f161f0a512548eb86c45a201cf523e [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>
12
13#include <asm/unaligned.h>
14
15#include <scsi/scsi.h>
16#include <scsi/scsi_cmnd.h>
Hannes Reinecke89d94752016-10-18 15:40:34 +090017
18#include "sd.h"
Hannes Reinecke89d94752016-10-18 15:40:34 +090019
20/**
Damien Le Moale98f42b2017-10-11 05:54:22 +090021 * sd_zbc_parse_report - Convert a zone descriptor to a struct blk_zone,
22 * @sdkp: The disk the report originated from
23 * @buf: Address of the report zone descriptor
24 * @zone: the destination zone structure
25 *
26 * All LBA sized values are converted to 512B sectors unit.
Hannes Reinecke89d94752016-10-18 15:40:34 +090027 */
Damien Le Moale98f42b2017-10-11 05:54:22 +090028static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
Hannes Reinecke89d94752016-10-18 15:40:34 +090029 struct blk_zone *zone)
30{
31 struct scsi_device *sdp = sdkp->device;
32
33 memset(zone, 0, sizeof(struct blk_zone));
34
35 zone->type = buf[0] & 0x0f;
36 zone->cond = (buf[1] >> 4) & 0xf;
37 if (buf[1] & 0x01)
38 zone->reset = 1;
39 if (buf[1] & 0x02)
40 zone->non_seq = 1;
41
42 zone->len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
43 zone->start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
44 zone->wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
45 if (zone->type != ZBC_ZONE_TYPE_CONV &&
46 zone->cond == ZBC_ZONE_COND_FULL)
47 zone->wp = zone->start + zone->len;
48}
49
50/**
Christoph Hellwige76239a2018-10-12 19:08:49 +090051 * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
Damien Le Moale98f42b2017-10-11 05:54:22 +090052 * @sdkp: The target disk
53 * @buf: Buffer to use for the reply
54 * @buflen: the buffer size
55 * @lba: Start LBA of the report
Damien Le Moald2e428e2018-10-12 19:08:41 +090056 * @partial: Do partial report
Damien Le Moale98f42b2017-10-11 05:54:22 +090057 *
58 * For internal use during device validation.
Damien Le Moald2e428e2018-10-12 19:08:41 +090059 * Using partial=true can significantly speed up execution of a report zones
60 * command because the disk does not have to count all possible report matching
61 * zones and will only report the count of zones fitting in the command reply
62 * buffer.
Hannes Reinecke89d94752016-10-18 15:40:34 +090063 */
Christoph Hellwige76239a2018-10-12 19:08:49 +090064static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
65 unsigned int buflen, sector_t lba,
66 bool partial)
Hannes Reinecke89d94752016-10-18 15:40:34 +090067{
68 struct scsi_device *sdp = sdkp->device;
69 const int timeout = sdp->request_queue->rq_timeout;
70 struct scsi_sense_hdr sshdr;
71 unsigned char cmd[16];
72 unsigned int rep_len;
73 int result;
74
75 memset(cmd, 0, 16);
76 cmd[0] = ZBC_IN;
77 cmd[1] = ZI_REPORT_ZONES;
78 put_unaligned_be64(lba, &cmd[2]);
79 put_unaligned_be32(buflen, &cmd[10]);
Damien Le Moald2e428e2018-10-12 19:08:41 +090080 if (partial)
81 cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
Hannes Reinecke89d94752016-10-18 15:40:34 +090082 memset(buf, 0, buflen);
83
84 result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
85 buf, buflen, &sshdr,
86 timeout, SD_MAX_RETRIES, NULL);
87 if (result) {
88 sd_printk(KERN_ERR, sdkp,
89 "REPORT ZONES lba %llu failed with %d/%d\n",
90 (unsigned long long)lba,
91 host_byte(result), driver_byte(result));
92 return -EIO;
93 }
94
95 rep_len = get_unaligned_be32(&buf[0]);
96 if (rep_len < 64) {
97 sd_printk(KERN_ERR, sdkp,
98 "REPORT ZONES report invalid length %u\n",
99 rep_len);
100 return -EIO;
101 }
102
103 return 0;
104}
105
Damien Le Moale98f42b2017-10-11 05:54:22 +0900106/**
Christoph Hellwige76239a2018-10-12 19:08:49 +0900107 * sd_zbc_report_zones - Disk report zones operation.
108 * @disk: The target disk
109 * @sector: Start 512B sector of the report
110 * @zones: Array of zone descriptors
111 * @nr_zones: Number of descriptors in the array
112 * @gfp_mask: Memory allocation mask
Damien Le Moale98f42b2017-10-11 05:54:22 +0900113 *
Christoph Hellwige76239a2018-10-12 19:08:49 +0900114 * Execute a report zones command on the target disk.
Damien Le Moale98f42b2017-10-11 05:54:22 +0900115 */
Christoph Hellwige76239a2018-10-12 19:08:49 +0900116int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
117 struct blk_zone *zones, unsigned int *nr_zones,
118 gfp_t gfp_mask)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900119{
Christoph Hellwige76239a2018-10-12 19:08:49 +0900120 struct scsi_disk *sdkp = scsi_disk(disk);
121 unsigned int i, buflen, nrz = *nr_zones;
122 unsigned char *buf;
123 size_t offset = 0;
124 int ret = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900125
126 if (!sd_is_zoned(sdkp))
127 /* Not a zoned device */
Christoph Hellwige76239a2018-10-12 19:08:49 +0900128 return -EOPNOTSUPP;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900129
Christoph Hellwige76239a2018-10-12 19:08:49 +0900130 /*
Masato Suzuki515ce602019-02-14 15:01:18 +0900131 * Get a reply buffer for the number of requested zones plus a header,
132 * without exceeding the device maximum command size. For ATA disks,
133 * buffers must be aligned to 512B.
Christoph Hellwige76239a2018-10-12 19:08:49 +0900134 */
Masato Suzuki515ce602019-02-14 15:01:18 +0900135 buflen = min(queue_max_hw_sectors(disk->queue) << 9,
136 roundup((nrz + 1) * 64, 512));
Christoph Hellwige76239a2018-10-12 19:08:49 +0900137 buf = kmalloc(buflen, gfp_mask);
138 if (!buf)
139 return -ENOMEM;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900140
Christoph Hellwige76239a2018-10-12 19:08:49 +0900141 ret = sd_zbc_do_report_zones(sdkp, buf, buflen,
142 sectors_to_logical(sdkp->device, sector), true);
143 if (ret)
144 goto out_free_buf;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900145
Christoph Hellwige76239a2018-10-12 19:08:49 +0900146 nrz = min(nrz, get_unaligned_be32(&buf[0]) / 64);
147 for (i = 0; i < nrz; i++) {
148 offset += 64;
149 sd_zbc_parse_report(sdkp, buf + offset, zones);
150 zones++;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900151 }
Christoph Hellwige76239a2018-10-12 19:08:49 +0900152
153 *nr_zones = nrz;
154
155out_free_buf:
156 kfree(buf);
157
158 return ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900159}
160
Damien Le Moale98f42b2017-10-11 05:54:22 +0900161/**
162 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
163 * @sdkp: The target disk
164 */
Hannes Reinecke89d94752016-10-18 15:40:34 +0900165static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
166{
167 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
168}
169
Damien Le Moale98f42b2017-10-11 05:54:22 +0900170/**
Damien Le Moale98f42b2017-10-11 05:54:22 +0900171 * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command.
172 * @cmd: the command to setup
173 *
174 * Called from sd_init_command() for a REQ_OP_ZONE_RESET request.
175 */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100176blk_status_t sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900177{
178 struct request *rq = cmd->request;
179 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
180 sector_t sector = blk_rq_pos(rq);
181 sector_t block = sectors_to_logical(sdkp->device, sector);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900182
183 if (!sd_is_zoned(sdkp))
184 /* Not a zoned device */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100185 return BLK_STS_IOERR;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900186
187 if (sdkp->device->changed)
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100188 return BLK_STS_IOERR;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900189
190 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
191 /* Unaligned request */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100192 return BLK_STS_IOERR;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900193
Hannes Reinecke89d94752016-10-18 15:40:34 +0900194 cmd->cmd_len = 16;
195 memset(cmd->cmnd, 0, cmd->cmd_len);
196 cmd->cmnd[0] = ZBC_OUT;
197 cmd->cmnd[1] = ZO_RESET_WRITE_POINTER;
198 put_unaligned_be64(block, &cmd->cmnd[2]);
199
200 rq->timeout = SD_TIMEOUT;
201 cmd->sc_data_direction = DMA_NONE;
202 cmd->transfersize = 0;
203 cmd->allowed = 0;
204
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100205 return BLK_STS_OK;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900206}
207
Damien Le Moale98f42b2017-10-11 05:54:22 +0900208/**
Damien Le Moale98f42b2017-10-11 05:54:22 +0900209 * sd_zbc_complete - ZBC command post processing.
210 * @cmd: Completed command
211 * @good_bytes: Command reply bytes
212 * @sshdr: command sense header
213 *
214 * Called from sd_done(). Process report zones reply and handle reset zone
215 * and write commands errors.
216 */
217void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
Hannes Reinecke89d94752016-10-18 15:40:34 +0900218 struct scsi_sense_hdr *sshdr)
219{
220 int result = cmd->result;
221 struct request *rq = cmd->request;
222
223 switch (req_op(rq)) {
Damien Le Moal868ed5a2017-04-24 16:51:15 +0900224 case REQ_OP_ZONE_RESET:
225
226 if (result &&
227 sshdr->sense_key == ILLEGAL_REQUEST &&
228 sshdr->asc == 0x24)
229 /*
230 * INVALID FIELD IN CDB error: reset of a conventional
231 * zone was attempted. Nothing to worry about, so be
232 * quiet about the error.
233 */
234 rq->rq_flags |= RQF_QUIET;
235 break;
236
Hannes Reinecke89d94752016-10-18 15:40:34 +0900237 case REQ_OP_WRITE:
Christoph Hellwig02d26102017-04-05 19:21:02 +0200238 case REQ_OP_WRITE_ZEROES:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900239 case REQ_OP_WRITE_SAME:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900240 break;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900241 }
242}
243
244/**
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900245 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
Damien Le Moale98f42b2017-10-11 05:54:22 +0900246 * @sdkp: Target disk
247 * @buf: Buffer where to store the VPD page data
248 *
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900249 * Read VPD page B6, get information and check that reads are unconstrained.
Hannes Reinecke89d94752016-10-18 15:40:34 +0900250 */
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900251static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
252 unsigned char *buf)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900253{
254
255 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
256 sd_printk(KERN_NOTICE, sdkp,
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900257 "Read zoned characteristics VPD page failed\n");
Hannes Reinecke89d94752016-10-18 15:40:34 +0900258 return -ENODEV;
259 }
260
261 if (sdkp->device->type != TYPE_ZBC) {
262 /* Host-aware */
263 sdkp->urswrz = 1;
Damien Le Moal4a109032017-10-11 05:54:25 +0900264 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
265 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900266 sdkp->zones_max_open = 0;
267 } else {
268 /* Host-managed */
269 sdkp->urswrz = buf[4] & 1;
270 sdkp->zones_optimal_open = 0;
271 sdkp->zones_optimal_nonseq = 0;
Damien Le Moal4a109032017-10-11 05:54:25 +0900272 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900273 }
274
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900275 /*
276 * Check for unconstrained reads: host-managed devices with
277 * constrained reads (drives failing read after write pointer)
278 * are not supported.
279 */
280 if (!sdkp->urswrz) {
281 if (sdkp->first_scan)
282 sd_printk(KERN_NOTICE, sdkp,
283 "constrained reads devices are not supported\n");
284 return -ENODEV;
285 }
286
Hannes Reinecke89d94752016-10-18 15:40:34 +0900287 return 0;
288}
289
Damien Le Moale8c77ec2017-10-11 05:54:24 +0900290#define SD_ZBC_BUF_SIZE 131072U
Hannes Reinecke89d94752016-10-18 15:40:34 +0900291
Damien Le Moale98f42b2017-10-11 05:54:22 +0900292/**
Damien Le Moald2e428e2018-10-12 19:08:41 +0900293 * sd_zbc_check_zones - Check the device capacity and zone sizes
Damien Le Moale98f42b2017-10-11 05:54:22 +0900294 * @sdkp: Target disk
295 *
Damien Le Moald2e428e2018-10-12 19:08:41 +0900296 * Check that the device capacity as reported by READ CAPACITY matches the
297 * max_lba value (plus one)of the report zones command reply. Also check that
298 * all zones of the device have an equal size, only allowing the last zone of
299 * the disk to have a smaller size (runt zone). The zone size must also be a
300 * power of two.
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700301 *
Damien Le Moalf13cff62018-07-03 15:23:58 +0900302 * Returns the zone size in number of blocks upon success or an error code
303 * upon failure.
Damien Le Moale98f42b2017-10-11 05:54:22 +0900304 */
Damien Le Moal5f832a32018-10-12 19:08:42 +0900305static int sd_zbc_check_zones(struct scsi_disk *sdkp, u32 *zblocks)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900306{
Damien Le Moal4b433922018-03-02 07:19:28 +0900307 u64 zone_blocks = 0;
Damien Le Moald2e428e2018-10-12 19:08:41 +0900308 sector_t max_lba, block = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900309 unsigned char *buf;
310 unsigned char *rec;
311 unsigned int buf_len;
312 unsigned int list_length;
Damien Le Moal5f832a32018-10-12 19:08:42 +0900313 int ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900314 u8 same;
315
Hannes Reinecke89d94752016-10-18 15:40:34 +0900316 /* Get a buffer */
317 buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
318 if (!buf)
319 return -ENOMEM;
320
Damien Le Moald2e428e2018-10-12 19:08:41 +0900321 /* Do a report zone to get max_lba and the same field */
Christoph Hellwige76239a2018-10-12 19:08:49 +0900322 ret = sd_zbc_do_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0, false);
Damien Le Moal4b433922018-03-02 07:19:28 +0900323 if (ret)
324 goto out_free;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900325
Damien Le Moald2e428e2018-10-12 19:08:41 +0900326 if (sdkp->rc_basis == 0) {
327 /* The max_lba field is the capacity of this device */
328 max_lba = get_unaligned_be64(&buf[8]);
329 if (sdkp->capacity != max_lba + 1) {
330 if (sdkp->first_scan)
331 sd_printk(KERN_WARNING, sdkp,
332 "Changing capacity from %llu to max LBA+1 %llu\n",
333 (unsigned long long)sdkp->capacity,
334 (unsigned long long)max_lba + 1);
335 sdkp->capacity = max_lba + 1;
336 }
337 }
338
339 /*
340 * Check same field: for any value other than 0, we know that all zones
341 * have the same size.
342 */
Hannes Reinecke89d94752016-10-18 15:40:34 +0900343 same = buf[4] & 0x0f;
344 if (same > 0) {
345 rec = &buf[64];
346 zone_blocks = get_unaligned_be64(&rec[8]);
347 goto out;
348 }
349
350 /*
351 * Check the size of all zones: all zones must be of
352 * equal size, except the last zone which can be smaller
353 * than other zones.
354 */
355 do {
356
357 /* Parse REPORT ZONES header */
358 list_length = get_unaligned_be32(&buf[0]) + 64;
359 rec = buf + 64;
Damien Le Moale8c77ec2017-10-11 05:54:24 +0900360 buf_len = min(list_length, SD_ZBC_BUF_SIZE);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900361
362 /* Parse zone descriptors */
363 while (rec < buf + buf_len) {
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700364 u64 this_zone_blocks = get_unaligned_be64(&rec[8]);
365
366 if (zone_blocks == 0) {
367 zone_blocks = this_zone_blocks;
368 } else if (this_zone_blocks != zone_blocks &&
369 (block + this_zone_blocks < sdkp->capacity
370 || this_zone_blocks > zone_blocks)) {
Damien Le Moal3aadbe22018-05-31 17:42:40 +0900371 zone_blocks = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900372 goto out;
373 }
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700374 block += this_zone_blocks;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900375 rec += 64;
376 }
377
378 if (block < sdkp->capacity) {
Christoph Hellwige76239a2018-10-12 19:08:49 +0900379 ret = sd_zbc_do_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE,
380 block, true);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900381 if (ret)
Damien Le Moal4b433922018-03-02 07:19:28 +0900382 goto out_free;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900383 }
384
385 } while (block < sdkp->capacity);
386
Hannes Reinecke89d94752016-10-18 15:40:34 +0900387out:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900388 if (!zone_blocks) {
389 if (sdkp->first_scan)
390 sd_printk(KERN_NOTICE, sdkp,
391 "Devices with non constant zone "
392 "size are not supported\n");
Damien Le Moal4b433922018-03-02 07:19:28 +0900393 ret = -ENODEV;
394 } else if (!is_power_of_2(zone_blocks)) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900395 if (sdkp->first_scan)
396 sd_printk(KERN_NOTICE, sdkp,
397 "Devices with non power of 2 zone "
398 "size are not supported\n");
Damien Le Moal4b433922018-03-02 07:19:28 +0900399 ret = -ENODEV;
400 } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900401 if (sdkp->first_scan)
402 sd_printk(KERN_NOTICE, sdkp,
403 "Zone size too large\n");
Damien Le Moal5f832a32018-10-12 19:08:42 +0900404 ret = -EFBIG;
Damien Le Moal4b433922018-03-02 07:19:28 +0900405 } else {
Damien Le Moal5f832a32018-10-12 19:08:42 +0900406 *zblocks = zone_blocks;
407 ret = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900408 }
409
Damien Le Moal4b433922018-03-02 07:19:28 +0900410out_free:
411 kfree(buf);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900412
Damien Le Moal4b433922018-03-02 07:19:28 +0900413 return ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900414}
415
Damien Le Moale98f42b2017-10-11 05:54:22 +0900416int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900417{
Damien Le Moalbf505452018-10-12 19:08:50 +0900418 struct gendisk *disk = sdkp->disk;
419 unsigned int nr_zones;
Damien Le Moal5f832a32018-10-12 19:08:42 +0900420 u32 zone_blocks;
Bart Van Asschef7053242017-04-24 16:51:14 +0900421 int ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900422
423 if (!sd_is_zoned(sdkp))
424 /*
425 * Device managed or normal SCSI disk,
426 * no special handling required
427 */
428 return 0;
429
Damien Le Moal7f9d35d2018-10-12 19:08:40 +0900430 /* Check zoned block device characteristics (unconstrained reads) */
431 ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900432 if (ret)
433 goto err;
434
Hannes Reinecke89d94752016-10-18 15:40:34 +0900435 /*
436 * Check zone size: only devices with a constant zone size (except
437 * an eventual last runt zone) that is a power of 2 are supported.
438 */
Damien Le Moal5f832a32018-10-12 19:08:42 +0900439 ret = sd_zbc_check_zones(sdkp, &zone_blocks);
440 if (ret != 0)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900441 goto err;
442
443 /* The drive satisfies the kernel restrictions: set it up */
Damien Le Moalbf505452018-10-12 19:08:50 +0900444 blk_queue_chunk_sectors(sdkp->disk->queue,
445 logical_to_sectors(sdkp->device, zone_blocks));
446 nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
447
448 /* READ16/WRITE16 is mandatory for ZBC disks */
449 sdkp->device->use_16_for_rw = 1;
450 sdkp->device->use_10_for_rw = 0;
451
452 /*
Damien Le Moal88fc41c2019-01-30 15:54:58 +0900453 * Revalidate the disk zone bitmaps once the block device capacity is
454 * set on the second revalidate execution during disk scan and if
455 * something changed when executing a normal revalidate.
Damien Le Moalbf505452018-10-12 19:08:50 +0900456 */
Damien Le Moal88fc41c2019-01-30 15:54:58 +0900457 if (sdkp->first_scan) {
458 sdkp->zone_blocks = zone_blocks;
459 sdkp->nr_zones = nr_zones;
Damien Le Moalbf505452018-10-12 19:08:50 +0900460 return 0;
Damien Le Moal88fc41c2019-01-30 15:54:58 +0900461 }
462
Damien Le Moalbf505452018-10-12 19:08:50 +0900463 if (sdkp->zone_blocks != zone_blocks ||
464 sdkp->nr_zones != nr_zones ||
465 disk->queue->nr_zones != nr_zones) {
466 ret = blk_revalidate_disk_zones(disk);
467 if (ret != 0)
468 goto err;
469 sdkp->zone_blocks = zone_blocks;
470 sdkp->nr_zones = nr_zones;
471 }
Hannes Reinecke89d94752016-10-18 15:40:34 +0900472
Hannes Reinecke89d94752016-10-18 15:40:34 +0900473 return 0;
474
475err:
476 sdkp->capacity = 0;
477
478 return ret;
479}
480
Hannes Reinecke89d94752016-10-18 15:40:34 +0900481void sd_zbc_print_zones(struct scsi_disk *sdkp)
482{
483 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
484 return;
485
486 if (sdkp->capacity & (sdkp->zone_blocks - 1))
487 sd_printk(KERN_NOTICE, sdkp,
488 "%u zones of %u logical blocks + 1 runt zone\n",
489 sdkp->nr_zones - 1,
490 sdkp->zone_blocks);
491 else
492 sd_printk(KERN_NOTICE, sdkp,
493 "%u zones of %u logical blocks\n",
494 sdkp->nr_zones,
495 sdkp->zone_blocks);
496}