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