sclp_cmd.c 15 KB

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