Thomas Gleixner | 4505153 | 2019-05-29 16:57:47 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Interfaces to retrieve and set PDC Stable options (firmware) |
| 4 | * |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 5 | * Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * DEV NOTE: the PDC Procedures reference states that: |
| 8 | * "A minimum of 96 bytes of Stable Storage is required. Providing more than |
| 9 | * 96 bytes of Stable Storage is optional [...]. Failure to provide the |
| 10 | * optional locations from 96 to 192 results in the loss of certain |
| 11 | * functionality during boot." |
| 12 | * |
| 13 | * Since locations between 96 and 192 are the various paths, most (if not |
| 14 | * all) PA-RISC machines should have them. Anyway, for safety reasons, the |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 15 | * following code can deal with just 96 bytes of Stable Storage, and all |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * sizes between 96 and 192 bytes (provided they are multiple of struct |
| 17 | * device_path size, eg: 128, 160 and 192) to provide full information. |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 18 | * One last word: there's one path we can always count on: the primary path. |
| 19 | * Anything above 224 bytes is used for 'osdep2' OS-dependent storage area. |
| 20 | * |
| 21 | * The first OS-dependent area should always be available. Obviously, this is |
| 22 | * not true for the other one. Also bear in mind that reading/writing from/to |
| 23 | * osdep2 is much more expensive than from/to osdep1. |
| 24 | * NOTE: We do not handle the 2 bytes OS-dep area at 0x5D, nor the first |
| 25 | * 2 bytes of storage available right after OSID. That's a total of 4 bytes |
| 26 | * sacrificed: -ETOOLAZY :P |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 27 | * |
| 28 | * The current policy wrt file permissions is: |
| 29 | * - write: root only |
| 30 | * - read: (reading triggers PDC calls) ? root only : everyone |
| 31 | * The rationale is that PDC calls could hog (DoS) the machine. |
| 32 | * |
| 33 | * TODO: |
| 34 | * - timer/fastsize write calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | #undef PDCS_DEBUG |
| 38 | #ifdef PDCS_DEBUG |
| 39 | #define DPRINTK(fmt, args...) printk(KERN_DEBUG fmt, ## args) |
| 40 | #else |
| 41 | #define DPRINTK(fmt, args...) |
| 42 | #endif |
| 43 | |
| 44 | #include <linux/module.h> |
| 45 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/kernel.h> |
| 47 | #include <linux/string.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 48 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/ctype.h> |
| 50 | #include <linux/sysfs.h> |
| 51 | #include <linux/kobject.h> |
| 52 | #include <linux/device.h> |
| 53 | #include <linux/errno.h> |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 54 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | #include <asm/pdc.h> |
| 57 | #include <asm/page.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 58 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <asm/hardware.h> |
| 60 | |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 61 | #define PDCS_VERSION "0.30" |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 62 | #define PDCS_PREFIX "PDC Stable Storage" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | #define PDCS_ADDR_PPRI 0x00 |
| 65 | #define PDCS_ADDR_OSID 0x40 |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 66 | #define PDCS_ADDR_OSD1 0x48 |
| 67 | #define PDCS_ADDR_DIAG 0x58 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #define PDCS_ADDR_FSIZ 0x5C |
| 69 | #define PDCS_ADDR_PCON 0x60 |
| 70 | #define PDCS_ADDR_PALT 0x80 |
| 71 | #define PDCS_ADDR_PKBD 0xA0 |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 72 | #define PDCS_ADDR_OSD2 0xE0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>"); |
| 75 | MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data"); |
| 76 | MODULE_LICENSE("GPL"); |
| 77 | MODULE_VERSION(PDCS_VERSION); |
| 78 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 79 | /* holds Stable Storage size. Initialized once and for all, no lock needed */ |
Helge Deller | 8039de1 | 2006-01-10 20:35:03 -0500 | [diff] [blame] | 80 | static unsigned long pdcs_size __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 82 | /* holds OS ID. Initialized once and for all, hopefully to 0x0006 */ |
| 83 | static u16 pdcs_osid __read_mostly; |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | /* This struct defines what we need to deal with a parisc pdc path entry */ |
| 86 | struct pdcspath_entry { |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 87 | rwlock_t rw_lock; /* to protect path entry access */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | short ready; /* entry record is valid if != 0 */ |
| 89 | unsigned long addr; /* entry address in stable storage */ |
| 90 | char *name; /* entry name */ |
| 91 | struct device_path devpath; /* device path in parisc representation */ |
| 92 | struct device *dev; /* corresponding device */ |
| 93 | struct kobject kobj; |
| 94 | }; |
| 95 | |
| 96 | struct pdcspath_attribute { |
| 97 | struct attribute attr; |
| 98 | ssize_t (*show)(struct pdcspath_entry *entry, char *buf); |
| 99 | ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count); |
| 100 | }; |
| 101 | |
| 102 | #define PDCSPATH_ENTRY(_addr, _name) \ |
| 103 | struct pdcspath_entry pdcspath_entry_##_name = { \ |
| 104 | .ready = 0, \ |
| 105 | .addr = _addr, \ |
| 106 | .name = __stringify(_name), \ |
| 107 | }; |
| 108 | |
| 109 | #define PDCS_ATTR(_name, _mode, _show, _store) \ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 110 | struct kobj_attribute pdcs_attr_##_name = { \ |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 111 | .attr = {.name = __stringify(_name), .mode = _mode}, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | .show = _show, \ |
| 113 | .store = _store, \ |
| 114 | }; |
| 115 | |
| 116 | #define PATHS_ATTR(_name, _mode, _show, _store) \ |
| 117 | struct pdcspath_attribute paths_attr_##_name = { \ |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 118 | .attr = {.name = __stringify(_name), .mode = _mode}, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 131 | * you access the files provided by the facilities. We store a copy of the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 135 | * |
| 136 | * This function expects to be called with @entry->rw_lock write-hold. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | */ |
| 138 | static int |
| 139 | pdcspath_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 VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 176 | * |
| 177 | * This function expects to be called with @entry->rw_lock write-hold. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 179 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | pdcspath_store(struct pdcspath_entry *entry) |
| 181 | { |
| 182 | struct device_path *devpath; |
| 183 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 184 | BUG_ON(!entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | devpath = &entry->devpath; |
| 187 | |
| 188 | /* We expect the caller to set the ready flag to 0 if the hardware |
| 189 | path struct provided is invalid, so that we know we have to fill it. |
| 190 | First case, we don't have a preset hwpath... */ |
| 191 | if (!entry->ready) { |
| 192 | /* ...but we have a device, map it */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 193 | BUG_ON(!entry->dev); |
| 194 | device_to_hwpath(entry->dev, (struct hardware_path *)devpath); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | /* else, we expect the provided hwpath to be valid. */ |
| 197 | |
| 198 | DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__, |
| 199 | entry, devpath, entry->addr); |
| 200 | |
| 201 | /* addr, devpath and count must be word aligned */ |
Julia Lawall | 93c3e91 | 2012-11-03 10:58:36 +0000 | [diff] [blame] | 202 | if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK) |
| 203 | WARN(1, KERN_ERR "%s: an error occurred when writing to PDC.\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | "It is likely that the Stable Storage data has been corrupted.\n" |
| 205 | "Please check it carefully upon next reboot.\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 207 | /* kobject is already registered */ |
| 208 | entry->ready = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | DPRINTK("%s: device: 0x%p\n", __func__, entry->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
| 214 | * pdcspath_hwpath_read - This function handles hardware path pretty printing. |
| 215 | * @entry: An allocated and populated pdscpath_entry struct. |
| 216 | * @buf: The output buffer to write to. |
| 217 | * |
| 218 | * We will call this function to format the output of the hwpath attribute file. |
| 219 | */ |
| 220 | static ssize_t |
| 221 | pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf) |
| 222 | { |
| 223 | char *out = buf; |
| 224 | struct device_path *devpath; |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 225 | short i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | if (!entry || !buf) |
| 228 | return -EINVAL; |
| 229 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 230 | read_lock(&entry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | devpath = &entry->devpath; |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 232 | i = entry->ready; |
| 233 | read_unlock(&entry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 235 | if (!i) /* entry is not ready */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return -ENODATA; |
| 237 | |
| 238 | for (i = 0; i < 6; i++) { |
| 239 | if (devpath->bc[i] >= 128) |
| 240 | continue; |
| 241 | out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]); |
| 242 | } |
| 243 | out += sprintf(out, "%u\n", (unsigned char)devpath->mod); |
| 244 | |
| 245 | return out - buf; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * pdcspath_hwpath_write - This function handles hardware path modifying. |
| 250 | * @entry: An allocated and populated pdscpath_entry struct. |
| 251 | * @buf: The input buffer to read from. |
| 252 | * @count: The number of bytes to be read. |
| 253 | * |
| 254 | * We will call this function to change the current hardware path. |
| 255 | * Hardware paths are to be given '/'-delimited, without brackets. |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 256 | * We make sure that the provided path actually maps to an existing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | * device, BUT nothing would prevent some foolish user to set the path to some |
| 258 | * PCI bridge or even a CPU... |
| 259 | * A better work around would be to make sure we are at the end of a device tree |
| 260 | * for instance, but it would be IMHO beyond the simple scope of that driver. |
| 261 | * The aim is to provide a facility. Data correctness is left to userland. |
| 262 | */ |
| 263 | static ssize_t |
| 264 | pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count) |
| 265 | { |
| 266 | struct hardware_path hwpath; |
| 267 | unsigned short i; |
Helge Deller | c735483 | 2014-09-21 22:31:08 +0200 | [diff] [blame] | 268 | char in[64], *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | struct device *dev; |
Kyle McMartin | 26f0324 | 2007-10-18 00:04:06 -0700 | [diff] [blame] | 270 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | if (!entry || !buf || !count) |
| 273 | return -EINVAL; |
| 274 | |
| 275 | /* We'll use a local copy of buf */ |
Helge Deller | c735483 | 2014-09-21 22:31:08 +0200 | [diff] [blame] | 276 | count = min_t(size_t, count, sizeof(in)-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | strncpy(in, buf, count); |
Helge Deller | c735483 | 2014-09-21 22:31:08 +0200 | [diff] [blame] | 278 | in[count] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 314 | write_lock(&entry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | entry->ready = 0; |
| 316 | entry->dev = dev; |
| 317 | |
| 318 | /* Now, dive in. Write back to the hardware */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 319 | pdcspath_store(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | /* Update the symlink to the real device */ |
| 322 | sysfs_remove_link(&entry->kobj, "device"); |
James Bottomley | 93964fd | 2017-07-31 15:49:54 +0200 | [diff] [blame] | 323 | write_unlock(&entry->rw_lock); |
| 324 | |
Kyle McMartin | 26f0324 | 2007-10-18 00:04:06 -0700 | [diff] [blame] | 325 | ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device"); |
| 326 | WARN_ON(ret); |
| 327 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 328 | printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | entry->name, buf); |
| 330 | |
| 331 | return count; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing. |
| 336 | * @entry: An allocated and populated pdscpath_entry struct. |
| 337 | * @buf: The output buffer to write to. |
| 338 | * |
| 339 | * We will call this function to format the output of the layer attribute file. |
| 340 | */ |
| 341 | static ssize_t |
| 342 | pdcspath_layer_read(struct pdcspath_entry *entry, char *buf) |
| 343 | { |
| 344 | char *out = buf; |
| 345 | struct device_path *devpath; |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 346 | short i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | if (!entry || !buf) |
| 349 | return -EINVAL; |
| 350 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 351 | read_lock(&entry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | devpath = &entry->devpath; |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 353 | i = entry->ready; |
| 354 | read_unlock(&entry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 356 | if (!i) /* entry is not ready */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return -ENODATA; |
| 358 | |
Roel Kluin | 447c233 | 2009-08-02 08:02:28 +0000 | [diff] [blame] | 359 | for (i = 0; i < 6 && devpath->layers[i]; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | out += sprintf(out, "%u ", devpath->layers[i]); |
| 361 | |
| 362 | out += sprintf(out, "\n"); |
| 363 | |
| 364 | return out - buf; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * pdcspath_layer_write - This function handles extended layer modifying. |
| 369 | * @entry: An allocated and populated pdscpath_entry struct. |
| 370 | * @buf: The input buffer to read from. |
| 371 | * @count: The number of bytes to be read. |
| 372 | * |
| 373 | * We will call this function to change the current layer value. |
| 374 | * Layers are to be given '.'-delimited, without brackets. |
| 375 | * XXX beware we are far less checky WRT input data provided than for hwpath. |
| 376 | * Potential harm can be done, since there's no way to check the validity of |
| 377 | * the layer fields. |
| 378 | */ |
| 379 | static ssize_t |
| 380 | pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count) |
| 381 | { |
| 382 | unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */ |
| 383 | unsigned short i; |
Helge Deller | c735483 | 2014-09-21 22:31:08 +0200 | [diff] [blame] | 384 | char in[64], *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | if (!entry || !buf || !count) |
| 387 | return -EINVAL; |
| 388 | |
| 389 | /* We'll use a local copy of buf */ |
Helge Deller | c735483 | 2014-09-21 22:31:08 +0200 | [diff] [blame] | 390 | count = min_t(size_t, count, sizeof(in)-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | strncpy(in, buf, count); |
Helge Deller | c735483 | 2014-09-21 22:31:08 +0200 | [diff] [blame] | 392 | in[count] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | /* Let's clean up the target. 0 is a blank pattern */ |
| 395 | memset(&layers, 0, sizeof(layers)); |
| 396 | |
| 397 | /* First, pick the first layer */ |
| 398 | if (unlikely(!isdigit(*in))) |
| 399 | return -EINVAL; |
| 400 | layers[0] = simple_strtoul(in, NULL, 10); |
| 401 | DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]); |
| 402 | |
| 403 | temp = in; |
| 404 | for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) { |
| 405 | if (unlikely(!isdigit(*(++temp)))) |
| 406 | return -EINVAL; |
| 407 | layers[i] = simple_strtoul(temp, NULL, 10); |
| 408 | DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]); |
| 409 | } |
| 410 | |
| 411 | /* So far so good, let's get in deep */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 412 | write_lock(&entry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | /* First, overwrite the current layers with the new ones, not touching |
| 415 | the hardware path. */ |
| 416 | memcpy(&entry->devpath.layers, &layers, sizeof(layers)); |
| 417 | |
| 418 | /* Now, dive in. Write back to the hardware */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 419 | pdcspath_store(entry); |
| 420 | write_unlock(&entry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 422 | printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | entry->name, buf); |
| 424 | |
| 425 | return count; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * pdcspath_attr_show - Generic read function call wrapper. |
| 430 | * @kobj: The kobject to get info from. |
| 431 | * @attr: The attribute looked upon. |
| 432 | * @buf: The output buffer. |
| 433 | */ |
| 434 | static ssize_t |
| 435 | pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) |
| 436 | { |
| 437 | struct pdcspath_entry *entry = to_pdcspath_entry(kobj); |
| 438 | struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr); |
| 439 | ssize_t ret = 0; |
| 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (pdcs_attr->show) |
| 442 | ret = pdcs_attr->show(entry, buf); |
| 443 | |
| 444 | return ret; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * pdcspath_attr_store - Generic write function call wrapper. |
| 449 | * @kobj: The kobject to write info to. |
| 450 | * @attr: The attribute to be modified. |
| 451 | * @buf: The input buffer. |
| 452 | * @count: The size of the buffer. |
| 453 | */ |
| 454 | static ssize_t |
| 455 | pdcspath_attr_store(struct kobject *kobj, struct attribute *attr, |
| 456 | const char *buf, size_t count) |
| 457 | { |
| 458 | struct pdcspath_entry *entry = to_pdcspath_entry(kobj); |
| 459 | struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr); |
| 460 | ssize_t ret = 0; |
| 461 | |
| 462 | if (!capable(CAP_SYS_ADMIN)) |
| 463 | return -EACCES; |
| 464 | |
| 465 | if (pdcs_attr->store) |
| 466 | ret = pdcs_attr->store(entry, buf, count); |
| 467 | |
| 468 | return ret; |
| 469 | } |
| 470 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 471 | static const struct sysfs_ops pdcspath_attr_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | .show = pdcspath_attr_show, |
| 473 | .store = pdcspath_attr_store, |
| 474 | }; |
| 475 | |
| 476 | /* These are the two attributes of any PDC path. */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 477 | static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write); |
| 478 | static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | static struct attribute *paths_subsys_attrs[] = { |
| 481 | &paths_attr_hwpath.attr, |
| 482 | &paths_attr_layer.attr, |
| 483 | NULL, |
| 484 | }; |
| 485 | |
| 486 | /* Specific kobject type for our PDC paths */ |
| 487 | static struct kobj_type ktype_pdcspath = { |
| 488 | .sysfs_ops = &pdcspath_attr_ops, |
| 489 | .default_attrs = paths_subsys_attrs, |
| 490 | }; |
| 491 | |
| 492 | /* We hard define the 4 types of path we expect to find */ |
| 493 | static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary); |
| 494 | static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console); |
| 495 | static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative); |
| 496 | static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard); |
| 497 | |
| 498 | /* An array containing all PDC paths we will deal with */ |
| 499 | static struct pdcspath_entry *pdcspath_entries[] = { |
| 500 | &pdcspath_entry_primary, |
| 501 | &pdcspath_entry_alternative, |
| 502 | &pdcspath_entry_console, |
| 503 | &pdcspath_entry_keyboard, |
| 504 | NULL, |
| 505 | }; |
| 506 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 507 | |
| 508 | /* For more insight of what's going on here, refer to PDC Procedures doc, |
| 509 | * Section PDC_STABLE */ |
| 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | /** |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 512 | * pdcs_size_read - Stable Storage size output. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | * @buf: The output buffer to write to. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 515 | static ssize_t pdcs_size_read(struct kobject *kobj, |
| 516 | struct kobj_attribute *attr, |
| 517 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
| 519 | char *out = buf; |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 520 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 521 | if (!buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | return -EINVAL; |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* show the size of the stable storage */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 525 | out += sprintf(out, "%ld\n", pdcs_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 527 | return out - buf; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * pdcs_auto_read - Stable Storage autoboot/search flag output. |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 532 | * @buf: The output buffer to write to. |
| 533 | * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag |
| 534 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 535 | static ssize_t pdcs_auto_read(struct kobject *kobj, |
| 536 | struct kobj_attribute *attr, |
| 537 | char *buf, int knob) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 538 | { |
| 539 | char *out = buf; |
| 540 | struct pdcspath_entry *pathentry; |
Helge Deller | 67a5a59 | 2006-03-27 19:52:14 +0000 | [diff] [blame] | 541 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 542 | if (!buf) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 543 | return -EINVAL; |
| 544 | |
| 545 | /* Current flags are stored in primary boot path entry */ |
| 546 | pathentry = &pdcspath_entry_primary; |
| 547 | |
| 548 | read_lock(&pathentry->rw_lock); |
| 549 | out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ? |
| 550 | "On" : "Off"); |
| 551 | read_unlock(&pathentry->rw_lock); |
| 552 | |
| 553 | return out - buf; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * pdcs_autoboot_read - Stable Storage autoboot flag output. |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 558 | * @buf: The output buffer to write to. |
| 559 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 560 | static ssize_t pdcs_autoboot_read(struct kobject *kobj, |
| 561 | struct kobj_attribute *attr, char *buf) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 562 | { |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 563 | return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT); |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /** |
| 567 | * pdcs_autosearch_read - Stable Storage autoboot flag output. |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 568 | * @buf: The output buffer to write to. |
| 569 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 570 | static ssize_t pdcs_autosearch_read(struct kobject *kobj, |
| 571 | struct kobj_attribute *attr, char *buf) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 572 | { |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 573 | return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH); |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /** |
| 577 | * pdcs_timer_read - Stable Storage timer count output (in seconds). |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 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 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 582 | static ssize_t pdcs_timer_read(struct kobject *kobj, |
| 583 | struct kobj_attribute *attr, char *buf) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 584 | { |
| 585 | char *out = buf; |
| 586 | struct pdcspath_entry *pathentry; |
| 587 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 588 | if (!buf) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 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. |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 605 | * @buf: The output buffer to write to. |
| 606 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 607 | static ssize_t pdcs_osid_read(struct kobject *kobj, |
| 608 | struct kobj_attribute *attr, char *buf) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 609 | { |
| 610 | char *out = buf; |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 611 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 612 | if (!buf) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 613 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Kyle McMartin | 67a061a | 2006-06-25 16:58:57 +0000 | [diff] [blame] | 615 | out += sprintf(out, "%s dependent data (0x%.4x)\n", |
| 616 | os_id_to_string(pdcs_osid), pdcs_osid); |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 617 | |
| 618 | return out - buf; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output. |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 623 | * @buf: The output buffer to write to. |
| 624 | * |
| 625 | * This can hold 16 bytes of OS-Dependent data. |
| 626 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 627 | static ssize_t pdcs_osdep1_read(struct kobject *kobj, |
| 628 | struct kobj_attribute *attr, char *buf) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 629 | { |
| 630 | char *out = buf; |
| 631 | u32 result[4]; |
| 632 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 633 | if (!buf) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 634 | return -EINVAL; |
| 635 | |
| 636 | if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK) |
| 637 | return -EIO; |
| 638 | |
| 639 | out += sprintf(out, "0x%.8x\n", result[0]); |
| 640 | out += sprintf(out, "0x%.8x\n", result[1]); |
| 641 | out += sprintf(out, "0x%.8x\n", result[2]); |
| 642 | out += sprintf(out, "0x%.8x\n", result[3]); |
| 643 | |
| 644 | return out - buf; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * pdcs_diagnostic_read - Stable Storage Diagnostic register output. |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 649 | * @buf: The output buffer to write to. |
| 650 | * |
| 651 | * I have NFC how to interpret the content of that register ;-). |
| 652 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 653 | static ssize_t pdcs_diagnostic_read(struct kobject *kobj, |
| 654 | struct kobj_attribute *attr, char *buf) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 655 | { |
| 656 | char *out = buf; |
| 657 | u32 result; |
| 658 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 659 | if (!buf) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 660 | return -EINVAL; |
| 661 | |
| 662 | /* get diagnostic */ |
| 663 | if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK) |
| 664 | return -EIO; |
| 665 | |
| 666 | out += sprintf(out, "0x%.4x\n", (result >> 16)); |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 667 | |
| 668 | return out - buf; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * pdcs_fastsize_read - Stable Storage FastSize register output. |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 673 | * @buf: The output buffer to write to. |
| 674 | * |
| 675 | * This register holds the amount of system RAM to be tested during boot sequence. |
| 676 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 677 | static ssize_t pdcs_fastsize_read(struct kobject *kobj, |
| 678 | struct kobj_attribute *attr, char *buf) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 679 | { |
| 680 | char *out = buf; |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 681 | u32 result; |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 682 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 683 | if (!buf) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 684 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
| 686 | /* get fast-size */ |
| 687 | if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK) |
| 688 | return -EIO; |
| 689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | if ((result & 0x0F) < 0x0E) |
Randolph Chung | abff754 | 2005-10-21 22:57:13 -0400 | [diff] [blame] | 691 | out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | else |
| 693 | out += sprintf(out, "All"); |
| 694 | out += sprintf(out, "\n"); |
| 695 | |
| 696 | return out - buf; |
| 697 | } |
| 698 | |
| 699 | /** |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 700 | * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output. |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 701 | * @buf: The output buffer to write to. |
| 702 | * |
| 703 | * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available. |
| 704 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 705 | static ssize_t pdcs_osdep2_read(struct kobject *kobj, |
| 706 | struct kobj_attribute *attr, char *buf) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 707 | { |
| 708 | char *out = buf; |
| 709 | unsigned long size; |
| 710 | unsigned short i; |
| 711 | u32 result; |
| 712 | |
| 713 | if (unlikely(pdcs_size <= 224)) |
| 714 | return -ENODATA; |
| 715 | |
| 716 | size = pdcs_size - 224; |
| 717 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 718 | if (!buf) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 719 | return -EINVAL; |
| 720 | |
| 721 | for (i=0; i<size; i+=4) { |
| 722 | if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result, |
| 723 | sizeof(result)) != PDC_OK)) |
| 724 | return -EIO; |
| 725 | out += sprintf(out, "0x%.8x\n", result); |
| 726 | } |
| 727 | |
| 728 | return out - buf; |
| 729 | } |
| 730 | |
| 731 | /** |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 732 | * pdcs_auto_write - This function handles autoboot/search flag modifying. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | * @buf: The input buffer to read from. |
| 734 | * @count: The number of bytes to be read. |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 735 | * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | * |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 737 | * We will call this function to change the current autoboot flag. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | * We expect a precise syntax: |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 739 | * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 741 | static ssize_t pdcs_auto_write(struct kobject *kobj, |
| 742 | struct kobj_attribute *attr, const char *buf, |
| 743 | size_t count, int knob) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | { |
| 745 | struct pdcspath_entry *pathentry; |
| 746 | unsigned char flags; |
Rickard Strandqvist | 94c457d | 2014-09-14 18:02:12 +0200 | [diff] [blame] | 747 | char in[8], *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | char c; |
| 749 | |
| 750 | if (!capable(CAP_SYS_ADMIN)) |
| 751 | return -EACCES; |
| 752 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 753 | if (!buf || !count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | return -EINVAL; |
| 755 | |
| 756 | /* We'll use a local copy of buf */ |
Helge Deller | c735483 | 2014-09-21 22:31:08 +0200 | [diff] [blame] | 757 | count = min_t(size_t, count, sizeof(in)-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | strncpy(in, buf, count); |
Rickard Strandqvist | 94c457d | 2014-09-14 18:02:12 +0200 | [diff] [blame] | 759 | in[count] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
| 761 | /* Current flags are stored in primary boot path entry */ |
| 762 | pathentry = &pdcspath_entry_primary; |
| 763 | |
| 764 | /* Be nice to the existing flag record */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 765 | read_lock(&pathentry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | flags = pathentry->devpath.flags; |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 767 | read_unlock(&pathentry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | DPRINTK("%s: flags before: 0x%X\n", __func__, flags); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 770 | |
| 771 | temp = skip_spaces(in); |
| 772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | c = *temp++ - '0'; |
| 774 | if ((c != 0) && (c != 1)) |
| 775 | goto parse_error; |
| 776 | if (c == 0) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 777 | flags &= ~knob; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | else |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 779 | flags |= knob; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | DPRINTK("%s: flags after: 0x%X\n", __func__, flags); |
| 782 | |
| 783 | /* So far so good, let's get in deep */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 784 | write_lock(&pathentry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | |
| 786 | /* Change the path entry flags first */ |
| 787 | pathentry->devpath.flags = flags; |
| 788 | |
| 789 | /* Now, dive in. Write back to the hardware */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 790 | pdcspath_store(pathentry); |
| 791 | write_unlock(&pathentry->rw_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 793 | printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n", |
| 794 | (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch", |
| 795 | (flags & knob) ? "On" : "Off"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
| 797 | return count; |
| 798 | |
| 799 | parse_error: |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 800 | printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | return -EINVAL; |
| 802 | } |
| 803 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 804 | /** |
| 805 | * pdcs_autoboot_write - This function handles autoboot flag modifying. |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 806 | * @buf: The input buffer to read from. |
| 807 | * @count: The number of bytes to be read. |
| 808 | * |
| 809 | * We will call this function to change the current boot flags. |
| 810 | * We expect a precise syntax: |
| 811 | * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On |
| 812 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 813 | static ssize_t pdcs_autoboot_write(struct kobject *kobj, |
| 814 | struct kobj_attribute *attr, |
| 815 | const char *buf, size_t count) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 816 | { |
Joel Soete | ff451d7 | 2008-02-18 18:26:11 -0800 | [diff] [blame] | 817 | return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT); |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /** |
| 821 | * pdcs_autosearch_write - This function handles autosearch flag modifying. |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 822 | * @buf: The input buffer to read from. |
| 823 | * @count: The number of bytes to be read. |
| 824 | * |
| 825 | * We will call this function to change the current boot flags. |
| 826 | * We expect a precise syntax: |
| 827 | * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On |
| 828 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 829 | static ssize_t pdcs_autosearch_write(struct kobject *kobj, |
| 830 | struct kobj_attribute *attr, |
| 831 | const char *buf, size_t count) |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 832 | { |
Joel Soete | ff451d7 | 2008-02-18 18:26:11 -0800 | [diff] [blame] | 833 | return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH); |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 836 | /** |
| 837 | * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input. |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 838 | * @buf: The input buffer to read from. |
| 839 | * @count: The number of bytes to be read. |
| 840 | * |
| 841 | * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte |
| 842 | * write approach. It's up to userspace to deal with it when constructing |
| 843 | * its input buffer. |
| 844 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 845 | static ssize_t pdcs_osdep1_write(struct kobject *kobj, |
| 846 | struct kobj_attribute *attr, |
| 847 | const char *buf, size_t count) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 848 | { |
| 849 | u8 in[16]; |
| 850 | |
| 851 | if (!capable(CAP_SYS_ADMIN)) |
| 852 | return -EACCES; |
| 853 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 854 | if (!buf || !count) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 855 | return -EINVAL; |
| 856 | |
Kyle McMartin | ec1fdc2 | 2006-06-21 19:27:29 +0000 | [diff] [blame] | 857 | if (unlikely(pdcs_osid != OS_ID_LINUX)) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 858 | return -EPERM; |
| 859 | |
| 860 | if (count > 16) |
| 861 | return -EMSGSIZE; |
| 862 | |
| 863 | /* We'll use a local copy of buf */ |
| 864 | memset(in, 0, 16); |
| 865 | memcpy(in, buf, count); |
| 866 | |
| 867 | if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK) |
| 868 | return -EIO; |
| 869 | |
| 870 | return count; |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input. |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 875 | * @buf: The input buffer to read from. |
| 876 | * @count: The number of bytes to be read. |
| 877 | * |
| 878 | * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a |
| 879 | * byte-by-byte write approach. It's up to userspace to deal with it when |
| 880 | * constructing its input buffer. |
| 881 | */ |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 882 | static ssize_t pdcs_osdep2_write(struct kobject *kobj, |
| 883 | struct kobj_attribute *attr, |
| 884 | const char *buf, size_t count) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 885 | { |
| 886 | unsigned long size; |
| 887 | unsigned short i; |
| 888 | u8 in[4]; |
| 889 | |
| 890 | if (!capable(CAP_SYS_ADMIN)) |
| 891 | return -EACCES; |
| 892 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 893 | if (!buf || !count) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 894 | return -EINVAL; |
| 895 | |
| 896 | if (unlikely(pdcs_size <= 224)) |
| 897 | return -ENOSYS; |
| 898 | |
Kyle McMartin | ec1fdc2 | 2006-06-21 19:27:29 +0000 | [diff] [blame] | 899 | if (unlikely(pdcs_osid != OS_ID_LINUX)) |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 900 | return -EPERM; |
| 901 | |
| 902 | size = pdcs_size - 224; |
| 903 | |
| 904 | if (count > size) |
| 905 | return -EMSGSIZE; |
| 906 | |
| 907 | /* We'll use a local copy of buf */ |
| 908 | |
| 909 | for (i=0; i<count; i+=4) { |
| 910 | memset(in, 0, 4); |
| 911 | memcpy(in, buf+i, (count-i < 4) ? count-i : 4); |
| 912 | if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in, |
| 913 | sizeof(in)) != PDC_OK)) |
| 914 | return -EIO; |
| 915 | } |
| 916 | |
| 917 | return count; |
| 918 | } |
| 919 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 920 | /* The remaining attributes. */ |
| 921 | static PDCS_ATTR(size, 0444, pdcs_size_read, NULL); |
| 922 | static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write); |
| 923 | static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write); |
| 924 | static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL); |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 925 | static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL); |
| 926 | static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write); |
| 927 | static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL); |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 928 | static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL); |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 929 | static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 931 | static struct attribute *pdcs_subsys_attrs[] = { |
| 932 | &pdcs_attr_size.attr, |
| 933 | &pdcs_attr_autoboot.attr, |
| 934 | &pdcs_attr_autosearch.attr, |
| 935 | &pdcs_attr_timer.attr, |
| 936 | &pdcs_attr_osid.attr, |
| 937 | &pdcs_attr_osdep1.attr, |
| 938 | &pdcs_attr_diagnostic.attr, |
| 939 | &pdcs_attr_fastsize.attr, |
| 940 | &pdcs_attr_osdep2.attr, |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 941 | NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | }; |
| 943 | |
Arvind Yadav | 343fdfb | 2017-07-11 14:30:34 +0530 | [diff] [blame] | 944 | static const struct attribute_group pdcs_attr_group = { |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 945 | .attrs = pdcs_subsys_attrs, |
| 946 | }; |
| 947 | |
Greg Kroah-Hartman | c829a5b4 | 2007-11-07 13:56:19 -0800 | [diff] [blame] | 948 | static struct kobject *stable_kobj; |
Greg Kroah-Hartman | 4443d07 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 949 | static struct kset *paths_kset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
| 951 | /** |
| 952 | * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage. |
| 953 | * |
| 954 | * It creates kobjects corresponding to each path entry with nice sysfs |
| 955 | * links to the real device. This is where the magic takes place: when |
| 956 | * registering the subsystem attributes during module init, each kobject hereby |
| 957 | * created will show in the sysfs tree as a folder containing files as defined |
| 958 | * by path_subsys_attr[]. |
| 959 | */ |
| 960 | static inline int __init |
| 961 | pdcs_register_pathentries(void) |
| 962 | { |
| 963 | unsigned short i; |
| 964 | struct pdcspath_entry *entry; |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 965 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 967 | /* Initialize the entries rw_lock before anything else */ |
| 968 | for (i = 0; (entry = pdcspath_entries[i]); i++) |
| 969 | rwlock_init(&entry->rw_lock); |
| 970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | for (i = 0; (entry = pdcspath_entries[i]); i++) { |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 972 | write_lock(&entry->rw_lock); |
| 973 | err = pdcspath_fetch(entry); |
| 974 | write_unlock(&entry->rw_lock); |
| 975 | |
| 976 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | continue; |
| 978 | |
Greg Kroah-Hartman | 4443d07 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 979 | entry->kobj.kset = paths_kset; |
Greg Kroah-Hartman | 73f368c | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 980 | err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL, |
| 981 | "%s", entry->name); |
Miaoqian Lin | e184036 | 2022-01-20 12:18:12 +0000 | [diff] [blame] | 982 | if (err) { |
| 983 | kobject_put(&entry->kobj); |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 984 | return err; |
Miaoqian Lin | e184036 | 2022-01-20 12:18:12 +0000 | [diff] [blame] | 985 | } |
Greg Kroah-Hartman | 73f368c | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 986 | |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 987 | /* kobject is now registered */ |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 988 | write_lock(&entry->rw_lock); |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 989 | entry->ready = 2; |
James Bottomley | 93964fd | 2017-07-31 15:49:54 +0200 | [diff] [blame] | 990 | write_unlock(&entry->rw_lock); |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | /* Add a nice symlink to the real device */ |
Kyle McMartin | 26f0324 | 2007-10-18 00:04:06 -0700 | [diff] [blame] | 993 | if (entry->dev) { |
| 994 | err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device"); |
| 995 | WARN_ON(err); |
| 996 | } |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 997 | |
Greg Kroah-Hartman | 73f368c | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 998 | kobject_uevent(&entry->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * pdcs_unregister_pathentries - Routine called when unregistering the module. |
| 1006 | */ |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 1007 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | pdcs_unregister_pathentries(void) |
| 1009 | { |
| 1010 | unsigned short i; |
| 1011 | struct pdcspath_entry *entry; |
| 1012 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 1013 | for (i = 0; (entry = pdcspath_entries[i]); i++) { |
| 1014 | read_lock(&entry->rw_lock); |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 1015 | if (entry->ready >= 2) |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 1016 | kobject_put(&entry->kobj); |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 1017 | read_unlock(&entry->rw_lock); |
| 1018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /* |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 1022 | * For now we register the stable subsystem with the firmware subsystem |
| 1023 | * and the paths subsystem with the stable subsystem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | */ |
| 1025 | static int __init |
| 1026 | pdc_stable_init(void) |
| 1027 | { |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 1028 | int rc = 0, error = 0; |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 1029 | u32 result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
| 1031 | /* find the size of the stable storage */ |
| 1032 | if (pdc_stable_get_size(&pdcs_size) != PDC_OK) |
| 1033 | return -ENODEV; |
| 1034 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 1035 | /* make sure we have enough data */ |
| 1036 | if (pdcs_size < 96) |
| 1037 | return -ENODATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 1039 | printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION); |
| 1040 | |
Thibaut Varene | 3f9edb5 | 2006-05-04 18:43:34 -0600 | [diff] [blame] | 1041 | /* get OSID */ |
| 1042 | if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK) |
| 1043 | return -EIO; |
| 1044 | |
| 1045 | /* the actual result is 16 bits away */ |
| 1046 | pdcs_osid = (u16)(result >> 16); |
| 1047 | |
Greg Kroah-Hartman | c829a5b4 | 2007-11-07 13:56:19 -0800 | [diff] [blame] | 1048 | /* For now we'll register the directory at /sys/firmware/stable */ |
| 1049 | stable_kobj = kobject_create_and_add("stable", firmware_kobj); |
| 1050 | if (!stable_kobj) { |
Greg Kroah-Hartman | 4443d07 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 1051 | rc = -ENOMEM; |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 1052 | goto fail_firmreg; |
Greg Kroah-Hartman | 4443d07 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 1053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 1055 | /* Don't forget the root entries */ |
Joel Soete | ff451d7 | 2008-02-18 18:26:11 -0800 | [diff] [blame] | 1056 | error = sysfs_create_group(stable_kobj, &pdcs_attr_group); |
Greg Kroah-Hartman | 7f54821 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 1057 | |
Greg Kroah-Hartman | 4443d07 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 1058 | /* register the paths kset as a child of the stable kset */ |
Greg Kroah-Hartman | c829a5b4 | 2007-11-07 13:56:19 -0800 | [diff] [blame] | 1059 | paths_kset = kset_create_and_add("paths", NULL, stable_kobj); |
Greg Kroah-Hartman | 4443d07 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 1060 | if (!paths_kset) { |
| 1061 | rc = -ENOMEM; |
| 1062 | goto fail_ksetreg; |
| 1063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
Greg Kroah-Hartman | 4443d07 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 1065 | /* now we create all "files" for the paths kset */ |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 1066 | if ((rc = pdcs_register_pathentries())) |
| 1067 | goto fail_pdcsreg; |
| 1068 | |
| 1069 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 1071 | fail_pdcsreg: |
| 1072 | pdcs_unregister_pathentries(); |
Greg Kroah-Hartman | 4443d07 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 1073 | kset_unregister(paths_kset); |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 1074 | |
Greg Kroah-Hartman | 4443d07 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 1075 | fail_ksetreg: |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 1076 | kobject_put(stable_kobj); |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 1077 | |
| 1078 | fail_firmreg: |
Thibaut VARENE | c742842 | 2006-01-11 13:59:53 -0700 | [diff] [blame] | 1079 | printk(KERN_INFO PDCS_PREFIX " bailing out\n"); |
Thibaut VARENE | 4b991da | 2006-01-10 20:48:01 -0500 | [diff] [blame] | 1080 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static void __exit |
| 1084 | pdc_stable_exit(void) |
| 1085 | { |
| 1086 | pdcs_unregister_pathentries(); |
Greg Kroah-Hartman | 4443d07 | 2007-11-02 15:25:00 -0700 | [diff] [blame] | 1087 | kset_unregister(paths_kset); |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 1088 | kobject_put(stable_kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | module_init(pdc_stable_init); |
| 1093 | module_exit(pdc_stable_exit); |