extmem.c 18 KB

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