1 // SPDX-License-Identifier: GPL-2.0-only
3 * Interfaces to retrieve and set PDC Stable options (firmware)
5 * Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
7 * DEV NOTE: the PDC Procedures reference states that:
8 * "A minimum of 96 bytes of Stable Storage is required. Providing more than
9 * 96 bytes of Stable Storage is optional [...]. Failure to provide the
10 * optional locations from 96 to 192 results in the loss of certain
11 * functionality during boot."
13 * Since locations between 96 and 192 are the various paths, most (if not
14 * all) PA-RISC machines should have them. Anyway, for safety reasons, the
15 * following code can deal with just 96 bytes of Stable Storage, and all
16 * sizes between 96 and 192 bytes (provided they are multiple of struct
17 * pdc_module_path size, eg: 128, 160 and 192) to provide full information.
18 * One last word: there's one path we can always count on: the primary path.
19 * Anything above 224 bytes is used for 'osdep2' OS-dependent storage area.
21 * The first OS-dependent area should always be available. Obviously, this is
22 * not true for the other one. Also bear in mind that reading/writing from/to
23 * osdep2 is much more expensive than from/to osdep1.
24 * NOTE: We do not handle the 2 bytes OS-dep area at 0x5D, nor the first
25 * 2 bytes of storage available right after OSID. That's a total of 4 bytes
26 * sacrificed: -ETOOLAZY :P
28 * The current policy wrt file permissions is:
30 * - read: (reading triggers PDC calls) ? root only : everyone
31 * The rationale is that PDC calls could hog (DoS) the machine.
34 * - timer/fastsize write calls
39 #define DPRINTK(fmt, args...) printk(KERN_DEBUG fmt, ## args)
41 #define DPRINTK(fmt, args...)
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/kernel.h>
47 #include <linux/string.h>
48 #include <linux/capability.h>
49 #include <linux/ctype.h>
50 #include <linux/sysfs.h>
51 #include <linux/kobject.h>
52 #include <linux/device.h>
53 #include <linux/errno.h>
54 #include <linux/spinlock.h>
58 #include <linux/uaccess.h>
59 #include <asm/hardware.h>
61 #define PDCS_VERSION "0.30"
62 #define PDCS_PREFIX "PDC Stable Storage"
64 #define PDCS_ADDR_PPRI 0x00
65 #define PDCS_ADDR_OSID 0x40
66 #define PDCS_ADDR_OSD1 0x48
67 #define PDCS_ADDR_DIAG 0x58
68 #define PDCS_ADDR_FSIZ 0x5C
69 #define PDCS_ADDR_PCON 0x60
70 #define PDCS_ADDR_PALT 0x80
71 #define PDCS_ADDR_PKBD 0xA0
72 #define PDCS_ADDR_OSD2 0xE0
74 MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
75 MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
76 MODULE_LICENSE("GPL");
77 MODULE_VERSION(PDCS_VERSION);
79 /* holds Stable Storage size. Initialized once and for all, no lock needed */
80 static unsigned long pdcs_size __read_mostly;
82 /* holds OS ID. Initialized once and for all, hopefully to 0x0006 */
83 static u16 pdcs_osid __read_mostly;
85 /* This struct defines what we need to deal with a parisc pdc path entry */
86 struct pdcspath_entry {
87 rwlock_t rw_lock; /* to protect path entry access */
88 short ready; /* entry record is valid if != 0 */
89 unsigned long addr; /* entry address in stable storage */
90 char *name; /* entry name */
91 struct pdc_module_path devpath; /* device path in parisc representation */
92 struct device *dev; /* corresponding device */
96 struct pdcspath_attribute {
97 struct attribute attr;
98 ssize_t (*show)(struct pdcspath_entry *entry, char *buf);
99 ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count);
102 #define PDCSPATH_ENTRY(_addr, _name) \
103 struct pdcspath_entry pdcspath_entry_##_name = { \
106 .name = __stringify(_name), \
109 #define PDCS_ATTR(_name, _mode, _show, _store) \
110 struct kobj_attribute pdcs_attr_##_name = { \
111 .attr = {.name = __stringify(_name), .mode = _mode}, \
116 #define PATHS_ATTR(_name, _mode, _show, _store) \
117 struct pdcspath_attribute paths_attr_##_name = { \
118 .attr = {.name = __stringify(_name), .mode = _mode}, \
123 #define to_pdcspath_attribute(_attr) container_of(_attr, struct pdcspath_attribute, attr)
124 #define to_pdcspath_entry(obj) container_of(obj, struct pdcspath_entry, kobj)
127 * pdcspath_fetch - This function populates the path entry structs.
128 * @entry: A pointer to an allocated pdcspath_entry.
130 * The general idea is that you don't read from the Stable Storage every time
131 * you access the files provided by the facilities. We store a copy of the
132 * content of the stable storage WRT various paths in these structs. We read
133 * these structs when reading the files, and we will write to these structs when
134 * writing to the files, and only then write them back to the Stable Storage.
136 * This function expects to be called with @entry->rw_lock write-hold.
139 pdcspath_fetch(struct pdcspath_entry *entry)
141 struct pdc_module_path *devpath;
146 devpath = &entry->devpath;
148 DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
149 entry, devpath, entry->addr);
151 /* addr, devpath and count must be word aligned */
152 if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
155 /* Find the matching device.
156 NOTE: hardware_path overlays with pdc_module_path, so the nice cast can
158 entry->dev = hwpath_to_device((struct hardware_path *)devpath);
162 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
168 * pdcspath_store - This function writes a path to stable storage.
169 * @entry: A pointer to an allocated pdcspath_entry.
171 * It can be used in two ways: either by passing it a preset devpath struct
172 * containing an already computed hardware path, or by passing it a device
173 * pointer, from which it'll find out the corresponding hardware path.
174 * For now we do not handle the case where there's an error in writing to the
175 * Stable Storage area, so you'd better not mess up the data :P
177 * This function expects to be called with @entry->rw_lock write-hold.
180 pdcspath_store(struct pdcspath_entry *entry)
182 struct pdc_module_path *devpath;
186 devpath = &entry->devpath;
188 /* We expect the caller to set the ready flag to 0 if the hardware
189 path struct provided is invalid, so that we know we have to fill it.
190 First case, we don't have a preset hwpath... */
192 /* ...but we have a device, map it */
194 device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
196 /* else, we expect the provided hwpath to be valid. */
198 DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
199 entry, devpath, entry->addr);
201 /* addr, devpath and count must be word aligned */
202 if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
203 WARN(1, KERN_ERR "%s: an error occurred when writing to PDC.\n"
204 "It is likely that the Stable Storage data has been corrupted.\n"
205 "Please check it carefully upon next reboot.\n", __func__);
207 /* kobject is already registered */
210 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
214 * pdcspath_hwpath_read - This function handles hardware path pretty printing.
215 * @entry: An allocated and populated pdscpath_entry struct.
216 * @buf: The output buffer to write to.
218 * We will call this function to format the output of the hwpath attribute file.
221 pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
224 struct pdc_module_path *devpath;
230 read_lock(&entry->rw_lock);
231 devpath = &entry->devpath;
233 read_unlock(&entry->rw_lock);
235 if (!i) /* entry is not ready */
238 for (i = 0; i < 6; i++) {
239 if (devpath->path.bc[i] < 0)
241 out += sprintf(out, "%d/", devpath->path.bc[i]);
243 out += sprintf(out, "%u\n", (unsigned char)devpath->path.mod);
249 * pdcspath_hwpath_write - This function handles hardware path modifying.
250 * @entry: An allocated and populated pdscpath_entry struct.
251 * @buf: The input buffer to read from.
252 * @count: The number of bytes to be read.
254 * We will call this function to change the current hardware path.
255 * Hardware paths are to be given '/'-delimited, without brackets.
256 * We make sure that the provided path actually maps to an existing
257 * device, BUT nothing would prevent some foolish user to set the path to some
258 * PCI bridge or even a CPU...
259 * A better work around would be to make sure we are at the end of a device tree
260 * for instance, but it would be IMHO beyond the simple scope of that driver.
261 * The aim is to provide a facility. Data correctness is left to userland.
264 pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
266 struct hardware_path hwpath;
272 if (!entry || !buf || !count)
275 /* We'll use a local copy of buf */
276 count = min_t(size_t, count, sizeof(in)-1);
277 strncpy(in, buf, count);
280 /* Let's clean up the target. 0xff is a blank pattern */
281 memset(&hwpath, 0xff, sizeof(hwpath));
283 /* First, pick the mod field (the last one of the input string) */
284 if (!(temp = strrchr(in, '/')))
287 hwpath.mod = simple_strtoul(temp+1, NULL, 10);
288 in[temp-in] = '\0'; /* truncate the remaining string. just precaution */
289 DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
291 /* Then, loop for each delimiter, making sure we don't have too many.
292 we write the bc fields in a down-top way. No matter what, we stop
293 before writing the last field. If there are too many fields anyway,
294 then the user is a moron and it'll be caught up later when we'll
295 check the consistency of the given hwpath. */
296 for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
297 hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
299 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.path.bc[i]);
302 /* Store the final field */
303 hwpath.bc[i] = simple_strtoul(in, NULL, 10);
304 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.path.bc[i]);
306 /* Now we check that the user isn't trying to lure us */
307 if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
308 printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
309 "hardware path: %s\n", __func__, entry->name, buf);
313 /* So far so good, let's get in deep */
314 write_lock(&entry->rw_lock);
318 /* Now, dive in. Write back to the hardware */
319 pdcspath_store(entry);
321 /* Update the symlink to the real device */
322 sysfs_remove_link(&entry->kobj, "device");
323 write_unlock(&entry->rw_lock);
325 ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
328 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
335 * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
336 * @entry: An allocated and populated pdscpath_entry struct.
337 * @buf: The output buffer to write to.
339 * We will call this function to format the output of the layer attribute file.
342 pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
345 struct pdc_module_path *devpath;
351 read_lock(&entry->rw_lock);
352 devpath = &entry->devpath;
354 read_unlock(&entry->rw_lock);
356 if (!i) /* entry is not ready */
359 for (i = 0; i < 6 && devpath->layers[i]; i++)
360 out += sprintf(out, "%u ", devpath->layers[i]);
362 out += sprintf(out, "\n");
368 * pdcspath_layer_write - This function handles extended layer modifying.
369 * @entry: An allocated and populated pdscpath_entry struct.
370 * @buf: The input buffer to read from.
371 * @count: The number of bytes to be read.
373 * We will call this function to change the current layer value.
374 * Layers are to be given '.'-delimited, without brackets.
375 * XXX beware we are far less checky WRT input data provided than for hwpath.
376 * Potential harm can be done, since there's no way to check the validity of
380 pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
382 unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
386 if (!entry || !buf || !count)
389 /* We'll use a local copy of buf */
390 count = min_t(size_t, count, sizeof(in)-1);
391 strncpy(in, buf, count);
394 /* Let's clean up the target. 0 is a blank pattern */
395 memset(&layers, 0, sizeof(layers));
397 /* First, pick the first layer */
398 if (unlikely(!isdigit(*in)))
400 layers[0] = simple_strtoul(in, NULL, 10);
401 DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
404 for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
405 if (unlikely(!isdigit(*(++temp))))
407 layers[i] = simple_strtoul(temp, NULL, 10);
408 DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
411 /* So far so good, let's get in deep */
412 write_lock(&entry->rw_lock);
414 /* First, overwrite the current layers with the new ones, not touching
415 the hardware path. */
416 memcpy(&entry->devpath.layers, &layers, sizeof(layers));
418 /* Now, dive in. Write back to the hardware */
419 pdcspath_store(entry);
420 write_unlock(&entry->rw_lock);
422 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
429 * pdcspath_attr_show - Generic read function call wrapper.
430 * @kobj: The kobject to get info from.
431 * @attr: The attribute looked upon.
432 * @buf: The output buffer.
435 pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
437 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
438 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
442 ret = pdcs_attr->show(entry, buf);
448 * pdcspath_attr_store - Generic write function call wrapper.
449 * @kobj: The kobject to write info to.
450 * @attr: The attribute to be modified.
451 * @buf: The input buffer.
452 * @count: The size of the buffer.
455 pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
456 const char *buf, size_t count)
458 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
459 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
462 if (!capable(CAP_SYS_ADMIN))
465 if (pdcs_attr->store)
466 ret = pdcs_attr->store(entry, buf, count);
471 static const struct sysfs_ops pdcspath_attr_ops = {
472 .show = pdcspath_attr_show,
473 .store = pdcspath_attr_store,
476 /* These are the two attributes of any PDC path. */
477 static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
478 static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
480 static struct attribute *paths_subsys_attrs[] = {
481 &paths_attr_hwpath.attr,
482 &paths_attr_layer.attr,
485 ATTRIBUTE_GROUPS(paths_subsys);
487 /* Specific kobject type for our PDC paths */
488 static struct kobj_type ktype_pdcspath = {
489 .sysfs_ops = &pdcspath_attr_ops,
490 .default_groups = paths_subsys_groups,
493 /* We hard define the 4 types of path we expect to find */
494 static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
495 static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
496 static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
497 static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
499 /* An array containing all PDC paths we will deal with */
500 static struct pdcspath_entry *pdcspath_entries[] = {
501 &pdcspath_entry_primary,
502 &pdcspath_entry_alternative,
503 &pdcspath_entry_console,
504 &pdcspath_entry_keyboard,
509 /* For more insight of what's going on here, refer to PDC Procedures doc,
510 * Section PDC_STABLE */
513 * pdcs_size_read - Stable Storage size output.
514 * @buf: The output buffer to write to.
516 static ssize_t pdcs_size_read(struct kobject *kobj,
517 struct kobj_attribute *attr,
525 /* show the size of the stable storage */
526 out += sprintf(out, "%ld\n", pdcs_size);
532 * pdcs_auto_read - Stable Storage autoboot/search flag output.
533 * @buf: The output buffer to write to.
534 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
536 static ssize_t pdcs_auto_read(struct kobject *kobj,
537 struct kobj_attribute *attr,
541 struct pdcspath_entry *pathentry;
546 /* Current flags are stored in primary boot path entry */
547 pathentry = &pdcspath_entry_primary;
549 read_lock(&pathentry->rw_lock);
550 out += sprintf(out, "%s\n", (pathentry->devpath.path.flags & knob) ?
552 read_unlock(&pathentry->rw_lock);
558 * pdcs_autoboot_read - Stable Storage autoboot flag output.
559 * @buf: The output buffer to write to.
561 static ssize_t pdcs_autoboot_read(struct kobject *kobj,
562 struct kobj_attribute *attr, char *buf)
564 return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT);
568 * pdcs_autosearch_read - Stable Storage autoboot flag output.
569 * @buf: The output buffer to write to.
571 static ssize_t pdcs_autosearch_read(struct kobject *kobj,
572 struct kobj_attribute *attr, char *buf)
574 return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH);
578 * pdcs_timer_read - Stable Storage timer count output (in seconds).
579 * @buf: The output buffer to write to.
581 * The value of the timer field correponds to a number of seconds in powers of 2.
583 static ssize_t pdcs_timer_read(struct kobject *kobj,
584 struct kobj_attribute *attr, char *buf)
587 struct pdcspath_entry *pathentry;
592 /* Current flags are stored in primary boot path entry */
593 pathentry = &pdcspath_entry_primary;
595 /* print the timer value in seconds */
596 read_lock(&pathentry->rw_lock);
597 out += sprintf(out, "%u\n", (pathentry->devpath.path.flags & PF_TIMER) ?
598 (1 << (pathentry->devpath.path.flags & PF_TIMER)) : 0);
599 read_unlock(&pathentry->rw_lock);
605 * pdcs_osid_read - Stable Storage OS ID register output.
606 * @buf: The output buffer to write to.
608 static ssize_t pdcs_osid_read(struct kobject *kobj,
609 struct kobj_attribute *attr, char *buf)
616 out += sprintf(out, "%s dependent data (0x%.4x)\n",
617 os_id_to_string(pdcs_osid), pdcs_osid);
623 * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
624 * @buf: The output buffer to write to.
626 * This can hold 16 bytes of OS-Dependent data.
628 static ssize_t pdcs_osdep1_read(struct kobject *kobj,
629 struct kobj_attribute *attr, char *buf)
637 if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
640 out += sprintf(out, "0x%.8x\n", result[0]);
641 out += sprintf(out, "0x%.8x\n", result[1]);
642 out += sprintf(out, "0x%.8x\n", result[2]);
643 out += sprintf(out, "0x%.8x\n", result[3]);
649 * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
650 * @buf: The output buffer to write to.
652 * I have NFC how to interpret the content of that register ;-).
654 static ssize_t pdcs_diagnostic_read(struct kobject *kobj,
655 struct kobj_attribute *attr, char *buf)
664 if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
667 out += sprintf(out, "0x%.4x\n", (result >> 16));
673 * pdcs_fastsize_read - Stable Storage FastSize register output.
674 * @buf: The output buffer to write to.
676 * This register holds the amount of system RAM to be tested during boot sequence.
678 static ssize_t pdcs_fastsize_read(struct kobject *kobj,
679 struct kobj_attribute *attr, char *buf)
688 if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
691 if ((result & 0x0F) < 0x0E)
692 out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
694 out += sprintf(out, "All");
695 out += sprintf(out, "\n");
701 * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
702 * @buf: The output buffer to write to.
704 * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
706 static ssize_t pdcs_osdep2_read(struct kobject *kobj,
707 struct kobj_attribute *attr, char *buf)
714 if (unlikely(pdcs_size <= 224))
717 size = pdcs_size - 224;
722 for (i=0; i<size; i+=4) {
723 if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
724 sizeof(result)) != PDC_OK))
726 out += sprintf(out, "0x%.8x\n", result);
733 * pdcs_auto_write - This function handles autoboot/search flag modifying.
734 * @buf: The input buffer to read from.
735 * @count: The number of bytes to be read.
736 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
738 * We will call this function to change the current autoboot flag.
739 * We expect a precise syntax:
740 * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
742 static ssize_t pdcs_auto_write(struct kobject *kobj,
743 struct kobj_attribute *attr, const char *buf,
744 size_t count, int knob)
746 struct pdcspath_entry *pathentry;
751 if (!capable(CAP_SYS_ADMIN))
757 /* We'll use a local copy of buf */
758 count = min_t(size_t, count, sizeof(in)-1);
759 strncpy(in, buf, count);
762 /* Current flags are stored in primary boot path entry */
763 pathentry = &pdcspath_entry_primary;
765 /* Be nice to the existing flag record */
766 read_lock(&pathentry->rw_lock);
767 flags = pathentry->devpath.path.flags;
768 read_unlock(&pathentry->rw_lock);
770 DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
772 temp = skip_spaces(in);
775 if ((c != 0) && (c != 1))
782 DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
784 /* So far so good, let's get in deep */
785 write_lock(&pathentry->rw_lock);
787 /* Change the path entry flags first */
788 pathentry->devpath.path.flags = flags;
790 /* Now, dive in. Write back to the hardware */
791 pdcspath_store(pathentry);
792 write_unlock(&pathentry->rw_lock);
794 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
795 (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
796 (flags & knob) ? "On" : "Off");
801 printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
806 * pdcs_autoboot_write - This function handles autoboot flag modifying.
807 * @buf: The input buffer to read from.
808 * @count: The number of bytes to be read.
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
814 static ssize_t pdcs_autoboot_write(struct kobject *kobj,
815 struct kobj_attribute *attr,
816 const char *buf, size_t count)
818 return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT);
822 * pdcs_autosearch_write - This function handles autosearch flag modifying.
823 * @buf: The input buffer to read from.
824 * @count: The number of bytes to be read.
826 * We will call this function to change the current boot flags.
827 * We expect a precise syntax:
828 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
830 static ssize_t pdcs_autosearch_write(struct kobject *kobj,
831 struct kobj_attribute *attr,
832 const char *buf, size_t count)
834 return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH);
838 * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
839 * @buf: The input buffer to read from.
840 * @count: The number of bytes to be read.
842 * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
843 * write approach. It's up to userspace to deal with it when constructing
846 static ssize_t pdcs_osdep1_write(struct kobject *kobj,
847 struct kobj_attribute *attr,
848 const char *buf, size_t count)
852 if (!capable(CAP_SYS_ADMIN))
858 if (unlikely(pdcs_osid != OS_ID_LINUX))
864 /* We'll use a local copy of buf */
866 memcpy(in, buf, count);
868 if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
875 * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
876 * @buf: The input buffer to read from.
877 * @count: The number of bytes to be read.
879 * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
880 * byte-by-byte write approach. It's up to userspace to deal with it when
881 * constructing its input buffer.
883 static ssize_t pdcs_osdep2_write(struct kobject *kobj,
884 struct kobj_attribute *attr,
885 const char *buf, size_t count)
891 if (!capable(CAP_SYS_ADMIN))
897 if (unlikely(pdcs_size <= 224))
900 if (unlikely(pdcs_osid != OS_ID_LINUX))
903 size = pdcs_size - 224;
908 /* We'll use a local copy of buf */
910 for (i=0; i<count; i+=4) {
912 memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
913 if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
914 sizeof(in)) != PDC_OK))
921 /* The remaining attributes. */
922 static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
923 static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
924 static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
925 static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
926 static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
927 static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
928 static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
929 static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
930 static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
932 static struct attribute *pdcs_subsys_attrs[] = {
933 &pdcs_attr_size.attr,
934 &pdcs_attr_autoboot.attr,
935 &pdcs_attr_autosearch.attr,
936 &pdcs_attr_timer.attr,
937 &pdcs_attr_osid.attr,
938 &pdcs_attr_osdep1.attr,
939 &pdcs_attr_diagnostic.attr,
940 &pdcs_attr_fastsize.attr,
941 &pdcs_attr_osdep2.attr,
945 static const struct attribute_group pdcs_attr_group = {
946 .attrs = pdcs_subsys_attrs,
949 static struct kobject *stable_kobj;
950 static struct kset *paths_kset;
953 * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
955 * It creates kobjects corresponding to each path entry with nice sysfs
956 * links to the real device. This is where the magic takes place: when
957 * registering the subsystem attributes during module init, each kobject hereby
958 * created will show in the sysfs tree as a folder containing files as defined
959 * by path_subsys_attr[].
961 static inline int __init
962 pdcs_register_pathentries(void)
965 struct pdcspath_entry *entry;
968 /* Initialize the entries rw_lock before anything else */
969 for (i = 0; (entry = pdcspath_entries[i]); i++)
970 rwlock_init(&entry->rw_lock);
972 for (i = 0; (entry = pdcspath_entries[i]); i++) {
973 write_lock(&entry->rw_lock);
974 err = pdcspath_fetch(entry);
975 write_unlock(&entry->rw_lock);
980 entry->kobj.kset = paths_kset;
981 err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL,
984 kobject_put(&entry->kobj);
988 /* kobject is now registered */
989 write_lock(&entry->rw_lock);
991 write_unlock(&entry->rw_lock);
993 /* Add a nice symlink to the real device */
995 err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
999 kobject_uevent(&entry->kobj, KOBJ_ADD);
1006 * pdcs_unregister_pathentries - Routine called when unregistering the module.
1009 pdcs_unregister_pathentries(void)
1012 struct pdcspath_entry *entry;
1014 for (i = 0; (entry = pdcspath_entries[i]); i++) {
1015 read_lock(&entry->rw_lock);
1016 if (entry->ready >= 2)
1017 kobject_put(&entry->kobj);
1018 read_unlock(&entry->rw_lock);
1023 * For now we register the stable subsystem with the firmware subsystem
1024 * and the paths subsystem with the stable subsystem
1027 pdc_stable_init(void)
1029 int rc = 0, error = 0;
1032 /* find the size of the stable storage */
1033 if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
1036 /* make sure we have enough data */
1040 printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
1043 if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
1046 /* the actual result is 16 bits away */
1047 pdcs_osid = (u16)(result >> 16);
1049 /* For now we'll register the directory at /sys/firmware/stable */
1050 stable_kobj = kobject_create_and_add("stable", firmware_kobj);
1056 /* Don't forget the root entries */
1057 error = sysfs_create_group(stable_kobj, &pdcs_attr_group);
1059 /* register the paths kset as a child of the stable kset */
1060 paths_kset = kset_create_and_add("paths", NULL, stable_kobj);
1066 /* now we create all "files" for the paths kset */
1067 if ((rc = pdcs_register_pathentries()))
1073 pdcs_unregister_pathentries();
1074 kset_unregister(paths_kset);
1077 kobject_put(stable_kobj);
1080 printk(KERN_INFO PDCS_PREFIX " bailing out\n");
1085 pdc_stable_exit(void)
1087 pdcs_unregister_pathentries();
1088 kset_unregister(paths_kset);
1089 kobject_put(stable_kobj);
1093 module_init(pdc_stable_init);
1094 module_exit(pdc_stable_exit);