1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * * Neither the name of Cavium Networks nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
49 * Small helper utilities.
51 * <hr>$Revision: 49448 $<hr>
53 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
54 #include <linux/module.h>
56 #include <asm/octeon/cvmx.h>
57 #include <asm/octeon/cvmx-config.h>
58 #include <asm/octeon/cvmx-pip.h>
59 #include <asm/octeon/cvmx-ipd.h>
60 #include <asm/octeon/cvmx-helper.h>
61 #include <asm/octeon/cvmx-gmxx-defs.h>
62 #include <asm/octeon/cvmx-pko-defs.h>
64 #if !defined(__FreeBSD__) || !defined(_KERNEL)
65 #include "executive-config.h"
66 #include "cvmx-config.h"
69 #include "cvmx-bootmem.h"
76 #include "cvmx-sysinfo.h"
77 #include "cvmx-helper.h"
78 #include "cvmx-helper-util.h"
79 #include "cvmx-version.h"
82 #ifdef CVMX_ENABLE_HELPER_FUNCTIONS
84 #ifndef CVMX_BUILD_FOR_LINUX_KERNEL
86 * Get the version of the CVMX libraries.
88 * @return Version string. Note this buffer is allocated statically
89 * and will be shared by all callers.
91 const char *cvmx_helper_get_version(void)
93 return OCTEON_SDK_VERSION_STRING;
98 * Convert a interface mode into a human readable string
100 * @param mode Mode to convert
104 const char *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t mode)
108 case CVMX_HELPER_INTERFACE_MODE_DISABLED: return "DISABLED";
109 case CVMX_HELPER_INTERFACE_MODE_RGMII: return "RGMII";
110 case CVMX_HELPER_INTERFACE_MODE_GMII: return "GMII";
111 case CVMX_HELPER_INTERFACE_MODE_SPI: return "SPI";
112 case CVMX_HELPER_INTERFACE_MODE_PCIE: return "PCIE";
113 case CVMX_HELPER_INTERFACE_MODE_XAUI: return "XAUI";
114 case CVMX_HELPER_INTERFACE_MODE_SGMII: return "SGMII";
115 case CVMX_HELPER_INTERFACE_MODE_PICMG: return "PICMG";
116 case CVMX_HELPER_INTERFACE_MODE_NPI: return "NPI";
117 case CVMX_HELPER_INTERFACE_MODE_LOOP: return "LOOP";
118 case CVMX_HELPER_INTERFACE_MODE_SRIO: return "SRIO";
125 * Debug routine to dump the packet structure to the console
127 * @param work Work queue entry containing the packet to dump
130 int cvmx_helper_dump_packet(cvmx_wqe_t *work)
133 uint64_t remaining_bytes;
134 cvmx_buf_ptr_t buffer_ptr;
135 uint64_t start_of_buffer;
136 uint8_t * data_address;
137 uint8_t * end_of_data;
139 cvmx_dprintf("Packet Length: %u\n", work->len);
140 cvmx_dprintf(" Input Port: %u\n", work->ipprt);
141 cvmx_dprintf(" QoS: %u\n", work->qos);
142 cvmx_dprintf(" Buffers: %u\n", work->word2.s.bufs);
144 if (work->word2.s.bufs == 0)
146 cvmx_ipd_wqe_fpa_queue_t wqe_pool;
147 wqe_pool.u64 = cvmx_read_csr(CVMX_IPD_WQE_FPA_QUEUE);
149 buffer_ptr.s.pool = wqe_pool.s.wqe_pool;
150 buffer_ptr.s.size = 128;
151 buffer_ptr.s.addr = cvmx_ptr_to_phys(work->packet_data);
152 if (cvmx_likely(!work->word2.s.not_IP))
154 cvmx_pip_ip_offset_t pip_ip_offset;
155 pip_ip_offset.u64 = cvmx_read_csr(CVMX_PIP_IP_OFFSET);
156 buffer_ptr.s.addr += (pip_ip_offset.s.offset<<3) - work->word2.s.ip_offset;
157 buffer_ptr.s.addr += (work->word2.s.is_v6^1)<<2;
161 /* WARNING: This code assume that the packet is not RAW. If it was,
162 we would use PIP_GBL_CFG[RAW_SHF] instead of
163 PIP_GBL_CFG[NIP_SHF] */
164 cvmx_pip_gbl_cfg_t pip_gbl_cfg;
165 pip_gbl_cfg.u64 = cvmx_read_csr(CVMX_PIP_GBL_CFG);
166 buffer_ptr.s.addr += pip_gbl_cfg.s.nip_shf;
170 buffer_ptr = work->packet_ptr;
171 remaining_bytes = work->len;
173 while (remaining_bytes)
175 start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
176 cvmx_dprintf(" Buffer Start:%llx\n", (unsigned long long)start_of_buffer);
177 cvmx_dprintf(" Buffer I : %u\n", buffer_ptr.s.i);
178 cvmx_dprintf(" Buffer Back: %u\n", buffer_ptr.s.back);
179 cvmx_dprintf(" Buffer Pool: %u\n", buffer_ptr.s.pool);
180 cvmx_dprintf(" Buffer Data: %llx\n", (unsigned long long)buffer_ptr.s.addr);
181 cvmx_dprintf(" Buffer Size: %u\n", buffer_ptr.s.size);
183 cvmx_dprintf("\t\t");
184 data_address = (uint8_t *)cvmx_phys_to_ptr(buffer_ptr.s.addr);
185 end_of_data = data_address + buffer_ptr.s.size;
187 while (data_address < end_of_data)
189 if (remaining_bytes == 0)
193 cvmx_dprintf("%02x", (unsigned int)*data_address);
195 if (remaining_bytes && (count == 7))
197 cvmx_dprintf("\n\t\t");
206 buffer_ptr = *(cvmx_buf_ptr_t*)cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
213 * Setup Random Early Drop on a specific input queue
215 * @param queue Input queue to setup RED on (0-7)
217 * Packets will begin slowly dropping when there are less than
218 * this many packet buffers free in FPA 0.
220 * All incomming packets will be dropped when there are less
221 * than this many free packet buffers in FPA 0.
222 * @return Zero on success. Negative on failure
224 int cvmx_helper_setup_red_queue(int queue, int pass_thresh, int drop_thresh)
226 cvmx_ipd_qosx_red_marks_t red_marks;
227 cvmx_ipd_red_quex_param_t red_param;
229 /* Set RED to begin dropping packets when there are pass_thresh buffers
230 left. It will linearly drop more packets until reaching drop_thresh
233 red_marks.s.drop = drop_thresh;
234 red_marks.s.pass = pass_thresh;
235 cvmx_write_csr(CVMX_IPD_QOSX_RED_MARKS(queue), red_marks.u64);
237 /* Use the actual queue 0 counter, not the average */
239 red_param.s.prb_con = (255ul<<24) / (red_marks.s.pass - red_marks.s.drop);
240 red_param.s.avg_con = 1;
241 red_param.s.new_con = 255;
242 red_param.s.use_pcnt = 1;
243 cvmx_write_csr(CVMX_IPD_RED_QUEX_PARAM(queue), red_param.u64);
249 * Setup Random Early Drop to automatically begin dropping packets.
252 * Packets will begin slowly dropping when there are less than
253 * this many packet buffers free in FPA 0.
255 * All incomming packets will be dropped when there are less
256 * than this many free packet buffers in FPA 0.
257 * @return Zero on success. Negative on failure
259 int cvmx_helper_setup_red(int pass_thresh, int drop_thresh)
261 cvmx_ipd_portx_bp_page_cnt_t page_cnt;
262 cvmx_ipd_bp_prt_red_end_t ipd_bp_prt_red_end;
263 cvmx_ipd_red_port_enable_t red_port_enable;
268 /* Disable backpressure based on queued buffers. It needs SW support */
270 page_cnt.s.bp_enb = 0;
271 page_cnt.s.page_cnt = 100;
272 for (interface=0; interface<2; interface++)
274 for (port=cvmx_helper_get_first_ipd_port(interface); port<cvmx_helper_get_last_ipd_port(interface); port++)
275 cvmx_write_csr(CVMX_IPD_PORTX_BP_PAGE_CNT(port), page_cnt.u64);
278 for (queue=0; queue<8; queue++)
279 cvmx_helper_setup_red_queue(queue, pass_thresh, drop_thresh);
281 /* Shutoff the dropping based on the per port page count. SW isn't
282 decrementing it right now */
283 ipd_bp_prt_red_end.u64 = 0;
284 ipd_bp_prt_red_end.s.prt_enb = 0;
285 cvmx_write_csr(CVMX_IPD_BP_PRT_RED_END, ipd_bp_prt_red_end.u64);
287 red_port_enable.u64 = 0;
288 red_port_enable.s.prt_enb = 0xfffffffffull;
289 red_port_enable.s.avg_dly = 10000;
290 red_port_enable.s.prb_dly = 10000;
291 cvmx_write_csr(CVMX_IPD_RED_PORT_ENABLE, red_port_enable.u64);
293 /* Shutoff the dropping of packets based on RED for SRIO ports */
294 if (OCTEON_IS_MODEL(OCTEON_CN6XXX))
296 cvmx_ipd_red_port_enable2_t red_port_enable2;
297 red_port_enable2.u64 = 0;
298 red_port_enable2.s.prt_enb = 0xf0;
299 cvmx_write_csr(CVMX_IPD_RED_PORT_ENABLE2, red_port_enable2.u64);
304 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
305 EXPORT_SYMBOL(cvmx_helper_setup_red);
311 * Setup the common GMX settings that determine the number of
312 * ports. These setting apply to almost all configurations of all
315 * @param interface Interface to configure
316 * @param num_ports Number of ports on the interface
318 * @return Zero on success, negative on failure
320 int __cvmx_helper_setup_gmx(int interface, int num_ports)
322 cvmx_gmxx_tx_prts_t gmx_tx_prts;
323 cvmx_gmxx_rx_prts_t gmx_rx_prts;
324 cvmx_pko_reg_gmx_port_mode_t pko_mode;
325 cvmx_gmxx_txx_thresh_t gmx_tx_thresh;
328 /* Tell GMX the number of TX ports on this interface */
329 gmx_tx_prts.u64 = cvmx_read_csr(CVMX_GMXX_TX_PRTS(interface));
330 gmx_tx_prts.s.prts = num_ports;
331 cvmx_write_csr(CVMX_GMXX_TX_PRTS(interface), gmx_tx_prts.u64);
333 /* Tell GMX the number of RX ports on this interface. This only
334 ** applies to *GMII and XAUI ports */
335 if (cvmx_helper_interface_get_mode(interface) == CVMX_HELPER_INTERFACE_MODE_RGMII
336 || cvmx_helper_interface_get_mode(interface) == CVMX_HELPER_INTERFACE_MODE_SGMII
337 || cvmx_helper_interface_get_mode(interface) == CVMX_HELPER_INTERFACE_MODE_GMII
338 || cvmx_helper_interface_get_mode(interface) == CVMX_HELPER_INTERFACE_MODE_XAUI)
342 cvmx_dprintf("__cvmx_helper_setup_gmx: Illegal num_ports\n");
346 gmx_rx_prts.u64 = cvmx_read_csr(CVMX_GMXX_RX_PRTS(interface));
347 gmx_rx_prts.s.prts = num_ports;
348 cvmx_write_csr(CVMX_GMXX_RX_PRTS(interface), gmx_rx_prts.u64);
351 /* Skip setting CVMX_PKO_REG_GMX_PORT_MODE on 30XX, 31XX, and 50XX */
352 if (!OCTEON_IS_MODEL(OCTEON_CN30XX) && !OCTEON_IS_MODEL(OCTEON_CN31XX) && !OCTEON_IS_MODEL(OCTEON_CN50XX))
354 /* Tell PKO the number of ports on this interface */
355 pko_mode.u64 = cvmx_read_csr(CVMX_PKO_REG_GMX_PORT_MODE);
359 pko_mode.s.mode0 = 4;
360 else if (num_ports == 2)
361 pko_mode.s.mode0 = 3;
362 else if (num_ports <= 4)
363 pko_mode.s.mode0 = 2;
364 else if (num_ports <= 8)
365 pko_mode.s.mode0 = 1;
367 pko_mode.s.mode0 = 0;
372 pko_mode.s.mode1 = 4;
373 else if (num_ports == 2)
374 pko_mode.s.mode1 = 3;
375 else if (num_ports <= 4)
376 pko_mode.s.mode1 = 2;
377 else if (num_ports <= 8)
378 pko_mode.s.mode1 = 1;
380 pko_mode.s.mode1 = 0;
382 cvmx_write_csr(CVMX_PKO_REG_GMX_PORT_MODE, pko_mode.u64);
385 /* Set GMX to buffer as much data as possible before starting transmit.
386 This reduces the chances that we have a TX under run due to memory
387 contention. Any packet that fits entirely in the GMX FIFO can never
388 have an under run regardless of memory load */
389 gmx_tx_thresh.u64 = cvmx_read_csr(CVMX_GMXX_TXX_THRESH(0, interface));
390 if (OCTEON_IS_MODEL(OCTEON_CN30XX) || OCTEON_IS_MODEL(OCTEON_CN31XX) || OCTEON_IS_MODEL(OCTEON_CN50XX))
392 /* These chips have a fixed max threshold of 0x40 */
393 gmx_tx_thresh.s.cnt = 0x40;
397 /* Choose the max value for the number of ports */
399 gmx_tx_thresh.s.cnt = 0x100 / 1;
400 else if (num_ports == 2)
401 gmx_tx_thresh.s.cnt = 0x100 / 2;
403 gmx_tx_thresh.s.cnt = 0x100 / 4;
405 /* SPI and XAUI can have lots of ports but the GMX hardware only ever has
409 for (index=0; index<num_ports; index++)
410 cvmx_write_csr(CVMX_GMXX_TXX_THRESH(index, interface), gmx_tx_thresh.u64);
417 * Returns the IPD/PKO port number for a port on the given
420 * @param interface Interface to use
421 * @param port Port on the interface
423 * @return IPD/PKO port number
425 int cvmx_helper_get_ipd_port(int interface, int port)
430 case 1: return port + 16;
431 case 2: return port + 32;
432 case 3: return port + 36;
433 case 4: return port + 40;
434 case 5: return port + 42;
438 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
439 EXPORT_SYMBOL(cvmx_helper_get_ipd_port);
442 #endif /* CVMX_ENABLE_HELPER_FUNCTIONS */
446 * Returns the interface number for an IPD/PKO port number.
448 * @param ipd_port IPD/PKO port number
450 * @return Interface number
452 int cvmx_helper_get_interface_num(int ipd_port)
456 else if (ipd_port < 32)
458 else if (ipd_port < 36)
460 else if (ipd_port < 40)
462 else if (ipd_port < 42)
464 else if (ipd_port < 44)
467 cvmx_dprintf("cvmx_helper_get_interface_num: Illegal IPD port number\n");
471 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
472 EXPORT_SYMBOL(cvmx_helper_get_interface_num);
477 * Returns the interface index number for an IPD/PKO port
480 * @param ipd_port IPD/PKO port number
482 * @return Interface index number
484 int cvmx_helper_get_interface_index_num(int ipd_port)
487 return ipd_port & 15;
488 else if (ipd_port < 40)
490 else if (ipd_port < 44)
493 cvmx_dprintf("cvmx_helper_get_interface_index_num: Illegal IPD port number\n");
497 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
498 EXPORT_SYMBOL(cvmx_helper_get_interface_index_num);