xref: /openbmc/linux/arch/m68k/emu/nfblock.c (revision b2edd2fd)
1b2edd2fdSRoman Zippel /*
2b2edd2fdSRoman Zippel  * ARAnyM block device driver
3b2edd2fdSRoman Zippel  *
4b2edd2fdSRoman Zippel  * This file is subject to the terms and conditions of the GNU General Public
5b2edd2fdSRoman Zippel  * License.  See the file COPYING in the main directory of this archive
6b2edd2fdSRoman Zippel  * for more details.
7b2edd2fdSRoman Zippel  */
8b2edd2fdSRoman Zippel 
9b2edd2fdSRoman Zippel #include <linux/module.h>
10b2edd2fdSRoman Zippel #include <linux/moduleparam.h>
11b2edd2fdSRoman Zippel #include <linux/init.h>
12b2edd2fdSRoman Zippel 
13b2edd2fdSRoman Zippel #include <linux/kernel.h>
14b2edd2fdSRoman Zippel #include <linux/errno.h>
15b2edd2fdSRoman Zippel #include <linux/types.h>
16b2edd2fdSRoman Zippel #include <linux/genhd.h>
17b2edd2fdSRoman Zippel #include <linux/blkdev.h>
18b2edd2fdSRoman Zippel #include <linux/hdreg.h>
19b2edd2fdSRoman Zippel #include <linux/slab.h>
20b2edd2fdSRoman Zippel 
21b2edd2fdSRoman Zippel #include <asm/natfeat.h>
22b2edd2fdSRoman Zippel 
23b2edd2fdSRoman Zippel static long nfhd_id;
24b2edd2fdSRoman Zippel 
25b2edd2fdSRoman Zippel enum {
26b2edd2fdSRoman Zippel 	/* emulation entry points */
27b2edd2fdSRoman Zippel 	NFHD_READ_WRITE = 10,
28b2edd2fdSRoman Zippel 	NFHD_GET_CAPACITY = 14,
29b2edd2fdSRoman Zippel 
30b2edd2fdSRoman Zippel 	/* skip ACSI devices */
31b2edd2fdSRoman Zippel 	NFHD_DEV_OFFSET = 8,
32b2edd2fdSRoman Zippel };
33b2edd2fdSRoman Zippel 
34b2edd2fdSRoman Zippel static inline s32 nfhd_read_write(u32 major, u32 minor, u32 rwflag, u32 recno,
35b2edd2fdSRoman Zippel 				  u32 count, u32 buf)
36b2edd2fdSRoman Zippel {
37b2edd2fdSRoman Zippel 	return nf_call(nfhd_id + NFHD_READ_WRITE, major, minor, rwflag, recno,
38b2edd2fdSRoman Zippel 		       count, buf);
39b2edd2fdSRoman Zippel }
40b2edd2fdSRoman Zippel 
41b2edd2fdSRoman Zippel static inline s32 nfhd_get_capacity(u32 major, u32 minor, u32 *blocks,
42b2edd2fdSRoman Zippel 				    u32 *blocksize)
43b2edd2fdSRoman Zippel {
44b2edd2fdSRoman Zippel 	return nf_call(nfhd_id + NFHD_GET_CAPACITY, major, minor, blocks,
45b2edd2fdSRoman Zippel 		       blocksize);
46b2edd2fdSRoman Zippel }
47b2edd2fdSRoman Zippel 
48b2edd2fdSRoman Zippel static LIST_HEAD(nfhd_list);
49b2edd2fdSRoman Zippel 
50b2edd2fdSRoman Zippel static int major_num;
51b2edd2fdSRoman Zippel module_param(major_num, int, 0);
52b2edd2fdSRoman Zippel 
53b2edd2fdSRoman Zippel struct nfhd_device {
54b2edd2fdSRoman Zippel 	struct list_head list;
55b2edd2fdSRoman Zippel 	int id;
56b2edd2fdSRoman Zippel 	u32 blocks, bsize;
57b2edd2fdSRoman Zippel 	int bshift;
58b2edd2fdSRoman Zippel 	struct request_queue *queue;
59b2edd2fdSRoman Zippel 	struct gendisk *disk;
60b2edd2fdSRoman Zippel };
61b2edd2fdSRoman Zippel 
62b2edd2fdSRoman Zippel static int nfhd_make_request(struct request_queue *queue, struct bio *bio)
63b2edd2fdSRoman Zippel {
64b2edd2fdSRoman Zippel 	struct nfhd_device *dev = queue->queuedata;
65b2edd2fdSRoman Zippel 	struct bio_vec *bvec;
66b2edd2fdSRoman Zippel 	int i, dir, len, shift;
67b2edd2fdSRoman Zippel 	sector_t sec = bio->bi_sector;
68b2edd2fdSRoman Zippel 
69b2edd2fdSRoman Zippel 	dir = bio_data_dir(bio);
70b2edd2fdSRoman Zippel 	shift = dev->bshift;
71b2edd2fdSRoman Zippel 	bio_for_each_segment(bvec, bio, i) {
72b2edd2fdSRoman Zippel 		len = bvec->bv_len;
73b2edd2fdSRoman Zippel 		len >>= 9;
74b2edd2fdSRoman Zippel 		nfhd_read_write(dev->id, 0, dir, sec >> shift, len >> shift,
75b2edd2fdSRoman Zippel 				bvec_to_phys(bvec));
76b2edd2fdSRoman Zippel 		sec += len;
77b2edd2fdSRoman Zippel 	}
78b2edd2fdSRoman Zippel 	bio_endio(bio, 0);
79b2edd2fdSRoman Zippel 	return 0;
80b2edd2fdSRoman Zippel }
81b2edd2fdSRoman Zippel 
82b2edd2fdSRoman Zippel static int nfhd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
83b2edd2fdSRoman Zippel {
84b2edd2fdSRoman Zippel 	struct nfhd_device *dev = bdev->bd_disk->private_data;
85b2edd2fdSRoman Zippel 
86b2edd2fdSRoman Zippel 	geo->cylinders = dev->blocks >> (6 - dev->bshift);
87b2edd2fdSRoman Zippel 	geo->heads = 4;
88b2edd2fdSRoman Zippel 	geo->sectors = 16;
89b2edd2fdSRoman Zippel 
90b2edd2fdSRoman Zippel 	return 0;
91b2edd2fdSRoman Zippel }
92b2edd2fdSRoman Zippel 
93b2edd2fdSRoman Zippel static const struct block_device_operations nfhd_ops = {
94b2edd2fdSRoman Zippel 	.owner	= THIS_MODULE,
95b2edd2fdSRoman Zippel 	.getgeo	= nfhd_getgeo,
96b2edd2fdSRoman Zippel };
97b2edd2fdSRoman Zippel 
98b2edd2fdSRoman Zippel static int __init nfhd_init_one(int id, u32 blocks, u32 bsize)
99b2edd2fdSRoman Zippel {
100b2edd2fdSRoman Zippel 	struct nfhd_device *dev;
101b2edd2fdSRoman Zippel 	int dev_id = id - NFHD_DEV_OFFSET;
102b2edd2fdSRoman Zippel 
103b2edd2fdSRoman Zippel 	pr_info("nfhd%u: found device with %u blocks (%u bytes)\n", dev_id,
104b2edd2fdSRoman Zippel 		blocks, bsize);
105b2edd2fdSRoman Zippel 
106b2edd2fdSRoman Zippel 	if (bsize < 512 || (bsize & (bsize - 1))) {
107b2edd2fdSRoman Zippel 		pr_warn("nfhd%u: invalid block size\n", dev_id);
108b2edd2fdSRoman Zippel 		return -EINVAL;
109b2edd2fdSRoman Zippel 	}
110b2edd2fdSRoman Zippel 
111b2edd2fdSRoman Zippel 	dev = kmalloc(sizeof(struct nfhd_device), GFP_KERNEL);
112b2edd2fdSRoman Zippel 	if (!dev)
113b2edd2fdSRoman Zippel 		goto out;
114b2edd2fdSRoman Zippel 
115b2edd2fdSRoman Zippel 	dev->id = id;
116b2edd2fdSRoman Zippel 	dev->blocks = blocks;
117b2edd2fdSRoman Zippel 	dev->bsize = bsize;
118b2edd2fdSRoman Zippel 	dev->bshift = ffs(bsize) - 10;
119b2edd2fdSRoman Zippel 
120b2edd2fdSRoman Zippel 	dev->queue = blk_alloc_queue(GFP_KERNEL);
121b2edd2fdSRoman Zippel 	if (dev->queue == NULL)
122b2edd2fdSRoman Zippel 		goto free_dev;
123b2edd2fdSRoman Zippel 
124b2edd2fdSRoman Zippel 	dev->queue->queuedata = dev;
125b2edd2fdSRoman Zippel 	blk_queue_make_request(dev->queue, nfhd_make_request);
126b2edd2fdSRoman Zippel 	blk_queue_logical_block_size(dev->queue, bsize);
127b2edd2fdSRoman Zippel 
128b2edd2fdSRoman Zippel 	dev->disk = alloc_disk(16);
129b2edd2fdSRoman Zippel 	if (!dev->disk)
130b2edd2fdSRoman Zippel 		goto free_queue;
131b2edd2fdSRoman Zippel 
132b2edd2fdSRoman Zippel 	dev->disk->major = major_num;
133b2edd2fdSRoman Zippel 	dev->disk->first_minor = dev_id * 16;
134b2edd2fdSRoman Zippel 	dev->disk->fops = &nfhd_ops;
135b2edd2fdSRoman Zippel 	dev->disk->private_data = dev;
136b2edd2fdSRoman Zippel 	sprintf(dev->disk->disk_name, "nfhd%u", dev_id);
137b2edd2fdSRoman Zippel 	set_capacity(dev->disk, (sector_t)blocks * (bsize / 512));
138b2edd2fdSRoman Zippel 	dev->disk->queue = dev->queue;
139b2edd2fdSRoman Zippel 
140b2edd2fdSRoman Zippel 	add_disk(dev->disk);
141b2edd2fdSRoman Zippel 
142b2edd2fdSRoman Zippel 	list_add_tail(&dev->list, &nfhd_list);
143b2edd2fdSRoman Zippel 
144b2edd2fdSRoman Zippel 	return 0;
145b2edd2fdSRoman Zippel 
146b2edd2fdSRoman Zippel free_queue:
147b2edd2fdSRoman Zippel 	blk_cleanup_queue(dev->queue);
148b2edd2fdSRoman Zippel free_dev:
149b2edd2fdSRoman Zippel 	kfree(dev);
150b2edd2fdSRoman Zippel out:
151b2edd2fdSRoman Zippel 	return -ENOMEM;
152b2edd2fdSRoman Zippel }
153b2edd2fdSRoman Zippel 
154b2edd2fdSRoman Zippel static int __init nfhd_init(void)
155b2edd2fdSRoman Zippel {
156b2edd2fdSRoman Zippel 	u32 blocks, bsize;
157b2edd2fdSRoman Zippel 	int i;
158b2edd2fdSRoman Zippel 
159b2edd2fdSRoman Zippel 	nfhd_id = nf_get_id("XHDI");
160b2edd2fdSRoman Zippel 	if (!nfhd_id)
161b2edd2fdSRoman Zippel 		return -ENODEV;
162b2edd2fdSRoman Zippel 
163b2edd2fdSRoman Zippel 	major_num = register_blkdev(major_num, "nfhd");
164b2edd2fdSRoman Zippel 	if (major_num <= 0) {
165b2edd2fdSRoman Zippel 		pr_warn("nfhd: unable to get major number\n");
166b2edd2fdSRoman Zippel 		return major_num;
167b2edd2fdSRoman Zippel 	}
168b2edd2fdSRoman Zippel 
169b2edd2fdSRoman Zippel 	for (i = NFHD_DEV_OFFSET; i < 24; i++) {
170b2edd2fdSRoman Zippel 		if (nfhd_get_capacity(i, 0, &blocks, &bsize))
171b2edd2fdSRoman Zippel 			continue;
172b2edd2fdSRoman Zippel 		nfhd_init_one(i, blocks, bsize);
173b2edd2fdSRoman Zippel 	}
174b2edd2fdSRoman Zippel 
175b2edd2fdSRoman Zippel 	return 0;
176b2edd2fdSRoman Zippel }
177b2edd2fdSRoman Zippel 
178b2edd2fdSRoman Zippel static void __exit nfhd_exit(void)
179b2edd2fdSRoman Zippel {
180b2edd2fdSRoman Zippel 	struct nfhd_device *dev, *next;
181b2edd2fdSRoman Zippel 
182b2edd2fdSRoman Zippel 	list_for_each_entry_safe(dev, next, &nfhd_list, list) {
183b2edd2fdSRoman Zippel 		list_del(&dev->list);
184b2edd2fdSRoman Zippel 		del_gendisk(dev->disk);
185b2edd2fdSRoman Zippel 		put_disk(dev->disk);
186b2edd2fdSRoman Zippel 		blk_cleanup_queue(dev->queue);
187b2edd2fdSRoman Zippel 		kfree(dev);
188b2edd2fdSRoman Zippel 	}
189b2edd2fdSRoman Zippel 	unregister_blkdev(major_num, "nfhd");
190b2edd2fdSRoman Zippel }
191b2edd2fdSRoman Zippel 
192b2edd2fdSRoman Zippel module_init(nfhd_init);
193b2edd2fdSRoman Zippel module_exit(nfhd_exit);
194b2edd2fdSRoman Zippel 
195b2edd2fdSRoman Zippel MODULE_LICENSE("GPL");
196