pciehp_pci.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * PCI Express Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2003-2004 Intel Corporation
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19. * NON INFRINGEMENT. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/types.h>
  32. #include <linux/pci.h>
  33. #include "../pci.h"
  34. #include "pciehp.h"
  35. static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
  36. {
  37. u16 pci_cmd, pci_bctl;
  38. if (hpp->revision > 1) {
  39. printk(KERN_WARNING "%s: Rev.%d type0 record not supported\n",
  40. __func__, hpp->revision);
  41. return;
  42. }
  43. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
  44. pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
  45. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  46. if (hpp->enable_serr)
  47. pci_cmd |= PCI_COMMAND_SERR;
  48. else
  49. pci_cmd &= ~PCI_COMMAND_SERR;
  50. if (hpp->enable_perr)
  51. pci_cmd |= PCI_COMMAND_PARITY;
  52. else
  53. pci_cmd &= ~PCI_COMMAND_PARITY;
  54. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  55. /* Program bridge control value */
  56. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  57. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  58. hpp->latency_timer);
  59. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  60. if (hpp->enable_serr)
  61. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  62. else
  63. pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
  64. if (hpp->enable_perr)
  65. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  66. else
  67. pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
  68. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  69. }
  70. }
  71. static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
  72. {
  73. int pos;
  74. u16 reg16;
  75. u32 reg32;
  76. if (hpp->revision > 1) {
  77. printk(KERN_WARNING "%s: Rev.%d type2 record not supported\n",
  78. __func__, hpp->revision);
  79. return;
  80. }
  81. /* Find PCI Express capability */
  82. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  83. if (!pos)
  84. return;
  85. /* Initialize Device Control Register */
  86. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &reg16);
  87. reg16 = (reg16 & hpp->pci_exp_devctl_and) | hpp->pci_exp_devctl_or;
  88. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, reg16);
  89. /* Initialize Link Control Register */
  90. if (dev->subordinate) {
  91. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &reg16);
  92. reg16 = (reg16 & hpp->pci_exp_lnkctl_and)
  93. | hpp->pci_exp_lnkctl_or;
  94. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, reg16);
  95. }
  96. /* Find Advanced Error Reporting Enhanced Capability */
  97. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  98. if (!pos)
  99. return;
  100. /* Initialize Uncorrectable Error Mask Register */
  101. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
  102. reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
  103. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
  104. /* Initialize Uncorrectable Error Severity Register */
  105. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
  106. reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
  107. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
  108. /* Initialize Correctable Error Mask Register */
  109. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
  110. reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
  111. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
  112. /* Initialize Advanced Error Capabilities and Control Register */
  113. pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
  114. reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
  115. pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
  116. /*
  117. * FIXME: The following two registers are not supported yet.
  118. *
  119. * o Secondary Uncorrectable Error Severity Register
  120. * o Secondary Uncorrectable Error Mask Register
  121. */
  122. }
  123. static void program_fw_provided_values(struct pci_dev *dev)
  124. {
  125. struct pci_dev *cdev;
  126. struct hotplug_params hpp;
  127. /* Program hpp values for this device */
  128. if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
  129. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  130. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
  131. return;
  132. if (pciehp_get_hp_params_from_firmware(dev, &hpp)) {
  133. printk(KERN_WARNING "%s: Could not get hotplug parameters\n",
  134. __func__);
  135. return;
  136. }
  137. if (hpp.t2)
  138. program_hpp_type2(dev, hpp.t2);
  139. if (hpp.t0)
  140. program_hpp_type0(dev, hpp.t0);
  141. /* Program child devices */
  142. if (dev->subordinate) {
  143. list_for_each_entry(cdev, &dev->subordinate->devices,
  144. bus_list)
  145. program_fw_provided_values(cdev);
  146. }
  147. }
  148. static int __ref pciehp_add_bridge(struct pci_dev *dev)
  149. {
  150. struct pci_bus *parent = dev->bus;
  151. int pass, busnr, start = parent->secondary;
  152. int end = parent->subordinate;
  153. for (busnr = start; busnr <= end; busnr++) {
  154. if (!pci_find_bus(pci_domain_nr(parent), busnr))
  155. break;
  156. }
  157. if (busnr-- > end) {
  158. err("No bus number available for hot-added bridge %s\n",
  159. pci_name(dev));
  160. return -1;
  161. }
  162. for (pass = 0; pass < 2; pass++)
  163. busnr = pci_scan_bridge(parent, dev, busnr, pass);
  164. if (!dev->subordinate)
  165. return -1;
  166. pci_bus_size_bridges(dev->subordinate);
  167. pci_bus_assign_resources(parent);
  168. pci_enable_bridges(parent);
  169. pci_bus_add_devices(parent);
  170. return 0;
  171. }
  172. int pciehp_configure_device(struct slot *p_slot)
  173. {
  174. struct pci_dev *dev;
  175. struct pci_bus *parent = p_slot->ctrl->pci_dev->subordinate;
  176. int num, fn;
  177. dev = pci_get_slot(parent, PCI_DEVFN(p_slot->device, 0));
  178. if (dev) {
  179. err("Device %s already exists at %x:%x, cannot hot-add\n",
  180. pci_name(dev), p_slot->bus, p_slot->device);
  181. pci_dev_put(dev);
  182. return -EINVAL;
  183. }
  184. num = pci_scan_slot(parent, PCI_DEVFN(p_slot->device, 0));
  185. if (num == 0) {
  186. err("No new device found\n");
  187. return -ENODEV;
  188. }
  189. for (fn = 0; fn < 8; fn++) {
  190. dev = pci_get_slot(parent, PCI_DEVFN(p_slot->device, fn));
  191. if (!dev)
  192. continue;
  193. if ((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
  194. err("Cannot hot-add display device %s\n",
  195. pci_name(dev));
  196. pci_dev_put(dev);
  197. continue;
  198. }
  199. if ((dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) ||
  200. (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)) {
  201. pciehp_add_bridge(dev);
  202. }
  203. program_fw_provided_values(dev);
  204. pci_dev_put(dev);
  205. }
  206. pci_bus_assign_resources(parent);
  207. pci_bus_add_devices(parent);
  208. return 0;
  209. }
  210. int pciehp_unconfigure_device(struct slot *p_slot)
  211. {
  212. int ret, rc = 0;
  213. int j;
  214. u8 bctl = 0;
  215. u8 presence = 0;
  216. struct pci_bus *parent = p_slot->ctrl->pci_dev->subordinate;
  217. u16 command;
  218. dbg("%s: bus/dev = %x/%x\n", __func__, p_slot->bus,
  219. p_slot->device);
  220. ret = p_slot->hpc_ops->get_adapter_status(p_slot, &presence);
  221. if (ret)
  222. presence = 0;
  223. for (j = 0; j < 8; j++) {
  224. struct pci_dev* temp = pci_get_slot(parent,
  225. (p_slot->device << 3) | j);
  226. if (!temp)
  227. continue;
  228. if ((temp->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
  229. err("Cannot remove display device %s\n",
  230. pci_name(temp));
  231. pci_dev_put(temp);
  232. continue;
  233. }
  234. if (temp->hdr_type == PCI_HEADER_TYPE_BRIDGE && presence) {
  235. pci_read_config_byte(temp, PCI_BRIDGE_CONTROL, &bctl);
  236. if (bctl & PCI_BRIDGE_CTL_VGA) {
  237. err("Cannot remove display device %s\n",
  238. pci_name(temp));
  239. pci_dev_put(temp);
  240. continue;
  241. }
  242. }
  243. pci_remove_bus_device(temp);
  244. /*
  245. * Ensure that no new Requests will be generated from
  246. * the device.
  247. */
  248. if (presence) {
  249. pci_read_config_word(temp, PCI_COMMAND, &command);
  250. command &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
  251. command |= PCI_COMMAND_INTX_DISABLE;
  252. pci_write_config_word(temp, PCI_COMMAND, command);
  253. }
  254. pci_dev_put(temp);
  255. }
  256. /*
  257. * Some PCI Express root ports require fixup after hot-plug operation.
  258. */
  259. if (pcie_mch_quirk)
  260. pci_fixup_device(pci_fixup_final, p_slot->ctrl->pci_dev);
  261. return rc;
  262. }