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 { 44b2edd2fdSRoman Zippel return nf_call(nfhd_id + NFHD_GET_CAPACITY, major, minor, blocks, 45b2edd2fdSRoman Zippel 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 625a7bbad2SChristoph Hellwig static void nfhd_make_request(struct request_queue *queue, struct bio *bio) 63b2edd2fdSRoman Zippel { 64b2edd2fdSRoman Zippel struct nfhd_device *dev = queue->queuedata; 65b2edd2fdSRoman Zippel struct bio_vec *bvec; 66b2edd2fdSRoman Zippel int i, dir, len, shift; 67b2edd2fdSRoman Zippel sector_t sec = bio->bi_sector; 68b2edd2fdSRoman Zippel 69b2edd2fdSRoman Zippel dir = bio_data_dir(bio); 70b2edd2fdSRoman Zippel shift = dev->bshift; 71b2edd2fdSRoman Zippel bio_for_each_segment(bvec, bio, i) { 72b2edd2fdSRoman Zippel len = bvec->bv_len; 73b2edd2fdSRoman Zippel len >>= 9; 74b2edd2fdSRoman Zippel nfhd_read_write(dev->id, 0, dir, sec >> shift, len >> shift, 75b2edd2fdSRoman Zippel bvec_to_phys(bvec)); 76b2edd2fdSRoman Zippel sec += len; 77b2edd2fdSRoman Zippel } 78b2edd2fdSRoman Zippel bio_endio(bio, 0); 79b2edd2fdSRoman Zippel } 80b2edd2fdSRoman Zippel 81b2edd2fdSRoman Zippel static int nfhd_getgeo(struct block_device *bdev, struct hd_geometry *geo) 82b2edd2fdSRoman Zippel { 83b2edd2fdSRoman Zippel struct nfhd_device *dev = bdev->bd_disk->private_data; 84b2edd2fdSRoman Zippel 85b2edd2fdSRoman Zippel geo->cylinders = dev->blocks >> (6 - dev->bshift); 86b2edd2fdSRoman Zippel geo->heads = 4; 87b2edd2fdSRoman Zippel geo->sectors = 16; 88b2edd2fdSRoman Zippel 89b2edd2fdSRoman Zippel return 0; 90b2edd2fdSRoman Zippel } 91b2edd2fdSRoman Zippel 92b2edd2fdSRoman Zippel static const struct block_device_operations nfhd_ops = { 93b2edd2fdSRoman Zippel .owner = THIS_MODULE, 94b2edd2fdSRoman Zippel .getgeo = nfhd_getgeo, 95b2edd2fdSRoman Zippel }; 96b2edd2fdSRoman Zippel 97b2edd2fdSRoman Zippel static int __init nfhd_init_one(int id, u32 blocks, u32 bsize) 98b2edd2fdSRoman Zippel { 99b2edd2fdSRoman Zippel struct nfhd_device *dev; 100b2edd2fdSRoman Zippel int dev_id = id - NFHD_DEV_OFFSET; 101b2edd2fdSRoman Zippel 102b2edd2fdSRoman Zippel pr_info("nfhd%u: found device with %u blocks (%u bytes)\n", dev_id, 103b2edd2fdSRoman Zippel blocks, bsize); 104b2edd2fdSRoman Zippel 105b2edd2fdSRoman Zippel if (bsize < 512 || (bsize & (bsize - 1))) { 106b2edd2fdSRoman Zippel pr_warn("nfhd%u: invalid block size\n", dev_id); 107b2edd2fdSRoman Zippel return -EINVAL; 108b2edd2fdSRoman Zippel } 109b2edd2fdSRoman Zippel 110b2edd2fdSRoman Zippel dev = kmalloc(sizeof(struct nfhd_device), GFP_KERNEL); 111b2edd2fdSRoman Zippel if (!dev) 112b2edd2fdSRoman Zippel goto out; 113b2edd2fdSRoman Zippel 114b2edd2fdSRoman Zippel dev->id = id; 115b2edd2fdSRoman Zippel dev->blocks = blocks; 116b2edd2fdSRoman Zippel dev->bsize = bsize; 117b2edd2fdSRoman Zippel dev->bshift = ffs(bsize) - 10; 118b2edd2fdSRoman Zippel 119b2edd2fdSRoman Zippel dev->queue = blk_alloc_queue(GFP_KERNEL); 120b2edd2fdSRoman Zippel if (dev->queue == NULL) 121b2edd2fdSRoman Zippel goto free_dev; 122b2edd2fdSRoman Zippel 123b2edd2fdSRoman Zippel dev->queue->queuedata = dev; 124b2edd2fdSRoman Zippel blk_queue_make_request(dev->queue, nfhd_make_request); 125b2edd2fdSRoman Zippel blk_queue_logical_block_size(dev->queue, bsize); 126b2edd2fdSRoman Zippel 127b2edd2fdSRoman Zippel dev->disk = alloc_disk(16); 128b2edd2fdSRoman Zippel if (!dev->disk) 129b2edd2fdSRoman Zippel goto free_queue; 130b2edd2fdSRoman Zippel 131b2edd2fdSRoman Zippel dev->disk->major = major_num; 132b2edd2fdSRoman Zippel dev->disk->first_minor = dev_id * 16; 133b2edd2fdSRoman Zippel dev->disk->fops = &nfhd_ops; 134b2edd2fdSRoman Zippel dev->disk->private_data = dev; 135b2edd2fdSRoman Zippel sprintf(dev->disk->disk_name, "nfhd%u", dev_id); 136b2edd2fdSRoman Zippel set_capacity(dev->disk, (sector_t)blocks * (bsize / 512)); 137b2edd2fdSRoman Zippel dev->disk->queue = dev->queue; 138b2edd2fdSRoman Zippel 139b2edd2fdSRoman Zippel add_disk(dev->disk); 140b2edd2fdSRoman Zippel 141b2edd2fdSRoman Zippel list_add_tail(&dev->list, &nfhd_list); 142b2edd2fdSRoman Zippel 143b2edd2fdSRoman Zippel return 0; 144b2edd2fdSRoman Zippel 145b2edd2fdSRoman Zippel free_queue: 146b2edd2fdSRoman Zippel blk_cleanup_queue(dev->queue); 147b2edd2fdSRoman Zippel free_dev: 148b2edd2fdSRoman Zippel kfree(dev); 149b2edd2fdSRoman Zippel out: 150b2edd2fdSRoman Zippel return -ENOMEM; 151b2edd2fdSRoman Zippel } 152b2edd2fdSRoman Zippel 153b2edd2fdSRoman Zippel static int __init nfhd_init(void) 154b2edd2fdSRoman Zippel { 155b2edd2fdSRoman Zippel u32 blocks, bsize; 156b2edd2fdSRoman Zippel int i; 157b2edd2fdSRoman Zippel 158b2edd2fdSRoman Zippel nfhd_id = nf_get_id("XHDI"); 159b2edd2fdSRoman Zippel if (!nfhd_id) 160b2edd2fdSRoman Zippel return -ENODEV; 161b2edd2fdSRoman Zippel 162b2edd2fdSRoman Zippel major_num = register_blkdev(major_num, "nfhd"); 163b2edd2fdSRoman Zippel if (major_num <= 0) { 164b2edd2fdSRoman Zippel pr_warn("nfhd: unable to get major number\n"); 165b2edd2fdSRoman Zippel return major_num; 166b2edd2fdSRoman Zippel } 167b2edd2fdSRoman Zippel 168b2edd2fdSRoman Zippel for (i = NFHD_DEV_OFFSET; i < 24; i++) { 169b2edd2fdSRoman Zippel if (nfhd_get_capacity(i, 0, &blocks, &bsize)) 170b2edd2fdSRoman Zippel continue; 171b2edd2fdSRoman Zippel nfhd_init_one(i, blocks, bsize); 172b2edd2fdSRoman Zippel } 173b2edd2fdSRoman Zippel 174b2edd2fdSRoman Zippel return 0; 175b2edd2fdSRoman Zippel } 176b2edd2fdSRoman Zippel 177b2edd2fdSRoman Zippel static void __exit nfhd_exit(void) 178b2edd2fdSRoman Zippel { 179b2edd2fdSRoman Zippel struct nfhd_device *dev, *next; 180b2edd2fdSRoman Zippel 181b2edd2fdSRoman Zippel list_for_each_entry_safe(dev, next, &nfhd_list, list) { 182b2edd2fdSRoman Zippel list_del(&dev->list); 183b2edd2fdSRoman Zippel del_gendisk(dev->disk); 184b2edd2fdSRoman Zippel put_disk(dev->disk); 185b2edd2fdSRoman Zippel blk_cleanup_queue(dev->queue); 186b2edd2fdSRoman Zippel kfree(dev); 187b2edd2fdSRoman Zippel } 188b2edd2fdSRoman Zippel unregister_blkdev(major_num, "nfhd"); 189b2edd2fdSRoman Zippel } 190b2edd2fdSRoman Zippel 191b2edd2fdSRoman Zippel module_init(nfhd_init); 192b2edd2fdSRoman Zippel module_exit(nfhd_exit); 193b2edd2fdSRoman Zippel 194b2edd2fdSRoman Zippel MODULE_LICENSE("GPL"); 195