Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  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#include <scsi/scsi_dbg.h>
 32#include <scsi/scsi_device.h>
 33#include <scsi/scsi_driver.h>
 34#include <scsi/scsi_host.h>
 35#include <scsi/scsi_eh.h>
 36
 37#include "sd.h"
 38#include "scsi_priv.h"
 39
 40enum zbc_zone_type {
 41	ZBC_ZONE_TYPE_CONV = 0x1,
 42	ZBC_ZONE_TYPE_SEQWRITE_REQ,
 43	ZBC_ZONE_TYPE_SEQWRITE_PREF,
 44	ZBC_ZONE_TYPE_RESERVED,
 45};
 46
 47enum zbc_zone_cond {
 48	ZBC_ZONE_COND_NO_WP,
 49	ZBC_ZONE_COND_EMPTY,
 50	ZBC_ZONE_COND_IMP_OPEN,
 51	ZBC_ZONE_COND_EXP_OPEN,
 52	ZBC_ZONE_COND_CLOSED,
 53	ZBC_ZONE_COND_READONLY = 0xd,
 54	ZBC_ZONE_COND_FULL,
 55	ZBC_ZONE_COND_OFFLINE,
 56};
 57
 58/**
 59 * Convert a zone descriptor to a zone struct.
 60 */
 61static void sd_zbc_parse_report(struct scsi_disk *sdkp,
 62				u8 *buf,
 63				struct blk_zone *zone)
 64{
 65	struct scsi_device *sdp = sdkp->device;
 66
 67	memset(zone, 0, sizeof(struct blk_zone));
 68
 69	zone->type = buf[0] & 0x0f;
 70	zone->cond = (buf[1] >> 4) & 0xf;
 71	if (buf[1] & 0x01)
 72		zone->reset = 1;
 73	if (buf[1] & 0x02)
 74		zone->non_seq = 1;
 75
 76	zone->len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
 77	zone->start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
 78	zone->wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
 79	if (zone->type != ZBC_ZONE_TYPE_CONV &&
 80	    zone->cond == ZBC_ZONE_COND_FULL)
 81		zone->wp = zone->start + zone->len;
 82}
 83
 84/**
 85 * Issue a REPORT ZONES scsi command.
 86 */
 87static int sd_zbc_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
 88			       unsigned int buflen, sector_t lba)
 89{
 90	struct scsi_device *sdp = sdkp->device;
 91	const int timeout = sdp->request_queue->rq_timeout;
 92	struct scsi_sense_hdr sshdr;
 93	unsigned char cmd[16];
 94	unsigned int rep_len;
 95	int result;
 96
 97	memset(cmd, 0, 16);
 98	cmd[0] = ZBC_IN;
 99	cmd[1] = ZI_REPORT_ZONES;
100	put_unaligned_be64(lba, &cmd[2]);
101	put_unaligned_be32(buflen, &cmd[10]);
102	memset(buf, 0, buflen);
103
104	result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
105				  buf, buflen, &sshdr,
106				  timeout, SD_MAX_RETRIES, NULL);
107	if (result) {
108		sd_printk(KERN_ERR, sdkp,
109			  "REPORT ZONES lba %llu failed with %d/%d\n",
110			  (unsigned long long)lba,
111			  host_byte(result), driver_byte(result));
112		return -EIO;
113	}
114
115	rep_len = get_unaligned_be32(&buf[0]);
116	if (rep_len < 64) {
117		sd_printk(KERN_ERR, sdkp,
118			  "REPORT ZONES report invalid length %u\n",
119			  rep_len);
120		return -EIO;
121	}
122
123	return 0;
124}
125
126int sd_zbc_setup_report_cmnd(struct scsi_cmnd *cmd)
127{
128	struct request *rq = cmd->request;
129	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
130	sector_t lba, sector = blk_rq_pos(rq);
131	unsigned int nr_bytes = blk_rq_bytes(rq);
132	int ret;
133
134	WARN_ON(nr_bytes == 0);
135
136	if (!sd_is_zoned(sdkp))
137		/* Not a zoned device */
138		return BLKPREP_KILL;
139
140	ret = scsi_init_io(cmd);
141	if (ret != BLKPREP_OK)
142		return ret;
143
144	cmd->cmd_len = 16;
145	memset(cmd->cmnd, 0, cmd->cmd_len);
146	cmd->cmnd[0] = ZBC_IN;
147	cmd->cmnd[1] = ZI_REPORT_ZONES;
148	lba = sectors_to_logical(sdkp->device, sector);
149	put_unaligned_be64(lba, &cmd->cmnd[2]);
150	put_unaligned_be32(nr_bytes, &cmd->cmnd[10]);
151	/* Do partial report for speeding things up */
152	cmd->cmnd[14] = ZBC_REPORT_ZONE_PARTIAL;
153
154	cmd->sc_data_direction = DMA_FROM_DEVICE;
155	cmd->sdb.length = nr_bytes;
156	cmd->transfersize = sdkp->device->sector_size;
157	cmd->allowed = 0;
158
159	/*
160	 * Report may return less bytes than requested. Make sure
161	 * to report completion on the entire initial request.
162	 */
163	rq->__data_len = nr_bytes;
164
165	return BLKPREP_OK;
166}
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
223static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
224{
225	return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
226}
227
228static inline unsigned int sd_zbc_zone_no(struct scsi_disk *sdkp,
229					  sector_t sector)
230{
231	return sectors_to_logical(sdkp->device, sector) >> sdkp->zone_shift;
232}
233
234int sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd)
235{
236	struct request *rq = cmd->request;
237	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
238	sector_t sector = blk_rq_pos(rq);
239	sector_t block = sectors_to_logical(sdkp->device, sector);
240	unsigned int zno = block >> sdkp->zone_shift;
241
242	if (!sd_is_zoned(sdkp))
243		/* Not a zoned device */
244		return BLKPREP_KILL;
245
246	if (sdkp->device->changed)
247		return BLKPREP_KILL;
248
249	if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
250		/* Unaligned request */
251		return BLKPREP_KILL;
252
253	/* Do not allow concurrent reset and writes */
254	if (sdkp->zones_wlock &&
255	    test_and_set_bit(zno, sdkp->zones_wlock))
256		return BLKPREP_DEFER;
257
258	cmd->cmd_len = 16;
259	memset(cmd->cmnd, 0, cmd->cmd_len);
260	cmd->cmnd[0] = ZBC_OUT;
261	cmd->cmnd[1] = ZO_RESET_WRITE_POINTER;
262	put_unaligned_be64(block, &cmd->cmnd[2]);
263
264	rq->timeout = SD_TIMEOUT;
265	cmd->sc_data_direction = DMA_NONE;
266	cmd->transfersize = 0;
267	cmd->allowed = 0;
268
269	return BLKPREP_OK;
270}
271
272int sd_zbc_setup_write_cmnd(struct scsi_cmnd *cmd)
273{
274	struct request *rq = cmd->request;
275	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
276	sector_t sector = blk_rq_pos(rq);
277	sector_t zone_sectors = sd_zbc_zone_sectors(sdkp);
278	unsigned int zno = sd_zbc_zone_no(sdkp, sector);
279
280	/*
281	 * Note: Checks of the alignment of the write command on
282	 * logical blocks is done in sd.c
283	 */
284
285	/* Do not allow zone boundaries crossing on host-managed drives */
286	if (blk_queue_zoned_model(sdkp->disk->queue) == BLK_ZONED_HM &&
287	    (sector & (zone_sectors - 1)) + blk_rq_sectors(rq) > zone_sectors)
288		return BLKPREP_KILL;
289
290	/*
291	 * Do not issue more than one write at a time per
292	 * zone. This solves write ordering problems due to
293	 * the unlocking of the request queue in the dispatch
294	 * path in the non scsi-mq case. For scsi-mq, this
295	 * also avoids potential write reordering when multiple
296	 * threads running on different CPUs write to the same
297	 * zone (with a synchronized sequential pattern).
298	 */
299	if (sdkp->zones_wlock &&
300	    test_and_set_bit(zno, sdkp->zones_wlock))
301		return BLKPREP_DEFER;
302
303	return BLKPREP_OK;
304}
305
306static void sd_zbc_unlock_zone(struct request *rq)
307{
308	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
309
310	if (sdkp->zones_wlock) {
311		unsigned int zno = sd_zbc_zone_no(sdkp, blk_rq_pos(rq));
312		WARN_ON_ONCE(!test_bit(zno, sdkp->zones_wlock));
313		clear_bit_unlock(zno, sdkp->zones_wlock);
314		smp_mb__after_atomic();
315	}
316}
317
318void sd_zbc_cancel_write_cmnd(struct scsi_cmnd *cmd)
319{
320	sd_zbc_unlock_zone(cmd->request);
321}
322
323void sd_zbc_complete(struct scsi_cmnd *cmd,
324		     unsigned int good_bytes,
325		     struct scsi_sense_hdr *sshdr)
326{
327	int result = cmd->result;
328	struct request *rq = cmd->request;
329
330	switch (req_op(rq)) {
331	case REQ_OP_WRITE:
332	case REQ_OP_WRITE_SAME:
333	case REQ_OP_ZONE_RESET:
334
335		/* Unlock the zone */
336		sd_zbc_unlock_zone(rq);
337
338		if (!result ||
339		    sshdr->sense_key != ILLEGAL_REQUEST)
340			break;
341
342		switch (sshdr->asc) {
343		case 0x24:
344			/*
345			 * INVALID FIELD IN CDB error: For a zone reset,
346			 * this means that a reset of a conventional
347			 * zone was attempted. Nothing to worry about in
348			 * this case, so be quiet about the error.
349			 */
350			if (req_op(rq) == REQ_OP_ZONE_RESET)
351				rq->rq_flags |= RQF_QUIET;
352			break;
353		case 0x21:
354			/*
355			 * INVALID ADDRESS FOR WRITE error: It is unlikely that
356			 * retrying write requests failed with any kind of
357			 * alignement error will result in success. So don't.
358			 */
359			cmd->allowed = 0;
360			break;
361		}
362
363		break;
364
365	case REQ_OP_ZONE_REPORT:
366
367		if (!result)
368			sd_zbc_report_zones_complete(cmd, good_bytes);
369		break;
370
371	}
372}
373
374/**
375 * Read zoned block device characteristics (VPD page B6).
376 */
377static int sd_zbc_read_zoned_characteristics(struct scsi_disk *sdkp,
378					     unsigned char *buf)
379{
380
381	if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
382		sd_printk(KERN_NOTICE, sdkp,
383			  "Unconstrained-read check failed\n");
384		return -ENODEV;
385	}
386
387	if (sdkp->device->type != TYPE_ZBC) {
388		/* Host-aware */
389		sdkp->urswrz = 1;
390		sdkp->zones_optimal_open = get_unaligned_be64(&buf[8]);
391		sdkp->zones_optimal_nonseq = get_unaligned_be64(&buf[12]);
392		sdkp->zones_max_open = 0;
393	} else {
394		/* Host-managed */
395		sdkp->urswrz = buf[4] & 1;
396		sdkp->zones_optimal_open = 0;
397		sdkp->zones_optimal_nonseq = 0;
398		sdkp->zones_max_open = get_unaligned_be64(&buf[16]);
399	}
400
401	return 0;
402}
403
404/**
405 * Check reported capacity.
406 */
407static int sd_zbc_check_capacity(struct scsi_disk *sdkp,
408				 unsigned char *buf)
409{
410	sector_t lba;
411	int ret;
412
413	if (sdkp->rc_basis != 0)
414		return 0;
415
416	/* Do a report zone to get the maximum LBA to check capacity */
417	ret = sd_zbc_report_zones(sdkp, buf, SD_BUF_SIZE, 0);
418	if (ret)
419		return ret;
420
421	/* The max_lba field is the capacity of this device */
422	lba = get_unaligned_be64(&buf[8]);
423	if (lba + 1 == sdkp->capacity)
424		return 0;
425
426	if (sdkp->first_scan)
427		sd_printk(KERN_WARNING, sdkp,
428			  "Changing capacity from %llu to max LBA+1 %llu\n",
429			  (unsigned long long)sdkp->capacity,
430			  (unsigned long long)lba + 1);
431	sdkp->capacity = lba + 1;
432
433	return 0;
434}
435
436#define SD_ZBC_BUF_SIZE 131072
437
438static int sd_zbc_check_zone_size(struct scsi_disk *sdkp)
439{
440	u64 zone_blocks;
441	sector_t block = 0;
442	unsigned char *buf;
443	unsigned char *rec;
444	unsigned int buf_len;
445	unsigned int list_length;
446	int ret;
447	u8 same;
448
449	sdkp->zone_blocks = 0;
450
451	/* Get a buffer */
452	buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
453	if (!buf)
454		return -ENOMEM;
455
456	/* Do a report zone to get the same field */
457	ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0);
458	if (ret) {
459		zone_blocks = 0;
460		goto out;
461	}
462
463	same = buf[4] & 0x0f;
464	if (same > 0) {
465		rec = &buf[64];
466		zone_blocks = get_unaligned_be64(&rec[8]);
467		goto out;
468	}
469
470	/*
471	 * Check the size of all zones: all zones must be of
472	 * equal size, except the last zone which can be smaller
473	 * than other zones.
474	 */
475	do {
476
477		/* Parse REPORT ZONES header */
478		list_length = get_unaligned_be32(&buf[0]) + 64;
479		rec = buf + 64;
480		if (list_length < SD_ZBC_BUF_SIZE)
481			buf_len = list_length;
482		else
483			buf_len = SD_ZBC_BUF_SIZE;
484
485		/* Parse zone descriptors */
486		while (rec < buf + buf_len) {
487			zone_blocks = get_unaligned_be64(&rec[8]);
488			if (sdkp->zone_blocks == 0) {
489				sdkp->zone_blocks = zone_blocks;
490			} else if (zone_blocks != sdkp->zone_blocks &&
491				   (block + zone_blocks < sdkp->capacity
492				    || zone_blocks > sdkp->zone_blocks)) {
493				zone_blocks = 0;
494				goto out;
495			}
496			block += zone_blocks;
497			rec += 64;
498		}
499
500		if (block < sdkp->capacity) {
501			ret = sd_zbc_report_zones(sdkp, buf,
502						  SD_ZBC_BUF_SIZE, block);
503			if (ret)
504				return ret;
505		}
506
507	} while (block < sdkp->capacity);
508
509	zone_blocks = sdkp->zone_blocks;
510
511out:
512	kfree(buf);
513
514	if (!zone_blocks) {
515		if (sdkp->first_scan)
516			sd_printk(KERN_NOTICE, sdkp,
517				  "Devices with non constant zone "
518				  "size are not supported\n");
519		return -ENODEV;
520	}
521
522	if (!is_power_of_2(zone_blocks)) {
523		if (sdkp->first_scan)
524			sd_printk(KERN_NOTICE, sdkp,
525				  "Devices with non power of 2 zone "
526				  "size are not supported\n");
527		return -ENODEV;
528	}
529
530	if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
531		if (sdkp->first_scan)
532			sd_printk(KERN_NOTICE, sdkp,
533				  "Zone size too large\n");
534		return -ENODEV;
535	}
536
537	sdkp->zone_blocks = zone_blocks;
538
539	return 0;
540}
541
542static int sd_zbc_setup(struct scsi_disk *sdkp)
543{
544
545	/* chunk_sectors indicates the zone size */
546	blk_queue_chunk_sectors(sdkp->disk->queue,
547			logical_to_sectors(sdkp->device, sdkp->zone_blocks));
548	sdkp->zone_shift = ilog2(sdkp->zone_blocks);
549	sdkp->nr_zones = sdkp->capacity >> sdkp->zone_shift;
550	if (sdkp->capacity & (sdkp->zone_blocks - 1))
551		sdkp->nr_zones++;
552
553	if (!sdkp->zones_wlock) {
554		sdkp->zones_wlock = kcalloc(BITS_TO_LONGS(sdkp->nr_zones),
555					    sizeof(unsigned long),
556					    GFP_KERNEL);
557		if (!sdkp->zones_wlock)
558			return -ENOMEM;
559	}
560
561	return 0;
562}
563
564int sd_zbc_read_zones(struct scsi_disk *sdkp,
565		      unsigned char *buf)
566{
567	sector_t capacity;
568	int ret = 0;
569
570	if (!sd_is_zoned(sdkp))
571		/*
572		 * Device managed or normal SCSI disk,
573		 * no special handling required
574		 */
575		return 0;
576
577
578	/* Get zoned block device characteristics */
579	ret = sd_zbc_read_zoned_characteristics(sdkp, buf);
580	if (ret)
581		goto err;
582
583	/*
584	 * Check for unconstrained reads: host-managed devices with
585	 * constrained reads (drives failing read after write pointer)
586	 * are not supported.
587	 */
588	if (!sdkp->urswrz) {
589		if (sdkp->first_scan)
590			sd_printk(KERN_NOTICE, sdkp,
591			  "constrained reads devices are not supported\n");
592		ret = -ENODEV;
593		goto err;
594	}
595
596	/* Check capacity */
597	ret = sd_zbc_check_capacity(sdkp, buf);
598	if (ret)
599		goto err;
600	capacity = logical_to_sectors(sdkp->device, sdkp->capacity);
601
602	/*
603	 * Check zone size: only devices with a constant zone size (except
604	 * an eventual last runt zone) that is a power of 2 are supported.
605	 */
606	ret = sd_zbc_check_zone_size(sdkp);
607	if (ret)
608		goto err;
609
610	/* The drive satisfies the kernel restrictions: set it up */
611	ret = sd_zbc_setup(sdkp);
612	if (ret)
613		goto err;
614
615	/* READ16/WRITE16 is mandatory for ZBC disks */
616	sdkp->device->use_16_for_rw = 1;
617	sdkp->device->use_10_for_rw = 0;
618
619	return 0;
620
621err:
622	sdkp->capacity = 0;
623
624	return ret;
625}
626
627void sd_zbc_remove(struct scsi_disk *sdkp)
628{
629	kfree(sdkp->zones_wlock);
630	sdkp->zones_wlock = NULL;
631}
632
633void sd_zbc_print_zones(struct scsi_disk *sdkp)
634{
635	if (!sd_is_zoned(sdkp) || !sdkp->capacity)
636		return;
637
638	if (sdkp->capacity & (sdkp->zone_blocks - 1))
639		sd_printk(KERN_NOTICE, sdkp,
640			  "%u zones of %u logical blocks + 1 runt zone\n",
641			  sdkp->nr_zones - 1,
642			  sdkp->zone_blocks);
643	else
644		sd_printk(KERN_NOTICE, sdkp,
645			  "%u zones of %u logical blocks\n",
646			  sdkp->nr_zones,
647			  sdkp->zone_blocks);
648}