aboutsummaryrefslogtreecommitdiff
path: root/drivers/scsi/esas2r/esas2r_log.c
blob: 65fdf22b0ba9ad4af8a4d166955956d6f164dba3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 *  linux/drivers/scsi/esas2r/esas2r_log.c
 *      For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
 *
 *  Copyright (c) 2001-2013 ATTO Technology, Inc.
 *  (mailto:linuxdrivers@attotech.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * NO WARRANTY
 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
 * solely responsible for determining the appropriateness of using and
 * distributing the Program and assumes all risks associated with its
 * exercise of rights under this Agreement, including but not limited to
 * the risks and costs of program errors, damage to or loss of data,
 * programs or equipment, and unavailability or interruption of operations.
 *
 * DISCLAIMER OF LIABILITY
 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

#include "esas2r.h"

/*
 * this module within the driver is tasked with providing logging functionality.
 * the event_log_level module parameter controls the level of messages that are
 * written to the system log.  the default level of messages that are written
 * are critical and warning messages.  if other types of messages are desired,
 * one simply needs to load the module with the correct value for the
 * event_log_level module parameter.  for example:
 *
 * insmod <module> event_log_level=1
 *
 * will load the module and only critical events will be written by this module
 * to the system log.  if critical, warning, and information-level messages are
 * desired, the correct value for the event_log_level module parameter
 * would be as follows:
 *
 * insmod <module> event_log_level=3
 */

#define EVENT_LOG_BUFF_SIZE 1024

static long event_log_level = ESAS2R_LOG_DFLT;

module_param(event_log_level, long, S_IRUGO | S_IRUSR);
MODULE_PARM_DESC(event_log_level,
		 "Specifies the level of events to report to the system log.  Critical and warning level events are logged by default.");

/* A shared buffer to use for formatting messages. */
static char event_buffer[EVENT_LOG_BUFF_SIZE];

/* A lock to protect the shared buffer used for formatting messages. */
static DEFINE_SPINLOCK(event_buffer_lock);

/**
 * translates an esas2r-defined logging event level to a kernel logging level.
 *
 * @param [in] level the esas2r-defined logging event level to translate
 *
 * @return the corresponding kernel logging level.
 */
static const char *translate_esas2r_event_level_to_kernel(const long level)
{
	switch (level) {
	case ESAS2R_LOG_CRIT:
		return KERN_CRIT;

	case ESAS2R_LOG_WARN:
		return KERN_WARNING;

	case ESAS2R_LOG_INFO:
		return KERN_INFO;

	case ESAS2R_LOG_DEBG:
	case ESAS2R_LOG_TRCE:
	default:
		return KERN_DEBUG;
	}
}

/**
 * the master logging function.  this function will format the message as
 * outlined by the formatting string, the input device information and the
 * substitution arguments and output the resulting string to the system log.
 *
 * @param [in] level  the event log level of the message
 * @param [in] dev    the device information
 * @param [in] format the formatting string for the message
 * @param [in] args   the substition arguments to the formatting string
 *
 * @return 0 on success, or -1 if an error occurred.
 */
static int esas2r_log_master(const long level,
			     const struct device *dev,
			     const char *format,
			     va_list args)
{
	if (level <= event_log_level) {
		unsigned long flags = 0;
		int retval = 0;
		char *buffer = event_buffer;
		size_t buflen = EVENT_LOG_BUFF_SIZE;
		const char *fmt_nodev = "%s%s: ";
		const char *fmt_dev = "%s%s [%s, %s, %s]";
		const char *slevel =
			translate_esas2r_event_level_to_kernel(level);

		spin_lock_irqsave(&event_buffer_lock, flags);

		memset(buffer, 0, buflen);

		/*
		 * format the level onto the beginning of the string and do
		 * some pointer arithmetic to move the pointer to the point
		 * where the actual message can be inserted.
		 */

		if (dev == NULL) {
			snprintf(buffer, buflen, fmt_nodev, slevel,
				 ESAS2R_DRVR_NAME);
		} else {
			snprintf(buffer, buflen, fmt_dev, slevel,
				 ESAS2R_DRVR_NAME,
				 (dev->driver ? dev->driver->name : "unknown"),
				 (dev->bus ? dev->bus->name : "unknown"),
				 dev_name(dev));
		}

		buffer += strlen(event_buffer);
		buflen -= strlen(event_buffer);

		retval = vsnprintf(buffer, buflen, format, args);
		if (retval < 0) {
			spin_unlock_irqrestore(&event_buffer_lock, flags);
			return -1;
		}

		/*
		 * Put a line break at the end of the formatted string so that
		 * we don't wind up with run-on messages.
		 */
		printk("%s\n", event_buffer);

		spin_unlock_irqrestore(&event_buffer_lock, flags);
	}

	return 0;
}

/**
 * formats and logs a message to the system log.
 *
 * @param [in] level  the event level of the message
 * @param [in] format the formating string for the message
 * @param [in] ...    the substitution arguments to the formatting string
 *
 * @return 0 on success, or -1 if an error occurred.
 */
int esas2r_log(const long level, const char *format, ...)
{
	int retval = 0;
	va_list args;

	va_start(args, format);

	retval = esas2r_log_master(level, NULL, format, args);

	va_end(args);

	return retval;
}

/**
 * formats and logs a message to the system log.  this message will include
 * device information.
 *
 * @param [in] level   the event level of the message
 * @param [in] dev     the device information
 * @param [in] format  the formatting string for the message
 * @param [in] ...     the substitution arguments to the formatting string
 *
 * @return 0 on success, or -1 if an error occurred.
 */
int esas2r_log_dev(const long level,
		   const struct device *dev,
		   const char *format,
		   ...)
{
	int retval = 0;
	va_list args;

	va_start(args, format);

	retval = esas2r_log_master(level, dev, format, args);

	va_end(args);

	return retval;
}

/**
 * formats and logs a message to the system log.  this message will include
 * device information.
 *
 * @param [in] level   the event level of the message
 * @param [in] buf
 * @param [in] len
 *
 * @return 0 on success, or -1 if an error occurred.
 */
int esas2r_log_hexdump(const long level,
		       const void *buf,
		       size_t len)
{
	if (level <= event_log_level) {
		print_hex_dump(translate_esas2r_event_level_to_kernel(level),
			       "", DUMP_PREFIX_OFFSET, 16, 1, buf,
			       len, true);
	}

	return 1;
}