acpiphp_ibm.c 14 KB

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