1 // SPDX-License-Identifier: GPL-2.0-only
3 * IBM Accelerator Family 'GenWQE'
5 * (C) Copyright IBM Corp. 2013
14 * Module initialization and PCIe setup. Card health monitoring and
15 * recovery functionality. Character device creation and deletion are
16 * controlled from here.
19 #include <linux/types.h>
20 #include <linux/pci.h>
21 #include <linux/err.h>
22 #include <linux/string.h>
23 #include <linux/sched.h>
24 #include <linux/wait.h>
25 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/module.h>
28 #include <linux/notifier.h>
29 #include <linux/device.h>
30 #include <linux/log2.h>
32 #include "card_base.h"
33 #include "card_ddcb.h"
40 MODULE_DESCRIPTION("GenWQE Card");
41 MODULE_VERSION(DRV_VERSION);
42 MODULE_LICENSE("GPL");
44 static char genwqe_driver_name[] = GENWQE_DEVNAME;
46 static struct dentry *debugfs_genwqe;
47 static struct genwqe_dev *genwqe_devices[GENWQE_CARD_NO_MAX];
49 /* PCI structure for identifying device by PCI vendor and device ID */
50 static const struct pci_device_id genwqe_device_table[] = {
51 { .vendor = PCI_VENDOR_ID_IBM,
52 .device = PCI_DEVICE_GENWQE,
53 .subvendor = PCI_SUBVENDOR_ID_IBM,
54 .subdevice = PCI_SUBSYSTEM_ID_GENWQE5,
55 .class = (PCI_CLASSCODE_GENWQE5 << 8),
59 /* Initial SR-IOV bring-up image */
60 { .vendor = PCI_VENDOR_ID_IBM,
61 .device = PCI_DEVICE_GENWQE,
62 .subvendor = PCI_SUBVENDOR_ID_IBM_SRIOV,
63 .subdevice = PCI_SUBSYSTEM_ID_GENWQE5_SRIOV,
64 .class = (PCI_CLASSCODE_GENWQE5_SRIOV << 8),
68 { .vendor = PCI_VENDOR_ID_IBM, /* VF Vendor ID */
69 .device = 0x0000, /* VF Device ID */
70 .subvendor = PCI_SUBVENDOR_ID_IBM_SRIOV,
71 .subdevice = PCI_SUBSYSTEM_ID_GENWQE5_SRIOV,
72 .class = (PCI_CLASSCODE_GENWQE5_SRIOV << 8),
77 { .vendor = PCI_VENDOR_ID_IBM,
78 .device = PCI_DEVICE_GENWQE,
79 .subvendor = PCI_SUBVENDOR_ID_IBM_SRIOV,
80 .subdevice = PCI_SUBSYSTEM_ID_GENWQE5,
81 .class = (PCI_CLASSCODE_GENWQE5_SRIOV << 8),
85 { .vendor = PCI_VENDOR_ID_IBM, /* VF Vendor ID */
86 .device = 0x0000, /* VF Device ID */
87 .subvendor = PCI_SUBVENDOR_ID_IBM_SRIOV,
88 .subdevice = PCI_SUBSYSTEM_ID_GENWQE5,
89 .class = (PCI_CLASSCODE_GENWQE5_SRIOV << 8),
93 /* Even one more ... */
94 { .vendor = PCI_VENDOR_ID_IBM,
95 .device = PCI_DEVICE_GENWQE,
96 .subvendor = PCI_SUBVENDOR_ID_IBM,
97 .subdevice = PCI_SUBSYSTEM_ID_GENWQE5_NEW,
98 .class = (PCI_CLASSCODE_GENWQE5 << 8),
102 { 0, } /* 0 terminated list. */
105 MODULE_DEVICE_TABLE(pci, genwqe_device_table);
108 * genwqe_devnode() - Set default access mode for genwqe devices.
109 * @dev: Pointer to device (unused)
110 * @mode: Carrier to pass-back given mode (permissions)
112 * Default mode should be rw for everybody. Do not change default
115 static char *genwqe_devnode(const struct device *dev, umode_t *mode)
122 static const struct class class_genwqe = {
123 .name = GENWQE_DEVNAME,
124 .devnode = genwqe_devnode,
128 * genwqe_dev_alloc() - Create and prepare a new card descriptor
130 * Return: Pointer to card descriptor, or ERR_PTR(err) on error
132 static struct genwqe_dev *genwqe_dev_alloc(void)
134 unsigned int i = 0, j;
135 struct genwqe_dev *cd;
137 for (i = 0; i < GENWQE_CARD_NO_MAX; i++) {
138 if (genwqe_devices[i] == NULL)
141 if (i >= GENWQE_CARD_NO_MAX)
142 return ERR_PTR(-ENODEV);
144 cd = kzalloc(sizeof(struct genwqe_dev), GFP_KERNEL);
146 return ERR_PTR(-ENOMEM);
149 cd->class_genwqe = &class_genwqe;
150 cd->debugfs_genwqe = debugfs_genwqe;
153 * This comes from kernel config option and can be overritten via
156 cd->use_platform_recovery = CONFIG_GENWQE_PLATFORM_ERROR_RECOVERY;
158 init_waitqueue_head(&cd->queue_waitq);
160 spin_lock_init(&cd->file_lock);
161 INIT_LIST_HEAD(&cd->file_list);
163 cd->card_state = GENWQE_CARD_UNUSED;
164 spin_lock_init(&cd->print_lock);
166 cd->ddcb_software_timeout = GENWQE_DDCB_SOFTWARE_TIMEOUT;
167 cd->kill_timeout = GENWQE_KILL_TIMEOUT;
169 for (j = 0; j < GENWQE_MAX_VFS; j++)
170 cd->vf_jobtimeout_msec[j] = GENWQE_VF_JOBTIMEOUT_MSEC;
172 genwqe_devices[i] = cd;
176 static void genwqe_dev_free(struct genwqe_dev *cd)
181 genwqe_devices[cd->card_idx] = NULL;
186 * genwqe_bus_reset() - Card recovery
187 * @cd: GenWQE device information
189 * pci_reset_function() will recover the device and ensure that the
190 * registers are accessible again when it completes with success. If
191 * not, the card will stay dead and registers will be unaccessible
194 static int genwqe_bus_reset(struct genwqe_dev *cd)
197 struct pci_dev *pci_dev = cd->pci_dev;
200 if (cd->err_inject & GENWQE_INJECT_BUS_RESET_FAILURE)
205 pci_iounmap(pci_dev, mmio);
207 pci_release_mem_regions(pci_dev);
210 * Firmware/BIOS might change memory mapping during bus reset.
211 * Settings like enable bus-mastering, ... are backuped and
212 * restored by the pci_reset_function().
214 dev_dbg(&pci_dev->dev, "[%s] pci_reset function ...\n", __func__);
215 rc = pci_reset_function(pci_dev);
217 dev_err(&pci_dev->dev,
218 "[%s] err: failed reset func (rc %d)\n", __func__, rc);
221 dev_dbg(&pci_dev->dev, "[%s] done with rc=%d\n", __func__, rc);
224 * Here is the right spot to clear the register read
225 * failure. pci_bus_reset() does this job in real systems.
227 cd->err_inject &= ~(GENWQE_INJECT_HARDWARE_FAILURE |
228 GENWQE_INJECT_GFIR_FATAL |
229 GENWQE_INJECT_GFIR_INFO);
231 rc = pci_request_mem_regions(pci_dev, genwqe_driver_name);
233 dev_err(&pci_dev->dev,
234 "[%s] err: request bars failed (%d)\n", __func__, rc);
238 cd->mmio = pci_iomap(pci_dev, 0, 0);
239 if (cd->mmio == NULL) {
240 dev_err(&pci_dev->dev,
241 "[%s] err: mapping BAR0 failed\n", __func__);
248 * Hardware circumvention section. Certain bitstreams in our test-lab
249 * had different kinds of problems. Here is where we adjust those
250 * bitstreams to function will with this version of our device driver.
252 * Thise circumventions are applied to the physical function only.
253 * The magical numbers below are identifying development/manufacturing
254 * versions of the bitstream used on the card.
256 * Turn off error reporting for old/manufacturing images.
259 bool genwqe_need_err_masking(struct genwqe_dev *cd)
261 return (cd->slu_unitcfg & 0xFFFF0ull) < 0x32170ull;
264 static void genwqe_tweak_hardware(struct genwqe_dev *cd)
266 struct pci_dev *pci_dev = cd->pci_dev;
268 /* Mask FIRs for development images */
269 if (((cd->slu_unitcfg & 0xFFFF0ull) >= 0x32000ull) &&
270 ((cd->slu_unitcfg & 0xFFFF0ull) <= 0x33250ull)) {
271 dev_warn(&pci_dev->dev,
272 "FIRs masked due to bitstream %016llx.%016llx\n",
273 cd->slu_unitcfg, cd->app_unitcfg);
275 __genwqe_writeq(cd, IO_APP_SEC_LEM_DEBUG_OVR,
276 0xFFFFFFFFFFFFFFFFull);
278 __genwqe_writeq(cd, IO_APP_ERR_ACT_MASK,
279 0x0000000000000000ull);
284 * genwqe_recovery_on_fatal_gfir_required() - Version depended actions
285 * @cd: GenWQE device information
287 * Bitstreams older than 2013-02-17 have a bug where fatal GFIRs must
288 * be ignored. This is e.g. true for the bitstream we gave to the card
289 * manufacturer, but also for some old bitstreams we released to our
292 int genwqe_recovery_on_fatal_gfir_required(struct genwqe_dev *cd)
294 return (cd->slu_unitcfg & 0xFFFF0ull) >= 0x32170ull;
297 int genwqe_flash_readback_fails(struct genwqe_dev *cd)
299 return (cd->slu_unitcfg & 0xFFFF0ull) < 0x32170ull;
303 * genwqe_T_psec() - Calculate PF/VF timeout register content
304 * @cd: GenWQE device information
306 * Note: From a design perspective it turned out to be a bad idea to
307 * use codes here to specifiy the frequency/speed values. An old
308 * driver cannot understand new codes and is therefore always a
309 * problem. Better is to measure out the value or put the
310 * speed/frequency directly into a register which is always a valid
311 * value for old as well as for new software.
314 static int genwqe_T_psec(struct genwqe_dev *cd)
316 u16 speed; /* 1/f -> 250, 200, 166, 175 */
317 static const int T[] = { 4000, 5000, 6000, 5714 };
319 speed = (u16)((cd->slu_unitcfg >> 28) & 0x0full);
320 if (speed >= ARRAY_SIZE(T))
321 return -1; /* illegal value */
327 * genwqe_setup_pf_jtimer() - Setup PF hardware timeouts for DDCB execution
328 * @cd: GenWQE device information
330 * Do this _after_ card_reset() is called. Otherwise the values will
331 * vanish. The settings need to be done when the queues are inactive.
333 * The max. timeout value is 2^(10+x) * T (6ns for 166MHz) * 15/16.
334 * The min. timeout value is 2^(10+x) * T (6ns for 166MHz) * 14/16.
336 static bool genwqe_setup_pf_jtimer(struct genwqe_dev *cd)
338 u32 T = genwqe_T_psec(cd);
341 if (GENWQE_PF_JOBTIMEOUT_MSEC == 0)
344 /* PF: large value needed, flash update 2sec per block */
345 x = ilog2(GENWQE_PF_JOBTIMEOUT_MSEC *
346 16000000000uL/(T * 15)) - 10;
348 genwqe_write_vreg(cd, IO_SLC_VF_APPJOB_TIMEOUT,
349 0xff00 | (x & 0xff), 0);
354 * genwqe_setup_vf_jtimer() - Setup VF hardware timeouts for DDCB execution
355 * @cd: GenWQE device information
357 static bool genwqe_setup_vf_jtimer(struct genwqe_dev *cd)
359 struct pci_dev *pci_dev = cd->pci_dev;
361 u32 T = genwqe_T_psec(cd);
365 totalvfs = pci_sriov_get_totalvfs(pci_dev);
369 for (vf = 0; vf < totalvfs; vf++) {
371 if (cd->vf_jobtimeout_msec[vf] == 0)
374 x = ilog2(cd->vf_jobtimeout_msec[vf] *
375 16000000000uL/(T * 15)) - 10;
377 genwqe_write_vreg(cd, IO_SLC_VF_APPJOB_TIMEOUT,
378 0xff00 | (x & 0xff), vf + 1);
383 static int genwqe_ffdc_buffs_alloc(struct genwqe_dev *cd)
385 unsigned int type, e = 0;
387 for (type = 0; type < GENWQE_DBG_UNITS; type++) {
389 case GENWQE_DBG_UNIT0:
390 e = genwqe_ffdc_buff_size(cd, 0);
392 case GENWQE_DBG_UNIT1:
393 e = genwqe_ffdc_buff_size(cd, 1);
395 case GENWQE_DBG_UNIT2:
396 e = genwqe_ffdc_buff_size(cd, 2);
398 case GENWQE_DBG_REGS:
399 e = GENWQE_FFDC_REGS;
403 /* currently support only the debug units mentioned here */
404 cd->ffdc[type].entries = e;
405 cd->ffdc[type].regs =
406 kmalloc_array(e, sizeof(struct genwqe_reg),
409 * regs == NULL is ok, the using code treats this as no regs,
410 * Printing warning is ok in this case.
416 static void genwqe_ffdc_buffs_free(struct genwqe_dev *cd)
420 for (type = 0; type < GENWQE_DBG_UNITS; type++) {
421 kfree(cd->ffdc[type].regs);
422 cd->ffdc[type].regs = NULL;
426 static int genwqe_read_ids(struct genwqe_dev *cd)
430 struct pci_dev *pci_dev = cd->pci_dev;
432 cd->slu_unitcfg = __genwqe_readq(cd, IO_SLU_UNITCFG);
433 if (cd->slu_unitcfg == IO_ILLEGAL_VALUE) {
434 dev_err(&pci_dev->dev,
435 "err: SLUID=%016llx\n", cd->slu_unitcfg);
440 slu_id = genwqe_get_slu_id(cd);
441 if (slu_id < GENWQE_SLU_ARCH_REQ || slu_id == 0xff) {
442 dev_err(&pci_dev->dev,
443 "err: incompatible SLU Architecture %u\n", slu_id);
448 cd->app_unitcfg = __genwqe_readq(cd, IO_APP_UNITCFG);
449 if (cd->app_unitcfg == IO_ILLEGAL_VALUE) {
450 dev_err(&pci_dev->dev,
451 "err: APPID=%016llx\n", cd->app_unitcfg);
455 genwqe_read_app_id(cd, cd->app_name, sizeof(cd->app_name));
458 * Is access to all registers possible? If we are a VF the
459 * answer is obvious. If we run fully virtualized, we need to
460 * check if we can access all registers. If we do not have
461 * full access we will cause an UR and some informational FIRs
462 * in the PF, but that should not harm.
464 if (pci_dev->is_virtfn)
465 cd->is_privileged = 0;
467 cd->is_privileged = (__genwqe_readq(cd, IO_SLU_BITSTREAM)
468 != IO_ILLEGAL_VALUE);
474 static int genwqe_start(struct genwqe_dev *cd)
477 struct pci_dev *pci_dev = cd->pci_dev;
479 err = genwqe_read_ids(cd);
483 if (genwqe_is_privileged(cd)) {
484 /* do this after the tweaks. alloc fail is acceptable */
485 genwqe_ffdc_buffs_alloc(cd);
486 genwqe_stop_traps(cd);
488 /* Collect registers e.g. FIRs, UNITIDs, traces ... */
489 genwqe_read_ffdc_regs(cd, cd->ffdc[GENWQE_DBG_REGS].regs,
490 cd->ffdc[GENWQE_DBG_REGS].entries, 0);
492 genwqe_ffdc_buff_read(cd, GENWQE_DBG_UNIT0,
493 cd->ffdc[GENWQE_DBG_UNIT0].regs,
494 cd->ffdc[GENWQE_DBG_UNIT0].entries);
496 genwqe_ffdc_buff_read(cd, GENWQE_DBG_UNIT1,
497 cd->ffdc[GENWQE_DBG_UNIT1].regs,
498 cd->ffdc[GENWQE_DBG_UNIT1].entries);
500 genwqe_ffdc_buff_read(cd, GENWQE_DBG_UNIT2,
501 cd->ffdc[GENWQE_DBG_UNIT2].regs,
502 cd->ffdc[GENWQE_DBG_UNIT2].entries);
504 genwqe_start_traps(cd);
506 if (cd->card_state == GENWQE_CARD_FATAL_ERROR) {
507 dev_warn(&pci_dev->dev,
508 "[%s] chip reload/recovery!\n", __func__);
511 * Stealth Mode: Reload chip on either hot
514 cd->softreset = 0x7Cull;
515 __genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET,
518 err = genwqe_bus_reset(cd);
520 dev_err(&pci_dev->dev,
521 "[%s] err: bus reset failed!\n",
527 * Re-read the IDs because
528 * it could happen that the bitstream load
531 err = genwqe_read_ids(cd);
537 err = genwqe_setup_service_layer(cd); /* does a reset to the card */
539 dev_err(&pci_dev->dev,
540 "[%s] err: could not setup servicelayer!\n", __func__);
545 if (genwqe_is_privileged(cd)) { /* code is running _after_ reset */
546 genwqe_tweak_hardware(cd);
548 genwqe_setup_pf_jtimer(cd);
549 genwqe_setup_vf_jtimer(cd);
552 err = genwqe_device_create(cd);
554 dev_err(&pci_dev->dev,
555 "err: chdev init failed! (err=%d)\n", err);
556 goto out_release_service_layer;
560 out_release_service_layer:
561 genwqe_release_service_layer(cd);
563 if (genwqe_is_privileged(cd))
564 genwqe_ffdc_buffs_free(cd);
569 * genwqe_stop() - Stop card operation
570 * @cd: GenWQE device information
573 * As long as genwqe_thread runs we might access registers during
574 * error data capture. Same is with the genwqe_health_thread.
575 * When genwqe_bus_reset() fails this function might called two times:
576 * first by the genwqe_health_thread() and later by genwqe_remove() to
577 * unbind the device. We must be able to survive that.
579 * This function must be robust enough to be called twice.
581 static int genwqe_stop(struct genwqe_dev *cd)
583 genwqe_finish_queue(cd); /* no register access */
584 genwqe_device_remove(cd); /* device removed, procs killed */
585 genwqe_release_service_layer(cd); /* here genwqe_thread is stopped */
587 if (genwqe_is_privileged(cd)) {
588 pci_disable_sriov(cd->pci_dev); /* access pci config space */
589 genwqe_ffdc_buffs_free(cd);
596 * genwqe_recover_card() - Try to recover the card if it is possible
597 * @cd: GenWQE device information
598 * @fatal_err: Indicate whether to attempt soft reset
600 * If fatal_err is set no register access is possible anymore. It is
601 * likely that genwqe_start fails in that situation. Proper error
602 * handling is required in this case.
604 * genwqe_bus_reset() will cause the pci code to call genwqe_remove()
605 * and later genwqe_probe() for all virtual functions.
607 static int genwqe_recover_card(struct genwqe_dev *cd, int fatal_err)
610 struct pci_dev *pci_dev = cd->pci_dev;
615 * Make sure chip is not reloaded to maintain FFDC. Write SLU
616 * Reset Register, CPLDReset field to 0.
619 cd->softreset = 0x70ull;
620 __genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET, cd->softreset);
623 rc = genwqe_bus_reset(cd);
625 dev_err(&pci_dev->dev,
626 "[%s] err: card recovery impossible!\n", __func__);
630 rc = genwqe_start(cd);
632 dev_err(&pci_dev->dev,
633 "[%s] err: failed to launch device!\n", __func__);
639 static int genwqe_health_check_cond(struct genwqe_dev *cd, u64 *gfir)
641 *gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
642 return (*gfir & GFIR_ERR_TRIGGER) &&
643 genwqe_recovery_on_fatal_gfir_required(cd);
647 * genwqe_fir_checking() - Check the fault isolation registers of the card
648 * @cd: GenWQE device information
650 * If this code works ok, can be tried out with help of the genwqe_poke tool:
651 * sudo ./tools/genwqe_poke 0x8 0xfefefefefef
653 * Now the relevant FIRs/sFIRs should be printed out and the driver should
654 * invoke recovery (devices are removed and readded).
656 static u64 genwqe_fir_checking(struct genwqe_dev *cd)
658 int j, iterations = 0;
659 u64 mask, fir, fec, uid, gfir, gfir_masked, sfir, sfec;
660 u32 fir_addr, fir_clr_addr, fec_addr, sfir_addr, sfec_addr;
661 struct pci_dev *pci_dev = cd->pci_dev;
665 if (iterations > 16) {
666 dev_err(&pci_dev->dev, "* exit looping after %d times\n",
671 gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
673 dev_err(&pci_dev->dev, "* 0x%08x 0x%016llx\n",
674 IO_SLC_CFGREG_GFIR, gfir);
675 if (gfir == IO_ILLEGAL_VALUE)
679 * Avoid printing when to GFIR bit is on prevents contignous
680 * printout e.g. for the following bug:
681 * FIR set without a 2ndary FIR/FIR cannot be cleared
682 * Comment out the following if to get the prints:
687 gfir_masked = gfir & GFIR_ERR_TRIGGER; /* fatal errors */
689 for (uid = 0; uid < GENWQE_MAX_UNITS; uid++) { /* 0..2 in zEDC */
691 /* read the primary FIR (pfir) */
692 fir_addr = (uid << 24) + 0x08;
693 fir = __genwqe_readq(cd, fir_addr);
695 continue; /* no error in this unit */
697 dev_err(&pci_dev->dev, "* 0x%08x 0x%016llx\n", fir_addr, fir);
698 if (fir == IO_ILLEGAL_VALUE)
701 /* read primary FEC */
702 fec_addr = (uid << 24) + 0x18;
703 fec = __genwqe_readq(cd, fec_addr);
705 dev_err(&pci_dev->dev, "* 0x%08x 0x%016llx\n", fec_addr, fec);
706 if (fec == IO_ILLEGAL_VALUE)
709 for (j = 0, mask = 1ULL; j < 64; j++, mask <<= 1) {
711 /* secondary fir empty, skip it */
712 if ((fir & mask) == 0x0)
715 sfir_addr = (uid << 24) + 0x100 + 0x08 * j;
716 sfir = __genwqe_readq(cd, sfir_addr);
718 if (sfir == IO_ILLEGAL_VALUE)
720 dev_err(&pci_dev->dev,
721 "* 0x%08x 0x%016llx\n", sfir_addr, sfir);
723 sfec_addr = (uid << 24) + 0x300 + 0x08 * j;
724 sfec = __genwqe_readq(cd, sfec_addr);
726 if (sfec == IO_ILLEGAL_VALUE)
728 dev_err(&pci_dev->dev,
729 "* 0x%08x 0x%016llx\n", sfec_addr, sfec);
731 gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
732 if (gfir == IO_ILLEGAL_VALUE)
735 /* gfir turned on during routine! get out and
737 if ((gfir_masked == 0x0) &&
738 (gfir & GFIR_ERR_TRIGGER)) {
742 /* do not clear if we entered with a fatal gfir */
743 if (gfir_masked == 0x0) {
745 /* NEW clear by mask the logged bits */
746 sfir_addr = (uid << 24) + 0x100 + 0x08 * j;
747 __genwqe_writeq(cd, sfir_addr, sfir);
749 dev_dbg(&pci_dev->dev,
750 "[HM] Clearing 2ndary FIR 0x%08x with 0x%016llx\n",
754 * note, these cannot be error-Firs
755 * since gfir_masked is 0 after sfir
756 * was read. Also, it is safe to do
757 * this write if sfir=0. Still need to
758 * clear the primary. This just means
759 * there is no secondary FIR.
762 /* clear by mask the logged bit. */
763 fir_clr_addr = (uid << 24) + 0x10;
764 __genwqe_writeq(cd, fir_clr_addr, mask);
766 dev_dbg(&pci_dev->dev,
767 "[HM] Clearing primary FIR 0x%08x with 0x%016llx\n",
772 gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
773 if (gfir == IO_ILLEGAL_VALUE)
776 if ((gfir_masked == 0x0) && (gfir & GFIR_ERR_TRIGGER)) {
778 * Check once more that it didn't go on after all the
781 dev_dbg(&pci_dev->dev, "ACK! Another FIR! Recursing %d!\n",
788 return IO_ILLEGAL_VALUE;
792 * genwqe_pci_fundamental_reset() - trigger a PCIe fundamental reset on the slot
793 * @pci_dev: PCI device information struct
795 * Note: pci_set_pcie_reset_state() is not implemented on all archs, so this
796 * reset method will not work in all cases.
798 * Return: 0 on success or error code from pci_set_pcie_reset_state()
800 static int genwqe_pci_fundamental_reset(struct pci_dev *pci_dev)
805 * lock pci config space access from userspace,
806 * save state and issue PCIe fundamental reset
808 pci_cfg_access_lock(pci_dev);
809 pci_save_state(pci_dev);
810 rc = pci_set_pcie_reset_state(pci_dev, pcie_warm_reset);
812 /* keep PCIe reset asserted for 250ms */
814 pci_set_pcie_reset_state(pci_dev, pcie_deassert_reset);
815 /* Wait for 2s to reload flash and train the link */
818 pci_restore_state(pci_dev);
819 pci_cfg_access_unlock(pci_dev);
824 static int genwqe_platform_recovery(struct genwqe_dev *cd)
826 struct pci_dev *pci_dev = cd->pci_dev;
829 dev_info(&pci_dev->dev,
830 "[%s] resetting card for error recovery\n", __func__);
832 /* Clear out error injection flags */
833 cd->err_inject &= ~(GENWQE_INJECT_HARDWARE_FAILURE |
834 GENWQE_INJECT_GFIR_FATAL |
835 GENWQE_INJECT_GFIR_INFO);
839 /* Try recoverying the card with fundamental reset */
840 rc = genwqe_pci_fundamental_reset(pci_dev);
842 rc = genwqe_start(cd);
844 dev_info(&pci_dev->dev,
845 "[%s] card recovered\n", __func__);
847 dev_err(&pci_dev->dev,
848 "[%s] err: cannot start card services! (err=%d)\n",
851 dev_err(&pci_dev->dev,
852 "[%s] card reset failed\n", __func__);
859 * genwqe_reload_bistream() - reload card bitstream
860 * @cd: GenWQE device information
862 * Set the appropriate register and call fundamental reset to reaload the card
865 * Return: 0 on success, error code otherwise
867 static int genwqe_reload_bistream(struct genwqe_dev *cd)
869 struct pci_dev *pci_dev = cd->pci_dev;
872 dev_info(&pci_dev->dev,
873 "[%s] resetting card for bitstream reload\n",
879 * Cause a CPLD reprogram with the 'next_bitstream'
880 * partition on PCIe hot or fundamental reset
882 __genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET,
883 (cd->softreset & 0xcull) | 0x70ull);
885 rc = genwqe_pci_fundamental_reset(pci_dev);
888 * A fundamental reset failure can be caused
889 * by lack of support on the arch, so we just
890 * log the error and try to start the card
893 dev_err(&pci_dev->dev,
894 "[%s] err: failed to reset card for bitstream reload\n",
898 rc = genwqe_start(cd);
900 dev_err(&pci_dev->dev,
901 "[%s] err: cannot start card services! (err=%d)\n",
905 dev_info(&pci_dev->dev,
906 "[%s] card reloaded\n", __func__);
912 * genwqe_health_thread() - Health checking thread
913 * @data: GenWQE device information
915 * This thread is only started for the PF of the card.
917 * This thread monitors the health of the card. A critical situation
918 * is when we read registers which contain -1 (IO_ILLEGAL_VALUE). In
919 * this case we need to be recovered from outside. Writing to
920 * registers will very likely not work either.
922 * This thread must only exit if kthread_should_stop() becomes true.
924 * Condition for the health-thread to trigger:
925 * a) when a kthread_stop() request comes in or
926 * b) a critical GFIR occured
928 * Informational GFIRs are checked and potentially printed in
929 * GENWQE_HEALTH_CHECK_INTERVAL seconds.
931 static int genwqe_health_thread(void *data)
933 int rc, should_stop = 0;
934 struct genwqe_dev *cd = data;
935 struct pci_dev *pci_dev = cd->pci_dev;
936 u64 gfir, gfir_masked, slu_unitcfg, app_unitcfg;
939 while (!kthread_should_stop()) {
940 rc = wait_event_interruptible_timeout(cd->health_waitq,
941 (genwqe_health_check_cond(cd, &gfir) ||
942 (should_stop = kthread_should_stop())),
943 GENWQE_HEALTH_CHECK_INTERVAL * HZ);
948 if (gfir == IO_ILLEGAL_VALUE) {
949 dev_err(&pci_dev->dev,
950 "[%s] GFIR=%016llx\n", __func__, gfir);
954 slu_unitcfg = __genwqe_readq(cd, IO_SLU_UNITCFG);
955 if (slu_unitcfg == IO_ILLEGAL_VALUE) {
956 dev_err(&pci_dev->dev,
957 "[%s] SLU_UNITCFG=%016llx\n",
958 __func__, slu_unitcfg);
962 app_unitcfg = __genwqe_readq(cd, IO_APP_UNITCFG);
963 if (app_unitcfg == IO_ILLEGAL_VALUE) {
964 dev_err(&pci_dev->dev,
965 "[%s] APP_UNITCFG=%016llx\n",
966 __func__, app_unitcfg);
970 gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
971 if (gfir == IO_ILLEGAL_VALUE) {
972 dev_err(&pci_dev->dev,
973 "[%s] %s: GFIR=%016llx\n", __func__,
974 (gfir & GFIR_ERR_TRIGGER) ? "err" : "info",
979 gfir_masked = genwqe_fir_checking(cd);
980 if (gfir_masked == IO_ILLEGAL_VALUE)
984 * GFIR ErrorTrigger bits set => reset the card!
985 * Never do this for old/manufacturing images!
987 if ((gfir_masked) && !cd->skip_recovery &&
988 genwqe_recovery_on_fatal_gfir_required(cd)) {
990 cd->card_state = GENWQE_CARD_FATAL_ERROR;
992 rc = genwqe_recover_card(cd, 0);
994 /* FIXME Card is unusable and needs unbind! */
999 if (cd->card_state == GENWQE_CARD_RELOAD_BITSTREAM) {
1000 /* Userspace requested card bitstream reload */
1001 rc = genwqe_reload_bistream(cd);
1006 cd->last_gfir = gfir;
1013 if (cd->use_platform_recovery) {
1015 * Since we use raw accessors, EEH errors won't be detected
1016 * by the platform until we do a non-raw MMIO or config space
1019 readq(cd->mmio + IO_SLC_CFGREG_GFIR);
1021 /* We do nothing if the card is going over PCI recovery */
1022 if (pci_channel_offline(pci_dev))
1026 * If it's supported by the platform, we try a fundamental reset
1027 * to recover from a fatal error. Otherwise, we continue to wait
1028 * for an external recovery procedure to take care of it.
1030 rc = genwqe_platform_recovery(cd);
1032 goto health_thread_begin;
1035 dev_err(&pci_dev->dev,
1036 "[%s] card unusable. Please trigger unbind!\n", __func__);
1038 /* Bring down logical devices to inform user space via udev remove. */
1039 cd->card_state = GENWQE_CARD_FATAL_ERROR;
1042 /* genwqe_bus_reset failed(). Now wait for genwqe_remove(). */
1043 while (!kthread_should_stop())
1049 static int genwqe_health_check_start(struct genwqe_dev *cd)
1053 if (GENWQE_HEALTH_CHECK_INTERVAL <= 0)
1054 return 0; /* valid for disabling the service */
1056 /* moved before request_irq() */
1057 /* init_waitqueue_head(&cd->health_waitq); */
1059 cd->health_thread = kthread_run(genwqe_health_thread, cd,
1060 GENWQE_DEVNAME "%d_health",
1062 if (IS_ERR(cd->health_thread)) {
1063 rc = PTR_ERR(cd->health_thread);
1064 cd->health_thread = NULL;
1070 static int genwqe_health_thread_running(struct genwqe_dev *cd)
1072 return cd->health_thread != NULL;
1075 static int genwqe_health_check_stop(struct genwqe_dev *cd)
1077 if (!genwqe_health_thread_running(cd))
1080 kthread_stop(cd->health_thread);
1081 cd->health_thread = NULL;
1086 * genwqe_pci_setup() - Allocate PCIe related resources for our card
1087 * @cd: GenWQE device information
1089 static int genwqe_pci_setup(struct genwqe_dev *cd)
1092 struct pci_dev *pci_dev = cd->pci_dev;
1094 err = pci_enable_device_mem(pci_dev);
1096 dev_err(&pci_dev->dev,
1097 "err: failed to enable pci memory (err=%d)\n", err);
1101 /* Reserve PCI I/O and memory resources */
1102 err = pci_request_mem_regions(pci_dev, genwqe_driver_name);
1104 dev_err(&pci_dev->dev,
1105 "[%s] err: request bars failed (%d)\n", __func__, err);
1107 goto err_disable_device;
1110 /* check for 64-bit DMA address supported (DAC) */
1111 /* check for 32-bit DMA address supported (SAC) */
1112 if (dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64)) &&
1113 dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(32))) {
1114 dev_err(&pci_dev->dev,
1115 "err: neither DMA32 nor DMA64 supported\n");
1117 goto out_release_resources;
1120 pci_set_master(pci_dev);
1122 /* EEH recovery requires PCIe fundamental reset */
1123 pci_dev->needs_freset = 1;
1125 /* request complete BAR-0 space (length = 0) */
1126 cd->mmio_len = pci_resource_len(pci_dev, 0);
1127 cd->mmio = pci_iomap(pci_dev, 0, 0);
1128 if (cd->mmio == NULL) {
1129 dev_err(&pci_dev->dev,
1130 "[%s] err: mapping BAR0 failed\n", __func__);
1132 goto out_release_resources;
1135 cd->num_vfs = pci_sriov_get_totalvfs(pci_dev);
1136 if (cd->num_vfs < 0)
1139 err = genwqe_read_ids(cd);
1146 pci_iounmap(pci_dev, cd->mmio);
1147 out_release_resources:
1148 pci_release_mem_regions(pci_dev);
1150 pci_disable_device(pci_dev);
1156 * genwqe_pci_remove() - Free PCIe related resources for our card
1157 * @cd: GenWQE device information
1159 static void genwqe_pci_remove(struct genwqe_dev *cd)
1161 struct pci_dev *pci_dev = cd->pci_dev;
1164 pci_iounmap(pci_dev, cd->mmio);
1166 pci_release_mem_regions(pci_dev);
1167 pci_disable_device(pci_dev);
1171 * genwqe_probe() - Device initialization
1172 * @pci_dev: PCI device information struct
1173 * @id: PCI device ID
1175 * Callable for multiple cards. This function is called on bind.
1177 * Return: 0 if succeeded, < 0 when failed
1179 static int genwqe_probe(struct pci_dev *pci_dev,
1180 const struct pci_device_id *id)
1183 struct genwqe_dev *cd;
1185 genwqe_init_crc32();
1187 cd = genwqe_dev_alloc();
1189 dev_err(&pci_dev->dev, "err: could not alloc mem (err=%d)!\n",
1194 dev_set_drvdata(&pci_dev->dev, cd);
1195 cd->pci_dev = pci_dev;
1197 err = genwqe_pci_setup(cd);
1199 dev_err(&pci_dev->dev,
1200 "err: problems with PCI setup (err=%d)\n", err);
1204 err = genwqe_start(cd);
1206 dev_err(&pci_dev->dev,
1207 "err: cannot start card services! (err=%d)\n", err);
1208 goto out_pci_remove;
1211 if (genwqe_is_privileged(cd)) {
1212 err = genwqe_health_check_start(cd);
1214 dev_err(&pci_dev->dev,
1215 "err: cannot start health checking! (err=%d)\n",
1217 goto out_stop_services;
1225 genwqe_pci_remove(cd);
1227 genwqe_dev_free(cd);
1232 * genwqe_remove() - Called when device is removed (hot-plugable)
1233 * @pci_dev: PCI device information struct
1235 * Or when driver is unloaded respecitively when unbind is done.
1237 static void genwqe_remove(struct pci_dev *pci_dev)
1239 struct genwqe_dev *cd = dev_get_drvdata(&pci_dev->dev);
1241 genwqe_health_check_stop(cd);
1244 * genwqe_stop() must survive if it is called twice
1245 * sequentially. This happens when the health thread calls it
1246 * and fails on genwqe_bus_reset().
1249 genwqe_pci_remove(cd);
1250 genwqe_dev_free(cd);
1254 * genwqe_err_error_detected() - Error detection callback
1255 * @pci_dev: PCI device information struct
1256 * @state: PCI channel state
1258 * This callback is called by the PCI subsystem whenever a PCI bus
1259 * error is detected.
1261 static pci_ers_result_t genwqe_err_error_detected(struct pci_dev *pci_dev,
1262 pci_channel_state_t state)
1264 struct genwqe_dev *cd;
1266 dev_err(&pci_dev->dev, "[%s] state=%d\n", __func__, state);
1268 cd = dev_get_drvdata(&pci_dev->dev);
1270 return PCI_ERS_RESULT_DISCONNECT;
1273 genwqe_health_check_stop(cd);
1277 * On permanent failure, the PCI code will call device remove
1278 * after the return of this function.
1279 * genwqe_stop() can be called twice.
1281 if (state == pci_channel_io_perm_failure) {
1282 return PCI_ERS_RESULT_DISCONNECT;
1284 genwqe_pci_remove(cd);
1285 return PCI_ERS_RESULT_NEED_RESET;
1289 static pci_ers_result_t genwqe_err_slot_reset(struct pci_dev *pci_dev)
1292 struct genwqe_dev *cd = dev_get_drvdata(&pci_dev->dev);
1294 rc = genwqe_pci_setup(cd);
1296 return PCI_ERS_RESULT_RECOVERED;
1298 dev_err(&pci_dev->dev,
1299 "err: problems with PCI setup (err=%d)\n", rc);
1300 return PCI_ERS_RESULT_DISCONNECT;
1304 static pci_ers_result_t genwqe_err_result_none(struct pci_dev *dev)
1306 return PCI_ERS_RESULT_NONE;
1309 static void genwqe_err_resume(struct pci_dev *pci_dev)
1312 struct genwqe_dev *cd = dev_get_drvdata(&pci_dev->dev);
1314 rc = genwqe_start(cd);
1316 rc = genwqe_health_check_start(cd);
1318 dev_err(&pci_dev->dev,
1319 "err: cannot start health checking! (err=%d)\n",
1322 dev_err(&pci_dev->dev,
1323 "err: cannot start card services! (err=%d)\n", rc);
1327 static int genwqe_sriov_configure(struct pci_dev *dev, int numvfs)
1330 struct genwqe_dev *cd = dev_get_drvdata(&dev->dev);
1333 genwqe_setup_vf_jtimer(cd);
1334 rc = pci_enable_sriov(dev, numvfs);
1340 pci_disable_sriov(dev);
1346 static const struct pci_error_handlers genwqe_err_handler = {
1347 .error_detected = genwqe_err_error_detected,
1348 .mmio_enabled = genwqe_err_result_none,
1349 .slot_reset = genwqe_err_slot_reset,
1350 .resume = genwqe_err_resume,
1353 static struct pci_driver genwqe_driver = {
1354 .name = genwqe_driver_name,
1355 .id_table = genwqe_device_table,
1356 .probe = genwqe_probe,
1357 .remove = genwqe_remove,
1358 .sriov_configure = genwqe_sriov_configure,
1359 .err_handler = &genwqe_err_handler,
1363 * genwqe_init_module() - Driver registration and initialization
1365 static int __init genwqe_init_module(void)
1369 rc = class_register(&class_genwqe);
1371 pr_err("[%s] create class failed\n", __func__);
1375 debugfs_genwqe = debugfs_create_dir(GENWQE_DEVNAME, NULL);
1377 rc = pci_register_driver(&genwqe_driver);
1379 pr_err("[%s] pci_reg_driver (rc=%d)\n", __func__, rc);
1386 debugfs_remove(debugfs_genwqe);
1387 class_unregister(&class_genwqe);
1392 * genwqe_exit_module() - Driver exit
1394 static void __exit genwqe_exit_module(void)
1396 pci_unregister_driver(&genwqe_driver);
1397 debugfs_remove(debugfs_genwqe);
1398 class_unregister(&class_genwqe);
1401 module_init(genwqe_init_module);
1402 module_exit(genwqe_exit_module);