xref: /openbmc/linux/arch/s390/hypfs/hypfs_diag_fs.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
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_diag.h"
22 #include "hypfs.h"
23 
24 #define TMP_SIZE 64		/* size of temporary buffers */
25 
26 static char *diag224_cpu_names;			/* diag 224 name table */
27 static int diag224_idx2name(int index, char *name);
28 
29 /*
30  * DIAG 204 member access functions.
31  *
32  * Since we have two different diag 204 data formats for old and new s390
33  * machines, we do not access the structs directly, but use getter functions for
34  * each struct member instead. This should make the code more readable.
35  */
36 
37 /* Time information block */
38 
info_blk_hdr__size(enum diag204_format type)39 static inline int info_blk_hdr__size(enum diag204_format type)
40 {
41 	if (type == DIAG204_INFO_SIMPLE)
42 		return sizeof(struct diag204_info_blk_hdr);
43 	else /* DIAG204_INFO_EXT */
44 		return sizeof(struct diag204_x_info_blk_hdr);
45 }
46 
info_blk_hdr__npar(enum diag204_format type,void * hdr)47 static inline __u8 info_blk_hdr__npar(enum diag204_format type, void *hdr)
48 {
49 	if (type == DIAG204_INFO_SIMPLE)
50 		return ((struct diag204_info_blk_hdr *)hdr)->npar;
51 	else /* DIAG204_INFO_EXT */
52 		return ((struct diag204_x_info_blk_hdr *)hdr)->npar;
53 }
54 
info_blk_hdr__flags(enum diag204_format type,void * hdr)55 static inline __u8 info_blk_hdr__flags(enum diag204_format type, void *hdr)
56 {
57 	if (type == DIAG204_INFO_SIMPLE)
58 		return ((struct diag204_info_blk_hdr *)hdr)->flags;
59 	else /* DIAG204_INFO_EXT */
60 		return ((struct diag204_x_info_blk_hdr *)hdr)->flags;
61 }
62 
63 /* Partition header */
64 
part_hdr__size(enum diag204_format type)65 static inline int part_hdr__size(enum diag204_format type)
66 {
67 	if (type == DIAG204_INFO_SIMPLE)
68 		return sizeof(struct diag204_part_hdr);
69 	else /* DIAG204_INFO_EXT */
70 		return sizeof(struct diag204_x_part_hdr);
71 }
72 
part_hdr__rcpus(enum diag204_format type,void * hdr)73 static inline __u8 part_hdr__rcpus(enum diag204_format type, void *hdr)
74 {
75 	if (type == DIAG204_INFO_SIMPLE)
76 		return ((struct diag204_part_hdr *)hdr)->cpus;
77 	else /* DIAG204_INFO_EXT */
78 		return ((struct diag204_x_part_hdr *)hdr)->rcpus;
79 }
80 
part_hdr__part_name(enum diag204_format type,void * hdr,char * name)81 static inline void part_hdr__part_name(enum diag204_format type, void *hdr,
82 				       char *name)
83 {
84 	if (type == DIAG204_INFO_SIMPLE)
85 		memcpy(name, ((struct diag204_part_hdr *)hdr)->part_name,
86 		       DIAG204_LPAR_NAME_LEN);
87 	else /* DIAG204_INFO_EXT */
88 		memcpy(name, ((struct diag204_x_part_hdr *)hdr)->part_name,
89 		       DIAG204_LPAR_NAME_LEN);
90 	EBCASC(name, DIAG204_LPAR_NAME_LEN);
91 	name[DIAG204_LPAR_NAME_LEN] = 0;
92 	strim(name);
93 }
94 
95 /* CPU info block */
96 
cpu_info__size(enum diag204_format type)97 static inline int cpu_info__size(enum diag204_format type)
98 {
99 	if (type == DIAG204_INFO_SIMPLE)
100 		return sizeof(struct diag204_cpu_info);
101 	else /* DIAG204_INFO_EXT */
102 		return sizeof(struct diag204_x_cpu_info);
103 }
104 
cpu_info__ctidx(enum diag204_format type,void * hdr)105 static inline __u8 cpu_info__ctidx(enum diag204_format type, void *hdr)
106 {
107 	if (type == DIAG204_INFO_SIMPLE)
108 		return ((struct diag204_cpu_info *)hdr)->ctidx;
109 	else /* DIAG204_INFO_EXT */
110 		return ((struct diag204_x_cpu_info *)hdr)->ctidx;
111 }
112 
cpu_info__cpu_addr(enum diag204_format type,void * hdr)113 static inline __u16 cpu_info__cpu_addr(enum diag204_format type, void *hdr)
114 {
115 	if (type == DIAG204_INFO_SIMPLE)
116 		return ((struct diag204_cpu_info *)hdr)->cpu_addr;
117 	else /* DIAG204_INFO_EXT */
118 		return ((struct diag204_x_cpu_info *)hdr)->cpu_addr;
119 }
120 
cpu_info__acc_time(enum diag204_format type,void * hdr)121 static inline __u64 cpu_info__acc_time(enum diag204_format type, void *hdr)
122 {
123 	if (type == DIAG204_INFO_SIMPLE)
124 		return ((struct diag204_cpu_info *)hdr)->acc_time;
125 	else /* DIAG204_INFO_EXT */
126 		return ((struct diag204_x_cpu_info *)hdr)->acc_time;
127 }
128 
cpu_info__lp_time(enum diag204_format type,void * hdr)129 static inline __u64 cpu_info__lp_time(enum diag204_format type, void *hdr)
130 {
131 	if (type == DIAG204_INFO_SIMPLE)
132 		return ((struct diag204_cpu_info *)hdr)->lp_time;
133 	else /* DIAG204_INFO_EXT */
134 		return ((struct diag204_x_cpu_info *)hdr)->lp_time;
135 }
136 
cpu_info__online_time(enum diag204_format type,void * hdr)137 static inline __u64 cpu_info__online_time(enum diag204_format type, void *hdr)
138 {
139 	if (type == DIAG204_INFO_SIMPLE)
140 		return 0;	/* online_time not available in simple info */
141 	else /* DIAG204_INFO_EXT */
142 		return ((struct diag204_x_cpu_info *)hdr)->online_time;
143 }
144 
145 /* Physical header */
146 
phys_hdr__size(enum diag204_format type)147 static inline int phys_hdr__size(enum diag204_format type)
148 {
149 	if (type == DIAG204_INFO_SIMPLE)
150 		return sizeof(struct diag204_phys_hdr);
151 	else /* DIAG204_INFO_EXT */
152 		return sizeof(struct diag204_x_phys_hdr);
153 }
154 
phys_hdr__cpus(enum diag204_format type,void * hdr)155 static inline __u8 phys_hdr__cpus(enum diag204_format type, void *hdr)
156 {
157 	if (type == DIAG204_INFO_SIMPLE)
158 		return ((struct diag204_phys_hdr *)hdr)->cpus;
159 	else /* DIAG204_INFO_EXT */
160 		return ((struct diag204_x_phys_hdr *)hdr)->cpus;
161 }
162 
163 /* Physical CPU info block */
164 
phys_cpu__size(enum diag204_format type)165 static inline int phys_cpu__size(enum diag204_format type)
166 {
167 	if (type == DIAG204_INFO_SIMPLE)
168 		return sizeof(struct diag204_phys_cpu);
169 	else /* DIAG204_INFO_EXT */
170 		return sizeof(struct diag204_x_phys_cpu);
171 }
172 
phys_cpu__cpu_addr(enum diag204_format type,void * hdr)173 static inline __u16 phys_cpu__cpu_addr(enum diag204_format type, void *hdr)
174 {
175 	if (type == DIAG204_INFO_SIMPLE)
176 		return ((struct diag204_phys_cpu *)hdr)->cpu_addr;
177 	else /* DIAG204_INFO_EXT */
178 		return ((struct diag204_x_phys_cpu *)hdr)->cpu_addr;
179 }
180 
phys_cpu__mgm_time(enum diag204_format type,void * hdr)181 static inline __u64 phys_cpu__mgm_time(enum diag204_format type, void *hdr)
182 {
183 	if (type == DIAG204_INFO_SIMPLE)
184 		return ((struct diag204_phys_cpu *)hdr)->mgm_time;
185 	else /* DIAG204_INFO_EXT */
186 		return ((struct diag204_x_phys_cpu *)hdr)->mgm_time;
187 }
188 
phys_cpu__ctidx(enum diag204_format type,void * hdr)189 static inline __u64 phys_cpu__ctidx(enum diag204_format type, void *hdr)
190 {
191 	if (type == DIAG204_INFO_SIMPLE)
192 		return ((struct diag204_phys_cpu *)hdr)->ctidx;
193 	else /* DIAG204_INFO_EXT */
194 		return ((struct diag204_x_phys_cpu *)hdr)->ctidx;
195 }
196 
197 /*
198  * Functions to create the directory structure
199  * *******************************************
200  */
201 
hypfs_create_cpu_files(struct dentry * cpus_dir,void * cpu_info)202 static int hypfs_create_cpu_files(struct dentry *cpus_dir, void *cpu_info)
203 {
204 	struct dentry *cpu_dir;
205 	char buffer[TMP_SIZE];
206 	void *rc;
207 
208 	snprintf(buffer, TMP_SIZE, "%d", cpu_info__cpu_addr(diag204_get_info_type(),
209 							    cpu_info));
210 	cpu_dir = hypfs_mkdir(cpus_dir, buffer);
211 	if (IS_ERR(cpu_dir))
212 		return PTR_ERR(cpu_dir);
213 	rc = hypfs_create_u64(cpu_dir, "mgmtime",
214 			      cpu_info__acc_time(diag204_get_info_type(), cpu_info) -
215 			      cpu_info__lp_time(diag204_get_info_type(), cpu_info));
216 	if (IS_ERR(rc))
217 		return PTR_ERR(rc);
218 	rc = hypfs_create_u64(cpu_dir, "cputime",
219 			      cpu_info__lp_time(diag204_get_info_type(), cpu_info));
220 	if (IS_ERR(rc))
221 		return PTR_ERR(rc);
222 	if (diag204_get_info_type() == DIAG204_INFO_EXT) {
223 		rc = hypfs_create_u64(cpu_dir, "onlinetime",
224 				      cpu_info__online_time(diag204_get_info_type(),
225 							    cpu_info));
226 		if (IS_ERR(rc))
227 			return PTR_ERR(rc);
228 	}
229 	diag224_idx2name(cpu_info__ctidx(diag204_get_info_type(), cpu_info), buffer);
230 	rc = hypfs_create_str(cpu_dir, "type", buffer);
231 	return PTR_ERR_OR_ZERO(rc);
232 }
233 
hypfs_create_lpar_files(struct dentry * systems_dir,void * part_hdr)234 static void *hypfs_create_lpar_files(struct dentry *systems_dir, void *part_hdr)
235 {
236 	struct dentry *cpus_dir;
237 	struct dentry *lpar_dir;
238 	char lpar_name[DIAG204_LPAR_NAME_LEN + 1];
239 	void *cpu_info;
240 	int i;
241 
242 	part_hdr__part_name(diag204_get_info_type(), part_hdr, lpar_name);
243 	lpar_name[DIAG204_LPAR_NAME_LEN] = 0;
244 	lpar_dir = hypfs_mkdir(systems_dir, lpar_name);
245 	if (IS_ERR(lpar_dir))
246 		return lpar_dir;
247 	cpus_dir = hypfs_mkdir(lpar_dir, "cpus");
248 	if (IS_ERR(cpus_dir))
249 		return cpus_dir;
250 	cpu_info = part_hdr + part_hdr__size(diag204_get_info_type());
251 	for (i = 0; i < part_hdr__rcpus(diag204_get_info_type(), part_hdr); i++) {
252 		int rc;
253 
254 		rc = hypfs_create_cpu_files(cpus_dir, cpu_info);
255 		if (rc)
256 			return ERR_PTR(rc);
257 		cpu_info += cpu_info__size(diag204_get_info_type());
258 	}
259 	return cpu_info;
260 }
261 
hypfs_create_phys_cpu_files(struct dentry * cpus_dir,void * cpu_info)262 static int hypfs_create_phys_cpu_files(struct dentry *cpus_dir, void *cpu_info)
263 {
264 	struct dentry *cpu_dir;
265 	char buffer[TMP_SIZE];
266 	void *rc;
267 
268 	snprintf(buffer, TMP_SIZE, "%i", phys_cpu__cpu_addr(diag204_get_info_type(),
269 							    cpu_info));
270 	cpu_dir = hypfs_mkdir(cpus_dir, buffer);
271 	if (IS_ERR(cpu_dir))
272 		return PTR_ERR(cpu_dir);
273 	rc = hypfs_create_u64(cpu_dir, "mgmtime",
274 			      phys_cpu__mgm_time(diag204_get_info_type(), cpu_info));
275 	if (IS_ERR(rc))
276 		return PTR_ERR(rc);
277 	diag224_idx2name(phys_cpu__ctidx(diag204_get_info_type(), cpu_info), buffer);
278 	rc = hypfs_create_str(cpu_dir, "type", buffer);
279 	return PTR_ERR_OR_ZERO(rc);
280 }
281 
hypfs_create_phys_files(struct dentry * parent_dir,void * phys_hdr)282 static void *hypfs_create_phys_files(struct dentry *parent_dir, void *phys_hdr)
283 {
284 	int i;
285 	void *cpu_info;
286 	struct dentry *cpus_dir;
287 
288 	cpus_dir = hypfs_mkdir(parent_dir, "cpus");
289 	if (IS_ERR(cpus_dir))
290 		return cpus_dir;
291 	cpu_info = phys_hdr + phys_hdr__size(diag204_get_info_type());
292 	for (i = 0; i < phys_hdr__cpus(diag204_get_info_type(), phys_hdr); i++) {
293 		int rc;
294 
295 		rc = hypfs_create_phys_cpu_files(cpus_dir, cpu_info);
296 		if (rc)
297 			return ERR_PTR(rc);
298 		cpu_info += phys_cpu__size(diag204_get_info_type());
299 	}
300 	return cpu_info;
301 }
302 
hypfs_diag_create_files(struct dentry * root)303 int hypfs_diag_create_files(struct dentry *root)
304 {
305 	struct dentry *systems_dir, *hyp_dir;
306 	void *time_hdr, *part_hdr;
307 	void *buffer, *ptr;
308 	int i, rc, pages;
309 
310 	buffer = diag204_get_buffer(diag204_get_info_type(), &pages);
311 	if (IS_ERR(buffer))
312 		return PTR_ERR(buffer);
313 	rc = diag204_store(buffer, pages);
314 	if (rc)
315 		return rc;
316 
317 	systems_dir = hypfs_mkdir(root, "systems");
318 	if (IS_ERR(systems_dir)) {
319 		rc = PTR_ERR(systems_dir);
320 		goto err_out;
321 	}
322 	time_hdr = (struct x_info_blk_hdr *)buffer;
323 	part_hdr = time_hdr + info_blk_hdr__size(diag204_get_info_type());
324 	for (i = 0; i < info_blk_hdr__npar(diag204_get_info_type(), time_hdr); i++) {
325 		part_hdr = hypfs_create_lpar_files(systems_dir, part_hdr);
326 		if (IS_ERR(part_hdr)) {
327 			rc = PTR_ERR(part_hdr);
328 			goto err_out;
329 		}
330 	}
331 	if (info_blk_hdr__flags(diag204_get_info_type(), time_hdr) &
332 	    DIAG204_LPAR_PHYS_FLG) {
333 		ptr = hypfs_create_phys_files(root, part_hdr);
334 		if (IS_ERR(ptr)) {
335 			rc = PTR_ERR(ptr);
336 			goto err_out;
337 		}
338 	}
339 	hyp_dir = hypfs_mkdir(root, "hyp");
340 	if (IS_ERR(hyp_dir)) {
341 		rc = PTR_ERR(hyp_dir);
342 		goto err_out;
343 	}
344 	ptr = hypfs_create_str(hyp_dir, "type", "LPAR Hypervisor");
345 	if (IS_ERR(ptr)) {
346 		rc = PTR_ERR(ptr);
347 		goto err_out;
348 	}
349 	rc = 0;
350 
351 err_out:
352 	return rc;
353 }
354 
355 /* Diagnose 224 functions */
356 
diag224_idx2name(int index,char * name)357 static int diag224_idx2name(int index, char *name)
358 {
359 	memcpy(name, diag224_cpu_names + ((index + 1) * DIAG204_CPU_NAME_LEN),
360 	       DIAG204_CPU_NAME_LEN);
361 	name[DIAG204_CPU_NAME_LEN] = 0;
362 	strim(name);
363 	return 0;
364 }
365 
diag224_get_name_table(void)366 static int diag224_get_name_table(void)
367 {
368 	/* memory must be below 2GB */
369 	diag224_cpu_names = (char *)__get_free_page(GFP_KERNEL | GFP_DMA);
370 	if (!diag224_cpu_names)
371 		return -ENOMEM;
372 	if (diag224(diag224_cpu_names)) {
373 		free_page((unsigned long)diag224_cpu_names);
374 		return -EOPNOTSUPP;
375 	}
376 	EBCASC(diag224_cpu_names + 16, (*diag224_cpu_names + 1) * 16);
377 	return 0;
378 }
379 
diag224_delete_name_table(void)380 static void diag224_delete_name_table(void)
381 {
382 	free_page((unsigned long)diag224_cpu_names);
383 }
384 
__hypfs_diag_fs_init(void)385 int __init __hypfs_diag_fs_init(void)
386 {
387 	if (MACHINE_IS_LPAR)
388 		return diag224_get_name_table();
389 	return 0;
390 }
391 
__hypfs_diag_fs_exit(void)392 void __hypfs_diag_fs_exit(void)
393 {
394 	diag224_delete_name_table();
395 }
396