2 * SCSI Zoned Block commands
4 * Copyright (C) 2014-2015 SUSE Linux GmbH
5 * Written by: Hannes Reinecke <hare@suse.de>
6 * Modified by: Damien Le Moal <damien.lemoal@hgst.com>
7 * Modified by: Shaun Tancheff <shaun.tancheff@seagate.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
25 #include <linux/blkdev.h>
27 #include <asm/unaligned.h>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
35 * sd_zbc_parse_report - Convert a zone descriptor to a struct blk_zone,
36 * @sdkp: The disk the report originated from
37 * @buf: Address of the report zone descriptor
38 * @zone: the destination zone structure
40 * All LBA sized values are converted to 512B sectors unit.
42 static void sd_zbc_parse_report(struct scsi_disk
*sdkp
, u8
*buf
,
43 struct blk_zone
*zone
)
45 struct scsi_device
*sdp
= sdkp
->device
;
47 memset(zone
, 0, sizeof(struct blk_zone
));
49 zone
->type
= buf
[0] & 0x0f;
50 zone
->cond
= (buf
[1] >> 4) & 0xf;
56 zone
->len
= logical_to_sectors(sdp
, get_unaligned_be64(&buf
[8]));
57 zone
->start
= logical_to_sectors(sdp
, get_unaligned_be64(&buf
[16]));
58 zone
->wp
= logical_to_sectors(sdp
, get_unaligned_be64(&buf
[24]));
59 if (zone
->type
!= ZBC_ZONE_TYPE_CONV
&&
60 zone
->cond
== ZBC_ZONE_COND_FULL
)
61 zone
->wp
= zone
->start
+ zone
->len
;
65 * sd_zbc_report_zones - Issue a REPORT ZONES scsi command.
66 * @sdkp: The target disk
67 * @buf: Buffer to use for the reply
68 * @buflen: the buffer size
69 * @lba: Start LBA of the report
71 * For internal use during device validation.
73 static int sd_zbc_report_zones(struct scsi_disk
*sdkp
, unsigned char *buf
,
74 unsigned int buflen
, sector_t lba
)
76 struct scsi_device
*sdp
= sdkp
->device
;
77 const int timeout
= sdp
->request_queue
->rq_timeout
;
78 struct scsi_sense_hdr sshdr
;
79 unsigned char cmd
[16];
85 cmd
[1] = ZI_REPORT_ZONES
;
86 put_unaligned_be64(lba
, &cmd
[2]);
87 put_unaligned_be32(buflen
, &cmd
[10]);
88 memset(buf
, 0, buflen
);
90 result
= scsi_execute_req(sdp
, cmd
, DMA_FROM_DEVICE
,
92 timeout
, SD_MAX_RETRIES
, NULL
);
94 sd_printk(KERN_ERR
, sdkp
,
95 "REPORT ZONES lba %llu failed with %d/%d\n",
96 (unsigned long long)lba
,
97 host_byte(result
), driver_byte(result
));
101 rep_len
= get_unaligned_be32(&buf
[0]);
103 sd_printk(KERN_ERR
, sdkp
,
104 "REPORT ZONES report invalid length %u\n",
113 * sd_zbc_setup_report_cmnd - Prepare a REPORT ZONES scsi command
114 * @cmd: The command to setup
116 * Call in sd_init_command() for a REQ_OP_ZONE_REPORT request.
118 int sd_zbc_setup_report_cmnd(struct scsi_cmnd
*cmd
)
120 struct request
*rq
= cmd
->request
;
121 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
122 sector_t lba
, sector
= blk_rq_pos(rq
);
123 unsigned int nr_bytes
= blk_rq_bytes(rq
);
126 WARN_ON(nr_bytes
== 0);
128 if (!sd_is_zoned(sdkp
))
129 /* Not a zoned device */
132 ret
= scsi_init_io(cmd
);
133 if (ret
!= BLKPREP_OK
)
137 memset(cmd
->cmnd
, 0, cmd
->cmd_len
);
138 cmd
->cmnd
[0] = ZBC_IN
;
139 cmd
->cmnd
[1] = ZI_REPORT_ZONES
;
140 lba
= sectors_to_logical(sdkp
->device
, sector
);
141 put_unaligned_be64(lba
, &cmd
->cmnd
[2]);
142 put_unaligned_be32(nr_bytes
, &cmd
->cmnd
[10]);
143 /* Do partial report for speeding things up */
144 cmd
->cmnd
[14] = ZBC_REPORT_ZONE_PARTIAL
;
146 cmd
->sc_data_direction
= DMA_FROM_DEVICE
;
147 cmd
->sdb
.length
= nr_bytes
;
148 cmd
->transfersize
= sdkp
->device
->sector_size
;
155 * sd_zbc_report_zones_complete - Process a REPORT ZONES scsi command reply.
156 * @scmd: The completed report zones command
157 * @good_bytes: reply size in bytes
159 * Convert all reported zone descriptors to struct blk_zone. The conversion
160 * is done in-place, directly in the request specified sg buffer.
162 static void sd_zbc_report_zones_complete(struct scsi_cmnd
*scmd
,
163 unsigned int good_bytes
)
165 struct request
*rq
= scmd
->request
;
166 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
167 struct sg_mapping_iter miter
;
168 struct blk_zone_report_hdr hdr
;
169 struct blk_zone zone
;
170 unsigned int offset
, bytes
= 0;
177 memset(&hdr
, 0, sizeof(struct blk_zone_report_hdr
));
179 sg_miter_start(&miter
, scsi_sglist(scmd
), scsi_sg_count(scmd
),
180 SG_MITER_TO_SG
| SG_MITER_ATOMIC
);
182 local_irq_save(flags
);
183 while (sg_miter_next(&miter
) && bytes
< good_bytes
) {
189 /* Set the report header */
190 hdr
.nr_zones
= min_t(unsigned int,
191 (good_bytes
- 64) / 64,
192 get_unaligned_be32(&buf
[0]) / 64);
193 memcpy(buf
, &hdr
, sizeof(struct blk_zone_report_hdr
));
198 /* Parse zone descriptors */
199 while (offset
< miter
.length
&& hdr
.nr_zones
) {
200 WARN_ON(offset
> miter
.length
);
201 buf
= miter
.addr
+ offset
;
202 sd_zbc_parse_report(sdkp
, buf
, &zone
);
203 memcpy(buf
, &zone
, sizeof(struct blk_zone
));
213 sg_miter_stop(&miter
);
214 local_irq_restore(flags
);
218 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
219 * @sdkp: The target disk
221 static inline sector_t
sd_zbc_zone_sectors(struct scsi_disk
*sdkp
)
223 return logical_to_sectors(sdkp
->device
, sdkp
->zone_blocks
);
227 * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command.
228 * @cmd: the command to setup
230 * Called from sd_init_command() for a REQ_OP_ZONE_RESET request.
232 int sd_zbc_setup_reset_cmnd(struct scsi_cmnd
*cmd
)
234 struct request
*rq
= cmd
->request
;
235 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
236 sector_t sector
= blk_rq_pos(rq
);
237 sector_t block
= sectors_to_logical(sdkp
->device
, sector
);
239 if (!sd_is_zoned(sdkp
))
240 /* Not a zoned device */
243 if (sdkp
->device
->changed
)
246 if (sector
& (sd_zbc_zone_sectors(sdkp
) - 1))
247 /* Unaligned request */
251 memset(cmd
->cmnd
, 0, cmd
->cmd_len
);
252 cmd
->cmnd
[0] = ZBC_OUT
;
253 cmd
->cmnd
[1] = ZO_RESET_WRITE_POINTER
;
254 put_unaligned_be64(block
, &cmd
->cmnd
[2]);
256 rq
->timeout
= SD_TIMEOUT
;
257 cmd
->sc_data_direction
= DMA_NONE
;
258 cmd
->transfersize
= 0;
265 * sd_zbc_complete - ZBC command post processing.
266 * @cmd: Completed command
267 * @good_bytes: Command reply bytes
268 * @sshdr: command sense header
270 * Called from sd_done(). Process report zones reply and handle reset zone
271 * and write commands errors.
273 void sd_zbc_complete(struct scsi_cmnd
*cmd
, unsigned int good_bytes
,
274 struct scsi_sense_hdr
*sshdr
)
276 int result
= cmd
->result
;
277 struct request
*rq
= cmd
->request
;
279 switch (req_op(rq
)) {
280 case REQ_OP_ZONE_RESET
:
283 sshdr
->sense_key
== ILLEGAL_REQUEST
&&
286 * INVALID FIELD IN CDB error: reset of a conventional
287 * zone was attempted. Nothing to worry about, so be
288 * quiet about the error.
290 rq
->rq_flags
|= RQF_QUIET
;
294 case REQ_OP_WRITE_ZEROES
:
295 case REQ_OP_WRITE_SAME
:
298 case REQ_OP_ZONE_REPORT
:
301 sd_zbc_report_zones_complete(cmd
, good_bytes
);
308 * sd_zbc_read_zoned_characteristics - Read zoned block device characteristics
310 * @buf: Buffer where to store the VPD page data
314 static int sd_zbc_read_zoned_characteristics(struct scsi_disk
*sdkp
,
318 if (scsi_get_vpd_page(sdkp
->device
, 0xb6, buf
, 64)) {
319 sd_printk(KERN_NOTICE
, sdkp
,
320 "Unconstrained-read check failed\n");
324 if (sdkp
->device
->type
!= TYPE_ZBC
) {
327 sdkp
->zones_optimal_open
= get_unaligned_be32(&buf
[8]);
328 sdkp
->zones_optimal_nonseq
= get_unaligned_be32(&buf
[12]);
329 sdkp
->zones_max_open
= 0;
332 sdkp
->urswrz
= buf
[4] & 1;
333 sdkp
->zones_optimal_open
= 0;
334 sdkp
->zones_optimal_nonseq
= 0;
335 sdkp
->zones_max_open
= get_unaligned_be32(&buf
[16]);
342 * sd_zbc_check_capacity - Check reported capacity.
344 * @buf: Buffer to use for commands
346 * ZBC drive may report only the capacity of the first conventional zones at
347 * LBA 0. This is indicated by the RC_BASIS field of the read capacity reply.
348 * Check this here. If the disk reported only its conventional zones capacity,
349 * get the total capacity by doing a report zones.
351 static int sd_zbc_check_capacity(struct scsi_disk
*sdkp
, unsigned char *buf
)
356 if (sdkp
->rc_basis
!= 0)
359 /* Do a report zone to get the maximum LBA to check capacity */
360 ret
= sd_zbc_report_zones(sdkp
, buf
, SD_BUF_SIZE
, 0);
364 /* The max_lba field is the capacity of this device */
365 lba
= get_unaligned_be64(&buf
[8]);
366 if (lba
+ 1 == sdkp
->capacity
)
369 if (sdkp
->first_scan
)
370 sd_printk(KERN_WARNING
, sdkp
,
371 "Changing capacity from %llu to max LBA+1 %llu\n",
372 (unsigned long long)sdkp
->capacity
,
373 (unsigned long long)lba
+ 1);
374 sdkp
->capacity
= lba
+ 1;
379 #define SD_ZBC_BUF_SIZE 131072U
382 * sd_zbc_check_zone_size - Check the device zone sizes
385 * Check that all zones of the device are equal. The last zone can however
386 * be smaller. The zone size must also be a power of two number of LBAs.
388 * Returns the zone size in number of blocks upon success or an error code
391 static s64
sd_zbc_check_zone_size(struct scsi_disk
*sdkp
)
397 unsigned int buf_len
;
398 unsigned int list_length
;
403 buf
= kmalloc(SD_ZBC_BUF_SIZE
, GFP_KERNEL
);
407 /* Do a report zone to get the same field */
408 ret
= sd_zbc_report_zones(sdkp
, buf
, SD_ZBC_BUF_SIZE
, 0);
412 same
= buf
[4] & 0x0f;
415 zone_blocks
= get_unaligned_be64(&rec
[8]);
420 * Check the size of all zones: all zones must be of
421 * equal size, except the last zone which can be smaller
426 /* Parse REPORT ZONES header */
427 list_length
= get_unaligned_be32(&buf
[0]) + 64;
429 buf_len
= min(list_length
, SD_ZBC_BUF_SIZE
);
431 /* Parse zone descriptors */
432 while (rec
< buf
+ buf_len
) {
433 u64 this_zone_blocks
= get_unaligned_be64(&rec
[8]);
435 if (zone_blocks
== 0) {
436 zone_blocks
= this_zone_blocks
;
437 } else if (this_zone_blocks
!= zone_blocks
&&
438 (block
+ this_zone_blocks
< sdkp
->capacity
439 || this_zone_blocks
> zone_blocks
)) {
443 block
+= this_zone_blocks
;
447 if (block
< sdkp
->capacity
) {
448 ret
= sd_zbc_report_zones(sdkp
, buf
,
449 SD_ZBC_BUF_SIZE
, block
);
454 } while (block
< sdkp
->capacity
);
458 if (sdkp
->first_scan
)
459 sd_printk(KERN_NOTICE
, sdkp
,
460 "Devices with non constant zone "
461 "size are not supported\n");
463 } else if (!is_power_of_2(zone_blocks
)) {
464 if (sdkp
->first_scan
)
465 sd_printk(KERN_NOTICE
, sdkp
,
466 "Devices with non power of 2 zone "
467 "size are not supported\n");
469 } else if (logical_to_sectors(sdkp
->device
, zone_blocks
) > UINT_MAX
) {
470 if (sdkp
->first_scan
)
471 sd_printk(KERN_NOTICE
, sdkp
,
472 "Zone size too large\n");
485 * sd_zbc_alloc_zone_bitmap - Allocate a zone bitmap (one bit per zone).
486 * @nr_zones: Number of zones to allocate space for.
487 * @numa_node: NUMA node to allocate the memory from.
489 static inline unsigned long *
490 sd_zbc_alloc_zone_bitmap(u32 nr_zones
, int numa_node
)
492 return kcalloc_node(BITS_TO_LONGS(nr_zones
), sizeof(unsigned long),
493 GFP_KERNEL
, numa_node
);
497 * sd_zbc_get_seq_zones - Parse report zones reply to identify sequential zones
499 * @buf: report reply buffer
500 * @buflen: length of @buf
501 * @zone_shift: logarithm base 2 of the number of blocks in a zone
502 * @seq_zones_bitmap: bitmap of sequential zones to set
504 * Parse reported zone descriptors in @buf to identify sequential zones and
505 * set the reported zone bit in @seq_zones_bitmap accordingly.
506 * Since read-only and offline zones cannot be written, do not
507 * mark them as sequential in the bitmap.
508 * Return the LBA after the last zone reported.
510 static sector_t
sd_zbc_get_seq_zones(struct scsi_disk
*sdkp
, unsigned char *buf
,
511 unsigned int buflen
, u32 zone_shift
,
512 unsigned long *seq_zones_bitmap
)
514 sector_t lba
, next_lba
= sdkp
->capacity
;
515 unsigned int buf_len
, list_length
;
519 list_length
= get_unaligned_be32(&buf
[0]) + 64;
520 buf_len
= min(list_length
, buflen
);
523 while (rec
< buf
+ buf_len
) {
524 type
= rec
[0] & 0x0f;
525 cond
= (rec
[1] >> 4) & 0xf;
526 lba
= get_unaligned_be64(&rec
[16]);
527 if (type
!= ZBC_ZONE_TYPE_CONV
&&
528 cond
!= ZBC_ZONE_COND_READONLY
&&
529 cond
!= ZBC_ZONE_COND_OFFLINE
)
530 set_bit(lba
>> zone_shift
, seq_zones_bitmap
);
531 next_lba
= lba
+ get_unaligned_be64(&rec
[8]);
539 * sd_zbc_setup_seq_zones_bitmap - Initialize a seq zone bitmap.
541 * @zone_shift: logarithm base 2 of the number of blocks in a zone
542 * @nr_zones: number of zones to set up a seq zone bitmap for
544 * Allocate a zone bitmap and initialize it by identifying sequential zones.
546 static unsigned long *
547 sd_zbc_setup_seq_zones_bitmap(struct scsi_disk
*sdkp
, u32 zone_shift
,
550 struct request_queue
*q
= sdkp
->disk
->queue
;
551 unsigned long *seq_zones_bitmap
;
556 seq_zones_bitmap
= sd_zbc_alloc_zone_bitmap(nr_zones
, q
->node
);
557 if (!seq_zones_bitmap
)
558 return ERR_PTR(-ENOMEM
);
560 buf
= kmalloc(SD_ZBC_BUF_SIZE
, GFP_KERNEL
);
564 while (lba
< sdkp
->capacity
) {
565 ret
= sd_zbc_report_zones(sdkp
, buf
, SD_ZBC_BUF_SIZE
, lba
);
568 lba
= sd_zbc_get_seq_zones(sdkp
, buf
, SD_ZBC_BUF_SIZE
,
569 zone_shift
, seq_zones_bitmap
);
572 if (lba
!= sdkp
->capacity
) {
573 /* Something went wrong */
580 kfree(seq_zones_bitmap
);
583 return seq_zones_bitmap
;
586 static void sd_zbc_cleanup(struct scsi_disk
*sdkp
)
588 struct request_queue
*q
= sdkp
->disk
->queue
;
590 kfree(q
->seq_zones_bitmap
);
591 q
->seq_zones_bitmap
= NULL
;
593 kfree(q
->seq_zones_wlock
);
594 q
->seq_zones_wlock
= NULL
;
599 static int sd_zbc_setup(struct scsi_disk
*sdkp
, u32 zone_blocks
)
601 struct request_queue
*q
= sdkp
->disk
->queue
;
602 u32 zone_shift
= ilog2(zone_blocks
);
606 /* chunk_sectors indicates the zone size */
607 blk_queue_chunk_sectors(q
,
608 logical_to_sectors(sdkp
->device
, zone_blocks
));
609 nr_zones
= round_up(sdkp
->capacity
, zone_blocks
) >> zone_shift
;
612 * Initialize the device request queue information if the number
615 if (nr_zones
!= sdkp
->nr_zones
|| nr_zones
!= q
->nr_zones
) {
616 unsigned long *seq_zones_wlock
= NULL
, *seq_zones_bitmap
= NULL
;
617 size_t zone_bitmap_size
;
620 seq_zones_wlock
= sd_zbc_alloc_zone_bitmap(nr_zones
,
622 if (!seq_zones_wlock
) {
627 seq_zones_bitmap
= sd_zbc_setup_seq_zones_bitmap(sdkp
,
628 zone_shift
, nr_zones
);
629 if (IS_ERR(seq_zones_bitmap
)) {
630 ret
= PTR_ERR(seq_zones_bitmap
);
631 kfree(seq_zones_wlock
);
635 zone_bitmap_size
= BITS_TO_LONGS(nr_zones
) *
636 sizeof(unsigned long);
637 blk_mq_freeze_queue(q
);
638 if (q
->nr_zones
!= nr_zones
) {
639 /* READ16/WRITE16 is mandatory for ZBC disks */
640 sdkp
->device
->use_16_for_rw
= 1;
641 sdkp
->device
->use_10_for_rw
= 0;
643 sdkp
->zone_blocks
= zone_blocks
;
644 sdkp
->zone_shift
= zone_shift
;
645 sdkp
->nr_zones
= nr_zones
;
646 q
->nr_zones
= nr_zones
;
647 swap(q
->seq_zones_wlock
, seq_zones_wlock
);
648 swap(q
->seq_zones_bitmap
, seq_zones_bitmap
);
649 } else if (memcmp(q
->seq_zones_bitmap
, seq_zones_bitmap
,
650 zone_bitmap_size
) != 0) {
651 memcpy(q
->seq_zones_bitmap
, seq_zones_bitmap
,
654 blk_mq_unfreeze_queue(q
);
655 kfree(seq_zones_wlock
);
656 kfree(seq_zones_bitmap
);
662 sd_zbc_cleanup(sdkp
);
666 int sd_zbc_read_zones(struct scsi_disk
*sdkp
, unsigned char *buf
)
671 if (!sd_is_zoned(sdkp
))
673 * Device managed or normal SCSI disk,
674 * no special handling required
678 /* Get zoned block device characteristics */
679 ret
= sd_zbc_read_zoned_characteristics(sdkp
, buf
);
684 * Check for unconstrained reads: host-managed devices with
685 * constrained reads (drives failing read after write pointer)
689 if (sdkp
->first_scan
)
690 sd_printk(KERN_NOTICE
, sdkp
,
691 "constrained reads devices are not supported\n");
697 ret
= sd_zbc_check_capacity(sdkp
, buf
);
702 * Check zone size: only devices with a constant zone size (except
703 * an eventual last runt zone) that is a power of 2 are supported.
705 zone_blocks
= sd_zbc_check_zone_size(sdkp
);
707 if (zone_blocks
!= (u32
)zone_blocks
)
713 /* The drive satisfies the kernel restrictions: set it up */
714 ret
= sd_zbc_setup(sdkp
, zone_blocks
);
722 sd_zbc_cleanup(sdkp
);
727 void sd_zbc_remove(struct scsi_disk
*sdkp
)
729 sd_zbc_cleanup(sdkp
);
732 void sd_zbc_print_zones(struct scsi_disk
*sdkp
)
734 if (!sd_is_zoned(sdkp
) || !sdkp
->capacity
)
737 if (sdkp
->capacity
& (sdkp
->zone_blocks
- 1))
738 sd_printk(KERN_NOTICE
, sdkp
,
739 "%u zones of %u logical blocks + 1 runt zone\n",
743 sd_printk(KERN_NOTICE
, sdkp
,
744 "%u zones of %u logical blocks\n",