xref: /linux/drivers/infiniband/hw/qib/qib_pcie.c (revision b43ab901d671e3e3cad425ea5e9a3c74e266dcdd)
1 /*
2  * Copyright (c) 2008, 2009 QLogic Corporation. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/pci.h>
34 #include <linux/io.h>
35 #include <linux/delay.h>
36 #include <linux/vmalloc.h>
37 #include <linux/aer.h>
38 #include <linux/module.h>
39 
40 #include "qib.h"
41 
42 /*
43  * This file contains PCIe utility routines that are common to the
44  * various QLogic InfiniPath adapters
45  */
46 
47 /*
48  * Code to adjust PCIe capabilities.
49  * To minimize the change footprint, we call it
50  * from qib_pcie_params, which every chip-specific
51  * file calls, even though this violates some
52  * expectations of harmlessness.
53  */
54 static int qib_tune_pcie_caps(struct qib_devdata *);
55 static int qib_tune_pcie_coalesce(struct qib_devdata *);
56 
57 /*
58  * Do all the common PCIe setup and initialization.
59  * devdata is not yet allocated, and is not allocated until after this
60  * routine returns success.  Therefore qib_dev_err() can't be used for error
61  * printing.
62  */
63 int qib_pcie_init(struct pci_dev *pdev, const struct pci_device_id *ent)
64 {
65 	int ret;
66 
67 	ret = pci_enable_device(pdev);
68 	if (ret) {
69 		/*
70 		 * This can happen (in theory) iff:
71 		 * We did a chip reset, and then failed to reprogram the
72 		 * BAR, or the chip reset due to an internal error.  We then
73 		 * unloaded the driver and reloaded it.
74 		 *
75 		 * Both reset cases set the BAR back to initial state.  For
76 		 * the latter case, the AER sticky error bit at offset 0x718
77 		 * should be set, but the Linux kernel doesn't yet know
78 		 * about that, it appears.  If the original BAR was retained
79 		 * in the kernel data structures, this may be OK.
80 		 */
81 		qib_early_err(&pdev->dev, "pci enable failed: error %d\n",
82 			      -ret);
83 		goto done;
84 	}
85 
86 	ret = pci_request_regions(pdev, QIB_DRV_NAME);
87 	if (ret) {
88 		qib_devinfo(pdev, "pci_request_regions fails: err %d\n", -ret);
89 		goto bail;
90 	}
91 
92 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
93 	if (ret) {
94 		/*
95 		 * If the 64 bit setup fails, try 32 bit.  Some systems
96 		 * do not setup 64 bit maps on systems with 2GB or less
97 		 * memory installed.
98 		 */
99 		ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
100 		if (ret) {
101 			qib_devinfo(pdev, "Unable to set DMA mask: %d\n", ret);
102 			goto bail;
103 		}
104 		ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
105 	} else
106 		ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
107 	if (ret) {
108 		qib_early_err(&pdev->dev,
109 			      "Unable to set DMA consistent mask: %d\n", ret);
110 		goto bail;
111 	}
112 
113 	pci_set_master(pdev);
114 	ret = pci_enable_pcie_error_reporting(pdev);
115 	if (ret) {
116 		qib_early_err(&pdev->dev,
117 			      "Unable to enable pcie error reporting: %d\n",
118 			      ret);
119 		ret = 0;
120 	}
121 	goto done;
122 
123 bail:
124 	pci_disable_device(pdev);
125 	pci_release_regions(pdev);
126 done:
127 	return ret;
128 }
129 
130 /*
131  * Do remaining PCIe setup, once dd is allocated, and save away
132  * fields required to re-initialize after a chip reset, or for
133  * various other purposes
134  */
135 int qib_pcie_ddinit(struct qib_devdata *dd, struct pci_dev *pdev,
136 		    const struct pci_device_id *ent)
137 {
138 	unsigned long len;
139 	resource_size_t addr;
140 
141 	dd->pcidev = pdev;
142 	pci_set_drvdata(pdev, dd);
143 
144 	addr = pci_resource_start(pdev, 0);
145 	len = pci_resource_len(pdev, 0);
146 
147 #if defined(__powerpc__)
148 	/* There isn't a generic way to specify writethrough mappings */
149 	dd->kregbase = __ioremap(addr, len, _PAGE_NO_CACHE | _PAGE_WRITETHRU);
150 #else
151 	dd->kregbase = ioremap_nocache(addr, len);
152 #endif
153 
154 	if (!dd->kregbase)
155 		return -ENOMEM;
156 
157 	dd->kregend = (u64 __iomem *)((void __iomem *) dd->kregbase + len);
158 	dd->physaddr = addr;        /* used for io_remap, etc. */
159 
160 	/*
161 	 * Save BARs to rewrite after device reset.  Save all 64 bits of
162 	 * BAR, just in case.
163 	 */
164 	dd->pcibar0 = addr;
165 	dd->pcibar1 = addr >> 32;
166 	dd->deviceid = ent->device; /* save for later use */
167 	dd->vendorid = ent->vendor;
168 
169 	return 0;
170 }
171 
172 /*
173  * Do PCIe cleanup, after chip-specific cleanup, etc.  Just prior
174  * to releasing the dd memory.
175  * void because none of the core pcie cleanup returns are void
176  */
177 void qib_pcie_ddcleanup(struct qib_devdata *dd)
178 {
179 	u64 __iomem *base = (void __iomem *) dd->kregbase;
180 
181 	dd->kregbase = NULL;
182 	iounmap(base);
183 	if (dd->piobase)
184 		iounmap(dd->piobase);
185 	if (dd->userbase)
186 		iounmap(dd->userbase);
187 	if (dd->piovl15base)
188 		iounmap(dd->piovl15base);
189 
190 	pci_disable_device(dd->pcidev);
191 	pci_release_regions(dd->pcidev);
192 
193 	pci_set_drvdata(dd->pcidev, NULL);
194 }
195 
196 static void qib_msix_setup(struct qib_devdata *dd, int pos, u32 *msixcnt,
197 			   struct msix_entry *msix_entry)
198 {
199 	int ret;
200 	u32 tabsize = 0;
201 	u16 msix_flags;
202 
203 	pci_read_config_word(dd->pcidev, pos + PCI_MSIX_FLAGS, &msix_flags);
204 	tabsize = 1 + (msix_flags & PCI_MSIX_FLAGS_QSIZE);
205 	if (tabsize > *msixcnt)
206 		tabsize = *msixcnt;
207 	ret = pci_enable_msix(dd->pcidev, msix_entry, tabsize);
208 	if (ret > 0) {
209 		tabsize = ret;
210 		ret = pci_enable_msix(dd->pcidev, msix_entry, tabsize);
211 	}
212 	if (ret) {
213 		qib_dev_err(dd, "pci_enable_msix %d vectors failed: %d, "
214 			    "falling back to INTx\n", tabsize, ret);
215 		tabsize = 0;
216 	}
217 	*msixcnt = tabsize;
218 
219 	if (ret)
220 		qib_enable_intx(dd->pcidev);
221 
222 }
223 
224 /**
225  * We save the msi lo and hi values, so we can restore them after
226  * chip reset (the kernel PCI infrastructure doesn't yet handle that
227  * correctly.
228  */
229 static int qib_msi_setup(struct qib_devdata *dd, int pos)
230 {
231 	struct pci_dev *pdev = dd->pcidev;
232 	u16 control;
233 	int ret;
234 
235 	ret = pci_enable_msi(pdev);
236 	if (ret)
237 		qib_dev_err(dd, "pci_enable_msi failed: %d, "
238 			    "interrupts may not work\n", ret);
239 	/* continue even if it fails, we may still be OK... */
240 
241 	pci_read_config_dword(pdev, pos + PCI_MSI_ADDRESS_LO,
242 			      &dd->msi_lo);
243 	pci_read_config_dword(pdev, pos + PCI_MSI_ADDRESS_HI,
244 			      &dd->msi_hi);
245 	pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &control);
246 	/* now save the data (vector) info */
247 	pci_read_config_word(pdev, pos + ((control & PCI_MSI_FLAGS_64BIT)
248 				    ? 12 : 8),
249 			     &dd->msi_data);
250 	return ret;
251 }
252 
253 int qib_pcie_params(struct qib_devdata *dd, u32 minw, u32 *nent,
254 		    struct msix_entry *entry)
255 {
256 	u16 linkstat, speed;
257 	int pos = 0, pose, ret = 1;
258 
259 	pose = pci_pcie_cap(dd->pcidev);
260 	if (!pose) {
261 		qib_dev_err(dd, "Can't find PCI Express capability!\n");
262 		/* set up something... */
263 		dd->lbus_width = 1;
264 		dd->lbus_speed = 2500; /* Gen1, 2.5GHz */
265 		goto bail;
266 	}
267 
268 	pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSIX);
269 	if (nent && *nent && pos) {
270 		qib_msix_setup(dd, pos, nent, entry);
271 		ret = 0; /* did it, either MSIx or INTx */
272 	} else {
273 		pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSI);
274 		if (pos)
275 			ret = qib_msi_setup(dd, pos);
276 		else
277 			qib_dev_err(dd, "No PCI MSI or MSIx capability!\n");
278 	}
279 	if (!pos)
280 		qib_enable_intx(dd->pcidev);
281 
282 	pci_read_config_word(dd->pcidev, pose + PCI_EXP_LNKSTA, &linkstat);
283 	/*
284 	 * speed is bits 0-3, linkwidth is bits 4-8
285 	 * no defines for them in headers
286 	 */
287 	speed = linkstat & 0xf;
288 	linkstat >>= 4;
289 	linkstat &= 0x1f;
290 	dd->lbus_width = linkstat;
291 
292 	switch (speed) {
293 	case 1:
294 		dd->lbus_speed = 2500; /* Gen1, 2.5GHz */
295 		break;
296 	case 2:
297 		dd->lbus_speed = 5000; /* Gen1, 5GHz */
298 		break;
299 	default: /* not defined, assume gen1 */
300 		dd->lbus_speed = 2500;
301 		break;
302 	}
303 
304 	/*
305 	 * Check against expected pcie width and complain if "wrong"
306 	 * on first initialization, not afterwards (i.e., reset).
307 	 */
308 	if (minw && linkstat < minw)
309 		qib_dev_err(dd,
310 			    "PCIe width %u (x%u HCA), performance reduced\n",
311 			    linkstat, minw);
312 
313 	qib_tune_pcie_caps(dd);
314 
315 	qib_tune_pcie_coalesce(dd);
316 
317 bail:
318 	/* fill in string, even on errors */
319 	snprintf(dd->lbus_info, sizeof(dd->lbus_info),
320 		 "PCIe,%uMHz,x%u\n", dd->lbus_speed, dd->lbus_width);
321 	return ret;
322 }
323 
324 /*
325  * Setup pcie interrupt stuff again after a reset.  I'd like to just call
326  * pci_enable_msi() again for msi, but when I do that,
327  * the MSI enable bit doesn't get set in the command word, and
328  * we switch to to a different interrupt vector, which is confusing,
329  * so I instead just do it all inline.  Perhaps somehow can tie this
330  * into the PCIe hotplug support at some point
331  */
332 int qib_reinit_intr(struct qib_devdata *dd)
333 {
334 	int pos;
335 	u16 control;
336 	int ret = 0;
337 
338 	/* If we aren't using MSI, don't restore it */
339 	if (!dd->msi_lo)
340 		goto bail;
341 
342 	pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSI);
343 	if (!pos) {
344 		qib_dev_err(dd, "Can't find MSI capability, "
345 			    "can't restore MSI settings\n");
346 		ret = 0;
347 		/* nothing special for MSIx, just MSI */
348 		goto bail;
349 	}
350 	pci_write_config_dword(dd->pcidev, pos + PCI_MSI_ADDRESS_LO,
351 			       dd->msi_lo);
352 	pci_write_config_dword(dd->pcidev, pos + PCI_MSI_ADDRESS_HI,
353 			       dd->msi_hi);
354 	pci_read_config_word(dd->pcidev, pos + PCI_MSI_FLAGS, &control);
355 	if (!(control & PCI_MSI_FLAGS_ENABLE)) {
356 		control |= PCI_MSI_FLAGS_ENABLE;
357 		pci_write_config_word(dd->pcidev, pos + PCI_MSI_FLAGS,
358 				      control);
359 	}
360 	/* now rewrite the data (vector) info */
361 	pci_write_config_word(dd->pcidev, pos +
362 			      ((control & PCI_MSI_FLAGS_64BIT) ? 12 : 8),
363 			      dd->msi_data);
364 	ret = 1;
365 bail:
366 	if (!ret && (dd->flags & QIB_HAS_INTX)) {
367 		qib_enable_intx(dd->pcidev);
368 		ret = 1;
369 	}
370 
371 	/* and now set the pci master bit again */
372 	pci_set_master(dd->pcidev);
373 
374 	return ret;
375 }
376 
377 /*
378  * Disable msi interrupt if enabled, and clear msi_lo.
379  * This is used primarily for the fallback to INTx, but
380  * is also used in reinit after reset, and during cleanup.
381  */
382 void qib_nomsi(struct qib_devdata *dd)
383 {
384 	dd->msi_lo = 0;
385 	pci_disable_msi(dd->pcidev);
386 }
387 
388 /*
389  * Same as qib_nosmi, but for MSIx.
390  */
391 void qib_nomsix(struct qib_devdata *dd)
392 {
393 	pci_disable_msix(dd->pcidev);
394 }
395 
396 /*
397  * Similar to pci_intx(pdev, 1), except that we make sure
398  * msi(x) is off.
399  */
400 void qib_enable_intx(struct pci_dev *pdev)
401 {
402 	u16 cw, new;
403 	int pos;
404 
405 	/* first, turn on INTx */
406 	pci_read_config_word(pdev, PCI_COMMAND, &cw);
407 	new = cw & ~PCI_COMMAND_INTX_DISABLE;
408 	if (new != cw)
409 		pci_write_config_word(pdev, PCI_COMMAND, new);
410 
411 	pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
412 	if (pos) {
413 		/* then turn off MSI */
414 		pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &cw);
415 		new = cw & ~PCI_MSI_FLAGS_ENABLE;
416 		if (new != cw)
417 			pci_write_config_word(pdev, pos + PCI_MSI_FLAGS, new);
418 	}
419 	pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
420 	if (pos) {
421 		/* then turn off MSIx */
422 		pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &cw);
423 		new = cw & ~PCI_MSIX_FLAGS_ENABLE;
424 		if (new != cw)
425 			pci_write_config_word(pdev, pos + PCI_MSIX_FLAGS, new);
426 	}
427 }
428 
429 /*
430  * These two routines are helper routines for the device reset code
431  * to move all the pcie code out of the chip-specific driver code.
432  */
433 void qib_pcie_getcmd(struct qib_devdata *dd, u16 *cmd, u8 *iline, u8 *cline)
434 {
435 	pci_read_config_word(dd->pcidev, PCI_COMMAND, cmd);
436 	pci_read_config_byte(dd->pcidev, PCI_INTERRUPT_LINE, iline);
437 	pci_read_config_byte(dd->pcidev, PCI_CACHE_LINE_SIZE, cline);
438 }
439 
440 void qib_pcie_reenable(struct qib_devdata *dd, u16 cmd, u8 iline, u8 cline)
441 {
442 	int r;
443 	r = pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_0,
444 				   dd->pcibar0);
445 	if (r)
446 		qib_dev_err(dd, "rewrite of BAR0 failed: %d\n", r);
447 	r = pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_1,
448 				   dd->pcibar1);
449 	if (r)
450 		qib_dev_err(dd, "rewrite of BAR1 failed: %d\n", r);
451 	/* now re-enable memory access, and restore cosmetic settings */
452 	pci_write_config_word(dd->pcidev, PCI_COMMAND, cmd);
453 	pci_write_config_byte(dd->pcidev, PCI_INTERRUPT_LINE, iline);
454 	pci_write_config_byte(dd->pcidev, PCI_CACHE_LINE_SIZE, cline);
455 	r = pci_enable_device(dd->pcidev);
456 	if (r)
457 		qib_dev_err(dd, "pci_enable_device failed after "
458 			    "reset: %d\n", r);
459 }
460 
461 /* code to adjust PCIe capabilities. */
462 
463 static int fld2val(int wd, int mask)
464 {
465 	int lsbmask;
466 
467 	if (!mask)
468 		return 0;
469 	wd &= mask;
470 	lsbmask = mask ^ (mask & (mask - 1));
471 	wd /= lsbmask;
472 	return wd;
473 }
474 
475 static int val2fld(int wd, int mask)
476 {
477 	int lsbmask;
478 
479 	if (!mask)
480 		return 0;
481 	lsbmask = mask ^ (mask & (mask - 1));
482 	wd *= lsbmask;
483 	return wd;
484 }
485 
486 static int qib_pcie_coalesce;
487 module_param_named(pcie_coalesce, qib_pcie_coalesce, int, S_IRUGO);
488 MODULE_PARM_DESC(pcie_coalesce, "tune PCIe colescing on some Intel chipsets");
489 
490 /*
491  * Enable PCIe completion and data coalescing, on Intel 5x00 and 7300
492  * chipsets.   This is known to be unsafe for some revisions of some
493  * of these chipsets, with some BIOS settings, and enabling it on those
494  * systems may result in the system crashing, and/or data corruption.
495  */
496 static int qib_tune_pcie_coalesce(struct qib_devdata *dd)
497 {
498 	int r;
499 	struct pci_dev *parent;
500 	int ppos;
501 	u16 devid;
502 	u32 mask, bits, val;
503 
504 	if (!qib_pcie_coalesce)
505 		return 0;
506 
507 	/* Find out supported and configured values for parent (root) */
508 	parent = dd->pcidev->bus->self;
509 	if (parent->bus->parent) {
510 		qib_devinfo(dd->pcidev, "Parent not root\n");
511 		return 1;
512 	}
513 	ppos = pci_pcie_cap(parent);
514 	if (!ppos)
515 		return 1;
516 	if (parent->vendor != 0x8086)
517 		return 1;
518 
519 	/*
520 	 *  - bit 12: Max_rdcmp_Imt_EN: need to set to 1
521 	 *  - bit 11: COALESCE_FORCE: need to set to 0
522 	 *  - bit 10: COALESCE_EN: need to set to 1
523 	 *  (but limitations on some on some chipsets)
524 	 *
525 	 *  On the Intel 5000, 5100, and 7300 chipsets, there is
526 	 *  also: - bit 25:24: COALESCE_MODE, need to set to 0
527 	 */
528 	devid = parent->device;
529 	if (devid >= 0x25e2 && devid <= 0x25fa) {
530 		/* 5000 P/V/X/Z */
531 		if (parent->revision <= 0xb2)
532 			bits = 1U << 10;
533 		else
534 			bits = 7U << 10;
535 		mask = (3U << 24) | (7U << 10);
536 	} else if (devid >= 0x65e2 && devid <= 0x65fa) {
537 		/* 5100 */
538 		bits = 1U << 10;
539 		mask = (3U << 24) | (7U << 10);
540 	} else if (devid >= 0x4021 && devid <= 0x402e) {
541 		/* 5400 */
542 		bits = 7U << 10;
543 		mask = 7U << 10;
544 	} else if (devid >= 0x3604 && devid <= 0x360a) {
545 		/* 7300 */
546 		bits = 7U << 10;
547 		mask = (3U << 24) | (7U << 10);
548 	} else {
549 		/* not one of the chipsets that we know about */
550 		return 1;
551 	}
552 	pci_read_config_dword(parent, 0x48, &val);
553 	val &= ~mask;
554 	val |= bits;
555 	r = pci_write_config_dword(parent, 0x48, val);
556 	return 0;
557 }
558 
559 /*
560  * BIOS may not set PCIe bus-utilization parameters for best performance.
561  * Check and optionally adjust them to maximize our throughput.
562  */
563 static int qib_pcie_caps = 0x51;
564 module_param_named(pcie_caps, qib_pcie_caps, int, S_IRUGO);
565 MODULE_PARM_DESC(pcie_caps, "Max PCIe tuning: Payload (0..3), ReadReq (4..7)");
566 
567 static int qib_tune_pcie_caps(struct qib_devdata *dd)
568 {
569 	int ret = 1; /* Assume the worst */
570 	struct pci_dev *parent;
571 	int ppos, epos;
572 	u16 pcaps, pctl, ecaps, ectl;
573 	int rc_sup, ep_sup;
574 	int rc_cur, ep_cur;
575 
576 	/* Find out supported and configured values for parent (root) */
577 	parent = dd->pcidev->bus->self;
578 	if (parent->bus->parent) {
579 		qib_devinfo(dd->pcidev, "Parent not root\n");
580 		goto bail;
581 	}
582 	ppos = pci_pcie_cap(parent);
583 	if (ppos) {
584 		pci_read_config_word(parent, ppos + PCI_EXP_DEVCAP, &pcaps);
585 		pci_read_config_word(parent, ppos + PCI_EXP_DEVCTL, &pctl);
586 	} else
587 		goto bail;
588 	/* Find out supported and configured values for endpoint (us) */
589 	epos = pci_pcie_cap(dd->pcidev);
590 	if (epos) {
591 		pci_read_config_word(dd->pcidev, epos + PCI_EXP_DEVCAP, &ecaps);
592 		pci_read_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, &ectl);
593 	} else
594 		goto bail;
595 	ret = 0;
596 	/* Find max payload supported by root, endpoint */
597 	rc_sup = fld2val(pcaps, PCI_EXP_DEVCAP_PAYLOAD);
598 	ep_sup = fld2val(ecaps, PCI_EXP_DEVCAP_PAYLOAD);
599 	if (rc_sup > ep_sup)
600 		rc_sup = ep_sup;
601 
602 	rc_cur = fld2val(pctl, PCI_EXP_DEVCTL_PAYLOAD);
603 	ep_cur = fld2val(ectl, PCI_EXP_DEVCTL_PAYLOAD);
604 
605 	/* If Supported greater than limit in module param, limit it */
606 	if (rc_sup > (qib_pcie_caps & 7))
607 		rc_sup = qib_pcie_caps & 7;
608 	/* If less than (allowed, supported), bump root payload */
609 	if (rc_sup > rc_cur) {
610 		rc_cur = rc_sup;
611 		pctl = (pctl & ~PCI_EXP_DEVCTL_PAYLOAD) |
612 			val2fld(rc_cur, PCI_EXP_DEVCTL_PAYLOAD);
613 		pci_write_config_word(parent, ppos + PCI_EXP_DEVCTL, pctl);
614 	}
615 	/* If less than (allowed, supported), bump endpoint payload */
616 	if (rc_sup > ep_cur) {
617 		ep_cur = rc_sup;
618 		ectl = (ectl & ~PCI_EXP_DEVCTL_PAYLOAD) |
619 			val2fld(ep_cur, PCI_EXP_DEVCTL_PAYLOAD);
620 		pci_write_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, ectl);
621 	}
622 
623 	/*
624 	 * Now the Read Request size.
625 	 * No field for max supported, but PCIe spec limits it to 4096,
626 	 * which is code '5' (log2(4096) - 7)
627 	 */
628 	rc_sup = 5;
629 	if (rc_sup > ((qib_pcie_caps >> 4) & 7))
630 		rc_sup = (qib_pcie_caps >> 4) & 7;
631 	rc_cur = fld2val(pctl, PCI_EXP_DEVCTL_READRQ);
632 	ep_cur = fld2val(ectl, PCI_EXP_DEVCTL_READRQ);
633 
634 	if (rc_sup > rc_cur) {
635 		rc_cur = rc_sup;
636 		pctl = (pctl & ~PCI_EXP_DEVCTL_READRQ) |
637 			val2fld(rc_cur, PCI_EXP_DEVCTL_READRQ);
638 		pci_write_config_word(parent, ppos + PCI_EXP_DEVCTL, pctl);
639 	}
640 	if (rc_sup > ep_cur) {
641 		ep_cur = rc_sup;
642 		ectl = (ectl & ~PCI_EXP_DEVCTL_READRQ) |
643 			val2fld(ep_cur, PCI_EXP_DEVCTL_READRQ);
644 		pci_write_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, ectl);
645 	}
646 bail:
647 	return ret;
648 }
649 /* End of PCIe capability tuning */
650 
651 /*
652  * From here through qib_pci_err_handler definition is invoked via
653  * PCI error infrastructure, registered via pci
654  */
655 static pci_ers_result_t
656 qib_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
657 {
658 	struct qib_devdata *dd = pci_get_drvdata(pdev);
659 	pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
660 
661 	switch (state) {
662 	case pci_channel_io_normal:
663 		qib_devinfo(pdev, "State Normal, ignoring\n");
664 		break;
665 
666 	case pci_channel_io_frozen:
667 		qib_devinfo(pdev, "State Frozen, requesting reset\n");
668 		pci_disable_device(pdev);
669 		ret = PCI_ERS_RESULT_NEED_RESET;
670 		break;
671 
672 	case pci_channel_io_perm_failure:
673 		qib_devinfo(pdev, "State Permanent Failure, disabling\n");
674 		if (dd) {
675 			/* no more register accesses! */
676 			dd->flags &= ~QIB_PRESENT;
677 			qib_disable_after_error(dd);
678 		}
679 		 /* else early, or other problem */
680 		ret =  PCI_ERS_RESULT_DISCONNECT;
681 		break;
682 
683 	default: /* shouldn't happen */
684 		qib_devinfo(pdev, "QIB PCI errors detected (state %d)\n",
685 			state);
686 		break;
687 	}
688 	return ret;
689 }
690 
691 static pci_ers_result_t
692 qib_pci_mmio_enabled(struct pci_dev *pdev)
693 {
694 	u64 words = 0U;
695 	struct qib_devdata *dd = pci_get_drvdata(pdev);
696 	pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
697 
698 	if (dd && dd->pport) {
699 		words = dd->f_portcntr(dd->pport, QIBPORTCNTR_WORDRCV);
700 		if (words == ~0ULL)
701 			ret = PCI_ERS_RESULT_NEED_RESET;
702 	}
703 	qib_devinfo(pdev, "QIB mmio_enabled function called, "
704 		 "read wordscntr %Lx, returning %d\n", words, ret);
705 	return  ret;
706 }
707 
708 static pci_ers_result_t
709 qib_pci_slot_reset(struct pci_dev *pdev)
710 {
711 	qib_devinfo(pdev, "QIB link_reset function called, ignored\n");
712 	return PCI_ERS_RESULT_CAN_RECOVER;
713 }
714 
715 static pci_ers_result_t
716 qib_pci_link_reset(struct pci_dev *pdev)
717 {
718 	qib_devinfo(pdev, "QIB link_reset function called, ignored\n");
719 	return PCI_ERS_RESULT_CAN_RECOVER;
720 }
721 
722 static void
723 qib_pci_resume(struct pci_dev *pdev)
724 {
725 	struct qib_devdata *dd = pci_get_drvdata(pdev);
726 	qib_devinfo(pdev, "QIB resume function called\n");
727 	pci_cleanup_aer_uncorrect_error_status(pdev);
728 	/*
729 	 * Running jobs will fail, since it's asynchronous
730 	 * unlike sysfs-requested reset.   Better than
731 	 * doing nothing.
732 	 */
733 	qib_init(dd, 1); /* same as re-init after reset */
734 }
735 
736 struct pci_error_handlers qib_pci_err_handler = {
737 	.error_detected = qib_pci_error_detected,
738 	.mmio_enabled = qib_pci_mmio_enabled,
739 	.link_reset = qib_pci_link_reset,
740 	.slot_reset = qib_pci_slot_reset,
741 	.resume = qib_pci_resume,
742 };
743