acpiphp_glue.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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(acpi_handle *handle)
  320. {
  321. struct acpiphp_bridge *bridge;
  322. struct acpi_pci_root *root = acpi_pci_find_root(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(acpi_handle handle)
  387. {
  388. acpi_status status;
  389. unsigned long long tmp;
  390. acpi_handle dummy_handle;
  391. /* if the bridge doesn't have _STA, we assume it is always there */
  392. status = acpi_get_handle(handle, "_STA", &dummy_handle);
  393. if (ACPI_SUCCESS(status)) {
  394. status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
  395. if (ACPI_FAILURE(status)) {
  396. dbg("%s: _STA evaluation failure\n", __func__);
  397. return 0;
  398. }
  399. if ((tmp & ACPI_STA_FUNCTIONING) == 0)
  400. /* don't register this object */
  401. return 0;
  402. }
  403. /* check if this bridge has ejectable slots */
  404. if (detect_ejectable_slots(handle) > 0) {
  405. dbg("found PCI host-bus bridge with hot-pluggable slots\n");
  406. add_host_bridge(handle);
  407. }
  408. /* search P2P bridges under this host bridge */
  409. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  410. find_p2p_bridge, NULL, NULL, NULL);
  411. if (ACPI_FAILURE(status))
  412. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  413. return 0;
  414. }
  415. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  416. {
  417. struct acpiphp_bridge *bridge;
  418. list_for_each_entry(bridge, &bridge_list, list)
  419. if (bridge->handle == handle)
  420. return bridge;
  421. return NULL;
  422. }
  423. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  424. {
  425. struct acpiphp_slot *slot, *next;
  426. struct acpiphp_func *func, *tmp;
  427. acpi_status status;
  428. acpi_handle handle = bridge->handle;
  429. status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  430. handle_hotplug_event_bridge);
  431. if (ACPI_FAILURE(status))
  432. err("failed to remove notify handler\n");
  433. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  434. ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
  435. status = acpi_install_notify_handler(bridge->func->handle,
  436. ACPI_SYSTEM_NOTIFY,
  437. handle_hotplug_event_func,
  438. bridge->func);
  439. if (ACPI_FAILURE(status))
  440. err("failed to install interrupt notify handler\n");
  441. }
  442. slot = bridge->slots;
  443. while (slot) {
  444. next = slot->next;
  445. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
  446. if (is_dock_device(func->handle)) {
  447. unregister_hotplug_dock_device(func->handle);
  448. unregister_dock_notifier(&func->nb);
  449. }
  450. if (!(func->flags & FUNC_HAS_DCK)) {
  451. status = acpi_remove_notify_handler(func->handle,
  452. ACPI_SYSTEM_NOTIFY,
  453. handle_hotplug_event_func);
  454. if (ACPI_FAILURE(status))
  455. err("failed to remove notify handler\n");
  456. }
  457. list_del(&func->sibling);
  458. kfree(func);
  459. }
  460. acpiphp_unregister_hotplug_slot(slot);
  461. list_del(&slot->funcs);
  462. kfree(slot);
  463. slot = next;
  464. }
  465. /*
  466. * Only P2P bridges have a pci_dev
  467. */
  468. if (bridge->pci_dev)
  469. put_device(&bridge->pci_bus->dev);
  470. pci_dev_put(bridge->pci_dev);
  471. list_del(&bridge->list);
  472. kfree(bridge);
  473. }
  474. static acpi_status
  475. cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  476. {
  477. struct acpiphp_bridge *bridge;
  478. /* cleanup p2p bridges under this P2P bridge
  479. in a depth-first manner */
  480. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  481. cleanup_p2p_bridge, NULL, NULL, NULL);
  482. bridge = acpiphp_handle_to_bridge(handle);
  483. if (bridge)
  484. cleanup_bridge(bridge);
  485. return AE_OK;
  486. }
  487. static void remove_bridge(acpi_handle handle)
  488. {
  489. struct acpiphp_bridge *bridge;
  490. /* cleanup p2p bridges under this host bridge
  491. in a depth-first manner */
  492. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  493. (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
  494. /*
  495. * On root bridges with hotplug slots directly underneath (ie,
  496. * no p2p bridge between), we call cleanup_bridge().
  497. *
  498. * The else clause cleans up root bridges that either had no
  499. * hotplug slots at all, or had a p2p bridge underneath.
  500. */
  501. bridge = acpiphp_handle_to_bridge(handle);
  502. if (bridge)
  503. cleanup_bridge(bridge);
  504. else
  505. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  506. handle_hotplug_event_bridge);
  507. }
  508. static int power_on_slot(struct acpiphp_slot *slot)
  509. {
  510. acpi_status status;
  511. struct acpiphp_func *func;
  512. int retval = 0;
  513. /* if already enabled, just skip */
  514. if (slot->flags & SLOT_POWEREDON)
  515. goto err_exit;
  516. list_for_each_entry(func, &slot->funcs, sibling) {
  517. if (func->flags & FUNC_HAS_PS0) {
  518. dbg("%s: executing _PS0\n", __func__);
  519. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  520. if (ACPI_FAILURE(status)) {
  521. warn("%s: _PS0 failed\n", __func__);
  522. retval = -1;
  523. goto err_exit;
  524. } else
  525. break;
  526. }
  527. }
  528. /* TBD: evaluate _STA to check if the slot is enabled */
  529. slot->flags |= SLOT_POWEREDON;
  530. err_exit:
  531. return retval;
  532. }
  533. static int power_off_slot(struct acpiphp_slot *slot)
  534. {
  535. acpi_status status;
  536. struct acpiphp_func *func;
  537. int retval = 0;
  538. /* if already disabled, just skip */
  539. if ((slot->flags & SLOT_POWEREDON) == 0)
  540. goto err_exit;
  541. list_for_each_entry(func, &slot->funcs, sibling) {
  542. if (func->flags & FUNC_HAS_PS3) {
  543. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  544. if (ACPI_FAILURE(status)) {
  545. warn("%s: _PS3 failed\n", __func__);
  546. retval = -1;
  547. goto err_exit;
  548. } else
  549. break;
  550. }
  551. }
  552. /* TBD: evaluate _STA to check if the slot is disabled */
  553. slot->flags &= (~SLOT_POWEREDON);
  554. err_exit:
  555. return retval;
  556. }
  557. /**
  558. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  559. * @bus: bus to start search with
  560. */
  561. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  562. {
  563. struct list_head *tmp;
  564. unsigned char max, n;
  565. /*
  566. * pci_bus_max_busnr will return the highest
  567. * reserved busnr for all these children.
  568. * that is equivalent to the bus->subordinate
  569. * value. We don't want to use the parent's
  570. * bus->subordinate value because it could have
  571. * padding in it.
  572. */
  573. max = bus->busn_res.start;
  574. list_for_each(tmp, &bus->children) {
  575. n = pci_bus_max_busnr(pci_bus_b(tmp));
  576. if (n > max)
  577. max = n;
  578. }
  579. return max;
  580. }
  581. /**
  582. * acpiphp_bus_add - add a new bus to acpi subsystem
  583. * @func: acpiphp_func of the bridge
  584. */
  585. static int acpiphp_bus_add(struct acpiphp_func *func)
  586. {
  587. acpi_handle phandle;
  588. struct acpi_device *device, *pdevice;
  589. int ret_val;
  590. acpi_get_parent(func->handle, &phandle);
  591. if (acpi_bus_get_device(phandle, &pdevice)) {
  592. dbg("no parent device, assuming NULL\n");
  593. pdevice = NULL;
  594. }
  595. if (!acpi_bus_get_device(func->handle, &device)) {
  596. dbg("bus exists... trim\n");
  597. /* this shouldn't be in here, so remove
  598. * the bus then re-add it...
  599. */
  600. ret_val = acpi_bus_trim(device, 1);
  601. dbg("acpi_bus_trim return %x\n", ret_val);
  602. }
  603. ret_val = acpi_bus_add(&device, pdevice, func->handle,
  604. ACPI_BUS_TYPE_DEVICE);
  605. if (ret_val) {
  606. dbg("error adding bus, %x\n",
  607. -ret_val);
  608. goto acpiphp_bus_add_out;
  609. }
  610. ret_val = acpi_bus_start(device);
  611. acpiphp_bus_add_out:
  612. return ret_val;
  613. }
  614. /**
  615. * acpiphp_bus_trim - trim a bus from acpi subsystem
  616. * @handle: handle to acpi namespace
  617. */
  618. static int acpiphp_bus_trim(acpi_handle handle)
  619. {
  620. struct acpi_device *device;
  621. int retval;
  622. retval = acpi_bus_get_device(handle, &device);
  623. if (retval) {
  624. dbg("acpi_device not found\n");
  625. return retval;
  626. }
  627. retval = acpi_bus_trim(device, 1);
  628. if (retval)
  629. err("cannot remove from acpi list\n");
  630. return retval;
  631. }
  632. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  633. {
  634. struct acpiphp_func *func;
  635. union acpi_object params[2];
  636. struct acpi_object_list arg_list;
  637. list_for_each_entry(func, &slot->funcs, sibling) {
  638. arg_list.count = 2;
  639. arg_list.pointer = params;
  640. params[0].type = ACPI_TYPE_INTEGER;
  641. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  642. params[1].type = ACPI_TYPE_INTEGER;
  643. params[1].integer.value = 1;
  644. /* _REG is optional, we don't care about if there is failure */
  645. acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
  646. }
  647. }
  648. /**
  649. * enable_device - enable, configure a slot
  650. * @slot: slot to be enabled
  651. *
  652. * This function should be called per *physical slot*,
  653. * not per each slot object in ACPI namespace.
  654. */
  655. static int __ref enable_device(struct acpiphp_slot *slot)
  656. {
  657. struct pci_dev *dev;
  658. struct pci_bus *bus = slot->bridge->pci_bus;
  659. struct acpiphp_func *func;
  660. int retval = 0;
  661. int num, max, pass;
  662. acpi_status status;
  663. if (slot->flags & SLOT_ENABLED)
  664. goto err_exit;
  665. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  666. if (num == 0) {
  667. /* Maybe only part of funcs are added. */
  668. dbg("No new device found\n");
  669. goto err_exit;
  670. }
  671. max = acpiphp_max_busnr(bus);
  672. for (pass = 0; pass < 2; pass++) {
  673. list_for_each_entry(dev, &bus->devices, bus_list) {
  674. if (PCI_SLOT(dev->devfn) != slot->device)
  675. continue;
  676. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  677. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  678. max = pci_scan_bridge(bus, dev, max, pass);
  679. if (pass && dev->subordinate)
  680. pci_bus_size_bridges(dev->subordinate);
  681. }
  682. }
  683. }
  684. list_for_each_entry(func, &slot->funcs, sibling)
  685. acpiphp_bus_add(func);
  686. pci_bus_assign_resources(bus);
  687. acpiphp_sanitize_bus(bus);
  688. acpiphp_set_hpp_values(bus);
  689. acpiphp_set_acpi_region(slot);
  690. pci_enable_bridges(bus);
  691. list_for_each_entry(dev, &bus->devices, bus_list) {
  692. /* Assume that newly added devices are powered on already. */
  693. if (!dev->is_added)
  694. dev->current_state = PCI_D0;
  695. }
  696. pci_bus_add_devices(bus);
  697. slot->flags |= SLOT_ENABLED;
  698. list_for_each_entry(func, &slot->funcs, sibling) {
  699. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  700. func->function));
  701. if (!dev) {
  702. /* Do not set SLOT_ENABLED flag if some funcs
  703. are not added. */
  704. slot->flags &= (~SLOT_ENABLED);
  705. continue;
  706. }
  707. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
  708. dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
  709. pci_dev_put(dev);
  710. continue;
  711. }
  712. status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
  713. if (ACPI_FAILURE(status))
  714. warn("find_p2p_bridge failed (error code = 0x%x)\n",
  715. status);
  716. pci_dev_put(dev);
  717. }
  718. err_exit:
  719. return retval;
  720. }
  721. static void disable_bridges(struct pci_bus *bus)
  722. {
  723. struct pci_dev *dev;
  724. list_for_each_entry(dev, &bus->devices, bus_list) {
  725. if (dev->subordinate) {
  726. disable_bridges(dev->subordinate);
  727. pci_disable_device(dev);
  728. }
  729. }
  730. }
  731. /* return first device in slot, acquiring a reference on it */
  732. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  733. {
  734. struct pci_bus *bus = slot->bridge->pci_bus;
  735. struct pci_dev *dev;
  736. struct pci_dev *ret = NULL;
  737. down_read(&pci_bus_sem);
  738. list_for_each_entry(dev, &bus->devices, bus_list)
  739. if (PCI_SLOT(dev->devfn) == slot->device) {
  740. ret = pci_dev_get(dev);
  741. break;
  742. }
  743. up_read(&pci_bus_sem);
  744. return ret;
  745. }
  746. /**
  747. * disable_device - disable a slot
  748. * @slot: ACPI PHP slot
  749. */
  750. static int disable_device(struct acpiphp_slot *slot)
  751. {
  752. struct acpiphp_func *func;
  753. struct pci_dev *pdev;
  754. struct pci_bus *bus = slot->bridge->pci_bus;
  755. /* The slot will be enabled when func 0 is added, so check
  756. func 0 before disable the slot. */
  757. pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  758. if (!pdev)
  759. goto err_exit;
  760. pci_dev_put(pdev);
  761. list_for_each_entry(func, &slot->funcs, sibling) {
  762. if (func->bridge) {
  763. /* cleanup p2p bridges under this P2P bridge */
  764. cleanup_p2p_bridge(func->bridge->handle,
  765. (u32)1, NULL, NULL);
  766. func->bridge = NULL;
  767. }
  768. }
  769. /*
  770. * enable_device() enumerates all functions in this device via
  771. * pci_scan_slot(), whether they have associated ACPI hotplug
  772. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  773. * here.
  774. */
  775. while ((pdev = dev_in_slot(slot))) {
  776. pci_stop_bus_device(pdev);
  777. if (pdev->subordinate) {
  778. disable_bridges(pdev->subordinate);
  779. pci_disable_device(pdev);
  780. }
  781. __pci_remove_bus_device(pdev);
  782. pci_dev_put(pdev);
  783. }
  784. list_for_each_entry(func, &slot->funcs, sibling) {
  785. acpiphp_bus_trim(func->handle);
  786. }
  787. slot->flags &= (~SLOT_ENABLED);
  788. err_exit:
  789. return 0;
  790. }
  791. /**
  792. * get_slot_status - get ACPI slot status
  793. * @slot: ACPI PHP slot
  794. *
  795. * If a slot has _STA for each function and if any one of them
  796. * returned non-zero status, return it.
  797. *
  798. * If a slot doesn't have _STA and if any one of its functions'
  799. * configuration space is configured, return 0x0f as a _STA.
  800. *
  801. * Otherwise return 0.
  802. */
  803. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  804. {
  805. acpi_status status;
  806. unsigned long long sta = 0;
  807. u32 dvid;
  808. struct acpiphp_func *func;
  809. list_for_each_entry(func, &slot->funcs, sibling) {
  810. if (func->flags & FUNC_HAS_STA) {
  811. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  812. if (ACPI_SUCCESS(status) && sta)
  813. break;
  814. } else {
  815. pci_bus_read_config_dword(slot->bridge->pci_bus,
  816. PCI_DEVFN(slot->device,
  817. func->function),
  818. PCI_VENDOR_ID, &dvid);
  819. if (dvid != 0xffffffff) {
  820. sta = ACPI_STA_ALL;
  821. break;
  822. }
  823. }
  824. }
  825. return (unsigned int)sta;
  826. }
  827. /**
  828. * acpiphp_eject_slot - physically eject the slot
  829. * @slot: ACPI PHP slot
  830. */
  831. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  832. {
  833. acpi_status status;
  834. struct acpiphp_func *func;
  835. struct acpi_object_list arg_list;
  836. union acpi_object arg;
  837. list_for_each_entry(func, &slot->funcs, sibling) {
  838. /* We don't want to call _EJ0 on non-existing functions. */
  839. if ((func->flags & FUNC_HAS_EJ0)) {
  840. /* _EJ0 method take one argument */
  841. arg_list.count = 1;
  842. arg_list.pointer = &arg;
  843. arg.type = ACPI_TYPE_INTEGER;
  844. arg.integer.value = 1;
  845. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  846. if (ACPI_FAILURE(status)) {
  847. warn("%s: _EJ0 failed\n", __func__);
  848. return -1;
  849. } else
  850. break;
  851. }
  852. }
  853. return 0;
  854. }
  855. /**
  856. * acpiphp_check_bridge - re-enumerate devices
  857. * @bridge: where to begin re-enumeration
  858. *
  859. * Iterate over all slots under this bridge and make sure that if a
  860. * card is present they are enabled, and if not they are disabled.
  861. */
  862. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  863. {
  864. struct acpiphp_slot *slot;
  865. int retval = 0;
  866. int enabled, disabled;
  867. enabled = disabled = 0;
  868. for (slot = bridge->slots; slot; slot = slot->next) {
  869. unsigned int status = get_slot_status(slot);
  870. if (slot->flags & SLOT_ENABLED) {
  871. if (status == ACPI_STA_ALL)
  872. continue;
  873. retval = acpiphp_disable_slot(slot);
  874. if (retval) {
  875. err("Error occurred in disabling\n");
  876. goto err_exit;
  877. } else {
  878. acpiphp_eject_slot(slot);
  879. }
  880. disabled++;
  881. } else {
  882. if (status != ACPI_STA_ALL)
  883. continue;
  884. retval = acpiphp_enable_slot(slot);
  885. if (retval) {
  886. err("Error occurred in enabling\n");
  887. goto err_exit;
  888. }
  889. enabled++;
  890. }
  891. }
  892. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  893. err_exit:
  894. return retval;
  895. }
  896. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  897. {
  898. struct pci_dev *dev;
  899. list_for_each_entry(dev, &bus->devices, bus_list)
  900. pci_configure_slot(dev);
  901. }
  902. /*
  903. * Remove devices for which we could not assign resources, call
  904. * arch specific code to fix-up the bus
  905. */
  906. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  907. {
  908. struct pci_dev *dev;
  909. int i;
  910. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  911. list_for_each_entry(dev, &bus->devices, bus_list) {
  912. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  913. struct resource *res = &dev->resource[i];
  914. if ((res->flags & type_mask) && !res->start &&
  915. res->end) {
  916. /* Could not assign a required resources
  917. * for this device, remove it */
  918. pci_stop_and_remove_bus_device(dev);
  919. break;
  920. }
  921. }
  922. }
  923. }
  924. /* Program resources in newly inserted bridge */
  925. static int acpiphp_configure_bridge (acpi_handle handle)
  926. {
  927. struct pci_bus *bus;
  928. if (acpi_is_root_bridge(handle)) {
  929. struct acpi_pci_root *root = acpi_pci_find_root(handle);
  930. bus = root->bus;
  931. } else {
  932. struct pci_dev *pdev = acpi_get_pci_dev(handle);
  933. bus = pdev->subordinate;
  934. pci_dev_put(pdev);
  935. }
  936. pci_bus_size_bridges(bus);
  937. pci_bus_assign_resources(bus);
  938. acpiphp_sanitize_bus(bus);
  939. acpiphp_set_hpp_values(bus);
  940. pci_enable_bridges(bus);
  941. return 0;
  942. }
  943. static void handle_bridge_insertion(acpi_handle handle, u32 type)
  944. {
  945. struct acpi_device *device, *pdevice;
  946. acpi_handle phandle;
  947. if ((type != ACPI_NOTIFY_BUS_CHECK) &&
  948. (type != ACPI_NOTIFY_DEVICE_CHECK)) {
  949. err("unexpected notification type %d\n", type);
  950. return;
  951. }
  952. acpi_get_parent(handle, &phandle);
  953. if (acpi_bus_get_device(phandle, &pdevice)) {
  954. dbg("no parent device, assuming NULL\n");
  955. pdevice = NULL;
  956. }
  957. if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
  958. err("cannot add bridge to acpi list\n");
  959. return;
  960. }
  961. if (!acpiphp_configure_bridge(handle) &&
  962. !acpi_bus_start(device))
  963. add_bridge(handle);
  964. else
  965. err("cannot configure and start bridge\n");
  966. }
  967. /*
  968. * ACPI event handlers
  969. */
  970. static acpi_status
  971. count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  972. {
  973. int *count = (int *)context;
  974. struct acpiphp_bridge *bridge;
  975. bridge = acpiphp_handle_to_bridge(handle);
  976. if (bridge)
  977. (*count)++;
  978. return AE_OK ;
  979. }
  980. static acpi_status
  981. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  982. {
  983. struct acpiphp_bridge *bridge;
  984. char objname[64];
  985. struct acpi_buffer buffer = { .length = sizeof(objname),
  986. .pointer = objname };
  987. bridge = acpiphp_handle_to_bridge(handle);
  988. if (bridge) {
  989. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  990. dbg("%s: re-enumerating slots under %s\n",
  991. __func__, objname);
  992. acpiphp_check_bridge(bridge);
  993. }
  994. return AE_OK ;
  995. }
  996. struct acpiphp_hp_work {
  997. struct work_struct work;
  998. acpi_handle handle;
  999. u32 type;
  1000. void *context;
  1001. };
  1002. static void alloc_acpiphp_hp_work(acpi_handle handle, u32 type,
  1003. void *context,
  1004. void (*func)(struct work_struct *work))
  1005. {
  1006. struct acpiphp_hp_work *hp_work;
  1007. int ret;
  1008. hp_work = kmalloc(sizeof(*hp_work), GFP_KERNEL);
  1009. if (!hp_work)
  1010. return;
  1011. hp_work->handle = handle;
  1012. hp_work->type = type;
  1013. hp_work->context = context;
  1014. INIT_WORK(&hp_work->work, func);
  1015. ret = queue_work(kacpi_hotplug_wq, &hp_work->work);
  1016. if (!ret)
  1017. kfree(hp_work);
  1018. }
  1019. static void _handle_hotplug_event_bridge(struct work_struct *work)
  1020. {
  1021. struct acpiphp_bridge *bridge;
  1022. char objname[64];
  1023. struct acpi_buffer buffer = { .length = sizeof(objname),
  1024. .pointer = objname };
  1025. struct acpi_device *device;
  1026. int num_sub_bridges = 0;
  1027. struct acpiphp_hp_work *hp_work;
  1028. acpi_handle handle;
  1029. u32 type;
  1030. hp_work = container_of(work, struct acpiphp_hp_work, work);
  1031. handle = hp_work->handle;
  1032. type = hp_work->type;
  1033. if (acpi_bus_get_device(handle, &device)) {
  1034. /* This bridge must have just been physically inserted */
  1035. handle_bridge_insertion(handle, type);
  1036. goto out;
  1037. }
  1038. bridge = acpiphp_handle_to_bridge(handle);
  1039. if (type == ACPI_NOTIFY_BUS_CHECK) {
  1040. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
  1041. count_sub_bridges, NULL, &num_sub_bridges, NULL);
  1042. }
  1043. if (!bridge && !num_sub_bridges) {
  1044. err("cannot get bridge info\n");
  1045. goto out;
  1046. }
  1047. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1048. switch (type) {
  1049. case ACPI_NOTIFY_BUS_CHECK:
  1050. /* bus re-enumerate */
  1051. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1052. if (bridge) {
  1053. dbg("%s: re-enumerating slots under %s\n",
  1054. __func__, objname);
  1055. acpiphp_check_bridge(bridge);
  1056. }
  1057. if (num_sub_bridges)
  1058. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  1059. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  1060. break;
  1061. case ACPI_NOTIFY_DEVICE_CHECK:
  1062. /* device check */
  1063. dbg("%s: Device check notify on %s\n", __func__, objname);
  1064. acpiphp_check_bridge(bridge);
  1065. break;
  1066. case ACPI_NOTIFY_DEVICE_WAKE:
  1067. /* wake event */
  1068. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1069. break;
  1070. case ACPI_NOTIFY_EJECT_REQUEST:
  1071. /* request device eject */
  1072. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1073. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  1074. (bridge->flags & BRIDGE_HAS_EJ0)) {
  1075. struct acpiphp_slot *slot;
  1076. slot = bridge->func->slot;
  1077. if (!acpiphp_disable_slot(slot))
  1078. acpiphp_eject_slot(slot);
  1079. }
  1080. break;
  1081. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  1082. printk(KERN_ERR "Device %s cannot be configured due"
  1083. " to a frequency mismatch\n", objname);
  1084. break;
  1085. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  1086. printk(KERN_ERR "Device %s cannot be configured due"
  1087. " to a bus mode mismatch\n", objname);
  1088. break;
  1089. case ACPI_NOTIFY_POWER_FAULT:
  1090. printk(KERN_ERR "Device %s has suffered a power fault\n",
  1091. objname);
  1092. break;
  1093. default:
  1094. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1095. break;
  1096. }
  1097. out:
  1098. kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
  1099. }
  1100. /**
  1101. * handle_hotplug_event_bridge - handle ACPI event on bridges
  1102. * @handle: Notify()'ed acpi_handle
  1103. * @type: Notify code
  1104. * @context: pointer to acpiphp_bridge structure
  1105. *
  1106. * Handles ACPI event notification on {host,p2p} bridges.
  1107. */
  1108. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
  1109. void *context)
  1110. {
  1111. /*
  1112. * Currently the code adds all hotplug events to the kacpid_wq
  1113. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  1114. * The proper way to fix this is to reorganize the code so that
  1115. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  1116. * For now just re-add this work to the kacpi_hotplug_wq so we
  1117. * don't deadlock on hotplug actions.
  1118. */
  1119. alloc_acpiphp_hp_work(handle, type, context,
  1120. _handle_hotplug_event_bridge);
  1121. }
  1122. static void _handle_hotplug_event_func(struct work_struct *work)
  1123. {
  1124. struct acpiphp_func *func;
  1125. char objname[64];
  1126. struct acpi_buffer buffer = { .length = sizeof(objname),
  1127. .pointer = objname };
  1128. struct acpiphp_hp_work *hp_work;
  1129. acpi_handle handle;
  1130. u32 type;
  1131. void *context;
  1132. hp_work = container_of(work, struct acpiphp_hp_work, work);
  1133. handle = hp_work->handle;
  1134. type = hp_work->type;
  1135. context = hp_work->context;
  1136. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1137. func = (struct acpiphp_func *)context;
  1138. switch (type) {
  1139. case ACPI_NOTIFY_BUS_CHECK:
  1140. /* bus re-enumerate */
  1141. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1142. acpiphp_enable_slot(func->slot);
  1143. break;
  1144. case ACPI_NOTIFY_DEVICE_CHECK:
  1145. /* device check : re-enumerate from parent bus */
  1146. dbg("%s: Device check notify on %s\n", __func__, objname);
  1147. acpiphp_check_bridge(func->slot->bridge);
  1148. break;
  1149. case ACPI_NOTIFY_DEVICE_WAKE:
  1150. /* wake event */
  1151. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1152. break;
  1153. case ACPI_NOTIFY_EJECT_REQUEST:
  1154. /* request device eject */
  1155. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1156. if (!(acpiphp_disable_slot(func->slot)))
  1157. acpiphp_eject_slot(func->slot);
  1158. break;
  1159. default:
  1160. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1161. break;
  1162. }
  1163. kfree(hp_work); /* allocated in handle_hotplug_event_func */
  1164. }
  1165. /**
  1166. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1167. * @handle: Notify()'ed acpi_handle
  1168. * @type: Notify code
  1169. * @context: pointer to acpiphp_func structure
  1170. *
  1171. * Handles ACPI event notification on slots.
  1172. */
  1173. static void handle_hotplug_event_func(acpi_handle handle, u32 type,
  1174. void *context)
  1175. {
  1176. /*
  1177. * Currently the code adds all hotplug events to the kacpid_wq
  1178. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  1179. * The proper way to fix this is to reorganize the code so that
  1180. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  1181. * For now just re-add this work to the kacpi_hotplug_wq so we
  1182. * don't deadlock on hotplug actions.
  1183. */
  1184. alloc_acpiphp_hp_work(handle, type, context,
  1185. _handle_hotplug_event_func);
  1186. }
  1187. static acpi_status
  1188. find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1189. {
  1190. int *count = (int *)context;
  1191. if (!acpi_is_root_bridge(handle))
  1192. return AE_OK;
  1193. (*count)++;
  1194. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1195. handle_hotplug_event_bridge, NULL);
  1196. return AE_OK ;
  1197. }
  1198. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1199. .add = add_bridge,
  1200. .remove = remove_bridge,
  1201. };
  1202. /**
  1203. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1204. */
  1205. int __init acpiphp_glue_init(void)
  1206. {
  1207. int num = 0;
  1208. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  1209. ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
  1210. if (num <= 0)
  1211. return -1;
  1212. else
  1213. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1214. return 0;
  1215. }
  1216. /**
  1217. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1218. *
  1219. * This function frees all data allocated in acpiphp_glue_init().
  1220. */
  1221. void acpiphp_glue_exit(void)
  1222. {
  1223. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1224. }
  1225. /**
  1226. * acpiphp_get_num_slots - count number of slots in a system
  1227. */
  1228. int __init acpiphp_get_num_slots(void)
  1229. {
  1230. struct acpiphp_bridge *bridge;
  1231. int num_slots = 0;
  1232. list_for_each_entry(bridge, &bridge_list, list) {
  1233. dbg("Bus %04x:%02x has %d slot%s\n",
  1234. pci_domain_nr(bridge->pci_bus),
  1235. bridge->pci_bus->number, bridge->nr_slots,
  1236. bridge->nr_slots == 1 ? "" : "s");
  1237. num_slots += bridge->nr_slots;
  1238. }
  1239. dbg("Total %d slots\n", num_slots);
  1240. return num_slots;
  1241. }
  1242. #if 0
  1243. /**
  1244. * acpiphp_for_each_slot - call function for each slot
  1245. * @fn: callback function
  1246. * @data: context to be passed to callback function
  1247. */
  1248. static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
  1249. {
  1250. struct list_head *node;
  1251. struct acpiphp_bridge *bridge;
  1252. struct acpiphp_slot *slot;
  1253. int retval = 0;
  1254. list_for_each (node, &bridge_list) {
  1255. bridge = (struct acpiphp_bridge *)node;
  1256. for (slot = bridge->slots; slot; slot = slot->next) {
  1257. retval = fn(slot, data);
  1258. if (!retval)
  1259. goto err_exit;
  1260. }
  1261. }
  1262. err_exit:
  1263. return retval;
  1264. }
  1265. #endif
  1266. /**
  1267. * acpiphp_enable_slot - power on slot
  1268. * @slot: ACPI PHP slot
  1269. */
  1270. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1271. {
  1272. int retval;
  1273. mutex_lock(&slot->crit_sect);
  1274. /* wake up all functions */
  1275. retval = power_on_slot(slot);
  1276. if (retval)
  1277. goto err_exit;
  1278. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1279. /* configure all functions */
  1280. retval = enable_device(slot);
  1281. if (retval)
  1282. power_off_slot(slot);
  1283. } else {
  1284. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1285. power_off_slot(slot);
  1286. }
  1287. err_exit:
  1288. mutex_unlock(&slot->crit_sect);
  1289. return retval;
  1290. }
  1291. /**
  1292. * acpiphp_disable_slot - power off slot
  1293. * @slot: ACPI PHP slot
  1294. */
  1295. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1296. {
  1297. int retval = 0;
  1298. mutex_lock(&slot->crit_sect);
  1299. /* unconfigure all functions */
  1300. retval = disable_device(slot);
  1301. if (retval)
  1302. goto err_exit;
  1303. /* power off all functions */
  1304. retval = power_off_slot(slot);
  1305. if (retval)
  1306. goto err_exit;
  1307. err_exit:
  1308. mutex_unlock(&slot->crit_sect);
  1309. return retval;
  1310. }
  1311. /*
  1312. * slot enabled: 1
  1313. * slot disabled: 0
  1314. */
  1315. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1316. {
  1317. return (slot->flags & SLOT_POWEREDON);
  1318. }
  1319. /*
  1320. * latch open: 1
  1321. * latch closed: 0
  1322. */
  1323. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1324. {
  1325. unsigned int sta;
  1326. sta = get_slot_status(slot);
  1327. return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
  1328. }
  1329. /*
  1330. * adapter presence : 1
  1331. * absence : 0
  1332. */
  1333. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1334. {
  1335. unsigned int sta;
  1336. sta = get_slot_status(slot);
  1337. return (sta == 0) ? 0 : 1;
  1338. }