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 62c62b37d9SChristoph Hellwig static blk_qc_t nfhd_submit_bio(struct bio *bio) 63b2edd2fdSRoman Zippel { 646d41bb4dSChristoph Hellwig struct nfhd_device *dev = bio->bi_disk->private_data; 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, 96c62b37d9SChristoph Hellwig .submit_bio = nfhd_submit_bio, 97b2edd2fdSRoman Zippel .getgeo = nfhd_getgeo, 98b2edd2fdSRoman Zippel }; 99b2edd2fdSRoman Zippel 100b2edd2fdSRoman Zippel static int __init nfhd_init_one(int id, u32 blocks, u32 bsize) 101b2edd2fdSRoman Zippel { 102b2edd2fdSRoman Zippel struct nfhd_device *dev; 103b2edd2fdSRoman Zippel int dev_id = id - NFHD_DEV_OFFSET; 104b2edd2fdSRoman Zippel 105b2edd2fdSRoman Zippel pr_info("nfhd%u: found device with %u blocks (%u bytes)\n", dev_id, 106b2edd2fdSRoman Zippel blocks, bsize); 107b2edd2fdSRoman Zippel 108b2edd2fdSRoman Zippel if (bsize < 512 || (bsize & (bsize - 1))) { 109b2edd2fdSRoman Zippel pr_warn("nfhd%u: invalid block size\n", dev_id); 110b2edd2fdSRoman Zippel return -EINVAL; 111b2edd2fdSRoman Zippel } 112b2edd2fdSRoman Zippel 113b2edd2fdSRoman Zippel dev = kmalloc(sizeof(struct nfhd_device), GFP_KERNEL); 114b2edd2fdSRoman Zippel if (!dev) 115b2edd2fdSRoman Zippel goto out; 116b2edd2fdSRoman Zippel 117b2edd2fdSRoman Zippel dev->id = id; 118b2edd2fdSRoman Zippel dev->blocks = blocks; 119b2edd2fdSRoman Zippel dev->bsize = bsize; 120b2edd2fdSRoman Zippel dev->bshift = ffs(bsize) - 10; 121b2edd2fdSRoman Zippel 122c62b37d9SChristoph Hellwig dev->queue = blk_alloc_queue(NUMA_NO_NODE); 123b2edd2fdSRoman Zippel if (dev->queue == NULL) 124b2edd2fdSRoman Zippel goto free_dev; 125b2edd2fdSRoman Zippel 126b2edd2fdSRoman Zippel blk_queue_logical_block_size(dev->queue, bsize); 127b2edd2fdSRoman Zippel 128b2edd2fdSRoman Zippel dev->disk = alloc_disk(16); 129b2edd2fdSRoman Zippel if (!dev->disk) 130b2edd2fdSRoman Zippel goto free_queue; 131b2edd2fdSRoman Zippel 132b2edd2fdSRoman Zippel dev->disk->major = major_num; 133b2edd2fdSRoman Zippel dev->disk->first_minor = dev_id * 16; 134b2edd2fdSRoman Zippel dev->disk->fops = &nfhd_ops; 135b2edd2fdSRoman Zippel dev->disk->private_data = dev; 136b2edd2fdSRoman Zippel sprintf(dev->disk->disk_name, "nfhd%u", dev_id); 137b2edd2fdSRoman Zippel set_capacity(dev->disk, (sector_t)blocks * (bsize / 512)); 138b2edd2fdSRoman Zippel dev->disk->queue = dev->queue; 139b2edd2fdSRoman Zippel 140b2edd2fdSRoman Zippel add_disk(dev->disk); 141b2edd2fdSRoman Zippel 142b2edd2fdSRoman Zippel list_add_tail(&dev->list, &nfhd_list); 143b2edd2fdSRoman Zippel 144b2edd2fdSRoman Zippel return 0; 145b2edd2fdSRoman Zippel 146b2edd2fdSRoman Zippel free_queue: 147b2edd2fdSRoman Zippel blk_cleanup_queue(dev->queue); 148b2edd2fdSRoman Zippel free_dev: 149b2edd2fdSRoman Zippel kfree(dev); 150b2edd2fdSRoman Zippel out: 151b2edd2fdSRoman Zippel return -ENOMEM; 152b2edd2fdSRoman Zippel } 153b2edd2fdSRoman Zippel 154b2edd2fdSRoman Zippel static int __init nfhd_init(void) 155b2edd2fdSRoman Zippel { 156b2edd2fdSRoman Zippel u32 blocks, bsize; 15730363d65SChengguang Xu int ret; 158b2edd2fdSRoman Zippel int i; 159b2edd2fdSRoman Zippel 160b2edd2fdSRoman Zippel nfhd_id = nf_get_id("XHDI"); 161b2edd2fdSRoman Zippel if (!nfhd_id) 162b2edd2fdSRoman Zippel return -ENODEV; 163b2edd2fdSRoman Zippel 16430363d65SChengguang Xu ret = register_blkdev(major_num, "nfhd"); 16530363d65SChengguang Xu if (ret < 0) { 166b2edd2fdSRoman Zippel pr_warn("nfhd: unable to get major number\n"); 16730363d65SChengguang Xu return ret; 168b2edd2fdSRoman Zippel } 169b2edd2fdSRoman Zippel 17030363d65SChengguang Xu if (!major_num) 17130363d65SChengguang Xu major_num = ret; 17230363d65SChengguang Xu 173b2edd2fdSRoman Zippel for (i = NFHD_DEV_OFFSET; i < 24; i++) { 174b2edd2fdSRoman Zippel if (nfhd_get_capacity(i, 0, &blocks, &bsize)) 175b2edd2fdSRoman Zippel continue; 176b2edd2fdSRoman Zippel nfhd_init_one(i, blocks, bsize); 177b2edd2fdSRoman Zippel } 178b2edd2fdSRoman Zippel 179b2edd2fdSRoman Zippel return 0; 180b2edd2fdSRoman Zippel } 181b2edd2fdSRoman Zippel 182b2edd2fdSRoman Zippel static void __exit nfhd_exit(void) 183b2edd2fdSRoman Zippel { 184b2edd2fdSRoman Zippel struct nfhd_device *dev, *next; 185b2edd2fdSRoman Zippel 186b2edd2fdSRoman Zippel list_for_each_entry_safe(dev, next, &nfhd_list, list) { 187b2edd2fdSRoman Zippel list_del(&dev->list); 188b2edd2fdSRoman Zippel del_gendisk(dev->disk); 189b2edd2fdSRoman Zippel put_disk(dev->disk); 190b2edd2fdSRoman Zippel blk_cleanup_queue(dev->queue); 191b2edd2fdSRoman Zippel kfree(dev); 192b2edd2fdSRoman Zippel } 193b2edd2fdSRoman Zippel unregister_blkdev(major_num, "nfhd"); 194b2edd2fdSRoman Zippel } 195b2edd2fdSRoman Zippel 196b2edd2fdSRoman Zippel module_init(nfhd_init); 197b2edd2fdSRoman Zippel module_exit(nfhd_exit); 198b2edd2fdSRoman Zippel 199b2edd2fdSRoman Zippel MODULE_LICENSE("GPL"); 200