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