Linux Audio

Check our new training course

Loading...
v6.2
  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
 27static char *diag224_cpu_names;			/* diag 224 name table */
 28static enum diag204_sc diag204_store_sc;	/* used subcode for store */
 29static enum diag204_format diag204_info_type;	/* used diag 204 data format */
 30
 31static void *diag204_buf;		/* 4K aligned buffer for diag204 data */
 32static void *diag204_buf_vmalloc;	/* vmalloc pointer for diag204 data */
 33static int diag204_buf_pages;		/* number of pages for diag204 data */
 34
 35static 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
 47static 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
 55static 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
 63static 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
 73static 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
 81static 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
 89static 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
105static 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
113static 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
121static 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
129static 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
137static 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
145static 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
155static 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
163static 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
173static 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
181static 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
189static 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
197static 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
213static 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
226static void *page_align_ptr(void *ptr)
227{
228	return (void *) PAGE_ALIGN((unsigned long) ptr);
229}
230
231static 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
242static 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
251static 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
284static 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	}
322out:
323	rc = 0;
324fail_store:
325	diag204_free_buffer();
326fail_alloc:
327	return rc;
328}
329
330static 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
339static 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);
350out:
351	return buf;
352}
353
354/* Diagnose 224 functions */
355
356static 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
370static void diag224_delete_name_table(void)
371{
372	free_page((unsigned long) diag224_cpu_names);
373}
374
375static 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
384struct 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
391struct dbfs_d204 {
392	struct dbfs_d204_hdr	hdr;	/* 64 byte header */
393	char			buf[];	/* d204 buffer */
394} __attribute__ ((packed));
395
396static 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
421static 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
451void 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
464static 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
494static 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
521static 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
541static 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
561int 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
606err_out:
607	return rc;
608}
v6.9.4
  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 DBFS_D204_HDR_VERSION	0
 25
 
 26static enum diag204_sc diag204_store_sc;	/* used subcode for store */
 27static enum diag204_format diag204_info_type;	/* used diag 204 data format */
 28
 29static void *diag204_buf;		/* 4K aligned buffer for diag204 data */
 
 30static int diag204_buf_pages;		/* number of pages for diag204 data */
 31
 32static struct dentry *dbfs_d204_file;
 33
 34enum diag204_format diag204_get_info_type(void)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 35{
 36	return diag204_info_type;
 
 
 
 37}
 38
 39static void diag204_set_info_type(enum diag204_format type)
 40{
 41	diag204_info_type = type;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 42}
 43
 44/* Diagnose 204 functions */
 45/*
 46 * For the old diag subcode 4 with simple data format we have to use real
 47 * memory. If we use subcode 6 or 7 with extended data format, we can (and
 48 * should) use vmalloc, since we need a lot of memory in that case. Currently
 49 * up to 93 pages!
 50 */
 51
 52static void diag204_free_buffer(void)
 53{
 54	vfree(diag204_buf);
 
 
 
 
 
 
 
 55	diag204_buf = NULL;
 56}
 57
 58void *diag204_get_buffer(enum diag204_format fmt, int *pages)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 59{
 60	if (diag204_buf) {
 61		*pages = diag204_buf_pages;
 62		return diag204_buf;
 63	}
 64	if (fmt == DIAG204_INFO_SIMPLE) {
 65		*pages = 1;
 
 66	} else {/* DIAG204_INFO_EXT */
 67		*pages = diag204((unsigned long)DIAG204_SUBC_RSI |
 68				 (unsigned long)DIAG204_INFO_EXT, 0, NULL);
 69		if (*pages <= 0)
 70			return ERR_PTR(-EOPNOTSUPP);
 
 
 71	}
 72	diag204_buf = __vmalloc_node(array_size(*pages, PAGE_SIZE),
 73				     PAGE_SIZE, GFP_KERNEL, NUMA_NO_NODE,
 74				     __builtin_return_address(0));
 75	if (!diag204_buf)
 76		return ERR_PTR(-ENOMEM);
 77	diag204_buf_pages = *pages;
 78	return diag204_buf;
 79}
 80
 81/*
 82 * diag204_probe() has to find out, which type of diagnose 204 implementation
 83 * we have on our machine. Currently there are three possible scanarios:
 84 *   - subcode 4   + simple data format (only one page)
 85 *   - subcode 4-6 + extended data format
 86 *   - subcode 4-7 + extended data format
 87 *
 88 * Subcode 5 is used to retrieve the size of the data, provided by subcodes
 89 * 6 and 7. Subcode 7 basically has the same function as subcode 6. In addition
 90 * to subcode 6 it provides also information about secondary cpus.
 91 * In order to get as much information as possible, we first try
 92 * subcode 7, then 6 and if both fail, we use subcode 4.
 93 */
 94
 95static int diag204_probe(void)
 96{
 97	void *buf;
 98	int pages, rc;
 99
100	buf = diag204_get_buffer(DIAG204_INFO_EXT, &pages);
101	if (!IS_ERR(buf)) {
102		if (diag204((unsigned long)DIAG204_SUBC_STIB7 |
103			    (unsigned long)DIAG204_INFO_EXT, pages, buf) >= 0) {
104			diag204_store_sc = DIAG204_SUBC_STIB7;
105			diag204_set_info_type(DIAG204_INFO_EXT);
106			goto out;
107		}
108		if (diag204((unsigned long)DIAG204_SUBC_STIB6 |
109			    (unsigned long)DIAG204_INFO_EXT, pages, buf) >= 0) {
110			diag204_store_sc = DIAG204_SUBC_STIB6;
111			diag204_set_info_type(DIAG204_INFO_EXT);
112			goto out;
113		}
114		diag204_free_buffer();
115	}
116
117	/* subcodes 6 and 7 failed, now try subcode 4 */
118
119	buf = diag204_get_buffer(DIAG204_INFO_SIMPLE, &pages);
120	if (IS_ERR(buf)) {
121		rc = PTR_ERR(buf);
122		goto fail_alloc;
123	}
124	if (diag204((unsigned long)DIAG204_SUBC_STIB4 |
125		    (unsigned long)DIAG204_INFO_SIMPLE, pages, buf) >= 0) {
126		diag204_store_sc = DIAG204_SUBC_STIB4;
127		diag204_set_info_type(DIAG204_INFO_SIMPLE);
128		goto out;
129	} else {
130		rc = -EOPNOTSUPP;
131		goto fail_store;
132	}
133out:
134	rc = 0;
135fail_store:
136	diag204_free_buffer();
137fail_alloc:
138	return rc;
139}
140
141int diag204_store(void *buf, int pages)
142{
143	int rc;
144
145	rc = diag204((unsigned long)diag204_store_sc |
146		     (unsigned long)diag204_get_info_type(), pages, buf);
147	return rc < 0 ? -EOPNOTSUPP : 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148}
149
150struct dbfs_d204_hdr {
151	u64	len;		/* Length of d204 buffer without header */
152	u16	version;	/* Version of header */
153	u8	sc;		/* Used subcode */
154	char	reserved[53];
155} __attribute__ ((packed));
156
157struct dbfs_d204 {
158	struct dbfs_d204_hdr	hdr;	/* 64 byte header */
159	char			buf[];	/* d204 buffer */
160} __attribute__ ((packed));
161
162static int dbfs_d204_create(void **data, void **data_free_ptr, size_t *size)
163{
164	struct dbfs_d204 *d204;
165	int rc, buf_size;
166	void *base;
167
168	buf_size = PAGE_SIZE * (diag204_buf_pages + 1) + sizeof(d204->hdr);
169	base = vzalloc(buf_size);
170	if (!base)
171		return -ENOMEM;
172	d204 = PTR_ALIGN(base + sizeof(d204->hdr), PAGE_SIZE) - sizeof(d204->hdr);
173	rc = diag204_store(d204->buf, diag204_buf_pages);
174	if (rc) {
175		vfree(base);
176		return rc;
177	}
178	d204->hdr.version = DBFS_D204_HDR_VERSION;
179	d204->hdr.len = PAGE_SIZE * diag204_buf_pages;
180	d204->hdr.sc = diag204_store_sc;
181	*data = d204;
182	*data_free_ptr = base;
183	*size = d204->hdr.len + sizeof(struct dbfs_d204_hdr);
184	return 0;
185}
186
187static struct hypfs_dbfs_file dbfs_file_d204 = {
188	.name		= "diag_204",
189	.data_create	= dbfs_d204_create,
190	.data_free	= vfree,
191};
192
193__init int hypfs_diag_init(void)
194{
195	int rc;
196
197	if (diag204_probe()) {
198		pr_info("The hardware system does not support hypfs\n");
199		return -ENODATA;
200	}
201
202	if (diag204_get_info_type() == DIAG204_INFO_EXT)
203		hypfs_dbfs_create_file(&dbfs_file_d204);
204
205	rc = hypfs_diag_fs_init();
206	if (rc) {
207		pr_err("The hardware system does not provide all functions required by hypfs\n");
208		debugfs_remove(dbfs_d204_file);
 
 
 
 
209	}
210	return rc;
211}
212
213void hypfs_diag_exit(void)
214{
215	debugfs_remove(dbfs_d204_file);
216	hypfs_diag_fs_exit();
217	diag204_free_buffer();
218	hypfs_dbfs_remove_file(&dbfs_file_d204);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219}