]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/include/can_queue.h
aacfd846f2b52ebfada182ef72a91897a8810d5b
[lincan.git] / lincan / include / can_queue.h
1 #ifndef _CAN_QUEUE_H
2 #define _CAN_QUEUE_H
3
4 #include "./can.h"
5 #include "./constants.h"
6 #include "./can_sysdep.h"
7
8 /**
9  * struct canque_slot_t - one CAN message slot in the CAN FIFO queue 
10  * @next: pointer to the next/younger slot
11  * @slot_flags: space for flags and optional command describing action
12  *      associated with slot data
13  * @msg: space for one CAN message
14  *
15  * This structure is used to store CAN messages in the CAN FIFO queue.
16  */
17  struct canque_slot_t {
18         struct canque_slot_t *next;
19         unsigned long slot_flags;
20         struct canmsg_t msg;
21 };
22
23 #define CAN_SLOTF_CMD   0x00ff  /*  */
24
25 /**
26  * struct canque_fifo_t - CAN FIFO queue representation
27  * @fifo_flags: this field holds global flags describing state of the FIFO.
28  *      %CAN_FIFOF_ERROR is set when some error condition occurs.
29  *      %CAN_FIFOF_ERR2BLOCK defines, that error should lead to the FIFO block state.
30  *      %CAN_FIFOF_BLOCK state blocks insertion of the next messages. 
31  *      %CAN_FIFOF_OVERRUN attempt to acquire new slot, when FIFO is full. 
32  *      %CAN_FIFOF_FULL indicates FIFO full state. 
33  *      %CAN_FIFOF_EMPTY indicates no allocated slot in the FIFO.
34  *      %CAN_FIFOF_DEAD condition indication. Used when FIFO is beeing destroyed.
35  * @error_code: futher description of error condition
36  * @head: pointer to the FIFO head, oldest slot
37  * @tail: pointer to the location, where pointer to newly inserted slot
38  *      should be added
39  * @flist: pointer to list of the free slots associated with queue
40  * @entry: pointer to the memory allocated for the list slots.
41  * @fifo_lock: the lock to ensure atomicity of slot manipulation operations.
42  * @slotnr:  number of allocated slots
43  *
44  * This structure represents CAN FIFO queue. It is implemented as 
45  * a single linked list of slots prepared for processing. The empty slots
46  * are stored in single linked list (@flist).
47  */
48 struct canque_fifo_t {
49         unsigned long fifo_flags;
50         unsigned long error_code;
51         struct canque_slot_t *head;     /* points to the oldest entry */
52         struct canque_slot_t **tail;    /* points to NULL pointer for chaining */
53         struct canque_slot_t *flist;    /* points the first entry in the free list */
54         struct canque_slot_t *entry;    /* points to first allocated entry */
55         can_spinlock_t fifo_lock;       /* can_spin_lock_irqsave / can_spin_unlock_irqrestore */
56         int    slotsnr;
57 };
58
59 #define CAN_FIFOF_DESTROY_b     15
60 #define CAN_FIFOF_ERROR_b       14
61 #define CAN_FIFOF_ERR2BLOCK_b   13
62 #define CAN_FIFOF_BLOCK_b       12
63 #define CAN_FIFOF_OVERRUN_b     11
64 #define CAN_FIFOF_FULL_b        10
65 #define CAN_FIFOF_EMPTY_b       9
66 #define CAN_FIFOF_DEAD_b        8
67 #define CAN_FIFOF_INACTIVE_b    7
68 #define CAN_FIFOF_FREEONEMPTY_b 6
69 #define CAN_FIFOF_READY_b       5
70 #define CAN_FIFOF_NOTIFYPEND_b  4
71 #define CAN_FIFOF_RTL_MEM_b     3
72
73 #define CAN_FIFOF_DESTROY       (1<<CAN_FIFOF_DESTROY_b)
74 #define CAN_FIFOF_ERROR         (1<<CAN_FIFOF_ERROR_b)
75 #define CAN_FIFOF_ERR2BLOCK     (1<<CAN_FIFOF_ERR2BLOCK_b)
76 #define CAN_FIFOF_BLOCK         (1<<CAN_FIFOF_BLOCK_b)
77 #define CAN_FIFOF_OVERRUN       (1<<CAN_FIFOF_OVERRUN_b)
78 #define CAN_FIFOF_FULL          (1<<CAN_FIFOF_FULL_b)
79 #define CAN_FIFOF_EMPTY         (1<<CAN_FIFOF_EMPTY_b)
80 #define CAN_FIFOF_DEAD          (1<<CAN_FIFOF_DEAD_b)
81 #define CAN_FIFOF_INACTIVE      (1<<CAN_FIFOF_INACTIVE_b)
82 #define CAN_FIFOF_FREEONEMPTY   (1<<CAN_FIFOF_FREEONEMPTY_b)
83 #define CAN_FIFOF_READY         (1<<CAN_FIFOF_READY_b)
84 #define CAN_FIFOF_NOTIFYPEND    (1<<CAN_FIFOF_NOTIFYPEND_b)
85 #define CAN_FIFOF_RTL_MEM       (1<<CAN_FIFOF_RTL_MEM_b)
86
87 #define canque_fifo_test_fl(fifo,fifo_fl) \
88   test_bit(CAN_FIFOF_##fifo_fl##_b,&(fifo)->fifo_flags)
89 #define canque_fifo_set_fl(fifo,fifo_fl) \
90   set_bit(CAN_FIFOF_##fifo_fl##_b,&(fifo)->fifo_flags)
91 #define canque_fifo_clear_fl(fifo,fifo_fl) \
92   clear_bit(CAN_FIFOF_##fifo_fl##_b,&(fifo)->fifo_flags)
93 #define canque_fifo_test_and_set_fl(fifo,fifo_fl) \
94   test_and_set_bit(CAN_FIFOF_##fifo_fl##_b,&(fifo)->fifo_flags)
95 #define canque_fifo_test_and_clear_fl(fifo,fifo_fl) \
96   test_and_clear_bit(CAN_FIFOF_##fifo_fl##_b,&(fifo)->fifo_flags)
97
98
99 /**
100  * canque_fifo_get_inslot - allocate slot for the input of one CAN message 
101  * @fifo: pointer to the FIFO structure
102  * @slotp: pointer to location to store pointer to the allocated slot.
103  * @cmd: optional command associated with allocated slot.
104  *
105  * Return Value: The function returns negative value if there is no
106  *      free slot in the FIFO queue.
107  */
108 static inline
109 int canque_fifo_get_inslot(struct canque_fifo_t *fifo, struct canque_slot_t **slotp, int cmd)
110 {
111         can_spin_irqflags_t flags;
112         struct canque_slot_t *slot;
113         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
114         /* get the first free slot slot from flist */
115         if(!(slot=fifo->flist)) {
116                 canque_fifo_set_fl(fifo,OVERRUN);
117                 canque_fifo_set_fl(fifo,FULL);
118                 can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
119                 *slotp=NULL;
120                 return -1;
121         }
122         /* adjust free slot list */
123         if(!(fifo->flist=slot->next))
124                 canque_fifo_set_fl(fifo,FULL);
125         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
126         *slotp=slot;
127         slot->slot_flags=cmd&CAN_SLOTF_CMD;
128         return 1;
129 }
130
131 /**
132  * canque_fifo_put_inslot - releases slot to further processing
133  * @fifo: pointer to the FIFO structure
134  * @slot: pointer to the slot previously acquired by canque_fifo_get_inslot().
135  *
136  * Return Value: The nonzero return value indicates, that the queue was empty
137  *      before call to the function. The caller should wake-up output side of the queue.
138  */
139 static inline
140 int canque_fifo_put_inslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
141 {
142         int ret;
143         can_spin_irqflags_t flags;
144         slot->next=NULL;
145         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
146         if(*fifo->tail) can_printk(KERN_CRIT "canque_fifo_put_inslot: fifo->tail != NULL\n");
147         *fifo->tail=slot;
148         fifo->tail=&slot->next;
149         ret=0;
150         if(canque_fifo_test_and_clear_fl(fifo,EMPTY))
151           ret=CAN_FIFOF_EMPTY;  /* Fifo has been empty before put */
152         if(canque_fifo_test_and_clear_fl(fifo,INACTIVE))
153           ret=CAN_FIFOF_INACTIVE; /* Fifo has been empty before put */
154         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
155         return ret;
156 }
157
158 /**
159  * canque_fifo_abort_inslot - release and abort slot
160  * @fifo: pointer to the FIFO structure
161  * @slot: pointer to the slot previously acquired by canque_fifo_get_inslot().
162  *
163  * Return Value: The nonzero value indicates, that fifo was full
164  */
165 static inline
166 int canque_fifo_abort_inslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
167 {
168         int ret=0;
169         can_spin_irqflags_t flags;
170         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
171         slot->next=fifo->flist;
172         fifo->flist=slot;
173         if(canque_fifo_test_and_clear_fl(fifo,FULL))
174                 ret=CAN_FIFOF_FULL;
175         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
176         return ret;
177 }
178
179 /**
180  * canque_fifo_test_outslot - test and get ready slot from the FIFO
181  * @fifo: pointer to the FIFO structure
182  * @slotp: pointer to location to store pointer to the oldest slot from the FIFO.
183  *
184  * Return Value: The negative value indicates, that queue is empty.
185  *      The positive or zero value represents command stored into slot by
186  *      the call to the function canque_fifo_get_inslot().
187  *      The successfully acquired FIFO output slot has to be released by
188  *      the call canque_fifo_free_outslot() or canque_fifo_again_outslot().
189  */
190 static inline
191 int canque_fifo_test_outslot(struct canque_fifo_t *fifo, struct canque_slot_t **slotp)
192 {
193         can_spin_irqflags_t flags;
194         int cmd;
195         struct canque_slot_t *slot;
196         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
197         if(!(slot=fifo->head)){;
198                 canque_fifo_set_fl(fifo,EMPTY);
199                 can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
200                 *slotp=NULL;
201                 return -1;
202         }
203         if(!(fifo->head=slot->next))
204                 fifo->tail=&fifo->head;
205         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
206
207         *slotp=slot;
208         cmd=slot->slot_flags;
209         return cmd&CAN_SLOTF_CMD;
210 }
211
212
213 /**
214  * canque_fifo_free_outslot - free processed FIFO slot
215  * @fifo: pointer to the FIFO structure
216  * @slot: pointer to the slot previously acquired by canque_fifo_test_outslot().
217  *
218  * Return Value: The returned value informs about FIFO state change.
219  *      The mask %CAN_FIFOF_FULL indicates, that the FIFO was full before
220  *      the function call. The mask %CAN_FIFOF_EMPTY informs, that last ready slot
221  *      has been processed.
222  */
223 static inline
224 int canque_fifo_free_outslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
225 {
226         int ret=0;
227         can_spin_irqflags_t flags;
228         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
229         slot->next=fifo->flist;
230         fifo->flist=slot;
231         if(canque_fifo_test_and_clear_fl(fifo,FULL))
232                 ret=CAN_FIFOF_FULL;
233         if(!(fifo->head)){
234                 canque_fifo_set_fl(fifo,EMPTY);
235                 ret|=CAN_FIFOF_EMPTY;
236         }
237         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
238         return ret;
239 }
240
241 /**
242  * canque_fifo_again_outslot - interrupt and postpone processing of the slot
243  * @fifo: pointer to the FIFO structure
244  * @slot: pointer to the slot previously acquired by canque_fifo_test_outslot().
245  *
246  * Return Value: The function cannot fail..
247  */
248 static inline
249 int canque_fifo_again_outslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
250 {
251         can_spin_irqflags_t flags;
252         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
253         if(!(slot->next=fifo->head))
254                 fifo->tail=&slot->next;
255         fifo->head=slot;
256         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
257         return 1;
258 }
259
260 int canque_fifo_flush_slots(struct canque_fifo_t *fifo);
261
262 int canque_fifo_init_slots(struct canque_fifo_t *fifo);
263
264 #define CANQUEUE_PRIO_NR  3
265
266 /* Forward declarations for external types */
267 struct msgobj_t;
268 struct chip_t;
269
270 /**
271  * struct canque_edge_t - CAN message delivery subsystem graph edge
272  * @fifo: place where primitive @struct canque_fifo_t FIFO is located.
273  * @filtid: the possible CAN message identifiers filter.
274  * @filtmask: the filter mask, the comparison considers only
275  *      @filtid bits corresponding to set bits in the @filtmask field.
276  * @inpeers: the lists of all peers FIFOs connected by their
277  *      input side (@inends) to the same terminal (@struct canque_ends_t).
278  * @outpeers: the lists of all peers FIFOs connected by their
279  *      output side (@outends) to the same terminal (@struct canque_ends_t).
280  * @activepeers: the lists of peers FIFOs connected by their
281  *      output side (@outends) to the same terminal (@struct canque_ends_t)
282  *      with same priority and active state.
283  * @inends: the pointer to the FIFO input side terminal (@struct canque_ends_t).
284  * @outends: the pointer to the FIFO output side terminal (@struct canque_ends_t).
285  * @edge_used: the atomic usage counter, mainly used for safe destruction of the edge.
286  * @edge_prio: the assigned queue priority from the range 0 to %CANQUEUE_PRIO_NR-1
287  * @edge_num: edge sequential number intended for debugging purposes only
288  * @pending_peers: edges with pending delayed events (RTL->Linux calls)
289  * @pending_inops: bitmask of pending operations
290  * @pending_outops: bitmask of pending operations
291  *
292  * This structure represents one direction connection from messages source 
293  * (@inends) to message consumer (@outends) fifo ends hub. The edge contains
294  * &struct canque_fifo_t for message fifo implementation.
295  */
296 struct canque_edge_t {
297         struct canque_fifo_t fifo;
298         unsigned long filtid;
299         unsigned long filtmask;
300         struct list_head inpeers;
301         struct list_head outpeers;
302         struct list_head activepeers;
303         struct canque_ends_t *inends;
304         struct canque_ends_t *outends;
305         atomic_t edge_used;
306         int edge_prio;
307         int edge_num;
308     #ifdef CAN_WITH_RTL
309         struct list_head pending_peers;
310         unsigned long pending_inops;
311         unsigned long pending_outops;
312     #endif /*CAN_WITH_RTL*/
313 };
314
315 /**
316  * struct canque_ends_t - CAN message delivery subsystem graph vertex (FIFO ends)
317  * @ends_flags: this field holds flags describing state of the ENDS structure.
318  * @active: the array of the lists of active edges directed to the ends structure
319  *      with ready messages. The array is indexed by the edges priorities. 
320  * @idle: the list of the edges directed to the ends structure with empty FIFOs.
321  * @inlist: the list of outgoing edges input sides.
322  * @ends_lock: the lock synchronizing operations between threads accessing
323  *      same ends structure.
324  * @notify: pointer to notify procedure. The next state changes are notified.
325  *      %CANQUEUE_NOTIFY_EMPTY (out->in call) - all slots are processed by FIFO out side. 
326  *      %CANQUEUE_NOTIFY_SPACE (out->in call) - full state negated => there is space for new message.
327  *      %CANQUEUE_NOTIFY_PROC  (in->out call) - empty state negated => out side is requested to process slots.
328  *      %CANQUEUE_NOTIFY_NOUSR (both) - notify, that the last user has released the edge usage
329  *              called with some lock to prevent edge disappear.
330  *      %CANQUEUE_NOTIFY_DEAD  (both) - edge is in progress of deletion.
331  *      %CANQUEUE_NOTIFY_ATACH (both) - new edge has been attached to end.
332  *      %CANQUEUE_NOTIFY_FILTCH (out->in call) - edge filter rules changed
333  *      %CANQUEUE_NOTIFY_ERROR  (out->in call) - error in messages processing.
334  * @context: space to store ends user specific information
335  * @endinfo: space to store some other ends usage specific informations
336  *      mainly for waking-up by the notify calls.
337  * @dead_peers: used to chain ends wanting for postponed destruction
338  *
339  * Structure represents place to connect edges to for CAN communication entity.
340  * The zero, one or more incoming and outgoing edges can be connected to
341  * this structure.
342  */
343 struct canque_ends_t {
344         unsigned long ends_flags;
345         struct list_head active[CANQUEUE_PRIO_NR];
346         struct list_head idle;
347         struct list_head inlist;
348         struct list_head outlist;
349         can_spinlock_t ends_lock;       /* can_spin_lock_irqsave / can_spin_unlock_irqrestore */
350         void (*notify)(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what);
351         void *context;
352         union {
353                 struct {
354                         wait_queue_head_t readq;
355                         wait_queue_head_t writeq;
356                         wait_queue_head_t emptyq;
357                     #ifdef CAN_ENABLE_KERN_FASYNC
358                         struct fasync_struct *fasync;
359                     #endif /*CAN_ENABLE_KERN_FASYNC*/
360                 } fileinfo;
361             #ifdef CAN_WITH_RTL
362                 struct {
363                         rtl_spinlock_t rtl_lock;
364                         rtl_wait_t rtl_readq;
365                         rtl_wait_t rtl_writeq;
366                         rtl_wait_t rtl_emptyq;
367                         unsigned long pend_flags;
368                 } rtlinfo;
369             #endif /*CAN_WITH_RTL*/
370                 struct {
371                         struct msgobj_t *msgobj;
372                         struct chip_t *chip;
373                     #ifndef CAN_WITH_RTL
374                         wait_queue_head_t daemonq;
375                     #else /*CAN_WITH_RTL*/
376                         pthread_t worker_thread;
377                     #endif /*CAN_WITH_RTL*/
378                 } chipinfo;
379         } endinfo;
380         struct list_head dead_peers;
381 };
382
383 #define CANQUEUE_NOTIFY_EMPTY  1 /* out -> in - all slots are processed by FIFO out side */
384 #define CANQUEUE_NOTIFY_SPACE  2 /* out -> in - full state negated => there is space for new message */
385 #define CANQUEUE_NOTIFY_PROC   3 /* in -> out - empty state negated => out side is requested to process slots */
386 #define CANQUEUE_NOTIFY_NOUSR  4 /* called with some lock to prevent edge disappear */
387 #define CANQUEUE_NOTIFY_DEAD   5 /*  */
388 #define CANQUEUE_NOTIFY_DEAD_WANTED 6 /*  */
389 #define CANQUEUE_NOTIFY_ATTACH 7 /*  */
390 #define CANQUEUE_NOTIFY_FILTCH 8 /* filter changed */
391 #define CANQUEUE_NOTIFY_ERROR      0x10000 /* error notifiers */
392 #define CANQUEUE_NOTIFY_ERRTX_PREP 0x11001 /* tx preparation error */
393 #define CANQUEUE_NOTIFY_ERRTX_SEND 0x11002 /* tx send error */
394 #define CANQUEUE_NOTIFY_ERRTX_BUS  0x11003 /* tx bus error */
395
396 #define CAN_ENDSF_DEAD    (1<<0)
397 #define CAN_ENDSF_MEM_RTL (1<<1)
398
399 /**
400  * canque_notify_inends - request to send notification to the input ends
401  * @qedge: pointer to the edge structure
402  * @what: notification type
403  */
404 static inline
405 void canque_notify_inends(struct canque_edge_t *qedge, int what)
406 {
407         if(qedge->inends)
408                 if(qedge->inends->notify)
409                         qedge->inends->notify(qedge->inends,qedge,what);
410 }
411
412 /**
413  * canque_notify_outends - request to send notification to the output ends
414  * @qedge: pointer to the edge structure
415  * @what: notification type
416  */
417 static inline
418 void canque_notify_outends(struct canque_edge_t *qedge, int what)
419 {
420         if(qedge->outends)
421                 if(qedge->outends->notify)
422                         qedge->outends->notify(qedge->outends,qedge,what);
423 }
424
425 /**
426  * canque_notify_bothends - request to send notification to the both ends
427  * @qedge: pointer to the edge structure
428  * @what: notification type
429  */
430 static inline
431 void canque_notify_bothends(struct canque_edge_t *qedge, int what)
432 {
433         canque_notify_inends(qedge, what);
434         canque_notify_outends(qedge, what);
435 }
436
437 /**
438  * canque_activate_edge - mark output end of the edge as active
439  * @qedge: pointer to the edge structure
440  * @inends: input side of the edge
441  *
442  * Function call moves output side of the edge from idle onto active edges
443  * list.
444  */
445 static inline
446 void canque_activate_edge(struct canque_ends_t *inends, struct canque_edge_t *qedge)
447 {
448         can_spin_irqflags_t flags;
449         struct canque_ends_t *outends;
450         if(qedge->edge_prio>=CANQUEUE_PRIO_NR)
451                 qedge->edge_prio=CANQUEUE_PRIO_NR-1;
452         can_spin_lock_irqsave(&inends->ends_lock, flags);
453         if((outends=qedge->outends)){
454                 can_spin_lock(&outends->ends_lock);
455                 can_spin_lock(&qedge->fifo.fifo_lock);
456                 if(!canque_fifo_test_fl(&qedge->fifo,EMPTY)){
457                         list_del(&qedge->activepeers);
458                         list_add_tail(&qedge->activepeers,&outends->active[qedge->edge_prio]);
459                 }
460                 can_spin_unlock(&qedge->fifo.fifo_lock);
461                 can_spin_unlock(&outends->ends_lock);
462
463         }
464         can_spin_unlock_irqrestore(&inends->ends_lock, flags);
465 }
466
467 /**
468  * canque_filtid2internal - converts message ID and filter flags into internal format
469  * @id: CAN message 11 or 29 bit identifier
470  * @filtflags: CAN message flags
471  *
472  * This function maps message ID and %MSG_RTR, %MSG_EXT and %MSG_LOCAL into one 32 bit number
473  */
474 static inline
475 unsigned int canque_filtid2internal(unsigned long id, int filtflags)
476 {
477         filtflags &= MSG_RTR|MSG_EXT|MSG_LOCAL;
478         filtflags += filtflags&MSG_RTR;
479         return (id&MSG_ID_MASK) | (filtflags<<28);
480 }
481
482 int canque_get_inslot(struct canque_ends_t *qends,
483         struct canque_edge_t **qedgep, struct canque_slot_t **slotp, int cmd);
484         
485 int canque_get_inslot4id(struct canque_ends_t *qends,
486         struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
487         int cmd, unsigned long id, int prio);
488         
489 int canque_put_inslot(struct canque_ends_t *qends,
490         struct canque_edge_t *qedge, struct canque_slot_t *slot);
491
492 int canque_abort_inslot(struct canque_ends_t *qends,
493         struct canque_edge_t *qedge, struct canque_slot_t *slot);
494
495 int canque_filter_msg2edges(struct canque_ends_t *qends, struct canmsg_t *msg);
496
497 int canque_test_outslot(struct canque_ends_t *qends,
498         struct canque_edge_t **qedgep, struct canque_slot_t **slotp);
499
500 int canque_free_outslot(struct canque_ends_t *qends,
501         struct canque_edge_t *qedge, struct canque_slot_t *slot);
502
503 int canque_again_outslot(struct canque_ends_t *qends,
504         struct canque_edge_t *qedge, struct canque_slot_t *slot);
505
506 int canque_set_filt(struct canque_edge_t *qedge,
507         unsigned long filtid, unsigned long filtmask, int flags);
508         
509 int canque_flush(struct canque_edge_t *qedge);
510
511 int canqueue_disconnect_edge(struct canque_edge_t *qedge);
512
513 int canqueue_connect_edge(struct canque_edge_t *qedge, struct canque_ends_t *inends, struct canque_ends_t *outends);
514
515 int canqueue_ends_init_gen(struct canque_ends_t *qends);
516
517 void canqueue_block_inlist(struct canque_ends_t *qends);
518
519 void canqueue_block_outlist(struct canque_ends_t *qends);
520
521 int canqueue_ends_kill_inlist(struct canque_ends_t *qends, int send_rest);
522
523 int canqueue_ends_kill_outlist(struct canque_ends_t *qends);
524
525 /* edge reference and traversal functions */
526
527 void canque_edge_do_dead(struct canque_edge_t *edge, int dead_fl);
528
529 static inline
530 void canque_edge_incref(struct canque_edge_t *edge)
531 {
532         atomic_inc(&edge->edge_used);
533 }
534
535 static inline
536 void canque_edge_decref(struct canque_edge_t *edge)
537 {
538         can_spin_irqflags_t flags;
539         struct canque_ends_t *inends=edge->inends;
540         struct canque_ends_t *outends=edge->outends;
541         int dead_fl;
542         
543         can_spin_lock_irqsave(&inends->ends_lock, flags);
544         can_spin_lock(&outends->ends_lock);
545         if(atomic_dec_and_test(&edge->edge_used)) {
546                 dead_fl=canque_fifo_test_and_set_fl(&edge->fifo,DEAD);
547                 /*This should not be there, but it cannot be outside of the lock :-(*/
548                 canque_notify_bothends(edge,CANQUEUE_NOTIFY_NOUSR);
549                 can_spin_unlock(&outends->ends_lock);
550                 can_spin_unlock_irqrestore(&inends->ends_lock, flags);
551                 canque_edge_do_dead(edge, dead_fl);
552         } else {
553                 can_spin_unlock(&outends->ends_lock);
554                 can_spin_unlock_irqrestore(&inends->ends_lock, flags);
555         }
556 }
557
558 static inline
559 struct canque_edge_t *canque_first_inedge(struct canque_ends_t *qends)
560 {
561         can_spin_irqflags_t flags;
562         struct list_head *entry;
563         struct canque_edge_t *edge;
564         
565         can_spin_lock_irqsave(&qends->ends_lock, flags);
566         entry=qends->inlist.next;
567     skip_dead:
568         if(entry != &qends->inlist) {
569                 edge=list_entry(entry,struct canque_edge_t,inpeers);
570                 if(canque_fifo_test_fl(&edge->fifo,DEAD)) {
571                         entry=entry->next;
572                         goto skip_dead;
573                 }
574                 canque_edge_incref(edge);
575         } else {
576                 edge=NULL;
577         }
578         can_spin_unlock_irqrestore(&qends->ends_lock, flags);
579         return edge;
580 }
581
582
583 static inline
584 struct canque_edge_t *canque_next_inedge(struct canque_ends_t *qends, struct canque_edge_t *edge)
585 {
586         can_spin_irqflags_t flags;
587         struct list_head *entry;
588         struct canque_edge_t *next;
589         
590         can_spin_lock_irqsave(&qends->ends_lock, flags);
591         entry=edge->inpeers.next;
592     skip_dead:
593         if(entry != &qends->inlist) {
594                 next=list_entry(entry,struct canque_edge_t,inpeers);
595                 if(canque_fifo_test_fl(&edge->fifo,DEAD)) {
596                         entry=entry->next;
597                         goto skip_dead;
598                 }
599                 canque_edge_incref(next);
600         } else {
601                 next=NULL;
602         }
603         can_spin_unlock_irqrestore(&qends->ends_lock, flags);
604         canque_edge_decref(edge);
605         return next;
606 }
607
608 #define canque_for_each_inedge(qends, edge) \
609             for(edge=canque_first_inedge(qends);edge;edge=canque_next_inedge(qends, edge))
610
611 static inline
612 struct canque_edge_t *canque_first_outedge(struct canque_ends_t *qends)
613 {
614         can_spin_irqflags_t flags;
615         struct list_head *entry;
616         struct canque_edge_t *edge;
617         
618         can_spin_lock_irqsave(&qends->ends_lock, flags);
619         entry=qends->outlist.next;
620     skip_dead:
621         if(entry != &qends->outlist) {
622                 edge=list_entry(entry,struct canque_edge_t,outpeers);
623                 if(canque_fifo_test_fl(&edge->fifo,DEAD)) {
624                         entry=entry->next;
625                         goto skip_dead;
626                 }
627                 canque_edge_incref(edge);
628         } else {
629                 edge=NULL;
630         }
631         can_spin_unlock_irqrestore(&qends->ends_lock, flags);
632         return edge;
633 }
634
635
636 static inline
637 struct canque_edge_t *canque_next_outedge(struct canque_ends_t *qends, struct canque_edge_t *edge)
638 {
639         can_spin_irqflags_t flags;
640         struct list_head *entry;
641         struct canque_edge_t *next;
642         
643         can_spin_lock_irqsave(&qends->ends_lock, flags);
644         entry=edge->outpeers.next;
645     skip_dead:
646         if(entry != &qends->outlist) {
647                 next=list_entry(entry,struct canque_edge_t,outpeers);
648                 if(canque_fifo_test_fl(&edge->fifo,DEAD)) {
649                         entry=entry->next;
650                         goto skip_dead;
651                 }
652                 canque_edge_incref(next);
653         } else {
654                 next=NULL;
655         }
656         can_spin_unlock_irqrestore(&qends->ends_lock, flags);
657         canque_edge_decref(edge);
658         return next;
659 }
660
661 #define canque_for_each_outedge(qends, edge) \
662             for(edge=canque_first_outedge(qends);edge;edge=canque_next_outedge(qends, edge))
663
664 /* Linux kernel specific functions */
665
666 int canque_fifo_init_kern(struct canque_fifo_t *fifo, int slotsnr);
667
668 int canque_fifo_done_kern(struct canque_fifo_t *fifo);
669
670 struct canque_edge_t *canque_new_edge_kern(int slotsnr);
671
672 int canque_get_inslot4id_wait_kern(struct canque_ends_t *qends,
673         struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
674         int cmd, unsigned long id, int prio);
675
676 int canque_get_outslot_wait_kern(struct canque_ends_t *qends,
677         struct canque_edge_t **qedgep, struct canque_slot_t **slotp);
678
679 int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge);
680
681 int canqueue_ends_init_kern(struct canque_ends_t *qends);
682
683 int canqueue_ends_dispose_kern(struct canque_ends_t *qends, int sync);
684
685 void canqueue_ends_dispose_postpone(struct canque_ends_t *qends);
686
687 void canqueue_kern_initialize(void);
688
689 #ifdef CAN_WITH_RTL
690 /* RT-Linux specific functions */
691
692 void canqueue_rtl_initialize(void);
693 void canqueue_rtl_done(void);
694
695 int canqueue_rtl2lin_check_and_pend(struct canque_ends_t *qends,
696                          struct canque_edge_t *qedge, int what);
697
698 struct canque_edge_t *canque_new_edge_rtl(int slotsnr);
699
700 void canque_dispose_edge_rtl(struct canque_edge_t *qedge);
701
702 void canque_ends_free_rtl(struct canque_ends_t *qends);
703
704 int canqueue_ends_dispose_rtl(struct canque_ends_t *qends, int sync);
705
706 #else /*CAN_WITH_RTL*/
707
708 static inline int canqueue_rtl2lin_check_and_pend(struct canque_ends_t *qends,
709                         struct canque_edge_t *qedge, int what) { return 0; }
710
711 #endif /*CAN_WITH_RTL*/
712
713
714 #endif /*_CAN_QUEUE_H*/