Skip to content

Commit 89d9475

Browse files
hreineckeaxboe
authored andcommitted
sd: Implement support for ZBC devices
Implement ZBC support functions to setup zoned disks, both host-managed and host-aware models. Only zoned disks that satisfy the following conditions are supported: 1) All zones are the same size, with the exception of an eventual last smaller runt zone. 2) For host-managed disks, reads are unrestricted (reads are not failed due to zone or write pointer alignement constraints). Zoned disks that do not satisfy these 2 conditions are setup with a capacity of 0 to prevent their use. The function sd_zbc_read_zones, called from sd_revalidate_disk, checks that the device satisfies the above two constraints. This function may also change the disk capacity previously set by sd_read_capacity for devices reporting only the capacity of conventional zones at the beginning of the LBA range (i.e. devices reporting rc_basis set to 0). The capacity message output was moved out of sd_read_capacity into a new function sd_print_capacity to include this eventual capacity change by sd_zbc_read_zones. This new function also includes a call to sd_zbc_print_zones to display the number of zones and zone size of the device. Signed-off-by: Hannes Reinecke <[email protected]> [Damien: * Removed zone cache support * Removed mapping of discard to reset write pointer command * Modified sd_zbc_read_zones to include checks that the device satisfies the kernel constraints * Implemeted REPORT ZONES setup and post-processing based on code from Shaun Tancheff <[email protected]> * Removed confusing use of 512B sector units in functions interface] Signed-off-by: Damien Le Moal <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Shaun Tancheff <[email protected]> Tested-by: Shaun Tancheff <[email protected]> Acked-by: Martin K. Petersen <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 3ed05a9 commit 89d9475

File tree

5 files changed

+843
-35
lines changed

5 files changed

+843
-35
lines changed

drivers/scsi/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ hv_storvsc-y := storvsc_drv.o
173173

174174
sd_mod-objs := sd.o
175175
sd_mod-$(CONFIG_BLK_DEV_INTEGRITY) += sd_dif.o
176+
sd_mod-$(CONFIG_BLK_DEV_ZONED) += sd_zbc.o
176177

177178
sr_mod-objs := sr.o sr_ioctl.o sr_vendor.o
178179
ncr53c8xx-flags-$(CONFIG_SCSI_ZALON) \

drivers/scsi/sd.c

Lines changed: 113 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
9393
MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
9494
MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
9595
MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
96+
MODULE_ALIAS_SCSI_DEVICE(TYPE_ZBC);
9697

9798
#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
9899
#define SD_MINORS 16
@@ -163,7 +164,7 @@ cache_type_store(struct device *dev, struct device_attribute *attr,
163164
static const char temp[] = "temporary ";
164165
int len;
165166

166-
if (sdp->type != TYPE_DISK)
167+
if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
167168
/* no cache control on RBC devices; theoretically they
168169
* can do it, but there's probably so many exceptions
169170
* it's not worth the risk */
@@ -262,7 +263,7 @@ allow_restart_store(struct device *dev, struct device_attribute *attr,
262263
if (!capable(CAP_SYS_ADMIN))
263264
return -EACCES;
264265

265-
if (sdp->type != TYPE_DISK)
266+
if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
266267
return -EINVAL;
267268

268269
sdp->allow_restart = simple_strtoul(buf, NULL, 10);
@@ -392,6 +393,11 @@ provisioning_mode_store(struct device *dev, struct device_attribute *attr,
392393
if (!capable(CAP_SYS_ADMIN))
393394
return -EACCES;
394395

396+
if (sd_is_zoned(sdkp)) {
397+
sd_config_discard(sdkp, SD_LBP_DISABLE);
398+
return count;
399+
}
400+
395401
if (sdp->type != TYPE_DISK)
396402
return -EINVAL;
397403

@@ -459,7 +465,7 @@ max_write_same_blocks_store(struct device *dev, struct device_attribute *attr,
459465
if (!capable(CAP_SYS_ADMIN))
460466
return -EACCES;
461467

462-
if (sdp->type != TYPE_DISK)
468+
if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
463469
return -EINVAL;
464470

465471
err = kstrtoul(buf, 10, &max);
@@ -844,6 +850,12 @@ static int sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
844850

845851
BUG_ON(bio_offset(bio) || bio_iovec(bio).bv_len != sdp->sector_size);
846852

853+
if (sd_is_zoned(sdkp)) {
854+
ret = sd_zbc_setup_write_cmnd(cmd);
855+
if (ret != BLKPREP_OK)
856+
return ret;
857+
}
858+
847859
sector >>= ilog2(sdp->sector_size) - 9;
848860
nr_sectors >>= ilog2(sdp->sector_size) - 9;
849861

@@ -901,19 +913,25 @@ static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
901913
struct request *rq = SCpnt->request;
902914
struct scsi_device *sdp = SCpnt->device;
903915
struct gendisk *disk = rq->rq_disk;
904-
struct scsi_disk *sdkp;
916+
struct scsi_disk *sdkp = scsi_disk(disk);
905917
sector_t block = blk_rq_pos(rq);
906918
sector_t threshold;
907919
unsigned int this_count = blk_rq_sectors(rq);
908920
unsigned int dif, dix;
921+
bool zoned_write = sd_is_zoned(sdkp) && rq_data_dir(rq) == WRITE;
909922
int ret;
910923
unsigned char protect;
911924

925+
if (zoned_write) {
926+
ret = sd_zbc_setup_write_cmnd(SCpnt);
927+
if (ret != BLKPREP_OK)
928+
return ret;
929+
}
930+
912931
ret = scsi_init_io(SCpnt);
913932
if (ret != BLKPREP_OK)
914933
goto out;
915934
SCpnt = rq->special;
916-
sdkp = scsi_disk(disk);
917935

918936
/* from here on until we're complete, any goto out
919937
* is used for a killable error condition */
@@ -1132,6 +1150,9 @@ static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
11321150
*/
11331151
ret = BLKPREP_OK;
11341152
out:
1153+
if (zoned_write && ret != BLKPREP_OK)
1154+
sd_zbc_cancel_write_cmnd(SCpnt);
1155+
11351156
return ret;
11361157
}
11371158

@@ -1149,6 +1170,10 @@ static int sd_init_command(struct scsi_cmnd *cmd)
11491170
case REQ_OP_READ:
11501171
case REQ_OP_WRITE:
11511172
return sd_setup_read_write_cmnd(cmd);
1173+
case REQ_OP_ZONE_REPORT:
1174+
return sd_zbc_setup_report_cmnd(cmd);
1175+
case REQ_OP_ZONE_RESET:
1176+
return sd_zbc_setup_reset_cmnd(cmd);
11521177
default:
11531178
BUG();
11541179
}
@@ -1780,14 +1805,28 @@ static int sd_done(struct scsi_cmnd *SCpnt)
17801805
unsigned char op = SCpnt->cmnd[0];
17811806
unsigned char unmap = SCpnt->cmnd[1] & 8;
17821807

1783-
if (req_op(req) == REQ_OP_DISCARD || req_op(req) == REQ_OP_WRITE_SAME) {
1808+
switch (req_op(req)) {
1809+
case REQ_OP_DISCARD:
1810+
case REQ_OP_WRITE_SAME:
1811+
case REQ_OP_ZONE_RESET:
17841812
if (!result) {
17851813
good_bytes = blk_rq_bytes(req);
17861814
scsi_set_resid(SCpnt, 0);
17871815
} else {
17881816
good_bytes = 0;
17891817
scsi_set_resid(SCpnt, blk_rq_bytes(req));
17901818
}
1819+
break;
1820+
case REQ_OP_ZONE_REPORT:
1821+
if (!result) {
1822+
good_bytes = scsi_bufflen(SCpnt)
1823+
- scsi_get_resid(SCpnt);
1824+
scsi_set_resid(SCpnt, 0);
1825+
} else {
1826+
good_bytes = 0;
1827+
scsi_set_resid(SCpnt, blk_rq_bytes(req));
1828+
}
1829+
break;
17911830
}
17921831

17931832
if (result) {
@@ -1848,7 +1887,11 @@ static int sd_done(struct scsi_cmnd *SCpnt)
18481887
default:
18491888
break;
18501889
}
1890+
18511891
out:
1892+
if (sd_is_zoned(sdkp))
1893+
sd_zbc_complete(SCpnt, good_bytes, &sshdr);
1894+
18521895
SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
18531896
"sd_done: completed %d of %d bytes\n",
18541897
good_bytes, scsi_bufflen(SCpnt)));
@@ -1983,7 +2026,6 @@ sd_spinup_disk(struct scsi_disk *sdkp)
19832026
}
19842027
}
19852028

1986-
19872029
/*
19882030
* Determine whether disk supports Data Integrity Field.
19892031
*/
@@ -2133,6 +2175,9 @@ static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
21332175
/* Logical blocks per physical block exponent */
21342176
sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
21352177

2178+
/* RC basis */
2179+
sdkp->rc_basis = (buffer[12] >> 4) & 0x3;
2180+
21362181
/* Lowest aligned logical block */
21372182
alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
21382183
blk_queue_alignment_offset(sdp->request_queue, alignment);
@@ -2242,7 +2287,6 @@ sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
22422287
{
22432288
int sector_size;
22442289
struct scsi_device *sdp = sdkp->device;
2245-
sector_t old_capacity = sdkp->capacity;
22462290

22472291
if (sd_try_rc16_first(sdp)) {
22482292
sector_size = read_capacity_16(sdkp, sdp, buffer);
@@ -2323,35 +2367,44 @@ sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
23232367
sector_size = 512;
23242368
}
23252369
blk_queue_logical_block_size(sdp->request_queue, sector_size);
2370+
blk_queue_physical_block_size(sdp->request_queue,
2371+
sdkp->physical_block_size);
2372+
sdkp->device->sector_size = sector_size;
23262373

2327-
{
2328-
char cap_str_2[10], cap_str_10[10];
2374+
if (sdkp->capacity > 0xffffffff)
2375+
sdp->use_16_for_rw = 1;
23292376

2330-
string_get_size(sdkp->capacity, sector_size,
2331-
STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
2332-
string_get_size(sdkp->capacity, sector_size,
2333-
STRING_UNITS_10, cap_str_10,
2334-
sizeof(cap_str_10));
2377+
}
23352378

2336-
if (sdkp->first_scan || old_capacity != sdkp->capacity) {
2337-
sd_printk(KERN_NOTICE, sdkp,
2338-
"%llu %d-byte logical blocks: (%s/%s)\n",
2339-
(unsigned long long)sdkp->capacity,
2340-
sector_size, cap_str_10, cap_str_2);
2379+
/*
2380+
* Print disk capacity
2381+
*/
2382+
static void
2383+
sd_print_capacity(struct scsi_disk *sdkp,
2384+
sector_t old_capacity)
2385+
{
2386+
int sector_size = sdkp->device->sector_size;
2387+
char cap_str_2[10], cap_str_10[10];
23412388

2342-
if (sdkp->physical_block_size != sector_size)
2343-
sd_printk(KERN_NOTICE, sdkp,
2344-
"%u-byte physical blocks\n",
2345-
sdkp->physical_block_size);
2346-
}
2347-
}
2389+
string_get_size(sdkp->capacity, sector_size,
2390+
STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
2391+
string_get_size(sdkp->capacity, sector_size,
2392+
STRING_UNITS_10, cap_str_10,
2393+
sizeof(cap_str_10));
23482394

2349-
if (sdkp->capacity > 0xffffffff)
2350-
sdp->use_16_for_rw = 1;
2395+
if (sdkp->first_scan || old_capacity != sdkp->capacity) {
2396+
sd_printk(KERN_NOTICE, sdkp,
2397+
"%llu %d-byte logical blocks: (%s/%s)\n",
2398+
(unsigned long long)sdkp->capacity,
2399+
sector_size, cap_str_10, cap_str_2);
23512400

2352-
blk_queue_physical_block_size(sdp->request_queue,
2353-
sdkp->physical_block_size);
2354-
sdkp->device->sector_size = sector_size;
2401+
if (sdkp->physical_block_size != sector_size)
2402+
sd_printk(KERN_NOTICE, sdkp,
2403+
"%u-byte physical blocks\n",
2404+
sdkp->physical_block_size);
2405+
2406+
sd_zbc_print_zones(sdkp);
2407+
}
23552408
}
23562409

23572410
/* called with buffer of length 512 */
@@ -2613,7 +2666,7 @@ static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
26132666
struct scsi_mode_data data;
26142667
struct scsi_sense_hdr sshdr;
26152668

2616-
if (sdp->type != TYPE_DISK)
2669+
if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
26172670
return;
26182671

26192672
if (sdkp->protection_type == 0)
@@ -2720,6 +2773,7 @@ static void sd_read_block_limits(struct scsi_disk *sdkp)
27202773
*/
27212774
static void sd_read_block_characteristics(struct scsi_disk *sdkp)
27222775
{
2776+
struct request_queue *q = sdkp->disk->queue;
27232777
unsigned char *buffer;
27242778
u16 rot;
27252779
const int vpd_len = 64;
@@ -2734,10 +2788,21 @@ static void sd_read_block_characteristics(struct scsi_disk *sdkp)
27342788
rot = get_unaligned_be16(&buffer[4]);
27352789

27362790
if (rot == 1) {
2737-
queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
2738-
queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, sdkp->disk->queue);
2791+
queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
2792+
queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
27392793
}
27402794

2795+
sdkp->zoned = (buffer[8] >> 4) & 3;
2796+
if (sdkp->zoned == 1)
2797+
q->limits.zoned = BLK_ZONED_HA;
2798+
else if (sdkp->device->type == TYPE_ZBC)
2799+
q->limits.zoned = BLK_ZONED_HM;
2800+
else
2801+
q->limits.zoned = BLK_ZONED_NONE;
2802+
if (blk_queue_is_zoned(q) && sdkp->first_scan)
2803+
sd_printk(KERN_NOTICE, sdkp, "Host-%s zoned block device\n",
2804+
q->limits.zoned == BLK_ZONED_HM ? "managed" : "aware");
2805+
27412806
out:
27422807
kfree(buffer);
27432808
}
@@ -2809,6 +2874,7 @@ static int sd_revalidate_disk(struct gendisk *disk)
28092874
struct scsi_disk *sdkp = scsi_disk(disk);
28102875
struct scsi_device *sdp = sdkp->device;
28112876
struct request_queue *q = sdkp->disk->queue;
2877+
sector_t old_capacity = sdkp->capacity;
28122878
unsigned char *buffer;
28132879
unsigned int dev_max, rw_max;
28142880

@@ -2842,8 +2908,11 @@ static int sd_revalidate_disk(struct gendisk *disk)
28422908
sd_read_block_provisioning(sdkp);
28432909
sd_read_block_limits(sdkp);
28442910
sd_read_block_characteristics(sdkp);
2911+
sd_zbc_read_zones(sdkp, buffer);
28452912
}
28462913

2914+
sd_print_capacity(sdkp, old_capacity);
2915+
28472916
sd_read_write_protect_flag(sdkp, buffer);
28482917
sd_read_cache_type(sdkp, buffer);
28492918
sd_read_app_tag_own(sdkp, buffer);
@@ -3041,9 +3110,16 @@ static int sd_probe(struct device *dev)
30413110

30423111
scsi_autopm_get_device(sdp);
30433112
error = -ENODEV;
3044-
if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
3113+
if (sdp->type != TYPE_DISK &&
3114+
sdp->type != TYPE_ZBC &&
3115+
sdp->type != TYPE_MOD &&
3116+
sdp->type != TYPE_RBC)
30453117
goto out;
30463118

3119+
#ifndef CONFIG_BLK_DEV_ZONED
3120+
if (sdp->type == TYPE_ZBC)
3121+
goto out;
3122+
#endif
30473123
SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
30483124
"sd_probe\n"));
30493125

@@ -3147,6 +3223,8 @@ static int sd_remove(struct device *dev)
31473223
del_gendisk(sdkp->disk);
31483224
sd_shutdown(dev);
31493225

3226+
sd_zbc_remove(sdkp);
3227+
31503228
blk_register_region(devt, SD_MINORS, NULL,
31513229
sd_default_probe, NULL, NULL);
31523230

0 commit comments

Comments
 (0)