pdc_stable.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*
  2. * Interfaces to retrieve and set PDC Stable options (firmware)
  3. *
  4. * Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. *
  20. * DEV NOTE: the PDC Procedures reference states that:
  21. * "A minimum of 96 bytes of Stable Storage is required. Providing more than
  22. * 96 bytes of Stable Storage is optional [...]. Failure to provide the
  23. * optional locations from 96 to 192 results in the loss of certain
  24. * functionality during boot."
  25. *
  26. * Since locations between 96 and 192 are the various paths, most (if not
  27. * all) PA-RISC machines should have them. Anyway, for safety reasons, the
  28. * following code can deal with just 96 bytes of Stable Storage, and all
  29. * sizes between 96 and 192 bytes (provided they are multiple of struct
  30. * device_path size, eg: 128, 160 and 192) to provide full information.
  31. * One last word: there's one path we can always count on: the primary path.
  32. * Anything above 224 bytes is used for 'osdep2' OS-dependent storage area.
  33. *
  34. * The first OS-dependent area should always be available. Obviously, this is
  35. * not true for the other one. Also bear in mind that reading/writing from/to
  36. * osdep2 is much more expensive than from/to osdep1.
  37. * NOTE: We do not handle the 2 bytes OS-dep area at 0x5D, nor the first
  38. * 2 bytes of storage available right after OSID. That's a total of 4 bytes
  39. * sacrificed: -ETOOLAZY :P
  40. *
  41. * The current policy wrt file permissions is:
  42. * - write: root only
  43. * - read: (reading triggers PDC calls) ? root only : everyone
  44. * The rationale is that PDC calls could hog (DoS) the machine.
  45. *
  46. * TODO:
  47. * - timer/fastsize write calls
  48. */
  49. #undef PDCS_DEBUG
  50. #ifdef PDCS_DEBUG
  51. #define DPRINTK(fmt, args...) printk(KERN_DEBUG fmt, ## args)
  52. #else
  53. #define DPRINTK(fmt, args...)
  54. #endif
  55. #include <linux/module.h>
  56. #include <linux/init.h>
  57. #include <linux/kernel.h>
  58. #include <linux/string.h>
  59. #include <linux/capability.h>
  60. #include <linux/ctype.h>
  61. #include <linux/sysfs.h>
  62. #include <linux/kobject.h>
  63. #include <linux/device.h>
  64. #include <linux/errno.h>
  65. #include <linux/spinlock.h>
  66. #include <asm/pdc.h>
  67. #include <asm/page.h>
  68. #include <asm/uaccess.h>
  69. #include <asm/hardware.h>
  70. #define PDCS_VERSION "0.30"
  71. #define PDCS_PREFIX "PDC Stable Storage"
  72. #define PDCS_ADDR_PPRI 0x00
  73. #define PDCS_ADDR_OSID 0x40
  74. #define PDCS_ADDR_OSD1 0x48
  75. #define PDCS_ADDR_DIAG 0x58
  76. #define PDCS_ADDR_FSIZ 0x5C
  77. #define PDCS_ADDR_PCON 0x60
  78. #define PDCS_ADDR_PALT 0x80
  79. #define PDCS_ADDR_PKBD 0xA0
  80. #define PDCS_ADDR_OSD2 0xE0
  81. MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
  82. MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
  83. MODULE_LICENSE("GPL");
  84. MODULE_VERSION(PDCS_VERSION);
  85. /* holds Stable Storage size. Initialized once and for all, no lock needed */
  86. static unsigned long pdcs_size __read_mostly;
  87. /* holds OS ID. Initialized once and for all, hopefully to 0x0006 */
  88. static u16 pdcs_osid __read_mostly;
  89. /* This struct defines what we need to deal with a parisc pdc path entry */
  90. struct pdcspath_entry {
  91. rwlock_t rw_lock; /* to protect path entry access */
  92. short ready; /* entry record is valid if != 0 */
  93. unsigned long addr; /* entry address in stable storage */
  94. char *name; /* entry name */
  95. struct device_path devpath; /* device path in parisc representation */
  96. struct device *dev; /* corresponding device */
  97. struct kobject kobj;
  98. };
  99. struct pdcspath_attribute {
  100. struct attribute attr;
  101. ssize_t (*show)(struct pdcspath_entry *entry, char *buf);
  102. ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count);
  103. };
  104. #define PDCSPATH_ENTRY(_addr, _name) \
  105. struct pdcspath_entry pdcspath_entry_##_name = { \
  106. .ready = 0, \
  107. .addr = _addr, \
  108. .name = __stringify(_name), \
  109. };
  110. #define PDCS_ATTR(_name, _mode, _show, _store) \
  111. struct subsys_attribute pdcs_attr_##_name = { \
  112. .attr = {.name = __stringify(_name), .mode = _mode}, \
  113. .show = _show, \
  114. .store = _store, \
  115. };
  116. #define PATHS_ATTR(_name, _mode, _show, _store) \
  117. struct pdcspath_attribute paths_attr_##_name = { \
  118. .attr = {.name = __stringify(_name), .mode = _mode}, \
  119. .show = _show, \
  120. .store = _store, \
  121. };
  122. #define to_pdcspath_attribute(_attr) container_of(_attr, struct pdcspath_attribute, attr)
  123. #define to_pdcspath_entry(obj) container_of(obj, struct pdcspath_entry, kobj)
  124. /**
  125. * pdcspath_fetch - This function populates the path entry structs.
  126. * @entry: A pointer to an allocated pdcspath_entry.
  127. *
  128. * The general idea is that you don't read from the Stable Storage every time
  129. * you access the files provided by the facilites. We store a copy of the
  130. * content of the stable storage WRT various paths in these structs. We read
  131. * these structs when reading the files, and we will write to these structs when
  132. * writing to the files, and only then write them back to the Stable Storage.
  133. *
  134. * This function expects to be called with @entry->rw_lock write-hold.
  135. */
  136. static int
  137. pdcspath_fetch(struct pdcspath_entry *entry)
  138. {
  139. struct device_path *devpath;
  140. if (!entry)
  141. return -EINVAL;
  142. devpath = &entry->devpath;
  143. DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
  144. entry, devpath, entry->addr);
  145. /* addr, devpath and count must be word aligned */
  146. if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
  147. return -EIO;
  148. /* Find the matching device.
  149. NOTE: hardware_path overlays with device_path, so the nice cast can
  150. be used */
  151. entry->dev = hwpath_to_device((struct hardware_path *)devpath);
  152. entry->ready = 1;
  153. DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
  154. return 0;
  155. }
  156. /**
  157. * pdcspath_store - This function writes a path to stable storage.
  158. * @entry: A pointer to an allocated pdcspath_entry.
  159. *
  160. * It can be used in two ways: either by passing it a preset devpath struct
  161. * containing an already computed hardware path, or by passing it a device
  162. * pointer, from which it'll find out the corresponding hardware path.
  163. * For now we do not handle the case where there's an error in writing to the
  164. * Stable Storage area, so you'd better not mess up the data :P
  165. *
  166. * This function expects to be called with @entry->rw_lock write-hold.
  167. */
  168. static void
  169. pdcspath_store(struct pdcspath_entry *entry)
  170. {
  171. struct device_path *devpath;
  172. BUG_ON(!entry);
  173. devpath = &entry->devpath;
  174. /* We expect the caller to set the ready flag to 0 if the hardware
  175. path struct provided is invalid, so that we know we have to fill it.
  176. First case, we don't have a preset hwpath... */
  177. if (!entry->ready) {
  178. /* ...but we have a device, map it */
  179. BUG_ON(!entry->dev);
  180. device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
  181. }
  182. /* else, we expect the provided hwpath to be valid. */
  183. DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
  184. entry, devpath, entry->addr);
  185. /* addr, devpath and count must be word aligned */
  186. if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK) {
  187. printk(KERN_ERR "%s: an error occured when writing to PDC.\n"
  188. "It is likely that the Stable Storage data has been corrupted.\n"
  189. "Please check it carefully upon next reboot.\n", __func__);
  190. WARN_ON(1);
  191. }
  192. /* kobject is already registered */
  193. entry->ready = 2;
  194. DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
  195. }
  196. /**
  197. * pdcspath_hwpath_read - This function handles hardware path pretty printing.
  198. * @entry: An allocated and populated pdscpath_entry struct.
  199. * @buf: The output buffer to write to.
  200. *
  201. * We will call this function to format the output of the hwpath attribute file.
  202. */
  203. static ssize_t
  204. pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
  205. {
  206. char *out = buf;
  207. struct device_path *devpath;
  208. short i;
  209. if (!entry || !buf)
  210. return -EINVAL;
  211. read_lock(&entry->rw_lock);
  212. devpath = &entry->devpath;
  213. i = entry->ready;
  214. read_unlock(&entry->rw_lock);
  215. if (!i) /* entry is not ready */
  216. return -ENODATA;
  217. for (i = 0; i < 6; i++) {
  218. if (devpath->bc[i] >= 128)
  219. continue;
  220. out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
  221. }
  222. out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
  223. return out - buf;
  224. }
  225. /**
  226. * pdcspath_hwpath_write - This function handles hardware path modifying.
  227. * @entry: An allocated and populated pdscpath_entry struct.
  228. * @buf: The input buffer to read from.
  229. * @count: The number of bytes to be read.
  230. *
  231. * We will call this function to change the current hardware path.
  232. * Hardware paths are to be given '/'-delimited, without brackets.
  233. * We make sure that the provided path actually maps to an existing
  234. * device, BUT nothing would prevent some foolish user to set the path to some
  235. * PCI bridge or even a CPU...
  236. * A better work around would be to make sure we are at the end of a device tree
  237. * for instance, but it would be IMHO beyond the simple scope of that driver.
  238. * The aim is to provide a facility. Data correctness is left to userland.
  239. */
  240. static ssize_t
  241. pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
  242. {
  243. struct hardware_path hwpath;
  244. unsigned short i;
  245. char in[count+1], *temp;
  246. struct device *dev;
  247. int ret;
  248. if (!entry || !buf || !count)
  249. return -EINVAL;
  250. /* We'll use a local copy of buf */
  251. memset(in, 0, count+1);
  252. strncpy(in, buf, count);
  253. /* Let's clean up the target. 0xff is a blank pattern */
  254. memset(&hwpath, 0xff, sizeof(hwpath));
  255. /* First, pick the mod field (the last one of the input string) */
  256. if (!(temp = strrchr(in, '/')))
  257. return -EINVAL;
  258. hwpath.mod = simple_strtoul(temp+1, NULL, 10);
  259. in[temp-in] = '\0'; /* truncate the remaining string. just precaution */
  260. DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
  261. /* Then, loop for each delimiter, making sure we don't have too many.
  262. we write the bc fields in a down-top way. No matter what, we stop
  263. before writing the last field. If there are too many fields anyway,
  264. then the user is a moron and it'll be caught up later when we'll
  265. check the consistency of the given hwpath. */
  266. for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
  267. hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
  268. in[temp-in] = '\0';
  269. DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
  270. }
  271. /* Store the final field */
  272. hwpath.bc[i] = simple_strtoul(in, NULL, 10);
  273. DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
  274. /* Now we check that the user isn't trying to lure us */
  275. if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
  276. printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
  277. "hardware path: %s\n", __func__, entry->name, buf);
  278. return -EINVAL;
  279. }
  280. /* So far so good, let's get in deep */
  281. write_lock(&entry->rw_lock);
  282. entry->ready = 0;
  283. entry->dev = dev;
  284. /* Now, dive in. Write back to the hardware */
  285. pdcspath_store(entry);
  286. /* Update the symlink to the real device */
  287. sysfs_remove_link(&entry->kobj, "device");
  288. ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
  289. WARN_ON(ret);
  290. write_unlock(&entry->rw_lock);
  291. printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
  292. entry->name, buf);
  293. return count;
  294. }
  295. /**
  296. * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
  297. * @entry: An allocated and populated pdscpath_entry struct.
  298. * @buf: The output buffer to write to.
  299. *
  300. * We will call this function to format the output of the layer attribute file.
  301. */
  302. static ssize_t
  303. pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
  304. {
  305. char *out = buf;
  306. struct device_path *devpath;
  307. short i;
  308. if (!entry || !buf)
  309. return -EINVAL;
  310. read_lock(&entry->rw_lock);
  311. devpath = &entry->devpath;
  312. i = entry->ready;
  313. read_unlock(&entry->rw_lock);
  314. if (!i) /* entry is not ready */
  315. return -ENODATA;
  316. for (i = 0; devpath->layers[i] && (likely(i < 6)); i++)
  317. out += sprintf(out, "%u ", devpath->layers[i]);
  318. out += sprintf(out, "\n");
  319. return out - buf;
  320. }
  321. /**
  322. * pdcspath_layer_write - This function handles extended layer modifying.
  323. * @entry: An allocated and populated pdscpath_entry struct.
  324. * @buf: The input buffer to read from.
  325. * @count: The number of bytes to be read.
  326. *
  327. * We will call this function to change the current layer value.
  328. * Layers are to be given '.'-delimited, without brackets.
  329. * XXX beware we are far less checky WRT input data provided than for hwpath.
  330. * Potential harm can be done, since there's no way to check the validity of
  331. * the layer fields.
  332. */
  333. static ssize_t
  334. pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
  335. {
  336. unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
  337. unsigned short i;
  338. char in[count+1], *temp;
  339. if (!entry || !buf || !count)
  340. return -EINVAL;
  341. /* We'll use a local copy of buf */
  342. memset(in, 0, count+1);
  343. strncpy(in, buf, count);
  344. /* Let's clean up the target. 0 is a blank pattern */
  345. memset(&layers, 0, sizeof(layers));
  346. /* First, pick the first layer */
  347. if (unlikely(!isdigit(*in)))
  348. return -EINVAL;
  349. layers[0] = simple_strtoul(in, NULL, 10);
  350. DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
  351. temp = in;
  352. for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
  353. if (unlikely(!isdigit(*(++temp))))
  354. return -EINVAL;
  355. layers[i] = simple_strtoul(temp, NULL, 10);
  356. DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
  357. }
  358. /* So far so good, let's get in deep */
  359. write_lock(&entry->rw_lock);
  360. /* First, overwrite the current layers with the new ones, not touching
  361. the hardware path. */
  362. memcpy(&entry->devpath.layers, &layers, sizeof(layers));
  363. /* Now, dive in. Write back to the hardware */
  364. pdcspath_store(entry);
  365. write_unlock(&entry->rw_lock);
  366. printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
  367. entry->name, buf);
  368. return count;
  369. }
  370. /**
  371. * pdcspath_attr_show - Generic read function call wrapper.
  372. * @kobj: The kobject to get info from.
  373. * @attr: The attribute looked upon.
  374. * @buf: The output buffer.
  375. */
  376. static ssize_t
  377. pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  378. {
  379. struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
  380. struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
  381. ssize_t ret = 0;
  382. if (pdcs_attr->show)
  383. ret = pdcs_attr->show(entry, buf);
  384. return ret;
  385. }
  386. /**
  387. * pdcspath_attr_store - Generic write function call wrapper.
  388. * @kobj: The kobject to write info to.
  389. * @attr: The attribute to be modified.
  390. * @buf: The input buffer.
  391. * @count: The size of the buffer.
  392. */
  393. static ssize_t
  394. pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
  395. const char *buf, size_t count)
  396. {
  397. struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
  398. struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
  399. ssize_t ret = 0;
  400. if (!capable(CAP_SYS_ADMIN))
  401. return -EACCES;
  402. if (pdcs_attr->store)
  403. ret = pdcs_attr->store(entry, buf, count);
  404. return ret;
  405. }
  406. static struct sysfs_ops pdcspath_attr_ops = {
  407. .show = pdcspath_attr_show,
  408. .store = pdcspath_attr_store,
  409. };
  410. /* These are the two attributes of any PDC path. */
  411. static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
  412. static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
  413. static struct attribute *paths_subsys_attrs[] = {
  414. &paths_attr_hwpath.attr,
  415. &paths_attr_layer.attr,
  416. NULL,
  417. };
  418. /* Specific kobject type for our PDC paths */
  419. static struct kobj_type ktype_pdcspath = {
  420. .sysfs_ops = &pdcspath_attr_ops,
  421. .default_attrs = paths_subsys_attrs,
  422. };
  423. /* We hard define the 4 types of path we expect to find */
  424. static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
  425. static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
  426. static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
  427. static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
  428. /* An array containing all PDC paths we will deal with */
  429. static struct pdcspath_entry *pdcspath_entries[] = {
  430. &pdcspath_entry_primary,
  431. &pdcspath_entry_alternative,
  432. &pdcspath_entry_console,
  433. &pdcspath_entry_keyboard,
  434. NULL,
  435. };
  436. /* For more insight of what's going on here, refer to PDC Procedures doc,
  437. * Section PDC_STABLE */
  438. /**
  439. * pdcs_size_read - Stable Storage size output.
  440. * @kset: An allocated and populated struct kset. We don't use it tho.
  441. * @buf: The output buffer to write to.
  442. */
  443. static ssize_t
  444. pdcs_size_read(struct kset *kset, char *buf)
  445. {
  446. char *out = buf;
  447. if (!kset || !buf)
  448. return -EINVAL;
  449. /* show the size of the stable storage */
  450. out += sprintf(out, "%ld\n", pdcs_size);
  451. return out - buf;
  452. }
  453. /**
  454. * pdcs_auto_read - Stable Storage autoboot/search flag output.
  455. * @kset: An allocated and populated struct kset. We don't use it tho.
  456. * @buf: The output buffer to write to.
  457. * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
  458. */
  459. static ssize_t
  460. pdcs_auto_read(struct kset *kset, char *buf, int knob)
  461. {
  462. char *out = buf;
  463. struct pdcspath_entry *pathentry;
  464. if (!kset || !buf)
  465. return -EINVAL;
  466. /* Current flags are stored in primary boot path entry */
  467. pathentry = &pdcspath_entry_primary;
  468. read_lock(&pathentry->rw_lock);
  469. out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
  470. "On" : "Off");
  471. read_unlock(&pathentry->rw_lock);
  472. return out - buf;
  473. }
  474. /**
  475. * pdcs_autoboot_read - Stable Storage autoboot flag output.
  476. * @kset: An allocated and populated struct kset. We don't use it tho.
  477. * @buf: The output buffer to write to.
  478. */
  479. static inline ssize_t
  480. pdcs_autoboot_read(struct kset *kset, char *buf)
  481. {
  482. return pdcs_auto_read(kset, buf, PF_AUTOBOOT);
  483. }
  484. /**
  485. * pdcs_autosearch_read - Stable Storage autoboot flag output.
  486. * @kset: An allocated and populated struct kset. We don't use it tho.
  487. * @buf: The output buffer to write to.
  488. */
  489. static inline ssize_t
  490. pdcs_autosearch_read(struct kset *kset, char *buf)
  491. {
  492. return pdcs_auto_read(kset, buf, PF_AUTOSEARCH);
  493. }
  494. /**
  495. * pdcs_timer_read - Stable Storage timer count output (in seconds).
  496. * @kset: An allocated and populated struct kset. We don't use it tho.
  497. * @buf: The output buffer to write to.
  498. *
  499. * The value of the timer field correponds to a number of seconds in powers of 2.
  500. */
  501. static ssize_t
  502. pdcs_timer_read(struct kset *kset, char *buf)
  503. {
  504. char *out = buf;
  505. struct pdcspath_entry *pathentry;
  506. if (!kset || !buf)
  507. return -EINVAL;
  508. /* Current flags are stored in primary boot path entry */
  509. pathentry = &pdcspath_entry_primary;
  510. /* print the timer value in seconds */
  511. read_lock(&pathentry->rw_lock);
  512. out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
  513. (1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
  514. read_unlock(&pathentry->rw_lock);
  515. return out - buf;
  516. }
  517. /**
  518. * pdcs_osid_read - Stable Storage OS ID register output.
  519. * @kset: An allocated and populated struct kset. We don't use it tho.
  520. * @buf: The output buffer to write to.
  521. */
  522. static ssize_t
  523. pdcs_osid_read(struct kset *kset, char *buf)
  524. {
  525. char *out = buf;
  526. if (!kset || !buf)
  527. return -EINVAL;
  528. out += sprintf(out, "%s dependent data (0x%.4x)\n",
  529. os_id_to_string(pdcs_osid), pdcs_osid);
  530. return out - buf;
  531. }
  532. /**
  533. * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
  534. * @kset: An allocated and populated struct kset. We don't use it tho.
  535. * @buf: The output buffer to write to.
  536. *
  537. * This can hold 16 bytes of OS-Dependent data.
  538. */
  539. static ssize_t
  540. pdcs_osdep1_read(struct kset *kset, char *buf)
  541. {
  542. char *out = buf;
  543. u32 result[4];
  544. if (!kset || !buf)
  545. return -EINVAL;
  546. if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
  547. return -EIO;
  548. out += sprintf(out, "0x%.8x\n", result[0]);
  549. out += sprintf(out, "0x%.8x\n", result[1]);
  550. out += sprintf(out, "0x%.8x\n", result[2]);
  551. out += sprintf(out, "0x%.8x\n", result[3]);
  552. return out - buf;
  553. }
  554. /**
  555. * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
  556. * @kset: An allocated and populated struct kset. We don't use it tho.
  557. * @buf: The output buffer to write to.
  558. *
  559. * I have NFC how to interpret the content of that register ;-).
  560. */
  561. static ssize_t
  562. pdcs_diagnostic_read(struct kset *kset, char *buf)
  563. {
  564. char *out = buf;
  565. u32 result;
  566. if (!kset || !buf)
  567. return -EINVAL;
  568. /* get diagnostic */
  569. if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
  570. return -EIO;
  571. out += sprintf(out, "0x%.4x\n", (result >> 16));
  572. return out - buf;
  573. }
  574. /**
  575. * pdcs_fastsize_read - Stable Storage FastSize register output.
  576. * @kset: An allocated and populated struct kset. We don't use it tho.
  577. * @buf: The output buffer to write to.
  578. *
  579. * This register holds the amount of system RAM to be tested during boot sequence.
  580. */
  581. static ssize_t
  582. pdcs_fastsize_read(struct kset *kset, char *buf)
  583. {
  584. char *out = buf;
  585. u32 result;
  586. if (!kset || !buf)
  587. return -EINVAL;
  588. /* get fast-size */
  589. if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
  590. return -EIO;
  591. if ((result & 0x0F) < 0x0E)
  592. out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
  593. else
  594. out += sprintf(out, "All");
  595. out += sprintf(out, "\n");
  596. return out - buf;
  597. }
  598. /**
  599. * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
  600. * @kset: An allocated and populated struct kset. We don't use it tho.
  601. * @buf: The output buffer to write to.
  602. *
  603. * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
  604. */
  605. static ssize_t
  606. pdcs_osdep2_read(struct kset *kset, char *buf)
  607. {
  608. char *out = buf;
  609. unsigned long size;
  610. unsigned short i;
  611. u32 result;
  612. if (unlikely(pdcs_size <= 224))
  613. return -ENODATA;
  614. size = pdcs_size - 224;
  615. if (!kset || !buf)
  616. return -EINVAL;
  617. for (i=0; i<size; i+=4) {
  618. if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
  619. sizeof(result)) != PDC_OK))
  620. return -EIO;
  621. out += sprintf(out, "0x%.8x\n", result);
  622. }
  623. return out - buf;
  624. }
  625. /**
  626. * pdcs_auto_write - This function handles autoboot/search flag modifying.
  627. * @kset: An allocated and populated struct kset. We don't use it tho.
  628. * @buf: The input buffer to read from.
  629. * @count: The number of bytes to be read.
  630. * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
  631. *
  632. * We will call this function to change the current autoboot flag.
  633. * We expect a precise syntax:
  634. * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
  635. */
  636. static ssize_t
  637. pdcs_auto_write(struct kset *kset, const char *buf, size_t count, int knob)
  638. {
  639. struct pdcspath_entry *pathentry;
  640. unsigned char flags;
  641. char in[count+1], *temp;
  642. char c;
  643. if (!capable(CAP_SYS_ADMIN))
  644. return -EACCES;
  645. if (!kset || !buf || !count)
  646. return -EINVAL;
  647. /* We'll use a local copy of buf */
  648. memset(in, 0, count+1);
  649. strncpy(in, buf, count);
  650. /* Current flags are stored in primary boot path entry */
  651. pathentry = &pdcspath_entry_primary;
  652. /* Be nice to the existing flag record */
  653. read_lock(&pathentry->rw_lock);
  654. flags = pathentry->devpath.flags;
  655. read_unlock(&pathentry->rw_lock);
  656. DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
  657. temp = in;
  658. while (*temp && isspace(*temp))
  659. temp++;
  660. c = *temp++ - '0';
  661. if ((c != 0) && (c != 1))
  662. goto parse_error;
  663. if (c == 0)
  664. flags &= ~knob;
  665. else
  666. flags |= knob;
  667. DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
  668. /* So far so good, let's get in deep */
  669. write_lock(&pathentry->rw_lock);
  670. /* Change the path entry flags first */
  671. pathentry->devpath.flags = flags;
  672. /* Now, dive in. Write back to the hardware */
  673. pdcspath_store(pathentry);
  674. write_unlock(&pathentry->rw_lock);
  675. printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
  676. (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
  677. (flags & knob) ? "On" : "Off");
  678. return count;
  679. parse_error:
  680. printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
  681. return -EINVAL;
  682. }
  683. /**
  684. * pdcs_autoboot_write - This function handles autoboot flag modifying.
  685. * @kset: An allocated and populated struct kset. We don't use it tho.
  686. * @buf: The input buffer to read from.
  687. * @count: The number of bytes to be read.
  688. *
  689. * We will call this function to change the current boot flags.
  690. * We expect a precise syntax:
  691. * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
  692. */
  693. static inline ssize_t
  694. pdcs_autoboot_write(struct kset *kset, const char *buf, size_t count)
  695. {
  696. return pdcs_auto_write(kset, buf, count, PF_AUTOBOOT);
  697. }
  698. /**
  699. * pdcs_autosearch_write - This function handles autosearch flag modifying.
  700. * @kset: An allocated and populated struct kset. We don't use it tho.
  701. * @buf: The input buffer to read from.
  702. * @count: The number of bytes to be read.
  703. *
  704. * We will call this function to change the current boot flags.
  705. * We expect a precise syntax:
  706. * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
  707. */
  708. static inline ssize_t
  709. pdcs_autosearch_write(struct kset *kset, const char *buf, size_t count)
  710. {
  711. return pdcs_auto_write(kset, buf, count, PF_AUTOSEARCH);
  712. }
  713. /**
  714. * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
  715. * @kset: An allocated and populated struct kset. We don't use it tho.
  716. * @buf: The input buffer to read from.
  717. * @count: The number of bytes to be read.
  718. *
  719. * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
  720. * write approach. It's up to userspace to deal with it when constructing
  721. * its input buffer.
  722. */
  723. static ssize_t
  724. pdcs_osdep1_write(struct kset *kset, const char *buf, size_t count)
  725. {
  726. u8 in[16];
  727. if (!capable(CAP_SYS_ADMIN))
  728. return -EACCES;
  729. if (!kset || !buf || !count)
  730. return -EINVAL;
  731. if (unlikely(pdcs_osid != OS_ID_LINUX))
  732. return -EPERM;
  733. if (count > 16)
  734. return -EMSGSIZE;
  735. /* We'll use a local copy of buf */
  736. memset(in, 0, 16);
  737. memcpy(in, buf, count);
  738. if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
  739. return -EIO;
  740. return count;
  741. }
  742. /**
  743. * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
  744. * @kset: An allocated and populated struct kset. We don't use it tho.
  745. * @buf: The input buffer to read from.
  746. * @count: The number of bytes to be read.
  747. *
  748. * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
  749. * byte-by-byte write approach. It's up to userspace to deal with it when
  750. * constructing its input buffer.
  751. */
  752. static ssize_t
  753. pdcs_osdep2_write(struct kset *kset, const char *buf, size_t count)
  754. {
  755. unsigned long size;
  756. unsigned short i;
  757. u8 in[4];
  758. if (!capable(CAP_SYS_ADMIN))
  759. return -EACCES;
  760. if (!kset || !buf || !count)
  761. return -EINVAL;
  762. if (unlikely(pdcs_size <= 224))
  763. return -ENOSYS;
  764. if (unlikely(pdcs_osid != OS_ID_LINUX))
  765. return -EPERM;
  766. size = pdcs_size - 224;
  767. if (count > size)
  768. return -EMSGSIZE;
  769. /* We'll use a local copy of buf */
  770. for (i=0; i<count; i+=4) {
  771. memset(in, 0, 4);
  772. memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
  773. if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
  774. sizeof(in)) != PDC_OK))
  775. return -EIO;
  776. }
  777. return count;
  778. }
  779. /* The remaining attributes. */
  780. static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
  781. static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
  782. static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
  783. static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
  784. static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
  785. static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
  786. static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
  787. static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
  788. static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
  789. static struct subsys_attribute *pdcs_subsys_attrs[] = {
  790. &pdcs_attr_size,
  791. &pdcs_attr_autoboot,
  792. &pdcs_attr_autosearch,
  793. &pdcs_attr_timer,
  794. &pdcs_attr_osid,
  795. &pdcs_attr_osdep1,
  796. &pdcs_attr_diagnostic,
  797. &pdcs_attr_fastsize,
  798. &pdcs_attr_osdep2,
  799. NULL,
  800. };
  801. static decl_subsys(paths, &ktype_pdcspath, NULL);
  802. static decl_subsys(stable, NULL, NULL);
  803. /**
  804. * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
  805. *
  806. * It creates kobjects corresponding to each path entry with nice sysfs
  807. * links to the real device. This is where the magic takes place: when
  808. * registering the subsystem attributes during module init, each kobject hereby
  809. * created will show in the sysfs tree as a folder containing files as defined
  810. * by path_subsys_attr[].
  811. */
  812. static inline int __init
  813. pdcs_register_pathentries(void)
  814. {
  815. unsigned short i;
  816. struct pdcspath_entry *entry;
  817. int err;
  818. /* Initialize the entries rw_lock before anything else */
  819. for (i = 0; (entry = pdcspath_entries[i]); i++)
  820. rwlock_init(&entry->rw_lock);
  821. for (i = 0; (entry = pdcspath_entries[i]); i++) {
  822. write_lock(&entry->rw_lock);
  823. err = pdcspath_fetch(entry);
  824. write_unlock(&entry->rw_lock);
  825. if (err < 0)
  826. continue;
  827. if ((err = kobject_set_name(&entry->kobj, "%s", entry->name)))
  828. return err;
  829. kobj_set_kset_s(entry, paths_subsys);
  830. if ((err = kobject_register(&entry->kobj)))
  831. return err;
  832. /* kobject is now registered */
  833. write_lock(&entry->rw_lock);
  834. entry->ready = 2;
  835. /* Add a nice symlink to the real device */
  836. if (entry->dev) {
  837. err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
  838. WARN_ON(err);
  839. }
  840. write_unlock(&entry->rw_lock);
  841. }
  842. return 0;
  843. }
  844. /**
  845. * pdcs_unregister_pathentries - Routine called when unregistering the module.
  846. */
  847. static inline void
  848. pdcs_unregister_pathentries(void)
  849. {
  850. unsigned short i;
  851. struct pdcspath_entry *entry;
  852. for (i = 0; (entry = pdcspath_entries[i]); i++) {
  853. read_lock(&entry->rw_lock);
  854. if (entry->ready >= 2)
  855. kobject_unregister(&entry->kobj);
  856. read_unlock(&entry->rw_lock);
  857. }
  858. }
  859. /*
  860. * For now we register the stable subsystem with the firmware subsystem
  861. * and the paths subsystem with the stable subsystem
  862. */
  863. static int __init
  864. pdc_stable_init(void)
  865. {
  866. struct subsys_attribute *attr;
  867. int i, rc = 0, error = 0;
  868. u32 result;
  869. /* find the size of the stable storage */
  870. if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
  871. return -ENODEV;
  872. /* make sure we have enough data */
  873. if (pdcs_size < 96)
  874. return -ENODATA;
  875. printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
  876. /* get OSID */
  877. if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
  878. return -EIO;
  879. /* the actual result is 16 bits away */
  880. pdcs_osid = (u16)(result >> 16);
  881. /* For now we'll register the stable subsys within this driver */
  882. if ((rc = firmware_register(&stable_subsys)))
  883. goto fail_firmreg;
  884. /* Don't forget the root entries */
  885. for (i = 0; (attr = pdcs_subsys_attrs[i]) && !error; i++)
  886. if (attr->show)
  887. error = subsys_create_file(&stable_subsys, attr);
  888. /* register the paths subsys as a subsystem of stable subsys */
  889. kobj_set_kset_s(&paths_subsys, stable_subsys);
  890. if ((rc = subsystem_register(&paths_subsys)))
  891. goto fail_subsysreg;
  892. /* now we create all "files" for the paths subsys */
  893. if ((rc = pdcs_register_pathentries()))
  894. goto fail_pdcsreg;
  895. return rc;
  896. fail_pdcsreg:
  897. pdcs_unregister_pathentries();
  898. subsystem_unregister(&paths_subsys);
  899. fail_subsysreg:
  900. firmware_unregister(&stable_subsys);
  901. fail_firmreg:
  902. printk(KERN_INFO PDCS_PREFIX " bailing out\n");
  903. return rc;
  904. }
  905. static void __exit
  906. pdc_stable_exit(void)
  907. {
  908. pdcs_unregister_pathentries();
  909. subsystem_unregister(&paths_subsys);
  910. firmware_unregister(&stable_subsys);
  911. }
  912. module_init(pdc_stable_init);
  913. module_exit(pdc_stable_exit);