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