sclp_cmd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. sccb->header.function_code = 0x40;
  223. rc = sclp_sync_request_timeout(0x00080001 | id << 8, sccb,
  224. SCLP_QUEUE_INTERVAL);
  225. if (rc)
  226. goto out;
  227. switch (sccb->header.response_code) {
  228. case 0x0020:
  229. set_bit(id, sclp_storage_ids);
  230. for (i = 0; i < sccb->assigned; i++) {
  231. if (sccb->entries[i])
  232. sclp_unassign_storage(sccb->entries[i] >> 16);
  233. }
  234. break;
  235. default:
  236. rc = -EIO;
  237. break;
  238. }
  239. out:
  240. free_page((unsigned long) sccb);
  241. return rc;
  242. }
  243. static int sclp_mem_change_state(unsigned long start, unsigned long size,
  244. int online)
  245. {
  246. struct memory_increment *incr;
  247. unsigned long long istart;
  248. int rc = 0;
  249. list_for_each_entry(incr, &sclp_mem_list, list) {
  250. istart = rn2addr(incr->rn);
  251. if (start + size - 1 < istart)
  252. break;
  253. if (start > istart + sclp.rzm - 1)
  254. continue;
  255. if (online)
  256. rc |= sclp_assign_storage(incr->rn);
  257. else
  258. sclp_unassign_storage(incr->rn);
  259. if (rc == 0)
  260. incr->standby = online ? 0 : 1;
  261. }
  262. return rc ? -EIO : 0;
  263. }
  264. static bool contains_standby_increment(unsigned long start, unsigned long end)
  265. {
  266. struct memory_increment *incr;
  267. unsigned long istart;
  268. list_for_each_entry(incr, &sclp_mem_list, list) {
  269. istart = rn2addr(incr->rn);
  270. if (end - 1 < istart)
  271. continue;
  272. if (start > istart + sclp.rzm - 1)
  273. continue;
  274. if (incr->standby)
  275. return true;
  276. }
  277. return false;
  278. }
  279. static int sclp_mem_notifier(struct notifier_block *nb,
  280. unsigned long action, void *data)
  281. {
  282. unsigned long start, size;
  283. struct memory_notify *arg;
  284. unsigned char id;
  285. int rc = 0;
  286. arg = data;
  287. start = arg->start_pfn << PAGE_SHIFT;
  288. size = arg->nr_pages << PAGE_SHIFT;
  289. mutex_lock(&sclp_mem_mutex);
  290. for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
  291. sclp_attach_storage(id);
  292. switch (action) {
  293. case MEM_GOING_OFFLINE:
  294. /*
  295. * We do not allow to set memory blocks offline that contain
  296. * standby memory. This is done to simplify the "memory online"
  297. * case.
  298. */
  299. if (contains_standby_increment(start, start + size))
  300. rc = -EPERM;
  301. break;
  302. case MEM_ONLINE:
  303. case MEM_CANCEL_OFFLINE:
  304. break;
  305. case MEM_GOING_ONLINE:
  306. rc = sclp_mem_change_state(start, size, 1);
  307. break;
  308. case MEM_CANCEL_ONLINE:
  309. sclp_mem_change_state(start, size, 0);
  310. break;
  311. case MEM_OFFLINE:
  312. sclp_mem_change_state(start, size, 0);
  313. break;
  314. default:
  315. rc = -EINVAL;
  316. break;
  317. }
  318. if (!rc)
  319. sclp_mem_state_changed = 1;
  320. mutex_unlock(&sclp_mem_mutex);
  321. return rc ? NOTIFY_BAD : NOTIFY_OK;
  322. }
  323. static struct notifier_block sclp_mem_nb = {
  324. .notifier_call = sclp_mem_notifier,
  325. };
  326. static void __init align_to_block_size(unsigned long long *start,
  327. unsigned long long *size,
  328. unsigned long long alignment)
  329. {
  330. unsigned long long start_align, size_align;
  331. start_align = roundup(*start, alignment);
  332. size_align = rounddown(*start + *size, alignment) - start_align;
  333. pr_info("Standby memory at 0x%llx (%lluM of %lluM usable)\n",
  334. *start, size_align >> 20, *size >> 20);
  335. *start = start_align;
  336. *size = size_align;
  337. }
  338. static void __init add_memory_merged(u16 rn)
  339. {
  340. unsigned long long start, size, addr, block_size;
  341. static u16 first_rn, num;
  342. if (rn && first_rn && (first_rn + num == rn)) {
  343. num++;
  344. return;
  345. }
  346. if (!first_rn)
  347. goto skip_add;
  348. start = rn2addr(first_rn);
  349. size = (unsigned long long) num * sclp.rzm;
  350. if (start >= VMEM_MAX_PHYS)
  351. goto skip_add;
  352. if (start + size > VMEM_MAX_PHYS)
  353. size = VMEM_MAX_PHYS - start;
  354. if (memory_end_set && (start >= memory_end))
  355. goto skip_add;
  356. if (memory_end_set && (start + size > memory_end))
  357. size = memory_end - start;
  358. block_size = memory_block_size_bytes();
  359. align_to_block_size(&start, &size, block_size);
  360. if (!size)
  361. goto skip_add;
  362. for (addr = start; addr < start + size; addr += block_size)
  363. add_memory(numa_pfn_to_nid(PFN_DOWN(addr)), addr, block_size);
  364. skip_add:
  365. first_rn = rn;
  366. num = 1;
  367. }
  368. static void __init sclp_add_standby_memory(void)
  369. {
  370. struct memory_increment *incr;
  371. list_for_each_entry(incr, &sclp_mem_list, list)
  372. if (incr->standby)
  373. add_memory_merged(incr->rn);
  374. add_memory_merged(0);
  375. }
  376. static void __init insert_increment(u16 rn, int standby, int assigned)
  377. {
  378. struct memory_increment *incr, *new_incr;
  379. struct list_head *prev;
  380. u16 last_rn;
  381. new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
  382. if (!new_incr)
  383. return;
  384. new_incr->rn = rn;
  385. new_incr->standby = standby;
  386. last_rn = 0;
  387. prev = &sclp_mem_list;
  388. list_for_each_entry(incr, &sclp_mem_list, list) {
  389. if (assigned && incr->rn > rn)
  390. break;
  391. if (!assigned && incr->rn - last_rn > 1)
  392. break;
  393. last_rn = incr->rn;
  394. prev = &incr->list;
  395. }
  396. if (!assigned)
  397. new_incr->rn = last_rn + 1;
  398. if (new_incr->rn > sclp.rnmax) {
  399. kfree(new_incr);
  400. return;
  401. }
  402. list_add(&new_incr->list, prev);
  403. }
  404. static int sclp_mem_freeze(struct device *dev)
  405. {
  406. if (!sclp_mem_state_changed)
  407. return 0;
  408. pr_err("Memory hotplug state changed, suspend refused.\n");
  409. return -EPERM;
  410. }
  411. struct read_storage_sccb {
  412. struct sccb_header header;
  413. u16 max_id;
  414. u16 assigned;
  415. u16 standby;
  416. u16 :16;
  417. u32 entries[0];
  418. } __packed;
  419. static const struct dev_pm_ops sclp_mem_pm_ops = {
  420. .freeze = sclp_mem_freeze,
  421. };
  422. static struct platform_driver sclp_mem_pdrv = {
  423. .driver = {
  424. .name = "sclp_mem",
  425. .pm = &sclp_mem_pm_ops,
  426. },
  427. };
  428. static int __init sclp_detect_standby_memory(void)
  429. {
  430. struct platform_device *sclp_pdev;
  431. struct read_storage_sccb *sccb;
  432. int i, id, assigned, rc;
  433. if (OLDMEM_BASE) /* No standby memory in kdump mode */
  434. return 0;
  435. if ((sclp.facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
  436. return 0;
  437. rc = -ENOMEM;
  438. sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
  439. if (!sccb)
  440. goto out;
  441. assigned = 0;
  442. for (id = 0; id <= sclp_max_storage_id; id++) {
  443. memset(sccb, 0, PAGE_SIZE);
  444. sccb->header.length = PAGE_SIZE;
  445. rc = sclp_sync_request(0x00040001 | id << 8, sccb);
  446. if (rc)
  447. goto out;
  448. switch (sccb->header.response_code) {
  449. case 0x0010:
  450. set_bit(id, sclp_storage_ids);
  451. for (i = 0; i < sccb->assigned; i++) {
  452. if (!sccb->entries[i])
  453. continue;
  454. assigned++;
  455. insert_increment(sccb->entries[i] >> 16, 0, 1);
  456. }
  457. break;
  458. case 0x0310:
  459. break;
  460. case 0x0410:
  461. for (i = 0; i < sccb->assigned; i++) {
  462. if (!sccb->entries[i])
  463. continue;
  464. assigned++;
  465. insert_increment(sccb->entries[i] >> 16, 1, 1);
  466. }
  467. break;
  468. default:
  469. rc = -EIO;
  470. break;
  471. }
  472. if (!rc)
  473. sclp_max_storage_id = sccb->max_id;
  474. }
  475. if (rc || list_empty(&sclp_mem_list))
  476. goto out;
  477. for (i = 1; i <= sclp.rnmax - assigned; i++)
  478. insert_increment(0, 1, 0);
  479. rc = register_memory_notifier(&sclp_mem_nb);
  480. if (rc)
  481. goto out;
  482. rc = platform_driver_register(&sclp_mem_pdrv);
  483. if (rc)
  484. goto out;
  485. sclp_pdev = platform_device_register_simple("sclp_mem", -1, NULL, 0);
  486. rc = PTR_ERR_OR_ZERO(sclp_pdev);
  487. if (rc)
  488. goto out_driver;
  489. sclp_add_standby_memory();
  490. goto out;
  491. out_driver:
  492. platform_driver_unregister(&sclp_mem_pdrv);
  493. out:
  494. free_page((unsigned long) sccb);
  495. return rc;
  496. }
  497. __initcall(sclp_detect_standby_memory);
  498. #endif /* CONFIG_MEMORY_HOTPLUG */
  499. /*
  500. * Channel path configuration related functions.
  501. */
  502. #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
  503. #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
  504. #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
  505. struct chp_cfg_sccb {
  506. struct sccb_header header;
  507. u8 ccm;
  508. u8 reserved[6];
  509. u8 cssid;
  510. } __attribute__((packed));
  511. static int do_chp_configure(sclp_cmdw_t cmd)
  512. {
  513. struct chp_cfg_sccb *sccb;
  514. int rc;
  515. if (!SCLP_HAS_CHP_RECONFIG)
  516. return -EOPNOTSUPP;
  517. /* Prepare sccb. */
  518. sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  519. if (!sccb)
  520. return -ENOMEM;
  521. sccb->header.length = sizeof(*sccb);
  522. rc = sclp_sync_request(cmd, sccb);
  523. if (rc)
  524. goto out;
  525. switch (sccb->header.response_code) {
  526. case 0x0020:
  527. case 0x0120:
  528. case 0x0440:
  529. case 0x0450:
  530. break;
  531. default:
  532. pr_warn("configure channel-path failed (cmd=0x%08x, response=0x%04x)\n",
  533. cmd, sccb->header.response_code);
  534. rc = -EIO;
  535. break;
  536. }
  537. out:
  538. free_page((unsigned long) sccb);
  539. return rc;
  540. }
  541. /**
  542. * sclp_chp_configure - perform configure channel-path sclp command
  543. * @chpid: channel-path ID
  544. *
  545. * Perform configure channel-path command sclp command for specified chpid.
  546. * Return 0 after command successfully finished, non-zero otherwise.
  547. */
  548. int sclp_chp_configure(struct chp_id chpid)
  549. {
  550. return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
  551. }
  552. /**
  553. * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
  554. * @chpid: channel-path ID
  555. *
  556. * Perform deconfigure channel-path command sclp command for specified chpid
  557. * and wait for completion. On success return 0. Return non-zero otherwise.
  558. */
  559. int sclp_chp_deconfigure(struct chp_id chpid)
  560. {
  561. return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
  562. }
  563. struct chp_info_sccb {
  564. struct sccb_header header;
  565. u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
  566. u8 standby[SCLP_CHP_INFO_MASK_SIZE];
  567. u8 configured[SCLP_CHP_INFO_MASK_SIZE];
  568. u8 ccm;
  569. u8 reserved[6];
  570. u8 cssid;
  571. } __attribute__((packed));
  572. /**
  573. * sclp_chp_read_info - perform read channel-path information sclp command
  574. * @info: resulting channel-path information data
  575. *
  576. * Perform read channel-path information sclp command and wait for completion.
  577. * On success, store channel-path information in @info and return 0. Return
  578. * non-zero otherwise.
  579. */
  580. int sclp_chp_read_info(struct sclp_chp_info *info)
  581. {
  582. struct chp_info_sccb *sccb;
  583. int rc;
  584. if (!SCLP_HAS_CHP_INFO)
  585. return -EOPNOTSUPP;
  586. /* Prepare sccb. */
  587. sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  588. if (!sccb)
  589. return -ENOMEM;
  590. sccb->header.length = sizeof(*sccb);
  591. rc = sclp_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
  592. if (rc)
  593. goto out;
  594. if (sccb->header.response_code != 0x0010) {
  595. pr_warn("read channel-path info failed (response=0x%04x)\n",
  596. sccb->header.response_code);
  597. rc = -EIO;
  598. goto out;
  599. }
  600. memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
  601. memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
  602. memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
  603. out:
  604. free_page((unsigned long) sccb);
  605. return rc;
  606. }