]>
Commit | Line | Data |
---|---|---|
89d94756 HR |
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> | |
89d94756 HR |
31 | |
32 | #include "sd.h" | |
89d94756 HR |
33 | |
34 | /** | |
e98f42bc DLM |
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. | |
89d94756 | 41 | */ |
e98f42bc | 42 | static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf, |
89d94756 HR |
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 | /** | |
e98f42bc DLM |
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. | |
89d94756 HR |
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 | ||
e98f42bc DLM |
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 | */ | |
89d94756 HR |
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 | ||
e98f42bc DLM |
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 | */ | |
89d94756 HR |
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 | ||
e98f42bc DLM |
223 | /** |
224 | * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors. | |
225 | * @sdkp: The target disk | |
226 | */ | |
89d94756 HR |
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 | ||
e98f42bc DLM |
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 | */ | |
89d94756 HR |
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 | ||
e98f42bc DLM |
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 | */ | |
89d94756 HR |
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); | |
89d94756 HR |
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 | ||
89d94756 HR |
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 | ||
e98f42bc DLM |
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 | */ | |
a90dfdc2 | 298 | int sd_zbc_write_lock_zone(struct scsi_cmnd *cmd) |
89d94756 HR |
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 | |
e98f42bc | 320 | * path in the non scsi-mq case. |
89d94756 HR |
321 | */ |
322 | if (sdkp->zones_wlock && | |
323 | test_and_set_bit(zno, sdkp->zones_wlock)) | |
324 | return BLKPREP_DEFER; | |
325 | ||
70e42fd0 DLM |
326 | WARN_ON_ONCE(cmd->flags & SCMD_ZONE_WRITE_LOCK); |
327 | cmd->flags |= SCMD_ZONE_WRITE_LOCK; | |
328 | ||
89d94756 HR |
329 | return BLKPREP_OK; |
330 | } | |
331 | ||
e98f42bc DLM |
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 | */ | |
a90dfdc2 | 339 | void sd_zbc_write_unlock_zone(struct scsi_cmnd *cmd) |
89d94756 | 340 | { |
a90dfdc2 | 341 | struct request *rq = cmd->request; |
89d94756 HR |
342 | struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); |
343 | ||
70e42fd0 | 344 | if (sdkp->zones_wlock && cmd->flags & SCMD_ZONE_WRITE_LOCK) { |
89d94756 HR |
345 | unsigned int zno = sd_zbc_zone_no(sdkp, blk_rq_pos(rq)); |
346 | WARN_ON_ONCE(!test_bit(zno, sdkp->zones_wlock)); | |
70e42fd0 | 347 | cmd->flags &= ~SCMD_ZONE_WRITE_LOCK; |
89d94756 HR |
348 | clear_bit_unlock(zno, sdkp->zones_wlock); |
349 | smp_mb__after_atomic(); | |
350 | } | |
351 | } | |
352 | ||
e98f42bc DLM |
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, | |
89d94756 HR |
363 | struct scsi_sense_hdr *sshdr) |
364 | { | |
365 | int result = cmd->result; | |
366 | struct request *rq = cmd->request; | |
367 | ||
368 | switch (req_op(rq)) { | |
868ed5a5 DLM |
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 | ||
89d94756 | 382 | case REQ_OP_WRITE: |
02d26103 | 383 | case REQ_OP_WRITE_ZEROES: |
89d94756 | 384 | case REQ_OP_WRITE_SAME: |
89d94756 | 385 | |
868ed5a5 DLM |
386 | if (result && |
387 | sshdr->sense_key == ILLEGAL_REQUEST && | |
388 | sshdr->asc == 0x21) | |
89d94756 HR |
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; | |
89d94756 HR |
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 | /** | |
e98f42bc DLM |
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. | |
89d94756 HR |
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; | |
4a109032 DLM |
426 | sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]); |
427 | sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]); | |
89d94756 HR |
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; | |
4a109032 | 434 | sdkp->zones_max_open = get_unaligned_be32(&buf[16]); |
89d94756 HR |
435 | } |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
440 | /** | |
e98f42bc DLM |
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. | |
89d94756 | 449 | */ |
e98f42bc | 450 | static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf) |
89d94756 HR |
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 | ||
e8c77ec4 | 478 | #define SD_ZBC_BUF_SIZE 131072U |
89d94756 | 479 | |
e98f42bc DLM |
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. | |
2d0bd9e8 | 486 | * |
c68a75f6 DLM |
487 | * Returns the zone size in number of blocks upon success or an error code |
488 | * upon failure. | |
e98f42bc | 489 | */ |
2d0bd9e8 | 490 | static s64 sd_zbc_check_zone_size(struct scsi_disk *sdkp) |
89d94756 | 491 | { |
eda1d157 | 492 | u64 zone_blocks = 0; |
89d94756 HR |
493 | sector_t block = 0; |
494 | unsigned char *buf; | |
495 | unsigned char *rec; | |
496 | unsigned int buf_len; | |
497 | unsigned int list_length; | |
c68a75f6 | 498 | s64 ret; |
89d94756 HR |
499 | u8 same; |
500 | ||
89d94756 HR |
501 | /* Get a buffer */ |
502 | buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL); | |
503 | if (!buf) | |
504 | return -ENOMEM; | |
505 | ||
506 | /* Do a report zone to get the same field */ | |
507 | ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0); | |
eda1d157 DLM |
508 | if (ret) |
509 | goto out_free; | |
89d94756 HR |
510 | |
511 | same = buf[4] & 0x0f; | |
512 | if (same > 0) { | |
513 | rec = &buf[64]; | |
514 | zone_blocks = get_unaligned_be64(&rec[8]); | |
515 | goto out; | |
516 | } | |
517 | ||
518 | /* | |
519 | * Check the size of all zones: all zones must be of | |
520 | * equal size, except the last zone which can be smaller | |
521 | * than other zones. | |
522 | */ | |
523 | do { | |
524 | ||
525 | /* Parse REPORT ZONES header */ | |
526 | list_length = get_unaligned_be32(&buf[0]) + 64; | |
527 | rec = buf + 64; | |
e8c77ec4 | 528 | buf_len = min(list_length, SD_ZBC_BUF_SIZE); |
89d94756 HR |
529 | |
530 | /* Parse zone descriptors */ | |
531 | while (rec < buf + buf_len) { | |
2d0bd9e8 BVA |
532 | u64 this_zone_blocks = get_unaligned_be64(&rec[8]); |
533 | ||
534 | if (zone_blocks == 0) { | |
535 | zone_blocks = this_zone_blocks; | |
536 | } else if (this_zone_blocks != zone_blocks && | |
537 | (block + this_zone_blocks < sdkp->capacity | |
538 | || this_zone_blocks > zone_blocks)) { | |
89d94756 HR |
539 | zone_blocks = 0; |
540 | goto out; | |
541 | } | |
2d0bd9e8 | 542 | block += this_zone_blocks; |
89d94756 HR |
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) | |
eda1d157 | 550 | goto out_free; |
89d94756 HR |
551 | } |
552 | ||
553 | } while (block < sdkp->capacity); | |
554 | ||
89d94756 | 555 | out: |
89d94756 HR |
556 | if (!zone_blocks) { |
557 | if (sdkp->first_scan) | |
558 | sd_printk(KERN_NOTICE, sdkp, | |
559 | "Devices with non constant zone " | |
560 | "size are not supported\n"); | |
eda1d157 DLM |
561 | ret = -ENODEV; |
562 | } else if (!is_power_of_2(zone_blocks)) { | |
89d94756 HR |
563 | if (sdkp->first_scan) |
564 | sd_printk(KERN_NOTICE, sdkp, | |
565 | "Devices with non power of 2 zone " | |
566 | "size are not supported\n"); | |
eda1d157 DLM |
567 | ret = -ENODEV; |
568 | } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) { | |
89d94756 HR |
569 | if (sdkp->first_scan) |
570 | sd_printk(KERN_NOTICE, sdkp, | |
571 | "Zone size too large\n"); | |
eda1d157 DLM |
572 | ret = -ENODEV; |
573 | } else { | |
2d0bd9e8 | 574 | ret = zone_blocks; |
89d94756 HR |
575 | } |
576 | ||
eda1d157 DLM |
577 | out_free: |
578 | kfree(buf); | |
89d94756 | 579 | |
eda1d157 | 580 | return ret; |
89d94756 HR |
581 | } |
582 | ||
2d0bd9e8 | 583 | static int sd_zbc_setup(struct scsi_disk *sdkp, u32 zone_blocks) |
89d94756 | 584 | { |
2d0bd9e8 BVA |
585 | struct request_queue *q = sdkp->disk->queue; |
586 | u32 zone_shift = ilog2(zone_blocks); | |
587 | u32 nr_zones; | |
5eed92d1 | 588 | |
89d94756 | 589 | /* chunk_sectors indicates the zone size */ |
2d0bd9e8 BVA |
590 | blk_queue_chunk_sectors(q, |
591 | logical_to_sectors(sdkp->device, zone_blocks)); | |
592 | nr_zones = round_up(sdkp->capacity, zone_blocks) >> zone_shift; | |
593 | ||
594 | /* | |
595 | * Initialize the disk zone write lock bitmap if the number | |
596 | * of zones changed. | |
597 | */ | |
598 | if (nr_zones != sdkp->nr_zones) { | |
599 | unsigned long *zones_wlock = NULL; | |
600 | ||
601 | if (nr_zones) { | |
602 | zones_wlock = kcalloc(BITS_TO_LONGS(nr_zones), | |
603 | sizeof(unsigned long), | |
604 | GFP_KERNEL); | |
605 | if (!zones_wlock) | |
606 | return -ENOMEM; | |
607 | } | |
608 | ||
609 | blk_mq_freeze_queue(q); | |
610 | sdkp->zone_blocks = zone_blocks; | |
611 | sdkp->zone_shift = zone_shift; | |
612 | sdkp->nr_zones = nr_zones; | |
613 | swap(sdkp->zones_wlock, zones_wlock); | |
614 | blk_mq_unfreeze_queue(q); | |
615 | ||
616 | kfree(zones_wlock); | |
617 | ||
618 | /* READ16/WRITE16 is mandatory for ZBC disks */ | |
619 | sdkp->device->use_16_for_rw = 1; | |
620 | sdkp->device->use_10_for_rw = 0; | |
89d94756 HR |
621 | } |
622 | ||
623 | return 0; | |
624 | } | |
625 | ||
e98f42bc | 626 | int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf) |
89d94756 | 627 | { |
2d0bd9e8 | 628 | int64_t zone_blocks; |
f7053240 | 629 | int ret; |
89d94756 HR |
630 | |
631 | if (!sd_is_zoned(sdkp)) | |
632 | /* | |
633 | * Device managed or normal SCSI disk, | |
634 | * no special handling required | |
635 | */ | |
636 | return 0; | |
637 | ||
89d94756 HR |
638 | /* Get zoned block device characteristics */ |
639 | ret = sd_zbc_read_zoned_characteristics(sdkp, buf); | |
640 | if (ret) | |
641 | goto err; | |
642 | ||
643 | /* | |
644 | * Check for unconstrained reads: host-managed devices with | |
645 | * constrained reads (drives failing read after write pointer) | |
646 | * are not supported. | |
647 | */ | |
648 | if (!sdkp->urswrz) { | |
649 | if (sdkp->first_scan) | |
650 | sd_printk(KERN_NOTICE, sdkp, | |
651 | "constrained reads devices are not supported\n"); | |
652 | ret = -ENODEV; | |
653 | goto err; | |
654 | } | |
655 | ||
656 | /* Check capacity */ | |
657 | ret = sd_zbc_check_capacity(sdkp, buf); | |
658 | if (ret) | |
659 | goto err; | |
89d94756 HR |
660 | |
661 | /* | |
662 | * Check zone size: only devices with a constant zone size (except | |
663 | * an eventual last runt zone) that is a power of 2 are supported. | |
664 | */ | |
2d0bd9e8 BVA |
665 | zone_blocks = sd_zbc_check_zone_size(sdkp); |
666 | ret = -EFBIG; | |
667 | if (zone_blocks != (u32)zone_blocks) | |
668 | goto err; | |
669 | ret = zone_blocks; | |
670 | if (ret < 0) | |
89d94756 HR |
671 | goto err; |
672 | ||
673 | /* The drive satisfies the kernel restrictions: set it up */ | |
2d0bd9e8 | 674 | ret = sd_zbc_setup(sdkp, zone_blocks); |
89d94756 HR |
675 | if (ret) |
676 | goto err; | |
677 | ||
678 | return 0; | |
679 | ||
680 | err: | |
681 | sdkp->capacity = 0; | |
682 | ||
683 | return ret; | |
684 | } | |
685 | ||
686 | void sd_zbc_remove(struct scsi_disk *sdkp) | |
687 | { | |
688 | kfree(sdkp->zones_wlock); | |
689 | sdkp->zones_wlock = NULL; | |
2d0bd9e8 | 690 | sdkp->nr_zones = 0; |
89d94756 HR |
691 | } |
692 | ||
693 | void sd_zbc_print_zones(struct scsi_disk *sdkp) | |
694 | { | |
695 | if (!sd_is_zoned(sdkp) || !sdkp->capacity) | |
696 | return; | |
697 | ||
698 | if (sdkp->capacity & (sdkp->zone_blocks - 1)) | |
699 | sd_printk(KERN_NOTICE, sdkp, | |
700 | "%u zones of %u logical blocks + 1 runt zone\n", | |
701 | sdkp->nr_zones - 1, | |
702 | sdkp->zone_blocks); | |
703 | else | |
704 | sd_printk(KERN_NOTICE, sdkp, | |
705 | "%u zones of %u logical blocks\n", | |
706 | sdkp->nr_zones, | |
707 | sdkp->zone_blocks); | |
708 | } |