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_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. struct qin64 *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
  229. struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
  230. int diag_cc, rc, i;
  231. unsigned long dummy, vmrc;
  232. if ((qin == NULL) || (qout == NULL)) {
  233. rc = -ENOMEM;
  234. goto out_free;
  235. }
  236. /* initialize diag input parameters */
  237. qin->qopcode = DCSS_FINDSEGA;
  238. qin->qoutptr = (unsigned long) qout;
  239. qin->qoutlen = sizeof(struct qout64);
  240. memcpy (qin->qname, seg->dcss_name, 8);
  241. diag_cc = dcss_diag(&segext_scode, qin, &dummy, &vmrc);
  242. if (diag_cc < 0) {
  243. rc = diag_cc;
  244. goto out_free;
  245. }
  246. if (diag_cc > 1) {
  247. pr_warning("Querying a DCSS type failed with rc=%ld\n", vmrc);
  248. rc = dcss_diag_translate_rc (vmrc);
  249. goto out_free;
  250. }
  251. #ifdef CONFIG_64BIT
  252. /* Only old format of output area of Diagnose x'64' is supported,
  253. copy data for the new format. */
  254. if (segext_scode == DCSS_SEGEXT) {
  255. struct qout64_old *qout_old;
  256. qout_old = kzalloc(sizeof(struct qout64_old), GFP_DMA);
  257. if (qout_old == NULL) {
  258. rc = -ENOMEM;
  259. goto out_free;
  260. }
  261. memcpy(qout_old, qout, sizeof(struct qout64_old));
  262. qout->segstart = (unsigned long) qout_old->segstart;
  263. qout->segend = (unsigned long) qout_old->segend;
  264. qout->segcnt = qout_old->segcnt;
  265. qout->segrcnt = qout_old->segrcnt;
  266. if (qout->segcnt > 6)
  267. qout->segrcnt = 6;
  268. for (i = 0; i < qout->segrcnt; i++) {
  269. qout->range[i].start =
  270. (unsigned long) qout_old->range[i].start;
  271. qout->range[i].end =
  272. (unsigned long) qout_old->range[i].end;
  273. }
  274. kfree(qout_old);
  275. }
  276. #endif
  277. if (qout->segcnt > 6) {
  278. rc = -ENOTSUPP;
  279. goto out_free;
  280. }
  281. if (qout->segcnt == 1) {
  282. seg->vm_segtype = qout->range[0].start & 0xff;
  283. } else {
  284. /* multi-part segment. only one type supported here:
  285. - all parts are contiguous
  286. - all parts are either EW or EN type
  287. - maximum 6 parts allowed */
  288. unsigned long start = qout->segstart >> PAGE_SHIFT;
  289. for (i=0; i<qout->segcnt; i++) {
  290. if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
  291. ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
  292. rc = -ENOTSUPP;
  293. goto out_free;
  294. }
  295. if (start != qout->range[i].start >> PAGE_SHIFT) {
  296. rc = -ENOTSUPP;
  297. goto out_free;
  298. }
  299. start = (qout->range[i].end >> PAGE_SHIFT) + 1;
  300. }
  301. seg->vm_segtype = SEG_TYPE_EWEN;
  302. }
  303. /* analyze diag output and update seg */
  304. seg->start_addr = qout->segstart;
  305. seg->end = qout->segend;
  306. memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
  307. seg->segcnt = qout->segcnt;
  308. rc = 0;
  309. out_free:
  310. kfree(qin);
  311. kfree(qout);
  312. return rc;
  313. }
  314. /*
  315. * get info about a segment
  316. * possible return values:
  317. * -ENOSYS : we are not running on VM
  318. * -EIO : could not perform query diagnose
  319. * -ENOENT : no such segment
  320. * -ENOTSUPP: multi-part segment cannot be used with linux
  321. * -ENOMEM : out of memory
  322. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  323. */
  324. int
  325. segment_type (char* name)
  326. {
  327. int rc;
  328. struct dcss_segment seg;
  329. if (!MACHINE_IS_VM)
  330. return -ENOSYS;
  331. dcss_mkname(name, seg.dcss_name);
  332. rc = query_segment_type (&seg);
  333. if (rc < 0)
  334. return rc;
  335. return seg.vm_segtype;
  336. }
  337. /*
  338. * check if segment collides with other segments that are currently loaded
  339. * returns 1 if this is the case, 0 if no collision was found
  340. */
  341. static int
  342. segment_overlaps_others (struct dcss_segment *seg)
  343. {
  344. struct list_head *l;
  345. struct dcss_segment *tmp;
  346. BUG_ON(!mutex_is_locked(&dcss_lock));
  347. list_for_each(l, &dcss_list) {
  348. tmp = list_entry(l, struct dcss_segment, list);
  349. if ((tmp->start_addr >> 20) > (seg->end >> 20))
  350. continue;
  351. if ((tmp->end >> 20) < (seg->start_addr >> 20))
  352. continue;
  353. if (seg == tmp)
  354. continue;
  355. return 1;
  356. }
  357. return 0;
  358. }
  359. /*
  360. * real segment loading function, called from segment_load
  361. */
  362. static int
  363. __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
  364. {
  365. struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
  366. GFP_DMA);
  367. int rc, diag_cc;
  368. unsigned long start_addr, end_addr, dummy;
  369. if (seg == NULL) {
  370. rc = -ENOMEM;
  371. goto out;
  372. }
  373. dcss_mkname (name, seg->dcss_name);
  374. rc = query_segment_type (seg);
  375. if (rc < 0)
  376. goto out_free;
  377. if (loadshr_scode == DCSS_LOADSHRX) {
  378. if (segment_overlaps_others(seg)) {
  379. rc = -EBUSY;
  380. goto out_free;
  381. }
  382. }
  383. rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  384. if (rc)
  385. goto out_free;
  386. seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  387. if (seg->res == NULL) {
  388. rc = -ENOMEM;
  389. goto out_shared;
  390. }
  391. seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  392. seg->res->start = seg->start_addr;
  393. seg->res->end = seg->end;
  394. memcpy(&seg->res_name, seg->dcss_name, 8);
  395. EBCASC(seg->res_name, 8);
  396. seg->res_name[8] = '\0';
  397. strncat(seg->res_name, " (DCSS)", 7);
  398. seg->res->name = seg->res_name;
  399. rc = seg->vm_segtype;
  400. if (rc == SEG_TYPE_SC ||
  401. ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
  402. seg->res->flags |= IORESOURCE_READONLY;
  403. if (request_resource(&iomem_resource, seg->res)) {
  404. rc = -EBUSY;
  405. kfree(seg->res);
  406. goto out_shared;
  407. }
  408. if (do_nonshared)
  409. diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
  410. &start_addr, &end_addr);
  411. else
  412. diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
  413. &start_addr, &end_addr);
  414. if (diag_cc < 0) {
  415. dcss_diag(&purgeseg_scode, seg->dcss_name,
  416. &dummy, &dummy);
  417. rc = diag_cc;
  418. goto out_resource;
  419. }
  420. if (diag_cc > 1) {
  421. pr_warning("Loading DCSS %s failed with rc=%ld\n", name,
  422. end_addr);
  423. rc = dcss_diag_translate_rc(end_addr);
  424. dcss_diag(&purgeseg_scode, seg->dcss_name,
  425. &dummy, &dummy);
  426. goto out_resource;
  427. }
  428. seg->start_addr = start_addr;
  429. seg->end = end_addr;
  430. seg->do_nonshared = do_nonshared;
  431. atomic_set(&seg->ref_count, 1);
  432. list_add(&seg->list, &dcss_list);
  433. *addr = seg->start_addr;
  434. *end = seg->end;
  435. if (do_nonshared)
  436. pr_info("DCSS %s of range %p to %p and type %s loaded as "
  437. "exclusive-writable\n", name, (void*) seg->start_addr,
  438. (void*) seg->end, segtype_string[seg->vm_segtype]);
  439. else {
  440. pr_info("DCSS %s of range %p to %p and type %s loaded in "
  441. "shared access mode\n", name, (void*) seg->start_addr,
  442. (void*) seg->end, segtype_string[seg->vm_segtype]);
  443. }
  444. goto out;
  445. out_resource:
  446. release_resource(seg->res);
  447. kfree(seg->res);
  448. out_shared:
  449. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  450. out_free:
  451. kfree(seg);
  452. out:
  453. return rc;
  454. }
  455. /*
  456. * this function loads a DCSS segment
  457. * name : name of the DCSS
  458. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  459. * 1 indicates that the dcss should be exclusive for this linux image
  460. * addr : will be filled with start address of the segment
  461. * end : will be filled with end address of the segment
  462. * return values:
  463. * -ENOSYS : we are not running on VM
  464. * -EIO : could not perform query or load diagnose
  465. * -ENOENT : no such segment
  466. * -ENOTSUPP: multi-part segment cannot be used with linux
  467. * -ENOSPC : segment cannot be used (overlaps with storage)
  468. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  469. * -ERANGE : segment cannot be used (exceeds kernel mapping range)
  470. * -EPERM : segment is currently loaded with incompatible permissions
  471. * -ENOMEM : out of memory
  472. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  473. */
  474. int
  475. segment_load (char *name, int do_nonshared, unsigned long *addr,
  476. unsigned long *end)
  477. {
  478. struct dcss_segment *seg;
  479. int rc;
  480. if (!MACHINE_IS_VM)
  481. return -ENOSYS;
  482. mutex_lock(&dcss_lock);
  483. seg = segment_by_name (name);
  484. if (seg == NULL)
  485. rc = __segment_load (name, do_nonshared, addr, end);
  486. else {
  487. if (do_nonshared == seg->do_nonshared) {
  488. atomic_inc(&seg->ref_count);
  489. *addr = seg->start_addr;
  490. *end = seg->end;
  491. rc = seg->vm_segtype;
  492. } else {
  493. *addr = *end = 0;
  494. rc = -EPERM;
  495. }
  496. }
  497. mutex_unlock(&dcss_lock);
  498. return rc;
  499. }
  500. /*
  501. * this function modifies the shared state of a DCSS segment. note that
  502. * name : name of the DCSS
  503. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  504. * 1 indicates that the dcss should be exclusive for this linux image
  505. * return values:
  506. * -EIO : could not perform load diagnose (segment gone!)
  507. * -ENOENT : no such segment (segment gone!)
  508. * -EAGAIN : segment is in use by other exploiters, try later
  509. * -EINVAL : no segment with the given name is currently loaded - name invalid
  510. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  511. * 0 : operation succeeded
  512. */
  513. int
  514. segment_modify_shared (char *name, int do_nonshared)
  515. {
  516. struct dcss_segment *seg;
  517. unsigned long start_addr, end_addr, dummy;
  518. int rc, diag_cc;
  519. mutex_lock(&dcss_lock);
  520. seg = segment_by_name (name);
  521. if (seg == NULL) {
  522. rc = -EINVAL;
  523. goto out_unlock;
  524. }
  525. if (do_nonshared == seg->do_nonshared) {
  526. pr_info("DCSS %s is already in the requested access "
  527. "mode\n", name);
  528. rc = 0;
  529. goto out_unlock;
  530. }
  531. if (atomic_read (&seg->ref_count) != 1) {
  532. pr_warning("DCSS %s is in use and cannot be reloaded\n",
  533. name);
  534. rc = -EAGAIN;
  535. goto out_unlock;
  536. }
  537. release_resource(seg->res);
  538. if (do_nonshared)
  539. seg->res->flags &= ~IORESOURCE_READONLY;
  540. else
  541. if (seg->vm_segtype == SEG_TYPE_SR ||
  542. seg->vm_segtype == SEG_TYPE_ER)
  543. seg->res->flags |= IORESOURCE_READONLY;
  544. if (request_resource(&iomem_resource, seg->res)) {
  545. pr_warning("DCSS %s overlaps with used memory resources "
  546. "and cannot be reloaded\n", name);
  547. rc = -EBUSY;
  548. kfree(seg->res);
  549. goto out_del_mem;
  550. }
  551. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  552. if (do_nonshared)
  553. diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
  554. &start_addr, &end_addr);
  555. else
  556. diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
  557. &start_addr, &end_addr);
  558. if (diag_cc < 0) {
  559. rc = diag_cc;
  560. goto out_del_res;
  561. }
  562. if (diag_cc > 1) {
  563. pr_warning("Reloading DCSS %s failed with rc=%ld\n", name,
  564. end_addr);
  565. rc = dcss_diag_translate_rc(end_addr);
  566. goto out_del_res;
  567. }
  568. seg->start_addr = start_addr;
  569. seg->end = end_addr;
  570. seg->do_nonshared = do_nonshared;
  571. rc = 0;
  572. goto out_unlock;
  573. out_del_res:
  574. release_resource(seg->res);
  575. kfree(seg->res);
  576. out_del_mem:
  577. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  578. list_del(&seg->list);
  579. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  580. kfree(seg);
  581. out_unlock:
  582. mutex_unlock(&dcss_lock);
  583. return rc;
  584. }
  585. /*
  586. * Decrease the use count of a DCSS segment and remove
  587. * it from the address space if nobody is using it
  588. * any longer.
  589. */
  590. void
  591. segment_unload(char *name)
  592. {
  593. unsigned long dummy;
  594. struct dcss_segment *seg;
  595. if (!MACHINE_IS_VM)
  596. return;
  597. mutex_lock(&dcss_lock);
  598. seg = segment_by_name (name);
  599. if (seg == NULL) {
  600. pr_err("Unloading unknown DCSS %s failed\n", name);
  601. goto out_unlock;
  602. }
  603. if (atomic_dec_return(&seg->ref_count) != 0)
  604. goto out_unlock;
  605. release_resource(seg->res);
  606. kfree(seg->res);
  607. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  608. list_del(&seg->list);
  609. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  610. kfree(seg);
  611. out_unlock:
  612. mutex_unlock(&dcss_lock);
  613. }
  614. /*
  615. * save segment content permanently
  616. */
  617. void
  618. segment_save(char *name)
  619. {
  620. struct dcss_segment *seg;
  621. int startpfn = 0;
  622. int endpfn = 0;
  623. char cmd1[160];
  624. char cmd2[80];
  625. int i, response;
  626. if (!MACHINE_IS_VM)
  627. return;
  628. mutex_lock(&dcss_lock);
  629. seg = segment_by_name (name);
  630. if (seg == NULL) {
  631. pr_err("Saving unknown DCSS %s failed\n", name);
  632. goto out;
  633. }
  634. startpfn = seg->start_addr >> PAGE_SHIFT;
  635. endpfn = (seg->end) >> PAGE_SHIFT;
  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 -ENOTSUPP:
  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);