acpiphp_glue.c 32 KB

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