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 
27 struct simdisk {
28 	const char *filename;
29 	spinlock_t lock;
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 
86 		simc_lseek(dev->fd, offset, SEEK_SET);
87 		READ_ONCE(*buffer);
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 
103 static blk_qc_t simdisk_submit_bio(struct bio *bio)
104 {
105 	struct simdisk *dev = bio->bi_bdev->bd_disk->private_data;
106 	struct bio_vec bvec;
107 	struct bvec_iter iter;
108 	sector_t sector = bio->bi_iter.bi_sector;
109 
110 	bio_for_each_segment(bvec, bio, iter) {
111 		char *buffer = kmap_atomic(bvec.bv_page) + bvec.bv_offset;
112 		unsigned len = bvec.bv_len >> SECTOR_SHIFT;
113 
114 		simdisk_transfer(dev, sector, len, buffer,
115 				bio_data_dir(bio) == WRITE);
116 		sector += len;
117 		kunmap_atomic(buffer);
118 	}
119 
120 	bio_endio(bio);
121 	return BLK_QC_T_NONE;
122 }
123 
124 static int simdisk_open(struct block_device *bdev, fmode_t mode)
125 {
126 	struct simdisk *dev = bdev->bd_disk->private_data;
127 
128 	spin_lock(&dev->lock);
129 	++dev->users;
130 	spin_unlock(&dev->lock);
131 	return 0;
132 }
133 
134 static void simdisk_release(struct gendisk *disk, fmode_t mode)
135 {
136 	struct simdisk *dev = disk->private_data;
137 	spin_lock(&dev->lock);
138 	--dev->users;
139 	spin_unlock(&dev->lock);
140 }
141 
142 static const struct block_device_operations simdisk_ops = {
143 	.owner		= THIS_MODULE,
144 	.submit_bio	= simdisk_submit_bio,
145 	.open		= simdisk_open,
146 	.release	= simdisk_release,
147 };
148 
149 static struct simdisk *sddev;
150 static struct proc_dir_entry *simdisk_procdir;
151 
152 static int simdisk_attach(struct simdisk *dev, const char *filename)
153 {
154 	int err = 0;
155 
156 	filename = kstrdup(filename, GFP_KERNEL);
157 	if (filename == NULL)
158 		return -ENOMEM;
159 
160 	spin_lock(&dev->lock);
161 
162 	if (dev->fd != -1) {
163 		err = -EBUSY;
164 		goto out;
165 	}
166 	dev->fd = simc_open(filename, O_RDWR, 0);
167 	if (dev->fd == -1) {
168 		pr_err("SIMDISK: Can't open %s: %d\n", filename, errno);
169 		err = -ENODEV;
170 		goto out;
171 	}
172 	dev->size = simc_lseek(dev->fd, 0, SEEK_END);
173 	set_capacity(dev->gd, dev->size >> SECTOR_SHIFT);
174 	dev->filename = filename;
175 	pr_info("SIMDISK: %s=%s\n", dev->gd->disk_name, dev->filename);
176 out:
177 	if (err)
178 		kfree(filename);
179 	spin_unlock(&dev->lock);
180 
181 	return err;
182 }
183 
184 static int simdisk_detach(struct simdisk *dev)
185 {
186 	int err = 0;
187 
188 	spin_lock(&dev->lock);
189 
190 	if (dev->users != 0) {
191 		err = -EBUSY;
192 	} else if (dev->fd != -1) {
193 		if (simc_close(dev->fd)) {
194 			pr_err("SIMDISK: error closing %s: %d\n",
195 					dev->filename, errno);
196 			err = -EIO;
197 		} else {
198 			pr_info("SIMDISK: %s detached from %s\n",
199 					dev->gd->disk_name, dev->filename);
200 			dev->fd = -1;
201 			kfree(dev->filename);
202 			dev->filename = NULL;
203 		}
204 	}
205 	spin_unlock(&dev->lock);
206 	return err;
207 }
208 
209 static ssize_t proc_read_simdisk(struct file *file, char __user *buf,
210 			size_t size, loff_t *ppos)
211 {
212 	struct simdisk *dev = PDE_DATA(file_inode(file));
213 	const char *s = dev->filename;
214 	if (s) {
215 		ssize_t n = simple_read_from_buffer(buf, size, ppos,
216 							s, strlen(s));
217 		if (n < 0)
218 			return n;
219 		buf += n;
220 		size -= n;
221 	}
222 	return simple_read_from_buffer(buf, size, ppos, "\n", 1);
223 }
224 
225 static ssize_t proc_write_simdisk(struct file *file, const char __user *buf,
226 			size_t count, loff_t *ppos)
227 {
228 	char *tmp = memdup_user_nul(buf, count);
229 	struct simdisk *dev = PDE_DATA(file_inode(file));
230 	int err;
231 
232 	if (IS_ERR(tmp))
233 		return PTR_ERR(tmp);
234 
235 	err = simdisk_detach(dev);
236 	if (err != 0)
237 		goto out_free;
238 
239 	if (count > 0 && tmp[count - 1] == '\n')
240 		tmp[count - 1] = 0;
241 
242 	if (tmp[0])
243 		err = simdisk_attach(dev, tmp);
244 
245 	if (err == 0)
246 		err = count;
247 out_free:
248 	kfree(tmp);
249 	return err;
250 }
251 
252 static const struct proc_ops simdisk_proc_ops = {
253 	.proc_read	= proc_read_simdisk,
254 	.proc_write	= proc_write_simdisk,
255 	.proc_lseek	= default_llseek,
256 };
257 
258 static int __init simdisk_setup(struct simdisk *dev, int which,
259 		struct proc_dir_entry *procdir)
260 {
261 	char tmp[2] = { '0' + which, 0 };
262 
263 	dev->fd = -1;
264 	dev->filename = NULL;
265 	spin_lock_init(&dev->lock);
266 	dev->users = 0;
267 
268 	dev->gd = blk_alloc_disk(NUMA_NO_NODE);
269 	if (!dev->gd)
270 		return -ENOMEM;
271 	dev->gd->major = simdisk_major;
272 	dev->gd->first_minor = which;
273 	dev->gd->minors = SIMDISK_MINORS;
274 	dev->gd->fops = &simdisk_ops;
275 	dev->gd->private_data = dev;
276 	snprintf(dev->gd->disk_name, 32, "simdisk%d", which);
277 	set_capacity(dev->gd, 0);
278 	add_disk(dev->gd);
279 
280 	dev->procfile = proc_create_data(tmp, 0644, procdir, &simdisk_proc_ops, dev);
281 	return 0;
282 }
283 
284 static int __init simdisk_init(void)
285 {
286 	int i;
287 
288 	if (register_blkdev(simdisk_major, "simdisk") < 0) {
289 		pr_err("SIMDISK: register_blkdev: %d\n", simdisk_major);
290 		return -EIO;
291 	}
292 	pr_info("SIMDISK: major: %d\n", simdisk_major);
293 
294 	if (n_files > simdisk_count)
295 		simdisk_count = n_files;
296 	if (simdisk_count > MAX_SIMDISK_COUNT)
297 		simdisk_count = MAX_SIMDISK_COUNT;
298 
299 	sddev = kmalloc_array(simdisk_count, sizeof(*sddev), GFP_KERNEL);
300 	if (sddev == NULL)
301 		goto out_unregister;
302 
303 	simdisk_procdir = proc_mkdir("simdisk", 0);
304 	if (simdisk_procdir == NULL)
305 		goto out_free_unregister;
306 
307 	for (i = 0; i < simdisk_count; ++i) {
308 		if (simdisk_setup(sddev + i, i, simdisk_procdir) == 0) {
309 			if (filename[i] != NULL && filename[i][0] != 0 &&
310 					(n_files == 0 || i < n_files))
311 				simdisk_attach(sddev + i, filename[i]);
312 		}
313 	}
314 
315 	return 0;
316 
317 out_free_unregister:
318 	kfree(sddev);
319 out_unregister:
320 	unregister_blkdev(simdisk_major, "simdisk");
321 	return -ENOMEM;
322 }
323 module_init(simdisk_init);
324 
325 static void simdisk_teardown(struct simdisk *dev, int which,
326 		struct proc_dir_entry *procdir)
327 {
328 	char tmp[2] = { '0' + which, 0 };
329 
330 	simdisk_detach(dev);
331 	if (dev->gd) {
332 		del_gendisk(dev->gd);
333 		blk_cleanup_disk(dev->gd);
334 	}
335 	remove_proc_entry(tmp, procdir);
336 }
337 
338 static void __exit simdisk_exit(void)
339 {
340 	int i;
341 
342 	for (i = 0; i < simdisk_count; ++i)
343 		simdisk_teardown(sddev + i, i, simdisk_procdir);
344 	remove_proc_entry("simdisk", 0);
345 	kfree(sddev);
346 	unregister_blkdev(simdisk_major, "simdisk");
347 }
348 module_exit(simdisk_exit);
349 
350 MODULE_ALIAS_BLOCKDEV_MAJOR(SIMDISK_MAJOR);
351 
352 MODULE_LICENSE("GPL");
353