extmem.c 19 KB

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