acpiphp_glue.c 38 KB

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