blob: 31ad2b9093a7cf017d4e699798ac11e1f9164685 [file] [log] [blame]
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001/*
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 Johansson771f7402007-05-08 00:47:21 -050036#include <asm/irq.h>
37
Olof Johanssonf5cd7872007-01-31 21:43:54 -060038#include "pasemi_mac.h"
39
Olof Johansson8dc121a2007-10-02 16:26:53 -050040/* 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 Johanssonf5cd7872007-01-31 21:43:54 -060046
47/* TODO list
48 *
Olof Johanssonf5cd7872007-01-31 21:43:54 -060049 * - Multicast support
50 * - Large MTU support
Olof Johansson7ddeae22007-10-02 16:27:28 -050051 * - SW LRO
52 * - Multiqueue RX/TX
Olof Johanssonf5cd7872007-01-31 21:43:54 -060053 */
54
55
56/* Must be a power of two */
Olof Johanssonad5da102007-10-02 16:27:15 -050057#define RX_RING_SIZE 4096
58#define TX_RING_SIZE 4096
Olof Johanssonf5cd7872007-01-31 21:43:54 -060059
Olof Johanssonceb51362007-05-08 00:47:49 -050060#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 Johanssonfc9e4d22007-10-02 16:25:53 -050070#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 Johanssonf5cd7872007-01-31 21:43:54 -060074#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
75
Olof Johansson021fa222007-08-22 09:13:11 -050076#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 Johanssonf5cd7872007-01-31 21:43:54 -060080#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
81
Olof Johanssonceb51362007-05-08 00:47:49 -050082MODULE_LICENSE("GPL");
83MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
84MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
85
86static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
87module_param(debug, int, 0);
88MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060089
90static struct pasdma_status *dma_status;
91
Olof Johanssona85b9422007-09-15 13:40:59 -070092static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
93 unsigned int val)
94{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070095 out_le32(mac->iob_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -070096}
97
98static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
99{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700100 return in_le32(mac->regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700101}
102
103static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
104 unsigned int val)
105{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700106 out_le32(mac->regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700107}
108
109static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
110{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700111 return in_le32(mac->dma_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700112}
113
114static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
115 unsigned int val)
116{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700117 out_le32(mac->dma_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700118}
119
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600120static 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.net1af7f052007-05-12 14:57:46 -0500124 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600125 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.net1af7f052007-05-12 14:57:46 -0500134 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500135
olof@lixom.net1af7f052007-05-12 14:57:46 -0500136 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 Johanssona5fd22e2007-05-08 00:48:02 -0500145 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700146 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500147
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600148 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.net1af7f052007-05-12 14:57:46 -0500154
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600155 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.net1af7f052007-05-12 14:57:46 -0500162 memcpy(mac->mac_addr, addr, 6);
163
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600164 return 0;
165}
166
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500167static 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 Johanssonf5cd7872007-01-31 21:43:54 -0600191static 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 Johansson021fa222007-08-22 09:13:11 -0500204 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500205 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600206 RX_RING_SIZE, GFP_KERNEL);
207
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500208 if (!ring->ring_info)
209 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600210
211 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500212 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
213 RX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600214 &ring->dma, GFP_KERNEL);
215
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500216 if (!ring->ring)
217 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600218
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500219 memset(ring->ring, 0, RX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600220
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 Johanssona85b9422007-09-15 13:40:59 -0700229 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600230
Olof Johanssona85b9422007-09-15 13:40:59 -0700231 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
232 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500233 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600234
Olof Johanssona85b9422007-09-15 13:40:59 -0700235 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id),
Olof Johanssonc0efd522007-08-22 09:12:52 -0500236 PAS_DMA_RXCHAN_CFG_HBU(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600237
Olof Johanssona85b9422007-09-15 13:40:59 -0700238 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
239 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600240
Olof Johanssona85b9422007-09-15 13:40:59 -0700241 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 Johanssonf5cd7872007-01-31 21:43:54 -0600244
Olof Johanssonc0efd522007-08-22 09:12:52 -0500245 write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if),
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500246 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 Johanssonc0efd522007-08-22 09:12:52 -0500249
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600250 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
259out_buffers:
260 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500261 RX_RING_SIZE * sizeof(u64),
262 mac->rx->ring, mac->rx->dma);
263out_ring_desc:
264 kfree(ring->ring_info);
265out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600266 kfree(ring);
267out_ring:
268 return -ENOMEM;
269}
270
271
272static 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 Johansson021fa222007-08-22 09:13:11 -0500285 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500286 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600287 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500288 if (!ring->ring_info)
289 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600290
291 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500292 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
293 TX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600294 &ring->dma, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500295 if (!ring->ring)
296 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600297
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500298 memset(ring->ring, 0, TX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600299
Olof Johanssona85b9422007-09-15 13:40:59 -0700300 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
301 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600302 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500303 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600304
Olof Johanssona85b9422007-09-15 13:40:59 -0700305 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600306
Olof Johanssona85b9422007-09-15 13:40:59 -0700307 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 Johanssonf5cd7872007-01-31 21:43:54 -0600312
Olof Johansson021fa222007-08-22 09:13:11 -0500313 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600314 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 Johanssonfc9e4d22007-10-02 16:25:53 -0500322out_ring_desc:
323 kfree(ring->ring_info);
324out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600325 kfree(ring);
326out_ring:
327 return -ENOMEM;
328}
329
330static void pasemi_mac_free_tx_resources(struct net_device *dev)
331{
332 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500333 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600334 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500335 dma_addr_t dmas[MAX_SKB_FRAGS+1];
336 int freed;
Olof Johanssonad5da102007-10-02 16:27:15 -0500337 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600338
Olof Johanssonad5da102007-10-02 16:27:15 -0500339 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 Johanssonfc9e4d22007-10-02 16:25:53 -0500347 info = &TX_RING_INFO(mac, i+1);
348 if (info->dma && info->skb) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500349 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 Johanssonf5cd7872007-01-31 21:43:54 -0600354 }
355
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500356 for (i = 0; i < TX_RING_SIZE; i++)
357 TX_RING(mac, i) = 0;
358
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600359 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500360 TX_RING_SIZE * sizeof(u64),
361 mac->tx->ring, mac->tx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600362
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500363 kfree(mac->tx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600364 kfree(mac->tx);
365 mac->tx = NULL;
366}
367
368static 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 Johanssonf5cd7872007-01-31 21:43:54 -0600373
374 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500375 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 Johanssonf5cd7872007-01-31 21:43:54 -0600382 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500383 info->dma = 0;
384 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600385 }
386
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500387 for (i = 0; i < RX_RING_SIZE; i++)
388 RX_RING(mac, i) = 0;
389
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600390 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500391 RX_RING_SIZE * sizeof(u64),
392 mac->rx->ring, mac->rx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600393
394 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
395 mac->rx->buffers, mac->rx->buf_dma);
396
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500397 kfree(mac->rx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600398 kfree(mac->rx);
399 mac->rx = NULL;
400}
401
Olof Johansson928773c2007-09-26 16:25:06 -0500402static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600403{
404 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500405 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600406
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500407 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600408 return;
409
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500410 fill = mac->rx->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500411 for (count = 0; count < limit; count++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500412 struct pasemi_mac_buffer *info = &RX_RING_INFO(mac, fill);
413 u64 *buff = &RX_BUFF(mac, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600414 struct sk_buff *skb;
415 dma_addr_t dma;
416
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500417 /* Entry in use? */
418 WARN_ON(*buff);
419
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500420 /* skb might still be in there for recycle on short receives */
421 if (info->skb)
422 skb = info->skb;
Olof Johansson8dc121a2007-10-02 16:26:53 -0500423 else {
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500424 skb = dev_alloc_skb(BUF_SIZE);
Olof Johansson8dc121a2007-10-02 16:26:53 -0500425 skb_reserve(skb, LOCAL_SKB_ALIGN);
426 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600427
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500428 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600429 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600430
Olof Johansson8dc121a2007-10-02 16:26:53 -0500431 dma = pci_map_single(mac->dma_pdev, skb->data,
432 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600433 PCI_DMA_FROMDEVICE);
434
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500435 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600436 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600437 break;
438 }
439
440 info->skb = skb;
441 info->dma = dma;
442 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500443 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600444 }
445
446 wmb();
447
Olof Johansson928773c2007-09-26 16:25:06 -0500448 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600449
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500450 mac->rx->next_to_fill = (mac->rx->next_to_fill + count) &
451 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600452}
453
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500454static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
455{
Olof Johansson52a94352007-05-12 18:01:09 -0500456 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500457 /* Re-enable packet count interrupts: finally
458 * ack the packet count interrupt we got in rx_intr.
459 */
460
Olof Johansson52a94352007-05-12 18:01:09 -0500461 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500462
Olof Johansson52a94352007-05-12 18:01:09 -0500463 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500464
Olof Johanssona85b9422007-09-15 13:40:59 -0700465 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500466}
467
468static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
469{
Olof Johansson52a94352007-05-12 18:01:09 -0500470 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500471
472 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500473 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500474
Olof Johansson52a94352007-05-12 18:01:09 -0500475 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500476
Olof Johanssona85b9422007-09-15 13:40:59 -0700477 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500478}
479
480
Olof Johansson69c29d82007-10-02 16:24:51 -0500481static 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
498static 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 Johanssonf5cd7872007-01-31 21:43:54 -0600513static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
514{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500515 unsigned int n;
516 int count;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500517 struct pasemi_mac_buffer *info;
518 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500519 unsigned int len;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500520 u64 macrx;
521 dma_addr_t dma;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500522 int buf_index;
523 u64 eval;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600524
525 spin_lock(&mac->rx->lock);
526
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500527 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600528
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500529 prefetch(RX_RING(mac, n));
530
531 for (count = 0; count < limit; count++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500532 macrx = RX_RING(mac, n);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600533
Olof Johansson69c29d82007-10-02 16:24:51 -0500534 if ((macrx & XCT_MACRX_E) ||
535 (*mac->rx_status & PAS_STATUS_ERROR))
536 pasemi_mac_rx_error(mac, macrx);
537
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500538 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600539 break;
540
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600541 info = NULL;
542
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500543 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600544
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500545 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 Johanssonfc9e4d22007-10-02 16:25:53 -0500551
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500552 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600553
Olof Johanssonad5da102007-10-02 16:27:15 -0500554 prefetch(skb);
555 prefetch(&skb->data_len);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600556
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500557 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600558
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500559 if (len < 256) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500560 struct sk_buff *new_skb;
561
562 new_skb = netdev_alloc_skb(mac->netdev,
563 len + LOCAL_SKB_ALIGN);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500564 if (new_skb) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500565 skb_reserve(new_skb, LOCAL_SKB_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500566 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500567 /* 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 Johanssonf5cd7872007-01-31 21:43:54 -0600573
Olof Johanssonad5da102007-10-02 16:27:15 -0500574 pci_unmap_single(mac->dma_pdev, dma, len, PCI_DMA_FROMDEVICE);
575
576 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500577
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600578 skb_put(skb, len);
579
Olof Johansson26fcfa92007-08-22 09:12:59 -0500580 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500581 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500582 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600583 XCT_MACRX_CSUM_S;
584 } else
585 skb->ip_summed = CHECKSUM_NONE;
586
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700587 mac->netdev->stats.rx_bytes += len;
588 mac->netdev->stats.rx_packets++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600589
Olof Johansson26fcfa92007-08-22 09:12:59 -0500590 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600591 netif_receive_skb(skb);
592
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500593 RX_RING(mac, n) = 0;
594 RX_RING(mac, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500595
Olof Johanssonad5da102007-10-02 16:27:15 -0500596 /* Need to zero it out since hardware doesn't, since the
597 * replenish loop uses it to tell when it's done.
598 */
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500599 RX_BUFF(mac, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500600
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500601 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600602 }
603
Olof Johansson9a50beb2007-10-02 16:26:30 -0500604 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 Johanssonb5254ee2007-10-02 16:27:57 -0500609
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500610 mac->rx->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500611
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 Johanssonf5cd7872007-01-31 21:43:54 -0600619
620 spin_unlock(&mac->rx->lock);
621
622 return count;
623}
624
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500625/* Can't make this too large or we blow the kernel stack limits */
626#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
627
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600628static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
629{
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500630 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500631 unsigned int start, descr_count, buf_count, batch_limit;
632 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500633 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500634 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500635 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
636 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600637
Olof Johansson02df6cf2007-08-22 09:13:03 -0500638 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500639 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500640restart:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600641 spin_lock_irqsave(&mac->tx->lock, flags);
642
643 start = mac->tx->next_to_clean;
Olof Johanssonad5da102007-10-02 16:27:15 -0500644 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 Johansson02df6cf2007-08-22 09:13:03 -0500649
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500650 buf_count = 0;
651 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600652
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500653 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500654 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500655 i += buf_count) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500656 u64 mactx = TX_RING(mac, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500657 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500658
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500659 if ((mactx & XCT_MACTX_E) ||
Olof Johansson69c29d82007-10-02 16:24:51 -0500660 (*mac->tx_status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500661 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500662
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500663 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500664 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600665 break;
666
Olof Johanssonad5da102007-10-02 16:27:15 -0500667 skb = TX_RING_INFO(mac, i+1).skb;
668 skbs[descr_count] = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500669
Olof Johanssonad5da102007-10-02 16:27:15 -0500670 buf_count = 2 + skb_shinfo(skb)->nr_frags;
671 for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500672 dmas[descr_count][j] = TX_RING_INFO(mac, i+1+j).dma;
673
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500674 TX_RING(mac, i) = 0;
675 TX_RING(mac, i+1) = 0;
676
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500677 /* 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 Johanssonf5cd7872007-01-31 21:43:54 -0600683 }
Olof Johanssonad5da102007-10-02 16:27:15 -0500684 mac->tx->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500685
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600686 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500687 netif_wake_queue(mac->netdev);
688
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500689 for (i = 0; i < descr_count; i++)
690 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500691
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500692 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500693
694 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500695 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500696 goto restart;
697
698 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600699}
700
701
702static 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 Johansson6dfa7522007-05-08 00:47:32 -0500708 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600709 return IRQ_NONE;
710
Olof Johansson6dfa7522007-05-08 00:47:32 -0500711 /* Don't reset packet count so it won't fire again but clear
712 * all others.
713 */
714
Olof Johansson6dfa7522007-05-08 00:47:32 -0500715 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 Johanssonf5cd7872007-01-31 21:43:54 -0600720 if (*mac->rx_status & PAS_STATUS_TIMER)
721 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
722
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700723 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500724
Olof Johanssona85b9422007-09-15 13:40:59 -0700725 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600726
727 return IRQ_HANDLED;
728}
729
730static 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 Johansson52a94352007-05-12 18:01:09 -0500734 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600735
Olof Johansson6dfa7522007-05-08 00:47:32 -0500736 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600737 return IRQ_NONE;
738
739 pasemi_mac_clean_tx(mac);
740
Olof Johansson52a94352007-05-12 18:01:09 -0500741 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 Johansson6dfa7522007-05-08 00:47:32 -0500744
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 Johanssonf5cd7872007-01-31 21:43:54 -0600749
Olof Johanssona85b9422007-09-15 13:40:59 -0700750 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600751
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600752 return IRQ_HANDLED;
753}
754
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500755static 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 Johanssona85b9422007-09-15 13:40:59 -0700776 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500777 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 Johanssona85b9422007-09-15 13:40:59 -0700808 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500809
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
815static 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 Torvalds90287802007-05-08 11:57:17 -0700827 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500828 if (!ph)
829 return -ENODEV;
830 phy_dn = of_find_node_by_phandle(*ph);
831
Linus Torvalds90287802007-05-08 11:57:17 -0700832 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500833 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
857err:
858 of_node_put(phy_dn);
859 return -ENODEV;
860}
861
862
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600863static int pasemi_mac_open(struct net_device *dev)
864{
865 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500866 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600867 unsigned int flags;
868 int ret;
869
870 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700871 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600872
873 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700874 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600875
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 Johanssona85b9422007-09-15 13:40:59 -0700880 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600881
Olof Johanssona85b9422007-09-15 13:40:59 -0700882 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
883 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600884
Olof Johanssona85b9422007-09-15 13:40:59 -0700885 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
Olof Johansson02df6cf2007-08-22 09:13:03 -0500886 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600887
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500888 /* 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 Johansson6dfa7522007-05-08 00:47:32 -0500892 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700893 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
894 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600895
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600896 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 Johanssona85b9422007-09-15 13:40:59 -0700904 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 Johanssonf5cd7872007-01-31 21:43:54 -0600907
908 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700909 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
Olof Johansson9e81d332007-10-02 16:27:39 -0500910 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 Johanssonf5cd7872007-01-31 21:43:54 -0600915
916 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700917 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
918 PAS_DMA_RXCHAN_CCMDSTA_EN |
Olof Johansson9e81d332007-10-02 16:27:39 -0500919 PAS_DMA_RXCHAN_CCMDSTA_DU |
920 PAS_DMA_RXCHAN_CCMDSTA_OD |
921 PAS_DMA_RXCHAN_CCMDSTA_FD |
922 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600923
924 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700925 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
Olof Johansson9e81d332007-10-02 16:27:39 -0500926 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 Johanssonf5cd7872007-01-31 21:43:54 -0600931
Olof Johansson928773c2007-09-26 16:25:06 -0500932 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600933
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500934 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), RX_RING_SIZE>>1);
935
Olof Johansson36033762007-09-26 16:24:42 -0500936 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 Johanssonbb6e9592007-05-08 00:47:54 -0500947 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 Johanssonf5cd7872007-01-31 21:43:54 -0600954 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700955 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600956
Olof Johansson771f7402007-05-08 00:47:21 -0500957 /* 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 Johanssonf5cd7872007-01-31 21:43:54 -0600969 mac->tx->irq_name, dev);
970 if (ret) {
971 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500972 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600973 goto out_tx_int;
974 }
975
Olof Johansson771f7402007-05-08 00:47:21 -0500976 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600977 mac->rx->irq_name, dev);
978 if (ret) {
979 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500980 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600981 goto out_rx_int;
982 }
983
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500984 if (mac->phydev)
985 phy_start(mac->phydev);
986
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600987 return 0;
988
989out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500990 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600991out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700992 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600993 netif_stop_queue(dev);
994 pasemi_mac_free_tx_resources(dev);
995out_tx_resources:
996 pasemi_mac_free_rx_resources(dev);
997out_rx_resources:
998
999 return ret;
1000}
1001
1002#define MAX_RETRIES 5000
1003
1004static int pasemi_mac_close(struct net_device *dev)
1005{
1006 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001007 unsigned int sta;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001008 int retries;
1009
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001010 if (mac->phydev) {
1011 phy_stop(mac->phydev);
1012 phy_disconnect(mac->phydev);
1013 }
1014
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001015 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001016 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001017
Olof Johansson9e81d332007-10-02 16:27:39 -05001018 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 Johanssonf5cd7872007-01-31 21:43:54 -06001038 /* 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 Johanssona85b9422007-09-15 13:40:59 -07001043 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 Johanssonf5cd7872007-01-31 21:43:54 -06001046
1047 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson9e81d332007-10-02 16:27:39 -05001048 sta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
1049 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001050 break;
1051 cond_resched();
1052 }
1053
Olof Johansson9e81d332007-10-02 16:27:39 -05001054 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001055 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001056
1057 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson9e81d332007-10-02 16:27:39 -05001058 sta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
1059 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001060 break;
1061 cond_resched();
1062 }
1063
Olof Johansson9e81d332007-10-02 16:27:39 -05001064 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001065 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001066
1067 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson9e81d332007-10-02 16:27:39 -05001068 sta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1069 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001070 break;
1071 cond_resched();
1072 }
1073
Olof Johansson9e81d332007-10-02 16:27:39 -05001074 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001075 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001076
1077 /* Then, disable the channel. This must be done separately from
1078 * stopping, since you can't disable when active.
1079 */
1080
Olof Johanssona85b9422007-09-15 13:40:59 -07001081 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 Johanssonf5cd7872007-01-31 21:43:54 -06001084
Olof Johansson771f7402007-05-08 00:47:21 -05001085 free_irq(mac->tx_irq, dev);
1086 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001087
1088 /* Free resources */
1089 pasemi_mac_free_rx_resources(dev);
1090 pasemi_mac_free_tx_resources(dev);
1091
1092 return 0;
1093}
1094
1095static 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 Johanssonad3c20d2007-10-02 16:26:13 -05001099 u64 dflags, mactx;
1100 dma_addr_t map[MAX_SKB_FRAGS+1];
1101 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001102 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001103 int i, nfrags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001104
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 Melod56f90a2007-04-10 20:50:43 -07001108 const unsigned char *nh = skb_network_header(skb);
1109
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001110 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001111 case IPPROTO_TCP:
1112 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001113 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001114 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001115 break;
1116 case IPPROTO_UDP:
1117 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001118 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001119 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001120 break;
1121 }
1122 }
1123
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001124 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001125
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001126 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 Johanssonf5cd7872007-01-31 21:43:54 -06001144
Olof Johansson26fcfa92007-08-22 09:12:59 -05001145 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001146
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001147 txring = mac->tx;
1148
1149 spin_lock_irqsave(&txring->lock, flags);
1150
Olof Johanssonad5da102007-10-02 16:27:15 -05001151 /* 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 Johanssonf5cd7872007-01-31 21:43:54 -06001159 }
1160
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001161 TX_RING(mac, txring->next_to_fill) = mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001162 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 Johanssonf5cd7872007-01-31 21:43:54 -06001169
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001170 /* 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 Johanssonf5cd7872007-01-31 21:43:54 -06001176
Olof Johanssonad5da102007-10-02 16:27:15 -05001177 txring->next_to_fill = (txring->next_to_fill + nfrags + 1) &
1178 (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001179
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001180 dev->stats.tx_packets++;
1181 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001182
1183 spin_unlock_irqrestore(&txring->lock, flags);
1184
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001185 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001186
1187 return NETDEV_TX_OK;
1188
1189out_err:
1190 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001191out_err_nolock:
1192 while (nfrags--)
1193 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1194 PCI_DMA_TODEVICE);
1195
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001196 return NETDEV_TX_BUSY;
1197}
1198
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001199static 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 Johanssona85b9422007-09-15 13:40:59 -07001204 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001205
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 Johanssona85b9422007-09-15 13:40:59 -07001212 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001213}
1214
1215
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001216static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001217{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001218 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1219 struct net_device *dev = mac->netdev;
1220 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001221
Olof Johansson829185e2007-09-15 13:53:19 -07001222 pasemi_mac_clean_tx(mac);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001223 pkts = pasemi_mac_clean_rx(mac, budget);
1224 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001225 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001226 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001227
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001228 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001229 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001230 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001231}
1232
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001233static 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;
1247fallback:
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
1255static 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 Johanssonf5cd7872007-01-31 21:43:54 -06001300static int __devinit
1301pasemi_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 Perches0795af52007-10-03 17:59:30 -07001307 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001308
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 Johanssonf5cd7872007-01-31 21:43:54 -06001321 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 Johanssonf5cd7872007-01-31 21:43:54 -06001328
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001329 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1330
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001331 dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX | NETIF_F_SG;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001332
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001333 /* 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 Johanssonf5cd7872007-01-31 21:43:54 -06001368 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001369
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001370 err = pasemi_mac_map_regs(mac);
1371 if (err)
1372 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001373
1374 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1375 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1376
Olof Johanssonceb51362007-05-08 00:47:49 -05001377 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1378
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001379 /* Enable most messages by default */
1380 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1381
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001382 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 Johansson69c29d82007-10-02 16:24:51 -05001388 } else if netif_msg_probe(mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001389 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
Joe Perches0795af52007-10-03 17:59:30 -07001390 "hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001391 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1392 mac->dma_if, mac->dma_txch, mac->dma_rxch,
Joe Perches0795af52007-10-03 17:59:30 -07001393 print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001394
1395 return err;
1396
1397out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001398 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 Johanssonf5cd7872007-01-31 21:43:54 -06001409 free_netdev(dev);
1410out_disable_device:
1411 pci_disable_device(pdev);
1412 return err;
1413
1414}
1415
1416static 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 Johanssonb6e05a12007-09-15 13:44:07 -07001432 iounmap(mac->regs);
1433 iounmap(mac->dma_regs);
1434 iounmap(mac->iob_regs);
1435
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001436 pci_set_drvdata(pdev, NULL);
1437 free_netdev(netdev);
1438}
1439
1440static 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.netfd178252007-05-12 14:57:36 -05001443 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001444};
1445
1446MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1447
1448static 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
1455static 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
1462int pasemi_mac_init_module(void)
1463{
1464 return pci_register_driver(&pasemi_mac_driver);
1465}
1466
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001467module_init(pasemi_mac_init_module);
1468module_exit(pasemi_mac_cleanup_module);