reset.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/errno.h>
  34. #include <linux/pci.h>
  35. #include <linux/delay.h>
  36. #include <linux/slab.h>
  37. #include <linux/jiffies.h>
  38. #include "mlx4.h"
  39. int mlx4_reset(struct mlx4_dev *dev)
  40. {
  41. void __iomem *reset;
  42. u32 *hca_header = NULL;
  43. int pcie_cap;
  44. u16 devctl;
  45. u16 linkctl;
  46. u16 vendor;
  47. unsigned long end;
  48. u32 sem;
  49. int i;
  50. int err = 0;
  51. #define MLX4_RESET_BASE 0xf0000
  52. #define MLX4_RESET_SIZE 0x400
  53. #define MLX4_SEM_OFFSET 0x3fc
  54. #define MLX4_RESET_OFFSET 0x10
  55. #define MLX4_RESET_VALUE swab32(1)
  56. #define MLX4_SEM_TIMEOUT_JIFFIES (10 * HZ)
  57. #define MLX4_RESET_TIMEOUT_JIFFIES (2 * HZ)
  58. /*
  59. * Reset the chip. This is somewhat ugly because we have to
  60. * save off the PCI header before reset and then restore it
  61. * after the chip reboots. We skip config space offsets 22
  62. * and 23 since those have a special meaning.
  63. */
  64. /* Do we need to save off the full 4K PCI Express header?? */
  65. hca_header = kmalloc(256, GFP_KERNEL);
  66. if (!hca_header) {
  67. err = -ENOMEM;
  68. mlx4_err(dev, "Couldn't allocate memory to save HCA PCI header, aborting\n");
  69. goto out;
  70. }
  71. pcie_cap = pci_pcie_cap(dev->persist->pdev);
  72. for (i = 0; i < 64; ++i) {
  73. if (i == 22 || i == 23)
  74. continue;
  75. if (pci_read_config_dword(dev->persist->pdev, i * 4,
  76. hca_header + i)) {
  77. err = -ENODEV;
  78. mlx4_err(dev, "Couldn't save HCA PCI header, aborting\n");
  79. goto out;
  80. }
  81. }
  82. reset = ioremap(pci_resource_start(dev->persist->pdev, 0) +
  83. MLX4_RESET_BASE,
  84. MLX4_RESET_SIZE);
  85. if (!reset) {
  86. err = -ENOMEM;
  87. mlx4_err(dev, "Couldn't map HCA reset register, aborting\n");
  88. goto out;
  89. }
  90. /* grab HW semaphore to lock out flash updates */
  91. end = jiffies + MLX4_SEM_TIMEOUT_JIFFIES;
  92. do {
  93. sem = readl(reset + MLX4_SEM_OFFSET);
  94. if (!sem)
  95. break;
  96. msleep(1);
  97. } while (time_before(jiffies, end));
  98. if (sem) {
  99. mlx4_err(dev, "Failed to obtain HW semaphore, aborting\n");
  100. err = -EAGAIN;
  101. iounmap(reset);
  102. goto out;
  103. }
  104. /* actually hit reset */
  105. writel(MLX4_RESET_VALUE, reset + MLX4_RESET_OFFSET);
  106. iounmap(reset);
  107. /* Docs say to wait one second before accessing device */
  108. msleep(1000);
  109. end = jiffies + MLX4_RESET_TIMEOUT_JIFFIES;
  110. do {
  111. if (!pci_read_config_word(dev->persist->pdev, PCI_VENDOR_ID,
  112. &vendor) && vendor != 0xffff)
  113. break;
  114. msleep(1);
  115. } while (time_before(jiffies, end));
  116. if (vendor == 0xffff) {
  117. err = -ENODEV;
  118. mlx4_err(dev, "PCI device did not come back after reset, aborting\n");
  119. goto out;
  120. }
  121. /* Now restore the PCI headers */
  122. if (pcie_cap) {
  123. devctl = hca_header[(pcie_cap + PCI_EXP_DEVCTL) / 4];
  124. if (pcie_capability_write_word(dev->persist->pdev,
  125. PCI_EXP_DEVCTL,
  126. devctl)) {
  127. err = -ENODEV;
  128. mlx4_err(dev, "Couldn't restore HCA PCI Express Device Control register, aborting\n");
  129. goto out;
  130. }
  131. linkctl = hca_header[(pcie_cap + PCI_EXP_LNKCTL) / 4];
  132. if (pcie_capability_write_word(dev->persist->pdev,
  133. PCI_EXP_LNKCTL,
  134. linkctl)) {
  135. err = -ENODEV;
  136. mlx4_err(dev, "Couldn't restore HCA PCI Express Link control register, aborting\n");
  137. goto out;
  138. }
  139. }
  140. for (i = 0; i < 16; ++i) {
  141. if (i * 4 == PCI_COMMAND)
  142. continue;
  143. if (pci_write_config_dword(dev->persist->pdev, i * 4,
  144. hca_header[i])) {
  145. err = -ENODEV;
  146. mlx4_err(dev, "Couldn't restore HCA reg %x, aborting\n",
  147. i);
  148. goto out;
  149. }
  150. }
  151. if (pci_write_config_dword(dev->persist->pdev, PCI_COMMAND,
  152. hca_header[PCI_COMMAND / 4])) {
  153. err = -ENODEV;
  154. mlx4_err(dev, "Couldn't restore HCA COMMAND, aborting\n");
  155. goto out;
  156. }
  157. out:
  158. kfree(hca_header);
  159. return err;
  160. }