blob: e4b73c2a053af704c54e88cfc6a1a64b9b799ce2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Interfaces to retrieve and set PDC Stable options (firmware)
3 *
Thibaut VARENEc7428422006-01-11 13:59:53 -07004 * Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or modify
Thibaut VARENEa81dd182006-02-03 18:06:30 -07007 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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
Thibaut VARENEc7428422006-01-11 13:59:53 -070028 * following code can deal with just 96 bytes of Stable Storage, and all
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * 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.
Thibaut Varene3f9edb52006-05-04 18:43:34 -060031 * 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
Thibaut VARENEc7428422006-01-11 13:59:53 -070040 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/kernel.h>
60#include <linux/string.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080061#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/ctype.h>
63#include <linux/sysfs.h>
64#include <linux/kobject.h>
65#include <linux/device.h>
66#include <linux/errno.h>
Thibaut VARENEc7428422006-01-11 13:59:53 -070067#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#include <asm/pdc.h>
70#include <asm/page.h>
71#include <asm/uaccess.h>
72#include <asm/hardware.h>
73
Thibaut Varene3f9edb52006-05-04 18:43:34 -060074#define PDCS_VERSION "0.30"
Thibaut VARENEc7428422006-01-11 13:59:53 -070075#define PDCS_PREFIX "PDC Stable Storage"
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#define PDCS_ADDR_PPRI 0x00
78#define PDCS_ADDR_OSID 0x40
Thibaut Varene3f9edb52006-05-04 18:43:34 -060079#define PDCS_ADDR_OSD1 0x48
80#define PDCS_ADDR_DIAG 0x58
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define PDCS_ADDR_FSIZ 0x5C
82#define PDCS_ADDR_PCON 0x60
83#define PDCS_ADDR_PALT 0x80
84#define PDCS_ADDR_PKBD 0xA0
Thibaut Varene3f9edb52006-05-04 18:43:34 -060085#define PDCS_ADDR_OSD2 0xE0
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
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
Thibaut VARENEc7428422006-01-11 13:59:53 -070092/* holds Stable Storage size. Initialized once and for all, no lock needed */
Helge Deller8039de12006-01-10 20:35:03 -050093static unsigned long pdcs_size __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Thibaut Varene3f9edb52006-05-04 18:43:34 -060095/* holds OS ID. Initialized once and for all, hopefully to 0x0006 */
96static u16 pdcs_osid __read_mostly;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* This struct defines what we need to deal with a parisc pdc path entry */
99struct pdcspath_entry {
Thibaut VARENEc7428422006-01-11 13:59:53 -0700100 rwlock_t rw_lock; /* to protect path entry access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 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) \
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700123struct kobj_attribute pdcs_attr_##_name = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900124 .attr = {.name = __stringify(_name), .mode = _mode}, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .show = _show, \
126 .store = _store, \
127};
128
129#define PATHS_ATTR(_name, _mode, _show, _store) \
130struct pdcspath_attribute paths_attr_##_name = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900131 .attr = {.name = __stringify(_name), .mode = _mode}, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300144 * you access the files provided by the facilities. We store a copy of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * 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.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700148 *
149 * This function expects to be called with @entry->rw_lock write-hold.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 */
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
Thibaut VARENEc7428422006-01-11 13:59:53 -0700189 *
190 * This function expects to be called with @entry->rw_lock write-hold.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700192static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193pdcspath_store(struct pdcspath_entry *entry)
194{
195 struct device_path *devpath;
196
Thibaut VARENEc7428422006-01-11 13:59:53 -0700197 BUG_ON(!entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
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 */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700206 BUG_ON(!entry->dev);
207 device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
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 */
Julia Lawall93c3e912012-11-03 10:58:36 +0000215 if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
216 WARN(1, KERN_ERR "%s: an error occurred when writing to PDC.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 "It is likely that the Stable Storage data has been corrupted.\n"
218 "Please check it carefully upon next reboot.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500220 /* kobject is already registered */
221 entry->ready = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/**
227 * pdcspath_hwpath_read - This function handles hardware path pretty printing.
228 * @entry: An allocated and populated pdscpath_entry struct.
229 * @buf: The output buffer to write to.
230 *
231 * We will call this function to format the output of the hwpath attribute file.
232 */
233static ssize_t
234pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
235{
236 char *out = buf;
237 struct device_path *devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700238 short i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (!entry || !buf)
241 return -EINVAL;
242
Thibaut VARENEc7428422006-01-11 13:59:53 -0700243 read_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 devpath = &entry->devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700245 i = entry->ready;
246 read_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Thibaut VARENEc7428422006-01-11 13:59:53 -0700248 if (!i) /* entry is not ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -ENODATA;
250
251 for (i = 0; i < 6; i++) {
252 if (devpath->bc[i] >= 128)
253 continue;
254 out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
255 }
256 out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
257
258 return out - buf;
259}
260
261/**
262 * pdcspath_hwpath_write - This function handles hardware path modifying.
263 * @entry: An allocated and populated pdscpath_entry struct.
264 * @buf: The input buffer to read from.
265 * @count: The number of bytes to be read.
266 *
267 * We will call this function to change the current hardware path.
268 * Hardware paths are to be given '/'-delimited, without brackets.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700269 * We make sure that the provided path actually maps to an existing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * device, BUT nothing would prevent some foolish user to set the path to some
271 * PCI bridge or even a CPU...
272 * A better work around would be to make sure we are at the end of a device tree
273 * for instance, but it would be IMHO beyond the simple scope of that driver.
274 * The aim is to provide a facility. Data correctness is left to userland.
275 */
276static ssize_t
277pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
278{
279 struct hardware_path hwpath;
280 unsigned short i;
281 char in[count+1], *temp;
282 struct device *dev;
Kyle McMartin26f03242007-10-18 00:04:06 -0700283 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (!entry || !buf || !count)
286 return -EINVAL;
287
288 /* We'll use a local copy of buf */
289 memset(in, 0, count+1);
290 strncpy(in, buf, count);
291
292 /* Let's clean up the target. 0xff is a blank pattern */
293 memset(&hwpath, 0xff, sizeof(hwpath));
294
295 /* First, pick the mod field (the last one of the input string) */
296 if (!(temp = strrchr(in, '/')))
297 return -EINVAL;
298
299 hwpath.mod = simple_strtoul(temp+1, NULL, 10);
300 in[temp-in] = '\0'; /* truncate the remaining string. just precaution */
301 DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
302
303 /* Then, loop for each delimiter, making sure we don't have too many.
304 we write the bc fields in a down-top way. No matter what, we stop
305 before writing the last field. If there are too many fields anyway,
306 then the user is a moron and it'll be caught up later when we'll
307 check the consistency of the given hwpath. */
308 for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
309 hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
310 in[temp-in] = '\0';
311 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
312 }
313
314 /* Store the final field */
315 hwpath.bc[i] = simple_strtoul(in, NULL, 10);
316 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
317
318 /* Now we check that the user isn't trying to lure us */
319 if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
320 printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
321 "hardware path: %s\n", __func__, entry->name, buf);
322 return -EINVAL;
323 }
324
325 /* So far so good, let's get in deep */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700326 write_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 entry->ready = 0;
328 entry->dev = dev;
329
330 /* Now, dive in. Write back to the hardware */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700331 pdcspath_store(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Update the symlink to the real device */
334 sysfs_remove_link(&entry->kobj, "device");
Kyle McMartin26f03242007-10-18 00:04:06 -0700335 ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
336 WARN_ON(ret);
337
Thibaut VARENEc7428422006-01-11 13:59:53 -0700338 write_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Thibaut VARENEc7428422006-01-11 13:59:53 -0700340 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 entry->name, buf);
342
343 return count;
344}
345
346/**
347 * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
348 * @entry: An allocated and populated pdscpath_entry struct.
349 * @buf: The output buffer to write to.
350 *
351 * We will call this function to format the output of the layer attribute file.
352 */
353static ssize_t
354pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
355{
356 char *out = buf;
357 struct device_path *devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700358 short i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (!entry || !buf)
361 return -EINVAL;
362
Thibaut VARENEc7428422006-01-11 13:59:53 -0700363 read_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 devpath = &entry->devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700365 i = entry->ready;
366 read_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Thibaut VARENEc7428422006-01-11 13:59:53 -0700368 if (!i) /* entry is not ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -ENODATA;
370
Roel Kluin447c2332009-08-02 08:02:28 +0000371 for (i = 0; i < 6 && devpath->layers[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 out += sprintf(out, "%u ", devpath->layers[i]);
373
374 out += sprintf(out, "\n");
375
376 return out - buf;
377}
378
379/**
380 * pdcspath_layer_write - This function handles extended layer modifying.
381 * @entry: An allocated and populated pdscpath_entry struct.
382 * @buf: The input buffer to read from.
383 * @count: The number of bytes to be read.
384 *
385 * We will call this function to change the current layer value.
386 * Layers are to be given '.'-delimited, without brackets.
387 * XXX beware we are far less checky WRT input data provided than for hwpath.
388 * Potential harm can be done, since there's no way to check the validity of
389 * the layer fields.
390 */
391static ssize_t
392pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
393{
394 unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
395 unsigned short i;
396 char in[count+1], *temp;
397
398 if (!entry || !buf || !count)
399 return -EINVAL;
400
401 /* We'll use a local copy of buf */
402 memset(in, 0, count+1);
403 strncpy(in, buf, count);
404
405 /* Let's clean up the target. 0 is a blank pattern */
406 memset(&layers, 0, sizeof(layers));
407
408 /* First, pick the first layer */
409 if (unlikely(!isdigit(*in)))
410 return -EINVAL;
411 layers[0] = simple_strtoul(in, NULL, 10);
412 DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
413
414 temp = in;
415 for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
416 if (unlikely(!isdigit(*(++temp))))
417 return -EINVAL;
418 layers[i] = simple_strtoul(temp, NULL, 10);
419 DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
420 }
421
422 /* So far so good, let's get in deep */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700423 write_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* First, overwrite the current layers with the new ones, not touching
426 the hardware path. */
427 memcpy(&entry->devpath.layers, &layers, sizeof(layers));
428
429 /* Now, dive in. Write back to the hardware */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700430 pdcspath_store(entry);
431 write_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Thibaut VARENEc7428422006-01-11 13:59:53 -0700433 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 entry->name, buf);
435
436 return count;
437}
438
439/**
440 * pdcspath_attr_show - Generic read function call wrapper.
441 * @kobj: The kobject to get info from.
442 * @attr: The attribute looked upon.
443 * @buf: The output buffer.
444 */
445static ssize_t
446pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
447{
448 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
449 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
450 ssize_t ret = 0;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (pdcs_attr->show)
453 ret = pdcs_attr->show(entry, buf);
454
455 return ret;
456}
457
458/**
459 * pdcspath_attr_store - Generic write function call wrapper.
460 * @kobj: The kobject to write info to.
461 * @attr: The attribute to be modified.
462 * @buf: The input buffer.
463 * @count: The size of the buffer.
464 */
465static ssize_t
466pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
467 const char *buf, size_t count)
468{
469 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
470 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
471 ssize_t ret = 0;
472
473 if (!capable(CAP_SYS_ADMIN))
474 return -EACCES;
475
476 if (pdcs_attr->store)
477 ret = pdcs_attr->store(entry, buf, count);
478
479 return ret;
480}
481
Emese Revfy52cf25d2010-01-19 02:58:23 +0100482static const struct sysfs_ops pdcspath_attr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 .show = pdcspath_attr_show,
484 .store = pdcspath_attr_store,
485};
486
487/* These are the two attributes of any PDC path. */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700488static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
489static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491static struct attribute *paths_subsys_attrs[] = {
492 &paths_attr_hwpath.attr,
493 &paths_attr_layer.attr,
494 NULL,
495};
496
497/* Specific kobject type for our PDC paths */
498static struct kobj_type ktype_pdcspath = {
499 .sysfs_ops = &pdcspath_attr_ops,
500 .default_attrs = paths_subsys_attrs,
501};
502
503/* We hard define the 4 types of path we expect to find */
504static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
505static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
506static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
507static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
508
509/* An array containing all PDC paths we will deal with */
510static struct pdcspath_entry *pdcspath_entries[] = {
511 &pdcspath_entry_primary,
512 &pdcspath_entry_alternative,
513 &pdcspath_entry_console,
514 &pdcspath_entry_keyboard,
515 NULL,
516};
517
Thibaut VARENEc7428422006-01-11 13:59:53 -0700518
519/* For more insight of what's going on here, refer to PDC Procedures doc,
520 * Section PDC_STABLE */
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/**
Thibaut VARENEc7428422006-01-11 13:59:53 -0700523 * pdcs_size_read - Stable Storage size output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * @buf: The output buffer to write to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700526static ssize_t pdcs_size_read(struct kobject *kobj,
527 struct kobj_attribute *attr,
528 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 char *out = buf;
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700531
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700532 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return -EINVAL;
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* show the size of the stable storage */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700536 out += sprintf(out, "%ld\n", pdcs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Thibaut VARENEc7428422006-01-11 13:59:53 -0700538 return out - buf;
539}
540
541/**
542 * pdcs_auto_read - Stable Storage autoboot/search flag output.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700543 * @buf: The output buffer to write to.
544 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
545 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700546static ssize_t pdcs_auto_read(struct kobject *kobj,
547 struct kobj_attribute *attr,
548 char *buf, int knob)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700549{
550 char *out = buf;
551 struct pdcspath_entry *pathentry;
Helge Deller67a5a592006-03-27 19:52:14 +0000552
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700553 if (!buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700554 return -EINVAL;
555
556 /* Current flags are stored in primary boot path entry */
557 pathentry = &pdcspath_entry_primary;
558
559 read_lock(&pathentry->rw_lock);
560 out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
561 "On" : "Off");
562 read_unlock(&pathentry->rw_lock);
563
564 return out - buf;
565}
566
567/**
568 * pdcs_autoboot_read - Stable Storage autoboot flag output.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700569 * @buf: The output buffer to write to.
570 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700571static ssize_t pdcs_autoboot_read(struct kobject *kobj,
572 struct kobj_attribute *attr, char *buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700573{
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700574 return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700575}
576
577/**
578 * pdcs_autosearch_read - Stable Storage autoboot flag output.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700579 * @buf: The output buffer to write to.
580 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700581static ssize_t pdcs_autosearch_read(struct kobject *kobj,
582 struct kobj_attribute *attr, char *buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700583{
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700584 return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700585}
586
587/**
588 * pdcs_timer_read - Stable Storage timer count output (in seconds).
Thibaut VARENEc7428422006-01-11 13:59:53 -0700589 * @buf: The output buffer to write to.
590 *
591 * The value of the timer field correponds to a number of seconds in powers of 2.
592 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700593static ssize_t pdcs_timer_read(struct kobject *kobj,
594 struct kobj_attribute *attr, char *buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700595{
596 char *out = buf;
597 struct pdcspath_entry *pathentry;
598
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700599 if (!buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700600 return -EINVAL;
601
602 /* Current flags are stored in primary boot path entry */
603 pathentry = &pdcspath_entry_primary;
604
605 /* print the timer value in seconds */
606 read_lock(&pathentry->rw_lock);
607 out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
608 (1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
609 read_unlock(&pathentry->rw_lock);
610
611 return out - buf;
612}
613
614/**
615 * pdcs_osid_read - Stable Storage OS ID register output.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700616 * @buf: The output buffer to write to.
617 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700618static ssize_t pdcs_osid_read(struct kobject *kobj,
619 struct kobj_attribute *attr, char *buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700620{
621 char *out = buf;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700622
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700623 if (!buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700624 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Kyle McMartin67a061a2006-06-25 16:58:57 +0000626 out += sprintf(out, "%s dependent data (0x%.4x)\n",
627 os_id_to_string(pdcs_osid), pdcs_osid);
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600628
629 return out - buf;
630}
631
632/**
633 * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600634 * @buf: The output buffer to write to.
635 *
636 * This can hold 16 bytes of OS-Dependent data.
637 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700638static ssize_t pdcs_osdep1_read(struct kobject *kobj,
639 struct kobj_attribute *attr, char *buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600640{
641 char *out = buf;
642 u32 result[4];
643
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700644 if (!buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600645 return -EINVAL;
646
647 if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
648 return -EIO;
649
650 out += sprintf(out, "0x%.8x\n", result[0]);
651 out += sprintf(out, "0x%.8x\n", result[1]);
652 out += sprintf(out, "0x%.8x\n", result[2]);
653 out += sprintf(out, "0x%.8x\n", result[3]);
654
655 return out - buf;
656}
657
658/**
659 * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600660 * @buf: The output buffer to write to.
661 *
662 * I have NFC how to interpret the content of that register ;-).
663 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700664static ssize_t pdcs_diagnostic_read(struct kobject *kobj,
665 struct kobj_attribute *attr, char *buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600666{
667 char *out = buf;
668 u32 result;
669
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700670 if (!buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600671 return -EINVAL;
672
673 /* get diagnostic */
674 if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
675 return -EIO;
676
677 out += sprintf(out, "0x%.4x\n", (result >> 16));
Thibaut VARENEc7428422006-01-11 13:59:53 -0700678
679 return out - buf;
680}
681
682/**
683 * pdcs_fastsize_read - Stable Storage FastSize register output.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700684 * @buf: The output buffer to write to.
685 *
686 * This register holds the amount of system RAM to be tested during boot sequence.
687 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700688static ssize_t pdcs_fastsize_read(struct kobject *kobj,
689 struct kobj_attribute *attr, char *buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700690{
691 char *out = buf;
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600692 u32 result;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700693
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700694 if (!buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700695 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* get fast-size */
698 if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
699 return -EIO;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if ((result & 0x0F) < 0x0E)
Randolph Chungabff7542005-10-21 22:57:13 -0400702 out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 else
704 out += sprintf(out, "All");
705 out += sprintf(out, "\n");
706
707 return out - buf;
708}
709
710/**
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600711 * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600712 * @buf: The output buffer to write to.
713 *
714 * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
715 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700716static ssize_t pdcs_osdep2_read(struct kobject *kobj,
717 struct kobj_attribute *attr, char *buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600718{
719 char *out = buf;
720 unsigned long size;
721 unsigned short i;
722 u32 result;
723
724 if (unlikely(pdcs_size <= 224))
725 return -ENODATA;
726
727 size = pdcs_size - 224;
728
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700729 if (!buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600730 return -EINVAL;
731
732 for (i=0; i<size; i+=4) {
733 if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
734 sizeof(result)) != PDC_OK))
735 return -EIO;
736 out += sprintf(out, "0x%.8x\n", result);
737 }
738
739 return out - buf;
740}
741
742/**
Thibaut VARENEc7428422006-01-11 13:59:53 -0700743 * pdcs_auto_write - This function handles autoboot/search flag modifying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * @buf: The input buffer to read from.
745 * @count: The number of bytes to be read.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700746 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 *
Thibaut VARENEc7428422006-01-11 13:59:53 -0700748 * We will call this function to change the current autoboot flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * We expect a precise syntax:
Thibaut VARENEc7428422006-01-11 13:59:53 -0700750 * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700752static ssize_t pdcs_auto_write(struct kobject *kobj,
753 struct kobj_attribute *attr, const char *buf,
754 size_t count, int knob)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct pdcspath_entry *pathentry;
757 unsigned char flags;
Rickard Strandqvist94c457d2014-09-14 18:02:12 +0200758 char in[8], *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 char c;
760
761 if (!capable(CAP_SYS_ADMIN))
762 return -EACCES;
763
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700764 if (!buf || !count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return -EINVAL;
766
767 /* We'll use a local copy of buf */
Rickard Strandqvist94c457d2014-09-14 18:02:12 +0200768 count = min_t(size_t, count, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 strncpy(in, buf, count);
Rickard Strandqvist94c457d2014-09-14 18:02:12 +0200770 in[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /* Current flags are stored in primary boot path entry */
773 pathentry = &pdcspath_entry_primary;
774
775 /* Be nice to the existing flag record */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700776 read_lock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 flags = pathentry->devpath.flags;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700778 read_unlock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
André Goddard Rosae7d28602009-12-14 18:01:06 -0800781
782 temp = skip_spaces(in);
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 c = *temp++ - '0';
785 if ((c != 0) && (c != 1))
786 goto parse_error;
787 if (c == 0)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700788 flags &= ~knob;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 else
Thibaut VARENEc7428422006-01-11 13:59:53 -0700790 flags |= knob;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
793
794 /* So far so good, let's get in deep */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700795 write_lock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* Change the path entry flags first */
798 pathentry->devpath.flags = flags;
799
800 /* Now, dive in. Write back to the hardware */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700801 pdcspath_store(pathentry);
802 write_unlock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Thibaut VARENEc7428422006-01-11 13:59:53 -0700804 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
805 (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
806 (flags & knob) ? "On" : "Off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 return count;
809
810parse_error:
Thibaut VARENEc7428422006-01-11 13:59:53 -0700811 printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -EINVAL;
813}
814
Thibaut VARENEc7428422006-01-11 13:59:53 -0700815/**
816 * pdcs_autoboot_write - This function handles autoboot flag modifying.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700817 * @buf: The input buffer to read from.
818 * @count: The number of bytes to be read.
819 *
820 * We will call this function to change the current boot flags.
821 * We expect a precise syntax:
822 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
823 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700824static ssize_t pdcs_autoboot_write(struct kobject *kobj,
825 struct kobj_attribute *attr,
826 const char *buf, size_t count)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700827{
Joel Soeteff451d72008-02-18 18:26:11 -0800828 return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700829}
830
831/**
832 * pdcs_autosearch_write - This function handles autosearch flag modifying.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700833 * @buf: The input buffer to read from.
834 * @count: The number of bytes to be read.
835 *
836 * We will call this function to change the current boot flags.
837 * We expect a precise syntax:
838 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
839 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700840static ssize_t pdcs_autosearch_write(struct kobject *kobj,
841 struct kobj_attribute *attr,
842 const char *buf, size_t count)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700843{
Joel Soeteff451d72008-02-18 18:26:11 -0800844 return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700845}
846
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600847/**
848 * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600849 * @buf: The input buffer to read from.
850 * @count: The number of bytes to be read.
851 *
852 * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
853 * write approach. It's up to userspace to deal with it when constructing
854 * its input buffer.
855 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700856static ssize_t pdcs_osdep1_write(struct kobject *kobj,
857 struct kobj_attribute *attr,
858 const char *buf, size_t count)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600859{
860 u8 in[16];
861
862 if (!capable(CAP_SYS_ADMIN))
863 return -EACCES;
864
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700865 if (!buf || !count)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600866 return -EINVAL;
867
Kyle McMartinec1fdc22006-06-21 19:27:29 +0000868 if (unlikely(pdcs_osid != OS_ID_LINUX))
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600869 return -EPERM;
870
871 if (count > 16)
872 return -EMSGSIZE;
873
874 /* We'll use a local copy of buf */
875 memset(in, 0, 16);
876 memcpy(in, buf, count);
877
878 if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
879 return -EIO;
880
881 return count;
882}
883
884/**
885 * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600886 * @buf: The input buffer to read from.
887 * @count: The number of bytes to be read.
888 *
889 * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
890 * byte-by-byte write approach. It's up to userspace to deal with it when
891 * constructing its input buffer.
892 */
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700893static ssize_t pdcs_osdep2_write(struct kobject *kobj,
894 struct kobj_attribute *attr,
895 const char *buf, size_t count)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600896{
897 unsigned long size;
898 unsigned short i;
899 u8 in[4];
900
901 if (!capable(CAP_SYS_ADMIN))
902 return -EACCES;
903
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700904 if (!buf || !count)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600905 return -EINVAL;
906
907 if (unlikely(pdcs_size <= 224))
908 return -ENOSYS;
909
Kyle McMartinec1fdc22006-06-21 19:27:29 +0000910 if (unlikely(pdcs_osid != OS_ID_LINUX))
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600911 return -EPERM;
912
913 size = pdcs_size - 224;
914
915 if (count > size)
916 return -EMSGSIZE;
917
918 /* We'll use a local copy of buf */
919
920 for (i=0; i<count; i+=4) {
921 memset(in, 0, 4);
922 memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
923 if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
924 sizeof(in)) != PDC_OK))
925 return -EIO;
926 }
927
928 return count;
929}
930
Thibaut VARENEc7428422006-01-11 13:59:53 -0700931/* The remaining attributes. */
932static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
933static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
934static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
935static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600936static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
937static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
938static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700939static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600940static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700942static struct attribute *pdcs_subsys_attrs[] = {
943 &pdcs_attr_size.attr,
944 &pdcs_attr_autoboot.attr,
945 &pdcs_attr_autosearch.attr,
946 &pdcs_attr_timer.attr,
947 &pdcs_attr_osid.attr,
948 &pdcs_attr_osdep1.attr,
949 &pdcs_attr_diagnostic.attr,
950 &pdcs_attr_fastsize.attr,
951 &pdcs_attr_osdep2.attr,
Thibaut VARENEc7428422006-01-11 13:59:53 -0700952 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953};
954
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -0700955static struct attribute_group pdcs_attr_group = {
956 .attrs = pdcs_subsys_attrs,
957};
958
Greg Kroah-Hartmanc829a5b42007-11-07 13:56:19 -0800959static struct kobject *stable_kobj;
Greg Kroah-Hartman4443d072007-11-02 15:25:00 -0700960static struct kset *paths_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962/**
963 * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
964 *
965 * It creates kobjects corresponding to each path entry with nice sysfs
966 * links to the real device. This is where the magic takes place: when
967 * registering the subsystem attributes during module init, each kobject hereby
968 * created will show in the sysfs tree as a folder containing files as defined
969 * by path_subsys_attr[].
970 */
971static inline int __init
972pdcs_register_pathentries(void)
973{
974 unsigned short i;
975 struct pdcspath_entry *entry;
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500976 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Thibaut VARENEc7428422006-01-11 13:59:53 -0700978 /* Initialize the entries rw_lock before anything else */
979 for (i = 0; (entry = pdcspath_entries[i]); i++)
980 rwlock_init(&entry->rw_lock);
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 for (i = 0; (entry = pdcspath_entries[i]); i++) {
Thibaut VARENEc7428422006-01-11 13:59:53 -0700983 write_lock(&entry->rw_lock);
984 err = pdcspath_fetch(entry);
985 write_unlock(&entry->rw_lock);
986
987 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 continue;
989
Greg Kroah-Hartman4443d072007-11-02 15:25:00 -0700990 entry->kobj.kset = paths_kset;
Greg Kroah-Hartman73f368c2007-12-17 15:54:39 -0400991 err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL,
992 "%s", entry->name);
993 if (err)
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500994 return err;
Greg Kroah-Hartman73f368c2007-12-17 15:54:39 -0400995
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500996 /* kobject is now registered */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700997 write_lock(&entry->rw_lock);
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500998 entry->ready = 2;
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* Add a nice symlink to the real device */
Kyle McMartin26f03242007-10-18 00:04:06 -07001001 if (entry->dev) {
1002 err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
1003 WARN_ON(err);
1004 }
Thibaut VARENEc7428422006-01-11 13:59:53 -07001005
1006 write_unlock(&entry->rw_lock);
Greg Kroah-Hartman73f368c2007-12-17 15:54:39 -04001007 kobject_uevent(&entry->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
1010 return 0;
1011}
1012
1013/**
1014 * pdcs_unregister_pathentries - Routine called when unregistering the module.
1015 */
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001016static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017pdcs_unregister_pathentries(void)
1018{
1019 unsigned short i;
1020 struct pdcspath_entry *entry;
1021
Thibaut VARENEc7428422006-01-11 13:59:53 -07001022 for (i = 0; (entry = pdcspath_entries[i]); i++) {
1023 read_lock(&entry->rw_lock);
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001024 if (entry->ready >= 2)
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001025 kobject_put(&entry->kobj);
Thibaut VARENEc7428422006-01-11 13:59:53 -07001026 read_unlock(&entry->rw_lock);
1027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030/*
Thibaut VARENEc7428422006-01-11 13:59:53 -07001031 * For now we register the stable subsystem with the firmware subsystem
1032 * and the paths subsystem with the stable subsystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
1034static int __init
1035pdc_stable_init(void)
1036{
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -07001037 int rc = 0, error = 0;
Thibaut Varene3f9edb52006-05-04 18:43:34 -06001038 u32 result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /* find the size of the stable storage */
1041 if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
1042 return -ENODEV;
1043
Thibaut VARENEc7428422006-01-11 13:59:53 -07001044 /* make sure we have enough data */
1045 if (pdcs_size < 96)
1046 return -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Thibaut VARENEc7428422006-01-11 13:59:53 -07001048 printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
1049
Thibaut Varene3f9edb52006-05-04 18:43:34 -06001050 /* get OSID */
1051 if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
1052 return -EIO;
1053
1054 /* the actual result is 16 bits away */
1055 pdcs_osid = (u16)(result >> 16);
1056
Greg Kroah-Hartmanc829a5b42007-11-07 13:56:19 -08001057 /* For now we'll register the directory at /sys/firmware/stable */
1058 stable_kobj = kobject_create_and_add("stable", firmware_kobj);
1059 if (!stable_kobj) {
Greg Kroah-Hartman4443d072007-11-02 15:25:00 -07001060 rc = -ENOMEM;
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001061 goto fail_firmreg;
Greg Kroah-Hartman4443d072007-11-02 15:25:00 -07001062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Thibaut VARENEc7428422006-01-11 13:59:53 -07001064 /* Don't forget the root entries */
Joel Soeteff451d72008-02-18 18:26:11 -08001065 error = sysfs_create_group(stable_kobj, &pdcs_attr_group);
Greg Kroah-Hartman7f548212007-11-02 15:25:00 -07001066
Greg Kroah-Hartman4443d072007-11-02 15:25:00 -07001067 /* register the paths kset as a child of the stable kset */
Greg Kroah-Hartmanc829a5b42007-11-07 13:56:19 -08001068 paths_kset = kset_create_and_add("paths", NULL, stable_kobj);
Greg Kroah-Hartman4443d072007-11-02 15:25:00 -07001069 if (!paths_kset) {
1070 rc = -ENOMEM;
1071 goto fail_ksetreg;
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Greg Kroah-Hartman4443d072007-11-02 15:25:00 -07001074 /* now we create all "files" for the paths kset */
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001075 if ((rc = pdcs_register_pathentries()))
1076 goto fail_pdcsreg;
1077
1078 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001080fail_pdcsreg:
1081 pdcs_unregister_pathentries();
Greg Kroah-Hartman4443d072007-11-02 15:25:00 -07001082 kset_unregister(paths_kset);
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001083
Greg Kroah-Hartman4443d072007-11-02 15:25:00 -07001084fail_ksetreg:
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001085 kobject_put(stable_kobj);
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001086
1087fail_firmreg:
Thibaut VARENEc7428422006-01-11 13:59:53 -07001088 printk(KERN_INFO PDCS_PREFIX " bailing out\n");
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001089 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092static void __exit
1093pdc_stable_exit(void)
1094{
1095 pdcs_unregister_pathentries();
Greg Kroah-Hartman4443d072007-11-02 15:25:00 -07001096 kset_unregister(paths_kset);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001097 kobject_put(stable_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100
1101module_init(pdc_stable_init);
1102module_exit(pdc_stable_exit);