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> |
| 12 | |
| 13 | #include <asm/unaligned.h> |
| 14 | |
| 15 | #include <scsi/scsi.h> |
| 16 | #include <scsi/scsi_cmnd.h> |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 17 | |
| 18 | #include "sd.h" |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 19 | |
| 20 | /** |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 21 | * sd_zbc_parse_report - Convert a zone descriptor to a struct blk_zone, |
| 22 | * @sdkp: The disk the report originated from |
| 23 | * @buf: Address of the report zone descriptor |
| 24 | * @zone: the destination zone structure |
| 25 | * |
| 26 | * All LBA sized values are converted to 512B sectors unit. |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 27 | */ |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 28 | static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf, |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 29 | struct blk_zone *zone) |
| 30 | { |
| 31 | struct scsi_device *sdp = sdkp->device; |
| 32 | |
| 33 | memset(zone, 0, sizeof(struct blk_zone)); |
| 34 | |
| 35 | zone->type = buf[0] & 0x0f; |
| 36 | zone->cond = (buf[1] >> 4) & 0xf; |
| 37 | if (buf[1] & 0x01) |
| 38 | zone->reset = 1; |
| 39 | if (buf[1] & 0x02) |
| 40 | zone->non_seq = 1; |
| 41 | |
| 42 | zone->len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8])); |
| 43 | zone->start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16])); |
| 44 | zone->wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24])); |
| 45 | if (zone->type != ZBC_ZONE_TYPE_CONV && |
| 46 | zone->cond == ZBC_ZONE_COND_FULL) |
| 47 | zone->wp = zone->start + zone->len; |
| 48 | } |
| 49 | |
| 50 | /** |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 51 | * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command. |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 52 | * @sdkp: The target disk |
| 53 | * @buf: Buffer to use for the reply |
| 54 | * @buflen: the buffer size |
| 55 | * @lba: Start LBA of the report |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 56 | * @partial: Do partial report |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 57 | * |
| 58 | * For internal use during device validation. |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 59 | * Using partial=true can significantly speed up execution of a report zones |
| 60 | * command because the disk does not have to count all possible report matching |
| 61 | * zones and will only report the count of zones fitting in the command reply |
| 62 | * buffer. |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 63 | */ |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 64 | static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf, |
| 65 | unsigned int buflen, sector_t lba, |
| 66 | bool partial) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 67 | { |
| 68 | struct scsi_device *sdp = sdkp->device; |
| 69 | const int timeout = sdp->request_queue->rq_timeout; |
| 70 | struct scsi_sense_hdr sshdr; |
| 71 | unsigned char cmd[16]; |
| 72 | unsigned int rep_len; |
| 73 | int result; |
| 74 | |
| 75 | memset(cmd, 0, 16); |
| 76 | cmd[0] = ZBC_IN; |
| 77 | cmd[1] = ZI_REPORT_ZONES; |
| 78 | put_unaligned_be64(lba, &cmd[2]); |
| 79 | put_unaligned_be32(buflen, &cmd[10]); |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 80 | if (partial) |
| 81 | cmd[14] = ZBC_REPORT_ZONE_PARTIAL; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 82 | memset(buf, 0, buflen); |
| 83 | |
| 84 | result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, |
| 85 | buf, buflen, &sshdr, |
| 86 | timeout, SD_MAX_RETRIES, NULL); |
| 87 | if (result) { |
| 88 | sd_printk(KERN_ERR, sdkp, |
| 89 | "REPORT ZONES lba %llu failed with %d/%d\n", |
| 90 | (unsigned long long)lba, |
| 91 | host_byte(result), driver_byte(result)); |
| 92 | return -EIO; |
| 93 | } |
| 94 | |
| 95 | rep_len = get_unaligned_be32(&buf[0]); |
| 96 | if (rep_len < 64) { |
| 97 | sd_printk(KERN_ERR, sdkp, |
| 98 | "REPORT ZONES report invalid length %u\n", |
| 99 | rep_len); |
| 100 | return -EIO; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 106 | /** |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 107 | * sd_zbc_report_zones - Disk report zones operation. |
| 108 | * @disk: The target disk |
| 109 | * @sector: Start 512B sector of the report |
| 110 | * @zones: Array of zone descriptors |
| 111 | * @nr_zones: Number of descriptors in the array |
| 112 | * @gfp_mask: Memory allocation mask |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 113 | * |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 114 | * Execute a report zones command on the target disk. |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 115 | */ |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 116 | int sd_zbc_report_zones(struct gendisk *disk, sector_t sector, |
| 117 | struct blk_zone *zones, unsigned int *nr_zones, |
| 118 | gfp_t gfp_mask) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 119 | { |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 120 | struct scsi_disk *sdkp = scsi_disk(disk); |
| 121 | unsigned int i, buflen, nrz = *nr_zones; |
| 122 | unsigned char *buf; |
| 123 | size_t offset = 0; |
| 124 | int ret = 0; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 125 | |
| 126 | if (!sd_is_zoned(sdkp)) |
| 127 | /* Not a zoned device */ |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 128 | return -EOPNOTSUPP; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 129 | |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 130 | /* |
Masato Suzuki | 515ce60 | 2019-02-14 15:01:18 +0900 | [diff] [blame] | 131 | * Get a reply buffer for the number of requested zones plus a header, |
| 132 | * without exceeding the device maximum command size. For ATA disks, |
| 133 | * buffers must be aligned to 512B. |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 134 | */ |
Masato Suzuki | 515ce60 | 2019-02-14 15:01:18 +0900 | [diff] [blame] | 135 | buflen = min(queue_max_hw_sectors(disk->queue) << 9, |
| 136 | roundup((nrz + 1) * 64, 512)); |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 137 | buf = kmalloc(buflen, gfp_mask); |
| 138 | if (!buf) |
| 139 | return -ENOMEM; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 140 | |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 141 | ret = sd_zbc_do_report_zones(sdkp, buf, buflen, |
| 142 | sectors_to_logical(sdkp->device, sector), true); |
| 143 | if (ret) |
| 144 | goto out_free_buf; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 145 | |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 146 | nrz = min(nrz, get_unaligned_be32(&buf[0]) / 64); |
| 147 | for (i = 0; i < nrz; i++) { |
| 148 | offset += 64; |
| 149 | sd_zbc_parse_report(sdkp, buf + offset, zones); |
| 150 | zones++; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 151 | } |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 152 | |
| 153 | *nr_zones = nrz; |
| 154 | |
| 155 | out_free_buf: |
| 156 | kfree(buf); |
| 157 | |
| 158 | return ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 159 | } |
| 160 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 161 | /** |
| 162 | * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors. |
| 163 | * @sdkp: The target disk |
| 164 | */ |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 165 | static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp) |
| 166 | { |
| 167 | return logical_to_sectors(sdkp->device, sdkp->zone_blocks); |
| 168 | } |
| 169 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 170 | /** |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 171 | * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command. |
| 172 | * @cmd: the command to setup |
| 173 | * |
| 174 | * Called from sd_init_command() for a REQ_OP_ZONE_RESET request. |
| 175 | */ |
Christoph Hellwig | 159b2cb | 2018-11-09 14:42:39 +0100 | [diff] [blame] | 176 | blk_status_t sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 177 | { |
| 178 | struct request *rq = cmd->request; |
| 179 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 180 | sector_t sector = blk_rq_pos(rq); |
| 181 | sector_t block = sectors_to_logical(sdkp->device, sector); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 182 | |
| 183 | if (!sd_is_zoned(sdkp)) |
| 184 | /* Not a zoned device */ |
Christoph Hellwig | 159b2cb | 2018-11-09 14:42:39 +0100 | [diff] [blame] | 185 | return BLK_STS_IOERR; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 186 | |
| 187 | if (sdkp->device->changed) |
Christoph Hellwig | 159b2cb | 2018-11-09 14:42:39 +0100 | [diff] [blame] | 188 | return BLK_STS_IOERR; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 189 | |
| 190 | if (sector & (sd_zbc_zone_sectors(sdkp) - 1)) |
| 191 | /* Unaligned request */ |
Christoph Hellwig | 159b2cb | 2018-11-09 14:42:39 +0100 | [diff] [blame] | 192 | return BLK_STS_IOERR; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 193 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 194 | cmd->cmd_len = 16; |
| 195 | memset(cmd->cmnd, 0, cmd->cmd_len); |
| 196 | cmd->cmnd[0] = ZBC_OUT; |
| 197 | cmd->cmnd[1] = ZO_RESET_WRITE_POINTER; |
| 198 | put_unaligned_be64(block, &cmd->cmnd[2]); |
| 199 | |
| 200 | rq->timeout = SD_TIMEOUT; |
| 201 | cmd->sc_data_direction = DMA_NONE; |
| 202 | cmd->transfersize = 0; |
| 203 | cmd->allowed = 0; |
| 204 | |
Christoph Hellwig | 159b2cb | 2018-11-09 14:42:39 +0100 | [diff] [blame] | 205 | return BLK_STS_OK; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 206 | } |
| 207 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 208 | /** |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 209 | * sd_zbc_complete - ZBC command post processing. |
| 210 | * @cmd: Completed command |
| 211 | * @good_bytes: Command reply bytes |
| 212 | * @sshdr: command sense header |
| 213 | * |
| 214 | * Called from sd_done(). Process report zones reply and handle reset zone |
| 215 | * and write commands errors. |
| 216 | */ |
| 217 | void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes, |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 218 | struct scsi_sense_hdr *sshdr) |
| 219 | { |
| 220 | int result = cmd->result; |
| 221 | struct request *rq = cmd->request; |
| 222 | |
| 223 | switch (req_op(rq)) { |
Damien Le Moal | 868ed5a | 2017-04-24 16:51:15 +0900 | [diff] [blame] | 224 | case REQ_OP_ZONE_RESET: |
| 225 | |
| 226 | if (result && |
| 227 | sshdr->sense_key == ILLEGAL_REQUEST && |
| 228 | sshdr->asc == 0x24) |
| 229 | /* |
| 230 | * INVALID FIELD IN CDB error: reset of a conventional |
| 231 | * zone was attempted. Nothing to worry about, so be |
| 232 | * quiet about the error. |
| 233 | */ |
| 234 | rq->rq_flags |= RQF_QUIET; |
| 235 | break; |
| 236 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 237 | case REQ_OP_WRITE: |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 238 | case REQ_OP_WRITE_ZEROES: |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 239 | case REQ_OP_WRITE_SAME: |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 240 | break; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 245 | * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 246 | * @sdkp: Target disk |
| 247 | * @buf: Buffer where to store the VPD page data |
| 248 | * |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 249 | * Read VPD page B6, get information and check that reads are unconstrained. |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 250 | */ |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 251 | static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp, |
| 252 | unsigned char *buf) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 253 | { |
| 254 | |
| 255 | if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) { |
| 256 | sd_printk(KERN_NOTICE, sdkp, |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 257 | "Read zoned characteristics VPD page failed\n"); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 258 | return -ENODEV; |
| 259 | } |
| 260 | |
| 261 | if (sdkp->device->type != TYPE_ZBC) { |
| 262 | /* Host-aware */ |
| 263 | sdkp->urswrz = 1; |
Damien Le Moal | 4a10903 | 2017-10-11 05:54:25 +0900 | [diff] [blame] | 264 | sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]); |
| 265 | sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 266 | sdkp->zones_max_open = 0; |
| 267 | } else { |
| 268 | /* Host-managed */ |
| 269 | sdkp->urswrz = buf[4] & 1; |
| 270 | sdkp->zones_optimal_open = 0; |
| 271 | sdkp->zones_optimal_nonseq = 0; |
Damien Le Moal | 4a10903 | 2017-10-11 05:54:25 +0900 | [diff] [blame] | 272 | sdkp->zones_max_open = get_unaligned_be32(&buf[16]); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 273 | } |
| 274 | |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 275 | /* |
| 276 | * Check for unconstrained reads: host-managed devices with |
| 277 | * constrained reads (drives failing read after write pointer) |
| 278 | * are not supported. |
| 279 | */ |
| 280 | if (!sdkp->urswrz) { |
| 281 | if (sdkp->first_scan) |
| 282 | sd_printk(KERN_NOTICE, sdkp, |
| 283 | "constrained reads devices are not supported\n"); |
| 284 | return -ENODEV; |
| 285 | } |
| 286 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
Damien Le Moal | e8c77ec | 2017-10-11 05:54:24 +0900 | [diff] [blame] | 290 | #define SD_ZBC_BUF_SIZE 131072U |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 291 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 292 | /** |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 293 | * sd_zbc_check_zones - Check the device capacity and zone sizes |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 294 | * @sdkp: Target disk |
| 295 | * |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 296 | * Check that the device capacity as reported by READ CAPACITY matches the |
| 297 | * max_lba value (plus one)of the report zones command reply. Also check that |
| 298 | * all zones of the device have an equal size, only allowing the last zone of |
| 299 | * the disk to have a smaller size (runt zone). The zone size must also be a |
| 300 | * power of two. |
Bart Van Assche | ccce20f | 2018-04-16 18:04:41 -0700 | [diff] [blame] | 301 | * |
Damien Le Moal | f13cff6 | 2018-07-03 15:23:58 +0900 | [diff] [blame] | 302 | * Returns the zone size in number of blocks upon success or an error code |
| 303 | * upon failure. |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 304 | */ |
Damien Le Moal | 5f832a3 | 2018-10-12 19:08:42 +0900 | [diff] [blame] | 305 | static int sd_zbc_check_zones(struct scsi_disk *sdkp, u32 *zblocks) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 306 | { |
Damien Le Moal | 4b43392 | 2018-03-02 07:19:28 +0900 | [diff] [blame] | 307 | u64 zone_blocks = 0; |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 308 | sector_t max_lba, block = 0; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 309 | unsigned char *buf; |
| 310 | unsigned char *rec; |
| 311 | unsigned int buf_len; |
| 312 | unsigned int list_length; |
Damien Le Moal | 5f832a3 | 2018-10-12 19:08:42 +0900 | [diff] [blame] | 313 | int ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 314 | u8 same; |
| 315 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 316 | /* Get a buffer */ |
| 317 | buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL); |
| 318 | if (!buf) |
| 319 | return -ENOMEM; |
| 320 | |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 321 | /* Do a report zone to get max_lba and the same field */ |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 322 | ret = sd_zbc_do_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0, false); |
Damien Le Moal | 4b43392 | 2018-03-02 07:19:28 +0900 | [diff] [blame] | 323 | if (ret) |
| 324 | goto out_free; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 325 | |
Damien Le Moal | d2e428e | 2018-10-12 19:08:41 +0900 | [diff] [blame] | 326 | if (sdkp->rc_basis == 0) { |
| 327 | /* The max_lba field is the capacity of this device */ |
| 328 | max_lba = get_unaligned_be64(&buf[8]); |
| 329 | if (sdkp->capacity != max_lba + 1) { |
| 330 | if (sdkp->first_scan) |
| 331 | sd_printk(KERN_WARNING, sdkp, |
| 332 | "Changing capacity from %llu to max LBA+1 %llu\n", |
| 333 | (unsigned long long)sdkp->capacity, |
| 334 | (unsigned long long)max_lba + 1); |
| 335 | sdkp->capacity = max_lba + 1; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Check same field: for any value other than 0, we know that all zones |
| 341 | * have the same size. |
| 342 | */ |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 343 | same = buf[4] & 0x0f; |
| 344 | if (same > 0) { |
| 345 | rec = &buf[64]; |
| 346 | zone_blocks = get_unaligned_be64(&rec[8]); |
| 347 | goto out; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Check the size of all zones: all zones must be of |
| 352 | * equal size, except the last zone which can be smaller |
| 353 | * than other zones. |
| 354 | */ |
| 355 | do { |
| 356 | |
| 357 | /* Parse REPORT ZONES header */ |
| 358 | list_length = get_unaligned_be32(&buf[0]) + 64; |
| 359 | rec = buf + 64; |
Damien Le Moal | e8c77ec | 2017-10-11 05:54:24 +0900 | [diff] [blame] | 360 | buf_len = min(list_length, SD_ZBC_BUF_SIZE); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 361 | |
| 362 | /* Parse zone descriptors */ |
| 363 | while (rec < buf + buf_len) { |
Bart Van Assche | ccce20f | 2018-04-16 18:04:41 -0700 | [diff] [blame] | 364 | u64 this_zone_blocks = get_unaligned_be64(&rec[8]); |
| 365 | |
| 366 | if (zone_blocks == 0) { |
| 367 | zone_blocks = this_zone_blocks; |
| 368 | } else if (this_zone_blocks != zone_blocks && |
| 369 | (block + this_zone_blocks < sdkp->capacity |
| 370 | || this_zone_blocks > zone_blocks)) { |
Damien Le Moal | 3aadbe2 | 2018-05-31 17:42:40 +0900 | [diff] [blame] | 371 | zone_blocks = 0; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 372 | goto out; |
| 373 | } |
Bart Van Assche | ccce20f | 2018-04-16 18:04:41 -0700 | [diff] [blame] | 374 | block += this_zone_blocks; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 375 | rec += 64; |
| 376 | } |
| 377 | |
| 378 | if (block < sdkp->capacity) { |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 379 | ret = sd_zbc_do_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, |
| 380 | block, true); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 381 | if (ret) |
Damien Le Moal | 4b43392 | 2018-03-02 07:19:28 +0900 | [diff] [blame] | 382 | goto out_free; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | } while (block < sdkp->capacity); |
| 386 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 387 | out: |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 388 | if (!zone_blocks) { |
| 389 | if (sdkp->first_scan) |
| 390 | sd_printk(KERN_NOTICE, sdkp, |
| 391 | "Devices with non constant zone " |
| 392 | "size are not supported\n"); |
Damien Le Moal | 4b43392 | 2018-03-02 07:19:28 +0900 | [diff] [blame] | 393 | ret = -ENODEV; |
| 394 | } else if (!is_power_of_2(zone_blocks)) { |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 395 | if (sdkp->first_scan) |
| 396 | sd_printk(KERN_NOTICE, sdkp, |
| 397 | "Devices with non power of 2 zone " |
| 398 | "size are not supported\n"); |
Damien Le Moal | 4b43392 | 2018-03-02 07:19:28 +0900 | [diff] [blame] | 399 | ret = -ENODEV; |
| 400 | } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) { |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 401 | if (sdkp->first_scan) |
| 402 | sd_printk(KERN_NOTICE, sdkp, |
| 403 | "Zone size too large\n"); |
Damien Le Moal | 5f832a3 | 2018-10-12 19:08:42 +0900 | [diff] [blame] | 404 | ret = -EFBIG; |
Damien Le Moal | 4b43392 | 2018-03-02 07:19:28 +0900 | [diff] [blame] | 405 | } else { |
Damien Le Moal | 5f832a3 | 2018-10-12 19:08:42 +0900 | [diff] [blame] | 406 | *zblocks = zone_blocks; |
| 407 | ret = 0; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 408 | } |
| 409 | |
Damien Le Moal | 4b43392 | 2018-03-02 07:19:28 +0900 | [diff] [blame] | 410 | out_free: |
| 411 | kfree(buf); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 412 | |
Damien Le Moal | 4b43392 | 2018-03-02 07:19:28 +0900 | [diff] [blame] | 413 | return ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 414 | } |
| 415 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 416 | int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 417 | { |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 418 | struct gendisk *disk = sdkp->disk; |
| 419 | unsigned int nr_zones; |
Damien Le Moal | 5f832a3 | 2018-10-12 19:08:42 +0900 | [diff] [blame] | 420 | u32 zone_blocks; |
Bart Van Assche | f705324 | 2017-04-24 16:51:14 +0900 | [diff] [blame] | 421 | int ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 422 | |
| 423 | if (!sd_is_zoned(sdkp)) |
| 424 | /* |
| 425 | * Device managed or normal SCSI disk, |
| 426 | * no special handling required |
| 427 | */ |
| 428 | return 0; |
| 429 | |
Damien Le Moal | 7f9d35d | 2018-10-12 19:08:40 +0900 | [diff] [blame] | 430 | /* Check zoned block device characteristics (unconstrained reads) */ |
| 431 | ret = sd_zbc_check_zoned_characteristics(sdkp, buf); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 432 | if (ret) |
| 433 | goto err; |
| 434 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 435 | /* |
| 436 | * Check zone size: only devices with a constant zone size (except |
| 437 | * an eventual last runt zone) that is a power of 2 are supported. |
| 438 | */ |
Damien Le Moal | 5f832a3 | 2018-10-12 19:08:42 +0900 | [diff] [blame] | 439 | ret = sd_zbc_check_zones(sdkp, &zone_blocks); |
| 440 | if (ret != 0) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 441 | goto err; |
| 442 | |
| 443 | /* The drive satisfies the kernel restrictions: set it up */ |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 444 | blk_queue_chunk_sectors(sdkp->disk->queue, |
| 445 | logical_to_sectors(sdkp->device, zone_blocks)); |
| 446 | nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks); |
| 447 | |
| 448 | /* READ16/WRITE16 is mandatory for ZBC disks */ |
| 449 | sdkp->device->use_16_for_rw = 1; |
| 450 | sdkp->device->use_10_for_rw = 0; |
| 451 | |
| 452 | /* |
Damien Le Moal | 88fc41c | 2019-01-30 15:54:58 +0900 | [diff] [blame] | 453 | * Revalidate the disk zone bitmaps once the block device capacity is |
| 454 | * set on the second revalidate execution during disk scan and if |
| 455 | * something changed when executing a normal revalidate. |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 456 | */ |
Damien Le Moal | 88fc41c | 2019-01-30 15:54:58 +0900 | [diff] [blame] | 457 | if (sdkp->first_scan) { |
| 458 | sdkp->zone_blocks = zone_blocks; |
| 459 | sdkp->nr_zones = nr_zones; |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 460 | return 0; |
Damien Le Moal | 88fc41c | 2019-01-30 15:54:58 +0900 | [diff] [blame] | 461 | } |
| 462 | |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 463 | if (sdkp->zone_blocks != zone_blocks || |
| 464 | sdkp->nr_zones != nr_zones || |
| 465 | disk->queue->nr_zones != nr_zones) { |
| 466 | ret = blk_revalidate_disk_zones(disk); |
| 467 | if (ret != 0) |
| 468 | goto err; |
| 469 | sdkp->zone_blocks = zone_blocks; |
| 470 | sdkp->nr_zones = nr_zones; |
| 471 | } |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 472 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 473 | return 0; |
| 474 | |
| 475 | err: |
| 476 | sdkp->capacity = 0; |
| 477 | |
| 478 | return ret; |
| 479 | } |
| 480 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 481 | void sd_zbc_print_zones(struct scsi_disk *sdkp) |
| 482 | { |
| 483 | if (!sd_is_zoned(sdkp) || !sdkp->capacity) |
| 484 | return; |
| 485 | |
| 486 | if (sdkp->capacity & (sdkp->zone_blocks - 1)) |
| 487 | sd_printk(KERN_NOTICE, sdkp, |
| 488 | "%u zones of %u logical blocks + 1 runt zone\n", |
| 489 | sdkp->nr_zones - 1, |
| 490 | sdkp->zone_blocks); |
| 491 | else |
| 492 | sd_printk(KERN_NOTICE, sdkp, |
| 493 | "%u zones of %u logical blocks\n", |
| 494 | sdkp->nr_zones, |
| 495 | sdkp->zone_blocks); |
| 496 | } |