acpiphp_glue.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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. list_for_each_entry(func, &slot->funcs, sibling) {
  760. if (func->bridge) {
  761. /* cleanup p2p bridges under this P2P bridge */
  762. cleanup_p2p_bridge(func->bridge->handle,
  763. (u32)1, NULL, NULL);
  764. func->bridge = NULL;
  765. }
  766. }
  767. /*
  768. * enable_device() enumerates all functions in this device via
  769. * pci_scan_slot(), whether they have associated ACPI hotplug
  770. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  771. * here.
  772. */
  773. while ((pdev = dev_in_slot(slot))) {
  774. pci_stop_and_remove_bus_device(pdev);
  775. pci_dev_put(pdev);
  776. }
  777. list_for_each_entry(func, &slot->funcs, sibling) {
  778. acpiphp_bus_trim(func->handle);
  779. }
  780. slot->flags &= (~SLOT_ENABLED);
  781. err_exit:
  782. return 0;
  783. }
  784. /**
  785. * get_slot_status - get ACPI slot status
  786. * @slot: ACPI PHP slot
  787. *
  788. * If a slot has _STA for each function and if any one of them
  789. * returned non-zero status, return it.
  790. *
  791. * If a slot doesn't have _STA and if any one of its functions'
  792. * configuration space is configured, return 0x0f as a _STA.
  793. *
  794. * Otherwise return 0.
  795. */
  796. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  797. {
  798. acpi_status status;
  799. unsigned long long sta = 0;
  800. u32 dvid;
  801. struct acpiphp_func *func;
  802. list_for_each_entry(func, &slot->funcs, sibling) {
  803. if (func->flags & FUNC_HAS_STA) {
  804. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  805. if (ACPI_SUCCESS(status) && sta)
  806. break;
  807. } else {
  808. pci_bus_read_config_dword(slot->bridge->pci_bus,
  809. PCI_DEVFN(slot->device,
  810. func->function),
  811. PCI_VENDOR_ID, &dvid);
  812. if (dvid != 0xffffffff) {
  813. sta = ACPI_STA_ALL;
  814. break;
  815. }
  816. }
  817. }
  818. return (unsigned int)sta;
  819. }
  820. /**
  821. * acpiphp_eject_slot - physically eject the slot
  822. * @slot: ACPI PHP slot
  823. */
  824. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  825. {
  826. acpi_status status;
  827. struct acpiphp_func *func;
  828. struct acpi_object_list arg_list;
  829. union acpi_object arg;
  830. list_for_each_entry(func, &slot->funcs, sibling) {
  831. /* We don't want to call _EJ0 on non-existing functions. */
  832. if ((func->flags & FUNC_HAS_EJ0)) {
  833. /* _EJ0 method take one argument */
  834. arg_list.count = 1;
  835. arg_list.pointer = &arg;
  836. arg.type = ACPI_TYPE_INTEGER;
  837. arg.integer.value = 1;
  838. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  839. if (ACPI_FAILURE(status)) {
  840. warn("%s: _EJ0 failed\n", __func__);
  841. return -1;
  842. } else
  843. break;
  844. }
  845. }
  846. return 0;
  847. }
  848. /**
  849. * acpiphp_check_bridge - re-enumerate devices
  850. * @bridge: where to begin re-enumeration
  851. *
  852. * Iterate over all slots under this bridge and make sure that if a
  853. * card is present they are enabled, and if not they are disabled.
  854. */
  855. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  856. {
  857. struct acpiphp_slot *slot;
  858. int retval = 0;
  859. int enabled, disabled;
  860. enabled = disabled = 0;
  861. for (slot = bridge->slots; slot; slot = slot->next) {
  862. unsigned int status = get_slot_status(slot);
  863. if (slot->flags & SLOT_ENABLED) {
  864. if (status == ACPI_STA_ALL)
  865. continue;
  866. retval = acpiphp_disable_slot(slot);
  867. if (retval) {
  868. err("Error occurred in disabling\n");
  869. goto err_exit;
  870. } else {
  871. acpiphp_eject_slot(slot);
  872. }
  873. disabled++;
  874. } else {
  875. if (status != ACPI_STA_ALL)
  876. continue;
  877. retval = acpiphp_enable_slot(slot);
  878. if (retval) {
  879. err("Error occurred in enabling\n");
  880. goto err_exit;
  881. }
  882. enabled++;
  883. }
  884. }
  885. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  886. err_exit:
  887. return retval;
  888. }
  889. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  890. {
  891. struct pci_dev *dev;
  892. list_for_each_entry(dev, &bus->devices, bus_list)
  893. pci_configure_slot(dev);
  894. }
  895. /*
  896. * Remove devices for which we could not assign resources, call
  897. * arch specific code to fix-up the bus
  898. */
  899. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  900. {
  901. struct pci_dev *dev, *tmp;
  902. int i;
  903. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  904. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  905. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  906. struct resource *res = &dev->resource[i];
  907. if ((res->flags & type_mask) && !res->start &&
  908. res->end) {
  909. /* Could not assign a required resources
  910. * for this device, remove it */
  911. pci_stop_and_remove_bus_device(dev);
  912. break;
  913. }
  914. }
  915. }
  916. }
  917. /*
  918. * ACPI event handlers
  919. */
  920. static acpi_status
  921. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  922. {
  923. struct acpiphp_bridge *bridge;
  924. char objname[64];
  925. struct acpi_buffer buffer = { .length = sizeof(objname),
  926. .pointer = objname };
  927. bridge = acpiphp_handle_to_bridge(handle);
  928. if (bridge) {
  929. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  930. dbg("%s: re-enumerating slots under %s\n",
  931. __func__, objname);
  932. acpiphp_check_bridge(bridge);
  933. }
  934. return AE_OK ;
  935. }
  936. static void _handle_hotplug_event_bridge(struct work_struct *work)
  937. {
  938. struct acpiphp_bridge *bridge;
  939. char objname[64];
  940. struct acpi_buffer buffer = { .length = sizeof(objname),
  941. .pointer = objname };
  942. struct acpi_hp_work *hp_work;
  943. acpi_handle handle;
  944. u32 type;
  945. hp_work = container_of(work, struct acpi_hp_work, work);
  946. handle = hp_work->handle;
  947. type = hp_work->type;
  948. bridge = (struct acpiphp_bridge *)hp_work->context;
  949. acpi_scan_lock_acquire();
  950. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  951. switch (type) {
  952. case ACPI_NOTIFY_BUS_CHECK:
  953. /* bus re-enumerate */
  954. dbg("%s: Bus check notify on %s\n", __func__, objname);
  955. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  956. acpiphp_check_bridge(bridge);
  957. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  958. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  959. break;
  960. case ACPI_NOTIFY_DEVICE_CHECK:
  961. /* device check */
  962. dbg("%s: Device check notify on %s\n", __func__, objname);
  963. acpiphp_check_bridge(bridge);
  964. break;
  965. case ACPI_NOTIFY_DEVICE_WAKE:
  966. /* wake event */
  967. dbg("%s: Device wake notify on %s\n", __func__, objname);
  968. break;
  969. case ACPI_NOTIFY_EJECT_REQUEST:
  970. /* request device eject */
  971. dbg("%s: Device eject notify on %s\n", __func__, objname);
  972. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  973. struct acpiphp_slot *slot;
  974. slot = bridge->func->slot;
  975. if (!acpiphp_disable_slot(slot))
  976. acpiphp_eject_slot(slot);
  977. }
  978. break;
  979. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  980. printk(KERN_ERR "Device %s cannot be configured due"
  981. " to a frequency mismatch\n", objname);
  982. break;
  983. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  984. printk(KERN_ERR "Device %s cannot be configured due"
  985. " to a bus mode mismatch\n", objname);
  986. break;
  987. case ACPI_NOTIFY_POWER_FAULT:
  988. printk(KERN_ERR "Device %s has suffered a power fault\n",
  989. objname);
  990. break;
  991. default:
  992. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  993. break;
  994. }
  995. acpi_scan_lock_release();
  996. kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
  997. }
  998. /**
  999. * handle_hotplug_event_bridge - handle ACPI event on bridges
  1000. * @handle: Notify()'ed acpi_handle
  1001. * @type: Notify code
  1002. * @context: pointer to acpiphp_bridge structure
  1003. *
  1004. * Handles ACPI event notification on {host,p2p} bridges.
  1005. */
  1006. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
  1007. void *context)
  1008. {
  1009. /*
  1010. * Currently the code adds all hotplug events to the kacpid_wq
  1011. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  1012. * The proper way to fix this is to reorganize the code so that
  1013. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  1014. * For now just re-add this work to the kacpi_hotplug_wq so we
  1015. * don't deadlock on hotplug actions.
  1016. */
  1017. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
  1018. }
  1019. static void _handle_hotplug_event_func(struct work_struct *work)
  1020. {
  1021. struct acpiphp_func *func;
  1022. char objname[64];
  1023. struct acpi_buffer buffer = { .length = sizeof(objname),
  1024. .pointer = objname };
  1025. struct acpi_hp_work *hp_work;
  1026. acpi_handle handle;
  1027. u32 type;
  1028. hp_work = container_of(work, struct acpi_hp_work, work);
  1029. handle = hp_work->handle;
  1030. type = hp_work->type;
  1031. func = (struct acpiphp_func *)hp_work->context;
  1032. acpi_scan_lock_acquire();
  1033. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1034. switch (type) {
  1035. case ACPI_NOTIFY_BUS_CHECK:
  1036. /* bus re-enumerate */
  1037. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1038. acpiphp_enable_slot(func->slot);
  1039. break;
  1040. case ACPI_NOTIFY_DEVICE_CHECK:
  1041. /* device check : re-enumerate from parent bus */
  1042. dbg("%s: Device check notify on %s\n", __func__, objname);
  1043. acpiphp_check_bridge(func->slot->bridge);
  1044. break;
  1045. case ACPI_NOTIFY_DEVICE_WAKE:
  1046. /* wake event */
  1047. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1048. break;
  1049. case ACPI_NOTIFY_EJECT_REQUEST:
  1050. /* request device eject */
  1051. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1052. if (!(acpiphp_disable_slot(func->slot)))
  1053. acpiphp_eject_slot(func->slot);
  1054. break;
  1055. default:
  1056. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1057. break;
  1058. }
  1059. acpi_scan_lock_release();
  1060. kfree(hp_work); /* allocated in handle_hotplug_event_func */
  1061. }
  1062. /**
  1063. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1064. * @handle: Notify()'ed acpi_handle
  1065. * @type: Notify code
  1066. * @context: pointer to acpiphp_func structure
  1067. *
  1068. * Handles ACPI event notification on slots.
  1069. */
  1070. static void handle_hotplug_event_func(acpi_handle handle, u32 type,
  1071. void *context)
  1072. {
  1073. /*
  1074. * Currently the code adds all hotplug events to the kacpid_wq
  1075. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  1076. * The proper way to fix this is to reorganize the code so that
  1077. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  1078. * For now just re-add this work to the kacpi_hotplug_wq so we
  1079. * don't deadlock on hotplug actions.
  1080. */
  1081. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
  1082. }
  1083. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1084. .add = add_bridge,
  1085. .remove = remove_bridge,
  1086. };
  1087. /**
  1088. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1089. */
  1090. int __init acpiphp_glue_init(void)
  1091. {
  1092. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1093. return 0;
  1094. }
  1095. /**
  1096. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1097. *
  1098. * This function frees all data allocated in acpiphp_glue_init().
  1099. */
  1100. void acpiphp_glue_exit(void)
  1101. {
  1102. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1103. }
  1104. /**
  1105. * acpiphp_enable_slot - power on slot
  1106. * @slot: ACPI PHP slot
  1107. */
  1108. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1109. {
  1110. int retval;
  1111. mutex_lock(&slot->crit_sect);
  1112. /* wake up all functions */
  1113. retval = power_on_slot(slot);
  1114. if (retval)
  1115. goto err_exit;
  1116. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1117. /* configure all functions */
  1118. retval = enable_device(slot);
  1119. if (retval)
  1120. power_off_slot(slot);
  1121. } else {
  1122. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1123. power_off_slot(slot);
  1124. }
  1125. err_exit:
  1126. mutex_unlock(&slot->crit_sect);
  1127. return retval;
  1128. }
  1129. /**
  1130. * acpiphp_disable_slot - power off slot
  1131. * @slot: ACPI PHP slot
  1132. */
  1133. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1134. {
  1135. int retval = 0;
  1136. mutex_lock(&slot->crit_sect);
  1137. /* unconfigure all functions */
  1138. retval = disable_device(slot);
  1139. if (retval)
  1140. goto err_exit;
  1141. /* power off all functions */
  1142. retval = power_off_slot(slot);
  1143. if (retval)
  1144. goto err_exit;
  1145. err_exit:
  1146. mutex_unlock(&slot->crit_sect);
  1147. return retval;
  1148. }
  1149. /*
  1150. * slot enabled: 1
  1151. * slot disabled: 0
  1152. */
  1153. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1154. {
  1155. return (slot->flags & SLOT_POWEREDON);
  1156. }
  1157. /*
  1158. * latch open: 1
  1159. * latch closed: 0
  1160. */
  1161. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1162. {
  1163. unsigned int sta;
  1164. sta = get_slot_status(slot);
  1165. return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
  1166. }
  1167. /*
  1168. * adapter presence : 1
  1169. * absence : 0
  1170. */
  1171. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1172. {
  1173. unsigned int sta;
  1174. sta = get_slot_status(slot);
  1175. return (sta == 0) ? 0 : 1;
  1176. }