acpiphp_glue.c 35 KB

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