xref: /openbmc/linux/arch/s390/hypfs/hypfs_diag.c (revision 67694c07)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Hypervisor filesystem for Linux on s390. Diag 204 and 224
4  *    implementation.
5  *
6  *    Copyright IBM Corp. 2006, 2008
7  *    Author(s): Michael Holzheu <holzheu@de.ibm.com>
8  */
9 
10 #define KMSG_COMPONENT "hypfs"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
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 <linux/mm.h>
19 #include <asm/diag.h>
20 #include <asm/ebcdic.h>
21 #include "hypfs.h"
22 
23 #define TMP_SIZE 64		/* size of temporary buffers */
24 
25 #define DBFS_D204_HDR_VERSION	0
26 
27 static char *diag224_cpu_names;			/* diag 224 name table */
28 static enum diag204_sc diag204_store_sc;	/* used subcode for store */
29 static enum diag204_format diag204_info_type;	/* used diag 204 data format */
30 
31 static void *diag204_buf;		/* 4K aligned buffer for diag204 data */
32 static void *diag204_buf_vmalloc;	/* vmalloc pointer for diag204 data */
33 static int diag204_buf_pages;		/* number of pages for diag204 data */
34 
35 static struct dentry *dbfs_d204_file;
36 
37 /*
38  * DIAG 204 member access functions.
39  *
40  * Since we have two different diag 204 data formats for old and new s390
41  * machines, we do not access the structs directly, but use getter functions for
42  * each struct member instead. This should make the code more readable.
43  */
44 
45 /* Time information block */
46 
47 static inline int info_blk_hdr__size(enum diag204_format type)
48 {
49 	if (type == DIAG204_INFO_SIMPLE)
50 		return sizeof(struct diag204_info_blk_hdr);
51 	else /* DIAG204_INFO_EXT */
52 		return sizeof(struct diag204_x_info_blk_hdr);
53 }
54 
55 static inline __u8 info_blk_hdr__npar(enum diag204_format type, void *hdr)
56 {
57 	if (type == DIAG204_INFO_SIMPLE)
58 		return ((struct diag204_info_blk_hdr *)hdr)->npar;
59 	else /* DIAG204_INFO_EXT */
60 		return ((struct diag204_x_info_blk_hdr *)hdr)->npar;
61 }
62 
63 static inline __u8 info_blk_hdr__flags(enum diag204_format type, void *hdr)
64 {
65 	if (type == DIAG204_INFO_SIMPLE)
66 		return ((struct diag204_info_blk_hdr *)hdr)->flags;
67 	else /* DIAG204_INFO_EXT */
68 		return ((struct diag204_x_info_blk_hdr *)hdr)->flags;
69 }
70 
71 /* Partition header */
72 
73 static inline int part_hdr__size(enum diag204_format type)
74 {
75 	if (type == DIAG204_INFO_SIMPLE)
76 		return sizeof(struct diag204_part_hdr);
77 	else /* DIAG204_INFO_EXT */
78 		return sizeof(struct diag204_x_part_hdr);
79 }
80 
81 static inline __u8 part_hdr__rcpus(enum diag204_format type, void *hdr)
82 {
83 	if (type == DIAG204_INFO_SIMPLE)
84 		return ((struct diag204_part_hdr *)hdr)->cpus;
85 	else /* DIAG204_INFO_EXT */
86 		return ((struct diag204_x_part_hdr *)hdr)->rcpus;
87 }
88 
89 static inline void part_hdr__part_name(enum diag204_format type, void *hdr,
90 				       char *name)
91 {
92 	if (type == DIAG204_INFO_SIMPLE)
93 		memcpy(name, ((struct diag204_part_hdr *)hdr)->part_name,
94 		       DIAG204_LPAR_NAME_LEN);
95 	else /* DIAG204_INFO_EXT */
96 		memcpy(name, ((struct diag204_x_part_hdr *)hdr)->part_name,
97 		       DIAG204_LPAR_NAME_LEN);
98 	EBCASC(name, DIAG204_LPAR_NAME_LEN);
99 	name[DIAG204_LPAR_NAME_LEN] = 0;
100 	strim(name);
101 }
102 
103 /* CPU info block */
104 
105 static inline int cpu_info__size(enum diag204_format type)
106 {
107 	if (type == DIAG204_INFO_SIMPLE)
108 		return sizeof(struct diag204_cpu_info);
109 	else /* DIAG204_INFO_EXT */
110 		return sizeof(struct diag204_x_cpu_info);
111 }
112 
113 static inline __u8 cpu_info__ctidx(enum diag204_format type, void *hdr)
114 {
115 	if (type == DIAG204_INFO_SIMPLE)
116 		return ((struct diag204_cpu_info *)hdr)->ctidx;
117 	else /* DIAG204_INFO_EXT */
118 		return ((struct diag204_x_cpu_info *)hdr)->ctidx;
119 }
120 
121 static inline __u16 cpu_info__cpu_addr(enum diag204_format type, void *hdr)
122 {
123 	if (type == DIAG204_INFO_SIMPLE)
124 		return ((struct diag204_cpu_info *)hdr)->cpu_addr;
125 	else /* DIAG204_INFO_EXT */
126 		return ((struct diag204_x_cpu_info *)hdr)->cpu_addr;
127 }
128 
129 static inline __u64 cpu_info__acc_time(enum diag204_format type, void *hdr)
130 {
131 	if (type == DIAG204_INFO_SIMPLE)
132 		return ((struct diag204_cpu_info *)hdr)->acc_time;
133 	else /* DIAG204_INFO_EXT */
134 		return ((struct diag204_x_cpu_info *)hdr)->acc_time;
135 }
136 
137 static inline __u64 cpu_info__lp_time(enum diag204_format type, void *hdr)
138 {
139 	if (type == DIAG204_INFO_SIMPLE)
140 		return ((struct diag204_cpu_info *)hdr)->lp_time;
141 	else /* DIAG204_INFO_EXT */
142 		return ((struct diag204_x_cpu_info *)hdr)->lp_time;
143 }
144 
145 static inline __u64 cpu_info__online_time(enum diag204_format type, void *hdr)
146 {
147 	if (type == DIAG204_INFO_SIMPLE)
148 		return 0;	/* online_time not available in simple info */
149 	else /* DIAG204_INFO_EXT */
150 		return ((struct diag204_x_cpu_info *)hdr)->online_time;
151 }
152 
153 /* Physical header */
154 
155 static inline int phys_hdr__size(enum diag204_format type)
156 {
157 	if (type == DIAG204_INFO_SIMPLE)
158 		return sizeof(struct diag204_phys_hdr);
159 	else /* DIAG204_INFO_EXT */
160 		return sizeof(struct diag204_x_phys_hdr);
161 }
162 
163 static inline __u8 phys_hdr__cpus(enum diag204_format type, void *hdr)
164 {
165 	if (type == DIAG204_INFO_SIMPLE)
166 		return ((struct diag204_phys_hdr *)hdr)->cpus;
167 	else /* DIAG204_INFO_EXT */
168 		return ((struct diag204_x_phys_hdr *)hdr)->cpus;
169 }
170 
171 /* Physical CPU info block */
172 
173 static inline int phys_cpu__size(enum diag204_format type)
174 {
175 	if (type == DIAG204_INFO_SIMPLE)
176 		return sizeof(struct diag204_phys_cpu);
177 	else /* DIAG204_INFO_EXT */
178 		return sizeof(struct diag204_x_phys_cpu);
179 }
180 
181 static inline __u16 phys_cpu__cpu_addr(enum diag204_format type, void *hdr)
182 {
183 	if (type == DIAG204_INFO_SIMPLE)
184 		return ((struct diag204_phys_cpu *)hdr)->cpu_addr;
185 	else /* DIAG204_INFO_EXT */
186 		return ((struct diag204_x_phys_cpu *)hdr)->cpu_addr;
187 }
188 
189 static inline __u64 phys_cpu__mgm_time(enum diag204_format type, void *hdr)
190 {
191 	if (type == DIAG204_INFO_SIMPLE)
192 		return ((struct diag204_phys_cpu *)hdr)->mgm_time;
193 	else /* DIAG204_INFO_EXT */
194 		return ((struct diag204_x_phys_cpu *)hdr)->mgm_time;
195 }
196 
197 static inline __u64 phys_cpu__ctidx(enum diag204_format type, void *hdr)
198 {
199 	if (type == DIAG204_INFO_SIMPLE)
200 		return ((struct diag204_phys_cpu *)hdr)->ctidx;
201 	else /* DIAG204_INFO_EXT */
202 		return ((struct diag204_x_phys_cpu *)hdr)->ctidx;
203 }
204 
205 /* Diagnose 204 functions */
206 /*
207  * For the old diag subcode 4 with simple data format we have to use real
208  * memory. If we use subcode 6 or 7 with extended data format, we can (and
209  * should) use vmalloc, since we need a lot of memory in that case. Currently
210  * up to 93 pages!
211  */
212 
213 static void diag204_free_buffer(void)
214 {
215 	if (!diag204_buf)
216 		return;
217 	if (diag204_buf_vmalloc) {
218 		vfree(diag204_buf_vmalloc);
219 		diag204_buf_vmalloc = NULL;
220 	} else {
221 		free_pages((unsigned long) diag204_buf, 0);
222 	}
223 	diag204_buf = NULL;
224 }
225 
226 static void *page_align_ptr(void *ptr)
227 {
228 	return (void *) PAGE_ALIGN((unsigned long) ptr);
229 }
230 
231 static void *diag204_alloc_vbuf(int pages)
232 {
233 	/* The buffer has to be page aligned! */
234 	diag204_buf_vmalloc = vmalloc(array_size(PAGE_SIZE, (pages + 1)));
235 	if (!diag204_buf_vmalloc)
236 		return ERR_PTR(-ENOMEM);
237 	diag204_buf = page_align_ptr(diag204_buf_vmalloc);
238 	diag204_buf_pages = pages;
239 	return diag204_buf;
240 }
241 
242 static void *diag204_alloc_rbuf(void)
243 {
244 	diag204_buf = (void*)__get_free_pages(GFP_KERNEL,0);
245 	if (!diag204_buf)
246 		return ERR_PTR(-ENOMEM);
247 	diag204_buf_pages = 1;
248 	return diag204_buf;
249 }
250 
251 static void *diag204_get_buffer(enum diag204_format fmt, int *pages)
252 {
253 	if (diag204_buf) {
254 		*pages = diag204_buf_pages;
255 		return diag204_buf;
256 	}
257 	if (fmt == DIAG204_INFO_SIMPLE) {
258 		*pages = 1;
259 		return diag204_alloc_rbuf();
260 	} else {/* DIAG204_INFO_EXT */
261 		*pages = diag204((unsigned long)DIAG204_SUBC_RSI |
262 				 (unsigned long)DIAG204_INFO_EXT, 0, NULL);
263 		if (*pages <= 0)
264 			return ERR_PTR(-ENOSYS);
265 		else
266 			return diag204_alloc_vbuf(*pages);
267 	}
268 }
269 
270 /*
271  * diag204_probe() has to find out, which type of diagnose 204 implementation
272  * we have on our machine. Currently there are three possible scanarios:
273  *   - subcode 4   + simple data format (only one page)
274  *   - subcode 4-6 + extended data format
275  *   - subcode 4-7 + extended data format
276  *
277  * Subcode 5 is used to retrieve the size of the data, provided by subcodes
278  * 6 and 7. Subcode 7 basically has the same function as subcode 6. In addition
279  * to subcode 6 it provides also information about secondary cpus.
280  * In order to get as much information as possible, we first try
281  * subcode 7, then 6 and if both fail, we use subcode 4.
282  */
283 
284 static int diag204_probe(void)
285 {
286 	void *buf;
287 	int pages, rc;
288 
289 	buf = diag204_get_buffer(DIAG204_INFO_EXT, &pages);
290 	if (!IS_ERR(buf)) {
291 		if (diag204((unsigned long)DIAG204_SUBC_STIB7 |
292 			    (unsigned long)DIAG204_INFO_EXT, pages, buf) >= 0) {
293 			diag204_store_sc = DIAG204_SUBC_STIB7;
294 			diag204_info_type = DIAG204_INFO_EXT;
295 			goto out;
296 		}
297 		if (diag204((unsigned long)DIAG204_SUBC_STIB6 |
298 			    (unsigned long)DIAG204_INFO_EXT, pages, buf) >= 0) {
299 			diag204_store_sc = DIAG204_SUBC_STIB6;
300 			diag204_info_type = DIAG204_INFO_EXT;
301 			goto out;
302 		}
303 		diag204_free_buffer();
304 	}
305 
306 	/* subcodes 6 and 7 failed, now try subcode 4 */
307 
308 	buf = diag204_get_buffer(DIAG204_INFO_SIMPLE, &pages);
309 	if (IS_ERR(buf)) {
310 		rc = PTR_ERR(buf);
311 		goto fail_alloc;
312 	}
313 	if (diag204((unsigned long)DIAG204_SUBC_STIB4 |
314 		    (unsigned long)DIAG204_INFO_SIMPLE, pages, buf) >= 0) {
315 		diag204_store_sc = DIAG204_SUBC_STIB4;
316 		diag204_info_type = DIAG204_INFO_SIMPLE;
317 		goto out;
318 	} else {
319 		rc = -ENOSYS;
320 		goto fail_store;
321 	}
322 out:
323 	rc = 0;
324 fail_store:
325 	diag204_free_buffer();
326 fail_alloc:
327 	return rc;
328 }
329 
330 static int diag204_do_store(void *buf, int pages)
331 {
332 	int rc;
333 
334 	rc = diag204((unsigned long) diag204_store_sc |
335 		     (unsigned long) diag204_info_type, pages, buf);
336 	return rc < 0 ? -ENOSYS : 0;
337 }
338 
339 static void *diag204_store(void)
340 {
341 	void *buf;
342 	int pages, rc;
343 
344 	buf = diag204_get_buffer(diag204_info_type, &pages);
345 	if (IS_ERR(buf))
346 		goto out;
347 	rc = diag204_do_store(buf, pages);
348 	if (rc)
349 		return ERR_PTR(rc);
350 out:
351 	return buf;
352 }
353 
354 /* Diagnose 224 functions */
355 
356 static int diag224_get_name_table(void)
357 {
358 	/* memory must be below 2GB */
359 	diag224_cpu_names = (char *) __get_free_page(GFP_KERNEL | GFP_DMA);
360 	if (!diag224_cpu_names)
361 		return -ENOMEM;
362 	if (diag224(diag224_cpu_names)) {
363 		free_page((unsigned long) diag224_cpu_names);
364 		return -EOPNOTSUPP;
365 	}
366 	EBCASC(diag224_cpu_names + 16, (*diag224_cpu_names + 1) * 16);
367 	return 0;
368 }
369 
370 static void diag224_delete_name_table(void)
371 {
372 	free_page((unsigned long) diag224_cpu_names);
373 }
374 
375 static int diag224_idx2name(int index, char *name)
376 {
377 	memcpy(name, diag224_cpu_names + ((index + 1) * DIAG204_CPU_NAME_LEN),
378 	       DIAG204_CPU_NAME_LEN);
379 	name[DIAG204_CPU_NAME_LEN] = 0;
380 	strim(name);
381 	return 0;
382 }
383 
384 struct dbfs_d204_hdr {
385 	u64	len;		/* Length of d204 buffer without header */
386 	u16	version;	/* Version of header */
387 	u8	sc;		/* Used subcode */
388 	char	reserved[53];
389 } __attribute__ ((packed));
390 
391 struct dbfs_d204 {
392 	struct dbfs_d204_hdr	hdr;	/* 64 byte header */
393 	char			buf[];	/* d204 buffer */
394 } __attribute__ ((packed));
395 
396 static int dbfs_d204_create(void **data, void **data_free_ptr, size_t *size)
397 {
398 	struct dbfs_d204 *d204;
399 	int rc, buf_size;
400 	void *base;
401 
402 	buf_size = PAGE_SIZE * (diag204_buf_pages + 1) + sizeof(d204->hdr);
403 	base = vzalloc(buf_size);
404 	if (!base)
405 		return -ENOMEM;
406 	d204 = page_align_ptr(base + sizeof(d204->hdr)) - sizeof(d204->hdr);
407 	rc = diag204_do_store(d204->buf, diag204_buf_pages);
408 	if (rc) {
409 		vfree(base);
410 		return rc;
411 	}
412 	d204->hdr.version = DBFS_D204_HDR_VERSION;
413 	d204->hdr.len = PAGE_SIZE * diag204_buf_pages;
414 	d204->hdr.sc = diag204_store_sc;
415 	*data = d204;
416 	*data_free_ptr = base;
417 	*size = d204->hdr.len + sizeof(struct dbfs_d204_hdr);
418 	return 0;
419 }
420 
421 static struct hypfs_dbfs_file dbfs_file_d204 = {
422 	.name		= "diag_204",
423 	.data_create	= dbfs_d204_create,
424 	.data_free	= vfree,
425 };
426 
427 __init int hypfs_diag_init(void)
428 {
429 	int rc;
430 
431 	if (diag204_probe()) {
432 		pr_info("The hardware system does not support hypfs\n");
433 		return -ENODATA;
434 	}
435 
436 	if (diag204_info_type == DIAG204_INFO_EXT)
437 		hypfs_dbfs_create_file(&dbfs_file_d204);
438 
439 	if (MACHINE_IS_LPAR) {
440 		rc = diag224_get_name_table();
441 		if (rc) {
442 			pr_err("The hardware system does not provide all "
443 			       "functions required by hypfs\n");
444 			debugfs_remove(dbfs_d204_file);
445 			return rc;
446 		}
447 	}
448 	return 0;
449 }
450 
451 void hypfs_diag_exit(void)
452 {
453 	debugfs_remove(dbfs_d204_file);
454 	diag224_delete_name_table();
455 	diag204_free_buffer();
456 	hypfs_dbfs_remove_file(&dbfs_file_d204);
457 }
458 
459 /*
460  * Functions to create the directory structure
461  * *******************************************
462  */
463 
464 static int hypfs_create_cpu_files(struct dentry *cpus_dir, void *cpu_info)
465 {
466 	struct dentry *cpu_dir;
467 	char buffer[TMP_SIZE];
468 	void *rc;
469 
470 	snprintf(buffer, TMP_SIZE, "%d", cpu_info__cpu_addr(diag204_info_type,
471 							    cpu_info));
472 	cpu_dir = hypfs_mkdir(cpus_dir, buffer);
473 	rc = hypfs_create_u64(cpu_dir, "mgmtime",
474 			      cpu_info__acc_time(diag204_info_type, cpu_info) -
475 			      cpu_info__lp_time(diag204_info_type, cpu_info));
476 	if (IS_ERR(rc))
477 		return PTR_ERR(rc);
478 	rc = hypfs_create_u64(cpu_dir, "cputime",
479 			      cpu_info__lp_time(diag204_info_type, cpu_info));
480 	if (IS_ERR(rc))
481 		return PTR_ERR(rc);
482 	if (diag204_info_type == DIAG204_INFO_EXT) {
483 		rc = hypfs_create_u64(cpu_dir, "onlinetime",
484 				      cpu_info__online_time(diag204_info_type,
485 							    cpu_info));
486 		if (IS_ERR(rc))
487 			return PTR_ERR(rc);
488 	}
489 	diag224_idx2name(cpu_info__ctidx(diag204_info_type, cpu_info), buffer);
490 	rc = hypfs_create_str(cpu_dir, "type", buffer);
491 	return PTR_ERR_OR_ZERO(rc);
492 }
493 
494 static void *hypfs_create_lpar_files(struct dentry *systems_dir, void *part_hdr)
495 {
496 	struct dentry *cpus_dir;
497 	struct dentry *lpar_dir;
498 	char lpar_name[DIAG204_LPAR_NAME_LEN + 1];
499 	void *cpu_info;
500 	int i;
501 
502 	part_hdr__part_name(diag204_info_type, part_hdr, lpar_name);
503 	lpar_name[DIAG204_LPAR_NAME_LEN] = 0;
504 	lpar_dir = hypfs_mkdir(systems_dir, lpar_name);
505 	if (IS_ERR(lpar_dir))
506 		return lpar_dir;
507 	cpus_dir = hypfs_mkdir(lpar_dir, "cpus");
508 	if (IS_ERR(cpus_dir))
509 		return cpus_dir;
510 	cpu_info = part_hdr + part_hdr__size(diag204_info_type);
511 	for (i = 0; i < part_hdr__rcpus(diag204_info_type, part_hdr); i++) {
512 		int rc;
513 		rc = hypfs_create_cpu_files(cpus_dir, cpu_info);
514 		if (rc)
515 			return ERR_PTR(rc);
516 		cpu_info += cpu_info__size(diag204_info_type);
517 	}
518 	return cpu_info;
519 }
520 
521 static int hypfs_create_phys_cpu_files(struct dentry *cpus_dir, void *cpu_info)
522 {
523 	struct dentry *cpu_dir;
524 	char buffer[TMP_SIZE];
525 	void *rc;
526 
527 	snprintf(buffer, TMP_SIZE, "%i", phys_cpu__cpu_addr(diag204_info_type,
528 							    cpu_info));
529 	cpu_dir = hypfs_mkdir(cpus_dir, buffer);
530 	if (IS_ERR(cpu_dir))
531 		return PTR_ERR(cpu_dir);
532 	rc = hypfs_create_u64(cpu_dir, "mgmtime",
533 			      phys_cpu__mgm_time(diag204_info_type, cpu_info));
534 	if (IS_ERR(rc))
535 		return PTR_ERR(rc);
536 	diag224_idx2name(phys_cpu__ctidx(diag204_info_type, cpu_info), buffer);
537 	rc = hypfs_create_str(cpu_dir, "type", buffer);
538 	return PTR_ERR_OR_ZERO(rc);
539 }
540 
541 static void *hypfs_create_phys_files(struct dentry *parent_dir, void *phys_hdr)
542 {
543 	int i;
544 	void *cpu_info;
545 	struct dentry *cpus_dir;
546 
547 	cpus_dir = hypfs_mkdir(parent_dir, "cpus");
548 	if (IS_ERR(cpus_dir))
549 		return cpus_dir;
550 	cpu_info = phys_hdr + phys_hdr__size(diag204_info_type);
551 	for (i = 0; i < phys_hdr__cpus(diag204_info_type, phys_hdr); i++) {
552 		int rc;
553 		rc = hypfs_create_phys_cpu_files(cpus_dir, cpu_info);
554 		if (rc)
555 			return ERR_PTR(rc);
556 		cpu_info += phys_cpu__size(diag204_info_type);
557 	}
558 	return cpu_info;
559 }
560 
561 int hypfs_diag_create_files(struct dentry *root)
562 {
563 	struct dentry *systems_dir, *hyp_dir;
564 	void *time_hdr, *part_hdr;
565 	int i, rc;
566 	void *buffer, *ptr;
567 
568 	buffer = diag204_store();
569 	if (IS_ERR(buffer))
570 		return PTR_ERR(buffer);
571 
572 	systems_dir = hypfs_mkdir(root, "systems");
573 	if (IS_ERR(systems_dir)) {
574 		rc = PTR_ERR(systems_dir);
575 		goto err_out;
576 	}
577 	time_hdr = (struct x_info_blk_hdr *)buffer;
578 	part_hdr = time_hdr + info_blk_hdr__size(diag204_info_type);
579 	for (i = 0; i < info_blk_hdr__npar(diag204_info_type, time_hdr); i++) {
580 		part_hdr = hypfs_create_lpar_files(systems_dir, part_hdr);
581 		if (IS_ERR(part_hdr)) {
582 			rc = PTR_ERR(part_hdr);
583 			goto err_out;
584 		}
585 	}
586 	if (info_blk_hdr__flags(diag204_info_type, time_hdr) &
587 	    DIAG204_LPAR_PHYS_FLG) {
588 		ptr = hypfs_create_phys_files(root, part_hdr);
589 		if (IS_ERR(ptr)) {
590 			rc = PTR_ERR(ptr);
591 			goto err_out;
592 		}
593 	}
594 	hyp_dir = hypfs_mkdir(root, "hyp");
595 	if (IS_ERR(hyp_dir)) {
596 		rc = PTR_ERR(hyp_dir);
597 		goto err_out;
598 	}
599 	ptr = hypfs_create_str(hyp_dir, "type", "LPAR Hypervisor");
600 	if (IS_ERR(ptr)) {
601 		rc = PTR_ERR(ptr);
602 		goto err_out;
603 	}
604 	rc = 0;
605 
606 err_out:
607 	return rc;
608 }
609