acpiphp_glue.c 38 KB

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