mthca_reset.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/init.h>
  33. #include <linux/errno.h>
  34. #include <linux/pci.h>
  35. #include <linux/delay.h>
  36. #include <linux/slab.h>
  37. #include "mthca_dev.h"
  38. #include "mthca_cmd.h"
  39. int mthca_reset(struct mthca_dev *mdev)
  40. {
  41. int i;
  42. int err = 0;
  43. u32 *hca_header = NULL;
  44. u32 *bridge_header = NULL;
  45. struct pci_dev *bridge = NULL;
  46. int bridge_pcix_cap = 0;
  47. int hca_pcie_cap = 0;
  48. int hca_pcix_cap = 0;
  49. u16 devctl;
  50. u16 linkctl;
  51. #define MTHCA_RESET_OFFSET 0xf0010
  52. #define MTHCA_RESET_VALUE swab32(1)
  53. /*
  54. * Reset the chip. This is somewhat ugly because we have to
  55. * save off the PCI header before reset and then restore it
  56. * after the chip reboots. We skip config space offsets 22
  57. * and 23 since those have a special meaning.
  58. *
  59. * To make matters worse, for Tavor (PCI-X HCA) we have to
  60. * find the associated bridge device and save off its PCI
  61. * header as well.
  62. */
  63. if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE)) {
  64. /* Look for the bridge -- its device ID will be 2 more
  65. than HCA's device ID. */
  66. while ((bridge = pci_get_device(mdev->pdev->vendor,
  67. mdev->pdev->device + 2,
  68. bridge)) != NULL) {
  69. if (bridge->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  70. bridge->subordinate == mdev->pdev->bus) {
  71. mthca_dbg(mdev, "Found bridge: %s\n",
  72. pci_name(bridge));
  73. break;
  74. }
  75. }
  76. if (!bridge) {
  77. /*
  78. * Didn't find a bridge for a Tavor device --
  79. * assume we're in no-bridge mode and hope for
  80. * the best.
  81. */
  82. mthca_warn(mdev, "No bridge found for %s\n",
  83. pci_name(mdev->pdev));
  84. }
  85. }
  86. /* For Arbel do we need to save off the full 4K PCI Express header?? */
  87. hca_header = kmalloc(256, GFP_KERNEL);
  88. if (!hca_header) {
  89. err = -ENOMEM;
  90. mthca_err(mdev, "Couldn't allocate memory to save HCA "
  91. "PCI header, aborting.\n");
  92. goto out;
  93. }
  94. for (i = 0; i < 64; ++i) {
  95. if (i == 22 || i == 23)
  96. continue;
  97. if (pci_read_config_dword(mdev->pdev, i * 4, hca_header + i)) {
  98. err = -ENODEV;
  99. mthca_err(mdev, "Couldn't save HCA "
  100. "PCI header, aborting.\n");
  101. goto out;
  102. }
  103. }
  104. hca_pcix_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
  105. hca_pcie_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
  106. if (bridge) {
  107. bridge_header = kmalloc(256, GFP_KERNEL);
  108. if (!bridge_header) {
  109. err = -ENOMEM;
  110. mthca_err(mdev, "Couldn't allocate memory to save HCA "
  111. "bridge PCI header, aborting.\n");
  112. goto out;
  113. }
  114. for (i = 0; i < 64; ++i) {
  115. if (i == 22 || i == 23)
  116. continue;
  117. if (pci_read_config_dword(bridge, i * 4, bridge_header + i)) {
  118. err = -ENODEV;
  119. mthca_err(mdev, "Couldn't save HCA bridge "
  120. "PCI header, aborting.\n");
  121. goto out;
  122. }
  123. }
  124. bridge_pcix_cap = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
  125. if (!bridge_pcix_cap) {
  126. err = -ENODEV;
  127. mthca_err(mdev, "Couldn't locate HCA bridge "
  128. "PCI-X capability, aborting.\n");
  129. goto out;
  130. }
  131. }
  132. /* actually hit reset */
  133. {
  134. void __iomem *reset = ioremap(pci_resource_start(mdev->pdev, 0) +
  135. MTHCA_RESET_OFFSET, 4);
  136. if (!reset) {
  137. err = -ENOMEM;
  138. mthca_err(mdev, "Couldn't map HCA reset register, "
  139. "aborting.\n");
  140. goto out;
  141. }
  142. writel(MTHCA_RESET_VALUE, reset);
  143. iounmap(reset);
  144. }
  145. /* Docs say to wait one second before accessing device */
  146. msleep(1000);
  147. /* Now wait for PCI device to start responding again */
  148. {
  149. u32 v;
  150. int c = 0;
  151. for (c = 0; c < 100; ++c) {
  152. if (pci_read_config_dword(bridge ? bridge : mdev->pdev, 0, &v)) {
  153. err = -ENODEV;
  154. mthca_err(mdev, "Couldn't access HCA after reset, "
  155. "aborting.\n");
  156. goto out;
  157. }
  158. if (v != 0xffffffff)
  159. goto good;
  160. msleep(100);
  161. }
  162. err = -ENODEV;
  163. mthca_err(mdev, "PCI device did not come back after reset, "
  164. "aborting.\n");
  165. goto out;
  166. }
  167. good:
  168. /* Now restore the PCI headers */
  169. if (bridge) {
  170. if (pci_write_config_dword(bridge, bridge_pcix_cap + 0x8,
  171. bridge_header[(bridge_pcix_cap + 0x8) / 4])) {
  172. err = -ENODEV;
  173. mthca_err(mdev, "Couldn't restore HCA bridge Upstream "
  174. "split transaction control, aborting.\n");
  175. goto out;
  176. }
  177. if (pci_write_config_dword(bridge, bridge_pcix_cap + 0xc,
  178. bridge_header[(bridge_pcix_cap + 0xc) / 4])) {
  179. err = -ENODEV;
  180. mthca_err(mdev, "Couldn't restore HCA bridge Downstream "
  181. "split transaction control, aborting.\n");
  182. goto out;
  183. }
  184. /*
  185. * Bridge control register is at 0x3e, so we'll
  186. * naturally restore it last in this loop.
  187. */
  188. for (i = 0; i < 16; ++i) {
  189. if (i * 4 == PCI_COMMAND)
  190. continue;
  191. if (pci_write_config_dword(bridge, i * 4, bridge_header[i])) {
  192. err = -ENODEV;
  193. mthca_err(mdev, "Couldn't restore HCA bridge reg %x, "
  194. "aborting.\n", i);
  195. goto out;
  196. }
  197. }
  198. if (pci_write_config_dword(bridge, PCI_COMMAND,
  199. bridge_header[PCI_COMMAND / 4])) {
  200. err = -ENODEV;
  201. mthca_err(mdev, "Couldn't restore HCA bridge COMMAND, "
  202. "aborting.\n");
  203. goto out;
  204. }
  205. }
  206. if (hca_pcix_cap) {
  207. if (pci_write_config_dword(mdev->pdev, hca_pcix_cap,
  208. hca_header[hca_pcix_cap / 4])) {
  209. err = -ENODEV;
  210. mthca_err(mdev, "Couldn't restore HCA PCI-X "
  211. "command register, aborting.\n");
  212. goto out;
  213. }
  214. }
  215. if (hca_pcie_cap) {
  216. devctl = hca_header[(hca_pcie_cap + PCI_EXP_DEVCTL) / 4];
  217. if (pci_write_config_word(mdev->pdev, hca_pcie_cap + PCI_EXP_DEVCTL,
  218. devctl)) {
  219. err = -ENODEV;
  220. mthca_err(mdev, "Couldn't restore HCA PCI Express "
  221. "Device Control register, aborting.\n");
  222. goto out;
  223. }
  224. linkctl = hca_header[(hca_pcie_cap + PCI_EXP_LNKCTL) / 4];
  225. if (pci_write_config_word(mdev->pdev, hca_pcie_cap + PCI_EXP_LNKCTL,
  226. linkctl)) {
  227. err = -ENODEV;
  228. mthca_err(mdev, "Couldn't restore HCA PCI Express "
  229. "Link control register, aborting.\n");
  230. goto out;
  231. }
  232. }
  233. for (i = 0; i < 16; ++i) {
  234. if (i * 4 == PCI_COMMAND)
  235. continue;
  236. if (pci_write_config_dword(mdev->pdev, i * 4, hca_header[i])) {
  237. err = -ENODEV;
  238. mthca_err(mdev, "Couldn't restore HCA reg %x, "
  239. "aborting.\n", i);
  240. goto out;
  241. }
  242. }
  243. if (pci_write_config_dword(mdev->pdev, PCI_COMMAND,
  244. hca_header[PCI_COMMAND / 4])) {
  245. err = -ENODEV;
  246. mthca_err(mdev, "Couldn't restore HCA COMMAND, "
  247. "aborting.\n");
  248. goto out;
  249. }
  250. out:
  251. if (bridge)
  252. pci_dev_put(bridge);
  253. kfree(bridge_header);
  254. kfree(hca_header);
  255. return err;
  256. }