blob: 8eb6ad1a3a1dec402dd852aa60224f250d4da440 [file] [log] [blame]
Victor Prupisb6c7e872008-05-19 14:50:38 -07001/*
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 Torvalds7c0f6ba2016-12-24 11:46:01 -080020#include <linux/uaccess.h>
Victor Prupisb6c7e872008-05-19 14:50:38 -070021#include <platform/simcall.h>
22
23#define SIMDISK_MAJOR 240
Victor Prupisb6c7e872008-05-19 14:50:38 -070024#define SIMDISK_MINORS 1
25#define MAX_SIMDISK_COUNT 10
26
27struct simdisk {
28 const char *filename;
29 spinlock_t lock;
Victor Prupisb6c7e872008-05-19 14:50:38 -070030 struct gendisk *gd;
31 struct proc_dir_entry *procfile;
32 int users;
33 unsigned long size;
34 int fd;
35};
36
37
38static int simdisk_count = CONFIG_BLK_DEV_SIMDISK_COUNT;
39module_param(simdisk_count, int, S_IRUGO);
40MODULE_PARM_DESC(simdisk_count, "Number of simdisk units.");
41
42static int n_files;
43static 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
52static 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
62static const struct kernel_param_ops simdisk_param_ops_filename = {
63 .set = simdisk_param_set_filename,
64};
65module_param_cb(filename, &simdisk_param_ops_filename, &n_files, 0);
66MODULE_PARM_DESC(filename, "Backing storage filename.");
67
68static int simdisk_major = SIMDISK_MAJOR;
69
70static 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 Filippovf4b93ba2013-04-15 08:39:41 +040086 simc_lseek(dev->fd, offset, SEEK_SET);
Max Filippov549409b42016-09-20 17:58:20 -070087 READ_ONCE(*buffer);
Victor Prupisb6c7e872008-05-19 14:50:38 -070088 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 Hellwig3e087732021-10-12 13:12:24 +0200103static void simdisk_submit_bio(struct bio *bio)
Victor Prupisb6c7e872008-05-19 14:50:38 -0700104{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100105 struct simdisk *dev = bio->bi_bdev->bd_disk->private_data;
Jens Axboe675675a2014-01-29 08:37:32 -0700106 struct bio_vec bvec;
107 struct bvec_iter iter;
108 sector_t sector = bio->bi_iter.bi_sector;
Victor Prupisb6c7e872008-05-19 14:50:38 -0700109
Jens Axboe675675a2014-01-29 08:37:32 -0700110 bio_for_each_segment(bvec, bio, iter) {
Christoph Hellwigd004a5e72017-11-08 19:13:48 +0100111 char *buffer = kmap_atomic(bvec.bv_page) + bvec.bv_offset;
Jens Axboe675675a2014-01-29 08:37:32 -0700112 unsigned len = bvec.bv_len >> SECTOR_SHIFT;
Victor Prupisb6c7e872008-05-19 14:50:38 -0700113
114 simdisk_transfer(dev, sector, len, buffer,
115 bio_data_dir(bio) == WRITE);
116 sector += len;
Christoph Hellwig0e78ecc2017-11-10 09:17:50 +0100117 kunmap_atomic(buffer);
Victor Prupisb6c7e872008-05-19 14:50:38 -0700118 }
Victor Prupisb6c7e872008-05-19 14:50:38 -0700119
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200120 bio_endio(bio);
Victor Prupisb6c7e872008-05-19 14:50:38 -0700121}
122
Victor Prupisb6c7e872008-05-19 14:50:38 -0700123static 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 Prupisb6c7e872008-05-19 14:50:38 -0700128 ++dev->users;
129 spin_unlock(&dev->lock);
130 return 0;
131}
132
Al Virodb2a1442013-05-05 21:52:57 -0400133static void simdisk_release(struct gendisk *disk, fmode_t mode)
Victor Prupisb6c7e872008-05-19 14:50:38 -0700134{
135 struct simdisk *dev = disk->private_data;
136 spin_lock(&dev->lock);
137 --dev->users;
138 spin_unlock(&dev->lock);
Victor Prupisb6c7e872008-05-19 14:50:38 -0700139}
140
141static const struct block_device_operations simdisk_ops = {
142 .owner = THIS_MODULE,
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200143 .submit_bio = simdisk_submit_bio,
Victor Prupisb6c7e872008-05-19 14:50:38 -0700144 .open = simdisk_open,
145 .release = simdisk_release,
146};
147
148static struct simdisk *sddev;
149static struct proc_dir_entry *simdisk_procdir;
150
151static 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 Filippovf4b93ba2013-04-15 08:39:41 +0400171 dev->size = simc_lseek(dev->fd, 0, SEEK_END);
Victor Prupisb6c7e872008-05-19 14:50:38 -0700172 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);
175out:
176 if (err)
177 kfree(filename);
178 spin_unlock(&dev->lock);
179
180 return err;
181}
182
183static 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 Viroa69755b2013-03-31 15:18:19 -0400208static ssize_t proc_read_simdisk(struct file *file, char __user *buf,
209 size_t size, loff_t *ppos)
Victor Prupisb6c7e872008-05-19 14:50:38 -0700210{
Muchun Song359745d2022-01-21 22:14:23 -0800211 struct simdisk *dev = pde_data(file_inode(file));
Geert Uytterhoeven0757f612013-05-09 14:08:31 +0200212 const char *s = dev->filename;
Al Viroa69755b2013-03-31 15:18:19 -0400213 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 Prupisb6c7e872008-05-19 14:50:38 -0700222}
223
Al Viroa69755b2013-03-31 15:18:19 -0400224static ssize_t proc_write_simdisk(struct file *file, const char __user *buf,
Al Viro4fe56e82013-05-04 16:00:50 -0400225 size_t count, loff_t *ppos)
Victor Prupisb6c7e872008-05-19 14:50:38 -0700226{
Al Viro16e5c1f2015-12-24 00:06:05 -0500227 char *tmp = memdup_user_nul(buf, count);
Muchun Song359745d2022-01-21 22:14:23 -0800228 struct simdisk *dev = pde_data(file_inode(file));
Victor Prupisb6c7e872008-05-19 14:50:38 -0700229 int err;
230
Al Viro16e5c1f2015-12-24 00:06:05 -0500231 if (IS_ERR(tmp))
232 return PTR_ERR(tmp);
Victor Prupisb6c7e872008-05-19 14:50:38 -0700233
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 Prupisb6c7e872008-05-19 14:50:38 -0700240
241 if (tmp[0])
242 err = simdisk_attach(dev, tmp);
243
244 if (err == 0)
245 err = count;
246out_free:
247 kfree(tmp);
248 return err;
249}
250
Alexey Dobriyan97a32532020-02-03 17:37:17 -0800251static 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 Viroa69755b2013-03-31 15:18:19 -0400255};
256
Victor Prupisb6c7e872008-05-19 14:50:38 -0700257static int __init simdisk_setup(struct simdisk *dev, int which,
258 struct proc_dir_entry *procdir)
259{
260 char tmp[2] = { '0' + which, 0 };
Luis Chamberlaindb8eda92021-09-27 15:01:01 -0700261 int err = -ENOMEM;
Victor Prupisb6c7e872008-05-19 14:50:38 -0700262
263 dev->fd = -1;
264 dev->filename = NULL;
265 spin_lock_init(&dev->lock);
266 dev->users = 0;
267
Christoph Hellwigb1833ed2021-05-21 07:51:10 +0200268 dev->gd = blk_alloc_disk(NUMA_NO_NODE);
269 if (!dev->gd)
Luis Chamberlaindb8eda92021-09-27 15:01:01 -0700270 goto out;
Victor Prupisb6c7e872008-05-19 14:50:38 -0700271 dev->gd->major = simdisk_major;
272 dev->gd->first_minor = which;
Christoph Hellwigb1833ed2021-05-21 07:51:10 +0200273 dev->gd->minors = SIMDISK_MINORS;
Victor Prupisb6c7e872008-05-19 14:50:38 -0700274 dev->gd->fops = &simdisk_ops;
Victor Prupisb6c7e872008-05-19 14:50:38 -0700275 dev->gd->private_data = dev;
276 snprintf(dev->gd->disk_name, 32, "simdisk%d", which);
277 set_capacity(dev->gd, 0);
Luis Chamberlaindb8eda92021-09-27 15:01:01 -0700278 err = add_disk(dev->gd);
279 if (err)
280 goto out_cleanup_disk;
Victor Prupisb6c7e872008-05-19 14:50:38 -0700281
Alexey Dobriyan97a32532020-02-03 17:37:17 -0800282 dev->procfile = proc_create_data(tmp, 0644, procdir, &simdisk_proc_ops, dev);
Luis Chamberlaindb8eda92021-09-27 15:01:01 -0700283
Victor Prupisb6c7e872008-05-19 14:50:38 -0700284 return 0;
Luis Chamberlaindb8eda92021-09-27 15:01:01 -0700285
286out_cleanup_disk:
287 blk_cleanup_disk(dev->gd);
288out:
289 return err;
Victor Prupisb6c7e872008-05-19 14:50:38 -0700290}
291
292static 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 Elfring5e78e462016-08-25 14:28:09 +0200307 sddev = kmalloc_array(simdisk_count, sizeof(*sddev), GFP_KERNEL);
Victor Prupisb6c7e872008-05-19 14:50:38 -0700308 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
325out_free_unregister:
326 kfree(sddev);
327out_unregister:
328 unregister_blkdev(simdisk_major, "simdisk");
329 return -ENOMEM;
330}
331module_init(simdisk_init);
332
333static 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 Hellwigb1833ed2021-05-21 07:51:10 +0200339 if (dev->gd) {
Victor Prupisb6c7e872008-05-19 14:50:38 -0700340 del_gendisk(dev->gd);
Christoph Hellwigb1833ed2021-05-21 07:51:10 +0200341 blk_cleanup_disk(dev->gd);
342 }
Victor Prupisb6c7e872008-05-19 14:50:38 -0700343 remove_proc_entry(tmp, procdir);
344}
345
346static 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}
356module_exit(simdisk_exit);
357
358MODULE_ALIAS_BLOCKDEV_MAJOR(SIMDISK_MAJOR);
359
360MODULE_LICENSE("GPL");