acpiphp_glue.c 38 KB

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