acpiphp_glue.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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. static DEFINE_MUTEX(bridge_mutex);
  53. #define MY_NAME "acpiphp_glue"
  54. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  55. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  56. static void acpiphp_set_hpp_values(struct pci_bus *bus);
  57. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
  58. static void free_bridge(struct kref *kref);
  59. /* callback routine to check for the existence of a pci dock device */
  60. static acpi_status
  61. is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
  62. {
  63. int *count = (int *)context;
  64. if (is_dock_device(handle)) {
  65. (*count)++;
  66. return AE_CTRL_TERMINATE;
  67. } else {
  68. return AE_OK;
  69. }
  70. }
  71. static inline void get_bridge(struct acpiphp_bridge *bridge)
  72. {
  73. kref_get(&bridge->ref);
  74. }
  75. static inline void put_bridge(struct acpiphp_bridge *bridge)
  76. {
  77. kref_put(&bridge->ref, free_bridge);
  78. }
  79. static void free_bridge(struct kref *kref)
  80. {
  81. struct acpiphp_bridge *bridge;
  82. struct acpiphp_slot *slot, *next;
  83. struct acpiphp_func *func, *tmp;
  84. bridge = container_of(kref, struct acpiphp_bridge, ref);
  85. list_for_each_entry_safe(slot, next, &bridge->slots, node) {
  86. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
  87. kfree(func);
  88. }
  89. kfree(slot);
  90. }
  91. /* Release reference acquired by acpiphp_bridge_handle_to_function() */
  92. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)
  93. put_bridge(bridge->func->slot->bridge);
  94. put_device(&bridge->pci_bus->dev);
  95. pci_dev_put(bridge->pci_dev);
  96. kfree(bridge);
  97. }
  98. /*
  99. * the _DCK method can do funny things... and sometimes not
  100. * hah-hah funny.
  101. *
  102. * TBD - figure out a way to only call fixups for
  103. * systems that require them.
  104. */
  105. static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
  106. void *v)
  107. {
  108. struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
  109. struct pci_bus *bus = func->slot->bridge->pci_bus;
  110. u32 buses;
  111. if (!bus->self)
  112. return NOTIFY_OK;
  113. /* fixup bad _DCK function that rewrites
  114. * secondary bridge on slot
  115. */
  116. pci_read_config_dword(bus->self,
  117. PCI_PRIMARY_BUS,
  118. &buses);
  119. if (((buses >> 8) & 0xff) != bus->busn_res.start) {
  120. buses = (buses & 0xff000000)
  121. | ((unsigned int)(bus->primary) << 0)
  122. | ((unsigned int)(bus->busn_res.start) << 8)
  123. | ((unsigned int)(bus->busn_res.end) << 16);
  124. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  125. }
  126. return NOTIFY_OK;
  127. }
  128. static const struct acpi_dock_ops acpiphp_dock_ops = {
  129. .handler = handle_hotplug_event_func,
  130. };
  131. /* Check whether the PCI device is managed by native PCIe hotplug driver */
  132. static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
  133. {
  134. u32 reg32;
  135. acpi_handle tmp;
  136. struct acpi_pci_root *root;
  137. /* Check whether the PCIe port supports native PCIe hotplug */
  138. if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
  139. return false;
  140. if (!(reg32 & PCI_EXP_SLTCAP_HPC))
  141. return false;
  142. /*
  143. * Check whether native PCIe hotplug has been enabled for
  144. * this PCIe hierarchy.
  145. */
  146. tmp = acpi_find_root_bridge_handle(pdev);
  147. if (!tmp)
  148. return false;
  149. root = acpi_pci_find_root(tmp);
  150. if (!root)
  151. return false;
  152. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  153. return false;
  154. return true;
  155. }
  156. /* callback routine to register each ACPI PCI slot object */
  157. static acpi_status
  158. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  159. {
  160. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
  161. struct acpiphp_slot *slot;
  162. struct acpiphp_func *newfunc;
  163. acpi_handle tmp;
  164. acpi_status status = AE_OK;
  165. unsigned long long adr, sun;
  166. int device, function, retval, found = 0;
  167. struct pci_bus *pbus = bridge->pci_bus;
  168. struct pci_dev *pdev;
  169. u32 val;
  170. if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
  171. return AE_OK;
  172. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  173. if (ACPI_FAILURE(status)) {
  174. warn("can't evaluate _ADR (%#x)\n", status);
  175. return AE_OK;
  176. }
  177. device = (adr >> 16) & 0xffff;
  178. function = adr & 0xffff;
  179. pdev = bridge->pci_dev;
  180. if (pdev && device_is_managed_by_native_pciehp(pdev))
  181. return AE_OK;
  182. newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
  183. if (!newfunc)
  184. return AE_NO_MEMORY;
  185. newfunc->handle = handle;
  186. newfunc->function = function;
  187. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  188. newfunc->flags = FUNC_HAS_EJ0;
  189. if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
  190. newfunc->flags |= FUNC_HAS_STA;
  191. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
  192. newfunc->flags |= FUNC_HAS_PS0;
  193. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
  194. newfunc->flags |= FUNC_HAS_PS3;
  195. if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
  196. newfunc->flags |= FUNC_HAS_DCK;
  197. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  198. if (ACPI_FAILURE(status)) {
  199. /*
  200. * use the count of the number of slots we've found
  201. * for the number of the slot
  202. */
  203. sun = bridge->nr_slots+1;
  204. }
  205. /* search for objects that share the same slot */
  206. list_for_each_entry(slot, &bridge->slots, node)
  207. if (slot->device == device) {
  208. if (slot->sun != sun)
  209. warn("sibling found, but _SUN doesn't match!\n");
  210. found = 1;
  211. break;
  212. }
  213. if (!found) {
  214. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  215. if (!slot) {
  216. kfree(newfunc);
  217. return AE_NO_MEMORY;
  218. }
  219. slot->bridge = bridge;
  220. slot->device = device;
  221. slot->sun = sun;
  222. INIT_LIST_HEAD(&slot->funcs);
  223. mutex_init(&slot->crit_sect);
  224. mutex_lock(&bridge_mutex);
  225. list_add_tail(&slot->node, &bridge->slots);
  226. mutex_unlock(&bridge_mutex);
  227. bridge->nr_slots++;
  228. dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  229. slot->sun, pci_domain_nr(pbus), pbus->number, device);
  230. retval = acpiphp_register_hotplug_slot(slot);
  231. if (retval) {
  232. if (retval == -EBUSY)
  233. warn("Slot %llu already registered by another "
  234. "hotplug driver\n", slot->sun);
  235. else
  236. warn("acpiphp_register_hotplug_slot failed "
  237. "(err code = 0x%x)\n", retval);
  238. goto err_exit;
  239. }
  240. }
  241. newfunc->slot = slot;
  242. mutex_lock(&bridge_mutex);
  243. list_add_tail(&newfunc->sibling, &slot->funcs);
  244. mutex_unlock(&bridge_mutex);
  245. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  246. &val, 60*1000))
  247. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  248. if (is_dock_device(handle)) {
  249. /* we don't want to call this device's _EJ0
  250. * because we want the dock notify handler
  251. * to call it after it calls _DCK
  252. */
  253. newfunc->flags &= ~FUNC_HAS_EJ0;
  254. if (register_hotplug_dock_device(handle,
  255. &acpiphp_dock_ops, newfunc))
  256. dbg("failed to register dock device\n");
  257. /* we need to be notified when dock events happen
  258. * outside of the hotplug operation, since we may
  259. * need to do fixups before we can hotplug.
  260. */
  261. newfunc->nb.notifier_call = post_dock_fixups;
  262. if (register_dock_notifier(&newfunc->nb))
  263. dbg("failed to register a dock notifier");
  264. }
  265. /* install notify handler */
  266. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  267. status = acpi_install_notify_handler(handle,
  268. ACPI_SYSTEM_NOTIFY,
  269. handle_hotplug_event_func,
  270. newfunc);
  271. if (ACPI_FAILURE(status))
  272. err("failed to register interrupt notify handler\n");
  273. } else
  274. status = AE_OK;
  275. return status;
  276. err_exit:
  277. bridge->nr_slots--;
  278. mutex_lock(&bridge_mutex);
  279. list_del(&slot->node);
  280. mutex_unlock(&bridge_mutex);
  281. kfree(slot);
  282. kfree(newfunc);
  283. return AE_OK;
  284. }
  285. /* see if it's worth looking at this bridge */
  286. static int detect_ejectable_slots(acpi_handle handle)
  287. {
  288. int found = acpi_pci_detect_ejectable(handle);
  289. if (!found) {
  290. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  291. is_pci_dock_device, NULL, (void *)&found, NULL);
  292. }
  293. return found;
  294. }
  295. /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
  296. static void init_bridge_misc(struct acpiphp_bridge *bridge)
  297. {
  298. acpi_status status;
  299. /* must be added to the list prior to calling register_slot */
  300. mutex_lock(&bridge_mutex);
  301. list_add(&bridge->list, &bridge_list);
  302. mutex_unlock(&bridge_mutex);
  303. /* register all slot objects under this bridge */
  304. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
  305. register_slot, NULL, bridge, NULL);
  306. if (ACPI_FAILURE(status)) {
  307. mutex_lock(&bridge_mutex);
  308. list_del(&bridge->list);
  309. mutex_unlock(&bridge_mutex);
  310. return;
  311. }
  312. /* install notify handler for P2P bridges */
  313. if (!pci_is_root_bus(bridge->pci_bus)) {
  314. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  315. status = acpi_remove_notify_handler(bridge->func->handle,
  316. ACPI_SYSTEM_NOTIFY,
  317. handle_hotplug_event_func);
  318. if (ACPI_FAILURE(status))
  319. err("failed to remove notify handler\n");
  320. }
  321. status = acpi_install_notify_handler(bridge->handle,
  322. ACPI_SYSTEM_NOTIFY,
  323. handle_hotplug_event_bridge,
  324. bridge);
  325. if (ACPI_FAILURE(status)) {
  326. err("failed to register interrupt notify handler\n");
  327. }
  328. }
  329. }
  330. /* find acpiphp_func from acpiphp_bridge */
  331. static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
  332. {
  333. struct acpiphp_bridge *bridge;
  334. struct acpiphp_slot *slot;
  335. struct acpiphp_func *func = NULL;
  336. mutex_lock(&bridge_mutex);
  337. list_for_each_entry(bridge, &bridge_list, list) {
  338. list_for_each_entry(slot, &bridge->slots, node) {
  339. list_for_each_entry(func, &slot->funcs, sibling) {
  340. if (func->handle == handle) {
  341. get_bridge(func->slot->bridge);
  342. mutex_unlock(&bridge_mutex);
  343. return func;
  344. }
  345. }
  346. }
  347. }
  348. mutex_unlock(&bridge_mutex);
  349. return NULL;
  350. }
  351. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  352. {
  353. struct acpiphp_bridge *bridge;
  354. mutex_lock(&bridge_mutex);
  355. list_for_each_entry(bridge, &bridge_list, list)
  356. if (bridge->handle == handle) {
  357. get_bridge(bridge);
  358. mutex_unlock(&bridge_mutex);
  359. return bridge;
  360. }
  361. mutex_unlock(&bridge_mutex);
  362. return NULL;
  363. }
  364. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  365. {
  366. struct acpiphp_slot *slot;
  367. struct acpiphp_func *func;
  368. acpi_status status;
  369. acpi_handle handle = bridge->handle;
  370. if (!pci_is_root_bus(bridge->pci_bus)) {
  371. status = acpi_remove_notify_handler(handle,
  372. ACPI_SYSTEM_NOTIFY,
  373. handle_hotplug_event_bridge);
  374. if (ACPI_FAILURE(status))
  375. err("failed to remove notify handler\n");
  376. }
  377. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  378. status = acpi_install_notify_handler(bridge->func->handle,
  379. ACPI_SYSTEM_NOTIFY,
  380. handle_hotplug_event_func,
  381. bridge->func);
  382. if (ACPI_FAILURE(status))
  383. err("failed to install interrupt notify handler\n");
  384. }
  385. list_for_each_entry(slot, &bridge->slots, node) {
  386. list_for_each_entry(func, &slot->funcs, sibling) {
  387. if (is_dock_device(func->handle)) {
  388. unregister_hotplug_dock_device(func->handle);
  389. unregister_dock_notifier(&func->nb);
  390. }
  391. if (!(func->flags & FUNC_HAS_DCK)) {
  392. status = acpi_remove_notify_handler(func->handle,
  393. ACPI_SYSTEM_NOTIFY,
  394. handle_hotplug_event_func);
  395. if (ACPI_FAILURE(status))
  396. err("failed to remove notify handler\n");
  397. }
  398. }
  399. acpiphp_unregister_hotplug_slot(slot);
  400. }
  401. mutex_lock(&bridge_mutex);
  402. list_del(&bridge->list);
  403. mutex_unlock(&bridge_mutex);
  404. }
  405. static int power_on_slot(struct acpiphp_slot *slot)
  406. {
  407. acpi_status status;
  408. struct acpiphp_func *func;
  409. int retval = 0;
  410. /* if already enabled, just skip */
  411. if (slot->flags & SLOT_POWEREDON)
  412. goto err_exit;
  413. list_for_each_entry(func, &slot->funcs, sibling) {
  414. if (func->flags & FUNC_HAS_PS0) {
  415. dbg("%s: executing _PS0\n", __func__);
  416. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  417. if (ACPI_FAILURE(status)) {
  418. warn("%s: _PS0 failed\n", __func__);
  419. retval = -1;
  420. goto err_exit;
  421. } else
  422. break;
  423. }
  424. }
  425. /* TBD: evaluate _STA to check if the slot is enabled */
  426. slot->flags |= SLOT_POWEREDON;
  427. err_exit:
  428. return retval;
  429. }
  430. static int power_off_slot(struct acpiphp_slot *slot)
  431. {
  432. acpi_status status;
  433. struct acpiphp_func *func;
  434. int retval = 0;
  435. /* if already disabled, just skip */
  436. if ((slot->flags & SLOT_POWEREDON) == 0)
  437. goto err_exit;
  438. list_for_each_entry(func, &slot->funcs, sibling) {
  439. if (func->flags & FUNC_HAS_PS3) {
  440. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  441. if (ACPI_FAILURE(status)) {
  442. warn("%s: _PS3 failed\n", __func__);
  443. retval = -1;
  444. goto err_exit;
  445. } else
  446. break;
  447. }
  448. }
  449. /* TBD: evaluate _STA to check if the slot is disabled */
  450. slot->flags &= (~SLOT_POWEREDON);
  451. err_exit:
  452. return retval;
  453. }
  454. /**
  455. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  456. * @bus: bus to start search with
  457. */
  458. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  459. {
  460. struct list_head *tmp;
  461. unsigned char max, n;
  462. /*
  463. * pci_bus_max_busnr will return the highest
  464. * reserved busnr for all these children.
  465. * that is equivalent to the bus->subordinate
  466. * value. We don't want to use the parent's
  467. * bus->subordinate value because it could have
  468. * padding in it.
  469. */
  470. max = bus->busn_res.start;
  471. list_for_each(tmp, &bus->children) {
  472. n = pci_bus_max_busnr(pci_bus_b(tmp));
  473. if (n > max)
  474. max = n;
  475. }
  476. return max;
  477. }
  478. /**
  479. * acpiphp_bus_add - add a new bus to acpi subsystem
  480. * @func: acpiphp_func of the bridge
  481. */
  482. static int acpiphp_bus_add(struct acpiphp_func *func)
  483. {
  484. struct acpi_device *device;
  485. int ret_val;
  486. if (!acpi_bus_get_device(func->handle, &device)) {
  487. dbg("bus exists... trim\n");
  488. /* this shouldn't be in here, so remove
  489. * the bus then re-add it...
  490. */
  491. acpi_bus_trim(device);
  492. }
  493. ret_val = acpi_bus_scan(func->handle);
  494. if (!ret_val)
  495. ret_val = acpi_bus_get_device(func->handle, &device);
  496. if (ret_val)
  497. dbg("error adding bus, %x\n", -ret_val);
  498. return ret_val;
  499. }
  500. /**
  501. * acpiphp_bus_trim - trim a bus from acpi subsystem
  502. * @handle: handle to acpi namespace
  503. */
  504. static int acpiphp_bus_trim(acpi_handle handle)
  505. {
  506. struct acpi_device *device;
  507. int retval;
  508. retval = acpi_bus_get_device(handle, &device);
  509. if (retval) {
  510. dbg("acpi_device not found\n");
  511. return retval;
  512. }
  513. acpi_bus_trim(device);
  514. return 0;
  515. }
  516. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  517. {
  518. struct acpiphp_func *func;
  519. union acpi_object params[2];
  520. struct acpi_object_list arg_list;
  521. list_for_each_entry(func, &slot->funcs, sibling) {
  522. arg_list.count = 2;
  523. arg_list.pointer = params;
  524. params[0].type = ACPI_TYPE_INTEGER;
  525. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  526. params[1].type = ACPI_TYPE_INTEGER;
  527. params[1].integer.value = 1;
  528. /* _REG is optional, we don't care about if there is failure */
  529. acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
  530. }
  531. }
  532. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  533. {
  534. struct acpiphp_func *func;
  535. if (!dev->subordinate)
  536. return;
  537. /* quirk, or pcie could set it already */
  538. if (dev->is_hotplug_bridge)
  539. return;
  540. if (PCI_SLOT(dev->devfn) != slot->device)
  541. return;
  542. list_for_each_entry(func, &slot->funcs, sibling) {
  543. if (PCI_FUNC(dev->devfn) == func->function) {
  544. /* check if this bridge has ejectable slots */
  545. if ((detect_ejectable_slots(func->handle) > 0))
  546. dev->is_hotplug_bridge = 1;
  547. break;
  548. }
  549. }
  550. }
  551. /**
  552. * enable_device - enable, configure a slot
  553. * @slot: slot to be enabled
  554. *
  555. * This function should be called per *physical slot*,
  556. * not per each slot object in ACPI namespace.
  557. */
  558. static int __ref enable_device(struct acpiphp_slot *slot)
  559. {
  560. struct pci_dev *dev;
  561. struct pci_bus *bus = slot->bridge->pci_bus;
  562. struct acpiphp_func *func;
  563. int num, max, pass;
  564. if (slot->flags & SLOT_ENABLED)
  565. goto err_exit;
  566. list_for_each_entry(func, &slot->funcs, sibling)
  567. acpiphp_bus_add(func);
  568. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  569. if (num == 0) {
  570. /* Maybe only part of funcs are added. */
  571. dbg("No new device found\n");
  572. goto err_exit;
  573. }
  574. max = acpiphp_max_busnr(bus);
  575. for (pass = 0; pass < 2; pass++) {
  576. list_for_each_entry(dev, &bus->devices, bus_list) {
  577. if (PCI_SLOT(dev->devfn) != slot->device)
  578. continue;
  579. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  580. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  581. max = pci_scan_bridge(bus, dev, max, pass);
  582. if (pass && dev->subordinate) {
  583. check_hotplug_bridge(slot, dev);
  584. pci_bus_size_bridges(dev->subordinate);
  585. }
  586. }
  587. }
  588. }
  589. pci_bus_assign_resources(bus);
  590. acpiphp_sanitize_bus(bus);
  591. acpiphp_set_hpp_values(bus);
  592. acpiphp_set_acpi_region(slot);
  593. pci_enable_bridges(bus);
  594. list_for_each_entry(dev, &bus->devices, bus_list) {
  595. /* Assume that newly added devices are powered on already. */
  596. if (!dev->is_added)
  597. dev->current_state = PCI_D0;
  598. }
  599. pci_bus_add_devices(bus);
  600. slot->flags |= SLOT_ENABLED;
  601. list_for_each_entry(func, &slot->funcs, sibling) {
  602. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  603. func->function));
  604. if (!dev) {
  605. /* Do not set SLOT_ENABLED flag if some funcs
  606. are not added. */
  607. slot->flags &= (~SLOT_ENABLED);
  608. continue;
  609. }
  610. }
  611. err_exit:
  612. return 0;
  613. }
  614. /* return first device in slot, acquiring a reference on it */
  615. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  616. {
  617. struct pci_bus *bus = slot->bridge->pci_bus;
  618. struct pci_dev *dev;
  619. struct pci_dev *ret = NULL;
  620. down_read(&pci_bus_sem);
  621. list_for_each_entry(dev, &bus->devices, bus_list)
  622. if (PCI_SLOT(dev->devfn) == slot->device) {
  623. ret = pci_dev_get(dev);
  624. break;
  625. }
  626. up_read(&pci_bus_sem);
  627. return ret;
  628. }
  629. /**
  630. * disable_device - disable a slot
  631. * @slot: ACPI PHP slot
  632. */
  633. static int disable_device(struct acpiphp_slot *slot)
  634. {
  635. struct acpiphp_func *func;
  636. struct pci_dev *pdev;
  637. /*
  638. * enable_device() enumerates all functions in this device via
  639. * pci_scan_slot(), whether they have associated ACPI hotplug
  640. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  641. * here.
  642. */
  643. while ((pdev = dev_in_slot(slot))) {
  644. pci_stop_and_remove_bus_device(pdev);
  645. pci_dev_put(pdev);
  646. }
  647. list_for_each_entry(func, &slot->funcs, sibling) {
  648. acpiphp_bus_trim(func->handle);
  649. }
  650. slot->flags &= (~SLOT_ENABLED);
  651. return 0;
  652. }
  653. /**
  654. * get_slot_status - get ACPI slot status
  655. * @slot: ACPI PHP slot
  656. *
  657. * If a slot has _STA for each function and if any one of them
  658. * returned non-zero status, return it.
  659. *
  660. * If a slot doesn't have _STA and if any one of its functions'
  661. * configuration space is configured, return 0x0f as a _STA.
  662. *
  663. * Otherwise return 0.
  664. */
  665. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  666. {
  667. acpi_status status;
  668. unsigned long long sta = 0;
  669. u32 dvid;
  670. struct acpiphp_func *func;
  671. list_for_each_entry(func, &slot->funcs, sibling) {
  672. if (func->flags & FUNC_HAS_STA) {
  673. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  674. if (ACPI_SUCCESS(status) && sta)
  675. break;
  676. } else {
  677. pci_bus_read_config_dword(slot->bridge->pci_bus,
  678. PCI_DEVFN(slot->device,
  679. func->function),
  680. PCI_VENDOR_ID, &dvid);
  681. if (dvid != 0xffffffff) {
  682. sta = ACPI_STA_ALL;
  683. break;
  684. }
  685. }
  686. }
  687. return (unsigned int)sta;
  688. }
  689. /**
  690. * acpiphp_eject_slot - physically eject the slot
  691. * @slot: ACPI PHP slot
  692. */
  693. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  694. {
  695. acpi_status status;
  696. struct acpiphp_func *func;
  697. struct acpi_object_list arg_list;
  698. union acpi_object arg;
  699. list_for_each_entry(func, &slot->funcs, sibling) {
  700. /* We don't want to call _EJ0 on non-existing functions. */
  701. if ((func->flags & FUNC_HAS_EJ0)) {
  702. /* _EJ0 method take one argument */
  703. arg_list.count = 1;
  704. arg_list.pointer = &arg;
  705. arg.type = ACPI_TYPE_INTEGER;
  706. arg.integer.value = 1;
  707. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  708. if (ACPI_FAILURE(status)) {
  709. warn("%s: _EJ0 failed\n", __func__);
  710. return -1;
  711. } else
  712. break;
  713. }
  714. }
  715. return 0;
  716. }
  717. /**
  718. * acpiphp_check_bridge - re-enumerate devices
  719. * @bridge: where to begin re-enumeration
  720. *
  721. * Iterate over all slots under this bridge and make sure that if a
  722. * card is present they are enabled, and if not they are disabled.
  723. */
  724. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  725. {
  726. struct acpiphp_slot *slot;
  727. int retval = 0;
  728. int enabled, disabled;
  729. enabled = disabled = 0;
  730. list_for_each_entry(slot, &bridge->slots, node) {
  731. unsigned int status = get_slot_status(slot);
  732. if (slot->flags & SLOT_ENABLED) {
  733. if (status == ACPI_STA_ALL)
  734. continue;
  735. retval = acpiphp_disable_slot(slot);
  736. if (retval) {
  737. err("Error occurred in disabling\n");
  738. goto err_exit;
  739. } else {
  740. acpiphp_eject_slot(slot);
  741. }
  742. disabled++;
  743. } else {
  744. if (status != ACPI_STA_ALL)
  745. continue;
  746. retval = acpiphp_enable_slot(slot);
  747. if (retval) {
  748. err("Error occurred in enabling\n");
  749. goto err_exit;
  750. }
  751. enabled++;
  752. }
  753. }
  754. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  755. err_exit:
  756. return retval;
  757. }
  758. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  759. {
  760. struct pci_dev *dev;
  761. list_for_each_entry(dev, &bus->devices, bus_list)
  762. pci_configure_slot(dev);
  763. }
  764. /*
  765. * Remove devices for which we could not assign resources, call
  766. * arch specific code to fix-up the bus
  767. */
  768. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  769. {
  770. struct pci_dev *dev, *tmp;
  771. int i;
  772. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  773. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  774. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  775. struct resource *res = &dev->resource[i];
  776. if ((res->flags & type_mask) && !res->start &&
  777. res->end) {
  778. /* Could not assign a required resources
  779. * for this device, remove it */
  780. pci_stop_and_remove_bus_device(dev);
  781. break;
  782. }
  783. }
  784. }
  785. }
  786. /*
  787. * ACPI event handlers
  788. */
  789. static acpi_status
  790. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  791. {
  792. struct acpiphp_bridge *bridge;
  793. char objname[64];
  794. struct acpi_buffer buffer = { .length = sizeof(objname),
  795. .pointer = objname };
  796. bridge = acpiphp_handle_to_bridge(handle);
  797. if (bridge) {
  798. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  799. dbg("%s: re-enumerating slots under %s\n",
  800. __func__, objname);
  801. acpiphp_check_bridge(bridge);
  802. put_bridge(bridge);
  803. }
  804. return AE_OK ;
  805. }
  806. static void _handle_hotplug_event_bridge(struct work_struct *work)
  807. {
  808. struct acpiphp_bridge *bridge;
  809. char objname[64];
  810. struct acpi_buffer buffer = { .length = sizeof(objname),
  811. .pointer = objname };
  812. struct acpi_hp_work *hp_work;
  813. acpi_handle handle;
  814. u32 type;
  815. hp_work = container_of(work, struct acpi_hp_work, work);
  816. handle = hp_work->handle;
  817. type = hp_work->type;
  818. bridge = (struct acpiphp_bridge *)hp_work->context;
  819. acpi_scan_lock_acquire();
  820. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  821. switch (type) {
  822. case ACPI_NOTIFY_BUS_CHECK:
  823. /* bus re-enumerate */
  824. dbg("%s: Bus check notify on %s\n", __func__, objname);
  825. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  826. acpiphp_check_bridge(bridge);
  827. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  828. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  829. break;
  830. case ACPI_NOTIFY_DEVICE_CHECK:
  831. /* device check */
  832. dbg("%s: Device check notify on %s\n", __func__, objname);
  833. acpiphp_check_bridge(bridge);
  834. break;
  835. case ACPI_NOTIFY_DEVICE_WAKE:
  836. /* wake event */
  837. dbg("%s: Device wake notify on %s\n", __func__, objname);
  838. break;
  839. case ACPI_NOTIFY_EJECT_REQUEST:
  840. /* request device eject */
  841. dbg("%s: Device eject notify on %s\n", __func__, objname);
  842. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  843. struct acpiphp_slot *slot;
  844. slot = bridge->func->slot;
  845. if (!acpiphp_disable_slot(slot))
  846. acpiphp_eject_slot(slot);
  847. }
  848. break;
  849. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  850. printk(KERN_ERR "Device %s cannot be configured due"
  851. " to a frequency mismatch\n", objname);
  852. break;
  853. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  854. printk(KERN_ERR "Device %s cannot be configured due"
  855. " to a bus mode mismatch\n", objname);
  856. break;
  857. case ACPI_NOTIFY_POWER_FAULT:
  858. printk(KERN_ERR "Device %s has suffered a power fault\n",
  859. objname);
  860. break;
  861. default:
  862. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  863. break;
  864. }
  865. acpi_scan_lock_release();
  866. kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
  867. put_bridge(bridge);
  868. }
  869. /**
  870. * handle_hotplug_event_bridge - handle ACPI event on bridges
  871. * @handle: Notify()'ed acpi_handle
  872. * @type: Notify code
  873. * @context: pointer to acpiphp_bridge structure
  874. *
  875. * Handles ACPI event notification on {host,p2p} bridges.
  876. */
  877. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
  878. void *context)
  879. {
  880. struct acpiphp_bridge *bridge = context;
  881. /*
  882. * Currently the code adds all hotplug events to the kacpid_wq
  883. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  884. * The proper way to fix this is to reorganize the code so that
  885. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  886. * For now just re-add this work to the kacpi_hotplug_wq so we
  887. * don't deadlock on hotplug actions.
  888. */
  889. get_bridge(bridge);
  890. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
  891. }
  892. static void _handle_hotplug_event_func(struct work_struct *work)
  893. {
  894. struct acpiphp_func *func;
  895. char objname[64];
  896. struct acpi_buffer buffer = { .length = sizeof(objname),
  897. .pointer = objname };
  898. struct acpi_hp_work *hp_work;
  899. acpi_handle handle;
  900. u32 type;
  901. hp_work = container_of(work, struct acpi_hp_work, work);
  902. handle = hp_work->handle;
  903. type = hp_work->type;
  904. func = (struct acpiphp_func *)hp_work->context;
  905. acpi_scan_lock_acquire();
  906. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  907. switch (type) {
  908. case ACPI_NOTIFY_BUS_CHECK:
  909. /* bus re-enumerate */
  910. dbg("%s: Bus check notify on %s\n", __func__, objname);
  911. acpiphp_enable_slot(func->slot);
  912. break;
  913. case ACPI_NOTIFY_DEVICE_CHECK:
  914. /* device check : re-enumerate from parent bus */
  915. dbg("%s: Device check notify on %s\n", __func__, objname);
  916. acpiphp_check_bridge(func->slot->bridge);
  917. break;
  918. case ACPI_NOTIFY_DEVICE_WAKE:
  919. /* wake event */
  920. dbg("%s: Device wake notify on %s\n", __func__, objname);
  921. break;
  922. case ACPI_NOTIFY_EJECT_REQUEST:
  923. /* request device eject */
  924. dbg("%s: Device eject notify on %s\n", __func__, objname);
  925. if (!(acpiphp_disable_slot(func->slot)))
  926. acpiphp_eject_slot(func->slot);
  927. break;
  928. default:
  929. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  930. break;
  931. }
  932. acpi_scan_lock_release();
  933. kfree(hp_work); /* allocated in handle_hotplug_event_func */
  934. put_bridge(func->slot->bridge);
  935. }
  936. /**
  937. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  938. * @handle: Notify()'ed acpi_handle
  939. * @type: Notify code
  940. * @context: pointer to acpiphp_func structure
  941. *
  942. * Handles ACPI event notification on slots.
  943. */
  944. static void handle_hotplug_event_func(acpi_handle handle, u32 type,
  945. void *context)
  946. {
  947. struct acpiphp_func *func = context;
  948. /*
  949. * Currently the code adds all hotplug events to the kacpid_wq
  950. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  951. * The proper way to fix this is to reorganize the code so that
  952. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  953. * For now just re-add this work to the kacpi_hotplug_wq so we
  954. * don't deadlock on hotplug actions.
  955. */
  956. get_bridge(func->slot->bridge);
  957. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
  958. }
  959. /*
  960. * Create hotplug slots for the PCI bus.
  961. * It should always return 0 to avoid skipping following notifiers.
  962. */
  963. void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
  964. {
  965. acpi_handle dummy_handle;
  966. struct acpiphp_bridge *bridge;
  967. if (acpiphp_disabled)
  968. return;
  969. if (detect_ejectable_slots(handle) <= 0)
  970. return;
  971. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  972. if (bridge == NULL) {
  973. err("out of memory\n");
  974. return;
  975. }
  976. INIT_LIST_HEAD(&bridge->slots);
  977. kref_init(&bridge->ref);
  978. bridge->handle = handle;
  979. bridge->pci_dev = pci_dev_get(bus->self);
  980. bridge->pci_bus = bus;
  981. /*
  982. * Grab a ref to the subordinate PCI bus in case the bus is
  983. * removed via PCI core logical hotplug. The ref pins the bus
  984. * (which we access during module unload).
  985. */
  986. get_device(&bus->dev);
  987. if (!pci_is_root_bus(bridge->pci_bus) &&
  988. ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  989. "_EJ0", &dummy_handle))) {
  990. dbg("found ejectable p2p bridge\n");
  991. bridge->flags |= BRIDGE_HAS_EJ0;
  992. bridge->func = acpiphp_bridge_handle_to_function(handle);
  993. }
  994. init_bridge_misc(bridge);
  995. }
  996. /* Destroy hotplug slots associated with the PCI bus */
  997. void acpiphp_remove_slots(struct pci_bus *bus)
  998. {
  999. struct acpiphp_bridge *bridge, *tmp;
  1000. if (acpiphp_disabled)
  1001. return;
  1002. list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
  1003. if (bridge->pci_bus == bus) {
  1004. cleanup_bridge(bridge);
  1005. put_bridge(bridge);
  1006. break;
  1007. }
  1008. }
  1009. /**
  1010. * acpiphp_enable_slot - power on slot
  1011. * @slot: ACPI PHP slot
  1012. */
  1013. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1014. {
  1015. int retval;
  1016. mutex_lock(&slot->crit_sect);
  1017. /* wake up all functions */
  1018. retval = power_on_slot(slot);
  1019. if (retval)
  1020. goto err_exit;
  1021. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1022. /* configure all functions */
  1023. retval = enable_device(slot);
  1024. if (retval)
  1025. power_off_slot(slot);
  1026. } else {
  1027. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1028. power_off_slot(slot);
  1029. }
  1030. err_exit:
  1031. mutex_unlock(&slot->crit_sect);
  1032. return retval;
  1033. }
  1034. /**
  1035. * acpiphp_disable_slot - power off slot
  1036. * @slot: ACPI PHP slot
  1037. */
  1038. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1039. {
  1040. int retval = 0;
  1041. mutex_lock(&slot->crit_sect);
  1042. /* unconfigure all functions */
  1043. retval = disable_device(slot);
  1044. if (retval)
  1045. goto err_exit;
  1046. /* power off all functions */
  1047. retval = power_off_slot(slot);
  1048. if (retval)
  1049. goto err_exit;
  1050. err_exit:
  1051. mutex_unlock(&slot->crit_sect);
  1052. return retval;
  1053. }
  1054. /*
  1055. * slot enabled: 1
  1056. * slot disabled: 0
  1057. */
  1058. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1059. {
  1060. return (slot->flags & SLOT_POWEREDON);
  1061. }
  1062. /*
  1063. * latch open: 1
  1064. * latch closed: 0
  1065. */
  1066. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1067. {
  1068. unsigned int sta;
  1069. sta = get_slot_status(slot);
  1070. return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
  1071. }
  1072. /*
  1073. * adapter presence : 1
  1074. * absence : 0
  1075. */
  1076. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1077. {
  1078. unsigned int sta;
  1079. sta = get_slot_status(slot);
  1080. return (sta == 0) ? 0 : 1;
  1081. }