acpiphp_glue.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. /* callback routine to register each ACPI PCI slot object */
  218. static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
  219. void **rv)
  220. {
  221. struct acpiphp_bridge *bridge = data;
  222. struct acpiphp_context *context;
  223. struct acpi_device *adev;
  224. struct acpiphp_slot *slot;
  225. struct acpiphp_func *newfunc;
  226. acpi_status status = AE_OK;
  227. unsigned long long adr;
  228. int device, function;
  229. struct pci_bus *pbus = bridge->pci_bus;
  230. struct pci_dev *pdev = bridge->pci_dev;
  231. u32 val;
  232. if (pdev && device_is_managed_by_native_pciehp(pdev))
  233. return AE_OK;
  234. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  235. if (ACPI_FAILURE(status)) {
  236. if (status != AE_NOT_FOUND)
  237. acpi_handle_warn(handle,
  238. "can't evaluate _ADR (%#x)\n", status);
  239. return AE_OK;
  240. }
  241. if (acpi_bus_get_device(handle, &adev))
  242. return AE_OK;
  243. device = (adr >> 16) & 0xffff;
  244. function = adr & 0xffff;
  245. acpi_lock_hp_context();
  246. context = acpiphp_init_context(adev);
  247. if (!context) {
  248. acpi_unlock_hp_context();
  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. acpi_unlock_hp_context();
  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. acpi_lock_hp_context();
  269. acpiphp_put_context(context);
  270. acpi_unlock_hp_context();
  271. return AE_NO_MEMORY;
  272. }
  273. slot->bus = bridge->pci_bus;
  274. slot->device = device;
  275. INIT_LIST_HEAD(&slot->funcs);
  276. list_add_tail(&slot->node, &bridge->slots);
  277. /* Register slots for ejectable functions only. */
  278. if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
  279. unsigned long long sun;
  280. int retval;
  281. bridge->nr_slots++;
  282. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  283. if (ACPI_FAILURE(status))
  284. sun = bridge->nr_slots;
  285. pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  286. sun, pci_domain_nr(pbus), pbus->number, device);
  287. retval = acpiphp_register_hotplug_slot(slot, sun);
  288. if (retval) {
  289. slot->slot = NULL;
  290. bridge->nr_slots--;
  291. if (retval == -EBUSY)
  292. pr_warn("Slot %llu already registered by another "
  293. "hotplug driver\n", sun);
  294. else
  295. pr_warn("acpiphp_register_hotplug_slot failed "
  296. "(err code = 0x%x)\n", retval);
  297. }
  298. /* Even if the slot registration fails, we can still use it. */
  299. }
  300. slot_found:
  301. newfunc->slot = slot;
  302. list_add_tail(&newfunc->sibling, &slot->funcs);
  303. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  304. &val, 60*1000))
  305. slot->flags |= SLOT_ENABLED;
  306. if (is_dock_device(handle)) {
  307. /* we don't want to call this device's _EJ0
  308. * because we want the dock notify handler
  309. * to call it after it calls _DCK
  310. */
  311. newfunc->flags &= ~FUNC_HAS_EJ0;
  312. if (register_hotplug_dock_device(handle,
  313. &acpiphp_dock_ops, context,
  314. acpiphp_dock_init, acpiphp_dock_release))
  315. pr_debug("failed to register dock device\n");
  316. }
  317. /* install notify handler */
  318. if (!(newfunc->flags & FUNC_HAS_DCK))
  319. acpi_install_hotplug_notify_handler(handle, NULL);
  320. return AE_OK;
  321. }
  322. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  323. {
  324. struct acpi_device *adev = acpi_bus_get_acpi_device(handle);
  325. struct acpiphp_context *context;
  326. struct acpiphp_bridge *bridge = NULL;
  327. if (!adev)
  328. return NULL;
  329. acpi_lock_hp_context();
  330. context = acpiphp_get_context(adev);
  331. if (context) {
  332. bridge = context->bridge;
  333. if (bridge)
  334. get_bridge(bridge);
  335. acpiphp_put_context(context);
  336. }
  337. acpi_unlock_hp_context();
  338. acpi_bus_put_acpi_device(adev);
  339. return bridge;
  340. }
  341. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  342. {
  343. struct acpiphp_slot *slot;
  344. struct acpiphp_func *func;
  345. list_for_each_entry(slot, &bridge->slots, node) {
  346. list_for_each_entry(func, &slot->funcs, sibling) {
  347. acpi_handle handle = func_to_handle(func);
  348. if (is_dock_device(handle))
  349. unregister_hotplug_dock_device(handle);
  350. if (!(func->flags & FUNC_HAS_DCK))
  351. acpi_remove_hotplug_notify_handler(handle);
  352. }
  353. slot->flags |= SLOT_IS_GOING_AWAY;
  354. if (slot->slot)
  355. acpiphp_unregister_hotplug_slot(slot);
  356. }
  357. mutex_lock(&bridge_mutex);
  358. list_del(&bridge->list);
  359. mutex_unlock(&bridge_mutex);
  360. acpi_lock_hp_context();
  361. bridge->is_going_away = true;
  362. acpi_unlock_hp_context();
  363. }
  364. /**
  365. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  366. * @bus: bus to start search with
  367. */
  368. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  369. {
  370. struct list_head *tmp;
  371. unsigned char max, n;
  372. /*
  373. * pci_bus_max_busnr will return the highest
  374. * reserved busnr for all these children.
  375. * that is equivalent to the bus->subordinate
  376. * value. We don't want to use the parent's
  377. * bus->subordinate value because it could have
  378. * padding in it.
  379. */
  380. max = bus->busn_res.start;
  381. list_for_each(tmp, &bus->children) {
  382. n = pci_bus_max_busnr(pci_bus_b(tmp));
  383. if (n > max)
  384. max = n;
  385. }
  386. return max;
  387. }
  388. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  389. {
  390. struct acpiphp_func *func;
  391. union acpi_object params[2];
  392. struct acpi_object_list arg_list;
  393. list_for_each_entry(func, &slot->funcs, sibling) {
  394. arg_list.count = 2;
  395. arg_list.pointer = params;
  396. params[0].type = ACPI_TYPE_INTEGER;
  397. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  398. params[1].type = ACPI_TYPE_INTEGER;
  399. params[1].integer.value = 1;
  400. /* _REG is optional, we don't care about if there is failure */
  401. acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
  402. NULL);
  403. }
  404. }
  405. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  406. {
  407. struct acpiphp_func *func;
  408. /* quirk, or pcie could set it already */
  409. if (dev->is_hotplug_bridge)
  410. return;
  411. list_for_each_entry(func, &slot->funcs, sibling) {
  412. if (PCI_FUNC(dev->devfn) == func->function) {
  413. dev->is_hotplug_bridge = 1;
  414. break;
  415. }
  416. }
  417. }
  418. static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
  419. {
  420. struct acpiphp_func *func;
  421. list_for_each_entry(func, &slot->funcs, sibling) {
  422. struct acpi_device *adev = func_to_acpi_device(func);
  423. acpi_bus_scan(adev->handle);
  424. if (acpi_device_enumerated(adev))
  425. acpi_device_set_power(adev, ACPI_STATE_D0);
  426. }
  427. return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
  428. }
  429. /**
  430. * enable_slot - enable, configure a slot
  431. * @slot: slot to be enabled
  432. *
  433. * This function should be called per *physical slot*,
  434. * not per each slot object in ACPI namespace.
  435. */
  436. static void __ref enable_slot(struct acpiphp_slot *slot)
  437. {
  438. struct pci_dev *dev;
  439. struct pci_bus *bus = slot->bus;
  440. struct acpiphp_func *func;
  441. int max, pass;
  442. LIST_HEAD(add_list);
  443. acpiphp_rescan_slot(slot);
  444. max = acpiphp_max_busnr(bus);
  445. for (pass = 0; pass < 2; pass++) {
  446. list_for_each_entry(dev, &bus->devices, bus_list) {
  447. if (PCI_SLOT(dev->devfn) != slot->device)
  448. continue;
  449. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  450. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  451. max = pci_scan_bridge(bus, dev, max, pass);
  452. if (pass && dev->subordinate) {
  453. check_hotplug_bridge(slot, dev);
  454. pcibios_resource_survey_bus(dev->subordinate);
  455. __pci_bus_size_bridges(dev->subordinate,
  456. &add_list);
  457. }
  458. }
  459. }
  460. }
  461. __pci_bus_assign_resources(bus, &add_list, NULL);
  462. acpiphp_sanitize_bus(bus);
  463. acpiphp_set_hpp_values(bus);
  464. acpiphp_set_acpi_region(slot);
  465. list_for_each_entry(dev, &bus->devices, bus_list) {
  466. /* Assume that newly added devices are powered on already. */
  467. if (!dev->is_added)
  468. dev->current_state = PCI_D0;
  469. }
  470. pci_bus_add_devices(bus);
  471. slot->flags |= SLOT_ENABLED;
  472. list_for_each_entry(func, &slot->funcs, sibling) {
  473. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  474. func->function));
  475. if (!dev) {
  476. /* Do not set SLOT_ENABLED flag if some funcs
  477. are not added. */
  478. slot->flags &= (~SLOT_ENABLED);
  479. continue;
  480. }
  481. }
  482. }
  483. /**
  484. * disable_slot - disable a slot
  485. * @slot: ACPI PHP slot
  486. */
  487. static void disable_slot(struct acpiphp_slot *slot)
  488. {
  489. struct pci_bus *bus = slot->bus;
  490. struct pci_dev *dev, *prev;
  491. struct acpiphp_func *func;
  492. /*
  493. * enable_slot() enumerates all functions in this device via
  494. * pci_scan_slot(), whether they have associated ACPI hotplug
  495. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  496. * here.
  497. */
  498. list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
  499. if (PCI_SLOT(dev->devfn) == slot->device)
  500. pci_stop_and_remove_bus_device(dev);
  501. list_for_each_entry(func, &slot->funcs, sibling)
  502. acpi_bus_trim(func_to_acpi_device(func));
  503. slot->flags &= (~SLOT_ENABLED);
  504. }
  505. static bool acpiphp_no_hotplug(struct acpi_device *adev)
  506. {
  507. return adev && adev->flags.no_hotplug;
  508. }
  509. static bool slot_no_hotplug(struct acpiphp_slot *slot)
  510. {
  511. struct acpiphp_func *func;
  512. list_for_each_entry(func, &slot->funcs, sibling)
  513. if (acpiphp_no_hotplug(func_to_acpi_device(func)))
  514. return true;
  515. return false;
  516. }
  517. /**
  518. * get_slot_status - get ACPI slot status
  519. * @slot: ACPI PHP slot
  520. *
  521. * If a slot has _STA for each function and if any one of them
  522. * returned non-zero status, return it.
  523. *
  524. * If a slot doesn't have _STA and if any one of its functions'
  525. * configuration space is configured, return 0x0f as a _STA.
  526. *
  527. * Otherwise return 0.
  528. */
  529. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  530. {
  531. unsigned long long sta = 0;
  532. struct acpiphp_func *func;
  533. list_for_each_entry(func, &slot->funcs, sibling) {
  534. if (func->flags & FUNC_HAS_STA) {
  535. acpi_status status;
  536. status = acpi_evaluate_integer(func_to_handle(func),
  537. "_STA", NULL, &sta);
  538. if (ACPI_SUCCESS(status) && sta)
  539. break;
  540. } else {
  541. u32 dvid;
  542. pci_bus_read_config_dword(slot->bus,
  543. PCI_DEVFN(slot->device,
  544. func->function),
  545. PCI_VENDOR_ID, &dvid);
  546. if (dvid != 0xffffffff) {
  547. sta = ACPI_STA_ALL;
  548. break;
  549. }
  550. }
  551. }
  552. return (unsigned int)sta;
  553. }
  554. /**
  555. * trim_stale_devices - remove PCI devices that are not responding.
  556. * @dev: PCI device to start walking the hierarchy from.
  557. */
  558. static void trim_stale_devices(struct pci_dev *dev)
  559. {
  560. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  561. struct pci_bus *bus = dev->subordinate;
  562. bool alive = false;
  563. if (adev) {
  564. acpi_status status;
  565. unsigned long long sta;
  566. status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
  567. alive = (ACPI_SUCCESS(status) && sta == ACPI_STA_ALL)
  568. || acpiphp_no_hotplug(adev);
  569. }
  570. if (!alive) {
  571. u32 v;
  572. /* Check if the device responds. */
  573. alive = pci_bus_read_dev_vendor_id(dev->bus, dev->devfn, &v, 0);
  574. }
  575. if (!alive) {
  576. pci_stop_and_remove_bus_device(dev);
  577. if (adev)
  578. acpi_bus_trim(adev);
  579. } else if (bus) {
  580. struct pci_dev *child, *tmp;
  581. /* The device is a bridge. so check the bus below it. */
  582. pm_runtime_get_sync(&dev->dev);
  583. list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
  584. trim_stale_devices(child);
  585. pm_runtime_put(&dev->dev);
  586. }
  587. }
  588. /**
  589. * acpiphp_check_bridge - re-enumerate devices
  590. * @bridge: where to begin re-enumeration
  591. *
  592. * Iterate over all slots under this bridge and make sure that if a
  593. * card is present they are enabled, and if not they are disabled.
  594. */
  595. static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  596. {
  597. struct acpiphp_slot *slot;
  598. /* Bail out if the bridge is going away. */
  599. if (bridge->is_going_away)
  600. return;
  601. list_for_each_entry(slot, &bridge->slots, node) {
  602. struct pci_bus *bus = slot->bus;
  603. struct pci_dev *dev, *tmp;
  604. if (slot_no_hotplug(slot)) {
  605. ; /* do nothing */
  606. } else if (get_slot_status(slot) == ACPI_STA_ALL) {
  607. /* remove stale devices if any */
  608. list_for_each_entry_safe_reverse(dev, tmp,
  609. &bus->devices, bus_list)
  610. if (PCI_SLOT(dev->devfn) == slot->device)
  611. trim_stale_devices(dev);
  612. /* configure all functions */
  613. enable_slot(slot);
  614. } else {
  615. disable_slot(slot);
  616. }
  617. }
  618. }
  619. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  620. {
  621. struct pci_dev *dev;
  622. list_for_each_entry(dev, &bus->devices, bus_list)
  623. pci_configure_slot(dev);
  624. }
  625. /*
  626. * Remove devices for which we could not assign resources, call
  627. * arch specific code to fix-up the bus
  628. */
  629. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  630. {
  631. struct pci_dev *dev, *tmp;
  632. int i;
  633. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  634. list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
  635. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  636. struct resource *res = &dev->resource[i];
  637. if ((res->flags & type_mask) && !res->start &&
  638. res->end) {
  639. /* Could not assign a required resources
  640. * for this device, remove it */
  641. pci_stop_and_remove_bus_device(dev);
  642. break;
  643. }
  644. }
  645. }
  646. }
  647. /*
  648. * ACPI event handlers
  649. */
  650. void acpiphp_check_host_bridge(acpi_handle handle)
  651. {
  652. struct acpiphp_bridge *bridge;
  653. bridge = acpiphp_handle_to_bridge(handle);
  654. if (bridge) {
  655. pci_lock_rescan_remove();
  656. acpiphp_check_bridge(bridge);
  657. pci_unlock_rescan_remove();
  658. put_bridge(bridge);
  659. }
  660. }
  661. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
  662. static void hotplug_event(u32 type, struct acpiphp_context *context)
  663. {
  664. acpi_handle handle = context->hp.self->handle;
  665. struct acpiphp_func *func = &context->func;
  666. struct acpiphp_slot *slot = func->slot;
  667. struct acpiphp_bridge *bridge;
  668. acpi_lock_hp_context();
  669. bridge = context->bridge;
  670. if (bridge)
  671. get_bridge(bridge);
  672. acpi_unlock_hp_context();
  673. pci_lock_rescan_remove();
  674. switch (type) {
  675. case ACPI_NOTIFY_BUS_CHECK:
  676. /* bus re-enumerate */
  677. acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
  678. if (bridge)
  679. acpiphp_check_bridge(bridge);
  680. else if (!(slot->flags & SLOT_IS_GOING_AWAY))
  681. enable_slot(slot);
  682. break;
  683. case ACPI_NOTIFY_DEVICE_CHECK:
  684. /* device check */
  685. acpi_handle_debug(handle, "Device check in %s()\n", __func__);
  686. if (bridge) {
  687. acpiphp_check_bridge(bridge);
  688. } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
  689. /*
  690. * Check if anything has changed in the slot and rescan
  691. * from the parent if that's the case.
  692. */
  693. if (acpiphp_rescan_slot(slot))
  694. acpiphp_check_bridge(func->parent);
  695. }
  696. break;
  697. case ACPI_NOTIFY_EJECT_REQUEST:
  698. /* request device eject */
  699. acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
  700. acpiphp_disable_and_eject_slot(slot);
  701. break;
  702. }
  703. pci_unlock_rescan_remove();
  704. if (bridge)
  705. put_bridge(bridge);
  706. }
  707. static int acpiphp_hotplug_event(struct acpi_device *adev, u32 type)
  708. {
  709. struct acpiphp_context *context;
  710. acpi_lock_hp_context();
  711. context = acpiphp_get_context(adev);
  712. if (!context || context->func.parent->is_going_away) {
  713. acpi_unlock_hp_context();
  714. return -ENODATA;
  715. }
  716. get_bridge(context->func.parent);
  717. acpiphp_put_context(context);
  718. acpi_unlock_hp_context();
  719. hotplug_event(type, context);
  720. put_bridge(context->func.parent);
  721. return 0;
  722. }
  723. /**
  724. * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
  725. * @bus: PCI bus to enumerate the slots for.
  726. *
  727. * A "slot" is an object associated with a PCI device number. All functions
  728. * (PCI devices) with the same bus and device number belong to the same slot.
  729. */
  730. void acpiphp_enumerate_slots(struct pci_bus *bus)
  731. {
  732. struct acpiphp_bridge *bridge;
  733. struct acpi_device *adev;
  734. acpi_handle handle;
  735. acpi_status status;
  736. if (acpiphp_disabled)
  737. return;
  738. adev = ACPI_COMPANION(bus->bridge);
  739. if (!adev)
  740. return;
  741. handle = adev->handle;
  742. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  743. if (!bridge) {
  744. acpi_handle_err(handle, "No memory for bridge object\n");
  745. return;
  746. }
  747. INIT_LIST_HEAD(&bridge->slots);
  748. kref_init(&bridge->ref);
  749. bridge->pci_dev = pci_dev_get(bus->self);
  750. bridge->pci_bus = bus;
  751. /*
  752. * Grab a ref to the subordinate PCI bus in case the bus is
  753. * removed via PCI core logical hotplug. The ref pins the bus
  754. * (which we access during module unload).
  755. */
  756. get_device(&bus->dev);
  757. if (!pci_is_root_bus(bridge->pci_bus)) {
  758. struct acpiphp_context *context;
  759. /*
  760. * This bridge should have been registered as a hotplug function
  761. * under its parent, so the context should be there, unless the
  762. * parent is going to be handled by pciehp, in which case this
  763. * bridge is not interesting to us either.
  764. */
  765. acpi_lock_hp_context();
  766. context = acpiphp_get_context(adev);
  767. if (!context) {
  768. acpi_unlock_hp_context();
  769. put_device(&bus->dev);
  770. pci_dev_put(bridge->pci_dev);
  771. kfree(bridge);
  772. return;
  773. }
  774. bridge->context = context;
  775. context->bridge = bridge;
  776. /* Get a reference to the parent bridge. */
  777. get_bridge(context->func.parent);
  778. acpi_unlock_hp_context();
  779. }
  780. /* must be added to the list prior to calling register_slot */
  781. mutex_lock(&bridge_mutex);
  782. list_add(&bridge->list, &bridge_list);
  783. mutex_unlock(&bridge_mutex);
  784. /* register all slot objects under this bridge */
  785. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  786. register_slot, NULL, bridge, NULL);
  787. if (ACPI_FAILURE(status)) {
  788. acpi_handle_err(handle, "failed to register slots\n");
  789. cleanup_bridge(bridge);
  790. put_bridge(bridge);
  791. }
  792. }
  793. /**
  794. * acpiphp_remove_slots - Remove slot objects associated with a given bus.
  795. * @bus: PCI bus to remove the slot objects for.
  796. */
  797. void acpiphp_remove_slots(struct pci_bus *bus)
  798. {
  799. struct acpiphp_bridge *bridge;
  800. if (acpiphp_disabled)
  801. return;
  802. mutex_lock(&bridge_mutex);
  803. list_for_each_entry(bridge, &bridge_list, list)
  804. if (bridge->pci_bus == bus) {
  805. mutex_unlock(&bridge_mutex);
  806. cleanup_bridge(bridge);
  807. put_bridge(bridge);
  808. return;
  809. }
  810. mutex_unlock(&bridge_mutex);
  811. }
  812. /**
  813. * acpiphp_enable_slot - power on slot
  814. * @slot: ACPI PHP slot
  815. */
  816. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  817. {
  818. pci_lock_rescan_remove();
  819. if (slot->flags & SLOT_IS_GOING_AWAY)
  820. return -ENODEV;
  821. /* configure all functions */
  822. if (!(slot->flags & SLOT_ENABLED))
  823. enable_slot(slot);
  824. pci_unlock_rescan_remove();
  825. return 0;
  826. }
  827. /**
  828. * acpiphp_disable_and_eject_slot - power off and eject slot
  829. * @slot: ACPI PHP slot
  830. */
  831. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
  832. {
  833. struct acpiphp_func *func;
  834. if (slot->flags & SLOT_IS_GOING_AWAY)
  835. return -ENODEV;
  836. /* unconfigure all functions */
  837. disable_slot(slot);
  838. list_for_each_entry(func, &slot->funcs, sibling)
  839. if (func->flags & FUNC_HAS_EJ0) {
  840. acpi_handle handle = func_to_handle(func);
  841. if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
  842. acpi_handle_err(handle, "_EJ0 failed\n");
  843. break;
  844. }
  845. return 0;
  846. }
  847. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  848. {
  849. int ret;
  850. pci_lock_rescan_remove();
  851. ret = acpiphp_disable_and_eject_slot(slot);
  852. pci_unlock_rescan_remove();
  853. return ret;
  854. }
  855. /*
  856. * slot enabled: 1
  857. * slot disabled: 0
  858. */
  859. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  860. {
  861. return (slot->flags & SLOT_ENABLED);
  862. }
  863. /*
  864. * latch open: 1
  865. * latch closed: 0
  866. */
  867. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  868. {
  869. return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
  870. }
  871. /*
  872. * adapter presence : 1
  873. * absence : 0
  874. */
  875. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  876. {
  877. return !!get_slot_status(slot);
  878. }