blob: e9110b9b8bcdec50ec26ef974557447ebadb213a [file] [log] [blame]
Roman Zippelb2edd2f2008-11-18 21:02:19 +01001/*
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
23static long nfhd_id;
24
25enum {
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
34static 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
41static inline s32 nfhd_get_capacity(u32 major, u32 minor, u32 *blocks,
42 u32 *blocksize)
43{
Geert Uytterhoeven55490052013-08-14 10:45:00 +020044 return nf_call(nfhd_id + NFHD_GET_CAPACITY, major, minor,
45 virt_to_phys(blocks), virt_to_phys(blocksize));
Roman Zippelb2edd2f2008-11-18 21:02:19 +010046}
47
48static LIST_HEAD(nfhd_list);
49
50static int major_num;
51module_param(major_num, int, 0);
52
53struct nfhd_device {
54 struct list_head list;
55 int id;
56 u32 blocks, bsize;
57 int bshift;
58 struct request_queue *queue;
59 struct gendisk *disk;
60};
61
Jens Axboedece1632015-11-05 10:41:16 -070062static blk_qc_t nfhd_make_request(struct request_queue *queue, struct bio *bio)
Roman Zippelb2edd2f2008-11-18 21:02:19 +010063{
64 struct nfhd_device *dev = queue->queuedata;
Kent Overstreet79886132013-11-23 17:19:00 -080065 struct bio_vec bvec;
66 struct bvec_iter iter;
67 int dir, len, shift;
Kent Overstreet4f024f32013-10-11 15:44:27 -070068 sector_t sec = bio->bi_iter.bi_sector;
Roman Zippelb2edd2f2008-11-18 21:02:19 +010069
70 dir = bio_data_dir(bio);
71 shift = dev->bshift;
Kent Overstreet79886132013-11-23 17:19:00 -080072 bio_for_each_segment(bvec, bio, iter) {
73 len = bvec.bv_len;
Roman Zippelb2edd2f2008-11-18 21:02:19 +010074 len >>= 9;
75 nfhd_read_write(dev->id, 0, dir, sec >> shift, len >> shift,
Kent Overstreet79886132013-11-23 17:19:00 -080076 bvec_to_phys(&bvec));
Roman Zippelb2edd2f2008-11-18 21:02:19 +010077 sec += len;
78 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020079 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -070080 return BLK_QC_T_NONE;
Roman Zippelb2edd2f2008-11-18 21:02:19 +010081}
82
83static int nfhd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
84{
85 struct nfhd_device *dev = bdev->bd_disk->private_data;
86
87 geo->cylinders = dev->blocks >> (6 - dev->bshift);
88 geo->heads = 4;
89 geo->sectors = 16;
90
91 return 0;
92}
93
94static const struct block_device_operations nfhd_ops = {
95 .owner = THIS_MODULE,
96 .getgeo = nfhd_getgeo,
97};
98
99static int __init nfhd_init_one(int id, u32 blocks, u32 bsize)
100{
101 struct nfhd_device *dev;
102 int dev_id = id - NFHD_DEV_OFFSET;
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->queue = blk_alloc_queue(GFP_KERNEL);
122 if (dev->queue == NULL)
123 goto free_dev;
124
125 dev->queue->queuedata = dev;
126 blk_queue_make_request(dev->queue, nfhd_make_request);
127 blk_queue_logical_block_size(dev->queue, bsize);
128
129 dev->disk = alloc_disk(16);
130 if (!dev->disk)
131 goto free_queue;
132
133 dev->disk->major = major_num;
134 dev->disk->first_minor = dev_id * 16;
135 dev->disk->fops = &nfhd_ops;
136 dev->disk->private_data = dev;
137 sprintf(dev->disk->disk_name, "nfhd%u", dev_id);
138 set_capacity(dev->disk, (sector_t)blocks * (bsize / 512));
139 dev->disk->queue = dev->queue;
140
141 add_disk(dev->disk);
142
143 list_add_tail(&dev->list, &nfhd_list);
144
145 return 0;
146
147free_queue:
148 blk_cleanup_queue(dev->queue);
149free_dev:
150 kfree(dev);
151out:
152 return -ENOMEM;
153}
154
155static int __init nfhd_init(void)
156{
157 u32 blocks, bsize;
158 int i;
159
160 nfhd_id = nf_get_id("XHDI");
161 if (!nfhd_id)
162 return -ENODEV;
163
164 major_num = register_blkdev(major_num, "nfhd");
165 if (major_num <= 0) {
166 pr_warn("nfhd: unable to get major number\n");
167 return major_num;
168 }
169
170 for (i = NFHD_DEV_OFFSET; i < 24; i++) {
171 if (nfhd_get_capacity(i, 0, &blocks, &bsize))
172 continue;
173 nfhd_init_one(i, blocks, bsize);
174 }
175
176 return 0;
177}
178
179static void __exit nfhd_exit(void)
180{
181 struct nfhd_device *dev, *next;
182
183 list_for_each_entry_safe(dev, next, &nfhd_list, list) {
184 list_del(&dev->list);
185 del_gendisk(dev->disk);
186 put_disk(dev->disk);
187 blk_cleanup_queue(dev->queue);
188 kfree(dev);
189 }
190 unregister_blkdev(major_num, "nfhd");
191}
192
193module_init(nfhd_init);
194module_exit(nfhd_exit);
195
196MODULE_LICENSE("GPL");