xref: /openbmc/linux/arch/m68k/emu/nfblock.c (revision 30363d65)
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 request_queue *queue;
59b2edd2fdSRoman Zippel 	struct gendisk *disk;
60b2edd2fdSRoman Zippel };
61b2edd2fdSRoman Zippel 
62dece1635SJens Axboe static blk_qc_t nfhd_make_request(struct request_queue *queue, struct bio *bio)
63b2edd2fdSRoman Zippel {
64b2edd2fdSRoman Zippel 	struct nfhd_device *dev = queue->queuedata;
657988613bSKent Overstreet 	struct bio_vec bvec;
667988613bSKent Overstreet 	struct bvec_iter iter;
677988613bSKent Overstreet 	int dir, len, shift;
684f024f37SKent Overstreet 	sector_t sec = bio->bi_iter.bi_sector;
69b2edd2fdSRoman Zippel 
70b2edd2fdSRoman Zippel 	dir = bio_data_dir(bio);
71b2edd2fdSRoman Zippel 	shift = dev->bshift;
727988613bSKent Overstreet 	bio_for_each_segment(bvec, bio, iter) {
737988613bSKent Overstreet 		len = bvec.bv_len;
74b2edd2fdSRoman Zippel 		len >>= 9;
75b2edd2fdSRoman Zippel 		nfhd_read_write(dev->id, 0, dir, sec >> shift, len >> shift,
766e768461SChristoph Hellwig 				page_to_phys(bvec.bv_page) + bvec.bv_offset);
77b2edd2fdSRoman Zippel 		sec += len;
78b2edd2fdSRoman Zippel 	}
794246a0b6SChristoph Hellwig 	bio_endio(bio);
80dece1635SJens Axboe 	return BLK_QC_T_NONE;
81b2edd2fdSRoman Zippel }
82b2edd2fdSRoman Zippel 
83b2edd2fdSRoman Zippel static int nfhd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
84b2edd2fdSRoman Zippel {
85b2edd2fdSRoman Zippel 	struct nfhd_device *dev = bdev->bd_disk->private_data;
86b2edd2fdSRoman Zippel 
87b2edd2fdSRoman Zippel 	geo->cylinders = dev->blocks >> (6 - dev->bshift);
88b2edd2fdSRoman Zippel 	geo->heads = 4;
89b2edd2fdSRoman Zippel 	geo->sectors = 16;
90b2edd2fdSRoman Zippel 
91b2edd2fdSRoman Zippel 	return 0;
92b2edd2fdSRoman Zippel }
93b2edd2fdSRoman Zippel 
94b2edd2fdSRoman Zippel static const struct block_device_operations nfhd_ops = {
95b2edd2fdSRoman Zippel 	.owner	= THIS_MODULE,
96b2edd2fdSRoman Zippel 	.getgeo	= nfhd_getgeo,
97b2edd2fdSRoman Zippel };
98b2edd2fdSRoman Zippel 
99b2edd2fdSRoman Zippel static int __init nfhd_init_one(int id, u32 blocks, u32 bsize)
100b2edd2fdSRoman Zippel {
101b2edd2fdSRoman Zippel 	struct nfhd_device *dev;
102b2edd2fdSRoman Zippel 	int dev_id = id - NFHD_DEV_OFFSET;
103b2edd2fdSRoman Zippel 
104b2edd2fdSRoman Zippel 	pr_info("nfhd%u: found device with %u blocks (%u bytes)\n", dev_id,
105b2edd2fdSRoman Zippel 		blocks, bsize);
106b2edd2fdSRoman Zippel 
107b2edd2fdSRoman Zippel 	if (bsize < 512 || (bsize & (bsize - 1))) {
108b2edd2fdSRoman Zippel 		pr_warn("nfhd%u: invalid block size\n", dev_id);
109b2edd2fdSRoman Zippel 		return -EINVAL;
110b2edd2fdSRoman Zippel 	}
111b2edd2fdSRoman Zippel 
112b2edd2fdSRoman Zippel 	dev = kmalloc(sizeof(struct nfhd_device), GFP_KERNEL);
113b2edd2fdSRoman Zippel 	if (!dev)
114b2edd2fdSRoman Zippel 		goto out;
115b2edd2fdSRoman Zippel 
116b2edd2fdSRoman Zippel 	dev->id = id;
117b2edd2fdSRoman Zippel 	dev->blocks = blocks;
118b2edd2fdSRoman Zippel 	dev->bsize = bsize;
119b2edd2fdSRoman Zippel 	dev->bshift = ffs(bsize) - 10;
120b2edd2fdSRoman Zippel 
121b2edd2fdSRoman Zippel 	dev->queue = blk_alloc_queue(GFP_KERNEL);
122b2edd2fdSRoman Zippel 	if (dev->queue == NULL)
123b2edd2fdSRoman Zippel 		goto free_dev;
124b2edd2fdSRoman Zippel 
125b2edd2fdSRoman Zippel 	dev->queue->queuedata = dev;
126b2edd2fdSRoman Zippel 	blk_queue_make_request(dev->queue, nfhd_make_request);
127b2edd2fdSRoman Zippel 	blk_queue_logical_block_size(dev->queue, bsize);
128b2edd2fdSRoman Zippel 
129b2edd2fdSRoman Zippel 	dev->disk = alloc_disk(16);
130b2edd2fdSRoman Zippel 	if (!dev->disk)
131b2edd2fdSRoman Zippel 		goto free_queue;
132b2edd2fdSRoman Zippel 
133b2edd2fdSRoman Zippel 	dev->disk->major = major_num;
134b2edd2fdSRoman Zippel 	dev->disk->first_minor = dev_id * 16;
135b2edd2fdSRoman Zippel 	dev->disk->fops = &nfhd_ops;
136b2edd2fdSRoman Zippel 	dev->disk->private_data = dev;
137b2edd2fdSRoman Zippel 	sprintf(dev->disk->disk_name, "nfhd%u", dev_id);
138b2edd2fdSRoman Zippel 	set_capacity(dev->disk, (sector_t)blocks * (bsize / 512));
139b2edd2fdSRoman Zippel 	dev->disk->queue = dev->queue;
140b2edd2fdSRoman Zippel 
141b2edd2fdSRoman Zippel 	add_disk(dev->disk);
142b2edd2fdSRoman Zippel 
143b2edd2fdSRoman Zippel 	list_add_tail(&dev->list, &nfhd_list);
144b2edd2fdSRoman Zippel 
145b2edd2fdSRoman Zippel 	return 0;
146b2edd2fdSRoman Zippel 
147b2edd2fdSRoman Zippel free_queue:
148b2edd2fdSRoman Zippel 	blk_cleanup_queue(dev->queue);
149b2edd2fdSRoman Zippel free_dev:
150b2edd2fdSRoman Zippel 	kfree(dev);
151b2edd2fdSRoman Zippel out:
152b2edd2fdSRoman Zippel 	return -ENOMEM;
153b2edd2fdSRoman Zippel }
154b2edd2fdSRoman Zippel 
155b2edd2fdSRoman Zippel static int __init nfhd_init(void)
156b2edd2fdSRoman Zippel {
157b2edd2fdSRoman Zippel 	u32 blocks, bsize;
15830363d65SChengguang Xu 	int ret;
159b2edd2fdSRoman Zippel 	int i;
160b2edd2fdSRoman Zippel 
161b2edd2fdSRoman Zippel 	nfhd_id = nf_get_id("XHDI");
162b2edd2fdSRoman Zippel 	if (!nfhd_id)
163b2edd2fdSRoman Zippel 		return -ENODEV;
164b2edd2fdSRoman Zippel 
16530363d65SChengguang Xu 	ret = register_blkdev(major_num, "nfhd");
16630363d65SChengguang Xu 	if (ret < 0) {
167b2edd2fdSRoman Zippel 		pr_warn("nfhd: unable to get major number\n");
16830363d65SChengguang Xu 		return ret;
169b2edd2fdSRoman Zippel 	}
170b2edd2fdSRoman Zippel 
17130363d65SChengguang Xu 	if (!major_num)
17230363d65SChengguang Xu 		major_num = ret;
17330363d65SChengguang Xu 
174b2edd2fdSRoman Zippel 	for (i = NFHD_DEV_OFFSET; i < 24; i++) {
175b2edd2fdSRoman Zippel 		if (nfhd_get_capacity(i, 0, &blocks, &bsize))
176b2edd2fdSRoman Zippel 			continue;
177b2edd2fdSRoman Zippel 		nfhd_init_one(i, blocks, bsize);
178b2edd2fdSRoman Zippel 	}
179b2edd2fdSRoman Zippel 
180b2edd2fdSRoman Zippel 	return 0;
181b2edd2fdSRoman Zippel }
182b2edd2fdSRoman Zippel 
183b2edd2fdSRoman Zippel static void __exit nfhd_exit(void)
184b2edd2fdSRoman Zippel {
185b2edd2fdSRoman Zippel 	struct nfhd_device *dev, *next;
186b2edd2fdSRoman Zippel 
187b2edd2fdSRoman Zippel 	list_for_each_entry_safe(dev, next, &nfhd_list, list) {
188b2edd2fdSRoman Zippel 		list_del(&dev->list);
189b2edd2fdSRoman Zippel 		del_gendisk(dev->disk);
190b2edd2fdSRoman Zippel 		put_disk(dev->disk);
191b2edd2fdSRoman Zippel 		blk_cleanup_queue(dev->queue);
192b2edd2fdSRoman Zippel 		kfree(dev);
193b2edd2fdSRoman Zippel 	}
194b2edd2fdSRoman Zippel 	unregister_blkdev(major_num, "nfhd");
195b2edd2fdSRoman Zippel }
196b2edd2fdSRoman Zippel 
197b2edd2fdSRoman Zippel module_init(nfhd_init);
198b2edd2fdSRoman Zippel module_exit(nfhd_exit);
199b2edd2fdSRoman Zippel 
200b2edd2fdSRoman Zippel MODULE_LICENSE("GPL");
201