blob: 216bb4a2c6338d6f462fea425e0cca690634aa41 [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);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500282 if (info->skb) {
283 if (info->dma) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600284 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
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500332 /* skb might still be in there for recycle on short receives */
333 if (info->skb)
334 skb = info->skb;
335 else
336 skb = dev_alloc_skb(BUF_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600337
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500338 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600339 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600340
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600341 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
342 PCI_DMA_FROMDEVICE);
343
344 if (dma_mapping_error(dma)) {
345 dev_kfree_skb_irq(info->skb);
346 count = i - start;
347 break;
348 }
349
350 info->skb = skb;
351 info->dma = dma;
352 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
353 }
354
355 wmb();
356
357 pci_write_config_dword(mac->dma_pdev,
358 PAS_DMA_RXCHAN_INCR(mac->dma_rxch),
359 count);
360 pci_write_config_dword(mac->dma_pdev,
361 PAS_DMA_RXINT_INCR(mac->dma_if),
362 count);
363
364 mac->rx->next_to_fill += count;
365}
366
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500367static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
368{
369 unsigned int reg, stat;
370 /* Re-enable packet count interrupts: finally
371 * ack the packet count interrupt we got in rx_intr.
372 */
373
374 pci_read_config_dword(mac->iob_pdev,
375 PAS_IOB_DMA_RXCH_STAT(mac->dma_rxch),
376 &stat);
377
378 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(stat & PAS_IOB_DMA_RXCH_STAT_CNTDEL_M)
379 | PAS_IOB_DMA_RXCH_RESET_PINTC;
380
381 pci_write_config_dword(mac->iob_pdev,
382 PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch),
383 reg);
384}
385
386static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
387{
388 unsigned int reg, stat;
389
390 /* Re-enable packet count interrupts */
391 pci_read_config_dword(mac->iob_pdev,
392 PAS_IOB_DMA_TXCH_STAT(mac->dma_txch), &stat);
393
394 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(stat & PAS_IOB_DMA_TXCH_STAT_CNTDEL_M)
395 | PAS_IOB_DMA_TXCH_RESET_PINTC;
396
397 pci_write_config_dword(mac->iob_pdev,
398 PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
399}
400
401
402
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600403static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
404{
405 unsigned int i;
406 int start, count;
407
408 spin_lock(&mac->rx->lock);
409
410 start = mac->rx->next_to_clean;
411 count = 0;
412
413 for (i = start; i < (start + RX_RING_SIZE) && count < limit; i++) {
414 struct pas_dma_xct_descr *dp;
415 struct pasemi_mac_buffer *info;
416 struct sk_buff *skb;
417 unsigned int j, len;
418 dma_addr_t dma;
419
420 rmb();
421
422 dp = &RX_DESC(mac, i);
423
424 if (!(dp->macrx & XCT_MACRX_O))
425 break;
426
427 count++;
428
429 info = NULL;
430
431 /* We have to scan for our skb since there's no way
432 * to back-map them from the descriptor, and if we
433 * have several receive channels then they might not
434 * show up in the same order as they were put on the
435 * interface ring.
436 */
437
438 dma = (dp->ptr & XCT_PTR_ADDR_M);
439 for (j = start; j < (start + RX_RING_SIZE); j++) {
440 info = &RX_DESC_INFO(mac, j);
441 if (info->dma == dma)
442 break;
443 }
444
445 BUG_ON(!info);
446 BUG_ON(info->dma != dma);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500447 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600448
449 pci_unmap_single(mac->dma_pdev, info->dma, info->skb->len,
450 PCI_DMA_FROMDEVICE);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500451 info->dma = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600452
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600453
454 len = (dp->macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500455 if (len < 256) {
456 struct sk_buff *new_skb =
457 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
458 if (new_skb) {
459 skb_reserve(new_skb, NET_IP_ALIGN);
460 memcpy(new_skb->data - NET_IP_ALIGN,
461 skb->data - NET_IP_ALIGN,
462 len + NET_IP_ALIGN);
463 /* save the skb in buffer_info as good */
464 skb = new_skb;
465 }
466 /* else just continue with the old one */
467 } else
468 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600469
470 skb_put(skb, len);
471
472 skb->protocol = eth_type_trans(skb, mac->netdev);
473
474 if ((dp->macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
475 skb->ip_summed = CHECKSUM_COMPLETE;
476 skb->csum = (dp->macrx & XCT_MACRX_CSUM_M) >>
477 XCT_MACRX_CSUM_S;
478 } else
479 skb->ip_summed = CHECKSUM_NONE;
480
481 mac->stats.rx_bytes += len;
482 mac->stats.rx_packets++;
483
484 netif_receive_skb(skb);
485
486 info->dma = 0;
487 info->skb = NULL;
488 dp->ptr = 0;
489 dp->macrx = 0;
490 }
491
492 mac->rx->next_to_clean += count;
493 pasemi_mac_replenish_rx_ring(mac->netdev);
494
495 spin_unlock(&mac->rx->lock);
496
497 return count;
498}
499
500static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
501{
502 int i;
503 struct pasemi_mac_buffer *info;
504 struct pas_dma_xct_descr *dp;
505 int start, count;
506 int flags;
507
508 spin_lock_irqsave(&mac->tx->lock, flags);
509
510 start = mac->tx->next_to_clean;
511 count = 0;
512
513 for (i = start; i < mac->tx->next_to_use; i++) {
514 dp = &TX_DESC(mac, i);
515 if (!dp || (dp->mactx & XCT_MACTX_O))
516 break;
517
518 count++;
519
520 info = &TX_DESC_INFO(mac, i);
521
522 pci_unmap_single(mac->dma_pdev, info->dma,
523 info->skb->len, PCI_DMA_TODEVICE);
524 dev_kfree_skb_irq(info->skb);
525
526 info->skb = NULL;
527 info->dma = 0;
528 dp->mactx = 0;
529 dp->ptr = 0;
530 }
531 mac->tx->next_to_clean += count;
532 spin_unlock_irqrestore(&mac->tx->lock, flags);
533
Olof Johansson0ce68c72007-04-28 15:36:40 -0500534 netif_wake_queue(mac->netdev);
535
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600536 return count;
537}
538
539
540static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
541{
542 struct net_device *dev = data;
543 struct pasemi_mac *mac = netdev_priv(dev);
544 unsigned int reg;
545
Olof Johansson6dfa7522007-05-08 00:47:32 -0500546 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600547 return IRQ_NONE;
548
Olof Johansson6dfa7522007-05-08 00:47:32 -0500549 if (*mac->rx_status & PAS_STATUS_ERROR)
550 printk("rx_status reported error\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600551
Olof Johansson6dfa7522007-05-08 00:47:32 -0500552 /* Don't reset packet count so it won't fire again but clear
553 * all others.
554 */
555
556 pci_read_config_dword(mac->dma_pdev, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), &reg);
557
558 reg = 0;
559 if (*mac->rx_status & PAS_STATUS_SOFT)
560 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
561 if (*mac->rx_status & PAS_STATUS_ERROR)
562 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600563 if (*mac->rx_status & PAS_STATUS_TIMER)
564 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
565
Olof Johansson6dfa7522007-05-08 00:47:32 -0500566 netif_rx_schedule(dev);
567
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600568 pci_write_config_dword(mac->iob_pdev,
569 PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
570
571
572 return IRQ_HANDLED;
573}
574
575static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
576{
577 struct net_device *dev = data;
578 struct pasemi_mac *mac = netdev_priv(dev);
579 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600580
Olof Johansson6dfa7522007-05-08 00:47:32 -0500581 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600582 return IRQ_NONE;
583
584 pasemi_mac_clean_tx(mac);
585
Olof Johansson6dfa7522007-05-08 00:47:32 -0500586 reg = PAS_IOB_DMA_TXCH_RESET_PINTC;
587
588 if (*mac->tx_status & PAS_STATUS_SOFT)
589 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
590 if (*mac->tx_status & PAS_STATUS_ERROR)
591 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600592
593 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch),
594 reg);
595
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600596 return IRQ_HANDLED;
597}
598
599static int pasemi_mac_open(struct net_device *dev)
600{
601 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500602 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600603 unsigned int flags;
604 int ret;
605
606 /* enable rx section */
607 pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_RXCMD,
608 PAS_DMA_COM_RXCMD_EN);
609
610 /* enable tx section */
611 pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_TXCMD,
612 PAS_DMA_COM_TXCMD_EN);
613
614 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
615 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
616 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
617
618 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_TXP, flags);
619
620 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
621 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
622
623 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
624
625 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
Olof Johansson6dfa7522007-05-08 00:47:32 -0500626 PAS_IOB_DMA_RXCH_CFG_CNTTH(1));
627
628 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
629 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600630
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500631 /* Clear out any residual packet count state from firmware */
632 pasemi_mac_restart_rx_intr(mac);
633 pasemi_mac_restart_tx_intr(mac);
634
Olof Johansson6dfa7522007-05-08 00:47:32 -0500635 /* 0xffffff is max value, about 16ms */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600636 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
Olof Johansson6dfa7522007-05-08 00:47:32 -0500637 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600638
639 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
640
641 ret = pasemi_mac_setup_rx_resources(dev);
642 if (ret)
643 goto out_rx_resources;
644
645 ret = pasemi_mac_setup_tx_resources(dev);
646 if (ret)
647 goto out_tx_resources;
648
649 pci_write_config_dword(mac->pdev, PAS_MAC_IPC_CHNL,
650 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
651 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
652
653 /* enable rx if */
654 pci_write_config_dword(mac->dma_pdev,
655 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
656 PAS_DMA_RXINT_RCMDSTA_EN);
657
658 /* enable rx channel */
659 pci_write_config_dword(mac->dma_pdev,
660 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
661 PAS_DMA_RXCHAN_CCMDSTA_EN |
662 PAS_DMA_RXCHAN_CCMDSTA_DU);
663
664 /* enable tx channel */
665 pci_write_config_dword(mac->dma_pdev,
666 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
667 PAS_DMA_TXCHAN_TCMDSTA_EN);
668
669 pasemi_mac_replenish_rx_ring(dev);
670
671 netif_start_queue(dev);
672 netif_poll_enable(dev);
673
Olof Johansson771f7402007-05-08 00:47:21 -0500674 /* Interrupts are a bit different for our DMA controller: While
675 * it's got one a regular PCI device header, the interrupt there
676 * is really the base of the range it's using. Each tx and rx
677 * channel has it's own interrupt source.
678 */
679
680 base_irq = virq_to_hw(mac->dma_pdev->irq);
681
682 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
683 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
684
685 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600686 mac->tx->irq_name, dev);
687 if (ret) {
688 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500689 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600690 goto out_tx_int;
691 }
692
Olof Johansson771f7402007-05-08 00:47:21 -0500693 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600694 mac->rx->irq_name, dev);
695 if (ret) {
696 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500697 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600698 goto out_rx_int;
699 }
700
701 return 0;
702
703out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500704 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600705out_tx_int:
706 netif_poll_disable(dev);
707 netif_stop_queue(dev);
708 pasemi_mac_free_tx_resources(dev);
709out_tx_resources:
710 pasemi_mac_free_rx_resources(dev);
711out_rx_resources:
712
713 return ret;
714}
715
716#define MAX_RETRIES 5000
717
718static int pasemi_mac_close(struct net_device *dev)
719{
720 struct pasemi_mac *mac = netdev_priv(dev);
721 unsigned int stat;
722 int retries;
723
724 netif_stop_queue(dev);
725
726 /* Clean out any pending buffers */
727 pasemi_mac_clean_tx(mac);
728 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
729
730 /* Disable interface */
731 pci_write_config_dword(mac->dma_pdev,
732 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
733 PAS_DMA_TXCHAN_TCMDSTA_ST);
734 pci_write_config_dword(mac->dma_pdev,
735 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
736 PAS_DMA_RXINT_RCMDSTA_ST);
737 pci_write_config_dword(mac->dma_pdev,
738 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
739 PAS_DMA_RXCHAN_CCMDSTA_ST);
740
741 for (retries = 0; retries < MAX_RETRIES; retries++) {
742 pci_read_config_dword(mac->dma_pdev,
743 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
744 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500745 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600746 break;
747 cond_resched();
748 }
749
Olof Johansson0ce68c72007-04-28 15:36:40 -0500750 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600751 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600752
753 for (retries = 0; retries < MAX_RETRIES; retries++) {
754 pci_read_config_dword(mac->dma_pdev,
755 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
756 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500757 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600758 break;
759 cond_resched();
760 }
761
Olof Johansson0ce68c72007-04-28 15:36:40 -0500762 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600763 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600764
765 for (retries = 0; retries < MAX_RETRIES; retries++) {
766 pci_read_config_dword(mac->dma_pdev,
767 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
768 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500769 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600770 break;
771 cond_resched();
772 }
773
Olof Johansson0ce68c72007-04-28 15:36:40 -0500774 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600775 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600776
777 /* Then, disable the channel. This must be done separately from
778 * stopping, since you can't disable when active.
779 */
780
781 pci_write_config_dword(mac->dma_pdev,
782 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
783 pci_write_config_dword(mac->dma_pdev,
784 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
785 pci_write_config_dword(mac->dma_pdev,
786 PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
787
Olof Johansson771f7402007-05-08 00:47:21 -0500788 free_irq(mac->tx_irq, dev);
789 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600790
791 /* Free resources */
792 pasemi_mac_free_rx_resources(dev);
793 pasemi_mac_free_tx_resources(dev);
794
795 return 0;
796}
797
798static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
799{
800 struct pasemi_mac *mac = netdev_priv(dev);
801 struct pasemi_mac_txring *txring;
802 struct pasemi_mac_buffer *info;
803 struct pas_dma_xct_descr *dp;
804 u64 dflags;
805 dma_addr_t map;
806 int flags;
807
808 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
809
810 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700811 const unsigned char *nh = skb_network_header(skb);
812
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700813 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600814 case IPPROTO_TCP:
815 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300816 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700817 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600818 break;
819 case IPPROTO_UDP:
820 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300821 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700822 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600823 break;
824 }
825 }
826
827 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
828
829 if (dma_mapping_error(map))
830 return NETDEV_TX_BUSY;
831
832 txring = mac->tx;
833
834 spin_lock_irqsave(&txring->lock, flags);
835
836 if (txring->next_to_clean - txring->next_to_use == TX_RING_SIZE) {
837 spin_unlock_irqrestore(&txring->lock, flags);
838 pasemi_mac_clean_tx(mac);
839 spin_lock_irqsave(&txring->lock, flags);
840
841 if (txring->next_to_clean - txring->next_to_use ==
842 TX_RING_SIZE) {
843 /* Still no room -- stop the queue and wait for tx
844 * intr when there's room.
845 */
846 netif_stop_queue(dev);
847 goto out_err;
848 }
849 }
850
851
852 dp = &TX_DESC(mac, txring->next_to_use);
853 info = &TX_DESC_INFO(mac, txring->next_to_use);
854
855 dp->mactx = dflags | XCT_MACTX_LLEN(skb->len);
856 dp->ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
857 info->dma = map;
858 info->skb = skb;
859
860 txring->next_to_use++;
861 mac->stats.tx_packets++;
862 mac->stats.tx_bytes += skb->len;
863
864 spin_unlock_irqrestore(&txring->lock, flags);
865
866 pci_write_config_dword(mac->dma_pdev,
867 PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
868
869 return NETDEV_TX_OK;
870
871out_err:
872 spin_unlock_irqrestore(&txring->lock, flags);
873 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
874 return NETDEV_TX_BUSY;
875}
876
877static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
878{
879 struct pasemi_mac *mac = netdev_priv(dev);
880
881 return &mac->stats;
882}
883
884static void pasemi_mac_set_rx_mode(struct net_device *dev)
885{
886 struct pasemi_mac *mac = netdev_priv(dev);
887 unsigned int flags;
888
889 pci_read_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, &flags);
890
891 /* Set promiscuous */
892 if (dev->flags & IFF_PROMISC)
893 flags |= PAS_MAC_CFG_PCFG_PR;
894 else
895 flags &= ~PAS_MAC_CFG_PCFG_PR;
896
897 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
898}
899
900
901static int pasemi_mac_poll(struct net_device *dev, int *budget)
902{
903 int pkts, limit = min(*budget, dev->quota);
904 struct pasemi_mac *mac = netdev_priv(dev);
905
906 pkts = pasemi_mac_clean_rx(mac, limit);
907
908 if (pkts < limit) {
909 /* all done, no more packets present */
910 netif_rx_complete(dev);
911
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500912 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600913 return 0;
914 } else {
915 /* used up our quantum, so reschedule */
916 dev->quota -= pkts;
917 *budget -= pkts;
918 return 1;
919 }
920}
921
922static int __devinit
923pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
924{
925 static int index = 0;
926 struct net_device *dev;
927 struct pasemi_mac *mac;
928 int err;
929
930 err = pci_enable_device(pdev);
931 if (err)
932 return err;
933
934 dev = alloc_etherdev(sizeof(struct pasemi_mac));
935 if (dev == NULL) {
936 dev_err(&pdev->dev,
937 "pasemi_mac: Could not allocate ethernet device.\n");
938 err = -ENOMEM;
939 goto out_disable_device;
940 }
941
942 SET_MODULE_OWNER(dev);
943 pci_set_drvdata(pdev, dev);
944 SET_NETDEV_DEV(dev, &pdev->dev);
945
946 mac = netdev_priv(dev);
947
948 mac->pdev = pdev;
949 mac->netdev = dev;
950 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
951
952 if (!mac->dma_pdev) {
953 dev_err(&pdev->dev, "Can't find DMA Controller\n");
954 err = -ENODEV;
955 goto out_free_netdev;
956 }
957
958 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
959
960 if (!mac->iob_pdev) {
961 dev_err(&pdev->dev, "Can't find I/O Bridge\n");
962 err = -ENODEV;
963 goto out_put_dma_pdev;
964 }
965
966 /* These should come out of the device tree eventually */
967 mac->dma_txch = index;
968 mac->dma_rxch = index;
969
970 /* We probe GMAC before XAUI, but the DMA interfaces are
971 * in XAUI, GMAC order.
972 */
973 if (index < 4)
974 mac->dma_if = index + 2;
975 else
976 mac->dma_if = index - 4;
977 index++;
978
979 switch (pdev->device) {
980 case 0xa005:
981 mac->type = MAC_TYPE_GMAC;
982 break;
983 case 0xa006:
984 mac->type = MAC_TYPE_XAUI;
985 break;
986 default:
987 err = -ENODEV;
988 goto out;
989 }
990
991 /* get mac addr from device tree */
992 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
993 err = -ENODEV;
994 goto out;
995 }
996 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
997
998 dev->open = pasemi_mac_open;
999 dev->stop = pasemi_mac_close;
1000 dev->hard_start_xmit = pasemi_mac_start_tx;
1001 dev->get_stats = pasemi_mac_get_stats;
1002 dev->set_multicast_list = pasemi_mac_set_rx_mode;
1003 dev->weight = 64;
1004 dev->poll = pasemi_mac_poll;
1005 dev->features = NETIF_F_HW_CSUM;
1006
1007 /* The dma status structure is located in the I/O bridge, and
1008 * is cache coherent.
1009 */
1010 if (!dma_status)
1011 /* XXXOJN This should come from the device tree */
1012 dma_status = __ioremap(0xfd800000, 0x1000, 0);
1013
1014 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1015 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1016
1017 err = register_netdev(dev);
1018
1019 if (err) {
1020 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1021 err);
1022 goto out;
1023 } else
1024 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1025 "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1026 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1027 mac->dma_if, mac->dma_txch, mac->dma_rxch,
1028 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1029 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1030
1031 return err;
1032
1033out:
1034 pci_dev_put(mac->iob_pdev);
1035out_put_dma_pdev:
1036 pci_dev_put(mac->dma_pdev);
1037out_free_netdev:
1038 free_netdev(dev);
1039out_disable_device:
1040 pci_disable_device(pdev);
1041 return err;
1042
1043}
1044
1045static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1046{
1047 struct net_device *netdev = pci_get_drvdata(pdev);
1048 struct pasemi_mac *mac;
1049
1050 if (!netdev)
1051 return;
1052
1053 mac = netdev_priv(netdev);
1054
1055 unregister_netdev(netdev);
1056
1057 pci_disable_device(pdev);
1058 pci_dev_put(mac->dma_pdev);
1059 pci_dev_put(mac->iob_pdev);
1060
1061 pci_set_drvdata(pdev, NULL);
1062 free_netdev(netdev);
1063}
1064
1065static struct pci_device_id pasemi_mac_pci_tbl[] = {
1066 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1067 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
1068};
1069
1070MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1071
1072static struct pci_driver pasemi_mac_driver = {
1073 .name = "pasemi_mac",
1074 .id_table = pasemi_mac_pci_tbl,
1075 .probe = pasemi_mac_probe,
1076 .remove = __devexit_p(pasemi_mac_remove),
1077};
1078
1079static void __exit pasemi_mac_cleanup_module(void)
1080{
1081 pci_unregister_driver(&pasemi_mac_driver);
1082 __iounmap(dma_status);
1083 dma_status = NULL;
1084}
1085
1086int pasemi_mac_init_module(void)
1087{
1088 return pci_register_driver(&pasemi_mac_driver);
1089}
1090
1091MODULE_LICENSE("GPL");
1092MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
1093MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
1094
1095module_init(pasemi_mac_init_module);
1096module_exit(pasemi_mac_cleanup_module);