1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2ded19addSJeff Kirsher /* 3ded19addSJeff Kirsher * Copyright (C) 2006-2007 PA Semi, Inc 4ded19addSJeff Kirsher * 5ded19addSJeff Kirsher * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs 6ded19addSJeff Kirsher */ 7ded19addSJeff Kirsher 8ded19addSJeff Kirsher #include <linux/module.h> 9ded19addSJeff Kirsher #include <linux/pci.h> 10ded19addSJeff Kirsher #include <linux/slab.h> 11ded19addSJeff Kirsher #include <linux/interrupt.h> 12ded19addSJeff Kirsher #include <linux/dmaengine.h> 13ded19addSJeff Kirsher #include <linux/delay.h> 14ded19addSJeff Kirsher #include <linux/netdevice.h> 15ded19addSJeff Kirsher #include <linux/of_mdio.h> 16ded19addSJeff Kirsher #include <linux/etherdevice.h> 17ded19addSJeff Kirsher #include <asm/dma-mapping.h> 18ded19addSJeff Kirsher #include <linux/in.h> 19ded19addSJeff Kirsher #include <linux/skbuff.h> 20ded19addSJeff Kirsher 21ded19addSJeff Kirsher #include <linux/ip.h> 22ded19addSJeff Kirsher #include <net/checksum.h> 23ded19addSJeff Kirsher #include <linux/prefetch.h> 24ded19addSJeff Kirsher 25ded19addSJeff Kirsher #include <asm/irq.h> 26ded19addSJeff Kirsher #include <asm/firmware.h> 27ded19addSJeff Kirsher #include <asm/pasemi_dma.h> 28ded19addSJeff Kirsher 29ded19addSJeff Kirsher #include "pasemi_mac.h" 30ded19addSJeff Kirsher 31ded19addSJeff Kirsher /* We have our own align, since ppc64 in general has it at 0 because 32ded19addSJeff Kirsher * of design flaws in some of the server bridge chips. However, for 33ded19addSJeff Kirsher * PWRficient doing the unaligned copies is more expensive than doing 34ded19addSJeff Kirsher * unaligned DMA, so make sure the data is aligned instead. 35ded19addSJeff Kirsher */ 36ded19addSJeff Kirsher #define LOCAL_SKB_ALIGN 2 37ded19addSJeff Kirsher 38ded19addSJeff Kirsher /* TODO list 39ded19addSJeff Kirsher * 40ded19addSJeff Kirsher * - Multicast support 41ded19addSJeff Kirsher * - Large MTU support 42ded19addSJeff Kirsher * - Multiqueue RX/TX 43ded19addSJeff Kirsher */ 44ded19addSJeff Kirsher 4544770e11SJarod Wilson #define PE_MIN_MTU (ETH_ZLEN + ETH_HLEN) 46ded19addSJeff Kirsher #define PE_MAX_MTU 9000 47ded19addSJeff Kirsher #define PE_DEF_MTU ETH_DATA_LEN 48ded19addSJeff Kirsher 49ded19addSJeff Kirsher #define DEFAULT_MSG_ENABLE \ 50ded19addSJeff Kirsher (NETIF_MSG_DRV | \ 51ded19addSJeff Kirsher NETIF_MSG_PROBE | \ 52ded19addSJeff Kirsher NETIF_MSG_LINK | \ 53ded19addSJeff Kirsher NETIF_MSG_TIMER | \ 54ded19addSJeff Kirsher NETIF_MSG_IFDOWN | \ 55ded19addSJeff Kirsher NETIF_MSG_IFUP | \ 56ded19addSJeff Kirsher NETIF_MSG_RX_ERR | \ 57ded19addSJeff Kirsher NETIF_MSG_TX_ERR) 58ded19addSJeff Kirsher 59ded19addSJeff Kirsher MODULE_LICENSE("GPL"); 60ded19addSJeff Kirsher MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>"); 61ded19addSJeff Kirsher MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver"); 62ded19addSJeff Kirsher 63ded19addSJeff Kirsher static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */ 64ded19addSJeff Kirsher module_param(debug, int, 0); 65ded19addSJeff Kirsher MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value"); 66ded19addSJeff Kirsher 67ded19addSJeff Kirsher extern const struct ethtool_ops pasemi_mac_ethtool_ops; 68ded19addSJeff Kirsher 69ded19addSJeff Kirsher static int translation_enabled(void) 70ded19addSJeff Kirsher { 71ded19addSJeff Kirsher #if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE) 72ded19addSJeff Kirsher return 1; 73ded19addSJeff Kirsher #else 74ded19addSJeff Kirsher return firmware_has_feature(FW_FEATURE_LPAR); 75ded19addSJeff Kirsher #endif 76ded19addSJeff Kirsher } 77ded19addSJeff Kirsher 78ded19addSJeff Kirsher static void write_iob_reg(unsigned int reg, unsigned int val) 79ded19addSJeff Kirsher { 80ded19addSJeff Kirsher pasemi_write_iob_reg(reg, val); 81ded19addSJeff Kirsher } 82ded19addSJeff Kirsher 83ded19addSJeff Kirsher static unsigned int read_mac_reg(const struct pasemi_mac *mac, unsigned int reg) 84ded19addSJeff Kirsher { 85ded19addSJeff Kirsher return pasemi_read_mac_reg(mac->dma_if, reg); 86ded19addSJeff Kirsher } 87ded19addSJeff Kirsher 88ded19addSJeff Kirsher static void write_mac_reg(const struct pasemi_mac *mac, unsigned int reg, 89ded19addSJeff Kirsher unsigned int val) 90ded19addSJeff Kirsher { 91ded19addSJeff Kirsher pasemi_write_mac_reg(mac->dma_if, reg, val); 92ded19addSJeff Kirsher } 93ded19addSJeff Kirsher 94ded19addSJeff Kirsher static unsigned int read_dma_reg(unsigned int reg) 95ded19addSJeff Kirsher { 96ded19addSJeff Kirsher return pasemi_read_dma_reg(reg); 97ded19addSJeff Kirsher } 98ded19addSJeff Kirsher 99ded19addSJeff Kirsher static void write_dma_reg(unsigned int reg, unsigned int val) 100ded19addSJeff Kirsher { 101ded19addSJeff Kirsher pasemi_write_dma_reg(reg, val); 102ded19addSJeff Kirsher } 103ded19addSJeff Kirsher 104ded19addSJeff Kirsher static struct pasemi_mac_rxring *rx_ring(const struct pasemi_mac *mac) 105ded19addSJeff Kirsher { 106ded19addSJeff Kirsher return mac->rx; 107ded19addSJeff Kirsher } 108ded19addSJeff Kirsher 109ded19addSJeff Kirsher static struct pasemi_mac_txring *tx_ring(const struct pasemi_mac *mac) 110ded19addSJeff Kirsher { 111ded19addSJeff Kirsher return mac->tx; 112ded19addSJeff Kirsher } 113ded19addSJeff Kirsher 114ded19addSJeff Kirsher static inline void prefetch_skb(const struct sk_buff *skb) 115ded19addSJeff Kirsher { 116ded19addSJeff Kirsher const void *d = skb; 117ded19addSJeff Kirsher 118ded19addSJeff Kirsher prefetch(d); 119ded19addSJeff Kirsher prefetch(d+64); 120ded19addSJeff Kirsher prefetch(d+128); 121ded19addSJeff Kirsher prefetch(d+192); 122ded19addSJeff Kirsher } 123ded19addSJeff Kirsher 124ded19addSJeff Kirsher static int mac_to_intf(struct pasemi_mac *mac) 125ded19addSJeff Kirsher { 126ded19addSJeff Kirsher struct pci_dev *pdev = mac->pdev; 127ded19addSJeff Kirsher u32 tmp; 128ded19addSJeff Kirsher int nintf, off, i, j; 129ded19addSJeff Kirsher int devfn = pdev->devfn; 130ded19addSJeff Kirsher 131ded19addSJeff Kirsher tmp = read_dma_reg(PAS_DMA_CAP_IFI); 132ded19addSJeff Kirsher nintf = (tmp & PAS_DMA_CAP_IFI_NIN_M) >> PAS_DMA_CAP_IFI_NIN_S; 133ded19addSJeff Kirsher off = (tmp & PAS_DMA_CAP_IFI_IOFF_M) >> PAS_DMA_CAP_IFI_IOFF_S; 134ded19addSJeff Kirsher 135ded19addSJeff Kirsher /* IOFF contains the offset to the registers containing the 136ded19addSJeff Kirsher * DMA interface-to-MAC-pci-id mappings, and NIN contains number 137ded19addSJeff Kirsher * of total interfaces. Each register contains 4 devfns. 138ded19addSJeff Kirsher * Just do a linear search until we find the devfn of the MAC 139ded19addSJeff Kirsher * we're trying to look up. 140ded19addSJeff Kirsher */ 141ded19addSJeff Kirsher 142ded19addSJeff Kirsher for (i = 0; i < (nintf+3)/4; i++) { 143ded19addSJeff Kirsher tmp = read_dma_reg(off+4*i); 144ded19addSJeff Kirsher for (j = 0; j < 4; j++) { 145ded19addSJeff Kirsher if (((tmp >> (8*j)) & 0xff) == devfn) 146ded19addSJeff Kirsher return i*4 + j; 147ded19addSJeff Kirsher } 148ded19addSJeff Kirsher } 149ded19addSJeff Kirsher return -1; 150ded19addSJeff Kirsher } 151ded19addSJeff Kirsher 152ded19addSJeff Kirsher static void pasemi_mac_intf_disable(struct pasemi_mac *mac) 153ded19addSJeff Kirsher { 154ded19addSJeff Kirsher unsigned int flags; 155ded19addSJeff Kirsher 156ded19addSJeff Kirsher flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG); 157ded19addSJeff Kirsher flags &= ~PAS_MAC_CFG_PCFG_PE; 158ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags); 159ded19addSJeff Kirsher } 160ded19addSJeff Kirsher 161ded19addSJeff Kirsher static void pasemi_mac_intf_enable(struct pasemi_mac *mac) 162ded19addSJeff Kirsher { 163ded19addSJeff Kirsher unsigned int flags; 164ded19addSJeff Kirsher 165ded19addSJeff Kirsher flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG); 166ded19addSJeff Kirsher flags |= PAS_MAC_CFG_PCFG_PE; 167ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags); 168ded19addSJeff Kirsher } 169ded19addSJeff Kirsher 170ded19addSJeff Kirsher static int pasemi_get_mac_addr(struct pasemi_mac *mac) 171ded19addSJeff Kirsher { 172ded19addSJeff Kirsher struct pci_dev *pdev = mac->pdev; 173ded19addSJeff Kirsher struct device_node *dn = pci_device_to_OF_node(pdev); 174ded19addSJeff Kirsher int len; 175ded19addSJeff Kirsher const u8 *maddr; 1761409a932SJoe Perches u8 addr[ETH_ALEN]; 177ded19addSJeff Kirsher 178ded19addSJeff Kirsher if (!dn) { 179ded19addSJeff Kirsher dev_dbg(&pdev->dev, 180ded19addSJeff Kirsher "No device node for mac, not configuring\n"); 181ded19addSJeff Kirsher return -ENOENT; 182ded19addSJeff Kirsher } 183ded19addSJeff Kirsher 184ded19addSJeff Kirsher maddr = of_get_property(dn, "local-mac-address", &len); 185ded19addSJeff Kirsher 1861409a932SJoe Perches if (maddr && len == ETH_ALEN) { 1871409a932SJoe Perches memcpy(mac->mac_addr, maddr, ETH_ALEN); 188ded19addSJeff Kirsher return 0; 189ded19addSJeff Kirsher } 190ded19addSJeff Kirsher 191ded19addSJeff Kirsher /* Some old versions of firmware mistakenly uses mac-address 192ded19addSJeff Kirsher * (and as a string) instead of a byte array in local-mac-address. 193ded19addSJeff Kirsher */ 194ded19addSJeff Kirsher 195ded19addSJeff Kirsher if (maddr == NULL) 196ded19addSJeff Kirsher maddr = of_get_property(dn, "mac-address", NULL); 197ded19addSJeff Kirsher 198ded19addSJeff Kirsher if (maddr == NULL) { 199ded19addSJeff Kirsher dev_warn(&pdev->dev, 200ded19addSJeff Kirsher "no mac address in device tree, not configuring\n"); 201ded19addSJeff Kirsher return -ENOENT; 202ded19addSJeff Kirsher } 203ded19addSJeff Kirsher 204143337c9SAndy Shevchenko if (!mac_pton(maddr, addr)) { 205ded19addSJeff Kirsher dev_warn(&pdev->dev, 206ded19addSJeff Kirsher "can't parse mac address, not configuring\n"); 207ded19addSJeff Kirsher return -EINVAL; 208ded19addSJeff Kirsher } 209ded19addSJeff Kirsher 2101409a932SJoe Perches memcpy(mac->mac_addr, addr, ETH_ALEN); 211ded19addSJeff Kirsher 212ded19addSJeff Kirsher return 0; 213ded19addSJeff Kirsher } 214ded19addSJeff Kirsher 215ded19addSJeff Kirsher static int pasemi_mac_set_mac_addr(struct net_device *dev, void *p) 216ded19addSJeff Kirsher { 217ded19addSJeff Kirsher struct pasemi_mac *mac = netdev_priv(dev); 218ded19addSJeff Kirsher struct sockaddr *addr = p; 219ded19addSJeff Kirsher unsigned int adr0, adr1; 220ded19addSJeff Kirsher 221ded19addSJeff Kirsher if (!is_valid_ether_addr(addr->sa_data)) 222504f9b5aSDanny Kukawka return -EADDRNOTAVAIL; 223ded19addSJeff Kirsher 224ded19addSJeff Kirsher memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); 225ded19addSJeff Kirsher 226ded19addSJeff Kirsher adr0 = dev->dev_addr[2] << 24 | 227ded19addSJeff Kirsher dev->dev_addr[3] << 16 | 228ded19addSJeff Kirsher dev->dev_addr[4] << 8 | 229ded19addSJeff Kirsher dev->dev_addr[5]; 230ded19addSJeff Kirsher adr1 = read_mac_reg(mac, PAS_MAC_CFG_ADR1); 231ded19addSJeff Kirsher adr1 &= ~0xffff; 232ded19addSJeff Kirsher adr1 |= dev->dev_addr[0] << 8 | dev->dev_addr[1]; 233ded19addSJeff Kirsher 234ded19addSJeff Kirsher pasemi_mac_intf_disable(mac); 235ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_CFG_ADR0, adr0); 236ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_CFG_ADR1, adr1); 237ded19addSJeff Kirsher pasemi_mac_intf_enable(mac); 238ded19addSJeff Kirsher 239ded19addSJeff Kirsher return 0; 240ded19addSJeff Kirsher } 241ded19addSJeff Kirsher 242ded19addSJeff Kirsher static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac, 243ded19addSJeff Kirsher const int nfrags, 244ded19addSJeff Kirsher struct sk_buff *skb, 245ded19addSJeff Kirsher const dma_addr_t *dmas) 246ded19addSJeff Kirsher { 247ded19addSJeff Kirsher int f; 248ded19addSJeff Kirsher struct pci_dev *pdev = mac->dma_pdev; 249ded19addSJeff Kirsher 250ded19addSJeff Kirsher pci_unmap_single(pdev, dmas[0], skb_headlen(skb), PCI_DMA_TODEVICE); 251ded19addSJeff Kirsher 252ded19addSJeff Kirsher for (f = 0; f < nfrags; f++) { 2539e903e08SEric Dumazet const skb_frag_t *frag = &skb_shinfo(skb)->frags[f]; 254ded19addSJeff Kirsher 2559e903e08SEric Dumazet pci_unmap_page(pdev, dmas[f+1], skb_frag_size(frag), PCI_DMA_TODEVICE); 256ded19addSJeff Kirsher } 257ded19addSJeff Kirsher dev_kfree_skb_irq(skb); 258ded19addSJeff Kirsher 259ded19addSJeff Kirsher /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs, 260ded19addSJeff Kirsher * aligned up to a power of 2 261ded19addSJeff Kirsher */ 262ded19addSJeff Kirsher return (nfrags + 3) & ~1; 263ded19addSJeff Kirsher } 264ded19addSJeff Kirsher 265ded19addSJeff Kirsher static struct pasemi_mac_csring *pasemi_mac_setup_csring(struct pasemi_mac *mac) 266ded19addSJeff Kirsher { 267ded19addSJeff Kirsher struct pasemi_mac_csring *ring; 268ded19addSJeff Kirsher u32 val; 269ded19addSJeff Kirsher unsigned int cfg; 270ded19addSJeff Kirsher int chno; 271ded19addSJeff Kirsher 272ded19addSJeff Kirsher ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_csring), 273ded19addSJeff Kirsher offsetof(struct pasemi_mac_csring, chan)); 274ded19addSJeff Kirsher 275ded19addSJeff Kirsher if (!ring) { 276ded19addSJeff Kirsher dev_err(&mac->pdev->dev, "Can't allocate checksum channel\n"); 277ded19addSJeff Kirsher goto out_chan; 278ded19addSJeff Kirsher } 279ded19addSJeff Kirsher 280ded19addSJeff Kirsher chno = ring->chan.chno; 281ded19addSJeff Kirsher 282ded19addSJeff Kirsher ring->size = CS_RING_SIZE; 283ded19addSJeff Kirsher ring->next_to_fill = 0; 284ded19addSJeff Kirsher 285ded19addSJeff Kirsher /* Allocate descriptors */ 286ded19addSJeff Kirsher if (pasemi_dma_alloc_ring(&ring->chan, CS_RING_SIZE)) 287ded19addSJeff Kirsher goto out_ring_desc; 288ded19addSJeff Kirsher 289ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno), 290ded19addSJeff Kirsher PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma)); 291ded19addSJeff Kirsher val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32); 292ded19addSJeff Kirsher val |= PAS_DMA_TXCHAN_BASEU_SIZ(CS_RING_SIZE >> 3); 293ded19addSJeff Kirsher 294ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val); 295ded19addSJeff Kirsher 296ded19addSJeff Kirsher ring->events[0] = pasemi_dma_alloc_flag(); 297ded19addSJeff Kirsher ring->events[1] = pasemi_dma_alloc_flag(); 298ded19addSJeff Kirsher if (ring->events[0] < 0 || ring->events[1] < 0) 299ded19addSJeff Kirsher goto out_flags; 300ded19addSJeff Kirsher 301ded19addSJeff Kirsher pasemi_dma_clear_flag(ring->events[0]); 302ded19addSJeff Kirsher pasemi_dma_clear_flag(ring->events[1]); 303ded19addSJeff Kirsher 304ded19addSJeff Kirsher ring->fun = pasemi_dma_alloc_fun(); 305ded19addSJeff Kirsher if (ring->fun < 0) 306ded19addSJeff Kirsher goto out_fun; 307ded19addSJeff Kirsher 308ded19addSJeff Kirsher cfg = PAS_DMA_TXCHAN_CFG_TY_FUNC | PAS_DMA_TXCHAN_CFG_UP | 309ded19addSJeff Kirsher PAS_DMA_TXCHAN_CFG_TATTR(ring->fun) | 310ded19addSJeff Kirsher PAS_DMA_TXCHAN_CFG_LPSQ | PAS_DMA_TXCHAN_CFG_LPDQ; 311ded19addSJeff Kirsher 312ded19addSJeff Kirsher if (translation_enabled()) 313ded19addSJeff Kirsher cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR; 314ded19addSJeff Kirsher 315ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg); 316ded19addSJeff Kirsher 317ded19addSJeff Kirsher /* enable channel */ 318ded19addSJeff Kirsher pasemi_dma_start_chan(&ring->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ | 319ded19addSJeff Kirsher PAS_DMA_TXCHAN_TCMDSTA_DB | 320ded19addSJeff Kirsher PAS_DMA_TXCHAN_TCMDSTA_DE | 321ded19addSJeff Kirsher PAS_DMA_TXCHAN_TCMDSTA_DA); 322ded19addSJeff Kirsher 323ded19addSJeff Kirsher return ring; 324ded19addSJeff Kirsher 325ded19addSJeff Kirsher out_fun: 326ded19addSJeff Kirsher out_flags: 327ded19addSJeff Kirsher if (ring->events[0] >= 0) 328ded19addSJeff Kirsher pasemi_dma_free_flag(ring->events[0]); 329ded19addSJeff Kirsher if (ring->events[1] >= 0) 330ded19addSJeff Kirsher pasemi_dma_free_flag(ring->events[1]); 331ded19addSJeff Kirsher pasemi_dma_free_ring(&ring->chan); 332ded19addSJeff Kirsher out_ring_desc: 333ded19addSJeff Kirsher pasemi_dma_free_chan(&ring->chan); 334ded19addSJeff Kirsher out_chan: 335ded19addSJeff Kirsher 336ded19addSJeff Kirsher return NULL; 337ded19addSJeff Kirsher } 338ded19addSJeff Kirsher 339ded19addSJeff Kirsher static void pasemi_mac_setup_csrings(struct pasemi_mac *mac) 340ded19addSJeff Kirsher { 341ded19addSJeff Kirsher int i; 342ded19addSJeff Kirsher mac->cs[0] = pasemi_mac_setup_csring(mac); 343ded19addSJeff Kirsher if (mac->type == MAC_TYPE_XAUI) 344ded19addSJeff Kirsher mac->cs[1] = pasemi_mac_setup_csring(mac); 345ded19addSJeff Kirsher else 346ded19addSJeff Kirsher mac->cs[1] = 0; 347ded19addSJeff Kirsher 348ded19addSJeff Kirsher for (i = 0; i < MAX_CS; i++) 349ded19addSJeff Kirsher if (mac->cs[i]) 350ded19addSJeff Kirsher mac->num_cs++; 351ded19addSJeff Kirsher } 352ded19addSJeff Kirsher 353ded19addSJeff Kirsher static void pasemi_mac_free_csring(struct pasemi_mac_csring *csring) 354ded19addSJeff Kirsher { 355ded19addSJeff Kirsher pasemi_dma_stop_chan(&csring->chan); 356ded19addSJeff Kirsher pasemi_dma_free_flag(csring->events[0]); 357ded19addSJeff Kirsher pasemi_dma_free_flag(csring->events[1]); 358ded19addSJeff Kirsher pasemi_dma_free_ring(&csring->chan); 359ded19addSJeff Kirsher pasemi_dma_free_chan(&csring->chan); 360ded19addSJeff Kirsher pasemi_dma_free_fun(csring->fun); 361ded19addSJeff Kirsher } 362ded19addSJeff Kirsher 363ded19addSJeff Kirsher static int pasemi_mac_setup_rx_resources(const struct net_device *dev) 364ded19addSJeff Kirsher { 365ded19addSJeff Kirsher struct pasemi_mac_rxring *ring; 366ded19addSJeff Kirsher struct pasemi_mac *mac = netdev_priv(dev); 367ded19addSJeff Kirsher int chno; 368ded19addSJeff Kirsher unsigned int cfg; 369ded19addSJeff Kirsher 370ded19addSJeff Kirsher ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring), 371ded19addSJeff Kirsher offsetof(struct pasemi_mac_rxring, chan)); 372ded19addSJeff Kirsher 373ded19addSJeff Kirsher if (!ring) { 374ded19addSJeff Kirsher dev_err(&mac->pdev->dev, "Can't allocate RX channel\n"); 375ded19addSJeff Kirsher goto out_chan; 376ded19addSJeff Kirsher } 377ded19addSJeff Kirsher chno = ring->chan.chno; 378ded19addSJeff Kirsher 379ded19addSJeff Kirsher spin_lock_init(&ring->lock); 380ded19addSJeff Kirsher 381ded19addSJeff Kirsher ring->size = RX_RING_SIZE; 3826396bb22SKees Cook ring->ring_info = kcalloc(RX_RING_SIZE, 3836396bb22SKees Cook sizeof(struct pasemi_mac_buffer), 3846396bb22SKees Cook GFP_KERNEL); 385ded19addSJeff Kirsher 386ded19addSJeff Kirsher if (!ring->ring_info) 387ded19addSJeff Kirsher goto out_ring_info; 388ded19addSJeff Kirsher 389ded19addSJeff Kirsher /* Allocate descriptors */ 390ded19addSJeff Kirsher if (pasemi_dma_alloc_ring(&ring->chan, RX_RING_SIZE)) 391ded19addSJeff Kirsher goto out_ring_desc; 392ded19addSJeff Kirsher 393750afb08SLuis Chamberlain ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev, 394ded19addSJeff Kirsher RX_RING_SIZE * sizeof(u64), 395ede23fa8SJoe Perches &ring->buf_dma, GFP_KERNEL); 396ded19addSJeff Kirsher if (!ring->buffers) 397ded19addSJeff Kirsher goto out_ring_desc; 398ded19addSJeff Kirsher 399ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno), 400ded19addSJeff Kirsher PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma)); 401ded19addSJeff Kirsher 402ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno), 403ded19addSJeff Kirsher PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) | 404ded19addSJeff Kirsher PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3)); 405ded19addSJeff Kirsher 406ded19addSJeff Kirsher cfg = PAS_DMA_RXCHAN_CFG_HBU(2); 407ded19addSJeff Kirsher 408ded19addSJeff Kirsher if (translation_enabled()) 409ded19addSJeff Kirsher cfg |= PAS_DMA_RXCHAN_CFG_CTR; 410ded19addSJeff Kirsher 411ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg); 412ded19addSJeff Kirsher 413ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if), 414ded19addSJeff Kirsher PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma)); 415ded19addSJeff Kirsher 416ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if), 417ded19addSJeff Kirsher PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) | 418ded19addSJeff Kirsher PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3)); 419ded19addSJeff Kirsher 420ded19addSJeff Kirsher cfg = PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2 | 421ded19addSJeff Kirsher PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP | 422ded19addSJeff Kirsher PAS_DMA_RXINT_CFG_HEN; 423ded19addSJeff Kirsher 424ded19addSJeff Kirsher if (translation_enabled()) 425ded19addSJeff Kirsher cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR; 426ded19addSJeff Kirsher 427ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg); 428ded19addSJeff Kirsher 429ded19addSJeff Kirsher ring->next_to_fill = 0; 430ded19addSJeff Kirsher ring->next_to_clean = 0; 431ded19addSJeff Kirsher ring->mac = mac; 432ded19addSJeff Kirsher mac->rx = ring; 433ded19addSJeff Kirsher 434ded19addSJeff Kirsher return 0; 435ded19addSJeff Kirsher 436ded19addSJeff Kirsher out_ring_desc: 437ded19addSJeff Kirsher kfree(ring->ring_info); 438ded19addSJeff Kirsher out_ring_info: 439ded19addSJeff Kirsher pasemi_dma_free_chan(&ring->chan); 440ded19addSJeff Kirsher out_chan: 441ded19addSJeff Kirsher return -ENOMEM; 442ded19addSJeff Kirsher } 443ded19addSJeff Kirsher 444ded19addSJeff Kirsher static struct pasemi_mac_txring * 445ded19addSJeff Kirsher pasemi_mac_setup_tx_resources(const struct net_device *dev) 446ded19addSJeff Kirsher { 447ded19addSJeff Kirsher struct pasemi_mac *mac = netdev_priv(dev); 448ded19addSJeff Kirsher u32 val; 449ded19addSJeff Kirsher struct pasemi_mac_txring *ring; 450ded19addSJeff Kirsher unsigned int cfg; 451ded19addSJeff Kirsher int chno; 452ded19addSJeff Kirsher 453ded19addSJeff Kirsher ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring), 454ded19addSJeff Kirsher offsetof(struct pasemi_mac_txring, chan)); 455ded19addSJeff Kirsher 456ded19addSJeff Kirsher if (!ring) { 457ded19addSJeff Kirsher dev_err(&mac->pdev->dev, "Can't allocate TX channel\n"); 458ded19addSJeff Kirsher goto out_chan; 459ded19addSJeff Kirsher } 460ded19addSJeff Kirsher 461ded19addSJeff Kirsher chno = ring->chan.chno; 462ded19addSJeff Kirsher 463ded19addSJeff Kirsher spin_lock_init(&ring->lock); 464ded19addSJeff Kirsher 465ded19addSJeff Kirsher ring->size = TX_RING_SIZE; 4666396bb22SKees Cook ring->ring_info = kcalloc(TX_RING_SIZE, 4676396bb22SKees Cook sizeof(struct pasemi_mac_buffer), 4686396bb22SKees Cook GFP_KERNEL); 469ded19addSJeff Kirsher if (!ring->ring_info) 470ded19addSJeff Kirsher goto out_ring_info; 471ded19addSJeff Kirsher 472ded19addSJeff Kirsher /* Allocate descriptors */ 473ded19addSJeff Kirsher if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE)) 474ded19addSJeff Kirsher goto out_ring_desc; 475ded19addSJeff Kirsher 476ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno), 477ded19addSJeff Kirsher PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma)); 478ded19addSJeff Kirsher val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32); 479ded19addSJeff Kirsher val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3); 480ded19addSJeff Kirsher 481ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val); 482ded19addSJeff Kirsher 483ded19addSJeff Kirsher cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE | 484ded19addSJeff Kirsher PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) | 485ded19addSJeff Kirsher PAS_DMA_TXCHAN_CFG_UP | 486ded19addSJeff Kirsher PAS_DMA_TXCHAN_CFG_WT(4); 487ded19addSJeff Kirsher 488ded19addSJeff Kirsher if (translation_enabled()) 489ded19addSJeff Kirsher cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR; 490ded19addSJeff Kirsher 491ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg); 492ded19addSJeff Kirsher 493ded19addSJeff Kirsher ring->next_to_fill = 0; 494ded19addSJeff Kirsher ring->next_to_clean = 0; 495ded19addSJeff Kirsher ring->mac = mac; 496ded19addSJeff Kirsher 497ded19addSJeff Kirsher return ring; 498ded19addSJeff Kirsher 499ded19addSJeff Kirsher out_ring_desc: 500ded19addSJeff Kirsher kfree(ring->ring_info); 501ded19addSJeff Kirsher out_ring_info: 502ded19addSJeff Kirsher pasemi_dma_free_chan(&ring->chan); 503ded19addSJeff Kirsher out_chan: 504ded19addSJeff Kirsher return NULL; 505ded19addSJeff Kirsher } 506ded19addSJeff Kirsher 507ded19addSJeff Kirsher static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac) 508ded19addSJeff Kirsher { 509ded19addSJeff Kirsher struct pasemi_mac_txring *txring = tx_ring(mac); 510ded19addSJeff Kirsher unsigned int i, j; 511ded19addSJeff Kirsher struct pasemi_mac_buffer *info; 512ded19addSJeff Kirsher dma_addr_t dmas[MAX_SKB_FRAGS+1]; 513ded19addSJeff Kirsher int freed, nfrags; 514ded19addSJeff Kirsher int start, limit; 515ded19addSJeff Kirsher 516ded19addSJeff Kirsher start = txring->next_to_clean; 517ded19addSJeff Kirsher limit = txring->next_to_fill; 518ded19addSJeff Kirsher 519ded19addSJeff Kirsher /* Compensate for when fill has wrapped and clean has not */ 520ded19addSJeff Kirsher if (start > limit) 521ded19addSJeff Kirsher limit += TX_RING_SIZE; 522ded19addSJeff Kirsher 523ded19addSJeff Kirsher for (i = start; i < limit; i += freed) { 524ded19addSJeff Kirsher info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)]; 525ded19addSJeff Kirsher if (info->dma && info->skb) { 526ded19addSJeff Kirsher nfrags = skb_shinfo(info->skb)->nr_frags; 527ded19addSJeff Kirsher for (j = 0; j <= nfrags; j++) 528ded19addSJeff Kirsher dmas[j] = txring->ring_info[(i+1+j) & 529ded19addSJeff Kirsher (TX_RING_SIZE-1)].dma; 530ded19addSJeff Kirsher freed = pasemi_mac_unmap_tx_skb(mac, nfrags, 531ded19addSJeff Kirsher info->skb, dmas); 532ea5cdcccSSyam Sidhardhan } else { 533ded19addSJeff Kirsher freed = 2; 534ded19addSJeff Kirsher } 535ea5cdcccSSyam Sidhardhan } 536ded19addSJeff Kirsher 537ded19addSJeff Kirsher kfree(txring->ring_info); 538ded19addSJeff Kirsher pasemi_dma_free_chan(&txring->chan); 539ded19addSJeff Kirsher 540ded19addSJeff Kirsher } 541ded19addSJeff Kirsher 542ded19addSJeff Kirsher static void pasemi_mac_free_rx_buffers(struct pasemi_mac *mac) 543ded19addSJeff Kirsher { 544ded19addSJeff Kirsher struct pasemi_mac_rxring *rx = rx_ring(mac); 545ded19addSJeff Kirsher unsigned int i; 546ded19addSJeff Kirsher struct pasemi_mac_buffer *info; 547ded19addSJeff Kirsher 548ded19addSJeff Kirsher for (i = 0; i < RX_RING_SIZE; i++) { 549ded19addSJeff Kirsher info = &RX_DESC_INFO(rx, i); 550ded19addSJeff Kirsher if (info->skb && info->dma) { 551ded19addSJeff Kirsher pci_unmap_single(mac->dma_pdev, 552ded19addSJeff Kirsher info->dma, 553ded19addSJeff Kirsher info->skb->len, 554ded19addSJeff Kirsher PCI_DMA_FROMDEVICE); 555ded19addSJeff Kirsher dev_kfree_skb_any(info->skb); 556ded19addSJeff Kirsher } 557ded19addSJeff Kirsher info->dma = 0; 558ded19addSJeff Kirsher info->skb = NULL; 559ded19addSJeff Kirsher } 560ded19addSJeff Kirsher 561ded19addSJeff Kirsher for (i = 0; i < RX_RING_SIZE; i++) 562ded19addSJeff Kirsher RX_BUFF(rx, i) = 0; 563ded19addSJeff Kirsher } 564ded19addSJeff Kirsher 565ded19addSJeff Kirsher static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac) 566ded19addSJeff Kirsher { 567ded19addSJeff Kirsher pasemi_mac_free_rx_buffers(mac); 568ded19addSJeff Kirsher 569ded19addSJeff Kirsher dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64), 570ded19addSJeff Kirsher rx_ring(mac)->buffers, rx_ring(mac)->buf_dma); 571ded19addSJeff Kirsher 572ded19addSJeff Kirsher kfree(rx_ring(mac)->ring_info); 573ded19addSJeff Kirsher pasemi_dma_free_chan(&rx_ring(mac)->chan); 574ded19addSJeff Kirsher mac->rx = NULL; 575ded19addSJeff Kirsher } 576ded19addSJeff Kirsher 5775c6239c8SStephen Rothwell static void pasemi_mac_replenish_rx_ring(struct net_device *dev, 578ded19addSJeff Kirsher const int limit) 579ded19addSJeff Kirsher { 580ded19addSJeff Kirsher const struct pasemi_mac *mac = netdev_priv(dev); 581ded19addSJeff Kirsher struct pasemi_mac_rxring *rx = rx_ring(mac); 582ded19addSJeff Kirsher int fill, count; 583ded19addSJeff Kirsher 584ded19addSJeff Kirsher if (limit <= 0) 585ded19addSJeff Kirsher return; 586ded19addSJeff Kirsher 587ded19addSJeff Kirsher fill = rx_ring(mac)->next_to_fill; 588ded19addSJeff Kirsher for (count = 0; count < limit; count++) { 589ded19addSJeff Kirsher struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill); 590ded19addSJeff Kirsher u64 *buff = &RX_BUFF(rx, fill); 591ded19addSJeff Kirsher struct sk_buff *skb; 592ded19addSJeff Kirsher dma_addr_t dma; 593ded19addSJeff Kirsher 594ded19addSJeff Kirsher /* Entry in use? */ 595ded19addSJeff Kirsher WARN_ON(*buff); 596ded19addSJeff Kirsher 597dae2e9f4SPradeep A. Dalvi skb = netdev_alloc_skb(dev, mac->bufsz); 598ded19addSJeff Kirsher skb_reserve(skb, LOCAL_SKB_ALIGN); 599ded19addSJeff Kirsher 600ded19addSJeff Kirsher if (unlikely(!skb)) 601ded19addSJeff Kirsher break; 602ded19addSJeff Kirsher 603ded19addSJeff Kirsher dma = pci_map_single(mac->dma_pdev, skb->data, 604ded19addSJeff Kirsher mac->bufsz - LOCAL_SKB_ALIGN, 605ded19addSJeff Kirsher PCI_DMA_FROMDEVICE); 606ded19addSJeff Kirsher 607ded19addSJeff Kirsher if (unlikely(pci_dma_mapping_error(mac->dma_pdev, dma))) { 608ded19addSJeff Kirsher dev_kfree_skb_irq(info->skb); 609ded19addSJeff Kirsher break; 610ded19addSJeff Kirsher } 611ded19addSJeff Kirsher 612ded19addSJeff Kirsher info->skb = skb; 613ded19addSJeff Kirsher info->dma = dma; 614ded19addSJeff Kirsher *buff = XCT_RXB_LEN(mac->bufsz) | XCT_RXB_ADDR(dma); 615ded19addSJeff Kirsher fill++; 616ded19addSJeff Kirsher } 617ded19addSJeff Kirsher 618ded19addSJeff Kirsher wmb(); 619ded19addSJeff Kirsher 620ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count); 621ded19addSJeff Kirsher 622ded19addSJeff Kirsher rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) & 623ded19addSJeff Kirsher (RX_RING_SIZE - 1); 624ded19addSJeff Kirsher } 625ded19addSJeff Kirsher 626ded19addSJeff Kirsher static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac) 627ded19addSJeff Kirsher { 628ded19addSJeff Kirsher struct pasemi_mac_rxring *rx = rx_ring(mac); 629ded19addSJeff Kirsher unsigned int reg, pcnt; 630ded19addSJeff Kirsher /* Re-enable packet count interrupts: finally 631ded19addSJeff Kirsher * ack the packet count interrupt we got in rx_intr. 632ded19addSJeff Kirsher */ 633ded19addSJeff Kirsher 634ded19addSJeff Kirsher pcnt = *rx->chan.status & PAS_STATUS_PCNT_M; 635ded19addSJeff Kirsher 636ded19addSJeff Kirsher reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC; 637ded19addSJeff Kirsher 638ded19addSJeff Kirsher if (*rx->chan.status & PAS_STATUS_TIMER) 639ded19addSJeff Kirsher reg |= PAS_IOB_DMA_RXCH_RESET_TINTC; 640ded19addSJeff Kirsher 641ded19addSJeff Kirsher write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg); 642ded19addSJeff Kirsher } 643ded19addSJeff Kirsher 644ded19addSJeff Kirsher static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac) 645ded19addSJeff Kirsher { 646ded19addSJeff Kirsher unsigned int reg, pcnt; 647ded19addSJeff Kirsher 648ded19addSJeff Kirsher /* Re-enable packet count interrupts */ 649ded19addSJeff Kirsher pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M; 650ded19addSJeff Kirsher 651ded19addSJeff Kirsher reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC; 652ded19addSJeff Kirsher 653ded19addSJeff Kirsher write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg); 654ded19addSJeff Kirsher } 655ded19addSJeff Kirsher 656ded19addSJeff Kirsher 657ded19addSJeff Kirsher static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac, 658ded19addSJeff Kirsher const u64 macrx) 659ded19addSJeff Kirsher { 660ded19addSJeff Kirsher unsigned int rcmdsta, ccmdsta; 661ded19addSJeff Kirsher struct pasemi_dmachan *chan = &rx_ring(mac)->chan; 662ded19addSJeff Kirsher 663ded19addSJeff Kirsher if (!netif_msg_rx_err(mac)) 664ded19addSJeff Kirsher return; 665ded19addSJeff Kirsher 666ded19addSJeff Kirsher rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if)); 667ded19addSJeff Kirsher ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno)); 668ded19addSJeff Kirsher 669ded19addSJeff Kirsher printk(KERN_ERR "pasemi_mac: rx error. macrx %016llx, rx status %llx\n", 670ded19addSJeff Kirsher macrx, *chan->status); 671ded19addSJeff Kirsher 672ded19addSJeff Kirsher printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n", 673ded19addSJeff Kirsher rcmdsta, ccmdsta); 674ded19addSJeff Kirsher } 675ded19addSJeff Kirsher 676ded19addSJeff Kirsher static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac, 677ded19addSJeff Kirsher const u64 mactx) 678ded19addSJeff Kirsher { 679ded19addSJeff Kirsher unsigned int cmdsta; 680ded19addSJeff Kirsher struct pasemi_dmachan *chan = &tx_ring(mac)->chan; 681ded19addSJeff Kirsher 682ded19addSJeff Kirsher if (!netif_msg_tx_err(mac)) 683ded19addSJeff Kirsher return; 684ded19addSJeff Kirsher 685ded19addSJeff Kirsher cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno)); 686ded19addSJeff Kirsher 687ded19addSJeff Kirsher printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016llx, "\ 688ded19addSJeff Kirsher "tx status 0x%016llx\n", mactx, *chan->status); 689ded19addSJeff Kirsher 690ded19addSJeff Kirsher printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta); 691ded19addSJeff Kirsher } 692ded19addSJeff Kirsher 693ded19addSJeff Kirsher static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx, 694ded19addSJeff Kirsher const int limit) 695ded19addSJeff Kirsher { 696ded19addSJeff Kirsher const struct pasemi_dmachan *chan = &rx->chan; 697ded19addSJeff Kirsher struct pasemi_mac *mac = rx->mac; 698ded19addSJeff Kirsher struct pci_dev *pdev = mac->dma_pdev; 699ded19addSJeff Kirsher unsigned int n; 700ded19addSJeff Kirsher int count, buf_index, tot_bytes, packets; 701ded19addSJeff Kirsher struct pasemi_mac_buffer *info; 702ded19addSJeff Kirsher struct sk_buff *skb; 703ded19addSJeff Kirsher unsigned int len; 704ded19addSJeff Kirsher u64 macrx, eval; 705ded19addSJeff Kirsher dma_addr_t dma; 706ded19addSJeff Kirsher 707ded19addSJeff Kirsher tot_bytes = 0; 708ded19addSJeff Kirsher packets = 0; 709ded19addSJeff Kirsher 710ded19addSJeff Kirsher spin_lock(&rx->lock); 711ded19addSJeff Kirsher 712ded19addSJeff Kirsher n = rx->next_to_clean; 713ded19addSJeff Kirsher 714ded19addSJeff Kirsher prefetch(&RX_DESC(rx, n)); 715ded19addSJeff Kirsher 716ded19addSJeff Kirsher for (count = 0; count < limit; count++) { 717ded19addSJeff Kirsher macrx = RX_DESC(rx, n); 718ded19addSJeff Kirsher prefetch(&RX_DESC(rx, n+4)); 719ded19addSJeff Kirsher 720ded19addSJeff Kirsher if ((macrx & XCT_MACRX_E) || 721ded19addSJeff Kirsher (*chan->status & PAS_STATUS_ERROR)) 722ded19addSJeff Kirsher pasemi_mac_rx_error(mac, macrx); 723ded19addSJeff Kirsher 724ded19addSJeff Kirsher if (!(macrx & XCT_MACRX_O)) 725ded19addSJeff Kirsher break; 726ded19addSJeff Kirsher 727ded19addSJeff Kirsher info = NULL; 728ded19addSJeff Kirsher 729ded19addSJeff Kirsher BUG_ON(!(macrx & XCT_MACRX_RR_8BRES)); 730ded19addSJeff Kirsher 731ded19addSJeff Kirsher eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >> 732ded19addSJeff Kirsher XCT_RXRES_8B_EVAL_S; 733ded19addSJeff Kirsher buf_index = eval-1; 734ded19addSJeff Kirsher 735ded19addSJeff Kirsher dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M); 736ded19addSJeff Kirsher info = &RX_DESC_INFO(rx, buf_index); 737ded19addSJeff Kirsher 738ded19addSJeff Kirsher skb = info->skb; 739ded19addSJeff Kirsher 740ded19addSJeff Kirsher prefetch_skb(skb); 741ded19addSJeff Kirsher 742ded19addSJeff Kirsher len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S; 743ded19addSJeff Kirsher 744ded19addSJeff Kirsher pci_unmap_single(pdev, dma, mac->bufsz - LOCAL_SKB_ALIGN, 745ded19addSJeff Kirsher PCI_DMA_FROMDEVICE); 746ded19addSJeff Kirsher 747ded19addSJeff Kirsher if (macrx & XCT_MACRX_CRC) { 748ded19addSJeff Kirsher /* CRC error flagged */ 749ded19addSJeff Kirsher mac->netdev->stats.rx_errors++; 750ded19addSJeff Kirsher mac->netdev->stats.rx_crc_errors++; 751ded19addSJeff Kirsher /* No need to free skb, it'll be reused */ 752ded19addSJeff Kirsher goto next; 753ded19addSJeff Kirsher } 754ded19addSJeff Kirsher 755ded19addSJeff Kirsher info->skb = NULL; 756ded19addSJeff Kirsher info->dma = 0; 757ded19addSJeff Kirsher 758ded19addSJeff Kirsher if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) { 759ded19addSJeff Kirsher skb->ip_summed = CHECKSUM_UNNECESSARY; 760ded19addSJeff Kirsher skb->csum = (macrx & XCT_MACRX_CSUM_M) >> 761ded19addSJeff Kirsher XCT_MACRX_CSUM_S; 762ea5cdcccSSyam Sidhardhan } else { 763ded19addSJeff Kirsher skb_checksum_none_assert(skb); 764ea5cdcccSSyam Sidhardhan } 765ded19addSJeff Kirsher 766ded19addSJeff Kirsher packets++; 767ded19addSJeff Kirsher tot_bytes += len; 768ded19addSJeff Kirsher 769ded19addSJeff Kirsher /* Don't include CRC */ 770ded19addSJeff Kirsher skb_put(skb, len-4); 771ded19addSJeff Kirsher 772ded19addSJeff Kirsher skb->protocol = eth_type_trans(skb, mac->netdev); 773a4f9cdb2SBen Hutchings napi_gro_receive(&mac->napi, skb); 774ded19addSJeff Kirsher 775ded19addSJeff Kirsher next: 776ded19addSJeff Kirsher RX_DESC(rx, n) = 0; 777ded19addSJeff Kirsher RX_DESC(rx, n+1) = 0; 778ded19addSJeff Kirsher 779ded19addSJeff Kirsher /* Need to zero it out since hardware doesn't, since the 780ded19addSJeff Kirsher * replenish loop uses it to tell when it's done. 781ded19addSJeff Kirsher */ 782ded19addSJeff Kirsher RX_BUFF(rx, buf_index) = 0; 783ded19addSJeff Kirsher 784ded19addSJeff Kirsher n += 4; 785ded19addSJeff Kirsher } 786ded19addSJeff Kirsher 787ded19addSJeff Kirsher if (n > RX_RING_SIZE) { 788ded19addSJeff Kirsher /* Errata 5971 workaround: L2 target of headers */ 789ded19addSJeff Kirsher write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0); 790ded19addSJeff Kirsher n &= (RX_RING_SIZE-1); 791ded19addSJeff Kirsher } 792ded19addSJeff Kirsher 793ded19addSJeff Kirsher rx_ring(mac)->next_to_clean = n; 794ded19addSJeff Kirsher 795ded19addSJeff Kirsher /* Increase is in number of 16-byte entries, and since each descriptor 796ded19addSJeff Kirsher * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with 797ded19addSJeff Kirsher * count*2. 798ded19addSJeff Kirsher */ 799ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1); 800ded19addSJeff Kirsher 801ded19addSJeff Kirsher pasemi_mac_replenish_rx_ring(mac->netdev, count); 802ded19addSJeff Kirsher 803ded19addSJeff Kirsher mac->netdev->stats.rx_bytes += tot_bytes; 804ded19addSJeff Kirsher mac->netdev->stats.rx_packets += packets; 805ded19addSJeff Kirsher 806ded19addSJeff Kirsher spin_unlock(&rx_ring(mac)->lock); 807ded19addSJeff Kirsher 808ded19addSJeff Kirsher return count; 809ded19addSJeff Kirsher } 810ded19addSJeff Kirsher 811ded19addSJeff Kirsher /* Can't make this too large or we blow the kernel stack limits */ 812ded19addSJeff Kirsher #define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS) 813ded19addSJeff Kirsher 814ded19addSJeff Kirsher static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring) 815ded19addSJeff Kirsher { 816ded19addSJeff Kirsher struct pasemi_dmachan *chan = &txring->chan; 817ded19addSJeff Kirsher struct pasemi_mac *mac = txring->mac; 818ded19addSJeff Kirsher int i, j; 819ded19addSJeff Kirsher unsigned int start, descr_count, buf_count, batch_limit; 820ded19addSJeff Kirsher unsigned int ring_limit; 821ded19addSJeff Kirsher unsigned int total_count; 822ded19addSJeff Kirsher unsigned long flags; 823ded19addSJeff Kirsher struct sk_buff *skbs[TX_CLEAN_BATCHSIZE]; 824ded19addSJeff Kirsher dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1]; 825ded19addSJeff Kirsher int nf[TX_CLEAN_BATCHSIZE]; 826ded19addSJeff Kirsher int nr_frags; 827ded19addSJeff Kirsher 828ded19addSJeff Kirsher total_count = 0; 829ded19addSJeff Kirsher batch_limit = TX_CLEAN_BATCHSIZE; 830ded19addSJeff Kirsher restart: 831ded19addSJeff Kirsher spin_lock_irqsave(&txring->lock, flags); 832ded19addSJeff Kirsher 833ded19addSJeff Kirsher start = txring->next_to_clean; 834ded19addSJeff Kirsher ring_limit = txring->next_to_fill; 835ded19addSJeff Kirsher 836ded19addSJeff Kirsher prefetch(&TX_DESC_INFO(txring, start+1).skb); 837ded19addSJeff Kirsher 838ded19addSJeff Kirsher /* Compensate for when fill has wrapped but clean has not */ 839ded19addSJeff Kirsher if (start > ring_limit) 840ded19addSJeff Kirsher ring_limit += TX_RING_SIZE; 841ded19addSJeff Kirsher 842ded19addSJeff Kirsher buf_count = 0; 843ded19addSJeff Kirsher descr_count = 0; 844ded19addSJeff Kirsher 845ded19addSJeff Kirsher for (i = start; 846ded19addSJeff Kirsher descr_count < batch_limit && i < ring_limit; 847ded19addSJeff Kirsher i += buf_count) { 848ded19addSJeff Kirsher u64 mactx = TX_DESC(txring, i); 849ded19addSJeff Kirsher struct sk_buff *skb; 850ded19addSJeff Kirsher 851ded19addSJeff Kirsher if ((mactx & XCT_MACTX_E) || 852ded19addSJeff Kirsher (*chan->status & PAS_STATUS_ERROR)) 853ded19addSJeff Kirsher pasemi_mac_tx_error(mac, mactx); 854ded19addSJeff Kirsher 855ded19addSJeff Kirsher /* Skip over control descriptors */ 856ded19addSJeff Kirsher if (!(mactx & XCT_MACTX_LLEN_M)) { 857ded19addSJeff Kirsher TX_DESC(txring, i) = 0; 858ded19addSJeff Kirsher TX_DESC(txring, i+1) = 0; 859ded19addSJeff Kirsher buf_count = 2; 860ded19addSJeff Kirsher continue; 861ded19addSJeff Kirsher } 862ded19addSJeff Kirsher 863ded19addSJeff Kirsher skb = TX_DESC_INFO(txring, i+1).skb; 864ded19addSJeff Kirsher nr_frags = TX_DESC_INFO(txring, i).dma; 865ded19addSJeff Kirsher 866ded19addSJeff Kirsher if (unlikely(mactx & XCT_MACTX_O)) 867ded19addSJeff Kirsher /* Not yet transmitted */ 868ded19addSJeff Kirsher break; 869ded19addSJeff Kirsher 870ded19addSJeff Kirsher buf_count = 2 + nr_frags; 871ded19addSJeff Kirsher /* Since we always fill with an even number of entries, make 872ded19addSJeff Kirsher * sure we skip any unused one at the end as well. 873ded19addSJeff Kirsher */ 874ded19addSJeff Kirsher if (buf_count & 1) 875ded19addSJeff Kirsher buf_count++; 876ded19addSJeff Kirsher 877ded19addSJeff Kirsher for (j = 0; j <= nr_frags; j++) 878ded19addSJeff Kirsher dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma; 879ded19addSJeff Kirsher 880ded19addSJeff Kirsher skbs[descr_count] = skb; 881ded19addSJeff Kirsher nf[descr_count] = nr_frags; 882ded19addSJeff Kirsher 883ded19addSJeff Kirsher TX_DESC(txring, i) = 0; 884ded19addSJeff Kirsher TX_DESC(txring, i+1) = 0; 885ded19addSJeff Kirsher 886ded19addSJeff Kirsher descr_count++; 887ded19addSJeff Kirsher } 888ded19addSJeff Kirsher txring->next_to_clean = i & (TX_RING_SIZE-1); 889ded19addSJeff Kirsher 890ded19addSJeff Kirsher spin_unlock_irqrestore(&txring->lock, flags); 891ded19addSJeff Kirsher netif_wake_queue(mac->netdev); 892ded19addSJeff Kirsher 893ded19addSJeff Kirsher for (i = 0; i < descr_count; i++) 894ded19addSJeff Kirsher pasemi_mac_unmap_tx_skb(mac, nf[i], skbs[i], dmas[i]); 895ded19addSJeff Kirsher 896ded19addSJeff Kirsher total_count += descr_count; 897ded19addSJeff Kirsher 898ded19addSJeff Kirsher /* If the batch was full, try to clean more */ 899ded19addSJeff Kirsher if (descr_count == batch_limit) 900ded19addSJeff Kirsher goto restart; 901ded19addSJeff Kirsher 902ded19addSJeff Kirsher return total_count; 903ded19addSJeff Kirsher } 904ded19addSJeff Kirsher 905ded19addSJeff Kirsher 906ded19addSJeff Kirsher static irqreturn_t pasemi_mac_rx_intr(int irq, void *data) 907ded19addSJeff Kirsher { 908ded19addSJeff Kirsher const struct pasemi_mac_rxring *rxring = data; 909ded19addSJeff Kirsher struct pasemi_mac *mac = rxring->mac; 910ded19addSJeff Kirsher const struct pasemi_dmachan *chan = &rxring->chan; 911ded19addSJeff Kirsher unsigned int reg; 912ded19addSJeff Kirsher 913ded19addSJeff Kirsher if (!(*chan->status & PAS_STATUS_CAUSE_M)) 914ded19addSJeff Kirsher return IRQ_NONE; 915ded19addSJeff Kirsher 916ded19addSJeff Kirsher /* Don't reset packet count so it won't fire again but clear 917ded19addSJeff Kirsher * all others. 918ded19addSJeff Kirsher */ 919ded19addSJeff Kirsher 920ded19addSJeff Kirsher reg = 0; 921ded19addSJeff Kirsher if (*chan->status & PAS_STATUS_SOFT) 922ded19addSJeff Kirsher reg |= PAS_IOB_DMA_RXCH_RESET_SINTC; 923ded19addSJeff Kirsher if (*chan->status & PAS_STATUS_ERROR) 924ded19addSJeff Kirsher reg |= PAS_IOB_DMA_RXCH_RESET_DINTC; 925ded19addSJeff Kirsher 926ded19addSJeff Kirsher napi_schedule(&mac->napi); 927ded19addSJeff Kirsher 928ded19addSJeff Kirsher write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg); 929ded19addSJeff Kirsher 930ded19addSJeff Kirsher return IRQ_HANDLED; 931ded19addSJeff Kirsher } 932ded19addSJeff Kirsher 933ded19addSJeff Kirsher #define TX_CLEAN_INTERVAL HZ 934ded19addSJeff Kirsher 935e99e88a9SKees Cook static void pasemi_mac_tx_timer(struct timer_list *t) 936ded19addSJeff Kirsher { 937e99e88a9SKees Cook struct pasemi_mac_txring *txring = from_timer(txring, t, clean_timer); 938ded19addSJeff Kirsher struct pasemi_mac *mac = txring->mac; 939ded19addSJeff Kirsher 940ded19addSJeff Kirsher pasemi_mac_clean_tx(txring); 941ded19addSJeff Kirsher 942ded19addSJeff Kirsher mod_timer(&txring->clean_timer, jiffies + TX_CLEAN_INTERVAL); 943ded19addSJeff Kirsher 944ded19addSJeff Kirsher pasemi_mac_restart_tx_intr(mac); 945ded19addSJeff Kirsher } 946ded19addSJeff Kirsher 947ded19addSJeff Kirsher static irqreturn_t pasemi_mac_tx_intr(int irq, void *data) 948ded19addSJeff Kirsher { 949ded19addSJeff Kirsher struct pasemi_mac_txring *txring = data; 950ded19addSJeff Kirsher const struct pasemi_dmachan *chan = &txring->chan; 951ded19addSJeff Kirsher struct pasemi_mac *mac = txring->mac; 952ded19addSJeff Kirsher unsigned int reg; 953ded19addSJeff Kirsher 954ded19addSJeff Kirsher if (!(*chan->status & PAS_STATUS_CAUSE_M)) 955ded19addSJeff Kirsher return IRQ_NONE; 956ded19addSJeff Kirsher 957ded19addSJeff Kirsher reg = 0; 958ded19addSJeff Kirsher 959ded19addSJeff Kirsher if (*chan->status & PAS_STATUS_SOFT) 960ded19addSJeff Kirsher reg |= PAS_IOB_DMA_TXCH_RESET_SINTC; 961ded19addSJeff Kirsher if (*chan->status & PAS_STATUS_ERROR) 962ded19addSJeff Kirsher reg |= PAS_IOB_DMA_TXCH_RESET_DINTC; 963ded19addSJeff Kirsher 964ded19addSJeff Kirsher mod_timer(&txring->clean_timer, jiffies + (TX_CLEAN_INTERVAL)*2); 965ded19addSJeff Kirsher 966ded19addSJeff Kirsher napi_schedule(&mac->napi); 967ded19addSJeff Kirsher 968ded19addSJeff Kirsher if (reg) 969ded19addSJeff Kirsher write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg); 970ded19addSJeff Kirsher 971ded19addSJeff Kirsher return IRQ_HANDLED; 972ded19addSJeff Kirsher } 973ded19addSJeff Kirsher 974ded19addSJeff Kirsher static void pasemi_adjust_link(struct net_device *dev) 975ded19addSJeff Kirsher { 976ded19addSJeff Kirsher struct pasemi_mac *mac = netdev_priv(dev); 977ded19addSJeff Kirsher int msg; 978ded19addSJeff Kirsher unsigned int flags; 979ded19addSJeff Kirsher unsigned int new_flags; 980ded19addSJeff Kirsher 98180721e7fSPhilippe Reynes if (!dev->phydev->link) { 982ded19addSJeff Kirsher /* If no link, MAC speed settings don't matter. Just report 983ded19addSJeff Kirsher * link down and return. 984ded19addSJeff Kirsher */ 985ded19addSJeff Kirsher if (mac->link && netif_msg_link(mac)) 986ded19addSJeff Kirsher printk(KERN_INFO "%s: Link is down.\n", dev->name); 987ded19addSJeff Kirsher 988ded19addSJeff Kirsher netif_carrier_off(dev); 989ded19addSJeff Kirsher pasemi_mac_intf_disable(mac); 990ded19addSJeff Kirsher mac->link = 0; 991ded19addSJeff Kirsher 992ded19addSJeff Kirsher return; 993ded19addSJeff Kirsher } else { 994ded19addSJeff Kirsher pasemi_mac_intf_enable(mac); 995ded19addSJeff Kirsher netif_carrier_on(dev); 996ded19addSJeff Kirsher } 997ded19addSJeff Kirsher 998ded19addSJeff Kirsher flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG); 999ded19addSJeff Kirsher new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M | 1000ded19addSJeff Kirsher PAS_MAC_CFG_PCFG_TSR_M); 1001ded19addSJeff Kirsher 100280721e7fSPhilippe Reynes if (!dev->phydev->duplex) 1003ded19addSJeff Kirsher new_flags |= PAS_MAC_CFG_PCFG_HD; 1004ded19addSJeff Kirsher 100580721e7fSPhilippe Reynes switch (dev->phydev->speed) { 1006ded19addSJeff Kirsher case 1000: 1007ded19addSJeff Kirsher new_flags |= PAS_MAC_CFG_PCFG_SPD_1G | 1008ded19addSJeff Kirsher PAS_MAC_CFG_PCFG_TSR_1G; 1009ded19addSJeff Kirsher break; 1010ded19addSJeff Kirsher case 100: 1011ded19addSJeff Kirsher new_flags |= PAS_MAC_CFG_PCFG_SPD_100M | 1012ded19addSJeff Kirsher PAS_MAC_CFG_PCFG_TSR_100M; 1013ded19addSJeff Kirsher break; 1014ded19addSJeff Kirsher case 10: 1015ded19addSJeff Kirsher new_flags |= PAS_MAC_CFG_PCFG_SPD_10M | 1016ded19addSJeff Kirsher PAS_MAC_CFG_PCFG_TSR_10M; 1017ded19addSJeff Kirsher break; 1018ded19addSJeff Kirsher default: 101980721e7fSPhilippe Reynes printk("Unsupported speed %d\n", dev->phydev->speed); 1020ded19addSJeff Kirsher } 1021ded19addSJeff Kirsher 1022ded19addSJeff Kirsher /* Print on link or speed/duplex change */ 102380721e7fSPhilippe Reynes msg = mac->link != dev->phydev->link || flags != new_flags; 1024ded19addSJeff Kirsher 102580721e7fSPhilippe Reynes mac->duplex = dev->phydev->duplex; 102680721e7fSPhilippe Reynes mac->speed = dev->phydev->speed; 102780721e7fSPhilippe Reynes mac->link = dev->phydev->link; 1028ded19addSJeff Kirsher 1029ded19addSJeff Kirsher if (new_flags != flags) 1030ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags); 1031ded19addSJeff Kirsher 1032ded19addSJeff Kirsher if (msg && netif_msg_link(mac)) 1033ded19addSJeff Kirsher printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n", 1034ded19addSJeff Kirsher dev->name, mac->speed, mac->duplex ? "full" : "half"); 1035ded19addSJeff Kirsher } 1036ded19addSJeff Kirsher 1037ded19addSJeff Kirsher static int pasemi_mac_phy_init(struct net_device *dev) 1038ded19addSJeff Kirsher { 1039ded19addSJeff Kirsher struct pasemi_mac *mac = netdev_priv(dev); 1040ded19addSJeff Kirsher struct device_node *dn, *phy_dn; 1041ded19addSJeff Kirsher struct phy_device *phydev; 1042ded19addSJeff Kirsher 1043ded19addSJeff Kirsher dn = pci_device_to_OF_node(mac->pdev); 1044ded19addSJeff Kirsher phy_dn = of_parse_phandle(dn, "phy-handle", 0); 1045ded19addSJeff Kirsher 1046ded19addSJeff Kirsher mac->link = 0; 1047ded19addSJeff Kirsher mac->speed = 0; 1048ded19addSJeff Kirsher mac->duplex = -1; 1049ded19addSJeff Kirsher 1050ded19addSJeff Kirsher phydev = of_phy_connect(dev, phy_dn, &pasemi_adjust_link, 0, 1051ded19addSJeff Kirsher PHY_INTERFACE_MODE_SGMII); 1052ded19addSJeff Kirsher 1053faf5577fSWen Yang of_node_put(phy_dn); 1054beb5ac20SWei Yongjun if (!phydev) { 1055ded19addSJeff Kirsher printk(KERN_ERR "%s: Could not attach to phy\n", dev->name); 1056beb5ac20SWei Yongjun return -ENODEV; 1057ded19addSJeff Kirsher } 1058ded19addSJeff Kirsher 1059ded19addSJeff Kirsher return 0; 1060ded19addSJeff Kirsher } 1061ded19addSJeff Kirsher 1062ded19addSJeff Kirsher 1063ded19addSJeff Kirsher static int pasemi_mac_open(struct net_device *dev) 1064ded19addSJeff Kirsher { 1065ded19addSJeff Kirsher struct pasemi_mac *mac = netdev_priv(dev); 1066ded19addSJeff Kirsher unsigned int flags; 1067ded19addSJeff Kirsher int i, ret; 1068ded19addSJeff Kirsher 1069ded19addSJeff Kirsher flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) | 1070ded19addSJeff Kirsher PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) | 1071ded19addSJeff Kirsher PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12); 1072ded19addSJeff Kirsher 1073ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_CFG_TXP, flags); 1074ded19addSJeff Kirsher 1075ded19addSJeff Kirsher ret = pasemi_mac_setup_rx_resources(dev); 1076ded19addSJeff Kirsher if (ret) 1077ded19addSJeff Kirsher goto out_rx_resources; 1078ded19addSJeff Kirsher 1079ded19addSJeff Kirsher mac->tx = pasemi_mac_setup_tx_resources(dev); 1080ded19addSJeff Kirsher 1081*aba84871SZhang Changzhong if (!mac->tx) { 1082*aba84871SZhang Changzhong ret = -ENOMEM; 1083ded19addSJeff Kirsher goto out_tx_ring; 1084*aba84871SZhang Changzhong } 1085ded19addSJeff Kirsher 1086ded19addSJeff Kirsher /* We might already have allocated rings in case mtu was changed 1087ded19addSJeff Kirsher * before interface was brought up. 1088ded19addSJeff Kirsher */ 1089ded19addSJeff Kirsher if (dev->mtu > 1500 && !mac->num_cs) { 1090ded19addSJeff Kirsher pasemi_mac_setup_csrings(mac); 1091*aba84871SZhang Changzhong if (!mac->num_cs) { 1092*aba84871SZhang Changzhong ret = -ENOMEM; 1093ded19addSJeff Kirsher goto out_tx_ring; 1094ded19addSJeff Kirsher } 1095*aba84871SZhang Changzhong } 1096ded19addSJeff Kirsher 1097ded19addSJeff Kirsher /* Zero out rmon counters */ 1098ded19addSJeff Kirsher for (i = 0; i < 32; i++) 1099ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_RMON(i), 0); 1100ded19addSJeff Kirsher 1101ded19addSJeff Kirsher /* 0x3ff with 33MHz clock is about 31us */ 1102ded19addSJeff Kirsher write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG, 1103ded19addSJeff Kirsher PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff)); 1104ded19addSJeff Kirsher 1105ded19addSJeff Kirsher write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno), 1106ded19addSJeff Kirsher PAS_IOB_DMA_RXCH_CFG_CNTTH(256)); 1107ded19addSJeff Kirsher 1108ded19addSJeff Kirsher write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno), 1109ded19addSJeff Kirsher PAS_IOB_DMA_TXCH_CFG_CNTTH(32)); 1110ded19addSJeff Kirsher 1111ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_IPC_CHNL, 1112ded19addSJeff Kirsher PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) | 1113ded19addSJeff Kirsher PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno)); 1114ded19addSJeff Kirsher 1115ded19addSJeff Kirsher /* enable rx if */ 1116ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 1117ded19addSJeff Kirsher PAS_DMA_RXINT_RCMDSTA_EN | 1118ded19addSJeff Kirsher PAS_DMA_RXINT_RCMDSTA_DROPS_M | 1119ded19addSJeff Kirsher PAS_DMA_RXINT_RCMDSTA_BP | 1120ded19addSJeff Kirsher PAS_DMA_RXINT_RCMDSTA_OO | 1121ded19addSJeff Kirsher PAS_DMA_RXINT_RCMDSTA_BT); 1122ded19addSJeff Kirsher 1123ded19addSJeff Kirsher /* enable rx channel */ 1124ded19addSJeff Kirsher pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU | 1125ded19addSJeff Kirsher PAS_DMA_RXCHAN_CCMDSTA_OD | 1126ded19addSJeff Kirsher PAS_DMA_RXCHAN_CCMDSTA_FD | 1127ded19addSJeff Kirsher PAS_DMA_RXCHAN_CCMDSTA_DT); 1128ded19addSJeff Kirsher 1129ded19addSJeff Kirsher /* enable tx channel */ 1130ded19addSJeff Kirsher pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ | 1131ded19addSJeff Kirsher PAS_DMA_TXCHAN_TCMDSTA_DB | 1132ded19addSJeff Kirsher PAS_DMA_TXCHAN_TCMDSTA_DE | 1133ded19addSJeff Kirsher PAS_DMA_TXCHAN_TCMDSTA_DA); 1134ded19addSJeff Kirsher 1135ded19addSJeff Kirsher pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE); 1136ded19addSJeff Kirsher 1137ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno), 1138ded19addSJeff Kirsher RX_RING_SIZE>>1); 1139ded19addSJeff Kirsher 1140ded19addSJeff Kirsher /* Clear out any residual packet count state from firmware */ 1141ded19addSJeff Kirsher pasemi_mac_restart_rx_intr(mac); 1142ded19addSJeff Kirsher pasemi_mac_restart_tx_intr(mac); 1143ded19addSJeff Kirsher 1144ded19addSJeff Kirsher flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE; 1145ded19addSJeff Kirsher 1146ded19addSJeff Kirsher if (mac->type == MAC_TYPE_GMAC) 1147ded19addSJeff Kirsher flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G; 1148ded19addSJeff Kirsher else 1149ded19addSJeff Kirsher flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G; 1150ded19addSJeff Kirsher 1151ded19addSJeff Kirsher /* Enable interface in MAC */ 1152ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags); 1153ded19addSJeff Kirsher 1154ded19addSJeff Kirsher ret = pasemi_mac_phy_init(dev); 1155ded19addSJeff Kirsher if (ret) { 1156ded19addSJeff Kirsher /* Since we won't get link notification, just enable RX */ 1157ded19addSJeff Kirsher pasemi_mac_intf_enable(mac); 1158ded19addSJeff Kirsher if (mac->type == MAC_TYPE_GMAC) { 1159ded19addSJeff Kirsher /* Warn for missing PHY on SGMII (1Gig) ports */ 1160ded19addSJeff Kirsher dev_warn(&mac->pdev->dev, 1161ded19addSJeff Kirsher "PHY init failed: %d.\n", ret); 1162ded19addSJeff Kirsher dev_warn(&mac->pdev->dev, 1163ded19addSJeff Kirsher "Defaulting to 1Gbit full duplex\n"); 1164ded19addSJeff Kirsher } 1165ded19addSJeff Kirsher } 1166ded19addSJeff Kirsher 1167ded19addSJeff Kirsher netif_start_queue(dev); 1168ded19addSJeff Kirsher napi_enable(&mac->napi); 1169ded19addSJeff Kirsher 1170ded19addSJeff Kirsher snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx", 1171ded19addSJeff Kirsher dev->name); 1172ded19addSJeff Kirsher 117378874271SMichael Opdenacker ret = request_irq(mac->tx->chan.irq, pasemi_mac_tx_intr, 0, 1174ded19addSJeff Kirsher mac->tx_irq_name, mac->tx); 1175ded19addSJeff Kirsher if (ret) { 1176ded19addSJeff Kirsher dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n", 1177ded19addSJeff Kirsher mac->tx->chan.irq, ret); 1178ded19addSJeff Kirsher goto out_tx_int; 1179ded19addSJeff Kirsher } 1180ded19addSJeff Kirsher 1181ded19addSJeff Kirsher snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx", 1182ded19addSJeff Kirsher dev->name); 1183ded19addSJeff Kirsher 118478874271SMichael Opdenacker ret = request_irq(mac->rx->chan.irq, pasemi_mac_rx_intr, 0, 1185ded19addSJeff Kirsher mac->rx_irq_name, mac->rx); 1186ded19addSJeff Kirsher if (ret) { 1187ded19addSJeff Kirsher dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n", 1188ded19addSJeff Kirsher mac->rx->chan.irq, ret); 1189ded19addSJeff Kirsher goto out_rx_int; 1190ded19addSJeff Kirsher } 1191ded19addSJeff Kirsher 119280721e7fSPhilippe Reynes if (dev->phydev) 119380721e7fSPhilippe Reynes phy_start(dev->phydev); 1194ded19addSJeff Kirsher 1195e99e88a9SKees Cook timer_setup(&mac->tx->clean_timer, pasemi_mac_tx_timer, 0); 1196187d6785SVaishali Thakkar mod_timer(&mac->tx->clean_timer, jiffies + HZ); 1197ded19addSJeff Kirsher 1198ded19addSJeff Kirsher return 0; 1199ded19addSJeff Kirsher 1200ded19addSJeff Kirsher out_rx_int: 1201ded19addSJeff Kirsher free_irq(mac->tx->chan.irq, mac->tx); 1202ded19addSJeff Kirsher out_tx_int: 1203ded19addSJeff Kirsher napi_disable(&mac->napi); 1204ded19addSJeff Kirsher netif_stop_queue(dev); 1205ded19addSJeff Kirsher out_tx_ring: 1206ded19addSJeff Kirsher if (mac->tx) 1207ded19addSJeff Kirsher pasemi_mac_free_tx_resources(mac); 1208ded19addSJeff Kirsher pasemi_mac_free_rx_resources(mac); 1209ded19addSJeff Kirsher out_rx_resources: 1210ded19addSJeff Kirsher 1211ded19addSJeff Kirsher return ret; 1212ded19addSJeff Kirsher } 1213ded19addSJeff Kirsher 1214ded19addSJeff Kirsher #define MAX_RETRIES 5000 1215ded19addSJeff Kirsher 1216ded19addSJeff Kirsher static void pasemi_mac_pause_txchan(struct pasemi_mac *mac) 1217ded19addSJeff Kirsher { 1218ded19addSJeff Kirsher unsigned int sta, retries; 1219ded19addSJeff Kirsher int txch = tx_ring(mac)->chan.chno; 1220ded19addSJeff Kirsher 1221ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 1222ded19addSJeff Kirsher PAS_DMA_TXCHAN_TCMDSTA_ST); 1223ded19addSJeff Kirsher 1224ded19addSJeff Kirsher for (retries = 0; retries < MAX_RETRIES; retries++) { 1225ded19addSJeff Kirsher sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch)); 1226ded19addSJeff Kirsher if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)) 1227ded19addSJeff Kirsher break; 1228ded19addSJeff Kirsher cond_resched(); 1229ded19addSJeff Kirsher } 1230ded19addSJeff Kirsher 1231ded19addSJeff Kirsher if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT) 1232ded19addSJeff Kirsher dev_err(&mac->dma_pdev->dev, 1233ded19addSJeff Kirsher "Failed to stop tx channel, tcmdsta %08x\n", sta); 1234ded19addSJeff Kirsher 1235ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0); 1236ded19addSJeff Kirsher } 1237ded19addSJeff Kirsher 1238ded19addSJeff Kirsher static void pasemi_mac_pause_rxchan(struct pasemi_mac *mac) 1239ded19addSJeff Kirsher { 1240ded19addSJeff Kirsher unsigned int sta, retries; 1241ded19addSJeff Kirsher int rxch = rx_ring(mac)->chan.chno; 1242ded19addSJeff Kirsher 1243ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 1244ded19addSJeff Kirsher PAS_DMA_RXCHAN_CCMDSTA_ST); 1245ded19addSJeff Kirsher for (retries = 0; retries < MAX_RETRIES; retries++) { 1246ded19addSJeff Kirsher sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch)); 1247ded19addSJeff Kirsher if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)) 1248ded19addSJeff Kirsher break; 1249ded19addSJeff Kirsher cond_resched(); 1250ded19addSJeff Kirsher } 1251ded19addSJeff Kirsher 1252ded19addSJeff Kirsher if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT) 1253ded19addSJeff Kirsher dev_err(&mac->dma_pdev->dev, 1254ded19addSJeff Kirsher "Failed to stop rx channel, ccmdsta 08%x\n", sta); 1255ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0); 1256ded19addSJeff Kirsher } 1257ded19addSJeff Kirsher 1258ded19addSJeff Kirsher static void pasemi_mac_pause_rxint(struct pasemi_mac *mac) 1259ded19addSJeff Kirsher { 1260ded19addSJeff Kirsher unsigned int sta, retries; 1261ded19addSJeff Kirsher 1262ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 1263ded19addSJeff Kirsher PAS_DMA_RXINT_RCMDSTA_ST); 1264ded19addSJeff Kirsher for (retries = 0; retries < MAX_RETRIES; retries++) { 1265ded19addSJeff Kirsher sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if)); 1266ded19addSJeff Kirsher if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT)) 1267ded19addSJeff Kirsher break; 1268ded19addSJeff Kirsher cond_resched(); 1269ded19addSJeff Kirsher } 1270ded19addSJeff Kirsher 1271ded19addSJeff Kirsher if (sta & PAS_DMA_RXINT_RCMDSTA_ACT) 1272ded19addSJeff Kirsher dev_err(&mac->dma_pdev->dev, 1273ded19addSJeff Kirsher "Failed to stop rx interface, rcmdsta %08x\n", sta); 1274ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0); 1275ded19addSJeff Kirsher } 1276ded19addSJeff Kirsher 1277ded19addSJeff Kirsher static int pasemi_mac_close(struct net_device *dev) 1278ded19addSJeff Kirsher { 1279ded19addSJeff Kirsher struct pasemi_mac *mac = netdev_priv(dev); 1280ded19addSJeff Kirsher unsigned int sta; 1281ded19addSJeff Kirsher int rxch, txch, i; 1282ded19addSJeff Kirsher 1283ded19addSJeff Kirsher rxch = rx_ring(mac)->chan.chno; 1284ded19addSJeff Kirsher txch = tx_ring(mac)->chan.chno; 1285ded19addSJeff Kirsher 128680721e7fSPhilippe Reynes if (dev->phydev) { 128780721e7fSPhilippe Reynes phy_stop(dev->phydev); 128880721e7fSPhilippe Reynes phy_disconnect(dev->phydev); 1289ded19addSJeff Kirsher } 1290ded19addSJeff Kirsher 1291ded19addSJeff Kirsher del_timer_sync(&mac->tx->clean_timer); 1292ded19addSJeff Kirsher 1293ded19addSJeff Kirsher netif_stop_queue(dev); 1294ded19addSJeff Kirsher napi_disable(&mac->napi); 1295ded19addSJeff Kirsher 1296ded19addSJeff Kirsher sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if)); 1297ded19addSJeff Kirsher if (sta & (PAS_DMA_RXINT_RCMDSTA_BP | 1298ded19addSJeff Kirsher PAS_DMA_RXINT_RCMDSTA_OO | 1299ded19addSJeff Kirsher PAS_DMA_RXINT_RCMDSTA_BT)) 1300ded19addSJeff Kirsher printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta); 1301ded19addSJeff Kirsher 1302ded19addSJeff Kirsher sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch)); 1303ded19addSJeff Kirsher if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU | 1304ded19addSJeff Kirsher PAS_DMA_RXCHAN_CCMDSTA_OD | 1305ded19addSJeff Kirsher PAS_DMA_RXCHAN_CCMDSTA_FD | 1306ded19addSJeff Kirsher PAS_DMA_RXCHAN_CCMDSTA_DT)) 1307ded19addSJeff Kirsher printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta); 1308ded19addSJeff Kirsher 1309ded19addSJeff Kirsher sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch)); 1310ded19addSJeff Kirsher if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB | 1311ded19addSJeff Kirsher PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA)) 1312ded19addSJeff Kirsher printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta); 1313ded19addSJeff Kirsher 1314ded19addSJeff Kirsher /* Clean out any pending buffers */ 1315ded19addSJeff Kirsher pasemi_mac_clean_tx(tx_ring(mac)); 1316ded19addSJeff Kirsher pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE); 1317ded19addSJeff Kirsher 1318ded19addSJeff Kirsher pasemi_mac_pause_txchan(mac); 1319ded19addSJeff Kirsher pasemi_mac_pause_rxint(mac); 1320ded19addSJeff Kirsher pasemi_mac_pause_rxchan(mac); 1321ded19addSJeff Kirsher pasemi_mac_intf_disable(mac); 1322ded19addSJeff Kirsher 1323ded19addSJeff Kirsher free_irq(mac->tx->chan.irq, mac->tx); 1324ded19addSJeff Kirsher free_irq(mac->rx->chan.irq, mac->rx); 1325ded19addSJeff Kirsher 1326ded19addSJeff Kirsher for (i = 0; i < mac->num_cs; i++) { 1327ded19addSJeff Kirsher pasemi_mac_free_csring(mac->cs[i]); 1328ded19addSJeff Kirsher mac->cs[i] = NULL; 1329ded19addSJeff Kirsher } 1330ded19addSJeff Kirsher 1331ded19addSJeff Kirsher mac->num_cs = 0; 1332ded19addSJeff Kirsher 1333ded19addSJeff Kirsher /* Free resources */ 1334ded19addSJeff Kirsher pasemi_mac_free_rx_resources(mac); 1335ded19addSJeff Kirsher pasemi_mac_free_tx_resources(mac); 1336ded19addSJeff Kirsher 1337ded19addSJeff Kirsher return 0; 1338ded19addSJeff Kirsher } 1339ded19addSJeff Kirsher 1340ded19addSJeff Kirsher static void pasemi_mac_queue_csdesc(const struct sk_buff *skb, 1341ded19addSJeff Kirsher const dma_addr_t *map, 1342ded19addSJeff Kirsher const unsigned int *map_size, 1343ded19addSJeff Kirsher struct pasemi_mac_txring *txring, 1344ded19addSJeff Kirsher struct pasemi_mac_csring *csring) 1345ded19addSJeff Kirsher { 1346ded19addSJeff Kirsher u64 fund; 1347ded19addSJeff Kirsher dma_addr_t cs_dest; 1348ded19addSJeff Kirsher const int nh_off = skb_network_offset(skb); 1349ded19addSJeff Kirsher const int nh_len = skb_network_header_len(skb); 1350ded19addSJeff Kirsher const int nfrags = skb_shinfo(skb)->nr_frags; 13511789b8aaSYueHaibing int cs_size, i, fill, hdr, evt; 1352ded19addSJeff Kirsher dma_addr_t csdma; 1353ded19addSJeff Kirsher 1354ded19addSJeff Kirsher fund = XCT_FUN_ST | XCT_FUN_RR_8BRES | 1355ded19addSJeff Kirsher XCT_FUN_O | XCT_FUN_FUN(csring->fun) | 1356ded19addSJeff Kirsher XCT_FUN_CRM_SIG | XCT_FUN_LLEN(skb->len - nh_off) | 1357ded19addSJeff Kirsher XCT_FUN_SHL(nh_len >> 2) | XCT_FUN_SE; 1358ded19addSJeff Kirsher 1359ded19addSJeff Kirsher switch (ip_hdr(skb)->protocol) { 1360ded19addSJeff Kirsher case IPPROTO_TCP: 1361ded19addSJeff Kirsher fund |= XCT_FUN_SIG_TCP4; 1362ded19addSJeff Kirsher /* TCP checksum is 16 bytes into the header */ 1363ded19addSJeff Kirsher cs_dest = map[0] + skb_transport_offset(skb) + 16; 1364ded19addSJeff Kirsher break; 1365ded19addSJeff Kirsher case IPPROTO_UDP: 1366ded19addSJeff Kirsher fund |= XCT_FUN_SIG_UDP4; 1367ded19addSJeff Kirsher /* UDP checksum is 6 bytes into the header */ 1368ded19addSJeff Kirsher cs_dest = map[0] + skb_transport_offset(skb) + 6; 1369ded19addSJeff Kirsher break; 1370ded19addSJeff Kirsher default: 1371ded19addSJeff Kirsher BUG(); 1372ded19addSJeff Kirsher } 1373ded19addSJeff Kirsher 1374ded19addSJeff Kirsher /* Do the checksum offloaded */ 1375ded19addSJeff Kirsher fill = csring->next_to_fill; 1376ded19addSJeff Kirsher hdr = fill; 1377ded19addSJeff Kirsher 1378ded19addSJeff Kirsher CS_DESC(csring, fill++) = fund; 1379ded19addSJeff Kirsher /* Room for 8BRES. Checksum result is really 2 bytes into it */ 1380ded19addSJeff Kirsher csdma = csring->chan.ring_dma + (fill & (CS_RING_SIZE-1)) * 8 + 2; 1381ded19addSJeff Kirsher CS_DESC(csring, fill++) = 0; 1382ded19addSJeff Kirsher 1383ded19addSJeff Kirsher CS_DESC(csring, fill) = XCT_PTR_LEN(map_size[0]-nh_off) | XCT_PTR_ADDR(map[0]+nh_off); 1384ded19addSJeff Kirsher for (i = 1; i <= nfrags; i++) 1385ded19addSJeff Kirsher CS_DESC(csring, fill+i) = XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]); 1386ded19addSJeff Kirsher 1387ded19addSJeff Kirsher fill += i; 1388ded19addSJeff Kirsher if (fill & 1) 1389ded19addSJeff Kirsher fill++; 1390ded19addSJeff Kirsher 1391ded19addSJeff Kirsher /* Copy the result into the TCP packet */ 1392ded19addSJeff Kirsher CS_DESC(csring, fill++) = XCT_FUN_O | XCT_FUN_FUN(csring->fun) | 1393ded19addSJeff Kirsher XCT_FUN_LLEN(2) | XCT_FUN_SE; 1394ded19addSJeff Kirsher CS_DESC(csring, fill++) = XCT_PTR_LEN(2) | XCT_PTR_ADDR(cs_dest) | XCT_PTR_T; 1395ded19addSJeff Kirsher CS_DESC(csring, fill++) = XCT_PTR_LEN(2) | XCT_PTR_ADDR(csdma); 1396ded19addSJeff Kirsher fill++; 1397ded19addSJeff Kirsher 1398ded19addSJeff Kirsher evt = !csring->last_event; 1399ded19addSJeff Kirsher csring->last_event = evt; 1400ded19addSJeff Kirsher 1401ded19addSJeff Kirsher /* Event handshaking with MAC TX */ 1402ded19addSJeff Kirsher CS_DESC(csring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O | 1403ded19addSJeff Kirsher CTRL_CMD_ETYPE_SET | CTRL_CMD_REG(csring->events[evt]); 1404ded19addSJeff Kirsher CS_DESC(csring, fill++) = 0; 1405ded19addSJeff Kirsher CS_DESC(csring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O | 1406ded19addSJeff Kirsher CTRL_CMD_ETYPE_WCLR | CTRL_CMD_REG(csring->events[!evt]); 1407ded19addSJeff Kirsher CS_DESC(csring, fill++) = 0; 1408ded19addSJeff Kirsher csring->next_to_fill = fill & (CS_RING_SIZE-1); 1409ded19addSJeff Kirsher 1410ded19addSJeff Kirsher cs_size = fill - hdr; 1411ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_INCR(csring->chan.chno), (cs_size) >> 1); 1412ded19addSJeff Kirsher 1413ded19addSJeff Kirsher /* TX-side event handshaking */ 1414ded19addSJeff Kirsher fill = txring->next_to_fill; 1415ded19addSJeff Kirsher TX_DESC(txring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O | 1416ded19addSJeff Kirsher CTRL_CMD_ETYPE_WSET | CTRL_CMD_REG(csring->events[evt]); 1417ded19addSJeff Kirsher TX_DESC(txring, fill++) = 0; 1418ded19addSJeff Kirsher TX_DESC(txring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O | 1419ded19addSJeff Kirsher CTRL_CMD_ETYPE_CLR | CTRL_CMD_REG(csring->events[!evt]); 1420ded19addSJeff Kirsher TX_DESC(txring, fill++) = 0; 1421ded19addSJeff Kirsher txring->next_to_fill = fill; 1422ded19addSJeff Kirsher 1423ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), 2); 1424ded19addSJeff Kirsher } 1425ded19addSJeff Kirsher 1426ded19addSJeff Kirsher static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev) 1427ded19addSJeff Kirsher { 1428ded19addSJeff Kirsher struct pasemi_mac * const mac = netdev_priv(dev); 1429ded19addSJeff Kirsher struct pasemi_mac_txring * const txring = tx_ring(mac); 1430ded19addSJeff Kirsher struct pasemi_mac_csring *csring; 1431ded19addSJeff Kirsher u64 dflags = 0; 1432ded19addSJeff Kirsher u64 mactx; 1433ded19addSJeff Kirsher dma_addr_t map[MAX_SKB_FRAGS+1]; 1434ded19addSJeff Kirsher unsigned int map_size[MAX_SKB_FRAGS+1]; 1435ded19addSJeff Kirsher unsigned long flags; 1436ded19addSJeff Kirsher int i, nfrags; 1437ded19addSJeff Kirsher int fill; 1438ded19addSJeff Kirsher const int nh_off = skb_network_offset(skb); 1439ded19addSJeff Kirsher const int nh_len = skb_network_header_len(skb); 1440ded19addSJeff Kirsher 1441ded19addSJeff Kirsher prefetch(&txring->ring_info); 1442ded19addSJeff Kirsher 1443ded19addSJeff Kirsher dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD; 1444ded19addSJeff Kirsher 1445ded19addSJeff Kirsher nfrags = skb_shinfo(skb)->nr_frags; 1446ded19addSJeff Kirsher 1447ded19addSJeff Kirsher map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb), 1448ded19addSJeff Kirsher PCI_DMA_TODEVICE); 1449ded19addSJeff Kirsher map_size[0] = skb_headlen(skb); 1450ded19addSJeff Kirsher if (pci_dma_mapping_error(mac->dma_pdev, map[0])) 1451ded19addSJeff Kirsher goto out_err_nolock; 1452ded19addSJeff Kirsher 1453ded19addSJeff Kirsher for (i = 0; i < nfrags; i++) { 1454ded19addSJeff Kirsher skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1455ded19addSJeff Kirsher 14564bb97caeSIan Campbell map[i + 1] = skb_frag_dma_map(&mac->dma_pdev->dev, frag, 0, 14579e903e08SEric Dumazet skb_frag_size(frag), DMA_TO_DEVICE); 14589e903e08SEric Dumazet map_size[i+1] = skb_frag_size(frag); 14595d6bcdfeSIan Campbell if (dma_mapping_error(&mac->dma_pdev->dev, map[i + 1])) { 1460ded19addSJeff Kirsher nfrags = i; 1461ded19addSJeff Kirsher goto out_err_nolock; 1462ded19addSJeff Kirsher } 1463ded19addSJeff Kirsher } 1464ded19addSJeff Kirsher 1465ded19addSJeff Kirsher if (skb->ip_summed == CHECKSUM_PARTIAL && skb->len <= 1540) { 1466ded19addSJeff Kirsher switch (ip_hdr(skb)->protocol) { 1467ded19addSJeff Kirsher case IPPROTO_TCP: 1468ded19addSJeff Kirsher dflags |= XCT_MACTX_CSUM_TCP; 1469ded19addSJeff Kirsher dflags |= XCT_MACTX_IPH(nh_len >> 2); 1470ded19addSJeff Kirsher dflags |= XCT_MACTX_IPO(nh_off); 1471ded19addSJeff Kirsher break; 1472ded19addSJeff Kirsher case IPPROTO_UDP: 1473ded19addSJeff Kirsher dflags |= XCT_MACTX_CSUM_UDP; 1474ded19addSJeff Kirsher dflags |= XCT_MACTX_IPH(nh_len >> 2); 1475ded19addSJeff Kirsher dflags |= XCT_MACTX_IPO(nh_off); 1476ded19addSJeff Kirsher break; 1477ded19addSJeff Kirsher default: 1478ded19addSJeff Kirsher WARN_ON(1); 1479ded19addSJeff Kirsher } 1480ded19addSJeff Kirsher } 1481ded19addSJeff Kirsher 1482ded19addSJeff Kirsher mactx = dflags | XCT_MACTX_LLEN(skb->len); 1483ded19addSJeff Kirsher 1484ded19addSJeff Kirsher spin_lock_irqsave(&txring->lock, flags); 1485ded19addSJeff Kirsher 1486ded19addSJeff Kirsher /* Avoid stepping on the same cache line that the DMA controller 1487ded19addSJeff Kirsher * is currently about to send, so leave at least 8 words available. 1488ded19addSJeff Kirsher * Total free space needed is mactx + fragments + 8 1489ded19addSJeff Kirsher */ 1490ded19addSJeff Kirsher if (RING_AVAIL(txring) < nfrags + 14) { 1491ded19addSJeff Kirsher /* no room -- stop the queue and wait for tx intr */ 1492ded19addSJeff Kirsher netif_stop_queue(dev); 1493ded19addSJeff Kirsher goto out_err; 1494ded19addSJeff Kirsher } 1495ded19addSJeff Kirsher 1496ded19addSJeff Kirsher /* Queue up checksum + event descriptors, if needed */ 1497ded19addSJeff Kirsher if (mac->num_cs && skb->ip_summed == CHECKSUM_PARTIAL && skb->len > 1540) { 1498ded19addSJeff Kirsher csring = mac->cs[mac->last_cs]; 1499ded19addSJeff Kirsher mac->last_cs = (mac->last_cs + 1) % mac->num_cs; 1500ded19addSJeff Kirsher 1501ded19addSJeff Kirsher pasemi_mac_queue_csdesc(skb, map, map_size, txring, csring); 1502ded19addSJeff Kirsher } 1503ded19addSJeff Kirsher 1504ded19addSJeff Kirsher fill = txring->next_to_fill; 1505ded19addSJeff Kirsher TX_DESC(txring, fill) = mactx; 1506ded19addSJeff Kirsher TX_DESC_INFO(txring, fill).dma = nfrags; 1507ded19addSJeff Kirsher fill++; 1508ded19addSJeff Kirsher TX_DESC_INFO(txring, fill).skb = skb; 1509ded19addSJeff Kirsher for (i = 0; i <= nfrags; i++) { 1510ded19addSJeff Kirsher TX_DESC(txring, fill+i) = 1511ded19addSJeff Kirsher XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]); 1512ded19addSJeff Kirsher TX_DESC_INFO(txring, fill+i).dma = map[i]; 1513ded19addSJeff Kirsher } 1514ded19addSJeff Kirsher 1515ded19addSJeff Kirsher /* We have to add an even number of 8-byte entries to the ring 1516ded19addSJeff Kirsher * even if the last one is unused. That means always an odd number 1517ded19addSJeff Kirsher * of pointers + one mactx descriptor. 1518ded19addSJeff Kirsher */ 1519ded19addSJeff Kirsher if (nfrags & 1) 1520ded19addSJeff Kirsher nfrags++; 1521ded19addSJeff Kirsher 1522ded19addSJeff Kirsher txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1); 1523ded19addSJeff Kirsher 1524ded19addSJeff Kirsher dev->stats.tx_packets++; 1525ded19addSJeff Kirsher dev->stats.tx_bytes += skb->len; 1526ded19addSJeff Kirsher 1527ded19addSJeff Kirsher spin_unlock_irqrestore(&txring->lock, flags); 1528ded19addSJeff Kirsher 1529ded19addSJeff Kirsher write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1); 1530ded19addSJeff Kirsher 1531ded19addSJeff Kirsher return NETDEV_TX_OK; 1532ded19addSJeff Kirsher 1533ded19addSJeff Kirsher out_err: 1534ded19addSJeff Kirsher spin_unlock_irqrestore(&txring->lock, flags); 1535ded19addSJeff Kirsher out_err_nolock: 1536ded19addSJeff Kirsher while (nfrags--) 1537ded19addSJeff Kirsher pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags], 1538ded19addSJeff Kirsher PCI_DMA_TODEVICE); 1539ded19addSJeff Kirsher 1540ded19addSJeff Kirsher return NETDEV_TX_BUSY; 1541ded19addSJeff Kirsher } 1542ded19addSJeff Kirsher 1543ded19addSJeff Kirsher static void pasemi_mac_set_rx_mode(struct net_device *dev) 1544ded19addSJeff Kirsher { 1545ded19addSJeff Kirsher const struct pasemi_mac *mac = netdev_priv(dev); 1546ded19addSJeff Kirsher unsigned int flags; 1547ded19addSJeff Kirsher 1548ded19addSJeff Kirsher flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG); 1549ded19addSJeff Kirsher 1550ded19addSJeff Kirsher /* Set promiscuous */ 1551ded19addSJeff Kirsher if (dev->flags & IFF_PROMISC) 1552ded19addSJeff Kirsher flags |= PAS_MAC_CFG_PCFG_PR; 1553ded19addSJeff Kirsher else 1554ded19addSJeff Kirsher flags &= ~PAS_MAC_CFG_PCFG_PR; 1555ded19addSJeff Kirsher 1556ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags); 1557ded19addSJeff Kirsher } 1558ded19addSJeff Kirsher 1559ded19addSJeff Kirsher 1560ded19addSJeff Kirsher static int pasemi_mac_poll(struct napi_struct *napi, int budget) 1561ded19addSJeff Kirsher { 1562ded19addSJeff Kirsher struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi); 1563ded19addSJeff Kirsher int pkts; 1564ded19addSJeff Kirsher 1565ded19addSJeff Kirsher pasemi_mac_clean_tx(tx_ring(mac)); 1566ded19addSJeff Kirsher pkts = pasemi_mac_clean_rx(rx_ring(mac), budget); 1567ded19addSJeff Kirsher if (pkts < budget) { 1568ded19addSJeff Kirsher /* all done, no more packets present */ 15696ad20165SEric Dumazet napi_complete_done(napi, pkts); 1570ded19addSJeff Kirsher 1571ded19addSJeff Kirsher pasemi_mac_restart_rx_intr(mac); 1572ded19addSJeff Kirsher pasemi_mac_restart_tx_intr(mac); 1573ded19addSJeff Kirsher } 1574ded19addSJeff Kirsher return pkts; 1575ded19addSJeff Kirsher } 1576ded19addSJeff Kirsher 1577ded19addSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 1578ded19addSJeff Kirsher /* 1579ded19addSJeff Kirsher * Polling 'interrupt' - used by things like netconsole to send skbs 1580ded19addSJeff Kirsher * without having to re-enable interrupts. It's not called while 1581ded19addSJeff Kirsher * the interrupt routine is executing. 1582ded19addSJeff Kirsher */ 1583ded19addSJeff Kirsher static void pasemi_mac_netpoll(struct net_device *dev) 1584ded19addSJeff Kirsher { 1585ded19addSJeff Kirsher const struct pasemi_mac *mac = netdev_priv(dev); 1586ded19addSJeff Kirsher 1587ded19addSJeff Kirsher disable_irq(mac->tx->chan.irq); 1588ded19addSJeff Kirsher pasemi_mac_tx_intr(mac->tx->chan.irq, mac->tx); 1589ded19addSJeff Kirsher enable_irq(mac->tx->chan.irq); 1590ded19addSJeff Kirsher 1591ded19addSJeff Kirsher disable_irq(mac->rx->chan.irq); 1592ded19addSJeff Kirsher pasemi_mac_rx_intr(mac->rx->chan.irq, mac->rx); 1593ded19addSJeff Kirsher enable_irq(mac->rx->chan.irq); 1594ded19addSJeff Kirsher } 1595ded19addSJeff Kirsher #endif 1596ded19addSJeff Kirsher 1597ded19addSJeff Kirsher static int pasemi_mac_change_mtu(struct net_device *dev, int new_mtu) 1598ded19addSJeff Kirsher { 1599ded19addSJeff Kirsher struct pasemi_mac *mac = netdev_priv(dev); 1600ded19addSJeff Kirsher unsigned int reg; 1601ded19addSJeff Kirsher unsigned int rcmdsta = 0; 1602ded19addSJeff Kirsher int running; 1603ded19addSJeff Kirsher int ret = 0; 1604ded19addSJeff Kirsher 1605ded19addSJeff Kirsher running = netif_running(dev); 1606ded19addSJeff Kirsher 1607ded19addSJeff Kirsher if (running) { 1608ded19addSJeff Kirsher /* Need to stop the interface, clean out all already 1609ded19addSJeff Kirsher * received buffers, free all unused buffers on the RX 1610ded19addSJeff Kirsher * interface ring, then finally re-fill the rx ring with 1611ded19addSJeff Kirsher * the new-size buffers and restart. 1612ded19addSJeff Kirsher */ 1613ded19addSJeff Kirsher 1614ded19addSJeff Kirsher napi_disable(&mac->napi); 1615ded19addSJeff Kirsher netif_tx_disable(dev); 1616ded19addSJeff Kirsher pasemi_mac_intf_disable(mac); 1617ded19addSJeff Kirsher 1618ded19addSJeff Kirsher rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if)); 1619ded19addSJeff Kirsher pasemi_mac_pause_rxint(mac); 1620ded19addSJeff Kirsher pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE); 1621ded19addSJeff Kirsher pasemi_mac_free_rx_buffers(mac); 1622ded19addSJeff Kirsher 1623ded19addSJeff Kirsher } 1624ded19addSJeff Kirsher 1625ded19addSJeff Kirsher /* Setup checksum channels if large MTU and none already allocated */ 162644770e11SJarod Wilson if (new_mtu > PE_DEF_MTU && !mac->num_cs) { 1627ded19addSJeff Kirsher pasemi_mac_setup_csrings(mac); 1628ded19addSJeff Kirsher if (!mac->num_cs) { 1629ded19addSJeff Kirsher ret = -ENOMEM; 1630ded19addSJeff Kirsher goto out; 1631ded19addSJeff Kirsher } 1632ded19addSJeff Kirsher } 1633ded19addSJeff Kirsher 1634ded19addSJeff Kirsher /* Change maxf, i.e. what size frames are accepted. 1635ded19addSJeff Kirsher * Need room for ethernet header and CRC word 1636ded19addSJeff Kirsher */ 1637ded19addSJeff Kirsher reg = read_mac_reg(mac, PAS_MAC_CFG_MACCFG); 1638ded19addSJeff Kirsher reg &= ~PAS_MAC_CFG_MACCFG_MAXF_M; 1639ded19addSJeff Kirsher reg |= PAS_MAC_CFG_MACCFG_MAXF(new_mtu + ETH_HLEN + 4); 1640ded19addSJeff Kirsher write_mac_reg(mac, PAS_MAC_CFG_MACCFG, reg); 1641ded19addSJeff Kirsher 1642ded19addSJeff Kirsher dev->mtu = new_mtu; 1643ded19addSJeff Kirsher /* MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */ 1644ded19addSJeff Kirsher mac->bufsz = new_mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128; 1645ded19addSJeff Kirsher 1646ded19addSJeff Kirsher out: 1647ded19addSJeff Kirsher if (running) { 1648ded19addSJeff Kirsher write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 1649ded19addSJeff Kirsher rcmdsta | PAS_DMA_RXINT_RCMDSTA_EN); 1650ded19addSJeff Kirsher 1651ded19addSJeff Kirsher rx_ring(mac)->next_to_fill = 0; 1652ded19addSJeff Kirsher pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE-1); 1653ded19addSJeff Kirsher 1654ded19addSJeff Kirsher napi_enable(&mac->napi); 1655ded19addSJeff Kirsher netif_start_queue(dev); 1656ded19addSJeff Kirsher pasemi_mac_intf_enable(mac); 1657ded19addSJeff Kirsher } 1658ded19addSJeff Kirsher 1659ded19addSJeff Kirsher return ret; 1660ded19addSJeff Kirsher } 1661ded19addSJeff Kirsher 1662ded19addSJeff Kirsher static const struct net_device_ops pasemi_netdev_ops = { 1663ded19addSJeff Kirsher .ndo_open = pasemi_mac_open, 1664ded19addSJeff Kirsher .ndo_stop = pasemi_mac_close, 1665ded19addSJeff Kirsher .ndo_start_xmit = pasemi_mac_start_tx, 1666afc4b13dSJiri Pirko .ndo_set_rx_mode = pasemi_mac_set_rx_mode, 1667ded19addSJeff Kirsher .ndo_set_mac_address = pasemi_mac_set_mac_addr, 1668ded19addSJeff Kirsher .ndo_change_mtu = pasemi_mac_change_mtu, 1669ded19addSJeff Kirsher .ndo_validate_addr = eth_validate_addr, 1670ded19addSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 1671ded19addSJeff Kirsher .ndo_poll_controller = pasemi_mac_netpoll, 1672ded19addSJeff Kirsher #endif 1673ded19addSJeff Kirsher }; 1674ded19addSJeff Kirsher 167503c4d832SBill Pemberton static int 1676ded19addSJeff Kirsher pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 1677ded19addSJeff Kirsher { 1678ded19addSJeff Kirsher struct net_device *dev; 1679ded19addSJeff Kirsher struct pasemi_mac *mac; 1680ded19addSJeff Kirsher int err, ret; 1681ded19addSJeff Kirsher 1682ded19addSJeff Kirsher err = pci_enable_device(pdev); 1683ded19addSJeff Kirsher if (err) 1684ded19addSJeff Kirsher return err; 1685ded19addSJeff Kirsher 1686ded19addSJeff Kirsher dev = alloc_etherdev(sizeof(struct pasemi_mac)); 1687ded19addSJeff Kirsher if (dev == NULL) { 1688ded19addSJeff Kirsher err = -ENOMEM; 1689ded19addSJeff Kirsher goto out_disable_device; 1690ded19addSJeff Kirsher } 1691ded19addSJeff Kirsher 1692ded19addSJeff Kirsher pci_set_drvdata(pdev, dev); 1693ded19addSJeff Kirsher SET_NETDEV_DEV(dev, &pdev->dev); 1694ded19addSJeff Kirsher 1695ded19addSJeff Kirsher mac = netdev_priv(dev); 1696ded19addSJeff Kirsher 1697ded19addSJeff Kirsher mac->pdev = pdev; 1698ded19addSJeff Kirsher mac->netdev = dev; 1699ded19addSJeff Kirsher 1700ded19addSJeff Kirsher netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64); 1701ded19addSJeff Kirsher 1702ded19addSJeff Kirsher dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG | 1703ded19addSJeff Kirsher NETIF_F_HIGHDMA | NETIF_F_GSO; 1704ded19addSJeff Kirsher 1705ded19addSJeff Kirsher mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL); 1706ded19addSJeff Kirsher if (!mac->dma_pdev) { 1707ded19addSJeff Kirsher dev_err(&mac->pdev->dev, "Can't find DMA Controller\n"); 1708ded19addSJeff Kirsher err = -ENODEV; 1709ded19addSJeff Kirsher goto out; 1710ded19addSJeff Kirsher } 171174ebe3e7SChristoph Hellwig dma_set_mask(&mac->dma_pdev->dev, DMA_BIT_MASK(64)); 1712ded19addSJeff Kirsher 1713ded19addSJeff Kirsher mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL); 1714ded19addSJeff Kirsher if (!mac->iob_pdev) { 1715ded19addSJeff Kirsher dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n"); 1716ded19addSJeff Kirsher err = -ENODEV; 1717ded19addSJeff Kirsher goto out; 1718ded19addSJeff Kirsher } 1719ded19addSJeff Kirsher 1720ded19addSJeff Kirsher /* get mac addr from device tree */ 1721ded19addSJeff Kirsher if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) { 1722ded19addSJeff Kirsher err = -ENODEV; 1723ded19addSJeff Kirsher goto out; 1724ded19addSJeff Kirsher } 1725ded19addSJeff Kirsher memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr)); 1726ded19addSJeff Kirsher 1727ded19addSJeff Kirsher ret = mac_to_intf(mac); 1728ded19addSJeff Kirsher if (ret < 0) { 1729ded19addSJeff Kirsher dev_err(&mac->pdev->dev, "Can't map DMA interface\n"); 1730ded19addSJeff Kirsher err = -ENODEV; 1731ded19addSJeff Kirsher goto out; 1732ded19addSJeff Kirsher } 1733ded19addSJeff Kirsher mac->dma_if = ret; 1734ded19addSJeff Kirsher 1735ded19addSJeff Kirsher switch (pdev->device) { 1736ded19addSJeff Kirsher case 0xa005: 1737ded19addSJeff Kirsher mac->type = MAC_TYPE_GMAC; 1738ded19addSJeff Kirsher break; 1739ded19addSJeff Kirsher case 0xa006: 1740ded19addSJeff Kirsher mac->type = MAC_TYPE_XAUI; 1741ded19addSJeff Kirsher break; 1742ded19addSJeff Kirsher default: 1743ded19addSJeff Kirsher err = -ENODEV; 1744ded19addSJeff Kirsher goto out; 1745ded19addSJeff Kirsher } 1746ded19addSJeff Kirsher 1747ded19addSJeff Kirsher dev->netdev_ops = &pasemi_netdev_ops; 1748ded19addSJeff Kirsher dev->mtu = PE_DEF_MTU; 174944770e11SJarod Wilson 175044770e11SJarod Wilson /* MTU range: 64 - 9000 */ 175144770e11SJarod Wilson dev->min_mtu = PE_MIN_MTU; 175244770e11SJarod Wilson dev->max_mtu = PE_MAX_MTU; 175344770e11SJarod Wilson 1754ded19addSJeff Kirsher /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */ 1755ded19addSJeff Kirsher mac->bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128; 1756ded19addSJeff Kirsher 1757ded19addSJeff Kirsher dev->ethtool_ops = &pasemi_mac_ethtool_ops; 1758ded19addSJeff Kirsher 1759ded19addSJeff Kirsher if (err) 1760ded19addSJeff Kirsher goto out; 1761ded19addSJeff Kirsher 1762ded19addSJeff Kirsher mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE); 1763ded19addSJeff Kirsher 1764ded19addSJeff Kirsher /* Enable most messages by default */ 1765ded19addSJeff Kirsher mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1; 1766ded19addSJeff Kirsher 1767ded19addSJeff Kirsher err = register_netdev(dev); 1768ded19addSJeff Kirsher 1769ded19addSJeff Kirsher if (err) { 1770ded19addSJeff Kirsher dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n", 1771ded19addSJeff Kirsher err); 1772ded19addSJeff Kirsher goto out; 1773ea5cdcccSSyam Sidhardhan } else if (netif_msg_probe(mac)) { 1774ded19addSJeff Kirsher printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %pM\n", 1775ded19addSJeff Kirsher dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI", 1776ded19addSJeff Kirsher mac->dma_if, dev->dev_addr); 1777ea5cdcccSSyam Sidhardhan } 1778ded19addSJeff Kirsher 1779ded19addSJeff Kirsher return err; 1780ded19addSJeff Kirsher 1781ded19addSJeff Kirsher out: 1782ded19addSJeff Kirsher pci_dev_put(mac->iob_pdev); 1783ded19addSJeff Kirsher pci_dev_put(mac->dma_pdev); 1784ded19addSJeff Kirsher 1785ded19addSJeff Kirsher free_netdev(dev); 1786ded19addSJeff Kirsher out_disable_device: 1787ded19addSJeff Kirsher pci_disable_device(pdev); 1788ded19addSJeff Kirsher return err; 1789ded19addSJeff Kirsher 1790ded19addSJeff Kirsher } 1791ded19addSJeff Kirsher 179203c4d832SBill Pemberton static void pasemi_mac_remove(struct pci_dev *pdev) 1793ded19addSJeff Kirsher { 1794ded19addSJeff Kirsher struct net_device *netdev = pci_get_drvdata(pdev); 1795ded19addSJeff Kirsher struct pasemi_mac *mac; 1796ded19addSJeff Kirsher 1797ded19addSJeff Kirsher if (!netdev) 1798ded19addSJeff Kirsher return; 1799ded19addSJeff Kirsher 1800ded19addSJeff Kirsher mac = netdev_priv(netdev); 1801ded19addSJeff Kirsher 1802ded19addSJeff Kirsher unregister_netdev(netdev); 1803ded19addSJeff Kirsher 1804ded19addSJeff Kirsher pci_disable_device(pdev); 1805ded19addSJeff Kirsher pci_dev_put(mac->dma_pdev); 1806ded19addSJeff Kirsher pci_dev_put(mac->iob_pdev); 1807ded19addSJeff Kirsher 1808ded19addSJeff Kirsher pasemi_dma_free_chan(&mac->tx->chan); 1809ded19addSJeff Kirsher pasemi_dma_free_chan(&mac->rx->chan); 1810ded19addSJeff Kirsher 1811ded19addSJeff Kirsher free_netdev(netdev); 1812ded19addSJeff Kirsher } 1813ded19addSJeff Kirsher 18149baa3c34SBenoit Taine static const struct pci_device_id pasemi_mac_pci_tbl[] = { 1815ded19addSJeff Kirsher { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) }, 1816ded19addSJeff Kirsher { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) }, 1817ded19addSJeff Kirsher { }, 1818ded19addSJeff Kirsher }; 1819ded19addSJeff Kirsher 1820ded19addSJeff Kirsher MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl); 1821ded19addSJeff Kirsher 1822ded19addSJeff Kirsher static struct pci_driver pasemi_mac_driver = { 1823ded19addSJeff Kirsher .name = "pasemi_mac", 1824ded19addSJeff Kirsher .id_table = pasemi_mac_pci_tbl, 1825ded19addSJeff Kirsher .probe = pasemi_mac_probe, 182603c4d832SBill Pemberton .remove = pasemi_mac_remove, 1827ded19addSJeff Kirsher }; 1828ded19addSJeff Kirsher 1829ded19addSJeff Kirsher static void __exit pasemi_mac_cleanup_module(void) 1830ded19addSJeff Kirsher { 1831ded19addSJeff Kirsher pci_unregister_driver(&pasemi_mac_driver); 1832ded19addSJeff Kirsher } 1833ded19addSJeff Kirsher 183464c40525SYueHaibing static int pasemi_mac_init_module(void) 1835ded19addSJeff Kirsher { 1836ded19addSJeff Kirsher int err; 1837ded19addSJeff Kirsher 1838ded19addSJeff Kirsher err = pasemi_dma_init(); 1839ded19addSJeff Kirsher if (err) 1840ded19addSJeff Kirsher return err; 1841ded19addSJeff Kirsher 1842ded19addSJeff Kirsher return pci_register_driver(&pasemi_mac_driver); 1843ded19addSJeff Kirsher } 1844ded19addSJeff Kirsher 1845ded19addSJeff Kirsher module_init(pasemi_mac_init_module); 1846ded19addSJeff Kirsher module_exit(pasemi_mac_cleanup_module); 1847