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