4aabfa312265cc898a6a0e3c609c04833e0fc301
[firefly-linux-kernel-4.4.55.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 void et131x_update_tcb_list(struct et131x_adapter *etdev);
98 static inline void et131x_free_send_packet(struct et131x_adapter *etdev,
99                                            struct tcb *tcb);
100 static int et131x_send_packet(struct sk_buff *skb,
101                               struct et131x_adapter *etdev);
102 static int nic_send_packet(struct et131x_adapter *etdev, struct tcb *tcb);
103
104 /**
105  * et131x_tx_dma_memory_alloc
106  * @adapter: pointer to our private adapter structure
107  *
108  * Returns 0 on success and errno on failure (as defined in errno.h).
109  *
110  * Allocates memory that will be visible both to the device and to the CPU.
111  * The OS will pass us packets, pointers to which we will insert in the Tx
112  * Descriptor queue. The device will read this queue to find the packets in
113  * memory. The device will update the "status" in memory each time it xmits a
114  * packet.
115  */
116 int et131x_tx_dma_memory_alloc(struct et131x_adapter *adapter)
117 {
118         int desc_size = 0;
119         struct tx_ring *tx_ring = &adapter->tx_ring;
120
121         /* Allocate memory for the TCB's (Transmit Control Block) */
122         adapter->tx_ring.MpTcbMem = (struct tcb *)
123                 kcalloc(NUM_TCB, sizeof(struct tcb), GFP_ATOMIC | GFP_DMA);
124         if (!adapter->tx_ring.MpTcbMem) {
125                 dev_err(&adapter->pdev->dev, "Cannot alloc memory for TCBs\n");
126                 return -ENOMEM;
127         }
128
129         /* Allocate enough memory for the Tx descriptor ring, and allocate
130          * some extra so that the ring can be aligned on a 4k boundary.
131          */
132         desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX) + 4096 - 1;
133         tx_ring->tx_desc_ring =
134             (struct tx_desc *) pci_alloc_consistent(adapter->pdev, desc_size,
135                                                     &tx_ring->tx_desc_ring_pa);
136         if (!adapter->tx_ring.tx_desc_ring) {
137                 dev_err(&adapter->pdev->dev, "Cannot alloc memory for Tx Ring\n");
138                 return -ENOMEM;
139         }
140
141         /* Save physical address
142          *
143          * NOTE: pci_alloc_consistent(), used above to alloc DMA regions,
144          * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
145          * are ever returned, make sure the high part is retrieved here before
146          * storing the adjusted address.
147          */
148         /* Allocate memory for the Tx status block */
149         tx_ring->pTxStatusVa = pci_alloc_consistent(adapter->pdev,
150                                                     sizeof(TX_STATUS_BLOCK_t),
151                                                     &tx_ring->pTxStatusPa);
152         if (!adapter->tx_ring.pTxStatusPa) {
153                 dev_err(&adapter->pdev->dev,
154                                   "Cannot alloc memory for Tx status block\n");
155                 return -ENOMEM;
156         }
157
158         /* Allocate memory for a dummy buffer */
159         tx_ring->pTxDummyBlkVa = pci_alloc_consistent(adapter->pdev,
160                                                       NIC_MIN_PACKET_SIZE,
161                                                       &tx_ring->pTxDummyBlkPa);
162         if (!adapter->tx_ring.pTxDummyBlkPa) {
163                 dev_err(&adapter->pdev->dev,
164                         "Cannot alloc memory for Tx dummy buffer\n");
165                 return -ENOMEM;
166         }
167
168         return 0;
169 }
170
171 /**
172  * et131x_tx_dma_memory_free - Free all memory allocated within this module
173  * @adapter: pointer to our private adapter structure
174  *
175  * Returns 0 on success and errno on failure (as defined in errno.h).
176  */
177 void et131x_tx_dma_memory_free(struct et131x_adapter *adapter)
178 {
179         int desc_size = 0;
180
181         if (adapter->tx_ring.tx_desc_ring) {
182                 /* Free memory relating to Tx rings here */
183                 desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX)
184                                                                         + 4096 - 1;
185                 pci_free_consistent(adapter->pdev,
186                                     desc_size,
187                                     adapter->tx_ring.tx_desc_ring,
188                                     adapter->tx_ring.tx_desc_ring_pa);
189                 adapter->tx_ring.tx_desc_ring = NULL;
190         }
191
192         /* Free memory for the Tx status block */
193         if (adapter->tx_ring.pTxStatusVa) {
194                 pci_free_consistent(adapter->pdev,
195                                     sizeof(TX_STATUS_BLOCK_t),
196                                     adapter->tx_ring.pTxStatusVa,
197                                     adapter->tx_ring.pTxStatusPa);
198
199                 adapter->tx_ring.pTxStatusVa = NULL;
200         }
201
202         /* Free memory for the dummy buffer */
203         if (adapter->tx_ring.pTxDummyBlkVa) {
204                 pci_free_consistent(adapter->pdev,
205                                     NIC_MIN_PACKET_SIZE,
206                                     adapter->tx_ring.pTxDummyBlkVa,
207                                     adapter->tx_ring.pTxDummyBlkPa);
208
209                 adapter->tx_ring.pTxDummyBlkVa = NULL;
210         }
211
212         /* Free the memory for the tcb structures */
213         kfree(adapter->tx_ring.MpTcbMem);
214 }
215
216 /**
217  * ConfigTxDmaRegs - Set up the tx dma section of the JAGCore.
218  * @etdev: pointer to our private adapter structure
219  */
220 void ConfigTxDmaRegs(struct et131x_adapter *etdev)
221 {
222         struct _TXDMA_t __iomem *txdma = &etdev->regs->txdma;
223
224         /* Load the hardware with the start of the transmit descriptor ring. */
225         writel((u32) ((u64)etdev->tx_ring.tx_desc_ring_pa >> 32),
226                &txdma->pr_base_hi);
227         writel((u32) etdev->tx_ring.tx_desc_ring_pa,
228                &txdma->pr_base_lo);
229
230         /* Initialise the transmit DMA engine */
231         writel(NUM_DESC_PER_RING_TX - 1, &txdma->pr_num_des.value);
232
233         /* Load the completion writeback physical address
234          *
235          * NOTE: pci_alloc_consistent(), used above to alloc DMA regions,
236          * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
237          * are ever returned, make sure the high part is retrieved here before
238          * storing the adjusted address.
239          */
240         writel(0, &txdma->dma_wb_base_hi);
241         writel(etdev->tx_ring.pTxStatusPa, &txdma->dma_wb_base_lo);
242
243         memset(etdev->tx_ring.pTxStatusVa, 0, sizeof(TX_STATUS_BLOCK_t));
244
245         writel(0, &txdma->service_request);
246         etdev->tx_ring.txDmaReadyToSend = 0;
247 }
248
249 /**
250  * et131x_tx_dma_disable - Stop of Tx_DMA on the ET1310
251  * @etdev: pointer to our adapter structure
252  */
253 void et131x_tx_dma_disable(struct et131x_adapter *etdev)
254 {
255         /* Setup the tramsmit dma configuration register */
256         writel(ET_TXDMA_CSR_HALT|ET_TXDMA_SNGL_EPKT,
257                                         &etdev->regs->txdma.csr);
258 }
259
260 /**
261  * et131x_tx_dma_enable - re-start of Tx_DMA on the ET1310.
262  * @etdev: pointer to our adapter structure
263  *
264  * Mainly used after a return to the D0 (full-power) state from a lower state.
265  */
266 void et131x_tx_dma_enable(struct et131x_adapter *etdev)
267 {
268         /* Setup the transmit dma configuration register for normal
269          * operation
270          */
271         writel(ET_TXDMA_SNGL_EPKT|(PARM_DMA_CACHE_DEF << ET_TXDMA_CACHE_SHIFT),
272                                         &etdev->regs->txdma.csr);
273 }
274
275 /**
276  * et131x_init_send - Initialize send data structures
277  * @adapter: pointer to our private adapter structure
278  */
279 void et131x_init_send(struct et131x_adapter *adapter)
280 {
281         struct tcb *tcb;
282         u32 count;
283         struct tx_ring *tx_ring;
284
285         /* Setup some convenience pointers */
286         tx_ring = &adapter->tx_ring;
287         tcb = adapter->tx_ring.MpTcbMem;
288
289         tx_ring->TCBReadyQueueHead = tcb;
290
291         /* Go through and set up each TCB */
292         for (count = 0; count < NUM_TCB; count++) {
293                 memset(tcb, 0, sizeof(struct tcb));
294
295                 /* Set the link pointer in HW TCB to the next TCB in the
296                  * chain.  If this is the last TCB in the chain, also set the
297                  * tail pointer.
298                  */
299                 if (count < NUM_TCB - 1) {
300                         tcb->Next = tcb + 1;
301                 } else {
302                         tx_ring->TCBReadyQueueTail = tcb;
303                         tcb->Next = NULL;
304                 }
305
306                 tcb++;
307         }
308
309         /* Curr send queue should now be empty */
310         tx_ring->CurrSendHead = NULL;
311         tx_ring->CurrSendTail = NULL;
312 }
313
314 /**
315  * et131x_send_packets - This function is called by the OS to send packets
316  * @skb: the packet(s) to send
317  * @netdev:device on which to TX the above packet(s)
318  *
319  * Return 0 in almost all cases; non-zero value in extreme hard failure only
320  */
321 int et131x_send_packets(struct sk_buff *skb, struct net_device *netdev)
322 {
323         int status = 0;
324         struct et131x_adapter *etdev = NULL;
325
326         etdev = netdev_priv(netdev);
327
328         /* Send these packets
329          *
330          * NOTE: The Linux Tx entry point is only given one packet at a time
331          * to Tx, so the PacketCount and it's array used makes no sense here
332          */
333
334         /* TCB is not available */
335         if (MP_TCB_RESOURCES_NOT_AVAILABLE(etdev)) {
336                 /* NOTE: If there's an error on send, no need to queue the
337                  * packet under Linux; if we just send an error up to the
338                  * netif layer, it will resend the skb to us.
339                  */
340                 status = -ENOMEM;
341         } else {
342                 /* We need to see if the link is up; if it's not, make the
343                  * netif layer think we're good and drop the packet
344                  */
345                 /*
346                  * if( MP_SHOULD_FAIL_SEND( etdev ) ||
347                  *  etdev->DriverNoPhyAccess )
348                  */
349                 if (MP_SHOULD_FAIL_SEND(etdev) || !netif_carrier_ok(netdev)) {
350                         dev_kfree_skb_any(skb);
351                         skb = NULL;
352
353                         etdev->net_stats.tx_dropped++;
354                 } else {
355                         status = et131x_send_packet(skb, etdev);
356
357                         if (status == -ENOMEM) {
358
359                                 /* NOTE: If there's an error on send, no need
360                                  * to queue the packet under Linux; if we just
361                                  * send an error up to the netif layer, it
362                                  * will resend the skb to us.
363                                  */
364                         } else if (status != 0) {
365                                 /* On any other error, make netif think we're
366                                  * OK and drop the packet
367                                  */
368                                 dev_kfree_skb_any(skb);
369                                 skb = NULL;
370                                 etdev->net_stats.tx_dropped++;
371                         }
372                 }
373         }
374         return status;
375 }
376
377 /**
378  * et131x_send_packet - Do the work to send a packet
379  * @skb: the packet(s) to send
380  * @etdev: a pointer to the device's private adapter structure
381  *
382  * Return 0 in almost all cases; non-zero value in extreme hard failure only.
383  *
384  * Assumption: Send spinlock has been acquired
385  */
386 static int et131x_send_packet(struct sk_buff *skb,
387                               struct et131x_adapter *etdev)
388 {
389         int status = 0;
390         struct tcb *tcb = NULL;
391         u16 *shbufva;
392         unsigned long flags;
393
394         /* All packets must have at least a MAC address and a protocol type */
395         if (skb->len < ETH_HLEN)
396                 return -EIO;
397
398         /* Get a TCB for this packet */
399         spin_lock_irqsave(&etdev->TCBReadyQLock, flags);
400
401         tcb = etdev->tx_ring.TCBReadyQueueHead;
402
403         if (tcb == NULL) {
404                 spin_unlock_irqrestore(&etdev->TCBReadyQLock, flags);
405                 return -ENOMEM;
406         }
407
408         etdev->tx_ring.TCBReadyQueueHead = tcb->Next;
409
410         if (etdev->tx_ring.TCBReadyQueueHead == NULL)
411                 etdev->tx_ring.TCBReadyQueueTail = NULL;
412
413         spin_unlock_irqrestore(&etdev->TCBReadyQLock, flags);
414
415         tcb->PacketLength = skb->len;
416         tcb->Packet = skb;
417
418         if ((skb->data != NULL) && ((skb->len - skb->data_len) >= 6)) {
419                 shbufva = (u16 *) skb->data;
420
421                 if ((shbufva[0] == 0xffff) &&
422                     (shbufva[1] == 0xffff) && (shbufva[2] == 0xffff)) {
423                         tcb->Flags |= fMP_DEST_BROAD;
424                 } else if ((shbufva[0] & 0x3) == 0x0001) {
425                         tcb->Flags |=  fMP_DEST_MULTI;
426                 }
427         }
428
429         tcb->Next = NULL;
430
431         /* Call the NIC specific send handler. */
432         if (status == 0)
433                 status = nic_send_packet(etdev, tcb);
434
435         if (status != 0) {
436                 spin_lock_irqsave(&etdev->TCBReadyQLock, flags);
437
438                 if (etdev->tx_ring.TCBReadyQueueTail) {
439                         etdev->tx_ring.TCBReadyQueueTail->Next = tcb;
440                 } else {
441                         /* Apparently ready Q is empty. */
442                         etdev->tx_ring.TCBReadyQueueHead = tcb;
443                 }
444
445                 etdev->tx_ring.TCBReadyQueueTail = tcb;
446                 spin_unlock_irqrestore(&etdev->TCBReadyQLock, flags);
447                 return status;
448         }
449         WARN_ON(etdev->tx_ring.nBusySend > NUM_TCB);
450         return 0;
451 }
452
453 /**
454  * nic_send_packet - NIC specific send handler for version B silicon.
455  * @etdev: pointer to our adapter
456  * @tcb: pointer to struct tcb
457  *
458  * Returns 0 or errno.
459  */
460 static int nic_send_packet(struct et131x_adapter *etdev, struct tcb *tcb)
461 {
462         u32 i;
463         struct tx_desc desc[24];        /* 24 x 16 byte */
464         u32 frag = 0;
465         u32 thiscopy, remainder;
466         struct sk_buff *skb = tcb->Packet;
467         u32 nr_frags = skb_shinfo(skb)->nr_frags + 1;
468         struct skb_frag_struct *frags = &skb_shinfo(skb)->frags[0];
469         unsigned long flags;
470
471         /* Part of the optimizations of this send routine restrict us to
472          * sending 24 fragments at a pass.  In practice we should never see
473          * more than 5 fragments.
474          *
475          * NOTE: The older version of this function (below) can handle any
476          * number of fragments. If needed, we can call this function,
477          * although it is less efficient.
478          */
479         if (nr_frags > 23)
480                 return -EIO;
481
482         memset(desc, 0, sizeof(struct tx_desc) * (nr_frags + 1));
483
484         for (i = 0; i < nr_frags; i++) {
485                 /* If there is something in this element, lets get a
486                  * descriptor from the ring and get the necessary data
487                  */
488                 if (i == 0) {
489                         /* If the fragments are smaller than a standard MTU,
490                          * then map them to a single descriptor in the Tx
491                          * Desc ring. However, if they're larger, as is
492                          * possible with support for jumbo packets, then
493                          * split them each across 2 descriptors.
494                          *
495                          * This will work until we determine why the hardware
496                          * doesn't seem to like large fragments.
497                          */
498                         if ((skb->len - skb->data_len) <= 1514) {
499                                 desc[frag].addr_hi = 0;
500                                 /* Low 16bits are length, high is vlan and
501                                    unused currently so zero */
502                                 desc[frag].len_vlan =
503                                         skb->len - skb->data_len;
504
505                                 /* NOTE: Here, the dma_addr_t returned from
506                                  * pci_map_single() is implicitly cast as a
507                                  * u32. Although dma_addr_t can be
508                                  * 64-bit, the address returned by
509                                  * pci_map_single() is always 32-bit
510                                  * addressable (as defined by the pci/dma
511                                  * subsystem)
512                                  */
513                                 desc[frag++].addr_lo =
514                                     pci_map_single(etdev->pdev,
515                                                    skb->data,
516                                                    skb->len -
517                                                    skb->data_len,
518                                                    PCI_DMA_TODEVICE);
519                         } else {
520                                 desc[frag].addr_hi = 0;
521                                 desc[frag].len_vlan =
522                                     (skb->len - skb->data_len) / 2;
523
524                                 /* NOTE: Here, the dma_addr_t returned from
525                                  * pci_map_single() is implicitly cast as a
526                                  * u32. Although dma_addr_t can be
527                                  * 64-bit, the address returned by
528                                  * pci_map_single() is always 32-bit
529                                  * addressable (as defined by the pci/dma
530                                  * subsystem)
531                                  */
532                                 desc[frag++].addr_lo =
533                                     pci_map_single(etdev->pdev,
534                                                    skb->data,
535                                                    ((skb->len -
536                                                      skb->data_len) / 2),
537                                                    PCI_DMA_TODEVICE);
538                                 desc[frag].addr_hi = 0;
539
540                                 desc[frag].len_vlan =
541                                     (skb->len - skb->data_len) / 2;
542
543                                 /* NOTE: Here, the dma_addr_t returned from
544                                  * pci_map_single() is implicitly cast as a
545                                  * u32. Although dma_addr_t can be
546                                  * 64-bit, the address returned by
547                                  * pci_map_single() is always 32-bit
548                                  * addressable (as defined by the pci/dma
549                                  * subsystem)
550                                  */
551                                 desc[frag++].addr_lo =
552                                     pci_map_single(etdev->pdev,
553                                                    skb->data +
554                                                    ((skb->len -
555                                                      skb->data_len) / 2),
556                                                    ((skb->len -
557                                                      skb->data_len) / 2),
558                                                    PCI_DMA_TODEVICE);
559                         }
560                 } else {
561                         desc[frag].addr_hi = 0;
562                         desc[frag].len_vlan =
563                                         frags[i - 1].size;
564
565                         /* NOTE: Here, the dma_addr_t returned from
566                          * pci_map_page() is implicitly cast as a u32.
567                          * Although dma_addr_t can be 64-bit, the address
568                          * returned by pci_map_page() is always 32-bit
569                          * addressable (as defined by the pci/dma subsystem)
570                          */
571                         desc[frag++].addr_lo =
572                             pci_map_page(etdev->pdev,
573                                          frags[i - 1].page,
574                                          frags[i - 1].page_offset,
575                                          frags[i - 1].size,
576                                          PCI_DMA_TODEVICE);
577                 }
578         }
579
580         if (frag == 0)
581                 return -EIO;
582
583         if (etdev->linkspeed == TRUEPHY_SPEED_1000MBPS) {
584                 if (++etdev->tx_ring.TxPacketsSinceLastinterrupt ==
585                     PARM_TX_NUM_BUFS_DEF) {
586                         /* Last element & Interrupt flag */
587                         desc[frag - 1].flags = 0x5;
588                         etdev->tx_ring.TxPacketsSinceLastinterrupt = 0;
589                 } else { /* Last element */
590                         desc[frag - 1].flags = 0x1;
591                 }
592         } else {
593                 desc[frag - 1].flags = 0x5;
594         }
595         desc[0].flags |= 2;     /* First element flag */
596
597         tcb->WrIndexStart = etdev->tx_ring.txDmaReadyToSend;
598         tcb->PacketStaleCount = 0;
599
600         spin_lock_irqsave(&etdev->SendHWLock, flags);
601
602         thiscopy = NUM_DESC_PER_RING_TX -
603                                 INDEX10(etdev->tx_ring.txDmaReadyToSend);
604
605         if (thiscopy >= frag) {
606                 remainder = 0;
607                 thiscopy = frag;
608         } else {
609                 remainder = frag - thiscopy;
610         }
611
612         memcpy(etdev->tx_ring.tx_desc_ring +
613                INDEX10(etdev->tx_ring.txDmaReadyToSend), desc,
614                sizeof(struct tx_desc) * thiscopy);
615
616         add_10bit(&etdev->tx_ring.txDmaReadyToSend, thiscopy);
617
618         if (INDEX10(etdev->tx_ring.txDmaReadyToSend)== 0 ||
619             INDEX10(etdev->tx_ring.txDmaReadyToSend) == NUM_DESC_PER_RING_TX) {
620                 etdev->tx_ring.txDmaReadyToSend &= ~ET_DMA10_MASK;
621                 etdev->tx_ring.txDmaReadyToSend ^= ET_DMA10_WRAP;
622         }
623
624         if (remainder) {
625                 memcpy(etdev->tx_ring.tx_desc_ring,
626                        desc + thiscopy,
627                        sizeof(struct tx_desc) * remainder);
628
629                 add_10bit(&etdev->tx_ring.txDmaReadyToSend, remainder);
630         }
631
632         if (INDEX10(etdev->tx_ring.txDmaReadyToSend) == 0) {
633                 if (etdev->tx_ring.txDmaReadyToSend)
634                         tcb->WrIndex = NUM_DESC_PER_RING_TX - 1;
635                 else
636                         tcb->WrIndex= ET_DMA10_WRAP | (NUM_DESC_PER_RING_TX - 1);
637         } else
638                 tcb->WrIndex = etdev->tx_ring.txDmaReadyToSend - 1;
639
640         spin_lock(&etdev->TCBSendQLock);
641
642         if (etdev->tx_ring.CurrSendTail)
643                 etdev->tx_ring.CurrSendTail->Next = tcb;
644         else
645                 etdev->tx_ring.CurrSendHead = tcb;
646
647         etdev->tx_ring.CurrSendTail = tcb;
648
649         WARN_ON(tcb->Next != NULL);
650
651         etdev->tx_ring.nBusySend++;
652
653         spin_unlock(&etdev->TCBSendQLock);
654
655         /* Write the new write pointer back to the device. */
656         writel(etdev->tx_ring.txDmaReadyToSend,
657                &etdev->regs->txdma.service_request);
658
659         /* For Gig only, we use Tx Interrupt coalescing.  Enable the software
660          * timer to wake us up if this packet isn't followed by N more.
661          */
662         if (etdev->linkspeed == TRUEPHY_SPEED_1000MBPS) {
663                 writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
664                        &etdev->regs->global.watchdog_timer);
665         }
666         spin_unlock_irqrestore(&etdev->SendHWLock, flags);
667
668         return 0;
669 }
670
671
672 /**
673  * et131x_free_send_packet - Recycle a struct tcb
674  * @etdev: pointer to our adapter
675  * @tcb: pointer to struct tcb
676  *
677  * Complete the packet if necessary
678  * Assumption - Send spinlock has been acquired
679  */
680 inline void et131x_free_send_packet(struct et131x_adapter *etdev,
681                                                 struct tcb *tcb)
682 {
683         unsigned long flags;
684         struct tx_desc *desc = NULL;
685         struct net_device_stats *stats = &etdev->net_stats;
686
687         if (tcb->Flags & fMP_DEST_BROAD)
688                 atomic_inc(&etdev->Stats.brdcstxmt);
689         else if (tcb->Flags & fMP_DEST_MULTI)
690                 atomic_inc(&etdev->Stats.multixmt);
691         else
692                 atomic_inc(&etdev->Stats.unixmt);
693
694         if (tcb->Packet) {
695                 stats->tx_bytes += tcb->Packet->len;
696
697                 /* Iterate through the TX descriptors on the ring
698                  * corresponding to this packet and umap the fragments
699                  * they point to
700                  */
701                 do {
702                         desc =(struct tx_desc *) (etdev->tx_ring.tx_desc_ring +
703                                                 INDEX10(tcb->WrIndexStart));
704
705                         pci_unmap_single(etdev->pdev,
706                                          desc->addr_lo,
707                                          desc->len_vlan, PCI_DMA_TODEVICE);
708
709                         add_10bit(&tcb->WrIndexStart, 1);
710                         if (INDEX10(tcb->WrIndexStart) >=
711                             NUM_DESC_PER_RING_TX) {
712                                 tcb->WrIndexStart &= ~ET_DMA10_MASK;
713                                 tcb->WrIndexStart ^= ET_DMA10_WRAP;
714                         }
715                 } while (desc != (etdev->tx_ring.tx_desc_ring +
716                                 INDEX10(tcb->WrIndex)));
717
718                 dev_kfree_skb_any(tcb->Packet);
719         }
720
721         memset(tcb, 0, sizeof(struct tcb));
722
723         /* Add the TCB to the Ready Q */
724         spin_lock_irqsave(&etdev->TCBReadyQLock, flags);
725
726         etdev->Stats.opackets++;
727
728         if (etdev->tx_ring.TCBReadyQueueTail) {
729                 etdev->tx_ring.TCBReadyQueueTail->Next = tcb;
730         } else {
731                 /* Apparently ready Q is empty. */
732                 etdev->tx_ring.TCBReadyQueueHead = tcb;
733         }
734
735         etdev->tx_ring.TCBReadyQueueTail = tcb;
736
737         spin_unlock_irqrestore(&etdev->TCBReadyQLock, flags);
738         WARN_ON(etdev->tx_ring.nBusySend < 0);
739 }
740
741 /**
742  * et131x_free_busy_send_packets - Free and complete the stopped active sends
743  * @etdev: pointer to our adapter
744  *
745  * Assumption - Send spinlock has been acquired
746  */
747 void et131x_free_busy_send_packets(struct et131x_adapter *etdev)
748 {
749         struct tcb *tcb;
750         struct list_head *entry;
751         unsigned long flags;
752         u32 freed = 0;
753
754         /* Any packets being sent? Check the first TCB on the send list */
755         spin_lock_irqsave(&etdev->TCBSendQLock, flags);
756
757         tcb = etdev->tx_ring.CurrSendHead;
758
759         while ((tcb != NULL) && (freed < NUM_TCB)) {
760                 struct tcb *pNext = tcb->Next;
761
762                 etdev->tx_ring.CurrSendHead = pNext;
763
764                 if (pNext == NULL)
765                         etdev->tx_ring.CurrSendTail = NULL;
766
767                 etdev->tx_ring.nBusySend--;
768
769                 spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
770
771                 freed++;
772                 et131x_free_send_packet(etdev, tcb);
773
774                 spin_lock_irqsave(&etdev->TCBSendQLock, flags);
775
776                 tcb = etdev->tx_ring.CurrSendHead;
777         }
778
779         WARN_ON(freed == NUM_TCB);
780
781         spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
782
783         etdev->tx_ring.nBusySend = 0;
784 }
785
786 /**
787  * et131x_handle_send_interrupt - Interrupt handler for sending processing
788  * @etdev: pointer to our adapter
789  *
790  * Re-claim the send resources, complete sends and get more to send from
791  * the send wait queue.
792  *
793  * Assumption - Send spinlock has been acquired
794  */
795 void et131x_handle_send_interrupt(struct et131x_adapter *etdev)
796 {
797         /* Mark as completed any packets which have been sent by the device. */
798         et131x_update_tcb_list(etdev);
799 }
800
801 /**
802  * et131x_update_tcb_list - Helper routine for Send Interrupt handler
803  * @etdev: pointer to our adapter
804  *
805  * Re-claims the send resources and completes sends.  Can also be called as
806  * part of the NIC send routine when the "ServiceComplete" indication has
807  * wrapped.
808  */
809 static void et131x_update_tcb_list(struct et131x_adapter *etdev)
810 {
811         unsigned long flags;
812         u32 serviced;
813         struct tcb * tcb;
814         u32 index;
815
816         serviced = readl(&etdev->regs->txdma.NewServiceComplete);
817         index = INDEX10(serviced);
818
819         /* Has the ring wrapped?  Process any descriptors that do not have
820          * the same "wrap" indicator as the current completion indicator
821          */
822         spin_lock_irqsave(&etdev->TCBSendQLock, flags);
823
824         tcb = etdev->tx_ring.CurrSendHead;
825
826         while (tcb &&
827                ((serviced ^ tcb->WrIndex) & ET_DMA10_WRAP) &&
828                index < INDEX10(tcb->WrIndex)) {
829                 etdev->tx_ring.nBusySend--;
830                 etdev->tx_ring.CurrSendHead = tcb->Next;
831                 if (tcb->Next == NULL)
832                         etdev->tx_ring.CurrSendTail = NULL;
833
834                 spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
835                 et131x_free_send_packet(etdev, tcb);
836                 spin_lock_irqsave(&etdev->TCBSendQLock, flags);
837
838                 /* Goto the next packet */
839                 tcb = etdev->tx_ring.CurrSendHead;
840         }
841         while (tcb &&
842                !((serviced ^ tcb->WrIndex) & ET_DMA10_WRAP)
843                && index > (tcb->WrIndex & ET_DMA10_MASK)) {
844                 etdev->tx_ring.nBusySend--;
845                 etdev->tx_ring.CurrSendHead = tcb->Next;
846                 if (tcb->Next == NULL)
847                         etdev->tx_ring.CurrSendTail = NULL;
848
849                 spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
850                 et131x_free_send_packet(etdev, tcb);
851                 spin_lock_irqsave(&etdev->TCBSendQLock, flags);
852
853                 /* Goto the next packet */
854                 tcb = etdev->tx_ring.CurrSendHead;
855         }
856
857         /* Wake up the queue when we hit a low-water mark */
858         if (etdev->tx_ring.nBusySend <= (NUM_TCB / 3))
859                 netif_wake_queue(etdev->netdev);
860
861         spin_unlock_irqrestore(&etdev->TCBSendQLock, flags);
862 }
863