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>
31 #include <scsi/scsi_dbg.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_driver.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_eh.h>
38 #include "scsi_priv.h"
41 ZBC_ZONE_TYPE_CONV
= 0x1,
42 ZBC_ZONE_TYPE_SEQWRITE_REQ
,
43 ZBC_ZONE_TYPE_SEQWRITE_PREF
,
44 ZBC_ZONE_TYPE_RESERVED
,
50 ZBC_ZONE_COND_IMP_OPEN
,
51 ZBC_ZONE_COND_EXP_OPEN
,
53 ZBC_ZONE_COND_READONLY
= 0xd,
55 ZBC_ZONE_COND_OFFLINE
,
59 * Convert a zone descriptor to a zone struct.
61 static void sd_zbc_parse_report(struct scsi_disk
*sdkp
,
63 struct blk_zone
*zone
)
65 struct scsi_device
*sdp
= sdkp
->device
;
67 memset(zone
, 0, sizeof(struct blk_zone
));
69 zone
->type
= buf
[0] & 0x0f;
70 zone
->cond
= (buf
[1] >> 4) & 0xf;
76 zone
->len
= logical_to_sectors(sdp
, get_unaligned_be64(&buf
[8]));
77 zone
->start
= logical_to_sectors(sdp
, get_unaligned_be64(&buf
[16]));
78 zone
->wp
= logical_to_sectors(sdp
, get_unaligned_be64(&buf
[24]));
79 if (zone
->type
!= ZBC_ZONE_TYPE_CONV
&&
80 zone
->cond
== ZBC_ZONE_COND_FULL
)
81 zone
->wp
= zone
->start
+ zone
->len
;
85 * Issue a REPORT ZONES scsi command.
87 static int sd_zbc_report_zones(struct scsi_disk
*sdkp
, unsigned char *buf
,
88 unsigned int buflen
, sector_t lba
)
90 struct scsi_device
*sdp
= sdkp
->device
;
91 const int timeout
= sdp
->request_queue
->rq_timeout
;
92 struct scsi_sense_hdr sshdr
;
93 unsigned char cmd
[16];
99 cmd
[1] = ZI_REPORT_ZONES
;
100 put_unaligned_be64(lba
, &cmd
[2]);
101 put_unaligned_be32(buflen
, &cmd
[10]);
102 memset(buf
, 0, buflen
);
104 result
= scsi_execute_req(sdp
, cmd
, DMA_FROM_DEVICE
,
106 timeout
, SD_MAX_RETRIES
, NULL
);
108 sd_printk(KERN_ERR
, sdkp
,
109 "REPORT ZONES lba %llu failed with %d/%d\n",
110 (unsigned long long)lba
,
111 host_byte(result
), driver_byte(result
));
115 rep_len
= get_unaligned_be32(&buf
[0]);
117 sd_printk(KERN_ERR
, sdkp
,
118 "REPORT ZONES report invalid length %u\n",
126 int sd_zbc_setup_report_cmnd(struct scsi_cmnd
*cmd
)
128 struct request
*rq
= cmd
->request
;
129 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
130 sector_t lba
, sector
= blk_rq_pos(rq
);
131 unsigned int nr_bytes
= blk_rq_bytes(rq
);
134 WARN_ON(nr_bytes
== 0);
136 if (!sd_is_zoned(sdkp
))
137 /* Not a zoned device */
140 ret
= scsi_init_io(cmd
);
141 if (ret
!= BLKPREP_OK
)
145 memset(cmd
->cmnd
, 0, cmd
->cmd_len
);
146 cmd
->cmnd
[0] = ZBC_IN
;
147 cmd
->cmnd
[1] = ZI_REPORT_ZONES
;
148 lba
= sectors_to_logical(sdkp
->device
, sector
);
149 put_unaligned_be64(lba
, &cmd
->cmnd
[2]);
150 put_unaligned_be32(nr_bytes
, &cmd
->cmnd
[10]);
151 /* Do partial report for speeding things up */
152 cmd
->cmnd
[14] = ZBC_REPORT_ZONE_PARTIAL
;
154 cmd
->sc_data_direction
= DMA_FROM_DEVICE
;
155 cmd
->sdb
.length
= nr_bytes
;
156 cmd
->transfersize
= sdkp
->device
->sector_size
;
160 * Report may return less bytes than requested. Make sure
161 * to report completion on the entire initial request.
163 rq
->__data_len
= nr_bytes
;
168 static void sd_zbc_report_zones_complete(struct scsi_cmnd
*scmd
,
169 unsigned int good_bytes
)
171 struct request
*rq
= scmd
->request
;
172 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
173 struct sg_mapping_iter miter
;
174 struct blk_zone_report_hdr hdr
;
175 struct blk_zone zone
;
176 unsigned int offset
, bytes
= 0;
183 memset(&hdr
, 0, sizeof(struct blk_zone_report_hdr
));
185 sg_miter_start(&miter
, scsi_sglist(scmd
), scsi_sg_count(scmd
),
186 SG_MITER_TO_SG
| SG_MITER_ATOMIC
);
188 local_irq_save(flags
);
189 while (sg_miter_next(&miter
) && bytes
< good_bytes
) {
195 /* Set the report header */
196 hdr
.nr_zones
= min_t(unsigned int,
197 (good_bytes
- 64) / 64,
198 get_unaligned_be32(&buf
[0]) / 64);
199 memcpy(buf
, &hdr
, sizeof(struct blk_zone_report_hdr
));
204 /* Parse zone descriptors */
205 while (offset
< miter
.length
&& hdr
.nr_zones
) {
206 WARN_ON(offset
> miter
.length
);
207 buf
= miter
.addr
+ offset
;
208 sd_zbc_parse_report(sdkp
, buf
, &zone
);
209 memcpy(buf
, &zone
, sizeof(struct blk_zone
));
219 sg_miter_stop(&miter
);
220 local_irq_restore(flags
);
223 static inline sector_t
sd_zbc_zone_sectors(struct scsi_disk
*sdkp
)
225 return logical_to_sectors(sdkp
->device
, sdkp
->zone_blocks
);
228 static inline unsigned int sd_zbc_zone_no(struct scsi_disk
*sdkp
,
231 return sectors_to_logical(sdkp
->device
, sector
) >> sdkp
->zone_shift
;
234 int sd_zbc_setup_reset_cmnd(struct scsi_cmnd
*cmd
)
236 struct request
*rq
= cmd
->request
;
237 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
238 sector_t sector
= blk_rq_pos(rq
);
239 sector_t block
= sectors_to_logical(sdkp
->device
, sector
);
241 if (!sd_is_zoned(sdkp
))
242 /* Not a zoned device */
245 if (sdkp
->device
->changed
)
248 if (sector
& (sd_zbc_zone_sectors(sdkp
) - 1))
249 /* Unaligned request */
253 memset(cmd
->cmnd
, 0, cmd
->cmd_len
);
254 cmd
->cmnd
[0] = ZBC_OUT
;
255 cmd
->cmnd
[1] = ZO_RESET_WRITE_POINTER
;
256 put_unaligned_be64(block
, &cmd
->cmnd
[2]);
258 rq
->timeout
= SD_TIMEOUT
;
259 cmd
->sc_data_direction
= DMA_NONE
;
260 cmd
->transfersize
= 0;
266 int sd_zbc_write_lock_zone(struct scsi_cmnd
*cmd
)
268 struct request
*rq
= cmd
->request
;
269 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
270 sector_t sector
= blk_rq_pos(rq
);
271 sector_t zone_sectors
= sd_zbc_zone_sectors(sdkp
);
272 unsigned int zno
= sd_zbc_zone_no(sdkp
, sector
);
275 * Note: Checks of the alignment of the write command on
276 * logical blocks is done in sd.c
279 /* Do not allow zone boundaries crossing on host-managed drives */
280 if (blk_queue_zoned_model(sdkp
->disk
->queue
) == BLK_ZONED_HM
&&
281 (sector
& (zone_sectors
- 1)) + blk_rq_sectors(rq
) > zone_sectors
)
285 * Do not issue more than one write at a time per
286 * zone. This solves write ordering problems due to
287 * the unlocking of the request queue in the dispatch
288 * path in the non scsi-mq case. For scsi-mq, this
289 * also avoids potential write reordering when multiple
290 * threads running on different CPUs write to the same
291 * zone (with a synchronized sequential pattern).
293 if (sdkp
->zones_wlock
&&
294 test_and_set_bit(zno
, sdkp
->zones_wlock
))
295 return BLKPREP_DEFER
;
300 void sd_zbc_write_unlock_zone(struct scsi_cmnd
*cmd
)
302 struct request
*rq
= cmd
->request
;
303 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
305 if (sdkp
->zones_wlock
) {
306 unsigned int zno
= sd_zbc_zone_no(sdkp
, blk_rq_pos(rq
));
307 WARN_ON_ONCE(!test_bit(zno
, sdkp
->zones_wlock
));
308 clear_bit_unlock(zno
, sdkp
->zones_wlock
);
309 smp_mb__after_atomic();
313 void sd_zbc_complete(struct scsi_cmnd
*cmd
,
314 unsigned int good_bytes
,
315 struct scsi_sense_hdr
*sshdr
)
317 int result
= cmd
->result
;
318 struct request
*rq
= cmd
->request
;
320 switch (req_op(rq
)) {
321 case REQ_OP_ZONE_RESET
:
324 sshdr
->sense_key
== ILLEGAL_REQUEST
&&
327 * INVALID FIELD IN CDB error: reset of a conventional
328 * zone was attempted. Nothing to worry about, so be
329 * quiet about the error.
331 rq
->rq_flags
|= RQF_QUIET
;
335 case REQ_OP_WRITE_ZEROES
:
336 case REQ_OP_WRITE_SAME
:
338 /* Unlock the zone */
339 sd_zbc_write_unlock_zone(cmd
);
342 sshdr
->sense_key
== ILLEGAL_REQUEST
&&
345 * INVALID ADDRESS FOR WRITE error: It is unlikely that
346 * retrying write requests failed with any kind of
347 * alignement error will result in success. So don't.
352 case REQ_OP_ZONE_REPORT
:
355 sd_zbc_report_zones_complete(cmd
, good_bytes
);
362 * Read zoned block device characteristics (VPD page B6).
364 static int sd_zbc_read_zoned_characteristics(struct scsi_disk
*sdkp
,
368 if (scsi_get_vpd_page(sdkp
->device
, 0xb6, buf
, 64)) {
369 sd_printk(KERN_NOTICE
, sdkp
,
370 "Unconstrained-read check failed\n");
374 if (sdkp
->device
->type
!= TYPE_ZBC
) {
377 sdkp
->zones_optimal_open
= get_unaligned_be64(&buf
[8]);
378 sdkp
->zones_optimal_nonseq
= get_unaligned_be64(&buf
[12]);
379 sdkp
->zones_max_open
= 0;
382 sdkp
->urswrz
= buf
[4] & 1;
383 sdkp
->zones_optimal_open
= 0;
384 sdkp
->zones_optimal_nonseq
= 0;
385 sdkp
->zones_max_open
= get_unaligned_be64(&buf
[16]);
392 * Check reported capacity.
394 static int sd_zbc_check_capacity(struct scsi_disk
*sdkp
,
400 if (sdkp
->rc_basis
!= 0)
403 /* Do a report zone to get the maximum LBA to check capacity */
404 ret
= sd_zbc_report_zones(sdkp
, buf
, SD_BUF_SIZE
, 0);
408 /* The max_lba field is the capacity of this device */
409 lba
= get_unaligned_be64(&buf
[8]);
410 if (lba
+ 1 == sdkp
->capacity
)
413 if (sdkp
->first_scan
)
414 sd_printk(KERN_WARNING
, sdkp
,
415 "Changing capacity from %llu to max LBA+1 %llu\n",
416 (unsigned long long)sdkp
->capacity
,
417 (unsigned long long)lba
+ 1);
418 sdkp
->capacity
= lba
+ 1;
423 #define SD_ZBC_BUF_SIZE 131072
425 static int sd_zbc_check_zone_size(struct scsi_disk
*sdkp
)
431 unsigned int buf_len
;
432 unsigned int list_length
;
436 sdkp
->zone_blocks
= 0;
439 buf
= kmalloc(SD_ZBC_BUF_SIZE
, GFP_KERNEL
);
443 /* Do a report zone to get the same field */
444 ret
= sd_zbc_report_zones(sdkp
, buf
, SD_ZBC_BUF_SIZE
, 0);
450 same
= buf
[4] & 0x0f;
453 zone_blocks
= get_unaligned_be64(&rec
[8]);
458 * Check the size of all zones: all zones must be of
459 * equal size, except the last zone which can be smaller
464 /* Parse REPORT ZONES header */
465 list_length
= get_unaligned_be32(&buf
[0]) + 64;
467 if (list_length
< SD_ZBC_BUF_SIZE
)
468 buf_len
= list_length
;
470 buf_len
= SD_ZBC_BUF_SIZE
;
472 /* Parse zone descriptors */
473 while (rec
< buf
+ buf_len
) {
474 zone_blocks
= get_unaligned_be64(&rec
[8]);
475 if (sdkp
->zone_blocks
== 0) {
476 sdkp
->zone_blocks
= zone_blocks
;
477 } else if (zone_blocks
!= sdkp
->zone_blocks
&&
478 (block
+ zone_blocks
< sdkp
->capacity
479 || zone_blocks
> sdkp
->zone_blocks
)) {
483 block
+= zone_blocks
;
487 if (block
< sdkp
->capacity
) {
488 ret
= sd_zbc_report_zones(sdkp
, buf
,
489 SD_ZBC_BUF_SIZE
, block
);
494 } while (block
< sdkp
->capacity
);
496 zone_blocks
= sdkp
->zone_blocks
;
502 if (sdkp
->first_scan
)
503 sd_printk(KERN_NOTICE
, sdkp
,
504 "Devices with non constant zone "
505 "size are not supported\n");
509 if (!is_power_of_2(zone_blocks
)) {
510 if (sdkp
->first_scan
)
511 sd_printk(KERN_NOTICE
, sdkp
,
512 "Devices with non power of 2 zone "
513 "size are not supported\n");
517 if (logical_to_sectors(sdkp
->device
, zone_blocks
) > UINT_MAX
) {
518 if (sdkp
->first_scan
)
519 sd_printk(KERN_NOTICE
, sdkp
,
520 "Zone size too large\n");
524 sdkp
->zone_blocks
= zone_blocks
;
529 static int sd_zbc_setup(struct scsi_disk
*sdkp
)
532 /* chunk_sectors indicates the zone size */
533 blk_queue_chunk_sectors(sdkp
->disk
->queue
,
534 logical_to_sectors(sdkp
->device
, sdkp
->zone_blocks
));
535 sdkp
->zone_shift
= ilog2(sdkp
->zone_blocks
);
536 sdkp
->nr_zones
= sdkp
->capacity
>> sdkp
->zone_shift
;
537 if (sdkp
->capacity
& (sdkp
->zone_blocks
- 1))
540 if (!sdkp
->zones_wlock
) {
541 sdkp
->zones_wlock
= kcalloc(BITS_TO_LONGS(sdkp
->nr_zones
),
542 sizeof(unsigned long),
544 if (!sdkp
->zones_wlock
)
551 int sd_zbc_read_zones(struct scsi_disk
*sdkp
,
556 if (!sd_is_zoned(sdkp
))
558 * Device managed or normal SCSI disk,
559 * no special handling required
564 /* Get zoned block device characteristics */
565 ret
= sd_zbc_read_zoned_characteristics(sdkp
, buf
);
570 * Check for unconstrained reads: host-managed devices with
571 * constrained reads (drives failing read after write pointer)
575 if (sdkp
->first_scan
)
576 sd_printk(KERN_NOTICE
, sdkp
,
577 "constrained reads devices are not supported\n");
583 ret
= sd_zbc_check_capacity(sdkp
, buf
);
588 * Check zone size: only devices with a constant zone size (except
589 * an eventual last runt zone) that is a power of 2 are supported.
591 ret
= sd_zbc_check_zone_size(sdkp
);
595 /* The drive satisfies the kernel restrictions: set it up */
596 ret
= sd_zbc_setup(sdkp
);
600 /* READ16/WRITE16 is mandatory for ZBC disks */
601 sdkp
->device
->use_16_for_rw
= 1;
602 sdkp
->device
->use_10_for_rw
= 0;
612 void sd_zbc_remove(struct scsi_disk
*sdkp
)
614 kfree(sdkp
->zones_wlock
);
615 sdkp
->zones_wlock
= NULL
;
618 void sd_zbc_print_zones(struct scsi_disk
*sdkp
)
620 if (!sd_is_zoned(sdkp
) || !sdkp
->capacity
)
623 if (sdkp
->capacity
& (sdkp
->zone_blocks
- 1))
624 sd_printk(KERN_NOTICE
, sdkp
,
625 "%u zones of %u logical blocks + 1 runt zone\n",
629 sd_printk(KERN_NOTICE
, sdkp
,
630 "%u zones of %u logical blocks\n",