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