acpiphp_glue.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. static DEFINE_MUTEX(bridge_mutex);
  53. #define MY_NAME "acpiphp_glue"
  54. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  55. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  56. static void acpiphp_set_hpp_values(struct pci_bus *bus);
  57. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
  58. static void free_bridge(struct kref *kref);
  59. /* callback routine to check for the existence of a pci dock device */
  60. static acpi_status
  61. is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
  62. {
  63. int *count = (int *)context;
  64. if (is_dock_device(handle)) {
  65. (*count)++;
  66. return AE_CTRL_TERMINATE;
  67. } else {
  68. return AE_OK;
  69. }
  70. }
  71. static inline void get_bridge(struct acpiphp_bridge *bridge)
  72. {
  73. kref_get(&bridge->ref);
  74. }
  75. static inline void put_bridge(struct acpiphp_bridge *bridge)
  76. {
  77. kref_put(&bridge->ref, free_bridge);
  78. }
  79. static void free_bridge(struct kref *kref)
  80. {
  81. struct acpiphp_bridge *bridge;
  82. struct acpiphp_slot *slot, *next;
  83. struct acpiphp_func *func, *tmp;
  84. bridge = container_of(kref, struct acpiphp_bridge, ref);
  85. list_for_each_entry_safe(slot, next, &bridge->slots, node) {
  86. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
  87. kfree(func);
  88. }
  89. kfree(slot);
  90. }
  91. /* Release reference acquired by acpiphp_bridge_handle_to_function() */
  92. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)
  93. put_bridge(bridge->func->slot->bridge);
  94. put_device(&bridge->pci_bus->dev);
  95. pci_dev_put(bridge->pci_dev);
  96. kfree(bridge);
  97. }
  98. /*
  99. * the _DCK method can do funny things... and sometimes not
  100. * hah-hah funny.
  101. *
  102. * TBD - figure out a way to only call fixups for
  103. * systems that require them.
  104. */
  105. static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
  106. void *v)
  107. {
  108. struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
  109. struct pci_bus *bus = func->slot->bridge->pci_bus;
  110. u32 buses;
  111. if (!bus->self)
  112. return NOTIFY_OK;
  113. /* fixup bad _DCK function that rewrites
  114. * secondary bridge on slot
  115. */
  116. pci_read_config_dword(bus->self,
  117. PCI_PRIMARY_BUS,
  118. &buses);
  119. if (((buses >> 8) & 0xff) != bus->busn_res.start) {
  120. buses = (buses & 0xff000000)
  121. | ((unsigned int)(bus->primary) << 0)
  122. | ((unsigned int)(bus->busn_res.start) << 8)
  123. | ((unsigned int)(bus->busn_res.end) << 16);
  124. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  125. }
  126. return NOTIFY_OK;
  127. }
  128. static const struct acpi_dock_ops acpiphp_dock_ops = {
  129. .handler = handle_hotplug_event_func,
  130. };
  131. /* Check whether the PCI device is managed by native PCIe hotplug driver */
  132. static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
  133. {
  134. u32 reg32;
  135. acpi_handle tmp;
  136. struct acpi_pci_root *root;
  137. /* Check whether the PCIe port supports native PCIe hotplug */
  138. if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
  139. return false;
  140. if (!(reg32 & PCI_EXP_SLTCAP_HPC))
  141. return false;
  142. /*
  143. * Check whether native PCIe hotplug has been enabled for
  144. * this PCIe hierarchy.
  145. */
  146. tmp = acpi_find_root_bridge_handle(pdev);
  147. if (!tmp)
  148. return false;
  149. root = acpi_pci_find_root(tmp);
  150. if (!root)
  151. return false;
  152. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  153. return false;
  154. return true;
  155. }
  156. /* callback routine to register each ACPI PCI slot object */
  157. static acpi_status
  158. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  159. {
  160. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
  161. struct acpiphp_slot *slot;
  162. struct acpiphp_func *newfunc;
  163. acpi_handle tmp;
  164. acpi_status status = AE_OK;
  165. unsigned long long adr, sun;
  166. int device, function, retval, found = 0;
  167. struct pci_bus *pbus = bridge->pci_bus;
  168. struct pci_dev *pdev;
  169. u32 val;
  170. if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
  171. return AE_OK;
  172. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  173. if (ACPI_FAILURE(status)) {
  174. warn("can't evaluate _ADR (%#x)\n", status);
  175. return AE_OK;
  176. }
  177. device = (adr >> 16) & 0xffff;
  178. function = adr & 0xffff;
  179. pdev = bridge->pci_dev;
  180. if (pdev && device_is_managed_by_native_pciehp(pdev))
  181. return AE_OK;
  182. newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
  183. if (!newfunc)
  184. return AE_NO_MEMORY;
  185. newfunc->handle = handle;
  186. newfunc->function = function;
  187. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  188. newfunc->flags = FUNC_HAS_EJ0;
  189. if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
  190. newfunc->flags |= FUNC_HAS_STA;
  191. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
  192. newfunc->flags |= FUNC_HAS_PS0;
  193. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
  194. newfunc->flags |= FUNC_HAS_PS3;
  195. if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
  196. newfunc->flags |= FUNC_HAS_DCK;
  197. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  198. if (ACPI_FAILURE(status)) {
  199. /*
  200. * use the count of the number of slots we've found
  201. * for the number of the slot
  202. */
  203. sun = bridge->nr_slots+1;
  204. }
  205. /* search for objects that share the same slot */
  206. list_for_each_entry(slot, &bridge->slots, node)
  207. if (slot->device == device) {
  208. if (slot->sun != sun)
  209. warn("sibling found, but _SUN doesn't match!\n");
  210. found = 1;
  211. break;
  212. }
  213. if (!found) {
  214. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  215. if (!slot) {
  216. kfree(newfunc);
  217. return AE_NO_MEMORY;
  218. }
  219. slot->bridge = bridge;
  220. slot->device = device;
  221. slot->sun = sun;
  222. INIT_LIST_HEAD(&slot->funcs);
  223. mutex_init(&slot->crit_sect);
  224. mutex_lock(&bridge_mutex);
  225. list_add_tail(&slot->node, &bridge->slots);
  226. mutex_unlock(&bridge_mutex);
  227. bridge->nr_slots++;
  228. dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  229. slot->sun, pci_domain_nr(pbus), pbus->number, device);
  230. retval = acpiphp_register_hotplug_slot(slot);
  231. if (retval) {
  232. if (retval == -EBUSY)
  233. warn("Slot %llu already registered by another "
  234. "hotplug driver\n", slot->sun);
  235. else
  236. warn("acpiphp_register_hotplug_slot failed "
  237. "(err code = 0x%x)\n", retval);
  238. goto err_exit;
  239. }
  240. }
  241. newfunc->slot = slot;
  242. mutex_lock(&bridge_mutex);
  243. list_add_tail(&newfunc->sibling, &slot->funcs);
  244. mutex_unlock(&bridge_mutex);
  245. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  246. &val, 60*1000))
  247. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  248. if (is_dock_device(handle)) {
  249. /* we don't want to call this device's _EJ0
  250. * because we want the dock notify handler
  251. * to call it after it calls _DCK
  252. */
  253. newfunc->flags &= ~FUNC_HAS_EJ0;
  254. if (register_hotplug_dock_device(handle,
  255. &acpiphp_dock_ops, newfunc))
  256. dbg("failed to register dock device\n");
  257. /* we need to be notified when dock events happen
  258. * outside of the hotplug operation, since we may
  259. * need to do fixups before we can hotplug.
  260. */
  261. newfunc->nb.notifier_call = post_dock_fixups;
  262. if (register_dock_notifier(&newfunc->nb))
  263. dbg("failed to register a dock notifier");
  264. }
  265. /* install notify handler */
  266. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  267. status = acpi_install_notify_handler(handle,
  268. ACPI_SYSTEM_NOTIFY,
  269. handle_hotplug_event_func,
  270. newfunc);
  271. if (ACPI_FAILURE(status))
  272. err("failed to register interrupt notify handler\n");
  273. } else
  274. status = AE_OK;
  275. return status;
  276. err_exit:
  277. bridge->nr_slots--;
  278. mutex_lock(&bridge_mutex);
  279. list_del(&slot->node);
  280. mutex_unlock(&bridge_mutex);
  281. kfree(slot);
  282. kfree(newfunc);
  283. return AE_OK;
  284. }
  285. /* see if it's worth looking at this bridge */
  286. static int detect_ejectable_slots(acpi_handle handle)
  287. {
  288. int found = acpi_pci_detect_ejectable(handle);
  289. if (!found) {
  290. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  291. is_pci_dock_device, NULL, (void *)&found, NULL);
  292. }
  293. return found;
  294. }
  295. /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
  296. static void init_bridge_misc(struct acpiphp_bridge *bridge)
  297. {
  298. acpi_status status;
  299. /* must be added to the list prior to calling register_slot */
  300. mutex_lock(&bridge_mutex);
  301. list_add(&bridge->list, &bridge_list);
  302. mutex_unlock(&bridge_mutex);
  303. /* register all slot objects under this bridge */
  304. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
  305. register_slot, NULL, bridge, NULL);
  306. if (ACPI_FAILURE(status)) {
  307. mutex_lock(&bridge_mutex);
  308. list_del(&bridge->list);
  309. mutex_unlock(&bridge_mutex);
  310. return;
  311. }
  312. /* install notify handler for P2P bridges */
  313. if (!pci_is_root_bus(bridge->pci_bus)) {
  314. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  315. status = acpi_remove_notify_handler(bridge->func->handle,
  316. ACPI_SYSTEM_NOTIFY,
  317. handle_hotplug_event_func);
  318. if (ACPI_FAILURE(status))
  319. err("failed to remove notify handler\n");
  320. }
  321. status = acpi_install_notify_handler(bridge->handle,
  322. ACPI_SYSTEM_NOTIFY,
  323. handle_hotplug_event_bridge,
  324. bridge);
  325. if (ACPI_FAILURE(status)) {
  326. err("failed to register interrupt notify handler\n");
  327. }
  328. }
  329. }
  330. /* find acpiphp_func from acpiphp_bridge */
  331. static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
  332. {
  333. struct acpiphp_bridge *bridge;
  334. struct acpiphp_slot *slot;
  335. struct acpiphp_func *func = NULL;
  336. mutex_lock(&bridge_mutex);
  337. list_for_each_entry(bridge, &bridge_list, list) {
  338. list_for_each_entry(slot, &bridge->slots, node) {
  339. list_for_each_entry(func, &slot->funcs, sibling) {
  340. if (func->handle == handle) {
  341. get_bridge(func->slot->bridge);
  342. mutex_unlock(&bridge_mutex);
  343. return func;
  344. }
  345. }
  346. }
  347. }
  348. mutex_unlock(&bridge_mutex);
  349. return NULL;
  350. }
  351. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  352. {
  353. struct acpiphp_bridge *bridge;
  354. mutex_lock(&bridge_mutex);
  355. list_for_each_entry(bridge, &bridge_list, list)
  356. if (bridge->handle == handle) {
  357. get_bridge(bridge);
  358. mutex_unlock(&bridge_mutex);
  359. return bridge;
  360. }
  361. mutex_unlock(&bridge_mutex);
  362. return NULL;
  363. }
  364. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  365. {
  366. struct acpiphp_slot *slot;
  367. struct acpiphp_func *func;
  368. acpi_status status;
  369. acpi_handle handle = bridge->handle;
  370. if (!pci_is_root_bus(bridge->pci_bus)) {
  371. status = acpi_remove_notify_handler(handle,
  372. ACPI_SYSTEM_NOTIFY,
  373. handle_hotplug_event_bridge);
  374. if (ACPI_FAILURE(status))
  375. err("failed to remove notify handler\n");
  376. }
  377. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  378. status = acpi_install_notify_handler(bridge->func->handle,
  379. ACPI_SYSTEM_NOTIFY,
  380. handle_hotplug_event_func,
  381. bridge->func);
  382. if (ACPI_FAILURE(status))
  383. err("failed to install interrupt notify handler\n");
  384. }
  385. list_for_each_entry(slot, &bridge->slots, node) {
  386. list_for_each_entry(func, &slot->funcs, sibling) {
  387. if (is_dock_device(func->handle)) {
  388. unregister_hotplug_dock_device(func->handle);
  389. unregister_dock_notifier(&func->nb);
  390. }
  391. if (!(func->flags & FUNC_HAS_DCK)) {
  392. status = acpi_remove_notify_handler(func->handle,
  393. ACPI_SYSTEM_NOTIFY,
  394. handle_hotplug_event_func);
  395. if (ACPI_FAILURE(status))
  396. err("failed to remove notify handler\n");
  397. }
  398. }
  399. acpiphp_unregister_hotplug_slot(slot);
  400. }
  401. mutex_lock(&bridge_mutex);
  402. list_del(&bridge->list);
  403. mutex_unlock(&bridge_mutex);
  404. }
  405. static int power_on_slot(struct acpiphp_slot *slot)
  406. {
  407. acpi_status status;
  408. struct acpiphp_func *func;
  409. int retval = 0;
  410. /* if already enabled, just skip */
  411. if (slot->flags & SLOT_POWEREDON)
  412. goto err_exit;
  413. list_for_each_entry(func, &slot->funcs, sibling) {
  414. if (func->flags & FUNC_HAS_PS0) {
  415. dbg("%s: executing _PS0\n", __func__);
  416. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  417. if (ACPI_FAILURE(status)) {
  418. warn("%s: _PS0 failed\n", __func__);
  419. retval = -1;
  420. goto err_exit;
  421. } else
  422. break;
  423. }
  424. }
  425. /* TBD: evaluate _STA to check if the slot is enabled */
  426. slot->flags |= SLOT_POWEREDON;
  427. err_exit:
  428. return retval;
  429. }
  430. static int power_off_slot(struct acpiphp_slot *slot)
  431. {
  432. acpi_status status;
  433. struct acpiphp_func *func;
  434. int retval = 0;
  435. /* if already disabled, just skip */
  436. if ((slot->flags & SLOT_POWEREDON) == 0)
  437. goto err_exit;
  438. list_for_each_entry(func, &slot->funcs, sibling) {
  439. if (func->flags & FUNC_HAS_PS3) {
  440. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  441. if (ACPI_FAILURE(status)) {
  442. warn("%s: _PS3 failed\n", __func__);
  443. retval = -1;
  444. goto err_exit;
  445. } else
  446. break;
  447. }
  448. }
  449. /* TBD: evaluate _STA to check if the slot is disabled */
  450. slot->flags &= (~SLOT_POWEREDON);
  451. err_exit:
  452. return retval;
  453. }
  454. /**
  455. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  456. * @bus: bus to start search with
  457. */
  458. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  459. {
  460. struct list_head *tmp;
  461. unsigned char max, n;
  462. /*
  463. * pci_bus_max_busnr will return the highest
  464. * reserved busnr for all these children.
  465. * that is equivalent to the bus->subordinate
  466. * value. We don't want to use the parent's
  467. * bus->subordinate value because it could have
  468. * padding in it.
  469. */
  470. max = bus->busn_res.start;
  471. list_for_each(tmp, &bus->children) {
  472. n = pci_bus_max_busnr(pci_bus_b(tmp));
  473. if (n > max)
  474. max = n;
  475. }
  476. return max;
  477. }
  478. /**
  479. * acpiphp_bus_add - add a new bus to acpi subsystem
  480. * @func: acpiphp_func of the bridge
  481. */
  482. static int acpiphp_bus_add(struct acpiphp_func *func)
  483. {
  484. struct acpi_device *device;
  485. int ret_val;
  486. if (!acpi_bus_get_device(func->handle, &device)) {
  487. dbg("bus exists... trim\n");
  488. /* this shouldn't be in here, so remove
  489. * the bus then re-add it...
  490. */
  491. acpi_bus_trim(device);
  492. }
  493. ret_val = acpi_bus_scan(func->handle);
  494. if (!ret_val)
  495. ret_val = acpi_bus_get_device(func->handle, &device);
  496. if (ret_val)
  497. dbg("error adding bus, %x\n", -ret_val);
  498. return ret_val;
  499. }
  500. /**
  501. * acpiphp_bus_trim - trim a bus from acpi subsystem
  502. * @handle: handle to acpi namespace
  503. */
  504. static int acpiphp_bus_trim(acpi_handle handle)
  505. {
  506. struct acpi_device *device;
  507. int retval;
  508. retval = acpi_bus_get_device(handle, &device);
  509. if (retval) {
  510. dbg("acpi_device not found\n");
  511. return retval;
  512. }
  513. acpi_bus_trim(device);
  514. return 0;
  515. }
  516. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  517. {
  518. struct acpiphp_func *func;
  519. union acpi_object params[2];
  520. struct acpi_object_list arg_list;
  521. list_for_each_entry(func, &slot->funcs, sibling) {
  522. arg_list.count = 2;
  523. arg_list.pointer = params;
  524. params[0].type = ACPI_TYPE_INTEGER;
  525. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  526. params[1].type = ACPI_TYPE_INTEGER;
  527. params[1].integer.value = 1;
  528. /* _REG is optional, we don't care about if there is failure */
  529. acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
  530. }
  531. }
  532. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  533. {
  534. struct acpiphp_func *func;
  535. if (!dev->subordinate)
  536. return;
  537. /* quirk, or pcie could set it already */
  538. if (dev->is_hotplug_bridge)
  539. return;
  540. if (PCI_SLOT(dev->devfn) != slot->device)
  541. return;
  542. list_for_each_entry(func, &slot->funcs, sibling) {
  543. if (PCI_FUNC(dev->devfn) == func->function) {
  544. /* check if this bridge has ejectable slots */
  545. if ((detect_ejectable_slots(func->handle) > 0))
  546. dev->is_hotplug_bridge = 1;
  547. break;
  548. }
  549. }
  550. }
  551. /**
  552. * enable_device - enable, configure a slot
  553. * @slot: slot to be enabled
  554. *
  555. * This function should be called per *physical slot*,
  556. * not per each slot object in ACPI namespace.
  557. */
  558. static int __ref enable_device(struct acpiphp_slot *slot)
  559. {
  560. struct pci_dev *dev;
  561. struct pci_bus *bus = slot->bridge->pci_bus;
  562. struct acpiphp_func *func;
  563. int num, max, pass;
  564. if (slot->flags & SLOT_ENABLED)
  565. goto err_exit;
  566. list_for_each_entry(func, &slot->funcs, sibling)
  567. acpiphp_bus_add(func);
  568. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  569. if (num == 0) {
  570. /* Maybe only part of funcs are added. */
  571. dbg("No new device found\n");
  572. goto err_exit;
  573. }
  574. max = acpiphp_max_busnr(bus);
  575. for (pass = 0; pass < 2; pass++) {
  576. list_for_each_entry(dev, &bus->devices, bus_list) {
  577. if (PCI_SLOT(dev->devfn) != slot->device)
  578. continue;
  579. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  580. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  581. max = pci_scan_bridge(bus, dev, max, pass);
  582. if (pass && dev->subordinate) {
  583. check_hotplug_bridge(slot, dev);
  584. pci_bus_size_bridges(dev->subordinate);
  585. }
  586. }
  587. }
  588. }
  589. pci_bus_assign_resources(bus);
  590. acpiphp_sanitize_bus(bus);
  591. acpiphp_set_hpp_values(bus);
  592. acpiphp_set_acpi_region(slot);
  593. pci_enable_bridges(bus);
  594. list_for_each_entry(dev, &bus->devices, bus_list) {
  595. /* Assume that newly added devices are powered on already. */
  596. if (!dev->is_added)
  597. dev->current_state = PCI_D0;
  598. }
  599. pci_bus_add_devices(bus);
  600. slot->flags |= SLOT_ENABLED;
  601. list_for_each_entry(func, &slot->funcs, sibling) {
  602. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  603. func->function));
  604. if (!dev) {
  605. /* Do not set SLOT_ENABLED flag if some funcs
  606. are not added. */
  607. slot->flags &= (~SLOT_ENABLED);
  608. continue;
  609. }
  610. }
  611. err_exit:
  612. return 0;
  613. }
  614. /* return first device in slot, acquiring a reference on it */
  615. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  616. {
  617. struct pci_bus *bus = slot->bridge->pci_bus;
  618. struct pci_dev *dev;
  619. struct pci_dev *ret = NULL;
  620. down_read(&pci_bus_sem);
  621. list_for_each_entry(dev, &bus->devices, bus_list)
  622. if (PCI_SLOT(dev->devfn) == slot->device) {
  623. ret = pci_dev_get(dev);
  624. break;
  625. }
  626. up_read(&pci_bus_sem);
  627. return ret;
  628. }
  629. /**
  630. * disable_device - disable a slot
  631. * @slot: ACPI PHP slot
  632. */
  633. static int disable_device(struct acpiphp_slot *slot)
  634. {
  635. struct acpiphp_func *func;
  636. struct pci_dev *pdev;
  637. /*
  638. * enable_device() enumerates all functions in this device via
  639. * pci_scan_slot(), whether they have associated ACPI hotplug
  640. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  641. * here.
  642. */
  643. while ((pdev = dev_in_slot(slot))) {
  644. pci_stop_and_remove_bus_device(pdev);
  645. pci_dev_put(pdev);
  646. }
  647. list_for_each_entry(func, &slot->funcs, sibling) {
  648. acpiphp_bus_trim(func->handle);
  649. }
  650. slot->flags &= (~SLOT_ENABLED);
  651. return 0;
  652. }
  653. /**
  654. * get_slot_status - get ACPI slot status
  655. * @slot: ACPI PHP slot
  656. *
  657. * If a slot has _STA for each function and if any one of them
  658. * returned non-zero status, return it.
  659. *
  660. * If a slot doesn't have _STA and if any one of its functions'
  661. * configuration space is configured, return 0x0f as a _STA.
  662. *
  663. * Otherwise return 0.
  664. */
  665. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  666. {
  667. acpi_status status;
  668. unsigned long long sta = 0;
  669. u32 dvid;
  670. struct acpiphp_func *func;
  671. list_for_each_entry(func, &slot->funcs, sibling) {
  672. if (func->flags & FUNC_HAS_STA) {
  673. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  674. if (ACPI_SUCCESS(status) && sta)
  675. break;
  676. } else {
  677. pci_bus_read_config_dword(slot->bridge->pci_bus,
  678. PCI_DEVFN(slot->device,
  679. func->function),
  680. PCI_VENDOR_ID, &dvid);
  681. if (dvid != 0xffffffff) {
  682. sta = ACPI_STA_ALL;
  683. break;
  684. }
  685. }
  686. }
  687. return (unsigned int)sta;
  688. }
  689. /**
  690. * acpiphp_eject_slot - physically eject the slot
  691. * @slot: ACPI PHP slot
  692. */
  693. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  694. {
  695. acpi_status status;
  696. struct acpiphp_func *func;
  697. struct acpi_object_list arg_list;
  698. union acpi_object arg;
  699. list_for_each_entry(func, &slot->funcs, sibling) {
  700. /* We don't want to call _EJ0 on non-existing functions. */
  701. if ((func->flags & FUNC_HAS_EJ0)) {
  702. /* _EJ0 method take one argument */
  703. arg_list.count = 1;
  704. arg_list.pointer = &arg;
  705. arg.type = ACPI_TYPE_INTEGER;
  706. arg.integer.value = 1;
  707. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  708. if (ACPI_FAILURE(status)) {
  709. warn("%s: _EJ0 failed\n", __func__);
  710. return -1;
  711. } else
  712. break;
  713. }
  714. }
  715. return 0;
  716. }
  717. /**
  718. * acpiphp_check_bridge - re-enumerate devices
  719. * @bridge: where to begin re-enumeration
  720. *
  721. * Iterate over all slots under this bridge and make sure that if a
  722. * card is present they are enabled, and if not they are disabled.
  723. */
  724. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  725. {
  726. struct acpiphp_slot *slot;
  727. int retval = 0;
  728. int enabled, disabled;
  729. enabled = disabled = 0;
  730. list_for_each_entry(slot, &bridge->slots, node) {
  731. unsigned int status = get_slot_status(slot);
  732. if (slot->flags & SLOT_ENABLED) {
  733. if (status == ACPI_STA_ALL)
  734. continue;
  735. retval = acpiphp_disable_slot(slot);
  736. if (retval) {
  737. err("Error occurred in disabling\n");
  738. goto err_exit;
  739. } else {
  740. acpiphp_eject_slot(slot);
  741. }
  742. disabled++;
  743. } else {
  744. if (status != ACPI_STA_ALL)
  745. continue;
  746. retval = acpiphp_enable_slot(slot);
  747. if (retval) {
  748. err("Error occurred in enabling\n");
  749. goto err_exit;
  750. }
  751. enabled++;
  752. }
  753. }
  754. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  755. err_exit:
  756. return retval;
  757. }
  758. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  759. {
  760. struct pci_dev *dev;
  761. list_for_each_entry(dev, &bus->devices, bus_list)
  762. pci_configure_slot(dev);
  763. }
  764. /*
  765. * Remove devices for which we could not assign resources, call
  766. * arch specific code to fix-up the bus
  767. */
  768. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  769. {
  770. struct pci_dev *dev, *tmp;
  771. int i;
  772. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  773. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  774. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  775. struct resource *res = &dev->resource[i];
  776. if ((res->flags & type_mask) && !res->start &&
  777. res->end) {
  778. /* Could not assign a required resources
  779. * for this device, remove it */
  780. pci_stop_and_remove_bus_device(dev);
  781. break;
  782. }
  783. }
  784. }
  785. }
  786. /*
  787. * ACPI event handlers
  788. */
  789. static acpi_status
  790. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  791. {
  792. struct acpiphp_bridge *bridge;
  793. char objname[64];
  794. struct acpi_buffer buffer = { .length = sizeof(objname),
  795. .pointer = objname };
  796. bridge = acpiphp_handle_to_bridge(handle);
  797. if (bridge) {
  798. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  799. dbg("%s: re-enumerating slots under %s\n",
  800. __func__, objname);
  801. acpiphp_check_bridge(bridge);
  802. put_bridge(bridge);
  803. }
  804. return AE_OK ;
  805. }
  806. void acpiphp_check_host_bridge(acpi_handle handle)
  807. {
  808. struct acpiphp_bridge *bridge;
  809. bridge = acpiphp_handle_to_bridge(handle);
  810. if (bridge) {
  811. acpiphp_check_bridge(bridge);
  812. put_bridge(bridge);
  813. }
  814. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  815. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  816. }
  817. static void _handle_hotplug_event_bridge(struct work_struct *work)
  818. {
  819. struct acpiphp_bridge *bridge;
  820. char objname[64];
  821. struct acpi_buffer buffer = { .length = sizeof(objname),
  822. .pointer = objname };
  823. struct acpi_hp_work *hp_work;
  824. acpi_handle handle;
  825. u32 type;
  826. hp_work = container_of(work, struct acpi_hp_work, work);
  827. handle = hp_work->handle;
  828. type = hp_work->type;
  829. bridge = (struct acpiphp_bridge *)hp_work->context;
  830. acpi_scan_lock_acquire();
  831. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  832. switch (type) {
  833. case ACPI_NOTIFY_BUS_CHECK:
  834. /* bus re-enumerate */
  835. dbg("%s: Bus check notify on %s\n", __func__, objname);
  836. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  837. acpiphp_check_bridge(bridge);
  838. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  839. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  840. break;
  841. case ACPI_NOTIFY_DEVICE_CHECK:
  842. /* device check */
  843. dbg("%s: Device check notify on %s\n", __func__, objname);
  844. acpiphp_check_bridge(bridge);
  845. break;
  846. case ACPI_NOTIFY_DEVICE_WAKE:
  847. /* wake event */
  848. dbg("%s: Device wake notify on %s\n", __func__, objname);
  849. break;
  850. case ACPI_NOTIFY_EJECT_REQUEST:
  851. /* request device eject */
  852. dbg("%s: Device eject notify on %s\n", __func__, objname);
  853. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  854. struct acpiphp_slot *slot;
  855. slot = bridge->func->slot;
  856. if (!acpiphp_disable_slot(slot))
  857. acpiphp_eject_slot(slot);
  858. }
  859. break;
  860. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  861. printk(KERN_ERR "Device %s cannot be configured due"
  862. " to a frequency mismatch\n", objname);
  863. break;
  864. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  865. printk(KERN_ERR "Device %s cannot be configured due"
  866. " to a bus mode mismatch\n", objname);
  867. break;
  868. case ACPI_NOTIFY_POWER_FAULT:
  869. printk(KERN_ERR "Device %s has suffered a power fault\n",
  870. objname);
  871. break;
  872. default:
  873. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  874. break;
  875. }
  876. acpi_scan_lock_release();
  877. kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
  878. put_bridge(bridge);
  879. }
  880. /**
  881. * handle_hotplug_event_bridge - handle ACPI event on bridges
  882. * @handle: Notify()'ed acpi_handle
  883. * @type: Notify code
  884. * @context: pointer to acpiphp_bridge structure
  885. *
  886. * Handles ACPI event notification on {host,p2p} bridges.
  887. */
  888. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
  889. void *context)
  890. {
  891. struct acpiphp_bridge *bridge = context;
  892. /*
  893. * Currently the code adds all hotplug events to the kacpid_wq
  894. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  895. * The proper way to fix this is to reorganize the code so that
  896. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  897. * For now just re-add this work to the kacpi_hotplug_wq so we
  898. * don't deadlock on hotplug actions.
  899. */
  900. get_bridge(bridge);
  901. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
  902. }
  903. static void _handle_hotplug_event_func(struct work_struct *work)
  904. {
  905. struct acpiphp_func *func;
  906. char objname[64];
  907. struct acpi_buffer buffer = { .length = sizeof(objname),
  908. .pointer = objname };
  909. struct acpi_hp_work *hp_work;
  910. acpi_handle handle;
  911. u32 type;
  912. hp_work = container_of(work, struct acpi_hp_work, work);
  913. handle = hp_work->handle;
  914. type = hp_work->type;
  915. func = (struct acpiphp_func *)hp_work->context;
  916. acpi_scan_lock_acquire();
  917. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  918. switch (type) {
  919. case ACPI_NOTIFY_BUS_CHECK:
  920. /* bus re-enumerate */
  921. dbg("%s: Bus check notify on %s\n", __func__, objname);
  922. acpiphp_enable_slot(func->slot);
  923. break;
  924. case ACPI_NOTIFY_DEVICE_CHECK:
  925. /* device check : re-enumerate from parent bus */
  926. dbg("%s: Device check notify on %s\n", __func__, objname);
  927. acpiphp_check_bridge(func->slot->bridge);
  928. break;
  929. case ACPI_NOTIFY_DEVICE_WAKE:
  930. /* wake event */
  931. dbg("%s: Device wake notify on %s\n", __func__, objname);
  932. break;
  933. case ACPI_NOTIFY_EJECT_REQUEST:
  934. /* request device eject */
  935. dbg("%s: Device eject notify on %s\n", __func__, objname);
  936. if (!(acpiphp_disable_slot(func->slot)))
  937. acpiphp_eject_slot(func->slot);
  938. break;
  939. default:
  940. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  941. break;
  942. }
  943. acpi_scan_lock_release();
  944. kfree(hp_work); /* allocated in handle_hotplug_event_func */
  945. put_bridge(func->slot->bridge);
  946. }
  947. /**
  948. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  949. * @handle: Notify()'ed acpi_handle
  950. * @type: Notify code
  951. * @context: pointer to acpiphp_func structure
  952. *
  953. * Handles ACPI event notification on slots.
  954. */
  955. static void handle_hotplug_event_func(acpi_handle handle, u32 type,
  956. void *context)
  957. {
  958. struct acpiphp_func *func = context;
  959. /*
  960. * Currently the code adds all hotplug events to the kacpid_wq
  961. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  962. * The proper way to fix this is to reorganize the code so that
  963. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  964. * For now just re-add this work to the kacpi_hotplug_wq so we
  965. * don't deadlock on hotplug actions.
  966. */
  967. get_bridge(func->slot->bridge);
  968. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
  969. }
  970. /*
  971. * Create hotplug slots for the PCI bus.
  972. * It should always return 0 to avoid skipping following notifiers.
  973. */
  974. void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
  975. {
  976. acpi_handle dummy_handle;
  977. struct acpiphp_bridge *bridge;
  978. if (acpiphp_disabled)
  979. return;
  980. if (detect_ejectable_slots(handle) <= 0)
  981. return;
  982. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  983. if (bridge == NULL) {
  984. err("out of memory\n");
  985. return;
  986. }
  987. INIT_LIST_HEAD(&bridge->slots);
  988. kref_init(&bridge->ref);
  989. bridge->handle = handle;
  990. bridge->pci_dev = pci_dev_get(bus->self);
  991. bridge->pci_bus = bus;
  992. /*
  993. * Grab a ref to the subordinate PCI bus in case the bus is
  994. * removed via PCI core logical hotplug. The ref pins the bus
  995. * (which we access during module unload).
  996. */
  997. get_device(&bus->dev);
  998. if (!pci_is_root_bus(bridge->pci_bus) &&
  999. ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  1000. "_EJ0", &dummy_handle))) {
  1001. dbg("found ejectable p2p bridge\n");
  1002. bridge->flags |= BRIDGE_HAS_EJ0;
  1003. bridge->func = acpiphp_bridge_handle_to_function(handle);
  1004. }
  1005. init_bridge_misc(bridge);
  1006. }
  1007. /* Destroy hotplug slots associated with the PCI bus */
  1008. void acpiphp_remove_slots(struct pci_bus *bus)
  1009. {
  1010. struct acpiphp_bridge *bridge, *tmp;
  1011. if (acpiphp_disabled)
  1012. return;
  1013. list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
  1014. if (bridge->pci_bus == bus) {
  1015. cleanup_bridge(bridge);
  1016. put_bridge(bridge);
  1017. break;
  1018. }
  1019. }
  1020. /**
  1021. * acpiphp_enable_slot - power on slot
  1022. * @slot: ACPI PHP slot
  1023. */
  1024. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1025. {
  1026. int retval;
  1027. mutex_lock(&slot->crit_sect);
  1028. /* wake up all functions */
  1029. retval = power_on_slot(slot);
  1030. if (retval)
  1031. goto err_exit;
  1032. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1033. /* configure all functions */
  1034. retval = enable_device(slot);
  1035. if (retval)
  1036. power_off_slot(slot);
  1037. } else {
  1038. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1039. power_off_slot(slot);
  1040. }
  1041. err_exit:
  1042. mutex_unlock(&slot->crit_sect);
  1043. return retval;
  1044. }
  1045. /**
  1046. * acpiphp_disable_slot - power off slot
  1047. * @slot: ACPI PHP slot
  1048. */
  1049. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1050. {
  1051. int retval = 0;
  1052. mutex_lock(&slot->crit_sect);
  1053. /* unconfigure all functions */
  1054. retval = disable_device(slot);
  1055. if (retval)
  1056. goto err_exit;
  1057. /* power off all functions */
  1058. retval = power_off_slot(slot);
  1059. if (retval)
  1060. goto err_exit;
  1061. err_exit:
  1062. mutex_unlock(&slot->crit_sect);
  1063. return retval;
  1064. }
  1065. /*
  1066. * slot enabled: 1
  1067. * slot disabled: 0
  1068. */
  1069. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1070. {
  1071. return (slot->flags & SLOT_POWEREDON);
  1072. }
  1073. /*
  1074. * latch open: 1
  1075. * latch closed: 0
  1076. */
  1077. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1078. {
  1079. unsigned int sta;
  1080. sta = get_slot_status(slot);
  1081. return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
  1082. }
  1083. /*
  1084. * adapter presence : 1
  1085. * absence : 0
  1086. */
  1087. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1088. {
  1089. unsigned int sta;
  1090. sta = get_slot_status(slot);
  1091. return (sta == 0) ? 0 : 1;
  1092. }