PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/pci/hotplug/pciehp_pci.c

https://github.com/kipill-nn/Kernel-for-Mega
C | 295 lines | 211 code | 32 blank | 52 comment | 45 complexity | 125ad9999fdca0cc05c71308cc7a1eae MD5 | raw file
  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. warn("Rev.%d type0 record not supported\n", hpp->revision);
  40. return;
  41. }
  42. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
  43. pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
  44. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  45. if (hpp->enable_serr)
  46. pci_cmd |= PCI_COMMAND_SERR;
  47. else
  48. pci_cmd &= ~PCI_COMMAND_SERR;
  49. if (hpp->enable_perr)
  50. pci_cmd |= PCI_COMMAND_PARITY;
  51. else
  52. pci_cmd &= ~PCI_COMMAND_PARITY;
  53. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  54. /* Program bridge control value */
  55. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  56. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  57. hpp->latency_timer);
  58. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  59. if (hpp->enable_serr)
  60. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  61. else
  62. pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
  63. if (hpp->enable_perr)
  64. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  65. else
  66. pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
  67. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  68. }
  69. }
  70. static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
  71. {
  72. int pos;
  73. u16 reg16;
  74. u32 reg32;
  75. if (hpp->revision > 1) {
  76. warn("Rev.%d type2 record not supported\n", hpp->revision);
  77. return;
  78. }
  79. /* Find PCI Express capability */
  80. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  81. if (!pos)
  82. return;
  83. /* Initialize Device Control Register */
  84. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &reg16);
  85. reg16 = (reg16 & hpp->pci_exp_devctl_and) | hpp->pci_exp_devctl_or;
  86. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, reg16);
  87. /* Initialize Link Control Register */
  88. if (dev->subordinate) {
  89. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &reg16);
  90. reg16 = (reg16 & hpp->pci_exp_lnkctl_and)
  91. | hpp->pci_exp_lnkctl_or;
  92. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, reg16);
  93. }
  94. /* Find Advanced Error Reporting Enhanced Capability */
  95. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  96. if (!pos)
  97. return;
  98. /* Initialize Uncorrectable Error Mask Register */
  99. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
  100. reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
  101. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
  102. /* Initialize Uncorrectable Error Severity Register */
  103. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
  104. reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
  105. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
  106. /* Initialize Correctable Error Mask Register */
  107. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
  108. reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
  109. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
  110. /* Initialize Advanced Error Capabilities and Control Register */
  111. pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
  112. reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
  113. pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
  114. /*
  115. * FIXME: The following two registers are not supported yet.
  116. *
  117. * o Secondary Uncorrectable Error Severity Register
  118. * o Secondary Uncorrectable Error Mask Register
  119. */
  120. }
  121. static void program_fw_provided_values(struct pci_dev *dev)
  122. {
  123. struct pci_dev *cdev;
  124. struct hotplug_params hpp;
  125. /* Program hpp values for this device */
  126. if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
  127. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  128. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
  129. return;
  130. if (pciehp_get_hp_params_from_firmware(dev, &hpp)) {
  131. warn("Could not get hotplug parameters\n");
  132. return;
  133. }
  134. if (hpp.t2)
  135. program_hpp_type2(dev, hpp.t2);
  136. if (hpp.t0)
  137. program_hpp_type0(dev, hpp.t0);
  138. /* Program child devices */
  139. if (dev->subordinate) {
  140. list_for_each_entry(cdev, &dev->subordinate->devices,
  141. bus_list)
  142. program_fw_provided_values(cdev);
  143. }
  144. }
  145. static int __ref pciehp_add_bridge(struct pci_dev *dev)
  146. {
  147. struct pci_bus *parent = dev->bus;
  148. int pass, busnr, start = parent->secondary;
  149. int end = parent->subordinate;
  150. for (busnr = start; busnr <= end; busnr++) {
  151. if (!pci_find_bus(pci_domain_nr(parent), busnr))
  152. break;
  153. }
  154. if (busnr-- > end) {
  155. err("No bus number available for hot-added bridge %s\n",
  156. pci_name(dev));
  157. return -1;
  158. }
  159. for (pass = 0; pass < 2; pass++)
  160. busnr = pci_scan_bridge(parent, dev, busnr, pass);
  161. if (!dev->subordinate)
  162. return -1;
  163. pci_bus_size_bridges(dev->subordinate);
  164. pci_bus_assign_resources(parent);
  165. pci_enable_bridges(parent);
  166. pci_bus_add_devices(parent);
  167. return 0;
  168. }
  169. int pciehp_configure_device(struct slot *p_slot)
  170. {
  171. struct pci_dev *dev;
  172. struct pci_bus *parent = p_slot->ctrl->pci_dev->subordinate;
  173. int num, fn;
  174. struct controller *ctrl = p_slot->ctrl;
  175. dev = pci_get_slot(parent, PCI_DEVFN(p_slot->device, 0));
  176. if (dev) {
  177. ctrl_err(ctrl, "Device %s already exists "
  178. "at %04x:%02x:%02x, cannot hot-add\n", pci_name(dev),
  179. pci_domain_nr(parent), p_slot->bus, p_slot->device);
  180. pci_dev_put(dev);
  181. return -EINVAL;
  182. }
  183. num = pci_scan_slot(parent, PCI_DEVFN(p_slot->device, 0));
  184. if (num == 0) {
  185. ctrl_err(ctrl, "No new device found\n");
  186. return -ENODEV;
  187. }
  188. for (fn = 0; fn < 8; fn++) {
  189. dev = pci_get_slot(parent, PCI_DEVFN(p_slot->device, fn));
  190. if (!dev)
  191. continue;
  192. if ((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
  193. ctrl_err(ctrl, "Cannot hot-add display device %s\n",
  194. pci_name(dev));
  195. pci_dev_put(dev);
  196. continue;
  197. }
  198. if ((dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) ||
  199. (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)) {
  200. pciehp_add_bridge(dev);
  201. }
  202. program_fw_provided_values(dev);
  203. pci_dev_put(dev);
  204. }
  205. pci_bus_assign_resources(parent);
  206. pci_bus_add_devices(parent);
  207. return 0;
  208. }
  209. int pciehp_unconfigure_device(struct slot *p_slot)
  210. {
  211. int ret, rc = 0;
  212. int j;
  213. u8 bctl = 0;
  214. u8 presence = 0;
  215. struct pci_bus *parent = p_slot->ctrl->pci_dev->subordinate;
  216. u16 command;
  217. struct controller *ctrl = p_slot->ctrl;
  218. ctrl_dbg(ctrl, "%s: domain:bus:dev = %04x:%02x:%02x\n",
  219. __func__, pci_domain_nr(parent), p_slot->bus, 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. ctrl_err(ctrl, "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. ctrl_err(ctrl,
  238. "Cannot remove display device %s\n",
  239. pci_name(temp));
  240. pci_dev_put(temp);
  241. continue;
  242. }
  243. }
  244. pci_remove_bus_device(temp);
  245. /*
  246. * Ensure that no new Requests will be generated from
  247. * the device.
  248. */
  249. if (presence) {
  250. pci_read_config_word(temp, PCI_COMMAND, &command);
  251. command &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
  252. command |= PCI_COMMAND_INTX_DISABLE;
  253. pci_write_config_word(temp, PCI_COMMAND, command);
  254. }
  255. pci_dev_put(temp);
  256. }
  257. /*
  258. * Some PCI Express root ports require fixup after hot-plug operation.
  259. */
  260. if (pcie_mch_quirk)
  261. pci_fixup_device(pci_fixup_final, p_slot->ctrl->pci_dev);
  262. return rc;
  263. }