acpiphp_glue.c 29 KB

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