acpiphp_glue.c 35 KB

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