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