pdc_stable.c 26.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/* 
 *    Interfaces to retrieve and set PDC Stable options (firmware)
 *
4
 *    Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *    DEV NOTE: the PDC Procedures reference states that:
 *    "A minimum of 96 bytes of Stable Storage is required. Providing more than
 *    96 bytes of Stable Storage is optional [...]. Failure to provide the
 *    optional locations from 96 to 192 results in the loss of certain
 *    functionality during boot."
 *
 *    Since locations between 96 and 192 are the various paths, most (if not
 *    all) PA-RISC machines should have them. Anyway, for safety reasons, the
29
 *    following code can deal with just 96 bytes of Stable Storage, and all
Linus Torvalds's avatar
Linus Torvalds committed
30 31 32 33
 *    sizes between 96 and 192 bytes (provided they are multiple of struct
 *    device_path size, eg: 128, 160 and 192) to provide full information.
 *    The code makes no use of data above 192 bytes. One last word: there's one
 *    path we can always count on: the primary path.
34 35 36 37 38 39 40 41
 *
 *    The current policy wrt file permissions is:
 *	- write: root only
 *	- read: (reading triggers PDC calls) ? root only : everyone
 *    The rationale is that PDC calls could hog (DoS) the machine.
 *
 *	TODO:
 *	- timer/fastsize write calls
Linus Torvalds's avatar
Linus Torvalds committed
42 43 44 45 46 47 48 49 50 51 52 53 54
 */

#undef PDCS_DEBUG
#ifdef PDCS_DEBUG
#define DPRINTK(fmt, args...)	printk(KERN_DEBUG fmt, ## args)
#else
#define DPRINTK(fmt, args...)
#endif

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/string.h>
55
#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58 59 60
#include <linux/ctype.h>
#include <linux/sysfs.h>
#include <linux/kobject.h>
#include <linux/device.h>
#include <linux/errno.h>
61
#include <linux/spinlock.h>
Linus Torvalds's avatar
Linus Torvalds committed
62 63 64 65 66 67

#include <asm/pdc.h>
#include <asm/page.h>
#include <asm/uaccess.h>
#include <asm/hardware.h>

68 69
#define PDCS_VERSION	"0.22"
#define PDCS_PREFIX	"PDC Stable Storage"
Linus Torvalds's avatar
Linus Torvalds committed
70 71 72 73 74 75 76 77 78 79 80 81 82

#define PDCS_ADDR_PPRI	0x00
#define PDCS_ADDR_OSID	0x40
#define PDCS_ADDR_FSIZ	0x5C
#define PDCS_ADDR_PCON	0x60
#define PDCS_ADDR_PALT	0x80
#define PDCS_ADDR_PKBD	0xA0

MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
MODULE_LICENSE("GPL");
MODULE_VERSION(PDCS_VERSION);

83
/* holds Stable Storage size. Initialized once and for all, no lock needed */
84
static unsigned long pdcs_size __read_mostly;
Linus Torvalds's avatar
Linus Torvalds committed
85 86 87

/* This struct defines what we need to deal with a parisc pdc path entry */
struct pdcspath_entry {
88
	rwlock_t rw_lock;		/* to protect path entry access */
Linus Torvalds's avatar
Linus Torvalds committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	short ready;			/* entry record is valid if != 0 */
	unsigned long addr;		/* entry address in stable storage */
	char *name;			/* entry name */
	struct device_path devpath;	/* device path in parisc representation */
	struct device *dev;		/* corresponding device */
	struct kobject kobj;
};

struct pdcspath_attribute {
	struct attribute attr;
	ssize_t (*show)(struct pdcspath_entry *entry, char *buf);
	ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count);
};

#define PDCSPATH_ENTRY(_addr, _name) \
struct pdcspath_entry pdcspath_entry_##_name = { \
	.ready = 0, \
	.addr = _addr, \
	.name = __stringify(_name), \
};

#define PDCS_ATTR(_name, _mode, _show, _store) \
struct subsys_attribute pdcs_attr_##_name = { \
	.attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
	.show = _show, \
	.store = _store, \
};

#define PATHS_ATTR(_name, _mode, _show, _store) \
struct pdcspath_attribute paths_attr_##_name = { \
	.attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
	.show = _show, \
	.store = _store, \
};

#define to_pdcspath_attribute(_attr) container_of(_attr, struct pdcspath_attribute, attr)
#define to_pdcspath_entry(obj)  container_of(obj, struct pdcspath_entry, kobj)

/**
 * pdcspath_fetch - This function populates the path entry structs.
 * @entry: A pointer to an allocated pdcspath_entry.
 * 
 * The general idea is that you don't read from the Stable Storage every time
 * you access the files provided by the facilites. We store a copy of the
 * content of the stable storage WRT various paths in these structs. We read
 * these structs when reading the files, and we will write to these structs when
 * writing to the files, and only then write them back to the Stable Storage.
136 137
 *
 * This function expects to be called with @entry->rw_lock write-hold.
Linus Torvalds's avatar
Linus Torvalds committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
 */
static int
pdcspath_fetch(struct pdcspath_entry *entry)
{
	struct device_path *devpath;

	if (!entry)
		return -EINVAL;

	devpath = &entry->devpath;
	
	DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
			entry, devpath, entry->addr);

	/* addr, devpath and count must be word aligned */
	if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
		return -EIO;
		
	/* Find the matching device.
	   NOTE: hardware_path overlays with device_path, so the nice cast can
	   be used */
	entry->dev = hwpath_to_device((struct hardware_path *)devpath);

	entry->ready = 1;
	
	DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
	
	return 0;
}

/**
 * pdcspath_store - This function writes a path to stable storage.
 * @entry: A pointer to an allocated pdcspath_entry.
 * 
 * It can be used in two ways: either by passing it a preset devpath struct
 * containing an already computed hardware path, or by passing it a device
 * pointer, from which it'll find out the corresponding hardware path.
 * For now we do not handle the case where there's an error in writing to the
 * Stable Storage area, so you'd better not mess up the data :P
177 178
 *
 * This function expects to be called with @entry->rw_lock write-hold.
Linus Torvalds's avatar
Linus Torvalds committed
179
 */
180
static void
Linus Torvalds's avatar
Linus Torvalds committed
181 182 183 184
pdcspath_store(struct pdcspath_entry *entry)
{
	struct device_path *devpath;

185
	BUG_ON(!entry);
Linus Torvalds's avatar
Linus Torvalds committed
186 187 188 189 190 191 192 193

	devpath = &entry->devpath;
	
	/* We expect the caller to set the ready flag to 0 if the hardware
	   path struct provided is invalid, so that we know we have to fill it.
	   First case, we don't have a preset hwpath... */
	if (!entry->ready) {
		/* ...but we have a device, map it */
194 195
		BUG_ON(!entry->dev);
		device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199 200 201 202 203 204 205 206
	}
	/* else, we expect the provided hwpath to be valid. */
	
	DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
			entry, devpath, entry->addr);

	/* addr, devpath and count must be word aligned */
	if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK) {
		printk(KERN_ERR "%s: an error occured when writing to PDC.\n"
				"It is likely that the Stable Storage data has been corrupted.\n"
				"Please check it carefully upon next reboot.\n", __func__);
207
		WARN_ON(1);
Linus Torvalds's avatar
Linus Torvalds committed
208 209
	}
		
210 211
	/* kobject is already registered */
	entry->ready = 2;
Linus Torvalds's avatar
Linus Torvalds committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	
	DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
}

/**
 * pdcspath_hwpath_read - This function handles hardware path pretty printing.
 * @entry: An allocated and populated pdscpath_entry struct.
 * @buf: The output buffer to write to.
 * 
 * We will call this function to format the output of the hwpath attribute file.
 */
static ssize_t
pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
{
	char *out = buf;
	struct device_path *devpath;
228
	short i;
Linus Torvalds's avatar
Linus Torvalds committed
229 230 231 232

	if (!entry || !buf)
		return -EINVAL;

233
	read_lock(&entry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
234
	devpath = &entry->devpath;
235 236
	i = entry->ready;
	read_unlock(&entry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
237

238
	if (!i)	/* entry is not ready */
Linus Torvalds's avatar
Linus Torvalds committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
		return -ENODATA;
	
	for (i = 0; i < 6; i++) {
		if (devpath->bc[i] >= 128)
			continue;
		out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
	}
	out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
	
	return out - buf;
}

/**
 * pdcspath_hwpath_write - This function handles hardware path modifying.
 * @entry: An allocated and populated pdscpath_entry struct.
 * @buf: The input buffer to read from.
 * @count: The number of bytes to be read.
 * 
 * We will call this function to change the current hardware path.
 * Hardware paths are to be given '/'-delimited, without brackets.
259
 * We make sure that the provided path actually maps to an existing
Linus Torvalds's avatar
Linus Torvalds committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
 * device, BUT nothing would prevent some foolish user to set the path to some
 * PCI bridge or even a CPU...
 * A better work around would be to make sure we are at the end of a device tree
 * for instance, but it would be IMHO beyond the simple scope of that driver.
 * The aim is to provide a facility. Data correctness is left to userland.
 */
static ssize_t
pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
{
	struct hardware_path hwpath;
	unsigned short i;
	char in[count+1], *temp;
	struct device *dev;

	if (!entry || !buf || !count)
		return -EINVAL;

	/* We'll use a local copy of buf */
	memset(in, 0, count+1);
	strncpy(in, buf, count);
	
	/* Let's clean up the target. 0xff is a blank pattern */
	memset(&hwpath, 0xff, sizeof(hwpath));
	
	/* First, pick the mod field (the last one of the input string) */
	if (!(temp = strrchr(in, '/')))
		return -EINVAL;
			
	hwpath.mod = simple_strtoul(temp+1, NULL, 10);
	in[temp-in] = '\0';	/* truncate the remaining string. just precaution */
	DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
	
	/* Then, loop for each delimiter, making sure we don't have too many.
	   we write the bc fields in a down-top way. No matter what, we stop
	   before writing the last field. If there are too many fields anyway,
	   then the user is a moron and it'll be caught up later when we'll
	   check the consistency of the given hwpath. */
	for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
		hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
		in[temp-in] = '\0';
		DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
	}
	
	/* Store the final field */		
	hwpath.bc[i] = simple_strtoul(in, NULL, 10);
	DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
	
	/* Now we check that the user isn't trying to lure us */
	if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
		printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
			"hardware path: %s\n", __func__, entry->name, buf);
		return -EINVAL;
	}
	
	/* So far so good, let's get in deep */
315
	write_lock(&entry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
316 317 318 319
	entry->ready = 0;
	entry->dev = dev;
	
	/* Now, dive in. Write back to the hardware */
320
	pdcspath_store(entry);
Linus Torvalds's avatar
Linus Torvalds committed
321 322 323 324
	
	/* Update the symlink to the real device */
	sysfs_remove_link(&entry->kobj, "device");
	sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
325
	write_unlock(&entry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
326
	
327
	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
Linus Torvalds's avatar
Linus Torvalds committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
		entry->name, buf);
	
	return count;
}

/**
 * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
 * @entry: An allocated and populated pdscpath_entry struct.
 * @buf: The output buffer to write to.
 * 
 * We will call this function to format the output of the layer attribute file.
 */
static ssize_t
pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
{
	char *out = buf;
	struct device_path *devpath;
345
	short i;
Linus Torvalds's avatar
Linus Torvalds committed
346 347 348 349

	if (!entry || !buf)
		return -EINVAL;
	
350
	read_lock(&entry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
351
	devpath = &entry->devpath;
352 353
	i = entry->ready;
	read_unlock(&entry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
354

355
	if (!i)	/* entry is not ready */
Linus Torvalds's avatar
Linus Torvalds committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
		return -ENODATA;
	
	for (i = 0; devpath->layers[i] && (likely(i < 6)); i++)
		out += sprintf(out, "%u ", devpath->layers[i]);

	out += sprintf(out, "\n");
	
	return out - buf;
}

/**
 * pdcspath_layer_write - This function handles extended layer modifying.
 * @entry: An allocated and populated pdscpath_entry struct.
 * @buf: The input buffer to read from.
 * @count: The number of bytes to be read.
 * 
 * We will call this function to change the current layer value.
 * Layers are to be given '.'-delimited, without brackets.
 * XXX beware we are far less checky WRT input data provided than for hwpath.
 * Potential harm can be done, since there's no way to check the validity of
 * the layer fields.
 */
static ssize_t
pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
{
	unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
	unsigned short i;
	char in[count+1], *temp;

	if (!entry || !buf || !count)
		return -EINVAL;

	/* We'll use a local copy of buf */
	memset(in, 0, count+1);
	strncpy(in, buf, count);
	
	/* Let's clean up the target. 0 is a blank pattern */
	memset(&layers, 0, sizeof(layers));
	
	/* First, pick the first layer */
	if (unlikely(!isdigit(*in)))
		return -EINVAL;
	layers[0] = simple_strtoul(in, NULL, 10);
	DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
	
	temp = in;
	for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
		if (unlikely(!isdigit(*(++temp))))
			return -EINVAL;
		layers[i] = simple_strtoul(temp, NULL, 10);
		DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
	}
		
	/* So far so good, let's get in deep */
410
	write_lock(&entry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
411 412 413 414 415 416
	
	/* First, overwrite the current layers with the new ones, not touching
	   the hardware path. */
	memcpy(&entry->devpath.layers, &layers, sizeof(layers));
	
	/* Now, dive in. Write back to the hardware */
417 418
	pdcspath_store(entry);
	write_unlock(&entry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
419
	
420
	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
Linus Torvalds's avatar
Linus Torvalds committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
		entry->name, buf);
	
	return count;
}

/**
 * pdcspath_attr_show - Generic read function call wrapper.
 * @kobj: The kobject to get info from.
 * @attr: The attribute looked upon.
 * @buf: The output buffer.
 */
static ssize_t
pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
	struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
	struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
	ssize_t ret = 0;

	if (pdcs_attr->show)
		ret = pdcs_attr->show(entry, buf);

	return ret;
}

/**
 * pdcspath_attr_store - Generic write function call wrapper.
 * @kobj: The kobject to write info to.
 * @attr: The attribute to be modified.
 * @buf: The input buffer.
 * @count: The size of the buffer.
 */
static ssize_t
pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
			const char *buf, size_t count)
{
	struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
	struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
	ssize_t ret = 0;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	if (pdcs_attr->store)
		ret = pdcs_attr->store(entry, buf, count);

	return ret;
}

static struct sysfs_ops pdcspath_attr_ops = {
	.show = pdcspath_attr_show,
	.store = pdcspath_attr_store,
};

/* These are the two attributes of any PDC path. */
475 476
static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
Linus Torvalds's avatar
Linus Torvalds committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

static struct attribute *paths_subsys_attrs[] = {
	&paths_attr_hwpath.attr,
	&paths_attr_layer.attr,
	NULL,
};

/* Specific kobject type for our PDC paths */
static struct kobj_type ktype_pdcspath = {
	.sysfs_ops = &pdcspath_attr_ops,
	.default_attrs = paths_subsys_attrs,
};

/* We hard define the 4 types of path we expect to find */
static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);

/* An array containing all PDC paths we will deal with */
static struct pdcspath_entry *pdcspath_entries[] = {
	&pdcspath_entry_primary,
	&pdcspath_entry_alternative,
	&pdcspath_entry_console,
	&pdcspath_entry_keyboard,
	NULL,
};

505 506 507 508

/* For more insight of what's going on here, refer to PDC Procedures doc,
 * Section PDC_STABLE */

Linus Torvalds's avatar
Linus Torvalds committed
509
/**
510
 * pdcs_size_read - Stable Storage size output.
Linus Torvalds's avatar
Linus Torvalds committed
511 512 513 514
 * @entry: An allocated and populated subsytem struct. We don't use it tho.
 * @buf: The output buffer to write to.
 */
static ssize_t
515
pdcs_size_read(struct subsystem *entry, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
516 517 518 519 520 521 522
{
	char *out = buf;
	
	if (!entry || !buf)
		return -EINVAL;
		
	/* show the size of the stable storage */
523
	out += sprintf(out, "%ld\n", pdcs_size);
Linus Torvalds's avatar
Linus Torvalds committed
524

525 526 527 528 529 530 531 532 533 534 535 536 537 538
	return out - buf;
}

/**
 * pdcs_auto_read - Stable Storage autoboot/search flag output.
 * @entry: An allocated and populated subsytem struct. We don't use it tho.
 * @buf: The output buffer to write to.
 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
 */
static ssize_t
pdcs_auto_read(struct subsystem *entry, char *buf, int knob)
{
	char *out = buf;
	struct pdcspath_entry *pathentry;
Linus Torvalds's avatar
Linus Torvalds committed
539
	
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
	if (!entry || !buf)
		return -EINVAL;

	/* Current flags are stored in primary boot path entry */
	pathentry = &pdcspath_entry_primary;

	read_lock(&pathentry->rw_lock);
	out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
					"On" : "Off");
	read_unlock(&pathentry->rw_lock);

	return out - buf;
}

/**
 * pdcs_autoboot_read - Stable Storage autoboot flag output.
 * @entry: An allocated and populated subsytem struct. We don't use it tho.
 * @buf: The output buffer to write to.
 */
static inline ssize_t
pdcs_autoboot_read(struct subsystem *entry, char *buf)
{
	return pdcs_auto_read(entry, buf, PF_AUTOBOOT);
}

/**
 * pdcs_autosearch_read - Stable Storage autoboot flag output.
 * @entry: An allocated and populated subsytem struct. We don't use it tho.
 * @buf: The output buffer to write to.
 */
static inline ssize_t
pdcs_autosearch_read(struct subsystem *entry, char *buf)
{
	return pdcs_auto_read(entry, buf, PF_AUTOSEARCH);
}

/**
 * pdcs_timer_read - Stable Storage timer count output (in seconds).
 * @entry: An allocated and populated subsytem struct. We don't use it tho.
 * @buf: The output buffer to write to.
 *
 * The value of the timer field correponds to a number of seconds in powers of 2.
 */
static ssize_t
pdcs_timer_read(struct subsystem *entry, char *buf)
{
	char *out = buf;
	struct pdcspath_entry *pathentry;

	if (!entry || !buf)
		return -EINVAL;

	/* Current flags are stored in primary boot path entry */
	pathentry = &pdcspath_entry_primary;

	/* print the timer value in seconds */
	read_lock(&pathentry->rw_lock);
	out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
				(1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
	read_unlock(&pathentry->rw_lock);

	return out - buf;
}

/**
 * pdcs_osid_read - Stable Storage OS ID register output.
 * @entry: An allocated and populated subsytem struct. We don't use it tho.
 * @buf: The output buffer to write to.
 */
static ssize_t
pdcs_osid_read(struct subsystem *entry, char *buf)
{
	char *out = buf;
	__u32 result;
	char *tmpstr = NULL;

	if (!entry || !buf)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

	/* get OSID */
	if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
		return -EIO;

	/* the actual result is 16 bits away */
	switch (result >> 16) {
		case 0x0000:	tmpstr = "No OS-dependent data"; break;
		case 0x0001:	tmpstr = "HP-UX dependent data"; break;
		case 0x0002:	tmpstr = "MPE-iX dependent data"; break;
		case 0x0003:	tmpstr = "OSF dependent data"; break;
		case 0x0004:	tmpstr = "HP-RT dependent data"; break;
		case 0x0005:	tmpstr = "Novell Netware dependent data"; break;
		default:	tmpstr = "Unknown"; break;
	}
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
	out += sprintf(out, "%s (0x%.4x)\n", tmpstr, (result >> 16));

	return out - buf;
}

/**
 * pdcs_fastsize_read - Stable Storage FastSize register output.
 * @entry: An allocated and populated subsytem struct. We don't use it tho.
 * @buf: The output buffer to write to.
 *
 * This register holds the amount of system RAM to be tested during boot sequence.
 */
static ssize_t
pdcs_fastsize_read(struct subsystem *entry, char *buf)
{
	char *out = buf;
	__u32 result;

	if (!entry || !buf)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
653 654 655 656 657 658

	/* get fast-size */
	if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
		return -EIO;

	if ((result & 0x0F) < 0x0E)
659
		out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
Linus Torvalds's avatar
Linus Torvalds committed
660 661 662 663 664 665 666 667
	else
		out += sprintf(out, "All");
	out += sprintf(out, "\n");
	
	return out - buf;
}

/**
668
 * pdcs_auto_write - This function handles autoboot/search flag modifying.
Linus Torvalds's avatar
Linus Torvalds committed
669 670 671
 * @entry: An allocated and populated subsytem struct. We don't use it tho.
 * @buf: The input buffer to read from.
 * @count: The number of bytes to be read.
672
 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
Linus Torvalds's avatar
Linus Torvalds committed
673
 * 
674
 * We will call this function to change the current autoboot flag.
Linus Torvalds's avatar
Linus Torvalds committed
675
 * We expect a precise syntax:
676
 *	\"n\" (n == 0 or 1) to toggle AutoBoot Off or On
Linus Torvalds's avatar
Linus Torvalds committed
677 678
 */
static ssize_t
679
pdcs_auto_write(struct subsystem *entry, const char *buf, size_t count, int knob)
Linus Torvalds's avatar
Linus Torvalds committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
{
	struct pdcspath_entry *pathentry;
	unsigned char flags;
	char in[count+1], *temp;
	char c;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	if (!entry || !buf || !count)
		return -EINVAL;

	/* We'll use a local copy of buf */
	memset(in, 0, count+1);
	strncpy(in, buf, count);

	/* Current flags are stored in primary boot path entry */
	pathentry = &pdcspath_entry_primary;
	
	/* Be nice to the existing flag record */
700
	read_lock(&pathentry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
701
	flags = pathentry->devpath.flags;
702
	read_unlock(&pathentry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
703 704 705 706 707 708 709 710 711 712 713 714
	
	DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
			
	temp = in;
	
	while (*temp && isspace(*temp))
		temp++;
	
	c = *temp++ - '0';
	if ((c != 0) && (c != 1))
		goto parse_error;
	if (c == 0)
715
		flags &= ~knob;
Linus Torvalds's avatar
Linus Torvalds committed
716
	else
717
		flags |= knob;
Linus Torvalds's avatar
Linus Torvalds committed
718 719 720 721
	
	DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
		
	/* So far so good, let's get in deep */
722
	write_lock(&pathentry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
723 724 725 726 727
	
	/* Change the path entry flags first */
	pathentry->devpath.flags = flags;
		
	/* Now, dive in. Write back to the hardware */
728 729
	pdcspath_store(pathentry);
	write_unlock(&pathentry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
730
	
731 732 733
	printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
		(knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
		(flags & knob) ? "On" : "Off");
Linus Torvalds's avatar
Linus Torvalds committed
734 735 736 737
	
	return count;

parse_error:
738
	printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
739 740 741
	return -EINVAL;
}

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
/**
 * pdcs_autoboot_write - This function handles autoboot flag modifying.
 * @entry: An allocated and populated subsytem struct. We don't use it tho.
 * @buf: The input buffer to read from.
 * @count: The number of bytes to be read.
 *
 * We will call this function to change the current boot flags.
 * We expect a precise syntax:
 *	\"n\" (n == 0 or 1) to toggle AutoSearch Off or On
 */
static inline ssize_t
pdcs_autoboot_write(struct subsystem *entry, const char *buf, size_t count)
{
	return pdcs_auto_write(entry, buf, count, PF_AUTOBOOT);
}

/**
 * pdcs_autosearch_write - This function handles autosearch flag modifying.
 * @entry: An allocated and populated subsytem struct. We don't use it tho.
 * @buf: The input buffer to read from.
 * @count: The number of bytes to be read.
 *
 * We will call this function to change the current boot flags.
 * We expect a precise syntax:
 *	\"n\" (n == 0 or 1) to toggle AutoSearch Off or On
 */
static inline ssize_t
pdcs_autosearch_write(struct subsystem *entry, const char *buf, size_t count)
{
	return pdcs_auto_write(entry, buf, count, PF_AUTOSEARCH);
}

/* The remaining attributes. */
static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
static PDCS_ATTR(osid, 0400, pdcs_osid_read, NULL);
static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
781 782

static struct subsys_attribute *pdcs_subsys_attrs[] = {
783 784 785 786 787 788 789
	&pdcs_attr_size,
	&pdcs_attr_autoboot,
	&pdcs_attr_autosearch,
	&pdcs_attr_timer,
	&pdcs_attr_osid,
	&pdcs_attr_fastsize,
	NULL,
Linus Torvalds's avatar
Linus Torvalds committed
790 791 792
};

static decl_subsys(paths, &ktype_pdcspath, NULL);
793
static decl_subsys(stable, NULL, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

/**
 * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
 * 
 * It creates kobjects corresponding to each path entry with nice sysfs
 * links to the real device. This is where the magic takes place: when
 * registering the subsystem attributes during module init, each kobject hereby
 * created will show in the sysfs tree as a folder containing files as defined
 * by path_subsys_attr[].
 */
static inline int __init
pdcs_register_pathentries(void)
{
	unsigned short i;
	struct pdcspath_entry *entry;
809
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
810
	
811 812 813 814
	/* Initialize the entries rw_lock before anything else */
	for (i = 0; (entry = pdcspath_entries[i]); i++)
		rwlock_init(&entry->rw_lock);

Linus Torvalds's avatar
Linus Torvalds committed
815
	for (i = 0; (entry = pdcspath_entries[i]); i++) {
816 817 818 819 820
		write_lock(&entry->rw_lock);
		err = pdcspath_fetch(entry);
		write_unlock(&entry->rw_lock);

		if (err < 0)
Linus Torvalds's avatar
Linus Torvalds committed
821 822
			continue;

823 824
		if ((err = kobject_set_name(&entry->kobj, "%s", entry->name)))
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
825
		kobj_set_kset_s(entry, paths_subsys);
826 827 828 829
		if ((err = kobject_register(&entry->kobj)))
			return err;
		
		/* kobject is now registered */
830
		write_lock(&entry->rw_lock);
831 832
		entry->ready = 2;
		
Linus Torvalds's avatar
Linus Torvalds committed
833
		/* Add a nice symlink to the real device */
834 835 836 837
		if (entry->dev)
			sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");

		write_unlock(&entry->rw_lock);
Linus Torvalds's avatar
Linus Torvalds committed
838 839 840 841 842 843 844 845
	}
	
	return 0;
}

/**
 * pdcs_unregister_pathentries - Routine called when unregistering the module.
 */
846
static inline void
Linus Torvalds's avatar
Linus Torvalds committed
847 848 849 850 851
pdcs_unregister_pathentries(void)
{
	unsigned short i;
	struct pdcspath_entry *entry;
	
852 853
	for (i = 0; (entry = pdcspath_entries[i]); i++) {
		read_lock(&entry->rw_lock);
854
		if (entry->ready >= 2)
855 856 857
			kobject_unregister(&entry->kobj);
		read_unlock(&entry->rw_lock);
	}
Linus Torvalds's avatar
Linus Torvalds committed
858 859 860
}

/*
861 862
 * For now we register the stable subsystem with the firmware subsystem
 * and the paths subsystem with the stable subsystem
Linus Torvalds's avatar
Linus Torvalds committed
863 864 865 866 867 868 869 870 871 872 873
 */
static int __init
pdc_stable_init(void)
{
	struct subsys_attribute *attr;
	int i, rc = 0, error = 0;

	/* find the size of the stable storage */
	if (pdc_stable_get_size(&pdcs_size) != PDC_OK) 
		return -ENODEV;

874 875 876 877 878
	/* make sure we have enough data */
	if (pdcs_size < 96)
		return -ENODATA;

	printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
Linus Torvalds's avatar
Linus Torvalds committed
879

880 881
	/* For now we'll register the stable subsys within this driver */
	if ((rc = firmware_register(&stable_subsys)))
882
		goto fail_firmreg;
Linus Torvalds's avatar
Linus Torvalds committed
883

884
	/* Don't forget the root entries */
Linus Torvalds's avatar
Linus Torvalds committed
885 886
	for (i = 0; (attr = pdcs_subsys_attrs[i]) && !error; i++)
		if (attr->show)
887
			error = subsys_create_file(&stable_subsys, attr);
Linus Torvalds's avatar
Linus Torvalds committed
888
	
889 890
	/* register the paths subsys as a subsystem of stable subsys */
	kset_set_kset_s(&paths_subsys, stable_subsys);
891 892
	if ((rc= subsystem_register(&paths_subsys)))
		goto fail_subsysreg;
Linus Torvalds's avatar
Linus Torvalds committed
893 894

	/* now we create all "files" for the paths subsys */
895 896 897 898
	if ((rc = pdcs_register_pathentries()))
		goto fail_pdcsreg;

	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
899
	
900 901 902 903 904
fail_pdcsreg:
	pdcs_unregister_pathentries();
	subsystem_unregister(&paths_subsys);
	
fail_subsysreg:
905
	firmware_unregister(&stable_subsys);
906 907
	
fail_firmreg:
908
	printk(KERN_INFO PDCS_PREFIX " bailing out\n");
909
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
910 911 912 913 914 915 916 917
}

static void __exit
pdc_stable_exit(void)
{
	pdcs_unregister_pathentries();
	subsystem_unregister(&paths_subsys);

918
	firmware_unregister(&stable_subsys);
Linus Torvalds's avatar
Linus Torvalds committed
919 920 921 922 923
}


module_init(pdc_stable_init);
module_exit(pdc_stable_exit);