]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nand/nandsim_log.c
MFV r337029:
[FreeBSD/FreeBSD.git] / sys / dev / nand / nandsim_log.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2009-2012 Semihalf
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 and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
37 #include <sys/proc.h>
38 #include <sys/alq.h>
39 #include <sys/time.h>
40
41 #include <machine/stdarg.h>
42
43 #include <dev/nand/nandsim_log.h>
44
45 int  nandsim_log_level;
46 int  nandsim_log_output;
47 int  log_size = NANDSIM_RAM_LOG_SIZE;
48
49 static int  nandsim_entry_size = NANDSIM_ENTRY_SIZE;
50 static int  nandsim_entry_count = NANDSIM_ENTRY_COUNT;
51 static int  str_index = 0;
52 static char string[NANDSIM_ENTRY_SIZE + 1] = {0};
53
54 int
55 nandsim_log_init(struct nandsim_softc *sc, char *filename)
56 {
57         int error = 0;
58
59         if (nandsim_log_output == NANDSIM_OUTPUT_FILE) {
60                 error = alq_open(&sc->alq, filename,
61                     curthread->td_ucred, 0644,
62                     nandsim_entry_size, nandsim_entry_count);
63         } else if (nandsim_log_output == NANDSIM_OUTPUT_RAM) {
64                 sc->log_buff = malloc(log_size, M_NANDSIM, M_WAITOK | M_ZERO);
65                 if (!sc->log_buff)
66                         error = ENOMEM;
67         }
68
69         return (error);
70 }
71
72 void
73 nandsim_log_close(struct nandsim_softc *sc)
74 {
75
76         if (nandsim_log_output == NANDSIM_OUTPUT_FILE) {
77                 memset(&string[str_index], 0, NANDSIM_ENTRY_SIZE - str_index);
78                 alq_write(sc->alq, (void *) string, ALQ_NOWAIT);
79                 str_index = 0;
80                 string[0] = '\0';
81                 alq_close(sc->alq);
82         } else if (nandsim_log_output == NANDSIM_OUTPUT_RAM) {
83                 free(sc->log_buff, M_NANDSIM);
84                 sc->log_buff = NULL;
85         }
86 }
87
88 void
89 nandsim_log(struct nandsim_chip *chip, int level, const char *fmt, ...)
90 {
91         char hdr[TIME_STR_SIZE];
92         char tmp[NANDSIM_ENTRY_SIZE];
93         struct nandsim_softc *sc;
94         struct timeval currtime;
95         va_list ap;
96         int hdr_len, len, rest;
97
98         if (nandsim_log_output == NANDSIM_OUTPUT_NONE)
99                 return;
100
101         if (chip == NULL)
102                 return;
103
104         sc = chip->sc;
105         if (!sc->alq && nandsim_log_output == NANDSIM_OUTPUT_FILE)
106                 return;
107
108         if (level <= nandsim_log_level) {
109                 microtime(&currtime);
110                 hdr_len = sprintf(hdr, "%08jd.%08li [chip:%d, ctrl:%d]: ",
111                     (intmax_t)currtime.tv_sec, currtime.tv_usec,
112                     chip->chip_num, chip->ctrl_num);
113
114                 switch(nandsim_log_output) {
115                 case NANDSIM_OUTPUT_CONSOLE:
116                         printf("%s", hdr);
117                         va_start(ap, fmt);
118                         vprintf(fmt, ap);
119                         va_end(ap);
120                         break;
121                 case NANDSIM_OUTPUT_RAM:
122                         va_start(ap, fmt);
123                         len = vsnprintf(tmp, NANDSIM_ENTRY_SIZE - 1, fmt, ap);
124                         tmp[NANDSIM_ENTRY_SIZE - 1] = 0;
125                         va_end(ap);
126
127                         rest = log_size - sc->log_idx - 1;
128                         if (rest >= hdr_len) {
129                                 bcopy(hdr, &sc->log_buff[sc->log_idx],
130                                     hdr_len);
131                                 sc->log_idx += hdr_len;
132                                 sc->log_buff[sc->log_idx] = 0;
133                         } else {
134                                 bcopy(hdr, &sc->log_buff[sc->log_idx], rest);
135                                 bcopy(&hdr[rest], sc->log_buff,
136                                     hdr_len - rest);
137                                 sc->log_idx = hdr_len - rest;
138                                 sc->log_buff[sc->log_idx] = 0;
139                         }
140
141                         rest = log_size - sc->log_idx - 1;
142                         if (rest >= len) {
143                                 bcopy(tmp, &sc->log_buff[sc->log_idx], len);
144                                 sc->log_idx += len;
145                                 sc->log_buff[sc->log_idx] = 0;
146                         } else {
147                                 bcopy(tmp, &sc->log_buff[sc->log_idx], rest);
148                                 bcopy(&tmp[rest], sc->log_buff, len - rest);
149                                 sc->log_idx = len - rest;
150                                 sc->log_buff[sc->log_idx] = 0;
151                         }
152
153                         break;
154
155                 case NANDSIM_OUTPUT_FILE:
156                         va_start(ap, fmt);
157                         len = vsnprintf(tmp, NANDSIM_ENTRY_SIZE - 1, fmt, ap);
158                         tmp[NANDSIM_ENTRY_SIZE - 1] = 0;
159                         va_end(ap);
160
161                         rest = NANDSIM_ENTRY_SIZE - str_index;
162                         if (rest >= hdr_len) {
163                                 strcat(string, hdr);
164                                 str_index += hdr_len;
165                         } else {
166                                 strlcat(string, hdr, NANDSIM_ENTRY_SIZE + 1);
167                                 alq_write(sc->alq, (void *) string,
168                                     ALQ_NOWAIT);
169                                 strcpy(string, &hdr[rest]);
170                                 str_index = hdr_len - rest;
171                         }
172                         rest = NANDSIM_ENTRY_SIZE - str_index;
173                         if (rest >= len) {
174                                 strcat(string, tmp);
175                                 str_index += len;
176                         } else {
177                                 strlcat(string, tmp, NANDSIM_ENTRY_SIZE + 1);
178                                 alq_write(sc->alq, (void *) string,
179                                     ALQ_NOWAIT);
180                                 strcpy(string, &tmp[rest]);
181                                 str_index = len - rest;
182                         }
183                         break;
184                 default:
185                         break;
186                 }
187         }
188 }