reset.c 4.9 KB

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