2 * arch/s390/hypfs/hypfs_diag.c
3 * Hypervisor filesystem for Linux on s390. Diag 204 and 224
6 * Copyright IBM Corp. 2006, 2008
7 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
10 #define KMSG_COMPONENT "hypfs"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/types.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #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 /* diag 204 subcodes */
33 /* The two available diag 204 data formats */
39 /* bit is set in flags, when physical cpu info is included in diag 204 data */
40 #define LPAR_PHYS_FLG 0x80
42 static char *diag224_cpu_names; /* diag 224 name table */
43 static enum diag204_sc diag204_store_sc; /* used subcode for store */
44 static enum diag204_format diag204_info_type; /* used diag 204 data format */
46 static void *diag204_buf; /* 4K aligned buffer for diag204 data */
47 static void *diag204_buf_vmalloc; /* vmalloc pointer for diag204 data */
48 static int diag204_buf_pages; /* number of pages for diag204 data */
51 * DIAG 204 data structures and member access functions.
53 * Since we have two different diag 204 data formats for old and new s390
54 * machines, we do not access the structs directly, but use getter functions for
55 * each struct member instead. This should make the code more readable.
58 /* Time information block */
67 } __attribute__ ((packed));
69 struct x_info_blk_hdr {
78 } __attribute__ ((packed));
80 static inline int info_blk_hdr__size(enum diag204_format type)
82 if (type == INFO_SIMPLE)
83 return sizeof(struct info_blk_hdr);
85 return sizeof(struct x_info_blk_hdr);
88 static inline __u8 info_blk_hdr__npar(enum diag204_format type, void *hdr)
90 if (type == INFO_SIMPLE)
91 return ((struct info_blk_hdr *)hdr)->npar;
93 return ((struct x_info_blk_hdr *)hdr)->npar;
96 static inline __u8 info_blk_hdr__flags(enum diag204_format type, void *hdr)
98 if (type == INFO_SIMPLE)
99 return ((struct info_blk_hdr *)hdr)->flags;
101 return ((struct x_info_blk_hdr *)hdr)->flags;
104 static inline __u16 info_blk_hdr__pcpus(enum diag204_format type, void *hdr)
106 if (type == INFO_SIMPLE)
107 return ((struct info_blk_hdr *)hdr)->phys_cpus;
109 return ((struct x_info_blk_hdr *)hdr)->phys_cpus;
112 /* Partition header */
118 char part_name[LPAR_NAME_LEN];
119 } __attribute__ ((packed));
127 char part_name[LPAR_NAME_LEN];
137 } __attribute__ ((packed));
139 static inline int part_hdr__size(enum diag204_format type)
141 if (type == INFO_SIMPLE)
142 return sizeof(struct part_hdr);
144 return sizeof(struct x_part_hdr);
147 static inline __u8 part_hdr__rcpus(enum diag204_format type, void *hdr)
149 if (type == INFO_SIMPLE)
150 return ((struct part_hdr *)hdr)->cpus;
152 return ((struct x_part_hdr *)hdr)->rcpus;
155 static inline void part_hdr__part_name(enum diag204_format type, void *hdr,
158 if (type == INFO_SIMPLE)
159 memcpy(name, ((struct part_hdr *)hdr)->part_name,
162 memcpy(name, ((struct x_part_hdr *)hdr)->part_name,
164 EBCASC(name, LPAR_NAME_LEN);
165 name[LPAR_NAME_LEN] = 0;
177 } __attribute__ ((packed));
196 } __attribute__ ((packed));
200 static inline int cpu_info__size(enum diag204_format type)
202 if (type == INFO_SIMPLE)
203 return sizeof(struct cpu_info);
205 return sizeof(struct x_cpu_info);
208 static inline __u8 cpu_info__ctidx(enum diag204_format type, void *hdr)
210 if (type == INFO_SIMPLE)
211 return ((struct cpu_info *)hdr)->ctidx;
213 return ((struct x_cpu_info *)hdr)->ctidx;
216 static inline __u16 cpu_info__cpu_addr(enum diag204_format type, void *hdr)
218 if (type == INFO_SIMPLE)
219 return ((struct cpu_info *)hdr)->cpu_addr;
221 return ((struct x_cpu_info *)hdr)->cpu_addr;
224 static inline __u64 cpu_info__acc_time(enum diag204_format type, void *hdr)
226 if (type == INFO_SIMPLE)
227 return ((struct cpu_info *)hdr)->acc_time;
229 return ((struct x_cpu_info *)hdr)->acc_time;
232 static inline __u64 cpu_info__lp_time(enum diag204_format type, void *hdr)
234 if (type == INFO_SIMPLE)
235 return ((struct cpu_info *)hdr)->lp_time;
237 return ((struct x_cpu_info *)hdr)->lp_time;
240 static inline __u64 cpu_info__online_time(enum diag204_format type, void *hdr)
242 if (type == INFO_SIMPLE)
243 return 0; /* online_time not available in simple info */
245 return ((struct x_cpu_info *)hdr)->online_time;
248 /* Physical header */
255 } __attribute__ ((packed));
263 } __attribute__ ((packed));
265 static inline int phys_hdr__size(enum diag204_format type)
267 if (type == INFO_SIMPLE)
268 return sizeof(struct phys_hdr);
270 return sizeof(struct x_phys_hdr);
273 static inline __u8 phys_hdr__cpus(enum diag204_format type, void *hdr)
275 if (type == INFO_SIMPLE)
276 return ((struct phys_hdr *)hdr)->cpus;
278 return ((struct x_phys_hdr *)hdr)->cpus;
281 /* Physical CPU info block */
290 } __attribute__ ((packed));
299 } __attribute__ ((packed));
301 static inline int phys_cpu__size(enum diag204_format type)
303 if (type == INFO_SIMPLE)
304 return sizeof(struct phys_cpu);
306 return sizeof(struct x_phys_cpu);
309 static inline __u16 phys_cpu__cpu_addr(enum diag204_format type, void *hdr)
311 if (type == INFO_SIMPLE)
312 return ((struct phys_cpu *)hdr)->cpu_addr;
314 return ((struct x_phys_cpu *)hdr)->cpu_addr;
317 static inline __u64 phys_cpu__mgm_time(enum diag204_format type, void *hdr)
319 if (type == INFO_SIMPLE)
320 return ((struct phys_cpu *)hdr)->mgm_time;
322 return ((struct x_phys_cpu *)hdr)->mgm_time;
325 static inline __u64 phys_cpu__ctidx(enum diag204_format type, void *hdr)
327 if (type == INFO_SIMPLE)
328 return ((struct phys_cpu *)hdr)->ctidx;
330 return ((struct x_phys_cpu *)hdr)->ctidx;
333 /* Diagnose 204 functions */
335 static int diag204(unsigned long subcode, unsigned long size, void *addr)
337 register unsigned long _subcode asm("0") = subcode;
338 register unsigned long _size asm("1") = size;
341 " diag %2,%0,0x204\n"
344 : "+d" (_subcode), "+d" (_size) : "d" (addr) : "memory");
351 * For the old diag subcode 4 with simple data format we have to use real
352 * memory. If we use subcode 6 or 7 with extended data format, we can (and
353 * should) use vmalloc, since we need a lot of memory in that case. Currently
357 static void diag204_free_buffer(void)
361 if (diag204_buf_vmalloc) {
362 vfree(diag204_buf_vmalloc);
363 diag204_buf_vmalloc = NULL;
365 free_pages((unsigned long) diag204_buf, 0);
367 diag204_buf_pages = 0;
371 static void *diag204_alloc_vbuf(int pages)
373 /* The buffer has to be page aligned! */
374 diag204_buf_vmalloc = vmalloc(PAGE_SIZE * (pages + 1));
375 if (!diag204_buf_vmalloc)
376 return ERR_PTR(-ENOMEM);
377 diag204_buf = (void*)((unsigned long)diag204_buf_vmalloc
378 & ~0xfffUL) + 0x1000;
379 diag204_buf_pages = pages;
383 static void *diag204_alloc_rbuf(void)
385 diag204_buf = (void*)__get_free_pages(GFP_KERNEL,0);
387 return ERR_PTR(-ENOMEM);
388 diag204_buf_pages = 1;
392 static void *diag204_get_buffer(enum diag204_format fmt, int *pages)
395 *pages = diag204_buf_pages;
398 if (fmt == INFO_SIMPLE) {
400 return diag204_alloc_rbuf();
401 } else {/* INFO_EXT */
402 *pages = diag204((unsigned long)SUBC_RSI |
403 (unsigned long)INFO_EXT, 0, NULL);
405 return ERR_PTR(-ENOSYS);
407 return diag204_alloc_vbuf(*pages);
412 * diag204_probe() has to find out, which type of diagnose 204 implementation
413 * we have on our machine. Currently there are three possible scanarios:
414 * - subcode 4 + simple data format (only one page)
415 * - subcode 4-6 + extended data format
416 * - subcode 4-7 + extended data format
418 * Subcode 5 is used to retrieve the size of the data, provided by subcodes
419 * 6 and 7. Subcode 7 basically has the same function as subcode 6. In addition
420 * to subcode 6 it provides also information about secondary cpus.
421 * In order to get as much information as possible, we first try
422 * subcode 7, then 6 and if both fail, we use subcode 4.
425 static int diag204_probe(void)
430 buf = diag204_get_buffer(INFO_EXT, &pages);
432 if (diag204((unsigned long)SUBC_STIB7 |
433 (unsigned long)INFO_EXT, pages, buf) >= 0) {
434 diag204_store_sc = SUBC_STIB7;
435 diag204_info_type = INFO_EXT;
438 if (diag204((unsigned long)SUBC_STIB6 |
439 (unsigned long)INFO_EXT, pages, buf) >= 0) {
440 diag204_store_sc = SUBC_STIB6;
441 diag204_info_type = INFO_EXT;
444 diag204_free_buffer();
447 /* subcodes 6 and 7 failed, now try subcode 4 */
449 buf = diag204_get_buffer(INFO_SIMPLE, &pages);
454 if (diag204((unsigned long)SUBC_STIB4 |
455 (unsigned long)INFO_SIMPLE, pages, buf) >= 0) {
456 diag204_store_sc = SUBC_STIB4;
457 diag204_info_type = INFO_SIMPLE;
466 diag204_free_buffer();
471 static void *diag204_store(void)
476 buf = diag204_get_buffer(diag204_info_type, &pages);
479 if (diag204((unsigned long)diag204_store_sc |
480 (unsigned long)diag204_info_type, pages, buf) < 0)
481 return ERR_PTR(-ENOSYS);
486 /* Diagnose 224 functions */
488 static int diag224(void *ptr)
490 int rc = -EOPNOTSUPP;
493 " diag %1,%2,0x224\n"
497 : "+d" (rc) :"d" (0), "d" (ptr) : "memory");
501 static int diag224_get_name_table(void)
503 /* memory must be below 2GB */
504 diag224_cpu_names = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
505 if (!diag224_cpu_names)
507 if (diag224(diag224_cpu_names)) {
508 kfree(diag224_cpu_names);
511 EBCASC(diag224_cpu_names + 16, (*diag224_cpu_names + 1) * 16);
515 static void diag224_delete_name_table(void)
517 kfree(diag224_cpu_names);
520 static int diag224_idx2name(int index, char *name)
522 memcpy(name, diag224_cpu_names + ((index + 1) * CPU_NAME_LEN),
524 name[CPU_NAME_LEN] = 0;
529 __init int hypfs_diag_init(void)
533 if (diag204_probe()) {
534 pr_err("The hardware system does not support hypfs\n");
537 rc = diag224_get_name_table();
539 diag204_free_buffer();
540 pr_err("The hardware system does not provide all "
541 "functions required by hypfs\n");
546 void hypfs_diag_exit(void)
548 diag224_delete_name_table();
549 diag204_free_buffer();
553 * Functions to create the directory structure
554 * *******************************************
557 static int hypfs_create_cpu_files(struct super_block *sb,
558 struct dentry *cpus_dir, void *cpu_info)
560 struct dentry *cpu_dir;
561 char buffer[TMP_SIZE];
564 snprintf(buffer, TMP_SIZE, "%d", cpu_info__cpu_addr(diag204_info_type,
566 cpu_dir = hypfs_mkdir(sb, cpus_dir, buffer);
567 rc = hypfs_create_u64(sb, cpu_dir, "mgmtime",
568 cpu_info__acc_time(diag204_info_type, cpu_info) -
569 cpu_info__lp_time(diag204_info_type, cpu_info));
572 rc = hypfs_create_u64(sb, cpu_dir, "cputime",
573 cpu_info__lp_time(diag204_info_type, cpu_info));
576 if (diag204_info_type == INFO_EXT) {
577 rc = hypfs_create_u64(sb, cpu_dir, "onlinetime",
578 cpu_info__online_time(diag204_info_type,
583 diag224_idx2name(cpu_info__ctidx(diag204_info_type, cpu_info), buffer);
584 rc = hypfs_create_str(sb, cpu_dir, "type", buffer);
590 static void *hypfs_create_lpar_files(struct super_block *sb,
591 struct dentry *systems_dir, void *part_hdr)
593 struct dentry *cpus_dir;
594 struct dentry *lpar_dir;
595 char lpar_name[LPAR_NAME_LEN + 1];
599 part_hdr__part_name(diag204_info_type, part_hdr, lpar_name);
600 lpar_name[LPAR_NAME_LEN] = 0;
601 lpar_dir = hypfs_mkdir(sb, systems_dir, lpar_name);
602 if (IS_ERR(lpar_dir))
604 cpus_dir = hypfs_mkdir(sb, lpar_dir, "cpus");
605 if (IS_ERR(cpus_dir))
607 cpu_info = part_hdr + part_hdr__size(diag204_info_type);
608 for (i = 0; i < part_hdr__rcpus(diag204_info_type, part_hdr); i++) {
610 rc = hypfs_create_cpu_files(sb, cpus_dir, cpu_info);
613 cpu_info += cpu_info__size(diag204_info_type);
618 static int hypfs_create_phys_cpu_files(struct super_block *sb,
619 struct dentry *cpus_dir, void *cpu_info)
621 struct dentry *cpu_dir;
622 char buffer[TMP_SIZE];
625 snprintf(buffer, TMP_SIZE, "%i", phys_cpu__cpu_addr(diag204_info_type,
627 cpu_dir = hypfs_mkdir(sb, cpus_dir, buffer);
629 return PTR_ERR(cpu_dir);
630 rc = hypfs_create_u64(sb, cpu_dir, "mgmtime",
631 phys_cpu__mgm_time(diag204_info_type, cpu_info));
634 diag224_idx2name(phys_cpu__ctidx(diag204_info_type, cpu_info), buffer);
635 rc = hypfs_create_str(sb, cpu_dir, "type", buffer);
641 static void *hypfs_create_phys_files(struct super_block *sb,
642 struct dentry *parent_dir, void *phys_hdr)
646 struct dentry *cpus_dir;
648 cpus_dir = hypfs_mkdir(sb, parent_dir, "cpus");
649 if (IS_ERR(cpus_dir))
651 cpu_info = phys_hdr + phys_hdr__size(diag204_info_type);
652 for (i = 0; i < phys_hdr__cpus(diag204_info_type, phys_hdr); i++) {
654 rc = hypfs_create_phys_cpu_files(sb, cpus_dir, cpu_info);
657 cpu_info += phys_cpu__size(diag204_info_type);
662 int hypfs_diag_create_files(struct super_block *sb, struct dentry *root)
664 struct dentry *systems_dir, *hyp_dir;
665 void *time_hdr, *part_hdr;
669 buffer = diag204_store();
671 return PTR_ERR(buffer);
673 systems_dir = hypfs_mkdir(sb, root, "systems");
674 if (IS_ERR(systems_dir)) {
675 rc = PTR_ERR(systems_dir);
678 time_hdr = (struct x_info_blk_hdr *)buffer;
679 part_hdr = time_hdr + info_blk_hdr__size(diag204_info_type);
680 for (i = 0; i < info_blk_hdr__npar(diag204_info_type, time_hdr); i++) {
681 part_hdr = hypfs_create_lpar_files(sb, systems_dir, part_hdr);
682 if (IS_ERR(part_hdr)) {
683 rc = PTR_ERR(part_hdr);
687 if (info_blk_hdr__flags(diag204_info_type, time_hdr) & LPAR_PHYS_FLG) {
688 ptr = hypfs_create_phys_files(sb, root, part_hdr);
694 hyp_dir = hypfs_mkdir(sb, root, "hyp");
695 if (IS_ERR(hyp_dir)) {
696 rc = PTR_ERR(hyp_dir);
699 ptr = hypfs_create_str(sb, hyp_dir, "type", "LPAR Hypervisor");