1 // SPDX-License-Identifier: GPL-2.0
3 * This file is based on code from OCTEON SDK by Cavium Networks.
5 * Copyright (c) 2003-2010 Cavium Networks
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/cache.h>
11 #include <linux/cpumask.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
15 #include <linux/string.h>
16 #include <linux/prefetch.h>
17 #include <linux/ratelimit.h>
18 #include <linux/smp.h>
19 #include <linux/interrupt.h>
22 #include <linux/xfrm.h>
24 #endif /* CONFIG_XFRM */
26 #include <asm/octeon/octeon.h>
28 #include "ethernet-defines.h"
29 #include "ethernet-mem.h"
30 #include "ethernet-rx.h"
31 #include "octeon-ethernet.h"
32 #include "ethernet-util.h"
34 #include <asm/octeon/cvmx-helper.h>
35 #include <asm/octeon/cvmx-wqe.h>
36 #include <asm/octeon/cvmx-fau.h>
37 #include <asm/octeon/cvmx-pow.h>
38 #include <asm/octeon/cvmx-pip.h>
39 #include <asm/octeon/cvmx-scratch.h>
41 #include <asm/octeon/cvmx-gmxx-defs.h>
43 static atomic_t oct_rx_ready
= ATOMIC_INIT(0);
45 static struct oct_rx_group
{
48 struct napi_struct napi
;
52 * cvm_oct_do_interrupt - interrupt handler.
53 * @irq: Interrupt number.
54 * @napi_id: Cookie to identify the NAPI instance.
56 * The interrupt occurs whenever the POW has packets in our group.
59 static irqreturn_t
cvm_oct_do_interrupt(int irq
, void *napi_id
)
61 /* Disable the IRQ and start napi_poll. */
62 disable_irq_nosync(irq
);
63 napi_schedule(napi_id
);
69 * cvm_oct_check_rcv_error - process receive errors
70 * @work: Work queue entry pointing to the packet.
72 * Returns Non-zero if the packet can be dropped, zero otherwise.
74 static inline int cvm_oct_check_rcv_error(cvmx_wqe_t
*work
)
78 if (octeon_has_feature(OCTEON_FEATURE_PKND
))
79 port
= work
->word0
.pip
.cn68xx
.pknd
;
81 port
= work
->word1
.cn38xx
.ipprt
;
83 if ((work
->word2
.snoip
.err_code
== 10) && (work
->word1
.len
<= 64)) {
85 * Ignore length errors on min size packets. Some
86 * equipment incorrectly pads packets to 64+4FCS
87 * instead of 60+4FCS. Note these packets still get
88 * counted as frame errors.
90 } else if (work
->word2
.snoip
.err_code
== 5 ||
91 work
->word2
.snoip
.err_code
== 7) {
93 * We received a packet with either an alignment error
94 * or a FCS error. This may be signalling that we are
95 * running 10Mbps with GMXX_RXX_FRM_CTL[PRE_CHK]
96 * off. If this is the case we need to parse the
97 * packet to determine if we can remove a non spec
98 * preamble and generate a correct packet.
100 int interface
= cvmx_helper_get_interface_num(port
);
101 int index
= cvmx_helper_get_interface_index_num(port
);
102 union cvmx_gmxx_rxx_frm_ctl gmxx_rxx_frm_ctl
;
104 gmxx_rxx_frm_ctl
.u64
=
105 cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL(index
, interface
));
106 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) {
119 /* Port received 0xd5 preamble */
120 work
->packet_ptr
.s
.addr
+= i
+ 1;
121 work
->word1
.len
-= i
+ 5;
122 } else if ((*ptr
& 0xf) == 0xd) {
123 /* Port received 0xd preamble */
124 work
->packet_ptr
.s
.addr
+= i
;
125 work
->word1
.len
-= i
+ 4;
126 for (i
= 0; i
< work
->word1
.len
; i
++) {
128 ((*ptr
& 0xf0) >> 4) |
129 ((*(ptr
+ 1) & 0xf) << 4);
133 printk_ratelimited("Port %d unknown preamble, packet dropped\n",
135 cvm_oct_free_work(work
);
140 printk_ratelimited("Port %d receive error code %d, packet dropped\n",
141 port
, work
->word2
.snoip
.err_code
);
142 cvm_oct_free_work(work
);
149 static void copy_segments_to_skb(cvmx_wqe_t
*work
, struct sk_buff
*skb
)
151 int segments
= work
->word2
.s
.bufs
;
152 union cvmx_buf_ptr segment_ptr
= work
->packet_ptr
;
153 int len
= work
->word1
.len
;
157 union cvmx_buf_ptr next_ptr
;
159 next_ptr
= *(union cvmx_buf_ptr
*)
160 cvmx_phys_to_ptr(segment_ptr
.s
.addr
- 8);
163 * Octeon Errata PKI-100: The segment size is wrong.
165 * Until it is fixed, calculate the segment size based on
166 * the packet pool buffer size.
167 * When it is fixed, the following line should be replaced
169 * int segment_size = segment_ptr.s.size;
172 CVMX_FPA_PACKET_POOL_SIZE
-
173 (segment_ptr
.s
.addr
-
174 (((segment_ptr
.s
.addr
>> 7) -
175 segment_ptr
.s
.back
) << 7));
177 /* Don't copy more than what is left in the packet */
178 if (segment_size
> len
)
181 /* Copy the data into the packet */
182 skb_put_data(skb
, cvmx_phys_to_ptr(segment_ptr
.s
.addr
),
185 segment_ptr
= next_ptr
;
189 static int cvm_oct_poll(struct oct_rx_group
*rx_group
, int budget
)
191 const int coreid
= cvmx_get_core_num();
195 int did_work_request
= 0;
196 int packet_not_copied
;
198 /* Prefetch cvm_oct_device since we know we need it soon */
199 prefetch(cvm_oct_device
);
201 if (USE_ASYNC_IOBDMA
) {
202 /* Save scratch in case userspace is using it */
204 old_scratch
= cvmx_scratch_read64(CVMX_SCR_SCRATCH
);
207 /* Only allow work for our group (and preserve priorities) */
208 if (OCTEON_IS_MODEL(OCTEON_CN68XX
)) {
209 old_group_mask
= cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid
));
210 cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid
),
211 BIT(rx_group
->group
));
212 cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid
)); /* Flush */
214 old_group_mask
= cvmx_read_csr(CVMX_POW_PP_GRP_MSKX(coreid
));
215 cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid
),
216 (old_group_mask
& ~0xFFFFull
) |
217 BIT(rx_group
->group
));
220 if (USE_ASYNC_IOBDMA
) {
221 cvmx_pow_work_request_async(CVMX_SCR_SCRATCH
, CVMX_POW_NO_WAIT
);
222 did_work_request
= 1;
225 while (rx_count
< budget
) {
226 struct sk_buff
*skb
= NULL
;
227 struct sk_buff
**pskb
= NULL
;
232 if (USE_ASYNC_IOBDMA
&& did_work_request
)
233 work
= cvmx_pow_work_response_async(CVMX_SCR_SCRATCH
);
235 work
= cvmx_pow_work_request_sync(CVMX_POW_NO_WAIT
);
238 did_work_request
= 0;
240 if (OCTEON_IS_MODEL(OCTEON_CN68XX
)) {
241 cvmx_write_csr(CVMX_SSO_WQ_IQ_DIS
,
242 BIT(rx_group
->group
));
243 cvmx_write_csr(CVMX_SSO_WQ_INT
,
244 BIT(rx_group
->group
));
246 union cvmx_pow_wq_int wq_int
;
249 wq_int
.s
.iq_dis
= BIT(rx_group
->group
);
250 wq_int
.s
.wq_int
= BIT(rx_group
->group
);
251 cvmx_write_csr(CVMX_POW_WQ_INT
, wq_int
.u64
);
255 pskb
= (struct sk_buff
**)
256 (cvm_oct_get_buffer_ptr(work
->packet_ptr
) -
260 if (USE_ASYNC_IOBDMA
&& rx_count
< (budget
- 1)) {
261 cvmx_pow_work_request_async_nocheck(CVMX_SCR_SCRATCH
,
263 did_work_request
= 1;
267 skb_in_hw
= work
->word2
.s
.bufs
== 1;
268 if (likely(skb_in_hw
)) {
270 prefetch(&skb
->head
);
274 if (octeon_has_feature(OCTEON_FEATURE_PKND
))
275 port
= work
->word0
.pip
.cn68xx
.pknd
;
277 port
= work
->word1
.cn38xx
.ipprt
;
279 prefetch(cvm_oct_device
[port
]);
281 /* Immediately throw away all packets with receive errors */
282 if (unlikely(work
->word2
.snoip
.rcv_error
)) {
283 if (cvm_oct_check_rcv_error(work
))
288 * We can only use the zero copy path if skbuffs are
289 * in the FPA pool and the packet fits in a single
292 if (likely(skb_in_hw
)) {
293 skb
->data
= skb
->head
+ work
->packet_ptr
.s
.addr
-
294 cvmx_ptr_to_phys(skb
->head
);
296 skb
->len
= work
->word1
.len
;
297 skb_set_tail_pointer(skb
, skb
->len
);
298 packet_not_copied
= 1;
301 * We have to copy the packet. First allocate
304 skb
= dev_alloc_skb(work
->word1
.len
);
306 cvm_oct_free_work(work
);
311 * Check if we've received a packet that was
312 * entirely stored in the work entry.
314 if (unlikely(work
->word2
.s
.bufs
== 0)) {
315 u8
*ptr
= work
->packet_data
;
317 if (likely(!work
->word2
.s
.not_IP
)) {
319 * The beginning of the packet
320 * moves for IP packets.
322 if (work
->word2
.s
.is_v6
)
327 skb_put_data(skb
, ptr
, work
->word1
.len
);
328 /* No packet buffers to free */
330 copy_segments_to_skb(work
, skb
);
332 packet_not_copied
= 0;
334 if (likely((port
< TOTAL_NUMBER_OF_PORTS
) &&
335 cvm_oct_device
[port
])) {
336 struct net_device
*dev
= cvm_oct_device
[port
];
339 * Only accept packets for devices that are
342 if (likely(dev
->flags
& IFF_UP
)) {
343 skb
->protocol
= eth_type_trans(skb
, dev
);
346 if (unlikely(work
->word2
.s
.not_IP
||
347 work
->word2
.s
.IP_exc
||
348 work
->word2
.s
.L4_error
||
349 !work
->word2
.s
.tcp_or_udp
))
350 skb
->ip_summed
= CHECKSUM_NONE
;
352 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
354 /* Increment RX stats for virtual ports */
355 if (port
>= CVMX_PIP_NUM_INPUT_PORTS
) {
356 dev
->stats
.rx_packets
++;
357 dev
->stats
.rx_bytes
+= skb
->len
;
359 netif_receive_skb(skb
);
362 * Drop any packet received for a device that
365 dev
->stats
.rx_dropped
++;
366 dev_kfree_skb_irq(skb
);
370 * Drop any packet received for a device that
373 printk_ratelimited("Port %d not controlled by Linux, packet dropped\n",
375 dev_kfree_skb_irq(skb
);
378 * Check to see if the skbuff and work share the same
381 if (likely(packet_not_copied
)) {
383 * This buffer needs to be replaced, increment
384 * the number of buffers we need to free by
387 cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE
,
390 cvmx_fpa_free(work
, CVMX_FPA_WQE_POOL
, 1);
392 cvm_oct_free_work(work
);
395 /* Restore the original POW group mask */
396 if (OCTEON_IS_MODEL(OCTEON_CN68XX
)) {
397 cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid
), old_group_mask
);
398 cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid
)); /* Flush */
400 cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid
), old_group_mask
);
403 if (USE_ASYNC_IOBDMA
) {
404 /* Restore the scratch area */
405 cvmx_scratch_write64(CVMX_SCR_SCRATCH
, old_scratch
);
407 cvm_oct_rx_refill_pool(0);
413 * cvm_oct_napi_poll - the NAPI poll function.
414 * @napi: The NAPI instance.
415 * @budget: Maximum number of packets to receive.
417 * Returns the number of packets processed.
419 static int cvm_oct_napi_poll(struct napi_struct
*napi
, int budget
)
421 struct oct_rx_group
*rx_group
= container_of(napi
, struct oct_rx_group
,
425 rx_count
= cvm_oct_poll(rx_group
, budget
);
427 if (rx_count
< budget
) {
429 napi_complete_done(napi
, rx_count
);
430 enable_irq(rx_group
->irq
);
435 #ifdef CONFIG_NET_POLL_CONTROLLER
437 * cvm_oct_poll_controller - poll for receive packets
440 * @dev: Device to poll. Unused
442 void cvm_oct_poll_controller(struct net_device
*dev
)
446 if (!atomic_read(&oct_rx_ready
))
449 for (i
= 0; i
< ARRAY_SIZE(oct_rx_group
); i
++) {
450 if (!(pow_receive_groups
& BIT(i
)))
453 cvm_oct_poll(&oct_rx_group
[i
], 16);
458 void cvm_oct_rx_initialize(void)
461 struct net_device
*dev_for_napi
= NULL
;
463 for (i
= 0; i
< TOTAL_NUMBER_OF_PORTS
; i
++) {
464 if (cvm_oct_device
[i
]) {
465 dev_for_napi
= cvm_oct_device
[i
];
471 panic("No net_devices were allocated.");
473 for (i
= 0; i
< ARRAY_SIZE(oct_rx_group
); i
++) {
476 if (!(pow_receive_groups
& BIT(i
)))
479 netif_napi_add(dev_for_napi
, &oct_rx_group
[i
].napi
,
480 cvm_oct_napi_poll
, rx_napi_weight
);
481 napi_enable(&oct_rx_group
[i
].napi
);
483 oct_rx_group
[i
].irq
= OCTEON_IRQ_WORKQ0
+ i
;
484 oct_rx_group
[i
].group
= i
;
486 /* Register an IRQ handler to receive POW interrupts */
487 ret
= request_irq(oct_rx_group
[i
].irq
, cvm_oct_do_interrupt
, 0,
488 "Ethernet", &oct_rx_group
[i
].napi
);
490 panic("Could not acquire Ethernet IRQ %d\n",
491 oct_rx_group
[i
].irq
);
493 disable_irq_nosync(oct_rx_group
[i
].irq
);
495 /* Enable POW interrupt when our port has at least one packet */
496 if (OCTEON_IS_MODEL(OCTEON_CN68XX
)) {
497 union cvmx_sso_wq_int_thrx int_thr
;
498 union cvmx_pow_wq_int_pc int_pc
;
502 int_thr
.s
.tc_thr
= 1;
503 cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(i
), int_thr
.u64
);
507 cvmx_write_csr(CVMX_SSO_WQ_INT_PC
, int_pc
.u64
);
509 union cvmx_pow_wq_int_thrx int_thr
;
510 union cvmx_pow_wq_int_pc int_pc
;
514 int_thr
.s
.tc_thr
= 1;
515 cvmx_write_csr(CVMX_POW_WQ_INT_THRX(i
), int_thr
.u64
);
519 cvmx_write_csr(CVMX_POW_WQ_INT_PC
, int_pc
.u64
);
522 /* Schedule NAPI now. This will indirectly enable the
525 napi_schedule(&oct_rx_group
[i
].napi
);
527 atomic_inc(&oct_rx_ready
);
530 void cvm_oct_rx_shutdown(void)
534 for (i
= 0; i
< ARRAY_SIZE(oct_rx_group
); i
++) {
535 if (!(pow_receive_groups
& BIT(i
)))
538 /* Disable POW interrupt */
539 if (OCTEON_IS_MODEL(OCTEON_CN68XX
))
540 cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(i
), 0);
542 cvmx_write_csr(CVMX_POW_WQ_INT_THRX(i
), 0);
544 /* Free the interrupt handler */
545 free_irq(oct_rx_group
[i
].irq
, cvm_oct_device
);
547 netif_napi_del(&oct_rx_group
[i
].napi
);