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