acpiphp_glue.c 34 KB

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