acpiphp_glue.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. #define pr_fmt(fmt) "acpiphp_glue: " fmt
  41. #include <linux/init.h>
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/pci.h>
  45. #include <linux/pci_hotplug.h>
  46. #include <linux/pci-acpi.h>
  47. #include <linux/pm_runtime.h>
  48. #include <linux/mutex.h>
  49. #include <linux/slab.h>
  50. #include <linux/acpi.h>
  51. #include "../pci.h"
  52. #include "acpiphp.h"
  53. static LIST_HEAD(bridge_list);
  54. static DEFINE_MUTEX(bridge_mutex);
  55. static DEFINE_MUTEX(acpiphp_context_lock);
  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. if (status != AE_NOT_FOUND)
  241. acpi_handle_warn(handle,
  242. "can't evaluate _ADR (%#x)\n", status);
  243. return AE_OK;
  244. }
  245. device = (adr >> 16) & 0xffff;
  246. function = adr & 0xffff;
  247. mutex_lock(&acpiphp_context_lock);
  248. context = acpiphp_init_context(handle);
  249. if (!context) {
  250. mutex_unlock(&acpiphp_context_lock);
  251. acpi_handle_err(handle, "No hotplug context\n");
  252. return AE_NOT_EXIST;
  253. }
  254. newfunc = &context->func;
  255. newfunc->function = function;
  256. newfunc->parent = bridge;
  257. mutex_unlock(&acpiphp_context_lock);
  258. if (acpi_has_method(handle, "_EJ0"))
  259. newfunc->flags = FUNC_HAS_EJ0;
  260. if (acpi_has_method(handle, "_STA"))
  261. newfunc->flags |= FUNC_HAS_STA;
  262. if (acpi_has_method(handle, "_DCK"))
  263. newfunc->flags |= FUNC_HAS_DCK;
  264. /* search for objects that share the same slot */
  265. list_for_each_entry(slot, &bridge->slots, node)
  266. if (slot->device == device)
  267. goto slot_found;
  268. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  269. if (!slot) {
  270. status = AE_NO_MEMORY;
  271. goto err;
  272. }
  273. slot->bus = bridge->pci_bus;
  274. slot->device = device;
  275. INIT_LIST_HEAD(&slot->funcs);
  276. mutex_init(&slot->crit_sect);
  277. list_add_tail(&slot->node, &bridge->slots);
  278. /* Register slots for ejectable functions only. */
  279. if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
  280. unsigned long long sun;
  281. int retval;
  282. bridge->nr_slots++;
  283. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  284. if (ACPI_FAILURE(status))
  285. sun = bridge->nr_slots;
  286. pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  287. sun, pci_domain_nr(pbus), pbus->number, device);
  288. retval = acpiphp_register_hotplug_slot(slot, sun);
  289. if (retval) {
  290. slot->slot = NULL;
  291. bridge->nr_slots--;
  292. if (retval == -EBUSY)
  293. pr_warn("Slot %llu already registered by another "
  294. "hotplug driver\n", sun);
  295. else
  296. pr_warn("acpiphp_register_hotplug_slot failed "
  297. "(err code = 0x%x)\n", retval);
  298. }
  299. /* Even if the slot registration fails, we can still use it. */
  300. }
  301. slot_found:
  302. newfunc->slot = slot;
  303. list_add_tail(&newfunc->sibling, &slot->funcs);
  304. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  305. &val, 60*1000))
  306. slot->flags |= SLOT_ENABLED;
  307. if (is_dock_device(handle)) {
  308. /* we don't want to call this device's _EJ0
  309. * because we want the dock notify handler
  310. * to call it after it calls _DCK
  311. */
  312. newfunc->flags &= ~FUNC_HAS_EJ0;
  313. if (register_hotplug_dock_device(handle,
  314. &acpiphp_dock_ops, context,
  315. acpiphp_dock_init, acpiphp_dock_release))
  316. pr_debug("failed to register dock device\n");
  317. }
  318. /* install notify handler */
  319. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  320. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  321. handle_hotplug_event,
  322. context);
  323. if (ACPI_FAILURE(status))
  324. acpi_handle_err(handle,
  325. "failed to install notify handler\n");
  326. }
  327. return AE_OK;
  328. err:
  329. mutex_lock(&acpiphp_context_lock);
  330. acpiphp_put_context(context);
  331. mutex_unlock(&acpiphp_context_lock);
  332. return status;
  333. }
  334. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  335. {
  336. struct acpiphp_context *context;
  337. struct acpiphp_bridge *bridge = NULL;
  338. mutex_lock(&acpiphp_context_lock);
  339. context = acpiphp_get_context(handle);
  340. if (context) {
  341. bridge = context->bridge;
  342. if (bridge)
  343. get_bridge(bridge);
  344. acpiphp_put_context(context);
  345. }
  346. mutex_unlock(&acpiphp_context_lock);
  347. return bridge;
  348. }
  349. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  350. {
  351. struct acpiphp_slot *slot;
  352. struct acpiphp_func *func;
  353. acpi_status status;
  354. list_for_each_entry(slot, &bridge->slots, node) {
  355. list_for_each_entry(func, &slot->funcs, sibling) {
  356. acpi_handle handle = func_to_handle(func);
  357. if (is_dock_device(handle))
  358. unregister_hotplug_dock_device(handle);
  359. if (!(func->flags & FUNC_HAS_DCK)) {
  360. status = acpi_remove_notify_handler(handle,
  361. ACPI_SYSTEM_NOTIFY,
  362. handle_hotplug_event);
  363. if (ACPI_FAILURE(status))
  364. pr_err("failed to remove notify handler\n");
  365. }
  366. }
  367. slot->flags |= SLOT_IS_GOING_AWAY;
  368. if (slot->slot)
  369. acpiphp_unregister_hotplug_slot(slot);
  370. }
  371. mutex_lock(&bridge_mutex);
  372. list_del(&bridge->list);
  373. mutex_unlock(&bridge_mutex);
  374. bridge->is_going_away = true;
  375. }
  376. /**
  377. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  378. * @bus: bus to start search with
  379. */
  380. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  381. {
  382. struct list_head *tmp;
  383. unsigned char max, n;
  384. /*
  385. * pci_bus_max_busnr will return the highest
  386. * reserved busnr for all these children.
  387. * that is equivalent to the bus->subordinate
  388. * value. We don't want to use the parent's
  389. * bus->subordinate value because it could have
  390. * padding in it.
  391. */
  392. max = bus->busn_res.start;
  393. list_for_each(tmp, &bus->children) {
  394. n = pci_bus_max_busnr(pci_bus_b(tmp));
  395. if (n > max)
  396. max = n;
  397. }
  398. return max;
  399. }
  400. /**
  401. * acpiphp_bus_trim - Trim device objects in an ACPI namespace subtree.
  402. * @handle: ACPI device object handle to start from.
  403. */
  404. static void acpiphp_bus_trim(acpi_handle handle)
  405. {
  406. struct acpi_device *adev = NULL;
  407. acpi_bus_get_device(handle, &adev);
  408. if (adev)
  409. acpi_bus_trim(adev);
  410. }
  411. /**
  412. * acpiphp_bus_add - Scan ACPI namespace subtree.
  413. * @handle: ACPI object handle to start the scan from.
  414. */
  415. static void acpiphp_bus_add(acpi_handle handle)
  416. {
  417. struct acpi_device *adev = NULL;
  418. acpi_bus_scan(handle);
  419. acpi_bus_get_device(handle, &adev);
  420. if (adev)
  421. acpi_device_set_power(adev, ACPI_STATE_D0);
  422. }
  423. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  424. {
  425. struct acpiphp_func *func;
  426. union acpi_object params[2];
  427. struct acpi_object_list arg_list;
  428. list_for_each_entry(func, &slot->funcs, sibling) {
  429. arg_list.count = 2;
  430. arg_list.pointer = params;
  431. params[0].type = ACPI_TYPE_INTEGER;
  432. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  433. params[1].type = ACPI_TYPE_INTEGER;
  434. params[1].integer.value = 1;
  435. /* _REG is optional, we don't care about if there is failure */
  436. acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
  437. NULL);
  438. }
  439. }
  440. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  441. {
  442. struct acpiphp_func *func;
  443. /* quirk, or pcie could set it already */
  444. if (dev->is_hotplug_bridge)
  445. return;
  446. list_for_each_entry(func, &slot->funcs, sibling) {
  447. if (PCI_FUNC(dev->devfn) == func->function) {
  448. dev->is_hotplug_bridge = 1;
  449. break;
  450. }
  451. }
  452. }
  453. static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
  454. {
  455. struct acpiphp_func *func;
  456. list_for_each_entry(func, &slot->funcs, sibling)
  457. acpiphp_bus_add(func_to_handle(func));
  458. return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
  459. }
  460. /**
  461. * enable_slot - enable, configure a slot
  462. * @slot: slot to be enabled
  463. *
  464. * This function should be called per *physical slot*,
  465. * not per each slot object in ACPI namespace.
  466. */
  467. static void __ref enable_slot(struct acpiphp_slot *slot)
  468. {
  469. struct pci_dev *dev;
  470. struct pci_bus *bus = slot->bus;
  471. struct acpiphp_func *func;
  472. int max, pass;
  473. LIST_HEAD(add_list);
  474. acpiphp_rescan_slot(slot);
  475. max = acpiphp_max_busnr(bus);
  476. for (pass = 0; pass < 2; pass++) {
  477. list_for_each_entry(dev, &bus->devices, bus_list) {
  478. if (PCI_SLOT(dev->devfn) != slot->device)
  479. continue;
  480. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  481. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  482. max = pci_scan_bridge(bus, dev, max, pass);
  483. if (pass && dev->subordinate) {
  484. check_hotplug_bridge(slot, dev);
  485. pcibios_resource_survey_bus(dev->subordinate);
  486. __pci_bus_size_bridges(dev->subordinate,
  487. &add_list);
  488. }
  489. }
  490. }
  491. }
  492. __pci_bus_assign_resources(bus, &add_list, NULL);
  493. acpiphp_sanitize_bus(bus);
  494. acpiphp_set_hpp_values(bus);
  495. acpiphp_set_acpi_region(slot);
  496. list_for_each_entry(dev, &bus->devices, bus_list) {
  497. /* Assume that newly added devices are powered on already. */
  498. if (!dev->is_added)
  499. dev->current_state = PCI_D0;
  500. }
  501. pci_bus_add_devices(bus);
  502. slot->flags |= SLOT_ENABLED;
  503. list_for_each_entry(func, &slot->funcs, sibling) {
  504. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  505. func->function));
  506. if (!dev) {
  507. /* Do not set SLOT_ENABLED flag if some funcs
  508. are not added. */
  509. slot->flags &= (~SLOT_ENABLED);
  510. continue;
  511. }
  512. }
  513. }
  514. /* return first device in slot, acquiring a reference on it */
  515. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  516. {
  517. struct pci_bus *bus = slot->bus;
  518. struct pci_dev *dev;
  519. struct pci_dev *ret = NULL;
  520. down_read(&pci_bus_sem);
  521. list_for_each_entry(dev, &bus->devices, bus_list)
  522. if (PCI_SLOT(dev->devfn) == slot->device) {
  523. ret = pci_dev_get(dev);
  524. break;
  525. }
  526. up_read(&pci_bus_sem);
  527. return ret;
  528. }
  529. /**
  530. * disable_slot - disable a slot
  531. * @slot: ACPI PHP slot
  532. */
  533. static void disable_slot(struct acpiphp_slot *slot)
  534. {
  535. struct acpiphp_func *func;
  536. struct pci_dev *pdev;
  537. /*
  538. * enable_slot() enumerates all functions in this device via
  539. * pci_scan_slot(), whether they have associated ACPI hotplug
  540. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  541. * here.
  542. */
  543. while ((pdev = dev_in_slot(slot))) {
  544. pci_stop_and_remove_bus_device(pdev);
  545. pci_dev_put(pdev);
  546. }
  547. list_for_each_entry(func, &slot->funcs, sibling)
  548. acpiphp_bus_trim(func_to_handle(func));
  549. slot->flags &= (~SLOT_ENABLED);
  550. }
  551. static bool acpiphp_no_hotplug(acpi_handle handle)
  552. {
  553. struct acpi_device *adev = NULL;
  554. acpi_bus_get_device(handle, &adev);
  555. return adev && adev->flags.no_hotplug;
  556. }
  557. static bool slot_no_hotplug(struct acpiphp_slot *slot)
  558. {
  559. struct acpiphp_func *func;
  560. list_for_each_entry(func, &slot->funcs, sibling)
  561. if (acpiphp_no_hotplug(func_to_handle(func)))
  562. return true;
  563. return false;
  564. }
  565. /**
  566. * get_slot_status - get ACPI slot status
  567. * @slot: ACPI PHP slot
  568. *
  569. * If a slot has _STA for each function and if any one of them
  570. * returned non-zero status, return it.
  571. *
  572. * If a slot doesn't have _STA and if any one of its functions'
  573. * configuration space is configured, return 0x0f as a _STA.
  574. *
  575. * Otherwise return 0.
  576. */
  577. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  578. {
  579. unsigned long long sta = 0;
  580. struct acpiphp_func *func;
  581. list_for_each_entry(func, &slot->funcs, sibling) {
  582. if (func->flags & FUNC_HAS_STA) {
  583. acpi_status status;
  584. status = acpi_evaluate_integer(func_to_handle(func),
  585. "_STA", NULL, &sta);
  586. if (ACPI_SUCCESS(status) && sta)
  587. break;
  588. } else {
  589. u32 dvid;
  590. pci_bus_read_config_dword(slot->bus,
  591. PCI_DEVFN(slot->device,
  592. func->function),
  593. PCI_VENDOR_ID, &dvid);
  594. if (dvid != 0xffffffff) {
  595. sta = ACPI_STA_ALL;
  596. break;
  597. }
  598. }
  599. }
  600. return (unsigned int)sta;
  601. }
  602. /**
  603. * trim_stale_devices - remove PCI devices that are not responding.
  604. * @dev: PCI device to start walking the hierarchy from.
  605. */
  606. static void trim_stale_devices(struct pci_dev *dev)
  607. {
  608. acpi_handle handle = ACPI_HANDLE(&dev->dev);
  609. struct pci_bus *bus = dev->subordinate;
  610. bool alive = false;
  611. if (handle) {
  612. acpi_status status;
  613. unsigned long long sta;
  614. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  615. alive = (ACPI_SUCCESS(status) && sta == ACPI_STA_ALL)
  616. || acpiphp_no_hotplug(handle);
  617. }
  618. if (!alive) {
  619. u32 v;
  620. /* Check if the device responds. */
  621. alive = pci_bus_read_dev_vendor_id(dev->bus, dev->devfn, &v, 0);
  622. }
  623. if (!alive) {
  624. pci_stop_and_remove_bus_device(dev);
  625. if (handle)
  626. acpiphp_bus_trim(handle);
  627. } else if (bus) {
  628. struct pci_dev *child, *tmp;
  629. /* The device is a bridge. so check the bus below it. */
  630. pm_runtime_get_sync(&dev->dev);
  631. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  632. trim_stale_devices(child);
  633. pm_runtime_put(&dev->dev);
  634. }
  635. }
  636. /**
  637. * acpiphp_check_bridge - re-enumerate devices
  638. * @bridge: where to begin re-enumeration
  639. *
  640. * Iterate over all slots under this bridge and make sure that if a
  641. * card is present they are enabled, and if not they are disabled.
  642. */
  643. static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  644. {
  645. struct acpiphp_slot *slot;
  646. /* Bail out if the bridge is going away. */
  647. if (bridge->is_going_away)
  648. return;
  649. list_for_each_entry(slot, &bridge->slots, node) {
  650. struct pci_bus *bus = slot->bus;
  651. struct pci_dev *dev, *tmp;
  652. mutex_lock(&slot->crit_sect);
  653. if (slot_no_hotplug(slot)) {
  654. ; /* do nothing */
  655. } else if (get_slot_status(slot) == ACPI_STA_ALL) {
  656. /* remove stale devices if any */
  657. list_for_each_entry_safe(dev, tmp, &bus->devices,
  658. bus_list)
  659. if (PCI_SLOT(dev->devfn) == slot->device)
  660. trim_stale_devices(dev);
  661. /* configure all functions */
  662. enable_slot(slot);
  663. } else {
  664. disable_slot(slot);
  665. }
  666. mutex_unlock(&slot->crit_sect);
  667. }
  668. }
  669. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  670. {
  671. struct pci_dev *dev;
  672. list_for_each_entry(dev, &bus->devices, bus_list)
  673. pci_configure_slot(dev);
  674. }
  675. /*
  676. * Remove devices for which we could not assign resources, call
  677. * arch specific code to fix-up the bus
  678. */
  679. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  680. {
  681. struct pci_dev *dev, *tmp;
  682. int i;
  683. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  684. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  685. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  686. struct resource *res = &dev->resource[i];
  687. if ((res->flags & type_mask) && !res->start &&
  688. res->end) {
  689. /* Could not assign a required resources
  690. * for this device, remove it */
  691. pci_stop_and_remove_bus_device(dev);
  692. break;
  693. }
  694. }
  695. }
  696. }
  697. /*
  698. * ACPI event handlers
  699. */
  700. void acpiphp_check_host_bridge(acpi_handle handle)
  701. {
  702. struct acpiphp_bridge *bridge;
  703. bridge = acpiphp_handle_to_bridge(handle);
  704. if (bridge) {
  705. acpiphp_check_bridge(bridge);
  706. put_bridge(bridge);
  707. }
  708. }
  709. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
  710. static void hotplug_event(acpi_handle handle, u32 type, void *data)
  711. {
  712. struct acpiphp_context *context = data;
  713. struct acpiphp_func *func = &context->func;
  714. struct acpiphp_bridge *bridge;
  715. char objname[64];
  716. struct acpi_buffer buffer = { .length = sizeof(objname),
  717. .pointer = objname };
  718. mutex_lock(&acpiphp_context_lock);
  719. bridge = context->bridge;
  720. if (bridge)
  721. get_bridge(bridge);
  722. mutex_unlock(&acpiphp_context_lock);
  723. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  724. switch (type) {
  725. case ACPI_NOTIFY_BUS_CHECK:
  726. /* bus re-enumerate */
  727. pr_debug("%s: Bus check notify on %s\n", __func__, objname);
  728. pr_debug("%s: re-enumerating slots under %s\n",
  729. __func__, objname);
  730. if (bridge) {
  731. acpiphp_check_bridge(bridge);
  732. } else {
  733. struct acpiphp_slot *slot = func->slot;
  734. if (slot->flags & SLOT_IS_GOING_AWAY)
  735. break;
  736. mutex_lock(&slot->crit_sect);
  737. enable_slot(slot);
  738. mutex_unlock(&slot->crit_sect);
  739. }
  740. break;
  741. case ACPI_NOTIFY_DEVICE_CHECK:
  742. /* device check */
  743. pr_debug("%s: Device check notify on %s\n", __func__, objname);
  744. if (bridge) {
  745. acpiphp_check_bridge(bridge);
  746. } else {
  747. struct acpiphp_slot *slot = func->slot;
  748. int ret;
  749. if (slot->flags & SLOT_IS_GOING_AWAY)
  750. break;
  751. /*
  752. * Check if anything has changed in the slot and rescan
  753. * from the parent if that's the case.
  754. */
  755. mutex_lock(&slot->crit_sect);
  756. ret = acpiphp_rescan_slot(slot);
  757. mutex_unlock(&slot->crit_sect);
  758. if (ret)
  759. acpiphp_check_bridge(func->parent);
  760. }
  761. break;
  762. case ACPI_NOTIFY_EJECT_REQUEST:
  763. /* request device eject */
  764. pr_debug("%s: Device eject notify on %s\n", __func__, objname);
  765. acpiphp_disable_and_eject_slot(func->slot);
  766. break;
  767. }
  768. if (bridge)
  769. put_bridge(bridge);
  770. }
  771. static void hotplug_event_work(void *data, u32 type)
  772. {
  773. struct acpiphp_context *context = data;
  774. acpi_handle handle = context->handle;
  775. acpi_scan_lock_acquire();
  776. pci_lock_rescan_remove();
  777. hotplug_event(handle, type, context);
  778. pci_unlock_rescan_remove();
  779. acpi_scan_lock_release();
  780. acpi_evaluate_hotplug_ost(handle, type, ACPI_OST_SC_SUCCESS, NULL);
  781. put_bridge(context->func.parent);
  782. }
  783. /**
  784. * handle_hotplug_event - handle ACPI hotplug event
  785. * @handle: Notify()'ed acpi_handle
  786. * @type: Notify code
  787. * @data: pointer to acpiphp_context structure
  788. *
  789. * Handles ACPI event notification on slots.
  790. */
  791. static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
  792. {
  793. struct acpiphp_context *context;
  794. u32 ost_code = ACPI_OST_SC_SUCCESS;
  795. switch (type) {
  796. case ACPI_NOTIFY_BUS_CHECK:
  797. case ACPI_NOTIFY_DEVICE_CHECK:
  798. break;
  799. case ACPI_NOTIFY_EJECT_REQUEST:
  800. ost_code = ACPI_OST_SC_EJECT_IN_PROGRESS;
  801. acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
  802. break;
  803. case ACPI_NOTIFY_DEVICE_WAKE:
  804. return;
  805. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  806. acpi_handle_err(handle, "Device cannot be configured due "
  807. "to a frequency mismatch\n");
  808. goto out;
  809. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  810. acpi_handle_err(handle, "Device cannot be configured due "
  811. "to a bus mode mismatch\n");
  812. goto out;
  813. case ACPI_NOTIFY_POWER_FAULT:
  814. acpi_handle_err(handle, "Device has suffered a power fault\n");
  815. goto out;
  816. default:
  817. acpi_handle_warn(handle, "Unsupported event type 0x%x\n", type);
  818. ost_code = ACPI_OST_SC_UNRECOGNIZED_NOTIFY;
  819. goto out;
  820. }
  821. mutex_lock(&acpiphp_context_lock);
  822. context = acpiphp_get_context(handle);
  823. if (context && !WARN_ON(context->handle != handle)) {
  824. get_bridge(context->func.parent);
  825. acpiphp_put_context(context);
  826. acpi_hotplug_execute(hotplug_event_work, context, type);
  827. mutex_unlock(&acpiphp_context_lock);
  828. return;
  829. }
  830. mutex_unlock(&acpiphp_context_lock);
  831. ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
  832. out:
  833. acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
  834. }
  835. /*
  836. * Create hotplug slots for the PCI bus.
  837. * It should always return 0 to avoid skipping following notifiers.
  838. */
  839. void acpiphp_enumerate_slots(struct pci_bus *bus)
  840. {
  841. struct acpiphp_bridge *bridge;
  842. acpi_handle handle;
  843. acpi_status status;
  844. if (acpiphp_disabled)
  845. return;
  846. handle = ACPI_HANDLE(bus->bridge);
  847. if (!handle)
  848. return;
  849. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  850. if (!bridge) {
  851. acpi_handle_err(handle, "No memory for bridge object\n");
  852. return;
  853. }
  854. INIT_LIST_HEAD(&bridge->slots);
  855. kref_init(&bridge->ref);
  856. bridge->pci_dev = pci_dev_get(bus->self);
  857. bridge->pci_bus = bus;
  858. /*
  859. * Grab a ref to the subordinate PCI bus in case the bus is
  860. * removed via PCI core logical hotplug. The ref pins the bus
  861. * (which we access during module unload).
  862. */
  863. get_device(&bus->dev);
  864. if (!pci_is_root_bus(bridge->pci_bus)) {
  865. struct acpiphp_context *context;
  866. /*
  867. * This bridge should have been registered as a hotplug function
  868. * under its parent, so the context should be there, unless the
  869. * parent is going to be handled by pciehp, in which case this
  870. * bridge is not interesting to us either.
  871. */
  872. mutex_lock(&acpiphp_context_lock);
  873. context = acpiphp_get_context(handle);
  874. if (!context) {
  875. mutex_unlock(&acpiphp_context_lock);
  876. put_device(&bus->dev);
  877. pci_dev_put(bridge->pci_dev);
  878. kfree(bridge);
  879. return;
  880. }
  881. bridge->context = context;
  882. context->bridge = bridge;
  883. /* Get a reference to the parent bridge. */
  884. get_bridge(context->func.parent);
  885. mutex_unlock(&acpiphp_context_lock);
  886. }
  887. /* must be added to the list prior to calling register_slot */
  888. mutex_lock(&bridge_mutex);
  889. list_add(&bridge->list, &bridge_list);
  890. mutex_unlock(&bridge_mutex);
  891. /* register all slot objects under this bridge */
  892. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  893. register_slot, NULL, bridge, NULL);
  894. if (ACPI_FAILURE(status)) {
  895. acpi_handle_err(handle, "failed to register slots\n");
  896. cleanup_bridge(bridge);
  897. put_bridge(bridge);
  898. }
  899. }
  900. /* Destroy hotplug slots associated with the PCI bus */
  901. void acpiphp_remove_slots(struct pci_bus *bus)
  902. {
  903. struct acpiphp_bridge *bridge;
  904. if (acpiphp_disabled)
  905. return;
  906. mutex_lock(&bridge_mutex);
  907. list_for_each_entry(bridge, &bridge_list, list)
  908. if (bridge->pci_bus == bus) {
  909. mutex_unlock(&bridge_mutex);
  910. cleanup_bridge(bridge);
  911. put_bridge(bridge);
  912. return;
  913. }
  914. mutex_unlock(&bridge_mutex);
  915. }
  916. /**
  917. * acpiphp_enable_slot - power on slot
  918. * @slot: ACPI PHP slot
  919. */
  920. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  921. {
  922. pci_lock_rescan_remove();
  923. if (slot->flags & SLOT_IS_GOING_AWAY)
  924. return -ENODEV;
  925. mutex_lock(&slot->crit_sect);
  926. /* configure all functions */
  927. if (!(slot->flags & SLOT_ENABLED))
  928. enable_slot(slot);
  929. mutex_unlock(&slot->crit_sect);
  930. pci_unlock_rescan_remove();
  931. return 0;
  932. }
  933. /**
  934. * acpiphp_disable_and_eject_slot - power off and eject slot
  935. * @slot: ACPI PHP slot
  936. */
  937. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
  938. {
  939. struct acpiphp_func *func;
  940. if (slot->flags & SLOT_IS_GOING_AWAY)
  941. return -ENODEV;
  942. mutex_lock(&slot->crit_sect);
  943. /* unconfigure all functions */
  944. disable_slot(slot);
  945. list_for_each_entry(func, &slot->funcs, sibling)
  946. if (func->flags & FUNC_HAS_EJ0) {
  947. acpi_handle handle = func_to_handle(func);
  948. if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
  949. acpi_handle_err(handle, "_EJ0 failed\n");
  950. break;
  951. }
  952. mutex_unlock(&slot->crit_sect);
  953. return 0;
  954. }
  955. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  956. {
  957. int ret;
  958. pci_lock_rescan_remove();
  959. ret = acpiphp_disable_and_eject_slot(slot);
  960. pci_unlock_rescan_remove();
  961. return ret;
  962. }
  963. /*
  964. * slot enabled: 1
  965. * slot disabled: 0
  966. */
  967. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  968. {
  969. return (slot->flags & SLOT_ENABLED);
  970. }
  971. /*
  972. * latch open: 1
  973. * latch closed: 0
  974. */
  975. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  976. {
  977. return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
  978. }
  979. /*
  980. * adapter presence : 1
  981. * absence : 0
  982. */
  983. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  984. {
  985. return !!get_slot_status(slot);
  986. }