cvmx-helper-util.h revision 210284
1210284Sjmallett/***********************license start***************
2210284Sjmallett *  Copyright (c) 2003-2008 Cavium Networks (support@cavium.com). All rights
3210284Sjmallett *  reserved.
4210284Sjmallett *
5210284Sjmallett *
6210284Sjmallett *  Redistribution and use in source and binary forms, with or without
7210284Sjmallett *  modification, are permitted provided that the following conditions are
8210284Sjmallett *  met:
9210284Sjmallett *
10210284Sjmallett *      * Redistributions of source code must retain the above copyright
11210284Sjmallett *        notice, this list of conditions and the following disclaimer.
12210284Sjmallett *
13210284Sjmallett *      * Redistributions in binary form must reproduce the above
14210284Sjmallett *        copyright notice, this list of conditions and the following
15210284Sjmallett *        disclaimer in the documentation and/or other materials provided
16210284Sjmallett *        with the distribution.
17210284Sjmallett *
18210284Sjmallett *      * Neither the name of Cavium Networks nor the names of
19210284Sjmallett *        its contributors may be used to endorse or promote products
20210284Sjmallett *        derived from this software without specific prior written
21210284Sjmallett *        permission.
22210284Sjmallett *
23210284Sjmallett *  TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24210284Sjmallett *  AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25210284Sjmallett *  OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26210284Sjmallett *  RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27210284Sjmallett *  REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28210284Sjmallett *  DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29210284Sjmallett *  OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30210284Sjmallett *  PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31210284Sjmallett *  POSSESSION OR CORRESPONDENCE TO DESCRIPTION.  THE ENTIRE RISK ARISING OUT
32210284Sjmallett *  OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
33210284Sjmallett *
34210284Sjmallett *
35210284Sjmallett *  For any questions regarding licensing please contact marketing@caviumnetworks.com
36210284Sjmallett *
37210284Sjmallett ***********************license end**************************************/
38210284Sjmallett
39210284Sjmallett
40210284Sjmallett
41210284Sjmallett
42210284Sjmallett
43210284Sjmallett
44210284Sjmallett/**
45210284Sjmallett * @file
46210284Sjmallett *
47210284Sjmallett * Small helper utilities.
48210284Sjmallett *
49210284Sjmallett * <hr>$Revision: 41586 $<hr>
50210284Sjmallett */
51210284Sjmallett
52210284Sjmallett#ifndef __CVMX_HELPER_UTIL_H__
53210284Sjmallett#define __CVMX_HELPER_UTIL_H__
54210284Sjmallett
55210284Sjmallett
56210284Sjmallett#ifdef CVMX_ENABLE_HELPER_FUNCTIONS
57210284Sjmallett
58210284Sjmallett/**
59210284Sjmallett * Convert a interface mode into a human readable string
60210284Sjmallett *
61210284Sjmallett * @param mode   Mode to convert
62210284Sjmallett *
63210284Sjmallett * @return String
64210284Sjmallett */
65210284Sjmallettextern const char *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t mode);
66210284Sjmallett
67210284Sjmallett/**
68210284Sjmallett * Debug routine to dump the packet structure to the console
69210284Sjmallett *
70210284Sjmallett * @param work   Work queue entry containing the packet to dump
71210284Sjmallett * @return
72210284Sjmallett */
73210284Sjmallettextern int cvmx_helper_dump_packet(cvmx_wqe_t *work);
74210284Sjmallett
75210284Sjmallett/**
76210284Sjmallett * Setup Random Early Drop on a specific input queue
77210284Sjmallett *
78210284Sjmallett * @param queue  Input queue to setup RED on (0-7)
79210284Sjmallett * @param pass_thresh
80210284Sjmallett *               Packets will begin slowly dropping when there are less than
81210284Sjmallett *               this many packet buffers free in FPA 0.
82210284Sjmallett * @param drop_thresh
83210284Sjmallett *               All incomming packets will be dropped when there are less
84210284Sjmallett *               than this many free packet buffers in FPA 0.
85210284Sjmallett * @return Zero on success. Negative on failure
86210284Sjmallett */
87210284Sjmallettextern int cvmx_helper_setup_red_queue(int queue, int pass_thresh, int drop_thresh);
88210284Sjmallett
89210284Sjmallett/**
90210284Sjmallett * Setup Random Early Drop to automatically begin dropping packets.
91210284Sjmallett *
92210284Sjmallett * @param pass_thresh
93210284Sjmallett *               Packets will begin slowly dropping when there are less than
94210284Sjmallett *               this many packet buffers free in FPA 0.
95210284Sjmallett * @param drop_thresh
96210284Sjmallett *               All incomming packets will be dropped when there are less
97210284Sjmallett *               than this many free packet buffers in FPA 0.
98210284Sjmallett * @return Zero on success. Negative on failure
99210284Sjmallett */
100210284Sjmallettextern int cvmx_helper_setup_red(int pass_thresh, int drop_thresh);
101210284Sjmallett
102210284Sjmallett
103210284Sjmallett/**
104210284Sjmallett * Get the version of the CVMX libraries.
105210284Sjmallett *
106210284Sjmallett * @return Version string. Note this buffer is allocated statically
107210284Sjmallett *         and will be shared by all callers.
108210284Sjmallett */
109210284Sjmallettextern const char *cvmx_helper_get_version(void);
110210284Sjmallett
111210284Sjmallett
112210284Sjmallett/**
113210284Sjmallett * @INTERNAL
114210284Sjmallett * Setup the common GMX settings that determine the number of
115210284Sjmallett * ports. These setting apply to almost all configurations of all
116210284Sjmallett * chips.
117210284Sjmallett *
118210284Sjmallett * @param interface Interface to configure
119210284Sjmallett * @param num_ports Number of ports on the interface
120210284Sjmallett *
121210284Sjmallett * @return Zero on success, negative on failure
122210284Sjmallett */
123210284Sjmallettextern int __cvmx_helper_setup_gmx(int interface, int num_ports);
124210284Sjmallett
125210284Sjmallett/**
126210284Sjmallett * Returns the IPD/PKO port number for a port on the given
127210284Sjmallett * interface.
128210284Sjmallett *
129210284Sjmallett * @param interface Interface to use
130210284Sjmallett * @param port      Port on the interface
131210284Sjmallett *
132210284Sjmallett * @return IPD/PKO port number
133210284Sjmallett */
134210284Sjmallettextern int cvmx_helper_get_ipd_port(int interface, int port);
135210284Sjmallett
136210284Sjmallett
137210284Sjmallett/**
138210284Sjmallett * Returns the IPD/PKO port number for the first port on the given
139210284Sjmallett * interface.
140210284Sjmallett *
141210284Sjmallett * @param interface Interface to use
142210284Sjmallett *
143210284Sjmallett * @return IPD/PKO port number
144210284Sjmallett */
145210284Sjmallettstatic inline int cvmx_helper_get_first_ipd_port(int interface)
146210284Sjmallett{
147210284Sjmallett    return (cvmx_helper_get_ipd_port (interface, 0));
148210284Sjmallett}
149210284Sjmallett
150210284Sjmallett/**
151210284Sjmallett * Returns the IPD/PKO port number for the last port on the given
152210284Sjmallett * interface.
153210284Sjmallett *
154210284Sjmallett * @param interface Interface to use
155210284Sjmallett *
156210284Sjmallett * @return IPD/PKO port number
157210284Sjmallett */
158210284Sjmallettstatic inline int cvmx_helper_get_last_ipd_port (int interface)
159210284Sjmallett{
160210284Sjmallett    extern int cvmx_helper_ports_on_interface (int interface);
161210284Sjmallett
162210284Sjmallett    return (cvmx_helper_get_first_ipd_port (interface) +
163210284Sjmallett  	    cvmx_helper_ports_on_interface (interface) - 1);
164210284Sjmallett}
165210284Sjmallett
166210284Sjmallett
167210284Sjmallett/**
168210284Sjmallett * Free the packet buffers contained in a work queue entry.
169210284Sjmallett * The work queue entry is not freed.
170210284Sjmallett *
171210284Sjmallett * @param work   Work queue entry with packet to free
172210284Sjmallett */
173210284Sjmallettstatic inline void cvmx_helper_free_packet_data(cvmx_wqe_t *work)
174210284Sjmallett{
175210284Sjmallett    uint64_t        number_buffers;
176210284Sjmallett    cvmx_buf_ptr_t  buffer_ptr;
177210284Sjmallett    cvmx_buf_ptr_t  next_buffer_ptr;
178210284Sjmallett    uint64_t        start_of_buffer;
179210284Sjmallett
180210284Sjmallett    number_buffers = work->word2.s.bufs;
181210284Sjmallett    if (number_buffers == 0)
182210284Sjmallett        return;
183210284Sjmallett    buffer_ptr = work->packet_ptr;
184210284Sjmallett
185210284Sjmallett    /* Since the number of buffers is not zero, we know this is not a dynamic
186210284Sjmallett        short packet. We need to check if it is a packet received with
187210284Sjmallett        IPD_CTL_STATUS[NO_WPTR]. If this is true, we need to free all buffers
188210284Sjmallett        except for the first one. The caller doesn't expect their WQE pointer
189210284Sjmallett        to be freed */
190210284Sjmallett    start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
191210284Sjmallett    if (cvmx_ptr_to_phys(work) == start_of_buffer)
192210284Sjmallett    {
193210284Sjmallett        next_buffer_ptr = *(cvmx_buf_ptr_t*)cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
194210284Sjmallett        buffer_ptr = next_buffer_ptr;
195210284Sjmallett        number_buffers--;
196210284Sjmallett    }
197210284Sjmallett
198210284Sjmallett    while (number_buffers--)
199210284Sjmallett    {
200210284Sjmallett        /* Remember the back pointer is in cache lines, not 64bit words */
201210284Sjmallett        start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
202210284Sjmallett        /* Read pointer to next buffer before we free the current buffer. */
203210284Sjmallett        next_buffer_ptr = *(cvmx_buf_ptr_t*)cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
204210284Sjmallett        cvmx_fpa_free(cvmx_phys_to_ptr(start_of_buffer), buffer_ptr.s.pool, 0);
205210284Sjmallett        buffer_ptr = next_buffer_ptr;
206210284Sjmallett    }
207210284Sjmallett}
208210284Sjmallett
209210284Sjmallett#endif /* CVMX_ENABLE_HELPER_FUNCTIONS */
210210284Sjmallett
211210284Sjmallett/**
212210284Sjmallett * Returns the interface number for an IPD/PKO port number.
213210284Sjmallett *
214210284Sjmallett * @param ipd_port IPD/PKO port number
215210284Sjmallett *
216210284Sjmallett * @return Interface number
217210284Sjmallett */
218210284Sjmallettextern int cvmx_helper_get_interface_num(int ipd_port);
219210284Sjmallett
220210284Sjmallett
221210284Sjmallett/**
222210284Sjmallett * Returns the interface index number for an IPD/PKO port
223210284Sjmallett * number.
224210284Sjmallett *
225210284Sjmallett * @param ipd_port IPD/PKO port number
226210284Sjmallett *
227210284Sjmallett * @return Interface index number
228210284Sjmallett */
229210284Sjmallettextern int cvmx_helper_get_interface_index_num(int ipd_port);
230210284Sjmallett
231210284Sjmallett/**
232210284Sjmallett * Initialize the internal QLM JTAG logic to allow programming
233210284Sjmallett * of the JTAG chain by the cvmx_helper_qlm_jtag_*() functions.
234210284Sjmallett * These functions should only be used at the direction of Cavium
235210284Sjmallett * Networks. Programming incorrect values into the JTAG chain
236210284Sjmallett * can cause chip damage.
237210284Sjmallett */
238210284Sjmallettextern void cvmx_helper_qlm_jtag_init(void);
239210284Sjmallett
240210284Sjmallett/**
241210284Sjmallett * Write up to 32bits into the QLM jtag chain. Bits are shifted
242210284Sjmallett * into the MSB and out the LSB, so you should shift in the low
243210284Sjmallett * order bits followed by the high order bits. The JTAG chain is
244210284Sjmallett * 4 * 268 bits long, or 1072.
245210284Sjmallett *
246210284Sjmallett * @param qlm    QLM to shift value into
247210284Sjmallett * @param bits   Number of bits to shift in (1-32).
248210284Sjmallett * @param data   Data to shift in. Bit 0 enters the chain first, followed by
249210284Sjmallett *               bit 1, etc.
250210284Sjmallett *
251210284Sjmallett * @return The low order bits of the JTAG chain that shifted out of the
252210284Sjmallett *         circle.
253210284Sjmallett */
254210284Sjmallettextern uint32_t cvmx_helper_qlm_jtag_shift(int qlm, int bits, uint32_t data);
255210284Sjmallett
256210284Sjmallett/**
257210284Sjmallett * Shift long sequences of zeros into the QLM JTAG chain. It is
258210284Sjmallett * common to need to shift more than 32 bits of zeros into the
259210284Sjmallett * chain. This function is a convience wrapper around
260210284Sjmallett * cvmx_helper_qlm_jtag_shift() to shift more than 32 bits of
261210284Sjmallett * zeros at a time.
262210284Sjmallett *
263210284Sjmallett * @param qlm    QLM to shift zeros into
264210284Sjmallett * @param bits
265210284Sjmallett */
266210284Sjmallettextern void cvmx_helper_qlm_jtag_shift_zeros(int qlm, int bits);
267210284Sjmallett
268210284Sjmallett/**
269210284Sjmallett * Program the QLM JTAG chain into all lanes of the QLM. You must
270210284Sjmallett * have already shifted in 268*4, or 1072 bits into the JTAG
271210284Sjmallett * chain. Updating invalid values can possibly cause chip damage.
272210284Sjmallett *
273210284Sjmallett * @param qlm    QLM to program
274210284Sjmallett */
275210284Sjmallettextern void cvmx_helper_qlm_jtag_update(int qlm);
276210284Sjmallett
277210284Sjmallett#endif  /* __CVMX_HELPER_H__ */
278