extmem.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * File...........: arch/s390/mm/extmem.c
  3. * Author(s)......: Carsten Otte <cotte@de.ibm.com>
  4. * Rob M van der Heij <rvdheij@nl.ibm.com>
  5. * Steven Shultz <shultzss@us.ibm.com>
  6. * Bugreports.to..: <Linux390@de.ibm.com>
  7. * (C) IBM Corporation 2002-2004
  8. */
  9. #define KMSG_COMPONENT "extmem"
  10. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/list.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/ctype.h>
  19. #include <linux/ioport.h>
  20. #include <asm/page.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/ebcdic.h>
  23. #include <asm/errno.h>
  24. #include <asm/extmem.h>
  25. #include <asm/cpcmd.h>
  26. #include <asm/setup.h>
  27. #define DCSS_LOADSHR 0x00
  28. #define DCSS_LOADNSR 0x04
  29. #define DCSS_PURGESEG 0x08
  30. #define DCSS_FINDSEG 0x0c
  31. #define DCSS_LOADNOLY 0x10
  32. #define DCSS_SEGEXT 0x18
  33. #define DCSS_LOADSHRX 0x20
  34. #define DCSS_LOADNSRX 0x24
  35. #define DCSS_FINDSEGX 0x2c
  36. #define DCSS_SEGEXTX 0x38
  37. #define DCSS_FINDSEGA 0x0c
  38. struct qrange {
  39. unsigned long start; /* last byte type */
  40. unsigned long end; /* last byte reserved */
  41. };
  42. struct qout64 {
  43. unsigned long segstart;
  44. unsigned long segend;
  45. int segcnt;
  46. int segrcnt;
  47. struct qrange range[6];
  48. };
  49. #ifdef CONFIG_64BIT
  50. struct qrange_old {
  51. unsigned int start; /* last byte type */
  52. unsigned int end; /* last byte reserved */
  53. };
  54. /* output area format for the Diag x'64' old subcode x'18' */
  55. struct qout64_old {
  56. int segstart;
  57. int segend;
  58. int segcnt;
  59. int segrcnt;
  60. struct qrange_old range[6];
  61. };
  62. #endif
  63. struct qin64 {
  64. char qopcode;
  65. char rsrv1[3];
  66. char qrcode;
  67. char rsrv2[3];
  68. char qname[8];
  69. unsigned int qoutptr;
  70. short int qoutlen;
  71. };
  72. struct dcss_segment {
  73. struct list_head list;
  74. char dcss_name[8];
  75. char res_name[15];
  76. unsigned long start_addr;
  77. unsigned long end;
  78. atomic_t ref_count;
  79. int do_nonshared;
  80. unsigned int vm_segtype;
  81. struct qrange range[6];
  82. int segcnt;
  83. struct resource *res;
  84. };
  85. static DEFINE_MUTEX(dcss_lock);
  86. static LIST_HEAD(dcss_list);
  87. static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
  88. "EW/EN-MIXED" };
  89. static int loadshr_scode, loadnsr_scode, findseg_scode;
  90. static int segext_scode, purgeseg_scode;
  91. static int scode_set;
  92. /* set correct Diag x'64' subcodes. */
  93. static int
  94. dcss_set_subcodes(void)
  95. {
  96. #ifdef CONFIG_64BIT
  97. char *name = kmalloc(8 * sizeof(char), GFP_KERNEL | GFP_DMA);
  98. unsigned long rx, ry;
  99. int rc;
  100. if (name == NULL)
  101. return -ENOMEM;
  102. rx = (unsigned long) name;
  103. ry = DCSS_FINDSEGX;
  104. strcpy(name, "dummy");
  105. asm volatile(
  106. " diag %0,%1,0x64\n"
  107. "0: ipm %2\n"
  108. " srl %2,28\n"
  109. " j 2f\n"
  110. "1: la %2,3\n"
  111. "2:\n"
  112. EX_TABLE(0b, 1b)
  113. : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
  114. kfree(name);
  115. /* Diag x'64' new subcodes are supported, set to new subcodes */
  116. if (rc != 3) {
  117. loadshr_scode = DCSS_LOADSHRX;
  118. loadnsr_scode = DCSS_LOADNSRX;
  119. purgeseg_scode = DCSS_PURGESEG;
  120. findseg_scode = DCSS_FINDSEGX;
  121. segext_scode = DCSS_SEGEXTX;
  122. return 0;
  123. }
  124. #endif
  125. /* Diag x'64' new subcodes are not supported, set to old subcodes */
  126. loadshr_scode = DCSS_LOADNOLY;
  127. loadnsr_scode = DCSS_LOADNSR;
  128. purgeseg_scode = DCSS_PURGESEG;
  129. findseg_scode = DCSS_FINDSEG;
  130. segext_scode = DCSS_SEGEXT;
  131. return 0;
  132. }
  133. /*
  134. * Create the 8 bytes, ebcdic VM segment name from
  135. * an ascii name.
  136. */
  137. static void
  138. dcss_mkname(char *name, char *dcss_name)
  139. {
  140. int i;
  141. for (i = 0; i < 8; i++) {
  142. if (name[i] == '\0')
  143. break;
  144. dcss_name[i] = toupper(name[i]);
  145. };
  146. for (; i < 8; i++)
  147. dcss_name[i] = ' ';
  148. ASCEBC(dcss_name, 8);
  149. }
  150. /*
  151. * search all segments in dcss_list, and return the one
  152. * namend *name. If not found, return NULL.
  153. */
  154. static struct dcss_segment *
  155. segment_by_name (char *name)
  156. {
  157. char dcss_name[9];
  158. struct list_head *l;
  159. struct dcss_segment *tmp, *retval = NULL;
  160. BUG_ON(!mutex_is_locked(&dcss_lock));
  161. dcss_mkname (name, dcss_name);
  162. list_for_each (l, &dcss_list) {
  163. tmp = list_entry (l, struct dcss_segment, list);
  164. if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
  165. retval = tmp;
  166. break;
  167. }
  168. }
  169. return retval;
  170. }
  171. /*
  172. * Perform a function on a dcss segment.
  173. */
  174. static inline int
  175. dcss_diag(int *func, void *parameter,
  176. unsigned long *ret1, unsigned long *ret2)
  177. {
  178. unsigned long rx, ry;
  179. int rc;
  180. if (scode_set == 0) {
  181. rc = dcss_set_subcodes();
  182. if (rc < 0)
  183. return rc;
  184. scode_set = 1;
  185. }
  186. rx = (unsigned long) parameter;
  187. ry = (unsigned long) *func;
  188. #ifdef CONFIG_64BIT
  189. /* 64-bit Diag x'64' new subcode, keep in 64-bit addressing mode */
  190. if (*func > DCSS_SEGEXT)
  191. asm volatile(
  192. " diag %0,%1,0x64\n"
  193. " ipm %2\n"
  194. " srl %2,28\n"
  195. : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
  196. /* 31-bit Diag x'64' old subcode, switch to 31-bit addressing mode */
  197. else
  198. asm volatile(
  199. " sam31\n"
  200. " diag %0,%1,0x64\n"
  201. " sam64\n"
  202. " ipm %2\n"
  203. " srl %2,28\n"
  204. : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
  205. #else
  206. asm volatile(
  207. " diag %0,%1,0x64\n"
  208. " ipm %2\n"
  209. " srl %2,28\n"
  210. : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
  211. #endif
  212. *ret1 = rx;
  213. *ret2 = ry;
  214. return rc;
  215. }
  216. static inline int
  217. dcss_diag_translate_rc (int vm_rc) {
  218. if (vm_rc == 44)
  219. return -ENOENT;
  220. return -EIO;
  221. }
  222. /* do a diag to get info about a segment.
  223. * fills start_address, end and vm_segtype fields
  224. */
  225. static int
  226. query_segment_type (struct dcss_segment *seg)
  227. {
  228. unsigned long dummy, vmrc;
  229. int diag_cc, rc, i;
  230. struct qout64 *qout;
  231. struct qin64 *qin;
  232. qin = kmalloc(sizeof(*qin), GFP_KERNEL | GFP_DMA);
  233. qout = kmalloc(sizeof(*qout), GFP_KERNEL | GFP_DMA);
  234. if ((qin == NULL) || (qout == NULL)) {
  235. rc = -ENOMEM;
  236. goto out_free;
  237. }
  238. /* initialize diag input parameters */
  239. qin->qopcode = DCSS_FINDSEGA;
  240. qin->qoutptr = (unsigned long) qout;
  241. qin->qoutlen = sizeof(struct qout64);
  242. memcpy (qin->qname, seg->dcss_name, 8);
  243. diag_cc = dcss_diag(&segext_scode, qin, &dummy, &vmrc);
  244. if (diag_cc < 0) {
  245. rc = diag_cc;
  246. goto out_free;
  247. }
  248. if (diag_cc > 1) {
  249. pr_warning("Querying a DCSS type failed with rc=%ld\n", vmrc);
  250. rc = dcss_diag_translate_rc (vmrc);
  251. goto out_free;
  252. }
  253. #ifdef CONFIG_64BIT
  254. /* Only old format of output area of Diagnose x'64' is supported,
  255. copy data for the new format. */
  256. if (segext_scode == DCSS_SEGEXT) {
  257. struct qout64_old *qout_old;
  258. qout_old = kzalloc(sizeof(*qout_old), GFP_KERNEL | GFP_DMA);
  259. if (qout_old == NULL) {
  260. rc = -ENOMEM;
  261. goto out_free;
  262. }
  263. memcpy(qout_old, qout, sizeof(struct qout64_old));
  264. qout->segstart = (unsigned long) qout_old->segstart;
  265. qout->segend = (unsigned long) qout_old->segend;
  266. qout->segcnt = qout_old->segcnt;
  267. qout->segrcnt = qout_old->segrcnt;
  268. if (qout->segcnt > 6)
  269. qout->segrcnt = 6;
  270. for (i = 0; i < qout->segrcnt; i++) {
  271. qout->range[i].start =
  272. (unsigned long) qout_old->range[i].start;
  273. qout->range[i].end =
  274. (unsigned long) qout_old->range[i].end;
  275. }
  276. kfree(qout_old);
  277. }
  278. #endif
  279. if (qout->segcnt > 6) {
  280. rc = -EOPNOTSUPP;
  281. goto out_free;
  282. }
  283. if (qout->segcnt == 1) {
  284. seg->vm_segtype = qout->range[0].start & 0xff;
  285. } else {
  286. /* multi-part segment. only one type supported here:
  287. - all parts are contiguous
  288. - all parts are either EW or EN type
  289. - maximum 6 parts allowed */
  290. unsigned long start = qout->segstart >> PAGE_SHIFT;
  291. for (i=0; i<qout->segcnt; i++) {
  292. if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
  293. ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
  294. rc = -EOPNOTSUPP;
  295. goto out_free;
  296. }
  297. if (start != qout->range[i].start >> PAGE_SHIFT) {
  298. rc = -EOPNOTSUPP;
  299. goto out_free;
  300. }
  301. start = (qout->range[i].end >> PAGE_SHIFT) + 1;
  302. }
  303. seg->vm_segtype = SEG_TYPE_EWEN;
  304. }
  305. /* analyze diag output and update seg */
  306. seg->start_addr = qout->segstart;
  307. seg->end = qout->segend;
  308. memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
  309. seg->segcnt = qout->segcnt;
  310. rc = 0;
  311. out_free:
  312. kfree(qin);
  313. kfree(qout);
  314. return rc;
  315. }
  316. /*
  317. * get info about a segment
  318. * possible return values:
  319. * -ENOSYS : we are not running on VM
  320. * -EIO : could not perform query diagnose
  321. * -ENOENT : no such segment
  322. * -EOPNOTSUPP: multi-part segment cannot be used with linux
  323. * -ENOMEM : out of memory
  324. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  325. */
  326. int
  327. segment_type (char* name)
  328. {
  329. int rc;
  330. struct dcss_segment seg;
  331. if (!MACHINE_IS_VM)
  332. return -ENOSYS;
  333. dcss_mkname(name, seg.dcss_name);
  334. rc = query_segment_type (&seg);
  335. if (rc < 0)
  336. return rc;
  337. return seg.vm_segtype;
  338. }
  339. /*
  340. * check if segment collides with other segments that are currently loaded
  341. * returns 1 if this is the case, 0 if no collision was found
  342. */
  343. static int
  344. segment_overlaps_others (struct dcss_segment *seg)
  345. {
  346. struct list_head *l;
  347. struct dcss_segment *tmp;
  348. BUG_ON(!mutex_is_locked(&dcss_lock));
  349. list_for_each(l, &dcss_list) {
  350. tmp = list_entry(l, struct dcss_segment, list);
  351. if ((tmp->start_addr >> 20) > (seg->end >> 20))
  352. continue;
  353. if ((tmp->end >> 20) < (seg->start_addr >> 20))
  354. continue;
  355. if (seg == tmp)
  356. continue;
  357. return 1;
  358. }
  359. return 0;
  360. }
  361. /*
  362. * real segment loading function, called from segment_load
  363. */
  364. static int
  365. __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
  366. {
  367. unsigned long start_addr, end_addr, dummy;
  368. struct dcss_segment *seg;
  369. int rc, diag_cc;
  370. start_addr = end_addr = 0;
  371. seg = kmalloc(sizeof(*seg), GFP_KERNEL | GFP_DMA);
  372. if (seg == NULL) {
  373. rc = -ENOMEM;
  374. goto out;
  375. }
  376. dcss_mkname (name, seg->dcss_name);
  377. rc = query_segment_type (seg);
  378. if (rc < 0)
  379. goto out_free;
  380. if (loadshr_scode == DCSS_LOADSHRX) {
  381. if (segment_overlaps_others(seg)) {
  382. rc = -EBUSY;
  383. goto out_free;
  384. }
  385. }
  386. rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  387. if (rc)
  388. goto out_free;
  389. seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  390. if (seg->res == NULL) {
  391. rc = -ENOMEM;
  392. goto out_shared;
  393. }
  394. seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  395. seg->res->start = seg->start_addr;
  396. seg->res->end = seg->end;
  397. memcpy(&seg->res_name, seg->dcss_name, 8);
  398. EBCASC(seg->res_name, 8);
  399. seg->res_name[8] = '\0';
  400. strncat(seg->res_name, " (DCSS)", 7);
  401. seg->res->name = seg->res_name;
  402. rc = seg->vm_segtype;
  403. if (rc == SEG_TYPE_SC ||
  404. ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
  405. seg->res->flags |= IORESOURCE_READONLY;
  406. if (request_resource(&iomem_resource, seg->res)) {
  407. rc = -EBUSY;
  408. kfree(seg->res);
  409. goto out_shared;
  410. }
  411. if (do_nonshared)
  412. diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
  413. &start_addr, &end_addr);
  414. else
  415. diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
  416. &start_addr, &end_addr);
  417. if (diag_cc < 0) {
  418. dcss_diag(&purgeseg_scode, seg->dcss_name,
  419. &dummy, &dummy);
  420. rc = diag_cc;
  421. goto out_resource;
  422. }
  423. if (diag_cc > 1) {
  424. pr_warning("Loading DCSS %s failed with rc=%ld\n", name,
  425. end_addr);
  426. rc = dcss_diag_translate_rc(end_addr);
  427. dcss_diag(&purgeseg_scode, seg->dcss_name,
  428. &dummy, &dummy);
  429. goto out_resource;
  430. }
  431. seg->start_addr = start_addr;
  432. seg->end = end_addr;
  433. seg->do_nonshared = do_nonshared;
  434. atomic_set(&seg->ref_count, 1);
  435. list_add(&seg->list, &dcss_list);
  436. *addr = seg->start_addr;
  437. *end = seg->end;
  438. if (do_nonshared)
  439. pr_info("DCSS %s of range %p to %p and type %s loaded as "
  440. "exclusive-writable\n", name, (void*) seg->start_addr,
  441. (void*) seg->end, segtype_string[seg->vm_segtype]);
  442. else {
  443. pr_info("DCSS %s of range %p to %p and type %s loaded in "
  444. "shared access mode\n", name, (void*) seg->start_addr,
  445. (void*) seg->end, segtype_string[seg->vm_segtype]);
  446. }
  447. goto out;
  448. out_resource:
  449. release_resource(seg->res);
  450. kfree(seg->res);
  451. out_shared:
  452. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  453. out_free:
  454. kfree(seg);
  455. out:
  456. return rc;
  457. }
  458. /*
  459. * this function loads a DCSS segment
  460. * name : name of the DCSS
  461. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  462. * 1 indicates that the dcss should be exclusive for this linux image
  463. * addr : will be filled with start address of the segment
  464. * end : will be filled with end address of the segment
  465. * return values:
  466. * -ENOSYS : we are not running on VM
  467. * -EIO : could not perform query or load diagnose
  468. * -ENOENT : no such segment
  469. * -EOPNOTSUPP: multi-part segment cannot be used with linux
  470. * -ENOSPC : segment cannot be used (overlaps with storage)
  471. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  472. * -ERANGE : segment cannot be used (exceeds kernel mapping range)
  473. * -EPERM : segment is currently loaded with incompatible permissions
  474. * -ENOMEM : out of memory
  475. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  476. */
  477. int
  478. segment_load (char *name, int do_nonshared, unsigned long *addr,
  479. unsigned long *end)
  480. {
  481. struct dcss_segment *seg;
  482. int rc;
  483. if (!MACHINE_IS_VM)
  484. return -ENOSYS;
  485. mutex_lock(&dcss_lock);
  486. seg = segment_by_name (name);
  487. if (seg == NULL)
  488. rc = __segment_load (name, do_nonshared, addr, end);
  489. else {
  490. if (do_nonshared == seg->do_nonshared) {
  491. atomic_inc(&seg->ref_count);
  492. *addr = seg->start_addr;
  493. *end = seg->end;
  494. rc = seg->vm_segtype;
  495. } else {
  496. *addr = *end = 0;
  497. rc = -EPERM;
  498. }
  499. }
  500. mutex_unlock(&dcss_lock);
  501. return rc;
  502. }
  503. /*
  504. * this function modifies the shared state of a DCSS segment. note that
  505. * name : name of the DCSS
  506. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  507. * 1 indicates that the dcss should be exclusive for this linux image
  508. * return values:
  509. * -EIO : could not perform load diagnose (segment gone!)
  510. * -ENOENT : no such segment (segment gone!)
  511. * -EAGAIN : segment is in use by other exploiters, try later
  512. * -EINVAL : no segment with the given name is currently loaded - name invalid
  513. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  514. * 0 : operation succeeded
  515. */
  516. int
  517. segment_modify_shared (char *name, int do_nonshared)
  518. {
  519. struct dcss_segment *seg;
  520. unsigned long start_addr, end_addr, dummy;
  521. int rc, diag_cc;
  522. start_addr = end_addr = 0;
  523. mutex_lock(&dcss_lock);
  524. seg = segment_by_name (name);
  525. if (seg == NULL) {
  526. rc = -EINVAL;
  527. goto out_unlock;
  528. }
  529. if (do_nonshared == seg->do_nonshared) {
  530. pr_info("DCSS %s is already in the requested access "
  531. "mode\n", name);
  532. rc = 0;
  533. goto out_unlock;
  534. }
  535. if (atomic_read (&seg->ref_count) != 1) {
  536. pr_warning("DCSS %s is in use and cannot be reloaded\n",
  537. name);
  538. rc = -EAGAIN;
  539. goto out_unlock;
  540. }
  541. release_resource(seg->res);
  542. if (do_nonshared)
  543. seg->res->flags &= ~IORESOURCE_READONLY;
  544. else
  545. if (seg->vm_segtype == SEG_TYPE_SR ||
  546. seg->vm_segtype == SEG_TYPE_ER)
  547. seg->res->flags |= IORESOURCE_READONLY;
  548. if (request_resource(&iomem_resource, seg->res)) {
  549. pr_warning("DCSS %s overlaps with used memory resources "
  550. "and cannot be reloaded\n", name);
  551. rc = -EBUSY;
  552. kfree(seg->res);
  553. goto out_del_mem;
  554. }
  555. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  556. if (do_nonshared)
  557. diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
  558. &start_addr, &end_addr);
  559. else
  560. diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
  561. &start_addr, &end_addr);
  562. if (diag_cc < 0) {
  563. rc = diag_cc;
  564. goto out_del_res;
  565. }
  566. if (diag_cc > 1) {
  567. pr_warning("Reloading DCSS %s failed with rc=%ld\n", name,
  568. end_addr);
  569. rc = dcss_diag_translate_rc(end_addr);
  570. goto out_del_res;
  571. }
  572. seg->start_addr = start_addr;
  573. seg->end = end_addr;
  574. seg->do_nonshared = do_nonshared;
  575. rc = 0;
  576. goto out_unlock;
  577. out_del_res:
  578. release_resource(seg->res);
  579. kfree(seg->res);
  580. out_del_mem:
  581. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  582. list_del(&seg->list);
  583. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  584. kfree(seg);
  585. out_unlock:
  586. mutex_unlock(&dcss_lock);
  587. return rc;
  588. }
  589. /*
  590. * Decrease the use count of a DCSS segment and remove
  591. * it from the address space if nobody is using it
  592. * any longer.
  593. */
  594. void
  595. segment_unload(char *name)
  596. {
  597. unsigned long dummy;
  598. struct dcss_segment *seg;
  599. if (!MACHINE_IS_VM)
  600. return;
  601. mutex_lock(&dcss_lock);
  602. seg = segment_by_name (name);
  603. if (seg == NULL) {
  604. pr_err("Unloading unknown DCSS %s failed\n", name);
  605. goto out_unlock;
  606. }
  607. if (atomic_dec_return(&seg->ref_count) != 0)
  608. goto out_unlock;
  609. release_resource(seg->res);
  610. kfree(seg->res);
  611. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  612. list_del(&seg->list);
  613. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  614. kfree(seg);
  615. out_unlock:
  616. mutex_unlock(&dcss_lock);
  617. }
  618. /*
  619. * save segment content permanently
  620. */
  621. void
  622. segment_save(char *name)
  623. {
  624. struct dcss_segment *seg;
  625. char cmd1[160];
  626. char cmd2[80];
  627. int i, response;
  628. if (!MACHINE_IS_VM)
  629. return;
  630. mutex_lock(&dcss_lock);
  631. seg = segment_by_name (name);
  632. if (seg == NULL) {
  633. pr_err("Saving unknown DCSS %s failed\n", name);
  634. goto out;
  635. }
  636. sprintf(cmd1, "DEFSEG %s", name);
  637. for (i=0; i<seg->segcnt; i++) {
  638. sprintf(cmd1+strlen(cmd1), " %lX-%lX %s",
  639. seg->range[i].start >> PAGE_SHIFT,
  640. seg->range[i].end >> PAGE_SHIFT,
  641. segtype_string[seg->range[i].start & 0xff]);
  642. }
  643. sprintf(cmd2, "SAVESEG %s", name);
  644. response = 0;
  645. cpcmd(cmd1, NULL, 0, &response);
  646. if (response) {
  647. pr_err("Saving a DCSS failed with DEFSEG response code "
  648. "%i\n", response);
  649. goto out;
  650. }
  651. cpcmd(cmd2, NULL, 0, &response);
  652. if (response) {
  653. pr_err("Saving a DCSS failed with SAVESEG response code "
  654. "%i\n", response);
  655. goto out;
  656. }
  657. out:
  658. mutex_unlock(&dcss_lock);
  659. }
  660. /*
  661. * print appropriate error message for segment_load()/segment_type()
  662. * return code
  663. */
  664. void segment_warning(int rc, char *seg_name)
  665. {
  666. switch (rc) {
  667. case -ENOENT:
  668. pr_err("DCSS %s cannot be loaded or queried\n", seg_name);
  669. break;
  670. case -ENOSYS:
  671. pr_err("DCSS %s cannot be loaded or queried without "
  672. "z/VM\n", seg_name);
  673. break;
  674. case -EIO:
  675. pr_err("Loading or querying DCSS %s resulted in a "
  676. "hardware error\n", seg_name);
  677. break;
  678. case -EOPNOTSUPP:
  679. pr_err("DCSS %s has multiple page ranges and cannot be "
  680. "loaded or queried\n", seg_name);
  681. break;
  682. case -ENOSPC:
  683. pr_err("DCSS %s overlaps with used storage and cannot "
  684. "be loaded\n", seg_name);
  685. break;
  686. case -EBUSY:
  687. pr_err("%s needs used memory resources and cannot be "
  688. "loaded or queried\n", seg_name);
  689. break;
  690. case -EPERM:
  691. pr_err("DCSS %s is already loaded in a different access "
  692. "mode\n", seg_name);
  693. break;
  694. case -ENOMEM:
  695. pr_err("There is not enough memory to load or query "
  696. "DCSS %s\n", seg_name);
  697. break;
  698. case -ERANGE:
  699. pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
  700. "and cannot be loaded\n", seg_name, VMEM_MAX_PHYS);
  701. break;
  702. default:
  703. break;
  704. }
  705. }
  706. EXPORT_SYMBOL(segment_load);
  707. EXPORT_SYMBOL(segment_unload);
  708. EXPORT_SYMBOL(segment_save);
  709. EXPORT_SYMBOL(segment_type);
  710. EXPORT_SYMBOL(segment_modify_shared);
  711. EXPORT_SYMBOL(segment_warning);