Linux Audio

Check our new training course

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