acpiphp_glue.c 38 KB

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