acpiphp_glue.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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. if (bridge->type != BRIDGE_TYPE_HOST) {
  449. status = acpi_remove_notify_handler(handle,
  450. ACPI_SYSTEM_NOTIFY,
  451. handle_hotplug_event_bridge);
  452. if (ACPI_FAILURE(status))
  453. err("failed to remove notify handler\n");
  454. }
  455. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  456. ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
  457. status = acpi_install_notify_handler(bridge->func->handle,
  458. ACPI_SYSTEM_NOTIFY,
  459. handle_hotplug_event_func,
  460. bridge->func);
  461. if (ACPI_FAILURE(status))
  462. err("failed to install interrupt notify handler\n");
  463. }
  464. slot = bridge->slots;
  465. while (slot) {
  466. next = slot->next;
  467. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
  468. if (is_dock_device(func->handle)) {
  469. unregister_hotplug_dock_device(func->handle);
  470. unregister_dock_notifier(&func->nb);
  471. }
  472. if (!(func->flags & FUNC_HAS_DCK)) {
  473. status = acpi_remove_notify_handler(func->handle,
  474. ACPI_SYSTEM_NOTIFY,
  475. handle_hotplug_event_func);
  476. if (ACPI_FAILURE(status))
  477. err("failed to remove notify handler\n");
  478. }
  479. list_del(&func->sibling);
  480. kfree(func);
  481. }
  482. acpiphp_unregister_hotplug_slot(slot);
  483. list_del(&slot->funcs);
  484. kfree(slot);
  485. slot = next;
  486. }
  487. /*
  488. * Only P2P bridges have a pci_dev
  489. */
  490. if (bridge->pci_dev)
  491. put_device(&bridge->pci_bus->dev);
  492. pci_dev_put(bridge->pci_dev);
  493. list_del(&bridge->list);
  494. kfree(bridge);
  495. }
  496. static acpi_status
  497. cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  498. {
  499. struct acpiphp_bridge *bridge;
  500. /* cleanup p2p bridges under this P2P bridge
  501. in a depth-first manner */
  502. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  503. cleanup_p2p_bridge, NULL, NULL, NULL);
  504. bridge = acpiphp_handle_to_bridge(handle);
  505. if (bridge)
  506. cleanup_bridge(bridge);
  507. return AE_OK;
  508. }
  509. static void remove_bridge(struct acpi_pci_root *root)
  510. {
  511. struct acpiphp_bridge *bridge;
  512. acpi_handle handle = root->device->handle;
  513. /* cleanup p2p bridges under this host bridge
  514. in a depth-first manner */
  515. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  516. (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
  517. /*
  518. * On root bridges with hotplug slots directly underneath (ie,
  519. * no p2p bridge between), we call cleanup_bridge().
  520. *
  521. * The else clause cleans up root bridges that either had no
  522. * hotplug slots at all, or had a p2p bridge underneath.
  523. */
  524. bridge = acpiphp_handle_to_bridge(handle);
  525. if (bridge)
  526. cleanup_bridge(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. struct acpi_device *device;
  608. int ret_val;
  609. if (!acpi_bus_get_device(func->handle, &device)) {
  610. dbg("bus exists... trim\n");
  611. /* this shouldn't be in here, so remove
  612. * the bus then re-add it...
  613. */
  614. ret_val = acpi_bus_trim(device);
  615. dbg("acpi_bus_trim return %x\n", ret_val);
  616. }
  617. ret_val = acpi_bus_scan(func->handle);
  618. if (!ret_val)
  619. ret_val = acpi_bus_get_device(func->handle, &device);
  620. if (ret_val)
  621. dbg("error adding bus, %x\n", -ret_val);
  622. return ret_val;
  623. }
  624. /**
  625. * acpiphp_bus_trim - trim a bus from acpi subsystem
  626. * @handle: handle to acpi namespace
  627. */
  628. static int acpiphp_bus_trim(acpi_handle handle)
  629. {
  630. struct acpi_device *device;
  631. int retval;
  632. retval = acpi_bus_get_device(handle, &device);
  633. if (retval) {
  634. dbg("acpi_device not found\n");
  635. return retval;
  636. }
  637. retval = acpi_bus_trim(device);
  638. if (retval)
  639. err("cannot remove from acpi list\n");
  640. return retval;
  641. }
  642. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  643. {
  644. struct acpiphp_func *func;
  645. union acpi_object params[2];
  646. struct acpi_object_list arg_list;
  647. list_for_each_entry(func, &slot->funcs, sibling) {
  648. arg_list.count = 2;
  649. arg_list.pointer = params;
  650. params[0].type = ACPI_TYPE_INTEGER;
  651. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  652. params[1].type = ACPI_TYPE_INTEGER;
  653. params[1].integer.value = 1;
  654. /* _REG is optional, we don't care about if there is failure */
  655. acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
  656. }
  657. }
  658. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  659. {
  660. struct acpiphp_func *func;
  661. if (!dev->subordinate)
  662. return;
  663. /* quirk, or pcie could set it already */
  664. if (dev->is_hotplug_bridge)
  665. return;
  666. if (PCI_SLOT(dev->devfn) != slot->device)
  667. return;
  668. list_for_each_entry(func, &slot->funcs, sibling) {
  669. if (PCI_FUNC(dev->devfn) == func->function) {
  670. /* check if this bridge has ejectable slots */
  671. if ((detect_ejectable_slots(func->handle) > 0))
  672. dev->is_hotplug_bridge = 1;
  673. break;
  674. }
  675. }
  676. }
  677. /**
  678. * enable_device - enable, configure a slot
  679. * @slot: slot to be enabled
  680. *
  681. * This function should be called per *physical slot*,
  682. * not per each slot object in ACPI namespace.
  683. */
  684. static int __ref enable_device(struct acpiphp_slot *slot)
  685. {
  686. struct pci_dev *dev;
  687. struct pci_bus *bus = slot->bridge->pci_bus;
  688. struct acpiphp_func *func;
  689. int retval = 0;
  690. int num, max, pass;
  691. acpi_status status;
  692. if (slot->flags & SLOT_ENABLED)
  693. goto err_exit;
  694. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  695. if (num == 0) {
  696. /* Maybe only part of funcs are added. */
  697. dbg("No new device found\n");
  698. goto err_exit;
  699. }
  700. max = acpiphp_max_busnr(bus);
  701. for (pass = 0; pass < 2; pass++) {
  702. list_for_each_entry(dev, &bus->devices, bus_list) {
  703. if (PCI_SLOT(dev->devfn) != slot->device)
  704. continue;
  705. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  706. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  707. max = pci_scan_bridge(bus, dev, max, pass);
  708. if (pass && dev->subordinate) {
  709. check_hotplug_bridge(slot, dev);
  710. pci_bus_size_bridges(dev->subordinate);
  711. }
  712. }
  713. }
  714. }
  715. list_for_each_entry(func, &slot->funcs, sibling)
  716. acpiphp_bus_add(func);
  717. pci_bus_assign_resources(bus);
  718. acpiphp_sanitize_bus(bus);
  719. acpiphp_set_hpp_values(bus);
  720. acpiphp_set_acpi_region(slot);
  721. pci_enable_bridges(bus);
  722. list_for_each_entry(dev, &bus->devices, bus_list) {
  723. /* Assume that newly added devices are powered on already. */
  724. if (!dev->is_added)
  725. dev->current_state = PCI_D0;
  726. }
  727. pci_bus_add_devices(bus);
  728. slot->flags |= SLOT_ENABLED;
  729. list_for_each_entry(func, &slot->funcs, sibling) {
  730. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  731. func->function));
  732. if (!dev) {
  733. /* Do not set SLOT_ENABLED flag if some funcs
  734. are not added. */
  735. slot->flags &= (~SLOT_ENABLED);
  736. continue;
  737. }
  738. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
  739. dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
  740. pci_dev_put(dev);
  741. continue;
  742. }
  743. status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
  744. if (ACPI_FAILURE(status))
  745. warn("find_p2p_bridge failed (error code = 0x%x)\n",
  746. status);
  747. pci_dev_put(dev);
  748. }
  749. err_exit:
  750. return retval;
  751. }
  752. /* return first device in slot, acquiring a reference on it */
  753. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  754. {
  755. struct pci_bus *bus = slot->bridge->pci_bus;
  756. struct pci_dev *dev;
  757. struct pci_dev *ret = NULL;
  758. down_read(&pci_bus_sem);
  759. list_for_each_entry(dev, &bus->devices, bus_list)
  760. if (PCI_SLOT(dev->devfn) == slot->device) {
  761. ret = pci_dev_get(dev);
  762. break;
  763. }
  764. up_read(&pci_bus_sem);
  765. return ret;
  766. }
  767. /**
  768. * disable_device - disable a slot
  769. * @slot: ACPI PHP slot
  770. */
  771. static int disable_device(struct acpiphp_slot *slot)
  772. {
  773. struct acpiphp_func *func;
  774. struct pci_dev *pdev;
  775. struct pci_bus *bus = slot->bridge->pci_bus;
  776. /* The slot will be enabled when func 0 is added, so check
  777. func 0 before disable the slot. */
  778. pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  779. if (!pdev)
  780. goto err_exit;
  781. pci_dev_put(pdev);
  782. list_for_each_entry(func, &slot->funcs, sibling) {
  783. if (func->bridge) {
  784. /* cleanup p2p bridges under this P2P bridge */
  785. cleanup_p2p_bridge(func->bridge->handle,
  786. (u32)1, NULL, NULL);
  787. func->bridge = NULL;
  788. }
  789. }
  790. /*
  791. * enable_device() enumerates all functions in this device via
  792. * pci_scan_slot(), whether they have associated ACPI hotplug
  793. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  794. * here.
  795. */
  796. while ((pdev = dev_in_slot(slot))) {
  797. pci_stop_and_remove_bus_device(pdev);
  798. pci_dev_put(pdev);
  799. }
  800. list_for_each_entry(func, &slot->funcs, sibling) {
  801. acpiphp_bus_trim(func->handle);
  802. }
  803. slot->flags &= (~SLOT_ENABLED);
  804. err_exit:
  805. return 0;
  806. }
  807. /**
  808. * get_slot_status - get ACPI slot status
  809. * @slot: ACPI PHP slot
  810. *
  811. * If a slot has _STA for each function and if any one of them
  812. * returned non-zero status, return it.
  813. *
  814. * If a slot doesn't have _STA and if any one of its functions'
  815. * configuration space is configured, return 0x0f as a _STA.
  816. *
  817. * Otherwise return 0.
  818. */
  819. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  820. {
  821. acpi_status status;
  822. unsigned long long sta = 0;
  823. u32 dvid;
  824. struct acpiphp_func *func;
  825. list_for_each_entry(func, &slot->funcs, sibling) {
  826. if (func->flags & FUNC_HAS_STA) {
  827. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  828. if (ACPI_SUCCESS(status) && sta)
  829. break;
  830. } else {
  831. pci_bus_read_config_dword(slot->bridge->pci_bus,
  832. PCI_DEVFN(slot->device,
  833. func->function),
  834. PCI_VENDOR_ID, &dvid);
  835. if (dvid != 0xffffffff) {
  836. sta = ACPI_STA_ALL;
  837. break;
  838. }
  839. }
  840. }
  841. return (unsigned int)sta;
  842. }
  843. /**
  844. * acpiphp_eject_slot - physically eject the slot
  845. * @slot: ACPI PHP slot
  846. */
  847. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  848. {
  849. acpi_status status;
  850. struct acpiphp_func *func;
  851. struct acpi_object_list arg_list;
  852. union acpi_object arg;
  853. list_for_each_entry(func, &slot->funcs, sibling) {
  854. /* We don't want to call _EJ0 on non-existing functions. */
  855. if ((func->flags & FUNC_HAS_EJ0)) {
  856. /* _EJ0 method take one argument */
  857. arg_list.count = 1;
  858. arg_list.pointer = &arg;
  859. arg.type = ACPI_TYPE_INTEGER;
  860. arg.integer.value = 1;
  861. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  862. if (ACPI_FAILURE(status)) {
  863. warn("%s: _EJ0 failed\n", __func__);
  864. return -1;
  865. } else
  866. break;
  867. }
  868. }
  869. return 0;
  870. }
  871. /**
  872. * acpiphp_check_bridge - re-enumerate devices
  873. * @bridge: where to begin re-enumeration
  874. *
  875. * Iterate over all slots under this bridge and make sure that if a
  876. * card is present they are enabled, and if not they are disabled.
  877. */
  878. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  879. {
  880. struct acpiphp_slot *slot;
  881. int retval = 0;
  882. int enabled, disabled;
  883. enabled = disabled = 0;
  884. for (slot = bridge->slots; slot; slot = slot->next) {
  885. unsigned int status = get_slot_status(slot);
  886. if (slot->flags & SLOT_ENABLED) {
  887. if (status == ACPI_STA_ALL)
  888. continue;
  889. retval = acpiphp_disable_slot(slot);
  890. if (retval) {
  891. err("Error occurred in disabling\n");
  892. goto err_exit;
  893. } else {
  894. acpiphp_eject_slot(slot);
  895. }
  896. disabled++;
  897. } else {
  898. if (status != ACPI_STA_ALL)
  899. continue;
  900. retval = acpiphp_enable_slot(slot);
  901. if (retval) {
  902. err("Error occurred in enabling\n");
  903. goto err_exit;
  904. }
  905. enabled++;
  906. }
  907. }
  908. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  909. err_exit:
  910. return retval;
  911. }
  912. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  913. {
  914. struct pci_dev *dev;
  915. list_for_each_entry(dev, &bus->devices, bus_list)
  916. pci_configure_slot(dev);
  917. }
  918. /*
  919. * Remove devices for which we could not assign resources, call
  920. * arch specific code to fix-up the bus
  921. */
  922. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  923. {
  924. struct pci_dev *dev;
  925. int i;
  926. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  927. list_for_each_entry(dev, &bus->devices, bus_list) {
  928. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  929. struct resource *res = &dev->resource[i];
  930. if ((res->flags & type_mask) && !res->start &&
  931. res->end) {
  932. /* Could not assign a required resources
  933. * for this device, remove it */
  934. pci_stop_and_remove_bus_device(dev);
  935. break;
  936. }
  937. }
  938. }
  939. }
  940. /* Program resources in newly inserted bridge */
  941. static int acpiphp_configure_p2p_bridge(acpi_handle handle)
  942. {
  943. struct pci_dev *pdev = acpi_get_pci_dev(handle);
  944. struct pci_bus *bus = pdev->subordinate;
  945. pci_dev_put(pdev);
  946. pci_bus_size_bridges(bus);
  947. pci_bus_assign_resources(bus);
  948. acpiphp_sanitize_bus(bus);
  949. acpiphp_set_hpp_values(bus);
  950. pci_enable_bridges(bus);
  951. return 0;
  952. }
  953. static void handle_p2p_bridge_insertion(acpi_handle handle, u32 type)
  954. {
  955. struct acpi_device *device;
  956. if ((type != ACPI_NOTIFY_BUS_CHECK) &&
  957. (type != ACPI_NOTIFY_DEVICE_CHECK)) {
  958. err("unexpected notification type %d\n", type);
  959. return;
  960. }
  961. if (acpi_bus_scan(handle)) {
  962. err("cannot add bridge to acpi list\n");
  963. return;
  964. }
  965. if (acpi_bus_get_device(handle, &device)) {
  966. err("ACPI device object missing\n");
  967. return;
  968. }
  969. if (!acpiphp_configure_p2p_bridge(handle))
  970. add_p2p_bridge(handle);
  971. else
  972. err("cannot configure and start bridge\n");
  973. }
  974. /*
  975. * ACPI event handlers
  976. */
  977. static acpi_status
  978. count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  979. {
  980. int *count = (int *)context;
  981. struct acpiphp_bridge *bridge;
  982. bridge = acpiphp_handle_to_bridge(handle);
  983. if (bridge)
  984. (*count)++;
  985. return AE_OK ;
  986. }
  987. static acpi_status
  988. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  989. {
  990. struct acpiphp_bridge *bridge;
  991. char objname[64];
  992. struct acpi_buffer buffer = { .length = sizeof(objname),
  993. .pointer = objname };
  994. bridge = acpiphp_handle_to_bridge(handle);
  995. if (bridge) {
  996. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  997. dbg("%s: re-enumerating slots under %s\n",
  998. __func__, objname);
  999. acpiphp_check_bridge(bridge);
  1000. }
  1001. return AE_OK ;
  1002. }
  1003. static void _handle_hotplug_event_bridge(struct work_struct *work)
  1004. {
  1005. struct acpiphp_bridge *bridge;
  1006. char objname[64];
  1007. struct acpi_buffer buffer = { .length = sizeof(objname),
  1008. .pointer = objname };
  1009. struct acpi_device *device;
  1010. int num_sub_bridges = 0;
  1011. struct acpi_hp_work *hp_work;
  1012. acpi_handle handle;
  1013. u32 type;
  1014. hp_work = container_of(work, struct acpi_hp_work, work);
  1015. handle = hp_work->handle;
  1016. type = hp_work->type;
  1017. if (acpi_bus_get_device(handle, &device)) {
  1018. /* This bridge must have just been physically inserted */
  1019. handle_p2p_bridge_insertion(handle, type);
  1020. goto out;
  1021. }
  1022. bridge = acpiphp_handle_to_bridge(handle);
  1023. if (type == ACPI_NOTIFY_BUS_CHECK) {
  1024. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
  1025. count_sub_bridges, NULL, &num_sub_bridges, NULL);
  1026. }
  1027. if (!bridge && !num_sub_bridges) {
  1028. err("cannot get bridge info\n");
  1029. goto out;
  1030. }
  1031. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1032. switch (type) {
  1033. case ACPI_NOTIFY_BUS_CHECK:
  1034. /* bus re-enumerate */
  1035. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1036. if (bridge) {
  1037. dbg("%s: re-enumerating slots under %s\n",
  1038. __func__, objname);
  1039. acpiphp_check_bridge(bridge);
  1040. }
  1041. if (num_sub_bridges)
  1042. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  1043. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  1044. break;
  1045. case ACPI_NOTIFY_DEVICE_CHECK:
  1046. /* device check */
  1047. dbg("%s: Device check notify on %s\n", __func__, objname);
  1048. acpiphp_check_bridge(bridge);
  1049. break;
  1050. case ACPI_NOTIFY_DEVICE_WAKE:
  1051. /* wake event */
  1052. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1053. break;
  1054. case ACPI_NOTIFY_EJECT_REQUEST:
  1055. /* request device eject */
  1056. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1057. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  1058. (bridge->flags & BRIDGE_HAS_EJ0)) {
  1059. struct acpiphp_slot *slot;
  1060. slot = bridge->func->slot;
  1061. if (!acpiphp_disable_slot(slot))
  1062. acpiphp_eject_slot(slot);
  1063. }
  1064. break;
  1065. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  1066. printk(KERN_ERR "Device %s cannot be configured due"
  1067. " to a frequency mismatch\n", objname);
  1068. break;
  1069. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  1070. printk(KERN_ERR "Device %s cannot be configured due"
  1071. " to a bus mode mismatch\n", objname);
  1072. break;
  1073. case ACPI_NOTIFY_POWER_FAULT:
  1074. printk(KERN_ERR "Device %s has suffered a power fault\n",
  1075. objname);
  1076. break;
  1077. default:
  1078. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1079. break;
  1080. }
  1081. out:
  1082. kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
  1083. }
  1084. /**
  1085. * handle_hotplug_event_bridge - handle ACPI event on bridges
  1086. * @handle: Notify()'ed acpi_handle
  1087. * @type: Notify code
  1088. * @context: pointer to acpiphp_bridge structure
  1089. *
  1090. * Handles ACPI event notification on {host,p2p} bridges.
  1091. */
  1092. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
  1093. void *context)
  1094. {
  1095. /*
  1096. * Currently the code adds all hotplug events to the kacpid_wq
  1097. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  1098. * The proper way to fix this is to reorganize the code so that
  1099. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  1100. * For now just re-add this work to the kacpi_hotplug_wq so we
  1101. * don't deadlock on hotplug actions.
  1102. */
  1103. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
  1104. }
  1105. static void _handle_hotplug_event_func(struct work_struct *work)
  1106. {
  1107. struct acpiphp_func *func;
  1108. char objname[64];
  1109. struct acpi_buffer buffer = { .length = sizeof(objname),
  1110. .pointer = objname };
  1111. struct acpi_hp_work *hp_work;
  1112. acpi_handle handle;
  1113. u32 type;
  1114. void *context;
  1115. hp_work = container_of(work, struct acpi_hp_work, work);
  1116. handle = hp_work->handle;
  1117. type = hp_work->type;
  1118. context = hp_work->context;
  1119. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1120. func = (struct acpiphp_func *)context;
  1121. switch (type) {
  1122. case ACPI_NOTIFY_BUS_CHECK:
  1123. /* bus re-enumerate */
  1124. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1125. acpiphp_enable_slot(func->slot);
  1126. break;
  1127. case ACPI_NOTIFY_DEVICE_CHECK:
  1128. /* device check : re-enumerate from parent bus */
  1129. dbg("%s: Device check notify on %s\n", __func__, objname);
  1130. acpiphp_check_bridge(func->slot->bridge);
  1131. break;
  1132. case ACPI_NOTIFY_DEVICE_WAKE:
  1133. /* wake event */
  1134. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1135. break;
  1136. case ACPI_NOTIFY_EJECT_REQUEST:
  1137. /* request device eject */
  1138. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1139. if (!(acpiphp_disable_slot(func->slot)))
  1140. acpiphp_eject_slot(func->slot);
  1141. break;
  1142. default:
  1143. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1144. break;
  1145. }
  1146. kfree(hp_work); /* allocated in handle_hotplug_event_func */
  1147. }
  1148. /**
  1149. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1150. * @handle: Notify()'ed acpi_handle
  1151. * @type: Notify code
  1152. * @context: pointer to acpiphp_func structure
  1153. *
  1154. * Handles ACPI event notification on slots.
  1155. */
  1156. static void handle_hotplug_event_func(acpi_handle handle, u32 type,
  1157. void *context)
  1158. {
  1159. /*
  1160. * Currently the code adds all hotplug events to the kacpid_wq
  1161. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  1162. * The proper way to fix this is to reorganize the code so that
  1163. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  1164. * For now just re-add this work to the kacpi_hotplug_wq so we
  1165. * don't deadlock on hotplug actions.
  1166. */
  1167. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
  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. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1179. return 0;
  1180. }
  1181. /**
  1182. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1183. *
  1184. * This function frees all data allocated in acpiphp_glue_init().
  1185. */
  1186. void acpiphp_glue_exit(void)
  1187. {
  1188. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1189. }
  1190. /**
  1191. * acpiphp_enable_slot - power on slot
  1192. * @slot: ACPI PHP slot
  1193. */
  1194. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1195. {
  1196. int retval;
  1197. mutex_lock(&slot->crit_sect);
  1198. /* wake up all functions */
  1199. retval = power_on_slot(slot);
  1200. if (retval)
  1201. goto err_exit;
  1202. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1203. /* configure all functions */
  1204. retval = enable_device(slot);
  1205. if (retval)
  1206. power_off_slot(slot);
  1207. } else {
  1208. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1209. power_off_slot(slot);
  1210. }
  1211. err_exit:
  1212. mutex_unlock(&slot->crit_sect);
  1213. return retval;
  1214. }
  1215. /**
  1216. * acpiphp_disable_slot - power off slot
  1217. * @slot: ACPI PHP slot
  1218. */
  1219. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1220. {
  1221. int retval = 0;
  1222. mutex_lock(&slot->crit_sect);
  1223. /* unconfigure all functions */
  1224. retval = disable_device(slot);
  1225. if (retval)
  1226. goto err_exit;
  1227. /* power off all functions */
  1228. retval = power_off_slot(slot);
  1229. if (retval)
  1230. goto err_exit;
  1231. err_exit:
  1232. mutex_unlock(&slot->crit_sect);
  1233. return retval;
  1234. }
  1235. /*
  1236. * slot enabled: 1
  1237. * slot disabled: 0
  1238. */
  1239. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1240. {
  1241. return (slot->flags & SLOT_POWEREDON);
  1242. }
  1243. /*
  1244. * latch open: 1
  1245. * latch closed: 0
  1246. */
  1247. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1248. {
  1249. unsigned int sta;
  1250. sta = get_slot_status(slot);
  1251. return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
  1252. }
  1253. /*
  1254. * adapter presence : 1
  1255. * absence : 0
  1256. */
  1257. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1258. {
  1259. unsigned int sta;
  1260. sta = get_slot_status(slot);
  1261. return (sta == 0) ? 0 : 1;
  1262. }