acpiphp_glue.c 38 KB

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