df5e494a9d3887ca9b71c9cd8721c7ffdaea0d54
[linux-2.6.git] / 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/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
53 #define MTHCA_RESET_OFFSET 0xf0010
54 #define MTHCA_RESET_VALUE  swab32(1)
55
56         /*
57          * Reset the chip.  This is somewhat ugly because we have to
58          * save off the PCI header before reset and then restore it
59          * after the chip reboots.  We skip config space offsets 22
60          * and 23 since those have a special meaning.
61          *
62          * To make matters worse, for Tavor (PCI-X HCA) we have to
63          * find the associated bridge device and save off its PCI
64          * header as well.
65          */
66
67         if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE)) {
68                 /* Look for the bridge -- its device ID will be 2 more
69                    than HCA's device ID. */
70                 while ((bridge = pci_get_device(mdev->pdev->vendor,
71                                                 mdev->pdev->device + 2,
72                                                 bridge)) != NULL) {
73                         if (bridge->hdr_type    == PCI_HEADER_TYPE_BRIDGE &&
74                             bridge->subordinate == mdev->pdev->bus) {
75                                 mthca_dbg(mdev, "Found bridge: %s\n",
76                                           pci_name(bridge));
77                                 break;
78                         }
79                 }
80
81                 if (!bridge) {
82                         /*
83                          * Didn't find a bridge for a Tavor device --
84                          * assume we're in no-bridge mode and hope for
85                          * the best.
86                          */
87                         mthca_warn(mdev, "No bridge found for %s\n",
88                                   pci_name(mdev->pdev));
89                 }
90
91         }
92
93         /* For Arbel do we need to save off the full 4K PCI Express header?? */
94         hca_header = kmalloc(256, GFP_KERNEL);
95         if (!hca_header) {
96                 err = -ENOMEM;
97                 mthca_err(mdev, "Couldn't allocate memory to save HCA "
98                           "PCI header, aborting.\n");
99                 goto out;
100         }
101
102         for (i = 0; i < 64; ++i) {
103                 if (i == 22 || i == 23)
104                         continue;
105                 if (pci_read_config_dword(mdev->pdev, i * 4, hca_header + i)) {
106                         err = -ENODEV;
107                         mthca_err(mdev, "Couldn't save HCA "
108                                   "PCI header, aborting.\n");
109                         goto out;
110                 }
111         }
112
113         if (bridge) {
114                 bridge_header = kmalloc(256, GFP_KERNEL);
115                 if (!bridge_header) {
116                         err = -ENOMEM;
117                         mthca_err(mdev, "Couldn't allocate memory to save HCA "
118                                   "bridge PCI header, aborting.\n");
119                         goto out;
120                 }
121
122                 for (i = 0; i < 64; ++i) {
123                         if (i == 22 || i == 23)
124                                 continue;
125                         if (pci_read_config_dword(bridge, i * 4, bridge_header + i)) {
126                                 err = -ENODEV;
127                                 mthca_err(mdev, "Couldn't save HCA bridge "
128                                           "PCI header, aborting.\n");
129                                 goto out;
130                         }
131                 }
132         }
133
134         /* actually hit reset */
135         {
136                 void __iomem *reset = ioremap(pci_resource_start(mdev->pdev, 0) +
137                                               MTHCA_RESET_OFFSET, 4);
138
139                 if (!reset) {
140                         err = -ENOMEM;
141                         mthca_err(mdev, "Couldn't map HCA reset register, "
142                                   "aborting.\n");
143                         goto out;
144                 }
145
146                 writel(MTHCA_RESET_VALUE, reset);
147                 iounmap(reset);
148         }
149
150         /* Docs say to wait one second before accessing device */
151         msleep(1000);
152
153         /* Now wait for PCI device to start responding again */
154         {
155                 u32 v;
156                 int c = 0;
157
158                 for (c = 0; c < 100; ++c) {
159                         if (pci_read_config_dword(bridge ? bridge : mdev->pdev, 0, &v)) {
160                                 err = -ENODEV;
161                                 mthca_err(mdev, "Couldn't access HCA after reset, "
162                                           "aborting.\n");
163                                 goto out;
164                         }
165
166                         if (v != 0xffffffff)
167                                 goto good;
168
169                         msleep(100);
170                 }
171
172                 err = -ENODEV;
173                 mthca_err(mdev, "PCI device did not come back after reset, "
174                           "aborting.\n");
175                 goto out;
176         }
177
178 good:
179         /* Now restore the PCI headers */
180         if (bridge) {
181                 /*
182                  * Bridge control register is at 0x3e, so we'll
183                  * naturally restore it last in this loop.
184                  */
185                 for (i = 0; i < 16; ++i) {
186                         if (i * 4 == PCI_COMMAND)
187                                 continue;
188
189                         if (pci_write_config_dword(bridge, i * 4, bridge_header[i])) {
190                                 err = -ENODEV;
191                                 mthca_err(mdev, "Couldn't restore HCA bridge reg %x, "
192                                           "aborting.\n", i);
193                                 goto out;
194                         }
195                 }
196
197                 if (pci_write_config_dword(bridge, PCI_COMMAND,
198                                            bridge_header[PCI_COMMAND / 4])) {
199                         err = -ENODEV;
200                         mthca_err(mdev, "Couldn't restore HCA bridge COMMAND, "
201                                   "aborting.\n");
202                         goto out;
203                 }
204         }
205
206         for (i = 0; i < 16; ++i) {
207                 if (i * 4 == PCI_COMMAND)
208                         continue;
209
210                 if (pci_write_config_dword(mdev->pdev, i * 4, hca_header[i])) {
211                         err = -ENODEV;
212                         mthca_err(mdev, "Couldn't restore HCA reg %x, "
213                                   "aborting.\n", i);
214                         goto out;
215                 }
216         }
217
218         if (pci_write_config_dword(mdev->pdev, PCI_COMMAND,
219                                    hca_header[PCI_COMMAND / 4])) {
220                 err = -ENODEV;
221                 mthca_err(mdev, "Couldn't restore HCA COMMAND, "
222                           "aborting.\n");
223                 goto out;
224         }
225
226 out:
227         if (bridge)
228                 pci_dev_put(bridge);
229         kfree(bridge_header);
230         kfree(hca_header);
231
232         return err;
233 }