2 * arch/s390/hypfs/hypfs_diag.c
3 * Hypervisor filesystem for Linux on s390. Diag 204 and 224
6 * Copyright (C) IBM Corp. 2006
7 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/vmalloc.h>
14 #include <asm/ebcdic.h>
17 #define LPAR_NAME_LEN 8 /* lpar name len in diag 204 data */
18 #define CPU_NAME_LEN 16 /* type name len of cpus in diag224 name table */
19 #define TMP_SIZE 64 /* size of temporary buffers */
21 /* diag 204 subcodes */
29 /* The two available diag 204 data formats */
35 /* bit is set in flags, when physical cpu info is included in diag 204 data */
36 #define LPAR_PHYS_FLG 0x80
38 static char *diag224_cpu_names
; /* diag 224 name table */
39 static enum diag204_sc diag204_store_sc
; /* used subcode for store */
40 static enum diag204_format diag204_info_type
; /* used diag 204 data format */
42 static void *diag204_buf
; /* 4K aligned buffer for diag204 data */
43 static void *diag204_buf_vmalloc
; /* vmalloc pointer for diag204 data */
44 static int diag204_buf_pages
; /* number of pages for diag204 data */
47 * DIAG 204 data structures and member access functions.
49 * Since we have two different diag 204 data formats for old and new s390
50 * machines, we do not access the structs directly, but use getter functions for
51 * each struct member instead. This should make the code more readable.
54 /* Time information block */
63 } __attribute__ ((packed
));
65 struct x_info_blk_hdr
{
74 } __attribute__ ((packed
));
76 static inline int info_blk_hdr__size(enum diag204_format type
)
78 if (type
== INFO_SIMPLE
)
79 return sizeof(struct info_blk_hdr
);
81 return sizeof(struct x_info_blk_hdr
);
84 static inline __u8
info_blk_hdr__npar(enum diag204_format type
, void *hdr
)
86 if (type
== INFO_SIMPLE
)
87 return ((struct info_blk_hdr
*)hdr
)->npar
;
89 return ((struct x_info_blk_hdr
*)hdr
)->npar
;
92 static inline __u8
info_blk_hdr__flags(enum diag204_format type
, void *hdr
)
94 if (type
== INFO_SIMPLE
)
95 return ((struct info_blk_hdr
*)hdr
)->flags
;
97 return ((struct x_info_blk_hdr
*)hdr
)->flags
;
100 static inline __u16
info_blk_hdr__pcpus(enum diag204_format type
, void *hdr
)
102 if (type
== INFO_SIMPLE
)
103 return ((struct info_blk_hdr
*)hdr
)->phys_cpus
;
105 return ((struct x_info_blk_hdr
*)hdr
)->phys_cpus
;
108 /* Partition header */
114 char part_name
[LPAR_NAME_LEN
];
115 } __attribute__ ((packed
));
123 char part_name
[LPAR_NAME_LEN
];
133 } __attribute__ ((packed
));
135 static inline int part_hdr__size(enum diag204_format type
)
137 if (type
== INFO_SIMPLE
)
138 return sizeof(struct part_hdr
);
140 return sizeof(struct x_part_hdr
);
143 static inline __u8
part_hdr__rcpus(enum diag204_format type
, void *hdr
)
145 if (type
== INFO_SIMPLE
)
146 return ((struct part_hdr
*)hdr
)->cpus
;
148 return ((struct x_part_hdr
*)hdr
)->rcpus
;
151 static inline void part_hdr__part_name(enum diag204_format type
, void *hdr
,
154 if (type
== INFO_SIMPLE
)
155 memcpy(name
, ((struct part_hdr
*)hdr
)->part_name
,
158 memcpy(name
, ((struct x_part_hdr
*)hdr
)->part_name
,
160 EBCASC(name
, LPAR_NAME_LEN
);
161 name
[LPAR_NAME_LEN
] = 0;
173 } __attribute__ ((packed
));
192 } __attribute__ ((packed
));
196 static inline int cpu_info__size(enum diag204_format type
)
198 if (type
== INFO_SIMPLE
)
199 return sizeof(struct cpu_info
);
201 return sizeof(struct x_cpu_info
);
204 static inline __u8
cpu_info__ctidx(enum diag204_format type
, void *hdr
)
206 if (type
== INFO_SIMPLE
)
207 return ((struct cpu_info
*)hdr
)->ctidx
;
209 return ((struct x_cpu_info
*)hdr
)->ctidx
;
212 static inline __u16
cpu_info__cpu_addr(enum diag204_format type
, void *hdr
)
214 if (type
== INFO_SIMPLE
)
215 return ((struct cpu_info
*)hdr
)->cpu_addr
;
217 return ((struct x_cpu_info
*)hdr
)->cpu_addr
;
220 static inline __u64
cpu_info__acc_time(enum diag204_format type
, void *hdr
)
222 if (type
== INFO_SIMPLE
)
223 return ((struct cpu_info
*)hdr
)->acc_time
;
225 return ((struct x_cpu_info
*)hdr
)->acc_time
;
228 static inline __u64
cpu_info__lp_time(enum diag204_format type
, void *hdr
)
230 if (type
== INFO_SIMPLE
)
231 return ((struct cpu_info
*)hdr
)->lp_time
;
233 return ((struct x_cpu_info
*)hdr
)->lp_time
;
236 static inline __u64
cpu_info__online_time(enum diag204_format type
, void *hdr
)
238 if (type
== INFO_SIMPLE
)
239 return 0; /* online_time not available in simple info */
241 return ((struct x_cpu_info
*)hdr
)->online_time
;
244 /* Physical header */
251 } __attribute__ ((packed
));
259 } __attribute__ ((packed
));
261 static inline int phys_hdr__size(enum diag204_format type
)
263 if (type
== INFO_SIMPLE
)
264 return sizeof(struct phys_hdr
);
266 return sizeof(struct x_phys_hdr
);
269 static inline __u8
phys_hdr__cpus(enum diag204_format type
, void *hdr
)
271 if (type
== INFO_SIMPLE
)
272 return ((struct phys_hdr
*)hdr
)->cpus
;
274 return ((struct x_phys_hdr
*)hdr
)->cpus
;
277 /* Physical CPU info block */
286 } __attribute__ ((packed
));
295 } __attribute__ ((packed
));
297 static inline int phys_cpu__size(enum diag204_format type
)
299 if (type
== INFO_SIMPLE
)
300 return sizeof(struct phys_cpu
);
302 return sizeof(struct x_phys_cpu
);
305 static inline __u16
phys_cpu__cpu_addr(enum diag204_format type
, void *hdr
)
307 if (type
== INFO_SIMPLE
)
308 return ((struct phys_cpu
*)hdr
)->cpu_addr
;
310 return ((struct x_phys_cpu
*)hdr
)->cpu_addr
;
313 static inline __u64
phys_cpu__mgm_time(enum diag204_format type
, void *hdr
)
315 if (type
== INFO_SIMPLE
)
316 return ((struct phys_cpu
*)hdr
)->mgm_time
;
318 return ((struct x_phys_cpu
*)hdr
)->mgm_time
;
321 static inline __u64
phys_cpu__ctidx(enum diag204_format type
, void *hdr
)
323 if (type
== INFO_SIMPLE
)
324 return ((struct phys_cpu
*)hdr
)->ctidx
;
326 return ((struct x_phys_cpu
*)hdr
)->ctidx
;
329 /* Diagnose 204 functions */
331 static int diag204(unsigned long subcode
, unsigned long size
, void *addr
)
333 register unsigned long _subcode
asm("0") = subcode
;
334 register unsigned long _size
asm("1") = size
;
337 " diag %2,%0,0x204\n"
340 : "+d" (_subcode
), "+d" (_size
) : "d" (addr
) : "memory");
347 * For the old diag subcode 4 with simple data format we have to use real
348 * memory. If we use subcode 6 or 7 with extended data format, we can (and
349 * should) use vmalloc, since we need a lot of memory in that case. Currently
353 static void diag204_free_buffer(void)
357 if (diag204_buf_vmalloc
) {
358 vfree(diag204_buf_vmalloc
);
359 diag204_buf_vmalloc
= NULL
;
361 free_pages((unsigned long) diag204_buf
, 0);
363 diag204_buf_pages
= 0;
367 static void *diag204_alloc_vbuf(int pages
)
369 /* The buffer has to be page aligned! */
370 diag204_buf_vmalloc
= vmalloc(PAGE_SIZE
* (pages
+ 1));
371 if (!diag204_buf_vmalloc
)
372 return ERR_PTR(-ENOMEM
);
373 diag204_buf
= (void*)((unsigned long)diag204_buf_vmalloc
374 & ~0xfffUL
) + 0x1000;
375 diag204_buf_pages
= pages
;
379 static void *diag204_alloc_rbuf(void)
381 diag204_buf
= (void*)__get_free_pages(GFP_KERNEL
,0);
383 return ERR_PTR(-ENOMEM
);
384 diag204_buf_pages
= 1;
388 static void *diag204_get_buffer(enum diag204_format fmt
, int *pages
)
391 *pages
= diag204_buf_pages
;
394 if (fmt
== INFO_SIMPLE
) {
396 return diag204_alloc_rbuf();
397 } else {/* INFO_EXT */
398 *pages
= diag204((unsigned long)SUBC_RSI
|
399 (unsigned long)INFO_EXT
, 0, NULL
);
401 return ERR_PTR(-ENOSYS
);
403 return diag204_alloc_vbuf(*pages
);
408 * diag204_probe() has to find out, which type of diagnose 204 implementation
409 * we have on our machine. Currently there are three possible scanarios:
410 * - subcode 4 + simple data format (only one page)
411 * - subcode 4-6 + extended data format
412 * - subcode 4-7 + extended data format
414 * Subcode 5 is used to retrieve the size of the data, provided by subcodes
415 * 6 and 7. Subcode 7 basically has the same function as subcode 6. In addition
416 * to subcode 6 it provides also information about secondary cpus.
417 * In order to get as much information as possible, we first try
418 * subcode 7, then 6 and if both fail, we use subcode 4.
421 static int diag204_probe(void)
426 buf
= diag204_get_buffer(INFO_EXT
, &pages
);
428 if (diag204((unsigned long)SUBC_STIB7
|
429 (unsigned long)INFO_EXT
, pages
, buf
) >= 0) {
430 diag204_store_sc
= SUBC_STIB7
;
431 diag204_info_type
= INFO_EXT
;
434 if (diag204((unsigned long)SUBC_STIB6
|
435 (unsigned long)INFO_EXT
, pages
, buf
) >= 0) {
436 diag204_store_sc
= SUBC_STIB7
;
437 diag204_info_type
= INFO_EXT
;
440 diag204_free_buffer();
443 /* subcodes 6 and 7 failed, now try subcode 4 */
445 buf
= diag204_get_buffer(INFO_SIMPLE
, &pages
);
450 if (diag204((unsigned long)SUBC_STIB4
|
451 (unsigned long)INFO_SIMPLE
, pages
, buf
) >= 0) {
452 diag204_store_sc
= SUBC_STIB4
;
453 diag204_info_type
= INFO_SIMPLE
;
462 diag204_free_buffer();
467 static void *diag204_store(void)
472 buf
= diag204_get_buffer(diag204_info_type
, &pages
);
475 if (diag204((unsigned long)diag204_store_sc
|
476 (unsigned long)diag204_info_type
, pages
, buf
) < 0)
477 return ERR_PTR(-ENOSYS
);
482 /* Diagnose 224 functions */
484 static int diag224(void *ptr
)
489 " diag %1,%2,0x224\n"
493 : "+d" (rc
) :"d" (0), "d" (ptr
) : "memory");
497 static int diag224_get_name_table(void)
499 /* memory must be below 2GB */
500 diag224_cpu_names
= kmalloc(PAGE_SIZE
, GFP_KERNEL
| GFP_DMA
);
501 if (!diag224_cpu_names
)
503 if (diag224(diag224_cpu_names
)) {
504 kfree(diag224_cpu_names
);
507 EBCASC(diag224_cpu_names
+ 16, (*diag224_cpu_names
+ 1) * 16);
511 static void diag224_delete_name_table(void)
513 kfree(diag224_cpu_names
);
516 static int diag224_idx2name(int index
, char *name
)
518 memcpy(name
, diag224_cpu_names
+ ((index
+ 1) * CPU_NAME_LEN
),
520 name
[CPU_NAME_LEN
] = 0;
525 __init
int hypfs_diag_init(void)
529 if (diag204_probe()) {
530 printk(KERN_ERR
"hypfs: diag 204 not working.");
533 rc
= diag224_get_name_table();
535 diag204_free_buffer();
536 printk(KERN_ERR
"hypfs: could not get name table.\n");
541 void hypfs_diag_exit(void)
543 diag224_delete_name_table();
544 diag204_free_buffer();
548 * Functions to create the directory structure
549 * *******************************************
552 static int hypfs_create_cpu_files(struct super_block
*sb
,
553 struct dentry
*cpus_dir
, void *cpu_info
)
555 struct dentry
*cpu_dir
;
556 char buffer
[TMP_SIZE
];
559 snprintf(buffer
, TMP_SIZE
, "%d", cpu_info__cpu_addr(diag204_info_type
,
561 cpu_dir
= hypfs_mkdir(sb
, cpus_dir
, buffer
);
562 rc
= hypfs_create_u64(sb
, cpu_dir
, "mgmtime",
563 cpu_info__acc_time(diag204_info_type
, cpu_info
) -
564 cpu_info__lp_time(diag204_info_type
, cpu_info
));
567 rc
= hypfs_create_u64(sb
, cpu_dir
, "cputime",
568 cpu_info__lp_time(diag204_info_type
, cpu_info
));
571 if (diag204_info_type
== INFO_EXT
) {
572 rc
= hypfs_create_u64(sb
, cpu_dir
, "onlinetime",
573 cpu_info__online_time(diag204_info_type
,
578 diag224_idx2name(cpu_info__ctidx(diag204_info_type
, cpu_info
), buffer
);
579 rc
= hypfs_create_str(sb
, cpu_dir
, "type", buffer
);
585 static void *hypfs_create_lpar_files(struct super_block
*sb
,
586 struct dentry
*systems_dir
, void *part_hdr
)
588 struct dentry
*cpus_dir
;
589 struct dentry
*lpar_dir
;
590 char lpar_name
[LPAR_NAME_LEN
+ 1];
594 part_hdr__part_name(diag204_info_type
, part_hdr
, lpar_name
);
595 lpar_name
[LPAR_NAME_LEN
] = 0;
596 lpar_dir
= hypfs_mkdir(sb
, systems_dir
, lpar_name
);
597 if (IS_ERR(lpar_dir
))
599 cpus_dir
= hypfs_mkdir(sb
, lpar_dir
, "cpus");
600 if (IS_ERR(cpus_dir
))
602 cpu_info
= part_hdr
+ part_hdr__size(diag204_info_type
);
603 for (i
= 0; i
< part_hdr__rcpus(diag204_info_type
, part_hdr
); i
++) {
605 rc
= hypfs_create_cpu_files(sb
, cpus_dir
, cpu_info
);
608 cpu_info
+= cpu_info__size(diag204_info_type
);
613 static int hypfs_create_phys_cpu_files(struct super_block
*sb
,
614 struct dentry
*cpus_dir
, void *cpu_info
)
616 struct dentry
*cpu_dir
;
617 char buffer
[TMP_SIZE
];
620 snprintf(buffer
, TMP_SIZE
, "%i", phys_cpu__cpu_addr(diag204_info_type
,
622 cpu_dir
= hypfs_mkdir(sb
, cpus_dir
, buffer
);
624 return PTR_ERR(cpu_dir
);
625 rc
= hypfs_create_u64(sb
, cpu_dir
, "mgmtime",
626 phys_cpu__mgm_time(diag204_info_type
, cpu_info
));
629 diag224_idx2name(phys_cpu__ctidx(diag204_info_type
, cpu_info
), buffer
);
630 rc
= hypfs_create_str(sb
, cpu_dir
, "type", buffer
);
636 static void *hypfs_create_phys_files(struct super_block
*sb
,
637 struct dentry
*parent_dir
, void *phys_hdr
)
641 struct dentry
*cpus_dir
;
643 cpus_dir
= hypfs_mkdir(sb
, parent_dir
, "cpus");
644 if (IS_ERR(cpus_dir
))
646 cpu_info
= phys_hdr
+ phys_hdr__size(diag204_info_type
);
647 for (i
= 0; i
< phys_hdr__cpus(diag204_info_type
, phys_hdr
); i
++) {
649 rc
= hypfs_create_phys_cpu_files(sb
, cpus_dir
, cpu_info
);
652 cpu_info
+= phys_cpu__size(diag204_info_type
);
657 int hypfs_diag_create_files(struct super_block
*sb
, struct dentry
*root
)
659 struct dentry
*systems_dir
, *hyp_dir
;
660 void *time_hdr
, *part_hdr
;
664 buffer
= diag204_store();
666 return PTR_ERR(buffer
);
668 systems_dir
= hypfs_mkdir(sb
, root
, "systems");
669 if (IS_ERR(systems_dir
)) {
670 rc
= PTR_ERR(systems_dir
);
673 time_hdr
= (struct x_info_blk_hdr
*)buffer
;
674 part_hdr
= time_hdr
+ info_blk_hdr__size(diag204_info_type
);
675 for (i
= 0; i
< info_blk_hdr__npar(diag204_info_type
, time_hdr
); i
++) {
676 part_hdr
= hypfs_create_lpar_files(sb
, systems_dir
, part_hdr
);
677 if (IS_ERR(part_hdr
)) {
678 rc
= PTR_ERR(part_hdr
);
682 if (info_blk_hdr__flags(diag204_info_type
, time_hdr
) & LPAR_PHYS_FLG
) {
683 ptr
= hypfs_create_phys_files(sb
, root
, part_hdr
);
689 hyp_dir
= hypfs_mkdir(sb
, root
, "hyp");
690 if (IS_ERR(hyp_dir
)) {
691 rc
= PTR_ERR(hyp_dir
);
694 ptr
= hypfs_create_str(sb
, hyp_dir
, "type", "LPAR Hypervisor");