pdc_stable.c 27 KB

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