acpiphp_glue.c 26 KB

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