1139749Simp/***********************license start*************** 233323Sphk * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights 333323Sphk * reserved. 433323Sphk * 533323Sphk * 633323Sphk * Redistribution and use in source and binary forms, with or without 733323Sphk * modification, are permitted provided that the following conditions are 833323Sphk * met: 933323Sphk * 1036938Sphk * * Redistributions of source code must retain the above copyright 1136938Sphk * notice, this list of conditions and the following disclaimer. 12185003Sjhb * 1336938Sphk * * Redistributions in binary form must reproduce the above 1436938Sphk * copyright notice, this list of conditions and the following 1533323Sphk * disclaimer in the documentation and/or other materials provided 1633323Sphk * with the distribution. 17119418Sobrien 18119418Sobrien * * Neither the name of Cavium Inc. nor the names of 19119418Sobrien * its contributors may be used to endorse or promote products 2033323Sphk * derived from this software without specific prior written 21187576Sjhb * permission. 2233323Sphk 2333323Sphk * This Software, including technical data, may be subject to U.S. export control 2455939Snsouch * laws, including the U.S. Export Administration Act and its associated 25187576Sjhb * regulations, and may be subject to export or import regulations in other 2655939Snsouch * countries. 2733323Sphk 2836739Sphk * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 2955939Snsouch * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR 3055939Snsouch * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO 3155939Snsouch * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR 3233323Sphk * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM 3333323Sphk * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, 3455939Snsouch * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF 3555939Snsouch * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR 3633323Sphk * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR 3749550Sphk * PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 3833323Sphk ***********************license end**************************************/ 3997228Speter 4088220Simp 4149550Sphk 42143390Simp 4383818Sphk 44130585Sphk 4583818Sphk 4683818Sphk/** 4783818Sphk * @file 48187576Sjhb * 4983818Sphk * Small helper utilities. 5055939Snsouch * 51187576Sjhb * <hr>$Revision: 70030 $<hr> 5255939Snsouch */ 5355939Snsouch 5449550Sphk#ifndef __CVMX_HELPER_UTIL_H__ 5533323Sphk#define __CVMX_HELPER_UTIL_H__ 56187576Sjhb 5783818Sphk#include "cvmx.h" 5833323Sphk#include "cvmx-mio-defs.h" 5955939Snsouch 6055939Snsouch#ifdef CVMX_ENABLE_HELPER_FUNCTIONS 6133323Sphk 6255939Snsouchtypedef char cvmx_pknd_t; 6333323Sphktypedef char cvmx_bpid_t; 6433396Sphk 6533396Sphk#define CVMX_INVALID_PKND ((cvmx_pknd_t) -1) 6636739Sphk#define CVMX_INVALID_BPID ((cvmx_bpid_t) -1) 6733323Sphk#define CVMX_MAX_PKND ((cvmx_pknd_t) 64) 6847625Sphk#define CVMX_MAX_BPID ((cvmx_bpid_t) 64) 69126080Sphk 70111815Sphk#define CVMX_HELPER_MAX_IFACE 9 71111815Sphk 72111815Sphk/** 73111815Sphk * Convert a interface mode into a human readable string 7447625Sphk * 7533323Sphk * @param mode Mode to convert 7656455Speter * 7756455Speter * @return String 7856455Speter */ 7956455Speterextern const char *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t mode); 80127189Sguido 81127189Sguido/** 82184130Sjhb * Debug routine to dump the packet structure to the console 83127189Sguido * 84127189Sguido * @param work Work queue entry containing the packet to dump 8556455Speter * @return 8656455Speter */ 8755939Snsouchextern int cvmx_helper_dump_packet(cvmx_wqe_t *work); 8883818Sphk 8983818Sphk/** 9083818Sphk * Setup Random Early Drop on a specific input queue 9183818Sphk * 9283818Sphk * @param queue Input queue to setup RED on (0-7) 9383818Sphk * @param pass_thresh 9488220Simp * Packets will begin slowly dropping when there are less than 9583818Sphk * this many packet buffers free in FPA 0. 9683818Sphk * @param drop_thresh 9783818Sphk * All incomming packets will be dropped when there are less 9883818Sphk * than this many free packet buffers in FPA 0. 9955939Snsouch * @return Zero on success. Negative on failure 10033323Sphk */ 10188220Simpextern int cvmx_helper_setup_red_queue(int queue, int pass_thresh, int drop_thresh); 10288220Simp 10388220Simp/** 10488220Simp * Setup Random Early Drop to automatically begin dropping packets. 10588220Simp * 10688220Simp * @param pass_thresh 10788220Simp * Packets will begin slowly dropping when there are less than 10888220Simp * this many packet buffers free in FPA 0. 10988220Simp * @param drop_thresh 11088220Simp * All incomming packets will be dropped when there are less 111130585Sphk * than this many free packet buffers in FPA 0. 112187576Sjhb * @return Zero on success. Negative on failure 11333323Sphk */ 114183053Sjhbextern int cvmx_helper_setup_red(int pass_thresh, int drop_thresh); 115183053Sjhb 116183053Sjhb 117183053Sjhb/** 11888220Simp * Get the version of the CVMX libraries. 119187576Sjhb * 120187576Sjhb * @return Version string. Note this buffer is allocated statically 12188220Simp * and will be shared by all callers. 122187576Sjhb */ 12388220Simpextern const char *cvmx_helper_get_version(void); 124187576Sjhb 125187576Sjhb 126187576Sjhb/** 127187576Sjhb * @INTERNAL 128187576Sjhb * Setup the common GMX settings that determine the number of 129187576Sjhb * ports. These setting apply to almost all configurations of all 130187576Sjhb * chips. 131187576Sjhb * 132187576Sjhb * @param interface Interface to configure 133187576Sjhb * @param num_ports Number of ports on the interface 134187576Sjhb * 13588220Simp * @return Zero on success, negative on failure 13688220Simp */ 13783818Sphkextern int __cvmx_helper_setup_gmx(int interface, int num_ports); 13888220Simp 139108322Srwatson/** 14088220Simp * @INTERNAL 14183818Sphk * Get the number of ipd_ports on an interface. 14288220Simp * 14388220Simp * @param interface 14483818Sphk * 14533323Sphk * @return the number of ipd_ports on the interface and -1 for error. 146187576Sjhb */ 147187576Sjhbextern int __cvmx_helper_get_num_ipd_ports(int interface); 148187576Sjhb 14983818Sphk/** 150187576Sjhb * @INTERNAL 15133323Sphk * Get the number of pko_ports on an interface. 15283818Sphk * 15383818Sphk * @param interface 15488220Simp * 15583818Sphk * @return the number of pko_ports on the interface. 15683818Sphk */ 15783818Sphkextern int __cvmx_helper_get_num_pko_ports(int interface); 15883818Sphk 15983818Sphk/* 16083818Sphk * @INTERNAL 16183818Sphk * 16283818Sphk * @param interface 16383818Sphk * @param port 16483818Sphk * @param link_info 16583818Sphk * 16683818Sphk * @return 0 for success and -1 for failure 16783818Sphk */ 16883818Sphkextern int __cvmx_helper_set_link_info(int interface, int port, 16983818Sphk cvmx_helper_link_info_t link_info); 17088220Simp 17183818Sphk/** 17283818Sphk * @INTERNAL 17383818Sphk * 17483818Sphk * @param interface 17583818Sphk * @param port 17683818Sphk * 17783818Sphk * @return valid link_info on success or -1 on failure 17883818Sphk */ 17983818Sphkextern cvmx_helper_link_info_t __cvmx_helper_get_link_info(int interface, 18083818Sphk int port); 18183818Sphk 18288220Simpenum cvmx_pko_padding { 18383818Sphk CVMX_PKO_PADDING_NONE = 0, 18483818Sphk CVMX_PKO_PADDING_60 = 1, 18583818Sphk}; 18683818Sphk 187187576Sjhb/** 18883818Sphk * @INTERNAL 18983818Sphk * 19088220Simp * @param interface 191108322Srwatson * @param num_ipd_ports is the number of ipd_ports on the interface 19288220Simp * @param has_fcs indicates if PKO does FCS for the ports on this 19383818Sphk * @param pad The padding that PKO should apply. 19488220Simp * interface. 195106563Sjhb * 19683818Sphk * @return 0 for success and -1 for failure 19783818Sphk */ 198187576Sjhbextern int __cvmx_helper_init_interface(int interface, int num_ipd_ports, int has_fcs, enum cvmx_pko_padding pad); 19983818Sphk 20083818Sphk/** 20188220Simp * @INTERNAL 202187576Sjhb * 20333323Sphk * @param interface 20455939Snsouch * 20533323Sphk * @return 0 if PKO does not do FCS and 1 otherwise. 20633323Sphk */ 20733323Sphkextern int __cvmx_helper_get_has_fcs(int interface); 208130585Sphk 20933323Sphk 21083818Sphkextern enum cvmx_pko_padding __cvmx_helper_get_pko_padding(int interface); 211187576Sjhb 212106563Sjhb/** 213187576Sjhb * Returns the IPD port number for a port on the given 21433323Sphk * interface. 215187576Sjhb * 216187576Sjhb * @param interface Interface to use 217187576Sjhb * @param port Port on the interface 218187576Sjhb * 219187576Sjhb * @return IPD port number 220187576Sjhb */ 221187576Sjhbextern int cvmx_helper_get_ipd_port(int interface, int port); 22283818Sphk 22383818Sphk/** 22483818Sphk * Returns the PKO port number for a port on the given interface, 225187576Sjhb * This is the base pko_port for o68 and ipd_port for older models. 226187576Sjhb * 227187576Sjhb * @param interface Interface to use 22846053Sphk * @param port Port on the interface 22955939Snsouch * 23055939Snsouch * @return PKO port number and -1 on error. 23183818Sphk */ 23288220Simpextern int cvmx_helper_get_pko_port(int interface, int port); 23383818Sphk 23483818Sphk/** 23583818Sphk * Returns the IPD/PKO port number for the first port on the given 236143390Simp * interface. 23783818Sphk * 238187576Sjhb * @param interface Interface to use 239187576Sjhb * 24083818Sphk * @return IPD/PKO port number 24146053Sphk */ 24283818Sphkstatic inline int cvmx_helper_get_first_ipd_port(int interface) 243187576Sjhb{ 244187576Sjhb return (cvmx_helper_get_ipd_port (interface, 0)); 24533323Sphk} 24633323Sphk 24733323Sphk/** 24833323Sphk * Returns the IPD/PKO port number for the last port on the given 249130585Sphk * interface. 25033323Sphk * 25183818Sphk * @param interface Interface to use 252106563Sjhb * 25333323Sphk * @return IPD/PKO port number 254187576Sjhb */ 25583818Sphkstatic inline int cvmx_helper_get_last_ipd_port (int interface) 256187576Sjhb{ 25783818Sphk return (cvmx_helper_get_first_ipd_port (interface) + 258143390Simp cvmx_helper_ports_on_interface (interface) - 1); 259187576Sjhb} 26083818Sphk 26183818Sphk 26283818Sphk/** 26343433Snsouch * Free the packet buffers contained in a work queue entry. 26483818Sphk * The work queue entry is not freed. 26583818Sphk * 26643433Snsouch * @param work Work queue entry with packet to free 26783818Sphk */ 26883818Sphkstatic inline void cvmx_helper_free_packet_data(cvmx_wqe_t *work) 26983818Sphk{ 270187576Sjhb uint64_t number_buffers; 271187576Sjhb cvmx_buf_ptr_t buffer_ptr; 27233323Sphk cvmx_buf_ptr_t next_buffer_ptr; 27333323Sphk uint64_t start_of_buffer; 27433323Sphk 27533323Sphk number_buffers = work->word2.s.bufs; 27683818Sphk if (number_buffers == 0) 27783818Sphk return; 27883818Sphk buffer_ptr = work->packet_ptr; 27983818Sphk 28083818Sphk /* Since the number of buffers is not zero, we know this is not a dynamic 281187576Sjhb short packet. We need to check if it is a packet received with 28283818Sphk IPD_CTL_STATUS[NO_WPTR]. If this is true, we need to free all buffers 283185003Sjhb except for the first one. The caller doesn't expect their WQE pointer 28483818Sphk to be freed */ 28583818Sphk start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7; 28683818Sphk if (cvmx_ptr_to_phys(work) == start_of_buffer) 28783818Sphk { 28895523Sphk next_buffer_ptr = *(cvmx_buf_ptr_t*)cvmx_phys_to_ptr(buffer_ptr.s.addr - 8); 28995523Sphk buffer_ptr = next_buffer_ptr; 29095523Sphk number_buffers--; 29195523Sphk } 29295523Sphk 29383818Sphk while (number_buffers--) 29483818Sphk { 29583818Sphk /* Remember the back pointer is in cache lines, not 64bit words */ 296187576Sjhb start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7; 29783818Sphk /* Read pointer to next buffer before we free the current buffer. */ 29883818Sphk next_buffer_ptr = *(cvmx_buf_ptr_t*)cvmx_phys_to_ptr(buffer_ptr.s.addr - 8); 299187576Sjhb cvmx_fpa_free(cvmx_phys_to_ptr(start_of_buffer), buffer_ptr.s.pool, 0); 30055939Snsouch buffer_ptr = next_buffer_ptr; 30133323Sphk } 302145075Simp} 30333323Sphk 304187576Sjhb#endif /* CVMX_ENABLE_HELPER_FUNCTIONS */ 30595523Sphk 306145075Simp/** 307187576Sjhb * Returns the interface number for an IPD/PKO port number. 308187576Sjhb * 309185003Sjhb * @param ipd_port IPD/PKO port number 310145075Simp * 31195523Sphk * @return Interface number 312185003Sjhb */ 313145075Simpextern int cvmx_helper_get_interface_num(int ipd_port); 31433323Sphk 31533323Sphk/** 31636739Sphk * Returns the interface index number for an IPD/PKO port 317130585Sphk * number. 31833323Sphk * 31983818Sphk * @param ipd_port IPD/PKO port number 320106563Sjhb * 321143398Simp * @return Interface index number 32233323Sphk */ 323187576Sjhbextern int cvmx_helper_get_interface_index_num(int ipd_port); 324143398Simp 325187576Sjhb/** 326143398Simp * Get port kind for a given port in an interface. 32733323Sphk * 32833323Sphk * @param interface Interface 32956455Speter * @param port index of the port in the interface 33056455Speter * 33156455Speter * @return port kind on sucicess and -1 on failure 33256455Speter */ 33356455Speterextern int cvmx_helper_get_pknd(int interface, int port); 33456455Speter 33556455Speter/** 33656455Speter * Get bpid for a given port in an interface. 33756455Speter * 33856455Speter * @param interface Interface 33956455Speter * @param port index of the port in the interface 34056455Speter * 34156455Speter * @return port kind on sucicess and -1 on failure 34256455Speter */ 34355939Snsouchextern int cvmx_helper_get_bpid(int interface, int port); 344153610Sru 345 346/** 347 * Internal functions. 348 */ 349extern int __cvmx_helper_post_init_interfaces(void); 350extern void __cvmx_helper_shutdown_interfaces(void); 351 352extern void cvmx_helper_show_stats(int port); 353 354#endif /* __CVMX_HELPER_H__ */ 355