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