File: /usr/src/linux/drivers/net/sonic.c
1 /*
2 * sonic.c
3 *
4 * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
5 *
6 * This driver is based on work from Andreas Busse, but most of
7 * the code is rewritten.
8 *
9 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
10 *
11 * Core code included by system sonic drivers
12 */
13
14 /*
15 * Sources: Olivetti M700-10 Risc Personal Computer hardware handbook,
16 * National Semiconductors data sheet for the DP83932B Sonic Ethernet
17 * controller, and the files "8390.c" and "skeleton.c" in this directory.
18 */
19
20
21
22 /*
23 * Open/initialize the SONIC controller.
24 *
25 * This routine should set everything up anew at each open, even
26 * registers that "should" only need to be set once at boot, so that
27 * there is non-reboot way to recover if something goes wrong.
28 */
29 static int sonic_open(struct net_device *dev)
30 {
31 if (sonic_debug > 2)
32 printk("sonic_open: initializing sonic driver.\n");
33
34 /*
35 * We don't need to deal with auto-irq stuff since we
36 * hardwire the sonic interrupt.
37 */
38 /*
39 * XXX Horrible work around: We install sonic_interrupt as fast interrupt.
40 * This means that during execution of the handler interrupt are disabled
41 * covering another bug otherwise corrupting data. This doesn't mean
42 * this glue works ok under all situations.
43 */
44 // if (sonic_request_irq(dev->irq, &sonic_interrupt, 0, "sonic", dev)) {
45 if (sonic_request_irq(dev->irq, &sonic_interrupt, SA_INTERRUPT, "sonic", dev)) {
46 printk ("\n%s: unable to get IRQ %d .\n", dev->name, dev->irq);
47 return -EAGAIN;
48 }
49
50 /*
51 * Initialize the SONIC
52 */
53 sonic_init(dev);
54
55 dev->tbusy = 0;
56 dev->interrupt = 0;
57 dev->start = 1;
58
59 if (sonic_debug > 2)
60 printk("sonic_open: Initialization done.\n");
61
62 return 0;
63 }
64
65
66 /*
67 * Close the SONIC device
68 */
69 static int
70 sonic_close(struct net_device *dev)
71 {
72 unsigned int base_addr = dev->base_addr;
73
74 if (sonic_debug > 2)
75 printk ("sonic_close\n");
76
77 dev->tbusy = 1;
78 dev->start = 0;
79
80 /*
81 * stop the SONIC, disable interrupts
82 */
83 SONIC_WRITE(SONIC_ISR,0x7fff);
84 SONIC_WRITE(SONIC_IMR,0);
85 SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
86
87 sonic_free_irq(dev->irq, dev); /* release the IRQ */
88
89 return 0;
90 }
91
92
93 /*
94 * transmit packet
95 */
96 static int sonic_send_packet(struct sk_buff *skb, struct net_device *dev)
97 {
98 struct sonic_local *lp = (struct sonic_local *)dev->priv;
99 unsigned int base_addr = dev->base_addr;
100 unsigned int laddr;
101 int entry,length;
102
103 if (sonic_debug > 2)
104 printk("sonic_send_packet: skb=%p, dev=%p\n",skb,dev);
105
106 if (dev->tbusy) {
107 int tickssofar = jiffies - dev->trans_start;
108
109 /* If we get here, some higher level has decided we are broken.
110 There should really be a "kick me" function call instead. */
111
112 if (sonic_debug > 1)
113 printk("sonic_send_packet: called with dev->tbusy = 1 !\n");
114
115 if (tickssofar < 5)
116 return 1;
117
118 printk("%s: transmit timed out.\n", dev->name);
119
120 /* Try to restart the adaptor. */
121 sonic_init(dev);
122 dev->tbusy=0;
123 dev->trans_start = jiffies;
124 }
125
126 /*
127 * Block a timer-based transmit from overlapping. This could better be
128 * done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
129 */
130 if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
131 printk("%s: Transmitter access conflict.\n", dev->name);
132 return 1;
133 }
134
135 /*
136 * Map the packet data into the logical DMA address space
137 */
138 if ((laddr = vdma_alloc(PHYSADDR(skb->data),skb->len)) == ~0UL) {
139 printk("%s: no VDMA entry for transmit available.\n",dev->name);
140 dev_kfree_skb(skb);
141 dev->tbusy = 0;
142 return 1;
143 }
144 entry = lp->cur_tx & SONIC_TDS_MASK;
145 lp->tx_laddr[entry] = laddr;
146 lp->tx_skb[entry] = skb;
147
148 length = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
149 flush_cache_all();
150
151 /*
152 * Setup the transmit descriptor and issue the transmit command.
153 */
154 lp->tda[entry].tx_status = 0; /* clear status */
155 lp->tda[entry].tx_frag_count = 1; /* single fragment */
156 lp->tda[entry].tx_pktsize = length; /* length of packet */
157 lp->tda[entry].tx_frag_ptr_l = laddr & 0xffff;
158 lp->tda[entry].tx_frag_ptr_h = laddr >> 16;
159 lp->tda[entry].tx_frag_size = length;
160 lp->cur_tx++;
161 lp->stats.tx_bytes += length;
162
163 if (sonic_debug > 2)
164 printk("sonic_send_packet: issueing Tx command\n");
165
166 SONIC_WRITE(SONIC_CMD,SONIC_CR_TXP);
167
168 dev->trans_start = jiffies;
169
170 if (lp->cur_tx < lp->dirty_tx + SONIC_NUM_TDS)
171 dev->tbusy = 0;
172 else
173 lp->tx_full = 1;
174
175 return 0;
176 }
177
178
179 /*
180 * The typical workload of the driver:
181 * Handle the network interface interrupts.
182 */
183 static void
184 sonic_interrupt(int irq, void *dev_id, struct pt_regs * regs)
185 {
186 struct net_device *dev = (struct net_device *)dev_id;
187 unsigned int base_addr = dev->base_addr;
188 struct sonic_local *lp;
189 int status;
190
191 if (dev == NULL) {
192 printk ("sonic_interrupt: irq %d for unknown device.\n", irq);
193 return;
194 }
195 dev->interrupt = 1;
196 lp = (struct sonic_local *)dev->priv;
197
198 status = SONIC_READ(SONIC_ISR);
199 SONIC_WRITE(SONIC_ISR,0x7fff); /* clear all bits */
200
201 if (sonic_debug > 2)
202 printk("sonic_interrupt: ISR=%x\n",status);
203
204 if (status & SONIC_INT_PKTRX) {
205 sonic_rx(dev); /* got packet(s) */
206 }
207
208 if (status & SONIC_INT_TXDN) {
209 int dirty_tx = lp->dirty_tx;
210
211 while (dirty_tx < lp->cur_tx) {
212 int entry = dirty_tx & SONIC_TDS_MASK;
213 int status = lp->tda[entry].tx_status;
214
215 if (sonic_debug > 3)
216 printk ("sonic_interrupt: status %d, cur_tx %d, dirty_tx %d\n",
217 status,lp->cur_tx,lp->dirty_tx);
218
219 if (status == 0) {
220 /* It still hasn't been Txed, kick the sonic again */
221 SONIC_WRITE(SONIC_CMD,SONIC_CR_TXP);
222 break;
223 }
224
225 /* put back EOL and free descriptor */
226 lp->tda[entry].tx_frag_count = 0;
227 lp->tda[entry].tx_status = 0;
228
229 if (status & 0x0001)
230 lp->stats.tx_packets++;
231 else {
232 lp->stats.tx_errors++;
233 if (status & 0x0642) lp->stats.tx_aborted_errors++;
234 if (status & 0x0180) lp->stats.tx_carrier_errors++;
235 if (status & 0x0020) lp->stats.tx_window_errors++;
236 if (status & 0x0004) lp->stats.tx_fifo_errors++;
237 }
238
239 /* We must free the original skb */
240 if (lp->tx_skb[entry]) {
241 dev_kfree_skb(lp->tx_skb[entry]);
242 lp->tx_skb[entry] = 0;
243 }
244 /* and the VDMA address */
245 vdma_free(lp->tx_laddr[entry]);
246 dirty_tx++;
247 }
248
249 if (lp->tx_full && dev->tbusy
250 && dirty_tx + SONIC_NUM_TDS > lp->cur_tx + 2) {
251 /* The ring is no longer full, clear tbusy. */
252 lp->tx_full = 0;
253 dev->tbusy = 0;
254 mark_bh(NET_BH);
255 }
256
257 lp->dirty_tx = dirty_tx;
258 }
259
260 /*
261 * check error conditions
262 */
263 if (status & SONIC_INT_RFO) {
264 printk ("%s: receive fifo underrun\n",dev->name);
265 lp->stats.rx_fifo_errors++;
266 }
267 if (status & SONIC_INT_RDE) {
268 printk ("%s: receive descriptors exhausted\n",dev->name);
269 lp->stats.rx_dropped++;
270 }
271 if (status & SONIC_INT_RBE) {
272 printk ("%s: receive buffer exhausted\n",dev->name);
273 lp->stats.rx_dropped++;
274 }
275 if (status & SONIC_INT_RBAE) {
276 printk ("%s: receive buffer area exhausted\n",dev->name);
277 lp->stats.rx_dropped++;
278 }
279
280 /* counter overruns; all counters are 16bit wide */
281 if (status & SONIC_INT_FAE)
282 lp->stats.rx_frame_errors += 65536;
283 if (status & SONIC_INT_CRC)
284 lp->stats.rx_crc_errors += 65536;
285 if (status & SONIC_INT_MP)
286 lp->stats.rx_missed_errors += 65536;
287
288 /* transmit error */
289 if (status & SONIC_INT_TXER)
290 lp->stats.tx_errors++;
291
292 /*
293 * clear interrupt bits and return
294 */
295 SONIC_WRITE(SONIC_ISR,status);
296 dev->interrupt = 0;
297 return;
298 }
299
300 /*
301 * We have a good packet(s), get it/them out of the buffers.
302 */
303 static void
304 sonic_rx(struct net_device *dev)
305 {
306 unsigned int base_addr = dev->base_addr;
307 struct sonic_local *lp = (struct sonic_local *)dev->priv;
308 sonic_rd_t *rd = &lp->rda[lp->cur_rx & SONIC_RDS_MASK];
309 int status;
310
311 while (rd->in_use == 0) {
312 struct sk_buff *skb;
313 int pkt_len;
314 unsigned char *pkt_ptr;
315
316 status = rd->rx_status;
317 if (sonic_debug > 3)
318 printk ("status %x, cur_rx %d, cur_rra %x\n",status,lp->cur_rx,lp->cur_rra);
319 if (status & SONIC_RCR_PRX) {
320 pkt_len = rd->rx_pktlen;
321 pkt_ptr = (char *)sonic_chiptomem((rd->rx_pktptr_h << 16) +
322 rd->rx_pktptr_l);
323
324 if (sonic_debug > 3)
325 printk ("pktptr %p (rba %p) h:%x l:%x, bsize h:%x l:%x\n", pkt_ptr,lp->rba,
326 rd->rx_pktptr_h,rd->rx_pktptr_l,
327 SONIC_READ(SONIC_RBWC1),SONIC_READ(SONIC_RBWC0));
328
329 /* Malloc up new buffer. */
330 skb = dev_alloc_skb(pkt_len+2);
331 if (skb == NULL) {
332 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
333 lp->stats.rx_dropped++;
334 break;
335 }
336 skb->dev = dev;
337 skb_reserve(skb,2); /* 16 byte align */
338 skb_put(skb,pkt_len); /* Make room */
339 eth_copy_and_sum(skb, pkt_ptr, pkt_len, 0);
340 skb->protocol=eth_type_trans(skb,dev);
341 netif_rx(skb); /* pass the packet to upper layers */
342 dev->last_rx = jiffies;
343 lp->stats.rx_packets++;
344 lp->stats.rx_bytes += pkt_len;
345
346 } else {
347 /* This should only happen, if we enable accepting broken packets. */
348 lp->stats.rx_errors++;
349 if (status & SONIC_RCR_FAER) lp->stats.rx_frame_errors++;
350 if (status & SONIC_RCR_CRCR) lp->stats.rx_crc_errors++;
351 }
352
353 rd->in_use = 1;
354 rd = &lp->rda[(++lp->cur_rx) & SONIC_RDS_MASK];
355 /* now give back the buffer to the receive buffer area */
356 if (status & SONIC_RCR_LPKT) {
357 /*
358 * this was the last packet out of the current receice buffer
359 * give the buffer back to the SONIC
360 */
361 lp->cur_rra += sizeof(sonic_rr_t);
362 if (lp->cur_rra > (lp->rra_laddr + (SONIC_NUM_RRS-1) * sizeof(sonic_rr_t)))
363 lp->cur_rra = lp->rra_laddr;
364 SONIC_WRITE(SONIC_RWP, lp->cur_rra & 0xffff);
365 } else
366 printk ("%s: rx desc without RCR_LPKT. Shouldn't happen !?\n",dev->name);
367 }
368 /*
369 * If any worth-while packets have been received, dev_rint()
370 * has done a mark_bh(NET_BH) for us and will work on them
371 * when we get to the bottom-half routine.
372 */
373 return;
374 }
375
376
377 /*
378 * Get the current statistics.
379 * This may be called with the device open or closed.
380 */
381 static struct net_device_stats *
382 sonic_get_stats(struct net_device *dev)
383 {
384 struct sonic_local *lp = (struct sonic_local *)dev->priv;
385 unsigned int base_addr = dev->base_addr;
386
387 /* read the tally counter from the SONIC and reset them */
388 lp->stats.rx_crc_errors += SONIC_READ(SONIC_CRCT);
389 SONIC_WRITE(SONIC_CRCT,0xffff);
390 lp->stats.rx_frame_errors += SONIC_READ(SONIC_FAET);
391 SONIC_WRITE(SONIC_FAET,0xffff);
392 lp->stats.rx_missed_errors += SONIC_READ(SONIC_MPT);
393 SONIC_WRITE(SONIC_MPT,0xffff);
394
395 return &lp->stats;
396 }
397
398
399 /*
400 * Set or clear the multicast filter for this adaptor.
401 */
402 static void
403 sonic_multicast_list(struct net_device *dev)
404 {
405 struct sonic_local *lp = (struct sonic_local *)dev->priv;
406 unsigned int base_addr = dev->base_addr;
407 unsigned int rcr;
408 struct dev_mc_list *dmi = dev->mc_list;
409 unsigned char *addr;
410 int i;
411
412 rcr = SONIC_READ(SONIC_RCR) & ~(SONIC_RCR_PRO | SONIC_RCR_AMC);
413 rcr |= SONIC_RCR_BRD; /* accept broadcast packets */
414
415 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
416 rcr |= SONIC_RCR_PRO;
417 } else {
418 if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 15)) {
419 rcr |= SONIC_RCR_AMC;
420 } else {
421 if (sonic_debug > 2)
422 printk ("sonic_multicast_list: mc_count %d\n",dev->mc_count);
423 lp->cda.cam_enable = 1; /* always enable our own address */
424 for (i = 1; i <= dev->mc_count; i++) {
425 addr = dmi->dmi_addr;
426 dmi = dmi->next;
427 lp->cda.cam_desc[i].cam_cap0 = addr[1] << 8 | addr[0];
428 lp->cda.cam_desc[i].cam_cap1 = addr[3] << 8 | addr[2];
429 lp->cda.cam_desc[i].cam_cap2 = addr[5] << 8 | addr[4];
430 lp->cda.cam_enable |= (1 << i);
431 }
432 SONIC_WRITE(SONIC_CDC,16);
433 /* issue Load CAM command */
434 SONIC_WRITE(SONIC_CDP, lp->cda_laddr & 0xffff);
435 SONIC_WRITE(SONIC_CMD,SONIC_CR_LCAM);
436 }
437 }
438
439 if (sonic_debug > 2)
440 printk("sonic_multicast_list: setting RCR=%x\n",rcr);
441
442 SONIC_WRITE(SONIC_RCR,rcr);
443 }
444
445
446 /*
447 * Initialize the SONIC ethernet controller.
448 */
449 static int sonic_init(struct net_device *dev)
450 {
451 unsigned int base_addr = dev->base_addr;
452 unsigned int cmd;
453 struct sonic_local *lp = (struct sonic_local *)dev->priv;
454 unsigned int rra_start;
455 unsigned int rra_end;
456 int i;
457
458 /*
459 * put the Sonic into software-reset mode and
460 * disable all interrupts
461 */
462 SONIC_WRITE(SONIC_ISR,0x7fff);
463 SONIC_WRITE(SONIC_IMR,0);
464 SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
465
466 /*
467 * clear software reset flag, disable receiver, clear and
468 * enable interrupts, then completely initialize the SONIC
469 */
470 SONIC_WRITE(SONIC_CMD,0);
471 SONIC_WRITE(SONIC_CMD,SONIC_CR_RXDIS);
472
473 /*
474 * initialize the receive resource area
475 */
476 if (sonic_debug > 2)
477 printk ("sonic_init: initialize receive resource area\n");
478
479 rra_start = lp->rra_laddr & 0xffff;
480 rra_end = (rra_start + (SONIC_NUM_RRS * sizeof(sonic_rr_t))) & 0xffff;
481
482 for (i = 0; i < SONIC_NUM_RRS; i++) {
483 lp->rra[i].rx_bufadr_l = (lp->rba_laddr + i * SONIC_RBSIZE) & 0xffff;
484 lp->rra[i].rx_bufadr_h = (lp->rba_laddr + i * SONIC_RBSIZE) >> 16;
485 lp->rra[i].rx_bufsize_l = SONIC_RBSIZE >> 1;
486 lp->rra[i].rx_bufsize_h = 0;
487 }
488
489 /* initialize all RRA registers */
490 SONIC_WRITE(SONIC_RSA,rra_start);
491 SONIC_WRITE(SONIC_REA,rra_end);
492 SONIC_WRITE(SONIC_RRP,rra_start);
493 SONIC_WRITE(SONIC_RWP,rra_end);
494 SONIC_WRITE(SONIC_URRA,lp->rra_laddr >> 16);
495 SONIC_WRITE(SONIC_EOBC,(SONIC_RBSIZE-2) >> 1);
496
497 lp->cur_rra = lp->rra_laddr + (SONIC_NUM_RRS-1) * sizeof(sonic_rr_t);
498
499 /* load the resource pointers */
500 if (sonic_debug > 3)
501 printk("sonic_init: issueing RRRA command\n");
502
503 SONIC_WRITE(SONIC_CMD,SONIC_CR_RRRA);
504 i = 0;
505 while (i++ < 100) {
506 if (SONIC_READ(SONIC_CMD) & SONIC_CR_RRRA)
507 break;
508 }
509
510 if (sonic_debug > 2)
511 printk("sonic_init: status=%x\n",SONIC_READ(SONIC_CMD));
512
513 /*
514 * Initialize the receive descriptors so that they
515 * become a circular linked list, ie. let the last
516 * descriptor point to the first again.
517 */
518 if (sonic_debug > 2)
519 printk ("sonic_init: initialize receive descriptors\n");
520 for (i=0; i<SONIC_NUM_RDS; i++) {
521 lp->rda[i].rx_status = 0;
522 lp->rda[i].rx_pktlen = 0;
523 lp->rda[i].rx_pktptr_l = 0;
524 lp->rda[i].rx_pktptr_h = 0;
525 lp->rda[i].rx_seqno = 0;
526 lp->rda[i].in_use = 1;
527 lp->rda[i].link = lp->rda_laddr + (i+1) * sizeof (sonic_rd_t);
528 }
529 /* fix last descriptor */
530 lp->rda[SONIC_NUM_RDS-1].link = lp->rda_laddr;
531 lp->cur_rx = 0;
532 SONIC_WRITE(SONIC_URDA,lp->rda_laddr >> 16);
533 SONIC_WRITE(SONIC_CRDA,lp->rda_laddr & 0xffff);
534
535 /*
536 * initialize transmit descriptors
537 */
538 if (sonic_debug > 2)
539 printk ("sonic_init: initialize transmit descriptors\n");
540 for (i = 0; i < SONIC_NUM_TDS; i++) {
541 lp->tda[i].tx_status = 0;
542 lp->tda[i].tx_config = 0;
543 lp->tda[i].tx_pktsize = 0;
544 lp->tda[i].tx_frag_count = 0;
545 lp->tda[i].link = (lp->tda_laddr + (i+1) * sizeof (sonic_td_t)) | SONIC_END_OF_LINKS;
546 }
547 lp->tda[SONIC_NUM_TDS-1].link = (lp->tda_laddr & 0xffff) | SONIC_END_OF_LINKS;
548
549 SONIC_WRITE(SONIC_UTDA,lp->tda_laddr >> 16);
550 SONIC_WRITE(SONIC_CTDA,lp->tda_laddr & 0xffff);
551 lp->cur_tx = lp->dirty_tx = 0;
552
553 /*
554 * put our own address to CAM desc[0]
555 */
556 lp->cda.cam_desc[0].cam_cap0 = dev->dev_addr[1] << 8 | dev->dev_addr[0];
557 lp->cda.cam_desc[0].cam_cap1 = dev->dev_addr[3] << 8 | dev->dev_addr[2];
558 lp->cda.cam_desc[0].cam_cap2 = dev->dev_addr[5] << 8 | dev->dev_addr[4];
559 lp->cda.cam_enable = 1;
560
561 for (i=0; i < 16; i++)
562 lp->cda.cam_desc[i].cam_entry_pointer = i;
563
564 /*
565 * initialize CAM registers
566 */
567 SONIC_WRITE(SONIC_CDP, lp->cda_laddr & 0xffff);
568 SONIC_WRITE(SONIC_CDC,16);
569
570 /*
571 * load the CAM
572 */
573 SONIC_WRITE(SONIC_CMD,SONIC_CR_LCAM);
574
575 i = 0;
576 while (i++ < 100) {
577 if (SONIC_READ(SONIC_ISR) & SONIC_INT_LCD)
578 break;
579 }
580 if (sonic_debug > 2) {
581 printk("sonic_init: CMD=%x, ISR=%x\n",
582 SONIC_READ(SONIC_CMD),
583 SONIC_READ(SONIC_ISR));
584 }
585
586 /*
587 * enable receiver, disable loopback
588 * and enable all interrupts
589 */
590 SONIC_WRITE(SONIC_CMD,SONIC_CR_RXEN | SONIC_CR_STP);
591 SONIC_WRITE(SONIC_RCR,SONIC_RCR_DEFAULT);
592 SONIC_WRITE(SONIC_TCR,SONIC_TCR_DEFAULT);
593 SONIC_WRITE(SONIC_ISR,0x7fff);
594 SONIC_WRITE(SONIC_IMR,SONIC_IMR_DEFAULT);
595
596 cmd = SONIC_READ(SONIC_CMD);
597 if ((cmd & SONIC_CR_RXEN) == 0 ||
598 (cmd & SONIC_CR_STP) == 0)
599 printk("sonic_init: failed, status=%x\n",cmd);
600
601 if (sonic_debug > 2)
602 printk("sonic_init: new status=%x\n",SONIC_READ(SONIC_CMD));
603
604 return(0);
605 }
606
607
608 /*
609 * Local variables:
610 * compile-command: "mipsel-linux-gcc -D__KERNEL__ -D__mips64 -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O2 -mcpu=r4000 -c sonic.c"
611 * version-control: t
612 * kept-new-versions: 5
613 * tab-width: 4
614 * End:
615 */
616