1 // SPDX-License-Identifier: GPL-2.0-only
3 * SCSI Zoned Block commands
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>
11 #include <linux/blkdev.h>
13 #include <asm/unaligned.h>
15 #include <scsi/scsi.h>
16 #include <scsi/scsi_cmnd.h>
21 * 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
26 * All LBA sized values are converted to 512B sectors unit.
28 static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
29 struct blk_zone *zone)
31 struct scsi_device *sdp = sdkp->device;
33 memset(zone, 0, sizeof(struct blk_zone));
35 zone->type = buf[0] & 0x0f;
36 zone->cond = (buf[1] >> 4) & 0xf;
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;
51 * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
52 * @sdkp: The target disk
53 * @buf: Buffer to use for the reply
54 * @buflen: the buffer size
55 * @lba: Start LBA of the report
56 * @partial: Do partial report
58 * For internal use during device validation.
59 * 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
64 static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
65 unsigned int buflen, sector_t lba,
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];
77 cmd[1] = ZI_REPORT_ZONES;
78 put_unaligned_be64(lba, &cmd[2]);
79 put_unaligned_be32(buflen, &cmd[10]);
81 cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
82 memset(buf, 0, buflen);
84 result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
86 timeout, SD_MAX_RETRIES, NULL);
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));
95 rep_len = get_unaligned_be32(&buf[0]);
97 sd_printk(KERN_ERR, sdkp,
98 "REPORT ZONES report invalid length %u\n",
107 * 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
114 * Execute a report zones command on the target disk.
116 int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
117 struct blk_zone *zones, unsigned int *nr_zones,
120 struct scsi_disk *sdkp = scsi_disk(disk);
121 unsigned int i, buflen, nrz = *nr_zones;
126 if (!sd_is_zoned(sdkp))
127 /* Not a zoned device */
131 * 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.
135 buflen = min(queue_max_hw_sectors(disk->queue) << 9,
136 roundup((nrz + 1) * 64, 512));
137 buf = kmalloc(buflen, gfp_mask);
141 ret = sd_zbc_do_report_zones(sdkp, buf, buflen,
142 sectors_to_logical(sdkp->device, sector), true);
146 nrz = min(nrz, get_unaligned_be32(&buf[0]) / 64);
147 for (i = 0; i < nrz; i++) {
149 sd_zbc_parse_report(sdkp, buf + offset, zones);
162 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
163 * @sdkp: The target disk
165 static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
167 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
171 * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command.
172 * @cmd: the command to setup
174 * Called from sd_init_command() for a REQ_OP_ZONE_RESET request.
176 blk_status_t sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd)
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);
183 if (!sd_is_zoned(sdkp))
184 /* Not a zoned device */
185 return BLK_STS_IOERR;
187 if (sdkp->device->changed)
188 return BLK_STS_IOERR;
190 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
191 /* Unaligned request */
192 return BLK_STS_IOERR;
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]);
200 rq->timeout = SD_TIMEOUT;
201 cmd->sc_data_direction = DMA_NONE;
202 cmd->transfersize = 0;
209 * sd_zbc_complete - ZBC command post processing.
210 * @cmd: Completed command
211 * @good_bytes: Command reply bytes
212 * @sshdr: command sense header
214 * Called from sd_done(). Process report zones reply and handle reset zone
215 * and write commands errors.
217 void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
218 struct scsi_sense_hdr *sshdr)
220 int result = cmd->result;
221 struct request *rq = cmd->request;
223 switch (req_op(rq)) {
224 case REQ_OP_ZONE_RESET:
227 sshdr->sense_key == ILLEGAL_REQUEST &&
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.
234 rq->rq_flags |= RQF_QUIET;
238 case REQ_OP_WRITE_ZEROES:
239 case REQ_OP_WRITE_SAME:
245 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
247 * @buf: Buffer where to store the VPD page data
249 * Read VPD page B6, get information and check that reads are unconstrained.
251 static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
255 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
256 sd_printk(KERN_NOTICE, sdkp,
257 "Read zoned characteristics VPD page failed\n");
261 if (sdkp->device->type != TYPE_ZBC) {
264 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
265 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
266 sdkp->zones_max_open = 0;
269 sdkp->urswrz = buf[4] & 1;
270 sdkp->zones_optimal_open = 0;
271 sdkp->zones_optimal_nonseq = 0;
272 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
276 * Check for unconstrained reads: host-managed devices with
277 * constrained reads (drives failing read after write pointer)
281 if (sdkp->first_scan)
282 sd_printk(KERN_NOTICE, sdkp,
283 "constrained reads devices are not supported\n");
290 #define SD_ZBC_BUF_SIZE 131072U
293 * sd_zbc_check_zones - Check the device capacity and zone sizes
296 * 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
302 * Returns the zone size in number of blocks upon success or an error code
305 static int sd_zbc_check_zones(struct scsi_disk *sdkp, u32 *zblocks)
308 sector_t max_lba, block = 0;
311 unsigned int buf_len;
312 unsigned int list_length;
317 buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
321 /* Do a report zone to get max_lba and the same field */
322 ret = sd_zbc_do_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0, false);
326 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;
340 * Check same field: for any value other than 0, we know that all zones
341 * have the same size.
343 same = buf[4] & 0x0f;
346 zone_blocks = get_unaligned_be64(&rec[8]);
351 * Check the size of all zones: all zones must be of
352 * equal size, except the last zone which can be smaller
357 /* Parse REPORT ZONES header */
358 list_length = get_unaligned_be32(&buf[0]) + 64;
360 buf_len = min(list_length, SD_ZBC_BUF_SIZE);
362 /* Parse zone descriptors */
363 while (rec < buf + buf_len) {
364 u64 this_zone_blocks = get_unaligned_be64(&rec[8]);
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)) {
374 block += this_zone_blocks;
378 if (block < sdkp->capacity) {
379 ret = sd_zbc_do_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE,
385 } while (block < sdkp->capacity);
389 if (sdkp->first_scan)
390 sd_printk(KERN_NOTICE, sdkp,
391 "Devices with non constant zone "
392 "size are not supported\n");
394 } else if (!is_power_of_2(zone_blocks)) {
395 if (sdkp->first_scan)
396 sd_printk(KERN_NOTICE, sdkp,
397 "Devices with non power of 2 zone "
398 "size are not supported\n");
400 } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
401 if (sdkp->first_scan)
402 sd_printk(KERN_NOTICE, sdkp,
403 "Zone size too large\n");
406 *zblocks = zone_blocks;
416 int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
418 struct gendisk *disk = sdkp->disk;
419 unsigned int nr_zones;
423 if (!sd_is_zoned(sdkp))
425 * Device managed or normal SCSI disk,
426 * no special handling required
430 /* Check zoned block device characteristics (unconstrained reads) */
431 ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
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.
439 ret = sd_zbc_check_zones(sdkp, &zone_blocks);
443 /* The drive satisfies the kernel restrictions: set it up */
444 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);
448 /* READ16/WRITE16 is mandatory for ZBC disks */
449 sdkp->device->use_16_for_rw = 1;
450 sdkp->device->use_10_for_rw = 0;
453 * 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.
457 if (sdkp->first_scan) {
458 sdkp->zone_blocks = zone_blocks;
459 sdkp->nr_zones = nr_zones;
463 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);
469 sdkp->zone_blocks = zone_blocks;
470 sdkp->nr_zones = nr_zones;
481 void sd_zbc_print_zones(struct scsi_disk *sdkp)
483 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
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",
492 sd_printk(KERN_NOTICE, sdkp,
493 "%u zones of %u logical blocks\n",