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