Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2007 PA Semi, Inc |
| 3 | * |
| 4 | * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/pci.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/dmaengine.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/etherdevice.h> |
| 28 | #include <asm/dma-mapping.h> |
| 29 | #include <linux/in.h> |
| 30 | #include <linux/skbuff.h> |
| 31 | |
| 32 | #include <linux/ip.h> |
| 33 | #include <linux/tcp.h> |
| 34 | #include <net/checksum.h> |
| 35 | |
Olof Johansson | 771f740 | 2007-05-08 00:47:21 -0500 | [diff] [blame] | 36 | #include <asm/irq.h> |
| 37 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 38 | #include "pasemi_mac.h" |
| 39 | |
Olof Johansson | 8dc121a | 2007-10-02 16:26:53 -0500 | [diff] [blame] | 40 | /* We have our own align, since ppc64 in general has it at 0 because |
| 41 | * of design flaws in some of the server bridge chips. However, for |
| 42 | * PWRficient doing the unaligned copies is more expensive than doing |
| 43 | * unaligned DMA, so make sure the data is aligned instead. |
| 44 | */ |
| 45 | #define LOCAL_SKB_ALIGN 2 |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 46 | |
| 47 | /* TODO list |
| 48 | * |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 49 | * - Multicast support |
| 50 | * - Large MTU support |
Olof Johansson | 7ddeae2 | 2007-10-02 16:27:28 -0500 | [diff] [blame] | 51 | * - SW LRO |
| 52 | * - Multiqueue RX/TX |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 53 | */ |
| 54 | |
| 55 | |
| 56 | /* Must be a power of two */ |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 57 | #define RX_RING_SIZE 4096 |
| 58 | #define TX_RING_SIZE 4096 |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 59 | |
Olof Johansson | ceb5136 | 2007-05-08 00:47:49 -0500 | [diff] [blame] | 60 | #define DEFAULT_MSG_ENABLE \ |
| 61 | (NETIF_MSG_DRV | \ |
| 62 | NETIF_MSG_PROBE | \ |
| 63 | NETIF_MSG_LINK | \ |
| 64 | NETIF_MSG_TIMER | \ |
| 65 | NETIF_MSG_IFDOWN | \ |
| 66 | NETIF_MSG_IFUP | \ |
| 67 | NETIF_MSG_RX_ERR | \ |
| 68 | NETIF_MSG_TX_ERR) |
| 69 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 70 | #define TX_RING(mac, num) ((mac)->tx->ring[(num) & (TX_RING_SIZE-1)]) |
| 71 | #define TX_RING_INFO(mac, num) ((mac)->tx->ring_info[(num) & (TX_RING_SIZE-1)]) |
| 72 | #define RX_RING(mac, num) ((mac)->rx->ring[(num) & (RX_RING_SIZE-1)]) |
| 73 | #define RX_RING_INFO(mac, num) ((mac)->rx->ring_info[(num) & (RX_RING_SIZE-1)]) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 74 | #define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)]) |
| 75 | |
Olof Johansson | 021fa22 | 2007-08-22 09:13:11 -0500 | [diff] [blame] | 76 | #define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \ |
| 77 | & ((ring)->size - 1)) |
| 78 | #define RING_AVAIL(ring) ((ring->size) - RING_USED(ring)) |
| 79 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 80 | #define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */ |
| 81 | |
Olof Johansson | ceb5136 | 2007-05-08 00:47:49 -0500 | [diff] [blame] | 82 | MODULE_LICENSE("GPL"); |
| 83 | MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>"); |
| 84 | MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver"); |
| 85 | |
| 86 | static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */ |
| 87 | module_param(debug, int, 0); |
| 88 | MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value"); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 89 | |
| 90 | static struct pasdma_status *dma_status; |
| 91 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 92 | static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg, |
| 93 | unsigned int val) |
| 94 | { |
Olof Johansson | b6e05a1 | 2007-09-15 13:44:07 -0700 | [diff] [blame] | 95 | out_le32(mac->iob_regs+reg, val); |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg) |
| 99 | { |
Olof Johansson | b6e05a1 | 2007-09-15 13:44:07 -0700 | [diff] [blame] | 100 | return in_le32(mac->regs+reg); |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg, |
| 104 | unsigned int val) |
| 105 | { |
Olof Johansson | b6e05a1 | 2007-09-15 13:44:07 -0700 | [diff] [blame] | 106 | out_le32(mac->regs+reg, val); |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg) |
| 110 | { |
Olof Johansson | b6e05a1 | 2007-09-15 13:44:07 -0700 | [diff] [blame] | 111 | return in_le32(mac->dma_regs+reg); |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg, |
| 115 | unsigned int val) |
| 116 | { |
Olof Johansson | b6e05a1 | 2007-09-15 13:44:07 -0700 | [diff] [blame] | 117 | out_le32(mac->dma_regs+reg, val); |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 120 | static int pasemi_get_mac_addr(struct pasemi_mac *mac) |
| 121 | { |
| 122 | struct pci_dev *pdev = mac->pdev; |
| 123 | struct device_node *dn = pci_device_to_OF_node(pdev); |
olof@lixom.net | 1af7f05 | 2007-05-12 14:57:46 -0500 | [diff] [blame] | 124 | int len; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 125 | const u8 *maddr; |
| 126 | u8 addr[6]; |
| 127 | |
| 128 | if (!dn) { |
| 129 | dev_dbg(&pdev->dev, |
| 130 | "No device node for mac, not configuring\n"); |
| 131 | return -ENOENT; |
| 132 | } |
| 133 | |
olof@lixom.net | 1af7f05 | 2007-05-12 14:57:46 -0500 | [diff] [blame] | 134 | maddr = of_get_property(dn, "local-mac-address", &len); |
Olof Johansson | a5fd22e | 2007-05-08 00:48:02 -0500 | [diff] [blame] | 135 | |
olof@lixom.net | 1af7f05 | 2007-05-12 14:57:46 -0500 | [diff] [blame] | 136 | if (maddr && len == 6) { |
| 137 | memcpy(mac->mac_addr, maddr, 6); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* Some old versions of firmware mistakenly uses mac-address |
| 142 | * (and as a string) instead of a byte array in local-mac-address. |
| 143 | */ |
| 144 | |
Olof Johansson | a5fd22e | 2007-05-08 00:48:02 -0500 | [diff] [blame] | 145 | if (maddr == NULL) |
Linus Torvalds | 9028780 | 2007-05-08 11:57:17 -0700 | [diff] [blame] | 146 | maddr = of_get_property(dn, "mac-address", NULL); |
Olof Johansson | a5fd22e | 2007-05-08 00:48:02 -0500 | [diff] [blame] | 147 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 148 | if (maddr == NULL) { |
| 149 | dev_warn(&pdev->dev, |
| 150 | "no mac address in device tree, not configuring\n"); |
| 151 | return -ENOENT; |
| 152 | } |
| 153 | |
olof@lixom.net | 1af7f05 | 2007-05-12 14:57:46 -0500 | [diff] [blame] | 154 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 155 | if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0], |
| 156 | &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) { |
| 157 | dev_warn(&pdev->dev, |
| 158 | "can't parse mac address, not configuring\n"); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | |
olof@lixom.net | 1af7f05 | 2007-05-12 14:57:46 -0500 | [diff] [blame] | 162 | memcpy(mac->mac_addr, addr, 6); |
| 163 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 167 | static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac, |
| 168 | struct sk_buff *skb, |
| 169 | dma_addr_t *dmas) |
| 170 | { |
| 171 | int f; |
| 172 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 173 | |
| 174 | pci_unmap_single(mac->dma_pdev, dmas[0], skb_headlen(skb), |
| 175 | PCI_DMA_TODEVICE); |
| 176 | |
| 177 | for (f = 0; f < nfrags; f++) { |
| 178 | skb_frag_t *frag = &skb_shinfo(skb)->frags[f]; |
| 179 | |
| 180 | pci_unmap_page(mac->dma_pdev, dmas[f+1], frag->size, |
| 181 | PCI_DMA_TODEVICE); |
| 182 | } |
| 183 | dev_kfree_skb_irq(skb); |
| 184 | |
| 185 | /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs, |
| 186 | * aligned up to a power of 2 |
| 187 | */ |
| 188 | return (nfrags + 3) & ~1; |
| 189 | } |
| 190 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 191 | static int pasemi_mac_setup_rx_resources(struct net_device *dev) |
| 192 | { |
| 193 | struct pasemi_mac_rxring *ring; |
| 194 | struct pasemi_mac *mac = netdev_priv(dev); |
| 195 | int chan_id = mac->dma_rxch; |
| 196 | |
| 197 | ring = kzalloc(sizeof(*ring), GFP_KERNEL); |
| 198 | |
| 199 | if (!ring) |
| 200 | goto out_ring; |
| 201 | |
| 202 | spin_lock_init(&ring->lock); |
| 203 | |
Olof Johansson | 021fa22 | 2007-08-22 09:13:11 -0500 | [diff] [blame] | 204 | ring->size = RX_RING_SIZE; |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 205 | ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) * |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 206 | RX_RING_SIZE, GFP_KERNEL); |
| 207 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 208 | if (!ring->ring_info) |
| 209 | goto out_ring_info; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 210 | |
| 211 | /* Allocate descriptors */ |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 212 | ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev, |
| 213 | RX_RING_SIZE * sizeof(u64), |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 214 | &ring->dma, GFP_KERNEL); |
| 215 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 216 | if (!ring->ring) |
| 217 | goto out_ring_desc; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 218 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 219 | memset(ring->ring, 0, RX_RING_SIZE * sizeof(u64)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 220 | |
| 221 | ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev, |
| 222 | RX_RING_SIZE * sizeof(u64), |
| 223 | &ring->buf_dma, GFP_KERNEL); |
| 224 | if (!ring->buffers) |
| 225 | goto out_buffers; |
| 226 | |
| 227 | memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64)); |
| 228 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 229 | write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 230 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 231 | write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id), |
| 232 | PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 233 | PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 234 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 235 | write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id), |
Olof Johansson | c0efd52 | 2007-08-22 09:12:52 -0500 | [diff] [blame] | 236 | PAS_DMA_RXCHAN_CFG_HBU(2)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 237 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 238 | write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if), |
| 239 | PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers))); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 240 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 241 | write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if), |
| 242 | PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) | |
| 243 | PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 244 | |
Olof Johansson | c0efd52 | 2007-08-22 09:12:52 -0500 | [diff] [blame] | 245 | write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if), |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 246 | PAS_DMA_RXINT_CFG_DHL(3) | PAS_DMA_RXINT_CFG_L2 | |
| 247 | PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP | |
| 248 | PAS_DMA_RXINT_CFG_HEN); |
Olof Johansson | c0efd52 | 2007-08-22 09:12:52 -0500 | [diff] [blame] | 249 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 250 | ring->next_to_fill = 0; |
| 251 | ring->next_to_clean = 0; |
| 252 | |
| 253 | snprintf(ring->irq_name, sizeof(ring->irq_name), |
| 254 | "%s rx", dev->name); |
| 255 | mac->rx = ring; |
| 256 | |
| 257 | return 0; |
| 258 | |
| 259 | out_buffers: |
| 260 | dma_free_coherent(&mac->dma_pdev->dev, |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 261 | RX_RING_SIZE * sizeof(u64), |
| 262 | mac->rx->ring, mac->rx->dma); |
| 263 | out_ring_desc: |
| 264 | kfree(ring->ring_info); |
| 265 | out_ring_info: |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 266 | kfree(ring); |
| 267 | out_ring: |
| 268 | return -ENOMEM; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | static int pasemi_mac_setup_tx_resources(struct net_device *dev) |
| 273 | { |
| 274 | struct pasemi_mac *mac = netdev_priv(dev); |
| 275 | u32 val; |
| 276 | int chan_id = mac->dma_txch; |
| 277 | struct pasemi_mac_txring *ring; |
| 278 | |
| 279 | ring = kzalloc(sizeof(*ring), GFP_KERNEL); |
| 280 | if (!ring) |
| 281 | goto out_ring; |
| 282 | |
| 283 | spin_lock_init(&ring->lock); |
| 284 | |
Olof Johansson | 021fa22 | 2007-08-22 09:13:11 -0500 | [diff] [blame] | 285 | ring->size = TX_RING_SIZE; |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 286 | ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) * |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 287 | TX_RING_SIZE, GFP_KERNEL); |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 288 | if (!ring->ring_info) |
| 289 | goto out_ring_info; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 290 | |
| 291 | /* Allocate descriptors */ |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 292 | ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev, |
| 293 | TX_RING_SIZE * sizeof(u64), |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 294 | &ring->dma, GFP_KERNEL); |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 295 | if (!ring->ring) |
| 296 | goto out_ring_desc; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 297 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 298 | memset(ring->ring, 0, TX_RING_SIZE * sizeof(u64)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 299 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 300 | write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id), |
| 301 | PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 302 | val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32); |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 303 | val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 304 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 305 | write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 306 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 307 | write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id), |
| 308 | PAS_DMA_TXCHAN_CFG_TY_IFACE | |
| 309 | PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) | |
| 310 | PAS_DMA_TXCHAN_CFG_UP | |
| 311 | PAS_DMA_TXCHAN_CFG_WT(2)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 312 | |
Olof Johansson | 021fa22 | 2007-08-22 09:13:11 -0500 | [diff] [blame] | 313 | ring->next_to_fill = 0; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 314 | ring->next_to_clean = 0; |
| 315 | |
| 316 | snprintf(ring->irq_name, sizeof(ring->irq_name), |
| 317 | "%s tx", dev->name); |
| 318 | mac->tx = ring; |
| 319 | |
| 320 | return 0; |
| 321 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 322 | out_ring_desc: |
| 323 | kfree(ring->ring_info); |
| 324 | out_ring_info: |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 325 | kfree(ring); |
| 326 | out_ring: |
| 327 | return -ENOMEM; |
| 328 | } |
| 329 | |
| 330 | static void pasemi_mac_free_tx_resources(struct net_device *dev) |
| 331 | { |
| 332 | struct pasemi_mac *mac = netdev_priv(dev); |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 333 | unsigned int i, j; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 334 | struct pasemi_mac_buffer *info; |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 335 | dma_addr_t dmas[MAX_SKB_FRAGS+1]; |
| 336 | int freed; |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 337 | int start, limit; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 338 | |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 339 | start = mac->tx->next_to_clean; |
| 340 | limit = mac->tx->next_to_fill; |
| 341 | |
| 342 | /* Compensate for when fill has wrapped and clean has not */ |
| 343 | if (start > limit) |
| 344 | limit += TX_RING_SIZE; |
| 345 | |
| 346 | for (i = start; i < limit; i += freed) { |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 347 | info = &TX_RING_INFO(mac, i+1); |
| 348 | if (info->dma && info->skb) { |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 349 | for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++) |
| 350 | dmas[j] = TX_RING_INFO(mac, i+1+j).dma; |
| 351 | freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas); |
| 352 | } else |
| 353 | freed = 2; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 354 | } |
| 355 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 356 | for (i = 0; i < TX_RING_SIZE; i++) |
| 357 | TX_RING(mac, i) = 0; |
| 358 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 359 | dma_free_coherent(&mac->dma_pdev->dev, |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 360 | TX_RING_SIZE * sizeof(u64), |
| 361 | mac->tx->ring, mac->tx->dma); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 362 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 363 | kfree(mac->tx->ring_info); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 364 | kfree(mac->tx); |
| 365 | mac->tx = NULL; |
| 366 | } |
| 367 | |
| 368 | static void pasemi_mac_free_rx_resources(struct net_device *dev) |
| 369 | { |
| 370 | struct pasemi_mac *mac = netdev_priv(dev); |
| 371 | unsigned int i; |
| 372 | struct pasemi_mac_buffer *info; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 373 | |
| 374 | for (i = 0; i < RX_RING_SIZE; i++) { |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 375 | info = &RX_RING_INFO(mac, i); |
| 376 | if (info->skb && info->dma) { |
| 377 | pci_unmap_single(mac->dma_pdev, |
| 378 | info->dma, |
| 379 | info->skb->len, |
| 380 | PCI_DMA_FROMDEVICE); |
| 381 | dev_kfree_skb_any(info->skb); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 382 | } |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 383 | info->dma = 0; |
| 384 | info->skb = NULL; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 385 | } |
| 386 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 387 | for (i = 0; i < RX_RING_SIZE; i++) |
| 388 | RX_RING(mac, i) = 0; |
| 389 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 390 | dma_free_coherent(&mac->dma_pdev->dev, |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 391 | RX_RING_SIZE * sizeof(u64), |
| 392 | mac->rx->ring, mac->rx->dma); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 393 | |
| 394 | dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64), |
| 395 | mac->rx->buffers, mac->rx->buf_dma); |
| 396 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 397 | kfree(mac->rx->ring_info); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 398 | kfree(mac->rx); |
| 399 | mac->rx = NULL; |
| 400 | } |
| 401 | |
Olof Johansson | 928773c | 2007-09-26 16:25:06 -0500 | [diff] [blame] | 402 | static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 403 | { |
| 404 | struct pasemi_mac *mac = netdev_priv(dev); |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 405 | int fill, count; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 406 | |
Olof Johansson | cd4ceb2 | 2007-05-08 00:47:45 -0500 | [diff] [blame] | 407 | if (limit <= 0) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 408 | return; |
| 409 | |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 410 | fill = mac->rx->next_to_fill; |
Olof Johansson | 928773c | 2007-09-26 16:25:06 -0500 | [diff] [blame] | 411 | for (count = 0; count < limit; count++) { |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 412 | struct pasemi_mac_buffer *info = &RX_RING_INFO(mac, fill); |
| 413 | u64 *buff = &RX_BUFF(mac, fill); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 414 | struct sk_buff *skb; |
| 415 | dma_addr_t dma; |
| 416 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 417 | /* Entry in use? */ |
| 418 | WARN_ON(*buff); |
| 419 | |
Olof Johansson | 9f05cfe | 2007-05-08 00:47:37 -0500 | [diff] [blame] | 420 | /* skb might still be in there for recycle on short receives */ |
| 421 | if (info->skb) |
| 422 | skb = info->skb; |
Olof Johansson | 8dc121a | 2007-10-02 16:26:53 -0500 | [diff] [blame] | 423 | else { |
Olof Johansson | 9f05cfe | 2007-05-08 00:47:37 -0500 | [diff] [blame] | 424 | skb = dev_alloc_skb(BUF_SIZE); |
Olof Johansson | 8dc121a | 2007-10-02 16:26:53 -0500 | [diff] [blame] | 425 | skb_reserve(skb, LOCAL_SKB_ALIGN); |
| 426 | } |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 427 | |
Olof Johansson | 9f05cfe | 2007-05-08 00:47:37 -0500 | [diff] [blame] | 428 | if (unlikely(!skb)) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 429 | break; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 430 | |
Olof Johansson | 8dc121a | 2007-10-02 16:26:53 -0500 | [diff] [blame] | 431 | dma = pci_map_single(mac->dma_pdev, skb->data, |
| 432 | BUF_SIZE - LOCAL_SKB_ALIGN, |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 433 | PCI_DMA_FROMDEVICE); |
| 434 | |
Olof Johansson | cd4ceb2 | 2007-05-08 00:47:45 -0500 | [diff] [blame] | 435 | if (unlikely(dma_mapping_error(dma))) { |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 436 | dev_kfree_skb_irq(info->skb); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | |
| 440 | info->skb = skb; |
| 441 | info->dma = dma; |
| 442 | *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma); |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 443 | fill++; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | wmb(); |
| 447 | |
Olof Johansson | 928773c | 2007-09-26 16:25:06 -0500 | [diff] [blame] | 448 | write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 449 | |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 450 | mac->rx->next_to_fill = (mac->rx->next_to_fill + count) & |
| 451 | (RX_RING_SIZE - 1); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 452 | } |
| 453 | |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 454 | static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac) |
| 455 | { |
Olof Johansson | 52a9435 | 2007-05-12 18:01:09 -0500 | [diff] [blame] | 456 | unsigned int reg, pcnt; |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 457 | /* Re-enable packet count interrupts: finally |
| 458 | * ack the packet count interrupt we got in rx_intr. |
| 459 | */ |
| 460 | |
Olof Johansson | 52a9435 | 2007-05-12 18:01:09 -0500 | [diff] [blame] | 461 | pcnt = *mac->rx_status & PAS_STATUS_PCNT_M; |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 462 | |
Olof Johansson | 52a9435 | 2007-05-12 18:01:09 -0500 | [diff] [blame] | 463 | reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC; |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 464 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 465 | write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg); |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac) |
| 469 | { |
Olof Johansson | 52a9435 | 2007-05-12 18:01:09 -0500 | [diff] [blame] | 470 | unsigned int reg, pcnt; |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 471 | |
| 472 | /* Re-enable packet count interrupts */ |
Olof Johansson | 52a9435 | 2007-05-12 18:01:09 -0500 | [diff] [blame] | 473 | pcnt = *mac->tx_status & PAS_STATUS_PCNT_M; |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 474 | |
Olof Johansson | 52a9435 | 2007-05-12 18:01:09 -0500 | [diff] [blame] | 475 | reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC; |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 476 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 477 | write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg); |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
Olof Johansson | 69c29d8 | 2007-10-02 16:24:51 -0500 | [diff] [blame] | 481 | static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx) |
| 482 | { |
| 483 | unsigned int rcmdsta, ccmdsta; |
| 484 | |
| 485 | if (!netif_msg_rx_err(mac)) |
| 486 | return; |
| 487 | |
| 488 | rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if)); |
| 489 | ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch)); |
| 490 | |
| 491 | printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n", |
| 492 | macrx, *mac->rx_status); |
| 493 | |
| 494 | printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n", |
| 495 | rcmdsta, ccmdsta); |
| 496 | } |
| 497 | |
| 498 | static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx) |
| 499 | { |
| 500 | unsigned int cmdsta; |
| 501 | |
| 502 | if (!netif_msg_tx_err(mac)) |
| 503 | return; |
| 504 | |
| 505 | cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch)); |
| 506 | |
| 507 | printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\ |
| 508 | "tx status 0x%016lx\n", mactx, *mac->tx_status); |
| 509 | |
| 510 | printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta); |
| 511 | } |
| 512 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 513 | static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit) |
| 514 | { |
Olof Johansson | cd4ceb2 | 2007-05-08 00:47:45 -0500 | [diff] [blame] | 515 | unsigned int n; |
| 516 | int count; |
Olof Johansson | cd4ceb2 | 2007-05-08 00:47:45 -0500 | [diff] [blame] | 517 | struct pasemi_mac_buffer *info; |
| 518 | struct sk_buff *skb; |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 519 | unsigned int len; |
Olof Johansson | cd4ceb2 | 2007-05-08 00:47:45 -0500 | [diff] [blame] | 520 | u64 macrx; |
| 521 | dma_addr_t dma; |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 522 | int buf_index; |
| 523 | u64 eval; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 524 | |
| 525 | spin_lock(&mac->rx->lock); |
| 526 | |
Olof Johansson | cd4ceb2 | 2007-05-08 00:47:45 -0500 | [diff] [blame] | 527 | n = mac->rx->next_to_clean; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 528 | |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 529 | prefetch(RX_RING(mac, n)); |
| 530 | |
| 531 | for (count = 0; count < limit; count++) { |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 532 | macrx = RX_RING(mac, n); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 533 | |
Olof Johansson | 69c29d8 | 2007-10-02 16:24:51 -0500 | [diff] [blame] | 534 | if ((macrx & XCT_MACRX_E) || |
| 535 | (*mac->rx_status & PAS_STATUS_ERROR)) |
| 536 | pasemi_mac_rx_error(mac, macrx); |
| 537 | |
Olof Johansson | cd4ceb2 | 2007-05-08 00:47:45 -0500 | [diff] [blame] | 538 | if (!(macrx & XCT_MACRX_O)) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 539 | break; |
| 540 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 541 | info = NULL; |
| 542 | |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 543 | BUG_ON(!(macrx & XCT_MACRX_RR_8BRES)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 544 | |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 545 | eval = (RX_RING(mac, n+1) & XCT_RXRES_8B_EVAL_M) >> |
| 546 | XCT_RXRES_8B_EVAL_S; |
| 547 | buf_index = eval-1; |
| 548 | |
| 549 | dma = (RX_RING(mac, n+2) & XCT_PTR_ADDR_M); |
| 550 | info = &RX_RING_INFO(mac, buf_index); |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 551 | |
Olof Johansson | 9f05cfe | 2007-05-08 00:47:37 -0500 | [diff] [blame] | 552 | skb = info->skb; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 553 | |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 554 | prefetch(skb); |
| 555 | prefetch(&skb->data_len); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 556 | |
Olof Johansson | cd4ceb2 | 2007-05-08 00:47:45 -0500 | [diff] [blame] | 557 | len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 558 | |
Olof Johansson | 9f05cfe | 2007-05-08 00:47:37 -0500 | [diff] [blame] | 559 | if (len < 256) { |
Olof Johansson | 8dc121a | 2007-10-02 16:26:53 -0500 | [diff] [blame] | 560 | struct sk_buff *new_skb; |
| 561 | |
| 562 | new_skb = netdev_alloc_skb(mac->netdev, |
| 563 | len + LOCAL_SKB_ALIGN); |
Olof Johansson | 9f05cfe | 2007-05-08 00:47:37 -0500 | [diff] [blame] | 564 | if (new_skb) { |
Olof Johansson | 8dc121a | 2007-10-02 16:26:53 -0500 | [diff] [blame] | 565 | skb_reserve(new_skb, LOCAL_SKB_ALIGN); |
Olof Johansson | 7334486 | 2007-08-22 09:12:55 -0500 | [diff] [blame] | 566 | memcpy(new_skb->data, skb->data, len); |
Olof Johansson | 9f05cfe | 2007-05-08 00:47:37 -0500 | [diff] [blame] | 567 | /* save the skb in buffer_info as good */ |
| 568 | skb = new_skb; |
| 569 | } |
| 570 | /* else just continue with the old one */ |
| 571 | } else |
| 572 | info->skb = NULL; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 573 | |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 574 | pci_unmap_single(mac->dma_pdev, dma, len, PCI_DMA_FROMDEVICE); |
| 575 | |
| 576 | info->dma = 0; |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 577 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 578 | skb_put(skb, len); |
| 579 | |
Olof Johansson | 26fcfa9 | 2007-08-22 09:12:59 -0500 | [diff] [blame] | 580 | if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) { |
Olof Johansson | 38bf318 | 2007-08-22 09:13:24 -0500 | [diff] [blame] | 581 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Olof Johansson | cd4ceb2 | 2007-05-08 00:47:45 -0500 | [diff] [blame] | 582 | skb->csum = (macrx & XCT_MACRX_CSUM_M) >> |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 583 | XCT_MACRX_CSUM_S; |
| 584 | } else |
| 585 | skb->ip_summed = CHECKSUM_NONE; |
| 586 | |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 587 | mac->netdev->stats.rx_bytes += len; |
| 588 | mac->netdev->stats.rx_packets++; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 589 | |
Olof Johansson | 26fcfa9 | 2007-08-22 09:12:59 -0500 | [diff] [blame] | 590 | skb->protocol = eth_type_trans(skb, mac->netdev); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 591 | netif_receive_skb(skb); |
| 592 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 593 | RX_RING(mac, n) = 0; |
| 594 | RX_RING(mac, n+1) = 0; |
Olof Johansson | cd4ceb2 | 2007-05-08 00:47:45 -0500 | [diff] [blame] | 595 | |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 596 | /* Need to zero it out since hardware doesn't, since the |
| 597 | * replenish loop uses it to tell when it's done. |
| 598 | */ |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 599 | RX_BUFF(mac, buf_index) = 0; |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 600 | |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 601 | n += 4; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 602 | } |
| 603 | |
Olof Johansson | 9a50beb | 2007-10-02 16:26:30 -0500 | [diff] [blame] | 604 | if (n > RX_RING_SIZE) { |
| 605 | /* Errata 5971 workaround: L2 target of headers */ |
| 606 | write_iob_reg(mac, PAS_IOB_COM_PKTHDRCNT, 0); |
| 607 | n &= (RX_RING_SIZE-1); |
| 608 | } |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 609 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 610 | mac->rx->next_to_clean = n; |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 611 | |
| 612 | /* Increase is in number of 16-byte entries, and since each descriptor |
| 613 | * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with |
| 614 | * count*2. |
| 615 | */ |
| 616 | write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count << 1); |
| 617 | |
| 618 | pasemi_mac_replenish_rx_ring(mac->netdev, count); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 619 | |
| 620 | spin_unlock(&mac->rx->lock); |
| 621 | |
| 622 | return count; |
| 623 | } |
| 624 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 625 | /* Can't make this too large or we blow the kernel stack limits */ |
| 626 | #define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS) |
| 627 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 628 | static int pasemi_mac_clean_tx(struct pasemi_mac *mac) |
| 629 | { |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 630 | int i, j; |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 631 | unsigned int start, descr_count, buf_count, batch_limit; |
| 632 | unsigned int ring_limit; |
Olof Johansson | 02df6cf | 2007-08-22 09:13:03 -0500 | [diff] [blame] | 633 | unsigned int total_count; |
Olof Johansson | ca7e235 | 2007-09-26 16:23:59 -0500 | [diff] [blame] | 634 | unsigned long flags; |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 635 | struct sk_buff *skbs[TX_CLEAN_BATCHSIZE]; |
| 636 | dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1]; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 637 | |
Olof Johansson | 02df6cf | 2007-08-22 09:13:03 -0500 | [diff] [blame] | 638 | total_count = 0; |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 639 | batch_limit = TX_CLEAN_BATCHSIZE; |
Olof Johansson | 02df6cf | 2007-08-22 09:13:03 -0500 | [diff] [blame] | 640 | restart: |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 641 | spin_lock_irqsave(&mac->tx->lock, flags); |
| 642 | |
| 643 | start = mac->tx->next_to_clean; |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 644 | ring_limit = mac->tx->next_to_fill; |
| 645 | |
| 646 | /* Compensate for when fill has wrapped but clean has not */ |
| 647 | if (start > ring_limit) |
| 648 | ring_limit += TX_RING_SIZE; |
Olof Johansson | 02df6cf | 2007-08-22 09:13:03 -0500 | [diff] [blame] | 649 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 650 | buf_count = 0; |
| 651 | descr_count = 0; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 652 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 653 | for (i = start; |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 654 | descr_count < batch_limit && i < ring_limit; |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 655 | i += buf_count) { |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 656 | u64 mactx = TX_RING(mac, i); |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 657 | struct sk_buff *skb; |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 658 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 659 | if ((mactx & XCT_MACTX_E) || |
Olof Johansson | 69c29d8 | 2007-10-02 16:24:51 -0500 | [diff] [blame] | 660 | (*mac->tx_status & PAS_STATUS_ERROR)) |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 661 | pasemi_mac_tx_error(mac, mactx); |
Olof Johansson | 69c29d8 | 2007-10-02 16:24:51 -0500 | [diff] [blame] | 662 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 663 | if (unlikely(mactx & XCT_MACTX_O)) |
Olof Johansson | 02df6cf | 2007-08-22 09:13:03 -0500 | [diff] [blame] | 664 | /* Not yet transmitted */ |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 665 | break; |
| 666 | |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 667 | skb = TX_RING_INFO(mac, i+1).skb; |
| 668 | skbs[descr_count] = skb; |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 669 | |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 670 | buf_count = 2 + skb_shinfo(skb)->nr_frags; |
| 671 | for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++) |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 672 | dmas[descr_count][j] = TX_RING_INFO(mac, i+1+j).dma; |
| 673 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 674 | TX_RING(mac, i) = 0; |
| 675 | TX_RING(mac, i+1) = 0; |
| 676 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 677 | /* Since we always fill with an even number of entries, make |
| 678 | * sure we skip any unused one at the end as well. |
| 679 | */ |
| 680 | if (buf_count & 1) |
| 681 | buf_count++; |
| 682 | descr_count++; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 683 | } |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 684 | mac->tx->next_to_clean = i & (TX_RING_SIZE-1); |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 685 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 686 | spin_unlock_irqrestore(&mac->tx->lock, flags); |
Olof Johansson | 0ce68c7 | 2007-04-28 15:36:40 -0500 | [diff] [blame] | 687 | netif_wake_queue(mac->netdev); |
| 688 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 689 | for (i = 0; i < descr_count; i++) |
| 690 | pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]); |
Olof Johansson | 02df6cf | 2007-08-22 09:13:03 -0500 | [diff] [blame] | 691 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 692 | total_count += descr_count; |
Olof Johansson | 02df6cf | 2007-08-22 09:13:03 -0500 | [diff] [blame] | 693 | |
| 694 | /* If the batch was full, try to clean more */ |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 695 | if (descr_count == batch_limit) |
Olof Johansson | 02df6cf | 2007-08-22 09:13:03 -0500 | [diff] [blame] | 696 | goto restart; |
| 697 | |
| 698 | return total_count; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | |
| 702 | static irqreturn_t pasemi_mac_rx_intr(int irq, void *data) |
| 703 | { |
| 704 | struct net_device *dev = data; |
| 705 | struct pasemi_mac *mac = netdev_priv(dev); |
| 706 | unsigned int reg; |
| 707 | |
Olof Johansson | 6dfa752 | 2007-05-08 00:47:32 -0500 | [diff] [blame] | 708 | if (!(*mac->rx_status & PAS_STATUS_CAUSE_M)) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 709 | return IRQ_NONE; |
| 710 | |
Olof Johansson | 6dfa752 | 2007-05-08 00:47:32 -0500 | [diff] [blame] | 711 | /* Don't reset packet count so it won't fire again but clear |
| 712 | * all others. |
| 713 | */ |
| 714 | |
Olof Johansson | 6dfa752 | 2007-05-08 00:47:32 -0500 | [diff] [blame] | 715 | reg = 0; |
| 716 | if (*mac->rx_status & PAS_STATUS_SOFT) |
| 717 | reg |= PAS_IOB_DMA_RXCH_RESET_SINTC; |
| 718 | if (*mac->rx_status & PAS_STATUS_ERROR) |
| 719 | reg |= PAS_IOB_DMA_RXCH_RESET_DINTC; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 720 | if (*mac->rx_status & PAS_STATUS_TIMER) |
| 721 | reg |= PAS_IOB_DMA_RXCH_RESET_TINTC; |
| 722 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 723 | netif_rx_schedule(dev, &mac->napi); |
Olof Johansson | 6dfa752 | 2007-05-08 00:47:32 -0500 | [diff] [blame] | 724 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 725 | write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 726 | |
| 727 | return IRQ_HANDLED; |
| 728 | } |
| 729 | |
| 730 | static irqreturn_t pasemi_mac_tx_intr(int irq, void *data) |
| 731 | { |
| 732 | struct net_device *dev = data; |
| 733 | struct pasemi_mac *mac = netdev_priv(dev); |
Olof Johansson | 52a9435 | 2007-05-12 18:01:09 -0500 | [diff] [blame] | 734 | unsigned int reg, pcnt; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 735 | |
Olof Johansson | 6dfa752 | 2007-05-08 00:47:32 -0500 | [diff] [blame] | 736 | if (!(*mac->tx_status & PAS_STATUS_CAUSE_M)) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 737 | return IRQ_NONE; |
| 738 | |
| 739 | pasemi_mac_clean_tx(mac); |
| 740 | |
Olof Johansson | 52a9435 | 2007-05-12 18:01:09 -0500 | [diff] [blame] | 741 | pcnt = *mac->tx_status & PAS_STATUS_PCNT_M; |
| 742 | |
| 743 | reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC; |
Olof Johansson | 6dfa752 | 2007-05-08 00:47:32 -0500 | [diff] [blame] | 744 | |
| 745 | if (*mac->tx_status & PAS_STATUS_SOFT) |
| 746 | reg |= PAS_IOB_DMA_TXCH_RESET_SINTC; |
| 747 | if (*mac->tx_status & PAS_STATUS_ERROR) |
| 748 | reg |= PAS_IOB_DMA_TXCH_RESET_DINTC; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 749 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 750 | write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 751 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 752 | return IRQ_HANDLED; |
| 753 | } |
| 754 | |
Olof Johansson | bb6e959 | 2007-05-08 00:47:54 -0500 | [diff] [blame] | 755 | static void pasemi_adjust_link(struct net_device *dev) |
| 756 | { |
| 757 | struct pasemi_mac *mac = netdev_priv(dev); |
| 758 | int msg; |
| 759 | unsigned int flags; |
| 760 | unsigned int new_flags; |
| 761 | |
| 762 | if (!mac->phydev->link) { |
| 763 | /* If no link, MAC speed settings don't matter. Just report |
| 764 | * link down and return. |
| 765 | */ |
| 766 | if (mac->link && netif_msg_link(mac)) |
| 767 | printk(KERN_INFO "%s: Link is down.\n", dev->name); |
| 768 | |
| 769 | netif_carrier_off(dev); |
| 770 | mac->link = 0; |
| 771 | |
| 772 | return; |
| 773 | } else |
| 774 | netif_carrier_on(dev); |
| 775 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 776 | flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG); |
Olof Johansson | bb6e959 | 2007-05-08 00:47:54 -0500 | [diff] [blame] | 777 | new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M | |
| 778 | PAS_MAC_CFG_PCFG_TSR_M); |
| 779 | |
| 780 | if (!mac->phydev->duplex) |
| 781 | new_flags |= PAS_MAC_CFG_PCFG_HD; |
| 782 | |
| 783 | switch (mac->phydev->speed) { |
| 784 | case 1000: |
| 785 | new_flags |= PAS_MAC_CFG_PCFG_SPD_1G | |
| 786 | PAS_MAC_CFG_PCFG_TSR_1G; |
| 787 | break; |
| 788 | case 100: |
| 789 | new_flags |= PAS_MAC_CFG_PCFG_SPD_100M | |
| 790 | PAS_MAC_CFG_PCFG_TSR_100M; |
| 791 | break; |
| 792 | case 10: |
| 793 | new_flags |= PAS_MAC_CFG_PCFG_SPD_10M | |
| 794 | PAS_MAC_CFG_PCFG_TSR_10M; |
| 795 | break; |
| 796 | default: |
| 797 | printk("Unsupported speed %d\n", mac->phydev->speed); |
| 798 | } |
| 799 | |
| 800 | /* Print on link or speed/duplex change */ |
| 801 | msg = mac->link != mac->phydev->link || flags != new_flags; |
| 802 | |
| 803 | mac->duplex = mac->phydev->duplex; |
| 804 | mac->speed = mac->phydev->speed; |
| 805 | mac->link = mac->phydev->link; |
| 806 | |
| 807 | if (new_flags != flags) |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 808 | write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags); |
Olof Johansson | bb6e959 | 2007-05-08 00:47:54 -0500 | [diff] [blame] | 809 | |
| 810 | if (msg && netif_msg_link(mac)) |
| 811 | printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n", |
| 812 | dev->name, mac->speed, mac->duplex ? "full" : "half"); |
| 813 | } |
| 814 | |
| 815 | static int pasemi_mac_phy_init(struct net_device *dev) |
| 816 | { |
| 817 | struct pasemi_mac *mac = netdev_priv(dev); |
| 818 | struct device_node *dn, *phy_dn; |
| 819 | struct phy_device *phydev; |
| 820 | unsigned int phy_id; |
| 821 | const phandle *ph; |
| 822 | const unsigned int *prop; |
| 823 | struct resource r; |
| 824 | int ret; |
| 825 | |
| 826 | dn = pci_device_to_OF_node(mac->pdev); |
Linus Torvalds | 9028780 | 2007-05-08 11:57:17 -0700 | [diff] [blame] | 827 | ph = of_get_property(dn, "phy-handle", NULL); |
Olof Johansson | bb6e959 | 2007-05-08 00:47:54 -0500 | [diff] [blame] | 828 | if (!ph) |
| 829 | return -ENODEV; |
| 830 | phy_dn = of_find_node_by_phandle(*ph); |
| 831 | |
Linus Torvalds | 9028780 | 2007-05-08 11:57:17 -0700 | [diff] [blame] | 832 | prop = of_get_property(phy_dn, "reg", NULL); |
Olof Johansson | bb6e959 | 2007-05-08 00:47:54 -0500 | [diff] [blame] | 833 | ret = of_address_to_resource(phy_dn->parent, 0, &r); |
| 834 | if (ret) |
| 835 | goto err; |
| 836 | |
| 837 | phy_id = *prop; |
| 838 | snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id); |
| 839 | |
| 840 | of_node_put(phy_dn); |
| 841 | |
| 842 | mac->link = 0; |
| 843 | mac->speed = 0; |
| 844 | mac->duplex = -1; |
| 845 | |
| 846 | phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII); |
| 847 | |
| 848 | if (IS_ERR(phydev)) { |
| 849 | printk(KERN_ERR "%s: Could not attach to phy\n", dev->name); |
| 850 | return PTR_ERR(phydev); |
| 851 | } |
| 852 | |
| 853 | mac->phydev = phydev; |
| 854 | |
| 855 | return 0; |
| 856 | |
| 857 | err: |
| 858 | of_node_put(phy_dn); |
| 859 | return -ENODEV; |
| 860 | } |
| 861 | |
| 862 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 863 | static int pasemi_mac_open(struct net_device *dev) |
| 864 | { |
| 865 | struct pasemi_mac *mac = netdev_priv(dev); |
Olof Johansson | 771f740 | 2007-05-08 00:47:21 -0500 | [diff] [blame] | 866 | int base_irq; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 867 | unsigned int flags; |
| 868 | int ret; |
| 869 | |
| 870 | /* enable rx section */ |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 871 | write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 872 | |
| 873 | /* enable tx section */ |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 874 | write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 875 | |
| 876 | flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) | |
| 877 | PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) | |
| 878 | PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12); |
| 879 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 880 | write_mac_reg(mac, PAS_MAC_CFG_TXP, flags); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 881 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 882 | write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch), |
| 883 | PAS_IOB_DMA_RXCH_CFG_CNTTH(0)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 884 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 885 | write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch), |
Olof Johansson | 02df6cf | 2007-08-22 09:13:03 -0500 | [diff] [blame] | 886 | PAS_IOB_DMA_TXCH_CFG_CNTTH(128)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 887 | |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 888 | /* Clear out any residual packet count state from firmware */ |
| 889 | pasemi_mac_restart_rx_intr(mac); |
| 890 | pasemi_mac_restart_tx_intr(mac); |
| 891 | |
Olof Johansson | 6dfa752 | 2007-05-08 00:47:32 -0500 | [diff] [blame] | 892 | /* 0xffffff is max value, about 16ms */ |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 893 | write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG, |
| 894 | PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 895 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 896 | ret = pasemi_mac_setup_rx_resources(dev); |
| 897 | if (ret) |
| 898 | goto out_rx_resources; |
| 899 | |
| 900 | ret = pasemi_mac_setup_tx_resources(dev); |
| 901 | if (ret) |
| 902 | goto out_tx_resources; |
| 903 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 904 | write_mac_reg(mac, PAS_MAC_IPC_CHNL, |
| 905 | PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) | |
| 906 | PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 907 | |
| 908 | /* enable rx if */ |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 909 | write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 910 | PAS_DMA_RXINT_RCMDSTA_EN | |
| 911 | PAS_DMA_RXINT_RCMDSTA_DROPS_M | |
| 912 | PAS_DMA_RXINT_RCMDSTA_BP | |
| 913 | PAS_DMA_RXINT_RCMDSTA_OO | |
| 914 | PAS_DMA_RXINT_RCMDSTA_BT); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 915 | |
| 916 | /* enable rx channel */ |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 917 | write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), |
| 918 | PAS_DMA_RXCHAN_CCMDSTA_EN | |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 919 | PAS_DMA_RXCHAN_CCMDSTA_DU | |
| 920 | PAS_DMA_RXCHAN_CCMDSTA_OD | |
| 921 | PAS_DMA_RXCHAN_CCMDSTA_FD | |
| 922 | PAS_DMA_RXCHAN_CCMDSTA_DT); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 923 | |
| 924 | /* enable tx channel */ |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 925 | write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 926 | PAS_DMA_TXCHAN_TCMDSTA_EN | |
| 927 | PAS_DMA_TXCHAN_TCMDSTA_SZ | |
| 928 | PAS_DMA_TXCHAN_TCMDSTA_DB | |
| 929 | PAS_DMA_TXCHAN_TCMDSTA_DE | |
| 930 | PAS_DMA_TXCHAN_TCMDSTA_DA); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 931 | |
Olof Johansson | 928773c | 2007-09-26 16:25:06 -0500 | [diff] [blame] | 932 | pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 933 | |
Olof Johansson | b5254ee | 2007-10-02 16:27:57 -0500 | [diff] [blame^] | 934 | write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), RX_RING_SIZE>>1); |
| 935 | |
Olof Johansson | 3603376 | 2007-09-26 16:24:42 -0500 | [diff] [blame] | 936 | flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE | |
| 937 | PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE; |
| 938 | |
| 939 | if (mac->type == MAC_TYPE_GMAC) |
| 940 | flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G; |
| 941 | else |
| 942 | flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G; |
| 943 | |
| 944 | /* Enable interface in MAC */ |
| 945 | write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags); |
| 946 | |
Olof Johansson | bb6e959 | 2007-05-08 00:47:54 -0500 | [diff] [blame] | 947 | ret = pasemi_mac_phy_init(dev); |
| 948 | /* Some configs don't have PHYs (XAUI etc), so don't complain about |
| 949 | * failed init due to -ENODEV. |
| 950 | */ |
| 951 | if (ret && ret != -ENODEV) |
| 952 | dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret); |
| 953 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 954 | netif_start_queue(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 955 | napi_enable(&mac->napi); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 956 | |
Olof Johansson | 771f740 | 2007-05-08 00:47:21 -0500 | [diff] [blame] | 957 | /* Interrupts are a bit different for our DMA controller: While |
| 958 | * it's got one a regular PCI device header, the interrupt there |
| 959 | * is really the base of the range it's using. Each tx and rx |
| 960 | * channel has it's own interrupt source. |
| 961 | */ |
| 962 | |
| 963 | base_irq = virq_to_hw(mac->dma_pdev->irq); |
| 964 | |
| 965 | mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch); |
| 966 | mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch); |
| 967 | |
| 968 | ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED, |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 969 | mac->tx->irq_name, dev); |
| 970 | if (ret) { |
| 971 | dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n", |
Olof Johansson | 771f740 | 2007-05-08 00:47:21 -0500 | [diff] [blame] | 972 | base_irq + mac->dma_txch, ret); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 973 | goto out_tx_int; |
| 974 | } |
| 975 | |
Olof Johansson | 771f740 | 2007-05-08 00:47:21 -0500 | [diff] [blame] | 976 | ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED, |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 977 | mac->rx->irq_name, dev); |
| 978 | if (ret) { |
| 979 | dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n", |
Olof Johansson | 771f740 | 2007-05-08 00:47:21 -0500 | [diff] [blame] | 980 | base_irq + 20 + mac->dma_rxch, ret); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 981 | goto out_rx_int; |
| 982 | } |
| 983 | |
Olof Johansson | bb6e959 | 2007-05-08 00:47:54 -0500 | [diff] [blame] | 984 | if (mac->phydev) |
| 985 | phy_start(mac->phydev); |
| 986 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 987 | return 0; |
| 988 | |
| 989 | out_rx_int: |
Olof Johansson | 771f740 | 2007-05-08 00:47:21 -0500 | [diff] [blame] | 990 | free_irq(mac->tx_irq, dev); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 991 | out_tx_int: |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 992 | napi_disable(&mac->napi); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 993 | netif_stop_queue(dev); |
| 994 | pasemi_mac_free_tx_resources(dev); |
| 995 | out_tx_resources: |
| 996 | pasemi_mac_free_rx_resources(dev); |
| 997 | out_rx_resources: |
| 998 | |
| 999 | return ret; |
| 1000 | } |
| 1001 | |
| 1002 | #define MAX_RETRIES 5000 |
| 1003 | |
| 1004 | static int pasemi_mac_close(struct net_device *dev) |
| 1005 | { |
| 1006 | struct pasemi_mac *mac = netdev_priv(dev); |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 1007 | unsigned int sta; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1008 | int retries; |
| 1009 | |
Olof Johansson | bb6e959 | 2007-05-08 00:47:54 -0500 | [diff] [blame] | 1010 | if (mac->phydev) { |
| 1011 | phy_stop(mac->phydev); |
| 1012 | phy_disconnect(mac->phydev); |
| 1013 | } |
| 1014 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1015 | netif_stop_queue(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1016 | napi_disable(&mac->napi); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1017 | |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 1018 | sta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if)); |
| 1019 | if (sta & (PAS_DMA_RXINT_RCMDSTA_BP | |
| 1020 | PAS_DMA_RXINT_RCMDSTA_OO | |
| 1021 | PAS_DMA_RXINT_RCMDSTA_BT)) |
| 1022 | printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta); |
| 1023 | |
| 1024 | sta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch)); |
| 1025 | if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU | |
| 1026 | PAS_DMA_RXCHAN_CCMDSTA_OD | |
| 1027 | PAS_DMA_RXCHAN_CCMDSTA_FD | |
| 1028 | PAS_DMA_RXCHAN_CCMDSTA_DT)) |
| 1029 | printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta); |
| 1030 | |
| 1031 | sta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch)); |
| 1032 | if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | |
| 1033 | PAS_DMA_TXCHAN_TCMDSTA_DB | |
| 1034 | PAS_DMA_TXCHAN_TCMDSTA_DE | |
| 1035 | PAS_DMA_TXCHAN_TCMDSTA_DA)) |
| 1036 | printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta); |
| 1037 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1038 | /* Clean out any pending buffers */ |
| 1039 | pasemi_mac_clean_tx(mac); |
| 1040 | pasemi_mac_clean_rx(mac, RX_RING_SIZE); |
| 1041 | |
| 1042 | /* Disable interface */ |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 1043 | write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST); |
| 1044 | write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST); |
| 1045 | write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1046 | |
| 1047 | for (retries = 0; retries < MAX_RETRIES; retries++) { |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 1048 | sta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch)); |
| 1049 | if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1050 | break; |
| 1051 | cond_resched(); |
| 1052 | } |
| 1053 | |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 1054 | if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1055 | dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n"); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1056 | |
| 1057 | for (retries = 0; retries < MAX_RETRIES; retries++) { |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 1058 | sta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch)); |
| 1059 | if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1060 | break; |
| 1061 | cond_resched(); |
| 1062 | } |
| 1063 | |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 1064 | if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1065 | dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n"); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1066 | |
| 1067 | for (retries = 0; retries < MAX_RETRIES; retries++) { |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 1068 | sta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if)); |
| 1069 | if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT)) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1070 | break; |
| 1071 | cond_resched(); |
| 1072 | } |
| 1073 | |
Olof Johansson | 9e81d33 | 2007-10-02 16:27:39 -0500 | [diff] [blame] | 1074 | if (sta & PAS_DMA_RXINT_RCMDSTA_ACT) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1075 | dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n"); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1076 | |
| 1077 | /* Then, disable the channel. This must be done separately from |
| 1078 | * stopping, since you can't disable when active. |
| 1079 | */ |
| 1080 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 1081 | write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0); |
| 1082 | write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0); |
| 1083 | write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1084 | |
Olof Johansson | 771f740 | 2007-05-08 00:47:21 -0500 | [diff] [blame] | 1085 | free_irq(mac->tx_irq, dev); |
| 1086 | free_irq(mac->rx_irq, dev); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1087 | |
| 1088 | /* Free resources */ |
| 1089 | pasemi_mac_free_rx_resources(dev); |
| 1090 | pasemi_mac_free_tx_resources(dev); |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev) |
| 1096 | { |
| 1097 | struct pasemi_mac *mac = netdev_priv(dev); |
| 1098 | struct pasemi_mac_txring *txring; |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 1099 | u64 dflags, mactx; |
| 1100 | dma_addr_t map[MAX_SKB_FRAGS+1]; |
| 1101 | unsigned int map_size[MAX_SKB_FRAGS+1]; |
Olof Johansson | ca7e235 | 2007-09-26 16:23:59 -0500 | [diff] [blame] | 1102 | unsigned long flags; |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 1103 | int i, nfrags; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1104 | |
| 1105 | dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD; |
| 1106 | |
| 1107 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 1108 | const unsigned char *nh = skb_network_header(skb); |
| 1109 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1110 | switch (ip_hdr(skb)->protocol) { |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1111 | case IPPROTO_TCP: |
| 1112 | dflags |= XCT_MACTX_CSUM_TCP; |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 1113 | dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 1114 | dflags |= XCT_MACTX_IPO(nh - skb->data); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1115 | break; |
| 1116 | case IPPROTO_UDP: |
| 1117 | dflags |= XCT_MACTX_CSUM_UDP; |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 1118 | dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 1119 | dflags |= XCT_MACTX_IPO(nh - skb->data); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1120 | break; |
| 1121 | } |
| 1122 | } |
| 1123 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 1124 | nfrags = skb_shinfo(skb)->nr_frags; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1125 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 1126 | map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb), |
| 1127 | PCI_DMA_TODEVICE); |
| 1128 | map_size[0] = skb_headlen(skb); |
| 1129 | if (dma_mapping_error(map[0])) |
| 1130 | goto out_err_nolock; |
| 1131 | |
| 1132 | for (i = 0; i < nfrags; i++) { |
| 1133 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1134 | |
| 1135 | map[i+1] = pci_map_page(mac->dma_pdev, frag->page, |
| 1136 | frag->page_offset, frag->size, |
| 1137 | PCI_DMA_TODEVICE); |
| 1138 | map_size[i+1] = frag->size; |
| 1139 | if (dma_mapping_error(map[i+1])) { |
| 1140 | nfrags = i; |
| 1141 | goto out_err_nolock; |
| 1142 | } |
| 1143 | } |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1144 | |
Olof Johansson | 26fcfa9 | 2007-08-22 09:12:59 -0500 | [diff] [blame] | 1145 | mactx = dflags | XCT_MACTX_LLEN(skb->len); |
Olof Johansson | 26fcfa9 | 2007-08-22 09:12:59 -0500 | [diff] [blame] | 1146 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1147 | txring = mac->tx; |
| 1148 | |
| 1149 | spin_lock_irqsave(&txring->lock, flags); |
| 1150 | |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 1151 | /* Avoid stepping on the same cache line that the DMA controller |
| 1152 | * is currently about to send, so leave at least 8 words available. |
| 1153 | * Total free space needed is mactx + fragments + 8 |
| 1154 | */ |
| 1155 | if (RING_AVAIL(txring) < nfrags + 10) { |
| 1156 | /* no room -- stop the queue and wait for tx intr */ |
| 1157 | netif_stop_queue(dev); |
| 1158 | goto out_err; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1159 | } |
| 1160 | |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 1161 | TX_RING(mac, txring->next_to_fill) = mactx; |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 1162 | txring->next_to_fill++; |
| 1163 | TX_RING_INFO(mac, txring->next_to_fill).skb = skb; |
| 1164 | for (i = 0; i <= nfrags; i++) { |
| 1165 | TX_RING(mac, txring->next_to_fill+i) = |
| 1166 | XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]); |
| 1167 | TX_RING_INFO(mac, txring->next_to_fill+i).dma = map[i]; |
| 1168 | } |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1169 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 1170 | /* We have to add an even number of 8-byte entries to the ring |
| 1171 | * even if the last one is unused. That means always an odd number |
| 1172 | * of pointers + one mactx descriptor. |
| 1173 | */ |
| 1174 | if (nfrags & 1) |
| 1175 | nfrags++; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1176 | |
Olof Johansson | ad5da10 | 2007-10-02 16:27:15 -0500 | [diff] [blame] | 1177 | txring->next_to_fill = (txring->next_to_fill + nfrags + 1) & |
| 1178 | (TX_RING_SIZE-1); |
Olof Johansson | fc9e4d2 | 2007-10-02 16:25:53 -0500 | [diff] [blame] | 1179 | |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1180 | dev->stats.tx_packets++; |
| 1181 | dev->stats.tx_bytes += skb->len; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1182 | |
| 1183 | spin_unlock_irqrestore(&txring->lock, flags); |
| 1184 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 1185 | write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), (nfrags+2) >> 1); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1186 | |
| 1187 | return NETDEV_TX_OK; |
| 1188 | |
| 1189 | out_err: |
| 1190 | spin_unlock_irqrestore(&txring->lock, flags); |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 1191 | out_err_nolock: |
| 1192 | while (nfrags--) |
| 1193 | pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags], |
| 1194 | PCI_DMA_TODEVICE); |
| 1195 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1196 | return NETDEV_TX_BUSY; |
| 1197 | } |
| 1198 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1199 | static void pasemi_mac_set_rx_mode(struct net_device *dev) |
| 1200 | { |
| 1201 | struct pasemi_mac *mac = netdev_priv(dev); |
| 1202 | unsigned int flags; |
| 1203 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 1204 | flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1205 | |
| 1206 | /* Set promiscuous */ |
| 1207 | if (dev->flags & IFF_PROMISC) |
| 1208 | flags |= PAS_MAC_CFG_PCFG_PR; |
| 1209 | else |
| 1210 | flags &= ~PAS_MAC_CFG_PCFG_PR; |
| 1211 | |
Olof Johansson | a85b942 | 2007-09-15 13:40:59 -0700 | [diff] [blame] | 1212 | write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1216 | static int pasemi_mac_poll(struct napi_struct *napi, int budget) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1217 | { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1218 | struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi); |
| 1219 | struct net_device *dev = mac->netdev; |
| 1220 | int pkts; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1221 | |
Olof Johansson | 829185e | 2007-09-15 13:53:19 -0700 | [diff] [blame] | 1222 | pasemi_mac_clean_tx(mac); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1223 | pkts = pasemi_mac_clean_rx(mac, budget); |
| 1224 | if (pkts < budget) { |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1225 | /* all done, no more packets present */ |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1226 | netif_rx_complete(dev, napi); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1227 | |
Olof Johansson | 1b0335ea | 2007-05-08 00:47:26 -0500 | [diff] [blame] | 1228 | pasemi_mac_restart_rx_intr(mac); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1229 | } |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1230 | return pkts; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1231 | } |
| 1232 | |
Olof Johansson | b6e05a1 | 2007-09-15 13:44:07 -0700 | [diff] [blame] | 1233 | static void __iomem * __devinit map_onedev(struct pci_dev *p, int index) |
| 1234 | { |
| 1235 | struct device_node *dn; |
| 1236 | void __iomem *ret; |
| 1237 | |
| 1238 | dn = pci_device_to_OF_node(p); |
| 1239 | if (!dn) |
| 1240 | goto fallback; |
| 1241 | |
| 1242 | ret = of_iomap(dn, index); |
| 1243 | if (!ret) |
| 1244 | goto fallback; |
| 1245 | |
| 1246 | return ret; |
| 1247 | fallback: |
| 1248 | /* This is hardcoded and ugly, but we have some firmware versions |
| 1249 | * that don't provide the register space in the device tree. Luckily |
| 1250 | * they are at well-known locations so we can just do the math here. |
| 1251 | */ |
| 1252 | return ioremap(0xe0000000 + (p->devfn << 12), 0x2000); |
| 1253 | } |
| 1254 | |
| 1255 | static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac) |
| 1256 | { |
| 1257 | struct resource res; |
| 1258 | struct device_node *dn; |
| 1259 | int err; |
| 1260 | |
| 1261 | mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL); |
| 1262 | if (!mac->dma_pdev) { |
| 1263 | dev_err(&mac->pdev->dev, "Can't find DMA Controller\n"); |
| 1264 | return -ENODEV; |
| 1265 | } |
| 1266 | |
| 1267 | mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL); |
| 1268 | if (!mac->iob_pdev) { |
| 1269 | dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n"); |
| 1270 | return -ENODEV; |
| 1271 | } |
| 1272 | |
| 1273 | mac->regs = map_onedev(mac->pdev, 0); |
| 1274 | mac->dma_regs = map_onedev(mac->dma_pdev, 0); |
| 1275 | mac->iob_regs = map_onedev(mac->iob_pdev, 0); |
| 1276 | |
| 1277 | if (!mac->regs || !mac->dma_regs || !mac->iob_regs) { |
| 1278 | dev_err(&mac->pdev->dev, "Can't map registers\n"); |
| 1279 | return -ENODEV; |
| 1280 | } |
| 1281 | |
| 1282 | /* The dma status structure is located in the I/O bridge, and |
| 1283 | * is cache coherent. |
| 1284 | */ |
| 1285 | if (!dma_status) { |
| 1286 | dn = pci_device_to_OF_node(mac->iob_pdev); |
| 1287 | if (dn) |
| 1288 | err = of_address_to_resource(dn, 1, &res); |
| 1289 | if (!dn || err) { |
| 1290 | /* Fallback for old firmware */ |
| 1291 | res.start = 0xfd800000; |
| 1292 | res.end = res.start + 0x1000; |
| 1293 | } |
| 1294 | dma_status = __ioremap(res.start, res.end-res.start, 0); |
| 1295 | } |
| 1296 | |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1300 | static int __devinit |
| 1301 | pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 1302 | { |
| 1303 | static int index = 0; |
| 1304 | struct net_device *dev; |
| 1305 | struct pasemi_mac *mac; |
| 1306 | int err; |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 1307 | DECLARE_MAC_BUF(mac_buf); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1308 | |
| 1309 | err = pci_enable_device(pdev); |
| 1310 | if (err) |
| 1311 | return err; |
| 1312 | |
| 1313 | dev = alloc_etherdev(sizeof(struct pasemi_mac)); |
| 1314 | if (dev == NULL) { |
| 1315 | dev_err(&pdev->dev, |
| 1316 | "pasemi_mac: Could not allocate ethernet device.\n"); |
| 1317 | err = -ENOMEM; |
| 1318 | goto out_disable_device; |
| 1319 | } |
| 1320 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1321 | pci_set_drvdata(pdev, dev); |
| 1322 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 1323 | |
| 1324 | mac = netdev_priv(dev); |
| 1325 | |
| 1326 | mac->pdev = pdev; |
| 1327 | mac->netdev = dev; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1328 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1329 | netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64); |
| 1330 | |
Olof Johansson | ad3c20d | 2007-10-02 16:26:13 -0500 | [diff] [blame] | 1331 | dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX | NETIF_F_SG; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1332 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1333 | /* These should come out of the device tree eventually */ |
| 1334 | mac->dma_txch = index; |
| 1335 | mac->dma_rxch = index; |
| 1336 | |
| 1337 | /* We probe GMAC before XAUI, but the DMA interfaces are |
| 1338 | * in XAUI, GMAC order. |
| 1339 | */ |
| 1340 | if (index < 4) |
| 1341 | mac->dma_if = index + 2; |
| 1342 | else |
| 1343 | mac->dma_if = index - 4; |
| 1344 | index++; |
| 1345 | |
| 1346 | switch (pdev->device) { |
| 1347 | case 0xa005: |
| 1348 | mac->type = MAC_TYPE_GMAC; |
| 1349 | break; |
| 1350 | case 0xa006: |
| 1351 | mac->type = MAC_TYPE_XAUI; |
| 1352 | break; |
| 1353 | default: |
| 1354 | err = -ENODEV; |
| 1355 | goto out; |
| 1356 | } |
| 1357 | |
| 1358 | /* get mac addr from device tree */ |
| 1359 | if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) { |
| 1360 | err = -ENODEV; |
| 1361 | goto out; |
| 1362 | } |
| 1363 | memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr)); |
| 1364 | |
| 1365 | dev->open = pasemi_mac_open; |
| 1366 | dev->stop = pasemi_mac_close; |
| 1367 | dev->hard_start_xmit = pasemi_mac_start_tx; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1368 | dev->set_multicast_list = pasemi_mac_set_rx_mode; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1369 | |
Olof Johansson | b6e05a1 | 2007-09-15 13:44:07 -0700 | [diff] [blame] | 1370 | err = pasemi_mac_map_regs(mac); |
| 1371 | if (err) |
| 1372 | goto out; |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1373 | |
| 1374 | mac->rx_status = &dma_status->rx_sta[mac->dma_rxch]; |
| 1375 | mac->tx_status = &dma_status->tx_sta[mac->dma_txch]; |
| 1376 | |
Olof Johansson | ceb5136 | 2007-05-08 00:47:49 -0500 | [diff] [blame] | 1377 | mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE); |
| 1378 | |
Olof Johansson | bb6e959 | 2007-05-08 00:47:54 -0500 | [diff] [blame] | 1379 | /* Enable most messages by default */ |
| 1380 | mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1; |
| 1381 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1382 | err = register_netdev(dev); |
| 1383 | |
| 1384 | if (err) { |
| 1385 | dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n", |
| 1386 | err); |
| 1387 | goto out; |
Olof Johansson | 69c29d8 | 2007-10-02 16:24:51 -0500 | [diff] [blame] | 1388 | } else if netif_msg_probe(mac) |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1389 | printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, " |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 1390 | "hw addr %s\n", |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1391 | dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI", |
| 1392 | mac->dma_if, mac->dma_txch, mac->dma_rxch, |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 1393 | print_mac(mac_buf, dev->dev_addr)); |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1394 | |
| 1395 | return err; |
| 1396 | |
| 1397 | out: |
Olof Johansson | b6e05a1 | 2007-09-15 13:44:07 -0700 | [diff] [blame] | 1398 | if (mac->iob_pdev) |
| 1399 | pci_dev_put(mac->iob_pdev); |
| 1400 | if (mac->dma_pdev) |
| 1401 | pci_dev_put(mac->dma_pdev); |
| 1402 | if (mac->dma_regs) |
| 1403 | iounmap(mac->dma_regs); |
| 1404 | if (mac->iob_regs) |
| 1405 | iounmap(mac->iob_regs); |
| 1406 | if (mac->regs) |
| 1407 | iounmap(mac->regs); |
| 1408 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1409 | free_netdev(dev); |
| 1410 | out_disable_device: |
| 1411 | pci_disable_device(pdev); |
| 1412 | return err; |
| 1413 | |
| 1414 | } |
| 1415 | |
| 1416 | static void __devexit pasemi_mac_remove(struct pci_dev *pdev) |
| 1417 | { |
| 1418 | struct net_device *netdev = pci_get_drvdata(pdev); |
| 1419 | struct pasemi_mac *mac; |
| 1420 | |
| 1421 | if (!netdev) |
| 1422 | return; |
| 1423 | |
| 1424 | mac = netdev_priv(netdev); |
| 1425 | |
| 1426 | unregister_netdev(netdev); |
| 1427 | |
| 1428 | pci_disable_device(pdev); |
| 1429 | pci_dev_put(mac->dma_pdev); |
| 1430 | pci_dev_put(mac->iob_pdev); |
| 1431 | |
Olof Johansson | b6e05a1 | 2007-09-15 13:44:07 -0700 | [diff] [blame] | 1432 | iounmap(mac->regs); |
| 1433 | iounmap(mac->dma_regs); |
| 1434 | iounmap(mac->iob_regs); |
| 1435 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1436 | pci_set_drvdata(pdev, NULL); |
| 1437 | free_netdev(netdev); |
| 1438 | } |
| 1439 | |
| 1440 | static struct pci_device_id pasemi_mac_pci_tbl[] = { |
| 1441 | { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) }, |
| 1442 | { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) }, |
olof@lixom.net | fd17825 | 2007-05-12 14:57:36 -0500 | [diff] [blame] | 1443 | { }, |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1444 | }; |
| 1445 | |
| 1446 | MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl); |
| 1447 | |
| 1448 | static struct pci_driver pasemi_mac_driver = { |
| 1449 | .name = "pasemi_mac", |
| 1450 | .id_table = pasemi_mac_pci_tbl, |
| 1451 | .probe = pasemi_mac_probe, |
| 1452 | .remove = __devexit_p(pasemi_mac_remove), |
| 1453 | }; |
| 1454 | |
| 1455 | static void __exit pasemi_mac_cleanup_module(void) |
| 1456 | { |
| 1457 | pci_unregister_driver(&pasemi_mac_driver); |
| 1458 | __iounmap(dma_status); |
| 1459 | dma_status = NULL; |
| 1460 | } |
| 1461 | |
| 1462 | int pasemi_mac_init_module(void) |
| 1463 | { |
| 1464 | return pci_register_driver(&pasemi_mac_driver); |
| 1465 | } |
| 1466 | |
Olof Johansson | f5cd787 | 2007-01-31 21:43:54 -0600 | [diff] [blame] | 1467 | module_init(pasemi_mac_init_module); |
| 1468 | module_exit(pasemi_mac_cleanup_module); |