mthca_reset.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "mthca_dev.h"
  40. #include "mthca_cmd.h"
  41. int mthca_reset(struct mthca_dev *mdev)
  42. {
  43. int i;
  44. int err = 0;
  45. u32 *hca_header = NULL;
  46. u32 *bridge_header = NULL;
  47. struct pci_dev *bridge = NULL;
  48. #define MTHCA_RESET_OFFSET 0xf0010
  49. #define MTHCA_RESET_VALUE swab32(1)
  50. /*
  51. * Reset the chip. This is somewhat ugly because we have to
  52. * save off the PCI header before reset and then restore it
  53. * after the chip reboots. We skip config space offsets 22
  54. * and 23 since those have a special meaning.
  55. *
  56. * To make matters worse, for Tavor (PCI-X HCA) we have to
  57. * find the associated bridge device and save off its PCI
  58. * header as well.
  59. */
  60. if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE)) {
  61. /* Look for the bridge -- its device ID will be 2 more
  62. than HCA's device ID. */
  63. while ((bridge = pci_get_device(mdev->pdev->vendor,
  64. mdev->pdev->device + 2,
  65. bridge)) != NULL) {
  66. if (bridge->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  67. bridge->subordinate == mdev->pdev->bus) {
  68. mthca_dbg(mdev, "Found bridge: %s\n",
  69. pci_name(bridge));
  70. break;
  71. }
  72. }
  73. if (!bridge) {
  74. /*
  75. * Didn't find a bridge for a Tavor device --
  76. * assume we're in no-bridge mode and hope for
  77. * the best.
  78. */
  79. mthca_warn(mdev, "No bridge found for %s\n",
  80. pci_name(mdev->pdev));
  81. }
  82. }
  83. /* For Arbel do we need to save off the full 4K PCI Express header?? */
  84. hca_header = kmalloc(256, GFP_KERNEL);
  85. if (!hca_header) {
  86. err = -ENOMEM;
  87. mthca_err(mdev, "Couldn't allocate memory to save HCA "
  88. "PCI header, aborting.\n");
  89. goto out;
  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 out;
  99. }
  100. }
  101. if (bridge) {
  102. bridge_header = kmalloc(256, GFP_KERNEL);
  103. if (!bridge_header) {
  104. err = -ENOMEM;
  105. mthca_err(mdev, "Couldn't allocate memory to save HCA "
  106. "bridge PCI header, aborting.\n");
  107. goto out;
  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 out;
  117. }
  118. }
  119. }
  120. /* actually hit reset */
  121. {
  122. void __iomem *reset = ioremap(pci_resource_start(mdev->pdev, 0) +
  123. MTHCA_RESET_OFFSET, 4);
  124. if (!reset) {
  125. err = -ENOMEM;
  126. mthca_err(mdev, "Couldn't map HCA reset register, "
  127. "aborting.\n");
  128. goto out;
  129. }
  130. writel(MTHCA_RESET_VALUE, reset);
  131. iounmap(reset);
  132. }
  133. /* Docs say to wait one second before accessing device */
  134. msleep(1000);
  135. /* Now wait for PCI device to start responding again */
  136. {
  137. u32 v;
  138. int c = 0;
  139. for (c = 0; c < 100; ++c) {
  140. if (pci_read_config_dword(bridge ? bridge : mdev->pdev, 0, &v)) {
  141. err = -ENODEV;
  142. mthca_err(mdev, "Couldn't access HCA after reset, "
  143. "aborting.\n");
  144. goto out;
  145. }
  146. if (v != 0xffffffff)
  147. goto good;
  148. msleep(100);
  149. }
  150. err = -ENODEV;
  151. mthca_err(mdev, "PCI device did not come back after reset, "
  152. "aborting.\n");
  153. goto out;
  154. }
  155. good:
  156. /* Now restore the PCI headers */
  157. if (bridge) {
  158. /*
  159. * Bridge control register is at 0x3e, so we'll
  160. * naturally restore it last in this loop.
  161. */
  162. for (i = 0; i < 16; ++i) {
  163. if (i * 4 == PCI_COMMAND)
  164. continue;
  165. if (pci_write_config_dword(bridge, i * 4, bridge_header[i])) {
  166. err = -ENODEV;
  167. mthca_err(mdev, "Couldn't restore HCA bridge reg %x, "
  168. "aborting.\n", i);
  169. goto out;
  170. }
  171. }
  172. if (pci_write_config_dword(bridge, PCI_COMMAND,
  173. bridge_header[PCI_COMMAND / 4])) {
  174. err = -ENODEV;
  175. mthca_err(mdev, "Couldn't restore HCA bridge COMMAND, "
  176. "aborting.\n");
  177. goto out;
  178. }
  179. }
  180. for (i = 0; i < 16; ++i) {
  181. if (i * 4 == PCI_COMMAND)
  182. continue;
  183. if (pci_write_config_dword(mdev->pdev, i * 4, hca_header[i])) {
  184. err = -ENODEV;
  185. mthca_err(mdev, "Couldn't restore HCA reg %x, "
  186. "aborting.\n", i);
  187. goto out;
  188. }
  189. }
  190. if (pci_write_config_dword(mdev->pdev, PCI_COMMAND,
  191. hca_header[PCI_COMMAND / 4])) {
  192. err = -ENODEV;
  193. mthca_err(mdev, "Couldn't restore HCA COMMAND, "
  194. "aborting.\n");
  195. goto out;
  196. }
  197. out:
  198. if (bridge)
  199. pci_dev_put(bridge);
  200. kfree(bridge_header);
  201. kfree(hca_header);
  202. return err;
  203. }