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