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>
22 #include <asm/setup.h>
26 #define SCLP_CMDW_READ_SCP_INFO 0x00020001
27 #define SCLP_CMDW_READ_SCP_INFO_FORCED 0x00120001
29 struct read_info_sccb
{
30 struct sccb_header header
; /* 0-7 */
33 u8 _reserved0
[24 - 11]; /* 11-15 */
34 u8 loadparm
[8]; /* 24-31 */
35 u8 _reserved1
[48 - 32]; /* 32-47 */
36 u64 facilities
; /* 48-55 */
37 u8 _reserved2
[84 - 56]; /* 56-83 */
39 u8 _reserved3
[91 - 85]; /* 85-90 */
41 u8 _reserved4
[100 - 92]; /* 92-99 */
42 u32 rnsize2
; /* 100-103 */
43 u64 rnmax2
; /* 104-111 */
44 u8 _reserved5
[4096 - 112]; /* 112-4095 */
45 } __attribute__((packed
, aligned(PAGE_SIZE
)));
47 static struct read_info_sccb __initdata early_read_info_sccb
;
48 static int __initdata early_read_info_sccb_valid
;
52 static unsigned long long rzm
;
53 static unsigned long long rnmax
;
55 static int __init
sclp_cmd_sync_early(sclp_cmdw_t cmd
, void *sccb
)
60 rc
= sclp_service_call(cmd
, sccb
);
63 __load_psw_mask(PSW_BASE_BITS
| PSW_MASK_EXT
|
64 PSW_MASK_WAIT
| PSW_DEFAULT_KEY
);
67 /* Contents of the sccb might have changed. */
69 __ctl_clear_bit(0, 9);
73 static void __init
sclp_read_info_early(void)
77 struct read_info_sccb
*sccb
;
78 sclp_cmdw_t commands
[] = {SCLP_CMDW_READ_SCP_INFO_FORCED
,
79 SCLP_CMDW_READ_SCP_INFO
};
81 sccb
= &early_read_info_sccb
;
82 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
84 memset(sccb
, 0, sizeof(*sccb
));
85 sccb
->header
.length
= sizeof(*sccb
);
86 sccb
->header
.control_mask
[2] = 0x80;
87 rc
= sclp_cmd_sync_early(commands
[i
], sccb
);
88 } while (rc
== -EBUSY
);
92 if (sccb
->header
.response_code
== 0x10) {
93 early_read_info_sccb_valid
= 1;
96 if (sccb
->header
.response_code
!= 0x1f0)
101 void __init
sclp_facilities_detect(void)
103 struct read_info_sccb
*sccb
;
105 sclp_read_info_early();
106 if (!early_read_info_sccb_valid
)
109 sccb
= &early_read_info_sccb
;
110 sclp_facilities
= sccb
->facilities
;
111 sclp_fac84
= sccb
->fac84
;
112 rnmax
= sccb
->rnmax
? sccb
->rnmax
: sccb
->rnmax2
;
113 rzm
= sccb
->rnsize
? sccb
->rnsize
: sccb
->rnsize2
;
117 unsigned long long sclp_get_rnmax(void)
122 unsigned long long sclp_get_rzm(void)
128 * This function will be called after sclp_facilities_detect(), which gets
129 * called from early.c code. Therefore the sccb should have valid contents.
131 void __init
sclp_get_ipl_info(struct sclp_ipl_info
*info
)
133 struct read_info_sccb
*sccb
;
135 if (!early_read_info_sccb_valid
)
137 sccb
= &early_read_info_sccb
;
139 if (sccb
->flags
& 0x2)
141 memcpy(&info
->loadparm
, &sccb
->loadparm
, LOADPARM_LEN
);
144 static void sclp_sync_callback(struct sclp_req
*req
, void *data
)
146 struct completion
*completion
= data
;
148 complete(completion
);
151 static int do_sync_request(sclp_cmdw_t cmd
, void *sccb
)
153 struct completion completion
;
154 struct sclp_req
*request
;
157 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
160 request
->command
= cmd
;
161 request
->sccb
= sccb
;
162 request
->status
= SCLP_REQ_FILLED
;
163 request
->callback
= sclp_sync_callback
;
164 request
->callback_data
= &completion
;
165 init_completion(&completion
);
167 /* Perform sclp request. */
168 rc
= sclp_add_request(request
);
171 wait_for_completion(&completion
);
173 /* Check response. */
174 if (request
->status
!= SCLP_REQ_DONE
) {
175 pr_warning("sync request failed (cmd=0x%08x, "
176 "status=0x%02x)\n", cmd
, request
->status
);
185 * CPU configuration related functions.
188 #define SCLP_CMDW_READ_CPU_INFO 0x00010001
189 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
190 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
192 struct read_cpu_info_sccb
{
193 struct sccb_header header
;
195 u16 offset_configured
;
198 u8 reserved
[4096 - 16];
199 } __attribute__((packed
, aligned(PAGE_SIZE
)));
201 static void sclp_fill_cpu_info(struct sclp_cpu_info
*info
,
202 struct read_cpu_info_sccb
*sccb
)
204 char *page
= (char *) sccb
;
206 memset(info
, 0, sizeof(*info
));
207 info
->configured
= sccb
->nr_configured
;
208 info
->standby
= sccb
->nr_standby
;
209 info
->combined
= sccb
->nr_configured
+ sccb
->nr_standby
;
210 info
->has_cpu_type
= sclp_fac84
& 0x1;
211 memcpy(&info
->cpu
, page
+ sccb
->offset_configured
,
212 info
->combined
* sizeof(struct sclp_cpu_entry
));
215 int sclp_get_cpu_info(struct sclp_cpu_info
*info
)
218 struct read_cpu_info_sccb
*sccb
;
220 if (!SCLP_HAS_CPU_INFO
)
222 sccb
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
225 sccb
->header
.length
= sizeof(*sccb
);
226 rc
= do_sync_request(SCLP_CMDW_READ_CPU_INFO
, sccb
);
229 if (sccb
->header
.response_code
!= 0x0010) {
230 pr_warning("readcpuinfo failed (response=0x%04x)\n",
231 sccb
->header
.response_code
);
235 sclp_fill_cpu_info(info
, sccb
);
237 free_page((unsigned long) sccb
);
241 struct cpu_configure_sccb
{
242 struct sccb_header header
;
243 } __attribute__((packed
, aligned(8)));
245 static int do_cpu_configure(sclp_cmdw_t cmd
)
247 struct cpu_configure_sccb
*sccb
;
250 if (!SCLP_HAS_CPU_RECONFIG
)
253 * This is not going to cross a page boundary since we force
254 * kmalloc to have a minimum alignment of 8 bytes on s390.
256 sccb
= kzalloc(sizeof(*sccb
), GFP_KERNEL
| GFP_DMA
);
259 sccb
->header
.length
= sizeof(*sccb
);
260 rc
= do_sync_request(cmd
, sccb
);
263 switch (sccb
->header
.response_code
) {
268 pr_warning("configure cpu failed (cmd=0x%08x, "
269 "response=0x%04x)\n", cmd
,
270 sccb
->header
.response_code
);
279 int sclp_cpu_configure(u8 cpu
)
281 return do_cpu_configure(SCLP_CMDW_CONFIGURE_CPU
| cpu
<< 8);
284 int sclp_cpu_deconfigure(u8 cpu
)
286 return do_cpu_configure(SCLP_CMDW_DECONFIGURE_CPU
| cpu
<< 8);
289 #ifdef CONFIG_MEMORY_HOTPLUG
291 static DEFINE_MUTEX(sclp_mem_mutex
);
292 static LIST_HEAD(sclp_mem_list
);
293 static u8 sclp_max_storage_id
;
294 static unsigned long sclp_storage_ids
[256 / BITS_PER_LONG
];
296 struct memory_increment
{
297 struct list_head list
;
303 struct assign_storage_sccb
{
304 struct sccb_header header
;
308 static unsigned long long rn2addr(u16 rn
)
310 return (unsigned long long) (rn
- 1) * rzm
;
313 static int do_assign_storage(sclp_cmdw_t cmd
, u16 rn
)
315 struct assign_storage_sccb
*sccb
;
318 sccb
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
321 sccb
->header
.length
= PAGE_SIZE
;
323 rc
= do_sync_request(cmd
, sccb
);
326 switch (sccb
->header
.response_code
) {
331 pr_warning("assign storage failed (cmd=0x%08x, "
332 "response=0x%04x, rn=0x%04x)\n", cmd
,
333 sccb
->header
.response_code
, rn
);
338 free_page((unsigned long) sccb
);
342 static int sclp_assign_storage(u16 rn
)
344 return do_assign_storage(0x000d0001, rn
);
347 static int sclp_unassign_storage(u16 rn
)
349 return do_assign_storage(0x000c0001, rn
);
352 struct attach_storage_sccb
{
353 struct sccb_header header
;
360 static int sclp_attach_storage(u8 id
)
362 struct attach_storage_sccb
*sccb
;
366 sccb
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
369 sccb
->header
.length
= PAGE_SIZE
;
370 rc
= do_sync_request(0x00080001 | id
<< 8, sccb
);
373 switch (sccb
->header
.response_code
) {
375 set_bit(id
, sclp_storage_ids
);
376 for (i
= 0; i
< sccb
->assigned
; i
++)
377 sclp_unassign_storage(sccb
->entries
[i
] >> 16);
384 free_page((unsigned long) sccb
);
388 static int sclp_mem_change_state(unsigned long start
, unsigned long size
,
391 struct memory_increment
*incr
;
392 unsigned long long istart
;
395 list_for_each_entry(incr
, &sclp_mem_list
, list
) {
396 istart
= rn2addr(incr
->rn
);
397 if (start
+ size
- 1 < istart
)
399 if (start
> istart
+ rzm
- 1)
402 if (incr
->usecount
++)
405 * Don't break the loop if one assign fails. Loop may
406 * be walked again on CANCEL and we can't save
407 * information if state changed before or not.
408 * So continue and increase usecount for all increments.
410 rc
|= sclp_assign_storage(incr
->rn
);
412 if (--incr
->usecount
)
414 sclp_unassign_storage(incr
->rn
);
417 return rc
? -EIO
: 0;
420 static int sclp_mem_notifier(struct notifier_block
*nb
,
421 unsigned long action
, void *data
)
423 unsigned long start
, size
;
424 struct memory_notify
*arg
;
429 start
= arg
->start_pfn
<< PAGE_SHIFT
;
430 size
= arg
->nr_pages
<< PAGE_SHIFT
;
431 mutex_lock(&sclp_mem_mutex
);
432 for (id
= 0; id
<= sclp_max_storage_id
; id
++)
433 if (!test_bit(id
, sclp_storage_ids
))
434 sclp_attach_storage(id
);
437 case MEM_GOING_OFFLINE
:
438 case MEM_CANCEL_OFFLINE
:
440 case MEM_GOING_ONLINE
:
441 rc
= sclp_mem_change_state(start
, size
, 1);
443 case MEM_CANCEL_ONLINE
:
444 sclp_mem_change_state(start
, size
, 0);
447 sclp_mem_change_state(start
, size
, 0);
453 mutex_unlock(&sclp_mem_mutex
);
454 return rc
? NOTIFY_BAD
: NOTIFY_OK
;
457 static struct notifier_block sclp_mem_nb
= {
458 .notifier_call
= sclp_mem_notifier
,
461 static void __init
add_memory_merged(u16 rn
)
463 static u16 first_rn
, num
;
464 unsigned long long start
, size
;
466 if (rn
&& first_rn
&& (first_rn
+ num
== rn
)) {
472 start
= rn2addr(first_rn
);
473 size
= (unsigned long long ) num
* rzm
;
474 if (start
>= VMEM_MAX_PHYS
)
476 if (start
+ size
> VMEM_MAX_PHYS
)
477 size
= VMEM_MAX_PHYS
- start
;
478 if (memory_end_set
&& (start
>= memory_end
))
480 if (memory_end_set
&& (start
+ size
> memory_end
))
481 size
= memory_end
- start
;
482 add_memory(0, start
, size
);
488 static void __init
sclp_add_standby_memory(void)
490 struct memory_increment
*incr
;
492 list_for_each_entry(incr
, &sclp_mem_list
, list
)
494 add_memory_merged(incr
->rn
);
495 add_memory_merged(0);
498 static void __init
insert_increment(u16 rn
, int standby
, int assigned
)
500 struct memory_increment
*incr
, *new_incr
;
501 struct list_head
*prev
;
504 new_incr
= kzalloc(sizeof(*new_incr
), GFP_KERNEL
);
508 new_incr
->standby
= standby
;
510 prev
= &sclp_mem_list
;
511 list_for_each_entry(incr
, &sclp_mem_list
, list
) {
512 if (assigned
&& incr
->rn
> rn
)
514 if (!assigned
&& incr
->rn
- last_rn
> 1)
520 new_incr
->rn
= last_rn
+ 1;
521 if (new_incr
->rn
> rnmax
) {
525 list_add(&new_incr
->list
, prev
);
528 struct read_storage_sccb
{
529 struct sccb_header header
;
537 static int __init
sclp_detect_standby_memory(void)
539 struct read_storage_sccb
*sccb
;
540 int i
, id
, assigned
, rc
;
542 if (!early_read_info_sccb_valid
)
544 if ((sclp_facilities
& 0xe00000000000ULL
) != 0xe00000000000ULL
)
547 sccb
= (void *) __get_free_page(GFP_KERNEL
| GFP_DMA
);
551 for (id
= 0; id
<= sclp_max_storage_id
; id
++) {
552 memset(sccb
, 0, PAGE_SIZE
);
553 sccb
->header
.length
= PAGE_SIZE
;
554 rc
= do_sync_request(0x00040001 | id
<< 8, sccb
);
557 switch (sccb
->header
.response_code
) {
559 set_bit(id
, sclp_storage_ids
);
560 for (i
= 0; i
< sccb
->assigned
; i
++) {
561 if (!sccb
->entries
[i
])
564 insert_increment(sccb
->entries
[i
] >> 16, 0, 1);
570 for (i
= 0; i
< sccb
->assigned
; i
++) {
571 if (!sccb
->entries
[i
])
574 insert_increment(sccb
->entries
[i
] >> 16, 1, 1);
582 sclp_max_storage_id
= sccb
->max_id
;
584 if (rc
|| list_empty(&sclp_mem_list
))
586 for (i
= 1; i
<= rnmax
- assigned
; i
++)
587 insert_increment(0, 1, 0);
588 rc
= register_memory_notifier(&sclp_mem_nb
);
591 sclp_add_standby_memory();
593 free_page((unsigned long) sccb
);
596 __initcall(sclp_detect_standby_memory
);
598 #endif /* CONFIG_MEMORY_HOTPLUG */
601 * Channel path configuration related functions.
604 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
605 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
606 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
608 struct chp_cfg_sccb
{
609 struct sccb_header header
;
613 } __attribute__((packed
));
615 static int do_chp_configure(sclp_cmdw_t cmd
)
617 struct chp_cfg_sccb
*sccb
;
620 if (!SCLP_HAS_CHP_RECONFIG
)
623 sccb
= (struct chp_cfg_sccb
*) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
626 sccb
->header
.length
= sizeof(*sccb
);
627 rc
= do_sync_request(cmd
, sccb
);
630 switch (sccb
->header
.response_code
) {
637 pr_warning("configure channel-path failed "
638 "(cmd=0x%08x, response=0x%04x)\n", cmd
,
639 sccb
->header
.response_code
);
644 free_page((unsigned long) sccb
);
649 * sclp_chp_configure - perform configure channel-path sclp command
650 * @chpid: channel-path ID
652 * Perform configure channel-path command sclp command for specified chpid.
653 * Return 0 after command successfully finished, non-zero otherwise.
655 int sclp_chp_configure(struct chp_id chpid
)
657 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH
| chpid
.id
<< 8);
661 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
662 * @chpid: channel-path ID
664 * Perform deconfigure channel-path command sclp command for specified chpid
665 * and wait for completion. On success return 0. Return non-zero otherwise.
667 int sclp_chp_deconfigure(struct chp_id chpid
)
669 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH
| chpid
.id
<< 8);
672 struct chp_info_sccb
{
673 struct sccb_header header
;
674 u8 recognized
[SCLP_CHP_INFO_MASK_SIZE
];
675 u8 standby
[SCLP_CHP_INFO_MASK_SIZE
];
676 u8 configured
[SCLP_CHP_INFO_MASK_SIZE
];
680 } __attribute__((packed
));
683 * sclp_chp_read_info - perform read channel-path information sclp command
684 * @info: resulting channel-path information data
686 * Perform read channel-path information sclp command and wait for completion.
687 * On success, store channel-path information in @info and return 0. Return
688 * non-zero otherwise.
690 int sclp_chp_read_info(struct sclp_chp_info
*info
)
692 struct chp_info_sccb
*sccb
;
695 if (!SCLP_HAS_CHP_INFO
)
698 sccb
= (struct chp_info_sccb
*) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
701 sccb
->header
.length
= sizeof(*sccb
);
702 rc
= do_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION
, sccb
);
705 if (sccb
->header
.response_code
!= 0x0010) {
706 pr_warning("read channel-path info failed "
707 "(response=0x%04x)\n", sccb
->header
.response_code
);
711 memcpy(info
->recognized
, sccb
->recognized
, SCLP_CHP_INFO_MASK_SIZE
);
712 memcpy(info
->standby
, sccb
->standby
, SCLP_CHP_INFO_MASK_SIZE
);
713 memcpy(info
->configured
, sccb
->configured
, SCLP_CHP_INFO_MASK_SIZE
);
715 free_page((unsigned long) sccb
);