sonic.c
来自「linux 内核源代码」· C语言 代码 · 共 743 行 · 第 1/2 页
C
743 行
/* * sonic.c * * (C) 2005 Finn Thain * * Converted to DMA API, added zero-copy buffer handling, and * (from the mac68k project) introduced dhd's support for 16-bit cards. * * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de) * * This driver is based on work from Andreas Busse, but most of * the code is rewritten. * * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de) * * Core code included by system sonic drivers * * And... partially rewritten again by David Huggins-Daines in order * to cope with screwed up Macintosh NICs that may or may not use * 16-bit DMA. * * (C) 1999 David Huggins-Daines <dhd@debian.org> * *//* * Sources: Olivetti M700-10 Risc Personal Computer hardware handbook, * National Semiconductors data sheet for the DP83932B Sonic Ethernet * controller, and the files "8390.c" and "skeleton.c" in this directory. * * Additional sources: Nat Semi data sheet for the DP83932C and Nat Semi * Application Note AN-746, the files "lance.c" and "ibmlana.c". See also * the NetBSD file "sys/arch/mac68k/dev/if_sn.c". *//* * Open/initialize the SONIC controller. * * This routine should set everything up anew at each open, even * registers that "should" only need to be set once at boot, so that * there is non-reboot way to recover if something goes wrong. */static int sonic_open(struct net_device *dev){ struct sonic_local *lp = netdev_priv(dev); int i; if (sonic_debug > 2) printk("sonic_open: initializing sonic driver.\n"); for (i = 0; i < SONIC_NUM_RRS; i++) { struct sk_buff *skb = dev_alloc_skb(SONIC_RBSIZE + 2); if (skb == NULL) { while(i > 0) { /* free any that were allocated successfully */ i--; dev_kfree_skb(lp->rx_skb[i]); lp->rx_skb[i] = NULL; } printk(KERN_ERR "%s: couldn't allocate receive buffers\n", dev->name); return -ENOMEM; } /* align IP header unless DMA requires otherwise */ if (SONIC_BUS_SCALE(lp->dma_bitmode) == 2) skb_reserve(skb, 2); lp->rx_skb[i] = skb; } for (i = 0; i < SONIC_NUM_RRS; i++) { dma_addr_t laddr = dma_map_single(lp->device, skb_put(lp->rx_skb[i], SONIC_RBSIZE), SONIC_RBSIZE, DMA_FROM_DEVICE); if (!laddr) { while(i > 0) { /* free any that were mapped successfully */ i--; dma_unmap_single(lp->device, lp->rx_laddr[i], SONIC_RBSIZE, DMA_FROM_DEVICE); lp->rx_laddr[i] = (dma_addr_t)0; } for (i = 0; i < SONIC_NUM_RRS; i++) { dev_kfree_skb(lp->rx_skb[i]); lp->rx_skb[i] = NULL; } printk(KERN_ERR "%s: couldn't map rx DMA buffers\n", dev->name); return -ENOMEM; } lp->rx_laddr[i] = laddr; } /* * Initialize the SONIC */ sonic_init(dev); netif_start_queue(dev); if (sonic_debug > 2) printk("sonic_open: Initialization done.\n"); return 0;}/* * Close the SONIC device */static int sonic_close(struct net_device *dev){ struct sonic_local *lp = netdev_priv(dev); int i; if (sonic_debug > 2) printk("sonic_close\n"); netif_stop_queue(dev); /* * stop the SONIC, disable interrupts */ SONIC_WRITE(SONIC_IMR, 0); SONIC_WRITE(SONIC_ISR, 0x7fff); SONIC_WRITE(SONIC_CMD, SONIC_CR_RST); /* unmap and free skbs that haven't been transmitted */ for (i = 0; i < SONIC_NUM_TDS; i++) { if(lp->tx_laddr[i]) { dma_unmap_single(lp->device, lp->tx_laddr[i], lp->tx_len[i], DMA_TO_DEVICE); lp->tx_laddr[i] = (dma_addr_t)0; } if(lp->tx_skb[i]) { dev_kfree_skb(lp->tx_skb[i]); lp->tx_skb[i] = NULL; } } /* unmap and free the receive buffers */ for (i = 0; i < SONIC_NUM_RRS; i++) { if(lp->rx_laddr[i]) { dma_unmap_single(lp->device, lp->rx_laddr[i], SONIC_RBSIZE, DMA_FROM_DEVICE); lp->rx_laddr[i] = (dma_addr_t)0; } if(lp->rx_skb[i]) { dev_kfree_skb(lp->rx_skb[i]); lp->rx_skb[i] = NULL; } } return 0;}static void sonic_tx_timeout(struct net_device *dev){ struct sonic_local *lp = netdev_priv(dev); int i; /* * put the Sonic into software-reset mode and * disable all interrupts before releasing DMA buffers */ SONIC_WRITE(SONIC_IMR, 0); SONIC_WRITE(SONIC_ISR, 0x7fff); SONIC_WRITE(SONIC_CMD, SONIC_CR_RST); /* We could resend the original skbs. Easier to re-initialise. */ for (i = 0; i < SONIC_NUM_TDS; i++) { if(lp->tx_laddr[i]) { dma_unmap_single(lp->device, lp->tx_laddr[i], lp->tx_len[i], DMA_TO_DEVICE); lp->tx_laddr[i] = (dma_addr_t)0; } if(lp->tx_skb[i]) { dev_kfree_skb(lp->tx_skb[i]); lp->tx_skb[i] = NULL; } } /* Try to restart the adaptor. */ sonic_init(dev); lp->stats.tx_errors++; dev->trans_start = jiffies; netif_wake_queue(dev);}/* * transmit packet * * Appends new TD during transmission thus avoiding any TX interrupts * until we run out of TDs. * This routine interacts closely with the ISR in that it may, * set tx_skb[i] * reset the status flags of the new TD * set and reset EOL flags * stop the tx queue * The ISR interacts with this routine in various ways. It may, * reset tx_skb[i] * test the EOL and status flags of the TDs * wake the tx queue * Concurrently with all of this, the SONIC is potentially writing to * the status flags of the TDs. * Until some mutual exclusion is added, this code will not work with SMP. However, * MIPS Jazz machines and m68k Macs were all uni-processor machines. */static int sonic_send_packet(struct sk_buff *skb, struct net_device *dev){ struct sonic_local *lp = netdev_priv(dev); dma_addr_t laddr; int length; int entry = lp->next_tx; if (sonic_debug > 2) printk("sonic_send_packet: skb=%p, dev=%p\n", skb, dev); length = skb->len; if (length < ETH_ZLEN) { if (skb_padto(skb, ETH_ZLEN)) return 0; length = ETH_ZLEN; } /* * Map the packet data into the logical DMA address space */ laddr = dma_map_single(lp->device, skb->data, length, DMA_TO_DEVICE); if (!laddr) { printk(KERN_ERR "%s: failed to map tx DMA buffer.\n", dev->name); dev_kfree_skb(skb); return 1; } sonic_tda_put(dev, entry, SONIC_TD_STATUS, 0); /* clear status */ sonic_tda_put(dev, entry, SONIC_TD_FRAG_COUNT, 1); /* single fragment */ sonic_tda_put(dev, entry, SONIC_TD_PKTSIZE, length); /* length of packet */ sonic_tda_put(dev, entry, SONIC_TD_FRAG_PTR_L, laddr & 0xffff); sonic_tda_put(dev, entry, SONIC_TD_FRAG_PTR_H, laddr >> 16); sonic_tda_put(dev, entry, SONIC_TD_FRAG_SIZE, length); sonic_tda_put(dev, entry, SONIC_TD_LINK, sonic_tda_get(dev, entry, SONIC_TD_LINK) | SONIC_EOL); /* * Must set tx_skb[entry] only after clearing status, and * before clearing EOL and before stopping queue */ wmb(); lp->tx_len[entry] = length; lp->tx_laddr[entry] = laddr; lp->tx_skb[entry] = skb; wmb(); sonic_tda_put(dev, lp->eol_tx, SONIC_TD_LINK, sonic_tda_get(dev, lp->eol_tx, SONIC_TD_LINK) & ~SONIC_EOL); lp->eol_tx = entry; lp->next_tx = (entry + 1) & SONIC_TDS_MASK; if (lp->tx_skb[lp->next_tx] != NULL) { /* The ring is full, the ISR has yet to process the next TD. */ if (sonic_debug > 3) printk("%s: stopping queue\n", dev->name); netif_stop_queue(dev); /* after this packet, wait for ISR to free up some TDAs */ } else netif_start_queue(dev); if (sonic_debug > 2) printk("sonic_send_packet: issuing Tx command\n"); SONIC_WRITE(SONIC_CMD, SONIC_CR_TXP); dev->trans_start = jiffies; return 0;}/* * The typical workload of the driver: * Handle the network interface interrupts. */static irqreturn_t sonic_interrupt(int irq, void *dev_id){ struct net_device *dev = dev_id; struct sonic_local *lp = netdev_priv(dev); int status; if (!(status = SONIC_READ(SONIC_ISR) & SONIC_IMR_DEFAULT)) return IRQ_NONE; do { if (status & SONIC_INT_PKTRX) { if (sonic_debug > 2) printk("%s: packet rx\n", dev->name); sonic_rx(dev); /* got packet(s) */ SONIC_WRITE(SONIC_ISR, SONIC_INT_PKTRX); /* clear the interrupt */ } if (status & SONIC_INT_TXDN) { int entry = lp->cur_tx; int td_status; int freed_some = 0; /* At this point, cur_tx is the index of a TD that is one of: * unallocated/freed (status set & tx_skb[entry] clear) * allocated and sent (status set & tx_skb[entry] set ) * allocated and not yet sent (status clear & tx_skb[entry] set ) * still being allocated by sonic_send_packet (status clear & tx_skb[entry] clear) */ if (sonic_debug > 2) printk("%s: tx done\n", dev->name); while (lp->tx_skb[entry] != NULL) { if ((td_status = sonic_tda_get(dev, entry, SONIC_TD_STATUS)) == 0) break; if (td_status & 0x0001) { lp->stats.tx_packets++; lp->stats.tx_bytes += sonic_tda_get(dev, entry, SONIC_TD_PKTSIZE); } else { lp->stats.tx_errors++; if (td_status & 0x0642) lp->stats.tx_aborted_errors++; if (td_status & 0x0180) lp->stats.tx_carrier_errors++; if (td_status & 0x0020) lp->stats.tx_window_errors++; if (td_status & 0x0004) lp->stats.tx_fifo_errors++; } /* We must free the original skb */ dev_kfree_skb_irq(lp->tx_skb[entry]); lp->tx_skb[entry] = NULL; /* and unmap DMA buffer */ dma_unmap_single(lp->device, lp->tx_laddr[entry], lp->tx_len[entry], DMA_TO_DEVICE); lp->tx_laddr[entry] = (dma_addr_t)0; freed_some = 1; if (sonic_tda_get(dev, entry, SONIC_TD_LINK) & SONIC_EOL) { entry = (entry + 1) & SONIC_TDS_MASK; break; } entry = (entry + 1) & SONIC_TDS_MASK; } if (freed_some || lp->tx_skb[entry] == NULL) netif_wake_queue(dev); /* The ring is no longer full */ lp->cur_tx = entry; SONIC_WRITE(SONIC_ISR, SONIC_INT_TXDN); /* clear the interrupt */ } /* * check error conditions */ if (status & SONIC_INT_RFO) { if (sonic_debug > 1) printk("%s: rx fifo overrun\n", dev->name); lp->stats.rx_fifo_errors++; SONIC_WRITE(SONIC_ISR, SONIC_INT_RFO); /* clear the interrupt */ } if (status & SONIC_INT_RDE) { if (sonic_debug > 1) printk("%s: rx descriptors exhausted\n", dev->name); lp->stats.rx_dropped++; SONIC_WRITE(SONIC_ISR, SONIC_INT_RDE); /* clear the interrupt */ } if (status & SONIC_INT_RBAE) { if (sonic_debug > 1) printk("%s: rx buffer area exceeded\n", dev->name); lp->stats.rx_dropped++; SONIC_WRITE(SONIC_ISR, SONIC_INT_RBAE); /* clear the interrupt */ } /* counter overruns; all counters are 16bit wide */ if (status & SONIC_INT_FAE) { lp->stats.rx_frame_errors += 65536; SONIC_WRITE(SONIC_ISR, SONIC_INT_FAE); /* clear the interrupt */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?