Merge branch 'pm-cpufreq'
[linux-2.6-block.git] / drivers / staging / octeon / ethernet-tx.c
CommitLineData
67620987
AK
1/*
2 * This file is based on code from OCTEON SDK by Cavium Networks.
80ff0fd3 3 *
166bdaa9 4 * Copyright (c) 2003-2010 Cavium Networks
80ff0fd3
DD
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.
67620987
AK
9 */
10
80ff0fd3
DD
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/netdevice.h>
80ff0fd3
DD
14#include <linux/etherdevice.h>
15#include <linux/ip.h>
7a2eaf93 16#include <linux/ratelimit.h>
80ff0fd3 17#include <linux/string.h>
dc890df0 18#include <linux/interrupt.h>
80ff0fd3
DD
19#include <net/dst.h>
20#ifdef CONFIG_XFRM
21#include <linux/xfrm.h>
22#include <net/xfrm.h>
23#endif /* CONFIG_XFRM */
24
60063497 25#include <linux/atomic.h>
80ff0fd3
DD
26
27#include <asm/octeon/octeon.h>
28
29#include "ethernet-defines.h"
30#include "octeon-ethernet.h"
a620c163 31#include "ethernet-tx.h"
80ff0fd3
DD
32#include "ethernet-util.h"
33
af866496
DD
34#include <asm/octeon/cvmx-wqe.h>
35#include <asm/octeon/cvmx-fau.h>
36#include <asm/octeon/cvmx-pip.h>
37#include <asm/octeon/cvmx-pko.h>
38#include <asm/octeon/cvmx-helper.h>
80ff0fd3 39
af866496 40#include <asm/octeon/cvmx-gmxx-defs.h>
80ff0fd3 41
924cc268
DD
42#define CVM_OCT_SKB_CB(skb) ((u64 *)((skb)->cb))
43
80ff0fd3
DD
44/*
45 * You can define GET_SKBUFF_QOS() to override how the skbuff output
46 * function determines which output queue is used. The default
47 * implementation always uses the base queue for the port. If, for
215c47c9 48 * example, you wanted to use the skb->priority field, define
80ff0fd3
DD
49 * GET_SKBUFF_QOS as: #define GET_SKBUFF_QOS(skb) ((skb)->priority)
50 */
51#ifndef GET_SKBUFF_QOS
52#define GET_SKBUFF_QOS(skb) 0
53#endif
54
4898c560
DD
55static void cvm_oct_tx_do_cleanup(unsigned long arg);
56static DECLARE_TASKLET(cvm_oct_tx_cleanup_tasklet, cvm_oct_tx_do_cleanup, 0);
57
58/* Maximum number of SKBs to try to free per xmit packet. */
59#define MAX_SKB_TO_FREE (MAX_OUT_QUEUE_DEPTH * 2)
6888fc87
DD
60
61static inline int32_t cvm_oct_adjust_skb_to_free(int32_t skb_to_free, int fau)
62{
63 int32_t undo;
b9fc9cf2 64
a012649d
EA
65 undo = skb_to_free > 0 ? MAX_SKB_TO_FREE : skb_to_free +
66 MAX_SKB_TO_FREE;
6888fc87
DD
67 if (undo > 0)
68 cvmx_fau_atomic_add32(fau, -undo);
a012649d
EA
69 skb_to_free = -skb_to_free > MAX_SKB_TO_FREE ? MAX_SKB_TO_FREE :
70 -skb_to_free;
6888fc87
DD
71 return skb_to_free;
72}
73
4898c560
DD
74static void cvm_oct_kick_tx_poll_watchdog(void)
75{
76 union cvmx_ciu_timx ciu_timx;
b9fc9cf2 77
4898c560
DD
78 ciu_timx.u64 = 0;
79 ciu_timx.s.one_shot = 1;
80 ciu_timx.s.len = cvm_oct_tx_poll_interval;
81 cvmx_write_csr(CVMX_CIU_TIMX(1), ciu_timx.u64);
82}
83
54396b6b 84static void cvm_oct_free_tx_skbs(struct net_device *dev)
6888fc87
DD
85{
86 int32_t skb_to_free;
87 int qos, queues_per_port;
4898c560
DD
88 int total_freed = 0;
89 int total_remaining = 0;
90 unsigned long flags;
91 struct octeon_ethernet *priv = netdev_priv(dev);
92
6888fc87
DD
93 queues_per_port = cvmx_pko_get_num_queues(priv->port);
94 /* Drain any pending packets in the free list */
95 for (qos = 0; qos < queues_per_port; qos++) {
96 if (skb_queue_len(&priv->tx_free_list[qos]) == 0)
97 continue;
a012649d
EA
98 skb_to_free = cvmx_fau_fetch_and_add32(priv->fau+qos*4,
99 MAX_SKB_TO_FREE);
100 skb_to_free = cvm_oct_adjust_skb_to_free(skb_to_free,
101 priv->fau+qos*4);
6888fc87 102
4898c560
DD
103
104 total_freed += skb_to_free;
105 if (skb_to_free > 0) {
106 struct sk_buff *to_free_list = NULL;
b9fc9cf2 107
4898c560
DD
108 spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
109 while (skb_to_free > 0) {
a012649d 110 struct sk_buff *t;
b9fc9cf2 111
a012649d 112 t = __skb_dequeue(&priv->tx_free_list[qos]);
4898c560
DD
113 t->next = to_free_list;
114 to_free_list = t;
115 skb_to_free--;
116 }
a012649d
EA
117 spin_unlock_irqrestore(&priv->tx_free_list[qos].lock,
118 flags);
4898c560
DD
119 /* Do the actual freeing outside of the lock. */
120 while (to_free_list) {
121 struct sk_buff *t = to_free_list;
b9fc9cf2 122
4898c560
DD
123 to_free_list = to_free_list->next;
124 dev_kfree_skb_any(t);
125 }
6888fc87 126 }
4898c560 127 total_remaining += skb_queue_len(&priv->tx_free_list[qos]);
6888fc87 128 }
4898c560 129 if (total_freed >= 0 && netif_queue_stopped(dev))
6888fc87 130 netif_wake_queue(dev);
4898c560
DD
131 if (total_remaining)
132 cvm_oct_kick_tx_poll_watchdog();
6888fc87
DD
133}
134
80ff0fd3 135/**
ec977c5b 136 * cvm_oct_xmit - transmit a packet
80ff0fd3
DD
137 * @skb: Packet to send
138 * @dev: Device info structure
ec977c5b
DD
139 *
140 * Returns Always returns NETDEV_TX_OK
80ff0fd3
DD
141 */
142int cvm_oct_xmit(struct sk_buff *skb, struct net_device *dev)
143{
144 cvmx_pko_command_word0_t pko_command;
145 union cvmx_buf_ptr hw_buffer;
146 uint64_t old_scratch;
147 uint64_t old_scratch2;
80ff0fd3 148 int qos;
924cc268 149 int i;
6888fc87 150 enum {QUEUE_CORE, QUEUE_HW, QUEUE_DROP} queue_type;
80ff0fd3 151 struct octeon_ethernet *priv = netdev_priv(dev);
6888fc87 152 struct sk_buff *to_free_list;
a620c163 153 int32_t skb_to_free;
80ff0fd3 154 int32_t buffers_to_free;
4898c560 155 u32 total_to_clean;
6888fc87 156 unsigned long flags;
80ff0fd3
DD
157#if REUSE_SKBUFFS_WITHOUT_FREE
158 unsigned char *fpa_head;
159#endif
160
161 /*
215c47c9
JM
162 * Prefetch the private data structure. It is larger than the
163 * one cache line.
80ff0fd3
DD
164 */
165 prefetch(priv);
166
80ff0fd3
DD
167 /*
168 * The check on CVMX_PKO_QUEUES_PER_PORT_* is designed to
169 * completely remove "qos" in the event neither interface
170 * supports multiple queues per port.
171 */
172 if ((CVMX_PKO_QUEUES_PER_PORT_INTERFACE0 > 1) ||
173 (CVMX_PKO_QUEUES_PER_PORT_INTERFACE1 > 1)) {
174 qos = GET_SKBUFF_QOS(skb);
175 if (qos <= 0)
176 qos = 0;
177 else if (qos >= cvmx_pko_get_num_queues(priv->port))
178 qos = 0;
179 } else
180 qos = 0;
181
182 if (USE_ASYNC_IOBDMA) {
183 /* Save scratch in case userspace is using it */
184 CVMX_SYNCIOBDMA;
185 old_scratch = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
186 old_scratch2 = cvmx_scratch_read64(CVMX_SCR_SCRATCH + 8);
187
188 /*
a620c163
DD
189 * Fetch and increment the number of packets to be
190 * freed.
80ff0fd3
DD
191 */
192 cvmx_fau_async_fetch_and_add32(CVMX_SCR_SCRATCH + 8,
193 FAU_NUM_PACKET_BUFFERS_TO_FREE,
194 0);
195 cvmx_fau_async_fetch_and_add32(CVMX_SCR_SCRATCH,
a620c163
DD
196 priv->fau + qos * 4,
197 MAX_SKB_TO_FREE);
80ff0fd3
DD
198 }
199
924cc268
DD
200 /*
201 * We have space for 6 segment pointers, If there will be more
202 * than that, we must linearize.
203 */
204 if (unlikely(skb_shinfo(skb)->nr_frags > 5)) {
205 if (unlikely(__skb_linearize(skb))) {
206 queue_type = QUEUE_DROP;
207 if (USE_ASYNC_IOBDMA) {
a012649d
EA
208 /*
209 * Get the number of skbuffs in use
210 * by the hardware
211 */
924cc268 212 CVMX_SYNCIOBDMA;
a012649d
EA
213 skb_to_free =
214 cvmx_scratch_read64(CVMX_SCR_SCRATCH);
924cc268 215 } else {
a012649d
EA
216 /*
217 * Get the number of skbuffs in use
218 * by the hardware
219 */
220 skb_to_free = cvmx_fau_fetch_and_add32(
221 priv->fau + qos * 4, MAX_SKB_TO_FREE);
924cc268 222 }
a012649d
EA
223 skb_to_free = cvm_oct_adjust_skb_to_free(skb_to_free,
224 priv->fau + qos * 4);
924cc268
DD
225 spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
226 goto skip_xmit;
227 }
228 }
229
80ff0fd3
DD
230 /*
231 * The CN3XXX series of parts has an errata (GMX-401) which
232 * causes the GMX block to hang if a collision occurs towards
233 * the end of a <68 byte packet. As a workaround for this, we
234 * pad packets to be 68 bytes whenever we are in half duplex
235 * mode. We don't handle the case of having a small packet but
236 * no room to add the padding. The kernel should always give
237 * us at least a cache line
238 */
239 if ((skb->len < 64) && OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
240 union cvmx_gmxx_prtx_cfg gmx_prt_cfg;
241 int interface = INTERFACE(priv->port);
242 int index = INDEX(priv->port);
243
244 if (interface < 2) {
245 /* We only need to pad packet in half duplex mode */
246 gmx_prt_cfg.u64 =
247 cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
248 if (gmx_prt_cfg.s.duplex == 0) {
249 int add_bytes = 64 - skb->len;
b9fc9cf2 250
80ff0fd3
DD
251 if ((skb_tail_pointer(skb) + add_bytes) <=
252 skb_end_pointer(skb))
253 memset(__skb_put(skb, add_bytes), 0,
254 add_bytes);
255 }
256 }
257 }
258
80ff0fd3
DD
259 /* Build the PKO command */
260 pko_command.u64 = 0;
8a5cc923
PM
261#ifdef __LITTLE_ENDIAN
262 pko_command.s.le = 1;
263#endif
80ff0fd3
DD
264 pko_command.s.n2 = 1; /* Don't pollute L2 with the outgoing packet */
265 pko_command.s.segs = 1;
266 pko_command.s.total_bytes = skb->len;
267 pko_command.s.size0 = CVMX_FAU_OP_SIZE_32;
268 pko_command.s.subone0 = 1;
269
270 pko_command.s.dontfree = 1;
924cc268
DD
271
272 /* Build the PKO buffer pointer */
273 hw_buffer.u64 = 0;
274 if (skb_shinfo(skb)->nr_frags == 0) {
275 hw_buffer.s.addr = XKPHYS_TO_PHYS((u64)skb->data);
276 hw_buffer.s.pool = 0;
277 hw_buffer.s.size = skb->len;
278 } else {
279 hw_buffer.s.addr = XKPHYS_TO_PHYS((u64)skb->data);
280 hw_buffer.s.pool = 0;
281 hw_buffer.s.size = skb_headlen(skb);
282 CVM_OCT_SKB_CB(skb)[0] = hw_buffer.u64;
283 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
284 struct skb_frag_struct *fs = skb_shinfo(skb)->frags + i;
b9fc9cf2 285
a012649d
EA
286 hw_buffer.s.addr = XKPHYS_TO_PHYS(
287 (u64)(page_address(fs->page.p) +
288 fs->page_offset));
924cc268
DD
289 hw_buffer.s.size = fs->size;
290 CVM_OCT_SKB_CB(skb)[i + 1] = hw_buffer.u64;
291 }
292 hw_buffer.s.addr = XKPHYS_TO_PHYS((u64)CVM_OCT_SKB_CB(skb));
293 hw_buffer.s.size = skb_shinfo(skb)->nr_frags + 1;
294 pko_command.s.segs = skb_shinfo(skb)->nr_frags + 1;
295 pko_command.s.gather = 1;
296 goto dont_put_skbuff_in_hw;
297 }
298
80ff0fd3
DD
299 /*
300 * See if we can put this skb in the FPA pool. Any strange
301 * behavior from the Linux networking stack will most likely
302 * be caused by a bug in the following code. If some field is
215c47c9
JM
303 * in use by the network stack and gets carried over when a
304 * buffer is reused, bad things may happen. If in doubt and
80ff0fd3
DD
305 * you dont need the absolute best performance, disable the
306 * define REUSE_SKBUFFS_WITHOUT_FREE. The reuse of buffers has
307 * shown a 25% increase in performance under some loads.
308 */
309#if REUSE_SKBUFFS_WITHOUT_FREE
166bdaa9 310 fpa_head = skb->head + 256 - ((unsigned long)skb->head & 0x7f);
80ff0fd3
DD
311 if (unlikely(skb->data < fpa_head)) {
312 /*
313 * printk("TX buffer beginning can't meet FPA
314 * alignment constraints\n");
315 */
316 goto dont_put_skbuff_in_hw;
317 }
318 if (unlikely
319 ((skb_end_pointer(skb) - fpa_head) < CVMX_FPA_PACKET_POOL_SIZE)) {
320 /*
321 printk("TX buffer isn't large enough for the FPA\n");
322 */
323 goto dont_put_skbuff_in_hw;
324 }
325 if (unlikely(skb_shared(skb))) {
326 /*
327 printk("TX buffer sharing data with someone else\n");
328 */
329 goto dont_put_skbuff_in_hw;
330 }
331 if (unlikely(skb_cloned(skb))) {
332 /*
333 printk("TX buffer has been cloned\n");
334 */
335 goto dont_put_skbuff_in_hw;
336 }
337 if (unlikely(skb_header_cloned(skb))) {
338 /*
339 printk("TX buffer header has been cloned\n");
340 */
341 goto dont_put_skbuff_in_hw;
342 }
343 if (unlikely(skb->destructor)) {
344 /*
345 printk("TX buffer has a destructor\n");
346 */
347 goto dont_put_skbuff_in_hw;
348 }
349 if (unlikely(skb_shinfo(skb)->nr_frags)) {
350 /*
351 printk("TX buffer has fragments\n");
352 */
353 goto dont_put_skbuff_in_hw;
354 }
355 if (unlikely
356 (skb->truesize !=
ec47ea82 357 sizeof(*skb) + skb_end_offset(skb))) {
80ff0fd3
DD
358 /*
359 printk("TX buffer truesize has been changed\n");
360 */
361 goto dont_put_skbuff_in_hw;
362 }
363
364 /*
365 * We can use this buffer in the FPA. We don't need the FAU
366 * update anymore
367 */
80ff0fd3
DD
368 pko_command.s.dontfree = 0;
369
a012649d
EA
370 hw_buffer.s.back = ((unsigned long)skb->data >> 7) -
371 ((unsigned long)fpa_head >> 7);
372
80ff0fd3
DD
373 *(struct sk_buff **)(fpa_head - sizeof(void *)) = skb;
374
375 /*
376 * The skbuff will be reused without ever being freed. We must
f696a108 377 * cleanup a bunch of core things.
80ff0fd3 378 */
f696a108
DD
379 dst_release(skb_dst(skb));
380 skb_dst_set(skb, NULL);
80ff0fd3
DD
381#ifdef CONFIG_XFRM
382 secpath_put(skb->sp);
383 skb->sp = NULL;
384#endif
385 nf_reset(skb);
386
387#ifdef CONFIG_NET_SCHED
388 skb->tc_index = 0;
389#ifdef CONFIG_NET_CLS_ACT
390 skb->tc_verd = 0;
391#endif /* CONFIG_NET_CLS_ACT */
392#endif /* CONFIG_NET_SCHED */
6888fc87 393#endif /* REUSE_SKBUFFS_WITHOUT_FREE */
80ff0fd3
DD
394
395dont_put_skbuff_in_hw:
80ff0fd3
DD
396
397 /* Check if we can use the hardware checksumming */
6646baf7 398 if ((skb->protocol == htons(ETH_P_IP)) &&
861e82d5
JK
399 (ip_hdr(skb)->version == 4) &&
400 (ip_hdr(skb)->ihl == 5) &&
401 ((ip_hdr(skb)->frag_off == 0) ||
402 (ip_hdr(skb)->frag_off == htons(1 << 14))) &&
403 ((ip_hdr(skb)->protocol == IPPROTO_TCP) ||
404 (ip_hdr(skb)->protocol == IPPROTO_UDP))) {
80ff0fd3
DD
405 /* Use hardware checksum calc */
406 pko_command.s.ipoffp1 = sizeof(struct ethhdr) + 1;
407 }
408
409 if (USE_ASYNC_IOBDMA) {
410 /* Get the number of skbuffs in use by the hardware */
411 CVMX_SYNCIOBDMA;
a620c163 412 skb_to_free = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
80ff0fd3
DD
413 buffers_to_free = cvmx_scratch_read64(CVMX_SCR_SCRATCH + 8);
414 } else {
415 /* Get the number of skbuffs in use by the hardware */
a620c163
DD
416 skb_to_free = cvmx_fau_fetch_and_add32(priv->fau + qos * 4,
417 MAX_SKB_TO_FREE);
80ff0fd3
DD
418 buffers_to_free =
419 cvmx_fau_fetch_and_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE, 0);
420 }
421
6888fc87 422 skb_to_free = cvm_oct_adjust_skb_to_free(skb_to_free, priv->fau+qos*4);
a620c163 423
80ff0fd3
DD
424 /*
425 * If we're sending faster than the receive can free them then
426 * don't do the HW free.
427 */
4898c560 428 if ((buffers_to_free < -100) && !pko_command.s.dontfree)
80ff0fd3 429 pko_command.s.dontfree = 1;
80ff0fd3 430
4898c560 431 if (pko_command.s.dontfree) {
6888fc87 432 queue_type = QUEUE_CORE;
4898c560
DD
433 pko_command.s.reg0 = priv->fau+qos*4;
434 } else {
6888fc87 435 queue_type = QUEUE_HW;
4898c560
DD
436 }
437 if (USE_ASYNC_IOBDMA)
a012649d
EA
438 cvmx_fau_async_fetch_and_add32(
439 CVMX_SCR_SCRATCH, FAU_TOTAL_TX_TO_CLEAN, 1);
6888fc87
DD
440
441 spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
80ff0fd3
DD
442
443 /* Drop this packet if we have too many already queued to the HW */
a012649d
EA
444 if (unlikely(skb_queue_len(&priv->tx_free_list[qos]) >=
445 MAX_OUT_QUEUE_DEPTH)) {
446
6888fc87
DD
447 if (dev->tx_queue_len != 0) {
448 /* Drop the lock when notifying the core. */
a012649d
EA
449 spin_unlock_irqrestore(&priv->tx_free_list[qos].lock,
450 flags);
6888fc87 451 netif_stop_queue(dev);
a012649d
EA
452 spin_lock_irqsave(&priv->tx_free_list[qos].lock,
453 flags);
6888fc87
DD
454 } else {
455 /* If not using normal queueing. */
456 queue_type = QUEUE_DROP;
457 goto skip_xmit;
458 }
80ff0fd3 459 }
6888fc87
DD
460
461 cvmx_pko_send_packet_prepare(priv->port, priv->queue + qos,
462 CVMX_PKO_LOCK_NONE);
463
80ff0fd3 464 /* Send the packet to the output queue */
6888fc87
DD
465 if (unlikely(cvmx_pko_send_packet_finish(priv->port,
466 priv->queue + qos,
467 pko_command, hw_buffer,
468 CVMX_PKO_LOCK_NONE))) {
a012649d
EA
469 printk_ratelimited("%s: Failed to send the packet\n",
470 dev->name);
6888fc87 471 queue_type = QUEUE_DROP;
80ff0fd3 472 }
6888fc87
DD
473skip_xmit:
474 to_free_list = NULL;
80ff0fd3 475
6888fc87
DD
476 switch (queue_type) {
477 case QUEUE_DROP:
478 skb->next = to_free_list;
479 to_free_list = skb;
480 priv->stats.tx_dropped++;
481 break;
482 case QUEUE_HW:
483 cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE, -1);
484 break;
485 case QUEUE_CORE:
486 __skb_queue_tail(&priv->tx_free_list[qos], skb);
487 break;
488 default:
489 BUG();
80ff0fd3
DD
490 }
491
6888fc87
DD
492 while (skb_to_free > 0) {
493 struct sk_buff *t = __skb_dequeue(&priv->tx_free_list[qos]);
b9fc9cf2 494
6888fc87
DD
495 t->next = to_free_list;
496 to_free_list = t;
497 skb_to_free--;
80ff0fd3
DD
498 }
499
6888fc87
DD
500 spin_unlock_irqrestore(&priv->tx_free_list[qos].lock, flags);
501
502 /* Do the actual freeing outside of the lock. */
503 while (to_free_list) {
504 struct sk_buff *t = to_free_list;
b9fc9cf2 505
6888fc87
DD
506 to_free_list = to_free_list->next;
507 dev_kfree_skb_any(t);
508 }
509
510 if (USE_ASYNC_IOBDMA) {
4898c560
DD
511 CVMX_SYNCIOBDMA;
512 total_to_clean = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
6888fc87
DD
513 /* Restore the scratch area */
514 cvmx_scratch_write64(CVMX_SCR_SCRATCH, old_scratch);
515 cvmx_scratch_write64(CVMX_SCR_SCRATCH + 8, old_scratch2);
4898c560 516 } else {
a012649d
EA
517 total_to_clean = cvmx_fau_fetch_and_add32(
518 FAU_TOTAL_TX_TO_CLEAN, 1);
80ff0fd3
DD
519 }
520
4898c560
DD
521 if (total_to_clean & 0x3ff) {
522 /*
523 * Schedule the cleanup tasklet every 1024 packets for
524 * the pathological case of high traffic on one port
525 * delaying clean up of packets on a different port
526 * that is blocked waiting for the cleanup.
527 */
528 tasklet_schedule(&cvm_oct_tx_cleanup_tasklet);
529 }
530
531 cvm_oct_kick_tx_poll_watchdog();
532
6888fc87 533 return NETDEV_TX_OK;
80ff0fd3
DD
534}
535
536/**
ec977c5b 537 * cvm_oct_xmit_pow - transmit a packet to the POW
80ff0fd3
DD
538 * @skb: Packet to send
539 * @dev: Device info structure
ec977c5b 540
80ff0fd3
DD
541 * Returns Always returns zero
542 */
543int cvm_oct_xmit_pow(struct sk_buff *skb, struct net_device *dev)
544{
545 struct octeon_ethernet *priv = netdev_priv(dev);
546 void *packet_buffer;
547 void *copy_location;
548
549 /* Get a work queue entry */
550 cvmx_wqe_t *work = cvmx_fpa_alloc(CVMX_FPA_WQE_POOL);
b9fc9cf2 551
80ff0fd3 552 if (unlikely(work == NULL)) {
6b478c2c
EA
553 printk_ratelimited("%s: Failed to allocate a work queue entry\n",
554 dev->name);
80ff0fd3 555 priv->stats.tx_dropped++;
8b6da5fb 556 dev_kfree_skb_any(skb);
80ff0fd3
DD
557 return 0;
558 }
559
560 /* Get a packet buffer */
561 packet_buffer = cvmx_fpa_alloc(CVMX_FPA_PACKET_POOL);
562 if (unlikely(packet_buffer == NULL)) {
7a2eaf93
CD
563 printk_ratelimited("%s: Failed to allocate a packet buffer\n",
564 dev->name);
c93b0e75 565 cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, 1);
80ff0fd3 566 priv->stats.tx_dropped++;
8b6da5fb 567 dev_kfree_skb_any(skb);
80ff0fd3
DD
568 return 0;
569 }
570
571 /*
572 * Calculate where we need to copy the data to. We need to
573 * leave 8 bytes for a next pointer (unused). We also need to
574 * include any configure skip. Then we need to align the IP
575 * packet src and dest into the same 64bit word. The below
576 * calculation may add a little extra, but that doesn't
577 * hurt.
578 */
579 copy_location = packet_buffer + sizeof(uint64_t);
580 copy_location += ((CVMX_HELPER_FIRST_MBUFF_SKIP + 7) & 0xfff8) + 6;
581
582 /*
583 * We have to copy the packet since whoever processes this
584 * packet will free it to a hardware pool. We can't use the
585 * trick of counting outstanding packets like in
586 * cvm_oct_xmit.
587 */
588 memcpy(copy_location, skb->data, skb->len);
589
590 /*
591 * Fill in some of the work queue fields. We may need to add
592 * more if the software at the other end needs them.
593 */
594 work->hw_chksum = skb->csum;
595 work->len = skb->len;
596 work->ipprt = priv->port;
597 work->qos = priv->port & 0x7;
598 work->grp = pow_send_group;
599 work->tag_type = CVMX_HELPER_INPUT_TAG_TYPE;
600 work->tag = pow_send_group; /* FIXME */
601 /* Default to zero. Sets of zero later are commented out */
602 work->word2.u64 = 0;
603 work->word2.s.bufs = 1;
604 work->packet_ptr.u64 = 0;
605 work->packet_ptr.s.addr = cvmx_ptr_to_phys(copy_location);
606 work->packet_ptr.s.pool = CVMX_FPA_PACKET_POOL;
607 work->packet_ptr.s.size = CVMX_FPA_PACKET_POOL_SIZE;
608 work->packet_ptr.s.back = (copy_location - packet_buffer) >> 7;
609
610 if (skb->protocol == htons(ETH_P_IP)) {
611 work->word2.s.ip_offset = 14;
612#if 0
613 work->word2.s.vlan_valid = 0; /* FIXME */
614 work->word2.s.vlan_cfi = 0; /* FIXME */
615 work->word2.s.vlan_id = 0; /* FIXME */
616 work->word2.s.dec_ipcomp = 0; /* FIXME */
617#endif
618 work->word2.s.tcp_or_udp =
081f6749
DD
619 (ip_hdr(skb)->protocol == IPPROTO_TCP)
620 || (ip_hdr(skb)->protocol == IPPROTO_UDP);
80ff0fd3
DD
621#if 0
622 /* FIXME */
623 work->word2.s.dec_ipsec = 0;
624 /* We only support IPv4 right now */
625 work->word2.s.is_v6 = 0;
626 /* Hardware would set to zero */
627 work->word2.s.software = 0;
628 /* No error, packet is internal */
629 work->word2.s.L4_error = 0;
630#endif
631 work->word2.s.is_frag = !((ip_hdr(skb)->frag_off == 0)
632 || (ip_hdr(skb)->frag_off ==
633 1 << 14));
634#if 0
635 /* Assume Linux is sending a good packet */
636 work->word2.s.IP_exc = 0;
637#endif
638 work->word2.s.is_bcast = (skb->pkt_type == PACKET_BROADCAST);
639 work->word2.s.is_mcast = (skb->pkt_type == PACKET_MULTICAST);
640#if 0
641 /* This is an IP packet */
642 work->word2.s.not_IP = 0;
643 /* No error, packet is internal */
644 work->word2.s.rcv_error = 0;
645 /* No error, packet is internal */
646 work->word2.s.err_code = 0;
647#endif
648
649 /*
650 * When copying the data, include 4 bytes of the
651 * ethernet header to align the same way hardware
652 * does.
653 */
654 memcpy(work->packet_data, skb->data + 10,
655 sizeof(work->packet_data));
656 } else {
657#if 0
658 work->word2.snoip.vlan_valid = 0; /* FIXME */
659 work->word2.snoip.vlan_cfi = 0; /* FIXME */
660 work->word2.snoip.vlan_id = 0; /* FIXME */
661 work->word2.snoip.software = 0; /* Hardware would set to zero */
662#endif
663 work->word2.snoip.is_rarp = skb->protocol == htons(ETH_P_RARP);
664 work->word2.snoip.is_arp = skb->protocol == htons(ETH_P_ARP);
665 work->word2.snoip.is_bcast =
666 (skb->pkt_type == PACKET_BROADCAST);
667 work->word2.snoip.is_mcast =
668 (skb->pkt_type == PACKET_MULTICAST);
669 work->word2.snoip.not_IP = 1; /* IP was done up above */
670#if 0
671 /* No error, packet is internal */
672 work->word2.snoip.rcv_error = 0;
673 /* No error, packet is internal */
674 work->word2.snoip.err_code = 0;
675#endif
676 memcpy(work->packet_data, skb->data, sizeof(work->packet_data));
677 }
678
679 /* Submit the packet to the POW */
680 cvmx_pow_work_submit(work, work->tag, work->tag_type, work->qos,
681 work->grp);
682 priv->stats.tx_packets++;
683 priv->stats.tx_bytes += skb->len;
8b6da5fb 684 dev_consume_skb_any(skb);
80ff0fd3
DD
685 return 0;
686}
687
80ff0fd3 688/**
ec977c5b 689 * cvm_oct_tx_shutdown_dev - free all skb that are currently queued for TX.
80ff0fd3 690 * @dev: Device being shutdown
ec977c5b 691 *
80ff0fd3 692 */
4898c560 693void cvm_oct_tx_shutdown_dev(struct net_device *dev)
80ff0fd3
DD
694{
695 struct octeon_ethernet *priv = netdev_priv(dev);
696 unsigned long flags;
697 int qos;
698
699 for (qos = 0; qos < 16; qos++) {
700 spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
701 while (skb_queue_len(&priv->tx_free_list[qos]))
702 dev_kfree_skb_any(__skb_dequeue
703 (&priv->tx_free_list[qos]));
704 spin_unlock_irqrestore(&priv->tx_free_list[qos].lock, flags);
705 }
706}
4898c560
DD
707
708static void cvm_oct_tx_do_cleanup(unsigned long arg)
709{
710 int port;
711
712 for (port = 0; port < TOTAL_NUMBER_OF_PORTS; port++) {
713 if (cvm_oct_device[port]) {
714 struct net_device *dev = cvm_oct_device[port];
b9fc9cf2 715
4898c560
DD
716 cvm_oct_free_tx_skbs(dev);
717 }
718 }
719}
720
721static irqreturn_t cvm_oct_tx_cleanup_watchdog(int cpl, void *dev_id)
722{
723 /* Disable the interrupt. */
724 cvmx_write_csr(CVMX_CIU_TIMX(1), 0);
725 /* Do the work in the tasklet. */
726 tasklet_schedule(&cvm_oct_tx_cleanup_tasklet);
727 return IRQ_HANDLED;
728}
729
730void cvm_oct_tx_initialize(void)
731{
732 int i;
733
734 /* Disable the interrupt. */
735 cvmx_write_csr(CVMX_CIU_TIMX(1), 0);
811a7519 736 /* Register an IRQ handler to receive CIU_TIMX(1) interrupts */
4898c560
DD
737 i = request_irq(OCTEON_IRQ_TIMER1,
738 cvm_oct_tx_cleanup_watchdog, 0,
739 "Ethernet", cvm_oct_device);
740
741 if (i)
742 panic("Could not acquire Ethernet IRQ %d\n", OCTEON_IRQ_TIMER1);
743}
744
745void cvm_oct_tx_shutdown(void)
746{
747 /* Free the interrupt handler */
748 free_irq(OCTEON_IRQ_TIMER1, cvm_oct_device);
749}