blob: 687c1413c4ee5e446b7838acf849a3db9fdde771 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sonic.c
3 *
Finn Thainefcce832005-08-20 15:53:22 +10004 * (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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * This driver is based on work from Andreas Busse, but most of
12 * the code is rewritten.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
15 *
16 * Core code included by system sonic drivers
Finn Thainefcce832005-08-20 15:53:22 +100017 *
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
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.
Finn Thainefcce832005-08-20 15:53:22 +100030 *
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".
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
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 */
45static int sonic_open(struct net_device *dev)
46{
Finn Thainefcce832005-08-20 15:53:22 +100047 struct sonic_local *lp = netdev_priv(dev);
48 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 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.
Finn Thainefcce832005-08-20 15:53:22 +100062 *
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".
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
Finn Thainefcce832005-08-20 15:53:22 +100071 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return -EAGAIN;
74 }
75
Finn Thainefcce832005-08-20 15:53:22 +100076 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 }
Finn Thainefcce832005-08-20 15:53:22 +100088 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /*
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 */
131static int sonic_close(struct net_device *dev)
132{
Finn Thainefcce832005-08-20 15:53:22 +1000133 struct sonic_local *lp = netdev_priv(dev);
134 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 SONIC_WRITE(SONIC_IMR, 0);
Finn Thainefcce832005-08-20 15:53:22 +1000145 SONIC_WRITE(SONIC_ISR, 0x7fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
147
Finn Thainefcce832005-08-20 15:53:22 +1000148 /* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return 0;
175}
176
177static void sonic_tx_timeout(struct net_device *dev)
178{
Finn Thainefcce832005-08-20 15:53:22 +1000179 struct sonic_local *lp = netdev_priv(dev);
180 int i;
Finn Thaind74472f2007-05-01 22:33:02 +0200181 /*
182 * put the Sonic into software-reset mode and
183 * disable all interrupts before releasing DMA buffers
184 */
Finn Thainefcce832005-08-20 15:53:22 +1000185 SONIC_WRITE(SONIC_IMR, 0);
Finn Thaind74472f2007-05-01 22:33:02 +0200186 SONIC_WRITE(SONIC_ISR, 0x7fff);
187 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
Finn Thainefcce832005-08-20 15:53:22 +1000188 /* We could resend the original skbs. Easier to re-initialise. */
189 for (i = 0; i < SONIC_NUM_TDS; i++) {
190 if(lp->tx_laddr[i]) {
191 dma_unmap_single(lp->device, lp->tx_laddr[i], lp->tx_len[i], DMA_TO_DEVICE);
192 lp->tx_laddr[i] = (dma_addr_t)0;
193 }
194 if(lp->tx_skb[i]) {
195 dev_kfree_skb(lp->tx_skb[i]);
196 lp->tx_skb[i] = NULL;
197 }
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Try to restart the adaptor. */
200 sonic_init(dev);
201 lp->stats.tx_errors++;
202 dev->trans_start = jiffies;
203 netif_wake_queue(dev);
204}
205
206/*
207 * transmit packet
Finn Thainefcce832005-08-20 15:53:22 +1000208 *
209 * Appends new TD during transmission thus avoiding any TX interrupts
210 * until we run out of TDs.
211 * This routine interacts closely with the ISR in that it may,
212 * set tx_skb[i]
213 * reset the status flags of the new TD
214 * set and reset EOL flags
215 * stop the tx queue
216 * The ISR interacts with this routine in various ways. It may,
217 * reset tx_skb[i]
218 * test the EOL and status flags of the TDs
219 * wake the tx queue
220 * Concurrently with all of this, the SONIC is potentially writing to
221 * the status flags of the TDs.
222 * Until some mutual exclusion is added, this code will not work with SMP. However,
223 * MIPS Jazz machines and m68k Macs were all uni-processor machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
Finn Thainefcce832005-08-20 15:53:22 +1000225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static int sonic_send_packet(struct sk_buff *skb, struct net_device *dev)
227{
Finn Thainefcce832005-08-20 15:53:22 +1000228 struct sonic_local *lp = netdev_priv(dev);
229 dma_addr_t laddr;
230 int length;
231 int entry = lp->next_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (sonic_debug > 2)
234 printk("sonic_send_packet: skb=%p, dev=%p\n", skb, dev);
235
Finn Thainefcce832005-08-20 15:53:22 +1000236 length = skb->len;
237 if (length < ETH_ZLEN) {
Herbert Xu5b057c62006-06-23 02:06:41 -0700238 if (skb_padto(skb, ETH_ZLEN))
Finn Thainefcce832005-08-20 15:53:22 +1000239 return 0;
240 length = ETH_ZLEN;
241 }
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /*
244 * Map the packet data into the logical DMA address space
245 */
Finn Thainefcce832005-08-20 15:53:22 +1000246
247 laddr = dma_map_single(lp->device, skb->data, length, DMA_TO_DEVICE);
248 if (!laddr) {
249 printk(KERN_ERR "%s: failed to map tx DMA buffer.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 1;
252 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400253
Finn Thainefcce832005-08-20 15:53:22 +1000254 sonic_tda_put(dev, entry, SONIC_TD_STATUS, 0); /* clear status */
255 sonic_tda_put(dev, entry, SONIC_TD_FRAG_COUNT, 1); /* single fragment */
256 sonic_tda_put(dev, entry, SONIC_TD_PKTSIZE, length); /* length of packet */
257 sonic_tda_put(dev, entry, SONIC_TD_FRAG_PTR_L, laddr & 0xffff);
258 sonic_tda_put(dev, entry, SONIC_TD_FRAG_PTR_H, laddr >> 16);
259 sonic_tda_put(dev, entry, SONIC_TD_FRAG_SIZE, length);
260 sonic_tda_put(dev, entry, SONIC_TD_LINK,
261 sonic_tda_get(dev, entry, SONIC_TD_LINK) | SONIC_EOL);
262
263 /*
264 * Must set tx_skb[entry] only after clearing status, and
265 * before clearing EOL and before stopping queue
266 */
267 wmb();
268 lp->tx_len[entry] = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 lp->tx_laddr[entry] = laddr;
270 lp->tx_skb[entry] = skb;
271
Finn Thainefcce832005-08-20 15:53:22 +1000272 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Finn Thainefcce832005-08-20 15:53:22 +1000277 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (sonic_debug > 2)
Finn Thainefcce832005-08-20 15:53:22 +1000287 printk("sonic_send_packet: issuing Tx command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 SONIC_WRITE(SONIC_CMD, SONIC_CR_TXP);
290
291 dev->trans_start = jiffies;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294}
295
296/*
297 * The typical workload of the driver:
298 * Handle the network interface interrupts.
299 */
David Howells7d12e782006-10-05 14:55:46 +0100300static irqreturn_t sonic_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Jeff Garzikc31f28e2006-10-06 14:56:04 -0400302 struct net_device *dev = dev_id;
Finn Thainefcce832005-08-20 15:53:22 +1000303 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int status;
305
Finn Thainefcce832005-08-20 15:53:22 +1000306 if (!(status = SONIC_READ(SONIC_ISR) & SONIC_IMR_DEFAULT))
307 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Finn Thainefcce832005-08-20 15:53:22 +1000309 do {
310 if (status & SONIC_INT_PKTRX) {
311 if (sonic_debug > 2)
312 printk("%s: packet rx\n", dev->name);
313 sonic_rx(dev); /* got packet(s) */
314 SONIC_WRITE(SONIC_ISR, SONIC_INT_PKTRX); /* clear the interrupt */
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Finn Thainefcce832005-08-20 15:53:22 +1000317 if (status & SONIC_INT_TXDN) {
318 int entry = lp->cur_tx;
319 int td_status;
320 int freed_some = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Finn Thainefcce832005-08-20 15:53:22 +1000322 /* At this point, cur_tx is the index of a TD that is one of:
323 * unallocated/freed (status set & tx_skb[entry] clear)
324 * allocated and sent (status set & tx_skb[entry] set )
325 * allocated and not yet sent (status clear & tx_skb[entry] set )
326 * still being allocated by sonic_send_packet (status clear & tx_skb[entry] clear)
327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Finn Thainefcce832005-08-20 15:53:22 +1000329 if (sonic_debug > 2)
330 printk("%s: tx done\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Finn Thainefcce832005-08-20 15:53:22 +1000332 while (lp->tx_skb[entry] != NULL) {
333 if ((td_status = sonic_tda_get(dev, entry, SONIC_TD_STATUS)) == 0)
334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Finn Thainefcce832005-08-20 15:53:22 +1000336 if (td_status & 0x0001) {
337 lp->stats.tx_packets++;
338 lp->stats.tx_bytes += sonic_tda_get(dev, entry, SONIC_TD_PKTSIZE);
339 } else {
340 lp->stats.tx_errors++;
341 if (td_status & 0x0642)
342 lp->stats.tx_aborted_errors++;
343 if (td_status & 0x0180)
344 lp->stats.tx_carrier_errors++;
345 if (td_status & 0x0020)
346 lp->stats.tx_window_errors++;
347 if (td_status & 0x0004)
348 lp->stats.tx_fifo_errors++;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Finn Thainefcce832005-08-20 15:53:22 +1000351 /* We must free the original skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 dev_kfree_skb_irq(lp->tx_skb[entry]);
Finn Thainefcce832005-08-20 15:53:22 +1000353 lp->tx_skb[entry] = NULL;
354 /* and unmap DMA buffer */
355 dma_unmap_single(lp->device, lp->tx_laddr[entry], lp->tx_len[entry], DMA_TO_DEVICE);
356 lp->tx_laddr[entry] = (dma_addr_t)0;
357 freed_some = 1;
358
359 if (sonic_tda_get(dev, entry, SONIC_TD_LINK) & SONIC_EOL) {
360 entry = (entry + 1) & SONIC_TDS_MASK;
361 break;
362 }
363 entry = (entry + 1) & SONIC_TDS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Finn Thainefcce832005-08-20 15:53:22 +1000365
366 if (freed_some || lp->tx_skb[entry] == NULL)
367 netif_wake_queue(dev); /* The ring is no longer full */
368 lp->cur_tx = entry;
369 SONIC_WRITE(SONIC_ISR, SONIC_INT_TXDN); /* clear the interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371
Finn Thainefcce832005-08-20 15:53:22 +1000372 /*
373 * check error conditions
374 */
375 if (status & SONIC_INT_RFO) {
376 if (sonic_debug > 1)
377 printk("%s: rx fifo overrun\n", dev->name);
378 lp->stats.rx_fifo_errors++;
379 SONIC_WRITE(SONIC_ISR, SONIC_INT_RFO); /* clear the interrupt */
380 }
381 if (status & SONIC_INT_RDE) {
382 if (sonic_debug > 1)
383 printk("%s: rx descriptors exhausted\n", dev->name);
384 lp->stats.rx_dropped++;
385 SONIC_WRITE(SONIC_ISR, SONIC_INT_RDE); /* clear the interrupt */
386 }
387 if (status & SONIC_INT_RBAE) {
388 if (sonic_debug > 1)
389 printk("%s: rx buffer area exceeded\n", dev->name);
390 lp->stats.rx_dropped++;
391 SONIC_WRITE(SONIC_ISR, SONIC_INT_RBAE); /* clear the interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Finn Thainefcce832005-08-20 15:53:22 +1000394 /* counter overruns; all counters are 16bit wide */
395 if (status & SONIC_INT_FAE) {
396 lp->stats.rx_frame_errors += 65536;
397 SONIC_WRITE(SONIC_ISR, SONIC_INT_FAE); /* clear the interrupt */
398 }
399 if (status & SONIC_INT_CRC) {
400 lp->stats.rx_crc_errors += 65536;
401 SONIC_WRITE(SONIC_ISR, SONIC_INT_CRC); /* clear the interrupt */
402 }
403 if (status & SONIC_INT_MP) {
404 lp->stats.rx_missed_errors += 65536;
405 SONIC_WRITE(SONIC_ISR, SONIC_INT_MP); /* clear the interrupt */
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Finn Thainefcce832005-08-20 15:53:22 +1000408 /* transmit error */
409 if (status & SONIC_INT_TXER) {
410 if ((SONIC_READ(SONIC_TCR) & SONIC_TCR_FU) && (sonic_debug > 2))
411 printk(KERN_ERR "%s: tx fifo underrun\n", dev->name);
412 SONIC_WRITE(SONIC_ISR, SONIC_INT_TXER); /* clear the interrupt */
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Finn Thainefcce832005-08-20 15:53:22 +1000415 /* bus retry */
416 if (status & SONIC_INT_BR) {
417 printk(KERN_ERR "%s: Bus retry occurred! Device interrupt disabled.\n",
418 dev->name);
419 /* ... to help debug DMA problems causing endless interrupts. */
420 /* Bounce the eth interface to turn on the interrupt again. */
421 SONIC_WRITE(SONIC_IMR, 0);
422 SONIC_WRITE(SONIC_ISR, SONIC_INT_BR); /* clear the interrupt */
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Finn Thainefcce832005-08-20 15:53:22 +1000425 /* load CAM done */
426 if (status & SONIC_INT_LCD)
427 SONIC_WRITE(SONIC_ISR, SONIC_INT_LCD); /* clear the interrupt */
428 } while((status = SONIC_READ(SONIC_ISR) & SONIC_IMR_DEFAULT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return IRQ_HANDLED;
430}
431
432/*
Finn Thainefcce832005-08-20 15:53:22 +1000433 * We have a good packet(s), pass it/them up the network stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
435static void sonic_rx(struct net_device *dev)
436{
Finn Thainefcce832005-08-20 15:53:22 +1000437 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int status;
Finn Thainefcce832005-08-20 15:53:22 +1000439 int entry = lp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Finn Thainefcce832005-08-20 15:53:22 +1000441 while (sonic_rda_get(dev, entry, SONIC_RD_IN_USE) == 0) {
442 struct sk_buff *used_skb;
443 struct sk_buff *new_skb;
444 dma_addr_t new_laddr;
445 u16 bufadr_l;
446 u16 bufadr_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Finn Thainefcce832005-08-20 15:53:22 +1000449 status = sonic_rda_get(dev, entry, SONIC_RD_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (status & SONIC_RCR_PRX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* Malloc up new buffer. */
Finn Thainefcce832005-08-20 15:53:22 +1000452 new_skb = dev_alloc_skb(SONIC_RBSIZE + 2);
453 if (new_skb == NULL) {
454 printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 lp->stats.rx_dropped++;
456 break;
457 }
Finn Thainefcce832005-08-20 15:53:22 +1000458 /* provide 16 byte IP header alignment unless DMA requires otherwise */
459 if(SONIC_BUS_SCALE(lp->dma_bitmode) == 2)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400460 skb_reserve(new_skb, 2);
Finn Thainefcce832005-08-20 15:53:22 +1000461
462 new_laddr = dma_map_single(lp->device, skb_put(new_skb, SONIC_RBSIZE),
463 SONIC_RBSIZE, DMA_FROM_DEVICE);
464 if (!new_laddr) {
465 dev_kfree_skb(new_skb);
466 printk(KERN_ERR "%s: Failed to map rx buffer, dropping packet.\n", dev->name);
467 lp->stats.rx_dropped++;
468 break;
469 }
470
471 /* now we have a new skb to replace it, pass the used one up the stack */
472 dma_unmap_single(lp->device, lp->rx_laddr[entry], SONIC_RBSIZE, DMA_FROM_DEVICE);
473 used_skb = lp->rx_skb[entry];
474 pkt_len = sonic_rda_get(dev, entry, SONIC_RD_PKTLEN);
475 skb_trim(used_skb, pkt_len);
476 used_skb->protocol = eth_type_trans(used_skb, dev);
477 netif_rx(used_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 dev->last_rx = jiffies;
479 lp->stats.rx_packets++;
480 lp->stats.rx_bytes += pkt_len;
481
Finn Thainefcce832005-08-20 15:53:22 +1000482 /* and insert the new skb */
483 lp->rx_laddr[entry] = new_laddr;
484 lp->rx_skb[entry] = new_skb;
485
486 bufadr_l = (unsigned long)new_laddr & 0xffff;
487 bufadr_h = (unsigned long)new_laddr >> 16;
488 sonic_rra_put(dev, entry, SONIC_RR_BUFADR_L, bufadr_l);
489 sonic_rra_put(dev, entry, SONIC_RR_BUFADR_H, bufadr_h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 } else {
491 /* This should only happen, if we enable accepting broken packets. */
492 lp->stats.rx_errors++;
493 if (status & SONIC_RCR_FAER)
494 lp->stats.rx_frame_errors++;
495 if (status & SONIC_RCR_CRCR)
496 lp->stats.rx_crc_errors++;
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (status & SONIC_RCR_LPKT) {
499 /*
Finn Thainefcce832005-08-20 15:53:22 +1000500 * this was the last packet out of the current receive buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * give the buffer back to the SONIC
502 */
Finn Thainefcce832005-08-20 15:53:22 +1000503 lp->cur_rwp += SIZEOF_SONIC_RR * SONIC_BUS_SCALE(lp->dma_bitmode);
504 if (lp->cur_rwp >= lp->rra_end) lp->cur_rwp = lp->rra_laddr & 0xffff;
505 SONIC_WRITE(SONIC_RWP, lp->cur_rwp);
506 if (SONIC_READ(SONIC_ISR) & SONIC_INT_RBE) {
507 if (sonic_debug > 2)
508 printk("%s: rx buffer exhausted\n", dev->name);
509 SONIC_WRITE(SONIC_ISR, SONIC_INT_RBE); /* clear the flag */
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else
Finn Thainefcce832005-08-20 15:53:22 +1000512 printk(KERN_ERR "%s: rx desc without RCR_LPKT. Shouldn't happen !?\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 dev->name);
Finn Thainefcce832005-08-20 15:53:22 +1000514 /*
515 * give back the descriptor
516 */
517 sonic_rda_put(dev, entry, SONIC_RD_LINK,
518 sonic_rda_get(dev, entry, SONIC_RD_LINK) | SONIC_EOL);
519 sonic_rda_put(dev, entry, SONIC_RD_IN_USE, 1);
520 sonic_rda_put(dev, lp->eol_rx, SONIC_RD_LINK,
521 sonic_rda_get(dev, lp->eol_rx, SONIC_RD_LINK) & ~SONIC_EOL);
522 lp->eol_rx = entry;
523 lp->cur_rx = entry = (entry + 1) & SONIC_RDS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 /*
Finn Thainefcce832005-08-20 15:53:22 +1000526 * If any worth-while packets have been received, netif_rx()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * has done a mark_bh(NET_BH) for us and will work on them
528 * when we get to the bottom-half routine.
529 */
530}
531
532
533/*
534 * Get the current statistics.
535 * This may be called with the device open or closed.
536 */
537static struct net_device_stats *sonic_get_stats(struct net_device *dev)
538{
Finn Thainefcce832005-08-20 15:53:22 +1000539 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* read the tally counter from the SONIC and reset them */
542 lp->stats.rx_crc_errors += SONIC_READ(SONIC_CRCT);
543 SONIC_WRITE(SONIC_CRCT, 0xffff);
544 lp->stats.rx_frame_errors += SONIC_READ(SONIC_FAET);
545 SONIC_WRITE(SONIC_FAET, 0xffff);
546 lp->stats.rx_missed_errors += SONIC_READ(SONIC_MPT);
547 SONIC_WRITE(SONIC_MPT, 0xffff);
548
549 return &lp->stats;
550}
551
552
553/*
554 * Set or clear the multicast filter for this adaptor.
555 */
556static void sonic_multicast_list(struct net_device *dev)
557{
Finn Thainefcce832005-08-20 15:53:22 +1000558 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 unsigned int rcr;
560 struct dev_mc_list *dmi = dev->mc_list;
561 unsigned char *addr;
562 int i;
563
564 rcr = SONIC_READ(SONIC_RCR) & ~(SONIC_RCR_PRO | SONIC_RCR_AMC);
565 rcr |= SONIC_RCR_BRD; /* accept broadcast packets */
566
567 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
568 rcr |= SONIC_RCR_PRO;
569 } else {
570 if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 15)) {
571 rcr |= SONIC_RCR_AMC;
572 } else {
573 if (sonic_debug > 2)
Finn Thainefcce832005-08-20 15:53:22 +1000574 printk("sonic_multicast_list: mc_count %d\n", dev->mc_count);
575 sonic_set_cam_enable(dev, 1); /* always enable our own address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 for (i = 1; i <= dev->mc_count; i++) {
577 addr = dmi->dmi_addr;
578 dmi = dmi->next;
Finn Thainefcce832005-08-20 15:53:22 +1000579 sonic_cda_put(dev, i, SONIC_CD_CAP0, addr[1] << 8 | addr[0]);
580 sonic_cda_put(dev, i, SONIC_CD_CAP1, addr[3] << 8 | addr[2]);
581 sonic_cda_put(dev, i, SONIC_CD_CAP2, addr[5] << 8 | addr[4]);
582 sonic_set_cam_enable(dev, sonic_get_cam_enable(dev) | (1 << i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 SONIC_WRITE(SONIC_CDC, 16);
585 /* issue Load CAM command */
586 SONIC_WRITE(SONIC_CDP, lp->cda_laddr & 0xffff);
587 SONIC_WRITE(SONIC_CMD, SONIC_CR_LCAM);
588 }
589 }
590
591 if (sonic_debug > 2)
592 printk("sonic_multicast_list: setting RCR=%x\n", rcr);
593
594 SONIC_WRITE(SONIC_RCR, rcr);
595}
596
597
598/*
599 * Initialize the SONIC ethernet controller.
600 */
601static int sonic_init(struct net_device *dev)
602{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 unsigned int cmd;
Finn Thainefcce832005-08-20 15:53:22 +1000604 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int i;
606
607 /*
608 * put the Sonic into software-reset mode and
609 * disable all interrupts
610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 SONIC_WRITE(SONIC_IMR, 0);
Finn Thainefcce832005-08-20 15:53:22 +1000612 SONIC_WRITE(SONIC_ISR, 0x7fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
614
615 /*
616 * clear software reset flag, disable receiver, clear and
617 * enable interrupts, then completely initialize the SONIC
618 */
619 SONIC_WRITE(SONIC_CMD, 0);
620 SONIC_WRITE(SONIC_CMD, SONIC_CR_RXDIS);
621
622 /*
623 * initialize the receive resource area
624 */
625 if (sonic_debug > 2)
626 printk("sonic_init: initialize receive resource area\n");
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 for (i = 0; i < SONIC_NUM_RRS; i++) {
Finn Thainefcce832005-08-20 15:53:22 +1000629 u16 bufadr_l = (unsigned long)lp->rx_laddr[i] & 0xffff;
630 u16 bufadr_h = (unsigned long)lp->rx_laddr[i] >> 16;
631 sonic_rra_put(dev, i, SONIC_RR_BUFADR_L, bufadr_l);
632 sonic_rra_put(dev, i, SONIC_RR_BUFADR_H, bufadr_h);
633 sonic_rra_put(dev, i, SONIC_RR_BUFSIZE_L, SONIC_RBSIZE >> 1);
634 sonic_rra_put(dev, i, SONIC_RR_BUFSIZE_H, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
637 /* initialize all RRA registers */
Finn Thainefcce832005-08-20 15:53:22 +1000638 lp->rra_end = (lp->rra_laddr + SONIC_NUM_RRS * SIZEOF_SONIC_RR *
639 SONIC_BUS_SCALE(lp->dma_bitmode)) & 0xffff;
640 lp->cur_rwp = (lp->rra_laddr + (SONIC_NUM_RRS - 1) * SIZEOF_SONIC_RR *
641 SONIC_BUS_SCALE(lp->dma_bitmode)) & 0xffff;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400642
Finn Thainefcce832005-08-20 15:53:22 +1000643 SONIC_WRITE(SONIC_RSA, lp->rra_laddr & 0xffff);
644 SONIC_WRITE(SONIC_REA, lp->rra_end);
645 SONIC_WRITE(SONIC_RRP, lp->rra_laddr & 0xffff);
646 SONIC_WRITE(SONIC_RWP, lp->cur_rwp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 SONIC_WRITE(SONIC_URRA, lp->rra_laddr >> 16);
Finn Thainefcce832005-08-20 15:53:22 +1000648 SONIC_WRITE(SONIC_EOBC, (SONIC_RBSIZE >> 1) - (lp->dma_bitmode ? 2 : 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* load the resource pointers */
651 if (sonic_debug > 3)
Finn Thainefcce832005-08-20 15:53:22 +1000652 printk("sonic_init: issuing RRRA command\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 SONIC_WRITE(SONIC_CMD, SONIC_CR_RRRA);
655 i = 0;
656 while (i++ < 100) {
657 if (SONIC_READ(SONIC_CMD) & SONIC_CR_RRRA)
658 break;
659 }
660
661 if (sonic_debug > 2)
Finn Thainefcce832005-08-20 15:53:22 +1000662 printk("sonic_init: status=%x i=%d\n", SONIC_READ(SONIC_CMD), i);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * Initialize the receive descriptors so that they
666 * become a circular linked list, ie. let the last
667 * descriptor point to the first again.
668 */
669 if (sonic_debug > 2)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400670 printk("sonic_init: initialize receive descriptors\n");
Finn Thainefcce832005-08-20 15:53:22 +1000671 for (i=0; i<SONIC_NUM_RDS; i++) {
672 sonic_rda_put(dev, i, SONIC_RD_STATUS, 0);
673 sonic_rda_put(dev, i, SONIC_RD_PKTLEN, 0);
674 sonic_rda_put(dev, i, SONIC_RD_PKTPTR_L, 0);
675 sonic_rda_put(dev, i, SONIC_RD_PKTPTR_H, 0);
676 sonic_rda_put(dev, i, SONIC_RD_SEQNO, 0);
677 sonic_rda_put(dev, i, SONIC_RD_IN_USE, 1);
678 sonic_rda_put(dev, i, SONIC_RD_LINK,
679 lp->rda_laddr +
680 ((i+1) * SIZEOF_SONIC_RD * SONIC_BUS_SCALE(lp->dma_bitmode)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 /* fix last descriptor */
Finn Thainefcce832005-08-20 15:53:22 +1000683 sonic_rda_put(dev, SONIC_NUM_RDS - 1, SONIC_RD_LINK,
684 (lp->rda_laddr & 0xffff) | SONIC_EOL);
685 lp->eol_rx = SONIC_NUM_RDS - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 lp->cur_rx = 0;
687 SONIC_WRITE(SONIC_URDA, lp->rda_laddr >> 16);
688 SONIC_WRITE(SONIC_CRDA, lp->rda_laddr & 0xffff);
689
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400690 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * initialize transmit descriptors
692 */
693 if (sonic_debug > 2)
694 printk("sonic_init: initialize transmit descriptors\n");
695 for (i = 0; i < SONIC_NUM_TDS; i++) {
Finn Thainefcce832005-08-20 15:53:22 +1000696 sonic_tda_put(dev, i, SONIC_TD_STATUS, 0);
697 sonic_tda_put(dev, i, SONIC_TD_CONFIG, 0);
698 sonic_tda_put(dev, i, SONIC_TD_PKTSIZE, 0);
699 sonic_tda_put(dev, i, SONIC_TD_FRAG_COUNT, 0);
700 sonic_tda_put(dev, i, SONIC_TD_LINK,
701 (lp->tda_laddr & 0xffff) +
702 (i + 1) * SIZEOF_SONIC_TD * SONIC_BUS_SCALE(lp->dma_bitmode));
703 lp->tx_skb[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Finn Thainefcce832005-08-20 15:53:22 +1000705 /* fix last descriptor */
706 sonic_tda_put(dev, SONIC_NUM_TDS - 1, SONIC_TD_LINK,
707 (lp->tda_laddr & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 SONIC_WRITE(SONIC_UTDA, lp->tda_laddr >> 16);
710 SONIC_WRITE(SONIC_CTDA, lp->tda_laddr & 0xffff);
Finn Thainefcce832005-08-20 15:53:22 +1000711 lp->cur_tx = lp->next_tx = 0;
712 lp->eol_tx = SONIC_NUM_TDS - 1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * put our own address to CAM desc[0]
716 */
Finn Thainefcce832005-08-20 15:53:22 +1000717 sonic_cda_put(dev, 0, SONIC_CD_CAP0, dev->dev_addr[1] << 8 | dev->dev_addr[0]);
718 sonic_cda_put(dev, 0, SONIC_CD_CAP1, dev->dev_addr[3] << 8 | dev->dev_addr[2]);
719 sonic_cda_put(dev, 0, SONIC_CD_CAP2, dev->dev_addr[5] << 8 | dev->dev_addr[4]);
720 sonic_set_cam_enable(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 for (i = 0; i < 16; i++)
Finn Thainefcce832005-08-20 15:53:22 +1000723 sonic_cda_put(dev, i, SONIC_CD_ENTRY_POINTER, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /*
726 * initialize CAM registers
727 */
728 SONIC_WRITE(SONIC_CDP, lp->cda_laddr & 0xffff);
729 SONIC_WRITE(SONIC_CDC, 16);
730
731 /*
732 * load the CAM
733 */
734 SONIC_WRITE(SONIC_CMD, SONIC_CR_LCAM);
735
736 i = 0;
737 while (i++ < 100) {
738 if (SONIC_READ(SONIC_ISR) & SONIC_INT_LCD)
739 break;
740 }
741 if (sonic_debug > 2) {
Finn Thainefcce832005-08-20 15:53:22 +1000742 printk("sonic_init: CMD=%x, ISR=%x\n, i=%d",
743 SONIC_READ(SONIC_CMD), SONIC_READ(SONIC_ISR), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
746 /*
747 * enable receiver, disable loopback
748 * and enable all interrupts
749 */
750 SONIC_WRITE(SONIC_CMD, SONIC_CR_RXEN | SONIC_CR_STP);
751 SONIC_WRITE(SONIC_RCR, SONIC_RCR_DEFAULT);
752 SONIC_WRITE(SONIC_TCR, SONIC_TCR_DEFAULT);
753 SONIC_WRITE(SONIC_ISR, 0x7fff);
754 SONIC_WRITE(SONIC_IMR, SONIC_IMR_DEFAULT);
755
756 cmd = SONIC_READ(SONIC_CMD);
757 if ((cmd & SONIC_CR_RXEN) == 0 || (cmd & SONIC_CR_STP) == 0)
Finn Thainefcce832005-08-20 15:53:22 +1000758 printk(KERN_ERR "sonic_init: failed, status=%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (sonic_debug > 2)
761 printk("sonic_init: new status=%x\n",
762 SONIC_READ(SONIC_CMD));
763
764 return 0;
765}
766
767MODULE_LICENSE("GPL");