acpiphp_glue.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  619. {
  620. struct acpiphp_func *func;
  621. union acpi_object params[2];
  622. struct acpi_object_list arg_list;
  623. list_for_each_entry(func, &slot->funcs, sibling) {
  624. arg_list.count = 2;
  625. arg_list.pointer = params;
  626. params[0].type = ACPI_TYPE_INTEGER;
  627. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  628. params[1].type = ACPI_TYPE_INTEGER;
  629. params[1].integer.value = 1;
  630. /* _REG is optional, we don't care about if there is failure */
  631. acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
  632. }
  633. }
  634. /**
  635. * enable_device - enable, configure a slot
  636. * @slot: slot to be enabled
  637. *
  638. * This function should be called per *physical slot*,
  639. * not per each slot object in ACPI namespace.
  640. */
  641. static int __ref enable_device(struct acpiphp_slot *slot)
  642. {
  643. struct pci_dev *dev;
  644. struct pci_bus *bus = slot->bridge->pci_bus;
  645. struct acpiphp_func *func;
  646. int retval = 0;
  647. int num, max, pass;
  648. acpi_status status;
  649. if (slot->flags & SLOT_ENABLED)
  650. goto err_exit;
  651. /* sanity check: dev should be NULL when hot-plugged in */
  652. dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  653. if (dev) {
  654. /* This case shouldn't happen */
  655. err("pci_dev structure already exists.\n");
  656. pci_dev_put(dev);
  657. retval = -1;
  658. goto err_exit;
  659. }
  660. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  661. if (num == 0) {
  662. err("No new device found\n");
  663. retval = -1;
  664. goto err_exit;
  665. }
  666. max = acpiphp_max_busnr(bus);
  667. for (pass = 0; pass < 2; pass++) {
  668. list_for_each_entry(dev, &bus->devices, bus_list) {
  669. if (PCI_SLOT(dev->devfn) != slot->device)
  670. continue;
  671. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  672. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  673. max = pci_scan_bridge(bus, dev, max, pass);
  674. if (pass && dev->subordinate)
  675. pci_bus_size_bridges(dev->subordinate);
  676. }
  677. }
  678. }
  679. list_for_each_entry(func, &slot->funcs, sibling)
  680. acpiphp_bus_add(func);
  681. pci_bus_assign_resources(bus);
  682. acpiphp_sanitize_bus(bus);
  683. acpiphp_set_hpp_values(bus);
  684. acpiphp_set_acpi_region(slot);
  685. pci_enable_bridges(bus);
  686. pci_bus_add_devices(bus);
  687. list_for_each_entry(func, &slot->funcs, sibling) {
  688. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  689. func->function));
  690. if (!dev)
  691. continue;
  692. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
  693. dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
  694. pci_dev_put(dev);
  695. continue;
  696. }
  697. status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
  698. if (ACPI_FAILURE(status))
  699. warn("find_p2p_bridge failed (error code = 0x%x)\n",
  700. status);
  701. pci_dev_put(dev);
  702. }
  703. slot->flags |= SLOT_ENABLED;
  704. err_exit:
  705. return retval;
  706. }
  707. static void disable_bridges(struct pci_bus *bus)
  708. {
  709. struct pci_dev *dev;
  710. list_for_each_entry(dev, &bus->devices, bus_list) {
  711. if (dev->subordinate) {
  712. disable_bridges(dev->subordinate);
  713. pci_disable_device(dev);
  714. }
  715. }
  716. }
  717. /**
  718. * disable_device - disable a slot
  719. * @slot: ACPI PHP slot
  720. */
  721. static int disable_device(struct acpiphp_slot *slot)
  722. {
  723. struct acpiphp_func *func;
  724. struct pci_dev *pdev;
  725. /* is this slot already disabled? */
  726. if (!(slot->flags & SLOT_ENABLED))
  727. goto err_exit;
  728. list_for_each_entry(func, &slot->funcs, sibling) {
  729. if (func->bridge) {
  730. /* cleanup p2p bridges under this P2P bridge */
  731. cleanup_p2p_bridge(func->bridge->handle,
  732. (u32)1, NULL, NULL);
  733. func->bridge = NULL;
  734. }
  735. pdev = pci_get_slot(slot->bridge->pci_bus,
  736. PCI_DEVFN(slot->device, func->function));
  737. if (pdev) {
  738. pci_stop_bus_device(pdev);
  739. if (pdev->subordinate) {
  740. disable_bridges(pdev->subordinate);
  741. pci_disable_device(pdev);
  742. }
  743. pci_remove_bus_device(pdev);
  744. pci_dev_put(pdev);
  745. }
  746. }
  747. list_for_each_entry(func, &slot->funcs, sibling) {
  748. acpiphp_bus_trim(func->handle);
  749. }
  750. slot->flags &= (~SLOT_ENABLED);
  751. err_exit:
  752. return 0;
  753. }
  754. /**
  755. * get_slot_status - get ACPI slot status
  756. * @slot: ACPI PHP slot
  757. *
  758. * If a slot has _STA for each function and if any one of them
  759. * returned non-zero status, return it.
  760. *
  761. * If a slot doesn't have _STA and if any one of its functions'
  762. * configuration space is configured, return 0x0f as a _STA.
  763. *
  764. * Otherwise return 0.
  765. */
  766. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  767. {
  768. acpi_status status;
  769. unsigned long long sta = 0;
  770. u32 dvid;
  771. struct acpiphp_func *func;
  772. list_for_each_entry(func, &slot->funcs, sibling) {
  773. if (func->flags & FUNC_HAS_STA) {
  774. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  775. if (ACPI_SUCCESS(status) && sta)
  776. break;
  777. } else {
  778. pci_bus_read_config_dword(slot->bridge->pci_bus,
  779. PCI_DEVFN(slot->device,
  780. func->function),
  781. PCI_VENDOR_ID, &dvid);
  782. if (dvid != 0xffffffff) {
  783. sta = ACPI_STA_ALL;
  784. break;
  785. }
  786. }
  787. }
  788. return (unsigned int)sta;
  789. }
  790. /**
  791. * acpiphp_eject_slot - physically eject the slot
  792. * @slot: ACPI PHP slot
  793. */
  794. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  795. {
  796. acpi_status status;
  797. struct acpiphp_func *func;
  798. struct acpi_object_list arg_list;
  799. union acpi_object arg;
  800. list_for_each_entry(func, &slot->funcs, sibling) {
  801. /* We don't want to call _EJ0 on non-existing functions. */
  802. if ((func->flags & FUNC_HAS_EJ0)) {
  803. /* _EJ0 method take one argument */
  804. arg_list.count = 1;
  805. arg_list.pointer = &arg;
  806. arg.type = ACPI_TYPE_INTEGER;
  807. arg.integer.value = 1;
  808. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  809. if (ACPI_FAILURE(status)) {
  810. warn("%s: _EJ0 failed\n", __func__);
  811. return -1;
  812. } else
  813. break;
  814. }
  815. }
  816. return 0;
  817. }
  818. /**
  819. * acpiphp_check_bridge - re-enumerate devices
  820. * @bridge: where to begin re-enumeration
  821. *
  822. * Iterate over all slots under this bridge and make sure that if a
  823. * card is present they are enabled, and if not they are disabled.
  824. */
  825. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  826. {
  827. struct acpiphp_slot *slot;
  828. int retval = 0;
  829. int enabled, disabled;
  830. enabled = disabled = 0;
  831. for (slot = bridge->slots; slot; slot = slot->next) {
  832. unsigned int status = get_slot_status(slot);
  833. if (slot->flags & SLOT_ENABLED) {
  834. if (status == ACPI_STA_ALL)
  835. continue;
  836. retval = acpiphp_disable_slot(slot);
  837. if (retval) {
  838. err("Error occurred in disabling\n");
  839. goto err_exit;
  840. } else {
  841. acpiphp_eject_slot(slot);
  842. }
  843. disabled++;
  844. } else {
  845. if (status != ACPI_STA_ALL)
  846. continue;
  847. retval = acpiphp_enable_slot(slot);
  848. if (retval) {
  849. err("Error occurred in enabling\n");
  850. goto err_exit;
  851. }
  852. enabled++;
  853. }
  854. }
  855. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  856. err_exit:
  857. return retval;
  858. }
  859. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  860. {
  861. struct pci_dev *dev;
  862. list_for_each_entry(dev, &bus->devices, bus_list)
  863. pci_configure_slot(dev);
  864. }
  865. /*
  866. * Remove devices for which we could not assign resources, call
  867. * arch specific code to fix-up the bus
  868. */
  869. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  870. {
  871. struct pci_dev *dev;
  872. int i;
  873. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  874. list_for_each_entry(dev, &bus->devices, bus_list) {
  875. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  876. struct resource *res = &dev->resource[i];
  877. if ((res->flags & type_mask) && !res->start &&
  878. res->end) {
  879. /* Could not assign a required resources
  880. * for this device, remove it */
  881. pci_remove_bus_device(dev);
  882. break;
  883. }
  884. }
  885. }
  886. }
  887. /* Program resources in newly inserted bridge */
  888. static int acpiphp_configure_bridge (acpi_handle handle)
  889. {
  890. struct pci_bus *bus;
  891. if (acpi_is_root_bridge(handle)) {
  892. struct acpi_pci_root *root = acpi_pci_find_root(handle);
  893. bus = root->bus;
  894. } else {
  895. struct pci_dev *pdev = acpi_get_pci_dev(handle);
  896. bus = pdev->subordinate;
  897. pci_dev_put(pdev);
  898. }
  899. pci_bus_size_bridges(bus);
  900. pci_bus_assign_resources(bus);
  901. acpiphp_sanitize_bus(bus);
  902. acpiphp_set_hpp_values(bus);
  903. pci_enable_bridges(bus);
  904. return 0;
  905. }
  906. static void handle_bridge_insertion(acpi_handle handle, u32 type)
  907. {
  908. struct acpi_device *device, *pdevice;
  909. acpi_handle phandle;
  910. if ((type != ACPI_NOTIFY_BUS_CHECK) &&
  911. (type != ACPI_NOTIFY_DEVICE_CHECK)) {
  912. err("unexpected notification type %d\n", type);
  913. return;
  914. }
  915. acpi_get_parent(handle, &phandle);
  916. if (acpi_bus_get_device(phandle, &pdevice)) {
  917. dbg("no parent device, assuming NULL\n");
  918. pdevice = NULL;
  919. }
  920. if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
  921. err("cannot add bridge to acpi list\n");
  922. return;
  923. }
  924. if (!acpiphp_configure_bridge(handle) &&
  925. !acpi_bus_start(device))
  926. add_bridge(handle);
  927. else
  928. err("cannot configure and start bridge\n");
  929. }
  930. /*
  931. * ACPI event handlers
  932. */
  933. static acpi_status
  934. count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  935. {
  936. int *count = (int *)context;
  937. struct acpiphp_bridge *bridge;
  938. bridge = acpiphp_handle_to_bridge(handle);
  939. if (bridge)
  940. (*count)++;
  941. return AE_OK ;
  942. }
  943. static acpi_status
  944. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  945. {
  946. struct acpiphp_bridge *bridge;
  947. char objname[64];
  948. struct acpi_buffer buffer = { .length = sizeof(objname),
  949. .pointer = objname };
  950. bridge = acpiphp_handle_to_bridge(handle);
  951. if (bridge) {
  952. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  953. dbg("%s: re-enumerating slots under %s\n",
  954. __func__, objname);
  955. acpiphp_check_bridge(bridge);
  956. }
  957. return AE_OK ;
  958. }
  959. /**
  960. * handle_hotplug_event_bridge - handle ACPI event on bridges
  961. * @handle: Notify()'ed acpi_handle
  962. * @type: Notify code
  963. * @context: pointer to acpiphp_bridge structure
  964. *
  965. * Handles ACPI event notification on {host,p2p} bridges.
  966. */
  967. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
  968. {
  969. struct acpiphp_bridge *bridge;
  970. char objname[64];
  971. struct acpi_buffer buffer = { .length = sizeof(objname),
  972. .pointer = objname };
  973. struct acpi_device *device;
  974. int num_sub_bridges = 0;
  975. if (acpi_bus_get_device(handle, &device)) {
  976. /* This bridge must have just been physically inserted */
  977. handle_bridge_insertion(handle, type);
  978. return;
  979. }
  980. bridge = acpiphp_handle_to_bridge(handle);
  981. if (type == ACPI_NOTIFY_BUS_CHECK) {
  982. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
  983. count_sub_bridges, NULL, &num_sub_bridges, NULL);
  984. }
  985. if (!bridge && !num_sub_bridges) {
  986. err("cannot get bridge info\n");
  987. return;
  988. }
  989. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  990. switch (type) {
  991. case ACPI_NOTIFY_BUS_CHECK:
  992. /* bus re-enumerate */
  993. dbg("%s: Bus check notify on %s\n", __func__, objname);
  994. if (bridge) {
  995. dbg("%s: re-enumerating slots under %s\n",
  996. __func__, objname);
  997. acpiphp_check_bridge(bridge);
  998. }
  999. if (num_sub_bridges)
  1000. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  1001. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  1002. break;
  1003. case ACPI_NOTIFY_DEVICE_CHECK:
  1004. /* device check */
  1005. dbg("%s: Device check notify on %s\n", __func__, objname);
  1006. acpiphp_check_bridge(bridge);
  1007. break;
  1008. case ACPI_NOTIFY_DEVICE_WAKE:
  1009. /* wake event */
  1010. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1011. break;
  1012. case ACPI_NOTIFY_EJECT_REQUEST:
  1013. /* request device eject */
  1014. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1015. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  1016. (bridge->flags & BRIDGE_HAS_EJ0)) {
  1017. struct acpiphp_slot *slot;
  1018. slot = bridge->func->slot;
  1019. if (!acpiphp_disable_slot(slot))
  1020. acpiphp_eject_slot(slot);
  1021. }
  1022. break;
  1023. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  1024. printk(KERN_ERR "Device %s cannot be configured due"
  1025. " to a frequency mismatch\n", objname);
  1026. break;
  1027. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  1028. printk(KERN_ERR "Device %s cannot be configured due"
  1029. " to a bus mode mismatch\n", objname);
  1030. break;
  1031. case ACPI_NOTIFY_POWER_FAULT:
  1032. printk(KERN_ERR "Device %s has suffered a power fault\n",
  1033. objname);
  1034. break;
  1035. default:
  1036. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1037. break;
  1038. }
  1039. }
  1040. /**
  1041. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1042. * @handle: Notify()'ed acpi_handle
  1043. * @type: Notify code
  1044. * @context: pointer to acpiphp_func structure
  1045. *
  1046. * Handles ACPI event notification on slots.
  1047. */
  1048. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
  1049. {
  1050. struct acpiphp_func *func;
  1051. char objname[64];
  1052. struct acpi_buffer buffer = { .length = sizeof(objname),
  1053. .pointer = objname };
  1054. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1055. func = (struct acpiphp_func *)context;
  1056. switch (type) {
  1057. case ACPI_NOTIFY_BUS_CHECK:
  1058. /* bus re-enumerate */
  1059. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1060. acpiphp_enable_slot(func->slot);
  1061. break;
  1062. case ACPI_NOTIFY_DEVICE_CHECK:
  1063. /* device check : re-enumerate from parent bus */
  1064. dbg("%s: Device check notify on %s\n", __func__, objname);
  1065. acpiphp_check_bridge(func->slot->bridge);
  1066. break;
  1067. case ACPI_NOTIFY_DEVICE_WAKE:
  1068. /* wake event */
  1069. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1070. break;
  1071. case ACPI_NOTIFY_EJECT_REQUEST:
  1072. /* request device eject */
  1073. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1074. if (!(acpiphp_disable_slot(func->slot)))
  1075. acpiphp_eject_slot(func->slot);
  1076. break;
  1077. default:
  1078. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1079. break;
  1080. }
  1081. }
  1082. static acpi_status
  1083. find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1084. {
  1085. int *count = (int *)context;
  1086. if (acpi_is_root_bridge(handle)) {
  1087. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1088. handle_hotplug_event_bridge, NULL);
  1089. (*count)++;
  1090. }
  1091. return AE_OK ;
  1092. }
  1093. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1094. .add = add_bridge,
  1095. .remove = remove_bridge,
  1096. };
  1097. /**
  1098. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1099. */
  1100. int __init acpiphp_glue_init(void)
  1101. {
  1102. int num = 0;
  1103. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  1104. ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
  1105. if (num <= 0)
  1106. return -1;
  1107. else
  1108. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1109. return 0;
  1110. }
  1111. /**
  1112. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1113. *
  1114. * This function frees all data allocated in acpiphp_glue_init().
  1115. */
  1116. void acpiphp_glue_exit(void)
  1117. {
  1118. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1119. }
  1120. /**
  1121. * acpiphp_get_num_slots - count number of slots in a system
  1122. */
  1123. int __init acpiphp_get_num_slots(void)
  1124. {
  1125. struct acpiphp_bridge *bridge;
  1126. int num_slots = 0;
  1127. list_for_each_entry(bridge, &bridge_list, list) {
  1128. dbg("Bus %04x:%02x has %d slot%s\n",
  1129. pci_domain_nr(bridge->pci_bus),
  1130. bridge->pci_bus->number, bridge->nr_slots,
  1131. bridge->nr_slots == 1 ? "" : "s");
  1132. num_slots += bridge->nr_slots;
  1133. }
  1134. dbg("Total %d slots\n", num_slots);
  1135. return num_slots;
  1136. }
  1137. #if 0
  1138. /**
  1139. * acpiphp_for_each_slot - call function for each slot
  1140. * @fn: callback function
  1141. * @data: context to be passed to callback function
  1142. */
  1143. static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
  1144. {
  1145. struct list_head *node;
  1146. struct acpiphp_bridge *bridge;
  1147. struct acpiphp_slot *slot;
  1148. int retval = 0;
  1149. list_for_each (node, &bridge_list) {
  1150. bridge = (struct acpiphp_bridge *)node;
  1151. for (slot = bridge->slots; slot; slot = slot->next) {
  1152. retval = fn(slot, data);
  1153. if (!retval)
  1154. goto err_exit;
  1155. }
  1156. }
  1157. err_exit:
  1158. return retval;
  1159. }
  1160. #endif
  1161. /**
  1162. * acpiphp_enable_slot - power on slot
  1163. * @slot: ACPI PHP slot
  1164. */
  1165. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1166. {
  1167. int retval;
  1168. mutex_lock(&slot->crit_sect);
  1169. /* wake up all functions */
  1170. retval = power_on_slot(slot);
  1171. if (retval)
  1172. goto err_exit;
  1173. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1174. /* configure all functions */
  1175. retval = enable_device(slot);
  1176. if (retval)
  1177. power_off_slot(slot);
  1178. } else {
  1179. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1180. power_off_slot(slot);
  1181. }
  1182. err_exit:
  1183. mutex_unlock(&slot->crit_sect);
  1184. return retval;
  1185. }
  1186. /**
  1187. * acpiphp_disable_slot - power off slot
  1188. * @slot: ACPI PHP slot
  1189. */
  1190. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1191. {
  1192. int retval = 0;
  1193. mutex_lock(&slot->crit_sect);
  1194. /* unconfigure all functions */
  1195. retval = disable_device(slot);
  1196. if (retval)
  1197. goto err_exit;
  1198. /* power off all functions */
  1199. retval = power_off_slot(slot);
  1200. if (retval)
  1201. goto err_exit;
  1202. err_exit:
  1203. mutex_unlock(&slot->crit_sect);
  1204. return retval;
  1205. }
  1206. /*
  1207. * slot enabled: 1
  1208. * slot disabled: 0
  1209. */
  1210. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1211. {
  1212. return (slot->flags & SLOT_POWEREDON);
  1213. }
  1214. /*
  1215. * latch open: 1
  1216. * latch closed: 0
  1217. */
  1218. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1219. {
  1220. unsigned int sta;
  1221. sta = get_slot_status(slot);
  1222. return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
  1223. }
  1224. /*
  1225. * adapter presence : 1
  1226. * absence : 0
  1227. */
  1228. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1229. {
  1230. unsigned int sta;
  1231. sta = get_slot_status(slot);
  1232. return (sta == 0) ? 0 : 1;
  1233. }