⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cancel.c

📁 PCI驱动编程实例
💻 C
📖 第 1 页 / 共 2 页
字号:
//
//    None.
//
//  IRQL:
//
//    This routine is called at IRQL DISPATCH_LEVEL
//
//  NOTES:
//
//
///////////////////////////////////////////////////////////////////////////////
VOID
OsrWatchdogTimer(PDEVICE_OBJECT DeviceObject,
                 PVOID Context) 
{
    POSR_DEVICE_EXT devExt;
    KIRQL oldIrql;

    devExt = DeviceObject->DeviceExtension;

    //
    // Take out the write queue lock while checking the in-progress write
    //
    KeAcquireSpinLock(&devExt->WriteQueueLock, &oldIrql);

    //
    // If there's a write operation in progress
    //
    if(devExt->CurrentWriteIrp) {

        //
        // If the watchdog count on the current write IRP has expired
        //
        if(! --((ULONG)(devExt->CurrentWriteIrp->Tail.Overlay.DriverContext[0])) ) {

#if DBG
            DbgPrint("OsrWatchdogTimer: WRITE killed 0x%0x\n", devExt->CurrentWriteIrp);
#endif
            //            
            // Soft reset the write processing on this device, and
            // queue a DpcForIsr() to cancel the in progress request.
            //
            (void)KeSynchronizeExecution(devExt->InterruptObject,
                                    CancelInProgressWrite,
                                    devExt);
        }

    }

    //
    // Drop the lock
    //
    KeReleaseSpinLock(&devExt->WriteQueueLock, oldIrql);


    //
    // Take out the read queue lock while checking the in-progress read
    //
    KeAcquireSpinLock(&devExt->ReadQueueLock, &oldIrql);

    //
    // If there's a write operation in progress
    //
    if(devExt->CurrentReadIrp) {

        //
        // If the watchdog count on the current write IRP has expired
        //
        if(! --((ULONG)(devExt->CurrentReadIrp->Tail.Overlay.DriverContext[0])) ) {

#if DBG
            DbgPrint("OsrWatchdogTimer: READ killed 0x%0x\n", devExt->CurrentReadIrp);
#endif
            //            
            // Soft reset the write processing on this device, and
            // queue a DpcForIsr() to cancel the in progress request.
            //
            (void)KeSynchronizeExecution(devExt->InterruptObject,
                                    CancelInProgressRead,
                                    devExt);
        }

    }

    //
    // Drop the lock
    //
    KeReleaseSpinLock(&devExt->ReadQueueLock, oldIrql);

}

///////////////////////////////////////////////////////////////////////////////
//
// CancelInProgressRead
//
//    This is a synchronize function, called with the ISR spinlock, and the
//    read queue spin lock HELD.
//    Returns with these locks HELD.
//
//  INPUTS:
//
//      SynchronizeContext - Address of our device extension.
//
//  OUTPUTS:
//
//      None.
//
//  RETURNS:
//
//      TRUE.
//
//  IRQL:
//
//      DIRQL
//
//  NOTES:
//
//
///////////////////////////////////////////////////////////////////////////////
BOOLEAN
CancelInProgressRead(IN PVOID SynchronizeContext)
{
    POSR_DEVICE_EXT devExt = (POSR_DEVICE_EXT)SynchronizeContext;
    ULONG temp;

    //
    // Get the current MCSR register value, mask off the bits that need
    // to be preserved, and CLEAR the WRITE_ENABLE bit... thus STOPPING
    // the transfer dead in its tracks. (Yes, it IS WRITE_ENABLE)
    //
    if(devExt->State > STATE_ALL_BELOW_NO_HW) {
        temp = READ_PORT_ULONG(devExt->AmccBaseRegisterAddress+MCSR_OFF);
    }

    //
    // Preserve these bits
    //
    temp &= (AMCC_MCSR_READ_ENABLE|
                AMCC_MCSR_READ_FIFO_MGMT|
                AMCC_MCSR_READ_PRIORITY|
                AMCC_MCSR_WRITE_ENABLE|
                AMCC_MCSR_WRITE_FIFO_MGMT|
                AMCC_MCSR_WRITE_PRIORITY);
    //
    // Clear this bit
    //
    temp &= ~AMCC_MCSR_WRITE_ENABLE;

    //
    // DO it on the device
    //
    if(devExt->State > STATE_ALL_BELOW_NO_HW) {
        WRITE_PORT_ULONG(devExt->AmccBaseRegisterAddress+MCSR_OFF, temp); 
    }

    //
    // Indicate the read is complete (setting _WRITE_COMP bit)
    //
    devExt->IntCsr |= AMCC_INT_WRITE_COMP;

    //
    // Indicate to the DpcforIsr that the request is being
    // cancelled.  The DpcForIsr specifically looks at the
    // ReadTotalLength flag to determine if the requsest is being
    // cancelled.
    //
    devExt->ReadTotalLength = 0;
    devExt->ReadSoFar = 0;
    
    //
    // Request the DPC for ISR, just as if the read were
    // ordinarily completing.  NOTE: NO CHANCE OF RACE HERE, since
    // we're HOLDING THE ISR SPINLOCK.
    //
    IoRequestDpc(devExt->FunctionalDeviceObject, 0, NULL);

    return(TRUE);
}    

///////////////////////////////////////////////////////////////////////////////
//
// CancelInProgressWrite
//
//    This is a synchronize function, called with the ISR spinlock,and the write
//    queue spin lock ALL HELD. Returns with these locks HELD.
//
//  INPUTS:
//
//      SynchronizeContext - Address of our device extension.
//
//  OUTPUTS:
//
//      None.
//
//  RETURNS:
//
//    TRUE.
//
//  IRQL:
//
//      DIRQL
//
//  NOTES:
//
//
///////////////////////////////////////////////////////////////////////////////
BOOLEAN
CancelInProgressWrite(IN PVOID SynchronizeContext)
{
    POSR_DEVICE_EXT devExt = (POSR_DEVICE_EXT)SynchronizeContext;
    ULONG temp;

    //
    // Get the current MCSR register value, mask off the bits that need
    // to be preserved, and CLEAR the READ_ENABLE bit... thus STOPPING
    // the transfer dead in its tracks.
    //
    if(devExt->State > STATE_ALL_BELOW_NO_HW) {
        temp = READ_PORT_ULONG(devExt->AmccBaseRegisterAddress+MCSR_OFF);
    }

    //
    // Preserve these bits
    //
    temp &= (AMCC_MCSR_READ_ENABLE|
                AMCC_MCSR_READ_FIFO_MGMT|
                AMCC_MCSR_READ_PRIORITY|
                AMCC_MCSR_WRITE_ENABLE|
                AMCC_MCSR_WRITE_FIFO_MGMT|
                AMCC_MCSR_WRITE_PRIORITY);

    //
    // Clearing READ_ENABLE stops the WRITE operation
    //
    temp &= ~AMCC_MCSR_READ_ENABLE;

    //
    // Tell it to the device
    //
    if(devExt->State > STATE_ALL_BELOW_NO_HW) {
        WRITE_PORT_ULONG(devExt->AmccBaseRegisterAddress+MCSR_OFF, temp); 
    }

    //
    // Fake the write completion by setting the appropriate bit in
    // our private copy of the IntCSR
    //
    devExt->IntCsr |= AMCC_INT_READ_COMP;

    //
    // Indicate to the DPC for ISR that the request is being
    // cancelled
    //
    devExt->WriteTotalLength = 0;
    devExt->WriteSoFar = 0;
    
    //
    // Request the DPC for ISR, just as if the read were
    // ordinarily completing.
    //
    IoRequestDpc(devExt->FunctionalDeviceObject, 0, NULL);

    return(TRUE);
}    

//
// OsrClearQueues
//
VOID OsrClearQueues(POSR_DEVICE_EXT DevExt)
{
    PLIST_ENTRY entry;
    PIRP irp;
    KIRQL oldIrql;

    //
    // Cancel everything on the read queue
    //
    KeAcquireSpinLock(&DevExt->ReadQueueLock, &oldIrql);

    //
    // Ensure the current read request gets cancelled at the first
    // opportunity
    //
    if(DevExt->CurrentReadIrp) {
        (ULONG)DevExt->CurrentReadIrp->Tail.Overlay.DriverContext[0] = 1;
    }

    while(TRUE) {

        entry = RemoveHeadList(&DevExt->ReadQueue);

        if(entry == &DevExt->ReadQueue) {
            break;
        }

        irp = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);

        irp->IoStatus.Status = STATUS_DEVICE_OFF_LINE;
        irp->IoStatus.Information = 0;
    
        OsrRequestDecrement(DevExt);

        IoCompleteRequest(irp, IO_NO_INCREMENT);

    }

    KeReleaseSpinLock(&DevExt->ReadQueueLock, oldIrql);


    //
    // Cancel everything on the write queue
    //
    KeAcquireSpinLock(&DevExt->WriteQueueLock, &oldIrql);

    //
    // Ensure the current write IRP gets cancelled in the next second
    //
    if(DevExt->CurrentWriteIrp) {
        (ULONG)DevExt->CurrentWriteIrp->Tail.Overlay.DriverContext[0] = 1;
    }

    while(TRUE) {

        entry = RemoveHeadList(&DevExt->WriteQueue);

        if(entry == &DevExt->WriteQueue) {
            break;
        }

        irp = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);

        irp->IoStatus.Status = STATUS_DEVICE_OFF_LINE;
        irp->IoStatus.Information = 0;
    
        OsrRequestDecrement(DevExt);

        IoCompleteRequest(irp, IO_NO_INCREMENT);

    }

    KeReleaseSpinLock(&DevExt->WriteQueueLock, oldIrql);


}    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -