]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/proc.c
removed debugging stuff (caller id).
[socketcan-devel.git] / kernel / 2.6 / net / can / proc.c
1 /*
2  * proc.c - procfs support for Protocol family CAN core module
3  *
4  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, the following disclaimer and
12  *    the referenced file 'COPYING'.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of Volkswagen nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * Alternatively, provided that this notice is retained in full, this
21  * software may be distributed under the terms of the GNU General
22  * Public License ("GPL") version 2 as distributed in the 'COPYING'
23  * file from the main directory of the linux kernel source.
24  *
25  * The provided data structures and external interfaces from this code
26  * are not restricted to be used by modules with a GPL compatible license.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39  * DAMAGE.
40  *
41  * Send feedback to <socketcan-users@lists.berlios.de>
42  *
43  */
44
45 #include <linux/module.h>
46 #include <linux/proc_fs.h>
47 #include <linux/list.h>
48 #include <linux/rcupdate.h>
49
50 #include <linux/can/core.h>
51 #include "af_can.h"
52
53 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
54 RCSID("$Id$");
55
56 /*
57  * proc filenames for the PF_CAN core
58  */
59
60 #define CAN_PROC_VERSION     "version"
61 #define CAN_PROC_STATS       "stats"
62 #define CAN_PROC_RESET_STATS "reset_stats"
63 #define CAN_PROC_RCVLIST_ALL "rcvlist_all"
64 #define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
65 #define CAN_PROC_RCVLIST_INV "rcvlist_inv"
66 #define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
67 #define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
68 #define CAN_PROC_RCVLIST_ERR "rcvlist_err"
69
70 static struct proc_dir_entry *can_dir         = NULL;
71 static struct proc_dir_entry *pde_version     = NULL;
72 static struct proc_dir_entry *pde_stats       = NULL;
73 static struct proc_dir_entry *pde_reset_stats = NULL;
74 static struct proc_dir_entry *pde_rcvlist_all = NULL;
75 static struct proc_dir_entry *pde_rcvlist_fil = NULL;
76 static struct proc_dir_entry *pde_rcvlist_inv = NULL;
77 static struct proc_dir_entry *pde_rcvlist_sff = NULL;
78 static struct proc_dir_entry *pde_rcvlist_eff = NULL;
79 static struct proc_dir_entry *pde_rcvlist_err = NULL;
80
81 /* 
82  * af_can statistics stuff
83  */
84
85 static void can_init_stats(void)
86 {
87         memset(&stats, 0, sizeof(stats));
88         stats.jiffies_init  = jiffies;
89         pstats.stats_reset++;
90 }
91
92 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
93                                unsigned long count)
94 {
95         unsigned long ret = 0;
96
97         if (oldjif == newjif)
98                 return 0;
99
100         /* see can_rcv() - this should NEVER happen! */
101         if (count > (ULONG_MAX / HZ)) {
102                 printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
103                        count);
104                 return 99999999;
105         }
106
107         ret = (count * HZ) / (newjif - oldjif);
108
109         return ret;
110 }
111
112 void can_stat_update(unsigned long data)
113 {
114         unsigned long j = jiffies; /* snapshot */
115
116         if (j < stats.jiffies_init) /* jiffies overflow */
117                 can_init_stats();
118
119         /* stats.rx_frames is the definitively max. statistic value */
120
121         /* prevent overflow in calc_rate() */
122         if (stats.rx_frames > (ULONG_MAX / HZ))
123                 can_init_stats();
124
125         /* matches overflow - very improbable */
126         if (stats.matches > (ULONG_MAX / 100))
127                 can_init_stats();
128
129         /* calc total values */
130         if (stats.rx_frames)
131                 stats.total_rx_match_ratio = (stats.matches * 100) / 
132                                                 stats.rx_frames;
133
134         stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
135                                         stats.tx_frames);
136         stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
137                                         stats.rx_frames);
138
139         /* calc current values */
140         if (stats.rx_frames_delta)
141                 stats.current_rx_match_ratio =
142                         (stats.matches_delta * 100) / stats.rx_frames_delta;
143
144         stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
145         stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
146
147         /* check / update maximum values */
148         if (stats.max_tx_rate < stats.current_tx_rate)
149                 stats.max_tx_rate = stats.current_tx_rate;
150
151         if (stats.max_rx_rate < stats.current_rx_rate)
152                 stats.max_rx_rate = stats.current_rx_rate;
153
154         if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
155                 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
156
157         /* clear values for 'current rate' calculation */
158         stats.tx_frames_delta = 0;
159         stats.rx_frames_delta = 0;
160         stats.matches_delta   = 0;
161
162         /* restart timer */
163         stattimer.expires = jiffies + HZ; /* every second */
164         add_timer(&stattimer);
165 }
166
167 /* 
168  * proc read functions
169  */
170
171 static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
172                              struct net_device *dev)
173 {
174         struct receiver *r;
175         struct hlist_node *n;
176
177         rcu_read_lock();
178         hlist_for_each_entry_rcu(r, n, rx_list, list) {
179                 char *fmt = (r->can_id & CAN_EFF_FLAG)?
180                         "   %-5s  %08X  %08x  %08x  %08x  %8ld  %s\n" :
181                         "   %-5s     %03X    %08x  %08x  %08x  %8ld  %s\n";
182
183                 len += snprintf(page + len, PAGE_SIZE - len, fmt,
184                                 DNAME(dev), r->can_id, r->mask,
185                                 (unsigned int)r->func, (unsigned int)r->data,
186                                 r->matches, r->ident);
187
188                 /* does a typical line fit into the current buffer? */
189
190                 /* 100 Bytes before end of buffer */
191                 if (len > PAGE_SIZE - 100) {
192                         /* mark output cut off */
193                         len += snprintf(page + len, PAGE_SIZE - len,
194                                         "   (..)\n");
195                         break;
196                 }
197         }
198         rcu_read_unlock();
199
200         return len;
201 }
202
203 static int can_print_recv_banner(char *page, int len)
204 {
205         /*
206          *                  can1.  00000000  00000000  00000000
207          *                 .......          0  tp20
208          */
209         len += snprintf(page + len, PAGE_SIZE - len,
210                         "  device   can_id   can_mask  function"
211                         "  userdata   matches  ident\n");
212
213         return len;
214 }
215
216 static int can_proc_read_stats(char *page, char **start, off_t off,
217                                int count, int *eof, void *data)
218 {
219         int len = 0;
220
221         len += snprintf(page + len, PAGE_SIZE - len, "\n");
222         len += snprintf(page + len, PAGE_SIZE - len,
223                         " %8ld transmitted frames (TXF)\n", stats.tx_frames);
224         len += snprintf(page + len, PAGE_SIZE - len,
225                         " %8ld received frames (RXF)\n", stats.rx_frames);
226         len += snprintf(page + len, PAGE_SIZE - len,
227                         " %8ld matched frames (RXMF)\n", stats.matches);
228
229         len += snprintf(page + len, PAGE_SIZE - len, "\n");
230
231         len += snprintf(page + len, PAGE_SIZE - len,
232                         " %8ld %% total match ratio (RXMR)\n",
233                         stats.total_rx_match_ratio);
234
235         len += snprintf(page + len, PAGE_SIZE - len,
236                         " %8ld frames/s total tx rate (TXR)\n",
237                         stats.total_tx_rate);
238         len += snprintf(page + len, PAGE_SIZE - len,
239                         " %8ld frames/s total rx rate (RXR)\n",
240                         stats.total_rx_rate);
241
242         len += snprintf(page + len, PAGE_SIZE - len, "\n");
243
244         len += snprintf(page + len, PAGE_SIZE - len,
245                         " %8ld %% current match ratio (CRXMR)\n",
246                         stats.current_rx_match_ratio);
247
248         len += snprintf(page + len, PAGE_SIZE - len,
249                         " %8ld frames/s current tx rate (CTXR)\n",
250                         stats.current_tx_rate);
251         len += snprintf(page + len, PAGE_SIZE - len,
252                         " %8ld frames/s current rx rate (CRXR)\n",
253                         stats.current_rx_rate);
254
255         len += snprintf(page + len, PAGE_SIZE - len, "\n");
256
257         len += snprintf(page + len, PAGE_SIZE - len,
258                         " %8ld %% max match ratio (MRXMR)\n",
259                         stats.max_rx_match_ratio);
260
261         len += snprintf(page + len, PAGE_SIZE - len,
262                         " %8ld frames/s max tx rate (MTXR)\n",
263                         stats.max_tx_rate);
264         len += snprintf(page + len, PAGE_SIZE - len,
265                         " %8ld frames/s max rx rate (MRXR)\n",
266                         stats.max_rx_rate);
267
268         len += snprintf(page + len, PAGE_SIZE - len, "\n");
269
270         len += snprintf(page + len, PAGE_SIZE - len,
271                         " %8ld current receive list entries (CRCV)\n",
272                         pstats.rcv_entries);
273         len += snprintf(page + len, PAGE_SIZE - len,
274                         " %8ld maximum receive list entries (MRCV)\n",
275                         pstats.rcv_entries_max);
276
277         if (pstats.stats_reset)
278                 len += snprintf(page + len, PAGE_SIZE - len,
279                                 "\n %8ld statistic resets (STR)\n",
280                                 pstats.stats_reset);
281
282         len += snprintf(page + len, PAGE_SIZE - len, "\n");
283
284         *eof = 1;
285         return len;
286 }
287
288 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
289                                      int count, int *eof, void *data)
290 {
291         int len = 0;
292
293         can_init_stats();
294
295         len += snprintf(page + len, PAGE_SIZE - len,
296                         "CAN statistic reset #%ld done.\n",
297                         pstats.stats_reset);
298
299         *eof = 1;
300         return len;
301 }
302
303 static int can_proc_read_version(char *page, char **start, off_t off,
304                                  int count, int *eof, void *data)
305 {
306         int len = 0;
307
308         len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
309                         CAN_VERSION_STRING);
310         *eof = 1;
311         return len;
312 }
313
314 static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
315                                      int count, int *eof, void *data)
316 {
317         int len = 0;
318         struct dev_rcv_lists *d;
319         struct hlist_node *n;
320
321         /* RX_ALL */
322         len += snprintf(page + len, PAGE_SIZE - len,
323                         "\nreceive list 'rx_all':\n");
324
325         /* find receive list for this device */
326         rcu_read_lock();
327         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
328
329                 if (!hlist_empty(&d->rx_all)) {
330                         len = can_print_recv_banner(page, len);
331                         len = can_print_rcvlist(page, len, &d->rx_all, d->dev);
332                 } else
333                         len += snprintf(page + len, PAGE_SIZE - len,
334                                         "  (%s: no entry)\n", DNAME(d->dev));
335
336                 /* exit on end of buffer? */
337                 if (len > PAGE_SIZE - 100)
338                         break;
339         }
340         rcu_read_unlock();
341
342         len += snprintf(page + len, PAGE_SIZE - len, "\n");
343
344         *eof = 1;
345         return len;
346 }
347
348 static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
349                                      int count, int *eof, void *data)
350 {
351         int len = 0;
352         struct dev_rcv_lists *d;
353         struct hlist_node *n;
354
355         /* RX_FIL */
356         len += snprintf(page + len, PAGE_SIZE - len,
357                         "\nreceive list 'rx_fil':\n");
358
359         /* find receive list for this device */
360         rcu_read_lock();
361         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
362
363                 if (!hlist_empty(&d->rx_fil)) {
364                         len = can_print_recv_banner(page, len);
365                         len = can_print_rcvlist(page, len, &d->rx_fil, d->dev);
366                 } else
367                         len += snprintf(page + len, PAGE_SIZE - len,
368                                         "  (%s: no entry)\n", DNAME(d->dev));
369
370                 /* exit on end of buffer? */
371                 if (len > PAGE_SIZE - 100)
372                         break;
373         }
374         rcu_read_unlock();
375
376         len += snprintf(page + len, PAGE_SIZE - len, "\n");
377
378         *eof = 1;
379         return len;
380 }
381
382 static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
383                                      int count, int *eof, void *data)
384 {
385         int len = 0;
386         struct dev_rcv_lists *d;
387         struct hlist_node *n;
388
389         /* RX_INV */
390         len += snprintf(page + len, PAGE_SIZE - len,
391                         "\nreceive list 'rx_inv':\n");
392
393         /* find receive list for this device */
394         rcu_read_lock();
395         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
396
397                 if (!hlist_empty(&d->rx_inv)) {
398                         len = can_print_recv_banner(page, len);
399                         len = can_print_rcvlist(page, len, &d->rx_inv, d->dev);
400                 } else
401                         len += snprintf(page + len, PAGE_SIZE - len,
402                                         "  (%s: no entry)\n", DNAME(d->dev));
403
404                 /* exit on end of buffer? */
405                 if (len > PAGE_SIZE - 100)
406                         break;
407         }
408         rcu_read_unlock();
409
410         len += snprintf(page + len, PAGE_SIZE - len, "\n");
411
412         *eof = 1;
413         return len;
414 }
415
416 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
417                                      int count, int *eof, void *data)
418 {
419         int len = 0;
420         struct dev_rcv_lists *d;
421         struct hlist_node *n;
422
423         /* RX_SFF */
424         len += snprintf(page + len, PAGE_SIZE - len,
425                         "\nreceive list 'rx_sff':\n");
426
427         /* find receive list for this device */
428         rcu_read_lock();
429         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
430                 int i, all_empty = 1;
431                 /* check wether at least one list is non-empty */
432                 for (i = 0; i < 0x800; i++)
433                         if (!hlist_empty(&d->rx_sff[i])) {
434                                 all_empty = 0;
435                                 break;
436                         }
437
438                 if (!all_empty) {
439                         len = can_print_recv_banner(page, len);
440                         for (i = 0; i < 0x800; i++) {
441                                 if (!hlist_empty(&d->rx_sff[i]) &&
442                                     len < PAGE_SIZE - 100)
443                                         len = can_print_rcvlist(page, len,
444                                                                 &d->rx_sff[i],
445                                                                 d->dev);
446                         }
447                 } else
448                         len += snprintf(page + len, PAGE_SIZE - len,
449                                         "  (%s: no entry)\n", DNAME(d->dev));
450
451                 /* exit on end of buffer? */
452                 if (len > PAGE_SIZE - 100)
453                         break;
454         }
455         rcu_read_unlock();
456
457         len += snprintf(page + len, PAGE_SIZE - len, "\n");
458
459         *eof = 1;
460         return len;
461 }
462
463 static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
464                                      int count, int *eof, void *data)
465 {
466         int len = 0;
467         struct dev_rcv_lists *d;
468         struct hlist_node *n;
469
470         /* RX_EFF */
471         len += snprintf(page + len, PAGE_SIZE - len,
472                         "\nreceive list 'rx_eff':\n");
473
474         /* find receive list for this device */
475         rcu_read_lock();
476         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
477
478                 if (!hlist_empty(&d->rx_eff)) {
479                         len = can_print_recv_banner(page, len);
480                         len = can_print_rcvlist(page, len, &d->rx_eff, d->dev);
481                 } else
482                         len += snprintf(page + len, PAGE_SIZE - len,
483                                         "  (%s: no entry)\n", DNAME(d->dev));
484
485                 /* exit on end of buffer? */
486                 if (len > PAGE_SIZE - 100)
487                         break;
488         }
489         rcu_read_unlock();
490
491         len += snprintf(page + len, PAGE_SIZE - len, "\n");
492
493         *eof = 1;
494         return len;
495 }
496
497 static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
498                                      int count, int *eof, void *data)
499 {
500         int len = 0;
501         struct dev_rcv_lists *d;
502         struct hlist_node *n;
503
504         /* RX_ERR */
505         len += snprintf(page + len, PAGE_SIZE - len,
506                         "\nreceive list 'rx_err':\n");
507
508         /* find receive list for this device */
509         rcu_read_lock();
510         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
511
512                 if (!hlist_empty(&d->rx_err)) {
513                         len = can_print_recv_banner(page, len);
514                         len = can_print_rcvlist(page, len, &d->rx_err, d->dev);
515                 } else
516                         len += snprintf(page + len, PAGE_SIZE - len,
517                                         "  (%s: no entry)\n", DNAME(d->dev));
518
519                 /* exit on end of buffer? */
520                 if (len > PAGE_SIZE - 100)
521                         break;
522         }
523         rcu_read_unlock();
524
525         len += snprintf(page + len, PAGE_SIZE - len, "\n");
526
527         *eof = 1;
528         return len;
529 }
530
531 /*
532  * proc utility functions
533  */
534
535 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
536                                                         mode_t mode,
537                                                         read_proc_t* read_proc,
538                                                         void *data)
539 {
540         if (can_dir)
541                 return create_proc_read_entry(name, mode, can_dir, read_proc,
542                                               data);
543         else
544                 return NULL;
545 }
546
547 static void can_remove_proc_readentry(const char *name)
548 {
549         if (can_dir)
550                 remove_proc_entry(name, can_dir);
551 }
552
553 /*
554  * can_init_proc - create main CAN proc directory and procfs entries
555  */
556 void can_init_proc(void)
557 {
558         /* create /proc/net/can directory */
559         can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
560
561         if (!can_dir) {
562                 printk(KERN_INFO "can: failed to create /proc/%s . "
563                        "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
564                 return;
565         }
566
567         can_dir->owner = THIS_MODULE;
568
569         /* own procfs entries from the AF_CAN core */
570         pde_version     = can_create_proc_readentry(
571                 CAN_PROC_VERSION, 0644, can_proc_read_version, NULL);
572         pde_stats       = can_create_proc_readentry(
573                 CAN_PROC_STATS, 0644, can_proc_read_stats, NULL);
574         pde_reset_stats = can_create_proc_readentry(
575                 CAN_PROC_RESET_STATS, 0644, can_proc_read_reset_stats, NULL);
576         pde_rcvlist_all = can_create_proc_readentry(
577                 CAN_PROC_RCVLIST_ALL, 0644, can_proc_read_rcvlist_all, NULL);
578         pde_rcvlist_fil = can_create_proc_readentry(
579                 CAN_PROC_RCVLIST_FIL, 0644, can_proc_read_rcvlist_fil, NULL);
580         pde_rcvlist_inv = can_create_proc_readentry(
581                 CAN_PROC_RCVLIST_INV, 0644, can_proc_read_rcvlist_inv, NULL);
582         pde_rcvlist_sff = can_create_proc_readentry(
583                 CAN_PROC_RCVLIST_SFF, 0644, can_proc_read_rcvlist_sff, NULL);
584         pde_rcvlist_eff = can_create_proc_readentry(
585                 CAN_PROC_RCVLIST_EFF, 0644, can_proc_read_rcvlist_eff, NULL);
586         pde_rcvlist_err = can_create_proc_readentry(
587                 CAN_PROC_RCVLIST_ERR, 0644, can_proc_read_rcvlist_err, NULL);
588 }
589
590 /*
591  * can_remove_proc - remove procfs entries and main CAN proc directory
592  */
593 void can_remove_proc(void)
594 {
595         if (pde_version)
596                 can_remove_proc_readentry(CAN_PROC_VERSION);
597
598         if (pde_stats)
599                 can_remove_proc_readentry(CAN_PROC_STATS);
600
601         if (pde_reset_stats)
602                 can_remove_proc_readentry(CAN_PROC_RESET_STATS);
603
604         if (pde_rcvlist_all)
605                 can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
606
607         if (pde_rcvlist_fil)
608                 can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
609
610         if (pde_rcvlist_inv)
611                 can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
612
613         if (pde_rcvlist_sff)
614                 can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
615
616         if (pde_rcvlist_eff)
617                 can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
618
619         if (pde_rcvlist_err)
620                 can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
621
622         if (can_dir)
623                 remove_proc_entry(CAN_PROC_DIR, NULL);
624 }