1210284Sjmallett/***********************license start***************
2232812Sjmallett * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
3215990Sjmallett * reserved.
4210284Sjmallett *
5210284Sjmallett *
6215990Sjmallett * Redistribution and use in source and binary forms, with or without
7215990Sjmallett * modification, are permitted provided that the following conditions are
8215990Sjmallett * met:
9210284Sjmallett *
10215990Sjmallett *   * Redistributions of source code must retain the above copyright
11215990Sjmallett *     notice, this list of conditions and the following disclaimer.
12210284Sjmallett *
13215990Sjmallett *   * Redistributions in binary form must reproduce the above
14215990Sjmallett *     copyright notice, this list of conditions and the following
15215990Sjmallett *     disclaimer in the documentation and/or other materials provided
16215990Sjmallett *     with the distribution.
17215990Sjmallett
18232812Sjmallett *   * Neither the name of Cavium Inc. nor the names of
19215990Sjmallett *     its contributors may be used to endorse or promote products
20215990Sjmallett *     derived from this software without specific prior written
21215990Sjmallett *     permission.
22215990Sjmallett
23215990Sjmallett * This Software, including technical data, may be subject to U.S. export  control
24215990Sjmallett * laws, including the U.S. Export Administration Act and its  associated
25215990Sjmallett * regulations, and may be subject to export or import  regulations in other
26215990Sjmallett * countries.
27215990Sjmallett
28215990Sjmallett * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29232812Sjmallett * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30215990Sjmallett * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31215990Sjmallett * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32215990Sjmallett * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33215990Sjmallett * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34215990Sjmallett * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35215990Sjmallett * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36215990Sjmallett * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
37215990Sjmallett * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38210284Sjmallett ***********************license end**************************************/
39210284Sjmallett
40210284Sjmallett
41210284Sjmallett
42210284Sjmallett
43210284Sjmallett
44210284Sjmallett
45215990Sjmallett
46210284Sjmallett/**
47210284Sjmallett * @file
48210284Sjmallett *
49210284Sjmallett * Small helper utilities.
50210284Sjmallett *
51232812Sjmallett * <hr>$Revision: 70030 $<hr>
52210284Sjmallett */
53210284Sjmallett
54210284Sjmallett#ifndef __CVMX_HELPER_UTIL_H__
55210284Sjmallett#define __CVMX_HELPER_UTIL_H__
56210284Sjmallett
57232812Sjmallett#include "cvmx.h"
58232812Sjmallett#include "cvmx-mio-defs.h"
59210284Sjmallett
60210284Sjmallett#ifdef CVMX_ENABLE_HELPER_FUNCTIONS
61210284Sjmallett
62232812Sjmalletttypedef char cvmx_pknd_t;
63232812Sjmalletttypedef char cvmx_bpid_t;
64232812Sjmallett
65232812Sjmallett#define CVMX_INVALID_PKND	((cvmx_pknd_t) -1)
66232812Sjmallett#define CVMX_INVALID_BPID	((cvmx_bpid_t) -1)
67232812Sjmallett#define CVMX_MAX_PKND		((cvmx_pknd_t) 64)
68232812Sjmallett#define CVMX_MAX_BPID		((cvmx_bpid_t) 64)
69232812Sjmallett
70232812Sjmallett#define CVMX_HELPER_MAX_IFACE		9
71232812Sjmallett
72210284Sjmallett/**
73210284Sjmallett * Convert a interface mode into a human readable string
74210284Sjmallett *
75210284Sjmallett * @param mode   Mode to convert
76210284Sjmallett *
77210284Sjmallett * @return String
78210284Sjmallett */
79210284Sjmallettextern const char *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t mode);
80210284Sjmallett
81210284Sjmallett/**
82210284Sjmallett * Debug routine to dump the packet structure to the console
83210284Sjmallett *
84210284Sjmallett * @param work   Work queue entry containing the packet to dump
85210284Sjmallett * @return
86210284Sjmallett */
87210284Sjmallettextern int cvmx_helper_dump_packet(cvmx_wqe_t *work);
88210284Sjmallett
89210284Sjmallett/**
90210284Sjmallett * Setup Random Early Drop on a specific input queue
91210284Sjmallett *
92210284Sjmallett * @param queue  Input queue to setup RED on (0-7)
93210284Sjmallett * @param pass_thresh
94210284Sjmallett *               Packets will begin slowly dropping when there are less than
95210284Sjmallett *               this many packet buffers free in FPA 0.
96210284Sjmallett * @param drop_thresh
97210284Sjmallett *               All incomming packets will be dropped when there are less
98210284Sjmallett *               than this many free packet buffers in FPA 0.
99210284Sjmallett * @return Zero on success. Negative on failure
100210284Sjmallett */
101210284Sjmallettextern int cvmx_helper_setup_red_queue(int queue, int pass_thresh, int drop_thresh);
102210284Sjmallett
103210284Sjmallett/**
104210284Sjmallett * Setup Random Early Drop to automatically begin dropping packets.
105210284Sjmallett *
106210284Sjmallett * @param pass_thresh
107210284Sjmallett *               Packets will begin slowly dropping when there are less than
108210284Sjmallett *               this many packet buffers free in FPA 0.
109210284Sjmallett * @param drop_thresh
110210284Sjmallett *               All incomming packets will be dropped when there are less
111210284Sjmallett *               than this many free packet buffers in FPA 0.
112210284Sjmallett * @return Zero on success. Negative on failure
113210284Sjmallett */
114210284Sjmallettextern int cvmx_helper_setup_red(int pass_thresh, int drop_thresh);
115210284Sjmallett
116210284Sjmallett
117210284Sjmallett/**
118210284Sjmallett * Get the version of the CVMX libraries.
119210284Sjmallett *
120210284Sjmallett * @return Version string. Note this buffer is allocated statically
121210284Sjmallett *         and will be shared by all callers.
122210284Sjmallett */
123210284Sjmallettextern const char *cvmx_helper_get_version(void);
124210284Sjmallett
125210284Sjmallett
126210284Sjmallett/**
127210284Sjmallett * @INTERNAL
128210284Sjmallett * Setup the common GMX settings that determine the number of
129210284Sjmallett * ports. These setting apply to almost all configurations of all
130210284Sjmallett * chips.
131210284Sjmallett *
132210284Sjmallett * @param interface Interface to configure
133210284Sjmallett * @param num_ports Number of ports on the interface
134210284Sjmallett *
135210284Sjmallett * @return Zero on success, negative on failure
136210284Sjmallett */
137210284Sjmallettextern int __cvmx_helper_setup_gmx(int interface, int num_ports);
138210284Sjmallett
139210284Sjmallett/**
140232812Sjmallett * @INTERNAL
141232812Sjmallett * Get the number of ipd_ports on an interface.
142232812Sjmallett *
143232812Sjmallett * @param interface
144232812Sjmallett *
145232812Sjmallett * @return the number of ipd_ports on the interface and -1 for error.
146232812Sjmallett */
147232812Sjmallettextern int __cvmx_helper_get_num_ipd_ports(int interface);
148232812Sjmallett
149232812Sjmallett/**
150232812Sjmallett * @INTERNAL
151232812Sjmallett * Get the number of pko_ports on an interface.
152232812Sjmallett *
153232812Sjmallett * @param interface
154232812Sjmallett *
155232812Sjmallett * @return the number of pko_ports on the interface.
156232812Sjmallett */
157232812Sjmallettextern int __cvmx_helper_get_num_pko_ports(int interface);
158232812Sjmallett
159232812Sjmallett/*
160232812Sjmallett * @INTERNAL
161232812Sjmallett *
162232812Sjmallett * @param interface
163232812Sjmallett * @param port
164232812Sjmallett * @param link_info
165232812Sjmallett *
166232812Sjmallett * @return 0 for success and -1 for failure
167232812Sjmallett */
168232812Sjmallettextern int __cvmx_helper_set_link_info(int interface, int port,
169232812Sjmallett    cvmx_helper_link_info_t link_info);
170232812Sjmallett
171232812Sjmallett/**
172232812Sjmallett * @INTERNAL
173232812Sjmallett *
174232812Sjmallett * @param interface
175232812Sjmallett * @param port
176232812Sjmallett *
177232812Sjmallett * @return valid link_info on success or -1 on failure
178232812Sjmallett */
179232812Sjmallettextern cvmx_helper_link_info_t __cvmx_helper_get_link_info(int interface,
180232812Sjmallett    int port);
181232812Sjmallett
182232812Sjmallettenum cvmx_pko_padding {
183232812Sjmallett	CVMX_PKO_PADDING_NONE = 0,
184232812Sjmallett	CVMX_PKO_PADDING_60 = 1,
185232812Sjmallett};
186232812Sjmallett
187232812Sjmallett/**
188232812Sjmallett * @INTERNAL
189232812Sjmallett *
190232812Sjmallett * @param interface
191232812Sjmallett * @param num_ipd_ports is the number of ipd_ports on the interface
192232812Sjmallett * @param has_fcs indicates if PKO does FCS for the ports on this
193232812Sjmallett * @param pad The padding that PKO should apply.
194210284Sjmallett * interface.
195210284Sjmallett *
196232812Sjmallett * @return 0 for success and -1 for failure
197232812Sjmallett */
198232812Sjmallettextern int __cvmx_helper_init_interface(int interface, int num_ipd_ports, int has_fcs, enum cvmx_pko_padding pad);
199232812Sjmallett
200232812Sjmallett/**
201232812Sjmallett * @INTERNAL
202232812Sjmallett *
203232812Sjmallett * @param interface
204232812Sjmallett *
205232812Sjmallett * @return 0 if PKO does not do FCS and 1 otherwise.
206232812Sjmallett */
207232812Sjmallettextern int __cvmx_helper_get_has_fcs(int interface);
208232812Sjmallett
209232812Sjmallett
210232812Sjmallettextern enum cvmx_pko_padding __cvmx_helper_get_pko_padding(int interface);
211232812Sjmallett
212232812Sjmallett/**
213232812Sjmallett * Returns the IPD port number for a port on the given
214232812Sjmallett * interface.
215232812Sjmallett *
216210284Sjmallett * @param interface Interface to use
217210284Sjmallett * @param port      Port on the interface
218210284Sjmallett *
219232812Sjmallett * @return IPD port number
220210284Sjmallett */
221210284Sjmallettextern int cvmx_helper_get_ipd_port(int interface, int port);
222210284Sjmallett
223232812Sjmallett/**
224232812Sjmallett * Returns the PKO port number for a port on the given interface,
225232812Sjmallett * This is the base pko_port for o68 and ipd_port for older models.
226232812Sjmallett *
227232812Sjmallett * @param interface Interface to use
228232812Sjmallett * @param port      Port on the interface
229232812Sjmallett *
230232812Sjmallett * @return PKO port number and -1 on error.
231232812Sjmallett */
232232812Sjmallettextern int cvmx_helper_get_pko_port(int interface, int port);
233210284Sjmallett
234210284Sjmallett/**
235210284Sjmallett * Returns the IPD/PKO port number for the first port on the given
236210284Sjmallett * interface.
237210284Sjmallett *
238210284Sjmallett * @param interface Interface to use
239210284Sjmallett *
240210284Sjmallett * @return IPD/PKO port number
241210284Sjmallett */
242210284Sjmallettstatic inline int cvmx_helper_get_first_ipd_port(int interface)
243210284Sjmallett{
244210284Sjmallett    return (cvmx_helper_get_ipd_port (interface, 0));
245210284Sjmallett}
246210284Sjmallett
247210284Sjmallett/**
248210284Sjmallett * Returns the IPD/PKO port number for the last port on the given
249210284Sjmallett * interface.
250210284Sjmallett *
251210284Sjmallett * @param interface Interface to use
252210284Sjmallett *
253210284Sjmallett * @return IPD/PKO port number
254210284Sjmallett */
255210284Sjmallettstatic inline int cvmx_helper_get_last_ipd_port (int interface)
256210284Sjmallett{
257210284Sjmallett    return (cvmx_helper_get_first_ipd_port (interface) +
258210284Sjmallett  	    cvmx_helper_ports_on_interface (interface) - 1);
259210284Sjmallett}
260210284Sjmallett
261210284Sjmallett
262210284Sjmallett/**
263210284Sjmallett * Free the packet buffers contained in a work queue entry.
264210284Sjmallett * The work queue entry is not freed.
265210284Sjmallett *
266210284Sjmallett * @param work   Work queue entry with packet to free
267210284Sjmallett */
268210284Sjmallettstatic inline void cvmx_helper_free_packet_data(cvmx_wqe_t *work)
269210284Sjmallett{
270210284Sjmallett    uint64_t        number_buffers;
271210284Sjmallett    cvmx_buf_ptr_t  buffer_ptr;
272210284Sjmallett    cvmx_buf_ptr_t  next_buffer_ptr;
273210284Sjmallett    uint64_t        start_of_buffer;
274210284Sjmallett
275210284Sjmallett    number_buffers = work->word2.s.bufs;
276210284Sjmallett    if (number_buffers == 0)
277210284Sjmallett        return;
278210284Sjmallett    buffer_ptr = work->packet_ptr;
279210284Sjmallett
280210284Sjmallett    /* Since the number of buffers is not zero, we know this is not a dynamic
281210284Sjmallett        short packet. We need to check if it is a packet received with
282210284Sjmallett        IPD_CTL_STATUS[NO_WPTR]. If this is true, we need to free all buffers
283210284Sjmallett        except for the first one. The caller doesn't expect their WQE pointer
284210284Sjmallett        to be freed */
285210284Sjmallett    start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
286210284Sjmallett    if (cvmx_ptr_to_phys(work) == start_of_buffer)
287210284Sjmallett    {
288210284Sjmallett        next_buffer_ptr = *(cvmx_buf_ptr_t*)cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
289210284Sjmallett        buffer_ptr = next_buffer_ptr;
290210284Sjmallett        number_buffers--;
291210284Sjmallett    }
292210284Sjmallett
293210284Sjmallett    while (number_buffers--)
294210284Sjmallett    {
295210284Sjmallett        /* Remember the back pointer is in cache lines, not 64bit words */
296210284Sjmallett        start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
297210284Sjmallett        /* Read pointer to next buffer before we free the current buffer. */
298210284Sjmallett        next_buffer_ptr = *(cvmx_buf_ptr_t*)cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
299210284Sjmallett        cvmx_fpa_free(cvmx_phys_to_ptr(start_of_buffer), buffer_ptr.s.pool, 0);
300210284Sjmallett        buffer_ptr = next_buffer_ptr;
301210284Sjmallett    }
302210284Sjmallett}
303210284Sjmallett
304210284Sjmallett#endif /* CVMX_ENABLE_HELPER_FUNCTIONS */
305210284Sjmallett
306210284Sjmallett/**
307210284Sjmallett * Returns the interface number for an IPD/PKO port number.
308210284Sjmallett *
309210284Sjmallett * @param ipd_port IPD/PKO port number
310210284Sjmallett *
311210284Sjmallett * @return Interface number
312210284Sjmallett */
313210284Sjmallettextern int cvmx_helper_get_interface_num(int ipd_port);
314210284Sjmallett
315210284Sjmallett/**
316210284Sjmallett * Returns the interface index number for an IPD/PKO port
317210284Sjmallett * number.
318210284Sjmallett *
319210284Sjmallett * @param ipd_port IPD/PKO port number
320210284Sjmallett *
321210284Sjmallett * @return Interface index number
322210284Sjmallett */
323210284Sjmallettextern int cvmx_helper_get_interface_index_num(int ipd_port);
324210284Sjmallett
325232812Sjmallett/**
326232812Sjmallett * Get port kind for a given port in an interface.
327232812Sjmallett *
328232812Sjmallett * @param interface  Interface
329232812Sjmallett * @param port       index of the port in the interface
330232812Sjmallett *
331232812Sjmallett * @return port kind on sucicess  and -1 on failure
332232812Sjmallett */
333232812Sjmallettextern int cvmx_helper_get_pknd(int interface, int port);
334232812Sjmallett
335232812Sjmallett/**
336232812Sjmallett * Get bpid for a given port in an interface.
337232812Sjmallett *
338232812Sjmallett * @param interface  Interface
339232812Sjmallett * @param port       index of the port in the interface
340232812Sjmallett *
341232812Sjmallett * @return port kind on sucicess  and -1 on failure
342232812Sjmallett */
343232812Sjmallettextern int cvmx_helper_get_bpid(int interface, int port);
344232812Sjmallett
345232812Sjmallett
346232812Sjmallett/**
347232812Sjmallett * Internal functions.
348232812Sjmallett */
349232812Sjmallettextern int __cvmx_helper_post_init_interfaces(void);
350232812Sjmallettextern void __cvmx_helper_shutdown_interfaces(void);
351232812Sjmallett
352232812Sjmallettextern void cvmx_helper_show_stats(int port);
353232812Sjmallett
354210284Sjmallett#endif  /* __CVMX_HELPER_H__ */
355