net/sonic: Fix CAM initialization
[platform/kernel/linux-rpi.git] / drivers / net / ethernet / natsemi / sonic.c
1 /*
2  * sonic.c
3  *
4  * (C) 2005 Finn Thain
5  *
6  * Converted to DMA API, added zero-copy buffer handling, and
7  * (from the mac68k project) introduced dhd's support for 16-bit cards.
8  *
9  * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
10  *
11  * This driver is based on work from Andreas Busse, but most of
12  * the code is rewritten.
13  *
14  * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
15  *
16  *    Core code included by system sonic drivers
17  *
18  * And... partially rewritten again by David Huggins-Daines in order
19  * to cope with screwed up Macintosh NICs that may or may not use
20  * 16-bit DMA.
21  *
22  * (C) 1999 David Huggins-Daines <dhd@debian.org>
23  *
24  */
25
26 /*
27  * Sources: Olivetti M700-10 Risc Personal Computer hardware handbook,
28  * National Semiconductors data sheet for the DP83932B Sonic Ethernet
29  * controller, and the files "8390.c" and "skeleton.c" in this directory.
30  *
31  * Additional sources: Nat Semi data sheet for the DP83932C and Nat Semi
32  * Application Note AN-746, the files "lance.c" and "ibmlana.c". See also
33  * the NetBSD file "sys/arch/mac68k/dev/if_sn.c".
34  */
35
36 static unsigned int version_printed;
37
38 static int sonic_debug = -1;
39 module_param(sonic_debug, int, 0);
40 MODULE_PARM_DESC(sonic_debug, "debug message level");
41
42 static void sonic_msg_init(struct net_device *dev)
43 {
44         struct sonic_local *lp = netdev_priv(dev);
45
46         lp->msg_enable = netif_msg_init(sonic_debug, 0);
47
48         if (version_printed++ == 0)
49                 netif_dbg(lp, drv, dev, "%s", version);
50 }
51
52 /*
53  * Open/initialize the SONIC controller.
54  *
55  * This routine should set everything up anew at each open, even
56  *  registers that "should" only need to be set once at boot, so that
57  *  there is non-reboot way to recover if something goes wrong.
58  */
59 static int sonic_open(struct net_device *dev)
60 {
61         struct sonic_local *lp = netdev_priv(dev);
62         int i;
63
64         netif_dbg(lp, ifup, dev, "%s: initializing sonic driver\n", __func__);
65
66         spin_lock_init(&lp->lock);
67
68         for (i = 0; i < SONIC_NUM_RRS; i++) {
69                 struct sk_buff *skb = netdev_alloc_skb(dev, SONIC_RBSIZE + 2);
70                 if (skb == NULL) {
71                         while(i > 0) { /* free any that were allocated successfully */
72                                 i--;
73                                 dev_kfree_skb(lp->rx_skb[i]);
74                                 lp->rx_skb[i] = NULL;
75                         }
76                         printk(KERN_ERR "%s: couldn't allocate receive buffers\n",
77                                dev->name);
78                         return -ENOMEM;
79                 }
80                 /* align IP header unless DMA requires otherwise */
81                 if (SONIC_BUS_SCALE(lp->dma_bitmode) == 2)
82                         skb_reserve(skb, 2);
83                 lp->rx_skb[i] = skb;
84         }
85
86         for (i = 0; i < SONIC_NUM_RRS; i++) {
87                 dma_addr_t laddr = dma_map_single(lp->device, skb_put(lp->rx_skb[i], SONIC_RBSIZE),
88                                                   SONIC_RBSIZE, DMA_FROM_DEVICE);
89                 if (dma_mapping_error(lp->device, laddr)) {
90                         while(i > 0) { /* free any that were mapped successfully */
91                                 i--;
92                                 dma_unmap_single(lp->device, lp->rx_laddr[i], SONIC_RBSIZE, DMA_FROM_DEVICE);
93                                 lp->rx_laddr[i] = (dma_addr_t)0;
94                         }
95                         for (i = 0; i < SONIC_NUM_RRS; i++) {
96                                 dev_kfree_skb(lp->rx_skb[i]);
97                                 lp->rx_skb[i] = NULL;
98                         }
99                         printk(KERN_ERR "%s: couldn't map rx DMA buffers\n",
100                                dev->name);
101                         return -ENOMEM;
102                 }
103                 lp->rx_laddr[i] = laddr;
104         }
105
106         /*
107          * Initialize the SONIC
108          */
109         sonic_init(dev);
110
111         netif_start_queue(dev);
112
113         netif_dbg(lp, ifup, dev, "%s: Initialization done\n", __func__);
114
115         return 0;
116 }
117
118 /* Wait for the SONIC to become idle. */
119 static void sonic_quiesce(struct net_device *dev, u16 mask)
120 {
121         struct sonic_local * __maybe_unused lp = netdev_priv(dev);
122         int i;
123         u16 bits;
124
125         for (i = 0; i < 1000; ++i) {
126                 bits = SONIC_READ(SONIC_CMD) & mask;
127                 if (!bits)
128                         return;
129                 if (irqs_disabled() || in_interrupt())
130                         udelay(20);
131                 else
132                         usleep_range(100, 200);
133         }
134         WARN_ONCE(1, "command deadline expired! 0x%04x\n", bits);
135 }
136
137 /*
138  * Close the SONIC device
139  */
140 static int sonic_close(struct net_device *dev)
141 {
142         struct sonic_local *lp = netdev_priv(dev);
143         int i;
144
145         netif_dbg(lp, ifdown, dev, "%s\n", __func__);
146
147         netif_stop_queue(dev);
148
149         /*
150          * stop the SONIC, disable interrupts
151          */
152         SONIC_WRITE(SONIC_CMD, SONIC_CR_RXDIS);
153         sonic_quiesce(dev, SONIC_CR_ALL);
154
155         SONIC_WRITE(SONIC_IMR, 0);
156         SONIC_WRITE(SONIC_ISR, 0x7fff);
157         SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
158
159         /* unmap and free skbs that haven't been transmitted */
160         for (i = 0; i < SONIC_NUM_TDS; i++) {
161                 if(lp->tx_laddr[i]) {
162                         dma_unmap_single(lp->device, lp->tx_laddr[i], lp->tx_len[i], DMA_TO_DEVICE);
163                         lp->tx_laddr[i] = (dma_addr_t)0;
164                 }
165                 if(lp->tx_skb[i]) {
166                         dev_kfree_skb(lp->tx_skb[i]);
167                         lp->tx_skb[i] = NULL;
168                 }
169         }
170
171         /* unmap and free the receive buffers */
172         for (i = 0; i < SONIC_NUM_RRS; i++) {
173                 if(lp->rx_laddr[i]) {
174                         dma_unmap_single(lp->device, lp->rx_laddr[i], SONIC_RBSIZE, DMA_FROM_DEVICE);
175                         lp->rx_laddr[i] = (dma_addr_t)0;
176                 }
177                 if(lp->rx_skb[i]) {
178                         dev_kfree_skb(lp->rx_skb[i]);
179                         lp->rx_skb[i] = NULL;
180                 }
181         }
182
183         return 0;
184 }
185
186 static void sonic_tx_timeout(struct net_device *dev)
187 {
188         struct sonic_local *lp = netdev_priv(dev);
189         int i;
190         /*
191          * put the Sonic into software-reset mode and
192          * disable all interrupts before releasing DMA buffers
193          */
194         SONIC_WRITE(SONIC_CMD, SONIC_CR_RXDIS);
195         sonic_quiesce(dev, SONIC_CR_ALL);
196
197         SONIC_WRITE(SONIC_IMR, 0);
198         SONIC_WRITE(SONIC_ISR, 0x7fff);
199         SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
200         /* We could resend the original skbs. Easier to re-initialise. */
201         for (i = 0; i < SONIC_NUM_TDS; i++) {
202                 if(lp->tx_laddr[i]) {
203                         dma_unmap_single(lp->device, lp->tx_laddr[i], lp->tx_len[i], DMA_TO_DEVICE);
204                         lp->tx_laddr[i] = (dma_addr_t)0;
205                 }
206                 if(lp->tx_skb[i]) {
207                         dev_kfree_skb(lp->tx_skb[i]);
208                         lp->tx_skb[i] = NULL;
209                 }
210         }
211         /* Try to restart the adaptor. */
212         sonic_init(dev);
213         lp->stats.tx_errors++;
214         netif_trans_update(dev); /* prevent tx timeout */
215         netif_wake_queue(dev);
216 }
217
218 /*
219  * transmit packet
220  *
221  * Appends new TD during transmission thus avoiding any TX interrupts
222  * until we run out of TDs.
223  * This routine interacts closely with the ISR in that it may,
224  *   set tx_skb[i]
225  *   reset the status flags of the new TD
226  *   set and reset EOL flags
227  *   stop the tx queue
228  * The ISR interacts with this routine in various ways. It may,
229  *   reset tx_skb[i]
230  *   test the EOL and status flags of the TDs
231  *   wake the tx queue
232  * Concurrently with all of this, the SONIC is potentially writing to
233  * the status flags of the TDs.
234  */
235
236 static int sonic_send_packet(struct sk_buff *skb, struct net_device *dev)
237 {
238         struct sonic_local *lp = netdev_priv(dev);
239         dma_addr_t laddr;
240         int length;
241         int entry;
242         unsigned long flags;
243
244         netif_dbg(lp, tx_queued, dev, "%s: skb=%p\n", __func__, skb);
245
246         length = skb->len;
247         if (length < ETH_ZLEN) {
248                 if (skb_padto(skb, ETH_ZLEN))
249                         return NETDEV_TX_OK;
250                 length = ETH_ZLEN;
251         }
252
253         /*
254          * Map the packet data into the logical DMA address space
255          */
256
257         laddr = dma_map_single(lp->device, skb->data, length, DMA_TO_DEVICE);
258         if (!laddr) {
259                 pr_err_ratelimited("%s: failed to map tx DMA buffer.\n", dev->name);
260                 dev_kfree_skb_any(skb);
261                 return NETDEV_TX_OK;
262         }
263
264         spin_lock_irqsave(&lp->lock, flags);
265
266         entry = lp->next_tx;
267
268         sonic_tda_put(dev, entry, SONIC_TD_STATUS, 0);       /* clear status */
269         sonic_tda_put(dev, entry, SONIC_TD_FRAG_COUNT, 1);   /* single fragment */
270         sonic_tda_put(dev, entry, SONIC_TD_PKTSIZE, length); /* length of packet */
271         sonic_tda_put(dev, entry, SONIC_TD_FRAG_PTR_L, laddr & 0xffff);
272         sonic_tda_put(dev, entry, SONIC_TD_FRAG_PTR_H, laddr >> 16);
273         sonic_tda_put(dev, entry, SONIC_TD_FRAG_SIZE, length);
274         sonic_tda_put(dev, entry, SONIC_TD_LINK,
275                 sonic_tda_get(dev, entry, SONIC_TD_LINK) | SONIC_EOL);
276
277         wmb();
278         lp->tx_len[entry] = length;
279         lp->tx_laddr[entry] = laddr;
280         lp->tx_skb[entry] = skb;
281
282         wmb();
283         sonic_tda_put(dev, lp->eol_tx, SONIC_TD_LINK,
284                                   sonic_tda_get(dev, lp->eol_tx, SONIC_TD_LINK) & ~SONIC_EOL);
285         lp->eol_tx = entry;
286
287         lp->next_tx = (entry + 1) & SONIC_TDS_MASK;
288         if (lp->tx_skb[lp->next_tx] != NULL) {
289                 /* The ring is full, the ISR has yet to process the next TD. */
290                 netif_dbg(lp, tx_queued, dev, "%s: stopping queue\n", __func__);
291                 netif_stop_queue(dev);
292                 /* after this packet, wait for ISR to free up some TDAs */
293         } else netif_start_queue(dev);
294
295         netif_dbg(lp, tx_queued, dev, "%s: issuing Tx command\n", __func__);
296
297         SONIC_WRITE(SONIC_CMD, SONIC_CR_TXP);
298
299         spin_unlock_irqrestore(&lp->lock, flags);
300
301         return NETDEV_TX_OK;
302 }
303
304 /*
305  * The typical workload of the driver:
306  * Handle the network interface interrupts.
307  */
308 static irqreturn_t sonic_interrupt(int irq, void *dev_id)
309 {
310         struct net_device *dev = dev_id;
311         struct sonic_local *lp = netdev_priv(dev);
312         int status;
313         unsigned long flags;
314
315         /* The lock has two purposes. Firstly, it synchronizes sonic_interrupt()
316          * with sonic_send_packet() so that the two functions can share state.
317          * Secondly, it makes sonic_interrupt() re-entrant, as that is required
318          * by macsonic which must use two IRQs with different priority levels.
319          */
320         spin_lock_irqsave(&lp->lock, flags);
321
322         status = SONIC_READ(SONIC_ISR) & SONIC_IMR_DEFAULT;
323         if (!status) {
324                 spin_unlock_irqrestore(&lp->lock, flags);
325
326                 return IRQ_NONE;
327         }
328
329         do {
330                 SONIC_WRITE(SONIC_ISR, status); /* clear the interrupt(s) */
331
332                 if (status & SONIC_INT_PKTRX) {
333                         netif_dbg(lp, intr, dev, "%s: packet rx\n", __func__);
334                         sonic_rx(dev);  /* got packet(s) */
335                 }
336
337                 if (status & SONIC_INT_TXDN) {
338                         int entry = lp->cur_tx;
339                         int td_status;
340                         int freed_some = 0;
341
342                         /* The state of a Transmit Descriptor may be inferred
343                          * from { tx_skb[entry], td_status } as follows.
344                          * { clear, clear } => the TD has never been used
345                          * { set,   clear } => the TD was handed to SONIC
346                          * { set,   set   } => the TD was handed back
347                          * { clear, set   } => the TD is available for re-use
348                          */
349
350                         netif_dbg(lp, intr, dev, "%s: tx done\n", __func__);
351
352                         while (lp->tx_skb[entry] != NULL) {
353                                 if ((td_status = sonic_tda_get(dev, entry, SONIC_TD_STATUS)) == 0)
354                                         break;
355
356                                 if (td_status & SONIC_TCR_PTX) {
357                                         lp->stats.tx_packets++;
358                                         lp->stats.tx_bytes += sonic_tda_get(dev, entry, SONIC_TD_PKTSIZE);
359                                 } else {
360                                         if (td_status & (SONIC_TCR_EXD |
361                                             SONIC_TCR_EXC | SONIC_TCR_BCM))
362                                                 lp->stats.tx_aborted_errors++;
363                                         if (td_status &
364                                             (SONIC_TCR_NCRS | SONIC_TCR_CRLS))
365                                                 lp->stats.tx_carrier_errors++;
366                                         if (td_status & SONIC_TCR_OWC)
367                                                 lp->stats.tx_window_errors++;
368                                         if (td_status & SONIC_TCR_FU)
369                                                 lp->stats.tx_fifo_errors++;
370                                 }
371
372                                 /* We must free the original skb */
373                                 dev_kfree_skb_irq(lp->tx_skb[entry]);
374                                 lp->tx_skb[entry] = NULL;
375                                 /* and unmap DMA buffer */
376                                 dma_unmap_single(lp->device, lp->tx_laddr[entry], lp->tx_len[entry], DMA_TO_DEVICE);
377                                 lp->tx_laddr[entry] = (dma_addr_t)0;
378                                 freed_some = 1;
379
380                                 if (sonic_tda_get(dev, entry, SONIC_TD_LINK) & SONIC_EOL) {
381                                         entry = (entry + 1) & SONIC_TDS_MASK;
382                                         break;
383                                 }
384                                 entry = (entry + 1) & SONIC_TDS_MASK;
385                         }
386
387                         if (freed_some || lp->tx_skb[entry] == NULL)
388                                 netif_wake_queue(dev);  /* The ring is no longer full */
389                         lp->cur_tx = entry;
390                 }
391
392                 /*
393                  * check error conditions
394                  */
395                 if (status & SONIC_INT_RFO) {
396                         netif_dbg(lp, rx_err, dev, "%s: rx fifo overrun\n",
397                                   __func__);
398                 }
399                 if (status & SONIC_INT_RDE) {
400                         netif_dbg(lp, rx_err, dev, "%s: rx descriptors exhausted\n",
401                                   __func__);
402                 }
403                 if (status & SONIC_INT_RBAE) {
404                         netif_dbg(lp, rx_err, dev, "%s: rx buffer area exceeded\n",
405                                   __func__);
406                 }
407
408                 /* counter overruns; all counters are 16bit wide */
409                 if (status & SONIC_INT_FAE)
410                         lp->stats.rx_frame_errors += 65536;
411                 if (status & SONIC_INT_CRC)
412                         lp->stats.rx_crc_errors += 65536;
413                 if (status & SONIC_INT_MP)
414                         lp->stats.rx_missed_errors += 65536;
415
416                 /* transmit error */
417                 if (status & SONIC_INT_TXER)
418                         if (SONIC_READ(SONIC_TCR) & SONIC_TCR_FU)
419                                 netif_dbg(lp, tx_err, dev, "%s: tx fifo underrun\n",
420                                           __func__);
421
422                 /* bus retry */
423                 if (status & SONIC_INT_BR) {
424                         printk(KERN_ERR "%s: Bus retry occurred! Device interrupt disabled.\n",
425                                 dev->name);
426                         /* ... to help debug DMA problems causing endless interrupts. */
427                         /* Bounce the eth interface to turn on the interrupt again. */
428                         SONIC_WRITE(SONIC_IMR, 0);
429                 }
430
431                 status = SONIC_READ(SONIC_ISR) & SONIC_IMR_DEFAULT;
432         } while (status);
433
434         spin_unlock_irqrestore(&lp->lock, flags);
435
436         return IRQ_HANDLED;
437 }
438
439 /* Return the array index corresponding to a given Receive Buffer pointer. */
440 static int index_from_addr(struct sonic_local *lp, dma_addr_t addr,
441                            unsigned int last)
442 {
443         unsigned int i = last;
444
445         do {
446                 i = (i + 1) & SONIC_RRS_MASK;
447                 if (addr == lp->rx_laddr[i])
448                         return i;
449         } while (i != last);
450
451         return -ENOENT;
452 }
453
454 /* Allocate and map a new skb to be used as a receive buffer. */
455 static bool sonic_alloc_rb(struct net_device *dev, struct sonic_local *lp,
456                            struct sk_buff **new_skb, dma_addr_t *new_addr)
457 {
458         *new_skb = netdev_alloc_skb(dev, SONIC_RBSIZE + 2);
459         if (!*new_skb)
460                 return false;
461
462         if (SONIC_BUS_SCALE(lp->dma_bitmode) == 2)
463                 skb_reserve(*new_skb, 2);
464
465         *new_addr = dma_map_single(lp->device, skb_put(*new_skb, SONIC_RBSIZE),
466                                    SONIC_RBSIZE, DMA_FROM_DEVICE);
467         if (!*new_addr) {
468                 dev_kfree_skb(*new_skb);
469                 *new_skb = NULL;
470                 return false;
471         }
472
473         return true;
474 }
475
476 /* Place a new receive resource in the Receive Resource Area and update RWP. */
477 static void sonic_update_rra(struct net_device *dev, struct sonic_local *lp,
478                              dma_addr_t old_addr, dma_addr_t new_addr)
479 {
480         unsigned int entry = sonic_rr_entry(dev, SONIC_READ(SONIC_RWP));
481         unsigned int end = sonic_rr_entry(dev, SONIC_READ(SONIC_RRP));
482         u32 buf;
483
484         /* The resources in the range [RRP, RWP) belong to the SONIC. This loop
485          * scans the other resources in the RRA, those in the range [RWP, RRP).
486          */
487         do {
488                 buf = (sonic_rra_get(dev, entry, SONIC_RR_BUFADR_H) << 16) |
489                       sonic_rra_get(dev, entry, SONIC_RR_BUFADR_L);
490
491                 if (buf == old_addr)
492                         break;
493
494                 entry = (entry + 1) & SONIC_RRS_MASK;
495         } while (entry != end);
496
497         WARN_ONCE(buf != old_addr, "failed to find resource!\n");
498
499         sonic_rra_put(dev, entry, SONIC_RR_BUFADR_H, new_addr >> 16);
500         sonic_rra_put(dev, entry, SONIC_RR_BUFADR_L, new_addr & 0xffff);
501
502         entry = (entry + 1) & SONIC_RRS_MASK;
503
504         SONIC_WRITE(SONIC_RWP, sonic_rr_addr(dev, entry));
505 }
506
507 /*
508  * We have a good packet(s), pass it/them up the network stack.
509  */
510 static void sonic_rx(struct net_device *dev)
511 {
512         struct sonic_local *lp = netdev_priv(dev);
513         int entry = lp->cur_rx;
514         int prev_entry = lp->eol_rx;
515         bool rbe = false;
516
517         while (sonic_rda_get(dev, entry, SONIC_RD_IN_USE) == 0) {
518                 u16 status = sonic_rda_get(dev, entry, SONIC_RD_STATUS);
519
520                 /* If the RD has LPKT set, the chip has finished with the RB */
521                 if ((status & SONIC_RCR_PRX) && (status & SONIC_RCR_LPKT)) {
522                         struct sk_buff *new_skb;
523                         dma_addr_t new_laddr;
524                         u32 addr = (sonic_rda_get(dev, entry,
525                                                   SONIC_RD_PKTPTR_H) << 16) |
526                                    sonic_rda_get(dev, entry, SONIC_RD_PKTPTR_L);
527                         int i = index_from_addr(lp, addr, entry);
528
529                         if (i < 0) {
530                                 WARN_ONCE(1, "failed to find buffer!\n");
531                                 break;
532                         }
533
534                         if (sonic_alloc_rb(dev, lp, &new_skb, &new_laddr)) {
535                                 struct sk_buff *used_skb = lp->rx_skb[i];
536                                 int pkt_len;
537
538                                 /* Pass the used buffer up the stack */
539                                 dma_unmap_single(lp->device, addr, SONIC_RBSIZE,
540                                                  DMA_FROM_DEVICE);
541
542                                 pkt_len = sonic_rda_get(dev, entry,
543                                                         SONIC_RD_PKTLEN);
544                                 skb_trim(used_skb, pkt_len);
545                                 used_skb->protocol = eth_type_trans(used_skb,
546                                                                     dev);
547                                 netif_rx(used_skb);
548                                 lp->stats.rx_packets++;
549                                 lp->stats.rx_bytes += pkt_len;
550
551                                 lp->rx_skb[i] = new_skb;
552                                 lp->rx_laddr[i] = new_laddr;
553                         } else {
554                                 /* Failed to obtain a new buffer so re-use it */
555                                 new_laddr = addr;
556                                 lp->stats.rx_dropped++;
557                         }
558                         /* If RBE is already asserted when RWP advances then
559                          * it's safe to clear RBE after processing this packet.
560                          */
561                         rbe = rbe || SONIC_READ(SONIC_ISR) & SONIC_INT_RBE;
562                         sonic_update_rra(dev, lp, addr, new_laddr);
563                 }
564                 /*
565                  * give back the descriptor
566                  */
567                 sonic_rda_put(dev, entry, SONIC_RD_STATUS, 0);
568                 sonic_rda_put(dev, entry, SONIC_RD_IN_USE, 1);
569
570                 prev_entry = entry;
571                 entry = (entry + 1) & SONIC_RDS_MASK;
572         }
573
574         lp->cur_rx = entry;
575
576         if (prev_entry != lp->eol_rx) {
577                 /* Advance the EOL flag to put descriptors back into service */
578                 sonic_rda_put(dev, prev_entry, SONIC_RD_LINK, SONIC_EOL |
579                               sonic_rda_get(dev, prev_entry, SONIC_RD_LINK));
580                 sonic_rda_put(dev, lp->eol_rx, SONIC_RD_LINK, ~SONIC_EOL &
581                               sonic_rda_get(dev, lp->eol_rx, SONIC_RD_LINK));
582                 lp->eol_rx = prev_entry;
583         }
584
585         if (rbe)
586                 SONIC_WRITE(SONIC_ISR, SONIC_INT_RBE);
587         /*
588          * If any worth-while packets have been received, netif_rx()
589          * has done a mark_bh(NET_BH) for us and will work on them
590          * when we get to the bottom-half routine.
591          */
592 }
593
594
595 /*
596  * Get the current statistics.
597  * This may be called with the device open or closed.
598  */
599 static struct net_device_stats *sonic_get_stats(struct net_device *dev)
600 {
601         struct sonic_local *lp = netdev_priv(dev);
602
603         /* read the tally counter from the SONIC and reset them */
604         lp->stats.rx_crc_errors += SONIC_READ(SONIC_CRCT);
605         SONIC_WRITE(SONIC_CRCT, 0xffff);
606         lp->stats.rx_frame_errors += SONIC_READ(SONIC_FAET);
607         SONIC_WRITE(SONIC_FAET, 0xffff);
608         lp->stats.rx_missed_errors += SONIC_READ(SONIC_MPT);
609         SONIC_WRITE(SONIC_MPT, 0xffff);
610
611         return &lp->stats;
612 }
613
614
615 /*
616  * Set or clear the multicast filter for this adaptor.
617  */
618 static void sonic_multicast_list(struct net_device *dev)
619 {
620         struct sonic_local *lp = netdev_priv(dev);
621         unsigned int rcr;
622         struct netdev_hw_addr *ha;
623         unsigned char *addr;
624         int i;
625
626         rcr = SONIC_READ(SONIC_RCR) & ~(SONIC_RCR_PRO | SONIC_RCR_AMC);
627         rcr |= SONIC_RCR_BRD;   /* accept broadcast packets */
628
629         if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
630                 rcr |= SONIC_RCR_PRO;
631         } else {
632                 if ((dev->flags & IFF_ALLMULTI) ||
633                     (netdev_mc_count(dev) > 15)) {
634                         rcr |= SONIC_RCR_AMC;
635                 } else {
636                         unsigned long flags;
637
638                         netif_dbg(lp, ifup, dev, "%s: mc_count %d\n", __func__,
639                                   netdev_mc_count(dev));
640                         sonic_set_cam_enable(dev, 1);  /* always enable our own address */
641                         i = 1;
642                         netdev_for_each_mc_addr(ha, dev) {
643                                 addr = ha->addr;
644                                 sonic_cda_put(dev, i, SONIC_CD_CAP0, addr[1] << 8 | addr[0]);
645                                 sonic_cda_put(dev, i, SONIC_CD_CAP1, addr[3] << 8 | addr[2]);
646                                 sonic_cda_put(dev, i, SONIC_CD_CAP2, addr[5] << 8 | addr[4]);
647                                 sonic_set_cam_enable(dev, sonic_get_cam_enable(dev) | (1 << i));
648                                 i++;
649                         }
650                         SONIC_WRITE(SONIC_CDC, 16);
651                         SONIC_WRITE(SONIC_CDP, lp->cda_laddr & 0xffff);
652
653                         /* LCAM and TXP commands can't be used simultaneously */
654                         spin_lock_irqsave(&lp->lock, flags);
655                         sonic_quiesce(dev, SONIC_CR_TXP);
656                         SONIC_WRITE(SONIC_CMD, SONIC_CR_LCAM);
657                         sonic_quiesce(dev, SONIC_CR_LCAM);
658                         spin_unlock_irqrestore(&lp->lock, flags);
659                 }
660         }
661
662         netif_dbg(lp, ifup, dev, "%s: setting RCR=%x\n", __func__, rcr);
663
664         SONIC_WRITE(SONIC_RCR, rcr);
665 }
666
667
668 /*
669  * Initialize the SONIC ethernet controller.
670  */
671 static int sonic_init(struct net_device *dev)
672 {
673         struct sonic_local *lp = netdev_priv(dev);
674         int i;
675
676         /*
677          * put the Sonic into software-reset mode and
678          * disable all interrupts
679          */
680         SONIC_WRITE(SONIC_IMR, 0);
681         SONIC_WRITE(SONIC_ISR, 0x7fff);
682         SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
683
684         /* While in reset mode, clear CAM Enable register */
685         SONIC_WRITE(SONIC_CE, 0);
686
687         /*
688          * clear software reset flag, disable receiver, clear and
689          * enable interrupts, then completely initialize the SONIC
690          */
691         SONIC_WRITE(SONIC_CMD, 0);
692         SONIC_WRITE(SONIC_CMD, SONIC_CR_RXDIS | SONIC_CR_STP);
693         sonic_quiesce(dev, SONIC_CR_ALL);
694
695         /*
696          * initialize the receive resource area
697          */
698         netif_dbg(lp, ifup, dev, "%s: initialize receive resource area\n",
699                   __func__);
700
701         for (i = 0; i < SONIC_NUM_RRS; i++) {
702                 u16 bufadr_l = (unsigned long)lp->rx_laddr[i] & 0xffff;
703                 u16 bufadr_h = (unsigned long)lp->rx_laddr[i] >> 16;
704                 sonic_rra_put(dev, i, SONIC_RR_BUFADR_L, bufadr_l);
705                 sonic_rra_put(dev, i, SONIC_RR_BUFADR_H, bufadr_h);
706                 sonic_rra_put(dev, i, SONIC_RR_BUFSIZE_L, SONIC_RBSIZE >> 1);
707                 sonic_rra_put(dev, i, SONIC_RR_BUFSIZE_H, 0);
708         }
709
710         /* initialize all RRA registers */
711         SONIC_WRITE(SONIC_RSA, sonic_rr_addr(dev, 0));
712         SONIC_WRITE(SONIC_REA, sonic_rr_addr(dev, SONIC_NUM_RRS));
713         SONIC_WRITE(SONIC_RRP, sonic_rr_addr(dev, 0));
714         SONIC_WRITE(SONIC_RWP, sonic_rr_addr(dev, SONIC_NUM_RRS - 1));
715         SONIC_WRITE(SONIC_URRA, lp->rra_laddr >> 16);
716         SONIC_WRITE(SONIC_EOBC, (SONIC_RBSIZE >> 1) - (lp->dma_bitmode ? 2 : 1));
717
718         /* load the resource pointers */
719         netif_dbg(lp, ifup, dev, "%s: issuing RRRA command\n", __func__);
720
721         SONIC_WRITE(SONIC_CMD, SONIC_CR_RRRA);
722         sonic_quiesce(dev, SONIC_CR_RRRA);
723
724         /*
725          * Initialize the receive descriptors so that they
726          * become a circular linked list, ie. let the last
727          * descriptor point to the first again.
728          */
729         netif_dbg(lp, ifup, dev, "%s: initialize receive descriptors\n",
730                   __func__);
731
732         for (i=0; i<SONIC_NUM_RDS; i++) {
733                 sonic_rda_put(dev, i, SONIC_RD_STATUS, 0);
734                 sonic_rda_put(dev, i, SONIC_RD_PKTLEN, 0);
735                 sonic_rda_put(dev, i, SONIC_RD_PKTPTR_L, 0);
736                 sonic_rda_put(dev, i, SONIC_RD_PKTPTR_H, 0);
737                 sonic_rda_put(dev, i, SONIC_RD_SEQNO, 0);
738                 sonic_rda_put(dev, i, SONIC_RD_IN_USE, 1);
739                 sonic_rda_put(dev, i, SONIC_RD_LINK,
740                         lp->rda_laddr +
741                         ((i+1) * SIZEOF_SONIC_RD * SONIC_BUS_SCALE(lp->dma_bitmode)));
742         }
743         /* fix last descriptor */
744         sonic_rda_put(dev, SONIC_NUM_RDS - 1, SONIC_RD_LINK,
745                 (lp->rda_laddr & 0xffff) | SONIC_EOL);
746         lp->eol_rx = SONIC_NUM_RDS - 1;
747         lp->cur_rx = 0;
748         SONIC_WRITE(SONIC_URDA, lp->rda_laddr >> 16);
749         SONIC_WRITE(SONIC_CRDA, lp->rda_laddr & 0xffff);
750
751         /*
752          * initialize transmit descriptors
753          */
754         netif_dbg(lp, ifup, dev, "%s: initialize transmit descriptors\n",
755                   __func__);
756
757         for (i = 0; i < SONIC_NUM_TDS; i++) {
758                 sonic_tda_put(dev, i, SONIC_TD_STATUS, 0);
759                 sonic_tda_put(dev, i, SONIC_TD_CONFIG, 0);
760                 sonic_tda_put(dev, i, SONIC_TD_PKTSIZE, 0);
761                 sonic_tda_put(dev, i, SONIC_TD_FRAG_COUNT, 0);
762                 sonic_tda_put(dev, i, SONIC_TD_LINK,
763                         (lp->tda_laddr & 0xffff) +
764                         (i + 1) * SIZEOF_SONIC_TD * SONIC_BUS_SCALE(lp->dma_bitmode));
765                 lp->tx_skb[i] = NULL;
766         }
767         /* fix last descriptor */
768         sonic_tda_put(dev, SONIC_NUM_TDS - 1, SONIC_TD_LINK,
769                 (lp->tda_laddr & 0xffff));
770
771         SONIC_WRITE(SONIC_UTDA, lp->tda_laddr >> 16);
772         SONIC_WRITE(SONIC_CTDA, lp->tda_laddr & 0xffff);
773         lp->cur_tx = lp->next_tx = 0;
774         lp->eol_tx = SONIC_NUM_TDS - 1;
775
776         /*
777          * put our own address to CAM desc[0]
778          */
779         sonic_cda_put(dev, 0, SONIC_CD_CAP0, dev->dev_addr[1] << 8 | dev->dev_addr[0]);
780         sonic_cda_put(dev, 0, SONIC_CD_CAP1, dev->dev_addr[3] << 8 | dev->dev_addr[2]);
781         sonic_cda_put(dev, 0, SONIC_CD_CAP2, dev->dev_addr[5] << 8 | dev->dev_addr[4]);
782         sonic_set_cam_enable(dev, 1);
783
784         for (i = 0; i < 16; i++)
785                 sonic_cda_put(dev, i, SONIC_CD_ENTRY_POINTER, i);
786
787         /*
788          * initialize CAM registers
789          */
790         SONIC_WRITE(SONIC_CDP, lp->cda_laddr & 0xffff);
791         SONIC_WRITE(SONIC_CDC, 16);
792
793         /*
794          * load the CAM
795          */
796         SONIC_WRITE(SONIC_CMD, SONIC_CR_LCAM);
797         sonic_quiesce(dev, SONIC_CR_LCAM);
798
799         /*
800          * enable receiver, disable loopback
801          * and enable all interrupts
802          */
803         SONIC_WRITE(SONIC_RCR, SONIC_RCR_DEFAULT);
804         SONIC_WRITE(SONIC_TCR, SONIC_TCR_DEFAULT);
805         SONIC_WRITE(SONIC_ISR, 0x7fff);
806         SONIC_WRITE(SONIC_IMR, SONIC_IMR_DEFAULT);
807         SONIC_WRITE(SONIC_CMD, SONIC_CR_RXEN);
808
809         netif_dbg(lp, ifup, dev, "%s: new status=%x\n", __func__,
810                   SONIC_READ(SONIC_CMD));
811
812         return 0;
813 }
814
815 MODULE_LICENSE("GPL");