PCIe : Interrupts not received in kernel module

Hello,

I wrote a driver for PCIe base card. Upon writing to a control register, the FPGA generates a legacy (but PCIe) interrupt every ~10msec.

The driver worked fine on a specified chipset with Server 2016 and now it does not work on Server 2019 with a newer chipset.

According to the FPGA debugger, the FPGA generates interrupts.

In the driver I read offset 0x6 in the configuration space. D3 is set to 1 as expected.

What can be the reason that PLxEvtInterruptIsr is not called at all ?

According to the device manager, the card takes interrupt 8 and vector 0x82.

Following is the code that calls WdfInterruptCreate.

Thank you,

Zvi

/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    PURPOSE.

Module Name:

    IsrDpc.c

Abstract:

    Contains routines related to interrupt and dpc handling.

Environment:

    Kernel mode

--*/

#include "precomp.h"

#include "IsrDpc.tmh"
#include "ModuleIn.h"
#include "ModuleIoControl.h"

NTSTATUS
PLxInterruptCreate(
    IN PDEVICE_EXTENSION DevExt
    )
/*++
Routine Description:

    Configure and create the WDFINTERRUPT object.
    This routine is called by EvtDeviceAdd callback.

Arguments:

    DevExt      Pointer to our DEVICE_EXTENSION

Return Value:

    NTSTATUS code

--*/
{
    NTSTATUS                    status;
    WDF_INTERRUPT_CONFIG        InterruptConfig;

	KdPrint(("--> PLxInterruptCreate\n"));

    WDF_INTERRUPT_CONFIG_INIT( &InterruptConfig,
                               PLxEvtInterruptIsr,
                               PLxEvtInterruptDpc );

    InterruptConfig.EvtInterruptEnable  = PLxEvtInterruptEnable;
    InterruptConfig.EvtInterruptDisable = PLxEvtInterruptDisable;

    // JOHNR: Enable testing of the DpcForIsr Synchronization
    InterruptConfig.AutomaticSerialization = TRUE;

    //
    // Unlike WDM, framework driver should create interrupt object in EvtDeviceAdd and
    // let the framework do the resource parsing and registration of ISR with the kernel.
    // Framework connects the interrupt after invoking the EvtDeviceD0Entry callback
    // and disconnect before invoking EvtDeviceD0Exit. EvtInterruptEnable is called after
    // the interrupt interrupt is connected and EvtInterruptDisable before the interrupt is
    // disconnected.
    //
    status = WdfInterruptCreate( DevExt->Device,
                                 &InterruptConfig,
                                 WDF_NO_OBJECT_ATTRIBUTES,
                                 &DevExt->Interrupt );

    if( !NT_SUCCESS(status) ) {
        /*TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
                    "WdfInterruptCreate failed: %!STATUS!", status);*/
    }

	KdPrint(("<-- PLxInterruptCreate\n"));

    return status;
}

BOOLEAN
PLxEvtInterruptIsr(
    IN WDFINTERRUPT Interrupt,
    IN ULONG        MessageID
    )
/*++
Routine Description:

    Interrupt handler for this driver. Called at DIRQL level when the
    device or another device sharing the same interrupt line asserts
    the interrupt. The driver first checks the device to make sure whether
    this interrupt is generated by its device and if so clear the interrupt
    register to disable further generation of interrupts and queue a
    DPC to do other I/O work related to interrupt - such as reading
    the device memory, starting a DMA transaction, coping it to
    the request buffer and completing the request, etc.

Arguments:

    Interupt   - Handle to WDFINTERRUPT Object for this device.
    MessageID  - MSI message ID (always 0 in this configuration)

Return Value:

     TRUE   --  This device generated the interrupt.
     FALSE  --  This device did not generated this interrupt.

--*/
{
    PDEVICE_EXTENSION   devExt;
    BOOLEAN             isRecognized = FALSE;
	CARD_STATUS_U		status;
	UINT32				Channel;
	UINT32 DataMask, HeaderMask;
	int BitId = 0;

    UNREFERENCED_PARAMETER(MessageID);

	KdPrint (("--> PLxInterruptHandler\n"));
	devExt = PLxGetDeviceContext(WdfInterruptGetDevice(Interrupt));

	//Read status register 
	status.Word = READ_REGISTER_ULONG((PVOID)((UINT64)devExt->Bar1Address + STATUS_REGISTER_OFFSET));

	//The interrupt status is a 32bits word. 
	//1st byte contains the status of channel 0
	//2nd byte contains the status of channel 1
	//3rd byte contains the status of channel 2
	//4th byte contains the status of channel 3

	for (Channel = 0; Channel<N_CHANNELS; Channel++)
	{
		//Bits 0,8,16,24
		DataMask = 1 << BitId;
		if (status.Word & DataMask)
		{
			devExt->InterruptPending[Channel] = 1;
			(devExt->InterruptCounter[Channel])++;

			//Check if also header bit is '1'
			HeaderMask = DataMask << 1;
			if (status.Word & HeaderMask)
			{
				devExt->HeaderBlockReady[Channel] = 1;
				//KdPrint (("Channel:%d Header block ready\n",Channel));
			}
			else
				devExt->HeaderBlockReady[Channel] = 0;

			isRecognized = TRUE;
		}

		//Move to next byte 
		BitId += 8;
	}

	if (isRecognized)
		WdfInterruptQueueDpcForIsr(devExt->Interrupt);

	//Write status register to clear interrupts
	WRITE_REGISTER_ULONG((PVOID)((UINT64)devExt->Bar1Address + STATUS_REGISTER_OFFSET), status.Word);

    devExt  = PLxGetDeviceContext(WdfInterruptGetDevice(Interrupt));
 
    return isRecognized;
}

_Use_decl_annotations_
VOID
PLxEvtInterruptDpc(
    WDFINTERRUPT Interrupt,
    WDFOBJECT    Device
    )
/*++

Routine Description:

    DPC callback for ISR. Please note that on a multiprocessor system,
    you could have more than one DPCs running simulataneously on
    multiple processors. So if you are accesing any global resources
    make sure to synchrnonize the accesses with a spinlock.

Arguments:

    Interupt  - Handle to WDFINTERRUPT Object for this device.
    Device    - WDFDEVICE object passed to InterruptCreate

Return Value:

--*/
{
    PDEVICE_EXTENSION   devExt;
	ULONG InterruptPending[N_CHANNELS] = { 0,0,0,0 };
	int Channel;
	NTSTATUS            status;
	WDFREQUEST          request;
	MODULE_INTERRUPT_REPLY *pInterruptReply;
	void	*pOutBuffer;
	size_t Length;
	int bytesReturned;

    UNREFERENCED_PARAMETER(Device);


    KdPrint (("--> PLxEvtInterruptDpc"));

    devExt  = PLxGetDeviceContext(WdfInterruptGetDevice(Interrupt));

    //
    // Acquire this device's InterruptSpinLock.
    //
    WdfInterruptAcquireLock( Interrupt );

	for (Channel = 0; Channel<N_CHANNELS; Channel++)
	{
		if (devExt->InterruptPending[Channel])
		{
			devExt->InterruptPending[Channel] = 0;
			InterruptPending[Channel] = 1;
		}
	}

    //
    // Release our interrupt spinlock
    //
    WdfInterruptReleaseLock( Interrupt );

	//Handle pending interrupts
	for (Channel = 0; Channel<N_CHANNELS; Channel++)
	{
		if (InterruptPending[Channel])
		{
			status = WdfIoQueueRetrieveNextRequest(devExt->InternalQueue[Channel], &request);
			//Request is pending in queue. EvtIoctl sent "was interrupt" request
			if (NT_SUCCESS(status))
			{
				status = WdfRequestRetrieveOutputBuffer(request, sizeof(MODULE_INTERRUPT_REPLY),
					&pOutBuffer, &Length);

				pInterruptReply = (MODULE_INTERRUPT_REPLY *)pOutBuffer;

				pInterruptReply->Counter = devExt->InterruptCounter[Channel];
				pInterruptReply->HeaderBlockReady = devExt->HeaderBlockReady[Channel];

				//KdPrint (("HeaderBlockReady[Channel]=%d\n",pInterruptReply->HeaderBlockReady));
				//KdPrint (("WdfRequestRetrieveOutputBuffer: status=0x%x counter=%d\n",status,pInterruptReply->Counter));

				bytesReturned = sizeof(MODULE_INTERRUPT_REPLY);

				WdfRequestCompleteWithInformation(request, STATUS_SUCCESS, bytesReturned);
			}
		}

		KdPrint (("<-- PLxEvtInterruptDpc"));
	}

    return;
}

NTSTATUS
PLxEvtInterruptEnable(
    IN WDFINTERRUPT Interrupt,
    IN WDFDEVICE    Device
    )
/*++

Routine Description:

    Called by the framework at DIRQL immediately after registering the ISR with the kernel
    by calling IoConnectInterrupt.

Return Value:

    NTSTATUS
--*/
{
 	UNREFERENCED_PARAMETER(Device);
	UNREFERENCED_PARAMETER(Interrupt);

    return STATUS_SUCCESS;
}

NTSTATUS
PLxEvtInterruptDisable(
    IN WDFINTERRUPT Interrupt,
    IN WDFDEVICE    Device
    )
/*++

Routine Description:

    Called by the framework at DIRQL before Deregistering the ISR with the kernel
    by calling IoDisconnectInterrupt.

Return Value:

    NTSTATUS
--*/
{
	UNREFERENCED_PARAMETER(Device);
	UNREFERENCED_PARAMETER(Interrupt);

    return STATUS_SUCCESS;
}

Are you 100% sure the new chipset is using legacy interrupts, and not MSI interrupts?

SOMETIMES, not always, the BIOS has settings to manage the legacy interrupts. It's worth checking.

Please, for the love of engineering practice, change the comments and routine names to be your own, not copying PLx directly from the sample. If you have files called "Pci9x5x.sys" and "pci9x5x.inf", you should be embarrassed.

What? It is CMOS/RTC legacy interrupt fixed number.

Hello,

I double checked:

Interrupt level: 0x7, Vector: 072

This is the message I got in DbgView

But in the device manager, IRQ is 0x13

Does it make sense ?

Thank you,

Zvi

>Does it make sense?

Yes. Now it is pretty ordinary config. IRQ 0x10 - 0x13 (16-19) are usual values for HALs with APIC (all modern OS) for PIRQA - PIRQD. So next check is does some other device use the same IRQ (IRQ 0x13).

Hi,

Can you please explain how can I check that other device is using the same IRQ ?

If there is such device, what should I do ?

Thank you,

Zvi

Some new motherboards block interrupts and/or DMA from 3rd party hardware devices by default. Check the BIOS for setting that looks like "DMA protection" and disable it (assuming you understand the consequences).

Device Manager, of course, Resources view.

It depends but in such situation problem can be not in your device.

You can disable all but your device, having an exclusive ISR.

detect chained interrupts sample
#define PCI_NODE  "PCI\\"
#define DEV_PROP_HARDWAREID   2
#define DEV_PROP_DESCRIPTION  3
  
/*Replace with own from Device Manager*/
#define DEVICE_ID "VEN_XXXX&DEV_YYYY&SUBSYS_2"
  
typedef struct _DEVIRQ {
    char  HardwareId[MAX_DEVICE_ID_LEN];
    char  Description[200];
    DWORD Irql;
} DEVIRQL;
  
unsigned int            sharedirq=0;
  
/*Code based on WDK devcon source code available at:
  WinDDK\7600.16385.1\src\setup\devcon\             */
BOOL GetDeviceProperty(HDEVINFO Devs, PSP_DEVINFO_DATA DevInfo, DWORD Flag, char *Property, UINT Count);
BOOL GetDeviceIrqResource(HDEVINFO Devs, PSP_DEVINFO_DATA DevInfo, PDWORD IrqLine);
  
DWORD PrintSharedIrqLine(const char *DeviceId)
{
    HDEVINFO devs = INVALID_HANDLE_VALUE;
    DWORD devIndex, irqLine, irqDevice,
          irqCount = 0, sameIrq = 0;
    SP_DEVINFO_DATA devInfo;
    SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
    char devID[MAX_DEVICE_ID_LEN];
    char hwId[MAX_DEVICE_ID_LEN];
    DEVIRQL *di = NULL, *prevDi = NULL;
  
    devs = SetupDiGetClassDevsEx(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT, NULL, NULL, NULL);
    if(devs == INVALID_HANDLE_VALUE) {
        goto Exit;
    }
    SetupDiOpenDeviceInfo(devs, PCI_NODE, NULL, 0, NULL);
    devInfoListDetail.cbSize = sizeof(devInfoListDetail);
    if(!SetupDiGetDeviceInfoListDetail(devs,&devInfoListDetail)) {
        goto Exit;
    }
    devInfo.cbSize = sizeof(devInfo);
    for(devIndex = 0; SetupDiEnumDeviceInfo(devs, devIndex, &devInfo); devIndex ++) {
        if(CM_Get_Device_ID_Ex(devInfo.DevInst, devID, MAX_DEVICE_ID_LEN, 0, devInfoListDetail.RemoteMachineHandle) != CR_SUCCESS) {
            continue;
        }
        if(GetDeviceProperty(devs, &devInfo, DEV_PROP_HARDWAREID, hwId, _countof(hwId))) {
            if(*hwId && strstr(hwId, PCI_NODE)) {
                if(GetDeviceIrqResource(devs, &devInfo, &irqLine)) {
                    di = (DEVIRQL *)realloc(prevDi = di, (irqCount+1)*sizeof(*di));
                    if(!di) {
                        goto Exit;
                    }
                    memcpy(di[irqCount].HardwareId, hwId, MAX_DEVICE_ID_LEN);
                    di[irqCount].Irql = irqLine;
                    if(strstr(hwId, DeviceId)) {
                        irqDevice = irqLine;
                    }
                    di[irqCount].Description[0] = '\0';
                    GetDeviceProperty(devs, &devInfo, DEV_PROP_DESCRIPTION, di[irqCount].Description, _countof(di[irqCount].Description));
                    irqCount ++;
                }
            }
        }
    }
    for(devIndex = 0; devIndex < irqCount; devIndex ++) {
        if(di[devIndex].Irql == irqDevice) {
            sameIrq ++;
        }
    }
    if(sameIrq > 1) {
        printf("%d devices share the same interrupt line %d:\n", sameIrq, irqDevice);
        for(devIndex = 0; devIndex < irqCount; devIndex ++) {
            if(di[devIndex].Irql == irqDevice) {
                printf("  %s\n  > %s\n", di[devIndex].Description, di[devIndex].HardwareId);
            }
        }
    } else if(sameIrq == 1) {
        printf("One device has the interrupt line %d assigned:\n  %s\n  > %s\n",
               irqDevice, di[0].Description, di[0].HardwareId);
    } else {
        printf("No device with id %s* has an interrupt line assigned.\n",
               DeviceId);
    }
    printf("\n");
  
Exit:
    if(!di) {
        free(prevDi);
    } else {
        free(di);
    }
    if(devs != INVALID_HANDLE_VALUE) {
        SetupDiDestroyDeviceInfoList(devs);
    }
    return sameIrq;
}

BOOL GetDeviceProperty(HDEVINFO Devs, PSP_DEVINFO_DATA DevInfo, DWORD Flag, char *Property, UINT Count)
{
    char *buffer = NULL;
    DWORD size;
    DWORD reqSize;
    DWORD dataType;
    BOOL ret = FALSE;

    if(Flag == DEV_PROP_HARDWAREID) {
        Flag = SPDRP_HARDWAREID;
    } else {
        Flag = SPDRP_DEVICEDESC;
    }
    size = 0;
    SetupDiGetDeviceRegistryProperty(Devs, DevInfo, Flag, &dataType, (LPBYTE)Property, size, &reqSize);
    if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
        goto Exit;
    }
    buffer = (char *)malloc(reqSize+2);
    if(!buffer) {
        goto Exit;
    }
    size = reqSize;
    if(!SetupDiGetDeviceRegistryProperty(Devs, DevInfo, Flag, &dataType, (LPBYTE)buffer, size, &reqSize)) {
        goto Exit;
    }
    strncpy(Property, buffer, Count);
    Property[Count-1] = '\0';
    ret = TRUE;

Exit:
    if(buffer) {
        free(buffer);
    }
    return ret;
}

BOOL GetDeviceIrqResource(HDEVINFO Devs, PSP_DEVINFO_DATA DevInfo, PDWORD IrqLine)
{
    SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
    ULONG status = 0;
    ULONG problem = 0;
    LOG_CONF config = 0;
    RES_DES prevResDes;
    RES_DES resDes = 0;
    BOOL ret = FALSE;
    RESOURCEID resId = ResType_IRQ;
    ULONG dataSize;
    PBYTE resDesData;

    devInfoListDetail.cbSize = sizeof(devInfoListDetail);
    if((!SetupDiGetDeviceInfoListDetail(Devs,&devInfoListDetail)) ||
            (CM_Get_DevNode_Status_Ex(&status,&problem,DevInfo->DevInst,0,devInfoListDetail.RemoteMachineHandle)!=CR_SUCCESS)) {
        return FALSE;
    }
    if(!(status & DN_HAS_PROBLEM)) {
        if (CM_Get_First_Log_Conf_Ex(&config,
                                     DevInfo->DevInst,
                                     ALLOC_LOG_CONF,
                                     devInfoListDetail.RemoteMachineHandle) == CR_SUCCESS) {
            ret = TRUE;
        }
    }
    if(!ret) {
        if (CM_Get_First_Log_Conf_Ex(&config,
                                     DevInfo->DevInst,
                                     FORCED_LOG_CONF,
                                     devInfoListDetail.RemoteMachineHandle) == CR_SUCCESS) {
            ret = TRUE;
        }
    }
    if(!ret) {
        if(!(status & DN_HAS_PROBLEM) || (problem != CM_PROB_HARDWARE_DISABLED)) {
            if (CM_Get_First_Log_Conf_Ex(&config,
                                         DevInfo->DevInst,
                                         BOOT_LOG_CONF,
                                         devInfoListDetail.RemoteMachineHandle) == CR_SUCCESS) {
                ret = TRUE;
            }
        }
    }
    if(!ret) {
        return ret;
    }
    prevResDes = (RES_DES)config;
    while(CM_Get_Next_Res_Des_Ex(&resDes, prevResDes, ResType_IRQ, &resId, 0, devInfoListDetail.RemoteMachineHandle)==CR_SUCCESS) {
        if(prevResDes != config) {
            CM_Free_Res_Des_Handle(prevResDes);
        }
        prevResDes = resDes;
        if(CM_Get_Res_Des_Data_Size_Ex(&dataSize,resDes,0, devInfoListDetail.RemoteMachineHandle)!=CR_SUCCESS) {
            continue;
        }
        resDesData = (PBYTE)malloc(dataSize);
        if(!resDesData) {
            continue;
        }
        if(CM_Get_Res_Des_Data_Ex(resDes, resDesData, dataSize, 0, devInfoListDetail.RemoteMachineHandle) != CR_SUCCESS) {
            free(resDesData);
            continue;
        }
        *IrqLine = ((PIRQ_RESOURCE)resDesData)->IRQ_Header.IRQD_Alloc_Num;
        free(resDesData);
        ret = TRUE;
        break;
    }
    if(prevResDes != config) {
        CM_Free_Res_Des_Handle(prevResDes);
    }
    CM_Free_Log_Conf_Handle(config);
    return ret;
}
1 Like

!idt in the debugger will also tell you if you’re sharing.