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