nfblock.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/genhd.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/hdreg.h>
  17. #include <linux/slab.h>
  18. #include <asm/natfeat.h>
  19. static long nfhd_id;
  20. enum {
  21. /* emulation entry points */
  22. NFHD_READ_WRITE = 10,
  23. NFHD_GET_CAPACITY = 14,
  24. /* skip ACSI devices */
  25. NFHD_DEV_OFFSET = 8,
  26. };
  27. static inline s32 nfhd_read_write(u32 major, u32 minor, u32 rwflag, u32 recno,
  28. u32 count, u32 buf)
  29. {
  30. return nf_call(nfhd_id + NFHD_READ_WRITE, major, minor, rwflag, recno,
  31. count, buf);
  32. }
  33. static inline s32 nfhd_get_capacity(u32 major, u32 minor, u32 *blocks,
  34. u32 *blocksize)
  35. {
  36. return nf_call(nfhd_id + NFHD_GET_CAPACITY, major, minor,
  37. virt_to_phys(blocks), virt_to_phys(blocksize));
  38. }
  39. static LIST_HEAD(nfhd_list);
  40. static int major_num;
  41. module_param(major_num, int, 0);
  42. struct nfhd_device {
  43. struct list_head list;
  44. int id;
  45. u32 blocks, bsize;
  46. int bshift;
  47. struct request_queue *queue;
  48. struct gendisk *disk;
  49. };
  50. static blk_qc_t nfhd_make_request(struct request_queue *queue, struct bio *bio)
  51. {
  52. struct nfhd_device *dev = queue->queuedata;
  53. struct bio_vec bvec;
  54. struct bvec_iter iter;
  55. int dir, len, shift;
  56. sector_t sec = bio->bi_iter.bi_sector;
  57. dir = bio_data_dir(bio);
  58. shift = dev->bshift;
  59. bio_for_each_segment(bvec, bio, iter) {
  60. len = bvec.bv_len;
  61. len >>= 9;
  62. nfhd_read_write(dev->id, 0, dir, sec >> shift, len >> shift,
  63. bvec_to_phys(&bvec));
  64. sec += len;
  65. }
  66. bio_endio(bio);
  67. return BLK_QC_T_NONE;
  68. }
  69. static int nfhd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  70. {
  71. struct nfhd_device *dev = bdev->bd_disk->private_data;
  72. geo->cylinders = dev->blocks >> (6 - dev->bshift);
  73. geo->heads = 4;
  74. geo->sectors = 16;
  75. return 0;
  76. }
  77. static const struct block_device_operations nfhd_ops = {
  78. .owner = THIS_MODULE,
  79. .getgeo = nfhd_getgeo,
  80. };
  81. static int __init nfhd_init_one(int id, u32 blocks, u32 bsize)
  82. {
  83. struct nfhd_device *dev;
  84. int dev_id = id - NFHD_DEV_OFFSET;
  85. pr_info("nfhd%u: found device with %u blocks (%u bytes)\n", dev_id,
  86. blocks, bsize);
  87. if (bsize < 512 || (bsize & (bsize - 1))) {
  88. pr_warn("nfhd%u: invalid block size\n", dev_id);
  89. return -EINVAL;
  90. }
  91. dev = kmalloc(sizeof(struct nfhd_device), GFP_KERNEL);
  92. if (!dev)
  93. goto out;
  94. dev->id = id;
  95. dev->blocks = blocks;
  96. dev->bsize = bsize;
  97. dev->bshift = ffs(bsize) - 10;
  98. dev->queue = blk_alloc_queue(GFP_KERNEL);
  99. if (dev->queue == NULL)
  100. goto free_dev;
  101. dev->queue->queuedata = dev;
  102. blk_queue_make_request(dev->queue, nfhd_make_request);
  103. blk_queue_logical_block_size(dev->queue, bsize);
  104. dev->disk = alloc_disk(16);
  105. if (!dev->disk)
  106. goto free_queue;
  107. dev->disk->major = major_num;
  108. dev->disk->first_minor = dev_id * 16;
  109. dev->disk->fops = &nfhd_ops;
  110. dev->disk->private_data = dev;
  111. sprintf(dev->disk->disk_name, "nfhd%u", dev_id);
  112. set_capacity(dev->disk, (sector_t)blocks * (bsize / 512));
  113. dev->disk->queue = dev->queue;
  114. add_disk(dev->disk);
  115. list_add_tail(&dev->list, &nfhd_list);
  116. return 0;
  117. free_queue:
  118. blk_cleanup_queue(dev->queue);
  119. free_dev:
  120. kfree(dev);
  121. out:
  122. return -ENOMEM;
  123. }
  124. static int __init nfhd_init(void)
  125. {
  126. u32 blocks, bsize;
  127. int i;
  128. nfhd_id = nf_get_id("XHDI");
  129. if (!nfhd_id)
  130. return -ENODEV;
  131. major_num = register_blkdev(major_num, "nfhd");
  132. if (major_num <= 0) {
  133. pr_warn("nfhd: unable to get major number\n");
  134. return major_num;
  135. }
  136. for (i = NFHD_DEV_OFFSET; i < 24; i++) {
  137. if (nfhd_get_capacity(i, 0, &blocks, &bsize))
  138. continue;
  139. nfhd_init_one(i, blocks, bsize);
  140. }
  141. return 0;
  142. }
  143. static void __exit nfhd_exit(void)
  144. {
  145. struct nfhd_device *dev, *next;
  146. list_for_each_entry_safe(dev, next, &nfhd_list, list) {
  147. list_del(&dev->list);
  148. del_gendisk(dev->disk);
  149. put_disk(dev->disk);
  150. blk_cleanup_queue(dev->queue);
  151. kfree(dev);
  152. }
  153. unregister_blkdev(major_num, "nfhd");
  154. }
  155. module_init(nfhd_init);
  156. module_exit(nfhd_exit);
  157. MODULE_LICENSE("GPL");