acpiphp_glue.c 27 KB

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