2019-06-01 11:08:25 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2016-10-18 09:40:34 +03:00
/*
* SCSI Zoned Block commands
*
* Copyright ( C ) 2014 - 2015 SUSE Linux GmbH
* Written by : Hannes Reinecke < hare @ suse . de >
* Modified by : Damien Le Moal < damien . lemoal @ hgst . com >
* Modified by : Shaun Tancheff < shaun . tancheff @ seagate . com >
*/
# include <linux/blkdev.h>
2019-07-01 08:09:17 +03:00
# include <linux/vmalloc.h>
# include <linux/sched/mm.h>
2020-05-12 11:55:51 +03:00
# include <linux/mutex.h>
2016-10-18 09:40:34 +03:00
# include <asm/unaligned.h>
# include <scsi/scsi.h>
# include <scsi/scsi_cmnd.h>
# include "sd.h"
2022-11-30 13:36:16 +03:00
# define CREATE_TRACE_POINTS
# include "sd_trace.h"
2022-04-21 21:30:15 +03:00
/**
* sd_zbc_get_zone_wp_offset - Get zone write pointer offset .
* @ zone : Zone for which to return the write pointer offset .
*
* Return : offset of the write pointer from the start of the zone .
*/
2020-05-12 11:55:51 +03:00
static unsigned int sd_zbc_get_zone_wp_offset ( struct blk_zone * zone )
{
if ( zone - > type = = ZBC_ZONE_TYPE_CONV )
return 0 ;
switch ( zone - > cond ) {
case BLK_ZONE_COND_IMP_OPEN :
case BLK_ZONE_COND_EXP_OPEN :
case BLK_ZONE_COND_CLOSED :
return zone - > wp - zone - > start ;
case BLK_ZONE_COND_FULL :
return zone - > len ;
case BLK_ZONE_COND_EMPTY :
case BLK_ZONE_COND_OFFLINE :
case BLK_ZONE_COND_READONLY :
default :
/*
* Offline and read - only zones do not have a valid
* write pointer . Use 0 as for an empty zone .
*/
return 0 ;
}
}
2022-04-21 21:30:20 +03:00
/* Whether or not a SCSI zone descriptor describes a gap zone. */
static bool sd_zbc_is_gap_zone ( const u8 buf [ 64 ] )
{
return ( buf [ 0 ] & 0xf ) = = ZBC_ZONE_TYPE_GAP ;
}
2022-04-21 21:30:15 +03:00
/**
* sd_zbc_parse_report - Parse a SCSI zone descriptor
* @ sdkp : SCSI disk pointer .
* @ buf : SCSI zone descriptor .
* @ idx : Index of the zone relative to the first zone reported by the current
* sd_zbc_report_zones ( ) call .
* @ cb : Callback function pointer .
* @ data : Second argument passed to @ cb .
*
* Return : Value returned by @ cb .
*
* Convert a SCSI zone descriptor into struct blk_zone format . Additionally ,
* call @ cb ( blk_zone , @ data ) .
*/
static int sd_zbc_parse_report ( struct scsi_disk * sdkp , const u8 buf [ 64 ] ,
2019-11-11 05:39:30 +03:00
unsigned int idx , report_zones_cb cb , void * data )
2016-10-18 09:40:34 +03:00
{
struct scsi_device * sdp = sdkp - > device ;
2019-11-11 05:39:30 +03:00
struct blk_zone zone = { 0 } ;
2022-04-21 21:30:20 +03:00
sector_t start_lba , gran ;
2020-05-12 11:55:51 +03:00
int ret ;
2016-10-18 09:40:34 +03:00
2022-04-21 21:30:20 +03:00
if ( WARN_ON_ONCE ( sd_zbc_is_gap_zone ( buf ) ) )
return - EINVAL ;
2019-11-11 05:39:30 +03:00
zone . type = buf [ 0 ] & 0x0f ;
zone . cond = ( buf [ 1 ] > > 4 ) & 0xf ;
2016-10-18 09:40:34 +03:00
if ( buf [ 1 ] & 0x01 )
2019-11-11 05:39:30 +03:00
zone . reset = 1 ;
2016-10-18 09:40:34 +03:00
if ( buf [ 1 ] & 0x02 )
2019-11-11 05:39:30 +03:00
zone . non_seq = 1 ;
2022-04-21 21:30:20 +03:00
start_lba = get_unaligned_be64 ( & buf [ 16 ] ) ;
zone . start = logical_to_sectors ( sdp , start_lba ) ;
zone . capacity = logical_to_sectors ( sdp , get_unaligned_be64 ( & buf [ 8 ] ) ) ;
zone . len = zone . capacity ;
if ( sdkp - > zone_starting_lba_gran ) {
gran = logical_to_sectors ( sdp , sdkp - > zone_starting_lba_gran ) ;
if ( zone . len > gran ) {
sd_printk ( KERN_ERR , sdkp ,
" Invalid zone at LBA %llu with capacity %llu and length %llu; granularity = %llu \n " ,
start_lba ,
sectors_to_logical ( sdp , zone . capacity ) ,
sectors_to_logical ( sdp , zone . len ) ,
sectors_to_logical ( sdp , gran ) ) ;
return - EINVAL ;
}
/*
* Use the starting LBA granularity instead of the zone length
* obtained from the REPORT ZONES command .
*/
zone . len = gran ;
}
2021-12-01 17:28:30 +03:00
if ( zone . cond = = ZBC_ZONE_COND_FULL )
2019-11-11 05:39:30 +03:00
zone . wp = zone . start + zone . len ;
2021-12-01 17:28:31 +03:00
else
zone . wp = logical_to_sectors ( sdp , get_unaligned_be64 ( & buf [ 24 ] ) ) ;
2019-11-11 05:39:30 +03:00
2020-05-12 11:55:51 +03:00
ret = cb ( & zone , idx , data ) ;
if ( ret )
return ret ;
if ( sdkp - > rev_wp_offset )
sdkp - > rev_wp_offset [ idx ] = sd_zbc_get_zone_wp_offset ( & zone ) ;
return 0 ;
2016-10-18 09:40:34 +03:00
}
/**
2018-10-12 13:08:49 +03:00
* sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command .
2017-10-10 23:54:22 +03:00
* @ sdkp : The target disk
2019-07-01 08:09:17 +03:00
* @ buf : vmalloc - ed buffer to use for the reply
2017-10-10 23:54:22 +03:00
* @ buflen : the buffer size
* @ lba : Start LBA of the report
2018-10-12 13:08:41 +03:00
* @ partial : Do partial report
2017-10-10 23:54:22 +03:00
*
* For internal use during device validation .
2018-10-12 13:08:41 +03:00
* Using partial = true can significantly speed up execution of a report zones
* command because the disk does not have to count all possible report matching
* zones and will only report the count of zones fitting in the command reply
* buffer .
2016-10-18 09:40:34 +03:00
*/
2018-10-12 13:08:49 +03:00
static int sd_zbc_do_report_zones ( struct scsi_disk * sdkp , unsigned char * buf ,
unsigned int buflen , sector_t lba ,
bool partial )
2016-10-18 09:40:34 +03:00
{
struct scsi_device * sdp = sdkp - > device ;
const int timeout = sdp - > request_queue - > rq_timeout ;
struct scsi_sense_hdr sshdr ;
2022-12-29 22:01:48 +03:00
const struct scsi_exec_args exec_args = {
. sshdr = & sshdr ,
} ;
2016-10-18 09:40:34 +03:00
unsigned char cmd [ 16 ] ;
unsigned int rep_len ;
int result ;
memset ( cmd , 0 , 16 ) ;
cmd [ 0 ] = ZBC_IN ;
cmd [ 1 ] = ZI_REPORT_ZONES ;
put_unaligned_be64 ( lba , & cmd [ 2 ] ) ;
put_unaligned_be32 ( buflen , & cmd [ 10 ] ) ;
2018-10-12 13:08:41 +03:00
if ( partial )
cmd [ 14 ] = ZBC_REPORT_ZONE_PARTIAL ;
2016-10-18 09:40:34 +03:00
2022-12-29 22:01:48 +03:00
result = scsi_execute_cmd ( sdp , cmd , REQ_OP_DRV_IN , buf , buflen ,
timeout , SD_MAX_RETRIES , & exec_args ) ;
2016-10-18 09:40:34 +03:00
if ( result ) {
sd_printk ( KERN_ERR , sdkp ,
2019-11-25 10:05:18 +03:00
" REPORT ZONES start lba %llu failed \n " , lba ) ;
sd_print_result ( sdkp , " REPORT ZONES " , result ) ;
2021-04-27 11:30:15 +03:00
if ( result > 0 & & scsi_sense_valid ( & sshdr ) )
2019-11-25 10:05:18 +03:00
sd_print_sense_hdr ( sdkp , & sshdr ) ;
2016-10-18 09:40:34 +03:00
return - EIO ;
}
rep_len = get_unaligned_be32 ( & buf [ 0 ] ) ;
if ( rep_len < 64 ) {
sd_printk ( KERN_ERR , sdkp ,
" REPORT ZONES report invalid length %u \n " ,
rep_len ) ;
return - EIO ;
}
return 0 ;
}
2019-07-01 08:09:17 +03:00
/**
2021-03-17 12:11:58 +03:00
* sd_zbc_alloc_report_buffer ( ) - Allocate a buffer for report zones reply .
2019-07-01 08:09:17 +03:00
* @ sdkp : The target disk
* @ nr_zones : Maximum number of zones to report
* @ buflen : Size of the buffer allocated
*
* Try to allocate a reply buffer for the number of requested zones .
* The size of the buffer allocated may be smaller than requested to
* satify the device constraint ( max_hw_sectors , max_segments , etc ) .
*
* Return the address of the allocated buffer and update @ buflen with
* the size of the allocated buffer .
*/
static void * sd_zbc_alloc_report_buffer ( struct scsi_disk * sdkp ,
unsigned int nr_zones , size_t * buflen )
{
struct request_queue * q = sdkp - > disk - > queue ;
size_t bufsize ;
void * buf ;
/*
* Report zone buffer size should be at most 64 B times the number of
2021-09-06 17:06:42 +03:00
* zones requested plus the 64 B reply header , but should be aligned
* to SECTOR_SIZE for ATA devices .
2019-07-01 08:09:17 +03:00
* Make sure that this size does not exceed the hardware capabilities .
* Furthermore , since the report zone command cannot be split , make
* sure that the allocated buffer can always be mapped by limiting the
* number of pages allocated to the HBA max segments limit .
*/
2022-04-21 21:30:18 +03:00
nr_zones = min ( nr_zones , sdkp - > zone_info . nr_zones ) ;
2019-11-11 05:39:29 +03:00
bufsize = roundup ( ( nr_zones + 1 ) * 64 , SECTOR_SIZE ) ;
2019-07-01 08:09:17 +03:00
bufsize = min_t ( size_t , bufsize ,
queue_max_hw_sectors ( q ) < < SECTOR_SHIFT ) ;
bufsize = min_t ( size_t , bufsize , queue_max_segments ( q ) < < PAGE_SHIFT ) ;
2019-11-11 05:39:29 +03:00
while ( bufsize > = SECTOR_SIZE ) {
buf = __vmalloc ( bufsize ,
2020-06-02 07:51:40 +03:00
GFP_KERNEL | __GFP_ZERO | __GFP_NORETRY ) ;
2019-11-11 05:39:29 +03:00
if ( buf ) {
* buflen = bufsize ;
return buf ;
}
2021-09-06 17:06:42 +03:00
bufsize = rounddown ( bufsize > > 1 , SECTOR_SIZE ) ;
2019-11-11 05:39:29 +03:00
}
2019-07-01 08:09:17 +03:00
2019-11-11 05:39:29 +03:00
return NULL ;
2019-07-01 08:09:17 +03:00
}
2017-10-10 23:54:22 +03:00
/**
2019-11-11 05:39:30 +03:00
* sd_zbc_zone_sectors - Get the device zone size in number of 512 B sectors .
* @ sdkp : The target disk
2017-10-10 23:54:22 +03:00
*/
2019-11-11 05:39:30 +03:00
static inline sector_t sd_zbc_zone_sectors ( struct scsi_disk * sdkp )
{
2022-04-21 21:30:18 +03:00
return logical_to_sectors ( sdkp - > device , sdkp - > zone_info . zone_blocks ) ;
2019-11-11 05:39:30 +03:00
}
2022-04-21 21:30:15 +03:00
/**
* sd_zbc_report_zones - SCSI . report_zones ( ) callback .
* @ disk : Disk to report zones for .
* @ sector : Start sector .
* @ nr_zones : Maximum number of zones to report .
* @ cb : Callback function called to report zone information .
* @ data : Second argument passed to @ cb .
*
* Called by the block layer to iterate over zone information . See also the
* disk - > fops - > report_zones ( ) calls in block / blk - zoned . c .
*/
2018-10-12 13:08:49 +03:00
int sd_zbc_report_zones ( struct gendisk * disk , sector_t sector ,
2019-11-11 05:39:30 +03:00
unsigned int nr_zones , report_zones_cb cb , void * data )
2016-10-18 09:40:34 +03:00
{
2018-10-12 13:08:49 +03:00
struct scsi_disk * sdkp = scsi_disk ( disk ) ;
2022-04-21 21:30:17 +03:00
sector_t lba = sectors_to_logical ( sdkp - > device , sector ) ;
2019-11-11 05:39:30 +03:00
unsigned int nr , i ;
2018-10-12 13:08:49 +03:00
unsigned char * buf ;
2022-04-21 21:30:20 +03:00
u64 zone_length , start_lba ;
2019-11-11 05:39:30 +03:00
size_t offset , buflen = 0 ;
int zone_idx = 0 ;
int ret ;
2016-10-18 09:40:34 +03:00
if ( ! sd_is_zoned ( sdkp ) )
/* Not a zoned device */
2018-10-12 13:08:49 +03:00
return - EOPNOTSUPP ;
2016-10-18 09:40:34 +03:00
2022-04-21 21:30:17 +03:00
if ( ! sdkp - > capacity )
2020-02-19 09:38:00 +03:00
/* Device gone or invalid */
return - ENODEV ;
2019-11-11 05:39:30 +03:00
buf = sd_zbc_alloc_report_buffer ( sdkp , nr_zones , & buflen ) ;
2018-10-12 13:08:49 +03:00
if ( ! buf )
return - ENOMEM ;
2016-10-18 09:40:34 +03:00
2022-04-21 21:30:17 +03:00
while ( zone_idx < nr_zones & & lba < sdkp - > capacity ) {
ret = sd_zbc_do_report_zones ( sdkp , buf , buflen , lba , true ) ;
2019-11-11 05:39:30 +03:00
if ( ret )
goto out ;
offset = 0 ;
nr = min ( nr_zones , get_unaligned_be32 ( & buf [ 0 ] ) / 64 ) ;
if ( ! nr )
break ;
for ( i = 0 ; i < nr & & zone_idx < nr_zones ; i + + ) {
offset + = 64 ;
2022-04-21 21:30:20 +03:00
start_lba = get_unaligned_be64 ( & buf [ offset + 16 ] ) ;
zone_length = get_unaligned_be64 ( & buf [ offset + 8 ] ) ;
if ( ( zone_idx = = 0 & &
( lba < start_lba | |
lba > = start_lba + zone_length ) ) | |
( zone_idx > 0 & & start_lba ! = lba ) | |
start_lba + zone_length < start_lba ) {
sd_printk ( KERN_ERR , sdkp ,
" Zone %d at LBA %llu is invalid: %llu + %llu \n " ,
zone_idx , lba , start_lba , zone_length ) ;
ret = - EINVAL ;
goto out ;
}
lba = start_lba + zone_length ;
if ( sd_zbc_is_gap_zone ( & buf [ offset ] ) ) {
if ( sdkp - > zone_starting_lba_gran )
continue ;
sd_printk ( KERN_ERR , sdkp ,
" Gap zone without constant LBA offsets \n " ) ;
ret = - EINVAL ;
goto out ;
}
2019-11-11 05:39:30 +03:00
ret = sd_zbc_parse_report ( sdkp , buf + offset , zone_idx ,
cb , data ) ;
if ( ret )
goto out ;
2022-04-21 21:30:20 +03:00
2019-11-11 05:39:30 +03:00
zone_idx + + ;
}
2018-10-12 13:08:49 +03:00
}
2016-10-18 09:40:34 +03:00
2019-11-11 05:39:30 +03:00
ret = zone_idx ;
2019-07-01 08:09:17 +03:00
out :
kvfree ( buf ) ;
2018-10-12 13:08:49 +03:00
return ret ;
2016-10-18 09:40:34 +03:00
}
2020-05-12 11:55:50 +03:00
static blk_status_t sd_zbc_cmnd_checks ( struct scsi_cmnd * cmd )
{
2021-08-10 02:03:06 +03:00
struct request * rq = scsi_cmd_to_rq ( cmd ) ;
2021-11-26 15:18:00 +03:00
struct scsi_disk * sdkp = scsi_disk ( rq - > q - > disk ) ;
2020-05-12 11:55:50 +03:00
sector_t sector = blk_rq_pos ( rq ) ;
if ( ! sd_is_zoned ( sdkp ) )
/* Not a zoned device */
return BLK_STS_IOERR ;
if ( sdkp - > device - > changed )
return BLK_STS_IOERR ;
if ( sector & ( sd_zbc_zone_sectors ( sdkp ) - 1 ) )
/* Unaligned request */
return BLK_STS_IOERR ;
return BLK_STS_OK ;
}
2020-05-12 11:55:51 +03:00
# define SD_ZBC_INVALID_WP_OFST (~0u)
# define SD_ZBC_UPDATING_WP_OFST (SD_ZBC_INVALID_WP_OFST - 1)
static int sd_zbc_update_wp_offset_cb ( struct blk_zone * zone , unsigned int idx ,
void * data )
{
struct scsi_disk * sdkp = data ;
lockdep_assert_held ( & sdkp - > zones_wp_offset_lock ) ;
sdkp - > zones_wp_offset [ idx ] = sd_zbc_get_zone_wp_offset ( zone ) ;
return 0 ;
}
2022-04-21 21:30:15 +03:00
/*
* An attempt to append a zone triggered an invalid write pointer error .
* Reread the write pointer of the zone ( s ) in which the append failed .
*/
2020-05-12 11:55:51 +03:00
static void sd_zbc_update_wp_offset_workfn ( struct work_struct * work )
{
struct scsi_disk * sdkp ;
2021-03-10 12:48:06 +03:00
unsigned long flags ;
2021-09-18 00:23:14 +03:00
sector_t zno ;
2020-05-12 11:55:51 +03:00
int ret ;
sdkp = container_of ( work , struct scsi_disk , zone_wp_offset_work ) ;
2021-03-10 12:48:06 +03:00
spin_lock_irqsave ( & sdkp - > zones_wp_offset_lock , flags ) ;
2022-04-21 21:30:18 +03:00
for ( zno = 0 ; zno < sdkp - > zone_info . nr_zones ; zno + + ) {
2020-05-12 11:55:51 +03:00
if ( sdkp - > zones_wp_offset [ zno ] ! = SD_ZBC_UPDATING_WP_OFST )
continue ;
2021-03-10 12:48:06 +03:00
spin_unlock_irqrestore ( & sdkp - > zones_wp_offset_lock , flags ) ;
2020-05-12 11:55:51 +03:00
ret = sd_zbc_do_report_zones ( sdkp , sdkp - > zone_wp_update_buf ,
SD_BUF_SIZE ,
2022-04-21 21:30:18 +03:00
zno * sdkp - > zone_info . zone_blocks , true ) ;
2021-03-10 12:48:06 +03:00
spin_lock_irqsave ( & sdkp - > zones_wp_offset_lock , flags ) ;
2020-05-12 11:55:51 +03:00
if ( ! ret )
sd_zbc_parse_report ( sdkp , sdkp - > zone_wp_update_buf + 64 ,
zno , sd_zbc_update_wp_offset_cb ,
sdkp ) ;
}
2021-03-10 12:48:06 +03:00
spin_unlock_irqrestore ( & sdkp - > zones_wp_offset_lock , flags ) ;
2020-05-12 11:55:51 +03:00
scsi_device_put ( sdkp - > device ) ;
}
/**
* sd_zbc_prepare_zone_append ( ) - Prepare an emulated ZONE_APPEND command .
* @ cmd : the command to setup
* @ lba : the LBA to patch
* @ nr_blocks : the number of LBAs to be written
*
* Called from sd_setup_read_write_cmnd ( ) for REQ_OP_ZONE_APPEND .
* @ sd_zbc_prepare_zone_append ( ) handles the necessary zone wrote locking and
* patching of the lba for an emulated ZONE_APPEND command .
*
* In case the cached write pointer offset is % SD_ZBC_INVALID_WP_OFST it will
* schedule a REPORT ZONES command and return BLK_STS_IOERR .
*/
blk_status_t sd_zbc_prepare_zone_append ( struct scsi_cmnd * cmd , sector_t * lba ,
unsigned int nr_blocks )
{
2021-08-10 02:03:06 +03:00
struct request * rq = scsi_cmd_to_rq ( cmd ) ;
2021-11-26 15:18:00 +03:00
struct scsi_disk * sdkp = scsi_disk ( rq - > q - > disk ) ;
2020-05-12 11:55:51 +03:00
unsigned int wp_offset , zno = blk_rq_zone_no ( rq ) ;
2021-03-10 12:48:06 +03:00
unsigned long flags ;
2020-05-12 11:55:51 +03:00
blk_status_t ret ;
ret = sd_zbc_cmnd_checks ( cmd ) ;
if ( ret ! = BLK_STS_OK )
return ret ;
if ( ! blk_rq_zone_is_seq ( rq ) )
return BLK_STS_IOERR ;
/* Unlock of the write lock will happen in sd_zbc_complete() */
if ( ! blk_req_zone_write_trylock ( rq ) )
return BLK_STS_ZONE_RESOURCE ;
2021-03-10 12:48:06 +03:00
spin_lock_irqsave ( & sdkp - > zones_wp_offset_lock , flags ) ;
2020-05-12 11:55:51 +03:00
wp_offset = sdkp - > zones_wp_offset [ zno ] ;
switch ( wp_offset ) {
case SD_ZBC_INVALID_WP_OFST :
/*
* We are about to schedule work to update a zone write pointer
* offset , which will cause the zone append command to be
* requeued . So make sure that the scsi device does not go away
* while the work is being processed .
*/
if ( scsi_device_get ( sdkp - > device ) ) {
ret = BLK_STS_IOERR ;
break ;
}
sdkp - > zones_wp_offset [ zno ] = SD_ZBC_UPDATING_WP_OFST ;
schedule_work ( & sdkp - > zone_wp_offset_work ) ;
fallthrough ;
case SD_ZBC_UPDATING_WP_OFST :
ret = BLK_STS_DEV_RESOURCE ;
break ;
default :
wp_offset = sectors_to_logical ( sdkp - > device , wp_offset ) ;
2022-04-21 21:30:18 +03:00
if ( wp_offset + nr_blocks > sdkp - > zone_info . zone_blocks ) {
2020-05-12 11:55:51 +03:00
ret = BLK_STS_IOERR ;
break ;
}
2022-11-30 13:36:16 +03:00
trace_scsi_prepare_zone_append ( cmd , * lba , wp_offset ) ;
2020-05-12 11:55:51 +03:00
* lba + = wp_offset ;
}
2021-03-10 12:48:06 +03:00
spin_unlock_irqrestore ( & sdkp - > zones_wp_offset_lock , flags ) ;
2020-05-12 11:55:51 +03:00
if ( ret )
blk_req_zone_write_unlock ( rq ) ;
return ret ;
}
2017-10-10 23:54:22 +03:00
/**
2019-10-27 17:05:47 +03:00
* sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command . The operations
* can be RESET WRITE POINTER , OPEN , CLOSE or FINISH .
2017-10-10 23:54:22 +03:00
* @ cmd : the command to setup
2019-10-27 17:05:47 +03:00
* @ op : Operation to be performed
* @ all : All zones control
2017-10-10 23:54:22 +03:00
*
2019-10-27 17:05:47 +03:00
* Called from sd_init_command ( ) for REQ_OP_ZONE_RESET , REQ_OP_ZONE_RESET_ALL ,
* REQ_OP_ZONE_OPEN , REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests .
2017-10-10 23:54:22 +03:00
*/
2019-10-27 17:05:47 +03:00
blk_status_t sd_zbc_setup_zone_mgmt_cmnd ( struct scsi_cmnd * cmd ,
unsigned char op , bool all )
2016-10-18 09:40:34 +03:00
{
2021-08-10 02:03:06 +03:00
struct request * rq = scsi_cmd_to_rq ( cmd ) ;
2016-10-18 09:40:34 +03:00
sector_t sector = blk_rq_pos ( rq ) ;
2021-11-26 15:18:00 +03:00
struct scsi_disk * sdkp = scsi_disk ( rq - > q - > disk ) ;
2016-10-18 09:40:34 +03:00
sector_t block = sectors_to_logical ( sdkp - > device , sector ) ;
2020-05-12 11:55:50 +03:00
blk_status_t ret ;
2016-10-18 09:40:34 +03:00
2020-05-12 11:55:50 +03:00
ret = sd_zbc_cmnd_checks ( cmd ) ;
if ( ret ! = BLK_STS_OK )
return ret ;
2016-10-18 09:40:34 +03:00
cmd - > cmd_len = 16 ;
memset ( cmd - > cmnd , 0 , cmd - > cmd_len ) ;
cmd - > cmnd [ 0 ] = ZBC_OUT ;
2019-10-27 17:05:47 +03:00
cmd - > cmnd [ 1 ] = op ;
2019-08-01 20:26:37 +03:00
if ( all )
cmd - > cmnd [ 14 ] = 0x1 ;
else
put_unaligned_be64 ( block , & cmd - > cmnd [ 2 ] ) ;
2016-10-18 09:40:34 +03:00
rq - > timeout = SD_TIMEOUT ;
cmd - > sc_data_direction = DMA_NONE ;
cmd - > transfersize = 0 ;
cmd - > allowed = 0 ;
2018-11-09 16:42:39 +03:00
return BLK_STS_OK ;
2016-10-18 09:40:34 +03:00
}
2020-05-12 11:55:51 +03:00
static bool sd_zbc_need_zone_wp_update ( struct request * rq )
{
switch ( req_op ( rq ) ) {
case REQ_OP_ZONE_APPEND :
case REQ_OP_ZONE_FINISH :
case REQ_OP_ZONE_RESET :
case REQ_OP_ZONE_RESET_ALL :
return true ;
case REQ_OP_WRITE :
case REQ_OP_WRITE_ZEROES :
return blk_rq_zone_is_seq ( rq ) ;
default :
return false ;
}
}
/**
* sd_zbc_zone_wp_update - Update cached zone write pointer upon cmd completion
* @ cmd : Completed command
* @ good_bytes : Command reply bytes
*
* Called from sd_zbc_complete ( ) to handle the update of the cached zone write
* pointer value in case an update is needed .
*/
static unsigned int sd_zbc_zone_wp_update ( struct scsi_cmnd * cmd ,
unsigned int good_bytes )
{
int result = cmd - > result ;
2021-08-10 02:03:06 +03:00
struct request * rq = scsi_cmd_to_rq ( cmd ) ;
2021-11-26 15:18:00 +03:00
struct scsi_disk * sdkp = scsi_disk ( rq - > q - > disk ) ;
2020-05-12 11:55:51 +03:00
unsigned int zno = blk_rq_zone_no ( rq ) ;
2022-07-14 21:06:27 +03:00
enum req_op op = req_op ( rq ) ;
2021-03-10 12:48:06 +03:00
unsigned long flags ;
2020-05-12 11:55:51 +03:00
/*
* If we got an error for a command that needs updating the write
* pointer offset cache , we must mark the zone wp offset entry as
* invalid to force an update from disk the next time a zone append
* command is issued .
*/
2021-03-10 12:48:06 +03:00
spin_lock_irqsave ( & sdkp - > zones_wp_offset_lock , flags ) ;
2020-05-12 11:55:51 +03:00
if ( result & & op ! = REQ_OP_ZONE_RESET_ALL ) {
if ( op = = REQ_OP_ZONE_APPEND ) {
/* Force complete completion (no retry) */
good_bytes = 0 ;
scsi_set_resid ( cmd , blk_rq_bytes ( rq ) ) ;
}
/*
* Force an update of the zone write pointer offset on
* the next zone append access .
*/
if ( sdkp - > zones_wp_offset [ zno ] ! = SD_ZBC_UPDATING_WP_OFST )
sdkp - > zones_wp_offset [ zno ] = SD_ZBC_INVALID_WP_OFST ;
goto unlock_wp_offset ;
}
switch ( op ) {
case REQ_OP_ZONE_APPEND :
2022-11-30 13:36:16 +03:00
trace_scsi_zone_wp_update ( cmd , rq - > __sector ,
sdkp - > zones_wp_offset [ zno ] , good_bytes ) ;
2020-05-12 11:55:51 +03:00
rq - > __sector + = sdkp - > zones_wp_offset [ zno ] ;
fallthrough ;
case REQ_OP_WRITE_ZEROES :
case REQ_OP_WRITE :
if ( sdkp - > zones_wp_offset [ zno ] < sd_zbc_zone_sectors ( sdkp ) )
sdkp - > zones_wp_offset [ zno ] + =
good_bytes > > SECTOR_SHIFT ;
break ;
case REQ_OP_ZONE_RESET :
sdkp - > zones_wp_offset [ zno ] = 0 ;
break ;
case REQ_OP_ZONE_FINISH :
sdkp - > zones_wp_offset [ zno ] = sd_zbc_zone_sectors ( sdkp ) ;
break ;
case REQ_OP_ZONE_RESET_ALL :
memset ( sdkp - > zones_wp_offset , 0 ,
2022-04-21 21:30:18 +03:00
sdkp - > zone_info . nr_zones * sizeof ( unsigned int ) ) ;
2020-05-12 11:55:51 +03:00
break ;
default :
break ;
}
unlock_wp_offset :
2021-03-10 12:48:06 +03:00
spin_unlock_irqrestore ( & sdkp - > zones_wp_offset_lock , flags ) ;
2020-05-12 11:55:51 +03:00
return good_bytes ;
}
2017-10-10 23:54:22 +03:00
/**
* sd_zbc_complete - ZBC command post processing .
* @ cmd : Completed command
* @ good_bytes : Command reply bytes
* @ sshdr : command sense header
*
2020-05-12 11:55:51 +03:00
* Called from sd_done ( ) to handle zone commands errors and updates to the
* device queue zone write pointer offset cahce .
2017-10-10 23:54:22 +03:00
*/
2020-05-12 11:55:51 +03:00
unsigned int sd_zbc_complete ( struct scsi_cmnd * cmd , unsigned int good_bytes ,
2016-10-18 09:40:34 +03:00
struct scsi_sense_hdr * sshdr )
{
int result = cmd - > result ;
2021-08-10 02:03:06 +03:00
struct request * rq = scsi_cmd_to_rq ( cmd ) ;
2016-10-18 09:40:34 +03:00
2019-10-27 17:05:47 +03:00
if ( op_is_zone_mgmt ( req_op ( rq ) ) & &
2019-10-27 17:05:44 +03:00
result & &
sshdr - > sense_key = = ILLEGAL_REQUEST & &
sshdr - > asc = = 0x24 ) {
/*
2019-10-27 17:05:47 +03:00
* INVALID FIELD IN CDB error : a zone management command was
* attempted on a conventional zone . Nothing to worry about ,
* so be quiet about the error .
2019-10-27 17:05:44 +03:00
*/
rq - > rq_flags | = RQF_QUIET ;
2020-05-12 11:55:51 +03:00
} else if ( sd_zbc_need_zone_wp_update ( rq ) )
good_bytes = sd_zbc_zone_wp_update ( cmd , good_bytes ) ;
if ( req_op ( rq ) = = REQ_OP_ZONE_APPEND )
blk_req_zone_write_unlock ( rq ) ;
return good_bytes ;
2016-10-18 09:40:34 +03:00
}
/**
2018-10-12 13:08:40 +03:00
* sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
2017-10-10 23:54:22 +03:00
* @ sdkp : Target disk
* @ buf : Buffer where to store the VPD page data
*
2018-10-12 13:08:40 +03:00
* Read VPD page B6 , get information and check that reads are unconstrained .
2016-10-18 09:40:34 +03:00
*/
2018-10-12 13:08:40 +03:00
static int sd_zbc_check_zoned_characteristics ( struct scsi_disk * sdkp ,
unsigned char * buf )
2016-10-18 09:40:34 +03:00
{
2022-04-21 21:30:20 +03:00
u64 zone_starting_lba_gran ;
2016-10-18 09:40:34 +03:00
if ( scsi_get_vpd_page ( sdkp - > device , 0xb6 , buf , 64 ) ) {
sd_printk ( KERN_NOTICE , sdkp ,
2018-10-12 13:08:40 +03:00
" Read zoned characteristics VPD page failed \n " ) ;
2016-10-18 09:40:34 +03:00
return - ENODEV ;
}
if ( sdkp - > device - > type ! = TYPE_ZBC ) {
/* Host-aware */
sdkp - > urswrz = 1 ;
2017-10-10 23:54:25 +03:00
sdkp - > zones_optimal_open = get_unaligned_be32 ( & buf [ 8 ] ) ;
sdkp - > zones_optimal_nonseq = get_unaligned_be32 ( & buf [ 12 ] ) ;
2016-10-18 09:40:34 +03:00
sdkp - > zones_max_open = 0 ;
2022-04-21 21:30:19 +03:00
return 0 ;
2016-10-18 09:40:34 +03:00
}
2022-04-21 21:30:19 +03:00
/* Host-managed */
sdkp - > urswrz = buf [ 4 ] & 1 ;
sdkp - > zones_optimal_open = 0 ;
sdkp - > zones_optimal_nonseq = 0 ;
sdkp - > zones_max_open = get_unaligned_be32 ( & buf [ 16 ] ) ;
2022-04-21 21:30:20 +03:00
/* Check zone alignment method */
switch ( buf [ 23 ] & 0xf ) {
case 0 :
case ZBC_CONSTANT_ZONE_LENGTH :
/* Use zone length */
break ;
case ZBC_CONSTANT_ZONE_START_OFFSET :
zone_starting_lba_gran = get_unaligned_be64 ( & buf [ 24 ] ) ;
if ( zone_starting_lba_gran = = 0 | |
! is_power_of_2 ( zone_starting_lba_gran ) | |
logical_to_sectors ( sdkp - > device , zone_starting_lba_gran ) >
UINT_MAX ) {
sd_printk ( KERN_ERR , sdkp ,
" Invalid zone starting LBA granularity %llu \n " ,
zone_starting_lba_gran ) ;
return - ENODEV ;
}
sdkp - > zone_starting_lba_gran = zone_starting_lba_gran ;
break ;
default :
sd_printk ( KERN_ERR , sdkp , " Invalid zone alignment method \n " ) ;
return - ENODEV ;
}
2022-04-21 21:30:19 +03:00
2018-10-12 13:08:40 +03:00
/*
* Check for unconstrained reads : host - managed devices with
* constrained reads ( drives failing read after write pointer )
* are not supported .
*/
if ( ! sdkp - > urswrz ) {
if ( sdkp - > first_scan )
sd_printk ( KERN_NOTICE , sdkp ,
" constrained reads devices are not supported \n " ) ;
return - ENODEV ;
}
2016-10-18 09:40:34 +03:00
return 0 ;
}
2017-10-10 23:54:22 +03:00
/**
2019-12-20 10:58:23 +03:00
* sd_zbc_check_capacity - Check the device capacity
2017-10-10 23:54:22 +03:00
* @ sdkp : Target disk
2019-12-20 10:58:23 +03:00
* @ buf : command buffer
2022-04-21 21:30:15 +03:00
* @ zblocks : zone size in logical blocks
2017-10-10 23:54:22 +03:00
*
2019-12-20 10:58:23 +03:00
* Get the device zone size and check that the device capacity as reported
* by READ CAPACITY matches the max_lba value ( plus one ) of the report zones
* command reply for devices with RC_BASIS = = 0.
2018-04-17 04:04:41 +03:00
*
2019-12-20 10:58:23 +03:00
* Returns 0 upon success or an error code upon failure .
2017-10-10 23:54:22 +03:00
*/
2019-12-20 10:58:23 +03:00
static int sd_zbc_check_capacity ( struct scsi_disk * sdkp , unsigned char * buf ,
u32 * zblocks )
2016-10-18 09:40:34 +03:00
{
2019-12-20 10:58:23 +03:00
u64 zone_blocks ;
block: Enhance blk_revalidate_disk_zones()
For ZBC and ZAC zoned devices, the scsi driver revalidation processing
implemented by sd_revalidate_disk() includes a call to
sd_zbc_read_zones() which executes a full disk zone report used to
check that all zones of the disk are the same size. This processing is
followed by a call to blk_revalidate_disk_zones(), used to initialize
the device request queue zone bitmaps (zone type and zone write lock
bitmaps). To do so, blk_revalidate_disk_zones() also executes a full
device zone report to obtain zone types. As a result, the entire
zoned block device revalidation process includes two full device zone
report.
By moving the zone size checks into blk_revalidate_disk_zones(), this
process can be optimized to a single full device zone report, leading to
shorter device scan and revalidation times. This patch implements this
optimization, reducing the original full device zone report implemented
in sd_zbc_check_zones() to a single, small, report zones command
execution to obtain the size of the first zone of the device. Checks
whether all zones of the device are the same size as the first zone
size are moved to the generic blk_check_zone() function called from
blk_revalidate_disk_zones().
This optimization also has the following benefits:
1) fewer memory allocations in the scsi layer during disk revalidation
as the potentailly large buffer for zone report execution is not
needed.
2) Implement zone checks in a generic manner, reducing the burden on
device driver which only need to obtain the zone size and check that
this size is a power of 2 number of LBAs. Any new type of zoned
block device will benefit from this.
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-11-11 05:39:22 +03:00
sector_t max_lba ;
2016-10-18 09:40:34 +03:00
unsigned char * rec ;
2018-10-12 13:08:42 +03:00
int ret ;
2019-07-01 08:09:17 +03:00
block: Enhance blk_revalidate_disk_zones()
For ZBC and ZAC zoned devices, the scsi driver revalidation processing
implemented by sd_revalidate_disk() includes a call to
sd_zbc_read_zones() which executes a full disk zone report used to
check that all zones of the disk are the same size. This processing is
followed by a call to blk_revalidate_disk_zones(), used to initialize
the device request queue zone bitmaps (zone type and zone write lock
bitmaps). To do so, blk_revalidate_disk_zones() also executes a full
device zone report to obtain zone types. As a result, the entire
zoned block device revalidation process includes two full device zone
report.
By moving the zone size checks into blk_revalidate_disk_zones(), this
process can be optimized to a single full device zone report, leading to
shorter device scan and revalidation times. This patch implements this
optimization, reducing the original full device zone report implemented
in sd_zbc_check_zones() to a single, small, report zones command
execution to obtain the size of the first zone of the device. Checks
whether all zones of the device are the same size as the first zone
size are moved to the generic blk_check_zone() function called from
blk_revalidate_disk_zones().
This optimization also has the following benefits:
1) fewer memory allocations in the scsi layer during disk revalidation
as the potentailly large buffer for zone report execution is not
needed.
2) Implement zone checks in a generic manner, reducing the burden on
device driver which only need to obtain the zone size and check that
this size is a power of 2 number of LBAs. Any new type of zoned
block device will benefit from this.
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-11-11 05:39:22 +03:00
/* Do a report zone to get max_lba and the size of the first zone */
ret = sd_zbc_do_report_zones ( sdkp , buf , SD_BUF_SIZE , 0 , false ) ;
2018-03-02 01:19:28 +03:00
if ( ret )
block: Enhance blk_revalidate_disk_zones()
For ZBC and ZAC zoned devices, the scsi driver revalidation processing
implemented by sd_revalidate_disk() includes a call to
sd_zbc_read_zones() which executes a full disk zone report used to
check that all zones of the disk are the same size. This processing is
followed by a call to blk_revalidate_disk_zones(), used to initialize
the device request queue zone bitmaps (zone type and zone write lock
bitmaps). To do so, blk_revalidate_disk_zones() also executes a full
device zone report to obtain zone types. As a result, the entire
zoned block device revalidation process includes two full device zone
report.
By moving the zone size checks into blk_revalidate_disk_zones(), this
process can be optimized to a single full device zone report, leading to
shorter device scan and revalidation times. This patch implements this
optimization, reducing the original full device zone report implemented
in sd_zbc_check_zones() to a single, small, report zones command
execution to obtain the size of the first zone of the device. Checks
whether all zones of the device are the same size as the first zone
size are moved to the generic blk_check_zone() function called from
blk_revalidate_disk_zones().
This optimization also has the following benefits:
1) fewer memory allocations in the scsi layer during disk revalidation
as the potentailly large buffer for zone report execution is not
needed.
2) Implement zone checks in a generic manner, reducing the burden on
device driver which only need to obtain the zone size and check that
this size is a power of 2 number of LBAs. Any new type of zoned
block device will benefit from this.
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-11-11 05:39:22 +03:00
return ret ;
2016-10-18 09:40:34 +03:00
2018-10-12 13:08:41 +03:00
if ( sdkp - > rc_basis = = 0 ) {
/* The max_lba field is the capacity of this device */
max_lba = get_unaligned_be64 ( & buf [ 8 ] ) ;
if ( sdkp - > capacity ! = max_lba + 1 ) {
if ( sdkp - > first_scan )
sd_printk ( KERN_WARNING , sdkp ,
" Changing capacity from %llu to max LBA+1 %llu \n " ,
( unsigned long long ) sdkp - > capacity ,
( unsigned long long ) max_lba + 1 ) ;
sdkp - > capacity = max_lba + 1 ;
}
}
2022-04-21 21:30:20 +03:00
if ( sdkp - > zone_starting_lba_gran = = 0 ) {
/* Get the size of the first reported zone */
rec = buf + 64 ;
zone_blocks = get_unaligned_be64 ( & rec [ 8 ] ) ;
if ( logical_to_sectors ( sdkp - > device , zone_blocks ) > UINT_MAX ) {
if ( sdkp - > first_scan )
sd_printk ( KERN_NOTICE , sdkp ,
" Zone size too large \n " ) ;
return - EFBIG ;
}
} else {
zone_blocks = sdkp - > zone_starting_lba_gran ;
2016-10-18 09:40:34 +03:00
}
2022-04-21 21:30:16 +03:00
if ( ! is_power_of_2 ( zone_blocks ) ) {
sd_printk ( KERN_ERR , sdkp ,
" Zone size %llu is not a power of two. \n " ,
zone_blocks ) ;
return - EINVAL ;
}
block: Enhance blk_revalidate_disk_zones()
For ZBC and ZAC zoned devices, the scsi driver revalidation processing
implemented by sd_revalidate_disk() includes a call to
sd_zbc_read_zones() which executes a full disk zone report used to
check that all zones of the disk are the same size. This processing is
followed by a call to blk_revalidate_disk_zones(), used to initialize
the device request queue zone bitmaps (zone type and zone write lock
bitmaps). To do so, blk_revalidate_disk_zones() also executes a full
device zone report to obtain zone types. As a result, the entire
zoned block device revalidation process includes two full device zone
report.
By moving the zone size checks into blk_revalidate_disk_zones(), this
process can be optimized to a single full device zone report, leading to
shorter device scan and revalidation times. This patch implements this
optimization, reducing the original full device zone report implemented
in sd_zbc_check_zones() to a single, small, report zones command
execution to obtain the size of the first zone of the device. Checks
whether all zones of the device are the same size as the first zone
size are moved to the generic blk_check_zone() function called from
blk_revalidate_disk_zones().
This optimization also has the following benefits:
1) fewer memory allocations in the scsi layer during disk revalidation
as the potentailly large buffer for zone report execution is not
needed.
2) Implement zone checks in a generic manner, reducing the burden on
device driver which only need to obtain the zone size and check that
this size is a power of 2 number of LBAs. Any new type of zoned
block device will benefit from this.
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-11-11 05:39:22 +03:00
* zblocks = zone_blocks ;
2016-10-18 09:40:34 +03:00
block: Enhance blk_revalidate_disk_zones()
For ZBC and ZAC zoned devices, the scsi driver revalidation processing
implemented by sd_revalidate_disk() includes a call to
sd_zbc_read_zones() which executes a full disk zone report used to
check that all zones of the disk are the same size. This processing is
followed by a call to blk_revalidate_disk_zones(), used to initialize
the device request queue zone bitmaps (zone type and zone write lock
bitmaps). To do so, blk_revalidate_disk_zones() also executes a full
device zone report to obtain zone types. As a result, the entire
zoned block device revalidation process includes two full device zone
report.
By moving the zone size checks into blk_revalidate_disk_zones(), this
process can be optimized to a single full device zone report, leading to
shorter device scan and revalidation times. This patch implements this
optimization, reducing the original full device zone report implemented
in sd_zbc_check_zones() to a single, small, report zones command
execution to obtain the size of the first zone of the device. Checks
whether all zones of the device are the same size as the first zone
size are moved to the generic blk_check_zone() function called from
blk_revalidate_disk_zones().
This optimization also has the following benefits:
1) fewer memory allocations in the scsi layer during disk revalidation
as the potentailly large buffer for zone report execution is not
needed.
2) Implement zone checks in a generic manner, reducing the burden on
device driver which only need to obtain the zone size and check that
this size is a power of 2 number of LBAs. Any new type of zoned
block device will benefit from this.
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-11-11 05:39:22 +03:00
return 0 ;
2016-10-18 09:40:34 +03:00
}
scsi: sd_zbc: Improve zone revalidation
Currently, for zoned disks, since blk_revalidate_disk_zones() requires the
disk capacity to be set already to operate correctly, zones revalidation
can only be done on the second revalidate scan once the gendisk capacity is
set at the end of the first scan. As a result, if zone revalidation fails,
there is no second chance to recover from the failure and the disk capacity
is changed to 0, with the disk left unusable.
This can be improved by shuffling around code, specifically, by moving the
call to sd_zbc_revalidate_zones() from sd_zbc_read_zones() to the end of
sd_revalidate_disk(), after set_capacity_revalidate_and_notify() is called
to set the gendisk capacity. With this change, if sd_zbc_revalidate_zones()
fails on the first scan, the second scan will call it again to recover, if
possible.
Using the new struct scsi_disk fields rev_nr_zones and rev_zone_blocks,
sd_zbc_revalidate_zones() does actual work only if it detects a change with
the disk zone configuration. This means that for a successful zones
revalidation on the first scan, the second scan will not cause another
heavy full check.
While at it, remove the unecesary "extern" declaration of
sd_zbc_read_zones().
Link: https://lore.kernel.org/r/20200731054928.668547-1-damien.lemoal@wdc.com
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2020-07-31 08:49:28 +03:00
static void sd_zbc_print_zones ( struct scsi_disk * sdkp )
{
if ( ! sd_is_zoned ( sdkp ) | | ! sdkp - > capacity )
return ;
2022-04-21 21:30:18 +03:00
if ( sdkp - > capacity & ( sdkp - > zone_info . zone_blocks - 1 ) )
scsi: sd_zbc: Improve zone revalidation
Currently, for zoned disks, since blk_revalidate_disk_zones() requires the
disk capacity to be set already to operate correctly, zones revalidation
can only be done on the second revalidate scan once the gendisk capacity is
set at the end of the first scan. As a result, if zone revalidation fails,
there is no second chance to recover from the failure and the disk capacity
is changed to 0, with the disk left unusable.
This can be improved by shuffling around code, specifically, by moving the
call to sd_zbc_revalidate_zones() from sd_zbc_read_zones() to the end of
sd_revalidate_disk(), after set_capacity_revalidate_and_notify() is called
to set the gendisk capacity. With this change, if sd_zbc_revalidate_zones()
fails on the first scan, the second scan will call it again to recover, if
possible.
Using the new struct scsi_disk fields rev_nr_zones and rev_zone_blocks,
sd_zbc_revalidate_zones() does actual work only if it detects a change with
the disk zone configuration. This means that for a successful zones
revalidation on the first scan, the second scan will not cause another
heavy full check.
While at it, remove the unecesary "extern" declaration of
sd_zbc_read_zones().
Link: https://lore.kernel.org/r/20200731054928.668547-1-damien.lemoal@wdc.com
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2020-07-31 08:49:28 +03:00
sd_printk ( KERN_NOTICE , sdkp ,
" %u zones of %u logical blocks + 1 runt zone \n " ,
2022-04-21 21:30:18 +03:00
sdkp - > zone_info . nr_zones - 1 ,
sdkp - > zone_info . zone_blocks ) ;
scsi: sd_zbc: Improve zone revalidation
Currently, for zoned disks, since blk_revalidate_disk_zones() requires the
disk capacity to be set already to operate correctly, zones revalidation
can only be done on the second revalidate scan once the gendisk capacity is
set at the end of the first scan. As a result, if zone revalidation fails,
there is no second chance to recover from the failure and the disk capacity
is changed to 0, with the disk left unusable.
This can be improved by shuffling around code, specifically, by moving the
call to sd_zbc_revalidate_zones() from sd_zbc_read_zones() to the end of
sd_revalidate_disk(), after set_capacity_revalidate_and_notify() is called
to set the gendisk capacity. With this change, if sd_zbc_revalidate_zones()
fails on the first scan, the second scan will call it again to recover, if
possible.
Using the new struct scsi_disk fields rev_nr_zones and rev_zone_blocks,
sd_zbc_revalidate_zones() does actual work only if it detects a change with
the disk zone configuration. This means that for a successful zones
revalidation on the first scan, the second scan will not cause another
heavy full check.
While at it, remove the unecesary "extern" declaration of
sd_zbc_read_zones().
Link: https://lore.kernel.org/r/20200731054928.668547-1-damien.lemoal@wdc.com
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2020-07-31 08:49:28 +03:00
else
sd_printk ( KERN_NOTICE , sdkp ,
" %u zones of %u logical blocks \n " ,
2022-04-21 21:30:18 +03:00
sdkp - > zone_info . nr_zones ,
sdkp - > zone_info . zone_blocks ) ;
scsi: sd_zbc: Improve zone revalidation
Currently, for zoned disks, since blk_revalidate_disk_zones() requires the
disk capacity to be set already to operate correctly, zones revalidation
can only be done on the second revalidate scan once the gendisk capacity is
set at the end of the first scan. As a result, if zone revalidation fails,
there is no second chance to recover from the failure and the disk capacity
is changed to 0, with the disk left unusable.
This can be improved by shuffling around code, specifically, by moving the
call to sd_zbc_revalidate_zones() from sd_zbc_read_zones() to the end of
sd_revalidate_disk(), after set_capacity_revalidate_and_notify() is called
to set the gendisk capacity. With this change, if sd_zbc_revalidate_zones()
fails on the first scan, the second scan will call it again to recover, if
possible.
Using the new struct scsi_disk fields rev_nr_zones and rev_zone_blocks,
sd_zbc_revalidate_zones() does actual work only if it detects a change with
the disk zone configuration. This means that for a successful zones
revalidation on the first scan, the second scan will not cause another
heavy full check.
While at it, remove the unecesary "extern" declaration of
sd_zbc_read_zones().
Link: https://lore.kernel.org/r/20200731054928.668547-1-damien.lemoal@wdc.com
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2020-07-31 08:49:28 +03:00
}
2020-09-15 10:33:47 +03:00
static int sd_zbc_init_disk ( struct scsi_disk * sdkp )
{
sdkp - > zones_wp_offset = NULL ;
spin_lock_init ( & sdkp - > zones_wp_offset_lock ) ;
sdkp - > rev_wp_offset = NULL ;
mutex_init ( & sdkp - > rev_mutex ) ;
INIT_WORK ( & sdkp - > zone_wp_offset_work , sd_zbc_update_wp_offset_workfn ) ;
sdkp - > zone_wp_update_buf = kzalloc ( SD_BUF_SIZE , GFP_KERNEL ) ;
if ( ! sdkp - > zone_wp_update_buf )
return - ENOMEM ;
return 0 ;
}
2022-06-01 09:25:44 +03:00
void sd_zbc_free_zone_info ( struct scsi_disk * sdkp )
2020-09-15 10:33:47 +03:00
{
2022-06-01 09:25:44 +03:00
if ( ! sdkp - > zone_wp_update_buf )
return ;
2021-01-28 07:47:33 +03:00
/* Serialize against revalidate zones */
mutex_lock ( & sdkp - > rev_mutex ) ;
2020-09-15 10:33:47 +03:00
kvfree ( sdkp - > zones_wp_offset ) ;
sdkp - > zones_wp_offset = NULL ;
kfree ( sdkp - > zone_wp_update_buf ) ;
sdkp - > zone_wp_update_buf = NULL ;
2021-01-28 07:47:33 +03:00
2022-04-21 21:30:18 +03:00
sdkp - > early_zone_info = ( struct zoned_disk_info ) { } ;
sdkp - > zone_info = ( struct zoned_disk_info ) { } ;
2021-01-28 07:47:33 +03:00
mutex_unlock ( & sdkp - > rev_mutex ) ;
}
2020-05-12 11:55:51 +03:00
static void sd_zbc_revalidate_zones_cb ( struct gendisk * disk )
{
struct scsi_disk * sdkp = scsi_disk ( disk ) ;
swap ( sdkp - > zones_wp_offset , sdkp - > rev_wp_offset ) ;
}
2022-04-21 21:30:15 +03:00
/*
* Call blk_revalidate_disk_zones ( ) if any of the zoned disk properties have
* changed that make it necessary to call that function . Called by
* sd_revalidate_disk ( ) after the gendisk capacity has been set .
*/
scsi: sd_zbc: Improve zone revalidation
Currently, for zoned disks, since blk_revalidate_disk_zones() requires the
disk capacity to be set already to operate correctly, zones revalidation
can only be done on the second revalidate scan once the gendisk capacity is
set at the end of the first scan. As a result, if zone revalidation fails,
there is no second chance to recover from the failure and the disk capacity
is changed to 0, with the disk left unusable.
This can be improved by shuffling around code, specifically, by moving the
call to sd_zbc_revalidate_zones() from sd_zbc_read_zones() to the end of
sd_revalidate_disk(), after set_capacity_revalidate_and_notify() is called
to set the gendisk capacity. With this change, if sd_zbc_revalidate_zones()
fails on the first scan, the second scan will call it again to recover, if
possible.
Using the new struct scsi_disk fields rev_nr_zones and rev_zone_blocks,
sd_zbc_revalidate_zones() does actual work only if it detects a change with
the disk zone configuration. This means that for a successful zones
revalidation on the first scan, the second scan will not cause another
heavy full check.
While at it, remove the unecesary "extern" declaration of
sd_zbc_read_zones().
Link: https://lore.kernel.org/r/20200731054928.668547-1-damien.lemoal@wdc.com
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2020-07-31 08:49:28 +03:00
int sd_zbc_revalidate_zones ( struct scsi_disk * sdkp )
2020-05-12 11:55:51 +03:00
{
struct gendisk * disk = sdkp - > disk ;
scsi: sd_zbc: Improve zone revalidation
Currently, for zoned disks, since blk_revalidate_disk_zones() requires the
disk capacity to be set already to operate correctly, zones revalidation
can only be done on the second revalidate scan once the gendisk capacity is
set at the end of the first scan. As a result, if zone revalidation fails,
there is no second chance to recover from the failure and the disk capacity
is changed to 0, with the disk left unusable.
This can be improved by shuffling around code, specifically, by moving the
call to sd_zbc_revalidate_zones() from sd_zbc_read_zones() to the end of
sd_revalidate_disk(), after set_capacity_revalidate_and_notify() is called
to set the gendisk capacity. With this change, if sd_zbc_revalidate_zones()
fails on the first scan, the second scan will call it again to recover, if
possible.
Using the new struct scsi_disk fields rev_nr_zones and rev_zone_blocks,
sd_zbc_revalidate_zones() does actual work only if it detects a change with
the disk zone configuration. This means that for a successful zones
revalidation on the first scan, the second scan will not cause another
heavy full check.
While at it, remove the unecesary "extern" declaration of
sd_zbc_read_zones().
Link: https://lore.kernel.org/r/20200731054928.668547-1-damien.lemoal@wdc.com
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2020-07-31 08:49:28 +03:00
struct request_queue * q = disk - > queue ;
2022-04-21 21:30:18 +03:00
u32 zone_blocks = sdkp - > early_zone_info . zone_blocks ;
unsigned int nr_zones = sdkp - > early_zone_info . nr_zones ;
2020-05-12 11:55:51 +03:00
int ret = 0 ;
2021-02-17 16:52:45 +03:00
unsigned int flags ;
2020-05-12 11:55:51 +03:00
2020-09-15 10:33:47 +03:00
/*
* For all zoned disks , initialize zone append emulation data if not
* already done . This is necessary also for host - aware disks used as
* regular disks due to the presence of partitions as these partitions
* may be deleted and the disk zoned model changed back from
* BLK_ZONED_NONE to BLK_ZONED_HA .
*/
if ( sd_is_zoned ( sdkp ) & & ! sdkp - > zone_wp_update_buf ) {
ret = sd_zbc_init_disk ( sdkp ) ;
if ( ret )
return ret ;
}
2020-09-15 10:33:46 +03:00
/*
* There is nothing to do for regular disks , including host - aware disks
* that have partitions .
*/
if ( ! blk_queue_is_zoned ( q ) )
scsi: sd_zbc: Improve zone revalidation
Currently, for zoned disks, since blk_revalidate_disk_zones() requires the
disk capacity to be set already to operate correctly, zones revalidation
can only be done on the second revalidate scan once the gendisk capacity is
set at the end of the first scan. As a result, if zone revalidation fails,
there is no second chance to recover from the failure and the disk capacity
is changed to 0, with the disk left unusable.
This can be improved by shuffling around code, specifically, by moving the
call to sd_zbc_revalidate_zones() from sd_zbc_read_zones() to the end of
sd_revalidate_disk(), after set_capacity_revalidate_and_notify() is called
to set the gendisk capacity. With this change, if sd_zbc_revalidate_zones()
fails on the first scan, the second scan will call it again to recover, if
possible.
Using the new struct scsi_disk fields rev_nr_zones and rev_zone_blocks,
sd_zbc_revalidate_zones() does actual work only if it detects a change with
the disk zone configuration. This means that for a successful zones
revalidation on the first scan, the second scan will not cause another
heavy full check.
While at it, remove the unecesary "extern" declaration of
sd_zbc_read_zones().
Link: https://lore.kernel.org/r/20200731054928.668547-1-damien.lemoal@wdc.com
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2020-07-31 08:49:28 +03:00
return 0 ;
2020-05-12 11:55:51 +03:00
/*
* Make sure revalidate zones are serialized to ensure exclusive
* updates of the scsi disk data .
*/
mutex_lock ( & sdkp - > rev_mutex ) ;
2022-04-21 21:30:18 +03:00
if ( sdkp - > zone_info . zone_blocks = = zone_blocks & &
sdkp - > zone_info . nr_zones = = nr_zones & &
2022-07-06 10:03:50 +03:00
disk - > nr_zones = = nr_zones )
2020-05-12 11:55:51 +03:00
goto unlock ;
2021-02-17 16:52:45 +03:00
flags = memalloc_noio_save ( ) ;
2022-04-21 21:30:18 +03:00
sdkp - > zone_info . zone_blocks = zone_blocks ;
sdkp - > zone_info . nr_zones = nr_zones ;
2021-02-17 16:52:45 +03:00
sdkp - > rev_wp_offset = kvcalloc ( nr_zones , sizeof ( u32 ) , GFP_KERNEL ) ;
2020-05-12 11:55:51 +03:00
if ( ! sdkp - > rev_wp_offset ) {
ret = - ENOMEM ;
2021-02-17 16:52:45 +03:00
memalloc_noio_restore ( flags ) ;
2020-05-12 11:55:51 +03:00
goto unlock ;
}
2023-07-03 05:48:08 +03:00
blk_queue_chunk_sectors ( q ,
logical_to_sectors ( sdkp - > device , zone_blocks ) ) ;
blk_queue_max_zone_append_sectors ( q ,
q - > limits . max_segments < < PAGE_SECTORS_SHIFT ) ;
2020-05-12 11:55:51 +03:00
ret = blk_revalidate_disk_zones ( disk , sd_zbc_revalidate_zones_cb ) ;
2021-02-17 16:52:45 +03:00
memalloc_noio_restore ( flags ) ;
2020-05-12 11:55:51 +03:00
kvfree ( sdkp - > rev_wp_offset ) ;
sdkp - > rev_wp_offset = NULL ;
scsi: sd_zbc: Improve zone revalidation
Currently, for zoned disks, since blk_revalidate_disk_zones() requires the
disk capacity to be set already to operate correctly, zones revalidation
can only be done on the second revalidate scan once the gendisk capacity is
set at the end of the first scan. As a result, if zone revalidation fails,
there is no second chance to recover from the failure and the disk capacity
is changed to 0, with the disk left unusable.
This can be improved by shuffling around code, specifically, by moving the
call to sd_zbc_revalidate_zones() from sd_zbc_read_zones() to the end of
sd_revalidate_disk(), after set_capacity_revalidate_and_notify() is called
to set the gendisk capacity. With this change, if sd_zbc_revalidate_zones()
fails on the first scan, the second scan will call it again to recover, if
possible.
Using the new struct scsi_disk fields rev_nr_zones and rev_zone_blocks,
sd_zbc_revalidate_zones() does actual work only if it detects a change with
the disk zone configuration. This means that for a successful zones
revalidation on the first scan, the second scan will not cause another
heavy full check.
While at it, remove the unecesary "extern" declaration of
sd_zbc_read_zones().
Link: https://lore.kernel.org/r/20200731054928.668547-1-damien.lemoal@wdc.com
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2020-07-31 08:49:28 +03:00
if ( ret ) {
2022-04-21 21:30:18 +03:00
sdkp - > zone_info = ( struct zoned_disk_info ) { } ;
scsi: sd_zbc: Improve zone revalidation
Currently, for zoned disks, since blk_revalidate_disk_zones() requires the
disk capacity to be set already to operate correctly, zones revalidation
can only be done on the second revalidate scan once the gendisk capacity is
set at the end of the first scan. As a result, if zone revalidation fails,
there is no second chance to recover from the failure and the disk capacity
is changed to 0, with the disk left unusable.
This can be improved by shuffling around code, specifically, by moving the
call to sd_zbc_revalidate_zones() from sd_zbc_read_zones() to the end of
sd_revalidate_disk(), after set_capacity_revalidate_and_notify() is called
to set the gendisk capacity. With this change, if sd_zbc_revalidate_zones()
fails on the first scan, the second scan will call it again to recover, if
possible.
Using the new struct scsi_disk fields rev_nr_zones and rev_zone_blocks,
sd_zbc_revalidate_zones() does actual work only if it detects a change with
the disk zone configuration. This means that for a successful zones
revalidation on the first scan, the second scan will not cause another
heavy full check.
While at it, remove the unecesary "extern" declaration of
sd_zbc_read_zones().
Link: https://lore.kernel.org/r/20200731054928.668547-1-damien.lemoal@wdc.com
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2020-07-31 08:49:28 +03:00
sdkp - > capacity = 0 ;
goto unlock ;
}
sd_zbc_print_zones ( sdkp ) ;
2020-05-12 11:55:51 +03:00
unlock :
mutex_unlock ( & sdkp - > rev_mutex ) ;
return ret ;
}
2022-04-21 21:30:15 +03:00
/**
* sd_zbc_read_zones - Read zone information and update the request queue
* @ sdkp : SCSI disk pointer .
* @ buf : 512 byte buffer used for storing SCSI command output .
*
* Read zone information and update the request queue zone characteristics and
* also the zoned device information in * sdkp . Called by sd_revalidate_disk ( )
* before the gendisk capacity has been set .
*/
int sd_zbc_read_zones ( struct scsi_disk * sdkp , u8 buf [ SD_BUF_SIZE ] )
2016-10-18 09:40:34 +03:00
{
2018-10-12 13:08:50 +03:00
struct gendisk * disk = sdkp - > disk ;
2020-05-12 11:55:51 +03:00
struct request_queue * q = disk - > queue ;
2018-10-12 13:08:50 +03:00
unsigned int nr_zones ;
2019-07-17 04:51:49 +03:00
u32 zone_blocks = 0 ;
2017-04-24 10:51:14 +03:00
int ret ;
2016-10-18 09:40:34 +03:00
2022-06-01 09:25:44 +03:00
if ( ! sd_is_zoned ( sdkp ) ) {
2016-10-18 09:40:34 +03:00
/*
2022-06-01 09:25:44 +03:00
* Device managed or normal SCSI disk , no special handling
* required . Nevertheless , free the disk zone information in
* case the device type changed .
2016-10-18 09:40:34 +03:00
*/
2022-06-01 09:25:44 +03:00
sd_zbc_free_zone_info ( sdkp ) ;
2016-10-18 09:40:34 +03:00
return 0 ;
2022-06-01 09:25:44 +03:00
}
2016-10-18 09:40:34 +03:00
2022-11-15 03:29:05 +03:00
/* READ16/WRITE16/SYNC16 is mandatory for ZBC devices */
2021-01-28 07:47:33 +03:00
sdkp - > device - > use_16_for_rw = 1 ;
sdkp - > device - > use_10_for_rw = 0 ;
2022-11-15 03:29:05 +03:00
sdkp - > device - > use_16_for_sync = 1 ;
2021-01-28 07:47:33 +03:00
if ( ! blk_queue_is_zoned ( q ) ) {
/*
* This can happen for a host aware disk with partitions .
2022-06-01 09:25:44 +03:00
* The block device zone model was already cleared by
2022-07-06 10:03:40 +03:00
* disk_set_zoned ( ) . Only free the scsi disk zone
2021-01-28 07:47:33 +03:00
* information and exit early .
*/
2022-06-01 09:25:44 +03:00
sd_zbc_free_zone_info ( sdkp ) ;
2021-01-28 07:47:33 +03:00
return 0 ;
}
2018-10-12 13:08:40 +03:00
/* Check zoned block device characteristics (unconstrained reads) */
ret = sd_zbc_check_zoned_characteristics ( sdkp , buf ) ;
2016-10-18 09:40:34 +03:00
if ( ret )
goto err ;
2019-12-20 10:58:23 +03:00
/* Check the device capacity reported by report zones */
ret = sd_zbc_check_capacity ( sdkp , buf , & zone_blocks ) ;
2018-10-12 13:08:42 +03:00
if ( ret ! = 0 )
2016-10-18 09:40:34 +03:00
goto err ;
/* The drive satisfies the kernel restrictions: set it up */
2020-05-12 11:55:51 +03:00
blk_queue_flag_set ( QUEUE_FLAG_ZONE_RESETALL , q ) ;
blk_queue_required_elevator_features ( q , ELEVATOR_F_ZBD_SEQ_WRITE ) ;
2020-07-15 00:18:23 +03:00
if ( sdkp - > zones_max_open = = U32_MAX )
2022-07-06 10:03:44 +03:00
disk_set_max_open_zones ( disk , 0 ) ;
2020-07-15 00:18:23 +03:00
else
2022-07-06 10:03:44 +03:00
disk_set_max_open_zones ( disk , sdkp - > zones_max_open ) ;
disk_set_max_active_zones ( disk , 0 ) ;
2018-10-12 13:08:50 +03:00
nr_zones = round_up ( sdkp - > capacity , zone_blocks ) > > ilog2 ( zone_blocks ) ;
2022-04-21 21:30:18 +03:00
sdkp - > early_zone_info . nr_zones = nr_zones ;
sdkp - > early_zone_info . zone_blocks = zone_blocks ;
2016-10-18 09:40:34 +03:00
return 0 ;
err :
sdkp - > capacity = 0 ;
return ret ;
}