dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / staging / octeon / ethernet-rx.c
blob6aed3cf6c0b43402d11511f47167922f75ca2aec
1 /*
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.
9 */
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>
17 #include <linux/ip.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>
23 #include <net/dst.h>
24 #ifdef CONFIG_XFRM
25 #include <linux/xfrm.h>
26 #include <net/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;
50 /**
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);
64 return IRQ_HANDLED;
67 /**
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)
75 int port;
77 if (octeon_has_feature(OCTEON_FEATURE_PKND))
78 port = work->word0.pip.cn68xx.pknd;
79 else
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) {
107 u8 *ptr =
108 cvmx_phys_to_ptr(work->packet_ptr.s.addr);
109 int i = 0;
111 while (i < work->word1.len - 1) {
112 if (*ptr != 0x55)
113 break;
114 ptr++;
115 i++;
118 if (*ptr == 0xd5) {
120 printk_ratelimited("Port %d received 0xd5 preamble\n",
121 port);
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",
128 port);
130 work->packet_ptr.s.addr += i;
131 work->word1.len -= i + 4;
132 for (i = 0; i < work->word1.len; i++) {
133 *ptr =
134 ((*ptr & 0xf0) >> 4) |
135 ((*(ptr + 1) & 0xf) << 4);
136 ptr++;
138 } else {
139 printk_ratelimited("Port %d unknown preamble, packet dropped\n",
140 port);
142 cvmx_helper_dump_packet(work);
144 cvm_oct_free_work(work);
145 return 1;
148 } else {
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);
152 return 1;
155 return 0;
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();
168 u64 old_group_mask;
169 u64 old_scratch;
170 int rx_count = 0;
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 */
179 CVMX_SYNCIOBDMA;
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 */
189 } else {
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;
203 int skb_in_hw;
204 cvmx_wqe_t *work;
205 int port;
207 if (USE_ASYNC_IOBDMA && did_work_request)
208 work = cvmx_pow_work_response_async(CVMX_SCR_SCRATCH);
209 else
210 work = cvmx_pow_work_request_sync(CVMX_POW_NO_WAIT);
212 prefetch(work);
213 did_work_request = 0;
214 if (work == NULL) {
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);
220 } else {
221 union cvmx_pow_wq_int wq_int;
223 wq_int.u64 = 0;
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);
228 break;
230 pskb = (struct sk_buff **)(cvm_oct_get_buffer_ptr(work->packet_ptr) -
231 sizeof(void *));
232 prefetch(pskb);
234 if (USE_ASYNC_IOBDMA && rx_count < (budget - 1)) {
235 cvmx_pow_work_request_async_nocheck(CVMX_SCR_SCRATCH,
236 CVMX_POW_NO_WAIT);
237 did_work_request = 1;
239 rx_count++;
241 skb_in_hw = work->word2.s.bufs == 1;
242 if (likely(skb_in_hw)) {
243 skb = *pskb;
244 prefetch(&skb->head);
245 prefetch(&skb->len);
248 if (octeon_has_feature(OCTEON_FEATURE_PKND))
249 port = work->word0.pip.cn68xx.pknd;
250 else
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))
258 continue;
262 * We can only use the zero copy path if skbuffs are
263 * in the FPA pool and the packet fits in a single
264 * buffer.
266 if (likely(skb_in_hw)) {
267 skb->data = skb->head + work->packet_ptr.s.addr -
268 cvmx_ptr_to_phys(skb->head);
269 prefetch(skb->data);
270 skb->len = work->word1.len;
271 skb_set_tail_pointer(skb, skb->len);
272 packet_not_copied = 1;
273 } else {
275 * We have to copy the packet. First allocate
276 * an skbuff for it.
278 skb = dev_alloc_skb(work->word1.len);
279 if (!skb) {
280 cvm_oct_free_work(work);
281 continue;
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)
297 ptr += 2;
298 else
299 ptr += 6;
301 memcpy(skb_put(skb, work->word1.len), ptr,
302 work->word1.len);
303 /* No packet buffers to free */
304 } else {
305 int segments = work->word2.s.bufs;
306 union cvmx_buf_ptr segment_ptr =
307 work->packet_ptr;
308 int len = work->word1.len;
310 while (segments--) {
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;
323 int segment_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)
333 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),
337 segment_size);
338 len -= segment_size;
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
351 * currently up.
353 if (likely(dev->flags & IFF_UP)) {
354 skb->protocol = eth_type_trans(skb, dev);
355 skb->dev = 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;
362 else
363 skb->ip_summed = CHECKSUM_UNNECESSARY;
365 /* Increment RX stats for virtual ports */
366 if (port >= CVMX_PIP_NUM_INPUT_PORTS) {
367 #ifdef CONFIG_64BIT
368 atomic64_add(1,
369 (atomic64_t *)&priv->stats.rx_packets);
370 atomic64_add(skb->len,
371 (atomic64_t *)&priv->stats.rx_bytes);
372 #else
373 atomic_add(1,
374 (atomic_t *)&priv->stats.rx_packets);
375 atomic_add(skb->len,
376 (atomic_t *)&priv->stats.rx_bytes);
377 #endif
379 netif_receive_skb(skb);
380 } else {
381 /* Drop any packet received for a device that isn't up */
383 printk_ratelimited("%s: Device not up, packet dropped\n",
384 dev->name);
386 #ifdef CONFIG_64BIT
387 atomic64_add(1,
388 (atomic64_t *)&priv->stats.rx_dropped);
389 #else
390 atomic_add(1,
391 (atomic_t *)&priv->stats.rx_dropped);
392 #endif
393 dev_kfree_skb_irq(skb);
395 } else {
397 * Drop any packet received for a device that
398 * doesn't exist.
400 printk_ratelimited("Port %d not controlled by Linux, packet dropped\n",
401 port);
402 dev_kfree_skb_irq(skb);
405 * Check to see if the skbuff and work share the same
406 * packet buffer.
408 if (likely(packet_not_copied)) {
410 * This buffer needs to be replaced, increment
411 * the number of buffers we need to free by
412 * one.
414 cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE,
417 cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, 1);
418 } else {
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 */
426 } else {
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) {
437 /* No more work */
438 napi_complete(napi);
439 enable_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group);
441 return rx_count;
444 #ifdef CONFIG_NET_POLL_CONTROLLER
446 * cvm_oct_poll_controller - poll for receive packets
447 * device.
449 * @dev: Device to poll. Unused
451 void cvm_oct_poll_controller(struct net_device *dev)
453 cvm_oct_napi_poll(NULL, 16);
455 #endif
457 void cvm_oct_rx_initialize(void)
459 int i;
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];
465 break;
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,
473 rx_napi_weight);
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);
480 if (i)
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;
491 int_thr.u64 = 0;
492 int_thr.s.tc_en = 1;
493 int_thr.s.tc_thr = 1;
494 cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(pow_receive_group),
495 int_thr.u64);
497 int_pc.u64 = 0;
498 int_pc.s.pc_thr = 5;
499 cvmx_write_csr(CVMX_SSO_WQ_INT_PC, int_pc.u64);
500 } else {
501 union cvmx_pow_wq_int_thrx int_thr;
502 union cvmx_pow_wq_int_pc int_pc;
504 int_thr.u64 = 0;
505 int_thr.s.tc_en = 1;
506 int_thr.s.tc_thr = 1;
507 cvmx_write_csr(CVMX_POW_WQ_INT_THRX(pow_receive_group),
508 int_thr.u64);
510 int_pc.u64 = 0;
511 int_pc.s.pc_thr = 5;
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);