acpiphp_glue.c 38 KB

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