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