xref: /linux/drivers/s390/block/dasd_genhd.c (revision d99ff463ecf651437e9e4abe68f331dfb6b5bd9d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *		    Horst Hummel <Horst.Hummel@de.ibm.com>
5  *		    Carsten Otte <Cotte@de.ibm.com>
6  *		    Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * Copyright IBM Corp. 1999, 2001
9  *
10  * gendisk related functions for the dasd driver.
11  *
12  */
13 
14 #define KMSG_COMPONENT "dasd"
15 
16 #include <linux/interrupt.h>
17 #include <linux/major.h>
18 #include <linux/fs.h>
19 #include <linux/blkpg.h>
20 
21 #include <linux/uaccess.h>
22 
23 /* This is ugly... */
24 #define PRINTK_HEADER "dasd_gendisk:"
25 
26 #include "dasd_int.h"
27 
28 static unsigned int queue_depth = 32;
29 static unsigned int nr_hw_queues = 4;
30 
31 module_param(queue_depth, uint, 0444);
32 MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices");
33 
34 module_param(nr_hw_queues, uint, 0444);
35 MODULE_PARM_DESC(nr_hw_queues, "Default number of hardware queues for new DASD devices");
36 
37 /*
38  * Allocate and register gendisk structure for device.
39  */
40 int dasd_gendisk_alloc(struct dasd_block *block)
41 {
42 	struct gendisk *gdp;
43 	struct dasd_device *base;
44 	int len, rc;
45 
46 	/* Make sure the minor for this device exists. */
47 	base = block->base;
48 	if (base->devindex >= DASD_PER_MAJOR)
49 		return -EBUSY;
50 
51 	block->tag_set.ops = &dasd_mq_ops;
52 	block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
53 	block->tag_set.nr_hw_queues = nr_hw_queues;
54 	block->tag_set.queue_depth = queue_depth;
55 	block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
56 	block->tag_set.numa_node = NUMA_NO_NODE;
57 	rc = blk_mq_alloc_tag_set(&block->tag_set);
58 	if (rc)
59 		return rc;
60 
61 	gdp = blk_mq_alloc_disk(&block->tag_set, block);
62 	if (IS_ERR(gdp)) {
63 		blk_mq_free_tag_set(&block->tag_set);
64 		return PTR_ERR(gdp);
65 	}
66 
67 	/* Initialize gendisk structure. */
68 	gdp->major = DASD_MAJOR;
69 	gdp->first_minor = base->devindex << DASD_PARTN_BITS;
70 	gdp->minors = 1 << DASD_PARTN_BITS;
71 	gdp->fops = &dasd_device_operations;
72 
73 	/*
74 	 * Set device name.
75 	 *   dasda - dasdz : 26 devices
76 	 *   dasdaa - dasdzz : 676 devices, added up = 702
77 	 *   dasdaaa - dasdzzz : 17576 devices, added up = 18278
78 	 *   dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
79 	 */
80 	len = sprintf(gdp->disk_name, "dasd");
81 	if (base->devindex > 25) {
82 		if (base->devindex > 701) {
83 			if (base->devindex > 18277)
84 			        len += sprintf(gdp->disk_name + len, "%c",
85 					       'a'+(((base->devindex-18278)
86 						     /17576)%26));
87 			len += sprintf(gdp->disk_name + len, "%c",
88 				       'a'+(((base->devindex-702)/676)%26));
89 		}
90 		len += sprintf(gdp->disk_name + len, "%c",
91 			       'a'+(((base->devindex-26)/26)%26));
92 	}
93 	len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
94 
95 	if (base->features & DASD_FEATURE_READONLY ||
96 	    test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
97 		set_disk_ro(gdp, 1);
98 	dasd_add_link_to_gendisk(gdp, base);
99 	block->gdp = gdp;
100 	set_capacity(block->gdp, 0);
101 
102 	rc = device_add_disk(&base->cdev->dev, block->gdp, NULL);
103 	if (rc) {
104 		dasd_gendisk_free(block);
105 		return rc;
106 	}
107 
108 	return 0;
109 }
110 
111 /*
112  * Unregister and free gendisk structure for device.
113  */
114 void dasd_gendisk_free(struct dasd_block *block)
115 {
116 	if (block->gdp) {
117 		del_gendisk(block->gdp);
118 		block->gdp->private_data = NULL;
119 		put_disk(block->gdp);
120 		block->gdp = NULL;
121 		blk_mq_free_tag_set(&block->tag_set);
122 	}
123 }
124 
125 /*
126  * Trigger a partition detection.
127  */
128 int dasd_scan_partitions(struct dasd_block *block)
129 {
130 	struct block_device *bdev;
131 	int rc;
132 
133 	bdev = blkdev_get_by_dev(disk_devt(block->gdp), BLK_OPEN_READ, NULL,
134 				 NULL);
135 	if (IS_ERR(bdev)) {
136 		DBF_DEV_EVENT(DBF_ERR, block->base,
137 			      "scan partitions error, blkdev_get returned %ld",
138 			      PTR_ERR(bdev));
139 		return -ENODEV;
140 	}
141 
142 	mutex_lock(&block->gdp->open_mutex);
143 	rc = bdev_disk_changed(block->gdp, false);
144 	mutex_unlock(&block->gdp->open_mutex);
145 	if (rc)
146 		DBF_DEV_EVENT(DBF_ERR, block->base,
147 				"scan partitions error, rc %d", rc);
148 
149 	/*
150 	 * Since the matching blkdev_put call to the blkdev_get in
151 	 * this function is not called before dasd_destroy_partitions
152 	 * the offline open_count limit needs to be increased from
153 	 * 0 to 1. This is done by setting device->bdev (see
154 	 * dasd_generic_set_offline). As long as the partition
155 	 * detection is running no offline should be allowed. That
156 	 * is why the assignment to device->bdev is done AFTER
157 	 * the BLKRRPART ioctl.
158 	 */
159 	block->bdev = bdev;
160 	return 0;
161 }
162 
163 /*
164  * Remove all inodes in the system for a device, delete the
165  * partitions and make device unusable by setting its size to zero.
166  */
167 void dasd_destroy_partitions(struct dasd_block *block)
168 {
169 	struct block_device *bdev;
170 
171 	/*
172 	 * Get the bdev pointer from the device structure and clear
173 	 * device->bdev to lower the offline open_count limit again.
174 	 */
175 	bdev = block->bdev;
176 	block->bdev = NULL;
177 
178 	mutex_lock(&bdev->bd_disk->open_mutex);
179 	bdev_disk_changed(bdev->bd_disk, true);
180 	mutex_unlock(&bdev->bd_disk->open_mutex);
181 
182 	/* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
183 	blkdev_put(bdev, NULL);
184 }
185 
186 int dasd_gendisk_init(void)
187 {
188 	int rc;
189 
190 	/* Register to static dasd major 94 */
191 	rc = register_blkdev(DASD_MAJOR, "dasd");
192 	if (rc != 0) {
193 		pr_warn("Registering the device driver with major number %d failed\n",
194 			DASD_MAJOR);
195 		return rc;
196 	}
197 	return 0;
198 }
199 
200 void dasd_gendisk_exit(void)
201 {
202 	unregister_blkdev(DASD_MAJOR, "dasd");
203 }
204