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