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

📄 device_main.c

📁 VIA VT6655 x86下的Linux Source Code
💻 C
📖 第 1 页 / 共 5 页
字号:
    if (dev)
        unregister_netdev(dev);        
    
    if (pDevice->PortOffset)
        iounmap((PVOID)pDevice->PortOffset);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
    if (pDevice->pcid)   
        pci_release_regions(pDevice->pcid);
    if (dev)                
        free_netdev(dev);
#else
    if (pDevice->ioaddr)
        release_region(pDevice->ioaddr,pDevice->io_size);
    if (dev)        
        kfree(dev);
#endif

    if (pDevice->pcid) {
        pci_set_drvdata(pDevice->pcid,NULL);
    }
    kfree(pDevice);
    
}
#endif// ifndef PRIVATE_OBJ

static BOOL device_init_rings(PSDevice pDevice) {
    void*   vir_pool;

    
    /*allocate all RD/TD rings a single pool*/      
    vir_pool = pci_alloc_consistent(pDevice->pcid, 
                    pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
                    pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
                    pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
                    pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
                    &pDevice->pool_dma);
                    
    if (vir_pool == NULL) {
        DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s : allocate desc dma memory failed\n", pDevice->dev->name);
        return FALSE;
    }
    
    memset(vir_pool, 0, 
            pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
            pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +            
            pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
            pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc) 
          );

    pDevice->aRD0Ring = vir_pool;
    pDevice->aRD1Ring = vir_pool + 
                        pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
                            

    pDevice->rd0_pool_dma = pDevice->pool_dma;
    pDevice->rd1_pool_dma = pDevice->rd0_pool_dma + 
                            pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);

    pDevice->tx0_bufs = pci_alloc_consistent(pDevice->pcid,
                    pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
                    pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
                    CB_BEACON_BUF_SIZE +
                    CB_MAX_BUF_SIZE,
                    &pDevice->tx_bufs_dma0);
            
    if (pDevice->tx0_bufs == NULL) {
        DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: allocate buf dma memory failed\n", pDevice->dev->name);
        pci_free_consistent(pDevice->pcid, 
            pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
            pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +             
            pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
            pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
            vir_pool, pDevice->pool_dma
            );
        return FALSE;
    }   
    
    memset(pDevice->tx0_bufs, 0,
           pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
           pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
           CB_BEACON_BUF_SIZE +
           CB_MAX_BUF_SIZE
          );  
    
    pDevice->td0_pool_dma = pDevice->rd1_pool_dma +
            pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);

    pDevice->td1_pool_dma = pDevice->td0_pool_dma +
            pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
            

    // vir_pool: pvoid type
    pDevice->apTD0Rings = vir_pool 
                          + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
                          + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
                          
    pDevice->apTD1Rings = vir_pool 
            + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
            + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc)
            + pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
            
                        
    pDevice->tx1_bufs = pDevice->tx0_bufs +
            pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;

            
    pDevice->tx_beacon_bufs = pDevice->tx1_bufs +
            pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
            
    pDevice->pbyTmpBuff = pDevice->tx_beacon_bufs +
            CB_BEACON_BUF_SIZE;

    pDevice->tx_bufs_dma1 = pDevice->tx_bufs_dma0 +
            pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;

            
    pDevice->tx_beacon_dma = pDevice->tx_bufs_dma1 +
            pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;            
            
            
    return TRUE;    
}

static void device_free_rings(PSDevice pDevice) {
    
    pci_free_consistent(pDevice->pcid,
            pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
            pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +             
            pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
            pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc)
            ,        
            pDevice->aRD0Ring, pDevice->pool_dma
        );
        
    if (pDevice->tx0_bufs)
        pci_free_consistent(pDevice->pcid,
           pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
           pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
           CB_BEACON_BUF_SIZE +
           CB_MAX_BUF_SIZE,
           pDevice->tx0_bufs, pDevice->tx_bufs_dma0
        );
}

static void device_init_rd0_ring(PSDevice pDevice) {
    int i;
    dma_addr_t      curr = pDevice->rd0_pool_dma;
    PSRxDesc        pDesc;
    
    /* Init the RD0 ring entries */
    for (i = 0; i < pDevice->sOpts.nRxDescs0; i ++, curr += sizeof(SRxDesc)) {
        pDesc = &(pDevice->aRD0Ring[i]);
        pDesc->pRDInfo = alloc_rd_info();
        ASSERT(pDesc->pRDInfo);
        if (!device_alloc_rx_buf(pDevice, pDesc)) {
            DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc rx bufs\n",
            pDevice->dev->name);
        }
        pDesc->next = &(pDevice->aRD0Ring[(i+1) % pDevice->sOpts.nRxDescs0]);
        pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
        pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
    }

    pDevice->aRD0Ring[i-1].next_desc = cpu_to_le32(pDevice->rd0_pool_dma);
    pDevice->pCurrRD[0] = &(pDevice->aRD0Ring[0]);
}


static void device_init_rd1_ring(PSDevice pDevice) {
    int i;
    dma_addr_t      curr = pDevice->rd1_pool_dma;
    PSRxDesc        pDesc;

    /* Init the RD1 ring entries */
    for (i = 0; i < pDevice->sOpts.nRxDescs1; i ++, curr += sizeof(SRxDesc)) {
        pDesc = &(pDevice->aRD1Ring[i]);
        pDesc->pRDInfo = alloc_rd_info();
        ASSERT(pDesc->pRDInfo);
        if (!device_alloc_rx_buf(pDevice, pDesc)) {
            DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc rx bufs\n",
            pDevice->dev->name);
        }
        pDesc->next = &(pDevice->aRD1Ring[(i+1) % pDevice->sOpts.nRxDescs1]);
        pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
        pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
    }

    pDevice->aRD1Ring[i-1].next_desc = cpu_to_le32(pDevice->rd1_pool_dma);
    pDevice->pCurrRD[1] = &(pDevice->aRD1Ring[0]);
}    


static void device_init_defrag_cb(PSDevice pDevice) {
    int i;    
    PSDeFragControlBlock pDeF;    

    /* Init the fragment ctl entries */
    for (i = 0; i < CB_MAX_RX_FRAG; i++) {
        pDeF = &(pDevice->sRxDFCB[i]);
        if (!device_alloc_frag_buf(pDevice, pDeF)) {
            DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc frag bufs\n",
                pDevice->dev->name);
        };
    }
    pDevice->cbDFCB = CB_MAX_RX_FRAG;
    pDevice->cbFreeDFCB = pDevice->cbDFCB; 
}        
    
    


static void device_free_rd0_ring(PSDevice pDevice) { 
    int i;

    for (i = 0; i < pDevice->sOpts.nRxDescs0; i++) {
        PSRxDesc        pDesc =&(pDevice->aRD0Ring[i]);
        PDEVICE_RD_INFO  pRDInfo =pDesc->pRDInfo;
        
        pci_unmap_single(pDevice->pcid,pRDInfo->skb_dma,
           pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
           
        dev_kfree_skb(pRDInfo->skb);
        
        kfree((PVOID)pDesc->pRDInfo);
    }

}

static void device_free_rd1_ring(PSDevice pDevice) { 
    int i;

    
    for (i = 0; i < pDevice->sOpts.nRxDescs1; i++) {
        PSRxDesc        pDesc=&(pDevice->aRD1Ring[i]);
        PDEVICE_RD_INFO  pRDInfo=pDesc->pRDInfo;
        
        pci_unmap_single(pDevice->pcid,pRDInfo->skb_dma,
           pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
           
        dev_kfree_skb(pRDInfo->skb);
        
        kfree((PVOID)pDesc->pRDInfo);
    }

}

static void device_free_frag_buf(PSDevice pDevice) { 
    PSDeFragControlBlock pDeF;    
    int i;
    
    for (i = 0; i < CB_MAX_RX_FRAG; i++) {
        
        pDeF = &(pDevice->sRxDFCB[i]);        
        
        if (pDeF->skb)
            dev_kfree_skb(pDeF->skb);
        
    }

}

static void device_init_td0_ring(PSDevice pDevice) {
    int i;
    dma_addr_t  curr;
    PSTxDesc        pDesc;
    
    curr = pDevice->td0_pool_dma;
    for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++, curr += sizeof(STxDesc)) {
        pDesc = &(pDevice->apTD0Rings[i]);
        pDesc->pTDInfo = alloc_td_info();
        ASSERT(pDesc->pTDInfo);
        if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
            pDesc->pTDInfo->buf = pDevice->tx0_bufs + (i)*PKT_BUF_SZ;
            pDesc->pTDInfo->buf_dma = pDevice->tx_bufs_dma0 + (i)*PKT_BUF_SZ;
        }        
        pDesc->next =&(pDevice->apTD0Rings[(i+1) % pDevice->sOpts.nTxDescs[0]]);
        pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
        pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
    }
    
    pDevice->apTD0Rings[i-1].next_desc = cpu_to_le32(pDevice->td0_pool_dma);
    pDevice->apTailTD[0] = pDevice->apCurrTD[0] =&(pDevice->apTD0Rings[0]);

}

static void device_init_td1_ring(PSDevice pDevice) {
    int i;
    dma_addr_t  curr;
    PSTxDesc    pDesc;
    
    /* Init the TD ring entries */
    curr=pDevice->td1_pool_dma;
    for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++, curr+=sizeof(STxDesc)) {
        pDesc=&(pDevice->apTD1Rings[i]);
        pDesc->pTDInfo = alloc_td_info();
        ASSERT(pDesc->pTDInfo);
        if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
            pDesc->pTDInfo->buf=pDevice->tx1_bufs+(i)*PKT_BUF_SZ;
            pDesc->pTDInfo->buf_dma=pDevice->tx_bufs_dma1+(i)*PKT_BUF_SZ;
        }                
        pDesc->next=&(pDevice->apTD1Rings[(i+1) % pDevice->sOpts.nTxDescs[1]]);
        pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
        pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
    }
    
    pDevice->apTD1Rings[i-1].next_desc = cpu_to_le32(pDevice->td1_pool_dma);
    pDevice->apTailTD[1] = pDevice->apCurrTD[1] = &(pDevice->apTD1Rings[0]);
}



static void device_free_td0_ring(PSDevice pDevice) {
    int i;
    for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++) {
        PSTxDesc        pDesc=&(pDevice->apTD0Rings[i]);
        PDEVICE_TD_INFO  pTDInfo=pDesc->pTDInfo;

        if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
            pci_unmap_single(pDevice->pcid,pTDInfo->skb_dma,
               pTDInfo->skb->len, PCI_DMA_TODEVICE);
           
        if (pTDInfo->skb)
            dev_kfree_skb(pTDInfo->skb);
           
        kfree((PVOID)pDesc->pTDInfo);
    }   
}

static void device_free_td1_ring(PSDevice pDevice) {
    int i;
    
    for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++) {
        PSTxDesc        pDesc=&(pDevice->apTD1Rings[i]);
        PDEVICE_TD_INFO  pTDInfo=pDesc->pTDInfo;

        if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))        
            pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma,
               pTDInfo->skb->len, PCI_DMA_TODEVICE);

        if (pTDInfo->skb) 
            dev_kfree_skb(pTDInfo->skb);
           
        kfree((PVOID)pDesc->pTDInfo);
    }   

}



/*-----------------------------------------------------------------*/

static int device_rx_srv(PSDevice pDevice, UINT uIdx) {
    PSRxDesc    pRD;
    int works = 0;


    for (pRD = pDevice->pCurrRD[uIdx];
         pRD->m_rd0RD0.f1Owner == OWNED_BY_HOST;
         pRD = pRD->next) {
//        DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->pCurrRD = %x, works = %d\n", pRD, works);
        if (works++>15)
            break;
        if (device_receive_frame(pDevice, pRD)) {
            if (!device_alloc_rx_buf(pDevice,pRD)) {
                    DEVICE_PRT(MSG_LEVEL_ERR, KERN_ERR
                    "%s: can not allocate rx buf\n", pDevice->dev->name); 
                    break;
            }
        }

⌨️ 快捷键说明

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