mthca_reset.c 5.9 KB

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