xpram.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 <linux/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 blk_qc_t xpram_make_request(struct request_queue *q, struct bio *bio)
  165. {
  166. xpram_device_t *xdev = bio->bi_disk->private_data;
  167. struct bio_vec bvec;
  168. struct bvec_iter iter;
  169. unsigned int index;
  170. unsigned long page_addr;
  171. unsigned long bytes;
  172. blk_queue_split(q, &bio);
  173. if ((bio->bi_iter.bi_sector & 7) != 0 ||
  174. (bio->bi_iter.bi_size & 4095) != 0)
  175. /* Request is not page-aligned. */
  176. goto fail;
  177. if ((bio->bi_iter.bi_size >> 12) > xdev->size)
  178. /* Request size is no page-aligned. */
  179. goto fail;
  180. if ((bio->bi_iter.bi_sector >> 3) > 0xffffffffU - xdev->offset)
  181. goto fail;
  182. index = (bio->bi_iter.bi_sector >> 3) + xdev->offset;
  183. bio_for_each_segment(bvec, bio, iter) {
  184. page_addr = (unsigned long)
  185. kmap(bvec.bv_page) + bvec.bv_offset;
  186. bytes = bvec.bv_len;
  187. if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
  188. /* More paranoia. */
  189. goto fail;
  190. while (bytes > 0) {
  191. if (bio_data_dir(bio) == READ) {
  192. if (xpram_page_in(page_addr, index) != 0)
  193. goto fail;
  194. } else {
  195. if (xpram_page_out(page_addr, index) != 0)
  196. goto fail;
  197. }
  198. page_addr += 4096;
  199. bytes -= 4096;
  200. index++;
  201. }
  202. }
  203. bio_endio(bio);
  204. return BLK_QC_T_NONE;
  205. fail:
  206. bio_io_error(bio);
  207. return BLK_QC_T_NONE;
  208. }
  209. static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  210. {
  211. unsigned long size;
  212. /*
  213. * get geometry: we have to fake one... trim the size to a
  214. * multiple of 64 (32k): tell we have 16 sectors, 4 heads,
  215. * whatever cylinders. Tell also that data starts at sector. 4.
  216. */
  217. size = (xpram_pages * 8) & ~0x3f;
  218. geo->cylinders = size >> 6;
  219. geo->heads = 4;
  220. geo->sectors = 16;
  221. geo->start = 4;
  222. return 0;
  223. }
  224. static const struct block_device_operations xpram_devops =
  225. {
  226. .owner = THIS_MODULE,
  227. .getgeo = xpram_getgeo,
  228. };
  229. /*
  230. * Setup xpram_sizes array.
  231. */
  232. static int __init xpram_setup_sizes(unsigned long pages)
  233. {
  234. unsigned long mem_needed;
  235. unsigned long mem_auto;
  236. unsigned long long size;
  237. char *sizes_end;
  238. int mem_auto_no;
  239. int i;
  240. /* Check number of devices. */
  241. if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
  242. pr_err("%d is not a valid number of XPRAM devices\n",devs);
  243. return -EINVAL;
  244. }
  245. xpram_devs = devs;
  246. /*
  247. * Copy sizes array to xpram_sizes and align partition
  248. * sizes to page boundary.
  249. */
  250. mem_needed = 0;
  251. mem_auto_no = 0;
  252. for (i = 0; i < xpram_devs; i++) {
  253. if (sizes[i]) {
  254. size = simple_strtoull(sizes[i], &sizes_end, 0);
  255. switch (*sizes_end) {
  256. case 'g':
  257. case 'G':
  258. size <<= 20;
  259. break;
  260. case 'm':
  261. case 'M':
  262. size <<= 10;
  263. }
  264. xpram_sizes[i] = (size + 3) & -4UL;
  265. }
  266. if (xpram_sizes[i])
  267. mem_needed += xpram_sizes[i];
  268. else
  269. mem_auto_no++;
  270. }
  271. pr_info(" number of devices (partitions): %d \n", xpram_devs);
  272. for (i = 0; i < xpram_devs; i++) {
  273. if (xpram_sizes[i])
  274. pr_info(" size of partition %d: %u kB\n",
  275. i, xpram_sizes[i]);
  276. else
  277. pr_info(" size of partition %d to be set "
  278. "automatically\n",i);
  279. }
  280. pr_info(" memory needed (for sized partitions): %lu kB\n",
  281. mem_needed);
  282. pr_info(" partitions to be sized automatically: %d\n",
  283. mem_auto_no);
  284. if (mem_needed > pages * 4) {
  285. pr_err("Not enough expanded memory available\n");
  286. return -EINVAL;
  287. }
  288. /*
  289. * partitioning:
  290. * xpram_sizes[i] != 0; partition i has size xpram_sizes[i] kB
  291. * else: ; all partitions with zero xpram_sizes[i]
  292. * partition equally the remaining space
  293. */
  294. if (mem_auto_no) {
  295. mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
  296. pr_info(" automatically determined "
  297. "partition size: %lu kB\n", mem_auto);
  298. for (i = 0; i < xpram_devs; i++)
  299. if (xpram_sizes[i] == 0)
  300. xpram_sizes[i] = mem_auto;
  301. }
  302. return 0;
  303. }
  304. static int __init xpram_setup_blkdev(void)
  305. {
  306. unsigned long offset;
  307. int i, rc = -ENOMEM;
  308. for (i = 0; i < xpram_devs; i++) {
  309. xpram_disks[i] = alloc_disk(1);
  310. if (!xpram_disks[i])
  311. goto out;
  312. xpram_queues[i] = blk_alloc_queue(GFP_KERNEL);
  313. if (!xpram_queues[i]) {
  314. put_disk(xpram_disks[i]);
  315. goto out;
  316. }
  317. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, xpram_queues[i]);
  318. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, xpram_queues[i]);
  319. blk_queue_make_request(xpram_queues[i], xpram_make_request);
  320. blk_queue_logical_block_size(xpram_queues[i], 4096);
  321. }
  322. /*
  323. * Register xpram major.
  324. */
  325. rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
  326. if (rc < 0)
  327. goto out;
  328. /*
  329. * Setup device structures.
  330. */
  331. offset = 0;
  332. for (i = 0; i < xpram_devs; i++) {
  333. struct gendisk *disk = xpram_disks[i];
  334. xpram_devices[i].size = xpram_sizes[i] / 4;
  335. xpram_devices[i].offset = offset;
  336. offset += xpram_devices[i].size;
  337. disk->major = XPRAM_MAJOR;
  338. disk->first_minor = i;
  339. disk->fops = &xpram_devops;
  340. disk->private_data = &xpram_devices[i];
  341. disk->queue = xpram_queues[i];
  342. sprintf(disk->disk_name, "slram%d", i);
  343. set_capacity(disk, xpram_sizes[i] << 1);
  344. add_disk(disk);
  345. }
  346. return 0;
  347. out:
  348. while (i--) {
  349. blk_cleanup_queue(xpram_queues[i]);
  350. put_disk(xpram_disks[i]);
  351. }
  352. return rc;
  353. }
  354. /*
  355. * Resume failed: Print error message and call panic.
  356. */
  357. static void xpram_resume_error(const char *message)
  358. {
  359. pr_err("Resuming the system failed: %s\n", message);
  360. panic("xpram resume error\n");
  361. }
  362. /*
  363. * Check if xpram setup changed between suspend and resume.
  364. */
  365. static int xpram_restore(struct device *dev)
  366. {
  367. if (!xpram_pages)
  368. return 0;
  369. if (xpram_present() != 0)
  370. xpram_resume_error("xpram disappeared");
  371. if (xpram_pages != xpram_highest_page_index() + 1)
  372. xpram_resume_error("Size of xpram changed");
  373. return 0;
  374. }
  375. static const struct dev_pm_ops xpram_pm_ops = {
  376. .restore = xpram_restore,
  377. };
  378. static struct platform_driver xpram_pdrv = {
  379. .driver = {
  380. .name = XPRAM_NAME,
  381. .pm = &xpram_pm_ops,
  382. },
  383. };
  384. static struct platform_device *xpram_pdev;
  385. /*
  386. * Finally, the init/exit functions.
  387. */
  388. static void __exit xpram_exit(void)
  389. {
  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]);
  395. }
  396. unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
  397. platform_device_unregister(xpram_pdev);
  398. platform_driver_unregister(&xpram_pdrv);
  399. }
  400. static int __init xpram_init(void)
  401. {
  402. 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;
  407. }
  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. rc = platform_driver_register(&xpram_pdrv);
  415. if (rc)
  416. return rc;
  417. xpram_pdev = platform_device_register_simple(XPRAM_NAME, -1, NULL, 0);
  418. if (IS_ERR(xpram_pdev)) {
  419. rc = PTR_ERR(xpram_pdev);
  420. goto fail_platform_driver_unregister;
  421. }
  422. rc = xpram_setup_blkdev();
  423. if (rc)
  424. goto fail_platform_device_unregister;
  425. return 0;
  426. fail_platform_device_unregister:
  427. platform_device_unregister(xpram_pdev);
  428. fail_platform_driver_unregister:
  429. platform_driver_unregister(&xpram_pdrv);
  430. return rc;
  431. }
  432. module_init(xpram_init);
  433. module_exit(xpram_exit);