2008-11-18 20:02:19 +00:00
|
|
|
/*
|
|
|
|
* ARAnyM block device driver
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU General Public
|
|
|
|
* License. See the file COPYING in the main directory of this archive
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/hdreg.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
#include <asm/natfeat.h>
|
|
|
|
|
|
|
|
static long nfhd_id;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
/* emulation entry points */
|
|
|
|
NFHD_READ_WRITE = 10,
|
|
|
|
NFHD_GET_CAPACITY = 14,
|
|
|
|
|
|
|
|
/* skip ACSI devices */
|
|
|
|
NFHD_DEV_OFFSET = 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline s32 nfhd_read_write(u32 major, u32 minor, u32 rwflag, u32 recno,
|
|
|
|
u32 count, u32 buf)
|
|
|
|
{
|
|
|
|
return nf_call(nfhd_id + NFHD_READ_WRITE, major, minor, rwflag, recno,
|
|
|
|
count, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline s32 nfhd_get_capacity(u32 major, u32 minor, u32 *blocks,
|
|
|
|
u32 *blocksize)
|
|
|
|
{
|
2013-08-14 08:45:00 +00:00
|
|
|
return nf_call(nfhd_id + NFHD_GET_CAPACITY, major, minor,
|
|
|
|
virt_to_phys(blocks), virt_to_phys(blocksize));
|
2008-11-18 20:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static LIST_HEAD(nfhd_list);
|
|
|
|
|
|
|
|
static int major_num;
|
|
|
|
module_param(major_num, int, 0);
|
|
|
|
|
|
|
|
struct nfhd_device {
|
|
|
|
struct list_head list;
|
|
|
|
int id;
|
|
|
|
u32 blocks, bsize;
|
|
|
|
int bshift;
|
|
|
|
struct gendisk *disk;
|
|
|
|
};
|
|
|
|
|
2021-10-12 11:12:24 +00:00
|
|
|
static void nfhd_submit_bio(struct bio *bio)
|
2008-11-18 20:02:19 +00:00
|
|
|
{
|
2021-01-24 10:02:34 +00:00
|
|
|
struct nfhd_device *dev = bio->bi_bdev->bd_disk->private_data;
|
2013-11-24 01:19:00 +00:00
|
|
|
struct bio_vec bvec;
|
|
|
|
struct bvec_iter iter;
|
|
|
|
int dir, len, shift;
|
2013-10-11 22:44:27 +00:00
|
|
|
sector_t sec = bio->bi_iter.bi_sector;
|
2008-11-18 20:02:19 +00:00
|
|
|
|
|
|
|
dir = bio_data_dir(bio);
|
|
|
|
shift = dev->bshift;
|
2013-11-24 01:19:00 +00:00
|
|
|
bio_for_each_segment(bvec, bio, iter) {
|
|
|
|
len = bvec.bv_len;
|
2008-11-18 20:02:19 +00:00
|
|
|
len >>= 9;
|
|
|
|
nfhd_read_write(dev->id, 0, dir, sec >> shift, len >> shift,
|
2018-09-24 07:43:53 +00:00
|
|
|
page_to_phys(bvec.bv_page) + bvec.bv_offset);
|
2008-11-18 20:02:19 +00:00
|
|
|
sec += len;
|
|
|
|
}
|
2015-07-20 13:29:37 +00:00
|
|
|
bio_endio(bio);
|
2008-11-18 20:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int nfhd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
|
|
|
|
{
|
|
|
|
struct nfhd_device *dev = bdev->bd_disk->private_data;
|
|
|
|
|
|
|
|
geo->cylinders = dev->blocks >> (6 - dev->bshift);
|
|
|
|
geo->heads = 4;
|
|
|
|
geo->sectors = 16;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct block_device_operations nfhd_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
2020-07-01 08:59:43 +00:00
|
|
|
.submit_bio = nfhd_submit_bio,
|
2008-11-18 20:02:19 +00:00
|
|
|
.getgeo = nfhd_getgeo,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init nfhd_init_one(int id, u32 blocks, u32 bsize)
|
|
|
|
{
|
|
|
|
struct nfhd_device *dev;
|
|
|
|
int dev_id = id - NFHD_DEV_OFFSET;
|
2021-10-15 23:30:25 +00:00
|
|
|
int err = -ENOMEM;
|
2008-11-18 20:02:19 +00:00
|
|
|
|
|
|
|
pr_info("nfhd%u: found device with %u blocks (%u bytes)\n", dev_id,
|
|
|
|
blocks, bsize);
|
|
|
|
|
|
|
|
if (bsize < 512 || (bsize & (bsize - 1))) {
|
|
|
|
pr_warn("nfhd%u: invalid block size\n", dev_id);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = kmalloc(sizeof(struct nfhd_device), GFP_KERNEL);
|
|
|
|
if (!dev)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
dev->id = id;
|
|
|
|
dev->blocks = blocks;
|
|
|
|
dev->bsize = bsize;
|
|
|
|
dev->bshift = ffs(bsize) - 10;
|
|
|
|
|
2021-05-21 05:51:09 +00:00
|
|
|
dev->disk = blk_alloc_disk(NUMA_NO_NODE);
|
2008-11-18 20:02:19 +00:00
|
|
|
if (!dev->disk)
|
2021-05-21 05:51:09 +00:00
|
|
|
goto free_dev;
|
2008-11-18 20:02:19 +00:00
|
|
|
|
|
|
|
dev->disk->major = major_num;
|
|
|
|
dev->disk->first_minor = dev_id * 16;
|
2021-05-21 05:51:09 +00:00
|
|
|
dev->disk->minors = 16;
|
2008-11-18 20:02:19 +00:00
|
|
|
dev->disk->fops = &nfhd_ops;
|
|
|
|
dev->disk->private_data = dev;
|
|
|
|
sprintf(dev->disk->disk_name, "nfhd%u", dev_id);
|
|
|
|
set_capacity(dev->disk, (sector_t)blocks * (bsize / 512));
|
2021-05-21 05:51:09 +00:00
|
|
|
blk_queue_logical_block_size(dev->disk->queue, bsize);
|
2021-10-15 23:30:25 +00:00
|
|
|
err = add_disk(dev->disk);
|
|
|
|
if (err)
|
|
|
|
goto out_cleanup_disk;
|
2008-11-18 20:02:19 +00:00
|
|
|
|
|
|
|
list_add_tail(&dev->list, &nfhd_list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2021-10-15 23:30:25 +00:00
|
|
|
out_cleanup_disk:
|
2022-06-19 06:05:52 +00:00
|
|
|
put_disk(dev->disk);
|
2008-11-18 20:02:19 +00:00
|
|
|
free_dev:
|
|
|
|
kfree(dev);
|
|
|
|
out:
|
2021-10-15 23:30:25 +00:00
|
|
|
return err;
|
2008-11-18 20:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __init nfhd_init(void)
|
|
|
|
{
|
|
|
|
u32 blocks, bsize;
|
2019-02-01 03:26:02 +00:00
|
|
|
int ret;
|
2008-11-18 20:02:19 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
nfhd_id = nf_get_id("XHDI");
|
|
|
|
if (!nfhd_id)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2019-02-01 03:26:02 +00:00
|
|
|
ret = register_blkdev(major_num, "nfhd");
|
|
|
|
if (ret < 0) {
|
2008-11-18 20:02:19 +00:00
|
|
|
pr_warn("nfhd: unable to get major number\n");
|
2019-02-01 03:26:02 +00:00
|
|
|
return ret;
|
2008-11-18 20:02:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 03:26:02 +00:00
|
|
|
if (!major_num)
|
|
|
|
major_num = ret;
|
|
|
|
|
2008-11-18 20:02:19 +00:00
|
|
|
for (i = NFHD_DEV_OFFSET; i < 24; i++) {
|
|
|
|
if (nfhd_get_capacity(i, 0, &blocks, &bsize))
|
|
|
|
continue;
|
|
|
|
nfhd_init_one(i, blocks, bsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit nfhd_exit(void)
|
|
|
|
{
|
|
|
|
struct nfhd_device *dev, *next;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(dev, next, &nfhd_list, list) {
|
|
|
|
list_del(&dev->list);
|
|
|
|
del_gendisk(dev->disk);
|
2022-06-19 06:05:52 +00:00
|
|
|
put_disk(dev->disk);
|
2008-11-18 20:02:19 +00:00
|
|
|
kfree(dev);
|
|
|
|
}
|
|
|
|
unregister_blkdev(major_num, "nfhd");
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(nfhd_init);
|
|
|
|
module_exit(nfhd_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|