4 * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.
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
41 * Send feedback to <llcf@volkswagen.de>
45 #include <linux/module.h>
46 #include <linux/proc_fs.h>
55 #define CAN_PROC_VERSION "version"
56 #define CAN_PROC_STATS "stats"
57 #define CAN_PROC_RESET_STATS "reset_stats"
58 #define CAN_PROC_RCVLIST_ALL "rcvlist_all"
59 #define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
60 #define CAN_PROC_RCVLIST_INV "rcvlist_inv"
61 #define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
62 #define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
63 #define CAN_PROC_RCVLIST_ERR "rcvlist_err"
65 static void can_init_stats(int caller);
66 static void can_stat_update(unsigned long data);
68 static struct proc_dir_entry *can_create_proc_read_entry(const char *name, mode_t mode, read_proc_t* read_proc, void *data);
69 static void can_remove_proc_entry(const char *name);
70 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif, unsigned long count);
72 static int can_proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data);
73 static int can_proc_read_stats(char *page, char **start, off_t off, int count, int *eof, void *data);
74 static int can_proc_read_reset_stats(char *page, char **start, off_t off, int count, int *eof, void *data);
75 static int can_proc_read_rcvlist_all(char *page, char **start, off_t off, int count, int *eof, void *data);
76 static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off, int count, int *eof, void *data);
77 static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off, int count, int *eof, void *data);
78 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off, int count, int *eof, void *data);
79 static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off, int count, int *eof, void *data);
80 static int can_proc_read_rcvlist_err(char *page, char **start, off_t off, int count, int *eof, void *data);
82 static struct proc_dir_entry *can_dir = NULL;
83 static struct proc_dir_entry *pde_version = NULL;
84 static struct proc_dir_entry *pde_stats = NULL;
85 static struct proc_dir_entry *pde_reset_stats = NULL;
86 static struct proc_dir_entry *pde_rcvlist_all = NULL;
87 static struct proc_dir_entry *pde_rcvlist_fil = NULL;
88 static struct proc_dir_entry *pde_rcvlist_inv = NULL;
89 static struct proc_dir_entry *pde_rcvlist_sff = NULL;
90 static struct proc_dir_entry *pde_rcvlist_eff = NULL;
91 static struct proc_dir_entry *pde_rcvlist_err = NULL;
93 struct timer_list stattimer; /* timer for statistics update */
95 struct s_stats stats; /* statistics */
96 struct s_pstats pstats;
98 extern struct rcv_dev_list *rx_dev_list; /* rx dispatcher structures */
99 extern int stats_timer; /* module parameter. default: on */
101 /**************************************************/
102 /* procfs init / remove */
103 /**************************************************/
105 void can_init_proc(void)
110 /* create /proc/can directory */
111 can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
115 can_dir->owner = THIS_MODULE;
117 /* own procfs entries from the AF_CAN core */
118 pde_version = can_create_proc_read_entry(CAN_PROC_VERSION, 0644, can_proc_read_version, NULL);
119 pde_stats = can_create_proc_read_entry(CAN_PROC_STATS, 0644, can_proc_read_stats, NULL);
120 pde_reset_stats = can_create_proc_read_entry(CAN_PROC_RESET_STATS, 0644, can_proc_read_reset_stats, NULL);
121 pde_rcvlist_all = can_create_proc_read_entry(CAN_PROC_RCVLIST_ALL, 0644, can_proc_read_rcvlist_all, NULL);
122 pde_rcvlist_fil = can_create_proc_read_entry(CAN_PROC_RCVLIST_FIL, 0644, can_proc_read_rcvlist_fil, NULL);
123 pde_rcvlist_inv = can_create_proc_read_entry(CAN_PROC_RCVLIST_INV, 0644, can_proc_read_rcvlist_inv, NULL);
124 pde_rcvlist_sff = can_create_proc_read_entry(CAN_PROC_RCVLIST_SFF, 0644, can_proc_read_rcvlist_sff, NULL);
125 pde_rcvlist_eff = can_create_proc_read_entry(CAN_PROC_RCVLIST_EFF, 0644, can_proc_read_rcvlist_eff, NULL);
126 pde_rcvlist_err = can_create_proc_read_entry(CAN_PROC_RCVLIST_ERR, 0644, can_proc_read_rcvlist_err, NULL);
129 /* the statistics are updated every second (timer triggered) */
130 stattimer.function = can_stat_update;
132 stattimer.expires = jiffies + HZ; /* every second */
133 add_timer(&stattimer); /* start statistics timer */
136 printk(KERN_INFO "af_can: failed to create CAN_PROC_DIR. CONFIG_PROC_FS missing?\n");
139 void can_remove_proc(void)
143 can_remove_proc_entry(CAN_PROC_VERSION);
147 can_remove_proc_entry(CAN_PROC_STATS);
150 if (pde_reset_stats) {
151 can_remove_proc_entry(CAN_PROC_RESET_STATS);
154 if (pde_rcvlist_all) {
155 can_remove_proc_entry(CAN_PROC_RCVLIST_ALL);
158 if (pde_rcvlist_fil) {
159 can_remove_proc_entry(CAN_PROC_RCVLIST_FIL);
162 if (pde_rcvlist_inv) {
163 can_remove_proc_entry(CAN_PROC_RCVLIST_INV);
166 if (pde_rcvlist_sff) {
167 can_remove_proc_entry(CAN_PROC_RCVLIST_SFF);
170 if (pde_rcvlist_eff) {
171 can_remove_proc_entry(CAN_PROC_RCVLIST_EFF);
174 if (pde_rcvlist_err) {
175 can_remove_proc_entry(CAN_PROC_RCVLIST_ERR);
179 remove_proc_entry(CAN_PROC_DIR, NULL);
183 /**************************************************/
184 /* proc read functions */
185 /**************************************************/
187 int can_print_recv_list(char *page, int len, struct rcv_list *rx_list, struct net_device *dev)
192 for (p = rx_list; p; p = p->next) {
194 /* can1. 00000000 00000000 00000000 .......0 tp20 */
195 if (p->can_id & CAN_EFF_FLAG) /* EFF & CAN_ID_ALL */
196 len += snprintf(page + len, PAGE_SIZE - len, " %-5s %08X %08x %08x %08x %8ld %s\n",
197 dev->name, p->can_id, p->mask, (unsigned int)p->func,
198 (unsigned int)p->data, p->matches, p->ident);
200 len += snprintf(page + len, PAGE_SIZE - len, " %-5s %03X %08x %08x %08x %8ld %s\n",
201 dev->name, p->can_id, p->mask, (unsigned int)p->func,
202 (unsigned int)p->data, p->matches, p->ident);
204 /* does a typical line fit into the current buffer? */
205 if (len > PAGE_SIZE - 100) { /* 100 Bytes before end of buffer */
206 len += snprintf(page + len, PAGE_SIZE - len, " (..)\n"); /* mark output cutted off */
215 static int can_print_recv_banner(char *page, int len)
217 /* can1. 00000000 00000000 00000000 .......0 tp20 */
218 len += snprintf(page + len, PAGE_SIZE - len,
219 " device can_id can_mask function userdata matches ident\n");
224 int can_proc_read_stats(char *page, char **start, off_t off, int count, int *eof, void *data)
230 len += snprintf(page + len, PAGE_SIZE - len, "\n");
231 len += snprintf(page + len, PAGE_SIZE - len, " %8ld transmitted frames (TXF)\n",
233 len += snprintf(page + len, PAGE_SIZE - len, " %8ld received frames (RXF)\n",
235 len += snprintf(page + len, PAGE_SIZE - len, " %8ld matched frames (RXMF)\n",
238 len += snprintf(page + len, PAGE_SIZE - len, "\n");
240 len += snprintf(page + len, PAGE_SIZE - len, " %8ld %% total match ratio (RXMR)\n",
241 stats.total_rx_match_ratio);
243 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s total tx rate (TXR)\n",
244 stats.total_tx_rate);
245 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s total rx rate (RXR)\n",
246 stats.total_rx_rate);
248 len += snprintf(page + len, PAGE_SIZE - len, "\n");
250 len += snprintf(page + len, PAGE_SIZE - len, " %8ld %% current match ratio (CRXMR)\n",
251 stats.current_rx_match_ratio);
253 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s current tx rate (CTXR)\n",
254 stats.current_tx_rate);
255 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s current rx rate (CRXR)\n",
256 stats.current_rx_rate);
258 len += snprintf(page + len, PAGE_SIZE - len, "\n");
260 len += snprintf(page + len, PAGE_SIZE - len, " %8ld %% max match ratio (MRXMR)\n",
261 stats.max_rx_match_ratio);
263 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s max tx rate (MTXR)\n",
265 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s max rx rate (MRXR)\n",
268 len += snprintf(page + len, PAGE_SIZE - len, "\n");
270 len += snprintf(page + len, PAGE_SIZE - len, " %8ld current receive list entries (CRCV)\n", pstats.rcv_entries);
271 len += snprintf(page + len, PAGE_SIZE - len, " %8ld maximum receive list entries (MRCV)\n", pstats.rcv_entries_max);
273 if (pstats.stats_reset)
274 len += snprintf(page + len, PAGE_SIZE - len, "\n %8ld statistic resets (STR)\n", pstats.stats_reset);
276 len += snprintf(page + len, PAGE_SIZE - len, "\n");
284 int can_proc_read_reset_stats(char *page, char **start, off_t off, int count, int *eof, void *data)
292 len += snprintf(page + len, PAGE_SIZE - len, "CAN statistic reset #%ld done.\n", pstats.stats_reset);
300 int can_proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data)
306 len += snprintf(page + len, PAGE_SIZE - len,
307 "%06X [ Volkswagen AG - Low Level CAN Framework (LLCF) v%s ]\n",
308 LLCF_VERSION_CODE, VERSION);
316 int can_proc_read_rcvlist_all(char *page, char **start, off_t off, int count, int *eof, void *data)
319 struct rcv_dev_list *p;
324 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_all':\n");
326 /* find receive list for this device */
327 for (p = rx_dev_list; p; p = p->next) {
330 len = can_print_recv_banner(page, len);
331 len = can_print_recv_list(page, len, p->rx_all, p->dev);
334 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
337 len += snprintf(page + len, PAGE_SIZE - len, "\n");
345 int can_proc_read_rcvlist_fil(char *page, char **start, off_t off, int count, int *eof, void *data)
348 struct rcv_dev_list *p;
353 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_fil':\n");
355 /* find receive list for this device */
356 for (p = rx_dev_list; p; p = p->next) {
359 len = can_print_recv_banner(page, len);
360 len = can_print_recv_list(page, len, p->rx_fil, p->dev);
363 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
366 len += snprintf(page + len, PAGE_SIZE - len, "\n");
374 int can_proc_read_rcvlist_inv(char *page, char **start, off_t off, int count, int *eof, void *data)
377 struct rcv_dev_list *p;
382 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_inv':\n");
384 /* find receive list for this device */
385 for (p = rx_dev_list; p; p = p->next) {
388 len = can_print_recv_banner(page, len);
389 len = can_print_recv_list(page, len, p->rx_inv, p->dev);
392 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
395 len += snprintf(page + len, PAGE_SIZE - len, "\n");
403 int can_proc_read_rcvlist_sff(char *page, char **start, off_t off, int count, int *eof, void *data)
406 unsigned long id = 0;
408 struct rcv_dev_list *p;
413 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_sff':\n");
415 /* find receive list for this device */
416 for (p = rx_dev_list; p; p = p->next) {
418 for(i=0; i<0x800; i++)
419 id |= (unsigned long) p->rx_sff[i]; /* check if any entry available */
422 len = can_print_recv_banner(page, len);
423 for(i=0; i<0x800; i++) {
424 if ((p->rx_sff[i]) && (len < PAGE_SIZE - 100))
425 len = can_print_recv_list(page, len, p->rx_sff[i], p->dev);
429 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
432 len += snprintf(page + len, PAGE_SIZE - len, "\n");
440 int can_proc_read_rcvlist_eff(char *page, char **start, off_t off, int count, int *eof, void *data)
443 struct rcv_dev_list *p;
448 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_eff':\n");
450 /* find receive list for this device */
451 for (p = rx_dev_list; p; p = p->next) {
454 len = can_print_recv_banner(page, len);
455 len = can_print_recv_list(page, len, p->rx_eff, p->dev);
458 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
461 len += snprintf(page + len, PAGE_SIZE - len, "\n");
469 int can_proc_read_rcvlist_err(char *page, char **start, off_t off, int count, int *eof, void *data)
472 struct rcv_dev_list *p;
477 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_err':\n");
479 /* find receive list for this device */
480 for (p = rx_dev_list; p; p = p->next) {
483 len = can_print_recv_banner(page, len);
484 len = can_print_recv_list(page, len, p->rx_err, p->dev);
487 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
490 len += snprintf(page + len, PAGE_SIZE - len, "\n");
498 /**************************************************/
499 /* proc utility functions */
500 /**************************************************/
502 static struct proc_dir_entry *can_create_proc_read_entry(const char *name, mode_t mode, read_proc_t* read_proc, void *data)
505 return create_proc_read_entry(name, mode, can_dir, read_proc, data);
510 static void can_remove_proc_entry(const char *name)
513 remove_proc_entry(name, can_dir);
516 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif, unsigned long count){
518 unsigned long ret = 0;
520 if (oldjif == newjif)
523 if (count > (ULONG_MAX / HZ)) { /* see can_rcv() - this should NEVER happen! */
524 printk(KERN_ERR "af_can: calc_rate: count exceeded! %ld\n", count);
528 ret = ((count * HZ) / (newjif - oldjif));
533 /**************************************************/
534 /* af_can statistics stuff */
535 /**************************************************/
537 static void can_init_stats(int caller){
539 memset(&stats, 0, sizeof(stats));
540 stats.jiffies_init = jiffies;
541 pstats.stats_reset++;
544 static void can_stat_update(unsigned long data){
546 unsigned long j = jiffies; /* snapshot */
548 //DBG("af_can: can_stat_update() jiffies = %ld\n", j);
550 if (j < stats.jiffies_init) /* jiffies overflow */
553 /* stats.rx_frames is the definitively max. statistic value */
554 if (stats.rx_frames > (ULONG_MAX / HZ)) /* prevent overflow in calc_rate() */
555 can_init_stats(3); /* restart */
557 if (stats.matches > (ULONG_MAX / 100)) /* matches overflow - very improbable */
560 /* calc total values */
562 stats.total_rx_match_ratio = (stats.matches * 100) / stats.rx_frames;
564 stats.total_tx_rate = calc_rate(stats.jiffies_init, j, stats.tx_frames);
565 stats.total_rx_rate = calc_rate(stats.jiffies_init, j, stats.rx_frames);
567 /* calc current values */
568 if (stats.rx_frames_delta)
569 stats.current_rx_match_ratio = (stats.matches_delta * 100) / stats.rx_frames_delta;
571 stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
572 stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
574 /* check / update maximum values */
575 if (stats.max_tx_rate < stats.current_tx_rate)
576 stats.max_tx_rate = stats.current_tx_rate;
578 if (stats.max_rx_rate < stats.current_rx_rate)
579 stats.max_rx_rate = stats.current_rx_rate;
581 if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
582 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
584 /* clear values for 'current rate' calculation */
585 stats.tx_frames_delta = 0;
586 stats.rx_frames_delta = 0;
587 stats.matches_delta = 0;
590 stattimer.expires = jiffies + HZ; /* every second */
591 add_timer(&stattimer);
594 /**************************************************/
596 /**************************************************/