1af866496SDavid Daney /***********************license start***************
2af866496SDavid Daney  * Author: Cavium Networks
3af866496SDavid Daney  *
4af866496SDavid Daney  * Contact: support@caviumnetworks.com
5af866496SDavid Daney  * This file is part of the OCTEON SDK
6af866496SDavid Daney  *
7af866496SDavid Daney  * Copyright (c) 2003-2008 Cavium Networks
8af866496SDavid Daney  *
9af866496SDavid Daney  * This file is free software; you can redistribute it and/or modify
10af866496SDavid Daney  * it under the terms of the GNU General Public License, Version 2, as
11af866496SDavid Daney  * published by the Free Software Foundation.
12af866496SDavid Daney  *
13af866496SDavid Daney  * This file is distributed in the hope that it will be useful, but
14af866496SDavid Daney  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15af866496SDavid Daney  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16af866496SDavid Daney  * NONINFRINGEMENT.  See the GNU General Public License for more
17af866496SDavid Daney  * details.
18af866496SDavid Daney  *
19af866496SDavid Daney  * You should have received a copy of the GNU General Public License
20af866496SDavid Daney  * along with this file; if not, write to the Free Software
21af866496SDavid Daney  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22af866496SDavid Daney  * or visit http://www.gnu.org/licenses/.
23af866496SDavid Daney  *
24af866496SDavid Daney  * This file may also be available under a different license from Cavium.
25af866496SDavid Daney  * Contact Cavium Networks for more information
26af866496SDavid Daney  ***********************license end**************************************/
27af866496SDavid Daney 
28af866496SDavid Daney /*
29af866496SDavid Daney  *
30af866496SDavid Daney  * Small helper utilities.
31af866496SDavid Daney  *
32af866496SDavid Daney  */
33af866496SDavid Daney 
34af866496SDavid Daney #ifndef __CVMX_HELPER_UTIL_H__
35af866496SDavid Daney #define __CVMX_HELPER_UTIL_H__
36af866496SDavid Daney 
37af866496SDavid Daney /**
38af866496SDavid Daney  * Convert a interface mode into a human readable string
39af866496SDavid Daney  *
40af866496SDavid Daney  * @mode:   Mode to convert
41af866496SDavid Daney  *
42af866496SDavid Daney  * Returns String
43af866496SDavid Daney  */
44af866496SDavid Daney extern const char
45af866496SDavid Daney     *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t mode);
46af866496SDavid Daney 
47af866496SDavid Daney /**
48af866496SDavid Daney  * Setup Random Early Drop to automatically begin dropping packets.
49af866496SDavid Daney  *
50af866496SDavid Daney  * @pass_thresh:
51af866496SDavid Daney  *		 Packets will begin slowly dropping when there are less than
52af866496SDavid Daney  *		 this many packet buffers free in FPA 0.
53af866496SDavid Daney  * @drop_thresh:
54d82603c6SJorrit Schippers  *		 All incoming packets will be dropped when there are less
55af866496SDavid Daney  *		 than this many free packet buffers in FPA 0.
56af866496SDavid Daney  * Returns Zero on success. Negative on failure
57af866496SDavid Daney  */
58af866496SDavid Daney extern int cvmx_helper_setup_red(int pass_thresh, int drop_thresh);
59af866496SDavid Daney 
60af866496SDavid Daney /**
61af866496SDavid Daney  * Get the version of the CVMX libraries.
62af866496SDavid Daney  *
63af866496SDavid Daney  * Returns Version string. Note this buffer is allocated statically
64af866496SDavid Daney  *	   and will be shared by all callers.
65af866496SDavid Daney  */
66af866496SDavid Daney extern const char *cvmx_helper_get_version(void);
67af866496SDavid Daney 
68af866496SDavid Daney /**
69af866496SDavid Daney  * Setup the common GMX settings that determine the number of
70af866496SDavid Daney  * ports. These setting apply to almost all configurations of all
71af866496SDavid Daney  * chips.
72af866496SDavid Daney  *
73af866496SDavid Daney  * @interface: Interface to configure
74af866496SDavid Daney  * @num_ports: Number of ports on the interface
75af866496SDavid Daney  *
76af866496SDavid Daney  * Returns Zero on success, negative on failure
77af866496SDavid Daney  */
78af866496SDavid Daney extern int __cvmx_helper_setup_gmx(int interface, int num_ports);
79af866496SDavid Daney 
80af866496SDavid Daney /**
81af866496SDavid Daney  * Returns the IPD/PKO port number for a port on the given
82af866496SDavid Daney  * interface.
83af866496SDavid Daney  *
84af866496SDavid Daney  * @interface: Interface to use
85af866496SDavid Daney  * @port:      Port on the interface
86af866496SDavid Daney  *
87af866496SDavid Daney  * Returns IPD/PKO port number
88af866496SDavid Daney  */
89af866496SDavid Daney extern int cvmx_helper_get_ipd_port(int interface, int port);
90af866496SDavid Daney 
91af866496SDavid Daney /**
92af866496SDavid Daney  * Returns the IPD/PKO port number for the first port on the given
93af866496SDavid Daney  * interface.
94af866496SDavid Daney  *
95af866496SDavid Daney  * @interface: Interface to use
96af866496SDavid Daney  *
97af866496SDavid Daney  * Returns IPD/PKO port number
98af866496SDavid Daney  */
cvmx_helper_get_first_ipd_port(int interface)99af866496SDavid Daney static inline int cvmx_helper_get_first_ipd_port(int interface)
100af866496SDavid Daney {
101af866496SDavid Daney 	return cvmx_helper_get_ipd_port(interface, 0);
102af866496SDavid Daney }
103af866496SDavid Daney 
104af866496SDavid Daney /**
105af866496SDavid Daney  * Returns the IPD/PKO port number for the last port on the given
106af866496SDavid Daney  * interface.
107af866496SDavid Daney  *
108af866496SDavid Daney  * @interface: Interface to use
109af866496SDavid Daney  *
110af866496SDavid Daney  * Returns IPD/PKO port number
111af866496SDavid Daney  */
cvmx_helper_get_last_ipd_port(int interface)112af866496SDavid Daney static inline int cvmx_helper_get_last_ipd_port(int interface)
113af866496SDavid Daney {
114af866496SDavid Daney 	extern int cvmx_helper_ports_on_interface(int interface);
115af866496SDavid Daney 
116af866496SDavid Daney 	return cvmx_helper_get_first_ipd_port(interface) +
117af866496SDavid Daney 	       cvmx_helper_ports_on_interface(interface) - 1;
118af866496SDavid Daney }
119af866496SDavid Daney 
120af866496SDavid Daney /**
121af866496SDavid Daney  * Free the packet buffers contained in a work queue entry.
122af866496SDavid Daney  * The work queue entry is not freed.
123af866496SDavid Daney  *
124af866496SDavid Daney  * @work:   Work queue entry with packet to free
125af866496SDavid Daney  */
cvmx_helper_free_packet_data(struct cvmx_wqe * work)126f9546de4SChris Packham static inline void cvmx_helper_free_packet_data(struct cvmx_wqe *work)
127af866496SDavid Daney {
128af866496SDavid Daney 	uint64_t number_buffers;
129af866496SDavid Daney 	union cvmx_buf_ptr buffer_ptr;
130af866496SDavid Daney 	union cvmx_buf_ptr next_buffer_ptr;
131af866496SDavid Daney 	uint64_t start_of_buffer;
132af866496SDavid Daney 
133af866496SDavid Daney 	number_buffers = work->word2.s.bufs;
134af866496SDavid Daney 	if (number_buffers == 0)
135af866496SDavid Daney 		return;
136af866496SDavid Daney 	buffer_ptr = work->packet_ptr;
137af866496SDavid Daney 
138af866496SDavid Daney 	/*
139af866496SDavid Daney 	 * Since the number of buffers is not zero, we know this is
140af866496SDavid Daney 	 * not a dynamic short packet. We need to check if it is a
141af866496SDavid Daney 	 * packet received with IPD_CTL_STATUS[NO_WPTR]. If this is
142af866496SDavid Daney 	 * true, we need to free all buffers except for the first
143af866496SDavid Daney 	 * one. The caller doesn't expect their WQE pointer to be
144af866496SDavid Daney 	 * freed
145af866496SDavid Daney 	 */
146af866496SDavid Daney 	start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
147af866496SDavid Daney 	if (cvmx_ptr_to_phys(work) == start_of_buffer) {
148af866496SDavid Daney 		next_buffer_ptr =
149af866496SDavid Daney 		    *(union cvmx_buf_ptr *) cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
150af866496SDavid Daney 		buffer_ptr = next_buffer_ptr;
151af866496SDavid Daney 		number_buffers--;
152af866496SDavid Daney 	}
153af866496SDavid Daney 
154af866496SDavid Daney 	while (number_buffers--) {
155af866496SDavid Daney 		/*
156af866496SDavid Daney 		 * Remember the back pointer is in cache lines, not
157af866496SDavid Daney 		 * 64bit words
158af866496SDavid Daney 		 */
159af866496SDavid Daney 		start_of_buffer =
160af866496SDavid Daney 		    ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
161af866496SDavid Daney 		/*
162af866496SDavid Daney 		 * Read pointer to next buffer before we free the
163af866496SDavid Daney 		 * current buffer.
164af866496SDavid Daney 		 */
165af866496SDavid Daney 		next_buffer_ptr =
166af866496SDavid Daney 		    *(union cvmx_buf_ptr *) cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
167af866496SDavid Daney 		cvmx_fpa_free(cvmx_phys_to_ptr(start_of_buffer),
168af866496SDavid Daney 			      buffer_ptr.s.pool, 0);
169af866496SDavid Daney 		buffer_ptr = next_buffer_ptr;
170af866496SDavid Daney 	}
171af866496SDavid Daney }
172af866496SDavid Daney 
173af866496SDavid Daney /**
174af866496SDavid Daney  * Returns the interface number for an IPD/PKO port number.
175af866496SDavid Daney  *
176af866496SDavid Daney  * @ipd_port: IPD/PKO port number
177af866496SDavid Daney  *
178af866496SDavid Daney  * Returns Interface number
179af866496SDavid Daney  */
180af866496SDavid Daney extern int cvmx_helper_get_interface_num(int ipd_port);
181af866496SDavid Daney 
182af866496SDavid Daney /**
183af866496SDavid Daney  * Returns the interface index number for an IPD/PKO port
184af866496SDavid Daney  * number.
185af866496SDavid Daney  *
186af866496SDavid Daney  * @ipd_port: IPD/PKO port number
187af866496SDavid Daney  *
188af866496SDavid Daney  * Returns Interface index number
189af866496SDavid Daney  */
190af866496SDavid Daney extern int cvmx_helper_get_interface_index_num(int ipd_port);
191af866496SDavid Daney 
192af866496SDavid Daney #endif /* __CVMX_HELPER_H__ */
193