blob: ea9e1e0ed5b8502be262940459d3cad7d4ec0392 [file] [log] [blame]
Hannes Reinecke89d94752016-10-18 15:40:34 +09001/*
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>
31#include <scsi/scsi_dbg.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_driver.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_eh.h>
36
37#include "sd.h"
38#include "scsi_priv.h"
39
40enum zbc_zone_type {
41 ZBC_ZONE_TYPE_CONV = 0x1,
42 ZBC_ZONE_TYPE_SEQWRITE_REQ,
43 ZBC_ZONE_TYPE_SEQWRITE_PREF,
44 ZBC_ZONE_TYPE_RESERVED,
45};
46
47enum zbc_zone_cond {
48 ZBC_ZONE_COND_NO_WP,
49 ZBC_ZONE_COND_EMPTY,
50 ZBC_ZONE_COND_IMP_OPEN,
51 ZBC_ZONE_COND_EXP_OPEN,
52 ZBC_ZONE_COND_CLOSED,
53 ZBC_ZONE_COND_READONLY = 0xd,
54 ZBC_ZONE_COND_FULL,
55 ZBC_ZONE_COND_OFFLINE,
56};
57
58/**
59 * Convert a zone descriptor to a zone struct.
60 */
61static void sd_zbc_parse_report(struct scsi_disk *sdkp,
62 u8 *buf,
63 struct blk_zone *zone)
64{
65 struct scsi_device *sdp = sdkp->device;
66
67 memset(zone, 0, sizeof(struct blk_zone));
68
69 zone->type = buf[0] & 0x0f;
70 zone->cond = (buf[1] >> 4) & 0xf;
71 if (buf[1] & 0x01)
72 zone->reset = 1;
73 if (buf[1] & 0x02)
74 zone->non_seq = 1;
75
76 zone->len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
77 zone->start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
78 zone->wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
79 if (zone->type != ZBC_ZONE_TYPE_CONV &&
80 zone->cond == ZBC_ZONE_COND_FULL)
81 zone->wp = zone->start + zone->len;
82}
83
84/**
85 * Issue a REPORT ZONES scsi command.
86 */
87static int sd_zbc_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
88 unsigned int buflen, sector_t lba)
89{
90 struct scsi_device *sdp = sdkp->device;
91 const int timeout = sdp->request_queue->rq_timeout;
92 struct scsi_sense_hdr sshdr;
93 unsigned char cmd[16];
94 unsigned int rep_len;
95 int result;
96
97 memset(cmd, 0, 16);
98 cmd[0] = ZBC_IN;
99 cmd[1] = ZI_REPORT_ZONES;
100 put_unaligned_be64(lba, &cmd[2]);
101 put_unaligned_be32(buflen, &cmd[10]);
102 memset(buf, 0, buflen);
103
104 result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
105 buf, buflen, &sshdr,
106 timeout, SD_MAX_RETRIES, NULL);
107 if (result) {
108 sd_printk(KERN_ERR, sdkp,
109 "REPORT ZONES lba %llu failed with %d/%d\n",
110 (unsigned long long)lba,
111 host_byte(result), driver_byte(result));
112 return -EIO;
113 }
114
115 rep_len = get_unaligned_be32(&buf[0]);
116 if (rep_len < 64) {
117 sd_printk(KERN_ERR, sdkp,
118 "REPORT ZONES report invalid length %u\n",
119 rep_len);
120 return -EIO;
121 }
122
123 return 0;
124}
125
126int sd_zbc_setup_report_cmnd(struct scsi_cmnd *cmd)
127{
128 struct request *rq = cmd->request;
129 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
130 sector_t lba, sector = blk_rq_pos(rq);
131 unsigned int nr_bytes = blk_rq_bytes(rq);
132 int ret;
133
134 WARN_ON(nr_bytes == 0);
135
136 if (!sd_is_zoned(sdkp))
137 /* Not a zoned device */
138 return BLKPREP_KILL;
139
140 ret = scsi_init_io(cmd);
141 if (ret != BLKPREP_OK)
142 return ret;
143
144 cmd->cmd_len = 16;
145 memset(cmd->cmnd, 0, cmd->cmd_len);
146 cmd->cmnd[0] = ZBC_IN;
147 cmd->cmnd[1] = ZI_REPORT_ZONES;
148 lba = sectors_to_logical(sdkp->device, sector);
149 put_unaligned_be64(lba, &cmd->cmnd[2]);
150 put_unaligned_be32(nr_bytes, &cmd->cmnd[10]);
151 /* Do partial report for speeding things up */
152 cmd->cmnd[14] = ZBC_REPORT_ZONE_PARTIAL;
153
154 cmd->sc_data_direction = DMA_FROM_DEVICE;
155 cmd->sdb.length = nr_bytes;
156 cmd->transfersize = sdkp->device->sector_size;
157 cmd->allowed = 0;
158
159 /*
160 * Report may return less bytes than requested. Make sure
161 * to report completion on the entire initial request.
162 */
163 rq->__data_len = nr_bytes;
164
165 return BLKPREP_OK;
166}
167
168static 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
223static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
224{
225 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
226}
227
228static inline unsigned int sd_zbc_zone_no(struct scsi_disk *sdkp,
229 sector_t sector)
230{
231 return sectors_to_logical(sdkp->device, sector) >> sdkp->zone_shift;
232}
233
234int sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd)
235{
236 struct request *rq = cmd->request;
237 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
238 sector_t sector = blk_rq_pos(rq);
239 sector_t block = sectors_to_logical(sdkp->device, sector);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900240
241 if (!sd_is_zoned(sdkp))
242 /* Not a zoned device */
243 return BLKPREP_KILL;
244
245 if (sdkp->device->changed)
246 return BLKPREP_KILL;
247
248 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
249 /* Unaligned request */
250 return BLKPREP_KILL;
251
Hannes Reinecke89d94752016-10-18 15:40:34 +0900252 cmd->cmd_len = 16;
253 memset(cmd->cmnd, 0, cmd->cmd_len);
254 cmd->cmnd[0] = ZBC_OUT;
255 cmd->cmnd[1] = ZO_RESET_WRITE_POINTER;
256 put_unaligned_be64(block, &cmd->cmnd[2]);
257
258 rq->timeout = SD_TIMEOUT;
259 cmd->sc_data_direction = DMA_NONE;
260 cmd->transfersize = 0;
261 cmd->allowed = 0;
262
263 return BLKPREP_OK;
264}
265
Damien Le Moala90dfdc2017-04-24 16:51:13 +0900266int sd_zbc_write_lock_zone(struct scsi_cmnd *cmd)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900267{
268 struct request *rq = cmd->request;
269 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
270 sector_t sector = blk_rq_pos(rq);
271 sector_t zone_sectors = sd_zbc_zone_sectors(sdkp);
272 unsigned int zno = sd_zbc_zone_no(sdkp, sector);
273
274 /*
275 * Note: Checks of the alignment of the write command on
276 * logical blocks is done in sd.c
277 */
278
279 /* Do not allow zone boundaries crossing on host-managed drives */
280 if (blk_queue_zoned_model(sdkp->disk->queue) == BLK_ZONED_HM &&
281 (sector & (zone_sectors - 1)) + blk_rq_sectors(rq) > zone_sectors)
282 return BLKPREP_KILL;
283
284 /*
285 * Do not issue more than one write at a time per
286 * zone. This solves write ordering problems due to
287 * the unlocking of the request queue in the dispatch
288 * path in the non scsi-mq case. For scsi-mq, this
289 * also avoids potential write reordering when multiple
290 * threads running on different CPUs write to the same
291 * zone (with a synchronized sequential pattern).
292 */
293 if (sdkp->zones_wlock &&
294 test_and_set_bit(zno, sdkp->zones_wlock))
295 return BLKPREP_DEFER;
296
Damien Le Moal70e42fd2017-08-09 12:00:22 +0900297 WARN_ON_ONCE(cmd->flags & SCMD_ZONE_WRITE_LOCK);
298 cmd->flags |= SCMD_ZONE_WRITE_LOCK;
299
Hannes Reinecke89d94752016-10-18 15:40:34 +0900300 return BLKPREP_OK;
301}
302
Damien Le Moala90dfdc2017-04-24 16:51:13 +0900303void sd_zbc_write_unlock_zone(struct scsi_cmnd *cmd)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900304{
Damien Le Moala90dfdc2017-04-24 16:51:13 +0900305 struct request *rq = cmd->request;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900306 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
307
Damien Le Moal70e42fd2017-08-09 12:00:22 +0900308 if (sdkp->zones_wlock && cmd->flags & SCMD_ZONE_WRITE_LOCK) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900309 unsigned int zno = sd_zbc_zone_no(sdkp, blk_rq_pos(rq));
310 WARN_ON_ONCE(!test_bit(zno, sdkp->zones_wlock));
Damien Le Moal70e42fd2017-08-09 12:00:22 +0900311 cmd->flags &= ~SCMD_ZONE_WRITE_LOCK;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900312 clear_bit_unlock(zno, sdkp->zones_wlock);
313 smp_mb__after_atomic();
314 }
315}
316
Hannes Reinecke89d94752016-10-18 15:40:34 +0900317void sd_zbc_complete(struct scsi_cmnd *cmd,
318 unsigned int good_bytes,
319 struct scsi_sense_hdr *sshdr)
320{
321 int result = cmd->result;
322 struct request *rq = cmd->request;
323
324 switch (req_op(rq)) {
Damien Le Moal868ed5a2017-04-24 16:51:15 +0900325 case REQ_OP_ZONE_RESET:
326
327 if (result &&
328 sshdr->sense_key == ILLEGAL_REQUEST &&
329 sshdr->asc == 0x24)
330 /*
331 * INVALID FIELD IN CDB error: reset of a conventional
332 * zone was attempted. Nothing to worry about, so be
333 * quiet about the error.
334 */
335 rq->rq_flags |= RQF_QUIET;
336 break;
337
Hannes Reinecke89d94752016-10-18 15:40:34 +0900338 case REQ_OP_WRITE:
Christoph Hellwig02d26102017-04-05 19:21:02 +0200339 case REQ_OP_WRITE_ZEROES:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900340 case REQ_OP_WRITE_SAME:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900341
Damien Le Moal868ed5a2017-04-24 16:51:15 +0900342 if (result &&
343 sshdr->sense_key == ILLEGAL_REQUEST &&
344 sshdr->asc == 0x21)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900345 /*
346 * INVALID ADDRESS FOR WRITE error: It is unlikely that
347 * retrying write requests failed with any kind of
348 * alignement error will result in success. So don't.
349 */
350 cmd->allowed = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900351 break;
352
353 case REQ_OP_ZONE_REPORT:
354
355 if (!result)
356 sd_zbc_report_zones_complete(cmd, good_bytes);
357 break;
358
359 }
360}
361
362/**
363 * Read zoned block device characteristics (VPD page B6).
364 */
365static int sd_zbc_read_zoned_characteristics(struct scsi_disk *sdkp,
366 unsigned char *buf)
367{
368
369 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
370 sd_printk(KERN_NOTICE, sdkp,
371 "Unconstrained-read check failed\n");
372 return -ENODEV;
373 }
374
375 if (sdkp->device->type != TYPE_ZBC) {
376 /* Host-aware */
377 sdkp->urswrz = 1;
Damien Le Moal49414382017-10-11 05:54:25 +0900378 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
379 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900380 sdkp->zones_max_open = 0;
381 } else {
382 /* Host-managed */
383 sdkp->urswrz = buf[4] & 1;
384 sdkp->zones_optimal_open = 0;
385 sdkp->zones_optimal_nonseq = 0;
Damien Le Moal49414382017-10-11 05:54:25 +0900386 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900387 }
388
389 return 0;
390}
391
392/**
393 * Check reported capacity.
394 */
395static int sd_zbc_check_capacity(struct scsi_disk *sdkp,
396 unsigned char *buf)
397{
398 sector_t lba;
399 int ret;
400
401 if (sdkp->rc_basis != 0)
402 return 0;
403
404 /* Do a report zone to get the maximum LBA to check capacity */
405 ret = sd_zbc_report_zones(sdkp, buf, SD_BUF_SIZE, 0);
406 if (ret)
407 return ret;
408
409 /* The max_lba field is the capacity of this device */
410 lba = get_unaligned_be64(&buf[8]);
411 if (lba + 1 == sdkp->capacity)
412 return 0;
413
414 if (sdkp->first_scan)
415 sd_printk(KERN_WARNING, sdkp,
416 "Changing capacity from %llu to max LBA+1 %llu\n",
417 (unsigned long long)sdkp->capacity,
418 (unsigned long long)lba + 1);
419 sdkp->capacity = lba + 1;
420
421 return 0;
422}
423
424#define SD_ZBC_BUF_SIZE 131072
425
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700426/**
427 * sd_zbc_check_zone_size - Check the device zone sizes
428 * @sdkp: Target disk
429 *
430 * Check that all zones of the device are equal. The last zone can however
431 * be smaller. The zone size must also be a power of two number of LBAs.
432 *
433 * Returns the zone size in bytes upon success or an error code upon failure.
434 */
435static s64 sd_zbc_check_zone_size(struct scsi_disk *sdkp)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900436{
Damien Le Moal11106362018-03-02 07:19:28 +0900437 u64 zone_blocks = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900438 sector_t block = 0;
439 unsigned char *buf;
440 unsigned char *rec;
441 unsigned int buf_len;
442 unsigned int list_length;
443 int ret;
444 u8 same;
445
Hannes Reinecke89d94752016-10-18 15:40:34 +0900446 /* Get a buffer */
447 buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
448 if (!buf)
449 return -ENOMEM;
450
451 /* Do a report zone to get the same field */
452 ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0);
Damien Le Moal11106362018-03-02 07:19:28 +0900453 if (ret)
454 goto out_free;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900455
456 same = buf[4] & 0x0f;
457 if (same > 0) {
458 rec = &buf[64];
459 zone_blocks = get_unaligned_be64(&rec[8]);
460 goto out;
461 }
462
463 /*
464 * Check the size of all zones: all zones must be of
465 * equal size, except the last zone which can be smaller
466 * than other zones.
467 */
468 do {
469
470 /* Parse REPORT ZONES header */
471 list_length = get_unaligned_be32(&buf[0]) + 64;
472 rec = buf + 64;
473 if (list_length < SD_ZBC_BUF_SIZE)
474 buf_len = list_length;
475 else
476 buf_len = SD_ZBC_BUF_SIZE;
477
478 /* Parse zone descriptors */
479 while (rec < buf + buf_len) {
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700480 u64 this_zone_blocks = get_unaligned_be64(&rec[8]);
481
482 if (zone_blocks == 0) {
483 zone_blocks = this_zone_blocks;
484 } else if (this_zone_blocks != zone_blocks &&
485 (block + this_zone_blocks < sdkp->capacity
486 || this_zone_blocks > zone_blocks)) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900487 zone_blocks = 0;
488 goto out;
489 }
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700490 block += this_zone_blocks;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900491 rec += 64;
492 }
493
494 if (block < sdkp->capacity) {
495 ret = sd_zbc_report_zones(sdkp, buf,
496 SD_ZBC_BUF_SIZE, block);
497 if (ret)
Damien Le Moal11106362018-03-02 07:19:28 +0900498 goto out_free;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900499 }
500
501 } while (block < sdkp->capacity);
502
Hannes Reinecke89d94752016-10-18 15:40:34 +0900503out:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900504 if (!zone_blocks) {
505 if (sdkp->first_scan)
506 sd_printk(KERN_NOTICE, sdkp,
507 "Devices with non constant zone "
508 "size are not supported\n");
Damien Le Moal11106362018-03-02 07:19:28 +0900509 ret = -ENODEV;
510 } else if (!is_power_of_2(zone_blocks)) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900511 if (sdkp->first_scan)
512 sd_printk(KERN_NOTICE, sdkp,
513 "Devices with non power of 2 zone "
514 "size are not supported\n");
Damien Le Moal11106362018-03-02 07:19:28 +0900515 ret = -ENODEV;
516 } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900517 if (sdkp->first_scan)
518 sd_printk(KERN_NOTICE, sdkp,
519 "Zone size too large\n");
Damien Le Moal11106362018-03-02 07:19:28 +0900520 ret = -ENODEV;
521 } else {
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700522 ret = zone_blocks;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900523 }
524
Damien Le Moal11106362018-03-02 07:19:28 +0900525out_free:
526 kfree(buf);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900527
Damien Le Moal11106362018-03-02 07:19:28 +0900528 return ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900529}
530
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700531static int sd_zbc_setup(struct scsi_disk *sdkp, u32 zone_blocks)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900532{
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700533 struct request_queue *q = sdkp->disk->queue;
534 u32 zone_shift = ilog2(zone_blocks);
535 u32 nr_zones;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900536
537 /* chunk_sectors indicates the zone size */
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700538 blk_queue_chunk_sectors(q,
539 logical_to_sectors(sdkp->device, zone_blocks));
540 nr_zones = round_up(sdkp->capacity, zone_blocks) >> zone_shift;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900541
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700542 /*
543 * Initialize the disk zone write lock bitmap if the number
544 * of zones changed.
545 */
546 if (nr_zones != sdkp->nr_zones) {
547 unsigned long *zones_wlock = NULL;
548
549 if (nr_zones) {
550 zones_wlock = kcalloc(BITS_TO_LONGS(nr_zones),
551 sizeof(unsigned long),
552 GFP_KERNEL);
553 if (!zones_wlock)
554 return -ENOMEM;
555 }
556
557 blk_mq_freeze_queue(q);
558 sdkp->zone_blocks = zone_blocks;
559 sdkp->zone_shift = zone_shift;
560 sdkp->nr_zones = nr_zones;
561 swap(sdkp->zones_wlock, zones_wlock);
562 blk_mq_unfreeze_queue(q);
563
564 kfree(zones_wlock);
565
566 /* READ16/WRITE16 is mandatory for ZBC disks */
567 sdkp->device->use_16_for_rw = 1;
568 sdkp->device->use_10_for_rw = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900569 }
570
571 return 0;
572}
573
574int sd_zbc_read_zones(struct scsi_disk *sdkp,
575 unsigned char *buf)
576{
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700577 int64_t zone_blocks;
Bart Van Asschef7053242017-04-24 16:51:14 +0900578 int ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900579
580 if (!sd_is_zoned(sdkp))
581 /*
582 * Device managed or normal SCSI disk,
583 * no special handling required
584 */
585 return 0;
586
587
588 /* Get zoned block device characteristics */
589 ret = sd_zbc_read_zoned_characteristics(sdkp, buf);
590 if (ret)
591 goto err;
592
593 /*
594 * Check for unconstrained reads: host-managed devices with
595 * constrained reads (drives failing read after write pointer)
596 * are not supported.
597 */
598 if (!sdkp->urswrz) {
599 if (sdkp->first_scan)
600 sd_printk(KERN_NOTICE, sdkp,
601 "constrained reads devices are not supported\n");
602 ret = -ENODEV;
603 goto err;
604 }
605
606 /* Check capacity */
607 ret = sd_zbc_check_capacity(sdkp, buf);
608 if (ret)
609 goto err;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900610
611 /*
612 * Check zone size: only devices with a constant zone size (except
613 * an eventual last runt zone) that is a power of 2 are supported.
614 */
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700615 zone_blocks = sd_zbc_check_zone_size(sdkp);
616 ret = -EFBIG;
617 if (zone_blocks != (u32)zone_blocks)
618 goto err;
619 ret = zone_blocks;
620 if (ret < 0)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900621 goto err;
622
623 /* The drive satisfies the kernel restrictions: set it up */
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700624 ret = sd_zbc_setup(sdkp, zone_blocks);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900625 if (ret)
626 goto err;
627
Hannes Reinecke89d94752016-10-18 15:40:34 +0900628 return 0;
629
630err:
631 sdkp->capacity = 0;
632
633 return ret;
634}
635
636void sd_zbc_remove(struct scsi_disk *sdkp)
637{
638 kfree(sdkp->zones_wlock);
639 sdkp->zones_wlock = NULL;
Bart Van Assche7cb10a42018-04-16 18:04:41 -0700640 sdkp->nr_zones = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900641}
642
643void sd_zbc_print_zones(struct scsi_disk *sdkp)
644{
645 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
646 return;
647
648 if (sdkp->capacity & (sdkp->zone_blocks - 1))
649 sd_printk(KERN_NOTICE, sdkp,
650 "%u zones of %u logical blocks + 1 runt zone\n",
651 sdkp->nr_zones - 1,
652 sdkp->zone_blocks);
653 else
654 sd_printk(KERN_NOTICE, sdkp,
655 "%u zones of %u logical blocks\n",
656 sdkp->nr_zones,
657 sdkp->zone_blocks);
658}