42#ifndef __DEV_CAN_CAN_QUEUE_H
43#define __DEV_CAN_CAN_QUEUE_H
49#include <rtems/timespec.h>
50#include <rtems/status-checks.h>
51#include <rtems/thread.h>
79#define RTEMS_CAN_SLOTF_CMD ( 0x00ff )
83#define RTEMS_CAN_QUEUE_PRIO_NR ( 3 )
145#define CAN_FIFOF_DESTROY ( 1 << 15 )
146#define CAN_FIFOF_ERROR ( 1 << 14 )
147#define CAN_FIFOF_ERR2BLOCK ( 1 << 13 )
148#define CAN_FIFOF_BLOCK ( 1 << 12 )
149#define CAN_FIFOF_OVERRUN ( 1 << 11 )
150#define CAN_FIFOF_FULL ( 1 << 10 )
151#define CAN_FIFOF_EMPTY ( 1 << 9 )
152#define CAN_FIFOF_DEAD ( 1 << 8 )
153#define CAN_FIFOF_INACTIVE ( 1 << 7 )
154#define CAN_FIFOF_FREEONEMPTY ( 1 << 6 )
155#define CAN_FIFOF_READY ( 1 << 5 )
156#define CAN_FIFOF_NOTIFYPEND ( 1 << 4 )
157#define CAN_FIFOF_RTL_MEM ( 1 << 3 )
169static inline int canque_fifo_test_fl(
struct canque_fifo_t *fifo,
int flag )
171 return ( atomic_load( &fifo->
fifo_flags ) &flag ) ? 1 : 0;
183static inline void canque_fifo_set_fl(
struct canque_fifo_t *fifo,
int flag )
197static inline void canque_fifo_clear_fl(
struct canque_fifo_t *fifo,
int flag )
212static inline int canque_fifo_test_and_set_fl(
217 return ( atomic_fetch_or( &fifo->
fifo_flags, flag ) & flag ) ? 1 : 0;
230static inline int canque_fifo_test_and_clear_fl(
235 return ( atomic_fetch_and( &fifo->
fifo_flags, ~flag ) & flag ) ? 1 : 0;
247static inline int canque_fifo_out_is_ready_unprotected(
251 return ( ( fifo->
head ) != NULL ) ? 1 : 0;
268static inline bool canque_filter_match(
273 return ( ( filter->
id^
id ) & filter->
id_mask ) ||
288static inline int canque_fifo_get_inslot(
298 if ( ( slot = fifo->
flist ) == NULL ) {
299 canque_fifo_set_fl( fifo, CAN_FIFOF_OVERRUN );
300 canque_fifo_set_fl( fifo, CAN_FIFOF_FULL );
307 if ( ( fifo->
flist = slot->
next ) == NULL ) {
308 canque_fifo_set_fl( fifo, CAN_FIFOF_FULL );
330static inline int canque_fifo_put_inslot(
342 if ( canque_fifo_test_and_clear_fl( fifo, CAN_FIFOF_EMPTY ) ) {
344 ret = CAN_FIFOF_EMPTY;
347 if ( canque_fifo_test_and_clear_fl( fifo, CAN_FIFOF_INACTIVE ) ) {
349 ret |= CAN_FIFOF_INACTIVE;
352 if ( canque_fifo_test_and_clear_fl( fifo, CAN_FIFOF_OVERRUN ) ) {
372static inline int canque_fifo_abort_inslot(
382 if ( canque_fifo_test_and_clear_fl( fifo, CAN_FIFOF_FULL ) ) {
383 ret = CAN_FIFOF_FULL;
405static inline int canque_fifo_test_outslot(
414 if ( ( slot = fifo->
head ) == NULL ) {
420 if ( ( fifo->
head=slot->
next ) == NULL ) {
447static inline int canque_fifo_free_outslot(
457 if ( canque_fifo_test_and_clear_fl( fifo, CAN_FIFOF_FULL ) ) {
458 ret = CAN_FIFOF_FULL;
463 if ( ( fifo->
head ) == NULL ) {
464 canque_fifo_set_fl( fifo, CAN_FIFOF_INACTIVE );
465 ret |= CAN_FIFOF_INACTIVE;
467 canque_fifo_set_fl( fifo, CAN_FIFOF_EMPTY );
468 ret |= CAN_FIFOF_EMPTY;
488static inline int canque_fifo_again_outslot(
496 if ( ( slot->
next = fifo->
head ) == NULL ) {
498 ret = CAN_FIFOF_INACTIVE;
517static inline int canque_fifo_slot_size(
int maxdlen )
613 struct canqueue_edges_list_t
idle;
659#define CANQUEUE_NOTIFY_EMPTY ( 1 )
663#define CANQUEUE_NOTIFY_SPACE ( 2 )
667#define CANQUEUE_NOTIFY_PROC ( 3 )
672#define CANQUEUE_NOTIFY_NOUSR ( 4 )
676#define CANQUEUE_NOTIFY_DEAD ( 5 )
680#define CANQUEUE_NOTIFY_DEAD_WANTED ( 6 )
684#define CANQUEUE_NOTIFY_ATTACH ( 7 )
696static inline void canque_notify_inends(
struct canque_edge_t *qedge,
int what )
715static inline void canque_notify_outends(
736static inline void canque_notify_bothends(
741 canque_notify_inends( qedge, what );
742 canque_notify_outends( qedge, what );
758static inline void canque_activate_edge(
769 if ( ( outends = qedge->
outends ) != NULL ) {
774 TAILQ_REMOVE( qedge->
peershead, qedge, activepeers );
777 TAILQ_INSERT_HEAD( &outends->
active[qedge->
edge_prio], qedge, activepeers );
781 rtems_mutex_unlock( &outends->
ends_lock );
785static inline void canque_edge_to_idle_unprotected(
791 TAILQ_REMOVE( qedge->
peershead, qedge, activepeers );
793 TAILQ_INSERT_TAIL( &outends->
idle, qedge, activepeers );
831 unsigned int flags2add
893static inline void canque_edge_incref(
struct canque_edge_t *edge )
901static inline void __canque_edge_decref_body(
struct canque_edge_t *edge )
907 if ( inends < outends ) {
910 if ( atomic_fetch_sub( &edge->
edge_used, 1 ) == 1 ) {
911 dead_fl = !canque_fifo_test_and_set_fl( &edge->
fifo, CAN_FIFOF_DEAD );
913 rtems_mutex_unlock( &outends->
ends_lock );
914 rtems_mutex_unlock( &inends->
ends_lock );
917 if ( outends != inends ) {
920 if ( atomic_fetch_sub( &edge->
edge_used, 1 ) == 1 ) {
921 dead_fl = !canque_fifo_test_and_set_fl( &edge->
fifo, CAN_FIFOF_DEAD );
923 if ( outends != inends ) {
924 rtems_mutex_unlock( &inends->
ends_lock );
926 rtems_mutex_unlock( &outends->
ends_lock );
930 canque_edge_do_dead( edge );
946static inline void canque_edge_decref(
struct canque_edge_t *edge )
950 x = atomic_load_explicit( &edge->
edge_used, memory_order_relaxed );
954 return __canque_edge_decref( edge );
956 }
while( !atomic_compare_exchange_strong(&edge->
edge_used, &x, x-1 ) );
966 edge = TAILQ_FIRST( &qends->
inlist );
969 if ( edge != NULL ) {
970 if ( canque_fifo_test_fl( &edge->
fifo, CAN_FIFOF_DEAD ) ) {
971 edge = TAILQ_NEXT( edge, inpeers );
974 canque_edge_incref( edge );
991 next = TAILQ_NEXT( edge, inpeers );
994 if ( next != NULL ) {
995 if ( canque_fifo_test_fl( &edge->
fifo,CAN_FIFOF_DEAD ) ) {
996 next = TAILQ_NEXT( next, inpeers );
999 canque_edge_incref( next );
1003 rtems_mutex_unlock( &qends->
ends_lock );
1004 canque_edge_decref( edge );
1008#define canque_for_each_inedge( qends, edge ) \
1009 for ( edge = canque_first_inedge( qends ); edge; edge = canque_next_inedge( qends, edge ) )
1017 edge = TAILQ_FIRST( &qends->
outlist );
1020 if ( edge != NULL ) {
1021 if ( canque_fifo_test_fl( &edge->
fifo, CAN_FIFOF_DEAD ) ) {
1022 edge = TAILQ_NEXT( edge, outpeers );
1025 canque_edge_incref( edge );
1029 rtems_mutex_unlock( &qends->
ends_lock );
1043 next = TAILQ_NEXT( edge, outpeers );
1046 if ( next != NULL ) {
1047 if ( canque_fifo_test_fl( &edge->
fifo,CAN_FIFOF_DEAD ) ) {
1048 next = TAILQ_NEXT( next, outpeers );
1051 canque_edge_incref( next );
1055 rtems_mutex_unlock( &qends->
ends_lock );
1056 canque_edge_decref( edge );
1060#define canque_for_each_outedge( qends, edge ) \
1061 for ( edge = canque_first_outedge( qends ); edge; edge = canque_next_outedge( qends, edge ) )
1084 rtems_interval timeout
This header file is part of CAN/CAN FD bus common support. It implements structure that represents fi...
This header file is part of CAN/CAN FD bus common support. It implements CAN frame structure and rela...
#define RTEMS_CAN_SLOTF_CMD
Macro for command associated with allocated slot.
Definition: can-queue.h:79
int canque_pending_outslot_prio(struct canque_ends_t *qends, int prio_min)
This function tests ready outslot with minimum priority.
Definition: can-queue.c:533
void canqueue_block_inlist(struct canque_ends_t *qends)
This function blocks slot allocation of all outgoing edges of specified ends.
Definition: can-queue.c:861
int canque_put_inslot(struct canque_ends_t *qends, struct canque_edge_t *qedge, struct canque_slot_t *slot)
This function schedules filled slot for processing.
Definition: can-queue.c:295
int canque_flush(struct canque_edge_t *qedge)
This function flushes all ready slots in the edge.
Definition: can-queue.c:669
int canque_fifo_init_slots(struct canque_fifo_t *fifo)
This function initializes slot chain of one CAN FIFO.
Definition: can-queue.c:100
int canqueue_disconnect_edge(struct canque_edge_t *qedge)
This function disconnects edge from communication entities.
Definition: can-queue.c:782
int canque_fifo_done_kern(struct canque_fifo_t *fifo)
This function frees slots allocated for CAN FIFO.
Definition: can-quekern.c:332
int canque_abort_inslot(struct canque_ends_t *qends, struct canque_edge_t *qedge, struct canque_slot_t *slot)
This function aborts preparation of the message in the slot.
Definition: can-queue.c:329
int canque_get_inslot(struct canque_ends_t *qends, struct canque_edge_t **qedgep, struct canque_slot_t **slotp, int cmd)
This function finds one outgoing edge and allocates slot from it.
Definition: can-queue.c:143
int canque_fifo_flush_slots(struct canque_fifo_t *fifo)
This function frees all ready slots from the FIFO.
Definition: can-queue.c:60
int canque_filter_frame2edges(struct canque_ends_t *qends, struct canque_edge_t *src_edge, struct can_frame *frame, unsigned int flags2add)
This function sends message into all edges which accept its ID.
Definition: can-queue.c:362
int canque_test_inslot(struct canque_ends_t *qends)
This function tests whether there is a free space in any outgoing edge.
Definition: can-queue.c:261
int canque_test_outslot(struct canque_ends_t *qends, struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
This function tests and retrieves ready slot for given ends.
Definition: can-queue.c:456
int canqueue_ends_init_gen(struct canque_ends_t *qends)
This function implements subsystem independent routine to initialize ends state.
Definition: can-queue.c:700
int canque_get_inslot4prio(struct canque_ends_t *qends, struct canque_edge_t **qedgep, struct canque_slot_t **slotp, const struct can_frame_header *header, int cmd, int prio)
This function finds best outgoing edge and slot for given ID.
Definition: can-queue.c:192
int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge, bool nowait, rtems_interval timeout)
This function waits for all slots processing.
Definition: can-quekern.c:247
int canqueue_ends_kill_outlist(struct canque_ends_t *qends)
This function sends request to die to all incomming edges.
Definition: can-queue.c:931
void canqueue_block_outlist(struct canque_ends_t *qends)
This function blocks slot allocation of all incoming edges of specified ends.
Definition: can-queue.c:879
int canque_free_outslot(struct canque_ends_t *qends, struct canque_edge_t *qedge, struct canque_slot_t *slot)
This function frees processed output slot.
Definition: can-queue.c:583
#define RTEMS_CAN_QUEUE_PRIO_NR
This macro contains maximum number of priorites for queues.
Definition: can-queue.h:83
int canqueue_ends_dispose_kern(struct canque_ends_t *qends, bool nonblock)
This function provides finalizing of the ends structure for clients.
Definition: can-quekern.c:451
int canqueue_ends_flush_inlist(struct canque_ends_t *qends)
Definition: can-queue.c:949
struct canque_edge_t * canque_new_edge_kern(int slotsnrm, int maxdlen)
This function allocates new edge structure.
Definition: can-quekern.c:352
int canqueue_kern_initialize(void)
This function provides initialization of kernel queue side.
Definition: can-quekern.c:486
int canqueue_ends_flush_outlist(struct canque_ends_t *qends)
This function flushes all messages in outgoing edges.
Definition: can-queue.c:967
int canque_fifo_init_kern(struct canque_fifo_t *fifo, int slotsnr, int maxdlen)
This function initializes one CAN FIFO.
Definition: can-quekern.c:298
int canqueue_connect_edge(struct canque_edge_t *qedge, struct canque_ends_t *inends, struct canque_ends_t *outends)
This function connects edge between two communication entities.
Definition: can-queue.c:724
int canque_again_outslot(struct canque_ends_t *qends, struct canque_edge_t *qedge, struct canque_slot_t *slot)
This function reschedules output slot to process it again later.
Definition: can-queue.c:628
int canqueue_ends_sync_all_kern(struct canque_ends_t *qends, struct timespec *ts)
This function waits for all ends to TX their messages.
Definition: can-quekern.c:391
int canqueue_ends_kill_inlist(struct canque_ends_t *qends, int send_rest)
This function sends request to die to all outgoing edges.
Definition: can-queue.c:899
#define CAN_FRAME_FIFO_OVERFLOW
Represents local FIFO overflow.
Definition: can-frame.h:116
This structure is used to represent CAN ID and flags in one unified structure.
Definition: can-filter.h:49
uint32_t flags_mask
This member is a bitfield that holds CAN flags forbidden in CAN frame. If the frame has some of these...
Definition: can-filter.h:75
uint32_t flags
This member is a bitfield that holds CAN flags required in CAN frame to be assigned to the FIFO queue...
Definition: can-filter.h:67
uint32_t id
This member is a bitfield that holds required CAN identifier values to assign CAN frame to the FIFO q...
Definition: can-filter.h:54
uint32_t id_mask
This member is a bitfield that holds forbidden CAN identifier values. If the frame has identifier in ...
Definition: can-filter.h:60
This structure represents one CAN frame. It consists of CAN header and data.
Definition: can-frame.h:533
struct can_frame_header header
This member stores the structure can_frame_header representing CAN header.
Definition: can-frame.h:538
uint8_t data[CAN_FRAME_MAX_DLEN]
This member stores CAN data.
Definition: can-frame.h:542
This structure represents one direction connection from messages source ( inends) to message consumer...
Definition: can-queue.h:537
struct can_filter filter
This member holds place where primitive can_filter is located.
Definition: can-queue.h:547
atomic_uint edge_used
This member holds the atomic usage counter, mainly used for safe destruction of the edge.
Definition: can-queue.h:582
TAILQ_ENTRY(canque_edge_t) activepeers
This member holds the lists of peers FIFOs connected by their output side ( outends) to the same term...
struct canqueue_edges_list_t * peershead
This member holds the pointer to the activepeers head.
Definition: can-queue.h:567
int edge_prio
This member holds the the assigned queue priority from the range 0 to RTEMS_CAN_QUEUE_PRIO_NR - 1.
Definition: can-queue.h:587
TAILQ_ENTRY(canque_edge_t) inpeers
This member holds the lists of all peers FIFOs connected by their input side ( inends) to the same te...
struct canque_fifo_t fifo
This member holds place where primitive canque_fifo_t FIFO is located.
Definition: can-queue.h:542
int edge_num
This member holds the edge sequential number intended for debugging purposes only.
Definition: can-queue.h:592
TAILQ_ENTRY(canque_edge_t) outpeers
This member holds the lists of all peers FIFOs connected by their output side ( outends) to the same ...
struct canque_ends_t * inends
This member holds the pointer to the FIFO input side terminal ( canque_ends_t).
Definition: can-queue.h:572
struct canque_ends_t * outends
This member holds the pointer to the FIFO output side terminal ( canque_ends_t).
Definition: can-queue.h:577
This structure represents place to connect edges to for CAN communication entity. The zero,...
Definition: can-queue.h:603
TAILQ_ENTRY(canque_ends_t) dead_peers
This member is used to chain ends wanting for postponed destruction.
struct canqueue_edges_list_t active[RTEMS_CAN_QUEUE_PRIO_NR]
This member holds the array of the lists of active edges directed to the ends structure.
Definition: can-queue.h:608
void(* notify)(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
pointer to notify procedure. The next state changes are notified.
Definition: can-queue.h:647
struct canqueue_edges_list_t idle
This member holds the list of the edges directed to the ends structure with empty FIFOs.
Definition: can-queue.h:613
rtems_mutex ends_lock
This member holds the lock synchronizing operations between threads accessing the ends.
Definition: can-queue.h:627
struct canqueue_edges_list_t outlist
This member holds the list of all incoming edges output sides. Each of there edges is listed on one o...
Definition: can-queue.h:622
struct canqueue_edges_list_t inlist
This member holds the list of outgoing edges input sides.
Definition: can-queue.h:617
This structure represents CAN FIFO queue. It is implemented as a single linked list of slots prepared...
Definition: can-queue.h:91
int maxdlen
This member holds maximum data length of one CAN frame.
Definition: can-queue.h:133
atomic_uint fifo_flags
This field holds global flags describing state of the FIFO. CAN_FIFOF_ERROR is set when some error co...
Definition: can-queue.h:102
struct canque_slot_t * entry
This member holds the pointer to the memory allocated for the list slots.
Definition: can-queue.h:129
struct canque_slot_t * head
This member holds the pointer to the FIFO head, oldest slot.
Definition: can-queue.h:114
unsigned long error_code
This member holds futher description of error condition.
Definition: can-queue.h:110
struct canque_slot_t * flist
This member holds the pointer to list of the free slots associated with queue.
Definition: can-queue.h:124
struct canque_slot_t ** tail
This member holds the pointer to the location, where pointer to newly inserted slot should be added.
Definition: can-queue.h:119
unsigned int out_taken
This member holds the number of elements in the fifo.
Definition: can-queue.h:106
rtems_mutex fifo_lock
This member holds the lock to ensure atomicity of slot manipulation operations.
Definition: can-queue.h:142
int slotsnr
This member holds the number of allocated slots.
Definition: can-queue.h:137
This structure represents one CAN message slot in the CAN FIFO queue.
Definition: can-queue.h:59
struct can_frame frame
This member holds can_frame structure representing one CAN frame/message.
Definition: can-queue.h:73
atomic_uint slot_flags
Space for flags and optional command describing action associated with slot data.
Definition: can-queue.h:68
struct canque_slot_t * next
Pointer to next/younger slot.
Definition: can-queue.h:63