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