blob: 1382be64cc3f17610b0eef2571f6848dca432ec4 [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) \
123struct subsys_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
144 * you access the files provided by the facilites. We store a copy of the
145 * content of the stable storage WRT various paths in these structs. We read
146 * these structs when reading the files, and we will write to these structs when
147 * writing to the files, and only then write them back to the Stable Storage.
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 */
215 if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK) {
216 printk(KERN_ERR "%s: an error occured when writing to PDC.\n"
217 "It is likely that the Stable Storage data has been corrupted.\n"
218 "Please check it carefully upon next reboot.\n", __func__);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700219 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500222 /* kobject is already registered */
223 entry->ready = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/**
229 * pdcspath_hwpath_read - This function handles hardware path pretty printing.
230 * @entry: An allocated and populated pdscpath_entry struct.
231 * @buf: The output buffer to write to.
232 *
233 * We will call this function to format the output of the hwpath attribute file.
234 */
235static ssize_t
236pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
237{
238 char *out = buf;
239 struct device_path *devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700240 short i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 if (!entry || !buf)
243 return -EINVAL;
244
Thibaut VARENEc7428422006-01-11 13:59:53 -0700245 read_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 devpath = &entry->devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700247 i = entry->ready;
248 read_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Thibaut VARENEc7428422006-01-11 13:59:53 -0700250 if (!i) /* entry is not ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -ENODATA;
252
253 for (i = 0; i < 6; i++) {
254 if (devpath->bc[i] >= 128)
255 continue;
256 out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
257 }
258 out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
259
260 return out - buf;
261}
262
263/**
264 * pdcspath_hwpath_write - This function handles hardware path modifying.
265 * @entry: An allocated and populated pdscpath_entry struct.
266 * @buf: The input buffer to read from.
267 * @count: The number of bytes to be read.
268 *
269 * We will call this function to change the current hardware path.
270 * Hardware paths are to be given '/'-delimited, without brackets.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700271 * We make sure that the provided path actually maps to an existing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * device, BUT nothing would prevent some foolish user to set the path to some
273 * PCI bridge or even a CPU...
274 * A better work around would be to make sure we are at the end of a device tree
275 * for instance, but it would be IMHO beyond the simple scope of that driver.
276 * The aim is to provide a facility. Data correctness is left to userland.
277 */
278static ssize_t
279pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
280{
281 struct hardware_path hwpath;
282 unsigned short i;
283 char in[count+1], *temp;
284 struct device *dev;
Kyle McMartin26f03242007-10-18 00:04:06 -0700285 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (!entry || !buf || !count)
288 return -EINVAL;
289
290 /* We'll use a local copy of buf */
291 memset(in, 0, count+1);
292 strncpy(in, buf, count);
293
294 /* Let's clean up the target. 0xff is a blank pattern */
295 memset(&hwpath, 0xff, sizeof(hwpath));
296
297 /* First, pick the mod field (the last one of the input string) */
298 if (!(temp = strrchr(in, '/')))
299 return -EINVAL;
300
301 hwpath.mod = simple_strtoul(temp+1, NULL, 10);
302 in[temp-in] = '\0'; /* truncate the remaining string. just precaution */
303 DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
304
305 /* Then, loop for each delimiter, making sure we don't have too many.
306 we write the bc fields in a down-top way. No matter what, we stop
307 before writing the last field. If there are too many fields anyway,
308 then the user is a moron and it'll be caught up later when we'll
309 check the consistency of the given hwpath. */
310 for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
311 hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
312 in[temp-in] = '\0';
313 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
314 }
315
316 /* Store the final field */
317 hwpath.bc[i] = simple_strtoul(in, NULL, 10);
318 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
319
320 /* Now we check that the user isn't trying to lure us */
321 if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
322 printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
323 "hardware path: %s\n", __func__, entry->name, buf);
324 return -EINVAL;
325 }
326
327 /* So far so good, let's get in deep */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700328 write_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 entry->ready = 0;
330 entry->dev = dev;
331
332 /* Now, dive in. Write back to the hardware */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700333 pdcspath_store(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* Update the symlink to the real device */
336 sysfs_remove_link(&entry->kobj, "device");
Kyle McMartin26f03242007-10-18 00:04:06 -0700337 ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
338 WARN_ON(ret);
339
Thibaut VARENEc7428422006-01-11 13:59:53 -0700340 write_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Thibaut VARENEc7428422006-01-11 13:59:53 -0700342 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 entry->name, buf);
344
345 return count;
346}
347
348/**
349 * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
350 * @entry: An allocated and populated pdscpath_entry struct.
351 * @buf: The output buffer to write to.
352 *
353 * We will call this function to format the output of the layer attribute file.
354 */
355static ssize_t
356pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
357{
358 char *out = buf;
359 struct device_path *devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700360 short i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if (!entry || !buf)
363 return -EINVAL;
364
Thibaut VARENEc7428422006-01-11 13:59:53 -0700365 read_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 devpath = &entry->devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700367 i = entry->ready;
368 read_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Thibaut VARENEc7428422006-01-11 13:59:53 -0700370 if (!i) /* entry is not ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return -ENODATA;
372
373 for (i = 0; devpath->layers[i] && (likely(i < 6)); i++)
374 out += sprintf(out, "%u ", devpath->layers[i]);
375
376 out += sprintf(out, "\n");
377
378 return out - buf;
379}
380
381/**
382 * pdcspath_layer_write - This function handles extended layer modifying.
383 * @entry: An allocated and populated pdscpath_entry struct.
384 * @buf: The input buffer to read from.
385 * @count: The number of bytes to be read.
386 *
387 * We will call this function to change the current layer value.
388 * Layers are to be given '.'-delimited, without brackets.
389 * XXX beware we are far less checky WRT input data provided than for hwpath.
390 * Potential harm can be done, since there's no way to check the validity of
391 * the layer fields.
392 */
393static ssize_t
394pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
395{
396 unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
397 unsigned short i;
398 char in[count+1], *temp;
399
400 if (!entry || !buf || !count)
401 return -EINVAL;
402
403 /* We'll use a local copy of buf */
404 memset(in, 0, count+1);
405 strncpy(in, buf, count);
406
407 /* Let's clean up the target. 0 is a blank pattern */
408 memset(&layers, 0, sizeof(layers));
409
410 /* First, pick the first layer */
411 if (unlikely(!isdigit(*in)))
412 return -EINVAL;
413 layers[0] = simple_strtoul(in, NULL, 10);
414 DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
415
416 temp = in;
417 for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
418 if (unlikely(!isdigit(*(++temp))))
419 return -EINVAL;
420 layers[i] = simple_strtoul(temp, NULL, 10);
421 DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
422 }
423
424 /* So far so good, let's get in deep */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700425 write_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* First, overwrite the current layers with the new ones, not touching
428 the hardware path. */
429 memcpy(&entry->devpath.layers, &layers, sizeof(layers));
430
431 /* Now, dive in. Write back to the hardware */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700432 pdcspath_store(entry);
433 write_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Thibaut VARENEc7428422006-01-11 13:59:53 -0700435 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 entry->name, buf);
437
438 return count;
439}
440
441/**
442 * pdcspath_attr_show - Generic read function call wrapper.
443 * @kobj: The kobject to get info from.
444 * @attr: The attribute looked upon.
445 * @buf: The output buffer.
446 */
447static ssize_t
448pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
449{
450 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
451 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
452 ssize_t ret = 0;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (pdcs_attr->show)
455 ret = pdcs_attr->show(entry, buf);
456
457 return ret;
458}
459
460/**
461 * pdcspath_attr_store - Generic write function call wrapper.
462 * @kobj: The kobject to write info to.
463 * @attr: The attribute to be modified.
464 * @buf: The input buffer.
465 * @count: The size of the buffer.
466 */
467static ssize_t
468pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
469 const char *buf, size_t count)
470{
471 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
472 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
473 ssize_t ret = 0;
474
475 if (!capable(CAP_SYS_ADMIN))
476 return -EACCES;
477
478 if (pdcs_attr->store)
479 ret = pdcs_attr->store(entry, buf, count);
480
481 return ret;
482}
483
484static struct sysfs_ops pdcspath_attr_ops = {
485 .show = pdcspath_attr_show,
486 .store = pdcspath_attr_store,
487};
488
489/* These are the two attributes of any PDC path. */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700490static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
491static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493static struct attribute *paths_subsys_attrs[] = {
494 &paths_attr_hwpath.attr,
495 &paths_attr_layer.attr,
496 NULL,
497};
498
499/* Specific kobject type for our PDC paths */
500static struct kobj_type ktype_pdcspath = {
501 .sysfs_ops = &pdcspath_attr_ops,
502 .default_attrs = paths_subsys_attrs,
503};
504
505/* We hard define the 4 types of path we expect to find */
506static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
507static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
508static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
509static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
510
511/* An array containing all PDC paths we will deal with */
512static struct pdcspath_entry *pdcspath_entries[] = {
513 &pdcspath_entry_primary,
514 &pdcspath_entry_alternative,
515 &pdcspath_entry_console,
516 &pdcspath_entry_keyboard,
517 NULL,
518};
519
Thibaut VARENEc7428422006-01-11 13:59:53 -0700520
521/* For more insight of what's going on here, refer to PDC Procedures doc,
522 * Section PDC_STABLE */
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/**
Thibaut VARENEc7428422006-01-11 13:59:53 -0700525 * pdcs_size_read - Stable Storage size output.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700526 * @kset: An allocated and populated struct kset. We don't use it tho.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * @buf: The output buffer to write to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
529static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700530pdcs_size_read(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 char *out = buf;
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700533
534 if (!kset || !buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return -EINVAL;
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* show the size of the stable storage */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700538 out += sprintf(out, "%ld\n", pdcs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Thibaut VARENEc7428422006-01-11 13:59:53 -0700540 return out - buf;
541}
542
543/**
544 * pdcs_auto_read - Stable Storage autoboot/search flag output.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700545 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700546 * @buf: The output buffer to write to.
547 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
548 */
549static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700550pdcs_auto_read(struct kset *kset, char *buf, int knob)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700551{
552 char *out = buf;
553 struct pdcspath_entry *pathentry;
Helge Deller67a5a592006-03-27 19:52:14 +0000554
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700555 if (!kset || !buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700556 return -EINVAL;
557
558 /* Current flags are stored in primary boot path entry */
559 pathentry = &pdcspath_entry_primary;
560
561 read_lock(&pathentry->rw_lock);
562 out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
563 "On" : "Off");
564 read_unlock(&pathentry->rw_lock);
565
566 return out - buf;
567}
568
569/**
570 * pdcs_autoboot_read - Stable Storage autoboot flag output.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700571 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700572 * @buf: The output buffer to write to.
573 */
574static inline ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700575pdcs_autoboot_read(struct kset *kset, char *buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700576{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700577 return pdcs_auto_read(kset, buf, PF_AUTOBOOT);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700578}
579
580/**
581 * pdcs_autosearch_read - Stable Storage autoboot flag output.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700582 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700583 * @buf: The output buffer to write to.
584 */
585static inline ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700586pdcs_autosearch_read(struct kset *kset, char *buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700587{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700588 return pdcs_auto_read(kset, buf, PF_AUTOSEARCH);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700589}
590
591/**
592 * pdcs_timer_read - Stable Storage timer count output (in seconds).
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700593 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700594 * @buf: The output buffer to write to.
595 *
596 * The value of the timer field correponds to a number of seconds in powers of 2.
597 */
598static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700599pdcs_timer_read(struct kset *kset, char *buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700600{
601 char *out = buf;
602 struct pdcspath_entry *pathentry;
603
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700604 if (!kset || !buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700605 return -EINVAL;
606
607 /* Current flags are stored in primary boot path entry */
608 pathentry = &pdcspath_entry_primary;
609
610 /* print the timer value in seconds */
611 read_lock(&pathentry->rw_lock);
612 out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
613 (1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
614 read_unlock(&pathentry->rw_lock);
615
616 return out - buf;
617}
618
619/**
620 * pdcs_osid_read - Stable Storage OS ID register output.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700621 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700622 * @buf: The output buffer to write to.
623 */
624static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700625pdcs_osid_read(struct kset *kset, char *buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700626{
627 char *out = buf;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700628
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700629 if (!kset || !buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700630 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Kyle McMartin67a061a2006-06-25 16:58:57 +0000632 out += sprintf(out, "%s dependent data (0x%.4x)\n",
633 os_id_to_string(pdcs_osid), pdcs_osid);
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600634
635 return out - buf;
636}
637
638/**
639 * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700640 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600641 * @buf: The output buffer to write to.
642 *
643 * This can hold 16 bytes of OS-Dependent data.
644 */
645static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700646pdcs_osdep1_read(struct kset *kset, char *buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600647{
648 char *out = buf;
649 u32 result[4];
650
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700651 if (!kset || !buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600652 return -EINVAL;
653
654 if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
655 return -EIO;
656
657 out += sprintf(out, "0x%.8x\n", result[0]);
658 out += sprintf(out, "0x%.8x\n", result[1]);
659 out += sprintf(out, "0x%.8x\n", result[2]);
660 out += sprintf(out, "0x%.8x\n", result[3]);
661
662 return out - buf;
663}
664
665/**
666 * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700667 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600668 * @buf: The output buffer to write to.
669 *
670 * I have NFC how to interpret the content of that register ;-).
671 */
672static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700673pdcs_diagnostic_read(struct kset *kset, char *buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600674{
675 char *out = buf;
676 u32 result;
677
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700678 if (!kset || !buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600679 return -EINVAL;
680
681 /* get diagnostic */
682 if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
683 return -EIO;
684
685 out += sprintf(out, "0x%.4x\n", (result >> 16));
Thibaut VARENEc7428422006-01-11 13:59:53 -0700686
687 return out - buf;
688}
689
690/**
691 * pdcs_fastsize_read - Stable Storage FastSize register output.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700692 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700693 * @buf: The output buffer to write to.
694 *
695 * This register holds the amount of system RAM to be tested during boot sequence.
696 */
697static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700698pdcs_fastsize_read(struct kset *kset, char *buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700699{
700 char *out = buf;
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600701 u32 result;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700702
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700703 if (!kset || !buf)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700704 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /* get fast-size */
707 if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
708 return -EIO;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if ((result & 0x0F) < 0x0E)
Randolph Chungabff7542005-10-21 22:57:13 -0400711 out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 else
713 out += sprintf(out, "All");
714 out += sprintf(out, "\n");
715
716 return out - buf;
717}
718
719/**
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600720 * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700721 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600722 * @buf: The output buffer to write to.
723 *
724 * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
725 */
726static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700727pdcs_osdep2_read(struct kset *kset, char *buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600728{
729 char *out = buf;
730 unsigned long size;
731 unsigned short i;
732 u32 result;
733
734 if (unlikely(pdcs_size <= 224))
735 return -ENODATA;
736
737 size = pdcs_size - 224;
738
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700739 if (!kset || !buf)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600740 return -EINVAL;
741
742 for (i=0; i<size; i+=4) {
743 if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
744 sizeof(result)) != PDC_OK))
745 return -EIO;
746 out += sprintf(out, "0x%.8x\n", result);
747 }
748
749 return out - buf;
750}
751
752/**
Thibaut VARENEc7428422006-01-11 13:59:53 -0700753 * pdcs_auto_write - This function handles autoboot/search flag modifying.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700754 * @kset: An allocated and populated struct kset. We don't use it tho.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * @buf: The input buffer to read from.
756 * @count: The number of bytes to be read.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700757 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *
Thibaut VARENEc7428422006-01-11 13:59:53 -0700759 * We will call this function to change the current autoboot flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * We expect a precise syntax:
Thibaut VARENEc7428422006-01-11 13:59:53 -0700761 * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
763static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700764pdcs_auto_write(struct kset *kset, const char *buf, size_t count, int knob)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct pdcspath_entry *pathentry;
767 unsigned char flags;
768 char in[count+1], *temp;
769 char c;
770
771 if (!capable(CAP_SYS_ADMIN))
772 return -EACCES;
773
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700774 if (!kset || !buf || !count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return -EINVAL;
776
777 /* We'll use a local copy of buf */
778 memset(in, 0, count+1);
779 strncpy(in, buf, count);
780
781 /* Current flags are stored in primary boot path entry */
782 pathentry = &pdcspath_entry_primary;
783
784 /* Be nice to the existing flag record */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700785 read_lock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 flags = pathentry->devpath.flags;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700787 read_unlock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
790
791 temp = in;
792
793 while (*temp && isspace(*temp))
794 temp++;
795
796 c = *temp++ - '0';
797 if ((c != 0) && (c != 1))
798 goto parse_error;
799 if (c == 0)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700800 flags &= ~knob;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 else
Thibaut VARENEc7428422006-01-11 13:59:53 -0700802 flags |= knob;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
805
806 /* So far so good, let's get in deep */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700807 write_lock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /* Change the path entry flags first */
810 pathentry->devpath.flags = flags;
811
812 /* Now, dive in. Write back to the hardware */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700813 pdcspath_store(pathentry);
814 write_unlock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Thibaut VARENEc7428422006-01-11 13:59:53 -0700816 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
817 (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
818 (flags & knob) ? "On" : "Off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 return count;
821
822parse_error:
Thibaut VARENEc7428422006-01-11 13:59:53 -0700823 printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -EINVAL;
825}
826
Thibaut VARENEc7428422006-01-11 13:59:53 -0700827/**
828 * pdcs_autoboot_write - This function handles autoboot flag modifying.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700829 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700830 * @buf: The input buffer to read from.
831 * @count: The number of bytes to be read.
832 *
833 * We will call this function to change the current boot flags.
834 * We expect a precise syntax:
835 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
836 */
837static inline ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700838pdcs_autoboot_write(struct kset *kset, const char *buf, size_t count)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700839{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700840 return pdcs_auto_write(kset, buf, count, PF_AUTOBOOT);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700841}
842
843/**
844 * pdcs_autosearch_write - This function handles autosearch flag modifying.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700845 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700846 * @buf: The input buffer to read from.
847 * @count: The number of bytes to be read.
848 *
849 * We will call this function to change the current boot flags.
850 * We expect a precise syntax:
851 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
852 */
853static inline ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700854pdcs_autosearch_write(struct kset *kset, const char *buf, size_t count)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700855{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700856 return pdcs_auto_write(kset, buf, count, PF_AUTOSEARCH);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700857}
858
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600859/**
860 * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700861 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600862 * @buf: The input buffer to read from.
863 * @count: The number of bytes to be read.
864 *
865 * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
866 * write approach. It's up to userspace to deal with it when constructing
867 * its input buffer.
868 */
869static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700870pdcs_osdep1_write(struct kset *kset, const char *buf, size_t count)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600871{
872 u8 in[16];
873
874 if (!capable(CAP_SYS_ADMIN))
875 return -EACCES;
876
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700877 if (!kset || !buf || !count)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600878 return -EINVAL;
879
Kyle McMartinec1fdc22006-06-21 19:27:29 +0000880 if (unlikely(pdcs_osid != OS_ID_LINUX))
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600881 return -EPERM;
882
883 if (count > 16)
884 return -EMSGSIZE;
885
886 /* We'll use a local copy of buf */
887 memset(in, 0, 16);
888 memcpy(in, buf, count);
889
890 if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
891 return -EIO;
892
893 return count;
894}
895
896/**
897 * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700898 * @kset: An allocated and populated struct kset. We don't use it tho.
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600899 * @buf: The input buffer to read from.
900 * @count: The number of bytes to be read.
901 *
902 * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
903 * byte-by-byte write approach. It's up to userspace to deal with it when
904 * constructing its input buffer.
905 */
906static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700907pdcs_osdep2_write(struct kset *kset, const char *buf, size_t count)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600908{
909 unsigned long size;
910 unsigned short i;
911 u8 in[4];
912
913 if (!capable(CAP_SYS_ADMIN))
914 return -EACCES;
915
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700916 if (!kset || !buf || !count)
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600917 return -EINVAL;
918
919 if (unlikely(pdcs_size <= 224))
920 return -ENOSYS;
921
Kyle McMartinec1fdc22006-06-21 19:27:29 +0000922 if (unlikely(pdcs_osid != OS_ID_LINUX))
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600923 return -EPERM;
924
925 size = pdcs_size - 224;
926
927 if (count > size)
928 return -EMSGSIZE;
929
930 /* We'll use a local copy of buf */
931
932 for (i=0; i<count; i+=4) {
933 memset(in, 0, 4);
934 memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
935 if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
936 sizeof(in)) != PDC_OK))
937 return -EIO;
938 }
939
940 return count;
941}
942
Thibaut VARENEc7428422006-01-11 13:59:53 -0700943/* The remaining attributes. */
944static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
945static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
946static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
947static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600948static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
949static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
950static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700951static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600952static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954static struct subsys_attribute *pdcs_subsys_attrs[] = {
Thibaut VARENEc7428422006-01-11 13:59:53 -0700955 &pdcs_attr_size,
956 &pdcs_attr_autoboot,
957 &pdcs_attr_autosearch,
958 &pdcs_attr_timer,
959 &pdcs_attr_osid,
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600960 &pdcs_attr_osdep1,
961 &pdcs_attr_diagnostic,
Thibaut VARENEc7428422006-01-11 13:59:53 -0700962 &pdcs_attr_fastsize,
Thibaut Varene3f9edb52006-05-04 18:43:34 -0600963 &pdcs_attr_osdep2,
Thibaut VARENEc7428422006-01-11 13:59:53 -0700964 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965};
966
Greg Kroah-Hartman3514fac2007-10-16 10:11:44 -0600967static decl_subsys(paths, NULL);
968static decl_subsys(stable, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970/**
971 * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
972 *
973 * It creates kobjects corresponding to each path entry with nice sysfs
974 * links to the real device. This is where the magic takes place: when
975 * registering the subsystem attributes during module init, each kobject hereby
976 * created will show in the sysfs tree as a folder containing files as defined
977 * by path_subsys_attr[].
978 */
979static inline int __init
980pdcs_register_pathentries(void)
981{
982 unsigned short i;
983 struct pdcspath_entry *entry;
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500984 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Thibaut VARENEc7428422006-01-11 13:59:53 -0700986 /* Initialize the entries rw_lock before anything else */
987 for (i = 0; (entry = pdcspath_entries[i]); i++)
988 rwlock_init(&entry->rw_lock);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 for (i = 0; (entry = pdcspath_entries[i]); i++) {
Thibaut VARENEc7428422006-01-11 13:59:53 -0700991 write_lock(&entry->rw_lock);
992 err = pdcspath_fetch(entry);
993 write_unlock(&entry->rw_lock);
994
995 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 continue;
997
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500998 if ((err = kobject_set_name(&entry->kobj, "%s", entry->name)))
999 return err;
Greg Kroah-Hartman3514fac2007-10-16 10:11:44 -06001000 entry->kobj.kset = &paths_subsys;
1001 entry->kobj.ktype = &ktype_pdcspath;
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001002 if ((err = kobject_register(&entry->kobj)))
1003 return err;
1004
1005 /* kobject is now registered */
Thibaut VARENEc7428422006-01-11 13:59:53 -07001006 write_lock(&entry->rw_lock);
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001007 entry->ready = 2;
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* Add a nice symlink to the real device */
Kyle McMartin26f03242007-10-18 00:04:06 -07001010 if (entry->dev) {
1011 err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
1012 WARN_ON(err);
1013 }
Thibaut VARENEc7428422006-01-11 13:59:53 -07001014
1015 write_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
1018 return 0;
1019}
1020
1021/**
1022 * pdcs_unregister_pathentries - Routine called when unregistering the module.
1023 */
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001024static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025pdcs_unregister_pathentries(void)
1026{
1027 unsigned short i;
1028 struct pdcspath_entry *entry;
1029
Thibaut VARENEc7428422006-01-11 13:59:53 -07001030 for (i = 0; (entry = pdcspath_entries[i]); i++) {
1031 read_lock(&entry->rw_lock);
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001032 if (entry->ready >= 2)
Thibaut VARENEc7428422006-01-11 13:59:53 -07001033 kobject_unregister(&entry->kobj);
1034 read_unlock(&entry->rw_lock);
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/*
Thibaut VARENEc7428422006-01-11 13:59:53 -07001039 * For now we register the stable subsystem with the firmware subsystem
1040 * and the paths subsystem with the stable subsystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
1042static int __init
1043pdc_stable_init(void)
1044{
1045 struct subsys_attribute *attr;
1046 int i, rc = 0, error = 0;
Thibaut Varene3f9edb52006-05-04 18:43:34 -06001047 u32 result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 /* find the size of the stable storage */
1050 if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
1051 return -ENODEV;
1052
Thibaut VARENEc7428422006-01-11 13:59:53 -07001053 /* make sure we have enough data */
1054 if (pdcs_size < 96)
1055 return -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Thibaut VARENEc7428422006-01-11 13:59:53 -07001057 printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
1058
Thibaut Varene3f9edb52006-05-04 18:43:34 -06001059 /* get OSID */
1060 if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
1061 return -EIO;
1062
1063 /* the actual result is 16 bits away */
1064 pdcs_osid = (u16)(result >> 16);
1065
Thibaut VARENEc7428422006-01-11 13:59:53 -07001066 /* For now we'll register the stable subsys within this driver */
1067 if ((rc = firmware_register(&stable_subsys)))
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001068 goto fail_firmreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Thibaut VARENEc7428422006-01-11 13:59:53 -07001070 /* Don't forget the root entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 for (i = 0; (attr = pdcs_subsys_attrs[i]) && !error; i++)
1072 if (attr->show)
Thibaut VARENEc7428422006-01-11 13:59:53 -07001073 error = subsys_create_file(&stable_subsys, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Thibaut VARENEc7428422006-01-11 13:59:53 -07001075 /* register the paths subsys as a subsystem of stable subsys */
Greg Kroah-Hartman3514fac2007-10-16 10:11:44 -06001076 paths_subsys.kobj.kset = &stable_subsys;
Thibaut Varene3f9edb52006-05-04 18:43:34 -06001077 if ((rc = subsystem_register(&paths_subsys)))
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001078 goto fail_subsysreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /* now we create all "files" for the paths subsys */
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001081 if ((rc = pdcs_register_pathentries()))
1082 goto fail_pdcsreg;
1083
1084 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001086fail_pdcsreg:
1087 pdcs_unregister_pathentries();
1088 subsystem_unregister(&paths_subsys);
1089
1090fail_subsysreg:
Thibaut VARENEc7428422006-01-11 13:59:53 -07001091 firmware_unregister(&stable_subsys);
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001092
1093fail_firmreg:
Thibaut VARENEc7428422006-01-11 13:59:53 -07001094 printk(KERN_INFO PDCS_PREFIX " bailing out\n");
Thibaut VARENE4b991da2006-01-10 20:48:01 -05001095 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
1098static void __exit
1099pdc_stable_exit(void)
1100{
1101 pdcs_unregister_pathentries();
1102 subsystem_unregister(&paths_subsys);
1103
Thibaut VARENEc7428422006-01-11 13:59:53 -07001104 firmware_unregister(&stable_subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107
1108module_init(pdc_stable_init);
1109module_exit(pdc_stable_exit);