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