acpiphp_glue.c 35 KB

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