xref: /freebsd/sys/compat/linuxkpi/common/include/linux/printk.h (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef _LINUXKPI_LINUX_PRINTK_H_
30 #define	_LINUXKPI_LINUX_PRINTK_H_
31 
32 #include <linux/kernel.h>
33 
34 /* GID printing macros */
35 #define	GID_PRINT_FMT			"%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x"
36 #define	GID_PRINT_ARGS(gid_raw)		htons(((u16 *)gid_raw)[0]), htons(((u16 *)gid_raw)[1]),\
37 					htons(((u16 *)gid_raw)[2]), htons(((u16 *)gid_raw)[3]),\
38 					htons(((u16 *)gid_raw)[4]), htons(((u16 *)gid_raw)[5]),\
39 					htons(((u16 *)gid_raw)[6]), htons(((u16 *)gid_raw)[7])
40 
41 enum {
42 	DUMP_PREFIX_NONE,
43 	DUMP_PREFIX_ADDRESS,
44 	DUMP_PREFIX_OFFSET
45 };
46 
47 static inline void
48 print_hex_dump(const char *level, const char *prefix_str,
49     const int prefix_type, const int rowsize, const int groupsize,
50     const void *buf, size_t len, const bool ascii)
51 {
52 	typedef const struct { long long value; } __packed *print_64p_t;
53 	typedef const struct { uint32_t value; } __packed *print_32p_t;
54 	typedef const struct { uint16_t value; } __packed *print_16p_t;
55 	const void *buf_old = buf;
56 	int row;
57 
58 	while (len > 0) {
59 		if (level != NULL)
60 			printf("%s", level);
61 		if (prefix_str != NULL)
62 			printf("%s ", prefix_str);
63 
64 		switch (prefix_type) {
65 		case DUMP_PREFIX_ADDRESS:
66 			printf("[%p] ", buf);
67 			break;
68 		case DUMP_PREFIX_OFFSET:
69 			printf("[%#tx] ", ((const char *)buf -
70 			    (const char *)buf_old));
71 			break;
72 		default:
73 			break;
74 		}
75 		for (row = 0; row != rowsize; row++) {
76 			if (groupsize == 8 && len > 7) {
77 				printf("%016llx ", ((print_64p_t)buf)->value);
78 				buf = (const uint8_t *)buf + 8;
79 				len -= 8;
80 			} else if (groupsize == 4 && len > 3) {
81 				printf("%08x ", ((print_32p_t)buf)->value);
82 				buf = (const uint8_t *)buf + 4;
83 				len -= 4;
84 			} else if (groupsize == 2 && len > 1) {
85 				printf("%04x ", ((print_16p_t)buf)->value);
86 				buf = (const uint8_t *)buf + 2;
87 				len -= 2;
88 			} else if (len > 0) {
89 				printf("%02x ", *(const uint8_t *)buf);
90 				buf = (const uint8_t *)buf + 1;
91 				len--;
92 			} else {
93 				break;
94 			}
95 		}
96 		printf("\n");
97 	}
98 }
99 
100 static inline void
101 print_hex_dump_bytes(const char *prefix_str, const int prefix_type,
102     const void *buf, size_t len)
103 {
104 	print_hex_dump(NULL, prefix_str, prefix_type, 16, 1, buf, len, 0);
105 }
106 
107 #define	printk_ratelimit() ({			\
108 	static linux_ratelimit_t __ratelimited;	\
109 	linux_ratelimited(&__ratelimited);	\
110 })
111 
112 #define	printk_ratelimited(...) ({		\
113 	bool __retval = printk_ratelimit();	\
114 	if (__retval)				\
115 		printk(__VA_ARGS__);		\
116 	__retval;				\
117 })
118 
119 #define	pr_err_ratelimited(fmt, ...) \
120 	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
121 
122 #define	print_hex_dump_debug(...) \
123 	print_hex_dump(KERN_DEBUG, ##__VA_ARGS__)
124 
125 #define	pr_info_ratelimited(fmt, ...) \
126 	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
127 
128 #endif					/* _LINUXKPI_LINUX_PRINTK_H_ */
129