pdc_stable.c 30 KB

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