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