acpiphp_glue.c 30 KB

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