mthca_reset.c 7.6 KB

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