esas2r_log.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * linux/drivers/scsi/esas2r/esas2r_log.c
  3. * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
  4. *
  5. * Copyright (c) 2001-2013 ATTO Technology, Inc.
  6. * (mailto:linuxdrivers@attotech.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. *
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include "esas2r.h"
  44. /*
  45. * this module within the driver is tasked with providing logging functionality.
  46. * the event_log_level module parameter controls the level of messages that are
  47. * written to the system log. the default level of messages that are written
  48. * are critical and warning messages. if other types of messages are desired,
  49. * one simply needs to load the module with the correct value for the
  50. * event_log_level module parameter. for example:
  51. *
  52. * insmod <module> event_log_level=1
  53. *
  54. * will load the module and only critical events will be written by this module
  55. * to the system log. if critical, warning, and information-level messages are
  56. * desired, the correct value for the event_log_level module parameter
  57. * would be as follows:
  58. *
  59. * insmod <module> event_log_level=3
  60. */
  61. #define EVENT_LOG_BUFF_SIZE 1024
  62. static long event_log_level = ESAS2R_LOG_DFLT;
  63. module_param(event_log_level, long, S_IRUGO | S_IRUSR);
  64. MODULE_PARM_DESC(event_log_level,
  65. "Specifies the level of events to report to the system log. Critical and warning level events are logged by default.");
  66. /* A shared buffer to use for formatting messages. */
  67. static char event_buffer[EVENT_LOG_BUFF_SIZE];
  68. /* A lock to protect the shared buffer used for formatting messages. */
  69. static DEFINE_SPINLOCK(event_buffer_lock);
  70. /**
  71. * translates an esas2r-defined logging event level to a kernel logging level.
  72. *
  73. * @param [in] level the esas2r-defined logging event level to translate
  74. *
  75. * @return the corresponding kernel logging level.
  76. */
  77. static const char *translate_esas2r_event_level_to_kernel(const long level)
  78. {
  79. switch (level) {
  80. case ESAS2R_LOG_CRIT:
  81. return KERN_CRIT;
  82. case ESAS2R_LOG_WARN:
  83. return KERN_WARNING;
  84. case ESAS2R_LOG_INFO:
  85. return KERN_INFO;
  86. case ESAS2R_LOG_DEBG:
  87. case ESAS2R_LOG_TRCE:
  88. default:
  89. return KERN_DEBUG;
  90. }
  91. }
  92. /**
  93. * the master logging function. this function will format the message as
  94. * outlined by the formatting string, the input device information and the
  95. * substitution arguments and output the resulting string to the system log.
  96. *
  97. * @param [in] level the event log level of the message
  98. * @param [in] dev the device information
  99. * @param [in] format the formatting string for the message
  100. * @param [in] args the substition arguments to the formatting string
  101. *
  102. * @return 0 on success, or -1 if an error occurred.
  103. */
  104. static int esas2r_log_master(const long level,
  105. const struct device *dev,
  106. const char *format,
  107. va_list args)
  108. {
  109. if (level <= event_log_level) {
  110. unsigned long flags = 0;
  111. int retval = 0;
  112. char *buffer = event_buffer;
  113. size_t buflen = EVENT_LOG_BUFF_SIZE;
  114. const char *fmt_nodev = "%s%s: ";
  115. const char *fmt_dev = "%s%s [%s, %s, %s]";
  116. const char *slevel =
  117. translate_esas2r_event_level_to_kernel(level);
  118. spin_lock_irqsave(&event_buffer_lock, flags);
  119. if (buffer == NULL) {
  120. spin_unlock_irqrestore(&event_buffer_lock, flags);
  121. return -1;
  122. }
  123. memset(buffer, 0, buflen);
  124. /*
  125. * format the level onto the beginning of the string and do
  126. * some pointer arithmetic to move the pointer to the point
  127. * where the actual message can be inserted.
  128. */
  129. if (dev == NULL) {
  130. snprintf(buffer, buflen, fmt_nodev, slevel,
  131. ESAS2R_DRVR_NAME);
  132. } else {
  133. snprintf(buffer, buflen, fmt_dev, slevel,
  134. ESAS2R_DRVR_NAME,
  135. (dev->driver ? dev->driver->name : "unknown"),
  136. (dev->bus ? dev->bus->name : "unknown"),
  137. dev_name(dev));
  138. }
  139. buffer += strlen(event_buffer);
  140. buflen -= strlen(event_buffer);
  141. retval = vsnprintf(buffer, buflen, format, args);
  142. if (retval < 0) {
  143. spin_unlock_irqrestore(&event_buffer_lock, flags);
  144. return -1;
  145. }
  146. /*
  147. * Put a line break at the end of the formatted string so that
  148. * we don't wind up with run-on messages.
  149. */
  150. printk("%s\n", event_buffer);
  151. spin_unlock_irqrestore(&event_buffer_lock, flags);
  152. }
  153. return 0;
  154. }
  155. /**
  156. * formats and logs a message to the system log.
  157. *
  158. * @param [in] level the event level of the message
  159. * @param [in] format the formating string for the message
  160. * @param [in] ... the substitution arguments to the formatting string
  161. *
  162. * @return 0 on success, or -1 if an error occurred.
  163. */
  164. int esas2r_log(const long level, const char *format, ...)
  165. {
  166. int retval = 0;
  167. va_list args;
  168. va_start(args, format);
  169. retval = esas2r_log_master(level, NULL, format, args);
  170. va_end(args);
  171. return retval;
  172. }
  173. /**
  174. * formats and logs a message to the system log. this message will include
  175. * device information.
  176. *
  177. * @param [in] level the event level of the message
  178. * @param [in] dev the device information
  179. * @param [in] format the formatting string for the message
  180. * @param [in] ... the substitution arguments to the formatting string
  181. *
  182. * @return 0 on success, or -1 if an error occurred.
  183. */
  184. int esas2r_log_dev(const long level,
  185. const struct device *dev,
  186. const char *format,
  187. ...)
  188. {
  189. int retval = 0;
  190. va_list args;
  191. va_start(args, format);
  192. retval = esas2r_log_master(level, dev, format, args);
  193. va_end(args);
  194. return retval;
  195. }
  196. /**
  197. * formats and logs a message to the system log. this message will include
  198. * device information.
  199. *
  200. * @param [in] level the event level of the message
  201. * @param [in] buf
  202. * @param [in] len
  203. *
  204. * @return 0 on success, or -1 if an error occurred.
  205. */
  206. int esas2r_log_hexdump(const long level,
  207. const void *buf,
  208. size_t len)
  209. {
  210. if (level <= event_log_level) {
  211. print_hex_dump(translate_esas2r_event_level_to_kernel(level),
  212. "", DUMP_PREFIX_OFFSET, 16, 1, buf,
  213. len, true);
  214. }
  215. return 1;
  216. }