2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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>
41 int mlx4_reset(struct mlx4_dev
*dev
)
44 u32
*hca_header
= NULL
;
54 #define MLX4_RESET_BASE 0xf0000
55 #define MLX4_RESET_SIZE 0x400
56 #define MLX4_SEM_OFFSET 0x3fc
57 #define MLX4_RESET_OFFSET 0x10
58 #define MLX4_RESET_VALUE swab32(1)
60 #define MLX4_SEM_TIMEOUT_JIFFIES (10 * HZ)
61 #define MLX4_RESET_TIMEOUT_JIFFIES (2 * HZ)
64 * Reset the chip. This is somewhat ugly because we have to
65 * save off the PCI header before reset and then restore it
66 * after the chip reboots. We skip config space offsets 22
67 * and 23 since those have a special meaning.
70 /* Do we need to save off the full 4K PCI Express header?? */
71 hca_header
= kmalloc(256, GFP_KERNEL
);
74 mlx4_err(dev
, "Couldn't allocate memory to save HCA "
75 "PCI header, aborting.\n");
79 pcie_cap
= pci_find_capability(dev
->pdev
, PCI_CAP_ID_EXP
);
81 for (i
= 0; i
< 64; ++i
) {
82 if (i
== 22 || i
== 23)
84 if (pci_read_config_dword(dev
->pdev
, i
* 4, hca_header
+ i
)) {
86 mlx4_err(dev
, "Couldn't save HCA "
87 "PCI header, aborting.\n");
92 reset
= ioremap(pci_resource_start(dev
->pdev
, 0) + MLX4_RESET_BASE
,
96 mlx4_err(dev
, "Couldn't map HCA reset register, aborting.\n");
100 /* grab HW semaphore to lock out flash updates */
101 end
= jiffies
+ MLX4_SEM_TIMEOUT_JIFFIES
;
103 sem
= readl(reset
+ MLX4_SEM_OFFSET
);
108 } while (time_before(jiffies
, end
));
111 mlx4_err(dev
, "Failed to obtain HW semaphore, aborting\n");
117 /* actually hit reset */
118 writel(MLX4_RESET_VALUE
, reset
+ MLX4_RESET_OFFSET
);
121 end
= jiffies
+ MLX4_RESET_TIMEOUT_JIFFIES
;
123 if (!pci_read_config_word(dev
->pdev
, PCI_VENDOR_ID
, &vendor
) &&
128 } while (time_before(jiffies
, end
));
130 if (vendor
== 0xffff) {
132 mlx4_err(dev
, "PCI device did not come back after reset, "
137 /* Now restore the PCI headers */
139 devctl
= hca_header
[(pcie_cap
+ PCI_EXP_DEVCTL
) / 4];
140 if (pci_write_config_word(dev
->pdev
, pcie_cap
+ PCI_EXP_DEVCTL
,
143 mlx4_err(dev
, "Couldn't restore HCA PCI Express "
144 "Device Control register, aborting.\n");
147 linkctl
= hca_header
[(pcie_cap
+ PCI_EXP_LNKCTL
) / 4];
148 if (pci_write_config_word(dev
->pdev
, pcie_cap
+ PCI_EXP_LNKCTL
,
151 mlx4_err(dev
, "Couldn't restore HCA PCI Express "
152 "Link control register, aborting.\n");
157 for (i
= 0; i
< 16; ++i
) {
158 if (i
* 4 == PCI_COMMAND
)
161 if (pci_write_config_dword(dev
->pdev
, i
* 4, hca_header
[i
])) {
163 mlx4_err(dev
, "Couldn't restore HCA reg %x, "
169 if (pci_write_config_dword(dev
->pdev
, PCI_COMMAND
,
170 hca_header
[PCI_COMMAND
/ 4])) {
172 mlx4_err(dev
, "Couldn't restore HCA COMMAND, "