sclp_cmd.c 15 KB

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