pdc_stable.c 31 KB

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