acpiphp_glue.c 29 KB

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