2 * Hypervisor filesystem for Linux on s390. Diag 204 and 224
5 * Copyright IBM Corp. 2006, 2008
6 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
9 #define KMSG_COMPONENT "hypfs"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/vmalloc.h>
18 #include <asm/ebcdic.h>
21 #define LPAR_NAME_LEN 8 /* lpar name len in diag 204 data */
22 #define CPU_NAME_LEN 16 /* type name len of cpus in diag224 name table */
23 #define TMP_SIZE 64 /* size of temporary buffers */
25 #define DBFS_D204_HDR_VERSION 0
27 /* diag 204 subcodes */
35 /* The two available diag 204 data formats */
41 /* bit is set in flags, when physical cpu info is included in diag 204 data */
42 #define LPAR_PHYS_FLG 0x80
44 static char *diag224_cpu_names
; /* diag 224 name table */
45 static enum diag204_sc diag204_store_sc
; /* used subcode for store */
46 static enum diag204_format diag204_info_type
; /* used diag 204 data format */
48 static void *diag204_buf
; /* 4K aligned buffer for diag204 data */
49 static void *diag204_buf_vmalloc
; /* vmalloc pointer for diag204 data */
50 static int diag204_buf_pages
; /* number of pages for diag204 data */
52 static struct dentry
*dbfs_d204_file
;
55 * DIAG 204 data structures and member access functions.
57 * Since we have two different diag 204 data formats for old and new s390
58 * machines, we do not access the structs directly, but use getter functions for
59 * each struct member instead. This should make the code more readable.
62 /* Time information block */
71 } __attribute__ ((packed
));
73 struct x_info_blk_hdr
{
82 } __attribute__ ((packed
));
84 static inline int info_blk_hdr__size(enum diag204_format type
)
86 if (type
== INFO_SIMPLE
)
87 return sizeof(struct info_blk_hdr
);
89 return sizeof(struct x_info_blk_hdr
);
92 static inline __u8
info_blk_hdr__npar(enum diag204_format type
, void *hdr
)
94 if (type
== INFO_SIMPLE
)
95 return ((struct info_blk_hdr
*)hdr
)->npar
;
97 return ((struct x_info_blk_hdr
*)hdr
)->npar
;
100 static inline __u8
info_blk_hdr__flags(enum diag204_format type
, void *hdr
)
102 if (type
== INFO_SIMPLE
)
103 return ((struct info_blk_hdr
*)hdr
)->flags
;
105 return ((struct x_info_blk_hdr
*)hdr
)->flags
;
108 static inline __u16
info_blk_hdr__pcpus(enum diag204_format type
, void *hdr
)
110 if (type
== INFO_SIMPLE
)
111 return ((struct info_blk_hdr
*)hdr
)->phys_cpus
;
113 return ((struct x_info_blk_hdr
*)hdr
)->phys_cpus
;
116 /* Partition header */
122 char part_name
[LPAR_NAME_LEN
];
123 } __attribute__ ((packed
));
131 char part_name
[LPAR_NAME_LEN
];
141 } __attribute__ ((packed
));
143 static inline int part_hdr__size(enum diag204_format type
)
145 if (type
== INFO_SIMPLE
)
146 return sizeof(struct part_hdr
);
148 return sizeof(struct x_part_hdr
);
151 static inline __u8
part_hdr__rcpus(enum diag204_format type
, void *hdr
)
153 if (type
== INFO_SIMPLE
)
154 return ((struct part_hdr
*)hdr
)->cpus
;
156 return ((struct x_part_hdr
*)hdr
)->rcpus
;
159 static inline void part_hdr__part_name(enum diag204_format type
, void *hdr
,
162 if (type
== INFO_SIMPLE
)
163 memcpy(name
, ((struct part_hdr
*)hdr
)->part_name
,
166 memcpy(name
, ((struct x_part_hdr
*)hdr
)->part_name
,
168 EBCASC(name
, LPAR_NAME_LEN
);
169 name
[LPAR_NAME_LEN
] = 0;
181 } __attribute__ ((packed
));
200 } __attribute__ ((packed
));
204 static inline int cpu_info__size(enum diag204_format type
)
206 if (type
== INFO_SIMPLE
)
207 return sizeof(struct cpu_info
);
209 return sizeof(struct x_cpu_info
);
212 static inline __u8
cpu_info__ctidx(enum diag204_format type
, void *hdr
)
214 if (type
== INFO_SIMPLE
)
215 return ((struct cpu_info
*)hdr
)->ctidx
;
217 return ((struct x_cpu_info
*)hdr
)->ctidx
;
220 static inline __u16
cpu_info__cpu_addr(enum diag204_format type
, void *hdr
)
222 if (type
== INFO_SIMPLE
)
223 return ((struct cpu_info
*)hdr
)->cpu_addr
;
225 return ((struct x_cpu_info
*)hdr
)->cpu_addr
;
228 static inline __u64
cpu_info__acc_time(enum diag204_format type
, void *hdr
)
230 if (type
== INFO_SIMPLE
)
231 return ((struct cpu_info
*)hdr
)->acc_time
;
233 return ((struct x_cpu_info
*)hdr
)->acc_time
;
236 static inline __u64
cpu_info__lp_time(enum diag204_format type
, void *hdr
)
238 if (type
== INFO_SIMPLE
)
239 return ((struct cpu_info
*)hdr
)->lp_time
;
241 return ((struct x_cpu_info
*)hdr
)->lp_time
;
244 static inline __u64
cpu_info__online_time(enum diag204_format type
, void *hdr
)
246 if (type
== INFO_SIMPLE
)
247 return 0; /* online_time not available in simple info */
249 return ((struct x_cpu_info
*)hdr
)->online_time
;
252 /* Physical header */
259 } __attribute__ ((packed
));
267 } __attribute__ ((packed
));
269 static inline int phys_hdr__size(enum diag204_format type
)
271 if (type
== INFO_SIMPLE
)
272 return sizeof(struct phys_hdr
);
274 return sizeof(struct x_phys_hdr
);
277 static inline __u8
phys_hdr__cpus(enum diag204_format type
, void *hdr
)
279 if (type
== INFO_SIMPLE
)
280 return ((struct phys_hdr
*)hdr
)->cpus
;
282 return ((struct x_phys_hdr
*)hdr
)->cpus
;
285 /* Physical CPU info block */
294 } __attribute__ ((packed
));
303 } __attribute__ ((packed
));
305 static inline int phys_cpu__size(enum diag204_format type
)
307 if (type
== INFO_SIMPLE
)
308 return sizeof(struct phys_cpu
);
310 return sizeof(struct x_phys_cpu
);
313 static inline __u16
phys_cpu__cpu_addr(enum diag204_format type
, void *hdr
)
315 if (type
== INFO_SIMPLE
)
316 return ((struct phys_cpu
*)hdr
)->cpu_addr
;
318 return ((struct x_phys_cpu
*)hdr
)->cpu_addr
;
321 static inline __u64
phys_cpu__mgm_time(enum diag204_format type
, void *hdr
)
323 if (type
== INFO_SIMPLE
)
324 return ((struct phys_cpu
*)hdr
)->mgm_time
;
326 return ((struct x_phys_cpu
*)hdr
)->mgm_time
;
329 static inline __u64
phys_cpu__ctidx(enum diag204_format type
, void *hdr
)
331 if (type
== INFO_SIMPLE
)
332 return ((struct phys_cpu
*)hdr
)->ctidx
;
334 return ((struct x_phys_cpu
*)hdr
)->ctidx
;
337 /* Diagnose 204 functions */
339 static int diag204(unsigned long subcode
, unsigned long size
, void *addr
)
341 register unsigned long _subcode
asm("0") = subcode
;
342 register unsigned long _size
asm("1") = size
;
345 " diag %2,%0,0x204\n"
348 : "+d" (_subcode
), "+d" (_size
) : "d" (addr
) : "memory");
355 * For the old diag subcode 4 with simple data format we have to use real
356 * memory. If we use subcode 6 or 7 with extended data format, we can (and
357 * should) use vmalloc, since we need a lot of memory in that case. Currently
361 static void diag204_free_buffer(void)
365 if (diag204_buf_vmalloc
) {
366 vfree(diag204_buf_vmalloc
);
367 diag204_buf_vmalloc
= NULL
;
369 free_pages((unsigned long) diag204_buf
, 0);
374 static void *page_align_ptr(void *ptr
)
376 return (void *) PAGE_ALIGN((unsigned long) ptr
);
379 static void *diag204_alloc_vbuf(int pages
)
381 /* The buffer has to be page aligned! */
382 diag204_buf_vmalloc
= vmalloc(PAGE_SIZE
* (pages
+ 1));
383 if (!diag204_buf_vmalloc
)
384 return ERR_PTR(-ENOMEM
);
385 diag204_buf
= page_align_ptr(diag204_buf_vmalloc
);
386 diag204_buf_pages
= pages
;
390 static void *diag204_alloc_rbuf(void)
392 diag204_buf
= (void*)__get_free_pages(GFP_KERNEL
,0);
394 return ERR_PTR(-ENOMEM
);
395 diag204_buf_pages
= 1;
399 static void *diag204_get_buffer(enum diag204_format fmt
, int *pages
)
402 *pages
= diag204_buf_pages
;
405 if (fmt
== INFO_SIMPLE
) {
407 return diag204_alloc_rbuf();
408 } else {/* INFO_EXT */
409 *pages
= diag204((unsigned long)SUBC_RSI
|
410 (unsigned long)INFO_EXT
, 0, NULL
);
412 return ERR_PTR(-ENOSYS
);
414 return diag204_alloc_vbuf(*pages
);
419 * diag204_probe() has to find out, which type of diagnose 204 implementation
420 * we have on our machine. Currently there are three possible scanarios:
421 * - subcode 4 + simple data format (only one page)
422 * - subcode 4-6 + extended data format
423 * - subcode 4-7 + extended data format
425 * Subcode 5 is used to retrieve the size of the data, provided by subcodes
426 * 6 and 7. Subcode 7 basically has the same function as subcode 6. In addition
427 * to subcode 6 it provides also information about secondary cpus.
428 * In order to get as much information as possible, we first try
429 * subcode 7, then 6 and if both fail, we use subcode 4.
432 static int diag204_probe(void)
437 buf
= diag204_get_buffer(INFO_EXT
, &pages
);
439 if (diag204((unsigned long)SUBC_STIB7
|
440 (unsigned long)INFO_EXT
, pages
, buf
) >= 0) {
441 diag204_store_sc
= SUBC_STIB7
;
442 diag204_info_type
= INFO_EXT
;
445 if (diag204((unsigned long)SUBC_STIB6
|
446 (unsigned long)INFO_EXT
, pages
, buf
) >= 0) {
447 diag204_store_sc
= SUBC_STIB6
;
448 diag204_info_type
= INFO_EXT
;
451 diag204_free_buffer();
454 /* subcodes 6 and 7 failed, now try subcode 4 */
456 buf
= diag204_get_buffer(INFO_SIMPLE
, &pages
);
461 if (diag204((unsigned long)SUBC_STIB4
|
462 (unsigned long)INFO_SIMPLE
, pages
, buf
) >= 0) {
463 diag204_store_sc
= SUBC_STIB4
;
464 diag204_info_type
= INFO_SIMPLE
;
473 diag204_free_buffer();
478 static int diag204_do_store(void *buf
, int pages
)
482 rc
= diag204((unsigned long) diag204_store_sc
|
483 (unsigned long) diag204_info_type
, pages
, buf
);
484 return rc
< 0 ? -ENOSYS
: 0;
487 static void *diag204_store(void)
492 buf
= diag204_get_buffer(diag204_info_type
, &pages
);
495 rc
= diag204_do_store(buf
, pages
);
502 /* Diagnose 224 functions */
504 static int diag224(void *ptr
)
506 int rc
= -EOPNOTSUPP
;
509 " diag %1,%2,0x224\n"
513 : "+d" (rc
) :"d" (0), "d" (ptr
) : "memory");
517 static int diag224_get_name_table(void)
519 /* memory must be below 2GB */
520 diag224_cpu_names
= kmalloc(PAGE_SIZE
, GFP_KERNEL
| GFP_DMA
);
521 if (!diag224_cpu_names
)
523 if (diag224(diag224_cpu_names
)) {
524 kfree(diag224_cpu_names
);
527 EBCASC(diag224_cpu_names
+ 16, (*diag224_cpu_names
+ 1) * 16);
531 static void diag224_delete_name_table(void)
533 kfree(diag224_cpu_names
);
536 static int diag224_idx2name(int index
, char *name
)
538 memcpy(name
, diag224_cpu_names
+ ((index
+ 1) * CPU_NAME_LEN
),
540 name
[CPU_NAME_LEN
] = 0;
545 struct dbfs_d204_hdr
{
546 u64 len
; /* Length of d204 buffer without header */
547 u16 version
; /* Version of header */
548 u8 sc
; /* Used subcode */
550 } __attribute__ ((packed
));
553 struct dbfs_d204_hdr hdr
; /* 64 byte header */
554 char buf
[]; /* d204 buffer */
555 } __attribute__ ((packed
));
557 static int dbfs_d204_create(void **data
, void **data_free_ptr
, size_t *size
)
559 struct dbfs_d204
*d204
;
563 buf_size
= PAGE_SIZE
* (diag204_buf_pages
+ 1) + sizeof(d204
->hdr
);
564 base
= vzalloc(buf_size
);
567 d204
= page_align_ptr(base
+ sizeof(d204
->hdr
)) - sizeof(d204
->hdr
);
568 rc
= diag204_do_store(d204
->buf
, diag204_buf_pages
);
573 d204
->hdr
.version
= DBFS_D204_HDR_VERSION
;
574 d204
->hdr
.len
= PAGE_SIZE
* diag204_buf_pages
;
575 d204
->hdr
.sc
= diag204_store_sc
;
577 *data_free_ptr
= base
;
578 *size
= d204
->hdr
.len
+ sizeof(struct dbfs_d204_hdr
);
582 static struct hypfs_dbfs_file dbfs_file_d204
= {
584 .data_create
= dbfs_d204_create
,
588 __init
int hypfs_diag_init(void)
592 if (diag204_probe()) {
593 pr_err("The hardware system does not support hypfs\n");
596 if (diag204_info_type
== INFO_EXT
) {
597 rc
= hypfs_dbfs_create_file(&dbfs_file_d204
);
601 if (MACHINE_IS_LPAR
) {
602 rc
= diag224_get_name_table();
604 pr_err("The hardware system does not provide all "
605 "functions required by hypfs\n");
606 debugfs_remove(dbfs_d204_file
);
613 void hypfs_diag_exit(void)
615 debugfs_remove(dbfs_d204_file
);
616 diag224_delete_name_table();
617 diag204_free_buffer();
618 hypfs_dbfs_remove_file(&dbfs_file_d204
);
622 * Functions to create the directory structure
623 * *******************************************
626 static int hypfs_create_cpu_files(struct super_block
*sb
,
627 struct dentry
*cpus_dir
, void *cpu_info
)
629 struct dentry
*cpu_dir
;
630 char buffer
[TMP_SIZE
];
633 snprintf(buffer
, TMP_SIZE
, "%d", cpu_info__cpu_addr(diag204_info_type
,
635 cpu_dir
= hypfs_mkdir(sb
, cpus_dir
, buffer
);
636 rc
= hypfs_create_u64(sb
, cpu_dir
, "mgmtime",
637 cpu_info__acc_time(diag204_info_type
, cpu_info
) -
638 cpu_info__lp_time(diag204_info_type
, cpu_info
));
641 rc
= hypfs_create_u64(sb
, cpu_dir
, "cputime",
642 cpu_info__lp_time(diag204_info_type
, cpu_info
));
645 if (diag204_info_type
== INFO_EXT
) {
646 rc
= hypfs_create_u64(sb
, cpu_dir
, "onlinetime",
647 cpu_info__online_time(diag204_info_type
,
652 diag224_idx2name(cpu_info__ctidx(diag204_info_type
, cpu_info
), buffer
);
653 rc
= hypfs_create_str(sb
, cpu_dir
, "type", buffer
);
659 static void *hypfs_create_lpar_files(struct super_block
*sb
,
660 struct dentry
*systems_dir
, void *part_hdr
)
662 struct dentry
*cpus_dir
;
663 struct dentry
*lpar_dir
;
664 char lpar_name
[LPAR_NAME_LEN
+ 1];
668 part_hdr__part_name(diag204_info_type
, part_hdr
, lpar_name
);
669 lpar_name
[LPAR_NAME_LEN
] = 0;
670 lpar_dir
= hypfs_mkdir(sb
, systems_dir
, lpar_name
);
671 if (IS_ERR(lpar_dir
))
673 cpus_dir
= hypfs_mkdir(sb
, lpar_dir
, "cpus");
674 if (IS_ERR(cpus_dir
))
676 cpu_info
= part_hdr
+ part_hdr__size(diag204_info_type
);
677 for (i
= 0; i
< part_hdr__rcpus(diag204_info_type
, part_hdr
); i
++) {
679 rc
= hypfs_create_cpu_files(sb
, cpus_dir
, cpu_info
);
682 cpu_info
+= cpu_info__size(diag204_info_type
);
687 static int hypfs_create_phys_cpu_files(struct super_block
*sb
,
688 struct dentry
*cpus_dir
, void *cpu_info
)
690 struct dentry
*cpu_dir
;
691 char buffer
[TMP_SIZE
];
694 snprintf(buffer
, TMP_SIZE
, "%i", phys_cpu__cpu_addr(diag204_info_type
,
696 cpu_dir
= hypfs_mkdir(sb
, cpus_dir
, buffer
);
698 return PTR_ERR(cpu_dir
);
699 rc
= hypfs_create_u64(sb
, cpu_dir
, "mgmtime",
700 phys_cpu__mgm_time(diag204_info_type
, cpu_info
));
703 diag224_idx2name(phys_cpu__ctidx(diag204_info_type
, cpu_info
), buffer
);
704 rc
= hypfs_create_str(sb
, cpu_dir
, "type", buffer
);
710 static void *hypfs_create_phys_files(struct super_block
*sb
,
711 struct dentry
*parent_dir
, void *phys_hdr
)
715 struct dentry
*cpus_dir
;
717 cpus_dir
= hypfs_mkdir(sb
, parent_dir
, "cpus");
718 if (IS_ERR(cpus_dir
))
720 cpu_info
= phys_hdr
+ phys_hdr__size(diag204_info_type
);
721 for (i
= 0; i
< phys_hdr__cpus(diag204_info_type
, phys_hdr
); i
++) {
723 rc
= hypfs_create_phys_cpu_files(sb
, cpus_dir
, cpu_info
);
726 cpu_info
+= phys_cpu__size(diag204_info_type
);
731 int hypfs_diag_create_files(struct super_block
*sb
, struct dentry
*root
)
733 struct dentry
*systems_dir
, *hyp_dir
;
734 void *time_hdr
, *part_hdr
;
738 buffer
= diag204_store();
740 return PTR_ERR(buffer
);
742 systems_dir
= hypfs_mkdir(sb
, root
, "systems");
743 if (IS_ERR(systems_dir
)) {
744 rc
= PTR_ERR(systems_dir
);
747 time_hdr
= (struct x_info_blk_hdr
*)buffer
;
748 part_hdr
= time_hdr
+ info_blk_hdr__size(diag204_info_type
);
749 for (i
= 0; i
< info_blk_hdr__npar(diag204_info_type
, time_hdr
); i
++) {
750 part_hdr
= hypfs_create_lpar_files(sb
, systems_dir
, part_hdr
);
751 if (IS_ERR(part_hdr
)) {
752 rc
= PTR_ERR(part_hdr
);
756 if (info_blk_hdr__flags(diag204_info_type
, time_hdr
) & LPAR_PHYS_FLG
) {
757 ptr
= hypfs_create_phys_files(sb
, root
, part_hdr
);
763 hyp_dir
= hypfs_mkdir(sb
, root
, "hyp");
764 if (IS_ERR(hyp_dir
)) {
765 rc
= PTR_ERR(hyp_dir
);
768 ptr
= hypfs_create_str(sb
, hyp_dir
, "type", "LPAR Hypervisor");