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 | /** |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 65 | * sd_zbc_report_zones - Issue a REPORT ZONES scsi command. |
| 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 |
| 70 | * |
| 71 | * For internal use during device validation. |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 72 | */ |
| 73 | static int sd_zbc_report_zones(struct scsi_disk *sdkp, unsigned char *buf, |
| 74 | unsigned int buflen, sector_t lba) |
| 75 | { |
| 76 | struct scsi_device *sdp = sdkp->device; |
| 77 | const int timeout = sdp->request_queue->rq_timeout; |
| 78 | struct scsi_sense_hdr sshdr; |
| 79 | unsigned char cmd[16]; |
| 80 | unsigned int rep_len; |
| 81 | int result; |
| 82 | |
| 83 | memset(cmd, 0, 16); |
| 84 | cmd[0] = ZBC_IN; |
| 85 | cmd[1] = ZI_REPORT_ZONES; |
| 86 | put_unaligned_be64(lba, &cmd[2]); |
| 87 | put_unaligned_be32(buflen, &cmd[10]); |
| 88 | memset(buf, 0, buflen); |
| 89 | |
| 90 | result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, |
| 91 | buf, buflen, &sshdr, |
| 92 | timeout, SD_MAX_RETRIES, NULL); |
| 93 | if (result) { |
| 94 | sd_printk(KERN_ERR, sdkp, |
| 95 | "REPORT ZONES lba %llu failed with %d/%d\n", |
| 96 | (unsigned long long)lba, |
| 97 | host_byte(result), driver_byte(result)); |
| 98 | return -EIO; |
| 99 | } |
| 100 | |
| 101 | rep_len = get_unaligned_be32(&buf[0]); |
| 102 | if (rep_len < 64) { |
| 103 | sd_printk(KERN_ERR, sdkp, |
| 104 | "REPORT ZONES report invalid length %u\n", |
| 105 | rep_len); |
| 106 | return -EIO; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 112 | /** |
| 113 | * sd_zbc_setup_report_cmnd - Prepare a REPORT ZONES scsi command |
| 114 | * @cmd: The command to setup |
| 115 | * |
| 116 | * Call in sd_init_command() for a REQ_OP_ZONE_REPORT request. |
| 117 | */ |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 118 | int sd_zbc_setup_report_cmnd(struct scsi_cmnd *cmd) |
| 119 | { |
| 120 | struct request *rq = cmd->request; |
| 121 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 122 | sector_t lba, sector = blk_rq_pos(rq); |
| 123 | unsigned int nr_bytes = blk_rq_bytes(rq); |
| 124 | int ret; |
| 125 | |
| 126 | WARN_ON(nr_bytes == 0); |
| 127 | |
| 128 | if (!sd_is_zoned(sdkp)) |
| 129 | /* Not a zoned device */ |
| 130 | return BLKPREP_KILL; |
| 131 | |
| 132 | ret = scsi_init_io(cmd); |
| 133 | if (ret != BLKPREP_OK) |
| 134 | return ret; |
| 135 | |
| 136 | cmd->cmd_len = 16; |
| 137 | memset(cmd->cmnd, 0, cmd->cmd_len); |
| 138 | cmd->cmnd[0] = ZBC_IN; |
| 139 | cmd->cmnd[1] = ZI_REPORT_ZONES; |
| 140 | lba = sectors_to_logical(sdkp->device, sector); |
| 141 | put_unaligned_be64(lba, &cmd->cmnd[2]); |
| 142 | put_unaligned_be32(nr_bytes, &cmd->cmnd[10]); |
| 143 | /* Do partial report for speeding things up */ |
| 144 | cmd->cmnd[14] = ZBC_REPORT_ZONE_PARTIAL; |
| 145 | |
| 146 | cmd->sc_data_direction = DMA_FROM_DEVICE; |
| 147 | cmd->sdb.length = nr_bytes; |
| 148 | cmd->transfersize = sdkp->device->sector_size; |
| 149 | cmd->allowed = 0; |
| 150 | |
| 151 | /* |
| 152 | * Report may return less bytes than requested. Make sure |
| 153 | * to report completion on the entire initial request. |
| 154 | */ |
| 155 | rq->__data_len = nr_bytes; |
| 156 | |
| 157 | return BLKPREP_OK; |
| 158 | } |
| 159 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 160 | /** |
| 161 | * sd_zbc_report_zones_complete - Process a REPORT ZONES scsi command reply. |
| 162 | * @scmd: The completed report zones command |
| 163 | * @good_bytes: reply size in bytes |
| 164 | * |
| 165 | * Convert all reported zone descriptors to struct blk_zone. The conversion |
| 166 | * is done in-place, directly in the request specified sg buffer. |
| 167 | */ |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 168 | static void sd_zbc_report_zones_complete(struct scsi_cmnd *scmd, |
| 169 | unsigned int good_bytes) |
| 170 | { |
| 171 | struct request *rq = scmd->request; |
| 172 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 173 | struct sg_mapping_iter miter; |
| 174 | struct blk_zone_report_hdr hdr; |
| 175 | struct blk_zone zone; |
| 176 | unsigned int offset, bytes = 0; |
| 177 | unsigned long flags; |
| 178 | u8 *buf; |
| 179 | |
| 180 | if (good_bytes < 64) |
| 181 | return; |
| 182 | |
| 183 | memset(&hdr, 0, sizeof(struct blk_zone_report_hdr)); |
| 184 | |
| 185 | sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd), |
| 186 | SG_MITER_TO_SG | SG_MITER_ATOMIC); |
| 187 | |
| 188 | local_irq_save(flags); |
| 189 | while (sg_miter_next(&miter) && bytes < good_bytes) { |
| 190 | |
| 191 | buf = miter.addr; |
| 192 | offset = 0; |
| 193 | |
| 194 | if (bytes == 0) { |
| 195 | /* Set the report header */ |
| 196 | hdr.nr_zones = min_t(unsigned int, |
| 197 | (good_bytes - 64) / 64, |
| 198 | get_unaligned_be32(&buf[0]) / 64); |
| 199 | memcpy(buf, &hdr, sizeof(struct blk_zone_report_hdr)); |
| 200 | offset += 64; |
| 201 | bytes += 64; |
| 202 | } |
| 203 | |
| 204 | /* Parse zone descriptors */ |
| 205 | while (offset < miter.length && hdr.nr_zones) { |
| 206 | WARN_ON(offset > miter.length); |
| 207 | buf = miter.addr + offset; |
| 208 | sd_zbc_parse_report(sdkp, buf, &zone); |
| 209 | memcpy(buf, &zone, sizeof(struct blk_zone)); |
| 210 | offset += 64; |
| 211 | bytes += 64; |
| 212 | hdr.nr_zones--; |
| 213 | } |
| 214 | |
| 215 | if (!hdr.nr_zones) |
| 216 | break; |
| 217 | |
| 218 | } |
| 219 | sg_miter_stop(&miter); |
| 220 | local_irq_restore(flags); |
| 221 | } |
| 222 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 223 | /** |
| 224 | * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors. |
| 225 | * @sdkp: The target disk |
| 226 | */ |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 227 | static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp) |
| 228 | { |
| 229 | return logical_to_sectors(sdkp->device, sdkp->zone_blocks); |
| 230 | } |
| 231 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 232 | /** |
| 233 | * sd_zbc_zone_no - Get the number of the zone conataining a sector. |
| 234 | * @sdkp: The target disk |
| 235 | * @sector: 512B sector address contained in the zone |
| 236 | */ |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 237 | static inline unsigned int sd_zbc_zone_no(struct scsi_disk *sdkp, |
| 238 | sector_t sector) |
| 239 | { |
| 240 | return sectors_to_logical(sdkp->device, sector) >> sdkp->zone_shift; |
| 241 | } |
| 242 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 243 | /** |
| 244 | * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command. |
| 245 | * @cmd: the command to setup |
| 246 | * |
| 247 | * Called from sd_init_command() for a REQ_OP_ZONE_RESET request. |
| 248 | */ |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 249 | int sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd) |
| 250 | { |
| 251 | struct request *rq = cmd->request; |
| 252 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 253 | sector_t sector = blk_rq_pos(rq); |
| 254 | sector_t block = sectors_to_logical(sdkp->device, sector); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 255 | |
| 256 | if (!sd_is_zoned(sdkp)) |
| 257 | /* Not a zoned device */ |
| 258 | return BLKPREP_KILL; |
| 259 | |
| 260 | if (sdkp->device->changed) |
| 261 | return BLKPREP_KILL; |
| 262 | |
| 263 | if (sector & (sd_zbc_zone_sectors(sdkp) - 1)) |
| 264 | /* Unaligned request */ |
| 265 | return BLKPREP_KILL; |
| 266 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 267 | cmd->cmd_len = 16; |
| 268 | memset(cmd->cmnd, 0, cmd->cmd_len); |
| 269 | cmd->cmnd[0] = ZBC_OUT; |
| 270 | cmd->cmnd[1] = ZO_RESET_WRITE_POINTER; |
| 271 | put_unaligned_be64(block, &cmd->cmnd[2]); |
| 272 | |
| 273 | rq->timeout = SD_TIMEOUT; |
| 274 | cmd->sc_data_direction = DMA_NONE; |
| 275 | cmd->transfersize = 0; |
| 276 | cmd->allowed = 0; |
| 277 | |
| 278 | return BLKPREP_OK; |
| 279 | } |
| 280 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 281 | /** |
| 282 | * sd_zbc_write_lock_zone - Write lock a sequential zone. |
| 283 | * @cmd: write command |
| 284 | * |
| 285 | * Called from sd_init_cmd() for write requests (standard write, write same or |
| 286 | * write zeroes operations). If the request target zone is not already locked, |
| 287 | * the zone is locked and BLKPREP_OK returned, allowing the request to proceed |
| 288 | * through dispatch in scsi_request_fn(). Otherwise, BLKPREP_DEFER is returned, |
| 289 | * forcing the request to wait for the zone to be unlocked, that is, for the |
| 290 | * previously issued write request targeting the same zone to complete. |
| 291 | * |
| 292 | * This is called from blk_peek_request() context with the queue lock held and |
| 293 | * before the request is removed from the scheduler. As a result, multiple |
| 294 | * contexts executing concurrently scsi_request_fn() cannot result in write |
| 295 | * sequence reordering as only a single write request per zone is allowed to |
| 296 | * proceed. |
| 297 | */ |
Damien Le Moal | a90dfdc | 2017-04-24 16:51:13 +0900 | [diff] [blame] | 298 | int sd_zbc_write_lock_zone(struct scsi_cmnd *cmd) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 299 | { |
| 300 | struct request *rq = cmd->request; |
| 301 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 302 | sector_t sector = blk_rq_pos(rq); |
| 303 | sector_t zone_sectors = sd_zbc_zone_sectors(sdkp); |
| 304 | unsigned int zno = sd_zbc_zone_no(sdkp, sector); |
| 305 | |
| 306 | /* |
| 307 | * Note: Checks of the alignment of the write command on |
| 308 | * logical blocks is done in sd.c |
| 309 | */ |
| 310 | |
| 311 | /* Do not allow zone boundaries crossing on host-managed drives */ |
| 312 | if (blk_queue_zoned_model(sdkp->disk->queue) == BLK_ZONED_HM && |
| 313 | (sector & (zone_sectors - 1)) + blk_rq_sectors(rq) > zone_sectors) |
| 314 | return BLKPREP_KILL; |
| 315 | |
| 316 | /* |
| 317 | * Do not issue more than one write at a time per |
| 318 | * zone. This solves write ordering problems due to |
| 319 | * the unlocking of the request queue in the dispatch |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 320 | * path in the non scsi-mq case. |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 321 | */ |
| 322 | if (sdkp->zones_wlock && |
| 323 | test_and_set_bit(zno, sdkp->zones_wlock)) |
| 324 | return BLKPREP_DEFER; |
| 325 | |
Damien Le Moal | 70e42fd | 2017-08-09 12:00:22 +0900 | [diff] [blame] | 326 | WARN_ON_ONCE(cmd->flags & SCMD_ZONE_WRITE_LOCK); |
| 327 | cmd->flags |= SCMD_ZONE_WRITE_LOCK; |
| 328 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 329 | return BLKPREP_OK; |
| 330 | } |
| 331 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 332 | /** |
| 333 | * sd_zbc_write_unlock_zone - Write unlock a sequential zone. |
| 334 | * @cmd: write command |
| 335 | * |
| 336 | * Called from sd_uninit_cmd(). Unlocking the request target zone will allow |
| 337 | * dispatching the next write request for the zone. |
| 338 | */ |
Damien Le Moal | a90dfdc | 2017-04-24 16:51:13 +0900 | [diff] [blame] | 339 | void sd_zbc_write_unlock_zone(struct scsi_cmnd *cmd) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 340 | { |
Damien Le Moal | a90dfdc | 2017-04-24 16:51:13 +0900 | [diff] [blame] | 341 | struct request *rq = cmd->request; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 342 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
| 343 | |
Damien Le Moal | 70e42fd | 2017-08-09 12:00:22 +0900 | [diff] [blame] | 344 | if (sdkp->zones_wlock && cmd->flags & SCMD_ZONE_WRITE_LOCK) { |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 345 | unsigned int zno = sd_zbc_zone_no(sdkp, blk_rq_pos(rq)); |
| 346 | WARN_ON_ONCE(!test_bit(zno, sdkp->zones_wlock)); |
Damien Le Moal | 70e42fd | 2017-08-09 12:00:22 +0900 | [diff] [blame] | 347 | cmd->flags &= ~SCMD_ZONE_WRITE_LOCK; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 348 | clear_bit_unlock(zno, sdkp->zones_wlock); |
| 349 | smp_mb__after_atomic(); |
| 350 | } |
| 351 | } |
| 352 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 353 | /** |
| 354 | * sd_zbc_complete - ZBC command post processing. |
| 355 | * @cmd: Completed command |
| 356 | * @good_bytes: Command reply bytes |
| 357 | * @sshdr: command sense header |
| 358 | * |
| 359 | * Called from sd_done(). Process report zones reply and handle reset zone |
| 360 | * and write commands errors. |
| 361 | */ |
| 362 | void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes, |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 363 | struct scsi_sense_hdr *sshdr) |
| 364 | { |
| 365 | int result = cmd->result; |
| 366 | struct request *rq = cmd->request; |
| 367 | |
| 368 | switch (req_op(rq)) { |
Damien Le Moal | 868ed5a | 2017-04-24 16:51:15 +0900 | [diff] [blame] | 369 | case REQ_OP_ZONE_RESET: |
| 370 | |
| 371 | if (result && |
| 372 | sshdr->sense_key == ILLEGAL_REQUEST && |
| 373 | sshdr->asc == 0x24) |
| 374 | /* |
| 375 | * INVALID FIELD IN CDB error: reset of a conventional |
| 376 | * zone was attempted. Nothing to worry about, so be |
| 377 | * quiet about the error. |
| 378 | */ |
| 379 | rq->rq_flags |= RQF_QUIET; |
| 380 | break; |
| 381 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 382 | case REQ_OP_WRITE: |
Christoph Hellwig | 02d2610 | 2017-04-05 19:21:02 +0200 | [diff] [blame] | 383 | case REQ_OP_WRITE_ZEROES: |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 384 | case REQ_OP_WRITE_SAME: |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 385 | |
Damien Le Moal | 868ed5a | 2017-04-24 16:51:15 +0900 | [diff] [blame] | 386 | if (result && |
| 387 | sshdr->sense_key == ILLEGAL_REQUEST && |
| 388 | sshdr->asc == 0x21) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 389 | /* |
| 390 | * INVALID ADDRESS FOR WRITE error: It is unlikely that |
| 391 | * retrying write requests failed with any kind of |
| 392 | * alignement error will result in success. So don't. |
| 393 | */ |
| 394 | cmd->allowed = 0; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 395 | break; |
| 396 | |
| 397 | case REQ_OP_ZONE_REPORT: |
| 398 | |
| 399 | if (!result) |
| 400 | sd_zbc_report_zones_complete(cmd, good_bytes); |
| 401 | break; |
| 402 | |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /** |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 407 | * sd_zbc_read_zoned_characteristics - Read zoned block device characteristics |
| 408 | * @sdkp: Target disk |
| 409 | * @buf: Buffer where to store the VPD page data |
| 410 | * |
| 411 | * Read VPD page B6. |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 412 | */ |
| 413 | static int sd_zbc_read_zoned_characteristics(struct scsi_disk *sdkp, |
| 414 | unsigned char *buf) |
| 415 | { |
| 416 | |
| 417 | if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) { |
| 418 | sd_printk(KERN_NOTICE, sdkp, |
| 419 | "Unconstrained-read check failed\n"); |
| 420 | return -ENODEV; |
| 421 | } |
| 422 | |
| 423 | if (sdkp->device->type != TYPE_ZBC) { |
| 424 | /* Host-aware */ |
| 425 | sdkp->urswrz = 1; |
Damien Le Moal | 4a10903 | 2017-10-11 05:54:25 +0900 | [diff] [blame] | 426 | sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]); |
| 427 | sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 428 | sdkp->zones_max_open = 0; |
| 429 | } else { |
| 430 | /* Host-managed */ |
| 431 | sdkp->urswrz = buf[4] & 1; |
| 432 | sdkp->zones_optimal_open = 0; |
| 433 | sdkp->zones_optimal_nonseq = 0; |
Damien Le Moal | 4a10903 | 2017-10-11 05:54:25 +0900 | [diff] [blame] | 434 | sdkp->zones_max_open = get_unaligned_be32(&buf[16]); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | /** |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 441 | * sd_zbc_check_capacity - Check reported capacity. |
| 442 | * @sdkp: Target disk |
| 443 | * @buf: Buffer to use for commands |
| 444 | * |
| 445 | * ZBC drive may report only the capacity of the first conventional zones at |
| 446 | * LBA 0. This is indicated by the RC_BASIS field of the read capacity reply. |
| 447 | * Check this here. If the disk reported only its conventional zones capacity, |
| 448 | * get the total capacity by doing a report zones. |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 449 | */ |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 450 | static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 451 | { |
| 452 | sector_t lba; |
| 453 | int ret; |
| 454 | |
| 455 | if (sdkp->rc_basis != 0) |
| 456 | return 0; |
| 457 | |
| 458 | /* Do a report zone to get the maximum LBA to check capacity */ |
| 459 | ret = sd_zbc_report_zones(sdkp, buf, SD_BUF_SIZE, 0); |
| 460 | if (ret) |
| 461 | return ret; |
| 462 | |
| 463 | /* The max_lba field is the capacity of this device */ |
| 464 | lba = get_unaligned_be64(&buf[8]); |
| 465 | if (lba + 1 == sdkp->capacity) |
| 466 | return 0; |
| 467 | |
| 468 | if (sdkp->first_scan) |
| 469 | sd_printk(KERN_WARNING, sdkp, |
| 470 | "Changing capacity from %llu to max LBA+1 %llu\n", |
| 471 | (unsigned long long)sdkp->capacity, |
| 472 | (unsigned long long)lba + 1); |
| 473 | sdkp->capacity = lba + 1; |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Damien Le Moal | e8c77ec | 2017-10-11 05:54:24 +0900 | [diff] [blame] | 478 | #define SD_ZBC_BUF_SIZE 131072U |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 479 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 480 | /** |
| 481 | * sd_zbc_check_zone_size - Check the device zone sizes |
| 482 | * @sdkp: Target disk |
| 483 | * |
| 484 | * Check that all zones of the device are equal. The last zone can however |
| 485 | * be smaller. The zone size must also be a power of two number of LBAs. |
| 486 | */ |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 487 | static int sd_zbc_check_zone_size(struct scsi_disk *sdkp) |
| 488 | { |
| 489 | u64 zone_blocks; |
| 490 | sector_t block = 0; |
| 491 | unsigned char *buf; |
| 492 | unsigned char *rec; |
| 493 | unsigned int buf_len; |
| 494 | unsigned int list_length; |
| 495 | int ret; |
| 496 | u8 same; |
| 497 | |
| 498 | sdkp->zone_blocks = 0; |
| 499 | |
| 500 | /* Get a buffer */ |
| 501 | buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL); |
| 502 | if (!buf) |
| 503 | return -ENOMEM; |
| 504 | |
| 505 | /* Do a report zone to get the same field */ |
| 506 | ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0); |
Arnd Bergmann | 5f2808f | 2016-10-21 17:32:24 +0200 | [diff] [blame] | 507 | if (ret) { |
| 508 | zone_blocks = 0; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 509 | goto out; |
Arnd Bergmann | 5f2808f | 2016-10-21 17:32:24 +0200 | [diff] [blame] | 510 | } |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 511 | |
| 512 | same = buf[4] & 0x0f; |
| 513 | if (same > 0) { |
| 514 | rec = &buf[64]; |
| 515 | zone_blocks = get_unaligned_be64(&rec[8]); |
| 516 | goto out; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Check the size of all zones: all zones must be of |
| 521 | * equal size, except the last zone which can be smaller |
| 522 | * than other zones. |
| 523 | */ |
| 524 | do { |
| 525 | |
| 526 | /* Parse REPORT ZONES header */ |
| 527 | list_length = get_unaligned_be32(&buf[0]) + 64; |
| 528 | rec = buf + 64; |
Damien Le Moal | e8c77ec | 2017-10-11 05:54:24 +0900 | [diff] [blame] | 529 | buf_len = min(list_length, SD_ZBC_BUF_SIZE); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 530 | |
| 531 | /* Parse zone descriptors */ |
| 532 | while (rec < buf + buf_len) { |
| 533 | zone_blocks = get_unaligned_be64(&rec[8]); |
| 534 | if (sdkp->zone_blocks == 0) { |
| 535 | sdkp->zone_blocks = zone_blocks; |
| 536 | } else if (zone_blocks != sdkp->zone_blocks && |
| 537 | (block + zone_blocks < sdkp->capacity |
| 538 | || zone_blocks > sdkp->zone_blocks)) { |
| 539 | zone_blocks = 0; |
| 540 | goto out; |
| 541 | } |
| 542 | block += zone_blocks; |
| 543 | rec += 64; |
| 544 | } |
| 545 | |
| 546 | if (block < sdkp->capacity) { |
| 547 | ret = sd_zbc_report_zones(sdkp, buf, |
| 548 | SD_ZBC_BUF_SIZE, block); |
| 549 | if (ret) |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | } while (block < sdkp->capacity); |
| 554 | |
| 555 | zone_blocks = sdkp->zone_blocks; |
| 556 | |
| 557 | out: |
| 558 | kfree(buf); |
| 559 | |
| 560 | if (!zone_blocks) { |
| 561 | if (sdkp->first_scan) |
| 562 | sd_printk(KERN_NOTICE, sdkp, |
| 563 | "Devices with non constant zone " |
| 564 | "size are not supported\n"); |
| 565 | return -ENODEV; |
| 566 | } |
| 567 | |
| 568 | if (!is_power_of_2(zone_blocks)) { |
| 569 | if (sdkp->first_scan) |
| 570 | sd_printk(KERN_NOTICE, sdkp, |
| 571 | "Devices with non power of 2 zone " |
| 572 | "size are not supported\n"); |
| 573 | return -ENODEV; |
| 574 | } |
| 575 | |
| 576 | if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) { |
| 577 | if (sdkp->first_scan) |
| 578 | sd_printk(KERN_NOTICE, sdkp, |
| 579 | "Zone size too large\n"); |
| 580 | return -ENODEV; |
| 581 | } |
| 582 | |
| 583 | sdkp->zone_blocks = zone_blocks; |
Damien Le Moal | 5eed92d | 2017-10-11 05:54:23 +0900 | [diff] [blame] | 584 | sdkp->zone_shift = ilog2(zone_blocks); |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static int sd_zbc_setup(struct scsi_disk *sdkp) |
| 590 | { |
| 591 | |
Damien Le Moal | 5eed92d | 2017-10-11 05:54:23 +0900 | [diff] [blame] | 592 | /* READ16/WRITE16 is mandatory for ZBC disks */ |
| 593 | sdkp->device->use_16_for_rw = 1; |
| 594 | sdkp->device->use_10_for_rw = 0; |
| 595 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 596 | /* chunk_sectors indicates the zone size */ |
| 597 | blk_queue_chunk_sectors(sdkp->disk->queue, |
| 598 | logical_to_sectors(sdkp->device, sdkp->zone_blocks)); |
Damien Le Moal | e8c77ec | 2017-10-11 05:54:24 +0900 | [diff] [blame] | 599 | sdkp->nr_zones = |
| 600 | round_up(sdkp->capacity, sdkp->zone_blocks) >> sdkp->zone_shift; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 601 | |
| 602 | if (!sdkp->zones_wlock) { |
| 603 | sdkp->zones_wlock = kcalloc(BITS_TO_LONGS(sdkp->nr_zones), |
| 604 | sizeof(unsigned long), |
| 605 | GFP_KERNEL); |
| 606 | if (!sdkp->zones_wlock) |
| 607 | return -ENOMEM; |
| 608 | } |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
Damien Le Moal | e98f42b | 2017-10-11 05:54:22 +0900 | [diff] [blame] | 613 | int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf) |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 614 | { |
Bart Van Assche | f705324 | 2017-04-24 16:51:14 +0900 | [diff] [blame] | 615 | int ret; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 616 | |
| 617 | if (!sd_is_zoned(sdkp)) |
| 618 | /* |
| 619 | * Device managed or normal SCSI disk, |
| 620 | * no special handling required |
| 621 | */ |
| 622 | return 0; |
| 623 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 624 | /* Get zoned block device characteristics */ |
| 625 | ret = sd_zbc_read_zoned_characteristics(sdkp, buf); |
| 626 | if (ret) |
| 627 | goto err; |
| 628 | |
| 629 | /* |
| 630 | * Check for unconstrained reads: host-managed devices with |
| 631 | * constrained reads (drives failing read after write pointer) |
| 632 | * are not supported. |
| 633 | */ |
| 634 | if (!sdkp->urswrz) { |
| 635 | if (sdkp->first_scan) |
| 636 | sd_printk(KERN_NOTICE, sdkp, |
| 637 | "constrained reads devices are not supported\n"); |
| 638 | ret = -ENODEV; |
| 639 | goto err; |
| 640 | } |
| 641 | |
| 642 | /* Check capacity */ |
| 643 | ret = sd_zbc_check_capacity(sdkp, buf); |
| 644 | if (ret) |
| 645 | goto err; |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 646 | |
| 647 | /* |
| 648 | * Check zone size: only devices with a constant zone size (except |
| 649 | * an eventual last runt zone) that is a power of 2 are supported. |
| 650 | */ |
| 651 | ret = sd_zbc_check_zone_size(sdkp); |
| 652 | if (ret) |
| 653 | goto err; |
| 654 | |
| 655 | /* The drive satisfies the kernel restrictions: set it up */ |
| 656 | ret = sd_zbc_setup(sdkp); |
| 657 | if (ret) |
| 658 | goto err; |
| 659 | |
Hannes Reinecke | 89d9475 | 2016-10-18 15:40:34 +0900 | [diff] [blame] | 660 | return 0; |
| 661 | |
| 662 | err: |
| 663 | sdkp->capacity = 0; |
| 664 | |
| 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | void sd_zbc_remove(struct scsi_disk *sdkp) |
| 669 | { |
| 670 | kfree(sdkp->zones_wlock); |
| 671 | sdkp->zones_wlock = NULL; |
| 672 | } |
| 673 | |
| 674 | void sd_zbc_print_zones(struct scsi_disk *sdkp) |
| 675 | { |
| 676 | if (!sd_is_zoned(sdkp) || !sdkp->capacity) |
| 677 | return; |
| 678 | |
| 679 | if (sdkp->capacity & (sdkp->zone_blocks - 1)) |
| 680 | sd_printk(KERN_NOTICE, sdkp, |
| 681 | "%u zones of %u logical blocks + 1 runt zone\n", |
| 682 | sdkp->nr_zones - 1, |
| 683 | sdkp->zone_blocks); |
| 684 | else |
| 685 | sd_printk(KERN_NOTICE, sdkp, |
| 686 | "%u zones of %u logical blocks\n", |
| 687 | sdkp->nr_zones, |
| 688 | sdkp->zone_blocks); |
| 689 | } |