acpiphp_glue.c 38 KB

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