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/diag.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. 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. struct qin64 {
  62. char qopcode;
  63. char rsrv1[3];
  64. char qrcode;
  65. char rsrv2[3];
  66. char qname[8];
  67. unsigned int qoutptr;
  68. short int qoutlen;
  69. };
  70. struct dcss_segment {
  71. struct list_head list;
  72. char dcss_name[8];
  73. char res_name[15];
  74. unsigned long start_addr;
  75. unsigned long end;
  76. atomic_t ref_count;
  77. int do_nonshared;
  78. unsigned int vm_segtype;
  79. struct qrange range[6];
  80. int segcnt;
  81. struct resource *res;
  82. };
  83. static DEFINE_MUTEX(dcss_lock);
  84. static LIST_HEAD(dcss_list);
  85. static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
  86. "EW/EN-MIXED" };
  87. static int loadshr_scode, loadnsr_scode;
  88. static int segext_scode, purgeseg_scode;
  89. static int scode_set;
  90. /* set correct Diag x'64' subcodes. */
  91. static int
  92. dcss_set_subcodes(void)
  93. {
  94. char *name = kmalloc(8 * sizeof(char), GFP_KERNEL | GFP_DMA);
  95. unsigned long rx, ry;
  96. int rc;
  97. if (name == NULL)
  98. return -ENOMEM;
  99. rx = (unsigned long) name;
  100. ry = DCSS_FINDSEGX;
  101. strcpy(name, "dummy");
  102. diag_stat_inc(DIAG_STAT_X064);
  103. asm volatile(
  104. " diag %0,%1,0x64\n"
  105. "0: ipm %2\n"
  106. " srl %2,28\n"
  107. " j 2f\n"
  108. "1: la %2,3\n"
  109. "2:\n"
  110. EX_TABLE(0b, 1b)
  111. : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
  112. kfree(name);
  113. /* Diag x'64' new subcodes are supported, set to new subcodes */
  114. if (rc != 3) {
  115. loadshr_scode = DCSS_LOADSHRX;
  116. loadnsr_scode = DCSS_LOADNSRX;
  117. purgeseg_scode = DCSS_PURGESEG;
  118. segext_scode = DCSS_SEGEXTX;
  119. return 0;
  120. }
  121. /* Diag x'64' new subcodes are not supported, set to old subcodes */
  122. loadshr_scode = DCSS_LOADNOLY;
  123. loadnsr_scode = DCSS_LOADNSR;
  124. purgeseg_scode = DCSS_PURGESEG;
  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. diag_stat_inc(DIAG_STAT_X064);
  185. if (*func > DCSS_SEGEXT)
  186. asm volatile(
  187. " diag %0,%1,0x64\n"
  188. " ipm %2\n"
  189. " srl %2,28\n"
  190. : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
  191. /* 31-bit Diag x'64' old subcode, switch to 31-bit addressing mode */
  192. else
  193. asm volatile(
  194. " sam31\n"
  195. " diag %0,%1,0x64\n"
  196. " sam64\n"
  197. " ipm %2\n"
  198. " srl %2,28\n"
  199. : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
  200. *ret1 = rx;
  201. *ret2 = ry;
  202. return rc;
  203. }
  204. static inline int
  205. dcss_diag_translate_rc (int vm_rc) {
  206. if (vm_rc == 44)
  207. return -ENOENT;
  208. return -EIO;
  209. }
  210. /* do a diag to get info about a segment.
  211. * fills start_address, end and vm_segtype fields
  212. */
  213. static int
  214. query_segment_type (struct dcss_segment *seg)
  215. {
  216. unsigned long dummy, vmrc;
  217. int diag_cc, rc, i;
  218. struct qout64 *qout;
  219. struct qin64 *qin;
  220. qin = kmalloc(sizeof(*qin), GFP_KERNEL | GFP_DMA);
  221. qout = kmalloc(sizeof(*qout), GFP_KERNEL | GFP_DMA);
  222. if ((qin == NULL) || (qout == NULL)) {
  223. rc = -ENOMEM;
  224. goto out_free;
  225. }
  226. /* initialize diag input parameters */
  227. qin->qopcode = DCSS_FINDSEGA;
  228. qin->qoutptr = (unsigned long) qout;
  229. qin->qoutlen = sizeof(struct qout64);
  230. memcpy (qin->qname, seg->dcss_name, 8);
  231. diag_cc = dcss_diag(&segext_scode, qin, &dummy, &vmrc);
  232. if (diag_cc < 0) {
  233. rc = diag_cc;
  234. goto out_free;
  235. }
  236. if (diag_cc > 1) {
  237. pr_warn("Querying a DCSS type failed with rc=%ld\n", vmrc);
  238. rc = dcss_diag_translate_rc (vmrc);
  239. goto out_free;
  240. }
  241. /* Only old format of output area of Diagnose x'64' is supported,
  242. copy data for the new format. */
  243. if (segext_scode == DCSS_SEGEXT) {
  244. struct qout64_old *qout_old;
  245. qout_old = kzalloc(sizeof(*qout_old), GFP_KERNEL | GFP_DMA);
  246. if (qout_old == NULL) {
  247. rc = -ENOMEM;
  248. goto out_free;
  249. }
  250. memcpy(qout_old, qout, sizeof(struct qout64_old));
  251. qout->segstart = (unsigned long) qout_old->segstart;
  252. qout->segend = (unsigned long) qout_old->segend;
  253. qout->segcnt = qout_old->segcnt;
  254. qout->segrcnt = qout_old->segrcnt;
  255. if (qout->segcnt > 6)
  256. qout->segrcnt = 6;
  257. for (i = 0; i < qout->segrcnt; i++) {
  258. qout->range[i].start =
  259. (unsigned long) qout_old->range[i].start;
  260. qout->range[i].end =
  261. (unsigned long) qout_old->range[i].end;
  262. }
  263. kfree(qout_old);
  264. }
  265. if (qout->segcnt > 6) {
  266. rc = -EOPNOTSUPP;
  267. goto out_free;
  268. }
  269. if (qout->segcnt == 1) {
  270. seg->vm_segtype = qout->range[0].start & 0xff;
  271. } else {
  272. /* multi-part segment. only one type supported here:
  273. - all parts are contiguous
  274. - all parts are either EW or EN type
  275. - maximum 6 parts allowed */
  276. unsigned long start = qout->segstart >> PAGE_SHIFT;
  277. for (i=0; i<qout->segcnt; i++) {
  278. if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
  279. ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
  280. rc = -EOPNOTSUPP;
  281. goto out_free;
  282. }
  283. if (start != qout->range[i].start >> PAGE_SHIFT) {
  284. rc = -EOPNOTSUPP;
  285. goto out_free;
  286. }
  287. start = (qout->range[i].end >> PAGE_SHIFT) + 1;
  288. }
  289. seg->vm_segtype = SEG_TYPE_EWEN;
  290. }
  291. /* analyze diag output and update seg */
  292. seg->start_addr = qout->segstart;
  293. seg->end = qout->segend;
  294. memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
  295. seg->segcnt = qout->segcnt;
  296. rc = 0;
  297. out_free:
  298. kfree(qin);
  299. kfree(qout);
  300. return rc;
  301. }
  302. /*
  303. * get info about a segment
  304. * possible return values:
  305. * -ENOSYS : we are not running on VM
  306. * -EIO : could not perform query diagnose
  307. * -ENOENT : no such segment
  308. * -EOPNOTSUPP: multi-part segment cannot be used with linux
  309. * -ENOMEM : out of memory
  310. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  311. */
  312. int
  313. segment_type (char* name)
  314. {
  315. int rc;
  316. struct dcss_segment seg;
  317. if (!MACHINE_IS_VM)
  318. return -ENOSYS;
  319. dcss_mkname(name, seg.dcss_name);
  320. rc = query_segment_type (&seg);
  321. if (rc < 0)
  322. return rc;
  323. return seg.vm_segtype;
  324. }
  325. /*
  326. * check if segment collides with other segments that are currently loaded
  327. * returns 1 if this is the case, 0 if no collision was found
  328. */
  329. static int
  330. segment_overlaps_others (struct dcss_segment *seg)
  331. {
  332. struct list_head *l;
  333. struct dcss_segment *tmp;
  334. BUG_ON(!mutex_is_locked(&dcss_lock));
  335. list_for_each(l, &dcss_list) {
  336. tmp = list_entry(l, struct dcss_segment, list);
  337. if ((tmp->start_addr >> 20) > (seg->end >> 20))
  338. continue;
  339. if ((tmp->end >> 20) < (seg->start_addr >> 20))
  340. continue;
  341. if (seg == tmp)
  342. continue;
  343. return 1;
  344. }
  345. return 0;
  346. }
  347. /*
  348. * real segment loading function, called from segment_load
  349. */
  350. static int
  351. __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
  352. {
  353. unsigned long start_addr, end_addr, dummy;
  354. struct dcss_segment *seg;
  355. int rc, diag_cc;
  356. start_addr = end_addr = 0;
  357. seg = kmalloc(sizeof(*seg), GFP_KERNEL | GFP_DMA);
  358. if (seg == NULL) {
  359. rc = -ENOMEM;
  360. goto out;
  361. }
  362. dcss_mkname (name, seg->dcss_name);
  363. rc = query_segment_type (seg);
  364. if (rc < 0)
  365. goto out_free;
  366. if (loadshr_scode == DCSS_LOADSHRX) {
  367. if (segment_overlaps_others(seg)) {
  368. rc = -EBUSY;
  369. goto out_free;
  370. }
  371. }
  372. rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  373. if (rc)
  374. goto out_free;
  375. seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  376. if (seg->res == NULL) {
  377. rc = -ENOMEM;
  378. goto out_shared;
  379. }
  380. seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  381. seg->res->start = seg->start_addr;
  382. seg->res->end = seg->end;
  383. memcpy(&seg->res_name, seg->dcss_name, 8);
  384. EBCASC(seg->res_name, 8);
  385. seg->res_name[8] = '\0';
  386. strncat(seg->res_name, " (DCSS)", 7);
  387. seg->res->name = seg->res_name;
  388. rc = seg->vm_segtype;
  389. if (rc == SEG_TYPE_SC ||
  390. ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
  391. seg->res->flags |= IORESOURCE_READONLY;
  392. if (request_resource(&iomem_resource, seg->res)) {
  393. rc = -EBUSY;
  394. kfree(seg->res);
  395. goto out_shared;
  396. }
  397. if (do_nonshared)
  398. diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
  399. &start_addr, &end_addr);
  400. else
  401. diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
  402. &start_addr, &end_addr);
  403. if (diag_cc < 0) {
  404. dcss_diag(&purgeseg_scode, seg->dcss_name,
  405. &dummy, &dummy);
  406. rc = diag_cc;
  407. goto out_resource;
  408. }
  409. if (diag_cc > 1) {
  410. pr_warn("Loading DCSS %s failed with rc=%ld\n", name, 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_warn("DCSS %s is in use and cannot be reloaded\n", name);
  522. rc = -EAGAIN;
  523. goto out_unlock;
  524. }
  525. release_resource(seg->res);
  526. if (do_nonshared)
  527. seg->res->flags &= ~IORESOURCE_READONLY;
  528. else
  529. if (seg->vm_segtype == SEG_TYPE_SR ||
  530. seg->vm_segtype == SEG_TYPE_ER)
  531. seg->res->flags |= IORESOURCE_READONLY;
  532. if (request_resource(&iomem_resource, seg->res)) {
  533. pr_warn("DCSS %s overlaps with used memory resources and cannot be reloaded\n",
  534. name);
  535. rc = -EBUSY;
  536. kfree(seg->res);
  537. goto out_del_mem;
  538. }
  539. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  540. if (do_nonshared)
  541. diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
  542. &start_addr, &end_addr);
  543. else
  544. diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
  545. &start_addr, &end_addr);
  546. if (diag_cc < 0) {
  547. rc = diag_cc;
  548. goto out_del_res;
  549. }
  550. if (diag_cc > 1) {
  551. pr_warn("Reloading DCSS %s failed with rc=%ld\n",
  552. name, end_addr);
  553. rc = dcss_diag_translate_rc(end_addr);
  554. goto out_del_res;
  555. }
  556. seg->start_addr = start_addr;
  557. seg->end = end_addr;
  558. seg->do_nonshared = do_nonshared;
  559. rc = 0;
  560. goto out_unlock;
  561. out_del_res:
  562. release_resource(seg->res);
  563. kfree(seg->res);
  564. out_del_mem:
  565. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  566. list_del(&seg->list);
  567. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  568. kfree(seg);
  569. out_unlock:
  570. mutex_unlock(&dcss_lock);
  571. return rc;
  572. }
  573. /*
  574. * Decrease the use count of a DCSS segment and remove
  575. * it from the address space if nobody is using it
  576. * any longer.
  577. */
  578. void
  579. segment_unload(char *name)
  580. {
  581. unsigned long dummy;
  582. struct dcss_segment *seg;
  583. if (!MACHINE_IS_VM)
  584. return;
  585. mutex_lock(&dcss_lock);
  586. seg = segment_by_name (name);
  587. if (seg == NULL) {
  588. pr_err("Unloading unknown DCSS %s failed\n", name);
  589. goto out_unlock;
  590. }
  591. if (atomic_dec_return(&seg->ref_count) != 0)
  592. goto out_unlock;
  593. release_resource(seg->res);
  594. kfree(seg->res);
  595. vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
  596. list_del(&seg->list);
  597. dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
  598. kfree(seg);
  599. out_unlock:
  600. mutex_unlock(&dcss_lock);
  601. }
  602. /*
  603. * save segment content permanently
  604. */
  605. void
  606. segment_save(char *name)
  607. {
  608. struct dcss_segment *seg;
  609. char cmd1[160];
  610. char cmd2[80];
  611. int i, response;
  612. if (!MACHINE_IS_VM)
  613. return;
  614. mutex_lock(&dcss_lock);
  615. seg = segment_by_name (name);
  616. if (seg == NULL) {
  617. pr_err("Saving unknown DCSS %s failed\n", name);
  618. goto out;
  619. }
  620. sprintf(cmd1, "DEFSEG %s", name);
  621. for (i=0; i<seg->segcnt; i++) {
  622. sprintf(cmd1+strlen(cmd1), " %lX-%lX %s",
  623. seg->range[i].start >> PAGE_SHIFT,
  624. seg->range[i].end >> PAGE_SHIFT,
  625. segtype_string[seg->range[i].start & 0xff]);
  626. }
  627. sprintf(cmd2, "SAVESEG %s", name);
  628. response = 0;
  629. cpcmd(cmd1, NULL, 0, &response);
  630. if (response) {
  631. pr_err("Saving a DCSS failed with DEFSEG response code "
  632. "%i\n", response);
  633. goto out;
  634. }
  635. cpcmd(cmd2, NULL, 0, &response);
  636. if (response) {
  637. pr_err("Saving a DCSS failed with SAVESEG response code "
  638. "%i\n", response);
  639. goto out;
  640. }
  641. out:
  642. mutex_unlock(&dcss_lock);
  643. }
  644. /*
  645. * print appropriate error message for segment_load()/segment_type()
  646. * return code
  647. */
  648. void segment_warning(int rc, char *seg_name)
  649. {
  650. switch (rc) {
  651. case -ENOENT:
  652. pr_err("DCSS %s cannot be loaded or queried\n", seg_name);
  653. break;
  654. case -ENOSYS:
  655. pr_err("DCSS %s cannot be loaded or queried without "
  656. "z/VM\n", seg_name);
  657. break;
  658. case -EIO:
  659. pr_err("Loading or querying DCSS %s resulted in a "
  660. "hardware error\n", seg_name);
  661. break;
  662. case -EOPNOTSUPP:
  663. pr_err("DCSS %s has multiple page ranges and cannot be "
  664. "loaded or queried\n", seg_name);
  665. break;
  666. case -ENOSPC:
  667. pr_err("DCSS %s overlaps with used storage and cannot "
  668. "be loaded\n", seg_name);
  669. break;
  670. case -EBUSY:
  671. pr_err("%s needs used memory resources and cannot be "
  672. "loaded or queried\n", seg_name);
  673. break;
  674. case -EPERM:
  675. pr_err("DCSS %s is already loaded in a different access "
  676. "mode\n", seg_name);
  677. break;
  678. case -ENOMEM:
  679. pr_err("There is not enough memory to load or query "
  680. "DCSS %s\n", seg_name);
  681. break;
  682. case -ERANGE:
  683. pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
  684. "and cannot be loaded\n", seg_name, VMEM_MAX_PHYS);
  685. break;
  686. default:
  687. break;
  688. }
  689. }
  690. EXPORT_SYMBOL(segment_load);
  691. EXPORT_SYMBOL(segment_unload);
  692. EXPORT_SYMBOL(segment_save);
  693. EXPORT_SYMBOL(segment_type);
  694. EXPORT_SYMBOL(segment_modify_shared);
  695. EXPORT_SYMBOL(segment_warning);