linux/arch/xtensa/platforms/iss/simdisk.c
<<
>>
Prefs
   1/*
   2 * arch/xtensa/platforms/iss/simdisk.c
   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 * Copyright (C) 2001-2013 Tensilica Inc.
   9 *   Authors    Victor Prupis
  10 */
  11
  12#include <linux/module.h>
  13#include <linux/moduleparam.h>
  14#include <linux/kernel.h>
  15#include <linux/init.h>
  16#include <linux/string.h>
  17#include <linux/blkdev.h>
  18#include <linux/bio.h>
  19#include <linux/proc_fs.h>
  20#include <linux/uaccess.h>
  21#include <platform/simcall.h>
  22
  23#define SIMDISK_MAJOR 240
  24#define SIMDISK_MINORS 1
  25#define MAX_SIMDISK_COUNT 10
  26
  27struct simdisk {
  28        const char *filename;
  29        spinlock_t lock;
  30        struct request_queue *queue;
  31        struct gendisk *gd;
  32        struct proc_dir_entry *procfile;
  33        int users;
  34        unsigned long size;
  35        int fd;
  36};
  37
  38
  39static int simdisk_count = CONFIG_BLK_DEV_SIMDISK_COUNT;
  40module_param(simdisk_count, int, S_IRUGO);
  41MODULE_PARM_DESC(simdisk_count, "Number of simdisk units.");
  42
  43static int n_files;
  44static const char *filename[MAX_SIMDISK_COUNT] = {
  45#ifdef CONFIG_SIMDISK0_FILENAME
  46        CONFIG_SIMDISK0_FILENAME,
  47#ifdef CONFIG_SIMDISK1_FILENAME
  48        CONFIG_SIMDISK1_FILENAME,
  49#endif
  50#endif
  51};
  52
  53static int simdisk_param_set_filename(const char *val,
  54                const struct kernel_param *kp)
  55{
  56        if (n_files < ARRAY_SIZE(filename))
  57                filename[n_files++] = val;
  58        else
  59                return -EINVAL;
  60        return 0;
  61}
  62
  63static const struct kernel_param_ops simdisk_param_ops_filename = {
  64        .set = simdisk_param_set_filename,
  65};
  66module_param_cb(filename, &simdisk_param_ops_filename, &n_files, 0);
  67MODULE_PARM_DESC(filename, "Backing storage filename.");
  68
  69static int simdisk_major = SIMDISK_MAJOR;
  70
  71static void simdisk_transfer(struct simdisk *dev, unsigned long sector,
  72                unsigned long nsect, char *buffer, int write)
  73{
  74        unsigned long offset = sector << SECTOR_SHIFT;
  75        unsigned long nbytes = nsect << SECTOR_SHIFT;
  76
  77        if (offset > dev->size || dev->size - offset < nbytes) {
  78                pr_notice("Beyond-end %s (%ld %ld)\n",
  79                                write ? "write" : "read", offset, nbytes);
  80                return;
  81        }
  82
  83        spin_lock(&dev->lock);
  84        while (nbytes > 0) {
  85                unsigned long io;
  86
  87                simc_lseek(dev->fd, offset, SEEK_SET);
  88                READ_ONCE(*buffer);
  89                if (write)
  90                        io = simc_write(dev->fd, buffer, nbytes);
  91                else
  92                        io = simc_read(dev->fd, buffer, nbytes);
  93                if (io == -1) {
  94                        pr_err("SIMDISK: IO error %d\n", errno);
  95                        break;
  96                }
  97                buffer += io;
  98                offset += io;
  99                nbytes -= io;
 100        }
 101        spin_unlock(&dev->lock);
 102}
 103
 104static blk_qc_t simdisk_make_request(struct request_queue *q, struct bio *bio)
 105{
 106        struct simdisk *dev = q->queuedata;
 107        struct bio_vec bvec;
 108        struct bvec_iter iter;
 109        sector_t sector = bio->bi_iter.bi_sector;
 110
 111        bio_for_each_segment(bvec, bio, iter) {
 112                char *buffer = kmap_atomic(bvec.bv_page) + bvec.bv_offset;
 113                unsigned len = bvec.bv_len >> SECTOR_SHIFT;
 114
 115                simdisk_transfer(dev, sector, len, buffer,
 116                                bio_data_dir(bio) == WRITE);
 117                sector += len;
 118                kunmap_atomic(buffer);
 119        }
 120
 121        bio_endio(bio);
 122        return BLK_QC_T_NONE;
 123}
 124
 125static int simdisk_open(struct block_device *bdev, fmode_t mode)
 126{
 127        struct simdisk *dev = bdev->bd_disk->private_data;
 128
 129        spin_lock(&dev->lock);
 130        if (!dev->users)
 131                check_disk_change(bdev);
 132        ++dev->users;
 133        spin_unlock(&dev->lock);
 134        return 0;
 135}
 136
 137static void simdisk_release(struct gendisk *disk, fmode_t mode)
 138{
 139        struct simdisk *dev = disk->private_data;
 140        spin_lock(&dev->lock);
 141        --dev->users;
 142        spin_unlock(&dev->lock);
 143}
 144
 145static const struct block_device_operations simdisk_ops = {
 146        .owner          = THIS_MODULE,
 147        .open           = simdisk_open,
 148        .release        = simdisk_release,
 149};
 150
 151static struct simdisk *sddev;
 152static struct proc_dir_entry *simdisk_procdir;
 153
 154static int simdisk_attach(struct simdisk *dev, const char *filename)
 155{
 156        int err = 0;
 157
 158        filename = kstrdup(filename, GFP_KERNEL);
 159        if (filename == NULL)
 160                return -ENOMEM;
 161
 162        spin_lock(&dev->lock);
 163
 164        if (dev->fd != -1) {
 165                err = -EBUSY;
 166                goto out;
 167        }
 168        dev->fd = simc_open(filename, O_RDWR, 0);
 169        if (dev->fd == -1) {
 170                pr_err("SIMDISK: Can't open %s: %d\n", filename, errno);
 171                err = -ENODEV;
 172                goto out;
 173        }
 174        dev->size = simc_lseek(dev->fd, 0, SEEK_END);
 175        set_capacity(dev->gd, dev->size >> SECTOR_SHIFT);
 176        dev->filename = filename;
 177        pr_info("SIMDISK: %s=%s\n", dev->gd->disk_name, dev->filename);
 178out:
 179        if (err)
 180                kfree(filename);
 181        spin_unlock(&dev->lock);
 182
 183        return err;
 184}
 185
 186static int simdisk_detach(struct simdisk *dev)
 187{
 188        int err = 0;
 189
 190        spin_lock(&dev->lock);
 191
 192        if (dev->users != 0) {
 193                err = -EBUSY;
 194        } else if (dev->fd != -1) {
 195                if (simc_close(dev->fd)) {
 196                        pr_err("SIMDISK: error closing %s: %d\n",
 197                                        dev->filename, errno);
 198                        err = -EIO;
 199                } else {
 200                        pr_info("SIMDISK: %s detached from %s\n",
 201                                        dev->gd->disk_name, dev->filename);
 202                        dev->fd = -1;
 203                        kfree(dev->filename);
 204                        dev->filename = NULL;
 205                }
 206        }
 207        spin_unlock(&dev->lock);
 208        return err;
 209}
 210
 211static ssize_t proc_read_simdisk(struct file *file, char __user *buf,
 212                        size_t size, loff_t *ppos)
 213{
 214        struct simdisk *dev = PDE_DATA(file_inode(file));
 215        const char *s = dev->filename;
 216        if (s) {
 217                ssize_t n = simple_read_from_buffer(buf, size, ppos,
 218                                                        s, strlen(s));
 219                if (n < 0)
 220                        return n;
 221                buf += n;
 222                size -= n;
 223        }
 224        return simple_read_from_buffer(buf, size, ppos, "\n", 1);
 225}
 226
 227static ssize_t proc_write_simdisk(struct file *file, const char __user *buf,
 228                        size_t count, loff_t *ppos)
 229{
 230        char *tmp = memdup_user_nul(buf, count);
 231        struct simdisk *dev = PDE_DATA(file_inode(file));
 232        int err;
 233
 234        if (IS_ERR(tmp))
 235                return PTR_ERR(tmp);
 236
 237        err = simdisk_detach(dev);
 238        if (err != 0)
 239                goto out_free;
 240
 241        if (count > 0 && tmp[count - 1] == '\n')
 242                tmp[count - 1] = 0;
 243
 244        if (tmp[0])
 245                err = simdisk_attach(dev, tmp);
 246
 247        if (err == 0)
 248                err = count;
 249out_free:
 250        kfree(tmp);
 251        return err;
 252}
 253
 254static const struct file_operations fops = {
 255        .read = proc_read_simdisk,
 256        .write = proc_write_simdisk,
 257        .llseek = default_llseek,
 258};
 259
 260static int __init simdisk_setup(struct simdisk *dev, int which,
 261                struct proc_dir_entry *procdir)
 262{
 263        char tmp[2] = { '0' + which, 0 };
 264
 265        dev->fd = -1;
 266        dev->filename = NULL;
 267        spin_lock_init(&dev->lock);
 268        dev->users = 0;
 269
 270        dev->queue = blk_alloc_queue(GFP_KERNEL);
 271        if (dev->queue == NULL) {
 272                pr_err("blk_alloc_queue failed\n");
 273                goto out_alloc_queue;
 274        }
 275
 276        blk_queue_make_request(dev->queue, simdisk_make_request);
 277        dev->queue->queuedata = dev;
 278
 279        dev->gd = alloc_disk(SIMDISK_MINORS);
 280        if (dev->gd == NULL) {
 281                pr_err("alloc_disk failed\n");
 282                goto out_alloc_disk;
 283        }
 284        dev->gd->major = simdisk_major;
 285        dev->gd->first_minor = which;
 286        dev->gd->fops = &simdisk_ops;
 287        dev->gd->queue = dev->queue;
 288        dev->gd->private_data = dev;
 289        snprintf(dev->gd->disk_name, 32, "simdisk%d", which);
 290        set_capacity(dev->gd, 0);
 291        add_disk(dev->gd);
 292
 293        dev->procfile = proc_create_data(tmp, 0644, procdir, &fops, dev);
 294        return 0;
 295
 296out_alloc_disk:
 297        blk_cleanup_queue(dev->queue);
 298        dev->queue = NULL;
 299out_alloc_queue:
 300        simc_close(dev->fd);
 301        return -EIO;
 302}
 303
 304static int __init simdisk_init(void)
 305{
 306        int i;
 307
 308        if (register_blkdev(simdisk_major, "simdisk") < 0) {
 309                pr_err("SIMDISK: register_blkdev: %d\n", simdisk_major);
 310                return -EIO;
 311        }
 312        pr_info("SIMDISK: major: %d\n", simdisk_major);
 313
 314        if (n_files > simdisk_count)
 315                simdisk_count = n_files;
 316        if (simdisk_count > MAX_SIMDISK_COUNT)
 317                simdisk_count = MAX_SIMDISK_COUNT;
 318
 319        sddev = kmalloc_array(simdisk_count, sizeof(*sddev), GFP_KERNEL);
 320        if (sddev == NULL)
 321                goto out_unregister;
 322
 323        simdisk_procdir = proc_mkdir("simdisk", 0);
 324        if (simdisk_procdir == NULL)
 325                goto out_free_unregister;
 326
 327        for (i = 0; i < simdisk_count; ++i) {
 328                if (simdisk_setup(sddev + i, i, simdisk_procdir) == 0) {
 329                        if (filename[i] != NULL && filename[i][0] != 0 &&
 330                                        (n_files == 0 || i < n_files))
 331                                simdisk_attach(sddev + i, filename[i]);
 332                }
 333        }
 334
 335        return 0;
 336
 337out_free_unregister:
 338        kfree(sddev);
 339out_unregister:
 340        unregister_blkdev(simdisk_major, "simdisk");
 341        return -ENOMEM;
 342}
 343module_init(simdisk_init);
 344
 345static void simdisk_teardown(struct simdisk *dev, int which,
 346                struct proc_dir_entry *procdir)
 347{
 348        char tmp[2] = { '0' + which, 0 };
 349
 350        simdisk_detach(dev);
 351        if (dev->gd)
 352                del_gendisk(dev->gd);
 353        if (dev->queue)
 354                blk_cleanup_queue(dev->queue);
 355        remove_proc_entry(tmp, procdir);
 356}
 357
 358static void __exit simdisk_exit(void)
 359{
 360        int i;
 361
 362        for (i = 0; i < simdisk_count; ++i)
 363                simdisk_teardown(sddev + i, i, simdisk_procdir);
 364        remove_proc_entry("simdisk", 0);
 365        kfree(sddev);
 366        unregister_blkdev(simdisk_major, "simdisk");
 367}
 368module_exit(simdisk_exit);
 369
 370MODULE_ALIAS_BLOCKDEV_MAJOR(SIMDISK_MAJOR);
 371
 372MODULE_LICENSE("GPL");
 373