acpiphp_glue.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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, found = 0;
  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. list_for_each_entry(slot, &bridge->slots, node)
  179. if (slot->device == device) {
  180. if (slot->sun != sun)
  181. warn("sibling found, but _SUN doesn't match!\n");
  182. found = 1;
  183. break;
  184. }
  185. if (!found) {
  186. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  187. if (!slot) {
  188. kfree(newfunc);
  189. return AE_NO_MEMORY;
  190. }
  191. slot->bridge = bridge;
  192. slot->device = device;
  193. slot->sun = sun;
  194. INIT_LIST_HEAD(&slot->funcs);
  195. mutex_init(&slot->crit_sect);
  196. list_add_tail(&slot->node, &bridge->slots);
  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. list_del(&slot->node);
  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. list_for_each_entry(slot, &bridge->slots, node) {
  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. list_for_each_entry_safe(slot, next, &bridge->slots, node) {
  338. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
  339. if (is_dock_device(func->handle)) {
  340. unregister_hotplug_dock_device(func->handle);
  341. unregister_dock_notifier(&func->nb);
  342. }
  343. if (!(func->flags & FUNC_HAS_DCK)) {
  344. status = acpi_remove_notify_handler(func->handle,
  345. ACPI_SYSTEM_NOTIFY,
  346. handle_hotplug_event_func);
  347. if (ACPI_FAILURE(status))
  348. err("failed to remove notify handler\n");
  349. }
  350. list_del(&func->sibling);
  351. kfree(func);
  352. }
  353. acpiphp_unregister_hotplug_slot(slot);
  354. list_del(&slot->funcs);
  355. kfree(slot);
  356. }
  357. put_device(&bridge->pci_bus->dev);
  358. pci_dev_put(bridge->pci_dev);
  359. list_del(&bridge->list);
  360. kfree(bridge);
  361. }
  362. static int power_on_slot(struct acpiphp_slot *slot)
  363. {
  364. acpi_status status;
  365. struct acpiphp_func *func;
  366. int retval = 0;
  367. /* if already enabled, just skip */
  368. if (slot->flags & SLOT_POWEREDON)
  369. goto err_exit;
  370. list_for_each_entry(func, &slot->funcs, sibling) {
  371. if (func->flags & FUNC_HAS_PS0) {
  372. dbg("%s: executing _PS0\n", __func__);
  373. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  374. if (ACPI_FAILURE(status)) {
  375. warn("%s: _PS0 failed\n", __func__);
  376. retval = -1;
  377. goto err_exit;
  378. } else
  379. break;
  380. }
  381. }
  382. /* TBD: evaluate _STA to check if the slot is enabled */
  383. slot->flags |= SLOT_POWEREDON;
  384. err_exit:
  385. return retval;
  386. }
  387. static int power_off_slot(struct acpiphp_slot *slot)
  388. {
  389. acpi_status status;
  390. struct acpiphp_func *func;
  391. int retval = 0;
  392. /* if already disabled, just skip */
  393. if ((slot->flags & SLOT_POWEREDON) == 0)
  394. goto err_exit;
  395. list_for_each_entry(func, &slot->funcs, sibling) {
  396. if (func->flags & FUNC_HAS_PS3) {
  397. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  398. if (ACPI_FAILURE(status)) {
  399. warn("%s: _PS3 failed\n", __func__);
  400. retval = -1;
  401. goto err_exit;
  402. } else
  403. break;
  404. }
  405. }
  406. /* TBD: evaluate _STA to check if the slot is disabled */
  407. slot->flags &= (~SLOT_POWEREDON);
  408. err_exit:
  409. return retval;
  410. }
  411. /**
  412. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  413. * @bus: bus to start search with
  414. */
  415. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  416. {
  417. struct list_head *tmp;
  418. unsigned char max, n;
  419. /*
  420. * pci_bus_max_busnr will return the highest
  421. * reserved busnr for all these children.
  422. * that is equivalent to the bus->subordinate
  423. * value. We don't want to use the parent's
  424. * bus->subordinate value because it could have
  425. * padding in it.
  426. */
  427. max = bus->busn_res.start;
  428. list_for_each(tmp, &bus->children) {
  429. n = pci_bus_max_busnr(pci_bus_b(tmp));
  430. if (n > max)
  431. max = n;
  432. }
  433. return max;
  434. }
  435. /**
  436. * acpiphp_bus_add - add a new bus to acpi subsystem
  437. * @func: acpiphp_func of the bridge
  438. */
  439. static int acpiphp_bus_add(struct acpiphp_func *func)
  440. {
  441. struct acpi_device *device;
  442. int ret_val;
  443. if (!acpi_bus_get_device(func->handle, &device)) {
  444. dbg("bus exists... trim\n");
  445. /* this shouldn't be in here, so remove
  446. * the bus then re-add it...
  447. */
  448. acpi_bus_trim(device);
  449. }
  450. ret_val = acpi_bus_scan(func->handle);
  451. if (!ret_val)
  452. ret_val = acpi_bus_get_device(func->handle, &device);
  453. if (ret_val)
  454. dbg("error adding bus, %x\n", -ret_val);
  455. return ret_val;
  456. }
  457. /**
  458. * acpiphp_bus_trim - trim a bus from acpi subsystem
  459. * @handle: handle to acpi namespace
  460. */
  461. static int acpiphp_bus_trim(acpi_handle handle)
  462. {
  463. struct acpi_device *device;
  464. int retval;
  465. retval = acpi_bus_get_device(handle, &device);
  466. if (retval) {
  467. dbg("acpi_device not found\n");
  468. return retval;
  469. }
  470. acpi_bus_trim(device);
  471. return 0;
  472. }
  473. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  474. {
  475. struct acpiphp_func *func;
  476. union acpi_object params[2];
  477. struct acpi_object_list arg_list;
  478. list_for_each_entry(func, &slot->funcs, sibling) {
  479. arg_list.count = 2;
  480. arg_list.pointer = params;
  481. params[0].type = ACPI_TYPE_INTEGER;
  482. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  483. params[1].type = ACPI_TYPE_INTEGER;
  484. params[1].integer.value = 1;
  485. /* _REG is optional, we don't care about if there is failure */
  486. acpi_evaluate_object(func->handle, "_REG", &arg_list, 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. /* check if this bridge has ejectable slots */
  502. if ((detect_ejectable_slots(func->handle) > 0))
  503. dev->is_hotplug_bridge = 1;
  504. break;
  505. }
  506. }
  507. }
  508. /**
  509. * enable_device - enable, configure a slot
  510. * @slot: slot to be enabled
  511. *
  512. * This function should be called per *physical slot*,
  513. * not per each slot object in ACPI namespace.
  514. */
  515. static int __ref enable_device(struct acpiphp_slot *slot)
  516. {
  517. struct pci_dev *dev;
  518. struct pci_bus *bus = slot->bridge->pci_bus;
  519. struct acpiphp_func *func;
  520. int retval = 0;
  521. int num, max, pass;
  522. if (slot->flags & SLOT_ENABLED)
  523. goto err_exit;
  524. list_for_each_entry(func, &slot->funcs, sibling)
  525. acpiphp_bus_add(func);
  526. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  527. if (num == 0) {
  528. /* Maybe only part of funcs are added. */
  529. dbg("No new device found\n");
  530. goto err_exit;
  531. }
  532. max = acpiphp_max_busnr(bus);
  533. for (pass = 0; pass < 2; pass++) {
  534. list_for_each_entry(dev, &bus->devices, bus_list) {
  535. if (PCI_SLOT(dev->devfn) != slot->device)
  536. continue;
  537. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  538. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  539. max = pci_scan_bridge(bus, dev, max, pass);
  540. if (pass && dev->subordinate) {
  541. check_hotplug_bridge(slot, dev);
  542. pci_bus_size_bridges(dev->subordinate);
  543. }
  544. }
  545. }
  546. }
  547. pci_bus_assign_resources(bus);
  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 retval;
  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->bridge->pci_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->handle);
  607. }
  608. slot->flags &= (~SLOT_ENABLED);
  609. return 0;
  610. }
  611. /**
  612. * get_slot_status - get ACPI slot status
  613. * @slot: ACPI PHP slot
  614. *
  615. * If a slot has _STA for each function and if any one of them
  616. * returned non-zero status, return it.
  617. *
  618. * If a slot doesn't have _STA and if any one of its functions'
  619. * configuration space is configured, return 0x0f as a _STA.
  620. *
  621. * Otherwise return 0.
  622. */
  623. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  624. {
  625. acpi_status status;
  626. unsigned long long sta = 0;
  627. u32 dvid;
  628. struct acpiphp_func *func;
  629. list_for_each_entry(func, &slot->funcs, sibling) {
  630. if (func->flags & FUNC_HAS_STA) {
  631. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  632. if (ACPI_SUCCESS(status) && sta)
  633. break;
  634. } else {
  635. pci_bus_read_config_dword(slot->bridge->pci_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. acpi_status status;
  654. struct acpiphp_func *func;
  655. struct acpi_object_list arg_list;
  656. union acpi_object arg;
  657. list_for_each_entry(func, &slot->funcs, sibling) {
  658. /* We don't want to call _EJ0 on non-existing functions. */
  659. if ((func->flags & FUNC_HAS_EJ0)) {
  660. /* _EJ0 method take one argument */
  661. arg_list.count = 1;
  662. arg_list.pointer = &arg;
  663. arg.type = ACPI_TYPE_INTEGER;
  664. arg.integer.value = 1;
  665. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  666. if (ACPI_FAILURE(status)) {
  667. warn("%s: _EJ0 failed\n", __func__);
  668. return -1;
  669. } else
  670. break;
  671. }
  672. }
  673. return 0;
  674. }
  675. /**
  676. * acpiphp_check_bridge - re-enumerate devices
  677. * @bridge: where to begin re-enumeration
  678. *
  679. * Iterate over all slots under this bridge and make sure that if a
  680. * card is present they are enabled, and if not they are disabled.
  681. */
  682. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  683. {
  684. struct acpiphp_slot *slot;
  685. int retval = 0;
  686. int enabled, disabled;
  687. enabled = disabled = 0;
  688. list_for_each_entry(slot, &bridge->slots, node) {
  689. unsigned int status = get_slot_status(slot);
  690. if (slot->flags & SLOT_ENABLED) {
  691. if (status == ACPI_STA_ALL)
  692. continue;
  693. retval = acpiphp_disable_slot(slot);
  694. if (retval) {
  695. err("Error occurred in disabling\n");
  696. goto err_exit;
  697. } else {
  698. acpiphp_eject_slot(slot);
  699. }
  700. disabled++;
  701. } else {
  702. if (status != ACPI_STA_ALL)
  703. continue;
  704. retval = acpiphp_enable_slot(slot);
  705. if (retval) {
  706. err("Error occurred in enabling\n");
  707. goto err_exit;
  708. }
  709. enabled++;
  710. }
  711. }
  712. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  713. err_exit:
  714. return retval;
  715. }
  716. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  717. {
  718. struct pci_dev *dev;
  719. list_for_each_entry(dev, &bus->devices, bus_list)
  720. pci_configure_slot(dev);
  721. }
  722. /*
  723. * Remove devices for which we could not assign resources, call
  724. * arch specific code to fix-up the bus
  725. */
  726. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  727. {
  728. struct pci_dev *dev, *tmp;
  729. int i;
  730. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  731. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  732. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  733. struct resource *res = &dev->resource[i];
  734. if ((res->flags & type_mask) && !res->start &&
  735. res->end) {
  736. /* Could not assign a required resources
  737. * for this device, remove it */
  738. pci_stop_and_remove_bus_device(dev);
  739. break;
  740. }
  741. }
  742. }
  743. }
  744. /*
  745. * ACPI event handlers
  746. */
  747. static acpi_status
  748. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  749. {
  750. struct acpiphp_bridge *bridge;
  751. char objname[64];
  752. struct acpi_buffer buffer = { .length = sizeof(objname),
  753. .pointer = objname };
  754. bridge = acpiphp_handle_to_bridge(handle);
  755. if (bridge) {
  756. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  757. dbg("%s: re-enumerating slots under %s\n",
  758. __func__, objname);
  759. acpiphp_check_bridge(bridge);
  760. }
  761. return AE_OK ;
  762. }
  763. static void _handle_hotplug_event_bridge(struct work_struct *work)
  764. {
  765. struct acpiphp_bridge *bridge;
  766. char objname[64];
  767. struct acpi_buffer buffer = { .length = sizeof(objname),
  768. .pointer = objname };
  769. struct acpi_hp_work *hp_work;
  770. acpi_handle handle;
  771. u32 type;
  772. hp_work = container_of(work, struct acpi_hp_work, work);
  773. handle = hp_work->handle;
  774. type = hp_work->type;
  775. bridge = (struct acpiphp_bridge *)hp_work->context;
  776. acpi_scan_lock_acquire();
  777. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  778. switch (type) {
  779. case ACPI_NOTIFY_BUS_CHECK:
  780. /* bus re-enumerate */
  781. dbg("%s: Bus check notify on %s\n", __func__, objname);
  782. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  783. acpiphp_check_bridge(bridge);
  784. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  785. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  786. break;
  787. case ACPI_NOTIFY_DEVICE_CHECK:
  788. /* device check */
  789. dbg("%s: Device check notify on %s\n", __func__, objname);
  790. acpiphp_check_bridge(bridge);
  791. break;
  792. case ACPI_NOTIFY_DEVICE_WAKE:
  793. /* wake event */
  794. dbg("%s: Device wake notify on %s\n", __func__, objname);
  795. break;
  796. case ACPI_NOTIFY_EJECT_REQUEST:
  797. /* request device eject */
  798. dbg("%s: Device eject notify on %s\n", __func__, objname);
  799. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  800. struct acpiphp_slot *slot;
  801. slot = bridge->func->slot;
  802. if (!acpiphp_disable_slot(slot))
  803. acpiphp_eject_slot(slot);
  804. }
  805. break;
  806. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  807. printk(KERN_ERR "Device %s cannot be configured due"
  808. " to a frequency mismatch\n", objname);
  809. break;
  810. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  811. printk(KERN_ERR "Device %s cannot be configured due"
  812. " to a bus mode mismatch\n", objname);
  813. break;
  814. case ACPI_NOTIFY_POWER_FAULT:
  815. printk(KERN_ERR "Device %s has suffered a power fault\n",
  816. objname);
  817. break;
  818. default:
  819. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  820. break;
  821. }
  822. acpi_scan_lock_release();
  823. kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
  824. }
  825. /**
  826. * handle_hotplug_event_bridge - handle ACPI event on bridges
  827. * @handle: Notify()'ed acpi_handle
  828. * @type: Notify code
  829. * @context: pointer to acpiphp_bridge structure
  830. *
  831. * Handles ACPI event notification on {host,p2p} bridges.
  832. */
  833. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
  834. void *context)
  835. {
  836. /*
  837. * Currently the code adds all hotplug events to the kacpid_wq
  838. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  839. * The proper way to fix this is to reorganize the code so that
  840. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  841. * For now just re-add this work to the kacpi_hotplug_wq so we
  842. * don't deadlock on hotplug actions.
  843. */
  844. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
  845. }
  846. static void _handle_hotplug_event_func(struct work_struct *work)
  847. {
  848. struct acpiphp_func *func;
  849. char objname[64];
  850. struct acpi_buffer buffer = { .length = sizeof(objname),
  851. .pointer = objname };
  852. struct acpi_hp_work *hp_work;
  853. acpi_handle handle;
  854. u32 type;
  855. hp_work = container_of(work, struct acpi_hp_work, work);
  856. handle = hp_work->handle;
  857. type = hp_work->type;
  858. func = (struct acpiphp_func *)hp_work->context;
  859. acpi_scan_lock_acquire();
  860. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  861. switch (type) {
  862. case ACPI_NOTIFY_BUS_CHECK:
  863. /* bus re-enumerate */
  864. dbg("%s: Bus check notify on %s\n", __func__, objname);
  865. acpiphp_enable_slot(func->slot);
  866. break;
  867. case ACPI_NOTIFY_DEVICE_CHECK:
  868. /* device check : re-enumerate from parent bus */
  869. dbg("%s: Device check notify on %s\n", __func__, objname);
  870. acpiphp_check_bridge(func->slot->bridge);
  871. break;
  872. case ACPI_NOTIFY_DEVICE_WAKE:
  873. /* wake event */
  874. dbg("%s: Device wake notify on %s\n", __func__, objname);
  875. break;
  876. case ACPI_NOTIFY_EJECT_REQUEST:
  877. /* request device eject */
  878. dbg("%s: Device eject notify on %s\n", __func__, objname);
  879. if (!(acpiphp_disable_slot(func->slot)))
  880. acpiphp_eject_slot(func->slot);
  881. break;
  882. default:
  883. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  884. break;
  885. }
  886. acpi_scan_lock_release();
  887. kfree(hp_work); /* allocated in handle_hotplug_event_func */
  888. }
  889. /**
  890. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  891. * @handle: Notify()'ed acpi_handle
  892. * @type: Notify code
  893. * @context: pointer to acpiphp_func structure
  894. *
  895. * Handles ACPI event notification on slots.
  896. */
  897. static void handle_hotplug_event_func(acpi_handle handle, u32 type,
  898. void *context)
  899. {
  900. /*
  901. * Currently the code adds all hotplug events to the kacpid_wq
  902. * queue when it should add hotplug events to the kacpi_hotplug_wq.
  903. * The proper way to fix this is to reorganize the code so that
  904. * drivers (dock, etc.) do not call acpi_os_execute(), etc.
  905. * For now just re-add this work to the kacpi_hotplug_wq so we
  906. * don't deadlock on hotplug actions.
  907. */
  908. alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
  909. }
  910. /*
  911. * Create hotplug slots for the PCI bus.
  912. * It should always return 0 to avoid skipping following notifiers.
  913. */
  914. void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
  915. {
  916. acpi_handle dummy_handle;
  917. struct acpiphp_bridge *bridge;
  918. if (acpiphp_disabled)
  919. return;
  920. if (detect_ejectable_slots(handle) <= 0)
  921. return;
  922. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  923. if (bridge == NULL) {
  924. err("out of memory\n");
  925. return;
  926. }
  927. INIT_LIST_HEAD(&bridge->slots);
  928. bridge->handle = handle;
  929. bridge->pci_dev = pci_dev_get(bus->self);
  930. bridge->pci_bus = bus;
  931. /*
  932. * Grab a ref to the subordinate PCI bus in case the bus is
  933. * removed via PCI core logical hotplug. The ref pins the bus
  934. * (which we access during module unload).
  935. */
  936. get_device(&bus->dev);
  937. if (!pci_is_root_bus(bridge->pci_bus) &&
  938. ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  939. "_EJ0", &dummy_handle))) {
  940. dbg("found ejectable p2p bridge\n");
  941. bridge->flags |= BRIDGE_HAS_EJ0;
  942. bridge->func = acpiphp_bridge_handle_to_function(handle);
  943. }
  944. init_bridge_misc(bridge);
  945. }
  946. /* Destroy hotplug slots associated with the PCI bus */
  947. void acpiphp_remove_slots(struct pci_bus *bus)
  948. {
  949. struct acpiphp_bridge *bridge, *tmp;
  950. if (acpiphp_disabled)
  951. return;
  952. list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
  953. if (bridge->pci_bus == bus) {
  954. cleanup_bridge(bridge);
  955. break;
  956. }
  957. }
  958. /**
  959. * acpiphp_enable_slot - power on slot
  960. * @slot: ACPI PHP slot
  961. */
  962. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  963. {
  964. int retval;
  965. mutex_lock(&slot->crit_sect);
  966. /* wake up all functions */
  967. retval = power_on_slot(slot);
  968. if (retval)
  969. goto err_exit;
  970. if (get_slot_status(slot) == ACPI_STA_ALL) {
  971. /* configure all functions */
  972. retval = enable_device(slot);
  973. if (retval)
  974. power_off_slot(slot);
  975. } else {
  976. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  977. power_off_slot(slot);
  978. }
  979. err_exit:
  980. mutex_unlock(&slot->crit_sect);
  981. return retval;
  982. }
  983. /**
  984. * acpiphp_disable_slot - power off slot
  985. * @slot: ACPI PHP slot
  986. */
  987. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  988. {
  989. int retval = 0;
  990. mutex_lock(&slot->crit_sect);
  991. /* unconfigure all functions */
  992. retval = disable_device(slot);
  993. if (retval)
  994. goto err_exit;
  995. /* power off all functions */
  996. retval = power_off_slot(slot);
  997. if (retval)
  998. goto err_exit;
  999. err_exit:
  1000. mutex_unlock(&slot->crit_sect);
  1001. return retval;
  1002. }
  1003. /*
  1004. * slot enabled: 1
  1005. * slot disabled: 0
  1006. */
  1007. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1008. {
  1009. return (slot->flags & SLOT_POWEREDON);
  1010. }
  1011. /*
  1012. * latch open: 1
  1013. * latch closed: 0
  1014. */
  1015. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1016. {
  1017. unsigned int sta;
  1018. sta = get_slot_status(slot);
  1019. return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
  1020. }
  1021. /*
  1022. * adapter presence : 1
  1023. * absence : 0
  1024. */
  1025. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1026. {
  1027. unsigned int sta;
  1028. sta = get_slot_status(slot);
  1029. return (sta == 0) ? 0 : 1;
  1030. }