reset.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. 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 "mlx4.h"
  38. int mlx4_reset(struct mlx4_dev *dev)
  39. {
  40. void __iomem *reset;
  41. u32 *hca_header = NULL;
  42. int pcie_cap;
  43. u16 devctl;
  44. u16 linkctl;
  45. u16 vendor;
  46. unsigned long end;
  47. u32 sem;
  48. int i;
  49. int err = 0;
  50. #define MLX4_RESET_BASE 0xf0000
  51. #define MLX4_RESET_SIZE 0x400
  52. #define MLX4_SEM_OFFSET 0x3fc
  53. #define MLX4_RESET_OFFSET 0x10
  54. #define MLX4_RESET_VALUE swab32(1)
  55. #define MLX4_SEM_TIMEOUT_JIFFIES (10 * HZ)
  56. #define MLX4_RESET_TIMEOUT_JIFFIES (2 * HZ)
  57. /*
  58. * Reset the chip. This is somewhat ugly because we have to
  59. * save off the PCI header before reset and then restore it
  60. * after the chip reboots. We skip config space offsets 22
  61. * and 23 since those have a special meaning.
  62. */
  63. /* Do we need to save off the full 4K PCI Express header?? */
  64. hca_header = kmalloc(256, GFP_KERNEL);
  65. if (!hca_header) {
  66. err = -ENOMEM;
  67. mlx4_err(dev, "Couldn't allocate memory to save HCA "
  68. "PCI header, aborting.\n");
  69. goto out;
  70. }
  71. pcie_cap = pci_find_capability(dev->pdev, PCI_CAP_ID_EXP);
  72. for (i = 0; i < 64; ++i) {
  73. if (i == 22 || i == 23)
  74. continue;
  75. if (pci_read_config_dword(dev->pdev, i * 4, hca_header + i)) {
  76. err = -ENODEV;
  77. mlx4_err(dev, "Couldn't save HCA "
  78. "PCI header, aborting.\n");
  79. goto out;
  80. }
  81. }
  82. reset = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_RESET_BASE,
  83. MLX4_RESET_SIZE);
  84. if (!reset) {
  85. err = -ENOMEM;
  86. mlx4_err(dev, "Couldn't map HCA reset register, aborting.\n");
  87. goto out;
  88. }
  89. /* grab HW semaphore to lock out flash updates */
  90. end = jiffies + MLX4_SEM_TIMEOUT_JIFFIES;
  91. do {
  92. sem = readl(reset + MLX4_SEM_OFFSET);
  93. if (!sem)
  94. break;
  95. msleep(1);
  96. } while (time_before(jiffies, end));
  97. if (sem) {
  98. mlx4_err(dev, "Failed to obtain HW semaphore, aborting\n");
  99. err = -EAGAIN;
  100. iounmap(reset);
  101. goto out;
  102. }
  103. /* actually hit reset */
  104. writel(MLX4_RESET_VALUE, reset + MLX4_RESET_OFFSET);
  105. iounmap(reset);
  106. end = jiffies + MLX4_RESET_TIMEOUT_JIFFIES;
  107. do {
  108. if (!pci_read_config_word(dev->pdev, PCI_VENDOR_ID, &vendor) &&
  109. vendor != 0xffff)
  110. break;
  111. msleep(1);
  112. } while (time_before(jiffies, end));
  113. if (vendor == 0xffff) {
  114. err = -ENODEV;
  115. mlx4_err(dev, "PCI device did not come back after reset, "
  116. "aborting.\n");
  117. goto out;
  118. }
  119. /* Now restore the PCI headers */
  120. if (pcie_cap) {
  121. devctl = hca_header[(pcie_cap + PCI_EXP_DEVCTL) / 4];
  122. if (pci_write_config_word(dev->pdev, pcie_cap + PCI_EXP_DEVCTL,
  123. devctl)) {
  124. err = -ENODEV;
  125. mlx4_err(dev, "Couldn't restore HCA PCI Express "
  126. "Device Control register, aborting.\n");
  127. goto out;
  128. }
  129. linkctl = hca_header[(pcie_cap + PCI_EXP_LNKCTL) / 4];
  130. if (pci_write_config_word(dev->pdev, pcie_cap + PCI_EXP_LNKCTL,
  131. linkctl)) {
  132. err = -ENODEV;
  133. mlx4_err(dev, "Couldn't restore HCA PCI Express "
  134. "Link control register, aborting.\n");
  135. goto out;
  136. }
  137. }
  138. for (i = 0; i < 16; ++i) {
  139. if (i * 4 == PCI_COMMAND)
  140. continue;
  141. if (pci_write_config_dword(dev->pdev, i * 4, hca_header[i])) {
  142. err = -ENODEV;
  143. mlx4_err(dev, "Couldn't restore HCA reg %x, "
  144. "aborting.\n", i);
  145. goto out;
  146. }
  147. }
  148. if (pci_write_config_dword(dev->pdev, PCI_COMMAND,
  149. hca_header[PCI_COMMAND / 4])) {
  150. err = -ENODEV;
  151. mlx4_err(dev, "Couldn't restore HCA COMMAND, "
  152. "aborting.\n");
  153. goto out;
  154. }
  155. out:
  156. kfree(hca_header);
  157. return err;
  158. }