Linux Audio

Check our new training course

Loading...
v6.8
  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#define CREATE_TRACE_POINTS
 24#include "sd_trace.h"
 25
 26/**
 27 * sd_zbc_get_zone_wp_offset - Get zone write pointer offset.
 28 * @zone: Zone for which to return the write pointer offset.
 
 
 29 *
 30 * Return: offset of the write pointer from the start of the zone.
 31 */
 32static unsigned int sd_zbc_get_zone_wp_offset(struct blk_zone *zone)
 33{
 34	if (zone->type == ZBC_ZONE_TYPE_CONV)
 35		return 0;
 36
 37	switch (zone->cond) {
 38	case BLK_ZONE_COND_IMP_OPEN:
 39	case BLK_ZONE_COND_EXP_OPEN:
 40	case BLK_ZONE_COND_CLOSED:
 41		return zone->wp - zone->start;
 42	case BLK_ZONE_COND_FULL:
 43		return zone->len;
 44	case BLK_ZONE_COND_EMPTY:
 45	case BLK_ZONE_COND_OFFLINE:
 46	case BLK_ZONE_COND_READONLY:
 47	default:
 48		/*
 49		 * Offline and read-only zones do not have a valid
 50		 * write pointer. Use 0 as for an empty zone.
 51		 */
 52		return 0;
 53	}
 54}
 55
 56/* Whether or not a SCSI zone descriptor describes a gap zone. */
 57static bool sd_zbc_is_gap_zone(const u8 buf[64])
 58{
 59	return (buf[0] & 0xf) == ZBC_ZONE_TYPE_GAP;
 60}
 61
 62/**
 63 * sd_zbc_parse_report - Parse a SCSI zone descriptor
 64 * @sdkp: SCSI disk pointer.
 65 * @buf: SCSI zone descriptor.
 66 * @idx: Index of the zone relative to the first zone reported by the current
 67 *	sd_zbc_report_zones() call.
 68 * @cb: Callback function pointer.
 69 * @data: Second argument passed to @cb.
 70 *
 71 * Return: Value returned by @cb.
 72 *
 73 * Convert a SCSI zone descriptor into struct blk_zone format. Additionally,
 74 * call @cb(blk_zone, @data).
 75 */
 76static int sd_zbc_parse_report(struct scsi_disk *sdkp, const u8 buf[64],
 77			       unsigned int idx, report_zones_cb cb, void *data)
 78{
 79	struct scsi_device *sdp = sdkp->device;
 80	struct blk_zone zone = { 0 };
 81	sector_t start_lba, gran;
 82	int ret;
 83
 84	if (WARN_ON_ONCE(sd_zbc_is_gap_zone(buf)))
 85		return -EINVAL;
 86
 87	zone.type = buf[0] & 0x0f;
 88	zone.cond = (buf[1] >> 4) & 0xf;
 89	if (buf[1] & 0x01)
 90		zone.reset = 1;
 91	if (buf[1] & 0x02)
 92		zone.non_seq = 1;
 93
 94	start_lba = get_unaligned_be64(&buf[16]);
 95	zone.start = logical_to_sectors(sdp, start_lba);
 96	zone.capacity = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
 97	zone.len = zone.capacity;
 98	if (sdkp->zone_starting_lba_gran) {
 99		gran = logical_to_sectors(sdp, sdkp->zone_starting_lba_gran);
100		if (zone.len > gran) {
101			sd_printk(KERN_ERR, sdkp,
102				  "Invalid zone at LBA %llu with capacity %llu and length %llu; granularity = %llu\n",
103				  start_lba,
104				  sectors_to_logical(sdp, zone.capacity),
105				  sectors_to_logical(sdp, zone.len),
106				  sectors_to_logical(sdp, gran));
107			return -EINVAL;
108		}
109		/*
110		 * Use the starting LBA granularity instead of the zone length
111		 * obtained from the REPORT ZONES command.
112		 */
113		zone.len = gran;
114	}
115	if (zone.cond == ZBC_ZONE_COND_FULL)
116		zone.wp = zone.start + zone.len;
117	else
118		zone.wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
119
120	ret = cb(&zone, idx, data);
121	if (ret)
122		return ret;
123
124	if (sdkp->rev_wp_offset)
125		sdkp->rev_wp_offset[idx] = sd_zbc_get_zone_wp_offset(&zone);
126
127	return 0;
 
 
128}
129
130/**
131 * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
132 * @sdkp: The target disk
133 * @buf: vmalloc-ed buffer to use for the reply
134 * @buflen: the buffer size
135 * @lba: Start LBA of the report
136 * @partial: Do partial report
137 *
138 * For internal use during device validation.
139 * Using partial=true can significantly speed up execution of a report zones
140 * command because the disk does not have to count all possible report matching
141 * zones and will only report the count of zones fitting in the command reply
142 * buffer.
143 */
144static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
145				  unsigned int buflen, sector_t lba,
146				  bool partial)
147{
148	struct scsi_device *sdp = sdkp->device;
149	const int timeout = sdp->request_queue->rq_timeout;
150	struct scsi_sense_hdr sshdr;
151	const struct scsi_exec_args exec_args = {
152		.sshdr = &sshdr,
153	};
154	unsigned char cmd[16];
155	unsigned int rep_len;
156	int result;
157
158	memset(cmd, 0, 16);
159	cmd[0] = ZBC_IN;
160	cmd[1] = ZI_REPORT_ZONES;
161	put_unaligned_be64(lba, &cmd[2]);
162	put_unaligned_be32(buflen, &cmd[10]);
163	if (partial)
164		cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
165
166	result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, buf, buflen,
167				  timeout, SD_MAX_RETRIES, &exec_args);
 
168	if (result) {
169		sd_printk(KERN_ERR, sdkp,
170			  "REPORT ZONES start lba %llu failed\n", lba);
171		sd_print_result(sdkp, "REPORT ZONES", result);
172		if (result > 0 && scsi_sense_valid(&sshdr))
173			sd_print_sense_hdr(sdkp, &sshdr);
174		return -EIO;
175	}
176
177	rep_len = get_unaligned_be32(&buf[0]);
178	if (rep_len < 64) {
179		sd_printk(KERN_ERR, sdkp,
180			  "REPORT ZONES report invalid length %u\n",
181			  rep_len);
182		return -EIO;
183	}
184
185	return 0;
186}
187
188/**
189 * sd_zbc_alloc_report_buffer() - Allocate a buffer for report zones reply.
190 * @sdkp: The target disk
191 * @nr_zones: Maximum number of zones to report
192 * @buflen: Size of the buffer allocated
193 *
194 * Try to allocate a reply buffer for the number of requested zones.
195 * The size of the buffer allocated may be smaller than requested to
196 * satify the device constraint (max_hw_sectors, max_segments, etc).
197 *
198 * Return the address of the allocated buffer and update @buflen with
199 * the size of the allocated buffer.
200 */
201static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
202					unsigned int nr_zones, size_t *buflen)
203{
204	struct request_queue *q = sdkp->disk->queue;
205	size_t bufsize;
206	void *buf;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
208	/*
209	 * Report zone buffer size should be at most 64B times the number of
210	 * zones requested plus the 64B reply header, but should be aligned
211	 * to SECTOR_SIZE for ATA devices.
212	 * Make sure that this size does not exceed the hardware capabilities.
213	 * Furthermore, since the report zone command cannot be split, make
214	 * sure that the allocated buffer can always be mapped by limiting the
215	 * number of pages allocated to the HBA max segments limit.
216	 */
217	nr_zones = min(nr_zones, sdkp->zone_info.nr_zones);
218	bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE);
219	bufsize = min_t(size_t, bufsize,
220			queue_max_hw_sectors(q) << SECTOR_SHIFT);
221	bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
222
223	while (bufsize >= SECTOR_SIZE) {
224		buf = __vmalloc(bufsize,
225				GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY);
226		if (buf) {
227			*buflen = bufsize;
228			return buf;
229		}
230		bufsize = rounddown(bufsize >> 1, SECTOR_SIZE);
231	}
232
233	return NULL;
234}
235
236/**
237 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
238 * @sdkp: The target disk
239 */
240static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
241{
242	return logical_to_sectors(sdkp->device, sdkp->zone_info.zone_blocks);
243}
244
245/**
246 * sd_zbc_report_zones - SCSI .report_zones() callback.
247 * @disk: Disk to report zones for.
248 * @sector: Start sector.
249 * @nr_zones: Maximum number of zones to report.
250 * @cb: Callback function called to report zone information.
251 * @data: Second argument passed to @cb.
252 *
253 * Called by the block layer to iterate over zone information. See also the
254 * disk->fops->report_zones() calls in block/blk-zoned.c.
255 */
256int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
257			unsigned int nr_zones, report_zones_cb cb, void *data)
258{
259	struct scsi_disk *sdkp = scsi_disk(disk);
260	sector_t lba = sectors_to_logical(sdkp->device, sector);
261	unsigned int nr, i;
262	unsigned char *buf;
263	u64 zone_length, start_lba;
264	size_t offset, buflen = 0;
265	int zone_idx = 0;
266	int ret;
267
268	if (!sd_is_zoned(sdkp))
269		/* Not a zoned device */
270		return -EOPNOTSUPP;
271
272	if (!sdkp->capacity)
273		/* Device gone or invalid */
274		return -ENODEV;
275
276	buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen);
277	if (!buf)
278		return -ENOMEM;
279
280	while (zone_idx < nr_zones && lba < sdkp->capacity) {
281		ret = sd_zbc_do_report_zones(sdkp, buf, buflen, lba, true);
282		if (ret)
283			goto out;
284
 
285		offset = 0;
286		nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64);
287		if (!nr)
288			break;
289
290		for (i = 0; i < nr && zone_idx < nr_zones; i++) {
 
 
 
 
 
291			offset += 64;
292			start_lba = get_unaligned_be64(&buf[offset + 16]);
293			zone_length = get_unaligned_be64(&buf[offset + 8]);
294			if ((zone_idx == 0 &&
295			    (lba < start_lba ||
296			     lba >= start_lba + zone_length)) ||
297			    (zone_idx > 0 && start_lba != lba) ||
298			    start_lba + zone_length < start_lba) {
299				sd_printk(KERN_ERR, sdkp,
300					  "Zone %d at LBA %llu is invalid: %llu + %llu\n",
301					  zone_idx, lba, start_lba, zone_length);
302				ret = -EINVAL;
303				goto out;
304			}
305			lba = start_lba + zone_length;
306			if (sd_zbc_is_gap_zone(&buf[offset])) {
307				if (sdkp->zone_starting_lba_gran)
308					continue;
309				sd_printk(KERN_ERR, sdkp,
310					  "Gap zone without constant LBA offsets\n");
311				ret = -EINVAL;
312				goto out;
313			}
314
315			ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx,
316						  cb, data);
317			if (ret)
318				goto out;
319
320			zone_idx++;
321		}
322	}
323
324	ret = zone_idx;
325out:
326	kvfree(buf);
327	return ret;
328}
329
330static blk_status_t sd_zbc_cmnd_checks(struct scsi_cmnd *cmd)
331{
332	struct request *rq = scsi_cmd_to_rq(cmd);
333	struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
334	sector_t sector = blk_rq_pos(rq);
335
336	if (!sd_is_zoned(sdkp))
337		/* Not a zoned device */
338		return BLK_STS_IOERR;
339
340	if (sdkp->device->changed)
341		return BLK_STS_IOERR;
342
343	if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
344		/* Unaligned request */
345		return BLK_STS_IOERR;
346
347	return BLK_STS_OK;
348}
349
350#define SD_ZBC_INVALID_WP_OFST	(~0u)
351#define SD_ZBC_UPDATING_WP_OFST	(SD_ZBC_INVALID_WP_OFST - 1)
352
353static int sd_zbc_update_wp_offset_cb(struct blk_zone *zone, unsigned int idx,
354				    void *data)
355{
356	struct scsi_disk *sdkp = data;
357
358	lockdep_assert_held(&sdkp->zones_wp_offset_lock);
359
360	sdkp->zones_wp_offset[idx] = sd_zbc_get_zone_wp_offset(zone);
361
362	return 0;
363}
364
365/*
366 * An attempt to append a zone triggered an invalid write pointer error.
367 * Reread the write pointer of the zone(s) in which the append failed.
368 */
369static void sd_zbc_update_wp_offset_workfn(struct work_struct *work)
370{
371	struct scsi_disk *sdkp;
372	unsigned long flags;
373	sector_t zno;
374	int ret;
375
376	sdkp = container_of(work, struct scsi_disk, zone_wp_offset_work);
377
378	spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
379	for (zno = 0; zno < sdkp->zone_info.nr_zones; zno++) {
380		if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST)
381			continue;
382
383		spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
384		ret = sd_zbc_do_report_zones(sdkp, sdkp->zone_wp_update_buf,
385					     SD_BUF_SIZE,
386					     zno * sdkp->zone_info.zone_blocks, true);
387		spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
388		if (!ret)
389			sd_zbc_parse_report(sdkp, sdkp->zone_wp_update_buf + 64,
390					    zno, sd_zbc_update_wp_offset_cb,
391					    sdkp);
392	}
393	spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
394
395	scsi_device_put(sdkp->device);
396}
397
398/**
399 * sd_zbc_prepare_zone_append() - Prepare an emulated ZONE_APPEND command.
400 * @cmd: the command to setup
401 * @lba: the LBA to patch
402 * @nr_blocks: the number of LBAs to be written
403 *
404 * Called from sd_setup_read_write_cmnd() for REQ_OP_ZONE_APPEND.
405 * @sd_zbc_prepare_zone_append() handles the necessary zone wrote locking and
406 * patching of the lba for an emulated ZONE_APPEND command.
407 *
408 * In case the cached write pointer offset is %SD_ZBC_INVALID_WP_OFST it will
409 * schedule a REPORT ZONES command and return BLK_STS_IOERR.
410 */
411blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba,
412					unsigned int nr_blocks)
413{
414	struct request *rq = scsi_cmd_to_rq(cmd);
415	struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
416	unsigned int wp_offset, zno = blk_rq_zone_no(rq);
417	unsigned long flags;
418	blk_status_t ret;
419
420	ret = sd_zbc_cmnd_checks(cmd);
421	if (ret != BLK_STS_OK)
422		return ret;
423
424	if (!blk_rq_zone_is_seq(rq))
425		return BLK_STS_IOERR;
426
427	/* Unlock of the write lock will happen in sd_zbc_complete() */
428	if (!blk_req_zone_write_trylock(rq))
429		return BLK_STS_ZONE_RESOURCE;
430
431	spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
432	wp_offset = sdkp->zones_wp_offset[zno];
433	switch (wp_offset) {
434	case SD_ZBC_INVALID_WP_OFST:
435		/*
436		 * We are about to schedule work to update a zone write pointer
437		 * offset, which will cause the zone append command to be
438		 * requeued. So make sure that the scsi device does not go away
439		 * while the work is being processed.
440		 */
441		if (scsi_device_get(sdkp->device)) {
442			ret = BLK_STS_IOERR;
443			break;
444		}
445		sdkp->zones_wp_offset[zno] = SD_ZBC_UPDATING_WP_OFST;
446		schedule_work(&sdkp->zone_wp_offset_work);
447		fallthrough;
448	case SD_ZBC_UPDATING_WP_OFST:
449		ret = BLK_STS_DEV_RESOURCE;
450		break;
451	default:
452		wp_offset = sectors_to_logical(sdkp->device, wp_offset);
453		if (wp_offset + nr_blocks > sdkp->zone_info.zone_blocks) {
454			ret = BLK_STS_IOERR;
455			break;
456		}
457
458		trace_scsi_prepare_zone_append(cmd, *lba, wp_offset);
459		*lba += wp_offset;
460	}
461	spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
462	if (ret)
463		blk_req_zone_write_unlock(rq);
464	return ret;
465}
466
467/**
468 * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations
469 *			can be RESET WRITE POINTER, OPEN, CLOSE or FINISH.
470 * @cmd: the command to setup
471 * @op: Operation to be performed
472 * @all: All zones control
473 *
474 * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL,
475 * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests.
476 */
477blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
478					 unsigned char op, bool all)
479{
480	struct request *rq = scsi_cmd_to_rq(cmd);
 
481	sector_t sector = blk_rq_pos(rq);
482	struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
483	sector_t block = sectors_to_logical(sdkp->device, sector);
484	blk_status_t ret;
485
486	ret = sd_zbc_cmnd_checks(cmd);
487	if (ret != BLK_STS_OK)
488		return ret;
 
 
 
 
 
 
 
489
490	cmd->cmd_len = 16;
491	memset(cmd->cmnd, 0, cmd->cmd_len);
492	cmd->cmnd[0] = ZBC_OUT;
493	cmd->cmnd[1] = op;
494	if (all)
495		cmd->cmnd[14] = 0x1;
496	else
497		put_unaligned_be64(block, &cmd->cmnd[2]);
498
499	rq->timeout = SD_TIMEOUT;
500	cmd->sc_data_direction = DMA_NONE;
501	cmd->transfersize = 0;
502	cmd->allowed = 0;
503
504	return BLK_STS_OK;
505}
506
507static bool sd_zbc_need_zone_wp_update(struct request *rq)
508{
509	switch (req_op(rq)) {
510	case REQ_OP_ZONE_APPEND:
511	case REQ_OP_ZONE_FINISH:
512	case REQ_OP_ZONE_RESET:
513	case REQ_OP_ZONE_RESET_ALL:
514		return true;
515	case REQ_OP_WRITE:
516	case REQ_OP_WRITE_ZEROES:
517		return blk_rq_zone_is_seq(rq);
518	default:
519		return false;
520	}
521}
522
523/**
524 * sd_zbc_zone_wp_update - Update cached zone write pointer upon cmd completion
525 * @cmd: Completed command
526 * @good_bytes: Command reply bytes
 
527 *
528 * Called from sd_zbc_complete() to handle the update of the cached zone write
529 * pointer value in case an update is needed.
530 */
531static unsigned int sd_zbc_zone_wp_update(struct scsi_cmnd *cmd,
532					  unsigned int good_bytes)
533{
534	int result = cmd->result;
535	struct request *rq = scsi_cmd_to_rq(cmd);
536	struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
537	unsigned int zno = blk_rq_zone_no(rq);
538	enum req_op op = req_op(rq);
539	unsigned long flags;
540
541	/*
542	 * If we got an error for a command that needs updating the write
543	 * pointer offset cache, we must mark the zone wp offset entry as
544	 * invalid to force an update from disk the next time a zone append
545	 * command is issued.
546	 */
547	spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
548
549	if (result && op != REQ_OP_ZONE_RESET_ALL) {
550		if (op == REQ_OP_ZONE_APPEND) {
551			/* Force complete completion (no retry) */
552			good_bytes = 0;
553			scsi_set_resid(cmd, blk_rq_bytes(rq));
554		}
555
556		/*
557		 * Force an update of the zone write pointer offset on
558		 * the next zone append access.
559		 */
560		if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST)
561			sdkp->zones_wp_offset[zno] = SD_ZBC_INVALID_WP_OFST;
562		goto unlock_wp_offset;
563	}
564
565	switch (op) {
566	case REQ_OP_ZONE_APPEND:
567		trace_scsi_zone_wp_update(cmd, rq->__sector,
568				  sdkp->zones_wp_offset[zno], good_bytes);
569		rq->__sector += sdkp->zones_wp_offset[zno];
570		fallthrough;
571	case REQ_OP_WRITE_ZEROES:
572	case REQ_OP_WRITE:
573		if (sdkp->zones_wp_offset[zno] < sd_zbc_zone_sectors(sdkp))
574			sdkp->zones_wp_offset[zno] +=
575						good_bytes >> SECTOR_SHIFT;
576		break;
577	case REQ_OP_ZONE_RESET:
578		sdkp->zones_wp_offset[zno] = 0;
579		break;
580	case REQ_OP_ZONE_FINISH:
581		sdkp->zones_wp_offset[zno] = sd_zbc_zone_sectors(sdkp);
582		break;
583	case REQ_OP_ZONE_RESET_ALL:
584		memset(sdkp->zones_wp_offset, 0,
585		       sdkp->zone_info.nr_zones * sizeof(unsigned int));
586		break;
587	default:
588		break;
589	}
590
591unlock_wp_offset:
592	spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
 
 
 
 
 
 
 
 
593
594	return good_bytes;
595}
 
596
597/**
598 * sd_zbc_complete - ZBC command post processing.
599 * @cmd: Completed command
600 * @good_bytes: Command reply bytes
601 * @sshdr: command sense header
602 *
603 * Called from sd_done() to handle zone commands errors and updates to the
604 * device queue zone write pointer offset cahce.
605 */
606unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
607		     struct scsi_sense_hdr *sshdr)
608{
609	int result = cmd->result;
610	struct request *rq = scsi_cmd_to_rq(cmd);
611
612	if (op_is_zone_mgmt(req_op(rq)) &&
613	    result &&
614	    sshdr->sense_key == ILLEGAL_REQUEST &&
615	    sshdr->asc == 0x24) {
616		/*
617		 * INVALID FIELD IN CDB error: a zone management command was
618		 * attempted on a conventional zone. Nothing to worry about,
619		 * so be quiet about the error.
620		 */
621		rq->rq_flags |= RQF_QUIET;
622	} else if (sd_zbc_need_zone_wp_update(rq))
623		good_bytes = sd_zbc_zone_wp_update(cmd, good_bytes);
624
625	if (req_op(rq) == REQ_OP_ZONE_APPEND)
626		blk_req_zone_write_unlock(rq);
 
627
628	return good_bytes;
629}
630
631/**
632 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
633 * @sdkp: Target disk
634 * @buf: Buffer where to store the VPD page data
635 *
636 * Read VPD page B6, get information and check that reads are unconstrained.
637 */
638static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
639					      unsigned char *buf)
640{
641	u64 zone_starting_lba_gran;
642
643	if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
644		sd_printk(KERN_NOTICE, sdkp,
645			  "Read zoned characteristics VPD page failed\n");
646		return -ENODEV;
647	}
648
649	if (sdkp->device->type != TYPE_ZBC) {
650		/* Host-aware */
651		sdkp->urswrz = 1;
652		sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
653		sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
654		sdkp->zones_max_open = 0;
655		return 0;
 
 
 
 
 
656	}
657
658	/* Host-managed */
659	sdkp->urswrz = buf[4] & 1;
660	sdkp->zones_optimal_open = 0;
661	sdkp->zones_optimal_nonseq = 0;
662	sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
663	/* Check zone alignment method */
664	switch (buf[23] & 0xf) {
665	case 0:
666	case ZBC_CONSTANT_ZONE_LENGTH:
667		/* Use zone length */
668		break;
669	case ZBC_CONSTANT_ZONE_START_OFFSET:
670		zone_starting_lba_gran = get_unaligned_be64(&buf[24]);
671		if (zone_starting_lba_gran == 0 ||
672		    !is_power_of_2(zone_starting_lba_gran) ||
673		    logical_to_sectors(sdkp->device, zone_starting_lba_gran) >
674		    UINT_MAX) {
675			sd_printk(KERN_ERR, sdkp,
676				  "Invalid zone starting LBA granularity %llu\n",
677				  zone_starting_lba_gran);
678			return -ENODEV;
679		}
680		sdkp->zone_starting_lba_gran = zone_starting_lba_gran;
681		break;
682	default:
683		sd_printk(KERN_ERR, sdkp, "Invalid zone alignment method\n");
684		return -ENODEV;
685	}
686
687	/*
688	 * Check for unconstrained reads: host-managed devices with
689	 * constrained reads (drives failing read after write pointer)
690	 * are not supported.
691	 */
692	if (!sdkp->urswrz) {
693		if (sdkp->first_scan)
694			sd_printk(KERN_NOTICE, sdkp,
695			  "constrained reads devices are not supported\n");
696		return -ENODEV;
697	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
698
699	return 0;
700}
701
 
 
702/**
703 * sd_zbc_check_capacity - Check the device capacity
704 * @sdkp: Target disk
705 * @buf: command buffer
706 * @zblocks: zone size in logical blocks
707 *
708 * Get the device zone size and check that the device capacity as reported
709 * by READ CAPACITY matches the max_lba value (plus one) of the report zones
710 * command reply for devices with RC_BASIS == 0.
711 *
712 * Returns 0 upon success or an error code upon failure.
713 */
714static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
715				 u32 *zblocks)
716{
717	u64 zone_blocks;
718	sector_t max_lba;
 
719	unsigned char *rec;
 
 
720	int ret;
 
 
 
 
 
 
721
722	/* Do a report zone to get max_lba and the size of the first zone */
723	ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);
724	if (ret)
725		return ret;
726
727	if (sdkp->rc_basis == 0) {
728		/* The max_lba field is the capacity of this device */
729		max_lba = get_unaligned_be64(&buf[8]);
730		if (sdkp->capacity != max_lba + 1) {
731			if (sdkp->first_scan)
732				sd_printk(KERN_WARNING, sdkp,
733					"Changing capacity from %llu to max LBA+1 %llu\n",
734					(unsigned long long)sdkp->capacity,
735					(unsigned long long)max_lba + 1);
736			sdkp->capacity = max_lba + 1;
737		}
738	}
739
740	if (sdkp->zone_starting_lba_gran == 0) {
741		/* Get the size of the first reported zone */
 
 
 
 
 
 
 
742		rec = buf + 64;
743		zone_blocks = get_unaligned_be64(&rec[8]);
744		if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
745			if (sdkp->first_scan)
746				sd_printk(KERN_NOTICE, sdkp,
747					  "Zone size too large\n");
748			return -EFBIG;
 
 
 
 
 
 
 
 
 
 
749		}
750	} else {
751		zone_blocks = sdkp->zone_starting_lba_gran;
752	}
753
754	if (!is_power_of_2(zone_blocks)) {
755		sd_printk(KERN_ERR, sdkp,
756			  "Zone size %llu is not a power of two.\n",
757			  zone_blocks);
758		return -EINVAL;
759	}
760
761	*zblocks = zone_blocks;
762
763	return 0;
764}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
765
766static void sd_zbc_print_zones(struct scsi_disk *sdkp)
767{
768	if (!sd_is_zoned(sdkp) || !sdkp->capacity)
769		return;
770
771	if (sdkp->capacity & (sdkp->zone_info.zone_blocks - 1))
772		sd_printk(KERN_NOTICE, sdkp,
773			  "%u zones of %u logical blocks + 1 runt zone\n",
774			  sdkp->zone_info.nr_zones - 1,
775			  sdkp->zone_info.zone_blocks);
776	else
777		sd_printk(KERN_NOTICE, sdkp,
778			  "%u zones of %u logical blocks\n",
779			  sdkp->zone_info.nr_zones,
780			  sdkp->zone_info.zone_blocks);
781}
782
783static int sd_zbc_init_disk(struct scsi_disk *sdkp)
 
 
 
 
 
 
784{
785	sdkp->zones_wp_offset = NULL;
786	spin_lock_init(&sdkp->zones_wp_offset_lock);
787	sdkp->rev_wp_offset = NULL;
788	mutex_init(&sdkp->rev_mutex);
789	INIT_WORK(&sdkp->zone_wp_offset_work, sd_zbc_update_wp_offset_workfn);
790	sdkp->zone_wp_update_buf = kzalloc(SD_BUF_SIZE, GFP_KERNEL);
791	if (!sdkp->zone_wp_update_buf)
792		return -ENOMEM;
793
794	return 0;
795}
796
797void sd_zbc_free_zone_info(struct scsi_disk *sdkp)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
798{
799	if (!sdkp->zone_wp_update_buf)
800		return;
801
802	/* Serialize against revalidate zones */
803	mutex_lock(&sdkp->rev_mutex);
804
805	kvfree(sdkp->zones_wp_offset);
806	sdkp->zones_wp_offset = NULL;
807	kfree(sdkp->zone_wp_update_buf);
808	sdkp->zone_wp_update_buf = NULL;
809
810	sdkp->early_zone_info = (struct zoned_disk_info){ };
811	sdkp->zone_info = (struct zoned_disk_info){ };
 
 
 
 
 
 
 
 
 
812
813	mutex_unlock(&sdkp->rev_mutex);
814}
815
816static void sd_zbc_revalidate_zones_cb(struct gendisk *disk)
 
 
 
 
 
 
 
 
 
 
817{
818	struct scsi_disk *sdkp = scsi_disk(disk);
 
 
 
 
819
820	swap(sdkp->zones_wp_offset, sdkp->rev_wp_offset);
821}
 
822
823/*
824 * Call blk_revalidate_disk_zones() if any of the zoned disk properties have
825 * changed that make it necessary to call that function. Called by
826 * sd_revalidate_disk() after the gendisk capacity has been set.
827 */
828int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
829{
830	struct gendisk *disk = sdkp->disk;
831	struct request_queue *q = disk->queue;
832	u32 zone_blocks = sdkp->early_zone_info.zone_blocks;
833	unsigned int nr_zones = sdkp->early_zone_info.nr_zones;
834	int ret = 0;
835	unsigned int flags;
836
837	/*
838	 * For all zoned disks, initialize zone append emulation data if not
839	 * already done.
840	 */
841	if (sd_is_zoned(sdkp) && !sdkp->zone_wp_update_buf) {
842		ret = sd_zbc_init_disk(sdkp);
843		if (ret)
844			return ret;
 
 
845	}
846
847	/*
848	 * There is nothing to do for regular disks, including host-aware disks
849	 * that have partitions.
850	 */
851	if (!blk_queue_is_zoned(q))
852		return 0;
853
854	/*
855	 * Make sure revalidate zones are serialized to ensure exclusive
856	 * updates of the scsi disk data.
857	 */
858	mutex_lock(&sdkp->rev_mutex);
859
860	if (sdkp->zone_info.zone_blocks == zone_blocks &&
861	    sdkp->zone_info.nr_zones == nr_zones &&
862	    disk->nr_zones == nr_zones)
863		goto unlock;
864
865	flags = memalloc_noio_save();
866	sdkp->zone_info.zone_blocks = zone_blocks;
867	sdkp->zone_info.nr_zones = nr_zones;
868	sdkp->rev_wp_offset = kvcalloc(nr_zones, sizeof(u32), GFP_KERNEL);
869	if (!sdkp->rev_wp_offset) {
870		ret = -ENOMEM;
871		memalloc_noio_restore(flags);
872		goto unlock;
873	}
 
 
 
 
 
 
 
 
 
 
 
 
874
 
 
 
 
 
 
 
 
 
 
 
875	blk_queue_chunk_sectors(q,
876			logical_to_sectors(sdkp->device, zone_blocks));
877	blk_queue_max_zone_append_sectors(q,
878			q->limits.max_segments << PAGE_SECTORS_SHIFT);
879
880	ret = blk_revalidate_disk_zones(disk, sd_zbc_revalidate_zones_cb);
881
882	memalloc_noio_restore(flags);
883	kvfree(sdkp->rev_wp_offset);
884	sdkp->rev_wp_offset = NULL;
 
 
 
 
 
 
 
 
 
 
885
886	if (ret) {
887		sdkp->zone_info = (struct zoned_disk_info){ };
888		sdkp->capacity = 0;
889		goto unlock;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
890	}
891
892	sd_zbc_print_zones(sdkp);
893
894unlock:
895	mutex_unlock(&sdkp->rev_mutex);
896
 
 
897	return ret;
898}
899
900/**
901 * sd_zbc_read_zones - Read zone information and update the request queue
902 * @sdkp: SCSI disk pointer.
903 * @buf: 512 byte buffer used for storing SCSI command output.
904 *
905 * Read zone information and update the request queue zone characteristics and
906 * also the zoned device information in *sdkp. Called by sd_revalidate_disk()
907 * before the gendisk capacity has been set.
908 */
909int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE])
910{
911	struct gendisk *disk = sdkp->disk;
912	struct request_queue *q = disk->queue;
913	unsigned int nr_zones;
914	u32 zone_blocks = 0;
915	int ret;
916
917	if (!sd_is_zoned(sdkp)) {
918		/*
919		 * Device managed or normal SCSI disk, no special handling
920		 * required. Nevertheless, free the disk zone information in
921		 * case the device type changed.
922		 */
923		sd_zbc_free_zone_info(sdkp);
924		return 0;
925	}
926
927	/* READ16/WRITE16/SYNC16 is mandatory for ZBC devices */
928	sdkp->device->use_16_for_rw = 1;
929	sdkp->device->use_10_for_rw = 0;
930	sdkp->device->use_16_for_sync = 1;
931
932	/* Check zoned block device characteristics (unconstrained reads) */
933	ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
 
 
 
 
 
 
 
 
 
 
 
 
 
934	if (ret)
935		goto err;
936
937	/* Check the device capacity reported by report zones */
938	ret = sd_zbc_check_capacity(sdkp, buf, &zone_blocks);
939	if (ret != 0)
 
 
 
 
 
 
 
940		goto err;
941
942	/* The drive satisfies the kernel restrictions: set it up */
943	blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
944	blk_queue_required_elevator_features(q, ELEVATOR_F_ZBD_SEQ_WRITE);
945	if (sdkp->zones_max_open == U32_MAX)
946		disk_set_max_open_zones(disk, 0);
947	else
948		disk_set_max_open_zones(disk, sdkp->zones_max_open);
949	disk_set_max_active_zones(disk, 0);
950	nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
951
952	sdkp->early_zone_info.nr_zones = nr_zones;
953	sdkp->early_zone_info.zone_blocks = zone_blocks;
954
955	return 0;
956
957err:
958	sdkp->capacity = 0;
 
959
960	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
961}
v4.17
 
  1/*
  2 * SCSI Zoned Block commands
  3 *
  4 * Copyright (C) 2014-2015 SUSE Linux GmbH
  5 * Written by: Hannes Reinecke <hare@suse.de>
  6 * Modified by: Damien Le Moal <damien.lemoal@hgst.com>
  7 * Modified by: Shaun Tancheff <shaun.tancheff@seagate.com>
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License version
 11 * 2 as published by the Free Software Foundation.
 12 *
 13 * This program is distributed in the hope that it will be useful, but
 14 * WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 * General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; see the file COPYING.  If not, write to
 20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
 21 * USA.
 22 *
 23 */
 24
 25#include <linux/blkdev.h>
 
 
 
 26
 27#include <asm/unaligned.h>
 28
 29#include <scsi/scsi.h>
 30#include <scsi/scsi_cmnd.h>
 31
 32#include "sd.h"
 33
 
 
 
 34/**
 35 * sd_zbc_parse_report - Convert a zone descriptor to a struct blk_zone,
 36 * @sdkp: The disk the report originated from
 37 * @buf: Address of the report zone descriptor
 38 * @zone: the destination zone structure
 39 *
 40 * All LBA sized values are converted to 512B sectors unit.
 41 */
 42static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
 43				struct blk_zone *zone)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 44{
 45	struct scsi_device *sdp = sdkp->device;
 
 
 
 46
 47	memset(zone, 0, sizeof(struct blk_zone));
 
 48
 49	zone->type = buf[0] & 0x0f;
 50	zone->cond = (buf[1] >> 4) & 0xf;
 51	if (buf[1] & 0x01)
 52		zone->reset = 1;
 53	if (buf[1] & 0x02)
 54		zone->non_seq = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 55
 56	zone->len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
 57	zone->start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
 58	zone->wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
 59	if (zone->type != ZBC_ZONE_TYPE_CONV &&
 60	    zone->cond == ZBC_ZONE_COND_FULL)
 61		zone->wp = zone->start + zone->len;
 62}
 63
 64/**
 65 * sd_zbc_report_zones - Issue a REPORT ZONES scsi command.
 66 * @sdkp: The target disk
 67 * @buf: Buffer to use for the reply
 68 * @buflen: the buffer size
 69 * @lba: Start LBA of the report
 
 70 *
 71 * For internal use during device validation.
 72 */
 73static int sd_zbc_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
 74			       unsigned int buflen, sector_t lba)
 
 
 
 
 
 75{
 76	struct scsi_device *sdp = sdkp->device;
 77	const int timeout = sdp->request_queue->rq_timeout;
 78	struct scsi_sense_hdr sshdr;
 
 
 
 79	unsigned char cmd[16];
 80	unsigned int rep_len;
 81	int result;
 82
 83	memset(cmd, 0, 16);
 84	cmd[0] = ZBC_IN;
 85	cmd[1] = ZI_REPORT_ZONES;
 86	put_unaligned_be64(lba, &cmd[2]);
 87	put_unaligned_be32(buflen, &cmd[10]);
 88	memset(buf, 0, buflen);
 
 89
 90	result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
 91				  buf, buflen, &sshdr,
 92				  timeout, SD_MAX_RETRIES, NULL);
 93	if (result) {
 94		sd_printk(KERN_ERR, sdkp,
 95			  "REPORT ZONES lba %llu failed with %d/%d\n",
 96			  (unsigned long long)lba,
 97			  host_byte(result), driver_byte(result));
 
 98		return -EIO;
 99	}
100
101	rep_len = get_unaligned_be32(&buf[0]);
102	if (rep_len < 64) {
103		sd_printk(KERN_ERR, sdkp,
104			  "REPORT ZONES report invalid length %u\n",
105			  rep_len);
106		return -EIO;
107	}
108
109	return 0;
110}
111
112/**
113 * sd_zbc_setup_report_cmnd - Prepare a REPORT ZONES scsi command
114 * @cmd: The command to setup
 
 
 
 
 
 
115 *
116 * Call in sd_init_command() for a REQ_OP_ZONE_REPORT request.
 
117 */
118int sd_zbc_setup_report_cmnd(struct scsi_cmnd *cmd)
 
119{
120	struct request *rq = cmd->request;
121	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
122	sector_t lba, sector = blk_rq_pos(rq);
123	unsigned int nr_bytes = blk_rq_bytes(rq);
124	int ret;
125
126	WARN_ON(nr_bytes == 0);
127
128	if (!sd_is_zoned(sdkp))
129		/* Not a zoned device */
130		return BLKPREP_KILL;
131
132	ret = scsi_init_io(cmd);
133	if (ret != BLKPREP_OK)
134		return ret;
135
136	cmd->cmd_len = 16;
137	memset(cmd->cmnd, 0, cmd->cmd_len);
138	cmd->cmnd[0] = ZBC_IN;
139	cmd->cmnd[1] = ZI_REPORT_ZONES;
140	lba = sectors_to_logical(sdkp->device, sector);
141	put_unaligned_be64(lba, &cmd->cmnd[2]);
142	put_unaligned_be32(nr_bytes, &cmd->cmnd[10]);
143	/* Do partial report for speeding things up */
144	cmd->cmnd[14] = ZBC_REPORT_ZONE_PARTIAL;
145
146	cmd->sc_data_direction = DMA_FROM_DEVICE;
147	cmd->sdb.length = nr_bytes;
148	cmd->transfersize = sdkp->device->sector_size;
149	cmd->allowed = 0;
150
151	/*
152	 * Report may return less bytes than requested. Make sure
153	 * to report completion on the entire initial request.
 
 
 
 
 
154	 */
155	rq->__data_len = nr_bytes;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
157	return BLKPREP_OK;
 
 
 
 
 
 
158}
159
160/**
161 * sd_zbc_report_zones_complete - Process a REPORT ZONES scsi command reply.
162 * @scmd: The completed report zones command
163 * @good_bytes: reply size in bytes
 
 
 
164 *
165 * Convert all reported zone descriptors to struct blk_zone. The conversion
166 * is done in-place, directly in the request specified sg buffer.
167 */
168static void sd_zbc_report_zones_complete(struct scsi_cmnd *scmd,
169					 unsigned int good_bytes)
170{
171	struct request *rq = scmd->request;
172	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
173	struct sg_mapping_iter miter;
174	struct blk_zone_report_hdr hdr;
175	struct blk_zone zone;
176	unsigned int offset, bytes = 0;
177	unsigned long flags;
178	u8 *buf;
179
180	if (good_bytes < 64)
181		return;
 
182
183	memset(&hdr, 0, sizeof(struct blk_zone_report_hdr));
 
 
184
185	sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
186		       SG_MITER_TO_SG | SG_MITER_ATOMIC);
 
187
188	local_irq_save(flags);
189	while (sg_miter_next(&miter) && bytes < good_bytes) {
 
 
190
191		buf = miter.addr;
192		offset = 0;
 
 
 
193
194		if (bytes == 0) {
195			/* Set the report header */
196			hdr.nr_zones = min_t(unsigned int,
197					 (good_bytes - 64) / 64,
198					 get_unaligned_be32(&buf[0]) / 64);
199			memcpy(buf, &hdr, sizeof(struct blk_zone_report_hdr));
200			offset += 64;
201			bytes += 64;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202		}
 
 
 
 
 
 
 
 
 
 
 
 
 
203
204		/* Parse zone descriptors */
205		while (offset < miter.length && hdr.nr_zones) {
206			WARN_ON(offset > miter.length);
207			buf = miter.addr + offset;
208			sd_zbc_parse_report(sdkp, buf, &zone);
209			memcpy(buf, &zone, sizeof(struct blk_zone));
210			offset += 64;
211			bytes += 64;
212			hdr.nr_zones--;
213		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
215		if (!hdr.nr_zones)
216			break;
 
 
217
 
 
 
 
 
 
 
 
 
218	}
219	sg_miter_stop(&miter);
220	local_irq_restore(flags);
 
221}
222
223/**
224 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
225 * @sdkp: The target disk
226 */
227static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
228{
229	return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230}
231
232/**
233 * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command.
 
234 * @cmd: the command to setup
 
 
235 *
236 * Called from sd_init_command() for a REQ_OP_ZONE_RESET request.
 
237 */
238int sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd)
 
239{
240	struct request *rq = cmd->request;
241	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
242	sector_t sector = blk_rq_pos(rq);
 
243	sector_t block = sectors_to_logical(sdkp->device, sector);
 
244
245	if (!sd_is_zoned(sdkp))
246		/* Not a zoned device */
247		return BLKPREP_KILL;
248
249	if (sdkp->device->changed)
250		return BLKPREP_KILL;
251
252	if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
253		/* Unaligned request */
254		return BLKPREP_KILL;
255
256	cmd->cmd_len = 16;
257	memset(cmd->cmnd, 0, cmd->cmd_len);
258	cmd->cmnd[0] = ZBC_OUT;
259	cmd->cmnd[1] = ZO_RESET_WRITE_POINTER;
260	put_unaligned_be64(block, &cmd->cmnd[2]);
 
 
 
261
262	rq->timeout = SD_TIMEOUT;
263	cmd->sc_data_direction = DMA_NONE;
264	cmd->transfersize = 0;
265	cmd->allowed = 0;
266
267	return BLKPREP_OK;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268}
269
270/**
271 * sd_zbc_complete - ZBC command post processing.
272 * @cmd: Completed command
273 * @good_bytes: Command reply bytes
274 * @sshdr: command sense header
275 *
276 * Called from sd_done(). Process report zones reply and handle reset zone
277 * and write commands errors.
278 */
279void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
280		     struct scsi_sense_hdr *sshdr)
281{
282	int result = cmd->result;
283	struct request *rq = cmd->request;
 
 
 
 
 
 
 
 
 
 
 
 
284
285	switch (req_op(rq)) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286	case REQ_OP_ZONE_RESET:
 
 
 
 
 
 
 
 
 
 
 
 
287
288		if (result &&
289		    sshdr->sense_key == ILLEGAL_REQUEST &&
290		    sshdr->asc == 0x24)
291			/*
292			 * INVALID FIELD IN CDB error: reset of a conventional
293			 * zone was attempted. Nothing to worry about, so be
294			 * quiet about the error.
295			 */
296			rq->rq_flags |= RQF_QUIET;
297		break;
298
299	case REQ_OP_WRITE:
300	case REQ_OP_WRITE_ZEROES:
301	case REQ_OP_WRITE_SAME:
302
303		if (result &&
304		    sshdr->sense_key == ILLEGAL_REQUEST &&
305		    sshdr->asc == 0x21)
306			/*
307			 * INVALID ADDRESS FOR WRITE error: It is unlikely that
308			 * retrying write requests failed with any kind of
309			 * alignement error will result in success. So don't.
310			 */
311			cmd->allowed = 0;
312		break;
 
 
 
 
313
314	case REQ_OP_ZONE_REPORT:
 
 
 
 
 
 
 
 
 
 
 
315
316		if (!result)
317			sd_zbc_report_zones_complete(cmd, good_bytes);
318		break;
319
320	}
321}
322
323/**
324 * sd_zbc_read_zoned_characteristics - Read zoned block device characteristics
325 * @sdkp: Target disk
326 * @buf: Buffer where to store the VPD page data
327 *
328 * Read VPD page B6.
329 */
330static int sd_zbc_read_zoned_characteristics(struct scsi_disk *sdkp,
331					     unsigned char *buf)
332{
 
333
334	if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
335		sd_printk(KERN_NOTICE, sdkp,
336			  "Unconstrained-read check failed\n");
337		return -ENODEV;
338	}
339
340	if (sdkp->device->type != TYPE_ZBC) {
341		/* Host-aware */
342		sdkp->urswrz = 1;
343		sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
344		sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
345		sdkp->zones_max_open = 0;
346	} else {
347		/* Host-managed */
348		sdkp->urswrz = buf[4] & 1;
349		sdkp->zones_optimal_open = 0;
350		sdkp->zones_optimal_nonseq = 0;
351		sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
352	}
353
354	return 0;
355}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
357/**
358 * sd_zbc_check_capacity - Check reported capacity.
359 * @sdkp: Target disk
360 * @buf: Buffer to use for commands
361 *
362 * ZBC drive may report only the capacity of the first conventional zones at
363 * LBA 0. This is indicated by the RC_BASIS field of the read capacity reply.
364 * Check this here. If the disk reported only its conventional zones capacity,
365 * get the total capacity by doing a report zones.
366 */
367static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf)
368{
369	sector_t lba;
370	int ret;
371
372	if (sdkp->rc_basis != 0)
373		return 0;
374
375	/* Do a report zone to get the maximum LBA to check capacity */
376	ret = sd_zbc_report_zones(sdkp, buf, SD_BUF_SIZE, 0);
377	if (ret)
378		return ret;
379
380	/* The max_lba field is the capacity of this device */
381	lba = get_unaligned_be64(&buf[8]);
382	if (lba + 1 == sdkp->capacity)
383		return 0;
384
385	if (sdkp->first_scan)
386		sd_printk(KERN_WARNING, sdkp,
387			  "Changing capacity from %llu to max LBA+1 %llu\n",
388			  (unsigned long long)sdkp->capacity,
389			  (unsigned long long)lba + 1);
390	sdkp->capacity = lba + 1;
391
392	return 0;
393}
394
395#define SD_ZBC_BUF_SIZE 131072U
396
397/**
398 * sd_zbc_check_zone_size - Check the device zone sizes
399 * @sdkp: Target disk
 
 
400 *
401 * Check that all zones of the device are equal. The last zone can however
402 * be smaller. The zone size must also be a power of two number of LBAs.
 
403 *
404 * Returns the zone size in bytes upon success or an error code upon failure.
405 */
406static s64 sd_zbc_check_zone_size(struct scsi_disk *sdkp)
 
407{
408	u64 zone_blocks = 0;
409	sector_t block = 0;
410	unsigned char *buf;
411	unsigned char *rec;
412	unsigned int buf_len;
413	unsigned int list_length;
414	int ret;
415	u8 same;
416
417	/* Get a buffer */
418	buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
419	if (!buf)
420		return -ENOMEM;
421
422	/* Do a report zone to get the same field */
423	ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0);
424	if (ret)
425		goto out_free;
426
427	same = buf[4] & 0x0f;
428	if (same > 0) {
429		rec = &buf[64];
430		zone_blocks = get_unaligned_be64(&rec[8]);
431		goto out;
 
 
 
 
 
 
432	}
433
434	/*
435	 * Check the size of all zones: all zones must be of
436	 * equal size, except the last zone which can be smaller
437	 * than other zones.
438	 */
439	do {
440
441		/* Parse REPORT ZONES header */
442		list_length = get_unaligned_be32(&buf[0]) + 64;
443		rec = buf + 64;
444		buf_len = min(list_length, SD_ZBC_BUF_SIZE);
445
446		/* Parse zone descriptors */
447		while (rec < buf + buf_len) {
448			u64 this_zone_blocks = get_unaligned_be64(&rec[8]);
449
450			if (zone_blocks == 0) {
451				zone_blocks = this_zone_blocks;
452			} else if (this_zone_blocks != zone_blocks &&
453				   (block + this_zone_blocks < sdkp->capacity
454				    || this_zone_blocks > zone_blocks)) {
455				this_zone_blocks = 0;
456				goto out;
457			}
458			block += this_zone_blocks;
459			rec += 64;
460		}
 
 
 
461
462		if (block < sdkp->capacity) {
463			ret = sd_zbc_report_zones(sdkp, buf,
464						  SD_ZBC_BUF_SIZE, block);
465			if (ret)
466				goto out_free;
467		}
468
469	} while (block < sdkp->capacity);
470
471out:
472	if (!zone_blocks) {
473		if (sdkp->first_scan)
474			sd_printk(KERN_NOTICE, sdkp,
475				  "Devices with non constant zone "
476				  "size are not supported\n");
477		ret = -ENODEV;
478	} else if (!is_power_of_2(zone_blocks)) {
479		if (sdkp->first_scan)
480			sd_printk(KERN_NOTICE, sdkp,
481				  "Devices with non power of 2 zone "
482				  "size are not supported\n");
483		ret = -ENODEV;
484	} else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
485		if (sdkp->first_scan)
486			sd_printk(KERN_NOTICE, sdkp,
487				  "Zone size too large\n");
488		ret = -ENODEV;
489	} else {
490		ret = zone_blocks;
491	}
492
493out_free:
494	kfree(buf);
 
 
495
496	return ret;
 
 
 
 
 
 
 
 
 
497}
498
499/**
500 * sd_zbc_alloc_zone_bitmap - Allocate a zone bitmap (one bit per zone).
501 * @nr_zones: Number of zones to allocate space for.
502 * @numa_node: NUMA node to allocate the memory from.
503 */
504static inline unsigned long *
505sd_zbc_alloc_zone_bitmap(u32 nr_zones, int numa_node)
506{
507	return kzalloc_node(BITS_TO_LONGS(nr_zones) * sizeof(unsigned long),
508			    GFP_KERNEL, numa_node);
 
 
 
 
 
 
 
 
509}
510
511/**
512 * sd_zbc_get_seq_zones - Parse report zones reply to identify sequential zones
513 * @sdkp: disk used
514 * @buf: report reply buffer
515 * @buflen: length of @buf
516 * @zone_shift: logarithm base 2 of the number of blocks in a zone
517 * @seq_zones_bitmap: bitmap of sequential zones to set
518 *
519 * Parse reported zone descriptors in @buf to identify sequential zones and
520 * set the reported zone bit in @seq_zones_bitmap accordingly.
521 * Since read-only and offline zones cannot be written, do not
522 * mark them as sequential in the bitmap.
523 * Return the LBA after the last zone reported.
524 */
525static sector_t sd_zbc_get_seq_zones(struct scsi_disk *sdkp, unsigned char *buf,
526				     unsigned int buflen, u32 zone_shift,
527				     unsigned long *seq_zones_bitmap)
528{
529	sector_t lba, next_lba = sdkp->capacity;
530	unsigned int buf_len, list_length;
531	unsigned char *rec;
532	u8 type, cond;
 
533
534	list_length = get_unaligned_be32(&buf[0]) + 64;
535	buf_len = min(list_length, buflen);
536	rec = buf + 64;
 
537
538	while (rec < buf + buf_len) {
539		type = rec[0] & 0x0f;
540		cond = (rec[1] >> 4) & 0xf;
541		lba = get_unaligned_be64(&rec[16]);
542		if (type != ZBC_ZONE_TYPE_CONV &&
543		    cond != ZBC_ZONE_COND_READONLY &&
544		    cond != ZBC_ZONE_COND_OFFLINE)
545			set_bit(lba >> zone_shift, seq_zones_bitmap);
546		next_lba = lba + get_unaligned_be64(&rec[8]);
547		rec += 64;
548	}
549
550	return next_lba;
551}
552
553/**
554 * sd_zbc_setup_seq_zones_bitmap - Initialize a seq zone bitmap.
555 * @sdkp: target disk
556 * @zone_shift: logarithm base 2 of the number of blocks in a zone
557 * @nr_zones: number of zones to set up a seq zone bitmap for
558 *
559 * Allocate a zone bitmap and initialize it by identifying sequential zones.
560 */
561static unsigned long *
562sd_zbc_setup_seq_zones_bitmap(struct scsi_disk *sdkp, u32 zone_shift,
563			      u32 nr_zones)
564{
565	struct request_queue *q = sdkp->disk->queue;
566	unsigned long *seq_zones_bitmap;
567	sector_t lba = 0;
568	unsigned char *buf;
569	int ret = -ENOMEM;
570
571	seq_zones_bitmap = sd_zbc_alloc_zone_bitmap(nr_zones, q->node);
572	if (!seq_zones_bitmap)
573		return ERR_PTR(-ENOMEM);
574
575	buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
576	if (!buf)
577		goto out;
 
 
 
 
 
 
 
 
 
 
578
579	while (lba < sdkp->capacity) {
580		ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, lba);
 
 
 
 
581		if (ret)
582			goto out;
583		lba = sd_zbc_get_seq_zones(sdkp, buf, SD_ZBC_BUF_SIZE,
584					   zone_shift, seq_zones_bitmap);
585	}
586
587	if (lba != sdkp->capacity) {
588		/* Something went wrong */
589		ret = -EIO;
590	}
 
 
 
 
 
 
 
 
591
592out:
593	kfree(buf);
594	if (ret) {
595		kfree(seq_zones_bitmap);
596		return ERR_PTR(ret);
 
 
 
 
 
 
 
 
597	}
598	return seq_zones_bitmap;
599}
600
601static void sd_zbc_cleanup(struct scsi_disk *sdkp)
602{
603	struct request_queue *q = sdkp->disk->queue;
604
605	kfree(q->seq_zones_bitmap);
606	q->seq_zones_bitmap = NULL;
607
608	kfree(q->seq_zones_wlock);
609	q->seq_zones_wlock = NULL;
610
611	q->nr_zones = 0;
612}
613
614static int sd_zbc_setup(struct scsi_disk *sdkp, u32 zone_blocks)
615{
616	struct request_queue *q = sdkp->disk->queue;
617	u32 zone_shift = ilog2(zone_blocks);
618	u32 nr_zones;
619	int ret;
620
621	/* chunk_sectors indicates the zone size */
622	blk_queue_chunk_sectors(q,
623			logical_to_sectors(sdkp->device, zone_blocks));
624	nr_zones = round_up(sdkp->capacity, zone_blocks) >> zone_shift;
 
625
626	/*
627	 * Initialize the device request queue information if the number
628	 * of zones changed.
629	 */
630	if (nr_zones != sdkp->nr_zones || nr_zones != q->nr_zones) {
631		unsigned long *seq_zones_wlock = NULL, *seq_zones_bitmap = NULL;
632		size_t zone_bitmap_size;
633
634		if (nr_zones) {
635			seq_zones_wlock = sd_zbc_alloc_zone_bitmap(nr_zones,
636								   q->node);
637			if (!seq_zones_wlock) {
638				ret = -ENOMEM;
639				goto err;
640			}
641
642			seq_zones_bitmap = sd_zbc_setup_seq_zones_bitmap(sdkp,
643							zone_shift, nr_zones);
644			if (IS_ERR(seq_zones_bitmap)) {
645				ret = PTR_ERR(seq_zones_bitmap);
646				kfree(seq_zones_wlock);
647				goto err;
648			}
649		}
650		zone_bitmap_size = BITS_TO_LONGS(nr_zones) *
651			sizeof(unsigned long);
652		blk_mq_freeze_queue(q);
653		if (q->nr_zones != nr_zones) {
654			/* READ16/WRITE16 is mandatory for ZBC disks */
655			sdkp->device->use_16_for_rw = 1;
656			sdkp->device->use_10_for_rw = 0;
657
658			sdkp->zone_blocks = zone_blocks;
659			sdkp->zone_shift = zone_shift;
660			sdkp->nr_zones = nr_zones;
661			q->nr_zones = nr_zones;
662			swap(q->seq_zones_wlock, seq_zones_wlock);
663			swap(q->seq_zones_bitmap, seq_zones_bitmap);
664		} else if (memcmp(q->seq_zones_bitmap, seq_zones_bitmap,
665				  zone_bitmap_size) != 0) {
666			memcpy(q->seq_zones_bitmap, seq_zones_bitmap,
667			       zone_bitmap_size);
668		}
669		blk_mq_unfreeze_queue(q);
670		kfree(seq_zones_wlock);
671		kfree(seq_zones_bitmap);
672	}
673
674	return 0;
 
 
 
675
676err:
677	sd_zbc_cleanup(sdkp);
678	return ret;
679}
680
681int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
682{
683	int64_t zone_blocks;
 
 
 
 
 
 
 
 
 
 
 
 
684	int ret;
685
686	if (!sd_is_zoned(sdkp))
687		/*
688		 * Device managed or normal SCSI disk,
689		 * no special handling required
 
690		 */
 
691		return 0;
 
692
693	/* Get zoned block device characteristics */
694	ret = sd_zbc_read_zoned_characteristics(sdkp, buf);
695	if (ret)
696		goto err;
697
698	/*
699	 * Check for unconstrained reads: host-managed devices with
700	 * constrained reads (drives failing read after write pointer)
701	 * are not supported.
702	 */
703	if (!sdkp->urswrz) {
704		if (sdkp->first_scan)
705			sd_printk(KERN_NOTICE, sdkp,
706			  "constrained reads devices are not supported\n");
707		ret = -ENODEV;
708		goto err;
709	}
710
711	/* Check capacity */
712	ret = sd_zbc_check_capacity(sdkp, buf);
713	if (ret)
714		goto err;
715
716	/*
717	 * Check zone size: only devices with a constant zone size (except
718	 * an eventual last runt zone) that is a power of 2 are supported.
719	 */
720	zone_blocks = sd_zbc_check_zone_size(sdkp);
721	ret = -EFBIG;
722	if (zone_blocks != (u32)zone_blocks)
723		goto err;
724	ret = zone_blocks;
725	if (ret < 0)
726		goto err;
727
728	/* The drive satisfies the kernel restrictions: set it up */
729	ret = sd_zbc_setup(sdkp, zone_blocks);
730	if (ret)
731		goto err;
 
 
 
 
 
 
 
 
732
733	return 0;
734
735err:
736	sdkp->capacity = 0;
737	sd_zbc_cleanup(sdkp);
738
739	return ret;
740}
741
742void sd_zbc_remove(struct scsi_disk *sdkp)
743{
744	sd_zbc_cleanup(sdkp);
745}
746
747void sd_zbc_print_zones(struct scsi_disk *sdkp)
748{
749	if (!sd_is_zoned(sdkp) || !sdkp->capacity)
750		return;
751
752	if (sdkp->capacity & (sdkp->zone_blocks - 1))
753		sd_printk(KERN_NOTICE, sdkp,
754			  "%u zones of %u logical blocks + 1 runt zone\n",
755			  sdkp->nr_zones - 1,
756			  sdkp->zone_blocks);
757	else
758		sd_printk(KERN_NOTICE, sdkp,
759			  "%u zones of %u logical blocks\n",
760			  sdkp->nr_zones,
761			  sdkp->zone_blocks);
762}