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