acpiphp_glue.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /*
  2. * ACPI PCI HotPlug glue functions to ACPI CA subsystem
  3. *
  4. * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
  5. * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
  6. * Copyright (C) 2002,2003 NEC Corporation
  7. * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
  8. * Copyright (C) 2003-2005 Hewlett Packard
  9. * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
  10. * Copyright (C) 2005 Intel Corporation
  11. *
  12. * All rights reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or (at
  17. * your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  22. * NON INFRINGEMENT. See the GNU General Public License for more
  23. * details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. * Send feedback to <kristen.c.accardi@intel.com>
  30. *
  31. */
  32. /*
  33. * Lifetime rules for pci_dev:
  34. * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
  35. * when the bridge is scanned and it loses a refcount when the bridge
  36. * is removed.
  37. * - When a P2P bridge is present, we elevate the refcount on the subordinate
  38. * bus. It loses the refcount when the the driver unloads.
  39. */
  40. #include <linux/init.h>
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/pci.h>
  44. #include <linux/pci_hotplug.h>
  45. #include <linux/pci-acpi.h>
  46. #include <linux/mutex.h>
  47. #include <linux/slab.h>
  48. #include <linux/acpi.h>
  49. #include "../pci.h"
  50. #include "acpiphp.h"
  51. static LIST_HEAD(bridge_list);
  52. #define MY_NAME "acpiphp_glue"
  53. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  54. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  55. static void acpiphp_set_hpp_values(struct pci_bus *bus);
  56. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
  57. /* callback routine to check for the existence of a pci dock device */
  58. static acpi_status
  59. is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
  60. {
  61. int *count = (int *)context;
  62. if (is_dock_device(handle)) {
  63. (*count)++;
  64. return AE_CTRL_TERMINATE;
  65. } else {
  66. return AE_OK;
  67. }
  68. }
  69. /*
  70. * the _DCK method can do funny things... and sometimes not
  71. * hah-hah funny.
  72. *
  73. * TBD - figure out a way to only call fixups for
  74. * systems that require them.
  75. */
  76. static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
  77. void *v)
  78. {
  79. struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
  80. struct pci_bus *bus = func->slot->bridge->pci_bus;
  81. u32 buses;
  82. if (!bus->self)
  83. return NOTIFY_OK;
  84. /* fixup bad _DCK function that rewrites
  85. * secondary bridge on slot
  86. */
  87. pci_read_config_dword(bus->self,
  88. PCI_PRIMARY_BUS,
  89. &buses);
  90. if (((buses >> 8) & 0xff) != bus->busn_res.start) {
  91. buses = (buses & 0xff000000)
  92. | ((unsigned int)(bus->primary) << 0)
  93. | ((unsigned int)(bus->busn_res.start) << 8)
  94. | ((unsigned int)(bus->busn_res.end) << 16);
  95. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  96. }
  97. return NOTIFY_OK;
  98. }
  99. static const struct acpi_dock_ops acpiphp_dock_ops = {
  100. .handler = handle_hotplug_event_func,
  101. };
  102. /* Check whether the PCI device is managed by native PCIe hotplug driver */
  103. static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
  104. {
  105. u32 reg32;
  106. acpi_handle tmp;
  107. struct acpi_pci_root *root;
  108. /* Check whether the PCIe port supports native PCIe hotplug */
  109. if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
  110. return false;
  111. if (!(reg32 & PCI_EXP_SLTCAP_HPC))
  112. return false;
  113. /*
  114. * Check whether native PCIe hotplug has been enabled for
  115. * this PCIe hierarchy.
  116. */
  117. tmp = acpi_find_root_bridge_handle(pdev);
  118. if (!tmp)
  119. return false;
  120. root = acpi_pci_find_root(tmp);
  121. if (!root)
  122. return false;
  123. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  124. return false;
  125. return true;
  126. }
  127. /* callback routine to register each ACPI PCI slot object */
  128. static acpi_status
  129. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  130. {
  131. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
  132. struct acpiphp_slot *slot;
  133. struct acpiphp_func *newfunc;
  134. acpi_handle tmp;
  135. acpi_status status = AE_OK;
  136. unsigned long long adr, sun;
  137. int device, function, retval;
  138. struct pci_bus *pbus = bridge->pci_bus;
  139. struct pci_dev *pdev;
  140. if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
  141. return AE_OK;
  142. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  143. if (ACPI_FAILURE(status)) {
  144. warn("can't evaluate _ADR (%#x)\n", status);
  145. return AE_OK;
  146. }
  147. device = (adr >> 16) & 0xffff;
  148. function = adr & 0xffff;
  149. pdev = pbus->self;
  150. if (pdev && device_is_managed_by_native_pciehp(pdev))
  151. return AE_OK;
  152. newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
  153. if (!newfunc)
  154. return AE_NO_MEMORY;
  155. INIT_LIST_HEAD(&newfunc->sibling);
  156. newfunc->handle = handle;
  157. newfunc->function = function;
  158. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  159. newfunc->flags = FUNC_HAS_EJ0;
  160. if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
  161. newfunc->flags |= FUNC_HAS_STA;
  162. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
  163. newfunc->flags |= FUNC_HAS_PS0;
  164. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
  165. newfunc->flags |= FUNC_HAS_PS3;
  166. if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
  167. newfunc->flags |= FUNC_HAS_DCK;
  168. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  169. if (ACPI_FAILURE(status)) {
  170. /*
  171. * use the count of the number of slots we've found
  172. * for the number of the slot
  173. */
  174. sun = bridge->nr_slots+1;
  175. }
  176. /* search for objects that share the same slot */
  177. for (slot = bridge->slots; slot; slot = slot->next)
  178. if (slot->device == device) {
  179. if (slot->sun != sun)
  180. warn("sibling found, but _SUN doesn't match!\n");
  181. break;
  182. }
  183. if (!slot) {
  184. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  185. if (!slot) {
  186. kfree(newfunc);
  187. return AE_NO_MEMORY;
  188. }
  189. slot->bridge = bridge;
  190. slot->device = device;
  191. slot->sun = sun;
  192. INIT_LIST_HEAD(&slot->funcs);
  193. mutex_init(&slot->crit_sect);
  194. slot->next = bridge->slots;
  195. bridge->slots = slot;
  196. bridge->nr_slots++;
  197. dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  198. slot->sun, pci_domain_nr(pbus), pbus->number, device);
  199. retval = acpiphp_register_hotplug_slot(slot);
  200. if (retval) {
  201. if (retval == -EBUSY)
  202. warn("Slot %llu already registered by another "
  203. "hotplug driver\n", slot->sun);
  204. else
  205. warn("acpiphp_register_hotplug_slot failed "
  206. "(err code = 0x%x)\n", retval);
  207. goto err_exit;
  208. }
  209. }
  210. newfunc->slot = slot;
  211. list_add_tail(&newfunc->sibling, &slot->funcs);
  212. pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
  213. if (pdev) {
  214. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  215. pci_dev_put(pdev);
  216. }
  217. if (is_dock_device(handle)) {
  218. /* we don't want to call this device's _EJ0
  219. * because we want the dock notify handler
  220. * to call it after it calls _DCK
  221. */
  222. newfunc->flags &= ~FUNC_HAS_EJ0;
  223. if (register_hotplug_dock_device(handle,
  224. &acpiphp_dock_ops, newfunc))
  225. dbg("failed to register dock device\n");
  226. /* we need to be notified when dock events happen
  227. * outside of the hotplug operation, since we may
  228. * need to do fixups before we can hotplug.
  229. */
  230. newfunc->nb.notifier_call = post_dock_fixups;
  231. if (register_dock_notifier(&newfunc->nb))
  232. dbg("failed to register a dock notifier");
  233. }
  234. /* install notify handler */
  235. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  236. status = acpi_install_notify_handler(handle,
  237. ACPI_SYSTEM_NOTIFY,
  238. handle_hotplug_event_func,
  239. newfunc);
  240. if (ACPI_FAILURE(status))
  241. err("failed to register interrupt notify handler\n");
  242. } else
  243. status = AE_OK;
  244. return status;
  245. err_exit:
  246. bridge->nr_slots--;
  247. bridge->slots = slot->next;
  248. kfree(slot);
  249. kfree(newfunc);
  250. return AE_OK;
  251. }
  252. /* see if it's worth looking at this bridge */
  253. static int detect_ejectable_slots(acpi_handle handle)
  254. {
  255. int found = acpi_pci_detect_ejectable(handle);
  256. if (!found) {
  257. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  258. is_pci_dock_device, NULL, (void *)&found, NULL);
  259. }
  260. return found;
  261. }
  262. /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
  263. static void init_bridge_misc(struct acpiphp_bridge *bridge)
  264. {
  265. acpi_status status;
  266. /* must be added to the list prior to calling register_slot */
  267. list_add(&bridge->list, &bridge_list);
  268. /* register all slot objects under this bridge */
  269. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
  270. register_slot, NULL, bridge, NULL);
  271. if (ACPI_FAILURE(status)) {
  272. list_del(&bridge->list);
  273. return;
  274. }
  275. /* install notify handler for P2P bridges */
  276. if (!pci_is_root_bus(bridge->pci_bus)) {
  277. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  278. status = acpi_remove_notify_handler(bridge->func->handle,
  279. ACPI_SYSTEM_NOTIFY,
  280. handle_hotplug_event_func);
  281. if (ACPI_FAILURE(status))
  282. err("failed to remove notify handler\n");
  283. }
  284. status = acpi_install_notify_handler(bridge->handle,
  285. ACPI_SYSTEM_NOTIFY,
  286. handle_hotplug_event_bridge,
  287. bridge);
  288. if (ACPI_FAILURE(status)) {
  289. err("failed to register interrupt notify handler\n");
  290. }
  291. }
  292. }
  293. /* find acpiphp_func from acpiphp_bridge */
  294. static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
  295. {
  296. struct acpiphp_bridge *bridge;
  297. struct acpiphp_slot *slot;
  298. struct acpiphp_func *func;
  299. list_for_each_entry(bridge, &bridge_list, list) {
  300. for (slot = bridge->slots; slot; slot = slot->next) {
  301. list_for_each_entry(func, &slot->funcs, sibling) {
  302. if (func->handle == handle)
  303. return func;
  304. }
  305. }
  306. }
  307. return NULL;
  308. }
  309. static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
  310. {
  311. acpi_handle dummy_handle;
  312. struct acpiphp_func *func;
  313. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  314. "_EJ0", &dummy_handle))) {
  315. bridge->flags |= BRIDGE_HAS_EJ0;
  316. dbg("found ejectable p2p bridge\n");
  317. /* make link between PCI bridge and PCI function */
  318. func = acpiphp_bridge_handle_to_function(bridge->handle);
  319. if (!func)
  320. return;
  321. bridge->func = func;
  322. func->bridge = bridge;
  323. }
  324. }
  325. /* allocate and initialize host bridge data structure */
  326. static void add_host_bridge(struct acpi_pci_root *root)
  327. {
  328. struct acpiphp_bridge *bridge;
  329. acpi_handle handle = root->device->handle;
  330. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  331. if (bridge == NULL)
  332. return;
  333. bridge->handle = handle;
  334. bridge->pci_bus = root->bus;
  335. init_bridge_misc(bridge);
  336. }
  337. /* allocate and initialize PCI-to-PCI bridge data structure */
  338. static void add_p2p_bridge(acpi_handle *handle)
  339. {
  340. struct acpiphp_bridge *bridge;
  341. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  342. if (bridge == NULL) {
  343. err("out of memory\n");
  344. return;
  345. }
  346. bridge->handle = handle;
  347. config_p2p_bridge_flags(bridge);
  348. bridge->pci_dev = acpi_get_pci_dev(handle);
  349. bridge->pci_bus = bridge->pci_dev->subordinate;
  350. if (!bridge->pci_bus) {
  351. err("This is not a PCI-to-PCI bridge!\n");
  352. goto err;
  353. }
  354. /*
  355. * Grab a ref to the subordinate PCI bus in case the bus is
  356. * removed via PCI core logical hotplug. The ref pins the bus
  357. * (which we access during module unload).
  358. */
  359. get_device(&bridge->pci_bus->dev);
  360. init_bridge_misc(bridge);
  361. return;
  362. err:
  363. pci_dev_put(bridge->pci_dev);
  364. kfree(bridge);
  365. return;
  366. }
  367. /* callback routine to find P2P bridges */
  368. static acpi_status
  369. find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  370. {
  371. acpi_status status;
  372. struct pci_dev *dev;
  373. dev = acpi_get_pci_dev(handle);
  374. if (!dev || !dev->subordinate)
  375. goto out;
  376. /* check if this bridge has ejectable slots */
  377. if ((detect_ejectable_slots(handle) > 0)) {
  378. dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
  379. add_p2p_bridge(handle);
  380. }
  381. /* search P2P bridges under this p2p bridge */
  382. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  383. find_p2p_bridge, NULL, NULL, NULL);
  384. if (ACPI_FAILURE(status))
  385. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  386. out:
  387. pci_dev_put(dev);
  388. return AE_OK;
  389. }
  390. /* find hot-pluggable slots, and then find P2P bridge */
  391. static int add_bridge(struct acpi_pci_root *root)
  392. {
  393. acpi_status status;
  394. unsigned long long tmp;
  395. acpi_handle dummy_handle;
  396. acpi_handle handle = root->device->handle;
  397. /* if the bridge doesn't have _STA, we assume it is always there */
  398. status = acpi_get_handle(handle, "_STA", &dummy_handle);
  399. if (ACPI_SUCCESS(status)) {
  400. status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
  401. if (ACPI_FAILURE(status)) {
  402. dbg("%s: _STA evaluation failure\n", __func__);
  403. return 0;
  404. }
  405. if ((tmp & ACPI_STA_FUNCTIONING) == 0)
  406. /* don't register this object */
  407. return 0;
  408. }
  409. /* check if this bridge has ejectable slots */
  410. if (detect_ejectable_slots(handle) > 0) {
  411. dbg("found PCI host-bus bridge with hot-pluggable slots\n");
  412. add_host_bridge(root);
  413. }
  414. /* search P2P bridges under this host bridge */
  415. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  416. find_p2p_bridge, NULL, NULL, NULL);
  417. if (ACPI_FAILURE(status))
  418. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  419. return 0;
  420. }
  421. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  422. {
  423. struct acpiphp_bridge *bridge;
  424. list_for_each_entry(bridge, &bridge_list, list)
  425. if (bridge->handle == handle)
  426. return bridge;
  427. return NULL;
  428. }
  429. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  430. {
  431. struct acpiphp_slot *slot, *next;
  432. struct acpiphp_func *func, *tmp;
  433. acpi_status status;
  434. acpi_handle handle = bridge->handle;
  435. if (!pci_is_root_bus(bridge->pci_bus)) {
  436. status = acpi_remove_notify_handler(handle,
  437. ACPI_SYSTEM_NOTIFY,
  438. handle_hotplug_event_bridge);
  439. if (ACPI_FAILURE(status))
  440. err("failed to remove notify handler\n");
  441. }
  442. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  443. status = acpi_install_notify_handler(bridge->func->handle,
  444. ACPI_SYSTEM_NOTIFY,
  445. handle_hotplug_event_func,
  446. bridge->func);
  447. if (ACPI_FAILURE(status))
  448. err("failed to install interrupt notify handler\n");
  449. }
  450. slot = bridge->slots;
  451. while (slot) {
  452. next = slot->next;
  453. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
  454. if (is_dock_device(func->handle)) {
  455. unregister_hotplug_dock_device(func->handle);
  456. unregister_dock_notifier(&func->nb);
  457. }
  458. if (!(func->flags & FUNC_HAS_DCK)) {
  459. status = acpi_remove_notify_handler(func->handle,
  460. ACPI_SYSTEM_NOTIFY,
  461. handle_hotplug_event_func);
  462. if (ACPI_FAILURE(status))
  463. err("failed to remove notify handler\n");
  464. }
  465. list_del(&func->sibling);
  466. kfree(func);
  467. }
  468. acpiphp_unregister_hotplug_slot(slot);
  469. list_del(&slot->funcs);
  470. kfree(slot);
  471. slot = next;
  472. }
  473. /*
  474. * Only P2P bridges have a pci_dev
  475. */
  476. if (bridge->pci_dev)
  477. put_device(&bridge->pci_bus->dev);
  478. pci_dev_put(bridge->pci_dev);
  479. list_del(&bridge->list);
  480. kfree(bridge);
  481. }
  482. static acpi_status
  483. cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  484. {
  485. struct acpiphp_bridge *bridge;
  486. /* cleanup p2p bridges under this P2P bridge
  487. in a depth-first manner */
  488. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  489. cleanup_p2p_bridge, NULL, NULL, NULL);
  490. bridge = acpiphp_handle_to_bridge(handle);
  491. if (bridge)
  492. cleanup_bridge(bridge);
  493. return AE_OK;
  494. }
  495. static void remove_bridge(struct acpi_pci_root *root)
  496. {
  497. struct acpiphp_bridge *bridge;
  498. acpi_handle handle = root->device->handle;
  499. /* cleanup p2p bridges under this host bridge
  500. in a depth-first manner */
  501. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  502. (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
  503. /*
  504. * On root bridges with hotplug slots directly underneath (ie,
  505. * no p2p bridge between), we call cleanup_bridge().
  506. *
  507. * The else clause cleans up root bridges that either had no
  508. * hotplug slots at all, or had a p2p bridge underneath.
  509. */
  510. bridge = acpiphp_handle_to_bridge(handle);
  511. if (bridge)
  512. cleanup_bridge(bridge);
  513. }
  514. static int power_on_slot(struct acpiphp_slot *slot)
  515. {
  516. acpi_status status;
  517. struct acpiphp_func *func;
  518. int retval = 0;
  519. /* if already enabled, just skip */
  520. if (slot->flags & SLOT_POWEREDON)
  521. goto err_exit;
  522. list_for_each_entry(func, &slot->funcs, sibling) {
  523. if (func->flags & FUNC_HAS_PS0) {
  524. dbg("%s: executing _PS0\n", __func__);
  525. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  526. if (ACPI_FAILURE(status)) {
  527. warn("%s: _PS0 failed\n", __func__);
  528. retval = -1;
  529. goto err_exit;
  530. } else
  531. break;
  532. }
  533. }
  534. /* TBD: evaluate _STA to check if the slot is enabled */
  535. slot->flags |= SLOT_POWEREDON;
  536. err_exit:
  537. return retval;
  538. }
  539. static int power_off_slot(struct acpiphp_slot *slot)
  540. {
  541. acpi_status status;
  542. struct acpiphp_func *func;
  543. int retval = 0;
  544. /* if already disabled, just skip */
  545. if ((slot->flags & SLOT_POWEREDON) == 0)
  546. goto err_exit;
  547. list_for_each_entry(func, &slot->funcs, sibling) {
  548. if (func->flags & FUNC_HAS_PS3) {
  549. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  550. if (ACPI_FAILURE(status)) {
  551. warn("%s: _PS3 failed\n", __func__);
  552. retval = -1;
  553. goto err_exit;
  554. } else
  555. break;
  556. }
  557. }
  558. /* TBD: evaluate _STA to check if the slot is disabled */
  559. slot->flags &= (~SLOT_POWEREDON);
  560. err_exit:
  561. return retval;
  562. }
  563. /**
  564. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  565. * @bus: bus to start search with
  566. */
  567. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  568. {
  569. struct list_head *tmp;
  570. unsigned char max, n;
  571. /*
  572. * pci_bus_max_busnr will return the highest
  573. * reserved busnr for all these children.
  574. * that is equivalent to the bus->subordinate
  575. * value. We don't want to use the parent's
  576. * bus->subordinate value because it could have
  577. * padding in it.
  578. */
  579. max = bus->busn_res.start;
  580. list_for_each(tmp, &bus->children) {
  581. n = pci_bus_max_busnr(pci_bus_b(tmp));
  582. if (n > max)
  583. max = n;
  584. }
  585. return max;
  586. }
  587. /**
  588. * acpiphp_bus_add - add a new bus to acpi subsystem
  589. * @func: acpiphp_func of the bridge
  590. */
  591. static int acpiphp_bus_add(struct acpiphp_func *func)
  592. {
  593. struct acpi_device *device;
  594. int ret_val;
  595. if (!acpi_bus_get_device(func->handle, &device)) {
  596. dbg("bus exists... trim\n");
  597. /* this shouldn't be in here, so remove
  598. * the bus then re-add it...
  599. */
  600. acpi_bus_trim(device);
  601. }
  602. ret_val = acpi_bus_scan(func->handle);
  603. if (!ret_val)
  604. ret_val = acpi_bus_get_device(func->handle, &device);
  605. if (ret_val)
  606. dbg("error adding bus, %x\n", -ret_val);
  607. return ret_val;
  608. }
  609. /**
  610. * acpiphp_bus_trim - trim a bus from acpi subsystem
  611. * @handle: handle to acpi namespace
  612. */
  613. static int acpiphp_bus_trim(acpi_handle handle)
  614. {
  615. struct acpi_device *device;
  616. int retval;
  617. retval = acpi_bus_get_device(handle, &device);
  618. if (retval) {
  619. dbg("acpi_device not found\n");
  620. return retval;
  621. }
  622. acpi_bus_trim(device);
  623. return 0;
  624. }
  625. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  626. {
  627. struct acpiphp_func *func;
  628. union acpi_object params[2];
  629. struct acpi_object_list arg_list;
  630. list_for_each_entry(func, &slot->funcs, sibling) {
  631. arg_list.count = 2;
  632. arg_list.pointer = params;
  633. params[0].type = ACPI_TYPE_INTEGER;
  634. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  635. params[1].type = ACPI_TYPE_INTEGER;
  636. params[1].integer.value = 1;
  637. /* _REG is optional, we don't care about if there is failure */
  638. acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
  639. }
  640. }
  641. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  642. {
  643. struct acpiphp_func *func;
  644. if (!dev->subordinate)
  645. return;
  646. /* quirk, or pcie could set it already */
  647. if (dev->is_hotplug_bridge)
  648. return;
  649. if (PCI_SLOT(dev->devfn) != slot->device)
  650. return;
  651. list_for_each_entry(func, &slot->funcs, sibling) {
  652. if (PCI_FUNC(dev->devfn) == func->function) {
  653. /* check if this bridge has ejectable slots */
  654. if ((detect_ejectable_slots(func->handle) > 0))
  655. dev->is_hotplug_bridge = 1;
  656. break;
  657. }
  658. }
  659. }
  660. /**
  661. * enable_device - enable, configure a slot
  662. * @slot: slot to be enabled
  663. *
  664. * This function should be called per *physical slot*,
  665. * not per each slot object in ACPI namespace.
  666. */
  667. static int __ref enable_device(struct acpiphp_slot *slot)
  668. {
  669. struct pci_dev *dev;
  670. struct pci_bus *bus = slot->bridge->pci_bus;
  671. struct acpiphp_func *func;
  672. int retval = 0;
  673. int num, max, pass;
  674. acpi_status status;
  675. if (slot->flags & SLOT_ENABLED)
  676. goto err_exit;
  677. list_for_each_entry(func, &slot->funcs, sibling)
  678. acpiphp_bus_add(func);
  679. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  680. if (num == 0) {
  681. /* Maybe only part of funcs are added. */
  682. dbg("No new device found\n");
  683. goto err_exit;
  684. }
  685. max = acpiphp_max_busnr(bus);
  686. for (pass = 0; pass < 2; pass++) {
  687. list_for_each_entry(dev, &bus->devices, bus_list) {
  688. if (PCI_SLOT(dev->devfn) != slot->device)
  689. continue;
  690. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  691. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  692. max = pci_scan_bridge(bus, dev, max, pass);
  693. if (pass && dev->subordinate) {
  694. check_hotplug_bridge(slot, dev);
  695. pci_bus_size_bridges(dev->subordinate);
  696. }
  697. }
  698. }
  699. }
  700. pci_bus_assign_resources(bus);
  701. acpiphp_sanitize_bus(bus);
  702. acpiphp_set_hpp_values(bus);
  703. acpiphp_set_acpi_region(slot);
  704. pci_enable_bridges(bus);
  705. list_for_each_entry(dev, &bus->devices, bus_list) {
  706. /* Assume that newly added devices are powered on already. */
  707. if (!dev->is_added)
  708. dev->current_state = PCI_D0;
  709. }
  710. pci_bus_add_devices(bus);
  711. slot->flags |= SLOT_ENABLED;
  712. list_for_each_entry(func, &slot->funcs, sibling) {
  713. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  714. func->function));
  715. if (!dev) {
  716. /* Do not set SLOT_ENABLED flag if some funcs
  717. are not added. */
  718. slot->flags &= (~SLOT_ENABLED);
  719. continue;
  720. }
  721. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
  722. dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
  723. pci_dev_put(dev);
  724. continue;
  725. }
  726. status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
  727. if (ACPI_FAILURE(status))
  728. warn("find_p2p_bridge failed (error code = 0x%x)\n",
  729. status);
  730. pci_dev_put(dev);
  731. }
  732. err_exit:
  733. return retval;
  734. }
  735. /* return first device in slot, acquiring a reference on it */
  736. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  737. {
  738. struct pci_bus *bus = slot->bridge->pci_bus;
  739. struct pci_dev *dev;
  740. struct pci_dev *ret = NULL;
  741. down_read(&pci_bus_sem);
  742. list_for_each_entry(dev, &bus->devices, bus_list)
  743. if (PCI_SLOT(dev->devfn) == slot->device) {
  744. ret = pci_dev_get(dev);
  745. break;
  746. }
  747. up_read(&pci_bus_sem);
  748. return ret;
  749. }
  750. /**
  751. * disable_device - disable a slot
  752. * @slot: ACPI PHP slot
  753. */
  754. static int disable_device(struct acpiphp_slot *slot)
  755. {
  756. struct acpiphp_func *func;
  757. struct pci_dev *pdev;
  758. struct pci_bus *bus = slot->bridge->pci_bus;
  759. /* The slot will be enabled when func 0 is added, so check
  760. func 0 before disable the slot. */
  761. pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  762. if (!pdev)
  763. goto err_exit;
  764. pci_dev_put(pdev);
  765. list_for_each_entry(func, &slot->funcs, sibling) {
  766. if (func->bridge) {
  767. /* cleanup p2p bridges under this P2P bridge */
  768. cleanup_p2p_bridge(func->bridge->handle,
  769. (u32)1, NULL, NULL);
  770. func->bridge = NULL;
  771. }
  772. }
  773. /*
  774. * enable_device() enumerates all functions in this device via
  775. * pci_scan_slot(), whether they have associated ACPI hotplug
  776. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  777. * here.
  778. */
  779. while ((pdev = dev_in_slot(slot))) {
  780. pci_stop_and_remove_bus_device(pdev);
  781. pci_dev_put(pdev);
  782. }
  783. list_for_each_entry(func, &slot->funcs, sibling) {
  784. acpiphp_bus_trim(func->handle);
  785. }
  786. slot->flags &= (~SLOT_ENABLED);
  787. err_exit:
  788. return 0;
  789. }
  790. /**
  791. * get_slot_status - get ACPI slot status
  792. * @slot: ACPI PHP slot
  793. *
  794. * If a slot has _STA for each function and if any one of them
  795. * returned non-zero status, return it.
  796. *
  797. * If a slot doesn't have _STA and if any one of its functions'
  798. * configuration space is configured, return 0x0f as a _STA.
  799. *
  800. * Otherwise return 0.
  801. */
  802. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  803. {
  804. acpi_status status;
  805. unsigned long long sta = 0;
  806. u32 dvid;
  807. struct acpiphp_func *func;
  808. list_for_each_entry(func, &slot->funcs, sibling) {
  809. if (func->flags & FUNC_HAS_STA) {
  810. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  811. if (ACPI_SUCCESS(status) && sta)
  812. break;
  813. } else {
  814. pci_bus_read_config_dword(slot->bridge->pci_bus,
  815. PCI_DEVFN(slot->device,
  816. func->function),
  817. PCI_VENDOR_ID, &dvid);
  818. if (dvid != 0xffffffff) {
  819. sta = ACPI_STA_ALL;
  820. break;
  821. }
  822. }
  823. }
  824. return (unsigned int)sta;
  825. }
  826. /**
  827. * acpiphp_eject_slot - physically eject the slot
  828. * @slot: ACPI PHP slot
  829. */
  830. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  831. {
  832. acpi_status status;
  833. struct acpiphp_func *func;
  834. struct acpi_object_list arg_list;
  835. union acpi_object arg;
  836. list_for_each_entry(func, &slot->funcs, sibling) {
  837. /* We don't want to call _EJ0 on non-existing functions. */
  838. if ((func->flags & FUNC_HAS_EJ0)) {
  839. /* _EJ0 method take one argument */
  840. arg_list.count = 1;
  841. arg_list.pointer = &arg;
  842. arg.type = ACPI_TYPE_INTEGER;
  843. arg.integer.value = 1;
  844. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  845. if (ACPI_FAILURE(status)) {
  846. warn("%s: _EJ0 failed\n", __func__);
  847. return -1;
  848. } else
  849. break;
  850. }
  851. }
  852. return 0;
  853. }
  854. /**
  855. * acpiphp_check_bridge - re-enumerate devices
  856. * @bridge: where to begin re-enumeration
  857. *
  858. * Iterate over all slots under this bridge and make sure that if a
  859. * card is present they are enabled, and if not they are disabled.
  860. */
  861. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  862. {
  863. struct acpiphp_slot *slot;
  864. int retval = 0;
  865. int enabled, disabled;
  866. enabled = disabled = 0;
  867. for (slot = bridge->slots; slot; slot = slot->next) {
  868. unsigned int status = get_slot_status(slot);
  869. if (slot->flags & SLOT_ENABLED) {
  870. if (status == ACPI_STA_ALL)
  871. continue;
  872. retval = acpiphp_disable_slot(slot);
  873. if (retval) {
  874. err("Error occurred in disabling\n");
  875. goto err_exit;
  876. } else {
  877. acpiphp_eject_slot(slot);
  878. }
  879. disabled++;
  880. } else {
  881. if (status != ACPI_STA_ALL)
  882. continue;
  883. retval = acpiphp_enable_slot(slot);
  884. if (retval) {
  885. err("Error occurred in enabling\n");
  886. goto err_exit;
  887. }
  888. enabled++;
  889. }
  890. }
  891. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  892. err_exit:
  893. return retval;
  894. }
  895. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  896. {
  897. struct pci_dev *dev;
  898. list_for_each_entry(dev, &bus->devices, bus_list)
  899. pci_configure_slot(dev);
  900. }
  901. /*
  902. * Remove devices for which we could not assign resources, call
  903. * arch specific code to fix-up the bus
  904. */
  905. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  906. {
  907. struct pci_dev *dev;
  908. int i;
  909. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  910. list_for_each_entry(dev, &bus->devices, bus_list) {
  911. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  912. struct resource *res = &dev->resource[i];
  913. if ((res->flags & type_mask) && !res->start &&
  914. res->end) {
  915. /* Could not assign a required resources
  916. * for this device, remove it */
  917. pci_stop_and_remove_bus_device(dev);
  918. break;
  919. }
  920. }
  921. }
  922. }
  923. /*
  924. * ACPI event handlers
  925. */
  926. static acpi_status
  927. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  928. {
  929. struct acpiphp_bridge *bridge;
  930. char objname[64];
  931. struct acpi_buffer buffer = { .length = sizeof(objname),
  932. .pointer = objname };
  933. bridge = acpiphp_handle_to_bridge(handle);
  934. if (bridge) {
  935. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  936. dbg("%s: re-enumerating slots under %s\n",
  937. __func__, objname);
  938. acpiphp_check_bridge(bridge);
  939. }
  940. return AE_OK ;
  941. }
  942. static void _handle_hotplug_event_bridge(struct work_struct *work)
  943. {
  944. struct acpiphp_bridge *bridge;
  945. char objname[64];
  946. struct acpi_buffer buffer = { .length = sizeof(objname),
  947. .pointer = objname };
  948. struct acpi_hp_work *hp_work;
  949. acpi_handle handle;
  950. u32 type;
  951. hp_work = container_of(work, struct acpi_hp_work, work);
  952. handle = hp_work->handle;
  953. type = hp_work->type;
  954. bridge = (struct acpiphp_bridge *)hp_work->context;
  955. acpi_scan_lock_acquire();
  956. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  957. switch (type) {
  958. case ACPI_NOTIFY_BUS_CHECK:
  959. /* bus re-enumerate */
  960. dbg("%s: Bus check notify on %s\n", __func__, objname);
  961. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  962. acpiphp_check_bridge(bridge);
  963. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  964. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  965. break;
  966. case ACPI_NOTIFY_DEVICE_CHECK:
  967. /* device check */
  968. dbg("%s: Device check notify on %s\n", __func__, objname);
  969. acpiphp_check_bridge(bridge);
  970. break;
  971. case ACPI_NOTIFY_DEVICE_WAKE:
  972. /* wake event */
  973. dbg("%s: Device wake notify on %s\n", __func__, objname);
  974. break;
  975. case ACPI_NOTIFY_EJECT_REQUEST:
  976. /* request device eject */
  977. dbg("%s: Device eject notify on %s\n", __func__, objname);
  978. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  979. struct acpiphp_slot *slot;
  980. slot = bridge->func->slot;
  981. if (!acpiphp_disable_slot(slot))
  982. acpiphp_eject_slot(slot);
  983. }
  984. break;
  985. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  986. printk(KERN_ERR "Device %s cannot be configured due"
  987. " to a frequency mismatch\n", objname);
  988. break;
  989. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  990. printk(KERN_ERR "Device %s cannot be configured due"
  991. " to a bus mode mismatch\n", objname);
  992. break;
  993. case ACPI_NOTIFY_POWER_FAULT:
  994. printk(KERN_ERR "Device %s has suffered a power fault\n",
  995. objname);
  996. break;
  997. default:
  998. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  999. break;
  1000. }
  1001. acpi_scan_lock_release();
  1002. kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
  1003. }
  1004. /**
  1005. * handle_hotplug_event_bridge - handle ACPI event on bridges
  1006. * @handle: Notify()'ed acpi_handle
  1007. * @type: Notify code
  1008. * @context: pointer to acpiphp_bridge structure
  1009. *
  1010. * Handles ACPI event notification on {host,p2p} bridges.
  1011. */
  1012. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
  1013. void *context)
  1014. {
  1015. /*
  1016. * Currently the code adds all hotplug events to the kacpid_wq
  1017. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  1018. * The proper way to fix this is to reorganize the code so that
  1019. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  1020. * For now just re-add this work to the kacpi_hotplug_wq so we
  1021. * don't deadlock on hotplug actions.
  1022. */
  1023. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
  1024. }
  1025. static void _handle_hotplug_event_func(struct work_struct *work)
  1026. {
  1027. struct acpiphp_func *func;
  1028. char objname[64];
  1029. struct acpi_buffer buffer = { .length = sizeof(objname),
  1030. .pointer = objname };
  1031. struct acpi_hp_work *hp_work;
  1032. acpi_handle handle;
  1033. u32 type;
  1034. hp_work = container_of(work, struct acpi_hp_work, work);
  1035. handle = hp_work->handle;
  1036. type = hp_work->type;
  1037. func = (struct acpiphp_func *)hp_work->context;
  1038. acpi_scan_lock_acquire();
  1039. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1040. switch (type) {
  1041. case ACPI_NOTIFY_BUS_CHECK:
  1042. /* bus re-enumerate */
  1043. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1044. acpiphp_enable_slot(func->slot);
  1045. break;
  1046. case ACPI_NOTIFY_DEVICE_CHECK:
  1047. /* device check : re-enumerate from parent bus */
  1048. dbg("%s: Device check notify on %s\n", __func__, objname);
  1049. acpiphp_check_bridge(func->slot->bridge);
  1050. break;
  1051. case ACPI_NOTIFY_DEVICE_WAKE:
  1052. /* wake event */
  1053. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1054. break;
  1055. case ACPI_NOTIFY_EJECT_REQUEST:
  1056. /* request device eject */
  1057. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1058. if (!(acpiphp_disable_slot(func->slot)))
  1059. acpiphp_eject_slot(func->slot);
  1060. break;
  1061. default:
  1062. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1063. break;
  1064. }
  1065. acpi_scan_lock_release();
  1066. kfree(hp_work); /* allocated in handle_hotplug_event_func */
  1067. }
  1068. /**
  1069. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1070. * @handle: Notify()'ed acpi_handle
  1071. * @type: Notify code
  1072. * @context: pointer to acpiphp_func structure
  1073. *
  1074. * Handles ACPI event notification on slots.
  1075. */
  1076. static void handle_hotplug_event_func(acpi_handle handle, u32 type,
  1077. void *context)
  1078. {
  1079. /*
  1080. * Currently the code adds all hotplug events to the kacpid_wq
  1081. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  1082. * The proper way to fix this is to reorganize the code so that
  1083. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  1084. * For now just re-add this work to the kacpi_hotplug_wq so we
  1085. * don't deadlock on hotplug actions.
  1086. */
  1087. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
  1088. }
  1089. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1090. .add = add_bridge,
  1091. .remove = remove_bridge,
  1092. };
  1093. /**
  1094. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1095. */
  1096. int __init acpiphp_glue_init(void)
  1097. {
  1098. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1099. return 0;
  1100. }
  1101. /**
  1102. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1103. *
  1104. * This function frees all data allocated in acpiphp_glue_init().
  1105. */
  1106. void acpiphp_glue_exit(void)
  1107. {
  1108. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1109. }
  1110. /**
  1111. * acpiphp_enable_slot - power on slot
  1112. * @slot: ACPI PHP slot
  1113. */
  1114. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1115. {
  1116. int retval;
  1117. mutex_lock(&slot->crit_sect);
  1118. /* wake up all functions */
  1119. retval = power_on_slot(slot);
  1120. if (retval)
  1121. goto err_exit;
  1122. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1123. /* configure all functions */
  1124. retval = enable_device(slot);
  1125. if (retval)
  1126. power_off_slot(slot);
  1127. } else {
  1128. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1129. power_off_slot(slot);
  1130. }
  1131. err_exit:
  1132. mutex_unlock(&slot->crit_sect);
  1133. return retval;
  1134. }
  1135. /**
  1136. * acpiphp_disable_slot - power off slot
  1137. * @slot: ACPI PHP slot
  1138. */
  1139. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1140. {
  1141. int retval = 0;
  1142. mutex_lock(&slot->crit_sect);
  1143. /* unconfigure all functions */
  1144. retval = disable_device(slot);
  1145. if (retval)
  1146. goto err_exit;
  1147. /* power off all functions */
  1148. retval = power_off_slot(slot);
  1149. if (retval)
  1150. goto err_exit;
  1151. err_exit:
  1152. mutex_unlock(&slot->crit_sect);
  1153. return retval;
  1154. }
  1155. /*
  1156. * slot enabled: 1
  1157. * slot disabled: 0
  1158. */
  1159. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1160. {
  1161. return (slot->flags & SLOT_POWEREDON);
  1162. }
  1163. /*
  1164. * latch open: 1
  1165. * latch closed: 0
  1166. */
  1167. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1168. {
  1169. unsigned int sta;
  1170. sta = get_slot_status(slot);
  1171. return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
  1172. }
  1173. /*
  1174. * adapter presence : 1
  1175. * absence : 0
  1176. */
  1177. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1178. {
  1179. unsigned int sta;
  1180. sta = get_slot_status(slot);
  1181. return (sta == 0) ? 0 : 1;
  1182. }