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 return sclp_sync_request_timeout(cmd, sccb, 0);
43 int sclp_sync_request_timeout(sclp_cmdw_t cmd, void *sccb, int timeout)
45 struct completion completion;
46 struct sclp_req *request;
49 request = kzalloc(sizeof(*request), GFP_KERNEL);
53 request->queue_timeout = timeout;
54 request->command = cmd;
56 request->status = SCLP_REQ_FILLED;
57 request->callback = sclp_sync_callback;
58 request->callback_data = &completion;
59 init_completion(&completion);
61 /* Perform sclp request. */
62 rc = sclp_add_request(request);
65 wait_for_completion(&completion);
68 if (request->status != SCLP_REQ_DONE) {
69 pr_warning("sync request failed (cmd=0x%08x, "
70 "status=0x%02x)\n", cmd, request->status);
79 * CPU configuration related functions.
82 #define SCLP_CMDW_READ_CPU_INFO 0x00010001
83 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
84 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
86 struct read_cpu_info_sccb {
87 struct sccb_header header;
89 u16 offset_configured;
92 u8 reserved[4096 - 16];
93 } __attribute__((packed, aligned(PAGE_SIZE)));
95 static void sclp_fill_core_info(struct sclp_core_info *info,
96 struct read_cpu_info_sccb *sccb)
98 char *page = (char *) sccb;
100 memset(info, 0, sizeof(*info));
101 info->configured = sccb->nr_configured;
102 info->standby = sccb->nr_standby;
103 info->combined = sccb->nr_configured + sccb->nr_standby;
104 memcpy(&info->core, page + sccb->offset_configured,
105 info->combined * sizeof(struct sclp_core_entry));
108 int sclp_get_core_info(struct sclp_core_info *info)
111 struct read_cpu_info_sccb *sccb;
113 if (!SCLP_HAS_CPU_INFO)
115 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
118 sccb->header.length = sizeof(*sccb);
119 rc = sclp_sync_request_timeout(SCLP_CMDW_READ_CPU_INFO, sccb,
120 SCLP_QUEUE_INTERVAL);
123 if (sccb->header.response_code != 0x0010) {
124 pr_warning("readcpuinfo failed (response=0x%04x)\n",
125 sccb->header.response_code);
129 sclp_fill_core_info(info, sccb);
131 free_page((unsigned long) sccb);
135 struct cpu_configure_sccb {
136 struct sccb_header header;
137 } __attribute__((packed, aligned(8)));
139 static int do_core_configure(sclp_cmdw_t cmd)
141 struct cpu_configure_sccb *sccb;
144 if (!SCLP_HAS_CPU_RECONFIG)
147 * This is not going to cross a page boundary since we force
148 * kmalloc to have a minimum alignment of 8 bytes on s390.
150 sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
153 sccb->header.length = sizeof(*sccb);
154 rc = sclp_sync_request_timeout(cmd, sccb, SCLP_QUEUE_INTERVAL);
157 switch (sccb->header.response_code) {
162 pr_warning("configure cpu failed (cmd=0x%08x, "
163 "response=0x%04x)\n", cmd,
164 sccb->header.response_code);
173 int sclp_core_configure(u8 core)
175 return do_core_configure(SCLP_CMDW_CONFIGURE_CPU | core << 8);
178 int sclp_core_deconfigure(u8 core)
180 return do_core_configure(SCLP_CMDW_DECONFIGURE_CPU | core << 8);
183 #ifdef CONFIG_MEMORY_HOTPLUG
185 static DEFINE_MUTEX(sclp_mem_mutex);
186 static LIST_HEAD(sclp_mem_list);
187 static u8 sclp_max_storage_id;
188 static DECLARE_BITMAP(sclp_storage_ids, 256);
189 static int sclp_mem_state_changed;
191 struct memory_increment {
192 struct list_head list;
197 struct assign_storage_sccb {
198 struct sccb_header header;
202 int arch_get_memory_phys_device(unsigned long start_pfn)
206 return PFN_PHYS(start_pfn) >> ilog2(sclp.rzm);
209 static unsigned long long rn2addr(u16 rn)
211 return (unsigned long long) (rn - 1) * sclp.rzm;
214 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
216 struct assign_storage_sccb *sccb;
219 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
222 sccb->header.length = PAGE_SIZE;
224 rc = sclp_sync_request_timeout(cmd, sccb, SCLP_QUEUE_INTERVAL);
227 switch (sccb->header.response_code) {
232 pr_warning("assign storage failed (cmd=0x%08x, "
233 "response=0x%04x, rn=0x%04x)\n", cmd,
234 sccb->header.response_code, rn);
239 free_page((unsigned long) sccb);
243 static int sclp_assign_storage(u16 rn)
245 unsigned long long start;
248 rc = do_assign_storage(0x000d0001, rn);
252 storage_key_init_range(start, start + sclp.rzm);
256 static int sclp_unassign_storage(u16 rn)
258 return do_assign_storage(0x000c0001, rn);
261 struct attach_storage_sccb {
262 struct sccb_header header;
269 static int sclp_attach_storage(u8 id)
271 struct attach_storage_sccb *sccb;
275 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
278 sccb->header.length = PAGE_SIZE;
279 rc = sclp_sync_request_timeout(0x00080001 | id << 8, sccb,
280 SCLP_QUEUE_INTERVAL);
283 switch (sccb->header.response_code) {
285 set_bit(id, sclp_storage_ids);
286 for (i = 0; i < sccb->assigned; i++) {
287 if (sccb->entries[i])
288 sclp_unassign_storage(sccb->entries[i] >> 16);
296 free_page((unsigned long) sccb);
300 static int sclp_mem_change_state(unsigned long start, unsigned long size,
303 struct memory_increment *incr;
304 unsigned long long istart;
307 list_for_each_entry(incr, &sclp_mem_list, list) {
308 istart = rn2addr(incr->rn);
309 if (start + size - 1 < istart)
311 if (start > istart + sclp.rzm - 1)
314 rc |= sclp_assign_storage(incr->rn);
316 sclp_unassign_storage(incr->rn);
318 incr->standby = online ? 0 : 1;
320 return rc ? -EIO : 0;
323 static bool contains_standby_increment(unsigned long start, unsigned long end)
325 struct memory_increment *incr;
326 unsigned long istart;
328 list_for_each_entry(incr, &sclp_mem_list, list) {
329 istart = rn2addr(incr->rn);
330 if (end - 1 < istart)
332 if (start > istart + sclp.rzm - 1)
340 static int sclp_mem_notifier(struct notifier_block *nb,
341 unsigned long action, void *data)
343 unsigned long start, size;
344 struct memory_notify *arg;
349 start = arg->start_pfn << PAGE_SHIFT;
350 size = arg->nr_pages << PAGE_SHIFT;
351 mutex_lock(&sclp_mem_mutex);
352 for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
353 sclp_attach_storage(id);
355 case MEM_GOING_OFFLINE:
357 * We do not allow to set memory blocks offline that contain
358 * standby memory. This is done to simplify the "memory online"
361 if (contains_standby_increment(start, start + size))
365 case MEM_CANCEL_OFFLINE:
367 case MEM_GOING_ONLINE:
368 rc = sclp_mem_change_state(start, size, 1);
370 case MEM_CANCEL_ONLINE:
371 sclp_mem_change_state(start, size, 0);
374 sclp_mem_change_state(start, size, 0);
381 sclp_mem_state_changed = 1;
382 mutex_unlock(&sclp_mem_mutex);
383 return rc ? NOTIFY_BAD : NOTIFY_OK;
386 static struct notifier_block sclp_mem_nb = {
387 .notifier_call = sclp_mem_notifier,
390 static void __init align_to_block_size(unsigned long long *start,
391 unsigned long long *size)
393 unsigned long long start_align, size_align, alignment;
395 alignment = memory_block_size_bytes();
396 start_align = roundup(*start, alignment);
397 size_align = rounddown(*start + *size, alignment) - start_align;
399 pr_info("Standby memory at 0x%llx (%lluM of %lluM usable)\n",
400 *start, size_align >> 20, *size >> 20);
401 *start = start_align;
405 static void __init add_memory_merged(u16 rn)
407 static u16 first_rn, num;
408 unsigned long long start, size;
410 if (rn && first_rn && (first_rn + num == rn)) {
416 start = rn2addr(first_rn);
417 size = (unsigned long long) num * sclp.rzm;
418 if (start >= VMEM_MAX_PHYS)
420 if (start + size > VMEM_MAX_PHYS)
421 size = VMEM_MAX_PHYS - start;
422 if (memory_end_set && (start >= memory_end))
424 if (memory_end_set && (start + size > memory_end))
425 size = memory_end - start;
426 align_to_block_size(&start, &size);
428 add_memory(0, start, size);
434 static void __init sclp_add_standby_memory(void)
436 struct memory_increment *incr;
438 list_for_each_entry(incr, &sclp_mem_list, list)
440 add_memory_merged(incr->rn);
441 add_memory_merged(0);
444 static void __init insert_increment(u16 rn, int standby, int assigned)
446 struct memory_increment *incr, *new_incr;
447 struct list_head *prev;
450 new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
454 new_incr->standby = standby;
456 prev = &sclp_mem_list;
457 list_for_each_entry(incr, &sclp_mem_list, list) {
458 if (assigned && incr->rn > rn)
460 if (!assigned && incr->rn - last_rn > 1)
466 new_incr->rn = last_rn + 1;
467 if (new_incr->rn > sclp.rnmax) {
471 list_add(&new_incr->list, prev);
474 static int sclp_mem_freeze(struct device *dev)
476 if (!sclp_mem_state_changed)
478 pr_err("Memory hotplug state changed, suspend refused.\n");
482 struct read_storage_sccb {
483 struct sccb_header header;
491 static const struct dev_pm_ops sclp_mem_pm_ops = {
492 .freeze = sclp_mem_freeze,
495 static struct platform_driver sclp_mem_pdrv = {
498 .pm = &sclp_mem_pm_ops,
502 static int __init sclp_detect_standby_memory(void)
504 struct platform_device *sclp_pdev;
505 struct read_storage_sccb *sccb;
506 int i, id, assigned, rc;
508 if (OLDMEM_BASE) /* No standby memory in kdump mode */
510 if ((sclp.facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
513 sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
517 for (id = 0; id <= sclp_max_storage_id; id++) {
518 memset(sccb, 0, PAGE_SIZE);
519 sccb->header.length = PAGE_SIZE;
520 rc = sclp_sync_request(0x00040001 | id << 8, sccb);
523 switch (sccb->header.response_code) {
525 set_bit(id, sclp_storage_ids);
526 for (i = 0; i < sccb->assigned; i++) {
527 if (!sccb->entries[i])
530 insert_increment(sccb->entries[i] >> 16, 0, 1);
536 for (i = 0; i < sccb->assigned; i++) {
537 if (!sccb->entries[i])
540 insert_increment(sccb->entries[i] >> 16, 1, 1);
548 sclp_max_storage_id = sccb->max_id;
550 if (rc || list_empty(&sclp_mem_list))
552 for (i = 1; i <= sclp.rnmax - assigned; i++)
553 insert_increment(0, 1, 0);
554 rc = register_memory_notifier(&sclp_mem_nb);
557 rc = platform_driver_register(&sclp_mem_pdrv);
560 sclp_pdev = platform_device_register_simple("sclp_mem", -1, NULL, 0);
561 rc = PTR_ERR_OR_ZERO(sclp_pdev);
564 sclp_add_standby_memory();
567 platform_driver_unregister(&sclp_mem_pdrv);
569 free_page((unsigned long) sccb);
572 __initcall(sclp_detect_standby_memory);
574 #endif /* CONFIG_MEMORY_HOTPLUG */
577 * PCI I/O adapter configuration related functions.
579 #define SCLP_CMDW_CONFIGURE_PCI 0x001a0001
580 #define SCLP_CMDW_DECONFIGURE_PCI 0x001b0001
582 #define SCLP_RECONFIG_PCI_ATPYE 2
584 struct pci_cfg_sccb {
585 struct sccb_header header;
586 u8 atype; /* adapter type */
589 u32 aid; /* adapter identifier */
592 static int do_pci_configure(sclp_cmdw_t cmd, u32 fid)
594 struct pci_cfg_sccb *sccb;
597 if (!SCLP_HAS_PCI_RECONFIG)
600 sccb = (struct pci_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
604 sccb->header.length = PAGE_SIZE;
605 sccb->atype = SCLP_RECONFIG_PCI_ATPYE;
607 rc = sclp_sync_request(cmd, sccb);
610 switch (sccb->header.response_code) {
615 pr_warn("configure PCI I/O adapter failed: cmd=0x%08x response=0x%04x\n",
616 cmd, sccb->header.response_code);
621 free_page((unsigned long) sccb);
625 int sclp_pci_configure(u32 fid)
627 return do_pci_configure(SCLP_CMDW_CONFIGURE_PCI, fid);
629 EXPORT_SYMBOL(sclp_pci_configure);
631 int sclp_pci_deconfigure(u32 fid)
633 return do_pci_configure(SCLP_CMDW_DECONFIGURE_PCI, fid);
635 EXPORT_SYMBOL(sclp_pci_deconfigure);
638 * Channel path configuration related functions.
641 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
642 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
643 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
645 struct chp_cfg_sccb {
646 struct sccb_header header;
650 } __attribute__((packed));
652 static int do_chp_configure(sclp_cmdw_t cmd)
654 struct chp_cfg_sccb *sccb;
657 if (!SCLP_HAS_CHP_RECONFIG)
660 sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
663 sccb->header.length = sizeof(*sccb);
664 rc = sclp_sync_request(cmd, sccb);
667 switch (sccb->header.response_code) {
674 pr_warning("configure channel-path failed "
675 "(cmd=0x%08x, response=0x%04x)\n", cmd,
676 sccb->header.response_code);
681 free_page((unsigned long) sccb);
686 * sclp_chp_configure - perform configure channel-path sclp command
687 * @chpid: channel-path ID
689 * Perform configure channel-path command sclp command for specified chpid.
690 * Return 0 after command successfully finished, non-zero otherwise.
692 int sclp_chp_configure(struct chp_id chpid)
694 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
698 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
699 * @chpid: channel-path ID
701 * Perform deconfigure channel-path command sclp command for specified chpid
702 * and wait for completion. On success return 0. Return non-zero otherwise.
704 int sclp_chp_deconfigure(struct chp_id chpid)
706 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
709 struct chp_info_sccb {
710 struct sccb_header header;
711 u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
712 u8 standby[SCLP_CHP_INFO_MASK_SIZE];
713 u8 configured[SCLP_CHP_INFO_MASK_SIZE];
717 } __attribute__((packed));
720 * sclp_chp_read_info - perform read channel-path information sclp command
721 * @info: resulting channel-path information data
723 * Perform read channel-path information sclp command and wait for completion.
724 * On success, store channel-path information in @info and return 0. Return
725 * non-zero otherwise.
727 int sclp_chp_read_info(struct sclp_chp_info *info)
729 struct chp_info_sccb *sccb;
732 if (!SCLP_HAS_CHP_INFO)
735 sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
738 sccb->header.length = sizeof(*sccb);
739 rc = sclp_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
742 if (sccb->header.response_code != 0x0010) {
743 pr_warning("read channel-path info failed "
744 "(response=0x%04x)\n", sccb->header.response_code);
748 memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
749 memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
750 memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
752 free_page((unsigned long) sccb);