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