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
23static unsigned int queue_depth = 32;
24static unsigned int nr_hw_queues = 4;
25
26module_param(queue_depth, uint, 0444);
27MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices");
28
29module_param(nr_hw_queues, uint, 0444);
30MODULE_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 */
35int 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	blk_queue_flag_set(QUEUE_FLAG_NONROT, gdp->queue);
72
73	/* Initialize gendisk structure. */
74	gdp->major = DASD_MAJOR;
75	gdp->first_minor = base->devindex << DASD_PARTN_BITS;
76	gdp->minors = 1 << DASD_PARTN_BITS;
77	gdp->fops = &dasd_device_operations;
78
79	/*
80	 * Set device name.
81	 *   dasda - dasdz : 26 devices
82	 *   dasdaa - dasdzz : 676 devices, added up = 702
83	 *   dasdaaa - dasdzzz : 17576 devices, added up = 18278
84	 *   dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
85	 */
86	len = sprintf(gdp->disk_name, "dasd");
87	if (base->devindex > 25) {
88		if (base->devindex > 701) {
89			if (base->devindex > 18277)
90			        len += sprintf(gdp->disk_name + len, "%c",
91					       'a'+(((base->devindex-18278)
92						     /17576)%26));
93			len += sprintf(gdp->disk_name + len, "%c",
94				       'a'+(((base->devindex-702)/676)%26));
95		}
96		len += sprintf(gdp->disk_name + len, "%c",
97			       'a'+(((base->devindex-26)/26)%26));
98	}
99	len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
100
101	if (base->features & DASD_FEATURE_READONLY ||
102	    test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
103		set_disk_ro(gdp, 1);
104	dasd_add_link_to_gendisk(gdp, base);
105	block->gdp = gdp;
106	set_capacity(block->gdp, 0);
107
108	rc = device_add_disk(&base->cdev->dev, block->gdp, NULL);
109	if (rc) {
110		dasd_gendisk_free(block);
111		return rc;
112	}
113
114	return 0;
115}
116
117/*
118 * Unregister and free gendisk structure for device.
119 */
120void dasd_gendisk_free(struct dasd_block *block)
121{
122	if (block->gdp) {
123		del_gendisk(block->gdp);
124		block->gdp->private_data = NULL;
125		put_disk(block->gdp);
126		block->gdp = NULL;
127		blk_mq_free_tag_set(&block->tag_set);
128	}
129}
130
131/*
132 * Trigger a partition detection.
133 */
134int dasd_scan_partitions(struct dasd_block *block)
135{
136	struct file *bdev_file;
137	int rc;
138
139	bdev_file = bdev_file_open_by_dev(disk_devt(block->gdp), BLK_OPEN_READ,
140				       NULL, NULL);
141	if (IS_ERR(bdev_file)) {
142		DBF_DEV_EVENT(DBF_ERR, block->base,
143			      "scan partitions error, blkdev_get returned %ld",
144			      PTR_ERR(bdev_file));
145		return -ENODEV;
146	}
147
148	mutex_lock(&block->gdp->open_mutex);
149	rc = bdev_disk_changed(block->gdp, false);
150	mutex_unlock(&block->gdp->open_mutex);
151	if (rc)
152		DBF_DEV_EVENT(DBF_ERR, block->base,
153				"scan partitions error, rc %d", rc);
154
155	/*
156	 * Since the matching fput() call to the
157	 * bdev_file_open_by_path() in this function is not called before
158	 * dasd_destroy_partitions the offline open_count limit needs to be
159	 * increased from 0 to 1. This is done by setting device->bdev_file
160	 * (see dasd_generic_set_offline). As long as the partition detection
161	 * is running no offline should be allowed. That is why the assignment
162	 * to block->bdev_file is done AFTER the BLKRRPART ioctl.
163	 */
164	block->bdev_file = bdev_file;
165	return 0;
166}
167
168/*
169 * Remove all inodes in the system for a device, delete the
170 * partitions and make device unusable by setting its size to zero.
171 */
172void dasd_destroy_partitions(struct dasd_block *block)
173{
174	struct file *bdev_file;
175
176	/*
177	 * Get the bdev_file pointer from the device structure and clear
178	 * device->bdev_file to lower the offline open_count limit again.
179	 */
180	bdev_file = block->bdev_file;
181	block->bdev_file = NULL;
182
183	mutex_lock(&file_bdev(bdev_file)->bd_disk->open_mutex);
184	bdev_disk_changed(file_bdev(bdev_file)->bd_disk, true);
185	mutex_unlock(&file_bdev(bdev_file)->bd_disk->open_mutex);
186
187	/* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
188	fput(bdev_file);
189}
190
191int dasd_gendisk_init(void)
192{
193	int rc;
194
195	/* Register to static dasd major 94 */
196	rc = register_blkdev(DASD_MAJOR, "dasd");
197	if (rc != 0) {
198		pr_warn("Registering the device driver with major number %d failed\n",
199			DASD_MAJOR);
200		return rc;
201	}
202	return 0;
203}
204
205void dasd_gendisk_exit(void)
206{
207	unregister_blkdev(DASD_MAJOR, "dasd");
208}
209