• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define KMSG_COMPONENT "xpram"
29 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30 
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/blkdev.h>
37 #include <linux/blkpg.h>
38 #include <linux/hdreg.h>  /* HDIO_GETGEO */
39 #include <linux/device.h>
40 #include <linux/bio.h>
41 #include <linux/suspend.h>
42 #include <linux/platform_device.h>
43 #include <linux/gfp.h>
44 #include <asm/uaccess.h>
45 
46 #define XPRAM_NAME	"xpram"
47 #define XPRAM_DEVS	1	/* one partition */
48 #define XPRAM_MAX_DEVS	32	/* maximal number of devices (partitions) */
49 
50 typedef struct {
51 	unsigned int	size;		/* size of xpram segment in pages */
52 	unsigned int	offset;		/* start page of xpram segment */
53 } xpram_device_t;
54 
55 static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
56 static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
57 static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
58 static struct request_queue *xpram_queues[XPRAM_MAX_DEVS];
59 static unsigned int xpram_pages;
60 static int xpram_devs;
61 
62 /*
63  * Parameter parsing functions.
64  */
65 static int devs = XPRAM_DEVS;
66 static char *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  */
xpram_page_in(unsigned long page_addr,unsigned int xpage_index)90 static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
91 {
92 	int cc = 2;	/* return unused cc 2 if pgin traps */
93 
94 	asm volatile(
95 		"	.insn	rre,0xb22e0000,%1,%2\n"  /* pgin %1,%2 */
96 		"0:	ipm	%0\n"
97 		"	srl	%0,28\n"
98 		"1:\n"
99 		EX_TABLE(0b,1b)
100 		: "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
101 	if (cc == 3)
102 		return -ENXIO;
103 	if (cc == 2)
104 		return -ENXIO;
105 	if (cc == 1)
106 		return -EIO;
107 	return 0;
108 }
109 
110 /*
111  * Copy a 4kB page of main memory to an expanded memory page
112  * Arguments
113  *           page_addr:    address of source page
114  *           xpage_index:  index of expandeded memory page
115  * Return value
116  *           0:            if operation succeeds
117  *           -EIO:         if pgout failed
118  *           -ENXIO:       if xpram has vanished
119  */
xpram_page_out(unsigned long page_addr,unsigned int xpage_index)120 static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
121 {
122 	int cc = 2;	/* return unused cc 2 if pgin traps */
123 
124 	asm volatile(
125 		"	.insn	rre,0xb22f0000,%1,%2\n"  /* pgout %1,%2 */
126 		"0:	ipm	%0\n"
127 		"	srl	%0,28\n"
128 		"1:\n"
129 		EX_TABLE(0b,1b)
130 		: "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
131 	if (cc == 3)
132 		return -ENXIO;
133 	if (cc == 2)
134 		return -ENXIO;
135 	if (cc == 1)
136 		return -EIO;
137 	return 0;
138 }
139 
140 /*
141  * Check if xpram is available.
142  */
xpram_present(void)143 static int xpram_present(void)
144 {
145 	unsigned long mem_page;
146 	int rc;
147 
148 	mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
149 	if (!mem_page)
150 		return -ENOMEM;
151 	rc = xpram_page_in(mem_page, 0);
152 	free_page(mem_page);
153 	return rc ? -ENXIO : 0;
154 }
155 
156 /*
157  * Return index of the last available xpram page.
158  */
xpram_highest_page_index(void)159 static unsigned long xpram_highest_page_index(void)
160 {
161 	unsigned int page_index, add_bit;
162 	unsigned long mem_page;
163 
164 	mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
165 	if (!mem_page)
166 		return 0;
167 
168 	page_index = 0;
169 	add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
170 	while (add_bit > 0) {
171 		if (xpram_page_in(mem_page, page_index | add_bit) == 0)
172 			page_index |= add_bit;
173 		add_bit >>= 1;
174 	}
175 
176 	free_page (mem_page);
177 
178 	return page_index;
179 }
180 
181 /*
182  * Block device make request function.
183  */
xpram_make_request(struct request_queue * q,struct bio * bio)184 static void xpram_make_request(struct request_queue *q, struct bio *bio)
185 {
186 	xpram_device_t *xdev = bio->bi_bdev->bd_disk->private_data;
187 	struct bio_vec *bvec;
188 	unsigned int index;
189 	unsigned long page_addr;
190 	unsigned long bytes;
191 	int i;
192 
193 	if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
194 		/* Request is not page-aligned. */
195 		goto fail;
196 	if ((bio->bi_size >> 12) > xdev->size)
197 		/* Request size is no page-aligned. */
198 		goto fail;
199 	if ((bio->bi_sector >> 3) > 0xffffffffU - xdev->offset)
200 		goto fail;
201 	index = (bio->bi_sector >> 3) + xdev->offset;
202 	bio_for_each_segment(bvec, bio, i) {
203 		page_addr = (unsigned long)
204 			kmap(bvec->bv_page) + bvec->bv_offset;
205 		bytes = bvec->bv_len;
206 		if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
207 			/* More paranoia. */
208 			goto fail;
209 		while (bytes > 0) {
210 			if (bio_data_dir(bio) == READ) {
211 				if (xpram_page_in(page_addr, index) != 0)
212 					goto fail;
213 			} else {
214 				if (xpram_page_out(page_addr, index) != 0)
215 					goto fail;
216 			}
217 			page_addr += 4096;
218 			bytes -= 4096;
219 			index++;
220 		}
221 	}
222 	set_bit(BIO_UPTODATE, &bio->bi_flags);
223 	bio_endio(bio, 0);
224 	return;
225 fail:
226 	bio_io_error(bio);
227 }
228 
xpram_getgeo(struct block_device * bdev,struct hd_geometry * geo)229 static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
230 {
231 	unsigned long size;
232 
233 	/*
234 	 * get geometry: we have to fake one...  trim the size to a
235 	 * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
236 	 * whatever cylinders. Tell also that data starts at sector. 4.
237 	 */
238 	size = (xpram_pages * 8) & ~0x3f;
239 	geo->cylinders = size >> 6;
240 	geo->heads = 4;
241 	geo->sectors = 16;
242 	geo->start = 4;
243 	return 0;
244 }
245 
246 static const struct block_device_operations xpram_devops =
247 {
248 	.owner	= THIS_MODULE,
249 	.getgeo	= xpram_getgeo,
250 };
251 
252 /*
253  * Setup xpram_sizes array.
254  */
xpram_setup_sizes(unsigned long pages)255 static int __init xpram_setup_sizes(unsigned long pages)
256 {
257 	unsigned long mem_needed;
258 	unsigned long mem_auto;
259 	unsigned long long size;
260 	int mem_auto_no;
261 	int i;
262 
263 	/* Check number of devices. */
264 	if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
265 		pr_err("%d is not a valid number of XPRAM devices\n",devs);
266 		return -EINVAL;
267 	}
268 	xpram_devs = devs;
269 
270 	/*
271 	 * Copy sizes array to xpram_sizes and align partition
272 	 * sizes to page boundary.
273 	 */
274 	mem_needed = 0;
275 	mem_auto_no = 0;
276 	for (i = 0; i < xpram_devs; i++) {
277 		if (sizes[i]) {
278 			size = simple_strtoull(sizes[i], &sizes[i], 0);
279 			switch (sizes[i][0]) {
280 			case 'g':
281 			case 'G':
282 				size <<= 20;
283 				break;
284 			case 'm':
285 			case 'M':
286 				size <<= 10;
287 			}
288 			xpram_sizes[i] = (size + 3) & -4UL;
289 		}
290 		if (xpram_sizes[i])
291 			mem_needed += xpram_sizes[i];
292 		else
293 			mem_auto_no++;
294 	}
295 
296 	pr_info("  number of devices (partitions): %d \n", xpram_devs);
297 	for (i = 0; i < xpram_devs; i++) {
298 		if (xpram_sizes[i])
299 			pr_info("  size of partition %d: %u kB\n",
300 				i, xpram_sizes[i]);
301 		else
302 			pr_info("  size of partition %d to be set "
303 				"automatically\n",i);
304 	}
305 	pr_info("  memory needed (for sized partitions): %lu kB\n",
306 		mem_needed);
307 	pr_info("  partitions to be sized automatically: %d\n",
308 		mem_auto_no);
309 
310 	if (mem_needed > pages * 4) {
311 		pr_err("Not enough expanded memory available\n");
312 		return -EINVAL;
313 	}
314 
315 	/*
316 	 * partitioning:
317 	 * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
318 	 * else:             ; all partitions with zero xpram_sizes[i]
319 	 *                     partition equally the remaining space
320 	 */
321 	if (mem_auto_no) {
322 		mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
323 		pr_info("  automatically determined "
324 			"partition size: %lu kB\n", mem_auto);
325 		for (i = 0; i < xpram_devs; i++)
326 			if (xpram_sizes[i] == 0)
327 				xpram_sizes[i] = mem_auto;
328 	}
329 	return 0;
330 }
331 
xpram_setup_blkdev(void)332 static int __init xpram_setup_blkdev(void)
333 {
334 	unsigned long offset;
335 	int i, rc = -ENOMEM;
336 
337 	for (i = 0; i < xpram_devs; i++) {
338 		xpram_disks[i] = alloc_disk(1);
339 		if (!xpram_disks[i])
340 			goto out;
341 		xpram_queues[i] = blk_alloc_queue(GFP_KERNEL);
342 		if (!xpram_queues[i]) {
343 			put_disk(xpram_disks[i]);
344 			goto out;
345 		}
346 		queue_flag_set_unlocked(QUEUE_FLAG_NONROT, xpram_queues[i]);
347 		queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, xpram_queues[i]);
348 		blk_queue_make_request(xpram_queues[i], xpram_make_request);
349 		blk_queue_logical_block_size(xpram_queues[i], 4096);
350 	}
351 
352 	/*
353 	 * Register xpram major.
354 	 */
355 	rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
356 	if (rc < 0)
357 		goto out;
358 
359 	/*
360 	 * Setup device structures.
361 	 */
362 	offset = 0;
363 	for (i = 0; i < xpram_devs; i++) {
364 		struct gendisk *disk = xpram_disks[i];
365 
366 		xpram_devices[i].size = xpram_sizes[i] / 4;
367 		xpram_devices[i].offset = offset;
368 		offset += xpram_devices[i].size;
369 		disk->major = XPRAM_MAJOR;
370 		disk->first_minor = i;
371 		disk->fops = &xpram_devops;
372 		disk->private_data = &xpram_devices[i];
373 		disk->queue = xpram_queues[i];
374 		sprintf(disk->disk_name, "slram%d", i);
375 		set_capacity(disk, xpram_sizes[i] << 1);
376 		add_disk(disk);
377 	}
378 
379 	return 0;
380 out:
381 	while (i--) {
382 		blk_cleanup_queue(xpram_queues[i]);
383 		put_disk(xpram_disks[i]);
384 	}
385 	return rc;
386 }
387 
388 /*
389  * Resume failed: Print error message and call panic.
390  */
xpram_resume_error(const char * message)391 static void xpram_resume_error(const char *message)
392 {
393 	pr_err("Resuming the system failed: %s\n", message);
394 	panic("xpram resume error\n");
395 }
396 
397 /*
398  * Check if xpram setup changed between suspend and resume.
399  */
xpram_restore(struct device * dev)400 static int xpram_restore(struct device *dev)
401 {
402 	if (!xpram_pages)
403 		return 0;
404 	if (xpram_present() != 0)
405 		xpram_resume_error("xpram disappeared");
406 	if (xpram_pages != xpram_highest_page_index() + 1)
407 		xpram_resume_error("Size of xpram changed");
408 	return 0;
409 }
410 
411 static const struct dev_pm_ops xpram_pm_ops = {
412 	.restore	= xpram_restore,
413 };
414 
415 static struct platform_driver xpram_pdrv = {
416 	.driver = {
417 		.name	= XPRAM_NAME,
418 		.owner	= THIS_MODULE,
419 		.pm	= &xpram_pm_ops,
420 	},
421 };
422 
423 static struct platform_device *xpram_pdev;
424 
425 /*
426  * Finally, the init/exit functions.
427  */
xpram_exit(void)428 static void __exit xpram_exit(void)
429 {
430 	int i;
431 	for (i = 0; i < xpram_devs; i++) {
432 		del_gendisk(xpram_disks[i]);
433 		blk_cleanup_queue(xpram_queues[i]);
434 		put_disk(xpram_disks[i]);
435 	}
436 	unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
437 	platform_device_unregister(xpram_pdev);
438 	platform_driver_unregister(&xpram_pdrv);
439 }
440 
xpram_init(void)441 static int __init xpram_init(void)
442 {
443 	int rc;
444 
445 	/* Find out size of expanded memory. */
446 	if (xpram_present() != 0) {
447 		pr_err("No expanded memory available\n");
448 		return -ENODEV;
449 	}
450 	xpram_pages = xpram_highest_page_index() + 1;
451 	pr_info("  %u pages expanded memory found (%lu KB).\n",
452 		xpram_pages, (unsigned long) xpram_pages*4);
453 	rc = xpram_setup_sizes(xpram_pages);
454 	if (rc)
455 		return rc;
456 	rc = platform_driver_register(&xpram_pdrv);
457 	if (rc)
458 		return rc;
459 	xpram_pdev = platform_device_register_simple(XPRAM_NAME, -1, NULL, 0);
460 	if (IS_ERR(xpram_pdev)) {
461 		rc = PTR_ERR(xpram_pdev);
462 		goto fail_platform_driver_unregister;
463 	}
464 	rc = xpram_setup_blkdev();
465 	if (rc)
466 		goto fail_platform_device_unregister;
467 	return 0;
468 
469 fail_platform_device_unregister:
470 	platform_device_unregister(xpram_pdev);
471 fail_platform_driver_unregister:
472 	platform_driver_unregister(&xpram_pdrv);
473 	return rc;
474 }
475 
476 module_init(xpram_init);
477 module_exit(xpram_exit);
478