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>
48 #include <net/can/af_can.h>
49 #include <net/can/version.h>
51 RCSID("$Id: proc.c,v 2.0 2006/04/13 10:37:19 ethuerm Exp $");
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"
64 static void can_init_stats(int caller);
65 static void can_stat_update(unsigned long data);
67 static struct proc_dir_entry *can_create_proc_read_entry(const char *name, mode_t mode, read_proc_t* read_proc, void *data);
68 static void can_remove_proc_entry(const char *name);
69 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif, unsigned long count);
71 static int can_proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data);
72 static int can_proc_read_stats(char *page, char **start, off_t off, int count, int *eof, void *data);
73 static int can_proc_read_reset_stats(char *page, char **start, off_t off, int count, int *eof, void *data);
74 static int can_proc_read_rcvlist_all(char *page, char **start, off_t off, int count, int *eof, void *data);
75 static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off, int count, int *eof, void *data);
76 static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off, int count, int *eof, void *data);
77 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off, int count, int *eof, void *data);
78 static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off, int count, int *eof, void *data);
80 static struct proc_dir_entry *can_dir = NULL;
81 static struct proc_dir_entry *pde_version = NULL;
82 static struct proc_dir_entry *pde_stats = NULL;
83 static struct proc_dir_entry *pde_reset_stats = NULL;
84 static struct proc_dir_entry *pde_rcvlist_all = NULL;
85 static struct proc_dir_entry *pde_rcvlist_fil = NULL;
86 static struct proc_dir_entry *pde_rcvlist_inv = NULL;
87 static struct proc_dir_entry *pde_rcvlist_sff = NULL;
88 static struct proc_dir_entry *pde_rcvlist_eff = NULL;
90 struct timer_list stattimer; /* timer for statistics update */
92 struct s_stats stats; /* statistics */
93 struct s_pstats pstats;
95 extern struct rcv_dev_list *rx_dev_list; /* rx dispatcher structures */
96 extern int stats_timer; /* module parameter. default: on */
98 /**************************************************/
99 /* procfs init / remove */
100 /**************************************************/
102 void can_init_proc(void)
107 /* create /proc/can directory */
108 can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
112 can_dir->owner = THIS_MODULE;
114 /* own procfs entries from the AF_CAN core */
115 pde_version = can_create_proc_read_entry(CAN_PROC_VERSION, 0644, can_proc_read_version, NULL);
116 pde_stats = can_create_proc_read_entry(CAN_PROC_STATS, 0644, can_proc_read_stats, NULL);
117 pde_reset_stats = can_create_proc_read_entry(CAN_PROC_RESET_STATS, 0644, can_proc_read_reset_stats, NULL);
118 pde_rcvlist_all = can_create_proc_read_entry(CAN_PROC_RCVLIST_ALL, 0644, can_proc_read_rcvlist_all, NULL);
119 pde_rcvlist_fil = can_create_proc_read_entry(CAN_PROC_RCVLIST_FIL, 0644, can_proc_read_rcvlist_fil, NULL);
120 pde_rcvlist_inv = can_create_proc_read_entry(CAN_PROC_RCVLIST_INV, 0644, can_proc_read_rcvlist_inv, NULL);
121 pde_rcvlist_sff = can_create_proc_read_entry(CAN_PROC_RCVLIST_SFF, 0644, can_proc_read_rcvlist_sff, NULL);
122 pde_rcvlist_eff = can_create_proc_read_entry(CAN_PROC_RCVLIST_EFF, 0644, can_proc_read_rcvlist_eff, NULL);
125 /* the statistics are updated every second (timer triggered) */
126 stattimer.function = can_stat_update;
128 stattimer.expires = jiffies + HZ; /* every second */
129 add_timer(&stattimer); /* start statistics timer */
132 printk(KERN_INFO "af_can: failed to create CAN_PROC_DIR. CONFIG_PROC_FS missing?\n");
135 void can_remove_proc(void)
139 can_remove_proc_entry(CAN_PROC_VERSION);
143 can_remove_proc_entry(CAN_PROC_STATS);
146 if (pde_reset_stats) {
147 can_remove_proc_entry(CAN_PROC_RESET_STATS);
150 if (pde_rcvlist_all) {
151 can_remove_proc_entry(CAN_PROC_RCVLIST_ALL);
154 if (pde_rcvlist_fil) {
155 can_remove_proc_entry(CAN_PROC_RCVLIST_FIL);
158 if (pde_rcvlist_inv) {
159 can_remove_proc_entry(CAN_PROC_RCVLIST_INV);
162 if (pde_rcvlist_sff) {
163 can_remove_proc_entry(CAN_PROC_RCVLIST_SFF);
166 if (pde_rcvlist_eff) {
167 can_remove_proc_entry(CAN_PROC_RCVLIST_EFF);
171 remove_proc_entry(CAN_PROC_DIR, NULL);
175 /**************************************************/
176 /* proc read functions */
177 /**************************************************/
179 int can_print_recv_list(char *page, int len, struct rcv_list *rx_list, struct net_device *dev)
184 for (p = rx_list; p; p = p->next) {
186 /* can1. 00000000 00000000 00000000 .......0 tp20 */
187 if (p->can_id & CAN_EFF_FLAG) /* EFF & CAN_ID_ALL */
188 len += snprintf(page + len, PAGE_SIZE - len, " %-5s %08X %08x %08x %08x %8ld %s\n",
189 dev->name, p->can_id, p->mask, (unsigned int)p->func,
190 (unsigned int)p->data, p->matches, p->ident);
192 len += snprintf(page + len, PAGE_SIZE - len, " %-5s %03X %08x %08x %08x %8ld %s\n",
193 dev->name, p->can_id, p->mask, (unsigned int)p->func,
194 (unsigned int)p->data, p->matches, p->ident);
196 /* does a typical line fit into the current buffer? */
197 if (len > PAGE_SIZE - 100) { /* 100 Bytes before end of buffer */
198 len += snprintf(page + len, PAGE_SIZE - len, " (..)\n"); /* mark output cutted off */
207 static int can_print_recv_banner(char *page, int len)
209 /* can1. 00000000 00000000 00000000 .......0 tp20 */
210 len += snprintf(page + len, PAGE_SIZE - len,
211 " device can_id can_mask function userdata matches ident\n");
216 int can_proc_read_stats(char *page, char **start, off_t off, int count, int *eof, void *data)
220 len += snprintf(page + len, PAGE_SIZE - len, "\n");
221 len += snprintf(page + len, PAGE_SIZE - len, " %8ld transmitted frames (TXF)\n",
223 len += snprintf(page + len, PAGE_SIZE - len, " %8ld received frames (RXF)\n",
225 len += snprintf(page + len, PAGE_SIZE - len, " %8ld matched frames (RXMF)\n",
228 len += snprintf(page + len, PAGE_SIZE - len, "\n");
230 len += snprintf(page + len, PAGE_SIZE - len, " %8ld %% total match ratio (RXMR)\n",
231 stats.total_rx_match_ratio);
233 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s total tx rate (TXR)\n",
234 stats.total_tx_rate);
235 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s total rx rate (RXR)\n",
236 stats.total_rx_rate);
238 len += snprintf(page + len, PAGE_SIZE - len, "\n");
240 len += snprintf(page + len, PAGE_SIZE - len, " %8ld %% current match ratio (CRXMR)\n",
241 stats.current_rx_match_ratio);
243 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s current tx rate (CTXR)\n",
244 stats.current_tx_rate);
245 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s current rx rate (CRXR)\n",
246 stats.current_rx_rate);
248 len += snprintf(page + len, PAGE_SIZE - len, "\n");
250 len += snprintf(page + len, PAGE_SIZE - len, " %8ld %% max match ratio (MRXMR)\n",
251 stats.max_rx_match_ratio);
253 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s max tx rate (MTXR)\n",
255 len += snprintf(page + len, PAGE_SIZE - len, " %8ld frames/s max rx rate (MRXR)\n",
258 len += snprintf(page + len, PAGE_SIZE - len, "\n");
260 len += snprintf(page + len, PAGE_SIZE - len, " %8ld current receive list entries (CRCV)\n", pstats.rcv_entries);
261 len += snprintf(page + len, PAGE_SIZE - len, " %8ld maximum receive list entries (MRCV)\n", pstats.rcv_entries_max);
263 if (pstats.stats_reset)
264 len += snprintf(page + len, PAGE_SIZE - len, "\n %8ld statistic resets (STR)\n", pstats.stats_reset);
266 len += snprintf(page + len, PAGE_SIZE - len, "\n");
272 int can_proc_read_reset_stats(char *page, char **start, off_t off, int count, int *eof, void *data)
278 len += snprintf(page + len, PAGE_SIZE - len, "CAN statistic reset #%ld done.\n", pstats.stats_reset);
284 int can_proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data)
288 len += snprintf(page + len, PAGE_SIZE - len,
289 "%06X [ Volkswagen AG - Low Level CAN Framework (LLCF) v%s ]\n",
290 LLCF_VERSION_CODE, VERSION);
296 int can_proc_read_rcvlist_all(char *page, char **start, off_t off, int count, int *eof, void *data)
299 struct rcv_dev_list *p;
302 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_all':\n");
304 /* find receive list for this device */
305 for (p = rx_dev_list; p; p = p->next) {
308 len = can_print_recv_banner(page, len);
309 len = can_print_recv_list(page, len, p->rx_all, p->dev);
312 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
315 len += snprintf(page + len, PAGE_SIZE - len, "\n");
321 int can_proc_read_rcvlist_fil(char *page, char **start, off_t off, int count, int *eof, void *data)
324 struct rcv_dev_list *p;
327 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_fil':\n");
329 /* find receive list for this device */
330 for (p = rx_dev_list; p; p = p->next) {
333 len = can_print_recv_banner(page, len);
334 len = can_print_recv_list(page, len, p->rx_fil, p->dev);
337 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
340 len += snprintf(page + len, PAGE_SIZE - len, "\n");
346 int can_proc_read_rcvlist_inv(char *page, char **start, off_t off, int count, int *eof, void *data)
349 struct rcv_dev_list *p;
352 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_inv':\n");
354 /* find receive list for this device */
355 for (p = rx_dev_list; p; p = p->next) {
358 len = can_print_recv_banner(page, len);
359 len = can_print_recv_list(page, len, p->rx_inv, p->dev);
362 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
365 len += snprintf(page + len, PAGE_SIZE - len, "\n");
371 int can_proc_read_rcvlist_sff(char *page, char **start, off_t off, int count, int *eof, void *data)
374 unsigned long id = 0;
376 struct rcv_dev_list *p;
379 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_sff':\n");
381 /* find receive list for this device */
382 for (p = rx_dev_list; p; p = p->next) {
384 for(i=0; i<0x800; i++)
385 id |= (unsigned long) p->rx_sff[i]; /* check if any entry available */
388 len = can_print_recv_banner(page, len);
389 for(i=0; i<0x800; i++) {
390 if ((p->rx_sff[i]) && (len < PAGE_SIZE - 100))
391 len = can_print_recv_list(page, len, p->rx_sff[i], p->dev);
395 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
398 len += snprintf(page + len, PAGE_SIZE - len, "\n");
404 int can_proc_read_rcvlist_eff(char *page, char **start, off_t off, int count, int *eof, void *data)
407 struct rcv_dev_list *p;
410 len += snprintf(page + len, PAGE_SIZE - len, "\nreceive list 'rx_eff':\n");
412 /* find receive list for this device */
413 for (p = rx_dev_list; p; p = p->next) {
416 len = can_print_recv_banner(page, len);
417 len = can_print_recv_list(page, len, p->rx_eff, p->dev);
420 len += snprintf(page + len, PAGE_SIZE - len, " (%s: no entry)\n", p->dev->name);
423 len += snprintf(page + len, PAGE_SIZE - len, "\n");
429 /**************************************************/
430 /* proc utility functions */
431 /**************************************************/
433 static struct proc_dir_entry *can_create_proc_read_entry(const char *name, mode_t mode, read_proc_t* read_proc, void *data)
436 return create_proc_read_entry(name, mode, can_dir, read_proc, data);
441 static void can_remove_proc_entry(const char *name)
444 remove_proc_entry(name, can_dir);
447 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif, unsigned long count){
449 unsigned long ret = 0;
451 if (oldjif == newjif)
454 if (count > (ULONG_MAX / HZ)) { /* see can_rcv() - this should NEVER happen! */
455 printk(KERN_ERR "af_can: calc_rate: count exceeded! %ld\n", count);
459 ret = ((count * HZ) / (newjif - oldjif));
464 /**************************************************/
465 /* af_can statistics stuff */
466 /**************************************************/
468 static void can_init_stats(int caller){
470 memset(&stats, 0, sizeof(stats));
471 stats.jiffies_init = jiffies;
472 pstats.stats_reset++;
475 static void can_stat_update(unsigned long data){
477 unsigned long j = jiffies; /* snapshot */
479 //DBG("af_can: can_stat_update() jiffies = %ld\n", j);
481 if (j < stats.jiffies_init) /* jiffies overflow */
484 /* stats.rx_frames is the definitively max. statistic value */
485 if (stats.rx_frames > (ULONG_MAX / HZ)) /* prevent overflow in calc_rate() */
486 can_init_stats(3); /* restart */
488 if (stats.matches > (ULONG_MAX / 100)) /* matches overflow - very improbable */
491 /* calc total values */
493 stats.total_rx_match_ratio = (stats.matches * 100) / stats.rx_frames;
495 stats.total_tx_rate = calc_rate(stats.jiffies_init, j, stats.tx_frames);
496 stats.total_rx_rate = calc_rate(stats.jiffies_init, j, stats.rx_frames);
498 /* calc current values */
499 if (stats.rx_frames_delta)
500 stats.current_rx_match_ratio = (stats.matches_delta * 100) / stats.rx_frames_delta;
502 stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
503 stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
505 /* check / update maximum values */
506 if (stats.max_tx_rate < stats.current_tx_rate)
507 stats.max_tx_rate = stats.current_tx_rate;
509 if (stats.max_rx_rate < stats.current_rx_rate)
510 stats.max_rx_rate = stats.current_rx_rate;
512 if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
513 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
515 /* clear values for 'current rate' calculation */
516 stats.tx_frames_delta = 0;
517 stats.rx_frames_delta = 0;
518 stats.matches_delta = 0;
521 stattimer.expires = jiffies + HZ; /* every second */
522 add_timer(&stattimer);
525 /**************************************************/
527 /**************************************************/