acpiphp_glue.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. ret_val = acpi_bus_trim(device);
  601. dbg("acpi_bus_trim return %x\n", ret_val);
  602. }
  603. ret_val = acpi_bus_scan(func->handle);
  604. if (!ret_val)
  605. ret_val = acpi_bus_get_device(func->handle, &device);
  606. if (ret_val)
  607. dbg("error adding bus, %x\n", -ret_val);
  608. return ret_val;
  609. }
  610. /**
  611. * acpiphp_bus_trim - trim a bus from acpi subsystem
  612. * @handle: handle to acpi namespace
  613. */
  614. static int acpiphp_bus_trim(acpi_handle handle)
  615. {
  616. struct acpi_device *device;
  617. int retval;
  618. retval = acpi_bus_get_device(handle, &device);
  619. if (retval) {
  620. dbg("acpi_device not found\n");
  621. return retval;
  622. }
  623. retval = acpi_bus_trim(device);
  624. if (retval)
  625. err("cannot remove from acpi list\n");
  626. return retval;
  627. }
  628. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  629. {
  630. struct acpiphp_func *func;
  631. union acpi_object params[2];
  632. struct acpi_object_list arg_list;
  633. list_for_each_entry(func, &slot->funcs, sibling) {
  634. arg_list.count = 2;
  635. arg_list.pointer = params;
  636. params[0].type = ACPI_TYPE_INTEGER;
  637. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  638. params[1].type = ACPI_TYPE_INTEGER;
  639. params[1].integer.value = 1;
  640. /* _REG is optional, we don't care about if there is failure */
  641. acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
  642. }
  643. }
  644. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  645. {
  646. struct acpiphp_func *func;
  647. if (!dev->subordinate)
  648. return;
  649. /* quirk, or pcie could set it already */
  650. if (dev->is_hotplug_bridge)
  651. return;
  652. if (PCI_SLOT(dev->devfn) != slot->device)
  653. return;
  654. list_for_each_entry(func, &slot->funcs, sibling) {
  655. if (PCI_FUNC(dev->devfn) == func->function) {
  656. /* check if this bridge has ejectable slots */
  657. if ((detect_ejectable_slots(func->handle) > 0))
  658. dev->is_hotplug_bridge = 1;
  659. break;
  660. }
  661. }
  662. }
  663. /**
  664. * enable_device - enable, configure a slot
  665. * @slot: slot to be enabled
  666. *
  667. * This function should be called per *physical slot*,
  668. * not per each slot object in ACPI namespace.
  669. */
  670. static int __ref enable_device(struct acpiphp_slot *slot)
  671. {
  672. struct pci_dev *dev;
  673. struct pci_bus *bus = slot->bridge->pci_bus;
  674. struct acpiphp_func *func;
  675. int retval = 0;
  676. int num, max, pass;
  677. acpi_status status;
  678. if (slot->flags & SLOT_ENABLED)
  679. goto err_exit;
  680. list_for_each_entry(func, &slot->funcs, sibling)
  681. acpiphp_bus_add(func);
  682. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  683. if (num == 0) {
  684. /* Maybe only part of funcs are added. */
  685. dbg("No new device found\n");
  686. goto err_exit;
  687. }
  688. max = acpiphp_max_busnr(bus);
  689. for (pass = 0; pass < 2; pass++) {
  690. list_for_each_entry(dev, &bus->devices, bus_list) {
  691. if (PCI_SLOT(dev->devfn) != slot->device)
  692. continue;
  693. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  694. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  695. max = pci_scan_bridge(bus, dev, max, pass);
  696. if (pass && dev->subordinate) {
  697. check_hotplug_bridge(slot, dev);
  698. pci_bus_size_bridges(dev->subordinate);
  699. }
  700. }
  701. }
  702. }
  703. pci_bus_assign_resources(bus);
  704. acpiphp_sanitize_bus(bus);
  705. acpiphp_set_hpp_values(bus);
  706. acpiphp_set_acpi_region(slot);
  707. pci_enable_bridges(bus);
  708. list_for_each_entry(dev, &bus->devices, bus_list) {
  709. /* Assume that newly added devices are powered on already. */
  710. if (!dev->is_added)
  711. dev->current_state = PCI_D0;
  712. }
  713. pci_bus_add_devices(bus);
  714. slot->flags |= SLOT_ENABLED;
  715. list_for_each_entry(func, &slot->funcs, sibling) {
  716. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  717. func->function));
  718. if (!dev) {
  719. /* Do not set SLOT_ENABLED flag if some funcs
  720. are not added. */
  721. slot->flags &= (~SLOT_ENABLED);
  722. continue;
  723. }
  724. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
  725. dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
  726. pci_dev_put(dev);
  727. continue;
  728. }
  729. status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
  730. if (ACPI_FAILURE(status))
  731. warn("find_p2p_bridge failed (error code = 0x%x)\n",
  732. status);
  733. pci_dev_put(dev);
  734. }
  735. err_exit:
  736. return retval;
  737. }
  738. /* return first device in slot, acquiring a reference on it */
  739. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  740. {
  741. struct pci_bus *bus = slot->bridge->pci_bus;
  742. struct pci_dev *dev;
  743. struct pci_dev *ret = NULL;
  744. down_read(&pci_bus_sem);
  745. list_for_each_entry(dev, &bus->devices, bus_list)
  746. if (PCI_SLOT(dev->devfn) == slot->device) {
  747. ret = pci_dev_get(dev);
  748. break;
  749. }
  750. up_read(&pci_bus_sem);
  751. return ret;
  752. }
  753. /**
  754. * disable_device - disable a slot
  755. * @slot: ACPI PHP slot
  756. */
  757. static int disable_device(struct acpiphp_slot *slot)
  758. {
  759. struct acpiphp_func *func;
  760. struct pci_dev *pdev;
  761. struct pci_bus *bus = slot->bridge->pci_bus;
  762. /* The slot will be enabled when func 0 is added, so check
  763. func 0 before disable the slot. */
  764. pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  765. if (!pdev)
  766. goto err_exit;
  767. pci_dev_put(pdev);
  768. list_for_each_entry(func, &slot->funcs, sibling) {
  769. if (func->bridge) {
  770. /* cleanup p2p bridges under this P2P bridge */
  771. cleanup_p2p_bridge(func->bridge->handle,
  772. (u32)1, NULL, NULL);
  773. func->bridge = NULL;
  774. }
  775. }
  776. /*
  777. * enable_device() enumerates all functions in this device via
  778. * pci_scan_slot(), whether they have associated ACPI hotplug
  779. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  780. * here.
  781. */
  782. while ((pdev = dev_in_slot(slot))) {
  783. pci_stop_and_remove_bus_device(pdev);
  784. pci_dev_put(pdev);
  785. }
  786. list_for_each_entry(func, &slot->funcs, sibling) {
  787. acpiphp_bus_trim(func->handle);
  788. }
  789. slot->flags &= (~SLOT_ENABLED);
  790. err_exit:
  791. return 0;
  792. }
  793. /**
  794. * get_slot_status - get ACPI slot status
  795. * @slot: ACPI PHP slot
  796. *
  797. * If a slot has _STA for each function and if any one of them
  798. * returned non-zero status, return it.
  799. *
  800. * If a slot doesn't have _STA and if any one of its functions'
  801. * configuration space is configured, return 0x0f as a _STA.
  802. *
  803. * Otherwise return 0.
  804. */
  805. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  806. {
  807. acpi_status status;
  808. unsigned long long sta = 0;
  809. u32 dvid;
  810. struct acpiphp_func *func;
  811. list_for_each_entry(func, &slot->funcs, sibling) {
  812. if (func->flags & FUNC_HAS_STA) {
  813. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  814. if (ACPI_SUCCESS(status) && sta)
  815. break;
  816. } else {
  817. pci_bus_read_config_dword(slot->bridge->pci_bus,
  818. PCI_DEVFN(slot->device,
  819. func->function),
  820. PCI_VENDOR_ID, &dvid);
  821. if (dvid != 0xffffffff) {
  822. sta = ACPI_STA_ALL;
  823. break;
  824. }
  825. }
  826. }
  827. return (unsigned int)sta;
  828. }
  829. /**
  830. * acpiphp_eject_slot - physically eject the slot
  831. * @slot: ACPI PHP slot
  832. */
  833. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  834. {
  835. acpi_status status;
  836. struct acpiphp_func *func;
  837. struct acpi_object_list arg_list;
  838. union acpi_object arg;
  839. list_for_each_entry(func, &slot->funcs, sibling) {
  840. /* We don't want to call _EJ0 on non-existing functions. */
  841. if ((func->flags & FUNC_HAS_EJ0)) {
  842. /* _EJ0 method take one argument */
  843. arg_list.count = 1;
  844. arg_list.pointer = &arg;
  845. arg.type = ACPI_TYPE_INTEGER;
  846. arg.integer.value = 1;
  847. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  848. if (ACPI_FAILURE(status)) {
  849. warn("%s: _EJ0 failed\n", __func__);
  850. return -1;
  851. } else
  852. break;
  853. }
  854. }
  855. return 0;
  856. }
  857. /**
  858. * acpiphp_check_bridge - re-enumerate devices
  859. * @bridge: where to begin re-enumeration
  860. *
  861. * Iterate over all slots under this bridge and make sure that if a
  862. * card is present they are enabled, and if not they are disabled.
  863. */
  864. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  865. {
  866. struct acpiphp_slot *slot;
  867. int retval = 0;
  868. int enabled, disabled;
  869. enabled = disabled = 0;
  870. for (slot = bridge->slots; slot; slot = slot->next) {
  871. unsigned int status = get_slot_status(slot);
  872. if (slot->flags & SLOT_ENABLED) {
  873. if (status == ACPI_STA_ALL)
  874. continue;
  875. retval = acpiphp_disable_slot(slot);
  876. if (retval) {
  877. err("Error occurred in disabling\n");
  878. goto err_exit;
  879. } else {
  880. acpiphp_eject_slot(slot);
  881. }
  882. disabled++;
  883. } else {
  884. if (status != ACPI_STA_ALL)
  885. continue;
  886. retval = acpiphp_enable_slot(slot);
  887. if (retval) {
  888. err("Error occurred in enabling\n");
  889. goto err_exit;
  890. }
  891. enabled++;
  892. }
  893. }
  894. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  895. err_exit:
  896. return retval;
  897. }
  898. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  899. {
  900. struct pci_dev *dev;
  901. list_for_each_entry(dev, &bus->devices, bus_list)
  902. pci_configure_slot(dev);
  903. }
  904. /*
  905. * Remove devices for which we could not assign resources, call
  906. * arch specific code to fix-up the bus
  907. */
  908. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  909. {
  910. struct pci_dev *dev;
  911. int i;
  912. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  913. list_for_each_entry(dev, &bus->devices, bus_list) {
  914. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  915. struct resource *res = &dev->resource[i];
  916. if ((res->flags & type_mask) && !res->start &&
  917. res->end) {
  918. /* Could not assign a required resources
  919. * for this device, remove it */
  920. pci_stop_and_remove_bus_device(dev);
  921. break;
  922. }
  923. }
  924. }
  925. }
  926. /*
  927. * ACPI event handlers
  928. */
  929. static acpi_status
  930. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  931. {
  932. struct acpiphp_bridge *bridge;
  933. char objname[64];
  934. struct acpi_buffer buffer = { .length = sizeof(objname),
  935. .pointer = objname };
  936. bridge = acpiphp_handle_to_bridge(handle);
  937. if (bridge) {
  938. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  939. dbg("%s: re-enumerating slots under %s\n",
  940. __func__, objname);
  941. acpiphp_check_bridge(bridge);
  942. }
  943. return AE_OK ;
  944. }
  945. static void _handle_hotplug_event_bridge(struct work_struct *work)
  946. {
  947. struct acpiphp_bridge *bridge;
  948. char objname[64];
  949. struct acpi_buffer buffer = { .length = sizeof(objname),
  950. .pointer = objname };
  951. struct acpi_hp_work *hp_work;
  952. acpi_handle handle;
  953. u32 type;
  954. hp_work = container_of(work, struct acpi_hp_work, work);
  955. handle = hp_work->handle;
  956. type = hp_work->type;
  957. bridge = (struct acpiphp_bridge *)hp_work->context;
  958. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  959. switch (type) {
  960. case ACPI_NOTIFY_BUS_CHECK:
  961. /* bus re-enumerate */
  962. dbg("%s: Bus check notify on %s\n", __func__, objname);
  963. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  964. acpiphp_check_bridge(bridge);
  965. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  966. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  967. break;
  968. case ACPI_NOTIFY_DEVICE_CHECK:
  969. /* device check */
  970. dbg("%s: Device check notify on %s\n", __func__, objname);
  971. acpiphp_check_bridge(bridge);
  972. break;
  973. case ACPI_NOTIFY_DEVICE_WAKE:
  974. /* wake event */
  975. dbg("%s: Device wake notify on %s\n", __func__, objname);
  976. break;
  977. case ACPI_NOTIFY_EJECT_REQUEST:
  978. /* request device eject */
  979. dbg("%s: Device eject notify on %s\n", __func__, objname);
  980. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  981. struct acpiphp_slot *slot;
  982. slot = bridge->func->slot;
  983. if (!acpiphp_disable_slot(slot))
  984. acpiphp_eject_slot(slot);
  985. }
  986. break;
  987. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  988. printk(KERN_ERR "Device %s cannot be configured due"
  989. " to a frequency mismatch\n", objname);
  990. break;
  991. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  992. printk(KERN_ERR "Device %s cannot be configured due"
  993. " to a bus mode mismatch\n", objname);
  994. break;
  995. case ACPI_NOTIFY_POWER_FAULT:
  996. printk(KERN_ERR "Device %s has suffered a power fault\n",
  997. objname);
  998. break;
  999. default:
  1000. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1001. break;
  1002. }
  1003. kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
  1004. }
  1005. /**
  1006. * handle_hotplug_event_bridge - handle ACPI event on bridges
  1007. * @handle: Notify()'ed acpi_handle
  1008. * @type: Notify code
  1009. * @context: pointer to acpiphp_bridge structure
  1010. *
  1011. * Handles ACPI event notification on {host,p2p} bridges.
  1012. */
  1013. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
  1014. void *context)
  1015. {
  1016. /*
  1017. * Currently the code adds all hotplug events to the kacpid_wq
  1018. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  1019. * The proper way to fix this is to reorganize the code so that
  1020. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  1021. * For now just re-add this work to the kacpi_hotplug_wq so we
  1022. * don't deadlock on hotplug actions.
  1023. */
  1024. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
  1025. }
  1026. static void _handle_hotplug_event_func(struct work_struct *work)
  1027. {
  1028. struct acpiphp_func *func;
  1029. char objname[64];
  1030. struct acpi_buffer buffer = { .length = sizeof(objname),
  1031. .pointer = objname };
  1032. struct acpi_hp_work *hp_work;
  1033. acpi_handle handle;
  1034. u32 type;
  1035. hp_work = container_of(work, struct acpi_hp_work, work);
  1036. handle = hp_work->handle;
  1037. type = hp_work->type;
  1038. func = (struct acpiphp_func *)hp_work->context;
  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. kfree(hp_work); /* allocated in handle_hotplug_event_func */
  1066. }
  1067. /**
  1068. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1069. * @handle: Notify()'ed acpi_handle
  1070. * @type: Notify code
  1071. * @context: pointer to acpiphp_func structure
  1072. *
  1073. * Handles ACPI event notification on slots.
  1074. */
  1075. static void handle_hotplug_event_func(acpi_handle handle, u32 type,
  1076. void *context)
  1077. {
  1078. /*
  1079. * Currently the code adds all hotplug events to the kacpid_wq
  1080. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  1081. * The proper way to fix this is to reorganize the code so that
  1082. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  1083. * For now just re-add this work to the kacpi_hotplug_wq so we
  1084. * don't deadlock on hotplug actions.
  1085. */
  1086. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
  1087. }
  1088. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1089. .add = add_bridge,
  1090. .remove = remove_bridge,
  1091. };
  1092. /**
  1093. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1094. */
  1095. int __init acpiphp_glue_init(void)
  1096. {
  1097. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1098. return 0;
  1099. }
  1100. /**
  1101. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1102. *
  1103. * This function frees all data allocated in acpiphp_glue_init().
  1104. */
  1105. void acpiphp_glue_exit(void)
  1106. {
  1107. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1108. }
  1109. /**
  1110. * acpiphp_enable_slot - power on slot
  1111. * @slot: ACPI PHP slot
  1112. */
  1113. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1114. {
  1115. int retval;
  1116. mutex_lock(&slot->crit_sect);
  1117. /* wake up all functions */
  1118. retval = power_on_slot(slot);
  1119. if (retval)
  1120. goto err_exit;
  1121. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1122. /* configure all functions */
  1123. retval = enable_device(slot);
  1124. if (retval)
  1125. power_off_slot(slot);
  1126. } else {
  1127. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1128. power_off_slot(slot);
  1129. }
  1130. err_exit:
  1131. mutex_unlock(&slot->crit_sect);
  1132. return retval;
  1133. }
  1134. /**
  1135. * acpiphp_disable_slot - power off slot
  1136. * @slot: ACPI PHP slot
  1137. */
  1138. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1139. {
  1140. int retval = 0;
  1141. mutex_lock(&slot->crit_sect);
  1142. /* unconfigure all functions */
  1143. retval = disable_device(slot);
  1144. if (retval)
  1145. goto err_exit;
  1146. /* power off all functions */
  1147. retval = power_off_slot(slot);
  1148. if (retval)
  1149. goto err_exit;
  1150. err_exit:
  1151. mutex_unlock(&slot->crit_sect);
  1152. return retval;
  1153. }
  1154. /*
  1155. * slot enabled: 1
  1156. * slot disabled: 0
  1157. */
  1158. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1159. {
  1160. return (slot->flags & SLOT_POWEREDON);
  1161. }
  1162. /*
  1163. * latch open: 1
  1164. * latch closed: 0
  1165. */
  1166. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1167. {
  1168. unsigned int sta;
  1169. sta = get_slot_status(slot);
  1170. return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
  1171. }
  1172. /*
  1173. * adapter presence : 1
  1174. * absence : 0
  1175. */
  1176. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1177. {
  1178. unsigned int sta;
  1179. sta = get_slot_status(slot);
  1180. return (sta == 0) ? 0 : 1;
  1181. }