acpiphp_glue.c 24 KB

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