1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com> 41da177e4SLinus Torvalds * Horst Hummel <Horst.Hummel@de.ibm.com> 51da177e4SLinus Torvalds * Carsten Otte <Cotte@de.ibm.com> 61da177e4SLinus Torvalds * Martin Schwidefsky <schwidefsky@de.ibm.com> 71da177e4SLinus Torvalds * Bugreports.to..: <Linux390@de.ibm.com> 8a53c8fabSHeiko Carstens * Copyright IBM Corp. 1999, 2001 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * gendisk related functions for the dasd driver. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 14fc19f381SStefan Haberland #define KMSG_COMPONENT "dasd" 15fc19f381SStefan Haberland 161da177e4SLinus Torvalds #include <linux/interrupt.h> 17b81e0c23SChristoph Hellwig #include <linux/major.h> 181da177e4SLinus Torvalds #include <linux/fs.h> 191da177e4SLinus Torvalds #include <linux/blkpg.h> 201da177e4SLinus Torvalds 217c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds /* This is ugly... */ 241da177e4SLinus Torvalds #define PRINTK_HEADER "dasd_gendisk:" 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include "dasd_int.h" 271da177e4SLinus Torvalds 28*c68f4f4eSChristoph Hellwig static unsigned int queue_depth = 32; 29*c68f4f4eSChristoph Hellwig static unsigned int nr_hw_queues = 4; 30*c68f4f4eSChristoph Hellwig 31*c68f4f4eSChristoph Hellwig module_param(queue_depth, uint, 0444); 32*c68f4f4eSChristoph Hellwig MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices"); 33*c68f4f4eSChristoph Hellwig 34*c68f4f4eSChristoph Hellwig module_param(nr_hw_queues, uint, 0444); 35*c68f4f4eSChristoph Hellwig MODULE_PARM_DESC(nr_hw_queues, "Default number of hardware queues for new DASD devices"); 369c2b9dbaSChristoph Hellwig 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * Allocate and register gendisk structure for device. 391da177e4SLinus Torvalds */ 408e09f215SStefan Weinhuber int dasd_gendisk_alloc(struct dasd_block *block) 411da177e4SLinus Torvalds { 421da177e4SLinus Torvalds struct gendisk *gdp; 438e09f215SStefan Weinhuber struct dasd_device *base; 4411dfe199SLuis Chamberlain int len, rc; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /* Make sure the minor for this device exists. */ 478e09f215SStefan Weinhuber base = block->base; 488e09f215SStefan Weinhuber if (base->devindex >= DASD_PER_MAJOR) 491da177e4SLinus Torvalds return -EBUSY; 501da177e4SLinus Torvalds 51*c68f4f4eSChristoph Hellwig block->tag_set.ops = &dasd_mq_ops; 52*c68f4f4eSChristoph Hellwig block->tag_set.cmd_size = sizeof(struct dasd_ccw_req); 53*c68f4f4eSChristoph Hellwig block->tag_set.nr_hw_queues = nr_hw_queues; 54*c68f4f4eSChristoph Hellwig block->tag_set.queue_depth = queue_depth; 55*c68f4f4eSChristoph Hellwig block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 56*c68f4f4eSChristoph Hellwig block->tag_set.numa_node = NUMA_NO_NODE; 57*c68f4f4eSChristoph Hellwig rc = blk_mq_alloc_tag_set(&block->tag_set); 58*c68f4f4eSChristoph Hellwig if (rc) 59*c68f4f4eSChristoph Hellwig return rc; 60*c68f4f4eSChristoph Hellwig 61*c68f4f4eSChristoph Hellwig gdp = blk_mq_alloc_disk(&block->tag_set, block); 62*c68f4f4eSChristoph Hellwig if (IS_ERR(gdp)) { 63*c68f4f4eSChristoph Hellwig blk_mq_free_tag_set(&block->tag_set); 64*c68f4f4eSChristoph Hellwig return PTR_ERR(gdp); 65*c68f4f4eSChristoph Hellwig } 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* Initialize gendisk structure. */ 681da177e4SLinus Torvalds gdp->major = DASD_MAJOR; 698e09f215SStefan Weinhuber gdp->first_minor = base->devindex << DASD_PARTN_BITS; 70a58bd768SChristoph Hellwig gdp->minors = 1 << DASD_PARTN_BITS; 711da177e4SLinus Torvalds gdp->fops = &dasd_device_operations; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* 741da177e4SLinus Torvalds * Set device name. 751da177e4SLinus Torvalds * dasda - dasdz : 26 devices 761da177e4SLinus Torvalds * dasdaa - dasdzz : 676 devices, added up = 702 771da177e4SLinus Torvalds * dasdaaa - dasdzzz : 17576 devices, added up = 18278 781da177e4SLinus Torvalds * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252 791da177e4SLinus Torvalds */ 801da177e4SLinus Torvalds len = sprintf(gdp->disk_name, "dasd"); 818e09f215SStefan Weinhuber if (base->devindex > 25) { 828e09f215SStefan Weinhuber if (base->devindex > 701) { 838e09f215SStefan Weinhuber if (base->devindex > 18277) 841da177e4SLinus Torvalds len += sprintf(gdp->disk_name + len, "%c", 858e09f215SStefan Weinhuber 'a'+(((base->devindex-18278) 861da177e4SLinus Torvalds /17576)%26)); 871da177e4SLinus Torvalds len += sprintf(gdp->disk_name + len, "%c", 888e09f215SStefan Weinhuber 'a'+(((base->devindex-702)/676)%26)); 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds len += sprintf(gdp->disk_name + len, "%c", 918e09f215SStefan Weinhuber 'a'+(((base->devindex-26)/26)%26)); 921da177e4SLinus Torvalds } 938e09f215SStefan Weinhuber len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26)); 941da177e4SLinus Torvalds 9533b62a30SStefan Weinhuber if (base->features & DASD_FEATURE_READONLY || 9633b62a30SStefan Weinhuber test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) 971da177e4SLinus Torvalds set_disk_ro(gdp, 1); 9865f8da47SStefan Weinhuber dasd_add_link_to_gendisk(gdp, base); 998e09f215SStefan Weinhuber block->gdp = gdp; 1008e09f215SStefan Weinhuber set_capacity(block->gdp, 0); 10111dfe199SLuis Chamberlain 10211dfe199SLuis Chamberlain rc = device_add_disk(&base->cdev->dev, block->gdp, NULL); 10311dfe199SLuis Chamberlain if (rc) { 10411dfe199SLuis Chamberlain dasd_gendisk_free(block); 10511dfe199SLuis Chamberlain return rc; 10611dfe199SLuis Chamberlain } 10711dfe199SLuis Chamberlain 1081da177e4SLinus Torvalds return 0; 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* 1121da177e4SLinus Torvalds * Unregister and free gendisk structure for device. 1131da177e4SLinus Torvalds */ 1148e09f215SStefan Weinhuber void dasd_gendisk_free(struct dasd_block *block) 1151da177e4SLinus Torvalds { 1168e09f215SStefan Weinhuber if (block->gdp) { 1178e09f215SStefan Weinhuber del_gendisk(block->gdp); 1189eb25122SStefan Haberland block->gdp->private_data = NULL; 1198e09f215SStefan Weinhuber put_disk(block->gdp); 1208e09f215SStefan Weinhuber block->gdp = NULL; 121*c68f4f4eSChristoph Hellwig blk_mq_free_tag_set(&block->tag_set); 1221da177e4SLinus Torvalds } 1238f61701bSHorst Hummel } 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* 1261da177e4SLinus Torvalds * Trigger a partition detection. 1271da177e4SLinus Torvalds */ 1288e09f215SStefan Weinhuber int dasd_scan_partitions(struct dasd_block *block) 1291da177e4SLinus Torvalds { 1301da177e4SLinus Torvalds struct block_device *bdev; 131a05e5780SJarod Wilson int rc; 1321da177e4SLinus Torvalds 133841ab72cSChristoph Hellwig bdev = blkdev_get_by_dev(disk_devt(block->gdp), FMODE_READ, NULL); 134841ab72cSChristoph Hellwig if (IS_ERR(bdev)) { 1356ebdf1c7SStefan Haberland DBF_DEV_EVENT(DBF_ERR, block->base, 136841ab72cSChristoph Hellwig "scan partitions error, blkdev_get returned %ld", 137841ab72cSChristoph Hellwig PTR_ERR(bdev)); 1386ebdf1c7SStefan Haberland return -ENODEV; 1396ebdf1c7SStefan Haberland } 1406029a06cSMing Lei 141a8698707SChristoph Hellwig mutex_lock(&block->gdp->open_mutex); 1420384264eSChristoph Hellwig rc = bdev_disk_changed(block->gdp, false); 143a8698707SChristoph Hellwig mutex_unlock(&block->gdp->open_mutex); 144a05e5780SJarod Wilson if (rc) 1456ebdf1c7SStefan Haberland DBF_DEV_EVENT(DBF_ERR, block->base, 146a05e5780SJarod Wilson "scan partitions error, rc %d", rc); 1476ebdf1c7SStefan Haberland 1481da177e4SLinus Torvalds /* 1491da177e4SLinus Torvalds * Since the matching blkdev_put call to the blkdev_get in 1501da177e4SLinus Torvalds * this function is not called before dasd_destroy_partitions 1511da177e4SLinus Torvalds * the offline open_count limit needs to be increased from 1521da177e4SLinus Torvalds * 0 to 1. This is done by setting device->bdev (see 1531da177e4SLinus Torvalds * dasd_generic_set_offline). As long as the partition 1541da177e4SLinus Torvalds * detection is running no offline should be allowed. That 1551da177e4SLinus Torvalds * is why the assignment to device->bdev is done AFTER 1561da177e4SLinus Torvalds * the BLKRRPART ioctl. 1571da177e4SLinus Torvalds */ 1588e09f215SStefan Weinhuber block->bdev = bdev; 1591da177e4SLinus Torvalds return 0; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* 1631da177e4SLinus Torvalds * Remove all inodes in the system for a device, delete the 1641da177e4SLinus Torvalds * partitions and make device unusable by setting its size to zero. 1651da177e4SLinus Torvalds */ 1668e09f215SStefan Weinhuber void dasd_destroy_partitions(struct dasd_block *block) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds struct block_device *bdev; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* 1711da177e4SLinus Torvalds * Get the bdev pointer from the device structure and clear 1721da177e4SLinus Torvalds * device->bdev to lower the offline open_count limit again. 1731da177e4SLinus Torvalds */ 1748e09f215SStefan Weinhuber bdev = block->bdev; 1758e09f215SStefan Weinhuber block->bdev = NULL; 1761da177e4SLinus Torvalds 177a8698707SChristoph Hellwig mutex_lock(&bdev->bd_disk->open_mutex); 1780384264eSChristoph Hellwig bdev_disk_changed(bdev->bd_disk, true); 179a8698707SChristoph Hellwig mutex_unlock(&bdev->bd_disk->open_mutex); 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */ 1829a1c3542SAl Viro blkdev_put(bdev, FMODE_READ); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1858e09f215SStefan Weinhuber int dasd_gendisk_init(void) 1861da177e4SLinus Torvalds { 1871da177e4SLinus Torvalds int rc; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* Register to static dasd major 94 */ 1901da177e4SLinus Torvalds rc = register_blkdev(DASD_MAJOR, "dasd"); 1911da177e4SLinus Torvalds if (rc != 0) { 192baebc70aSJoe Perches pr_warn("Registering the device driver with major number %d failed\n", 193baebc70aSJoe Perches DASD_MAJOR); 1941da177e4SLinus Torvalds return rc; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds return 0; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1998e09f215SStefan Weinhuber void dasd_gendisk_exit(void) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds unregister_blkdev(DASD_MAJOR, "dasd"); 2021da177e4SLinus Torvalds } 203