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