2 * Interfaces to retrieve and set PDC Stable options (firmware)
4 * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * DEV NOTE: the PDC Procedures reference states that:
22 * "A minimum of 96 bytes of Stable Storage is required. Providing more than
23 * 96 bytes of Stable Storage is optional [...]. Failure to provide the
24 * optional locations from 96 to 192 results in the loss of certain
25 * functionality during boot."
27 * Since locations between 96 and 192 are the various paths, most (if not
28 * all) PA-RISC machines should have them. Anyway, for safety reasons, the
29 * following code can deal with only 96 bytes of Stable Storage, and all
30 * sizes between 96 and 192 bytes (provided they are multiple of struct
31 * device_path size, eg: 128, 160 and 192) to provide full information.
32 * The code makes no use of data above 192 bytes. One last word: there's one
33 * path we can always count on: the primary path.
38 #define DPRINTK(fmt, args...) printk(KERN_DEBUG fmt, ## args)
40 #define DPRINTK(fmt, args...)
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/sched.h> /* for capable() */
46 #include <linux/kernel.h>
47 #include <linux/string.h>
48 #include <linux/ctype.h>
49 #include <linux/sysfs.h>
50 #include <linux/kobject.h>
51 #include <linux/device.h>
52 #include <linux/errno.h>
56 #include <asm/uaccess.h>
57 #include <asm/hardware.h>
59 #define PDCS_VERSION "0.09"
61 #define PDCS_ADDR_PPRI 0x00
62 #define PDCS_ADDR_OSID 0x40
63 #define PDCS_ADDR_FSIZ 0x5C
64 #define PDCS_ADDR_PCON 0x60
65 #define PDCS_ADDR_PALT 0x80
66 #define PDCS_ADDR_PKBD 0xA0
68 MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
69 MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
70 MODULE_LICENSE("GPL");
71 MODULE_VERSION(PDCS_VERSION);
73 static unsigned long pdcs_size = 0;
75 /* This struct defines what we need to deal with a parisc pdc path entry */
76 struct pdcspath_entry {
77 short ready; /* entry record is valid if != 0 */
78 unsigned long addr; /* entry address in stable storage */
79 char *name; /* entry name */
80 struct device_path devpath; /* device path in parisc representation */
81 struct device *dev; /* corresponding device */
85 struct pdcspath_attribute {
86 struct attribute attr;
87 ssize_t (*show)(struct pdcspath_entry *entry, char *buf);
88 ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count);
91 #define PDCSPATH_ENTRY(_addr, _name) \
92 struct pdcspath_entry pdcspath_entry_##_name = { \
95 .name = __stringify(_name), \
98 #define PDCS_ATTR(_name, _mode, _show, _store) \
99 struct subsys_attribute pdcs_attr_##_name = { \
100 .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
105 #define PATHS_ATTR(_name, _mode, _show, _store) \
106 struct pdcspath_attribute paths_attr_##_name = { \
107 .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
112 #define to_pdcspath_attribute(_attr) container_of(_attr, struct pdcspath_attribute, attr)
113 #define to_pdcspath_entry(obj) container_of(obj, struct pdcspath_entry, kobj)
116 * pdcspath_fetch - This function populates the path entry structs.
117 * @entry: A pointer to an allocated pdcspath_entry.
119 * The general idea is that you don't read from the Stable Storage every time
120 * you access the files provided by the facilites. We store a copy of the
121 * content of the stable storage WRT various paths in these structs. We read
122 * these structs when reading the files, and we will write to these structs when
123 * writing to the files, and only then write them back to the Stable Storage.
126 pdcspath_fetch(struct pdcspath_entry *entry)
128 struct device_path *devpath;
133 devpath = &entry->devpath;
135 DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
136 entry, devpath, entry->addr);
138 /* addr, devpath and count must be word aligned */
139 if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
142 /* Find the matching device.
143 NOTE: hardware_path overlays with device_path, so the nice cast can
145 entry->dev = hwpath_to_device((struct hardware_path *)devpath);
149 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
155 * pdcspath_store - This function writes a path to stable storage.
156 * @entry: A pointer to an allocated pdcspath_entry.
158 * It can be used in two ways: either by passing it a preset devpath struct
159 * containing an already computed hardware path, or by passing it a device
160 * pointer, from which it'll find out the corresponding hardware path.
161 * For now we do not handle the case where there's an error in writing to the
162 * Stable Storage area, so you'd better not mess up the data :P
165 pdcspath_store(struct pdcspath_entry *entry)
167 struct device_path *devpath;
172 devpath = &entry->devpath;
174 /* We expect the caller to set the ready flag to 0 if the hardware
175 path struct provided is invalid, so that we know we have to fill it.
176 First case, we don't have a preset hwpath... */
178 /* ...but we have a device, map it */
180 device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
184 /* else, we expect the provided hwpath to be valid. */
186 DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
187 entry, devpath, entry->addr);
189 /* addr, devpath and count must be word aligned */
190 if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK) {
191 printk(KERN_ERR "%s: an error occured when writing to PDC.\n"
192 "It is likely that the Stable Storage data has been corrupted.\n"
193 "Please check it carefully upon next reboot.\n", __func__);
199 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
205 * pdcspath_hwpath_read - This function handles hardware path pretty printing.
206 * @entry: An allocated and populated pdscpath_entry struct.
207 * @buf: The output buffer to write to.
209 * We will call this function to format the output of the hwpath attribute file.
212 pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
215 struct device_path *devpath;
221 devpath = &entry->devpath;
226 for (i = 0; i < 6; i++) {
227 if (devpath->bc[i] >= 128)
229 out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
231 out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
237 * pdcspath_hwpath_write - This function handles hardware path modifying.
238 * @entry: An allocated and populated pdscpath_entry struct.
239 * @buf: The input buffer to read from.
240 * @count: The number of bytes to be read.
242 * We will call this function to change the current hardware path.
243 * Hardware paths are to be given '/'-delimited, without brackets.
244 * We take care to make sure that the provided path actually maps to an existing
245 * device, BUT nothing would prevent some foolish user to set the path to some
246 * PCI bridge or even a CPU...
247 * A better work around would be to make sure we are at the end of a device tree
248 * for instance, but it would be IMHO beyond the simple scope of that driver.
249 * The aim is to provide a facility. Data correctness is left to userland.
252 pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
254 struct hardware_path hwpath;
256 char in[count+1], *temp;
259 if (!entry || !buf || !count)
262 /* We'll use a local copy of buf */
263 memset(in, 0, count+1);
264 strncpy(in, buf, count);
266 /* Let's clean up the target. 0xff is a blank pattern */
267 memset(&hwpath, 0xff, sizeof(hwpath));
269 /* First, pick the mod field (the last one of the input string) */
270 if (!(temp = strrchr(in, '/')))
273 hwpath.mod = simple_strtoul(temp+1, NULL, 10);
274 in[temp-in] = '\0'; /* truncate the remaining string. just precaution */
275 DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
277 /* Then, loop for each delimiter, making sure we don't have too many.
278 we write the bc fields in a down-top way. No matter what, we stop
279 before writing the last field. If there are too many fields anyway,
280 then the user is a moron and it'll be caught up later when we'll
281 check the consistency of the given hwpath. */
282 for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
283 hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
285 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
288 /* Store the final field */
289 hwpath.bc[i] = simple_strtoul(in, NULL, 10);
290 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
292 /* Now we check that the user isn't trying to lure us */
293 if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
294 printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
295 "hardware path: %s\n", __func__, entry->name, buf);
299 /* So far so good, let's get in deep */
303 /* Now, dive in. Write back to the hardware */
304 WARN_ON(pdcspath_store(entry)); /* this warn should *NEVER* happen */
306 /* Update the symlink to the real device */
307 sysfs_remove_link(&entry->kobj, "device");
308 sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
310 printk(KERN_INFO "PDC Stable Storage: changed \"%s\" path to \"%s\"\n",
317 * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
318 * @entry: An allocated and populated pdscpath_entry struct.
319 * @buf: The output buffer to write to.
321 * We will call this function to format the output of the layer attribute file.
324 pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
327 struct device_path *devpath;
333 devpath = &entry->devpath;
338 for (i = 0; devpath->layers[i] && (likely(i < 6)); i++)
339 out += sprintf(out, "%u ", devpath->layers[i]);
341 out += sprintf(out, "\n");
347 * pdcspath_layer_write - This function handles extended layer modifying.
348 * @entry: An allocated and populated pdscpath_entry struct.
349 * @buf: The input buffer to read from.
350 * @count: The number of bytes to be read.
352 * We will call this function to change the current layer value.
353 * Layers are to be given '.'-delimited, without brackets.
354 * XXX beware we are far less checky WRT input data provided than for hwpath.
355 * Potential harm can be done, since there's no way to check the validity of
359 pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
361 unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
363 char in[count+1], *temp;
365 if (!entry || !buf || !count)
368 /* We'll use a local copy of buf */
369 memset(in, 0, count+1);
370 strncpy(in, buf, count);
372 /* Let's clean up the target. 0 is a blank pattern */
373 memset(&layers, 0, sizeof(layers));
375 /* First, pick the first layer */
376 if (unlikely(!isdigit(*in)))
378 layers[0] = simple_strtoul(in, NULL, 10);
379 DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
382 for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
383 if (unlikely(!isdigit(*(++temp))))
385 layers[i] = simple_strtoul(temp, NULL, 10);
386 DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
389 /* So far so good, let's get in deep */
391 /* First, overwrite the current layers with the new ones, not touching
392 the hardware path. */
393 memcpy(&entry->devpath.layers, &layers, sizeof(layers));
395 /* Now, dive in. Write back to the hardware */
396 WARN_ON(pdcspath_store(entry)); /* this warn should *NEVER* happen */
398 printk(KERN_INFO "PDC Stable Storage: changed \"%s\" layers to \"%s\"\n",
405 * pdcspath_attr_show - Generic read function call wrapper.
406 * @kobj: The kobject to get info from.
407 * @attr: The attribute looked upon.
408 * @buf: The output buffer.
411 pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
413 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
414 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
417 if (!capable(CAP_SYS_ADMIN))
421 ret = pdcs_attr->show(entry, buf);
427 * pdcspath_attr_store - Generic write function call wrapper.
428 * @kobj: The kobject to write info to.
429 * @attr: The attribute to be modified.
430 * @buf: The input buffer.
431 * @count: The size of the buffer.
434 pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
435 const char *buf, size_t count)
437 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
438 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
441 if (!capable(CAP_SYS_ADMIN))
444 if (pdcs_attr->store)
445 ret = pdcs_attr->store(entry, buf, count);
450 static struct sysfs_ops pdcspath_attr_ops = {
451 .show = pdcspath_attr_show,
452 .store = pdcspath_attr_store,
455 /* These are the two attributes of any PDC path. */
456 static PATHS_ATTR(hwpath, 0600, pdcspath_hwpath_read, pdcspath_hwpath_write);
457 static PATHS_ATTR(layer, 0600, pdcspath_layer_read, pdcspath_layer_write);
459 static struct attribute *paths_subsys_attrs[] = {
460 &paths_attr_hwpath.attr,
461 &paths_attr_layer.attr,
465 /* Specific kobject type for our PDC paths */
466 static struct kobj_type ktype_pdcspath = {
467 .sysfs_ops = &pdcspath_attr_ops,
468 .default_attrs = paths_subsys_attrs,
471 /* We hard define the 4 types of path we expect to find */
472 static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
473 static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
474 static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
475 static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
477 /* An array containing all PDC paths we will deal with */
478 static struct pdcspath_entry *pdcspath_entries[] = {
479 &pdcspath_entry_primary,
480 &pdcspath_entry_alternative,
481 &pdcspath_entry_console,
482 &pdcspath_entry_keyboard,
487 * pdcs_info_read - Pretty printing of the remaining useful data.
488 * @entry: An allocated and populated subsytem struct. We don't use it tho.
489 * @buf: The output buffer to write to.
491 * We will call this function to format the output of the 'info' attribute file.
492 * Please refer to PDC Procedures documentation, section PDC_STABLE to get a
493 * better insight of what we're doing here.
496 pdcs_info_read(struct subsystem *entry, char *buf)
500 struct device_path devpath;
506 /* show the size of the stable storage */
507 out += sprintf(out, "Stable Storage size: %ld bytes\n", pdcs_size);
509 /* deal with flags */
510 if (pdc_stable_read(PDCS_ADDR_PPRI, &devpath, sizeof(devpath)) != PDC_OK)
513 out += sprintf(out, "Autoboot: %s\n", (devpath.flags & PF_AUTOBOOT) ? "On" : "Off");
514 out += sprintf(out, "Autosearch: %s\n", (devpath.flags & PF_AUTOSEARCH) ? "On" : "Off");
515 out += sprintf(out, "Timer: %u s\n", (devpath.flags & PF_TIMER) ? (1 << (devpath.flags & PF_TIMER)) : 0);
518 if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
521 /* the actual result is 16 bits away */
522 switch (result >> 16) {
523 case 0x0000: tmpstr = "No OS-dependent data"; break;
524 case 0x0001: tmpstr = "HP-UX dependent data"; break;
525 case 0x0002: tmpstr = "MPE-iX dependent data"; break;
526 case 0x0003: tmpstr = "OSF dependent data"; break;
527 case 0x0004: tmpstr = "HP-RT dependent data"; break;
528 case 0x0005: tmpstr = "Novell Netware dependent data"; break;
529 default: tmpstr = "Unknown"; break;
531 out += sprintf(out, "OS ID: %s (0x%.4x)\n", tmpstr, (result >> 16));
534 if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
537 out += sprintf(out, "Memory tested: ");
538 if ((result & 0x0F) < 0x0E)
539 out += sprintf(out, "%.3f MB", 0.256*(1<<(result & 0x0F)));
541 out += sprintf(out, "All");
542 out += sprintf(out, "\n");
548 * pdcs_info_write - This function handles boot flag modifying.
549 * @entry: An allocated and populated subsytem struct. We don't use it tho.
550 * @buf: The input buffer to read from.
551 * @count: The number of bytes to be read.
553 * We will call this function to change the current boot flags.
554 * We expect a precise syntax:
555 * \"n n\" (n == 0 or 1) to toggle respectively AutoBoot and AutoSearch
557 * As of now there is no incentive on my side to provide more "knobs" to that
558 * interface, since modifying the rest of the data is pretty meaningless when
559 * the machine is running and for the expected use of that facility, such as
560 * PALO setting up the boot disk when installing a Linux distribution...
563 pdcs_info_write(struct subsystem *entry, const char *buf, size_t count)
565 struct pdcspath_entry *pathentry;
567 char in[count+1], *temp;
570 if (!capable(CAP_SYS_ADMIN))
573 if (!entry || !buf || !count)
576 /* We'll use a local copy of buf */
577 memset(in, 0, count+1);
578 strncpy(in, buf, count);
580 /* Current flags are stored in primary boot path entry */
581 pathentry = &pdcspath_entry_primary;
583 /* Be nice to the existing flag record */
584 flags = pathentry->devpath.flags;
586 DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
590 while (*temp && isspace(*temp))
594 if ((c != 0) && (c != 1))
597 flags &= ~PF_AUTOBOOT;
599 flags |= PF_AUTOBOOT;
605 if ((c != 0) && (c != 1))
608 flags &= ~PF_AUTOSEARCH;
610 flags |= PF_AUTOSEARCH;
612 DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
614 /* So far so good, let's get in deep */
616 /* Change the path entry flags first */
617 pathentry->devpath.flags = flags;
619 /* Now, dive in. Write back to the hardware */
620 WARN_ON(pdcspath_store(pathentry)); /* this warn should *NEVER* happen */
622 printk(KERN_INFO "PDC Stable Storage: changed flags to \"%s\"\n", buf);
627 printk(KERN_WARNING "%s: Parse error: expect \"n n\" (n == 0 or 1) for AB and AS\n", __func__);
631 /* The last attribute (the 'root' one actually) with all remaining data. */
632 static PDCS_ATTR(info, 0600, pdcs_info_read, pdcs_info_write);
634 static struct subsys_attribute *pdcs_subsys_attrs[] = {
636 NULL, /* maybe more in the future? */
639 static decl_subsys(paths, &ktype_pdcspath, NULL);
640 static decl_subsys(pdc, NULL, NULL);
643 * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
645 * It creates kobjects corresponding to each path entry with nice sysfs
646 * links to the real device. This is where the magic takes place: when
647 * registering the subsystem attributes during module init, each kobject hereby
648 * created will show in the sysfs tree as a folder containing files as defined
649 * by path_subsys_attr[].
651 static inline int __init
652 pdcs_register_pathentries(void)
655 struct pdcspath_entry *entry;
657 for (i = 0; (entry = pdcspath_entries[i]); i++) {
658 if (pdcspath_fetch(entry) < 0)
661 kobject_set_name(&entry->kobj, "%s", entry->name);
662 kobj_set_kset_s(entry, paths_subsys);
663 kobject_register(&entry->kobj);
668 /* Add a nice symlink to the real device */
669 sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
676 * pdcs_unregister_pathentries - Routine called when unregistering the module.
678 static inline void __exit
679 pdcs_unregister_pathentries(void)
682 struct pdcspath_entry *entry;
684 for (i = 0; (entry = pdcspath_entries[i]); i++)
686 kobject_unregister(&entry->kobj);
690 * For now we register the pdc subsystem with the firmware subsystem
691 * and the paths subsystem with the pdc subsystem
694 pdc_stable_init(void)
696 struct subsys_attribute *attr;
697 int i, rc = 0, error = 0;
699 /* find the size of the stable storage */
700 if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
703 printk(KERN_INFO "PDC Stable Storage facility v%s\n", PDCS_VERSION);
705 /* For now we'll register the pdc subsys within this driver */
706 if ((rc = firmware_register(&pdc_subsys)))
709 /* Don't forget the info entry */
710 for (i = 0; (attr = pdcs_subsys_attrs[i]) && !error; i++)
712 error = subsys_create_file(&pdc_subsys, attr);
714 /* register the paths subsys as a subsystem of pdc subsys */
715 kset_set_kset_s(&paths_subsys, pdc_subsys);
716 subsystem_register(&paths_subsys);
718 /* now we create all "files" for the paths subsys */
719 pdcs_register_pathentries();
725 pdc_stable_exit(void)
727 pdcs_unregister_pathentries();
728 subsystem_unregister(&paths_subsys);
730 firmware_unregister(&pdc_subsys);
734 module_init(pdc_stable_init);
735 module_exit(pdc_stable_exit);