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