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