xref: /openbmc/linux/drivers/scsi/sd_zbc.c (revision 305c8388)
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 #include <linux/mutex.h>
15 
16 #include <asm/unaligned.h>
17 
18 #include <scsi/scsi.h>
19 #include <scsi/scsi_cmnd.h>
20 
21 #include "sd.h"
22 
23 static unsigned int sd_zbc_get_zone_wp_offset(struct blk_zone *zone)
24 {
25 	if (zone->type == ZBC_ZONE_TYPE_CONV)
26 		return 0;
27 
28 	switch (zone->cond) {
29 	case BLK_ZONE_COND_IMP_OPEN:
30 	case BLK_ZONE_COND_EXP_OPEN:
31 	case BLK_ZONE_COND_CLOSED:
32 		return zone->wp - zone->start;
33 	case BLK_ZONE_COND_FULL:
34 		return zone->len;
35 	case BLK_ZONE_COND_EMPTY:
36 	case BLK_ZONE_COND_OFFLINE:
37 	case BLK_ZONE_COND_READONLY:
38 	default:
39 		/*
40 		 * Offline and read-only zones do not have a valid
41 		 * write pointer. Use 0 as for an empty zone.
42 		 */
43 		return 0;
44 	}
45 }
46 
47 static int sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
48 			       unsigned int idx, report_zones_cb cb, void *data)
49 {
50 	struct scsi_device *sdp = sdkp->device;
51 	struct blk_zone zone = { 0 };
52 	int ret;
53 
54 	zone.type = buf[0] & 0x0f;
55 	zone.cond = (buf[1] >> 4) & 0xf;
56 	if (buf[1] & 0x01)
57 		zone.reset = 1;
58 	if (buf[1] & 0x02)
59 		zone.non_seq = 1;
60 
61 	zone.len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
62 	zone.start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
63 	zone.wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
64 	if (zone.type != ZBC_ZONE_TYPE_CONV &&
65 	    zone.cond == ZBC_ZONE_COND_FULL)
66 		zone.wp = zone.start + zone.len;
67 
68 	ret = cb(&zone, idx, data);
69 	if (ret)
70 		return ret;
71 
72 	if (sdkp->rev_wp_offset)
73 		sdkp->rev_wp_offset[idx] = sd_zbc_get_zone_wp_offset(&zone);
74 
75 	return 0;
76 }
77 
78 /**
79  * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
80  * @sdkp: The target disk
81  * @buf: vmalloc-ed buffer to use for the reply
82  * @buflen: the buffer size
83  * @lba: Start LBA of the report
84  * @partial: Do partial report
85  *
86  * For internal use during device validation.
87  * Using partial=true can significantly speed up execution of a report zones
88  * command because the disk does not have to count all possible report matching
89  * zones and will only report the count of zones fitting in the command reply
90  * buffer.
91  */
92 static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
93 				  unsigned int buflen, sector_t lba,
94 				  bool partial)
95 {
96 	struct scsi_device *sdp = sdkp->device;
97 	const int timeout = sdp->request_queue->rq_timeout;
98 	struct scsi_sense_hdr sshdr;
99 	unsigned char cmd[16];
100 	unsigned int rep_len;
101 	int result;
102 
103 	memset(cmd, 0, 16);
104 	cmd[0] = ZBC_IN;
105 	cmd[1] = ZI_REPORT_ZONES;
106 	put_unaligned_be64(lba, &cmd[2]);
107 	put_unaligned_be32(buflen, &cmd[10]);
108 	if (partial)
109 		cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
110 
111 	result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
112 				  buf, buflen, &sshdr,
113 				  timeout, SD_MAX_RETRIES, NULL);
114 	if (result) {
115 		sd_printk(KERN_ERR, sdkp,
116 			  "REPORT ZONES start lba %llu failed\n", lba);
117 		sd_print_result(sdkp, "REPORT ZONES", result);
118 		if (driver_byte(result) == DRIVER_SENSE &&
119 		    scsi_sense_valid(&sshdr))
120 			sd_print_sense_hdr(sdkp, &sshdr);
121 		return -EIO;
122 	}
123 
124 	rep_len = get_unaligned_be32(&buf[0]);
125 	if (rep_len < 64) {
126 		sd_printk(KERN_ERR, sdkp,
127 			  "REPORT ZONES report invalid length %u\n",
128 			  rep_len);
129 		return -EIO;
130 	}
131 
132 	return 0;
133 }
134 
135 /**
136  * Allocate a buffer for report zones reply.
137  * @sdkp: The target disk
138  * @nr_zones: Maximum number of zones to report
139  * @buflen: Size of the buffer allocated
140  *
141  * Try to allocate a reply buffer for the number of requested zones.
142  * The size of the buffer allocated may be smaller than requested to
143  * satify the device constraint (max_hw_sectors, max_segments, etc).
144  *
145  * Return the address of the allocated buffer and update @buflen with
146  * the size of the allocated buffer.
147  */
148 static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
149 					unsigned int nr_zones, size_t *buflen)
150 {
151 	struct request_queue *q = sdkp->disk->queue;
152 	size_t bufsize;
153 	void *buf;
154 
155 	/*
156 	 * Report zone buffer size should be at most 64B times the number of
157 	 * zones requested plus the 64B reply header, but should be at least
158 	 * SECTOR_SIZE for ATA devices.
159 	 * Make sure that this size does not exceed the hardware capabilities.
160 	 * Furthermore, since the report zone command cannot be split, make
161 	 * sure that the allocated buffer can always be mapped by limiting the
162 	 * number of pages allocated to the HBA max segments limit.
163 	 */
164 	nr_zones = min(nr_zones, sdkp->nr_zones);
165 	bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE);
166 	bufsize = min_t(size_t, bufsize,
167 			queue_max_hw_sectors(q) << SECTOR_SHIFT);
168 	bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
169 
170 	while (bufsize >= SECTOR_SIZE) {
171 		buf = __vmalloc(bufsize,
172 				GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY);
173 		if (buf) {
174 			*buflen = bufsize;
175 			return buf;
176 		}
177 		bufsize >>= 1;
178 	}
179 
180 	return NULL;
181 }
182 
183 /**
184  * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
185  * @sdkp: The target disk
186  */
187 static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
188 {
189 	return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
190 }
191 
192 int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
193 			unsigned int nr_zones, report_zones_cb cb, void *data)
194 {
195 	struct scsi_disk *sdkp = scsi_disk(disk);
196 	sector_t capacity = logical_to_sectors(sdkp->device, sdkp->capacity);
197 	unsigned int nr, i;
198 	unsigned char *buf;
199 	size_t offset, buflen = 0;
200 	int zone_idx = 0;
201 	int ret;
202 
203 	if (!sd_is_zoned(sdkp))
204 		/* Not a zoned device */
205 		return -EOPNOTSUPP;
206 
207 	if (!capacity)
208 		/* Device gone or invalid */
209 		return -ENODEV;
210 
211 	buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen);
212 	if (!buf)
213 		return -ENOMEM;
214 
215 	while (zone_idx < nr_zones && sector < capacity) {
216 		ret = sd_zbc_do_report_zones(sdkp, buf, buflen,
217 				sectors_to_logical(sdkp->device, sector), true);
218 		if (ret)
219 			goto out;
220 
221 		offset = 0;
222 		nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64);
223 		if (!nr)
224 			break;
225 
226 		for (i = 0; i < nr && zone_idx < nr_zones; i++) {
227 			offset += 64;
228 			ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx,
229 						  cb, data);
230 			if (ret)
231 				goto out;
232 			zone_idx++;
233 		}
234 
235 		sector += sd_zbc_zone_sectors(sdkp) * i;
236 	}
237 
238 	ret = zone_idx;
239 out:
240 	kvfree(buf);
241 	return ret;
242 }
243 
244 static blk_status_t sd_zbc_cmnd_checks(struct scsi_cmnd *cmd)
245 {
246 	struct request *rq = cmd->request;
247 	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
248 	sector_t sector = blk_rq_pos(rq);
249 
250 	if (!sd_is_zoned(sdkp))
251 		/* Not a zoned device */
252 		return BLK_STS_IOERR;
253 
254 	if (sdkp->device->changed)
255 		return BLK_STS_IOERR;
256 
257 	if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
258 		/* Unaligned request */
259 		return BLK_STS_IOERR;
260 
261 	return BLK_STS_OK;
262 }
263 
264 #define SD_ZBC_INVALID_WP_OFST	(~0u)
265 #define SD_ZBC_UPDATING_WP_OFST	(SD_ZBC_INVALID_WP_OFST - 1)
266 
267 static int sd_zbc_update_wp_offset_cb(struct blk_zone *zone, unsigned int idx,
268 				    void *data)
269 {
270 	struct scsi_disk *sdkp = data;
271 
272 	lockdep_assert_held(&sdkp->zones_wp_offset_lock);
273 
274 	sdkp->zones_wp_offset[idx] = sd_zbc_get_zone_wp_offset(zone);
275 
276 	return 0;
277 }
278 
279 static void sd_zbc_update_wp_offset_workfn(struct work_struct *work)
280 {
281 	struct scsi_disk *sdkp;
282 	unsigned int zno;
283 	int ret;
284 
285 	sdkp = container_of(work, struct scsi_disk, zone_wp_offset_work);
286 
287 	spin_lock_bh(&sdkp->zones_wp_offset_lock);
288 	for (zno = 0; zno < sdkp->nr_zones; zno++) {
289 		if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST)
290 			continue;
291 
292 		spin_unlock_bh(&sdkp->zones_wp_offset_lock);
293 		ret = sd_zbc_do_report_zones(sdkp, sdkp->zone_wp_update_buf,
294 					     SD_BUF_SIZE,
295 					     zno * sdkp->zone_blocks, true);
296 		spin_lock_bh(&sdkp->zones_wp_offset_lock);
297 		if (!ret)
298 			sd_zbc_parse_report(sdkp, sdkp->zone_wp_update_buf + 64,
299 					    zno, sd_zbc_update_wp_offset_cb,
300 					    sdkp);
301 	}
302 	spin_unlock_bh(&sdkp->zones_wp_offset_lock);
303 
304 	scsi_device_put(sdkp->device);
305 }
306 
307 /**
308  * sd_zbc_prepare_zone_append() - Prepare an emulated ZONE_APPEND command.
309  * @cmd: the command to setup
310  * @lba: the LBA to patch
311  * @nr_blocks: the number of LBAs to be written
312  *
313  * Called from sd_setup_read_write_cmnd() for REQ_OP_ZONE_APPEND.
314  * @sd_zbc_prepare_zone_append() handles the necessary zone wrote locking and
315  * patching of the lba for an emulated ZONE_APPEND command.
316  *
317  * In case the cached write pointer offset is %SD_ZBC_INVALID_WP_OFST it will
318  * schedule a REPORT ZONES command and return BLK_STS_IOERR.
319  */
320 blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba,
321 					unsigned int nr_blocks)
322 {
323 	struct request *rq = cmd->request;
324 	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
325 	unsigned int wp_offset, zno = blk_rq_zone_no(rq);
326 	blk_status_t ret;
327 
328 	ret = sd_zbc_cmnd_checks(cmd);
329 	if (ret != BLK_STS_OK)
330 		return ret;
331 
332 	if (!blk_rq_zone_is_seq(rq))
333 		return BLK_STS_IOERR;
334 
335 	/* Unlock of the write lock will happen in sd_zbc_complete() */
336 	if (!blk_req_zone_write_trylock(rq))
337 		return BLK_STS_ZONE_RESOURCE;
338 
339 	spin_lock_bh(&sdkp->zones_wp_offset_lock);
340 	wp_offset = sdkp->zones_wp_offset[zno];
341 	switch (wp_offset) {
342 	case SD_ZBC_INVALID_WP_OFST:
343 		/*
344 		 * We are about to schedule work to update a zone write pointer
345 		 * offset, which will cause the zone append command to be
346 		 * requeued. So make sure that the scsi device does not go away
347 		 * while the work is being processed.
348 		 */
349 		if (scsi_device_get(sdkp->device)) {
350 			ret = BLK_STS_IOERR;
351 			break;
352 		}
353 		sdkp->zones_wp_offset[zno] = SD_ZBC_UPDATING_WP_OFST;
354 		schedule_work(&sdkp->zone_wp_offset_work);
355 		fallthrough;
356 	case SD_ZBC_UPDATING_WP_OFST:
357 		ret = BLK_STS_DEV_RESOURCE;
358 		break;
359 	default:
360 		wp_offset = sectors_to_logical(sdkp->device, wp_offset);
361 		if (wp_offset + nr_blocks > sdkp->zone_blocks) {
362 			ret = BLK_STS_IOERR;
363 			break;
364 		}
365 
366 		*lba += wp_offset;
367 	}
368 	spin_unlock_bh(&sdkp->zones_wp_offset_lock);
369 	if (ret)
370 		blk_req_zone_write_unlock(rq);
371 	return ret;
372 }
373 
374 /**
375  * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations
376  *			can be RESET WRITE POINTER, OPEN, CLOSE or FINISH.
377  * @cmd: the command to setup
378  * @op: Operation to be performed
379  * @all: All zones control
380  *
381  * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL,
382  * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests.
383  */
384 blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
385 					 unsigned char op, bool all)
386 {
387 	struct request *rq = cmd->request;
388 	sector_t sector = blk_rq_pos(rq);
389 	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
390 	sector_t block = sectors_to_logical(sdkp->device, sector);
391 	blk_status_t ret;
392 
393 	ret = sd_zbc_cmnd_checks(cmd);
394 	if (ret != BLK_STS_OK)
395 		return ret;
396 
397 	cmd->cmd_len = 16;
398 	memset(cmd->cmnd, 0, cmd->cmd_len);
399 	cmd->cmnd[0] = ZBC_OUT;
400 	cmd->cmnd[1] = op;
401 	if (all)
402 		cmd->cmnd[14] = 0x1;
403 	else
404 		put_unaligned_be64(block, &cmd->cmnd[2]);
405 
406 	rq->timeout = SD_TIMEOUT;
407 	cmd->sc_data_direction = DMA_NONE;
408 	cmd->transfersize = 0;
409 	cmd->allowed = 0;
410 
411 	return BLK_STS_OK;
412 }
413 
414 static bool sd_zbc_need_zone_wp_update(struct request *rq)
415 {
416 	switch (req_op(rq)) {
417 	case REQ_OP_ZONE_APPEND:
418 	case REQ_OP_ZONE_FINISH:
419 	case REQ_OP_ZONE_RESET:
420 	case REQ_OP_ZONE_RESET_ALL:
421 		return true;
422 	case REQ_OP_WRITE:
423 	case REQ_OP_WRITE_ZEROES:
424 	case REQ_OP_WRITE_SAME:
425 		return blk_rq_zone_is_seq(rq);
426 	default:
427 		return false;
428 	}
429 }
430 
431 /**
432  * sd_zbc_zone_wp_update - Update cached zone write pointer upon cmd completion
433  * @cmd: Completed command
434  * @good_bytes: Command reply bytes
435  *
436  * Called from sd_zbc_complete() to handle the update of the cached zone write
437  * pointer value in case an update is needed.
438  */
439 static unsigned int sd_zbc_zone_wp_update(struct scsi_cmnd *cmd,
440 					  unsigned int good_bytes)
441 {
442 	int result = cmd->result;
443 	struct request *rq = cmd->request;
444 	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
445 	unsigned int zno = blk_rq_zone_no(rq);
446 	enum req_opf op = req_op(rq);
447 
448 	/*
449 	 * If we got an error for a command that needs updating the write
450 	 * pointer offset cache, we must mark the zone wp offset entry as
451 	 * invalid to force an update from disk the next time a zone append
452 	 * command is issued.
453 	 */
454 	spin_lock_bh(&sdkp->zones_wp_offset_lock);
455 
456 	if (result && op != REQ_OP_ZONE_RESET_ALL) {
457 		if (op == REQ_OP_ZONE_APPEND) {
458 			/* Force complete completion (no retry) */
459 			good_bytes = 0;
460 			scsi_set_resid(cmd, blk_rq_bytes(rq));
461 		}
462 
463 		/*
464 		 * Force an update of the zone write pointer offset on
465 		 * the next zone append access.
466 		 */
467 		if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST)
468 			sdkp->zones_wp_offset[zno] = SD_ZBC_INVALID_WP_OFST;
469 		goto unlock_wp_offset;
470 	}
471 
472 	switch (op) {
473 	case REQ_OP_ZONE_APPEND:
474 		rq->__sector += sdkp->zones_wp_offset[zno];
475 		fallthrough;
476 	case REQ_OP_WRITE_ZEROES:
477 	case REQ_OP_WRITE_SAME:
478 	case REQ_OP_WRITE:
479 		if (sdkp->zones_wp_offset[zno] < sd_zbc_zone_sectors(sdkp))
480 			sdkp->zones_wp_offset[zno] +=
481 						good_bytes >> SECTOR_SHIFT;
482 		break;
483 	case REQ_OP_ZONE_RESET:
484 		sdkp->zones_wp_offset[zno] = 0;
485 		break;
486 	case REQ_OP_ZONE_FINISH:
487 		sdkp->zones_wp_offset[zno] = sd_zbc_zone_sectors(sdkp);
488 		break;
489 	case REQ_OP_ZONE_RESET_ALL:
490 		memset(sdkp->zones_wp_offset, 0,
491 		       sdkp->nr_zones * sizeof(unsigned int));
492 		break;
493 	default:
494 		break;
495 	}
496 
497 unlock_wp_offset:
498 	spin_unlock_bh(&sdkp->zones_wp_offset_lock);
499 
500 	return good_bytes;
501 }
502 
503 /**
504  * sd_zbc_complete - ZBC command post processing.
505  * @cmd: Completed command
506  * @good_bytes: Command reply bytes
507  * @sshdr: command sense header
508  *
509  * Called from sd_done() to handle zone commands errors and updates to the
510  * device queue zone write pointer offset cahce.
511  */
512 unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
513 		     struct scsi_sense_hdr *sshdr)
514 {
515 	int result = cmd->result;
516 	struct request *rq = cmd->request;
517 
518 	if (op_is_zone_mgmt(req_op(rq)) &&
519 	    result &&
520 	    sshdr->sense_key == ILLEGAL_REQUEST &&
521 	    sshdr->asc == 0x24) {
522 		/*
523 		 * INVALID FIELD IN CDB error: a zone management command was
524 		 * attempted on a conventional zone. Nothing to worry about,
525 		 * so be quiet about the error.
526 		 */
527 		rq->rq_flags |= RQF_QUIET;
528 	} else if (sd_zbc_need_zone_wp_update(rq))
529 		good_bytes = sd_zbc_zone_wp_update(cmd, good_bytes);
530 
531 	if (req_op(rq) == REQ_OP_ZONE_APPEND)
532 		blk_req_zone_write_unlock(rq);
533 
534 	return good_bytes;
535 }
536 
537 /**
538  * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
539  * @sdkp: Target disk
540  * @buf: Buffer where to store the VPD page data
541  *
542  * Read VPD page B6, get information and check that reads are unconstrained.
543  */
544 static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
545 					      unsigned char *buf)
546 {
547 
548 	if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
549 		sd_printk(KERN_NOTICE, sdkp,
550 			  "Read zoned characteristics VPD page failed\n");
551 		return -ENODEV;
552 	}
553 
554 	if (sdkp->device->type != TYPE_ZBC) {
555 		/* Host-aware */
556 		sdkp->urswrz = 1;
557 		sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
558 		sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
559 		sdkp->zones_max_open = 0;
560 	} else {
561 		/* Host-managed */
562 		sdkp->urswrz = buf[4] & 1;
563 		sdkp->zones_optimal_open = 0;
564 		sdkp->zones_optimal_nonseq = 0;
565 		sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
566 	}
567 
568 	/*
569 	 * Check for unconstrained reads: host-managed devices with
570 	 * constrained reads (drives failing read after write pointer)
571 	 * are not supported.
572 	 */
573 	if (!sdkp->urswrz) {
574 		if (sdkp->first_scan)
575 			sd_printk(KERN_NOTICE, sdkp,
576 			  "constrained reads devices are not supported\n");
577 		return -ENODEV;
578 	}
579 
580 	return 0;
581 }
582 
583 /**
584  * sd_zbc_check_capacity - Check the device capacity
585  * @sdkp: Target disk
586  * @buf: command buffer
587  * @zblock: zone size in number of blocks
588  *
589  * Get the device zone size and check that the device capacity as reported
590  * by READ CAPACITY matches the max_lba value (plus one) of the report zones
591  * command reply for devices with RC_BASIS == 0.
592  *
593  * Returns 0 upon success or an error code upon failure.
594  */
595 static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
596 				 u32 *zblocks)
597 {
598 	u64 zone_blocks;
599 	sector_t max_lba;
600 	unsigned char *rec;
601 	int ret;
602 
603 	/* Do a report zone to get max_lba and the size of the first zone */
604 	ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);
605 	if (ret)
606 		return ret;
607 
608 	if (sdkp->rc_basis == 0) {
609 		/* The max_lba field is the capacity of this device */
610 		max_lba = get_unaligned_be64(&buf[8]);
611 		if (sdkp->capacity != max_lba + 1) {
612 			if (sdkp->first_scan)
613 				sd_printk(KERN_WARNING, sdkp,
614 					"Changing capacity from %llu to max LBA+1 %llu\n",
615 					(unsigned long long)sdkp->capacity,
616 					(unsigned long long)max_lba + 1);
617 			sdkp->capacity = max_lba + 1;
618 		}
619 	}
620 
621 	/* Get the size of the first reported zone */
622 	rec = buf + 64;
623 	zone_blocks = get_unaligned_be64(&rec[8]);
624 	if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
625 		if (sdkp->first_scan)
626 			sd_printk(KERN_NOTICE, sdkp,
627 				  "Zone size too large\n");
628 		return -EFBIG;
629 	}
630 
631 	*zblocks = zone_blocks;
632 
633 	return 0;
634 }
635 
636 static void sd_zbc_revalidate_zones_cb(struct gendisk *disk)
637 {
638 	struct scsi_disk *sdkp = scsi_disk(disk);
639 
640 	swap(sdkp->zones_wp_offset, sdkp->rev_wp_offset);
641 }
642 
643 static int sd_zbc_revalidate_zones(struct scsi_disk *sdkp,
644 				   u32 zone_blocks,
645 				   unsigned int nr_zones)
646 {
647 	struct gendisk *disk = sdkp->disk;
648 	int ret = 0;
649 
650 	/*
651 	 * Make sure revalidate zones are serialized to ensure exclusive
652 	 * updates of the scsi disk data.
653 	 */
654 	mutex_lock(&sdkp->rev_mutex);
655 
656 	/*
657 	 * Revalidate the disk zones to update the device request queue zone
658 	 * bitmaps and the zone write pointer offset array. Do this only once
659 	 * the device capacity is set on the second revalidate execution for
660 	 * disk scan or if something changed when executing a normal revalidate.
661 	 */
662 	if (sdkp->first_scan) {
663 		sdkp->zone_blocks = zone_blocks;
664 		sdkp->nr_zones = nr_zones;
665 		goto unlock;
666 	}
667 
668 	if (sdkp->zone_blocks == zone_blocks &&
669 	    sdkp->nr_zones == nr_zones &&
670 	    disk->queue->nr_zones == nr_zones)
671 		goto unlock;
672 
673 	sdkp->rev_wp_offset = kvcalloc(nr_zones, sizeof(u32), GFP_NOIO);
674 	if (!sdkp->rev_wp_offset) {
675 		ret = -ENOMEM;
676 		goto unlock;
677 	}
678 
679 	ret = blk_revalidate_disk_zones(disk, sd_zbc_revalidate_zones_cb);
680 
681 	kvfree(sdkp->rev_wp_offset);
682 	sdkp->rev_wp_offset = NULL;
683 
684 unlock:
685 	mutex_unlock(&sdkp->rev_mutex);
686 
687 	return ret;
688 }
689 
690 int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
691 {
692 	struct gendisk *disk = sdkp->disk;
693 	struct request_queue *q = disk->queue;
694 	unsigned int nr_zones;
695 	u32 zone_blocks = 0;
696 	u32 max_append;
697 	int ret;
698 
699 	if (!sd_is_zoned(sdkp))
700 		/*
701 		 * Device managed or normal SCSI disk,
702 		 * no special handling required
703 		 */
704 		return 0;
705 
706 	/* Check zoned block device characteristics (unconstrained reads) */
707 	ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
708 	if (ret)
709 		goto err;
710 
711 	/* Check the device capacity reported by report zones */
712 	ret = sd_zbc_check_capacity(sdkp, buf, &zone_blocks);
713 	if (ret != 0)
714 		goto err;
715 
716 	/* The drive satisfies the kernel restrictions: set it up */
717 	blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
718 	blk_queue_required_elevator_features(q, ELEVATOR_F_ZBD_SEQ_WRITE);
719 	nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
720 
721 	/* READ16/WRITE16 is mandatory for ZBC disks */
722 	sdkp->device->use_16_for_rw = 1;
723 	sdkp->device->use_10_for_rw = 0;
724 
725 	ret = sd_zbc_revalidate_zones(sdkp, zone_blocks, nr_zones);
726 	if (ret)
727 		goto err;
728 
729 	/*
730 	 * On the first scan 'chunk_sectors' isn't setup yet, so calling
731 	 * blk_queue_max_zone_append_sectors() will result in a WARN(). Defer
732 	 * this setting to the second scan.
733 	 */
734 	if (sdkp->first_scan)
735 		return 0;
736 
737 	max_append = min_t(u32, logical_to_sectors(sdkp->device, zone_blocks),
738 			   q->limits.max_segments << (PAGE_SHIFT - 9));
739 	max_append = min_t(u32, max_append, queue_max_hw_sectors(q));
740 
741 	blk_queue_max_zone_append_sectors(q, max_append);
742 
743 	return 0;
744 
745 err:
746 	sdkp->capacity = 0;
747 
748 	return ret;
749 }
750 
751 void sd_zbc_print_zones(struct scsi_disk *sdkp)
752 {
753 	if (!sd_is_zoned(sdkp) || !sdkp->capacity)
754 		return;
755 
756 	if (sdkp->capacity & (sdkp->zone_blocks - 1))
757 		sd_printk(KERN_NOTICE, sdkp,
758 			  "%u zones of %u logical blocks + 1 runt zone\n",
759 			  sdkp->nr_zones - 1,
760 			  sdkp->zone_blocks);
761 	else
762 		sd_printk(KERN_NOTICE, sdkp,
763 			  "%u zones of %u logical blocks\n",
764 			  sdkp->nr_zones,
765 			  sdkp->zone_blocks);
766 }
767 
768 int sd_zbc_init_disk(struct scsi_disk *sdkp)
769 {
770 	if (!sd_is_zoned(sdkp))
771 		return 0;
772 
773 	sdkp->zones_wp_offset = NULL;
774 	spin_lock_init(&sdkp->zones_wp_offset_lock);
775 	sdkp->rev_wp_offset = NULL;
776 	mutex_init(&sdkp->rev_mutex);
777 	INIT_WORK(&sdkp->zone_wp_offset_work, sd_zbc_update_wp_offset_workfn);
778 	sdkp->zone_wp_update_buf = kzalloc(SD_BUF_SIZE, GFP_KERNEL);
779 	if (!sdkp->zone_wp_update_buf)
780 		return -ENOMEM;
781 
782 	return 0;
783 }
784 
785 void sd_zbc_release_disk(struct scsi_disk *sdkp)
786 {
787 	kvfree(sdkp->zones_wp_offset);
788 	sdkp->zones_wp_offset = NULL;
789 	kfree(sdkp->zone_wp_update_buf);
790 	sdkp->zone_wp_update_buf = NULL;
791 }
792