blob: 78b127c404e9364f0b2c92fad1240047d47b80cf [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
40
41/* TODO list
42 *
43 * - Get rid of pci_{read,write}_config(), map registers with ioremap
44 * for performance
45 * - PHY support
46 * - Multicast support
47 * - Large MTU support
48 * - Other performance improvements
49 */
50
51
52/* Must be a power of two */
53#define RX_RING_SIZE 512
54#define TX_RING_SIZE 512
55
Olof Johanssonceb51362007-05-08 00:47:49 -050056#define DEFAULT_MSG_ENABLE \
57 (NETIF_MSG_DRV | \
58 NETIF_MSG_PROBE | \
59 NETIF_MSG_LINK | \
60 NETIF_MSG_TIMER | \
61 NETIF_MSG_IFDOWN | \
62 NETIF_MSG_IFUP | \
63 NETIF_MSG_RX_ERR | \
64 NETIF_MSG_TX_ERR)
65
Olof Johanssonf5cd7872007-01-31 21:43:54 -060066#define TX_DESC(mac, num) ((mac)->tx->desc[(num) & (TX_RING_SIZE-1)])
67#define TX_DESC_INFO(mac, num) ((mac)->tx->desc_info[(num) & (TX_RING_SIZE-1)])
68#define RX_DESC(mac, num) ((mac)->rx->desc[(num) & (RX_RING_SIZE-1)])
69#define RX_DESC_INFO(mac, num) ((mac)->rx->desc_info[(num) & (RX_RING_SIZE-1)])
70#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
71
72#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
73
Olof Johanssonceb51362007-05-08 00:47:49 -050074MODULE_LICENSE("GPL");
75MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
76MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
77
78static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
79module_param(debug, int, 0);
80MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
81
Olof Johanssonf5cd7872007-01-31 21:43:54 -060082static struct pasdma_status *dma_status;
83
84static int pasemi_get_mac_addr(struct pasemi_mac *mac)
85{
86 struct pci_dev *pdev = mac->pdev;
87 struct device_node *dn = pci_device_to_OF_node(pdev);
88 const u8 *maddr;
89 u8 addr[6];
90
91 if (!dn) {
92 dev_dbg(&pdev->dev,
93 "No device node for mac, not configuring\n");
94 return -ENOENT;
95 }
96
97 maddr = get_property(dn, "mac-address", NULL);
98 if (maddr == NULL) {
99 dev_warn(&pdev->dev,
100 "no mac address in device tree, not configuring\n");
101 return -ENOENT;
102 }
103
104 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
105 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
106 dev_warn(&pdev->dev,
107 "can't parse mac address, not configuring\n");
108 return -EINVAL;
109 }
110
111 memcpy(mac->mac_addr, addr, sizeof(addr));
112 return 0;
113}
114
115static int pasemi_mac_setup_rx_resources(struct net_device *dev)
116{
117 struct pasemi_mac_rxring *ring;
118 struct pasemi_mac *mac = netdev_priv(dev);
119 int chan_id = mac->dma_rxch;
120
121 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
122
123 if (!ring)
124 goto out_ring;
125
126 spin_lock_init(&ring->lock);
127
128 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
129 RX_RING_SIZE, GFP_KERNEL);
130
131 if (!ring->desc_info)
132 goto out_desc_info;
133
134 /* Allocate descriptors */
135 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
136 RX_RING_SIZE *
137 sizeof(struct pas_dma_xct_descr),
138 &ring->dma, GFP_KERNEL);
139
140 if (!ring->desc)
141 goto out_desc;
142
143 memset(ring->desc, 0, RX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
144
145 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
146 RX_RING_SIZE * sizeof(u64),
147 &ring->buf_dma, GFP_KERNEL);
148 if (!ring->buffers)
149 goto out_buffers;
150
151 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
152
153 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_BASEL(chan_id),
154 PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
155
156 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_BASEU(chan_id),
157 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
158 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 2));
159
160 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_CFG(chan_id),
161 PAS_DMA_RXCHAN_CFG_HBU(1));
162
163 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXINT_BASEL(mac->dma_if),
164 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
165
166 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXINT_BASEU(mac->dma_if),
167 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
168 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
169
170 ring->next_to_fill = 0;
171 ring->next_to_clean = 0;
172
173 snprintf(ring->irq_name, sizeof(ring->irq_name),
174 "%s rx", dev->name);
175 mac->rx = ring;
176
177 return 0;
178
179out_buffers:
180 dma_free_coherent(&mac->dma_pdev->dev,
181 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
182 mac->rx->desc, mac->rx->dma);
183out_desc:
184 kfree(ring->desc_info);
185out_desc_info:
186 kfree(ring);
187out_ring:
188 return -ENOMEM;
189}
190
191
192static int pasemi_mac_setup_tx_resources(struct net_device *dev)
193{
194 struct pasemi_mac *mac = netdev_priv(dev);
195 u32 val;
196 int chan_id = mac->dma_txch;
197 struct pasemi_mac_txring *ring;
198
199 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
200 if (!ring)
201 goto out_ring;
202
203 spin_lock_init(&ring->lock);
204
205 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
206 TX_RING_SIZE, GFP_KERNEL);
207 if (!ring->desc_info)
208 goto out_desc_info;
209
210 /* Allocate descriptors */
211 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
212 TX_RING_SIZE *
213 sizeof(struct pas_dma_xct_descr),
214 &ring->dma, GFP_KERNEL);
215 if (!ring->desc)
216 goto out_desc;
217
218 memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
219
220 pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_BASEL(chan_id),
221 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
222 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
223 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
224
225 pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_BASEU(chan_id), val);
226
227 pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_CFG(chan_id),
228 PAS_DMA_TXCHAN_CFG_TY_IFACE |
229 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
230 PAS_DMA_TXCHAN_CFG_UP |
231 PAS_DMA_TXCHAN_CFG_WT(2));
232
233 ring->next_to_use = 0;
234 ring->next_to_clean = 0;
235
236 snprintf(ring->irq_name, sizeof(ring->irq_name),
237 "%s tx", dev->name);
238 mac->tx = ring;
239
240 return 0;
241
242out_desc:
243 kfree(ring->desc_info);
244out_desc_info:
245 kfree(ring);
246out_ring:
247 return -ENOMEM;
248}
249
250static void pasemi_mac_free_tx_resources(struct net_device *dev)
251{
252 struct pasemi_mac *mac = netdev_priv(dev);
253 unsigned int i;
254 struct pasemi_mac_buffer *info;
255 struct pas_dma_xct_descr *dp;
256
257 for (i = 0; i < TX_RING_SIZE; i++) {
258 info = &TX_DESC_INFO(mac, i);
259 dp = &TX_DESC(mac, i);
260 if (info->dma) {
261 if (info->skb) {
262 pci_unmap_single(mac->dma_pdev,
263 info->dma,
264 info->skb->len,
265 PCI_DMA_TODEVICE);
266 dev_kfree_skb_any(info->skb);
267 }
268 info->dma = 0;
269 info->skb = NULL;
270 dp->mactx = 0;
271 dp->ptr = 0;
272 }
273 }
274
275 dma_free_coherent(&mac->dma_pdev->dev,
276 TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
277 mac->tx->desc, mac->tx->dma);
278
279 kfree(mac->tx->desc_info);
280 kfree(mac->tx);
281 mac->tx = NULL;
282}
283
284static void pasemi_mac_free_rx_resources(struct net_device *dev)
285{
286 struct pasemi_mac *mac = netdev_priv(dev);
287 unsigned int i;
288 struct pasemi_mac_buffer *info;
289 struct pas_dma_xct_descr *dp;
290
291 for (i = 0; i < RX_RING_SIZE; i++) {
292 info = &RX_DESC_INFO(mac, i);
293 dp = &RX_DESC(mac, i);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500294 if (info->skb) {
295 if (info->dma) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600296 pci_unmap_single(mac->dma_pdev,
297 info->dma,
298 info->skb->len,
299 PCI_DMA_FROMDEVICE);
300 dev_kfree_skb_any(info->skb);
301 }
302 info->dma = 0;
303 info->skb = NULL;
304 dp->macrx = 0;
305 dp->ptr = 0;
306 }
307 }
308
309 dma_free_coherent(&mac->dma_pdev->dev,
310 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
311 mac->rx->desc, mac->rx->dma);
312
313 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
314 mac->rx->buffers, mac->rx->buf_dma);
315
316 kfree(mac->rx->desc_info);
317 kfree(mac->rx);
318 mac->rx = NULL;
319}
320
321static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
322{
323 struct pasemi_mac *mac = netdev_priv(dev);
324 unsigned int i;
325 int start = mac->rx->next_to_fill;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500326 unsigned int limit, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600327
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500328 limit = (mac->rx->next_to_clean + RX_RING_SIZE -
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600329 mac->rx->next_to_fill) & (RX_RING_SIZE - 1);
330
331 /* Check to see if we're doing first-time setup */
332 if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500333 limit = RX_RING_SIZE;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600334
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500335 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600336 return;
337
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500338 i = start;
339 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600340 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
341 u64 *buff = &RX_BUFF(mac, i);
342 struct sk_buff *skb;
343 dma_addr_t dma;
344
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500345 /* skb might still be in there for recycle on short receives */
346 if (info->skb)
347 skb = info->skb;
348 else
349 skb = dev_alloc_skb(BUF_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600350
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500351 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600352 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600353
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600354 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
355 PCI_DMA_FROMDEVICE);
356
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500357 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600358 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600359 break;
360 }
361
362 info->skb = skb;
363 info->dma = dma;
364 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500365 i++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600366 }
367
368 wmb();
369
370 pci_write_config_dword(mac->dma_pdev,
371 PAS_DMA_RXCHAN_INCR(mac->dma_rxch),
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500372 limit - count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600373 pci_write_config_dword(mac->dma_pdev,
374 PAS_DMA_RXINT_INCR(mac->dma_if),
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500375 limit - count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600376
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500377 mac->rx->next_to_fill += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600378}
379
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500380static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
381{
382 unsigned int reg, stat;
383 /* Re-enable packet count interrupts: finally
384 * ack the packet count interrupt we got in rx_intr.
385 */
386
387 pci_read_config_dword(mac->iob_pdev,
388 PAS_IOB_DMA_RXCH_STAT(mac->dma_rxch),
389 &stat);
390
391 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(stat & PAS_IOB_DMA_RXCH_STAT_CNTDEL_M)
392 | PAS_IOB_DMA_RXCH_RESET_PINTC;
393
394 pci_write_config_dword(mac->iob_pdev,
395 PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch),
396 reg);
397}
398
399static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
400{
401 unsigned int reg, stat;
402
403 /* Re-enable packet count interrupts */
404 pci_read_config_dword(mac->iob_pdev,
405 PAS_IOB_DMA_TXCH_STAT(mac->dma_txch), &stat);
406
407 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(stat & PAS_IOB_DMA_TXCH_STAT_CNTDEL_M)
408 | PAS_IOB_DMA_TXCH_RESET_PINTC;
409
410 pci_write_config_dword(mac->iob_pdev,
411 PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
412}
413
414
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600415static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
416{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500417 unsigned int n;
418 int count;
419 struct pas_dma_xct_descr *dp;
420 struct pasemi_mac_buffer *info;
421 struct sk_buff *skb;
422 unsigned int i, len;
423 u64 macrx;
424 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600425
426 spin_lock(&mac->rx->lock);
427
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500428 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600429
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500430 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600431
432 rmb();
433
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500434 dp = &RX_DESC(mac, n);
435 macrx = dp->macrx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600436
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500437 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600438 break;
439
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600440
441 info = NULL;
442
443 /* We have to scan for our skb since there's no way
444 * to back-map them from the descriptor, and if we
445 * have several receive channels then they might not
446 * show up in the same order as they were put on the
447 * interface ring.
448 */
449
450 dma = (dp->ptr & XCT_PTR_ADDR_M);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500451 for (i = n; i < (n + RX_RING_SIZE); i++) {
452 info = &RX_DESC_INFO(mac, i);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600453 if (info->dma == dma)
454 break;
455 }
456
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500457 skb = info->skb;
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500458 info->dma = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600459
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500460 pci_unmap_single(mac->dma_pdev, dma, skb->len,
461 PCI_DMA_FROMDEVICE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600462
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500463 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
464
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500465 if (len < 256) {
466 struct sk_buff *new_skb =
467 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
468 if (new_skb) {
469 skb_reserve(new_skb, NET_IP_ALIGN);
470 memcpy(new_skb->data - NET_IP_ALIGN,
471 skb->data - NET_IP_ALIGN,
472 len + NET_IP_ALIGN);
473 /* save the skb in buffer_info as good */
474 skb = new_skb;
475 }
476 /* else just continue with the old one */
477 } else
478 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600479
480 skb_put(skb, len);
481
482 skb->protocol = eth_type_trans(skb, mac->netdev);
483
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500484 if ((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600485 skb->ip_summed = CHECKSUM_COMPLETE;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500486 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600487 XCT_MACRX_CSUM_S;
488 } else
489 skb->ip_summed = CHECKSUM_NONE;
490
491 mac->stats.rx_bytes += len;
492 mac->stats.rx_packets++;
493
494 netif_receive_skb(skb);
495
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600496 dp->ptr = 0;
497 dp->macrx = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500498
499 n++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600500 }
501
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500502 mac->rx->next_to_clean += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600503 pasemi_mac_replenish_rx_ring(mac->netdev);
504
505 spin_unlock(&mac->rx->lock);
506
507 return count;
508}
509
510static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
511{
512 int i;
513 struct pasemi_mac_buffer *info;
514 struct pas_dma_xct_descr *dp;
515 int start, count;
516 int flags;
517
518 spin_lock_irqsave(&mac->tx->lock, flags);
519
520 start = mac->tx->next_to_clean;
521 count = 0;
522
523 for (i = start; i < mac->tx->next_to_use; i++) {
524 dp = &TX_DESC(mac, i);
525 if (!dp || (dp->mactx & XCT_MACTX_O))
526 break;
527
528 count++;
529
530 info = &TX_DESC_INFO(mac, i);
531
532 pci_unmap_single(mac->dma_pdev, info->dma,
533 info->skb->len, PCI_DMA_TODEVICE);
534 dev_kfree_skb_irq(info->skb);
535
536 info->skb = NULL;
537 info->dma = 0;
538 dp->mactx = 0;
539 dp->ptr = 0;
540 }
541 mac->tx->next_to_clean += count;
542 spin_unlock_irqrestore(&mac->tx->lock, flags);
543
Olof Johansson0ce68c72007-04-28 15:36:40 -0500544 netif_wake_queue(mac->netdev);
545
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600546 return count;
547}
548
549
550static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
551{
552 struct net_device *dev = data;
553 struct pasemi_mac *mac = netdev_priv(dev);
554 unsigned int reg;
555
Olof Johansson6dfa7522007-05-08 00:47:32 -0500556 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600557 return IRQ_NONE;
558
Olof Johansson6dfa7522007-05-08 00:47:32 -0500559 if (*mac->rx_status & PAS_STATUS_ERROR)
560 printk("rx_status reported error\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600561
Olof Johansson6dfa7522007-05-08 00:47:32 -0500562 /* Don't reset packet count so it won't fire again but clear
563 * all others.
564 */
565
566 pci_read_config_dword(mac->dma_pdev, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), &reg);
567
568 reg = 0;
569 if (*mac->rx_status & PAS_STATUS_SOFT)
570 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
571 if (*mac->rx_status & PAS_STATUS_ERROR)
572 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600573 if (*mac->rx_status & PAS_STATUS_TIMER)
574 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
575
Olof Johansson6dfa7522007-05-08 00:47:32 -0500576 netif_rx_schedule(dev);
577
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600578 pci_write_config_dword(mac->iob_pdev,
579 PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
580
581
582 return IRQ_HANDLED;
583}
584
585static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
586{
587 struct net_device *dev = data;
588 struct pasemi_mac *mac = netdev_priv(dev);
589 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600590
Olof Johansson6dfa7522007-05-08 00:47:32 -0500591 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600592 return IRQ_NONE;
593
594 pasemi_mac_clean_tx(mac);
595
Olof Johansson6dfa7522007-05-08 00:47:32 -0500596 reg = PAS_IOB_DMA_TXCH_RESET_PINTC;
597
598 if (*mac->tx_status & PAS_STATUS_SOFT)
599 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
600 if (*mac->tx_status & PAS_STATUS_ERROR)
601 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600602
603 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch),
604 reg);
605
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600606 return IRQ_HANDLED;
607}
608
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500609static void pasemi_adjust_link(struct net_device *dev)
610{
611 struct pasemi_mac *mac = netdev_priv(dev);
612 int msg;
613 unsigned int flags;
614 unsigned int new_flags;
615
616 if (!mac->phydev->link) {
617 /* If no link, MAC speed settings don't matter. Just report
618 * link down and return.
619 */
620 if (mac->link && netif_msg_link(mac))
621 printk(KERN_INFO "%s: Link is down.\n", dev->name);
622
623 netif_carrier_off(dev);
624 mac->link = 0;
625
626 return;
627 } else
628 netif_carrier_on(dev);
629
630 pci_read_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, &flags);
631 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
632 PAS_MAC_CFG_PCFG_TSR_M);
633
634 if (!mac->phydev->duplex)
635 new_flags |= PAS_MAC_CFG_PCFG_HD;
636
637 switch (mac->phydev->speed) {
638 case 1000:
639 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
640 PAS_MAC_CFG_PCFG_TSR_1G;
641 break;
642 case 100:
643 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
644 PAS_MAC_CFG_PCFG_TSR_100M;
645 break;
646 case 10:
647 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
648 PAS_MAC_CFG_PCFG_TSR_10M;
649 break;
650 default:
651 printk("Unsupported speed %d\n", mac->phydev->speed);
652 }
653
654 /* Print on link or speed/duplex change */
655 msg = mac->link != mac->phydev->link || flags != new_flags;
656
657 mac->duplex = mac->phydev->duplex;
658 mac->speed = mac->phydev->speed;
659 mac->link = mac->phydev->link;
660
661 if (new_flags != flags)
662 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, new_flags);
663
664 if (msg && netif_msg_link(mac))
665 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
666 dev->name, mac->speed, mac->duplex ? "full" : "half");
667}
668
669static int pasemi_mac_phy_init(struct net_device *dev)
670{
671 struct pasemi_mac *mac = netdev_priv(dev);
672 struct device_node *dn, *phy_dn;
673 struct phy_device *phydev;
674 unsigned int phy_id;
675 const phandle *ph;
676 const unsigned int *prop;
677 struct resource r;
678 int ret;
679
680 dn = pci_device_to_OF_node(mac->pdev);
681 ph = get_property(dn, "phy-handle", NULL);
682 if (!ph)
683 return -ENODEV;
684 phy_dn = of_find_node_by_phandle(*ph);
685
686 prop = get_property(phy_dn, "reg", NULL);
687 ret = of_address_to_resource(phy_dn->parent, 0, &r);
688 if (ret)
689 goto err;
690
691 phy_id = *prop;
692 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
693
694 of_node_put(phy_dn);
695
696 mac->link = 0;
697 mac->speed = 0;
698 mac->duplex = -1;
699
700 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
701
702 if (IS_ERR(phydev)) {
703 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
704 return PTR_ERR(phydev);
705 }
706
707 mac->phydev = phydev;
708
709 return 0;
710
711err:
712 of_node_put(phy_dn);
713 return -ENODEV;
714}
715
716
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600717static int pasemi_mac_open(struct net_device *dev)
718{
719 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500720 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600721 unsigned int flags;
722 int ret;
723
724 /* enable rx section */
725 pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_RXCMD,
726 PAS_DMA_COM_RXCMD_EN);
727
728 /* enable tx section */
729 pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_TXCMD,
730 PAS_DMA_COM_TXCMD_EN);
731
732 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
733 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
734 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
735
736 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_TXP, flags);
737
738 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
739 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
740
741 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
742
743 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
Olof Johansson6dfa7522007-05-08 00:47:32 -0500744 PAS_IOB_DMA_RXCH_CFG_CNTTH(1));
745
746 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
747 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600748
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500749 /* Clear out any residual packet count state from firmware */
750 pasemi_mac_restart_rx_intr(mac);
751 pasemi_mac_restart_tx_intr(mac);
752
Olof Johansson6dfa7522007-05-08 00:47:32 -0500753 /* 0xffffff is max value, about 16ms */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600754 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
Olof Johansson6dfa7522007-05-08 00:47:32 -0500755 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600756
757 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
758
759 ret = pasemi_mac_setup_rx_resources(dev);
760 if (ret)
761 goto out_rx_resources;
762
763 ret = pasemi_mac_setup_tx_resources(dev);
764 if (ret)
765 goto out_tx_resources;
766
767 pci_write_config_dword(mac->pdev, PAS_MAC_IPC_CHNL,
768 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
769 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
770
771 /* enable rx if */
772 pci_write_config_dword(mac->dma_pdev,
773 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
774 PAS_DMA_RXINT_RCMDSTA_EN);
775
776 /* enable rx channel */
777 pci_write_config_dword(mac->dma_pdev,
778 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
779 PAS_DMA_RXCHAN_CCMDSTA_EN |
780 PAS_DMA_RXCHAN_CCMDSTA_DU);
781
782 /* enable tx channel */
783 pci_write_config_dword(mac->dma_pdev,
784 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
785 PAS_DMA_TXCHAN_TCMDSTA_EN);
786
787 pasemi_mac_replenish_rx_ring(dev);
788
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500789 ret = pasemi_mac_phy_init(dev);
790 /* Some configs don't have PHYs (XAUI etc), so don't complain about
791 * failed init due to -ENODEV.
792 */
793 if (ret && ret != -ENODEV)
794 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
795
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600796 netif_start_queue(dev);
797 netif_poll_enable(dev);
798
Olof Johansson771f7402007-05-08 00:47:21 -0500799 /* Interrupts are a bit different for our DMA controller: While
800 * it's got one a regular PCI device header, the interrupt there
801 * is really the base of the range it's using. Each tx and rx
802 * channel has it's own interrupt source.
803 */
804
805 base_irq = virq_to_hw(mac->dma_pdev->irq);
806
807 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
808 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
809
810 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600811 mac->tx->irq_name, dev);
812 if (ret) {
813 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500814 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600815 goto out_tx_int;
816 }
817
Olof Johansson771f7402007-05-08 00:47:21 -0500818 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600819 mac->rx->irq_name, dev);
820 if (ret) {
821 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500822 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600823 goto out_rx_int;
824 }
825
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500826 if (mac->phydev)
827 phy_start(mac->phydev);
828
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600829 return 0;
830
831out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500832 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600833out_tx_int:
834 netif_poll_disable(dev);
835 netif_stop_queue(dev);
836 pasemi_mac_free_tx_resources(dev);
837out_tx_resources:
838 pasemi_mac_free_rx_resources(dev);
839out_rx_resources:
840
841 return ret;
842}
843
844#define MAX_RETRIES 5000
845
846static int pasemi_mac_close(struct net_device *dev)
847{
848 struct pasemi_mac *mac = netdev_priv(dev);
849 unsigned int stat;
850 int retries;
851
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500852 if (mac->phydev) {
853 phy_stop(mac->phydev);
854 phy_disconnect(mac->phydev);
855 }
856
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600857 netif_stop_queue(dev);
858
859 /* Clean out any pending buffers */
860 pasemi_mac_clean_tx(mac);
861 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
862
863 /* Disable interface */
864 pci_write_config_dword(mac->dma_pdev,
865 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
866 PAS_DMA_TXCHAN_TCMDSTA_ST);
867 pci_write_config_dword(mac->dma_pdev,
868 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
869 PAS_DMA_RXINT_RCMDSTA_ST);
870 pci_write_config_dword(mac->dma_pdev,
871 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
872 PAS_DMA_RXCHAN_CCMDSTA_ST);
873
874 for (retries = 0; retries < MAX_RETRIES; retries++) {
875 pci_read_config_dword(mac->dma_pdev,
876 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
877 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500878 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600879 break;
880 cond_resched();
881 }
882
Olof Johansson0ce68c72007-04-28 15:36:40 -0500883 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600884 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600885
886 for (retries = 0; retries < MAX_RETRIES; retries++) {
887 pci_read_config_dword(mac->dma_pdev,
888 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
889 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500890 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600891 break;
892 cond_resched();
893 }
894
Olof Johansson0ce68c72007-04-28 15:36:40 -0500895 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600896 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600897
898 for (retries = 0; retries < MAX_RETRIES; retries++) {
899 pci_read_config_dword(mac->dma_pdev,
900 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
901 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500902 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600903 break;
904 cond_resched();
905 }
906
Olof Johansson0ce68c72007-04-28 15:36:40 -0500907 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600908 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600909
910 /* Then, disable the channel. This must be done separately from
911 * stopping, since you can't disable when active.
912 */
913
914 pci_write_config_dword(mac->dma_pdev,
915 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
916 pci_write_config_dword(mac->dma_pdev,
917 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
918 pci_write_config_dword(mac->dma_pdev,
919 PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
920
Olof Johansson771f7402007-05-08 00:47:21 -0500921 free_irq(mac->tx_irq, dev);
922 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600923
924 /* Free resources */
925 pasemi_mac_free_rx_resources(dev);
926 pasemi_mac_free_tx_resources(dev);
927
928 return 0;
929}
930
931static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
932{
933 struct pasemi_mac *mac = netdev_priv(dev);
934 struct pasemi_mac_txring *txring;
935 struct pasemi_mac_buffer *info;
936 struct pas_dma_xct_descr *dp;
937 u64 dflags;
938 dma_addr_t map;
939 int flags;
940
941 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
942
943 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700944 const unsigned char *nh = skb_network_header(skb);
945
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700946 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600947 case IPPROTO_TCP:
948 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300949 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700950 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600951 break;
952 case IPPROTO_UDP:
953 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300954 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700955 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600956 break;
957 }
958 }
959
960 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
961
962 if (dma_mapping_error(map))
963 return NETDEV_TX_BUSY;
964
965 txring = mac->tx;
966
967 spin_lock_irqsave(&txring->lock, flags);
968
969 if (txring->next_to_clean - txring->next_to_use == TX_RING_SIZE) {
970 spin_unlock_irqrestore(&txring->lock, flags);
971 pasemi_mac_clean_tx(mac);
972 spin_lock_irqsave(&txring->lock, flags);
973
974 if (txring->next_to_clean - txring->next_to_use ==
975 TX_RING_SIZE) {
976 /* Still no room -- stop the queue and wait for tx
977 * intr when there's room.
978 */
979 netif_stop_queue(dev);
980 goto out_err;
981 }
982 }
983
984
985 dp = &TX_DESC(mac, txring->next_to_use);
986 info = &TX_DESC_INFO(mac, txring->next_to_use);
987
988 dp->mactx = dflags | XCT_MACTX_LLEN(skb->len);
989 dp->ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
990 info->dma = map;
991 info->skb = skb;
992
993 txring->next_to_use++;
994 mac->stats.tx_packets++;
995 mac->stats.tx_bytes += skb->len;
996
997 spin_unlock_irqrestore(&txring->lock, flags);
998
999 pci_write_config_dword(mac->dma_pdev,
1000 PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
1001
1002 return NETDEV_TX_OK;
1003
1004out_err:
1005 spin_unlock_irqrestore(&txring->lock, flags);
1006 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
1007 return NETDEV_TX_BUSY;
1008}
1009
1010static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
1011{
1012 struct pasemi_mac *mac = netdev_priv(dev);
1013
1014 return &mac->stats;
1015}
1016
Olof Johanssonceb51362007-05-08 00:47:49 -05001017
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001018static void pasemi_mac_set_rx_mode(struct net_device *dev)
1019{
1020 struct pasemi_mac *mac = netdev_priv(dev);
1021 unsigned int flags;
1022
1023 pci_read_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, &flags);
1024
1025 /* Set promiscuous */
1026 if (dev->flags & IFF_PROMISC)
1027 flags |= PAS_MAC_CFG_PCFG_PR;
1028 else
1029 flags &= ~PAS_MAC_CFG_PCFG_PR;
1030
1031 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
1032}
1033
1034
1035static int pasemi_mac_poll(struct net_device *dev, int *budget)
1036{
1037 int pkts, limit = min(*budget, dev->quota);
1038 struct pasemi_mac *mac = netdev_priv(dev);
1039
1040 pkts = pasemi_mac_clean_rx(mac, limit);
1041
Olof Johanssoncd4ceb22007-05-08 00:47:45 -05001042 dev->quota -= pkts;
1043 *budget -= pkts;
1044
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001045 if (pkts < limit) {
1046 /* all done, no more packets present */
1047 netif_rx_complete(dev);
1048
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001049 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001050 return 0;
1051 } else {
1052 /* used up our quantum, so reschedule */
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001053 return 1;
1054 }
1055}
1056
1057static int __devinit
1058pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1059{
1060 static int index = 0;
1061 struct net_device *dev;
1062 struct pasemi_mac *mac;
1063 int err;
1064
1065 err = pci_enable_device(pdev);
1066 if (err)
1067 return err;
1068
1069 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1070 if (dev == NULL) {
1071 dev_err(&pdev->dev,
1072 "pasemi_mac: Could not allocate ethernet device.\n");
1073 err = -ENOMEM;
1074 goto out_disable_device;
1075 }
1076
1077 SET_MODULE_OWNER(dev);
1078 pci_set_drvdata(pdev, dev);
1079 SET_NETDEV_DEV(dev, &pdev->dev);
1080
1081 mac = netdev_priv(dev);
1082
1083 mac->pdev = pdev;
1084 mac->netdev = dev;
1085 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1086
1087 if (!mac->dma_pdev) {
1088 dev_err(&pdev->dev, "Can't find DMA Controller\n");
1089 err = -ENODEV;
1090 goto out_free_netdev;
1091 }
1092
1093 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1094
1095 if (!mac->iob_pdev) {
1096 dev_err(&pdev->dev, "Can't find I/O Bridge\n");
1097 err = -ENODEV;
1098 goto out_put_dma_pdev;
1099 }
1100
1101 /* These should come out of the device tree eventually */
1102 mac->dma_txch = index;
1103 mac->dma_rxch = index;
1104
1105 /* We probe GMAC before XAUI, but the DMA interfaces are
1106 * in XAUI, GMAC order.
1107 */
1108 if (index < 4)
1109 mac->dma_if = index + 2;
1110 else
1111 mac->dma_if = index - 4;
1112 index++;
1113
1114 switch (pdev->device) {
1115 case 0xa005:
1116 mac->type = MAC_TYPE_GMAC;
1117 break;
1118 case 0xa006:
1119 mac->type = MAC_TYPE_XAUI;
1120 break;
1121 default:
1122 err = -ENODEV;
1123 goto out;
1124 }
1125
1126 /* get mac addr from device tree */
1127 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1128 err = -ENODEV;
1129 goto out;
1130 }
1131 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1132
1133 dev->open = pasemi_mac_open;
1134 dev->stop = pasemi_mac_close;
1135 dev->hard_start_xmit = pasemi_mac_start_tx;
1136 dev->get_stats = pasemi_mac_get_stats;
1137 dev->set_multicast_list = pasemi_mac_set_rx_mode;
1138 dev->weight = 64;
1139 dev->poll = pasemi_mac_poll;
1140 dev->features = NETIF_F_HW_CSUM;
1141
1142 /* The dma status structure is located in the I/O bridge, and
1143 * is cache coherent.
1144 */
1145 if (!dma_status)
1146 /* XXXOJN This should come from the device tree */
1147 dma_status = __ioremap(0xfd800000, 0x1000, 0);
1148
1149 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1150 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1151
Olof Johanssonceb51362007-05-08 00:47:49 -05001152 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1153
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001154 /* Enable most messages by default */
1155 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1156
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001157 err = register_netdev(dev);
1158
1159 if (err) {
1160 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1161 err);
1162 goto out;
1163 } else
1164 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1165 "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1166 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1167 mac->dma_if, mac->dma_txch, mac->dma_rxch,
1168 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1169 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1170
1171 return err;
1172
1173out:
1174 pci_dev_put(mac->iob_pdev);
1175out_put_dma_pdev:
1176 pci_dev_put(mac->dma_pdev);
1177out_free_netdev:
1178 free_netdev(dev);
1179out_disable_device:
1180 pci_disable_device(pdev);
1181 return err;
1182
1183}
1184
1185static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1186{
1187 struct net_device *netdev = pci_get_drvdata(pdev);
1188 struct pasemi_mac *mac;
1189
1190 if (!netdev)
1191 return;
1192
1193 mac = netdev_priv(netdev);
1194
1195 unregister_netdev(netdev);
1196
1197 pci_disable_device(pdev);
1198 pci_dev_put(mac->dma_pdev);
1199 pci_dev_put(mac->iob_pdev);
1200
1201 pci_set_drvdata(pdev, NULL);
1202 free_netdev(netdev);
1203}
1204
1205static struct pci_device_id pasemi_mac_pci_tbl[] = {
1206 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1207 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
1208};
1209
1210MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1211
1212static struct pci_driver pasemi_mac_driver = {
1213 .name = "pasemi_mac",
1214 .id_table = pasemi_mac_pci_tbl,
1215 .probe = pasemi_mac_probe,
1216 .remove = __devexit_p(pasemi_mac_remove),
1217};
1218
1219static void __exit pasemi_mac_cleanup_module(void)
1220{
1221 pci_unregister_driver(&pasemi_mac_driver);
1222 __iounmap(dma_status);
1223 dma_status = NULL;
1224}
1225
1226int pasemi_mac_init_module(void)
1227{
1228 return pci_register_driver(&pasemi_mac_driver);
1229}
1230
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001231module_init(pasemi_mac_init_module);
1232module_exit(pasemi_mac_cleanup_module);