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**************************************/
30 * Interface to the hardware Packet Output unit.
32 * Starting with SDK 1.7.0, the PKO output functions now support
33 * two types of locking. CVMX_PKO_LOCK_ATOMIC_TAG continues to
34 * function similarly to previous SDKs by using POW atomic tags
35 * to preserve ordering and exclusivity. As a new option, you
36 * can now pass CVMX_PKO_LOCK_CMD_QUEUE which uses a ll/sc
37 * memory based locking instead. This locking has the advantage
38 * of not affecting the tag state but doesn't preserve packet
39 * ordering. CVMX_PKO_LOCK_CMD_QUEUE is appropriate in most
40 * generic code while CVMX_PKO_LOCK_CMD_QUEUE should be used
41 * with hand tuned fast path code.
43 * Some of other SDK differences visible to the command command
45 * - PKO indexes are no longer stored in the FAU. A large
46 * percentage of the FAU register block used to be tied up
47 * maintaining PKO queue pointers. These are now stored in a
49 * - The PKO <b>use_locking</b> parameter can now have a global
50 * effect. Since all application use the same named block,
51 * queue locking correctly applies across all operating
52 * systems when using CVMX_PKO_LOCK_CMD_QUEUE.
53 * - PKO 3 word commands are now supported. Use
54 * cvmx_pko_send_packet_finish3().
58 #ifndef __CVMX_PKO_H__
59 #define __CVMX_PKO_H__
61 #include <asm/octeon/cvmx-fpa.h>
62 #include <asm/octeon/cvmx-pow.h>
63 #include <asm/octeon/cvmx-cmd-queue.h>
64 #include <asm/octeon/cvmx-pko-defs.h>
66 /* Adjust the command buffer size by 1 word so that in the case of using only
67 * two word PKO commands no command words stradle buffers. The useful values
68 * for this are 0 and 1. */
69 #define CVMX_PKO_COMMAND_BUFFER_SIZE_ADJUST (1)
71 #define CVMX_PKO_MAX_OUTPUT_QUEUES_STATIC 256
72 #define CVMX_PKO_MAX_OUTPUT_QUEUES ((OCTEON_IS_MODEL(OCTEON_CN31XX) || \
73 OCTEON_IS_MODEL(OCTEON_CN3010) || OCTEON_IS_MODEL(OCTEON_CN3005) || \
74 OCTEON_IS_MODEL(OCTEON_CN50XX)) ? 32 : \
75 (OCTEON_IS_MODEL(OCTEON_CN58XX) || \
76 OCTEON_IS_MODEL(OCTEON_CN56XX)) ? 256 : 128)
77 #define CVMX_PKO_NUM_OUTPUT_PORTS 40
78 /* use this for queues that are not used */
79 #define CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID 63
80 #define CVMX_PKO_QUEUE_STATIC_PRIORITY 9
81 #define CVMX_PKO_ILLEGAL_QUEUE 0xFFFF
82 #define CVMX_PKO_MAX_QUEUE_DEPTH 0
86 CVMX_PKO_INVALID_PORT
,
87 CVMX_PKO_INVALID_QUEUE
,
88 CVMX_PKO_INVALID_PRIORITY
,
90 CVMX_PKO_PORT_ALREADY_SETUP
,
91 CVMX_PKO_CMD_QUEUE_INIT_ERROR
95 * This enumeration represents the differnet locking modes supported by PKO.
99 * PKO doesn't do any locking. It is the responsibility of the
100 * application to make sure that no other core is accessing
101 * the same queue at the same time
103 CVMX_PKO_LOCK_NONE
= 0,
105 * PKO performs an atomic tagswitch to insure exclusive access
106 * to the output queue. This will maintain packet ordering on
109 CVMX_PKO_LOCK_ATOMIC_TAG
= 1,
111 * PKO uses the common command queue locks to insure exclusive
112 * access to the output queue. This is a memory based
113 * ll/sc. This is the most portable locking mechanism.
115 CVMX_PKO_LOCK_CMD_QUEUE
= 2,
122 } cvmx_pko_port_status_t
;
125 * This structure defines the address to use on a packet enqueue
130 #ifdef __BIG_ENDIAN_BITFIELD
131 /* Must CVMX_IO_SEG */
132 uint64_t mem_space
:2;
134 uint64_t reserved
:13;
137 /* The ID of the device on the non-coherent bus */
140 uint64_t reserved2
:4;
142 uint64_t reserved3
:18;
144 * The hardware likes to have the output port in
145 * addition to the output queue,
149 * The output queue to send the packet to (0-127 are
154 uint64_t reserved4
:3;
156 uint64_t reserved4
:3;
159 uint64_t reserved3
:15;
160 uint64_t reserved2
:4;
163 uint64_t reserved
:13;
164 uint64_t mem_space
:2;
167 } cvmx_pko_doorbell_address_t
;
170 * Structure of the first packet output command word.
175 #ifdef __BIG_ENDIAN_BITFIELD
177 * The size of the reg1 operation - could be 8, 16,
182 * The size of the reg0 operation - could be 8, 16,
187 * If set, subtract 1, if clear, subtract packet
192 * The register, subtract will be done if reg1 is
196 /* If set, subtract 1, if clear, subtract packet size */
198 /* The register, subtract will be done if reg0 is non-zero */
201 * When set, interpret segment pointer and segment
202 * bytes in little endian order.
206 * When set, packet data not allocated in L2 cache by
211 * If set and rsp is set, word3 contains a pointer to
212 * a work queue entry.
215 /* If set, the hardware will send a response when done */
218 * If set, the supplied pkt_ptr is really a pointer to
219 * a list of pkt_ptr's.
223 * If ipoffp1 is non zero, (ipoffp1-1) is the number
224 * of bytes to IP header, and the hardware will
225 * calculate and insert the UDP/TCP checksum.
229 * If set, ignore the I bit (force to zero) from all
230 * pointer structures.
234 * If clear, the hardware will attempt to free the
235 * buffers containing the packet.
239 * The total number of segs in the packet, if gather
240 * set, also gather list length.
243 /* Including L2, but no trailing CRC */
244 uint64_t total_bytes
:16;
246 uint64_t total_bytes
:16;
264 } cvmx_pko_command_word0_t
;
266 /* CSR typedefs have been moved to cvmx-csr-*.h */
269 * Definition of internal state for Packet output processing
272 /* ptr to start of buffer, offset kept in FAU reg */
274 } cvmx_pko_state_elem_t
;
277 * Call before any other calls to initialize the packet
280 extern void cvmx_pko_initialize_global(void);
281 extern int cvmx_pko_initialize_local(void);
284 * Enables the packet output hardware. It must already be
287 extern void cvmx_pko_enable(void);
290 * Disables the packet output. Does not affect any configuration.
292 extern void cvmx_pko_disable(void);
295 * Shutdown and free resources required by packet output.
298 extern void cvmx_pko_shutdown(void);
301 * Configure a output port and the associated queues for use.
303 * @port: Port to configure.
304 * @base_queue: First queue number to associate with this port.
305 * @num_queues: Number of queues t oassociate with this port
306 * @priority: Array of priority levels for each queue. Values are
307 * allowed to be 1-8. A value of 8 get 8 times the traffic
308 * of a value of 1. There must be num_queues elements in the
311 extern cvmx_pko_status_t
cvmx_pko_config_port(uint64_t port
,
314 const uint64_t priority
[]);
317 * Ring the packet output doorbell. This tells the packet
318 * output hardware that "len" command words have been added
319 * to its pending list. This command includes the required
320 * CVMX_SYNCWS before the doorbell ring.
322 * @port: Port the packet is for
323 * @queue: Queue the packet is for
324 * @len: Length of the command in 64 bit words
326 static inline void cvmx_pko_doorbell(uint64_t port
, uint64_t queue
,
329 cvmx_pko_doorbell_address_t ptr
;
332 ptr
.s
.mem_space
= CVMX_IO_SEG
;
333 ptr
.s
.did
= CVMX_OCT_DID_PKT_SEND
;
338 * Need to make sure output queue data is in DRAM before
342 cvmx_write_io(ptr
.u64
, len
);
346 * Prepare to send a packet. This may initiate a tag switch to
347 * get exclusive access to the output queue structure, and
348 * performs other prep work for the packet send operation.
350 * cvmx_pko_send_packet_finish() MUST be called after this function is called,
351 * and must be called with the same port/queue/use_locking arguments.
353 * The use_locking parameter allows the caller to use three
354 * possible locking modes.
355 * - CVMX_PKO_LOCK_NONE
356 * - PKO doesn't do any locking. It is the responsibility
357 * of the application to make sure that no other core
358 * is accessing the same queue at the same time.
359 * - CVMX_PKO_LOCK_ATOMIC_TAG
360 * - PKO performs an atomic tagswitch to insure exclusive
361 * access to the output queue. This will maintain
362 * packet ordering on output.
363 * - CVMX_PKO_LOCK_CMD_QUEUE
364 * - PKO uses the common command queue locks to insure
365 * exclusive access to the output queue. This is a
366 * memory based ll/sc. This is the most portable
369 * NOTE: If atomic locking is used, the POW entry CANNOT be
370 * descheduled, as it does not contain a valid WQE pointer.
372 * @port: Port to send it on
373 * @queue: Queue to use
374 * @use_locking: CVMX_PKO_LOCK_NONE, CVMX_PKO_LOCK_ATOMIC_TAG, or
375 * CVMX_PKO_LOCK_CMD_QUEUE
378 static inline void cvmx_pko_send_packet_prepare(uint64_t port
, uint64_t queue
,
379 cvmx_pko_lock_t use_locking
)
381 if (use_locking
== CVMX_PKO_LOCK_ATOMIC_TAG
) {
383 * Must do a full switch here to handle all cases. We
384 * use a fake WQE pointer, as the POW does not access
385 * this memory. The WQE pointer and group are only
386 * used if this work is descheduled, which is not
388 * cvmx_pko_send_packet_prepare/cvmx_pko_send_packet_finish
389 * combination. Note that this is a special case in
390 * which these fake values can be used - this is not a
394 CVMX_TAG_SW_BITS_INTERNAL
<< CVMX_TAG_SW_SHIFT
|
395 CVMX_TAG_SUBGROUP_PKO
<< CVMX_TAG_SUBGROUP_SHIFT
|
396 (CVMX_TAG_SUBGROUP_MASK
& queue
);
397 cvmx_pow_tag_sw_full((cvmx_wqe_t
*) cvmx_phys_to_ptr(0x80), tag
,
398 CVMX_POW_TAG_TYPE_ATOMIC
, 0);
403 * Complete packet output. cvmx_pko_send_packet_prepare() must be
404 * called exactly once before this, and the same parameters must be
405 * passed to both cvmx_pko_send_packet_prepare() and
406 * cvmx_pko_send_packet_finish().
408 * @port: Port to send it on
409 * @queue: Queue to use
411 * PKO HW command word
412 * @packet: Packet to send
413 * @use_locking: CVMX_PKO_LOCK_NONE, CVMX_PKO_LOCK_ATOMIC_TAG, or
414 * CVMX_PKO_LOCK_CMD_QUEUE
416 * Returns returns CVMX_PKO_SUCCESS on success, or error code on
419 static inline cvmx_pko_status_t
cvmx_pko_send_packet_finish(
422 cvmx_pko_command_word0_t pko_command
,
423 union cvmx_buf_ptr packet
,
424 cvmx_pko_lock_t use_locking
)
426 cvmx_cmd_queue_result_t result
;
427 if (use_locking
== CVMX_PKO_LOCK_ATOMIC_TAG
)
428 cvmx_pow_tag_sw_wait();
429 result
= cvmx_cmd_queue_write2(CVMX_CMD_QUEUE_PKO(queue
),
430 (use_locking
== CVMX_PKO_LOCK_CMD_QUEUE
),
431 pko_command
.u64
, packet
.u64
);
432 if (likely(result
== CVMX_CMD_QUEUE_SUCCESS
)) {
433 cvmx_pko_doorbell(port
, queue
, 2);
434 return CVMX_PKO_SUCCESS
;
435 } else if ((result
== CVMX_CMD_QUEUE_NO_MEMORY
)
436 || (result
== CVMX_CMD_QUEUE_FULL
)) {
437 return CVMX_PKO_NO_MEMORY
;
439 return CVMX_PKO_INVALID_QUEUE
;
444 * Complete packet output. cvmx_pko_send_packet_prepare() must be
445 * called exactly once before this, and the same parameters must be
446 * passed to both cvmx_pko_send_packet_prepare() and
447 * cvmx_pko_send_packet_finish().
449 * @port: Port to send it on
450 * @queue: Queue to use
452 * PKO HW command word
453 * @packet: Packet to send
454 * @addr: Plysical address of a work queue entry or physical address
455 * to zero on complete.
456 * @use_locking: CVMX_PKO_LOCK_NONE, CVMX_PKO_LOCK_ATOMIC_TAG, or
457 * CVMX_PKO_LOCK_CMD_QUEUE
459 * Returns returns CVMX_PKO_SUCCESS on success, or error code on
462 static inline cvmx_pko_status_t
cvmx_pko_send_packet_finish3(
465 cvmx_pko_command_word0_t pko_command
,
466 union cvmx_buf_ptr packet
,
468 cvmx_pko_lock_t use_locking
)
470 cvmx_cmd_queue_result_t result
;
471 if (use_locking
== CVMX_PKO_LOCK_ATOMIC_TAG
)
472 cvmx_pow_tag_sw_wait();
473 result
= cvmx_cmd_queue_write3(CVMX_CMD_QUEUE_PKO(queue
),
474 (use_locking
== CVMX_PKO_LOCK_CMD_QUEUE
),
475 pko_command
.u64
, packet
.u64
, addr
);
476 if (likely(result
== CVMX_CMD_QUEUE_SUCCESS
)) {
477 cvmx_pko_doorbell(port
, queue
, 3);
478 return CVMX_PKO_SUCCESS
;
479 } else if ((result
== CVMX_CMD_QUEUE_NO_MEMORY
)
480 || (result
== CVMX_CMD_QUEUE_FULL
)) {
481 return CVMX_PKO_NO_MEMORY
;
483 return CVMX_PKO_INVALID_QUEUE
;
488 * Return the pko output queue associated with a port and a specific core.
489 * In normal mode (PKO lockless operation is disabled), the value returned
493 * @core: Core to get queue for
495 * Returns Core-specific output queue
497 static inline int cvmx_pko_get_base_queue_per_core(int port
, int core
)
499 #ifndef CVMX_HELPER_PKO_MAX_PORTS_INTERFACE0
500 #define CVMX_HELPER_PKO_MAX_PORTS_INTERFACE0 16
502 #ifndef CVMX_HELPER_PKO_MAX_PORTS_INTERFACE1
503 #define CVMX_HELPER_PKO_MAX_PORTS_INTERFACE1 16
506 if (port
< CVMX_PKO_MAX_PORTS_INTERFACE0
)
507 return port
* CVMX_PKO_QUEUES_PER_PORT_INTERFACE0
+ core
;
508 else if (port
>= 16 && port
< 16 + CVMX_PKO_MAX_PORTS_INTERFACE1
)
509 return CVMX_PKO_MAX_PORTS_INTERFACE0
*
510 CVMX_PKO_QUEUES_PER_PORT_INTERFACE0
+ (port
-
512 CVMX_PKO_QUEUES_PER_PORT_INTERFACE1
+ core
;
513 else if ((port
>= 32) && (port
< 36))
514 return CVMX_PKO_MAX_PORTS_INTERFACE0
*
515 CVMX_PKO_QUEUES_PER_PORT_INTERFACE0
+
516 CVMX_PKO_MAX_PORTS_INTERFACE1
*
517 CVMX_PKO_QUEUES_PER_PORT_INTERFACE1
+ (port
-
519 CVMX_PKO_QUEUES_PER_PORT_PCI
;
520 else if ((port
>= 36) && (port
< 40))
521 return CVMX_PKO_MAX_PORTS_INTERFACE0
*
522 CVMX_PKO_QUEUES_PER_PORT_INTERFACE0
+
523 CVMX_PKO_MAX_PORTS_INTERFACE1
*
524 CVMX_PKO_QUEUES_PER_PORT_INTERFACE1
+
525 4 * CVMX_PKO_QUEUES_PER_PORT_PCI
+ (port
-
527 CVMX_PKO_QUEUES_PER_PORT_LOOP
;
529 /* Given the limit on the number of ports we can map to
530 * CVMX_MAX_OUTPUT_QUEUES_STATIC queues (currently 256,
531 * divided among all cores), the remaining unmapped ports
532 * are assigned an illegal queue number */
533 return CVMX_PKO_ILLEGAL_QUEUE
;
537 * For a given port number, return the base pko output queue
541 * Returns Base output queue
543 static inline int cvmx_pko_get_base_queue(int port
)
545 if (OCTEON_IS_MODEL(OCTEON_CN68XX
))
548 return cvmx_pko_get_base_queue_per_core(port
, 0);
552 * For a given port number, return the number of pko output queues.
555 * Returns Number of output queues
557 static inline int cvmx_pko_get_num_queues(int port
)
560 return CVMX_PKO_QUEUES_PER_PORT_INTERFACE0
;
562 return CVMX_PKO_QUEUES_PER_PORT_INTERFACE1
;
564 return CVMX_PKO_QUEUES_PER_PORT_PCI
;
566 return CVMX_PKO_QUEUES_PER_PORT_LOOP
;
572 * Get the status counters for a port.
574 * @port_num: Port number to get statistics for.
575 * @clear: Set to 1 to clear the counters after they are read
576 * @status: Where to put the results.
578 static inline void cvmx_pko_get_port_status(uint64_t port_num
, uint64_t clear
,
579 cvmx_pko_port_status_t
*status
)
581 union cvmx_pko_reg_read_idx pko_reg_read_idx
;
582 union cvmx_pko_mem_count0 pko_mem_count0
;
583 union cvmx_pko_mem_count1 pko_mem_count1
;
585 pko_reg_read_idx
.u64
= 0;
586 pko_reg_read_idx
.s
.index
= port_num
;
587 cvmx_write_csr(CVMX_PKO_REG_READ_IDX
, pko_reg_read_idx
.u64
);
589 pko_mem_count0
.u64
= cvmx_read_csr(CVMX_PKO_MEM_COUNT0
);
590 status
->packets
= pko_mem_count0
.s
.count
;
592 pko_mem_count0
.s
.count
= port_num
;
593 cvmx_write_csr(CVMX_PKO_MEM_COUNT0
, pko_mem_count0
.u64
);
596 pko_mem_count1
.u64
= cvmx_read_csr(CVMX_PKO_MEM_COUNT1
);
597 status
->octets
= pko_mem_count1
.s
.count
;
599 pko_mem_count1
.s
.count
= port_num
;
600 cvmx_write_csr(CVMX_PKO_MEM_COUNT1
, pko_mem_count1
.u64
);
603 if (OCTEON_IS_MODEL(OCTEON_CN3XXX
)) {
604 union cvmx_pko_mem_debug9 debug9
;
605 pko_reg_read_idx
.s
.index
= cvmx_pko_get_base_queue(port_num
);
606 cvmx_write_csr(CVMX_PKO_REG_READ_IDX
, pko_reg_read_idx
.u64
);
607 debug9
.u64
= cvmx_read_csr(CVMX_PKO_MEM_DEBUG9
);
608 status
->doorbell
= debug9
.cn38xx
.doorbell
;
610 union cvmx_pko_mem_debug8 debug8
;
611 pko_reg_read_idx
.s
.index
= cvmx_pko_get_base_queue(port_num
);
612 cvmx_write_csr(CVMX_PKO_REG_READ_IDX
, pko_reg_read_idx
.u64
);
613 debug8
.u64
= cvmx_read_csr(CVMX_PKO_MEM_DEBUG8
);
614 status
->doorbell
= debug8
.cn58xx
.doorbell
;
619 * Rate limit a PKO port to a max packets/sec. This function is only
620 * supported on CN57XX, CN56XX, CN55XX, and CN54XX.
622 * @port: Port to rate limit
623 * @packets_s: Maximum packet/sec
624 * @burst: Maximum number of packets to burst in a row before rate
627 * Returns Zero on success, negative on failure
629 extern int cvmx_pko_rate_limit_packets(int port
, int packets_s
, int burst
);
632 * Rate limit a PKO port to a max bits/sec. This function is only
633 * supported on CN57XX, CN56XX, CN55XX, and CN54XX.
635 * @port: Port to rate limit
636 * @bits_s: PKO rate limit in bits/sec
637 * @burst: Maximum number of bits to burst before rate
640 * Returns Zero on success, negative on failure
642 extern int cvmx_pko_rate_limit_bits(int port
, uint64_t bits_s
, int burst
);
644 #endif /* __CVMX_PKO_H__ */