xref: /openbmc/linux/arch/m68k/emu/nfblock.c (revision 3e08773c)
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 {
4455490050SGeert Uytterhoeven 	return nf_call(nfhd_id + NFHD_GET_CAPACITY, major, minor,
4555490050SGeert Uytterhoeven 		       virt_to_phys(blocks), virt_to_phys(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 gendisk *disk;
59b2edd2fdSRoman Zippel };
60b2edd2fdSRoman Zippel 
61*3e08773cSChristoph Hellwig static void nfhd_submit_bio(struct bio *bio)
62b2edd2fdSRoman Zippel {
63309dca30SChristoph Hellwig 	struct nfhd_device *dev = bio->bi_bdev->bd_disk->private_data;
647988613bSKent Overstreet 	struct bio_vec bvec;
657988613bSKent Overstreet 	struct bvec_iter iter;
667988613bSKent Overstreet 	int dir, len, shift;
674f024f37SKent Overstreet 	sector_t sec = bio->bi_iter.bi_sector;
68b2edd2fdSRoman Zippel 
69b2edd2fdSRoman Zippel 	dir = bio_data_dir(bio);
70b2edd2fdSRoman Zippel 	shift = dev->bshift;
717988613bSKent Overstreet 	bio_for_each_segment(bvec, bio, iter) {
727988613bSKent Overstreet 		len = bvec.bv_len;
73b2edd2fdSRoman Zippel 		len >>= 9;
74b2edd2fdSRoman Zippel 		nfhd_read_write(dev->id, 0, dir, sec >> shift, len >> shift,
756e768461SChristoph Hellwig 				page_to_phys(bvec.bv_page) + bvec.bv_offset);
76b2edd2fdSRoman Zippel 		sec += len;
77b2edd2fdSRoman Zippel 	}
784246a0b6SChristoph Hellwig 	bio_endio(bio);
79b2edd2fdSRoman Zippel }
80b2edd2fdSRoman Zippel 
81b2edd2fdSRoman Zippel static int nfhd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
82b2edd2fdSRoman Zippel {
83b2edd2fdSRoman Zippel 	struct nfhd_device *dev = bdev->bd_disk->private_data;
84b2edd2fdSRoman Zippel 
85b2edd2fdSRoman Zippel 	geo->cylinders = dev->blocks >> (6 - dev->bshift);
86b2edd2fdSRoman Zippel 	geo->heads = 4;
87b2edd2fdSRoman Zippel 	geo->sectors = 16;
88b2edd2fdSRoman Zippel 
89b2edd2fdSRoman Zippel 	return 0;
90b2edd2fdSRoman Zippel }
91b2edd2fdSRoman Zippel 
92b2edd2fdSRoman Zippel static const struct block_device_operations nfhd_ops = {
93b2edd2fdSRoman Zippel 	.owner	= THIS_MODULE,
94c62b37d9SChristoph Hellwig 	.submit_bio = nfhd_submit_bio,
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 
120c3e23538SChristoph Hellwig 	dev->disk = blk_alloc_disk(NUMA_NO_NODE);
121b2edd2fdSRoman Zippel 	if (!dev->disk)
122c3e23538SChristoph Hellwig 		goto free_dev;
123b2edd2fdSRoman Zippel 
124b2edd2fdSRoman Zippel 	dev->disk->major = major_num;
125b2edd2fdSRoman Zippel 	dev->disk->first_minor = dev_id * 16;
126c3e23538SChristoph Hellwig 	dev->disk->minors = 16;
127b2edd2fdSRoman Zippel 	dev->disk->fops = &nfhd_ops;
128b2edd2fdSRoman Zippel 	dev->disk->private_data = dev;
129b2edd2fdSRoman Zippel 	sprintf(dev->disk->disk_name, "nfhd%u", dev_id);
130b2edd2fdSRoman Zippel 	set_capacity(dev->disk, (sector_t)blocks * (bsize / 512));
131c3e23538SChristoph Hellwig 	blk_queue_logical_block_size(dev->disk->queue, bsize);
132b2edd2fdSRoman Zippel 	add_disk(dev->disk);
133b2edd2fdSRoman Zippel 
134b2edd2fdSRoman Zippel 	list_add_tail(&dev->list, &nfhd_list);
135b2edd2fdSRoman Zippel 
136b2edd2fdSRoman Zippel 	return 0;
137b2edd2fdSRoman Zippel 
138b2edd2fdSRoman Zippel free_dev:
139b2edd2fdSRoman Zippel 	kfree(dev);
140b2edd2fdSRoman Zippel out:
141b2edd2fdSRoman Zippel 	return -ENOMEM;
142b2edd2fdSRoman Zippel }
143b2edd2fdSRoman Zippel 
144b2edd2fdSRoman Zippel static int __init nfhd_init(void)
145b2edd2fdSRoman Zippel {
146b2edd2fdSRoman Zippel 	u32 blocks, bsize;
14730363d65SChengguang Xu 	int ret;
148b2edd2fdSRoman Zippel 	int i;
149b2edd2fdSRoman Zippel 
150b2edd2fdSRoman Zippel 	nfhd_id = nf_get_id("XHDI");
151b2edd2fdSRoman Zippel 	if (!nfhd_id)
152b2edd2fdSRoman Zippel 		return -ENODEV;
153b2edd2fdSRoman Zippel 
15430363d65SChengguang Xu 	ret = register_blkdev(major_num, "nfhd");
15530363d65SChengguang Xu 	if (ret < 0) {
156b2edd2fdSRoman Zippel 		pr_warn("nfhd: unable to get major number\n");
15730363d65SChengguang Xu 		return ret;
158b2edd2fdSRoman Zippel 	}
159b2edd2fdSRoman Zippel 
16030363d65SChengguang Xu 	if (!major_num)
16130363d65SChengguang Xu 		major_num = ret;
16230363d65SChengguang Xu 
163b2edd2fdSRoman Zippel 	for (i = NFHD_DEV_OFFSET; i < 24; i++) {
164b2edd2fdSRoman Zippel 		if (nfhd_get_capacity(i, 0, &blocks, &bsize))
165b2edd2fdSRoman Zippel 			continue;
166b2edd2fdSRoman Zippel 		nfhd_init_one(i, blocks, bsize);
167b2edd2fdSRoman Zippel 	}
168b2edd2fdSRoman Zippel 
169b2edd2fdSRoman Zippel 	return 0;
170b2edd2fdSRoman Zippel }
171b2edd2fdSRoman Zippel 
172b2edd2fdSRoman Zippel static void __exit nfhd_exit(void)
173b2edd2fdSRoman Zippel {
174b2edd2fdSRoman Zippel 	struct nfhd_device *dev, *next;
175b2edd2fdSRoman Zippel 
176b2edd2fdSRoman Zippel 	list_for_each_entry_safe(dev, next, &nfhd_list, list) {
177b2edd2fdSRoman Zippel 		list_del(&dev->list);
178b2edd2fdSRoman Zippel 		del_gendisk(dev->disk);
179c3e23538SChristoph Hellwig 		blk_cleanup_disk(dev->disk);
180b2edd2fdSRoman Zippel 		kfree(dev);
181b2edd2fdSRoman Zippel 	}
182b2edd2fdSRoman Zippel 	unregister_blkdev(major_num, "nfhd");
183b2edd2fdSRoman Zippel }
184b2edd2fdSRoman Zippel 
185b2edd2fdSRoman Zippel module_init(nfhd_init);
186b2edd2fdSRoman Zippel module_exit(nfhd_exit);
187b2edd2fdSRoman Zippel 
188b2edd2fdSRoman Zippel MODULE_LICENSE("GPL");
189