]>
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 <[email protected]> | |
7 | * Modified by: Damien Le Moal <[email protected]> | |
8 | * Modified by: Shaun Tancheff <[email protected]> | |
89d94756 HR |
9 | */ |
10 | ||
11 | #include <linux/blkdev.h> | |
b091ac61 DLM |
12 | #include <linux/vmalloc.h> |
13 | #include <linux/sched/mm.h> | |
5795eb44 | 14 | #include <linux/mutex.h> |
89d94756 HR |
15 | |
16 | #include <asm/unaligned.h> | |
17 | ||
18 | #include <scsi/scsi.h> | |
19 | #include <scsi/scsi_cmnd.h> | |
89d94756 HR |
20 | |
21 | #include "sd.h" | |
89d94756 | 22 | |
9f5436f4 JT |
23 | #define CREATE_TRACE_POINTS |
24 | #include "sd_trace.h" | |
25 | ||
c976e588 DLM |
26 | /* Whether or not a SCSI zone descriptor describes a gap zone. */ |
27 | static bool sd_zbc_is_gap_zone(const u8 buf[64]) | |
28 | { | |
29 | return (buf[0] & 0xf) == ZBC_ZONE_TYPE_GAP; | |
30 | } | |
31 | ||
aa96bfb4 BVA |
32 | /** |
33 | * sd_zbc_parse_report - Parse a SCSI zone descriptor | |
34 | * @sdkp: SCSI disk pointer. | |
35 | * @buf: SCSI zone descriptor. | |
36 | * @idx: Index of the zone relative to the first zone reported by the current | |
37 | * sd_zbc_report_zones() call. | |
38 | * @cb: Callback function pointer. | |
39 | * @data: Second argument passed to @cb. | |
40 | * | |
41 | * Return: Value returned by @cb. | |
42 | * | |
43 | * Convert a SCSI zone descriptor into struct blk_zone format. Additionally, | |
44 | * call @cb(blk_zone, @data). | |
45 | */ | |
46 | static int sd_zbc_parse_report(struct scsi_disk *sdkp, const u8 buf[64], | |
d4100351 | 47 | unsigned int idx, report_zones_cb cb, void *data) |
89d94756 HR |
48 | { |
49 | struct scsi_device *sdp = sdkp->device; | |
d4100351 | 50 | struct blk_zone zone = { 0 }; |
c976e588 | 51 | sector_t start_lba, gran; |
5795eb44 | 52 | int ret; |
89d94756 | 53 | |
c976e588 DLM |
54 | if (WARN_ON_ONCE(sd_zbc_is_gap_zone(buf))) |
55 | return -EINVAL; | |
56 | ||
d4100351 CH |
57 | zone.type = buf[0] & 0x0f; |
58 | zone.cond = (buf[1] >> 4) & 0xf; | |
89d94756 | 59 | if (buf[1] & 0x01) |
d4100351 | 60 | zone.reset = 1; |
89d94756 | 61 | if (buf[1] & 0x02) |
d4100351 CH |
62 | zone.non_seq = 1; |
63 | ||
c976e588 DLM |
64 | start_lba = get_unaligned_be64(&buf[16]); |
65 | zone.start = logical_to_sectors(sdp, start_lba); | |
66 | zone.capacity = logical_to_sectors(sdp, get_unaligned_be64(&buf[8])); | |
67 | zone.len = zone.capacity; | |
68 | if (sdkp->zone_starting_lba_gran) { | |
69 | gran = logical_to_sectors(sdp, sdkp->zone_starting_lba_gran); | |
70 | if (zone.len > gran) { | |
71 | sd_printk(KERN_ERR, sdkp, | |
72 | "Invalid zone at LBA %llu with capacity %llu and length %llu; granularity = %llu\n", | |
73 | start_lba, | |
74 | sectors_to_logical(sdp, zone.capacity), | |
75 | sectors_to_logical(sdp, zone.len), | |
76 | sectors_to_logical(sdp, gran)); | |
77 | return -EINVAL; | |
78 | } | |
79 | /* | |
80 | * Use the starting LBA granularity instead of the zone length | |
81 | * obtained from the REPORT ZONES command. | |
82 | */ | |
83 | zone.len = gran; | |
84 | } | |
13202ebf | 85 | if (zone.cond == ZBC_ZONE_COND_FULL) |
d4100351 | 86 | zone.wp = zone.start + zone.len; |
bf3f120f NC |
87 | else |
88 | zone.wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24])); | |
d4100351 | 89 | |
5795eb44 JT |
90 | ret = cb(&zone, idx, data); |
91 | if (ret) | |
92 | return ret; | |
93 | ||
5795eb44 | 94 | return 0; |
89d94756 HR |
95 | } |
96 | ||
97 | /** | |
e76239a3 | 98 | * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command. |
e98f42bc | 99 | * @sdkp: The target disk |
b091ac61 | 100 | * @buf: vmalloc-ed buffer to use for the reply |
e98f42bc DLM |
101 | * @buflen: the buffer size |
102 | * @lba: Start LBA of the report | |
d2e428e4 | 103 | * @partial: Do partial report |
e98f42bc DLM |
104 | * |
105 | * For internal use during device validation. | |
d2e428e4 DLM |
106 | * Using partial=true can significantly speed up execution of a report zones |
107 | * command because the disk does not have to count all possible report matching | |
108 | * zones and will only report the count of zones fitting in the command reply | |
109 | * buffer. | |
89d94756 | 110 | */ |
e76239a3 CH |
111 | static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf, |
112 | unsigned int buflen, sector_t lba, | |
113 | bool partial) | |
89d94756 HR |
114 | { |
115 | struct scsi_device *sdp = sdkp->device; | |
116 | const int timeout = sdp->request_queue->rq_timeout; | |
117 | struct scsi_sense_hdr sshdr; | |
6ff236e8 MC |
118 | const struct scsi_exec_args exec_args = { |
119 | .sshdr = &sshdr, | |
120 | }; | |
89d94756 HR |
121 | unsigned char cmd[16]; |
122 | unsigned int rep_len; | |
123 | int result; | |
124 | ||
125 | memset(cmd, 0, 16); | |
126 | cmd[0] = ZBC_IN; | |
127 | cmd[1] = ZI_REPORT_ZONES; | |
128 | put_unaligned_be64(lba, &cmd[2]); | |
129 | put_unaligned_be32(buflen, &cmd[10]); | |
d2e428e4 DLM |
130 | if (partial) |
131 | cmd[14] = ZBC_REPORT_ZONE_PARTIAL; | |
89d94756 | 132 | |
6ff236e8 MC |
133 | result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, buf, buflen, |
134 | timeout, SD_MAX_RETRIES, &exec_args); | |
89d94756 HR |
135 | if (result) { |
136 | sd_printk(KERN_ERR, sdkp, | |
a35989a0 DLM |
137 | "REPORT ZONES start lba %llu failed\n", lba); |
138 | sd_print_result(sdkp, "REPORT ZONES", result); | |
464a00c9 | 139 | if (result > 0 && scsi_sense_valid(&sshdr)) |
a35989a0 | 140 | sd_print_sense_hdr(sdkp, &sshdr); |
89d94756 HR |
141 | return -EIO; |
142 | } | |
143 | ||
144 | rep_len = get_unaligned_be32(&buf[0]); | |
145 | if (rep_len < 64) { | |
146 | sd_printk(KERN_ERR, sdkp, | |
147 | "REPORT ZONES report invalid length %u\n", | |
148 | rep_len); | |
149 | return -EIO; | |
150 | } | |
151 | ||
152 | return 0; | |
153 | } | |
154 | ||
b091ac61 | 155 | /** |
59863cb5 | 156 | * sd_zbc_alloc_report_buffer() - Allocate a buffer for report zones reply. |
b091ac61 DLM |
157 | * @sdkp: The target disk |
158 | * @nr_zones: Maximum number of zones to report | |
159 | * @buflen: Size of the buffer allocated | |
160 | * | |
161 | * Try to allocate a reply buffer for the number of requested zones. | |
162 | * The size of the buffer allocated may be smaller than requested to | |
163 | * satify the device constraint (max_hw_sectors, max_segments, etc). | |
164 | * | |
165 | * Return the address of the allocated buffer and update @buflen with | |
166 | * the size of the allocated buffer. | |
167 | */ | |
168 | static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp, | |
169 | unsigned int nr_zones, size_t *buflen) | |
170 | { | |
171 | struct request_queue *q = sdkp->disk->queue; | |
172 | size_t bufsize; | |
173 | void *buf; | |
174 | ||
175 | /* | |
176 | * Report zone buffer size should be at most 64B times the number of | |
7215e909 NA |
177 | * zones requested plus the 64B reply header, but should be aligned |
178 | * to SECTOR_SIZE for ATA devices. | |
b091ac61 DLM |
179 | * Make sure that this size does not exceed the hardware capabilities. |
180 | * Furthermore, since the report zone command cannot be split, make | |
181 | * sure that the allocated buffer can always be mapped by limiting the | |
182 | * number of pages allocated to the HBA max segments limit. | |
183 | */ | |
628617be | 184 | nr_zones = min(nr_zones, sdkp->zone_info.nr_zones); |
23a50861 | 185 | bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE); |
b091ac61 DLM |
186 | bufsize = min_t(size_t, bufsize, |
187 | queue_max_hw_sectors(q) << SECTOR_SHIFT); | |
188 | bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT); | |
189 | ||
23a50861 DLM |
190 | while (bufsize >= SECTOR_SIZE) { |
191 | buf = __vmalloc(bufsize, | |
88dca4ca | 192 | GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY); |
23a50861 DLM |
193 | if (buf) { |
194 | *buflen = bufsize; | |
195 | return buf; | |
196 | } | |
7215e909 | 197 | bufsize = rounddown(bufsize >> 1, SECTOR_SIZE); |
23a50861 | 198 | } |
b091ac61 | 199 | |
23a50861 | 200 | return NULL; |
b091ac61 DLM |
201 | } |
202 | ||
e98f42bc | 203 | /** |
d4100351 CH |
204 | * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors. |
205 | * @sdkp: The target disk | |
e98f42bc | 206 | */ |
d4100351 CH |
207 | static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp) |
208 | { | |
628617be | 209 | return logical_to_sectors(sdkp->device, sdkp->zone_info.zone_blocks); |
d4100351 CH |
210 | } |
211 | ||
aa96bfb4 BVA |
212 | /** |
213 | * sd_zbc_report_zones - SCSI .report_zones() callback. | |
214 | * @disk: Disk to report zones for. | |
215 | * @sector: Start sector. | |
216 | * @nr_zones: Maximum number of zones to report. | |
217 | * @cb: Callback function called to report zone information. | |
218 | * @data: Second argument passed to @cb. | |
219 | * | |
220 | * Called by the block layer to iterate over zone information. See also the | |
221 | * disk->fops->report_zones() calls in block/blk-zoned.c. | |
222 | */ | |
e76239a3 | 223 | int sd_zbc_report_zones(struct gendisk *disk, sector_t sector, |
d4100351 | 224 | unsigned int nr_zones, report_zones_cb cb, void *data) |
89d94756 | 225 | { |
e76239a3 | 226 | struct scsi_disk *sdkp = scsi_disk(disk); |
43af5da0 | 227 | sector_t lba = sectors_to_logical(sdkp->device, sector); |
d4100351 | 228 | unsigned int nr, i; |
e76239a3 | 229 | unsigned char *buf; |
c976e588 | 230 | u64 zone_length, start_lba; |
d4100351 CH |
231 | size_t offset, buflen = 0; |
232 | int zone_idx = 0; | |
233 | int ret; | |
89d94756 HR |
234 | |
235 | if (!sd_is_zoned(sdkp)) | |
236 | /* Not a zoned device */ | |
e76239a3 | 237 | return -EOPNOTSUPP; |
89d94756 | 238 | |
43af5da0 | 239 | if (!sdkp->capacity) |
51fdaa04 DLM |
240 | /* Device gone or invalid */ |
241 | return -ENODEV; | |
242 | ||
d4100351 | 243 | buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen); |
e76239a3 CH |
244 | if (!buf) |
245 | return -ENOMEM; | |
89d94756 | 246 | |
43af5da0 DLM |
247 | while (zone_idx < nr_zones && lba < sdkp->capacity) { |
248 | ret = sd_zbc_do_report_zones(sdkp, buf, buflen, lba, true); | |
d4100351 CH |
249 | if (ret) |
250 | goto out; | |
251 | ||
252 | offset = 0; | |
253 | nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64); | |
254 | if (!nr) | |
255 | break; | |
256 | ||
257 | for (i = 0; i < nr && zone_idx < nr_zones; i++) { | |
258 | offset += 64; | |
c976e588 DLM |
259 | start_lba = get_unaligned_be64(&buf[offset + 16]); |
260 | zone_length = get_unaligned_be64(&buf[offset + 8]); | |
261 | if ((zone_idx == 0 && | |
262 | (lba < start_lba || | |
263 | lba >= start_lba + zone_length)) || | |
264 | (zone_idx > 0 && start_lba != lba) || | |
265 | start_lba + zone_length < start_lba) { | |
266 | sd_printk(KERN_ERR, sdkp, | |
267 | "Zone %d at LBA %llu is invalid: %llu + %llu\n", | |
268 | zone_idx, lba, start_lba, zone_length); | |
269 | ret = -EINVAL; | |
270 | goto out; | |
271 | } | |
272 | lba = start_lba + zone_length; | |
273 | if (sd_zbc_is_gap_zone(&buf[offset])) { | |
274 | if (sdkp->zone_starting_lba_gran) | |
275 | continue; | |
276 | sd_printk(KERN_ERR, sdkp, | |
277 | "Gap zone without constant LBA offsets\n"); | |
278 | ret = -EINVAL; | |
279 | goto out; | |
280 | } | |
281 | ||
d4100351 CH |
282 | ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx, |
283 | cb, data); | |
284 | if (ret) | |
285 | goto out; | |
c976e588 | 286 | |
d4100351 CH |
287 | zone_idx++; |
288 | } | |
e76239a3 | 289 | } |
89d94756 | 290 | |
d4100351 | 291 | ret = zone_idx; |
b091ac61 DLM |
292 | out: |
293 | kvfree(buf); | |
e76239a3 | 294 | return ret; |
89d94756 HR |
295 | } |
296 | ||
02494d35 JT |
297 | static blk_status_t sd_zbc_cmnd_checks(struct scsi_cmnd *cmd) |
298 | { | |
5999ccff | 299 | struct request *rq = scsi_cmd_to_rq(cmd); |
f3fa33ac | 300 | struct scsi_disk *sdkp = scsi_disk(rq->q->disk); |
02494d35 JT |
301 | sector_t sector = blk_rq_pos(rq); |
302 | ||
303 | if (!sd_is_zoned(sdkp)) | |
304 | /* Not a zoned device */ | |
305 | return BLK_STS_IOERR; | |
306 | ||
307 | if (sdkp->device->changed) | |
308 | return BLK_STS_IOERR; | |
309 | ||
310 | if (sector & (sd_zbc_zone_sectors(sdkp) - 1)) | |
311 | /* Unaligned request */ | |
312 | return BLK_STS_IOERR; | |
313 | ||
314 | return BLK_STS_OK; | |
315 | } | |
316 | ||
e98f42bc | 317 | /** |
ad512f20 AJ |
318 | * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations |
319 | * can be RESET WRITE POINTER, OPEN, CLOSE or FINISH. | |
e98f42bc | 320 | * @cmd: the command to setup |
ad512f20 AJ |
321 | * @op: Operation to be performed |
322 | * @all: All zones control | |
e98f42bc | 323 | * |
ad512f20 AJ |
324 | * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL, |
325 | * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests. | |
e98f42bc | 326 | */ |
ad512f20 AJ |
327 | blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd, |
328 | unsigned char op, bool all) | |
89d94756 | 329 | { |
5999ccff | 330 | struct request *rq = scsi_cmd_to_rq(cmd); |
89d94756 | 331 | sector_t sector = blk_rq_pos(rq); |
f3fa33ac | 332 | struct scsi_disk *sdkp = scsi_disk(rq->q->disk); |
89d94756 | 333 | sector_t block = sectors_to_logical(sdkp->device, sector); |
02494d35 | 334 | blk_status_t ret; |
89d94756 | 335 | |
02494d35 JT |
336 | ret = sd_zbc_cmnd_checks(cmd); |
337 | if (ret != BLK_STS_OK) | |
338 | return ret; | |
89d94756 | 339 | |
89d94756 HR |
340 | cmd->cmd_len = 16; |
341 | memset(cmd->cmnd, 0, cmd->cmd_len); | |
342 | cmd->cmnd[0] = ZBC_OUT; | |
ad512f20 | 343 | cmd->cmnd[1] = op; |
d81e9d49 CK |
344 | if (all) |
345 | cmd->cmnd[14] = 0x1; | |
346 | else | |
347 | put_unaligned_be64(block, &cmd->cmnd[2]); | |
89d94756 HR |
348 | |
349 | rq->timeout = SD_TIMEOUT; | |
350 | cmd->sc_data_direction = DMA_NONE; | |
351 | cmd->transfersize = 0; | |
352 | cmd->allowed = 0; | |
353 | ||
159b2cbf | 354 | return BLK_STS_OK; |
89d94756 HR |
355 | } |
356 | ||
e98f42bc DLM |
357 | /** |
358 | * sd_zbc_complete - ZBC command post processing. | |
359 | * @cmd: Completed command | |
360 | * @good_bytes: Command reply bytes | |
361 | * @sshdr: command sense header | |
362 | * | |
5795eb44 JT |
363 | * Called from sd_done() to handle zone commands errors and updates to the |
364 | * device queue zone write pointer offset cahce. | |
e98f42bc | 365 | */ |
5795eb44 | 366 | unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes, |
89d94756 HR |
367 | struct scsi_sense_hdr *sshdr) |
368 | { | |
369 | int result = cmd->result; | |
5999ccff | 370 | struct request *rq = scsi_cmd_to_rq(cmd); |
89d94756 | 371 | |
ad512f20 | 372 | if (op_is_zone_mgmt(req_op(rq)) && |
edc1f543 DLM |
373 | result && |
374 | sshdr->sense_key == ILLEGAL_REQUEST && | |
375 | sshdr->asc == 0x24) { | |
376 | /* | |
ad512f20 AJ |
377 | * INVALID FIELD IN CDB error: a zone management command was |
378 | * attempted on a conventional zone. Nothing to worry about, | |
379 | * so be quiet about the error. | |
edc1f543 DLM |
380 | */ |
381 | rq->rq_flags |= RQF_QUIET; | |
1846f308 | 382 | } |
5795eb44 JT |
383 | |
384 | return good_bytes; | |
89d94756 HR |
385 | } |
386 | ||
387 | /** | |
7f9d35d2 | 388 | * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics |
e98f42bc DLM |
389 | * @sdkp: Target disk |
390 | * @buf: Buffer where to store the VPD page data | |
391 | * | |
7f9d35d2 | 392 | * Read VPD page B6, get information and check that reads are unconstrained. |
89d94756 | 393 | */ |
7f9d35d2 DLM |
394 | static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp, |
395 | unsigned char *buf) | |
89d94756 | 396 | { |
c976e588 | 397 | u64 zone_starting_lba_gran; |
89d94756 HR |
398 | |
399 | if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) { | |
400 | sd_printk(KERN_NOTICE, sdkp, | |
7f9d35d2 | 401 | "Read zoned characteristics VPD page failed\n"); |
89d94756 HR |
402 | return -ENODEV; |
403 | } | |
404 | ||
405 | if (sdkp->device->type != TYPE_ZBC) { | |
406 | /* Host-aware */ | |
407 | sdkp->urswrz = 1; | |
4a109032 DLM |
408 | sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]); |
409 | sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]); | |
89d94756 | 410 | sdkp->zones_max_open = 0; |
60caf375 | 411 | return 0; |
89d94756 HR |
412 | } |
413 | ||
60caf375 DLM |
414 | /* Host-managed */ |
415 | sdkp->urswrz = buf[4] & 1; | |
416 | sdkp->zones_optimal_open = 0; | |
417 | sdkp->zones_optimal_nonseq = 0; | |
418 | sdkp->zones_max_open = get_unaligned_be32(&buf[16]); | |
c976e588 DLM |
419 | /* Check zone alignment method */ |
420 | switch (buf[23] & 0xf) { | |
421 | case 0: | |
422 | case ZBC_CONSTANT_ZONE_LENGTH: | |
423 | /* Use zone length */ | |
424 | break; | |
425 | case ZBC_CONSTANT_ZONE_START_OFFSET: | |
426 | zone_starting_lba_gran = get_unaligned_be64(&buf[24]); | |
427 | if (zone_starting_lba_gran == 0 || | |
428 | !is_power_of_2(zone_starting_lba_gran) || | |
429 | logical_to_sectors(sdkp->device, zone_starting_lba_gran) > | |
430 | UINT_MAX) { | |
431 | sd_printk(KERN_ERR, sdkp, | |
432 | "Invalid zone starting LBA granularity %llu\n", | |
433 | zone_starting_lba_gran); | |
434 | return -ENODEV; | |
435 | } | |
436 | sdkp->zone_starting_lba_gran = zone_starting_lba_gran; | |
437 | break; | |
438 | default: | |
439 | sd_printk(KERN_ERR, sdkp, "Invalid zone alignment method\n"); | |
440 | return -ENODEV; | |
441 | } | |
60caf375 | 442 | |
7f9d35d2 DLM |
443 | /* |
444 | * Check for unconstrained reads: host-managed devices with | |
445 | * constrained reads (drives failing read after write pointer) | |
446 | * are not supported. | |
447 | */ | |
448 | if (!sdkp->urswrz) { | |
449 | if (sdkp->first_scan) | |
450 | sd_printk(KERN_NOTICE, sdkp, | |
451 | "constrained reads devices are not supported\n"); | |
452 | return -ENODEV; | |
453 | } | |
454 | ||
89d94756 HR |
455 | return 0; |
456 | } | |
457 | ||
e98f42bc | 458 | /** |
dbfc5626 | 459 | * sd_zbc_check_capacity - Check the device capacity |
e98f42bc | 460 | * @sdkp: Target disk |
dbfc5626 | 461 | * @buf: command buffer |
aa96bfb4 | 462 | * @zblocks: zone size in logical blocks |
e98f42bc | 463 | * |
dbfc5626 DLM |
464 | * Get the device zone size and check that the device capacity as reported |
465 | * by READ CAPACITY matches the max_lba value (plus one) of the report zones | |
466 | * command reply for devices with RC_BASIS == 0. | |
ccce20fc | 467 | * |
dbfc5626 | 468 | * Returns 0 upon success or an error code upon failure. |
e98f42bc | 469 | */ |
dbfc5626 DLM |
470 | static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf, |
471 | u32 *zblocks) | |
89d94756 | 472 | { |
dbfc5626 | 473 | u64 zone_blocks; |
d9dd7308 | 474 | sector_t max_lba; |
89d94756 | 475 | unsigned char *rec; |
5f832a39 | 476 | int ret; |
b091ac61 | 477 | |
d9dd7308 DLM |
478 | /* Do a report zone to get max_lba and the size of the first zone */ |
479 | ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false); | |
4b433924 | 480 | if (ret) |
d9dd7308 | 481 | return ret; |
89d94756 | 482 | |
d2e428e4 DLM |
483 | if (sdkp->rc_basis == 0) { |
484 | /* The max_lba field is the capacity of this device */ | |
485 | max_lba = get_unaligned_be64(&buf[8]); | |
486 | if (sdkp->capacity != max_lba + 1) { | |
487 | if (sdkp->first_scan) | |
488 | sd_printk(KERN_WARNING, sdkp, | |
489 | "Changing capacity from %llu to max LBA+1 %llu\n", | |
490 | (unsigned long long)sdkp->capacity, | |
491 | (unsigned long long)max_lba + 1); | |
492 | sdkp->capacity = max_lba + 1; | |
493 | } | |
494 | } | |
495 | ||
c976e588 DLM |
496 | if (sdkp->zone_starting_lba_gran == 0) { |
497 | /* Get the size of the first reported zone */ | |
498 | rec = buf + 64; | |
499 | zone_blocks = get_unaligned_be64(&rec[8]); | |
500 | if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) { | |
501 | if (sdkp->first_scan) | |
502 | sd_printk(KERN_NOTICE, sdkp, | |
503 | "Zone size too large\n"); | |
504 | return -EFBIG; | |
505 | } | |
506 | } else { | |
507 | zone_blocks = sdkp->zone_starting_lba_gran; | |
89d94756 HR |
508 | } |
509 | ||
9a93b9c9 BVA |
510 | if (!is_power_of_2(zone_blocks)) { |
511 | sd_printk(KERN_ERR, sdkp, | |
512 | "Zone size %llu is not a power of two.\n", | |
513 | zone_blocks); | |
514 | return -EINVAL; | |
515 | } | |
516 | ||
d9dd7308 | 517 | *zblocks = zone_blocks; |
89d94756 | 518 | |
d9dd7308 | 519 | return 0; |
89d94756 HR |
520 | } |
521 | ||
a3d8a257 DLM |
522 | static void sd_zbc_print_zones(struct scsi_disk *sdkp) |
523 | { | |
524 | if (!sd_is_zoned(sdkp) || !sdkp->capacity) | |
525 | return; | |
526 | ||
628617be | 527 | if (sdkp->capacity & (sdkp->zone_info.zone_blocks - 1)) |
a3d8a257 DLM |
528 | sd_printk(KERN_NOTICE, sdkp, |
529 | "%u zones of %u logical blocks + 1 runt zone\n", | |
628617be BVA |
530 | sdkp->zone_info.nr_zones - 1, |
531 | sdkp->zone_info.zone_blocks); | |
a3d8a257 DLM |
532 | else |
533 | sd_printk(KERN_NOTICE, sdkp, | |
534 | "%u zones of %u logical blocks\n", | |
628617be BVA |
535 | sdkp->zone_info.nr_zones, |
536 | sdkp->zone_info.zone_blocks); | |
a3d8a257 DLM |
537 | } |
538 | ||
aa96bfb4 BVA |
539 | /* |
540 | * Call blk_revalidate_disk_zones() if any of the zoned disk properties have | |
541 | * changed that make it necessary to call that function. Called by | |
542 | * sd_revalidate_disk() after the gendisk capacity has been set. | |
543 | */ | |
a3d8a257 | 544 | int sd_zbc_revalidate_zones(struct scsi_disk *sdkp) |
5795eb44 JT |
545 | { |
546 | struct gendisk *disk = sdkp->disk; | |
a3d8a257 | 547 | struct request_queue *q = disk->queue; |
628617be BVA |
548 | u32 zone_blocks = sdkp->early_zone_info.zone_blocks; |
549 | unsigned int nr_zones = sdkp->early_zone_info.nr_zones; | |
9acced3f | 550 | unsigned int flags; |
1846f308 | 551 | int ret; |
6c5dee18 | 552 | |
27ba3e8f DLM |
553 | /* |
554 | * There is nothing to do for regular disks, including host-aware disks | |
555 | * that have partitions. | |
556 | */ | |
557 | if (!blk_queue_is_zoned(q)) | |
a3d8a257 DLM |
558 | return 0; |
559 | ||
628617be BVA |
560 | if (sdkp->zone_info.zone_blocks == zone_blocks && |
561 | sdkp->zone_info.nr_zones == nr_zones && | |
d86e716a | 562 | disk->nr_zones == nr_zones) |
1846f308 | 563 | return 0; |
5795eb44 | 564 | |
628617be BVA |
565 | sdkp->zone_info.zone_blocks = zone_blocks; |
566 | sdkp->zone_info.nr_zones = nr_zones; | |
5795eb44 | 567 | |
f79846ca DLM |
568 | blk_queue_chunk_sectors(q, |
569 | logical_to_sectors(sdkp->device, zone_blocks)); | |
f79846ca | 570 | |
1846f308 DLM |
571 | /* Enable block layer zone append emulation */ |
572 | blk_queue_max_zone_append_sectors(q, 0); | |
5795eb44 | 573 | |
1846f308 | 574 | flags = memalloc_noio_save(); |
9b3c08b9 | 575 | ret = blk_revalidate_disk_zones(disk); |
9acced3f | 576 | memalloc_noio_restore(flags); |
a3d8a257 | 577 | if (ret) { |
628617be | 578 | sdkp->zone_info = (struct zoned_disk_info){ }; |
a3d8a257 | 579 | sdkp->capacity = 0; |
1846f308 | 580 | return ret; |
a3d8a257 DLM |
581 | } |
582 | ||
a3d8a257 DLM |
583 | sd_zbc_print_zones(sdkp); |
584 | ||
1846f308 | 585 | return 0; |
5795eb44 JT |
586 | } |
587 | ||
aa96bfb4 BVA |
588 | /** |
589 | * sd_zbc_read_zones - Read zone information and update the request queue | |
590 | * @sdkp: SCSI disk pointer. | |
591 | * @buf: 512 byte buffer used for storing SCSI command output. | |
592 | * | |
593 | * Read zone information and update the request queue zone characteristics and | |
594 | * also the zoned device information in *sdkp. Called by sd_revalidate_disk() | |
595 | * before the gendisk capacity has been set. | |
596 | */ | |
597 | int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE]) | |
89d94756 | 598 | { |
bf505456 | 599 | struct gendisk *disk = sdkp->disk; |
5795eb44 | 600 | struct request_queue *q = disk->queue; |
bf505456 | 601 | unsigned int nr_zones; |
0cdc5858 | 602 | u32 zone_blocks = 0; |
f7053240 | 603 | int ret; |
89d94756 | 604 | |
30c4fdc3 | 605 | if (!sd_is_zoned(sdkp)) { |
89d94756 | 606 | /* |
30c4fdc3 | 607 | * Device managed or normal SCSI disk, no special handling |
1846f308 | 608 | * required. |
89d94756 HR |
609 | */ |
610 | return 0; | |
30c4fdc3 | 611 | } |
89d94756 | 612 | |
42c59077 | 613 | /* READ16/WRITE16/SYNC16 is mandatory for ZBC devices */ |
78e1663f DLM |
614 | sdkp->device->use_16_for_rw = 1; |
615 | sdkp->device->use_10_for_rw = 0; | |
42c59077 | 616 | sdkp->device->use_16_for_sync = 1; |
78e1663f | 617 | |
7f9d35d2 DLM |
618 | /* Check zoned block device characteristics (unconstrained reads) */ |
619 | ret = sd_zbc_check_zoned_characteristics(sdkp, buf); | |
89d94756 HR |
620 | if (ret) |
621 | goto err; | |
622 | ||
dbfc5626 DLM |
623 | /* Check the device capacity reported by report zones */ |
624 | ret = sd_zbc_check_capacity(sdkp, buf, &zone_blocks); | |
5f832a39 | 625 | if (ret != 0) |
89d94756 HR |
626 | goto err; |
627 | ||
628 | /* The drive satisfies the kernel restrictions: set it up */ | |
5795eb44 | 629 | blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q); |
e15864f8 | 630 | if (sdkp->zones_max_open == U32_MAX) |
982977df | 631 | disk_set_max_open_zones(disk, 0); |
e15864f8 | 632 | else |
982977df CH |
633 | disk_set_max_open_zones(disk, sdkp->zones_max_open); |
634 | disk_set_max_active_zones(disk, 0); | |
bf505456 DLM |
635 | nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks); |
636 | ||
628617be BVA |
637 | sdkp->early_zone_info.nr_zones = nr_zones; |
638 | sdkp->early_zone_info.zone_blocks = zone_blocks; | |
89d94756 HR |
639 | |
640 | return 0; | |
641 | ||
642 | err: | |
643 | sdkp->capacity = 0; | |
644 | ||
645 | return ret; | |
646 | } |