acpiphp_glue.c 38 KB

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