acpiphp_glue.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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(bridge->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, found = 0;
  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. found = 1;
  301. break;
  302. }
  303. if (!found) {
  304. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  305. if (!slot) {
  306. status = AE_NO_MEMORY;
  307. goto err_out;
  308. }
  309. slot->bridge = bridge;
  310. slot->device = device;
  311. slot->sun = sun;
  312. INIT_LIST_HEAD(&slot->funcs);
  313. mutex_init(&slot->crit_sect);
  314. mutex_lock(&bridge_mutex);
  315. list_add_tail(&slot->node, &bridge->slots);
  316. mutex_unlock(&bridge_mutex);
  317. bridge->nr_slots++;
  318. dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  319. slot->sun, pci_domain_nr(pbus), pbus->number, device);
  320. retval = acpiphp_register_hotplug_slot(slot);
  321. if (retval) {
  322. if (retval == -EBUSY)
  323. warn("Slot %llu already registered by another "
  324. "hotplug driver\n", slot->sun);
  325. else
  326. warn("acpiphp_register_hotplug_slot failed "
  327. "(err code = 0x%x)\n", retval);
  328. status = AE_OK;
  329. goto err;
  330. }
  331. }
  332. newfunc->slot = slot;
  333. mutex_lock(&bridge_mutex);
  334. list_add_tail(&newfunc->sibling, &slot->funcs);
  335. mutex_unlock(&bridge_mutex);
  336. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  337. &val, 60*1000))
  338. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  339. if (is_dock_device(handle)) {
  340. /* we don't want to call this device's _EJ0
  341. * because we want the dock notify handler
  342. * to call it after it calls _DCK
  343. */
  344. newfunc->flags &= ~FUNC_HAS_EJ0;
  345. if (register_hotplug_dock_device(handle,
  346. &acpiphp_dock_ops, context,
  347. acpiphp_dock_init, acpiphp_dock_release))
  348. dbg("failed to register dock device\n");
  349. }
  350. /* install notify handler */
  351. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  352. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  353. handle_hotplug_event,
  354. context);
  355. if (ACPI_SUCCESS(status))
  356. context->handler_for_func = true;
  357. else
  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_context *context;
  388. struct acpiphp_bridge *bridge = NULL;
  389. mutex_lock(&acpiphp_context_lock);
  390. context = acpiphp_get_context(handle);
  391. if (context) {
  392. bridge = context->bridge;
  393. if (bridge)
  394. get_bridge(bridge);
  395. acpiphp_put_context(context);
  396. }
  397. mutex_unlock(&acpiphp_context_lock);
  398. return bridge;
  399. }
  400. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  401. {
  402. struct acpiphp_slot *slot;
  403. struct acpiphp_func *func;
  404. acpi_status status;
  405. acpi_handle handle = bridge->handle;
  406. if (!bridge->context->handler_for_func) {
  407. status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  408. handle_hotplug_event);
  409. if (ACPI_FAILURE(status))
  410. err("failed to remove notify handler\n");
  411. }
  412. list_for_each_entry(slot, &bridge->slots, node) {
  413. list_for_each_entry(func, &slot->funcs, sibling) {
  414. if (is_dock_device(func->handle)) {
  415. unregister_hotplug_dock_device(func->handle);
  416. }
  417. if (!(func->flags & FUNC_HAS_DCK)) {
  418. func->context->handler_for_func = false;
  419. status = acpi_remove_notify_handler(func->handle,
  420. ACPI_SYSTEM_NOTIFY,
  421. handle_hotplug_event);
  422. if (ACPI_FAILURE(status))
  423. err("failed to remove notify handler\n");
  424. }
  425. }
  426. acpiphp_unregister_hotplug_slot(slot);
  427. }
  428. mutex_lock(&bridge_mutex);
  429. list_del(&bridge->list);
  430. mutex_unlock(&bridge_mutex);
  431. }
  432. static int power_on_slot(struct acpiphp_slot *slot)
  433. {
  434. acpi_status status;
  435. struct acpiphp_func *func;
  436. int retval = 0;
  437. /* if already enabled, just skip */
  438. if (slot->flags & SLOT_POWEREDON)
  439. goto err_exit;
  440. list_for_each_entry(func, &slot->funcs, sibling) {
  441. if (func->flags & FUNC_HAS_PS0) {
  442. dbg("%s: executing _PS0\n", __func__);
  443. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  444. if (ACPI_FAILURE(status)) {
  445. warn("%s: _PS0 failed\n", __func__);
  446. retval = -1;
  447. goto err_exit;
  448. } else
  449. break;
  450. }
  451. }
  452. /* TBD: evaluate _STA to check if the slot is enabled */
  453. slot->flags |= SLOT_POWEREDON;
  454. err_exit:
  455. return retval;
  456. }
  457. static int power_off_slot(struct acpiphp_slot *slot)
  458. {
  459. acpi_status status;
  460. struct acpiphp_func *func;
  461. int retval = 0;
  462. /* if already disabled, just skip */
  463. if ((slot->flags & SLOT_POWEREDON) == 0)
  464. goto err_exit;
  465. list_for_each_entry(func, &slot->funcs, sibling) {
  466. if (func->flags & FUNC_HAS_PS3) {
  467. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  468. if (ACPI_FAILURE(status)) {
  469. warn("%s: _PS3 failed\n", __func__);
  470. retval = -1;
  471. goto err_exit;
  472. } else
  473. break;
  474. }
  475. }
  476. /* TBD: evaluate _STA to check if the slot is disabled */
  477. slot->flags &= (~SLOT_POWEREDON);
  478. err_exit:
  479. return retval;
  480. }
  481. /**
  482. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  483. * @bus: bus to start search with
  484. */
  485. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  486. {
  487. struct list_head *tmp;
  488. unsigned char max, n;
  489. /*
  490. * pci_bus_max_busnr will return the highest
  491. * reserved busnr for all these children.
  492. * that is equivalent to the bus->subordinate
  493. * value. We don't want to use the parent's
  494. * bus->subordinate value because it could have
  495. * padding in it.
  496. */
  497. max = bus->busn_res.start;
  498. list_for_each(tmp, &bus->children) {
  499. n = pci_bus_max_busnr(pci_bus_b(tmp));
  500. if (n > max)
  501. max = n;
  502. }
  503. return max;
  504. }
  505. /**
  506. * acpiphp_bus_add - add a new bus to acpi subsystem
  507. * @func: acpiphp_func of the bridge
  508. */
  509. static int acpiphp_bus_add(struct acpiphp_func *func)
  510. {
  511. struct acpi_device *device;
  512. int ret_val;
  513. if (!acpi_bus_get_device(func->handle, &device)) {
  514. dbg("bus exists... trim\n");
  515. /* this shouldn't be in here, so remove
  516. * the bus then re-add it...
  517. */
  518. acpi_bus_trim(device);
  519. }
  520. ret_val = acpi_bus_scan(func->handle);
  521. if (!ret_val)
  522. ret_val = acpi_bus_get_device(func->handle, &device);
  523. if (ret_val)
  524. dbg("error adding bus, %x\n", -ret_val);
  525. return ret_val;
  526. }
  527. /**
  528. * acpiphp_bus_trim - trim a bus from acpi subsystem
  529. * @handle: handle to acpi namespace
  530. */
  531. static int acpiphp_bus_trim(acpi_handle handle)
  532. {
  533. struct acpi_device *device;
  534. int retval;
  535. retval = acpi_bus_get_device(handle, &device);
  536. if (retval) {
  537. dbg("acpi_device not found\n");
  538. return retval;
  539. }
  540. acpi_bus_trim(device);
  541. return 0;
  542. }
  543. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  544. {
  545. struct acpiphp_func *func;
  546. union acpi_object params[2];
  547. struct acpi_object_list arg_list;
  548. list_for_each_entry(func, &slot->funcs, sibling) {
  549. arg_list.count = 2;
  550. arg_list.pointer = params;
  551. params[0].type = ACPI_TYPE_INTEGER;
  552. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  553. params[1].type = ACPI_TYPE_INTEGER;
  554. params[1].integer.value = 1;
  555. /* _REG is optional, we don't care about if there is failure */
  556. acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
  557. }
  558. }
  559. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  560. {
  561. struct acpiphp_func *func;
  562. if (!dev->subordinate)
  563. return;
  564. /* quirk, or pcie could set it already */
  565. if (dev->is_hotplug_bridge)
  566. return;
  567. if (PCI_SLOT(dev->devfn) != slot->device)
  568. return;
  569. list_for_each_entry(func, &slot->funcs, sibling) {
  570. if (PCI_FUNC(dev->devfn) == func->function) {
  571. /* check if this bridge has ejectable slots */
  572. if ((detect_ejectable_slots(func->handle) > 0))
  573. dev->is_hotplug_bridge = 1;
  574. break;
  575. }
  576. }
  577. }
  578. /**
  579. * enable_device - enable, configure a slot
  580. * @slot: slot to be enabled
  581. *
  582. * This function should be called per *physical slot*,
  583. * not per each slot object in ACPI namespace.
  584. */
  585. static int __ref enable_device(struct acpiphp_slot *slot)
  586. {
  587. struct pci_dev *dev;
  588. struct pci_bus *bus = slot->bridge->pci_bus;
  589. struct acpiphp_func *func;
  590. int num, max, pass;
  591. LIST_HEAD(add_list);
  592. if (slot->flags & SLOT_ENABLED)
  593. goto err_exit;
  594. list_for_each_entry(func, &slot->funcs, sibling)
  595. acpiphp_bus_add(func);
  596. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  597. if (num == 0) {
  598. /* Maybe only part of funcs are added. */
  599. dbg("No new device found\n");
  600. goto err_exit;
  601. }
  602. max = acpiphp_max_busnr(bus);
  603. for (pass = 0; pass < 2; pass++) {
  604. list_for_each_entry(dev, &bus->devices, bus_list) {
  605. if (PCI_SLOT(dev->devfn) != slot->device)
  606. continue;
  607. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  608. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  609. max = pci_scan_bridge(bus, dev, max, pass);
  610. if (pass && dev->subordinate) {
  611. check_hotplug_bridge(slot, dev);
  612. pcibios_resource_survey_bus(dev->subordinate);
  613. __pci_bus_size_bridges(dev->subordinate,
  614. &add_list);
  615. }
  616. }
  617. }
  618. }
  619. __pci_bus_assign_resources(bus, &add_list, NULL);
  620. acpiphp_sanitize_bus(bus);
  621. acpiphp_set_hpp_values(bus);
  622. acpiphp_set_acpi_region(slot);
  623. pci_enable_bridges(bus);
  624. list_for_each_entry(dev, &bus->devices, bus_list) {
  625. /* Assume that newly added devices are powered on already. */
  626. if (!dev->is_added)
  627. dev->current_state = PCI_D0;
  628. }
  629. pci_bus_add_devices(bus);
  630. slot->flags |= SLOT_ENABLED;
  631. list_for_each_entry(func, &slot->funcs, sibling) {
  632. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  633. func->function));
  634. if (!dev) {
  635. /* Do not set SLOT_ENABLED flag if some funcs
  636. are not added. */
  637. slot->flags &= (~SLOT_ENABLED);
  638. continue;
  639. }
  640. }
  641. err_exit:
  642. return 0;
  643. }
  644. /* return first device in slot, acquiring a reference on it */
  645. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  646. {
  647. struct pci_bus *bus = slot->bridge->pci_bus;
  648. struct pci_dev *dev;
  649. struct pci_dev *ret = NULL;
  650. down_read(&pci_bus_sem);
  651. list_for_each_entry(dev, &bus->devices, bus_list)
  652. if (PCI_SLOT(dev->devfn) == slot->device) {
  653. ret = pci_dev_get(dev);
  654. break;
  655. }
  656. up_read(&pci_bus_sem);
  657. return ret;
  658. }
  659. /**
  660. * disable_device - disable a slot
  661. * @slot: ACPI PHP slot
  662. */
  663. static int disable_device(struct acpiphp_slot *slot)
  664. {
  665. struct acpiphp_func *func;
  666. struct pci_dev *pdev;
  667. /*
  668. * enable_device() enumerates all functions in this device via
  669. * pci_scan_slot(), whether they have associated ACPI hotplug
  670. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  671. * here.
  672. */
  673. while ((pdev = dev_in_slot(slot))) {
  674. pci_stop_and_remove_bus_device(pdev);
  675. pci_dev_put(pdev);
  676. }
  677. list_for_each_entry(func, &slot->funcs, sibling) {
  678. acpiphp_bus_trim(func->handle);
  679. }
  680. slot->flags &= (~SLOT_ENABLED);
  681. return 0;
  682. }
  683. /**
  684. * get_slot_status - get ACPI slot status
  685. * @slot: ACPI PHP slot
  686. *
  687. * If a slot has _STA for each function and if any one of them
  688. * returned non-zero status, return it.
  689. *
  690. * If a slot doesn't have _STA and if any one of its functions'
  691. * configuration space is configured, return 0x0f as a _STA.
  692. *
  693. * Otherwise return 0.
  694. */
  695. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  696. {
  697. acpi_status status;
  698. unsigned long long sta = 0;
  699. u32 dvid;
  700. struct acpiphp_func *func;
  701. list_for_each_entry(func, &slot->funcs, sibling) {
  702. if (func->flags & FUNC_HAS_STA) {
  703. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  704. if (ACPI_SUCCESS(status) && sta)
  705. break;
  706. } else {
  707. pci_bus_read_config_dword(slot->bridge->pci_bus,
  708. PCI_DEVFN(slot->device,
  709. func->function),
  710. PCI_VENDOR_ID, &dvid);
  711. if (dvid != 0xffffffff) {
  712. sta = ACPI_STA_ALL;
  713. break;
  714. }
  715. }
  716. }
  717. return (unsigned int)sta;
  718. }
  719. /**
  720. * acpiphp_eject_slot - physically eject the slot
  721. * @slot: ACPI PHP slot
  722. */
  723. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  724. {
  725. struct acpiphp_func *func;
  726. list_for_each_entry(func, &slot->funcs, sibling) {
  727. /* We don't want to call _EJ0 on non-existing functions. */
  728. if ((func->flags & FUNC_HAS_EJ0)) {
  729. if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
  730. return -1;
  731. else
  732. break;
  733. }
  734. }
  735. return 0;
  736. }
  737. /**
  738. * acpiphp_check_bridge - re-enumerate devices
  739. * @bridge: where to begin re-enumeration
  740. *
  741. * Iterate over all slots under this bridge and make sure that if a
  742. * card is present they are enabled, and if not they are disabled.
  743. */
  744. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  745. {
  746. struct acpiphp_slot *slot;
  747. int retval = 0;
  748. int enabled, disabled;
  749. enabled = disabled = 0;
  750. list_for_each_entry(slot, &bridge->slots, node) {
  751. unsigned int status = get_slot_status(slot);
  752. if (slot->flags & SLOT_ENABLED) {
  753. if (status == ACPI_STA_ALL)
  754. continue;
  755. retval = acpiphp_disable_slot(slot);
  756. if (retval) {
  757. err("Error occurred in disabling\n");
  758. goto err_exit;
  759. } else {
  760. acpiphp_eject_slot(slot);
  761. }
  762. disabled++;
  763. } else {
  764. if (status != ACPI_STA_ALL)
  765. continue;
  766. retval = acpiphp_enable_slot(slot);
  767. if (retval) {
  768. err("Error occurred in enabling\n");
  769. goto err_exit;
  770. }
  771. enabled++;
  772. }
  773. }
  774. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  775. err_exit:
  776. return retval;
  777. }
  778. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  779. {
  780. struct pci_dev *dev;
  781. list_for_each_entry(dev, &bus->devices, bus_list)
  782. pci_configure_slot(dev);
  783. }
  784. /*
  785. * Remove devices for which we could not assign resources, call
  786. * arch specific code to fix-up the bus
  787. */
  788. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  789. {
  790. struct pci_dev *dev, *tmp;
  791. int i;
  792. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  793. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  794. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  795. struct resource *res = &dev->resource[i];
  796. if ((res->flags & type_mask) && !res->start &&
  797. res->end) {
  798. /* Could not assign a required resources
  799. * for this device, remove it */
  800. pci_stop_and_remove_bus_device(dev);
  801. break;
  802. }
  803. }
  804. }
  805. }
  806. /*
  807. * ACPI event handlers
  808. */
  809. static acpi_status
  810. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  811. {
  812. struct acpiphp_bridge *bridge;
  813. char objname[64];
  814. struct acpi_buffer buffer = { .length = sizeof(objname),
  815. .pointer = objname };
  816. bridge = acpiphp_handle_to_bridge(handle);
  817. if (bridge) {
  818. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  819. dbg("%s: re-enumerating slots under %s\n",
  820. __func__, objname);
  821. acpiphp_check_bridge(bridge);
  822. put_bridge(bridge);
  823. }
  824. return AE_OK ;
  825. }
  826. void acpiphp_check_host_bridge(acpi_handle handle)
  827. {
  828. struct acpiphp_bridge *bridge;
  829. bridge = acpiphp_handle_to_bridge(handle);
  830. if (bridge) {
  831. acpiphp_check_bridge(bridge);
  832. put_bridge(bridge);
  833. }
  834. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  835. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  836. }
  837. static void hotplug_event(acpi_handle handle, u32 type, void *data)
  838. {
  839. struct acpiphp_context *context = data;
  840. struct acpiphp_bridge *bridge;
  841. struct acpiphp_func *func;
  842. char objname[64];
  843. struct acpi_buffer buffer = { .length = sizeof(objname),
  844. .pointer = objname };
  845. mutex_lock(&acpiphp_context_lock);
  846. bridge = context->bridge;
  847. if (bridge)
  848. get_bridge(bridge);
  849. /*
  850. * If context->func is not NULL, we are holding a reference to its
  851. * parent bridge, so it won't go away until we drop that reference.
  852. */
  853. func = context->func;
  854. mutex_unlock(&acpiphp_context_lock);
  855. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  856. switch (type) {
  857. case ACPI_NOTIFY_BUS_CHECK:
  858. /* bus re-enumerate */
  859. dbg("%s: Bus check notify on %s\n", __func__, objname);
  860. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  861. if (bridge) {
  862. acpiphp_check_bridge(bridge);
  863. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  864. ACPI_UINT32_MAX, check_sub_bridges,
  865. NULL, NULL, NULL);
  866. } else {
  867. acpiphp_enable_slot(func->slot);
  868. }
  869. break;
  870. case ACPI_NOTIFY_DEVICE_CHECK:
  871. /* device check */
  872. dbg("%s: Device check notify on %s\n", __func__, objname);
  873. if (bridge)
  874. acpiphp_check_bridge(bridge);
  875. else
  876. acpiphp_check_bridge(func->slot->bridge);
  877. break;
  878. case ACPI_NOTIFY_DEVICE_WAKE:
  879. /* wake event */
  880. dbg("%s: Device wake notify on %s\n", __func__, objname);
  881. break;
  882. case ACPI_NOTIFY_EJECT_REQUEST:
  883. /* request device eject */
  884. dbg("%s: Device eject notify on %s\n", __func__, objname);
  885. if (!func)
  886. break;
  887. if (bridge && !(bridge->flags & BRIDGE_HAS_EJ0))
  888. break;
  889. if (!(acpiphp_disable_slot(func->slot)))
  890. acpiphp_eject_slot(func->slot);
  891. break;
  892. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  893. printk(KERN_ERR "Device %s cannot be configured due"
  894. " to a frequency mismatch\n", objname);
  895. break;
  896. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  897. printk(KERN_ERR "Device %s cannot be configured due"
  898. " to a bus mode mismatch\n", objname);
  899. break;
  900. case ACPI_NOTIFY_POWER_FAULT:
  901. printk(KERN_ERR "Device %s has suffered a power fault\n",
  902. objname);
  903. break;
  904. default:
  905. warn("notify_handler: unknown event type 0x%x for %s\n", type,
  906. objname);
  907. break;
  908. }
  909. if (bridge)
  910. put_bridge(bridge);
  911. }
  912. static void hotplug_event_work(struct work_struct *work)
  913. {
  914. struct acpiphp_context *context;
  915. struct acpi_hp_work *hp_work;
  916. hp_work = container_of(work, struct acpi_hp_work, work);
  917. context = hp_work->context;
  918. acpi_scan_lock_acquire();
  919. hotplug_event(hp_work->handle, hp_work->type, context);
  920. acpi_scan_lock_release();
  921. kfree(hp_work); /* allocated in handle_hotplug_event() */
  922. mutex_lock(&acpiphp_context_lock);
  923. if (context->func)
  924. put_bridge(context->func->slot->bridge);
  925. else
  926. acpiphp_put_context(context);
  927. mutex_unlock(&acpiphp_context_lock);
  928. }
  929. /**
  930. * handle_hotplug_event - handle ACPI hotplug event
  931. * @handle: Notify()'ed acpi_handle
  932. * @type: Notify code
  933. * @data: pointer to acpiphp_context structure
  934. *
  935. * Handles ACPI event notification on slots.
  936. */
  937. static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
  938. {
  939. struct acpiphp_context *context;
  940. mutex_lock(&acpiphp_context_lock);
  941. context = acpiphp_get_context(handle);
  942. if (context) {
  943. if (context->func) {
  944. get_bridge(context->func->slot->bridge);
  945. acpiphp_put_context(context);
  946. } else if (!context->bridge) {
  947. acpiphp_put_context(context);
  948. context = NULL;
  949. }
  950. }
  951. mutex_unlock(&acpiphp_context_lock);
  952. /*
  953. * Currently the code adds all hotplug events to the kacpid_wq
  954. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  955. * The proper way to fix this is to reorganize the code so that
  956. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  957. * For now just re-add this work to the kacpi_hotplug_wq so we
  958. * don't deadlock on hotplug actions.
  959. */
  960. if (context)
  961. alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
  962. }
  963. /*
  964. * Create hotplug slots for the PCI bus.
  965. * It should always return 0 to avoid skipping following notifiers.
  966. */
  967. void acpiphp_enumerate_slots(struct pci_bus *bus)
  968. {
  969. struct acpiphp_context *context;
  970. struct acpiphp_bridge *bridge;
  971. acpi_handle handle;
  972. acpi_status status;
  973. if (acpiphp_disabled)
  974. return;
  975. handle = ACPI_HANDLE(bus->bridge);
  976. if (!handle || detect_ejectable_slots(handle) <= 0)
  977. return;
  978. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  979. if (!bridge) {
  980. acpi_handle_err(handle, "No memory for bridge object\n");
  981. return;
  982. }
  983. INIT_LIST_HEAD(&bridge->slots);
  984. kref_init(&bridge->ref);
  985. bridge->handle = handle;
  986. bridge->pci_dev = pci_dev_get(bus->self);
  987. bridge->pci_bus = bus;
  988. mutex_lock(&acpiphp_context_lock);
  989. context = acpiphp_get_context(handle);
  990. if (!context) {
  991. context = acpiphp_init_context(handle);
  992. if (!context) {
  993. mutex_unlock(&acpiphp_context_lock);
  994. acpi_handle_err(handle, "No hotplug context\n");
  995. kfree(bridge);
  996. return;
  997. }
  998. }
  999. bridge->context = context;
  1000. context->bridge = bridge;
  1001. mutex_unlock(&acpiphp_context_lock);
  1002. /*
  1003. * Grab a ref to the subordinate PCI bus in case the bus is
  1004. * removed via PCI core logical hotplug. The ref pins the bus
  1005. * (which we access during module unload).
  1006. */
  1007. get_device(&bus->dev);
  1008. /* must be added to the list prior to calling register_slot */
  1009. mutex_lock(&bridge_mutex);
  1010. list_add(&bridge->list, &bridge_list);
  1011. mutex_unlock(&bridge_mutex);
  1012. /* register all slot objects under this bridge */
  1013. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1,
  1014. register_slot, NULL, bridge, NULL);
  1015. if (ACPI_FAILURE(status)) {
  1016. acpi_handle_err(bridge->handle, "failed to register slots\n");
  1017. goto err;
  1018. }
  1019. if (pci_is_root_bus(bridge->pci_bus))
  1020. return;
  1021. if (acpi_has_method(bridge->handle, "_EJ0")) {
  1022. dbg("found ejectable p2p bridge\n");
  1023. bridge->flags |= BRIDGE_HAS_EJ0;
  1024. }
  1025. if (context->handler_for_func) {
  1026. /* Notify handler already installed. */
  1027. bridge->func = context->func;
  1028. get_bridge(context->func->slot->bridge);
  1029. return;
  1030. }
  1031. /* install notify handler for P2P bridges */
  1032. status = acpi_install_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
  1033. handle_hotplug_event, NULL);
  1034. if (ACPI_SUCCESS(status))
  1035. return;
  1036. acpi_handle_err(bridge->handle, "failed to register notify handler\n");
  1037. err:
  1038. cleanup_bridge(bridge);
  1039. put_bridge(bridge);
  1040. }
  1041. /* Destroy hotplug slots associated with the PCI bus */
  1042. void acpiphp_remove_slots(struct pci_bus *bus)
  1043. {
  1044. struct acpiphp_bridge *bridge, *tmp;
  1045. if (acpiphp_disabled)
  1046. return;
  1047. list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
  1048. if (bridge->pci_bus == bus) {
  1049. cleanup_bridge(bridge);
  1050. put_bridge(bridge);
  1051. break;
  1052. }
  1053. }
  1054. /**
  1055. * acpiphp_enable_slot - power on slot
  1056. * @slot: ACPI PHP slot
  1057. */
  1058. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1059. {
  1060. int retval;
  1061. mutex_lock(&slot->crit_sect);
  1062. /* wake up all functions */
  1063. retval = power_on_slot(slot);
  1064. if (retval)
  1065. goto err_exit;
  1066. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1067. /* configure all functions */
  1068. retval = enable_device(slot);
  1069. if (retval)
  1070. power_off_slot(slot);
  1071. } else {
  1072. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1073. power_off_slot(slot);
  1074. }
  1075. err_exit:
  1076. mutex_unlock(&slot->crit_sect);
  1077. return retval;
  1078. }
  1079. /**
  1080. * acpiphp_disable_slot - power off slot
  1081. * @slot: ACPI PHP slot
  1082. */
  1083. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1084. {
  1085. int retval = 0;
  1086. mutex_lock(&slot->crit_sect);
  1087. /* unconfigure all functions */
  1088. retval = disable_device(slot);
  1089. if (retval)
  1090. goto err_exit;
  1091. /* power off all functions */
  1092. retval = power_off_slot(slot);
  1093. if (retval)
  1094. goto err_exit;
  1095. err_exit:
  1096. mutex_unlock(&slot->crit_sect);
  1097. return retval;
  1098. }
  1099. /*
  1100. * slot enabled: 1
  1101. * slot disabled: 0
  1102. */
  1103. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1104. {
  1105. return (slot->flags & SLOT_POWEREDON);
  1106. }
  1107. /*
  1108. * latch open: 1
  1109. * latch closed: 0
  1110. */
  1111. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1112. {
  1113. unsigned int sta;
  1114. sta = get_slot_status(slot);
  1115. return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
  1116. }
  1117. /*
  1118. * adapter presence : 1
  1119. * absence : 0
  1120. */
  1121. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1122. {
  1123. unsigned int sta;
  1124. sta = get_slot_status(slot);
  1125. return (sta == 0) ? 0 : 1;
  1126. }