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>
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 #define DBFS_D204_HDR_VERSION 0
28 /* diag 204 subcodes */
36 /* The two available diag 204 data formats */
42 /* bit is set in flags, when physical cpu info is included in diag 204 data */
43 #define LPAR_PHYS_FLG 0x80
45 static char *diag224_cpu_names; /* diag 224 name table */
46 static enum diag204_sc diag204_store_sc; /* used subcode for store */
47 static enum diag204_format diag204_info_type; /* used diag 204 data format */
49 static void *diag204_buf; /* 4K aligned buffer for diag204 data */
50 static void *diag204_buf_vmalloc; /* vmalloc pointer for diag204 data */
51 static int diag204_buf_pages; /* number of pages for diag204 data */
53 static struct dentry *dbfs_d204_file;
56 * DIAG 204 data structures and member access functions.
58 * Since we have two different diag 204 data formats for old and new s390
59 * machines, we do not access the structs directly, but use getter functions for
60 * each struct member instead. This should make the code more readable.
63 /* Time information block */
72 } __attribute__ ((packed));
74 struct x_info_blk_hdr {
83 } __attribute__ ((packed));
85 static inline int info_blk_hdr__size(enum diag204_format type)
87 if (type == INFO_SIMPLE)
88 return sizeof(struct info_blk_hdr);
90 return sizeof(struct x_info_blk_hdr);
93 static inline __u8 info_blk_hdr__npar(enum diag204_format type, void *hdr)
95 if (type == INFO_SIMPLE)
96 return ((struct info_blk_hdr *)hdr)->npar;
98 return ((struct x_info_blk_hdr *)hdr)->npar;
101 static inline __u8 info_blk_hdr__flags(enum diag204_format type, void *hdr)
103 if (type == INFO_SIMPLE)
104 return ((struct info_blk_hdr *)hdr)->flags;
106 return ((struct x_info_blk_hdr *)hdr)->flags;
109 static inline __u16 info_blk_hdr__pcpus(enum diag204_format type, void *hdr)
111 if (type == INFO_SIMPLE)
112 return ((struct info_blk_hdr *)hdr)->phys_cpus;
114 return ((struct x_info_blk_hdr *)hdr)->phys_cpus;
117 /* Partition header */
123 char part_name[LPAR_NAME_LEN];
124 } __attribute__ ((packed));
132 char part_name[LPAR_NAME_LEN];
142 } __attribute__ ((packed));
144 static inline int part_hdr__size(enum diag204_format type)
146 if (type == INFO_SIMPLE)
147 return sizeof(struct part_hdr);
149 return sizeof(struct x_part_hdr);
152 static inline __u8 part_hdr__rcpus(enum diag204_format type, void *hdr)
154 if (type == INFO_SIMPLE)
155 return ((struct part_hdr *)hdr)->cpus;
157 return ((struct x_part_hdr *)hdr)->rcpus;
160 static inline void part_hdr__part_name(enum diag204_format type, void *hdr,
163 if (type == INFO_SIMPLE)
164 memcpy(name, ((struct part_hdr *)hdr)->part_name,
167 memcpy(name, ((struct x_part_hdr *)hdr)->part_name,
169 EBCASC(name, LPAR_NAME_LEN);
170 name[LPAR_NAME_LEN] = 0;
182 } __attribute__ ((packed));
201 } __attribute__ ((packed));
205 static inline int cpu_info__size(enum diag204_format type)
207 if (type == INFO_SIMPLE)
208 return sizeof(struct cpu_info);
210 return sizeof(struct x_cpu_info);
213 static inline __u8 cpu_info__ctidx(enum diag204_format type, void *hdr)
215 if (type == INFO_SIMPLE)
216 return ((struct cpu_info *)hdr)->ctidx;
218 return ((struct x_cpu_info *)hdr)->ctidx;
221 static inline __u16 cpu_info__cpu_addr(enum diag204_format type, void *hdr)
223 if (type == INFO_SIMPLE)
224 return ((struct cpu_info *)hdr)->cpu_addr;
226 return ((struct x_cpu_info *)hdr)->cpu_addr;
229 static inline __u64 cpu_info__acc_time(enum diag204_format type, void *hdr)
231 if (type == INFO_SIMPLE)
232 return ((struct cpu_info *)hdr)->acc_time;
234 return ((struct x_cpu_info *)hdr)->acc_time;
237 static inline __u64 cpu_info__lp_time(enum diag204_format type, void *hdr)
239 if (type == INFO_SIMPLE)
240 return ((struct cpu_info *)hdr)->lp_time;
242 return ((struct x_cpu_info *)hdr)->lp_time;
245 static inline __u64 cpu_info__online_time(enum diag204_format type, void *hdr)
247 if (type == INFO_SIMPLE)
248 return 0; /* online_time not available in simple info */
250 return ((struct x_cpu_info *)hdr)->online_time;
253 /* Physical header */
260 } __attribute__ ((packed));
268 } __attribute__ ((packed));
270 static inline int phys_hdr__size(enum diag204_format type)
272 if (type == INFO_SIMPLE)
273 return sizeof(struct phys_hdr);
275 return sizeof(struct x_phys_hdr);
278 static inline __u8 phys_hdr__cpus(enum diag204_format type, void *hdr)
280 if (type == INFO_SIMPLE)
281 return ((struct phys_hdr *)hdr)->cpus;
283 return ((struct x_phys_hdr *)hdr)->cpus;
286 /* Physical CPU info block */
295 } __attribute__ ((packed));
304 } __attribute__ ((packed));
306 static inline int phys_cpu__size(enum diag204_format type)
308 if (type == INFO_SIMPLE)
309 return sizeof(struct phys_cpu);
311 return sizeof(struct x_phys_cpu);
314 static inline __u16 phys_cpu__cpu_addr(enum diag204_format type, void *hdr)
316 if (type == INFO_SIMPLE)
317 return ((struct phys_cpu *)hdr)->cpu_addr;
319 return ((struct x_phys_cpu *)hdr)->cpu_addr;
322 static inline __u64 phys_cpu__mgm_time(enum diag204_format type, void *hdr)
324 if (type == INFO_SIMPLE)
325 return ((struct phys_cpu *)hdr)->mgm_time;
327 return ((struct x_phys_cpu *)hdr)->mgm_time;
330 static inline __u64 phys_cpu__ctidx(enum diag204_format type, void *hdr)
332 if (type == INFO_SIMPLE)
333 return ((struct phys_cpu *)hdr)->ctidx;
335 return ((struct x_phys_cpu *)hdr)->ctidx;
338 /* Diagnose 204 functions */
340 static int diag204(unsigned long subcode, unsigned long size, void *addr)
342 register unsigned long _subcode asm("0") = subcode;
343 register unsigned long _size asm("1") = size;
346 " diag %2,%0,0x204\n"
349 : "+d" (_subcode), "+d" (_size) : "d" (addr) : "memory");
356 * For the old diag subcode 4 with simple data format we have to use real
357 * memory. If we use subcode 6 or 7 with extended data format, we can (and
358 * should) use vmalloc, since we need a lot of memory in that case. Currently
362 static void diag204_free_buffer(void)
366 if (diag204_buf_vmalloc) {
367 vfree(diag204_buf_vmalloc);
368 diag204_buf_vmalloc = NULL;
370 free_pages((unsigned long) diag204_buf, 0);
375 static void *page_align_ptr(void *ptr)
377 return (void *) PAGE_ALIGN((unsigned long) ptr);
380 static void *diag204_alloc_vbuf(int pages)
382 /* The buffer has to be page aligned! */
383 diag204_buf_vmalloc = vmalloc(PAGE_SIZE * (pages + 1));
384 if (!diag204_buf_vmalloc)
385 return ERR_PTR(-ENOMEM);
386 diag204_buf = page_align_ptr(diag204_buf_vmalloc);
387 diag204_buf_pages = pages;
391 static void *diag204_alloc_rbuf(void)
393 diag204_buf = (void*)__get_free_pages(GFP_KERNEL,0);
395 return ERR_PTR(-ENOMEM);
396 diag204_buf_pages = 1;
400 static void *diag204_get_buffer(enum diag204_format fmt, int *pages)
403 *pages = diag204_buf_pages;
406 if (fmt == INFO_SIMPLE) {
408 return diag204_alloc_rbuf();
409 } else {/* INFO_EXT */
410 *pages = diag204((unsigned long)SUBC_RSI |
411 (unsigned long)INFO_EXT, 0, NULL);
413 return ERR_PTR(-ENOSYS);
415 return diag204_alloc_vbuf(*pages);
420 * diag204_probe() has to find out, which type of diagnose 204 implementation
421 * we have on our machine. Currently there are three possible scanarios:
422 * - subcode 4 + simple data format (only one page)
423 * - subcode 4-6 + extended data format
424 * - subcode 4-7 + extended data format
426 * Subcode 5 is used to retrieve the size of the data, provided by subcodes
427 * 6 and 7. Subcode 7 basically has the same function as subcode 6. In addition
428 * to subcode 6 it provides also information about secondary cpus.
429 * In order to get as much information as possible, we first try
430 * subcode 7, then 6 and if both fail, we use subcode 4.
433 static int diag204_probe(void)
438 buf = diag204_get_buffer(INFO_EXT, &pages);
440 if (diag204((unsigned long)SUBC_STIB7 |
441 (unsigned long)INFO_EXT, pages, buf) >= 0) {
442 diag204_store_sc = SUBC_STIB7;
443 diag204_info_type = INFO_EXT;
446 if (diag204((unsigned long)SUBC_STIB6 |
447 (unsigned long)INFO_EXT, pages, buf) >= 0) {
448 diag204_store_sc = SUBC_STIB6;
449 diag204_info_type = INFO_EXT;
452 diag204_free_buffer();
455 /* subcodes 6 and 7 failed, now try subcode 4 */
457 buf = diag204_get_buffer(INFO_SIMPLE, &pages);
462 if (diag204((unsigned long)SUBC_STIB4 |
463 (unsigned long)INFO_SIMPLE, pages, buf) >= 0) {
464 diag204_store_sc = SUBC_STIB4;
465 diag204_info_type = INFO_SIMPLE;
474 diag204_free_buffer();
479 static int diag204_do_store(void *buf, int pages)
483 rc = diag204((unsigned long) diag204_store_sc |
484 (unsigned long) diag204_info_type, pages, buf);
485 return rc < 0 ? -ENOSYS : 0;
488 static void *diag204_store(void)
493 buf = diag204_get_buffer(diag204_info_type, &pages);
496 rc = diag204_do_store(buf, pages);
503 /* Diagnose 224 functions */
505 static int diag224(void *ptr)
507 int rc = -EOPNOTSUPP;
510 " diag %1,%2,0x224\n"
514 : "+d" (rc) :"d" (0), "d" (ptr) : "memory");
518 static int diag224_get_name_table(void)
520 /* memory must be below 2GB */
521 diag224_cpu_names = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
522 if (!diag224_cpu_names)
524 if (diag224(diag224_cpu_names)) {
525 kfree(diag224_cpu_names);
528 EBCASC(diag224_cpu_names + 16, (*diag224_cpu_names + 1) * 16);
532 static void diag224_delete_name_table(void)
534 kfree(diag224_cpu_names);
537 static int diag224_idx2name(int index, char *name)
539 memcpy(name, diag224_cpu_names + ((index + 1) * CPU_NAME_LEN),
541 name[CPU_NAME_LEN] = 0;
546 struct dbfs_d204_hdr {
547 u64 len; /* Length of d204 buffer without header */
548 u16 version; /* Version of header */
549 u8 sc; /* Used subcode */
551 } __attribute__ ((packed));
554 struct dbfs_d204_hdr hdr; /* 64 byte header */
555 char buf[]; /* d204 buffer */
556 } __attribute__ ((packed));
558 static int dbfs_d204_create(void **data, void **data_free_ptr, size_t *size)
560 struct dbfs_d204 *d204;
564 buf_size = PAGE_SIZE * (diag204_buf_pages + 1) + sizeof(d204->hdr);
565 base = vmalloc(buf_size);
568 memset(base, 0, buf_size);
569 d204 = page_align_ptr(base + sizeof(d204->hdr)) - sizeof(d204->hdr);
570 rc = diag204_do_store(d204->buf, diag204_buf_pages);
575 d204->hdr.version = DBFS_D204_HDR_VERSION;
576 d204->hdr.len = PAGE_SIZE * diag204_buf_pages;
577 d204->hdr.sc = diag204_store_sc;
579 *data_free_ptr = base;
580 *size = d204->hdr.len + sizeof(struct dbfs_d204_hdr);
584 static struct hypfs_dbfs_file dbfs_file_d204 = {
586 .data_create = dbfs_d204_create,
590 __init int hypfs_diag_init(void)
594 if (diag204_probe()) {
595 pr_err("The hardware system does not support hypfs\n");
598 if (diag204_info_type == INFO_EXT) {
599 rc = hypfs_dbfs_create_file(&dbfs_file_d204);
603 if (MACHINE_IS_LPAR) {
604 rc = diag224_get_name_table();
606 pr_err("The hardware system does not provide all "
607 "functions required by hypfs\n");
608 debugfs_remove(dbfs_d204_file);
615 void hypfs_diag_exit(void)
617 debugfs_remove(dbfs_d204_file);
618 diag224_delete_name_table();
619 diag204_free_buffer();
620 hypfs_dbfs_remove_file(&dbfs_file_d204);
624 * Functions to create the directory structure
625 * *******************************************
628 static int hypfs_create_cpu_files(struct super_block *sb,
629 struct dentry *cpus_dir, void *cpu_info)
631 struct dentry *cpu_dir;
632 char buffer[TMP_SIZE];
635 snprintf(buffer, TMP_SIZE, "%d", cpu_info__cpu_addr(diag204_info_type,
637 cpu_dir = hypfs_mkdir(sb, cpus_dir, buffer);
638 rc = hypfs_create_u64(sb, cpu_dir, "mgmtime",
639 cpu_info__acc_time(diag204_info_type, cpu_info) -
640 cpu_info__lp_time(diag204_info_type, cpu_info));
643 rc = hypfs_create_u64(sb, cpu_dir, "cputime",
644 cpu_info__lp_time(diag204_info_type, cpu_info));
647 if (diag204_info_type == INFO_EXT) {
648 rc = hypfs_create_u64(sb, cpu_dir, "onlinetime",
649 cpu_info__online_time(diag204_info_type,
654 diag224_idx2name(cpu_info__ctidx(diag204_info_type, cpu_info), buffer);
655 rc = hypfs_create_str(sb, cpu_dir, "type", buffer);
661 static void *hypfs_create_lpar_files(struct super_block *sb,
662 struct dentry *systems_dir, void *part_hdr)
664 struct dentry *cpus_dir;
665 struct dentry *lpar_dir;
666 char lpar_name[LPAR_NAME_LEN + 1];
670 part_hdr__part_name(diag204_info_type, part_hdr, lpar_name);
671 lpar_name[LPAR_NAME_LEN] = 0;
672 lpar_dir = hypfs_mkdir(sb, systems_dir, lpar_name);
673 if (IS_ERR(lpar_dir))
675 cpus_dir = hypfs_mkdir(sb, lpar_dir, "cpus");
676 if (IS_ERR(cpus_dir))
678 cpu_info = part_hdr + part_hdr__size(diag204_info_type);
679 for (i = 0; i < part_hdr__rcpus(diag204_info_type, part_hdr); i++) {
681 rc = hypfs_create_cpu_files(sb, cpus_dir, cpu_info);
684 cpu_info += cpu_info__size(diag204_info_type);
689 static int hypfs_create_phys_cpu_files(struct super_block *sb,
690 struct dentry *cpus_dir, void *cpu_info)
692 struct dentry *cpu_dir;
693 char buffer[TMP_SIZE];
696 snprintf(buffer, TMP_SIZE, "%i", phys_cpu__cpu_addr(diag204_info_type,
698 cpu_dir = hypfs_mkdir(sb, cpus_dir, buffer);
700 return PTR_ERR(cpu_dir);
701 rc = hypfs_create_u64(sb, cpu_dir, "mgmtime",
702 phys_cpu__mgm_time(diag204_info_type, cpu_info));
705 diag224_idx2name(phys_cpu__ctidx(diag204_info_type, cpu_info), buffer);
706 rc = hypfs_create_str(sb, cpu_dir, "type", buffer);
712 static void *hypfs_create_phys_files(struct super_block *sb,
713 struct dentry *parent_dir, void *phys_hdr)
717 struct dentry *cpus_dir;
719 cpus_dir = hypfs_mkdir(sb, parent_dir, "cpus");
720 if (IS_ERR(cpus_dir))
722 cpu_info = phys_hdr + phys_hdr__size(diag204_info_type);
723 for (i = 0; i < phys_hdr__cpus(diag204_info_type, phys_hdr); i++) {
725 rc = hypfs_create_phys_cpu_files(sb, cpus_dir, cpu_info);
728 cpu_info += phys_cpu__size(diag204_info_type);
733 int hypfs_diag_create_files(struct super_block *sb, struct dentry *root)
735 struct dentry *systems_dir, *hyp_dir;
736 void *time_hdr, *part_hdr;
740 buffer = diag204_store();
742 return PTR_ERR(buffer);
744 systems_dir = hypfs_mkdir(sb, root, "systems");
745 if (IS_ERR(systems_dir)) {
746 rc = PTR_ERR(systems_dir);
749 time_hdr = (struct x_info_blk_hdr *)buffer;
750 part_hdr = time_hdr + info_blk_hdr__size(diag204_info_type);
751 for (i = 0; i < info_blk_hdr__npar(diag204_info_type, time_hdr); i++) {
752 part_hdr = hypfs_create_lpar_files(sb, systems_dir, part_hdr);
753 if (IS_ERR(part_hdr)) {
754 rc = PTR_ERR(part_hdr);
758 if (info_blk_hdr__flags(diag204_info_type, time_hdr) & LPAR_PHYS_FLG) {
759 ptr = hypfs_create_phys_files(sb, root, part_hdr);
765 hyp_dir = hypfs_mkdir(sb, root, "hyp");
766 if (IS_ERR(hyp_dir)) {
767 rc = PTR_ERR(hyp_dir);
770 ptr = hypfs_create_str(sb, hyp_dir, "type", "LPAR Hypervisor");