2 * Copyright (c) 2004 Topspin Communications. 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/errno.h>
34 #include <linux/pci.h>
35 #include <linux/delay.h>
36 #include <linux/slab.h>
38 #include "mthca_dev.h"
39 #include "mthca_cmd.h"
41 int mthca_reset(struct mthca_dev *mdev)
45 u32 *hca_header = NULL;
46 u32 *bridge_header = NULL;
47 struct pci_dev *bridge = NULL;
48 int bridge_pcix_cap = 0;
55 #define MTHCA_RESET_OFFSET 0xf0010
56 #define MTHCA_RESET_VALUE swab32(1)
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.
64 * To make matters worse, for Tavor (PCI-X HCA) we have to
65 * find the associated bridge device and save off its PCI
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 while ((bridge = pci_get_device(mdev->pdev->vendor,
73 mdev->pdev->device + 2,
75 if (bridge->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
76 bridge->subordinate == mdev->pdev->bus) {
77 mthca_dbg(mdev, "Found bridge: %s\n",
85 * Didn't find a bridge for a Tavor device --
86 * assume we're in no-bridge mode and hope for
89 mthca_warn(mdev, "No bridge found for %s\n",
90 pci_name(mdev->pdev));
95 /* For Arbel do we need to save off the full 4K PCI Express header?? */
96 hca_header = kmalloc(256, GFP_KERNEL);
102 for (i = 0; i < 64; ++i) {
103 if (i == 22 || i == 23)
105 if (pci_read_config_dword(mdev->pdev, i * 4, hca_header + i)) {
107 mthca_err(mdev, "Couldn't save HCA "
108 "PCI header, aborting.\n");
113 hca_pcix_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
114 hca_pcie_cap = pci_pcie_cap(mdev->pdev);
117 bridge_header = kmalloc(256, GFP_KERNEL);
118 if (!bridge_header) {
123 for (i = 0; i < 64; ++i) {
124 if (i == 22 || i == 23)
126 if (pci_read_config_dword(bridge, i * 4, bridge_header + i)) {
128 mthca_err(mdev, "Couldn't save HCA bridge "
129 "PCI header, aborting.\n");
133 bridge_pcix_cap = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
134 if (!bridge_pcix_cap) {
136 mthca_err(mdev, "Couldn't locate HCA bridge "
137 "PCI-X capability, aborting.\n");
142 /* actually hit reset */
144 void __iomem *reset = ioremap(pci_resource_start(mdev->pdev, 0) +
145 MTHCA_RESET_OFFSET, 4);
149 mthca_err(mdev, "Couldn't map HCA reset register, "
154 writel(MTHCA_RESET_VALUE, reset);
158 /* Docs say to wait one second before accessing device */
161 /* Now wait for PCI device to start responding again */
166 for (c = 0; c < 100; ++c) {
167 if (pci_read_config_dword(bridge ? bridge : mdev->pdev, 0, &v)) {
169 mthca_err(mdev, "Couldn't access HCA after reset, "
181 mthca_err(mdev, "PCI device did not come back after reset, "
187 /* Now restore the PCI headers */
189 if (pci_write_config_dword(bridge, bridge_pcix_cap + 0x8,
190 bridge_header[(bridge_pcix_cap + 0x8) / 4])) {
192 mthca_err(mdev, "Couldn't restore HCA bridge Upstream "
193 "split transaction control, aborting.\n");
196 if (pci_write_config_dword(bridge, bridge_pcix_cap + 0xc,
197 bridge_header[(bridge_pcix_cap + 0xc) / 4])) {
199 mthca_err(mdev, "Couldn't restore HCA bridge Downstream "
200 "split transaction control, aborting.\n");
204 * Bridge control register is at 0x3e, so we'll
205 * naturally restore it last in this loop.
207 for (i = 0; i < 16; ++i) {
208 if (i * 4 == PCI_COMMAND)
211 if (pci_write_config_dword(bridge, i * 4, bridge_header[i])) {
213 mthca_err(mdev, "Couldn't restore HCA bridge reg %x, "
219 if (pci_write_config_dword(bridge, PCI_COMMAND,
220 bridge_header[PCI_COMMAND / 4])) {
222 mthca_err(mdev, "Couldn't restore HCA bridge COMMAND, "
229 if (pci_write_config_dword(mdev->pdev, hca_pcix_cap,
230 hca_header[hca_pcix_cap / 4])) {
232 mthca_err(mdev, "Couldn't restore HCA PCI-X "
233 "command register, aborting.\n");
239 devctl = hca_header[(hca_pcie_cap + PCI_EXP_DEVCTL) / 4];
240 if (pcie_capability_write_word(mdev->pdev, PCI_EXP_DEVCTL,
243 mthca_err(mdev, "Couldn't restore HCA PCI Express "
244 "Device Control register, aborting.\n");
247 linkctl = hca_header[(hca_pcie_cap + PCI_EXP_LNKCTL) / 4];
248 if (pcie_capability_write_word(mdev->pdev, PCI_EXP_LNKCTL,
251 mthca_err(mdev, "Couldn't restore HCA PCI Express "
252 "Link control register, aborting.\n");
257 for (i = 0; i < 16; ++i) {
258 if (i * 4 == PCI_COMMAND)
261 if (pci_write_config_dword(mdev->pdev, i * 4, hca_header[i])) {
263 mthca_err(mdev, "Couldn't restore HCA reg %x, "
269 if (pci_write_config_dword(mdev->pdev, PCI_COMMAND,
270 hca_header[PCI_COMMAND / 4])) {
272 mthca_err(mdev, "Couldn't restore HCA COMMAND, "
276 kfree(bridge_header);