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/netdevice.h>
30#include <linux/init.h>
31#include <linux/etherdevice.h>
32#include <linux/ip.h>
33#include <linux/string.h>
34#include <linux/ethtool.h>
35#include <linux/mii.h>
36#include <linux/seq_file.h>
37#include <linux/proc_fs.h>
38#include <net/dst.h>
39#ifdef CONFIG_XFRM
40#include <linux/xfrm.h>
41#include <net/xfrm.h>
42#endif
43
44#include <asm/atomic.h>
45
46#include <asm/octeon/octeon.h>
47
48#include "ethernet-defines.h"
49#include "octeon-ethernet.h"
50#include "ethernet-tx.h"
51#include "ethernet-util.h"
52
53#include "cvmx-wqe.h"
54#include "cvmx-fau.h"
55#include "cvmx-pko.h"
56#include "cvmx-helper.h"
57
58#include "cvmx-gmxx-defs.h"
59
60
61
62
63
64
65
66
67#ifndef GET_SKBUFF_QOS
68#define GET_SKBUFF_QOS(skb) 0
69#endif
70
71
72
73
74
75
76
77
78int cvm_oct_xmit(struct sk_buff *skb, struct net_device *dev)
79{
80 cvmx_pko_command_word0_t pko_command;
81 union cvmx_buf_ptr hw_buffer;
82 uint64_t old_scratch;
83 uint64_t old_scratch2;
84 int dropped;
85 int qos;
86 int queue_it_up;
87 struct octeon_ethernet *priv = netdev_priv(dev);
88 int32_t skb_to_free;
89 int32_t undo;
90 int32_t buffers_to_free;
91#if REUSE_SKBUFFS_WITHOUT_FREE
92 unsigned char *fpa_head;
93#endif
94
95
96
97
98
99 prefetch(priv);
100
101
102 dropped = 0;
103
104
105
106
107
108
109 if ((CVMX_PKO_QUEUES_PER_PORT_INTERFACE0 > 1) ||
110 (CVMX_PKO_QUEUES_PER_PORT_INTERFACE1 > 1)) {
111 qos = GET_SKBUFF_QOS(skb);
112 if (qos <= 0)
113 qos = 0;
114 else if (qos >= cvmx_pko_get_num_queues(priv->port))
115 qos = 0;
116 } else
117 qos = 0;
118
119 if (USE_ASYNC_IOBDMA) {
120
121 CVMX_SYNCIOBDMA;
122 old_scratch = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
123 old_scratch2 = cvmx_scratch_read64(CVMX_SCR_SCRATCH + 8);
124
125
126
127
128
129 cvmx_fau_async_fetch_and_add32(CVMX_SCR_SCRATCH + 8,
130 FAU_NUM_PACKET_BUFFERS_TO_FREE,
131 0);
132 cvmx_fau_async_fetch_and_add32(CVMX_SCR_SCRATCH,
133 priv->fau + qos * 4,
134 MAX_SKB_TO_FREE);
135 }
136
137
138
139
140
141
142
143
144
145
146 if ((skb->len < 64) && OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
147 union cvmx_gmxx_prtx_cfg gmx_prt_cfg;
148 int interface = INTERFACE(priv->port);
149 int index = INDEX(priv->port);
150
151 if (interface < 2) {
152
153 gmx_prt_cfg.u64 =
154 cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
155 if (gmx_prt_cfg.s.duplex == 0) {
156 int add_bytes = 64 - skb->len;
157 if ((skb_tail_pointer(skb) + add_bytes) <=
158 skb_end_pointer(skb))
159 memset(__skb_put(skb, add_bytes), 0,
160 add_bytes);
161 }
162 }
163 }
164
165
166 hw_buffer.u64 = 0;
167 hw_buffer.s.addr = cvmx_ptr_to_phys(skb->data);
168 hw_buffer.s.pool = 0;
169 hw_buffer.s.size =
170 (unsigned long)skb_end_pointer(skb) - (unsigned long)skb->head;
171
172
173 pko_command.u64 = 0;
174 pko_command.s.n2 = 1;
175 pko_command.s.segs = 1;
176 pko_command.s.total_bytes = skb->len;
177 pko_command.s.size0 = CVMX_FAU_OP_SIZE_32;
178 pko_command.s.subone0 = 1;
179
180 pko_command.s.dontfree = 1;
181 pko_command.s.reg0 = priv->fau + qos * 4;
182
183
184
185
186
187
188
189
190
191
192#if REUSE_SKBUFFS_WITHOUT_FREE
193 fpa_head = skb->head + 128 - ((unsigned long)skb->head & 0x7f);
194 if (unlikely(skb->data < fpa_head)) {
195
196
197
198
199 goto dont_put_skbuff_in_hw;
200 }
201 if (unlikely
202 ((skb_end_pointer(skb) - fpa_head) < CVMX_FPA_PACKET_POOL_SIZE)) {
203
204
205
206 goto dont_put_skbuff_in_hw;
207 }
208 if (unlikely(skb_shared(skb))) {
209
210
211
212 goto dont_put_skbuff_in_hw;
213 }
214 if (unlikely(skb_cloned(skb))) {
215
216
217
218 goto dont_put_skbuff_in_hw;
219 }
220 if (unlikely(skb_header_cloned(skb))) {
221
222
223
224 goto dont_put_skbuff_in_hw;
225 }
226 if (unlikely(skb->destructor)) {
227
228
229
230 goto dont_put_skbuff_in_hw;
231 }
232 if (unlikely(skb_shinfo(skb)->nr_frags)) {
233
234
235
236 goto dont_put_skbuff_in_hw;
237 }
238 if (unlikely
239 (skb->truesize !=
240 sizeof(*skb) + skb_end_pointer(skb) - skb->head)) {
241
242
243
244 goto dont_put_skbuff_in_hw;
245 }
246
247
248
249
250
251 pko_command.s.reg0 = 0;
252 pko_command.s.dontfree = 0;
253
254 hw_buffer.s.back = (skb->data - fpa_head) >> 7;
255 *(struct sk_buff **)(fpa_head - sizeof(void *)) = skb;
256
257
258
259
260
261 dst_release(skb_dst(skb));
262 skb_dst_set(skb, NULL);
263#ifdef CONFIG_XFRM
264 secpath_put(skb->sp);
265 skb->sp = NULL;
266#endif
267 nf_reset(skb);
268
269#ifdef CONFIG_NET_SCHED
270 skb->tc_index = 0;
271#ifdef CONFIG_NET_CLS_ACT
272 skb->tc_verd = 0;
273#endif
274#endif
275
276dont_put_skbuff_in_hw:
277#endif
278
279
280 if (USE_HW_TCPUDP_CHECKSUM && (skb->protocol == htons(ETH_P_IP)) &&
281 (ip_hdr(skb)->version == 4) && (ip_hdr(skb)->ihl == 5) &&
282 ((ip_hdr(skb)->frag_off == 0) || (ip_hdr(skb)->frag_off == 1 << 14))
283 && ((ip_hdr(skb)->protocol == IP_PROTOCOL_TCP)
284 || (ip_hdr(skb)->protocol == IP_PROTOCOL_UDP))) {
285
286 pko_command.s.ipoffp1 = sizeof(struct ethhdr) + 1;
287 }
288
289 if (USE_ASYNC_IOBDMA) {
290
291 CVMX_SYNCIOBDMA;
292 skb_to_free = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
293 buffers_to_free = cvmx_scratch_read64(CVMX_SCR_SCRATCH + 8);
294 } else {
295
296 skb_to_free = cvmx_fau_fetch_and_add32(priv->fau + qos * 4,
297 MAX_SKB_TO_FREE);
298 buffers_to_free =
299 cvmx_fau_fetch_and_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE, 0);
300 }
301
302
303
304
305
306
307
308 undo = skb_to_free > 0 ?
309 MAX_SKB_TO_FREE : skb_to_free + MAX_SKB_TO_FREE;
310 if (undo > 0)
311 cvmx_fau_atomic_add32(priv->fau+qos*4, -undo);
312 skb_to_free = -skb_to_free > MAX_SKB_TO_FREE ?
313 MAX_SKB_TO_FREE : -skb_to_free;
314
315
316
317
318
319 if ((buffers_to_free < -100) && !pko_command.s.dontfree) {
320 pko_command.s.dontfree = 1;
321 pko_command.s.reg0 = priv->fau + qos * 4;
322 }
323
324 cvmx_pko_send_packet_prepare(priv->port, priv->queue + qos,
325 CVMX_PKO_LOCK_CMD_QUEUE);
326
327
328 if (unlikely
329 (skb_queue_len(&priv->tx_free_list[qos]) >= MAX_OUT_QUEUE_DEPTH)) {
330
331
332
333 dropped = 1;
334 }
335
336 else if (unlikely
337 (cvmx_pko_send_packet_finish
338 (priv->port, priv->queue + qos, pko_command, hw_buffer,
339 CVMX_PKO_LOCK_CMD_QUEUE))) {
340 DEBUGPRINT("%s: Failed to send the packet\n", dev->name);
341 dropped = 1;
342 }
343
344 if (USE_ASYNC_IOBDMA) {
345
346 cvmx_scratch_write64(CVMX_SCR_SCRATCH, old_scratch);
347 cvmx_scratch_write64(CVMX_SCR_SCRATCH + 8, old_scratch2);
348 }
349
350 queue_it_up = 0;
351 if (unlikely(dropped)) {
352 dev_kfree_skb_any(skb);
353 priv->stats.tx_dropped++;
354 } else {
355 if (USE_SKBUFFS_IN_HW) {
356
357 if (pko_command.s.dontfree)
358 queue_it_up = 1;
359 else
360 cvmx_fau_atomic_add32
361 (FAU_NUM_PACKET_BUFFERS_TO_FREE, -1);
362 } else {
363
364 queue_it_up = 1;
365 }
366 }
367
368 if (queue_it_up) {
369 spin_lock(&priv->tx_free_list[qos].lock);
370 __skb_queue_tail(&priv->tx_free_list[qos], skb);
371 cvm_oct_free_tx_skbs(priv, skb_to_free, qos, 0);
372 spin_unlock(&priv->tx_free_list[qos].lock);
373 } else {
374 cvm_oct_free_tx_skbs(priv, skb_to_free, qos, 1);
375 }
376
377 return 0;
378}
379
380
381
382
383
384
385
386
387int cvm_oct_xmit_pow(struct sk_buff *skb, struct net_device *dev)
388{
389 struct octeon_ethernet *priv = netdev_priv(dev);
390 void *packet_buffer;
391 void *copy_location;
392
393
394 cvmx_wqe_t *work = cvmx_fpa_alloc(CVMX_FPA_WQE_POOL);
395 if (unlikely(work == NULL)) {
396 DEBUGPRINT("%s: Failed to allocate a work queue entry\n",
397 dev->name);
398 priv->stats.tx_dropped++;
399 dev_kfree_skb(skb);
400 return 0;
401 }
402
403
404 packet_buffer = cvmx_fpa_alloc(CVMX_FPA_PACKET_POOL);
405 if (unlikely(packet_buffer == NULL)) {
406 DEBUGPRINT("%s: Failed to allocate a packet buffer\n",
407 dev->name);
408 cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, DONT_WRITEBACK(1));
409 priv->stats.tx_dropped++;
410 dev_kfree_skb(skb);
411 return 0;
412 }
413
414
415
416
417
418
419
420
421
422 copy_location = packet_buffer + sizeof(uint64_t);
423 copy_location += ((CVMX_HELPER_FIRST_MBUFF_SKIP + 7) & 0xfff8) + 6;
424
425
426
427
428
429
430
431 memcpy(copy_location, skb->data, skb->len);
432
433
434
435
436
437 work->hw_chksum = skb->csum;
438 work->len = skb->len;
439 work->ipprt = priv->port;
440 work->qos = priv->port & 0x7;
441 work->grp = pow_send_group;
442 work->tag_type = CVMX_HELPER_INPUT_TAG_TYPE;
443 work->tag = pow_send_group;
444
445 work->word2.u64 = 0;
446 work->word2.s.bufs = 1;
447 work->packet_ptr.u64 = 0;
448 work->packet_ptr.s.addr = cvmx_ptr_to_phys(copy_location);
449 work->packet_ptr.s.pool = CVMX_FPA_PACKET_POOL;
450 work->packet_ptr.s.size = CVMX_FPA_PACKET_POOL_SIZE;
451 work->packet_ptr.s.back = (copy_location - packet_buffer) >> 7;
452
453 if (skb->protocol == htons(ETH_P_IP)) {
454 work->word2.s.ip_offset = 14;
455#if 0
456 work->word2.s.vlan_valid = 0;
457 work->word2.s.vlan_cfi = 0;
458 work->word2.s.vlan_id = 0;
459 work->word2.s.dec_ipcomp = 0;
460#endif
461 work->word2.s.tcp_or_udp =
462 (ip_hdr(skb)->protocol == IP_PROTOCOL_TCP)
463 || (ip_hdr(skb)->protocol == IP_PROTOCOL_UDP);
464#if 0
465
466 work->word2.s.dec_ipsec = 0;
467
468 work->word2.s.is_v6 = 0;
469
470 work->word2.s.software = 0;
471
472 work->word2.s.L4_error = 0;
473#endif
474 work->word2.s.is_frag = !((ip_hdr(skb)->frag_off == 0)
475 || (ip_hdr(skb)->frag_off ==
476 1 << 14));
477#if 0
478
479 work->word2.s.IP_exc = 0;
480#endif
481 work->word2.s.is_bcast = (skb->pkt_type == PACKET_BROADCAST);
482 work->word2.s.is_mcast = (skb->pkt_type == PACKET_MULTICAST);
483#if 0
484
485 work->word2.s.not_IP = 0;
486
487 work->word2.s.rcv_error = 0;
488
489 work->word2.s.err_code = 0;
490#endif
491
492
493
494
495
496
497 memcpy(work->packet_data, skb->data + 10,
498 sizeof(work->packet_data));
499 } else {
500#if 0
501 work->word2.snoip.vlan_valid = 0;
502 work->word2.snoip.vlan_cfi = 0;
503 work->word2.snoip.vlan_id = 0;
504 work->word2.snoip.software = 0;
505#endif
506 work->word2.snoip.is_rarp = skb->protocol == htons(ETH_P_RARP);
507 work->word2.snoip.is_arp = skb->protocol == htons(ETH_P_ARP);
508 work->word2.snoip.is_bcast =
509 (skb->pkt_type == PACKET_BROADCAST);
510 work->word2.snoip.is_mcast =
511 (skb->pkt_type == PACKET_MULTICAST);
512 work->word2.snoip.not_IP = 1;
513#if 0
514
515 work->word2.snoip.rcv_error = 0;
516
517 work->word2.snoip.err_code = 0;
518#endif
519 memcpy(work->packet_data, skb->data, sizeof(work->packet_data));
520 }
521
522
523 cvmx_pow_work_submit(work, work->tag, work->tag_type, work->qos,
524 work->grp);
525 priv->stats.tx_packets++;
526 priv->stats.tx_bytes += skb->len;
527 dev_kfree_skb(skb);
528 return 0;
529}
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549int cvm_oct_transmit_qos(struct net_device *dev, void *work_queue_entry,
550 int do_free, int qos)
551{
552 unsigned long flags;
553 union cvmx_buf_ptr hw_buffer;
554 cvmx_pko_command_word0_t pko_command;
555 int dropped;
556 struct octeon_ethernet *priv = netdev_priv(dev);
557 cvmx_wqe_t *work = work_queue_entry;
558
559 if (!(dev->flags & IFF_UP)) {
560 DEBUGPRINT("%s: Device not up\n", dev->name);
561 if (do_free)
562 cvm_oct_free_work(work);
563 return -1;
564 }
565
566
567
568
569 if ((CVMX_PKO_QUEUES_PER_PORT_INTERFACE0 > 1) ||
570 (CVMX_PKO_QUEUES_PER_PORT_INTERFACE1 > 1)) {
571 if (qos <= 0)
572 qos = 0;
573 else if (qos >= cvmx_pko_get_num_queues(priv->port))
574 qos = 0;
575 } else
576 qos = 0;
577
578
579 dropped = 0;
580
581 local_irq_save(flags);
582 cvmx_pko_send_packet_prepare(priv->port, priv->queue + qos,
583 CVMX_PKO_LOCK_CMD_QUEUE);
584
585
586 hw_buffer.u64 = 0;
587 hw_buffer.s.addr = work->packet_ptr.s.addr;
588 hw_buffer.s.pool = CVMX_FPA_PACKET_POOL;
589 hw_buffer.s.size = CVMX_FPA_PACKET_POOL_SIZE;
590 hw_buffer.s.back = work->packet_ptr.s.back;
591
592
593 pko_command.u64 = 0;
594 pko_command.s.n2 = 1;
595 pko_command.s.dontfree = !do_free;
596 pko_command.s.segs = work->word2.s.bufs;
597 pko_command.s.total_bytes = work->len;
598
599
600 if (unlikely(work->word2.s.not_IP || work->word2.s.IP_exc))
601 pko_command.s.ipoffp1 = 0;
602 else
603 pko_command.s.ipoffp1 = sizeof(struct ethhdr) + 1;
604
605
606 if (unlikely
607 (cvmx_pko_send_packet_finish
608 (priv->port, priv->queue + qos, pko_command, hw_buffer,
609 CVMX_PKO_LOCK_CMD_QUEUE))) {
610 DEBUGPRINT("%s: Failed to send the packet\n", dev->name);
611 dropped = -1;
612 }
613 local_irq_restore(flags);
614
615 if (unlikely(dropped)) {
616 if (do_free)
617 cvm_oct_free_work(work);
618 priv->stats.tx_dropped++;
619 } else if (do_free)
620 cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, DONT_WRITEBACK(1));
621
622 return dropped;
623}
624EXPORT_SYMBOL(cvm_oct_transmit_qos);
625
626
627
628
629
630
631void cvm_oct_tx_shutdown(struct net_device *dev)
632{
633 struct octeon_ethernet *priv = netdev_priv(dev);
634 unsigned long flags;
635 int qos;
636
637 for (qos = 0; qos < 16; qos++) {
638 spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
639 while (skb_queue_len(&priv->tx_free_list[qos]))
640 dev_kfree_skb_any(__skb_dequeue
641 (&priv->tx_free_list[qos]));
642 spin_unlock_irqrestore(&priv->tx_free_list[qos].lock, flags);
643 }
644}
645