2 * arch/xtensa/platforms/iss/simdisk.c
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
8 * Copyright (C) 2001-2013 Tensilica Inc.
9 * Authors Victor Prupis
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>
23 #define SIMDISK_MAJOR 240
24 #define SIMDISK_MINORS 1
25 #define MAX_SIMDISK_COUNT 10
30 struct request_queue
*queue
;
32 struct proc_dir_entry
*procfile
;
39 static int simdisk_count
= CONFIG_BLK_DEV_SIMDISK_COUNT
;
40 module_param(simdisk_count
, int, S_IRUGO
);
41 MODULE_PARM_DESC(simdisk_count
, "Number of simdisk units.");
44 static const char *filename
[MAX_SIMDISK_COUNT
] = {
45 #ifdef CONFIG_SIMDISK0_FILENAME
46 CONFIG_SIMDISK0_FILENAME
,
47 #ifdef CONFIG_SIMDISK1_FILENAME
48 CONFIG_SIMDISK1_FILENAME
,
53 static int simdisk_param_set_filename(const char *val
,
54 const struct kernel_param
*kp
)
56 if (n_files
< ARRAY_SIZE(filename
))
57 filename
[n_files
++] = val
;
63 static const struct kernel_param_ops simdisk_param_ops_filename
= {
64 .set
= simdisk_param_set_filename
,
66 module_param_cb(filename
, &simdisk_param_ops_filename
, &n_files
, 0);
67 MODULE_PARM_DESC(filename
, "Backing storage filename.");
69 static int simdisk_major
= SIMDISK_MAJOR
;
71 static void simdisk_transfer(struct simdisk
*dev
, unsigned long sector
,
72 unsigned long nsect
, char *buffer
, int write
)
74 unsigned long offset
= sector
<< SECTOR_SHIFT
;
75 unsigned long nbytes
= nsect
<< SECTOR_SHIFT
;
77 if (offset
> dev
->size
|| dev
->size
- offset
< nbytes
) {
78 pr_notice("Beyond-end %s (%ld %ld)\n",
79 write
? "write" : "read", offset
, nbytes
);
83 spin_lock(&dev
->lock
);
87 simc_lseek(dev
->fd
, offset
, SEEK_SET
);
90 io
= simc_write(dev
->fd
, buffer
, nbytes
);
92 io
= simc_read(dev
->fd
, buffer
, nbytes
);
94 pr_err("SIMDISK: IO error %d\n", errno
);
101 spin_unlock(&dev
->lock
);
104 static blk_qc_t
simdisk_submit_bio(struct bio
*bio
)
106 struct simdisk
*dev
= bio
->bi_disk
->private_data
;
108 struct bvec_iter iter
;
109 sector_t sector
= bio
->bi_iter
.bi_sector
;
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
;
115 simdisk_transfer(dev
, sector
, len
, buffer
,
116 bio_data_dir(bio
) == WRITE
);
118 kunmap_atomic(buffer
);
122 return BLK_QC_T_NONE
;
125 static int simdisk_open(struct block_device
*bdev
, fmode_t mode
)
127 struct simdisk
*dev
= bdev
->bd_disk
->private_data
;
129 spin_lock(&dev
->lock
);
131 spin_unlock(&dev
->lock
);
135 static void simdisk_release(struct gendisk
*disk
, fmode_t mode
)
137 struct simdisk
*dev
= disk
->private_data
;
138 spin_lock(&dev
->lock
);
140 spin_unlock(&dev
->lock
);
143 static const struct block_device_operations simdisk_ops
= {
144 .owner
= THIS_MODULE
,
145 .submit_bio
= simdisk_submit_bio
,
146 .open
= simdisk_open
,
147 .release
= simdisk_release
,
150 static struct simdisk
*sddev
;
151 static struct proc_dir_entry
*simdisk_procdir
;
153 static int simdisk_attach(struct simdisk
*dev
, const char *filename
)
157 filename
= kstrdup(filename
, GFP_KERNEL
);
158 if (filename
== NULL
)
161 spin_lock(&dev
->lock
);
167 dev
->fd
= simc_open(filename
, O_RDWR
, 0);
169 pr_err("SIMDISK: Can't open %s: %d\n", filename
, errno
);
173 dev
->size
= simc_lseek(dev
->fd
, 0, SEEK_END
);
174 set_capacity(dev
->gd
, dev
->size
>> SECTOR_SHIFT
);
175 dev
->filename
= filename
;
176 pr_info("SIMDISK: %s=%s\n", dev
->gd
->disk_name
, dev
->filename
);
180 spin_unlock(&dev
->lock
);
185 static int simdisk_detach(struct simdisk
*dev
)
189 spin_lock(&dev
->lock
);
191 if (dev
->users
!= 0) {
193 } else if (dev
->fd
!= -1) {
194 if (simc_close(dev
->fd
)) {
195 pr_err("SIMDISK: error closing %s: %d\n",
196 dev
->filename
, errno
);
199 pr_info("SIMDISK: %s detached from %s\n",
200 dev
->gd
->disk_name
, dev
->filename
);
202 kfree(dev
->filename
);
203 dev
->filename
= NULL
;
206 spin_unlock(&dev
->lock
);
210 static ssize_t
proc_read_simdisk(struct file
*file
, char __user
*buf
,
211 size_t size
, loff_t
*ppos
)
213 struct simdisk
*dev
= PDE_DATA(file_inode(file
));
214 const char *s
= dev
->filename
;
216 ssize_t n
= simple_read_from_buffer(buf
, size
, ppos
,
223 return simple_read_from_buffer(buf
, size
, ppos
, "\n", 1);
226 static ssize_t
proc_write_simdisk(struct file
*file
, const char __user
*buf
,
227 size_t count
, loff_t
*ppos
)
229 char *tmp
= memdup_user_nul(buf
, count
);
230 struct simdisk
*dev
= PDE_DATA(file_inode(file
));
236 err
= simdisk_detach(dev
);
240 if (count
> 0 && tmp
[count
- 1] == '\n')
244 err
= simdisk_attach(dev
, tmp
);
253 static const struct proc_ops simdisk_proc_ops
= {
254 .proc_read
= proc_read_simdisk
,
255 .proc_write
= proc_write_simdisk
,
256 .proc_lseek
= default_llseek
,
259 static int __init
simdisk_setup(struct simdisk
*dev
, int which
,
260 struct proc_dir_entry
*procdir
)
262 char tmp
[2] = { '0' + which
, 0 };
265 dev
->filename
= NULL
;
266 spin_lock_init(&dev
->lock
);
269 dev
->queue
= blk_alloc_queue(NUMA_NO_NODE
);
270 if (dev
->queue
== NULL
) {
271 pr_err("blk_alloc_queue failed\n");
272 goto out_alloc_queue
;
275 dev
->gd
= alloc_disk(SIMDISK_MINORS
);
276 if (dev
->gd
== NULL
) {
277 pr_err("alloc_disk failed\n");
280 dev
->gd
->major
= simdisk_major
;
281 dev
->gd
->first_minor
= which
;
282 dev
->gd
->fops
= &simdisk_ops
;
283 dev
->gd
->queue
= dev
->queue
;
284 dev
->gd
->private_data
= dev
;
285 snprintf(dev
->gd
->disk_name
, 32, "simdisk%d", which
);
286 set_capacity(dev
->gd
, 0);
289 dev
->procfile
= proc_create_data(tmp
, 0644, procdir
, &simdisk_proc_ops
, dev
);
293 blk_cleanup_queue(dev
->queue
);
299 static int __init
simdisk_init(void)
303 if (register_blkdev(simdisk_major
, "simdisk") < 0) {
304 pr_err("SIMDISK: register_blkdev: %d\n", simdisk_major
);
307 pr_info("SIMDISK: major: %d\n", simdisk_major
);
309 if (n_files
> simdisk_count
)
310 simdisk_count
= n_files
;
311 if (simdisk_count
> MAX_SIMDISK_COUNT
)
312 simdisk_count
= MAX_SIMDISK_COUNT
;
314 sddev
= kmalloc_array(simdisk_count
, sizeof(*sddev
), GFP_KERNEL
);
318 simdisk_procdir
= proc_mkdir("simdisk", 0);
319 if (simdisk_procdir
== NULL
)
320 goto out_free_unregister
;
322 for (i
= 0; i
< simdisk_count
; ++i
) {
323 if (simdisk_setup(sddev
+ i
, i
, simdisk_procdir
) == 0) {
324 if (filename
[i
] != NULL
&& filename
[i
][0] != 0 &&
325 (n_files
== 0 || i
< n_files
))
326 simdisk_attach(sddev
+ i
, filename
[i
]);
335 unregister_blkdev(simdisk_major
, "simdisk");
338 module_init(simdisk_init
);
340 static void simdisk_teardown(struct simdisk
*dev
, int which
,
341 struct proc_dir_entry
*procdir
)
343 char tmp
[2] = { '0' + which
, 0 };
347 del_gendisk(dev
->gd
);
349 blk_cleanup_queue(dev
->queue
);
350 remove_proc_entry(tmp
, procdir
);
353 static void __exit
simdisk_exit(void)
357 for (i
= 0; i
< simdisk_count
; ++i
)
358 simdisk_teardown(sddev
+ i
, i
, simdisk_procdir
);
359 remove_proc_entry("simdisk", 0);
361 unregister_blkdev(simdisk_major
, "simdisk");
363 module_exit(simdisk_exit
);
365 MODULE_ALIAS_BLOCKDEV_MAJOR(SIMDISK_MAJOR
);
367 MODULE_LICENSE("GPL");