1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/cache.h>
30#include <linux/netdevice.h>
31#include <linux/init.h>
32#include <linux/etherdevice.h>
33#include <linux/ip.h>
34#include <linux/string.h>
35#include <linux/prefetch.h>
36#include <linux/ethtool.h>
37#include <linux/mii.h>
38#include <linux/seq_file.h>
39#include <linux/proc_fs.h>
40#include <net/dst.h>
41#ifdef CONFIG_XFRM
42#include <linux/xfrm.h>
43#include <net/xfrm.h>
44#endif
45
46#include <asm/atomic.h>
47
48#include <asm/octeon/octeon.h>
49
50#include "ethernet-defines.h"
51#include "octeon-ethernet.h"
52#include "ethernet-mem.h"
53#include "ethernet-util.h"
54
55#include "cvmx-helper.h"
56#include "cvmx-wqe.h"
57#include "cvmx-fau.h"
58#include "cvmx-pow.h"
59#include "cvmx-pip.h"
60#include "cvmx-scratch.h"
61
62#include "cvmx-gmxx-defs.h"
63
64struct cvm_tasklet_wrapper {
65 struct tasklet_struct t;
66};
67
68
69
70
71
72
73
74static struct cvm_tasklet_wrapper cvm_oct_tasklet[NR_CPUS];
75
76
77
78
79
80
81
82
83
84
85irqreturn_t cvm_oct_do_interrupt(int cpl, void *dev_id)
86{
87
88 if (INTERRUPT_LIMIT)
89 cvmx_write_csr(CVMX_POW_WQ_INT, 1 << pow_receive_group);
90 else
91 cvmx_write_csr(CVMX_POW_WQ_INT, 0x10001 << pow_receive_group);
92 preempt_disable();
93 tasklet_schedule(&cvm_oct_tasklet[smp_processor_id()].t);
94 preempt_enable();
95 return IRQ_HANDLED;
96}
97
98#ifdef CONFIG_NET_POLL_CONTROLLER
99
100
101
102
103
104
105
106
107void cvm_oct_poll_controller(struct net_device *dev)
108{
109 preempt_disable();
110 tasklet_schedule(&cvm_oct_tasklet[smp_processor_id()].t);
111 preempt_enable();
112}
113#endif
114
115
116
117
118
119
120
121
122static inline int cvm_oct_check_rcv_error(cvmx_wqe_t *work)
123{
124 if ((work->word2.snoip.err_code == 10) && (work->len <= 64)) {
125
126
127
128
129
130
131 } else
132 if (USE_10MBPS_PREAMBLE_WORKAROUND
133 && ((work->word2.snoip.err_code == 5)
134 || (work->word2.snoip.err_code == 7))) {
135
136
137
138
139
140
141
142
143
144 int interface = cvmx_helper_get_interface_num(work->ipprt);
145 int index = cvmx_helper_get_interface_index_num(work->ipprt);
146 union cvmx_gmxx_rxx_frm_ctl gmxx_rxx_frm_ctl;
147 gmxx_rxx_frm_ctl.u64 =
148 cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL(index, interface));
149 if (gmxx_rxx_frm_ctl.s.pre_chk == 0) {
150
151 uint8_t *ptr =
152 cvmx_phys_to_ptr(work->packet_ptr.s.addr);
153 int i = 0;
154
155 while (i < work->len - 1) {
156 if (*ptr != 0x55)
157 break;
158 ptr++;
159 i++;
160 }
161
162 if (*ptr == 0xd5) {
163
164
165
166 work->packet_ptr.s.addr += i + 1;
167 work->len -= i + 5;
168 } else if ((*ptr & 0xf) == 0xd) {
169
170
171
172 work->packet_ptr.s.addr += i;
173 work->len -= i + 4;
174 for (i = 0; i < work->len; i++) {
175 *ptr =
176 ((*ptr & 0xf0) >> 4) |
177 ((*(ptr + 1) & 0xf) << 4);
178 ptr++;
179 }
180 } else {
181 DEBUGPRINT("Port %d unknown preamble, packet "
182 "dropped\n",
183 work->ipprt);
184
185
186
187 cvm_oct_free_work(work);
188 return 1;
189 }
190 }
191 } else {
192 DEBUGPRINT("Port %d receive error code %d, packet dropped\n",
193 work->ipprt, work->word2.snoip.err_code);
194 cvm_oct_free_work(work);
195 return 1;
196 }
197
198 return 0;
199}
200
201
202
203
204
205
206void cvm_oct_tasklet_rx(unsigned long unused)
207{
208 const int coreid = cvmx_get_core_num();
209 uint64_t old_group_mask;
210 uint64_t old_scratch;
211 int rx_count = 0;
212 int number_to_free;
213 int num_freed;
214 int packet_not_copied;
215
216
217 prefetch(cvm_oct_device);
218
219 if (USE_ASYNC_IOBDMA) {
220
221 CVMX_SYNCIOBDMA;
222 old_scratch = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
223 }
224
225
226 old_group_mask = cvmx_read_csr(CVMX_POW_PP_GRP_MSKX(coreid));
227 cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid),
228 (old_group_mask & ~0xFFFFull) | 1 << pow_receive_group);
229
230 if (USE_ASYNC_IOBDMA)
231 cvmx_pow_work_request_async(CVMX_SCR_SCRATCH, CVMX_POW_NO_WAIT);
232
233 while (1) {
234 struct sk_buff *skb = NULL;
235 int skb_in_hw;
236 cvmx_wqe_t *work;
237
238 if (USE_ASYNC_IOBDMA) {
239 work = cvmx_pow_work_response_async(CVMX_SCR_SCRATCH);
240 } else {
241 if ((INTERRUPT_LIMIT == 0)
242 || likely(rx_count < MAX_RX_PACKETS))
243 work =
244 cvmx_pow_work_request_sync
245 (CVMX_POW_NO_WAIT);
246 else
247 work = NULL;
248 }
249 prefetch(work);
250 if (work == NULL)
251 break;
252
253
254
255
256
257
258 if (USE_ASYNC_IOBDMA) {
259
260 if ((INTERRUPT_LIMIT == 0)
261 || likely(rx_count < MAX_RX_PACKETS))
262 cvmx_pow_work_request_async_nocheck
263 (CVMX_SCR_SCRATCH, CVMX_POW_NO_WAIT);
264 else {
265 cvmx_scratch_write64(CVMX_SCR_SCRATCH,
266 0x8000000000000000ull);
267 cvmx_pow_tag_sw_null_nocheck();
268 }
269 }
270
271 skb_in_hw = USE_SKBUFFS_IN_HW && work->word2.s.bufs == 1;
272 if (likely(skb_in_hw)) {
273 skb =
274 *(struct sk_buff
275 **)(cvm_oct_get_buffer_ptr(work->packet_ptr) -
276 sizeof(void *));
277 prefetch(&skb->head);
278 prefetch(&skb->len);
279 }
280 prefetch(cvm_oct_device[work->ipprt]);
281
282 rx_count++;
283
284 if (unlikely(work->word2.snoip.rcv_error)) {
285 if (cvm_oct_check_rcv_error(work))
286 continue;
287 }
288
289
290
291
292
293
294 if (likely(skb_in_hw)) {
295
296
297
298
299
300
301
302 skb->data =
303 skb->head + work->packet_ptr.s.addr -
304 cvmx_ptr_to_phys(skb->head);
305 prefetch(skb->data);
306 skb->len = work->len;
307 skb_set_tail_pointer(skb, skb->len);
308 packet_not_copied = 1;
309 } else {
310
311
312
313
314
315 skb = dev_alloc_skb(work->len);
316 if (!skb) {
317 DEBUGPRINT("Port %d failed to allocate "
318 "skbuff, packet dropped\n",
319 work->ipprt);
320 cvm_oct_free_work(work);
321 continue;
322 }
323
324
325
326
327
328
329 if (unlikely(work->word2.s.bufs == 0)) {
330 uint8_t *ptr = work->packet_data;
331
332 if (likely(!work->word2.s.not_IP)) {
333
334
335
336
337 if (work->word2.s.is_v6)
338 ptr += 2;
339 else
340 ptr += 6;
341 }
342 memcpy(skb_put(skb, work->len), ptr, work->len);
343
344 } else {
345 int segments = work->word2.s.bufs;
346 union cvmx_buf_ptr segment_ptr =
347 work->packet_ptr;
348 int len = work->len;
349
350 while (segments--) {
351 union cvmx_buf_ptr next_ptr =
352 *(union cvmx_buf_ptr *)
353 cvmx_phys_to_ptr(segment_ptr.s.
354 addr - 8);
355
356
357
358
359
360
361
362
363
364 int segment_size =
365 CVMX_FPA_PACKET_POOL_SIZE -
366 (segment_ptr.s.addr -
367 (((segment_ptr.s.addr >> 7) -
368 segment_ptr.s.back) << 7));
369
370
371 if (segment_size > len)
372 segment_size = len;
373
374 memcpy(skb_put(skb, segment_size),
375 cvmx_phys_to_ptr(segment_ptr.s.
376 addr),
377 segment_size);
378
379
380 len -= segment_size;
381 segment_ptr = next_ptr;
382 }
383 }
384 packet_not_copied = 0;
385 }
386
387 if (likely((work->ipprt < TOTAL_NUMBER_OF_PORTS) &&
388 cvm_oct_device[work->ipprt])) {
389 struct net_device *dev = cvm_oct_device[work->ipprt];
390 struct octeon_ethernet *priv = netdev_priv(dev);
391
392
393
394 if (likely(dev->flags & IFF_UP)) {
395 skb->protocol = eth_type_trans(skb, dev);
396 skb->dev = dev;
397
398 if (unlikely
399 (work->word2.s.not_IP
400 || work->word2.s.IP_exc
401 || work->word2.s.L4_error))
402 skb->ip_summed = CHECKSUM_NONE;
403 else
404 skb->ip_summed = CHECKSUM_UNNECESSARY;
405
406
407 if (work->ipprt >= CVMX_PIP_NUM_INPUT_PORTS) {
408#ifdef CONFIG_64BIT
409 atomic64_add(1, (atomic64_t *)&priv->stats.rx_packets);
410 atomic64_add(skb->len, (atomic64_t *)&priv->stats.rx_bytes);
411#else
412 atomic_add(1, (atomic_t *)&priv->stats.rx_packets);
413 atomic_add(skb->len, (atomic_t *)&priv->stats.rx_bytes);
414#endif
415 }
416 netif_receive_skb(skb);
417 } else {
418
419
420
421
422
423
424
425
426#ifdef CONFIG_64BIT
427 atomic64_add(1, (atomic64_t *)&priv->stats.rx_dropped);
428#else
429 atomic_add(1, (atomic_t *)&priv->stats.rx_dropped);
430#endif
431 dev_kfree_skb_irq(skb);
432 }
433 } else {
434
435
436
437
438 DEBUGPRINT("Port %d not controlled by Linux, packet "
439 "dropped\n",
440 work->ipprt);
441 dev_kfree_skb_irq(skb);
442 }
443
444
445
446
447 if (USE_SKBUFFS_IN_HW && likely(packet_not_copied)) {
448
449
450
451
452
453 cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE,
454 1);
455
456 cvmx_fpa_free(work, CVMX_FPA_WQE_POOL,
457 DONT_WRITEBACK(1));
458 } else {
459 cvm_oct_free_work(work);
460 }
461 }
462
463
464 cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid), old_group_mask);
465 if (USE_ASYNC_IOBDMA) {
466
467 cvmx_scratch_write64(CVMX_SCR_SCRATCH, old_scratch);
468 }
469
470 if (USE_SKBUFFS_IN_HW) {
471
472 number_to_free =
473 cvmx_fau_fetch_and_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE, 0);
474
475 if (number_to_free > 0) {
476 cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE,
477 -number_to_free);
478 num_freed =
479 cvm_oct_mem_fill_fpa(CVMX_FPA_PACKET_POOL,
480 CVMX_FPA_PACKET_POOL_SIZE,
481 number_to_free);
482 if (num_freed != number_to_free) {
483 cvmx_fau_atomic_add32
484 (FAU_NUM_PACKET_BUFFERS_TO_FREE,
485 number_to_free - num_freed);
486 }
487 }
488 }
489}
490
491void cvm_oct_rx_initialize(void)
492{
493 int i;
494
495 for (i = 0; i < NR_CPUS; i++)
496 tasklet_init(&cvm_oct_tasklet[i].t, cvm_oct_tasklet_rx, 0);
497}
498
499void cvm_oct_rx_shutdown(void)
500{
501 int i;
502
503 for (i = 0; i < NR_CPUS; i++)
504 tasklet_kill(&cvm_oct_tasklet[i].t);
505}
506