]>
Commit | Line | Data |
---|---|---|
a98c5b19 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
89d94756 HR |
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> | |
89d94756 HR |
9 | */ |
10 | ||
11 | #include <linux/blkdev.h> | |
b091ac61 DLM |
12 | #include <linux/vmalloc.h> |
13 | #include <linux/sched/mm.h> | |
89d94756 HR |
14 | |
15 | #include <asm/unaligned.h> | |
16 | ||
17 | #include <scsi/scsi.h> | |
18 | #include <scsi/scsi_cmnd.h> | |
89d94756 HR |
19 | |
20 | #include "sd.h" | |
89d94756 | 21 | |
d4100351 CH |
22 | static int sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf, |
23 | unsigned int idx, report_zones_cb cb, void *data) | |
89d94756 HR |
24 | { |
25 | struct scsi_device *sdp = sdkp->device; | |
d4100351 | 26 | struct blk_zone zone = { 0 }; |
89d94756 | 27 | |
d4100351 CH |
28 | zone.type = buf[0] & 0x0f; |
29 | zone.cond = (buf[1] >> 4) & 0xf; | |
89d94756 | 30 | if (buf[1] & 0x01) |
d4100351 | 31 | zone.reset = 1; |
89d94756 | 32 | if (buf[1] & 0x02) |
d4100351 CH |
33 | zone.non_seq = 1; |
34 | ||
35 | zone.len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8])); | |
36 | zone.start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16])); | |
37 | zone.wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24])); | |
38 | if (zone.type != ZBC_ZONE_TYPE_CONV && | |
39 | zone.cond == ZBC_ZONE_COND_FULL) | |
40 | zone.wp = zone.start + zone.len; | |
41 | ||
42 | return cb(&zone, idx, data); | |
89d94756 HR |
43 | } |
44 | ||
45 | /** | |
e76239a3 | 46 | * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command. |
e98f42bc | 47 | * @sdkp: The target disk |
b091ac61 | 48 | * @buf: vmalloc-ed buffer to use for the reply |
e98f42bc DLM |
49 | * @buflen: the buffer size |
50 | * @lba: Start LBA of the report | |
d2e428e4 | 51 | * @partial: Do partial report |
e98f42bc DLM |
52 | * |
53 | * For internal use during device validation. | |
d2e428e4 DLM |
54 | * Using partial=true can significantly speed up execution of a report zones |
55 | * command because the disk does not have to count all possible report matching | |
56 | * zones and will only report the count of zones fitting in the command reply | |
57 | * buffer. | |
89d94756 | 58 | */ |
e76239a3 CH |
59 | static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf, |
60 | unsigned int buflen, sector_t lba, | |
61 | bool partial) | |
89d94756 HR |
62 | { |
63 | struct scsi_device *sdp = sdkp->device; | |
64 | const int timeout = sdp->request_queue->rq_timeout; | |
65 | struct scsi_sense_hdr sshdr; | |
66 | unsigned char cmd[16]; | |
67 | unsigned int rep_len; | |
68 | int result; | |
69 | ||
70 | memset(cmd, 0, 16); | |
71 | cmd[0] = ZBC_IN; | |
72 | cmd[1] = ZI_REPORT_ZONES; | |
73 | put_unaligned_be64(lba, &cmd[2]); | |
74 | put_unaligned_be32(buflen, &cmd[10]); | |
d2e428e4 DLM |
75 | if (partial) |
76 | cmd[14] = ZBC_REPORT_ZONE_PARTIAL; | |
89d94756 HR |
77 | |
78 | result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, | |
79 | buf, buflen, &sshdr, | |
80 | timeout, SD_MAX_RETRIES, NULL); | |
81 | if (result) { | |
82 | sd_printk(KERN_ERR, sdkp, | |
a35989a0 DLM |
83 | "REPORT ZONES start lba %llu failed\n", lba); |
84 | sd_print_result(sdkp, "REPORT ZONES", result); | |
85 | if (driver_byte(result) == DRIVER_SENSE && | |
86 | scsi_sense_valid(&sshdr)) | |
87 | sd_print_sense_hdr(sdkp, &sshdr); | |
89d94756 HR |
88 | return -EIO; |
89 | } | |
90 | ||
91 | rep_len = get_unaligned_be32(&buf[0]); | |
92 | if (rep_len < 64) { | |
93 | sd_printk(KERN_ERR, sdkp, | |
94 | "REPORT ZONES report invalid length %u\n", | |
95 | rep_len); | |
96 | return -EIO; | |
97 | } | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
b091ac61 DLM |
102 | /** |
103 | * Allocate a buffer for report zones reply. | |
104 | * @sdkp: The target disk | |
105 | * @nr_zones: Maximum number of zones to report | |
106 | * @buflen: Size of the buffer allocated | |
107 | * | |
108 | * Try to allocate a reply buffer for the number of requested zones. | |
109 | * The size of the buffer allocated may be smaller than requested to | |
110 | * satify the device constraint (max_hw_sectors, max_segments, etc). | |
111 | * | |
112 | * Return the address of the allocated buffer and update @buflen with | |
113 | * the size of the allocated buffer. | |
114 | */ | |
115 | static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp, | |
116 | unsigned int nr_zones, size_t *buflen) | |
117 | { | |
118 | struct request_queue *q = sdkp->disk->queue; | |
119 | size_t bufsize; | |
120 | void *buf; | |
121 | ||
122 | /* | |
123 | * Report zone buffer size should be at most 64B times the number of | |
124 | * zones requested plus the 64B reply header, but should be at least | |
125 | * SECTOR_SIZE for ATA devices. | |
126 | * Make sure that this size does not exceed the hardware capabilities. | |
127 | * Furthermore, since the report zone command cannot be split, make | |
128 | * sure that the allocated buffer can always be mapped by limiting the | |
129 | * number of pages allocated to the HBA max segments limit. | |
130 | */ | |
23a50861 DLM |
131 | nr_zones = min(nr_zones, sdkp->nr_zones); |
132 | bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE); | |
b091ac61 DLM |
133 | bufsize = min_t(size_t, bufsize, |
134 | queue_max_hw_sectors(q) << SECTOR_SHIFT); | |
135 | bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT); | |
136 | ||
23a50861 DLM |
137 | while (bufsize >= SECTOR_SIZE) { |
138 | buf = __vmalloc(bufsize, | |
88dca4ca | 139 | GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY); |
23a50861 DLM |
140 | if (buf) { |
141 | *buflen = bufsize; | |
142 | return buf; | |
143 | } | |
144 | bufsize >>= 1; | |
145 | } | |
b091ac61 | 146 | |
23a50861 | 147 | return NULL; |
b091ac61 DLM |
148 | } |
149 | ||
e98f42bc | 150 | /** |
d4100351 CH |
151 | * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors. |
152 | * @sdkp: The target disk | |
e98f42bc | 153 | */ |
d4100351 CH |
154 | static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp) |
155 | { | |
156 | return logical_to_sectors(sdkp->device, sdkp->zone_blocks); | |
157 | } | |
158 | ||
e76239a3 | 159 | int sd_zbc_report_zones(struct gendisk *disk, sector_t sector, |
d4100351 | 160 | unsigned int nr_zones, report_zones_cb cb, void *data) |
89d94756 | 161 | { |
e76239a3 | 162 | struct scsi_disk *sdkp = scsi_disk(disk); |
51fdaa04 | 163 | sector_t capacity = logical_to_sectors(sdkp->device, sdkp->capacity); |
d4100351 | 164 | unsigned int nr, i; |
e76239a3 | 165 | unsigned char *buf; |
d4100351 CH |
166 | size_t offset, buflen = 0; |
167 | int zone_idx = 0; | |
168 | int ret; | |
89d94756 HR |
169 | |
170 | if (!sd_is_zoned(sdkp)) | |
171 | /* Not a zoned device */ | |
e76239a3 | 172 | return -EOPNOTSUPP; |
89d94756 | 173 | |
51fdaa04 DLM |
174 | if (!capacity) |
175 | /* Device gone or invalid */ | |
176 | return -ENODEV; | |
177 | ||
d4100351 | 178 | buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen); |
e76239a3 CH |
179 | if (!buf) |
180 | return -ENOMEM; | |
89d94756 | 181 | |
51fdaa04 | 182 | while (zone_idx < nr_zones && sector < capacity) { |
d4100351 CH |
183 | ret = sd_zbc_do_report_zones(sdkp, buf, buflen, |
184 | sectors_to_logical(sdkp->device, sector), true); | |
185 | if (ret) | |
186 | goto out; | |
187 | ||
188 | offset = 0; | |
189 | nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64); | |
190 | if (!nr) | |
191 | break; | |
192 | ||
193 | for (i = 0; i < nr && zone_idx < nr_zones; i++) { | |
194 | offset += 64; | |
195 | ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx, | |
196 | cb, data); | |
197 | if (ret) | |
198 | goto out; | |
199 | zone_idx++; | |
200 | } | |
89d94756 | 201 | |
d4100351 | 202 | sector += sd_zbc_zone_sectors(sdkp) * i; |
e76239a3 | 203 | } |
89d94756 | 204 | |
d4100351 | 205 | ret = zone_idx; |
b091ac61 DLM |
206 | out: |
207 | kvfree(buf); | |
e76239a3 | 208 | return ret; |
89d94756 HR |
209 | } |
210 | ||
e98f42bc | 211 | /** |
ad512f20 AJ |
212 | * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations |
213 | * can be RESET WRITE POINTER, OPEN, CLOSE or FINISH. | |
e98f42bc | 214 | * @cmd: the command to setup |
ad512f20 AJ |
215 | * @op: Operation to be performed |
216 | * @all: All zones control | |
e98f42bc | 217 | * |
ad512f20 AJ |
218 | * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL, |
219 | * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests. | |
e98f42bc | 220 | */ |
ad512f20 AJ |
221 | blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd, |
222 | unsigned char op, bool all) | |
89d94756 HR |
223 | { |
224 | struct request *rq = cmd->request; | |
225 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); | |
226 | sector_t sector = blk_rq_pos(rq); | |
227 | sector_t block = sectors_to_logical(sdkp->device, sector); | |
89d94756 HR |
228 | |
229 | if (!sd_is_zoned(sdkp)) | |
230 | /* Not a zoned device */ | |
159b2cbf | 231 | return BLK_STS_IOERR; |
89d94756 HR |
232 | |
233 | if (sdkp->device->changed) | |
159b2cbf | 234 | return BLK_STS_IOERR; |
89d94756 HR |
235 | |
236 | if (sector & (sd_zbc_zone_sectors(sdkp) - 1)) | |
237 | /* Unaligned request */ | |
159b2cbf | 238 | return BLK_STS_IOERR; |
89d94756 | 239 | |
89d94756 HR |
240 | cmd->cmd_len = 16; |
241 | memset(cmd->cmnd, 0, cmd->cmd_len); | |
242 | cmd->cmnd[0] = ZBC_OUT; | |
ad512f20 | 243 | cmd->cmnd[1] = op; |
d81e9d49 CK |
244 | if (all) |
245 | cmd->cmnd[14] = 0x1; | |
246 | else | |
247 | put_unaligned_be64(block, &cmd->cmnd[2]); | |
89d94756 HR |
248 | |
249 | rq->timeout = SD_TIMEOUT; | |
250 | cmd->sc_data_direction = DMA_NONE; | |
251 | cmd->transfersize = 0; | |
252 | cmd->allowed = 0; | |
253 | ||
159b2cbf | 254 | return BLK_STS_OK; |
89d94756 HR |
255 | } |
256 | ||
e98f42bc DLM |
257 | /** |
258 | * sd_zbc_complete - ZBC command post processing. | |
259 | * @cmd: Completed command | |
260 | * @good_bytes: Command reply bytes | |
261 | * @sshdr: command sense header | |
262 | * | |
263 | * Called from sd_done(). Process report zones reply and handle reset zone | |
264 | * and write commands errors. | |
265 | */ | |
266 | void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes, | |
89d94756 HR |
267 | struct scsi_sense_hdr *sshdr) |
268 | { | |
269 | int result = cmd->result; | |
270 | struct request *rq = cmd->request; | |
271 | ||
ad512f20 | 272 | if (op_is_zone_mgmt(req_op(rq)) && |
edc1f543 DLM |
273 | result && |
274 | sshdr->sense_key == ILLEGAL_REQUEST && | |
275 | sshdr->asc == 0x24) { | |
276 | /* | |
ad512f20 AJ |
277 | * INVALID FIELD IN CDB error: a zone management command was |
278 | * attempted on a conventional zone. Nothing to worry about, | |
279 | * so be quiet about the error. | |
edc1f543 DLM |
280 | */ |
281 | rq->rq_flags |= RQF_QUIET; | |
89d94756 HR |
282 | } |
283 | } | |
284 | ||
285 | /** | |
7f9d35d2 | 286 | * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics |
e98f42bc DLM |
287 | * @sdkp: Target disk |
288 | * @buf: Buffer where to store the VPD page data | |
289 | * | |
7f9d35d2 | 290 | * Read VPD page B6, get information and check that reads are unconstrained. |
89d94756 | 291 | */ |
7f9d35d2 DLM |
292 | static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp, |
293 | unsigned char *buf) | |
89d94756 HR |
294 | { |
295 | ||
296 | if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) { | |
297 | sd_printk(KERN_NOTICE, sdkp, | |
7f9d35d2 | 298 | "Read zoned characteristics VPD page failed\n"); |
89d94756 HR |
299 | return -ENODEV; |
300 | } | |
301 | ||
302 | if (sdkp->device->type != TYPE_ZBC) { | |
303 | /* Host-aware */ | |
304 | sdkp->urswrz = 1; | |
4a109032 DLM |
305 | sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]); |
306 | sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]); | |
89d94756 HR |
307 | sdkp->zones_max_open = 0; |
308 | } else { | |
309 | /* Host-managed */ | |
310 | sdkp->urswrz = buf[4] & 1; | |
311 | sdkp->zones_optimal_open = 0; | |
312 | sdkp->zones_optimal_nonseq = 0; | |
4a109032 | 313 | sdkp->zones_max_open = get_unaligned_be32(&buf[16]); |
89d94756 HR |
314 | } |
315 | ||
7f9d35d2 DLM |
316 | /* |
317 | * Check for unconstrained reads: host-managed devices with | |
318 | * constrained reads (drives failing read after write pointer) | |
319 | * are not supported. | |
320 | */ | |
321 | if (!sdkp->urswrz) { | |
322 | if (sdkp->first_scan) | |
323 | sd_printk(KERN_NOTICE, sdkp, | |
324 | "constrained reads devices are not supported\n"); | |
325 | return -ENODEV; | |
326 | } | |
327 | ||
89d94756 HR |
328 | return 0; |
329 | } | |
330 | ||
e98f42bc | 331 | /** |
dbfc5626 | 332 | * sd_zbc_check_capacity - Check the device capacity |
e98f42bc | 333 | * @sdkp: Target disk |
dbfc5626 DLM |
334 | * @buf: command buffer |
335 | * @zblock: zone size in number of blocks | |
e98f42bc | 336 | * |
dbfc5626 DLM |
337 | * Get the device zone size and check that the device capacity as reported |
338 | * by READ CAPACITY matches the max_lba value (plus one) of the report zones | |
339 | * command reply for devices with RC_BASIS == 0. | |
ccce20fc | 340 | * |
dbfc5626 | 341 | * Returns 0 upon success or an error code upon failure. |
e98f42bc | 342 | */ |
dbfc5626 DLM |
343 | static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf, |
344 | u32 *zblocks) | |
89d94756 | 345 | { |
dbfc5626 | 346 | u64 zone_blocks; |
d9dd7308 | 347 | sector_t max_lba; |
89d94756 | 348 | unsigned char *rec; |
5f832a39 | 349 | int ret; |
b091ac61 | 350 | |
d9dd7308 DLM |
351 | /* Do a report zone to get max_lba and the size of the first zone */ |
352 | ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false); | |
4b433924 | 353 | if (ret) |
d9dd7308 | 354 | return ret; |
89d94756 | 355 | |
d2e428e4 DLM |
356 | if (sdkp->rc_basis == 0) { |
357 | /* The max_lba field is the capacity of this device */ | |
358 | max_lba = get_unaligned_be64(&buf[8]); | |
359 | if (sdkp->capacity != max_lba + 1) { | |
360 | if (sdkp->first_scan) | |
361 | sd_printk(KERN_WARNING, sdkp, | |
362 | "Changing capacity from %llu to max LBA+1 %llu\n", | |
363 | (unsigned long long)sdkp->capacity, | |
364 | (unsigned long long)max_lba + 1); | |
365 | sdkp->capacity = max_lba + 1; | |
366 | } | |
367 | } | |
368 | ||
dbfc5626 | 369 | /* Get the size of the first reported zone */ |
d9dd7308 DLM |
370 | rec = buf + 64; |
371 | zone_blocks = get_unaligned_be64(&rec[8]); | |
d9dd7308 | 372 | if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) { |
89d94756 HR |
373 | if (sdkp->first_scan) |
374 | sd_printk(KERN_NOTICE, sdkp, | |
375 | "Zone size too large\n"); | |
d9dd7308 | 376 | return -EFBIG; |
89d94756 HR |
377 | } |
378 | ||
d9dd7308 | 379 | *zblocks = zone_blocks; |
89d94756 | 380 | |
d9dd7308 | 381 | return 0; |
89d94756 HR |
382 | } |
383 | ||
e98f42bc | 384 | int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf) |
89d94756 | 385 | { |
bf505456 DLM |
386 | struct gendisk *disk = sdkp->disk; |
387 | unsigned int nr_zones; | |
0cdc5858 | 388 | u32 zone_blocks = 0; |
f7053240 | 389 | int ret; |
89d94756 HR |
390 | |
391 | if (!sd_is_zoned(sdkp)) | |
392 | /* | |
393 | * Device managed or normal SCSI disk, | |
394 | * no special handling required | |
395 | */ | |
396 | return 0; | |
397 | ||
7f9d35d2 DLM |
398 | /* Check zoned block device characteristics (unconstrained reads) */ |
399 | ret = sd_zbc_check_zoned_characteristics(sdkp, buf); | |
89d94756 HR |
400 | if (ret) |
401 | goto err; | |
402 | ||
dbfc5626 DLM |
403 | /* Check the device capacity reported by report zones */ |
404 | ret = sd_zbc_check_capacity(sdkp, buf, &zone_blocks); | |
5f832a39 | 405 | if (ret != 0) |
89d94756 HR |
406 | goto err; |
407 | ||
408 | /* The drive satisfies the kernel restrictions: set it up */ | |
d81e9d49 | 409 | blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, sdkp->disk->queue); |
ebddd2a1 DLM |
410 | blk_queue_required_elevator_features(sdkp->disk->queue, |
411 | ELEVATOR_F_ZBD_SEQ_WRITE); | |
bf505456 DLM |
412 | nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks); |
413 | ||
414 | /* READ16/WRITE16 is mandatory for ZBC disks */ | |
415 | sdkp->device->use_16_for_rw = 1; | |
416 | sdkp->device->use_10_for_rw = 0; | |
417 | ||
418 | /* | |
88fc41c4 DLM |
419 | * Revalidate the disk zone bitmaps once the block device capacity is |
420 | * set on the second revalidate execution during disk scan and if | |
421 | * something changed when executing a normal revalidate. | |
bf505456 | 422 | */ |
88fc41c4 DLM |
423 | if (sdkp->first_scan) { |
424 | sdkp->zone_blocks = zone_blocks; | |
425 | sdkp->nr_zones = nr_zones; | |
bf505456 | 426 | return 0; |
88fc41c4 DLM |
427 | } |
428 | ||
bf505456 DLM |
429 | if (sdkp->zone_blocks != zone_blocks || |
430 | sdkp->nr_zones != nr_zones || | |
431 | disk->queue->nr_zones != nr_zones) { | |
432 | ret = blk_revalidate_disk_zones(disk); | |
433 | if (ret != 0) | |
434 | goto err; | |
435 | sdkp->zone_blocks = zone_blocks; | |
436 | sdkp->nr_zones = nr_zones; | |
437 | } | |
89d94756 HR |
438 | |
439 | return 0; | |
440 | ||
441 | err: | |
442 | sdkp->capacity = 0; | |
443 | ||
444 | return ret; | |
445 | } | |
446 | ||
89d94756 HR |
447 | void sd_zbc_print_zones(struct scsi_disk *sdkp) |
448 | { | |
449 | if (!sd_is_zoned(sdkp) || !sdkp->capacity) | |
450 | return; | |
451 | ||
452 | if (sdkp->capacity & (sdkp->zone_blocks - 1)) | |
453 | sd_printk(KERN_NOTICE, sdkp, | |
454 | "%u zones of %u logical blocks + 1 runt zone\n", | |
455 | sdkp->nr_zones - 1, | |
456 | sdkp->zone_blocks); | |
457 | else | |
458 | sd_printk(KERN_NOTICE, sdkp, | |
459 | "%u zones of %u logical blocks\n", | |
460 | sdkp->nr_zones, | |
461 | sdkp->zone_blocks); | |
462 | } |