acpiphp_glue.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or (at
  15. * your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  20. * NON INFRINGEMENT. See the GNU General Public License for more
  21. * details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. *
  27. * Send feedback to <t-kochi@bq.jp.nec.com>
  28. *
  29. */
  30. /*
  31. * Lifetime rules for pci_dev:
  32. * - The one in acpiphp_func has its refcount elevated by pci_get_slot()
  33. * when the driver is loaded or when an insertion event occurs. It loses
  34. * a refcount when its ejected or the driver unloads.
  35. * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
  36. * when the bridge is scanned and it loses a refcount when the bridge
  37. * is removed.
  38. */
  39. #include <linux/init.h>
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/pci.h>
  43. #include <linux/smp_lock.h>
  44. #include <asm/semaphore.h>
  45. #include "../pci.h"
  46. #include "pci_hotplug.h"
  47. #include "acpiphp.h"
  48. static LIST_HEAD(bridge_list);
  49. #define MY_NAME "acpiphp_glue"
  50. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  51. static void handle_hotplug_event_func (acpi_handle, u32, void *);
  52. /*
  53. * initialization & terminatation routines
  54. */
  55. /**
  56. * is_ejectable - determine if a slot is ejectable
  57. * @handle: handle to acpi namespace
  58. *
  59. * Ejectable slot should satisfy at least these conditions:
  60. *
  61. * 1. has _ADR method
  62. * 2. has _EJ0 method
  63. *
  64. * optionally
  65. *
  66. * 1. has _STA method
  67. * 2. has _PS0 method
  68. * 3. has _PS3 method
  69. * 4. ..
  70. *
  71. */
  72. static int is_ejectable(acpi_handle handle)
  73. {
  74. acpi_status status;
  75. acpi_handle tmp;
  76. status = acpi_get_handle(handle, "_ADR", &tmp);
  77. if (ACPI_FAILURE(status)) {
  78. return 0;
  79. }
  80. status = acpi_get_handle(handle, "_EJ0", &tmp);
  81. if (ACPI_FAILURE(status)) {
  82. return 0;
  83. }
  84. return 1;
  85. }
  86. /* callback routine to check the existence of ejectable slots */
  87. static acpi_status
  88. is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  89. {
  90. int *count = (int *)context;
  91. if (is_ejectable(handle)) {
  92. (*count)++;
  93. /* only one ejectable slot is enough */
  94. return AE_CTRL_TERMINATE;
  95. } else {
  96. return AE_OK;
  97. }
  98. }
  99. /* callback routine to register each ACPI PCI slot object */
  100. static acpi_status
  101. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  102. {
  103. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
  104. struct acpiphp_slot *slot;
  105. struct acpiphp_func *newfunc;
  106. acpi_handle tmp;
  107. acpi_status status = AE_OK;
  108. unsigned long adr, sun;
  109. int device, function;
  110. static int num_slots = 0; /* XXX if we support I/O node hotplug... */
  111. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  112. if (ACPI_FAILURE(status))
  113. return AE_OK;
  114. status = acpi_get_handle(handle, "_EJ0", &tmp);
  115. if (ACPI_FAILURE(status))
  116. return AE_OK;
  117. device = (adr >> 16) & 0xffff;
  118. function = adr & 0xffff;
  119. newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
  120. if (!newfunc)
  121. return AE_NO_MEMORY;
  122. memset(newfunc, 0, sizeof(struct acpiphp_func));
  123. INIT_LIST_HEAD(&newfunc->sibling);
  124. newfunc->handle = handle;
  125. newfunc->function = function;
  126. newfunc->flags = FUNC_HAS_EJ0;
  127. if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
  128. newfunc->flags |= FUNC_HAS_STA;
  129. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
  130. newfunc->flags |= FUNC_HAS_PS0;
  131. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
  132. newfunc->flags |= FUNC_HAS_PS3;
  133. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  134. if (ACPI_FAILURE(status))
  135. sun = -1;
  136. /* search for objects that share the same slot */
  137. for (slot = bridge->slots; slot; slot = slot->next)
  138. if (slot->device == device) {
  139. if (slot->sun != sun)
  140. warn("sibling found, but _SUN doesn't match!\n");
  141. break;
  142. }
  143. if (!slot) {
  144. slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  145. if (!slot) {
  146. kfree(newfunc);
  147. return AE_NO_MEMORY;
  148. }
  149. memset(slot, 0, sizeof(struct acpiphp_slot));
  150. slot->bridge = bridge;
  151. slot->id = num_slots++;
  152. slot->device = device;
  153. slot->sun = sun;
  154. INIT_LIST_HEAD(&slot->funcs);
  155. init_MUTEX(&slot->crit_sect);
  156. slot->next = bridge->slots;
  157. bridge->slots = slot;
  158. bridge->nr_slots++;
  159. dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
  160. slot->sun, pci_domain_nr(bridge->pci_bus),
  161. bridge->pci_bus->number, slot->device);
  162. }
  163. newfunc->slot = slot;
  164. list_add_tail(&newfunc->sibling, &slot->funcs);
  165. /* associate corresponding pci_dev */
  166. newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
  167. PCI_DEVFN(device, function));
  168. if (newfunc->pci_dev) {
  169. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  170. }
  171. /* install notify handler */
  172. status = acpi_install_notify_handler(handle,
  173. ACPI_SYSTEM_NOTIFY,
  174. handle_hotplug_event_func,
  175. newfunc);
  176. if (ACPI_FAILURE(status)) {
  177. err("failed to register interrupt notify handler\n");
  178. return status;
  179. }
  180. return AE_OK;
  181. }
  182. /* see if it's worth looking at this bridge */
  183. static int detect_ejectable_slots(acpi_handle *bridge_handle)
  184. {
  185. acpi_status status;
  186. int count;
  187. count = 0;
  188. /* only check slots defined directly below bridge object */
  189. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
  190. is_ejectable_slot, (void *)&count, NULL);
  191. return count;
  192. }
  193. /* decode ACPI 2.0 _HPP hot plug parameters */
  194. static void decode_hpp(struct acpiphp_bridge *bridge)
  195. {
  196. acpi_status status;
  197. struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
  198. .pointer = NULL};
  199. union acpi_object *package;
  200. int i;
  201. /* default numbers */
  202. bridge->hpp.cache_line_size = 0x10;
  203. bridge->hpp.latency_timer = 0x40;
  204. bridge->hpp.enable_SERR = 0;
  205. bridge->hpp.enable_PERR = 0;
  206. status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
  207. if (ACPI_FAILURE(status)) {
  208. dbg("_HPP evaluation failed\n");
  209. return;
  210. }
  211. package = (union acpi_object *) buffer.pointer;
  212. if (!package || package->type != ACPI_TYPE_PACKAGE ||
  213. package->package.count != 4 || !package->package.elements) {
  214. err("invalid _HPP object; ignoring\n");
  215. goto err_exit;
  216. }
  217. for (i = 0; i < 4; i++) {
  218. if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
  219. err("invalid _HPP parameter type; ignoring\n");
  220. goto err_exit;
  221. }
  222. }
  223. bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
  224. bridge->hpp.latency_timer = package->package.elements[1].integer.value;
  225. bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
  226. bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
  227. dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
  228. bridge->hpp.cache_line_size,
  229. bridge->hpp.latency_timer,
  230. bridge->hpp.enable_SERR,
  231. bridge->hpp.enable_PERR);
  232. bridge->flags |= BRIDGE_HAS_HPP;
  233. err_exit:
  234. kfree(buffer.pointer);
  235. }
  236. /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
  237. static void init_bridge_misc(struct acpiphp_bridge *bridge)
  238. {
  239. acpi_status status;
  240. /* decode ACPI 2.0 _HPP (hot plug parameters) */
  241. decode_hpp(bridge);
  242. /* register all slot objects under this bridge */
  243. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
  244. register_slot, bridge, NULL);
  245. /* install notify handler */
  246. status = acpi_install_notify_handler(bridge->handle,
  247. ACPI_SYSTEM_NOTIFY,
  248. handle_hotplug_event_bridge,
  249. bridge);
  250. if (ACPI_FAILURE(status)) {
  251. err("failed to register interrupt notify handler\n");
  252. }
  253. list_add(&bridge->list, &bridge_list);
  254. }
  255. /* allocate and initialize host bridge data structure */
  256. static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
  257. {
  258. struct acpiphp_bridge *bridge;
  259. bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  260. if (bridge == NULL)
  261. return;
  262. memset(bridge, 0, sizeof(struct acpiphp_bridge));
  263. bridge->type = BRIDGE_TYPE_HOST;
  264. bridge->handle = handle;
  265. bridge->pci_bus = pci_bus;
  266. spin_lock_init(&bridge->res_lock);
  267. init_bridge_misc(bridge);
  268. }
  269. /* allocate and initialize PCI-to-PCI bridge data structure */
  270. static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
  271. {
  272. struct acpiphp_bridge *bridge;
  273. bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  274. if (bridge == NULL) {
  275. err("out of memory\n");
  276. return;
  277. }
  278. memset(bridge, 0, sizeof(struct acpiphp_bridge));
  279. bridge->type = BRIDGE_TYPE_P2P;
  280. bridge->handle = handle;
  281. bridge->pci_dev = pci_dev_get(pci_dev);
  282. bridge->pci_bus = pci_dev->subordinate;
  283. if (!bridge->pci_bus) {
  284. err("This is not a PCI-to-PCI bridge!\n");
  285. goto err;
  286. }
  287. spin_lock_init(&bridge->res_lock);
  288. init_bridge_misc(bridge);
  289. return;
  290. err:
  291. pci_dev_put(pci_dev);
  292. kfree(bridge);
  293. return;
  294. }
  295. /* callback routine to find P2P bridges */
  296. static acpi_status
  297. find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  298. {
  299. acpi_status status;
  300. acpi_handle dummy_handle;
  301. unsigned long tmp;
  302. int device, function;
  303. struct pci_dev *dev;
  304. struct pci_bus *pci_bus = context;
  305. status = acpi_get_handle(handle, "_ADR", &dummy_handle);
  306. if (ACPI_FAILURE(status))
  307. return AE_OK; /* continue */
  308. status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
  309. if (ACPI_FAILURE(status)) {
  310. dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
  311. return AE_OK;
  312. }
  313. device = (tmp >> 16) & 0xffff;
  314. function = tmp & 0xffff;
  315. dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
  316. if (!dev || !dev->subordinate)
  317. goto out;
  318. /* check if this bridge has ejectable slots */
  319. if (detect_ejectable_slots(handle) > 0) {
  320. dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
  321. add_p2p_bridge(handle, dev);
  322. }
  323. out:
  324. pci_dev_put(dev);
  325. return AE_OK;
  326. }
  327. /* find hot-pluggable slots, and then find P2P bridge */
  328. static int add_bridge(acpi_handle handle)
  329. {
  330. acpi_status status;
  331. unsigned long tmp;
  332. int seg, bus;
  333. acpi_handle dummy_handle;
  334. struct pci_bus *pci_bus;
  335. /* if the bridge doesn't have _STA, we assume it is always there */
  336. status = acpi_get_handle(handle, "_STA", &dummy_handle);
  337. if (ACPI_SUCCESS(status)) {
  338. status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
  339. if (ACPI_FAILURE(status)) {
  340. dbg("%s: _STA evaluation failure\n", __FUNCTION__);
  341. return 0;
  342. }
  343. if ((tmp & ACPI_STA_FUNCTIONING) == 0)
  344. /* don't register this object */
  345. return 0;
  346. }
  347. /* get PCI segment number */
  348. status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
  349. seg = ACPI_SUCCESS(status) ? tmp : 0;
  350. /* get PCI bus number */
  351. status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
  352. if (ACPI_SUCCESS(status)) {
  353. bus = tmp;
  354. } else {
  355. warn("can't get bus number, assuming 0\n");
  356. bus = 0;
  357. }
  358. pci_bus = pci_find_bus(seg, bus);
  359. if (!pci_bus) {
  360. err("Can't find bus %04x:%02x\n", seg, bus);
  361. return 0;
  362. }
  363. /* check if this bridge has ejectable slots */
  364. if (detect_ejectable_slots(handle) > 0) {
  365. dbg("found PCI host-bus bridge with hot-pluggable slots\n");
  366. add_host_bridge(handle, pci_bus);
  367. return 0;
  368. }
  369. /* search P2P bridges under this host bridge */
  370. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  371. find_p2p_bridge, pci_bus, NULL);
  372. if (ACPI_FAILURE(status))
  373. warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
  374. return 0;
  375. }
  376. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  377. {
  378. struct list_head *head;
  379. list_for_each(head, &bridge_list) {
  380. struct acpiphp_bridge *bridge = list_entry(head,
  381. struct acpiphp_bridge, list);
  382. if (bridge->handle == handle)
  383. return bridge;
  384. }
  385. return NULL;
  386. }
  387. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  388. {
  389. struct list_head *list, *tmp;
  390. struct acpiphp_slot *slot;
  391. acpi_status status;
  392. acpi_handle handle = bridge->handle;
  393. status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  394. handle_hotplug_event_bridge);
  395. if (ACPI_FAILURE(status))
  396. err("failed to remove notify handler\n");
  397. slot = bridge->slots;
  398. while (slot) {
  399. struct acpiphp_slot *next = slot->next;
  400. list_for_each_safe (list, tmp, &slot->funcs) {
  401. struct acpiphp_func *func;
  402. func = list_entry(list, struct acpiphp_func, sibling);
  403. status = acpi_remove_notify_handler(func->handle,
  404. ACPI_SYSTEM_NOTIFY,
  405. handle_hotplug_event_func);
  406. if (ACPI_FAILURE(status))
  407. err("failed to remove notify handler\n");
  408. pci_dev_put(func->pci_dev);
  409. list_del(list);
  410. kfree(func);
  411. }
  412. kfree(slot);
  413. slot = next;
  414. }
  415. pci_dev_put(bridge->pci_dev);
  416. list_del(&bridge->list);
  417. kfree(bridge);
  418. }
  419. static acpi_status
  420. cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  421. {
  422. struct acpiphp_bridge *bridge;
  423. if (!(bridge = acpiphp_handle_to_bridge(handle)))
  424. return AE_OK;
  425. cleanup_bridge(bridge);
  426. return AE_OK;
  427. }
  428. static void remove_bridge(acpi_handle handle)
  429. {
  430. struct acpiphp_bridge *bridge;
  431. bridge = acpiphp_handle_to_bridge(handle);
  432. if (bridge) {
  433. cleanup_bridge(bridge);
  434. } else {
  435. /* clean-up p2p bridges under this host bridge */
  436. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  437. (u32)1, cleanup_p2p_bridge, NULL, NULL);
  438. }
  439. }
  440. static int power_on_slot(struct acpiphp_slot *slot)
  441. {
  442. acpi_status status;
  443. struct acpiphp_func *func;
  444. struct list_head *l;
  445. int retval = 0;
  446. /* if already enabled, just skip */
  447. if (slot->flags & SLOT_POWEREDON)
  448. goto err_exit;
  449. list_for_each (l, &slot->funcs) {
  450. func = list_entry(l, struct acpiphp_func, sibling);
  451. if (func->flags & FUNC_HAS_PS0) {
  452. dbg("%s: executing _PS0\n", __FUNCTION__);
  453. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  454. if (ACPI_FAILURE(status)) {
  455. warn("%s: _PS0 failed\n", __FUNCTION__);
  456. retval = -1;
  457. goto err_exit;
  458. } else
  459. break;
  460. }
  461. }
  462. /* TBD: evaluate _STA to check if the slot is enabled */
  463. slot->flags |= SLOT_POWEREDON;
  464. err_exit:
  465. return retval;
  466. }
  467. static int power_off_slot(struct acpiphp_slot *slot)
  468. {
  469. acpi_status status;
  470. struct acpiphp_func *func;
  471. struct list_head *l;
  472. struct acpi_object_list arg_list;
  473. union acpi_object arg;
  474. int retval = 0;
  475. /* if already disabled, just skip */
  476. if ((slot->flags & SLOT_POWEREDON) == 0)
  477. goto err_exit;
  478. list_for_each (l, &slot->funcs) {
  479. func = list_entry(l, struct acpiphp_func, sibling);
  480. if (func->pci_dev && (func->flags & FUNC_HAS_PS3)) {
  481. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  482. if (ACPI_FAILURE(status)) {
  483. warn("%s: _PS3 failed\n", __FUNCTION__);
  484. retval = -1;
  485. goto err_exit;
  486. } else
  487. break;
  488. }
  489. }
  490. list_for_each (l, &slot->funcs) {
  491. func = list_entry(l, struct acpiphp_func, sibling);
  492. /* We don't want to call _EJ0 on non-existing functions. */
  493. if (func->pci_dev && (func->flags & FUNC_HAS_EJ0)) {
  494. /* _EJ0 method take one argument */
  495. arg_list.count = 1;
  496. arg_list.pointer = &arg;
  497. arg.type = ACPI_TYPE_INTEGER;
  498. arg.integer.value = 1;
  499. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  500. if (ACPI_FAILURE(status)) {
  501. warn("%s: _EJ0 failed\n", __FUNCTION__);
  502. retval = -1;
  503. goto err_exit;
  504. } else
  505. break;
  506. }
  507. }
  508. /* TBD: evaluate _STA to check if the slot is disabled */
  509. slot->flags &= (~SLOT_POWEREDON);
  510. err_exit:
  511. return retval;
  512. }
  513. /**
  514. * enable_device - enable, configure a slot
  515. * @slot: slot to be enabled
  516. *
  517. * This function should be called per *physical slot*,
  518. * not per each slot object in ACPI namespace.
  519. *
  520. */
  521. static int enable_device(struct acpiphp_slot *slot)
  522. {
  523. struct pci_dev *dev;
  524. struct pci_bus *bus = slot->bridge->pci_bus;
  525. struct list_head *l;
  526. struct acpiphp_func *func;
  527. int retval = 0;
  528. int num, max, pass;
  529. if (slot->flags & SLOT_ENABLED)
  530. goto err_exit;
  531. /* sanity check: dev should be NULL when hot-plugged in */
  532. dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  533. if (dev) {
  534. /* This case shouldn't happen */
  535. err("pci_dev structure already exists.\n");
  536. pci_dev_put(dev);
  537. retval = -1;
  538. goto err_exit;
  539. }
  540. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  541. if (num == 0) {
  542. err("No new device found\n");
  543. retval = -1;
  544. goto err_exit;
  545. }
  546. max = bus->secondary;
  547. for (pass = 0; pass < 2; pass++) {
  548. list_for_each_entry(dev, &bus->devices, bus_list) {
  549. if (PCI_SLOT(dev->devfn) != slot->device)
  550. continue;
  551. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  552. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  553. max = pci_scan_bridge(bus, dev, max, pass);
  554. }
  555. }
  556. pci_bus_assign_resources(bus);
  557. pci_bus_add_devices(bus);
  558. /* associate pci_dev to our representation */
  559. list_for_each (l, &slot->funcs) {
  560. func = list_entry(l, struct acpiphp_func, sibling);
  561. func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  562. func->function));
  563. }
  564. slot->flags |= SLOT_ENABLED;
  565. err_exit:
  566. return retval;
  567. }
  568. /**
  569. * disable_device - disable a slot
  570. */
  571. static int disable_device(struct acpiphp_slot *slot)
  572. {
  573. int retval = 0;
  574. struct acpiphp_func *func;
  575. struct list_head *l;
  576. /* is this slot already disabled? */
  577. if (!(slot->flags & SLOT_ENABLED))
  578. goto err_exit;
  579. list_for_each (l, &slot->funcs) {
  580. func = list_entry(l, struct acpiphp_func, sibling);
  581. if (!func->pci_dev)
  582. continue;
  583. pci_remove_bus_device(func->pci_dev);
  584. pci_dev_put(func->pci_dev);
  585. func->pci_dev = NULL;
  586. }
  587. slot->flags &= (~SLOT_ENABLED);
  588. err_exit:
  589. return retval;
  590. }
  591. /**
  592. * get_slot_status - get ACPI slot status
  593. *
  594. * if a slot has _STA for each function and if any one of them
  595. * returned non-zero status, return it
  596. *
  597. * if a slot doesn't have _STA and if any one of its functions'
  598. * configuration space is configured, return 0x0f as a _STA
  599. *
  600. * otherwise return 0
  601. */
  602. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  603. {
  604. acpi_status status;
  605. unsigned long sta = 0;
  606. u32 dvid;
  607. struct list_head *l;
  608. struct acpiphp_func *func;
  609. list_for_each (l, &slot->funcs) {
  610. func = list_entry(l, struct acpiphp_func, sibling);
  611. if (func->flags & FUNC_HAS_STA) {
  612. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  613. if (ACPI_SUCCESS(status) && sta)
  614. break;
  615. } else {
  616. pci_bus_read_config_dword(slot->bridge->pci_bus,
  617. PCI_DEVFN(slot->device,
  618. func->function),
  619. PCI_VENDOR_ID, &dvid);
  620. if (dvid != 0xffffffff) {
  621. sta = ACPI_STA_ALL;
  622. break;
  623. }
  624. }
  625. }
  626. return (unsigned int)sta;
  627. }
  628. /**
  629. * acpiphp_check_bridge - re-enumerate devices
  630. *
  631. * Iterate over all slots under this bridge and make sure that if a
  632. * card is present they are enabled, and if not they are disabled.
  633. */
  634. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  635. {
  636. struct acpiphp_slot *slot;
  637. int retval = 0;
  638. int enabled, disabled;
  639. enabled = disabled = 0;
  640. for (slot = bridge->slots; slot; slot = slot->next) {
  641. unsigned int status = get_slot_status(slot);
  642. if (slot->flags & SLOT_ENABLED) {
  643. if (status == ACPI_STA_ALL)
  644. continue;
  645. retval = acpiphp_disable_slot(slot);
  646. if (retval) {
  647. err("Error occurred in disabling\n");
  648. goto err_exit;
  649. }
  650. disabled++;
  651. } else {
  652. if (status != ACPI_STA_ALL)
  653. continue;
  654. retval = acpiphp_enable_slot(slot);
  655. if (retval) {
  656. err("Error occurred in enabling\n");
  657. goto err_exit;
  658. }
  659. enabled++;
  660. }
  661. }
  662. dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
  663. err_exit:
  664. return retval;
  665. }
  666. /*
  667. * ACPI event handlers
  668. */
  669. /**
  670. * handle_hotplug_event_bridge - handle ACPI event on bridges
  671. *
  672. * @handle: Notify()'ed acpi_handle
  673. * @type: Notify code
  674. * @context: pointer to acpiphp_bridge structure
  675. *
  676. * handles ACPI event notification on {host,p2p} bridges
  677. *
  678. */
  679. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
  680. {
  681. struct acpiphp_bridge *bridge;
  682. char objname[64];
  683. struct acpi_buffer buffer = { .length = sizeof(objname),
  684. .pointer = objname };
  685. bridge = (struct acpiphp_bridge *)context;
  686. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  687. switch (type) {
  688. case ACPI_NOTIFY_BUS_CHECK:
  689. /* bus re-enumerate */
  690. dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
  691. acpiphp_check_bridge(bridge);
  692. break;
  693. case ACPI_NOTIFY_DEVICE_CHECK:
  694. /* device check */
  695. dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
  696. acpiphp_check_bridge(bridge);
  697. break;
  698. case ACPI_NOTIFY_DEVICE_WAKE:
  699. /* wake event */
  700. dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
  701. break;
  702. case ACPI_NOTIFY_EJECT_REQUEST:
  703. /* request device eject */
  704. dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
  705. break;
  706. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  707. printk(KERN_ERR "Device %s cannot be configured due"
  708. " to a frequency mismatch\n", objname);
  709. break;
  710. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  711. printk(KERN_ERR "Device %s cannot be configured due"
  712. " to a bus mode mismatch\n", objname);
  713. break;
  714. case ACPI_NOTIFY_POWER_FAULT:
  715. printk(KERN_ERR "Device %s has suffered a power fault\n",
  716. objname);
  717. break;
  718. default:
  719. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  720. break;
  721. }
  722. }
  723. /**
  724. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  725. *
  726. * @handle: Notify()'ed acpi_handle
  727. * @type: Notify code
  728. * @context: pointer to acpiphp_func structure
  729. *
  730. * handles ACPI event notification on slots
  731. *
  732. */
  733. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
  734. {
  735. struct acpiphp_func *func;
  736. char objname[64];
  737. struct acpi_buffer buffer = { .length = sizeof(objname),
  738. .pointer = objname };
  739. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  740. func = (struct acpiphp_func *)context;
  741. switch (type) {
  742. case ACPI_NOTIFY_BUS_CHECK:
  743. /* bus re-enumerate */
  744. dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
  745. acpiphp_enable_slot(func->slot);
  746. break;
  747. case ACPI_NOTIFY_DEVICE_CHECK:
  748. /* device check : re-enumerate from parent bus */
  749. dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
  750. acpiphp_check_bridge(func->slot->bridge);
  751. break;
  752. case ACPI_NOTIFY_DEVICE_WAKE:
  753. /* wake event */
  754. dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
  755. break;
  756. case ACPI_NOTIFY_EJECT_REQUEST:
  757. /* request device eject */
  758. dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
  759. acpiphp_disable_slot(func->slot);
  760. break;
  761. default:
  762. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  763. break;
  764. }
  765. }
  766. static struct acpi_pci_driver acpi_pci_hp_driver = {
  767. .add = add_bridge,
  768. .remove = remove_bridge,
  769. };
  770. /**
  771. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  772. *
  773. */
  774. int __init acpiphp_glue_init(void)
  775. {
  776. int num;
  777. if (list_empty(&pci_root_buses))
  778. return -1;
  779. num = acpi_pci_register_driver(&acpi_pci_hp_driver);
  780. if (num <= 0)
  781. return -1;
  782. return 0;
  783. }
  784. /**
  785. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  786. *
  787. * This function frees all data allocated in acpiphp_glue_init()
  788. */
  789. void __exit acpiphp_glue_exit(void)
  790. {
  791. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  792. }
  793. /**
  794. * acpiphp_get_num_slots - count number of slots in a system
  795. */
  796. int __init acpiphp_get_num_slots(void)
  797. {
  798. struct list_head *node;
  799. struct acpiphp_bridge *bridge;
  800. int num_slots;
  801. num_slots = 0;
  802. list_for_each (node, &bridge_list) {
  803. bridge = (struct acpiphp_bridge *)node;
  804. dbg("Bus %04x:%02x has %d slot%s\n",
  805. pci_domain_nr(bridge->pci_bus),
  806. bridge->pci_bus->number, bridge->nr_slots,
  807. bridge->nr_slots == 1 ? "" : "s");
  808. num_slots += bridge->nr_slots;
  809. }
  810. dbg("Total %d slots\n", num_slots);
  811. return num_slots;
  812. }
  813. #if 0
  814. /**
  815. * acpiphp_for_each_slot - call function for each slot
  816. * @fn: callback function
  817. * @data: context to be passed to callback function
  818. *
  819. */
  820. static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
  821. {
  822. struct list_head *node;
  823. struct acpiphp_bridge *bridge;
  824. struct acpiphp_slot *slot;
  825. int retval = 0;
  826. list_for_each (node, &bridge_list) {
  827. bridge = (struct acpiphp_bridge *)node;
  828. for (slot = bridge->slots; slot; slot = slot->next) {
  829. retval = fn(slot, data);
  830. if (!retval)
  831. goto err_exit;
  832. }
  833. }
  834. err_exit:
  835. return retval;
  836. }
  837. #endif
  838. /* search matching slot from id */
  839. struct acpiphp_slot *get_slot_from_id(int id)
  840. {
  841. struct list_head *node;
  842. struct acpiphp_bridge *bridge;
  843. struct acpiphp_slot *slot;
  844. list_for_each (node, &bridge_list) {
  845. bridge = (struct acpiphp_bridge *)node;
  846. for (slot = bridge->slots; slot; slot = slot->next)
  847. if (slot->id == id)
  848. return slot;
  849. }
  850. /* should never happen! */
  851. err("%s: no object for id %d\n", __FUNCTION__, id);
  852. WARN_ON(1);
  853. return NULL;
  854. }
  855. /**
  856. * acpiphp_enable_slot - power on slot
  857. */
  858. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  859. {
  860. int retval;
  861. down(&slot->crit_sect);
  862. /* wake up all functions */
  863. retval = power_on_slot(slot);
  864. if (retval)
  865. goto err_exit;
  866. if (get_slot_status(slot) == ACPI_STA_ALL)
  867. /* configure all functions */
  868. retval = enable_device(slot);
  869. err_exit:
  870. up(&slot->crit_sect);
  871. return retval;
  872. }
  873. /**
  874. * acpiphp_disable_slot - power off slot
  875. */
  876. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  877. {
  878. int retval = 0;
  879. down(&slot->crit_sect);
  880. /* unconfigure all functions */
  881. retval = disable_device(slot);
  882. if (retval)
  883. goto err_exit;
  884. /* power off all functions */
  885. retval = power_off_slot(slot);
  886. if (retval)
  887. goto err_exit;
  888. err_exit:
  889. up(&slot->crit_sect);
  890. return retval;
  891. }
  892. /*
  893. * slot enabled: 1
  894. * slot disabled: 0
  895. */
  896. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  897. {
  898. unsigned int sta;
  899. sta = get_slot_status(slot);
  900. return (sta & ACPI_STA_ENABLED) ? 1 : 0;
  901. }
  902. /*
  903. * latch closed: 1
  904. * latch open: 0
  905. */
  906. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  907. {
  908. unsigned int sta;
  909. sta = get_slot_status(slot);
  910. return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
  911. }
  912. /*
  913. * adapter presence : 1
  914. * absence : 0
  915. */
  916. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  917. {
  918. unsigned int sta;
  919. sta = get_slot_status(slot);
  920. return (sta == 0) ? 0 : 1;
  921. }
  922. /*
  923. * pci address (seg/bus/dev)
  924. */
  925. u32 acpiphp_get_address(struct acpiphp_slot *slot)
  926. {
  927. u32 address;
  928. struct pci_bus *pci_bus = slot->bridge->pci_bus;
  929. address = (pci_domain_nr(pci_bus) << 16) |
  930. (pci_bus->number << 8) |
  931. slot->device;
  932. return address;
  933. }