extmem.c 18 KB

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