18c2ecf20Sopenharmony_ci/***********************license start*************** 28c2ecf20Sopenharmony_ci * Author: Cavium Networks 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Contact: support@caviumnetworks.com 58c2ecf20Sopenharmony_ci * This file is part of the OCTEON SDK 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright (c) 2003-2008 Cavium Networks 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This file is free software; you can redistribute it and/or modify 108c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License, Version 2, as 118c2ecf20Sopenharmony_ci * published by the Free Software Foundation. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * This file is distributed in the hope that it will be useful, but 148c2ecf20Sopenharmony_ci * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty 158c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or 168c2ecf20Sopenharmony_ci * NONINFRINGEMENT. See the GNU General Public License for more 178c2ecf20Sopenharmony_ci * details. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License 208c2ecf20Sopenharmony_ci * along with this file; if not, write to the Free Software 218c2ecf20Sopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 228c2ecf20Sopenharmony_ci * or visit http://www.gnu.org/licenses/. 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * This file may also be available under a different license from Cavium. 258c2ecf20Sopenharmony_ci * Contact Cavium Networks for more information 268c2ecf20Sopenharmony_ci ***********************license end**************************************/ 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/* 298c2ecf20Sopenharmony_ci * 308c2ecf20Sopenharmony_ci * Small helper utilities. 318c2ecf20Sopenharmony_ci * 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#ifndef __CVMX_HELPER_UTIL_H__ 358c2ecf20Sopenharmony_ci#define __CVMX_HELPER_UTIL_H__ 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/** 388c2ecf20Sopenharmony_ci * Convert a interface mode into a human readable string 398c2ecf20Sopenharmony_ci * 408c2ecf20Sopenharmony_ci * @mode: Mode to convert 418c2ecf20Sopenharmony_ci * 428c2ecf20Sopenharmony_ci * Returns String 438c2ecf20Sopenharmony_ci */ 448c2ecf20Sopenharmony_ciextern const char 458c2ecf20Sopenharmony_ci *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t mode); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci/** 488c2ecf20Sopenharmony_ci * Setup Random Early Drop to automatically begin dropping packets. 498c2ecf20Sopenharmony_ci * 508c2ecf20Sopenharmony_ci * @pass_thresh: 518c2ecf20Sopenharmony_ci * Packets will begin slowly dropping when there are less than 528c2ecf20Sopenharmony_ci * this many packet buffers free in FPA 0. 538c2ecf20Sopenharmony_ci * @drop_thresh: 548c2ecf20Sopenharmony_ci * All incoming packets will be dropped when there are less 558c2ecf20Sopenharmony_ci * than this many free packet buffers in FPA 0. 568c2ecf20Sopenharmony_ci * Returns Zero on success. Negative on failure 578c2ecf20Sopenharmony_ci */ 588c2ecf20Sopenharmony_ciextern int cvmx_helper_setup_red(int pass_thresh, int drop_thresh); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/** 618c2ecf20Sopenharmony_ci * Get the version of the CVMX libraries. 628c2ecf20Sopenharmony_ci * 638c2ecf20Sopenharmony_ci * Returns Version string. Note this buffer is allocated statically 648c2ecf20Sopenharmony_ci * and will be shared by all callers. 658c2ecf20Sopenharmony_ci */ 668c2ecf20Sopenharmony_ciextern const char *cvmx_helper_get_version(void); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci/** 698c2ecf20Sopenharmony_ci * Setup the common GMX settings that determine the number of 708c2ecf20Sopenharmony_ci * ports. These setting apply to almost all configurations of all 718c2ecf20Sopenharmony_ci * chips. 728c2ecf20Sopenharmony_ci * 738c2ecf20Sopenharmony_ci * @interface: Interface to configure 748c2ecf20Sopenharmony_ci * @num_ports: Number of ports on the interface 758c2ecf20Sopenharmony_ci * 768c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure 778c2ecf20Sopenharmony_ci */ 788c2ecf20Sopenharmony_ciextern int __cvmx_helper_setup_gmx(int interface, int num_ports); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci/** 818c2ecf20Sopenharmony_ci * Returns the IPD/PKO port number for a port on the given 828c2ecf20Sopenharmony_ci * interface. 838c2ecf20Sopenharmony_ci * 848c2ecf20Sopenharmony_ci * @interface: Interface to use 858c2ecf20Sopenharmony_ci * @port: Port on the interface 868c2ecf20Sopenharmony_ci * 878c2ecf20Sopenharmony_ci * Returns IPD/PKO port number 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_ciextern int cvmx_helper_get_ipd_port(int interface, int port); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/** 928c2ecf20Sopenharmony_ci * Returns the IPD/PKO port number for the first port on the given 938c2ecf20Sopenharmony_ci * interface. 948c2ecf20Sopenharmony_ci * 958c2ecf20Sopenharmony_ci * @interface: Interface to use 968c2ecf20Sopenharmony_ci * 978c2ecf20Sopenharmony_ci * Returns IPD/PKO port number 988c2ecf20Sopenharmony_ci */ 998c2ecf20Sopenharmony_cistatic inline int cvmx_helper_get_first_ipd_port(int interface) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci return cvmx_helper_get_ipd_port(interface, 0); 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci/** 1058c2ecf20Sopenharmony_ci * Returns the IPD/PKO port number for the last port on the given 1068c2ecf20Sopenharmony_ci * interface. 1078c2ecf20Sopenharmony_ci * 1088c2ecf20Sopenharmony_ci * @interface: Interface to use 1098c2ecf20Sopenharmony_ci * 1108c2ecf20Sopenharmony_ci * Returns IPD/PKO port number 1118c2ecf20Sopenharmony_ci */ 1128c2ecf20Sopenharmony_cistatic inline int cvmx_helper_get_last_ipd_port(int interface) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci extern int cvmx_helper_ports_on_interface(int interface); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci return cvmx_helper_get_first_ipd_port(interface) + 1178c2ecf20Sopenharmony_ci cvmx_helper_ports_on_interface(interface) - 1; 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci/** 1218c2ecf20Sopenharmony_ci * Free the packet buffers contained in a work queue entry. 1228c2ecf20Sopenharmony_ci * The work queue entry is not freed. 1238c2ecf20Sopenharmony_ci * 1248c2ecf20Sopenharmony_ci * @work: Work queue entry with packet to free 1258c2ecf20Sopenharmony_ci */ 1268c2ecf20Sopenharmony_cistatic inline void cvmx_helper_free_packet_data(struct cvmx_wqe *work) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci uint64_t number_buffers; 1298c2ecf20Sopenharmony_ci union cvmx_buf_ptr buffer_ptr; 1308c2ecf20Sopenharmony_ci union cvmx_buf_ptr next_buffer_ptr; 1318c2ecf20Sopenharmony_ci uint64_t start_of_buffer; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci number_buffers = work->word2.s.bufs; 1348c2ecf20Sopenharmony_ci if (number_buffers == 0) 1358c2ecf20Sopenharmony_ci return; 1368c2ecf20Sopenharmony_ci buffer_ptr = work->packet_ptr; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci /* 1398c2ecf20Sopenharmony_ci * Since the number of buffers is not zero, we know this is 1408c2ecf20Sopenharmony_ci * not a dynamic short packet. We need to check if it is a 1418c2ecf20Sopenharmony_ci * packet received with IPD_CTL_STATUS[NO_WPTR]. If this is 1428c2ecf20Sopenharmony_ci * true, we need to free all buffers except for the first 1438c2ecf20Sopenharmony_ci * one. The caller doesn't expect their WQE pointer to be 1448c2ecf20Sopenharmony_ci * freed 1458c2ecf20Sopenharmony_ci */ 1468c2ecf20Sopenharmony_ci start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7; 1478c2ecf20Sopenharmony_ci if (cvmx_ptr_to_phys(work) == start_of_buffer) { 1488c2ecf20Sopenharmony_ci next_buffer_ptr = 1498c2ecf20Sopenharmony_ci *(union cvmx_buf_ptr *) cvmx_phys_to_ptr(buffer_ptr.s.addr - 8); 1508c2ecf20Sopenharmony_ci buffer_ptr = next_buffer_ptr; 1518c2ecf20Sopenharmony_ci number_buffers--; 1528c2ecf20Sopenharmony_ci } 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci while (number_buffers--) { 1558c2ecf20Sopenharmony_ci /* 1568c2ecf20Sopenharmony_ci * Remember the back pointer is in cache lines, not 1578c2ecf20Sopenharmony_ci * 64bit words 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_ci start_of_buffer = 1608c2ecf20Sopenharmony_ci ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7; 1618c2ecf20Sopenharmony_ci /* 1628c2ecf20Sopenharmony_ci * Read pointer to next buffer before we free the 1638c2ecf20Sopenharmony_ci * current buffer. 1648c2ecf20Sopenharmony_ci */ 1658c2ecf20Sopenharmony_ci next_buffer_ptr = 1668c2ecf20Sopenharmony_ci *(union cvmx_buf_ptr *) cvmx_phys_to_ptr(buffer_ptr.s.addr - 8); 1678c2ecf20Sopenharmony_ci cvmx_fpa_free(cvmx_phys_to_ptr(start_of_buffer), 1688c2ecf20Sopenharmony_ci buffer_ptr.s.pool, 0); 1698c2ecf20Sopenharmony_ci buffer_ptr = next_buffer_ptr; 1708c2ecf20Sopenharmony_ci } 1718c2ecf20Sopenharmony_ci} 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci/** 1748c2ecf20Sopenharmony_ci * Returns the interface number for an IPD/PKO port number. 1758c2ecf20Sopenharmony_ci * 1768c2ecf20Sopenharmony_ci * @ipd_port: IPD/PKO port number 1778c2ecf20Sopenharmony_ci * 1788c2ecf20Sopenharmony_ci * Returns Interface number 1798c2ecf20Sopenharmony_ci */ 1808c2ecf20Sopenharmony_ciextern int cvmx_helper_get_interface_num(int ipd_port); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci/** 1838c2ecf20Sopenharmony_ci * Returns the interface index number for an IPD/PKO port 1848c2ecf20Sopenharmony_ci * number. 1858c2ecf20Sopenharmony_ci * 1868c2ecf20Sopenharmony_ci * @ipd_port: IPD/PKO port number 1878c2ecf20Sopenharmony_ci * 1888c2ecf20Sopenharmony_ci * Returns Interface index number 1898c2ecf20Sopenharmony_ci */ 1908c2ecf20Sopenharmony_ciextern int cvmx_helper_get_interface_index_num(int ipd_port); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci#endif /* __CVMX_HELPER_H__ */ 193