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 41 int mthca_reset(struct mthca_dev *mdev) 42 { 43 int i; 44 int err = 0; 45 u32 *hca_header = NULL; 46 #ifdef __linux__ 47 u32 *bridge_header = NULL; 48 #endif 49 struct pci_dev *bridge = NULL; 50 #ifdef __linux__ 51 int bridge_pcix_cap = 0; 52 #endif 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 #ifdef __linux__ 77 while ((bridge = pci_get_device(mdev->pdev->vendor, 78 mdev->pdev->device + 2, 79 bridge)) != NULL) { 80 if (bridge->hdr_type == PCI_HEADER_TYPE_BRIDGE && 81 bridge->subordinate == mdev->pdev->bus) { 82 mthca_dbg(mdev, "Found bridge: %s\n", 83 pci_name(bridge)); 84 break; 85 } 86 } 87 88 if (!bridge) { 89 /* 90 * Didn't find a bridge for a Tavor device -- 91 * assume we're in no-bridge mode and hope for 92 * the best. 93 */ 94 mthca_warn(mdev, "No bridge found for %s\n", 95 pci_name(mdev->pdev)); 96 } 97 #else 98 mthca_warn(mdev, "Reset on PCI-X is not supported.\n"); 99 goto out; 100 101 #endif 102 } 103 104 /* For Arbel do we need to save off the full 4K PCI Express header?? */ 105 hca_header = kmalloc(256, GFP_KERNEL); 106 if (!hca_header) { 107 err = -ENOMEM; 108 goto out; 109 } 110 111 for (i = 0; i < 64; ++i) { 112 if (i == 22 || i == 23) 113 continue; 114 if (pci_read_config_dword(mdev->pdev, i * 4, hca_header + i)) { 115 err = -ENODEV; 116 mthca_err(mdev, "Couldn't save HCA " 117 "PCI header, aborting.\n"); 118 goto out; 119 } 120 } 121 122 hca_pcix_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX); 123 hca_pcie_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP); 124 125 #ifdef __linux__ 126 if (bridge) { 127 bridge_header = kmalloc(256, GFP_KERNEL); 128 if (!bridge_header) { 129 err = -ENOMEM; 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 #ifdef __linux__ 199 /* Now restore the PCI headers */ 200 if (bridge) { 201 if (pci_write_config_dword(bridge, bridge_pcix_cap + 0x8, 202 bridge_header[(bridge_pcix_cap + 0x8) / 4])) { 203 err = -ENODEV; 204 mthca_err(mdev, "Couldn't restore HCA bridge Upstream " 205 "split transaction control, aborting.\n"); 206 goto out; 207 } 208 if (pci_write_config_dword(bridge, bridge_pcix_cap + 0xc, 209 bridge_header[(bridge_pcix_cap + 0xc) / 4])) { 210 err = -ENODEV; 211 mthca_err(mdev, "Couldn't restore HCA bridge Downstream " 212 "split transaction control, aborting.\n"); 213 goto out; 214 } 215 /* 216 * Bridge control register is at 0x3e, so we'll 217 * naturally restore it last in this loop. 218 */ 219 for (i = 0; i < 16; ++i) { 220 if (i * 4 == PCI_COMMAND) 221 continue; 222 223 if (pci_write_config_dword(bridge, i * 4, bridge_header[i])) { 224 err = -ENODEV; 225 mthca_err(mdev, "Couldn't restore HCA bridge reg %x, " 226 "aborting.\n", i); 227 goto out; 228 } 229 } 230 231 if (pci_write_config_dword(bridge, PCI_COMMAND, 232 bridge_header[PCI_COMMAND / 4])) { 233 err = -ENODEV; 234 mthca_err(mdev, "Couldn't restore HCA bridge COMMAND, " 235 "aborting.\n"); 236 goto out; 237 } 238 } 239 #endif 240 241 if (hca_pcix_cap) { 242 if (pci_write_config_dword(mdev->pdev, hca_pcix_cap, 243 hca_header[hca_pcix_cap / 4])) { 244 err = -ENODEV; 245 mthca_err(mdev, "Couldn't restore HCA PCI-X " 246 "command register, aborting.\n"); 247 goto out; 248 } 249 } 250 251 if (hca_pcie_cap) { 252 devctl = hca_header[(hca_pcie_cap + PCI_EXP_DEVCTL) / 4]; 253 if (pci_write_config_word(mdev->pdev, hca_pcie_cap + PCI_EXP_DEVCTL, 254 devctl)) { 255 err = -ENODEV; 256 mthca_err(mdev, "Couldn't restore HCA PCI Express " 257 "Device Control register, aborting.\n"); 258 goto out; 259 } 260 linkctl = hca_header[(hca_pcie_cap + PCI_EXP_LNKCTL) / 4]; 261 if (pci_write_config_word(mdev->pdev, hca_pcie_cap + PCI_EXP_LNKCTL, 262 linkctl)) { 263 err = -ENODEV; 264 mthca_err(mdev, "Couldn't restore HCA PCI Express " 265 "Link control register, aborting.\n"); 266 goto out; 267 } 268 } 269 270 for (i = 0; i < 16; ++i) { 271 if (i * 4 == PCI_COMMAND) 272 continue; 273 274 if (pci_write_config_dword(mdev->pdev, i * 4, hca_header[i])) { 275 err = -ENODEV; 276 mthca_err(mdev, "Couldn't restore HCA reg %x, " 277 "aborting.\n", i); 278 goto out; 279 } 280 } 281 282 if (pci_write_config_dword(mdev->pdev, PCI_COMMAND, 283 hca_header[PCI_COMMAND / 4])) { 284 err = -ENODEV; 285 mthca_err(mdev, "Couldn't restore HCA COMMAND, " 286 "aborting.\n"); 287 goto out; 288 } 289 290 out: 291 #ifdef __linux__ 292 if (bridge) 293 pci_dev_put(bridge); 294 kfree(bridge_header); 295 #endif 296 kfree(hca_header); 297 298 return err; 299 } 300