xref: /OK3568_Linux_fs/kernel/drivers/parisc/pdc_stable.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *    Interfaces to retrieve and set PDC Stable options (firmware)
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *    Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *    DEV NOTE: the PDC Procedures reference states that:
8*4882a593Smuzhiyun  *    "A minimum of 96 bytes of Stable Storage is required. Providing more than
9*4882a593Smuzhiyun  *    96 bytes of Stable Storage is optional [...]. Failure to provide the
10*4882a593Smuzhiyun  *    optional locations from 96 to 192 results in the loss of certain
11*4882a593Smuzhiyun  *    functionality during boot."
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  *    Since locations between 96 and 192 are the various paths, most (if not
14*4882a593Smuzhiyun  *    all) PA-RISC machines should have them. Anyway, for safety reasons, the
15*4882a593Smuzhiyun  *    following code can deal with just 96 bytes of Stable Storage, and all
16*4882a593Smuzhiyun  *    sizes between 96 and 192 bytes (provided they are multiple of struct
17*4882a593Smuzhiyun  *    device_path size, eg: 128, 160 and 192) to provide full information.
18*4882a593Smuzhiyun  *    One last word: there's one path we can always count on: the primary path.
19*4882a593Smuzhiyun  *    Anything above 224 bytes is used for 'osdep2' OS-dependent storage area.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  *    The first OS-dependent area should always be available. Obviously, this is
22*4882a593Smuzhiyun  *    not true for the other one. Also bear in mind that reading/writing from/to
23*4882a593Smuzhiyun  *    osdep2 is much more expensive than from/to osdep1.
24*4882a593Smuzhiyun  *    NOTE: We do not handle the 2 bytes OS-dep area at 0x5D, nor the first
25*4882a593Smuzhiyun  *    2 bytes of storage available right after OSID. That's a total of 4 bytes
26*4882a593Smuzhiyun  *    sacrificed: -ETOOLAZY :P
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  *    The current policy wrt file permissions is:
29*4882a593Smuzhiyun  *	- write: root only
30*4882a593Smuzhiyun  *	- read: (reading triggers PDC calls) ? root only : everyone
31*4882a593Smuzhiyun  *    The rationale is that PDC calls could hog (DoS) the machine.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  *	TODO:
34*4882a593Smuzhiyun  *	- timer/fastsize write calls
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #undef PDCS_DEBUG
38*4882a593Smuzhiyun #ifdef PDCS_DEBUG
39*4882a593Smuzhiyun #define DPRINTK(fmt, args...)	printk(KERN_DEBUG fmt, ## args)
40*4882a593Smuzhiyun #else
41*4882a593Smuzhiyun #define DPRINTK(fmt, args...)
42*4882a593Smuzhiyun #endif
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #include <linux/module.h>
45*4882a593Smuzhiyun #include <linux/init.h>
46*4882a593Smuzhiyun #include <linux/kernel.h>
47*4882a593Smuzhiyun #include <linux/string.h>
48*4882a593Smuzhiyun #include <linux/capability.h>
49*4882a593Smuzhiyun #include <linux/ctype.h>
50*4882a593Smuzhiyun #include <linux/sysfs.h>
51*4882a593Smuzhiyun #include <linux/kobject.h>
52*4882a593Smuzhiyun #include <linux/device.h>
53*4882a593Smuzhiyun #include <linux/errno.h>
54*4882a593Smuzhiyun #include <linux/spinlock.h>
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #include <asm/pdc.h>
57*4882a593Smuzhiyun #include <asm/page.h>
58*4882a593Smuzhiyun #include <linux/uaccess.h>
59*4882a593Smuzhiyun #include <asm/hardware.h>
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #define PDCS_VERSION	"0.30"
62*4882a593Smuzhiyun #define PDCS_PREFIX	"PDC Stable Storage"
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #define PDCS_ADDR_PPRI	0x00
65*4882a593Smuzhiyun #define PDCS_ADDR_OSID	0x40
66*4882a593Smuzhiyun #define PDCS_ADDR_OSD1	0x48
67*4882a593Smuzhiyun #define PDCS_ADDR_DIAG	0x58
68*4882a593Smuzhiyun #define PDCS_ADDR_FSIZ	0x5C
69*4882a593Smuzhiyun #define PDCS_ADDR_PCON	0x60
70*4882a593Smuzhiyun #define PDCS_ADDR_PALT	0x80
71*4882a593Smuzhiyun #define PDCS_ADDR_PKBD	0xA0
72*4882a593Smuzhiyun #define PDCS_ADDR_OSD2	0xE0
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
75*4882a593Smuzhiyun MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
76*4882a593Smuzhiyun MODULE_LICENSE("GPL");
77*4882a593Smuzhiyun MODULE_VERSION(PDCS_VERSION);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /* holds Stable Storage size. Initialized once and for all, no lock needed */
80*4882a593Smuzhiyun static unsigned long pdcs_size __read_mostly;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /* holds OS ID. Initialized once and for all, hopefully to 0x0006 */
83*4882a593Smuzhiyun static u16 pdcs_osid __read_mostly;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /* This struct defines what we need to deal with a parisc pdc path entry */
86*4882a593Smuzhiyun struct pdcspath_entry {
87*4882a593Smuzhiyun 	rwlock_t rw_lock;		/* to protect path entry access */
88*4882a593Smuzhiyun 	short ready;			/* entry record is valid if != 0 */
89*4882a593Smuzhiyun 	unsigned long addr;		/* entry address in stable storage */
90*4882a593Smuzhiyun 	char *name;			/* entry name */
91*4882a593Smuzhiyun 	struct device_path devpath;	/* device path in parisc representation */
92*4882a593Smuzhiyun 	struct device *dev;		/* corresponding device */
93*4882a593Smuzhiyun 	struct kobject kobj;
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun struct pdcspath_attribute {
97*4882a593Smuzhiyun 	struct attribute attr;
98*4882a593Smuzhiyun 	ssize_t (*show)(struct pdcspath_entry *entry, char *buf);
99*4882a593Smuzhiyun 	ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count);
100*4882a593Smuzhiyun };
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun #define PDCSPATH_ENTRY(_addr, _name) \
103*4882a593Smuzhiyun struct pdcspath_entry pdcspath_entry_##_name = { \
104*4882a593Smuzhiyun 	.ready = 0, \
105*4882a593Smuzhiyun 	.addr = _addr, \
106*4882a593Smuzhiyun 	.name = __stringify(_name), \
107*4882a593Smuzhiyun };
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun #define PDCS_ATTR(_name, _mode, _show, _store) \
110*4882a593Smuzhiyun struct kobj_attribute pdcs_attr_##_name = { \
111*4882a593Smuzhiyun 	.attr = {.name = __stringify(_name), .mode = _mode}, \
112*4882a593Smuzhiyun 	.show = _show, \
113*4882a593Smuzhiyun 	.store = _store, \
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun #define PATHS_ATTR(_name, _mode, _show, _store) \
117*4882a593Smuzhiyun struct pdcspath_attribute paths_attr_##_name = { \
118*4882a593Smuzhiyun 	.attr = {.name = __stringify(_name), .mode = _mode}, \
119*4882a593Smuzhiyun 	.show = _show, \
120*4882a593Smuzhiyun 	.store = _store, \
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun #define to_pdcspath_attribute(_attr) container_of(_attr, struct pdcspath_attribute, attr)
124*4882a593Smuzhiyun #define to_pdcspath_entry(obj)  container_of(obj, struct pdcspath_entry, kobj)
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /**
127*4882a593Smuzhiyun  * pdcspath_fetch - This function populates the path entry structs.
128*4882a593Smuzhiyun  * @entry: A pointer to an allocated pdcspath_entry.
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * The general idea is that you don't read from the Stable Storage every time
131*4882a593Smuzhiyun  * you access the files provided by the facilities. We store a copy of the
132*4882a593Smuzhiyun  * content of the stable storage WRT various paths in these structs. We read
133*4882a593Smuzhiyun  * these structs when reading the files, and we will write to these structs when
134*4882a593Smuzhiyun  * writing to the files, and only then write them back to the Stable Storage.
135*4882a593Smuzhiyun  *
136*4882a593Smuzhiyun  * This function expects to be called with @entry->rw_lock write-hold.
137*4882a593Smuzhiyun  */
138*4882a593Smuzhiyun static int
pdcspath_fetch(struct pdcspath_entry * entry)139*4882a593Smuzhiyun pdcspath_fetch(struct pdcspath_entry *entry)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	struct device_path *devpath;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	if (!entry)
144*4882a593Smuzhiyun 		return -EINVAL;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	devpath = &entry->devpath;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
149*4882a593Smuzhiyun 			entry, devpath, entry->addr);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/* addr, devpath and count must be word aligned */
152*4882a593Smuzhiyun 	if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
153*4882a593Smuzhiyun 		return -EIO;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/* Find the matching device.
156*4882a593Smuzhiyun 	   NOTE: hardware_path overlays with device_path, so the nice cast can
157*4882a593Smuzhiyun 	   be used */
158*4882a593Smuzhiyun 	entry->dev = hwpath_to_device((struct hardware_path *)devpath);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	entry->ready = 1;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	return 0;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun  * pdcspath_store - This function writes a path to stable storage.
169*4882a593Smuzhiyun  * @entry: A pointer to an allocated pdcspath_entry.
170*4882a593Smuzhiyun  *
171*4882a593Smuzhiyun  * It can be used in two ways: either by passing it a preset devpath struct
172*4882a593Smuzhiyun  * containing an already computed hardware path, or by passing it a device
173*4882a593Smuzhiyun  * pointer, from which it'll find out the corresponding hardware path.
174*4882a593Smuzhiyun  * For now we do not handle the case where there's an error in writing to the
175*4882a593Smuzhiyun  * Stable Storage area, so you'd better not mess up the data :P
176*4882a593Smuzhiyun  *
177*4882a593Smuzhiyun  * This function expects to be called with @entry->rw_lock write-hold.
178*4882a593Smuzhiyun  */
179*4882a593Smuzhiyun static void
pdcspath_store(struct pdcspath_entry * entry)180*4882a593Smuzhiyun pdcspath_store(struct pdcspath_entry *entry)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	struct device_path *devpath;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	BUG_ON(!entry);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	devpath = &entry->devpath;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/* We expect the caller to set the ready flag to 0 if the hardware
189*4882a593Smuzhiyun 	   path struct provided is invalid, so that we know we have to fill it.
190*4882a593Smuzhiyun 	   First case, we don't have a preset hwpath... */
191*4882a593Smuzhiyun 	if (!entry->ready) {
192*4882a593Smuzhiyun 		/* ...but we have a device, map it */
193*4882a593Smuzhiyun 		BUG_ON(!entry->dev);
194*4882a593Smuzhiyun 		device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun 	/* else, we expect the provided hwpath to be valid. */
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
199*4882a593Smuzhiyun 			entry, devpath, entry->addr);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	/* addr, devpath and count must be word aligned */
202*4882a593Smuzhiyun 	if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
203*4882a593Smuzhiyun 		WARN(1, KERN_ERR "%s: an error occurred when writing to PDC.\n"
204*4882a593Smuzhiyun 				"It is likely that the Stable Storage data has been corrupted.\n"
205*4882a593Smuzhiyun 				"Please check it carefully upon next reboot.\n", __func__);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	/* kobject is already registered */
208*4882a593Smuzhiyun 	entry->ready = 2;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun /**
214*4882a593Smuzhiyun  * pdcspath_hwpath_read - This function handles hardware path pretty printing.
215*4882a593Smuzhiyun  * @entry: An allocated and populated pdscpath_entry struct.
216*4882a593Smuzhiyun  * @buf: The output buffer to write to.
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  * We will call this function to format the output of the hwpath attribute file.
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun static ssize_t
pdcspath_hwpath_read(struct pdcspath_entry * entry,char * buf)221*4882a593Smuzhiyun pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	char *out = buf;
224*4882a593Smuzhiyun 	struct device_path *devpath;
225*4882a593Smuzhiyun 	short i;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	if (!entry || !buf)
228*4882a593Smuzhiyun 		return -EINVAL;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	read_lock(&entry->rw_lock);
231*4882a593Smuzhiyun 	devpath = &entry->devpath;
232*4882a593Smuzhiyun 	i = entry->ready;
233*4882a593Smuzhiyun 	read_unlock(&entry->rw_lock);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	if (!i)	/* entry is not ready */
236*4882a593Smuzhiyun 		return -ENODATA;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	for (i = 0; i < 6; i++) {
239*4882a593Smuzhiyun 		if (devpath->bc[i] >= 128)
240*4882a593Smuzhiyun 			continue;
241*4882a593Smuzhiyun 		out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 	out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	return out - buf;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun /**
249*4882a593Smuzhiyun  * pdcspath_hwpath_write - This function handles hardware path modifying.
250*4882a593Smuzhiyun  * @entry: An allocated and populated pdscpath_entry struct.
251*4882a593Smuzhiyun  * @buf: The input buffer to read from.
252*4882a593Smuzhiyun  * @count: The number of bytes to be read.
253*4882a593Smuzhiyun  *
254*4882a593Smuzhiyun  * We will call this function to change the current hardware path.
255*4882a593Smuzhiyun  * Hardware paths are to be given '/'-delimited, without brackets.
256*4882a593Smuzhiyun  * We make sure that the provided path actually maps to an existing
257*4882a593Smuzhiyun  * device, BUT nothing would prevent some foolish user to set the path to some
258*4882a593Smuzhiyun  * PCI bridge or even a CPU...
259*4882a593Smuzhiyun  * A better work around would be to make sure we are at the end of a device tree
260*4882a593Smuzhiyun  * for instance, but it would be IMHO beyond the simple scope of that driver.
261*4882a593Smuzhiyun  * The aim is to provide a facility. Data correctness is left to userland.
262*4882a593Smuzhiyun  */
263*4882a593Smuzhiyun static ssize_t
pdcspath_hwpath_write(struct pdcspath_entry * entry,const char * buf,size_t count)264*4882a593Smuzhiyun pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	struct hardware_path hwpath;
267*4882a593Smuzhiyun 	unsigned short i;
268*4882a593Smuzhiyun 	char in[64], *temp;
269*4882a593Smuzhiyun 	struct device *dev;
270*4882a593Smuzhiyun 	int ret;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	if (!entry || !buf || !count)
273*4882a593Smuzhiyun 		return -EINVAL;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	/* We'll use a local copy of buf */
276*4882a593Smuzhiyun 	count = min_t(size_t, count, sizeof(in)-1);
277*4882a593Smuzhiyun 	strncpy(in, buf, count);
278*4882a593Smuzhiyun 	in[count] = '\0';
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	/* Let's clean up the target. 0xff is a blank pattern */
281*4882a593Smuzhiyun 	memset(&hwpath, 0xff, sizeof(hwpath));
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	/* First, pick the mod field (the last one of the input string) */
284*4882a593Smuzhiyun 	if (!(temp = strrchr(in, '/')))
285*4882a593Smuzhiyun 		return -EINVAL;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	hwpath.mod = simple_strtoul(temp+1, NULL, 10);
288*4882a593Smuzhiyun 	in[temp-in] = '\0';	/* truncate the remaining string. just precaution */
289*4882a593Smuzhiyun 	DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	/* Then, loop for each delimiter, making sure we don't have too many.
292*4882a593Smuzhiyun 	   we write the bc fields in a down-top way. No matter what, we stop
293*4882a593Smuzhiyun 	   before writing the last field. If there are too many fields anyway,
294*4882a593Smuzhiyun 	   then the user is a moron and it'll be caught up later when we'll
295*4882a593Smuzhiyun 	   check the consistency of the given hwpath. */
296*4882a593Smuzhiyun 	for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
297*4882a593Smuzhiyun 		hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
298*4882a593Smuzhiyun 		in[temp-in] = '\0';
299*4882a593Smuzhiyun 		DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
300*4882a593Smuzhiyun 	}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	/* Store the final field */
303*4882a593Smuzhiyun 	hwpath.bc[i] = simple_strtoul(in, NULL, 10);
304*4882a593Smuzhiyun 	DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	/* Now we check that the user isn't trying to lure us */
307*4882a593Smuzhiyun 	if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
308*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
309*4882a593Smuzhiyun 			"hardware path: %s\n", __func__, entry->name, buf);
310*4882a593Smuzhiyun 		return -EINVAL;
311*4882a593Smuzhiyun 	}
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	/* So far so good, let's get in deep */
314*4882a593Smuzhiyun 	write_lock(&entry->rw_lock);
315*4882a593Smuzhiyun 	entry->ready = 0;
316*4882a593Smuzhiyun 	entry->dev = dev;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	/* Now, dive in. Write back to the hardware */
319*4882a593Smuzhiyun 	pdcspath_store(entry);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	/* Update the symlink to the real device */
322*4882a593Smuzhiyun 	sysfs_remove_link(&entry->kobj, "device");
323*4882a593Smuzhiyun 	write_unlock(&entry->rw_lock);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
326*4882a593Smuzhiyun 	WARN_ON(ret);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
329*4882a593Smuzhiyun 		entry->name, buf);
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	return count;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun /**
335*4882a593Smuzhiyun  * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
336*4882a593Smuzhiyun  * @entry: An allocated and populated pdscpath_entry struct.
337*4882a593Smuzhiyun  * @buf: The output buffer to write to.
338*4882a593Smuzhiyun  *
339*4882a593Smuzhiyun  * We will call this function to format the output of the layer attribute file.
340*4882a593Smuzhiyun  */
341*4882a593Smuzhiyun static ssize_t
pdcspath_layer_read(struct pdcspath_entry * entry,char * buf)342*4882a593Smuzhiyun pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	char *out = buf;
345*4882a593Smuzhiyun 	struct device_path *devpath;
346*4882a593Smuzhiyun 	short i;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	if (!entry || !buf)
349*4882a593Smuzhiyun 		return -EINVAL;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	read_lock(&entry->rw_lock);
352*4882a593Smuzhiyun 	devpath = &entry->devpath;
353*4882a593Smuzhiyun 	i = entry->ready;
354*4882a593Smuzhiyun 	read_unlock(&entry->rw_lock);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	if (!i)	/* entry is not ready */
357*4882a593Smuzhiyun 		return -ENODATA;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	for (i = 0; i < 6 && devpath->layers[i]; i++)
360*4882a593Smuzhiyun 		out += sprintf(out, "%u ", devpath->layers[i]);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	out += sprintf(out, "\n");
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	return out - buf;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun /**
368*4882a593Smuzhiyun  * pdcspath_layer_write - This function handles extended layer modifying.
369*4882a593Smuzhiyun  * @entry: An allocated and populated pdscpath_entry struct.
370*4882a593Smuzhiyun  * @buf: The input buffer to read from.
371*4882a593Smuzhiyun  * @count: The number of bytes to be read.
372*4882a593Smuzhiyun  *
373*4882a593Smuzhiyun  * We will call this function to change the current layer value.
374*4882a593Smuzhiyun  * Layers are to be given '.'-delimited, without brackets.
375*4882a593Smuzhiyun  * XXX beware we are far less checky WRT input data provided than for hwpath.
376*4882a593Smuzhiyun  * Potential harm can be done, since there's no way to check the validity of
377*4882a593Smuzhiyun  * the layer fields.
378*4882a593Smuzhiyun  */
379*4882a593Smuzhiyun static ssize_t
pdcspath_layer_write(struct pdcspath_entry * entry,const char * buf,size_t count)380*4882a593Smuzhiyun pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
383*4882a593Smuzhiyun 	unsigned short i;
384*4882a593Smuzhiyun 	char in[64], *temp;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	if (!entry || !buf || !count)
387*4882a593Smuzhiyun 		return -EINVAL;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	/* We'll use a local copy of buf */
390*4882a593Smuzhiyun 	count = min_t(size_t, count, sizeof(in)-1);
391*4882a593Smuzhiyun 	strncpy(in, buf, count);
392*4882a593Smuzhiyun 	in[count] = '\0';
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* Let's clean up the target. 0 is a blank pattern */
395*4882a593Smuzhiyun 	memset(&layers, 0, sizeof(layers));
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	/* First, pick the first layer */
398*4882a593Smuzhiyun 	if (unlikely(!isdigit(*in)))
399*4882a593Smuzhiyun 		return -EINVAL;
400*4882a593Smuzhiyun 	layers[0] = simple_strtoul(in, NULL, 10);
401*4882a593Smuzhiyun 	DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	temp = in;
404*4882a593Smuzhiyun 	for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
405*4882a593Smuzhiyun 		if (unlikely(!isdigit(*(++temp))))
406*4882a593Smuzhiyun 			return -EINVAL;
407*4882a593Smuzhiyun 		layers[i] = simple_strtoul(temp, NULL, 10);
408*4882a593Smuzhiyun 		DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	/* So far so good, let's get in deep */
412*4882a593Smuzhiyun 	write_lock(&entry->rw_lock);
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	/* First, overwrite the current layers with the new ones, not touching
415*4882a593Smuzhiyun 	   the hardware path. */
416*4882a593Smuzhiyun 	memcpy(&entry->devpath.layers, &layers, sizeof(layers));
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	/* Now, dive in. Write back to the hardware */
419*4882a593Smuzhiyun 	pdcspath_store(entry);
420*4882a593Smuzhiyun 	write_unlock(&entry->rw_lock);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
423*4882a593Smuzhiyun 		entry->name, buf);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	return count;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun /**
429*4882a593Smuzhiyun  * pdcspath_attr_show - Generic read function call wrapper.
430*4882a593Smuzhiyun  * @kobj: The kobject to get info from.
431*4882a593Smuzhiyun  * @attr: The attribute looked upon.
432*4882a593Smuzhiyun  * @buf: The output buffer.
433*4882a593Smuzhiyun  */
434*4882a593Smuzhiyun static ssize_t
pdcspath_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)435*4882a593Smuzhiyun pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun 	struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
438*4882a593Smuzhiyun 	struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
439*4882a593Smuzhiyun 	ssize_t ret = 0;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	if (pdcs_attr->show)
442*4882a593Smuzhiyun 		ret = pdcs_attr->show(entry, buf);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	return ret;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun /**
448*4882a593Smuzhiyun  * pdcspath_attr_store - Generic write function call wrapper.
449*4882a593Smuzhiyun  * @kobj: The kobject to write info to.
450*4882a593Smuzhiyun  * @attr: The attribute to be modified.
451*4882a593Smuzhiyun  * @buf: The input buffer.
452*4882a593Smuzhiyun  * @count: The size of the buffer.
453*4882a593Smuzhiyun  */
454*4882a593Smuzhiyun static ssize_t
pdcspath_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)455*4882a593Smuzhiyun pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
456*4882a593Smuzhiyun 			const char *buf, size_t count)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun 	struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
459*4882a593Smuzhiyun 	struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
460*4882a593Smuzhiyun 	ssize_t ret = 0;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
463*4882a593Smuzhiyun 		return -EACCES;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (pdcs_attr->store)
466*4882a593Smuzhiyun 		ret = pdcs_attr->store(entry, buf, count);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	return ret;
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun static const struct sysfs_ops pdcspath_attr_ops = {
472*4882a593Smuzhiyun 	.show = pdcspath_attr_show,
473*4882a593Smuzhiyun 	.store = pdcspath_attr_store,
474*4882a593Smuzhiyun };
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun /* These are the two attributes of any PDC path. */
477*4882a593Smuzhiyun static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
478*4882a593Smuzhiyun static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun static struct attribute *paths_subsys_attrs[] = {
481*4882a593Smuzhiyun 	&paths_attr_hwpath.attr,
482*4882a593Smuzhiyun 	&paths_attr_layer.attr,
483*4882a593Smuzhiyun 	NULL,
484*4882a593Smuzhiyun };
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun /* Specific kobject type for our PDC paths */
487*4882a593Smuzhiyun static struct kobj_type ktype_pdcspath = {
488*4882a593Smuzhiyun 	.sysfs_ops = &pdcspath_attr_ops,
489*4882a593Smuzhiyun 	.default_attrs = paths_subsys_attrs,
490*4882a593Smuzhiyun };
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun /* We hard define the 4 types of path we expect to find */
493*4882a593Smuzhiyun static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
494*4882a593Smuzhiyun static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
495*4882a593Smuzhiyun static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
496*4882a593Smuzhiyun static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun /* An array containing all PDC paths we will deal with */
499*4882a593Smuzhiyun static struct pdcspath_entry *pdcspath_entries[] = {
500*4882a593Smuzhiyun 	&pdcspath_entry_primary,
501*4882a593Smuzhiyun 	&pdcspath_entry_alternative,
502*4882a593Smuzhiyun 	&pdcspath_entry_console,
503*4882a593Smuzhiyun 	&pdcspath_entry_keyboard,
504*4882a593Smuzhiyun 	NULL,
505*4882a593Smuzhiyun };
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun /* For more insight of what's going on here, refer to PDC Procedures doc,
509*4882a593Smuzhiyun  * Section PDC_STABLE */
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun /**
512*4882a593Smuzhiyun  * pdcs_size_read - Stable Storage size output.
513*4882a593Smuzhiyun  * @buf: The output buffer to write to.
514*4882a593Smuzhiyun  */
pdcs_size_read(struct kobject * kobj,struct kobj_attribute * attr,char * buf)515*4882a593Smuzhiyun static ssize_t pdcs_size_read(struct kobject *kobj,
516*4882a593Smuzhiyun 			      struct kobj_attribute *attr,
517*4882a593Smuzhiyun 			      char *buf)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun 	char *out = buf;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	if (!buf)
522*4882a593Smuzhiyun 		return -EINVAL;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	/* show the size of the stable storage */
525*4882a593Smuzhiyun 	out += sprintf(out, "%ld\n", pdcs_size);
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	return out - buf;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun /**
531*4882a593Smuzhiyun  * pdcs_auto_read - Stable Storage autoboot/search flag output.
532*4882a593Smuzhiyun  * @buf: The output buffer to write to.
533*4882a593Smuzhiyun  * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
534*4882a593Smuzhiyun  */
pdcs_auto_read(struct kobject * kobj,struct kobj_attribute * attr,char * buf,int knob)535*4882a593Smuzhiyun static ssize_t pdcs_auto_read(struct kobject *kobj,
536*4882a593Smuzhiyun 			      struct kobj_attribute *attr,
537*4882a593Smuzhiyun 			      char *buf, int knob)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	char *out = buf;
540*4882a593Smuzhiyun 	struct pdcspath_entry *pathentry;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	if (!buf)
543*4882a593Smuzhiyun 		return -EINVAL;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	/* Current flags are stored in primary boot path entry */
546*4882a593Smuzhiyun 	pathentry = &pdcspath_entry_primary;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	read_lock(&pathentry->rw_lock);
549*4882a593Smuzhiyun 	out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
550*4882a593Smuzhiyun 					"On" : "Off");
551*4882a593Smuzhiyun 	read_unlock(&pathentry->rw_lock);
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	return out - buf;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun /**
557*4882a593Smuzhiyun  * pdcs_autoboot_read - Stable Storage autoboot flag output.
558*4882a593Smuzhiyun  * @buf: The output buffer to write to.
559*4882a593Smuzhiyun  */
pdcs_autoboot_read(struct kobject * kobj,struct kobj_attribute * attr,char * buf)560*4882a593Smuzhiyun static ssize_t pdcs_autoboot_read(struct kobject *kobj,
561*4882a593Smuzhiyun 				  struct kobj_attribute *attr, char *buf)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun 	return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT);
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun /**
567*4882a593Smuzhiyun  * pdcs_autosearch_read - Stable Storage autoboot flag output.
568*4882a593Smuzhiyun  * @buf: The output buffer to write to.
569*4882a593Smuzhiyun  */
pdcs_autosearch_read(struct kobject * kobj,struct kobj_attribute * attr,char * buf)570*4882a593Smuzhiyun static ssize_t pdcs_autosearch_read(struct kobject *kobj,
571*4882a593Smuzhiyun 				    struct kobj_attribute *attr, char *buf)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun 	return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH);
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun /**
577*4882a593Smuzhiyun  * pdcs_timer_read - Stable Storage timer count output (in seconds).
578*4882a593Smuzhiyun  * @buf: The output buffer to write to.
579*4882a593Smuzhiyun  *
580*4882a593Smuzhiyun  * The value of the timer field correponds to a number of seconds in powers of 2.
581*4882a593Smuzhiyun  */
pdcs_timer_read(struct kobject * kobj,struct kobj_attribute * attr,char * buf)582*4882a593Smuzhiyun static ssize_t pdcs_timer_read(struct kobject *kobj,
583*4882a593Smuzhiyun 			       struct kobj_attribute *attr, char *buf)
584*4882a593Smuzhiyun {
585*4882a593Smuzhiyun 	char *out = buf;
586*4882a593Smuzhiyun 	struct pdcspath_entry *pathentry;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	if (!buf)
589*4882a593Smuzhiyun 		return -EINVAL;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	/* Current flags are stored in primary boot path entry */
592*4882a593Smuzhiyun 	pathentry = &pdcspath_entry_primary;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	/* print the timer value in seconds */
595*4882a593Smuzhiyun 	read_lock(&pathentry->rw_lock);
596*4882a593Smuzhiyun 	out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
597*4882a593Smuzhiyun 				(1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
598*4882a593Smuzhiyun 	read_unlock(&pathentry->rw_lock);
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	return out - buf;
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun /**
604*4882a593Smuzhiyun  * pdcs_osid_read - Stable Storage OS ID register output.
605*4882a593Smuzhiyun  * @buf: The output buffer to write to.
606*4882a593Smuzhiyun  */
pdcs_osid_read(struct kobject * kobj,struct kobj_attribute * attr,char * buf)607*4882a593Smuzhiyun static ssize_t pdcs_osid_read(struct kobject *kobj,
608*4882a593Smuzhiyun 			      struct kobj_attribute *attr, char *buf)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun 	char *out = buf;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	if (!buf)
613*4882a593Smuzhiyun 		return -EINVAL;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	out += sprintf(out, "%s dependent data (0x%.4x)\n",
616*4882a593Smuzhiyun 		os_id_to_string(pdcs_osid), pdcs_osid);
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	return out - buf;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun /**
622*4882a593Smuzhiyun  * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
623*4882a593Smuzhiyun  * @buf: The output buffer to write to.
624*4882a593Smuzhiyun  *
625*4882a593Smuzhiyun  * This can hold 16 bytes of OS-Dependent data.
626*4882a593Smuzhiyun  */
pdcs_osdep1_read(struct kobject * kobj,struct kobj_attribute * attr,char * buf)627*4882a593Smuzhiyun static ssize_t pdcs_osdep1_read(struct kobject *kobj,
628*4882a593Smuzhiyun 				struct kobj_attribute *attr, char *buf)
629*4882a593Smuzhiyun {
630*4882a593Smuzhiyun 	char *out = buf;
631*4882a593Smuzhiyun 	u32 result[4];
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	if (!buf)
634*4882a593Smuzhiyun 		return -EINVAL;
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
637*4882a593Smuzhiyun 		return -EIO;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	out += sprintf(out, "0x%.8x\n", result[0]);
640*4882a593Smuzhiyun 	out += sprintf(out, "0x%.8x\n", result[1]);
641*4882a593Smuzhiyun 	out += sprintf(out, "0x%.8x\n", result[2]);
642*4882a593Smuzhiyun 	out += sprintf(out, "0x%.8x\n", result[3]);
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	return out - buf;
645*4882a593Smuzhiyun }
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun /**
648*4882a593Smuzhiyun  * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
649*4882a593Smuzhiyun  * @buf: The output buffer to write to.
650*4882a593Smuzhiyun  *
651*4882a593Smuzhiyun  * I have NFC how to interpret the content of that register ;-).
652*4882a593Smuzhiyun  */
pdcs_diagnostic_read(struct kobject * kobj,struct kobj_attribute * attr,char * buf)653*4882a593Smuzhiyun static ssize_t pdcs_diagnostic_read(struct kobject *kobj,
654*4882a593Smuzhiyun 				    struct kobj_attribute *attr, char *buf)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun 	char *out = buf;
657*4882a593Smuzhiyun 	u32 result;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	if (!buf)
660*4882a593Smuzhiyun 		return -EINVAL;
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	/* get diagnostic */
663*4882a593Smuzhiyun 	if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
664*4882a593Smuzhiyun 		return -EIO;
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	out += sprintf(out, "0x%.4x\n", (result >> 16));
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	return out - buf;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun /**
672*4882a593Smuzhiyun  * pdcs_fastsize_read - Stable Storage FastSize register output.
673*4882a593Smuzhiyun  * @buf: The output buffer to write to.
674*4882a593Smuzhiyun  *
675*4882a593Smuzhiyun  * This register holds the amount of system RAM to be tested during boot sequence.
676*4882a593Smuzhiyun  */
pdcs_fastsize_read(struct kobject * kobj,struct kobj_attribute * attr,char * buf)677*4882a593Smuzhiyun static ssize_t pdcs_fastsize_read(struct kobject *kobj,
678*4882a593Smuzhiyun 				  struct kobj_attribute *attr, char *buf)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun 	char *out = buf;
681*4882a593Smuzhiyun 	u32 result;
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	if (!buf)
684*4882a593Smuzhiyun 		return -EINVAL;
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	/* get fast-size */
687*4882a593Smuzhiyun 	if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
688*4882a593Smuzhiyun 		return -EIO;
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	if ((result & 0x0F) < 0x0E)
691*4882a593Smuzhiyun 		out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
692*4882a593Smuzhiyun 	else
693*4882a593Smuzhiyun 		out += sprintf(out, "All");
694*4882a593Smuzhiyun 	out += sprintf(out, "\n");
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	return out - buf;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun /**
700*4882a593Smuzhiyun  * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
701*4882a593Smuzhiyun  * @buf: The output buffer to write to.
702*4882a593Smuzhiyun  *
703*4882a593Smuzhiyun  * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
704*4882a593Smuzhiyun  */
pdcs_osdep2_read(struct kobject * kobj,struct kobj_attribute * attr,char * buf)705*4882a593Smuzhiyun static ssize_t pdcs_osdep2_read(struct kobject *kobj,
706*4882a593Smuzhiyun 				struct kobj_attribute *attr, char *buf)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun 	char *out = buf;
709*4882a593Smuzhiyun 	unsigned long size;
710*4882a593Smuzhiyun 	unsigned short i;
711*4882a593Smuzhiyun 	u32 result;
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	if (unlikely(pdcs_size <= 224))
714*4882a593Smuzhiyun 		return -ENODATA;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	size = pdcs_size - 224;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	if (!buf)
719*4882a593Smuzhiyun 		return -EINVAL;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	for (i=0; i<size; i+=4) {
722*4882a593Smuzhiyun 		if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
723*4882a593Smuzhiyun 					sizeof(result)) != PDC_OK))
724*4882a593Smuzhiyun 			return -EIO;
725*4882a593Smuzhiyun 		out += sprintf(out, "0x%.8x\n", result);
726*4882a593Smuzhiyun 	}
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	return out - buf;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun /**
732*4882a593Smuzhiyun  * pdcs_auto_write - This function handles autoboot/search flag modifying.
733*4882a593Smuzhiyun  * @buf: The input buffer to read from.
734*4882a593Smuzhiyun  * @count: The number of bytes to be read.
735*4882a593Smuzhiyun  * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
736*4882a593Smuzhiyun  *
737*4882a593Smuzhiyun  * We will call this function to change the current autoboot flag.
738*4882a593Smuzhiyun  * We expect a precise syntax:
739*4882a593Smuzhiyun  *	\"n\" (n == 0 or 1) to toggle AutoBoot Off or On
740*4882a593Smuzhiyun  */
pdcs_auto_write(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count,int knob)741*4882a593Smuzhiyun static ssize_t pdcs_auto_write(struct kobject *kobj,
742*4882a593Smuzhiyun 			       struct kobj_attribute *attr, const char *buf,
743*4882a593Smuzhiyun 			       size_t count, int knob)
744*4882a593Smuzhiyun {
745*4882a593Smuzhiyun 	struct pdcspath_entry *pathentry;
746*4882a593Smuzhiyun 	unsigned char flags;
747*4882a593Smuzhiyun 	char in[8], *temp;
748*4882a593Smuzhiyun 	char c;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
751*4882a593Smuzhiyun 		return -EACCES;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	if (!buf || !count)
754*4882a593Smuzhiyun 		return -EINVAL;
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	/* We'll use a local copy of buf */
757*4882a593Smuzhiyun 	count = min_t(size_t, count, sizeof(in)-1);
758*4882a593Smuzhiyun 	strncpy(in, buf, count);
759*4882a593Smuzhiyun 	in[count] = '\0';
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	/* Current flags are stored in primary boot path entry */
762*4882a593Smuzhiyun 	pathentry = &pdcspath_entry_primary;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	/* Be nice to the existing flag record */
765*4882a593Smuzhiyun 	read_lock(&pathentry->rw_lock);
766*4882a593Smuzhiyun 	flags = pathentry->devpath.flags;
767*4882a593Smuzhiyun 	read_unlock(&pathentry->rw_lock);
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	temp = skip_spaces(in);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	c = *temp++ - '0';
774*4882a593Smuzhiyun 	if ((c != 0) && (c != 1))
775*4882a593Smuzhiyun 		goto parse_error;
776*4882a593Smuzhiyun 	if (c == 0)
777*4882a593Smuzhiyun 		flags &= ~knob;
778*4882a593Smuzhiyun 	else
779*4882a593Smuzhiyun 		flags |= knob;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 	DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	/* So far so good, let's get in deep */
784*4882a593Smuzhiyun 	write_lock(&pathentry->rw_lock);
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	/* Change the path entry flags first */
787*4882a593Smuzhiyun 	pathentry->devpath.flags = flags;
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	/* Now, dive in. Write back to the hardware */
790*4882a593Smuzhiyun 	pdcspath_store(pathentry);
791*4882a593Smuzhiyun 	write_unlock(&pathentry->rw_lock);
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
794*4882a593Smuzhiyun 		(knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
795*4882a593Smuzhiyun 		(flags & knob) ? "On" : "Off");
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	return count;
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun parse_error:
800*4882a593Smuzhiyun 	printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
801*4882a593Smuzhiyun 	return -EINVAL;
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun /**
805*4882a593Smuzhiyun  * pdcs_autoboot_write - This function handles autoboot flag modifying.
806*4882a593Smuzhiyun  * @buf: The input buffer to read from.
807*4882a593Smuzhiyun  * @count: The number of bytes to be read.
808*4882a593Smuzhiyun  *
809*4882a593Smuzhiyun  * We will call this function to change the current boot flags.
810*4882a593Smuzhiyun  * We expect a precise syntax:
811*4882a593Smuzhiyun  *	\"n\" (n == 0 or 1) to toggle AutoSearch Off or On
812*4882a593Smuzhiyun  */
pdcs_autoboot_write(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)813*4882a593Smuzhiyun static ssize_t pdcs_autoboot_write(struct kobject *kobj,
814*4882a593Smuzhiyun 				   struct kobj_attribute *attr,
815*4882a593Smuzhiyun 				   const char *buf, size_t count)
816*4882a593Smuzhiyun {
817*4882a593Smuzhiyun 	return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT);
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun /**
821*4882a593Smuzhiyun  * pdcs_autosearch_write - This function handles autosearch flag modifying.
822*4882a593Smuzhiyun  * @buf: The input buffer to read from.
823*4882a593Smuzhiyun  * @count: The number of bytes to be read.
824*4882a593Smuzhiyun  *
825*4882a593Smuzhiyun  * We will call this function to change the current boot flags.
826*4882a593Smuzhiyun  * We expect a precise syntax:
827*4882a593Smuzhiyun  *	\"n\" (n == 0 or 1) to toggle AutoSearch Off or On
828*4882a593Smuzhiyun  */
pdcs_autosearch_write(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)829*4882a593Smuzhiyun static ssize_t pdcs_autosearch_write(struct kobject *kobj,
830*4882a593Smuzhiyun 				     struct kobj_attribute *attr,
831*4882a593Smuzhiyun 				     const char *buf, size_t count)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun 	return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH);
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun /**
837*4882a593Smuzhiyun  * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
838*4882a593Smuzhiyun  * @buf: The input buffer to read from.
839*4882a593Smuzhiyun  * @count: The number of bytes to be read.
840*4882a593Smuzhiyun  *
841*4882a593Smuzhiyun  * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
842*4882a593Smuzhiyun  * write approach. It's up to userspace to deal with it when constructing
843*4882a593Smuzhiyun  * its input buffer.
844*4882a593Smuzhiyun  */
pdcs_osdep1_write(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)845*4882a593Smuzhiyun static ssize_t pdcs_osdep1_write(struct kobject *kobj,
846*4882a593Smuzhiyun 				 struct kobj_attribute *attr,
847*4882a593Smuzhiyun 				 const char *buf, size_t count)
848*4882a593Smuzhiyun {
849*4882a593Smuzhiyun 	u8 in[16];
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
852*4882a593Smuzhiyun 		return -EACCES;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	if (!buf || !count)
855*4882a593Smuzhiyun 		return -EINVAL;
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	if (unlikely(pdcs_osid != OS_ID_LINUX))
858*4882a593Smuzhiyun 		return -EPERM;
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	if (count > 16)
861*4882a593Smuzhiyun 		return -EMSGSIZE;
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	/* We'll use a local copy of buf */
864*4882a593Smuzhiyun 	memset(in, 0, 16);
865*4882a593Smuzhiyun 	memcpy(in, buf, count);
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 	if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
868*4882a593Smuzhiyun 		return -EIO;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	return count;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun /**
874*4882a593Smuzhiyun  * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
875*4882a593Smuzhiyun  * @buf: The input buffer to read from.
876*4882a593Smuzhiyun  * @count: The number of bytes to be read.
877*4882a593Smuzhiyun  *
878*4882a593Smuzhiyun  * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
879*4882a593Smuzhiyun  * byte-by-byte write approach. It's up to userspace to deal with it when
880*4882a593Smuzhiyun  * constructing its input buffer.
881*4882a593Smuzhiyun  */
pdcs_osdep2_write(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)882*4882a593Smuzhiyun static ssize_t pdcs_osdep2_write(struct kobject *kobj,
883*4882a593Smuzhiyun 				 struct kobj_attribute *attr,
884*4882a593Smuzhiyun 				 const char *buf, size_t count)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun 	unsigned long size;
887*4882a593Smuzhiyun 	unsigned short i;
888*4882a593Smuzhiyun 	u8 in[4];
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	if (!capable(CAP_SYS_ADMIN))
891*4882a593Smuzhiyun 		return -EACCES;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	if (!buf || !count)
894*4882a593Smuzhiyun 		return -EINVAL;
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	if (unlikely(pdcs_size <= 224))
897*4882a593Smuzhiyun 		return -ENOSYS;
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	if (unlikely(pdcs_osid != OS_ID_LINUX))
900*4882a593Smuzhiyun 		return -EPERM;
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	size = pdcs_size - 224;
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 	if (count > size)
905*4882a593Smuzhiyun 		return -EMSGSIZE;
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	/* We'll use a local copy of buf */
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	for (i=0; i<count; i+=4) {
910*4882a593Smuzhiyun 		memset(in, 0, 4);
911*4882a593Smuzhiyun 		memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
912*4882a593Smuzhiyun 		if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
913*4882a593Smuzhiyun 					sizeof(in)) != PDC_OK))
914*4882a593Smuzhiyun 			return -EIO;
915*4882a593Smuzhiyun 	}
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	return count;
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun /* The remaining attributes. */
921*4882a593Smuzhiyun static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
922*4882a593Smuzhiyun static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
923*4882a593Smuzhiyun static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
924*4882a593Smuzhiyun static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
925*4882a593Smuzhiyun static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
926*4882a593Smuzhiyun static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
927*4882a593Smuzhiyun static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
928*4882a593Smuzhiyun static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
929*4882a593Smuzhiyun static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun static struct attribute *pdcs_subsys_attrs[] = {
932*4882a593Smuzhiyun 	&pdcs_attr_size.attr,
933*4882a593Smuzhiyun 	&pdcs_attr_autoboot.attr,
934*4882a593Smuzhiyun 	&pdcs_attr_autosearch.attr,
935*4882a593Smuzhiyun 	&pdcs_attr_timer.attr,
936*4882a593Smuzhiyun 	&pdcs_attr_osid.attr,
937*4882a593Smuzhiyun 	&pdcs_attr_osdep1.attr,
938*4882a593Smuzhiyun 	&pdcs_attr_diagnostic.attr,
939*4882a593Smuzhiyun 	&pdcs_attr_fastsize.attr,
940*4882a593Smuzhiyun 	&pdcs_attr_osdep2.attr,
941*4882a593Smuzhiyun 	NULL,
942*4882a593Smuzhiyun };
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun static const struct attribute_group pdcs_attr_group = {
945*4882a593Smuzhiyun 	.attrs = pdcs_subsys_attrs,
946*4882a593Smuzhiyun };
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun static struct kobject *stable_kobj;
949*4882a593Smuzhiyun static struct kset *paths_kset;
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun /**
952*4882a593Smuzhiyun  * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
953*4882a593Smuzhiyun  *
954*4882a593Smuzhiyun  * It creates kobjects corresponding to each path entry with nice sysfs
955*4882a593Smuzhiyun  * links to the real device. This is where the magic takes place: when
956*4882a593Smuzhiyun  * registering the subsystem attributes during module init, each kobject hereby
957*4882a593Smuzhiyun  * created will show in the sysfs tree as a folder containing files as defined
958*4882a593Smuzhiyun  * by path_subsys_attr[].
959*4882a593Smuzhiyun  */
960*4882a593Smuzhiyun static inline int __init
pdcs_register_pathentries(void)961*4882a593Smuzhiyun pdcs_register_pathentries(void)
962*4882a593Smuzhiyun {
963*4882a593Smuzhiyun 	unsigned short i;
964*4882a593Smuzhiyun 	struct pdcspath_entry *entry;
965*4882a593Smuzhiyun 	int err;
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	/* Initialize the entries rw_lock before anything else */
968*4882a593Smuzhiyun 	for (i = 0; (entry = pdcspath_entries[i]); i++)
969*4882a593Smuzhiyun 		rwlock_init(&entry->rw_lock);
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	for (i = 0; (entry = pdcspath_entries[i]); i++) {
972*4882a593Smuzhiyun 		write_lock(&entry->rw_lock);
973*4882a593Smuzhiyun 		err = pdcspath_fetch(entry);
974*4882a593Smuzhiyun 		write_unlock(&entry->rw_lock);
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 		if (err < 0)
977*4882a593Smuzhiyun 			continue;
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 		entry->kobj.kset = paths_kset;
980*4882a593Smuzhiyun 		err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL,
981*4882a593Smuzhiyun 					   "%s", entry->name);
982*4882a593Smuzhiyun 		if (err) {
983*4882a593Smuzhiyun 			kobject_put(&entry->kobj);
984*4882a593Smuzhiyun 			return err;
985*4882a593Smuzhiyun 		}
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 		/* kobject is now registered */
988*4882a593Smuzhiyun 		write_lock(&entry->rw_lock);
989*4882a593Smuzhiyun 		entry->ready = 2;
990*4882a593Smuzhiyun 		write_unlock(&entry->rw_lock);
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 		/* Add a nice symlink to the real device */
993*4882a593Smuzhiyun 		if (entry->dev) {
994*4882a593Smuzhiyun 			err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
995*4882a593Smuzhiyun 			WARN_ON(err);
996*4882a593Smuzhiyun 		}
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 		kobject_uevent(&entry->kobj, KOBJ_ADD);
999*4882a593Smuzhiyun 	}
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun 	return 0;
1002*4882a593Smuzhiyun }
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun /**
1005*4882a593Smuzhiyun  * pdcs_unregister_pathentries - Routine called when unregistering the module.
1006*4882a593Smuzhiyun  */
1007*4882a593Smuzhiyun static inline void
pdcs_unregister_pathentries(void)1008*4882a593Smuzhiyun pdcs_unregister_pathentries(void)
1009*4882a593Smuzhiyun {
1010*4882a593Smuzhiyun 	unsigned short i;
1011*4882a593Smuzhiyun 	struct pdcspath_entry *entry;
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun 	for (i = 0; (entry = pdcspath_entries[i]); i++) {
1014*4882a593Smuzhiyun 		read_lock(&entry->rw_lock);
1015*4882a593Smuzhiyun 		if (entry->ready >= 2)
1016*4882a593Smuzhiyun 			kobject_put(&entry->kobj);
1017*4882a593Smuzhiyun 		read_unlock(&entry->rw_lock);
1018*4882a593Smuzhiyun 	}
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun /*
1022*4882a593Smuzhiyun  * For now we register the stable subsystem with the firmware subsystem
1023*4882a593Smuzhiyun  * and the paths subsystem with the stable subsystem
1024*4882a593Smuzhiyun  */
1025*4882a593Smuzhiyun static int __init
pdc_stable_init(void)1026*4882a593Smuzhiyun pdc_stable_init(void)
1027*4882a593Smuzhiyun {
1028*4882a593Smuzhiyun 	int rc = 0, error = 0;
1029*4882a593Smuzhiyun 	u32 result;
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	/* find the size of the stable storage */
1032*4882a593Smuzhiyun 	if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
1033*4882a593Smuzhiyun 		return -ENODEV;
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 	/* make sure we have enough data */
1036*4882a593Smuzhiyun 	if (pdcs_size < 96)
1037*4882a593Smuzhiyun 		return -ENODATA;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	/* get OSID */
1042*4882a593Smuzhiyun 	if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
1043*4882a593Smuzhiyun 		return -EIO;
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	/* the actual result is 16 bits away */
1046*4882a593Smuzhiyun 	pdcs_osid = (u16)(result >> 16);
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	/* For now we'll register the directory at /sys/firmware/stable */
1049*4882a593Smuzhiyun 	stable_kobj = kobject_create_and_add("stable", firmware_kobj);
1050*4882a593Smuzhiyun 	if (!stable_kobj) {
1051*4882a593Smuzhiyun 		rc = -ENOMEM;
1052*4882a593Smuzhiyun 		goto fail_firmreg;
1053*4882a593Smuzhiyun 	}
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	/* Don't forget the root entries */
1056*4882a593Smuzhiyun 	error = sysfs_create_group(stable_kobj, &pdcs_attr_group);
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	/* register the paths kset as a child of the stable kset */
1059*4882a593Smuzhiyun 	paths_kset = kset_create_and_add("paths", NULL, stable_kobj);
1060*4882a593Smuzhiyun 	if (!paths_kset) {
1061*4882a593Smuzhiyun 		rc = -ENOMEM;
1062*4882a593Smuzhiyun 		goto fail_ksetreg;
1063*4882a593Smuzhiyun 	}
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	/* now we create all "files" for the paths kset */
1066*4882a593Smuzhiyun 	if ((rc = pdcs_register_pathentries()))
1067*4882a593Smuzhiyun 		goto fail_pdcsreg;
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	return rc;
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun fail_pdcsreg:
1072*4882a593Smuzhiyun 	pdcs_unregister_pathentries();
1073*4882a593Smuzhiyun 	kset_unregister(paths_kset);
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun fail_ksetreg:
1076*4882a593Smuzhiyun 	kobject_put(stable_kobj);
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun fail_firmreg:
1079*4882a593Smuzhiyun 	printk(KERN_INFO PDCS_PREFIX " bailing out\n");
1080*4882a593Smuzhiyun 	return rc;
1081*4882a593Smuzhiyun }
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun static void __exit
pdc_stable_exit(void)1084*4882a593Smuzhiyun pdc_stable_exit(void)
1085*4882a593Smuzhiyun {
1086*4882a593Smuzhiyun 	pdcs_unregister_pathentries();
1087*4882a593Smuzhiyun 	kset_unregister(paths_kset);
1088*4882a593Smuzhiyun 	kobject_put(stable_kobj);
1089*4882a593Smuzhiyun }
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun module_init(pdc_stable_init);
1093*4882a593Smuzhiyun module_exit(pdc_stable_exit);
1094