acpiphp_glue.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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 DEFINE_MUTEX(acpiphp_context_lock);
  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 functions 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. pr_debug("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. pr_warn("Slot %llu already registered by another "
  292. "hotplug driver\n", sun);
  293. else
  294. pr_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. pr_debug("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. pr_err("failed to remove notify handler\n");
  363. }
  364. }
  365. slot->flags |= SLOT_IS_GOING_AWAY;
  366. if (slot->slot)
  367. acpiphp_unregister_hotplug_slot(slot);
  368. }
  369. mutex_lock(&bridge_mutex);
  370. list_del(&bridge->list);
  371. mutex_unlock(&bridge_mutex);
  372. bridge->is_going_away = true;
  373. }
  374. /**
  375. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  376. * @bus: bus to start search with
  377. */
  378. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  379. {
  380. struct list_head *tmp;
  381. unsigned char max, n;
  382. /*
  383. * pci_bus_max_busnr will return the highest
  384. * reserved busnr for all these children.
  385. * that is equivalent to the bus->subordinate
  386. * value. We don't want to use the parent's
  387. * bus->subordinate value because it could have
  388. * padding in it.
  389. */
  390. max = bus->busn_res.start;
  391. list_for_each(tmp, &bus->children) {
  392. n = pci_bus_max_busnr(pci_bus_b(tmp));
  393. if (n > max)
  394. max = n;
  395. }
  396. return max;
  397. }
  398. /**
  399. * acpiphp_bus_trim - Trim device objects in an ACPI namespace subtree.
  400. * @handle: ACPI device object handle to start from.
  401. */
  402. static void acpiphp_bus_trim(acpi_handle handle)
  403. {
  404. struct acpi_device *adev = NULL;
  405. acpi_bus_get_device(handle, &adev);
  406. if (adev)
  407. acpi_bus_trim(adev);
  408. }
  409. /**
  410. * acpiphp_bus_add - Scan ACPI namespace subtree.
  411. * @handle: ACPI object handle to start the scan from.
  412. */
  413. static void acpiphp_bus_add(acpi_handle handle)
  414. {
  415. struct acpi_device *adev = NULL;
  416. acpi_bus_scan(handle);
  417. acpi_bus_get_device(handle, &adev);
  418. if (adev)
  419. acpi_device_set_power(adev, ACPI_STATE_D0);
  420. }
  421. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  422. {
  423. struct acpiphp_func *func;
  424. union acpi_object params[2];
  425. struct acpi_object_list arg_list;
  426. list_for_each_entry(func, &slot->funcs, sibling) {
  427. arg_list.count = 2;
  428. arg_list.pointer = params;
  429. params[0].type = ACPI_TYPE_INTEGER;
  430. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  431. params[1].type = ACPI_TYPE_INTEGER;
  432. params[1].integer.value = 1;
  433. /* _REG is optional, we don't care about if there is failure */
  434. acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
  435. NULL);
  436. }
  437. }
  438. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  439. {
  440. struct acpiphp_func *func;
  441. /* quirk, or pcie could set it already */
  442. if (dev->is_hotplug_bridge)
  443. return;
  444. list_for_each_entry(func, &slot->funcs, sibling) {
  445. if (PCI_FUNC(dev->devfn) == func->function) {
  446. dev->is_hotplug_bridge = 1;
  447. break;
  448. }
  449. }
  450. }
  451. static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
  452. {
  453. struct acpiphp_func *func;
  454. list_for_each_entry(func, &slot->funcs, sibling)
  455. acpiphp_bus_add(func_to_handle(func));
  456. return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
  457. }
  458. /**
  459. * enable_slot - enable, configure a slot
  460. * @slot: slot to be enabled
  461. *
  462. * This function should be called per *physical slot*,
  463. * not per each slot object in ACPI namespace.
  464. */
  465. static void __ref enable_slot(struct acpiphp_slot *slot)
  466. {
  467. struct pci_dev *dev;
  468. struct pci_bus *bus = slot->bus;
  469. struct acpiphp_func *func;
  470. int max, pass;
  471. LIST_HEAD(add_list);
  472. acpiphp_rescan_slot(slot);
  473. max = acpiphp_max_busnr(bus);
  474. for (pass = 0; pass < 2; pass++) {
  475. list_for_each_entry(dev, &bus->devices, bus_list) {
  476. if (PCI_SLOT(dev->devfn) != slot->device)
  477. continue;
  478. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  479. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  480. max = pci_scan_bridge(bus, dev, max, pass);
  481. if (pass && dev->subordinate) {
  482. check_hotplug_bridge(slot, dev);
  483. pcibios_resource_survey_bus(dev->subordinate);
  484. __pci_bus_size_bridges(dev->subordinate,
  485. &add_list);
  486. }
  487. }
  488. }
  489. }
  490. __pci_bus_assign_resources(bus, &add_list, NULL);
  491. acpiphp_sanitize_bus(bus);
  492. acpiphp_set_hpp_values(bus);
  493. acpiphp_set_acpi_region(slot);
  494. list_for_each_entry(dev, &bus->devices, bus_list) {
  495. /* Assume that newly added devices are powered on already. */
  496. if (!dev->is_added)
  497. dev->current_state = PCI_D0;
  498. }
  499. pci_bus_add_devices(bus);
  500. slot->flags |= SLOT_ENABLED;
  501. list_for_each_entry(func, &slot->funcs, sibling) {
  502. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  503. func->function));
  504. if (!dev) {
  505. /* Do not set SLOT_ENABLED flag if some funcs
  506. are not added. */
  507. slot->flags &= (~SLOT_ENABLED);
  508. continue;
  509. }
  510. }
  511. }
  512. /* return first device in slot, acquiring a reference on it */
  513. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  514. {
  515. struct pci_bus *bus = slot->bus;
  516. struct pci_dev *dev;
  517. struct pci_dev *ret = NULL;
  518. down_read(&pci_bus_sem);
  519. list_for_each_entry(dev, &bus->devices, bus_list)
  520. if (PCI_SLOT(dev->devfn) == slot->device) {
  521. ret = pci_dev_get(dev);
  522. break;
  523. }
  524. up_read(&pci_bus_sem);
  525. return ret;
  526. }
  527. /**
  528. * disable_slot - disable a slot
  529. * @slot: ACPI PHP slot
  530. */
  531. static void disable_slot(struct acpiphp_slot *slot)
  532. {
  533. struct acpiphp_func *func;
  534. struct pci_dev *pdev;
  535. /*
  536. * enable_slot() enumerates all functions in this device via
  537. * pci_scan_slot(), whether they have associated ACPI hotplug
  538. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  539. * here.
  540. */
  541. while ((pdev = dev_in_slot(slot))) {
  542. pci_stop_and_remove_bus_device(pdev);
  543. pci_dev_put(pdev);
  544. }
  545. list_for_each_entry(func, &slot->funcs, sibling)
  546. acpiphp_bus_trim(func_to_handle(func));
  547. slot->flags &= (~SLOT_ENABLED);
  548. }
  549. /**
  550. * get_slot_status - get ACPI slot status
  551. * @slot: ACPI PHP slot
  552. *
  553. * If a slot has _STA for each function and if any one of them
  554. * returned non-zero status, return it.
  555. *
  556. * If a slot doesn't have _STA and if any one of its functions'
  557. * configuration space is configured, return 0x0f as a _STA.
  558. *
  559. * Otherwise return 0.
  560. */
  561. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  562. {
  563. unsigned long long sta = 0;
  564. struct acpiphp_func *func;
  565. list_for_each_entry(func, &slot->funcs, sibling) {
  566. if (func->flags & FUNC_HAS_STA) {
  567. acpi_status status;
  568. status = acpi_evaluate_integer(func_to_handle(func),
  569. "_STA", NULL, &sta);
  570. if (ACPI_SUCCESS(status) && sta)
  571. break;
  572. } else {
  573. u32 dvid;
  574. pci_bus_read_config_dword(slot->bus,
  575. PCI_DEVFN(slot->device,
  576. func->function),
  577. PCI_VENDOR_ID, &dvid);
  578. if (dvid != 0xffffffff) {
  579. sta = ACPI_STA_ALL;
  580. break;
  581. }
  582. }
  583. }
  584. return (unsigned int)sta;
  585. }
  586. /**
  587. * trim_stale_devices - remove PCI devices that are not responding.
  588. * @dev: PCI device to start walking the hierarchy from.
  589. */
  590. static void trim_stale_devices(struct pci_dev *dev)
  591. {
  592. acpi_handle handle = ACPI_HANDLE(&dev->dev);
  593. struct pci_bus *bus = dev->subordinate;
  594. bool alive = false;
  595. if (handle) {
  596. acpi_status status;
  597. unsigned long long sta;
  598. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  599. alive = ACPI_SUCCESS(status) && sta == ACPI_STA_ALL;
  600. }
  601. if (!alive) {
  602. u32 v;
  603. /* Check if the device responds. */
  604. alive = pci_bus_read_dev_vendor_id(dev->bus, dev->devfn, &v, 0);
  605. }
  606. if (!alive) {
  607. pci_stop_and_remove_bus_device(dev);
  608. if (handle)
  609. acpiphp_bus_trim(handle);
  610. } else if (bus) {
  611. struct pci_dev *child, *tmp;
  612. /* The device is a bridge. so check the bus below it. */
  613. pm_runtime_get_sync(&dev->dev);
  614. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  615. trim_stale_devices(child);
  616. pm_runtime_put(&dev->dev);
  617. }
  618. }
  619. /**
  620. * acpiphp_check_bridge - re-enumerate devices
  621. * @bridge: where to begin re-enumeration
  622. *
  623. * Iterate over all slots under this bridge and make sure that if a
  624. * card is present they are enabled, and if not they are disabled.
  625. */
  626. static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  627. {
  628. struct acpiphp_slot *slot;
  629. /* Bail out if the bridge is going away. */
  630. if (bridge->is_going_away)
  631. return;
  632. list_for_each_entry(slot, &bridge->slots, node) {
  633. struct pci_bus *bus = slot->bus;
  634. struct pci_dev *dev, *tmp;
  635. mutex_lock(&slot->crit_sect);
  636. /* wake up all functions */
  637. if (get_slot_status(slot) == ACPI_STA_ALL) {
  638. /* remove stale devices if any */
  639. list_for_each_entry_safe(dev, tmp, &bus->devices,
  640. bus_list)
  641. if (PCI_SLOT(dev->devfn) == slot->device)
  642. trim_stale_devices(dev);
  643. /* configure all functions */
  644. enable_slot(slot);
  645. } else {
  646. disable_slot(slot);
  647. }
  648. mutex_unlock(&slot->crit_sect);
  649. }
  650. }
  651. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  652. {
  653. struct pci_dev *dev;
  654. list_for_each_entry(dev, &bus->devices, bus_list)
  655. pci_configure_slot(dev);
  656. }
  657. /*
  658. * Remove devices for which we could not assign resources, call
  659. * arch specific code to fix-up the bus
  660. */
  661. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  662. {
  663. struct pci_dev *dev, *tmp;
  664. int i;
  665. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  666. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  667. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  668. struct resource *res = &dev->resource[i];
  669. if ((res->flags & type_mask) && !res->start &&
  670. res->end) {
  671. /* Could not assign a required resources
  672. * for this device, remove it */
  673. pci_stop_and_remove_bus_device(dev);
  674. break;
  675. }
  676. }
  677. }
  678. }
  679. /*
  680. * ACPI event handlers
  681. */
  682. void acpiphp_check_host_bridge(acpi_handle handle)
  683. {
  684. struct acpiphp_bridge *bridge;
  685. bridge = acpiphp_handle_to_bridge(handle);
  686. if (bridge) {
  687. acpiphp_check_bridge(bridge);
  688. put_bridge(bridge);
  689. }
  690. }
  691. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
  692. static void hotplug_event(acpi_handle handle, u32 type, void *data)
  693. {
  694. struct acpiphp_context *context = data;
  695. struct acpiphp_func *func = &context->func;
  696. struct acpiphp_bridge *bridge;
  697. char objname[64];
  698. struct acpi_buffer buffer = { .length = sizeof(objname),
  699. .pointer = objname };
  700. mutex_lock(&acpiphp_context_lock);
  701. bridge = context->bridge;
  702. if (bridge)
  703. get_bridge(bridge);
  704. mutex_unlock(&acpiphp_context_lock);
  705. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  706. switch (type) {
  707. case ACPI_NOTIFY_BUS_CHECK:
  708. /* bus re-enumerate */
  709. pr_debug("%s: Bus check notify on %s\n", __func__, objname);
  710. pr_debug("%s: re-enumerating slots under %s\n",
  711. __func__, objname);
  712. if (bridge) {
  713. acpiphp_check_bridge(bridge);
  714. } else {
  715. struct acpiphp_slot *slot = func->slot;
  716. if (slot->flags & SLOT_IS_GOING_AWAY)
  717. break;
  718. mutex_lock(&slot->crit_sect);
  719. enable_slot(slot);
  720. mutex_unlock(&slot->crit_sect);
  721. }
  722. break;
  723. case ACPI_NOTIFY_DEVICE_CHECK:
  724. /* device check */
  725. pr_debug("%s: Device check notify on %s\n", __func__, objname);
  726. if (bridge) {
  727. acpiphp_check_bridge(bridge);
  728. } else {
  729. struct acpiphp_slot *slot = func->slot;
  730. int ret;
  731. if (slot->flags & SLOT_IS_GOING_AWAY)
  732. break;
  733. /*
  734. * Check if anything has changed in the slot and rescan
  735. * from the parent if that's the case.
  736. */
  737. mutex_lock(&slot->crit_sect);
  738. ret = acpiphp_rescan_slot(slot);
  739. mutex_unlock(&slot->crit_sect);
  740. if (ret)
  741. acpiphp_check_bridge(func->parent);
  742. }
  743. break;
  744. case ACPI_NOTIFY_EJECT_REQUEST:
  745. /* request device eject */
  746. pr_debug("%s: Device eject notify on %s\n", __func__, objname);
  747. acpiphp_disable_and_eject_slot(func->slot);
  748. break;
  749. }
  750. if (bridge)
  751. put_bridge(bridge);
  752. }
  753. static void hotplug_event_work(void *data, u32 type)
  754. {
  755. struct acpiphp_context *context = data;
  756. acpi_handle handle = context->handle;
  757. acpi_scan_lock_acquire();
  758. pci_lock_rescan_remove();
  759. hotplug_event(handle, type, context);
  760. pci_unlock_rescan_remove();
  761. acpi_scan_lock_release();
  762. acpi_evaluate_hotplug_ost(handle, type, ACPI_OST_SC_SUCCESS, NULL);
  763. put_bridge(context->func.parent);
  764. }
  765. /**
  766. * handle_hotplug_event - handle ACPI hotplug event
  767. * @handle: Notify()'ed acpi_handle
  768. * @type: Notify code
  769. * @data: pointer to acpiphp_context structure
  770. *
  771. * Handles ACPI event notification on slots.
  772. */
  773. static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
  774. {
  775. struct acpiphp_context *context;
  776. u32 ost_code = ACPI_OST_SC_SUCCESS;
  777. switch (type) {
  778. case ACPI_NOTIFY_BUS_CHECK:
  779. case ACPI_NOTIFY_DEVICE_CHECK:
  780. break;
  781. case ACPI_NOTIFY_EJECT_REQUEST:
  782. ost_code = ACPI_OST_SC_EJECT_IN_PROGRESS;
  783. acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
  784. break;
  785. case ACPI_NOTIFY_DEVICE_WAKE:
  786. return;
  787. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  788. acpi_handle_err(handle, "Device cannot be configured due "
  789. "to a frequency mismatch\n");
  790. goto out;
  791. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  792. acpi_handle_err(handle, "Device cannot be configured due "
  793. "to a bus mode mismatch\n");
  794. goto out;
  795. case ACPI_NOTIFY_POWER_FAULT:
  796. acpi_handle_err(handle, "Device has suffered a power fault\n");
  797. goto out;
  798. default:
  799. acpi_handle_warn(handle, "Unsupported event type 0x%x\n", type);
  800. ost_code = ACPI_OST_SC_UNRECOGNIZED_NOTIFY;
  801. goto out;
  802. }
  803. mutex_lock(&acpiphp_context_lock);
  804. context = acpiphp_get_context(handle);
  805. if (context && !WARN_ON(context->handle != handle)) {
  806. get_bridge(context->func.parent);
  807. acpiphp_put_context(context);
  808. acpi_hotplug_execute(hotplug_event_work, context, type);
  809. mutex_unlock(&acpiphp_context_lock);
  810. return;
  811. }
  812. mutex_unlock(&acpiphp_context_lock);
  813. ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
  814. out:
  815. acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
  816. }
  817. /*
  818. * Create hotplug slots for the PCI bus.
  819. * It should always return 0 to avoid skipping following notifiers.
  820. */
  821. void acpiphp_enumerate_slots(struct pci_bus *bus)
  822. {
  823. struct acpiphp_bridge *bridge;
  824. acpi_handle handle;
  825. acpi_status status;
  826. if (acpiphp_disabled)
  827. return;
  828. handle = ACPI_HANDLE(bus->bridge);
  829. if (!handle)
  830. return;
  831. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  832. if (!bridge) {
  833. acpi_handle_err(handle, "No memory for bridge object\n");
  834. return;
  835. }
  836. INIT_LIST_HEAD(&bridge->slots);
  837. kref_init(&bridge->ref);
  838. bridge->pci_dev = pci_dev_get(bus->self);
  839. bridge->pci_bus = bus;
  840. /*
  841. * Grab a ref to the subordinate PCI bus in case the bus is
  842. * removed via PCI core logical hotplug. The ref pins the bus
  843. * (which we access during module unload).
  844. */
  845. get_device(&bus->dev);
  846. if (!pci_is_root_bus(bridge->pci_bus)) {
  847. struct acpiphp_context *context;
  848. /*
  849. * This bridge should have been registered as a hotplug function
  850. * under its parent, so the context should be there, unless the
  851. * parent is going to be handled by pciehp, in which case this
  852. * bridge is not interesting to us either.
  853. */
  854. mutex_lock(&acpiphp_context_lock);
  855. context = acpiphp_get_context(handle);
  856. if (!context) {
  857. mutex_unlock(&acpiphp_context_lock);
  858. put_device(&bus->dev);
  859. pci_dev_put(bridge->pci_dev);
  860. kfree(bridge);
  861. return;
  862. }
  863. bridge->context = context;
  864. context->bridge = bridge;
  865. /* Get a reference to the parent bridge. */
  866. get_bridge(context->func.parent);
  867. mutex_unlock(&acpiphp_context_lock);
  868. }
  869. /* must be added to the list prior to calling register_slot */
  870. mutex_lock(&bridge_mutex);
  871. list_add(&bridge->list, &bridge_list);
  872. mutex_unlock(&bridge_mutex);
  873. /* register all slot objects under this bridge */
  874. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  875. register_slot, NULL, bridge, NULL);
  876. if (ACPI_FAILURE(status)) {
  877. acpi_handle_err(handle, "failed to register slots\n");
  878. cleanup_bridge(bridge);
  879. put_bridge(bridge);
  880. }
  881. }
  882. /* Destroy hotplug slots associated with the PCI bus */
  883. void acpiphp_remove_slots(struct pci_bus *bus)
  884. {
  885. struct acpiphp_bridge *bridge;
  886. if (acpiphp_disabled)
  887. return;
  888. mutex_lock(&bridge_mutex);
  889. list_for_each_entry(bridge, &bridge_list, list)
  890. if (bridge->pci_bus == bus) {
  891. mutex_unlock(&bridge_mutex);
  892. cleanup_bridge(bridge);
  893. put_bridge(bridge);
  894. return;
  895. }
  896. mutex_unlock(&bridge_mutex);
  897. }
  898. /**
  899. * acpiphp_enable_slot - power on slot
  900. * @slot: ACPI PHP slot
  901. */
  902. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  903. {
  904. pci_lock_rescan_remove();
  905. if (slot->flags & SLOT_IS_GOING_AWAY)
  906. return -ENODEV;
  907. mutex_lock(&slot->crit_sect);
  908. /* configure all functions */
  909. if (!(slot->flags & SLOT_ENABLED))
  910. enable_slot(slot);
  911. mutex_unlock(&slot->crit_sect);
  912. pci_unlock_rescan_remove();
  913. return 0;
  914. }
  915. /**
  916. * acpiphp_disable_and_eject_slot - power off and eject slot
  917. * @slot: ACPI PHP slot
  918. */
  919. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
  920. {
  921. struct acpiphp_func *func;
  922. if (slot->flags & SLOT_IS_GOING_AWAY)
  923. return -ENODEV;
  924. mutex_lock(&slot->crit_sect);
  925. /* unconfigure all functions */
  926. disable_slot(slot);
  927. list_for_each_entry(func, &slot->funcs, sibling)
  928. if (func->flags & FUNC_HAS_EJ0) {
  929. acpi_handle handle = func_to_handle(func);
  930. if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
  931. acpi_handle_err(handle, "_EJ0 failed\n");
  932. break;
  933. }
  934. mutex_unlock(&slot->crit_sect);
  935. return 0;
  936. }
  937. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  938. {
  939. int ret;
  940. pci_lock_rescan_remove();
  941. ret = acpiphp_disable_and_eject_slot(slot);
  942. pci_unlock_rescan_remove();
  943. return ret;
  944. }
  945. /*
  946. * slot enabled: 1
  947. * slot disabled: 0
  948. */
  949. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  950. {
  951. return (slot->flags & SLOT_ENABLED);
  952. }
  953. /*
  954. * latch open: 1
  955. * latch closed: 0
  956. */
  957. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  958. {
  959. return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
  960. }
  961. /*
  962. * adapter presence : 1
  963. * absence : 0
  964. */
  965. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  966. {
  967. return !!get_slot_status(slot);
  968. }