pdc_stable.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 kobj_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 facilities. 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. WARN(1, KERN_ERR "%s: an error occurred 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. /* kobject is already registered */
  191. entry->ready = 2;
  192. DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
  193. }
  194. /**
  195. * pdcspath_hwpath_read - This function handles hardware path pretty printing.
  196. * @entry: An allocated and populated pdscpath_entry struct.
  197. * @buf: The output buffer to write to.
  198. *
  199. * We will call this function to format the output of the hwpath attribute file.
  200. */
  201. static ssize_t
  202. pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
  203. {
  204. char *out = buf;
  205. struct device_path *devpath;
  206. short i;
  207. if (!entry || !buf)
  208. return -EINVAL;
  209. read_lock(&entry->rw_lock);
  210. devpath = &entry->devpath;
  211. i = entry->ready;
  212. read_unlock(&entry->rw_lock);
  213. if (!i) /* entry is not ready */
  214. return -ENODATA;
  215. for (i = 0; i < 6; i++) {
  216. if (devpath->bc[i] >= 128)
  217. continue;
  218. out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
  219. }
  220. out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
  221. return out - buf;
  222. }
  223. /**
  224. * pdcspath_hwpath_write - This function handles hardware path modifying.
  225. * @entry: An allocated and populated pdscpath_entry struct.
  226. * @buf: The input buffer to read from.
  227. * @count: The number of bytes to be read.
  228. *
  229. * We will call this function to change the current hardware path.
  230. * Hardware paths are to be given '/'-delimited, without brackets.
  231. * We make sure that the provided path actually maps to an existing
  232. * device, BUT nothing would prevent some foolish user to set the path to some
  233. * PCI bridge or even a CPU...
  234. * A better work around would be to make sure we are at the end of a device tree
  235. * for instance, but it would be IMHO beyond the simple scope of that driver.
  236. * The aim is to provide a facility. Data correctness is left to userland.
  237. */
  238. static ssize_t
  239. pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
  240. {
  241. struct hardware_path hwpath;
  242. unsigned short i;
  243. char in[count+1], *temp;
  244. struct device *dev;
  245. int ret;
  246. if (!entry || !buf || !count)
  247. return -EINVAL;
  248. /* We'll use a local copy of buf */
  249. memset(in, 0, count+1);
  250. strncpy(in, buf, count);
  251. /* Let's clean up the target. 0xff is a blank pattern */
  252. memset(&hwpath, 0xff, sizeof(hwpath));
  253. /* First, pick the mod field (the last one of the input string) */
  254. if (!(temp = strrchr(in, '/')))
  255. return -EINVAL;
  256. hwpath.mod = simple_strtoul(temp+1, NULL, 10);
  257. in[temp-in] = '\0'; /* truncate the remaining string. just precaution */
  258. DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
  259. /* Then, loop for each delimiter, making sure we don't have too many.
  260. we write the bc fields in a down-top way. No matter what, we stop
  261. before writing the last field. If there are too many fields anyway,
  262. then the user is a moron and it'll be caught up later when we'll
  263. check the consistency of the given hwpath. */
  264. for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
  265. hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
  266. in[temp-in] = '\0';
  267. DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
  268. }
  269. /* Store the final field */
  270. hwpath.bc[i] = simple_strtoul(in, NULL, 10);
  271. DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
  272. /* Now we check that the user isn't trying to lure us */
  273. if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
  274. printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
  275. "hardware path: %s\n", __func__, entry->name, buf);
  276. return -EINVAL;
  277. }
  278. /* So far so good, let's get in deep */
  279. write_lock(&entry->rw_lock);
  280. entry->ready = 0;
  281. entry->dev = dev;
  282. /* Now, dive in. Write back to the hardware */
  283. pdcspath_store(entry);
  284. /* Update the symlink to the real device */
  285. sysfs_remove_link(&entry->kobj, "device");
  286. ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
  287. WARN_ON(ret);
  288. write_unlock(&entry->rw_lock);
  289. printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
  290. entry->name, buf);
  291. return count;
  292. }
  293. /**
  294. * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
  295. * @entry: An allocated and populated pdscpath_entry struct.
  296. * @buf: The output buffer to write to.
  297. *
  298. * We will call this function to format the output of the layer attribute file.
  299. */
  300. static ssize_t
  301. pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
  302. {
  303. char *out = buf;
  304. struct device_path *devpath;
  305. short i;
  306. if (!entry || !buf)
  307. return -EINVAL;
  308. read_lock(&entry->rw_lock);
  309. devpath = &entry->devpath;
  310. i = entry->ready;
  311. read_unlock(&entry->rw_lock);
  312. if (!i) /* entry is not ready */
  313. return -ENODATA;
  314. for (i = 0; i < 6 && devpath->layers[i]; i++)
  315. out += sprintf(out, "%u ", devpath->layers[i]);
  316. out += sprintf(out, "\n");
  317. return out - buf;
  318. }
  319. /**
  320. * pdcspath_layer_write - This function handles extended layer modifying.
  321. * @entry: An allocated and populated pdscpath_entry struct.
  322. * @buf: The input buffer to read from.
  323. * @count: The number of bytes to be read.
  324. *
  325. * We will call this function to change the current layer value.
  326. * Layers are to be given '.'-delimited, without brackets.
  327. * XXX beware we are far less checky WRT input data provided than for hwpath.
  328. * Potential harm can be done, since there's no way to check the validity of
  329. * the layer fields.
  330. */
  331. static ssize_t
  332. pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
  333. {
  334. unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
  335. unsigned short i;
  336. char in[count+1], *temp;
  337. if (!entry || !buf || !count)
  338. return -EINVAL;
  339. /* We'll use a local copy of buf */
  340. memset(in, 0, count+1);
  341. strncpy(in, buf, count);
  342. /* Let's clean up the target. 0 is a blank pattern */
  343. memset(&layers, 0, sizeof(layers));
  344. /* First, pick the first layer */
  345. if (unlikely(!isdigit(*in)))
  346. return -EINVAL;
  347. layers[0] = simple_strtoul(in, NULL, 10);
  348. DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
  349. temp = in;
  350. for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
  351. if (unlikely(!isdigit(*(++temp))))
  352. return -EINVAL;
  353. layers[i] = simple_strtoul(temp, NULL, 10);
  354. DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
  355. }
  356. /* So far so good, let's get in deep */
  357. write_lock(&entry->rw_lock);
  358. /* First, overwrite the current layers with the new ones, not touching
  359. the hardware path. */
  360. memcpy(&entry->devpath.layers, &layers, sizeof(layers));
  361. /* Now, dive in. Write back to the hardware */
  362. pdcspath_store(entry);
  363. write_unlock(&entry->rw_lock);
  364. printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
  365. entry->name, buf);
  366. return count;
  367. }
  368. /**
  369. * pdcspath_attr_show - Generic read function call wrapper.
  370. * @kobj: The kobject to get info from.
  371. * @attr: The attribute looked upon.
  372. * @buf: The output buffer.
  373. */
  374. static ssize_t
  375. pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  376. {
  377. struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
  378. struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
  379. ssize_t ret = 0;
  380. if (pdcs_attr->show)
  381. ret = pdcs_attr->show(entry, buf);
  382. return ret;
  383. }
  384. /**
  385. * pdcspath_attr_store - Generic write function call wrapper.
  386. * @kobj: The kobject to write info to.
  387. * @attr: The attribute to be modified.
  388. * @buf: The input buffer.
  389. * @count: The size of the buffer.
  390. */
  391. static ssize_t
  392. pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
  393. const char *buf, size_t count)
  394. {
  395. struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
  396. struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
  397. ssize_t ret = 0;
  398. if (!capable(CAP_SYS_ADMIN))
  399. return -EACCES;
  400. if (pdcs_attr->store)
  401. ret = pdcs_attr->store(entry, buf, count);
  402. return ret;
  403. }
  404. static const struct sysfs_ops pdcspath_attr_ops = {
  405. .show = pdcspath_attr_show,
  406. .store = pdcspath_attr_store,
  407. };
  408. /* These are the two attributes of any PDC path. */
  409. static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
  410. static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
  411. static struct attribute *paths_subsys_attrs[] = {
  412. &paths_attr_hwpath.attr,
  413. &paths_attr_layer.attr,
  414. NULL,
  415. };
  416. /* Specific kobject type for our PDC paths */
  417. static struct kobj_type ktype_pdcspath = {
  418. .sysfs_ops = &pdcspath_attr_ops,
  419. .default_attrs = paths_subsys_attrs,
  420. };
  421. /* We hard define the 4 types of path we expect to find */
  422. static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
  423. static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
  424. static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
  425. static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
  426. /* An array containing all PDC paths we will deal with */
  427. static struct pdcspath_entry *pdcspath_entries[] = {
  428. &pdcspath_entry_primary,
  429. &pdcspath_entry_alternative,
  430. &pdcspath_entry_console,
  431. &pdcspath_entry_keyboard,
  432. NULL,
  433. };
  434. /* For more insight of what's going on here, refer to PDC Procedures doc,
  435. * Section PDC_STABLE */
  436. /**
  437. * pdcs_size_read - Stable Storage size output.
  438. * @buf: The output buffer to write to.
  439. */
  440. static ssize_t pdcs_size_read(struct kobject *kobj,
  441. struct kobj_attribute *attr,
  442. char *buf)
  443. {
  444. char *out = buf;
  445. if (!buf)
  446. return -EINVAL;
  447. /* show the size of the stable storage */
  448. out += sprintf(out, "%ld\n", pdcs_size);
  449. return out - buf;
  450. }
  451. /**
  452. * pdcs_auto_read - Stable Storage autoboot/search flag output.
  453. * @buf: The output buffer to write to.
  454. * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
  455. */
  456. static ssize_t pdcs_auto_read(struct kobject *kobj,
  457. struct kobj_attribute *attr,
  458. char *buf, int knob)
  459. {
  460. char *out = buf;
  461. struct pdcspath_entry *pathentry;
  462. if (!buf)
  463. return -EINVAL;
  464. /* Current flags are stored in primary boot path entry */
  465. pathentry = &pdcspath_entry_primary;
  466. read_lock(&pathentry->rw_lock);
  467. out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
  468. "On" : "Off");
  469. read_unlock(&pathentry->rw_lock);
  470. return out - buf;
  471. }
  472. /**
  473. * pdcs_autoboot_read - Stable Storage autoboot flag output.
  474. * @buf: The output buffer to write to.
  475. */
  476. static ssize_t pdcs_autoboot_read(struct kobject *kobj,
  477. struct kobj_attribute *attr, char *buf)
  478. {
  479. return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT);
  480. }
  481. /**
  482. * pdcs_autosearch_read - Stable Storage autoboot flag output.
  483. * @buf: The output buffer to write to.
  484. */
  485. static ssize_t pdcs_autosearch_read(struct kobject *kobj,
  486. struct kobj_attribute *attr, char *buf)
  487. {
  488. return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH);
  489. }
  490. /**
  491. * pdcs_timer_read - Stable Storage timer count output (in seconds).
  492. * @buf: The output buffer to write to.
  493. *
  494. * The value of the timer field correponds to a number of seconds in powers of 2.
  495. */
  496. static ssize_t pdcs_timer_read(struct kobject *kobj,
  497. struct kobj_attribute *attr, char *buf)
  498. {
  499. char *out = buf;
  500. struct pdcspath_entry *pathentry;
  501. if (!buf)
  502. return -EINVAL;
  503. /* Current flags are stored in primary boot path entry */
  504. pathentry = &pdcspath_entry_primary;
  505. /* print the timer value in seconds */
  506. read_lock(&pathentry->rw_lock);
  507. out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
  508. (1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
  509. read_unlock(&pathentry->rw_lock);
  510. return out - buf;
  511. }
  512. /**
  513. * pdcs_osid_read - Stable Storage OS ID register output.
  514. * @buf: The output buffer to write to.
  515. */
  516. static ssize_t pdcs_osid_read(struct kobject *kobj,
  517. struct kobj_attribute *attr, char *buf)
  518. {
  519. char *out = buf;
  520. if (!buf)
  521. return -EINVAL;
  522. out += sprintf(out, "%s dependent data (0x%.4x)\n",
  523. os_id_to_string(pdcs_osid), pdcs_osid);
  524. return out - buf;
  525. }
  526. /**
  527. * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
  528. * @buf: The output buffer to write to.
  529. *
  530. * This can hold 16 bytes of OS-Dependent data.
  531. */
  532. static ssize_t pdcs_osdep1_read(struct kobject *kobj,
  533. struct kobj_attribute *attr, char *buf)
  534. {
  535. char *out = buf;
  536. u32 result[4];
  537. if (!buf)
  538. return -EINVAL;
  539. if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
  540. return -EIO;
  541. out += sprintf(out, "0x%.8x\n", result[0]);
  542. out += sprintf(out, "0x%.8x\n", result[1]);
  543. out += sprintf(out, "0x%.8x\n", result[2]);
  544. out += sprintf(out, "0x%.8x\n", result[3]);
  545. return out - buf;
  546. }
  547. /**
  548. * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
  549. * @buf: The output buffer to write to.
  550. *
  551. * I have NFC how to interpret the content of that register ;-).
  552. */
  553. static ssize_t pdcs_diagnostic_read(struct kobject *kobj,
  554. struct kobj_attribute *attr, char *buf)
  555. {
  556. char *out = buf;
  557. u32 result;
  558. if (!buf)
  559. return -EINVAL;
  560. /* get diagnostic */
  561. if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
  562. return -EIO;
  563. out += sprintf(out, "0x%.4x\n", (result >> 16));
  564. return out - buf;
  565. }
  566. /**
  567. * pdcs_fastsize_read - Stable Storage FastSize register output.
  568. * @buf: The output buffer to write to.
  569. *
  570. * This register holds the amount of system RAM to be tested during boot sequence.
  571. */
  572. static ssize_t pdcs_fastsize_read(struct kobject *kobj,
  573. struct kobj_attribute *attr, char *buf)
  574. {
  575. char *out = buf;
  576. u32 result;
  577. if (!buf)
  578. return -EINVAL;
  579. /* get fast-size */
  580. if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
  581. return -EIO;
  582. if ((result & 0x0F) < 0x0E)
  583. out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
  584. else
  585. out += sprintf(out, "All");
  586. out += sprintf(out, "\n");
  587. return out - buf;
  588. }
  589. /**
  590. * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
  591. * @buf: The output buffer to write to.
  592. *
  593. * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
  594. */
  595. static ssize_t pdcs_osdep2_read(struct kobject *kobj,
  596. struct kobj_attribute *attr, char *buf)
  597. {
  598. char *out = buf;
  599. unsigned long size;
  600. unsigned short i;
  601. u32 result;
  602. if (unlikely(pdcs_size <= 224))
  603. return -ENODATA;
  604. size = pdcs_size - 224;
  605. if (!buf)
  606. return -EINVAL;
  607. for (i=0; i<size; i+=4) {
  608. if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
  609. sizeof(result)) != PDC_OK))
  610. return -EIO;
  611. out += sprintf(out, "0x%.8x\n", result);
  612. }
  613. return out - buf;
  614. }
  615. /**
  616. * pdcs_auto_write - This function handles autoboot/search flag modifying.
  617. * @buf: The input buffer to read from.
  618. * @count: The number of bytes to be read.
  619. * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
  620. *
  621. * We will call this function to change the current autoboot flag.
  622. * We expect a precise syntax:
  623. * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
  624. */
  625. static ssize_t pdcs_auto_write(struct kobject *kobj,
  626. struct kobj_attribute *attr, const char *buf,
  627. size_t count, int knob)
  628. {
  629. struct pdcspath_entry *pathentry;
  630. unsigned char flags;
  631. char in[count+1], *temp;
  632. char c;
  633. if (!capable(CAP_SYS_ADMIN))
  634. return -EACCES;
  635. if (!buf || !count)
  636. return -EINVAL;
  637. /* We'll use a local copy of buf */
  638. memset(in, 0, count+1);
  639. strncpy(in, buf, count);
  640. /* Current flags are stored in primary boot path entry */
  641. pathentry = &pdcspath_entry_primary;
  642. /* Be nice to the existing flag record */
  643. read_lock(&pathentry->rw_lock);
  644. flags = pathentry->devpath.flags;
  645. read_unlock(&pathentry->rw_lock);
  646. DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
  647. temp = skip_spaces(in);
  648. c = *temp++ - '0';
  649. if ((c != 0) && (c != 1))
  650. goto parse_error;
  651. if (c == 0)
  652. flags &= ~knob;
  653. else
  654. flags |= knob;
  655. DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
  656. /* So far so good, let's get in deep */
  657. write_lock(&pathentry->rw_lock);
  658. /* Change the path entry flags first */
  659. pathentry->devpath.flags = flags;
  660. /* Now, dive in. Write back to the hardware */
  661. pdcspath_store(pathentry);
  662. write_unlock(&pathentry->rw_lock);
  663. printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
  664. (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
  665. (flags & knob) ? "On" : "Off");
  666. return count;
  667. parse_error:
  668. printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
  669. return -EINVAL;
  670. }
  671. /**
  672. * pdcs_autoboot_write - This function handles autoboot flag modifying.
  673. * @buf: The input buffer to read from.
  674. * @count: The number of bytes to be read.
  675. *
  676. * We will call this function to change the current boot flags.
  677. * We expect a precise syntax:
  678. * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
  679. */
  680. static ssize_t pdcs_autoboot_write(struct kobject *kobj,
  681. struct kobj_attribute *attr,
  682. const char *buf, size_t count)
  683. {
  684. return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT);
  685. }
  686. /**
  687. * pdcs_autosearch_write - This function handles autosearch flag modifying.
  688. * @buf: The input buffer to read from.
  689. * @count: The number of bytes to be read.
  690. *
  691. * We will call this function to change the current boot flags.
  692. * We expect a precise syntax:
  693. * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
  694. */
  695. static ssize_t pdcs_autosearch_write(struct kobject *kobj,
  696. struct kobj_attribute *attr,
  697. const char *buf, size_t count)
  698. {
  699. return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH);
  700. }
  701. /**
  702. * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
  703. * @buf: The input buffer to read from.
  704. * @count: The number of bytes to be read.
  705. *
  706. * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
  707. * write approach. It's up to userspace to deal with it when constructing
  708. * its input buffer.
  709. */
  710. static ssize_t pdcs_osdep1_write(struct kobject *kobj,
  711. struct kobj_attribute *attr,
  712. const char *buf, size_t count)
  713. {
  714. u8 in[16];
  715. if (!capable(CAP_SYS_ADMIN))
  716. return -EACCES;
  717. if (!buf || !count)
  718. return -EINVAL;
  719. if (unlikely(pdcs_osid != OS_ID_LINUX))
  720. return -EPERM;
  721. if (count > 16)
  722. return -EMSGSIZE;
  723. /* We'll use a local copy of buf */
  724. memset(in, 0, 16);
  725. memcpy(in, buf, count);
  726. if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
  727. return -EIO;
  728. return count;
  729. }
  730. /**
  731. * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
  732. * @buf: The input buffer to read from.
  733. * @count: The number of bytes to be read.
  734. *
  735. * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
  736. * byte-by-byte write approach. It's up to userspace to deal with it when
  737. * constructing its input buffer.
  738. */
  739. static ssize_t pdcs_osdep2_write(struct kobject *kobj,
  740. struct kobj_attribute *attr,
  741. const char *buf, size_t count)
  742. {
  743. unsigned long size;
  744. unsigned short i;
  745. u8 in[4];
  746. if (!capable(CAP_SYS_ADMIN))
  747. return -EACCES;
  748. if (!buf || !count)
  749. return -EINVAL;
  750. if (unlikely(pdcs_size <= 224))
  751. return -ENOSYS;
  752. if (unlikely(pdcs_osid != OS_ID_LINUX))
  753. return -EPERM;
  754. size = pdcs_size - 224;
  755. if (count > size)
  756. return -EMSGSIZE;
  757. /* We'll use a local copy of buf */
  758. for (i=0; i<count; i+=4) {
  759. memset(in, 0, 4);
  760. memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
  761. if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
  762. sizeof(in)) != PDC_OK))
  763. return -EIO;
  764. }
  765. return count;
  766. }
  767. /* The remaining attributes. */
  768. static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
  769. static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
  770. static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
  771. static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
  772. static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
  773. static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
  774. static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
  775. static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
  776. static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
  777. static struct attribute *pdcs_subsys_attrs[] = {
  778. &pdcs_attr_size.attr,
  779. &pdcs_attr_autoboot.attr,
  780. &pdcs_attr_autosearch.attr,
  781. &pdcs_attr_timer.attr,
  782. &pdcs_attr_osid.attr,
  783. &pdcs_attr_osdep1.attr,
  784. &pdcs_attr_diagnostic.attr,
  785. &pdcs_attr_fastsize.attr,
  786. &pdcs_attr_osdep2.attr,
  787. NULL,
  788. };
  789. static struct attribute_group pdcs_attr_group = {
  790. .attrs = pdcs_subsys_attrs,
  791. };
  792. static struct kobject *stable_kobj;
  793. static struct kset *paths_kset;
  794. /**
  795. * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
  796. *
  797. * It creates kobjects corresponding to each path entry with nice sysfs
  798. * links to the real device. This is where the magic takes place: when
  799. * registering the subsystem attributes during module init, each kobject hereby
  800. * created will show in the sysfs tree as a folder containing files as defined
  801. * by path_subsys_attr[].
  802. */
  803. static inline int __init
  804. pdcs_register_pathentries(void)
  805. {
  806. unsigned short i;
  807. struct pdcspath_entry *entry;
  808. int err;
  809. /* Initialize the entries rw_lock before anything else */
  810. for (i = 0; (entry = pdcspath_entries[i]); i++)
  811. rwlock_init(&entry->rw_lock);
  812. for (i = 0; (entry = pdcspath_entries[i]); i++) {
  813. write_lock(&entry->rw_lock);
  814. err = pdcspath_fetch(entry);
  815. write_unlock(&entry->rw_lock);
  816. if (err < 0)
  817. continue;
  818. entry->kobj.kset = paths_kset;
  819. err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL,
  820. "%s", entry->name);
  821. if (err)
  822. return err;
  823. /* kobject is now registered */
  824. write_lock(&entry->rw_lock);
  825. entry->ready = 2;
  826. /* Add a nice symlink to the real device */
  827. if (entry->dev) {
  828. err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
  829. WARN_ON(err);
  830. }
  831. write_unlock(&entry->rw_lock);
  832. kobject_uevent(&entry->kobj, KOBJ_ADD);
  833. }
  834. return 0;
  835. }
  836. /**
  837. * pdcs_unregister_pathentries - Routine called when unregistering the module.
  838. */
  839. static inline void
  840. pdcs_unregister_pathentries(void)
  841. {
  842. unsigned short i;
  843. struct pdcspath_entry *entry;
  844. for (i = 0; (entry = pdcspath_entries[i]); i++) {
  845. read_lock(&entry->rw_lock);
  846. if (entry->ready >= 2)
  847. kobject_put(&entry->kobj);
  848. read_unlock(&entry->rw_lock);
  849. }
  850. }
  851. /*
  852. * For now we register the stable subsystem with the firmware subsystem
  853. * and the paths subsystem with the stable subsystem
  854. */
  855. static int __init
  856. pdc_stable_init(void)
  857. {
  858. int rc = 0, error = 0;
  859. u32 result;
  860. /* find the size of the stable storage */
  861. if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
  862. return -ENODEV;
  863. /* make sure we have enough data */
  864. if (pdcs_size < 96)
  865. return -ENODATA;
  866. printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
  867. /* get OSID */
  868. if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
  869. return -EIO;
  870. /* the actual result is 16 bits away */
  871. pdcs_osid = (u16)(result >> 16);
  872. /* For now we'll register the directory at /sys/firmware/stable */
  873. stable_kobj = kobject_create_and_add("stable", firmware_kobj);
  874. if (!stable_kobj) {
  875. rc = -ENOMEM;
  876. goto fail_firmreg;
  877. }
  878. /* Don't forget the root entries */
  879. error = sysfs_create_group(stable_kobj, &pdcs_attr_group);
  880. /* register the paths kset as a child of the stable kset */
  881. paths_kset = kset_create_and_add("paths", NULL, stable_kobj);
  882. if (!paths_kset) {
  883. rc = -ENOMEM;
  884. goto fail_ksetreg;
  885. }
  886. /* now we create all "files" for the paths kset */
  887. if ((rc = pdcs_register_pathentries()))
  888. goto fail_pdcsreg;
  889. return rc;
  890. fail_pdcsreg:
  891. pdcs_unregister_pathentries();
  892. kset_unregister(paths_kset);
  893. fail_ksetreg:
  894. kobject_put(stable_kobj);
  895. fail_firmreg:
  896. printk(KERN_INFO PDCS_PREFIX " bailing out\n");
  897. return rc;
  898. }
  899. static void __exit
  900. pdc_stable_exit(void)
  901. {
  902. pdcs_unregister_pathentries();
  903. kset_unregister(paths_kset);
  904. kobject_put(stable_kobj);
  905. }
  906. module_init(pdc_stable_init);
  907. module_exit(pdc_stable_exit);