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