Merge branch 'irq-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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                         union cvmx_pow_wq_int wq_int;
199
200                         wq_int.u64 = 0;
201                         wq_int.s.iq_dis = 1 << pow_receive_group;
202                         wq_int.s.wq_int = 1 << pow_receive_group;
203                         cvmx_write_csr(CVMX_POW_WQ_INT, wq_int.u64);
204                         break;
205                 }
206                 pskb = (struct sk_buff **)(cvm_oct_get_buffer_ptr(work->packet_ptr) -
207                         sizeof(void *));
208                 prefetch(pskb);
209
210                 if (USE_ASYNC_IOBDMA && rx_count < (budget - 1)) {
211                         cvmx_pow_work_request_async_nocheck(CVMX_SCR_SCRATCH,
212                                                             CVMX_POW_NO_WAIT);
213                         did_work_request = 1;
214                 }
215                 rx_count++;
216
217                 skb_in_hw = work->word2.s.bufs == 1;
218                 if (likely(skb_in_hw)) {
219                         skb = *pskb;
220                         prefetch(&skb->head);
221                         prefetch(&skb->len);
222                 }
223                 prefetch(cvm_oct_device[work->ipprt]);
224
225                 /* Immediately throw away all packets with receive errors */
226                 if (unlikely(work->word2.snoip.rcv_error)) {
227                         if (cvm_oct_check_rcv_error(work))
228                                 continue;
229                 }
230
231                 /*
232                  * We can only use the zero copy path if skbuffs are
233                  * in the FPA pool and the packet fits in a single
234                  * buffer.
235                  */
236                 if (likely(skb_in_hw)) {
237                         skb->data = skb->head + work->packet_ptr.s.addr -
238                                 cvmx_ptr_to_phys(skb->head);
239                         prefetch(skb->data);
240                         skb->len = work->len;
241                         skb_set_tail_pointer(skb, skb->len);
242                         packet_not_copied = 1;
243                 } else {
244                         /*
245                          * We have to copy the packet. First allocate
246                          * an skbuff for it.
247                          */
248                         skb = dev_alloc_skb(work->len);
249                         if (!skb) {
250                                 cvm_oct_free_work(work);
251                                 continue;
252                         }
253
254                         /*
255                          * Check if we've received a packet that was
256                          * entirely stored in the work entry.
257                          */
258                         if (unlikely(work->word2.s.bufs == 0)) {
259                                 uint8_t *ptr = work->packet_data;
260
261                                 if (likely(!work->word2.s.not_IP)) {
262                                         /*
263                                          * The beginning of the packet
264                                          * moves for IP packets.
265                                          */
266                                         if (work->word2.s.is_v6)
267                                                 ptr += 2;
268                                         else
269                                                 ptr += 6;
270                                 }
271                                 memcpy(skb_put(skb, work->len), ptr, work->len);
272                                 /* No packet buffers to free */
273                         } else {
274                                 int segments = work->word2.s.bufs;
275                                 union cvmx_buf_ptr segment_ptr =
276                                     work->packet_ptr;
277                                 int len = work->len;
278
279                                 while (segments--) {
280                                         union cvmx_buf_ptr next_ptr =
281                                             *(union cvmx_buf_ptr *)cvmx_phys_to_ptr(segment_ptr.s.addr - 8);
282
283                         /*
284                          * Octeon Errata PKI-100: The segment size is
285                          * wrong. Until it is fixed, calculate the
286                          * segment size based on the packet pool
287                          * buffer size. When it is fixed, the
288                          * following line should be replaced with this
289                          * one: int segment_size =
290                          * segment_ptr.s.size;
291                          */
292                                         int segment_size =
293                                             CVMX_FPA_PACKET_POOL_SIZE -
294                                             (segment_ptr.s.addr -
295                                              (((segment_ptr.s.addr >> 7) -
296                                                segment_ptr.s.back) << 7));
297                                         /*
298                                          * Don't copy more than what
299                                          * is left in the packet.
300                                          */
301                                         if (segment_size > len)
302                                                 segment_size = len;
303                                         /* Copy the data into the packet */
304                                         memcpy(skb_put(skb, segment_size),
305                                                cvmx_phys_to_ptr(segment_ptr.s.addr),
306                                                segment_size);
307                                         len -= segment_size;
308                                         segment_ptr = next_ptr;
309                                 }
310                         }
311                         packet_not_copied = 0;
312                 }
313
314                 if (likely((work->ipprt < TOTAL_NUMBER_OF_PORTS) &&
315                            cvm_oct_device[work->ipprt])) {
316                         struct net_device *dev = cvm_oct_device[work->ipprt];
317                         struct octeon_ethernet *priv = netdev_priv(dev);
318
319                         /*
320                          * Only accept packets for devices that are
321                          * currently up.
322                          */
323                         if (likely(dev->flags & IFF_UP)) {
324                                 skb->protocol = eth_type_trans(skb, dev);
325                                 skb->dev = dev;
326
327                                 if (unlikely(work->word2.s.not_IP ||
328                                              work->word2.s.IP_exc ||
329                                              work->word2.s.L4_error ||
330                                              !work->word2.s.tcp_or_udp))
331                                         skb->ip_summed = CHECKSUM_NONE;
332                                 else
333                                         skb->ip_summed = CHECKSUM_UNNECESSARY;
334
335                                 /* Increment RX stats for virtual ports */
336                                 if (work->ipprt >= CVMX_PIP_NUM_INPUT_PORTS) {
337 #ifdef CONFIG_64BIT
338                                         atomic64_add(1,
339                                                      (atomic64_t *)&priv->stats.rx_packets);
340                                         atomic64_add(skb->len,
341                                                      (atomic64_t *)&priv->stats.rx_bytes);
342 #else
343                                         atomic_add(1,
344                                                    (atomic_t *)&priv->stats.rx_packets);
345                                         atomic_add(skb->len,
346                                                    (atomic_t *)&priv->stats.rx_bytes);
347 #endif
348                                 }
349                                 netif_receive_skb(skb);
350                         } else {
351                                 /* Drop any packet received for a device that isn't up */
352                                 /*
353                                   printk_ratelimited("%s: Device not up, packet dropped\n",
354                                            dev->name);
355                                 */
356 #ifdef CONFIG_64BIT
357                                 atomic64_add(1,
358                                              (atomic64_t *)&priv->stats.rx_dropped);
359 #else
360                                 atomic_add(1,
361                                            (atomic_t *)&priv->stats.rx_dropped);
362 #endif
363                                 dev_kfree_skb_irq(skb);
364                         }
365                 } else {
366                         /*
367                          * Drop any packet received for a device that
368                          * doesn't exist.
369                          */
370                         printk_ratelimited("Port %d not controlled by Linux, packet dropped\n",
371                                    work->ipprt);
372                         dev_kfree_skb_irq(skb);
373                 }
374                 /*
375                  * Check to see if the skbuff and work share the same
376                  * packet buffer.
377                  */
378                 if (likely(packet_not_copied)) {
379                         /*
380                          * This buffer needs to be replaced, increment
381                          * the number of buffers we need to free by
382                          * one.
383                          */
384                         cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE,
385                                               1);
386
387                         cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, 1);
388                 } else {
389                         cvm_oct_free_work(work);
390                 }
391         }
392         /* Restore the original POW group mask */
393         cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid), old_group_mask);
394         if (USE_ASYNC_IOBDMA) {
395                 /* Restore the scratch area */
396                 cvmx_scratch_write64(CVMX_SCR_SCRATCH, old_scratch);
397         }
398         cvm_oct_rx_refill_pool(0);
399
400         if (rx_count < budget && napi != NULL) {
401                 /* No more work */
402                 napi_complete(napi);
403                 enable_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group);
404         }
405         return rx_count;
406 }
407
408 #ifdef CONFIG_NET_POLL_CONTROLLER
409 /**
410  * cvm_oct_poll_controller - poll for receive packets
411  * device.
412  *
413  * @dev:    Device to poll. Unused
414  */
415 void cvm_oct_poll_controller(struct net_device *dev)
416 {
417         cvm_oct_napi_poll(NULL, 16);
418 }
419 #endif
420
421 void cvm_oct_rx_initialize(void)
422 {
423         int i;
424         struct net_device *dev_for_napi = NULL;
425         union cvmx_pow_wq_int_thrx int_thr;
426         union cvmx_pow_wq_int_pc int_pc;
427
428         for (i = 0; i < TOTAL_NUMBER_OF_PORTS; i++) {
429                 if (cvm_oct_device[i]) {
430                         dev_for_napi = cvm_oct_device[i];
431                         break;
432                 }
433         }
434
435         if (NULL == dev_for_napi)
436                 panic("No net_devices were allocated.");
437
438         netif_napi_add(dev_for_napi, &cvm_oct_napi, cvm_oct_napi_poll,
439                        rx_napi_weight);
440         napi_enable(&cvm_oct_napi);
441
442         /* Register an IRQ handler to receive POW interrupts */
443         i = request_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group,
444                         cvm_oct_do_interrupt, 0, "Ethernet", cvm_oct_device);
445
446         if (i)
447                 panic("Could not acquire Ethernet IRQ %d\n",
448                       OCTEON_IRQ_WORKQ0 + pow_receive_group);
449
450         disable_irq_nosync(OCTEON_IRQ_WORKQ0 + pow_receive_group);
451
452         int_thr.u64 = 0;
453         int_thr.s.tc_en = 1;
454         int_thr.s.tc_thr = 1;
455         /* Enable POW interrupt when our port has at least one packet */
456         cvmx_write_csr(CVMX_POW_WQ_INT_THRX(pow_receive_group), int_thr.u64);
457
458         int_pc.u64 = 0;
459         int_pc.s.pc_thr = 5;
460         cvmx_write_csr(CVMX_POW_WQ_INT_PC, int_pc.u64);
461
462         /* Schedule NAPI now. This will indirectly enable the interrupt. */
463         napi_schedule(&cvm_oct_napi);
464 }
465
466 void cvm_oct_rx_shutdown(void)
467 {
468         netif_napi_del(&cvm_oct_napi);
469 }