acpiphp_glue.c 26 KB

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