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>
12 #include <linux/vmalloc.h>
13 #include <linux/sched/mm.h>
15 #include <asm/unaligned.h>
17 #include <scsi/scsi.h>
18 #include <scsi/scsi_cmnd.h>
23 * 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
28 * All LBA sized values are converted to 512B sectors unit.
30 static void sd_zbc_parse_report(struct scsi_disk
*sdkp
, u8
*buf
,
31 struct blk_zone
*zone
)
33 struct scsi_device
*sdp
= sdkp
->device
;
35 memset(zone
, 0, sizeof(struct blk_zone
));
37 zone
->type
= buf
[0] & 0x0f;
38 zone
->cond
= (buf
[1] >> 4) & 0xf;
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
;
53 * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
54 * @sdkp: The target disk
55 * @buf: vmalloc-ed buffer to use for the reply
56 * @buflen: the buffer size
57 * @lba: Start LBA of the report
58 * @partial: Do partial report
60 * For internal use during device validation.
61 * 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
66 static int sd_zbc_do_report_zones(struct scsi_disk
*sdkp
, unsigned char *buf
,
67 unsigned int buflen
, sector_t lba
,
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];
79 cmd
[1] = ZI_REPORT_ZONES
;
80 put_unaligned_be64(lba
, &cmd
[2]);
81 put_unaligned_be32(buflen
, &cmd
[10]);
83 cmd
[14] = ZBC_REPORT_ZONE_PARTIAL
;
85 result
= scsi_execute_req(sdp
, cmd
, DMA_FROM_DEVICE
,
87 timeout
, SD_MAX_RETRIES
, NULL
);
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
));
96 rep_len
= get_unaligned_be32(&buf
[0]);
98 sd_printk(KERN_ERR
, sdkp
,
99 "REPORT ZONES report invalid length %u\n",
108 * Maximum number of zones to get with one report zones command.
110 #define SD_ZBC_REPORT_MAX_ZONES 8192U
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
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).
122 * Return the address of the allocated buffer and update @buflen with
123 * the size of the allocated buffer.
125 static void *sd_zbc_alloc_report_buffer(struct scsi_disk
*sdkp
,
126 unsigned int nr_zones
, size_t *buflen
)
128 struct request_queue
*q
= sdkp
->disk
->queue
;
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.
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
);
147 buf
= vzalloc(bufsize
);
155 * 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
161 * Execute a report zones command on the target disk.
163 int sd_zbc_report_zones(struct gendisk
*disk
, sector_t sector
,
164 struct blk_zone
*zones
, unsigned int *nr_zones
)
166 struct scsi_disk
*sdkp
= scsi_disk(disk
);
167 unsigned int i
, nrz
= *nr_zones
;
169 size_t buflen
= 0, offset
= 0;
172 if (!sd_is_zoned(sdkp
))
173 /* Not a zoned device */
176 buf
= sd_zbc_alloc_report_buffer(sdkp
, nrz
, &buflen
);
180 ret
= sd_zbc_do_report_zones(sdkp
, buf
, buflen
,
181 sectors_to_logical(sdkp
->device
, sector
), true);
185 nrz
= min(nrz
, get_unaligned_be32(&buf
[0]) / 64);
186 for (i
= 0; i
< nrz
; i
++) {
188 sd_zbc_parse_report(sdkp
, buf
+ offset
, zones
);
201 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
202 * @sdkp: The target disk
204 static inline sector_t
sd_zbc_zone_sectors(struct scsi_disk
*sdkp
)
206 return logical_to_sectors(sdkp
->device
, sdkp
->zone_blocks
);
210 * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command.
211 * @cmd: the command to setup
213 * Called from sd_init_command() for a REQ_OP_ZONE_RESET request.
215 blk_status_t
sd_zbc_setup_reset_cmnd(struct scsi_cmnd
*cmd
)
217 struct request
*rq
= cmd
->request
;
218 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
219 sector_t sector
= blk_rq_pos(rq
);
220 sector_t block
= sectors_to_logical(sdkp
->device
, sector
);
222 if (!sd_is_zoned(sdkp
))
223 /* Not a zoned device */
224 return BLK_STS_IOERR
;
226 if (sdkp
->device
->changed
)
227 return BLK_STS_IOERR
;
229 if (sector
& (sd_zbc_zone_sectors(sdkp
) - 1))
230 /* Unaligned request */
231 return BLK_STS_IOERR
;
234 memset(cmd
->cmnd
, 0, cmd
->cmd_len
);
235 cmd
->cmnd
[0] = ZBC_OUT
;
236 cmd
->cmnd
[1] = ZO_RESET_WRITE_POINTER
;
237 put_unaligned_be64(block
, &cmd
->cmnd
[2]);
239 rq
->timeout
= SD_TIMEOUT
;
240 cmd
->sc_data_direction
= DMA_NONE
;
241 cmd
->transfersize
= 0;
248 * sd_zbc_complete - ZBC command post processing.
249 * @cmd: Completed command
250 * @good_bytes: Command reply bytes
251 * @sshdr: command sense header
253 * Called from sd_done(). Process report zones reply and handle reset zone
254 * and write commands errors.
256 void sd_zbc_complete(struct scsi_cmnd
*cmd
, unsigned int good_bytes
,
257 struct scsi_sense_hdr
*sshdr
)
259 int result
= cmd
->result
;
260 struct request
*rq
= cmd
->request
;
262 switch (req_op(rq
)) {
263 case REQ_OP_ZONE_RESET
:
266 sshdr
->sense_key
== ILLEGAL_REQUEST
&&
269 * INVALID FIELD IN CDB error: reset of a conventional
270 * zone was attempted. Nothing to worry about, so be
271 * quiet about the error.
273 rq
->rq_flags
|= RQF_QUIET
;
277 case REQ_OP_WRITE_ZEROES
:
278 case REQ_OP_WRITE_SAME
:
284 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
286 * @buf: Buffer where to store the VPD page data
288 * Read VPD page B6, get information and check that reads are unconstrained.
290 static int sd_zbc_check_zoned_characteristics(struct scsi_disk
*sdkp
,
294 if (scsi_get_vpd_page(sdkp
->device
, 0xb6, buf
, 64)) {
295 sd_printk(KERN_NOTICE
, sdkp
,
296 "Read zoned characteristics VPD page failed\n");
300 if (sdkp
->device
->type
!= TYPE_ZBC
) {
303 sdkp
->zones_optimal_open
= get_unaligned_be32(&buf
[8]);
304 sdkp
->zones_optimal_nonseq
= get_unaligned_be32(&buf
[12]);
305 sdkp
->zones_max_open
= 0;
308 sdkp
->urswrz
= buf
[4] & 1;
309 sdkp
->zones_optimal_open
= 0;
310 sdkp
->zones_optimal_nonseq
= 0;
311 sdkp
->zones_max_open
= get_unaligned_be32(&buf
[16]);
315 * Check for unconstrained reads: host-managed devices with
316 * constrained reads (drives failing read after write pointer)
320 if (sdkp
->first_scan
)
321 sd_printk(KERN_NOTICE
, sdkp
,
322 "constrained reads devices are not supported\n");
330 * sd_zbc_check_zones - Check the device capacity and zone sizes
333 * Check that the device capacity as reported by READ CAPACITY matches the
334 * max_lba value (plus one)of the report zones command reply. Also check that
335 * all zones of the device have an equal size, only allowing the last zone of
336 * the disk to have a smaller size (runt zone). The zone size must also be a
339 * Returns the zone size in number of blocks upon success or an error code
342 static int sd_zbc_check_zones(struct scsi_disk
*sdkp
, u32
*zblocks
)
344 size_t bufsize
, buflen
;
345 unsigned int noio_flag
;
347 sector_t max_lba
, block
= 0;
353 /* Do all memory allocations as if GFP_NOIO was specified */
354 noio_flag
= memalloc_noio_save();
357 buf
= sd_zbc_alloc_report_buffer(sdkp
, SD_ZBC_REPORT_MAX_ZONES
,
364 /* Do a report zone to get max_lba and the same field */
365 ret
= sd_zbc_do_report_zones(sdkp
, buf
, bufsize
, 0, false);
369 if (sdkp
->rc_basis
== 0) {
370 /* The max_lba field is the capacity of this device */
371 max_lba
= get_unaligned_be64(&buf
[8]);
372 if (sdkp
->capacity
!= max_lba
+ 1) {
373 if (sdkp
->first_scan
)
374 sd_printk(KERN_WARNING
, sdkp
,
375 "Changing capacity from %llu to max LBA+1 %llu\n",
376 (unsigned long long)sdkp
->capacity
,
377 (unsigned long long)max_lba
+ 1);
378 sdkp
->capacity
= max_lba
+ 1;
383 * Check same field: for any value other than 0, we know that all zones
384 * have the same size.
386 same
= buf
[4] & 0x0f;
389 zone_blocks
= get_unaligned_be64(&rec
[8]);
394 * Check the size of all zones: all zones must be of
395 * equal size, except the last zone which can be smaller
400 /* Parse REPORT ZONES header */
401 buflen
= min_t(size_t, get_unaligned_be32(&buf
[0]) + 64,
405 /* Parse zone descriptors */
406 while (rec
< buf
+ buflen
) {
407 u64 this_zone_blocks
= get_unaligned_be64(&rec
[8]);
409 if (zone_blocks
== 0) {
410 zone_blocks
= this_zone_blocks
;
411 } else if (this_zone_blocks
!= zone_blocks
&&
412 (block
+ this_zone_blocks
< sdkp
->capacity
413 || this_zone_blocks
> zone_blocks
)) {
417 block
+= this_zone_blocks
;
421 if (block
< sdkp
->capacity
) {
422 ret
= sd_zbc_do_report_zones(sdkp
, buf
, bufsize
, block
,
428 } while (block
< sdkp
->capacity
);
432 if (sdkp
->first_scan
)
433 sd_printk(KERN_NOTICE
, sdkp
,
434 "Devices with non constant zone "
435 "size are not supported\n");
437 } else if (!is_power_of_2(zone_blocks
)) {
438 if (sdkp
->first_scan
)
439 sd_printk(KERN_NOTICE
, sdkp
,
440 "Devices with non power of 2 zone "
441 "size are not supported\n");
443 } else if (logical_to_sectors(sdkp
->device
, zone_blocks
) > UINT_MAX
) {
444 if (sdkp
->first_scan
)
445 sd_printk(KERN_NOTICE
, sdkp
,
446 "Zone size too large\n");
449 *zblocks
= zone_blocks
;
454 memalloc_noio_restore(noio_flag
);
460 int sd_zbc_read_zones(struct scsi_disk
*sdkp
, unsigned char *buf
)
462 struct gendisk
*disk
= sdkp
->disk
;
463 unsigned int nr_zones
;
467 if (!sd_is_zoned(sdkp
))
469 * Device managed or normal SCSI disk,
470 * no special handling required
474 /* Check zoned block device characteristics (unconstrained reads) */
475 ret
= sd_zbc_check_zoned_characteristics(sdkp
, buf
);
480 * Check zone size: only devices with a constant zone size (except
481 * an eventual last runt zone) that is a power of 2 are supported.
483 ret
= sd_zbc_check_zones(sdkp
, &zone_blocks
);
487 /* The drive satisfies the kernel restrictions: set it up */
488 blk_queue_chunk_sectors(sdkp
->disk
->queue
,
489 logical_to_sectors(sdkp
->device
, zone_blocks
));
490 nr_zones
= round_up(sdkp
->capacity
, zone_blocks
) >> ilog2(zone_blocks
);
492 /* READ16/WRITE16 is mandatory for ZBC disks */
493 sdkp
->device
->use_16_for_rw
= 1;
494 sdkp
->device
->use_10_for_rw
= 0;
497 * Revalidate the disk zone bitmaps once the block device capacity is
498 * set on the second revalidate execution during disk scan and if
499 * something changed when executing a normal revalidate.
501 if (sdkp
->first_scan
) {
502 sdkp
->zone_blocks
= zone_blocks
;
503 sdkp
->nr_zones
= nr_zones
;
507 if (sdkp
->zone_blocks
!= zone_blocks
||
508 sdkp
->nr_zones
!= nr_zones
||
509 disk
->queue
->nr_zones
!= nr_zones
) {
510 ret
= blk_revalidate_disk_zones(disk
);
513 sdkp
->zone_blocks
= zone_blocks
;
514 sdkp
->nr_zones
= nr_zones
;
525 void sd_zbc_print_zones(struct scsi_disk
*sdkp
)
527 if (!sd_is_zoned(sdkp
) || !sdkp
->capacity
)
530 if (sdkp
->capacity
& (sdkp
->zone_blocks
- 1))
531 sd_printk(KERN_NOTICE
, sdkp
,
532 "%u zones of %u logical blocks + 1 runt zone\n",
536 sd_printk(KERN_NOTICE
, sdkp
,
537 "%u zones of %u logical blocks\n",