acpiphp_glue.c 30 KB

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