xref: /linux/drivers/parisc/pdc_stable.c (revision d24846a4246b6e61ecbd036880a4adf61681d241)
145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *    Interfaces to retrieve and set PDC Stable options (firmware)
41da177e4SLinus Torvalds  *
5c7428422SThibaut VARENE  *    Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *    DEV NOTE: the PDC Procedures reference states that:
81da177e4SLinus Torvalds  *    "A minimum of 96 bytes of Stable Storage is required. Providing more than
91da177e4SLinus Torvalds  *    96 bytes of Stable Storage is optional [...]. Failure to provide the
101da177e4SLinus Torvalds  *    optional locations from 96 to 192 results in the loss of certain
111da177e4SLinus Torvalds  *    functionality during boot."
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *    Since locations between 96 and 192 are the various paths, most (if not
141da177e4SLinus Torvalds  *    all) PA-RISC machines should have them. Anyway, for safety reasons, the
15c7428422SThibaut VARENE  *    following code can deal with just 96 bytes of Stable Storage, and all
161da177e4SLinus Torvalds  *    sizes between 96 and 192 bytes (provided they are multiple of struct
171da177e4SLinus Torvalds  *    device_path size, eg: 128, 160 and 192) to provide full information.
183f9edb53SThibaut Varene  *    One last word: there's one path we can always count on: the primary path.
193f9edb53SThibaut Varene  *    Anything above 224 bytes is used for 'osdep2' OS-dependent storage area.
203f9edb53SThibaut Varene  *
213f9edb53SThibaut Varene  *    The first OS-dependent area should always be available. Obviously, this is
223f9edb53SThibaut Varene  *    not true for the other one. Also bear in mind that reading/writing from/to
233f9edb53SThibaut Varene  *    osdep2 is much more expensive than from/to osdep1.
243f9edb53SThibaut Varene  *    NOTE: We do not handle the 2 bytes OS-dep area at 0x5D, nor the first
253f9edb53SThibaut Varene  *    2 bytes of storage available right after OSID. That's a total of 4 bytes
263f9edb53SThibaut Varene  *    sacrificed: -ETOOLAZY :P
27c7428422SThibaut VARENE  *
28c7428422SThibaut VARENE  *    The current policy wrt file permissions is:
29c7428422SThibaut VARENE  *	- write: root only
30c7428422SThibaut VARENE  *	- read: (reading triggers PDC calls) ? root only : everyone
31c7428422SThibaut VARENE  *    The rationale is that PDC calls could hog (DoS) the machine.
32c7428422SThibaut VARENE  *
33c7428422SThibaut VARENE  *	TODO:
34c7428422SThibaut VARENE  *	- timer/fastsize write calls
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #undef PDCS_DEBUG
381da177e4SLinus Torvalds #ifdef PDCS_DEBUG
391da177e4SLinus Torvalds #define DPRINTK(fmt, args...)	printk(KERN_DEBUG fmt, ## args)
401da177e4SLinus Torvalds #else
411da177e4SLinus Torvalds #define DPRINTK(fmt, args...)
421da177e4SLinus Torvalds #endif
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/module.h>
451da177e4SLinus Torvalds #include <linux/init.h>
461da177e4SLinus Torvalds #include <linux/kernel.h>
471da177e4SLinus Torvalds #include <linux/string.h>
48c59ede7bSRandy.Dunlap #include <linux/capability.h>
491da177e4SLinus Torvalds #include <linux/ctype.h>
501da177e4SLinus Torvalds #include <linux/sysfs.h>
511da177e4SLinus Torvalds #include <linux/kobject.h>
521da177e4SLinus Torvalds #include <linux/device.h>
531da177e4SLinus Torvalds #include <linux/errno.h>
54c7428422SThibaut VARENE #include <linux/spinlock.h>
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #include <asm/pdc.h>
571da177e4SLinus Torvalds #include <asm/page.h>
587c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
591da177e4SLinus Torvalds #include <asm/hardware.h>
601da177e4SLinus Torvalds 
613f9edb53SThibaut Varene #define PDCS_VERSION	"0.30"
62c7428422SThibaut VARENE #define PDCS_PREFIX	"PDC Stable Storage"
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #define PDCS_ADDR_PPRI	0x00
651da177e4SLinus Torvalds #define PDCS_ADDR_OSID	0x40
663f9edb53SThibaut Varene #define PDCS_ADDR_OSD1	0x48
673f9edb53SThibaut Varene #define PDCS_ADDR_DIAG	0x58
681da177e4SLinus Torvalds #define PDCS_ADDR_FSIZ	0x5C
691da177e4SLinus Torvalds #define PDCS_ADDR_PCON	0x60
701da177e4SLinus Torvalds #define PDCS_ADDR_PALT	0x80
711da177e4SLinus Torvalds #define PDCS_ADDR_PKBD	0xA0
723f9edb53SThibaut Varene #define PDCS_ADDR_OSD2	0xE0
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
751da177e4SLinus Torvalds MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
761da177e4SLinus Torvalds MODULE_LICENSE("GPL");
771da177e4SLinus Torvalds MODULE_VERSION(PDCS_VERSION);
781da177e4SLinus Torvalds 
79c7428422SThibaut VARENE /* holds Stable Storage size. Initialized once and for all, no lock needed */
808039de10SHelge Deller static unsigned long pdcs_size __read_mostly;
811da177e4SLinus Torvalds 
823f9edb53SThibaut Varene /* holds OS ID. Initialized once and for all, hopefully to 0x0006 */
833f9edb53SThibaut Varene static u16 pdcs_osid __read_mostly;
843f9edb53SThibaut Varene 
851da177e4SLinus Torvalds /* This struct defines what we need to deal with a parisc pdc path entry */
861da177e4SLinus Torvalds struct pdcspath_entry {
87c7428422SThibaut VARENE 	rwlock_t rw_lock;		/* to protect path entry access */
881da177e4SLinus Torvalds 	short ready;			/* entry record is valid if != 0 */
891da177e4SLinus Torvalds 	unsigned long addr;		/* entry address in stable storage */
901da177e4SLinus Torvalds 	char *name;			/* entry name */
911da177e4SLinus Torvalds 	struct device_path devpath;	/* device path in parisc representation */
921da177e4SLinus Torvalds 	struct device *dev;		/* corresponding device */
931da177e4SLinus Torvalds 	struct kobject kobj;
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds struct pdcspath_attribute {
971da177e4SLinus Torvalds 	struct attribute attr;
981da177e4SLinus Torvalds 	ssize_t (*show)(struct pdcspath_entry *entry, char *buf);
991da177e4SLinus Torvalds 	ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count);
1001da177e4SLinus Torvalds };
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds #define PDCSPATH_ENTRY(_addr, _name) \
1031da177e4SLinus Torvalds struct pdcspath_entry pdcspath_entry_##_name = { \
1041da177e4SLinus Torvalds 	.ready = 0, \
1051da177e4SLinus Torvalds 	.addr = _addr, \
1061da177e4SLinus Torvalds 	.name = __stringify(_name), \
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds #define PDCS_ATTR(_name, _mode, _show, _store) \
1107f548217SGreg Kroah-Hartman struct kobj_attribute pdcs_attr_##_name = { \
1117b595756STejun Heo 	.attr = {.name = __stringify(_name), .mode = _mode}, \
1121da177e4SLinus Torvalds 	.show = _show, \
1131da177e4SLinus Torvalds 	.store = _store, \
1141da177e4SLinus Torvalds };
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #define PATHS_ATTR(_name, _mode, _show, _store) \
1171da177e4SLinus Torvalds struct pdcspath_attribute paths_attr_##_name = { \
1187b595756STejun Heo 	.attr = {.name = __stringify(_name), .mode = _mode}, \
1191da177e4SLinus Torvalds 	.show = _show, \
1201da177e4SLinus Torvalds 	.store = _store, \
1211da177e4SLinus Torvalds };
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #define to_pdcspath_attribute(_attr) container_of(_attr, struct pdcspath_attribute, attr)
1241da177e4SLinus Torvalds #define to_pdcspath_entry(obj)  container_of(obj, struct pdcspath_entry, kobj)
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /**
1271da177e4SLinus Torvalds  * pdcspath_fetch - This function populates the path entry structs.
1281da177e4SLinus Torvalds  * @entry: A pointer to an allocated pdcspath_entry.
1291da177e4SLinus Torvalds  *
1301da177e4SLinus Torvalds  * The general idea is that you don't read from the Stable Storage every time
13125985edcSLucas De Marchi  * you access the files provided by the facilities. We store a copy of the
1321da177e4SLinus Torvalds  * content of the stable storage WRT various paths in these structs. We read
1331da177e4SLinus Torvalds  * these structs when reading the files, and we will write to these structs when
1341da177e4SLinus Torvalds  * writing to the files, and only then write them back to the Stable Storage.
135c7428422SThibaut VARENE  *
136c7428422SThibaut VARENE  * This function expects to be called with @entry->rw_lock write-hold.
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds static int
1391da177e4SLinus Torvalds pdcspath_fetch(struct pdcspath_entry *entry)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	struct device_path *devpath;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	if (!entry)
1441da177e4SLinus Torvalds 		return -EINVAL;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	devpath = &entry->devpath;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
1491da177e4SLinus Torvalds 			entry, devpath, entry->addr);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	/* addr, devpath and count must be word aligned */
1521da177e4SLinus Torvalds 	if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
1531da177e4SLinus Torvalds 		return -EIO;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	/* Find the matching device.
1561da177e4SLinus Torvalds 	   NOTE: hardware_path overlays with device_path, so the nice cast can
1571da177e4SLinus Torvalds 	   be used */
1581da177e4SLinus Torvalds 	entry->dev = hwpath_to_device((struct hardware_path *)devpath);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	entry->ready = 1;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	return 0;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /**
1681da177e4SLinus Torvalds  * pdcspath_store - This function writes a path to stable storage.
1691da177e4SLinus Torvalds  * @entry: A pointer to an allocated pdcspath_entry.
1701da177e4SLinus Torvalds  *
1711da177e4SLinus Torvalds  * It can be used in two ways: either by passing it a preset devpath struct
1721da177e4SLinus Torvalds  * containing an already computed hardware path, or by passing it a device
1731da177e4SLinus Torvalds  * pointer, from which it'll find out the corresponding hardware path.
1741da177e4SLinus Torvalds  * For now we do not handle the case where there's an error in writing to the
1751da177e4SLinus Torvalds  * Stable Storage area, so you'd better not mess up the data :P
176c7428422SThibaut VARENE  *
177c7428422SThibaut VARENE  * This function expects to be called with @entry->rw_lock write-hold.
1781da177e4SLinus Torvalds  */
179c7428422SThibaut VARENE static void
1801da177e4SLinus Torvalds pdcspath_store(struct pdcspath_entry *entry)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	struct device_path *devpath;
1831da177e4SLinus Torvalds 
184c7428422SThibaut VARENE 	BUG_ON(!entry);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	devpath = &entry->devpath;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	/* We expect the caller to set the ready flag to 0 if the hardware
1891da177e4SLinus Torvalds 	   path struct provided is invalid, so that we know we have to fill it.
1901da177e4SLinus Torvalds 	   First case, we don't have a preset hwpath... */
1911da177e4SLinus Torvalds 	if (!entry->ready) {
1921da177e4SLinus Torvalds 		/* ...but we have a device, map it */
193c7428422SThibaut VARENE 		BUG_ON(!entry->dev);
1941da177e4SLinus Torvalds 		device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 	/* else, we expect the provided hwpath to be valid. */
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
1991da177e4SLinus Torvalds 			entry, devpath, entry->addr);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* addr, devpath and count must be word aligned */
20293c3e913SJulia Lawall 	if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
20393c3e913SJulia Lawall 		WARN(1, KERN_ERR "%s: an error occurred when writing to PDC.\n"
2041da177e4SLinus Torvalds 				"It is likely that the Stable Storage data has been corrupted.\n"
2051da177e4SLinus Torvalds 				"Please check it carefully upon next reboot.\n", __func__);
2061da177e4SLinus Torvalds 
2074b991da7SThibaut VARENE 	/* kobject is already registered */
2084b991da7SThibaut VARENE 	entry->ready = 2;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds /**
2141da177e4SLinus Torvalds  * pdcspath_hwpath_read - This function handles hardware path pretty printing.
2151da177e4SLinus Torvalds  * @entry: An allocated and populated pdscpath_entry struct.
2161da177e4SLinus Torvalds  * @buf: The output buffer to write to.
2171da177e4SLinus Torvalds  *
2181da177e4SLinus Torvalds  * We will call this function to format the output of the hwpath attribute file.
2191da177e4SLinus Torvalds  */
2201da177e4SLinus Torvalds static ssize_t
2211da177e4SLinus Torvalds pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	char *out = buf;
2241da177e4SLinus Torvalds 	struct device_path *devpath;
225c7428422SThibaut VARENE 	short i;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	if (!entry || !buf)
2281da177e4SLinus Torvalds 		return -EINVAL;
2291da177e4SLinus Torvalds 
230c7428422SThibaut VARENE 	read_lock(&entry->rw_lock);
2311da177e4SLinus Torvalds 	devpath = &entry->devpath;
232c7428422SThibaut VARENE 	i = entry->ready;
233c7428422SThibaut VARENE 	read_unlock(&entry->rw_lock);
2341da177e4SLinus Torvalds 
235c7428422SThibaut VARENE 	if (!i)	/* entry is not ready */
2361da177e4SLinus Torvalds 		return -ENODATA;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	for (i = 0; i < 6; i++) {
2391da177e4SLinus Torvalds 		if (devpath->bc[i] >= 128)
2401da177e4SLinus Torvalds 			continue;
2411da177e4SLinus Torvalds 		out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
2421da177e4SLinus Torvalds 	}
2431da177e4SLinus Torvalds 	out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	return out - buf;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds /**
2491da177e4SLinus Torvalds  * pdcspath_hwpath_write - This function handles hardware path modifying.
2501da177e4SLinus Torvalds  * @entry: An allocated and populated pdscpath_entry struct.
2511da177e4SLinus Torvalds  * @buf: The input buffer to read from.
2521da177e4SLinus Torvalds  * @count: The number of bytes to be read.
2531da177e4SLinus Torvalds  *
2541da177e4SLinus Torvalds  * We will call this function to change the current hardware path.
2551da177e4SLinus Torvalds  * Hardware paths are to be given '/'-delimited, without brackets.
256c7428422SThibaut VARENE  * We make sure that the provided path actually maps to an existing
2571da177e4SLinus Torvalds  * device, BUT nothing would prevent some foolish user to set the path to some
2581da177e4SLinus Torvalds  * PCI bridge or even a CPU...
2591da177e4SLinus Torvalds  * A better work around would be to make sure we are at the end of a device tree
2601da177e4SLinus Torvalds  * for instance, but it would be IMHO beyond the simple scope of that driver.
2611da177e4SLinus Torvalds  * The aim is to provide a facility. Data correctness is left to userland.
2621da177e4SLinus Torvalds  */
2631da177e4SLinus Torvalds static ssize_t
2641da177e4SLinus Torvalds pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	struct hardware_path hwpath;
2671da177e4SLinus Torvalds 	unsigned short i;
268c735483dSHelge Deller 	char in[64], *temp;
2691da177e4SLinus Torvalds 	struct device *dev;
27026f03249SKyle McMartin 	int ret;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	if (!entry || !buf || !count)
2731da177e4SLinus Torvalds 		return -EINVAL;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/* We'll use a local copy of buf */
276c735483dSHelge Deller 	count = min_t(size_t, count, sizeof(in)-1);
2771da177e4SLinus Torvalds 	strncpy(in, buf, count);
278c735483dSHelge Deller 	in[count] = '\0';
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	/* Let's clean up the target. 0xff is a blank pattern */
2811da177e4SLinus Torvalds 	memset(&hwpath, 0xff, sizeof(hwpath));
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/* First, pick the mod field (the last one of the input string) */
2841da177e4SLinus Torvalds 	if (!(temp = strrchr(in, '/')))
2851da177e4SLinus Torvalds 		return -EINVAL;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	hwpath.mod = simple_strtoul(temp+1, NULL, 10);
2881da177e4SLinus Torvalds 	in[temp-in] = '\0';	/* truncate the remaining string. just precaution */
2891da177e4SLinus Torvalds 	DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/* Then, loop for each delimiter, making sure we don't have too many.
2921da177e4SLinus Torvalds 	   we write the bc fields in a down-top way. No matter what, we stop
2931da177e4SLinus Torvalds 	   before writing the last field. If there are too many fields anyway,
2941da177e4SLinus Torvalds 	   then the user is a moron and it'll be caught up later when we'll
2951da177e4SLinus Torvalds 	   check the consistency of the given hwpath. */
2961da177e4SLinus Torvalds 	for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
2971da177e4SLinus Torvalds 		hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
2981da177e4SLinus Torvalds 		in[temp-in] = '\0';
2991da177e4SLinus Torvalds 		DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	/* Store the final field */
3031da177e4SLinus Torvalds 	hwpath.bc[i] = simple_strtoul(in, NULL, 10);
3041da177e4SLinus Torvalds 	DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	/* Now we check that the user isn't trying to lure us */
3071da177e4SLinus Torvalds 	if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
3081da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
3091da177e4SLinus Torvalds 			"hardware path: %s\n", __func__, entry->name, buf);
3101da177e4SLinus Torvalds 		return -EINVAL;
3111da177e4SLinus Torvalds 	}
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	/* So far so good, let's get in deep */
314c7428422SThibaut VARENE 	write_lock(&entry->rw_lock);
3151da177e4SLinus Torvalds 	entry->ready = 0;
3161da177e4SLinus Torvalds 	entry->dev = dev;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	/* Now, dive in. Write back to the hardware */
319c7428422SThibaut VARENE 	pdcspath_store(entry);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	/* Update the symlink to the real device */
3221da177e4SLinus Torvalds 	sysfs_remove_link(&entry->kobj, "device");
32393964fd4SJames Bottomley 	write_unlock(&entry->rw_lock);
32493964fd4SJames Bottomley 
32526f03249SKyle McMartin 	ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
32626f03249SKyle McMartin 	WARN_ON(ret);
32726f03249SKyle McMartin 
328c7428422SThibaut VARENE 	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
3291da177e4SLinus Torvalds 		entry->name, buf);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	return count;
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds /**
3351da177e4SLinus Torvalds  * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
3361da177e4SLinus Torvalds  * @entry: An allocated and populated pdscpath_entry struct.
3371da177e4SLinus Torvalds  * @buf: The output buffer to write to.
3381da177e4SLinus Torvalds  *
3391da177e4SLinus Torvalds  * We will call this function to format the output of the layer attribute file.
3401da177e4SLinus Torvalds  */
3411da177e4SLinus Torvalds static ssize_t
3421da177e4SLinus Torvalds pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	char *out = buf;
3451da177e4SLinus Torvalds 	struct device_path *devpath;
346c7428422SThibaut VARENE 	short i;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	if (!entry || !buf)
3491da177e4SLinus Torvalds 		return -EINVAL;
3501da177e4SLinus Torvalds 
351c7428422SThibaut VARENE 	read_lock(&entry->rw_lock);
3521da177e4SLinus Torvalds 	devpath = &entry->devpath;
353c7428422SThibaut VARENE 	i = entry->ready;
354c7428422SThibaut VARENE 	read_unlock(&entry->rw_lock);
3551da177e4SLinus Torvalds 
356c7428422SThibaut VARENE 	if (!i)	/* entry is not ready */
3571da177e4SLinus Torvalds 		return -ENODATA;
3581da177e4SLinus Torvalds 
359447c233dSRoel Kluin 	for (i = 0; i < 6 && devpath->layers[i]; i++)
3601da177e4SLinus Torvalds 		out += sprintf(out, "%u ", devpath->layers[i]);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	out += sprintf(out, "\n");
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	return out - buf;
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds /**
3681da177e4SLinus Torvalds  * pdcspath_layer_write - This function handles extended layer modifying.
3691da177e4SLinus Torvalds  * @entry: An allocated and populated pdscpath_entry struct.
3701da177e4SLinus Torvalds  * @buf: The input buffer to read from.
3711da177e4SLinus Torvalds  * @count: The number of bytes to be read.
3721da177e4SLinus Torvalds  *
3731da177e4SLinus Torvalds  * We will call this function to change the current layer value.
3741da177e4SLinus Torvalds  * Layers are to be given '.'-delimited, without brackets.
3751da177e4SLinus Torvalds  * XXX beware we are far less checky WRT input data provided than for hwpath.
3761da177e4SLinus Torvalds  * Potential harm can be done, since there's no way to check the validity of
3771da177e4SLinus Torvalds  * the layer fields.
3781da177e4SLinus Torvalds  */
3791da177e4SLinus Torvalds static ssize_t
3801da177e4SLinus Torvalds pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds 	unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
3831da177e4SLinus Torvalds 	unsigned short i;
384c735483dSHelge Deller 	char in[64], *temp;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	if (!entry || !buf || !count)
3871da177e4SLinus Torvalds 		return -EINVAL;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/* We'll use a local copy of buf */
390c735483dSHelge Deller 	count = min_t(size_t, count, sizeof(in)-1);
3911da177e4SLinus Torvalds 	strncpy(in, buf, count);
392c735483dSHelge Deller 	in[count] = '\0';
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	/* Let's clean up the target. 0 is a blank pattern */
3951da177e4SLinus Torvalds 	memset(&layers, 0, sizeof(layers));
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	/* First, pick the first layer */
3981da177e4SLinus Torvalds 	if (unlikely(!isdigit(*in)))
3991da177e4SLinus Torvalds 		return -EINVAL;
4001da177e4SLinus Torvalds 	layers[0] = simple_strtoul(in, NULL, 10);
4011da177e4SLinus Torvalds 	DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	temp = in;
4041da177e4SLinus Torvalds 	for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
4051da177e4SLinus Torvalds 		if (unlikely(!isdigit(*(++temp))))
4061da177e4SLinus Torvalds 			return -EINVAL;
4071da177e4SLinus Torvalds 		layers[i] = simple_strtoul(temp, NULL, 10);
4081da177e4SLinus Torvalds 		DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	/* So far so good, let's get in deep */
412c7428422SThibaut VARENE 	write_lock(&entry->rw_lock);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	/* First, overwrite the current layers with the new ones, not touching
4151da177e4SLinus Torvalds 	   the hardware path. */
4161da177e4SLinus Torvalds 	memcpy(&entry->devpath.layers, &layers, sizeof(layers));
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	/* Now, dive in. Write back to the hardware */
419c7428422SThibaut VARENE 	pdcspath_store(entry);
420c7428422SThibaut VARENE 	write_unlock(&entry->rw_lock);
4211da177e4SLinus Torvalds 
422c7428422SThibaut VARENE 	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
4231da177e4SLinus Torvalds 		entry->name, buf);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	return count;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds /**
4291da177e4SLinus Torvalds  * pdcspath_attr_show - Generic read function call wrapper.
4301da177e4SLinus Torvalds  * @kobj: The kobject to get info from.
4311da177e4SLinus Torvalds  * @attr: The attribute looked upon.
4321da177e4SLinus Torvalds  * @buf: The output buffer.
4331da177e4SLinus Torvalds  */
4341da177e4SLinus Torvalds static ssize_t
4351da177e4SLinus Torvalds pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
4381da177e4SLinus Torvalds 	struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
4391da177e4SLinus Torvalds 	ssize_t ret = 0;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	if (pdcs_attr->show)
4421da177e4SLinus Torvalds 		ret = pdcs_attr->show(entry, buf);
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	return ret;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /**
4481da177e4SLinus Torvalds  * pdcspath_attr_store - Generic write function call wrapper.
4491da177e4SLinus Torvalds  * @kobj: The kobject to write info to.
4501da177e4SLinus Torvalds  * @attr: The attribute to be modified.
4511da177e4SLinus Torvalds  * @buf: The input buffer.
4521da177e4SLinus Torvalds  * @count: The size of the buffer.
4531da177e4SLinus Torvalds  */
4541da177e4SLinus Torvalds static ssize_t
4551da177e4SLinus Torvalds pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
4561da177e4SLinus Torvalds 			const char *buf, size_t count)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
4591da177e4SLinus Torvalds 	struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
4601da177e4SLinus Torvalds 	ssize_t ret = 0;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
4631da177e4SLinus Torvalds 		return -EACCES;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	if (pdcs_attr->store)
4661da177e4SLinus Torvalds 		ret = pdcs_attr->store(entry, buf, count);
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	return ret;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
47152cf25d0SEmese Revfy static const struct sysfs_ops pdcspath_attr_ops = {
4721da177e4SLinus Torvalds 	.show = pdcspath_attr_show,
4731da177e4SLinus Torvalds 	.store = pdcspath_attr_store,
4741da177e4SLinus Torvalds };
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds /* These are the two attributes of any PDC path. */
477c7428422SThibaut VARENE static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
478c7428422SThibaut VARENE static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds static struct attribute *paths_subsys_attrs[] = {
4811da177e4SLinus Torvalds 	&paths_attr_hwpath.attr,
4821da177e4SLinus Torvalds 	&paths_attr_layer.attr,
4831da177e4SLinus Torvalds 	NULL,
4841da177e4SLinus Torvalds };
48575c09aadSGreg Kroah-Hartman ATTRIBUTE_GROUPS(paths_subsys);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /* Specific kobject type for our PDC paths */
4881da177e4SLinus Torvalds static struct kobj_type ktype_pdcspath = {
4891da177e4SLinus Torvalds 	.sysfs_ops = &pdcspath_attr_ops,
49075c09aadSGreg Kroah-Hartman 	.default_groups = paths_subsys_groups,
4911da177e4SLinus Torvalds };
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds /* We hard define the 4 types of path we expect to find */
4941da177e4SLinus Torvalds static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
4951da177e4SLinus Torvalds static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
4961da177e4SLinus Torvalds static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
4971da177e4SLinus Torvalds static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /* An array containing all PDC paths we will deal with */
5001da177e4SLinus Torvalds static struct pdcspath_entry *pdcspath_entries[] = {
5011da177e4SLinus Torvalds 	&pdcspath_entry_primary,
5021da177e4SLinus Torvalds 	&pdcspath_entry_alternative,
5031da177e4SLinus Torvalds 	&pdcspath_entry_console,
5041da177e4SLinus Torvalds 	&pdcspath_entry_keyboard,
5051da177e4SLinus Torvalds 	NULL,
5061da177e4SLinus Torvalds };
5071da177e4SLinus Torvalds 
508c7428422SThibaut VARENE 
509c7428422SThibaut VARENE /* For more insight of what's going on here, refer to PDC Procedures doc,
510c7428422SThibaut VARENE  * Section PDC_STABLE */
511c7428422SThibaut VARENE 
5121da177e4SLinus Torvalds /**
513c7428422SThibaut VARENE  * pdcs_size_read - Stable Storage size output.
5141da177e4SLinus Torvalds  * @buf: The output buffer to write to.
5151da177e4SLinus Torvalds  */
5167f548217SGreg Kroah-Hartman static ssize_t pdcs_size_read(struct kobject *kobj,
5177f548217SGreg Kroah-Hartman 			      struct kobj_attribute *attr,
5187f548217SGreg Kroah-Hartman 			      char *buf)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	char *out = buf;
5211da177e4SLinus Torvalds 
5227f548217SGreg Kroah-Hartman 	if (!buf)
5231da177e4SLinus Torvalds 		return -EINVAL;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	/* show the size of the stable storage */
526c7428422SThibaut VARENE 	out += sprintf(out, "%ld\n", pdcs_size);
5271da177e4SLinus Torvalds 
528c7428422SThibaut VARENE 	return out - buf;
529c7428422SThibaut VARENE }
5301da177e4SLinus Torvalds 
531c7428422SThibaut VARENE /**
532c7428422SThibaut VARENE  * pdcs_auto_read - Stable Storage autoboot/search flag output.
533c7428422SThibaut VARENE  * @buf: The output buffer to write to.
534c7428422SThibaut VARENE  * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
535c7428422SThibaut VARENE  */
5367f548217SGreg Kroah-Hartman static ssize_t pdcs_auto_read(struct kobject *kobj,
5377f548217SGreg Kroah-Hartman 			      struct kobj_attribute *attr,
5387f548217SGreg Kroah-Hartman 			      char *buf, int knob)
539c7428422SThibaut VARENE {
540c7428422SThibaut VARENE 	char *out = buf;
541c7428422SThibaut VARENE 	struct pdcspath_entry *pathentry;
542c7428422SThibaut VARENE 
5437f548217SGreg Kroah-Hartman 	if (!buf)
544c7428422SThibaut VARENE 		return -EINVAL;
545c7428422SThibaut VARENE 
546c7428422SThibaut VARENE 	/* Current flags are stored in primary boot path entry */
547c7428422SThibaut VARENE 	pathentry = &pdcspath_entry_primary;
548c7428422SThibaut VARENE 
549c7428422SThibaut VARENE 	read_lock(&pathentry->rw_lock);
550c7428422SThibaut VARENE 	out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
551c7428422SThibaut VARENE 					"On" : "Off");
552c7428422SThibaut VARENE 	read_unlock(&pathentry->rw_lock);
553c7428422SThibaut VARENE 
554c7428422SThibaut VARENE 	return out - buf;
555c7428422SThibaut VARENE }
556c7428422SThibaut VARENE 
557c7428422SThibaut VARENE /**
558c7428422SThibaut VARENE  * pdcs_autoboot_read - Stable Storage autoboot flag output.
559c7428422SThibaut VARENE  * @buf: The output buffer to write to.
560c7428422SThibaut VARENE  */
5617f548217SGreg Kroah-Hartman static ssize_t pdcs_autoboot_read(struct kobject *kobj,
5627f548217SGreg Kroah-Hartman 				  struct kobj_attribute *attr, char *buf)
563c7428422SThibaut VARENE {
5647f548217SGreg Kroah-Hartman 	return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT);
565c7428422SThibaut VARENE }
566c7428422SThibaut VARENE 
567c7428422SThibaut VARENE /**
568c7428422SThibaut VARENE  * pdcs_autosearch_read - Stable Storage autoboot flag output.
569c7428422SThibaut VARENE  * @buf: The output buffer to write to.
570c7428422SThibaut VARENE  */
5717f548217SGreg Kroah-Hartman static ssize_t pdcs_autosearch_read(struct kobject *kobj,
5727f548217SGreg Kroah-Hartman 				    struct kobj_attribute *attr, char *buf)
573c7428422SThibaut VARENE {
5747f548217SGreg Kroah-Hartman 	return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH);
575c7428422SThibaut VARENE }
576c7428422SThibaut VARENE 
577c7428422SThibaut VARENE /**
578c7428422SThibaut VARENE  * pdcs_timer_read - Stable Storage timer count output (in seconds).
579c7428422SThibaut VARENE  * @buf: The output buffer to write to.
580c7428422SThibaut VARENE  *
581c7428422SThibaut VARENE  * The value of the timer field correponds to a number of seconds in powers of 2.
582c7428422SThibaut VARENE  */
5837f548217SGreg Kroah-Hartman static ssize_t pdcs_timer_read(struct kobject *kobj,
5847f548217SGreg Kroah-Hartman 			       struct kobj_attribute *attr, char *buf)
585c7428422SThibaut VARENE {
586c7428422SThibaut VARENE 	char *out = buf;
587c7428422SThibaut VARENE 	struct pdcspath_entry *pathentry;
588c7428422SThibaut VARENE 
5897f548217SGreg Kroah-Hartman 	if (!buf)
590c7428422SThibaut VARENE 		return -EINVAL;
591c7428422SThibaut VARENE 
592c7428422SThibaut VARENE 	/* Current flags are stored in primary boot path entry */
593c7428422SThibaut VARENE 	pathentry = &pdcspath_entry_primary;
594c7428422SThibaut VARENE 
595c7428422SThibaut VARENE 	/* print the timer value in seconds */
596c7428422SThibaut VARENE 	read_lock(&pathentry->rw_lock);
597c7428422SThibaut VARENE 	out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
598c7428422SThibaut VARENE 				(1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
599c7428422SThibaut VARENE 	read_unlock(&pathentry->rw_lock);
600c7428422SThibaut VARENE 
601c7428422SThibaut VARENE 	return out - buf;
602c7428422SThibaut VARENE }
603c7428422SThibaut VARENE 
604c7428422SThibaut VARENE /**
605c7428422SThibaut VARENE  * pdcs_osid_read - Stable Storage OS ID register output.
606c7428422SThibaut VARENE  * @buf: The output buffer to write to.
607c7428422SThibaut VARENE  */
6087f548217SGreg Kroah-Hartman static ssize_t pdcs_osid_read(struct kobject *kobj,
6097f548217SGreg Kroah-Hartman 			      struct kobj_attribute *attr, char *buf)
610c7428422SThibaut VARENE {
611c7428422SThibaut VARENE 	char *out = buf;
612c7428422SThibaut VARENE 
6137f548217SGreg Kroah-Hartman 	if (!buf)
614c7428422SThibaut VARENE 		return -EINVAL;
6151da177e4SLinus Torvalds 
61667a061a1SKyle McMartin 	out += sprintf(out, "%s dependent data (0x%.4x)\n",
61767a061a1SKyle McMartin 		os_id_to_string(pdcs_osid), pdcs_osid);
6183f9edb53SThibaut Varene 
6193f9edb53SThibaut Varene 	return out - buf;
6203f9edb53SThibaut Varene }
6213f9edb53SThibaut Varene 
6223f9edb53SThibaut Varene /**
6233f9edb53SThibaut Varene  * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
6243f9edb53SThibaut Varene  * @buf: The output buffer to write to.
6253f9edb53SThibaut Varene  *
6263f9edb53SThibaut Varene  * This can hold 16 bytes of OS-Dependent data.
6273f9edb53SThibaut Varene  */
6287f548217SGreg Kroah-Hartman static ssize_t pdcs_osdep1_read(struct kobject *kobj,
6297f548217SGreg Kroah-Hartman 				struct kobj_attribute *attr, char *buf)
6303f9edb53SThibaut Varene {
6313f9edb53SThibaut Varene 	char *out = buf;
6323f9edb53SThibaut Varene 	u32 result[4];
6333f9edb53SThibaut Varene 
6347f548217SGreg Kroah-Hartman 	if (!buf)
6353f9edb53SThibaut Varene 		return -EINVAL;
6363f9edb53SThibaut Varene 
6373f9edb53SThibaut Varene 	if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
6383f9edb53SThibaut Varene 		return -EIO;
6393f9edb53SThibaut Varene 
6403f9edb53SThibaut Varene 	out += sprintf(out, "0x%.8x\n", result[0]);
6413f9edb53SThibaut Varene 	out += sprintf(out, "0x%.8x\n", result[1]);
6423f9edb53SThibaut Varene 	out += sprintf(out, "0x%.8x\n", result[2]);
6433f9edb53SThibaut Varene 	out += sprintf(out, "0x%.8x\n", result[3]);
6443f9edb53SThibaut Varene 
6453f9edb53SThibaut Varene 	return out - buf;
6463f9edb53SThibaut Varene }
6473f9edb53SThibaut Varene 
6483f9edb53SThibaut Varene /**
6493f9edb53SThibaut Varene  * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
6503f9edb53SThibaut Varene  * @buf: The output buffer to write to.
6513f9edb53SThibaut Varene  *
6523f9edb53SThibaut Varene  * I have NFC how to interpret the content of that register ;-).
6533f9edb53SThibaut Varene  */
6547f548217SGreg Kroah-Hartman static ssize_t pdcs_diagnostic_read(struct kobject *kobj,
6557f548217SGreg Kroah-Hartman 				    struct kobj_attribute *attr, char *buf)
6563f9edb53SThibaut Varene {
6573f9edb53SThibaut Varene 	char *out = buf;
6583f9edb53SThibaut Varene 	u32 result;
6593f9edb53SThibaut Varene 
6607f548217SGreg Kroah-Hartman 	if (!buf)
6613f9edb53SThibaut Varene 		return -EINVAL;
6623f9edb53SThibaut Varene 
6633f9edb53SThibaut Varene 	/* get diagnostic */
6643f9edb53SThibaut Varene 	if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
6653f9edb53SThibaut Varene 		return -EIO;
6663f9edb53SThibaut Varene 
6673f9edb53SThibaut Varene 	out += sprintf(out, "0x%.4x\n", (result >> 16));
668c7428422SThibaut VARENE 
669c7428422SThibaut VARENE 	return out - buf;
670c7428422SThibaut VARENE }
671c7428422SThibaut VARENE 
672c7428422SThibaut VARENE /**
673c7428422SThibaut VARENE  * pdcs_fastsize_read - Stable Storage FastSize register output.
674c7428422SThibaut VARENE  * @buf: The output buffer to write to.
675c7428422SThibaut VARENE  *
676c7428422SThibaut VARENE  * This register holds the amount of system RAM to be tested during boot sequence.
677c7428422SThibaut VARENE  */
6787f548217SGreg Kroah-Hartman static ssize_t pdcs_fastsize_read(struct kobject *kobj,
6797f548217SGreg Kroah-Hartman 				  struct kobj_attribute *attr, char *buf)
680c7428422SThibaut VARENE {
681c7428422SThibaut VARENE 	char *out = buf;
6823f9edb53SThibaut Varene 	u32 result;
683c7428422SThibaut VARENE 
6847f548217SGreg Kroah-Hartman 	if (!buf)
685c7428422SThibaut VARENE 		return -EINVAL;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	/* get fast-size */
6881da177e4SLinus Torvalds 	if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
6891da177e4SLinus Torvalds 		return -EIO;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	if ((result & 0x0F) < 0x0E)
692abff7543SRandolph Chung 		out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
6931da177e4SLinus Torvalds 	else
6941da177e4SLinus Torvalds 		out += sprintf(out, "All");
6951da177e4SLinus Torvalds 	out += sprintf(out, "\n");
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	return out - buf;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds /**
7013f9edb53SThibaut Varene  * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
7023f9edb53SThibaut Varene  * @buf: The output buffer to write to.
7033f9edb53SThibaut Varene  *
7043f9edb53SThibaut Varene  * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
7053f9edb53SThibaut Varene  */
7067f548217SGreg Kroah-Hartman static ssize_t pdcs_osdep2_read(struct kobject *kobj,
7077f548217SGreg Kroah-Hartman 				struct kobj_attribute *attr, char *buf)
7083f9edb53SThibaut Varene {
7093f9edb53SThibaut Varene 	char *out = buf;
7103f9edb53SThibaut Varene 	unsigned long size;
7113f9edb53SThibaut Varene 	unsigned short i;
7123f9edb53SThibaut Varene 	u32 result;
7133f9edb53SThibaut Varene 
7143f9edb53SThibaut Varene 	if (unlikely(pdcs_size <= 224))
7153f9edb53SThibaut Varene 		return -ENODATA;
7163f9edb53SThibaut Varene 
7173f9edb53SThibaut Varene 	size = pdcs_size - 224;
7183f9edb53SThibaut Varene 
7197f548217SGreg Kroah-Hartman 	if (!buf)
7203f9edb53SThibaut Varene 		return -EINVAL;
7213f9edb53SThibaut Varene 
7223f9edb53SThibaut Varene 	for (i=0; i<size; i+=4) {
7233f9edb53SThibaut Varene 		if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
7243f9edb53SThibaut Varene 					sizeof(result)) != PDC_OK))
7253f9edb53SThibaut Varene 			return -EIO;
7263f9edb53SThibaut Varene 		out += sprintf(out, "0x%.8x\n", result);
7273f9edb53SThibaut Varene 	}
7283f9edb53SThibaut Varene 
7293f9edb53SThibaut Varene 	return out - buf;
7303f9edb53SThibaut Varene }
7313f9edb53SThibaut Varene 
7323f9edb53SThibaut Varene /**
733c7428422SThibaut VARENE  * pdcs_auto_write - This function handles autoboot/search flag modifying.
7341da177e4SLinus Torvalds  * @buf: The input buffer to read from.
7351da177e4SLinus Torvalds  * @count: The number of bytes to be read.
736c7428422SThibaut VARENE  * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
7371da177e4SLinus Torvalds  *
738c7428422SThibaut VARENE  * We will call this function to change the current autoboot flag.
7391da177e4SLinus Torvalds  * We expect a precise syntax:
740c7428422SThibaut VARENE  *	\"n\" (n == 0 or 1) to toggle AutoBoot Off or On
7411da177e4SLinus Torvalds  */
7427f548217SGreg Kroah-Hartman static ssize_t pdcs_auto_write(struct kobject *kobj,
7437f548217SGreg Kroah-Hartman 			       struct kobj_attribute *attr, const char *buf,
7447f548217SGreg Kroah-Hartman 			       size_t count, int knob)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	struct pdcspath_entry *pathentry;
7471da177e4SLinus Torvalds 	unsigned char flags;
74894c457deSRickard Strandqvist 	char in[8], *temp;
7491da177e4SLinus Torvalds 	char c;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
7521da177e4SLinus Torvalds 		return -EACCES;
7531da177e4SLinus Torvalds 
7547f548217SGreg Kroah-Hartman 	if (!buf || !count)
7551da177e4SLinus Torvalds 		return -EINVAL;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	/* We'll use a local copy of buf */
758c735483dSHelge Deller 	count = min_t(size_t, count, sizeof(in)-1);
7591da177e4SLinus Torvalds 	strncpy(in, buf, count);
76094c457deSRickard Strandqvist 	in[count] = '\0';
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	/* Current flags are stored in primary boot path entry */
7631da177e4SLinus Torvalds 	pathentry = &pdcspath_entry_primary;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	/* Be nice to the existing flag record */
766c7428422SThibaut VARENE 	read_lock(&pathentry->rw_lock);
7671da177e4SLinus Torvalds 	flags = pathentry->devpath.flags;
768c7428422SThibaut VARENE 	read_unlock(&pathentry->rw_lock);
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
7711da177e4SLinus Torvalds 
772e7d2860bSAndré Goddard Rosa 	temp = skip_spaces(in);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	c = *temp++ - '0';
7751da177e4SLinus Torvalds 	if ((c != 0) && (c != 1))
7761da177e4SLinus Torvalds 		goto parse_error;
7771da177e4SLinus Torvalds 	if (c == 0)
778c7428422SThibaut VARENE 		flags &= ~knob;
7791da177e4SLinus Torvalds 	else
780c7428422SThibaut VARENE 		flags |= knob;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	/* So far so good, let's get in deep */
785c7428422SThibaut VARENE 	write_lock(&pathentry->rw_lock);
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	/* Change the path entry flags first */
7881da177e4SLinus Torvalds 	pathentry->devpath.flags = flags;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	/* Now, dive in. Write back to the hardware */
791c7428422SThibaut VARENE 	pdcspath_store(pathentry);
792c7428422SThibaut VARENE 	write_unlock(&pathentry->rw_lock);
7931da177e4SLinus Torvalds 
794c7428422SThibaut VARENE 	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
795c7428422SThibaut VARENE 		(knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
796c7428422SThibaut VARENE 		(flags & knob) ? "On" : "Off");
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	return count;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds parse_error:
801c7428422SThibaut VARENE 	printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
8021da177e4SLinus Torvalds 	return -EINVAL;
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
805c7428422SThibaut VARENE /**
806c7428422SThibaut VARENE  * pdcs_autoboot_write - This function handles autoboot flag modifying.
807c7428422SThibaut VARENE  * @buf: The input buffer to read from.
808c7428422SThibaut VARENE  * @count: The number of bytes to be read.
809c7428422SThibaut VARENE  *
810c7428422SThibaut VARENE  * We will call this function to change the current boot flags.
811c7428422SThibaut VARENE  * We expect a precise syntax:
812c7428422SThibaut VARENE  *	\"n\" (n == 0 or 1) to toggle AutoSearch Off or On
813c7428422SThibaut VARENE  */
8147f548217SGreg Kroah-Hartman static ssize_t pdcs_autoboot_write(struct kobject *kobj,
8157f548217SGreg Kroah-Hartman 				   struct kobj_attribute *attr,
8167f548217SGreg Kroah-Hartman 				   const char *buf, size_t count)
817c7428422SThibaut VARENE {
818ff451d70SJoel Soete 	return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT);
819c7428422SThibaut VARENE }
820c7428422SThibaut VARENE 
821c7428422SThibaut VARENE /**
822c7428422SThibaut VARENE  * pdcs_autosearch_write - This function handles autosearch flag modifying.
823c7428422SThibaut VARENE  * @buf: The input buffer to read from.
824c7428422SThibaut VARENE  * @count: The number of bytes to be read.
825c7428422SThibaut VARENE  *
826c7428422SThibaut VARENE  * We will call this function to change the current boot flags.
827c7428422SThibaut VARENE  * We expect a precise syntax:
828c7428422SThibaut VARENE  *	\"n\" (n == 0 or 1) to toggle AutoSearch Off or On
829c7428422SThibaut VARENE  */
8307f548217SGreg Kroah-Hartman static ssize_t pdcs_autosearch_write(struct kobject *kobj,
8317f548217SGreg Kroah-Hartman 				     struct kobj_attribute *attr,
8327f548217SGreg Kroah-Hartman 				     const char *buf, size_t count)
833c7428422SThibaut VARENE {
834ff451d70SJoel Soete 	return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH);
835c7428422SThibaut VARENE }
836c7428422SThibaut VARENE 
8373f9edb53SThibaut Varene /**
8383f9edb53SThibaut Varene  * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
8393f9edb53SThibaut Varene  * @buf: The input buffer to read from.
8403f9edb53SThibaut Varene  * @count: The number of bytes to be read.
8413f9edb53SThibaut Varene  *
8423f9edb53SThibaut Varene  * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
8433f9edb53SThibaut Varene  * write approach. It's up to userspace to deal with it when constructing
8443f9edb53SThibaut Varene  * its input buffer.
8453f9edb53SThibaut Varene  */
8467f548217SGreg Kroah-Hartman static ssize_t pdcs_osdep1_write(struct kobject *kobj,
8477f548217SGreg Kroah-Hartman 				 struct kobj_attribute *attr,
8487f548217SGreg Kroah-Hartman 				 const char *buf, size_t count)
8493f9edb53SThibaut Varene {
8503f9edb53SThibaut Varene 	u8 in[16];
8513f9edb53SThibaut Varene 
8523f9edb53SThibaut Varene 	if (!capable(CAP_SYS_ADMIN))
8533f9edb53SThibaut Varene 		return -EACCES;
8543f9edb53SThibaut Varene 
8557f548217SGreg Kroah-Hartman 	if (!buf || !count)
8563f9edb53SThibaut Varene 		return -EINVAL;
8573f9edb53SThibaut Varene 
858ec1fdc24SKyle McMartin 	if (unlikely(pdcs_osid != OS_ID_LINUX))
8593f9edb53SThibaut Varene 		return -EPERM;
8603f9edb53SThibaut Varene 
8613f9edb53SThibaut Varene 	if (count > 16)
8623f9edb53SThibaut Varene 		return -EMSGSIZE;
8633f9edb53SThibaut Varene 
8643f9edb53SThibaut Varene 	/* We'll use a local copy of buf */
8653f9edb53SThibaut Varene 	memset(in, 0, 16);
8663f9edb53SThibaut Varene 	memcpy(in, buf, count);
8673f9edb53SThibaut Varene 
8683f9edb53SThibaut Varene 	if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
8693f9edb53SThibaut Varene 		return -EIO;
8703f9edb53SThibaut Varene 
8713f9edb53SThibaut Varene 	return count;
8723f9edb53SThibaut Varene }
8733f9edb53SThibaut Varene 
8743f9edb53SThibaut Varene /**
8753f9edb53SThibaut Varene  * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
8763f9edb53SThibaut Varene  * @buf: The input buffer to read from.
8773f9edb53SThibaut Varene  * @count: The number of bytes to be read.
8783f9edb53SThibaut Varene  *
8793f9edb53SThibaut Varene  * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
8803f9edb53SThibaut Varene  * byte-by-byte write approach. It's up to userspace to deal with it when
8813f9edb53SThibaut Varene  * constructing its input buffer.
8823f9edb53SThibaut Varene  */
8837f548217SGreg Kroah-Hartman static ssize_t pdcs_osdep2_write(struct kobject *kobj,
8847f548217SGreg Kroah-Hartman 				 struct kobj_attribute *attr,
8857f548217SGreg Kroah-Hartman 				 const char *buf, size_t count)
8863f9edb53SThibaut Varene {
8873f9edb53SThibaut Varene 	unsigned long size;
8883f9edb53SThibaut Varene 	unsigned short i;
8893f9edb53SThibaut Varene 	u8 in[4];
8903f9edb53SThibaut Varene 
8913f9edb53SThibaut Varene 	if (!capable(CAP_SYS_ADMIN))
8923f9edb53SThibaut Varene 		return -EACCES;
8933f9edb53SThibaut Varene 
8947f548217SGreg Kroah-Hartman 	if (!buf || !count)
8953f9edb53SThibaut Varene 		return -EINVAL;
8963f9edb53SThibaut Varene 
8973f9edb53SThibaut Varene 	if (unlikely(pdcs_size <= 224))
8983f9edb53SThibaut Varene 		return -ENOSYS;
8993f9edb53SThibaut Varene 
900ec1fdc24SKyle McMartin 	if (unlikely(pdcs_osid != OS_ID_LINUX))
9013f9edb53SThibaut Varene 		return -EPERM;
9023f9edb53SThibaut Varene 
9033f9edb53SThibaut Varene 	size = pdcs_size - 224;
9043f9edb53SThibaut Varene 
9053f9edb53SThibaut Varene 	if (count > size)
9063f9edb53SThibaut Varene 		return -EMSGSIZE;
9073f9edb53SThibaut Varene 
9083f9edb53SThibaut Varene 	/* We'll use a local copy of buf */
9093f9edb53SThibaut Varene 
9103f9edb53SThibaut Varene 	for (i=0; i<count; i+=4) {
9113f9edb53SThibaut Varene 		memset(in, 0, 4);
9123f9edb53SThibaut Varene 		memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
9133f9edb53SThibaut Varene 		if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
9143f9edb53SThibaut Varene 					sizeof(in)) != PDC_OK))
9153f9edb53SThibaut Varene 			return -EIO;
9163f9edb53SThibaut Varene 	}
9173f9edb53SThibaut Varene 
9183f9edb53SThibaut Varene 	return count;
9193f9edb53SThibaut Varene }
9203f9edb53SThibaut Varene 
921c7428422SThibaut VARENE /* The remaining attributes. */
922c7428422SThibaut VARENE static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
923c7428422SThibaut VARENE static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
924c7428422SThibaut VARENE static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
925c7428422SThibaut VARENE static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
9263f9edb53SThibaut Varene static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
9273f9edb53SThibaut Varene static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
9283f9edb53SThibaut Varene static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
929c7428422SThibaut VARENE static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
9303f9edb53SThibaut Varene static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
9311da177e4SLinus Torvalds 
9327f548217SGreg Kroah-Hartman static struct attribute *pdcs_subsys_attrs[] = {
9337f548217SGreg Kroah-Hartman 	&pdcs_attr_size.attr,
9347f548217SGreg Kroah-Hartman 	&pdcs_attr_autoboot.attr,
9357f548217SGreg Kroah-Hartman 	&pdcs_attr_autosearch.attr,
9367f548217SGreg Kroah-Hartman 	&pdcs_attr_timer.attr,
9377f548217SGreg Kroah-Hartman 	&pdcs_attr_osid.attr,
9387f548217SGreg Kroah-Hartman 	&pdcs_attr_osdep1.attr,
9397f548217SGreg Kroah-Hartman 	&pdcs_attr_diagnostic.attr,
9407f548217SGreg Kroah-Hartman 	&pdcs_attr_fastsize.attr,
9417f548217SGreg Kroah-Hartman 	&pdcs_attr_osdep2.attr,
942c7428422SThibaut VARENE 	NULL,
9431da177e4SLinus Torvalds };
9441da177e4SLinus Torvalds 
945343fdfb7SArvind Yadav static const struct attribute_group pdcs_attr_group = {
9467f548217SGreg Kroah-Hartman 	.attrs = pdcs_subsys_attrs,
9477f548217SGreg Kroah-Hartman };
9487f548217SGreg Kroah-Hartman 
949c829a5b4SGreg Kroah-Hartman static struct kobject *stable_kobj;
9504443d07fSGreg Kroah-Hartman static struct kset *paths_kset;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds /**
9531da177e4SLinus Torvalds  * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
9541da177e4SLinus Torvalds  *
9551da177e4SLinus Torvalds  * It creates kobjects corresponding to each path entry with nice sysfs
9561da177e4SLinus Torvalds  * links to the real device. This is where the magic takes place: when
9571da177e4SLinus Torvalds  * registering the subsystem attributes during module init, each kobject hereby
9581da177e4SLinus Torvalds  * created will show in the sysfs tree as a folder containing files as defined
9591da177e4SLinus Torvalds  * by path_subsys_attr[].
9601da177e4SLinus Torvalds  */
9611da177e4SLinus Torvalds static inline int __init
9621da177e4SLinus Torvalds pdcs_register_pathentries(void)
9631da177e4SLinus Torvalds {
9641da177e4SLinus Torvalds 	unsigned short i;
9651da177e4SLinus Torvalds 	struct pdcspath_entry *entry;
9664b991da7SThibaut VARENE 	int err;
9671da177e4SLinus Torvalds 
968c7428422SThibaut VARENE 	/* Initialize the entries rw_lock before anything else */
969c7428422SThibaut VARENE 	for (i = 0; (entry = pdcspath_entries[i]); i++)
970c7428422SThibaut VARENE 		rwlock_init(&entry->rw_lock);
971c7428422SThibaut VARENE 
9721da177e4SLinus Torvalds 	for (i = 0; (entry = pdcspath_entries[i]); i++) {
973c7428422SThibaut VARENE 		write_lock(&entry->rw_lock);
974c7428422SThibaut VARENE 		err = pdcspath_fetch(entry);
975c7428422SThibaut VARENE 		write_unlock(&entry->rw_lock);
976c7428422SThibaut VARENE 
977c7428422SThibaut VARENE 		if (err < 0)
9781da177e4SLinus Torvalds 			continue;
9791da177e4SLinus Torvalds 
9804443d07fSGreg Kroah-Hartman 		entry->kobj.kset = paths_kset;
98173f368cfSGreg Kroah-Hartman 		err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL,
98273f368cfSGreg Kroah-Hartman 					   "%s", entry->name);
983*d24846a4SMiaoqian Lin 		if (err) {
984*d24846a4SMiaoqian Lin 			kobject_put(&entry->kobj);
9854b991da7SThibaut VARENE 			return err;
986*d24846a4SMiaoqian Lin 		}
9874b991da7SThibaut VARENE 
9884b991da7SThibaut VARENE 		/* kobject is now registered */
989c7428422SThibaut VARENE 		write_lock(&entry->rw_lock);
9904b991da7SThibaut VARENE 		entry->ready = 2;
99193964fd4SJames Bottomley 		write_unlock(&entry->rw_lock);
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 		/* Add a nice symlink to the real device */
99426f03249SKyle McMartin 		if (entry->dev) {
99526f03249SKyle McMartin 			err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
99626f03249SKyle McMartin 			WARN_ON(err);
99726f03249SKyle McMartin 		}
998c7428422SThibaut VARENE 
99973f368cfSGreg Kroah-Hartman 		kobject_uevent(&entry->kobj, KOBJ_ADD);
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	return 0;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds /**
10061da177e4SLinus Torvalds  * pdcs_unregister_pathentries - Routine called when unregistering the module.
10071da177e4SLinus Torvalds  */
10084b991da7SThibaut VARENE static inline void
10091da177e4SLinus Torvalds pdcs_unregister_pathentries(void)
10101da177e4SLinus Torvalds {
10111da177e4SLinus Torvalds 	unsigned short i;
10121da177e4SLinus Torvalds 	struct pdcspath_entry *entry;
10131da177e4SLinus Torvalds 
1014c7428422SThibaut VARENE 	for (i = 0; (entry = pdcspath_entries[i]); i++) {
1015c7428422SThibaut VARENE 		read_lock(&entry->rw_lock);
10164b991da7SThibaut VARENE 		if (entry->ready >= 2)
1017c10997f6SGreg Kroah-Hartman 			kobject_put(&entry->kobj);
1018c7428422SThibaut VARENE 		read_unlock(&entry->rw_lock);
1019c7428422SThibaut VARENE 	}
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds /*
1023c7428422SThibaut VARENE  * For now we register the stable subsystem with the firmware subsystem
1024c7428422SThibaut VARENE  * and the paths subsystem with the stable subsystem
10251da177e4SLinus Torvalds  */
10261da177e4SLinus Torvalds static int __init
10271da177e4SLinus Torvalds pdc_stable_init(void)
10281da177e4SLinus Torvalds {
10297f548217SGreg Kroah-Hartman 	int rc = 0, error = 0;
10303f9edb53SThibaut Varene 	u32 result;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	/* find the size of the stable storage */
10331da177e4SLinus Torvalds 	if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
10341da177e4SLinus Torvalds 		return -ENODEV;
10351da177e4SLinus Torvalds 
1036c7428422SThibaut VARENE 	/* make sure we have enough data */
1037c7428422SThibaut VARENE 	if (pdcs_size < 96)
1038c7428422SThibaut VARENE 		return -ENODATA;
10391da177e4SLinus Torvalds 
1040c7428422SThibaut VARENE 	printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
1041c7428422SThibaut VARENE 
10423f9edb53SThibaut Varene 	/* get OSID */
10433f9edb53SThibaut Varene 	if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
10443f9edb53SThibaut Varene 		return -EIO;
10453f9edb53SThibaut Varene 
10463f9edb53SThibaut Varene 	/* the actual result is 16 bits away */
10473f9edb53SThibaut Varene 	pdcs_osid = (u16)(result >> 16);
10483f9edb53SThibaut Varene 
1049c829a5b4SGreg Kroah-Hartman 	/* For now we'll register the directory at /sys/firmware/stable */
1050c829a5b4SGreg Kroah-Hartman 	stable_kobj = kobject_create_and_add("stable", firmware_kobj);
1051c829a5b4SGreg Kroah-Hartman 	if (!stable_kobj) {
10524443d07fSGreg Kroah-Hartman 		rc = -ENOMEM;
10534b991da7SThibaut VARENE 		goto fail_firmreg;
10544443d07fSGreg Kroah-Hartman 	}
10551da177e4SLinus Torvalds 
1056c7428422SThibaut VARENE 	/* Don't forget the root entries */
1057ff451d70SJoel Soete 	error = sysfs_create_group(stable_kobj, &pdcs_attr_group);
10581da177e4SLinus Torvalds 
10594443d07fSGreg Kroah-Hartman 	/* register the paths kset as a child of the stable kset */
1060c829a5b4SGreg Kroah-Hartman 	paths_kset = kset_create_and_add("paths", NULL, stable_kobj);
10614443d07fSGreg Kroah-Hartman 	if (!paths_kset) {
10624443d07fSGreg Kroah-Hartman 		rc = -ENOMEM;
10634443d07fSGreg Kroah-Hartman 		goto fail_ksetreg;
10644443d07fSGreg Kroah-Hartman 	}
10651da177e4SLinus Torvalds 
10664443d07fSGreg Kroah-Hartman 	/* now we create all "files" for the paths kset */
10674b991da7SThibaut VARENE 	if ((rc = pdcs_register_pathentries()))
10684b991da7SThibaut VARENE 		goto fail_pdcsreg;
10691da177e4SLinus Torvalds 
10704b991da7SThibaut VARENE 	return rc;
10714b991da7SThibaut VARENE 
10724b991da7SThibaut VARENE fail_pdcsreg:
10734b991da7SThibaut VARENE 	pdcs_unregister_pathentries();
10744443d07fSGreg Kroah-Hartman 	kset_unregister(paths_kset);
10754b991da7SThibaut VARENE 
10764443d07fSGreg Kroah-Hartman fail_ksetreg:
1077c10997f6SGreg Kroah-Hartman 	kobject_put(stable_kobj);
10784b991da7SThibaut VARENE 
10794b991da7SThibaut VARENE fail_firmreg:
1080c7428422SThibaut VARENE 	printk(KERN_INFO PDCS_PREFIX " bailing out\n");
10814b991da7SThibaut VARENE 	return rc;
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds static void __exit
10851da177e4SLinus Torvalds pdc_stable_exit(void)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	pdcs_unregister_pathentries();
10884443d07fSGreg Kroah-Hartman 	kset_unregister(paths_kset);
1089c10997f6SGreg Kroah-Hartman 	kobject_put(stable_kobj);
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds module_init(pdc_stable_init);
10941da177e4SLinus Torvalds module_exit(pdc_stable_exit);
1095