pdc_stable.c 27 KB

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