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