]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/can_quekern.c
LinCAN driver structured comments updated.
[lincan.git] / lincan / src / can_quekern.c
1 /* can_quekern.c - CAN message queues functions for the Linux kernel
2  * Linux CAN-bus device driver.
3  * New CAN queues by Pavel Pisa - OCERA team member
4  * email:pisa@cmp.felk.cvut.cz
5  * This software is released under the GPL-License.
6  * Version lincan-0.2  9 Jul 2003
7  */
8
9 #include "../include/can.h"
10 #include "../include/can_sysdep.h"
11 #include "../include/can_queue.h"
12
13 //#define CAN_DEBUG
14
15 extern atomic_t edge_num_cnt;
16
17 #ifdef CAN_DEBUG
18         #define DEBUGQUE(fmt,args...) can_printk(KERN_ERR "can_quekern (debug): " fmt,\
19         ##args)
20
21 #else
22         #define DEBUGQUE(fmt,args...)
23 #endif
24
25 #define ERRMSGQUE(fmt,args...) can_printk(KERN_ERR "can_quekern: " fmt,\
26         ##args)
27
28
29 /* 
30  * Modifies Tx message processing 
31  *  0 .. local message processing disabled
32  *  1 .. local messages disabled by default but can be enabled by canque_set_filt
33  *  2 .. local messages enabled by default, can be disabled by canque_set_filt
34  */
35 extern int processlocal;
36
37 void canque_dead_func(unsigned long data);
38
39 /* Support for dead ends structures left after client close */
40 can_spinlock_t canque_dead_func_lock;
41 LIST_HEAD(canque_dead_ends);
42 /* retrieved by list_entry(canque_dead_ends.next,struct canque_ends_t,dead_peers) */
43 LIST_HEAD(canque_dead_edges);
44 /* retrieved by list_entry(canque_dead_edges.next,struct canque_edge_t,inpeers) */
45 DECLARE_TASKLET(canque_dead_tl, canque_dead_func, 0);
46 /* activated by tasklet_schedule(&canque_dead_tl) */
47
48
49 static inline
50 struct canque_edge_t *canque_dead_edges_cut_first(void)
51 {
52         can_spin_irqflags_t flags;
53         struct canque_edge_t *edge;
54         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
55         if(list_empty(&canque_dead_edges))
56                 edge=NULL;
57         else{
58                 edge=list_entry(canque_dead_edges.next,struct canque_edge_t,inpeers);
59                 list_del(&edge->inpeers);
60         }
61         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
62         return edge;
63 }
64
65 void canque_dead_func(unsigned long data)
66 {
67         can_spin_irqflags_t flags;
68         struct canque_edge_t *qedge;
69         struct canque_ends_t *qends;
70         struct list_head *entry;
71
72         while((qedge=canque_dead_edges_cut_first())){
73                 DEBUGQUE("edge %d disposed\n",qedge->edge_num);
74             #ifdef CAN_WITH_RTL
75                 if(canque_fifo_test_fl(&qedge->fifo,RTL_MEM)){
76                         canque_dispose_edge_rtl(qedge);
77                         continue;
78                 }
79             #endif /*CAN_WITH_RTL*/
80                 canque_fifo_done_kern(&qedge->fifo);
81                 kfree(qedge);
82         }
83         
84         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
85         entry=canque_dead_ends.next;
86         can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
87         /* lock can be released there, because only one instance of canque_dead_tl
88            can run at once and all other functions add ends only to head */
89         while(entry!=&canque_dead_ends){
90                 qends=list_entry(entry,struct canque_ends_t,dead_peers);
91                 entry=entry->next;
92                 if(!list_empty(&qends->inlist))
93                         continue;
94                 if(!list_empty(&qends->outlist))
95                         continue;
96                 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
97                 list_del(&qends->dead_peers);
98                 can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
99                 DEBUGQUE("ends structure disposed\n");
100             #ifdef CAN_WITH_RTL
101                 if(qends->ends_flags&CAN_ENDSF_MEM_RTL){
102                         canque_ends_free_rtl(qends);
103                         continue;
104                 }
105             #endif /*CAN_WITH_RTL*/
106                 kfree(qends);
107         }
108
109 }
110
111 static inline void canque_dead_tasklet_schedule(void)
112 {
113     #ifdef CAN_WITH_RTL
114         if(!rtl_rt_system_is_idle()){
115                 set_bit(CAN_RTL2LIN_PEND_DEAD_b,&canqueue_rtl2lin_pend);
116                 rtl_global_pend_irq (canqueue_rtl_irq);
117                 return;
118         }
119     #endif /*CAN_WITH_RTL*/
120
121         tasklet_schedule(&canque_dead_tl);
122 }
123
124
125 void canque_edge_do_dead(struct canque_edge_t *edge, int dead_fl)
126 {
127         can_spin_irqflags_t flags;
128         
129         if(dead_fl) return;
130         
131         canque_notify_bothends(edge,CANQUEUE_NOTIFY_NOUSR);
132     #ifdef CAN_WITH_RTL
133         /* The problem of the above call is, that in RT-Linux to Linux notify
134            case is edge scheduled for delayed notify delivery, this needs
135            to be reflected there */
136         if(atomic_read(&edge->edge_used)>0){
137                 can_spin_lock_irqsave(&edge->inends->ends_lock, flags);
138                 can_spin_lock(&edge->outends->ends_lock);
139                 if(atomic_read(&edge->edge_used)>0){
140                         /* left edge to live for a while, banshee comes again in a while */
141                         canque_fifo_clear_fl(&edge->fifo,DEAD);
142                         can_spin_unlock(&edge->outends->ends_lock);
143                         can_spin_unlock_irqrestore(&edge->inends->ends_lock, flags);
144                         can_printk(KERN_ERR "can_quertl (debug): canque_edge_do_dead postponed\n");
145                         return;
146                 }
147                 can_spin_unlock(&edge->outends->ends_lock);
148                 can_spin_unlock_irqrestore(&edge->inends->ends_lock, flags);
149         }
150     #endif /*CAN_WITH_RTL*/
151         
152         if(canqueue_disconnect_edge(edge)<0){
153                 ERRMSGQUE("canque_edge_do_dead: canqueue_disconnect_edge failed !!!\n");
154                 return;
155         }
156
157         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
158         list_add(&edge->inpeers,&canque_dead_edges);
159         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
160         canque_dead_tasklet_schedule();
161 }
162
163
164
165 /*if(qends->ends_flags & CAN_ENDSF_DEAD){
166         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
167         list_del(&qends->dead_peers);
168         list_add(&qends->dead_peers,&canque_dead_ends);
169         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
170         tasklet_schedule(&canque_dead_tl);
171 }*/
172
173
174 /**
175  * canqueue_notify_kern - notification callback handler for Linux userspace clients
176  * @qends: pointer to the callback side ends structure
177  * @qedge: edge which invoked notification 
178  * @what: notification type
179  *
180  * The notification event is handled directly by call of this function except case,
181  * when called from RT-Linux context in mixed mode Linux/RT-Linux compilation.
182  * It is not possible to directly call Linux kernel synchronization primitives
183  * in such case. The notification request is postponed and signaled by @pending_inops flags
184  * by call canqueue_rtl2lin_check_and_pend() function. 
185  * The edge reference count is increased until until all pending notifications are processed.
186  */
187 void canqueue_notify_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
188 {
189         DEBUGQUE("canqueue_notify_kern for edge %d, use %d and event %d\n",
190                         qedge->edge_num,(int)atomic_read(&qedge->edge_used),what);
191
192         /* delay event delivery for RT-Linux -> kernel notifications */
193         if(canqueue_rtl2lin_check_and_pend(qends,qedge,what)){
194                 DEBUGQUE("canqueue_notify_kern postponed\n");
195                 return;
196         }
197         
198         switch(what){
199                 case CANQUEUE_NOTIFY_EMPTY:
200                         wake_up(&qends->endinfo.fileinfo.emptyq);
201                         if(canque_fifo_test_and_clear_fl(&qedge->fifo, FREEONEMPTY))
202                                 canque_edge_decref(qedge);
203                         break;
204                 case CANQUEUE_NOTIFY_SPACE:
205                         wake_up(&qends->endinfo.fileinfo.writeq);
206                     #ifdef CAN_ENABLE_KERN_FASYNC
207                         /* Asynchronous I/O processing */
208                         kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_OUT); 
209                     #endif /*CAN_ENABLE_KERN_FASYNC*/
210                         break;
211                 case CANQUEUE_NOTIFY_PROC:
212                         wake_up(&qends->endinfo.fileinfo.readq);
213                     #ifdef CAN_ENABLE_KERN_FASYNC
214                         /* Asynchronous I/O processing */
215                         kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_IN); 
216                     #endif /*CAN_ENABLE_KERN_FASYNC*/
217                         break;
218                 case CANQUEUE_NOTIFY_NOUSR:
219                         wake_up(&qends->endinfo.fileinfo.readq);
220                         wake_up(&qends->endinfo.fileinfo.writeq);
221                         wake_up(&qends->endinfo.fileinfo.emptyq);
222                         break;
223                 case CANQUEUE_NOTIFY_DEAD_WANTED:
224                 case CANQUEUE_NOTIFY_DEAD:
225                         if(canque_fifo_test_and_clear_fl(&qedge->fifo, READY))
226                                 canque_edge_decref(qedge);
227                         break;
228                 case CANQUEUE_NOTIFY_ATTACH:
229                         break;
230         }
231 }
232
233 /**
234  * canqueue_ends_init_kern - Linux userspace clients specific ends initialization
235  * @qends: pointer to the callback side ends structure
236  */
237 int canqueue_ends_init_kern(struct canque_ends_t *qends)
238 {
239         canqueue_ends_init_gen(qends);
240         qends->context=NULL;
241         init_waitqueue_head(&qends->endinfo.fileinfo.readq);
242         init_waitqueue_head(&qends->endinfo.fileinfo.writeq);
243         init_waitqueue_head(&qends->endinfo.fileinfo.emptyq);
244     #ifdef CAN_ENABLE_KERN_FASYNC
245         qends->endinfo.fileinfo.fasync=NULL;
246     #endif /*CAN_ENABLE_KERN_FASYNC*/
247         
248         qends->notify=canqueue_notify_kern;
249         DEBUGQUE("canqueue_ends_init_kern\n");
250         return 0;
251 }
252
253
254 /**
255  * canque_get_inslot4id_wait_kern - find or wait for best outgoing edge and slot for given ID
256  * @qends: ends structure belonging to calling communication object
257  * @qedgep: place to store pointer to found edge
258  * @slotp: place to store pointer to  allocated slot
259  * @cmd: command type for slot
260  * @id: communication ID of message to send into edge
261  * @prio: optional priority of message
262  *
263  * Same as canque_get_inslot4id(), except, that it waits for free slot
264  * in case, that queue is full. Function is specific for Linux userspace clients.
265  * Return Value: If there is no usable edge negative value is returned.
266  */
267 int canque_get_inslot4id_wait_kern(struct canque_ends_t *qends,
268         struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
269         int cmd, unsigned long id, int prio)
270 {
271         int ret=-1;
272         DEBUGQUE("canque_get_inslot4id_wait_kern for cmd %d, id %ld, prio %d\n",cmd,id,prio);
273         wait_event_interruptible((qends->endinfo.fileinfo.writeq), 
274                 (ret=canque_get_inslot4id(qends,qedgep,slotp,cmd,id,prio))!=-1);
275         return ret;
276 }
277
278 /**
279  * canque_get_outslot_wait_kern - receive or wait for ready slot for given ends
280  * @qends: ends structure belonging to calling communication object
281  * @qedgep: place to store pointer to found edge
282  * @slotp: place to store pointer to received slot
283  *
284  * The same as canque_test_outslot(), except it waits in the case, that there is
285  * no ready slot for given ends. Function is specific for Linux userspace clients.
286  * Return Value: Negative value informs, that there is no ready output
287  *      slot for given ends. Positive value is equal to the command
288  *      slot has been allocated by the input side.
289  */
290 int canque_get_outslot_wait_kern(struct canque_ends_t *qends,
291         struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
292 {
293         int ret=-1;
294         DEBUGQUE("canque_get_outslot_wait_kern\n");
295         wait_event_interruptible((qends->endinfo.fileinfo.readq), 
296                 (ret=canque_test_outslot(qends,qedgep,slotp))!=-1);
297         return ret;
298 }
299
300 /**
301  * canque_sync_wait_kern - wait for all slots processing
302  * @qends: ends structure belonging to calling communication object
303  * @qedge: pointer to edge
304  *
305  * Functions waits for ends transition into empty state.
306  * Return Value: Positive value indicates, that edge empty state has been reached.
307  *      Negative or zero value informs about interrupted wait or other problem.
308  */
309 int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
310 {
311         int ret=-1;
312         DEBUGQUE("canque_sync_wait_kern\n");
313         wait_event_interruptible((qends->endinfo.fileinfo.emptyq), 
314                 (ret=canque_fifo_test_fl(&qedge->fifo,EMPTY)?1:0));
315         return ret;
316 }
317
318
319 /**
320  * canque_fifo_init_kern - initialize one CAN FIFO
321  * @fifo: pointer to the FIFO structure
322  * @slotsnr: number of requested slots
323  *
324  * Return Value: The negative value indicates, that there is no memory
325  *      to allocate space for the requested number of the slots.
326  */
327 int canque_fifo_init_kern(struct canque_fifo_t *fifo, int slotsnr)
328 {
329         int size;
330         if(!slotsnr) slotsnr=MAX_BUF_LENGTH;
331         size=sizeof(struct canque_slot_t)*slotsnr;
332         fifo->entry=kmalloc(size,GFP_KERNEL);
333         if(!fifo->entry) return -1;
334         fifo->slotsnr=slotsnr;
335         return canque_fifo_init_slots(fifo);
336 }
337
338 /**
339  * canque_fifo_done_kern - frees slots allocated for CAN FIFO
340  * @fifo: pointer to the FIFO structure
341  */
342 int canque_fifo_done_kern(struct canque_fifo_t *fifo)
343 {
344         if(fifo->entry)
345                 kfree(fifo->entry);
346         fifo->entry=NULL;
347         return 1;
348 }
349
350
351 /**
352  * canque_new_edge_kern - allocate new edge structure in the Linux kernel context
353  * @slotsnr: required number of slots in the newly allocated edge structure
354  *
355  * Return Value: Returns pointer to allocated slot structure or %NULL if
356  *      there is not enough memory to process operation.
357  */
358 struct canque_edge_t *canque_new_edge_kern(int slotsnr)
359 {
360         struct canque_edge_t *qedge;
361         qedge = (struct canque_edge_t *)kmalloc(sizeof(struct canque_edge_t), GFP_KERNEL);
362         if(qedge == NULL) return NULL;
363
364         memset(qedge,0,sizeof(struct canque_edge_t));
365         can_spin_lock_init(&qedge->fifo.fifo_lock);
366         if(canque_fifo_init_kern(&qedge->fifo, slotsnr)<0){
367                 kfree(qedge);
368                 DEBUGQUE("canque_new_edge_kern failed\n");
369                 return NULL;
370         }
371         atomic_set(&qedge->edge_used,1);
372         qedge->filtid = 0;
373         qedge->filtmask = canque_filtid2internal(0l, (processlocal<2)? MSG_LOCAL:0);
374         qedge->edge_prio = 0;
375     #ifdef CAN_DEBUG
376         /* not exactly clean, but enough for debugging */
377         atomic_inc(&edge_num_cnt);
378         qedge->edge_num=atomic_read(&edge_num_cnt);
379     #endif /* CAN_DEBUG */
380         DEBUGQUE("canque_new_edge_kern %d\n",qedge->edge_num);
381         return qedge;
382 }
383
384 #ifdef USE_SYNC_DISCONNECT_EDGE_KERN
385
386 /*not included in doc
387  * canqueue_disconnect_edge_kern - disconnect edge from communicating entities with wait
388  * @qends: ends structure belonging to calling communication object
389  * @qedge: pointer to edge
390  *
391  * Same as canqueue_disconnect_edge(), but tries to wait for state with zero
392  * use counter.
393  * Return Value: Negative value means, that edge is used and cannot
394  *      be disconnected yet. Operation has to be delayed.
395  */
396 int canqueue_disconnect_edge_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
397 {
398         canque_fifo_set_fl(&qedge->fifo,BLOCK);
399         DEBUGQUE("canqueue_disconnect_edge_kern %d called\n",qedge->edge_num);
400         if(!canque_fifo_test_and_set_fl(&qedge->fifo,DEAD)){
401                 canque_notify_bothends(qedge, CANQUEUE_NOTIFY_DEAD);
402                 
403                 if(atomic_read(&qedge->edge_used)>0)
404                         atomic_dec(&qedge->edge_used);
405
406                 DEBUGQUE("canqueue_disconnect_edge_kern %d waiting\n",qedge->edge_num);
407                 wait_event((qends->endinfo.fileinfo.emptyq), 
408                         (canqueue_disconnect_edge(qedge)>=0));
409
410                 /*set_current_state(TASK_UNINTERRUPTIBLE);*/
411                 /*schedule_timeout(HZ);*/
412                 return 0;
413         } else {
414                 DEBUGQUE("canqueue_disconnect_edge_kern cannot set DEAD\n");
415                 return -1;
416         }
417 }
418
419
420 int canqueue_disconnect_list_kern(struct canque_ends_t *qends, struct list_head *list)
421 {
422         struct canque_edge_t *edge;
423         can_spin_irqflags_t flags;
424         for(;;){
425                 can_spin_lock_irqsave(&qends->ends_lock,flags);
426                 if(list_empty(list)){
427                         can_spin_unlock_irqrestore(&qends->ends_lock,flags);
428                         return 0;
429                 }
430                 if(list == &qends->inlist)
431                         edge=list_entry(list->next,struct canque_edge_t,inpeers);
432                 else
433                         edge=list_entry(list->next,struct canque_edge_t,outpeers);
434                 atomic_inc(&edge->edge_used);
435                 can_spin_unlock_irqrestore(&qends->ends_lock,flags);
436                 if(canqueue_disconnect_edge_kern(qends, edge)>=0) {
437                         /* Free edge memory */
438                         canque_fifo_done_kern(&edge->fifo);
439                         kfree(edge);
440                 }else{
441                         canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
442                         canque_edge_decref(edge);
443                         DEBUGQUE("canqueue_disconnect_list_kern in troubles\n");
444                         DEBUGQUE("the edge %d has usage count %d and flags %ld\n",edge->edge_num,atomic_read(&edge->edge_used),edge->fifo.fifo_flags);
445                         return -1;
446                 }
447         }
448 }
449
450 #endif /*USE_SYNC_DISCONNECT_EDGE_KERN*/
451
452
453 int canqueue_ends_sync_all_kern(struct canque_ends_t *qends)
454 {
455         struct canque_edge_t *qedge;
456         
457         canque_for_each_inedge(qends, qedge){
458                 DEBUGQUE("canque_sync_wait_kern called for edge %d\n",qedge->edge_num);
459                 canque_sync_wait_kern(qends, qedge);
460         }
461         return 0;
462 }
463
464
465 void canqueue_ends_dispose_postpone(struct canque_ends_t *qends)
466 {
467         can_spin_irqflags_t flags;
468
469         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
470         qends->ends_flags |= CAN_ENDSF_DEAD;
471         list_add(&qends->dead_peers,&canque_dead_ends);
472         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
473         canque_dead_tasklet_schedule();
474 }
475
476
477 /**
478  * canqueue_ends_dispose_kern - finalizing of the ends structure for Linux kernel clients
479  * @qends: pointer to ends structure
480  * @sync: flag indicating, that user wants to wait for processing of all remaining
481  *      messages
482  *
483  * Return Value: Function should be designed such way to not fail.
484  */
485 int canqueue_ends_dispose_kern(struct canque_ends_t *qends, int sync)
486 {
487         int delayed;
488
489         DEBUGQUE("canqueue_ends_dispose_kern\n");
490         canqueue_block_inlist(qends);
491         canqueue_block_outlist(qends);
492
493         /*Wait for sending of all pending messages in the output FIFOs*/
494         if(sync)
495                 canqueue_ends_sync_all_kern(qends);
496         
497         /* Finish or kill all outgoing edges listed in inends */
498         delayed=canqueue_ends_kill_inlist(qends, 1);
499         /* Kill all incoming edges listed in outends */
500         delayed|=canqueue_ends_kill_outlist(qends);
501
502         wake_up(&qends->endinfo.fileinfo.readq);
503         wake_up(&qends->endinfo.fileinfo.writeq);
504         wake_up(&qends->endinfo.fileinfo.emptyq);
505
506         if(delayed){
507                 canqueue_ends_dispose_postpone(qends);
508
509                 DEBUGQUE("canqueue_ends_dispose_kern delayed\n");
510                 return 1;
511         }
512
513         kfree(qends);
514         DEBUGQUE("canqueue_ends_dispose_kern finished\n");
515         return 0;
516 }
517
518 void canqueue_kern_initialize()
519 {
520         can_spin_lock_init(&canque_dead_func_lock);
521 }