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/cache.h>
14 #include <linux/cpumask.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ip.h>
18 #include <linux/string.h>
19 #include <linux/prefetch.h>
20 #include <linux/ratelimit.h>
21 #include <linux/smp.h>
22 #include <linux/interrupt.h>
23 #include <net/dst.h>
24 #ifdef CONFIG_XFRM
25 #include <linux/xfrm.h>
26 #include <net/xfrm.h>
27 #endif /* CONFIG_XFRM */
28 
29 #include <linux/atomic.h>
30 
31 #include <asm/octeon/octeon.h>
32 
33 #include "ethernet-defines.h"
34 #include "ethernet-mem.h"
35 #include "ethernet-rx.h"
36 #include "octeon-ethernet.h"
37 #include "ethernet-util.h"
38 
39 #include <asm/octeon/cvmx-helper.h>
40 #include <asm/octeon/cvmx-wqe.h>
41 #include <asm/octeon/cvmx-fau.h>
42 #include <asm/octeon/cvmx-pow.h>
43 #include <asm/octeon/cvmx-pip.h>
44 #include <asm/octeon/cvmx-scratch.h>
45 
46 #include <asm/octeon/cvmx-gmxx-defs.h>
47 
48 static struct napi_struct cvm_oct_napi;
49 
50 /**
51  * cvm_oct_do_interrupt - interrupt handler.
52  * @cpl: Interrupt number. Unused
53  * @dev_id: Cookie to identify the device. Unused
54  *
55  * The interrupt occurs whenever the POW has packets in our group.
56  *
57  */
cvm_oct_do_interrupt(int cpl,void * dev_id)58 static irqreturn_t cvm_oct_do_interrupt(int cpl, void *dev_id)
59 {
60 	/* Disable the IRQ and start napi_poll. */
61 	disable_irq_nosync(OCTEON_IRQ_WORKQ0 + pow_receive_group);
62 	napi_schedule(&cvm_oct_napi);
63 
64 	return IRQ_HANDLED;
65 }
66 
67 /**
68  * cvm_oct_check_rcv_error - process receive errors
69  * @work: Work queue entry pointing to the packet.
70  *
71  * Returns Non-zero if the packet can be dropped, zero otherwise.
72  */
cvm_oct_check_rcv_error(cvmx_wqe_t * work)73 static inline int cvm_oct_check_rcv_error(cvmx_wqe_t *work)
74 {
75 	int port;
76 
77 	if (octeon_has_feature(OCTEON_FEATURE_PKND))
78 		port = work->word0.pip.cn68xx.pknd;
79 	else
80 		port = work->word1.cn38xx.ipprt;
81 
82 	if ((work->word2.snoip.err_code == 10) && (work->word1.len <= 64)) {
83 		/*
84 		 * Ignore length errors on min size packets. Some
85 		 * equipment incorrectly pads packets to 64+4FCS
86 		 * instead of 60+4FCS.  Note these packets still get
87 		 * counted as frame errors.
88 		 */
89 	} else if (work->word2.snoip.err_code == 5 ||
90 		   work->word2.snoip.err_code == 7) {
91 		/*
92 		 * We received a packet with either an alignment error
93 		 * or a FCS error. This may be signalling that we are
94 		 * running 10Mbps with GMXX_RXX_FRM_CTL[PRE_CHK]
95 		 * off. If this is the case we need to parse the
96 		 * packet to determine if we can remove a non spec
97 		 * preamble and generate a correct packet.
98 		 */
99 		int interface = cvmx_helper_get_interface_num(port);
100 		int index = cvmx_helper_get_interface_index_num(port);
101 		union cvmx_gmxx_rxx_frm_ctl gmxx_rxx_frm_ctl;
102 
103 		gmxx_rxx_frm_ctl.u64 =
104 		    cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL(index, interface));
105 		if (gmxx_rxx_frm_ctl.s.pre_chk == 0) {
106 
107 			u8 *ptr =
108 			    cvmx_phys_to_ptr(work->packet_ptr.s.addr);
109 			int i = 0;
110 
111 			while (i < work->word1.len - 1) {
112 				if (*ptr != 0x55)
113 					break;
114 				ptr++;
115 				i++;
116 			}
117 
118 			if (*ptr == 0xd5) {
119 				/*
120 				  printk_ratelimited("Port %d received 0xd5 preamble\n",
121 					  port);
122 				 */
123 				work->packet_ptr.s.addr += i + 1;
124 				work->word1.len -= i + 5;
125 			} else if ((*ptr & 0xf) == 0xd) {
126 				/*
127 				  printk_ratelimited("Port %d received 0x?d preamble\n",
128 					  port);
129 				 */
130 				work->packet_ptr.s.addr += i;
131 				work->word1.len -= i + 4;
132 				for (i = 0; i < work->word1.len; i++) {
133 					*ptr =
134 					    ((*ptr & 0xf0) >> 4) |
135 					    ((*(ptr + 1) & 0xf) << 4);
136 					ptr++;
137 				}
138 			} else {
139 				printk_ratelimited("Port %d unknown preamble, packet dropped\n",
140 						   port);
141 				/*
142 				   cvmx_helper_dump_packet(work);
143 				 */
144 				cvm_oct_free_work(work);
145 				return 1;
146 			}
147 		}
148 	} else {
149 		printk_ratelimited("Port %d receive error code %d, packet dropped\n",
150 				   port, work->word2.snoip.err_code);
151 		cvm_oct_free_work(work);
152 		return 1;
153 	}
154 
155 	return 0;
156 }
157 
158 /**
159  * cvm_oct_napi_poll - the NAPI poll function.
160  * @napi: The NAPI instance, or null if called from cvm_oct_poll_controller
161  * @budget: Maximum number of packets to receive.
162  *
163  * Returns the number of packets processed.
164  */
cvm_oct_napi_poll(struct napi_struct * napi,int budget)165 static int cvm_oct_napi_poll(struct napi_struct *napi, int budget)
166 {
167 	const int	coreid = cvmx_get_core_num();
168 	u64	old_group_mask;
169 	u64	old_scratch;
170 	int		rx_count = 0;
171 	int		did_work_request = 0;
172 	int		packet_not_copied;
173 
174 	/* Prefetch cvm_oct_device since we know we need it soon */
175 	prefetch(cvm_oct_device);
176 
177 	if (USE_ASYNC_IOBDMA) {
178 		/* Save scratch in case userspace is using it */
179 		CVMX_SYNCIOBDMA;
180 		old_scratch = cvmx_scratch_read64(CVMX_SCR_SCRATCH);
181 	}
182 
183 	/* Only allow work for our group (and preserve priorities) */
184 	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
185 		old_group_mask = cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid));
186 		cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid),
187 				1ull << pow_receive_group);
188 		cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid)); /* Flush */
189 	} else {
190 		old_group_mask = cvmx_read_csr(CVMX_POW_PP_GRP_MSKX(coreid));
191 		cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid),
192 			(old_group_mask & ~0xFFFFull) | 1 << pow_receive_group);
193 	}
194 
195 	if (USE_ASYNC_IOBDMA) {
196 		cvmx_pow_work_request_async(CVMX_SCR_SCRATCH, CVMX_POW_NO_WAIT);
197 		did_work_request = 1;
198 	}
199 
200 	while (rx_count < budget) {
201 		struct sk_buff *skb = NULL;
202 		struct sk_buff **pskb = NULL;
203 		int skb_in_hw;
204 		cvmx_wqe_t *work;
205 		int port;
206 
207 		if (USE_ASYNC_IOBDMA && did_work_request)
208 			work = cvmx_pow_work_response_async(CVMX_SCR_SCRATCH);
209 		else
210 			work = cvmx_pow_work_request_sync(CVMX_POW_NO_WAIT);
211 
212 		prefetch(work);
213 		did_work_request = 0;
214 		if (work == NULL) {
215 			if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
216 				cvmx_write_csr(CVMX_SSO_WQ_IQ_DIS,
217 					       1ull << pow_receive_group);
218 				cvmx_write_csr(CVMX_SSO_WQ_INT,
219 					       1ull << pow_receive_group);
220 			} else {
221 				union cvmx_pow_wq_int wq_int;
222 
223 				wq_int.u64 = 0;
224 				wq_int.s.iq_dis = 1 << pow_receive_group;
225 				wq_int.s.wq_int = 1 << pow_receive_group;
226 				cvmx_write_csr(CVMX_POW_WQ_INT, wq_int.u64);
227 			}
228 			break;
229 		}
230 		pskb = (struct sk_buff **)(cvm_oct_get_buffer_ptr(work->packet_ptr) -
231 			sizeof(void *));
232 		prefetch(pskb);
233 
234 		if (USE_ASYNC_IOBDMA && rx_count < (budget - 1)) {
235 			cvmx_pow_work_request_async_nocheck(CVMX_SCR_SCRATCH,
236 							    CVMX_POW_NO_WAIT);
237 			did_work_request = 1;
238 		}
239 		rx_count++;
240 
241 		skb_in_hw = work->word2.s.bufs == 1;
242 		if (likely(skb_in_hw)) {
243 			skb = *pskb;
244 			prefetch(&skb->head);
245 			prefetch(&skb->len);
246 		}
247 
248 		if (octeon_has_feature(OCTEON_FEATURE_PKND))
249 			port = work->word0.pip.cn68xx.pknd;
250 		else
251 			port = work->word1.cn38xx.ipprt;
252 
253 		prefetch(cvm_oct_device[port]);
254 
255 		/* Immediately throw away all packets with receive errors */
256 		if (unlikely(work->word2.snoip.rcv_error)) {
257 			if (cvm_oct_check_rcv_error(work))
258 				continue;
259 		}
260 
261 		/*
262 		 * We can only use the zero copy path if skbuffs are
263 		 * in the FPA pool and the packet fits in a single
264 		 * buffer.
265 		 */
266 		if (likely(skb_in_hw)) {
267 			skb->data = skb->head + work->packet_ptr.s.addr -
268 				cvmx_ptr_to_phys(skb->head);
269 			prefetch(skb->data);
270 			skb->len = work->word1.len;
271 			skb_set_tail_pointer(skb, skb->len);
272 			packet_not_copied = 1;
273 		} else {
274 			/*
275 			 * We have to copy the packet. First allocate
276 			 * an skbuff for it.
277 			 */
278 			skb = dev_alloc_skb(work->word1.len);
279 			if (!skb) {
280 				cvm_oct_free_work(work);
281 				continue;
282 			}
283 
284 			/*
285 			 * Check if we've received a packet that was
286 			 * entirely stored in the work entry.
287 			 */
288 			if (unlikely(work->word2.s.bufs == 0)) {
289 				u8 *ptr = work->packet_data;
290 
291 				if (likely(!work->word2.s.not_IP)) {
292 					/*
293 					 * The beginning of the packet
294 					 * moves for IP packets.
295 					 */
296 					if (work->word2.s.is_v6)
297 						ptr += 2;
298 					else
299 						ptr += 6;
300 				}
301 				memcpy(skb_put(skb, work->word1.len), ptr,
302 				       work->word1.len);
303 				/* No packet buffers to free */
304 			} else {
305 				int segments = work->word2.s.bufs;
306 				union cvmx_buf_ptr segment_ptr =
307 				    work->packet_ptr;
308 				int len = work->word1.len;
309 
310 				while (segments--) {
311 					union cvmx_buf_ptr next_ptr =
312 					    *(union cvmx_buf_ptr *)cvmx_phys_to_ptr(segment_ptr.s.addr - 8);
313 
314 			/*
315 			 * Octeon Errata PKI-100: The segment size is
316 			 * wrong. Until it is fixed, calculate the
317 			 * segment size based on the packet pool
318 			 * buffer size. When it is fixed, the
319 			 * following line should be replaced with this
320 			 * one: int segment_size =
321 			 * segment_ptr.s.size;
322 			 */
323 					int segment_size =
324 					    CVMX_FPA_PACKET_POOL_SIZE -
325 					    (segment_ptr.s.addr -
326 					     (((segment_ptr.s.addr >> 7) -
327 					       segment_ptr.s.back) << 7));
328 					/*
329 					 * Don't copy more than what
330 					 * is left in the packet.
331 					 */
332 					if (segment_size > len)
333 						segment_size = len;
334 					/* Copy the data into the packet */
335 					memcpy(skb_put(skb, segment_size),
336 					       cvmx_phys_to_ptr(segment_ptr.s.addr),
337 					       segment_size);
338 					len -= segment_size;
339 					segment_ptr = next_ptr;
340 				}
341 			}
342 			packet_not_copied = 0;
343 		}
344 		if (likely((port < TOTAL_NUMBER_OF_PORTS) &&
345 			   cvm_oct_device[port])) {
346 			struct net_device *dev = cvm_oct_device[port];
347 			struct octeon_ethernet *priv = netdev_priv(dev);
348 
349 			/*
350 			 * Only accept packets for devices that are
351 			 * currently up.
352 			 */
353 			if (likely(dev->flags & IFF_UP)) {
354 				skb->protocol = eth_type_trans(skb, dev);
355 				skb->dev = dev;
356 
357 				if (unlikely(work->word2.s.not_IP ||
358 					     work->word2.s.IP_exc ||
359 					     work->word2.s.L4_error ||
360 					     !work->word2.s.tcp_or_udp))
361 					skb->ip_summed = CHECKSUM_NONE;
362 				else
363 					skb->ip_summed = CHECKSUM_UNNECESSARY;
364 
365 				/* Increment RX stats for virtual ports */
366 				if (port >= CVMX_PIP_NUM_INPUT_PORTS) {
367 #ifdef CONFIG_64BIT
368 					atomic64_add(1,
369 						     (atomic64_t *)&priv->stats.rx_packets);
370 					atomic64_add(skb->len,
371 						     (atomic64_t *)&priv->stats.rx_bytes);
372 #else
373 					atomic_add(1,
374 						   (atomic_t *)&priv->stats.rx_packets);
375 					atomic_add(skb->len,
376 						   (atomic_t *)&priv->stats.rx_bytes);
377 #endif
378 				}
379 				netif_receive_skb(skb);
380 			} else {
381 				/* Drop any packet received for a device that isn't up */
382 				/*
383 				  printk_ratelimited("%s: Device not up, packet dropped\n",
384 					   dev->name);
385 				*/
386 #ifdef CONFIG_64BIT
387 				atomic64_add(1,
388 					     (atomic64_t *)&priv->stats.rx_dropped);
389 #else
390 				atomic_add(1,
391 					   (atomic_t *)&priv->stats.rx_dropped);
392 #endif
393 				dev_kfree_skb_irq(skb);
394 			}
395 		} else {
396 			/*
397 			 * Drop any packet received for a device that
398 			 * doesn't exist.
399 			 */
400 			printk_ratelimited("Port %d not controlled by Linux, packet dropped\n",
401 				   port);
402 			dev_kfree_skb_irq(skb);
403 		}
404 		/*
405 		 * Check to see if the skbuff and work share the same
406 		 * packet buffer.
407 		 */
408 		if (likely(packet_not_copied)) {
409 			/*
410 			 * This buffer needs to be replaced, increment
411 			 * the number of buffers we need to free by
412 			 * one.
413 			 */
414 			cvmx_fau_atomic_add32(FAU_NUM_PACKET_BUFFERS_TO_FREE,
415 					      1);
416 
417 			cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, 1);
418 		} else {
419 			cvm_oct_free_work(work);
420 		}
421 	}
422 	/* Restore the original POW group mask */
423 	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
424 		cvmx_write_csr(CVMX_SSO_PPX_GRP_MSK(coreid), old_group_mask);
425 		cvmx_read_csr(CVMX_SSO_PPX_GRP_MSK(coreid)); /* Flush */
426 	} else {
427 		cvmx_write_csr(CVMX_POW_PP_GRP_MSKX(coreid), old_group_mask);
428 	}
429 
430 	if (USE_ASYNC_IOBDMA) {
431 		/* Restore the scratch area */
432 		cvmx_scratch_write64(CVMX_SCR_SCRATCH, old_scratch);
433 	}
434 	cvm_oct_rx_refill_pool(0);
435 
436 	if (rx_count < budget && napi != NULL) {
437 		/* No more work */
438 		napi_complete(napi);
439 		enable_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group);
440 	}
441 	return rx_count;
442 }
443 
444 #ifdef CONFIG_NET_POLL_CONTROLLER
445 /**
446  * cvm_oct_poll_controller - poll for receive packets
447  * device.
448  *
449  * @dev:    Device to poll. Unused
450  */
cvm_oct_poll_controller(struct net_device * dev)451 void cvm_oct_poll_controller(struct net_device *dev)
452 {
453 	cvm_oct_napi_poll(NULL, 16);
454 }
455 #endif
456 
cvm_oct_rx_initialize(void)457 void cvm_oct_rx_initialize(void)
458 {
459 	int i;
460 	struct net_device *dev_for_napi = NULL;
461 
462 	for (i = 0; i < TOTAL_NUMBER_OF_PORTS; i++) {
463 		if (cvm_oct_device[i]) {
464 			dev_for_napi = cvm_oct_device[i];
465 			break;
466 		}
467 	}
468 
469 	if (NULL == dev_for_napi)
470 		panic("No net_devices were allocated.");
471 
472 	netif_napi_add(dev_for_napi, &cvm_oct_napi, cvm_oct_napi_poll,
473 		       rx_napi_weight);
474 	napi_enable(&cvm_oct_napi);
475 
476 	/* Register an IRQ handler to receive POW interrupts */
477 	i = request_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group,
478 			cvm_oct_do_interrupt, 0, "Ethernet", cvm_oct_device);
479 
480 	if (i)
481 		panic("Could not acquire Ethernet IRQ %d\n",
482 		      OCTEON_IRQ_WORKQ0 + pow_receive_group);
483 
484 	disable_irq_nosync(OCTEON_IRQ_WORKQ0 + pow_receive_group);
485 
486 	/* Enable POW interrupt when our port has at least one packet */
487 	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
488 		union cvmx_sso_wq_int_thrx int_thr;
489 		union cvmx_pow_wq_int_pc int_pc;
490 
491 		int_thr.u64 = 0;
492 		int_thr.s.tc_en = 1;
493 		int_thr.s.tc_thr = 1;
494 		cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(pow_receive_group),
495 			       int_thr.u64);
496 
497 		int_pc.u64 = 0;
498 		int_pc.s.pc_thr = 5;
499 		cvmx_write_csr(CVMX_SSO_WQ_INT_PC, int_pc.u64);
500 	} else {
501 		union cvmx_pow_wq_int_thrx int_thr;
502 		union cvmx_pow_wq_int_pc int_pc;
503 
504 		int_thr.u64 = 0;
505 		int_thr.s.tc_en = 1;
506 		int_thr.s.tc_thr = 1;
507 		cvmx_write_csr(CVMX_POW_WQ_INT_THRX(pow_receive_group),
508 			       int_thr.u64);
509 
510 		int_pc.u64 = 0;
511 		int_pc.s.pc_thr = 5;
512 		cvmx_write_csr(CVMX_POW_WQ_INT_PC, int_pc.u64);
513 	}
514 
515 	/* Schedule NAPI now. This will indirectly enable the interrupt. */
516 	napi_schedule(&cvm_oct_napi);
517 }
518 
cvm_oct_rx_shutdown(void)519 void cvm_oct_rx_shutdown(void)
520 {
521 	netif_napi_del(&cvm_oct_napi);
522 }
523