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