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 * Interface to the hardware Packet Output unit.
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * Starting with SDK 1.7.0, the PKO output functions now support
3362306a36Sopenharmony_ci * two types of locking. CVMX_PKO_LOCK_ATOMIC_TAG continues to
3462306a36Sopenharmony_ci * function similarly to previous SDKs by using POW atomic tags
3562306a36Sopenharmony_ci * to preserve ordering and exclusivity. As a new option, you
3662306a36Sopenharmony_ci * can now pass CVMX_PKO_LOCK_CMD_QUEUE which uses a ll/sc
3762306a36Sopenharmony_ci * memory based locking instead. This locking has the advantage
3862306a36Sopenharmony_ci * of not affecting the tag state but doesn't preserve packet
3962306a36Sopenharmony_ci * ordering. CVMX_PKO_LOCK_CMD_QUEUE is appropriate in most
4062306a36Sopenharmony_ci * generic code while CVMX_PKO_LOCK_CMD_QUEUE should be used
4162306a36Sopenharmony_ci * with hand tuned fast path code.
4262306a36Sopenharmony_ci *
4362306a36Sopenharmony_ci * Some of other SDK differences visible to the command queuing:
4462306a36Sopenharmony_ci * - PKO indexes are no longer stored in the FAU. A large
4562306a36Sopenharmony_ci *   percentage of the FAU register block used to be tied up
4662306a36Sopenharmony_ci *   maintaining PKO queue pointers. These are now stored in a
4762306a36Sopenharmony_ci *   global named block.
4862306a36Sopenharmony_ci * - The PKO <b>use_locking</b> parameter can now have a global
4962306a36Sopenharmony_ci *   effect. Since all application use the same named block,
5062306a36Sopenharmony_ci *   queue locking correctly applies across all operating
5162306a36Sopenharmony_ci *   systems when using CVMX_PKO_LOCK_CMD_QUEUE.
5262306a36Sopenharmony_ci * - PKO 3 word commands are now supported. Use
5362306a36Sopenharmony_ci *   cvmx_pko_send_packet_finish3().
5462306a36Sopenharmony_ci *
5562306a36Sopenharmony_ci */
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci#ifndef __CVMX_PKO_H__
5862306a36Sopenharmony_ci#define __CVMX_PKO_H__
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci#include <asm/octeon/cvmx-fpa.h>
6162306a36Sopenharmony_ci#include <asm/octeon/cvmx-pow.h>
6262306a36Sopenharmony_ci#include <asm/octeon/cvmx-cmd-queue.h>
6362306a36Sopenharmony_ci#include <asm/octeon/cvmx-pko-defs.h>
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/* Adjust the command buffer size by 1 word so that in the case of using only
6662306a36Sopenharmony_ci * two word PKO commands no command words stradle buffers.  The useful values
6762306a36Sopenharmony_ci * for this are 0 and 1. */
6862306a36Sopenharmony_ci#define CVMX_PKO_COMMAND_BUFFER_SIZE_ADJUST (1)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#define CVMX_PKO_MAX_OUTPUT_QUEUES_STATIC 256
7162306a36Sopenharmony_ci#define CVMX_PKO_MAX_OUTPUT_QUEUES	((OCTEON_IS_MODEL(OCTEON_CN31XX) || \
7262306a36Sopenharmony_ci	OCTEON_IS_MODEL(OCTEON_CN3010) || OCTEON_IS_MODEL(OCTEON_CN3005) || \
7362306a36Sopenharmony_ci	OCTEON_IS_MODEL(OCTEON_CN50XX)) ? 32 : \
7462306a36Sopenharmony_ci		(OCTEON_IS_MODEL(OCTEON_CN58XX) || \
7562306a36Sopenharmony_ci		OCTEON_IS_MODEL(OCTEON_CN56XX)) ? 256 : 128)
7662306a36Sopenharmony_ci#define CVMX_PKO_NUM_OUTPUT_PORTS	40
7762306a36Sopenharmony_ci/* use this for queues that are not used */
7862306a36Sopenharmony_ci#define CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID 63
7962306a36Sopenharmony_ci#define CVMX_PKO_QUEUE_STATIC_PRIORITY	9
8062306a36Sopenharmony_ci#define CVMX_PKO_ILLEGAL_QUEUE	0xFFFF
8162306a36Sopenharmony_ci#define CVMX_PKO_MAX_QUEUE_DEPTH 0
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_citypedef enum {
8462306a36Sopenharmony_ci	CVMX_PKO_SUCCESS,
8562306a36Sopenharmony_ci	CVMX_PKO_INVALID_PORT,
8662306a36Sopenharmony_ci	CVMX_PKO_INVALID_QUEUE,
8762306a36Sopenharmony_ci	CVMX_PKO_INVALID_PRIORITY,
8862306a36Sopenharmony_ci	CVMX_PKO_NO_MEMORY,
8962306a36Sopenharmony_ci	CVMX_PKO_PORT_ALREADY_SETUP,
9062306a36Sopenharmony_ci	CVMX_PKO_CMD_QUEUE_INIT_ERROR
9162306a36Sopenharmony_ci} cvmx_pko_status_t;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci/**
9462306a36Sopenharmony_ci * This enumeration represents the differnet locking modes supported by PKO.
9562306a36Sopenharmony_ci */
9662306a36Sopenharmony_citypedef enum {
9762306a36Sopenharmony_ci	/*
9862306a36Sopenharmony_ci	 * PKO doesn't do any locking. It is the responsibility of the
9962306a36Sopenharmony_ci	 * application to make sure that no other core is accessing
10062306a36Sopenharmony_ci	 * the same queue at the same time
10162306a36Sopenharmony_ci	 */
10262306a36Sopenharmony_ci	CVMX_PKO_LOCK_NONE = 0,
10362306a36Sopenharmony_ci	/*
10462306a36Sopenharmony_ci	 * PKO performs an atomic tagswitch to insure exclusive access
10562306a36Sopenharmony_ci	 * to the output queue. This will maintain packet ordering on
10662306a36Sopenharmony_ci	 * output.
10762306a36Sopenharmony_ci	 */
10862306a36Sopenharmony_ci	CVMX_PKO_LOCK_ATOMIC_TAG = 1,
10962306a36Sopenharmony_ci	/*
11062306a36Sopenharmony_ci	 * PKO uses the common command queue locks to insure exclusive
11162306a36Sopenharmony_ci	 * access to the output queue. This is a memory based
11262306a36Sopenharmony_ci	 * ll/sc. This is the most portable locking mechanism.
11362306a36Sopenharmony_ci	 */
11462306a36Sopenharmony_ci	CVMX_PKO_LOCK_CMD_QUEUE = 2,
11562306a36Sopenharmony_ci} cvmx_pko_lock_t;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_citypedef struct {
11862306a36Sopenharmony_ci	uint32_t packets;
11962306a36Sopenharmony_ci	uint64_t octets;
12062306a36Sopenharmony_ci	uint64_t doorbell;
12162306a36Sopenharmony_ci} cvmx_pko_port_status_t;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci/**
12462306a36Sopenharmony_ci * This structure defines the address to use on a packet enqueue
12562306a36Sopenharmony_ci */
12662306a36Sopenharmony_citypedef union {
12762306a36Sopenharmony_ci	uint64_t u64;
12862306a36Sopenharmony_ci	struct {
12962306a36Sopenharmony_ci#ifdef __BIG_ENDIAN_BITFIELD
13062306a36Sopenharmony_ci		/* Must CVMX_IO_SEG */
13162306a36Sopenharmony_ci		uint64_t mem_space:2;
13262306a36Sopenharmony_ci		/* Must be zero */
13362306a36Sopenharmony_ci		uint64_t reserved:13;
13462306a36Sopenharmony_ci		/* Must be one */
13562306a36Sopenharmony_ci		uint64_t is_io:1;
13662306a36Sopenharmony_ci		/* The ID of the device on the non-coherent bus */
13762306a36Sopenharmony_ci		uint64_t did:8;
13862306a36Sopenharmony_ci		/* Must be zero */
13962306a36Sopenharmony_ci		uint64_t reserved2:4;
14062306a36Sopenharmony_ci		/* Must be zero */
14162306a36Sopenharmony_ci		uint64_t reserved3:18;
14262306a36Sopenharmony_ci		/*
14362306a36Sopenharmony_ci		 * The hardware likes to have the output port in
14462306a36Sopenharmony_ci		 * addition to the output queue,
14562306a36Sopenharmony_ci		 */
14662306a36Sopenharmony_ci		uint64_t port:6;
14762306a36Sopenharmony_ci		/*
14862306a36Sopenharmony_ci		 * The output queue to send the packet to (0-127 are
14962306a36Sopenharmony_ci		 * legal)
15062306a36Sopenharmony_ci		 */
15162306a36Sopenharmony_ci		uint64_t queue:9;
15262306a36Sopenharmony_ci		/* Must be zero */
15362306a36Sopenharmony_ci		uint64_t reserved4:3;
15462306a36Sopenharmony_ci#else
15562306a36Sopenharmony_ci	        uint64_t reserved4:3;
15662306a36Sopenharmony_ci	        uint64_t queue:9;
15762306a36Sopenharmony_ci	        uint64_t port:9;
15862306a36Sopenharmony_ci	        uint64_t reserved3:15;
15962306a36Sopenharmony_ci	        uint64_t reserved2:4;
16062306a36Sopenharmony_ci	        uint64_t did:8;
16162306a36Sopenharmony_ci	        uint64_t is_io:1;
16262306a36Sopenharmony_ci	        uint64_t reserved:13;
16362306a36Sopenharmony_ci	        uint64_t mem_space:2;
16462306a36Sopenharmony_ci#endif
16562306a36Sopenharmony_ci	} s;
16662306a36Sopenharmony_ci} cvmx_pko_doorbell_address_t;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci/**
16962306a36Sopenharmony_ci * Structure of the first packet output command word.
17062306a36Sopenharmony_ci */
17162306a36Sopenharmony_ciunion cvmx_pko_command_word0 {
17262306a36Sopenharmony_ci	uint64_t u64;
17362306a36Sopenharmony_ci	struct {
17462306a36Sopenharmony_ci#ifdef __BIG_ENDIAN_BITFIELD
17562306a36Sopenharmony_ci		/*
17662306a36Sopenharmony_ci		 * The size of the reg1 operation - could be 8, 16,
17762306a36Sopenharmony_ci		 * 32, or 64 bits.
17862306a36Sopenharmony_ci		 */
17962306a36Sopenharmony_ci		uint64_t size1:2;
18062306a36Sopenharmony_ci		/*
18162306a36Sopenharmony_ci		 * The size of the reg0 operation - could be 8, 16,
18262306a36Sopenharmony_ci		 * 32, or 64 bits.
18362306a36Sopenharmony_ci		 */
18462306a36Sopenharmony_ci		uint64_t size0:2;
18562306a36Sopenharmony_ci		/*
18662306a36Sopenharmony_ci		 * If set, subtract 1, if clear, subtract packet
18762306a36Sopenharmony_ci		 * size.
18862306a36Sopenharmony_ci		 */
18962306a36Sopenharmony_ci		uint64_t subone1:1;
19062306a36Sopenharmony_ci		/*
19162306a36Sopenharmony_ci		 * The register, subtract will be done if reg1 is
19262306a36Sopenharmony_ci		 * non-zero.
19362306a36Sopenharmony_ci		 */
19462306a36Sopenharmony_ci		uint64_t reg1:11;
19562306a36Sopenharmony_ci		/* If set, subtract 1, if clear, subtract packet size */
19662306a36Sopenharmony_ci		uint64_t subone0:1;
19762306a36Sopenharmony_ci		/* The register, subtract will be done if reg0 is non-zero */
19862306a36Sopenharmony_ci		uint64_t reg0:11;
19962306a36Sopenharmony_ci		/*
20062306a36Sopenharmony_ci		 * When set, interpret segment pointer and segment
20162306a36Sopenharmony_ci		 * bytes in little endian order.
20262306a36Sopenharmony_ci		 */
20362306a36Sopenharmony_ci		uint64_t le:1;
20462306a36Sopenharmony_ci		/*
20562306a36Sopenharmony_ci		 * When set, packet data not allocated in L2 cache by
20662306a36Sopenharmony_ci		 * PKO.
20762306a36Sopenharmony_ci		 */
20862306a36Sopenharmony_ci		uint64_t n2:1;
20962306a36Sopenharmony_ci		/*
21062306a36Sopenharmony_ci		 * If set and rsp is set, word3 contains a pointer to
21162306a36Sopenharmony_ci		 * a work queue entry.
21262306a36Sopenharmony_ci		 */
21362306a36Sopenharmony_ci		uint64_t wqp:1;
21462306a36Sopenharmony_ci		/* If set, the hardware will send a response when done */
21562306a36Sopenharmony_ci		uint64_t rsp:1;
21662306a36Sopenharmony_ci		/*
21762306a36Sopenharmony_ci		 * If set, the supplied pkt_ptr is really a pointer to
21862306a36Sopenharmony_ci		 * a list of pkt_ptr's.
21962306a36Sopenharmony_ci		 */
22062306a36Sopenharmony_ci		uint64_t gather:1;
22162306a36Sopenharmony_ci		/*
22262306a36Sopenharmony_ci		 * If ipoffp1 is non zero, (ipoffp1-1) is the number
22362306a36Sopenharmony_ci		 * of bytes to IP header, and the hardware will
22462306a36Sopenharmony_ci		 * calculate and insert the UDP/TCP checksum.
22562306a36Sopenharmony_ci		 */
22662306a36Sopenharmony_ci		uint64_t ipoffp1:7;
22762306a36Sopenharmony_ci		/*
22862306a36Sopenharmony_ci		 * If set, ignore the I bit (force to zero) from all
22962306a36Sopenharmony_ci		 * pointer structures.
23062306a36Sopenharmony_ci		 */
23162306a36Sopenharmony_ci		uint64_t ignore_i:1;
23262306a36Sopenharmony_ci		/*
23362306a36Sopenharmony_ci		 * If clear, the hardware will attempt to free the
23462306a36Sopenharmony_ci		 * buffers containing the packet.
23562306a36Sopenharmony_ci		 */
23662306a36Sopenharmony_ci		uint64_t dontfree:1;
23762306a36Sopenharmony_ci		/*
23862306a36Sopenharmony_ci		 * The total number of segs in the packet, if gather
23962306a36Sopenharmony_ci		 * set, also gather list length.
24062306a36Sopenharmony_ci		 */
24162306a36Sopenharmony_ci		uint64_t segs:6;
24262306a36Sopenharmony_ci		/* Including L2, but no trailing CRC */
24362306a36Sopenharmony_ci		uint64_t total_bytes:16;
24462306a36Sopenharmony_ci#else
24562306a36Sopenharmony_ci	        uint64_t total_bytes:16;
24662306a36Sopenharmony_ci	        uint64_t segs:6;
24762306a36Sopenharmony_ci	        uint64_t dontfree:1;
24862306a36Sopenharmony_ci	        uint64_t ignore_i:1;
24962306a36Sopenharmony_ci	        uint64_t ipoffp1:7;
25062306a36Sopenharmony_ci	        uint64_t gather:1;
25162306a36Sopenharmony_ci	        uint64_t rsp:1;
25262306a36Sopenharmony_ci	        uint64_t wqp:1;
25362306a36Sopenharmony_ci	        uint64_t n2:1;
25462306a36Sopenharmony_ci	        uint64_t le:1;
25562306a36Sopenharmony_ci	        uint64_t reg0:11;
25662306a36Sopenharmony_ci	        uint64_t subone0:1;
25762306a36Sopenharmony_ci	        uint64_t reg1:11;
25862306a36Sopenharmony_ci	        uint64_t subone1:1;
25962306a36Sopenharmony_ci	        uint64_t size0:2;
26062306a36Sopenharmony_ci	        uint64_t size1:2;
26162306a36Sopenharmony_ci#endif
26262306a36Sopenharmony_ci	} s;
26362306a36Sopenharmony_ci};
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci/* CSR typedefs have been moved to cvmx-csr-*.h */
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci/**
26862306a36Sopenharmony_ci * Definition of internal state for Packet output processing
26962306a36Sopenharmony_ci */
27062306a36Sopenharmony_citypedef struct {
27162306a36Sopenharmony_ci	/* ptr to start of buffer, offset kept in FAU reg */
27262306a36Sopenharmony_ci	uint64_t *start_ptr;
27362306a36Sopenharmony_ci} cvmx_pko_state_elem_t;
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci/**
27662306a36Sopenharmony_ci * Call before any other calls to initialize the packet
27762306a36Sopenharmony_ci * output system.
27862306a36Sopenharmony_ci */
27962306a36Sopenharmony_ciextern void cvmx_pko_initialize_global(void);
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci/**
28262306a36Sopenharmony_ci * Enables the packet output hardware. It must already be
28362306a36Sopenharmony_ci * configured.
28462306a36Sopenharmony_ci */
28562306a36Sopenharmony_ciextern void cvmx_pko_enable(void);
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci/**
28862306a36Sopenharmony_ci * Disables the packet output. Does not affect any configuration.
28962306a36Sopenharmony_ci */
29062306a36Sopenharmony_ciextern void cvmx_pko_disable(void);
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci/**
29362306a36Sopenharmony_ci * Shutdown and free resources required by packet output.
29462306a36Sopenharmony_ci */
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ciextern void cvmx_pko_shutdown(void);
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci/**
29962306a36Sopenharmony_ci * Configure a output port and the associated queues for use.
30062306a36Sopenharmony_ci *
30162306a36Sopenharmony_ci * @port:	Port to configure.
30262306a36Sopenharmony_ci * @base_queue: First queue number to associate with this port.
30362306a36Sopenharmony_ci * @num_queues: Number of queues t oassociate with this port
30462306a36Sopenharmony_ci * @priority:	Array of priority levels for each queue. Values are
30562306a36Sopenharmony_ci *		     allowed to be 1-8. A value of 8 get 8 times the traffic
30662306a36Sopenharmony_ci *		     of a value of 1. There must be num_queues elements in the
30762306a36Sopenharmony_ci *		     array.
30862306a36Sopenharmony_ci */
30962306a36Sopenharmony_ciextern cvmx_pko_status_t cvmx_pko_config_port(uint64_t port,
31062306a36Sopenharmony_ci					      uint64_t base_queue,
31162306a36Sopenharmony_ci					      uint64_t num_queues,
31262306a36Sopenharmony_ci					      const uint64_t priority[]);
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci/**
31562306a36Sopenharmony_ci * Ring the packet output doorbell. This tells the packet
31662306a36Sopenharmony_ci * output hardware that "len" command words have been added
31762306a36Sopenharmony_ci * to its pending list.	 This command includes the required
31862306a36Sopenharmony_ci * CVMX_SYNCWS before the doorbell ring.
31962306a36Sopenharmony_ci *
32062306a36Sopenharmony_ci * @port:   Port the packet is for
32162306a36Sopenharmony_ci * @queue:  Queue the packet is for
32262306a36Sopenharmony_ci * @len:    Length of the command in 64 bit words
32362306a36Sopenharmony_ci */
32462306a36Sopenharmony_cistatic inline void cvmx_pko_doorbell(uint64_t port, uint64_t queue,
32562306a36Sopenharmony_ci				     uint64_t len)
32662306a36Sopenharmony_ci{
32762306a36Sopenharmony_ci	cvmx_pko_doorbell_address_t ptr;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	ptr.u64 = 0;
33062306a36Sopenharmony_ci	ptr.s.mem_space = CVMX_IO_SEG;
33162306a36Sopenharmony_ci	ptr.s.did = CVMX_OCT_DID_PKT_SEND;
33262306a36Sopenharmony_ci	ptr.s.is_io = 1;
33362306a36Sopenharmony_ci	ptr.s.port = port;
33462306a36Sopenharmony_ci	ptr.s.queue = queue;
33562306a36Sopenharmony_ci	/*
33662306a36Sopenharmony_ci	 * Need to make sure output queue data is in DRAM before
33762306a36Sopenharmony_ci	 * doorbell write.
33862306a36Sopenharmony_ci	 */
33962306a36Sopenharmony_ci	CVMX_SYNCWS;
34062306a36Sopenharmony_ci	cvmx_write_io(ptr.u64, len);
34162306a36Sopenharmony_ci}
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci/**
34462306a36Sopenharmony_ci * Prepare to send a packet.  This may initiate a tag switch to
34562306a36Sopenharmony_ci * get exclusive access to the output queue structure, and
34662306a36Sopenharmony_ci * performs other prep work for the packet send operation.
34762306a36Sopenharmony_ci *
34862306a36Sopenharmony_ci * cvmx_pko_send_packet_finish() MUST be called after this function is called,
34962306a36Sopenharmony_ci * and must be called with the same port/queue/use_locking arguments.
35062306a36Sopenharmony_ci *
35162306a36Sopenharmony_ci * The use_locking parameter allows the caller to use three
35262306a36Sopenharmony_ci * possible locking modes.
35362306a36Sopenharmony_ci * - CVMX_PKO_LOCK_NONE
35462306a36Sopenharmony_ci *	- PKO doesn't do any locking. It is the responsibility
35562306a36Sopenharmony_ci *	    of the application to make sure that no other core
35662306a36Sopenharmony_ci *	    is accessing the same queue at the same time.
35762306a36Sopenharmony_ci * - CVMX_PKO_LOCK_ATOMIC_TAG
35862306a36Sopenharmony_ci *	- PKO performs an atomic tagswitch to insure exclusive
35962306a36Sopenharmony_ci *	    access to the output queue. This will maintain
36062306a36Sopenharmony_ci *	    packet ordering on output.
36162306a36Sopenharmony_ci * - CVMX_PKO_LOCK_CMD_QUEUE
36262306a36Sopenharmony_ci *	- PKO uses the common command queue locks to insure
36362306a36Sopenharmony_ci *	    exclusive access to the output queue. This is a
36462306a36Sopenharmony_ci *	    memory based ll/sc. This is the most portable
36562306a36Sopenharmony_ci *	    locking mechanism.
36662306a36Sopenharmony_ci *
36762306a36Sopenharmony_ci * NOTE: If atomic locking is used, the POW entry CANNOT be
36862306a36Sopenharmony_ci * descheduled, as it does not contain a valid WQE pointer.
36962306a36Sopenharmony_ci *
37062306a36Sopenharmony_ci * @port:   Port to send it on
37162306a36Sopenharmony_ci * @queue:  Queue to use
37262306a36Sopenharmony_ci * @use_locking: CVMX_PKO_LOCK_NONE, CVMX_PKO_LOCK_ATOMIC_TAG, or
37362306a36Sopenharmony_ci *		 CVMX_PKO_LOCK_CMD_QUEUE
37462306a36Sopenharmony_ci */
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_cistatic inline void cvmx_pko_send_packet_prepare(uint64_t port, uint64_t queue,
37762306a36Sopenharmony_ci						cvmx_pko_lock_t use_locking)
37862306a36Sopenharmony_ci{
37962306a36Sopenharmony_ci	if (use_locking == CVMX_PKO_LOCK_ATOMIC_TAG) {
38062306a36Sopenharmony_ci		/*
38162306a36Sopenharmony_ci		 * Must do a full switch here to handle all cases.  We
38262306a36Sopenharmony_ci		 * use a fake WQE pointer, as the POW does not access
38362306a36Sopenharmony_ci		 * this memory.	 The WQE pointer and group are only
38462306a36Sopenharmony_ci		 * used if this work is descheduled, which is not
38562306a36Sopenharmony_ci		 * supported by the
38662306a36Sopenharmony_ci		 * cvmx_pko_send_packet_prepare/cvmx_pko_send_packet_finish
38762306a36Sopenharmony_ci		 * combination.	 Note that this is a special case in
38862306a36Sopenharmony_ci		 * which these fake values can be used - this is not a
38962306a36Sopenharmony_ci		 * general technique.
39062306a36Sopenharmony_ci		 */
39162306a36Sopenharmony_ci		uint32_t tag =
39262306a36Sopenharmony_ci		    CVMX_TAG_SW_BITS_INTERNAL << CVMX_TAG_SW_SHIFT |
39362306a36Sopenharmony_ci		    CVMX_TAG_SUBGROUP_PKO << CVMX_TAG_SUBGROUP_SHIFT |
39462306a36Sopenharmony_ci		    (CVMX_TAG_SUBGROUP_MASK & queue);
39562306a36Sopenharmony_ci		cvmx_pow_tag_sw_full((struct cvmx_wqe *) cvmx_phys_to_ptr(0x80), tag,
39662306a36Sopenharmony_ci				     CVMX_POW_TAG_TYPE_ATOMIC, 0);
39762306a36Sopenharmony_ci	}
39862306a36Sopenharmony_ci}
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci/**
40162306a36Sopenharmony_ci * Complete packet output. cvmx_pko_send_packet_prepare() must be
40262306a36Sopenharmony_ci * called exactly once before this, and the same parameters must be
40362306a36Sopenharmony_ci * passed to both cvmx_pko_send_packet_prepare() and
40462306a36Sopenharmony_ci * cvmx_pko_send_packet_finish().
40562306a36Sopenharmony_ci *
40662306a36Sopenharmony_ci * @port:   Port to send it on
40762306a36Sopenharmony_ci * @queue:  Queue to use
40862306a36Sopenharmony_ci * @pko_command:
40962306a36Sopenharmony_ci *		 PKO HW command word
41062306a36Sopenharmony_ci * @packet: Packet to send
41162306a36Sopenharmony_ci * @use_locking: CVMX_PKO_LOCK_NONE, CVMX_PKO_LOCK_ATOMIC_TAG, or
41262306a36Sopenharmony_ci *		 CVMX_PKO_LOCK_CMD_QUEUE
41362306a36Sopenharmony_ci *
41462306a36Sopenharmony_ci * Returns: CVMX_PKO_SUCCESS on success, or error code on
41562306a36Sopenharmony_ci * failure of output
41662306a36Sopenharmony_ci */
41762306a36Sopenharmony_cistatic inline cvmx_pko_status_t cvmx_pko_send_packet_finish(
41862306a36Sopenharmony_ci	uint64_t port,
41962306a36Sopenharmony_ci	uint64_t queue,
42062306a36Sopenharmony_ci	union cvmx_pko_command_word0 pko_command,
42162306a36Sopenharmony_ci	union cvmx_buf_ptr packet,
42262306a36Sopenharmony_ci	cvmx_pko_lock_t use_locking)
42362306a36Sopenharmony_ci{
42462306a36Sopenharmony_ci	cvmx_cmd_queue_result_t result;
42562306a36Sopenharmony_ci	if (use_locking == CVMX_PKO_LOCK_ATOMIC_TAG)
42662306a36Sopenharmony_ci		cvmx_pow_tag_sw_wait();
42762306a36Sopenharmony_ci	result = cvmx_cmd_queue_write2(CVMX_CMD_QUEUE_PKO(queue),
42862306a36Sopenharmony_ci				       (use_locking == CVMX_PKO_LOCK_CMD_QUEUE),
42962306a36Sopenharmony_ci				       pko_command.u64, packet.u64);
43062306a36Sopenharmony_ci	if (likely(result == CVMX_CMD_QUEUE_SUCCESS)) {
43162306a36Sopenharmony_ci		cvmx_pko_doorbell(port, queue, 2);
43262306a36Sopenharmony_ci		return CVMX_PKO_SUCCESS;
43362306a36Sopenharmony_ci	} else if ((result == CVMX_CMD_QUEUE_NO_MEMORY)
43462306a36Sopenharmony_ci		   || (result == CVMX_CMD_QUEUE_FULL)) {
43562306a36Sopenharmony_ci		return CVMX_PKO_NO_MEMORY;
43662306a36Sopenharmony_ci	} else {
43762306a36Sopenharmony_ci		return CVMX_PKO_INVALID_QUEUE;
43862306a36Sopenharmony_ci	}
43962306a36Sopenharmony_ci}
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci/**
44262306a36Sopenharmony_ci * Complete packet output. cvmx_pko_send_packet_prepare() must be
44362306a36Sopenharmony_ci * called exactly once before this, and the same parameters must be
44462306a36Sopenharmony_ci * passed to both cvmx_pko_send_packet_prepare() and
44562306a36Sopenharmony_ci * cvmx_pko_send_packet_finish().
44662306a36Sopenharmony_ci *
44762306a36Sopenharmony_ci * @port:   Port to send it on
44862306a36Sopenharmony_ci * @queue:  Queue to use
44962306a36Sopenharmony_ci * @pko_command:
45062306a36Sopenharmony_ci *		 PKO HW command word
45162306a36Sopenharmony_ci * @packet: Packet to send
45262306a36Sopenharmony_ci * @addr: Plysical address of a work queue entry or physical address
45362306a36Sopenharmony_ci *	  to zero on complete.
45462306a36Sopenharmony_ci * @use_locking: CVMX_PKO_LOCK_NONE, CVMX_PKO_LOCK_ATOMIC_TAG, or
45562306a36Sopenharmony_ci *		 CVMX_PKO_LOCK_CMD_QUEUE
45662306a36Sopenharmony_ci *
45762306a36Sopenharmony_ci * Returns: CVMX_PKO_SUCCESS on success, or error code on
45862306a36Sopenharmony_ci * failure of output
45962306a36Sopenharmony_ci */
46062306a36Sopenharmony_cistatic inline cvmx_pko_status_t cvmx_pko_send_packet_finish3(
46162306a36Sopenharmony_ci	uint64_t port,
46262306a36Sopenharmony_ci	uint64_t queue,
46362306a36Sopenharmony_ci	union cvmx_pko_command_word0 pko_command,
46462306a36Sopenharmony_ci	union cvmx_buf_ptr packet,
46562306a36Sopenharmony_ci	uint64_t addr,
46662306a36Sopenharmony_ci	cvmx_pko_lock_t use_locking)
46762306a36Sopenharmony_ci{
46862306a36Sopenharmony_ci	cvmx_cmd_queue_result_t result;
46962306a36Sopenharmony_ci	if (use_locking == CVMX_PKO_LOCK_ATOMIC_TAG)
47062306a36Sopenharmony_ci		cvmx_pow_tag_sw_wait();
47162306a36Sopenharmony_ci	result = cvmx_cmd_queue_write3(CVMX_CMD_QUEUE_PKO(queue),
47262306a36Sopenharmony_ci				       (use_locking == CVMX_PKO_LOCK_CMD_QUEUE),
47362306a36Sopenharmony_ci				       pko_command.u64, packet.u64, addr);
47462306a36Sopenharmony_ci	if (likely(result == CVMX_CMD_QUEUE_SUCCESS)) {
47562306a36Sopenharmony_ci		cvmx_pko_doorbell(port, queue, 3);
47662306a36Sopenharmony_ci		return CVMX_PKO_SUCCESS;
47762306a36Sopenharmony_ci	} else if ((result == CVMX_CMD_QUEUE_NO_MEMORY)
47862306a36Sopenharmony_ci		   || (result == CVMX_CMD_QUEUE_FULL)) {
47962306a36Sopenharmony_ci		return CVMX_PKO_NO_MEMORY;
48062306a36Sopenharmony_ci	} else {
48162306a36Sopenharmony_ci		return CVMX_PKO_INVALID_QUEUE;
48262306a36Sopenharmony_ci	}
48362306a36Sopenharmony_ci}
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci/**
48662306a36Sopenharmony_ci * Return the pko output queue associated with a port and a specific core.
48762306a36Sopenharmony_ci * In normal mode (PKO lockless operation is disabled), the value returned
48862306a36Sopenharmony_ci * is the base queue.
48962306a36Sopenharmony_ci *
49062306a36Sopenharmony_ci * @port:   Port number
49162306a36Sopenharmony_ci * @core:   Core to get queue for
49262306a36Sopenharmony_ci *
49362306a36Sopenharmony_ci * Returns Core-specific output queue
49462306a36Sopenharmony_ci */
49562306a36Sopenharmony_cistatic inline int cvmx_pko_get_base_queue_per_core(int port, int core)
49662306a36Sopenharmony_ci{
49762306a36Sopenharmony_ci#ifndef CVMX_HELPER_PKO_MAX_PORTS_INTERFACE0
49862306a36Sopenharmony_ci#define CVMX_HELPER_PKO_MAX_PORTS_INTERFACE0 16
49962306a36Sopenharmony_ci#endif
50062306a36Sopenharmony_ci#ifndef CVMX_HELPER_PKO_MAX_PORTS_INTERFACE1
50162306a36Sopenharmony_ci#define CVMX_HELPER_PKO_MAX_PORTS_INTERFACE1 16
50262306a36Sopenharmony_ci#endif
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci	if (port < CVMX_PKO_MAX_PORTS_INTERFACE0)
50562306a36Sopenharmony_ci		return port * CVMX_PKO_QUEUES_PER_PORT_INTERFACE0 + core;
50662306a36Sopenharmony_ci	else if (port >= 16 && port < 16 + CVMX_PKO_MAX_PORTS_INTERFACE1)
50762306a36Sopenharmony_ci		return CVMX_PKO_MAX_PORTS_INTERFACE0 *
50862306a36Sopenharmony_ci		    CVMX_PKO_QUEUES_PER_PORT_INTERFACE0 + (port -
50962306a36Sopenharmony_ci							   16) *
51062306a36Sopenharmony_ci		    CVMX_PKO_QUEUES_PER_PORT_INTERFACE1 + core;
51162306a36Sopenharmony_ci	else if ((port >= 32) && (port < 36))
51262306a36Sopenharmony_ci		return CVMX_PKO_MAX_PORTS_INTERFACE0 *
51362306a36Sopenharmony_ci		    CVMX_PKO_QUEUES_PER_PORT_INTERFACE0 +
51462306a36Sopenharmony_ci		    CVMX_PKO_MAX_PORTS_INTERFACE1 *
51562306a36Sopenharmony_ci		    CVMX_PKO_QUEUES_PER_PORT_INTERFACE1 + (port -
51662306a36Sopenharmony_ci							   32) *
51762306a36Sopenharmony_ci		    CVMX_PKO_QUEUES_PER_PORT_PCI;
51862306a36Sopenharmony_ci	else if ((port >= 36) && (port < 40))
51962306a36Sopenharmony_ci		return CVMX_PKO_MAX_PORTS_INTERFACE0 *
52062306a36Sopenharmony_ci		    CVMX_PKO_QUEUES_PER_PORT_INTERFACE0 +
52162306a36Sopenharmony_ci		    CVMX_PKO_MAX_PORTS_INTERFACE1 *
52262306a36Sopenharmony_ci		    CVMX_PKO_QUEUES_PER_PORT_INTERFACE1 +
52362306a36Sopenharmony_ci		    4 * CVMX_PKO_QUEUES_PER_PORT_PCI + (port -
52462306a36Sopenharmony_ci							36) *
52562306a36Sopenharmony_ci		    CVMX_PKO_QUEUES_PER_PORT_LOOP;
52662306a36Sopenharmony_ci	else
52762306a36Sopenharmony_ci		/* Given the limit on the number of ports we can map to
52862306a36Sopenharmony_ci		 * CVMX_MAX_OUTPUT_QUEUES_STATIC queues (currently 256,
52962306a36Sopenharmony_ci		 * divided among all cores), the remaining unmapped ports
53062306a36Sopenharmony_ci		 * are assigned an illegal queue number */
53162306a36Sopenharmony_ci		return CVMX_PKO_ILLEGAL_QUEUE;
53262306a36Sopenharmony_ci}
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci/**
53562306a36Sopenharmony_ci * For a given port number, return the base pko output queue
53662306a36Sopenharmony_ci * for the port.
53762306a36Sopenharmony_ci *
53862306a36Sopenharmony_ci * @port:   Port number
53962306a36Sopenharmony_ci * Returns Base output queue
54062306a36Sopenharmony_ci */
54162306a36Sopenharmony_cistatic inline int cvmx_pko_get_base_queue(int port)
54262306a36Sopenharmony_ci{
54362306a36Sopenharmony_ci	if (OCTEON_IS_MODEL(OCTEON_CN68XX))
54462306a36Sopenharmony_ci		return port;
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci	return cvmx_pko_get_base_queue_per_core(port, 0);
54762306a36Sopenharmony_ci}
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci/**
55062306a36Sopenharmony_ci * For a given port number, return the number of pko output queues.
55162306a36Sopenharmony_ci *
55262306a36Sopenharmony_ci * @port:   Port number
55362306a36Sopenharmony_ci * Returns Number of output queues
55462306a36Sopenharmony_ci */
55562306a36Sopenharmony_cistatic inline int cvmx_pko_get_num_queues(int port)
55662306a36Sopenharmony_ci{
55762306a36Sopenharmony_ci	if (port < 16)
55862306a36Sopenharmony_ci		return CVMX_PKO_QUEUES_PER_PORT_INTERFACE0;
55962306a36Sopenharmony_ci	else if (port < 32)
56062306a36Sopenharmony_ci		return CVMX_PKO_QUEUES_PER_PORT_INTERFACE1;
56162306a36Sopenharmony_ci	else if (port < 36)
56262306a36Sopenharmony_ci		return CVMX_PKO_QUEUES_PER_PORT_PCI;
56362306a36Sopenharmony_ci	else if (port < 40)
56462306a36Sopenharmony_ci		return CVMX_PKO_QUEUES_PER_PORT_LOOP;
56562306a36Sopenharmony_ci	else
56662306a36Sopenharmony_ci		return 0;
56762306a36Sopenharmony_ci}
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci/**
57062306a36Sopenharmony_ci * Get the status counters for a port.
57162306a36Sopenharmony_ci *
57262306a36Sopenharmony_ci * @port_num: Port number to get statistics for.
57362306a36Sopenharmony_ci * @clear:    Set to 1 to clear the counters after they are read
57462306a36Sopenharmony_ci * @status:   Where to put the results.
57562306a36Sopenharmony_ci */
57662306a36Sopenharmony_cistatic inline void cvmx_pko_get_port_status(uint64_t port_num, uint64_t clear,
57762306a36Sopenharmony_ci					    cvmx_pko_port_status_t *status)
57862306a36Sopenharmony_ci{
57962306a36Sopenharmony_ci	union cvmx_pko_reg_read_idx pko_reg_read_idx;
58062306a36Sopenharmony_ci	union cvmx_pko_mem_count0 pko_mem_count0;
58162306a36Sopenharmony_ci	union cvmx_pko_mem_count1 pko_mem_count1;
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci	pko_reg_read_idx.u64 = 0;
58462306a36Sopenharmony_ci	pko_reg_read_idx.s.index = port_num;
58562306a36Sopenharmony_ci	cvmx_write_csr(CVMX_PKO_REG_READ_IDX, pko_reg_read_idx.u64);
58662306a36Sopenharmony_ci
58762306a36Sopenharmony_ci	pko_mem_count0.u64 = cvmx_read_csr(CVMX_PKO_MEM_COUNT0);
58862306a36Sopenharmony_ci	status->packets = pko_mem_count0.s.count;
58962306a36Sopenharmony_ci	if (clear) {
59062306a36Sopenharmony_ci		pko_mem_count0.s.count = port_num;
59162306a36Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_MEM_COUNT0, pko_mem_count0.u64);
59262306a36Sopenharmony_ci	}
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci	pko_mem_count1.u64 = cvmx_read_csr(CVMX_PKO_MEM_COUNT1);
59562306a36Sopenharmony_ci	status->octets = pko_mem_count1.s.count;
59662306a36Sopenharmony_ci	if (clear) {
59762306a36Sopenharmony_ci		pko_mem_count1.s.count = port_num;
59862306a36Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_MEM_COUNT1, pko_mem_count1.u64);
59962306a36Sopenharmony_ci	}
60062306a36Sopenharmony_ci
60162306a36Sopenharmony_ci	if (OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
60262306a36Sopenharmony_ci		union cvmx_pko_mem_debug9 debug9;
60362306a36Sopenharmony_ci		pko_reg_read_idx.s.index = cvmx_pko_get_base_queue(port_num);
60462306a36Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_REG_READ_IDX, pko_reg_read_idx.u64);
60562306a36Sopenharmony_ci		debug9.u64 = cvmx_read_csr(CVMX_PKO_MEM_DEBUG9);
60662306a36Sopenharmony_ci		status->doorbell = debug9.cn38xx.doorbell;
60762306a36Sopenharmony_ci	} else {
60862306a36Sopenharmony_ci		union cvmx_pko_mem_debug8 debug8;
60962306a36Sopenharmony_ci		pko_reg_read_idx.s.index = cvmx_pko_get_base_queue(port_num);
61062306a36Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_REG_READ_IDX, pko_reg_read_idx.u64);
61162306a36Sopenharmony_ci		debug8.u64 = cvmx_read_csr(CVMX_PKO_MEM_DEBUG8);
61262306a36Sopenharmony_ci		status->doorbell = debug8.cn50xx.doorbell;
61362306a36Sopenharmony_ci	}
61462306a36Sopenharmony_ci}
61562306a36Sopenharmony_ci
61662306a36Sopenharmony_ci/**
61762306a36Sopenharmony_ci * Rate limit a PKO port to a max packets/sec. This function is only
61862306a36Sopenharmony_ci * supported on CN57XX, CN56XX, CN55XX, and CN54XX.
61962306a36Sopenharmony_ci *
62062306a36Sopenharmony_ci * @port:      Port to rate limit
62162306a36Sopenharmony_ci * @packets_s: Maximum packet/sec
62262306a36Sopenharmony_ci * @burst:     Maximum number of packets to burst in a row before rate
62362306a36Sopenharmony_ci *		    limiting cuts in.
62462306a36Sopenharmony_ci *
62562306a36Sopenharmony_ci * Returns Zero on success, negative on failure
62662306a36Sopenharmony_ci */
62762306a36Sopenharmony_ciextern int cvmx_pko_rate_limit_packets(int port, int packets_s, int burst);
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci/**
63062306a36Sopenharmony_ci * Rate limit a PKO port to a max bits/sec. This function is only
63162306a36Sopenharmony_ci * supported on CN57XX, CN56XX, CN55XX, and CN54XX.
63262306a36Sopenharmony_ci *
63362306a36Sopenharmony_ci * @port:   Port to rate limit
63462306a36Sopenharmony_ci * @bits_s: PKO rate limit in bits/sec
63562306a36Sopenharmony_ci * @burst:  Maximum number of bits to burst before rate
63662306a36Sopenharmony_ci *		 limiting cuts in.
63762306a36Sopenharmony_ci *
63862306a36Sopenharmony_ci * Returns Zero on success, negative on failure
63962306a36Sopenharmony_ci */
64062306a36Sopenharmony_ciextern int cvmx_pko_rate_limit_bits(int port, uint64_t bits_s, int burst);
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_ci#endif /* __CVMX_PKO_H__ */
643