acpiphp_glue.c 38 KB

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