acpiphp_glue.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ACPI PCI HotPlug glue functions to ACPI CA subsystem
  4. *
  5. * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
  6. * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
  7. * Copyright (C) 2002,2003 NEC Corporation
  8. * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
  9. * Copyright (C) 2003-2005 Hewlett Packard
  10. * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
  11. * Copyright (C) 2005 Intel Corporation
  12. *
  13. * All rights reserved.
  14. *
  15. * Send feedback to <kristen.c.accardi@intel.com>
  16. *
  17. */
  18. /*
  19. * Lifetime rules for pci_dev:
  20. * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
  21. * when the bridge is scanned and it loses a refcount when the bridge
  22. * is removed.
  23. * - When a P2P bridge is present, we elevate the refcount on the subordinate
  24. * bus. It loses the refcount when the the driver unloads.
  25. */
  26. #define pr_fmt(fmt) "acpiphp_glue: " fmt
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/pci.h>
  30. #include <linux/pci_hotplug.h>
  31. #include <linux/pci-acpi.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/mutex.h>
  34. #include <linux/slab.h>
  35. #include <linux/acpi.h>
  36. #include "../pci.h"
  37. #include "acpiphp.h"
  38. static LIST_HEAD(bridge_list);
  39. static DEFINE_MUTEX(bridge_mutex);
  40. static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type);
  41. static void acpiphp_post_dock_fixup(struct acpi_device *adev);
  42. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  43. static void hotplug_event(u32 type, struct acpiphp_context *context);
  44. static void free_bridge(struct kref *kref);
  45. /**
  46. * acpiphp_init_context - Create hotplug context and grab a reference to it.
  47. * @adev: ACPI device object to create the context for.
  48. *
  49. * Call under acpi_hp_context_lock.
  50. */
  51. static struct acpiphp_context *acpiphp_init_context(struct acpi_device *adev)
  52. {
  53. struct acpiphp_context *context;
  54. context = kzalloc(sizeof(*context), GFP_KERNEL);
  55. if (!context)
  56. return NULL;
  57. context->refcount = 1;
  58. context->hp.notify = acpiphp_hotplug_notify;
  59. context->hp.fixup = acpiphp_post_dock_fixup;
  60. acpi_set_hp_context(adev, &context->hp);
  61. return context;
  62. }
  63. /**
  64. * acpiphp_get_context - Get hotplug context and grab a reference to it.
  65. * @adev: ACPI device object to get the context for.
  66. *
  67. * Call under acpi_hp_context_lock.
  68. */
  69. static struct acpiphp_context *acpiphp_get_context(struct acpi_device *adev)
  70. {
  71. struct acpiphp_context *context;
  72. if (!adev->hp)
  73. return NULL;
  74. context = to_acpiphp_context(adev->hp);
  75. context->refcount++;
  76. return context;
  77. }
  78. /**
  79. * acpiphp_put_context - Drop a reference to ACPI hotplug context.
  80. * @context: ACPI hotplug context to drop a reference to.
  81. *
  82. * The context object is removed if there are no more references to it.
  83. *
  84. * Call under acpi_hp_context_lock.
  85. */
  86. static void acpiphp_put_context(struct acpiphp_context *context)
  87. {
  88. if (--context->refcount)
  89. return;
  90. WARN_ON(context->bridge);
  91. context->hp.self->hp = NULL;
  92. kfree(context);
  93. }
  94. static inline void get_bridge(struct acpiphp_bridge *bridge)
  95. {
  96. kref_get(&bridge->ref);
  97. }
  98. static inline void put_bridge(struct acpiphp_bridge *bridge)
  99. {
  100. kref_put(&bridge->ref, free_bridge);
  101. }
  102. static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
  103. {
  104. struct acpiphp_context *context;
  105. acpi_lock_hp_context();
  106. context = acpiphp_get_context(adev);
  107. if (!context || context->func.parent->is_going_away) {
  108. acpi_unlock_hp_context();
  109. return NULL;
  110. }
  111. get_bridge(context->func.parent);
  112. acpiphp_put_context(context);
  113. acpi_unlock_hp_context();
  114. return context;
  115. }
  116. static void acpiphp_let_context_go(struct acpiphp_context *context)
  117. {
  118. put_bridge(context->func.parent);
  119. }
  120. static void free_bridge(struct kref *kref)
  121. {
  122. struct acpiphp_context *context;
  123. struct acpiphp_bridge *bridge;
  124. struct acpiphp_slot *slot, *next;
  125. struct acpiphp_func *func, *tmp;
  126. acpi_lock_hp_context();
  127. bridge = container_of(kref, struct acpiphp_bridge, ref);
  128. list_for_each_entry_safe(slot, next, &bridge->slots, node) {
  129. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
  130. acpiphp_put_context(func_to_context(func));
  131. kfree(slot);
  132. }
  133. context = bridge->context;
  134. /* Root bridges will not have hotplug context. */
  135. if (context) {
  136. /* Release the reference taken by acpiphp_enumerate_slots(). */
  137. put_bridge(context->func.parent);
  138. context->bridge = NULL;
  139. acpiphp_put_context(context);
  140. }
  141. put_device(&bridge->pci_bus->dev);
  142. pci_dev_put(bridge->pci_dev);
  143. kfree(bridge);
  144. acpi_unlock_hp_context();
  145. }
  146. /**
  147. * acpiphp_post_dock_fixup - Post-dock fixups for PCI devices.
  148. * @adev: ACPI device object corresponding to a PCI device.
  149. *
  150. * TBD - figure out a way to only call fixups for systems that require them.
  151. */
  152. static void acpiphp_post_dock_fixup(struct acpi_device *adev)
  153. {
  154. struct acpiphp_context *context = acpiphp_grab_context(adev);
  155. struct pci_bus *bus;
  156. u32 buses;
  157. if (!context)
  158. return;
  159. bus = context->func.slot->bus;
  160. if (!bus->self)
  161. goto out;
  162. /* fixup bad _DCK function that rewrites
  163. * secondary bridge on slot
  164. */
  165. pci_read_config_dword(bus->self, PCI_PRIMARY_BUS, &buses);
  166. if (((buses >> 8) & 0xff) != bus->busn_res.start) {
  167. buses = (buses & 0xff000000)
  168. | ((unsigned int)(bus->primary) << 0)
  169. | ((unsigned int)(bus->busn_res.start) << 8)
  170. | ((unsigned int)(bus->busn_res.end) << 16);
  171. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  172. }
  173. out:
  174. acpiphp_let_context_go(context);
  175. }
  176. /**
  177. * acpiphp_add_context - Add ACPIPHP context to an ACPI device object.
  178. * @handle: ACPI handle of the object to add a context to.
  179. * @lvl: Not used.
  180. * @data: The object's parent ACPIPHP bridge.
  181. * @rv: Not used.
  182. */
  183. static acpi_status acpiphp_add_context(acpi_handle handle, u32 lvl, void *data,
  184. void **rv)
  185. {
  186. struct acpiphp_bridge *bridge = data;
  187. struct acpiphp_context *context;
  188. struct acpi_device *adev;
  189. struct acpiphp_slot *slot;
  190. struct acpiphp_func *newfunc;
  191. acpi_status status = AE_OK;
  192. unsigned long long adr;
  193. int device, function;
  194. struct pci_bus *pbus = bridge->pci_bus;
  195. struct pci_dev *pdev = bridge->pci_dev;
  196. u32 val;
  197. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  198. if (ACPI_FAILURE(status)) {
  199. if (status != AE_NOT_FOUND)
  200. acpi_handle_warn(handle,
  201. "can't evaluate _ADR (%#x)\n", status);
  202. return AE_OK;
  203. }
  204. if (acpi_bus_get_device(handle, &adev))
  205. return AE_OK;
  206. device = (adr >> 16) & 0xffff;
  207. function = adr & 0xffff;
  208. acpi_lock_hp_context();
  209. context = acpiphp_init_context(adev);
  210. if (!context) {
  211. acpi_unlock_hp_context();
  212. acpi_handle_err(handle, "No hotplug context\n");
  213. return AE_NOT_EXIST;
  214. }
  215. newfunc = &context->func;
  216. newfunc->function = function;
  217. newfunc->parent = bridge;
  218. acpi_unlock_hp_context();
  219. /*
  220. * If this is a dock device, its _EJ0 should be executed by the dock
  221. * notify handler after calling _DCK.
  222. */
  223. if (!is_dock_device(adev) && acpi_has_method(handle, "_EJ0"))
  224. newfunc->flags = FUNC_HAS_EJ0;
  225. if (acpi_has_method(handle, "_STA"))
  226. newfunc->flags |= FUNC_HAS_STA;
  227. /* search for objects that share the same slot */
  228. list_for_each_entry(slot, &bridge->slots, node)
  229. if (slot->device == device)
  230. goto slot_found;
  231. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  232. if (!slot) {
  233. acpi_lock_hp_context();
  234. acpiphp_put_context(context);
  235. acpi_unlock_hp_context();
  236. return AE_NO_MEMORY;
  237. }
  238. slot->bus = bridge->pci_bus;
  239. slot->device = device;
  240. INIT_LIST_HEAD(&slot->funcs);
  241. list_add_tail(&slot->node, &bridge->slots);
  242. /*
  243. * Expose slots to user space for functions that have _EJ0 or _RMV or
  244. * are located in dock stations. Do not expose them for devices handled
  245. * by the native PCIe hotplug (PCIeHP), becuase that code is supposed to
  246. * expose slots to user space in those cases.
  247. */
  248. if ((acpi_pci_check_ejectable(pbus, handle) || is_dock_device(adev))
  249. && !(pdev && pdev->is_hotplug_bridge && pciehp_is_native(pdev))) {
  250. unsigned long long sun;
  251. int retval;
  252. bridge->nr_slots++;
  253. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  254. if (ACPI_FAILURE(status))
  255. sun = bridge->nr_slots;
  256. pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  257. sun, pci_domain_nr(pbus), pbus->number, device);
  258. retval = acpiphp_register_hotplug_slot(slot, sun);
  259. if (retval) {
  260. slot->slot = NULL;
  261. bridge->nr_slots--;
  262. if (retval == -EBUSY)
  263. pr_warn("Slot %llu already registered by another hotplug driver\n", sun);
  264. else
  265. pr_warn("acpiphp_register_hotplug_slot failed (err code = 0x%x)\n", retval);
  266. }
  267. /* Even if the slot registration fails, we can still use it. */
  268. }
  269. slot_found:
  270. newfunc->slot = slot;
  271. list_add_tail(&newfunc->sibling, &slot->funcs);
  272. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  273. &val, 60*1000))
  274. slot->flags |= SLOT_ENABLED;
  275. return AE_OK;
  276. }
  277. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  278. {
  279. struct acpiphp_slot *slot;
  280. struct acpiphp_func *func;
  281. list_for_each_entry(slot, &bridge->slots, node) {
  282. list_for_each_entry(func, &slot->funcs, sibling) {
  283. struct acpi_device *adev = func_to_acpi_device(func);
  284. acpi_lock_hp_context();
  285. adev->hp->notify = NULL;
  286. adev->hp->fixup = NULL;
  287. acpi_unlock_hp_context();
  288. }
  289. slot->flags |= SLOT_IS_GOING_AWAY;
  290. if (slot->slot)
  291. acpiphp_unregister_hotplug_slot(slot);
  292. }
  293. mutex_lock(&bridge_mutex);
  294. list_del(&bridge->list);
  295. mutex_unlock(&bridge_mutex);
  296. acpi_lock_hp_context();
  297. bridge->is_going_away = true;
  298. acpi_unlock_hp_context();
  299. }
  300. /**
  301. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  302. * @bus: bus to start search with
  303. */
  304. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  305. {
  306. struct pci_bus *tmp;
  307. unsigned char max, n;
  308. /*
  309. * pci_bus_max_busnr will return the highest
  310. * reserved busnr for all these children.
  311. * that is equivalent to the bus->subordinate
  312. * value. We don't want to use the parent's
  313. * bus->subordinate value because it could have
  314. * padding in it.
  315. */
  316. max = bus->busn_res.start;
  317. list_for_each_entry(tmp, &bus->children, node) {
  318. n = pci_bus_max_busnr(tmp);
  319. if (n > max)
  320. max = n;
  321. }
  322. return max;
  323. }
  324. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  325. {
  326. struct acpiphp_func *func;
  327. union acpi_object params[2];
  328. struct acpi_object_list arg_list;
  329. list_for_each_entry(func, &slot->funcs, sibling) {
  330. arg_list.count = 2;
  331. arg_list.pointer = params;
  332. params[0].type = ACPI_TYPE_INTEGER;
  333. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  334. params[1].type = ACPI_TYPE_INTEGER;
  335. params[1].integer.value = 1;
  336. /* _REG is optional, we don't care about if there is failure */
  337. acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
  338. NULL);
  339. }
  340. }
  341. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  342. {
  343. struct acpiphp_func *func;
  344. /* quirk, or pcie could set it already */
  345. if (dev->is_hotplug_bridge)
  346. return;
  347. list_for_each_entry(func, &slot->funcs, sibling) {
  348. if (PCI_FUNC(dev->devfn) == func->function) {
  349. dev->is_hotplug_bridge = 1;
  350. break;
  351. }
  352. }
  353. }
  354. static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
  355. {
  356. struct acpiphp_func *func;
  357. list_for_each_entry(func, &slot->funcs, sibling) {
  358. struct acpi_device *adev = func_to_acpi_device(func);
  359. acpi_bus_scan(adev->handle);
  360. if (acpi_device_enumerated(adev))
  361. acpi_device_set_power(adev, ACPI_STATE_D0);
  362. }
  363. return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
  364. }
  365. /**
  366. * enable_slot - enable, configure a slot
  367. * @slot: slot to be enabled
  368. *
  369. * This function should be called per *physical slot*,
  370. * not per each slot object in ACPI namespace.
  371. */
  372. static void enable_slot(struct acpiphp_slot *slot)
  373. {
  374. struct pci_dev *dev;
  375. struct pci_bus *bus = slot->bus;
  376. struct acpiphp_func *func;
  377. int max, pass;
  378. LIST_HEAD(add_list);
  379. acpiphp_rescan_slot(slot);
  380. max = acpiphp_max_busnr(bus);
  381. for (pass = 0; pass < 2; pass++) {
  382. for_each_pci_bridge(dev, bus) {
  383. if (PCI_SLOT(dev->devfn) != slot->device)
  384. continue;
  385. max = pci_scan_bridge(bus, dev, max, pass);
  386. if (pass && dev->subordinate) {
  387. check_hotplug_bridge(slot, dev);
  388. pcibios_resource_survey_bus(dev->subordinate);
  389. __pci_bus_size_bridges(dev->subordinate, &add_list);
  390. }
  391. }
  392. }
  393. __pci_bus_assign_resources(bus, &add_list, NULL);
  394. acpiphp_sanitize_bus(bus);
  395. pcie_bus_configure_settings(bus);
  396. acpiphp_set_acpi_region(slot);
  397. list_for_each_entry(dev, &bus->devices, bus_list) {
  398. /* Assume that newly added devices are powered on already. */
  399. if (!dev->is_added)
  400. dev->current_state = PCI_D0;
  401. }
  402. pci_bus_add_devices(bus);
  403. slot->flags |= SLOT_ENABLED;
  404. list_for_each_entry(func, &slot->funcs, sibling) {
  405. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  406. func->function));
  407. if (!dev) {
  408. /* Do not set SLOT_ENABLED flag if some funcs
  409. are not added. */
  410. slot->flags &= (~SLOT_ENABLED);
  411. continue;
  412. }
  413. }
  414. }
  415. /**
  416. * disable_slot - disable a slot
  417. * @slot: ACPI PHP slot
  418. */
  419. static void disable_slot(struct acpiphp_slot *slot)
  420. {
  421. struct pci_bus *bus = slot->bus;
  422. struct pci_dev *dev, *prev;
  423. struct acpiphp_func *func;
  424. /*
  425. * enable_slot() enumerates all functions in this device via
  426. * pci_scan_slot(), whether they have associated ACPI hotplug
  427. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  428. * here.
  429. */
  430. list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
  431. if (PCI_SLOT(dev->devfn) == slot->device)
  432. pci_stop_and_remove_bus_device(dev);
  433. list_for_each_entry(func, &slot->funcs, sibling)
  434. acpi_bus_trim(func_to_acpi_device(func));
  435. slot->flags &= (~SLOT_ENABLED);
  436. }
  437. static bool slot_no_hotplug(struct acpiphp_slot *slot)
  438. {
  439. struct pci_bus *bus = slot->bus;
  440. struct pci_dev *dev;
  441. list_for_each_entry(dev, &bus->devices, bus_list) {
  442. if (PCI_SLOT(dev->devfn) == slot->device && dev->ignore_hotplug)
  443. return true;
  444. }
  445. return false;
  446. }
  447. /**
  448. * get_slot_status - get ACPI slot status
  449. * @slot: ACPI PHP slot
  450. *
  451. * If a slot has _STA for each function and if any one of them
  452. * returned non-zero status, return it.
  453. *
  454. * If a slot doesn't have _STA and if any one of its functions'
  455. * configuration space is configured, return 0x0f as a _STA.
  456. *
  457. * Otherwise return 0.
  458. */
  459. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  460. {
  461. unsigned long long sta = 0;
  462. struct acpiphp_func *func;
  463. u32 dvid;
  464. list_for_each_entry(func, &slot->funcs, sibling) {
  465. if (func->flags & FUNC_HAS_STA) {
  466. acpi_status status;
  467. status = acpi_evaluate_integer(func_to_handle(func),
  468. "_STA", NULL, &sta);
  469. if (ACPI_SUCCESS(status) && sta)
  470. break;
  471. } else {
  472. if (pci_bus_read_dev_vendor_id(slot->bus,
  473. PCI_DEVFN(slot->device, func->function),
  474. &dvid, 0)) {
  475. sta = ACPI_STA_ALL;
  476. break;
  477. }
  478. }
  479. }
  480. if (!sta) {
  481. /*
  482. * Check for the slot itself since it may be that the
  483. * ACPI slot is a device below PCIe upstream port so in
  484. * that case it may not even be reachable yet.
  485. */
  486. if (pci_bus_read_dev_vendor_id(slot->bus,
  487. PCI_DEVFN(slot->device, 0), &dvid, 0)) {
  488. sta = ACPI_STA_ALL;
  489. }
  490. }
  491. return (unsigned int)sta;
  492. }
  493. static inline bool device_status_valid(unsigned int sta)
  494. {
  495. /*
  496. * ACPI spec says that _STA may return bit 0 clear with bit 3 set
  497. * if the device is valid but does not require a device driver to be
  498. * loaded (Section 6.3.7 of ACPI 5.0A).
  499. */
  500. unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING;
  501. return (sta & mask) == mask;
  502. }
  503. /**
  504. * trim_stale_devices - remove PCI devices that are not responding.
  505. * @dev: PCI device to start walking the hierarchy from.
  506. */
  507. static void trim_stale_devices(struct pci_dev *dev)
  508. {
  509. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  510. struct pci_bus *bus = dev->subordinate;
  511. bool alive = dev->ignore_hotplug;
  512. if (adev) {
  513. acpi_status status;
  514. unsigned long long sta;
  515. status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
  516. alive = alive || (ACPI_SUCCESS(status) && device_status_valid(sta));
  517. }
  518. if (!alive)
  519. alive = pci_device_is_present(dev);
  520. if (!alive) {
  521. pci_stop_and_remove_bus_device(dev);
  522. if (adev)
  523. acpi_bus_trim(adev);
  524. } else if (bus) {
  525. struct pci_dev *child, *tmp;
  526. /* The device is a bridge. so check the bus below it. */
  527. pm_runtime_get_sync(&dev->dev);
  528. list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
  529. trim_stale_devices(child);
  530. pm_runtime_put(&dev->dev);
  531. }
  532. }
  533. /**
  534. * acpiphp_check_bridge - re-enumerate devices
  535. * @bridge: where to begin re-enumeration
  536. *
  537. * Iterate over all slots under this bridge and make sure that if a
  538. * card is present they are enabled, and if not they are disabled.
  539. */
  540. static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  541. {
  542. struct acpiphp_slot *slot;
  543. /* Bail out if the bridge is going away. */
  544. if (bridge->is_going_away)
  545. return;
  546. if (bridge->pci_dev)
  547. pm_runtime_get_sync(&bridge->pci_dev->dev);
  548. list_for_each_entry(slot, &bridge->slots, node) {
  549. struct pci_bus *bus = slot->bus;
  550. struct pci_dev *dev, *tmp;
  551. if (slot_no_hotplug(slot)) {
  552. ; /* do nothing */
  553. } else if (device_status_valid(get_slot_status(slot))) {
  554. /* remove stale devices if any */
  555. list_for_each_entry_safe_reverse(dev, tmp,
  556. &bus->devices, bus_list)
  557. if (PCI_SLOT(dev->devfn) == slot->device)
  558. trim_stale_devices(dev);
  559. /* configure all functions */
  560. enable_slot(slot);
  561. } else {
  562. disable_slot(slot);
  563. }
  564. }
  565. if (bridge->pci_dev)
  566. pm_runtime_put(&bridge->pci_dev->dev);
  567. }
  568. /*
  569. * Remove devices for which we could not assign resources, call
  570. * arch specific code to fix-up the bus
  571. */
  572. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  573. {
  574. struct pci_dev *dev, *tmp;
  575. int i;
  576. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  577. list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
  578. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
  579. struct resource *res = &dev->resource[i];
  580. if ((res->flags & type_mask) && !res->start &&
  581. res->end) {
  582. /* Could not assign a required resources
  583. * for this device, remove it */
  584. pci_stop_and_remove_bus_device(dev);
  585. break;
  586. }
  587. }
  588. }
  589. }
  590. /*
  591. * ACPI event handlers
  592. */
  593. void acpiphp_check_host_bridge(struct acpi_device *adev)
  594. {
  595. struct acpiphp_bridge *bridge = NULL;
  596. acpi_lock_hp_context();
  597. if (adev->hp) {
  598. bridge = to_acpiphp_root_context(adev->hp)->root_bridge;
  599. if (bridge)
  600. get_bridge(bridge);
  601. }
  602. acpi_unlock_hp_context();
  603. if (bridge) {
  604. pci_lock_rescan_remove();
  605. acpiphp_check_bridge(bridge);
  606. pci_unlock_rescan_remove();
  607. put_bridge(bridge);
  608. }
  609. }
  610. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
  611. static void hotplug_event(u32 type, struct acpiphp_context *context)
  612. {
  613. acpi_handle handle = context->hp.self->handle;
  614. struct acpiphp_func *func = &context->func;
  615. struct acpiphp_slot *slot = func->slot;
  616. struct acpiphp_bridge *bridge;
  617. acpi_lock_hp_context();
  618. bridge = context->bridge;
  619. if (bridge)
  620. get_bridge(bridge);
  621. acpi_unlock_hp_context();
  622. pci_lock_rescan_remove();
  623. switch (type) {
  624. case ACPI_NOTIFY_BUS_CHECK:
  625. /* bus re-enumerate */
  626. acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
  627. if (bridge)
  628. acpiphp_check_bridge(bridge);
  629. else if (!(slot->flags & SLOT_IS_GOING_AWAY))
  630. enable_slot(slot);
  631. break;
  632. case ACPI_NOTIFY_DEVICE_CHECK:
  633. /* device check */
  634. acpi_handle_debug(handle, "Device check in %s()\n", __func__);
  635. if (bridge) {
  636. acpiphp_check_bridge(bridge);
  637. } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
  638. /*
  639. * Check if anything has changed in the slot and rescan
  640. * from the parent if that's the case.
  641. */
  642. if (acpiphp_rescan_slot(slot))
  643. acpiphp_check_bridge(func->parent);
  644. }
  645. break;
  646. case ACPI_NOTIFY_EJECT_REQUEST:
  647. /* request device eject */
  648. acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
  649. acpiphp_disable_and_eject_slot(slot);
  650. break;
  651. }
  652. pci_unlock_rescan_remove();
  653. if (bridge)
  654. put_bridge(bridge);
  655. }
  656. static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type)
  657. {
  658. struct acpiphp_context *context;
  659. context = acpiphp_grab_context(adev);
  660. if (!context)
  661. return -ENODATA;
  662. hotplug_event(type, context);
  663. acpiphp_let_context_go(context);
  664. return 0;
  665. }
  666. /**
  667. * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
  668. * @bus: PCI bus to enumerate the slots for.
  669. *
  670. * A "slot" is an object associated with a PCI device number. All functions
  671. * (PCI devices) with the same bus and device number belong to the same slot.
  672. */
  673. void acpiphp_enumerate_slots(struct pci_bus *bus)
  674. {
  675. struct acpiphp_bridge *bridge;
  676. struct acpi_device *adev;
  677. acpi_handle handle;
  678. acpi_status status;
  679. if (acpiphp_disabled)
  680. return;
  681. adev = ACPI_COMPANION(bus->bridge);
  682. if (!adev)
  683. return;
  684. handle = adev->handle;
  685. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  686. if (!bridge)
  687. return;
  688. INIT_LIST_HEAD(&bridge->slots);
  689. kref_init(&bridge->ref);
  690. bridge->pci_dev = pci_dev_get(bus->self);
  691. bridge->pci_bus = bus;
  692. /*
  693. * Grab a ref to the subordinate PCI bus in case the bus is
  694. * removed via PCI core logical hotplug. The ref pins the bus
  695. * (which we access during module unload).
  696. */
  697. get_device(&bus->dev);
  698. acpi_lock_hp_context();
  699. if (pci_is_root_bus(bridge->pci_bus)) {
  700. struct acpiphp_root_context *root_context;
  701. root_context = kzalloc(sizeof(*root_context), GFP_KERNEL);
  702. if (!root_context)
  703. goto err;
  704. root_context->root_bridge = bridge;
  705. acpi_set_hp_context(adev, &root_context->hp);
  706. } else {
  707. struct acpiphp_context *context;
  708. /*
  709. * This bridge should have been registered as a hotplug function
  710. * under its parent, so the context should be there, unless the
  711. * parent is going to be handled by pciehp, in which case this
  712. * bridge is not interesting to us either.
  713. */
  714. context = acpiphp_get_context(adev);
  715. if (!context)
  716. goto err;
  717. bridge->context = context;
  718. context->bridge = bridge;
  719. /* Get a reference to the parent bridge. */
  720. get_bridge(context->func.parent);
  721. }
  722. acpi_unlock_hp_context();
  723. /* Must be added to the list prior to calling acpiphp_add_context(). */
  724. mutex_lock(&bridge_mutex);
  725. list_add(&bridge->list, &bridge_list);
  726. mutex_unlock(&bridge_mutex);
  727. /* register all slot objects under this bridge */
  728. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  729. acpiphp_add_context, NULL, bridge, NULL);
  730. if (ACPI_FAILURE(status)) {
  731. acpi_handle_err(handle, "failed to register slots\n");
  732. cleanup_bridge(bridge);
  733. put_bridge(bridge);
  734. }
  735. return;
  736. err:
  737. acpi_unlock_hp_context();
  738. put_device(&bus->dev);
  739. pci_dev_put(bridge->pci_dev);
  740. kfree(bridge);
  741. }
  742. static void acpiphp_drop_bridge(struct acpiphp_bridge *bridge)
  743. {
  744. if (pci_is_root_bus(bridge->pci_bus)) {
  745. struct acpiphp_root_context *root_context;
  746. struct acpi_device *adev;
  747. acpi_lock_hp_context();
  748. adev = ACPI_COMPANION(bridge->pci_bus->bridge);
  749. root_context = to_acpiphp_root_context(adev->hp);
  750. adev->hp = NULL;
  751. acpi_unlock_hp_context();
  752. kfree(root_context);
  753. }
  754. cleanup_bridge(bridge);
  755. put_bridge(bridge);
  756. }
  757. /**
  758. * acpiphp_remove_slots - Remove slot objects associated with a given bus.
  759. * @bus: PCI bus to remove the slot objects for.
  760. */
  761. void acpiphp_remove_slots(struct pci_bus *bus)
  762. {
  763. struct acpiphp_bridge *bridge;
  764. if (acpiphp_disabled)
  765. return;
  766. mutex_lock(&bridge_mutex);
  767. list_for_each_entry(bridge, &bridge_list, list)
  768. if (bridge->pci_bus == bus) {
  769. mutex_unlock(&bridge_mutex);
  770. acpiphp_drop_bridge(bridge);
  771. return;
  772. }
  773. mutex_unlock(&bridge_mutex);
  774. }
  775. /**
  776. * acpiphp_enable_slot - power on slot
  777. * @slot: ACPI PHP slot
  778. */
  779. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  780. {
  781. pci_lock_rescan_remove();
  782. if (slot->flags & SLOT_IS_GOING_AWAY) {
  783. pci_unlock_rescan_remove();
  784. return -ENODEV;
  785. }
  786. /* configure all functions */
  787. if (!(slot->flags & SLOT_ENABLED))
  788. enable_slot(slot);
  789. pci_unlock_rescan_remove();
  790. return 0;
  791. }
  792. /**
  793. * acpiphp_disable_and_eject_slot - power off and eject slot
  794. * @slot: ACPI PHP slot
  795. */
  796. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
  797. {
  798. struct acpiphp_func *func;
  799. if (slot->flags & SLOT_IS_GOING_AWAY)
  800. return -ENODEV;
  801. /* unconfigure all functions */
  802. disable_slot(slot);
  803. list_for_each_entry(func, &slot->funcs, sibling)
  804. if (func->flags & FUNC_HAS_EJ0) {
  805. acpi_handle handle = func_to_handle(func);
  806. if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
  807. acpi_handle_err(handle, "_EJ0 failed\n");
  808. break;
  809. }
  810. return 0;
  811. }
  812. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  813. {
  814. int ret;
  815. /*
  816. * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in
  817. * acpiphp_disable_and_eject_slot() will be synchronized properly.
  818. */
  819. acpi_scan_lock_acquire();
  820. pci_lock_rescan_remove();
  821. ret = acpiphp_disable_and_eject_slot(slot);
  822. pci_unlock_rescan_remove();
  823. acpi_scan_lock_release();
  824. return ret;
  825. }
  826. /*
  827. * slot enabled: 1
  828. * slot disabled: 0
  829. */
  830. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  831. {
  832. return (slot->flags & SLOT_ENABLED);
  833. }
  834. /*
  835. * latch open: 1
  836. * latch closed: 0
  837. */
  838. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  839. {
  840. return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
  841. }
  842. /*
  843. * adapter presence : 1
  844. * absence : 0
  845. */
  846. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  847. {
  848. return !!get_slot_status(slot);
  849. }