]> git.ipfire.org Git - thirdparty/linux.git/blob - drivers/scsi/sd_zbc.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[thirdparty/linux.git] / drivers / scsi / sd_zbc.c
1 // SPDX-License-Identifier: GPL-2.0-only
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>
9 */
10
11 #include <linux/blkdev.h>
12 #include <linux/vmalloc.h>
13 #include <linux/sched/mm.h>
14
15 #include <asm/unaligned.h>
16
17 #include <scsi/scsi.h>
18 #include <scsi/scsi_cmnd.h>
19
20 #include "sd.h"
21
22 static int sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
23 unsigned int idx, report_zones_cb cb, void *data)
24 {
25 struct scsi_device *sdp = sdkp->device;
26 struct blk_zone zone = { 0 };
27
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;
34
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;
41
42 return cb(&zone, idx, data);
43 }
44
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
52 *
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.
58 */
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)
62 {
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;
69
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;
77
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 start lba %llu failed\n", lba);
84 sd_print_result(sdkp, "REPORT ZONES", result);
85 if (driver_byte(result) == DRIVER_SENSE &&
86 scsi_sense_valid(&sshdr))
87 sd_print_sense_hdr(sdkp, &sshdr);
88 return -EIO;
89 }
90
91 rep_len = get_unaligned_be32(&buf[0]);
92 if (rep_len < 64) {
93 sd_printk(KERN_ERR, sdkp,
94 "REPORT ZONES report invalid length %u\n",
95 rep_len);
96 return -EIO;
97 }
98
99 return 0;
100 }
101
102 /**
103 * Allocate a buffer for report zones reply.
104 * @sdkp: The target disk
105 * @nr_zones: Maximum number of zones to report
106 * @buflen: Size of the buffer allocated
107 *
108 * Try to allocate a reply buffer for the number of requested zones.
109 * The size of the buffer allocated may be smaller than requested to
110 * satify the device constraint (max_hw_sectors, max_segments, etc).
111 *
112 * Return the address of the allocated buffer and update @buflen with
113 * the size of the allocated buffer.
114 */
115 static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
116 unsigned int nr_zones, size_t *buflen)
117 {
118 struct request_queue *q = sdkp->disk->queue;
119 size_t bufsize;
120 void *buf;
121
122 /*
123 * Report zone buffer size should be at most 64B times the number of
124 * zones requested plus the 64B reply header, but should be at least
125 * SECTOR_SIZE for ATA devices.
126 * Make sure that this size does not exceed the hardware capabilities.
127 * Furthermore, since the report zone command cannot be split, make
128 * sure that the allocated buffer can always be mapped by limiting the
129 * number of pages allocated to the HBA max segments limit.
130 */
131 nr_zones = min(nr_zones, sdkp->nr_zones);
132 bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE);
133 bufsize = min_t(size_t, bufsize,
134 queue_max_hw_sectors(q) << SECTOR_SHIFT);
135 bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
136
137 while (bufsize >= SECTOR_SIZE) {
138 buf = __vmalloc(bufsize,
139 GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY,
140 PAGE_KERNEL);
141 if (buf) {
142 *buflen = bufsize;
143 return buf;
144 }
145 bufsize >>= 1;
146 }
147
148 return NULL;
149 }
150
151 /**
152 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
153 * @sdkp: The target disk
154 */
155 static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
156 {
157 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
158 }
159
160 int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
161 unsigned int nr_zones, report_zones_cb cb, void *data)
162 {
163 struct scsi_disk *sdkp = scsi_disk(disk);
164 sector_t capacity = logical_to_sectors(sdkp->device, sdkp->capacity);
165 unsigned int nr, i;
166 unsigned char *buf;
167 size_t offset, buflen = 0;
168 int zone_idx = 0;
169 int ret;
170
171 if (!sd_is_zoned(sdkp))
172 /* Not a zoned device */
173 return -EOPNOTSUPP;
174
175 if (!capacity)
176 /* Device gone or invalid */
177 return -ENODEV;
178
179 buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen);
180 if (!buf)
181 return -ENOMEM;
182
183 while (zone_idx < nr_zones && sector < capacity) {
184 ret = sd_zbc_do_report_zones(sdkp, buf, buflen,
185 sectors_to_logical(sdkp->device, sector), true);
186 if (ret)
187 goto out;
188
189 offset = 0;
190 nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64);
191 if (!nr)
192 break;
193
194 for (i = 0; i < nr && zone_idx < nr_zones; i++) {
195 offset += 64;
196 ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx,
197 cb, data);
198 if (ret)
199 goto out;
200 zone_idx++;
201 }
202
203 sector += sd_zbc_zone_sectors(sdkp) * i;
204 }
205
206 ret = zone_idx;
207 out:
208 kvfree(buf);
209 return ret;
210 }
211
212 /**
213 * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations
214 * can be RESET WRITE POINTER, OPEN, CLOSE or FINISH.
215 * @cmd: the command to setup
216 * @op: Operation to be performed
217 * @all: All zones control
218 *
219 * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL,
220 * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests.
221 */
222 blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
223 unsigned char op, bool all)
224 {
225 struct request *rq = cmd->request;
226 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
227 sector_t sector = blk_rq_pos(rq);
228 sector_t block = sectors_to_logical(sdkp->device, sector);
229
230 if (!sd_is_zoned(sdkp))
231 /* Not a zoned device */
232 return BLK_STS_IOERR;
233
234 if (sdkp->device->changed)
235 return BLK_STS_IOERR;
236
237 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
238 /* Unaligned request */
239 return BLK_STS_IOERR;
240
241 cmd->cmd_len = 16;
242 memset(cmd->cmnd, 0, cmd->cmd_len);
243 cmd->cmnd[0] = ZBC_OUT;
244 cmd->cmnd[1] = op;
245 if (all)
246 cmd->cmnd[14] = 0x1;
247 else
248 put_unaligned_be64(block, &cmd->cmnd[2]);
249
250 rq->timeout = SD_TIMEOUT;
251 cmd->sc_data_direction = DMA_NONE;
252 cmd->transfersize = 0;
253 cmd->allowed = 0;
254
255 return BLK_STS_OK;
256 }
257
258 /**
259 * sd_zbc_complete - ZBC command post processing.
260 * @cmd: Completed command
261 * @good_bytes: Command reply bytes
262 * @sshdr: command sense header
263 *
264 * Called from sd_done(). Process report zones reply and handle reset zone
265 * and write commands errors.
266 */
267 void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
268 struct scsi_sense_hdr *sshdr)
269 {
270 int result = cmd->result;
271 struct request *rq = cmd->request;
272
273 if (op_is_zone_mgmt(req_op(rq)) &&
274 result &&
275 sshdr->sense_key == ILLEGAL_REQUEST &&
276 sshdr->asc == 0x24) {
277 /*
278 * INVALID FIELD IN CDB error: a zone management command was
279 * attempted on a conventional zone. Nothing to worry about,
280 * so be quiet about the error.
281 */
282 rq->rq_flags |= RQF_QUIET;
283 }
284 }
285
286 /**
287 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
288 * @sdkp: Target disk
289 * @buf: Buffer where to store the VPD page data
290 *
291 * Read VPD page B6, get information and check that reads are unconstrained.
292 */
293 static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
294 unsigned char *buf)
295 {
296
297 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
298 sd_printk(KERN_NOTICE, sdkp,
299 "Read zoned characteristics VPD page failed\n");
300 return -ENODEV;
301 }
302
303 if (sdkp->device->type != TYPE_ZBC) {
304 /* Host-aware */
305 sdkp->urswrz = 1;
306 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
307 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
308 sdkp->zones_max_open = 0;
309 } else {
310 /* Host-managed */
311 sdkp->urswrz = buf[4] & 1;
312 sdkp->zones_optimal_open = 0;
313 sdkp->zones_optimal_nonseq = 0;
314 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
315 }
316
317 /*
318 * Check for unconstrained reads: host-managed devices with
319 * constrained reads (drives failing read after write pointer)
320 * are not supported.
321 */
322 if (!sdkp->urswrz) {
323 if (sdkp->first_scan)
324 sd_printk(KERN_NOTICE, sdkp,
325 "constrained reads devices are not supported\n");
326 return -ENODEV;
327 }
328
329 return 0;
330 }
331
332 /**
333 * sd_zbc_check_capacity - Check the device capacity
334 * @sdkp: Target disk
335 * @buf: command buffer
336 * @zblock: zone size in number of blocks
337 *
338 * Get the device zone size and check that the device capacity as reported
339 * by READ CAPACITY matches the max_lba value (plus one) of the report zones
340 * command reply for devices with RC_BASIS == 0.
341 *
342 * Returns 0 upon success or an error code upon failure.
343 */
344 static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
345 u32 *zblocks)
346 {
347 u64 zone_blocks;
348 sector_t max_lba;
349 unsigned char *rec;
350 int ret;
351
352 /* Do a report zone to get max_lba and the size of the first zone */
353 ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);
354 if (ret)
355 return ret;
356
357 if (sdkp->rc_basis == 0) {
358 /* The max_lba field is the capacity of this device */
359 max_lba = get_unaligned_be64(&buf[8]);
360 if (sdkp->capacity != max_lba + 1) {
361 if (sdkp->first_scan)
362 sd_printk(KERN_WARNING, sdkp,
363 "Changing capacity from %llu to max LBA+1 %llu\n",
364 (unsigned long long)sdkp->capacity,
365 (unsigned long long)max_lba + 1);
366 sdkp->capacity = max_lba + 1;
367 }
368 }
369
370 /* Get the size of the first reported zone */
371 rec = buf + 64;
372 zone_blocks = get_unaligned_be64(&rec[8]);
373 if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
374 if (sdkp->first_scan)
375 sd_printk(KERN_NOTICE, sdkp,
376 "Zone size too large\n");
377 return -EFBIG;
378 }
379
380 *zblocks = zone_blocks;
381
382 return 0;
383 }
384
385 int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
386 {
387 struct gendisk *disk = sdkp->disk;
388 unsigned int nr_zones;
389 u32 zone_blocks = 0;
390 int ret;
391
392 if (!sd_is_zoned(sdkp))
393 /*
394 * Device managed or normal SCSI disk,
395 * no special handling required
396 */
397 return 0;
398
399 /* Check zoned block device characteristics (unconstrained reads) */
400 ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
401 if (ret)
402 goto err;
403
404 /* Check the device capacity reported by report zones */
405 ret = sd_zbc_check_capacity(sdkp, buf, &zone_blocks);
406 if (ret != 0)
407 goto err;
408
409 /* The drive satisfies the kernel restrictions: set it up */
410 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, sdkp->disk->queue);
411 blk_queue_required_elevator_features(sdkp->disk->queue,
412 ELEVATOR_F_ZBD_SEQ_WRITE);
413 nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
414
415 /* READ16/WRITE16 is mandatory for ZBC disks */
416 sdkp->device->use_16_for_rw = 1;
417 sdkp->device->use_10_for_rw = 0;
418
419 /*
420 * Revalidate the disk zone bitmaps once the block device capacity is
421 * set on the second revalidate execution during disk scan and if
422 * something changed when executing a normal revalidate.
423 */
424 if (sdkp->first_scan) {
425 sdkp->zone_blocks = zone_blocks;
426 sdkp->nr_zones = nr_zones;
427 return 0;
428 }
429
430 if (sdkp->zone_blocks != zone_blocks ||
431 sdkp->nr_zones != nr_zones ||
432 disk->queue->nr_zones != nr_zones) {
433 ret = blk_revalidate_disk_zones(disk);
434 if (ret != 0)
435 goto err;
436 sdkp->zone_blocks = zone_blocks;
437 sdkp->nr_zones = nr_zones;
438 }
439
440 return 0;
441
442 err:
443 sdkp->capacity = 0;
444
445 return ret;
446 }
447
448 void sd_zbc_print_zones(struct scsi_disk *sdkp)
449 {
450 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
451 return;
452
453 if (sdkp->capacity & (sdkp->zone_blocks - 1))
454 sd_printk(KERN_NOTICE, sdkp,
455 "%u zones of %u logical blocks + 1 runt zone\n",
456 sdkp->nr_zones - 1,
457 sdkp->zone_blocks);
458 else
459 sd_printk(KERN_NOTICE, sdkp,
460 "%u zones of %u logical blocks\n",
461 sdkp->nr_zones,
462 sdkp->zone_blocks);
463 }