2 * drivers/s390/char/sclp_cmd.c
4 * Copyright IBM Corp. 2007
5 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
6 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
9 #define KMSG_COMPONENT "sclp_cmd"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/completion.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
18 #include <linux/mmzone.h>
19 #include <linux/memory.h>
20 #include <asm/chpid.h>
25 #define SCLP_CMDW_READ_SCP_INFO 0x00020001
26 #define SCLP_CMDW_READ_SCP_INFO_FORCED 0x00120001
28 struct read_info_sccb {
29 struct sccb_header header; /* 0-7 */
32 u8 _reserved0[24 - 11]; /* 11-15 */
33 u8 loadparm[8]; /* 24-31 */
34 u8 _reserved1[48 - 32]; /* 32-47 */
35 u64 facilities; /* 48-55 */
36 u8 _reserved2[84 - 56]; /* 56-83 */
38 u8 _reserved3[91 - 85]; /* 85-90 */
40 u8 _reserved4[100 - 92]; /* 92-99 */
41 u32 rnsize2; /* 100-103 */
42 u64 rnmax2; /* 104-111 */
43 u8 _reserved5[4096 - 112]; /* 112-4095 */
44 } __attribute__((packed, aligned(PAGE_SIZE)));
46 static struct read_info_sccb __initdata early_read_info_sccb;
47 static int __initdata early_read_info_sccb_valid;
51 static unsigned long long rzm;
52 static unsigned long long rnmax;
54 static int __init sclp_cmd_sync_early(sclp_cmdw_t cmd, void *sccb)
59 rc = sclp_service_call(cmd, sccb);
62 __load_psw_mask(PSW_BASE_BITS | PSW_MASK_EXT |
63 PSW_MASK_WAIT | PSW_DEFAULT_KEY);
66 /* Contents of the sccb might have changed. */
68 __ctl_clear_bit(0, 9);
72 static void __init sclp_read_info_early(void)
76 struct read_info_sccb *sccb;
77 sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED,
78 SCLP_CMDW_READ_SCP_INFO};
80 sccb = &early_read_info_sccb;
81 for (i = 0; i < ARRAY_SIZE(commands); i++) {
83 memset(sccb, 0, sizeof(*sccb));
84 sccb->header.length = sizeof(*sccb);
85 sccb->header.control_mask[2] = 0x80;
86 rc = sclp_cmd_sync_early(commands[i], sccb);
87 } while (rc == -EBUSY);
91 if (sccb->header.response_code == 0x10) {
92 early_read_info_sccb_valid = 1;
95 if (sccb->header.response_code != 0x1f0)
100 void __init sclp_facilities_detect(void)
102 struct read_info_sccb *sccb;
104 sclp_read_info_early();
105 if (!early_read_info_sccb_valid)
108 sccb = &early_read_info_sccb;
109 sclp_facilities = sccb->facilities;
110 sclp_fac84 = sccb->fac84;
111 rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2;
112 rzm = sccb->rnsize ? sccb->rnsize : sccb->rnsize2;
116 unsigned long long sclp_get_rnmax(void)
121 unsigned long long sclp_get_rzm(void)
127 * This function will be called after sclp_facilities_detect(), which gets
128 * called from early.c code. Therefore the sccb should have valid contents.
130 void __init sclp_get_ipl_info(struct sclp_ipl_info *info)
132 struct read_info_sccb *sccb;
134 if (!early_read_info_sccb_valid)
136 sccb = &early_read_info_sccb;
138 if (sccb->flags & 0x2)
140 memcpy(&info->loadparm, &sccb->loadparm, LOADPARM_LEN);
143 static void sclp_sync_callback(struct sclp_req *req, void *data)
145 struct completion *completion = data;
147 complete(completion);
150 static int do_sync_request(sclp_cmdw_t cmd, void *sccb)
152 struct completion completion;
153 struct sclp_req *request;
156 request = kzalloc(sizeof(*request), GFP_KERNEL);
159 request->command = cmd;
160 request->sccb = sccb;
161 request->status = SCLP_REQ_FILLED;
162 request->callback = sclp_sync_callback;
163 request->callback_data = &completion;
164 init_completion(&completion);
166 /* Perform sclp request. */
167 rc = sclp_add_request(request);
170 wait_for_completion(&completion);
172 /* Check response. */
173 if (request->status != SCLP_REQ_DONE) {
174 pr_warning("sync request failed (cmd=0x%08x, "
175 "status=0x%02x)\n", cmd, request->status);
184 * CPU configuration related functions.
187 #define SCLP_CMDW_READ_CPU_INFO 0x00010001
188 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
189 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
191 struct read_cpu_info_sccb {
192 struct sccb_header header;
194 u16 offset_configured;
197 u8 reserved[4096 - 16];
198 } __attribute__((packed, aligned(PAGE_SIZE)));
200 static void sclp_fill_cpu_info(struct sclp_cpu_info *info,
201 struct read_cpu_info_sccb *sccb)
203 char *page = (char *) sccb;
205 memset(info, 0, sizeof(*info));
206 info->configured = sccb->nr_configured;
207 info->standby = sccb->nr_standby;
208 info->combined = sccb->nr_configured + sccb->nr_standby;
209 info->has_cpu_type = sclp_fac84 & 0x1;
210 memcpy(&info->cpu, page + sccb->offset_configured,
211 info->combined * sizeof(struct sclp_cpu_entry));
214 int sclp_get_cpu_info(struct sclp_cpu_info *info)
217 struct read_cpu_info_sccb *sccb;
219 if (!SCLP_HAS_CPU_INFO)
221 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
224 sccb->header.length = sizeof(*sccb);
225 rc = do_sync_request(SCLP_CMDW_READ_CPU_INFO, sccb);
228 if (sccb->header.response_code != 0x0010) {
229 pr_warning("readcpuinfo failed (response=0x%04x)\n",
230 sccb->header.response_code);
234 sclp_fill_cpu_info(info, sccb);
236 free_page((unsigned long) sccb);
240 struct cpu_configure_sccb {
241 struct sccb_header header;
242 } __attribute__((packed, aligned(8)));
244 static int do_cpu_configure(sclp_cmdw_t cmd)
246 struct cpu_configure_sccb *sccb;
249 if (!SCLP_HAS_CPU_RECONFIG)
252 * This is not going to cross a page boundary since we force
253 * kmalloc to have a minimum alignment of 8 bytes on s390.
255 sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
258 sccb->header.length = sizeof(*sccb);
259 rc = do_sync_request(cmd, sccb);
262 switch (sccb->header.response_code) {
267 pr_warning("configure cpu failed (cmd=0x%08x, "
268 "response=0x%04x)\n", cmd,
269 sccb->header.response_code);
278 int sclp_cpu_configure(u8 cpu)
280 return do_cpu_configure(SCLP_CMDW_CONFIGURE_CPU | cpu << 8);
283 int sclp_cpu_deconfigure(u8 cpu)
285 return do_cpu_configure(SCLP_CMDW_DECONFIGURE_CPU | cpu << 8);
288 #ifdef CONFIG_MEMORY_HOTPLUG
290 static DEFINE_MUTEX(sclp_mem_mutex);
291 static LIST_HEAD(sclp_mem_list);
292 static u8 sclp_max_storage_id;
293 static unsigned long sclp_storage_ids[256 / BITS_PER_LONG];
295 struct memory_increment {
296 struct list_head list;
302 struct assign_storage_sccb {
303 struct sccb_header header;
307 static unsigned long long rn2addr(u16 rn)
309 return (unsigned long long) (rn - 1) * rzm;
312 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
314 struct assign_storage_sccb *sccb;
317 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
320 sccb->header.length = PAGE_SIZE;
322 rc = do_sync_request(cmd, sccb);
325 switch (sccb->header.response_code) {
330 pr_warning("assign storage failed (cmd=0x%08x, "
331 "response=0x%04x, rn=0x%04x)\n", cmd,
332 sccb->header.response_code, rn);
337 free_page((unsigned long) sccb);
341 static int sclp_assign_storage(u16 rn)
343 return do_assign_storage(0x000d0001, rn);
346 static int sclp_unassign_storage(u16 rn)
348 return do_assign_storage(0x000c0001, rn);
351 struct attach_storage_sccb {
352 struct sccb_header header;
359 static int sclp_attach_storage(u8 id)
361 struct attach_storage_sccb *sccb;
365 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
368 sccb->header.length = PAGE_SIZE;
369 rc = do_sync_request(0x00080001 | id << 8, sccb);
372 switch (sccb->header.response_code) {
374 set_bit(id, sclp_storage_ids);
375 for (i = 0; i < sccb->assigned; i++)
376 sclp_unassign_storage(sccb->entries[i] >> 16);
383 free_page((unsigned long) sccb);
387 static int sclp_mem_change_state(unsigned long start, unsigned long size,
390 struct memory_increment *incr;
391 unsigned long long istart;
394 list_for_each_entry(incr, &sclp_mem_list, list) {
395 istart = rn2addr(incr->rn);
396 if (start + size - 1 < istart)
398 if (start > istart + rzm - 1)
401 if (incr->usecount++)
404 * Don't break the loop if one assign fails. Loop may
405 * be walked again on CANCEL and we can't save
406 * information if state changed before or not.
407 * So continue and increase usecount for all increments.
409 rc |= sclp_assign_storage(incr->rn);
411 if (--incr->usecount)
413 sclp_unassign_storage(incr->rn);
416 return rc ? -EIO : 0;
419 static int sclp_mem_notifier(struct notifier_block *nb,
420 unsigned long action, void *data)
422 unsigned long start, size;
423 struct memory_notify *arg;
428 start = arg->start_pfn << PAGE_SHIFT;
429 size = arg->nr_pages << PAGE_SHIFT;
430 mutex_lock(&sclp_mem_mutex);
431 for (id = 0; id <= sclp_max_storage_id; id++)
432 if (!test_bit(id, sclp_storage_ids))
433 sclp_attach_storage(id);
436 case MEM_GOING_OFFLINE:
437 case MEM_CANCEL_OFFLINE:
439 case MEM_GOING_ONLINE:
440 rc = sclp_mem_change_state(start, size, 1);
442 case MEM_CANCEL_ONLINE:
443 sclp_mem_change_state(start, size, 0);
446 sclp_mem_change_state(start, size, 0);
452 mutex_unlock(&sclp_mem_mutex);
453 return rc ? NOTIFY_BAD : NOTIFY_OK;
456 static struct notifier_block sclp_mem_nb = {
457 .notifier_call = sclp_mem_notifier,
460 static void __init add_memory_merged(u16 rn)
462 static u16 first_rn, num;
463 unsigned long long start, size;
465 if (rn && first_rn && (first_rn + num == rn)) {
471 start = rn2addr(first_rn);
472 size = (unsigned long long ) num * rzm;
473 if (start >= VMEM_MAX_PHYS)
475 if (start + size > VMEM_MAX_PHYS)
476 size = VMEM_MAX_PHYS - start;
477 add_memory(0, start, size);
483 static void __init sclp_add_standby_memory(void)
485 struct memory_increment *incr;
487 list_for_each_entry(incr, &sclp_mem_list, list)
489 add_memory_merged(incr->rn);
490 add_memory_merged(0);
493 static void __init insert_increment(u16 rn, int standby, int assigned)
495 struct memory_increment *incr, *new_incr;
496 struct list_head *prev;
499 new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
503 new_incr->standby = standby;
505 prev = &sclp_mem_list;
506 list_for_each_entry(incr, &sclp_mem_list, list) {
507 if (assigned && incr->rn > rn)
509 if (!assigned && incr->rn - last_rn > 1)
515 new_incr->rn = last_rn + 1;
516 if (new_incr->rn > rnmax) {
520 list_add(&new_incr->list, prev);
523 struct read_storage_sccb {
524 struct sccb_header header;
532 static int __init sclp_detect_standby_memory(void)
534 struct read_storage_sccb *sccb;
535 int i, id, assigned, rc;
537 if (!early_read_info_sccb_valid)
539 if ((sclp_facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
542 sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
546 for (id = 0; id <= sclp_max_storage_id; id++) {
547 memset(sccb, 0, PAGE_SIZE);
548 sccb->header.length = PAGE_SIZE;
549 rc = do_sync_request(0x00040001 | id << 8, sccb);
552 switch (sccb->header.response_code) {
554 set_bit(id, sclp_storage_ids);
555 for (i = 0; i < sccb->assigned; i++) {
556 if (!sccb->entries[i])
559 insert_increment(sccb->entries[i] >> 16, 0, 1);
565 for (i = 0; i < sccb->assigned; i++) {
566 if (!sccb->entries[i])
569 insert_increment(sccb->entries[i] >> 16, 1, 1);
577 sclp_max_storage_id = sccb->max_id;
579 if (rc || list_empty(&sclp_mem_list))
581 for (i = 1; i <= rnmax - assigned; i++)
582 insert_increment(0, 1, 0);
583 rc = register_memory_notifier(&sclp_mem_nb);
586 sclp_add_standby_memory();
588 free_page((unsigned long) sccb);
591 __initcall(sclp_detect_standby_memory);
593 #endif /* CONFIG_MEMORY_HOTPLUG */
596 * Channel path configuration related functions.
599 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
600 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
601 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
603 struct chp_cfg_sccb {
604 struct sccb_header header;
608 } __attribute__((packed));
610 static int do_chp_configure(sclp_cmdw_t cmd)
612 struct chp_cfg_sccb *sccb;
615 if (!SCLP_HAS_CHP_RECONFIG)
618 sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
621 sccb->header.length = sizeof(*sccb);
622 rc = do_sync_request(cmd, sccb);
625 switch (sccb->header.response_code) {
632 pr_warning("configure channel-path failed "
633 "(cmd=0x%08x, response=0x%04x)\n", cmd,
634 sccb->header.response_code);
639 free_page((unsigned long) sccb);
644 * sclp_chp_configure - perform configure channel-path sclp command
645 * @chpid: channel-path ID
647 * Perform configure channel-path command sclp command for specified chpid.
648 * Return 0 after command successfully finished, non-zero otherwise.
650 int sclp_chp_configure(struct chp_id chpid)
652 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
656 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
657 * @chpid: channel-path ID
659 * Perform deconfigure channel-path command sclp command for specified chpid
660 * and wait for completion. On success return 0. Return non-zero otherwise.
662 int sclp_chp_deconfigure(struct chp_id chpid)
664 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
667 struct chp_info_sccb {
668 struct sccb_header header;
669 u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
670 u8 standby[SCLP_CHP_INFO_MASK_SIZE];
671 u8 configured[SCLP_CHP_INFO_MASK_SIZE];
675 } __attribute__((packed));
678 * sclp_chp_read_info - perform read channel-path information sclp command
679 * @info: resulting channel-path information data
681 * Perform read channel-path information sclp command and wait for completion.
682 * On success, store channel-path information in @info and return 0. Return
683 * non-zero otherwise.
685 int sclp_chp_read_info(struct sclp_chp_info *info)
687 struct chp_info_sccb *sccb;
690 if (!SCLP_HAS_CHP_INFO)
693 sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
696 sccb->header.length = sizeof(*sccb);
697 rc = do_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
700 if (sccb->header.response_code != 0x0010) {
701 pr_warning("read channel-path info failed "
702 "(response=0x%04x)\n", sccb->header.response_code);
706 memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
707 memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
708 memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
710 free_page((unsigned long) sccb);