acpiphp_glue.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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/pm_runtime.h>
  47. #include <linux/mutex.h>
  48. #include <linux/slab.h>
  49. #include <linux/acpi.h>
  50. #include "../pci.h"
  51. #include "acpiphp.h"
  52. static LIST_HEAD(bridge_list);
  53. static DEFINE_MUTEX(bridge_mutex);
  54. static DEFINE_MUTEX(acpiphp_context_lock);
  55. #define MY_NAME "acpiphp_glue"
  56. static void handle_hotplug_event(acpi_handle handle, u32 type, void *data);
  57. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  58. static void acpiphp_set_hpp_values(struct pci_bus *bus);
  59. static void hotplug_event(acpi_handle handle, u32 type, void *data);
  60. static void free_bridge(struct kref *kref);
  61. static void acpiphp_context_handler(acpi_handle handle, void *context)
  62. {
  63. /* Intentionally empty. */
  64. }
  65. /**
  66. * acpiphp_init_context - Create hotplug context and grab a reference to it.
  67. * @handle: ACPI object handle to create the context for.
  68. *
  69. * Call under acpiphp_context_lock.
  70. */
  71. static struct acpiphp_context *acpiphp_init_context(acpi_handle handle)
  72. {
  73. struct acpiphp_context *context;
  74. acpi_status status;
  75. context = kzalloc(sizeof(*context), GFP_KERNEL);
  76. if (!context)
  77. return NULL;
  78. context->handle = handle;
  79. context->refcount = 1;
  80. status = acpi_attach_data(handle, acpiphp_context_handler, context);
  81. if (ACPI_FAILURE(status)) {
  82. kfree(context);
  83. return NULL;
  84. }
  85. return context;
  86. }
  87. /**
  88. * acpiphp_get_context - Get hotplug context and grab a reference to it.
  89. * @handle: ACPI object handle to get the context for.
  90. *
  91. * Call under acpiphp_context_lock.
  92. */
  93. static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
  94. {
  95. struct acpiphp_context *context = NULL;
  96. acpi_status status;
  97. void *data;
  98. status = acpi_get_data(handle, acpiphp_context_handler, &data);
  99. if (ACPI_SUCCESS(status)) {
  100. context = data;
  101. context->refcount++;
  102. }
  103. return context;
  104. }
  105. /**
  106. * acpiphp_put_context - Drop a reference to ACPI hotplug context.
  107. * @handle: ACPI object handle to put the context for.
  108. *
  109. * The context object is removed if there are no more references to it.
  110. *
  111. * Call under acpiphp_context_lock.
  112. */
  113. static void acpiphp_put_context(struct acpiphp_context *context)
  114. {
  115. if (--context->refcount)
  116. return;
  117. WARN_ON(context->bridge);
  118. acpi_detach_data(context->handle, acpiphp_context_handler);
  119. kfree(context);
  120. }
  121. static inline void get_bridge(struct acpiphp_bridge *bridge)
  122. {
  123. kref_get(&bridge->ref);
  124. }
  125. static inline void put_bridge(struct acpiphp_bridge *bridge)
  126. {
  127. kref_put(&bridge->ref, free_bridge);
  128. }
  129. static void free_bridge(struct kref *kref)
  130. {
  131. struct acpiphp_context *context;
  132. struct acpiphp_bridge *bridge;
  133. struct acpiphp_slot *slot, *next;
  134. struct acpiphp_func *func, *tmp;
  135. mutex_lock(&acpiphp_context_lock);
  136. bridge = container_of(kref, struct acpiphp_bridge, ref);
  137. list_for_each_entry_safe(slot, next, &bridge->slots, node) {
  138. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
  139. acpiphp_put_context(func_to_context(func));
  140. kfree(slot);
  141. }
  142. context = bridge->context;
  143. /* Root bridges will not have hotplug context. */
  144. if (context) {
  145. /* Release the reference taken by acpiphp_enumerate_slots(). */
  146. put_bridge(context->func.parent);
  147. context->bridge = NULL;
  148. acpiphp_put_context(context);
  149. }
  150. put_device(&bridge->pci_bus->dev);
  151. pci_dev_put(bridge->pci_dev);
  152. kfree(bridge);
  153. mutex_unlock(&acpiphp_context_lock);
  154. }
  155. /*
  156. * the _DCK method can do funny things... and sometimes not
  157. * hah-hah funny.
  158. *
  159. * TBD - figure out a way to only call fixups for
  160. * systems that require them.
  161. */
  162. static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
  163. {
  164. struct acpiphp_context *context = data;
  165. struct pci_bus *bus = context->func.slot->bus;
  166. u32 buses;
  167. if (!bus->self)
  168. return;
  169. /* fixup bad _DCK function that rewrites
  170. * secondary bridge on slot
  171. */
  172. pci_read_config_dword(bus->self,
  173. PCI_PRIMARY_BUS,
  174. &buses);
  175. if (((buses >> 8) & 0xff) != bus->busn_res.start) {
  176. buses = (buses & 0xff000000)
  177. | ((unsigned int)(bus->primary) << 0)
  178. | ((unsigned int)(bus->busn_res.start) << 8)
  179. | ((unsigned int)(bus->busn_res.end) << 16);
  180. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  181. }
  182. }
  183. static const struct acpi_dock_ops acpiphp_dock_ops = {
  184. .fixup = post_dock_fixups,
  185. .handler = hotplug_event,
  186. };
  187. /* Check whether the PCI device is managed by native PCIe hotplug driver */
  188. static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
  189. {
  190. u32 reg32;
  191. acpi_handle tmp;
  192. struct acpi_pci_root *root;
  193. /* Check whether the PCIe port supports native PCIe hotplug */
  194. if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
  195. return false;
  196. if (!(reg32 & PCI_EXP_SLTCAP_HPC))
  197. return false;
  198. /*
  199. * Check whether native PCIe hotplug has been enabled for
  200. * this PCIe hierarchy.
  201. */
  202. tmp = acpi_find_root_bridge_handle(pdev);
  203. if (!tmp)
  204. return false;
  205. root = acpi_pci_find_root(tmp);
  206. if (!root)
  207. return false;
  208. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  209. return false;
  210. return true;
  211. }
  212. static void acpiphp_dock_init(void *data)
  213. {
  214. struct acpiphp_context *context = data;
  215. get_bridge(context->func.parent);
  216. }
  217. static void acpiphp_dock_release(void *data)
  218. {
  219. struct acpiphp_context *context = data;
  220. put_bridge(context->func.parent);
  221. }
  222. /* callback routine to register each ACPI PCI slot object */
  223. static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
  224. void **rv)
  225. {
  226. struct acpiphp_bridge *bridge = data;
  227. struct acpiphp_context *context;
  228. struct acpiphp_slot *slot;
  229. struct acpiphp_func *newfunc;
  230. acpi_status status = AE_OK;
  231. unsigned long long adr;
  232. int device, function;
  233. struct pci_bus *pbus = bridge->pci_bus;
  234. struct pci_dev *pdev = bridge->pci_dev;
  235. u32 val;
  236. if (pdev && device_is_managed_by_native_pciehp(pdev))
  237. return AE_OK;
  238. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  239. if (ACPI_FAILURE(status)) {
  240. acpi_handle_warn(handle, "can't evaluate _ADR (%#x)\n", status);
  241. return AE_OK;
  242. }
  243. device = (adr >> 16) & 0xffff;
  244. function = adr & 0xffff;
  245. mutex_lock(&acpiphp_context_lock);
  246. context = acpiphp_init_context(handle);
  247. if (!context) {
  248. mutex_unlock(&acpiphp_context_lock);
  249. acpi_handle_err(handle, "No hotplug context\n");
  250. return AE_NOT_EXIST;
  251. }
  252. newfunc = &context->func;
  253. newfunc->function = function;
  254. newfunc->parent = bridge;
  255. mutex_unlock(&acpiphp_context_lock);
  256. if (acpi_has_method(handle, "_EJ0"))
  257. newfunc->flags = FUNC_HAS_EJ0;
  258. if (acpi_has_method(handle, "_STA"))
  259. newfunc->flags |= FUNC_HAS_STA;
  260. if (acpi_has_method(handle, "_DCK"))
  261. newfunc->flags |= FUNC_HAS_DCK;
  262. /* search for objects that share the same slot */
  263. list_for_each_entry(slot, &bridge->slots, node)
  264. if (slot->device == device)
  265. goto slot_found;
  266. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  267. if (!slot) {
  268. status = AE_NO_MEMORY;
  269. goto err;
  270. }
  271. slot->bus = bridge->pci_bus;
  272. slot->device = device;
  273. INIT_LIST_HEAD(&slot->funcs);
  274. mutex_init(&slot->crit_sect);
  275. list_add_tail(&slot->node, &bridge->slots);
  276. /* Register slots for ejectable funtions only. */
  277. if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
  278. unsigned long long sun;
  279. int retval;
  280. bridge->nr_slots++;
  281. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  282. if (ACPI_FAILURE(status))
  283. sun = bridge->nr_slots;
  284. dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  285. sun, pci_domain_nr(pbus), pbus->number, device);
  286. retval = acpiphp_register_hotplug_slot(slot, sun);
  287. if (retval) {
  288. bridge->nr_slots--;
  289. if (retval == -EBUSY)
  290. warn("Slot %llu already registered by another "
  291. "hotplug driver\n", sun);
  292. else
  293. warn("acpiphp_register_hotplug_slot failed "
  294. "(err code = 0x%x)\n", retval);
  295. }
  296. /* Even if the slot registration fails, we can still use it. */
  297. }
  298. slot_found:
  299. newfunc->slot = slot;
  300. list_add_tail(&newfunc->sibling, &slot->funcs);
  301. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  302. &val, 60*1000))
  303. slot->flags |= SLOT_ENABLED;
  304. if (is_dock_device(handle)) {
  305. /* we don't want to call this device's _EJ0
  306. * because we want the dock notify handler
  307. * to call it after it calls _DCK
  308. */
  309. newfunc->flags &= ~FUNC_HAS_EJ0;
  310. if (register_hotplug_dock_device(handle,
  311. &acpiphp_dock_ops, context,
  312. acpiphp_dock_init, acpiphp_dock_release))
  313. dbg("failed to register dock device\n");
  314. }
  315. /* install notify handler */
  316. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  317. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  318. handle_hotplug_event,
  319. context);
  320. if (ACPI_FAILURE(status))
  321. acpi_handle_err(handle,
  322. "failed to install notify handler\n");
  323. }
  324. return AE_OK;
  325. err:
  326. mutex_lock(&acpiphp_context_lock);
  327. acpiphp_put_context(context);
  328. mutex_unlock(&acpiphp_context_lock);
  329. return status;
  330. }
  331. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  332. {
  333. struct acpiphp_context *context;
  334. struct acpiphp_bridge *bridge = NULL;
  335. mutex_lock(&acpiphp_context_lock);
  336. context = acpiphp_get_context(handle);
  337. if (context) {
  338. bridge = context->bridge;
  339. if (bridge)
  340. get_bridge(bridge);
  341. acpiphp_put_context(context);
  342. }
  343. mutex_unlock(&acpiphp_context_lock);
  344. return bridge;
  345. }
  346. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  347. {
  348. struct acpiphp_slot *slot;
  349. struct acpiphp_func *func;
  350. acpi_status status;
  351. list_for_each_entry(slot, &bridge->slots, node) {
  352. list_for_each_entry(func, &slot->funcs, sibling) {
  353. acpi_handle handle = func_to_handle(func);
  354. if (is_dock_device(handle))
  355. unregister_hotplug_dock_device(handle);
  356. if (!(func->flags & FUNC_HAS_DCK)) {
  357. status = acpi_remove_notify_handler(handle,
  358. ACPI_SYSTEM_NOTIFY,
  359. handle_hotplug_event);
  360. if (ACPI_FAILURE(status))
  361. err("failed to remove notify handler\n");
  362. }
  363. }
  364. acpiphp_unregister_hotplug_slot(slot);
  365. }
  366. mutex_lock(&bridge_mutex);
  367. list_del(&bridge->list);
  368. mutex_unlock(&bridge_mutex);
  369. }
  370. /**
  371. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  372. * @bus: bus to start search with
  373. */
  374. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  375. {
  376. struct list_head *tmp;
  377. unsigned char max, n;
  378. /*
  379. * pci_bus_max_busnr will return the highest
  380. * reserved busnr for all these children.
  381. * that is equivalent to the bus->subordinate
  382. * value. We don't want to use the parent's
  383. * bus->subordinate value because it could have
  384. * padding in it.
  385. */
  386. max = bus->busn_res.start;
  387. list_for_each(tmp, &bus->children) {
  388. n = pci_bus_max_busnr(pci_bus_b(tmp));
  389. if (n > max)
  390. max = n;
  391. }
  392. return max;
  393. }
  394. /**
  395. * acpiphp_bus_trim - Trim device objects in an ACPI namespace subtree.
  396. * @handle: ACPI device object handle to start from.
  397. */
  398. static void acpiphp_bus_trim(acpi_handle handle)
  399. {
  400. struct acpi_device *adev = NULL;
  401. acpi_bus_get_device(handle, &adev);
  402. if (adev)
  403. acpi_bus_trim(adev);
  404. }
  405. /**
  406. * acpiphp_bus_add - Scan ACPI namespace subtree.
  407. * @handle: ACPI object handle to start the scan from.
  408. */
  409. static void acpiphp_bus_add(acpi_handle handle)
  410. {
  411. struct acpi_device *adev = NULL;
  412. acpiphp_bus_trim(handle);
  413. acpi_bus_scan(handle);
  414. acpi_bus_get_device(handle, &adev);
  415. if (adev)
  416. acpi_device_set_power(adev, ACPI_STATE_D0);
  417. }
  418. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  419. {
  420. struct acpiphp_func *func;
  421. union acpi_object params[2];
  422. struct acpi_object_list arg_list;
  423. list_for_each_entry(func, &slot->funcs, sibling) {
  424. arg_list.count = 2;
  425. arg_list.pointer = params;
  426. params[0].type = ACPI_TYPE_INTEGER;
  427. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  428. params[1].type = ACPI_TYPE_INTEGER;
  429. params[1].integer.value = 1;
  430. /* _REG is optional, we don't care about if there is failure */
  431. acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
  432. NULL);
  433. }
  434. }
  435. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  436. {
  437. struct acpiphp_func *func;
  438. /* quirk, or pcie could set it already */
  439. if (dev->is_hotplug_bridge)
  440. return;
  441. list_for_each_entry(func, &slot->funcs, sibling) {
  442. if (PCI_FUNC(dev->devfn) == func->function) {
  443. dev->is_hotplug_bridge = 1;
  444. break;
  445. }
  446. }
  447. }
  448. /**
  449. * enable_slot - enable, configure a slot
  450. * @slot: slot to be enabled
  451. *
  452. * This function should be called per *physical slot*,
  453. * not per each slot object in ACPI namespace.
  454. */
  455. static void __ref enable_slot(struct acpiphp_slot *slot)
  456. {
  457. struct pci_dev *dev;
  458. struct pci_bus *bus = slot->bus;
  459. struct acpiphp_func *func;
  460. int max, pass;
  461. LIST_HEAD(add_list);
  462. list_for_each_entry(func, &slot->funcs, sibling)
  463. acpiphp_bus_add(func_to_handle(func));
  464. pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  465. max = acpiphp_max_busnr(bus);
  466. for (pass = 0; pass < 2; pass++) {
  467. list_for_each_entry(dev, &bus->devices, bus_list) {
  468. if (PCI_SLOT(dev->devfn) != slot->device)
  469. continue;
  470. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  471. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  472. max = pci_scan_bridge(bus, dev, max, pass);
  473. if (pass && dev->subordinate) {
  474. check_hotplug_bridge(slot, dev);
  475. pcibios_resource_survey_bus(dev->subordinate);
  476. __pci_bus_size_bridges(dev->subordinate,
  477. &add_list);
  478. }
  479. }
  480. }
  481. }
  482. __pci_bus_assign_resources(bus, &add_list, NULL);
  483. acpiphp_sanitize_bus(bus);
  484. acpiphp_set_hpp_values(bus);
  485. acpiphp_set_acpi_region(slot);
  486. pci_enable_bridges(bus);
  487. list_for_each_entry(dev, &bus->devices, bus_list) {
  488. /* Assume that newly added devices are powered on already. */
  489. if (!dev->is_added)
  490. dev->current_state = PCI_D0;
  491. }
  492. pci_bus_add_devices(bus);
  493. slot->flags |= SLOT_ENABLED;
  494. list_for_each_entry(func, &slot->funcs, sibling) {
  495. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  496. func->function));
  497. if (!dev) {
  498. /* Do not set SLOT_ENABLED flag if some funcs
  499. are not added. */
  500. slot->flags &= (~SLOT_ENABLED);
  501. continue;
  502. }
  503. }
  504. }
  505. /* return first device in slot, acquiring a reference on it */
  506. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  507. {
  508. struct pci_bus *bus = slot->bus;
  509. struct pci_dev *dev;
  510. struct pci_dev *ret = NULL;
  511. down_read(&pci_bus_sem);
  512. list_for_each_entry(dev, &bus->devices, bus_list)
  513. if (PCI_SLOT(dev->devfn) == slot->device) {
  514. ret = pci_dev_get(dev);
  515. break;
  516. }
  517. up_read(&pci_bus_sem);
  518. return ret;
  519. }
  520. /**
  521. * disable_slot - disable a slot
  522. * @slot: ACPI PHP slot
  523. */
  524. static void disable_slot(struct acpiphp_slot *slot)
  525. {
  526. struct acpiphp_func *func;
  527. struct pci_dev *pdev;
  528. /*
  529. * enable_slot() enumerates all functions in this device via
  530. * pci_scan_slot(), whether they have associated ACPI hotplug
  531. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  532. * here.
  533. */
  534. while ((pdev = dev_in_slot(slot))) {
  535. pci_stop_and_remove_bus_device(pdev);
  536. pci_dev_put(pdev);
  537. }
  538. list_for_each_entry(func, &slot->funcs, sibling)
  539. acpiphp_bus_trim(func_to_handle(func));
  540. slot->flags &= (~SLOT_ENABLED);
  541. }
  542. /**
  543. * get_slot_status - get ACPI slot status
  544. * @slot: ACPI PHP slot
  545. *
  546. * If a slot has _STA for each function and if any one of them
  547. * returned non-zero status, return it.
  548. *
  549. * If a slot doesn't have _STA and if any one of its functions'
  550. * configuration space is configured, return 0x0f as a _STA.
  551. *
  552. * Otherwise return 0.
  553. */
  554. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  555. {
  556. unsigned long long sta = 0;
  557. struct acpiphp_func *func;
  558. list_for_each_entry(func, &slot->funcs, sibling) {
  559. if (func->flags & FUNC_HAS_STA) {
  560. acpi_status status;
  561. status = acpi_evaluate_integer(func_to_handle(func),
  562. "_STA", NULL, &sta);
  563. if (ACPI_SUCCESS(status) && sta)
  564. break;
  565. } else {
  566. u32 dvid;
  567. pci_bus_read_config_dword(slot->bus,
  568. PCI_DEVFN(slot->device,
  569. func->function),
  570. PCI_VENDOR_ID, &dvid);
  571. if (dvid != 0xffffffff) {
  572. sta = ACPI_STA_ALL;
  573. break;
  574. }
  575. }
  576. }
  577. return (unsigned int)sta;
  578. }
  579. /**
  580. * trim_stale_devices - remove PCI devices that are not responding.
  581. * @dev: PCI device to start walking the hierarchy from.
  582. */
  583. static void trim_stale_devices(struct pci_dev *dev)
  584. {
  585. acpi_handle handle = ACPI_HANDLE(&dev->dev);
  586. struct pci_bus *bus = dev->subordinate;
  587. bool alive = false;
  588. if (handle) {
  589. acpi_status status;
  590. unsigned long long sta;
  591. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  592. alive = ACPI_SUCCESS(status) && sta == ACPI_STA_ALL;
  593. }
  594. if (!alive) {
  595. u32 v;
  596. /* Check if the device responds. */
  597. alive = pci_bus_read_dev_vendor_id(dev->bus, dev->devfn, &v, 0);
  598. }
  599. if (!alive) {
  600. pci_stop_and_remove_bus_device(dev);
  601. if (handle)
  602. acpiphp_bus_trim(handle);
  603. } else if (bus) {
  604. struct pci_dev *child, *tmp;
  605. /* The device is a bridge. so check the bus below it. */
  606. pm_runtime_get_sync(&dev->dev);
  607. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  608. trim_stale_devices(child);
  609. pm_runtime_put(&dev->dev);
  610. }
  611. }
  612. /**
  613. * acpiphp_check_bridge - re-enumerate devices
  614. * @bridge: where to begin re-enumeration
  615. *
  616. * Iterate over all slots under this bridge and make sure that if a
  617. * card is present they are enabled, and if not they are disabled.
  618. */
  619. static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  620. {
  621. struct acpiphp_slot *slot;
  622. list_for_each_entry(slot, &bridge->slots, node) {
  623. struct pci_bus *bus = slot->bus;
  624. struct pci_dev *dev, *tmp;
  625. mutex_lock(&slot->crit_sect);
  626. /* wake up all functions */
  627. if (get_slot_status(slot) == ACPI_STA_ALL) {
  628. /* remove stale devices if any */
  629. list_for_each_entry_safe(dev, tmp, &bus->devices,
  630. bus_list)
  631. if (PCI_SLOT(dev->devfn) == slot->device)
  632. trim_stale_devices(dev);
  633. /* configure all functions */
  634. enable_slot(slot);
  635. } else {
  636. disable_slot(slot);
  637. }
  638. mutex_unlock(&slot->crit_sect);
  639. }
  640. }
  641. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  642. {
  643. struct pci_dev *dev;
  644. list_for_each_entry(dev, &bus->devices, bus_list)
  645. pci_configure_slot(dev);
  646. }
  647. /*
  648. * Remove devices for which we could not assign resources, call
  649. * arch specific code to fix-up the bus
  650. */
  651. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  652. {
  653. struct pci_dev *dev, *tmp;
  654. int i;
  655. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  656. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  657. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  658. struct resource *res = &dev->resource[i];
  659. if ((res->flags & type_mask) && !res->start &&
  660. res->end) {
  661. /* Could not assign a required resources
  662. * for this device, remove it */
  663. pci_stop_and_remove_bus_device(dev);
  664. break;
  665. }
  666. }
  667. }
  668. }
  669. /*
  670. * ACPI event handlers
  671. */
  672. void acpiphp_check_host_bridge(acpi_handle handle)
  673. {
  674. struct acpiphp_bridge *bridge;
  675. bridge = acpiphp_handle_to_bridge(handle);
  676. if (bridge) {
  677. acpiphp_check_bridge(bridge);
  678. put_bridge(bridge);
  679. }
  680. }
  681. static void hotplug_event(acpi_handle handle, u32 type, void *data)
  682. {
  683. struct acpiphp_context *context = data;
  684. struct acpiphp_func *func = &context->func;
  685. struct acpiphp_bridge *bridge;
  686. char objname[64];
  687. struct acpi_buffer buffer = { .length = sizeof(objname),
  688. .pointer = objname };
  689. mutex_lock(&acpiphp_context_lock);
  690. bridge = context->bridge;
  691. if (bridge)
  692. get_bridge(bridge);
  693. mutex_unlock(&acpiphp_context_lock);
  694. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  695. switch (type) {
  696. case ACPI_NOTIFY_BUS_CHECK:
  697. /* bus re-enumerate */
  698. dbg("%s: Bus check notify on %s\n", __func__, objname);
  699. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  700. if (bridge) {
  701. acpiphp_check_bridge(bridge);
  702. } else {
  703. struct acpiphp_slot *slot = func->slot;
  704. mutex_lock(&slot->crit_sect);
  705. enable_slot(slot);
  706. mutex_unlock(&slot->crit_sect);
  707. }
  708. break;
  709. case ACPI_NOTIFY_DEVICE_CHECK:
  710. /* device check */
  711. dbg("%s: Device check notify on %s\n", __func__, objname);
  712. if (bridge)
  713. acpiphp_check_bridge(bridge);
  714. else
  715. acpiphp_check_bridge(func->parent);
  716. break;
  717. case ACPI_NOTIFY_EJECT_REQUEST:
  718. /* request device eject */
  719. dbg("%s: Device eject notify on %s\n", __func__, objname);
  720. acpiphp_disable_and_eject_slot(func->slot);
  721. break;
  722. }
  723. if (bridge)
  724. put_bridge(bridge);
  725. }
  726. static void hotplug_event_work(struct work_struct *work)
  727. {
  728. struct acpiphp_context *context;
  729. struct acpi_hp_work *hp_work;
  730. hp_work = container_of(work, struct acpi_hp_work, work);
  731. context = hp_work->context;
  732. acpi_scan_lock_acquire();
  733. hotplug_event(hp_work->handle, hp_work->type, context);
  734. acpi_scan_lock_release();
  735. kfree(hp_work); /* allocated in handle_hotplug_event() */
  736. put_bridge(context->func.parent);
  737. }
  738. /**
  739. * handle_hotplug_event - handle ACPI hotplug event
  740. * @handle: Notify()'ed acpi_handle
  741. * @type: Notify code
  742. * @data: pointer to acpiphp_context structure
  743. *
  744. * Handles ACPI event notification on slots.
  745. */
  746. static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
  747. {
  748. struct acpiphp_context *context;
  749. switch (type) {
  750. case ACPI_NOTIFY_BUS_CHECK:
  751. case ACPI_NOTIFY_DEVICE_CHECK:
  752. case ACPI_NOTIFY_EJECT_REQUEST:
  753. break;
  754. case ACPI_NOTIFY_DEVICE_WAKE:
  755. return;
  756. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  757. acpi_handle_err(handle, "Device cannot be configured due "
  758. "to a frequency mismatch\n");
  759. return;
  760. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  761. acpi_handle_err(handle, "Device cannot be configured due "
  762. "to a bus mode mismatch\n");
  763. return;
  764. case ACPI_NOTIFY_POWER_FAULT:
  765. acpi_handle_err(handle, "Device has suffered a power fault\n");
  766. return;
  767. default:
  768. acpi_handle_warn(handle, "Unsupported event type 0x%x\n", type);
  769. return;
  770. }
  771. mutex_lock(&acpiphp_context_lock);
  772. context = acpiphp_get_context(handle);
  773. if (context) {
  774. get_bridge(context->func.parent);
  775. acpiphp_put_context(context);
  776. alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
  777. }
  778. mutex_unlock(&acpiphp_context_lock);
  779. }
  780. /*
  781. * Create hotplug slots for the PCI bus.
  782. * It should always return 0 to avoid skipping following notifiers.
  783. */
  784. void acpiphp_enumerate_slots(struct pci_bus *bus)
  785. {
  786. struct acpiphp_bridge *bridge;
  787. acpi_handle handle;
  788. acpi_status status;
  789. if (acpiphp_disabled)
  790. return;
  791. handle = ACPI_HANDLE(bus->bridge);
  792. if (!handle)
  793. return;
  794. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  795. if (!bridge) {
  796. acpi_handle_err(handle, "No memory for bridge object\n");
  797. return;
  798. }
  799. INIT_LIST_HEAD(&bridge->slots);
  800. kref_init(&bridge->ref);
  801. bridge->pci_dev = pci_dev_get(bus->self);
  802. bridge->pci_bus = bus;
  803. /*
  804. * Grab a ref to the subordinate PCI bus in case the bus is
  805. * removed via PCI core logical hotplug. The ref pins the bus
  806. * (which we access during module unload).
  807. */
  808. get_device(&bus->dev);
  809. if (!pci_is_root_bus(bridge->pci_bus)) {
  810. struct acpiphp_context *context;
  811. /*
  812. * This bridge should have been registered as a hotplug function
  813. * under its parent, so the context has to be there. If not, we
  814. * are in deep goo.
  815. */
  816. mutex_lock(&acpiphp_context_lock);
  817. context = acpiphp_get_context(handle);
  818. if (WARN_ON(!context)) {
  819. mutex_unlock(&acpiphp_context_lock);
  820. put_device(&bus->dev);
  821. kfree(bridge);
  822. return;
  823. }
  824. bridge->context = context;
  825. context->bridge = bridge;
  826. /* Get a reference to the parent bridge. */
  827. get_bridge(context->func.parent);
  828. mutex_unlock(&acpiphp_context_lock);
  829. }
  830. /* must be added to the list prior to calling register_slot */
  831. mutex_lock(&bridge_mutex);
  832. list_add(&bridge->list, &bridge_list);
  833. mutex_unlock(&bridge_mutex);
  834. /* register all slot objects under this bridge */
  835. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  836. register_slot, NULL, bridge, NULL);
  837. if (ACPI_FAILURE(status)) {
  838. acpi_handle_err(handle, "failed to register slots\n");
  839. cleanup_bridge(bridge);
  840. put_bridge(bridge);
  841. }
  842. }
  843. /* Destroy hotplug slots associated with the PCI bus */
  844. void acpiphp_remove_slots(struct pci_bus *bus)
  845. {
  846. struct acpiphp_bridge *bridge;
  847. if (acpiphp_disabled)
  848. return;
  849. mutex_lock(&bridge_mutex);
  850. list_for_each_entry(bridge, &bridge_list, list)
  851. if (bridge->pci_bus == bus) {
  852. mutex_unlock(&bridge_mutex);
  853. cleanup_bridge(bridge);
  854. put_bridge(bridge);
  855. return;
  856. }
  857. mutex_unlock(&bridge_mutex);
  858. }
  859. /**
  860. * acpiphp_enable_slot - power on slot
  861. * @slot: ACPI PHP slot
  862. */
  863. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  864. {
  865. mutex_lock(&slot->crit_sect);
  866. /* configure all functions */
  867. if (!(slot->flags & SLOT_ENABLED))
  868. enable_slot(slot);
  869. mutex_unlock(&slot->crit_sect);
  870. return 0;
  871. }
  872. /**
  873. * acpiphp_disable_and_eject_slot - power off and eject slot
  874. * @slot: ACPI PHP slot
  875. */
  876. int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
  877. {
  878. struct acpiphp_func *func;
  879. int retval = 0;
  880. mutex_lock(&slot->crit_sect);
  881. /* unconfigure all functions */
  882. disable_slot(slot);
  883. list_for_each_entry(func, &slot->funcs, sibling)
  884. if (func->flags & FUNC_HAS_EJ0) {
  885. acpi_handle handle = func_to_handle(func);
  886. if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
  887. acpi_handle_err(handle, "_EJ0 failed\n");
  888. break;
  889. }
  890. mutex_unlock(&slot->crit_sect);
  891. return retval;
  892. }
  893. /*
  894. * slot enabled: 1
  895. * slot disabled: 0
  896. */
  897. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  898. {
  899. return (slot->flags & SLOT_ENABLED);
  900. }
  901. /*
  902. * latch open: 1
  903. * latch closed: 0
  904. */
  905. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  906. {
  907. return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
  908. }
  909. /*
  910. * adapter presence : 1
  911. * absence : 0
  912. */
  913. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  914. {
  915. return !!get_slot_status(slot);
  916. }