162306a36Sopenharmony_ci/***********************license start***************
262306a36Sopenharmony_ci * Author: Cavium Networks
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Contact: support@caviumnetworks.com
562306a36Sopenharmony_ci * This file is part of the OCTEON SDK
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (c) 2003-2008 Cavium Networks
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This file is free software; you can redistribute it and/or modify
1062306a36Sopenharmony_ci * it under the terms of the GNU General Public License, Version 2, as
1162306a36Sopenharmony_ci * published by the Free Software Foundation.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * This file is distributed in the hope that it will be useful, but
1462306a36Sopenharmony_ci * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
1562306a36Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
1662306a36Sopenharmony_ci * NONINFRINGEMENT.  See the GNU General Public License for more
1762306a36Sopenharmony_ci * details.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
2062306a36Sopenharmony_ci * along with this file; if not, write to the Free Software
2162306a36Sopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2262306a36Sopenharmony_ci * or visit http://www.gnu.org/licenses/.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * This file may also be available under a different license from Cavium.
2562306a36Sopenharmony_ci * Contact Cavium Networks for more information
2662306a36Sopenharmony_ci ***********************license end**************************************/
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci *
3062306a36Sopenharmony_ci * Small helper utilities.
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#ifndef __CVMX_HELPER_UTIL_H__
3562306a36Sopenharmony_ci#define __CVMX_HELPER_UTIL_H__
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/**
3862306a36Sopenharmony_ci * Convert a interface mode into a human readable string
3962306a36Sopenharmony_ci *
4062306a36Sopenharmony_ci * @mode:   Mode to convert
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * Returns String
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_ciextern const char
4562306a36Sopenharmony_ci    *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t mode);
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/**
4862306a36Sopenharmony_ci * Setup Random Early Drop to automatically begin dropping packets.
4962306a36Sopenharmony_ci *
5062306a36Sopenharmony_ci * @pass_thresh:
5162306a36Sopenharmony_ci *		 Packets will begin slowly dropping when there are less than
5262306a36Sopenharmony_ci *		 this many packet buffers free in FPA 0.
5362306a36Sopenharmony_ci * @drop_thresh:
5462306a36Sopenharmony_ci *		 All incoming packets will be dropped when there are less
5562306a36Sopenharmony_ci *		 than this many free packet buffers in FPA 0.
5662306a36Sopenharmony_ci * Returns Zero on success. Negative on failure
5762306a36Sopenharmony_ci */
5862306a36Sopenharmony_ciextern int cvmx_helper_setup_red(int pass_thresh, int drop_thresh);
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci/**
6162306a36Sopenharmony_ci * Get the version of the CVMX libraries.
6262306a36Sopenharmony_ci *
6362306a36Sopenharmony_ci * Returns Version string. Note this buffer is allocated statically
6462306a36Sopenharmony_ci *	   and will be shared by all callers.
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_ciextern const char *cvmx_helper_get_version(void);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci/**
6962306a36Sopenharmony_ci * Setup the common GMX settings that determine the number of
7062306a36Sopenharmony_ci * ports. These setting apply to almost all configurations of all
7162306a36Sopenharmony_ci * chips.
7262306a36Sopenharmony_ci *
7362306a36Sopenharmony_ci * @interface: Interface to configure
7462306a36Sopenharmony_ci * @num_ports: Number of ports on the interface
7562306a36Sopenharmony_ci *
7662306a36Sopenharmony_ci * Returns Zero on success, negative on failure
7762306a36Sopenharmony_ci */
7862306a36Sopenharmony_ciextern int __cvmx_helper_setup_gmx(int interface, int num_ports);
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci/**
8162306a36Sopenharmony_ci * Returns the IPD/PKO port number for a port on the given
8262306a36Sopenharmony_ci * interface.
8362306a36Sopenharmony_ci *
8462306a36Sopenharmony_ci * @interface: Interface to use
8562306a36Sopenharmony_ci * @port:      Port on the interface
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci * Returns IPD/PKO port number
8862306a36Sopenharmony_ci */
8962306a36Sopenharmony_ciextern int cvmx_helper_get_ipd_port(int interface, int port);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci/**
9262306a36Sopenharmony_ci * Returns the IPD/PKO port number for the first port on the given
9362306a36Sopenharmony_ci * interface.
9462306a36Sopenharmony_ci *
9562306a36Sopenharmony_ci * @interface: Interface to use
9662306a36Sopenharmony_ci *
9762306a36Sopenharmony_ci * Returns IPD/PKO port number
9862306a36Sopenharmony_ci */
9962306a36Sopenharmony_cistatic inline int cvmx_helper_get_first_ipd_port(int interface)
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci	return cvmx_helper_get_ipd_port(interface, 0);
10262306a36Sopenharmony_ci}
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci/**
10562306a36Sopenharmony_ci * Returns the IPD/PKO port number for the last port on the given
10662306a36Sopenharmony_ci * interface.
10762306a36Sopenharmony_ci *
10862306a36Sopenharmony_ci * @interface: Interface to use
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci * Returns IPD/PKO port number
11162306a36Sopenharmony_ci */
11262306a36Sopenharmony_cistatic inline int cvmx_helper_get_last_ipd_port(int interface)
11362306a36Sopenharmony_ci{
11462306a36Sopenharmony_ci	extern int cvmx_helper_ports_on_interface(int interface);
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	return cvmx_helper_get_first_ipd_port(interface) +
11762306a36Sopenharmony_ci	       cvmx_helper_ports_on_interface(interface) - 1;
11862306a36Sopenharmony_ci}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci/**
12162306a36Sopenharmony_ci * Free the packet buffers contained in a work queue entry.
12262306a36Sopenharmony_ci * The work queue entry is not freed.
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci * @work:   Work queue entry with packet to free
12562306a36Sopenharmony_ci */
12662306a36Sopenharmony_cistatic inline void cvmx_helper_free_packet_data(struct cvmx_wqe *work)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	uint64_t number_buffers;
12962306a36Sopenharmony_ci	union cvmx_buf_ptr buffer_ptr;
13062306a36Sopenharmony_ci	union cvmx_buf_ptr next_buffer_ptr;
13162306a36Sopenharmony_ci	uint64_t start_of_buffer;
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	number_buffers = work->word2.s.bufs;
13462306a36Sopenharmony_ci	if (number_buffers == 0)
13562306a36Sopenharmony_ci		return;
13662306a36Sopenharmony_ci	buffer_ptr = work->packet_ptr;
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	/*
13962306a36Sopenharmony_ci	 * Since the number of buffers is not zero, we know this is
14062306a36Sopenharmony_ci	 * not a dynamic short packet. We need to check if it is a
14162306a36Sopenharmony_ci	 * packet received with IPD_CTL_STATUS[NO_WPTR]. If this is
14262306a36Sopenharmony_ci	 * true, we need to free all buffers except for the first
14362306a36Sopenharmony_ci	 * one. The caller doesn't expect their WQE pointer to be
14462306a36Sopenharmony_ci	 * freed
14562306a36Sopenharmony_ci	 */
14662306a36Sopenharmony_ci	start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
14762306a36Sopenharmony_ci	if (cvmx_ptr_to_phys(work) == start_of_buffer) {
14862306a36Sopenharmony_ci		next_buffer_ptr =
14962306a36Sopenharmony_ci		    *(union cvmx_buf_ptr *) cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
15062306a36Sopenharmony_ci		buffer_ptr = next_buffer_ptr;
15162306a36Sopenharmony_ci		number_buffers--;
15262306a36Sopenharmony_ci	}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	while (number_buffers--) {
15562306a36Sopenharmony_ci		/*
15662306a36Sopenharmony_ci		 * Remember the back pointer is in cache lines, not
15762306a36Sopenharmony_ci		 * 64bit words
15862306a36Sopenharmony_ci		 */
15962306a36Sopenharmony_ci		start_of_buffer =
16062306a36Sopenharmony_ci		    ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
16162306a36Sopenharmony_ci		/*
16262306a36Sopenharmony_ci		 * Read pointer to next buffer before we free the
16362306a36Sopenharmony_ci		 * current buffer.
16462306a36Sopenharmony_ci		 */
16562306a36Sopenharmony_ci		next_buffer_ptr =
16662306a36Sopenharmony_ci		    *(union cvmx_buf_ptr *) cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
16762306a36Sopenharmony_ci		cvmx_fpa_free(cvmx_phys_to_ptr(start_of_buffer),
16862306a36Sopenharmony_ci			      buffer_ptr.s.pool, 0);
16962306a36Sopenharmony_ci		buffer_ptr = next_buffer_ptr;
17062306a36Sopenharmony_ci	}
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci/**
17462306a36Sopenharmony_ci * Returns the interface number for an IPD/PKO port number.
17562306a36Sopenharmony_ci *
17662306a36Sopenharmony_ci * @ipd_port: IPD/PKO port number
17762306a36Sopenharmony_ci *
17862306a36Sopenharmony_ci * Returns Interface number
17962306a36Sopenharmony_ci */
18062306a36Sopenharmony_ciextern int cvmx_helper_get_interface_num(int ipd_port);
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci/**
18362306a36Sopenharmony_ci * Returns the interface index number for an IPD/PKO port
18462306a36Sopenharmony_ci * number.
18562306a36Sopenharmony_ci *
18662306a36Sopenharmony_ci * @ipd_port: IPD/PKO port number
18762306a36Sopenharmony_ci *
18862306a36Sopenharmony_ci * Returns Interface index number
18962306a36Sopenharmony_ci */
19062306a36Sopenharmony_ciextern int cvmx_helper_get_interface_index_num(int ipd_port);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci#endif /* __CVMX_HELPER_H__ */
193