blob: 4e54d854021955868e2c602ac829b9b217960efd [file] [log] [blame]
David Daney80ff0fd2009-05-05 17:35:21 -07001/*********************************************************************
2 * Author: Cavium Networks
3 *
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
6 *
David Daney166bdaa2010-01-27 13:22:53 -08007 * Copyright (c) 2003-2010 Cavium Networks
David Daney80ff0fd2009-05-05 17:35:21 -07008 *
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/netdevice.h>
David Daney80ff0fd2009-05-05 17:35:21 -070030#include <linux/etherdevice.h>
31#include <linux/ip.h>
Christian Dietrich7a2eaf92011-06-04 17:35:58 +020032#include <linux/ratelimit.h>
David Daney80ff0fd2009-05-05 17:35:21 -070033#include <linux/string.h>
Imre Kalozdc890df2012-04-19 12:27:27 +020034#include <linux/interrupt.h>
David Daney80ff0fd2009-05-05 17:35:21 -070035#include <net/dst.h>
36#ifdef CONFIG_XFRM
37#include <linux/xfrm.h>
38#include <net/xfrm.h>
39#endif /* CONFIG_XFRM */
40
Arun Sharma600634972011-07-26 16:09:06 -070041#include <linux/atomic.h>
David Daney80ff0fd2009-05-05 17:35:21 -070042
43#include <asm/octeon/octeon.h>
44
45#include "ethernet-defines.h"
46#include "octeon-ethernet.h"
David Daneya620c162009-06-23 16:20:56 -070047#include "ethernet-tx.h"
David Daney80ff0fd2009-05-05 17:35:21 -070048#include "ethernet-util.h"
49
David Daneyaf866492011-11-22 14:47:00 +000050#include <asm/octeon/cvmx-wqe.h>
51#include <asm/octeon/cvmx-fau.h>
52#include <asm/octeon/cvmx-pip.h>
53#include <asm/octeon/cvmx-pko.h>
54#include <asm/octeon/cvmx-helper.h>
David Daney80ff0fd2009-05-05 17:35:21 -070055
David Daneyaf866492011-11-22 14:47:00 +000056#include <asm/octeon/cvmx-gmxx-defs.h>
David Daney80ff0fd2009-05-05 17:35:21 -070057
David Daney924cc262010-01-07 11:05:05 -080058#define CVM_OCT_SKB_CB(skb) ((u64 *)((skb)->cb))
59
David Daney80ff0fd2009-05-05 17:35:21 -070060/*
61 * You can define GET_SKBUFF_QOS() to override how the skbuff output
62 * function determines which output queue is used. The default
63 * implementation always uses the base queue for the port. If, for
Justin P. Mattock215c47c2012-03-26 21:34:18 -070064 * example, you wanted to use the skb->priority field, define
David Daney80ff0fd2009-05-05 17:35:21 -070065 * GET_SKBUFF_QOS as: #define GET_SKBUFF_QOS(skb) ((skb)->priority)
66 */
67#ifndef GET_SKBUFF_QOS
68#define GET_SKBUFF_QOS(skb) 0
69#endif
70
David Daney4898c562010-02-15 15:06:47 -080071static void cvm_oct_tx_do_cleanup(unsigned long arg);
72static DECLARE_TASKLET(cvm_oct_tx_cleanup_tasklet, cvm_oct_tx_do_cleanup, 0);
73
74/* Maximum number of SKBs to try to free per xmit packet. */
75#define MAX_SKB_TO_FREE (MAX_OUT_QUEUE_DEPTH * 2)
David Daney6888fc82010-01-07 11:05:03 -080076
77static inline int32_t cvm_oct_adjust_skb_to_free(int32_t skb_to_free, int fau)
78{
79 int32_t undo;
Ebru Akagunduza0126492013-10-10 04:02:30 +030080 undo = skb_to_free > 0 ? MAX_SKB_TO_FREE : skb_to_free +
81 MAX_SKB_TO_FREE;
David Daney6888fc82010-01-07 11:05:03 -080082 if (undo > 0)
83 cvmx_fau_atomic_add32(fau, -undo);
Ebru Akagunduza0126492013-10-10 04:02:30 +030084 skb_to_free = -skb_to_free > MAX_SKB_TO_FREE ? MAX_SKB_TO_FREE :
85 -skb_to_free;
David Daney6888fc82010-01-07 11:05:03 -080086 return skb_to_free;
87}
88
David Daney4898c562010-02-15 15:06:47 -080089static void cvm_oct_kick_tx_poll_watchdog(void)
90{
91 union cvmx_ciu_timx ciu_timx;
92 ciu_timx.u64 = 0;
93 ciu_timx.s.one_shot = 1;
94 ciu_timx.s.len = cvm_oct_tx_poll_interval;
95 cvmx_write_csr(CVMX_CIU_TIMX(1), ciu_timx.u64);
96}
97
Aaro Koskinen54396b62014-03-02 00:09:07 +020098static void cvm_oct_free_tx_skbs(struct net_device *dev)
David Daney6888fc82010-01-07 11:05:03 -080099{
100 int32_t skb_to_free;
101 int qos, queues_per_port;
David Daney4898c562010-02-15 15:06:47 -0800102 int total_freed = 0;
103 int total_remaining = 0;
104 unsigned long flags;
105 struct octeon_ethernet *priv = netdev_priv(dev);
106
David Daney6888fc82010-01-07 11:05:03 -0800107 queues_per_port = cvmx_pko_get_num_queues(priv->port);
108 /* Drain any pending packets in the free list */
109 for (qos = 0; qos < queues_per_port; qos++) {
110 if (skb_queue_len(&priv->tx_free_list[qos]) == 0)
111 continue;
Ebru Akagunduza0126492013-10-10 04:02:30 +0300112 skb_to_free = cvmx_fau_fetch_and_add32(priv->fau+qos*4,
113 MAX_SKB_TO_FREE);
114 skb_to_free = cvm_oct_adjust_skb_to_free(skb_to_free,
115 priv->fau+qos*4);
David Daney6888fc82010-01-07 11:05:03 -0800116
David Daney4898c562010-02-15 15:06:47 -0800117
118 total_freed += skb_to_free;
119 if (skb_to_free > 0) {
120 struct sk_buff *to_free_list = NULL;
121 spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
122 while (skb_to_free > 0) {
Ebru Akagunduza0126492013-10-10 04:02:30 +0300123 struct sk_buff *t;
124 t = __skb_dequeue(&priv->tx_free_list[qos]);
David Daney4898c562010-02-15 15:06:47 -0800125 t->next = to_free_list;
126 to_free_list = t;
127 skb_to_free--;
128 }
Ebru Akagunduza0126492013-10-10 04:02:30 +0300129 spin_unlock_irqrestore(&priv->tx_free_list[qos].lock,
130 flags);
David Daney4898c562010-02-15 15:06:47 -0800131 /* Do the actual freeing outside of the lock. */
132 while (to_free_list) {
133 struct sk_buff *t = to_free_list;
134 to_free_list = to_free_list->next;
135 dev_kfree_skb_any(t);
136 }
David Daney6888fc82010-01-07 11:05:03 -0800137 }
David Daney4898c562010-02-15 15:06:47 -0800138 total_remaining += skb_queue_len(&priv->tx_free_list[qos]);
David Daney6888fc82010-01-07 11:05:03 -0800139 }
David Daney4898c562010-02-15 15:06:47 -0800140 if (total_freed >= 0 && netif_queue_stopped(dev))
David Daney6888fc82010-01-07 11:05:03 -0800141 netif_wake_queue(dev);
David Daney4898c562010-02-15 15:06:47 -0800142 if (total_remaining)
143 cvm_oct_kick_tx_poll_watchdog();
David Daney6888fc82010-01-07 11:05:03 -0800144}
145
David Daney80ff0fd2009-05-05 17:35:21 -0700146/**
David Daneyec977c52010-02-16 17:25:32 -0800147 * cvm_oct_xmit - transmit a packet
David Daney80ff0fd2009-05-05 17:35:21 -0700148 * @skb: Packet to send
149 * @dev: Device info structure
David Daneyec977c52010-02-16 17:25:32 -0800150 *
151 * Returns Always returns NETDEV_TX_OK
David Daney80ff0fd2009-05-05 17:35:21 -0700152 */
153int cvm_oct_xmit(struct sk_buff *skb, struct net_device *dev)
154{
155 cvmx_pko_command_word0_t pko_command;
156 union cvmx_buf_ptr hw_buffer;
157 uint64_t old_scratch;
158 uint64_t old_scratch2;
David Daney80ff0fd2009-05-05 17:35:21 -0700159 int qos;
David Daney924cc262010-01-07 11:05:05 -0800160 int i;
David Daney6888fc82010-01-07 11:05:03 -0800161 enum {QUEUE_CORE, QUEUE_HW, QUEUE_DROP} queue_type;
David Daney80ff0fd2009-05-05 17:35:21 -0700162 struct octeon_ethernet *priv = netdev_priv(dev);
David Daney6888fc82010-01-07 11:05:03 -0800163 struct sk_buff *to_free_list;
David Daneya620c162009-06-23 16:20:56 -0700164 int32_t skb_to_free;
David Daney80ff0fd2009-05-05 17:35:21 -0700165 int32_t buffers_to_free;
David Daney4898c562010-02-15 15:06:47 -0800166 u32 total_to_clean;
David Daney6888fc82010-01-07 11:05:03 -0800167 unsigned long flags;
David Daney80ff0fd2009-05-05 17:35:21 -0700168#if REUSE_SKBUFFS_WITHOUT_FREE
169 unsigned char *fpa_head;
170#endif
171
172 /*
Justin P. Mattock215c47c2012-03-26 21:34:18 -0700173 * Prefetch the private data structure. It is larger than the
174 * one cache line.
David Daney80ff0fd2009-05-05 17:35:21 -0700175 */
176 prefetch(priv);
177
David Daney80ff0fd2009-05-05 17:35:21 -0700178 /*
179 * The check on CVMX_PKO_QUEUES_PER_PORT_* is designed to
180 * completely remove "qos" in the event neither interface
181 * supports multiple queues per port.
182 */
183 if ((CVMX_PKO_QUEUES_PER_PORT_INTERFACE0 > 1) ||
184 (CVMX_PKO_QUEUES_PER_PORT_INTERFACE1 > 1)) {
185 qos = GET_SKBUFF_QOS(skb);
186 if (qos <= 0)
187 qos = 0;
188 else if (qos >= cvmx_pko_get_num_queues(priv->port))
189 qos = 0;
190 } else
191 qos = 0;
192
193 if (USE_ASYNC_IOBDMA) {
194 /* Save scratch in case userspace is using it */
195 CVMX_SYNCIOBDMA;
196 old_scratch = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
197 old_scratch2 = cvmx_scratch_read64(CVMX_SCR_SCRATCH + 8);
198
199 /*
David Daneya620c162009-06-23 16:20:56 -0700200 * Fetch and increment the number of packets to be
201 * freed.
David Daney80ff0fd2009-05-05 17:35:21 -0700202 */
203 cvmx_fau_async_fetch_and_add32(CVMX_SCR_SCRATCH + 8,
204 FAU_NUM_PACKET_BUFFERS_TO_FREE,
205 0);
206 cvmx_fau_async_fetch_and_add32(CVMX_SCR_SCRATCH,
David Daneya620c162009-06-23 16:20:56 -0700207 priv->fau + qos * 4,
208 MAX_SKB_TO_FREE);
David Daney80ff0fd2009-05-05 17:35:21 -0700209 }
210
211 /*
David Daney924cc262010-01-07 11:05:05 -0800212 * We have space for 6 segment pointers, If there will be more
213 * than that, we must linearize.
214 */
215 if (unlikely(skb_shinfo(skb)->nr_frags > 5)) {
216 if (unlikely(__skb_linearize(skb))) {
217 queue_type = QUEUE_DROP;
218 if (USE_ASYNC_IOBDMA) {
Ebru Akagunduza0126492013-10-10 04:02:30 +0300219 /*
220 * Get the number of skbuffs in use
221 * by the hardware
222 */
David Daney924cc262010-01-07 11:05:05 -0800223 CVMX_SYNCIOBDMA;
Ebru Akagunduza0126492013-10-10 04:02:30 +0300224 skb_to_free =
225 cvmx_scratch_read64(CVMX_SCR_SCRATCH);
David Daney924cc262010-01-07 11:05:05 -0800226 } else {
Ebru Akagunduza0126492013-10-10 04:02:30 +0300227 /*
228 * Get the number of skbuffs in use
229 * by the hardware
230 */
231 skb_to_free = cvmx_fau_fetch_and_add32(
232 priv->fau + qos * 4, MAX_SKB_TO_FREE);
David Daney924cc262010-01-07 11:05:05 -0800233 }
Ebru Akagunduza0126492013-10-10 04:02:30 +0300234 skb_to_free = cvm_oct_adjust_skb_to_free(skb_to_free,
235 priv->fau + qos * 4);
David Daney924cc262010-01-07 11:05:05 -0800236 spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
237 goto skip_xmit;
238 }
239 }
240
241 /*
David Daney80ff0fd2009-05-05 17:35:21 -0700242 * The CN3XXX series of parts has an errata (GMX-401) which
243 * causes the GMX block to hang if a collision occurs towards
244 * the end of a <68 byte packet. As a workaround for this, we
245 * pad packets to be 68 bytes whenever we are in half duplex
246 * mode. We don't handle the case of having a small packet but
247 * no room to add the padding. The kernel should always give
248 * us at least a cache line
249 */
250 if ((skb->len < 64) && OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
251 union cvmx_gmxx_prtx_cfg gmx_prt_cfg;
252 int interface = INTERFACE(priv->port);
253 int index = INDEX(priv->port);
254
255 if (interface < 2) {
256 /* We only need to pad packet in half duplex mode */
257 gmx_prt_cfg.u64 =
258 cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
259 if (gmx_prt_cfg.s.duplex == 0) {
260 int add_bytes = 64 - skb->len;
261 if ((skb_tail_pointer(skb) + add_bytes) <=
262 skb_end_pointer(skb))
263 memset(__skb_put(skb, add_bytes), 0,
264 add_bytes);
265 }
266 }
267 }
268
David Daney80ff0fd2009-05-05 17:35:21 -0700269 /* Build the PKO command */
270 pko_command.u64 = 0;
271 pko_command.s.n2 = 1; /* Don't pollute L2 with the outgoing packet */
272 pko_command.s.segs = 1;
273 pko_command.s.total_bytes = skb->len;
274 pko_command.s.size0 = CVMX_FAU_OP_SIZE_32;
275 pko_command.s.subone0 = 1;
276
277 pko_command.s.dontfree = 1;
David Daney924cc262010-01-07 11:05:05 -0800278
279 /* Build the PKO buffer pointer */
280 hw_buffer.u64 = 0;
281 if (skb_shinfo(skb)->nr_frags == 0) {
282 hw_buffer.s.addr = XKPHYS_TO_PHYS((u64)skb->data);
283 hw_buffer.s.pool = 0;
284 hw_buffer.s.size = skb->len;
285 } else {
286 hw_buffer.s.addr = XKPHYS_TO_PHYS((u64)skb->data);
287 hw_buffer.s.pool = 0;
288 hw_buffer.s.size = skb_headlen(skb);
289 CVM_OCT_SKB_CB(skb)[0] = hw_buffer.u64;
290 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
291 struct skb_frag_struct *fs = skb_shinfo(skb)->frags + i;
Ebru Akagunduza0126492013-10-10 04:02:30 +0300292 hw_buffer.s.addr = XKPHYS_TO_PHYS(
293 (u64)(page_address(fs->page.p) +
294 fs->page_offset));
David Daney924cc262010-01-07 11:05:05 -0800295 hw_buffer.s.size = fs->size;
296 CVM_OCT_SKB_CB(skb)[i + 1] = hw_buffer.u64;
297 }
298 hw_buffer.s.addr = XKPHYS_TO_PHYS((u64)CVM_OCT_SKB_CB(skb));
299 hw_buffer.s.size = skb_shinfo(skb)->nr_frags + 1;
300 pko_command.s.segs = skb_shinfo(skb)->nr_frags + 1;
301 pko_command.s.gather = 1;
302 goto dont_put_skbuff_in_hw;
303 }
304
David Daney80ff0fd2009-05-05 17:35:21 -0700305 /*
306 * See if we can put this skb in the FPA pool. Any strange
307 * behavior from the Linux networking stack will most likely
308 * be caused by a bug in the following code. If some field is
Justin P. Mattock215c47c2012-03-26 21:34:18 -0700309 * in use by the network stack and gets carried over when a
310 * buffer is reused, bad things may happen. If in doubt and
David Daney80ff0fd2009-05-05 17:35:21 -0700311 * you dont need the absolute best performance, disable the
312 * define REUSE_SKBUFFS_WITHOUT_FREE. The reuse of buffers has
313 * shown a 25% increase in performance under some loads.
314 */
315#if REUSE_SKBUFFS_WITHOUT_FREE
David Daney166bdaa2010-01-27 13:22:53 -0800316 fpa_head = skb->head + 256 - ((unsigned long)skb->head & 0x7f);
David Daney80ff0fd2009-05-05 17:35:21 -0700317 if (unlikely(skb->data < fpa_head)) {
318 /*
319 * printk("TX buffer beginning can't meet FPA
320 * alignment constraints\n");
321 */
322 goto dont_put_skbuff_in_hw;
323 }
324 if (unlikely
325 ((skb_end_pointer(skb) - fpa_head) < CVMX_FPA_PACKET_POOL_SIZE)) {
326 /*
327 printk("TX buffer isn't large enough for the FPA\n");
328 */
329 goto dont_put_skbuff_in_hw;
330 }
331 if (unlikely(skb_shared(skb))) {
332 /*
333 printk("TX buffer sharing data with someone else\n");
334 */
335 goto dont_put_skbuff_in_hw;
336 }
337 if (unlikely(skb_cloned(skb))) {
338 /*
339 printk("TX buffer has been cloned\n");
340 */
341 goto dont_put_skbuff_in_hw;
342 }
343 if (unlikely(skb_header_cloned(skb))) {
344 /*
345 printk("TX buffer header has been cloned\n");
346 */
347 goto dont_put_skbuff_in_hw;
348 }
349 if (unlikely(skb->destructor)) {
350 /*
351 printk("TX buffer has a destructor\n");
352 */
353 goto dont_put_skbuff_in_hw;
354 }
355 if (unlikely(skb_shinfo(skb)->nr_frags)) {
356 /*
357 printk("TX buffer has fragments\n");
358 */
359 goto dont_put_skbuff_in_hw;
360 }
361 if (unlikely
362 (skb->truesize !=
Alexander Duyckec47ea82012-05-04 14:26:56 +0000363 sizeof(*skb) + skb_end_offset(skb))) {
David Daney80ff0fd2009-05-05 17:35:21 -0700364 /*
365 printk("TX buffer truesize has been changed\n");
366 */
367 goto dont_put_skbuff_in_hw;
368 }
369
370 /*
371 * We can use this buffer in the FPA. We don't need the FAU
372 * update anymore
373 */
David Daney80ff0fd2009-05-05 17:35:21 -0700374 pko_command.s.dontfree = 0;
375
Ebru Akagunduza0126492013-10-10 04:02:30 +0300376 hw_buffer.s.back = ((unsigned long)skb->data >> 7) -
377 ((unsigned long)fpa_head >> 7);
378
David Daney80ff0fd2009-05-05 17:35:21 -0700379 *(struct sk_buff **)(fpa_head - sizeof(void *)) = skb;
380
381 /*
382 * The skbuff will be reused without ever being freed. We must
David Daneyf696a102009-06-23 11:34:08 -0700383 * cleanup a bunch of core things.
David Daney80ff0fd2009-05-05 17:35:21 -0700384 */
David Daneyf696a102009-06-23 11:34:08 -0700385 dst_release(skb_dst(skb));
386 skb_dst_set(skb, NULL);
David Daney80ff0fd2009-05-05 17:35:21 -0700387#ifdef CONFIG_XFRM
388 secpath_put(skb->sp);
389 skb->sp = NULL;
390#endif
391 nf_reset(skb);
392
393#ifdef CONFIG_NET_SCHED
394 skb->tc_index = 0;
395#ifdef CONFIG_NET_CLS_ACT
396 skb->tc_verd = 0;
397#endif /* CONFIG_NET_CLS_ACT */
398#endif /* CONFIG_NET_SCHED */
David Daney6888fc82010-01-07 11:05:03 -0800399#endif /* REUSE_SKBUFFS_WITHOUT_FREE */
David Daney80ff0fd2009-05-05 17:35:21 -0700400
401dont_put_skbuff_in_hw:
David Daney80ff0fd2009-05-05 17:35:21 -0700402
403 /* Check if we can use the hardware checksumming */
404 if (USE_HW_TCPUDP_CHECKSUM && (skb->protocol == htons(ETH_P_IP)) &&
405 (ip_hdr(skb)->version == 4) && (ip_hdr(skb)->ihl == 5) &&
406 ((ip_hdr(skb)->frag_off == 0) || (ip_hdr(skb)->frag_off == 1 << 14))
David Daney081f6742010-01-07 11:05:06 -0800407 && ((ip_hdr(skb)->protocol == IPPROTO_TCP)
408 || (ip_hdr(skb)->protocol == IPPROTO_UDP))) {
David Daney80ff0fd2009-05-05 17:35:21 -0700409 /* Use hardware checksum calc */
410 pko_command.s.ipoffp1 = sizeof(struct ethhdr) + 1;
411 }
412
413 if (USE_ASYNC_IOBDMA) {
414 /* Get the number of skbuffs in use by the hardware */
415 CVMX_SYNCIOBDMA;
David Daneya620c162009-06-23 16:20:56 -0700416 skb_to_free = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
David Daney80ff0fd2009-05-05 17:35:21 -0700417 buffers_to_free = cvmx_scratch_read64(CVMX_SCR_SCRATCH + 8);
418 } else {
419 /* Get the number of skbuffs in use by the hardware */
David Daneya620c162009-06-23 16:20:56 -0700420 skb_to_free = cvmx_fau_fetch_and_add32(priv->fau + qos * 4,
421 MAX_SKB_TO_FREE);
David Daney80ff0fd2009-05-05 17:35:21 -0700422 buffers_to_free =
423 cvmx_fau_fetch_and_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE, 0);
424 }
425
David Daney6888fc82010-01-07 11:05:03 -0800426 skb_to_free = cvm_oct_adjust_skb_to_free(skb_to_free, priv->fau+qos*4);
David Daneya620c162009-06-23 16:20:56 -0700427
428 /*
David Daney80ff0fd2009-05-05 17:35:21 -0700429 * If we're sending faster than the receive can free them then
430 * don't do the HW free.
431 */
David Daney4898c562010-02-15 15:06:47 -0800432 if ((buffers_to_free < -100) && !pko_command.s.dontfree)
David Daney80ff0fd2009-05-05 17:35:21 -0700433 pko_command.s.dontfree = 1;
David Daney80ff0fd2009-05-05 17:35:21 -0700434
David Daney4898c562010-02-15 15:06:47 -0800435 if (pko_command.s.dontfree) {
David Daney6888fc82010-01-07 11:05:03 -0800436 queue_type = QUEUE_CORE;
David Daney4898c562010-02-15 15:06:47 -0800437 pko_command.s.reg0 = priv->fau+qos*4;
438 } else {
David Daney6888fc82010-01-07 11:05:03 -0800439 queue_type = QUEUE_HW;
David Daney4898c562010-02-15 15:06:47 -0800440 }
441 if (USE_ASYNC_IOBDMA)
Ebru Akagunduza0126492013-10-10 04:02:30 +0300442 cvmx_fau_async_fetch_and_add32(
443 CVMX_SCR_SCRATCH, FAU_TOTAL_TX_TO_CLEAN, 1);
David Daney6888fc82010-01-07 11:05:03 -0800444
445 spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
David Daney80ff0fd2009-05-05 17:35:21 -0700446
447 /* Drop this packet if we have too many already queued to the HW */
Ebru Akagunduza0126492013-10-10 04:02:30 +0300448 if (unlikely(skb_queue_len(&priv->tx_free_list[qos]) >=
449 MAX_OUT_QUEUE_DEPTH)) {
450
David Daney6888fc82010-01-07 11:05:03 -0800451 if (dev->tx_queue_len != 0) {
452 /* Drop the lock when notifying the core. */
Ebru Akagunduza0126492013-10-10 04:02:30 +0300453 spin_unlock_irqrestore(&priv->tx_free_list[qos].lock,
454 flags);
David Daney6888fc82010-01-07 11:05:03 -0800455 netif_stop_queue(dev);
Ebru Akagunduza0126492013-10-10 04:02:30 +0300456 spin_lock_irqsave(&priv->tx_free_list[qos].lock,
457 flags);
David Daney6888fc82010-01-07 11:05:03 -0800458 } else {
459 /* If not using normal queueing. */
460 queue_type = QUEUE_DROP;
461 goto skip_xmit;
462 }
David Daney80ff0fd2009-05-05 17:35:21 -0700463 }
David Daney6888fc82010-01-07 11:05:03 -0800464
465 cvmx_pko_send_packet_prepare(priv->port, priv->queue + qos,
466 CVMX_PKO_LOCK_NONE);
467
David Daney80ff0fd2009-05-05 17:35:21 -0700468 /* Send the packet to the output queue */
David Daney6888fc82010-01-07 11:05:03 -0800469 if (unlikely(cvmx_pko_send_packet_finish(priv->port,
470 priv->queue + qos,
471 pko_command, hw_buffer,
472 CVMX_PKO_LOCK_NONE))) {
Ebru Akagunduza0126492013-10-10 04:02:30 +0300473 printk_ratelimited("%s: Failed to send the packet\n",
474 dev->name);
David Daney6888fc82010-01-07 11:05:03 -0800475 queue_type = QUEUE_DROP;
476 }
477skip_xmit:
478 to_free_list = NULL;
479
480 switch (queue_type) {
481 case QUEUE_DROP:
482 skb->next = to_free_list;
483 to_free_list = skb;
484 priv->stats.tx_dropped++;
485 break;
486 case QUEUE_HW:
487 cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE, -1);
488 break;
489 case QUEUE_CORE:
490 __skb_queue_tail(&priv->tx_free_list[qos], skb);
491 break;
492 default:
493 BUG();
494 }
495
496 while (skb_to_free > 0) {
497 struct sk_buff *t = __skb_dequeue(&priv->tx_free_list[qos]);
498 t->next = to_free_list;
499 to_free_list = t;
500 skb_to_free--;
501 }
502
503 spin_unlock_irqrestore(&priv->tx_free_list[qos].lock, flags);
504
505 /* Do the actual freeing outside of the lock. */
506 while (to_free_list) {
507 struct sk_buff *t = to_free_list;
508 to_free_list = to_free_list->next;
509 dev_kfree_skb_any(t);
David Daney80ff0fd2009-05-05 17:35:21 -0700510 }
511
512 if (USE_ASYNC_IOBDMA) {
David Daney4898c562010-02-15 15:06:47 -0800513 CVMX_SYNCIOBDMA;
514 total_to_clean = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
David Daney80ff0fd2009-05-05 17:35:21 -0700515 /* Restore the scratch area */
516 cvmx_scratch_write64(CVMX_SCR_SCRATCH, old_scratch);
517 cvmx_scratch_write64(CVMX_SCR_SCRATCH + 8, old_scratch2);
David Daney4898c562010-02-15 15:06:47 -0800518 } else {
Ebru Akagunduza0126492013-10-10 04:02:30 +0300519 total_to_clean = cvmx_fau_fetch_and_add32(
520 FAU_TOTAL_TX_TO_CLEAN, 1);
David Daney80ff0fd2009-05-05 17:35:21 -0700521 }
522
David Daney4898c562010-02-15 15:06:47 -0800523 if (total_to_clean & 0x3ff) {
524 /*
525 * Schedule the cleanup tasklet every 1024 packets for
526 * the pathological case of high traffic on one port
527 * delaying clean up of packets on a different port
528 * that is blocked waiting for the cleanup.
529 */
530 tasklet_schedule(&cvm_oct_tx_cleanup_tasklet);
531 }
532
533 cvm_oct_kick_tx_poll_watchdog();
534
David Daney6888fc82010-01-07 11:05:03 -0800535 return NETDEV_TX_OK;
David Daney80ff0fd2009-05-05 17:35:21 -0700536}
537
538/**
David Daneyec977c52010-02-16 17:25:32 -0800539 * cvm_oct_xmit_pow - transmit a packet to the POW
David Daney80ff0fd2009-05-05 17:35:21 -0700540 * @skb: Packet to send
541 * @dev: Device info structure
David Daneyec977c52010-02-16 17:25:32 -0800542
David Daney80ff0fd2009-05-05 17:35:21 -0700543 * Returns Always returns zero
544 */
545int cvm_oct_xmit_pow(struct sk_buff *skb, struct net_device *dev)
546{
547 struct octeon_ethernet *priv = netdev_priv(dev);
548 void *packet_buffer;
549 void *copy_location;
550
551 /* Get a work queue entry */
552 cvmx_wqe_t *work = cvmx_fpa_alloc(CVMX_FPA_WQE_POOL);
553 if (unlikely(work == NULL)) {
Ebru Akagunduz6b478c22013-10-10 04:02:29 +0300554 printk_ratelimited("%s: Failed to allocate a work queue entry\n",
555 dev->name);
David Daney80ff0fd2009-05-05 17:35:21 -0700556 priv->stats.tx_dropped++;
Eric W. Biederman8b6da5f2014-03-15 18:39:19 -0700557 dev_kfree_skb_any(skb);
David Daney80ff0fd2009-05-05 17:35:21 -0700558 return 0;
559 }
560
561 /* Get a packet buffer */
562 packet_buffer = cvmx_fpa_alloc(CVMX_FPA_PACKET_POOL);
563 if (unlikely(packet_buffer == NULL)) {
Christian Dietrich7a2eaf92011-06-04 17:35:58 +0200564 printk_ratelimited("%s: Failed to allocate a packet buffer\n",
565 dev->name);
David Daney80ff0fd2009-05-05 17:35:21 -0700566 cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, DONT_WRITEBACK(1));
567 priv->stats.tx_dropped++;
Eric W. Biederman8b6da5f2014-03-15 18:39:19 -0700568 dev_kfree_skb_any(skb);
David Daney80ff0fd2009-05-05 17:35:21 -0700569 return 0;
570 }
571
572 /*
573 * Calculate where we need to copy the data to. We need to
574 * leave 8 bytes for a next pointer (unused). We also need to
575 * include any configure skip. Then we need to align the IP
576 * packet src and dest into the same 64bit word. The below
577 * calculation may add a little extra, but that doesn't
578 * hurt.
579 */
580 copy_location = packet_buffer + sizeof(uint64_t);
581 copy_location += ((CVMX_HELPER_FIRST_MBUFF_SKIP + 7) & 0xfff8) + 6;
582
583 /*
584 * We have to copy the packet since whoever processes this
585 * packet will free it to a hardware pool. We can't use the
586 * trick of counting outstanding packets like in
587 * cvm_oct_xmit.
588 */
589 memcpy(copy_location, skb->data, skb->len);
590
591 /*
592 * Fill in some of the work queue fields. We may need to add
593 * more if the software at the other end needs them.
594 */
595 work->hw_chksum = skb->csum;
596 work->len = skb->len;
597 work->ipprt = priv->port;
598 work->qos = priv->port & 0x7;
599 work->grp = pow_send_group;
600 work->tag_type = CVMX_HELPER_INPUT_TAG_TYPE;
601 work->tag = pow_send_group; /* FIXME */
602 /* Default to zero. Sets of zero later are commented out */
603 work->word2.u64 = 0;
604 work->word2.s.bufs = 1;
605 work->packet_ptr.u64 = 0;
606 work->packet_ptr.s.addr = cvmx_ptr_to_phys(copy_location);
607 work->packet_ptr.s.pool = CVMX_FPA_PACKET_POOL;
608 work->packet_ptr.s.size = CVMX_FPA_PACKET_POOL_SIZE;
609 work->packet_ptr.s.back = (copy_location - packet_buffer) >> 7;
610
611 if (skb->protocol == htons(ETH_P_IP)) {
612 work->word2.s.ip_offset = 14;
613#if 0
614 work->word2.s.vlan_valid = 0; /* FIXME */
615 work->word2.s.vlan_cfi = 0; /* FIXME */
616 work->word2.s.vlan_id = 0; /* FIXME */
617 work->word2.s.dec_ipcomp = 0; /* FIXME */
618#endif
619 work->word2.s.tcp_or_udp =
David Daney081f6742010-01-07 11:05:06 -0800620 (ip_hdr(skb)->protocol == IPPROTO_TCP)
621 || (ip_hdr(skb)->protocol == IPPROTO_UDP);
David Daney80ff0fd2009-05-05 17:35:21 -0700622#if 0
623 /* FIXME */
624 work->word2.s.dec_ipsec = 0;
625 /* We only support IPv4 right now */
626 work->word2.s.is_v6 = 0;
627 /* Hardware would set to zero */
628 work->word2.s.software = 0;
629 /* No error, packet is internal */
630 work->word2.s.L4_error = 0;
631#endif
632 work->word2.s.is_frag = !((ip_hdr(skb)->frag_off == 0)
633 || (ip_hdr(skb)->frag_off ==
634 1 << 14));
635#if 0
636 /* Assume Linux is sending a good packet */
637 work->word2.s.IP_exc = 0;
638#endif
639 work->word2.s.is_bcast = (skb->pkt_type == PACKET_BROADCAST);
640 work->word2.s.is_mcast = (skb->pkt_type == PACKET_MULTICAST);
641#if 0
642 /* This is an IP packet */
643 work->word2.s.not_IP = 0;
644 /* No error, packet is internal */
645 work->word2.s.rcv_error = 0;
646 /* No error, packet is internal */
647 work->word2.s.err_code = 0;
648#endif
649
650 /*
651 * When copying the data, include 4 bytes of the
652 * ethernet header to align the same way hardware
653 * does.
654 */
655 memcpy(work->packet_data, skb->data + 10,
656 sizeof(work->packet_data));
657 } else {
658#if 0
659 work->word2.snoip.vlan_valid = 0; /* FIXME */
660 work->word2.snoip.vlan_cfi = 0; /* FIXME */
661 work->word2.snoip.vlan_id = 0; /* FIXME */
662 work->word2.snoip.software = 0; /* Hardware would set to zero */
663#endif
664 work->word2.snoip.is_rarp = skb->protocol == htons(ETH_P_RARP);
665 work->word2.snoip.is_arp = skb->protocol == htons(ETH_P_ARP);
666 work->word2.snoip.is_bcast =
667 (skb->pkt_type == PACKET_BROADCAST);
668 work->word2.snoip.is_mcast =
669 (skb->pkt_type == PACKET_MULTICAST);
670 work->word2.snoip.not_IP = 1; /* IP was done up above */
671#if 0
672 /* No error, packet is internal */
673 work->word2.snoip.rcv_error = 0;
674 /* No error, packet is internal */
675 work->word2.snoip.err_code = 0;
676#endif
677 memcpy(work->packet_data, skb->data, sizeof(work->packet_data));
678 }
679
680 /* Submit the packet to the POW */
681 cvmx_pow_work_submit(work, work->tag, work->tag_type, work->qos,
682 work->grp);
683 priv->stats.tx_packets++;
684 priv->stats.tx_bytes += skb->len;
Eric W. Biederman8b6da5f2014-03-15 18:39:19 -0700685 dev_consume_skb_any(skb);
David Daney80ff0fd2009-05-05 17:35:21 -0700686 return 0;
687}
688
689/**
David Daneyec977c52010-02-16 17:25:32 -0800690 * cvm_oct_tx_shutdown_dev - free all skb that are currently queued for TX.
David Daney80ff0fd2009-05-05 17:35:21 -0700691 * @dev: Device being shutdown
David Daneyec977c52010-02-16 17:25:32 -0800692 *
David Daney80ff0fd2009-05-05 17:35:21 -0700693 */
David Daney4898c562010-02-15 15:06:47 -0800694void cvm_oct_tx_shutdown_dev(struct net_device *dev)
David Daney80ff0fd2009-05-05 17:35:21 -0700695{
696 struct octeon_ethernet *priv = netdev_priv(dev);
697 unsigned long flags;
698 int qos;
699
700 for (qos = 0; qos < 16; qos++) {
701 spin_lock_irqsave(&priv->tx_free_list[qos].lock, flags);
702 while (skb_queue_len(&priv->tx_free_list[qos]))
703 dev_kfree_skb_any(__skb_dequeue
704 (&priv->tx_free_list[qos]));
705 spin_unlock_irqrestore(&priv->tx_free_list[qos].lock, flags);
706 }
707}
David Daney4898c562010-02-15 15:06:47 -0800708
709static void cvm_oct_tx_do_cleanup(unsigned long arg)
710{
711 int port;
712
713 for (port = 0; port < TOTAL_NUMBER_OF_PORTS; port++) {
714 if (cvm_oct_device[port]) {
715 struct net_device *dev = cvm_oct_device[port];
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);
Masanari Iida811a7512013-09-16 11:44:08 +0900736 /* Register an IRQ handler to receive CIU_TIMX(1) interrupts */
David Daney4898c562010-02-15 15:06:47 -0800737 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}