1 // SPDX-License-Identifier: GPL-2.0-only
3 * SCSI Zoned Block commands
5 * Copyright (C) 2014-2015 SUSE Linux GmbH
11 #include <linux/blkdev.h>
12 #include <linux/vmalloc.h>
13 #include <linux/sched/mm.h>
14 #include <linux/mutex.h>
16 #include <asm/unaligned.h>
18 #include <scsi/scsi.h>
19 #include <scsi/scsi_cmnd.h>
23 #define CREATE_TRACE_POINTS
27 * sd_zbc_get_zone_wp_offset - Get zone write pointer offset.
28 * @zone: Zone for which to return the write pointer offset.
30 * Return: offset of the write pointer from the start of the zone.
32 static unsigned int sd_zbc_get_zone_wp_offset(struct blk_zone *zone)
34 if (zone->type == ZBC_ZONE_TYPE_CONV)
38 case BLK_ZONE_COND_IMP_OPEN:
39 case BLK_ZONE_COND_EXP_OPEN:
40 case BLK_ZONE_COND_CLOSED:
41 return zone->wp - zone->start;
42 case BLK_ZONE_COND_FULL:
44 case BLK_ZONE_COND_EMPTY:
45 case BLK_ZONE_COND_OFFLINE:
46 case BLK_ZONE_COND_READONLY:
49 * Offline and read-only zones do not have a valid
50 * write pointer. Use 0 as for an empty zone.
56 /* Whether or not a SCSI zone descriptor describes a gap zone. */
57 static bool sd_zbc_is_gap_zone(const u8 buf[64])
59 return (buf[0] & 0xf) == ZBC_ZONE_TYPE_GAP;
63 * sd_zbc_parse_report - Parse a SCSI zone descriptor
64 * @sdkp: SCSI disk pointer.
65 * @buf: SCSI zone descriptor.
66 * @idx: Index of the zone relative to the first zone reported by the current
67 * sd_zbc_report_zones() call.
68 * @cb: Callback function pointer.
69 * @data: Second argument passed to @cb.
71 * Return: Value returned by @cb.
73 * Convert a SCSI zone descriptor into struct blk_zone format. Additionally,
74 * call @cb(blk_zone, @data).
76 static int sd_zbc_parse_report(struct scsi_disk *sdkp, const u8 buf[64],
77 unsigned int idx, report_zones_cb cb, void *data)
79 struct scsi_device *sdp = sdkp->device;
80 struct blk_zone zone = { 0 };
81 sector_t start_lba, gran;
84 if (WARN_ON_ONCE(sd_zbc_is_gap_zone(buf)))
87 zone.type = buf[0] & 0x0f;
88 zone.cond = (buf[1] >> 4) & 0xf;
94 start_lba = get_unaligned_be64(&buf[16]);
95 zone.start = logical_to_sectors(sdp, start_lba);
96 zone.capacity = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
97 zone.len = zone.capacity;
98 if (sdkp->zone_starting_lba_gran) {
99 gran = logical_to_sectors(sdp, sdkp->zone_starting_lba_gran);
100 if (zone.len > gran) {
101 sd_printk(KERN_ERR, sdkp,
102 "Invalid zone at LBA %llu with capacity %llu and length %llu; granularity = %llu\n",
104 sectors_to_logical(sdp, zone.capacity),
105 sectors_to_logical(sdp, zone.len),
106 sectors_to_logical(sdp, gran));
110 * Use the starting LBA granularity instead of the zone length
111 * obtained from the REPORT ZONES command.
115 if (zone.cond == ZBC_ZONE_COND_FULL)
116 zone.wp = zone.start + zone.len;
118 zone.wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
120 ret = cb(&zone, idx, data);
124 if (sdkp->rev_wp_offset)
125 sdkp->rev_wp_offset[idx] = sd_zbc_get_zone_wp_offset(&zone);
131 * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
132 * @sdkp: The target disk
133 * @buf: vmalloc-ed buffer to use for the reply
134 * @buflen: the buffer size
135 * @lba: Start LBA of the report
136 * @partial: Do partial report
138 * For internal use during device validation.
139 * Using partial=true can significantly speed up execution of a report zones
140 * command because the disk does not have to count all possible report matching
141 * zones and will only report the count of zones fitting in the command reply
144 static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
145 unsigned int buflen, sector_t lba,
148 struct scsi_device *sdp = sdkp->device;
149 const int timeout = sdp->request_queue->rq_timeout;
150 struct scsi_sense_hdr sshdr;
151 const struct scsi_exec_args exec_args = {
154 unsigned char cmd[16];
155 unsigned int rep_len;
160 cmd[1] = ZI_REPORT_ZONES;
161 put_unaligned_be64(lba, &cmd[2]);
162 put_unaligned_be32(buflen, &cmd[10]);
164 cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
166 result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, buf, buflen,
167 timeout, SD_MAX_RETRIES, &exec_args);
169 sd_printk(KERN_ERR, sdkp,
170 "REPORT ZONES start lba %llu failed\n", lba);
171 sd_print_result(sdkp, "REPORT ZONES", result);
172 if (result > 0 && scsi_sense_valid(&sshdr))
173 sd_print_sense_hdr(sdkp, &sshdr);
177 rep_len = get_unaligned_be32(&buf[0]);
179 sd_printk(KERN_ERR, sdkp,
180 "REPORT ZONES report invalid length %u\n",
189 * sd_zbc_alloc_report_buffer() - Allocate a buffer for report zones reply.
190 * @sdkp: The target disk
191 * @nr_zones: Maximum number of zones to report
192 * @buflen: Size of the buffer allocated
194 * Try to allocate a reply buffer for the number of requested zones.
195 * The size of the buffer allocated may be smaller than requested to
196 * satify the device constraint (max_hw_sectors, max_segments, etc).
198 * Return the address of the allocated buffer and update @buflen with
199 * the size of the allocated buffer.
201 static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
202 unsigned int nr_zones, size_t *buflen)
204 struct request_queue *q = sdkp->disk->queue;
209 * Report zone buffer size should be at most 64B times the number of
210 * zones requested plus the 64B reply header, but should be aligned
211 * to SECTOR_SIZE for ATA devices.
212 * Make sure that this size does not exceed the hardware capabilities.
213 * Furthermore, since the report zone command cannot be split, make
214 * sure that the allocated buffer can always be mapped by limiting the
215 * number of pages allocated to the HBA max segments limit.
217 nr_zones = min(nr_zones, sdkp->zone_info.nr_zones);
218 bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE);
219 bufsize = min_t(size_t, bufsize,
220 queue_max_hw_sectors(q) << SECTOR_SHIFT);
221 bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
223 while (bufsize >= SECTOR_SIZE) {
224 buf = __vmalloc(bufsize,
225 GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY);
230 bufsize = rounddown(bufsize >> 1, SECTOR_SIZE);
237 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
238 * @sdkp: The target disk
240 static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
242 return logical_to_sectors(sdkp->device, sdkp->zone_info.zone_blocks);
246 * sd_zbc_report_zones - SCSI .report_zones() callback.
247 * @disk: Disk to report zones for.
248 * @sector: Start sector.
249 * @nr_zones: Maximum number of zones to report.
250 * @cb: Callback function called to report zone information.
251 * @data: Second argument passed to @cb.
253 * Called by the block layer to iterate over zone information. See also the
254 * disk->fops->report_zones() calls in block/blk-zoned.c.
256 int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
257 unsigned int nr_zones, report_zones_cb cb, void *data)
259 struct scsi_disk *sdkp = scsi_disk(disk);
260 sector_t lba = sectors_to_logical(sdkp->device, sector);
263 u64 zone_length, start_lba;
264 size_t offset, buflen = 0;
268 if (!sd_is_zoned(sdkp))
269 /* Not a zoned device */
273 /* Device gone or invalid */
276 buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen);
280 while (zone_idx < nr_zones && lba < sdkp->capacity) {
281 ret = sd_zbc_do_report_zones(sdkp, buf, buflen, lba, true);
286 nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64);
290 for (i = 0; i < nr && zone_idx < nr_zones; i++) {
292 start_lba = get_unaligned_be64(&buf[offset + 16]);
293 zone_length = get_unaligned_be64(&buf[offset + 8]);
294 if ((zone_idx == 0 &&
296 lba >= start_lba + zone_length)) ||
297 (zone_idx > 0 && start_lba != lba) ||
298 start_lba + zone_length < start_lba) {
299 sd_printk(KERN_ERR, sdkp,
300 "Zone %d at LBA %llu is invalid: %llu + %llu\n",
301 zone_idx, lba, start_lba, zone_length);
305 lba = start_lba + zone_length;
306 if (sd_zbc_is_gap_zone(&buf[offset])) {
307 if (sdkp->zone_starting_lba_gran)
309 sd_printk(KERN_ERR, sdkp,
310 "Gap zone without constant LBA offsets\n");
315 ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx,
330 static blk_status_t sd_zbc_cmnd_checks(struct scsi_cmnd *cmd)
332 struct request *rq = scsi_cmd_to_rq(cmd);
333 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
334 sector_t sector = blk_rq_pos(rq);
336 if (!sd_is_zoned(sdkp))
337 /* Not a zoned device */
338 return BLK_STS_IOERR;
340 if (sdkp->device->changed)
341 return BLK_STS_IOERR;
343 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
344 /* Unaligned request */
345 return BLK_STS_IOERR;
350 #define SD_ZBC_INVALID_WP_OFST (~0u)
351 #define SD_ZBC_UPDATING_WP_OFST (SD_ZBC_INVALID_WP_OFST - 1)
353 static int sd_zbc_update_wp_offset_cb(struct blk_zone *zone, unsigned int idx,
356 struct scsi_disk *sdkp = data;
358 lockdep_assert_held(&sdkp->zones_wp_offset_lock);
360 sdkp->zones_wp_offset[idx] = sd_zbc_get_zone_wp_offset(zone);
366 * An attempt to append a zone triggered an invalid write pointer error.
367 * Reread the write pointer of the zone(s) in which the append failed.
369 static void sd_zbc_update_wp_offset_workfn(struct work_struct *work)
371 struct scsi_disk *sdkp;
376 sdkp = container_of(work, struct scsi_disk, zone_wp_offset_work);
378 spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
379 for (zno = 0; zno < sdkp->zone_info.nr_zones; zno++) {
380 if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST)
383 spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
384 ret = sd_zbc_do_report_zones(sdkp, sdkp->zone_wp_update_buf,
386 zno * sdkp->zone_info.zone_blocks, true);
387 spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
389 sd_zbc_parse_report(sdkp, sdkp->zone_wp_update_buf + 64,
390 zno, sd_zbc_update_wp_offset_cb,
393 spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
395 scsi_device_put(sdkp->device);
399 * sd_zbc_prepare_zone_append() - Prepare an emulated ZONE_APPEND command.
400 * @cmd: the command to setup
401 * @lba: the LBA to patch
402 * @nr_blocks: the number of LBAs to be written
404 * Called from sd_setup_read_write_cmnd() for REQ_OP_ZONE_APPEND.
405 * @sd_zbc_prepare_zone_append() handles the necessary zone wrote locking and
406 * patching of the lba for an emulated ZONE_APPEND command.
408 * In case the cached write pointer offset is %SD_ZBC_INVALID_WP_OFST it will
409 * schedule a REPORT ZONES command and return BLK_STS_IOERR.
411 blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba,
412 unsigned int nr_blocks)
414 struct request *rq = scsi_cmd_to_rq(cmd);
415 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
416 unsigned int wp_offset, zno = blk_rq_zone_no(rq);
420 ret = sd_zbc_cmnd_checks(cmd);
421 if (ret != BLK_STS_OK)
424 if (!blk_rq_zone_is_seq(rq))
425 return BLK_STS_IOERR;
427 /* Unlock of the write lock will happen in sd_zbc_complete() */
428 if (!blk_req_zone_write_trylock(rq))
429 return BLK_STS_ZONE_RESOURCE;
431 spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
432 wp_offset = sdkp->zones_wp_offset[zno];
434 case SD_ZBC_INVALID_WP_OFST:
436 * We are about to schedule work to update a zone write pointer
437 * offset, which will cause the zone append command to be
438 * requeued. So make sure that the scsi device does not go away
439 * while the work is being processed.
441 if (scsi_device_get(sdkp->device)) {
445 sdkp->zones_wp_offset[zno] = SD_ZBC_UPDATING_WP_OFST;
446 schedule_work(&sdkp->zone_wp_offset_work);
448 case SD_ZBC_UPDATING_WP_OFST:
449 ret = BLK_STS_DEV_RESOURCE;
452 wp_offset = sectors_to_logical(sdkp->device, wp_offset);
453 if (wp_offset + nr_blocks > sdkp->zone_info.zone_blocks) {
458 trace_scsi_prepare_zone_append(cmd, *lba, wp_offset);
461 spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
463 blk_req_zone_write_unlock(rq);
468 * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations
469 * can be RESET WRITE POINTER, OPEN, CLOSE or FINISH.
470 * @cmd: the command to setup
471 * @op: Operation to be performed
472 * @all: All zones control
474 * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL,
475 * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests.
477 blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
478 unsigned char op, bool all)
480 struct request *rq = scsi_cmd_to_rq(cmd);
481 sector_t sector = blk_rq_pos(rq);
482 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
483 sector_t block = sectors_to_logical(sdkp->device, sector);
486 ret = sd_zbc_cmnd_checks(cmd);
487 if (ret != BLK_STS_OK)
491 memset(cmd->cmnd, 0, cmd->cmd_len);
492 cmd->cmnd[0] = ZBC_OUT;
497 put_unaligned_be64(block, &cmd->cmnd[2]);
499 rq->timeout = SD_TIMEOUT;
500 cmd->sc_data_direction = DMA_NONE;
501 cmd->transfersize = 0;
507 static bool sd_zbc_need_zone_wp_update(struct request *rq)
509 switch (req_op(rq)) {
510 case REQ_OP_ZONE_APPEND:
511 case REQ_OP_ZONE_FINISH:
512 case REQ_OP_ZONE_RESET:
513 case REQ_OP_ZONE_RESET_ALL:
516 case REQ_OP_WRITE_ZEROES:
517 return blk_rq_zone_is_seq(rq);
524 * sd_zbc_zone_wp_update - Update cached zone write pointer upon cmd completion
525 * @cmd: Completed command
526 * @good_bytes: Command reply bytes
528 * Called from sd_zbc_complete() to handle the update of the cached zone write
529 * pointer value in case an update is needed.
531 static unsigned int sd_zbc_zone_wp_update(struct scsi_cmnd *cmd,
532 unsigned int good_bytes)
534 int result = cmd->result;
535 struct request *rq = scsi_cmd_to_rq(cmd);
536 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
537 unsigned int zno = blk_rq_zone_no(rq);
538 enum req_op op = req_op(rq);
542 * If we got an error for a command that needs updating the write
543 * pointer offset cache, we must mark the zone wp offset entry as
544 * invalid to force an update from disk the next time a zone append
547 spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
549 if (result && op != REQ_OP_ZONE_RESET_ALL) {
550 if (op == REQ_OP_ZONE_APPEND) {
551 /* Force complete completion (no retry) */
553 scsi_set_resid(cmd, blk_rq_bytes(rq));
557 * Force an update of the zone write pointer offset on
558 * the next zone append access.
560 if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST)
561 sdkp->zones_wp_offset[zno] = SD_ZBC_INVALID_WP_OFST;
562 goto unlock_wp_offset;
566 case REQ_OP_ZONE_APPEND:
567 trace_scsi_zone_wp_update(cmd, rq->__sector,
568 sdkp->zones_wp_offset[zno], good_bytes);
569 rq->__sector += sdkp->zones_wp_offset[zno];
571 case REQ_OP_WRITE_ZEROES:
573 if (sdkp->zones_wp_offset[zno] < sd_zbc_zone_sectors(sdkp))
574 sdkp->zones_wp_offset[zno] +=
575 good_bytes >> SECTOR_SHIFT;
577 case REQ_OP_ZONE_RESET:
578 sdkp->zones_wp_offset[zno] = 0;
580 case REQ_OP_ZONE_FINISH:
581 sdkp->zones_wp_offset[zno] = sd_zbc_zone_sectors(sdkp);
583 case REQ_OP_ZONE_RESET_ALL:
584 memset(sdkp->zones_wp_offset, 0,
585 sdkp->zone_info.nr_zones * sizeof(unsigned int));
592 spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
598 * sd_zbc_complete - ZBC command post processing.
599 * @cmd: Completed command
600 * @good_bytes: Command reply bytes
601 * @sshdr: command sense header
603 * Called from sd_done() to handle zone commands errors and updates to the
604 * device queue zone write pointer offset cahce.
606 unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
607 struct scsi_sense_hdr *sshdr)
609 int result = cmd->result;
610 struct request *rq = scsi_cmd_to_rq(cmd);
612 if (op_is_zone_mgmt(req_op(rq)) &&
614 sshdr->sense_key == ILLEGAL_REQUEST &&
615 sshdr->asc == 0x24) {
617 * INVALID FIELD IN CDB error: a zone management command was
618 * attempted on a conventional zone. Nothing to worry about,
619 * so be quiet about the error.
621 rq->rq_flags |= RQF_QUIET;
622 } else if (sd_zbc_need_zone_wp_update(rq))
623 good_bytes = sd_zbc_zone_wp_update(cmd, good_bytes);
625 if (req_op(rq) == REQ_OP_ZONE_APPEND)
626 blk_req_zone_write_unlock(rq);
632 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
634 * @buf: Buffer where to store the VPD page data
636 * Read VPD page B6, get information and check that reads are unconstrained.
638 static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
641 u64 zone_starting_lba_gran;
643 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
644 sd_printk(KERN_NOTICE, sdkp,
645 "Read zoned characteristics VPD page failed\n");
649 if (sdkp->device->type != TYPE_ZBC) {
652 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
653 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
654 sdkp->zones_max_open = 0;
659 sdkp->urswrz = buf[4] & 1;
660 sdkp->zones_optimal_open = 0;
661 sdkp->zones_optimal_nonseq = 0;
662 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
663 /* Check zone alignment method */
664 switch (buf[23] & 0xf) {
666 case ZBC_CONSTANT_ZONE_LENGTH:
667 /* Use zone length */
669 case ZBC_CONSTANT_ZONE_START_OFFSET:
670 zone_starting_lba_gran = get_unaligned_be64(&buf[24]);
671 if (zone_starting_lba_gran == 0 ||
672 !is_power_of_2(zone_starting_lba_gran) ||
673 logical_to_sectors(sdkp->device, zone_starting_lba_gran) >
675 sd_printk(KERN_ERR, sdkp,
676 "Invalid zone starting LBA granularity %llu\n",
677 zone_starting_lba_gran);
680 sdkp->zone_starting_lba_gran = zone_starting_lba_gran;
683 sd_printk(KERN_ERR, sdkp, "Invalid zone alignment method\n");
688 * Check for unconstrained reads: host-managed devices with
689 * constrained reads (drives failing read after write pointer)
693 if (sdkp->first_scan)
694 sd_printk(KERN_NOTICE, sdkp,
695 "constrained reads devices are not supported\n");
703 * sd_zbc_check_capacity - Check the device capacity
705 * @buf: command buffer
706 * @zblocks: zone size in logical blocks
708 * Get the device zone size and check that the device capacity as reported
709 * by READ CAPACITY matches the max_lba value (plus one) of the report zones
710 * command reply for devices with RC_BASIS == 0.
712 * Returns 0 upon success or an error code upon failure.
714 static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
722 /* Do a report zone to get max_lba and the size of the first zone */
723 ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);
727 if (sdkp->rc_basis == 0) {
728 /* The max_lba field is the capacity of this device */
729 max_lba = get_unaligned_be64(&buf[8]);
730 if (sdkp->capacity != max_lba + 1) {
731 if (sdkp->first_scan)
732 sd_printk(KERN_WARNING, sdkp,
733 "Changing capacity from %llu to max LBA+1 %llu\n",
734 (unsigned long long)sdkp->capacity,
735 (unsigned long long)max_lba + 1);
736 sdkp->capacity = max_lba + 1;
740 if (sdkp->zone_starting_lba_gran == 0) {
741 /* Get the size of the first reported zone */
743 zone_blocks = get_unaligned_be64(&rec[8]);
744 if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
745 if (sdkp->first_scan)
746 sd_printk(KERN_NOTICE, sdkp,
747 "Zone size too large\n");
751 zone_blocks = sdkp->zone_starting_lba_gran;
754 if (!is_power_of_2(zone_blocks)) {
755 sd_printk(KERN_ERR, sdkp,
756 "Zone size %llu is not a power of two.\n",
761 *zblocks = zone_blocks;
766 static void sd_zbc_print_zones(struct scsi_disk *sdkp)
768 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
771 if (sdkp->capacity & (sdkp->zone_info.zone_blocks - 1))
772 sd_printk(KERN_NOTICE, sdkp,
773 "%u zones of %u logical blocks + 1 runt zone\n",
774 sdkp->zone_info.nr_zones - 1,
775 sdkp->zone_info.zone_blocks);
777 sd_printk(KERN_NOTICE, sdkp,
778 "%u zones of %u logical blocks\n",
779 sdkp->zone_info.nr_zones,
780 sdkp->zone_info.zone_blocks);
783 static int sd_zbc_init_disk(struct scsi_disk *sdkp)
785 sdkp->zones_wp_offset = NULL;
786 spin_lock_init(&sdkp->zones_wp_offset_lock);
787 sdkp->rev_wp_offset = NULL;
788 mutex_init(&sdkp->rev_mutex);
789 INIT_WORK(&sdkp->zone_wp_offset_work, sd_zbc_update_wp_offset_workfn);
790 sdkp->zone_wp_update_buf = kzalloc(SD_BUF_SIZE, GFP_KERNEL);
791 if (!sdkp->zone_wp_update_buf)
797 void sd_zbc_free_zone_info(struct scsi_disk *sdkp)
799 if (!sdkp->zone_wp_update_buf)
802 /* Serialize against revalidate zones */
803 mutex_lock(&sdkp->rev_mutex);
805 kvfree(sdkp->zones_wp_offset);
806 sdkp->zones_wp_offset = NULL;
807 kfree(sdkp->zone_wp_update_buf);
808 sdkp->zone_wp_update_buf = NULL;
810 sdkp->early_zone_info = (struct zoned_disk_info){ };
811 sdkp->zone_info = (struct zoned_disk_info){ };
813 mutex_unlock(&sdkp->rev_mutex);
816 static void sd_zbc_revalidate_zones_cb(struct gendisk *disk)
818 struct scsi_disk *sdkp = scsi_disk(disk);
820 swap(sdkp->zones_wp_offset, sdkp->rev_wp_offset);
824 * Call blk_revalidate_disk_zones() if any of the zoned disk properties have
825 * changed that make it necessary to call that function. Called by
826 * sd_revalidate_disk() after the gendisk capacity has been set.
828 int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
830 struct gendisk *disk = sdkp->disk;
831 struct request_queue *q = disk->queue;
832 u32 zone_blocks = sdkp->early_zone_info.zone_blocks;
833 unsigned int nr_zones = sdkp->early_zone_info.nr_zones;
838 * For all zoned disks, initialize zone append emulation data if not
839 * already done. This is necessary also for host-aware disks used as
840 * regular disks due to the presence of partitions as these partitions
841 * may be deleted and the disk zoned model changed back from
842 * BLK_ZONED_NONE to BLK_ZONED_HA.
844 if (sd_is_zoned(sdkp) && !sdkp->zone_wp_update_buf) {
845 ret = sd_zbc_init_disk(sdkp);
851 * There is nothing to do for regular disks, including host-aware disks
852 * that have partitions.
854 if (!blk_queue_is_zoned(q))
858 * Make sure revalidate zones are serialized to ensure exclusive
859 * updates of the scsi disk data.
861 mutex_lock(&sdkp->rev_mutex);
863 if (sdkp->zone_info.zone_blocks == zone_blocks &&
864 sdkp->zone_info.nr_zones == nr_zones &&
865 disk->nr_zones == nr_zones)
868 flags = memalloc_noio_save();
869 sdkp->zone_info.zone_blocks = zone_blocks;
870 sdkp->zone_info.nr_zones = nr_zones;
871 sdkp->rev_wp_offset = kvcalloc(nr_zones, sizeof(u32), GFP_KERNEL);
872 if (!sdkp->rev_wp_offset) {
874 memalloc_noio_restore(flags);
878 blk_queue_chunk_sectors(q,
879 logical_to_sectors(sdkp->device, zone_blocks));
880 blk_queue_max_zone_append_sectors(q,
881 q->limits.max_segments << PAGE_SECTORS_SHIFT);
883 ret = blk_revalidate_disk_zones(disk, sd_zbc_revalidate_zones_cb);
885 memalloc_noio_restore(flags);
886 kvfree(sdkp->rev_wp_offset);
887 sdkp->rev_wp_offset = NULL;
890 sdkp->zone_info = (struct zoned_disk_info){ };
895 sd_zbc_print_zones(sdkp);
898 mutex_unlock(&sdkp->rev_mutex);
904 * sd_zbc_read_zones - Read zone information and update the request queue
905 * @sdkp: SCSI disk pointer.
906 * @buf: 512 byte buffer used for storing SCSI command output.
908 * Read zone information and update the request queue zone characteristics and
909 * also the zoned device information in *sdkp. Called by sd_revalidate_disk()
910 * before the gendisk capacity has been set.
912 int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE])
914 struct gendisk *disk = sdkp->disk;
915 struct request_queue *q = disk->queue;
916 unsigned int nr_zones;
920 if (!sd_is_zoned(sdkp)) {
922 * Device managed or normal SCSI disk, no special handling
923 * required. Nevertheless, free the disk zone information in
924 * case the device type changed.
926 sd_zbc_free_zone_info(sdkp);
930 /* READ16/WRITE16/SYNC16 is mandatory for ZBC devices */
931 sdkp->device->use_16_for_rw = 1;
932 sdkp->device->use_10_for_rw = 0;
933 sdkp->device->use_16_for_sync = 1;
935 if (!blk_queue_is_zoned(q)) {
937 * This can happen for a host aware disk with partitions.
938 * The block device zone model was already cleared by
939 * disk_set_zoned(). Only free the scsi disk zone
940 * information and exit early.
942 sd_zbc_free_zone_info(sdkp);
946 /* Check zoned block device characteristics (unconstrained reads) */
947 ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
951 /* Check the device capacity reported by report zones */
952 ret = sd_zbc_check_capacity(sdkp, buf, &zone_blocks);
956 /* The drive satisfies the kernel restrictions: set it up */
957 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
958 blk_queue_required_elevator_features(q, ELEVATOR_F_ZBD_SEQ_WRITE);
959 if (sdkp->zones_max_open == U32_MAX)
960 disk_set_max_open_zones(disk, 0);
962 disk_set_max_open_zones(disk, sdkp->zones_max_open);
963 disk_set_max_active_zones(disk, 0);
964 nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
966 sdkp->early_zone_info.nr_zones = nr_zones;
967 sdkp->early_zone_info.zone_blocks = zone_blocks;