1636bd9a5bc073e5c2287b1ad634138e397d763c
[linux-drm-fsl-dcu.git] / drivers / staging / octeon / ethernet-rx.c
1 /*
2  * This file is based on code from OCTEON SDK by Cavium Networks.
3  *
4  * Copyright (c) 2003-2010 Cavium Networks
5  *
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  */
10
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 */
28
29 #include <linux/atomic.h>
30
31 #include <asm/octeon/octeon.h>
32
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"
38
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>
45
46 #include <asm/octeon/cvmx-gmxx-defs.h>
47
48 static struct napi_struct cvm_oct_napi;
49
50 /**
51  * cvm_oct_do_interrupt - interrupt handler.
52  *
53  * The interrupt occurs whenever the POW has packets in our group.
54  *
55  */
56 static irqreturn_t cvm_oct_do_interrupt(int cpl, void *dev_id)
57 {
58         /* Disable the IRQ and start napi_poll. */
59         disable_irq_nosync(OCTEON_IRQ_WORKQ0 + pow_receive_group);
60         napi_schedule(&cvm_oct_napi);
61
62         return IRQ_HANDLED;
63 }
64
65 /**
66  * cvm_oct_check_rcv_error - process receive errors
67  * @work: Work queue entry pointing to the packet.
68  *
69  * Returns Non-zero if the packet can be dropped, zero otherwise.
70  */
71 static inline int cvm_oct_check_rcv_error(cvmx_wqe_t *work)
72 {
73         if ((work->word2.snoip.err_code == 10) && (work->len <= 64)) {
74                 /*
75                  * Ignore length errors on min size packets. Some
76                  * equipment incorrectly pads packets to 64+4FCS
77                  * instead of 60+4FCS.  Note these packets still get
78                  * counted as frame errors.
79                  */
80         } else if (work->word2.snoip.err_code == 5 ||
81                    work->word2.snoip.err_code == 7) {
82                 /*
83                  * We received a packet with either an alignment error
84                  * or a FCS error. This may be signalling that we are
85                  * running 10Mbps with GMXX_RXX_FRM_CTL[PRE_CHK]
86                  * off. If this is the case we need to parse the
87                  * packet to determine if we can remove a non spec
88                  * preamble and generate a correct packet.
89                  */
90                 int interface = cvmx_helper_get_interface_num(work->ipprt);
91                 int index = cvmx_helper_get_interface_index_num(work->ipprt);
92                 union cvmx_gmxx_rxx_frm_ctl gmxx_rxx_frm_ctl;
93
94                 gmxx_rxx_frm_ctl.u64 =
95                     cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL(index, interface));
96                 if (gmxx_rxx_frm_ctl.s.pre_chk == 0) {
97
98                         uint8_t *ptr =
99                             cvmx_phys_to_ptr(work->packet_ptr.s.addr);
100                         int i = 0;
101
102                         while (i < work->len - 1) {
103                                 if (*ptr != 0x55)
104                                         break;
105                                 ptr++;
106                                 i++;
107                         }
108
109                         if (*ptr == 0xd5) {
110                                 /*
111                                   printk_ratelimited("Port %d received 0xd5 preamble\n",
112                                           work->ipprt);
113                                  */
114                                 work->packet_ptr.s.addr += i + 1;
115                                 work->len -= i + 5;
116                         } else if ((*ptr & 0xf) == 0xd) {
117                                 /*
118                                   printk_ratelimited("Port %d received 0x?d preamble\n",
119                                           work->ipprt);
120                                  */
121                                 work->packet_ptr.s.addr += i;
122                                 work->len -= i + 4;
123                                 for (i = 0; i < work->len; i++) {
124                                         *ptr =
125                                             ((*ptr & 0xf0) >> 4) |
126                                             ((*(ptr + 1) & 0xf) << 4);
127                                         ptr++;
128                                 }
129                         } else {
130                                 printk_ratelimited("Port %d unknown preamble, packet dropped\n",
131                                                    work->ipprt);
132                                 /*
133                                    cvmx_helper_dump_packet(work);
134                                  */
135                                 cvm_oct_free_work(work);
136                                 return 1;
137                         }
138                 }
139         } else {
140                 printk_ratelimited("Port %d receive error code %d, packet dropped\n",
141                                    work->ipprt, work->word2.snoip.err_code);
142                 cvm_oct_free_work(work);
143                 return 1;
144         }
145
146         return 0;
147 }
148
149 /**
150  * cvm_oct_napi_poll - the NAPI poll function.
151  * @napi: The NAPI instance, or null if called from cvm_oct_poll_controller
152  * @budget: Maximum number of packets to receive.
153  *
154  * Returns the number of packets processed.
155  */
156 static int cvm_oct_napi_poll(struct napi_struct *napi, int budget)
157 {
158         const int       coreid = cvmx_get_core_num();
159         uint64_t        old_group_mask;
160         uint64_t        old_scratch;
161         int             rx_count = 0;
162         int             did_work_request = 0;
163         int             packet_not_copied;
164
165         /* Prefetch cvm_oct_device since we know we need it soon */
166         prefetch(cvm_oct_device);
167
168         if (USE_ASYNC_IOBDMA) {
169                 /* Save scratch in case userspace is using it */
170                 CVMX_SYNCIOBDMA;
171                 old_scratch = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
172         }
173
174         /* Only allow work for our group (and preserve priorities) */
175         old_group_mask = cvmx_read_csr(CVMX_POW_PP_GRP_MSKX(coreid));
176         cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid),
177                        (old_group_mask & ~0xFFFFull) | 1 << pow_receive_group);
178
179         if (USE_ASYNC_IOBDMA) {
180                 cvmx_pow_work_request_async(CVMX_SCR_SCRATCH, CVMX_POW_NO_WAIT);
181                 did_work_request = 1;
182         }
183
184         while (rx_count < budget) {
185                 struct sk_buff *skb = NULL;
186                 struct sk_buff **pskb = NULL;
187                 int skb_in_hw;
188                 cvmx_wqe_t *work;
189
190                 if (USE_ASYNC_IOBDMA && did_work_request)
191                         work = cvmx_pow_work_response_async(CVMX_SCR_SCRATCH);
192                 else
193                         work = cvmx_pow_work_request_sync(CVMX_POW_NO_WAIT);
194
195                 prefetch(work);
196                 did_work_request = 0;
197                 if (work == NULL) {
198                         if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
199                                 cvmx_write_csr(CVMX_SSO_WQ_IQ_DIS,
200                                                1ull << pow_receive_group);
201                                 cvmx_write_csr(CVMX_SSO_WQ_INT,
202                                                1ull << pow_receive_group);
203                         } else {
204                                 union cvmx_pow_wq_int wq_int;
205
206                                 wq_int.u64 = 0;
207                                 wq_int.s.iq_dis = 1 << pow_receive_group;
208                                 wq_int.s.wq_int = 1 << pow_receive_group;
209                                 cvmx_write_csr(CVMX_POW_WQ_INT, wq_int.u64);
210                         }
211                         break;
212                 }
213                 pskb = (struct sk_buff **)(cvm_oct_get_buffer_ptr(work->packet_ptr) -
214                         sizeof(void *));
215                 prefetch(pskb);
216
217                 if (USE_ASYNC_IOBDMA && rx_count < (budget - 1)) {
218                         cvmx_pow_work_request_async_nocheck(CVMX_SCR_SCRATCH,
219                                                             CVMX_POW_NO_WAIT);
220                         did_work_request = 1;
221                 }
222                 rx_count++;
223
224                 skb_in_hw = work->word2.s.bufs == 1;
225                 if (likely(skb_in_hw)) {
226                         skb = *pskb;
227                         prefetch(&skb->head);
228                         prefetch(&skb->len);
229                 }
230                 prefetch(cvm_oct_device[work->ipprt]);
231
232                 /* Immediately throw away all packets with receive errors */
233                 if (unlikely(work->word2.snoip.rcv_error)) {
234                         if (cvm_oct_check_rcv_error(work))
235                                 continue;
236                 }
237
238                 /*
239                  * We can only use the zero copy path if skbuffs are
240                  * in the FPA pool and the packet fits in a single
241                  * buffer.
242                  */
243                 if (likely(skb_in_hw)) {
244                         skb->data = skb->head + work->packet_ptr.s.addr -
245                                 cvmx_ptr_to_phys(skb->head);
246                         prefetch(skb->data);
247                         skb->len = work->len;
248                         skb_set_tail_pointer(skb, skb->len);
249                         packet_not_copied = 1;
250                 } else {
251                         /*
252                          * We have to copy the packet. First allocate
253                          * an skbuff for it.
254                          */
255                         skb = dev_alloc_skb(work->len);
256                         if (!skb) {
257                                 cvm_oct_free_work(work);
258                                 continue;
259                         }
260
261                         /*
262                          * Check if we've received a packet that was
263                          * entirely stored in the work entry.
264                          */
265                         if (unlikely(work->word2.s.bufs == 0)) {
266                                 uint8_t *ptr = work->packet_data;
267
268                                 if (likely(!work->word2.s.not_IP)) {
269                                         /*
270                                          * The beginning of the packet
271                                          * moves for IP packets.
272                                          */
273                                         if (work->word2.s.is_v6)
274                                                 ptr += 2;
275                                         else
276                                                 ptr += 6;
277                                 }
278                                 memcpy(skb_put(skb, work->len), ptr, work->len);
279                                 /* No packet buffers to free */
280                         } else {
281                                 int segments = work->word2.s.bufs;
282                                 union cvmx_buf_ptr segment_ptr =
283                                     work->packet_ptr;
284                                 int len = work->len;
285
286                                 while (segments--) {
287                                         union cvmx_buf_ptr next_ptr =
288                                             *(union cvmx_buf_ptr *)cvmx_phys_to_ptr(segment_ptr.s.addr - 8);
289
290                         /*
291                          * Octeon Errata PKI-100: The segment size is
292                          * wrong. Until it is fixed, calculate the
293                          * segment size based on the packet pool
294                          * buffer size. When it is fixed, the
295                          * following line should be replaced with this
296                          * one: int segment_size =
297                          * segment_ptr.s.size;
298                          */
299                                         int segment_size =
300                                             CVMX_FPA_PACKET_POOL_SIZE -
301                                             (segment_ptr.s.addr -
302                                              (((segment_ptr.s.addr >> 7) -
303                                                segment_ptr.s.back) << 7));
304                                         /*
305                                          * Don't copy more than what
306                                          * is left in the packet.
307                                          */
308                                         if (segment_size > len)
309                                                 segment_size = len;
310                                         /* Copy the data into the packet */
311                                         memcpy(skb_put(skb, segment_size),
312                                                cvmx_phys_to_ptr(segment_ptr.s.addr),
313                                                segment_size);
314                                         len -= segment_size;
315                                         segment_ptr = next_ptr;
316                                 }
317                         }
318                         packet_not_copied = 0;
319                 }
320
321                 if (likely((work->ipprt < TOTAL_NUMBER_OF_PORTS) &&
322                            cvm_oct_device[work->ipprt])) {
323                         struct net_device *dev = cvm_oct_device[work->ipprt];
324                         struct octeon_ethernet *priv = netdev_priv(dev);
325
326                         /*
327                          * Only accept packets for devices that are
328                          * currently up.
329                          */
330                         if (likely(dev->flags & IFF_UP)) {
331                                 skb->protocol = eth_type_trans(skb, dev);
332                                 skb->dev = dev;
333
334                                 if (unlikely(work->word2.s.not_IP ||
335                                              work->word2.s.IP_exc ||
336                                              work->word2.s.L4_error ||
337                                              !work->word2.s.tcp_or_udp))
338                                         skb->ip_summed = CHECKSUM_NONE;
339                                 else
340                                         skb->ip_summed = CHECKSUM_UNNECESSARY;
341
342                                 /* Increment RX stats for virtual ports */
343                                 if (work->ipprt >= CVMX_PIP_NUM_INPUT_PORTS) {
344 #ifdef CONFIG_64BIT
345                                         atomic64_add(1,
346                                                      (atomic64_t *)&priv->stats.rx_packets);
347                                         atomic64_add(skb->len,
348                                                      (atomic64_t *)&priv->stats.rx_bytes);
349 #else
350                                         atomic_add(1,
351                                                    (atomic_t *)&priv->stats.rx_packets);
352                                         atomic_add(skb->len,
353                                                    (atomic_t *)&priv->stats.rx_bytes);
354 #endif
355                                 }
356                                 netif_receive_skb(skb);
357                         } else {
358                                 /* Drop any packet received for a device that isn't up */
359                                 /*
360                                   printk_ratelimited("%s: Device not up, packet dropped\n",
361                                            dev->name);
362                                 */
363 #ifdef CONFIG_64BIT
364                                 atomic64_add(1,
365                                              (atomic64_t *)&priv->stats.rx_dropped);
366 #else
367                                 atomic_add(1,
368                                            (atomic_t *)&priv->stats.rx_dropped);
369 #endif
370                                 dev_kfree_skb_irq(skb);
371                         }
372                 } else {
373                         /*
374                          * Drop any packet received for a device that
375                          * doesn't exist.
376                          */
377                         printk_ratelimited("Port %d not controlled by Linux, packet dropped\n",
378                                    work->ipprt);
379                         dev_kfree_skb_irq(skb);
380                 }
381                 /*
382                  * Check to see if the skbuff and work share the same
383                  * packet buffer.
384                  */
385                 if (likely(packet_not_copied)) {
386                         /*
387                          * This buffer needs to be replaced, increment
388                          * the number of buffers we need to free by
389                          * one.
390                          */
391                         cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE,
392                                               1);
393
394                         cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, 1);
395                 } else {
396                         cvm_oct_free_work(work);
397                 }
398         }
399         /* Restore the original POW group mask */
400         cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid), old_group_mask);
401         if (USE_ASYNC_IOBDMA) {
402                 /* Restore the scratch area */
403                 cvmx_scratch_write64(CVMX_SCR_SCRATCH, old_scratch);
404         }
405         cvm_oct_rx_refill_pool(0);
406
407         if (rx_count < budget && napi != NULL) {
408                 /* No more work */
409                 napi_complete(napi);
410                 enable_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group);
411         }
412         return rx_count;
413 }
414
415 #ifdef CONFIG_NET_POLL_CONTROLLER
416 /**
417  * cvm_oct_poll_controller - poll for receive packets
418  * device.
419  *
420  * @dev:    Device to poll. Unused
421  */
422 void cvm_oct_poll_controller(struct net_device *dev)
423 {
424         cvm_oct_napi_poll(NULL, 16);
425 }
426 #endif
427
428 void cvm_oct_rx_initialize(void)
429 {
430         int i;
431         struct net_device *dev_for_napi = NULL;
432
433         for (i = 0; i < TOTAL_NUMBER_OF_PORTS; i++) {
434                 if (cvm_oct_device[i]) {
435                         dev_for_napi = cvm_oct_device[i];
436                         break;
437                 }
438         }
439
440         if (NULL == dev_for_napi)
441                 panic("No net_devices were allocated.");
442
443         netif_napi_add(dev_for_napi, &cvm_oct_napi, cvm_oct_napi_poll,
444                        rx_napi_weight);
445         napi_enable(&cvm_oct_napi);
446
447         /* Register an IRQ handler to receive POW interrupts */
448         i = request_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group,
449                         cvm_oct_do_interrupt, 0, "Ethernet", cvm_oct_device);
450
451         if (i)
452                 panic("Could not acquire Ethernet IRQ %d\n",
453                       OCTEON_IRQ_WORKQ0 + pow_receive_group);
454
455         disable_irq_nosync(OCTEON_IRQ_WORKQ0 + pow_receive_group);
456
457         /* Enable POW interrupt when our port has at least one packet */
458         if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
459                 union cvmx_sso_wq_int_thrx int_thr;
460                 union cvmx_pow_wq_int_pc int_pc;
461
462                 int_thr.u64 = 0;
463                 int_thr.s.tc_en = 1;
464                 int_thr.s.tc_thr = 1;
465                 cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(pow_receive_group),
466                                int_thr.u64);
467
468                 int_pc.u64 = 0;
469                 int_pc.s.pc_thr = 5;
470                 cvmx_write_csr(CVMX_SSO_WQ_INT_PC, int_pc.u64);
471         } else {
472                 union cvmx_pow_wq_int_thrx int_thr;
473                 union cvmx_pow_wq_int_pc int_pc;
474
475                 int_thr.u64 = 0;
476                 int_thr.s.tc_en = 1;
477                 int_thr.s.tc_thr = 1;
478                 cvmx_write_csr(CVMX_POW_WQ_INT_THRX(pow_receive_group),
479                                int_thr.u64);
480
481                 int_pc.u64 = 0;
482                 int_pc.s.pc_thr = 5;
483                 cvmx_write_csr(CVMX_POW_WQ_INT_PC, int_pc.u64);
484         }
485
486         /* Schedule NAPI now. This will indirectly enable the interrupt. */
487         napi_schedule(&cvm_oct_napi);
488 }
489
490 void cvm_oct_rx_shutdown(void)
491 {
492         netif_napi_del(&cvm_oct_napi);
493 }