xpram.c 12 KB

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