xref: /openbmc/linux/arch/mips/include/asm/octeon/cvmx-helper-util.h (revision d82603c6da7579c50ebe3fe7da6e3e267d9f6427)
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  * Debug routine to dump the packet structure to the console
49af866496SDavid Daney  *
50af866496SDavid Daney  * @work:   Work queue entry containing the packet to dump
51af866496SDavid Daney  * Returns
52af866496SDavid Daney  */
53af866496SDavid Daney extern int cvmx_helper_dump_packet(cvmx_wqe_t *work);
54af866496SDavid Daney 
55af866496SDavid Daney /**
56af866496SDavid Daney  * Setup Random Early Drop on a specific input queue
57af866496SDavid Daney  *
58af866496SDavid Daney  * @queue:  Input queue to setup RED on (0-7)
59af866496SDavid Daney  * @pass_thresh:
60af866496SDavid Daney  *               Packets will begin slowly dropping when there are less than
61af866496SDavid Daney  *               this many packet buffers free in FPA 0.
62af866496SDavid Daney  * @drop_thresh:
63*d82603c6SJorrit Schippers  *               All incoming packets will be dropped when there are less
64af866496SDavid Daney  *               than this many free packet buffers in FPA 0.
65af866496SDavid Daney  * Returns Zero on success. Negative on failure
66af866496SDavid Daney  */
67af866496SDavid Daney extern int cvmx_helper_setup_red_queue(int queue, int pass_thresh,
68af866496SDavid Daney 				       int drop_thresh);
69af866496SDavid Daney 
70af866496SDavid Daney /**
71af866496SDavid Daney  * Setup Random Early Drop to automatically begin dropping packets.
72af866496SDavid Daney  *
73af866496SDavid Daney  * @pass_thresh:
74af866496SDavid Daney  *               Packets will begin slowly dropping when there are less than
75af866496SDavid Daney  *               this many packet buffers free in FPA 0.
76af866496SDavid Daney  * @drop_thresh:
77*d82603c6SJorrit Schippers  *               All incoming packets will be dropped when there are less
78af866496SDavid Daney  *               than this many free packet buffers in FPA 0.
79af866496SDavid Daney  * Returns Zero on success. Negative on failure
80af866496SDavid Daney  */
81af866496SDavid Daney extern int cvmx_helper_setup_red(int pass_thresh, int drop_thresh);
82af866496SDavid Daney 
83af866496SDavid Daney /**
84af866496SDavid Daney  * Get the version of the CVMX libraries.
85af866496SDavid Daney  *
86af866496SDavid Daney  * Returns Version string. Note this buffer is allocated statically
87af866496SDavid Daney  *         and will be shared by all callers.
88af866496SDavid Daney  */
89af866496SDavid Daney extern const char *cvmx_helper_get_version(void);
90af866496SDavid Daney 
91af866496SDavid Daney /**
92af866496SDavid Daney  * Setup the common GMX settings that determine the number of
93af866496SDavid Daney  * ports. These setting apply to almost all configurations of all
94af866496SDavid Daney  * chips.
95af866496SDavid Daney  *
96af866496SDavid Daney  * @interface: Interface to configure
97af866496SDavid Daney  * @num_ports: Number of ports on the interface
98af866496SDavid Daney  *
99af866496SDavid Daney  * Returns Zero on success, negative on failure
100af866496SDavid Daney  */
101af866496SDavid Daney extern int __cvmx_helper_setup_gmx(int interface, int num_ports);
102af866496SDavid Daney 
103af866496SDavid Daney /**
104af866496SDavid Daney  * Returns the IPD/PKO port number for a port on the given
105af866496SDavid Daney  * interface.
106af866496SDavid Daney  *
107af866496SDavid Daney  * @interface: Interface to use
108af866496SDavid Daney  * @port:      Port on the interface
109af866496SDavid Daney  *
110af866496SDavid Daney  * Returns IPD/PKO port number
111af866496SDavid Daney  */
112af866496SDavid Daney extern int cvmx_helper_get_ipd_port(int interface, int port);
113af866496SDavid Daney 
114af866496SDavid Daney /**
115af866496SDavid Daney  * Returns the IPD/PKO port number for the first port on the given
116af866496SDavid Daney  * interface.
117af866496SDavid Daney  *
118af866496SDavid Daney  * @interface: Interface to use
119af866496SDavid Daney  *
120af866496SDavid Daney  * Returns IPD/PKO port number
121af866496SDavid Daney  */
122af866496SDavid Daney static inline int cvmx_helper_get_first_ipd_port(int interface)
123af866496SDavid Daney {
124af866496SDavid Daney 	return cvmx_helper_get_ipd_port(interface, 0);
125af866496SDavid Daney }
126af866496SDavid Daney 
127af866496SDavid Daney /**
128af866496SDavid Daney  * Returns the IPD/PKO port number for the last port on the given
129af866496SDavid Daney  * interface.
130af866496SDavid Daney  *
131af866496SDavid Daney  * @interface: Interface to use
132af866496SDavid Daney  *
133af866496SDavid Daney  * Returns IPD/PKO port number
134af866496SDavid Daney  */
135af866496SDavid Daney static inline int cvmx_helper_get_last_ipd_port(int interface)
136af866496SDavid Daney {
137af866496SDavid Daney 	extern int cvmx_helper_ports_on_interface(int interface);
138af866496SDavid Daney 
139af866496SDavid Daney 	return cvmx_helper_get_first_ipd_port(interface) +
140af866496SDavid Daney 	       cvmx_helper_ports_on_interface(interface) - 1;
141af866496SDavid Daney }
142af866496SDavid Daney 
143af866496SDavid Daney /**
144af866496SDavid Daney  * Free the packet buffers contained in a work queue entry.
145af866496SDavid Daney  * The work queue entry is not freed.
146af866496SDavid Daney  *
147af866496SDavid Daney  * @work:   Work queue entry with packet to free
148af866496SDavid Daney  */
149af866496SDavid Daney static inline void cvmx_helper_free_packet_data(cvmx_wqe_t *work)
150af866496SDavid Daney {
151af866496SDavid Daney 	uint64_t number_buffers;
152af866496SDavid Daney 	union cvmx_buf_ptr buffer_ptr;
153af866496SDavid Daney 	union cvmx_buf_ptr next_buffer_ptr;
154af866496SDavid Daney 	uint64_t start_of_buffer;
155af866496SDavid Daney 
156af866496SDavid Daney 	number_buffers = work->word2.s.bufs;
157af866496SDavid Daney 	if (number_buffers == 0)
158af866496SDavid Daney 		return;
159af866496SDavid Daney 	buffer_ptr = work->packet_ptr;
160af866496SDavid Daney 
161af866496SDavid Daney 	/*
162af866496SDavid Daney 	 * Since the number of buffers is not zero, we know this is
163af866496SDavid Daney 	 * not a dynamic short packet. We need to check if it is a
164af866496SDavid Daney 	 * packet received with IPD_CTL_STATUS[NO_WPTR]. If this is
165af866496SDavid Daney 	 * true, we need to free all buffers except for the first
166af866496SDavid Daney 	 * one. The caller doesn't expect their WQE pointer to be
167af866496SDavid Daney 	 * freed
168af866496SDavid Daney 	 */
169af866496SDavid Daney 	start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
170af866496SDavid Daney 	if (cvmx_ptr_to_phys(work) == start_of_buffer) {
171af866496SDavid Daney 		next_buffer_ptr =
172af866496SDavid Daney 		    *(union cvmx_buf_ptr *) cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
173af866496SDavid Daney 		buffer_ptr = next_buffer_ptr;
174af866496SDavid Daney 		number_buffers--;
175af866496SDavid Daney 	}
176af866496SDavid Daney 
177af866496SDavid Daney 	while (number_buffers--) {
178af866496SDavid Daney 		/*
179af866496SDavid Daney 		 * Remember the back pointer is in cache lines, not
180af866496SDavid Daney 		 * 64bit words
181af866496SDavid Daney 		 */
182af866496SDavid Daney 		start_of_buffer =
183af866496SDavid Daney 		    ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
184af866496SDavid Daney 		/*
185af866496SDavid Daney 		 * Read pointer to next buffer before we free the
186af866496SDavid Daney 		 * current buffer.
187af866496SDavid Daney 		 */
188af866496SDavid Daney 		next_buffer_ptr =
189af866496SDavid Daney 		    *(union cvmx_buf_ptr *) cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
190af866496SDavid Daney 		cvmx_fpa_free(cvmx_phys_to_ptr(start_of_buffer),
191af866496SDavid Daney 			      buffer_ptr.s.pool, 0);
192af866496SDavid Daney 		buffer_ptr = next_buffer_ptr;
193af866496SDavid Daney 	}
194af866496SDavid Daney }
195af866496SDavid Daney 
196af866496SDavid Daney /**
197af866496SDavid Daney  * Returns the interface number for an IPD/PKO port number.
198af866496SDavid Daney  *
199af866496SDavid Daney  * @ipd_port: IPD/PKO port number
200af866496SDavid Daney  *
201af866496SDavid Daney  * Returns Interface number
202af866496SDavid Daney  */
203af866496SDavid Daney extern int cvmx_helper_get_interface_num(int ipd_port);
204af866496SDavid Daney 
205af866496SDavid Daney /**
206af866496SDavid Daney  * Returns the interface index number for an IPD/PKO port
207af866496SDavid Daney  * number.
208af866496SDavid Daney  *
209af866496SDavid Daney  * @ipd_port: IPD/PKO port number
210af866496SDavid Daney  *
211af866496SDavid Daney  * Returns Interface index number
212af866496SDavid Daney  */
213af866496SDavid Daney extern int cvmx_helper_get_interface_index_num(int ipd_port);
214af866496SDavid Daney 
215af866496SDavid Daney #endif /* __CVMX_HELPER_H__ */
216