extmem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. seg = kmalloc(sizeof(*seg), GFP_KERNEL | GFP_DMA);
  371. if (seg == NULL) {
  372. rc = -ENOMEM;
  373. goto out;
  374. }
  375. dcss_mkname (name, seg->dcss_name);
  376. rc = query_segment_type (seg);
  377. if (rc < 0)
  378. goto out_free;
  379. if (loadshr_scode == DCSS_LOADSHRX) {
  380. if (segment_overlaps_others(seg)) {
  381. rc = -EBUSY;
  382. goto out_free;
  383. }
  384. }
  385. rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  386. if (rc)
  387. goto out_free;
  388. seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  389. if (seg->res == NULL) {
  390. rc = -ENOMEM;
  391. goto out_shared;
  392. }
  393. seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  394. seg->res->start = seg->start_addr;
  395. seg->res->end = seg->end;
  396. memcpy(&seg->res_name, seg->dcss_name, 8);
  397. EBCASC(seg->res_name, 8);
  398. seg->res_name[8] = '\0';
  399. strncat(seg->res_name, " (DCSS)", 7);
  400. seg->res->name = seg->res_name;
  401. rc = seg->vm_segtype;
  402. if (rc == SEG_TYPE_SC ||
  403. ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
  404. seg->res->flags |= IORESOURCE_READONLY;
  405. if (request_resource(&iomem_resource, seg->res)) {
  406. rc = -EBUSY;
  407. kfree(seg->res);
  408. goto out_shared;
  409. }
  410. if (do_nonshared)
  411. diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
  412. &start_addr, &end_addr);
  413. else
  414. diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
  415. &start_addr, &end_addr);
  416. if (diag_cc < 0) {
  417. dcss_diag(&purgeseg_scode, seg->dcss_name,
  418. &dummy, &dummy);
  419. rc = diag_cc;
  420. goto out_resource;
  421. }
  422. if (diag_cc > 1) {
  423. pr_warning("Loading DCSS %s failed with rc=%ld\n", name,
  424. end_addr);
  425. rc = dcss_diag_translate_rc(end_addr);
  426. dcss_diag(&purgeseg_scode, seg->dcss_name,
  427. &dummy, &dummy);
  428. goto out_resource;
  429. }
  430. seg->start_addr = start_addr;
  431. seg->end = end_addr;
  432. seg->do_nonshared = do_nonshared;
  433. atomic_set(&seg->ref_count, 1);
  434. list_add(&seg->list, &dcss_list);
  435. *addr = seg->start_addr;
  436. *end = seg->end;
  437. if (do_nonshared)
  438. pr_info("DCSS %s of range %p to %p and type %s loaded as "
  439. "exclusive-writable\n", name, (void*) seg->start_addr,
  440. (void*) seg->end, segtype_string[seg->vm_segtype]);
  441. else {
  442. pr_info("DCSS %s of range %p to %p and type %s loaded in "
  443. "shared access mode\n", name, (void*) seg->start_addr,
  444. (void*) seg->end, segtype_string[seg->vm_segtype]);
  445. }
  446. goto out;
  447. out_resource:
  448. release_resource(seg->res);
  449. kfree(seg->res);
  450. out_shared:
  451. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  452. out_free:
  453. kfree(seg);
  454. out:
  455. return rc;
  456. }
  457. /*
  458. * this function loads a DCSS segment
  459. * name : name of the DCSS
  460. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  461. * 1 indicates that the dcss should be exclusive for this linux image
  462. * addr : will be filled with start address of the segment
  463. * end : will be filled with end address of the segment
  464. * return values:
  465. * -ENOSYS : we are not running on VM
  466. * -EIO : could not perform query or load diagnose
  467. * -ENOENT : no such segment
  468. * -EOPNOTSUPP: multi-part segment cannot be used with linux
  469. * -ENOSPC : segment cannot be used (overlaps with storage)
  470. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  471. * -ERANGE : segment cannot be used (exceeds kernel mapping range)
  472. * -EPERM : segment is currently loaded with incompatible permissions
  473. * -ENOMEM : out of memory
  474. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  475. */
  476. int
  477. segment_load (char *name, int do_nonshared, unsigned long *addr,
  478. unsigned long *end)
  479. {
  480. struct dcss_segment *seg;
  481. int rc;
  482. if (!MACHINE_IS_VM)
  483. return -ENOSYS;
  484. mutex_lock(&dcss_lock);
  485. seg = segment_by_name (name);
  486. if (seg == NULL)
  487. rc = __segment_load (name, do_nonshared, addr, end);
  488. else {
  489. if (do_nonshared == seg->do_nonshared) {
  490. atomic_inc(&seg->ref_count);
  491. *addr = seg->start_addr;
  492. *end = seg->end;
  493. rc = seg->vm_segtype;
  494. } else {
  495. *addr = *end = 0;
  496. rc = -EPERM;
  497. }
  498. }
  499. mutex_unlock(&dcss_lock);
  500. return rc;
  501. }
  502. /*
  503. * this function modifies the shared state of a DCSS segment. note that
  504. * name : name of the DCSS
  505. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  506. * 1 indicates that the dcss should be exclusive for this linux image
  507. * return values:
  508. * -EIO : could not perform load diagnose (segment gone!)
  509. * -ENOENT : no such segment (segment gone!)
  510. * -EAGAIN : segment is in use by other exploiters, try later
  511. * -EINVAL : no segment with the given name is currently loaded - name invalid
  512. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  513. * 0 : operation succeeded
  514. */
  515. int
  516. segment_modify_shared (char *name, int do_nonshared)
  517. {
  518. struct dcss_segment *seg;
  519. unsigned long start_addr, end_addr, dummy;
  520. int rc, diag_cc;
  521. mutex_lock(&dcss_lock);
  522. seg = segment_by_name (name);
  523. if (seg == NULL) {
  524. rc = -EINVAL;
  525. goto out_unlock;
  526. }
  527. if (do_nonshared == seg->do_nonshared) {
  528. pr_info("DCSS %s is already in the requested access "
  529. "mode\n", name);
  530. rc = 0;
  531. goto out_unlock;
  532. }
  533. if (atomic_read (&seg->ref_count) != 1) {
  534. pr_warning("DCSS %s is in use and cannot be reloaded\n",
  535. name);
  536. rc = -EAGAIN;
  537. goto out_unlock;
  538. }
  539. release_resource(seg->res);
  540. if (do_nonshared)
  541. seg->res->flags &= ~IORESOURCE_READONLY;
  542. else
  543. if (seg->vm_segtype == SEG_TYPE_SR ||
  544. seg->vm_segtype == SEG_TYPE_ER)
  545. seg->res->flags |= IORESOURCE_READONLY;
  546. if (request_resource(&iomem_resource, seg->res)) {
  547. pr_warning("DCSS %s overlaps with used memory resources "
  548. "and cannot be reloaded\n", name);
  549. rc = -EBUSY;
  550. kfree(seg->res);
  551. goto out_del_mem;
  552. }
  553. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  554. if (do_nonshared)
  555. diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
  556. &start_addr, &end_addr);
  557. else
  558. diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
  559. &start_addr, &end_addr);
  560. if (diag_cc < 0) {
  561. rc = diag_cc;
  562. goto out_del_res;
  563. }
  564. if (diag_cc > 1) {
  565. pr_warning("Reloading DCSS %s failed with rc=%ld\n", name,
  566. end_addr);
  567. rc = dcss_diag_translate_rc(end_addr);
  568. goto out_del_res;
  569. }
  570. seg->start_addr = start_addr;
  571. seg->end = end_addr;
  572. seg->do_nonshared = do_nonshared;
  573. rc = 0;
  574. goto out_unlock;
  575. out_del_res:
  576. release_resource(seg->res);
  577. kfree(seg->res);
  578. out_del_mem:
  579. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  580. list_del(&seg->list);
  581. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  582. kfree(seg);
  583. out_unlock:
  584. mutex_unlock(&dcss_lock);
  585. return rc;
  586. }
  587. /*
  588. * Decrease the use count of a DCSS segment and remove
  589. * it from the address space if nobody is using it
  590. * any longer.
  591. */
  592. void
  593. segment_unload(char *name)
  594. {
  595. unsigned long dummy;
  596. struct dcss_segment *seg;
  597. if (!MACHINE_IS_VM)
  598. return;
  599. mutex_lock(&dcss_lock);
  600. seg = segment_by_name (name);
  601. if (seg == NULL) {
  602. pr_err("Unloading unknown DCSS %s failed\n", name);
  603. goto out_unlock;
  604. }
  605. if (atomic_dec_return(&seg->ref_count) != 0)
  606. goto out_unlock;
  607. release_resource(seg->res);
  608. kfree(seg->res);
  609. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  610. list_del(&seg->list);
  611. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  612. kfree(seg);
  613. out_unlock:
  614. mutex_unlock(&dcss_lock);
  615. }
  616. /*
  617. * save segment content permanently
  618. */
  619. void
  620. segment_save(char *name)
  621. {
  622. struct dcss_segment *seg;
  623. int startpfn = 0;
  624. int endpfn = 0;
  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. startpfn = seg->start_addr >> PAGE_SHIFT;
  637. endpfn = (seg->end) >> PAGE_SHIFT;
  638. sprintf(cmd1, "DEFSEG %s", name);
  639. for (i=0; i<seg->segcnt; i++) {
  640. sprintf(cmd1+strlen(cmd1), " %lX-%lX %s",
  641. seg->range[i].start >> PAGE_SHIFT,
  642. seg->range[i].end >> PAGE_SHIFT,
  643. segtype_string[seg->range[i].start & 0xff]);
  644. }
  645. sprintf(cmd2, "SAVESEG %s", name);
  646. response = 0;
  647. cpcmd(cmd1, NULL, 0, &response);
  648. if (response) {
  649. pr_err("Saving a DCSS failed with DEFSEG response code "
  650. "%i\n", response);
  651. goto out;
  652. }
  653. cpcmd(cmd2, NULL, 0, &response);
  654. if (response) {
  655. pr_err("Saving a DCSS failed with SAVESEG response code "
  656. "%i\n", response);
  657. goto out;
  658. }
  659. out:
  660. mutex_unlock(&dcss_lock);
  661. }
  662. /*
  663. * print appropriate error message for segment_load()/segment_type()
  664. * return code
  665. */
  666. void segment_warning(int rc, char *seg_name)
  667. {
  668. switch (rc) {
  669. case -ENOENT:
  670. pr_err("DCSS %s cannot be loaded or queried\n", seg_name);
  671. break;
  672. case -ENOSYS:
  673. pr_err("DCSS %s cannot be loaded or queried without "
  674. "z/VM\n", seg_name);
  675. break;
  676. case -EIO:
  677. pr_err("Loading or querying DCSS %s resulted in a "
  678. "hardware error\n", seg_name);
  679. break;
  680. case -EOPNOTSUPP:
  681. pr_err("DCSS %s has multiple page ranges and cannot be "
  682. "loaded or queried\n", seg_name);
  683. break;
  684. case -ENOSPC:
  685. pr_err("DCSS %s overlaps with used storage and cannot "
  686. "be loaded\n", seg_name);
  687. break;
  688. case -EBUSY:
  689. pr_err("%s needs used memory resources and cannot be "
  690. "loaded or queried\n", seg_name);
  691. break;
  692. case -EPERM:
  693. pr_err("DCSS %s is already loaded in a different access "
  694. "mode\n", seg_name);
  695. break;
  696. case -ENOMEM:
  697. pr_err("There is not enough memory to load or query "
  698. "DCSS %s\n", seg_name);
  699. break;
  700. case -ERANGE:
  701. pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
  702. "and cannot be loaded\n", seg_name, VMEM_MAX_PHYS);
  703. break;
  704. default:
  705. break;
  706. }
  707. }
  708. EXPORT_SYMBOL(segment_load);
  709. EXPORT_SYMBOL(segment_unload);
  710. EXPORT_SYMBOL(segment_save);
  711. EXPORT_SYMBOL(segment_type);
  712. EXPORT_SYMBOL(segment_modify_shared);
  713. EXPORT_SYMBOL(segment_warning);