acpiphp_ibm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * ACPI PCI Hot Plug IBM Extension
  3. *
  4. * Copyright (C) 2004 Vernon Mauery <vernux@us.ibm.com>
  5. * Copyright (C) 2004 IBM Corp.
  6. *
  7. * All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  17. * NON INFRINGEMENT. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * Send feedback to <vernux@us.ibm.com>
  25. *
  26. */
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <acpi/acpi_bus.h>
  31. #include <linux/sysfs.h>
  32. #include <linux/kobject.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/moduleparam.h>
  35. #include "acpiphp.h"
  36. #include "pci_hotplug.h"
  37. #define DRIVER_VERSION "1.0.1"
  38. #define DRIVER_AUTHOR "Irene Zubarev <zubarev@us.ibm.com>, Vernon Mauery <vernux@us.ibm.com>"
  39. #define DRIVER_DESC "ACPI Hot Plug PCI Controller Driver IBM extension"
  40. static int debug;
  41. MODULE_AUTHOR(DRIVER_AUTHOR);
  42. MODULE_DESCRIPTION(DRIVER_DESC);
  43. MODULE_LICENSE("GPL");
  44. MODULE_VERSION(DRIVER_VERSION);
  45. module_param(debug, bool, 0644);
  46. MODULE_PARM_DESC(debug, " Debugging mode enabled or not");
  47. #define MY_NAME "acpiphp_ibm"
  48. #undef dbg
  49. #define dbg(format, arg...) \
  50. do { \
  51. if (debug) \
  52. printk(KERN_DEBUG "%s: " format, \
  53. MY_NAME , ## arg); \
  54. } while (0)
  55. #define FOUND_APCI 0x61504349
  56. /* these are the names for the IBM ACPI pseudo-device */
  57. #define IBM_HARDWARE_ID1 "IBM37D0"
  58. #define IBM_HARDWARE_ID2 "IBM37D4"
  59. #define hpslot_to_sun(A) (((struct slot *)((A)->private))->acpi_slot->sun)
  60. /* union apci_descriptor - allows access to the
  61. * various device descriptors that are embedded in the
  62. * aPCI table
  63. */
  64. union apci_descriptor {
  65. struct {
  66. char sig[4];
  67. u8 len;
  68. } header;
  69. struct {
  70. u8 type;
  71. u8 len;
  72. u16 slot_id;
  73. u8 bus_id;
  74. u8 dev_num;
  75. u8 slot_num;
  76. u8 slot_attr[2];
  77. u8 attn;
  78. u8 status[2];
  79. u8 sun;
  80. u8 res[3];
  81. } slot;
  82. struct {
  83. u8 type;
  84. u8 len;
  85. } generic;
  86. };
  87. /* struct notification - keeps info about the device
  88. * that cause the ACPI notification event
  89. */
  90. struct notification {
  91. struct acpi_device *device;
  92. u8 event;
  93. };
  94. static int ibm_set_attention_status(struct hotplug_slot *slot, u8 status);
  95. static int ibm_get_attention_status(struct hotplug_slot *slot, u8 *status);
  96. static void ibm_handle_events(acpi_handle handle, u32 event, void *context);
  97. static int ibm_get_table_from_acpi(char **bufp);
  98. static ssize_t ibm_read_apci_table(struct kobject *kobj,
  99. char *buffer, loff_t pos, size_t size);
  100. static acpi_status __init ibm_find_acpi_device(acpi_handle handle,
  101. u32 lvl, void *context, void **rv);
  102. static int __init ibm_acpiphp_init(void);
  103. static void __exit ibm_acpiphp_exit(void);
  104. static acpi_handle ibm_acpi_handle;
  105. static struct notification ibm_note;
  106. static struct bin_attribute ibm_apci_table_attr = {
  107. .attr = {
  108. .name = "apci_table",
  109. .owner = THIS_MODULE,
  110. .mode = S_IRUGO,
  111. },
  112. .read = ibm_read_apci_table,
  113. .write = NULL,
  114. };
  115. static struct acpiphp_attention_info ibm_attention_info =
  116. {
  117. .set_attn = ibm_set_attention_status,
  118. .get_attn = ibm_get_attention_status,
  119. .owner = THIS_MODULE,
  120. };
  121. /**
  122. * ibm_slot_from_id - workaround for bad ibm hardware
  123. * @id: the slot number that linux refers to the slot by
  124. *
  125. * Description: this method returns the aCPI slot descriptor
  126. * corresponding to the Linux slot number. This descriptor
  127. * has info about the aPCI slot id and attention status.
  128. * This descriptor must be freed using kfree when done.
  129. **/
  130. static union apci_descriptor *ibm_slot_from_id(int id)
  131. {
  132. int ind = 0, size;
  133. union apci_descriptor *ret = NULL, *des;
  134. char *table;
  135. size = ibm_get_table_from_acpi(&table);
  136. des = (union apci_descriptor *)table;
  137. if (memcmp(des->header.sig, "aPCI", 4) != 0)
  138. goto ibm_slot_done;
  139. des = (union apci_descriptor *)&table[ind += des->header.len];
  140. while (ind < size && (des->generic.type != 0x82 ||
  141. des->slot.slot_num != id)) {
  142. des = (union apci_descriptor *)&table[ind += des->generic.len];
  143. }
  144. if (ind < size && des->slot.slot_num == id)
  145. ret = des;
  146. ibm_slot_done:
  147. if (ret) {
  148. ret = kmalloc(sizeof(union apci_descriptor), GFP_KERNEL);
  149. memcpy(ret, des, sizeof(union apci_descriptor));
  150. }
  151. kfree(table);
  152. return ret;
  153. }
  154. /**
  155. * ibm_set_attention_status - callback method to set the attention LED
  156. * @slot: the hotplug_slot to work with
  157. * @status: what to set the LED to (0 or 1)
  158. *
  159. * Description: this method is registered with the acpiphp module as a
  160. * callback to do the device specific task of setting the LED status
  161. **/
  162. static int ibm_set_attention_status(struct hotplug_slot *slot, u8 status)
  163. {
  164. union acpi_object args[2];
  165. struct acpi_object_list params = { .pointer = args, .count = 2 };
  166. acpi_status stat;
  167. unsigned long rc;
  168. union apci_descriptor *ibm_slot;
  169. ibm_slot = ibm_slot_from_id(hpslot_to_sun(slot));
  170. dbg("%s: set slot %d (%d) attention status to %d\n", __FUNCTION__,
  171. ibm_slot->slot.slot_num, ibm_slot->slot.slot_id,
  172. (status ? 1 : 0));
  173. args[0].type = ACPI_TYPE_INTEGER;
  174. args[0].integer.value = ibm_slot->slot.slot_id;
  175. args[1].type = ACPI_TYPE_INTEGER;
  176. args[1].integer.value = (status) ? 1 : 0;
  177. kfree(ibm_slot);
  178. stat = acpi_evaluate_integer(ibm_acpi_handle, "APLS", &params, &rc);
  179. if (ACPI_FAILURE(stat)) {
  180. err("APLS evaluation failed: 0x%08x\n", stat);
  181. return -ENODEV;
  182. } else if (!rc) {
  183. err("APLS method failed: 0x%08lx\n", rc);
  184. return -ERANGE;
  185. }
  186. return 0;
  187. }
  188. /**
  189. * ibm_get_attention_status - callback method to get attention LED status
  190. * @slot: the hotplug_slot to work with
  191. * @status: returns what the LED is set to (0 or 1)
  192. *
  193. * Description: this method is registered with the acpiphp module as a
  194. * callback to do the device specific task of getting the LED status
  195. *
  196. * Because there is no direct method of getting the LED status directly
  197. * from an ACPI call, we read the aPCI table and parse out our
  198. * slot descriptor to read the status from that.
  199. **/
  200. static int ibm_get_attention_status(struct hotplug_slot *slot, u8 *status)
  201. {
  202. union apci_descriptor *ibm_slot;
  203. ibm_slot = ibm_slot_from_id(hpslot_to_sun(slot));
  204. if (ibm_slot->slot.attn & 0xa0 || ibm_slot->slot.status[1] & 0x08)
  205. *status = 1;
  206. else
  207. *status = 0;
  208. dbg("%s: get slot %d (%d) attention status is %d\n", __FUNCTION__,
  209. ibm_slot->slot.slot_num, ibm_slot->slot.slot_id,
  210. *status);
  211. kfree(ibm_slot);
  212. return 0;
  213. }
  214. /**
  215. * ibm_handle_events - listens for ACPI events for the IBM37D0 device
  216. * @handle: an ACPI handle to the device that caused the event
  217. * @event: the event info (device specific)
  218. * @context: passed context (our notification struct)
  219. *
  220. * Description: this method is registered as a callback with the ACPI
  221. * subsystem it is called when this device has an event to notify the OS of
  222. *
  223. * The events actually come from the device as two events that get
  224. * synthesized into one event with data by this function. The event
  225. * ID comes first and then the slot number that caused it. We report
  226. * this as one event to the OS.
  227. *
  228. * From section 5.6.2.2 of the ACPI 2.0 spec, I understand that the OSPM will
  229. * only re-enable the interrupt that causes this event AFTER this method
  230. * has returned, thereby enforcing serial access for the notification struct.
  231. **/
  232. static void ibm_handle_events(acpi_handle handle, u32 event, void *context)
  233. {
  234. u8 detail = event & 0x0f;
  235. u8 subevent = event & 0xf0;
  236. struct notification *note = context;
  237. dbg("%s: Received notification %02x\n", __FUNCTION__, event);
  238. if (subevent == 0x80) {
  239. dbg("%s: generationg bus event\n", __FUNCTION__);
  240. acpi_bus_generate_event(note->device, note->event, detail);
  241. } else
  242. note->event = event;
  243. }
  244. /**
  245. * ibm_get_table_from_acpi - reads the APLS buffer from ACPI
  246. * @bufp: address to pointer to allocate for the table
  247. *
  248. * Description: this method reads the APLS buffer in from ACPI and
  249. * stores the "stripped" table into a single buffer
  250. * it allocates and passes the address back in bufp
  251. *
  252. * If NULL is passed in as buffer, this method only calculates
  253. * the size of the table and returns that without filling
  254. * in the buffer
  255. *
  256. * returns < 0 on error or the size of the table on success
  257. **/
  258. static int ibm_get_table_from_acpi(char **bufp)
  259. {
  260. union acpi_object *package;
  261. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  262. acpi_status status;
  263. char *lbuf = NULL;
  264. int i, size = -EIO;
  265. status = acpi_evaluate_object(ibm_acpi_handle, "APCI", NULL, &buffer);
  266. if (ACPI_FAILURE(status)) {
  267. err("%s: APCI evaluation failed\n", __FUNCTION__);
  268. return -ENODEV;
  269. }
  270. package = (union acpi_object *) buffer.pointer;
  271. if (!(package) ||
  272. (package->type != ACPI_TYPE_PACKAGE) ||
  273. !(package->package.elements)) {
  274. err("%s: Invalid APCI object\n", __FUNCTION__);
  275. goto read_table_done;
  276. }
  277. for(size = 0, i = 0; i < package->package.count; i++) {
  278. if (package->package.elements[i].type != ACPI_TYPE_BUFFER) {
  279. err("%s: Invalid APCI element %d\n", __FUNCTION__, i);
  280. goto read_table_done;
  281. }
  282. size += package->package.elements[i].buffer.length;
  283. }
  284. if (bufp == NULL)
  285. goto read_table_done;
  286. lbuf = kmalloc(size, GFP_KERNEL);
  287. dbg("%s: element count: %i, ASL table size: %i, &table = 0x%p\n",
  288. __FUNCTION__, package->package.count, size, lbuf);
  289. if (lbuf) {
  290. *bufp = lbuf;
  291. memset(lbuf, 0, size);
  292. } else {
  293. size = -ENOMEM;
  294. goto read_table_done;
  295. }
  296. size = 0;
  297. for (i=0; i<package->package.count; i++) {
  298. memcpy(&lbuf[size],
  299. package->package.elements[i].buffer.pointer,
  300. package->package.elements[i].buffer.length);
  301. size += package->package.elements[i].buffer.length;
  302. }
  303. read_table_done:
  304. kfree(buffer.pointer);
  305. return size;
  306. }
  307. /**
  308. * ibm_read_apci_table - callback for the sysfs apci_table file
  309. * @kobj: the kobject this binary attribute is a part of
  310. * @buffer: the kernel space buffer to fill
  311. * @pos: the offset into the file
  312. * @size: the number of bytes requested
  313. *
  314. * Description: gets registered with sysfs as the reader callback
  315. * to be executed when /sys/bus/pci/slots/apci_table gets read
  316. *
  317. * Since we don't get notified on open and close for this file,
  318. * things get really tricky here...
  319. * our solution is to only allow reading the table in all at once
  320. **/
  321. static ssize_t ibm_read_apci_table(struct kobject *kobj,
  322. char *buffer, loff_t pos, size_t size)
  323. {
  324. int bytes_read = -EINVAL;
  325. char *table = NULL;
  326. dbg("%s: pos = %d, size = %zd\n", __FUNCTION__, (int)pos, size);
  327. if (pos == 0) {
  328. bytes_read = ibm_get_table_from_acpi(&table);
  329. if (bytes_read > 0 && bytes_read <= size)
  330. memcpy(buffer, table, bytes_read);
  331. kfree(table);
  332. }
  333. return bytes_read;
  334. }
  335. /**
  336. * ibm_find_acpi_device - callback to find our ACPI device
  337. * @handle: the ACPI handle of the device we are inspecting
  338. * @lvl: depth into the namespace tree
  339. * @context: a pointer to our handle to fill when we find the device
  340. * @rv: a return value to fill if desired
  341. *
  342. * Description: used as a callback when calling acpi_walk_namespace
  343. * to find our device. When this method returns non-zero
  344. * acpi_walk_namespace quits its search and returns our value
  345. **/
  346. static acpi_status __init ibm_find_acpi_device(acpi_handle handle,
  347. u32 lvl, void *context, void **rv)
  348. {
  349. acpi_handle *phandle = (acpi_handle *)context;
  350. acpi_status status;
  351. struct acpi_device_info info;
  352. struct acpi_buffer info_buffer = {
  353. .length = sizeof(struct acpi_device_info),
  354. .pointer = &info,
  355. };
  356. status = acpi_get_object_info(handle, &info_buffer);
  357. if (ACPI_FAILURE(status)) {
  358. err("%s: Failed to get device information", __FUNCTION__);
  359. return 0;
  360. }
  361. info.hardware_id.value[sizeof(info.hardware_id.value) - 1] = '\0';
  362. if (info.current_status && (info.valid & ACPI_VALID_HID) &&
  363. (!strcmp(info.hardware_id.value, IBM_HARDWARE_ID1) ||
  364. !strcmp(info.hardware_id.value, IBM_HARDWARE_ID2))) {
  365. dbg("found hardware: %s, handle: %p\n", info.hardware_id.value,
  366. handle);
  367. *phandle = handle;
  368. /* returning non-zero causes the search to stop
  369. * and returns this value to the caller of
  370. * acpi_walk_namespace, but it also causes some warnings
  371. * in the acpi debug code to print...
  372. */
  373. return FOUND_APCI;
  374. }
  375. return 0;
  376. }
  377. static int __init ibm_acpiphp_init(void)
  378. {
  379. int retval = 0;
  380. acpi_status status;
  381. struct acpi_device *device;
  382. struct kobject *sysdir = &pci_hotplug_slots_subsys.kset.kobj;
  383. dbg("%s\n", __FUNCTION__);
  384. if (acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  385. ACPI_UINT32_MAX, ibm_find_acpi_device,
  386. &ibm_acpi_handle, NULL) != FOUND_APCI) {
  387. err("%s: acpi_walk_namespace failed\n", __FUNCTION__);
  388. retval = -ENODEV;
  389. goto init_return;
  390. }
  391. dbg("%s: found IBM aPCI device\n", __FUNCTION__);
  392. if (acpi_bus_get_device(ibm_acpi_handle, &device)) {
  393. err("%s: acpi_bus_get_device failed\n", __FUNCTION__);
  394. retval = -ENODEV;
  395. goto init_return;
  396. }
  397. if (acpiphp_register_attention(&ibm_attention_info)) {
  398. retval = -ENODEV;
  399. goto init_return;
  400. }
  401. ibm_note.device = device;
  402. status = acpi_install_notify_handler(ibm_acpi_handle,
  403. ACPI_DEVICE_NOTIFY, ibm_handle_events,
  404. &ibm_note);
  405. if (ACPI_FAILURE(status)) {
  406. err("%s: Failed to register notification handler\n",
  407. __FUNCTION__);
  408. retval = -EBUSY;
  409. goto init_cleanup;
  410. }
  411. ibm_apci_table_attr.size = ibm_get_table_from_acpi(NULL);
  412. retval = sysfs_create_bin_file(sysdir, &ibm_apci_table_attr);
  413. return retval;
  414. init_cleanup:
  415. acpiphp_unregister_attention(&ibm_attention_info);
  416. init_return:
  417. return retval;
  418. }
  419. static void __exit ibm_acpiphp_exit(void)
  420. {
  421. acpi_status status;
  422. struct kobject *sysdir = &pci_hotplug_slots_subsys.kset.kobj;
  423. dbg("%s\n", __FUNCTION__);
  424. if (acpiphp_unregister_attention(&ibm_attention_info))
  425. err("%s: attention info deregistration failed", __FUNCTION__);
  426. status = acpi_remove_notify_handler(
  427. ibm_acpi_handle,
  428. ACPI_DEVICE_NOTIFY,
  429. ibm_handle_events);
  430. if (ACPI_FAILURE(status))
  431. err("%s: Notification handler removal failed\n", __FUNCTION__);
  432. /* remove the /sys entries */
  433. if (sysfs_remove_bin_file(sysdir, &ibm_apci_table_attr))
  434. err("%s: removal of sysfs file apci_table failed\n",
  435. __FUNCTION__);
  436. }
  437. module_init(ibm_acpiphp_init);
  438. module_exit(ibm_acpiphp_exit);