acpiphp_glue.c 38 KB

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