summaryrefslogtreecommitdiff
path: root/arch/xtensa/platforms/iss/simdisk.c
blob: f58ffc3b68a855efe6929fe566753c2e07453d0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * arch/xtensa/platforms/iss/simdisk.c
 *
 * 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.
 *
 * Copyright (C) 2001-2013 Tensilica Inc.
 *   Authors	Victor Prupis
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#include <platform/simcall.h>

#define SIMDISK_MAJOR 240
#define SECTOR_SHIFT 9
#define SIMDISK_MINORS 1
#define MAX_SIMDISK_COUNT 10

struct simdisk {
	const char *filename;
	spinlock_t lock;
	struct request_queue *queue;
	struct gendisk *gd;
	struct proc_dir_entry *procfile;
	int users;
	unsigned long size;
	int fd;
};


static int simdisk_count = CONFIG_BLK_DEV_SIMDISK_COUNT;
module_param(simdisk_count, int, S_IRUGO);
MODULE_PARM_DESC(simdisk_count, "Number of simdisk units.");

static int n_files;
static const char *filename[MAX_SIMDISK_COUNT] = {
#ifdef CONFIG_SIMDISK0_FILENAME
	CONFIG_SIMDISK0_FILENAME,
#ifdef CONFIG_SIMDISK1_FILENAME
	CONFIG_SIMDISK1_FILENAME,
#endif
#endif
};

static int simdisk_param_set_filename(const char *val,
		const struct kernel_param *kp)
{
	if (n_files < ARRAY_SIZE(filename))
		filename[n_files++] = val;
	else
		return -EINVAL;
	return 0;
}

static const struct kernel_param_ops simdisk_param_ops_filename = {
	.set = simdisk_param_set_filename,
};
module_param_cb(filename, &simdisk_param_ops_filename, &n_files, 0);
MODULE_PARM_DESC(filename, "Backing storage filename.");

static int simdisk_major = SIMDISK_MAJOR;

static void simdisk_transfer(struct simdisk *dev, unsigned long sector,
		unsigned long nsect, char *buffer, int write)
{
	unsigned long offset = sector << SECTOR_SHIFT;
	unsigned long nbytes = nsect << SECTOR_SHIFT;

	if (offset > dev->size || dev->size - offset < nbytes) {
		pr_notice("Beyond-end %s (%ld %ld)\n",
				write ? "write" : "read", offset, nbytes);
		return;
	}

	spin_lock(&dev->lock);
	while (nbytes > 0) {
		unsigned long io;

		__simc(SYS_lseek, dev->fd, offset, SEEK_SET, 0, 0);
		if (write)
			io = simc_write(dev->fd, buffer, nbytes);
		else
			io = simc_read(dev->fd, buffer, nbytes);
		if (io == -1) {
			pr_err("SIMDISK: IO error %d\n", errno);
			break;
		}
		buffer += io;
		offset += io;
		nbytes -= io;
	}
	spin_unlock(&dev->lock);
}

static int simdisk_xfer_bio(struct simdisk *dev, struct bio *bio)
{
	int i;
	struct bio_vec *bvec;
	sector_t sector = bio->bi_sector;

	bio_for_each_segment(bvec, bio, i) {
		char *buffer = __bio_kmap_atomic(bio, i, KM_USER0);
		unsigned len = bvec->bv_len >> SECTOR_SHIFT;

		simdisk_transfer(dev, sector, len, buffer,
				bio_data_dir(bio) == WRITE);
		sector += len;
		__bio_kunmap_atomic(bio, KM_USER0);
	}
	return 0;
}

static void simdisk_make_request(struct request_queue *q, struct bio *bio)
{
	struct simdisk *dev = q->queuedata;
	int status = simdisk_xfer_bio(dev, bio);
	bio_endio(bio, status);
}


static int simdisk_open(struct block_device *bdev, fmode_t mode)
{
	struct simdisk *dev = bdev->bd_disk->private_data;

	spin_lock(&dev->lock);
	if (!dev->users)
		check_disk_change(bdev);
	++dev->users;
	spin_unlock(&dev->lock);
	return 0;
}

static int simdisk_release(struct gendisk *disk, fmode_t mode)
{
	struct simdisk *dev = disk->private_data;
	spin_lock(&dev->lock);
	--dev->users;
	spin_unlock(&dev->lock);
	return 0;
}

static const struct block_device_operations simdisk_ops = {
	.owner		= THIS_MODULE,
	.open		= simdisk_open,
	.release	= simdisk_release,
};

static struct simdisk *sddev;
static struct proc_dir_entry *simdisk_procdir;

static int simdisk_attach(struct simdisk *dev, const char *filename)
{
	int err = 0;

	filename = kstrdup(filename, GFP_KERNEL);
	if (filename == NULL)
		return -ENOMEM;

	spin_lock(&dev->lock);

	if (dev->fd != -1) {
		err = -EBUSY;
		goto out;
	}
	dev->fd = simc_open(filename, O_RDWR, 0);
	if (dev->fd == -1) {
		pr_err("SIMDISK: Can't open %s: %d\n", filename, errno);
		err = -ENODEV;
		goto out;
	}
	dev->size = __simc(SYS_lseek, dev->fd, 0, SEEK_END, 0, 0);
	set_capacity(dev->gd, dev->size >> SECTOR_SHIFT);
	dev->filename = filename;
	pr_info("SIMDISK: %s=%s\n", dev->gd->disk_name, dev->filename);
out:
	if (err)
		kfree(filename);
	spin_unlock(&dev->lock);

	return err;
}

static int simdisk_detach(struct simdisk *dev)
{
	int err = 0;

	spin_lock(&dev->lock);

	if (dev->users != 0) {
		err = -EBUSY;
	} else if (dev->fd != -1) {
		if (simc_close(dev->fd)) {
			pr_err("SIMDISK: error closing %s: %d\n",
					dev->filename, errno);
			err = -EIO;
		} else {
			pr_info("SIMDISK: %s detached from %s\n",
					dev->gd->disk_name, dev->filename);
			dev->fd = -1;
			kfree(dev->filename);
			dev->filename = NULL;
		}
	}
	spin_unlock(&dev->lock);
	return err;
}

static int proc_read_simdisk(char *page, char **start, off_t off,
		int count, int *eof, void *data)
{
	int len;
	struct simdisk *dev = (struct simdisk *) data;
	len = sprintf(page, "%s\n", dev->filename ? dev->filename : "");
	return len;
}

static int proc_write_simdisk(struct file *file, const char *buffer,
		unsigned long count, void *data)
{
	char *tmp = kmalloc(count + 1, GFP_KERNEL);
	struct simdisk *dev = (struct simdisk *) data;
	int err;

	if (tmp == NULL)
		return -ENOMEM;
	if (copy_from_user(tmp, buffer, count)) {
		err = -EFAULT;
		goto out_free;
	}

	err = simdisk_detach(dev);
	if (err != 0)
		goto out_free;

	if (count > 0 && tmp[count - 1] == '\n')
		tmp[count - 1] = 0;
	else
		tmp[count] = 0;

	if (tmp[0])
		err = simdisk_attach(dev, tmp);

	if (err == 0)
		err = count;
out_free:
	kfree(tmp);
	return err;
}

static int __init simdisk_setup(struct simdisk *dev, int which,
		struct proc_dir_entry *procdir)
{
	char tmp[2] = { '0' + which, 0 };

	dev->fd = -1;
	dev->filename = NULL;
	spin_lock_init(&dev->lock);
	dev->users = 0;

	dev->queue = blk_alloc_queue(GFP_KERNEL);
	if (dev->queue == NULL) {
		pr_err("blk_alloc_queue failed\n");
		goto out_alloc_queue;
	}

	blk_queue_make_request(dev->queue, simdisk_make_request);
	dev->queue->queuedata = dev;

	dev->gd = alloc_disk(SIMDISK_MINORS);
	if (dev->gd == NULL) {
		pr_err("alloc_disk failed\n");
		goto out_alloc_disk;
	}
	dev->gd->major = simdisk_major;
	dev->gd->first_minor = which;
	dev->gd->fops = &simdisk_ops;
	dev->gd->queue = dev->queue;
	dev->gd->private_data = dev;
	snprintf(dev->gd->disk_name, 32, "simdisk%d", which);
	set_capacity(dev->gd, 0);
	add_disk(dev->gd);

	dev->procfile = create_proc_entry(tmp, 0644, procdir);
	dev->procfile->data = dev;
	dev->procfile->read_proc = proc_read_simdisk;
	dev->procfile->write_proc = proc_write_simdisk;
	return 0;

out_alloc_disk:
	blk_cleanup_queue(dev->queue);
	dev->queue = NULL;
out_alloc_queue:
	simc_close(dev->fd);
	return -EIO;
}

static int __init simdisk_init(void)
{
	int i;

	if (register_blkdev(simdisk_major, "simdisk") < 0) {
		pr_err("SIMDISK: register_blkdev: %d\n", simdisk_major);
		return -EIO;
	}
	pr_info("SIMDISK: major: %d\n", simdisk_major);

	if (n_files > simdisk_count)
		simdisk_count = n_files;
	if (simdisk_count > MAX_SIMDISK_COUNT)
		simdisk_count = MAX_SIMDISK_COUNT;

	sddev = kmalloc(simdisk_count * sizeof(struct simdisk),
			GFP_KERNEL);
	if (sddev == NULL)
		goto out_unregister;

	simdisk_procdir = proc_mkdir("simdisk", 0);
	if (simdisk_procdir == NULL)
		goto out_free_unregister;

	for (i = 0; i < simdisk_count; ++i) {
		if (simdisk_setup(sddev + i, i, simdisk_procdir) == 0) {
			if (filename[i] != NULL && filename[i][0] != 0 &&
					(n_files == 0 || i < n_files))
				simdisk_attach(sddev + i, filename[i]);
		}
	}

	return 0;

out_free_unregister:
	kfree(sddev);
out_unregister:
	unregister_blkdev(simdisk_major, "simdisk");
	return -ENOMEM;
}
module_init(simdisk_init);

static void simdisk_teardown(struct simdisk *dev, int which,
		struct proc_dir_entry *procdir)
{
	char tmp[2] = { '0' + which, 0 };

	simdisk_detach(dev);
	if (dev->gd)
		del_gendisk(dev->gd);
	if (dev->queue)
		blk_cleanup_queue(dev->queue);
	remove_proc_entry(tmp, procdir);
}

static void __exit simdisk_exit(void)
{
	int i;

	for (i = 0; i < simdisk_count; ++i)
		simdisk_teardown(sddev + i, i, simdisk_procdir);
	remove_proc_entry("simdisk", 0);
	kfree(sddev);
	unregister_blkdev(simdisk_major, "simdisk");
}
module_exit(simdisk_exit);

MODULE_ALIAS_BLOCKDEV_MAJOR(SIMDISK_MAJOR);

MODULE_LICENSE("GPL");