staging: octeon: set up pknd for all interfaces
[cascardo/linux.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 <asm/octeon/octeon.h>
30
31 #include "ethernet-defines.h"
32 #include "ethernet-mem.h"
33 #include "ethernet-rx.h"
34 #include "octeon-ethernet.h"
35 #include "ethernet-util.h"
36
37 #include <asm/octeon/cvmx-helper.h>
38 #include <asm/octeon/cvmx-wqe.h>
39 #include <asm/octeon/cvmx-fau.h>
40 #include <asm/octeon/cvmx-pow.h>
41 #include <asm/octeon/cvmx-pip.h>
42 #include <asm/octeon/cvmx-scratch.h>
43
44 #include <asm/octeon/cvmx-gmxx-defs.h>
45
46 static struct oct_rx_group {
47         int irq;
48         int group;
49         struct napi_struct napi;
50 } oct_rx_group[16];
51
52 /**
53  * cvm_oct_do_interrupt - interrupt handler.
54  * @irq: Interrupt number.
55  * @napi_id: Cookie to identify the NAPI instance.
56  *
57  * The interrupt occurs whenever the POW has packets in our group.
58  *
59  */
60 static irqreturn_t cvm_oct_do_interrupt(int irq, void *napi_id)
61 {
62         /* Disable the IRQ and start napi_poll. */
63         disable_irq_nosync(irq);
64         napi_schedule(napi_id);
65
66         return IRQ_HANDLED;
67 }
68
69 /**
70  * cvm_oct_check_rcv_error - process receive errors
71  * @work: Work queue entry pointing to the packet.
72  *
73  * Returns Non-zero if the packet can be dropped, zero otherwise.
74  */
75 static inline int cvm_oct_check_rcv_error(cvmx_wqe_t *work)
76 {
77         int port;
78
79         if (octeon_has_feature(OCTEON_FEATURE_PKND))
80                 port = work->word0.pip.cn68xx.pknd;
81         else
82                 port = work->word1.cn38xx.ipprt;
83
84         if ((work->word2.snoip.err_code == 10) && (work->word1.len <= 64)) {
85                 /*
86                  * Ignore length errors on min size packets. Some
87                  * equipment incorrectly pads packets to 64+4FCS
88                  * instead of 60+4FCS.  Note these packets still get
89                  * counted as frame errors.
90                  */
91         } else if (work->word2.snoip.err_code == 5 ||
92                    work->word2.snoip.err_code == 7) {
93                 /*
94                  * We received a packet with either an alignment error
95                  * or a FCS error. This may be signalling that we are
96                  * running 10Mbps with GMXX_RXX_FRM_CTL[PRE_CHK]
97                  * off. If this is the case we need to parse the
98                  * packet to determine if we can remove a non spec
99                  * preamble and generate a correct packet.
100                  */
101                 int interface = cvmx_helper_get_interface_num(port);
102                 int index = cvmx_helper_get_interface_index_num(port);
103                 union cvmx_gmxx_rxx_frm_ctl gmxx_rxx_frm_ctl;
104
105                 gmxx_rxx_frm_ctl.u64 =
106                     cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL(index, interface));
107                 if (gmxx_rxx_frm_ctl.s.pre_chk == 0) {
108                         u8 *ptr =
109                             cvmx_phys_to_ptr(work->packet_ptr.s.addr);
110                         int i = 0;
111
112                         while (i < work->word1.len - 1) {
113                                 if (*ptr != 0x55)
114                                         break;
115                                 ptr++;
116                                 i++;
117                         }
118
119                         if (*ptr == 0xd5) {
120                                 /* Port received 0xd5 preamble */
121                                 work->packet_ptr.s.addr += i + 1;
122                                 work->word1.len -= i + 5;
123                         } else if ((*ptr & 0xf) == 0xd) {
124                                 /* Port received 0xd preamble */
125                                 work->packet_ptr.s.addr += i;
126                                 work->word1.len -= i + 4;
127                                 for (i = 0; i < work->word1.len; i++) {
128                                         *ptr =
129                                             ((*ptr & 0xf0) >> 4) |
130                                             ((*(ptr + 1) & 0xf) << 4);
131                                         ptr++;
132                                 }
133                         } else {
134                                 printk_ratelimited("Port %d unknown preamble, packet dropped\n",
135                                                    port);
136                                 cvm_oct_free_work(work);
137                                 return 1;
138                         }
139                 }
140         } else {
141                 printk_ratelimited("Port %d receive error code %d, packet dropped\n",
142                                    port, work->word2.snoip.err_code);
143                 cvm_oct_free_work(work);
144                 return 1;
145         }
146
147         return 0;
148 }
149
150 static int cvm_oct_poll(struct oct_rx_group *rx_group, int budget)
151 {
152         const int       coreid = cvmx_get_core_num();
153         u64     old_group_mask;
154         u64     old_scratch;
155         int             rx_count = 0;
156         int             did_work_request = 0;
157         int             packet_not_copied;
158
159         /* Prefetch cvm_oct_device since we know we need it soon */
160         prefetch(cvm_oct_device);
161
162         if (USE_ASYNC_IOBDMA) {
163                 /* Save scratch in case userspace is using it */
164                 CVMX_SYNCIOBDMA;
165                 old_scratch = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
166         }
167
168         /* Only allow work for our group (and preserve priorities) */
169         if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
170                 old_group_mask = cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid));
171                 cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid),
172                                BIT(rx_group->group));
173                 cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid)); /* Flush */
174         } else {
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) |
178                                BIT(rx_group->group));
179         }
180
181         if (USE_ASYNC_IOBDMA) {
182                 cvmx_pow_work_request_async(CVMX_SCR_SCRATCH, CVMX_POW_NO_WAIT);
183                 did_work_request = 1;
184         }
185
186         while (rx_count < budget) {
187                 struct sk_buff *skb = NULL;
188                 struct sk_buff **pskb = NULL;
189                 int skb_in_hw;
190                 cvmx_wqe_t *work;
191                 int port;
192
193                 if (USE_ASYNC_IOBDMA && did_work_request)
194                         work = cvmx_pow_work_response_async(CVMX_SCR_SCRATCH);
195                 else
196                         work = cvmx_pow_work_request_sync(CVMX_POW_NO_WAIT);
197
198                 prefetch(work);
199                 did_work_request = 0;
200                 if (!work) {
201                         if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
202                                 cvmx_write_csr(CVMX_SSO_WQ_IQ_DIS,
203                                                BIT(rx_group->group));
204                                 cvmx_write_csr(CVMX_SSO_WQ_INT,
205                                                BIT(rx_group->group));
206                         } else {
207                                 union cvmx_pow_wq_int wq_int;
208
209                                 wq_int.u64 = 0;
210                                 wq_int.s.iq_dis = BIT(rx_group->group);
211                                 wq_int.s.wq_int = BIT(rx_group->group);
212                                 cvmx_write_csr(CVMX_POW_WQ_INT, wq_int.u64);
213                         }
214                         break;
215                 }
216                 pskb = (struct sk_buff **)
217                         (cvm_oct_get_buffer_ptr(work->packet_ptr) -
218                         sizeof(void *));
219                 prefetch(pskb);
220
221                 if (USE_ASYNC_IOBDMA && rx_count < (budget - 1)) {
222                         cvmx_pow_work_request_async_nocheck(CVMX_SCR_SCRATCH,
223                                                             CVMX_POW_NO_WAIT);
224                         did_work_request = 1;
225                 }
226                 rx_count++;
227
228                 skb_in_hw = work->word2.s.bufs == 1;
229                 if (likely(skb_in_hw)) {
230                         skb = *pskb;
231                         prefetch(&skb->head);
232                         prefetch(&skb->len);
233                 }
234
235                 if (octeon_has_feature(OCTEON_FEATURE_PKND))
236                         port = work->word0.pip.cn68xx.pknd;
237                 else
238                         port = work->word1.cn38xx.ipprt;
239
240                 prefetch(cvm_oct_device[port]);
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->word1.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->word1.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                                 u8 *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->word1.len), ptr,
289                                        work->word1.len);
290                                 /* No packet buffers to free */
291                         } else {
292                                 int segments = work->word2.s.bufs;
293                                 union cvmx_buf_ptr segment_ptr =
294                                     work->packet_ptr;
295                                 int len = work->word1.len;
296
297                                 while (segments--) {
298                                         union cvmx_buf_ptr next_ptr =
299                                             *(union cvmx_buf_ptr *)
300                                               cvmx_phys_to_ptr(
301                                               segment_ptr.s.addr - 8);
302
303                         /*
304                          * Octeon Errata PKI-100: The segment size is
305                          * wrong. Until it is fixed, calculate the
306                          * segment size based on the packet pool
307                          * buffer size. When it is fixed, the
308                          * following line should be replaced with this
309                          * one: int segment_size =
310                          * segment_ptr.s.size;
311                          */
312                                         int segment_size =
313                                             CVMX_FPA_PACKET_POOL_SIZE -
314                                             (segment_ptr.s.addr -
315                                              (((segment_ptr.s.addr >> 7) -
316                                                segment_ptr.s.back) << 7));
317                                         /*
318                                          * Don't copy more than what
319                                          * is left in the packet.
320                                          */
321                                         if (segment_size > len)
322                                                 segment_size = len;
323                                         /* Copy the data into the packet */
324                                         memcpy(skb_put(skb, segment_size),
325                                                cvmx_phys_to_ptr(
326                                                segment_ptr.s.addr),
327                                                segment_size);
328                                         len -= segment_size;
329                                         segment_ptr = next_ptr;
330                                 }
331                         }
332                         packet_not_copied = 0;
333                 }
334                 if (likely((port < TOTAL_NUMBER_OF_PORTS) &&
335                            cvm_oct_device[port])) {
336                         struct net_device *dev = cvm_oct_device[port];
337                         struct octeon_ethernet *priv = netdev_priv(dev);
338
339                         /*
340                          * Only accept packets for devices that are
341                          * currently up.
342                          */
343                         if (likely(dev->flags & IFF_UP)) {
344                                 skb->protocol = eth_type_trans(skb, dev);
345                                 skb->dev = dev;
346
347                                 if (unlikely(work->word2.s.not_IP ||
348                                              work->word2.s.IP_exc ||
349                                              work->word2.s.L4_error ||
350                                              !work->word2.s.tcp_or_udp))
351                                         skb->ip_summed = CHECKSUM_NONE;
352                                 else
353                                         skb->ip_summed = CHECKSUM_UNNECESSARY;
354
355                                 /* Increment RX stats for virtual ports */
356                                 if (port >= CVMX_PIP_NUM_INPUT_PORTS) {
357                                         priv->stats.rx_packets++;
358                                         priv->stats.rx_bytes += skb->len;
359                                 }
360                                 netif_receive_skb(skb);
361                         } else {
362                                 /*
363                                  * Drop any packet received for a device that
364                                  * isn't up.
365                                  */
366                                 priv->stats.rx_dropped++;
367                                 dev_kfree_skb_irq(skb);
368                         }
369                 } else {
370                         /*
371                          * Drop any packet received for a device that
372                          * doesn't exist.
373                          */
374                         printk_ratelimited("Port %d not controlled by Linux, packet dropped\n",
375                                            port);
376                         dev_kfree_skb_irq(skb);
377                 }
378                 /*
379                  * Check to see if the skbuff and work share the same
380                  * packet buffer.
381                  */
382                 if (likely(packet_not_copied)) {
383                         /*
384                          * This buffer needs to be replaced, increment
385                          * the number of buffers we need to free by
386                          * one.
387                          */
388                         cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE,
389                                               1);
390
391                         cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, 1);
392                 } else {
393                         cvm_oct_free_work(work);
394                 }
395         }
396         /* Restore the original POW group mask */
397         if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
398                 cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid), old_group_mask);
399                 cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid)); /* Flush */
400         } else {
401                 cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid), old_group_mask);
402         }
403
404         if (USE_ASYNC_IOBDMA) {
405                 /* Restore the scratch area */
406                 cvmx_scratch_write64(CVMX_SCR_SCRATCH, old_scratch);
407         }
408         cvm_oct_rx_refill_pool(0);
409
410         return rx_count;
411 }
412
413 /**
414  * cvm_oct_napi_poll - the NAPI poll function.
415  * @napi: The NAPI instance.
416  * @budget: Maximum number of packets to receive.
417  *
418  * Returns the number of packets processed.
419  */
420 static int cvm_oct_napi_poll(struct napi_struct *napi, int budget)
421 {
422         struct oct_rx_group *rx_group = container_of(napi, struct oct_rx_group,
423                                                      napi);
424         int rx_count;
425
426         rx_count = cvm_oct_poll(rx_group, budget);
427
428         if (rx_count < budget) {
429                 /* No more work */
430                 napi_complete(napi);
431                 enable_irq(rx_group->irq);
432         }
433         return rx_count;
434 }
435
436 #ifdef CONFIG_NET_POLL_CONTROLLER
437 /**
438  * cvm_oct_poll_controller - poll for receive packets
439  * device.
440  *
441  * @dev:    Device to poll. Unused
442  */
443 void cvm_oct_poll_controller(struct net_device *dev)
444 {
445         int i;
446
447         for (i = 0; i < ARRAY_SIZE(oct_rx_group); i++) {
448
449                 if (!(pow_receive_groups & BIT(i)))
450                         continue;
451
452                 cvm_oct_poll(&oct_rx_group[i], 16);
453
454         }
455 }
456 #endif
457
458 void cvm_oct_rx_initialize(void)
459 {
460         int i;
461         struct net_device *dev_for_napi = NULL;
462
463         for (i = 0; i < TOTAL_NUMBER_OF_PORTS; i++) {
464                 if (cvm_oct_device[i]) {
465                         dev_for_napi = cvm_oct_device[i];
466                         break;
467                 }
468         }
469
470         if (!dev_for_napi)
471                 panic("No net_devices were allocated.");
472
473         for (i = 0; i < ARRAY_SIZE(oct_rx_group); i++) {
474                 int ret;
475
476                 if (!(pow_receive_groups & BIT(i)))
477                         continue;
478
479                 netif_napi_add(dev_for_napi, &oct_rx_group[i].napi,
480                                cvm_oct_napi_poll, rx_napi_weight);
481                 napi_enable(&oct_rx_group[i].napi);
482
483                 oct_rx_group[i].irq = OCTEON_IRQ_WORKQ0 + i;
484                 oct_rx_group[i].group = i;
485
486                 /* Register an IRQ handler to receive POW interrupts */
487                 ret = request_irq(oct_rx_group[i].irq, cvm_oct_do_interrupt, 0,
488                                   "Ethernet", &oct_rx_group[i].napi);
489                 if (ret)
490                         panic("Could not acquire Ethernet IRQ %d\n",
491                               oct_rx_group[i].irq);
492
493                 disable_irq_nosync(oct_rx_group[i].irq);
494
495                 /* Enable POW interrupt when our port has at least one packet */
496                 if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
497                         union cvmx_sso_wq_int_thrx int_thr;
498                         union cvmx_pow_wq_int_pc int_pc;
499
500                         int_thr.u64 = 0;
501                         int_thr.s.tc_en = 1;
502                         int_thr.s.tc_thr = 1;
503                         cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(i), int_thr.u64);
504
505                         int_pc.u64 = 0;
506                         int_pc.s.pc_thr = 5;
507                         cvmx_write_csr(CVMX_SSO_WQ_INT_PC, int_pc.u64);
508                 } else {
509                         union cvmx_pow_wq_int_thrx int_thr;
510                         union cvmx_pow_wq_int_pc int_pc;
511
512                         int_thr.u64 = 0;
513                         int_thr.s.tc_en = 1;
514                         int_thr.s.tc_thr = 1;
515                         cvmx_write_csr(CVMX_POW_WQ_INT_THRX(i), int_thr.u64);
516
517                         int_pc.u64 = 0;
518                         int_pc.s.pc_thr = 5;
519                         cvmx_write_csr(CVMX_POW_WQ_INT_PC, int_pc.u64);
520                 }
521
522                 /* Schedule NAPI now. This will indirectly enable the
523                  * interrupt.
524                  */
525                 napi_schedule(&oct_rx_group[i].napi);
526         }
527 }
528
529 void cvm_oct_rx_shutdown(void)
530 {
531         int i;
532
533         for (i = 0; i < ARRAY_SIZE(oct_rx_group); i++) {
534
535                 if (!(pow_receive_groups & BIT(i)))
536                         continue;
537
538                 /* Disable POW interrupt */
539                 if (OCTEON_IS_MODEL(OCTEON_CN68XX))
540                         cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(i), 0);
541                 else
542                         cvmx_write_csr(CVMX_POW_WQ_INT_THRX(i), 0);
543
544                 /* Free the interrupt handler */
545                 free_irq(oct_rx_group[i].irq, cvm_oct_device);
546
547                 netif_napi_del(&oct_rx_group[i].napi);
548         }
549 }