blob: 2bf13cf55358eab03247afe135a928d13c27ba1e [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
56#define TX_DESC(mac, num) ((mac)->tx->desc[(num) & (TX_RING_SIZE-1)])
57#define TX_DESC_INFO(mac, num) ((mac)->tx->desc_info[(num) & (TX_RING_SIZE-1)])
58#define RX_DESC(mac, num) ((mac)->rx->desc[(num) & (RX_RING_SIZE-1)])
59#define RX_DESC_INFO(mac, num) ((mac)->rx->desc_info[(num) & (RX_RING_SIZE-1)])
60#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
61
62#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
63
64/* XXXOJN these should come out of the device tree some day */
65#define PAS_DMA_CAP_BASE 0xe00d0040
66#define PAS_DMA_CAP_SIZE 0x100
67#define PAS_DMA_COM_BASE 0xe00d0100
68#define PAS_DMA_COM_SIZE 0x100
69
70static struct pasdma_status *dma_status;
71
72static int pasemi_get_mac_addr(struct pasemi_mac *mac)
73{
74 struct pci_dev *pdev = mac->pdev;
75 struct device_node *dn = pci_device_to_OF_node(pdev);
76 const u8 *maddr;
77 u8 addr[6];
78
79 if (!dn) {
80 dev_dbg(&pdev->dev,
81 "No device node for mac, not configuring\n");
82 return -ENOENT;
83 }
84
85 maddr = get_property(dn, "mac-address", NULL);
86 if (maddr == NULL) {
87 dev_warn(&pdev->dev,
88 "no mac address in device tree, not configuring\n");
89 return -ENOENT;
90 }
91
92 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
93 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
94 dev_warn(&pdev->dev,
95 "can't parse mac address, not configuring\n");
96 return -EINVAL;
97 }
98
99 memcpy(mac->mac_addr, addr, sizeof(addr));
100 return 0;
101}
102
103static int pasemi_mac_setup_rx_resources(struct net_device *dev)
104{
105 struct pasemi_mac_rxring *ring;
106 struct pasemi_mac *mac = netdev_priv(dev);
107 int chan_id = mac->dma_rxch;
108
109 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
110
111 if (!ring)
112 goto out_ring;
113
114 spin_lock_init(&ring->lock);
115
116 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
117 RX_RING_SIZE, GFP_KERNEL);
118
119 if (!ring->desc_info)
120 goto out_desc_info;
121
122 /* Allocate descriptors */
123 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
124 RX_RING_SIZE *
125 sizeof(struct pas_dma_xct_descr),
126 &ring->dma, GFP_KERNEL);
127
128 if (!ring->desc)
129 goto out_desc;
130
131 memset(ring->desc, 0, RX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
132
133 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
134 RX_RING_SIZE * sizeof(u64),
135 &ring->buf_dma, GFP_KERNEL);
136 if (!ring->buffers)
137 goto out_buffers;
138
139 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
140
141 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_BASEL(chan_id),
142 PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
143
144 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_BASEU(chan_id),
145 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
146 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 2));
147
148 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_CFG(chan_id),
149 PAS_DMA_RXCHAN_CFG_HBU(1));
150
151 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXINT_BASEL(mac->dma_if),
152 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
153
154 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXINT_BASEU(mac->dma_if),
155 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
156 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
157
158 ring->next_to_fill = 0;
159 ring->next_to_clean = 0;
160
161 snprintf(ring->irq_name, sizeof(ring->irq_name),
162 "%s rx", dev->name);
163 mac->rx = ring;
164
165 return 0;
166
167out_buffers:
168 dma_free_coherent(&mac->dma_pdev->dev,
169 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
170 mac->rx->desc, mac->rx->dma);
171out_desc:
172 kfree(ring->desc_info);
173out_desc_info:
174 kfree(ring);
175out_ring:
176 return -ENOMEM;
177}
178
179
180static int pasemi_mac_setup_tx_resources(struct net_device *dev)
181{
182 struct pasemi_mac *mac = netdev_priv(dev);
183 u32 val;
184 int chan_id = mac->dma_txch;
185 struct pasemi_mac_txring *ring;
186
187 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
188 if (!ring)
189 goto out_ring;
190
191 spin_lock_init(&ring->lock);
192
193 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
194 TX_RING_SIZE, GFP_KERNEL);
195 if (!ring->desc_info)
196 goto out_desc_info;
197
198 /* Allocate descriptors */
199 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
200 TX_RING_SIZE *
201 sizeof(struct pas_dma_xct_descr),
202 &ring->dma, GFP_KERNEL);
203 if (!ring->desc)
204 goto out_desc;
205
206 memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
207
208 pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_BASEL(chan_id),
209 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
210 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
211 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
212
213 pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_BASEU(chan_id), val);
214
215 pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_CFG(chan_id),
216 PAS_DMA_TXCHAN_CFG_TY_IFACE |
217 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
218 PAS_DMA_TXCHAN_CFG_UP |
219 PAS_DMA_TXCHAN_CFG_WT(2));
220
221 ring->next_to_use = 0;
222 ring->next_to_clean = 0;
223
224 snprintf(ring->irq_name, sizeof(ring->irq_name),
225 "%s tx", dev->name);
226 mac->tx = ring;
227
228 return 0;
229
230out_desc:
231 kfree(ring->desc_info);
232out_desc_info:
233 kfree(ring);
234out_ring:
235 return -ENOMEM;
236}
237
238static void pasemi_mac_free_tx_resources(struct net_device *dev)
239{
240 struct pasemi_mac *mac = netdev_priv(dev);
241 unsigned int i;
242 struct pasemi_mac_buffer *info;
243 struct pas_dma_xct_descr *dp;
244
245 for (i = 0; i < TX_RING_SIZE; i++) {
246 info = &TX_DESC_INFO(mac, i);
247 dp = &TX_DESC(mac, i);
248 if (info->dma) {
249 if (info->skb) {
250 pci_unmap_single(mac->dma_pdev,
251 info->dma,
252 info->skb->len,
253 PCI_DMA_TODEVICE);
254 dev_kfree_skb_any(info->skb);
255 }
256 info->dma = 0;
257 info->skb = NULL;
258 dp->mactx = 0;
259 dp->ptr = 0;
260 }
261 }
262
263 dma_free_coherent(&mac->dma_pdev->dev,
264 TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
265 mac->tx->desc, mac->tx->dma);
266
267 kfree(mac->tx->desc_info);
268 kfree(mac->tx);
269 mac->tx = NULL;
270}
271
272static void pasemi_mac_free_rx_resources(struct net_device *dev)
273{
274 struct pasemi_mac *mac = netdev_priv(dev);
275 unsigned int i;
276 struct pasemi_mac_buffer *info;
277 struct pas_dma_xct_descr *dp;
278
279 for (i = 0; i < RX_RING_SIZE; i++) {
280 info = &RX_DESC_INFO(mac, i);
281 dp = &RX_DESC(mac, i);
282 if (info->dma) {
283 if (info->skb) {
284 pci_unmap_single(mac->dma_pdev,
285 info->dma,
286 info->skb->len,
287 PCI_DMA_FROMDEVICE);
288 dev_kfree_skb_any(info->skb);
289 }
290 info->dma = 0;
291 info->skb = NULL;
292 dp->macrx = 0;
293 dp->ptr = 0;
294 }
295 }
296
297 dma_free_coherent(&mac->dma_pdev->dev,
298 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
299 mac->rx->desc, mac->rx->dma);
300
301 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
302 mac->rx->buffers, mac->rx->buf_dma);
303
304 kfree(mac->rx->desc_info);
305 kfree(mac->rx);
306 mac->rx = NULL;
307}
308
309static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
310{
311 struct pasemi_mac *mac = netdev_priv(dev);
312 unsigned int i;
313 int start = mac->rx->next_to_fill;
314 unsigned int count;
315
316 count = (mac->rx->next_to_clean + RX_RING_SIZE -
317 mac->rx->next_to_fill) & (RX_RING_SIZE - 1);
318
319 /* Check to see if we're doing first-time setup */
320 if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
321 count = RX_RING_SIZE;
322
323 if (count <= 0)
324 return;
325
326 for (i = start; i < start + count; i++) {
327 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
328 u64 *buff = &RX_BUFF(mac, i);
329 struct sk_buff *skb;
330 dma_addr_t dma;
331
332 skb = dev_alloc_skb(BUF_SIZE);
333
334 if (!skb) {
335 count = i - start;
336 break;
337 }
338
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600339 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
340 PCI_DMA_FROMDEVICE);
341
342 if (dma_mapping_error(dma)) {
343 dev_kfree_skb_irq(info->skb);
344 count = i - start;
345 break;
346 }
347
348 info->skb = skb;
349 info->dma = dma;
350 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
351 }
352
353 wmb();
354
355 pci_write_config_dword(mac->dma_pdev,
356 PAS_DMA_RXCHAN_INCR(mac->dma_rxch),
357 count);
358 pci_write_config_dword(mac->dma_pdev,
359 PAS_DMA_RXINT_INCR(mac->dma_if),
360 count);
361
362 mac->rx->next_to_fill += count;
363}
364
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500365static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
366{
367 unsigned int reg, stat;
368 /* Re-enable packet count interrupts: finally
369 * ack the packet count interrupt we got in rx_intr.
370 */
371
372 pci_read_config_dword(mac->iob_pdev,
373 PAS_IOB_DMA_RXCH_STAT(mac->dma_rxch),
374 &stat);
375
376 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(stat & PAS_IOB_DMA_RXCH_STAT_CNTDEL_M)
377 | PAS_IOB_DMA_RXCH_RESET_PINTC;
378
379 pci_write_config_dword(mac->iob_pdev,
380 PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch),
381 reg);
382}
383
384static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
385{
386 unsigned int reg, stat;
387
388 /* Re-enable packet count interrupts */
389 pci_read_config_dword(mac->iob_pdev,
390 PAS_IOB_DMA_TXCH_STAT(mac->dma_txch), &stat);
391
392 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(stat & PAS_IOB_DMA_TXCH_STAT_CNTDEL_M)
393 | PAS_IOB_DMA_TXCH_RESET_PINTC;
394
395 pci_write_config_dword(mac->iob_pdev,
396 PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
397}
398
399
400
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600401static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
402{
403 unsigned int i;
404 int start, count;
405
406 spin_lock(&mac->rx->lock);
407
408 start = mac->rx->next_to_clean;
409 count = 0;
410
411 for (i = start; i < (start + RX_RING_SIZE) && count < limit; i++) {
412 struct pas_dma_xct_descr *dp;
413 struct pasemi_mac_buffer *info;
414 struct sk_buff *skb;
415 unsigned int j, len;
416 dma_addr_t dma;
417
418 rmb();
419
420 dp = &RX_DESC(mac, i);
421
422 if (!(dp->macrx & XCT_MACRX_O))
423 break;
424
425 count++;
426
427 info = NULL;
428
429 /* We have to scan for our skb since there's no way
430 * to back-map them from the descriptor, and if we
431 * have several receive channels then they might not
432 * show up in the same order as they were put on the
433 * interface ring.
434 */
435
436 dma = (dp->ptr & XCT_PTR_ADDR_M);
437 for (j = start; j < (start + RX_RING_SIZE); j++) {
438 info = &RX_DESC_INFO(mac, j);
439 if (info->dma == dma)
440 break;
441 }
442
443 BUG_ON(!info);
444 BUG_ON(info->dma != dma);
445
446 pci_unmap_single(mac->dma_pdev, info->dma, info->skb->len,
447 PCI_DMA_FROMDEVICE);
448
449 skb = info->skb;
450
451 len = (dp->macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
452
453 skb_put(skb, len);
454
455 skb->protocol = eth_type_trans(skb, mac->netdev);
456
457 if ((dp->macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
458 skb->ip_summed = CHECKSUM_COMPLETE;
459 skb->csum = (dp->macrx & XCT_MACRX_CSUM_M) >>
460 XCT_MACRX_CSUM_S;
461 } else
462 skb->ip_summed = CHECKSUM_NONE;
463
464 mac->stats.rx_bytes += len;
465 mac->stats.rx_packets++;
466
467 netif_receive_skb(skb);
468
469 info->dma = 0;
470 info->skb = NULL;
471 dp->ptr = 0;
472 dp->macrx = 0;
473 }
474
475 mac->rx->next_to_clean += count;
476 pasemi_mac_replenish_rx_ring(mac->netdev);
477
478 spin_unlock(&mac->rx->lock);
479
480 return count;
481}
482
483static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
484{
485 int i;
486 struct pasemi_mac_buffer *info;
487 struct pas_dma_xct_descr *dp;
488 int start, count;
489 int flags;
490
491 spin_lock_irqsave(&mac->tx->lock, flags);
492
493 start = mac->tx->next_to_clean;
494 count = 0;
495
496 for (i = start; i < mac->tx->next_to_use; i++) {
497 dp = &TX_DESC(mac, i);
498 if (!dp || (dp->mactx & XCT_MACTX_O))
499 break;
500
501 count++;
502
503 info = &TX_DESC_INFO(mac, i);
504
505 pci_unmap_single(mac->dma_pdev, info->dma,
506 info->skb->len, PCI_DMA_TODEVICE);
507 dev_kfree_skb_irq(info->skb);
508
509 info->skb = NULL;
510 info->dma = 0;
511 dp->mactx = 0;
512 dp->ptr = 0;
513 }
514 mac->tx->next_to_clean += count;
515 spin_unlock_irqrestore(&mac->tx->lock, flags);
516
Olof Johansson0ce68c72007-04-28 15:36:40 -0500517 netif_wake_queue(mac->netdev);
518
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600519 return count;
520}
521
522
523static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
524{
525 struct net_device *dev = data;
526 struct pasemi_mac *mac = netdev_priv(dev);
527 unsigned int reg;
528
Olof Johansson6dfa7522007-05-08 00:47:32 -0500529 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600530 return IRQ_NONE;
531
Olof Johansson6dfa7522007-05-08 00:47:32 -0500532 if (*mac->rx_status & PAS_STATUS_ERROR)
533 printk("rx_status reported error\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600534
Olof Johansson6dfa7522007-05-08 00:47:32 -0500535 /* Don't reset packet count so it won't fire again but clear
536 * all others.
537 */
538
539 pci_read_config_dword(mac->dma_pdev, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), &reg);
540
541 reg = 0;
542 if (*mac->rx_status & PAS_STATUS_SOFT)
543 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
544 if (*mac->rx_status & PAS_STATUS_ERROR)
545 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600546 if (*mac->rx_status & PAS_STATUS_TIMER)
547 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
548
Olof Johansson6dfa7522007-05-08 00:47:32 -0500549 netif_rx_schedule(dev);
550
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600551 pci_write_config_dword(mac->iob_pdev,
552 PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
553
554
555 return IRQ_HANDLED;
556}
557
558static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
559{
560 struct net_device *dev = data;
561 struct pasemi_mac *mac = netdev_priv(dev);
562 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600563
Olof Johansson6dfa7522007-05-08 00:47:32 -0500564 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600565 return IRQ_NONE;
566
567 pasemi_mac_clean_tx(mac);
568
Olof Johansson6dfa7522007-05-08 00:47:32 -0500569 reg = PAS_IOB_DMA_TXCH_RESET_PINTC;
570
571 if (*mac->tx_status & PAS_STATUS_SOFT)
572 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
573 if (*mac->tx_status & PAS_STATUS_ERROR)
574 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600575
576 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch),
577 reg);
578
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600579 return IRQ_HANDLED;
580}
581
582static int pasemi_mac_open(struct net_device *dev)
583{
584 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500585 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600586 unsigned int flags;
587 int ret;
588
589 /* enable rx section */
590 pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_RXCMD,
591 PAS_DMA_COM_RXCMD_EN);
592
593 /* enable tx section */
594 pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_TXCMD,
595 PAS_DMA_COM_TXCMD_EN);
596
597 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
598 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
599 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
600
601 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_TXP, flags);
602
603 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
604 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
605
606 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
607
608 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
Olof Johansson6dfa7522007-05-08 00:47:32 -0500609 PAS_IOB_DMA_RXCH_CFG_CNTTH(1));
610
611 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
612 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600613
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500614 /* Clear out any residual packet count state from firmware */
615 pasemi_mac_restart_rx_intr(mac);
616 pasemi_mac_restart_tx_intr(mac);
617
Olof Johansson6dfa7522007-05-08 00:47:32 -0500618 /* 0xffffff is max value, about 16ms */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600619 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
Olof Johansson6dfa7522007-05-08 00:47:32 -0500620 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600621
622 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
623
624 ret = pasemi_mac_setup_rx_resources(dev);
625 if (ret)
626 goto out_rx_resources;
627
628 ret = pasemi_mac_setup_tx_resources(dev);
629 if (ret)
630 goto out_tx_resources;
631
632 pci_write_config_dword(mac->pdev, PAS_MAC_IPC_CHNL,
633 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
634 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
635
636 /* enable rx if */
637 pci_write_config_dword(mac->dma_pdev,
638 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
639 PAS_DMA_RXINT_RCMDSTA_EN);
640
641 /* enable rx channel */
642 pci_write_config_dword(mac->dma_pdev,
643 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
644 PAS_DMA_RXCHAN_CCMDSTA_EN |
645 PAS_DMA_RXCHAN_CCMDSTA_DU);
646
647 /* enable tx channel */
648 pci_write_config_dword(mac->dma_pdev,
649 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
650 PAS_DMA_TXCHAN_TCMDSTA_EN);
651
652 pasemi_mac_replenish_rx_ring(dev);
653
654 netif_start_queue(dev);
655 netif_poll_enable(dev);
656
Olof Johansson771f7402007-05-08 00:47:21 -0500657 /* Interrupts are a bit different for our DMA controller: While
658 * it's got one a regular PCI device header, the interrupt there
659 * is really the base of the range it's using. Each tx and rx
660 * channel has it's own interrupt source.
661 */
662
663 base_irq = virq_to_hw(mac->dma_pdev->irq);
664
665 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
666 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
667
668 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600669 mac->tx->irq_name, dev);
670 if (ret) {
671 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500672 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600673 goto out_tx_int;
674 }
675
Olof Johansson771f7402007-05-08 00:47:21 -0500676 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600677 mac->rx->irq_name, dev);
678 if (ret) {
679 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500680 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600681 goto out_rx_int;
682 }
683
684 return 0;
685
686out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500687 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600688out_tx_int:
689 netif_poll_disable(dev);
690 netif_stop_queue(dev);
691 pasemi_mac_free_tx_resources(dev);
692out_tx_resources:
693 pasemi_mac_free_rx_resources(dev);
694out_rx_resources:
695
696 return ret;
697}
698
699#define MAX_RETRIES 5000
700
701static int pasemi_mac_close(struct net_device *dev)
702{
703 struct pasemi_mac *mac = netdev_priv(dev);
704 unsigned int stat;
705 int retries;
706
707 netif_stop_queue(dev);
708
709 /* Clean out any pending buffers */
710 pasemi_mac_clean_tx(mac);
711 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
712
713 /* Disable interface */
714 pci_write_config_dword(mac->dma_pdev,
715 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
716 PAS_DMA_TXCHAN_TCMDSTA_ST);
717 pci_write_config_dword(mac->dma_pdev,
718 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
719 PAS_DMA_RXINT_RCMDSTA_ST);
720 pci_write_config_dword(mac->dma_pdev,
721 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
722 PAS_DMA_RXCHAN_CCMDSTA_ST);
723
724 for (retries = 0; retries < MAX_RETRIES; retries++) {
725 pci_read_config_dword(mac->dma_pdev,
726 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
727 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500728 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600729 break;
730 cond_resched();
731 }
732
Olof Johansson0ce68c72007-04-28 15:36:40 -0500733 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600734 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600735
736 for (retries = 0; retries < MAX_RETRIES; retries++) {
737 pci_read_config_dword(mac->dma_pdev,
738 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
739 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500740 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600741 break;
742 cond_resched();
743 }
744
Olof Johansson0ce68c72007-04-28 15:36:40 -0500745 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600746 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600747
748 for (retries = 0; retries < MAX_RETRIES; retries++) {
749 pci_read_config_dword(mac->dma_pdev,
750 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
751 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500752 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600753 break;
754 cond_resched();
755 }
756
Olof Johansson0ce68c72007-04-28 15:36:40 -0500757 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600758 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600759
760 /* Then, disable the channel. This must be done separately from
761 * stopping, since you can't disable when active.
762 */
763
764 pci_write_config_dword(mac->dma_pdev,
765 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
766 pci_write_config_dword(mac->dma_pdev,
767 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
768 pci_write_config_dword(mac->dma_pdev,
769 PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
770
Olof Johansson771f7402007-05-08 00:47:21 -0500771 free_irq(mac->tx_irq, dev);
772 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600773
774 /* Free resources */
775 pasemi_mac_free_rx_resources(dev);
776 pasemi_mac_free_tx_resources(dev);
777
778 return 0;
779}
780
781static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
782{
783 struct pasemi_mac *mac = netdev_priv(dev);
784 struct pasemi_mac_txring *txring;
785 struct pasemi_mac_buffer *info;
786 struct pas_dma_xct_descr *dp;
787 u64 dflags;
788 dma_addr_t map;
789 int flags;
790
791 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
792
793 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700794 const unsigned char *nh = skb_network_header(skb);
795
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700796 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600797 case IPPROTO_TCP:
798 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300799 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700800 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600801 break;
802 case IPPROTO_UDP:
803 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300804 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700805 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600806 break;
807 }
808 }
809
810 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
811
812 if (dma_mapping_error(map))
813 return NETDEV_TX_BUSY;
814
815 txring = mac->tx;
816
817 spin_lock_irqsave(&txring->lock, flags);
818
819 if (txring->next_to_clean - txring->next_to_use == TX_RING_SIZE) {
820 spin_unlock_irqrestore(&txring->lock, flags);
821 pasemi_mac_clean_tx(mac);
822 spin_lock_irqsave(&txring->lock, flags);
823
824 if (txring->next_to_clean - txring->next_to_use ==
825 TX_RING_SIZE) {
826 /* Still no room -- stop the queue and wait for tx
827 * intr when there's room.
828 */
829 netif_stop_queue(dev);
830 goto out_err;
831 }
832 }
833
834
835 dp = &TX_DESC(mac, txring->next_to_use);
836 info = &TX_DESC_INFO(mac, txring->next_to_use);
837
838 dp->mactx = dflags | XCT_MACTX_LLEN(skb->len);
839 dp->ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
840 info->dma = map;
841 info->skb = skb;
842
843 txring->next_to_use++;
844 mac->stats.tx_packets++;
845 mac->stats.tx_bytes += skb->len;
846
847 spin_unlock_irqrestore(&txring->lock, flags);
848
849 pci_write_config_dword(mac->dma_pdev,
850 PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
851
852 return NETDEV_TX_OK;
853
854out_err:
855 spin_unlock_irqrestore(&txring->lock, flags);
856 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
857 return NETDEV_TX_BUSY;
858}
859
860static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
861{
862 struct pasemi_mac *mac = netdev_priv(dev);
863
864 return &mac->stats;
865}
866
867static void pasemi_mac_set_rx_mode(struct net_device *dev)
868{
869 struct pasemi_mac *mac = netdev_priv(dev);
870 unsigned int flags;
871
872 pci_read_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, &flags);
873
874 /* Set promiscuous */
875 if (dev->flags & IFF_PROMISC)
876 flags |= PAS_MAC_CFG_PCFG_PR;
877 else
878 flags &= ~PAS_MAC_CFG_PCFG_PR;
879
880 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
881}
882
883
884static int pasemi_mac_poll(struct net_device *dev, int *budget)
885{
886 int pkts, limit = min(*budget, dev->quota);
887 struct pasemi_mac *mac = netdev_priv(dev);
888
889 pkts = pasemi_mac_clean_rx(mac, limit);
890
891 if (pkts < limit) {
892 /* all done, no more packets present */
893 netif_rx_complete(dev);
894
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500895 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600896 return 0;
897 } else {
898 /* used up our quantum, so reschedule */
899 dev->quota -= pkts;
900 *budget -= pkts;
901 return 1;
902 }
903}
904
905static int __devinit
906pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
907{
908 static int index = 0;
909 struct net_device *dev;
910 struct pasemi_mac *mac;
911 int err;
912
913 err = pci_enable_device(pdev);
914 if (err)
915 return err;
916
917 dev = alloc_etherdev(sizeof(struct pasemi_mac));
918 if (dev == NULL) {
919 dev_err(&pdev->dev,
920 "pasemi_mac: Could not allocate ethernet device.\n");
921 err = -ENOMEM;
922 goto out_disable_device;
923 }
924
925 SET_MODULE_OWNER(dev);
926 pci_set_drvdata(pdev, dev);
927 SET_NETDEV_DEV(dev, &pdev->dev);
928
929 mac = netdev_priv(dev);
930
931 mac->pdev = pdev;
932 mac->netdev = dev;
933 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
934
935 if (!mac->dma_pdev) {
936 dev_err(&pdev->dev, "Can't find DMA Controller\n");
937 err = -ENODEV;
938 goto out_free_netdev;
939 }
940
941 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
942
943 if (!mac->iob_pdev) {
944 dev_err(&pdev->dev, "Can't find I/O Bridge\n");
945 err = -ENODEV;
946 goto out_put_dma_pdev;
947 }
948
949 /* These should come out of the device tree eventually */
950 mac->dma_txch = index;
951 mac->dma_rxch = index;
952
953 /* We probe GMAC before XAUI, but the DMA interfaces are
954 * in XAUI, GMAC order.
955 */
956 if (index < 4)
957 mac->dma_if = index + 2;
958 else
959 mac->dma_if = index - 4;
960 index++;
961
962 switch (pdev->device) {
963 case 0xa005:
964 mac->type = MAC_TYPE_GMAC;
965 break;
966 case 0xa006:
967 mac->type = MAC_TYPE_XAUI;
968 break;
969 default:
970 err = -ENODEV;
971 goto out;
972 }
973
974 /* get mac addr from device tree */
975 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
976 err = -ENODEV;
977 goto out;
978 }
979 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
980
981 dev->open = pasemi_mac_open;
982 dev->stop = pasemi_mac_close;
983 dev->hard_start_xmit = pasemi_mac_start_tx;
984 dev->get_stats = pasemi_mac_get_stats;
985 dev->set_multicast_list = pasemi_mac_set_rx_mode;
986 dev->weight = 64;
987 dev->poll = pasemi_mac_poll;
988 dev->features = NETIF_F_HW_CSUM;
989
990 /* The dma status structure is located in the I/O bridge, and
991 * is cache coherent.
992 */
993 if (!dma_status)
994 /* XXXOJN This should come from the device tree */
995 dma_status = __ioremap(0xfd800000, 0x1000, 0);
996
997 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
998 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
999
1000 err = register_netdev(dev);
1001
1002 if (err) {
1003 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1004 err);
1005 goto out;
1006 } else
1007 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1008 "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1009 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1010 mac->dma_if, mac->dma_txch, mac->dma_rxch,
1011 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1012 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1013
1014 return err;
1015
1016out:
1017 pci_dev_put(mac->iob_pdev);
1018out_put_dma_pdev:
1019 pci_dev_put(mac->dma_pdev);
1020out_free_netdev:
1021 free_netdev(dev);
1022out_disable_device:
1023 pci_disable_device(pdev);
1024 return err;
1025
1026}
1027
1028static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1029{
1030 struct net_device *netdev = pci_get_drvdata(pdev);
1031 struct pasemi_mac *mac;
1032
1033 if (!netdev)
1034 return;
1035
1036 mac = netdev_priv(netdev);
1037
1038 unregister_netdev(netdev);
1039
1040 pci_disable_device(pdev);
1041 pci_dev_put(mac->dma_pdev);
1042 pci_dev_put(mac->iob_pdev);
1043
1044 pci_set_drvdata(pdev, NULL);
1045 free_netdev(netdev);
1046}
1047
1048static struct pci_device_id pasemi_mac_pci_tbl[] = {
1049 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1050 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
1051};
1052
1053MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1054
1055static struct pci_driver pasemi_mac_driver = {
1056 .name = "pasemi_mac",
1057 .id_table = pasemi_mac_pci_tbl,
1058 .probe = pasemi_mac_probe,
1059 .remove = __devexit_p(pasemi_mac_remove),
1060};
1061
1062static void __exit pasemi_mac_cleanup_module(void)
1063{
1064 pci_unregister_driver(&pasemi_mac_driver);
1065 __iounmap(dma_status);
1066 dma_status = NULL;
1067}
1068
1069int pasemi_mac_init_module(void)
1070{
1071 return pci_register_driver(&pasemi_mac_driver);
1072}
1073
1074MODULE_LICENSE("GPL");
1075MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
1076MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
1077
1078module_init(pasemi_mac_init_module);
1079module_exit(pasemi_mac_cleanup_module);