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 <asm/octeon/octeon.h>
31 #include "ethernet-defines.h"
32 #include "ethernet-mem.h"
33 #include "ethernet-rx.h"
34 #include "octeon-ethernet.h"
35 #include "ethernet-util.h"
37 #include <asm/octeon/cvmx-helper.h>
38 #include <asm/octeon/cvmx-wqe.h>
39 #include <asm/octeon/cvmx-fau.h>
40 #include <asm/octeon/cvmx-pow.h>
41 #include <asm/octeon/cvmx-pip.h>
42 #include <asm/octeon/cvmx-scratch.h>
44 #include <asm/octeon/cvmx-gmxx-defs.h>
46 static struct napi_struct cvm_oct_napi;
49 * cvm_oct_do_interrupt - interrupt handler.
50 * @cpl: Interrupt number. Unused
51 * @dev_id: Cookie to identify the device. Unused
53 * The interrupt occurs whenever the POW has packets in our group.
56 static irqreturn_t cvm_oct_do_interrupt(int cpl, void *dev_id)
58 /* Disable the IRQ and start napi_poll. */
59 disable_irq_nosync(OCTEON_IRQ_WORKQ0 + pow_receive_group);
60 napi_schedule(&cvm_oct_napi);
66 * cvm_oct_check_rcv_error - process receive errors
67 * @work: Work queue entry pointing to the packet.
69 * Returns Non-zero if the packet can be dropped, zero otherwise.
71 static inline int cvm_oct_check_rcv_error(cvmx_wqe_t *work)
75 if (octeon_has_feature(OCTEON_FEATURE_PKND))
76 port = work->word0.pip.cn68xx.pknd;
78 port = work->word1.cn38xx.ipprt;
80 if ((work->word2.snoip.err_code == 10) && (work->word1.len <= 64)) {
82 * Ignore length errors on min size packets. Some
83 * equipment incorrectly pads packets to 64+4FCS
84 * instead of 60+4FCS. Note these packets still get
85 * counted as frame errors.
87 } else if (work->word2.snoip.err_code == 5 ||
88 work->word2.snoip.err_code == 7) {
90 * We received a packet with either an alignment error
91 * or a FCS error. This may be signalling that we are
92 * running 10Mbps with GMXX_RXX_FRM_CTL[PRE_CHK]
93 * off. If this is the case we need to parse the
94 * packet to determine if we can remove a non spec
95 * preamble and generate a correct packet.
97 int interface = cvmx_helper_get_interface_num(port);
98 int index = cvmx_helper_get_interface_index_num(port);
99 union cvmx_gmxx_rxx_frm_ctl gmxx_rxx_frm_ctl;
101 gmxx_rxx_frm_ctl.u64 =
102 cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL(index, interface));
103 if (gmxx_rxx_frm_ctl.s.pre_chk == 0) {
106 cvmx_phys_to_ptr(work->packet_ptr.s.addr);
109 while (i < work->word1.len - 1) {
117 /* Port received 0xd5 preamble */
118 work->packet_ptr.s.addr += i + 1;
119 work->word1.len -= i + 5;
120 } else if ((*ptr & 0xf) == 0xd) {
121 /* Port received 0xd preamble */
122 work->packet_ptr.s.addr += i;
123 work->word1.len -= i + 4;
124 for (i = 0; i < work->word1.len; i++) {
126 ((*ptr & 0xf0) >> 4) |
127 ((*(ptr + 1) & 0xf) << 4);
131 printk_ratelimited("Port %d unknown preamble, packet dropped\n",
133 cvm_oct_free_work(work);
138 printk_ratelimited("Port %d receive error code %d, packet dropped\n",
139 port, work->word2.snoip.err_code);
140 cvm_oct_free_work(work);
148 * cvm_oct_napi_poll - the NAPI poll function.
149 * @napi: The NAPI instance, or null if called from cvm_oct_poll_controller
150 * @budget: Maximum number of packets to receive.
152 * Returns the number of packets processed.
154 static int cvm_oct_napi_poll(struct napi_struct *napi, int budget)
156 const int coreid = cvmx_get_core_num();
160 int did_work_request = 0;
161 int packet_not_copied;
163 /* Prefetch cvm_oct_device since we know we need it soon */
164 prefetch(cvm_oct_device);
166 if (USE_ASYNC_IOBDMA) {
167 /* Save scratch in case userspace is using it */
169 old_scratch = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
172 /* Only allow work for our group (and preserve priorities) */
173 if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
174 old_group_mask = cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid));
175 cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid),
176 1ull << pow_receive_group);
177 cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid)); /* Flush */
179 old_group_mask = cvmx_read_csr(CVMX_POW_PP_GRP_MSKX(coreid));
180 cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid),
181 (old_group_mask & ~0xFFFFull) | 1 << pow_receive_group);
184 if (USE_ASYNC_IOBDMA) {
185 cvmx_pow_work_request_async(CVMX_SCR_SCRATCH, CVMX_POW_NO_WAIT);
186 did_work_request = 1;
189 while (rx_count < budget) {
190 struct sk_buff *skb = NULL;
191 struct sk_buff **pskb = NULL;
196 if (USE_ASYNC_IOBDMA && did_work_request)
197 work = cvmx_pow_work_response_async(CVMX_SCR_SCRATCH);
199 work = cvmx_pow_work_request_sync(CVMX_POW_NO_WAIT);
202 did_work_request = 0;
204 if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
205 cvmx_write_csr(CVMX_SSO_WQ_IQ_DIS,
206 1ull << pow_receive_group);
207 cvmx_write_csr(CVMX_SSO_WQ_INT,
208 1ull << pow_receive_group);
210 union cvmx_pow_wq_int wq_int;
213 wq_int.s.iq_dis = 1 << pow_receive_group;
214 wq_int.s.wq_int = 1 << pow_receive_group;
215 cvmx_write_csr(CVMX_POW_WQ_INT, wq_int.u64);
219 pskb = (struct sk_buff **)(cvm_oct_get_buffer_ptr(work->packet_ptr) -
223 if (USE_ASYNC_IOBDMA && rx_count < (budget - 1)) {
224 cvmx_pow_work_request_async_nocheck(CVMX_SCR_SCRATCH,
226 did_work_request = 1;
230 skb_in_hw = work->word2.s.bufs == 1;
231 if (likely(skb_in_hw)) {
233 prefetch(&skb->head);
237 if (octeon_has_feature(OCTEON_FEATURE_PKND))
238 port = work->word0.pip.cn68xx.pknd;
240 port = work->word1.cn38xx.ipprt;
242 prefetch(cvm_oct_device[port]);
244 /* Immediately throw away all packets with receive errors */
245 if (unlikely(work->word2.snoip.rcv_error)) {
246 if (cvm_oct_check_rcv_error(work))
251 * We can only use the zero copy path if skbuffs are
252 * in the FPA pool and the packet fits in a single
255 if (likely(skb_in_hw)) {
256 skb->data = skb->head + work->packet_ptr.s.addr -
257 cvmx_ptr_to_phys(skb->head);
259 skb->len = work->word1.len;
260 skb_set_tail_pointer(skb, skb->len);
261 packet_not_copied = 1;
264 * We have to copy the packet. First allocate
267 skb = dev_alloc_skb(work->word1.len);
269 cvm_oct_free_work(work);
274 * Check if we've received a packet that was
275 * entirely stored in the work entry.
277 if (unlikely(work->word2.s.bufs == 0)) {
278 u8 *ptr = work->packet_data;
280 if (likely(!work->word2.s.not_IP)) {
282 * The beginning of the packet
283 * moves for IP packets.
285 if (work->word2.s.is_v6)
290 memcpy(skb_put(skb, work->word1.len), ptr,
292 /* No packet buffers to free */
294 int segments = work->word2.s.bufs;
295 union cvmx_buf_ptr segment_ptr =
297 int len = work->word1.len;
300 union cvmx_buf_ptr next_ptr =
301 *(union cvmx_buf_ptr *)cvmx_phys_to_ptr(segment_ptr.s.addr - 8);
304 * Octeon Errata PKI-100: The segment size is
305 * wrong. Until it is fixed, calculate the
306 * segment size based on the packet pool
307 * buffer size. When it is fixed, the
308 * following line should be replaced with this
309 * one: int segment_size =
310 * segment_ptr.s.size;
313 CVMX_FPA_PACKET_POOL_SIZE -
314 (segment_ptr.s.addr -
315 (((segment_ptr.s.addr >> 7) -
316 segment_ptr.s.back) << 7));
318 * Don't copy more than what
319 * is left in the packet.
321 if (segment_size > len)
323 /* Copy the data into the packet */
324 memcpy(skb_put(skb, segment_size),
325 cvmx_phys_to_ptr(segment_ptr.s.addr),
328 segment_ptr = next_ptr;
331 packet_not_copied = 0;
333 if (likely((port < TOTAL_NUMBER_OF_PORTS) &&
334 cvm_oct_device[port])) {
335 struct net_device *dev = cvm_oct_device[port];
336 struct octeon_ethernet *priv = netdev_priv(dev);
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 priv->stats.rx_packets++;
357 priv->stats.rx_bytes += skb->len;
359 netif_receive_skb(skb);
362 * Drop any packet received for a device that
365 priv->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);
409 if (rx_count < budget && napi) {
412 enable_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group);
417 #ifdef CONFIG_NET_POLL_CONTROLLER
419 * cvm_oct_poll_controller - poll for receive packets
422 * @dev: Device to poll. Unused
424 void cvm_oct_poll_controller(struct net_device *dev)
426 cvm_oct_napi_poll(NULL, 16);
430 void cvm_oct_rx_initialize(void)
433 struct net_device *dev_for_napi = NULL;
435 for (i = 0; i < TOTAL_NUMBER_OF_PORTS; i++) {
436 if (cvm_oct_device[i]) {
437 dev_for_napi = cvm_oct_device[i];
443 panic("No net_devices were allocated.");
445 netif_napi_add(dev_for_napi, &cvm_oct_napi, cvm_oct_napi_poll,
447 napi_enable(&cvm_oct_napi);
449 /* Register an IRQ handler to receive POW interrupts */
450 i = request_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group,
451 cvm_oct_do_interrupt, 0, "Ethernet", cvm_oct_device);
454 panic("Could not acquire Ethernet IRQ %d\n",
455 OCTEON_IRQ_WORKQ0 + pow_receive_group);
457 disable_irq_nosync(OCTEON_IRQ_WORKQ0 + pow_receive_group);
459 /* Enable POW interrupt when our port has at least one packet */
460 if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
461 union cvmx_sso_wq_int_thrx int_thr;
462 union cvmx_pow_wq_int_pc int_pc;
466 int_thr.s.tc_thr = 1;
467 cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(pow_receive_group),
472 cvmx_write_csr(CVMX_SSO_WQ_INT_PC, int_pc.u64);
474 union cvmx_pow_wq_int_thrx int_thr;
475 union cvmx_pow_wq_int_pc int_pc;
479 int_thr.s.tc_thr = 1;
480 cvmx_write_csr(CVMX_POW_WQ_INT_THRX(pow_receive_group),
485 cvmx_write_csr(CVMX_POW_WQ_INT_PC, int_pc.u64);
488 /* Schedule NAPI now. This will indirectly enable the interrupt. */
489 napi_schedule(&cvm_oct_napi);
492 void cvm_oct_rx_shutdown(void)
494 netif_napi_del(&cvm_oct_napi);