acpiphp_glue.c 26 KB

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