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/string.h>
16 #include <linux/vmalloc.h>
17 #include <asm/ebcdic.h>
20 #define LPAR_NAME_LEN 8 /* lpar name len in diag 204 data */
21 #define CPU_NAME_LEN 16 /* type name len of cpus in diag224 name table */
22 #define TMP_SIZE 64 /* size of temporary buffers */
24 /* diag 204 subcodes */
32 /* The two available diag 204 data formats */
38 /* bit is set in flags, when physical cpu info is included in diag 204 data */
39 #define LPAR_PHYS_FLG 0x80
41 static char *diag224_cpu_names; /* diag 224 name table */
42 static enum diag204_sc diag204_store_sc; /* used subcode for store */
43 static enum diag204_format diag204_info_type; /* used diag 204 data format */
45 static void *diag204_buf; /* 4K aligned buffer for diag204 data */
46 static void *diag204_buf_vmalloc; /* vmalloc pointer for diag204 data */
47 static int diag204_buf_pages; /* number of pages for diag204 data */
50 * DIAG 204 data structures and member access functions.
52 * Since we have two different diag 204 data formats for old and new s390
53 * machines, we do not access the structs directly, but use getter functions for
54 * each struct member instead. This should make the code more readable.
57 /* Time information block */
66 } __attribute__ ((packed));
68 struct x_info_blk_hdr {
77 } __attribute__ ((packed));
79 static inline int info_blk_hdr__size(enum diag204_format type)
81 if (type == INFO_SIMPLE)
82 return sizeof(struct info_blk_hdr);
84 return sizeof(struct x_info_blk_hdr);
87 static inline __u8 info_blk_hdr__npar(enum diag204_format type, void *hdr)
89 if (type == INFO_SIMPLE)
90 return ((struct info_blk_hdr *)hdr)->npar;
92 return ((struct x_info_blk_hdr *)hdr)->npar;
95 static inline __u8 info_blk_hdr__flags(enum diag204_format type, void *hdr)
97 if (type == INFO_SIMPLE)
98 return ((struct info_blk_hdr *)hdr)->flags;
100 return ((struct x_info_blk_hdr *)hdr)->flags;
103 static inline __u16 info_blk_hdr__pcpus(enum diag204_format type, void *hdr)
105 if (type == INFO_SIMPLE)
106 return ((struct info_blk_hdr *)hdr)->phys_cpus;
108 return ((struct x_info_blk_hdr *)hdr)->phys_cpus;
111 /* Partition header */
117 char part_name[LPAR_NAME_LEN];
118 } __attribute__ ((packed));
126 char part_name[LPAR_NAME_LEN];
136 } __attribute__ ((packed));
138 static inline int part_hdr__size(enum diag204_format type)
140 if (type == INFO_SIMPLE)
141 return sizeof(struct part_hdr);
143 return sizeof(struct x_part_hdr);
146 static inline __u8 part_hdr__rcpus(enum diag204_format type, void *hdr)
148 if (type == INFO_SIMPLE)
149 return ((struct part_hdr *)hdr)->cpus;
151 return ((struct x_part_hdr *)hdr)->rcpus;
154 static inline void part_hdr__part_name(enum diag204_format type, void *hdr,
157 if (type == INFO_SIMPLE)
158 memcpy(name, ((struct part_hdr *)hdr)->part_name,
161 memcpy(name, ((struct x_part_hdr *)hdr)->part_name,
163 EBCASC(name, LPAR_NAME_LEN);
164 name[LPAR_NAME_LEN] = 0;
176 } __attribute__ ((packed));
195 } __attribute__ ((packed));
199 static inline int cpu_info__size(enum diag204_format type)
201 if (type == INFO_SIMPLE)
202 return sizeof(struct cpu_info);
204 return sizeof(struct x_cpu_info);
207 static inline __u8 cpu_info__ctidx(enum diag204_format type, void *hdr)
209 if (type == INFO_SIMPLE)
210 return ((struct cpu_info *)hdr)->ctidx;
212 return ((struct x_cpu_info *)hdr)->ctidx;
215 static inline __u16 cpu_info__cpu_addr(enum diag204_format type, void *hdr)
217 if (type == INFO_SIMPLE)
218 return ((struct cpu_info *)hdr)->cpu_addr;
220 return ((struct x_cpu_info *)hdr)->cpu_addr;
223 static inline __u64 cpu_info__acc_time(enum diag204_format type, void *hdr)
225 if (type == INFO_SIMPLE)
226 return ((struct cpu_info *)hdr)->acc_time;
228 return ((struct x_cpu_info *)hdr)->acc_time;
231 static inline __u64 cpu_info__lp_time(enum diag204_format type, void *hdr)
233 if (type == INFO_SIMPLE)
234 return ((struct cpu_info *)hdr)->lp_time;
236 return ((struct x_cpu_info *)hdr)->lp_time;
239 static inline __u64 cpu_info__online_time(enum diag204_format type, void *hdr)
241 if (type == INFO_SIMPLE)
242 return 0; /* online_time not available in simple info */
244 return ((struct x_cpu_info *)hdr)->online_time;
247 /* Physical header */
254 } __attribute__ ((packed));
262 } __attribute__ ((packed));
264 static inline int phys_hdr__size(enum diag204_format type)
266 if (type == INFO_SIMPLE)
267 return sizeof(struct phys_hdr);
269 return sizeof(struct x_phys_hdr);
272 static inline __u8 phys_hdr__cpus(enum diag204_format type, void *hdr)
274 if (type == INFO_SIMPLE)
275 return ((struct phys_hdr *)hdr)->cpus;
277 return ((struct x_phys_hdr *)hdr)->cpus;
280 /* Physical CPU info block */
289 } __attribute__ ((packed));
298 } __attribute__ ((packed));
300 static inline int phys_cpu__size(enum diag204_format type)
302 if (type == INFO_SIMPLE)
303 return sizeof(struct phys_cpu);
305 return sizeof(struct x_phys_cpu);
308 static inline __u16 phys_cpu__cpu_addr(enum diag204_format type, void *hdr)
310 if (type == INFO_SIMPLE)
311 return ((struct phys_cpu *)hdr)->cpu_addr;
313 return ((struct x_phys_cpu *)hdr)->cpu_addr;
316 static inline __u64 phys_cpu__mgm_time(enum diag204_format type, void *hdr)
318 if (type == INFO_SIMPLE)
319 return ((struct phys_cpu *)hdr)->mgm_time;
321 return ((struct x_phys_cpu *)hdr)->mgm_time;
324 static inline __u64 phys_cpu__ctidx(enum diag204_format type, void *hdr)
326 if (type == INFO_SIMPLE)
327 return ((struct phys_cpu *)hdr)->ctidx;
329 return ((struct x_phys_cpu *)hdr)->ctidx;
332 /* Diagnose 204 functions */
334 static int diag204(unsigned long subcode, unsigned long size, void *addr)
336 register unsigned long _subcode asm("0") = subcode;
337 register unsigned long _size asm("1") = size;
340 " diag %2,%0,0x204\n"
343 : "+d" (_subcode), "+d" (_size) : "d" (addr) : "memory");
350 * For the old diag subcode 4 with simple data format we have to use real
351 * memory. If we use subcode 6 or 7 with extended data format, we can (and
352 * should) use vmalloc, since we need a lot of memory in that case. Currently
356 static void diag204_free_buffer(void)
360 if (diag204_buf_vmalloc) {
361 vfree(diag204_buf_vmalloc);
362 diag204_buf_vmalloc = NULL;
364 free_pages((unsigned long) diag204_buf, 0);
366 diag204_buf_pages = 0;
370 static void *diag204_alloc_vbuf(int pages)
372 /* The buffer has to be page aligned! */
373 diag204_buf_vmalloc = vmalloc(PAGE_SIZE * (pages + 1));
374 if (!diag204_buf_vmalloc)
375 return ERR_PTR(-ENOMEM);
376 diag204_buf = (void*)((unsigned long)diag204_buf_vmalloc
377 & ~0xfffUL) + 0x1000;
378 diag204_buf_pages = pages;
382 static void *diag204_alloc_rbuf(void)
384 diag204_buf = (void*)__get_free_pages(GFP_KERNEL,0);
386 return ERR_PTR(-ENOMEM);
387 diag204_buf_pages = 1;
391 static void *diag204_get_buffer(enum diag204_format fmt, int *pages)
394 *pages = diag204_buf_pages;
397 if (fmt == INFO_SIMPLE) {
399 return diag204_alloc_rbuf();
400 } else {/* INFO_EXT */
401 *pages = diag204((unsigned long)SUBC_RSI |
402 (unsigned long)INFO_EXT, 0, NULL);
404 return ERR_PTR(-ENOSYS);
406 return diag204_alloc_vbuf(*pages);
411 * diag204_probe() has to find out, which type of diagnose 204 implementation
412 * we have on our machine. Currently there are three possible scanarios:
413 * - subcode 4 + simple data format (only one page)
414 * - subcode 4-6 + extended data format
415 * - subcode 4-7 + extended data format
417 * Subcode 5 is used to retrieve the size of the data, provided by subcodes
418 * 6 and 7. Subcode 7 basically has the same function as subcode 6. In addition
419 * to subcode 6 it provides also information about secondary cpus.
420 * In order to get as much information as possible, we first try
421 * subcode 7, then 6 and if both fail, we use subcode 4.
424 static int diag204_probe(void)
429 buf = diag204_get_buffer(INFO_EXT, &pages);
431 if (diag204((unsigned long)SUBC_STIB7 |
432 (unsigned long)INFO_EXT, pages, buf) >= 0) {
433 diag204_store_sc = SUBC_STIB7;
434 diag204_info_type = INFO_EXT;
437 if (diag204((unsigned long)SUBC_STIB6 |
438 (unsigned long)INFO_EXT, pages, buf) >= 0) {
439 diag204_store_sc = SUBC_STIB7;
440 diag204_info_type = INFO_EXT;
443 diag204_free_buffer();
446 /* subcodes 6 and 7 failed, now try subcode 4 */
448 buf = diag204_get_buffer(INFO_SIMPLE, &pages);
453 if (diag204((unsigned long)SUBC_STIB4 |
454 (unsigned long)INFO_SIMPLE, pages, buf) >= 0) {
455 diag204_store_sc = SUBC_STIB4;
456 diag204_info_type = INFO_SIMPLE;
465 diag204_free_buffer();
470 static void *diag204_store(void)
475 buf = diag204_get_buffer(diag204_info_type, &pages);
478 if (diag204((unsigned long)diag204_store_sc |
479 (unsigned long)diag204_info_type, pages, buf) < 0)
480 return ERR_PTR(-ENOSYS);
485 /* Diagnose 224 functions */
487 static int diag224(void *ptr)
492 " diag %1,%2,0x224\n"
496 : "+d" (rc) :"d" (0), "d" (ptr) : "memory");
500 static int diag224_get_name_table(void)
502 /* memory must be below 2GB */
503 diag224_cpu_names = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
504 if (!diag224_cpu_names)
506 if (diag224(diag224_cpu_names)) {
507 kfree(diag224_cpu_names);
510 EBCASC(diag224_cpu_names + 16, (*diag224_cpu_names + 1) * 16);
514 static void diag224_delete_name_table(void)
516 kfree(diag224_cpu_names);
519 static int diag224_idx2name(int index, char *name)
521 memcpy(name, diag224_cpu_names + ((index + 1) * CPU_NAME_LEN),
523 name[CPU_NAME_LEN] = 0;
528 __init int hypfs_diag_init(void)
532 if (diag204_probe()) {
533 pr_err("The hardware system does not support hypfs\n");
536 rc = diag224_get_name_table();
538 diag204_free_buffer();
539 pr_err("The hardware system does not provide all "
540 "functions required by hypfs\n");
545 void hypfs_diag_exit(void)
547 diag224_delete_name_table();
548 diag204_free_buffer();
552 * Functions to create the directory structure
553 * *******************************************
556 static int hypfs_create_cpu_files(struct super_block *sb,
557 struct dentry *cpus_dir, void *cpu_info)
559 struct dentry *cpu_dir;
560 char buffer[TMP_SIZE];
563 snprintf(buffer, TMP_SIZE, "%d", cpu_info__cpu_addr(diag204_info_type,
565 cpu_dir = hypfs_mkdir(sb, cpus_dir, buffer);
566 rc = hypfs_create_u64(sb, cpu_dir, "mgmtime",
567 cpu_info__acc_time(diag204_info_type, cpu_info) -
568 cpu_info__lp_time(diag204_info_type, cpu_info));
571 rc = hypfs_create_u64(sb, cpu_dir, "cputime",
572 cpu_info__lp_time(diag204_info_type, cpu_info));
575 if (diag204_info_type == INFO_EXT) {
576 rc = hypfs_create_u64(sb, cpu_dir, "onlinetime",
577 cpu_info__online_time(diag204_info_type,
582 diag224_idx2name(cpu_info__ctidx(diag204_info_type, cpu_info), buffer);
583 rc = hypfs_create_str(sb, cpu_dir, "type", buffer);
589 static void *hypfs_create_lpar_files(struct super_block *sb,
590 struct dentry *systems_dir, void *part_hdr)
592 struct dentry *cpus_dir;
593 struct dentry *lpar_dir;
594 char lpar_name[LPAR_NAME_LEN + 1];
598 part_hdr__part_name(diag204_info_type, part_hdr, lpar_name);
599 lpar_name[LPAR_NAME_LEN] = 0;
600 lpar_dir = hypfs_mkdir(sb, systems_dir, lpar_name);
601 if (IS_ERR(lpar_dir))
603 cpus_dir = hypfs_mkdir(sb, lpar_dir, "cpus");
604 if (IS_ERR(cpus_dir))
606 cpu_info = part_hdr + part_hdr__size(diag204_info_type);
607 for (i = 0; i < part_hdr__rcpus(diag204_info_type, part_hdr); i++) {
609 rc = hypfs_create_cpu_files(sb, cpus_dir, cpu_info);
612 cpu_info += cpu_info__size(diag204_info_type);
617 static int hypfs_create_phys_cpu_files(struct super_block *sb,
618 struct dentry *cpus_dir, void *cpu_info)
620 struct dentry *cpu_dir;
621 char buffer[TMP_SIZE];
624 snprintf(buffer, TMP_SIZE, "%i", phys_cpu__cpu_addr(diag204_info_type,
626 cpu_dir = hypfs_mkdir(sb, cpus_dir, buffer);
628 return PTR_ERR(cpu_dir);
629 rc = hypfs_create_u64(sb, cpu_dir, "mgmtime",
630 phys_cpu__mgm_time(diag204_info_type, cpu_info));
633 diag224_idx2name(phys_cpu__ctidx(diag204_info_type, cpu_info), buffer);
634 rc = hypfs_create_str(sb, cpu_dir, "type", buffer);
640 static void *hypfs_create_phys_files(struct super_block *sb,
641 struct dentry *parent_dir, void *phys_hdr)
645 struct dentry *cpus_dir;
647 cpus_dir = hypfs_mkdir(sb, parent_dir, "cpus");
648 if (IS_ERR(cpus_dir))
650 cpu_info = phys_hdr + phys_hdr__size(diag204_info_type);
651 for (i = 0; i < phys_hdr__cpus(diag204_info_type, phys_hdr); i++) {
653 rc = hypfs_create_phys_cpu_files(sb, cpus_dir, cpu_info);
656 cpu_info += phys_cpu__size(diag204_info_type);
661 int hypfs_diag_create_files(struct super_block *sb, struct dentry *root)
663 struct dentry *systems_dir, *hyp_dir;
664 void *time_hdr, *part_hdr;
668 buffer = diag204_store();
670 return PTR_ERR(buffer);
672 systems_dir = hypfs_mkdir(sb, root, "systems");
673 if (IS_ERR(systems_dir)) {
674 rc = PTR_ERR(systems_dir);
677 time_hdr = (struct x_info_blk_hdr *)buffer;
678 part_hdr = time_hdr + info_blk_hdr__size(diag204_info_type);
679 for (i = 0; i < info_blk_hdr__npar(diag204_info_type, time_hdr); i++) {
680 part_hdr = hypfs_create_lpar_files(sb, systems_dir, part_hdr);
681 if (IS_ERR(part_hdr)) {
682 rc = PTR_ERR(part_hdr);
686 if (info_blk_hdr__flags(diag204_info_type, time_hdr) & LPAR_PHYS_FLG) {
687 ptr = hypfs_create_phys_files(sb, root, part_hdr);
693 hyp_dir = hypfs_mkdir(sb, root, "hyp");
694 if (IS_ERR(hyp_dir)) {
695 rc = PTR_ERR(hyp_dir);
698 ptr = hypfs_create_str(sb, hyp_dir, "type", "LPAR Hypervisor");