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