]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/staging/et131x/et1310_tx.c
Staging: et131x: Clean up the tx ring init
[linux-beck.git] / drivers / staging / et131x / et1310_tx.c
1 /*
2  * Agere Systems Inc.
3  * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
4  *
5  * Copyright © 2005 Agere Systems Inc.
6  * All rights reserved.
7  *   http://www.agere.com
8  *
9  *------------------------------------------------------------------------------
10  *
11  * et1310_tx.c - Routines used to perform data transmission.
12  *
13  *------------------------------------------------------------------------------
14  *
15  * SOFTWARE LICENSE
16  *
17  * This software is provided subject to the following terms and conditions,
18  * which you should read carefully before using the software.  Using this
19  * software indicates your acceptance of these terms and conditions.  If you do
20  * not agree with these terms and conditions, do not use the software.
21  *
22  * Copyright © 2005 Agere Systems Inc.
23  * All rights reserved.
24  *
25  * Redistribution and use in source or binary forms, with or without
26  * modifications, are permitted provided that the following conditions are met:
27  *
28  * . Redistributions of source code must retain the above copyright notice, this
29  *    list of conditions and the following Disclaimer as comments in the code as
30  *    well as in the documentation and/or other materials provided with the
31  *    distribution.
32  *
33  * . Redistributions in binary form must reproduce the above copyright notice,
34  *    this list of conditions and the following Disclaimer in the documentation
35  *    and/or other materials provided with the distribution.
36  *
37  * . Neither the name of Agere Systems Inc. nor the names of the contributors
38  *    may be used to endorse or promote products derived from this software
39  *    without specific prior written permission.
40  *
41  * Disclaimer
42  *
43  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
44  * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
45  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ANY
46  * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
47  * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
48  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51  * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
52  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
54  * DAMAGE.
55  *
56  */
57
58 #include "et131x_version.h"
59 #include "et131x_defs.h"
60
61 #include <linux/pci.h>
62 #include <linux/init.h>
63 #include <linux/module.h>
64 #include <linux/types.h>
65 #include <linux/kernel.h>
66
67 #include <linux/sched.h>
68 #include <linux/ptrace.h>
69 #include <linux/slab.h>
70 #include <linux/ctype.h>
71 #include <linux/string.h>
72 #include <linux/timer.h>
73 #include <linux/interrupt.h>
74 #include <linux/in.h>
75 #include <linux/delay.h>
76 #include <linux/io.h>
77 #include <linux/bitops.h>
78 #include <asm/system.h>
79
80 #include <linux/netdevice.h>
81 #include <linux/etherdevice.h>
82 #include <linux/skbuff.h>
83 #include <linux/if_arp.h>
84 #include <linux/ioport.h>
85
86 #include "et1310_phy.h"
87 #include "et1310_pm.h"
88 #include "et1310_jagcore.h"
89
90 #include "et131x_adapter.h"
91 #include "et131x_initpci.h"
92 #include "et131x_isr.h"
93
94 #include "et1310_tx.h"
95
96
97 static inline void et131x_free_send_packet(struct et131x_adapter *etdev,
98                                            struct tcb *tcb);
99 static int et131x_send_packet(struct sk_buff *skb,
100                               struct et131x_adapter *etdev);
101 static int nic_send_packet(struct et131x_adapter *etdev, struct tcb *tcb);
102
103 /**
104  * et131x_tx_dma_memory_alloc
105  * @adapter: pointer to our private adapter structure
106  *
107  * Returns 0 on success and errno on failure (as defined in errno.h).
108  *
109  * Allocates memory that will be visible both to the device and to the CPU.
110  * The OS will pass us packets, pointers to which we will insert in the Tx
111  * Descriptor queue. The device will read this queue to find the packets in
112  * memory. The device will update the "status" in memory each time it xmits a
113  * packet.
114  */
115 int et131x_tx_dma_memory_alloc(struct et131x_adapter *adapter)
116 {
117         int desc_size = 0;
118         struct tx_ring *tx_ring = &adapter->tx_ring;
119
120         /* Allocate memory for the TCB's (Transmit Control Block) */
121         adapter->tx_ring.MpTcbMem = (struct tcb *)
122                 kcalloc(NUM_TCB, sizeof(struct tcb), GFP_ATOMIC | GFP_DMA);
123         if (!adapter->tx_ring.MpTcbMem) {
124                 dev_err(&adapter->pdev->dev, "Cannot alloc memory for TCBs\n");
125                 return -ENOMEM;
126         }
127
128         /* Allocate enough memory for the Tx descriptor ring, and allocate
129          * some extra so that the ring can be aligned on a 4k boundary.
130          */
131         desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX) + 4096 - 1;
132         tx_ring->tx_desc_ring =
133             (struct tx_desc *) pci_alloc_consistent(adapter->pdev, desc_size,
134                                                     &tx_ring->tx_desc_ring_pa);
135         if (!adapter->tx_ring.tx_desc_ring) {
136                 dev_err(&adapter->pdev->dev, "Cannot alloc memory for Tx Ring\n");
137                 return -ENOMEM;
138         }
139
140         /* Save physical address
141          *
142          * NOTE: pci_alloc_consistent(), used above to alloc DMA regions,
143          * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
144          * are ever returned, make sure the high part is retrieved here before
145          * storing the adjusted address.
146          */
147         /* Allocate memory for the Tx status block */
148         tx_ring->pTxStatusVa = pci_alloc_consistent(adapter->pdev,
149                                                     sizeof(TX_STATUS_BLOCK_t),
150                                                     &tx_ring->pTxStatusPa);
151         if (!adapter->tx_ring.pTxStatusPa) {
152                 dev_err(&adapter->pdev->dev,
153                                   "Cannot alloc memory for Tx status block\n");
154                 return -ENOMEM;
155         }
156
157         /* Allocate memory for a dummy buffer */
158         tx_ring->pTxDummyBlkVa = pci_alloc_consistent(adapter->pdev,
159                                                       NIC_MIN_PACKET_SIZE,
160                                                       &tx_ring->pTxDummyBlkPa);
161         if (!adapter->tx_ring.pTxDummyBlkPa) {
162                 dev_err(&adapter->pdev->dev,
163                         "Cannot alloc memory for Tx dummy buffer\n");
164                 return -ENOMEM;
165         }
166
167         return 0;
168 }
169
170 /**
171  * et131x_tx_dma_memory_free - Free all memory allocated within this module
172  * @adapter: pointer to our private adapter structure
173  *
174  * Returns 0 on success and errno on failure (as defined in errno.h).
175  */
176 void et131x_tx_dma_memory_free(struct et131x_adapter *adapter)
177 {
178         int desc_size = 0;
179
180         if (adapter->tx_ring.tx_desc_ring) {
181                 /* Free memory relating to Tx rings here */
182                 desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX)
183                                                                         + 4096 - 1;
184                 pci_free_consistent(adapter->pdev,
185                                     desc_size,
186                                     adapter->tx_ring.tx_desc_ring,
187                                     adapter->tx_ring.tx_desc_ring_pa);
188                 adapter->tx_ring.tx_desc_ring = NULL;
189         }
190
191         /* Free memory for the Tx status block */
192         if (adapter->tx_ring.pTxStatusVa) {
193                 pci_free_consistent(adapter->pdev,
194                                     sizeof(TX_STATUS_BLOCK_t),
195                                     adapter->tx_ring.pTxStatusVa,
196                                     adapter->tx_ring.pTxStatusPa);
197
198                 adapter->tx_ring.pTxStatusVa = NULL;
199         }
200
201         /* Free memory for the dummy buffer */
202         if (adapter->tx_ring.pTxDummyBlkVa) {
203                 pci_free_consistent(adapter->pdev,
204                                     NIC_MIN_PACKET_SIZE,
205                                     adapter->tx_ring.pTxDummyBlkVa,
206                                     adapter->tx_ring.pTxDummyBlkPa);
207
208                 adapter->tx_ring.pTxDummyBlkVa = NULL;
209         }
210
211         /* Free the memory for the tcb structures */
212         kfree(adapter->tx_ring.MpTcbMem);
213 }
214
215 /**
216  * ConfigTxDmaRegs - Set up the tx dma section of the JAGCore.
217  * @etdev: pointer to our private adapter structure
218  */
219 void ConfigTxDmaRegs(struct et131x_adapter *etdev)
220 {
221         struct _TXDMA_t __iomem *txdma = &etdev->regs->txdma;
222
223         /* Load the hardware with the start of the transmit descriptor ring. */
224         writel((u32) ((u64)etdev->tx_ring.tx_desc_ring_pa >> 32),
225                &txdma->pr_base_hi);
226         writel((u32) etdev->tx_ring.tx_desc_ring_pa,
227                &txdma->pr_base_lo);
228
229         /* Initialise the transmit DMA engine */
230         writel(NUM_DESC_PER_RING_TX - 1, &txdma->pr_num_des.value);
231
232         /* Load the completion writeback physical address */
233         writel((u32)((u64)etdev->tx_ring.pTxStatusPa >> 32),
234                                                 &txdma->dma_wb_base_hi);
235         writel((u32)etdev->tx_ring.pTxStatusPa, &txdma->dma_wb_base_lo);
236
237         memset(etdev->tx_ring.pTxStatusVa, 0, sizeof(TX_STATUS_BLOCK_t));
238
239         writel(0, &txdma->service_request);
240         etdev->tx_ring.txDmaReadyToSend = 0;
241 }
242
243 /**
244  * et131x_tx_dma_disable - Stop of Tx_DMA on the ET1310
245  * @etdev: pointer to our adapter structure
246  */
247 void et131x_tx_dma_disable(struct et131x_adapter *etdev)
248 {
249         /* Setup the tramsmit dma configuration register */
250         writel(ET_TXDMA_CSR_HALT|ET_TXDMA_SNGL_EPKT,
251                                         &etdev->regs->txdma.csr);
252 }
253
254 /**
255  * et131x_tx_dma_enable - re-start of Tx_DMA on the ET1310.
256  * @etdev: pointer to our adapter structure
257  *
258  * Mainly used after a return to the D0 (full-power) state from a lower state.
259  */
260 void et131x_tx_dma_enable(struct et131x_adapter *etdev)
261 {
262         /* Setup the transmit dma configuration register for normal
263          * operation
264          */
265         writel(ET_TXDMA_SNGL_EPKT|(PARM_DMA_CACHE_DEF << ET_TXDMA_CACHE_SHIFT),
266                                         &etdev->regs->txdma.csr);
267 }
268
269 /**
270  * et131x_init_send - Initialize send data structures
271  * @adapter: pointer to our private adapter structure
272  */
273 void et131x_init_send(struct et131x_adapter *adapter)
274 {
275         struct tcb *tcb;
276         u32 ct;
277         struct tx_ring *tx_ring;
278
279         /* Setup some convenience pointers */
280         tx_ring = &adapter->tx_ring;
281         tcb = adapter->tx_ring.MpTcbMem;
282
283         tx_ring->TCBReadyQueueHead = tcb;
284
285         memset(tcb, 0, sizeof(struct tcb) * NUM_TCB);
286
287         /* Go through and set up each TCB */
288         for (ct = 0; ct++ < NUM_TCB; tcb++) {
289                 /* Set the link pointer in HW TCB to the next TCB in the
290                  * chain.  If this is the last TCB in the chain, also set the
291                  * tail pointer.
292                  */
293                 tcb->Next = tcb + 1;
294
295         tcb--;
296         tx_ring->TCBReadyQueueTail = tcb;
297         tcb->Next = NULL;
298         /* Curr send queue should now be empty */
299         tx_ring->CurrSendHead = NULL;
300         tx_ring->CurrSendTail = NULL;
301 }
302
303 /**
304  * et131x_send_packets - This function is called by the OS to send packets
305  * @skb: the packet(s) to send
306  * @netdev:device on which to TX the above packet(s)
307  *
308  * Return 0 in almost all cases; non-zero value in extreme hard failure only
309  */
310 int et131x_send_packets(struct sk_buff *skb, struct net_device *netdev)
311 {
312         int status = 0;
313         struct et131x_adapter *etdev = NULL;
314
315         etdev = netdev_priv(netdev);
316
317         /* Send these packets
318          *
319          * NOTE: The Linux Tx entry point is only given one packet at a time
320          * to Tx, so the PacketCount and it's array used makes no sense here
321          */
322
323         /* TCB is not available */
324         if (etdev->tx_ring.nBusySend >= NUM_TCB) {
325                 /* NOTE: If there's an error on send, no need to queue the
326                  * packet under Linux; if we just send an error up to the
327                  * netif layer, it will resend the skb to us.
328                  */
329                 status = -ENOMEM;
330         } else {
331                 /* We need to see if the link is up; if it's not, make the
332                  * netif layer think we're good and drop the packet
333                  */
334                 if ((etdev->Flags & fMP_ADAPTER_FAIL_SEND_MASK) ||
335                                         !netif_carrier_ok(netdev)) {
336                         dev_kfree_skb_any(skb);
337                         skb = NULL;
338
339                         etdev->net_stats.tx_dropped++;
340                 } else {
341                         status = et131x_send_packet(skb, etdev);
342                         if (status != 0 && status != -ENOMEM) {
343                                 /* On any other error, make netif think we're
344                                  * OK and drop the packet
345                                  */
346                                 dev_kfree_skb_any(skb);
347                                 skb = NULL;
348                                 etdev->net_stats.tx_dropped++;
349                         }
350                 }
351         }
352         return status;
353 }
354
355 /**
356  * et131x_send_packet - Do the work to send a packet
357  * @skb: the packet(s) to send
358  * @etdev: a pointer to the device's private adapter structure
359  *
360  * Return 0 in almost all cases; non-zero value in extreme hard failure only.
361  *
362  * Assumption: Send spinlock has been acquired
363  */
364 static int et131x_send_packet(struct sk_buff *skb,
365                               struct et131x_adapter *etdev)
366 {
367         int status;
368         struct tcb *tcb = NULL;
369         u16 *shbufva;
370         unsigned long flags;
371
372         /* All packets must have at least a MAC address and a protocol type */
373         if (skb->len < ETH_HLEN)
374                 return -EIO;
375
376         /* Get a TCB for this packet */
377         spin_lock_irqsave(&etdev->TCBReadyQLock, flags);
378
379         tcb = etdev->tx_ring.TCBReadyQueueHead;
380
381         if (tcb == NULL) {
382                 spin_unlock_irqrestore(&etdev->TCBReadyQLock, flags);
383                 return -ENOMEM;
384         }
385
386         etdev->tx_ring.TCBReadyQueueHead = tcb->Next;
387
388         if (etdev->tx_ring.TCBReadyQueueHead == NULL)
389                 etdev->tx_ring.TCBReadyQueueTail = NULL;
390
391         spin_unlock_irqrestore(&etdev->TCBReadyQLock, flags);
392
393         tcb->PacketLength = skb->len;
394         tcb->Packet = skb;
395
396         if ((skb->data != NULL) && ((skb->len - skb->data_len) >= 6)) {
397                 shbufva = (u16 *) skb->data;
398
399                 if ((shbufva[0] == 0xffff) &&
400                     (shbufva[1] == 0xffff) && (shbufva[2] == 0xffff)) {
401                         tcb->Flags |= fMP_DEST_BROAD;
402                 } else if ((shbufva[0] & 0x3) == 0x0001) {
403                         tcb->Flags |=  fMP_DEST_MULTI;
404                 }
405         }
406
407         tcb->Next = NULL;
408
409         /* Call the NIC specific send handler. */
410         status = nic_send_packet(etdev, tcb);
411
412         if (status != 0) {
413                 spin_lock_irqsave(&etdev->TCBReadyQLock, flags);
414
415                 if (etdev->tx_ring.TCBReadyQueueTail) {
416                         etdev->tx_ring.TCBReadyQueueTail->Next = tcb;
417                 } else {
418                         /* Apparently ready Q is empty. */
419                         etdev->tx_ring.TCBReadyQueueHead = tcb;
420                 }
421
422                 etdev->tx_ring.TCBReadyQueueTail = tcb;
423                 spin_unlock_irqrestore(&etdev->TCBReadyQLock, flags);
424                 return status;
425         }
426         WARN_ON(etdev->tx_ring.nBusySend > NUM_TCB);
427         return 0;
428 }
429
430 /**
431  * nic_send_packet - NIC specific send handler for version B silicon.
432  * @etdev: pointer to our adapter
433  * @tcb: pointer to struct tcb
434  *
435  * Returns 0 or errno.
436  */
437 static int nic_send_packet(struct et131x_adapter *etdev, struct tcb *tcb)
438 {
439         u32 i;
440         struct tx_desc desc[24];        /* 24 x 16 byte */
441         u32 frag = 0;
442         u32 thiscopy, remainder;
443         struct sk_buff *skb = tcb->Packet;
444         u32 nr_frags = skb_shinfo(skb)->nr_frags + 1;
445         struct skb_frag_struct *frags = &skb_shinfo(skb)->frags[0];
446         unsigned long flags;
447
448         /* Part of the optimizations of this send routine restrict us to
449          * sending 24 fragments at a pass.  In practice we should never see
450          * more than 5 fragments.
451          *
452          * NOTE: The older version of this function (below) can handle any
453          * number of fragments. If needed, we can call this function,
454          * although it is less efficient.
455          */
456         if (nr_frags > 23)
457                 return -EIO;
458
459         memset(desc, 0, sizeof(struct tx_desc) * (nr_frags + 1));
460
461         for (i = 0; i < nr_frags; i++) {
462                 /* If there is something in this element, lets get a
463                  * descriptor from the ring and get the necessary data
464                  */
465                 if (i == 0) {
466                         /* If the fragments are smaller than a standard MTU,
467                          * then map them to a single descriptor in the Tx
468                          * Desc ring. However, if they're larger, as is
469                          * possible with support for jumbo packets, then
470                          * split them each across 2 descriptors.
471                          *
472                          * This will work until we determine why the hardware
473                          * doesn't seem to like large fragments.
474                          */
475                         if ((skb->len - skb->data_len) <= 1514) {
476                                 desc[frag].addr_hi = 0;
477                                 /* Low 16bits are length, high is vlan and
478                                    unused currently so zero */
479                                 desc[frag].len_vlan =
480                                         skb->len - skb->data_len;
481
482                                 /* NOTE: Here, the dma_addr_t returned from
483                                  * pci_map_single() is implicitly cast as a
484                                  * u32. Although dma_addr_t can be
485                                  * 64-bit, the address returned by
486                                  * pci_map_single() is always 32-bit
487                                  * addressable (as defined by the pci/dma
488                                  * subsystem)
489                                  */
490                                 desc[frag++].addr_lo =
491                                     pci_map_single(etdev->pdev,
492                                                    skb->data,
493                                                    skb->len -
494                                                    skb->data_len,
495                                                    PCI_DMA_TODEVICE);
496                         } else {
497                                 desc[frag].addr_hi = 0;
498                                 desc[frag].len_vlan =
499                                     (skb->len - skb->data_len) / 2;
500
501                                 /* NOTE: Here, the dma_addr_t returned from
502                                  * pci_map_single() is implicitly cast as a
503                                  * u32. Although dma_addr_t can be
504                                  * 64-bit, the address returned by
505                                  * pci_map_single() is always 32-bit
506                                  * addressable (as defined by the pci/dma
507                                  * subsystem)
508                                  */
509                                 desc[frag++].addr_lo =
510                                     pci_map_single(etdev->pdev,
511                                                    skb->data,
512                                                    ((skb->len -
513                                                      skb->data_len) / 2),
514                                                    PCI_DMA_TODEVICE);
515                                 desc[frag].addr_hi = 0;
516
517                                 desc[frag].len_vlan =
518                                     (skb->len - skb->data_len) / 2;
519
520                                 /* NOTE: Here, the dma_addr_t returned from
521                                  * pci_map_single() is implicitly cast as a
522                                  * u32. Although dma_addr_t can be
523                                  * 64-bit, the address returned by
524                                  * pci_map_single() is always 32-bit
525                                  * addressable (as defined by the pci/dma
526                                  * subsystem)
527                                  */
528                                 desc[frag++].addr_lo =
529                                     pci_map_single(etdev->pdev,
530                                                    skb->data +
531                                                    ((skb->len -
532                                                      skb->data_len) / 2),
533                                                    ((skb->len -
534                                                      skb->data_len) / 2),
535                                                    PCI_DMA_TODEVICE);
536                         }
537                 } else {
538                         desc[frag].addr_hi = 0;
539                         desc[frag].len_vlan =
540                                         frags[i - 1].size;
541
542                         /* NOTE: Here, the dma_addr_t returned from
543                          * pci_map_page() is implicitly cast as a u32.
544                          * Although dma_addr_t can be 64-bit, the address
545                          * returned by pci_map_page() is always 32-bit
546                          * addressable (as defined by the pci/dma subsystem)
547                          */
548                         desc[frag++].addr_lo =
549                             pci_map_page(etdev->pdev,
550                                          frags[i - 1].page,
551                                          frags[i - 1].page_offset,
552                                          frags[i - 1].size,
553                                          PCI_DMA_TODEVICE);
554                 }
555         }
556
557         if (frag == 0)
558                 return -EIO;
559
560         if (etdev->linkspeed == TRUEPHY_SPEED_1000MBPS) {
561                 if (++etdev->tx_ring.TxPacketsSinceLastinterrupt ==
562                     PARM_TX_NUM_BUFS_DEF) {
563                         /* Last element & Interrupt flag */
564                         desc[frag - 1].flags = 0x5;
565                         etdev->tx_ring.TxPacketsSinceLastinterrupt = 0;
566                 } else { /* Last element */
567                         desc[frag - 1].flags = 0x1;
568                 }
569         } else {
570                 desc[frag - 1].flags = 0x5;
571         }
572         desc[0].flags |= 2;     /* First element flag */
573
574         tcb->WrIndexStart = etdev->tx_ring.txDmaReadyToSend;
575         tcb->PacketStaleCount = 0;
576
577         spin_lock_irqsave(&etdev->SendHWLock, flags);
578
579         thiscopy = NUM_DESC_PER_RING_TX -
580                                 INDEX10(etdev->tx_ring.txDmaReadyToSend);
581
582         if (thiscopy >= frag) {
583                 remainder = 0;
584                 thiscopy = frag;
585         } else {
586                 remainder = frag - thiscopy;
587         }
588
589         memcpy(etdev->tx_ring.tx_desc_ring +
590                INDEX10(etdev->tx_ring.txDmaReadyToSend), desc,
591                sizeof(struct tx_desc) * thiscopy);
592
593         add_10bit(&etdev->tx_ring.txDmaReadyToSend, thiscopy);
594
595         if (INDEX10(etdev->tx_ring.txDmaReadyToSend)== 0 ||
596             INDEX10(etdev->tx_ring.txDmaReadyToSend) == NUM_DESC_PER_RING_TX) {
597                 etdev->tx_ring.txDmaReadyToSend &= ~ET_DMA10_MASK;
598                 etdev->tx_ring.txDmaReadyToSend ^= ET_DMA10_WRAP;
599         }
600
601         if (remainder) {
602                 memcpy(etdev->tx_ring.tx_desc_ring,
603                        desc + thiscopy,
604                        sizeof(struct tx_desc) * remainder);
605
606                 add_10bit(&etdev->tx_ring.txDmaReadyToSend, remainder);
607         }
608
609         if (INDEX10(etdev->tx_ring.txDmaReadyToSend) == 0) {
610                 if (etdev->tx_ring.txDmaReadyToSend)
611                         tcb->WrIndex = NUM_DESC_PER_RING_TX - 1;
612                 else
613                         tcb->WrIndex= ET_DMA10_WRAP | (NUM_DESC_PER_RING_TX - 1);
614         } else
615                 tcb->WrIndex = etdev->tx_ring.txDmaReadyToSend - 1;
616
617         spin_lock(&etdev->TCBSendQLock);
618
619         if (etdev->tx_ring.CurrSendTail)
620                 etdev->tx_ring.CurrSendTail->Next = tcb;
621         else
622                 etdev->tx_ring.CurrSendHead = tcb;
623
624         etdev->tx_ring.CurrSendTail = tcb;
625
626         WARN_ON(tcb->Next != NULL);
627
628         etdev->tx_ring.nBusySend++;
629
630         spin_unlock(&etdev->TCBSendQLock);
631
632         /* Write the new write pointer back to the device. */
633         writel(etdev->tx_ring.txDmaReadyToSend,
634                &etdev->regs->txdma.service_request);
635
636         /* For Gig only, we use Tx Interrupt coalescing.  Enable the software
637          * timer to wake us up if this packet isn't followed by N more.
638          */
639         if (etdev->linkspeed == TRUEPHY_SPEED_1000MBPS) {
640                 writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
641                        &etdev->regs->global.watchdog_timer);
642         }
643         spin_unlock_irqrestore(&etdev->SendHWLock, flags);
644
645         return 0;
646 }
647
648
649 /**
650  * et131x_free_send_packet - Recycle a struct tcb
651  * @etdev: pointer to our adapter
652  * @tcb: pointer to struct tcb
653  *
654  * Complete the packet if necessary
655  * Assumption - Send spinlock has been acquired
656  */
657 inline void et131x_free_send_packet(struct et131x_adapter *etdev,
658                                                 struct tcb *tcb)
659 {
660         unsigned long flags;
661         struct tx_desc *desc = NULL;
662         struct net_device_stats *stats = &etdev->net_stats;
663
664         if (tcb->Flags & fMP_DEST_BROAD)
665                 atomic_inc(&etdev->Stats.brdcstxmt);
666         else if (tcb->Flags & fMP_DEST_MULTI)
667                 atomic_inc(&etdev->Stats.multixmt);
668         else
669                 atomic_inc(&etdev->Stats.unixmt);
670
671         if (tcb->Packet) {
672                 stats->tx_bytes += tcb->Packet->len;
673
674                 /* Iterate through the TX descriptors on the ring
675                  * corresponding to this packet and umap the fragments
676                  * they point to
677                  */
678                 do {
679                         desc =(struct tx_desc *) (etdev->tx_ring.tx_desc_ring +
680                                                 INDEX10(tcb->WrIndexStart));
681
682                         pci_unmap_single(etdev->pdev,
683                                          desc->addr_lo,
684                                          desc->len_vlan, PCI_DMA_TODEVICE);
685
686                         add_10bit(&tcb->WrIndexStart, 1);
687                         if (INDEX10(tcb->WrIndexStart) >=
688                             NUM_DESC_PER_RING_TX) {
689                                 tcb->WrIndexStart &= ~ET_DMA10_MASK;
690                                 tcb->WrIndexStart ^= ET_DMA10_WRAP;
691                         }
692                 } while (desc != (etdev->tx_ring.tx_desc_ring +
693                                 INDEX10(tcb->WrIndex)));
694
695                 dev_kfree_skb_any(tcb->Packet);
696         }
697
698         memset(tcb, 0, sizeof(struct tcb));
699
700         /* Add the TCB to the Ready Q */
701         spin_lock_irqsave(&etdev->TCBReadyQLock, flags);
702
703         etdev->Stats.opackets++;
704
705         if (etdev->tx_ring.TCBReadyQueueTail)
706                 etdev->tx_ring.TCBReadyQueueTail->Next = tcb;
707         else
708                 /* Apparently ready Q is empty. */
709                 etdev->tx_ring.TCBReadyQueueHead = tcb;
710
711         etdev->tx_ring.TCBReadyQueueTail = tcb;
712
713         spin_unlock_irqrestore(&etdev->TCBReadyQLock, flags);
714         WARN_ON(etdev->tx_ring.nBusySend < 0);
715 }
716
717 /**
718  * et131x_free_busy_send_packets - Free and complete the stopped active sends
719  * @etdev: pointer to our adapter
720  *
721  * Assumption - Send spinlock has been acquired
722  */
723 void et131x_free_busy_send_packets(struct et131x_adapter *etdev)
724 {
725         struct tcb *tcb;
726         unsigned long flags;
727         u32 freed = 0;
728
729         /* Any packets being sent? Check the first TCB on the send list */
730         spin_lock_irqsave(&etdev->TCBSendQLock, flags);
731
732         tcb = etdev->tx_ring.CurrSendHead;
733
734         while ((tcb != NULL) && (freed < NUM_TCB)) {
735                 struct tcb *pNext = tcb->Next;
736
737                 etdev->tx_ring.CurrSendHead = pNext;
738
739                 if (pNext == NULL)
740                         etdev->tx_ring.CurrSendTail = NULL;
741
742                 etdev->tx_ring.nBusySend--;
743
744                 spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
745
746                 freed++;
747                 et131x_free_send_packet(etdev, tcb);
748
749                 spin_lock_irqsave(&etdev->TCBSendQLock, flags);
750
751                 tcb = etdev->tx_ring.CurrSendHead;
752         }
753
754         WARN_ON(freed == NUM_TCB);
755
756         spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
757
758         etdev->tx_ring.nBusySend = 0;
759 }
760
761 /**
762  * et131x_handle_send_interrupt - Interrupt handler for sending processing
763  * @etdev: pointer to our adapter
764  *
765  * Re-claim the send resources, complete sends and get more to send from
766  * the send wait queue.
767  *
768  * Assumption - Send spinlock has been acquired
769  */
770 void et131x_handle_send_interrupt(struct et131x_adapter *etdev)
771 {
772         unsigned long flags;
773         u32 serviced;
774         struct tcb * tcb;
775         u32 index;
776
777         serviced = readl(&etdev->regs->txdma.NewServiceComplete);
778         index = INDEX10(serviced);
779
780         /* Has the ring wrapped?  Process any descriptors that do not have
781          * the same "wrap" indicator as the current completion indicator
782          */
783         spin_lock_irqsave(&etdev->TCBSendQLock, flags);
784
785         tcb = etdev->tx_ring.CurrSendHead;
786
787         while (tcb &&
788                ((serviced ^ tcb->WrIndex) & ET_DMA10_WRAP) &&
789                index < INDEX10(tcb->WrIndex)) {
790                 etdev->tx_ring.nBusySend--;
791                 etdev->tx_ring.CurrSendHead = tcb->Next;
792                 if (tcb->Next == NULL)
793                         etdev->tx_ring.CurrSendTail = NULL;
794
795                 spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
796                 et131x_free_send_packet(etdev, tcb);
797                 spin_lock_irqsave(&etdev->TCBSendQLock, flags);
798
799                 /* Goto the next packet */
800                 tcb = etdev->tx_ring.CurrSendHead;
801         }
802         while (tcb &&
803                !((serviced ^ tcb->WrIndex) & ET_DMA10_WRAP)
804                && index > (tcb->WrIndex & ET_DMA10_MASK)) {
805                 etdev->tx_ring.nBusySend--;
806                 etdev->tx_ring.CurrSendHead = tcb->Next;
807                 if (tcb->Next == NULL)
808                         etdev->tx_ring.CurrSendTail = NULL;
809
810                 spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
811                 et131x_free_send_packet(etdev, tcb);
812                 spin_lock_irqsave(&etdev->TCBSendQLock, flags);
813
814                 /* Goto the next packet */
815                 tcb = etdev->tx_ring.CurrSendHead;
816         }
817
818         /* Wake up the queue when we hit a low-water mark */
819         if (etdev->tx_ring.nBusySend <= (NUM_TCB / 3))
820                 netif_wake_queue(etdev->netdev);
821
822         spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
823 }
824