Thomas Gleixner | a98c5b1 | 2019-06-01 10:08:25 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 2 | /* |
| 3 | * SCSI Zoned Block commands |
| 4 | * |
| 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> |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/blkdev.h> |
Damien Le Moal | b091ac6 | 2019-07-01 14:09:17 +0900 | [diff] [blame] | 12 | #include <linux/vmalloc.h> |
| 13 | #include <linux/sched/mm.h> |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 14 | #include <linux/mutex.h> |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 15 | |
| 16 | #include <asm/unaligned.h> |
| 17 | |
| 18 | #include <scsi/scsi.h> |
| 19 | #include <scsi/scsi_cmnd.h> |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 20 | |
| 21 | #include "sd.h" |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 22 | |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 23 | static unsigned int sd_zbc_get_zone_wp_offset(struct blk_zone *zone) |
| 24 | { |
| 25 | if (zone->type == ZBC_ZONE_TYPE_CONV) |
| 26 | return 0; |
| 27 | |
| 28 | switch (zone->cond) { |
| 29 | case BLK_ZONE_COND_IMP_OPEN: |
| 30 | case BLK_ZONE_COND_EXP_OPEN: |
| 31 | case BLK_ZONE_COND_CLOSED: |
| 32 | return zone->wp - zone->start; |
| 33 | case BLK_ZONE_COND_FULL: |
| 34 | return zone->len; |
| 35 | case BLK_ZONE_COND_EMPTY: |
| 36 | case BLK_ZONE_COND_OFFLINE: |
| 37 | case BLK_ZONE_COND_READONLY: |
| 38 | default: |
| 39 | /* |
| 40 | * Offline and read-only zones do not have a valid |
| 41 | * write pointer. Use 0 as for an empty zone. |
| 42 | */ |
| 43 | return 0; |
| 44 | } |
| 45 | } |
| 46 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 47 | static int sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf, |
| 48 | unsigned int idx, report_zones_cb cb, void *data) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 49 | { |
| 50 | struct scsi_device *sdp = sdkp->device; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 51 | struct blk_zone zone = { 0 }; |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 52 | int ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 53 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 54 | zone.type = buf[0] & 0x0f; |
| 55 | zone.cond = (buf[1] >> 4) & 0xf; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 56 | if (buf[1] & 0x01) |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 57 | zone.reset = 1; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 58 | if (buf[1] & 0x02) |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 59 | zone.non_seq = 1; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 60 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 61 | zone.len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8])); |
Matias Bjørling | 82394db | 2020-06-29 12:06:37 -0700 | [diff] [blame] | 62 | zone.capacity = zone.len; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 63 | zone.start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16])); |
| 64 | zone.wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24])); |
| 65 | if (zone.type != ZBC_ZONE_TYPE_CONV && |
| 66 | zone.cond == ZBC_ZONE_COND_FULL) |
| 67 | zone.wp = zone.start + zone.len; |
| 68 | |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 69 | ret = cb(&zone, idx, data); |
| 70 | if (ret) |
| 71 | return ret; |
| 72 | |
| 73 | if (sdkp->rev_wp_offset) |
| 74 | sdkp->rev_wp_offset[idx] = sd_zbc_get_zone_wp_offset(&zone); |
| 75 | |
| 76 | return 0; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 80 | * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command. |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 81 | * @sdkp: The target disk |
Damien Le Moal | b091ac6 | 2019-07-01 14:09:17 +0900 | [diff] [blame] | 82 | * @buf: vmalloc-ed buffer to use for the reply |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 83 | * @buflen: the buffer size |
| 84 | * @lba: Start LBA of the report |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 85 | * @partial: Do partial report |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 86 | * |
| 87 | * For internal use during device validation. |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 88 | * Using partial=true can significantly speed up execution of a report zones |
| 89 | * command because the disk does not have to count all possible report matching |
| 90 | * zones and will only report the count of zones fitting in the command reply |
| 91 | * buffer. |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 92 | */ |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 93 | static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf, |
| 94 | unsigned int buflen, sector_t lba, |
| 95 | bool partial) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 96 | { |
| 97 | struct scsi_device *sdp = sdkp->device; |
| 98 | const int timeout = sdp->request_queue->rq_timeout; |
| 99 | struct scsi_sense_hdr sshdr; |
| 100 | unsigned char cmd[16]; |
| 101 | unsigned int rep_len; |
| 102 | int result; |
| 103 | |
| 104 | memset(cmd, 0, 16); |
| 105 | cmd[0] = ZBC_IN; |
| 106 | cmd[1] = ZI_REPORT_ZONES; |
| 107 | put_unaligned_be64(lba, &cmd[2]); |
| 108 | put_unaligned_be32(buflen, &cmd[10]); |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 109 | if (partial) |
| 110 | cmd[14] = ZBC_REPORT_ZONE_PARTIAL; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 111 | |
| 112 | result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, |
| 113 | buf, buflen, &sshdr, |
| 114 | timeout, SD_MAX_RETRIES, NULL); |
| 115 | if (result) { |
| 116 | sd_printk(KERN_ERR, sdkp, |
Damien Le Moal | a35989a | 2019-11-25 16:05:18 +0900 | [diff] [blame] | 117 | "REPORT ZONES start lba %llu failed\n", lba); |
| 118 | sd_print_result(sdkp, "REPORT ZONES", result); |
Hannes Reinecke | 464a00c | 2021-04-27 10:30:15 +0200 | [diff] [blame] | 119 | if (result > 0 && scsi_sense_valid(&sshdr)) |
Damien Le Moal | a35989a | 2019-11-25 16:05:18 +0900 | [diff] [blame] | 120 | sd_print_sense_hdr(sdkp, &sshdr); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 121 | return -EIO; |
| 122 | } |
| 123 | |
| 124 | rep_len = get_unaligned_be32(&buf[0]); |
| 125 | if (rep_len < 64) { |
| 126 | sd_printk(KERN_ERR, sdkp, |
| 127 | "REPORT ZONES report invalid length %u\n", |
| 128 | rep_len); |
| 129 | return -EIO; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Damien Le Moal | b091ac6 | 2019-07-01 14:09:17 +0900 | [diff] [blame] | 135 | /** |
Lee Jones | 59863cb | 2021-03-17 09:11:58 +0000 | [diff] [blame] | 136 | * sd_zbc_alloc_report_buffer() - Allocate a buffer for report zones reply. |
Damien Le Moal | b091ac6 | 2019-07-01 14:09:17 +0900 | [diff] [blame] | 137 | * @sdkp: The target disk |
| 138 | * @nr_zones: Maximum number of zones to report |
| 139 | * @buflen: Size of the buffer allocated |
| 140 | * |
| 141 | * Try to allocate a reply buffer for the number of requested zones. |
| 142 | * The size of the buffer allocated may be smaller than requested to |
| 143 | * satify the device constraint (max_hw_sectors, max_segments, etc). |
| 144 | * |
| 145 | * Return the address of the allocated buffer and update @buflen with |
| 146 | * the size of the allocated buffer. |
| 147 | */ |
| 148 | static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp, |
| 149 | unsigned int nr_zones, size_t *buflen) |
| 150 | { |
| 151 | struct request_queue *q = sdkp->disk->queue; |
| 152 | size_t bufsize; |
| 153 | void *buf; |
| 154 | |
| 155 | /* |
| 156 | * Report zone buffer size should be at most 64B times the number of |
Naohiro Aota | 7215e90 | 2021-09-06 23:06:42 +0900 | [diff] [blame] | 157 | * zones requested plus the 64B reply header, but should be aligned |
| 158 | * to SECTOR_SIZE for ATA devices. |
Damien Le Moal | b091ac6 | 2019-07-01 14:09:17 +0900 | [diff] [blame] | 159 | * Make sure that this size does not exceed the hardware capabilities. |
| 160 | * Furthermore, since the report zone command cannot be split, make |
| 161 | * sure that the allocated buffer can always be mapped by limiting the |
| 162 | * number of pages allocated to the HBA max segments limit. |
| 163 | */ |
Damien Le Moal | 23a5086 | 2019-11-11 11:39:29 +0900 | [diff] [blame] | 164 | nr_zones = min(nr_zones, sdkp->nr_zones); |
| 165 | bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE); |
Damien Le Moal | b091ac6 | 2019-07-01 14:09:17 +0900 | [diff] [blame] | 166 | bufsize = min_t(size_t, bufsize, |
| 167 | queue_max_hw_sectors(q) << SECTOR_SHIFT); |
| 168 | bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT); |
| 169 | |
Damien Le Moal | 23a5086 | 2019-11-11 11:39:29 +0900 | [diff] [blame] | 170 | while (bufsize >= SECTOR_SIZE) { |
| 171 | buf = __vmalloc(bufsize, |
Christoph Hellwig | 88dca4c | 2020-06-01 21:51:40 -0700 | [diff] [blame] | 172 | GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY); |
Damien Le Moal | 23a5086 | 2019-11-11 11:39:29 +0900 | [diff] [blame] | 173 | if (buf) { |
| 174 | *buflen = bufsize; |
| 175 | return buf; |
| 176 | } |
Naohiro Aota | 7215e90 | 2021-09-06 23:06:42 +0900 | [diff] [blame] | 177 | bufsize = rounddown(bufsize >> 1, SECTOR_SIZE); |
Damien Le Moal | 23a5086 | 2019-11-11 11:39:29 +0900 | [diff] [blame] | 178 | } |
Damien Le Moal | b091ac6 | 2019-07-01 14:09:17 +0900 | [diff] [blame] | 179 | |
Damien Le Moal | 23a5086 | 2019-11-11 11:39:29 +0900 | [diff] [blame] | 180 | return NULL; |
Damien Le Moal | b091ac6 | 2019-07-01 14:09:17 +0900 | [diff] [blame] | 181 | } |
| 182 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 183 | /** |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 184 | * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors. |
| 185 | * @sdkp: The target disk |
| 186 | */ |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 187 | static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp) |
| 188 | { |
| 189 | return logical_to_sectors(sdkp->device, sdkp->zone_blocks); |
| 190 | } |
| 191 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 192 | int sd_zbc_report_zones(struct gendisk *disk, sector_t sector, |
| 193 | unsigned int nr_zones, report_zones_cb cb, void *data) |
| 194 | { |
| 195 | struct scsi_disk *sdkp = scsi_disk(disk); |
Damien Le Moal | 51fdaa0 | 2020-02-19 15:38:00 +0900 | [diff] [blame] | 196 | sector_t capacity = logical_to_sectors(sdkp->device, sdkp->capacity); |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 197 | unsigned int nr, i; |
| 198 | unsigned char *buf; |
| 199 | size_t offset, buflen = 0; |
| 200 | int zone_idx = 0; |
| 201 | int ret; |
| 202 | |
| 203 | if (!sd_is_zoned(sdkp)) |
| 204 | /* Not a zoned device */ |
| 205 | return -EOPNOTSUPP; |
| 206 | |
Damien Le Moal | 51fdaa0 | 2020-02-19 15:38:00 +0900 | [diff] [blame] | 207 | if (!capacity) |
| 208 | /* Device gone or invalid */ |
| 209 | return -ENODEV; |
| 210 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 211 | buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen); |
| 212 | if (!buf) |
| 213 | return -ENOMEM; |
| 214 | |
Damien Le Moal | 51fdaa0 | 2020-02-19 15:38:00 +0900 | [diff] [blame] | 215 | while (zone_idx < nr_zones && sector < capacity) { |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 216 | ret = sd_zbc_do_report_zones(sdkp, buf, buflen, |
| 217 | sectors_to_logical(sdkp->device, sector), true); |
| 218 | if (ret) |
| 219 | goto out; |
| 220 | |
| 221 | offset = 0; |
| 222 | nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64); |
| 223 | if (!nr) |
| 224 | break; |
| 225 | |
| 226 | for (i = 0; i < nr && zone_idx < nr_zones; i++) { |
| 227 | offset += 64; |
| 228 | ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx, |
| 229 | cb, data); |
| 230 | if (ret) |
| 231 | goto out; |
| 232 | zone_idx++; |
| 233 | } |
| 234 | |
| 235 | sector += sd_zbc_zone_sectors(sdkp) * i; |
| 236 | } |
| 237 | |
| 238 | ret = zone_idx; |
| 239 | out: |
| 240 | kvfree(buf); |
| 241 | return ret; |
| 242 | } |
| 243 | |
Johannes Thumshirn | 02494d3 | 2020-05-12 17:55:50 +0900 | [diff] [blame] | 244 | static blk_status_t sd_zbc_cmnd_checks(struct scsi_cmnd *cmd) |
| 245 | { |
Bart Van Assche | 5999ccf | 2021-08-09 16:03:06 -0700 | [diff] [blame] | 246 | struct request *rq = scsi_cmd_to_rq(cmd); |
Johannes Thumshirn | 02494d3 | 2020-05-12 17:55:50 +0900 | [diff] [blame] | 247 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 248 | sector_t sector = blk_rq_pos(rq); |
| 249 | |
| 250 | if (!sd_is_zoned(sdkp)) |
| 251 | /* Not a zoned device */ |
| 252 | return BLK_STS_IOERR; |
| 253 | |
| 254 | if (sdkp->device->changed) |
| 255 | return BLK_STS_IOERR; |
| 256 | |
| 257 | if (sector & (sd_zbc_zone_sectors(sdkp) - 1)) |
| 258 | /* Unaligned request */ |
| 259 | return BLK_STS_IOERR; |
| 260 | |
| 261 | return BLK_STS_OK; |
| 262 | } |
| 263 | |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 264 | #define SD_ZBC_INVALID_WP_OFST (~0u) |
| 265 | #define SD_ZBC_UPDATING_WP_OFST (SD_ZBC_INVALID_WP_OFST - 1) |
| 266 | |
| 267 | static int sd_zbc_update_wp_offset_cb(struct blk_zone *zone, unsigned int idx, |
| 268 | void *data) |
| 269 | { |
| 270 | struct scsi_disk *sdkp = data; |
| 271 | |
| 272 | lockdep_assert_held(&sdkp->zones_wp_offset_lock); |
| 273 | |
| 274 | sdkp->zones_wp_offset[idx] = sd_zbc_get_zone_wp_offset(zone); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static void sd_zbc_update_wp_offset_workfn(struct work_struct *work) |
| 280 | { |
| 281 | struct scsi_disk *sdkp; |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 282 | unsigned long flags; |
Bart Van Assche | 1d479e6 | 2021-09-17 14:23:14 -0700 | [diff] [blame] | 283 | sector_t zno; |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 284 | int ret; |
| 285 | |
| 286 | sdkp = container_of(work, struct scsi_disk, zone_wp_offset_work); |
| 287 | |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 288 | spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 289 | for (zno = 0; zno < sdkp->nr_zones; zno++) { |
| 290 | if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST) |
| 291 | continue; |
| 292 | |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 293 | spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 294 | ret = sd_zbc_do_report_zones(sdkp, sdkp->zone_wp_update_buf, |
| 295 | SD_BUF_SIZE, |
| 296 | zno * sdkp->zone_blocks, true); |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 297 | spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 298 | if (!ret) |
| 299 | sd_zbc_parse_report(sdkp, sdkp->zone_wp_update_buf + 64, |
| 300 | zno, sd_zbc_update_wp_offset_cb, |
| 301 | sdkp); |
| 302 | } |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 303 | spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 304 | |
| 305 | scsi_device_put(sdkp->device); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * sd_zbc_prepare_zone_append() - Prepare an emulated ZONE_APPEND command. |
| 310 | * @cmd: the command to setup |
| 311 | * @lba: the LBA to patch |
| 312 | * @nr_blocks: the number of LBAs to be written |
| 313 | * |
| 314 | * Called from sd_setup_read_write_cmnd() for REQ_OP_ZONE_APPEND. |
| 315 | * @sd_zbc_prepare_zone_append() handles the necessary zone wrote locking and |
| 316 | * patching of the lba for an emulated ZONE_APPEND command. |
| 317 | * |
| 318 | * In case the cached write pointer offset is %SD_ZBC_INVALID_WP_OFST it will |
| 319 | * schedule a REPORT ZONES command and return BLK_STS_IOERR. |
| 320 | */ |
| 321 | blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba, |
| 322 | unsigned int nr_blocks) |
| 323 | { |
Bart Van Assche | 5999ccf | 2021-08-09 16:03:06 -0700 | [diff] [blame] | 324 | struct request *rq = scsi_cmd_to_rq(cmd); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 325 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 326 | unsigned int wp_offset, zno = blk_rq_zone_no(rq); |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 327 | unsigned long flags; |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 328 | blk_status_t ret; |
| 329 | |
| 330 | ret = sd_zbc_cmnd_checks(cmd); |
| 331 | if (ret != BLK_STS_OK) |
| 332 | return ret; |
| 333 | |
| 334 | if (!blk_rq_zone_is_seq(rq)) |
| 335 | return BLK_STS_IOERR; |
| 336 | |
| 337 | /* Unlock of the write lock will happen in sd_zbc_complete() */ |
| 338 | if (!blk_req_zone_write_trylock(rq)) |
| 339 | return BLK_STS_ZONE_RESOURCE; |
| 340 | |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 341 | spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 342 | wp_offset = sdkp->zones_wp_offset[zno]; |
| 343 | switch (wp_offset) { |
| 344 | case SD_ZBC_INVALID_WP_OFST: |
| 345 | /* |
| 346 | * We are about to schedule work to update a zone write pointer |
| 347 | * offset, which will cause the zone append command to be |
| 348 | * requeued. So make sure that the scsi device does not go away |
| 349 | * while the work is being processed. |
| 350 | */ |
| 351 | if (scsi_device_get(sdkp->device)) { |
| 352 | ret = BLK_STS_IOERR; |
| 353 | break; |
| 354 | } |
| 355 | sdkp->zones_wp_offset[zno] = SD_ZBC_UPDATING_WP_OFST; |
| 356 | schedule_work(&sdkp->zone_wp_offset_work); |
| 357 | fallthrough; |
| 358 | case SD_ZBC_UPDATING_WP_OFST: |
| 359 | ret = BLK_STS_DEV_RESOURCE; |
| 360 | break; |
| 361 | default: |
| 362 | wp_offset = sectors_to_logical(sdkp->device, wp_offset); |
| 363 | if (wp_offset + nr_blocks > sdkp->zone_blocks) { |
| 364 | ret = BLK_STS_IOERR; |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | *lba += wp_offset; |
| 369 | } |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 370 | spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 371 | if (ret) |
| 372 | blk_req_zone_write_unlock(rq); |
| 373 | return ret; |
| 374 | } |
| 375 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 376 | /** |
Ajay Joshi | ad512f2 | 2019-10-27 23:05:47 +0900 | [diff] [blame] | 377 | * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations |
| 378 | * can be RESET WRITE POINTER, OPEN, CLOSE or FINISH. |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 379 | * @cmd: the command to setup |
Ajay Joshi | ad512f2 | 2019-10-27 23:05:47 +0900 | [diff] [blame] | 380 | * @op: Operation to be performed |
| 381 | * @all: All zones control |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 382 | * |
Ajay Joshi | ad512f2 | 2019-10-27 23:05:47 +0900 | [diff] [blame] | 383 | * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL, |
| 384 | * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests. |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 385 | */ |
Ajay Joshi | ad512f2 | 2019-10-27 23:05:47 +0900 | [diff] [blame] | 386 | blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd, |
| 387 | unsigned char op, bool all) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 388 | { |
Bart Van Assche | 5999ccf | 2021-08-09 16:03:06 -0700 | [diff] [blame] | 389 | struct request *rq = scsi_cmd_to_rq(cmd); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 390 | sector_t sector = blk_rq_pos(rq); |
Johannes Thumshirn | 02494d3 | 2020-05-12 17:55:50 +0900 | [diff] [blame] | 391 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 392 | sector_t block = sectors_to_logical(sdkp->device, sector); |
Johannes Thumshirn | 02494d3 | 2020-05-12 17:55:50 +0900 | [diff] [blame] | 393 | blk_status_t ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 394 | |
Johannes Thumshirn | 02494d3 | 2020-05-12 17:55:50 +0900 | [diff] [blame] | 395 | ret = sd_zbc_cmnd_checks(cmd); |
| 396 | if (ret != BLK_STS_OK) |
| 397 | return ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 398 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 399 | cmd->cmd_len = 16; |
| 400 | memset(cmd->cmnd, 0, cmd->cmd_len); |
| 401 | cmd->cmnd[0] = ZBC_OUT; |
Ajay Joshi | ad512f2 | 2019-10-27 23:05:47 +0900 | [diff] [blame] | 402 | cmd->cmnd[1] = op; |
Chaitanya Kulkarni | d81e9d4 | 2019-08-01 10:26:37 -0700 | [diff] [blame] | 403 | if (all) |
| 404 | cmd->cmnd[14] = 0x1; |
| 405 | else |
| 406 | put_unaligned_be64(block, &cmd->cmnd[2]); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 407 | |
| 408 | rq->timeout = SD_TIMEOUT; |
| 409 | cmd->sc_data_direction = DMA_NONE; |
| 410 | cmd->transfersize = 0; |
| 411 | cmd->allowed = 0; |
| 412 | |
Christoph Hellwig | 159b2cb | 2018-11-09 14:42:39 +0100 | [diff] [blame] | 413 | return BLK_STS_OK; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 414 | } |
| 415 | |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 416 | static bool sd_zbc_need_zone_wp_update(struct request *rq) |
| 417 | { |
| 418 | switch (req_op(rq)) { |
| 419 | case REQ_OP_ZONE_APPEND: |
| 420 | case REQ_OP_ZONE_FINISH: |
| 421 | case REQ_OP_ZONE_RESET: |
| 422 | case REQ_OP_ZONE_RESET_ALL: |
| 423 | return true; |
| 424 | case REQ_OP_WRITE: |
| 425 | case REQ_OP_WRITE_ZEROES: |
| 426 | case REQ_OP_WRITE_SAME: |
| 427 | return blk_rq_zone_is_seq(rq); |
| 428 | default: |
| 429 | return false; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * sd_zbc_zone_wp_update - Update cached zone write pointer upon cmd completion |
| 435 | * @cmd: Completed command |
| 436 | * @good_bytes: Command reply bytes |
| 437 | * |
| 438 | * Called from sd_zbc_complete() to handle the update of the cached zone write |
| 439 | * pointer value in case an update is needed. |
| 440 | */ |
| 441 | static unsigned int sd_zbc_zone_wp_update(struct scsi_cmnd *cmd, |
| 442 | unsigned int good_bytes) |
| 443 | { |
| 444 | int result = cmd->result; |
Bart Van Assche | 5999ccf | 2021-08-09 16:03:06 -0700 | [diff] [blame] | 445 | struct request *rq = scsi_cmd_to_rq(cmd); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 446 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 447 | unsigned int zno = blk_rq_zone_no(rq); |
| 448 | enum req_opf op = req_op(rq); |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 449 | unsigned long flags; |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 450 | |
| 451 | /* |
| 452 | * If we got an error for a command that needs updating the write |
| 453 | * pointer offset cache, we must mark the zone wp offset entry as |
| 454 | * invalid to force an update from disk the next time a zone append |
| 455 | * command is issued. |
| 456 | */ |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 457 | spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 458 | |
| 459 | if (result && op != REQ_OP_ZONE_RESET_ALL) { |
| 460 | if (op == REQ_OP_ZONE_APPEND) { |
| 461 | /* Force complete completion (no retry) */ |
| 462 | good_bytes = 0; |
| 463 | scsi_set_resid(cmd, blk_rq_bytes(rq)); |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * Force an update of the zone write pointer offset on |
| 468 | * the next zone append access. |
| 469 | */ |
| 470 | if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST) |
| 471 | sdkp->zones_wp_offset[zno] = SD_ZBC_INVALID_WP_OFST; |
| 472 | goto unlock_wp_offset; |
| 473 | } |
| 474 | |
| 475 | switch (op) { |
| 476 | case REQ_OP_ZONE_APPEND: |
| 477 | rq->__sector += sdkp->zones_wp_offset[zno]; |
| 478 | fallthrough; |
| 479 | case REQ_OP_WRITE_ZEROES: |
| 480 | case REQ_OP_WRITE_SAME: |
| 481 | case REQ_OP_WRITE: |
| 482 | if (sdkp->zones_wp_offset[zno] < sd_zbc_zone_sectors(sdkp)) |
| 483 | sdkp->zones_wp_offset[zno] += |
| 484 | good_bytes >> SECTOR_SHIFT; |
| 485 | break; |
| 486 | case REQ_OP_ZONE_RESET: |
| 487 | sdkp->zones_wp_offset[zno] = 0; |
| 488 | break; |
| 489 | case REQ_OP_ZONE_FINISH: |
| 490 | sdkp->zones_wp_offset[zno] = sd_zbc_zone_sectors(sdkp); |
| 491 | break; |
| 492 | case REQ_OP_ZONE_RESET_ALL: |
| 493 | memset(sdkp->zones_wp_offset, 0, |
| 494 | sdkp->nr_zones * sizeof(unsigned int)); |
| 495 | break; |
| 496 | default: |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | unlock_wp_offset: |
Johannes Thumshirn | 2db4215 | 2021-03-10 18:48:06 +0900 | [diff] [blame] | 501 | spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 502 | |
| 503 | return good_bytes; |
| 504 | } |
| 505 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 506 | /** |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 507 | * sd_zbc_complete - ZBC command post processing. |
| 508 | * @cmd: Completed command |
| 509 | * @good_bytes: Command reply bytes |
| 510 | * @sshdr: command sense header |
| 511 | * |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 512 | * Called from sd_done() to handle zone commands errors and updates to the |
| 513 | * device queue zone write pointer offset cahce. |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 514 | */ |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 515 | unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes, |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 516 | struct scsi_sense_hdr *sshdr) |
| 517 | { |
| 518 | int result = cmd->result; |
Bart Van Assche | 5999ccf | 2021-08-09 16:03:06 -0700 | [diff] [blame] | 519 | struct request *rq = scsi_cmd_to_rq(cmd); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 520 | |
Ajay Joshi | ad512f2 | 2019-10-27 23:05:47 +0900 | [diff] [blame] | 521 | if (op_is_zone_mgmt(req_op(rq)) && |
Damien Le Moal | edc1f54 | 2019-10-27 23:05:44 +0900 | [diff] [blame] | 522 | result && |
| 523 | sshdr->sense_key == ILLEGAL_REQUEST && |
| 524 | sshdr->asc == 0x24) { |
| 525 | /* |
Ajay Joshi | ad512f2 | 2019-10-27 23:05:47 +0900 | [diff] [blame] | 526 | * INVALID FIELD IN CDB error: a zone management command was |
| 527 | * attempted on a conventional zone. Nothing to worry about, |
| 528 | * so be quiet about the error. |
Damien Le Moal | edc1f54 | 2019-10-27 23:05:44 +0900 | [diff] [blame] | 529 | */ |
| 530 | rq->rq_flags |= RQF_QUIET; |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 531 | } else if (sd_zbc_need_zone_wp_update(rq)) |
| 532 | good_bytes = sd_zbc_zone_wp_update(cmd, good_bytes); |
| 533 | |
| 534 | if (req_op(rq) == REQ_OP_ZONE_APPEND) |
| 535 | blk_req_zone_write_unlock(rq); |
| 536 | |
| 537 | return good_bytes; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /** |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 541 | * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 542 | * @sdkp: Target disk |
| 543 | * @buf: Buffer where to store the VPD page data |
| 544 | * |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 545 | * Read VPD page B6, get information and check that reads are unconstrained. |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 546 | */ |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 547 | static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp, |
| 548 | unsigned char *buf) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 549 | { |
| 550 | |
| 551 | if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) { |
| 552 | sd_printk(KERN_NOTICE, sdkp, |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 553 | "Read zoned characteristics VPD page failed\n"); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 554 | return -ENODEV; |
| 555 | } |
| 556 | |
| 557 | if (sdkp->device->type != TYPE_ZBC) { |
| 558 | /* Host-aware */ |
| 559 | sdkp->urswrz = 1; |
Damien Le Moal | 4a10903 | 2017-10-11 05:54:25 +0900 | [diff] [blame] | 560 | sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]); |
| 561 | sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 562 | sdkp->zones_max_open = 0; |
| 563 | } else { |
| 564 | /* Host-managed */ |
| 565 | sdkp->urswrz = buf[4] & 1; |
| 566 | sdkp->zones_optimal_open = 0; |
| 567 | sdkp->zones_optimal_nonseq = 0; |
Damien Le Moal | 4a10903 | 2017-10-11 05:54:25 +0900 | [diff] [blame] | 568 | sdkp->zones_max_open = get_unaligned_be32(&buf[16]); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 569 | } |
| 570 | |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 571 | /* |
| 572 | * Check for unconstrained reads: host-managed devices with |
| 573 | * constrained reads (drives failing read after write pointer) |
| 574 | * are not supported. |
| 575 | */ |
| 576 | if (!sdkp->urswrz) { |
| 577 | if (sdkp->first_scan) |
| 578 | sd_printk(KERN_NOTICE, sdkp, |
| 579 | "constrained reads devices are not supported\n"); |
| 580 | return -ENODEV; |
| 581 | } |
| 582 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 583 | return 0; |
| 584 | } |
| 585 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 586 | /** |
Damien Le Moal | dbfc562 | 2019-12-20 16:58:23 +0900 | [diff] [blame] | 587 | * sd_zbc_check_capacity - Check the device capacity |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 588 | * @sdkp: Target disk |
Damien Le Moal | dbfc562 | 2019-12-20 16:58:23 +0900 | [diff] [blame] | 589 | * @buf: command buffer |
Damien Le Moal | 8df513d | 2020-07-06 21:33:55 +0900 | [diff] [blame] | 590 | * @zblocks: zone size in number of blocks |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 591 | * |
Damien Le Moal | dbfc562 | 2019-12-20 16:58:23 +0900 | [diff] [blame] | 592 | * Get the device zone size and check that the device capacity as reported |
| 593 | * by READ CAPACITY matches the max_lba value (plus one) of the report zones |
| 594 | * command reply for devices with RC_BASIS == 0. |
Bart Van Assche | ccce20f | 2018-04-16 18:04:41 -0700 | [diff] [blame] | 595 | * |
Damien Le Moal | dbfc562 | 2019-12-20 16:58:23 +0900 | [diff] [blame] | 596 | * Returns 0 upon success or an error code upon failure. |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 597 | */ |
Damien Le Moal | dbfc562 | 2019-12-20 16:58:23 +0900 | [diff] [blame] | 598 | static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf, |
| 599 | u32 *zblocks) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 600 | { |
Damien Le Moal | dbfc562 | 2019-12-20 16:58:23 +0900 | [diff] [blame] | 601 | u64 zone_blocks; |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 602 | sector_t max_lba; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 603 | unsigned char *rec; |
Damien Le Moal | 5f832a3 | 2018-10-12 19:08:42 +0900 | [diff] [blame] | 604 | int ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 605 | |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 606 | /* Do a report zone to get max_lba and the size of the first zone */ |
| 607 | ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false); |
Damien Le Moal | 4b43392 | 2018-03-02 07:19:28 +0900 | [diff] [blame] | 608 | if (ret) |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 609 | return ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 610 | |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 611 | if (sdkp->rc_basis == 0) { |
| 612 | /* The max_lba field is the capacity of this device */ |
| 613 | max_lba = get_unaligned_be64(&buf[8]); |
| 614 | if (sdkp->capacity != max_lba + 1) { |
| 615 | if (sdkp->first_scan) |
| 616 | sd_printk(KERN_WARNING, sdkp, |
| 617 | "Changing capacity from %llu to max LBA+1 %llu\n", |
| 618 | (unsigned long long)sdkp->capacity, |
| 619 | (unsigned long long)max_lba + 1); |
| 620 | sdkp->capacity = max_lba + 1; |
| 621 | } |
| 622 | } |
| 623 | |
Damien Le Moal | dbfc562 | 2019-12-20 16:58:23 +0900 | [diff] [blame] | 624 | /* Get the size of the first reported zone */ |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 625 | rec = buf + 64; |
| 626 | zone_blocks = get_unaligned_be64(&rec[8]); |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 627 | if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) { |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 628 | if (sdkp->first_scan) |
| 629 | sd_printk(KERN_NOTICE, sdkp, |
| 630 | "Zone size too large\n"); |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 631 | return -EFBIG; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 632 | } |
| 633 | |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 634 | *zblocks = zone_blocks; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 635 | |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 636 | return 0; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 637 | } |
| 638 | |
Damien Le Moal | a3d8a257 | 2020-07-31 14:49:28 +0900 | [diff] [blame] | 639 | static void sd_zbc_print_zones(struct scsi_disk *sdkp) |
| 640 | { |
| 641 | if (!sd_is_zoned(sdkp) || !sdkp->capacity) |
| 642 | return; |
| 643 | |
| 644 | if (sdkp->capacity & (sdkp->zone_blocks - 1)) |
| 645 | sd_printk(KERN_NOTICE, sdkp, |
| 646 | "%u zones of %u logical blocks + 1 runt zone\n", |
| 647 | sdkp->nr_zones - 1, |
| 648 | sdkp->zone_blocks); |
| 649 | else |
| 650 | sd_printk(KERN_NOTICE, sdkp, |
| 651 | "%u zones of %u logical blocks\n", |
| 652 | sdkp->nr_zones, |
| 653 | sdkp->zone_blocks); |
| 654 | } |
| 655 | |
Damien Le Moal | 6c5dee1 | 2020-09-15 16:33:47 +0900 | [diff] [blame] | 656 | static int sd_zbc_init_disk(struct scsi_disk *sdkp) |
| 657 | { |
| 658 | sdkp->zones_wp_offset = NULL; |
| 659 | spin_lock_init(&sdkp->zones_wp_offset_lock); |
| 660 | sdkp->rev_wp_offset = NULL; |
| 661 | mutex_init(&sdkp->rev_mutex); |
| 662 | INIT_WORK(&sdkp->zone_wp_offset_work, sd_zbc_update_wp_offset_workfn); |
| 663 | sdkp->zone_wp_update_buf = kzalloc(SD_BUF_SIZE, GFP_KERNEL); |
| 664 | if (!sdkp->zone_wp_update_buf) |
| 665 | return -ENOMEM; |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
Damien Le Moal | 78e1663 | 2021-01-28 13:47:33 +0900 | [diff] [blame] | 670 | static void sd_zbc_clear_zone_info(struct scsi_disk *sdkp) |
Damien Le Moal | 6c5dee1 | 2020-09-15 16:33:47 +0900 | [diff] [blame] | 671 | { |
Damien Le Moal | 78e1663 | 2021-01-28 13:47:33 +0900 | [diff] [blame] | 672 | /* Serialize against revalidate zones */ |
| 673 | mutex_lock(&sdkp->rev_mutex); |
| 674 | |
Damien Le Moal | 6c5dee1 | 2020-09-15 16:33:47 +0900 | [diff] [blame] | 675 | kvfree(sdkp->zones_wp_offset); |
| 676 | sdkp->zones_wp_offset = NULL; |
| 677 | kfree(sdkp->zone_wp_update_buf); |
| 678 | sdkp->zone_wp_update_buf = NULL; |
Damien Le Moal | 78e1663 | 2021-01-28 13:47:33 +0900 | [diff] [blame] | 679 | |
| 680 | sdkp->nr_zones = 0; |
| 681 | sdkp->rev_nr_zones = 0; |
| 682 | sdkp->zone_blocks = 0; |
| 683 | sdkp->rev_zone_blocks = 0; |
| 684 | |
| 685 | mutex_unlock(&sdkp->rev_mutex); |
| 686 | } |
| 687 | |
| 688 | void sd_zbc_release_disk(struct scsi_disk *sdkp) |
| 689 | { |
| 690 | if (sd_is_zoned(sdkp)) |
| 691 | sd_zbc_clear_zone_info(sdkp); |
Damien Le Moal | 6c5dee1 | 2020-09-15 16:33:47 +0900 | [diff] [blame] | 692 | } |
| 693 | |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 694 | static void sd_zbc_revalidate_zones_cb(struct gendisk *disk) |
| 695 | { |
| 696 | struct scsi_disk *sdkp = scsi_disk(disk); |
| 697 | |
| 698 | swap(sdkp->zones_wp_offset, sdkp->rev_wp_offset); |
| 699 | } |
| 700 | |
Damien Le Moal | a3d8a257 | 2020-07-31 14:49:28 +0900 | [diff] [blame] | 701 | int sd_zbc_revalidate_zones(struct scsi_disk *sdkp) |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 702 | { |
| 703 | struct gendisk *disk = sdkp->disk; |
Damien Le Moal | a3d8a257 | 2020-07-31 14:49:28 +0900 | [diff] [blame] | 704 | struct request_queue *q = disk->queue; |
| 705 | u32 zone_blocks = sdkp->rev_zone_blocks; |
| 706 | unsigned int nr_zones = sdkp->rev_nr_zones; |
| 707 | u32 max_append; |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 708 | int ret = 0; |
Johannes Thumshirn | 9acced3 | 2021-02-17 22:52:45 +0900 | [diff] [blame] | 709 | unsigned int flags; |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 710 | |
Damien Le Moal | 27ba3e8 | 2020-09-15 16:33:46 +0900 | [diff] [blame] | 711 | /* |
Damien Le Moal | 6c5dee1 | 2020-09-15 16:33:47 +0900 | [diff] [blame] | 712 | * For all zoned disks, initialize zone append emulation data if not |
| 713 | * already done. This is necessary also for host-aware disks used as |
| 714 | * regular disks due to the presence of partitions as these partitions |
| 715 | * may be deleted and the disk zoned model changed back from |
| 716 | * BLK_ZONED_NONE to BLK_ZONED_HA. |
| 717 | */ |
| 718 | if (sd_is_zoned(sdkp) && !sdkp->zone_wp_update_buf) { |
| 719 | ret = sd_zbc_init_disk(sdkp); |
| 720 | if (ret) |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | /* |
Damien Le Moal | 27ba3e8 | 2020-09-15 16:33:46 +0900 | [diff] [blame] | 725 | * There is nothing to do for regular disks, including host-aware disks |
| 726 | * that have partitions. |
| 727 | */ |
| 728 | if (!blk_queue_is_zoned(q)) |
Damien Le Moal | a3d8a257 | 2020-07-31 14:49:28 +0900 | [diff] [blame] | 729 | return 0; |
| 730 | |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 731 | /* |
| 732 | * Make sure revalidate zones are serialized to ensure exclusive |
| 733 | * updates of the scsi disk data. |
| 734 | */ |
| 735 | mutex_lock(&sdkp->rev_mutex); |
| 736 | |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 737 | if (sdkp->zone_blocks == zone_blocks && |
| 738 | sdkp->nr_zones == nr_zones && |
| 739 | disk->queue->nr_zones == nr_zones) |
| 740 | goto unlock; |
| 741 | |
Johannes Thumshirn | 9acced3 | 2021-02-17 22:52:45 +0900 | [diff] [blame] | 742 | flags = memalloc_noio_save(); |
Damien Le Moal | a3d8a257 | 2020-07-31 14:49:28 +0900 | [diff] [blame] | 743 | sdkp->zone_blocks = zone_blocks; |
| 744 | sdkp->nr_zones = nr_zones; |
Johannes Thumshirn | 9acced3 | 2021-02-17 22:52:45 +0900 | [diff] [blame] | 745 | sdkp->rev_wp_offset = kvcalloc(nr_zones, sizeof(u32), GFP_KERNEL); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 746 | if (!sdkp->rev_wp_offset) { |
| 747 | ret = -ENOMEM; |
Johannes Thumshirn | 9acced3 | 2021-02-17 22:52:45 +0900 | [diff] [blame] | 748 | memalloc_noio_restore(flags); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 749 | goto unlock; |
| 750 | } |
| 751 | |
| 752 | ret = blk_revalidate_disk_zones(disk, sd_zbc_revalidate_zones_cb); |
| 753 | |
Johannes Thumshirn | 9acced3 | 2021-02-17 22:52:45 +0900 | [diff] [blame] | 754 | memalloc_noio_restore(flags); |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 755 | kvfree(sdkp->rev_wp_offset); |
| 756 | sdkp->rev_wp_offset = NULL; |
| 757 | |
Damien Le Moal | a3d8a257 | 2020-07-31 14:49:28 +0900 | [diff] [blame] | 758 | if (ret) { |
| 759 | sdkp->zone_blocks = 0; |
| 760 | sdkp->nr_zones = 0; |
| 761 | sdkp->capacity = 0; |
| 762 | goto unlock; |
| 763 | } |
| 764 | |
| 765 | max_append = min_t(u32, logical_to_sectors(sdkp->device, zone_blocks), |
| 766 | q->limits.max_segments << (PAGE_SHIFT - 9)); |
| 767 | max_append = min_t(u32, max_append, queue_max_hw_sectors(q)); |
| 768 | |
| 769 | blk_queue_max_zone_append_sectors(q, max_append); |
| 770 | |
| 771 | sd_zbc_print_zones(sdkp); |
| 772 | |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 773 | unlock: |
| 774 | mutex_unlock(&sdkp->rev_mutex); |
| 775 | |
| 776 | return ret; |
| 777 | } |
| 778 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 779 | int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 780 | { |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 781 | struct gendisk *disk = sdkp->disk; |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 782 | struct request_queue *q = disk->queue; |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 783 | unsigned int nr_zones; |
Damien Le Moal | 0cdc585 | 2019-07-17 10:51:49 +0900 | [diff] [blame] | 784 | u32 zone_blocks = 0; |
Bart Van Assche | f705324 | 2017-04-24 16:51:14 +0900 | [diff] [blame] | 785 | int ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 786 | |
| 787 | if (!sd_is_zoned(sdkp)) |
| 788 | /* |
| 789 | * Device managed or normal SCSI disk, |
| 790 | * no special handling required |
| 791 | */ |
| 792 | return 0; |
| 793 | |
Damien Le Moal | 78e1663 | 2021-01-28 13:47:33 +0900 | [diff] [blame] | 794 | /* READ16/WRITE16 is mandatory for ZBC disks */ |
| 795 | sdkp->device->use_16_for_rw = 1; |
| 796 | sdkp->device->use_10_for_rw = 0; |
| 797 | |
| 798 | if (!blk_queue_is_zoned(q)) { |
| 799 | /* |
| 800 | * This can happen for a host aware disk with partitions. |
| 801 | * The block device zone information was already cleared |
| 802 | * by blk_queue_set_zoned(). Only clear the scsi disk zone |
| 803 | * information and exit early. |
| 804 | */ |
| 805 | sd_zbc_clear_zone_info(sdkp); |
| 806 | return 0; |
| 807 | } |
| 808 | |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 809 | /* Check zoned block device characteristics (unconstrained reads) */ |
| 810 | ret = sd_zbc_check_zoned_characteristics(sdkp, buf); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 811 | if (ret) |
| 812 | goto err; |
| 813 | |
Damien Le Moal | dbfc562 | 2019-12-20 16:58:23 +0900 | [diff] [blame] | 814 | /* Check the device capacity reported by report zones */ |
| 815 | ret = sd_zbc_check_capacity(sdkp, buf, &zone_blocks); |
Damien Le Moal | 5f832a3 | 2018-10-12 19:08:42 +0900 | [diff] [blame] | 816 | if (ret != 0) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 817 | goto err; |
| 818 | |
| 819 | /* The drive satisfies the kernel restrictions: set it up */ |
Johannes Thumshirn | 5795eb4 | 2020-05-12 17:55:51 +0900 | [diff] [blame] | 820 | blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q); |
| 821 | blk_queue_required_elevator_features(q, ELEVATOR_F_ZBD_SEQ_WRITE); |
Niklas Cassel | e15864f | 2020-07-14 23:18:23 +0200 | [diff] [blame] | 822 | if (sdkp->zones_max_open == U32_MAX) |
| 823 | blk_queue_max_open_zones(q, 0); |
| 824 | else |
| 825 | blk_queue_max_open_zones(q, sdkp->zones_max_open); |
Niklas Cassel | 659bf82 | 2020-07-14 23:18:24 +0200 | [diff] [blame] | 826 | blk_queue_max_active_zones(q, 0); |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 827 | nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks); |
| 828 | |
Damien Le Moal | a805a4f | 2021-01-28 13:47:30 +0900 | [diff] [blame] | 829 | /* |
| 830 | * Per ZBC and ZAC specifications, writes in sequential write required |
| 831 | * zones of host-managed devices must be aligned to the device physical |
| 832 | * block size. |
| 833 | */ |
| 834 | if (blk_queue_zoned_model(q) == BLK_ZONED_HM) |
| 835 | blk_queue_zone_write_granularity(q, sdkp->physical_block_size); |
| 836 | |
Damien Le Moal | a3d8a257 | 2020-07-31 14:49:28 +0900 | [diff] [blame] | 837 | sdkp->rev_nr_zones = nr_zones; |
| 838 | sdkp->rev_zone_blocks = zone_blocks; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 839 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 840 | return 0; |
| 841 | |
| 842 | err: |
| 843 | sdkp->capacity = 0; |
| 844 | |
| 845 | return ret; |
| 846 | } |