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