xref: /OK3568_Linux_fs/kernel/drivers/infiniband/hw/hfi1/pcie.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright(c) 2015 - 2019 Intel Corporation.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
5*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*4882a593Smuzhiyun  * General Public License for more details.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * BSD LICENSE
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
21*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
22*4882a593Smuzhiyun  * are met:
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *  - Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun  *  - Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun  *    the documentation and/or other materials provided with the
29*4882a593Smuzhiyun  *    distribution.
30*4882a593Smuzhiyun  *  - Neither the name of Intel Corporation nor the names of its
31*4882a593Smuzhiyun  *    contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun  *    from this software without specific prior written permission.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <linux/pci.h>
49*4882a593Smuzhiyun #include <linux/io.h>
50*4882a593Smuzhiyun #include <linux/delay.h>
51*4882a593Smuzhiyun #include <linux/vmalloc.h>
52*4882a593Smuzhiyun #include <linux/aer.h>
53*4882a593Smuzhiyun #include <linux/module.h>
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #include "hfi.h"
56*4882a593Smuzhiyun #include "chip_registers.h"
57*4882a593Smuzhiyun #include "aspm.h"
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  * This file contains PCIe utility routines.
61*4882a593Smuzhiyun  */
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun  * Do all the common PCIe setup and initialization.
65*4882a593Smuzhiyun  */
hfi1_pcie_init(struct hfi1_devdata * dd)66*4882a593Smuzhiyun int hfi1_pcie_init(struct hfi1_devdata *dd)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	int ret;
69*4882a593Smuzhiyun 	struct pci_dev *pdev = dd->pcidev;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	ret = pci_enable_device(pdev);
72*4882a593Smuzhiyun 	if (ret) {
73*4882a593Smuzhiyun 		/*
74*4882a593Smuzhiyun 		 * This can happen (in theory) iff:
75*4882a593Smuzhiyun 		 * We did a chip reset, and then failed to reprogram the
76*4882a593Smuzhiyun 		 * BAR, or the chip reset due to an internal error.  We then
77*4882a593Smuzhiyun 		 * unloaded the driver and reloaded it.
78*4882a593Smuzhiyun 		 *
79*4882a593Smuzhiyun 		 * Both reset cases set the BAR back to initial state.  For
80*4882a593Smuzhiyun 		 * the latter case, the AER sticky error bit at offset 0x718
81*4882a593Smuzhiyun 		 * should be set, but the Linux kernel doesn't yet know
82*4882a593Smuzhiyun 		 * about that, it appears.  If the original BAR was retained
83*4882a593Smuzhiyun 		 * in the kernel data structures, this may be OK.
84*4882a593Smuzhiyun 		 */
85*4882a593Smuzhiyun 		dd_dev_err(dd, "pci enable failed: error %d\n", -ret);
86*4882a593Smuzhiyun 		return ret;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	ret = pci_request_regions(pdev, DRIVER_NAME);
90*4882a593Smuzhiyun 	if (ret) {
91*4882a593Smuzhiyun 		dd_dev_err(dd, "pci_request_regions fails: err %d\n", -ret);
92*4882a593Smuzhiyun 		goto bail;
93*4882a593Smuzhiyun 	}
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
96*4882a593Smuzhiyun 	if (ret) {
97*4882a593Smuzhiyun 		/*
98*4882a593Smuzhiyun 		 * If the 64 bit setup fails, try 32 bit.  Some systems
99*4882a593Smuzhiyun 		 * do not setup 64 bit maps on systems with 2GB or less
100*4882a593Smuzhiyun 		 * memory installed.
101*4882a593Smuzhiyun 		 */
102*4882a593Smuzhiyun 		ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
103*4882a593Smuzhiyun 		if (ret) {
104*4882a593Smuzhiyun 			dd_dev_err(dd, "Unable to set DMA mask: %d\n", ret);
105*4882a593Smuzhiyun 			goto bail;
106*4882a593Smuzhiyun 		}
107*4882a593Smuzhiyun 		ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
108*4882a593Smuzhiyun 	} else {
109*4882a593Smuzhiyun 		ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 	if (ret) {
112*4882a593Smuzhiyun 		dd_dev_err(dd, "Unable to set DMA consistent mask: %d\n", ret);
113*4882a593Smuzhiyun 		goto bail;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	pci_set_master(pdev);
117*4882a593Smuzhiyun 	(void)pci_enable_pcie_error_reporting(pdev);
118*4882a593Smuzhiyun 	return 0;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun bail:
121*4882a593Smuzhiyun 	hfi1_pcie_cleanup(pdev);
122*4882a593Smuzhiyun 	return ret;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /*
126*4882a593Smuzhiyun  * Clean what was done in hfi1_pcie_init()
127*4882a593Smuzhiyun  */
hfi1_pcie_cleanup(struct pci_dev * pdev)128*4882a593Smuzhiyun void hfi1_pcie_cleanup(struct pci_dev *pdev)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	pci_disable_device(pdev);
131*4882a593Smuzhiyun 	/*
132*4882a593Smuzhiyun 	 * Release regions should be called after the disable. OK to
133*4882a593Smuzhiyun 	 * call if request regions has not been called or failed.
134*4882a593Smuzhiyun 	 */
135*4882a593Smuzhiyun 	pci_release_regions(pdev);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun  * Do remaining PCIe setup, once dd is allocated, and save away
140*4882a593Smuzhiyun  * fields required to re-initialize after a chip reset, or for
141*4882a593Smuzhiyun  * various other purposes
142*4882a593Smuzhiyun  */
hfi1_pcie_ddinit(struct hfi1_devdata * dd,struct pci_dev * pdev)143*4882a593Smuzhiyun int hfi1_pcie_ddinit(struct hfi1_devdata *dd, struct pci_dev *pdev)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	unsigned long len;
146*4882a593Smuzhiyun 	resource_size_t addr;
147*4882a593Smuzhiyun 	int ret = 0;
148*4882a593Smuzhiyun 	u32 rcv_array_count;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	addr = pci_resource_start(pdev, 0);
151*4882a593Smuzhiyun 	len = pci_resource_len(pdev, 0);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	/*
154*4882a593Smuzhiyun 	 * The TXE PIO buffers are at the tail end of the chip space.
155*4882a593Smuzhiyun 	 * Cut them off and map them separately.
156*4882a593Smuzhiyun 	 */
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	/* sanity check vs expectations */
159*4882a593Smuzhiyun 	if (len != TXE_PIO_SEND + TXE_PIO_SIZE) {
160*4882a593Smuzhiyun 		dd_dev_err(dd, "chip PIO range does not match\n");
161*4882a593Smuzhiyun 		return -EINVAL;
162*4882a593Smuzhiyun 	}
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	dd->kregbase1 = ioremap(addr, RCV_ARRAY);
165*4882a593Smuzhiyun 	if (!dd->kregbase1) {
166*4882a593Smuzhiyun 		dd_dev_err(dd, "UC mapping of kregbase1 failed\n");
167*4882a593Smuzhiyun 		return -ENOMEM;
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 	dd_dev_info(dd, "UC base1: %p for %x\n", dd->kregbase1, RCV_ARRAY);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/* verify that reads actually work, save revision for reset check */
172*4882a593Smuzhiyun 	dd->revision = readq(dd->kregbase1 + CCE_REVISION);
173*4882a593Smuzhiyun 	if (dd->revision == ~(u64)0) {
174*4882a593Smuzhiyun 		dd_dev_err(dd, "Cannot read chip CSRs\n");
175*4882a593Smuzhiyun 		goto nomem;
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	rcv_array_count = readq(dd->kregbase1 + RCV_ARRAY_CNT);
179*4882a593Smuzhiyun 	dd_dev_info(dd, "RcvArray count: %u\n", rcv_array_count);
180*4882a593Smuzhiyun 	dd->base2_start  = RCV_ARRAY + rcv_array_count * 8;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	dd->kregbase2 = ioremap(
183*4882a593Smuzhiyun 		addr + dd->base2_start,
184*4882a593Smuzhiyun 		TXE_PIO_SEND - dd->base2_start);
185*4882a593Smuzhiyun 	if (!dd->kregbase2) {
186*4882a593Smuzhiyun 		dd_dev_err(dd, "UC mapping of kregbase2 failed\n");
187*4882a593Smuzhiyun 		goto nomem;
188*4882a593Smuzhiyun 	}
189*4882a593Smuzhiyun 	dd_dev_info(dd, "UC base2: %p for %x\n", dd->kregbase2,
190*4882a593Smuzhiyun 		    TXE_PIO_SEND - dd->base2_start);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	dd->piobase = ioremap_wc(addr + TXE_PIO_SEND, TXE_PIO_SIZE);
193*4882a593Smuzhiyun 	if (!dd->piobase) {
194*4882a593Smuzhiyun 		dd_dev_err(dd, "WC mapping of send buffers failed\n");
195*4882a593Smuzhiyun 		goto nomem;
196*4882a593Smuzhiyun 	}
197*4882a593Smuzhiyun 	dd_dev_info(dd, "WC piobase: %p for %x\n", dd->piobase, TXE_PIO_SIZE);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	dd->physaddr = addr;        /* used for io_remap, etc. */
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	/*
202*4882a593Smuzhiyun 	 * Map the chip's RcvArray as write-combining to allow us
203*4882a593Smuzhiyun 	 * to write an entire cacheline worth of entries in one shot.
204*4882a593Smuzhiyun 	 */
205*4882a593Smuzhiyun 	dd->rcvarray_wc = ioremap_wc(addr + RCV_ARRAY,
206*4882a593Smuzhiyun 				     rcv_array_count * 8);
207*4882a593Smuzhiyun 	if (!dd->rcvarray_wc) {
208*4882a593Smuzhiyun 		dd_dev_err(dd, "WC mapping of receive array failed\n");
209*4882a593Smuzhiyun 		goto nomem;
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 	dd_dev_info(dd, "WC RcvArray: %p for %x\n",
212*4882a593Smuzhiyun 		    dd->rcvarray_wc, rcv_array_count * 8);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	dd->flags |= HFI1_PRESENT;	/* chip.c CSR routines now work */
215*4882a593Smuzhiyun 	return 0;
216*4882a593Smuzhiyun nomem:
217*4882a593Smuzhiyun 	ret = -ENOMEM;
218*4882a593Smuzhiyun 	hfi1_pcie_ddcleanup(dd);
219*4882a593Smuzhiyun 	return ret;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun /*
223*4882a593Smuzhiyun  * Do PCIe cleanup related to dd, after chip-specific cleanup, etc.  Just prior
224*4882a593Smuzhiyun  * to releasing the dd memory.
225*4882a593Smuzhiyun  * Void because all of the core pcie cleanup functions are void.
226*4882a593Smuzhiyun  */
hfi1_pcie_ddcleanup(struct hfi1_devdata * dd)227*4882a593Smuzhiyun void hfi1_pcie_ddcleanup(struct hfi1_devdata *dd)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	dd->flags &= ~HFI1_PRESENT;
230*4882a593Smuzhiyun 	if (dd->kregbase1)
231*4882a593Smuzhiyun 		iounmap(dd->kregbase1);
232*4882a593Smuzhiyun 	dd->kregbase1 = NULL;
233*4882a593Smuzhiyun 	if (dd->kregbase2)
234*4882a593Smuzhiyun 		iounmap(dd->kregbase2);
235*4882a593Smuzhiyun 	dd->kregbase2 = NULL;
236*4882a593Smuzhiyun 	if (dd->rcvarray_wc)
237*4882a593Smuzhiyun 		iounmap(dd->rcvarray_wc);
238*4882a593Smuzhiyun 	dd->rcvarray_wc = NULL;
239*4882a593Smuzhiyun 	if (dd->piobase)
240*4882a593Smuzhiyun 		iounmap(dd->piobase);
241*4882a593Smuzhiyun 	dd->piobase = NULL;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun /* return the PCIe link speed from the given link status */
extract_speed(u16 linkstat)245*4882a593Smuzhiyun static u32 extract_speed(u16 linkstat)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun 	u32 speed;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	switch (linkstat & PCI_EXP_LNKSTA_CLS) {
250*4882a593Smuzhiyun 	default: /* not defined, assume Gen1 */
251*4882a593Smuzhiyun 	case PCI_EXP_LNKSTA_CLS_2_5GB:
252*4882a593Smuzhiyun 		speed = 2500; /* Gen 1, 2.5GHz */
253*4882a593Smuzhiyun 		break;
254*4882a593Smuzhiyun 	case PCI_EXP_LNKSTA_CLS_5_0GB:
255*4882a593Smuzhiyun 		speed = 5000; /* Gen 2, 5GHz */
256*4882a593Smuzhiyun 		break;
257*4882a593Smuzhiyun 	case PCI_EXP_LNKSTA_CLS_8_0GB:
258*4882a593Smuzhiyun 		speed = 8000; /* Gen 3, 8GHz */
259*4882a593Smuzhiyun 		break;
260*4882a593Smuzhiyun 	}
261*4882a593Smuzhiyun 	return speed;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun /* return the PCIe link speed from the given link status */
extract_width(u16 linkstat)265*4882a593Smuzhiyun static u32 extract_width(u16 linkstat)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	return (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /* read the link status and set dd->{lbus_width,lbus_speed,lbus_info} */
update_lbus_info(struct hfi1_devdata * dd)271*4882a593Smuzhiyun static void update_lbus_info(struct hfi1_devdata *dd)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	u16 linkstat;
274*4882a593Smuzhiyun 	int ret;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	ret = pcie_capability_read_word(dd->pcidev, PCI_EXP_LNKSTA, &linkstat);
277*4882a593Smuzhiyun 	if (ret) {
278*4882a593Smuzhiyun 		dd_dev_err(dd, "Unable to read from PCI config\n");
279*4882a593Smuzhiyun 		return;
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	dd->lbus_width = extract_width(linkstat);
283*4882a593Smuzhiyun 	dd->lbus_speed = extract_speed(linkstat);
284*4882a593Smuzhiyun 	snprintf(dd->lbus_info, sizeof(dd->lbus_info),
285*4882a593Smuzhiyun 		 "PCIe,%uMHz,x%u", dd->lbus_speed, dd->lbus_width);
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun /*
289*4882a593Smuzhiyun  * Read in the current PCIe link width and speed.  Find if the link is
290*4882a593Smuzhiyun  * Gen3 capable.
291*4882a593Smuzhiyun  */
pcie_speeds(struct hfi1_devdata * dd)292*4882a593Smuzhiyun int pcie_speeds(struct hfi1_devdata *dd)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun 	u32 linkcap;
295*4882a593Smuzhiyun 	struct pci_dev *parent = dd->pcidev->bus->self;
296*4882a593Smuzhiyun 	int ret;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (!pci_is_pcie(dd->pcidev)) {
299*4882a593Smuzhiyun 		dd_dev_err(dd, "Can't find PCI Express capability!\n");
300*4882a593Smuzhiyun 		return -EINVAL;
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	/* find if our max speed is Gen3 and parent supports Gen3 speeds */
304*4882a593Smuzhiyun 	dd->link_gen3_capable = 1;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	ret = pcie_capability_read_dword(dd->pcidev, PCI_EXP_LNKCAP, &linkcap);
307*4882a593Smuzhiyun 	if (ret) {
308*4882a593Smuzhiyun 		dd_dev_err(dd, "Unable to read from PCI config\n");
309*4882a593Smuzhiyun 		return pcibios_err_to_errno(ret);
310*4882a593Smuzhiyun 	}
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	if ((linkcap & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_8_0GB) {
313*4882a593Smuzhiyun 		dd_dev_info(dd,
314*4882a593Smuzhiyun 			    "This HFI is not Gen3 capable, max speed 0x%x, need 0x3\n",
315*4882a593Smuzhiyun 			    linkcap & PCI_EXP_LNKCAP_SLS);
316*4882a593Smuzhiyun 		dd->link_gen3_capable = 0;
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	/*
320*4882a593Smuzhiyun 	 * bus->max_bus_speed is set from the bridge's linkcap Max Link Speed
321*4882a593Smuzhiyun 	 */
322*4882a593Smuzhiyun 	if (parent &&
323*4882a593Smuzhiyun 	    (dd->pcidev->bus->max_bus_speed == PCIE_SPEED_2_5GT ||
324*4882a593Smuzhiyun 	     dd->pcidev->bus->max_bus_speed == PCIE_SPEED_5_0GT)) {
325*4882a593Smuzhiyun 		dd_dev_info(dd, "Parent PCIe bridge does not support Gen3\n");
326*4882a593Smuzhiyun 		dd->link_gen3_capable = 0;
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	/* obtain the link width and current speed */
330*4882a593Smuzhiyun 	update_lbus_info(dd);
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	dd_dev_info(dd, "%s\n", dd->lbus_info);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	return 0;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun /**
338*4882a593Smuzhiyun  * Restore command and BARs after a reset has wiped them out
339*4882a593Smuzhiyun  *
340*4882a593Smuzhiyun  * Returns 0 on success, otherwise a negative error value
341*4882a593Smuzhiyun  */
restore_pci_variables(struct hfi1_devdata * dd)342*4882a593Smuzhiyun int restore_pci_variables(struct hfi1_devdata *dd)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	int ret;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	ret = pci_write_config_word(dd->pcidev, PCI_COMMAND, dd->pci_command);
347*4882a593Smuzhiyun 	if (ret)
348*4882a593Smuzhiyun 		goto error;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	ret = pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_0,
351*4882a593Smuzhiyun 				     dd->pcibar0);
352*4882a593Smuzhiyun 	if (ret)
353*4882a593Smuzhiyun 		goto error;
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	ret = pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_1,
356*4882a593Smuzhiyun 				     dd->pcibar1);
357*4882a593Smuzhiyun 	if (ret)
358*4882a593Smuzhiyun 		goto error;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	ret = pci_write_config_dword(dd->pcidev, PCI_ROM_ADDRESS, dd->pci_rom);
361*4882a593Smuzhiyun 	if (ret)
362*4882a593Smuzhiyun 		goto error;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	ret = pcie_capability_write_word(dd->pcidev, PCI_EXP_DEVCTL,
365*4882a593Smuzhiyun 					 dd->pcie_devctl);
366*4882a593Smuzhiyun 	if (ret)
367*4882a593Smuzhiyun 		goto error;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	ret = pcie_capability_write_word(dd->pcidev, PCI_EXP_LNKCTL,
370*4882a593Smuzhiyun 					 dd->pcie_lnkctl);
371*4882a593Smuzhiyun 	if (ret)
372*4882a593Smuzhiyun 		goto error;
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	ret = pcie_capability_write_word(dd->pcidev, PCI_EXP_DEVCTL2,
375*4882a593Smuzhiyun 					 dd->pcie_devctl2);
376*4882a593Smuzhiyun 	if (ret)
377*4882a593Smuzhiyun 		goto error;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	ret = pci_write_config_dword(dd->pcidev, PCI_CFG_MSIX0, dd->pci_msix0);
380*4882a593Smuzhiyun 	if (ret)
381*4882a593Smuzhiyun 		goto error;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	if (pci_find_ext_capability(dd->pcidev, PCI_EXT_CAP_ID_TPH)) {
384*4882a593Smuzhiyun 		ret = pci_write_config_dword(dd->pcidev, PCIE_CFG_TPH2,
385*4882a593Smuzhiyun 					     dd->pci_tph2);
386*4882a593Smuzhiyun 		if (ret)
387*4882a593Smuzhiyun 			goto error;
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun 	return 0;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun error:
392*4882a593Smuzhiyun 	dd_dev_err(dd, "Unable to write to PCI config\n");
393*4882a593Smuzhiyun 	return pcibios_err_to_errno(ret);
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun /**
397*4882a593Smuzhiyun  * Save BARs and command to rewrite after device reset
398*4882a593Smuzhiyun  *
399*4882a593Smuzhiyun  * Returns 0 on success, otherwise a negative error value
400*4882a593Smuzhiyun  */
save_pci_variables(struct hfi1_devdata * dd)401*4882a593Smuzhiyun int save_pci_variables(struct hfi1_devdata *dd)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	int ret;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	ret = pci_read_config_dword(dd->pcidev, PCI_BASE_ADDRESS_0,
406*4882a593Smuzhiyun 				    &dd->pcibar0);
407*4882a593Smuzhiyun 	if (ret)
408*4882a593Smuzhiyun 		goto error;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	ret = pci_read_config_dword(dd->pcidev, PCI_BASE_ADDRESS_1,
411*4882a593Smuzhiyun 				    &dd->pcibar1);
412*4882a593Smuzhiyun 	if (ret)
413*4882a593Smuzhiyun 		goto error;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	ret = pci_read_config_dword(dd->pcidev, PCI_ROM_ADDRESS, &dd->pci_rom);
416*4882a593Smuzhiyun 	if (ret)
417*4882a593Smuzhiyun 		goto error;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	ret = pci_read_config_word(dd->pcidev, PCI_COMMAND, &dd->pci_command);
420*4882a593Smuzhiyun 	if (ret)
421*4882a593Smuzhiyun 		goto error;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	ret = pcie_capability_read_word(dd->pcidev, PCI_EXP_DEVCTL,
424*4882a593Smuzhiyun 					&dd->pcie_devctl);
425*4882a593Smuzhiyun 	if (ret)
426*4882a593Smuzhiyun 		goto error;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	ret = pcie_capability_read_word(dd->pcidev, PCI_EXP_LNKCTL,
429*4882a593Smuzhiyun 					&dd->pcie_lnkctl);
430*4882a593Smuzhiyun 	if (ret)
431*4882a593Smuzhiyun 		goto error;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	ret = pcie_capability_read_word(dd->pcidev, PCI_EXP_DEVCTL2,
434*4882a593Smuzhiyun 					&dd->pcie_devctl2);
435*4882a593Smuzhiyun 	if (ret)
436*4882a593Smuzhiyun 		goto error;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	ret = pci_read_config_dword(dd->pcidev, PCI_CFG_MSIX0, &dd->pci_msix0);
439*4882a593Smuzhiyun 	if (ret)
440*4882a593Smuzhiyun 		goto error;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	if (pci_find_ext_capability(dd->pcidev, PCI_EXT_CAP_ID_TPH)) {
443*4882a593Smuzhiyun 		ret = pci_read_config_dword(dd->pcidev, PCIE_CFG_TPH2,
444*4882a593Smuzhiyun 					    &dd->pci_tph2);
445*4882a593Smuzhiyun 		if (ret)
446*4882a593Smuzhiyun 			goto error;
447*4882a593Smuzhiyun 	}
448*4882a593Smuzhiyun 	return 0;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun error:
451*4882a593Smuzhiyun 	dd_dev_err(dd, "Unable to read from PCI config\n");
452*4882a593Smuzhiyun 	return pcibios_err_to_errno(ret);
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun /*
456*4882a593Smuzhiyun  * BIOS may not set PCIe bus-utilization parameters for best performance.
457*4882a593Smuzhiyun  * Check and optionally adjust them to maximize our throughput.
458*4882a593Smuzhiyun  */
459*4882a593Smuzhiyun static int hfi1_pcie_caps;
460*4882a593Smuzhiyun module_param_named(pcie_caps, hfi1_pcie_caps, int, 0444);
461*4882a593Smuzhiyun MODULE_PARM_DESC(pcie_caps, "Max PCIe tuning: Payload (0..3), ReadReq (4..7)");
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun /**
464*4882a593Smuzhiyun  * tune_pcie_caps() - Code to adjust PCIe capabilities.
465*4882a593Smuzhiyun  * @dd: Valid device data structure
466*4882a593Smuzhiyun  *
467*4882a593Smuzhiyun  */
tune_pcie_caps(struct hfi1_devdata * dd)468*4882a593Smuzhiyun void tune_pcie_caps(struct hfi1_devdata *dd)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	struct pci_dev *parent;
471*4882a593Smuzhiyun 	u16 rc_mpss, rc_mps, ep_mpss, ep_mps;
472*4882a593Smuzhiyun 	u16 rc_mrrs, ep_mrrs, max_mrrs, ectl;
473*4882a593Smuzhiyun 	int ret;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	/*
476*4882a593Smuzhiyun 	 * Turn on extended tags in DevCtl in case the BIOS has turned it off
477*4882a593Smuzhiyun 	 * to improve WFR SDMA bandwidth
478*4882a593Smuzhiyun 	 */
479*4882a593Smuzhiyun 	ret = pcie_capability_read_word(dd->pcidev, PCI_EXP_DEVCTL, &ectl);
480*4882a593Smuzhiyun 	if ((!ret) && !(ectl & PCI_EXP_DEVCTL_EXT_TAG)) {
481*4882a593Smuzhiyun 		dd_dev_info(dd, "Enabling PCIe extended tags\n");
482*4882a593Smuzhiyun 		ectl |= PCI_EXP_DEVCTL_EXT_TAG;
483*4882a593Smuzhiyun 		ret = pcie_capability_write_word(dd->pcidev,
484*4882a593Smuzhiyun 						 PCI_EXP_DEVCTL, ectl);
485*4882a593Smuzhiyun 		if (ret)
486*4882a593Smuzhiyun 			dd_dev_info(dd, "Unable to write to PCI config\n");
487*4882a593Smuzhiyun 	}
488*4882a593Smuzhiyun 	/* Find out supported and configured values for parent (root) */
489*4882a593Smuzhiyun 	parent = dd->pcidev->bus->self;
490*4882a593Smuzhiyun 	/*
491*4882a593Smuzhiyun 	 * The driver cannot perform the tuning if it does not have
492*4882a593Smuzhiyun 	 * access to the upstream component.
493*4882a593Smuzhiyun 	 */
494*4882a593Smuzhiyun 	if (!parent) {
495*4882a593Smuzhiyun 		dd_dev_info(dd, "Parent not found\n");
496*4882a593Smuzhiyun 		return;
497*4882a593Smuzhiyun 	}
498*4882a593Smuzhiyun 	if (!pci_is_root_bus(parent->bus)) {
499*4882a593Smuzhiyun 		dd_dev_info(dd, "Parent not root\n");
500*4882a593Smuzhiyun 		return;
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun 	if (!pci_is_pcie(parent)) {
503*4882a593Smuzhiyun 		dd_dev_info(dd, "Parent is not PCI Express capable\n");
504*4882a593Smuzhiyun 		return;
505*4882a593Smuzhiyun 	}
506*4882a593Smuzhiyun 	if (!pci_is_pcie(dd->pcidev)) {
507*4882a593Smuzhiyun 		dd_dev_info(dd, "PCI device is not PCI Express capable\n");
508*4882a593Smuzhiyun 		return;
509*4882a593Smuzhiyun 	}
510*4882a593Smuzhiyun 	rc_mpss = parent->pcie_mpss;
511*4882a593Smuzhiyun 	rc_mps = ffs(pcie_get_mps(parent)) - 8;
512*4882a593Smuzhiyun 	/* Find out supported and configured values for endpoint (us) */
513*4882a593Smuzhiyun 	ep_mpss = dd->pcidev->pcie_mpss;
514*4882a593Smuzhiyun 	ep_mps = ffs(pcie_get_mps(dd->pcidev)) - 8;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	/* Find max payload supported by root, endpoint */
517*4882a593Smuzhiyun 	if (rc_mpss > ep_mpss)
518*4882a593Smuzhiyun 		rc_mpss = ep_mpss;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	/* If Supported greater than limit in module param, limit it */
521*4882a593Smuzhiyun 	if (rc_mpss > (hfi1_pcie_caps & 7))
522*4882a593Smuzhiyun 		rc_mpss = hfi1_pcie_caps & 7;
523*4882a593Smuzhiyun 	/* If less than (allowed, supported), bump root payload */
524*4882a593Smuzhiyun 	if (rc_mpss > rc_mps) {
525*4882a593Smuzhiyun 		rc_mps = rc_mpss;
526*4882a593Smuzhiyun 		pcie_set_mps(parent, 128 << rc_mps);
527*4882a593Smuzhiyun 	}
528*4882a593Smuzhiyun 	/* If less than (allowed, supported), bump endpoint payload */
529*4882a593Smuzhiyun 	if (rc_mpss > ep_mps) {
530*4882a593Smuzhiyun 		ep_mps = rc_mpss;
531*4882a593Smuzhiyun 		pcie_set_mps(dd->pcidev, 128 << ep_mps);
532*4882a593Smuzhiyun 	}
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	/*
535*4882a593Smuzhiyun 	 * Now the Read Request size.
536*4882a593Smuzhiyun 	 * No field for max supported, but PCIe spec limits it to 4096,
537*4882a593Smuzhiyun 	 * which is code '5' (log2(4096) - 7)
538*4882a593Smuzhiyun 	 */
539*4882a593Smuzhiyun 	max_mrrs = 5;
540*4882a593Smuzhiyun 	if (max_mrrs > ((hfi1_pcie_caps >> 4) & 7))
541*4882a593Smuzhiyun 		max_mrrs = (hfi1_pcie_caps >> 4) & 7;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	max_mrrs = 128 << max_mrrs;
544*4882a593Smuzhiyun 	rc_mrrs = pcie_get_readrq(parent);
545*4882a593Smuzhiyun 	ep_mrrs = pcie_get_readrq(dd->pcidev);
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	if (max_mrrs > rc_mrrs) {
548*4882a593Smuzhiyun 		rc_mrrs = max_mrrs;
549*4882a593Smuzhiyun 		pcie_set_readrq(parent, rc_mrrs);
550*4882a593Smuzhiyun 	}
551*4882a593Smuzhiyun 	if (max_mrrs > ep_mrrs) {
552*4882a593Smuzhiyun 		ep_mrrs = max_mrrs;
553*4882a593Smuzhiyun 		pcie_set_readrq(dd->pcidev, ep_mrrs);
554*4882a593Smuzhiyun 	}
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /* End of PCIe capability tuning */
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun /*
560*4882a593Smuzhiyun  * From here through hfi1_pci_err_handler definition is invoked via
561*4882a593Smuzhiyun  * PCI error infrastructure, registered via pci
562*4882a593Smuzhiyun  */
563*4882a593Smuzhiyun static pci_ers_result_t
pci_error_detected(struct pci_dev * pdev,pci_channel_state_t state)564*4882a593Smuzhiyun pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun 	struct hfi1_devdata *dd = pci_get_drvdata(pdev);
567*4882a593Smuzhiyun 	pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	switch (state) {
570*4882a593Smuzhiyun 	case pci_channel_io_normal:
571*4882a593Smuzhiyun 		dd_dev_info(dd, "State Normal, ignoring\n");
572*4882a593Smuzhiyun 		break;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	case pci_channel_io_frozen:
575*4882a593Smuzhiyun 		dd_dev_info(dd, "State Frozen, requesting reset\n");
576*4882a593Smuzhiyun 		pci_disable_device(pdev);
577*4882a593Smuzhiyun 		ret = PCI_ERS_RESULT_NEED_RESET;
578*4882a593Smuzhiyun 		break;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	case pci_channel_io_perm_failure:
581*4882a593Smuzhiyun 		if (dd) {
582*4882a593Smuzhiyun 			dd_dev_info(dd, "State Permanent Failure, disabling\n");
583*4882a593Smuzhiyun 			/* no more register accesses! */
584*4882a593Smuzhiyun 			dd->flags &= ~HFI1_PRESENT;
585*4882a593Smuzhiyun 			hfi1_disable_after_error(dd);
586*4882a593Smuzhiyun 		}
587*4882a593Smuzhiyun 		 /* else early, or other problem */
588*4882a593Smuzhiyun 		ret =  PCI_ERS_RESULT_DISCONNECT;
589*4882a593Smuzhiyun 		break;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	default: /* shouldn't happen */
592*4882a593Smuzhiyun 		dd_dev_info(dd, "HFI1 PCI errors detected (state %d)\n",
593*4882a593Smuzhiyun 			    state);
594*4882a593Smuzhiyun 		break;
595*4882a593Smuzhiyun 	}
596*4882a593Smuzhiyun 	return ret;
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun static pci_ers_result_t
pci_mmio_enabled(struct pci_dev * pdev)600*4882a593Smuzhiyun pci_mmio_enabled(struct pci_dev *pdev)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun 	u64 words = 0U;
603*4882a593Smuzhiyun 	struct hfi1_devdata *dd = pci_get_drvdata(pdev);
604*4882a593Smuzhiyun 	pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	if (dd && dd->pport) {
607*4882a593Smuzhiyun 		words = read_port_cntr(dd->pport, C_RX_WORDS, CNTR_INVALID_VL);
608*4882a593Smuzhiyun 		if (words == ~0ULL)
609*4882a593Smuzhiyun 			ret = PCI_ERS_RESULT_NEED_RESET;
610*4882a593Smuzhiyun 		dd_dev_info(dd,
611*4882a593Smuzhiyun 			    "HFI1 mmio_enabled function called, read wordscntr %llx, returning %d\n",
612*4882a593Smuzhiyun 			    words, ret);
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 	return  ret;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun static pci_ers_result_t
pci_slot_reset(struct pci_dev * pdev)618*4882a593Smuzhiyun pci_slot_reset(struct pci_dev *pdev)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun 	struct hfi1_devdata *dd = pci_get_drvdata(pdev);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	dd_dev_info(dd, "HFI1 slot_reset function called, ignored\n");
623*4882a593Smuzhiyun 	return PCI_ERS_RESULT_CAN_RECOVER;
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun static void
pci_resume(struct pci_dev * pdev)627*4882a593Smuzhiyun pci_resume(struct pci_dev *pdev)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun 	struct hfi1_devdata *dd = pci_get_drvdata(pdev);
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	dd_dev_info(dd, "HFI1 resume function called\n");
632*4882a593Smuzhiyun 	/*
633*4882a593Smuzhiyun 	 * Running jobs will fail, since it's asynchronous
634*4882a593Smuzhiyun 	 * unlike sysfs-requested reset.   Better than
635*4882a593Smuzhiyun 	 * doing nothing.
636*4882a593Smuzhiyun 	 */
637*4882a593Smuzhiyun 	hfi1_init(dd, 1); /* same as re-init after reset */
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun const struct pci_error_handlers hfi1_pci_err_handler = {
641*4882a593Smuzhiyun 	.error_detected = pci_error_detected,
642*4882a593Smuzhiyun 	.mmio_enabled = pci_mmio_enabled,
643*4882a593Smuzhiyun 	.slot_reset = pci_slot_reset,
644*4882a593Smuzhiyun 	.resume = pci_resume,
645*4882a593Smuzhiyun };
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun /*============================================================================*/
648*4882a593Smuzhiyun /* PCIe Gen3 support */
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun /*
651*4882a593Smuzhiyun  * This code is separated out because it is expected to be removed in the
652*4882a593Smuzhiyun  * final shipping product.  If not, then it will be revisited and items
653*4882a593Smuzhiyun  * will be moved to more standard locations.
654*4882a593Smuzhiyun  */
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun /* ASIC_PCI_SD_HOST_STATUS.FW_DNLD_STS field values */
657*4882a593Smuzhiyun #define DL_STATUS_HFI0 0x1	/* hfi0 firmware download complete */
658*4882a593Smuzhiyun #define DL_STATUS_HFI1 0x2	/* hfi1 firmware download complete */
659*4882a593Smuzhiyun #define DL_STATUS_BOTH 0x3	/* hfi0 and hfi1 firmware download complete */
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun /* ASIC_PCI_SD_HOST_STATUS.FW_DNLD_ERR field values */
662*4882a593Smuzhiyun #define DL_ERR_NONE		0x0	/* no error */
663*4882a593Smuzhiyun #define DL_ERR_SWAP_PARITY	0x1	/* parity error in SerDes interrupt */
664*4882a593Smuzhiyun 					/*   or response data */
665*4882a593Smuzhiyun #define DL_ERR_DISABLED	0x2	/* hfi disabled */
666*4882a593Smuzhiyun #define DL_ERR_SECURITY	0x3	/* security check failed */
667*4882a593Smuzhiyun #define DL_ERR_SBUS		0x4	/* SBus status error */
668*4882a593Smuzhiyun #define DL_ERR_XFR_PARITY	0x5	/* parity error during ROM transfer*/
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun /* gasket block secondary bus reset delay */
671*4882a593Smuzhiyun #define SBR_DELAY_US 200000	/* 200ms */
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun static uint pcie_target = 3;
674*4882a593Smuzhiyun module_param(pcie_target, uint, S_IRUGO);
675*4882a593Smuzhiyun MODULE_PARM_DESC(pcie_target, "PCIe target speed (0 skip, 1-3 Gen1-3)");
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun static uint pcie_force;
678*4882a593Smuzhiyun module_param(pcie_force, uint, S_IRUGO);
679*4882a593Smuzhiyun MODULE_PARM_DESC(pcie_force, "Force driver to do a PCIe firmware download even if already at target speed");
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun static uint pcie_retry = 5;
682*4882a593Smuzhiyun module_param(pcie_retry, uint, S_IRUGO);
683*4882a593Smuzhiyun MODULE_PARM_DESC(pcie_retry, "Driver will try this many times to reach requested speed");
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun #define UNSET_PSET 255
686*4882a593Smuzhiyun #define DEFAULT_DISCRETE_PSET 2	/* discrete HFI */
687*4882a593Smuzhiyun #define DEFAULT_MCP_PSET 6	/* MCP HFI */
688*4882a593Smuzhiyun static uint pcie_pset = UNSET_PSET;
689*4882a593Smuzhiyun module_param(pcie_pset, uint, S_IRUGO);
690*4882a593Smuzhiyun MODULE_PARM_DESC(pcie_pset, "PCIe Eq Pset value to use, range is 0-10");
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun static uint pcie_ctle = 3; /* discrete on, integrated on */
693*4882a593Smuzhiyun module_param(pcie_ctle, uint, S_IRUGO);
694*4882a593Smuzhiyun MODULE_PARM_DESC(pcie_ctle, "PCIe static CTLE mode, bit 0 - discrete on/off, bit 1 - integrated on/off");
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun /* equalization columns */
697*4882a593Smuzhiyun #define PREC 0
698*4882a593Smuzhiyun #define ATTN 1
699*4882a593Smuzhiyun #define POST 2
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun /* discrete silicon preliminary equalization values */
702*4882a593Smuzhiyun static const u8 discrete_preliminary_eq[11][3] = {
703*4882a593Smuzhiyun 	/* prec   attn   post */
704*4882a593Smuzhiyun 	{  0x00,  0x00,  0x12 },	/* p0 */
705*4882a593Smuzhiyun 	{  0x00,  0x00,  0x0c },	/* p1 */
706*4882a593Smuzhiyun 	{  0x00,  0x00,  0x0f },	/* p2 */
707*4882a593Smuzhiyun 	{  0x00,  0x00,  0x09 },	/* p3 */
708*4882a593Smuzhiyun 	{  0x00,  0x00,  0x00 },	/* p4 */
709*4882a593Smuzhiyun 	{  0x06,  0x00,  0x00 },	/* p5 */
710*4882a593Smuzhiyun 	{  0x09,  0x00,  0x00 },	/* p6 */
711*4882a593Smuzhiyun 	{  0x06,  0x00,  0x0f },	/* p7 */
712*4882a593Smuzhiyun 	{  0x09,  0x00,  0x09 },	/* p8 */
713*4882a593Smuzhiyun 	{  0x0c,  0x00,  0x00 },	/* p9 */
714*4882a593Smuzhiyun 	{  0x00,  0x00,  0x18 },	/* p10 */
715*4882a593Smuzhiyun };
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun /* integrated silicon preliminary equalization values */
718*4882a593Smuzhiyun static const u8 integrated_preliminary_eq[11][3] = {
719*4882a593Smuzhiyun 	/* prec   attn   post */
720*4882a593Smuzhiyun 	{  0x00,  0x1e,  0x07 },	/* p0 */
721*4882a593Smuzhiyun 	{  0x00,  0x1e,  0x05 },	/* p1 */
722*4882a593Smuzhiyun 	{  0x00,  0x1e,  0x06 },	/* p2 */
723*4882a593Smuzhiyun 	{  0x00,  0x1e,  0x04 },	/* p3 */
724*4882a593Smuzhiyun 	{  0x00,  0x1e,  0x00 },	/* p4 */
725*4882a593Smuzhiyun 	{  0x03,  0x1e,  0x00 },	/* p5 */
726*4882a593Smuzhiyun 	{  0x04,  0x1e,  0x00 },	/* p6 */
727*4882a593Smuzhiyun 	{  0x03,  0x1e,  0x06 },	/* p7 */
728*4882a593Smuzhiyun 	{  0x03,  0x1e,  0x04 },	/* p8 */
729*4882a593Smuzhiyun 	{  0x05,  0x1e,  0x00 },	/* p9 */
730*4882a593Smuzhiyun 	{  0x00,  0x1e,  0x0a },	/* p10 */
731*4882a593Smuzhiyun };
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun static const u8 discrete_ctle_tunings[11][4] = {
734*4882a593Smuzhiyun 	/* DC     LF     HF     BW */
735*4882a593Smuzhiyun 	{  0x48,  0x0b,  0x04,  0x04 },	/* p0 */
736*4882a593Smuzhiyun 	{  0x60,  0x05,  0x0f,  0x0a },	/* p1 */
737*4882a593Smuzhiyun 	{  0x50,  0x09,  0x06,  0x06 },	/* p2 */
738*4882a593Smuzhiyun 	{  0x68,  0x05,  0x0f,  0x0a },	/* p3 */
739*4882a593Smuzhiyun 	{  0x80,  0x05,  0x0f,  0x0a },	/* p4 */
740*4882a593Smuzhiyun 	{  0x70,  0x05,  0x0f,  0x0a },	/* p5 */
741*4882a593Smuzhiyun 	{  0x68,  0x05,  0x0f,  0x0a },	/* p6 */
742*4882a593Smuzhiyun 	{  0x38,  0x0f,  0x00,  0x00 },	/* p7 */
743*4882a593Smuzhiyun 	{  0x48,  0x09,  0x06,  0x06 },	/* p8 */
744*4882a593Smuzhiyun 	{  0x60,  0x05,  0x0f,  0x0a },	/* p9 */
745*4882a593Smuzhiyun 	{  0x38,  0x0f,  0x00,  0x00 },	/* p10 */
746*4882a593Smuzhiyun };
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun static const u8 integrated_ctle_tunings[11][4] = {
749*4882a593Smuzhiyun 	/* DC     LF     HF     BW */
750*4882a593Smuzhiyun 	{  0x38,  0x0f,  0x00,  0x00 },	/* p0 */
751*4882a593Smuzhiyun 	{  0x38,  0x0f,  0x00,  0x00 },	/* p1 */
752*4882a593Smuzhiyun 	{  0x38,  0x0f,  0x00,  0x00 },	/* p2 */
753*4882a593Smuzhiyun 	{  0x38,  0x0f,  0x00,  0x00 },	/* p3 */
754*4882a593Smuzhiyun 	{  0x58,  0x0a,  0x05,  0x05 },	/* p4 */
755*4882a593Smuzhiyun 	{  0x48,  0x0a,  0x05,  0x05 },	/* p5 */
756*4882a593Smuzhiyun 	{  0x40,  0x0a,  0x05,  0x05 },	/* p6 */
757*4882a593Smuzhiyun 	{  0x38,  0x0f,  0x00,  0x00 },	/* p7 */
758*4882a593Smuzhiyun 	{  0x38,  0x0f,  0x00,  0x00 },	/* p8 */
759*4882a593Smuzhiyun 	{  0x38,  0x09,  0x06,  0x06 },	/* p9 */
760*4882a593Smuzhiyun 	{  0x38,  0x0e,  0x01,  0x01 },	/* p10 */
761*4882a593Smuzhiyun };
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun /* helper to format the value to write to hardware */
764*4882a593Smuzhiyun #define eq_value(pre, curr, post) \
765*4882a593Smuzhiyun 	((((u32)(pre)) << \
766*4882a593Smuzhiyun 			PCIE_CFG_REG_PL102_GEN3_EQ_PRE_CURSOR_PSET_SHIFT) \
767*4882a593Smuzhiyun 	| (((u32)(curr)) << PCIE_CFG_REG_PL102_GEN3_EQ_CURSOR_PSET_SHIFT) \
768*4882a593Smuzhiyun 	| (((u32)(post)) << \
769*4882a593Smuzhiyun 		PCIE_CFG_REG_PL102_GEN3_EQ_POST_CURSOR_PSET_SHIFT))
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun /*
772*4882a593Smuzhiyun  * Load the given EQ preset table into the PCIe hardware.
773*4882a593Smuzhiyun  */
load_eq_table(struct hfi1_devdata * dd,const u8 eq[11][3],u8 fs,u8 div)774*4882a593Smuzhiyun static int load_eq_table(struct hfi1_devdata *dd, const u8 eq[11][3], u8 fs,
775*4882a593Smuzhiyun 			 u8 div)
776*4882a593Smuzhiyun {
777*4882a593Smuzhiyun 	struct pci_dev *pdev = dd->pcidev;
778*4882a593Smuzhiyun 	u32 hit_error = 0;
779*4882a593Smuzhiyun 	u32 violation;
780*4882a593Smuzhiyun 	u32 i;
781*4882a593Smuzhiyun 	u8 c_minus1, c0, c_plus1;
782*4882a593Smuzhiyun 	int ret;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	for (i = 0; i < 11; i++) {
785*4882a593Smuzhiyun 		/* set index */
786*4882a593Smuzhiyun 		pci_write_config_dword(pdev, PCIE_CFG_REG_PL103, i);
787*4882a593Smuzhiyun 		/* write the value */
788*4882a593Smuzhiyun 		c_minus1 = eq[i][PREC] / div;
789*4882a593Smuzhiyun 		c0 = fs - (eq[i][PREC] / div) - (eq[i][POST] / div);
790*4882a593Smuzhiyun 		c_plus1 = eq[i][POST] / div;
791*4882a593Smuzhiyun 		pci_write_config_dword(pdev, PCIE_CFG_REG_PL102,
792*4882a593Smuzhiyun 				       eq_value(c_minus1, c0, c_plus1));
793*4882a593Smuzhiyun 		/* check if these coefficients violate EQ rules */
794*4882a593Smuzhiyun 		ret = pci_read_config_dword(dd->pcidev,
795*4882a593Smuzhiyun 					    PCIE_CFG_REG_PL105, &violation);
796*4882a593Smuzhiyun 		if (ret) {
797*4882a593Smuzhiyun 			dd_dev_err(dd, "Unable to read from PCI config\n");
798*4882a593Smuzhiyun 			hit_error = 1;
799*4882a593Smuzhiyun 			break;
800*4882a593Smuzhiyun 		}
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 		if (violation
803*4882a593Smuzhiyun 		    & PCIE_CFG_REG_PL105_GEN3_EQ_VIOLATE_COEF_RULES_SMASK){
804*4882a593Smuzhiyun 			if (hit_error == 0) {
805*4882a593Smuzhiyun 				dd_dev_err(dd,
806*4882a593Smuzhiyun 					   "Gen3 EQ Table Coefficient rule violations\n");
807*4882a593Smuzhiyun 				dd_dev_err(dd, "         prec   attn   post\n");
808*4882a593Smuzhiyun 			}
809*4882a593Smuzhiyun 			dd_dev_err(dd, "   p%02d:   %02x     %02x     %02x\n",
810*4882a593Smuzhiyun 				   i, (u32)eq[i][0], (u32)eq[i][1],
811*4882a593Smuzhiyun 				   (u32)eq[i][2]);
812*4882a593Smuzhiyun 			dd_dev_err(dd, "            %02x     %02x     %02x\n",
813*4882a593Smuzhiyun 				   (u32)c_minus1, (u32)c0, (u32)c_plus1);
814*4882a593Smuzhiyun 			hit_error = 1;
815*4882a593Smuzhiyun 		}
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun 	if (hit_error)
818*4882a593Smuzhiyun 		return -EINVAL;
819*4882a593Smuzhiyun 	return 0;
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun /*
823*4882a593Smuzhiyun  * Steps to be done after the PCIe firmware is downloaded and
824*4882a593Smuzhiyun  * before the SBR for the Pcie Gen3.
825*4882a593Smuzhiyun  * The SBus resource is already being held.
826*4882a593Smuzhiyun  */
pcie_post_steps(struct hfi1_devdata * dd)827*4882a593Smuzhiyun static void pcie_post_steps(struct hfi1_devdata *dd)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	int i;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	set_sbus_fast_mode(dd);
832*4882a593Smuzhiyun 	/*
833*4882a593Smuzhiyun 	 * Write to the PCIe PCSes to set the G3_LOCKED_NEXT bits to 1.
834*4882a593Smuzhiyun 	 * This avoids a spurious framing error that can otherwise be
835*4882a593Smuzhiyun 	 * generated by the MAC layer.
836*4882a593Smuzhiyun 	 *
837*4882a593Smuzhiyun 	 * Use individual addresses since no broadcast is set up.
838*4882a593Smuzhiyun 	 */
839*4882a593Smuzhiyun 	for (i = 0; i < NUM_PCIE_SERDES; i++) {
840*4882a593Smuzhiyun 		sbus_request(dd, pcie_pcs_addrs[dd->hfi1_id][i],
841*4882a593Smuzhiyun 			     0x03, WRITE_SBUS_RECEIVER, 0x00022132);
842*4882a593Smuzhiyun 	}
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	clear_sbus_fast_mode(dd);
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun /*
848*4882a593Smuzhiyun  * Trigger a secondary bus reset (SBR) on ourselves using our parent.
849*4882a593Smuzhiyun  *
850*4882a593Smuzhiyun  * Based on pci_parent_bus_reset() which is not exported by the
851*4882a593Smuzhiyun  * kernel core.
852*4882a593Smuzhiyun  */
trigger_sbr(struct hfi1_devdata * dd)853*4882a593Smuzhiyun static int trigger_sbr(struct hfi1_devdata *dd)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun 	struct pci_dev *dev = dd->pcidev;
856*4882a593Smuzhiyun 	struct pci_dev *pdev;
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	/* need a parent */
859*4882a593Smuzhiyun 	if (!dev->bus->self) {
860*4882a593Smuzhiyun 		dd_dev_err(dd, "%s: no parent device\n", __func__);
861*4882a593Smuzhiyun 		return -ENOTTY;
862*4882a593Smuzhiyun 	}
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	/* should not be anyone else on the bus */
865*4882a593Smuzhiyun 	list_for_each_entry(pdev, &dev->bus->devices, bus_list)
866*4882a593Smuzhiyun 		if (pdev != dev) {
867*4882a593Smuzhiyun 			dd_dev_err(dd,
868*4882a593Smuzhiyun 				   "%s: another device is on the same bus\n",
869*4882a593Smuzhiyun 				   __func__);
870*4882a593Smuzhiyun 			return -ENOTTY;
871*4882a593Smuzhiyun 		}
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	/*
874*4882a593Smuzhiyun 	 * This is an end around to do an SBR during probe time. A new API needs
875*4882a593Smuzhiyun 	 * to be implemented to have cleaner interface but this fixes the
876*4882a593Smuzhiyun 	 * current brokenness
877*4882a593Smuzhiyun 	 */
878*4882a593Smuzhiyun 	return pci_bridge_secondary_bus_reset(dev->bus->self);
879*4882a593Smuzhiyun }
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun /*
882*4882a593Smuzhiyun  * Write the given gasket interrupt register.
883*4882a593Smuzhiyun  */
write_gasket_interrupt(struct hfi1_devdata * dd,int index,u16 code,u16 data)884*4882a593Smuzhiyun static void write_gasket_interrupt(struct hfi1_devdata *dd, int index,
885*4882a593Smuzhiyun 				   u16 code, u16 data)
886*4882a593Smuzhiyun {
887*4882a593Smuzhiyun 	write_csr(dd, ASIC_PCIE_SD_INTRPT_LIST + (index * 8),
888*4882a593Smuzhiyun 		  (((u64)code << ASIC_PCIE_SD_INTRPT_LIST_INTRPT_CODE_SHIFT) |
889*4882a593Smuzhiyun 		   ((u64)data << ASIC_PCIE_SD_INTRPT_LIST_INTRPT_DATA_SHIFT)));
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun /*
893*4882a593Smuzhiyun  * Tell the gasket logic how to react to the reset.
894*4882a593Smuzhiyun  */
arm_gasket_logic(struct hfi1_devdata * dd)895*4882a593Smuzhiyun static void arm_gasket_logic(struct hfi1_devdata *dd)
896*4882a593Smuzhiyun {
897*4882a593Smuzhiyun 	u64 reg;
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	reg = (((u64)1 << dd->hfi1_id) <<
900*4882a593Smuzhiyun 	       ASIC_PCIE_SD_HOST_CMD_INTRPT_CMD_SHIFT) |
901*4882a593Smuzhiyun 	      ((u64)pcie_serdes_broadcast[dd->hfi1_id] <<
902*4882a593Smuzhiyun 	       ASIC_PCIE_SD_HOST_CMD_SBUS_RCVR_ADDR_SHIFT |
903*4882a593Smuzhiyun 	       ASIC_PCIE_SD_HOST_CMD_SBR_MODE_SMASK |
904*4882a593Smuzhiyun 	       ((u64)SBR_DELAY_US & ASIC_PCIE_SD_HOST_CMD_TIMER_MASK) <<
905*4882a593Smuzhiyun 	       ASIC_PCIE_SD_HOST_CMD_TIMER_SHIFT);
906*4882a593Smuzhiyun 	write_csr(dd, ASIC_PCIE_SD_HOST_CMD, reg);
907*4882a593Smuzhiyun 	/* read back to push the write */
908*4882a593Smuzhiyun 	read_csr(dd, ASIC_PCIE_SD_HOST_CMD);
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun /*
912*4882a593Smuzhiyun  * CCE_PCIE_CTRL long name helpers
913*4882a593Smuzhiyun  * We redefine these shorter macros to use in the code while leaving
914*4882a593Smuzhiyun  * chip_registers.h to be autogenerated from the hardware spec.
915*4882a593Smuzhiyun  */
916*4882a593Smuzhiyun #define LANE_BUNDLE_MASK              CCE_PCIE_CTRL_PCIE_LANE_BUNDLE_MASK
917*4882a593Smuzhiyun #define LANE_BUNDLE_SHIFT             CCE_PCIE_CTRL_PCIE_LANE_BUNDLE_SHIFT
918*4882a593Smuzhiyun #define LANE_DELAY_MASK               CCE_PCIE_CTRL_PCIE_LANE_DELAY_MASK
919*4882a593Smuzhiyun #define LANE_DELAY_SHIFT              CCE_PCIE_CTRL_PCIE_LANE_DELAY_SHIFT
920*4882a593Smuzhiyun #define MARGIN_OVERWRITE_ENABLE_SHIFT CCE_PCIE_CTRL_XMT_MARGIN_OVERWRITE_ENABLE_SHIFT
921*4882a593Smuzhiyun #define MARGIN_SHIFT                  CCE_PCIE_CTRL_XMT_MARGIN_SHIFT
922*4882a593Smuzhiyun #define MARGIN_G1_G2_OVERWRITE_MASK   CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_OVERWRITE_ENABLE_MASK
923*4882a593Smuzhiyun #define MARGIN_G1_G2_OVERWRITE_SHIFT  CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_OVERWRITE_ENABLE_SHIFT
924*4882a593Smuzhiyun #define MARGIN_GEN1_GEN2_MASK         CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_MASK
925*4882a593Smuzhiyun #define MARGIN_GEN1_GEN2_SHIFT        CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_SHIFT
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun  /*
928*4882a593Smuzhiyun   * Write xmt_margin for full-swing (WFR-B) or half-swing (WFR-C).
929*4882a593Smuzhiyun   */
write_xmt_margin(struct hfi1_devdata * dd,const char * fname)930*4882a593Smuzhiyun static void write_xmt_margin(struct hfi1_devdata *dd, const char *fname)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun 	u64 pcie_ctrl;
933*4882a593Smuzhiyun 	u64 xmt_margin;
934*4882a593Smuzhiyun 	u64 xmt_margin_oe;
935*4882a593Smuzhiyun 	u64 lane_delay;
936*4882a593Smuzhiyun 	u64 lane_bundle;
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	pcie_ctrl = read_csr(dd, CCE_PCIE_CTRL);
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	/*
941*4882a593Smuzhiyun 	 * For Discrete, use full-swing.
942*4882a593Smuzhiyun 	 *  - PCIe TX defaults to full-swing.
943*4882a593Smuzhiyun 	 *    Leave this register as default.
944*4882a593Smuzhiyun 	 * For Integrated, use half-swing
945*4882a593Smuzhiyun 	 *  - Copy xmt_margin and xmt_margin_oe
946*4882a593Smuzhiyun 	 *    from Gen1/Gen2 to Gen3.
947*4882a593Smuzhiyun 	 */
948*4882a593Smuzhiyun 	if (dd->pcidev->device == PCI_DEVICE_ID_INTEL1) { /* integrated */
949*4882a593Smuzhiyun 		/* extract initial fields */
950*4882a593Smuzhiyun 		xmt_margin = (pcie_ctrl >> MARGIN_GEN1_GEN2_SHIFT)
951*4882a593Smuzhiyun 			      & MARGIN_GEN1_GEN2_MASK;
952*4882a593Smuzhiyun 		xmt_margin_oe = (pcie_ctrl >> MARGIN_G1_G2_OVERWRITE_SHIFT)
953*4882a593Smuzhiyun 				 & MARGIN_G1_G2_OVERWRITE_MASK;
954*4882a593Smuzhiyun 		lane_delay = (pcie_ctrl >> LANE_DELAY_SHIFT) & LANE_DELAY_MASK;
955*4882a593Smuzhiyun 		lane_bundle = (pcie_ctrl >> LANE_BUNDLE_SHIFT)
956*4882a593Smuzhiyun 			       & LANE_BUNDLE_MASK;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 		/*
959*4882a593Smuzhiyun 		 * For A0, EFUSE values are not set.  Override with the
960*4882a593Smuzhiyun 		 * correct values.
961*4882a593Smuzhiyun 		 */
962*4882a593Smuzhiyun 		if (is_ax(dd)) {
963*4882a593Smuzhiyun 			/*
964*4882a593Smuzhiyun 			 * xmt_margin and OverwiteEnabel should be the
965*4882a593Smuzhiyun 			 * same for Gen1/Gen2 and Gen3
966*4882a593Smuzhiyun 			 */
967*4882a593Smuzhiyun 			xmt_margin = 0x5;
968*4882a593Smuzhiyun 			xmt_margin_oe = 0x1;
969*4882a593Smuzhiyun 			lane_delay = 0xF; /* Delay 240ns. */
970*4882a593Smuzhiyun 			lane_bundle = 0x0; /* Set to 1 lane. */
971*4882a593Smuzhiyun 		}
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 		/* overwrite existing values */
974*4882a593Smuzhiyun 		pcie_ctrl = (xmt_margin << MARGIN_GEN1_GEN2_SHIFT)
975*4882a593Smuzhiyun 			| (xmt_margin_oe << MARGIN_G1_G2_OVERWRITE_SHIFT)
976*4882a593Smuzhiyun 			| (xmt_margin << MARGIN_SHIFT)
977*4882a593Smuzhiyun 			| (xmt_margin_oe << MARGIN_OVERWRITE_ENABLE_SHIFT)
978*4882a593Smuzhiyun 			| (lane_delay << LANE_DELAY_SHIFT)
979*4882a593Smuzhiyun 			| (lane_bundle << LANE_BUNDLE_SHIFT);
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 		write_csr(dd, CCE_PCIE_CTRL, pcie_ctrl);
982*4882a593Smuzhiyun 	}
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	dd_dev_dbg(dd, "%s: program XMT margin, CcePcieCtrl 0x%llx\n",
985*4882a593Smuzhiyun 		   fname, pcie_ctrl);
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun /*
989*4882a593Smuzhiyun  * Do all the steps needed to transition the PCIe link to Gen3 speed.
990*4882a593Smuzhiyun  */
do_pcie_gen3_transition(struct hfi1_devdata * dd)991*4882a593Smuzhiyun int do_pcie_gen3_transition(struct hfi1_devdata *dd)
992*4882a593Smuzhiyun {
993*4882a593Smuzhiyun 	struct pci_dev *parent = dd->pcidev->bus->self;
994*4882a593Smuzhiyun 	u64 fw_ctrl;
995*4882a593Smuzhiyun 	u64 reg, therm;
996*4882a593Smuzhiyun 	u32 reg32, fs, lf;
997*4882a593Smuzhiyun 	u32 status, err;
998*4882a593Smuzhiyun 	int ret;
999*4882a593Smuzhiyun 	int do_retry, retry_count = 0;
1000*4882a593Smuzhiyun 	int intnum = 0;
1001*4882a593Smuzhiyun 	uint default_pset;
1002*4882a593Smuzhiyun 	uint pset = pcie_pset;
1003*4882a593Smuzhiyun 	u16 target_vector, target_speed;
1004*4882a593Smuzhiyun 	u16 lnkctl2, vendor;
1005*4882a593Smuzhiyun 	u8 div;
1006*4882a593Smuzhiyun 	const u8 (*eq)[3];
1007*4882a593Smuzhiyun 	const u8 (*ctle_tunings)[4];
1008*4882a593Smuzhiyun 	uint static_ctle_mode;
1009*4882a593Smuzhiyun 	int return_error = 0;
1010*4882a593Smuzhiyun 	u32 target_width;
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun 	/* PCIe Gen3 is for the ASIC only */
1013*4882a593Smuzhiyun 	if (dd->icode != ICODE_RTL_SILICON)
1014*4882a593Smuzhiyun 		return 0;
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 	if (pcie_target == 1) {			/* target Gen1 */
1017*4882a593Smuzhiyun 		target_vector = PCI_EXP_LNKCTL2_TLS_2_5GT;
1018*4882a593Smuzhiyun 		target_speed = 2500;
1019*4882a593Smuzhiyun 	} else if (pcie_target == 2) {		/* target Gen2 */
1020*4882a593Smuzhiyun 		target_vector = PCI_EXP_LNKCTL2_TLS_5_0GT;
1021*4882a593Smuzhiyun 		target_speed = 5000;
1022*4882a593Smuzhiyun 	} else if (pcie_target == 3) {		/* target Gen3 */
1023*4882a593Smuzhiyun 		target_vector = PCI_EXP_LNKCTL2_TLS_8_0GT;
1024*4882a593Smuzhiyun 		target_speed = 8000;
1025*4882a593Smuzhiyun 	} else {
1026*4882a593Smuzhiyun 		/* off or invalid target - skip */
1027*4882a593Smuzhiyun 		dd_dev_info(dd, "%s: Skipping PCIe transition\n", __func__);
1028*4882a593Smuzhiyun 		return 0;
1029*4882a593Smuzhiyun 	}
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	/* if already at target speed, done (unless forced) */
1032*4882a593Smuzhiyun 	if (dd->lbus_speed == target_speed) {
1033*4882a593Smuzhiyun 		dd_dev_info(dd, "%s: PCIe already at gen%d, %s\n", __func__,
1034*4882a593Smuzhiyun 			    pcie_target,
1035*4882a593Smuzhiyun 			    pcie_force ? "re-doing anyway" : "skipping");
1036*4882a593Smuzhiyun 		if (!pcie_force)
1037*4882a593Smuzhiyun 			return 0;
1038*4882a593Smuzhiyun 	}
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 	/*
1041*4882a593Smuzhiyun 	 * The driver cannot do the transition if it has no access to the
1042*4882a593Smuzhiyun 	 * upstream component
1043*4882a593Smuzhiyun 	 */
1044*4882a593Smuzhiyun 	if (!parent) {
1045*4882a593Smuzhiyun 		dd_dev_info(dd, "%s: No upstream, Can't do gen3 transition\n",
1046*4882a593Smuzhiyun 			    __func__);
1047*4882a593Smuzhiyun 		return 0;
1048*4882a593Smuzhiyun 	}
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	/* Previous Gen1/Gen2 bus width */
1051*4882a593Smuzhiyun 	target_width = dd->lbus_width;
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	/*
1054*4882a593Smuzhiyun 	 * Do the Gen3 transition.  Steps are those of the PCIe Gen3
1055*4882a593Smuzhiyun 	 * recipe.
1056*4882a593Smuzhiyun 	 */
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	/* step 1: pcie link working in gen1/gen2 */
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	/* step 2: if either side is not capable of Gen3, done */
1061*4882a593Smuzhiyun 	if (pcie_target == 3 && !dd->link_gen3_capable) {
1062*4882a593Smuzhiyun 		dd_dev_err(dd, "The PCIe link is not Gen3 capable\n");
1063*4882a593Smuzhiyun 		ret = -ENOSYS;
1064*4882a593Smuzhiyun 		goto done_no_mutex;
1065*4882a593Smuzhiyun 	}
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	/* hold the SBus resource across the firmware download and SBR */
1068*4882a593Smuzhiyun 	ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
1069*4882a593Smuzhiyun 	if (ret) {
1070*4882a593Smuzhiyun 		dd_dev_err(dd, "%s: unable to acquire SBus resource\n",
1071*4882a593Smuzhiyun 			   __func__);
1072*4882a593Smuzhiyun 		return ret;
1073*4882a593Smuzhiyun 	}
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 	/* make sure thermal polling is not causing interrupts */
1076*4882a593Smuzhiyun 	therm = read_csr(dd, ASIC_CFG_THERM_POLL_EN);
1077*4882a593Smuzhiyun 	if (therm) {
1078*4882a593Smuzhiyun 		write_csr(dd, ASIC_CFG_THERM_POLL_EN, 0x0);
1079*4882a593Smuzhiyun 		msleep(100);
1080*4882a593Smuzhiyun 		dd_dev_info(dd, "%s: Disabled therm polling\n",
1081*4882a593Smuzhiyun 			    __func__);
1082*4882a593Smuzhiyun 	}
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun retry:
1085*4882a593Smuzhiyun 	/* the SBus download will reset the spico for thermal */
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	/* step 3: download SBus Master firmware */
1088*4882a593Smuzhiyun 	/* step 4: download PCIe Gen3 SerDes firmware */
1089*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: downloading firmware\n", __func__);
1090*4882a593Smuzhiyun 	ret = load_pcie_firmware(dd);
1091*4882a593Smuzhiyun 	if (ret) {
1092*4882a593Smuzhiyun 		/* do not proceed if the firmware cannot be downloaded */
1093*4882a593Smuzhiyun 		return_error = 1;
1094*4882a593Smuzhiyun 		goto done;
1095*4882a593Smuzhiyun 	}
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	/* step 5: set up device parameter settings */
1098*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: setting PCIe registers\n", __func__);
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	/*
1101*4882a593Smuzhiyun 	 * PcieCfgSpcie1 - Link Control 3
1102*4882a593Smuzhiyun 	 * Leave at reset value.  No need to set PerfEq - link equalization
1103*4882a593Smuzhiyun 	 * will be performed automatically after the SBR when the target
1104*4882a593Smuzhiyun 	 * speed is 8GT/s.
1105*4882a593Smuzhiyun 	 */
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	/* clear all 16 per-lane error bits (PCIe: Lane Error Status) */
1108*4882a593Smuzhiyun 	pci_write_config_dword(dd->pcidev, PCIE_CFG_SPCIE2, 0xffff);
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	/* step 5a: Set Synopsys Port Logic registers */
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	/*
1113*4882a593Smuzhiyun 	 * PcieCfgRegPl2 - Port Force Link
1114*4882a593Smuzhiyun 	 *
1115*4882a593Smuzhiyun 	 * Set the low power field to 0x10 to avoid unnecessary power
1116*4882a593Smuzhiyun 	 * management messages.  All other fields are zero.
1117*4882a593Smuzhiyun 	 */
1118*4882a593Smuzhiyun 	reg32 = 0x10ul << PCIE_CFG_REG_PL2_LOW_PWR_ENT_CNT_SHIFT;
1119*4882a593Smuzhiyun 	pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL2, reg32);
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	/*
1122*4882a593Smuzhiyun 	 * PcieCfgRegPl100 - Gen3 Control
1123*4882a593Smuzhiyun 	 *
1124*4882a593Smuzhiyun 	 * turn off PcieCfgRegPl100.Gen3ZRxDcNonCompl
1125*4882a593Smuzhiyun 	 * turn on PcieCfgRegPl100.EqEieosCnt
1126*4882a593Smuzhiyun 	 * Everything else zero.
1127*4882a593Smuzhiyun 	 */
1128*4882a593Smuzhiyun 	reg32 = PCIE_CFG_REG_PL100_EQ_EIEOS_CNT_SMASK;
1129*4882a593Smuzhiyun 	pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL100, reg32);
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	/*
1132*4882a593Smuzhiyun 	 * PcieCfgRegPl101 - Gen3 EQ FS and LF
1133*4882a593Smuzhiyun 	 * PcieCfgRegPl102 - Gen3 EQ Presets to Coefficients Mapping
1134*4882a593Smuzhiyun 	 * PcieCfgRegPl103 - Gen3 EQ Preset Index
1135*4882a593Smuzhiyun 	 * PcieCfgRegPl105 - Gen3 EQ Status
1136*4882a593Smuzhiyun 	 *
1137*4882a593Smuzhiyun 	 * Give initial EQ settings.
1138*4882a593Smuzhiyun 	 */
1139*4882a593Smuzhiyun 	if (dd->pcidev->device == PCI_DEVICE_ID_INTEL0) { /* discrete */
1140*4882a593Smuzhiyun 		/* 1000mV, FS=24, LF = 8 */
1141*4882a593Smuzhiyun 		fs = 24;
1142*4882a593Smuzhiyun 		lf = 8;
1143*4882a593Smuzhiyun 		div = 3;
1144*4882a593Smuzhiyun 		eq = discrete_preliminary_eq;
1145*4882a593Smuzhiyun 		default_pset = DEFAULT_DISCRETE_PSET;
1146*4882a593Smuzhiyun 		ctle_tunings = discrete_ctle_tunings;
1147*4882a593Smuzhiyun 		/* bit 0 - discrete on/off */
1148*4882a593Smuzhiyun 		static_ctle_mode = pcie_ctle & 0x1;
1149*4882a593Smuzhiyun 	} else {
1150*4882a593Smuzhiyun 		/* 400mV, FS=29, LF = 9 */
1151*4882a593Smuzhiyun 		fs = 29;
1152*4882a593Smuzhiyun 		lf = 9;
1153*4882a593Smuzhiyun 		div = 1;
1154*4882a593Smuzhiyun 		eq = integrated_preliminary_eq;
1155*4882a593Smuzhiyun 		default_pset = DEFAULT_MCP_PSET;
1156*4882a593Smuzhiyun 		ctle_tunings = integrated_ctle_tunings;
1157*4882a593Smuzhiyun 		/* bit 1 - integrated on/off */
1158*4882a593Smuzhiyun 		static_ctle_mode = (pcie_ctle >> 1) & 0x1;
1159*4882a593Smuzhiyun 	}
1160*4882a593Smuzhiyun 	pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL101,
1161*4882a593Smuzhiyun 			       (fs <<
1162*4882a593Smuzhiyun 				PCIE_CFG_REG_PL101_GEN3_EQ_LOCAL_FS_SHIFT) |
1163*4882a593Smuzhiyun 			       (lf <<
1164*4882a593Smuzhiyun 				PCIE_CFG_REG_PL101_GEN3_EQ_LOCAL_LF_SHIFT));
1165*4882a593Smuzhiyun 	ret = load_eq_table(dd, eq, fs, div);
1166*4882a593Smuzhiyun 	if (ret)
1167*4882a593Smuzhiyun 		goto done;
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	/*
1170*4882a593Smuzhiyun 	 * PcieCfgRegPl106 - Gen3 EQ Control
1171*4882a593Smuzhiyun 	 *
1172*4882a593Smuzhiyun 	 * Set Gen3EqPsetReqVec, leave other fields 0.
1173*4882a593Smuzhiyun 	 */
1174*4882a593Smuzhiyun 	if (pset == UNSET_PSET)
1175*4882a593Smuzhiyun 		pset = default_pset;
1176*4882a593Smuzhiyun 	if (pset > 10) {	/* valid range is 0-10, inclusive */
1177*4882a593Smuzhiyun 		dd_dev_err(dd, "%s: Invalid Eq Pset %u, setting to %d\n",
1178*4882a593Smuzhiyun 			   __func__, pset, default_pset);
1179*4882a593Smuzhiyun 		pset = default_pset;
1180*4882a593Smuzhiyun 	}
1181*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: using EQ Pset %u\n", __func__, pset);
1182*4882a593Smuzhiyun 	pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL106,
1183*4882a593Smuzhiyun 			       ((1 << pset) <<
1184*4882a593Smuzhiyun 			PCIE_CFG_REG_PL106_GEN3_EQ_PSET_REQ_VEC_SHIFT) |
1185*4882a593Smuzhiyun 			PCIE_CFG_REG_PL106_GEN3_EQ_EVAL2MS_DISABLE_SMASK |
1186*4882a593Smuzhiyun 			PCIE_CFG_REG_PL106_GEN3_EQ_PHASE23_EXIT_MODE_SMASK);
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	/*
1189*4882a593Smuzhiyun 	 * step 5b: Do post firmware download steps via SBus
1190*4882a593Smuzhiyun 	 */
1191*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: doing pcie post steps\n", __func__);
1192*4882a593Smuzhiyun 	pcie_post_steps(dd);
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	/*
1195*4882a593Smuzhiyun 	 * step 5c: Program gasket interrupts
1196*4882a593Smuzhiyun 	 */
1197*4882a593Smuzhiyun 	/* set the Rx Bit Rate to REFCLK ratio */
1198*4882a593Smuzhiyun 	write_gasket_interrupt(dd, intnum++, 0x0006, 0x0050);
1199*4882a593Smuzhiyun 	/* disable pCal for PCIe Gen3 RX equalization */
1200*4882a593Smuzhiyun 	/* select adaptive or static CTLE */
1201*4882a593Smuzhiyun 	write_gasket_interrupt(dd, intnum++, 0x0026,
1202*4882a593Smuzhiyun 			       0x5b01 | (static_ctle_mode << 3));
1203*4882a593Smuzhiyun 	/*
1204*4882a593Smuzhiyun 	 * Enable iCal for PCIe Gen3 RX equalization, and set which
1205*4882a593Smuzhiyun 	 * evaluation of RX_EQ_EVAL will launch the iCal procedure.
1206*4882a593Smuzhiyun 	 */
1207*4882a593Smuzhiyun 	write_gasket_interrupt(dd, intnum++, 0x0026, 0x5202);
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	if (static_ctle_mode) {
1210*4882a593Smuzhiyun 		/* apply static CTLE tunings */
1211*4882a593Smuzhiyun 		u8 pcie_dc, pcie_lf, pcie_hf, pcie_bw;
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 		pcie_dc = ctle_tunings[pset][0];
1214*4882a593Smuzhiyun 		pcie_lf = ctle_tunings[pset][1];
1215*4882a593Smuzhiyun 		pcie_hf = ctle_tunings[pset][2];
1216*4882a593Smuzhiyun 		pcie_bw = ctle_tunings[pset][3];
1217*4882a593Smuzhiyun 		write_gasket_interrupt(dd, intnum++, 0x0026, 0x0200 | pcie_dc);
1218*4882a593Smuzhiyun 		write_gasket_interrupt(dd, intnum++, 0x0026, 0x0100 | pcie_lf);
1219*4882a593Smuzhiyun 		write_gasket_interrupt(dd, intnum++, 0x0026, 0x0000 | pcie_hf);
1220*4882a593Smuzhiyun 		write_gasket_interrupt(dd, intnum++, 0x0026, 0x5500 | pcie_bw);
1221*4882a593Smuzhiyun 	}
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun 	/* terminate list */
1224*4882a593Smuzhiyun 	write_gasket_interrupt(dd, intnum++, 0x0000, 0x0000);
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	/*
1227*4882a593Smuzhiyun 	 * step 5d: program XMT margin
1228*4882a593Smuzhiyun 	 */
1229*4882a593Smuzhiyun 	write_xmt_margin(dd, __func__);
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 	/*
1232*4882a593Smuzhiyun 	 * step 5e: disable active state power management (ASPM). It
1233*4882a593Smuzhiyun 	 * will be enabled if required later
1234*4882a593Smuzhiyun 	 */
1235*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: clearing ASPM\n", __func__);
1236*4882a593Smuzhiyun 	aspm_hw_disable_l1(dd);
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 	/*
1239*4882a593Smuzhiyun 	 * step 5f: clear DirectSpeedChange
1240*4882a593Smuzhiyun 	 * PcieCfgRegPl67.DirectSpeedChange must be zero to prevent the
1241*4882a593Smuzhiyun 	 * change in the speed target from starting before we are ready.
1242*4882a593Smuzhiyun 	 * This field defaults to 0 and we are not changing it, so nothing
1243*4882a593Smuzhiyun 	 * needs to be done.
1244*4882a593Smuzhiyun 	 */
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	/* step 5g: Set target link speed */
1247*4882a593Smuzhiyun 	/*
1248*4882a593Smuzhiyun 	 * Set target link speed to be target on both device and parent.
1249*4882a593Smuzhiyun 	 * On setting the parent: Some system BIOSs "helpfully" set the
1250*4882a593Smuzhiyun 	 * parent target speed to Gen2 to match the ASIC's initial speed.
1251*4882a593Smuzhiyun 	 * We can set the target Gen3 because we have already checked
1252*4882a593Smuzhiyun 	 * that it is Gen3 capable earlier.
1253*4882a593Smuzhiyun 	 */
1254*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: setting parent target link speed\n", __func__);
1255*4882a593Smuzhiyun 	ret = pcie_capability_read_word(parent, PCI_EXP_LNKCTL2, &lnkctl2);
1256*4882a593Smuzhiyun 	if (ret) {
1257*4882a593Smuzhiyun 		dd_dev_err(dd, "Unable to read from PCI config\n");
1258*4882a593Smuzhiyun 		return_error = 1;
1259*4882a593Smuzhiyun 		goto done;
1260*4882a593Smuzhiyun 	}
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: ..old link control2: 0x%x\n", __func__,
1263*4882a593Smuzhiyun 		    (u32)lnkctl2);
1264*4882a593Smuzhiyun 	/* only write to parent if target is not as high as ours */
1265*4882a593Smuzhiyun 	if ((lnkctl2 & PCI_EXP_LNKCTL2_TLS) < target_vector) {
1266*4882a593Smuzhiyun 		lnkctl2 &= ~PCI_EXP_LNKCTL2_TLS;
1267*4882a593Smuzhiyun 		lnkctl2 |= target_vector;
1268*4882a593Smuzhiyun 		dd_dev_info(dd, "%s: ..new link control2: 0x%x\n", __func__,
1269*4882a593Smuzhiyun 			    (u32)lnkctl2);
1270*4882a593Smuzhiyun 		ret = pcie_capability_write_word(parent,
1271*4882a593Smuzhiyun 						 PCI_EXP_LNKCTL2, lnkctl2);
1272*4882a593Smuzhiyun 		if (ret) {
1273*4882a593Smuzhiyun 			dd_dev_err(dd, "Unable to write to PCI config\n");
1274*4882a593Smuzhiyun 			return_error = 1;
1275*4882a593Smuzhiyun 			goto done;
1276*4882a593Smuzhiyun 		}
1277*4882a593Smuzhiyun 	} else {
1278*4882a593Smuzhiyun 		dd_dev_info(dd, "%s: ..target speed is OK\n", __func__);
1279*4882a593Smuzhiyun 	}
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: setting target link speed\n", __func__);
1282*4882a593Smuzhiyun 	ret = pcie_capability_read_word(dd->pcidev, PCI_EXP_LNKCTL2, &lnkctl2);
1283*4882a593Smuzhiyun 	if (ret) {
1284*4882a593Smuzhiyun 		dd_dev_err(dd, "Unable to read from PCI config\n");
1285*4882a593Smuzhiyun 		return_error = 1;
1286*4882a593Smuzhiyun 		goto done;
1287*4882a593Smuzhiyun 	}
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: ..old link control2: 0x%x\n", __func__,
1290*4882a593Smuzhiyun 		    (u32)lnkctl2);
1291*4882a593Smuzhiyun 	lnkctl2 &= ~PCI_EXP_LNKCTL2_TLS;
1292*4882a593Smuzhiyun 	lnkctl2 |= target_vector;
1293*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: ..new link control2: 0x%x\n", __func__,
1294*4882a593Smuzhiyun 		    (u32)lnkctl2);
1295*4882a593Smuzhiyun 	ret = pcie_capability_write_word(dd->pcidev, PCI_EXP_LNKCTL2, lnkctl2);
1296*4882a593Smuzhiyun 	if (ret) {
1297*4882a593Smuzhiyun 		dd_dev_err(dd, "Unable to write to PCI config\n");
1298*4882a593Smuzhiyun 		return_error = 1;
1299*4882a593Smuzhiyun 		goto done;
1300*4882a593Smuzhiyun 	}
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	/* step 5h: arm gasket logic */
1303*4882a593Smuzhiyun 	/* hold DC in reset across the SBR */
1304*4882a593Smuzhiyun 	write_csr(dd, CCE_DC_CTRL, CCE_DC_CTRL_DC_RESET_SMASK);
1305*4882a593Smuzhiyun 	(void)read_csr(dd, CCE_DC_CTRL); /* DC reset hold */
1306*4882a593Smuzhiyun 	/* save firmware control across the SBR */
1307*4882a593Smuzhiyun 	fw_ctrl = read_csr(dd, MISC_CFG_FW_CTRL);
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: arming gasket logic\n", __func__);
1310*4882a593Smuzhiyun 	arm_gasket_logic(dd);
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	/*
1313*4882a593Smuzhiyun 	 * step 6: quiesce PCIe link
1314*4882a593Smuzhiyun 	 * The chip has already been reset, so there will be no traffic
1315*4882a593Smuzhiyun 	 * from the chip.  Linux has no easy way to enforce that it will
1316*4882a593Smuzhiyun 	 * not try to access the device, so we just need to hope it doesn't
1317*4882a593Smuzhiyun 	 * do it while we are doing the reset.
1318*4882a593Smuzhiyun 	 */
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 	/*
1321*4882a593Smuzhiyun 	 * step 7: initiate the secondary bus reset (SBR)
1322*4882a593Smuzhiyun 	 * step 8: hardware brings the links back up
1323*4882a593Smuzhiyun 	 * step 9: wait for link speed transition to be complete
1324*4882a593Smuzhiyun 	 */
1325*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: calling trigger_sbr\n", __func__);
1326*4882a593Smuzhiyun 	ret = trigger_sbr(dd);
1327*4882a593Smuzhiyun 	if (ret)
1328*4882a593Smuzhiyun 		goto done;
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun 	/* step 10: decide what to do next */
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	/* check if we can read PCI space */
1333*4882a593Smuzhiyun 	ret = pci_read_config_word(dd->pcidev, PCI_VENDOR_ID, &vendor);
1334*4882a593Smuzhiyun 	if (ret) {
1335*4882a593Smuzhiyun 		dd_dev_info(dd,
1336*4882a593Smuzhiyun 			    "%s: read of VendorID failed after SBR, err %d\n",
1337*4882a593Smuzhiyun 			    __func__, ret);
1338*4882a593Smuzhiyun 		return_error = 1;
1339*4882a593Smuzhiyun 		goto done;
1340*4882a593Smuzhiyun 	}
1341*4882a593Smuzhiyun 	if (vendor == 0xffff) {
1342*4882a593Smuzhiyun 		dd_dev_info(dd, "%s: VendorID is all 1s after SBR\n", __func__);
1343*4882a593Smuzhiyun 		return_error = 1;
1344*4882a593Smuzhiyun 		ret = -EIO;
1345*4882a593Smuzhiyun 		goto done;
1346*4882a593Smuzhiyun 	}
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 	/* restore PCI space registers we know were reset */
1349*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: calling restore_pci_variables\n", __func__);
1350*4882a593Smuzhiyun 	ret = restore_pci_variables(dd);
1351*4882a593Smuzhiyun 	if (ret) {
1352*4882a593Smuzhiyun 		dd_dev_err(dd, "%s: Could not restore PCI variables\n",
1353*4882a593Smuzhiyun 			   __func__);
1354*4882a593Smuzhiyun 		return_error = 1;
1355*4882a593Smuzhiyun 		goto done;
1356*4882a593Smuzhiyun 	}
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	/* restore firmware control */
1359*4882a593Smuzhiyun 	write_csr(dd, MISC_CFG_FW_CTRL, fw_ctrl);
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	/*
1362*4882a593Smuzhiyun 	 * Check the gasket block status.
1363*4882a593Smuzhiyun 	 *
1364*4882a593Smuzhiyun 	 * This is the first CSR read after the SBR.  If the read returns
1365*4882a593Smuzhiyun 	 * all 1s (fails), the link did not make it back.
1366*4882a593Smuzhiyun 	 *
1367*4882a593Smuzhiyun 	 * Once we're sure we can read and write, clear the DC reset after
1368*4882a593Smuzhiyun 	 * the SBR.  Then check for any per-lane errors. Then look over
1369*4882a593Smuzhiyun 	 * the status.
1370*4882a593Smuzhiyun 	 */
1371*4882a593Smuzhiyun 	reg = read_csr(dd, ASIC_PCIE_SD_HOST_STATUS);
1372*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: gasket block status: 0x%llx\n", __func__, reg);
1373*4882a593Smuzhiyun 	if (reg == ~0ull) {	/* PCIe read failed/timeout */
1374*4882a593Smuzhiyun 		dd_dev_err(dd, "SBR failed - unable to read from device\n");
1375*4882a593Smuzhiyun 		return_error = 1;
1376*4882a593Smuzhiyun 		ret = -ENOSYS;
1377*4882a593Smuzhiyun 		goto done;
1378*4882a593Smuzhiyun 	}
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 	/* clear the DC reset */
1381*4882a593Smuzhiyun 	write_csr(dd, CCE_DC_CTRL, 0);
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	/* Set the LED off */
1384*4882a593Smuzhiyun 	setextled(dd, 0);
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 	/* check for any per-lane errors */
1387*4882a593Smuzhiyun 	ret = pci_read_config_dword(dd->pcidev, PCIE_CFG_SPCIE2, &reg32);
1388*4882a593Smuzhiyun 	if (ret) {
1389*4882a593Smuzhiyun 		dd_dev_err(dd, "Unable to read from PCI config\n");
1390*4882a593Smuzhiyun 		return_error = 1;
1391*4882a593Smuzhiyun 		goto done;
1392*4882a593Smuzhiyun 	}
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: per-lane errors: 0x%x\n", __func__, reg32);
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	/* extract status, look for our HFI */
1397*4882a593Smuzhiyun 	status = (reg >> ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_STS_SHIFT)
1398*4882a593Smuzhiyun 			& ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_STS_MASK;
1399*4882a593Smuzhiyun 	if ((status & (1 << dd->hfi1_id)) == 0) {
1400*4882a593Smuzhiyun 		dd_dev_err(dd,
1401*4882a593Smuzhiyun 			   "%s: gasket status 0x%x, expecting 0x%x\n",
1402*4882a593Smuzhiyun 			   __func__, status, 1 << dd->hfi1_id);
1403*4882a593Smuzhiyun 		ret = -EIO;
1404*4882a593Smuzhiyun 		goto done;
1405*4882a593Smuzhiyun 	}
1406*4882a593Smuzhiyun 
1407*4882a593Smuzhiyun 	/* extract error */
1408*4882a593Smuzhiyun 	err = (reg >> ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_ERR_SHIFT)
1409*4882a593Smuzhiyun 		& ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_ERR_MASK;
1410*4882a593Smuzhiyun 	if (err) {
1411*4882a593Smuzhiyun 		dd_dev_err(dd, "%s: gasket error %d\n", __func__, err);
1412*4882a593Smuzhiyun 		ret = -EIO;
1413*4882a593Smuzhiyun 		goto done;
1414*4882a593Smuzhiyun 	}
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	/* update our link information cache */
1417*4882a593Smuzhiyun 	update_lbus_info(dd);
1418*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: new speed and width: %s\n", __func__,
1419*4882a593Smuzhiyun 		    dd->lbus_info);
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 	if (dd->lbus_speed != target_speed ||
1422*4882a593Smuzhiyun 	    dd->lbus_width < target_width) { /* not target */
1423*4882a593Smuzhiyun 		/* maybe retry */
1424*4882a593Smuzhiyun 		do_retry = retry_count < pcie_retry;
1425*4882a593Smuzhiyun 		dd_dev_err(dd, "PCIe link speed or width did not match target%s\n",
1426*4882a593Smuzhiyun 			   do_retry ? ", retrying" : "");
1427*4882a593Smuzhiyun 		retry_count++;
1428*4882a593Smuzhiyun 		if (do_retry) {
1429*4882a593Smuzhiyun 			msleep(100); /* allow time to settle */
1430*4882a593Smuzhiyun 			goto retry;
1431*4882a593Smuzhiyun 		}
1432*4882a593Smuzhiyun 		ret = -EIO;
1433*4882a593Smuzhiyun 	}
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun done:
1436*4882a593Smuzhiyun 	if (therm) {
1437*4882a593Smuzhiyun 		write_csr(dd, ASIC_CFG_THERM_POLL_EN, 0x1);
1438*4882a593Smuzhiyun 		msleep(100);
1439*4882a593Smuzhiyun 		dd_dev_info(dd, "%s: Re-enable therm polling\n",
1440*4882a593Smuzhiyun 			    __func__);
1441*4882a593Smuzhiyun 	}
1442*4882a593Smuzhiyun 	release_chip_resource(dd, CR_SBUS);
1443*4882a593Smuzhiyun done_no_mutex:
1444*4882a593Smuzhiyun 	/* return no error if it is OK to be at current speed */
1445*4882a593Smuzhiyun 	if (ret && !return_error) {
1446*4882a593Smuzhiyun 		dd_dev_err(dd, "Proceeding at current speed PCIe speed\n");
1447*4882a593Smuzhiyun 		ret = 0;
1448*4882a593Smuzhiyun 	}
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	dd_dev_info(dd, "%s: done\n", __func__);
1451*4882a593Smuzhiyun 	return ret;
1452*4882a593Smuzhiyun }
1453