• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/parisc/
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 kobj_attribute pdcs_attr_##_name = { \
124	.attr = {.name = __stringify(_name), .mode = _mode}, \
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}, \
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	int ret;
271
272	if (!entry || !buf || !count)
273		return -EINVAL;
274
275	/* We'll use a local copy of buf */
276	memset(in, 0, count+1);
277	strncpy(in, buf, count);
278
279	/* Let's clean up the target. 0xff is a blank pattern */
280	memset(&hwpath, 0xff, sizeof(hwpath));
281
282	/* First, pick the mod field (the last one of the input string) */
283	if (!(temp = strrchr(in, '/')))
284		return -EINVAL;
285
286	hwpath.mod = simple_strtoul(temp+1, NULL, 10);
287	in[temp-in] = '\0';	/* truncate the remaining string. just precaution */
288	DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
289
290	/* Then, loop for each delimiter, making sure we don't have too many.
291	   we write the bc fields in a down-top way. No matter what, we stop
292	   before writing the last field. If there are too many fields anyway,
293	   then the user is a moron and it'll be caught up later when we'll
294	   check the consistency of the given hwpath. */
295	for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
296		hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
297		in[temp-in] = '\0';
298		DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
299	}
300
301	/* Store the final field */
302	hwpath.bc[i] = simple_strtoul(in, NULL, 10);
303	DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
304
305	/* Now we check that the user isn't trying to lure us */
306	if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
307		printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
308			"hardware path: %s\n", __func__, entry->name, buf);
309		return -EINVAL;
310	}
311
312	/* So far so good, let's get in deep */
313	write_lock(&entry->rw_lock);
314	entry->ready = 0;
315	entry->dev = dev;
316
317	/* Now, dive in. Write back to the hardware */
318	pdcspath_store(entry);
319
320	/* Update the symlink to the real device */
321	sysfs_remove_link(&entry->kobj, "device");
322	ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
323	WARN_ON(ret);
324
325	write_unlock(&entry->rw_lock);
326
327	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
328		entry->name, buf);
329
330	return count;
331}
332
333/**
334 * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
335 * @entry: An allocated and populated pdscpath_entry struct.
336 * @buf: The output buffer to write to.
337 *
338 * We will call this function to format the output of the layer attribute file.
339 */
340static ssize_t
341pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
342{
343	char *out = buf;
344	struct device_path *devpath;
345	short i;
346
347	if (!entry || !buf)
348		return -EINVAL;
349
350	read_lock(&entry->rw_lock);
351	devpath = &entry->devpath;
352	i = entry->ready;
353	read_unlock(&entry->rw_lock);
354
355	if (!i)	/* entry is not ready */
356		return -ENODATA;
357
358	for (i = 0; i < 6 && devpath->layers[i]; i++)
359		out += sprintf(out, "%u ", devpath->layers[i]);
360
361	out += sprintf(out, "\n");
362
363	return out - buf;
364}
365
366static ssize_t
367pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
368{
369	unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
370	unsigned short i;
371	char in[count+1], *temp;
372
373	if (!entry || !buf || !count)
374		return -EINVAL;
375
376	/* We'll use a local copy of buf */
377	memset(in, 0, count+1);
378	strncpy(in, buf, count);
379
380	/* Let's clean up the target. 0 is a blank pattern */
381	memset(&layers, 0, sizeof(layers));
382
383	/* First, pick the first layer */
384	if (unlikely(!isdigit(*in)))
385		return -EINVAL;
386	layers[0] = simple_strtoul(in, NULL, 10);
387	DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
388
389	temp = in;
390	for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
391		if (unlikely(!isdigit(*(++temp))))
392			return -EINVAL;
393		layers[i] = simple_strtoul(temp, NULL, 10);
394		DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
395	}
396
397	/* So far so good, let's get in deep */
398	write_lock(&entry->rw_lock);
399
400	/* First, overwrite the current layers with the new ones, not touching
401	   the hardware path. */
402	memcpy(&entry->devpath.layers, &layers, sizeof(layers));
403
404	/* Now, dive in. Write back to the hardware */
405	pdcspath_store(entry);
406	write_unlock(&entry->rw_lock);
407
408	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
409		entry->name, buf);
410
411	return count;
412}
413
414/**
415 * pdcspath_attr_show - Generic read function call wrapper.
416 * @kobj: The kobject to get info from.
417 * @attr: The attribute looked upon.
418 * @buf: The output buffer.
419 */
420static ssize_t
421pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
422{
423	struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
424	struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
425	ssize_t ret = 0;
426
427	if (pdcs_attr->show)
428		ret = pdcs_attr->show(entry, buf);
429
430	return ret;
431}
432
433/**
434 * pdcspath_attr_store - Generic write function call wrapper.
435 * @kobj: The kobject to write info to.
436 * @attr: The attribute to be modified.
437 * @buf: The input buffer.
438 * @count: The size of the buffer.
439 */
440static ssize_t
441pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
442			const char *buf, size_t count)
443{
444	struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
445	struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
446	ssize_t ret = 0;
447
448	if (!capable(CAP_SYS_ADMIN))
449		return -EACCES;
450
451	if (pdcs_attr->store)
452		ret = pdcs_attr->store(entry, buf, count);
453
454	return ret;
455}
456
457static const struct sysfs_ops pdcspath_attr_ops = {
458	.show = pdcspath_attr_show,
459	.store = pdcspath_attr_store,
460};
461
462/* These are the two attributes of any PDC path. */
463static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
464static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
465
466static struct attribute *paths_subsys_attrs[] = {
467	&paths_attr_hwpath.attr,
468	&paths_attr_layer.attr,
469	NULL,
470};
471
472/* Specific kobject type for our PDC paths */
473static struct kobj_type ktype_pdcspath = {
474	.sysfs_ops = &pdcspath_attr_ops,
475	.default_attrs = paths_subsys_attrs,
476};
477
478/* We hard define the 4 types of path we expect to find */
479static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
480static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
481static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
482static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
483
484/* An array containing all PDC paths we will deal with */
485static struct pdcspath_entry *pdcspath_entries[] = {
486	&pdcspath_entry_primary,
487	&pdcspath_entry_alternative,
488	&pdcspath_entry_console,
489	&pdcspath_entry_keyboard,
490	NULL,
491};
492
493
494/* For more insight of what's going on here, refer to PDC Procedures doc,
495 * Section PDC_STABLE */
496
497/**
498 * pdcs_size_read - Stable Storage size output.
499 * @buf: The output buffer to write to.
500 */
501static ssize_t pdcs_size_read(struct kobject *kobj,
502			      struct kobj_attribute *attr,
503			      char *buf)
504{
505	char *out = buf;
506
507	if (!buf)
508		return -EINVAL;
509
510	/* show the size of the stable storage */
511	out += sprintf(out, "%ld\n", pdcs_size);
512
513	return out - buf;
514}
515
516/**
517 * pdcs_auto_read - Stable Storage autoboot/search flag output.
518 * @buf: The output buffer to write to.
519 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
520 */
521static ssize_t pdcs_auto_read(struct kobject *kobj,
522			      struct kobj_attribute *attr,
523			      char *buf, int knob)
524{
525	char *out = buf;
526	struct pdcspath_entry *pathentry;
527
528	if (!buf)
529		return -EINVAL;
530
531	/* Current flags are stored in primary boot path entry */
532	pathentry = &pdcspath_entry_primary;
533
534	read_lock(&pathentry->rw_lock);
535	out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
536					"On" : "Off");
537	read_unlock(&pathentry->rw_lock);
538
539	return out - buf;
540}
541
542/**
543 * pdcs_autoboot_read - Stable Storage autoboot flag output.
544 * @buf: The output buffer to write to.
545 */
546static ssize_t pdcs_autoboot_read(struct kobject *kobj,
547				  struct kobj_attribute *attr, char *buf)
548{
549	return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT);
550}
551
552/**
553 * pdcs_autosearch_read - Stable Storage autoboot flag output.
554 * @buf: The output buffer to write to.
555 */
556static ssize_t pdcs_autosearch_read(struct kobject *kobj,
557				    struct kobj_attribute *attr, char *buf)
558{
559	return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH);
560}
561
562/**
563 * pdcs_timer_read - Stable Storage timer count output (in seconds).
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 pdcs_timer_read(struct kobject *kobj,
569			       struct kobj_attribute *attr, char *buf)
570{
571	char *out = buf;
572	struct pdcspath_entry *pathentry;
573
574	if (!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 * @buf: The output buffer to write to.
592 */
593static ssize_t pdcs_osid_read(struct kobject *kobj,
594			      struct kobj_attribute *attr, char *buf)
595{
596	char *out = buf;
597
598	if (!buf)
599		return -EINVAL;
600
601	out += sprintf(out, "%s dependent data (0x%.4x)\n",
602		os_id_to_string(pdcs_osid), pdcs_osid);
603
604	return out - buf;
605}
606
607/**
608 * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
609 * @buf: The output buffer to write to.
610 *
611 * This can hold 16 bytes of OS-Dependent data.
612 */
613static ssize_t pdcs_osdep1_read(struct kobject *kobj,
614				struct kobj_attribute *attr, char *buf)
615{
616	char *out = buf;
617	u32 result[4];
618
619	if (!buf)
620		return -EINVAL;
621
622	if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
623		return -EIO;
624
625	out += sprintf(out, "0x%.8x\n", result[0]);
626	out += sprintf(out, "0x%.8x\n", result[1]);
627	out += sprintf(out, "0x%.8x\n", result[2]);
628	out += sprintf(out, "0x%.8x\n", result[3]);
629
630	return out - buf;
631}
632
633/**
634 * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
635 * @buf: The output buffer to write to.
636 *
637 * I have NFC how to interpret the content of that register ;-).
638 */
639static ssize_t pdcs_diagnostic_read(struct kobject *kobj,
640				    struct kobj_attribute *attr, char *buf)
641{
642	char *out = buf;
643	u32 result;
644
645	if (!buf)
646		return -EINVAL;
647
648	/* get diagnostic */
649	if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
650		return -EIO;
651
652	out += sprintf(out, "0x%.4x\n", (result >> 16));
653
654	return out - buf;
655}
656
657/**
658 * pdcs_fastsize_read - Stable Storage FastSize register output.
659 * @buf: The output buffer to write to.
660 *
661 * This register holds the amount of system RAM to be tested during boot sequence.
662 */
663static ssize_t pdcs_fastsize_read(struct kobject *kobj,
664				  struct kobj_attribute *attr, char *buf)
665{
666	char *out = buf;
667	u32 result;
668
669	if (!buf)
670		return -EINVAL;
671
672	/* get fast-size */
673	if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
674		return -EIO;
675
676	if ((result & 0x0F) < 0x0E)
677		out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
678	else
679		out += sprintf(out, "All");
680	out += sprintf(out, "\n");
681
682	return out - buf;
683}
684
685/**
686 * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
687 * @buf: The output buffer to write to.
688 *
689 * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
690 */
691static ssize_t pdcs_osdep2_read(struct kobject *kobj,
692				struct kobj_attribute *attr, char *buf)
693{
694	char *out = buf;
695	unsigned long size;
696	unsigned short i;
697	u32 result;
698
699	if (unlikely(pdcs_size <= 224))
700		return -ENODATA;
701
702	size = pdcs_size - 224;
703
704	if (!buf)
705		return -EINVAL;
706
707	for (i=0; i<size; i+=4) {
708		if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
709					sizeof(result)) != PDC_OK))
710			return -EIO;
711		out += sprintf(out, "0x%.8x\n", result);
712	}
713
714	return out - buf;
715}
716
717/**
718 * pdcs_auto_write - This function handles autoboot/search flag modifying.
719 * @buf: The input buffer to read from.
720 * @count: The number of bytes to be read.
721 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
722 *
723 * We will call this function to change the current autoboot flag.
724 * We expect a precise syntax:
725 *	\"n\" (n == 0 or 1) to toggle AutoBoot Off or On
726 */
727static ssize_t pdcs_auto_write(struct kobject *kobj,
728			       struct kobj_attribute *attr, const char *buf,
729			       size_t count, int knob)
730{
731	struct pdcspath_entry *pathentry;
732	unsigned char flags;
733	char in[count+1], *temp;
734	char c;
735
736	if (!capable(CAP_SYS_ADMIN))
737		return -EACCES;
738
739	if (!buf || !count)
740		return -EINVAL;
741
742	/* We'll use a local copy of buf */
743	memset(in, 0, count+1);
744	strncpy(in, buf, count);
745
746	/* Current flags are stored in primary boot path entry */
747	pathentry = &pdcspath_entry_primary;
748
749	/* Be nice to the existing flag record */
750	read_lock(&pathentry->rw_lock);
751	flags = pathentry->devpath.flags;
752	read_unlock(&pathentry->rw_lock);
753
754	DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
755
756	temp = skip_spaces(in);
757
758	c = *temp++ - '0';
759	if ((c != 0) && (c != 1))
760		goto parse_error;
761	if (c == 0)
762		flags &= ~knob;
763	else
764		flags |= knob;
765
766	DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
767
768	/* So far so good, let's get in deep */
769	write_lock(&pathentry->rw_lock);
770
771	/* Change the path entry flags first */
772	pathentry->devpath.flags = flags;
773
774	/* Now, dive in. Write back to the hardware */
775	pdcspath_store(pathentry);
776	write_unlock(&pathentry->rw_lock);
777
778	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
779		(knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
780		(flags & knob) ? "On" : "Off");
781
782	return count;
783
784parse_error:
785	printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
786	return -EINVAL;
787}
788
789/**
790 * pdcs_autoboot_write - This function handles autoboot flag modifying.
791 * @buf: The input buffer to read from.
792 * @count: The number of bytes to be read.
793 *
794 * We will call this function to change the current boot flags.
795 * We expect a precise syntax:
796 *	\"n\" (n == 0 or 1) to toggle AutoSearch Off or On
797 */
798static ssize_t pdcs_autoboot_write(struct kobject *kobj,
799				   struct kobj_attribute *attr,
800				   const char *buf, size_t count)
801{
802	return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT);
803}
804
805/**
806 * pdcs_autosearch_write - This function handles autosearch flag modifying.
807 * @buf: The input buffer to read from.
808 * @count: The number of bytes to be read.
809 *
810 * We will call this function to change the current boot flags.
811 * We expect a precise syntax:
812 *	\"n\" (n == 0 or 1) to toggle AutoSearch Off or On
813 */
814static ssize_t pdcs_autosearch_write(struct kobject *kobj,
815				     struct kobj_attribute *attr,
816				     const char *buf, size_t count)
817{
818	return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH);
819}
820
821/**
822 * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
823 * @buf: The input buffer to read from.
824 * @count: The number of bytes to be read.
825 *
826 * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
827 * write approach. It's up to userspace to deal with it when constructing
828 * its input buffer.
829 */
830static ssize_t pdcs_osdep1_write(struct kobject *kobj,
831				 struct kobj_attribute *attr,
832				 const char *buf, size_t count)
833{
834	u8 in[16];
835
836	if (!capable(CAP_SYS_ADMIN))
837		return -EACCES;
838
839	if (!buf || !count)
840		return -EINVAL;
841
842	if (unlikely(pdcs_osid != OS_ID_LINUX))
843		return -EPERM;
844
845	if (count > 16)
846		return -EMSGSIZE;
847
848	/* We'll use a local copy of buf */
849	memset(in, 0, 16);
850	memcpy(in, buf, count);
851
852	if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
853		return -EIO;
854
855	return count;
856}
857
858/**
859 * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
860 * @buf: The input buffer to read from.
861 * @count: The number of bytes to be read.
862 *
863 * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
864 * byte-by-byte write approach. It's up to userspace to deal with it when
865 * constructing its input buffer.
866 */
867static ssize_t pdcs_osdep2_write(struct kobject *kobj,
868				 struct kobj_attribute *attr,
869				 const char *buf, size_t count)
870{
871	unsigned long size;
872	unsigned short i;
873	u8 in[4];
874
875	if (!capable(CAP_SYS_ADMIN))
876		return -EACCES;
877
878	if (!buf || !count)
879		return -EINVAL;
880
881	if (unlikely(pdcs_size <= 224))
882		return -ENOSYS;
883
884	if (unlikely(pdcs_osid != OS_ID_LINUX))
885		return -EPERM;
886
887	size = pdcs_size - 224;
888
889	if (count > size)
890		return -EMSGSIZE;
891
892	/* We'll use a local copy of buf */
893
894	for (i=0; i<count; i+=4) {
895		memset(in, 0, 4);
896		memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
897		if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
898					sizeof(in)) != PDC_OK))
899			return -EIO;
900	}
901
902	return count;
903}
904
905/* The remaining attributes. */
906static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
907static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
908static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
909static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
910static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
911static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
912static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
913static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
914static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
915
916static struct attribute *pdcs_subsys_attrs[] = {
917	&pdcs_attr_size.attr,
918	&pdcs_attr_autoboot.attr,
919	&pdcs_attr_autosearch.attr,
920	&pdcs_attr_timer.attr,
921	&pdcs_attr_osid.attr,
922	&pdcs_attr_osdep1.attr,
923	&pdcs_attr_diagnostic.attr,
924	&pdcs_attr_fastsize.attr,
925	&pdcs_attr_osdep2.attr,
926	NULL,
927};
928
929static struct attribute_group pdcs_attr_group = {
930	.attrs = pdcs_subsys_attrs,
931};
932
933static struct kobject *stable_kobj;
934static struct kset *paths_kset;
935
936/**
937 * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
938 *
939 * It creates kobjects corresponding to each path entry with nice sysfs
940 * links to the real device. This is where the magic takes place: when
941 * registering the subsystem attributes during module init, each kobject hereby
942 * created will show in the sysfs tree as a folder containing files as defined
943 * by path_subsys_attr[].
944 */
945static inline int __init
946pdcs_register_pathentries(void)
947{
948	unsigned short i;
949	struct pdcspath_entry *entry;
950	int err;
951
952	/* Initialize the entries rw_lock before anything else */
953	for (i = 0; (entry = pdcspath_entries[i]); i++)
954		rwlock_init(&entry->rw_lock);
955
956	for (i = 0; (entry = pdcspath_entries[i]); i++) {
957		write_lock(&entry->rw_lock);
958		err = pdcspath_fetch(entry);
959		write_unlock(&entry->rw_lock);
960
961		if (err < 0)
962			continue;
963
964		entry->kobj.kset = paths_kset;
965		err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL,
966					   "%s", entry->name);
967		if (err)
968			return err;
969
970		/* kobject is now registered */
971		write_lock(&entry->rw_lock);
972		entry->ready = 2;
973
974		/* Add a nice symlink to the real device */
975		if (entry->dev) {
976			err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
977			WARN_ON(err);
978		}
979
980		write_unlock(&entry->rw_lock);
981		kobject_uevent(&entry->kobj, KOBJ_ADD);
982	}
983
984	return 0;
985}
986
987/**
988 * pdcs_unregister_pathentries - Routine called when unregistering the module.
989 */
990static inline void
991pdcs_unregister_pathentries(void)
992{
993	unsigned short i;
994	struct pdcspath_entry *entry;
995
996	for (i = 0; (entry = pdcspath_entries[i]); i++) {
997		read_lock(&entry->rw_lock);
998		if (entry->ready >= 2)
999			kobject_put(&entry->kobj);
1000		read_unlock(&entry->rw_lock);
1001	}
1002}
1003
1004/*
1005 * For now we register the stable subsystem with the firmware subsystem
1006 * and the paths subsystem with the stable subsystem
1007 */
1008static int __init
1009pdc_stable_init(void)
1010{
1011	int rc = 0, error = 0;
1012	u32 result;
1013
1014	/* find the size of the stable storage */
1015	if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
1016		return -ENODEV;
1017
1018	/* make sure we have enough data */
1019	if (pdcs_size < 96)
1020		return -ENODATA;
1021
1022	printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
1023
1024	/* get OSID */
1025	if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
1026		return -EIO;
1027
1028	/* the actual result is 16 bits away */
1029	pdcs_osid = (u16)(result >> 16);
1030
1031	/* For now we'll register the directory at /sys/firmware/stable */
1032	stable_kobj = kobject_create_and_add("stable", firmware_kobj);
1033	if (!stable_kobj) {
1034		rc = -ENOMEM;
1035		goto fail_firmreg;
1036	}
1037
1038	/* Don't forget the root entries */
1039	error = sysfs_create_group(stable_kobj, &pdcs_attr_group);
1040
1041	/* register the paths kset as a child of the stable kset */
1042	paths_kset = kset_create_and_add("paths", NULL, stable_kobj);
1043	if (!paths_kset) {
1044		rc = -ENOMEM;
1045		goto fail_ksetreg;
1046	}
1047
1048	/* now we create all "files" for the paths kset */
1049	if ((rc = pdcs_register_pathentries()))
1050		goto fail_pdcsreg;
1051
1052	return rc;
1053
1054fail_pdcsreg:
1055	pdcs_unregister_pathentries();
1056	kset_unregister(paths_kset);
1057
1058fail_ksetreg:
1059	kobject_put(stable_kobj);
1060
1061fail_firmreg:
1062	printk(KERN_INFO PDCS_PREFIX " bailing out\n");
1063	return rc;
1064}
1065
1066static void __exit
1067pdc_stable_exit(void)
1068{
1069	pdcs_unregister_pathentries();
1070	kset_unregister(paths_kset);
1071	kobject_put(stable_kobj);
1072}
1073
1074
1075module_init(pdc_stable_init);
1076module_exit(pdc_stable_exit);
1077