1 /**********************************************************************
2 * Author: Cavium Networks
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
7 * Copyright (c) 2003-2007 Cavium Networks
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this file; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * or visit http://www.gnu.org/licenses/.
24 * This file may also be available under a different license from Cavium.
25 * Contact Cavium Networks for more information
26 **********************************************************************/
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/cache.h>
30 #include <linux/netdevice.h>
31 #include <linux/init.h>
32 #include <linux/etherdevice.h>
34 #include <linux/string.h>
35 #include <linux/prefetch.h>
36 #include <linux/ethtool.h>
37 #include <linux/mii.h>
38 #include <linux/seq_file.h>
39 #include <linux/proc_fs.h>
42 #include <linux/xfrm.h>
44 #endif /* CONFIG_XFRM */
46 #include <asm/atomic.h>
48 #include <asm/octeon/octeon.h>
50 #include "ethernet-defines.h"
51 #include "octeon-ethernet.h"
52 #include "ethernet-mem.h"
53 #include "ethernet-util.h"
55 #include "cvmx-helper.h"
60 #include "cvmx-scratch.h"
62 #include "cvmx-gmxx-defs.h"
64 struct cvm_tasklet_wrapper
{
65 struct tasklet_struct t
;
69 * Aligning the tasklet_struct on cachline boundries seems to decrease
70 * throughput even though in theory it would reduce contantion on the
71 * cache lines containing the locks.
74 static struct cvm_tasklet_wrapper cvm_oct_tasklet
[NR_CPUS
];
77 * Interrupt handler. The interrupt occurs whenever the POW
78 * transitions from 0->1 packets in our group.
85 irqreturn_t
cvm_oct_do_interrupt(int cpl
, void *dev_id
)
87 /* Acknowledge the interrupt */
89 cvmx_write_csr(CVMX_POW_WQ_INT
, 1 << pow_receive_group
);
91 cvmx_write_csr(CVMX_POW_WQ_INT
, 0x10001 << pow_receive_group
);
93 tasklet_schedule(&cvm_oct_tasklet
[smp_processor_id()].t
);
98 #ifdef CONFIG_NET_POLL_CONTROLLER
100 * This is called when the kernel needs to manually poll the
101 * device. For Octeon, this is simply calling the interrupt
102 * handler. We actually poll all the devices, not just the
105 * @dev: Device to poll. Unused
107 void cvm_oct_poll_controller(struct net_device
*dev
)
110 tasklet_schedule(&cvm_oct_tasklet
[smp_processor_id()].t
);
116 * This is called on receive errors, and determines if the packet
117 * can be dropped early-on in cvm_oct_tasklet_rx().
119 * @work: Work queue entry pointing to the packet.
120 * Returns Non-zero if the packet can be dropped, zero otherwise.
122 static inline int cvm_oct_check_rcv_error(cvmx_wqe_t
*work
)
124 if ((work
->word2
.snoip
.err_code
== 10) && (work
->len
<= 64)) {
126 * Ignore length errors on min size packets. Some
127 * equipment incorrectly pads packets to 64+4FCS
128 * instead of 60+4FCS. Note these packets still get
129 * counted as frame errors.
132 if (USE_10MBPS_PREAMBLE_WORKAROUND
133 && ((work
->word2
.snoip
.err_code
== 5)
134 || (work
->word2
.snoip
.err_code
== 7))) {
137 * We received a packet with either an alignment error
138 * or a FCS error. This may be signalling that we are
139 * running 10Mbps with GMXX_RXX_FRM_CTL[PRE_CHK}
140 * off. If this is the case we need to parse the
141 * packet to determine if we can remove a non spec
142 * preamble and generate a correct packet.
144 int interface
= cvmx_helper_get_interface_num(work
->ipprt
);
145 int index
= cvmx_helper_get_interface_index_num(work
->ipprt
);
146 union cvmx_gmxx_rxx_frm_ctl gmxx_rxx_frm_ctl
;
147 gmxx_rxx_frm_ctl
.u64
=
148 cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL(index
, interface
));
149 if (gmxx_rxx_frm_ctl
.s
.pre_chk
== 0) {
152 cvmx_phys_to_ptr(work
->packet_ptr
.s
.addr
);
155 while (i
< work
->len
- 1) {
164 DEBUGPRINT("Port %d received 0xd5 preamble\n", work->ipprt);
166 work
->packet_ptr
.s
.addr
+= i
+ 1;
168 } else if ((*ptr
& 0xf) == 0xd) {
170 DEBUGPRINT("Port %d received 0x?d preamble\n", work->ipprt);
172 work
->packet_ptr
.s
.addr
+= i
;
174 for (i
= 0; i
< work
->len
; i
++) {
176 ((*ptr
& 0xf0) >> 4) |
177 ((*(ptr
+ 1) & 0xf) << 4);
181 DEBUGPRINT("Port %d unknown preamble, packet "
185 cvmx_helper_dump_packet(work);
187 cvm_oct_free_work(work
);
192 DEBUGPRINT("Port %d receive error code %d, packet dropped\n",
193 work
->ipprt
, work
->word2
.snoip
.err_code
);
194 cvm_oct_free_work(work
);
202 * Tasklet function that is scheduled on a core when an interrupt occurs.
206 void cvm_oct_tasklet_rx(unsigned long unused
)
208 const int coreid
= cvmx_get_core_num();
209 uint64_t old_group_mask
;
210 uint64_t old_scratch
;
214 int packet_not_copied
;
216 /* Prefetch cvm_oct_device since we know we need it soon */
217 prefetch(cvm_oct_device
);
219 if (USE_ASYNC_IOBDMA
) {
220 /* Save scratch in case userspace is using it */
222 old_scratch
= cvmx_scratch_read64(CVMX_SCR_SCRATCH
);
225 /* Only allow work for our group (and preserve priorities) */
226 old_group_mask
= cvmx_read_csr(CVMX_POW_PP_GRP_MSKX(coreid
));
227 cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid
),
228 (old_group_mask
& ~0xFFFFull
) | 1 << pow_receive_group
);
230 if (USE_ASYNC_IOBDMA
)
231 cvmx_pow_work_request_async(CVMX_SCR_SCRATCH
, CVMX_POW_NO_WAIT
);
234 struct sk_buff
*skb
= NULL
;
238 if (USE_ASYNC_IOBDMA
) {
239 work
= cvmx_pow_work_response_async(CVMX_SCR_SCRATCH
);
241 if ((INTERRUPT_LIMIT
== 0)
242 || likely(rx_count
< MAX_RX_PACKETS
))
244 cvmx_pow_work_request_sync
254 * Limit each core to processing MAX_RX_PACKETS
255 * packets without a break. This way the RX can't
256 * starve the TX task.
258 if (USE_ASYNC_IOBDMA
) {
260 if ((INTERRUPT_LIMIT
== 0)
261 || likely(rx_count
< MAX_RX_PACKETS
))
262 cvmx_pow_work_request_async_nocheck
263 (CVMX_SCR_SCRATCH
, CVMX_POW_NO_WAIT
);
265 cvmx_scratch_write64(CVMX_SCR_SCRATCH
,
266 0x8000000000000000ull
);
267 cvmx_pow_tag_sw_null_nocheck();
271 skb_in_hw
= USE_SKBUFFS_IN_HW
&& work
->word2
.s
.bufs
== 1;
272 if (likely(skb_in_hw
)) {
275 **)(cvm_oct_get_buffer_ptr(work
->packet_ptr
) -
277 prefetch(&skb
->head
);
280 prefetch(cvm_oct_device
[work
->ipprt
]);
283 /* Immediately throw away all packets with receive errors */
284 if (unlikely(work
->word2
.snoip
.rcv_error
)) {
285 if (cvm_oct_check_rcv_error(work
))
290 * We can only use the zero copy path if skbuffs are
291 * in the FPA pool and the packet fits in a single
294 if (likely(skb_in_hw
)) {
296 * This calculation was changed in case the
297 * skb header is using a different address
298 * aliasing type than the buffer. It doesn't
299 * make any differnece now, but the new one is
303 skb
->head
+ work
->packet_ptr
.s
.addr
-
304 cvmx_ptr_to_phys(skb
->head
);
306 skb
->len
= work
->len
;
307 skb_set_tail_pointer(skb
, skb
->len
);
308 packet_not_copied
= 1;
312 * We have to copy the packet. First allocate
315 skb
= dev_alloc_skb(work
->len
);
317 DEBUGPRINT("Port %d failed to allocate "
318 "skbuff, packet dropped\n",
320 cvm_oct_free_work(work
);
325 * Check if we've received a packet that was
326 * entirely stored in the work entry. This is
329 if (unlikely(work
->word2
.s
.bufs
== 0)) {
330 uint8_t *ptr
= work
->packet_data
;
332 if (likely(!work
->word2
.s
.not_IP
)) {
334 * The beginning of the packet
335 * moves for IP packets.
337 if (work
->word2
.s
.is_v6
)
342 memcpy(skb_put(skb
, work
->len
), ptr
, work
->len
);
343 /* No packet buffers to free */
345 int segments
= work
->word2
.s
.bufs
;
346 union cvmx_buf_ptr segment_ptr
=
351 union cvmx_buf_ptr next_ptr
=
352 *(union cvmx_buf_ptr
*)
353 cvmx_phys_to_ptr(segment_ptr
.s
.
356 * Octeon Errata PKI-100: The segment size is
357 * wrong. Until it is fixed, calculate the
358 * segment size based on the packet pool
359 * buffer size. When it is fixed, the
360 * following line should be replaced with this
361 * one: int segment_size =
362 * segment_ptr.s.size;
365 CVMX_FPA_PACKET_POOL_SIZE
-
366 (segment_ptr
.s
.addr
-
367 (((segment_ptr
.s
.addr
>> 7) -
368 segment_ptr
.s
.back
) << 7));
369 /* Don't copy more than what is left
371 if (segment_size
> len
)
373 /* Copy the data into the packet */
374 memcpy(skb_put(skb
, segment_size
),
375 cvmx_phys_to_ptr(segment_ptr
.s
.
378 /* Reduce the amount of bytes left
381 segment_ptr
= next_ptr
;
384 packet_not_copied
= 0;
387 if (likely((work
->ipprt
< TOTAL_NUMBER_OF_PORTS
) &&
388 cvm_oct_device
[work
->ipprt
])) {
389 struct net_device
*dev
= cvm_oct_device
[work
->ipprt
];
390 struct octeon_ethernet
*priv
= netdev_priv(dev
);
392 /* Only accept packets for devices
393 that are currently up */
394 if (likely(dev
->flags
& IFF_UP
)) {
395 skb
->protocol
= eth_type_trans(skb
, dev
);
399 (work
->word2
.s
.not_IP
400 || work
->word2
.s
.IP_exc
401 || work
->word2
.s
.L4_error
))
402 skb
->ip_summed
= CHECKSUM_NONE
;
404 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
406 /* Increment RX stats for virtual ports */
407 if (work
->ipprt
>= CVMX_PIP_NUM_INPUT_PORTS
) {
409 atomic64_add(1, (atomic64_t
*)&priv
->stats
.rx_packets
);
410 atomic64_add(skb
->len
, (atomic64_t
*)&priv
->stats
.rx_bytes
);
412 atomic_add(1, (atomic_t
*)&priv
->stats
.rx_packets
);
413 atomic_add(skb
->len
, (atomic_t
*)&priv
->stats
.rx_bytes
);
416 netif_receive_skb(skb
);
419 * Drop any packet received for a
420 * device that isn't up.
423 DEBUGPRINT("%s: Device not up, packet dropped\n",
427 atomic64_add(1, (atomic64_t
*)&priv
->stats
.rx_dropped
);
429 atomic_add(1, (atomic_t
*)&priv
->stats
.rx_dropped
);
431 dev_kfree_skb_irq(skb
);
435 * Drop any packet received for a device that
438 DEBUGPRINT("Port %d not controlled by Linux, packet "
441 dev_kfree_skb_irq(skb
);
444 * Check to see if the skbuff and work share the same
447 if (USE_SKBUFFS_IN_HW
&& likely(packet_not_copied
)) {
449 * This buffer needs to be replaced, increment
450 * the number of buffers we need to free by
453 cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE
,
456 cvmx_fpa_free(work
, CVMX_FPA_WQE_POOL
,
459 cvm_oct_free_work(work
);
463 /* Restore the original POW group mask */
464 cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid
), old_group_mask
);
465 if (USE_ASYNC_IOBDMA
) {
466 /* Restore the scratch area */
467 cvmx_scratch_write64(CVMX_SCR_SCRATCH
, old_scratch
);
470 if (USE_SKBUFFS_IN_HW
) {
471 /* Refill the packet buffer pool */
473 cvmx_fau_fetch_and_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE
, 0);
475 if (number_to_free
> 0) {
476 cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE
,
479 cvm_oct_mem_fill_fpa(CVMX_FPA_PACKET_POOL
,
480 CVMX_FPA_PACKET_POOL_SIZE
,
482 if (num_freed
!= number_to_free
) {
483 cvmx_fau_atomic_add32
484 (FAU_NUM_PACKET_BUFFERS_TO_FREE
,
485 number_to_free
- num_freed
);
491 void cvm_oct_rx_initialize(void)
494 /* Initialize all of the tasklets */
495 for (i
= 0; i
< NR_CPUS
; i
++)
496 tasklet_init(&cvm_oct_tasklet
[i
].t
, cvm_oct_tasklet_rx
, 0);
499 void cvm_oct_rx_shutdown(void)
502 /* Shutdown all of the tasklets */
503 for (i
= 0; i
< NR_CPUS
; i
++)
504 tasklet_kill(&cvm_oct_tasklet
[i
].t
);