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 * Helper functions for common, but complicated tasks.
50210284Sjmallett *
51232812Sjmallett * <hr>$Revision: 70030 $<hr>
52210284Sjmallett */
53210284Sjmallett
54210284Sjmallett#ifndef __CVMX_HELPER_H__
55210284Sjmallett#define __CVMX_HELPER_H__
56210284Sjmallett
57215990Sjmallett#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
58215990Sjmallett#include <asm/octeon/cvmx.h>
59215990Sjmallett#include <asm/octeon/cvmx-config.h>
60215990Sjmallett#elif !defined(CVMX_BUILD_FOR_FREEBSD_KERNEL)
61210284Sjmallett#include "executive-config.h"
62210284Sjmallett#include "cvmx-config.h"
63210311Sjmallett#endif
64215990Sjmallett
65210284Sjmallett#include "cvmx-fpa.h"
66210284Sjmallett#include "cvmx-wqe.h"
67210284Sjmallett
68232812Sjmallett#ifdef  __cplusplus
69210284Sjmallettextern "C" {
70210284Sjmallett#endif
71210284Sjmallett
72232812Sjmallett/* Max number of GMXX */
73232812Sjmallett#define CVMX_HELPER_MAX_GMX             (OCTEON_IS_MODEL(OCTEON_CN68XX) ? 5 : 2)
74232812Sjmallett
75232812Sjmallett#define CVMX_HELPER_CSR_INIT0           0       /* Do not change as
76232812Sjmallett                                                   CVMX_HELPER_WRITE_CSR()
77232812Sjmallett                                                   assumes it */
78232812Sjmallett#define CVMX_HELPER_CSR_INIT_READ       -1
79232812Sjmallett
80232812Sjmallett/*
81232812Sjmallett * CVMX_HELPER_WRITE_CSR--set a field in a CSR with a value.
82232812Sjmallett *
83232812Sjmallett * @param chcsr_init    intial value of the csr (CVMX_HELPER_CSR_INIT_READ
84232812Sjmallett *                      means to use the existing csr value as the
85232812Sjmallett *                      initial value.)
86232812Sjmallett * @param chcsr_csr     the name of the csr
87232812Sjmallett * @param chcsr_type    the type of the csr (see the -defs.h)
88232812Sjmallett * @param chcsr_chip    the chip for the csr/field
89232812Sjmallett * @param chcsr_fld     the field in the csr
90232812Sjmallett * @param chcsr_val     the value for field
91232812Sjmallett */
92232812Sjmallett#define CVMX_HELPER_WRITE_CSR(chcsr_init, chcsr_csr, chcsr_type,        \
93232812Sjmallett    chcsr_chip, chcsr_fld, chcsr_val)                                   \
94232812Sjmallett        do {                                                            \
95232812Sjmallett                chcsr_type csr;                                         \
96232812Sjmallett                if ((chcsr_init) == CVMX_HELPER_CSR_INIT_READ)          \
97232812Sjmallett                        csr.u64 = cvmx_read_csr(chcsr_csr);             \
98232812Sjmallett                else                                                    \
99232812Sjmallett                        csr.u64 = (chcsr_init);                         \
100232812Sjmallett                csr.chcsr_chip.chcsr_fld = (chcsr_val);                 \
101232812Sjmallett                cvmx_write_csr((chcsr_csr), csr.u64);                   \
102232812Sjmallett        } while(0)
103232812Sjmallett
104232812Sjmallett/*
105232812Sjmallett * CVMX_HELPER_WRITE_CSR0--set a field in a CSR with the initial value of 0
106232812Sjmallett */
107232812Sjmallett#define CVMX_HELPER_WRITE_CSR0(chcsr_csr, chcsr_type, chcsr_chip,       \
108232812Sjmallett    chcsr_fld, chcsr_val)                                               \
109232812Sjmallett        CVMX_HELPER_WRITE_CSR(CVMX_HELPER_CSR_INIT0, chcsr_csr,         \
110232812Sjmallett            chcsr_type, chcsr_chip, chcsr_fld, chcsr_val)
111232812Sjmallett
112232812Sjmallett/*
113232812Sjmallett * CVMX_HELPER_WRITE_CSR1--set a field in a CSR with the initial value of
114232812Sjmallett *                      the CSR's current value.
115232812Sjmallett */
116232812Sjmallett#define CVMX_HELPER_WRITE_CSR1(chcsr_csr, chcsr_type, chcsr_chip,       \
117232812Sjmallett    chcsr_fld, chcsr_val)                                               \
118232812Sjmallett        CVMX_HELPER_WRITE_CSR(CVMX_HELPER_CSR_INIT_READ, chcsr_csr,     \
119232812Sjmallett            chcsr_type, chcsr_chip, chcsr_fld, chcsr_val)
120232812Sjmallett
121232812Sjmallett
122210284Sjmalletttypedef enum
123210284Sjmallett{
124210284Sjmallett    CVMX_HELPER_INTERFACE_MODE_DISABLED,
125210284Sjmallett    CVMX_HELPER_INTERFACE_MODE_RGMII,
126210284Sjmallett    CVMX_HELPER_INTERFACE_MODE_GMII,
127210284Sjmallett    CVMX_HELPER_INTERFACE_MODE_SPI,
128210284Sjmallett    CVMX_HELPER_INTERFACE_MODE_PCIE,
129210284Sjmallett    CVMX_HELPER_INTERFACE_MODE_XAUI,
130210284Sjmallett    CVMX_HELPER_INTERFACE_MODE_SGMII,
131210284Sjmallett    CVMX_HELPER_INTERFACE_MODE_PICMG,
132210284Sjmallett    CVMX_HELPER_INTERFACE_MODE_NPI,
133210284Sjmallett    CVMX_HELPER_INTERFACE_MODE_LOOP,
134215990Sjmallett    CVMX_HELPER_INTERFACE_MODE_SRIO,
135232812Sjmallett    CVMX_HELPER_INTERFACE_MODE_ILK,
136232812Sjmallett    CVMX_HELPER_INTERFACE_MODE_RXAUI,
137210284Sjmallett} cvmx_helper_interface_mode_t;
138210284Sjmallett
139210284Sjmalletttypedef union
140210284Sjmallett{
141210284Sjmallett    uint64_t u64;
142210284Sjmallett    struct
143210284Sjmallett    {
144210284Sjmallett        uint64_t    reserved_20_63  : 44;
145210284Sjmallett        uint64_t    link_up         : 1;    /**< Is the physical link up? */
146210284Sjmallett        uint64_t    full_duplex     : 1;    /**< 1 if the link is full duplex */
147210284Sjmallett        uint64_t    speed           : 18;   /**< Speed of the link in Mbps */
148210284Sjmallett    } s;
149210284Sjmallett} cvmx_helper_link_info_t;
150210284Sjmallett
151210284Sjmallett#include "cvmx-helper-fpa.h"
152210284Sjmallett
153210284Sjmallett#ifdef CVMX_ENABLE_PKO_FUNCTIONS
154210284Sjmallett
155210284Sjmallett#include "cvmx-helper-errata.h"
156232812Sjmallett#include "cvmx-helper-ilk.h"
157210284Sjmallett#include "cvmx-helper-loop.h"
158210284Sjmallett#include "cvmx-helper-npi.h"
159210284Sjmallett#include "cvmx-helper-rgmii.h"
160210284Sjmallett#include "cvmx-helper-sgmii.h"
161210284Sjmallett#include "cvmx-helper-spi.h"
162215990Sjmallett#include "cvmx-helper-srio.h"
163210284Sjmallett#include "cvmx-helper-xaui.h"
164210284Sjmallett
165210284Sjmallett/**
166210284Sjmallett * cvmx_override_pko_queue_priority(int ipd_port, uint64_t
167210284Sjmallett * priorities[16]) is a function pointer. It is meant to allow
168210284Sjmallett * customization of the PKO queue priorities based on the port
169210284Sjmallett * number. Users should set this pointer to a function before
170210284Sjmallett * calling any cvmx-helper operations.
171210284Sjmallett */
172232812Sjmallettextern CVMX_SHARED void (*cvmx_override_pko_queue_priority)(int ipd_port, uint64_t *priorities);
173210284Sjmallett
174210284Sjmallett/**
175210284Sjmallett * cvmx_override_ipd_port_setup(int ipd_port) is a function
176232812Sjmallett * pointer. It is meant to allow customization of the IPD port/port kind
177210284Sjmallett * setup before packet input/output comes online. It is called
178210284Sjmallett * after cvmx-helper does the default IPD configuration, but
179210284Sjmallett * before IPD is enabled. Users should set this pointer to a
180210284Sjmallett * function before calling any cvmx-helper operations.
181210284Sjmallett */
182232812Sjmallettextern CVMX_SHARED void (*cvmx_override_ipd_port_setup)(int ipd_port);
183210284Sjmallett
184210284Sjmallett/**
185210284Sjmallett * This function enables the IPD and also enables the packet interfaces.
186210284Sjmallett * The packet interfaces (RGMII and SPI) must be enabled after the
187210284Sjmallett * IPD.  This should be called by the user program after any additional
188210284Sjmallett * IPD configuration changes are made if CVMX_HELPER_ENABLE_IPD
189210284Sjmallett * is not set in the executive-config.h file.
190210284Sjmallett *
191210284Sjmallett * @return 0 on success
192210284Sjmallett *         -1 on failure
193210284Sjmallett */
194210284Sjmallettextern int cvmx_helper_ipd_and_packet_input_enable(void);
195210284Sjmallett
196210284Sjmallett/**
197232812Sjmallett * Initialize and allocate memory for the SSO.
198232812Sjmallett *
199232812Sjmallett * @param wqe_entries The maximum number of work queue entries to be
200232812Sjmallett * supported.
201232812Sjmallett *
202232812Sjmallett * @return Zero on success, non-zero on failure.
203232812Sjmallett */
204232812Sjmallettextern int cvmx_helper_initialize_sso(int wqe_entries);
205232812Sjmallett
206232812Sjmallett/**
207232812Sjmallett * Undo the effect of cvmx_helper_initialize_sso().
208232812Sjmallett *
209232812Sjmallett * Warning: since cvmx_bootmem_alloc() memory cannot be freed, the
210232812Sjmallett * memory allocated by cvmx_helper_initialize_sso() will be leaked.
211232812Sjmallett *
212232812Sjmallett * @return Zero on success, non-zero on failure.
213232812Sjmallett */
214232812Sjmallettextern int cvmx_helper_uninitialize_sso(void);
215232812Sjmallett
216232812Sjmallett/**
217210284Sjmallett * Initialize the PIP, IPD, and PKO hardware to support
218210284Sjmallett * simple priority based queues for the ethernet ports. Each
219210284Sjmallett * port is configured with a number of priority queues based
220210284Sjmallett * on CVMX_PKO_QUEUES_PER_PORT_* where each queue is lower
221210284Sjmallett * priority than the previous.
222210284Sjmallett *
223210284Sjmallett * @return Zero on success, non-zero on failure
224210284Sjmallett */
225210284Sjmallettextern int cvmx_helper_initialize_packet_io_global(void);
226210284Sjmallett
227210284Sjmallett/**
228210284Sjmallett * Does core local initialization for packet io
229210284Sjmallett *
230210284Sjmallett * @return Zero on success, non-zero on failure
231210284Sjmallett */
232210284Sjmallettextern int cvmx_helper_initialize_packet_io_local(void);
233210284Sjmallett
234210284Sjmallett/**
235215990Sjmallett * Undo the initialization performed in
236215990Sjmallett * cvmx_helper_initialize_packet_io_global(). After calling this routine and the
237215990Sjmallett * local version on each core, packet IO for Octeon will be disabled and placed
238215990Sjmallett * in the initial reset state. It will then be safe to call the initialize
239215990Sjmallett * later on. Note that this routine does not empty the FPA pools. It frees all
240215990Sjmallett * buffers used by the packet IO hardware to the FPA so a function emptying the
241215990Sjmallett * FPA after shutdown should find all packet buffers in the FPA.
242215990Sjmallett *
243215990Sjmallett * @return Zero on success, negative on failure.
244215990Sjmallett */
245215990Sjmallettextern int cvmx_helper_shutdown_packet_io_global(void);
246215990Sjmallett
247215990Sjmallett/**
248215990Sjmallett * Does core local shutdown of packet io
249215990Sjmallett *
250215990Sjmallett * @return Zero on success, non-zero on failure
251215990Sjmallett */
252215990Sjmallettextern int cvmx_helper_shutdown_packet_io_local(void);
253215990Sjmallett
254215990Sjmallett/**
255210284Sjmallett * Returns the number of ports on the given interface.
256210284Sjmallett * The interface must be initialized before the port count
257210284Sjmallett * can be returned.
258210284Sjmallett *
259210284Sjmallett * @param interface Which interface to return port count for.
260210284Sjmallett *
261210284Sjmallett * @return Port count for interface
262210284Sjmallett *         -1 for uninitialized interface
263210284Sjmallett */
264210284Sjmallettextern int cvmx_helper_ports_on_interface(int interface);
265210284Sjmallett
266210284Sjmallett/**
267210284Sjmallett * Return the number of interfaces the chip has. Each interface
268210284Sjmallett * may have multiple ports. Most chips support two interfaces,
269210284Sjmallett * but the CNX0XX and CNX1XX are exceptions. These only support
270210284Sjmallett * one interface.
271210284Sjmallett *
272210284Sjmallett * @return Number of interfaces on chip
273210284Sjmallett */
274210284Sjmallettextern int cvmx_helper_get_number_of_interfaces(void);
275210284Sjmallett
276210284Sjmallett/**
277210284Sjmallett * Get the operating mode of an interface. Depending on the Octeon
278210284Sjmallett * chip and configuration, this function returns an enumeration
279210284Sjmallett * of the type of packet I/O supported by an interface.
280210284Sjmallett *
281210284Sjmallett * @param interface Interface to probe
282210284Sjmallett *
283210284Sjmallett * @return Mode of the interface. Unknown or unsupported interfaces return
284210284Sjmallett *         DISABLED.
285210284Sjmallett */
286210284Sjmallettextern cvmx_helper_interface_mode_t cvmx_helper_interface_get_mode(int interface);
287210284Sjmallett
288210284Sjmallett/**
289210284Sjmallett * Auto configure an IPD/PKO port link state and speed. This
290210284Sjmallett * function basically does the equivalent of:
291210284Sjmallett * cvmx_helper_link_set(ipd_port, cvmx_helper_link_get(ipd_port));
292210284Sjmallett *
293210284Sjmallett * @param ipd_port IPD/PKO port to auto configure
294210284Sjmallett *
295210284Sjmallett * @return Link state after configure
296210284Sjmallett */
297210284Sjmallettextern cvmx_helper_link_info_t cvmx_helper_link_autoconf(int ipd_port);
298210284Sjmallett
299210284Sjmallett/**
300210284Sjmallett * Return the link state of an IPD/PKO port as returned by
301210284Sjmallett * auto negotiation. The result of this function may not match
302210284Sjmallett * Octeon's link config if auto negotiation has changed since
303210284Sjmallett * the last call to cvmx_helper_link_set().
304210284Sjmallett *
305210284Sjmallett * @param ipd_port IPD/PKO port to query
306210284Sjmallett *
307210284Sjmallett * @return Link state
308210284Sjmallett */
309210284Sjmallettextern cvmx_helper_link_info_t cvmx_helper_link_get(int ipd_port);
310210284Sjmallett
311210284Sjmallett/**
312210284Sjmallett * Configure an IPD/PKO port for the specified link state. This
313210284Sjmallett * function does not influence auto negotiation at the PHY level.
314210284Sjmallett * The passed link state must always match the link state returned
315210284Sjmallett * by cvmx_helper_link_get(). It is normally best to use
316210284Sjmallett * cvmx_helper_link_autoconf() instead.
317210284Sjmallett *
318210284Sjmallett * @param ipd_port  IPD/PKO port to configure
319210284Sjmallett * @param link_info The new link state
320210284Sjmallett *
321210284Sjmallett * @return Zero on success, negative on failure
322210284Sjmallett */
323210284Sjmallettextern int cvmx_helper_link_set(int ipd_port, cvmx_helper_link_info_t link_info);
324210284Sjmallett
325210284Sjmallett
326210284Sjmallett
327210284Sjmallett/**
328232812Sjmallett * This function probes an interface to determine the actual number of
329232812Sjmallett * hardware ports connected to it. It does some setup the ports but
330232812Sjmallett * doesn't enable them. The main goal here is to set the global
331232812Sjmallett * interface_port_count[interface] correctly. Final hardware setup of
332232812Sjmallett * the ports will be performed later.
333210284Sjmallett *
334210284Sjmallett * @param interface Interface to probe
335210284Sjmallett *
336210284Sjmallett * @return Zero on success, negative on failure
337210284Sjmallett */
338210284Sjmallettextern int cvmx_helper_interface_probe(int interface);
339210284Sjmallett
340210284Sjmallett/**
341232812Sjmallett * Determine the actual number of hardware ports connected to an
342232812Sjmallett * interface. It doesn't setup the ports or enable them.
343232812Sjmallett *
344232812Sjmallett * @param interface Interface to enumerate
345232812Sjmallett *
346232812Sjmallett * @return Zero on success, negative on failure
347232812Sjmallett */
348232812Sjmallettextern int cvmx_helper_interface_enumerate(int interface);
349232812Sjmallett
350232812Sjmallett/**
351210284Sjmallett * Configure a port for internal and/or external loopback. Internal loopback
352210284Sjmallett * causes packets sent by the port to be received by Octeon. External loopback
353210284Sjmallett * causes packets received from the wire to sent out again.
354210284Sjmallett *
355210284Sjmallett * @param ipd_port IPD/PKO port to loopback.
356210284Sjmallett * @param enable_internal
357210284Sjmallett *                 Non zero if you want internal loopback
358210284Sjmallett * @param enable_external
359210284Sjmallett *                 Non zero if you want external loopback
360210284Sjmallett *
361210284Sjmallett * @return Zero on success, negative on failure.
362210284Sjmallett */
363210284Sjmallettextern int cvmx_helper_configure_loopback(int ipd_port, int enable_internal, int enable_external);
364210284Sjmallett
365210311Sjmallett#include "cvmx-helper-util.h"
366210311Sjmallett
367210284Sjmallett#endif /* CVMX_ENABLE_PKO_FUNCTIONS */
368210284Sjmallett
369232812Sjmallett#ifdef  __cplusplus
370210284Sjmallett}
371210284Sjmallett#endif
372210284Sjmallett
373210284Sjmallett#endif  /* __CVMX_HELPER_H__ */
374