acpiphp_glue.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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. #define MY_NAME "acpiphp_glue"
  54. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  55. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  56. static void acpiphp_set_hpp_values(struct pci_bus *bus);
  57. static void hotplug_event_func(acpi_handle handle, u32 type, void *context);
  58. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
  59. static void free_bridge(struct kref *kref);
  60. /* callback routine to check for the existence of a pci dock device */
  61. static acpi_status
  62. is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
  63. {
  64. int *count = (int *)context;
  65. if (is_dock_device(handle)) {
  66. (*count)++;
  67. return AE_CTRL_TERMINATE;
  68. } else {
  69. return AE_OK;
  70. }
  71. }
  72. static inline void get_bridge(struct acpiphp_bridge *bridge)
  73. {
  74. kref_get(&bridge->ref);
  75. }
  76. static inline void put_bridge(struct acpiphp_bridge *bridge)
  77. {
  78. kref_put(&bridge->ref, free_bridge);
  79. }
  80. static void free_bridge(struct kref *kref)
  81. {
  82. struct acpiphp_bridge *bridge;
  83. struct acpiphp_slot *slot, *next;
  84. struct acpiphp_func *func, *tmp;
  85. bridge = container_of(kref, struct acpiphp_bridge, ref);
  86. list_for_each_entry_safe(slot, next, &bridge->slots, node) {
  87. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
  88. kfree(func);
  89. }
  90. kfree(slot);
  91. }
  92. /* Release reference acquired by acpiphp_bridge_handle_to_function() */
  93. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)
  94. put_bridge(bridge->func->slot->bridge);
  95. put_device(&bridge->pci_bus->dev);
  96. pci_dev_put(bridge->pci_dev);
  97. kfree(bridge);
  98. }
  99. /*
  100. * the _DCK method can do funny things... and sometimes not
  101. * hah-hah funny.
  102. *
  103. * TBD - figure out a way to only call fixups for
  104. * systems that require them.
  105. */
  106. static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
  107. void *v)
  108. {
  109. struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
  110. struct pci_bus *bus = func->slot->bridge->pci_bus;
  111. u32 buses;
  112. if (!bus->self)
  113. return NOTIFY_OK;
  114. /* fixup bad _DCK function that rewrites
  115. * secondary bridge on slot
  116. */
  117. pci_read_config_dword(bus->self,
  118. PCI_PRIMARY_BUS,
  119. &buses);
  120. if (((buses >> 8) & 0xff) != bus->busn_res.start) {
  121. buses = (buses & 0xff000000)
  122. | ((unsigned int)(bus->primary) << 0)
  123. | ((unsigned int)(bus->busn_res.start) << 8)
  124. | ((unsigned int)(bus->busn_res.end) << 16);
  125. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  126. }
  127. return NOTIFY_OK;
  128. }
  129. static const struct acpi_dock_ops acpiphp_dock_ops = {
  130. .handler = hotplug_event_func,
  131. };
  132. /* Check whether the PCI device is managed by native PCIe hotplug driver */
  133. static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
  134. {
  135. u32 reg32;
  136. acpi_handle tmp;
  137. struct acpi_pci_root *root;
  138. /* Check whether the PCIe port supports native PCIe hotplug */
  139. if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
  140. return false;
  141. if (!(reg32 & PCI_EXP_SLTCAP_HPC))
  142. return false;
  143. /*
  144. * Check whether native PCIe hotplug has been enabled for
  145. * this PCIe hierarchy.
  146. */
  147. tmp = acpi_find_root_bridge_handle(pdev);
  148. if (!tmp)
  149. return false;
  150. root = acpi_pci_find_root(tmp);
  151. if (!root)
  152. return false;
  153. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  154. return false;
  155. return true;
  156. }
  157. static void acpiphp_dock_init(void *data)
  158. {
  159. struct acpiphp_func *func = data;
  160. get_bridge(func->slot->bridge);
  161. }
  162. static void acpiphp_dock_release(void *data)
  163. {
  164. struct acpiphp_func *func = data;
  165. put_bridge(func->slot->bridge);
  166. }
  167. /* callback routine to register each ACPI PCI slot object */
  168. static acpi_status
  169. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  170. {
  171. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
  172. struct acpiphp_slot *slot;
  173. struct acpiphp_func *newfunc;
  174. acpi_handle tmp;
  175. acpi_status status = AE_OK;
  176. unsigned long long adr, sun;
  177. int device, function, retval, found = 0;
  178. struct pci_bus *pbus = bridge->pci_bus;
  179. struct pci_dev *pdev;
  180. u32 val;
  181. if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
  182. return AE_OK;
  183. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  184. if (ACPI_FAILURE(status)) {
  185. warn("can't evaluate _ADR (%#x)\n", status);
  186. return AE_OK;
  187. }
  188. device = (adr >> 16) & 0xffff;
  189. function = adr & 0xffff;
  190. pdev = bridge->pci_dev;
  191. if (pdev && device_is_managed_by_native_pciehp(pdev))
  192. return AE_OK;
  193. newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
  194. if (!newfunc)
  195. return AE_NO_MEMORY;
  196. newfunc->handle = handle;
  197. newfunc->function = function;
  198. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  199. newfunc->flags = FUNC_HAS_EJ0;
  200. if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
  201. newfunc->flags |= FUNC_HAS_STA;
  202. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
  203. newfunc->flags |= FUNC_HAS_PS0;
  204. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
  205. newfunc->flags |= FUNC_HAS_PS3;
  206. if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
  207. newfunc->flags |= FUNC_HAS_DCK;
  208. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  209. if (ACPI_FAILURE(status)) {
  210. /*
  211. * use the count of the number of slots we've found
  212. * for the number of the slot
  213. */
  214. sun = bridge->nr_slots+1;
  215. }
  216. /* search for objects that share the same slot */
  217. list_for_each_entry(slot, &bridge->slots, node)
  218. if (slot->device == device) {
  219. if (slot->sun != sun)
  220. warn("sibling found, but _SUN doesn't match!\n");
  221. found = 1;
  222. break;
  223. }
  224. if (!found) {
  225. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  226. if (!slot) {
  227. kfree(newfunc);
  228. return AE_NO_MEMORY;
  229. }
  230. slot->bridge = bridge;
  231. slot->device = device;
  232. slot->sun = sun;
  233. INIT_LIST_HEAD(&slot->funcs);
  234. mutex_init(&slot->crit_sect);
  235. mutex_lock(&bridge_mutex);
  236. list_add_tail(&slot->node, &bridge->slots);
  237. mutex_unlock(&bridge_mutex);
  238. bridge->nr_slots++;
  239. dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  240. slot->sun, pci_domain_nr(pbus), pbus->number, device);
  241. retval = acpiphp_register_hotplug_slot(slot);
  242. if (retval) {
  243. if (retval == -EBUSY)
  244. warn("Slot %llu already registered by another "
  245. "hotplug driver\n", slot->sun);
  246. else
  247. warn("acpiphp_register_hotplug_slot failed "
  248. "(err code = 0x%x)\n", retval);
  249. goto err_exit;
  250. }
  251. }
  252. newfunc->slot = slot;
  253. mutex_lock(&bridge_mutex);
  254. list_add_tail(&newfunc->sibling, &slot->funcs);
  255. mutex_unlock(&bridge_mutex);
  256. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  257. &val, 60*1000))
  258. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  259. if (is_dock_device(handle)) {
  260. /* we don't want to call this device's _EJ0
  261. * because we want the dock notify handler
  262. * to call it after it calls _DCK
  263. */
  264. newfunc->flags &= ~FUNC_HAS_EJ0;
  265. if (register_hotplug_dock_device(handle,
  266. &acpiphp_dock_ops, newfunc,
  267. acpiphp_dock_init, acpiphp_dock_release))
  268. dbg("failed to register dock device\n");
  269. /* we need to be notified when dock events happen
  270. * outside of the hotplug operation, since we may
  271. * need to do fixups before we can hotplug.
  272. */
  273. newfunc->nb.notifier_call = post_dock_fixups;
  274. if (register_dock_notifier(&newfunc->nb))
  275. dbg("failed to register a dock notifier");
  276. }
  277. /* install notify handler */
  278. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  279. status = acpi_install_notify_handler(handle,
  280. ACPI_SYSTEM_NOTIFY,
  281. handle_hotplug_event_func,
  282. newfunc);
  283. if (ACPI_FAILURE(status))
  284. err("failed to register interrupt notify handler\n");
  285. } else
  286. status = AE_OK;
  287. return status;
  288. err_exit:
  289. bridge->nr_slots--;
  290. mutex_lock(&bridge_mutex);
  291. list_del(&slot->node);
  292. mutex_unlock(&bridge_mutex);
  293. kfree(slot);
  294. kfree(newfunc);
  295. return AE_OK;
  296. }
  297. /* see if it's worth looking at this bridge */
  298. static int detect_ejectable_slots(acpi_handle handle)
  299. {
  300. int found = acpi_pci_detect_ejectable(handle);
  301. if (!found) {
  302. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  303. is_pci_dock_device, NULL, (void *)&found, NULL);
  304. }
  305. return found;
  306. }
  307. /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
  308. static void init_bridge_misc(struct acpiphp_bridge *bridge)
  309. {
  310. acpi_status status;
  311. /* must be added to the list prior to calling register_slot */
  312. mutex_lock(&bridge_mutex);
  313. list_add(&bridge->list, &bridge_list);
  314. mutex_unlock(&bridge_mutex);
  315. /* register all slot objects under this bridge */
  316. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
  317. register_slot, NULL, bridge, NULL);
  318. if (ACPI_FAILURE(status)) {
  319. mutex_lock(&bridge_mutex);
  320. list_del(&bridge->list);
  321. mutex_unlock(&bridge_mutex);
  322. return;
  323. }
  324. /* install notify handler for P2P bridges */
  325. if (!pci_is_root_bus(bridge->pci_bus)) {
  326. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  327. status = acpi_remove_notify_handler(bridge->func->handle,
  328. ACPI_SYSTEM_NOTIFY,
  329. handle_hotplug_event_func);
  330. if (ACPI_FAILURE(status))
  331. err("failed to remove notify handler\n");
  332. }
  333. status = acpi_install_notify_handler(bridge->handle,
  334. ACPI_SYSTEM_NOTIFY,
  335. handle_hotplug_event_bridge,
  336. bridge);
  337. if (ACPI_FAILURE(status)) {
  338. err("failed to register interrupt notify handler\n");
  339. }
  340. }
  341. }
  342. /* find acpiphp_func from acpiphp_bridge */
  343. static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
  344. {
  345. struct acpiphp_bridge *bridge;
  346. struct acpiphp_slot *slot;
  347. struct acpiphp_func *func = NULL;
  348. mutex_lock(&bridge_mutex);
  349. list_for_each_entry(bridge, &bridge_list, list) {
  350. list_for_each_entry(slot, &bridge->slots, node) {
  351. list_for_each_entry(func, &slot->funcs, sibling) {
  352. if (func->handle == handle) {
  353. get_bridge(func->slot->bridge);
  354. mutex_unlock(&bridge_mutex);
  355. return func;
  356. }
  357. }
  358. }
  359. }
  360. mutex_unlock(&bridge_mutex);
  361. return NULL;
  362. }
  363. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  364. {
  365. struct acpiphp_bridge *bridge;
  366. mutex_lock(&bridge_mutex);
  367. list_for_each_entry(bridge, &bridge_list, list)
  368. if (bridge->handle == handle) {
  369. get_bridge(bridge);
  370. mutex_unlock(&bridge_mutex);
  371. return bridge;
  372. }
  373. mutex_unlock(&bridge_mutex);
  374. return NULL;
  375. }
  376. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  377. {
  378. struct acpiphp_slot *slot;
  379. struct acpiphp_func *func;
  380. acpi_status status;
  381. acpi_handle handle = bridge->handle;
  382. if (!pci_is_root_bus(bridge->pci_bus)) {
  383. status = acpi_remove_notify_handler(handle,
  384. ACPI_SYSTEM_NOTIFY,
  385. handle_hotplug_event_bridge);
  386. if (ACPI_FAILURE(status))
  387. err("failed to remove notify handler\n");
  388. }
  389. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  390. status = acpi_install_notify_handler(bridge->func->handle,
  391. ACPI_SYSTEM_NOTIFY,
  392. handle_hotplug_event_func,
  393. bridge->func);
  394. if (ACPI_FAILURE(status))
  395. err("failed to install interrupt notify handler\n");
  396. }
  397. list_for_each_entry(slot, &bridge->slots, node) {
  398. list_for_each_entry(func, &slot->funcs, sibling) {
  399. if (is_dock_device(func->handle)) {
  400. unregister_hotplug_dock_device(func->handle);
  401. unregister_dock_notifier(&func->nb);
  402. }
  403. if (!(func->flags & FUNC_HAS_DCK)) {
  404. status = acpi_remove_notify_handler(func->handle,
  405. ACPI_SYSTEM_NOTIFY,
  406. handle_hotplug_event_func);
  407. if (ACPI_FAILURE(status))
  408. err("failed to remove notify handler\n");
  409. }
  410. }
  411. acpiphp_unregister_hotplug_slot(slot);
  412. }
  413. mutex_lock(&bridge_mutex);
  414. list_del(&bridge->list);
  415. mutex_unlock(&bridge_mutex);
  416. }
  417. static int power_on_slot(struct acpiphp_slot *slot)
  418. {
  419. acpi_status status;
  420. struct acpiphp_func *func;
  421. int retval = 0;
  422. /* if already enabled, just skip */
  423. if (slot->flags & SLOT_POWEREDON)
  424. goto err_exit;
  425. list_for_each_entry(func, &slot->funcs, sibling) {
  426. if (func->flags & FUNC_HAS_PS0) {
  427. dbg("%s: executing _PS0\n", __func__);
  428. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  429. if (ACPI_FAILURE(status)) {
  430. warn("%s: _PS0 failed\n", __func__);
  431. retval = -1;
  432. goto err_exit;
  433. } else
  434. break;
  435. }
  436. }
  437. /* TBD: evaluate _STA to check if the slot is enabled */
  438. slot->flags |= SLOT_POWEREDON;
  439. err_exit:
  440. return retval;
  441. }
  442. static int power_off_slot(struct acpiphp_slot *slot)
  443. {
  444. acpi_status status;
  445. struct acpiphp_func *func;
  446. int retval = 0;
  447. /* if already disabled, just skip */
  448. if ((slot->flags & SLOT_POWEREDON) == 0)
  449. goto err_exit;
  450. list_for_each_entry(func, &slot->funcs, sibling) {
  451. if (func->flags & FUNC_HAS_PS3) {
  452. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  453. if (ACPI_FAILURE(status)) {
  454. warn("%s: _PS3 failed\n", __func__);
  455. retval = -1;
  456. goto err_exit;
  457. } else
  458. break;
  459. }
  460. }
  461. /* TBD: evaluate _STA to check if the slot is disabled */
  462. slot->flags &= (~SLOT_POWEREDON);
  463. err_exit:
  464. return retval;
  465. }
  466. /**
  467. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  468. * @bus: bus to start search with
  469. */
  470. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  471. {
  472. struct list_head *tmp;
  473. unsigned char max, n;
  474. /*
  475. * pci_bus_max_busnr will return the highest
  476. * reserved busnr for all these children.
  477. * that is equivalent to the bus->subordinate
  478. * value. We don't want to use the parent's
  479. * bus->subordinate value because it could have
  480. * padding in it.
  481. */
  482. max = bus->busn_res.start;
  483. list_for_each(tmp, &bus->children) {
  484. n = pci_bus_max_busnr(pci_bus_b(tmp));
  485. if (n > max)
  486. max = n;
  487. }
  488. return max;
  489. }
  490. /**
  491. * acpiphp_bus_add - add a new bus to acpi subsystem
  492. * @func: acpiphp_func of the bridge
  493. */
  494. static int acpiphp_bus_add(struct acpiphp_func *func)
  495. {
  496. struct acpi_device *device;
  497. int ret_val;
  498. if (!acpi_bus_get_device(func->handle, &device)) {
  499. dbg("bus exists... trim\n");
  500. /* this shouldn't be in here, so remove
  501. * the bus then re-add it...
  502. */
  503. acpi_bus_trim(device);
  504. }
  505. ret_val = acpi_bus_scan(func->handle);
  506. if (!ret_val)
  507. ret_val = acpi_bus_get_device(func->handle, &device);
  508. if (ret_val)
  509. dbg("error adding bus, %x\n", -ret_val);
  510. return ret_val;
  511. }
  512. /**
  513. * acpiphp_bus_trim - trim a bus from acpi subsystem
  514. * @handle: handle to acpi namespace
  515. */
  516. static int acpiphp_bus_trim(acpi_handle handle)
  517. {
  518. struct acpi_device *device;
  519. int retval;
  520. retval = acpi_bus_get_device(handle, &device);
  521. if (retval) {
  522. dbg("acpi_device not found\n");
  523. return retval;
  524. }
  525. acpi_bus_trim(device);
  526. return 0;
  527. }
  528. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  529. {
  530. struct acpiphp_func *func;
  531. union acpi_object params[2];
  532. struct acpi_object_list arg_list;
  533. list_for_each_entry(func, &slot->funcs, sibling) {
  534. arg_list.count = 2;
  535. arg_list.pointer = params;
  536. params[0].type = ACPI_TYPE_INTEGER;
  537. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  538. params[1].type = ACPI_TYPE_INTEGER;
  539. params[1].integer.value = 1;
  540. /* _REG is optional, we don't care about if there is failure */
  541. acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
  542. }
  543. }
  544. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  545. {
  546. struct acpiphp_func *func;
  547. if (!dev->subordinate)
  548. return;
  549. /* quirk, or pcie could set it already */
  550. if (dev->is_hotplug_bridge)
  551. return;
  552. if (PCI_SLOT(dev->devfn) != slot->device)
  553. return;
  554. list_for_each_entry(func, &slot->funcs, sibling) {
  555. if (PCI_FUNC(dev->devfn) == func->function) {
  556. /* check if this bridge has ejectable slots */
  557. if ((detect_ejectable_slots(func->handle) > 0))
  558. dev->is_hotplug_bridge = 1;
  559. break;
  560. }
  561. }
  562. }
  563. /**
  564. * enable_device - enable, configure a slot
  565. * @slot: slot to be enabled
  566. *
  567. * This function should be called per *physical slot*,
  568. * not per each slot object in ACPI namespace.
  569. */
  570. static int __ref enable_device(struct acpiphp_slot *slot)
  571. {
  572. struct pci_dev *dev;
  573. struct pci_bus *bus = slot->bridge->pci_bus;
  574. struct acpiphp_func *func;
  575. int num, max, pass;
  576. LIST_HEAD(add_list);
  577. if (slot->flags & SLOT_ENABLED)
  578. goto err_exit;
  579. list_for_each_entry(func, &slot->funcs, sibling)
  580. acpiphp_bus_add(func);
  581. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  582. if (num == 0) {
  583. /* Maybe only part of funcs are added. */
  584. dbg("No new device found\n");
  585. goto err_exit;
  586. }
  587. max = acpiphp_max_busnr(bus);
  588. for (pass = 0; pass < 2; pass++) {
  589. list_for_each_entry(dev, &bus->devices, bus_list) {
  590. if (PCI_SLOT(dev->devfn) != slot->device)
  591. continue;
  592. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  593. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  594. max = pci_scan_bridge(bus, dev, max, pass);
  595. if (pass && dev->subordinate) {
  596. check_hotplug_bridge(slot, dev);
  597. pcibios_resource_survey_bus(dev->subordinate);
  598. __pci_bus_size_bridges(dev->subordinate,
  599. &add_list);
  600. }
  601. }
  602. }
  603. }
  604. __pci_bus_assign_resources(bus, &add_list, NULL);
  605. acpiphp_sanitize_bus(bus);
  606. acpiphp_set_hpp_values(bus);
  607. acpiphp_set_acpi_region(slot);
  608. list_for_each_entry(dev, &bus->devices, bus_list) {
  609. /* Assume that newly added devices are powered on already. */
  610. if (!dev->is_added)
  611. dev->current_state = PCI_D0;
  612. }
  613. pci_bus_add_devices(bus);
  614. slot->flags |= SLOT_ENABLED;
  615. list_for_each_entry(func, &slot->funcs, sibling) {
  616. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  617. func->function));
  618. if (!dev) {
  619. /* Do not set SLOT_ENABLED flag if some funcs
  620. are not added. */
  621. slot->flags &= (~SLOT_ENABLED);
  622. continue;
  623. }
  624. }
  625. err_exit:
  626. return 0;
  627. }
  628. /* return first device in slot, acquiring a reference on it */
  629. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  630. {
  631. struct pci_bus *bus = slot->bridge->pci_bus;
  632. struct pci_dev *dev;
  633. struct pci_dev *ret = NULL;
  634. down_read(&pci_bus_sem);
  635. list_for_each_entry(dev, &bus->devices, bus_list)
  636. if (PCI_SLOT(dev->devfn) == slot->device) {
  637. ret = pci_dev_get(dev);
  638. break;
  639. }
  640. up_read(&pci_bus_sem);
  641. return ret;
  642. }
  643. /**
  644. * disable_device - disable a slot
  645. * @slot: ACPI PHP slot
  646. */
  647. static int disable_device(struct acpiphp_slot *slot)
  648. {
  649. struct acpiphp_func *func;
  650. struct pci_dev *pdev;
  651. /*
  652. * enable_device() enumerates all functions in this device via
  653. * pci_scan_slot(), whether they have associated ACPI hotplug
  654. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  655. * here.
  656. */
  657. while ((pdev = dev_in_slot(slot))) {
  658. pci_stop_and_remove_bus_device(pdev);
  659. pci_dev_put(pdev);
  660. }
  661. list_for_each_entry(func, &slot->funcs, sibling) {
  662. acpiphp_bus_trim(func->handle);
  663. }
  664. slot->flags &= (~SLOT_ENABLED);
  665. return 0;
  666. }
  667. /**
  668. * get_slot_status - get ACPI slot status
  669. * @slot: ACPI PHP slot
  670. *
  671. * If a slot has _STA for each function and if any one of them
  672. * returned non-zero status, return it.
  673. *
  674. * If a slot doesn't have _STA and if any one of its functions'
  675. * configuration space is configured, return 0x0f as a _STA.
  676. *
  677. * Otherwise return 0.
  678. */
  679. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  680. {
  681. acpi_status status;
  682. unsigned long long sta = 0;
  683. u32 dvid;
  684. struct acpiphp_func *func;
  685. list_for_each_entry(func, &slot->funcs, sibling) {
  686. if (func->flags & FUNC_HAS_STA) {
  687. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  688. if (ACPI_SUCCESS(status) && sta)
  689. break;
  690. } else {
  691. pci_bus_read_config_dword(slot->bridge->pci_bus,
  692. PCI_DEVFN(slot->device,
  693. func->function),
  694. PCI_VENDOR_ID, &dvid);
  695. if (dvid != 0xffffffff) {
  696. sta = ACPI_STA_ALL;
  697. break;
  698. }
  699. }
  700. }
  701. return (unsigned int)sta;
  702. }
  703. /**
  704. * acpiphp_eject_slot - physically eject the slot
  705. * @slot: ACPI PHP slot
  706. */
  707. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  708. {
  709. acpi_status status;
  710. struct acpiphp_func *func;
  711. struct acpi_object_list arg_list;
  712. union acpi_object arg;
  713. list_for_each_entry(func, &slot->funcs, sibling) {
  714. /* We don't want to call _EJ0 on non-existing functions. */
  715. if ((func->flags & FUNC_HAS_EJ0)) {
  716. /* _EJ0 method take one argument */
  717. arg_list.count = 1;
  718. arg_list.pointer = &arg;
  719. arg.type = ACPI_TYPE_INTEGER;
  720. arg.integer.value = 1;
  721. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  722. if (ACPI_FAILURE(status)) {
  723. warn("%s: _EJ0 failed\n", __func__);
  724. return -1;
  725. } else
  726. break;
  727. }
  728. }
  729. return 0;
  730. }
  731. /**
  732. * acpiphp_check_bridge - re-enumerate devices
  733. * @bridge: where to begin re-enumeration
  734. *
  735. * Iterate over all slots under this bridge and make sure that if a
  736. * card is present they are enabled, and if not they are disabled.
  737. */
  738. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  739. {
  740. struct acpiphp_slot *slot;
  741. int retval = 0;
  742. int enabled, disabled;
  743. enabled = disabled = 0;
  744. list_for_each_entry(slot, &bridge->slots, node) {
  745. unsigned int status = get_slot_status(slot);
  746. if (slot->flags & SLOT_ENABLED) {
  747. if (status == ACPI_STA_ALL)
  748. continue;
  749. retval = acpiphp_disable_slot(slot);
  750. if (retval) {
  751. err("Error occurred in disabling\n");
  752. goto err_exit;
  753. } else {
  754. acpiphp_eject_slot(slot);
  755. }
  756. disabled++;
  757. } else {
  758. if (status != ACPI_STA_ALL)
  759. continue;
  760. retval = acpiphp_enable_slot(slot);
  761. if (retval) {
  762. err("Error occurred in enabling\n");
  763. goto err_exit;
  764. }
  765. enabled++;
  766. }
  767. }
  768. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  769. err_exit:
  770. return retval;
  771. }
  772. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  773. {
  774. struct pci_dev *dev;
  775. list_for_each_entry(dev, &bus->devices, bus_list)
  776. pci_configure_slot(dev);
  777. }
  778. /*
  779. * Remove devices for which we could not assign resources, call
  780. * arch specific code to fix-up the bus
  781. */
  782. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  783. {
  784. struct pci_dev *dev, *tmp;
  785. int i;
  786. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  787. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  788. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  789. struct resource *res = &dev->resource[i];
  790. if ((res->flags & type_mask) && !res->start &&
  791. res->end) {
  792. /* Could not assign a required resources
  793. * for this device, remove it */
  794. pci_stop_and_remove_bus_device(dev);
  795. break;
  796. }
  797. }
  798. }
  799. }
  800. /*
  801. * ACPI event handlers
  802. */
  803. static acpi_status
  804. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  805. {
  806. struct acpiphp_bridge *bridge;
  807. char objname[64];
  808. struct acpi_buffer buffer = { .length = sizeof(objname),
  809. .pointer = objname };
  810. bridge = acpiphp_handle_to_bridge(handle);
  811. if (bridge) {
  812. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  813. dbg("%s: re-enumerating slots under %s\n",
  814. __func__, objname);
  815. acpiphp_check_bridge(bridge);
  816. put_bridge(bridge);
  817. }
  818. return AE_OK ;
  819. }
  820. void acpiphp_check_host_bridge(acpi_handle handle)
  821. {
  822. struct acpiphp_bridge *bridge;
  823. bridge = acpiphp_handle_to_bridge(handle);
  824. if (bridge) {
  825. acpiphp_check_bridge(bridge);
  826. put_bridge(bridge);
  827. }
  828. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  829. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  830. }
  831. static void _handle_hotplug_event_bridge(struct work_struct *work)
  832. {
  833. struct acpiphp_bridge *bridge;
  834. char objname[64];
  835. struct acpi_buffer buffer = { .length = sizeof(objname),
  836. .pointer = objname };
  837. struct acpi_hp_work *hp_work;
  838. acpi_handle handle;
  839. u32 type;
  840. hp_work = container_of(work, struct acpi_hp_work, work);
  841. handle = hp_work->handle;
  842. type = hp_work->type;
  843. bridge = (struct acpiphp_bridge *)hp_work->context;
  844. acpi_scan_lock_acquire();
  845. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  846. switch (type) {
  847. case ACPI_NOTIFY_BUS_CHECK:
  848. /* bus re-enumerate */
  849. dbg("%s: Bus check notify on %s\n", __func__, objname);
  850. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  851. acpiphp_check_bridge(bridge);
  852. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  853. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  854. break;
  855. case ACPI_NOTIFY_DEVICE_CHECK:
  856. /* device check */
  857. dbg("%s: Device check notify on %s\n", __func__, objname);
  858. acpiphp_check_bridge(bridge);
  859. break;
  860. case ACPI_NOTIFY_DEVICE_WAKE:
  861. /* wake event */
  862. dbg("%s: Device wake notify on %s\n", __func__, objname);
  863. break;
  864. case ACPI_NOTIFY_EJECT_REQUEST:
  865. /* request device eject */
  866. dbg("%s: Device eject notify on %s\n", __func__, objname);
  867. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  868. struct acpiphp_slot *slot;
  869. slot = bridge->func->slot;
  870. if (!acpiphp_disable_slot(slot))
  871. acpiphp_eject_slot(slot);
  872. }
  873. break;
  874. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  875. printk(KERN_ERR "Device %s cannot be configured due"
  876. " to a frequency mismatch\n", objname);
  877. break;
  878. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  879. printk(KERN_ERR "Device %s cannot be configured due"
  880. " to a bus mode mismatch\n", objname);
  881. break;
  882. case ACPI_NOTIFY_POWER_FAULT:
  883. printk(KERN_ERR "Device %s has suffered a power fault\n",
  884. objname);
  885. break;
  886. default:
  887. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  888. break;
  889. }
  890. acpi_scan_lock_release();
  891. kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
  892. put_bridge(bridge);
  893. }
  894. /**
  895. * handle_hotplug_event_bridge - handle ACPI event on bridges
  896. * @handle: Notify()'ed acpi_handle
  897. * @type: Notify code
  898. * @context: pointer to acpiphp_bridge structure
  899. *
  900. * Handles ACPI event notification on {host,p2p} bridges.
  901. */
  902. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
  903. void *context)
  904. {
  905. struct acpiphp_bridge *bridge = context;
  906. /*
  907. * Currently the code adds all hotplug events to the kacpid_wq
  908. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  909. * The proper way to fix this is to reorganize the code so that
  910. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  911. * For now just re-add this work to the kacpi_hotplug_wq so we
  912. * don't deadlock on hotplug actions.
  913. */
  914. get_bridge(bridge);
  915. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
  916. }
  917. static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
  918. {
  919. struct acpiphp_func *func = context;
  920. char objname[64];
  921. struct acpi_buffer buffer = { .length = sizeof(objname),
  922. .pointer = objname };
  923. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  924. switch (type) {
  925. case ACPI_NOTIFY_BUS_CHECK:
  926. /* bus re-enumerate */
  927. dbg("%s: Bus check notify on %s\n", __func__, objname);
  928. acpiphp_enable_slot(func->slot);
  929. break;
  930. case ACPI_NOTIFY_DEVICE_CHECK:
  931. /* device check : re-enumerate from parent bus */
  932. dbg("%s: Device check notify on %s\n", __func__, objname);
  933. acpiphp_check_bridge(func->slot->bridge);
  934. break;
  935. case ACPI_NOTIFY_DEVICE_WAKE:
  936. /* wake event */
  937. dbg("%s: Device wake notify on %s\n", __func__, objname);
  938. break;
  939. case ACPI_NOTIFY_EJECT_REQUEST:
  940. /* request device eject */
  941. dbg("%s: Device eject notify on %s\n", __func__, objname);
  942. if (!(acpiphp_disable_slot(func->slot)))
  943. acpiphp_eject_slot(func->slot);
  944. break;
  945. default:
  946. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  947. break;
  948. }
  949. }
  950. static void _handle_hotplug_event_func(struct work_struct *work)
  951. {
  952. struct acpi_hp_work *hp_work;
  953. struct acpiphp_func *func;
  954. hp_work = container_of(work, struct acpi_hp_work, work);
  955. func = hp_work->context;
  956. acpi_scan_lock_acquire();
  957. hotplug_event_func(hp_work->handle, hp_work->type, func);
  958. acpi_scan_lock_release();
  959. kfree(hp_work); /* allocated in handle_hotplug_event_func */
  960. put_bridge(func->slot->bridge);
  961. }
  962. /**
  963. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  964. * @handle: Notify()'ed acpi_handle
  965. * @type: Notify code
  966. * @context: pointer to acpiphp_func structure
  967. *
  968. * Handles ACPI event notification on slots.
  969. */
  970. static void handle_hotplug_event_func(acpi_handle handle, u32 type,
  971. void *context)
  972. {
  973. struct acpiphp_func *func = context;
  974. /*
  975. * Currently the code adds all hotplug events to the kacpid_wq
  976. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  977. * The proper way to fix this is to reorganize the code so that
  978. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  979. * For now just re-add this work to the kacpi_hotplug_wq so we
  980. * don't deadlock on hotplug actions.
  981. */
  982. get_bridge(func->slot->bridge);
  983. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
  984. }
  985. /*
  986. * Create hotplug slots for the PCI bus.
  987. * It should always return 0 to avoid skipping following notifiers.
  988. */
  989. void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
  990. {
  991. acpi_handle dummy_handle;
  992. struct acpiphp_bridge *bridge;
  993. if (acpiphp_disabled)
  994. return;
  995. if (detect_ejectable_slots(handle) <= 0)
  996. return;
  997. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  998. if (bridge == NULL) {
  999. err("out of memory\n");
  1000. return;
  1001. }
  1002. INIT_LIST_HEAD(&bridge->slots);
  1003. kref_init(&bridge->ref);
  1004. bridge->handle = handle;
  1005. bridge->pci_dev = pci_dev_get(bus->self);
  1006. bridge->pci_bus = bus;
  1007. /*
  1008. * Grab a ref to the subordinate PCI bus in case the bus is
  1009. * removed via PCI core logical hotplug. The ref pins the bus
  1010. * (which we access during module unload).
  1011. */
  1012. get_device(&bus->dev);
  1013. if (!pci_is_root_bus(bridge->pci_bus) &&
  1014. ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  1015. "_EJ0", &dummy_handle))) {
  1016. dbg("found ejectable p2p bridge\n");
  1017. bridge->flags |= BRIDGE_HAS_EJ0;
  1018. bridge->func = acpiphp_bridge_handle_to_function(handle);
  1019. }
  1020. init_bridge_misc(bridge);
  1021. }
  1022. /* Destroy hotplug slots associated with the PCI bus */
  1023. void acpiphp_remove_slots(struct pci_bus *bus)
  1024. {
  1025. struct acpiphp_bridge *bridge, *tmp;
  1026. if (acpiphp_disabled)
  1027. return;
  1028. list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
  1029. if (bridge->pci_bus == bus) {
  1030. cleanup_bridge(bridge);
  1031. put_bridge(bridge);
  1032. break;
  1033. }
  1034. }
  1035. /**
  1036. * acpiphp_enable_slot - power on slot
  1037. * @slot: ACPI PHP slot
  1038. */
  1039. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1040. {
  1041. int retval;
  1042. mutex_lock(&slot->crit_sect);
  1043. /* wake up all functions */
  1044. retval = power_on_slot(slot);
  1045. if (retval)
  1046. goto err_exit;
  1047. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1048. /* configure all functions */
  1049. retval = enable_device(slot);
  1050. if (retval)
  1051. power_off_slot(slot);
  1052. } else {
  1053. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1054. power_off_slot(slot);
  1055. }
  1056. err_exit:
  1057. mutex_unlock(&slot->crit_sect);
  1058. return retval;
  1059. }
  1060. /**
  1061. * acpiphp_disable_slot - power off slot
  1062. * @slot: ACPI PHP slot
  1063. */
  1064. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1065. {
  1066. int retval = 0;
  1067. mutex_lock(&slot->crit_sect);
  1068. /* unconfigure all functions */
  1069. retval = disable_device(slot);
  1070. if (retval)
  1071. goto err_exit;
  1072. /* power off all functions */
  1073. retval = power_off_slot(slot);
  1074. if (retval)
  1075. goto err_exit;
  1076. err_exit:
  1077. mutex_unlock(&slot->crit_sect);
  1078. return retval;
  1079. }
  1080. /*
  1081. * slot enabled: 1
  1082. * slot disabled: 0
  1083. */
  1084. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1085. {
  1086. return (slot->flags & SLOT_POWEREDON);
  1087. }
  1088. /*
  1089. * latch open: 1
  1090. * latch closed: 0
  1091. */
  1092. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1093. {
  1094. unsigned int sta;
  1095. sta = get_slot_status(slot);
  1096. return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
  1097. }
  1098. /*
  1099. * adapter presence : 1
  1100. * absence : 0
  1101. */
  1102. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1103. {
  1104. unsigned int sta;
  1105. sta = get_slot_status(slot);
  1106. return (sta == 0) ? 0 : 1;
  1107. }