2 * Copyright IBM Corp. 2007,2012
4 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
5 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
8 #define KMSG_COMPONENT "sclp_cmd"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/completion.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
19 #include <linux/mmzone.h>
20 #include <linux/memory.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <asm/ctl_reg.h>
24 #include <asm/chpid.h>
25 #include <asm/setup.h>
31 static void sclp_sync_callback(struct sclp_req *req, void *data)
33 struct completion *completion = data;
38 int sclp_sync_request(sclp_cmdw_t cmd, void *sccb)
40 struct completion completion;
41 struct sclp_req *request;
44 request = kzalloc(sizeof(*request), GFP_KERNEL);
47 request->command = cmd;
49 request->status = SCLP_REQ_FILLED;
50 request->callback = sclp_sync_callback;
51 request->callback_data = &completion;
52 init_completion(&completion);
54 /* Perform sclp request. */
55 rc = sclp_add_request(request);
58 wait_for_completion(&completion);
61 if (request->status != SCLP_REQ_DONE) {
62 pr_warning("sync request failed (cmd=0x%08x, "
63 "status=0x%02x)\n", cmd, request->status);
72 * CPU configuration related functions.
75 #define SCLP_CMDW_READ_CPU_INFO 0x00010001
76 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
77 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
79 struct read_cpu_info_sccb {
80 struct sccb_header header;
82 u16 offset_configured;
85 u8 reserved[4096 - 16];
86 } __attribute__((packed, aligned(PAGE_SIZE)));
88 static void sclp_fill_cpu_info(struct sclp_cpu_info *info,
89 struct read_cpu_info_sccb *sccb)
91 char *page = (char *) sccb;
93 memset(info, 0, sizeof(*info));
94 info->configured = sccb->nr_configured;
95 info->standby = sccb->nr_standby;
96 info->combined = sccb->nr_configured + sccb->nr_standby;
97 info->has_cpu_type = sclp_fac84 & 0x1;
98 memcpy(&info->cpu, page + sccb->offset_configured,
99 info->combined * sizeof(struct sclp_cpu_entry));
102 int sclp_get_cpu_info(struct sclp_cpu_info *info)
105 struct read_cpu_info_sccb *sccb;
107 if (!SCLP_HAS_CPU_INFO)
109 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
112 sccb->header.length = sizeof(*sccb);
113 rc = sclp_sync_request(SCLP_CMDW_READ_CPU_INFO, sccb);
116 if (sccb->header.response_code != 0x0010) {
117 pr_warning("readcpuinfo failed (response=0x%04x)\n",
118 sccb->header.response_code);
122 sclp_fill_cpu_info(info, sccb);
124 free_page((unsigned long) sccb);
128 struct cpu_configure_sccb {
129 struct sccb_header header;
130 } __attribute__((packed, aligned(8)));
132 static int do_cpu_configure(sclp_cmdw_t cmd)
134 struct cpu_configure_sccb *sccb;
137 if (!SCLP_HAS_CPU_RECONFIG)
140 * This is not going to cross a page boundary since we force
141 * kmalloc to have a minimum alignment of 8 bytes on s390.
143 sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
146 sccb->header.length = sizeof(*sccb);
147 rc = sclp_sync_request(cmd, sccb);
150 switch (sccb->header.response_code) {
155 pr_warning("configure cpu failed (cmd=0x%08x, "
156 "response=0x%04x)\n", cmd,
157 sccb->header.response_code);
166 int sclp_cpu_configure(u8 cpu)
168 return do_cpu_configure(SCLP_CMDW_CONFIGURE_CPU | cpu << 8);
171 int sclp_cpu_deconfigure(u8 cpu)
173 return do_cpu_configure(SCLP_CMDW_DECONFIGURE_CPU | cpu << 8);
176 #ifdef CONFIG_MEMORY_HOTPLUG
178 static DEFINE_MUTEX(sclp_mem_mutex);
179 static LIST_HEAD(sclp_mem_list);
180 static u8 sclp_max_storage_id;
181 static unsigned long sclp_storage_ids[256 / BITS_PER_LONG];
182 static int sclp_mem_state_changed;
184 struct memory_increment {
185 struct list_head list;
190 struct assign_storage_sccb {
191 struct sccb_header header;
195 int arch_get_memory_phys_device(unsigned long start_pfn)
199 return PFN_PHYS(start_pfn) >> ilog2(sclp_rzm);
202 static unsigned long long rn2addr(u16 rn)
204 return (unsigned long long) (rn - 1) * sclp_rzm;
207 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
209 struct assign_storage_sccb *sccb;
212 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
215 sccb->header.length = PAGE_SIZE;
217 rc = sclp_sync_request(cmd, sccb);
220 switch (sccb->header.response_code) {
225 pr_warning("assign storage failed (cmd=0x%08x, "
226 "response=0x%04x, rn=0x%04x)\n", cmd,
227 sccb->header.response_code, rn);
232 free_page((unsigned long) sccb);
236 static int sclp_assign_storage(u16 rn)
238 unsigned long long start;
241 rc = do_assign_storage(0x000d0001, rn);
245 storage_key_init_range(start, start + sclp_rzm);
249 static int sclp_unassign_storage(u16 rn)
251 return do_assign_storage(0x000c0001, rn);
254 struct attach_storage_sccb {
255 struct sccb_header header;
262 static int sclp_attach_storage(u8 id)
264 struct attach_storage_sccb *sccb;
268 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
271 sccb->header.length = PAGE_SIZE;
272 rc = sclp_sync_request(0x00080001 | id << 8, sccb);
275 switch (sccb->header.response_code) {
277 set_bit(id, sclp_storage_ids);
278 for (i = 0; i < sccb->assigned; i++) {
279 if (sccb->entries[i])
280 sclp_unassign_storage(sccb->entries[i] >> 16);
288 free_page((unsigned long) sccb);
292 static int sclp_mem_change_state(unsigned long start, unsigned long size,
295 struct memory_increment *incr;
296 unsigned long long istart;
299 list_for_each_entry(incr, &sclp_mem_list, list) {
300 istart = rn2addr(incr->rn);
301 if (start + size - 1 < istart)
303 if (start > istart + sclp_rzm - 1)
306 rc |= sclp_assign_storage(incr->rn);
308 sclp_unassign_storage(incr->rn);
310 return rc ? -EIO : 0;
313 static int sclp_mem_notifier(struct notifier_block *nb,
314 unsigned long action, void *data)
316 unsigned long start, size;
317 struct memory_notify *arg;
322 start = arg->start_pfn << PAGE_SHIFT;
323 size = arg->nr_pages << PAGE_SHIFT;
324 mutex_lock(&sclp_mem_mutex);
325 for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
326 sclp_attach_storage(id);
329 case MEM_GOING_OFFLINE:
330 case MEM_CANCEL_OFFLINE:
332 case MEM_GOING_ONLINE:
333 rc = sclp_mem_change_state(start, size, 1);
335 case MEM_CANCEL_ONLINE:
336 sclp_mem_change_state(start, size, 0);
339 sclp_mem_change_state(start, size, 0);
346 sclp_mem_state_changed = 1;
347 mutex_unlock(&sclp_mem_mutex);
348 return rc ? NOTIFY_BAD : NOTIFY_OK;
351 static struct notifier_block sclp_mem_nb = {
352 .notifier_call = sclp_mem_notifier,
355 static void __init add_memory_merged(u16 rn)
357 static u16 first_rn, num;
358 unsigned long long start, size;
360 if (rn && first_rn && (first_rn + num == rn)) {
366 start = rn2addr(first_rn);
367 size = (unsigned long long) num * sclp_rzm;
368 if (start >= VMEM_MAX_PHYS)
370 if (start + size > VMEM_MAX_PHYS)
371 size = VMEM_MAX_PHYS - start;
372 if (memory_end_set && (start >= memory_end))
374 if (memory_end_set && (start + size > memory_end))
375 size = memory_end - start;
376 add_memory(0, start, size);
382 static void __init sclp_add_standby_memory(void)
384 struct memory_increment *incr;
386 list_for_each_entry(incr, &sclp_mem_list, list)
388 add_memory_merged(incr->rn);
389 add_memory_merged(0);
392 static void __init insert_increment(u16 rn, int standby, int assigned)
394 struct memory_increment *incr, *new_incr;
395 struct list_head *prev;
398 new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
402 new_incr->standby = standby;
404 prev = &sclp_mem_list;
405 list_for_each_entry(incr, &sclp_mem_list, list) {
406 if (assigned && incr->rn > rn)
408 if (!assigned && incr->rn - last_rn > 1)
414 new_incr->rn = last_rn + 1;
415 if (new_incr->rn > sclp_rnmax) {
419 list_add(&new_incr->list, prev);
422 static int sclp_mem_freeze(struct device *dev)
424 if (!sclp_mem_state_changed)
426 pr_err("Memory hotplug state changed, suspend refused.\n");
430 struct read_storage_sccb {
431 struct sccb_header header;
439 static const struct dev_pm_ops sclp_mem_pm_ops = {
440 .freeze = sclp_mem_freeze,
443 static struct platform_driver sclp_mem_pdrv = {
446 .pm = &sclp_mem_pm_ops,
450 static int __init sclp_detect_standby_memory(void)
452 struct platform_device *sclp_pdev;
453 struct read_storage_sccb *sccb;
454 int i, id, assigned, rc;
456 if (OLDMEM_BASE) /* No standby memory in kdump mode */
458 if ((sclp_facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
461 sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
465 for (id = 0; id <= sclp_max_storage_id; id++) {
466 memset(sccb, 0, PAGE_SIZE);
467 sccb->header.length = PAGE_SIZE;
468 rc = sclp_sync_request(0x00040001 | id << 8, sccb);
471 switch (sccb->header.response_code) {
473 set_bit(id, sclp_storage_ids);
474 for (i = 0; i < sccb->assigned; i++) {
475 if (!sccb->entries[i])
478 insert_increment(sccb->entries[i] >> 16, 0, 1);
484 for (i = 0; i < sccb->assigned; i++) {
485 if (!sccb->entries[i])
488 insert_increment(sccb->entries[i] >> 16, 1, 1);
496 sclp_max_storage_id = sccb->max_id;
498 if (rc || list_empty(&sclp_mem_list))
500 for (i = 1; i <= sclp_rnmax - assigned; i++)
501 insert_increment(0, 1, 0);
502 rc = register_memory_notifier(&sclp_mem_nb);
505 rc = platform_driver_register(&sclp_mem_pdrv);
508 sclp_pdev = platform_device_register_simple("sclp_mem", -1, NULL, 0);
509 rc = PTR_RET(sclp_pdev);
512 sclp_add_standby_memory();
515 platform_driver_unregister(&sclp_mem_pdrv);
517 free_page((unsigned long) sccb);
520 __initcall(sclp_detect_standby_memory);
522 #endif /* CONFIG_MEMORY_HOTPLUG */
525 * PCI I/O adapter configuration related functions.
527 #define SCLP_CMDW_CONFIGURE_PCI 0x001a0001
528 #define SCLP_CMDW_DECONFIGURE_PCI 0x001b0001
530 #define SCLP_RECONFIG_PCI_ATPYE 2
532 struct pci_cfg_sccb {
533 struct sccb_header header;
534 u8 atype; /* adapter type */
537 u32 aid; /* adapter identifier */
540 static int do_pci_configure(sclp_cmdw_t cmd, u32 fid)
542 struct pci_cfg_sccb *sccb;
545 if (!SCLP_HAS_PCI_RECONFIG)
548 sccb = (struct pci_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
552 sccb->header.length = PAGE_SIZE;
553 sccb->atype = SCLP_RECONFIG_PCI_ATPYE;
555 rc = sclp_sync_request(cmd, sccb);
558 switch (sccb->header.response_code) {
563 pr_warn("configure PCI I/O adapter failed: cmd=0x%08x response=0x%04x\n",
564 cmd, sccb->header.response_code);
569 free_page((unsigned long) sccb);
573 int sclp_pci_configure(u32 fid)
575 return do_pci_configure(SCLP_CMDW_CONFIGURE_PCI, fid);
577 EXPORT_SYMBOL(sclp_pci_configure);
579 int sclp_pci_deconfigure(u32 fid)
581 return do_pci_configure(SCLP_CMDW_DECONFIGURE_PCI, fid);
583 EXPORT_SYMBOL(sclp_pci_deconfigure);
586 * Channel path configuration related functions.
589 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
590 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
591 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
593 struct chp_cfg_sccb {
594 struct sccb_header header;
598 } __attribute__((packed));
600 static int do_chp_configure(sclp_cmdw_t cmd)
602 struct chp_cfg_sccb *sccb;
605 if (!SCLP_HAS_CHP_RECONFIG)
608 sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
611 sccb->header.length = sizeof(*sccb);
612 rc = sclp_sync_request(cmd, sccb);
615 switch (sccb->header.response_code) {
622 pr_warning("configure channel-path failed "
623 "(cmd=0x%08x, response=0x%04x)\n", cmd,
624 sccb->header.response_code);
629 free_page((unsigned long) sccb);
634 * sclp_chp_configure - perform configure channel-path sclp command
635 * @chpid: channel-path ID
637 * Perform configure channel-path command sclp command for specified chpid.
638 * Return 0 after command successfully finished, non-zero otherwise.
640 int sclp_chp_configure(struct chp_id chpid)
642 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
646 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
647 * @chpid: channel-path ID
649 * Perform deconfigure channel-path command sclp command for specified chpid
650 * and wait for completion. On success return 0. Return non-zero otherwise.
652 int sclp_chp_deconfigure(struct chp_id chpid)
654 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
657 struct chp_info_sccb {
658 struct sccb_header header;
659 u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
660 u8 standby[SCLP_CHP_INFO_MASK_SIZE];
661 u8 configured[SCLP_CHP_INFO_MASK_SIZE];
665 } __attribute__((packed));
668 * sclp_chp_read_info - perform read channel-path information sclp command
669 * @info: resulting channel-path information data
671 * Perform read channel-path information sclp command and wait for completion.
672 * On success, store channel-path information in @info and return 0. Return
673 * non-zero otherwise.
675 int sclp_chp_read_info(struct sclp_chp_info *info)
677 struct chp_info_sccb *sccb;
680 if (!SCLP_HAS_CHP_INFO)
683 sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
686 sccb->header.length = sizeof(*sccb);
687 rc = sclp_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
690 if (sccb->header.response_code != 0x0010) {
691 pr_warning("read channel-path info failed "
692 "(response=0x%04x)\n", sccb->header.response_code);
696 memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
697 memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
698 memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
700 free_page((unsigned long) sccb);
704 bool sclp_has_sprp(void)
706 return !!(sclp_fac84 & 0x2);