Pull esi-support into release branch
[linux-2.6] / drivers / s390 / block / xpram.c
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
7  *
8  * Author of XPRAM specific coding: Reinhard Buendgen
9  *                                  buendgen@de.ibm.com
10  * Rewrite for 2.5: Martin Schwidefsky <schwidefsky@de.ibm.com>
11  *
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
18  *
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[])
23  *
24  * Potential future improvements:
25  *   generic hard disk support to replace ad-hoc partitioning
26  */
27
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 <asm/uaccess.h>
40
41 #define XPRAM_NAME      "xpram"
42 #define XPRAM_DEVS      1       /* one partition */
43 #define XPRAM_MAX_DEVS  32      /* maximal number of devices (partitions) */
44
45 #define PRINT_DEBUG(x...)       printk(KERN_DEBUG XPRAM_NAME " debug:" x)
46 #define PRINT_INFO(x...)        printk(KERN_INFO XPRAM_NAME " info:" x)
47 #define PRINT_WARN(x...)        printk(KERN_WARNING XPRAM_NAME " warning:" x)
48 #define PRINT_ERR(x...)         printk(KERN_ERR XPRAM_NAME " error:" x)
49
50
51 typedef struct {
52         unsigned int    size;           /* size of xpram segment in pages */
53         unsigned int    offset;         /* start page of xpram segment */
54 } xpram_device_t;
55
56 static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
57 static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
58 static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
59 static unsigned int xpram_pages;
60 static int xpram_devs;
61
62 /*
63  * Parameter parsing functions.
64  */
65 static int __initdata devs = XPRAM_DEVS;
66 static char __initdata *sizes[XPRAM_MAX_DEVS];
67
68 module_param(devs, int, 0);
69 module_param_array(sizes, charp, NULL, 0);
70
71 MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
72                  "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
73 MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \
74                  "the defaults are 0s \n" \
75                  "All devices with size 0 equally partition the "
76                  "remaining space on the expanded strorage not "
77                  "claimed by explicit sizes\n");
78 MODULE_LICENSE("GPL");
79
80 /*
81  * Copy expanded memory page (4kB) into main memory                  
82  * Arguments                                                         
83  *           page_addr:    address of target page                    
84  *           xpage_index:  index of expandeded memory page           
85  * Return value                                                      
86  *           0:            if operation succeeds
87  *           -EIO:         if pgin failed
88  *           -ENXIO:       if xpram has vanished
89  */
90 static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
91 {
92         int cc;
93
94         __asm__ __volatile__ (
95                 "   lhi   %0,2\n"  /* return unused cc 2 if pgin traps */
96                 "   .insn rre,0xb22e0000,%1,%2\n"  /* pgin %1,%2 */
97                 "0: ipm   %0\n"
98                 "   srl   %0,28\n"
99                 "1:\n"
100 #ifndef CONFIG_64BIT
101                 ".section __ex_table,\"a\"\n"
102                 "   .align 4\n"
103                 "   .long  0b,1b\n"
104                 ".previous"
105 #else
106                 ".section __ex_table,\"a\"\n"
107                 "   .align 8\n"
108                 "   .quad 0b,1b\n"
109                 ".previous"
110 #endif
111                 : "=&d" (cc) 
112                 : "a" (__pa(page_addr)), "a" (xpage_index) 
113                 : "cc" );
114         if (cc == 3)
115                 return -ENXIO;
116         if (cc == 2) {
117                 PRINT_ERR("expanded storage lost!\n");
118                 return -ENXIO;
119         }
120         if (cc == 1) {
121                 PRINT_ERR("page in failed for page index %u.\n",
122                           xpage_index);
123                 return -EIO;
124         }
125         return 0;
126 }
127
128 /*
129  * Copy a 4kB page of main memory to an expanded memory page          
130  * Arguments                                                          
131  *           page_addr:    address of source page                     
132  *           xpage_index:  index of expandeded memory page            
133  * Return value                                                       
134  *           0:            if operation succeeds
135  *           -EIO:         if pgout failed
136  *           -ENXIO:       if xpram has vanished
137  */
138 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
139 {
140         int cc;
141
142         __asm__ __volatile__ (
143                 "   lhi   %0,2\n"  /* return unused cc 2 if pgout traps */
144                 "   .insn rre,0xb22f0000,%1,%2\n"  /* pgout %1,%2 */
145                 "0: ipm   %0\n"
146                 "   srl   %0,28\n"
147                 "1:\n"
148 #ifndef CONFIG_64BIT
149                 ".section __ex_table,\"a\"\n"
150                 "   .align 4\n"
151                 "   .long  0b,1b\n"
152                 ".previous"
153 #else
154                 ".section __ex_table,\"a\"\n"
155                 "   .align 8\n"
156                 "   .quad 0b,1b\n"
157                 ".previous"
158 #endif
159                 : "=&d" (cc) 
160                 : "a" (__pa(page_addr)), "a" (xpage_index) 
161                 : "cc" );
162         if (cc == 3)
163                 return -ENXIO;
164         if (cc == 2) {
165                 PRINT_ERR("expanded storage lost!\n");
166                 return -ENXIO;
167         }
168         if (cc == 1) {
169                 PRINT_ERR("page out failed for page index %u.\n",
170                           xpage_index);
171                 return -EIO;
172         }
173         return 0;
174 }
175
176 /*
177  * Check if xpram is available.
178  */
179 static int __init xpram_present(void)
180 {
181         unsigned long mem_page;
182         int rc;
183
184         mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
185         if (!mem_page)
186                 return -ENOMEM;
187         rc = xpram_page_in(mem_page, 0);
188         free_page(mem_page);
189         return rc ? -ENXIO : 0;
190 }
191
192 /*
193  * Return index of the last available xpram page.
194  */
195 static unsigned long __init xpram_highest_page_index(void)
196 {
197         unsigned int page_index, add_bit;
198         unsigned long mem_page;
199
200         mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
201         if (!mem_page)
202                 return 0;
203
204         page_index = 0;
205         add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
206         while (add_bit > 0) {
207                 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
208                         page_index |= add_bit;
209                 add_bit >>= 1;
210         }
211
212         free_page (mem_page);
213
214         return page_index;
215 }
216
217 /*
218  * Block device make request function.
219  */
220 static int xpram_make_request(request_queue_t *q, struct bio *bio)
221 {
222         xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
223         struct bio_vec *bvec;
224         unsigned int index;
225         unsigned long page_addr;
226         unsigned long bytes;
227         int i;
228
229         if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
230                 /* Request is not page-aligned. */
231                 goto fail;
232         if ((bio->bi_size >> 12) > xdev->size)
233                 /* Request size is no page-aligned. */
234                 goto fail;
235         if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
236                 goto fail;
237         index = (bio->bi_sector >> 3) + xdev->offset;
238         bio_for_each_segment(bvec, bio, i) {
239                 page_addr = (unsigned long)
240                         kmap(bvec->bv_page) + bvec->bv_offset;
241                 bytes = bvec->bv_len;
242                 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
243                         /* More paranoia. */
244                         goto fail;
245                 while (bytes > 0) {
246                         if (bio_data_dir(bio) == READ) {
247                                 if (xpram_page_in(page_addr, index) != 0)
248                                         goto fail;
249                         } else {
250                                 if (xpram_page_out(page_addr, index) != 0)
251                                         goto fail;
252                         }
253                         page_addr += 4096;
254                         bytes -= 4096;
255                         index++;
256                 }
257         }
258         set_bit(BIO_UPTODATE, &bio->bi_flags);
259         bytes = bio->bi_size;
260         bio->bi_size = 0;
261         bio->bi_end_io(bio, bytes, 0);
262         return 0;
263 fail:
264         bio_io_error(bio, bio->bi_size);
265         return 0;
266 }
267
268 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
269 {
270         unsigned long size;
271
272         /*
273          * get geometry: we have to fake one...  trim the size to a
274          * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
275          * whatever cylinders. Tell also that data starts at sector. 4.
276          */
277         size = (xpram_pages * 8) & ~0x3f;
278         geo->cylinders = size >> 6;
279         geo->heads = 4;
280         geo->sectors = 16;
281         geo->start = 4;
282         return 0;
283 }
284
285 static struct block_device_operations xpram_devops =
286 {
287         .owner  = THIS_MODULE,
288         .getgeo = xpram_getgeo,
289 };
290
291 /*
292  * Setup xpram_sizes array.
293  */
294 static int __init xpram_setup_sizes(unsigned long pages)
295 {
296         unsigned long mem_needed;
297         unsigned long mem_auto;
298         unsigned long long size;
299         int mem_auto_no;
300         int i;
301
302         /* Check number of devices. */
303         if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
304                 PRINT_ERR("invalid number %d of devices\n",devs);
305                 return -EINVAL;
306         }
307         xpram_devs = devs;
308
309         /*
310          * Copy sizes array to xpram_sizes and align partition
311          * sizes to page boundary.
312          */
313         mem_needed = 0;
314         mem_auto_no = 0;
315         for (i = 0; i < xpram_devs; i++) {
316                 if (sizes[i]) {
317                         size = simple_strtoull(sizes[i], &sizes[i], 0);
318                         switch (sizes[i][0]) {
319                         case 'g':
320                         case 'G':
321                                 size <<= 20;
322                                 break;
323                         case 'm':
324                         case 'M':
325                                 size <<= 10;
326                         }
327                         xpram_sizes[i] = (size + 3) & -4UL;
328                 }
329                 if (xpram_sizes[i])
330                         mem_needed += xpram_sizes[i];
331                 else
332                         mem_auto_no++;
333         }
334         
335         PRINT_INFO("  number of devices (partitions): %d \n", xpram_devs);
336         for (i = 0; i < xpram_devs; i++) {
337                 if (xpram_sizes[i])
338                         PRINT_INFO("  size of partition %d: %u kB\n",
339                                    i, xpram_sizes[i]);
340                 else
341                         PRINT_INFO("  size of partition %d to be set "
342                                    "automatically\n",i);
343         }
344         PRINT_DEBUG("  memory needed (for sized partitions): %lu kB\n",
345                     mem_needed);
346         PRINT_DEBUG("  partitions to be sized automatically: %d\n",
347                     mem_auto_no);
348
349         if (mem_needed > pages * 4) {
350                 PRINT_ERR("Not enough expanded memory available\n");
351                 return -EINVAL;
352         }
353
354         /*
355          * partitioning:
356          * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
357          * else:             ; all partitions with zero xpram_sizes[i]
358          *                     partition equally the remaining space
359          */
360         if (mem_auto_no) {
361                 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
362                 PRINT_INFO("  automatically determined "
363                            "partition size: %lu kB\n", mem_auto);
364                 for (i = 0; i < xpram_devs; i++)
365                         if (xpram_sizes[i] == 0)
366                                 xpram_sizes[i] = mem_auto;
367         }
368         return 0;
369 }
370
371 static struct request_queue *xpram_queue;
372
373 static int __init xpram_setup_blkdev(void)
374 {
375         unsigned long offset;
376         int i, rc = -ENOMEM;
377
378         for (i = 0; i < xpram_devs; i++) {
379                 struct gendisk *disk = alloc_disk(1);
380                 if (!disk)
381                         goto out;
382                 xpram_disks[i] = disk;
383         }
384
385         /*
386          * Register xpram major.
387          */
388         rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
389         if (rc < 0)
390                 goto out;
391
392         /*
393          * Assign the other needed values: make request function, sizes and
394          * hardsect size. All the minor devices feature the same value.
395          */
396         xpram_queue = blk_alloc_queue(GFP_KERNEL);
397         if (!xpram_queue) {
398                 rc = -ENOMEM;
399                 goto out_unreg;
400         }
401         blk_queue_make_request(xpram_queue, xpram_make_request);
402         blk_queue_hardsect_size(xpram_queue, 4096);
403
404         /*
405          * Setup device structures.
406          */
407         offset = 0;
408         for (i = 0; i < xpram_devs; i++) {
409                 struct gendisk *disk = xpram_disks[i];
410
411                 xpram_devices[i].size = xpram_sizes[i] / 4;
412                 xpram_devices[i].offset = offset;
413                 offset += xpram_devices[i].size;
414                 disk->major = XPRAM_MAJOR;
415                 disk->first_minor = i;
416                 disk->fops = &xpram_devops;
417                 disk->private_data = &xpram_devices[i];
418                 disk->queue = xpram_queue;
419                 sprintf(disk->disk_name, "slram%d", i);
420                 set_capacity(disk, xpram_sizes[i] << 1);
421                 add_disk(disk);
422         }
423
424         return 0;
425 out_unreg:
426         unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
427 out:
428         while (i--)
429                 put_disk(xpram_disks[i]);
430         return rc;
431 }
432
433 /*
434  * Finally, the init/exit functions.
435  */
436 static void __exit xpram_exit(void)
437 {
438         int i;
439         for (i = 0; i < xpram_devs; i++) {
440                 del_gendisk(xpram_disks[i]);
441                 put_disk(xpram_disks[i]);
442         }
443         unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
444         blk_cleanup_queue(xpram_queue);
445 }
446
447 static int __init xpram_init(void)
448 {
449         int rc;
450
451         /* Find out size of expanded memory. */
452         if (xpram_present() != 0) {
453                 PRINT_WARN("No expanded memory available\n");
454                 return -ENODEV;
455         }
456         xpram_pages = xpram_highest_page_index();
457         PRINT_INFO("  %u pages expanded memory found (%lu KB).\n",
458                    xpram_pages, (unsigned long) xpram_pages*4);
459         rc = xpram_setup_sizes(xpram_pages);
460         if (rc)
461                 return rc;
462         return xpram_setup_blkdev();
463 }
464
465 module_init(xpram_init);
466 module_exit(xpram_exit);