timers: add missing kernel-doc
[linux/fpc-iii.git] / drivers / s390 / block / xpram.c
blob76814f3e898a9c444dca70b4ddfc0cd7d885847c
1 /*
2 * Xpram.c -- the S/390 expanded memory RAM-disk
3 *
4 * significant parts of this code are based on
5 * the sbull device driver presented in
6 * A. Rubini: Linux Device Drivers
8 * Author of XPRAM specific coding: Reinhard Buendgen
9 * buendgen@de.ibm.com
10 * Rewrite for 2.5: Martin Schwidefsky <schwidefsky@de.ibm.com>
12 * External interfaces:
13 * Interfaces to linux kernel
14 * xpram_setup: read kernel parameters
15 * Device specific file operations
16 * xpram_iotcl
17 * xpram_open
19 * "ad-hoc" partitioning:
20 * the expanded memory can be partitioned among several devices
21 * (with different minors). The partitioning set up can be
22 * set by kernel or module parameters (int devs & int sizes[])
24 * Potential future improvements:
25 * generic hard disk support to replace ad-hoc partitioning
28 #define KMSG_COMPONENT "xpram"
29 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/ctype.h> /* isdigit, isxdigit */
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/blkdev.h>
38 #include <linux/blkpg.h>
39 #include <linux/hdreg.h> /* HDIO_GETGEO */
40 #include <linux/sysdev.h>
41 #include <linux/bio.h>
42 #include <asm/uaccess.h>
44 #define XPRAM_NAME "xpram"
45 #define XPRAM_DEVS 1 /* one partition */
46 #define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
48 typedef struct {
49 unsigned int size; /* size of xpram segment in pages */
50 unsigned int offset; /* start page of xpram segment */
51 } xpram_device_t;
53 static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
54 static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
55 static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
56 static struct request_queue *xpram_queues[XPRAM_MAX_DEVS];
57 static unsigned int xpram_pages;
58 static int xpram_devs;
61 * Parameter parsing functions.
63 static int __initdata devs = XPRAM_DEVS;
64 static char __initdata *sizes[XPRAM_MAX_DEVS];
66 module_param(devs, int, 0);
67 module_param_array(sizes, charp, NULL, 0);
69 MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
70 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
71 MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \
72 "the defaults are 0s \n" \
73 "All devices with size 0 equally partition the "
74 "remaining space on the expanded strorage not "
75 "claimed by explicit sizes\n");
76 MODULE_LICENSE("GPL");
79 * Copy expanded memory page (4kB) into main memory
80 * Arguments
81 * page_addr: address of target page
82 * xpage_index: index of expandeded memory page
83 * Return value
84 * 0: if operation succeeds
85 * -EIO: if pgin failed
86 * -ENXIO: if xpram has vanished
88 static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
90 int cc = 2; /* return unused cc 2 if pgin traps */
92 asm volatile(
93 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
94 "0: ipm %0\n"
95 " srl %0,28\n"
96 "1:\n"
97 EX_TABLE(0b,1b)
98 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
99 if (cc == 3)
100 return -ENXIO;
101 if (cc == 2)
102 return -ENXIO;
103 if (cc == 1)
104 return -EIO;
105 return 0;
109 * Copy a 4kB page of main memory to an expanded memory page
110 * Arguments
111 * page_addr: address of source page
112 * xpage_index: index of expandeded memory page
113 * Return value
114 * 0: if operation succeeds
115 * -EIO: if pgout failed
116 * -ENXIO: if xpram has vanished
118 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
120 int cc = 2; /* return unused cc 2 if pgin traps */
122 asm volatile(
123 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
124 "0: ipm %0\n"
125 " srl %0,28\n"
126 "1:\n"
127 EX_TABLE(0b,1b)
128 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
129 if (cc == 3)
130 return -ENXIO;
131 if (cc == 2)
132 return -ENXIO;
133 if (cc == 1)
134 return -EIO;
135 return 0;
139 * Check if xpram is available.
141 static int __init xpram_present(void)
143 unsigned long mem_page;
144 int rc;
146 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
147 if (!mem_page)
148 return -ENOMEM;
149 rc = xpram_page_in(mem_page, 0);
150 free_page(mem_page);
151 return rc ? -ENXIO : 0;
155 * Return index of the last available xpram page.
157 static unsigned long __init xpram_highest_page_index(void)
159 unsigned int page_index, add_bit;
160 unsigned long mem_page;
162 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
163 if (!mem_page)
164 return 0;
166 page_index = 0;
167 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
168 while (add_bit > 0) {
169 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
170 page_index |= add_bit;
171 add_bit >>= 1;
174 free_page (mem_page);
176 return page_index;
180 * Block device make request function.
182 static int xpram_make_request(struct request_queue *q, struct bio *bio)
184 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
185 struct bio_vec *bvec;
186 unsigned int index;
187 unsigned long page_addr;
188 unsigned long bytes;
189 int i;
191 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
192 /* Request is not page-aligned. */
193 goto fail;
194 if ((bio->bi_size >> 12) > xdev->size)
195 /* Request size is no page-aligned. */
196 goto fail;
197 if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
198 goto fail;
199 index = (bio->bi_sector >> 3) + xdev->offset;
200 bio_for_each_segment(bvec, bio, i) {
201 page_addr = (unsigned long)
202 kmap(bvec->bv_page) + bvec->bv_offset;
203 bytes = bvec->bv_len;
204 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
205 /* More paranoia. */
206 goto fail;
207 while (bytes > 0) {
208 if (bio_data_dir(bio) == READ) {
209 if (xpram_page_in(page_addr, index) != 0)
210 goto fail;
211 } else {
212 if (xpram_page_out(page_addr, index) != 0)
213 goto fail;
215 page_addr += 4096;
216 bytes -= 4096;
217 index++;
220 set_bit(BIO_UPTODATE, &bio->bi_flags);
221 bio_endio(bio, 0);
222 return 0;
223 fail:
224 bio_io_error(bio);
225 return 0;
228 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
230 unsigned long size;
233 * get geometry: we have to fake one... trim the size to a
234 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
235 * whatever cylinders. Tell also that data starts at sector. 4.
237 size = (xpram_pages * 8) & ~0x3f;
238 geo->cylinders = size >> 6;
239 geo->heads = 4;
240 geo->sectors = 16;
241 geo->start = 4;
242 return 0;
245 static struct block_device_operations xpram_devops =
247 .owner = THIS_MODULE,
248 .getgeo = xpram_getgeo,
252 * Setup xpram_sizes array.
254 static int __init xpram_setup_sizes(unsigned long pages)
256 unsigned long mem_needed;
257 unsigned long mem_auto;
258 unsigned long long size;
259 int mem_auto_no;
260 int i;
262 /* Check number of devices. */
263 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
264 pr_err("%d is not a valid number of XPRAM devices\n",devs);
265 return -EINVAL;
267 xpram_devs = devs;
270 * Copy sizes array to xpram_sizes and align partition
271 * sizes to page boundary.
273 mem_needed = 0;
274 mem_auto_no = 0;
275 for (i = 0; i < xpram_devs; i++) {
276 if (sizes[i]) {
277 size = simple_strtoull(sizes[i], &sizes[i], 0);
278 switch (sizes[i][0]) {
279 case 'g':
280 case 'G':
281 size <<= 20;
282 break;
283 case 'm':
284 case 'M':
285 size <<= 10;
287 xpram_sizes[i] = (size + 3) & -4UL;
289 if (xpram_sizes[i])
290 mem_needed += xpram_sizes[i];
291 else
292 mem_auto_no++;
295 pr_info(" number of devices (partitions): %d \n", xpram_devs);
296 for (i = 0; i < xpram_devs; i++) {
297 if (xpram_sizes[i])
298 pr_info(" size of partition %d: %u kB\n",
299 i, xpram_sizes[i]);
300 else
301 pr_info(" size of partition %d to be set "
302 "automatically\n",i);
304 pr_info(" memory needed (for sized partitions): %lu kB\n",
305 mem_needed);
306 pr_info(" partitions to be sized automatically: %d\n",
307 mem_auto_no);
309 if (mem_needed > pages * 4) {
310 pr_err("Not enough expanded memory available\n");
311 return -EINVAL;
315 * partitioning:
316 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
317 * else: ; all partitions with zero xpram_sizes[i]
318 * partition equally the remaining space
320 if (mem_auto_no) {
321 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
322 pr_info(" automatically determined "
323 "partition size: %lu kB\n", mem_auto);
324 for (i = 0; i < xpram_devs; i++)
325 if (xpram_sizes[i] == 0)
326 xpram_sizes[i] = mem_auto;
328 return 0;
331 static int __init xpram_setup_blkdev(void)
333 unsigned long offset;
334 int i, rc = -ENOMEM;
336 for (i = 0; i < xpram_devs; i++) {
337 xpram_disks[i] = alloc_disk(1);
338 if (!xpram_disks[i])
339 goto out;
340 xpram_queues[i] = blk_alloc_queue(GFP_KERNEL);
341 if (!xpram_queues[i]) {
342 put_disk(xpram_disks[i]);
343 goto out;
345 blk_queue_make_request(xpram_queues[i], xpram_make_request);
346 blk_queue_hardsect_size(xpram_queues[i], 4096);
350 * Register xpram major.
352 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
353 if (rc < 0)
354 goto out;
357 * Setup device structures.
359 offset = 0;
360 for (i = 0; i < xpram_devs; i++) {
361 struct gendisk *disk = xpram_disks[i];
363 xpram_devices[i].size = xpram_sizes[i] / 4;
364 xpram_devices[i].offset = offset;
365 offset += xpram_devices[i].size;
366 disk->major = XPRAM_MAJOR;
367 disk->first_minor = i;
368 disk->fops = &xpram_devops;
369 disk->private_data = &xpram_devices[i];
370 disk->queue = xpram_queues[i];
371 sprintf(disk->disk_name, "slram%d", i);
372 set_capacity(disk, xpram_sizes[i] << 1);
373 add_disk(disk);
376 return 0;
377 out:
378 while (i--) {
379 blk_cleanup_queue(xpram_queues[i]);
380 put_disk(xpram_disks[i]);
382 return rc;
386 * Finally, the init/exit functions.
388 static void __exit xpram_exit(void)
390 int i;
391 for (i = 0; i < xpram_devs; i++) {
392 del_gendisk(xpram_disks[i]);
393 blk_cleanup_queue(xpram_queues[i]);
394 put_disk(xpram_disks[i]);
396 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
399 static int __init xpram_init(void)
401 int rc;
403 /* Find out size of expanded memory. */
404 if (xpram_present() != 0) {
405 pr_err("No expanded memory available\n");
406 return -ENODEV;
408 xpram_pages = xpram_highest_page_index() + 1;
409 pr_info(" %u pages expanded memory found (%lu KB).\n",
410 xpram_pages, (unsigned long) xpram_pages*4);
411 rc = xpram_setup_sizes(xpram_pages);
412 if (rc)
413 return rc;
414 return xpram_setup_blkdev();
417 module_init(xpram_init);
418 module_exit(xpram_exit);