pdc_stable.c 31 KB

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