mthca_reset.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/errno.h>
  33. #include <linux/pci.h>
  34. #include <linux/delay.h>
  35. #include <linux/slab.h>
  36. #include "mthca_dev.h"
  37. #include "mthca_cmd.h"
  38. int mthca_reset(struct mthca_dev *mdev)
  39. {
  40. int i;
  41. int err = 0;
  42. u32 *hca_header = NULL;
  43. u32 *bridge_header = NULL;
  44. struct pci_dev *bridge = NULL;
  45. int bridge_pcix_cap = 0;
  46. int hca_pcie_cap = 0;
  47. int hca_pcix_cap = 0;
  48. u16 devctl;
  49. u16 linkctl;
  50. #define MTHCA_RESET_OFFSET 0xf0010
  51. #define MTHCA_RESET_VALUE swab32(1)
  52. /*
  53. * Reset the chip. This is somewhat ugly because we have to
  54. * save off the PCI header before reset and then restore it
  55. * after the chip reboots. We skip config space offsets 22
  56. * and 23 since those have a special meaning.
  57. *
  58. * To make matters worse, for Tavor (PCI-X HCA) we have to
  59. * find the associated bridge device and save off its PCI
  60. * header as well.
  61. */
  62. if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE)) {
  63. /* Look for the bridge -- its device ID will be 2 more
  64. than HCA's device ID. */
  65. while ((bridge = pci_get_device(mdev->pdev->vendor,
  66. mdev->pdev->device + 2,
  67. bridge)) != NULL) {
  68. if (bridge->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  69. bridge->subordinate == mdev->pdev->bus) {
  70. mthca_dbg(mdev, "Found bridge: %s\n",
  71. pci_name(bridge));
  72. break;
  73. }
  74. }
  75. if (!bridge) {
  76. /*
  77. * Didn't find a bridge for a Tavor device --
  78. * assume we're in no-bridge mode and hope for
  79. * the best.
  80. */
  81. mthca_warn(mdev, "No bridge found for %s\n",
  82. pci_name(mdev->pdev));
  83. }
  84. }
  85. /* For Arbel do we need to save off the full 4K PCI Express header?? */
  86. hca_header = kmalloc(256, GFP_KERNEL);
  87. if (!hca_header) {
  88. err = -ENOMEM;
  89. goto put_dev;
  90. }
  91. for (i = 0; i < 64; ++i) {
  92. if (i == 22 || i == 23)
  93. continue;
  94. if (pci_read_config_dword(mdev->pdev, i * 4, hca_header + i)) {
  95. err = -ENODEV;
  96. mthca_err(mdev, "Couldn't save HCA "
  97. "PCI header, aborting.\n");
  98. goto free_hca;
  99. }
  100. }
  101. hca_pcix_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
  102. hca_pcie_cap = pci_pcie_cap(mdev->pdev);
  103. if (bridge) {
  104. bridge_header = kmalloc(256, GFP_KERNEL);
  105. if (!bridge_header) {
  106. err = -ENOMEM;
  107. goto free_hca;
  108. }
  109. for (i = 0; i < 64; ++i) {
  110. if (i == 22 || i == 23)
  111. continue;
  112. if (pci_read_config_dword(bridge, i * 4, bridge_header + i)) {
  113. err = -ENODEV;
  114. mthca_err(mdev, "Couldn't save HCA bridge "
  115. "PCI header, aborting.\n");
  116. goto free_bh;
  117. }
  118. }
  119. bridge_pcix_cap = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
  120. if (!bridge_pcix_cap) {
  121. err = -ENODEV;
  122. mthca_err(mdev, "Couldn't locate HCA bridge "
  123. "PCI-X capability, aborting.\n");
  124. goto free_bh;
  125. }
  126. }
  127. /* actually hit reset */
  128. {
  129. void __iomem *reset = ioremap(pci_resource_start(mdev->pdev, 0) +
  130. MTHCA_RESET_OFFSET, 4);
  131. if (!reset) {
  132. err = -ENOMEM;
  133. mthca_err(mdev, "Couldn't map HCA reset register, "
  134. "aborting.\n");
  135. goto free_bh;
  136. }
  137. writel(MTHCA_RESET_VALUE, reset);
  138. iounmap(reset);
  139. }
  140. /* Docs say to wait one second before accessing device */
  141. msleep(1000);
  142. /* Now wait for PCI device to start responding again */
  143. {
  144. u32 v;
  145. int c = 0;
  146. for (c = 0; c < 100; ++c) {
  147. if (pci_read_config_dword(bridge ? bridge : mdev->pdev, 0, &v)) {
  148. err = -ENODEV;
  149. mthca_err(mdev, "Couldn't access HCA after reset, "
  150. "aborting.\n");
  151. goto free_bh;
  152. }
  153. if (v != 0xffffffff)
  154. goto good;
  155. msleep(100);
  156. }
  157. err = -ENODEV;
  158. mthca_err(mdev, "PCI device did not come back after reset, "
  159. "aborting.\n");
  160. goto free_bh;
  161. }
  162. good:
  163. /* Now restore the PCI headers */
  164. if (bridge) {
  165. if (pci_write_config_dword(bridge, bridge_pcix_cap + 0x8,
  166. bridge_header[(bridge_pcix_cap + 0x8) / 4])) {
  167. err = -ENODEV;
  168. mthca_err(mdev, "Couldn't restore HCA bridge Upstream "
  169. "split transaction control, aborting.\n");
  170. goto free_bh;
  171. }
  172. if (pci_write_config_dword(bridge, bridge_pcix_cap + 0xc,
  173. bridge_header[(bridge_pcix_cap + 0xc) / 4])) {
  174. err = -ENODEV;
  175. mthca_err(mdev, "Couldn't restore HCA bridge Downstream "
  176. "split transaction control, aborting.\n");
  177. goto free_bh;
  178. }
  179. /*
  180. * Bridge control register is at 0x3e, so we'll
  181. * naturally restore it last in this loop.
  182. */
  183. for (i = 0; i < 16; ++i) {
  184. if (i * 4 == PCI_COMMAND)
  185. continue;
  186. if (pci_write_config_dword(bridge, i * 4, bridge_header[i])) {
  187. err = -ENODEV;
  188. mthca_err(mdev, "Couldn't restore HCA bridge reg %x, "
  189. "aborting.\n", i);
  190. goto free_bh;
  191. }
  192. }
  193. if (pci_write_config_dword(bridge, PCI_COMMAND,
  194. bridge_header[PCI_COMMAND / 4])) {
  195. err = -ENODEV;
  196. mthca_err(mdev, "Couldn't restore HCA bridge COMMAND, "
  197. "aborting.\n");
  198. goto free_bh;
  199. }
  200. }
  201. if (hca_pcix_cap) {
  202. if (pci_write_config_dword(mdev->pdev, hca_pcix_cap,
  203. hca_header[hca_pcix_cap / 4])) {
  204. err = -ENODEV;
  205. mthca_err(mdev, "Couldn't restore HCA PCI-X "
  206. "command register, aborting.\n");
  207. goto free_bh;
  208. }
  209. }
  210. if (hca_pcie_cap) {
  211. devctl = hca_header[(hca_pcie_cap + PCI_EXP_DEVCTL) / 4];
  212. if (pcie_capability_write_word(mdev->pdev, PCI_EXP_DEVCTL,
  213. devctl)) {
  214. err = -ENODEV;
  215. mthca_err(mdev, "Couldn't restore HCA PCI Express "
  216. "Device Control register, aborting.\n");
  217. goto free_bh;
  218. }
  219. linkctl = hca_header[(hca_pcie_cap + PCI_EXP_LNKCTL) / 4];
  220. if (pcie_capability_write_word(mdev->pdev, PCI_EXP_LNKCTL,
  221. linkctl)) {
  222. err = -ENODEV;
  223. mthca_err(mdev, "Couldn't restore HCA PCI Express "
  224. "Link control register, aborting.\n");
  225. goto free_bh;
  226. }
  227. }
  228. for (i = 0; i < 16; ++i) {
  229. if (i * 4 == PCI_COMMAND)
  230. continue;
  231. if (pci_write_config_dword(mdev->pdev, i * 4, hca_header[i])) {
  232. err = -ENODEV;
  233. mthca_err(mdev, "Couldn't restore HCA reg %x, "
  234. "aborting.\n", i);
  235. goto free_bh;
  236. }
  237. }
  238. if (pci_write_config_dword(mdev->pdev, PCI_COMMAND,
  239. hca_header[PCI_COMMAND / 4])) {
  240. err = -ENODEV;
  241. mthca_err(mdev, "Couldn't restore HCA COMMAND, "
  242. "aborting.\n");
  243. }
  244. free_bh:
  245. kfree(bridge_header);
  246. free_hca:
  247. kfree(hca_header);
  248. put_dev:
  249. pci_dev_put(bridge);
  250. return err;
  251. }