3 * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
5 * Copyright © 2005 Agere Systems Inc.
9 * Copyright (c) 2011 Mark Einon <mark.einon@gmail.com>
11 *------------------------------------------------------------------------------
13 * et1310_rx.c - Routines used to perform data reception
15 *------------------------------------------------------------------------------
19 * This software is provided subject to the following terms and conditions,
20 * which you should read carefully before using the software. Using this
21 * software indicates your acceptance of these terms and conditions. If you do
22 * not agree with these terms and conditions, do not use the software.
24 * Copyright © 2005 Agere Systems Inc.
25 * All rights reserved.
27 * Redistribution and use in source or binary forms, with or without
28 * modifications, are permitted provided that the following conditions are met:
30 * . Redistributions of source code must retain the above copyright notice, this
31 * list of conditions and the following Disclaimer as comments in the code as
32 * well as in the documentation and/or other materials provided with the
35 * . Redistributions in binary form must reproduce the above copyright notice,
36 * this list of conditions and the following Disclaimer in the documentation
37 * and/or other materials provided with the distribution.
39 * . Neither the name of Agere Systems Inc. nor the names of the contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
46 * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
47 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ANY
48 * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
49 * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
50 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
60 #include "et131x_defs.h"
62 #include <linux/pci.h>
63 #include <linux/init.h>
64 #include <linux/module.h>
65 #include <linux/types.h>
66 #include <linux/kernel.h>
68 #include <linux/sched.h>
69 #include <linux/ptrace.h>
70 #include <linux/slab.h>
71 #include <linux/ctype.h>
72 #include <linux/string.h>
73 #include <linux/timer.h>
74 #include <linux/interrupt.h>
76 #include <linux/delay.h>
78 #include <linux/bitops.h>
79 #include <asm/system.h>
81 #include <linux/netdevice.h>
82 #include <linux/etherdevice.h>
83 #include <linux/skbuff.h>
84 #include <linux/if_arp.h>
85 #include <linux/ioport.h>
86 #include <linux/phy.h>
88 #include "et1310_phy.h"
89 #include "et131x_adapter.h"
90 #include "et1310_rx.h"
93 static inline u32
bump_free_buff_ring(u32
*free_buff_ring
, u32 limit
)
95 u32 tmp_free_buff_ring
= *free_buff_ring
;
97 /* This works for all cases where limit < 1024. The 1023 case
98 works because 1023++ is 1024 which means the if condition is not
99 taken but the carry of the bit into the wrap bit toggles the wrap
101 if ((tmp_free_buff_ring
& ET_DMA10_MASK
) > limit
) {
102 tmp_free_buff_ring
&= ~ET_DMA10_MASK
;
103 tmp_free_buff_ring
^= ET_DMA10_WRAP
;
105 /* For the 1023 case */
106 tmp_free_buff_ring
&= (ET_DMA10_MASK
|ET_DMA10_WRAP
);
107 *free_buff_ring
= tmp_free_buff_ring
;
108 return tmp_free_buff_ring
;
112 * et131x_rx_dma_memory_alloc
113 * @adapter: pointer to our private adapter structure
115 * Returns 0 on success and errno on failure (as defined in errno.h)
117 * Allocates Free buffer ring 1 for sure, free buffer ring 0 if required,
118 * and the Packet Status Ring.
120 int et131x_rx_dma_memory_alloc(struct et131x_adapter
*adapter
)
124 u32 pktstat_ringsize
, fbr_chunksize
;
125 struct rx_ring
*rx_ring
;
127 /* Setup some convenience pointers */
128 rx_ring
= &adapter
->rx_ring
;
130 /* Alloc memory for the lookup table */
132 rx_ring
->fbr
[0] = kmalloc(sizeof(struct fbr_lookup
), GFP_KERNEL
);
134 rx_ring
->fbr
[1] = kmalloc(sizeof(struct fbr_lookup
), GFP_KERNEL
);
136 /* The first thing we will do is configure the sizes of the buffer
137 * rings. These will change based on jumbo packet support. Larger
138 * jumbo packets increases the size of each entry in FBR0, and the
139 * number of entries in FBR0, while at the same time decreasing the
140 * number of entries in FBR1.
142 * FBR1 holds "large" frames, FBR0 holds "small" frames. If FBR1
143 * entries are huge in order to accommodate a "jumbo" frame, then it
144 * will have less entries. Conversely, FBR1 will now be relied upon
145 * to carry more "normal" frames, thus it's entry size also increases
146 * and the number of entries goes up too (since it now carries
147 * "small" + "regular" packets.
149 * In this scheme, we try to maintain 512 entries between the two
150 * rings. Also, FBR1 remains a constant size - when it's size doubles
151 * the number of entries halves. FBR0 increases in size, however.
154 if (adapter
->registry_jumbo_packet
< 2048) {
156 rx_ring
->fbr0_buffsize
= 256;
157 rx_ring
->fbr0_num_entries
= 512;
159 rx_ring
->fbr1_buffsize
= 2048;
160 rx_ring
->fbr1_num_entries
= 512;
161 } else if (adapter
->registry_jumbo_packet
< 4096) {
163 rx_ring
->fbr0_buffsize
= 512;
164 rx_ring
->fbr0_num_entries
= 1024;
166 rx_ring
->fbr1_buffsize
= 4096;
167 rx_ring
->fbr1_num_entries
= 512;
170 rx_ring
->fbr0_buffsize
= 1024;
171 rx_ring
->fbr0_num_entries
= 768;
173 rx_ring
->fbr1_buffsize
= 16384;
174 rx_ring
->fbr1_num_entries
= 128;
178 adapter
->rx_ring
.psr_num_entries
= adapter
->rx_ring
.fbr0_num_entries
+
179 adapter
->rx_ring
.fbr1_num_entries
;
181 adapter
->rx_ring
.psr_num_entries
= adapter
->rx_ring
.fbr1_num_entries
;
184 /* Allocate an area of memory for Free Buffer Ring 1 */
185 bufsize
= (sizeof(struct fbr_desc
) * rx_ring
->fbr1_num_entries
) + 0xfff;
186 rx_ring
->fbr1_ring_virtaddr
= pci_alloc_consistent(adapter
->pdev
,
188 &rx_ring
->fbr1_ring_physaddr
);
189 if (!rx_ring
->fbr1_ring_virtaddr
) {
190 dev_err(&adapter
->pdev
->dev
,
191 "Cannot alloc memory for Free Buffer Ring 1\n");
195 /* Save physical address
197 * NOTE: pci_alloc_consistent(), used above to alloc DMA regions,
198 * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
199 * are ever returned, make sure the high part is retrieved here
200 * before storing the adjusted address.
202 rx_ring
->fbr1_real_physaddr
= rx_ring
->fbr1_ring_physaddr
;
204 /* Align Free Buffer Ring 1 on a 4K boundary */
205 et131x_align_allocated_memory(adapter
,
206 &rx_ring
->fbr1_real_physaddr
,
207 &rx_ring
->fbr1_offset
, 0x0FFF);
209 rx_ring
->fbr1_ring_virtaddr
=
210 (void *)((u8
*) rx_ring
->fbr1_ring_virtaddr
+
211 rx_ring
->fbr1_offset
);
214 /* Allocate an area of memory for Free Buffer Ring 0 */
215 bufsize
= (sizeof(struct fbr_desc
) * rx_ring
->fbr0_num_entries
) + 0xfff;
216 rx_ring
->fbr0_ring_virtaddr
= pci_alloc_consistent(adapter
->pdev
,
218 &rx_ring
->fbr0_ring_physaddr
);
219 if (!rx_ring
->fbr0_ring_virtaddr
) {
220 dev_err(&adapter
->pdev
->dev
,
221 "Cannot alloc memory for Free Buffer Ring 0\n");
225 /* Save physical address
227 * NOTE: pci_alloc_consistent(), used above to alloc DMA regions,
228 * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
229 * are ever returned, make sure the high part is retrieved here before
230 * storing the adjusted address.
232 rx_ring
->fbr0_real_physaddr
= rx_ring
->fbr0_ring_physaddr
;
234 /* Align Free Buffer Ring 0 on a 4K boundary */
235 et131x_align_allocated_memory(adapter
,
236 &rx_ring
->fbr0_real_physaddr
,
237 &rx_ring
->fbr0_offset
, 0x0FFF);
239 rx_ring
->fbr0_ring_virtaddr
=
240 (void *)((u8
*) rx_ring
->fbr0_ring_virtaddr
+
241 rx_ring
->fbr0_offset
);
243 for (i
= 0; i
< (rx_ring
->fbr1_num_entries
/ FBR_CHUNKS
); i
++) {
245 u64 fbr1_tmp_physaddr
;
248 /* This code allocates an area of memory big enough for N
249 * free buffers + (buffer_size - 1) so that the buffers can
250 * be aligned on 4k boundaries. If each buffer were aligned
251 * to a buffer_size boundary, the effect would be to double
252 * the size of FBR0. By allocating N buffers at once, we
253 * reduce this overhead.
255 if (rx_ring
->fbr1_buffsize
> 4096)
258 fbr1_align
= rx_ring
->fbr1_buffsize
;
261 (FBR_CHUNKS
* rx_ring
->fbr1_buffsize
) + fbr1_align
- 1;
262 rx_ring
->fbr1_mem_virtaddrs
[i
] =
263 pci_alloc_consistent(adapter
->pdev
, fbr_chunksize
,
264 &rx_ring
->fbr1_mem_physaddrs
[i
]);
266 if (!rx_ring
->fbr1_mem_virtaddrs
[i
]) {
267 dev_err(&adapter
->pdev
->dev
,
268 "Could not alloc memory\n");
272 /* See NOTE in "Save Physical Address" comment above */
273 fbr1_tmp_physaddr
= rx_ring
->fbr1_mem_physaddrs
[i
];
275 et131x_align_allocated_memory(adapter
,
277 &fbr1_offset
, (fbr1_align
- 1));
279 for (j
= 0; j
< FBR_CHUNKS
; j
++) {
280 u32 index
= (i
* FBR_CHUNKS
) + j
;
282 /* Save the Virtual address of this index for quick
285 rx_ring
->fbr
[1]->virt
[index
] =
286 (u8
*) rx_ring
->fbr1_mem_virtaddrs
[i
] +
287 (j
* rx_ring
->fbr1_buffsize
) + fbr1_offset
;
289 /* now store the physical address in the descriptor
290 * so the device can access it
292 rx_ring
->fbr
[1]->bus_high
[index
] =
293 (u32
) (fbr1_tmp_physaddr
>> 32);
294 rx_ring
->fbr
[1]->bus_low
[index
] =
295 (u32
) fbr1_tmp_physaddr
;
297 fbr1_tmp_physaddr
+= rx_ring
->fbr1_buffsize
;
299 rx_ring
->fbr
[1]->buffer1
[index
] =
300 rx_ring
->fbr
[1]->virt
[index
];
301 rx_ring
->fbr
[1]->buffer2
[index
] =
302 rx_ring
->fbr
[1]->virt
[index
] - 4;
307 /* Same for FBR0 (if in use) */
308 for (i
= 0; i
< (rx_ring
->fbr0_num_entries
/ FBR_CHUNKS
); i
++) {
310 u64 fbr0_tmp_physaddr
;
313 ((FBR_CHUNKS
+ 1) * rx_ring
->fbr0_buffsize
) - 1;
314 rx_ring
->fbr0_mem_virtaddrs
[i
] =
315 pci_alloc_consistent(adapter
->pdev
, fbr_chunksize
,
316 &rx_ring
->fbr0_mem_physaddrs
[i
]);
318 if (!rx_ring
->fbr0_mem_virtaddrs
[i
]) {
319 dev_err(&adapter
->pdev
->dev
,
320 "Could not alloc memory\n");
324 /* See NOTE in "Save Physical Address" comment above */
325 fbr0_tmp_physaddr
= rx_ring
->fbr0_mem_physaddrs
[i
];
327 et131x_align_allocated_memory(adapter
,
330 rx_ring
->fbr0_buffsize
- 1);
332 for (j
= 0; j
< FBR_CHUNKS
; j
++) {
333 u32 index
= (i
* FBR_CHUNKS
) + j
;
335 rx_ring
->fbr
[0]->virt
[index
] =
336 (u8
*) rx_ring
->fbr0_mem_virtaddrs
[i
] +
337 (j
* rx_ring
->fbr0_buffsize
) + fbr0_offset
;
339 rx_ring
->fbr
[0]->bus_high
[index
] =
340 (u32
) (fbr0_tmp_physaddr
>> 32);
341 rx_ring
->fbr
[0]->bus_low
[index
] =
342 (u32
) fbr0_tmp_physaddr
;
344 fbr0_tmp_physaddr
+= rx_ring
->fbr0_buffsize
;
346 rx_ring
->fbr
[0]->buffer1
[index
] =
347 rx_ring
->fbr
[0]->virt
[index
];
348 rx_ring
->fbr
[0]->buffer2
[index
] =
349 rx_ring
->fbr
[0]->virt
[index
] - 4;
354 /* Allocate an area of memory for FIFO of Packet Status ring entries */
356 sizeof(struct pkt_stat_desc
) * adapter
->rx_ring
.psr_num_entries
;
358 rx_ring
->ps_ring_virtaddr
= pci_alloc_consistent(adapter
->pdev
,
360 &rx_ring
->ps_ring_physaddr
);
362 if (!rx_ring
->ps_ring_virtaddr
) {
363 dev_err(&adapter
->pdev
->dev
,
364 "Cannot alloc memory for Packet Status Ring\n");
367 printk(KERN_INFO
"Packet Status Ring %lx\n",
368 (unsigned long) rx_ring
->ps_ring_physaddr
);
371 * NOTE : pci_alloc_consistent(), used above to alloc DMA regions,
372 * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
373 * are ever returned, make sure the high part is retrieved here before
374 * storing the adjusted address.
377 /* Allocate an area of memory for writeback of status information */
378 rx_ring
->rx_status_block
= pci_alloc_consistent(adapter
->pdev
,
379 sizeof(struct rx_status_block
),
380 &rx_ring
->rx_status_bus
);
381 if (!rx_ring
->rx_status_block
) {
382 dev_err(&adapter
->pdev
->dev
,
383 "Cannot alloc memory for Status Block\n");
386 rx_ring
->num_rfd
= NIC_DEFAULT_NUM_RFD
;
387 printk(KERN_INFO
"PRS %lx\n", (unsigned long)rx_ring
->rx_status_bus
);
390 * pci_pool_create initializes a lookaside list. After successful
391 * creation, nonpaged fixed-size blocks can be allocated from and
392 * freed to the lookaside list.
393 * RFDs will be allocated from this pool.
395 rx_ring
->recv_lookaside
= kmem_cache_create(adapter
->netdev
->name
,
402 adapter
->flags
|= fMP_ADAPTER_RECV_LOOKASIDE
;
404 /* The RFDs are going to be put on lists later on, so initialize the
407 INIT_LIST_HEAD(&rx_ring
->recv_list
);
412 * et131x_rx_dma_memory_free - Free all memory allocated within this module.
413 * @adapter: pointer to our private adapter structure
415 void et131x_rx_dma_memory_free(struct et131x_adapter
*adapter
)
419 u32 pktstat_ringsize
;
421 struct rx_ring
*rx_ring
;
423 /* Setup some convenience pointers */
424 rx_ring
= &adapter
->rx_ring
;
426 /* Free RFDs and associated packet descriptors */
427 WARN_ON(rx_ring
->num_ready_recv
!= rx_ring
->num_rfd
);
429 while (!list_empty(&rx_ring
->recv_list
)) {
430 rfd
= (struct rfd
*) list_entry(rx_ring
->recv_list
.next
,
431 struct rfd
, list_node
);
433 list_del(&rfd
->list_node
);
435 kmem_cache_free(adapter
->rx_ring
.recv_lookaside
, rfd
);
438 /* Free Free Buffer Ring 1 */
439 if (rx_ring
->fbr1_ring_virtaddr
) {
440 /* First the packet memory */
441 for (index
= 0; index
<
442 (rx_ring
->fbr1_num_entries
/ FBR_CHUNKS
); index
++) {
443 if (rx_ring
->fbr1_mem_virtaddrs
[index
]) {
446 if (rx_ring
->fbr1_buffsize
> 4096)
449 fbr1_align
= rx_ring
->fbr1_buffsize
;
452 (rx_ring
->fbr1_buffsize
* FBR_CHUNKS
) +
455 pci_free_consistent(adapter
->pdev
,
457 rx_ring
->fbr1_mem_virtaddrs
[index
],
458 rx_ring
->fbr1_mem_physaddrs
[index
]);
460 rx_ring
->fbr1_mem_virtaddrs
[index
] = NULL
;
464 /* Now the FIFO itself */
465 rx_ring
->fbr1_ring_virtaddr
= (void *)((u8
*)
466 rx_ring
->fbr1_ring_virtaddr
- rx_ring
->fbr1_offset
);
468 bufsize
= (sizeof(struct fbr_desc
) * rx_ring
->fbr1_num_entries
)
471 pci_free_consistent(adapter
->pdev
, bufsize
,
472 rx_ring
->fbr1_ring_virtaddr
,
473 rx_ring
->fbr1_ring_physaddr
);
475 rx_ring
->fbr1_ring_virtaddr
= NULL
;
479 /* Now the same for Free Buffer Ring 0 */
480 if (rx_ring
->fbr0_ring_virtaddr
) {
481 /* First the packet memory */
482 for (index
= 0; index
<
483 (rx_ring
->fbr0_num_entries
/ FBR_CHUNKS
); index
++) {
484 if (rx_ring
->fbr0_mem_virtaddrs
[index
]) {
486 (rx_ring
->fbr0_buffsize
*
487 (FBR_CHUNKS
+ 1)) - 1;
489 pci_free_consistent(adapter
->pdev
,
491 rx_ring
->fbr0_mem_virtaddrs
[index
],
492 rx_ring
->fbr0_mem_physaddrs
[index
]);
494 rx_ring
->fbr0_mem_virtaddrs
[index
] = NULL
;
498 /* Now the FIFO itself */
499 rx_ring
->fbr0_ring_virtaddr
= (void *)((u8
*)
500 rx_ring
->fbr0_ring_virtaddr
- rx_ring
->fbr0_offset
);
502 bufsize
= (sizeof(struct fbr_desc
) * rx_ring
->fbr0_num_entries
)
505 pci_free_consistent(adapter
->pdev
,
507 rx_ring
->fbr0_ring_virtaddr
,
508 rx_ring
->fbr0_ring_physaddr
);
510 rx_ring
->fbr0_ring_virtaddr
= NULL
;
514 /* Free Packet Status Ring */
515 if (rx_ring
->ps_ring_virtaddr
) {
517 sizeof(struct pkt_stat_desc
) *
518 adapter
->rx_ring
.psr_num_entries
;
520 pci_free_consistent(adapter
->pdev
, pktstat_ringsize
,
521 rx_ring
->ps_ring_virtaddr
,
522 rx_ring
->ps_ring_physaddr
);
524 rx_ring
->ps_ring_virtaddr
= NULL
;
527 /* Free area of memory for the writeback of status information */
528 if (rx_ring
->rx_status_block
) {
529 pci_free_consistent(adapter
->pdev
,
530 sizeof(struct rx_status_block
),
531 rx_ring
->rx_status_block
, rx_ring
->rx_status_bus
);
532 rx_ring
->rx_status_block
= NULL
;
535 /* Free receive buffer pool */
537 /* Free receive packet pool */
539 /* Destroy the lookaside (RFD) pool */
540 if (adapter
->flags
& fMP_ADAPTER_RECV_LOOKASIDE
) {
541 kmem_cache_destroy(rx_ring
->recv_lookaside
);
542 adapter
->flags
&= ~fMP_ADAPTER_RECV_LOOKASIDE
;
545 /* Free the FBR Lookup Table */
547 kfree(rx_ring
->fbr
[0]);
550 kfree(rx_ring
->fbr
[1]);
553 rx_ring
->num_ready_recv
= 0;
557 * et131x_init_recv - Initialize receive data structures.
558 * @adapter: pointer to our private adapter structure
560 * Returns 0 on success and errno on failure (as defined in errno.h)
562 int et131x_init_recv(struct et131x_adapter
*adapter
)
564 int status
= -ENOMEM
;
565 struct rfd
*rfd
= NULL
;
568 struct rx_ring
*rx_ring
;
570 /* Setup some convenience pointers */
571 rx_ring
= &adapter
->rx_ring
;
574 for (rfdct
= 0; rfdct
< rx_ring
->num_rfd
; rfdct
++) {
575 rfd
= kmem_cache_alloc(rx_ring
->recv_lookaside
,
576 GFP_ATOMIC
| GFP_DMA
);
579 dev_err(&adapter
->pdev
->dev
,
580 "Couldn't alloc RFD out of kmem_cache\n");
587 /* Add this RFD to the recv_list */
588 list_add_tail(&rfd
->list_node
, &rx_ring
->recv_list
);
590 /* Increment both the available RFD's, and the total RFD's. */
591 rx_ring
->num_ready_recv
++;
595 if (numrfd
> NIC_MIN_NUM_RFD
)
598 rx_ring
->num_rfd
= numrfd
;
601 kmem_cache_free(rx_ring
->recv_lookaside
, rfd
);
602 dev_err(&adapter
->pdev
->dev
,
603 "Allocation problems in et131x_init_recv\n");
609 * et131x_config_rx_dma_regs - Start of Rx_DMA init sequence
610 * @adapter: pointer to our adapter structure
612 void et131x_config_rx_dma_regs(struct et131x_adapter
*adapter
)
614 struct rxdma_regs __iomem
*rx_dma
= &adapter
->regs
->rxdma
;
615 struct rx_ring
*rx_local
= &adapter
->rx_ring
;
616 struct fbr_desc
*fbr_entry
;
621 /* Halt RXDMA to perform the reconfigure. */
622 et131x_rx_dma_disable(adapter
);
624 /* Load the completion writeback physical address
626 * NOTE : pci_alloc_consistent(), used above to alloc DMA regions,
627 * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
628 * are ever returned, make sure the high part is retrieved here
629 * before storing the adjusted address.
631 writel((u32
) ((u64
)rx_local
->rx_status_bus
>> 32),
632 &rx_dma
->dma_wb_base_hi
);
633 writel((u32
) rx_local
->rx_status_bus
, &rx_dma
->dma_wb_base_lo
);
635 memset(rx_local
->rx_status_block
, 0, sizeof(struct rx_status_block
));
637 /* Set the address and parameters of the packet status ring into the
640 writel((u32
) ((u64
)rx_local
->ps_ring_physaddr
>> 32),
641 &rx_dma
->psr_base_hi
);
642 writel((u32
) rx_local
->ps_ring_physaddr
, &rx_dma
->psr_base_lo
);
643 writel(rx_local
->psr_num_entries
- 1, &rx_dma
->psr_num_des
);
644 writel(0, &rx_dma
->psr_full_offset
);
646 psr_num_des
= readl(&rx_dma
->psr_num_des
) & 0xFFF;
647 writel((psr_num_des
* LO_MARK_PERCENT_FOR_PSR
) / 100,
648 &rx_dma
->psr_min_des
);
650 spin_lock_irqsave(&adapter
->rcv_lock
, flags
);
652 /* These local variables track the PSR in the adapter structure */
653 rx_local
->local_psr_full
= 0;
655 /* Now's the best time to initialize FBR1 contents */
656 fbr_entry
= (struct fbr_desc
*) rx_local
->fbr1_ring_virtaddr
;
657 for (entry
= 0; entry
< rx_local
->fbr1_num_entries
; entry
++) {
658 fbr_entry
->addr_hi
= rx_local
->fbr
[1]->bus_high
[entry
];
659 fbr_entry
->addr_lo
= rx_local
->fbr
[1]->bus_low
[entry
];
660 fbr_entry
->word2
= entry
;
664 /* Set the address and parameters of Free buffer ring 1 (and 0 if
665 * required) into the 1310's registers
667 writel((u32
) (rx_local
->fbr1_real_physaddr
>> 32),
668 &rx_dma
->fbr1_base_hi
);
669 writel((u32
) rx_local
->fbr1_real_physaddr
, &rx_dma
->fbr1_base_lo
);
670 writel(rx_local
->fbr1_num_entries
- 1, &rx_dma
->fbr1_num_des
);
671 writel(ET_DMA10_WRAP
, &rx_dma
->fbr1_full_offset
);
673 /* This variable tracks the free buffer ring 1 full position, so it
674 * has to match the above.
676 rx_local
->local_fbr1_full
= ET_DMA10_WRAP
;
678 ((rx_local
->fbr1_num_entries
* LO_MARK_PERCENT_FOR_RX
) / 100) - 1,
679 &rx_dma
->fbr1_min_des
);
682 /* Now's the best time to initialize FBR0 contents */
683 fbr_entry
= (struct fbr_desc
*) rx_local
->fbr0_ring_virtaddr
;
684 for (entry
= 0; entry
< rx_local
->fbr0_num_entries
; entry
++) {
685 fbr_entry
->addr_hi
= rx_local
->fbr
[0]->bus_high
[entry
];
686 fbr_entry
->addr_lo
= rx_local
->fbr
[0]->bus_low
[entry
];
687 fbr_entry
->word2
= entry
;
691 writel((u32
) (rx_local
->fbr0_real_physaddr
>> 32),
692 &rx_dma
->fbr0_base_hi
);
693 writel((u32
) rx_local
->fbr0_real_physaddr
, &rx_dma
->fbr0_base_lo
);
694 writel(rx_local
->fbr0_num_entries
- 1, &rx_dma
->fbr0_num_des
);
695 writel(ET_DMA10_WRAP
, &rx_dma
->fbr0_full_offset
);
697 /* This variable tracks the free buffer ring 0 full position, so it
698 * has to match the above.
700 rx_local
->local_fbr0_full
= ET_DMA10_WRAP
;
702 ((rx_local
->fbr0_num_entries
* LO_MARK_PERCENT_FOR_RX
) / 100) - 1,
703 &rx_dma
->fbr0_min_des
);
706 /* Program the number of packets we will receive before generating an
708 * For version B silicon, this value gets updated once autoneg is
711 writel(PARM_RX_NUM_BUFS_DEF
, &rx_dma
->num_pkt_done
);
713 /* The "time_done" is not working correctly to coalesce interrupts
714 * after a given time period, but rather is giving us an interrupt
715 * regardless of whether we have received packets.
716 * This value gets updated once autoneg is complete.
718 writel(PARM_RX_TIME_INT_DEF
, &rx_dma
->max_pkt_time
);
720 spin_unlock_irqrestore(&adapter
->rcv_lock
, flags
);
724 * et131x_set_rx_dma_timer - Set the heartbeat timer according to line rate.
725 * @adapter: pointer to our adapter structure
727 void et131x_set_rx_dma_timer(struct et131x_adapter
*adapter
)
729 struct phy_device
*phydev
= adapter
->phydev
;
734 /* For version B silicon, we do not use the RxDMA timer for 10 and 100
735 * Mbits/s line rates. We do not enable and RxDMA interrupt coalescing.
737 if ((phydev
->speed
== SPEED_100
) || (phydev
->speed
== SPEED_10
)) {
738 writel(0, &adapter
->regs
->rxdma
.max_pkt_time
);
739 writel(1, &adapter
->regs
->rxdma
.num_pkt_done
);
744 * NICReturnRFD - Recycle a RFD and put it back onto the receive list
745 * @adapter: pointer to our adapter
746 * @rfd: pointer to the RFD
748 static void nic_return_rfd(struct et131x_adapter
*adapter
, struct rfd
*rfd
)
750 struct rx_ring
*rx_local
= &adapter
->rx_ring
;
751 struct rxdma_regs __iomem
*rx_dma
= &adapter
->regs
->rxdma
;
752 u16 buff_index
= rfd
->bufferindex
;
753 u8 ring_index
= rfd
->ringindex
;
756 /* We don't use any of the OOB data besides status. Otherwise, we
757 * need to clean up OOB data
761 (ring_index
== 0 && buff_index
< rx_local
->fbr0_num_entries
) ||
763 (ring_index
== 1 && buff_index
< rx_local
->fbr1_num_entries
)) {
764 spin_lock_irqsave(&adapter
->fbr_lock
, flags
);
766 if (ring_index
== 1) {
767 struct fbr_desc
*next
=
768 (struct fbr_desc
*) (rx_local
->fbr1_ring_virtaddr
) +
769 INDEX10(rx_local
->local_fbr1_full
);
771 /* Handle the Free Buffer Ring advancement here. Write
772 * the PA / Buffer Index for the returned buffer into
773 * the oldest (next to be freed)FBR entry
775 next
->addr_hi
= rx_local
->fbr
[1]->bus_high
[buff_index
];
776 next
->addr_lo
= rx_local
->fbr
[1]->bus_low
[buff_index
];
777 next
->word2
= buff_index
;
779 writel(bump_free_buff_ring(&rx_local
->local_fbr1_full
,
780 rx_local
->fbr1_num_entries
- 1),
781 &rx_dma
->fbr1_full_offset
);
785 struct fbr_desc
*next
= (struct fbr_desc
*)
786 rx_local
->fbr0_ring_virtaddr
+
787 INDEX10(rx_local
->local_fbr0_full
);
789 /* Handle the Free Buffer Ring advancement here. Write
790 * the PA / Buffer Index for the returned buffer into
791 * the oldest (next to be freed) FBR entry
793 next
->addr_hi
= rx_local
->fbr
[0]->bus_high
[buff_index
];
794 next
->addr_lo
= rx_local
->fbr
[0]->bus_low
[buff_index
];
795 next
->word2
= buff_index
;
797 writel(bump_free_buff_ring(&rx_local
->local_fbr0_full
,
798 rx_local
->fbr0_num_entries
- 1),
799 &rx_dma
->fbr0_full_offset
);
802 spin_unlock_irqrestore(&adapter
->fbr_lock
, flags
);
804 dev_err(&adapter
->pdev
->dev
,
805 "%s illegal Buffer Index returned\n", __func__
);
808 /* The processing on this RFD is done, so put it back on the tail of
811 spin_lock_irqsave(&adapter
->rcv_lock
, flags
);
812 list_add_tail(&rfd
->list_node
, &rx_local
->recv_list
);
813 rx_local
->num_ready_recv
++;
814 spin_unlock_irqrestore(&adapter
->rcv_lock
, flags
);
816 WARN_ON(rx_local
->num_ready_recv
> rx_local
->num_rfd
);
820 * et131x_rx_dma_disable - Stop of Rx_DMA on the ET1310
821 * @adapter: pointer to our adapter structure
823 void et131x_rx_dma_disable(struct et131x_adapter
*adapter
)
826 /* Setup the receive dma configuration register */
827 writel(0x00002001, &adapter
->regs
->rxdma
.csr
);
828 csr
= readl(&adapter
->regs
->rxdma
.csr
);
829 if ((csr
& 0x00020000) == 0) { /* Check halt status (bit 17) */
831 csr
= readl(&adapter
->regs
->rxdma
.csr
);
832 if ((csr
& 0x00020000) == 0)
833 dev_err(&adapter
->pdev
->dev
,
834 "RX Dma failed to enter halt state. CSR 0x%08x\n",
840 * et131x_rx_dma_enable - re-start of Rx_DMA on the ET1310.
841 * @adapter: pointer to our adapter structure
843 void et131x_rx_dma_enable(struct et131x_adapter
*adapter
)
845 /* Setup the receive dma configuration register for normal operation */
846 u32 csr
= 0x2000; /* FBR1 enable */
848 if (adapter
->rx_ring
.fbr1_buffsize
== 4096)
850 else if (adapter
->rx_ring
.fbr1_buffsize
== 8192)
852 else if (adapter
->rx_ring
.fbr1_buffsize
== 16384)
855 csr
|= 0x0400; /* FBR0 enable */
856 if (adapter
->rx_ring
.fbr0_buffsize
== 256)
858 else if (adapter
->rx_ring
.fbr0_buffsize
== 512)
860 else if (adapter
->rx_ring
.fbr0_buffsize
== 1024)
863 writel(csr
, &adapter
->regs
->rxdma
.csr
);
865 csr
= readl(&adapter
->regs
->rxdma
.csr
);
866 if ((csr
& 0x00020000) != 0) {
868 csr
= readl(&adapter
->regs
->rxdma
.csr
);
869 if ((csr
& 0x00020000) != 0) {
870 dev_err(&adapter
->pdev
->dev
,
871 "RX Dma failed to exit halt state. CSR 0x%08x\n",
878 * nic_rx_pkts - Checks the hardware for available packets
879 * @adapter: pointer to our adapter
881 * Returns rfd, a pointer to our MPRFD.
883 * Checks the hardware for available packets, using completion ring
884 * If packets are available, it gets an RFD from the recv_list, attaches
885 * the packet to it, puts the RFD in the RecvPendList, and also returns
886 * the pointer to the RFD.
888 static struct rfd
*nic_rx_pkts(struct et131x_adapter
*adapter
)
890 struct rx_ring
*rx_local
= &adapter
->rx_ring
;
891 struct rx_status_block
*status
;
892 struct pkt_stat_desc
*psr
;
897 struct list_head
*element
;
904 /* RX Status block is written by the DMA engine prior to every
905 * interrupt. It contains the next to be used entry in the Packet
906 * Status Ring, and also the two Free Buffer rings.
908 status
= rx_local
->rx_status_block
;
909 word1
= status
->word1
>> 16; /* Get the useful bits */
911 /* Check the PSR and wrap bits do not match */
912 if ((word1
& 0x1FFF) == (rx_local
->local_psr_full
& 0x1FFF))
913 /* Looks like this ring is not updated yet */
916 /* The packet status ring indicates that data is available. */
917 psr
= (struct pkt_stat_desc
*) (rx_local
->ps_ring_virtaddr
) +
918 (rx_local
->local_psr_full
& 0xFFF);
920 /* Grab any information that is required once the PSR is
921 * advanced, since we can no longer rely on the memory being
924 len
= psr
->word1
& 0xFFFF;
925 ring_index
= (psr
->word1
>> 26) & 0x03;
926 buff_index
= (psr
->word1
>> 16) & 0x3FF;
929 /* Indicate that we have used this PSR entry. */
931 add_12bit(&rx_local
->local_psr_full
, 1);
933 (rx_local
->local_psr_full
& 0xFFF) > rx_local
->psr_num_entries
- 1) {
934 /* Clear psr full and toggle the wrap bit */
935 rx_local
->local_psr_full
&= ~0xFFF;
936 rx_local
->local_psr_full
^= 0x1000;
939 writel(rx_local
->local_psr_full
,
940 &adapter
->regs
->rxdma
.psr_full_offset
);
948 if (ring_index
> 1 ||
950 buff_index
> rx_local
->fbr0_num_entries
- 1) ||
952 buff_index
> rx_local
->fbr1_num_entries
- 1))
954 if (ring_index
!= 1 || buff_index
> rx_local
->fbr1_num_entries
- 1)
957 /* Illegal buffer or ring index cannot be used by S/W*/
958 dev_err(&adapter
->pdev
->dev
,
959 "NICRxPkts PSR Entry %d indicates "
960 "length of %d and/or bad bi(%d)\n",
961 rx_local
->local_psr_full
& 0xFFF,
966 /* Get and fill the RFD. */
967 spin_lock_irqsave(&adapter
->rcv_lock
, flags
);
970 element
= rx_local
->recv_list
.next
;
971 rfd
= (struct rfd
*) list_entry(element
, struct rfd
, list_node
);
974 spin_unlock_irqrestore(&adapter
->rcv_lock
, flags
);
978 list_del(&rfd
->list_node
);
979 rx_local
->num_ready_recv
--;
981 spin_unlock_irqrestore(&adapter
->rcv_lock
, flags
);
983 rfd
->bufferindex
= buff_index
;
984 rfd
->ringindex
= ring_index
;
986 /* In V1 silicon, there is a bug which screws up filtering of
987 * runt packets. Therefore runt packet filtering is disabled
988 * in the MAC and the packets are dropped here. They are
991 if (len
< (NIC_MIN_PACKET_SIZE
+ 4)) {
992 adapter
->stats
.rx_other_errs
++;
997 /* Determine if this is a multicast packet coming in */
998 if ((word0
& ALCATEL_MULTICAST_PKT
) &&
999 !(word0
& ALCATEL_BROADCAST_PKT
)) {
1000 /* Promiscuous mode and Multicast mode are
1001 * not mutually exclusive as was first
1002 * thought. I guess Promiscuous is just
1003 * considered a super-set of the other
1004 * filters. Generally filter is 0x2b when in
1007 if ((adapter
->packet_filter
&
1008 ET131X_PACKET_TYPE_MULTICAST
)
1009 && !(adapter
->packet_filter
&
1010 ET131X_PACKET_TYPE_PROMISCUOUS
)
1011 && !(adapter
->packet_filter
&
1012 ET131X_PACKET_TYPE_ALL_MULTICAST
)) {
1013 buf
= rx_local
->fbr
[ring_index
]->
1016 /* Loop through our list to see if the
1017 * destination address of this packet
1018 * matches one in our list.
1020 for (i
= 0; i
< adapter
->multicast_addr_count
;
1023 adapter
->multicast_list
[i
][0]
1025 adapter
->multicast_list
[i
][1]
1027 adapter
->multicast_list
[i
][2]
1029 adapter
->multicast_list
[i
][3]
1031 adapter
->multicast_list
[i
][4]
1033 adapter
->multicast_list
[i
][5]) {
1038 /* If our index is equal to the number
1039 * of Multicast address we have, then
1040 * this means we did not find this
1041 * packet's matching address in our
1042 * list. Set the len to zero,
1043 * so we free our RFD when we return
1044 * from this function.
1046 if (i
== adapter
->multicast_addr_count
)
1051 adapter
->stats
.multicast_pkts_rcvd
++;
1052 } else if (word0
& ALCATEL_BROADCAST_PKT
)
1053 adapter
->stats
.broadcast_pkts_rcvd
++;
1055 /* Not sure what this counter measures in
1056 * promiscuous mode. Perhaps we should check
1057 * the MAC address to see if it is directed
1058 * to us in promiscuous mode.
1060 adapter
->stats
.unicast_pkts_rcvd
++;
1064 struct sk_buff
*skb
= NULL
;
1066 /*rfd->len = len - 4; */
1069 skb
= dev_alloc_skb(rfd
->len
+ 2);
1071 dev_err(&adapter
->pdev
->dev
,
1072 "Couldn't alloc an SKB for Rx\n");
1076 adapter
->net_stats
.rx_bytes
+= rfd
->len
;
1078 memcpy(skb_put(skb
, rfd
->len
),
1079 rx_local
->fbr
[ring_index
]->virt
[buff_index
],
1082 skb
->dev
= adapter
->netdev
;
1083 skb
->protocol
= eth_type_trans(skb
, adapter
->netdev
);
1084 skb
->ip_summed
= CHECKSUM_NONE
;
1091 nic_return_rfd(adapter
, rfd
);
1096 * et131x_reset_recv - Reset the receive list
1097 * @adapter: pointer to our adapter
1099 * Assumption, Rcv spinlock has been acquired.
1101 void et131x_reset_recv(struct et131x_adapter
*adapter
)
1103 WARN_ON(list_empty(&adapter
->rx_ring
.recv_list
));
1107 * et131x_handle_recv_interrupt - Interrupt handler for receive processing
1108 * @adapter: pointer to our adapter
1110 * Assumption, Rcv spinlock has been acquired.
1112 void et131x_handle_recv_interrupt(struct et131x_adapter
*adapter
)
1114 struct rfd
*rfd
= NULL
;
1118 /* Process up to available RFD's */
1119 while (count
< NUM_PACKETS_HANDLED
) {
1120 if (list_empty(&adapter
->rx_ring
.recv_list
)) {
1121 WARN_ON(adapter
->rx_ring
.num_ready_recv
!= 0);
1126 rfd
= nic_rx_pkts(adapter
);
1131 /* Do not receive any packets until a filter has been set.
1132 * Do not receive any packets until we have link.
1133 * If length is zero, return the RFD in order to advance the
1136 if (!adapter
->packet_filter
||
1137 !netif_carrier_ok(adapter
->netdev
) ||
1141 /* Increment the number of packets we received */
1142 adapter
->net_stats
.rx_packets
++;
1144 /* Set the status on the packet, either resources or success */
1145 if (adapter
->rx_ring
.num_ready_recv
< RFD_LOW_WATER_MARK
) {
1146 dev_warn(&adapter
->pdev
->dev
,
1147 "RFD's are running out\n");
1152 if (count
== NUM_PACKETS_HANDLED
|| !done
) {
1153 adapter
->rx_ring
.unfinished_receives
= true;
1154 writel(PARM_TX_TIME_INT_DEF
* NANO_IN_A_MICRO
,
1155 &adapter
->regs
->global
.watchdog_timer
);
1157 /* Watchdog timer will disable itself if appropriate. */
1158 adapter
->rx_ring
.unfinished_receives
= false;