1*33ec1ccbSHans Petter Selasky /*
2*33ec1ccbSHans Petter Selasky * Copyright (c) 2004 Topspin Communications. All rights reserved.
3*33ec1ccbSHans Petter Selasky *
4*33ec1ccbSHans Petter Selasky * This software is available to you under a choice of one of two
5*33ec1ccbSHans Petter Selasky * licenses. You may choose to be licensed under the terms of the GNU
6*33ec1ccbSHans Petter Selasky * General Public License (GPL) Version 2, available from the file
7*33ec1ccbSHans Petter Selasky * COPYING in the main directory of this source tree, or the
8*33ec1ccbSHans Petter Selasky * OpenIB.org BSD license below:
9*33ec1ccbSHans Petter Selasky *
10*33ec1ccbSHans Petter Selasky * Redistribution and use in source and binary forms, with or
11*33ec1ccbSHans Petter Selasky * without modification, are permitted provided that the following
12*33ec1ccbSHans Petter Selasky * conditions are met:
13*33ec1ccbSHans Petter Selasky *
14*33ec1ccbSHans Petter Selasky * - Redistributions of source code must retain the above
15*33ec1ccbSHans Petter Selasky * copyright notice, this list of conditions and the following
16*33ec1ccbSHans Petter Selasky * disclaimer.
17*33ec1ccbSHans Petter Selasky *
18*33ec1ccbSHans Petter Selasky * - Redistributions in binary form must reproduce the above
19*33ec1ccbSHans Petter Selasky * copyright notice, this list of conditions and the following
20*33ec1ccbSHans Petter Selasky * disclaimer in the documentation and/or other materials
21*33ec1ccbSHans Petter Selasky * provided with the distribution.
22*33ec1ccbSHans Petter Selasky *
23*33ec1ccbSHans Petter Selasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*33ec1ccbSHans Petter Selasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*33ec1ccbSHans Petter Selasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*33ec1ccbSHans Petter Selasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*33ec1ccbSHans Petter Selasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*33ec1ccbSHans Petter Selasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*33ec1ccbSHans Petter Selasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*33ec1ccbSHans Petter Selasky * SOFTWARE.
31*33ec1ccbSHans Petter Selasky */
32*33ec1ccbSHans Petter Selasky
33*33ec1ccbSHans Petter Selasky #include <linux/errno.h>
34*33ec1ccbSHans Petter Selasky #include <linux/pci.h>
35*33ec1ccbSHans Petter Selasky #include <linux/delay.h>
36*33ec1ccbSHans Petter Selasky #include <linux/slab.h>
37*33ec1ccbSHans Petter Selasky
38*33ec1ccbSHans Petter Selasky #include "mthca_dev.h"
39*33ec1ccbSHans Petter Selasky #include "mthca_cmd.h"
40*33ec1ccbSHans Petter Selasky
mthca_reset(struct mthca_dev * mdev)41*33ec1ccbSHans Petter Selasky int mthca_reset(struct mthca_dev *mdev)
42*33ec1ccbSHans Petter Selasky {
43*33ec1ccbSHans Petter Selasky int i;
44*33ec1ccbSHans Petter Selasky int err = 0;
45*33ec1ccbSHans Petter Selasky u32 *hca_header = NULL;
46*33ec1ccbSHans Petter Selasky #ifdef __linux__
47*33ec1ccbSHans Petter Selasky u32 *bridge_header = NULL;
48*33ec1ccbSHans Petter Selasky #endif
49*33ec1ccbSHans Petter Selasky struct pci_dev *bridge = NULL;
50*33ec1ccbSHans Petter Selasky #ifdef __linux__
51*33ec1ccbSHans Petter Selasky int bridge_pcix_cap = 0;
52*33ec1ccbSHans Petter Selasky #endif
53*33ec1ccbSHans Petter Selasky int hca_pcie_cap = 0;
54*33ec1ccbSHans Petter Selasky int hca_pcix_cap = 0;
55*33ec1ccbSHans Petter Selasky
56*33ec1ccbSHans Petter Selasky u16 devctl;
57*33ec1ccbSHans Petter Selasky u16 linkctl;
58*33ec1ccbSHans Petter Selasky
59*33ec1ccbSHans Petter Selasky #define MTHCA_RESET_OFFSET 0xf0010
60*33ec1ccbSHans Petter Selasky #define MTHCA_RESET_VALUE swab32(1)
61*33ec1ccbSHans Petter Selasky
62*33ec1ccbSHans Petter Selasky /*
63*33ec1ccbSHans Petter Selasky * Reset the chip. This is somewhat ugly because we have to
64*33ec1ccbSHans Petter Selasky * save off the PCI header before reset and then restore it
65*33ec1ccbSHans Petter Selasky * after the chip reboots. We skip config space offsets 22
66*33ec1ccbSHans Petter Selasky * and 23 since those have a special meaning.
67*33ec1ccbSHans Petter Selasky *
68*33ec1ccbSHans Petter Selasky * To make matters worse, for Tavor (PCI-X HCA) we have to
69*33ec1ccbSHans Petter Selasky * find the associated bridge device and save off its PCI
70*33ec1ccbSHans Petter Selasky * header as well.
71*33ec1ccbSHans Petter Selasky */
72*33ec1ccbSHans Petter Selasky
73*33ec1ccbSHans Petter Selasky if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE)) {
74*33ec1ccbSHans Petter Selasky /* Look for the bridge -- its device ID will be 2 more
75*33ec1ccbSHans Petter Selasky than HCA's device ID. */
76*33ec1ccbSHans Petter Selasky #ifdef __linux__
77*33ec1ccbSHans Petter Selasky while ((bridge = pci_get_device(mdev->pdev->vendor,
78*33ec1ccbSHans Petter Selasky mdev->pdev->device + 2,
79*33ec1ccbSHans Petter Selasky bridge)) != NULL) {
80*33ec1ccbSHans Petter Selasky if (bridge->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
81*33ec1ccbSHans Petter Selasky bridge->subordinate == mdev->pdev->bus) {
82*33ec1ccbSHans Petter Selasky mthca_dbg(mdev, "Found bridge: %s\n",
83*33ec1ccbSHans Petter Selasky pci_name(bridge));
84*33ec1ccbSHans Petter Selasky break;
85*33ec1ccbSHans Petter Selasky }
86*33ec1ccbSHans Petter Selasky }
87*33ec1ccbSHans Petter Selasky
88*33ec1ccbSHans Petter Selasky if (!bridge) {
89*33ec1ccbSHans Petter Selasky /*
90*33ec1ccbSHans Petter Selasky * Didn't find a bridge for a Tavor device --
91*33ec1ccbSHans Petter Selasky * assume we're in no-bridge mode and hope for
92*33ec1ccbSHans Petter Selasky * the best.
93*33ec1ccbSHans Petter Selasky */
94*33ec1ccbSHans Petter Selasky mthca_warn(mdev, "No bridge found for %s\n",
95*33ec1ccbSHans Petter Selasky pci_name(mdev->pdev));
96*33ec1ccbSHans Petter Selasky }
97*33ec1ccbSHans Petter Selasky #else
98*33ec1ccbSHans Petter Selasky mthca_warn(mdev, "Reset on PCI-X is not supported.\n");
99*33ec1ccbSHans Petter Selasky goto out;
100*33ec1ccbSHans Petter Selasky
101*33ec1ccbSHans Petter Selasky #endif
102*33ec1ccbSHans Petter Selasky }
103*33ec1ccbSHans Petter Selasky
104*33ec1ccbSHans Petter Selasky /* For Arbel do we need to save off the full 4K PCI Express header?? */
105*33ec1ccbSHans Petter Selasky hca_header = kmalloc(256, GFP_KERNEL);
106*33ec1ccbSHans Petter Selasky if (!hca_header) {
107*33ec1ccbSHans Petter Selasky err = -ENOMEM;
108*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't allocate memory to save HCA "
109*33ec1ccbSHans Petter Selasky "PCI header, aborting.\n");
110*33ec1ccbSHans Petter Selasky goto out;
111*33ec1ccbSHans Petter Selasky }
112*33ec1ccbSHans Petter Selasky
113*33ec1ccbSHans Petter Selasky for (i = 0; i < 64; ++i) {
114*33ec1ccbSHans Petter Selasky if (i == 22 || i == 23)
115*33ec1ccbSHans Petter Selasky continue;
116*33ec1ccbSHans Petter Selasky if (pci_read_config_dword(mdev->pdev, i * 4, hca_header + i)) {
117*33ec1ccbSHans Petter Selasky err = -ENODEV;
118*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't save HCA "
119*33ec1ccbSHans Petter Selasky "PCI header, aborting.\n");
120*33ec1ccbSHans Petter Selasky goto out;
121*33ec1ccbSHans Petter Selasky }
122*33ec1ccbSHans Petter Selasky }
123*33ec1ccbSHans Petter Selasky
124*33ec1ccbSHans Petter Selasky hca_pcix_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
125*33ec1ccbSHans Petter Selasky hca_pcie_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
126*33ec1ccbSHans Petter Selasky
127*33ec1ccbSHans Petter Selasky #ifdef __linux__
128*33ec1ccbSHans Petter Selasky if (bridge) {
129*33ec1ccbSHans Petter Selasky bridge_header = kmalloc(256, GFP_KERNEL);
130*33ec1ccbSHans Petter Selasky if (!bridge_header) {
131*33ec1ccbSHans Petter Selasky err = -ENOMEM;
132*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't allocate memory to save HCA "
133*33ec1ccbSHans Petter Selasky "bridge PCI header, aborting.\n");
134*33ec1ccbSHans Petter Selasky goto out;
135*33ec1ccbSHans Petter Selasky }
136*33ec1ccbSHans Petter Selasky
137*33ec1ccbSHans Petter Selasky for (i = 0; i < 64; ++i) {
138*33ec1ccbSHans Petter Selasky if (i == 22 || i == 23)
139*33ec1ccbSHans Petter Selasky continue;
140*33ec1ccbSHans Petter Selasky if (pci_read_config_dword(bridge, i * 4, bridge_header + i)) {
141*33ec1ccbSHans Petter Selasky err = -ENODEV;
142*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't save HCA bridge "
143*33ec1ccbSHans Petter Selasky "PCI header, aborting.\n");
144*33ec1ccbSHans Petter Selasky goto out;
145*33ec1ccbSHans Petter Selasky }
146*33ec1ccbSHans Petter Selasky }
147*33ec1ccbSHans Petter Selasky bridge_pcix_cap = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
148*33ec1ccbSHans Petter Selasky if (!bridge_pcix_cap) {
149*33ec1ccbSHans Petter Selasky err = -ENODEV;
150*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't locate HCA bridge "
151*33ec1ccbSHans Petter Selasky "PCI-X capability, aborting.\n");
152*33ec1ccbSHans Petter Selasky goto out;
153*33ec1ccbSHans Petter Selasky }
154*33ec1ccbSHans Petter Selasky }
155*33ec1ccbSHans Petter Selasky #endif
156*33ec1ccbSHans Petter Selasky
157*33ec1ccbSHans Petter Selasky /* actually hit reset */
158*33ec1ccbSHans Petter Selasky {
159*33ec1ccbSHans Petter Selasky void __iomem *reset = ioremap(pci_resource_start(mdev->pdev, 0) +
160*33ec1ccbSHans Petter Selasky MTHCA_RESET_OFFSET, 4);
161*33ec1ccbSHans Petter Selasky
162*33ec1ccbSHans Petter Selasky if (!reset) {
163*33ec1ccbSHans Petter Selasky err = -ENOMEM;
164*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't map HCA reset register, "
165*33ec1ccbSHans Petter Selasky "aborting.\n");
166*33ec1ccbSHans Petter Selasky goto out;
167*33ec1ccbSHans Petter Selasky }
168*33ec1ccbSHans Petter Selasky
169*33ec1ccbSHans Petter Selasky writel(MTHCA_RESET_VALUE, reset);
170*33ec1ccbSHans Petter Selasky iounmap(reset);
171*33ec1ccbSHans Petter Selasky }
172*33ec1ccbSHans Petter Selasky
173*33ec1ccbSHans Petter Selasky /* Docs say to wait one second before accessing device */
174*33ec1ccbSHans Petter Selasky msleep(1000);
175*33ec1ccbSHans Petter Selasky
176*33ec1ccbSHans Petter Selasky /* Now wait for PCI device to start responding again */
177*33ec1ccbSHans Petter Selasky {
178*33ec1ccbSHans Petter Selasky u32 v;
179*33ec1ccbSHans Petter Selasky int c = 0;
180*33ec1ccbSHans Petter Selasky
181*33ec1ccbSHans Petter Selasky for (c = 0; c < 100; ++c) {
182*33ec1ccbSHans Petter Selasky if (pci_read_config_dword(bridge ? bridge : mdev->pdev, 0, &v)) {
183*33ec1ccbSHans Petter Selasky err = -ENODEV;
184*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't access HCA after reset, "
185*33ec1ccbSHans Petter Selasky "aborting.\n");
186*33ec1ccbSHans Petter Selasky goto out;
187*33ec1ccbSHans Petter Selasky }
188*33ec1ccbSHans Petter Selasky
189*33ec1ccbSHans Petter Selasky if (v != 0xffffffff)
190*33ec1ccbSHans Petter Selasky goto good;
191*33ec1ccbSHans Petter Selasky
192*33ec1ccbSHans Petter Selasky msleep(100);
193*33ec1ccbSHans Petter Selasky }
194*33ec1ccbSHans Petter Selasky
195*33ec1ccbSHans Petter Selasky err = -ENODEV;
196*33ec1ccbSHans Petter Selasky mthca_err(mdev, "PCI device did not come back after reset, "
197*33ec1ccbSHans Petter Selasky "aborting.\n");
198*33ec1ccbSHans Petter Selasky goto out;
199*33ec1ccbSHans Petter Selasky }
200*33ec1ccbSHans Petter Selasky
201*33ec1ccbSHans Petter Selasky good:
202*33ec1ccbSHans Petter Selasky #ifdef __linux__
203*33ec1ccbSHans Petter Selasky /* Now restore the PCI headers */
204*33ec1ccbSHans Petter Selasky if (bridge) {
205*33ec1ccbSHans Petter Selasky if (pci_write_config_dword(bridge, bridge_pcix_cap + 0x8,
206*33ec1ccbSHans Petter Selasky bridge_header[(bridge_pcix_cap + 0x8) / 4])) {
207*33ec1ccbSHans Petter Selasky err = -ENODEV;
208*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't restore HCA bridge Upstream "
209*33ec1ccbSHans Petter Selasky "split transaction control, aborting.\n");
210*33ec1ccbSHans Petter Selasky goto out;
211*33ec1ccbSHans Petter Selasky }
212*33ec1ccbSHans Petter Selasky if (pci_write_config_dword(bridge, bridge_pcix_cap + 0xc,
213*33ec1ccbSHans Petter Selasky bridge_header[(bridge_pcix_cap + 0xc) / 4])) {
214*33ec1ccbSHans Petter Selasky err = -ENODEV;
215*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't restore HCA bridge Downstream "
216*33ec1ccbSHans Petter Selasky "split transaction control, aborting.\n");
217*33ec1ccbSHans Petter Selasky goto out;
218*33ec1ccbSHans Petter Selasky }
219*33ec1ccbSHans Petter Selasky /*
220*33ec1ccbSHans Petter Selasky * Bridge control register is at 0x3e, so we'll
221*33ec1ccbSHans Petter Selasky * naturally restore it last in this loop.
222*33ec1ccbSHans Petter Selasky */
223*33ec1ccbSHans Petter Selasky for (i = 0; i < 16; ++i) {
224*33ec1ccbSHans Petter Selasky if (i * 4 == PCI_COMMAND)
225*33ec1ccbSHans Petter Selasky continue;
226*33ec1ccbSHans Petter Selasky
227*33ec1ccbSHans Petter Selasky if (pci_write_config_dword(bridge, i * 4, bridge_header[i])) {
228*33ec1ccbSHans Petter Selasky err = -ENODEV;
229*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't restore HCA bridge reg %x, "
230*33ec1ccbSHans Petter Selasky "aborting.\n", i);
231*33ec1ccbSHans Petter Selasky goto out;
232*33ec1ccbSHans Petter Selasky }
233*33ec1ccbSHans Petter Selasky }
234*33ec1ccbSHans Petter Selasky
235*33ec1ccbSHans Petter Selasky if (pci_write_config_dword(bridge, PCI_COMMAND,
236*33ec1ccbSHans Petter Selasky bridge_header[PCI_COMMAND / 4])) {
237*33ec1ccbSHans Petter Selasky err = -ENODEV;
238*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't restore HCA bridge COMMAND, "
239*33ec1ccbSHans Petter Selasky "aborting.\n");
240*33ec1ccbSHans Petter Selasky goto out;
241*33ec1ccbSHans Petter Selasky }
242*33ec1ccbSHans Petter Selasky }
243*33ec1ccbSHans Petter Selasky #endif
244*33ec1ccbSHans Petter Selasky
245*33ec1ccbSHans Petter Selasky if (hca_pcix_cap) {
246*33ec1ccbSHans Petter Selasky if (pci_write_config_dword(mdev->pdev, hca_pcix_cap,
247*33ec1ccbSHans Petter Selasky hca_header[hca_pcix_cap / 4])) {
248*33ec1ccbSHans Petter Selasky err = -ENODEV;
249*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't restore HCA PCI-X "
250*33ec1ccbSHans Petter Selasky "command register, aborting.\n");
251*33ec1ccbSHans Petter Selasky goto out;
252*33ec1ccbSHans Petter Selasky }
253*33ec1ccbSHans Petter Selasky }
254*33ec1ccbSHans Petter Selasky
255*33ec1ccbSHans Petter Selasky if (hca_pcie_cap) {
256*33ec1ccbSHans Petter Selasky devctl = hca_header[(hca_pcie_cap + PCI_EXP_DEVCTL) / 4];
257*33ec1ccbSHans Petter Selasky if (pci_write_config_word(mdev->pdev, hca_pcie_cap + PCI_EXP_DEVCTL,
258*33ec1ccbSHans Petter Selasky devctl)) {
259*33ec1ccbSHans Petter Selasky err = -ENODEV;
260*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't restore HCA PCI Express "
261*33ec1ccbSHans Petter Selasky "Device Control register, aborting.\n");
262*33ec1ccbSHans Petter Selasky goto out;
263*33ec1ccbSHans Petter Selasky }
264*33ec1ccbSHans Petter Selasky linkctl = hca_header[(hca_pcie_cap + PCI_EXP_LNKCTL) / 4];
265*33ec1ccbSHans Petter Selasky if (pci_write_config_word(mdev->pdev, hca_pcie_cap + PCI_EXP_LNKCTL,
266*33ec1ccbSHans Petter Selasky linkctl)) {
267*33ec1ccbSHans Petter Selasky err = -ENODEV;
268*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't restore HCA PCI Express "
269*33ec1ccbSHans Petter Selasky "Link control register, aborting.\n");
270*33ec1ccbSHans Petter Selasky goto out;
271*33ec1ccbSHans Petter Selasky }
272*33ec1ccbSHans Petter Selasky }
273*33ec1ccbSHans Petter Selasky
274*33ec1ccbSHans Petter Selasky for (i = 0; i < 16; ++i) {
275*33ec1ccbSHans Petter Selasky if (i * 4 == PCI_COMMAND)
276*33ec1ccbSHans Petter Selasky continue;
277*33ec1ccbSHans Petter Selasky
278*33ec1ccbSHans Petter Selasky if (pci_write_config_dword(mdev->pdev, i * 4, hca_header[i])) {
279*33ec1ccbSHans Petter Selasky err = -ENODEV;
280*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't restore HCA reg %x, "
281*33ec1ccbSHans Petter Selasky "aborting.\n", i);
282*33ec1ccbSHans Petter Selasky goto out;
283*33ec1ccbSHans Petter Selasky }
284*33ec1ccbSHans Petter Selasky }
285*33ec1ccbSHans Petter Selasky
286*33ec1ccbSHans Petter Selasky if (pci_write_config_dword(mdev->pdev, PCI_COMMAND,
287*33ec1ccbSHans Petter Selasky hca_header[PCI_COMMAND / 4])) {
288*33ec1ccbSHans Petter Selasky err = -ENODEV;
289*33ec1ccbSHans Petter Selasky mthca_err(mdev, "Couldn't restore HCA COMMAND, "
290*33ec1ccbSHans Petter Selasky "aborting.\n");
291*33ec1ccbSHans Petter Selasky goto out;
292*33ec1ccbSHans Petter Selasky }
293*33ec1ccbSHans Petter Selasky
294*33ec1ccbSHans Petter Selasky out:
295*33ec1ccbSHans Petter Selasky #ifdef __linux__
296*33ec1ccbSHans Petter Selasky if (bridge)
297*33ec1ccbSHans Petter Selasky pci_dev_put(bridge);
298*33ec1ccbSHans Petter Selasky kfree(bridge_header);
299*33ec1ccbSHans Petter Selasky #endif
300*33ec1ccbSHans Petter Selasky kfree(hca_header);
301*33ec1ccbSHans Petter Selasky
302*33ec1ccbSHans Petter Selasky return err;
303*33ec1ccbSHans Petter Selasky }
304