sclp_cmd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * Copyright IBM Corp. 2007,2012
  3. *
  4. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
  5. * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
  6. */
  7. #define KMSG_COMPONENT "sclp_cmd"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <linux/completion.h>
  10. #include <linux/init.h>
  11. #include <linux/errno.h>
  12. #include <linux/err.h>
  13. #include <linux/export.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include <linux/mm.h>
  17. #include <linux/mmzone.h>
  18. #include <linux/memory.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <asm/ctl_reg.h>
  22. #include <asm/chpid.h>
  23. #include <asm/setup.h>
  24. #include <asm/page.h>
  25. #include <asm/sclp.h>
  26. #include <asm/numa.h>
  27. #include "sclp.h"
  28. static void sclp_sync_callback(struct sclp_req *req, void *data)
  29. {
  30. struct completion *completion = data;
  31. complete(completion);
  32. }
  33. int sclp_sync_request(sclp_cmdw_t cmd, void *sccb)
  34. {
  35. return sclp_sync_request_timeout(cmd, sccb, 0);
  36. }
  37. int sclp_sync_request_timeout(sclp_cmdw_t cmd, void *sccb, int timeout)
  38. {
  39. struct completion completion;
  40. struct sclp_req *request;
  41. int rc;
  42. request = kzalloc(sizeof(*request), GFP_KERNEL);
  43. if (!request)
  44. return -ENOMEM;
  45. if (timeout)
  46. request->queue_timeout = timeout;
  47. request->command = cmd;
  48. request->sccb = sccb;
  49. request->status = SCLP_REQ_FILLED;
  50. request->callback = sclp_sync_callback;
  51. request->callback_data = &completion;
  52. init_completion(&completion);
  53. /* Perform sclp request. */
  54. rc = sclp_add_request(request);
  55. if (rc)
  56. goto out;
  57. wait_for_completion(&completion);
  58. /* Check response. */
  59. if (request->status != SCLP_REQ_DONE) {
  60. pr_warn("sync request failed (cmd=0x%08x, status=0x%02x)\n",
  61. cmd, request->status);
  62. rc = -EIO;
  63. }
  64. out:
  65. kfree(request);
  66. return rc;
  67. }
  68. /*
  69. * CPU configuration related functions.
  70. */
  71. #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
  72. #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
  73. int _sclp_get_core_info(struct sclp_core_info *info)
  74. {
  75. int rc;
  76. struct read_cpu_info_sccb *sccb;
  77. if (!SCLP_HAS_CPU_INFO)
  78. return -EOPNOTSUPP;
  79. sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  80. if (!sccb)
  81. return -ENOMEM;
  82. sccb->header.length = sizeof(*sccb);
  83. rc = sclp_sync_request_timeout(SCLP_CMDW_READ_CPU_INFO, sccb,
  84. SCLP_QUEUE_INTERVAL);
  85. if (rc)
  86. goto out;
  87. if (sccb->header.response_code != 0x0010) {
  88. pr_warn("readcpuinfo failed (response=0x%04x)\n",
  89. sccb->header.response_code);
  90. rc = -EIO;
  91. goto out;
  92. }
  93. sclp_fill_core_info(info, sccb);
  94. out:
  95. free_page((unsigned long) sccb);
  96. return rc;
  97. }
  98. struct cpu_configure_sccb {
  99. struct sccb_header header;
  100. } __attribute__((packed, aligned(8)));
  101. static int do_core_configure(sclp_cmdw_t cmd)
  102. {
  103. struct cpu_configure_sccb *sccb;
  104. int rc;
  105. if (!SCLP_HAS_CPU_RECONFIG)
  106. return -EOPNOTSUPP;
  107. /*
  108. * This is not going to cross a page boundary since we force
  109. * kmalloc to have a minimum alignment of 8 bytes on s390.
  110. */
  111. sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
  112. if (!sccb)
  113. return -ENOMEM;
  114. sccb->header.length = sizeof(*sccb);
  115. rc = sclp_sync_request_timeout(cmd, sccb, SCLP_QUEUE_INTERVAL);
  116. if (rc)
  117. goto out;
  118. switch (sccb->header.response_code) {
  119. case 0x0020:
  120. case 0x0120:
  121. break;
  122. default:
  123. pr_warn("configure cpu failed (cmd=0x%08x, response=0x%04x)\n",
  124. cmd, sccb->header.response_code);
  125. rc = -EIO;
  126. break;
  127. }
  128. out:
  129. kfree(sccb);
  130. return rc;
  131. }
  132. int sclp_core_configure(u8 core)
  133. {
  134. return do_core_configure(SCLP_CMDW_CONFIGURE_CPU | core << 8);
  135. }
  136. int sclp_core_deconfigure(u8 core)
  137. {
  138. return do_core_configure(SCLP_CMDW_DECONFIGURE_CPU | core << 8);
  139. }
  140. #ifdef CONFIG_MEMORY_HOTPLUG
  141. static DEFINE_MUTEX(sclp_mem_mutex);
  142. static LIST_HEAD(sclp_mem_list);
  143. static u8 sclp_max_storage_id;
  144. static DECLARE_BITMAP(sclp_storage_ids, 256);
  145. static int sclp_mem_state_changed;
  146. struct memory_increment {
  147. struct list_head list;
  148. u16 rn;
  149. int standby;
  150. };
  151. struct assign_storage_sccb {
  152. struct sccb_header header;
  153. u16 rn;
  154. } __packed;
  155. int arch_get_memory_phys_device(unsigned long start_pfn)
  156. {
  157. if (!sclp.rzm)
  158. return 0;
  159. return PFN_PHYS(start_pfn) >> ilog2(sclp.rzm);
  160. }
  161. static unsigned long long rn2addr(u16 rn)
  162. {
  163. return (unsigned long long) (rn - 1) * sclp.rzm;
  164. }
  165. static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
  166. {
  167. struct assign_storage_sccb *sccb;
  168. int rc;
  169. sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  170. if (!sccb)
  171. return -ENOMEM;
  172. sccb->header.length = PAGE_SIZE;
  173. sccb->rn = rn;
  174. rc = sclp_sync_request_timeout(cmd, sccb, SCLP_QUEUE_INTERVAL);
  175. if (rc)
  176. goto out;
  177. switch (sccb->header.response_code) {
  178. case 0x0020:
  179. case 0x0120:
  180. break;
  181. default:
  182. pr_warn("assign storage failed (cmd=0x%08x, response=0x%04x, rn=0x%04x)\n",
  183. cmd, sccb->header.response_code, rn);
  184. rc = -EIO;
  185. break;
  186. }
  187. out:
  188. free_page((unsigned long) sccb);
  189. return rc;
  190. }
  191. static int sclp_assign_storage(u16 rn)
  192. {
  193. unsigned long long start;
  194. int rc;
  195. rc = do_assign_storage(0x000d0001, rn);
  196. if (rc)
  197. return rc;
  198. start = rn2addr(rn);
  199. storage_key_init_range(start, start + sclp.rzm);
  200. return 0;
  201. }
  202. static int sclp_unassign_storage(u16 rn)
  203. {
  204. return do_assign_storage(0x000c0001, rn);
  205. }
  206. struct attach_storage_sccb {
  207. struct sccb_header header;
  208. u16 :16;
  209. u16 assigned;
  210. u32 :32;
  211. u32 entries[0];
  212. } __packed;
  213. static int sclp_attach_storage(u8 id)
  214. {
  215. struct attach_storage_sccb *sccb;
  216. int rc;
  217. int i;
  218. sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  219. if (!sccb)
  220. return -ENOMEM;
  221. sccb->header.length = PAGE_SIZE;
  222. rc = sclp_sync_request_timeout(0x00080001 | id << 8, sccb,
  223. SCLP_QUEUE_INTERVAL);
  224. if (rc)
  225. goto out;
  226. switch (sccb->header.response_code) {
  227. case 0x0020:
  228. set_bit(id, sclp_storage_ids);
  229. for (i = 0; i < sccb->assigned; i++) {
  230. if (sccb->entries[i])
  231. sclp_unassign_storage(sccb->entries[i] >> 16);
  232. }
  233. break;
  234. default:
  235. rc = -EIO;
  236. break;
  237. }
  238. out:
  239. free_page((unsigned long) sccb);
  240. return rc;
  241. }
  242. static int sclp_mem_change_state(unsigned long start, unsigned long size,
  243. int online)
  244. {
  245. struct memory_increment *incr;
  246. unsigned long long istart;
  247. int rc = 0;
  248. list_for_each_entry(incr, &sclp_mem_list, list) {
  249. istart = rn2addr(incr->rn);
  250. if (start + size - 1 < istart)
  251. break;
  252. if (start > istart + sclp.rzm - 1)
  253. continue;
  254. if (online)
  255. rc |= sclp_assign_storage(incr->rn);
  256. else
  257. sclp_unassign_storage(incr->rn);
  258. if (rc == 0)
  259. incr->standby = online ? 0 : 1;
  260. }
  261. return rc ? -EIO : 0;
  262. }
  263. static bool contains_standby_increment(unsigned long start, unsigned long end)
  264. {
  265. struct memory_increment *incr;
  266. unsigned long istart;
  267. list_for_each_entry(incr, &sclp_mem_list, list) {
  268. istart = rn2addr(incr->rn);
  269. if (end - 1 < istart)
  270. continue;
  271. if (start > istart + sclp.rzm - 1)
  272. continue;
  273. if (incr->standby)
  274. return true;
  275. }
  276. return false;
  277. }
  278. static int sclp_mem_notifier(struct notifier_block *nb,
  279. unsigned long action, void *data)
  280. {
  281. unsigned long start, size;
  282. struct memory_notify *arg;
  283. unsigned char id;
  284. int rc = 0;
  285. arg = data;
  286. start = arg->start_pfn << PAGE_SHIFT;
  287. size = arg->nr_pages << PAGE_SHIFT;
  288. mutex_lock(&sclp_mem_mutex);
  289. for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
  290. sclp_attach_storage(id);
  291. switch (action) {
  292. case MEM_GOING_OFFLINE:
  293. /*
  294. * We do not allow to set memory blocks offline that contain
  295. * standby memory. This is done to simplify the "memory online"
  296. * case.
  297. */
  298. if (contains_standby_increment(start, start + size))
  299. rc = -EPERM;
  300. break;
  301. case MEM_ONLINE:
  302. case MEM_CANCEL_OFFLINE:
  303. break;
  304. case MEM_GOING_ONLINE:
  305. rc = sclp_mem_change_state(start, size, 1);
  306. break;
  307. case MEM_CANCEL_ONLINE:
  308. sclp_mem_change_state(start, size, 0);
  309. break;
  310. case MEM_OFFLINE:
  311. sclp_mem_change_state(start, size, 0);
  312. break;
  313. default:
  314. rc = -EINVAL;
  315. break;
  316. }
  317. if (!rc)
  318. sclp_mem_state_changed = 1;
  319. mutex_unlock(&sclp_mem_mutex);
  320. return rc ? NOTIFY_BAD : NOTIFY_OK;
  321. }
  322. static struct notifier_block sclp_mem_nb = {
  323. .notifier_call = sclp_mem_notifier,
  324. };
  325. static void __init align_to_block_size(unsigned long long *start,
  326. unsigned long long *size,
  327. unsigned long long alignment)
  328. {
  329. unsigned long long start_align, size_align;
  330. start_align = roundup(*start, alignment);
  331. size_align = rounddown(*start + *size, alignment) - start_align;
  332. pr_info("Standby memory at 0x%llx (%lluM of %lluM usable)\n",
  333. *start, size_align >> 20, *size >> 20);
  334. *start = start_align;
  335. *size = size_align;
  336. }
  337. static void __init add_memory_merged(u16 rn)
  338. {
  339. unsigned long long start, size, addr, block_size;
  340. static u16 first_rn, num;
  341. if (rn && first_rn && (first_rn + num == rn)) {
  342. num++;
  343. return;
  344. }
  345. if (!first_rn)
  346. goto skip_add;
  347. start = rn2addr(first_rn);
  348. size = (unsigned long long) num * sclp.rzm;
  349. if (start >= VMEM_MAX_PHYS)
  350. goto skip_add;
  351. if (start + size > VMEM_MAX_PHYS)
  352. size = VMEM_MAX_PHYS - start;
  353. if (memory_end_set && (start >= memory_end))
  354. goto skip_add;
  355. if (memory_end_set && (start + size > memory_end))
  356. size = memory_end - start;
  357. block_size = memory_block_size_bytes();
  358. align_to_block_size(&start, &size, block_size);
  359. if (!size)
  360. goto skip_add;
  361. for (addr = start; addr < start + size; addr += block_size)
  362. add_memory(numa_pfn_to_nid(PFN_DOWN(addr)), addr, block_size);
  363. skip_add:
  364. first_rn = rn;
  365. num = 1;
  366. }
  367. static void __init sclp_add_standby_memory(void)
  368. {
  369. struct memory_increment *incr;
  370. list_for_each_entry(incr, &sclp_mem_list, list)
  371. if (incr->standby)
  372. add_memory_merged(incr->rn);
  373. add_memory_merged(0);
  374. }
  375. static void __init insert_increment(u16 rn, int standby, int assigned)
  376. {
  377. struct memory_increment *incr, *new_incr;
  378. struct list_head *prev;
  379. u16 last_rn;
  380. new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
  381. if (!new_incr)
  382. return;
  383. new_incr->rn = rn;
  384. new_incr->standby = standby;
  385. last_rn = 0;
  386. prev = &sclp_mem_list;
  387. list_for_each_entry(incr, &sclp_mem_list, list) {
  388. if (assigned && incr->rn > rn)
  389. break;
  390. if (!assigned && incr->rn - last_rn > 1)
  391. break;
  392. last_rn = incr->rn;
  393. prev = &incr->list;
  394. }
  395. if (!assigned)
  396. new_incr->rn = last_rn + 1;
  397. if (new_incr->rn > sclp.rnmax) {
  398. kfree(new_incr);
  399. return;
  400. }
  401. list_add(&new_incr->list, prev);
  402. }
  403. static int sclp_mem_freeze(struct device *dev)
  404. {
  405. if (!sclp_mem_state_changed)
  406. return 0;
  407. pr_err("Memory hotplug state changed, suspend refused.\n");
  408. return -EPERM;
  409. }
  410. struct read_storage_sccb {
  411. struct sccb_header header;
  412. u16 max_id;
  413. u16 assigned;
  414. u16 standby;
  415. u16 :16;
  416. u32 entries[0];
  417. } __packed;
  418. static const struct dev_pm_ops sclp_mem_pm_ops = {
  419. .freeze = sclp_mem_freeze,
  420. };
  421. static struct platform_driver sclp_mem_pdrv = {
  422. .driver = {
  423. .name = "sclp_mem",
  424. .pm = &sclp_mem_pm_ops,
  425. },
  426. };
  427. static int __init sclp_detect_standby_memory(void)
  428. {
  429. struct platform_device *sclp_pdev;
  430. struct read_storage_sccb *sccb;
  431. int i, id, assigned, rc;
  432. if (OLDMEM_BASE) /* No standby memory in kdump mode */
  433. return 0;
  434. if ((sclp.facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
  435. return 0;
  436. rc = -ENOMEM;
  437. sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
  438. if (!sccb)
  439. goto out;
  440. assigned = 0;
  441. for (id = 0; id <= sclp_max_storage_id; id++) {
  442. memset(sccb, 0, PAGE_SIZE);
  443. sccb->header.length = PAGE_SIZE;
  444. rc = sclp_sync_request(0x00040001 | id << 8, sccb);
  445. if (rc)
  446. goto out;
  447. switch (sccb->header.response_code) {
  448. case 0x0010:
  449. set_bit(id, sclp_storage_ids);
  450. for (i = 0; i < sccb->assigned; i++) {
  451. if (!sccb->entries[i])
  452. continue;
  453. assigned++;
  454. insert_increment(sccb->entries[i] >> 16, 0, 1);
  455. }
  456. break;
  457. case 0x0310:
  458. break;
  459. case 0x0410:
  460. for (i = 0; i < sccb->assigned; i++) {
  461. if (!sccb->entries[i])
  462. continue;
  463. assigned++;
  464. insert_increment(sccb->entries[i] >> 16, 1, 1);
  465. }
  466. break;
  467. default:
  468. rc = -EIO;
  469. break;
  470. }
  471. if (!rc)
  472. sclp_max_storage_id = sccb->max_id;
  473. }
  474. if (rc || list_empty(&sclp_mem_list))
  475. goto out;
  476. for (i = 1; i <= sclp.rnmax - assigned; i++)
  477. insert_increment(0, 1, 0);
  478. rc = register_memory_notifier(&sclp_mem_nb);
  479. if (rc)
  480. goto out;
  481. rc = platform_driver_register(&sclp_mem_pdrv);
  482. if (rc)
  483. goto out;
  484. sclp_pdev = platform_device_register_simple("sclp_mem", -1, NULL, 0);
  485. rc = PTR_ERR_OR_ZERO(sclp_pdev);
  486. if (rc)
  487. goto out_driver;
  488. sclp_add_standby_memory();
  489. goto out;
  490. out_driver:
  491. platform_driver_unregister(&sclp_mem_pdrv);
  492. out:
  493. free_page((unsigned long) sccb);
  494. return rc;
  495. }
  496. __initcall(sclp_detect_standby_memory);
  497. #endif /* CONFIG_MEMORY_HOTPLUG */
  498. /*
  499. * Channel path configuration related functions.
  500. */
  501. #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
  502. #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
  503. #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
  504. struct chp_cfg_sccb {
  505. struct sccb_header header;
  506. u8 ccm;
  507. u8 reserved[6];
  508. u8 cssid;
  509. } __attribute__((packed));
  510. static int do_chp_configure(sclp_cmdw_t cmd)
  511. {
  512. struct chp_cfg_sccb *sccb;
  513. int rc;
  514. if (!SCLP_HAS_CHP_RECONFIG)
  515. return -EOPNOTSUPP;
  516. /* Prepare sccb. */
  517. sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  518. if (!sccb)
  519. return -ENOMEM;
  520. sccb->header.length = sizeof(*sccb);
  521. rc = sclp_sync_request(cmd, sccb);
  522. if (rc)
  523. goto out;
  524. switch (sccb->header.response_code) {
  525. case 0x0020:
  526. case 0x0120:
  527. case 0x0440:
  528. case 0x0450:
  529. break;
  530. default:
  531. pr_warn("configure channel-path failed (cmd=0x%08x, response=0x%04x)\n",
  532. cmd, sccb->header.response_code);
  533. rc = -EIO;
  534. break;
  535. }
  536. out:
  537. free_page((unsigned long) sccb);
  538. return rc;
  539. }
  540. /**
  541. * sclp_chp_configure - perform configure channel-path sclp command
  542. * @chpid: channel-path ID
  543. *
  544. * Perform configure channel-path command sclp command for specified chpid.
  545. * Return 0 after command successfully finished, non-zero otherwise.
  546. */
  547. int sclp_chp_configure(struct chp_id chpid)
  548. {
  549. return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
  550. }
  551. /**
  552. * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
  553. * @chpid: channel-path ID
  554. *
  555. * Perform deconfigure channel-path command sclp command for specified chpid
  556. * and wait for completion. On success return 0. Return non-zero otherwise.
  557. */
  558. int sclp_chp_deconfigure(struct chp_id chpid)
  559. {
  560. return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
  561. }
  562. struct chp_info_sccb {
  563. struct sccb_header header;
  564. u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
  565. u8 standby[SCLP_CHP_INFO_MASK_SIZE];
  566. u8 configured[SCLP_CHP_INFO_MASK_SIZE];
  567. u8 ccm;
  568. u8 reserved[6];
  569. u8 cssid;
  570. } __attribute__((packed));
  571. /**
  572. * sclp_chp_read_info - perform read channel-path information sclp command
  573. * @info: resulting channel-path information data
  574. *
  575. * Perform read channel-path information sclp command and wait for completion.
  576. * On success, store channel-path information in @info and return 0. Return
  577. * non-zero otherwise.
  578. */
  579. int sclp_chp_read_info(struct sclp_chp_info *info)
  580. {
  581. struct chp_info_sccb *sccb;
  582. int rc;
  583. if (!SCLP_HAS_CHP_INFO)
  584. return -EOPNOTSUPP;
  585. /* Prepare sccb. */
  586. sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  587. if (!sccb)
  588. return -ENOMEM;
  589. sccb->header.length = sizeof(*sccb);
  590. rc = sclp_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
  591. if (rc)
  592. goto out;
  593. if (sccb->header.response_code != 0x0010) {
  594. pr_warn("read channel-path info failed (response=0x%04x)\n",
  595. sccb->header.response_code);
  596. rc = -EIO;
  597. goto out;
  598. }
  599. memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
  600. memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
  601. memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
  602. out:
  603. free_page((unsigned long) sccb);
  604. return rc;
  605. }