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
33 #include <linux/errno.h>
34 #include <linux/pci.h>
35 #include <linux/delay.h>
36 #include <linux/slab.h>
37
38 #include "mthca_dev.h"
39 #include "mthca_cmd.h"
40
mthca_reset(struct mthca_dev * mdev)41 int mthca_reset(struct mthca_dev *mdev)
42 {
43 int i;
44 int err = 0;
45 u32 *hca_header = NULL;
46 u32 *bridge_header = NULL;
47 struct pci_dev *bridge = NULL;
48 int bridge_pcix_cap = 0;
49 int hca_pcie_cap = 0;
50 int hca_pcix_cap = 0;
51
52 u16 devctl;
53 u16 linkctl;
54
55 #define MTHCA_RESET_OFFSET 0xf0010
56 #define MTHCA_RESET_VALUE swab32(1)
57
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 * To make matters worse, for Tavor (PCI-X HCA) we have to
65 * find the associated bridge device and save off its PCI
66 * header as well.
67 */
68
69 if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE)) {
70 /* Look for the bridge -- its device ID will be 2 more
71 than HCA's device ID. */
72 #ifdef __linux__
73 while ((bridge = pci_get_device(mdev->pdev->vendor,
74 mdev->pdev->device + 2,
75 bridge)) != NULL) {
76 if (bridge->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
77 bridge->subordinate == mdev->pdev->bus) {
78 mthca_dbg(mdev, "Found bridge: %s\n",
79 pci_name(bridge));
80 break;
81 }
82 }
83
84 if (!bridge) {
85 /*
86 * Didn't find a bridge for a Tavor device --
87 * assume we're in no-bridge mode and hope for
88 * the best.
89 */
90 mthca_warn(mdev, "No bridge found for %s\n",
91 pci_name(mdev->pdev));
92 }
93 #else
94 mthca_warn(mdev, "Reset on PCI-X is not supported.\n");
95 goto out;
96
97 #endif
98 }
99
100 /* For Arbel do we need to save off the full 4K PCI Express header?? */
101 hca_header = kmalloc(256, GFP_KERNEL);
102 if (!hca_header) {
103 err = -ENOMEM;
104 mthca_err(mdev, "Couldn't allocate memory to save HCA "
105 "PCI header, aborting.\n");
106 goto out;
107 }
108
109 for (i = 0; i < 64; ++i) {
110 if (i == 22 || i == 23)
111 continue;
112 if (pci_read_config_dword(mdev->pdev, i * 4, hca_header + i)) {
113 err = -ENODEV;
114 mthca_err(mdev, "Couldn't save HCA "
115 "PCI header, aborting.\n");
116 goto out;
117 }
118 }
119
120 hca_pcix_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
121 hca_pcie_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
122
123 #ifdef __linux__
124 if (bridge) {
125 bridge_header = kmalloc(256, GFP_KERNEL);
126 if (!bridge_header) {
127 err = -ENOMEM;
128 mthca_err(mdev, "Couldn't allocate memory to save HCA "
129 "bridge PCI header, aborting.\n");
130 goto out;
131 }
132
133 for (i = 0; i < 64; ++i) {
134 if (i == 22 || i == 23)
135 continue;
136 if (pci_read_config_dword(bridge, i * 4, bridge_header + i)) {
137 err = -ENODEV;
138 mthca_err(mdev, "Couldn't save HCA bridge "
139 "PCI header, aborting.\n");
140 goto out;
141 }
142 }
143 bridge_pcix_cap = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
144 if (!bridge_pcix_cap) {
145 err = -ENODEV;
146 mthca_err(mdev, "Couldn't locate HCA bridge "
147 "PCI-X capability, aborting.\n");
148 goto out;
149 }
150 }
151 #endif
152
153 /* actually hit reset */
154 {
155 void __iomem *reset = ioremap(pci_resource_start(mdev->pdev, 0) +
156 MTHCA_RESET_OFFSET, 4);
157
158 if (!reset) {
159 err = -ENOMEM;
160 mthca_err(mdev, "Couldn't map HCA reset register, "
161 "aborting.\n");
162 goto out;
163 }
164
165 writel(MTHCA_RESET_VALUE, reset);
166 iounmap(reset);
167 }
168
169 /* Docs say to wait one second before accessing device */
170 msleep(1000);
171
172 /* Now wait for PCI device to start responding again */
173 {
174 u32 v;
175 int c = 0;
176
177 for (c = 0; c < 100; ++c) {
178 if (pci_read_config_dword(bridge ? bridge : mdev->pdev, 0, &v)) {
179 err = -ENODEV;
180 mthca_err(mdev, "Couldn't access HCA after reset, "
181 "aborting.\n");
182 goto out;
183 }
184
185 if (v != 0xffffffff)
186 goto good;
187
188 msleep(100);
189 }
190
191 err = -ENODEV;
192 mthca_err(mdev, "PCI device did not come back after reset, "
193 "aborting.\n");
194 goto out;
195 }
196
197 good:
198 /* Now restore the PCI headers */
199 if (bridge) {
200 if (pci_write_config_dword(bridge, bridge_pcix_cap + 0x8,
201 bridge_header[(bridge_pcix_cap + 0x8) / 4])) {
202 err = -ENODEV;
203 mthca_err(mdev, "Couldn't restore HCA bridge Upstream "
204 "split transaction control, aborting.\n");
205 goto out;
206 }
207 if (pci_write_config_dword(bridge, bridge_pcix_cap + 0xc,
208 bridge_header[(bridge_pcix_cap + 0xc) / 4])) {
209 err = -ENODEV;
210 mthca_err(mdev, "Couldn't restore HCA bridge Downstream "
211 "split transaction control, aborting.\n");
212 goto out;
213 }
214 /*
215 * Bridge control register is at 0x3e, so we'll
216 * naturally restore it last in this loop.
217 */
218 for (i = 0; i < 16; ++i) {
219 if (i * 4 == PCI_COMMAND)
220 continue;
221
222 if (pci_write_config_dword(bridge, i * 4, bridge_header[i])) {
223 err = -ENODEV;
224 mthca_err(mdev, "Couldn't restore HCA bridge reg %x, "
225 "aborting.\n", i);
226 goto out;
227 }
228 }
229
230 if (pci_write_config_dword(bridge, PCI_COMMAND,
231 bridge_header[PCI_COMMAND / 4])) {
232 err = -ENODEV;
233 mthca_err(mdev, "Couldn't restore HCA bridge COMMAND, "
234 "aborting.\n");
235 goto out;
236 }
237 }
238
239 if (hca_pcix_cap) {
240 if (pci_write_config_dword(mdev->pdev, hca_pcix_cap,
241 hca_header[hca_pcix_cap / 4])) {
242 err = -ENODEV;
243 mthca_err(mdev, "Couldn't restore HCA PCI-X "
244 "command register, aborting.\n");
245 goto out;
246 }
247 }
248
249 if (hca_pcie_cap) {
250 devctl = hca_header[(hca_pcie_cap + PCI_EXP_DEVCTL) / 4];
251 if (pci_write_config_word(mdev->pdev, hca_pcie_cap + PCI_EXP_DEVCTL,
252 devctl)) {
253 err = -ENODEV;
254 mthca_err(mdev, "Couldn't restore HCA PCI Express "
255 "Device Control register, aborting.\n");
256 goto out;
257 }
258 linkctl = hca_header[(hca_pcie_cap + PCI_EXP_LNKCTL) / 4];
259 if (pci_write_config_word(mdev->pdev, hca_pcie_cap + PCI_EXP_LNKCTL,
260 linkctl)) {
261 err = -ENODEV;
262 mthca_err(mdev, "Couldn't restore HCA PCI Express "
263 "Link control register, aborting.\n");
264 goto out;
265 }
266 }
267
268 for (i = 0; i < 16; ++i) {
269 if (i * 4 == PCI_COMMAND)
270 continue;
271
272 if (pci_write_config_dword(mdev->pdev, i * 4, hca_header[i])) {
273 err = -ENODEV;
274 mthca_err(mdev, "Couldn't restore HCA reg %x, "
275 "aborting.\n", i);
276 goto out;
277 }
278 }
279
280 if (pci_write_config_dword(mdev->pdev, PCI_COMMAND,
281 hca_header[PCI_COMMAND / 4])) {
282 err = -ENODEV;
283 mthca_err(mdev, "Couldn't restore HCA COMMAND, "
284 "aborting.\n");
285 goto out;
286 }
287
288 out:
289 #ifdef __linux__
290 if (bridge)
291 pci_dev_put(bridge);
292 #endif
293 kfree(bridge_header);
294 kfree(hca_header);
295
296 return err;
297 }
298