Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 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> |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 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 | { |
Geert Uytterhoeven | 5549005 | 2013-08-14 10:45:00 +0200 | [diff] [blame] | 43 | return nf_call(nfhd_id + NFHD_GET_CAPACITY, major, minor, |
| 44 | virt_to_phys(blocks), virt_to_phys(blocksize)); |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 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; |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 57 | struct gendisk *disk; |
| 58 | }; |
| 59 | |
Christoph Hellwig | 3e08773 | 2021-10-12 13:12:24 +0200 | [diff] [blame] | 60 | static void nfhd_submit_bio(struct bio *bio) |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 61 | { |
Christoph Hellwig | 309dca30 | 2021-01-24 11:02:34 +0100 | [diff] [blame] | 62 | struct nfhd_device *dev = bio->bi_bdev->bd_disk->private_data; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 63 | struct bio_vec bvec; |
| 64 | struct bvec_iter iter; |
| 65 | int dir, len, shift; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 66 | sector_t sec = bio->bi_iter.bi_sector; |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 67 | |
| 68 | dir = bio_data_dir(bio); |
| 69 | shift = dev->bshift; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 70 | bio_for_each_segment(bvec, bio, iter) { |
| 71 | len = bvec.bv_len; |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 72 | len >>= 9; |
| 73 | nfhd_read_write(dev->id, 0, dir, sec >> shift, len >> shift, |
Christoph Hellwig | 6e76846 | 2018-09-24 09:43:53 +0200 | [diff] [blame] | 74 | page_to_phys(bvec.bv_page) + bvec.bv_offset); |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 75 | sec += len; |
| 76 | } |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 77 | bio_endio(bio); |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 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, |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 93 | .submit_bio = nfhd_submit_bio, |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 94 | .getgeo = nfhd_getgeo, |
| 95 | }; |
| 96 | |
| 97 | static int __init nfhd_init_one(int id, u32 blocks, u32 bsize) |
| 98 | { |
| 99 | struct nfhd_device *dev; |
| 100 | int dev_id = id - NFHD_DEV_OFFSET; |
Luis Chamberlain | 21fd880 | 2021-10-15 16:30:25 -0700 | [diff] [blame] | 101 | int err = -ENOMEM; |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 102 | |
| 103 | pr_info("nfhd%u: found device with %u blocks (%u bytes)\n", dev_id, |
| 104 | blocks, bsize); |
| 105 | |
| 106 | if (bsize < 512 || (bsize & (bsize - 1))) { |
| 107 | pr_warn("nfhd%u: invalid block size\n", dev_id); |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | |
| 111 | dev = kmalloc(sizeof(struct nfhd_device), GFP_KERNEL); |
| 112 | if (!dev) |
| 113 | goto out; |
| 114 | |
| 115 | dev->id = id; |
| 116 | dev->blocks = blocks; |
| 117 | dev->bsize = bsize; |
| 118 | dev->bshift = ffs(bsize) - 10; |
| 119 | |
Christoph Hellwig | c3e2353 | 2021-05-21 07:51:09 +0200 | [diff] [blame] | 120 | dev->disk = blk_alloc_disk(NUMA_NO_NODE); |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 121 | if (!dev->disk) |
Christoph Hellwig | c3e2353 | 2021-05-21 07:51:09 +0200 | [diff] [blame] | 122 | goto free_dev; |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 123 | |
| 124 | dev->disk->major = major_num; |
| 125 | dev->disk->first_minor = dev_id * 16; |
Christoph Hellwig | c3e2353 | 2021-05-21 07:51:09 +0200 | [diff] [blame] | 126 | dev->disk->minors = 16; |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 127 | dev->disk->fops = &nfhd_ops; |
| 128 | dev->disk->private_data = dev; |
| 129 | sprintf(dev->disk->disk_name, "nfhd%u", dev_id); |
| 130 | set_capacity(dev->disk, (sector_t)blocks * (bsize / 512)); |
Christoph Hellwig | c3e2353 | 2021-05-21 07:51:09 +0200 | [diff] [blame] | 131 | blk_queue_logical_block_size(dev->disk->queue, bsize); |
Luis Chamberlain | 21fd880 | 2021-10-15 16:30:25 -0700 | [diff] [blame] | 132 | err = add_disk(dev->disk); |
| 133 | if (err) |
| 134 | goto out_cleanup_disk; |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 135 | |
| 136 | list_add_tail(&dev->list, &nfhd_list); |
| 137 | |
| 138 | return 0; |
| 139 | |
Luis Chamberlain | 21fd880 | 2021-10-15 16:30:25 -0700 | [diff] [blame] | 140 | out_cleanup_disk: |
Christoph Hellwig | 8b9ab62 | 2022-06-19 08:05:52 +0200 | [diff] [blame] | 141 | put_disk(dev->disk); |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 142 | free_dev: |
| 143 | kfree(dev); |
| 144 | out: |
Luis Chamberlain | 21fd880 | 2021-10-15 16:30:25 -0700 | [diff] [blame] | 145 | return err; |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static int __init nfhd_init(void) |
| 149 | { |
| 150 | u32 blocks, bsize; |
Chengguang Xu | 30363d6 | 2019-02-01 11:26:02 +0800 | [diff] [blame] | 151 | int ret; |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 152 | int i; |
| 153 | |
| 154 | nfhd_id = nf_get_id("XHDI"); |
| 155 | if (!nfhd_id) |
| 156 | return -ENODEV; |
| 157 | |
Chengguang Xu | 30363d6 | 2019-02-01 11:26:02 +0800 | [diff] [blame] | 158 | ret = register_blkdev(major_num, "nfhd"); |
| 159 | if (ret < 0) { |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 160 | pr_warn("nfhd: unable to get major number\n"); |
Chengguang Xu | 30363d6 | 2019-02-01 11:26:02 +0800 | [diff] [blame] | 161 | return ret; |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Chengguang Xu | 30363d6 | 2019-02-01 11:26:02 +0800 | [diff] [blame] | 164 | if (!major_num) |
| 165 | major_num = ret; |
| 166 | |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 167 | for (i = NFHD_DEV_OFFSET; i < 24; i++) { |
| 168 | if (nfhd_get_capacity(i, 0, &blocks, &bsize)) |
| 169 | continue; |
| 170 | nfhd_init_one(i, blocks, bsize); |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static void __exit nfhd_exit(void) |
| 177 | { |
| 178 | struct nfhd_device *dev, *next; |
| 179 | |
| 180 | list_for_each_entry_safe(dev, next, &nfhd_list, list) { |
| 181 | list_del(&dev->list); |
| 182 | del_gendisk(dev->disk); |
Christoph Hellwig | 8b9ab62 | 2022-06-19 08:05:52 +0200 | [diff] [blame] | 183 | put_disk(dev->disk); |
Roman Zippel | b2edd2f | 2008-11-18 21:02:19 +0100 | [diff] [blame] | 184 | kfree(dev); |
| 185 | } |
| 186 | unregister_blkdev(major_num, "nfhd"); |
| 187 | } |
| 188 | |
| 189 | module_init(nfhd_init); |
| 190 | module_exit(nfhd_exit); |
| 191 | |
| 192 | MODULE_LICENSE("GPL"); |