acpiphp_glue.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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. LIST_HEAD(add_list);
  565. if (slot->flags & SLOT_ENABLED)
  566. goto err_exit;
  567. list_for_each_entry(func, &slot->funcs, sibling)
  568. acpiphp_bus_add(func);
  569. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  570. if (num == 0) {
  571. /* Maybe only part of funcs are added. */
  572. dbg("No new device found\n");
  573. goto err_exit;
  574. }
  575. max = acpiphp_max_busnr(bus);
  576. for (pass = 0; pass < 2; pass++) {
  577. list_for_each_entry(dev, &bus->devices, bus_list) {
  578. if (PCI_SLOT(dev->devfn) != slot->device)
  579. continue;
  580. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  581. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  582. max = pci_scan_bridge(bus, dev, max, pass);
  583. if (pass && dev->subordinate) {
  584. check_hotplug_bridge(slot, dev);
  585. pcibios_resource_survey_bus(dev->subordinate);
  586. __pci_bus_size_bridges(dev->subordinate,
  587. &add_list);
  588. }
  589. }
  590. }
  591. }
  592. __pci_bus_assign_resources(bus, &add_list, NULL);
  593. acpiphp_sanitize_bus(bus);
  594. acpiphp_set_hpp_values(bus);
  595. acpiphp_set_acpi_region(slot);
  596. pci_enable_bridges(bus);
  597. list_for_each_entry(dev, &bus->devices, bus_list) {
  598. /* Assume that newly added devices are powered on already. */
  599. if (!dev->is_added)
  600. dev->current_state = PCI_D0;
  601. }
  602. pci_bus_add_devices(bus);
  603. slot->flags |= SLOT_ENABLED;
  604. list_for_each_entry(func, &slot->funcs, sibling) {
  605. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  606. func->function));
  607. if (!dev) {
  608. /* Do not set SLOT_ENABLED flag if some funcs
  609. are not added. */
  610. slot->flags &= (~SLOT_ENABLED);
  611. continue;
  612. }
  613. }
  614. err_exit:
  615. return 0;
  616. }
  617. /* return first device in slot, acquiring a reference on it */
  618. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  619. {
  620. struct pci_bus *bus = slot->bridge->pci_bus;
  621. struct pci_dev *dev;
  622. struct pci_dev *ret = NULL;
  623. down_read(&pci_bus_sem);
  624. list_for_each_entry(dev, &bus->devices, bus_list)
  625. if (PCI_SLOT(dev->devfn) == slot->device) {
  626. ret = pci_dev_get(dev);
  627. break;
  628. }
  629. up_read(&pci_bus_sem);
  630. return ret;
  631. }
  632. /**
  633. * disable_device - disable a slot
  634. * @slot: ACPI PHP slot
  635. */
  636. static int disable_device(struct acpiphp_slot *slot)
  637. {
  638. struct acpiphp_func *func;
  639. struct pci_dev *pdev;
  640. /*
  641. * enable_device() enumerates all functions in this device via
  642. * pci_scan_slot(), whether they have associated ACPI hotplug
  643. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  644. * here.
  645. */
  646. while ((pdev = dev_in_slot(slot))) {
  647. pci_stop_and_remove_bus_device(pdev);
  648. pci_dev_put(pdev);
  649. }
  650. list_for_each_entry(func, &slot->funcs, sibling) {
  651. acpiphp_bus_trim(func->handle);
  652. }
  653. slot->flags &= (~SLOT_ENABLED);
  654. return 0;
  655. }
  656. /**
  657. * get_slot_status - get ACPI slot status
  658. * @slot: ACPI PHP slot
  659. *
  660. * If a slot has _STA for each function and if any one of them
  661. * returned non-zero status, return it.
  662. *
  663. * If a slot doesn't have _STA and if any one of its functions'
  664. * configuration space is configured, return 0x0f as a _STA.
  665. *
  666. * Otherwise return 0.
  667. */
  668. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  669. {
  670. acpi_status status;
  671. unsigned long long sta = 0;
  672. u32 dvid;
  673. struct acpiphp_func *func;
  674. list_for_each_entry(func, &slot->funcs, sibling) {
  675. if (func->flags & FUNC_HAS_STA) {
  676. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  677. if (ACPI_SUCCESS(status) && sta)
  678. break;
  679. } else {
  680. pci_bus_read_config_dword(slot->bridge->pci_bus,
  681. PCI_DEVFN(slot->device,
  682. func->function),
  683. PCI_VENDOR_ID, &dvid);
  684. if (dvid != 0xffffffff) {
  685. sta = ACPI_STA_ALL;
  686. break;
  687. }
  688. }
  689. }
  690. return (unsigned int)sta;
  691. }
  692. /**
  693. * acpiphp_eject_slot - physically eject the slot
  694. * @slot: ACPI PHP slot
  695. */
  696. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  697. {
  698. acpi_status status;
  699. struct acpiphp_func *func;
  700. struct acpi_object_list arg_list;
  701. union acpi_object arg;
  702. list_for_each_entry(func, &slot->funcs, sibling) {
  703. /* We don't want to call _EJ0 on non-existing functions. */
  704. if ((func->flags & FUNC_HAS_EJ0)) {
  705. /* _EJ0 method take one argument */
  706. arg_list.count = 1;
  707. arg_list.pointer = &arg;
  708. arg.type = ACPI_TYPE_INTEGER;
  709. arg.integer.value = 1;
  710. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  711. if (ACPI_FAILURE(status)) {
  712. warn("%s: _EJ0 failed\n", __func__);
  713. return -1;
  714. } else
  715. break;
  716. }
  717. }
  718. return 0;
  719. }
  720. /**
  721. * acpiphp_check_bridge - re-enumerate devices
  722. * @bridge: where to begin re-enumeration
  723. *
  724. * Iterate over all slots under this bridge and make sure that if a
  725. * card is present they are enabled, and if not they are disabled.
  726. */
  727. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  728. {
  729. struct acpiphp_slot *slot;
  730. int retval = 0;
  731. int enabled, disabled;
  732. enabled = disabled = 0;
  733. list_for_each_entry(slot, &bridge->slots, node) {
  734. unsigned int status = get_slot_status(slot);
  735. if (slot->flags & SLOT_ENABLED) {
  736. if (status == ACPI_STA_ALL)
  737. continue;
  738. retval = acpiphp_disable_slot(slot);
  739. if (retval) {
  740. err("Error occurred in disabling\n");
  741. goto err_exit;
  742. } else {
  743. acpiphp_eject_slot(slot);
  744. }
  745. disabled++;
  746. } else {
  747. if (status != ACPI_STA_ALL)
  748. continue;
  749. retval = acpiphp_enable_slot(slot);
  750. if (retval) {
  751. err("Error occurred in enabling\n");
  752. goto err_exit;
  753. }
  754. enabled++;
  755. }
  756. }
  757. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  758. err_exit:
  759. return retval;
  760. }
  761. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  762. {
  763. struct pci_dev *dev;
  764. list_for_each_entry(dev, &bus->devices, bus_list)
  765. pci_configure_slot(dev);
  766. }
  767. /*
  768. * Remove devices for which we could not assign resources, call
  769. * arch specific code to fix-up the bus
  770. */
  771. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  772. {
  773. struct pci_dev *dev, *tmp;
  774. int i;
  775. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  776. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  777. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  778. struct resource *res = &dev->resource[i];
  779. if ((res->flags & type_mask) && !res->start &&
  780. res->end) {
  781. /* Could not assign a required resources
  782. * for this device, remove it */
  783. pci_stop_and_remove_bus_device(dev);
  784. break;
  785. }
  786. }
  787. }
  788. }
  789. /*
  790. * ACPI event handlers
  791. */
  792. static acpi_status
  793. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  794. {
  795. struct acpiphp_bridge *bridge;
  796. char objname[64];
  797. struct acpi_buffer buffer = { .length = sizeof(objname),
  798. .pointer = objname };
  799. bridge = acpiphp_handle_to_bridge(handle);
  800. if (bridge) {
  801. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  802. dbg("%s: re-enumerating slots under %s\n",
  803. __func__, objname);
  804. acpiphp_check_bridge(bridge);
  805. put_bridge(bridge);
  806. }
  807. return AE_OK ;
  808. }
  809. void acpiphp_check_host_bridge(acpi_handle handle)
  810. {
  811. struct acpiphp_bridge *bridge;
  812. bridge = acpiphp_handle_to_bridge(handle);
  813. if (bridge) {
  814. acpiphp_check_bridge(bridge);
  815. put_bridge(bridge);
  816. }
  817. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  818. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  819. }
  820. static void _handle_hotplug_event_bridge(struct work_struct *work)
  821. {
  822. struct acpiphp_bridge *bridge;
  823. char objname[64];
  824. struct acpi_buffer buffer = { .length = sizeof(objname),
  825. .pointer = objname };
  826. struct acpi_hp_work *hp_work;
  827. acpi_handle handle;
  828. u32 type;
  829. hp_work = container_of(work, struct acpi_hp_work, work);
  830. handle = hp_work->handle;
  831. type = hp_work->type;
  832. bridge = (struct acpiphp_bridge *)hp_work->context;
  833. acpi_scan_lock_acquire();
  834. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  835. switch (type) {
  836. case ACPI_NOTIFY_BUS_CHECK:
  837. /* bus re-enumerate */
  838. dbg("%s: Bus check notify on %s\n", __func__, objname);
  839. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  840. acpiphp_check_bridge(bridge);
  841. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  842. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  843. break;
  844. case ACPI_NOTIFY_DEVICE_CHECK:
  845. /* device check */
  846. dbg("%s: Device check notify on %s\n", __func__, objname);
  847. acpiphp_check_bridge(bridge);
  848. break;
  849. case ACPI_NOTIFY_DEVICE_WAKE:
  850. /* wake event */
  851. dbg("%s: Device wake notify on %s\n", __func__, objname);
  852. break;
  853. case ACPI_NOTIFY_EJECT_REQUEST:
  854. /* request device eject */
  855. dbg("%s: Device eject notify on %s\n", __func__, objname);
  856. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  857. struct acpiphp_slot *slot;
  858. slot = bridge->func->slot;
  859. if (!acpiphp_disable_slot(slot))
  860. acpiphp_eject_slot(slot);
  861. }
  862. break;
  863. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  864. printk(KERN_ERR "Device %s cannot be configured due"
  865. " to a frequency mismatch\n", objname);
  866. break;
  867. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  868. printk(KERN_ERR "Device %s cannot be configured due"
  869. " to a bus mode mismatch\n", objname);
  870. break;
  871. case ACPI_NOTIFY_POWER_FAULT:
  872. printk(KERN_ERR "Device %s has suffered a power fault\n",
  873. objname);
  874. break;
  875. default:
  876. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  877. break;
  878. }
  879. acpi_scan_lock_release();
  880. kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
  881. put_bridge(bridge);
  882. }
  883. /**
  884. * handle_hotplug_event_bridge - handle ACPI event on bridges
  885. * @handle: Notify()'ed acpi_handle
  886. * @type: Notify code
  887. * @context: pointer to acpiphp_bridge structure
  888. *
  889. * Handles ACPI event notification on {host,p2p} bridges.
  890. */
  891. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
  892. void *context)
  893. {
  894. struct acpiphp_bridge *bridge = context;
  895. /*
  896. * Currently the code adds all hotplug events to the kacpid_wq
  897. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  898. * The proper way to fix this is to reorganize the code so that
  899. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  900. * For now just re-add this work to the kacpi_hotplug_wq so we
  901. * don't deadlock on hotplug actions.
  902. */
  903. get_bridge(bridge);
  904. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
  905. }
  906. static void _handle_hotplug_event_func(struct work_struct *work)
  907. {
  908. struct acpiphp_func *func;
  909. char objname[64];
  910. struct acpi_buffer buffer = { .length = sizeof(objname),
  911. .pointer = objname };
  912. struct acpi_hp_work *hp_work;
  913. acpi_handle handle;
  914. u32 type;
  915. hp_work = container_of(work, struct acpi_hp_work, work);
  916. handle = hp_work->handle;
  917. type = hp_work->type;
  918. func = (struct acpiphp_func *)hp_work->context;
  919. acpi_scan_lock_acquire();
  920. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  921. switch (type) {
  922. case ACPI_NOTIFY_BUS_CHECK:
  923. /* bus re-enumerate */
  924. dbg("%s: Bus check notify on %s\n", __func__, objname);
  925. acpiphp_enable_slot(func->slot);
  926. break;
  927. case ACPI_NOTIFY_DEVICE_CHECK:
  928. /* device check : re-enumerate from parent bus */
  929. dbg("%s: Device check notify on %s\n", __func__, objname);
  930. acpiphp_check_bridge(func->slot->bridge);
  931. break;
  932. case ACPI_NOTIFY_DEVICE_WAKE:
  933. /* wake event */
  934. dbg("%s: Device wake notify on %s\n", __func__, objname);
  935. break;
  936. case ACPI_NOTIFY_EJECT_REQUEST:
  937. /* request device eject */
  938. dbg("%s: Device eject notify on %s\n", __func__, objname);
  939. if (!(acpiphp_disable_slot(func->slot)))
  940. acpiphp_eject_slot(func->slot);
  941. break;
  942. default:
  943. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  944. break;
  945. }
  946. acpi_scan_lock_release();
  947. kfree(hp_work); /* allocated in handle_hotplug_event_func */
  948. put_bridge(func->slot->bridge);
  949. }
  950. /**
  951. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  952. * @handle: Notify()'ed acpi_handle
  953. * @type: Notify code
  954. * @context: pointer to acpiphp_func structure
  955. *
  956. * Handles ACPI event notification on slots.
  957. */
  958. static void handle_hotplug_event_func(acpi_handle handle, u32 type,
  959. void *context)
  960. {
  961. struct acpiphp_func *func = context;
  962. /*
  963. * Currently the code adds all hotplug events to the kacpid_wq
  964. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  965. * The proper way to fix this is to reorganize the code so that
  966. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  967. * For now just re-add this work to the kacpi_hotplug_wq so we
  968. * don't deadlock on hotplug actions.
  969. */
  970. get_bridge(func->slot->bridge);
  971. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
  972. }
  973. /*
  974. * Create hotplug slots for the PCI bus.
  975. * It should always return 0 to avoid skipping following notifiers.
  976. */
  977. void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
  978. {
  979. acpi_handle dummy_handle;
  980. struct acpiphp_bridge *bridge;
  981. if (acpiphp_disabled)
  982. return;
  983. if (detect_ejectable_slots(handle) <= 0)
  984. return;
  985. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  986. if (bridge == NULL) {
  987. err("out of memory\n");
  988. return;
  989. }
  990. INIT_LIST_HEAD(&bridge->slots);
  991. kref_init(&bridge->ref);
  992. bridge->handle = handle;
  993. bridge->pci_dev = pci_dev_get(bus->self);
  994. bridge->pci_bus = bus;
  995. /*
  996. * Grab a ref to the subordinate PCI bus in case the bus is
  997. * removed via PCI core logical hotplug. The ref pins the bus
  998. * (which we access during module unload).
  999. */
  1000. get_device(&bus->dev);
  1001. if (!pci_is_root_bus(bridge->pci_bus) &&
  1002. ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  1003. "_EJ0", &dummy_handle))) {
  1004. dbg("found ejectable p2p bridge\n");
  1005. bridge->flags |= BRIDGE_HAS_EJ0;
  1006. bridge->func = acpiphp_bridge_handle_to_function(handle);
  1007. }
  1008. init_bridge_misc(bridge);
  1009. }
  1010. /* Destroy hotplug slots associated with the PCI bus */
  1011. void acpiphp_remove_slots(struct pci_bus *bus)
  1012. {
  1013. struct acpiphp_bridge *bridge, *tmp;
  1014. if (acpiphp_disabled)
  1015. return;
  1016. list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
  1017. if (bridge->pci_bus == bus) {
  1018. cleanup_bridge(bridge);
  1019. put_bridge(bridge);
  1020. break;
  1021. }
  1022. }
  1023. /**
  1024. * acpiphp_enable_slot - power on slot
  1025. * @slot: ACPI PHP slot
  1026. */
  1027. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1028. {
  1029. int retval;
  1030. mutex_lock(&slot->crit_sect);
  1031. /* wake up all functions */
  1032. retval = power_on_slot(slot);
  1033. if (retval)
  1034. goto err_exit;
  1035. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1036. /* configure all functions */
  1037. retval = enable_device(slot);
  1038. if (retval)
  1039. power_off_slot(slot);
  1040. } else {
  1041. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1042. power_off_slot(slot);
  1043. }
  1044. err_exit:
  1045. mutex_unlock(&slot->crit_sect);
  1046. return retval;
  1047. }
  1048. /**
  1049. * acpiphp_disable_slot - power off slot
  1050. * @slot: ACPI PHP slot
  1051. */
  1052. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1053. {
  1054. int retval = 0;
  1055. mutex_lock(&slot->crit_sect);
  1056. /* unconfigure all functions */
  1057. retval = disable_device(slot);
  1058. if (retval)
  1059. goto err_exit;
  1060. /* power off all functions */
  1061. retval = power_off_slot(slot);
  1062. if (retval)
  1063. goto err_exit;
  1064. err_exit:
  1065. mutex_unlock(&slot->crit_sect);
  1066. return retval;
  1067. }
  1068. /*
  1069. * slot enabled: 1
  1070. * slot disabled: 0
  1071. */
  1072. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1073. {
  1074. return (slot->flags & SLOT_POWEREDON);
  1075. }
  1076. /*
  1077. * latch open: 1
  1078. * latch closed: 0
  1079. */
  1080. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1081. {
  1082. unsigned int sta;
  1083. sta = get_slot_status(slot);
  1084. return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
  1085. }
  1086. /*
  1087. * adapter presence : 1
  1088. * absence : 0
  1089. */
  1090. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1091. {
  1092. unsigned int sta;
  1093. sta = get_slot_status(slot);
  1094. return (sta == 0) ? 0 : 1;
  1095. }