acpiphp_glue.c 38 KB

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