xref: /freebsd/sys/compat/linuxkpi/common/include/linux/printk.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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  * $FreeBSD$
30  */
31 #ifndef _LINUX_PRINTK_H_
32 #define	_LINUX_PRINTK_H_
33 
34 #include <linux/kernel.h>
35 
36 /* GID printing macros */
37 #define	GID_PRINT_FMT			"%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x"
38 #define	GID_PRINT_ARGS(gid_raw)		htons(((u16 *)gid_raw)[0]), htons(((u16 *)gid_raw)[1]),\
39 					htons(((u16 *)gid_raw)[2]), htons(((u16 *)gid_raw)[3]),\
40 					htons(((u16 *)gid_raw)[4]), htons(((u16 *)gid_raw)[5]),\
41 					htons(((u16 *)gid_raw)[6]), htons(((u16 *)gid_raw)[7])
42 
43 enum {
44 	DUMP_PREFIX_NONE,
45 	DUMP_PREFIX_ADDRESS,
46 	DUMP_PREFIX_OFFSET
47 };
48 
49 static inline void
50 print_hex_dump(const char *level, const char *prefix_str,
51     const int prefix_type, const int rowsize, const int groupsize,
52     const void *buf, size_t len, const bool ascii)
53 {
54 	typedef const struct { long long value; } __packed *print_64p_t;
55 	typedef const struct { uint32_t value; } __packed *print_32p_t;
56 	typedef const struct { uint16_t value; } __packed *print_16p_t;
57 	const void *buf_old = buf;
58 	int row;
59 
60 	while (len > 0) {
61 		if (level != NULL)
62 			printf("%s", level);
63 		if (prefix_str != NULL)
64 			printf("%s ", prefix_str);
65 
66 		switch (prefix_type) {
67 		case DUMP_PREFIX_ADDRESS:
68 			printf("[%p] ", buf);
69 			break;
70 		case DUMP_PREFIX_OFFSET:
71 			printf("[%#tx] ", ((const char *)buf -
72 			    (const char *)buf_old));
73 			break;
74 		default:
75 			break;
76 		}
77 		for (row = 0; row != rowsize; row++) {
78 			if (groupsize == 8 && len > 7) {
79 				printf("%016llx ", ((print_64p_t)buf)->value);
80 				buf = (const uint8_t *)buf + 8;
81 				len -= 8;
82 			} else if (groupsize == 4 && len > 3) {
83 				printf("%08x ", ((print_32p_t)buf)->value);
84 				buf = (const uint8_t *)buf + 4;
85 				len -= 4;
86 			} else if (groupsize == 2 && len > 1) {
87 				printf("%04x ", ((print_16p_t)buf)->value);
88 				buf = (const uint8_t *)buf + 2;
89 				len -= 2;
90 			} else if (len > 0) {
91 				printf("%02x ", *(const uint8_t *)buf);
92 				buf = (const uint8_t *)buf + 1;
93 				len--;
94 			} else {
95 				break;
96 			}
97 		}
98 		printf("\n");
99 	}
100 }
101 
102 static inline void
103 print_hex_dump_bytes(const char *prefix_str, const int prefix_type,
104     const void *buf, size_t len)
105 {
106 	print_hex_dump(NULL, prefix_str, prefix_type, 16, 1, buf, len, 0);
107 }
108 
109 #define	printk_ratelimit() ({			\
110 	static linux_ratelimit_t __ratelimited;	\
111 	linux_ratelimited(&__ratelimited);	\
112 })
113 
114 #define	printk_ratelimited(...) ({		\
115 	bool __retval = printk_ratelimit();	\
116 	if (__retval)				\
117 		printk(__VA_ARGS__);		\
118 	__retval;				\
119 })
120 
121 #define	pr_err_ratelimited(fmt, ...) \
122 	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
123 
124 #define	print_hex_dump_debug(...) \
125 	print_hex_dump(KERN_DEBUG, ##__VA_ARGS__)
126 
127 #define	pr_info_ratelimited(fmt, ...) \
128 	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
129 
130 #endif					/* _LINUX_PRINTK_H_ */
131