Merge branches 'pm-cpuidle', 'pm-core' and 'pm-sleep'
[linux-2.6-microblaze.git] / drivers / parisc / pdc_stable.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* 
3  *    Interfaces to retrieve and set PDC Stable options (firmware)
4  *
5  *    Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
6  *
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."
12  *
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.
20  *
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
27  *
28  *    The current policy wrt file permissions is:
29  *      - write: root only
30  *      - read: (reading triggers PDC calls) ? root only : everyone
31  *    The rationale is that PDC calls could hog (DoS) the machine.
32  *
33  *      TODO:
34  *      - timer/fastsize write calls
35  */
36
37 #undef PDCS_DEBUG
38 #ifdef PDCS_DEBUG
39 #define DPRINTK(fmt, args...)   printk(KERN_DEBUG fmt, ## args)
40 #else
41 #define DPRINTK(fmt, args...)
42 #endif
43
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>
55
56 #include <asm/pdc.h>
57 #include <asm/page.h>
58 #include <linux/uaccess.h>
59 #include <asm/hardware.h>
60
61 #define PDCS_VERSION    "0.30"
62 #define PDCS_PREFIX     "PDC Stable Storage"
63
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
73
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);
78
79 /* holds Stable Storage size. Initialized once and for all, no lock needed */
80 static unsigned long pdcs_size __read_mostly;
81
82 /* holds OS ID. Initialized once and for all, hopefully to 0x0006 */
83 static u16 pdcs_osid __read_mostly;
84
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 */
93         struct kobject kobj;
94 };
95
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);
100 };
101
102 #define PDCSPATH_ENTRY(_addr, _name) \
103 struct pdcspath_entry pdcspath_entry_##_name = { \
104         .ready = 0, \
105         .addr = _addr, \
106         .name = __stringify(_name), \
107 };
108
109 #define PDCS_ATTR(_name, _mode, _show, _store) \
110 struct kobj_attribute pdcs_attr_##_name = { \
111         .attr = {.name = __stringify(_name), .mode = _mode}, \
112         .show = _show, \
113         .store = _store, \
114 };
115
116 #define PATHS_ATTR(_name, _mode, _show, _store) \
117 struct pdcspath_attribute paths_attr_##_name = { \
118         .attr = {.name = __stringify(_name), .mode = _mode}, \
119         .show = _show, \
120         .store = _store, \
121 };
122
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)
125
126 /**
127  * pdcspath_fetch - This function populates the path entry structs.
128  * @entry: A pointer to an allocated pdcspath_entry.
129  * 
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.
135  *
136  * This function expects to be called with @entry->rw_lock write-hold.
137  */
138 static int
139 pdcspath_fetch(struct pdcspath_entry *entry)
140 {
141         struct pdc_module_path *devpath;
142
143         if (!entry)
144                 return -EINVAL;
145
146         devpath = &entry->devpath;
147         
148         DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
149                         entry, devpath, entry->addr);
150
151         /* addr, devpath and count must be word aligned */
152         if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
153                 return -EIO;
154                 
155         /* Find the matching device.
156            NOTE: hardware_path overlays with pdc_module_path, so the nice cast can
157            be used */
158         entry->dev = hwpath_to_device((struct hardware_path *)devpath);
159
160         entry->ready = 1;
161         
162         DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
163         
164         return 0;
165 }
166
167 /**
168  * pdcspath_store - This function writes a path to stable storage.
169  * @entry: A pointer to an allocated pdcspath_entry.
170  * 
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
176  *
177  * This function expects to be called with @entry->rw_lock write-hold.
178  */
179 static void
180 pdcspath_store(struct pdcspath_entry *entry)
181 {
182         struct pdc_module_path *devpath;
183
184         BUG_ON(!entry);
185
186         devpath = &entry->devpath;
187         
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... */
191         if (!entry->ready) {
192                 /* ...but we have a device, map it */
193                 BUG_ON(!entry->dev);
194                 device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
195         }
196         /* else, we expect the provided hwpath to be valid. */
197         
198         DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
199                         entry, devpath, entry->addr);
200
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__);
206                 
207         /* kobject is already registered */
208         entry->ready = 2;
209         
210         DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
211 }
212
213 /**
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.
217  * 
218  * We will call this function to format the output of the hwpath attribute file.
219  */
220 static ssize_t
221 pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
222 {
223         char *out = buf;
224         struct pdc_module_path *devpath;
225         short i;
226
227         if (!entry || !buf)
228                 return -EINVAL;
229
230         read_lock(&entry->rw_lock);
231         devpath = &entry->devpath;
232         i = entry->ready;
233         read_unlock(&entry->rw_lock);
234
235         if (!i) /* entry is not ready */
236                 return -ENODATA;
237         
238         for (i = 0; i < 6; i++) {
239                 if (devpath->path.bc[i] < 0)
240                         continue;
241                 out += sprintf(out, "%d/", devpath->path.bc[i]);
242         }
243         out += sprintf(out, "%u\n", (unsigned char)devpath->path.mod);
244         
245         return out - buf;
246 }
247
248 /**
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.
253  * 
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.
262  */
263 static ssize_t
264 pdcspath_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[64], *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         count = min_t(size_t, count, sizeof(in)-1);
277         strscpy(in, buf, count + 1);
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.path.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.path.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         write_unlock(&entry->rw_lock);
323
324         ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
325         WARN_ON(ret);
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  */
340 static ssize_t
341 pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
342 {
343         char *out = buf;
344         struct pdc_module_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
366 /**
367  * pdcspath_layer_write - This function handles extended layer modifying.
368  * @entry: An allocated and populated pdscpath_entry struct.
369  * @buf: The input buffer to read from.
370  * @count: The number of bytes to be read.
371  * 
372  * We will call this function to change the current layer value.
373  * Layers are to be given '.'-delimited, without brackets.
374  * XXX beware we are far less checky WRT input data provided than for hwpath.
375  * Potential harm can be done, since there's no way to check the validity of
376  * the layer fields.
377  */
378 static ssize_t
379 pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
380 {
381         unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
382         unsigned short i;
383         char in[64], *temp;
384
385         if (!entry || !buf || !count)
386                 return -EINVAL;
387
388         /* We'll use a local copy of buf */
389         count = min_t(size_t, count, sizeof(in)-1);
390         strscpy(in, buf, count + 1);
391         
392         /* Let's clean up the target. 0 is a blank pattern */
393         memset(&layers, 0, sizeof(layers));
394         
395         /* First, pick the first layer */
396         if (unlikely(!isdigit(*in)))
397                 return -EINVAL;
398         layers[0] = simple_strtoul(in, NULL, 10);
399         DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
400         
401         temp = in;
402         for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
403                 if (unlikely(!isdigit(*(++temp))))
404                         return -EINVAL;
405                 layers[i] = simple_strtoul(temp, NULL, 10);
406                 DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
407         }
408                 
409         /* So far so good, let's get in deep */
410         write_lock(&entry->rw_lock);
411         
412         /* First, overwrite the current layers with the new ones, not touching
413            the hardware path. */
414         memcpy(&entry->devpath.layers, &layers, sizeof(layers));
415         
416         /* Now, dive in. Write back to the hardware */
417         pdcspath_store(entry);
418         write_unlock(&entry->rw_lock);
419         
420         printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
421                 entry->name, buf);
422         
423         return count;
424 }
425
426 /**
427  * pdcspath_attr_show - Generic read function call wrapper.
428  * @kobj: The kobject to get info from.
429  * @attr: The attribute looked upon.
430  * @buf: The output buffer.
431  */
432 static ssize_t
433 pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
434 {
435         struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
436         struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
437         ssize_t ret = 0;
438
439         if (pdcs_attr->show)
440                 ret = pdcs_attr->show(entry, buf);
441
442         return ret;
443 }
444
445 /**
446  * pdcspath_attr_store - Generic write function call wrapper.
447  * @kobj: The kobject to write info to.
448  * @attr: The attribute to be modified.
449  * @buf: The input buffer.
450  * @count: The size of the buffer.
451  */
452 static ssize_t
453 pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
454                         const char *buf, size_t count)
455 {
456         struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
457         struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
458         ssize_t ret = 0;
459
460         if (!capable(CAP_SYS_ADMIN))
461                 return -EACCES;
462
463         if (pdcs_attr->store)
464                 ret = pdcs_attr->store(entry, buf, count);
465
466         return ret;
467 }
468
469 static const struct sysfs_ops pdcspath_attr_ops = {
470         .show = pdcspath_attr_show,
471         .store = pdcspath_attr_store,
472 };
473
474 /* These are the two attributes of any PDC path. */
475 static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
476 static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
477
478 static struct attribute *paths_subsys_attrs[] = {
479         &paths_attr_hwpath.attr,
480         &paths_attr_layer.attr,
481         NULL,
482 };
483 ATTRIBUTE_GROUPS(paths_subsys);
484
485 /* Specific kobject type for our PDC paths */
486 static struct kobj_type ktype_pdcspath = {
487         .sysfs_ops = &pdcspath_attr_ops,
488         .default_groups = paths_subsys_groups,
489 };
490
491 /* We hard define the 4 types of path we expect to find */
492 static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
493 static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
494 static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
495 static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
496
497 /* An array containing all PDC paths we will deal with */
498 static struct pdcspath_entry *pdcspath_entries[] = {
499         &pdcspath_entry_primary,
500         &pdcspath_entry_alternative,
501         &pdcspath_entry_console,
502         &pdcspath_entry_keyboard,
503         NULL,
504 };
505
506
507 /* For more insight of what's going on here, refer to PDC Procedures doc,
508  * Section PDC_STABLE */
509
510 /**
511  * pdcs_size_read - Stable Storage size output.
512  * @buf: The output buffer to write to.
513  */
514 static ssize_t pdcs_size_read(struct kobject *kobj,
515                               struct kobj_attribute *attr,
516                               char *buf)
517 {
518         char *out = buf;
519
520         if (!buf)
521                 return -EINVAL;
522
523         /* show the size of the stable storage */
524         out += sprintf(out, "%ld\n", pdcs_size);
525
526         return out - buf;
527 }
528
529 /**
530  * pdcs_auto_read - Stable Storage autoboot/search flag output.
531  * @buf: The output buffer to write to.
532  * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
533  */
534 static ssize_t pdcs_auto_read(struct kobject *kobj,
535                               struct kobj_attribute *attr,
536                               char *buf, int knob)
537 {
538         char *out = buf;
539         struct pdcspath_entry *pathentry;
540
541         if (!buf)
542                 return -EINVAL;
543
544         /* Current flags are stored in primary boot path entry */
545         pathentry = &pdcspath_entry_primary;
546
547         read_lock(&pathentry->rw_lock);
548         out += sprintf(out, "%s\n", (pathentry->devpath.path.flags & knob) ?
549                                         "On" : "Off");
550         read_unlock(&pathentry->rw_lock);
551
552         return out - buf;
553 }
554
555 /**
556  * pdcs_autoboot_read - Stable Storage autoboot flag output.
557  * @buf: The output buffer to write to.
558  */
559 static ssize_t pdcs_autoboot_read(struct kobject *kobj,
560                                   struct kobj_attribute *attr, char *buf)
561 {
562         return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT);
563 }
564
565 /**
566  * pdcs_autosearch_read - Stable Storage autoboot flag output.
567  * @buf: The output buffer to write to.
568  */
569 static ssize_t pdcs_autosearch_read(struct kobject *kobj,
570                                     struct kobj_attribute *attr, char *buf)
571 {
572         return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH);
573 }
574
575 /**
576  * pdcs_timer_read - Stable Storage timer count output (in seconds).
577  * @buf: The output buffer to write to.
578  *
579  * The value of the timer field correponds to a number of seconds in powers of 2.
580  */
581 static ssize_t pdcs_timer_read(struct kobject *kobj,
582                                struct kobj_attribute *attr, char *buf)
583 {
584         char *out = buf;
585         struct pdcspath_entry *pathentry;
586
587         if (!buf)
588                 return -EINVAL;
589
590         /* Current flags are stored in primary boot path entry */
591         pathentry = &pdcspath_entry_primary;
592
593         /* print the timer value in seconds */
594         read_lock(&pathentry->rw_lock);
595         out += sprintf(out, "%u\n", (pathentry->devpath.path.flags & PF_TIMER) ?
596                                 (1 << (pathentry->devpath.path.flags & PF_TIMER)) : 0);
597         read_unlock(&pathentry->rw_lock);
598
599         return out - buf;
600 }
601
602 /**
603  * pdcs_osid_read - Stable Storage OS ID register output.
604  * @buf: The output buffer to write to.
605  */
606 static ssize_t pdcs_osid_read(struct kobject *kobj,
607                               struct kobj_attribute *attr, char *buf)
608 {
609         char *out = buf;
610
611         if (!buf)
612                 return -EINVAL;
613
614         out += sprintf(out, "%s dependent data (0x%.4x)\n",
615                 os_id_to_string(pdcs_osid), pdcs_osid);
616
617         return out - buf;
618 }
619
620 /**
621  * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
622  * @buf: The output buffer to write to.
623  *
624  * This can hold 16 bytes of OS-Dependent data.
625  */
626 static ssize_t pdcs_osdep1_read(struct kobject *kobj,
627                                 struct kobj_attribute *attr, char *buf)
628 {
629         char *out = buf;
630         u32 result[4];
631
632         if (!buf)
633                 return -EINVAL;
634
635         if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
636                 return -EIO;
637
638         out += sprintf(out, "0x%.8x\n", result[0]);
639         out += sprintf(out, "0x%.8x\n", result[1]);
640         out += sprintf(out, "0x%.8x\n", result[2]);
641         out += sprintf(out, "0x%.8x\n", result[3]);
642
643         return out - buf;
644 }
645
646 /**
647  * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
648  * @buf: The output buffer to write to.
649  *
650  * I have NFC how to interpret the content of that register ;-).
651  */
652 static ssize_t pdcs_diagnostic_read(struct kobject *kobj,
653                                     struct kobj_attribute *attr, char *buf)
654 {
655         char *out = buf;
656         u32 result;
657
658         if (!buf)
659                 return -EINVAL;
660
661         /* get diagnostic */
662         if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
663                 return -EIO;
664
665         out += sprintf(out, "0x%.4x\n", (result >> 16));
666
667         return out - buf;
668 }
669
670 /**
671  * pdcs_fastsize_read - Stable Storage FastSize register output.
672  * @buf: The output buffer to write to.
673  *
674  * This register holds the amount of system RAM to be tested during boot sequence.
675  */
676 static ssize_t pdcs_fastsize_read(struct kobject *kobj,
677                                   struct kobj_attribute *attr, char *buf)
678 {
679         char *out = buf;
680         u32 result;
681
682         if (!buf)
683                 return -EINVAL;
684
685         /* get fast-size */
686         if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
687                 return -EIO;
688
689         if ((result & 0x0F) < 0x0E)
690                 out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
691         else
692                 out += sprintf(out, "All");
693         out += sprintf(out, "\n");
694         
695         return out - buf;
696 }
697
698 /**
699  * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
700  * @buf: The output buffer to write to.
701  *
702  * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
703  */
704 static ssize_t pdcs_osdep2_read(struct kobject *kobj,
705                                 struct kobj_attribute *attr, char *buf)
706 {
707         char *out = buf;
708         unsigned long size;
709         unsigned short i;
710         u32 result;
711
712         if (unlikely(pdcs_size <= 224))
713                 return -ENODATA;
714
715         size = pdcs_size - 224;
716
717         if (!buf)
718                 return -EINVAL;
719
720         for (i=0; i<size; i+=4) {
721                 if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
722                                         sizeof(result)) != PDC_OK))
723                         return -EIO;
724                 out += sprintf(out, "0x%.8x\n", result);
725         }
726
727         return out - buf;
728 }
729
730 /**
731  * pdcs_auto_write - This function handles autoboot/search flag modifying.
732  * @buf: The input buffer to read from.
733  * @count: The number of bytes to be read.
734  * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
735  * 
736  * We will call this function to change the current autoboot flag.
737  * We expect a precise syntax:
738  *      \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
739  */
740 static ssize_t pdcs_auto_write(struct kobject *kobj,
741                                struct kobj_attribute *attr, const char *buf,
742                                size_t count, int knob)
743 {
744         struct pdcspath_entry *pathentry;
745         unsigned char flags;
746         char in[8], *temp;
747         char c;
748
749         if (!capable(CAP_SYS_ADMIN))
750                 return -EACCES;
751
752         if (!buf || !count)
753                 return -EINVAL;
754
755         /* We'll use a local copy of buf */
756         count = min_t(size_t, count, sizeof(in)-1);
757         strscpy(in, buf, count + 1);
758
759         /* Current flags are stored in primary boot path entry */
760         pathentry = &pdcspath_entry_primary;
761         
762         /* Be nice to the existing flag record */
763         read_lock(&pathentry->rw_lock);
764         flags = pathentry->devpath.path.flags;
765         read_unlock(&pathentry->rw_lock);
766         
767         DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
768
769         temp = skip_spaces(in);
770
771         c = *temp++ - '0';
772         if ((c != 0) && (c != 1))
773                 goto parse_error;
774         if (c == 0)
775                 flags &= ~knob;
776         else
777                 flags |= knob;
778         
779         DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
780                 
781         /* So far so good, let's get in deep */
782         write_lock(&pathentry->rw_lock);
783         
784         /* Change the path entry flags first */
785         pathentry->devpath.path.flags = flags;
786                 
787         /* Now, dive in. Write back to the hardware */
788         pdcspath_store(pathentry);
789         write_unlock(&pathentry->rw_lock);
790         
791         printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
792                 (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
793                 (flags & knob) ? "On" : "Off");
794         
795         return count;
796
797 parse_error:
798         printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
799         return -EINVAL;
800 }
801
802 /**
803  * pdcs_autoboot_write - This function handles autoboot flag modifying.
804  * @buf: The input buffer to read from.
805  * @count: The number of bytes to be read.
806  *
807  * We will call this function to change the current boot flags.
808  * We expect a precise syntax:
809  *      \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
810  */
811 static ssize_t pdcs_autoboot_write(struct kobject *kobj,
812                                    struct kobj_attribute *attr,
813                                    const char *buf, size_t count)
814 {
815         return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT);
816 }
817
818 /**
819  * pdcs_autosearch_write - This function handles autosearch flag modifying.
820  * @buf: The input buffer to read from.
821  * @count: The number of bytes to be read.
822  *
823  * We will call this function to change the current boot flags.
824  * We expect a precise syntax:
825  *      \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
826  */
827 static ssize_t pdcs_autosearch_write(struct kobject *kobj,
828                                      struct kobj_attribute *attr,
829                                      const char *buf, size_t count)
830 {
831         return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH);
832 }
833
834 /**
835  * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
836  * @buf: The input buffer to read from.
837  * @count: The number of bytes to be read.
838  *
839  * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
840  * write approach. It's up to userspace to deal with it when constructing
841  * its input buffer.
842  */
843 static ssize_t pdcs_osdep1_write(struct kobject *kobj,
844                                  struct kobj_attribute *attr,
845                                  const char *buf, size_t count)
846 {
847         u8 in[16];
848
849         if (!capable(CAP_SYS_ADMIN))
850                 return -EACCES;
851
852         if (!buf || !count)
853                 return -EINVAL;
854
855         if (unlikely(pdcs_osid != OS_ID_LINUX))
856                 return -EPERM;
857
858         if (count > 16)
859                 return -EMSGSIZE;
860
861         /* We'll use a local copy of buf */
862         memset(in, 0, 16);
863         memcpy(in, buf, count);
864
865         if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
866                 return -EIO;
867
868         return count;
869 }
870
871 /**
872  * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
873  * @buf: The input buffer to read from.
874  * @count: The number of bytes to be read.
875  *
876  * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
877  * byte-by-byte write approach. It's up to userspace to deal with it when
878  * constructing its input buffer.
879  */
880 static ssize_t pdcs_osdep2_write(struct kobject *kobj,
881                                  struct kobj_attribute *attr,
882                                  const char *buf, size_t count)
883 {
884         unsigned long size;
885         unsigned short i;
886         u8 in[4];
887
888         if (!capable(CAP_SYS_ADMIN))
889                 return -EACCES;
890
891         if (!buf || !count)
892                 return -EINVAL;
893
894         if (unlikely(pdcs_size <= 224))
895                 return -ENOSYS;
896
897         if (unlikely(pdcs_osid != OS_ID_LINUX))
898                 return -EPERM;
899
900         size = pdcs_size - 224;
901
902         if (count > size)
903                 return -EMSGSIZE;
904
905         /* We'll use a local copy of buf */
906
907         for (i=0; i<count; i+=4) {
908                 memset(in, 0, 4);
909                 memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
910                 if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
911                                         sizeof(in)) != PDC_OK))
912                         return -EIO;
913         }
914
915         return count;
916 }
917
918 /* The remaining attributes. */
919 static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
920 static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
921 static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
922 static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
923 static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
924 static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
925 static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
926 static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
927 static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
928
929 static struct attribute *pdcs_subsys_attrs[] = {
930         &pdcs_attr_size.attr,
931         &pdcs_attr_autoboot.attr,
932         &pdcs_attr_autosearch.attr,
933         &pdcs_attr_timer.attr,
934         &pdcs_attr_osid.attr,
935         &pdcs_attr_osdep1.attr,
936         &pdcs_attr_diagnostic.attr,
937         &pdcs_attr_fastsize.attr,
938         &pdcs_attr_osdep2.attr,
939         NULL,
940 };
941
942 static const struct attribute_group pdcs_attr_group = {
943         .attrs = pdcs_subsys_attrs,
944 };
945
946 static struct kobject *stable_kobj;
947 static struct kset *paths_kset;
948
949 /**
950  * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
951  * 
952  * It creates kobjects corresponding to each path entry with nice sysfs
953  * links to the real device. This is where the magic takes place: when
954  * registering the subsystem attributes during module init, each kobject hereby
955  * created will show in the sysfs tree as a folder containing files as defined
956  * by path_subsys_attr[].
957  */
958 static inline int __init
959 pdcs_register_pathentries(void)
960 {
961         unsigned short i;
962         struct pdcspath_entry *entry;
963         int err;
964         
965         /* Initialize the entries rw_lock before anything else */
966         for (i = 0; (entry = pdcspath_entries[i]); i++)
967                 rwlock_init(&entry->rw_lock);
968
969         for (i = 0; (entry = pdcspath_entries[i]); i++) {
970                 write_lock(&entry->rw_lock);
971                 err = pdcspath_fetch(entry);
972                 write_unlock(&entry->rw_lock);
973
974                 if (err < 0)
975                         continue;
976
977                 entry->kobj.kset = paths_kset;
978                 err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL,
979                                            "%s", entry->name);
980                 if (err) {
981                         kobject_put(&entry->kobj);
982                         return err;
983                 }
984
985                 /* kobject is now registered */
986                 write_lock(&entry->rw_lock);
987                 entry->ready = 2;
988                 write_unlock(&entry->rw_lock);
989                 
990                 /* Add a nice symlink to the real device */
991                 if (entry->dev) {
992                         err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
993                         WARN_ON(err);
994                 }
995
996                 kobject_uevent(&entry->kobj, KOBJ_ADD);
997         }
998         
999         return 0;
1000 }
1001
1002 /**
1003  * pdcs_unregister_pathentries - Routine called when unregistering the module.
1004  */
1005 static inline void
1006 pdcs_unregister_pathentries(void)
1007 {
1008         unsigned short i;
1009         struct pdcspath_entry *entry;
1010         
1011         for (i = 0; (entry = pdcspath_entries[i]); i++) {
1012                 read_lock(&entry->rw_lock);
1013                 if (entry->ready >= 2)
1014                         kobject_put(&entry->kobj);
1015                 read_unlock(&entry->rw_lock);
1016         }
1017 }
1018
1019 /*
1020  * For now we register the stable subsystem with the firmware subsystem
1021  * and the paths subsystem with the stable subsystem
1022  */
1023 static int __init
1024 pdc_stable_init(void)
1025 {
1026         int rc = 0, error = 0;
1027         u32 result;
1028
1029         /* find the size of the stable storage */
1030         if (pdc_stable_get_size(&pdcs_size) != PDC_OK) 
1031                 return -ENODEV;
1032
1033         /* make sure we have enough data */
1034         if (pdcs_size < 96)
1035                 return -ENODATA;
1036
1037         printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
1038
1039         /* get OSID */
1040         if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
1041                 return -EIO;
1042
1043         /* the actual result is 16 bits away */
1044         pdcs_osid = (u16)(result >> 16);
1045
1046         /* For now we'll register the directory at /sys/firmware/stable */
1047         stable_kobj = kobject_create_and_add("stable", firmware_kobj);
1048         if (!stable_kobj) {
1049                 rc = -ENOMEM;
1050                 goto fail_firmreg;
1051         }
1052
1053         /* Don't forget the root entries */
1054         error = sysfs_create_group(stable_kobj, &pdcs_attr_group);
1055
1056         /* register the paths kset as a child of the stable kset */
1057         paths_kset = kset_create_and_add("paths", NULL, stable_kobj);
1058         if (!paths_kset) {
1059                 rc = -ENOMEM;
1060                 goto fail_ksetreg;
1061         }
1062
1063         /* now we create all "files" for the paths kset */
1064         if ((rc = pdcs_register_pathentries()))
1065                 goto fail_pdcsreg;
1066
1067         return rc;
1068         
1069 fail_pdcsreg:
1070         pdcs_unregister_pathentries();
1071         kset_unregister(paths_kset);
1072         
1073 fail_ksetreg:
1074         kobject_put(stable_kobj);
1075         
1076 fail_firmreg:
1077         printk(KERN_INFO PDCS_PREFIX " bailing out\n");
1078         return rc;
1079 }
1080
1081 static void __exit
1082 pdc_stable_exit(void)
1083 {
1084         pdcs_unregister_pathentries();
1085         kset_unregister(paths_kset);
1086         kobject_put(stable_kobj);
1087 }
1088
1089
1090 module_init(pdc_stable_init);
1091 module_exit(pdc_stable_exit);