acpiphp_glue.c 29 KB

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