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 #define SCLP_CMDW_READ_SCP_INFO 0x00020001
32 #define SCLP_CMDW_READ_SCP_INFO_FORCED 0x00120001
34 struct read_info_sccb {
35 struct sccb_header header; /* 0-7 */
38 u8 _reserved0[24 - 11]; /* 11-15 */
39 u8 loadparm[8]; /* 24-31 */
40 u8 _reserved1[48 - 32]; /* 32-47 */
41 u64 facilities; /* 48-55 */
42 u8 _reserved2[84 - 56]; /* 56-83 */
45 u8 _reserved3[91 - 86]; /* 86-90 */
47 u8 _reserved4[100 - 92]; /* 92-99 */
48 u32 rnsize2; /* 100-103 */
49 u64 rnmax2; /* 104-111 */
50 u8 _reserved5[4096 - 112]; /* 112-4095 */
51 } __attribute__((packed, aligned(PAGE_SIZE)));
53 static struct init_sccb __initdata early_event_mask_sccb __aligned(PAGE_SIZE);
54 static struct read_info_sccb __initdata early_read_info_sccb;
55 static int __initdata early_read_info_sccb_valid;
60 static unsigned long long rzm;
61 static unsigned long long rnmax;
63 static int __init sclp_cmd_sync_early(sclp_cmdw_t cmd, void *sccb)
68 rc = sclp_service_call(cmd, sccb);
71 __load_psw_mask(PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_EA |
72 PSW_MASK_BA | PSW_MASK_EXT | PSW_MASK_WAIT);
75 /* Contents of the sccb might have changed. */
77 __ctl_clear_bit(0, 9);
81 static void __init sclp_read_info_early(void)
85 struct read_info_sccb *sccb;
86 sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED,
87 SCLP_CMDW_READ_SCP_INFO};
89 sccb = &early_read_info_sccb;
90 for (i = 0; i < ARRAY_SIZE(commands); i++) {
92 memset(sccb, 0, sizeof(*sccb));
93 sccb->header.length = sizeof(*sccb);
94 sccb->header.function_code = 0x80;
95 sccb->header.control_mask[2] = 0x80;
96 rc = sclp_cmd_sync_early(commands[i], sccb);
97 } while (rc == -EBUSY);
101 if (sccb->header.response_code == 0x10) {
102 early_read_info_sccb_valid = 1;
105 if (sccb->header.response_code != 0x1f0)
110 static void __init sclp_event_mask_early(void)
112 struct init_sccb *sccb = &early_event_mask_sccb;
116 memset(sccb, 0, sizeof(*sccb));
117 sccb->header.length = sizeof(*sccb);
118 sccb->mask_length = sizeof(sccb_mask_t);
119 rc = sclp_cmd_sync_early(SCLP_CMDW_WRITE_EVENT_MASK, sccb);
120 } while (rc == -EBUSY);
123 void __init sclp_facilities_detect(void)
125 struct read_info_sccb *sccb;
127 sclp_read_info_early();
128 if (!early_read_info_sccb_valid)
131 sccb = &early_read_info_sccb;
132 sclp_facilities = sccb->facilities;
133 sclp_fac84 = sccb->fac84;
134 sclp_fac85 = sccb->fac85;
135 rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2;
136 rzm = sccb->rnsize ? sccb->rnsize : sccb->rnsize2;
139 sclp_event_mask_early();
142 bool __init sclp_has_linemode(void)
144 struct init_sccb *sccb = &early_event_mask_sccb;
146 if (sccb->header.response_code != 0x20)
148 if (sccb->sclp_send_mask & (EVTYP_MSG_MASK | EVTYP_PMSGCMD_MASK))
153 bool __init sclp_has_vt220(void)
155 struct init_sccb *sccb = &early_event_mask_sccb;
157 if (sccb->header.response_code != 0x20)
159 if (sccb->sclp_send_mask & EVTYP_VT220MSG_MASK)
164 unsigned long long sclp_get_rnmax(void)
169 unsigned long long sclp_get_rzm(void)
174 u8 sclp_get_fac85(void)
178 EXPORT_SYMBOL_GPL(sclp_get_fac85);
181 * This function will be called after sclp_facilities_detect(), which gets
182 * called from early.c code. Therefore the sccb should have valid contents.
184 void __init sclp_get_ipl_info(struct sclp_ipl_info *info)
186 struct read_info_sccb *sccb;
188 if (!early_read_info_sccb_valid)
190 sccb = &early_read_info_sccb;
192 if (sccb->flags & 0x2)
194 memcpy(&info->loadparm, &sccb->loadparm, LOADPARM_LEN);
197 static void sclp_sync_callback(struct sclp_req *req, void *data)
199 struct completion *completion = data;
201 complete(completion);
204 static int do_sync_request(sclp_cmdw_t cmd, void *sccb)
206 struct completion completion;
207 struct sclp_req *request;
210 request = kzalloc(sizeof(*request), GFP_KERNEL);
213 request->command = cmd;
214 request->sccb = sccb;
215 request->status = SCLP_REQ_FILLED;
216 request->callback = sclp_sync_callback;
217 request->callback_data = &completion;
218 init_completion(&completion);
220 /* Perform sclp request. */
221 rc = sclp_add_request(request);
224 wait_for_completion(&completion);
226 /* Check response. */
227 if (request->status != SCLP_REQ_DONE) {
228 pr_warning("sync request failed (cmd=0x%08x, "
229 "status=0x%02x)\n", cmd, request->status);
238 * CPU configuration related functions.
241 #define SCLP_CMDW_READ_CPU_INFO 0x00010001
242 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
243 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
245 struct read_cpu_info_sccb {
246 struct sccb_header header;
248 u16 offset_configured;
251 u8 reserved[4096 - 16];
252 } __attribute__((packed, aligned(PAGE_SIZE)));
254 static void sclp_fill_cpu_info(struct sclp_cpu_info *info,
255 struct read_cpu_info_sccb *sccb)
257 char *page = (char *) sccb;
259 memset(info, 0, sizeof(*info));
260 info->configured = sccb->nr_configured;
261 info->standby = sccb->nr_standby;
262 info->combined = sccb->nr_configured + sccb->nr_standby;
263 info->has_cpu_type = sclp_fac84 & 0x1;
264 memcpy(&info->cpu, page + sccb->offset_configured,
265 info->combined * sizeof(struct sclp_cpu_entry));
268 int sclp_get_cpu_info(struct sclp_cpu_info *info)
271 struct read_cpu_info_sccb *sccb;
273 if (!SCLP_HAS_CPU_INFO)
275 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
278 sccb->header.length = sizeof(*sccb);
279 rc = do_sync_request(SCLP_CMDW_READ_CPU_INFO, sccb);
282 if (sccb->header.response_code != 0x0010) {
283 pr_warning("readcpuinfo failed (response=0x%04x)\n",
284 sccb->header.response_code);
288 sclp_fill_cpu_info(info, sccb);
290 free_page((unsigned long) sccb);
294 struct cpu_configure_sccb {
295 struct sccb_header header;
296 } __attribute__((packed, aligned(8)));
298 static int do_cpu_configure(sclp_cmdw_t cmd)
300 struct cpu_configure_sccb *sccb;
303 if (!SCLP_HAS_CPU_RECONFIG)
306 * This is not going to cross a page boundary since we force
307 * kmalloc to have a minimum alignment of 8 bytes on s390.
309 sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
312 sccb->header.length = sizeof(*sccb);
313 rc = do_sync_request(cmd, sccb);
316 switch (sccb->header.response_code) {
321 pr_warning("configure cpu failed (cmd=0x%08x, "
322 "response=0x%04x)\n", cmd,
323 sccb->header.response_code);
332 int sclp_cpu_configure(u8 cpu)
334 return do_cpu_configure(SCLP_CMDW_CONFIGURE_CPU | cpu << 8);
337 int sclp_cpu_deconfigure(u8 cpu)
339 return do_cpu_configure(SCLP_CMDW_DECONFIGURE_CPU | cpu << 8);
342 #ifdef CONFIG_MEMORY_HOTPLUG
344 static DEFINE_MUTEX(sclp_mem_mutex);
345 static LIST_HEAD(sclp_mem_list);
346 static u8 sclp_max_storage_id;
347 static unsigned long sclp_storage_ids[256 / BITS_PER_LONG];
348 static int sclp_mem_state_changed;
350 struct memory_increment {
351 struct list_head list;
357 struct assign_storage_sccb {
358 struct sccb_header header;
362 int arch_get_memory_phys_device(unsigned long start_pfn)
366 return PFN_PHYS(start_pfn) >> ilog2(rzm);
369 static unsigned long long rn2addr(u16 rn)
371 return (unsigned long long) (rn - 1) * rzm;
374 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
376 struct assign_storage_sccb *sccb;
379 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
382 sccb->header.length = PAGE_SIZE;
384 rc = do_sync_request(cmd, sccb);
387 switch (sccb->header.response_code) {
392 pr_warning("assign storage failed (cmd=0x%08x, "
393 "response=0x%04x, rn=0x%04x)\n", cmd,
394 sccb->header.response_code, rn);
399 free_page((unsigned long) sccb);
403 static int sclp_assign_storage(u16 rn)
405 unsigned long long start;
408 rc = do_assign_storage(0x000d0001, rn);
412 storage_key_init_range(start, start + rzm);
416 static int sclp_unassign_storage(u16 rn)
418 return do_assign_storage(0x000c0001, rn);
421 struct attach_storage_sccb {
422 struct sccb_header header;
429 static int sclp_attach_storage(u8 id)
431 struct attach_storage_sccb *sccb;
435 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
438 sccb->header.length = PAGE_SIZE;
439 rc = do_sync_request(0x00080001 | id << 8, sccb);
442 switch (sccb->header.response_code) {
444 set_bit(id, sclp_storage_ids);
445 for (i = 0; i < sccb->assigned; i++) {
446 if (sccb->entries[i])
447 sclp_unassign_storage(sccb->entries[i] >> 16);
455 free_page((unsigned long) sccb);
459 static int sclp_mem_change_state(unsigned long start, unsigned long size,
462 struct memory_increment *incr;
463 unsigned long long istart;
466 list_for_each_entry(incr, &sclp_mem_list, list) {
467 istart = rn2addr(incr->rn);
468 if (start + size - 1 < istart)
470 if (start > istart + rzm - 1)
473 if (incr->usecount++)
476 * Don't break the loop if one assign fails. Loop may
477 * be walked again on CANCEL and we can't save
478 * information if state changed before or not.
479 * So continue and increase usecount for all increments.
481 rc |= sclp_assign_storage(incr->rn);
483 if (--incr->usecount)
485 sclp_unassign_storage(incr->rn);
488 return rc ? -EIO : 0;
491 static int sclp_mem_notifier(struct notifier_block *nb,
492 unsigned long action, void *data)
494 unsigned long start, size;
495 struct memory_notify *arg;
500 start = arg->start_pfn << PAGE_SHIFT;
501 size = arg->nr_pages << PAGE_SHIFT;
502 mutex_lock(&sclp_mem_mutex);
503 for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
504 sclp_attach_storage(id);
507 case MEM_GOING_OFFLINE:
508 case MEM_CANCEL_OFFLINE:
510 case MEM_GOING_ONLINE:
511 rc = sclp_mem_change_state(start, size, 1);
513 case MEM_CANCEL_ONLINE:
514 sclp_mem_change_state(start, size, 0);
517 sclp_mem_change_state(start, size, 0);
524 sclp_mem_state_changed = 1;
525 mutex_unlock(&sclp_mem_mutex);
526 return rc ? NOTIFY_BAD : NOTIFY_OK;
529 static struct notifier_block sclp_mem_nb = {
530 .notifier_call = sclp_mem_notifier,
533 static void __init add_memory_merged(u16 rn)
535 static u16 first_rn, num;
536 unsigned long long start, size;
538 if (rn && first_rn && (first_rn + num == rn)) {
544 start = rn2addr(first_rn);
545 size = (unsigned long long ) num * rzm;
546 if (start >= VMEM_MAX_PHYS)
548 if (start + size > VMEM_MAX_PHYS)
549 size = VMEM_MAX_PHYS - start;
550 if (memory_end_set && (start >= memory_end))
552 if (memory_end_set && (start + size > memory_end))
553 size = memory_end - start;
554 add_memory(0, start, size);
560 static void __init sclp_add_standby_memory(void)
562 struct memory_increment *incr;
564 list_for_each_entry(incr, &sclp_mem_list, list)
566 add_memory_merged(incr->rn);
567 add_memory_merged(0);
570 static void __init insert_increment(u16 rn, int standby, int assigned)
572 struct memory_increment *incr, *new_incr;
573 struct list_head *prev;
576 new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
580 new_incr->standby = standby;
582 new_incr->usecount = 1;
584 prev = &sclp_mem_list;
585 list_for_each_entry(incr, &sclp_mem_list, list) {
586 if (assigned && incr->rn > rn)
588 if (!assigned && incr->rn - last_rn > 1)
594 new_incr->rn = last_rn + 1;
595 if (new_incr->rn > rnmax) {
599 list_add(&new_incr->list, prev);
602 static int sclp_mem_freeze(struct device *dev)
604 if (!sclp_mem_state_changed)
606 pr_err("Memory hotplug state changed, suspend refused.\n");
610 struct read_storage_sccb {
611 struct sccb_header header;
619 static const struct dev_pm_ops sclp_mem_pm_ops = {
620 .freeze = sclp_mem_freeze,
623 static struct platform_driver sclp_mem_pdrv = {
626 .pm = &sclp_mem_pm_ops,
630 static int __init sclp_detect_standby_memory(void)
632 struct platform_device *sclp_pdev;
633 struct read_storage_sccb *sccb;
634 int i, id, assigned, rc;
636 if (!early_read_info_sccb_valid)
638 if ((sclp_facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
641 sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
645 for (id = 0; id <= sclp_max_storage_id; id++) {
646 memset(sccb, 0, PAGE_SIZE);
647 sccb->header.length = PAGE_SIZE;
648 rc = do_sync_request(0x00040001 | id << 8, sccb);
651 switch (sccb->header.response_code) {
653 set_bit(id, sclp_storage_ids);
654 for (i = 0; i < sccb->assigned; i++) {
655 if (!sccb->entries[i])
658 insert_increment(sccb->entries[i] >> 16, 0, 1);
664 for (i = 0; i < sccb->assigned; i++) {
665 if (!sccb->entries[i])
668 insert_increment(sccb->entries[i] >> 16, 1, 1);
676 sclp_max_storage_id = sccb->max_id;
678 if (rc || list_empty(&sclp_mem_list))
680 for (i = 1; i <= rnmax - assigned; i++)
681 insert_increment(0, 1, 0);
682 rc = register_memory_notifier(&sclp_mem_nb);
685 rc = platform_driver_register(&sclp_mem_pdrv);
688 sclp_pdev = platform_device_register_simple("sclp_mem", -1, NULL, 0);
689 rc = IS_ERR(sclp_pdev) ? PTR_ERR(sclp_pdev) : 0;
692 sclp_add_standby_memory();
695 platform_driver_unregister(&sclp_mem_pdrv);
697 free_page((unsigned long) sccb);
700 __initcall(sclp_detect_standby_memory);
702 #endif /* CONFIG_MEMORY_HOTPLUG */
705 * PCI I/O adapter configuration related functions.
707 #define SCLP_CMDW_CONFIGURE_PCI 0x001a0001
708 #define SCLP_CMDW_DECONFIGURE_PCI 0x001b0001
710 #define SCLP_RECONFIG_PCI_ATPYE 2
712 struct pci_cfg_sccb {
713 struct sccb_header header;
714 u8 atype; /* adapter type */
717 u32 aid; /* adapter identifier */
720 static int do_pci_configure(sclp_cmdw_t cmd, u32 fid)
722 struct pci_cfg_sccb *sccb;
725 if (!SCLP_HAS_PCI_RECONFIG)
728 sccb = (struct pci_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
732 sccb->header.length = PAGE_SIZE;
733 sccb->atype = SCLP_RECONFIG_PCI_ATPYE;
735 rc = do_sync_request(cmd, sccb);
738 switch (sccb->header.response_code) {
743 pr_warn("configure PCI I/O adapter failed: cmd=0x%08x response=0x%04x\n",
744 cmd, sccb->header.response_code);
749 free_page((unsigned long) sccb);
753 int sclp_pci_configure(u32 fid)
755 return do_pci_configure(SCLP_CMDW_CONFIGURE_PCI, fid);
757 EXPORT_SYMBOL(sclp_pci_configure);
759 int sclp_pci_deconfigure(u32 fid)
761 return do_pci_configure(SCLP_CMDW_DECONFIGURE_PCI, fid);
763 EXPORT_SYMBOL(sclp_pci_deconfigure);
766 * Channel path configuration related functions.
769 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
770 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
771 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
773 struct chp_cfg_sccb {
774 struct sccb_header header;
778 } __attribute__((packed));
780 static int do_chp_configure(sclp_cmdw_t cmd)
782 struct chp_cfg_sccb *sccb;
785 if (!SCLP_HAS_CHP_RECONFIG)
788 sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
791 sccb->header.length = sizeof(*sccb);
792 rc = do_sync_request(cmd, sccb);
795 switch (sccb->header.response_code) {
802 pr_warning("configure channel-path failed "
803 "(cmd=0x%08x, response=0x%04x)\n", cmd,
804 sccb->header.response_code);
809 free_page((unsigned long) sccb);
814 * sclp_chp_configure - perform configure channel-path sclp command
815 * @chpid: channel-path ID
817 * Perform configure channel-path command sclp command for specified chpid.
818 * Return 0 after command successfully finished, non-zero otherwise.
820 int sclp_chp_configure(struct chp_id chpid)
822 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
826 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
827 * @chpid: channel-path ID
829 * Perform deconfigure channel-path command sclp command for specified chpid
830 * and wait for completion. On success return 0. Return non-zero otherwise.
832 int sclp_chp_deconfigure(struct chp_id chpid)
834 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
837 struct chp_info_sccb {
838 struct sccb_header header;
839 u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
840 u8 standby[SCLP_CHP_INFO_MASK_SIZE];
841 u8 configured[SCLP_CHP_INFO_MASK_SIZE];
845 } __attribute__((packed));
848 * sclp_chp_read_info - perform read channel-path information sclp command
849 * @info: resulting channel-path information data
851 * Perform read channel-path information sclp command and wait for completion.
852 * On success, store channel-path information in @info and return 0. Return
853 * non-zero otherwise.
855 int sclp_chp_read_info(struct sclp_chp_info *info)
857 struct chp_info_sccb *sccb;
860 if (!SCLP_HAS_CHP_INFO)
863 sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
866 sccb->header.length = sizeof(*sccb);
867 rc = do_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
870 if (sccb->header.response_code != 0x0010) {
871 pr_warning("read channel-path info failed "
872 "(response=0x%04x)\n", sccb->header.response_code);
876 memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
877 memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
878 memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
880 free_page((unsigned long) sccb);