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/gfp.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/vmalloc.h>
19 #include <asm/ebcdic.h>
22 #define LPAR_NAME_LEN 8 /* lpar name len in diag 204 data */
23 #define CPU_NAME_LEN 16 /* type name len of cpus in diag224 name table */
24 #define TMP_SIZE 64 /* size of temporary buffers */
26 /* diag 204 subcodes */
34 /* The two available diag 204 data formats */
40 /* bit is set in flags, when physical cpu info is included in diag 204 data */
41 #define LPAR_PHYS_FLG 0x80
43 static char *diag224_cpu_names; /* diag 224 name table */
44 static enum diag204_sc diag204_store_sc; /* used subcode for store */
45 static enum diag204_format diag204_info_type; /* used diag 204 data format */
47 static void *diag204_buf; /* 4K aligned buffer for diag204 data */
48 static void *diag204_buf_vmalloc; /* vmalloc pointer for diag204 data */
49 static int diag204_buf_pages; /* number of pages for diag204 data */
52 * DIAG 204 data structures and member access functions.
54 * Since we have two different diag 204 data formats for old and new s390
55 * machines, we do not access the structs directly, but use getter functions for
56 * each struct member instead. This should make the code more readable.
59 /* Time information block */
68 } __attribute__ ((packed));
70 struct x_info_blk_hdr {
79 } __attribute__ ((packed));
81 static inline int info_blk_hdr__size(enum diag204_format type)
83 if (type == INFO_SIMPLE)
84 return sizeof(struct info_blk_hdr);
86 return sizeof(struct x_info_blk_hdr);
89 static inline __u8 info_blk_hdr__npar(enum diag204_format type, void *hdr)
91 if (type == INFO_SIMPLE)
92 return ((struct info_blk_hdr *)hdr)->npar;
94 return ((struct x_info_blk_hdr *)hdr)->npar;
97 static inline __u8 info_blk_hdr__flags(enum diag204_format type, void *hdr)
99 if (type == INFO_SIMPLE)
100 return ((struct info_blk_hdr *)hdr)->flags;
102 return ((struct x_info_blk_hdr *)hdr)->flags;
105 static inline __u16 info_blk_hdr__pcpus(enum diag204_format type, void *hdr)
107 if (type == INFO_SIMPLE)
108 return ((struct info_blk_hdr *)hdr)->phys_cpus;
110 return ((struct x_info_blk_hdr *)hdr)->phys_cpus;
113 /* Partition header */
119 char part_name[LPAR_NAME_LEN];
120 } __attribute__ ((packed));
128 char part_name[LPAR_NAME_LEN];
138 } __attribute__ ((packed));
140 static inline int part_hdr__size(enum diag204_format type)
142 if (type == INFO_SIMPLE)
143 return sizeof(struct part_hdr);
145 return sizeof(struct x_part_hdr);
148 static inline __u8 part_hdr__rcpus(enum diag204_format type, void *hdr)
150 if (type == INFO_SIMPLE)
151 return ((struct part_hdr *)hdr)->cpus;
153 return ((struct x_part_hdr *)hdr)->rcpus;
156 static inline void part_hdr__part_name(enum diag204_format type, void *hdr,
159 if (type == INFO_SIMPLE)
160 memcpy(name, ((struct part_hdr *)hdr)->part_name,
163 memcpy(name, ((struct x_part_hdr *)hdr)->part_name,
165 EBCASC(name, LPAR_NAME_LEN);
166 name[LPAR_NAME_LEN] = 0;
178 } __attribute__ ((packed));
197 } __attribute__ ((packed));
201 static inline int cpu_info__size(enum diag204_format type)
203 if (type == INFO_SIMPLE)
204 return sizeof(struct cpu_info);
206 return sizeof(struct x_cpu_info);
209 static inline __u8 cpu_info__ctidx(enum diag204_format type, void *hdr)
211 if (type == INFO_SIMPLE)
212 return ((struct cpu_info *)hdr)->ctidx;
214 return ((struct x_cpu_info *)hdr)->ctidx;
217 static inline __u16 cpu_info__cpu_addr(enum diag204_format type, void *hdr)
219 if (type == INFO_SIMPLE)
220 return ((struct cpu_info *)hdr)->cpu_addr;
222 return ((struct x_cpu_info *)hdr)->cpu_addr;
225 static inline __u64 cpu_info__acc_time(enum diag204_format type, void *hdr)
227 if (type == INFO_SIMPLE)
228 return ((struct cpu_info *)hdr)->acc_time;
230 return ((struct x_cpu_info *)hdr)->acc_time;
233 static inline __u64 cpu_info__lp_time(enum diag204_format type, void *hdr)
235 if (type == INFO_SIMPLE)
236 return ((struct cpu_info *)hdr)->lp_time;
238 return ((struct x_cpu_info *)hdr)->lp_time;
241 static inline __u64 cpu_info__online_time(enum diag204_format type, void *hdr)
243 if (type == INFO_SIMPLE)
244 return 0; /* online_time not available in simple info */
246 return ((struct x_cpu_info *)hdr)->online_time;
249 /* Physical header */
256 } __attribute__ ((packed));
264 } __attribute__ ((packed));
266 static inline int phys_hdr__size(enum diag204_format type)
268 if (type == INFO_SIMPLE)
269 return sizeof(struct phys_hdr);
271 return sizeof(struct x_phys_hdr);
274 static inline __u8 phys_hdr__cpus(enum diag204_format type, void *hdr)
276 if (type == INFO_SIMPLE)
277 return ((struct phys_hdr *)hdr)->cpus;
279 return ((struct x_phys_hdr *)hdr)->cpus;
282 /* Physical CPU info block */
291 } __attribute__ ((packed));
300 } __attribute__ ((packed));
302 static inline int phys_cpu__size(enum diag204_format type)
304 if (type == INFO_SIMPLE)
305 return sizeof(struct phys_cpu);
307 return sizeof(struct x_phys_cpu);
310 static inline __u16 phys_cpu__cpu_addr(enum diag204_format type, void *hdr)
312 if (type == INFO_SIMPLE)
313 return ((struct phys_cpu *)hdr)->cpu_addr;
315 return ((struct x_phys_cpu *)hdr)->cpu_addr;
318 static inline __u64 phys_cpu__mgm_time(enum diag204_format type, void *hdr)
320 if (type == INFO_SIMPLE)
321 return ((struct phys_cpu *)hdr)->mgm_time;
323 return ((struct x_phys_cpu *)hdr)->mgm_time;
326 static inline __u64 phys_cpu__ctidx(enum diag204_format type, void *hdr)
328 if (type == INFO_SIMPLE)
329 return ((struct phys_cpu *)hdr)->ctidx;
331 return ((struct x_phys_cpu *)hdr)->ctidx;
334 /* Diagnose 204 functions */
336 static int diag204(unsigned long subcode, unsigned long size, void *addr)
338 register unsigned long _subcode asm("0") = subcode;
339 register unsigned long _size asm("1") = size;
342 " diag %2,%0,0x204\n"
345 : "+d" (_subcode), "+d" (_size) : "d" (addr) : "memory");
352 * For the old diag subcode 4 with simple data format we have to use real
353 * memory. If we use subcode 6 or 7 with extended data format, we can (and
354 * should) use vmalloc, since we need a lot of memory in that case. Currently
358 static void diag204_free_buffer(void)
362 if (diag204_buf_vmalloc) {
363 vfree(diag204_buf_vmalloc);
364 diag204_buf_vmalloc = NULL;
366 free_pages((unsigned long) diag204_buf, 0);
368 diag204_buf_pages = 0;
372 static void *diag204_alloc_vbuf(int pages)
374 /* The buffer has to be page aligned! */
375 diag204_buf_vmalloc = vmalloc(PAGE_SIZE * (pages + 1));
376 if (!diag204_buf_vmalloc)
377 return ERR_PTR(-ENOMEM);
378 diag204_buf = (void*)((unsigned long)diag204_buf_vmalloc
379 & ~0xfffUL) + 0x1000;
380 diag204_buf_pages = pages;
384 static void *diag204_alloc_rbuf(void)
386 diag204_buf = (void*)__get_free_pages(GFP_KERNEL,0);
388 return ERR_PTR(-ENOMEM);
389 diag204_buf_pages = 1;
393 static void *diag204_get_buffer(enum diag204_format fmt, int *pages)
396 *pages = diag204_buf_pages;
399 if (fmt == INFO_SIMPLE) {
401 return diag204_alloc_rbuf();
402 } else {/* INFO_EXT */
403 *pages = diag204((unsigned long)SUBC_RSI |
404 (unsigned long)INFO_EXT, 0, NULL);
406 return ERR_PTR(-ENOSYS);
408 return diag204_alloc_vbuf(*pages);
413 * diag204_probe() has to find out, which type of diagnose 204 implementation
414 * we have on our machine. Currently there are three possible scanarios:
415 * - subcode 4 + simple data format (only one page)
416 * - subcode 4-6 + extended data format
417 * - subcode 4-7 + extended data format
419 * Subcode 5 is used to retrieve the size of the data, provided by subcodes
420 * 6 and 7. Subcode 7 basically has the same function as subcode 6. In addition
421 * to subcode 6 it provides also information about secondary cpus.
422 * In order to get as much information as possible, we first try
423 * subcode 7, then 6 and if both fail, we use subcode 4.
426 static int diag204_probe(void)
431 buf = diag204_get_buffer(INFO_EXT, &pages);
433 if (diag204((unsigned long)SUBC_STIB7 |
434 (unsigned long)INFO_EXT, pages, buf) >= 0) {
435 diag204_store_sc = SUBC_STIB7;
436 diag204_info_type = INFO_EXT;
439 if (diag204((unsigned long)SUBC_STIB6 |
440 (unsigned long)INFO_EXT, pages, buf) >= 0) {
441 diag204_store_sc = SUBC_STIB7;
442 diag204_info_type = INFO_EXT;
445 diag204_free_buffer();
448 /* subcodes 6 and 7 failed, now try subcode 4 */
450 buf = diag204_get_buffer(INFO_SIMPLE, &pages);
455 if (diag204((unsigned long)SUBC_STIB4 |
456 (unsigned long)INFO_SIMPLE, pages, buf) >= 0) {
457 diag204_store_sc = SUBC_STIB4;
458 diag204_info_type = INFO_SIMPLE;
467 diag204_free_buffer();
472 static void *diag204_store(void)
477 buf = diag204_get_buffer(diag204_info_type, &pages);
480 if (diag204((unsigned long)diag204_store_sc |
481 (unsigned long)diag204_info_type, pages, buf) < 0)
482 return ERR_PTR(-ENOSYS);
487 /* Diagnose 224 functions */
489 static int diag224(void *ptr)
494 " diag %1,%2,0x224\n"
498 : "+d" (rc) :"d" (0), "d" (ptr) : "memory");
502 static int diag224_get_name_table(void)
504 /* memory must be below 2GB */
505 diag224_cpu_names = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
506 if (!diag224_cpu_names)
508 if (diag224(diag224_cpu_names)) {
509 kfree(diag224_cpu_names);
512 EBCASC(diag224_cpu_names + 16, (*diag224_cpu_names + 1) * 16);
516 static void diag224_delete_name_table(void)
518 kfree(diag224_cpu_names);
521 static int diag224_idx2name(int index, char *name)
523 memcpy(name, diag224_cpu_names + ((index + 1) * CPU_NAME_LEN),
525 name[CPU_NAME_LEN] = 0;
530 __init int hypfs_diag_init(void)
534 if (diag204_probe()) {
535 pr_err("The hardware system does not support hypfs\n");
538 rc = diag224_get_name_table();
540 diag204_free_buffer();
541 pr_err("The hardware system does not provide all "
542 "functions required by hypfs\n");
547 void hypfs_diag_exit(void)
549 diag224_delete_name_table();
550 diag204_free_buffer();
554 * Functions to create the directory structure
555 * *******************************************
558 static int hypfs_create_cpu_files(struct super_block *sb,
559 struct dentry *cpus_dir, void *cpu_info)
561 struct dentry *cpu_dir;
562 char buffer[TMP_SIZE];
565 snprintf(buffer, TMP_SIZE, "%d", cpu_info__cpu_addr(diag204_info_type,
567 cpu_dir = hypfs_mkdir(sb, cpus_dir, buffer);
568 rc = hypfs_create_u64(sb, cpu_dir, "mgmtime",
569 cpu_info__acc_time(diag204_info_type, cpu_info) -
570 cpu_info__lp_time(diag204_info_type, cpu_info));
573 rc = hypfs_create_u64(sb, cpu_dir, "cputime",
574 cpu_info__lp_time(diag204_info_type, cpu_info));
577 if (diag204_info_type == INFO_EXT) {
578 rc = hypfs_create_u64(sb, cpu_dir, "onlinetime",
579 cpu_info__online_time(diag204_info_type,
584 diag224_idx2name(cpu_info__ctidx(diag204_info_type, cpu_info), buffer);
585 rc = hypfs_create_str(sb, cpu_dir, "type", buffer);
591 static void *hypfs_create_lpar_files(struct super_block *sb,
592 struct dentry *systems_dir, void *part_hdr)
594 struct dentry *cpus_dir;
595 struct dentry *lpar_dir;
596 char lpar_name[LPAR_NAME_LEN + 1];
600 part_hdr__part_name(diag204_info_type, part_hdr, lpar_name);
601 lpar_name[LPAR_NAME_LEN] = 0;
602 lpar_dir = hypfs_mkdir(sb, systems_dir, lpar_name);
603 if (IS_ERR(lpar_dir))
605 cpus_dir = hypfs_mkdir(sb, lpar_dir, "cpus");
606 if (IS_ERR(cpus_dir))
608 cpu_info = part_hdr + part_hdr__size(diag204_info_type);
609 for (i = 0; i < part_hdr__rcpus(diag204_info_type, part_hdr); i++) {
611 rc = hypfs_create_cpu_files(sb, cpus_dir, cpu_info);
614 cpu_info += cpu_info__size(diag204_info_type);
619 static int hypfs_create_phys_cpu_files(struct super_block *sb,
620 struct dentry *cpus_dir, void *cpu_info)
622 struct dentry *cpu_dir;
623 char buffer[TMP_SIZE];
626 snprintf(buffer, TMP_SIZE, "%i", phys_cpu__cpu_addr(diag204_info_type,
628 cpu_dir = hypfs_mkdir(sb, cpus_dir, buffer);
630 return PTR_ERR(cpu_dir);
631 rc = hypfs_create_u64(sb, cpu_dir, "mgmtime",
632 phys_cpu__mgm_time(diag204_info_type, cpu_info));
635 diag224_idx2name(phys_cpu__ctidx(diag204_info_type, cpu_info), buffer);
636 rc = hypfs_create_str(sb, cpu_dir, "type", buffer);
642 static void *hypfs_create_phys_files(struct super_block *sb,
643 struct dentry *parent_dir, void *phys_hdr)
647 struct dentry *cpus_dir;
649 cpus_dir = hypfs_mkdir(sb, parent_dir, "cpus");
650 if (IS_ERR(cpus_dir))
652 cpu_info = phys_hdr + phys_hdr__size(diag204_info_type);
653 for (i = 0; i < phys_hdr__cpus(diag204_info_type, phys_hdr); i++) {
655 rc = hypfs_create_phys_cpu_files(sb, cpus_dir, cpu_info);
658 cpu_info += phys_cpu__size(diag204_info_type);
663 int hypfs_diag_create_files(struct super_block *sb, struct dentry *root)
665 struct dentry *systems_dir, *hyp_dir;
666 void *time_hdr, *part_hdr;
670 buffer = diag204_store();
672 return PTR_ERR(buffer);
674 systems_dir = hypfs_mkdir(sb, root, "systems");
675 if (IS_ERR(systems_dir)) {
676 rc = PTR_ERR(systems_dir);
679 time_hdr = (struct x_info_blk_hdr *)buffer;
680 part_hdr = time_hdr + info_blk_hdr__size(diag204_info_type);
681 for (i = 0; i < info_blk_hdr__npar(diag204_info_type, time_hdr); i++) {
682 part_hdr = hypfs_create_lpar_files(sb, systems_dir, part_hdr);
683 if (IS_ERR(part_hdr)) {
684 rc = PTR_ERR(part_hdr);
688 if (info_blk_hdr__flags(diag204_info_type, time_hdr) & LPAR_PHYS_FLG) {
689 ptr = hypfs_create_phys_files(sb, root, part_hdr);
695 hyp_dir = hypfs_mkdir(sb, root, "hyp");
696 if (IS_ERR(hyp_dir)) {
697 rc = PTR_ERR(hyp_dir);
700 ptr = hypfs_create_str(sb, hyp_dir, "type", "LPAR Hypervisor");