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  */
29307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_PRINTK_H_
30307f78f3SVladimir Kondratyev #define	_LINUXKPI_LINUX_PRINTK_H_
31c2676069SHans Petter Selasky 
32c2676069SHans Petter Selasky #include <linux/kernel.h>
338d59ecb2SHans Petter Selasky 
348d59ecb2SHans Petter Selasky /* GID printing macros */
358d59ecb2SHans Petter Selasky #define	GID_PRINT_FMT			"%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x"
368d59ecb2SHans Petter Selasky #define	GID_PRINT_ARGS(gid_raw)		htons(((u16 *)gid_raw)[0]), htons(((u16 *)gid_raw)[1]),\
378d59ecb2SHans Petter Selasky 					htons(((u16 *)gid_raw)[2]), htons(((u16 *)gid_raw)[3]),\
388d59ecb2SHans Petter Selasky 					htons(((u16 *)gid_raw)[4]), htons(((u16 *)gid_raw)[5]),\
398d59ecb2SHans Petter Selasky 					htons(((u16 *)gid_raw)[6]), htons(((u16 *)gid_raw)[7])
408d59ecb2SHans Petter Selasky 
41c2676069SHans Petter Selasky enum {
42c2676069SHans Petter Selasky 	DUMP_PREFIX_NONE,
43c2676069SHans Petter Selasky 	DUMP_PREFIX_ADDRESS,
44c2676069SHans Petter Selasky 	DUMP_PREFIX_OFFSET
45c2676069SHans Petter Selasky };
46c2676069SHans Petter Selasky 
47c2676069SHans Petter Selasky static inline void
print_hex_dump(const char * level,const char * prefix_str,const int prefix_type,const int rowsize,const int groupsize,const void * buf,size_t len,const bool ascii)48c2676069SHans Petter Selasky print_hex_dump(const char *level, const char *prefix_str,
49c2676069SHans Petter Selasky     const int prefix_type, const int rowsize, const int groupsize,
50c2676069SHans Petter Selasky     const void *buf, size_t len, const bool ascii)
51c2676069SHans Petter Selasky {
52c2676069SHans Petter Selasky 	typedef const struct { long long value; } __packed *print_64p_t;
53c2676069SHans Petter Selasky 	typedef const struct { uint32_t value; } __packed *print_32p_t;
54c2676069SHans Petter Selasky 	typedef const struct { uint16_t value; } __packed *print_16p_t;
55c2676069SHans Petter Selasky 	const void *buf_old = buf;
56c2676069SHans Petter Selasky 	int row;
57c2676069SHans Petter Selasky 
58c2676069SHans Petter Selasky 	while (len > 0) {
59c2676069SHans Petter Selasky 		if (level != NULL)
60c2676069SHans Petter Selasky 			printf("%s", level);
61c2676069SHans Petter Selasky 		if (prefix_str != NULL)
62c2676069SHans Petter Selasky 			printf("%s ", prefix_str);
63c2676069SHans Petter Selasky 
64c2676069SHans Petter Selasky 		switch (prefix_type) {
65c2676069SHans Petter Selasky 		case DUMP_PREFIX_ADDRESS:
66c2676069SHans Petter Selasky 			printf("[%p] ", buf);
67c2676069SHans Petter Selasky 			break;
68c2676069SHans Petter Selasky 		case DUMP_PREFIX_OFFSET:
69ce8395ecSJohn Baldwin 			printf("[%#tx] ", ((const char *)buf -
70c2676069SHans Petter Selasky 			    (const char *)buf_old));
71c2676069SHans Petter Selasky 			break;
72c2676069SHans Petter Selasky 		default:
73c2676069SHans Petter Selasky 			break;
74c2676069SHans Petter Selasky 		}
75c2676069SHans Petter Selasky 		for (row = 0; row != rowsize; row++) {
76c2676069SHans Petter Selasky 			if (groupsize == 8 && len > 7) {
77c2676069SHans Petter Selasky 				printf("%016llx ", ((print_64p_t)buf)->value);
78c2676069SHans Petter Selasky 				buf = (const uint8_t *)buf + 8;
79c2676069SHans Petter Selasky 				len -= 8;
80c2676069SHans Petter Selasky 			} else if (groupsize == 4 && len > 3) {
81c2676069SHans Petter Selasky 				printf("%08x ", ((print_32p_t)buf)->value);
82c2676069SHans Petter Selasky 				buf = (const uint8_t *)buf + 4;
83c2676069SHans Petter Selasky 				len -= 4;
84c2676069SHans Petter Selasky 			} else if (groupsize == 2 && len > 1) {
85c2676069SHans Petter Selasky 				printf("%04x ", ((print_16p_t)buf)->value);
86c2676069SHans Petter Selasky 				buf = (const uint8_t *)buf + 2;
87c2676069SHans Petter Selasky 				len -= 2;
88c2676069SHans Petter Selasky 			} else if (len > 0) {
89c2676069SHans Petter Selasky 				printf("%02x ", *(const uint8_t *)buf);
90c2676069SHans Petter Selasky 				buf = (const uint8_t *)buf + 1;
91c2676069SHans Petter Selasky 				len--;
92c2676069SHans Petter Selasky 			} else {
93c2676069SHans Petter Selasky 				break;
94c2676069SHans Petter Selasky 			}
95c2676069SHans Petter Selasky 		}
96c2676069SHans Petter Selasky 		printf("\n");
97c2676069SHans Petter Selasky 	}
98c2676069SHans Petter Selasky }
99c2676069SHans Petter Selasky 
100c2676069SHans Petter Selasky static inline void
print_hex_dump_bytes(const char * prefix_str,const int prefix_type,const void * buf,size_t len)101c2676069SHans Petter Selasky print_hex_dump_bytes(const char *prefix_str, const int prefix_type,
102c2676069SHans Petter Selasky     const void *buf, size_t len)
103c2676069SHans Petter Selasky {
104c2676069SHans Petter Selasky 	print_hex_dump(NULL, prefix_str, prefix_type, 16, 1, buf, len, 0);
105c2676069SHans Petter Selasky }
106c2676069SHans Petter Selasky 
10703f8ddedSHans Petter Selasky #define	printk_ratelimit() ({			\
108c2676069SHans Petter Selasky 	static linux_ratelimit_t __ratelimited;	\
10903f8ddedSHans Petter Selasky 	linux_ratelimited(&__ratelimited);	\
11003f8ddedSHans Petter Selasky })
11103f8ddedSHans Petter Selasky 
11203f8ddedSHans Petter Selasky #define	printk_ratelimited(...) ({		\
11303f8ddedSHans Petter Selasky 	bool __retval = printk_ratelimit();	\
11403f8ddedSHans Petter Selasky 	if (__retval)				\
115c2676069SHans Petter Selasky 		printk(__VA_ARGS__);		\
11603f8ddedSHans Petter Selasky 	__retval;				\
11703f8ddedSHans Petter Selasky })
118c2676069SHans Petter Selasky 
119ab7da720SHans Petter Selasky #define	pr_err_ratelimited(fmt, ...) \
120ab7da720SHans Petter Selasky 	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
121ab7da720SHans Petter Selasky 
12234cb771eSHans Petter Selasky #define	print_hex_dump_debug(...) \
12334cb771eSHans Petter Selasky 	print_hex_dump(KERN_DEBUG, ##__VA_ARGS__)
12434cb771eSHans Petter Selasky 
125c4698825SHans Petter Selasky #define	pr_info_ratelimited(fmt, ...) \
126c4698825SHans Petter Selasky 	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
127c4698825SHans Petter Selasky 
128307f78f3SVladimir Kondratyev #endif					/* _LINUXKPI_LINUX_PRINTK_H_ */
129