esas2r_log.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. memset(buffer, 0, buflen);
  120. /*
  121. * format the level onto the beginning of the string and do
  122. * some pointer arithmetic to move the pointer to the point
  123. * where the actual message can be inserted.
  124. */
  125. if (dev == NULL) {
  126. snprintf(buffer, buflen, fmt_nodev, slevel,
  127. ESAS2R_DRVR_NAME);
  128. } else {
  129. snprintf(buffer, buflen, fmt_dev, slevel,
  130. ESAS2R_DRVR_NAME,
  131. (dev->driver ? dev->driver->name : "unknown"),
  132. (dev->bus ? dev->bus->name : "unknown"),
  133. dev_name(dev));
  134. }
  135. buffer += strlen(event_buffer);
  136. buflen -= strlen(event_buffer);
  137. retval = vsnprintf(buffer, buflen, format, args);
  138. if (retval < 0) {
  139. spin_unlock_irqrestore(&event_buffer_lock, flags);
  140. return -1;
  141. }
  142. /*
  143. * Put a line break at the end of the formatted string so that
  144. * we don't wind up with run-on messages.
  145. */
  146. printk("%s\n", event_buffer);
  147. spin_unlock_irqrestore(&event_buffer_lock, flags);
  148. }
  149. return 0;
  150. }
  151. /**
  152. * formats and logs a message to the system log.
  153. *
  154. * @param [in] level the event level of the message
  155. * @param [in] format the formating string for the message
  156. * @param [in] ... the substitution arguments to the formatting string
  157. *
  158. * @return 0 on success, or -1 if an error occurred.
  159. */
  160. int esas2r_log(const long level, const char *format, ...)
  161. {
  162. int retval = 0;
  163. va_list args;
  164. va_start(args, format);
  165. retval = esas2r_log_master(level, NULL, format, args);
  166. va_end(args);
  167. return retval;
  168. }
  169. /**
  170. * formats and logs a message to the system log. this message will include
  171. * device information.
  172. *
  173. * @param [in] level the event level of the message
  174. * @param [in] dev the device information
  175. * @param [in] format the formatting string for the message
  176. * @param [in] ... the substitution arguments to the formatting string
  177. *
  178. * @return 0 on success, or -1 if an error occurred.
  179. */
  180. int esas2r_log_dev(const long level,
  181. const struct device *dev,
  182. const char *format,
  183. ...)
  184. {
  185. int retval = 0;
  186. va_list args;
  187. va_start(args, format);
  188. retval = esas2r_log_master(level, dev, format, args);
  189. va_end(args);
  190. return retval;
  191. }
  192. /**
  193. * formats and logs a message to the system log. this message will include
  194. * device information.
  195. *
  196. * @param [in] level the event level of the message
  197. * @param [in] buf
  198. * @param [in] len
  199. *
  200. * @return 0 on success, or -1 if an error occurred.
  201. */
  202. int esas2r_log_hexdump(const long level,
  203. const void *buf,
  204. size_t len)
  205. {
  206. if (level <= event_log_level) {
  207. print_hex_dump(translate_esas2r_event_level_to_kernel(level),
  208. "", DUMP_PREFIX_OFFSET, 16, 1, buf,
  209. len, true);
  210. }
  211. return 1;
  212. }