📄 3c359.c
字号:
* Now to write the microcode into the shared ram * The microcode must finish at position 0xFFFF, so we must subtract * to get the start position for the code */ start = (0xFFFF - (mc_size) + 1 ) ; /* Looks strange but ensures compiler only uses 16 bit unsigned int for this */ printk(KERN_INFO "3C359: Uploading Microcode: "); for (i = start, j = 0; j < mc_size; i++, j++) { writel(MEM_BYTE_WRITE | 0XD0000 | i, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(microcode[j],xl_mmio + MMIO_MACDATA) ; if (j % 1024 == 0) printk("."); } printk("\n") ; for (i=0;i < 16; i++) { writel( (MEM_BYTE_WRITE | 0xDFFF0) + i, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(microcode[mc_size - 16 + i], xl_mmio + MMIO_MACDATA) ; } /* * Have to write the start address of the upload to FFF4, but * the address must be >> 4. You do not want to know how long * it took me to discover this. */ writel(MEM_WORD_WRITE | 0xDFFF4, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writew(start >> 4, xl_mmio + MMIO_MACDATA); /* Clear the CPAttention, memWrEn Bit */ writel( (IO_BYTE_READ | CPATTENTION), xl_mmio + MMIO_MAC_ACCESS_CMD) ; result_8 = readb(xl_mmio + MMIO_MACDATA) ; result_8 = result_8 & ~CPA_MEMWREN ; writel( (IO_BYTE_WRITE | CPATTENTION), xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(result_8, xl_mmio + MMIO_MACDATA) ; /* Clear the cpHold bit in pmbar */ writel( (IO_WORD_READ | PMBAR),xl_mmio + MMIO_MAC_ACCESS_CMD); result_16 = readw(xl_mmio + MMIO_MACDATA) ; result_16 = result_16 & ~PMB_CPHOLD ; writel( (IO_WORD_WRITE | PMBAR), xl_mmio + MMIO_MAC_ACCESS_CMD) ; writew(result_16,xl_mmio + MMIO_MACDATA) ; } /* If microcode upload required */ /* * The card should now go though a self test procedure and get itself ready * to be opened, we must wait for an srb response with the initialization * information. */#if XL_DEBUG printk(KERN_INFO "%s: Microcode uploaded, must wait for the self test to complete\n", dev->name);#endif writew(SETINDENABLE | 0xFFF, xl_mmio + MMIO_COMMAND) ; t=jiffies; while ( !(readw(xl_mmio + MMIO_INTSTATUS_AUTO) & INTSTAT_SRB) ) { schedule(); if(jiffies-t > 15*HZ) { printk(KERN_ERR "3COM 3C359 Velocity XL card not responding.\n"); return -ENODEV; } } /* * Write the RxBufArea with D000, RxEarlyThresh, TxStartThresh, * DnPriReqThresh, read the tech docs if you want to know what * values they need to be. */ writel(MMIO_WORD_WRITE | RXBUFAREA, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writew(0xD000, xl_mmio + MMIO_MACDATA) ; writel(MMIO_WORD_WRITE | RXEARLYTHRESH, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writew(0X0020, xl_mmio + MMIO_MACDATA) ; writew( SETTXSTARTTHRESH | 0x40 , xl_mmio + MMIO_COMMAND) ; writeb(0x04, xl_mmio + MMIO_DNBURSTTHRESH) ; writeb(0x04, xl_mmio + DNPRIREQTHRESH) ; /* * Read WRBR to provide the location of the srb block, have to use byte reads not word reads. * Tech docs have this wrong !!!! */ writel(MMIO_BYTE_READ | WRBR, xl_mmio + MMIO_MAC_ACCESS_CMD) ; xl_priv->srb = readb(xl_mmio + MMIO_MACDATA) << 8 ; writel( (MMIO_BYTE_READ | WRBR) + 1, xl_mmio + MMIO_MAC_ACCESS_CMD) ; xl_priv->srb = xl_priv->srb | readb(xl_mmio + MMIO_MACDATA) ;#if XL_DEBUG writel(IO_WORD_READ | SWITCHSETTINGS, xl_mmio + MMIO_MAC_ACCESS_CMD) ; if ( readw(xl_mmio + MMIO_MACDATA) & 2) { printk(KERN_INFO "Default ring speed 4 mbps \n") ; } else { printk(KERN_INFO "Default ring speed 16 mbps \n") ; } printk(KERN_INFO "%s: xl_priv->srb = %04x\n",xl_priv->xl_card_name, xl_priv->srb);#endif return 0;}static int xl_open(struct net_device *dev) { struct xl_private *xl_priv=(struct xl_private *)dev->priv; u8 __iomem *xl_mmio = xl_priv->xl_mmio ; u8 i ; u16 hwaddr[3] ; /* Should be u8[6] but we get word return values */ int open_err ; u16 switchsettings, switchsettings_eeprom ; if(request_irq(dev->irq, &xl_interrupt, SA_SHIRQ , "3c359", dev)) { return -EAGAIN; } /* * Read the information from the EEPROM that we need. I know we * should use ntohs, but the word gets stored reversed in the 16 * bit field anyway and it all works its self out when we memcpy * it into dev->dev_addr. */ hwaddr[0] = xl_ee_read(dev,0x10) ; hwaddr[1] = xl_ee_read(dev,0x11) ; hwaddr[2] = xl_ee_read(dev,0x12) ; /* Ring speed */ switchsettings_eeprom = xl_ee_read(dev,0x08) ; switchsettings = switchsettings_eeprom ; if (xl_priv->xl_ring_speed != 0) { if (xl_priv->xl_ring_speed == 4) switchsettings = switchsettings | 0x02 ; else switchsettings = switchsettings & ~0x02 ; } /* Only write EEProm if there has been a change */ if (switchsettings != switchsettings_eeprom) { xl_ee_write(dev,0x08,switchsettings) ; /* Hardware reset after changing EEProm */ xl_hw_reset(dev) ; } memcpy(dev->dev_addr,hwaddr,dev->addr_len) ; open_err = xl_open_hw(dev) ; /* * This really needs to be cleaned up with better error reporting. */ if (open_err != 0) { /* Something went wrong with the open command */ if (open_err & 0x07) { /* Wrong speed, retry at different speed */ printk(KERN_WARNING "%s: Open Error, retrying at different ringspeed \n", dev->name) ; switchsettings = switchsettings ^ 2 ; xl_ee_write(dev,0x08,switchsettings) ; xl_hw_reset(dev) ; open_err = xl_open_hw(dev) ; if (open_err != 0) { printk(KERN_WARNING "%s: Open error returned a second time, we're bombing out now\n", dev->name); free_irq(dev->irq,dev) ; return -ENODEV ; } } else { printk(KERN_WARNING "%s: Open Error = %04x\n", dev->name, open_err) ; free_irq(dev->irq,dev) ; return -ENODEV ; } } /* * Now to set up the Rx and Tx buffer structures */ /* These MUST be on 8 byte boundaries */ xl_priv->xl_tx_ring = kmalloc((sizeof(struct xl_tx_desc) * XL_TX_RING_SIZE) + 7, GFP_DMA | GFP_KERNEL) ; if (xl_priv->xl_tx_ring == NULL) { printk(KERN_WARNING "%s: Not enough memory to allocate rx buffers.\n", dev->name); free_irq(dev->irq,dev); return -ENOMEM; } xl_priv->xl_rx_ring = kmalloc((sizeof(struct xl_rx_desc) * XL_RX_RING_SIZE) +7, GFP_DMA | GFP_KERNEL) ; if (xl_priv->xl_tx_ring == NULL) { printk(KERN_WARNING "%s: Not enough memory to allocate rx buffers.\n", dev->name); free_irq(dev->irq,dev); kfree(xl_priv->xl_tx_ring); return -ENOMEM; } memset(xl_priv->xl_tx_ring,0,sizeof(struct xl_tx_desc) * XL_TX_RING_SIZE) ; memset(xl_priv->xl_rx_ring,0,sizeof(struct xl_rx_desc) * XL_RX_RING_SIZE) ; /* Setup Rx Ring */ for (i=0 ; i < XL_RX_RING_SIZE ; i++) { struct sk_buff *skb ; skb = dev_alloc_skb(xl_priv->pkt_buf_sz) ; if (skb==NULL) break ; skb->dev = dev ; xl_priv->xl_rx_ring[i].upfragaddr = pci_map_single(xl_priv->pdev, skb->data,xl_priv->pkt_buf_sz, PCI_DMA_FROMDEVICE) ; xl_priv->xl_rx_ring[i].upfraglen = xl_priv->pkt_buf_sz | RXUPLASTFRAG; xl_priv->rx_ring_skb[i] = skb ; } if (i==0) { printk(KERN_WARNING "%s: Not enough memory to allocate rx buffers. Adapter disabled \n",dev->name) ; free_irq(dev->irq,dev) ; return -EIO ; } xl_priv->rx_ring_no = i ; xl_priv->rx_ring_tail = 0 ; xl_priv->rx_ring_dma_addr = pci_map_single(xl_priv->pdev,xl_priv->xl_rx_ring, sizeof(struct xl_rx_desc) * XL_RX_RING_SIZE, PCI_DMA_TODEVICE) ; for (i=0;i<(xl_priv->rx_ring_no-1);i++) { xl_priv->xl_rx_ring[i].upnextptr = xl_priv->rx_ring_dma_addr + (sizeof (struct xl_rx_desc) * (i+1)) ; } xl_priv->xl_rx_ring[i].upnextptr = 0 ; writel(xl_priv->rx_ring_dma_addr, xl_mmio + MMIO_UPLISTPTR) ; /* Setup Tx Ring */ xl_priv->tx_ring_dma_addr = pci_map_single(xl_priv->pdev,xl_priv->xl_tx_ring, sizeof(struct xl_tx_desc) * XL_TX_RING_SIZE,PCI_DMA_TODEVICE) ; xl_priv->tx_ring_head = 1 ; xl_priv->tx_ring_tail = 255 ; /* Special marker for first packet */ xl_priv->free_ring_entries = XL_TX_RING_SIZE ; /* * Setup the first dummy DPD entry for polling to start working. */ xl_priv->xl_tx_ring[0].framestartheader = TXDPDEMPTY ; xl_priv->xl_tx_ring[0].buffer = 0 ; xl_priv->xl_tx_ring[0].buffer_length = 0 ; xl_priv->xl_tx_ring[0].dnnextptr = 0 ; writel(xl_priv->tx_ring_dma_addr, xl_mmio + MMIO_DNLISTPTR) ; writel(DNUNSTALL, xl_mmio + MMIO_COMMAND) ; writel(UPUNSTALL, xl_mmio + MMIO_COMMAND) ; writel(DNENABLE, xl_mmio + MMIO_COMMAND) ; writeb(0x40, xl_mmio + MMIO_DNPOLL) ; /* * Enable interrupts on the card */ writel(SETINTENABLE | INT_MASK, xl_mmio + MMIO_COMMAND) ; writel(SETINDENABLE | INT_MASK, xl_mmio + MMIO_COMMAND) ; netif_start_queue(dev) ; return 0; } static int xl_open_hw(struct net_device *dev) { struct xl_private *xl_priv=(struct xl_private *)dev->priv; u8 __iomem *xl_mmio = xl_priv->xl_mmio ; u16 vsoff ; char ver_str[33]; int open_err ; int i ; unsigned long t ; /* * Okay, let's build up the Open.NIC srb command * */ writel( (MEM_BYTE_WRITE | 0xD0000 | xl_priv->srb), xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(OPEN_NIC, xl_mmio + MMIO_MACDATA) ; /* * Use this as a test byte, if it comes back with the same value, the command didn't work */ writel( (MEM_BYTE_WRITE | 0xD0000 | xl_priv->srb)+ 2, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(0xff,xl_mmio + MMIO_MACDATA) ; /* Open options */ writel( (MEM_BYTE_WRITE | 0xD0000 | xl_priv->srb) + 8, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(0x00, xl_mmio + MMIO_MACDATA) ; writel( (MEM_BYTE_WRITE | 0xD0000 | xl_priv->srb) + 9, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(0x00, xl_mmio + MMIO_MACDATA) ; /* * Node address, be careful here, the docs say you can just put zeros here and it will use * the hardware address, it doesn't, you must include the node address in the open command. */ if (xl_priv->xl_laa[0]) { /* If using a LAA address */ for (i=10;i<16;i++) { writel( (MEM_BYTE_WRITE | 0xD0000 | xl_priv->srb) + i, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(xl_priv->xl_laa[i],xl_mmio + MMIO_MACDATA) ; } memcpy(dev->dev_addr,xl_priv->xl_laa,dev->addr_len) ; } else { /* Regular hardware address */ for (i=10;i<16;i++) { writel( (MEM_BYTE_WRITE | 0xD0000 | xl_priv->srb) + i, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(dev->dev_addr[i-10], xl_mmio + MMIO_MACDATA) ; } } /* Default everything else to 0 */ for (i = 16; i < 34; i++) { writel( (MEM_BYTE_WRITE | 0xD0000 | xl_priv->srb) + i, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(0x00,xl_mmio + MMIO_MACDATA) ; } /* * Set the csrb bit in the MISR register */ xl_wait_misr_flags(dev) ; writel(MEM_BYTE_WRITE | MF_CSRB, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(0xFF, xl_mmio + MMIO_MACDATA) ; writel(MMIO_BYTE_WRITE | MISR_SET, xl_mmio + MMIO_MAC_ACCESS_CMD) ; writeb(MISR_CSRB , xl_mmio + MMIO_MACDATA) ; /* * Now wait for the command to run */ t=jiffies; while (! (readw(xl_mmio + MMIO_INTSTATUS) & INTSTAT_SRB)) { schedule(); if(jiffies-t > 40*HZ) { printk(KERN_ERR "3COM 3C359 Velocity XL card not responding.\n"); break ; } } /* * Let's interpret the open response */ writel( (MEM_BYTE_READ | 0xD0000 | xl_priv->srb)+2, xl_mmio + MMIO_MAC_ACCESS_CMD) ; if (readb(xl_mmio + MMIO_MACDATA)!=0) { open_err = readb(xl_mmio + MMIO_MACDATA) << 8 ; writel( (MEM_BYTE_READ | 0xD0000 | xl_priv->srb) + 7, xl_mmio + MMIO_MAC_ACCESS_CMD) ; open_err |= readb(xl_mmio + MMIO_MACDATA) ; return open_err ; } else { writel( (MEM_WORD_READ | 0xD0000 | xl_priv->srb) + 8, xl_mmio + MMIO_MAC_ACCESS_CMD) ; xl_priv->asb = ntohs(readw(xl_mmio + MMIO_MACDATA)) ; printk(KERN_INFO "%s: Adapter Opened Details: ",dev->name) ; printk("ASB: %04x",xl_priv->asb ) ; writel( (MEM_WORD_READ | 0xD0000 | xl_priv->srb) + 10, xl_mmio + MMIO_MAC_ACCESS_CMD) ; printk(", SRB: %04x",ntohs(readw(xl_mmio + MMIO_MACDATA)) ) ; writel( (MEM_WORD_READ | 0xD0000 | xl_priv->srb) + 12, xl_mmio + MMIO_MAC_ACCESS_CMD) ; xl_priv->arb = ntohs(readw(xl_mmio + MMIO_MACDATA)) ; printk(", ARB: %04x \n",xl_priv->arb ) ; writel( (MEM_WORD_READ | 0xD0000 | xl_priv->srb) + 14, xl_mmio + MMIO_MAC_ACCESS_CMD) ; vsoff = ntohs(readw(xl_mmio + MMIO_MACDATA)) ; /* * Interesting, sending the individual characters directly to printk was causing klogd to use * use 100% of processor time, so we build up the string and print that instead. */ for (i=0;i<0x20;i++) { writel( (MEM_BYTE_READ | 0xD0000 | vsoff) + i, xl_mmio + MMIO_MAC_ACCESS_CMD) ; ver_str[i] = readb(xl_mmio + MMIO_MACDATA) ; } ver_str[i] = '\0' ; printk(KERN_INFO "%s: Microcode version String: %s \n",dev->name,ver_str); } /* * Issue the AckInterrupt */ writew(ACK_INTERRUPT | SRBRACK | LATCH_ACK, xl_mmio + MMIO_COMMAND) ; return 0 ; }/* * There are two ways of implementing rx on the 359 NIC, either * interrupt driven or polling. We are going to uses interrupts, * it is the easier way of doing things. * * The Rx works with a ring of Rx descriptors. At initialise time the ring * entries point to the next entry except for the last entry in the ring * which points to 0. The card is programmed with the location of the first * available descriptor and keeps reading the next_ptr until next_ptr is set * to 0. Hopefully with a ring size of 16 the card will never get to read a next_ptr * of 0. As the Rx interrupt is received we copy the frame up to the protocol layers * and then point the end of the ring to our current position and point our current * position to 0, therefore making the current position the last position on the ring. * The last position on the ring therefore loops continually loops around the rx ring. * * rx_ring_tail is the position on the ring to process next. (Think of a snake, the head * expands as the card adds new packets and we go around eating the tail processing the * packets.) * * Undoubtably it could be streamlined and improved upon, but at the moment it works * and the fast path through the routine is fine. * * adv_rx_ring could be inlined to increase performance, but its called a *lot* of times * in xl_rx so would increase the size of the function significantly. */static void adv_rx_ring(struct net_device *dev) /* Advance rx_ring, cut down on bloat in xl_rx */ { struct xl_private *xl_priv=(struct xl_private *)dev->priv; int prev_ring_loc ; prev_ring_loc = (xl_priv->rx_ring_tail + XL_RX_RING_SIZE - 1) & (XL_RX_RING_SIZE - 1); xl_priv->xl_rx_ring[prev_ring_loc].upnextptr = xl_priv->rx_ring_dma_addr + (sizeof (struct xl_rx_desc) * xl_priv->rx_ring_tail) ; xl_priv->xl_rx_ring[xl_priv->rx_ring_tail].framestatus = 0 ; xl_priv->xl_rx_ring[xl_priv->rx_ring_tail].upnextptr = 0 ; xl_priv->rx_ring_tail++ ; xl_priv->rx_ring_tail &= (XL_RX_RING_SIZE-1) ; return ; }static void xl_rx(struct net_device *dev){ struct xl_private *xl_priv=(struct xl_private *)dev->priv; u8 __iomem * xl_mmio = xl_priv->xl_mmio ; struct sk_buff *skb, *skb2 ; int frame_length = 0, copy_len = 0 ; int temp_ring_loc ; /* * Receive the next frame, loop around the ring until all frames * have been received. */ while (xl_priv->xl_rx_ring[xl_priv->rx_ring_tail].framestatus & (RXUPDCOMPLETE | RXUPDFULL) ) { /* Descriptor to process */ if (xl_priv->xl_rx_ring[xl_priv->rx_ring_tail].framestatus & RXUPDFULL ) { /* UpdFull, Multiple Descriptors used for the frame */ /* * This is a pain, you need to go through all the descriptors until the last one * for this frame to find the framelength */ temp_ring_loc = xl_priv->rx_ring_tail ; while (xl_priv->xl_rx_ring[temp_ring_loc].framestatus & RXUPDFULL ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -