acpiphp_ibm.c 14 KB

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