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