xref: /openbmc/linux/drivers/s390/block/dasd_genhd.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
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 
28c68f4f4eSChristoph Hellwig static unsigned int queue_depth = 32;
29c68f4f4eSChristoph Hellwig static unsigned int nr_hw_queues = 4;
30c68f4f4eSChristoph Hellwig 
31c68f4f4eSChristoph Hellwig module_param(queue_depth, uint, 0444);
32c68f4f4eSChristoph Hellwig MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices");
33c68f4f4eSChristoph Hellwig 
34c68f4f4eSChristoph Hellwig module_param(nr_hw_queues, uint, 0444);
35c68f4f4eSChristoph 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  */
dasd_gendisk_alloc(struct dasd_block * block)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 
51c68f4f4eSChristoph Hellwig 	block->tag_set.ops = &dasd_mq_ops;
52c68f4f4eSChristoph Hellwig 	block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
53c68f4f4eSChristoph Hellwig 	block->tag_set.nr_hw_queues = nr_hw_queues;
54c68f4f4eSChristoph Hellwig 	block->tag_set.queue_depth = queue_depth;
55c68f4f4eSChristoph Hellwig 	block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
56c68f4f4eSChristoph Hellwig 	block->tag_set.numa_node = NUMA_NO_NODE;
57c68f4f4eSChristoph Hellwig 	rc = blk_mq_alloc_tag_set(&block->tag_set);
58c68f4f4eSChristoph Hellwig 	if (rc)
59c68f4f4eSChristoph Hellwig 		return rc;
60c68f4f4eSChristoph Hellwig 
61c68f4f4eSChristoph Hellwig 	gdp = blk_mq_alloc_disk(&block->tag_set, block);
62c68f4f4eSChristoph Hellwig 	if (IS_ERR(gdp)) {
63c68f4f4eSChristoph Hellwig 		blk_mq_free_tag_set(&block->tag_set);
64c68f4f4eSChristoph Hellwig 		return PTR_ERR(gdp);
65c68f4f4eSChristoph 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  */
dasd_gendisk_free(struct dasd_block * block)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;
121c68f4f4eSChristoph 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  */
dasd_scan_partitions(struct dasd_block * block)1288e09f215SStefan Weinhuber int dasd_scan_partitions(struct dasd_block *block)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	struct block_device *bdev;
131a05e5780SJarod Wilson 	int rc;
1321da177e4SLinus Torvalds 
133*05bdb996SChristoph Hellwig 	bdev = blkdev_get_by_dev(disk_devt(block->gdp), BLK_OPEN_READ, NULL,
134*05bdb996SChristoph Hellwig 				 NULL);
135841ab72cSChristoph Hellwig 	if (IS_ERR(bdev)) {
1366ebdf1c7SStefan Haberland 		DBF_DEV_EVENT(DBF_ERR, block->base,
137841ab72cSChristoph Hellwig 			      "scan partitions error, blkdev_get returned %ld",
138841ab72cSChristoph Hellwig 			      PTR_ERR(bdev));
1396ebdf1c7SStefan Haberland 		return -ENODEV;
1406ebdf1c7SStefan Haberland 	}
1416029a06cSMing Lei 
142a8698707SChristoph Hellwig 	mutex_lock(&block->gdp->open_mutex);
1430384264eSChristoph Hellwig 	rc = bdev_disk_changed(block->gdp, false);
144a8698707SChristoph Hellwig 	mutex_unlock(&block->gdp->open_mutex);
145a05e5780SJarod Wilson 	if (rc)
1466ebdf1c7SStefan Haberland 		DBF_DEV_EVENT(DBF_ERR, block->base,
147a05e5780SJarod Wilson 				"scan partitions error, rc %d", rc);
1486ebdf1c7SStefan Haberland 
1491da177e4SLinus Torvalds 	/*
1501da177e4SLinus Torvalds 	 * Since the matching blkdev_put call to the blkdev_get in
1511da177e4SLinus Torvalds 	 * this function is not called before dasd_destroy_partitions
1521da177e4SLinus Torvalds 	 * the offline open_count limit needs to be increased from
1531da177e4SLinus Torvalds 	 * 0 to 1. This is done by setting device->bdev (see
1541da177e4SLinus Torvalds 	 * dasd_generic_set_offline). As long as the partition
1551da177e4SLinus Torvalds 	 * detection is running no offline should be allowed. That
1561da177e4SLinus Torvalds 	 * is why the assignment to device->bdev is done AFTER
1571da177e4SLinus Torvalds 	 * the BLKRRPART ioctl.
1581da177e4SLinus Torvalds 	 */
1598e09f215SStefan Weinhuber 	block->bdev = bdev;
1601da177e4SLinus Torvalds 	return 0;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds /*
1641da177e4SLinus Torvalds  * Remove all inodes in the system for a device, delete the
1651da177e4SLinus Torvalds  * partitions and make device unusable by setting its size to zero.
1661da177e4SLinus Torvalds  */
dasd_destroy_partitions(struct dasd_block * block)1678e09f215SStefan Weinhuber void dasd_destroy_partitions(struct dasd_block *block)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	struct block_device *bdev;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/*
1721da177e4SLinus Torvalds 	 * Get the bdev pointer from the device structure and clear
1731da177e4SLinus Torvalds 	 * device->bdev to lower the offline open_count limit again.
1741da177e4SLinus Torvalds 	 */
1758e09f215SStefan Weinhuber 	bdev = block->bdev;
1768e09f215SStefan Weinhuber 	block->bdev = NULL;
1771da177e4SLinus Torvalds 
178a8698707SChristoph Hellwig 	mutex_lock(&bdev->bd_disk->open_mutex);
1790384264eSChristoph Hellwig 	bdev_disk_changed(bdev->bd_disk, true);
180a8698707SChristoph Hellwig 	mutex_unlock(&bdev->bd_disk->open_mutex);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	/* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
1832736e8eeSChristoph Hellwig 	blkdev_put(bdev, NULL);
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
dasd_gendisk_init(void)1868e09f215SStefan Weinhuber int dasd_gendisk_init(void)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	int rc;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	/* Register to static dasd major 94 */
1911da177e4SLinus Torvalds 	rc = register_blkdev(DASD_MAJOR, "dasd");
1921da177e4SLinus Torvalds 	if (rc != 0) {
193baebc70aSJoe Perches 		pr_warn("Registering the device driver with major number %d failed\n",
194baebc70aSJoe Perches 			DASD_MAJOR);
1951da177e4SLinus Torvalds 		return rc;
1961da177e4SLinus Torvalds 	}
1971da177e4SLinus Torvalds 	return 0;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
dasd_gendisk_exit(void)2008e09f215SStefan Weinhuber void dasd_gendisk_exit(void)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	unregister_blkdev(DASD_MAJOR, "dasd");
2031da177e4SLinus Torvalds }
204