2 * This file is based on code from OCTEON SDK by Cavium Networks.
4 * Copyright (c) 2003-2010 Cavium Networks
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, Version 2, as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/cache.h>
14 #include <linux/cpumask.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
18 #include <linux/string.h>
19 #include <linux/prefetch.h>
20 #include <linux/ratelimit.h>
21 #include <linux/smp.h>
22 #include <linux/interrupt.h>
25 #include <linux/xfrm.h>
27 #endif /* CONFIG_XFRM */
29 #include <linux/atomic.h>
31 #include <asm/octeon/octeon.h>
33 #include "ethernet-defines.h"
34 #include "ethernet-mem.h"
35 #include "ethernet-rx.h"
36 #include "octeon-ethernet.h"
37 #include "ethernet-util.h"
39 #include <asm/octeon/cvmx-helper.h>
40 #include <asm/octeon/cvmx-wqe.h>
41 #include <asm/octeon/cvmx-fau.h>
42 #include <asm/octeon/cvmx-pow.h>
43 #include <asm/octeon/cvmx-pip.h>
44 #include <asm/octeon/cvmx-scratch.h>
46 #include <asm/octeon/cvmx-gmxx-defs.h>
48 static struct napi_struct cvm_oct_napi
;
51 * cvm_oct_do_interrupt - interrupt handler.
52 * @cpl: Interrupt number. Unused
53 * @dev_id: Cookie to identify the device. Unused
55 * The interrupt occurs whenever the POW has packets in our group.
58 static irqreturn_t
cvm_oct_do_interrupt(int cpl
, void *dev_id
)
60 /* Disable the IRQ and start napi_poll. */
61 disable_irq_nosync(OCTEON_IRQ_WORKQ0
+ pow_receive_group
);
62 napi_schedule(&cvm_oct_napi
);
68 * cvm_oct_check_rcv_error - process receive errors
69 * @work: Work queue entry pointing to the packet.
71 * Returns Non-zero if the packet can be dropped, zero otherwise.
73 static inline int cvm_oct_check_rcv_error(cvmx_wqe_t
*work
)
77 if (octeon_has_feature(OCTEON_FEATURE_PKND
))
78 port
= work
->word0
.pip
.cn68xx
.pknd
;
80 port
= work
->word1
.cn38xx
.ipprt
;
82 if ((work
->word2
.snoip
.err_code
== 10) && (work
->word1
.len
<= 64)) {
84 * Ignore length errors on min size packets. Some
85 * equipment incorrectly pads packets to 64+4FCS
86 * instead of 60+4FCS. Note these packets still get
87 * counted as frame errors.
89 } else if (work
->word2
.snoip
.err_code
== 5 ||
90 work
->word2
.snoip
.err_code
== 7) {
92 * We received a packet with either an alignment error
93 * or a FCS error. This may be signalling that we are
94 * running 10Mbps with GMXX_RXX_FRM_CTL[PRE_CHK]
95 * off. If this is the case we need to parse the
96 * packet to determine if we can remove a non spec
97 * preamble and generate a correct packet.
99 int interface
= cvmx_helper_get_interface_num(port
);
100 int index
= cvmx_helper_get_interface_index_num(port
);
101 union cvmx_gmxx_rxx_frm_ctl gmxx_rxx_frm_ctl
;
103 gmxx_rxx_frm_ctl
.u64
=
104 cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL(index
, interface
));
105 if (gmxx_rxx_frm_ctl
.s
.pre_chk
== 0) {
108 cvmx_phys_to_ptr(work
->packet_ptr
.s
.addr
);
111 while (i
< work
->word1
.len
- 1) {
120 printk_ratelimited("Port %d received 0xd5 preamble\n",
123 work
->packet_ptr
.s
.addr
+= i
+ 1;
124 work
->word1
.len
-= i
+ 5;
125 } else if ((*ptr
& 0xf) == 0xd) {
127 printk_ratelimited("Port %d received 0x?d preamble\n",
130 work
->packet_ptr
.s
.addr
+= i
;
131 work
->word1
.len
-= i
+ 4;
132 for (i
= 0; i
< work
->word1
.len
; i
++) {
134 ((*ptr
& 0xf0) >> 4) |
135 ((*(ptr
+ 1) & 0xf) << 4);
139 printk_ratelimited("Port %d unknown preamble, packet dropped\n",
142 cvmx_helper_dump_packet(work);
144 cvm_oct_free_work(work
);
149 printk_ratelimited("Port %d receive error code %d, packet dropped\n",
150 port
, work
->word2
.snoip
.err_code
);
151 cvm_oct_free_work(work
);
159 * cvm_oct_napi_poll - the NAPI poll function.
160 * @napi: The NAPI instance, or null if called from cvm_oct_poll_controller
161 * @budget: Maximum number of packets to receive.
163 * Returns the number of packets processed.
165 static int cvm_oct_napi_poll(struct napi_struct
*napi
, int budget
)
167 const int coreid
= cvmx_get_core_num();
171 int did_work_request
= 0;
172 int packet_not_copied
;
174 /* Prefetch cvm_oct_device since we know we need it soon */
175 prefetch(cvm_oct_device
);
177 if (USE_ASYNC_IOBDMA
) {
178 /* Save scratch in case userspace is using it */
180 old_scratch
= cvmx_scratch_read64(CVMX_SCR_SCRATCH
);
183 /* Only allow work for our group (and preserve priorities) */
184 if (OCTEON_IS_MODEL(OCTEON_CN68XX
)) {
185 old_group_mask
= cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid
));
186 cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid
),
187 1ull << pow_receive_group
);
188 cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid
)); /* Flush */
190 old_group_mask
= cvmx_read_csr(CVMX_POW_PP_GRP_MSKX(coreid
));
191 cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid
),
192 (old_group_mask
& ~0xFFFFull
) | 1 << pow_receive_group
);
195 if (USE_ASYNC_IOBDMA
) {
196 cvmx_pow_work_request_async(CVMX_SCR_SCRATCH
, CVMX_POW_NO_WAIT
);
197 did_work_request
= 1;
200 while (rx_count
< budget
) {
201 struct sk_buff
*skb
= NULL
;
202 struct sk_buff
**pskb
= NULL
;
207 if (USE_ASYNC_IOBDMA
&& did_work_request
)
208 work
= cvmx_pow_work_response_async(CVMX_SCR_SCRATCH
);
210 work
= cvmx_pow_work_request_sync(CVMX_POW_NO_WAIT
);
213 did_work_request
= 0;
215 if (OCTEON_IS_MODEL(OCTEON_CN68XX
)) {
216 cvmx_write_csr(CVMX_SSO_WQ_IQ_DIS
,
217 1ull << pow_receive_group
);
218 cvmx_write_csr(CVMX_SSO_WQ_INT
,
219 1ull << pow_receive_group
);
221 union cvmx_pow_wq_int wq_int
;
224 wq_int
.s
.iq_dis
= 1 << pow_receive_group
;
225 wq_int
.s
.wq_int
= 1 << pow_receive_group
;
226 cvmx_write_csr(CVMX_POW_WQ_INT
, wq_int
.u64
);
230 pskb
= (struct sk_buff
**)(cvm_oct_get_buffer_ptr(work
->packet_ptr
) -
234 if (USE_ASYNC_IOBDMA
&& rx_count
< (budget
- 1)) {
235 cvmx_pow_work_request_async_nocheck(CVMX_SCR_SCRATCH
,
237 did_work_request
= 1;
241 skb_in_hw
= work
->word2
.s
.bufs
== 1;
242 if (likely(skb_in_hw
)) {
244 prefetch(&skb
->head
);
248 if (octeon_has_feature(OCTEON_FEATURE_PKND
))
249 port
= work
->word0
.pip
.cn68xx
.pknd
;
251 port
= work
->word1
.cn38xx
.ipprt
;
253 prefetch(cvm_oct_device
[port
]);
255 /* Immediately throw away all packets with receive errors */
256 if (unlikely(work
->word2
.snoip
.rcv_error
)) {
257 if (cvm_oct_check_rcv_error(work
))
262 * We can only use the zero copy path if skbuffs are
263 * in the FPA pool and the packet fits in a single
266 if (likely(skb_in_hw
)) {
267 skb
->data
= skb
->head
+ work
->packet_ptr
.s
.addr
-
268 cvmx_ptr_to_phys(skb
->head
);
270 skb
->len
= work
->word1
.len
;
271 skb_set_tail_pointer(skb
, skb
->len
);
272 packet_not_copied
= 1;
275 * We have to copy the packet. First allocate
278 skb
= dev_alloc_skb(work
->word1
.len
);
280 cvm_oct_free_work(work
);
285 * Check if we've received a packet that was
286 * entirely stored in the work entry.
288 if (unlikely(work
->word2
.s
.bufs
== 0)) {
289 u8
*ptr
= work
->packet_data
;
291 if (likely(!work
->word2
.s
.not_IP
)) {
293 * The beginning of the packet
294 * moves for IP packets.
296 if (work
->word2
.s
.is_v6
)
301 memcpy(skb_put(skb
, work
->word1
.len
), ptr
,
303 /* No packet buffers to free */
305 int segments
= work
->word2
.s
.bufs
;
306 union cvmx_buf_ptr segment_ptr
=
308 int len
= work
->word1
.len
;
311 union cvmx_buf_ptr next_ptr
=
312 *(union cvmx_buf_ptr
*)cvmx_phys_to_ptr(segment_ptr
.s
.addr
- 8);
315 * Octeon Errata PKI-100: The segment size is
316 * wrong. Until it is fixed, calculate the
317 * segment size based on the packet pool
318 * buffer size. When it is fixed, the
319 * following line should be replaced with this
320 * one: int segment_size =
321 * segment_ptr.s.size;
324 CVMX_FPA_PACKET_POOL_SIZE
-
325 (segment_ptr
.s
.addr
-
326 (((segment_ptr
.s
.addr
>> 7) -
327 segment_ptr
.s
.back
) << 7));
329 * Don't copy more than what
330 * is left in the packet.
332 if (segment_size
> len
)
334 /* Copy the data into the packet */
335 memcpy(skb_put(skb
, segment_size
),
336 cvmx_phys_to_ptr(segment_ptr
.s
.addr
),
339 segment_ptr
= next_ptr
;
342 packet_not_copied
= 0;
344 if (likely((port
< TOTAL_NUMBER_OF_PORTS
) &&
345 cvm_oct_device
[port
])) {
346 struct net_device
*dev
= cvm_oct_device
[port
];
347 struct octeon_ethernet
*priv
= netdev_priv(dev
);
350 * Only accept packets for devices that are
353 if (likely(dev
->flags
& IFF_UP
)) {
354 skb
->protocol
= eth_type_trans(skb
, dev
);
357 if (unlikely(work
->word2
.s
.not_IP
||
358 work
->word2
.s
.IP_exc
||
359 work
->word2
.s
.L4_error
||
360 !work
->word2
.s
.tcp_or_udp
))
361 skb
->ip_summed
= CHECKSUM_NONE
;
363 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
365 /* Increment RX stats for virtual ports */
366 if (port
>= CVMX_PIP_NUM_INPUT_PORTS
) {
369 (atomic64_t
*)&priv
->stats
.rx_packets
);
370 atomic64_add(skb
->len
,
371 (atomic64_t
*)&priv
->stats
.rx_bytes
);
374 (atomic_t
*)&priv
->stats
.rx_packets
);
376 (atomic_t
*)&priv
->stats
.rx_bytes
);
379 netif_receive_skb(skb
);
381 /* Drop any packet received for a device that isn't up */
383 printk_ratelimited("%s: Device not up, packet dropped\n",
388 (atomic64_t
*)&priv
->stats
.rx_dropped
);
391 (atomic_t
*)&priv
->stats
.rx_dropped
);
393 dev_kfree_skb_irq(skb
);
397 * Drop any packet received for a device that
400 printk_ratelimited("Port %d not controlled by Linux, packet dropped\n",
402 dev_kfree_skb_irq(skb
);
405 * Check to see if the skbuff and work share the same
408 if (likely(packet_not_copied
)) {
410 * This buffer needs to be replaced, increment
411 * the number of buffers we need to free by
414 cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE
,
417 cvmx_fpa_free(work
, CVMX_FPA_WQE_POOL
, 1);
419 cvm_oct_free_work(work
);
422 /* Restore the original POW group mask */
423 if (OCTEON_IS_MODEL(OCTEON_CN68XX
)) {
424 cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid
), old_group_mask
);
425 cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid
)); /* Flush */
427 cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid
), old_group_mask
);
430 if (USE_ASYNC_IOBDMA
) {
431 /* Restore the scratch area */
432 cvmx_scratch_write64(CVMX_SCR_SCRATCH
, old_scratch
);
434 cvm_oct_rx_refill_pool(0);
436 if (rx_count
< budget
&& napi
!= NULL
) {
439 enable_irq(OCTEON_IRQ_WORKQ0
+ pow_receive_group
);
444 #ifdef CONFIG_NET_POLL_CONTROLLER
446 * cvm_oct_poll_controller - poll for receive packets
449 * @dev: Device to poll. Unused
451 void cvm_oct_poll_controller(struct net_device
*dev
)
453 cvm_oct_napi_poll(NULL
, 16);
457 void cvm_oct_rx_initialize(void)
460 struct net_device
*dev_for_napi
= NULL
;
462 for (i
= 0; i
< TOTAL_NUMBER_OF_PORTS
; i
++) {
463 if (cvm_oct_device
[i
]) {
464 dev_for_napi
= cvm_oct_device
[i
];
469 if (NULL
== dev_for_napi
)
470 panic("No net_devices were allocated.");
472 netif_napi_add(dev_for_napi
, &cvm_oct_napi
, cvm_oct_napi_poll
,
474 napi_enable(&cvm_oct_napi
);
476 /* Register an IRQ handler to receive POW interrupts */
477 i
= request_irq(OCTEON_IRQ_WORKQ0
+ pow_receive_group
,
478 cvm_oct_do_interrupt
, 0, "Ethernet", cvm_oct_device
);
481 panic("Could not acquire Ethernet IRQ %d\n",
482 OCTEON_IRQ_WORKQ0
+ pow_receive_group
);
484 disable_irq_nosync(OCTEON_IRQ_WORKQ0
+ pow_receive_group
);
486 /* Enable POW interrupt when our port has at least one packet */
487 if (OCTEON_IS_MODEL(OCTEON_CN68XX
)) {
488 union cvmx_sso_wq_int_thrx int_thr
;
489 union cvmx_pow_wq_int_pc int_pc
;
493 int_thr
.s
.tc_thr
= 1;
494 cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(pow_receive_group
),
499 cvmx_write_csr(CVMX_SSO_WQ_INT_PC
, int_pc
.u64
);
501 union cvmx_pow_wq_int_thrx int_thr
;
502 union cvmx_pow_wq_int_pc int_pc
;
506 int_thr
.s
.tc_thr
= 1;
507 cvmx_write_csr(CVMX_POW_WQ_INT_THRX(pow_receive_group
),
512 cvmx_write_csr(CVMX_POW_WQ_INT_PC
, int_pc
.u64
);
515 /* Schedule NAPI now. This will indirectly enable the interrupt. */
516 napi_schedule(&cvm_oct_napi
);
519 void cvm_oct_rx_shutdown(void)
521 netif_napi_del(&cvm_oct_napi
);