1 /***********************license start***************
2 * Author: Cavium Networks
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
7 * Copyright (c) 2003-2008 Cavium Networks
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this file; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * or visit http://www.gnu.org/licenses/.
24 * This file may also be available under a different license from Cavium.
25 * Contact Cavium Networks for more information
26 ***********************license end**************************************/
29 * Small helper utilities.
31 #include <linux/kernel.h>
33 #include <asm/octeon/octeon.h>
35 #include "cvmx-config.h"
43 #include "cvmx-helper.h"
44 #include "cvmx-helper-util.h"
46 #include <asm/octeon/cvmx-ipd-defs.h>
49 * Convert a interface mode into a human readable string
51 * @mode: Mode to convert
55 const char *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t
59 case CVMX_HELPER_INTERFACE_MODE_DISABLED
:
61 case CVMX_HELPER_INTERFACE_MODE_RGMII
:
63 case CVMX_HELPER_INTERFACE_MODE_GMII
:
65 case CVMX_HELPER_INTERFACE_MODE_SPI
:
67 case CVMX_HELPER_INTERFACE_MODE_PCIE
:
69 case CVMX_HELPER_INTERFACE_MODE_XAUI
:
71 case CVMX_HELPER_INTERFACE_MODE_SGMII
:
73 case CVMX_HELPER_INTERFACE_MODE_PICMG
:
75 case CVMX_HELPER_INTERFACE_MODE_NPI
:
77 case CVMX_HELPER_INTERFACE_MODE_LOOP
:
84 * Debug routine to dump the packet structure to the console
86 * @work: Work queue entry containing the packet to dump
89 int cvmx_helper_dump_packet(cvmx_wqe_t
*work
)
92 uint64_t remaining_bytes
;
93 union cvmx_buf_ptr buffer_ptr
;
94 uint64_t start_of_buffer
;
95 uint8_t *data_address
;
98 cvmx_dprintf("Packet Length: %u\n", work
->len
);
99 cvmx_dprintf(" Input Port: %u\n", work
->ipprt
);
100 cvmx_dprintf(" QoS: %u\n", work
->qos
);
101 cvmx_dprintf(" Buffers: %u\n", work
->word2
.s
.bufs
);
103 if (work
->word2
.s
.bufs
== 0) {
104 union cvmx_ipd_wqe_fpa_queue wqe_pool
;
105 wqe_pool
.u64
= cvmx_read_csr(CVMX_IPD_WQE_FPA_QUEUE
);
107 buffer_ptr
.s
.pool
= wqe_pool
.s
.wqe_pool
;
108 buffer_ptr
.s
.size
= 128;
109 buffer_ptr
.s
.addr
= cvmx_ptr_to_phys(work
->packet_data
);
110 if (likely(!work
->word2
.s
.not_IP
)) {
111 union cvmx_pip_ip_offset pip_ip_offset
;
112 pip_ip_offset
.u64
= cvmx_read_csr(CVMX_PIP_IP_OFFSET
);
114 (pip_ip_offset
.s
.offset
<< 3) -
115 work
->word2
.s
.ip_offset
;
116 buffer_ptr
.s
.addr
+= (work
->word2
.s
.is_v6
^ 1) << 2;
119 * WARNING: This code assumes that the packet
120 * is not RAW. If it was, we would use
121 * PIP_GBL_CFG[RAW_SHF] instead of
122 * PIP_GBL_CFG[NIP_SHF].
124 union cvmx_pip_gbl_cfg pip_gbl_cfg
;
125 pip_gbl_cfg
.u64
= cvmx_read_csr(CVMX_PIP_GBL_CFG
);
126 buffer_ptr
.s
.addr
+= pip_gbl_cfg
.s
.nip_shf
;
129 buffer_ptr
= work
->packet_ptr
;
130 remaining_bytes
= work
->len
;
132 while (remaining_bytes
) {
134 ((buffer_ptr
.s
.addr
>> 7) - buffer_ptr
.s
.back
) << 7;
135 cvmx_dprintf(" Buffer Start:%llx\n",
136 (unsigned long long)start_of_buffer
);
137 cvmx_dprintf(" Buffer I : %u\n", buffer_ptr
.s
.i
);
138 cvmx_dprintf(" Buffer Back: %u\n", buffer_ptr
.s
.back
);
139 cvmx_dprintf(" Buffer Pool: %u\n", buffer_ptr
.s
.pool
);
140 cvmx_dprintf(" Buffer Data: %llx\n",
141 (unsigned long long)buffer_ptr
.s
.addr
);
142 cvmx_dprintf(" Buffer Size: %u\n", buffer_ptr
.s
.size
);
144 cvmx_dprintf("\t\t");
145 data_address
= (uint8_t *) cvmx_phys_to_ptr(buffer_ptr
.s
.addr
);
146 end_of_data
= data_address
+ buffer_ptr
.s
.size
;
148 while (data_address
< end_of_data
) {
149 if (remaining_bytes
== 0)
153 cvmx_dprintf("%02x", (unsigned int)*data_address
);
155 if (remaining_bytes
&& (count
== 7)) {
156 cvmx_dprintf("\n\t\t");
164 buffer_ptr
= *(union cvmx_buf_ptr
*)
165 cvmx_phys_to_ptr(buffer_ptr
.s
.addr
- 8);
171 * Setup Random Early Drop on a specific input queue
173 * @queue: Input queue to setup RED on (0-7)
175 * Packets will begin slowly dropping when there are less than
176 * this many packet buffers free in FPA 0.
178 * All incomming packets will be dropped when there are less
179 * than this many free packet buffers in FPA 0.
180 * Returns Zero on success. Negative on failure
182 int cvmx_helper_setup_red_queue(int queue
, int pass_thresh
, int drop_thresh
)
184 union cvmx_ipd_qosx_red_marks red_marks
;
185 union cvmx_ipd_red_quex_param red_param
;
187 /* Set RED to begin dropping packets when there are pass_thresh buffers
188 left. It will linearly drop more packets until reaching drop_thresh
191 red_marks
.s
.drop
= drop_thresh
;
192 red_marks
.s
.pass
= pass_thresh
;
193 cvmx_write_csr(CVMX_IPD_QOSX_RED_MARKS(queue
), red_marks
.u64
);
195 /* Use the actual queue 0 counter, not the average */
197 red_param
.s
.prb_con
=
198 (255ul << 24) / (red_marks
.s
.pass
- red_marks
.s
.drop
);
199 red_param
.s
.avg_con
= 1;
200 red_param
.s
.new_con
= 255;
201 red_param
.s
.use_pcnt
= 1;
202 cvmx_write_csr(CVMX_IPD_RED_QUEX_PARAM(queue
), red_param
.u64
);
207 * Setup Random Early Drop to automatically begin dropping packets.
210 * Packets will begin slowly dropping when there are less than
211 * this many packet buffers free in FPA 0.
213 * All incomming packets will be dropped when there are less
214 * than this many free packet buffers in FPA 0.
215 * Returns Zero on success. Negative on failure
217 int cvmx_helper_setup_red(int pass_thresh
, int drop_thresh
)
219 union cvmx_ipd_portx_bp_page_cnt page_cnt
;
220 union cvmx_ipd_bp_prt_red_end ipd_bp_prt_red_end
;
221 union cvmx_ipd_red_port_enable red_port_enable
;
226 /* Disable backpressure based on queued buffers. It needs SW support */
228 page_cnt
.s
.bp_enb
= 0;
229 page_cnt
.s
.page_cnt
= 100;
230 for (interface
= 0; interface
< 2; interface
++) {
231 for (port
= cvmx_helper_get_first_ipd_port(interface
);
232 port
< cvmx_helper_get_last_ipd_port(interface
); port
++)
233 cvmx_write_csr(CVMX_IPD_PORTX_BP_PAGE_CNT(port
),
237 for (queue
= 0; queue
< 8; queue
++)
238 cvmx_helper_setup_red_queue(queue
, pass_thresh
, drop_thresh
);
240 /* Shutoff the dropping based on the per port page count. SW isn't
241 decrementing it right now */
242 ipd_bp_prt_red_end
.u64
= 0;
243 ipd_bp_prt_red_end
.s
.prt_enb
= 0;
244 cvmx_write_csr(CVMX_IPD_BP_PRT_RED_END
, ipd_bp_prt_red_end
.u64
);
246 red_port_enable
.u64
= 0;
247 red_port_enable
.s
.prt_enb
= 0xfffffffffull
;
248 red_port_enable
.s
.avg_dly
= 10000;
249 red_port_enable
.s
.prb_dly
= 10000;
250 cvmx_write_csr(CVMX_IPD_RED_PORT_ENABLE
, red_port_enable
.u64
);
256 * Setup the common GMX settings that determine the number of
257 * ports. These setting apply to almost all configurations of all
260 * @interface: Interface to configure
261 * @num_ports: Number of ports on the interface
263 * Returns Zero on success, negative on failure
265 int __cvmx_helper_setup_gmx(int interface
, int num_ports
)
267 union cvmx_gmxx_tx_prts gmx_tx_prts
;
268 union cvmx_gmxx_rx_prts gmx_rx_prts
;
269 union cvmx_pko_reg_gmx_port_mode pko_mode
;
270 union cvmx_gmxx_txx_thresh gmx_tx_thresh
;
273 /* Tell GMX the number of TX ports on this interface */
274 gmx_tx_prts
.u64
= cvmx_read_csr(CVMX_GMXX_TX_PRTS(interface
));
275 gmx_tx_prts
.s
.prts
= num_ports
;
276 cvmx_write_csr(CVMX_GMXX_TX_PRTS(interface
), gmx_tx_prts
.u64
);
278 /* Tell GMX the number of RX ports on this interface. This only
279 ** applies to *GMII and XAUI ports */
280 if (cvmx_helper_interface_get_mode(interface
) ==
281 CVMX_HELPER_INTERFACE_MODE_RGMII
282 || cvmx_helper_interface_get_mode(interface
) ==
283 CVMX_HELPER_INTERFACE_MODE_SGMII
284 || cvmx_helper_interface_get_mode(interface
) ==
285 CVMX_HELPER_INTERFACE_MODE_GMII
286 || cvmx_helper_interface_get_mode(interface
) ==
287 CVMX_HELPER_INTERFACE_MODE_XAUI
) {
289 cvmx_dprintf("__cvmx_helper_setup_gmx: Illegal "
294 gmx_rx_prts
.u64
= cvmx_read_csr(CVMX_GMXX_RX_PRTS(interface
));
295 gmx_rx_prts
.s
.prts
= num_ports
;
296 cvmx_write_csr(CVMX_GMXX_RX_PRTS(interface
), gmx_rx_prts
.u64
);
299 /* Skip setting CVMX_PKO_REG_GMX_PORT_MODE on 30XX, 31XX, and 50XX */
300 if (!OCTEON_IS_MODEL(OCTEON_CN30XX
) && !OCTEON_IS_MODEL(OCTEON_CN31XX
)
301 && !OCTEON_IS_MODEL(OCTEON_CN50XX
)) {
302 /* Tell PKO the number of ports on this interface */
303 pko_mode
.u64
= cvmx_read_csr(CVMX_PKO_REG_GMX_PORT_MODE
);
304 if (interface
== 0) {
306 pko_mode
.s
.mode0
= 4;
307 else if (num_ports
== 2)
308 pko_mode
.s
.mode0
= 3;
309 else if (num_ports
<= 4)
310 pko_mode
.s
.mode0
= 2;
311 else if (num_ports
<= 8)
312 pko_mode
.s
.mode0
= 1;
314 pko_mode
.s
.mode0
= 0;
317 pko_mode
.s
.mode1
= 4;
318 else if (num_ports
== 2)
319 pko_mode
.s
.mode1
= 3;
320 else if (num_ports
<= 4)
321 pko_mode
.s
.mode1
= 2;
322 else if (num_ports
<= 8)
323 pko_mode
.s
.mode1
= 1;
325 pko_mode
.s
.mode1
= 0;
327 cvmx_write_csr(CVMX_PKO_REG_GMX_PORT_MODE
, pko_mode
.u64
);
331 * Set GMX to buffer as much data as possible before starting
332 * transmit. This reduces the chances that we have a TX under
333 * run due to memory contention. Any packet that fits entirely
334 * in the GMX FIFO can never have an under run regardless of
337 gmx_tx_thresh
.u64
= cvmx_read_csr(CVMX_GMXX_TXX_THRESH(0, interface
));
338 if (OCTEON_IS_MODEL(OCTEON_CN30XX
) || OCTEON_IS_MODEL(OCTEON_CN31XX
)
339 || OCTEON_IS_MODEL(OCTEON_CN50XX
)) {
340 /* These chips have a fixed max threshold of 0x40 */
341 gmx_tx_thresh
.s
.cnt
= 0x40;
343 /* Choose the max value for the number of ports */
345 gmx_tx_thresh
.s
.cnt
= 0x100 / 1;
346 else if (num_ports
== 2)
347 gmx_tx_thresh
.s
.cnt
= 0x100 / 2;
349 gmx_tx_thresh
.s
.cnt
= 0x100 / 4;
352 * SPI and XAUI can have lots of ports but the GMX hardware
353 * only ever has a max of 4.
357 for (index
= 0; index
< num_ports
; index
++)
358 cvmx_write_csr(CVMX_GMXX_TXX_THRESH(index
, interface
),
365 * Returns the IPD/PKO port number for a port on teh given
368 * @interface: Interface to use
369 * @port: Port on the interface
371 * Returns IPD/PKO port number
373 int cvmx_helper_get_ipd_port(int interface
, int port
)
389 * Returns the interface number for an IPD/PKO port number.
391 * @ipd_port: IPD/PKO port number
393 * Returns Interface number
395 int cvmx_helper_get_interface_num(int ipd_port
)
399 else if (ipd_port
< 32)
401 else if (ipd_port
< 36)
403 else if (ipd_port
< 40)
406 cvmx_dprintf("cvmx_helper_get_interface_num: Illegal IPD "
413 * Returns the interface index number for an IPD/PKO port
416 * @ipd_port: IPD/PKO port number
418 * Returns Interface index number
420 int cvmx_helper_get_interface_index_num(int ipd_port
)
423 return ipd_port
& 15;
424 else if (ipd_port
< 36)
426 else if (ipd_port
< 40)
429 cvmx_dprintf("cvmx_helper_get_interface_index_num: "
430 "Illegal IPD port number\n");