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