]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/can_quekern.c
5089e5cbc2c8aec6cbc9ad95d51385c4afd65ff3
[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 void canqueue_notify_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
181 {
182         DEBUGQUE("canqueue_notify_kern for edge %d, use %d and event %d\n",
183                         qedge->edge_num,(int)atomic_read(&qedge->edge_used),what);
184
185         /* delay event delivery for RT-Linux -> kernel notifications */
186         if(canqueue_rtl2lin_check_and_pend(qends,qedge,what)){
187                 DEBUGQUE("canqueue_notify_kern postponed\n");
188                 return;
189         }
190         
191         switch(what){
192                 case CANQUEUE_NOTIFY_EMPTY:
193                         wake_up(&qends->endinfo.fileinfo.emptyq);
194                         if(canque_fifo_test_and_clear_fl(&qedge->fifo, FREEONEMPTY))
195                                 canque_edge_decref(qedge);
196                         break;
197                 case CANQUEUE_NOTIFY_SPACE:
198                         wake_up(&qends->endinfo.fileinfo.writeq);
199                     #ifdef CAN_ENABLE_KERN_FASYNC
200                         /* Asynchronous I/O processing */
201                         kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_OUT); 
202                     #endif /*CAN_ENABLE_KERN_FASYNC*/
203                         break;
204                 case CANQUEUE_NOTIFY_PROC:
205                         wake_up(&qends->endinfo.fileinfo.readq);
206                     #ifdef CAN_ENABLE_KERN_FASYNC
207                         /* Asynchronous I/O processing */
208                         kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_IN); 
209                     #endif /*CAN_ENABLE_KERN_FASYNC*/
210                         break;
211                 case CANQUEUE_NOTIFY_NOUSR:
212                         wake_up(&qends->endinfo.fileinfo.readq);
213                         wake_up(&qends->endinfo.fileinfo.writeq);
214                         wake_up(&qends->endinfo.fileinfo.emptyq);
215                         break;
216                 case CANQUEUE_NOTIFY_DEAD_WANTED:
217                 case CANQUEUE_NOTIFY_DEAD:
218                         if(canque_fifo_test_and_clear_fl(&qedge->fifo, READY))
219                                 canque_edge_decref(qedge);
220                         break;
221                 case CANQUEUE_NOTIFY_ATTACH:
222                         break;
223         }
224 }
225
226 /**
227  * canqueue_ends_init_kern - Linux userspace clients specific ends initialization
228  * @qends: pointer to the callback side ends structure
229  */
230 int canqueue_ends_init_kern(struct canque_ends_t *qends)
231 {
232         canqueue_ends_init_gen(qends);
233         qends->context=NULL;
234         init_waitqueue_head(&qends->endinfo.fileinfo.readq);
235         init_waitqueue_head(&qends->endinfo.fileinfo.writeq);
236         init_waitqueue_head(&qends->endinfo.fileinfo.emptyq);
237     #ifdef CAN_ENABLE_KERN_FASYNC
238         qends->endinfo.fileinfo.fasync=NULL;
239     #endif /*CAN_ENABLE_KERN_FASYNC*/
240         
241         qends->notify=canqueue_notify_kern;
242         DEBUGQUE("canqueue_ends_init_kern\n");
243         return 0;
244 }
245
246
247 /**
248  * canque_get_inslot4id_wait_kern - find or wait for best outgoing edge and slot for given ID
249  * @qends: ends structure belonging to calling communication object
250  * @qedgep: place to store pointer to found edge
251  * @slotp: place to store pointer to  allocated slot
252  * @cmd: command type for slot
253  * @id: communication ID of message to send into edge
254  * @prio: optional priority of message
255  *
256  * Same as canque_get_inslot4id(), except, that it waits for free slot
257  * in case, that queue is full. Function is specific for Linux userspace clients.
258  * Return Value: If there is no usable edge negative value is returned.
259  */
260 int canque_get_inslot4id_wait_kern(struct canque_ends_t *qends,
261         struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
262         int cmd, unsigned long id, int prio)
263 {
264         int ret=-1;
265         DEBUGQUE("canque_get_inslot4id_wait_kern for cmd %d, id %ld, prio %d\n",cmd,id,prio);
266         wait_event_interruptible((qends->endinfo.fileinfo.writeq), 
267                 (ret=canque_get_inslot4id(qends,qedgep,slotp,cmd,id,prio))!=-1);
268         return ret;
269 }
270
271 /**
272  * canque_get_outslot_wait_kern - receive or wait for ready slot for given ends
273  * @qends: ends structure belonging to calling communication object
274  * @qedgep: place to store pointer to found edge
275  * @slotp: place to store pointer to received slot
276  *
277  * The same as canque_test_outslot(), except it waits in the case, that there is
278  * no ready slot for given ends. Function is specific for Linux userspace clients.
279  * Return Value: Negative value informs, that there is no ready output
280  *      slot for given ends. Positive value is equal to the command
281  *      slot has been allocated by the input side.
282  */
283 int canque_get_outslot_wait_kern(struct canque_ends_t *qends,
284         struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
285 {
286         int ret=-1;
287         DEBUGQUE("canque_get_outslot_wait_kern\n");
288         wait_event_interruptible((qends->endinfo.fileinfo.readq), 
289                 (ret=canque_test_outslot(qends,qedgep,slotp))!=-1);
290         return ret;
291 }
292
293 /**
294  * canque_sync_wait_kern - wait for all slots processing
295  * @qends: ends structure belonging to calling communication object
296  * @qedge: pointer to edge
297  *
298  * Functions waits for ends transition into empty state.
299  * Return Value: Positive value indicates, that edge empty state has been reached.
300  *      Negative or zero value informs about interrupted wait or other problem.
301  */
302 int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
303 {
304         int ret=-1;
305         DEBUGQUE("canque_sync_wait_kern\n");
306         wait_event_interruptible((qends->endinfo.fileinfo.emptyq), 
307                 (ret=canque_fifo_test_fl(&qedge->fifo,EMPTY)?1:0));
308         return ret;
309 }
310
311
312 /**
313  * canque_fifo_init_kern - initialize one CAN FIFO
314  * @fifo: pointer to the FIFO structure
315  * @slotsnr: number of requested slots
316  *
317  * Return Value: The negative value indicates, that there is no memory
318  *      to allocate space for the requested number of the slots.
319  */
320 int canque_fifo_init_kern(struct canque_fifo_t *fifo, int slotsnr)
321 {
322         int size;
323         if(!slotsnr) slotsnr=MAX_BUF_LENGTH;
324         size=sizeof(struct canque_slot_t)*slotsnr;
325         fifo->entry=kmalloc(size,GFP_KERNEL);
326         if(!fifo->entry) return -1;
327         fifo->slotsnr=slotsnr;
328         return canque_fifo_init_slots(fifo);
329 }
330
331 /**
332  * canque_fifo_done_kern - frees slots allocated for CAN FIFO
333  * @fifo: pointer to the FIFO structure
334  */
335 int canque_fifo_done_kern(struct canque_fifo_t *fifo)
336 {
337         if(fifo->entry)
338                 kfree(fifo->entry);
339         fifo->entry=NULL;
340         return 1;
341 }
342
343
344 /**
345  * canque_new_edge_kern - allocate new edge structure in the Linux kernel context
346  * @slotsnr: required number of slots in the newly allocated edge structure
347  *
348  * Return Value: Returns pointer to allocated slot structure or %NULL if
349  *      there is not enough memory to process operation.
350  */
351 struct canque_edge_t *canque_new_edge_kern(int slotsnr)
352 {
353         struct canque_edge_t *qedge;
354         qedge = (struct canque_edge_t *)kmalloc(sizeof(struct canque_edge_t), GFP_KERNEL);
355         if(qedge == NULL) return NULL;
356
357         memset(qedge,0,sizeof(struct canque_edge_t));
358         can_spin_lock_init(&qedge->fifo.fifo_lock);
359         if(canque_fifo_init_kern(&qedge->fifo, slotsnr)<0){
360                 kfree(qedge);
361                 DEBUGQUE("canque_new_edge_kern failed\n");
362                 return NULL;
363         }
364         atomic_set(&qedge->edge_used,1);
365         qedge->filtid = 0;
366         qedge->filtmask = canque_filtid2internal(0l, (processlocal<2)? MSG_LOCAL:0);
367         qedge->edge_prio = 0;
368     #ifdef CAN_DEBUG
369         /* not exactly clean, but enough for debugging */
370         atomic_inc(&edge_num_cnt);
371         qedge->edge_num=atomic_read(&edge_num_cnt);
372     #endif /* CAN_DEBUG */
373         DEBUGQUE("canque_new_edge_kern %d\n",qedge->edge_num);
374         return qedge;
375 }
376
377 #ifdef USE_SYNC_DISCONNECT_EDGE_KERN
378
379 /**
380  * canqueue_disconnect_edge_kern - disconnect edge from communicating entities with wait
381  * @qends: ends structure belonging to calling communication object
382  * @qedge: pointer to edge
383  *
384  * Same as canqueue_disconnect_edge(), but tries to wait for state with zero
385  * use counter.
386  * Return Value: Negative value means, that edge is used and cannot
387  *      be disconnected yet. Operation has to be delayed.
388  */
389 int canqueue_disconnect_edge_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
390 {
391         canque_fifo_set_fl(&qedge->fifo,BLOCK);
392         DEBUGQUE("canqueue_disconnect_edge_kern %d called\n",qedge->edge_num);
393         if(!canque_fifo_test_and_set_fl(&qedge->fifo,DEAD)){
394                 canque_notify_bothends(qedge, CANQUEUE_NOTIFY_DEAD);
395                 
396                 if(atomic_read(&qedge->edge_used)>0)
397                         atomic_dec(&qedge->edge_used);
398
399                 DEBUGQUE("canqueue_disconnect_edge_kern %d waiting\n",qedge->edge_num);
400                 wait_event((qends->endinfo.fileinfo.emptyq), 
401                         (canqueue_disconnect_edge(qedge)>=0));
402
403                 /*set_current_state(TASK_UNINTERRUPTIBLE);*/
404                 /*schedule_timeout(HZ);*/
405                 return 0;
406         } else {
407                 DEBUGQUE("canqueue_disconnect_edge_kern cannot set DEAD\n");
408                 return -1;
409         }
410 }
411
412
413 int canqueue_disconnect_list_kern(struct canque_ends_t *qends, struct list_head *list)
414 {
415         struct canque_edge_t *edge;
416         can_spin_irqflags_t flags;
417         for(;;){
418                 can_spin_lock_irqsave(&qends->ends_lock,flags);
419                 if(list_empty(list)){
420                         can_spin_unlock_irqrestore(&qends->ends_lock,flags);
421                         return 0;
422                 }
423                 if(list == &qends->inlist)
424                         edge=list_entry(list->next,struct canque_edge_t,inpeers);
425                 else
426                         edge=list_entry(list->next,struct canque_edge_t,outpeers);
427                 atomic_inc(&edge->edge_used);
428                 can_spin_unlock_irqrestore(&qends->ends_lock,flags);
429                 if(canqueue_disconnect_edge_kern(qends, edge)>=0) {
430                         /* Free edge memory */
431                         canque_fifo_done_kern(&edge->fifo);
432                         kfree(edge);
433                 }else{
434                         canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
435                         canque_edge_decref(edge);
436                         DEBUGQUE("canqueue_disconnect_list_kern in troubles\n");
437                         DEBUGQUE("the edge %d has usage count %d and flags %ld\n",edge->edge_num,atomic_read(&edge->edge_used),edge->fifo.fifo_flags);
438                         return -1;
439                 }
440         }
441 }
442
443 #endif /*USE_SYNC_DISCONNECT_EDGE_KERN*/
444
445
446 int canqueue_ends_sync_all_kern(struct canque_ends_t *qends)
447 {
448         struct canque_edge_t *qedge;
449         
450         canque_for_each_inedge(qends, qedge){
451                 DEBUGQUE("canque_sync_wait_kern called for edge %d\n",qedge->edge_num);
452                 canque_sync_wait_kern(qends, qedge);
453         }
454         return 0;
455 }
456
457
458 void canqueue_ends_dispose_postpone(struct canque_ends_t *qends)
459 {
460         can_spin_irqflags_t flags;
461
462         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
463         qends->ends_flags |= CAN_ENDSF_DEAD;
464         list_add(&qends->dead_peers,&canque_dead_ends);
465         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
466         canque_dead_tasklet_schedule();
467 }
468
469
470 /**
471  * canqueue_ends_dispose_kern - finalizing of the ends structure for Linux kernel clients
472  * @qends: pointer to ends structure
473  * @sync: flag indicating, that user wants to wait for processing of all remaining
474  *      messages
475  *
476  * Return Value: Function should be designed such way to not fail.
477  */
478 int canqueue_ends_dispose_kern(struct canque_ends_t *qends, int sync)
479 {
480         int delayed;
481
482         DEBUGQUE("canqueue_ends_dispose_kern\n");
483         canqueue_block_inlist(qends);
484         canqueue_block_outlist(qends);
485
486         /*Wait for sending of all pending messages in the output FIFOs*/
487         if(sync)
488                 canqueue_ends_sync_all_kern(qends);
489         
490         /* Finish or kill all outgoing edges listed in inends */
491         delayed=canqueue_ends_kill_inlist(qends, 1);
492         /* Kill all incoming edges listed in outends */
493         delayed|=canqueue_ends_kill_outlist(qends);
494
495         wake_up(&qends->endinfo.fileinfo.readq);
496         wake_up(&qends->endinfo.fileinfo.writeq);
497         wake_up(&qends->endinfo.fileinfo.emptyq);
498
499         if(delayed){
500                 canqueue_ends_dispose_postpone(qends);
501
502                 DEBUGQUE("canqueue_ends_dispose_kern delayed\n");
503                 return 1;
504         }
505
506         kfree(qends);
507         DEBUGQUE("canqueue_ends_dispose_kern finished\n");
508         return 0;
509 }
510
511 void canqueue_kern_initialize()
512 {
513         can_spin_lock_init(&canque_dead_func_lock);
514 }