interconnect: qcom: Fix Kconfig indentation
[linux/fpc-iii.git] / drivers / scsi / sd_zbc.c
blob27d72c1d46546f1334d9879bca288d558756c45b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SCSI Zoned Block commands
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>
9 */
11 #include <linux/blkdev.h>
12 #include <linux/vmalloc.h>
13 #include <linux/sched/mm.h>
15 #include <asm/unaligned.h>
17 #include <scsi/scsi.h>
18 #include <scsi/scsi_cmnd.h>
20 #include "sd.h"
22 static int sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
23 unsigned int idx, report_zones_cb cb, void *data)
25 struct scsi_device *sdp = sdkp->device;
26 struct blk_zone zone = { 0 };
28 zone.type = buf[0] & 0x0f;
29 zone.cond = (buf[1] >> 4) & 0xf;
30 if (buf[1] & 0x01)
31 zone.reset = 1;
32 if (buf[1] & 0x02)
33 zone.non_seq = 1;
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;
42 return cb(&zone, idx, data);
45 /**
46 * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
47 * @sdkp: The target disk
48 * @buf: vmalloc-ed buffer to use for the reply
49 * @buflen: the buffer size
50 * @lba: Start LBA of the report
51 * @partial: Do partial report
53 * For internal use during device validation.
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.
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)
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;
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]);
75 if (partial)
76 cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
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,
83 "REPORT ZONES lba %llu failed with %d/%d\n",
84 (unsigned long long)lba,
85 host_byte(result), driver_byte(result));
86 return -EIO;
89 rep_len = get_unaligned_be32(&buf[0]);
90 if (rep_len < 64) {
91 sd_printk(KERN_ERR, sdkp,
92 "REPORT ZONES report invalid length %u\n",
93 rep_len);
94 return -EIO;
97 return 0;
101 * Allocate a buffer for report zones reply.
102 * @sdkp: The target disk
103 * @nr_zones: Maximum number of zones to report
104 * @buflen: Size of the buffer allocated
106 * Try to allocate a reply buffer for the number of requested zones.
107 * The size of the buffer allocated may be smaller than requested to
108 * satify the device constraint (max_hw_sectors, max_segments, etc).
110 * Return the address of the allocated buffer and update @buflen with
111 * the size of the allocated buffer.
113 static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
114 unsigned int nr_zones, size_t *buflen)
116 struct request_queue *q = sdkp->disk->queue;
117 size_t bufsize;
118 void *buf;
121 * Report zone buffer size should be at most 64B times the number of
122 * zones requested plus the 64B reply header, but should be at least
123 * SECTOR_SIZE for ATA devices.
124 * Make sure that this size does not exceed the hardware capabilities.
125 * Furthermore, since the report zone command cannot be split, make
126 * sure that the allocated buffer can always be mapped by limiting the
127 * number of pages allocated to the HBA max segments limit.
129 nr_zones = min(nr_zones, sdkp->nr_zones);
130 bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE);
131 bufsize = min_t(size_t, bufsize,
132 queue_max_hw_sectors(q) << SECTOR_SHIFT);
133 bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
135 while (bufsize >= SECTOR_SIZE) {
136 buf = __vmalloc(bufsize,
137 GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY,
138 PAGE_KERNEL);
139 if (buf) {
140 *buflen = bufsize;
141 return buf;
143 bufsize >>= 1;
146 return NULL;
150 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
151 * @sdkp: The target disk
153 static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
155 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
158 int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
159 unsigned int nr_zones, report_zones_cb cb, void *data)
161 struct scsi_disk *sdkp = scsi_disk(disk);
162 unsigned int nr, i;
163 unsigned char *buf;
164 size_t offset, buflen = 0;
165 int zone_idx = 0;
166 int ret;
168 if (!sd_is_zoned(sdkp))
169 /* Not a zoned device */
170 return -EOPNOTSUPP;
172 buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen);
173 if (!buf)
174 return -ENOMEM;
176 while (zone_idx < nr_zones && sector < get_capacity(disk)) {
177 ret = sd_zbc_do_report_zones(sdkp, buf, buflen,
178 sectors_to_logical(sdkp->device, sector), true);
179 if (ret)
180 goto out;
182 offset = 0;
183 nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64);
184 if (!nr)
185 break;
187 for (i = 0; i < nr && zone_idx < nr_zones; i++) {
188 offset += 64;
189 ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx,
190 cb, data);
191 if (ret)
192 goto out;
193 zone_idx++;
196 sector += sd_zbc_zone_sectors(sdkp) * i;
199 ret = zone_idx;
200 out:
201 kvfree(buf);
202 return ret;
206 * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations
207 * can be RESET WRITE POINTER, OPEN, CLOSE or FINISH.
208 * @cmd: the command to setup
209 * @op: Operation to be performed
210 * @all: All zones control
212 * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL,
213 * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests.
215 blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
216 unsigned char op, bool all)
218 struct request *rq = cmd->request;
219 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
220 sector_t sector = blk_rq_pos(rq);
221 sector_t block = sectors_to_logical(sdkp->device, sector);
223 if (!sd_is_zoned(sdkp))
224 /* Not a zoned device */
225 return BLK_STS_IOERR;
227 if (sdkp->device->changed)
228 return BLK_STS_IOERR;
230 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
231 /* Unaligned request */
232 return BLK_STS_IOERR;
234 cmd->cmd_len = 16;
235 memset(cmd->cmnd, 0, cmd->cmd_len);
236 cmd->cmnd[0] = ZBC_OUT;
237 cmd->cmnd[1] = op;
238 if (all)
239 cmd->cmnd[14] = 0x1;
240 else
241 put_unaligned_be64(block, &cmd->cmnd[2]);
243 rq->timeout = SD_TIMEOUT;
244 cmd->sc_data_direction = DMA_NONE;
245 cmd->transfersize = 0;
246 cmd->allowed = 0;
248 return BLK_STS_OK;
252 * sd_zbc_complete - ZBC command post processing.
253 * @cmd: Completed command
254 * @good_bytes: Command reply bytes
255 * @sshdr: command sense header
257 * Called from sd_done(). Process report zones reply and handle reset zone
258 * and write commands errors.
260 void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
261 struct scsi_sense_hdr *sshdr)
263 int result = cmd->result;
264 struct request *rq = cmd->request;
266 if (op_is_zone_mgmt(req_op(rq)) &&
267 result &&
268 sshdr->sense_key == ILLEGAL_REQUEST &&
269 sshdr->asc == 0x24) {
271 * INVALID FIELD IN CDB error: a zone management command was
272 * attempted on a conventional zone. Nothing to worry about,
273 * so be quiet about the error.
275 rq->rq_flags |= RQF_QUIET;
280 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
281 * @sdkp: Target disk
282 * @buf: Buffer where to store the VPD page data
284 * Read VPD page B6, get information and check that reads are unconstrained.
286 static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
287 unsigned char *buf)
290 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
291 sd_printk(KERN_NOTICE, sdkp,
292 "Read zoned characteristics VPD page failed\n");
293 return -ENODEV;
296 if (sdkp->device->type != TYPE_ZBC) {
297 /* Host-aware */
298 sdkp->urswrz = 1;
299 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
300 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
301 sdkp->zones_max_open = 0;
302 } else {
303 /* Host-managed */
304 sdkp->urswrz = buf[4] & 1;
305 sdkp->zones_optimal_open = 0;
306 sdkp->zones_optimal_nonseq = 0;
307 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
311 * Check for unconstrained reads: host-managed devices with
312 * constrained reads (drives failing read after write pointer)
313 * are not supported.
315 if (!sdkp->urswrz) {
316 if (sdkp->first_scan)
317 sd_printk(KERN_NOTICE, sdkp,
318 "constrained reads devices are not supported\n");
319 return -ENODEV;
322 return 0;
326 * sd_zbc_check_zones - Check the device capacity and zone sizes
327 * @sdkp: Target disk
329 * Check that the device capacity as reported by READ CAPACITY matches the
330 * max_lba value (plus one)of the report zones command reply. Also check that
331 * all zones of the device have an equal size, only allowing the last zone of
332 * the disk to have a smaller size (runt zone). The zone size must also be a
333 * power of two.
335 * Returns the zone size in number of blocks upon success or an error code
336 * upon failure.
338 static int sd_zbc_check_zones(struct scsi_disk *sdkp, unsigned char *buf,
339 u32 *zblocks)
341 u64 zone_blocks = 0;
342 sector_t max_lba;
343 unsigned char *rec;
344 int ret;
346 /* Do a report zone to get max_lba and the size of the first zone */
347 ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);
348 if (ret)
349 return ret;
351 if (sdkp->rc_basis == 0) {
352 /* The max_lba field is the capacity of this device */
353 max_lba = get_unaligned_be64(&buf[8]);
354 if (sdkp->capacity != max_lba + 1) {
355 if (sdkp->first_scan)
356 sd_printk(KERN_WARNING, sdkp,
357 "Changing capacity from %llu to max LBA+1 %llu\n",
358 (unsigned long long)sdkp->capacity,
359 (unsigned long long)max_lba + 1);
360 sdkp->capacity = max_lba + 1;
364 /* Parse REPORT ZONES header */
365 rec = buf + 64;
366 zone_blocks = get_unaligned_be64(&rec[8]);
367 if (!zone_blocks || !is_power_of_2(zone_blocks)) {
368 if (sdkp->first_scan)
369 sd_printk(KERN_NOTICE, sdkp,
370 "Devices with non power of 2 zone "
371 "size are not supported\n");
372 return -ENODEV;
375 if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
376 if (sdkp->first_scan)
377 sd_printk(KERN_NOTICE, sdkp,
378 "Zone size too large\n");
379 return -EFBIG;
382 *zblocks = zone_blocks;
384 return 0;
387 int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
389 struct gendisk *disk = sdkp->disk;
390 unsigned int nr_zones;
391 u32 zone_blocks = 0;
392 int ret;
394 if (!sd_is_zoned(sdkp))
396 * Device managed or normal SCSI disk,
397 * no special handling required
399 return 0;
401 /* Check zoned block device characteristics (unconstrained reads) */
402 ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
403 if (ret)
404 goto err;
407 * Check zone size: only devices with a constant zone size (except
408 * an eventual last runt zone) that is a power of 2 are supported.
410 ret = sd_zbc_check_zones(sdkp, buf, &zone_blocks);
411 if (ret != 0)
412 goto err;
414 /* The drive satisfies the kernel restrictions: set it up */
415 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, sdkp->disk->queue);
416 blk_queue_required_elevator_features(sdkp->disk->queue,
417 ELEVATOR_F_ZBD_SEQ_WRITE);
418 nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
420 /* READ16/WRITE16 is mandatory for ZBC disks */
421 sdkp->device->use_16_for_rw = 1;
422 sdkp->device->use_10_for_rw = 0;
425 * Revalidate the disk zone bitmaps once the block device capacity is
426 * set on the second revalidate execution during disk scan and if
427 * something changed when executing a normal revalidate.
429 if (sdkp->first_scan) {
430 sdkp->zone_blocks = zone_blocks;
431 sdkp->nr_zones = nr_zones;
432 return 0;
435 if (sdkp->zone_blocks != zone_blocks ||
436 sdkp->nr_zones != nr_zones ||
437 disk->queue->nr_zones != nr_zones) {
438 ret = blk_revalidate_disk_zones(disk);
439 if (ret != 0)
440 goto err;
441 sdkp->zone_blocks = zone_blocks;
442 sdkp->nr_zones = nr_zones;
445 return 0;
447 err:
448 sdkp->capacity = 0;
450 return ret;
453 void sd_zbc_print_zones(struct scsi_disk *sdkp)
455 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
456 return;
458 if (sdkp->capacity & (sdkp->zone_blocks - 1))
459 sd_printk(KERN_NOTICE, sdkp,
460 "%u zones of %u logical blocks + 1 runt zone\n",
461 sdkp->nr_zones - 1,
462 sdkp->zone_blocks);
463 else
464 sd_printk(KERN_NOTICE, sdkp,
465 "%u zones of %u logical blocks\n",
466 sdkp->nr_zones,
467 sdkp->zone_blocks);