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