blob: e5d67327d707798f748183dfce897d918b0afadd [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
Finn Thainefcce832005-08-20 15:53:22 +100053 for (i = 0; i < SONIC_NUM_RRS; i++) {
54 struct sk_buff *skb = dev_alloc_skb(SONIC_RBSIZE + 2);
55 if (skb == NULL) {
56 while(i > 0) { /* free any that were allocated successfully */
57 i--;
58 dev_kfree_skb(lp->rx_skb[i]);
59 lp->rx_skb[i] = NULL;
60 }
61 printk(KERN_ERR "%s: couldn't allocate receive buffers\n",
62 dev->name);
63 return -ENOMEM;
64 }
Finn Thainefcce832005-08-20 15:53:22 +100065 /* align IP header unless DMA requires otherwise */
66 if (SONIC_BUS_SCALE(lp->dma_bitmode) == 2)
67 skb_reserve(skb, 2);
68 lp->rx_skb[i] = skb;
69 }
70
71 for (i = 0; i < SONIC_NUM_RRS; i++) {
72 dma_addr_t laddr = dma_map_single(lp->device, skb_put(lp->rx_skb[i], SONIC_RBSIZE),
73 SONIC_RBSIZE, DMA_FROM_DEVICE);
74 if (!laddr) {
75 while(i > 0) { /* free any that were mapped successfully */
76 i--;
77 dma_unmap_single(lp->device, lp->rx_laddr[i], SONIC_RBSIZE, DMA_FROM_DEVICE);
78 lp->rx_laddr[i] = (dma_addr_t)0;
79 }
80 for (i = 0; i < SONIC_NUM_RRS; i++) {
81 dev_kfree_skb(lp->rx_skb[i]);
82 lp->rx_skb[i] = NULL;
83 }
84 printk(KERN_ERR "%s: couldn't map rx DMA buffers\n",
85 dev->name);
86 return -ENOMEM;
87 }
88 lp->rx_laddr[i] = laddr;
89 }
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /*
92 * Initialize the SONIC
93 */
94 sonic_init(dev);
95
96 netif_start_queue(dev);
97
98 if (sonic_debug > 2)
99 printk("sonic_open: Initialization done.\n");
100
101 return 0;
102}
103
104
105/*
106 * Close the SONIC device
107 */
108static int sonic_close(struct net_device *dev)
109{
Finn Thainefcce832005-08-20 15:53:22 +1000110 struct sonic_local *lp = netdev_priv(dev);
111 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if (sonic_debug > 2)
114 printk("sonic_close\n");
115
116 netif_stop_queue(dev);
117
118 /*
119 * stop the SONIC, disable interrupts
120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 SONIC_WRITE(SONIC_IMR, 0);
Finn Thainefcce832005-08-20 15:53:22 +1000122 SONIC_WRITE(SONIC_ISR, 0x7fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
124
Finn Thainefcce832005-08-20 15:53:22 +1000125 /* unmap and free skbs that haven't been transmitted */
126 for (i = 0; i < SONIC_NUM_TDS; i++) {
127 if(lp->tx_laddr[i]) {
128 dma_unmap_single(lp->device, lp->tx_laddr[i], lp->tx_len[i], DMA_TO_DEVICE);
129 lp->tx_laddr[i] = (dma_addr_t)0;
130 }
131 if(lp->tx_skb[i]) {
132 dev_kfree_skb(lp->tx_skb[i]);
133 lp->tx_skb[i] = NULL;
134 }
135 }
136
137 /* unmap and free the receive buffers */
138 for (i = 0; i < SONIC_NUM_RRS; i++) {
139 if(lp->rx_laddr[i]) {
140 dma_unmap_single(lp->device, lp->rx_laddr[i], SONIC_RBSIZE, DMA_FROM_DEVICE);
141 lp->rx_laddr[i] = (dma_addr_t)0;
142 }
143 if(lp->rx_skb[i]) {
144 dev_kfree_skb(lp->rx_skb[i]);
145 lp->rx_skb[i] = NULL;
146 }
147 }
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return 0;
150}
151
152static void sonic_tx_timeout(struct net_device *dev)
153{
Finn Thainefcce832005-08-20 15:53:22 +1000154 struct sonic_local *lp = netdev_priv(dev);
155 int i;
Finn Thaind74472f2007-05-01 22:33:02 +0200156 /*
157 * put the Sonic into software-reset mode and
158 * disable all interrupts before releasing DMA buffers
159 */
Finn Thainefcce832005-08-20 15:53:22 +1000160 SONIC_WRITE(SONIC_IMR, 0);
Finn Thaind74472f2007-05-01 22:33:02 +0200161 SONIC_WRITE(SONIC_ISR, 0x7fff);
162 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
Finn Thainefcce832005-08-20 15:53:22 +1000163 /* We could resend the original skbs. Easier to re-initialise. */
164 for (i = 0; i < SONIC_NUM_TDS; i++) {
165 if(lp->tx_laddr[i]) {
166 dma_unmap_single(lp->device, lp->tx_laddr[i], lp->tx_len[i], DMA_TO_DEVICE);
167 lp->tx_laddr[i] = (dma_addr_t)0;
168 }
169 if(lp->tx_skb[i]) {
170 dev_kfree_skb(lp->tx_skb[i]);
171 lp->tx_skb[i] = NULL;
172 }
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* Try to restart the adaptor. */
175 sonic_init(dev);
176 lp->stats.tx_errors++;
177 dev->trans_start = jiffies;
178 netif_wake_queue(dev);
179}
180
181/*
182 * transmit packet
Finn Thainefcce832005-08-20 15:53:22 +1000183 *
184 * Appends new TD during transmission thus avoiding any TX interrupts
185 * until we run out of TDs.
186 * This routine interacts closely with the ISR in that it may,
187 * set tx_skb[i]
188 * reset the status flags of the new TD
189 * set and reset EOL flags
190 * stop the tx queue
191 * The ISR interacts with this routine in various ways. It may,
192 * reset tx_skb[i]
193 * test the EOL and status flags of the TDs
194 * wake the tx queue
195 * Concurrently with all of this, the SONIC is potentially writing to
196 * the status flags of the TDs.
197 * Until some mutual exclusion is added, this code will not work with SMP. However,
198 * MIPS Jazz machines and m68k Macs were all uni-processor machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Finn Thainefcce832005-08-20 15:53:22 +1000200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int sonic_send_packet(struct sk_buff *skb, struct net_device *dev)
202{
Finn Thainefcce832005-08-20 15:53:22 +1000203 struct sonic_local *lp = netdev_priv(dev);
204 dma_addr_t laddr;
205 int length;
206 int entry = lp->next_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (sonic_debug > 2)
209 printk("sonic_send_packet: skb=%p, dev=%p\n", skb, dev);
210
Finn Thainefcce832005-08-20 15:53:22 +1000211 length = skb->len;
212 if (length < ETH_ZLEN) {
Herbert Xu5b057c62006-06-23 02:06:41 -0700213 if (skb_padto(skb, ETH_ZLEN))
Patrick McHardyec634fe2009-07-05 19:23:38 -0700214 return NETDEV_TX_OK;
Finn Thainefcce832005-08-20 15:53:22 +1000215 length = ETH_ZLEN;
216 }
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /*
219 * Map the packet data into the logical DMA address space
220 */
Finn Thainefcce832005-08-20 15:53:22 +1000221
222 laddr = dma_map_single(lp->device, skb->data, length, DMA_TO_DEVICE);
223 if (!laddr) {
224 printk(KERN_ERR "%s: failed to map tx DMA buffer.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dev_kfree_skb(skb);
David S. Miller05e9e612009-06-17 01:36:23 -0700226 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400228
Finn Thainefcce832005-08-20 15:53:22 +1000229 sonic_tda_put(dev, entry, SONIC_TD_STATUS, 0); /* clear status */
230 sonic_tda_put(dev, entry, SONIC_TD_FRAG_COUNT, 1); /* single fragment */
231 sonic_tda_put(dev, entry, SONIC_TD_PKTSIZE, length); /* length of packet */
232 sonic_tda_put(dev, entry, SONIC_TD_FRAG_PTR_L, laddr & 0xffff);
233 sonic_tda_put(dev, entry, SONIC_TD_FRAG_PTR_H, laddr >> 16);
234 sonic_tda_put(dev, entry, SONIC_TD_FRAG_SIZE, length);
235 sonic_tda_put(dev, entry, SONIC_TD_LINK,
236 sonic_tda_get(dev, entry, SONIC_TD_LINK) | SONIC_EOL);
237
238 /*
239 * Must set tx_skb[entry] only after clearing status, and
240 * before clearing EOL and before stopping queue
241 */
242 wmb();
243 lp->tx_len[entry] = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 lp->tx_laddr[entry] = laddr;
245 lp->tx_skb[entry] = skb;
246
Finn Thainefcce832005-08-20 15:53:22 +1000247 wmb();
248 sonic_tda_put(dev, lp->eol_tx, SONIC_TD_LINK,
249 sonic_tda_get(dev, lp->eol_tx, SONIC_TD_LINK) & ~SONIC_EOL);
250 lp->eol_tx = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Finn Thainefcce832005-08-20 15:53:22 +1000252 lp->next_tx = (entry + 1) & SONIC_TDS_MASK;
253 if (lp->tx_skb[lp->next_tx] != NULL) {
254 /* The ring is full, the ISR has yet to process the next TD. */
255 if (sonic_debug > 3)
256 printk("%s: stopping queue\n", dev->name);
257 netif_stop_queue(dev);
258 /* after this packet, wait for ISR to free up some TDAs */
259 } else netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if (sonic_debug > 2)
Finn Thainefcce832005-08-20 15:53:22 +1000262 printk("sonic_send_packet: issuing Tx command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 SONIC_WRITE(SONIC_CMD, SONIC_CR_TXP);
265
266 dev->trans_start = jiffies;
267
Patrick McHardyec634fe2009-07-05 19:23:38 -0700268 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/*
272 * The typical workload of the driver:
273 * Handle the network interface interrupts.
274 */
David Howells7d12e782006-10-05 14:55:46 +0100275static irqreturn_t sonic_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Jeff Garzikc31f28e2006-10-06 14:56:04 -0400277 struct net_device *dev = dev_id;
Finn Thainefcce832005-08-20 15:53:22 +1000278 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int status;
280
Finn Thainefcce832005-08-20 15:53:22 +1000281 if (!(status = SONIC_READ(SONIC_ISR) & SONIC_IMR_DEFAULT))
282 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Finn Thainefcce832005-08-20 15:53:22 +1000284 do {
285 if (status & SONIC_INT_PKTRX) {
286 if (sonic_debug > 2)
287 printk("%s: packet rx\n", dev->name);
288 sonic_rx(dev); /* got packet(s) */
289 SONIC_WRITE(SONIC_ISR, SONIC_INT_PKTRX); /* clear the interrupt */
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Finn Thainefcce832005-08-20 15:53:22 +1000292 if (status & SONIC_INT_TXDN) {
293 int entry = lp->cur_tx;
294 int td_status;
295 int freed_some = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Finn Thainefcce832005-08-20 15:53:22 +1000297 /* At this point, cur_tx is the index of a TD that is one of:
298 * unallocated/freed (status set & tx_skb[entry] clear)
299 * allocated and sent (status set & tx_skb[entry] set )
300 * allocated and not yet sent (status clear & tx_skb[entry] set )
301 * still being allocated by sonic_send_packet (status clear & tx_skb[entry] clear)
302 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Finn Thainefcce832005-08-20 15:53:22 +1000304 if (sonic_debug > 2)
305 printk("%s: tx done\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Finn Thainefcce832005-08-20 15:53:22 +1000307 while (lp->tx_skb[entry] != NULL) {
308 if ((td_status = sonic_tda_get(dev, entry, SONIC_TD_STATUS)) == 0)
309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Finn Thainefcce832005-08-20 15:53:22 +1000311 if (td_status & 0x0001) {
312 lp->stats.tx_packets++;
313 lp->stats.tx_bytes += sonic_tda_get(dev, entry, SONIC_TD_PKTSIZE);
314 } else {
315 lp->stats.tx_errors++;
316 if (td_status & 0x0642)
317 lp->stats.tx_aborted_errors++;
318 if (td_status & 0x0180)
319 lp->stats.tx_carrier_errors++;
320 if (td_status & 0x0020)
321 lp->stats.tx_window_errors++;
322 if (td_status & 0x0004)
323 lp->stats.tx_fifo_errors++;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Finn Thainefcce832005-08-20 15:53:22 +1000326 /* We must free the original skb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 dev_kfree_skb_irq(lp->tx_skb[entry]);
Finn Thainefcce832005-08-20 15:53:22 +1000328 lp->tx_skb[entry] = NULL;
329 /* and unmap DMA buffer */
330 dma_unmap_single(lp->device, lp->tx_laddr[entry], lp->tx_len[entry], DMA_TO_DEVICE);
331 lp->tx_laddr[entry] = (dma_addr_t)0;
332 freed_some = 1;
333
334 if (sonic_tda_get(dev, entry, SONIC_TD_LINK) & SONIC_EOL) {
335 entry = (entry + 1) & SONIC_TDS_MASK;
336 break;
337 }
338 entry = (entry + 1) & SONIC_TDS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Finn Thainefcce832005-08-20 15:53:22 +1000340
341 if (freed_some || lp->tx_skb[entry] == NULL)
342 netif_wake_queue(dev); /* The ring is no longer full */
343 lp->cur_tx = entry;
344 SONIC_WRITE(SONIC_ISR, SONIC_INT_TXDN); /* clear the interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
Finn Thainefcce832005-08-20 15:53:22 +1000347 /*
348 * check error conditions
349 */
350 if (status & SONIC_INT_RFO) {
351 if (sonic_debug > 1)
352 printk("%s: rx fifo overrun\n", dev->name);
353 lp->stats.rx_fifo_errors++;
354 SONIC_WRITE(SONIC_ISR, SONIC_INT_RFO); /* clear the interrupt */
355 }
356 if (status & SONIC_INT_RDE) {
357 if (sonic_debug > 1)
358 printk("%s: rx descriptors exhausted\n", dev->name);
359 lp->stats.rx_dropped++;
360 SONIC_WRITE(SONIC_ISR, SONIC_INT_RDE); /* clear the interrupt */
361 }
362 if (status & SONIC_INT_RBAE) {
363 if (sonic_debug > 1)
364 printk("%s: rx buffer area exceeded\n", dev->name);
365 lp->stats.rx_dropped++;
366 SONIC_WRITE(SONIC_ISR, SONIC_INT_RBAE); /* clear the interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
Finn Thainefcce832005-08-20 15:53:22 +1000369 /* counter overruns; all counters are 16bit wide */
370 if (status & SONIC_INT_FAE) {
371 lp->stats.rx_frame_errors += 65536;
372 SONIC_WRITE(SONIC_ISR, SONIC_INT_FAE); /* clear the interrupt */
373 }
374 if (status & SONIC_INT_CRC) {
375 lp->stats.rx_crc_errors += 65536;
376 SONIC_WRITE(SONIC_ISR, SONIC_INT_CRC); /* clear the interrupt */
377 }
378 if (status & SONIC_INT_MP) {
379 lp->stats.rx_missed_errors += 65536;
380 SONIC_WRITE(SONIC_ISR, SONIC_INT_MP); /* clear the interrupt */
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Finn Thainefcce832005-08-20 15:53:22 +1000383 /* transmit error */
384 if (status & SONIC_INT_TXER) {
385 if ((SONIC_READ(SONIC_TCR) & SONIC_TCR_FU) && (sonic_debug > 2))
386 printk(KERN_ERR "%s: tx fifo underrun\n", dev->name);
387 SONIC_WRITE(SONIC_ISR, SONIC_INT_TXER); /* clear the interrupt */
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Finn Thainefcce832005-08-20 15:53:22 +1000390 /* bus retry */
391 if (status & SONIC_INT_BR) {
392 printk(KERN_ERR "%s: Bus retry occurred! Device interrupt disabled.\n",
393 dev->name);
394 /* ... to help debug DMA problems causing endless interrupts. */
395 /* Bounce the eth interface to turn on the interrupt again. */
396 SONIC_WRITE(SONIC_IMR, 0);
397 SONIC_WRITE(SONIC_ISR, SONIC_INT_BR); /* clear the interrupt */
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Finn Thainefcce832005-08-20 15:53:22 +1000400 /* load CAM done */
401 if (status & SONIC_INT_LCD)
402 SONIC_WRITE(SONIC_ISR, SONIC_INT_LCD); /* clear the interrupt */
403 } while((status = SONIC_READ(SONIC_ISR) & SONIC_IMR_DEFAULT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return IRQ_HANDLED;
405}
406
407/*
Finn Thainefcce832005-08-20 15:53:22 +1000408 * We have a good packet(s), pass it/them up the network stack.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
410static void sonic_rx(struct net_device *dev)
411{
Finn Thainefcce832005-08-20 15:53:22 +1000412 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 int status;
Finn Thainefcce832005-08-20 15:53:22 +1000414 int entry = lp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Finn Thainefcce832005-08-20 15:53:22 +1000416 while (sonic_rda_get(dev, entry, SONIC_RD_IN_USE) == 0) {
417 struct sk_buff *used_skb;
418 struct sk_buff *new_skb;
419 dma_addr_t new_laddr;
420 u16 bufadr_l;
421 u16 bufadr_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 int pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Finn Thainefcce832005-08-20 15:53:22 +1000424 status = sonic_rda_get(dev, entry, SONIC_RD_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (status & SONIC_RCR_PRX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* Malloc up new buffer. */
Finn Thainefcce832005-08-20 15:53:22 +1000427 new_skb = dev_alloc_skb(SONIC_RBSIZE + 2);
428 if (new_skb == NULL) {
429 printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 lp->stats.rx_dropped++;
431 break;
432 }
Finn Thainefcce832005-08-20 15:53:22 +1000433 /* provide 16 byte IP header alignment unless DMA requires otherwise */
434 if(SONIC_BUS_SCALE(lp->dma_bitmode) == 2)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400435 skb_reserve(new_skb, 2);
Finn Thainefcce832005-08-20 15:53:22 +1000436
437 new_laddr = dma_map_single(lp->device, skb_put(new_skb, SONIC_RBSIZE),
438 SONIC_RBSIZE, DMA_FROM_DEVICE);
439 if (!new_laddr) {
440 dev_kfree_skb(new_skb);
441 printk(KERN_ERR "%s: Failed to map rx buffer, dropping packet.\n", dev->name);
442 lp->stats.rx_dropped++;
443 break;
444 }
445
446 /* now we have a new skb to replace it, pass the used one up the stack */
447 dma_unmap_single(lp->device, lp->rx_laddr[entry], SONIC_RBSIZE, DMA_FROM_DEVICE);
448 used_skb = lp->rx_skb[entry];
449 pkt_len = sonic_rda_get(dev, entry, SONIC_RD_PKTLEN);
450 skb_trim(used_skb, pkt_len);
451 used_skb->protocol = eth_type_trans(used_skb, dev);
452 netif_rx(used_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 lp->stats.rx_packets++;
454 lp->stats.rx_bytes += pkt_len;
455
Finn Thainefcce832005-08-20 15:53:22 +1000456 /* and insert the new skb */
457 lp->rx_laddr[entry] = new_laddr;
458 lp->rx_skb[entry] = new_skb;
459
460 bufadr_l = (unsigned long)new_laddr & 0xffff;
461 bufadr_h = (unsigned long)new_laddr >> 16;
462 sonic_rra_put(dev, entry, SONIC_RR_BUFADR_L, bufadr_l);
463 sonic_rra_put(dev, entry, SONIC_RR_BUFADR_H, bufadr_h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 } else {
465 /* This should only happen, if we enable accepting broken packets. */
466 lp->stats.rx_errors++;
467 if (status & SONIC_RCR_FAER)
468 lp->stats.rx_frame_errors++;
469 if (status & SONIC_RCR_CRCR)
470 lp->stats.rx_crc_errors++;
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (status & SONIC_RCR_LPKT) {
473 /*
Finn Thainefcce832005-08-20 15:53:22 +1000474 * this was the last packet out of the current receive buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * give the buffer back to the SONIC
476 */
Finn Thainefcce832005-08-20 15:53:22 +1000477 lp->cur_rwp += SIZEOF_SONIC_RR * SONIC_BUS_SCALE(lp->dma_bitmode);
478 if (lp->cur_rwp >= lp->rra_end) lp->cur_rwp = lp->rra_laddr & 0xffff;
479 SONIC_WRITE(SONIC_RWP, lp->cur_rwp);
480 if (SONIC_READ(SONIC_ISR) & SONIC_INT_RBE) {
481 if (sonic_debug > 2)
482 printk("%s: rx buffer exhausted\n", dev->name);
483 SONIC_WRITE(SONIC_ISR, SONIC_INT_RBE); /* clear the flag */
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } else
Finn Thainefcce832005-08-20 15:53:22 +1000486 printk(KERN_ERR "%s: rx desc without RCR_LPKT. Shouldn't happen !?\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 dev->name);
Finn Thainefcce832005-08-20 15:53:22 +1000488 /*
489 * give back the descriptor
490 */
491 sonic_rda_put(dev, entry, SONIC_RD_LINK,
492 sonic_rda_get(dev, entry, SONIC_RD_LINK) | SONIC_EOL);
493 sonic_rda_put(dev, entry, SONIC_RD_IN_USE, 1);
494 sonic_rda_put(dev, lp->eol_rx, SONIC_RD_LINK,
495 sonic_rda_get(dev, lp->eol_rx, SONIC_RD_LINK) & ~SONIC_EOL);
496 lp->eol_rx = entry;
497 lp->cur_rx = entry = (entry + 1) & SONIC_RDS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 /*
Finn Thainefcce832005-08-20 15:53:22 +1000500 * If any worth-while packets have been received, netif_rx()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * has done a mark_bh(NET_BH) for us and will work on them
502 * when we get to the bottom-half routine.
503 */
504}
505
506
507/*
508 * Get the current statistics.
509 * This may be called with the device open or closed.
510 */
511static struct net_device_stats *sonic_get_stats(struct net_device *dev)
512{
Finn Thainefcce832005-08-20 15:53:22 +1000513 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* read the tally counter from the SONIC and reset them */
516 lp->stats.rx_crc_errors += SONIC_READ(SONIC_CRCT);
517 SONIC_WRITE(SONIC_CRCT, 0xffff);
518 lp->stats.rx_frame_errors += SONIC_READ(SONIC_FAET);
519 SONIC_WRITE(SONIC_FAET, 0xffff);
520 lp->stats.rx_missed_errors += SONIC_READ(SONIC_MPT);
521 SONIC_WRITE(SONIC_MPT, 0xffff);
522
523 return &lp->stats;
524}
525
526
527/*
528 * Set or clear the multicast filter for this adaptor.
529 */
530static void sonic_multicast_list(struct net_device *dev)
531{
Finn Thainefcce832005-08-20 15:53:22 +1000532 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 unsigned int rcr;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000534 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned char *addr;
536 int i;
537
538 rcr = SONIC_READ(SONIC_RCR) & ~(SONIC_RCR_PRO | SONIC_RCR_AMC);
539 rcr |= SONIC_RCR_BRD; /* accept broadcast packets */
540
541 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
542 rcr |= SONIC_RCR_PRO;
543 } else {
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000544 if ((dev->flags & IFF_ALLMULTI) ||
545 (netdev_mc_count(dev) > 15)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 rcr |= SONIC_RCR_AMC;
547 } else {
548 if (sonic_debug > 2)
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000549 printk("sonic_multicast_list: mc_count %d\n",
550 netdev_mc_count(dev));
Finn Thainefcce832005-08-20 15:53:22 +1000551 sonic_set_cam_enable(dev, 1); /* always enable our own address */
Jiri Pirko55085902010-02-18 00:42:54 +0000552 i = 1;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000553 netdev_for_each_mc_addr(ha, dev) {
554 addr = ha->addr;
Finn Thainefcce832005-08-20 15:53:22 +1000555 sonic_cda_put(dev, i, SONIC_CD_CAP0, addr[1] << 8 | addr[0]);
556 sonic_cda_put(dev, i, SONIC_CD_CAP1, addr[3] << 8 | addr[2]);
557 sonic_cda_put(dev, i, SONIC_CD_CAP2, addr[5] << 8 | addr[4]);
558 sonic_set_cam_enable(dev, sonic_get_cam_enable(dev) | (1 << i));
Jiri Pirko55085902010-02-18 00:42:54 +0000559 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 SONIC_WRITE(SONIC_CDC, 16);
562 /* issue Load CAM command */
563 SONIC_WRITE(SONIC_CDP, lp->cda_laddr & 0xffff);
564 SONIC_WRITE(SONIC_CMD, SONIC_CR_LCAM);
565 }
566 }
567
568 if (sonic_debug > 2)
569 printk("sonic_multicast_list: setting RCR=%x\n", rcr);
570
571 SONIC_WRITE(SONIC_RCR, rcr);
572}
573
574
575/*
576 * Initialize the SONIC ethernet controller.
577 */
578static int sonic_init(struct net_device *dev)
579{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 unsigned int cmd;
Finn Thainefcce832005-08-20 15:53:22 +1000581 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int i;
583
584 /*
585 * put the Sonic into software-reset mode and
586 * disable all interrupts
587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 SONIC_WRITE(SONIC_IMR, 0);
Finn Thainefcce832005-08-20 15:53:22 +1000589 SONIC_WRITE(SONIC_ISR, 0x7fff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
591
592 /*
593 * clear software reset flag, disable receiver, clear and
594 * enable interrupts, then completely initialize the SONIC
595 */
596 SONIC_WRITE(SONIC_CMD, 0);
597 SONIC_WRITE(SONIC_CMD, SONIC_CR_RXDIS);
598
599 /*
600 * initialize the receive resource area
601 */
602 if (sonic_debug > 2)
603 printk("sonic_init: initialize receive resource area\n");
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 for (i = 0; i < SONIC_NUM_RRS; i++) {
Finn Thainefcce832005-08-20 15:53:22 +1000606 u16 bufadr_l = (unsigned long)lp->rx_laddr[i] & 0xffff;
607 u16 bufadr_h = (unsigned long)lp->rx_laddr[i] >> 16;
608 sonic_rra_put(dev, i, SONIC_RR_BUFADR_L, bufadr_l);
609 sonic_rra_put(dev, i, SONIC_RR_BUFADR_H, bufadr_h);
610 sonic_rra_put(dev, i, SONIC_RR_BUFSIZE_L, SONIC_RBSIZE >> 1);
611 sonic_rra_put(dev, i, SONIC_RR_BUFSIZE_H, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613
614 /* initialize all RRA registers */
Finn Thainefcce832005-08-20 15:53:22 +1000615 lp->rra_end = (lp->rra_laddr + SONIC_NUM_RRS * SIZEOF_SONIC_RR *
616 SONIC_BUS_SCALE(lp->dma_bitmode)) & 0xffff;
617 lp->cur_rwp = (lp->rra_laddr + (SONIC_NUM_RRS - 1) * SIZEOF_SONIC_RR *
618 SONIC_BUS_SCALE(lp->dma_bitmode)) & 0xffff;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400619
Finn Thainefcce832005-08-20 15:53:22 +1000620 SONIC_WRITE(SONIC_RSA, lp->rra_laddr & 0xffff);
621 SONIC_WRITE(SONIC_REA, lp->rra_end);
622 SONIC_WRITE(SONIC_RRP, lp->rra_laddr & 0xffff);
623 SONIC_WRITE(SONIC_RWP, lp->cur_rwp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 SONIC_WRITE(SONIC_URRA, lp->rra_laddr >> 16);
Finn Thainefcce832005-08-20 15:53:22 +1000625 SONIC_WRITE(SONIC_EOBC, (SONIC_RBSIZE >> 1) - (lp->dma_bitmode ? 2 : 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /* load the resource pointers */
628 if (sonic_debug > 3)
Finn Thainefcce832005-08-20 15:53:22 +1000629 printk("sonic_init: issuing RRRA command\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 SONIC_WRITE(SONIC_CMD, SONIC_CR_RRRA);
632 i = 0;
633 while (i++ < 100) {
634 if (SONIC_READ(SONIC_CMD) & SONIC_CR_RRRA)
635 break;
636 }
637
638 if (sonic_debug > 2)
Finn Thainefcce832005-08-20 15:53:22 +1000639 printk("sonic_init: status=%x i=%d\n", SONIC_READ(SONIC_CMD), i);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * Initialize the receive descriptors so that they
643 * become a circular linked list, ie. let the last
644 * descriptor point to the first again.
645 */
646 if (sonic_debug > 2)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400647 printk("sonic_init: initialize receive descriptors\n");
Finn Thainefcce832005-08-20 15:53:22 +1000648 for (i=0; i<SONIC_NUM_RDS; i++) {
649 sonic_rda_put(dev, i, SONIC_RD_STATUS, 0);
650 sonic_rda_put(dev, i, SONIC_RD_PKTLEN, 0);
651 sonic_rda_put(dev, i, SONIC_RD_PKTPTR_L, 0);
652 sonic_rda_put(dev, i, SONIC_RD_PKTPTR_H, 0);
653 sonic_rda_put(dev, i, SONIC_RD_SEQNO, 0);
654 sonic_rda_put(dev, i, SONIC_RD_IN_USE, 1);
655 sonic_rda_put(dev, i, SONIC_RD_LINK,
656 lp->rda_laddr +
657 ((i+1) * SIZEOF_SONIC_RD * SONIC_BUS_SCALE(lp->dma_bitmode)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 /* fix last descriptor */
Finn Thainefcce832005-08-20 15:53:22 +1000660 sonic_rda_put(dev, SONIC_NUM_RDS - 1, SONIC_RD_LINK,
661 (lp->rda_laddr & 0xffff) | SONIC_EOL);
662 lp->eol_rx = SONIC_NUM_RDS - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 lp->cur_rx = 0;
664 SONIC_WRITE(SONIC_URDA, lp->rda_laddr >> 16);
665 SONIC_WRITE(SONIC_CRDA, lp->rda_laddr & 0xffff);
666
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400667 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * initialize transmit descriptors
669 */
670 if (sonic_debug > 2)
671 printk("sonic_init: initialize transmit descriptors\n");
672 for (i = 0; i < SONIC_NUM_TDS; i++) {
Finn Thainefcce832005-08-20 15:53:22 +1000673 sonic_tda_put(dev, i, SONIC_TD_STATUS, 0);
674 sonic_tda_put(dev, i, SONIC_TD_CONFIG, 0);
675 sonic_tda_put(dev, i, SONIC_TD_PKTSIZE, 0);
676 sonic_tda_put(dev, i, SONIC_TD_FRAG_COUNT, 0);
677 sonic_tda_put(dev, i, SONIC_TD_LINK,
678 (lp->tda_laddr & 0xffff) +
679 (i + 1) * SIZEOF_SONIC_TD * SONIC_BUS_SCALE(lp->dma_bitmode));
680 lp->tx_skb[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Finn Thainefcce832005-08-20 15:53:22 +1000682 /* fix last descriptor */
683 sonic_tda_put(dev, SONIC_NUM_TDS - 1, SONIC_TD_LINK,
684 (lp->tda_laddr & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 SONIC_WRITE(SONIC_UTDA, lp->tda_laddr >> 16);
687 SONIC_WRITE(SONIC_CTDA, lp->tda_laddr & 0xffff);
Finn Thainefcce832005-08-20 15:53:22 +1000688 lp->cur_tx = lp->next_tx = 0;
689 lp->eol_tx = SONIC_NUM_TDS - 1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * put our own address to CAM desc[0]
693 */
Finn Thainefcce832005-08-20 15:53:22 +1000694 sonic_cda_put(dev, 0, SONIC_CD_CAP0, dev->dev_addr[1] << 8 | dev->dev_addr[0]);
695 sonic_cda_put(dev, 0, SONIC_CD_CAP1, dev->dev_addr[3] << 8 | dev->dev_addr[2]);
696 sonic_cda_put(dev, 0, SONIC_CD_CAP2, dev->dev_addr[5] << 8 | dev->dev_addr[4]);
697 sonic_set_cam_enable(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 for (i = 0; i < 16; i++)
Finn Thainefcce832005-08-20 15:53:22 +1000700 sonic_cda_put(dev, i, SONIC_CD_ENTRY_POINTER, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /*
703 * initialize CAM registers
704 */
705 SONIC_WRITE(SONIC_CDP, lp->cda_laddr & 0xffff);
706 SONIC_WRITE(SONIC_CDC, 16);
707
708 /*
709 * load the CAM
710 */
711 SONIC_WRITE(SONIC_CMD, SONIC_CR_LCAM);
712
713 i = 0;
714 while (i++ < 100) {
715 if (SONIC_READ(SONIC_ISR) & SONIC_INT_LCD)
716 break;
717 }
718 if (sonic_debug > 2) {
Finn Thainefcce832005-08-20 15:53:22 +1000719 printk("sonic_init: CMD=%x, ISR=%x\n, i=%d",
720 SONIC_READ(SONIC_CMD), SONIC_READ(SONIC_ISR), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 /*
724 * enable receiver, disable loopback
725 * and enable all interrupts
726 */
727 SONIC_WRITE(SONIC_CMD, SONIC_CR_RXEN | SONIC_CR_STP);
728 SONIC_WRITE(SONIC_RCR, SONIC_RCR_DEFAULT);
729 SONIC_WRITE(SONIC_TCR, SONIC_TCR_DEFAULT);
730 SONIC_WRITE(SONIC_ISR, 0x7fff);
731 SONIC_WRITE(SONIC_IMR, SONIC_IMR_DEFAULT);
732
733 cmd = SONIC_READ(SONIC_CMD);
734 if ((cmd & SONIC_CR_RXEN) == 0 || (cmd & SONIC_CR_STP) == 0)
Finn Thainefcce832005-08-20 15:53:22 +1000735 printk(KERN_ERR "sonic_init: failed, status=%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (sonic_debug > 2)
738 printk("sonic_init: new status=%x\n",
739 SONIC_READ(SONIC_CMD));
740
741 return 0;
742}
743
744MODULE_LICENSE("GPL");