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 		while ((bridge = pci_get_device(mdev->pdev->vendor,
74 						mdev->pdev->device + 2,
75 						bridge)) != NULL) {
76 			if (bridge->hdr_type    == PCI_HEADER_TYPE_BRIDGE &&
77 			    bridge->subordinate == mdev->pdev->bus) {
78 				mthca_dbg(mdev, "Found bridge: %s\n",
79 					  pci_name(bridge));
80 				break;
81 			}
82 		}
83 
84 		if (!bridge) {
85 			/*
86 			 * Didn't find a bridge for a Tavor device --
87 			 * assume we're in no-bridge mode and hope for
88 			 * the best.
89 			 */
90 			mthca_warn(mdev, "No bridge found for %s\n",
91 				  pci_name(mdev->pdev));
92 		}
93 
94 	}
95 
96 	/* For Arbel do we need to save off the full 4K PCI Express header?? */
97 	hca_header = kmalloc(256, GFP_KERNEL);
98 	if (!hca_header) {
99 		err = -ENOMEM;
100 		mthca_err(mdev, "Couldn't allocate memory to save HCA "
101 			  "PCI header, aborting.\n");
102 		goto out;
103 	}
104 
105 	for (i = 0; i < 64; ++i) {
106 		if (i == 22 || i == 23)
107 			continue;
108 		if (pci_read_config_dword(mdev->pdev, i * 4, hca_header + i)) {
109 			err = -ENODEV;
110 			mthca_err(mdev, "Couldn't save HCA "
111 				  "PCI header, aborting.\n");
112 			goto out;
113 		}
114 	}
115 
116 	hca_pcix_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
117 	hca_pcie_cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
118 
119 	if (bridge) {
120 		bridge_header = kmalloc(256, GFP_KERNEL);
121 		if (!bridge_header) {
122 			err = -ENOMEM;
123 			mthca_err(mdev, "Couldn't allocate memory to save HCA "
124 				  "bridge PCI header, aborting.\n");
125 			goto out;
126 		}
127 
128 		for (i = 0; i < 64; ++i) {
129 			if (i == 22 || i == 23)
130 				continue;
131 			if (pci_read_config_dword(bridge, i * 4, bridge_header + i)) {
132 				err = -ENODEV;
133 				mthca_err(mdev, "Couldn't save HCA bridge "
134 					  "PCI header, aborting.\n");
135 				goto out;
136 			}
137 		}
138 		bridge_pcix_cap = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
139 		if (!bridge_pcix_cap) {
140 				err = -ENODEV;
141 				mthca_err(mdev, "Couldn't locate HCA bridge "
142 					  "PCI-X capability, aborting.\n");
143 				goto out;
144 		}
145 	}
146 
147 	/* actually hit reset */
148 	{
149 		void __iomem *reset = ioremap(pci_resource_start(mdev->pdev, 0) +
150 					      MTHCA_RESET_OFFSET, 4);
151 
152 		if (!reset) {
153 			err = -ENOMEM;
154 			mthca_err(mdev, "Couldn't map HCA reset register, "
155 				  "aborting.\n");
156 			goto out;
157 		}
158 
159 		writel(MTHCA_RESET_VALUE, reset);
160 		iounmap(reset);
161 	}
162 
163 	/* Docs say to wait one second before accessing device */
164 	msleep(1000);
165 
166 	/* Now wait for PCI device to start responding again */
167 	{
168 		u32 v;
169 		int c = 0;
170 
171 		for (c = 0; c < 100; ++c) {
172 			if (pci_read_config_dword(bridge ? bridge : mdev->pdev, 0, &v)) {
173 				err = -ENODEV;
174 				mthca_err(mdev, "Couldn't access HCA after reset, "
175 					  "aborting.\n");
176 				goto out;
177 			}
178 
179 			if (v != 0xffffffff)
180 				goto good;
181 
182 			msleep(100);
183 		}
184 
185 		err = -ENODEV;
186 		mthca_err(mdev, "PCI device did not come back after reset, "
187 			  "aborting.\n");
188 		goto out;
189 	}
190 
191 good:
192 	/* Now restore the PCI headers */
193 	if (bridge) {
194 		if (pci_write_config_dword(bridge, bridge_pcix_cap + 0x8,
195 				 bridge_header[(bridge_pcix_cap + 0x8) / 4])) {
196 			err = -ENODEV;
197 			mthca_err(mdev, "Couldn't restore HCA bridge Upstream "
198 				  "split transaction control, aborting.\n");
199 			goto out;
200 		}
201 		if (pci_write_config_dword(bridge, bridge_pcix_cap + 0xc,
202 				 bridge_header[(bridge_pcix_cap + 0xc) / 4])) {
203 			err = -ENODEV;
204 			mthca_err(mdev, "Couldn't restore HCA bridge Downstream "
205 				  "split transaction control, aborting.\n");
206 			goto out;
207 		}
208 		/*
209 		 * Bridge control register is at 0x3e, so we'll
210 		 * naturally restore it last in this loop.
211 		 */
212 		for (i = 0; i < 16; ++i) {
213 			if (i * 4 == PCI_COMMAND)
214 				continue;
215 
216 			if (pci_write_config_dword(bridge, i * 4, bridge_header[i])) {
217 				err = -ENODEV;
218 				mthca_err(mdev, "Couldn't restore HCA bridge reg %x, "
219 					  "aborting.\n", i);
220 				goto out;
221 			}
222 		}
223 
224 		if (pci_write_config_dword(bridge, PCI_COMMAND,
225 					   bridge_header[PCI_COMMAND / 4])) {
226 			err = -ENODEV;
227 			mthca_err(mdev, "Couldn't restore HCA bridge COMMAND, "
228 				  "aborting.\n");
229 			goto out;
230 		}
231 	}
232 
233 	if (hca_pcix_cap) {
234 		if (pci_write_config_dword(mdev->pdev, hca_pcix_cap,
235 				 hca_header[hca_pcix_cap / 4])) {
236 			err = -ENODEV;
237 			mthca_err(mdev, "Couldn't restore HCA PCI-X "
238 				  "command register, aborting.\n");
239 			goto out;
240 		}
241 	}
242 
243 	if (hca_pcie_cap) {
244 		devctl = hca_header[(hca_pcie_cap + PCI_EXP_DEVCTL) / 4];
245 		if (pci_write_config_word(mdev->pdev, hca_pcie_cap + PCI_EXP_DEVCTL,
246 					   devctl)) {
247 			err = -ENODEV;
248 			mthca_err(mdev, "Couldn't restore HCA PCI Express "
249 				  "Device Control register, aborting.\n");
250 			goto out;
251 		}
252 		linkctl = hca_header[(hca_pcie_cap + PCI_EXP_LNKCTL) / 4];
253 		if (pci_write_config_word(mdev->pdev, hca_pcie_cap + PCI_EXP_LNKCTL,
254 					   linkctl)) {
255 			err = -ENODEV;
256 			mthca_err(mdev, "Couldn't restore HCA PCI Express "
257 				  "Link control register, aborting.\n");
258 			goto out;
259 		}
260 	}
261 
262 	for (i = 0; i < 16; ++i) {
263 		if (i * 4 == PCI_COMMAND)
264 			continue;
265 
266 		if (pci_write_config_dword(mdev->pdev, i * 4, hca_header[i])) {
267 			err = -ENODEV;
268 			mthca_err(mdev, "Couldn't restore HCA reg %x, "
269 				  "aborting.\n", i);
270 			goto out;
271 		}
272 	}
273 
274 	if (pci_write_config_dword(mdev->pdev, PCI_COMMAND,
275 				   hca_header[PCI_COMMAND / 4])) {
276 		err = -ENODEV;
277 		mthca_err(mdev, "Couldn't restore HCA COMMAND, "
278 			  "aborting.\n");
279 		goto out;
280 	}
281 
282 out:
283 	if (bridge)
284 		pci_dev_put(bridge);
285 	kfree(bridge_header);
286 	kfree(hca_header);
287 
288 	return err;
289 }
290