blob: bbeabe3fc4c6788984f3bd9567b3543681c1bf5a [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.
31 * The code makes no use of data above 192 bytes. One last word: there's one
32 * path we can always count on: the primary path.
Thibaut VARENEc7428422006-01-11 13:59:53 -070033 *
34 * The current policy wrt file permissions is:
35 * - write: root only
36 * - read: (reading triggers PDC calls) ? root only : everyone
37 * The rationale is that PDC calls could hog (DoS) the machine.
38 *
39 * TODO:
40 * - timer/fastsize write calls
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42
43#undef PDCS_DEBUG
44#ifdef PDCS_DEBUG
45#define DPRINTK(fmt, args...) printk(KERN_DEBUG fmt, ## args)
46#else
47#define DPRINTK(fmt, args...)
48#endif
49
50#include <linux/module.h>
51#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/kernel.h>
53#include <linux/string.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080054#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/ctype.h>
56#include <linux/sysfs.h>
57#include <linux/kobject.h>
58#include <linux/device.h>
59#include <linux/errno.h>
Thibaut VARENEc7428422006-01-11 13:59:53 -070060#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include <asm/pdc.h>
63#include <asm/page.h>
64#include <asm/uaccess.h>
65#include <asm/hardware.h>
66
Thibaut VARENEc7428422006-01-11 13:59:53 -070067#define PDCS_VERSION "0.22"
68#define PDCS_PREFIX "PDC Stable Storage"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define PDCS_ADDR_PPRI 0x00
71#define PDCS_ADDR_OSID 0x40
72#define PDCS_ADDR_FSIZ 0x5C
73#define PDCS_ADDR_PCON 0x60
74#define PDCS_ADDR_PALT 0x80
75#define PDCS_ADDR_PKBD 0xA0
76
77MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
78MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
79MODULE_LICENSE("GPL");
80MODULE_VERSION(PDCS_VERSION);
81
Thibaut VARENEc7428422006-01-11 13:59:53 -070082/* holds Stable Storage size. Initialized once and for all, no lock needed */
Helge Deller8039de12006-01-10 20:35:03 -050083static unsigned long pdcs_size __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* This struct defines what we need to deal with a parisc pdc path entry */
86struct pdcspath_entry {
Thibaut VARENEc7428422006-01-11 13:59:53 -070087 rwlock_t rw_lock; /* to protect path entry access */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 short ready; /* entry record is valid if != 0 */
89 unsigned long addr; /* entry address in stable storage */
90 char *name; /* entry name */
91 struct device_path devpath; /* device path in parisc representation */
92 struct device *dev; /* corresponding device */
93 struct kobject kobj;
94};
95
96struct pdcspath_attribute {
97 struct attribute attr;
98 ssize_t (*show)(struct pdcspath_entry *entry, char *buf);
99 ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count);
100};
101
102#define PDCSPATH_ENTRY(_addr, _name) \
103struct pdcspath_entry pdcspath_entry_##_name = { \
104 .ready = 0, \
105 .addr = _addr, \
106 .name = __stringify(_name), \
107};
108
109#define PDCS_ATTR(_name, _mode, _show, _store) \
110struct subsys_attribute pdcs_attr_##_name = { \
111 .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
112 .show = _show, \
113 .store = _store, \
114};
115
116#define PATHS_ATTR(_name, _mode, _show, _store) \
117struct pdcspath_attribute paths_attr_##_name = { \
118 .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
119 .show = _show, \
120 .store = _store, \
121};
122
123#define to_pdcspath_attribute(_attr) container_of(_attr, struct pdcspath_attribute, attr)
124#define to_pdcspath_entry(obj) container_of(obj, struct pdcspath_entry, kobj)
125
126/**
127 * pdcspath_fetch - This function populates the path entry structs.
128 * @entry: A pointer to an allocated pdcspath_entry.
129 *
130 * The general idea is that you don't read from the Stable Storage every time
131 * you access the files provided by the facilites. We store a copy of the
132 * content of the stable storage WRT various paths in these structs. We read
133 * these structs when reading the files, and we will write to these structs when
134 * writing to the files, and only then write them back to the Stable Storage.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700135 *
136 * This function expects to be called with @entry->rw_lock write-hold.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
138static int
139pdcspath_fetch(struct pdcspath_entry *entry)
140{
141 struct device_path *devpath;
142
143 if (!entry)
144 return -EINVAL;
145
146 devpath = &entry->devpath;
147
148 DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
149 entry, devpath, entry->addr);
150
151 /* addr, devpath and count must be word aligned */
152 if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
153 return -EIO;
154
155 /* Find the matching device.
156 NOTE: hardware_path overlays with device_path, so the nice cast can
157 be used */
158 entry->dev = hwpath_to_device((struct hardware_path *)devpath);
159
160 entry->ready = 1;
161
162 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
163
164 return 0;
165}
166
167/**
168 * pdcspath_store - This function writes a path to stable storage.
169 * @entry: A pointer to an allocated pdcspath_entry.
170 *
171 * It can be used in two ways: either by passing it a preset devpath struct
172 * containing an already computed hardware path, or by passing it a device
173 * pointer, from which it'll find out the corresponding hardware path.
174 * For now we do not handle the case where there's an error in writing to the
175 * Stable Storage area, so you'd better not mess up the data :P
Thibaut VARENEc7428422006-01-11 13:59:53 -0700176 *
177 * This function expects to be called with @entry->rw_lock write-hold.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700179static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180pdcspath_store(struct pdcspath_entry *entry)
181{
182 struct device_path *devpath;
183
Thibaut VARENEc7428422006-01-11 13:59:53 -0700184 BUG_ON(!entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 devpath = &entry->devpath;
187
188 /* We expect the caller to set the ready flag to 0 if the hardware
189 path struct provided is invalid, so that we know we have to fill it.
190 First case, we don't have a preset hwpath... */
191 if (!entry->ready) {
192 /* ...but we have a device, map it */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700193 BUG_ON(!entry->dev);
194 device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 /* else, we expect the provided hwpath to be valid. */
197
198 DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
199 entry, devpath, entry->addr);
200
201 /* addr, devpath and count must be word aligned */
202 if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK) {
203 printk(KERN_ERR "%s: an error occured when writing to PDC.\n"
204 "It is likely that the Stable Storage data has been corrupted.\n"
205 "Please check it carefully upon next reboot.\n", __func__);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700206 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500209 /* kobject is already registered */
210 entry->ready = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215/**
216 * pdcspath_hwpath_read - This function handles hardware path pretty printing.
217 * @entry: An allocated and populated pdscpath_entry struct.
218 * @buf: The output buffer to write to.
219 *
220 * We will call this function to format the output of the hwpath attribute file.
221 */
222static ssize_t
223pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
224{
225 char *out = buf;
226 struct device_path *devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700227 short i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (!entry || !buf)
230 return -EINVAL;
231
Thibaut VARENEc7428422006-01-11 13:59:53 -0700232 read_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 devpath = &entry->devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700234 i = entry->ready;
235 read_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Thibaut VARENEc7428422006-01-11 13:59:53 -0700237 if (!i) /* entry is not ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -ENODATA;
239
240 for (i = 0; i < 6; i++) {
241 if (devpath->bc[i] >= 128)
242 continue;
243 out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
244 }
245 out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
246
247 return out - buf;
248}
249
250/**
251 * pdcspath_hwpath_write - This function handles hardware path modifying.
252 * @entry: An allocated and populated pdscpath_entry struct.
253 * @buf: The input buffer to read from.
254 * @count: The number of bytes to be read.
255 *
256 * We will call this function to change the current hardware path.
257 * Hardware paths are to be given '/'-delimited, without brackets.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700258 * We make sure that the provided path actually maps to an existing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * device, BUT nothing would prevent some foolish user to set the path to some
260 * PCI bridge or even a CPU...
261 * A better work around would be to make sure we are at the end of a device tree
262 * for instance, but it would be IMHO beyond the simple scope of that driver.
263 * The aim is to provide a facility. Data correctness is left to userland.
264 */
265static ssize_t
266pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
267{
268 struct hardware_path hwpath;
269 unsigned short i;
270 char in[count+1], *temp;
271 struct device *dev;
272
273 if (!entry || !buf || !count)
274 return -EINVAL;
275
276 /* We'll use a local copy of buf */
277 memset(in, 0, count+1);
278 strncpy(in, buf, count);
279
280 /* Let's clean up the target. 0xff is a blank pattern */
281 memset(&hwpath, 0xff, sizeof(hwpath));
282
283 /* First, pick the mod field (the last one of the input string) */
284 if (!(temp = strrchr(in, '/')))
285 return -EINVAL;
286
287 hwpath.mod = simple_strtoul(temp+1, NULL, 10);
288 in[temp-in] = '\0'; /* truncate the remaining string. just precaution */
289 DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
290
291 /* Then, loop for each delimiter, making sure we don't have too many.
292 we write the bc fields in a down-top way. No matter what, we stop
293 before writing the last field. If there are too many fields anyway,
294 then the user is a moron and it'll be caught up later when we'll
295 check the consistency of the given hwpath. */
296 for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
297 hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
298 in[temp-in] = '\0';
299 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
300 }
301
302 /* Store the final field */
303 hwpath.bc[i] = simple_strtoul(in, NULL, 10);
304 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
305
306 /* Now we check that the user isn't trying to lure us */
307 if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
308 printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
309 "hardware path: %s\n", __func__, entry->name, buf);
310 return -EINVAL;
311 }
312
313 /* So far so good, let's get in deep */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700314 write_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 entry->ready = 0;
316 entry->dev = dev;
317
318 /* Now, dive in. Write back to the hardware */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700319 pdcspath_store(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /* Update the symlink to the real device */
322 sysfs_remove_link(&entry->kobj, "device");
323 sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
Thibaut VARENEc7428422006-01-11 13:59:53 -0700324 write_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Thibaut VARENEc7428422006-01-11 13:59:53 -0700326 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 entry->name, buf);
328
329 return count;
330}
331
332/**
333 * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
334 * @entry: An allocated and populated pdscpath_entry struct.
335 * @buf: The output buffer to write to.
336 *
337 * We will call this function to format the output of the layer attribute file.
338 */
339static ssize_t
340pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
341{
342 char *out = buf;
343 struct device_path *devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700344 short i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (!entry || !buf)
347 return -EINVAL;
348
Thibaut VARENEc7428422006-01-11 13:59:53 -0700349 read_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 devpath = &entry->devpath;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700351 i = entry->ready;
352 read_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Thibaut VARENEc7428422006-01-11 13:59:53 -0700354 if (!i) /* entry is not ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -ENODATA;
356
357 for (i = 0; devpath->layers[i] && (likely(i < 6)); i++)
358 out += sprintf(out, "%u ", devpath->layers[i]);
359
360 out += sprintf(out, "\n");
361
362 return out - buf;
363}
364
365/**
366 * pdcspath_layer_write - This function handles extended layer modifying.
367 * @entry: An allocated and populated pdscpath_entry struct.
368 * @buf: The input buffer to read from.
369 * @count: The number of bytes to be read.
370 *
371 * We will call this function to change the current layer value.
372 * Layers are to be given '.'-delimited, without brackets.
373 * XXX beware we are far less checky WRT input data provided than for hwpath.
374 * Potential harm can be done, since there's no way to check the validity of
375 * the layer fields.
376 */
377static ssize_t
378pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
379{
380 unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
381 unsigned short i;
382 char in[count+1], *temp;
383
384 if (!entry || !buf || !count)
385 return -EINVAL;
386
387 /* We'll use a local copy of buf */
388 memset(in, 0, count+1);
389 strncpy(in, buf, count);
390
391 /* Let's clean up the target. 0 is a blank pattern */
392 memset(&layers, 0, sizeof(layers));
393
394 /* First, pick the first layer */
395 if (unlikely(!isdigit(*in)))
396 return -EINVAL;
397 layers[0] = simple_strtoul(in, NULL, 10);
398 DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
399
400 temp = in;
401 for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
402 if (unlikely(!isdigit(*(++temp))))
403 return -EINVAL;
404 layers[i] = simple_strtoul(temp, NULL, 10);
405 DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
406 }
407
408 /* So far so good, let's get in deep */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700409 write_lock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* First, overwrite the current layers with the new ones, not touching
412 the hardware path. */
413 memcpy(&entry->devpath.layers, &layers, sizeof(layers));
414
415 /* Now, dive in. Write back to the hardware */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700416 pdcspath_store(entry);
417 write_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Thibaut VARENEc7428422006-01-11 13:59:53 -0700419 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 entry->name, buf);
421
422 return count;
423}
424
425/**
426 * pdcspath_attr_show - Generic read function call wrapper.
427 * @kobj: The kobject to get info from.
428 * @attr: The attribute looked upon.
429 * @buf: The output buffer.
430 */
431static ssize_t
432pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
433{
434 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
435 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
436 ssize_t ret = 0;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (pdcs_attr->show)
439 ret = pdcs_attr->show(entry, buf);
440
441 return ret;
442}
443
444/**
445 * pdcspath_attr_store - Generic write function call wrapper.
446 * @kobj: The kobject to write info to.
447 * @attr: The attribute to be modified.
448 * @buf: The input buffer.
449 * @count: The size of the buffer.
450 */
451static ssize_t
452pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
453 const char *buf, size_t count)
454{
455 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
456 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
457 ssize_t ret = 0;
458
459 if (!capable(CAP_SYS_ADMIN))
460 return -EACCES;
461
462 if (pdcs_attr->store)
463 ret = pdcs_attr->store(entry, buf, count);
464
465 return ret;
466}
467
468static struct sysfs_ops pdcspath_attr_ops = {
469 .show = pdcspath_attr_show,
470 .store = pdcspath_attr_store,
471};
472
473/* These are the two attributes of any PDC path. */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700474static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
475static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477static struct attribute *paths_subsys_attrs[] = {
478 &paths_attr_hwpath.attr,
479 &paths_attr_layer.attr,
480 NULL,
481};
482
483/* Specific kobject type for our PDC paths */
484static struct kobj_type ktype_pdcspath = {
485 .sysfs_ops = &pdcspath_attr_ops,
486 .default_attrs = paths_subsys_attrs,
487};
488
489/* We hard define the 4 types of path we expect to find */
490static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
491static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
492static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
493static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
494
495/* An array containing all PDC paths we will deal with */
496static struct pdcspath_entry *pdcspath_entries[] = {
497 &pdcspath_entry_primary,
498 &pdcspath_entry_alternative,
499 &pdcspath_entry_console,
500 &pdcspath_entry_keyboard,
501 NULL,
502};
503
Thibaut VARENEc7428422006-01-11 13:59:53 -0700504
505/* For more insight of what's going on here, refer to PDC Procedures doc,
506 * Section PDC_STABLE */
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/**
Thibaut VARENEc7428422006-01-11 13:59:53 -0700509 * pdcs_size_read - Stable Storage size output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * @entry: An allocated and populated subsytem struct. We don't use it tho.
511 * @buf: The output buffer to write to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513static ssize_t
Thibaut VARENEc7428422006-01-11 13:59:53 -0700514pdcs_size_read(struct subsystem *entry, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 char *out = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (!entry || !buf)
519 return -EINVAL;
520
521 /* show the size of the stable storage */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700522 out += sprintf(out, "%ld\n", pdcs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Thibaut VARENEc7428422006-01-11 13:59:53 -0700524 return out - buf;
525}
526
527/**
528 * pdcs_auto_read - Stable Storage autoboot/search flag output.
529 * @entry: An allocated and populated subsytem struct. We don't use it tho.
530 * @buf: The output buffer to write to.
531 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
532 */
533static ssize_t
534pdcs_auto_read(struct subsystem *entry, char *buf, int knob)
535{
536 char *out = buf;
537 struct pdcspath_entry *pathentry;
Helge Deller67a5a592006-03-27 19:52:14 +0000538
Thibaut VARENEc7428422006-01-11 13:59:53 -0700539 if (!entry || !buf)
540 return -EINVAL;
541
542 /* Current flags are stored in primary boot path entry */
543 pathentry = &pdcspath_entry_primary;
544
545 read_lock(&pathentry->rw_lock);
546 out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
547 "On" : "Off");
548 read_unlock(&pathentry->rw_lock);
549
550 return out - buf;
551}
552
553/**
554 * pdcs_autoboot_read - Stable Storage autoboot flag output.
555 * @entry: An allocated and populated subsytem struct. We don't use it tho.
556 * @buf: The output buffer to write to.
557 */
558static inline ssize_t
559pdcs_autoboot_read(struct subsystem *entry, char *buf)
560{
561 return pdcs_auto_read(entry, buf, PF_AUTOBOOT);
562}
563
564/**
565 * pdcs_autosearch_read - Stable Storage autoboot flag output.
566 * @entry: An allocated and populated subsytem struct. We don't use it tho.
567 * @buf: The output buffer to write to.
568 */
569static inline ssize_t
570pdcs_autosearch_read(struct subsystem *entry, char *buf)
571{
572 return pdcs_auto_read(entry, buf, PF_AUTOSEARCH);
573}
574
575/**
576 * pdcs_timer_read - Stable Storage timer count output (in seconds).
577 * @entry: An allocated and populated subsytem struct. We don't use it tho.
578 * @buf: The output buffer to write to.
579 *
580 * The value of the timer field correponds to a number of seconds in powers of 2.
581 */
582static ssize_t
583pdcs_timer_read(struct subsystem *entry, char *buf)
584{
585 char *out = buf;
586 struct pdcspath_entry *pathentry;
587
588 if (!entry || !buf)
589 return -EINVAL;
590
591 /* Current flags are stored in primary boot path entry */
592 pathentry = &pdcspath_entry_primary;
593
594 /* print the timer value in seconds */
595 read_lock(&pathentry->rw_lock);
596 out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
597 (1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
598 read_unlock(&pathentry->rw_lock);
599
600 return out - buf;
601}
602
603/**
604 * pdcs_osid_read - Stable Storage OS ID register output.
605 * @entry: An allocated and populated subsytem struct. We don't use it tho.
606 * @buf: The output buffer to write to.
607 */
608static ssize_t
609pdcs_osid_read(struct subsystem *entry, char *buf)
610{
611 char *out = buf;
612 __u32 result;
613 char *tmpstr = NULL;
614
615 if (!entry || !buf)
616 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* get OSID */
619 if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
620 return -EIO;
621
622 /* the actual result is 16 bits away */
623 switch (result >> 16) {
624 case 0x0000: tmpstr = "No OS-dependent data"; break;
625 case 0x0001: tmpstr = "HP-UX dependent data"; break;
626 case 0x0002: tmpstr = "MPE-iX dependent data"; break;
627 case 0x0003: tmpstr = "OSF dependent data"; break;
628 case 0x0004: tmpstr = "HP-RT dependent data"; break;
629 case 0x0005: tmpstr = "Novell Netware dependent data"; break;
630 default: tmpstr = "Unknown"; break;
631 }
Thibaut VARENEc7428422006-01-11 13:59:53 -0700632 out += sprintf(out, "%s (0x%.4x)\n", tmpstr, (result >> 16));
633
634 return out - buf;
635}
636
637/**
638 * pdcs_fastsize_read - Stable Storage FastSize register output.
639 * @entry: An allocated and populated subsytem struct. We don't use it tho.
640 * @buf: The output buffer to write to.
641 *
642 * This register holds the amount of system RAM to be tested during boot sequence.
643 */
644static ssize_t
645pdcs_fastsize_read(struct subsystem *entry, char *buf)
646{
647 char *out = buf;
648 __u32 result;
649
650 if (!entry || !buf)
651 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /* get fast-size */
654 if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
655 return -EIO;
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if ((result & 0x0F) < 0x0E)
Randolph Chungabff7542005-10-21 22:57:13 -0400658 out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 else
660 out += sprintf(out, "All");
661 out += sprintf(out, "\n");
662
663 return out - buf;
664}
665
666/**
Thibaut VARENEc7428422006-01-11 13:59:53 -0700667 * pdcs_auto_write - This function handles autoboot/search flag modifying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * @entry: An allocated and populated subsytem struct. We don't use it tho.
669 * @buf: The input buffer to read from.
670 * @count: The number of bytes to be read.
Thibaut VARENEc7428422006-01-11 13:59:53 -0700671 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 *
Thibaut VARENEc7428422006-01-11 13:59:53 -0700673 * We will call this function to change the current autoboot flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 * We expect a precise syntax:
Thibaut VARENEc7428422006-01-11 13:59:53 -0700675 * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
677static ssize_t
Thibaut VARENEc7428422006-01-11 13:59:53 -0700678pdcs_auto_write(struct subsystem *entry, const char *buf, size_t count, int knob)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct pdcspath_entry *pathentry;
681 unsigned char flags;
682 char in[count+1], *temp;
683 char c;
684
685 if (!capable(CAP_SYS_ADMIN))
686 return -EACCES;
687
688 if (!entry || !buf || !count)
689 return -EINVAL;
690
691 /* We'll use a local copy of buf */
692 memset(in, 0, count+1);
693 strncpy(in, buf, count);
694
695 /* Current flags are stored in primary boot path entry */
696 pathentry = &pdcspath_entry_primary;
697
698 /* Be nice to the existing flag record */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700699 read_lock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 flags = pathentry->devpath.flags;
Thibaut VARENEc7428422006-01-11 13:59:53 -0700701 read_unlock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
704
705 temp = in;
706
707 while (*temp && isspace(*temp))
708 temp++;
709
710 c = *temp++ - '0';
711 if ((c != 0) && (c != 1))
712 goto parse_error;
713 if (c == 0)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700714 flags &= ~knob;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 else
Thibaut VARENEc7428422006-01-11 13:59:53 -0700716 flags |= knob;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
719
720 /* So far so good, let's get in deep */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700721 write_lock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /* Change the path entry flags first */
724 pathentry->devpath.flags = flags;
725
726 /* Now, dive in. Write back to the hardware */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700727 pdcspath_store(pathentry);
728 write_unlock(&pathentry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Thibaut VARENEc7428422006-01-11 13:59:53 -0700730 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
731 (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
732 (flags & knob) ? "On" : "Off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 return count;
735
736parse_error:
Thibaut VARENEc7428422006-01-11 13:59:53 -0700737 printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EINVAL;
739}
740
Thibaut VARENEc7428422006-01-11 13:59:53 -0700741/**
742 * pdcs_autoboot_write - This function handles autoboot flag modifying.
743 * @entry: An allocated and populated subsytem struct. We don't use it tho.
744 * @buf: The input buffer to read from.
745 * @count: The number of bytes to be read.
746 *
747 * We will call this function to change the current boot flags.
748 * We expect a precise syntax:
749 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
750 */
751static inline ssize_t
752pdcs_autoboot_write(struct subsystem *entry, const char *buf, size_t count)
753{
754 return pdcs_auto_write(entry, buf, count, PF_AUTOBOOT);
755}
756
757/**
758 * pdcs_autosearch_write - This function handles autosearch flag modifying.
759 * @entry: An allocated and populated subsytem struct. We don't use it tho.
760 * @buf: The input buffer to read from.
761 * @count: The number of bytes to be read.
762 *
763 * We will call this function to change the current boot flags.
764 * We expect a precise syntax:
765 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
766 */
767static inline ssize_t
768pdcs_autosearch_write(struct subsystem *entry, const char *buf, size_t count)
769{
770 return pdcs_auto_write(entry, buf, count, PF_AUTOSEARCH);
771}
772
773/* The remaining attributes. */
774static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
775static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
776static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
777static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
778static PDCS_ATTR(osid, 0400, pdcs_osid_read, NULL);
779static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781static struct subsys_attribute *pdcs_subsys_attrs[] = {
Thibaut VARENEc7428422006-01-11 13:59:53 -0700782 &pdcs_attr_size,
783 &pdcs_attr_autoboot,
784 &pdcs_attr_autosearch,
785 &pdcs_attr_timer,
786 &pdcs_attr_osid,
787 &pdcs_attr_fastsize,
788 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789};
790
791static decl_subsys(paths, &ktype_pdcspath, NULL);
Thibaut VARENEc7428422006-01-11 13:59:53 -0700792static decl_subsys(stable, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794/**
795 * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
796 *
797 * It creates kobjects corresponding to each path entry with nice sysfs
798 * links to the real device. This is where the magic takes place: when
799 * registering the subsystem attributes during module init, each kobject hereby
800 * created will show in the sysfs tree as a folder containing files as defined
801 * by path_subsys_attr[].
802 */
803static inline int __init
804pdcs_register_pathentries(void)
805{
806 unsigned short i;
807 struct pdcspath_entry *entry;
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500808 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Thibaut VARENEc7428422006-01-11 13:59:53 -0700810 /* Initialize the entries rw_lock before anything else */
811 for (i = 0; (entry = pdcspath_entries[i]); i++)
812 rwlock_init(&entry->rw_lock);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 for (i = 0; (entry = pdcspath_entries[i]); i++) {
Thibaut VARENEc7428422006-01-11 13:59:53 -0700815 write_lock(&entry->rw_lock);
816 err = pdcspath_fetch(entry);
817 write_unlock(&entry->rw_lock);
818
819 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 continue;
821
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500822 if ((err = kobject_set_name(&entry->kobj, "%s", entry->name)))
823 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 kobj_set_kset_s(entry, paths_subsys);
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500825 if ((err = kobject_register(&entry->kobj)))
826 return err;
827
828 /* kobject is now registered */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700829 write_lock(&entry->rw_lock);
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500830 entry->ready = 2;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* Add a nice symlink to the real device */
Thibaut VARENEc7428422006-01-11 13:59:53 -0700833 if (entry->dev)
834 sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
835
836 write_unlock(&entry->rw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
839 return 0;
840}
841
842/**
843 * pdcs_unregister_pathentries - Routine called when unregistering the module.
844 */
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500845static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846pdcs_unregister_pathentries(void)
847{
848 unsigned short i;
849 struct pdcspath_entry *entry;
850
Thibaut VARENEc7428422006-01-11 13:59:53 -0700851 for (i = 0; (entry = pdcspath_entries[i]); i++) {
852 read_lock(&entry->rw_lock);
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500853 if (entry->ready >= 2)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700854 kobject_unregister(&entry->kobj);
855 read_unlock(&entry->rw_lock);
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859/*
Thibaut VARENEc7428422006-01-11 13:59:53 -0700860 * For now we register the stable subsystem with the firmware subsystem
861 * and the paths subsystem with the stable subsystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
863static int __init
864pdc_stable_init(void)
865{
866 struct subsys_attribute *attr;
867 int i, rc = 0, error = 0;
868
869 /* find the size of the stable storage */
870 if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
871 return -ENODEV;
872
Thibaut VARENEc7428422006-01-11 13:59:53 -0700873 /* make sure we have enough data */
874 if (pdcs_size < 96)
875 return -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Thibaut VARENEc7428422006-01-11 13:59:53 -0700877 printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
878
879 /* For now we'll register the stable subsys within this driver */
880 if ((rc = firmware_register(&stable_subsys)))
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500881 goto fail_firmreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Thibaut VARENEc7428422006-01-11 13:59:53 -0700883 /* Don't forget the root entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 for (i = 0; (attr = pdcs_subsys_attrs[i]) && !error; i++)
885 if (attr->show)
Thibaut VARENEc7428422006-01-11 13:59:53 -0700886 error = subsys_create_file(&stable_subsys, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Thibaut VARENEc7428422006-01-11 13:59:53 -0700888 /* register the paths subsys as a subsystem of stable subsys */
889 kset_set_kset_s(&paths_subsys, stable_subsys);
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500890 if ((rc= subsystem_register(&paths_subsys)))
891 goto fail_subsysreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* now we create all "files" for the paths subsys */
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500894 if ((rc = pdcs_register_pathentries()))
895 goto fail_pdcsreg;
896
897 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500899fail_pdcsreg:
900 pdcs_unregister_pathentries();
901 subsystem_unregister(&paths_subsys);
902
903fail_subsysreg:
Thibaut VARENEc7428422006-01-11 13:59:53 -0700904 firmware_unregister(&stable_subsys);
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500905
906fail_firmreg:
Thibaut VARENEc7428422006-01-11 13:59:53 -0700907 printk(KERN_INFO PDCS_PREFIX " bailing out\n");
Thibaut VARENE4b991da2006-01-10 20:48:01 -0500908 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911static void __exit
912pdc_stable_exit(void)
913{
914 pdcs_unregister_pathentries();
915 subsystem_unregister(&paths_subsys);
916
Thibaut VARENEc7428422006-01-11 13:59:53 -0700917 firmware_unregister(&stable_subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920
921module_init(pdc_stable_init);
922module_exit(pdc_stable_exit);