[PATCH] devfs: Remove devfs_remove() function from the kernel tree
[linux/fpc-iii.git] / drivers / s390 / block / xpram.c
blob15aad01bba07b868959b71cbfd782265b6420106
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 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/ctype.h> /* isdigit, isxdigit */
31 #include <linux/errno.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/blkdev.h>
35 #include <linux/blkpg.h>
36 #include <linux/hdreg.h> /* HDIO_GETGEO */
37 #include <linux/sysdev.h>
38 #include <linux/bio.h>
39 #include <linux/devfs_fs_kernel.h>
40 #include <asm/uaccess.h>
42 #define XPRAM_NAME "xpram"
43 #define XPRAM_DEVS 1 /* one partition */
44 #define XPRAM_MAX_DEVS 32 /* maximal number of devices (partitions) */
46 #define PRINT_DEBUG(x...) printk(KERN_DEBUG XPRAM_NAME " debug:" x)
47 #define PRINT_INFO(x...) printk(KERN_INFO XPRAM_NAME " info:" x)
48 #define PRINT_WARN(x...) printk(KERN_WARNING XPRAM_NAME " warning:" x)
49 #define PRINT_ERR(x...) printk(KERN_ERR XPRAM_NAME " error:" x)
52 static struct sysdev_class xpram_sysclass = {
53 set_kset_name("xpram"),
56 static struct sys_device xpram_sys_device = {
57 .id = 0,
58 .cls = &xpram_sysclass,
59 };
61 typedef struct {
62 unsigned int size; /* size of xpram segment in pages */
63 unsigned int offset; /* start page of xpram segment */
64 } xpram_device_t;
66 static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
67 static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
68 static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
69 static unsigned int xpram_pages;
70 static int xpram_devs;
73 * Parameter parsing functions.
75 static int devs = XPRAM_DEVS;
76 static unsigned int sizes[XPRAM_MAX_DEVS];
78 module_param(devs, int, 0);
79 module_param_array(sizes, int, NULL, 0);
81 MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
82 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
83 MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \
84 "the defaults are 0s \n" \
85 "All devices with size 0 equally partition the "
86 "remaining space on the expanded strorage not "
87 "claimed by explicit sizes\n");
88 MODULE_LICENSE("GPL");
90 #ifndef MODULE
92 * Parses the kernel parameters given in the kernel parameter line.
93 * The expected format is
94 * <number_of_partitions>[","<partition_size>]*
95 * where
96 * devices is a positive integer that initializes xpram_devs
97 * each size is a non-negative integer possibly followed by a
98 * magnitude (k,K,m,M,g,G), the list of sizes initialises
99 * xpram_sizes
101 * Arguments
102 * str: substring of kernel parameter line that contains xprams
103 * kernel parameters.
105 * Result 0 on success, -EINVAL else -- only for Version > 2.3
107 * Side effects
108 * the global variabls devs is set to the value of
109 * <number_of_partitions> and sizes[i] is set to the i-th
110 * partition size (if provided). A parsing error of a value
111 * results in this value being set to -EINVAL.
113 static int __init xpram_setup (char *str)
115 char *cp;
116 int i;
118 devs = simple_strtoul(str, &cp, 10);
119 if (cp <= str || devs > XPRAM_MAX_DEVS)
120 return 0;
121 for (i = 0; (i < devs) && (*cp++ == ','); i++) {
122 sizes[i] = simple_strtoul(cp, &cp, 10);
123 if (*cp == 'g' || *cp == 'G') {
124 sizes[i] <<= 20;
125 cp++;
126 } else if (*cp == 'm' || *cp == 'M') {
127 sizes[i] <<= 10;
128 cp++;
129 } else if (*cp == 'k' || *cp == 'K')
130 cp++;
131 while (isspace(*cp)) cp++;
133 if (*cp == ',' && i >= devs)
134 PRINT_WARN("partition sizes list has too many entries.\n");
135 else if (*cp != 0)
136 PRINT_WARN("ignored '%s' at end of parameter string.\n", cp);
137 return 1;
140 __setup("xpram_parts=", xpram_setup);
141 #endif
144 * Copy expanded memory page (4kB) into main memory
145 * Arguments
146 * page_addr: address of target page
147 * xpage_index: index of expandeded memory page
148 * Return value
149 * 0: if operation succeeds
150 * -EIO: if pgin failed
151 * -ENXIO: if xpram has vanished
153 static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
155 int cc;
157 __asm__ __volatile__ (
158 " lhi %0,2\n" /* return unused cc 2 if pgin traps */
159 " .insn rre,0xb22e0000,%1,%2\n" /* pgin %1,%2 */
160 "0: ipm %0\n"
161 " srl %0,28\n"
162 "1:\n"
163 #ifndef CONFIG_64BIT
164 ".section __ex_table,\"a\"\n"
165 " .align 4\n"
166 " .long 0b,1b\n"
167 ".previous"
168 #else
169 ".section __ex_table,\"a\"\n"
170 " .align 8\n"
171 " .quad 0b,1b\n"
172 ".previous"
173 #endif
174 : "=&d" (cc)
175 : "a" (__pa(page_addr)), "a" (xpage_index)
176 : "cc" );
177 if (cc == 3)
178 return -ENXIO;
179 if (cc == 2) {
180 PRINT_ERR("expanded storage lost!\n");
181 return -ENXIO;
183 if (cc == 1) {
184 PRINT_ERR("page in failed for page index %u.\n",
185 xpage_index);
186 return -EIO;
188 return 0;
192 * Copy a 4kB page of main memory to an expanded memory page
193 * Arguments
194 * page_addr: address of source page
195 * xpage_index: index of expandeded memory page
196 * Return value
197 * 0: if operation succeeds
198 * -EIO: if pgout failed
199 * -ENXIO: if xpram has vanished
201 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
203 int cc;
205 __asm__ __volatile__ (
206 " lhi %0,2\n" /* return unused cc 2 if pgout traps */
207 " .insn rre,0xb22f0000,%1,%2\n" /* pgout %1,%2 */
208 "0: ipm %0\n"
209 " srl %0,28\n"
210 "1:\n"
211 #ifndef CONFIG_64BIT
212 ".section __ex_table,\"a\"\n"
213 " .align 4\n"
214 " .long 0b,1b\n"
215 ".previous"
216 #else
217 ".section __ex_table,\"a\"\n"
218 " .align 8\n"
219 " .quad 0b,1b\n"
220 ".previous"
221 #endif
222 : "=&d" (cc)
223 : "a" (__pa(page_addr)), "a" (xpage_index)
224 : "cc" );
225 if (cc == 3)
226 return -ENXIO;
227 if (cc == 2) {
228 PRINT_ERR("expanded storage lost!\n");
229 return -ENXIO;
231 if (cc == 1) {
232 PRINT_ERR("page out failed for page index %u.\n",
233 xpage_index);
234 return -EIO;
236 return 0;
240 * Check if xpram is available.
242 static int __init xpram_present(void)
244 unsigned long mem_page;
245 int rc;
247 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
248 if (!mem_page)
249 return -ENOMEM;
250 rc = xpram_page_in(mem_page, 0);
251 free_page(mem_page);
252 return rc ? -ENXIO : 0;
256 * Return index of the last available xpram page.
258 static unsigned long __init xpram_highest_page_index(void)
260 unsigned int page_index, add_bit;
261 unsigned long mem_page;
263 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
264 if (!mem_page)
265 return 0;
267 page_index = 0;
268 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
269 while (add_bit > 0) {
270 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
271 page_index |= add_bit;
272 add_bit >>= 1;
275 free_page (mem_page);
277 return page_index;
281 * Block device make request function.
283 static int xpram_make_request(request_queue_t *q, struct bio *bio)
285 xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
286 struct bio_vec *bvec;
287 unsigned int index;
288 unsigned long page_addr;
289 unsigned long bytes;
290 int i;
292 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
293 /* Request is not page-aligned. */
294 goto fail;
295 if ((bio->bi_size >> 12) > xdev->size)
296 /* Request size is no page-aligned. */
297 goto fail;
298 if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
299 goto fail;
300 index = (bio->bi_sector >> 3) + xdev->offset;
301 bio_for_each_segment(bvec, bio, i) {
302 page_addr = (unsigned long)
303 kmap(bvec->bv_page) + bvec->bv_offset;
304 bytes = bvec->bv_len;
305 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
306 /* More paranoia. */
307 goto fail;
308 while (bytes > 0) {
309 if (bio_data_dir(bio) == READ) {
310 if (xpram_page_in(page_addr, index) != 0)
311 goto fail;
312 } else {
313 if (xpram_page_out(page_addr, index) != 0)
314 goto fail;
316 page_addr += 4096;
317 bytes -= 4096;
318 index++;
321 set_bit(BIO_UPTODATE, &bio->bi_flags);
322 bytes = bio->bi_size;
323 bio->bi_size = 0;
324 bio->bi_end_io(bio, bytes, 0);
325 return 0;
326 fail:
327 bio_io_error(bio, bio->bi_size);
328 return 0;
331 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
333 unsigned long size;
336 * get geometry: we have to fake one... trim the size to a
337 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
338 * whatever cylinders. Tell also that data starts at sector. 4.
340 size = (xpram_pages * 8) & ~0x3f;
341 geo->cylinders = size >> 6;
342 geo->heads = 4;
343 geo->sectors = 16;
344 geo->start = 4;
345 return 0;
348 static struct block_device_operations xpram_devops =
350 .owner = THIS_MODULE,
351 .getgeo = xpram_getgeo,
355 * Setup xpram_sizes array.
357 static int __init xpram_setup_sizes(unsigned long pages)
359 unsigned long mem_needed;
360 unsigned long mem_auto;
361 int mem_auto_no;
362 int i;
364 /* Check number of devices. */
365 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
366 PRINT_ERR("invalid number %d of devices\n",devs);
367 return -EINVAL;
369 xpram_devs = devs;
372 * Copy sizes array to xpram_sizes and align partition
373 * sizes to page boundary.
375 mem_needed = 0;
376 mem_auto_no = 0;
377 for (i = 0; i < xpram_devs; i++) {
378 xpram_sizes[i] = (sizes[i] + 3) & -4UL;
379 if (xpram_sizes[i])
380 mem_needed += xpram_sizes[i];
381 else
382 mem_auto_no++;
385 PRINT_INFO(" number of devices (partitions): %d \n", xpram_devs);
386 for (i = 0; i < xpram_devs; i++) {
387 if (xpram_sizes[i])
388 PRINT_INFO(" size of partition %d: %u kB\n",
389 i, xpram_sizes[i]);
390 else
391 PRINT_INFO(" size of partition %d to be set "
392 "automatically\n",i);
394 PRINT_DEBUG(" memory needed (for sized partitions): %lu kB\n",
395 mem_needed);
396 PRINT_DEBUG(" partitions to be sized automatically: %d\n",
397 mem_auto_no);
399 if (mem_needed > pages * 4) {
400 PRINT_ERR("Not enough expanded memory available\n");
401 return -EINVAL;
405 * partitioning:
406 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
407 * else: ; all partitions with zero xpram_sizes[i]
408 * partition equally the remaining space
410 if (mem_auto_no) {
411 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
412 PRINT_INFO(" automatically determined "
413 "partition size: %lu kB\n", mem_auto);
414 for (i = 0; i < xpram_devs; i++)
415 if (xpram_sizes[i] == 0)
416 xpram_sizes[i] = mem_auto;
418 return 0;
421 static struct request_queue *xpram_queue;
423 static int __init xpram_setup_blkdev(void)
425 unsigned long offset;
426 int i, rc = -ENOMEM;
428 for (i = 0; i < xpram_devs; i++) {
429 struct gendisk *disk = alloc_disk(1);
430 if (!disk)
431 goto out;
432 xpram_disks[i] = disk;
436 * Register xpram major.
438 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
439 if (rc < 0)
440 goto out;
443 * Assign the other needed values: make request function, sizes and
444 * hardsect size. All the minor devices feature the same value.
446 xpram_queue = blk_alloc_queue(GFP_KERNEL);
447 if (!xpram_queue) {
448 rc = -ENOMEM;
449 goto out_unreg;
451 blk_queue_make_request(xpram_queue, xpram_make_request);
452 blk_queue_hardsect_size(xpram_queue, 4096);
455 * Setup device structures.
457 offset = 0;
458 for (i = 0; i < xpram_devs; i++) {
459 struct gendisk *disk = xpram_disks[i];
461 xpram_devices[i].size = xpram_sizes[i] / 4;
462 xpram_devices[i].offset = offset;
463 offset += xpram_devices[i].size;
464 disk->major = XPRAM_MAJOR;
465 disk->first_minor = i;
466 disk->fops = &xpram_devops;
467 disk->private_data = &xpram_devices[i];
468 disk->queue = xpram_queue;
469 sprintf(disk->disk_name, "slram%d", i);
470 sprintf(disk->devfs_name, "slram/%d", i);
471 set_capacity(disk, xpram_sizes[i] << 1);
472 add_disk(disk);
475 return 0;
476 out_unreg:
477 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
478 out:
479 while (i--)
480 put_disk(xpram_disks[i]);
481 return rc;
485 * Finally, the init/exit functions.
487 static void __exit xpram_exit(void)
489 int i;
490 for (i = 0; i < xpram_devs; i++) {
491 del_gendisk(xpram_disks[i]);
492 put_disk(xpram_disks[i]);
494 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
495 blk_cleanup_queue(xpram_queue);
496 sysdev_unregister(&xpram_sys_device);
497 sysdev_class_unregister(&xpram_sysclass);
500 static int __init xpram_init(void)
502 int rc;
504 /* Find out size of expanded memory. */
505 if (xpram_present() != 0) {
506 PRINT_WARN("No expanded memory available\n");
507 return -ENODEV;
509 xpram_pages = xpram_highest_page_index();
510 PRINT_INFO(" %u pages expanded memory found (%lu KB).\n",
511 xpram_pages, (unsigned long) xpram_pages*4);
512 rc = xpram_setup_sizes(xpram_pages);
513 if (rc)
514 return rc;
515 rc = sysdev_class_register(&xpram_sysclass);
516 if (rc)
517 return rc;
519 rc = sysdev_register(&xpram_sys_device);
520 if (rc) {
521 sysdev_class_unregister(&xpram_sysclass);
522 return rc;
524 rc = xpram_setup_blkdev();
525 if (rc)
526 sysdev_unregister(&xpram_sys_device);
527 return rc;
530 module_init(xpram_init);
531 module_exit(xpram_exit);