xref: /linux/drivers/parisc/pdc_stable.c (revision 7c0f6ba682b9c7632072ffbedf8d328c8f3c42ba)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *    Interfaces to retrieve and set PDC Stable options (firmware)
31da177e4SLinus Torvalds  *
4c7428422SThibaut VARENE  *    Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *    This program is free software; you can redistribute it and/or modify
7a81dd18eSThibaut VARENE  *    it under the terms of the GNU General Public License, version 2, as
8a81dd18eSThibaut VARENE  *    published by the Free Software Foundation.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *    This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131da177e4SLinus Torvalds  *    GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *    You should have received a copy of the GNU General Public License
161da177e4SLinus Torvalds  *    along with this program; if not, write to the Free Software
171da177e4SLinus Torvalds  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *    DEV NOTE: the PDC Procedures reference states that:
211da177e4SLinus Torvalds  *    "A minimum of 96 bytes of Stable Storage is required. Providing more than
221da177e4SLinus Torvalds  *    96 bytes of Stable Storage is optional [...]. Failure to provide the
231da177e4SLinus Torvalds  *    optional locations from 96 to 192 results in the loss of certain
241da177e4SLinus Torvalds  *    functionality during boot."
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  *    Since locations between 96 and 192 are the various paths, most (if not
271da177e4SLinus Torvalds  *    all) PA-RISC machines should have them. Anyway, for safety reasons, the
28c7428422SThibaut VARENE  *    following code can deal with just 96 bytes of Stable Storage, and all
291da177e4SLinus Torvalds  *    sizes between 96 and 192 bytes (provided they are multiple of struct
301da177e4SLinus Torvalds  *    device_path size, eg: 128, 160 and 192) to provide full information.
313f9edb53SThibaut Varene  *    One last word: there's one path we can always count on: the primary path.
323f9edb53SThibaut Varene  *    Anything above 224 bytes is used for 'osdep2' OS-dependent storage area.
333f9edb53SThibaut Varene  *
343f9edb53SThibaut Varene  *    The first OS-dependent area should always be available. Obviously, this is
353f9edb53SThibaut Varene  *    not true for the other one. Also bear in mind that reading/writing from/to
363f9edb53SThibaut Varene  *    osdep2 is much more expensive than from/to osdep1.
373f9edb53SThibaut Varene  *    NOTE: We do not handle the 2 bytes OS-dep area at 0x5D, nor the first
383f9edb53SThibaut Varene  *    2 bytes of storage available right after OSID. That's a total of 4 bytes
393f9edb53SThibaut Varene  *    sacrificed: -ETOOLAZY :P
40c7428422SThibaut VARENE  *
41c7428422SThibaut VARENE  *    The current policy wrt file permissions is:
42c7428422SThibaut VARENE  *	- write: root only
43c7428422SThibaut VARENE  *	- read: (reading triggers PDC calls) ? root only : everyone
44c7428422SThibaut VARENE  *    The rationale is that PDC calls could hog (DoS) the machine.
45c7428422SThibaut VARENE  *
46c7428422SThibaut VARENE  *	TODO:
47c7428422SThibaut VARENE  *	- timer/fastsize write calls
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #undef PDCS_DEBUG
511da177e4SLinus Torvalds #ifdef PDCS_DEBUG
521da177e4SLinus Torvalds #define DPRINTK(fmt, args...)	printk(KERN_DEBUG fmt, ## args)
531da177e4SLinus Torvalds #else
541da177e4SLinus Torvalds #define DPRINTK(fmt, args...)
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds #include <linux/module.h>
581da177e4SLinus Torvalds #include <linux/init.h>
591da177e4SLinus Torvalds #include <linux/kernel.h>
601da177e4SLinus Torvalds #include <linux/string.h>
61c59ede7bSRandy.Dunlap #include <linux/capability.h>
621da177e4SLinus Torvalds #include <linux/ctype.h>
631da177e4SLinus Torvalds #include <linux/sysfs.h>
641da177e4SLinus Torvalds #include <linux/kobject.h>
651da177e4SLinus Torvalds #include <linux/device.h>
661da177e4SLinus Torvalds #include <linux/errno.h>
67c7428422SThibaut VARENE #include <linux/spinlock.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <asm/pdc.h>
701da177e4SLinus Torvalds #include <asm/page.h>
71*7c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
721da177e4SLinus Torvalds #include <asm/hardware.h>
731da177e4SLinus Torvalds 
743f9edb53SThibaut Varene #define PDCS_VERSION	"0.30"
75c7428422SThibaut VARENE #define PDCS_PREFIX	"PDC Stable Storage"
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds #define PDCS_ADDR_PPRI	0x00
781da177e4SLinus Torvalds #define PDCS_ADDR_OSID	0x40
793f9edb53SThibaut Varene #define PDCS_ADDR_OSD1	0x48
803f9edb53SThibaut Varene #define PDCS_ADDR_DIAG	0x58
811da177e4SLinus Torvalds #define PDCS_ADDR_FSIZ	0x5C
821da177e4SLinus Torvalds #define PDCS_ADDR_PCON	0x60
831da177e4SLinus Torvalds #define PDCS_ADDR_PALT	0x80
841da177e4SLinus Torvalds #define PDCS_ADDR_PKBD	0xA0
853f9edb53SThibaut Varene #define PDCS_ADDR_OSD2	0xE0
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
881da177e4SLinus Torvalds MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
891da177e4SLinus Torvalds MODULE_LICENSE("GPL");
901da177e4SLinus Torvalds MODULE_VERSION(PDCS_VERSION);
911da177e4SLinus Torvalds 
92c7428422SThibaut VARENE /* holds Stable Storage size. Initialized once and for all, no lock needed */
938039de10SHelge Deller static unsigned long pdcs_size __read_mostly;
941da177e4SLinus Torvalds 
953f9edb53SThibaut Varene /* holds OS ID. Initialized once and for all, hopefully to 0x0006 */
963f9edb53SThibaut Varene static u16 pdcs_osid __read_mostly;
973f9edb53SThibaut Varene 
981da177e4SLinus Torvalds /* This struct defines what we need to deal with a parisc pdc path entry */
991da177e4SLinus Torvalds struct pdcspath_entry {
100c7428422SThibaut VARENE 	rwlock_t rw_lock;		/* to protect path entry access */
1011da177e4SLinus Torvalds 	short ready;			/* entry record is valid if != 0 */
1021da177e4SLinus Torvalds 	unsigned long addr;		/* entry address in stable storage */
1031da177e4SLinus Torvalds 	char *name;			/* entry name */
1041da177e4SLinus Torvalds 	struct device_path devpath;	/* device path in parisc representation */
1051da177e4SLinus Torvalds 	struct device *dev;		/* corresponding device */
1061da177e4SLinus Torvalds 	struct kobject kobj;
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds struct pdcspath_attribute {
1101da177e4SLinus Torvalds 	struct attribute attr;
1111da177e4SLinus Torvalds 	ssize_t (*show)(struct pdcspath_entry *entry, char *buf);
1121da177e4SLinus Torvalds 	ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count);
1131da177e4SLinus Torvalds };
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #define PDCSPATH_ENTRY(_addr, _name) \
1161da177e4SLinus Torvalds struct pdcspath_entry pdcspath_entry_##_name = { \
1171da177e4SLinus Torvalds 	.ready = 0, \
1181da177e4SLinus Torvalds 	.addr = _addr, \
1191da177e4SLinus Torvalds 	.name = __stringify(_name), \
1201da177e4SLinus Torvalds };
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds #define PDCS_ATTR(_name, _mode, _show, _store) \
1237f548217SGreg Kroah-Hartman struct kobj_attribute pdcs_attr_##_name = { \
1247b595756STejun Heo 	.attr = {.name = __stringify(_name), .mode = _mode}, \
1251da177e4SLinus Torvalds 	.show = _show, \
1261da177e4SLinus Torvalds 	.store = _store, \
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds #define PATHS_ATTR(_name, _mode, _show, _store) \
1301da177e4SLinus Torvalds struct pdcspath_attribute paths_attr_##_name = { \
1317b595756STejun Heo 	.attr = {.name = __stringify(_name), .mode = _mode}, \
1321da177e4SLinus Torvalds 	.show = _show, \
1331da177e4SLinus Torvalds 	.store = _store, \
1341da177e4SLinus Torvalds };
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds #define to_pdcspath_attribute(_attr) container_of(_attr, struct pdcspath_attribute, attr)
1371da177e4SLinus Torvalds #define to_pdcspath_entry(obj)  container_of(obj, struct pdcspath_entry, kobj)
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /**
1401da177e4SLinus Torvalds  * pdcspath_fetch - This function populates the path entry structs.
1411da177e4SLinus Torvalds  * @entry: A pointer to an allocated pdcspath_entry.
1421da177e4SLinus Torvalds  *
1431da177e4SLinus Torvalds  * The general idea is that you don't read from the Stable Storage every time
14425985edcSLucas De Marchi  * you access the files provided by the facilities. We store a copy of the
1451da177e4SLinus Torvalds  * content of the stable storage WRT various paths in these structs. We read
1461da177e4SLinus Torvalds  * these structs when reading the files, and we will write to these structs when
1471da177e4SLinus Torvalds  * writing to the files, and only then write them back to the Stable Storage.
148c7428422SThibaut VARENE  *
149c7428422SThibaut VARENE  * This function expects to be called with @entry->rw_lock write-hold.
1501da177e4SLinus Torvalds  */
1511da177e4SLinus Torvalds static int
1521da177e4SLinus Torvalds pdcspath_fetch(struct pdcspath_entry *entry)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	struct device_path *devpath;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	if (!entry)
1571da177e4SLinus Torvalds 		return -EINVAL;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	devpath = &entry->devpath;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
1621da177e4SLinus Torvalds 			entry, devpath, entry->addr);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/* addr, devpath and count must be word aligned */
1651da177e4SLinus Torvalds 	if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
1661da177e4SLinus Torvalds 		return -EIO;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	/* Find the matching device.
1691da177e4SLinus Torvalds 	   NOTE: hardware_path overlays with device_path, so the nice cast can
1701da177e4SLinus Torvalds 	   be used */
1711da177e4SLinus Torvalds 	entry->dev = hwpath_to_device((struct hardware_path *)devpath);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	entry->ready = 1;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	return 0;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /**
1811da177e4SLinus Torvalds  * pdcspath_store - This function writes a path to stable storage.
1821da177e4SLinus Torvalds  * @entry: A pointer to an allocated pdcspath_entry.
1831da177e4SLinus Torvalds  *
1841da177e4SLinus Torvalds  * It can be used in two ways: either by passing it a preset devpath struct
1851da177e4SLinus Torvalds  * containing an already computed hardware path, or by passing it a device
1861da177e4SLinus Torvalds  * pointer, from which it'll find out the corresponding hardware path.
1871da177e4SLinus Torvalds  * For now we do not handle the case where there's an error in writing to the
1881da177e4SLinus Torvalds  * Stable Storage area, so you'd better not mess up the data :P
189c7428422SThibaut VARENE  *
190c7428422SThibaut VARENE  * This function expects to be called with @entry->rw_lock write-hold.
1911da177e4SLinus Torvalds  */
192c7428422SThibaut VARENE static void
1931da177e4SLinus Torvalds pdcspath_store(struct pdcspath_entry *entry)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	struct device_path *devpath;
1961da177e4SLinus Torvalds 
197c7428422SThibaut VARENE 	BUG_ON(!entry);
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	devpath = &entry->devpath;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* We expect the caller to set the ready flag to 0 if the hardware
2021da177e4SLinus Torvalds 	   path struct provided is invalid, so that we know we have to fill it.
2031da177e4SLinus Torvalds 	   First case, we don't have a preset hwpath... */
2041da177e4SLinus Torvalds 	if (!entry->ready) {
2051da177e4SLinus Torvalds 		/* ...but we have a device, map it */
206c7428422SThibaut VARENE 		BUG_ON(!entry->dev);
2071da177e4SLinus Torvalds 		device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 	/* else, we expect the provided hwpath to be valid. */
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
2121da177e4SLinus Torvalds 			entry, devpath, entry->addr);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	/* addr, devpath and count must be word aligned */
21593c3e913SJulia Lawall 	if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
21693c3e913SJulia Lawall 		WARN(1, KERN_ERR "%s: an error occurred when writing to PDC.\n"
2171da177e4SLinus Torvalds 				"It is likely that the Stable Storage data has been corrupted.\n"
2181da177e4SLinus Torvalds 				"Please check it carefully upon next reboot.\n", __func__);
2191da177e4SLinus Torvalds 
2204b991da7SThibaut VARENE 	/* kobject is already registered */
2214b991da7SThibaut VARENE 	entry->ready = 2;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds /**
2271da177e4SLinus Torvalds  * pdcspath_hwpath_read - This function handles hardware path pretty printing.
2281da177e4SLinus Torvalds  * @entry: An allocated and populated pdscpath_entry struct.
2291da177e4SLinus Torvalds  * @buf: The output buffer to write to.
2301da177e4SLinus Torvalds  *
2311da177e4SLinus Torvalds  * We will call this function to format the output of the hwpath attribute file.
2321da177e4SLinus Torvalds  */
2331da177e4SLinus Torvalds static ssize_t
2341da177e4SLinus Torvalds pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	char *out = buf;
2371da177e4SLinus Torvalds 	struct device_path *devpath;
238c7428422SThibaut VARENE 	short i;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if (!entry || !buf)
2411da177e4SLinus Torvalds 		return -EINVAL;
2421da177e4SLinus Torvalds 
243c7428422SThibaut VARENE 	read_lock(&entry->rw_lock);
2441da177e4SLinus Torvalds 	devpath = &entry->devpath;
245c7428422SThibaut VARENE 	i = entry->ready;
246c7428422SThibaut VARENE 	read_unlock(&entry->rw_lock);
2471da177e4SLinus Torvalds 
248c7428422SThibaut VARENE 	if (!i)	/* entry is not ready */
2491da177e4SLinus Torvalds 		return -ENODATA;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	for (i = 0; i < 6; i++) {
2521da177e4SLinus Torvalds 		if (devpath->bc[i] >= 128)
2531da177e4SLinus Torvalds 			continue;
2541da177e4SLinus Torvalds 		out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
2551da177e4SLinus Torvalds 	}
2561da177e4SLinus Torvalds 	out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	return out - buf;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds /**
2621da177e4SLinus Torvalds  * pdcspath_hwpath_write - This function handles hardware path modifying.
2631da177e4SLinus Torvalds  * @entry: An allocated and populated pdscpath_entry struct.
2641da177e4SLinus Torvalds  * @buf: The input buffer to read from.
2651da177e4SLinus Torvalds  * @count: The number of bytes to be read.
2661da177e4SLinus Torvalds  *
2671da177e4SLinus Torvalds  * We will call this function to change the current hardware path.
2681da177e4SLinus Torvalds  * Hardware paths are to be given '/'-delimited, without brackets.
269c7428422SThibaut VARENE  * We make sure that the provided path actually maps to an existing
2701da177e4SLinus Torvalds  * device, BUT nothing would prevent some foolish user to set the path to some
2711da177e4SLinus Torvalds  * PCI bridge or even a CPU...
2721da177e4SLinus Torvalds  * A better work around would be to make sure we are at the end of a device tree
2731da177e4SLinus Torvalds  * for instance, but it would be IMHO beyond the simple scope of that driver.
2741da177e4SLinus Torvalds  * The aim is to provide a facility. Data correctness is left to userland.
2751da177e4SLinus Torvalds  */
2761da177e4SLinus Torvalds static ssize_t
2771da177e4SLinus Torvalds pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	struct hardware_path hwpath;
2801da177e4SLinus Torvalds 	unsigned short i;
281c735483dSHelge Deller 	char in[64], *temp;
2821da177e4SLinus Torvalds 	struct device *dev;
28326f03249SKyle McMartin 	int ret;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	if (!entry || !buf || !count)
2861da177e4SLinus Torvalds 		return -EINVAL;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	/* We'll use a local copy of buf */
289c735483dSHelge Deller 	count = min_t(size_t, count, sizeof(in)-1);
2901da177e4SLinus Torvalds 	strncpy(in, buf, count);
291c735483dSHelge Deller 	in[count] = '\0';
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	/* Let's clean up the target. 0xff is a blank pattern */
2941da177e4SLinus Torvalds 	memset(&hwpath, 0xff, sizeof(hwpath));
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* First, pick the mod field (the last one of the input string) */
2971da177e4SLinus Torvalds 	if (!(temp = strrchr(in, '/')))
2981da177e4SLinus Torvalds 		return -EINVAL;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	hwpath.mod = simple_strtoul(temp+1, NULL, 10);
3011da177e4SLinus Torvalds 	in[temp-in] = '\0';	/* truncate the remaining string. just precaution */
3021da177e4SLinus Torvalds 	DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	/* Then, loop for each delimiter, making sure we don't have too many.
3051da177e4SLinus Torvalds 	   we write the bc fields in a down-top way. No matter what, we stop
3061da177e4SLinus Torvalds 	   before writing the last field. If there are too many fields anyway,
3071da177e4SLinus Torvalds 	   then the user is a moron and it'll be caught up later when we'll
3081da177e4SLinus Torvalds 	   check the consistency of the given hwpath. */
3091da177e4SLinus Torvalds 	for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
3101da177e4SLinus Torvalds 		hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
3111da177e4SLinus Torvalds 		in[temp-in] = '\0';
3121da177e4SLinus Torvalds 		DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	/* Store the final field */
3161da177e4SLinus Torvalds 	hwpath.bc[i] = simple_strtoul(in, NULL, 10);
3171da177e4SLinus Torvalds 	DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	/* Now we check that the user isn't trying to lure us */
3201da177e4SLinus Torvalds 	if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
3211da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
3221da177e4SLinus Torvalds 			"hardware path: %s\n", __func__, entry->name, buf);
3231da177e4SLinus Torvalds 		return -EINVAL;
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* So far so good, let's get in deep */
327c7428422SThibaut VARENE 	write_lock(&entry->rw_lock);
3281da177e4SLinus Torvalds 	entry->ready = 0;
3291da177e4SLinus Torvalds 	entry->dev = dev;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	/* Now, dive in. Write back to the hardware */
332c7428422SThibaut VARENE 	pdcspath_store(entry);
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	/* Update the symlink to the real device */
3351da177e4SLinus Torvalds 	sysfs_remove_link(&entry->kobj, "device");
33626f03249SKyle McMartin 	ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
33726f03249SKyle McMartin 	WARN_ON(ret);
33826f03249SKyle McMartin 
339c7428422SThibaut VARENE 	write_unlock(&entry->rw_lock);
3401da177e4SLinus Torvalds 
341c7428422SThibaut VARENE 	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
3421da177e4SLinus Torvalds 		entry->name, buf);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	return count;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /**
3481da177e4SLinus Torvalds  * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
3491da177e4SLinus Torvalds  * @entry: An allocated and populated pdscpath_entry struct.
3501da177e4SLinus Torvalds  * @buf: The output buffer to write to.
3511da177e4SLinus Torvalds  *
3521da177e4SLinus Torvalds  * We will call this function to format the output of the layer attribute file.
3531da177e4SLinus Torvalds  */
3541da177e4SLinus Torvalds static ssize_t
3551da177e4SLinus Torvalds pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
3561da177e4SLinus Torvalds {
3571da177e4SLinus Torvalds 	char *out = buf;
3581da177e4SLinus Torvalds 	struct device_path *devpath;
359c7428422SThibaut VARENE 	short i;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	if (!entry || !buf)
3621da177e4SLinus Torvalds 		return -EINVAL;
3631da177e4SLinus Torvalds 
364c7428422SThibaut VARENE 	read_lock(&entry->rw_lock);
3651da177e4SLinus Torvalds 	devpath = &entry->devpath;
366c7428422SThibaut VARENE 	i = entry->ready;
367c7428422SThibaut VARENE 	read_unlock(&entry->rw_lock);
3681da177e4SLinus Torvalds 
369c7428422SThibaut VARENE 	if (!i)	/* entry is not ready */
3701da177e4SLinus Torvalds 		return -ENODATA;
3711da177e4SLinus Torvalds 
372447c233dSRoel Kluin 	for (i = 0; i < 6 && devpath->layers[i]; i++)
3731da177e4SLinus Torvalds 		out += sprintf(out, "%u ", devpath->layers[i]);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	out += sprintf(out, "\n");
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	return out - buf;
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /**
3811da177e4SLinus Torvalds  * pdcspath_layer_write - This function handles extended layer modifying.
3821da177e4SLinus Torvalds  * @entry: An allocated and populated pdscpath_entry struct.
3831da177e4SLinus Torvalds  * @buf: The input buffer to read from.
3841da177e4SLinus Torvalds  * @count: The number of bytes to be read.
3851da177e4SLinus Torvalds  *
3861da177e4SLinus Torvalds  * We will call this function to change the current layer value.
3871da177e4SLinus Torvalds  * Layers are to be given '.'-delimited, without brackets.
3881da177e4SLinus Torvalds  * XXX beware we are far less checky WRT input data provided than for hwpath.
3891da177e4SLinus Torvalds  * Potential harm can be done, since there's no way to check the validity of
3901da177e4SLinus Torvalds  * the layer fields.
3911da177e4SLinus Torvalds  */
3921da177e4SLinus Torvalds static ssize_t
3931da177e4SLinus Torvalds pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
3961da177e4SLinus Torvalds 	unsigned short i;
397c735483dSHelge Deller 	char in[64], *temp;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	if (!entry || !buf || !count)
4001da177e4SLinus Torvalds 		return -EINVAL;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	/* We'll use a local copy of buf */
403c735483dSHelge Deller 	count = min_t(size_t, count, sizeof(in)-1);
4041da177e4SLinus Torvalds 	strncpy(in, buf, count);
405c735483dSHelge Deller 	in[count] = '\0';
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	/* Let's clean up the target. 0 is a blank pattern */
4081da177e4SLinus Torvalds 	memset(&layers, 0, sizeof(layers));
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	/* First, pick the first layer */
4111da177e4SLinus Torvalds 	if (unlikely(!isdigit(*in)))
4121da177e4SLinus Torvalds 		return -EINVAL;
4131da177e4SLinus Torvalds 	layers[0] = simple_strtoul(in, NULL, 10);
4141da177e4SLinus Torvalds 	DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	temp = in;
4171da177e4SLinus Torvalds 	for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
4181da177e4SLinus Torvalds 		if (unlikely(!isdigit(*(++temp))))
4191da177e4SLinus Torvalds 			return -EINVAL;
4201da177e4SLinus Torvalds 		layers[i] = simple_strtoul(temp, NULL, 10);
4211da177e4SLinus Torvalds 		DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	/* So far so good, let's get in deep */
425c7428422SThibaut VARENE 	write_lock(&entry->rw_lock);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	/* First, overwrite the current layers with the new ones, not touching
4281da177e4SLinus Torvalds 	   the hardware path. */
4291da177e4SLinus Torvalds 	memcpy(&entry->devpath.layers, &layers, sizeof(layers));
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	/* Now, dive in. Write back to the hardware */
432c7428422SThibaut VARENE 	pdcspath_store(entry);
433c7428422SThibaut VARENE 	write_unlock(&entry->rw_lock);
4341da177e4SLinus Torvalds 
435c7428422SThibaut VARENE 	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
4361da177e4SLinus Torvalds 		entry->name, buf);
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	return count;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds /**
4421da177e4SLinus Torvalds  * pdcspath_attr_show - Generic read function call wrapper.
4431da177e4SLinus Torvalds  * @kobj: The kobject to get info from.
4441da177e4SLinus Torvalds  * @attr: The attribute looked upon.
4451da177e4SLinus Torvalds  * @buf: The output buffer.
4461da177e4SLinus Torvalds  */
4471da177e4SLinus Torvalds static ssize_t
4481da177e4SLinus Torvalds pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
4511da177e4SLinus Torvalds 	struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
4521da177e4SLinus Torvalds 	ssize_t ret = 0;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	if (pdcs_attr->show)
4551da177e4SLinus Torvalds 		ret = pdcs_attr->show(entry, buf);
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	return ret;
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds /**
4611da177e4SLinus Torvalds  * pdcspath_attr_store - Generic write function call wrapper.
4621da177e4SLinus Torvalds  * @kobj: The kobject to write info to.
4631da177e4SLinus Torvalds  * @attr: The attribute to be modified.
4641da177e4SLinus Torvalds  * @buf: The input buffer.
4651da177e4SLinus Torvalds  * @count: The size of the buffer.
4661da177e4SLinus Torvalds  */
4671da177e4SLinus Torvalds static ssize_t
4681da177e4SLinus Torvalds pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
4691da177e4SLinus Torvalds 			const char *buf, size_t count)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
4721da177e4SLinus Torvalds 	struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
4731da177e4SLinus Torvalds 	ssize_t ret = 0;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
4761da177e4SLinus Torvalds 		return -EACCES;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	if (pdcs_attr->store)
4791da177e4SLinus Torvalds 		ret = pdcs_attr->store(entry, buf, count);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	return ret;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
48452cf25d0SEmese Revfy static const struct sysfs_ops pdcspath_attr_ops = {
4851da177e4SLinus Torvalds 	.show = pdcspath_attr_show,
4861da177e4SLinus Torvalds 	.store = pdcspath_attr_store,
4871da177e4SLinus Torvalds };
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds /* These are the two attributes of any PDC path. */
490c7428422SThibaut VARENE static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
491c7428422SThibaut VARENE static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds static struct attribute *paths_subsys_attrs[] = {
4941da177e4SLinus Torvalds 	&paths_attr_hwpath.attr,
4951da177e4SLinus Torvalds 	&paths_attr_layer.attr,
4961da177e4SLinus Torvalds 	NULL,
4971da177e4SLinus Torvalds };
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /* Specific kobject type for our PDC paths */
5001da177e4SLinus Torvalds static struct kobj_type ktype_pdcspath = {
5011da177e4SLinus Torvalds 	.sysfs_ops = &pdcspath_attr_ops,
5021da177e4SLinus Torvalds 	.default_attrs = paths_subsys_attrs,
5031da177e4SLinus Torvalds };
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds /* We hard define the 4 types of path we expect to find */
5061da177e4SLinus Torvalds static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
5071da177e4SLinus Torvalds static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
5081da177e4SLinus Torvalds static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
5091da177e4SLinus Torvalds static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds /* An array containing all PDC paths we will deal with */
5121da177e4SLinus Torvalds static struct pdcspath_entry *pdcspath_entries[] = {
5131da177e4SLinus Torvalds 	&pdcspath_entry_primary,
5141da177e4SLinus Torvalds 	&pdcspath_entry_alternative,
5151da177e4SLinus Torvalds 	&pdcspath_entry_console,
5161da177e4SLinus Torvalds 	&pdcspath_entry_keyboard,
5171da177e4SLinus Torvalds 	NULL,
5181da177e4SLinus Torvalds };
5191da177e4SLinus Torvalds 
520c7428422SThibaut VARENE 
521c7428422SThibaut VARENE /* For more insight of what's going on here, refer to PDC Procedures doc,
522c7428422SThibaut VARENE  * Section PDC_STABLE */
523c7428422SThibaut VARENE 
5241da177e4SLinus Torvalds /**
525c7428422SThibaut VARENE  * pdcs_size_read - Stable Storage size output.
5261da177e4SLinus Torvalds  * @buf: The output buffer to write to.
5271da177e4SLinus Torvalds  */
5287f548217SGreg Kroah-Hartman static ssize_t pdcs_size_read(struct kobject *kobj,
5297f548217SGreg Kroah-Hartman 			      struct kobj_attribute *attr,
5307f548217SGreg Kroah-Hartman 			      char *buf)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	char *out = buf;
5331da177e4SLinus Torvalds 
5347f548217SGreg Kroah-Hartman 	if (!buf)
5351da177e4SLinus Torvalds 		return -EINVAL;
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	/* show the size of the stable storage */
538c7428422SThibaut VARENE 	out += sprintf(out, "%ld\n", pdcs_size);
5391da177e4SLinus Torvalds 
540c7428422SThibaut VARENE 	return out - buf;
541c7428422SThibaut VARENE }
5421da177e4SLinus Torvalds 
543c7428422SThibaut VARENE /**
544c7428422SThibaut VARENE  * pdcs_auto_read - Stable Storage autoboot/search flag output.
545c7428422SThibaut VARENE  * @buf: The output buffer to write to.
546c7428422SThibaut VARENE  * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
547c7428422SThibaut VARENE  */
5487f548217SGreg Kroah-Hartman static ssize_t pdcs_auto_read(struct kobject *kobj,
5497f548217SGreg Kroah-Hartman 			      struct kobj_attribute *attr,
5507f548217SGreg Kroah-Hartman 			      char *buf, int knob)
551c7428422SThibaut VARENE {
552c7428422SThibaut VARENE 	char *out = buf;
553c7428422SThibaut VARENE 	struct pdcspath_entry *pathentry;
554c7428422SThibaut VARENE 
5557f548217SGreg Kroah-Hartman 	if (!buf)
556c7428422SThibaut VARENE 		return -EINVAL;
557c7428422SThibaut VARENE 
558c7428422SThibaut VARENE 	/* Current flags are stored in primary boot path entry */
559c7428422SThibaut VARENE 	pathentry = &pdcspath_entry_primary;
560c7428422SThibaut VARENE 
561c7428422SThibaut VARENE 	read_lock(&pathentry->rw_lock);
562c7428422SThibaut VARENE 	out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
563c7428422SThibaut VARENE 					"On" : "Off");
564c7428422SThibaut VARENE 	read_unlock(&pathentry->rw_lock);
565c7428422SThibaut VARENE 
566c7428422SThibaut VARENE 	return out - buf;
567c7428422SThibaut VARENE }
568c7428422SThibaut VARENE 
569c7428422SThibaut VARENE /**
570c7428422SThibaut VARENE  * pdcs_autoboot_read - Stable Storage autoboot flag output.
571c7428422SThibaut VARENE  * @buf: The output buffer to write to.
572c7428422SThibaut VARENE  */
5737f548217SGreg Kroah-Hartman static ssize_t pdcs_autoboot_read(struct kobject *kobj,
5747f548217SGreg Kroah-Hartman 				  struct kobj_attribute *attr, char *buf)
575c7428422SThibaut VARENE {
5767f548217SGreg Kroah-Hartman 	return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT);
577c7428422SThibaut VARENE }
578c7428422SThibaut VARENE 
579c7428422SThibaut VARENE /**
580c7428422SThibaut VARENE  * pdcs_autosearch_read - Stable Storage autoboot flag output.
581c7428422SThibaut VARENE  * @buf: The output buffer to write to.
582c7428422SThibaut VARENE  */
5837f548217SGreg Kroah-Hartman static ssize_t pdcs_autosearch_read(struct kobject *kobj,
5847f548217SGreg Kroah-Hartman 				    struct kobj_attribute *attr, char *buf)
585c7428422SThibaut VARENE {
5867f548217SGreg Kroah-Hartman 	return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH);
587c7428422SThibaut VARENE }
588c7428422SThibaut VARENE 
589c7428422SThibaut VARENE /**
590c7428422SThibaut VARENE  * pdcs_timer_read - Stable Storage timer count output (in seconds).
591c7428422SThibaut VARENE  * @buf: The output buffer to write to.
592c7428422SThibaut VARENE  *
593c7428422SThibaut VARENE  * The value of the timer field correponds to a number of seconds in powers of 2.
594c7428422SThibaut VARENE  */
5957f548217SGreg Kroah-Hartman static ssize_t pdcs_timer_read(struct kobject *kobj,
5967f548217SGreg Kroah-Hartman 			       struct kobj_attribute *attr, char *buf)
597c7428422SThibaut VARENE {
598c7428422SThibaut VARENE 	char *out = buf;
599c7428422SThibaut VARENE 	struct pdcspath_entry *pathentry;
600c7428422SThibaut VARENE 
6017f548217SGreg Kroah-Hartman 	if (!buf)
602c7428422SThibaut VARENE 		return -EINVAL;
603c7428422SThibaut VARENE 
604c7428422SThibaut VARENE 	/* Current flags are stored in primary boot path entry */
605c7428422SThibaut VARENE 	pathentry = &pdcspath_entry_primary;
606c7428422SThibaut VARENE 
607c7428422SThibaut VARENE 	/* print the timer value in seconds */
608c7428422SThibaut VARENE 	read_lock(&pathentry->rw_lock);
609c7428422SThibaut VARENE 	out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
610c7428422SThibaut VARENE 				(1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
611c7428422SThibaut VARENE 	read_unlock(&pathentry->rw_lock);
612c7428422SThibaut VARENE 
613c7428422SThibaut VARENE 	return out - buf;
614c7428422SThibaut VARENE }
615c7428422SThibaut VARENE 
616c7428422SThibaut VARENE /**
617c7428422SThibaut VARENE  * pdcs_osid_read - Stable Storage OS ID register output.
618c7428422SThibaut VARENE  * @buf: The output buffer to write to.
619c7428422SThibaut VARENE  */
6207f548217SGreg Kroah-Hartman static ssize_t pdcs_osid_read(struct kobject *kobj,
6217f548217SGreg Kroah-Hartman 			      struct kobj_attribute *attr, char *buf)
622c7428422SThibaut VARENE {
623c7428422SThibaut VARENE 	char *out = buf;
624c7428422SThibaut VARENE 
6257f548217SGreg Kroah-Hartman 	if (!buf)
626c7428422SThibaut VARENE 		return -EINVAL;
6271da177e4SLinus Torvalds 
62867a061a1SKyle McMartin 	out += sprintf(out, "%s dependent data (0x%.4x)\n",
62967a061a1SKyle McMartin 		os_id_to_string(pdcs_osid), pdcs_osid);
6303f9edb53SThibaut Varene 
6313f9edb53SThibaut Varene 	return out - buf;
6323f9edb53SThibaut Varene }
6333f9edb53SThibaut Varene 
6343f9edb53SThibaut Varene /**
6353f9edb53SThibaut Varene  * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
6363f9edb53SThibaut Varene  * @buf: The output buffer to write to.
6373f9edb53SThibaut Varene  *
6383f9edb53SThibaut Varene  * This can hold 16 bytes of OS-Dependent data.
6393f9edb53SThibaut Varene  */
6407f548217SGreg Kroah-Hartman static ssize_t pdcs_osdep1_read(struct kobject *kobj,
6417f548217SGreg Kroah-Hartman 				struct kobj_attribute *attr, char *buf)
6423f9edb53SThibaut Varene {
6433f9edb53SThibaut Varene 	char *out = buf;
6443f9edb53SThibaut Varene 	u32 result[4];
6453f9edb53SThibaut Varene 
6467f548217SGreg Kroah-Hartman 	if (!buf)
6473f9edb53SThibaut Varene 		return -EINVAL;
6483f9edb53SThibaut Varene 
6493f9edb53SThibaut Varene 	if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
6503f9edb53SThibaut Varene 		return -EIO;
6513f9edb53SThibaut Varene 
6523f9edb53SThibaut Varene 	out += sprintf(out, "0x%.8x\n", result[0]);
6533f9edb53SThibaut Varene 	out += sprintf(out, "0x%.8x\n", result[1]);
6543f9edb53SThibaut Varene 	out += sprintf(out, "0x%.8x\n", result[2]);
6553f9edb53SThibaut Varene 	out += sprintf(out, "0x%.8x\n", result[3]);
6563f9edb53SThibaut Varene 
6573f9edb53SThibaut Varene 	return out - buf;
6583f9edb53SThibaut Varene }
6593f9edb53SThibaut Varene 
6603f9edb53SThibaut Varene /**
6613f9edb53SThibaut Varene  * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
6623f9edb53SThibaut Varene  * @buf: The output buffer to write to.
6633f9edb53SThibaut Varene  *
6643f9edb53SThibaut Varene  * I have NFC how to interpret the content of that register ;-).
6653f9edb53SThibaut Varene  */
6667f548217SGreg Kroah-Hartman static ssize_t pdcs_diagnostic_read(struct kobject *kobj,
6677f548217SGreg Kroah-Hartman 				    struct kobj_attribute *attr, char *buf)
6683f9edb53SThibaut Varene {
6693f9edb53SThibaut Varene 	char *out = buf;
6703f9edb53SThibaut Varene 	u32 result;
6713f9edb53SThibaut Varene 
6727f548217SGreg Kroah-Hartman 	if (!buf)
6733f9edb53SThibaut Varene 		return -EINVAL;
6743f9edb53SThibaut Varene 
6753f9edb53SThibaut Varene 	/* get diagnostic */
6763f9edb53SThibaut Varene 	if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
6773f9edb53SThibaut Varene 		return -EIO;
6783f9edb53SThibaut Varene 
6793f9edb53SThibaut Varene 	out += sprintf(out, "0x%.4x\n", (result >> 16));
680c7428422SThibaut VARENE 
681c7428422SThibaut VARENE 	return out - buf;
682c7428422SThibaut VARENE }
683c7428422SThibaut VARENE 
684c7428422SThibaut VARENE /**
685c7428422SThibaut VARENE  * pdcs_fastsize_read - Stable Storage FastSize register output.
686c7428422SThibaut VARENE  * @buf: The output buffer to write to.
687c7428422SThibaut VARENE  *
688c7428422SThibaut VARENE  * This register holds the amount of system RAM to be tested during boot sequence.
689c7428422SThibaut VARENE  */
6907f548217SGreg Kroah-Hartman static ssize_t pdcs_fastsize_read(struct kobject *kobj,
6917f548217SGreg Kroah-Hartman 				  struct kobj_attribute *attr, char *buf)
692c7428422SThibaut VARENE {
693c7428422SThibaut VARENE 	char *out = buf;
6943f9edb53SThibaut Varene 	u32 result;
695c7428422SThibaut VARENE 
6967f548217SGreg Kroah-Hartman 	if (!buf)
697c7428422SThibaut VARENE 		return -EINVAL;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	/* get fast-size */
7001da177e4SLinus Torvalds 	if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
7011da177e4SLinus Torvalds 		return -EIO;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	if ((result & 0x0F) < 0x0E)
704abff7543SRandolph Chung 		out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
7051da177e4SLinus Torvalds 	else
7061da177e4SLinus Torvalds 		out += sprintf(out, "All");
7071da177e4SLinus Torvalds 	out += sprintf(out, "\n");
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	return out - buf;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds /**
7133f9edb53SThibaut Varene  * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
7143f9edb53SThibaut Varene  * @buf: The output buffer to write to.
7153f9edb53SThibaut Varene  *
7163f9edb53SThibaut Varene  * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
7173f9edb53SThibaut Varene  */
7187f548217SGreg Kroah-Hartman static ssize_t pdcs_osdep2_read(struct kobject *kobj,
7197f548217SGreg Kroah-Hartman 				struct kobj_attribute *attr, char *buf)
7203f9edb53SThibaut Varene {
7213f9edb53SThibaut Varene 	char *out = buf;
7223f9edb53SThibaut Varene 	unsigned long size;
7233f9edb53SThibaut Varene 	unsigned short i;
7243f9edb53SThibaut Varene 	u32 result;
7253f9edb53SThibaut Varene 
7263f9edb53SThibaut Varene 	if (unlikely(pdcs_size <= 224))
7273f9edb53SThibaut Varene 		return -ENODATA;
7283f9edb53SThibaut Varene 
7293f9edb53SThibaut Varene 	size = pdcs_size - 224;
7303f9edb53SThibaut Varene 
7317f548217SGreg Kroah-Hartman 	if (!buf)
7323f9edb53SThibaut Varene 		return -EINVAL;
7333f9edb53SThibaut Varene 
7343f9edb53SThibaut Varene 	for (i=0; i<size; i+=4) {
7353f9edb53SThibaut Varene 		if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
7363f9edb53SThibaut Varene 					sizeof(result)) != PDC_OK))
7373f9edb53SThibaut Varene 			return -EIO;
7383f9edb53SThibaut Varene 		out += sprintf(out, "0x%.8x\n", result);
7393f9edb53SThibaut Varene 	}
7403f9edb53SThibaut Varene 
7413f9edb53SThibaut Varene 	return out - buf;
7423f9edb53SThibaut Varene }
7433f9edb53SThibaut Varene 
7443f9edb53SThibaut Varene /**
745c7428422SThibaut VARENE  * pdcs_auto_write - This function handles autoboot/search flag modifying.
7461da177e4SLinus Torvalds  * @buf: The input buffer to read from.
7471da177e4SLinus Torvalds  * @count: The number of bytes to be read.
748c7428422SThibaut VARENE  * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
7491da177e4SLinus Torvalds  *
750c7428422SThibaut VARENE  * We will call this function to change the current autoboot flag.
7511da177e4SLinus Torvalds  * We expect a precise syntax:
752c7428422SThibaut VARENE  *	\"n\" (n == 0 or 1) to toggle AutoBoot Off or On
7531da177e4SLinus Torvalds  */
7547f548217SGreg Kroah-Hartman static ssize_t pdcs_auto_write(struct kobject *kobj,
7557f548217SGreg Kroah-Hartman 			       struct kobj_attribute *attr, const char *buf,
7567f548217SGreg Kroah-Hartman 			       size_t count, int knob)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	struct pdcspath_entry *pathentry;
7591da177e4SLinus Torvalds 	unsigned char flags;
76094c457deSRickard Strandqvist 	char in[8], *temp;
7611da177e4SLinus Torvalds 	char c;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
7641da177e4SLinus Torvalds 		return -EACCES;
7651da177e4SLinus Torvalds 
7667f548217SGreg Kroah-Hartman 	if (!buf || !count)
7671da177e4SLinus Torvalds 		return -EINVAL;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	/* We'll use a local copy of buf */
770c735483dSHelge Deller 	count = min_t(size_t, count, sizeof(in)-1);
7711da177e4SLinus Torvalds 	strncpy(in, buf, count);
77294c457deSRickard Strandqvist 	in[count] = '\0';
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	/* Current flags are stored in primary boot path entry */
7751da177e4SLinus Torvalds 	pathentry = &pdcspath_entry_primary;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	/* Be nice to the existing flag record */
778c7428422SThibaut VARENE 	read_lock(&pathentry->rw_lock);
7791da177e4SLinus Torvalds 	flags = pathentry->devpath.flags;
780c7428422SThibaut VARENE 	read_unlock(&pathentry->rw_lock);
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
7831da177e4SLinus Torvalds 
784e7d2860bSAndré Goddard Rosa 	temp = skip_spaces(in);
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	c = *temp++ - '0';
7871da177e4SLinus Torvalds 	if ((c != 0) && (c != 1))
7881da177e4SLinus Torvalds 		goto parse_error;
7891da177e4SLinus Torvalds 	if (c == 0)
790c7428422SThibaut VARENE 		flags &= ~knob;
7911da177e4SLinus Torvalds 	else
792c7428422SThibaut VARENE 		flags |= knob;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	/* So far so good, let's get in deep */
797c7428422SThibaut VARENE 	write_lock(&pathentry->rw_lock);
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	/* Change the path entry flags first */
8001da177e4SLinus Torvalds 	pathentry->devpath.flags = flags;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	/* Now, dive in. Write back to the hardware */
803c7428422SThibaut VARENE 	pdcspath_store(pathentry);
804c7428422SThibaut VARENE 	write_unlock(&pathentry->rw_lock);
8051da177e4SLinus Torvalds 
806c7428422SThibaut VARENE 	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
807c7428422SThibaut VARENE 		(knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
808c7428422SThibaut VARENE 		(flags & knob) ? "On" : "Off");
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	return count;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds parse_error:
813c7428422SThibaut VARENE 	printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
8141da177e4SLinus Torvalds 	return -EINVAL;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
817c7428422SThibaut VARENE /**
818c7428422SThibaut VARENE  * pdcs_autoboot_write - This function handles autoboot flag modifying.
819c7428422SThibaut VARENE  * @buf: The input buffer to read from.
820c7428422SThibaut VARENE  * @count: The number of bytes to be read.
821c7428422SThibaut VARENE  *
822c7428422SThibaut VARENE  * We will call this function to change the current boot flags.
823c7428422SThibaut VARENE  * We expect a precise syntax:
824c7428422SThibaut VARENE  *	\"n\" (n == 0 or 1) to toggle AutoSearch Off or On
825c7428422SThibaut VARENE  */
8267f548217SGreg Kroah-Hartman static ssize_t pdcs_autoboot_write(struct kobject *kobj,
8277f548217SGreg Kroah-Hartman 				   struct kobj_attribute *attr,
8287f548217SGreg Kroah-Hartman 				   const char *buf, size_t count)
829c7428422SThibaut VARENE {
830ff451d70SJoel Soete 	return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT);
831c7428422SThibaut VARENE }
832c7428422SThibaut VARENE 
833c7428422SThibaut VARENE /**
834c7428422SThibaut VARENE  * pdcs_autosearch_write - This function handles autosearch flag modifying.
835c7428422SThibaut VARENE  * @buf: The input buffer to read from.
836c7428422SThibaut VARENE  * @count: The number of bytes to be read.
837c7428422SThibaut VARENE  *
838c7428422SThibaut VARENE  * We will call this function to change the current boot flags.
839c7428422SThibaut VARENE  * We expect a precise syntax:
840c7428422SThibaut VARENE  *	\"n\" (n == 0 or 1) to toggle AutoSearch Off or On
841c7428422SThibaut VARENE  */
8427f548217SGreg Kroah-Hartman static ssize_t pdcs_autosearch_write(struct kobject *kobj,
8437f548217SGreg Kroah-Hartman 				     struct kobj_attribute *attr,
8447f548217SGreg Kroah-Hartman 				     const char *buf, size_t count)
845c7428422SThibaut VARENE {
846ff451d70SJoel Soete 	return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH);
847c7428422SThibaut VARENE }
848c7428422SThibaut VARENE 
8493f9edb53SThibaut Varene /**
8503f9edb53SThibaut Varene  * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
8513f9edb53SThibaut Varene  * @buf: The input buffer to read from.
8523f9edb53SThibaut Varene  * @count: The number of bytes to be read.
8533f9edb53SThibaut Varene  *
8543f9edb53SThibaut Varene  * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
8553f9edb53SThibaut Varene  * write approach. It's up to userspace to deal with it when constructing
8563f9edb53SThibaut Varene  * its input buffer.
8573f9edb53SThibaut Varene  */
8587f548217SGreg Kroah-Hartman static ssize_t pdcs_osdep1_write(struct kobject *kobj,
8597f548217SGreg Kroah-Hartman 				 struct kobj_attribute *attr,
8607f548217SGreg Kroah-Hartman 				 const char *buf, size_t count)
8613f9edb53SThibaut Varene {
8623f9edb53SThibaut Varene 	u8 in[16];
8633f9edb53SThibaut Varene 
8643f9edb53SThibaut Varene 	if (!capable(CAP_SYS_ADMIN))
8653f9edb53SThibaut Varene 		return -EACCES;
8663f9edb53SThibaut Varene 
8677f548217SGreg Kroah-Hartman 	if (!buf || !count)
8683f9edb53SThibaut Varene 		return -EINVAL;
8693f9edb53SThibaut Varene 
870ec1fdc24SKyle McMartin 	if (unlikely(pdcs_osid != OS_ID_LINUX))
8713f9edb53SThibaut Varene 		return -EPERM;
8723f9edb53SThibaut Varene 
8733f9edb53SThibaut Varene 	if (count > 16)
8743f9edb53SThibaut Varene 		return -EMSGSIZE;
8753f9edb53SThibaut Varene 
8763f9edb53SThibaut Varene 	/* We'll use a local copy of buf */
8773f9edb53SThibaut Varene 	memset(in, 0, 16);
8783f9edb53SThibaut Varene 	memcpy(in, buf, count);
8793f9edb53SThibaut Varene 
8803f9edb53SThibaut Varene 	if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
8813f9edb53SThibaut Varene 		return -EIO;
8823f9edb53SThibaut Varene 
8833f9edb53SThibaut Varene 	return count;
8843f9edb53SThibaut Varene }
8853f9edb53SThibaut Varene 
8863f9edb53SThibaut Varene /**
8873f9edb53SThibaut Varene  * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
8883f9edb53SThibaut Varene  * @buf: The input buffer to read from.
8893f9edb53SThibaut Varene  * @count: The number of bytes to be read.
8903f9edb53SThibaut Varene  *
8913f9edb53SThibaut Varene  * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
8923f9edb53SThibaut Varene  * byte-by-byte write approach. It's up to userspace to deal with it when
8933f9edb53SThibaut Varene  * constructing its input buffer.
8943f9edb53SThibaut Varene  */
8957f548217SGreg Kroah-Hartman static ssize_t pdcs_osdep2_write(struct kobject *kobj,
8967f548217SGreg Kroah-Hartman 				 struct kobj_attribute *attr,
8977f548217SGreg Kroah-Hartman 				 const char *buf, size_t count)
8983f9edb53SThibaut Varene {
8993f9edb53SThibaut Varene 	unsigned long size;
9003f9edb53SThibaut Varene 	unsigned short i;
9013f9edb53SThibaut Varene 	u8 in[4];
9023f9edb53SThibaut Varene 
9033f9edb53SThibaut Varene 	if (!capable(CAP_SYS_ADMIN))
9043f9edb53SThibaut Varene 		return -EACCES;
9053f9edb53SThibaut Varene 
9067f548217SGreg Kroah-Hartman 	if (!buf || !count)
9073f9edb53SThibaut Varene 		return -EINVAL;
9083f9edb53SThibaut Varene 
9093f9edb53SThibaut Varene 	if (unlikely(pdcs_size <= 224))
9103f9edb53SThibaut Varene 		return -ENOSYS;
9113f9edb53SThibaut Varene 
912ec1fdc24SKyle McMartin 	if (unlikely(pdcs_osid != OS_ID_LINUX))
9133f9edb53SThibaut Varene 		return -EPERM;
9143f9edb53SThibaut Varene 
9153f9edb53SThibaut Varene 	size = pdcs_size - 224;
9163f9edb53SThibaut Varene 
9173f9edb53SThibaut Varene 	if (count > size)
9183f9edb53SThibaut Varene 		return -EMSGSIZE;
9193f9edb53SThibaut Varene 
9203f9edb53SThibaut Varene 	/* We'll use a local copy of buf */
9213f9edb53SThibaut Varene 
9223f9edb53SThibaut Varene 	for (i=0; i<count; i+=4) {
9233f9edb53SThibaut Varene 		memset(in, 0, 4);
9243f9edb53SThibaut Varene 		memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
9253f9edb53SThibaut Varene 		if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
9263f9edb53SThibaut Varene 					sizeof(in)) != PDC_OK))
9273f9edb53SThibaut Varene 			return -EIO;
9283f9edb53SThibaut Varene 	}
9293f9edb53SThibaut Varene 
9303f9edb53SThibaut Varene 	return count;
9313f9edb53SThibaut Varene }
9323f9edb53SThibaut Varene 
933c7428422SThibaut VARENE /* The remaining attributes. */
934c7428422SThibaut VARENE static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
935c7428422SThibaut VARENE static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
936c7428422SThibaut VARENE static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
937c7428422SThibaut VARENE static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
9383f9edb53SThibaut Varene static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
9393f9edb53SThibaut Varene static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
9403f9edb53SThibaut Varene static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
941c7428422SThibaut VARENE static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
9423f9edb53SThibaut Varene static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
9431da177e4SLinus Torvalds 
9447f548217SGreg Kroah-Hartman static struct attribute *pdcs_subsys_attrs[] = {
9457f548217SGreg Kroah-Hartman 	&pdcs_attr_size.attr,
9467f548217SGreg Kroah-Hartman 	&pdcs_attr_autoboot.attr,
9477f548217SGreg Kroah-Hartman 	&pdcs_attr_autosearch.attr,
9487f548217SGreg Kroah-Hartman 	&pdcs_attr_timer.attr,
9497f548217SGreg Kroah-Hartman 	&pdcs_attr_osid.attr,
9507f548217SGreg Kroah-Hartman 	&pdcs_attr_osdep1.attr,
9517f548217SGreg Kroah-Hartman 	&pdcs_attr_diagnostic.attr,
9527f548217SGreg Kroah-Hartman 	&pdcs_attr_fastsize.attr,
9537f548217SGreg Kroah-Hartman 	&pdcs_attr_osdep2.attr,
954c7428422SThibaut VARENE 	NULL,
9551da177e4SLinus Torvalds };
9561da177e4SLinus Torvalds 
9577f548217SGreg Kroah-Hartman static struct attribute_group pdcs_attr_group = {
9587f548217SGreg Kroah-Hartman 	.attrs = pdcs_subsys_attrs,
9597f548217SGreg Kroah-Hartman };
9607f548217SGreg Kroah-Hartman 
961c829a5b4SGreg Kroah-Hartman static struct kobject *stable_kobj;
9624443d07fSGreg Kroah-Hartman static struct kset *paths_kset;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds /**
9651da177e4SLinus Torvalds  * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
9661da177e4SLinus Torvalds  *
9671da177e4SLinus Torvalds  * It creates kobjects corresponding to each path entry with nice sysfs
9681da177e4SLinus Torvalds  * links to the real device. This is where the magic takes place: when
9691da177e4SLinus Torvalds  * registering the subsystem attributes during module init, each kobject hereby
9701da177e4SLinus Torvalds  * created will show in the sysfs tree as a folder containing files as defined
9711da177e4SLinus Torvalds  * by path_subsys_attr[].
9721da177e4SLinus Torvalds  */
9731da177e4SLinus Torvalds static inline int __init
9741da177e4SLinus Torvalds pdcs_register_pathentries(void)
9751da177e4SLinus Torvalds {
9761da177e4SLinus Torvalds 	unsigned short i;
9771da177e4SLinus Torvalds 	struct pdcspath_entry *entry;
9784b991da7SThibaut VARENE 	int err;
9791da177e4SLinus Torvalds 
980c7428422SThibaut VARENE 	/* Initialize the entries rw_lock before anything else */
981c7428422SThibaut VARENE 	for (i = 0; (entry = pdcspath_entries[i]); i++)
982c7428422SThibaut VARENE 		rwlock_init(&entry->rw_lock);
983c7428422SThibaut VARENE 
9841da177e4SLinus Torvalds 	for (i = 0; (entry = pdcspath_entries[i]); i++) {
985c7428422SThibaut VARENE 		write_lock(&entry->rw_lock);
986c7428422SThibaut VARENE 		err = pdcspath_fetch(entry);
987c7428422SThibaut VARENE 		write_unlock(&entry->rw_lock);
988c7428422SThibaut VARENE 
989c7428422SThibaut VARENE 		if (err < 0)
9901da177e4SLinus Torvalds 			continue;
9911da177e4SLinus Torvalds 
9924443d07fSGreg Kroah-Hartman 		entry->kobj.kset = paths_kset;
99373f368cfSGreg Kroah-Hartman 		err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL,
99473f368cfSGreg Kroah-Hartman 					   "%s", entry->name);
99573f368cfSGreg Kroah-Hartman 		if (err)
9964b991da7SThibaut VARENE 			return err;
9974b991da7SThibaut VARENE 
9984b991da7SThibaut VARENE 		/* kobject is now registered */
999c7428422SThibaut VARENE 		write_lock(&entry->rw_lock);
10004b991da7SThibaut VARENE 		entry->ready = 2;
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 		/* Add a nice symlink to the real device */
100326f03249SKyle McMartin 		if (entry->dev) {
100426f03249SKyle McMartin 			err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
100526f03249SKyle McMartin 			WARN_ON(err);
100626f03249SKyle McMartin 		}
1007c7428422SThibaut VARENE 
1008c7428422SThibaut VARENE 		write_unlock(&entry->rw_lock);
100973f368cfSGreg Kroah-Hartman 		kobject_uevent(&entry->kobj, KOBJ_ADD);
10101da177e4SLinus Torvalds 	}
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	return 0;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds /**
10161da177e4SLinus Torvalds  * pdcs_unregister_pathentries - Routine called when unregistering the module.
10171da177e4SLinus Torvalds  */
10184b991da7SThibaut VARENE static inline void
10191da177e4SLinus Torvalds pdcs_unregister_pathentries(void)
10201da177e4SLinus Torvalds {
10211da177e4SLinus Torvalds 	unsigned short i;
10221da177e4SLinus Torvalds 	struct pdcspath_entry *entry;
10231da177e4SLinus Torvalds 
1024c7428422SThibaut VARENE 	for (i = 0; (entry = pdcspath_entries[i]); i++) {
1025c7428422SThibaut VARENE 		read_lock(&entry->rw_lock);
10264b991da7SThibaut VARENE 		if (entry->ready >= 2)
1027c10997f6SGreg Kroah-Hartman 			kobject_put(&entry->kobj);
1028c7428422SThibaut VARENE 		read_unlock(&entry->rw_lock);
1029c7428422SThibaut VARENE 	}
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds /*
1033c7428422SThibaut VARENE  * For now we register the stable subsystem with the firmware subsystem
1034c7428422SThibaut VARENE  * and the paths subsystem with the stable subsystem
10351da177e4SLinus Torvalds  */
10361da177e4SLinus Torvalds static int __init
10371da177e4SLinus Torvalds pdc_stable_init(void)
10381da177e4SLinus Torvalds {
10397f548217SGreg Kroah-Hartman 	int rc = 0, error = 0;
10403f9edb53SThibaut Varene 	u32 result;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	/* find the size of the stable storage */
10431da177e4SLinus Torvalds 	if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
10441da177e4SLinus Torvalds 		return -ENODEV;
10451da177e4SLinus Torvalds 
1046c7428422SThibaut VARENE 	/* make sure we have enough data */
1047c7428422SThibaut VARENE 	if (pdcs_size < 96)
1048c7428422SThibaut VARENE 		return -ENODATA;
10491da177e4SLinus Torvalds 
1050c7428422SThibaut VARENE 	printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
1051c7428422SThibaut VARENE 
10523f9edb53SThibaut Varene 	/* get OSID */
10533f9edb53SThibaut Varene 	if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
10543f9edb53SThibaut Varene 		return -EIO;
10553f9edb53SThibaut Varene 
10563f9edb53SThibaut Varene 	/* the actual result is 16 bits away */
10573f9edb53SThibaut Varene 	pdcs_osid = (u16)(result >> 16);
10583f9edb53SThibaut Varene 
1059c829a5b4SGreg Kroah-Hartman 	/* For now we'll register the directory at /sys/firmware/stable */
1060c829a5b4SGreg Kroah-Hartman 	stable_kobj = kobject_create_and_add("stable", firmware_kobj);
1061c829a5b4SGreg Kroah-Hartman 	if (!stable_kobj) {
10624443d07fSGreg Kroah-Hartman 		rc = -ENOMEM;
10634b991da7SThibaut VARENE 		goto fail_firmreg;
10644443d07fSGreg Kroah-Hartman 	}
10651da177e4SLinus Torvalds 
1066c7428422SThibaut VARENE 	/* Don't forget the root entries */
1067ff451d70SJoel Soete 	error = sysfs_create_group(stable_kobj, &pdcs_attr_group);
10681da177e4SLinus Torvalds 
10694443d07fSGreg Kroah-Hartman 	/* register the paths kset as a child of the stable kset */
1070c829a5b4SGreg Kroah-Hartman 	paths_kset = kset_create_and_add("paths", NULL, stable_kobj);
10714443d07fSGreg Kroah-Hartman 	if (!paths_kset) {
10724443d07fSGreg Kroah-Hartman 		rc = -ENOMEM;
10734443d07fSGreg Kroah-Hartman 		goto fail_ksetreg;
10744443d07fSGreg Kroah-Hartman 	}
10751da177e4SLinus Torvalds 
10764443d07fSGreg Kroah-Hartman 	/* now we create all "files" for the paths kset */
10774b991da7SThibaut VARENE 	if ((rc = pdcs_register_pathentries()))
10784b991da7SThibaut VARENE 		goto fail_pdcsreg;
10791da177e4SLinus Torvalds 
10804b991da7SThibaut VARENE 	return rc;
10814b991da7SThibaut VARENE 
10824b991da7SThibaut VARENE fail_pdcsreg:
10834b991da7SThibaut VARENE 	pdcs_unregister_pathentries();
10844443d07fSGreg Kroah-Hartman 	kset_unregister(paths_kset);
10854b991da7SThibaut VARENE 
10864443d07fSGreg Kroah-Hartman fail_ksetreg:
1087c10997f6SGreg Kroah-Hartman 	kobject_put(stable_kobj);
10884b991da7SThibaut VARENE 
10894b991da7SThibaut VARENE fail_firmreg:
1090c7428422SThibaut VARENE 	printk(KERN_INFO PDCS_PREFIX " bailing out\n");
10914b991da7SThibaut VARENE 	return rc;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds static void __exit
10951da177e4SLinus Torvalds pdc_stable_exit(void)
10961da177e4SLinus Torvalds {
10971da177e4SLinus Torvalds 	pdcs_unregister_pathentries();
10984443d07fSGreg Kroah-Hartman 	kset_unregister(paths_kset);
1099c10997f6SGreg Kroah-Hartman 	kobject_put(stable_kobj);
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds module_init(pdc_stable_init);
11041da177e4SLinus Torvalds module_exit(pdc_stable_exit);
1105