acpiphp_ibm.c 14 KB

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