xref: /freebsd/sys/compat/linuxkpi/common/include/linux/printk.h (revision c2676069cb28588e8da6ddf525bc34175ebbeb27)
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.
5*c2676069SHans 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  */
31*c2676069SHans Petter Selasky #ifndef _LINUX_PRINTK_H_
32*c2676069SHans Petter Selasky #define	_LINUX_PRINTK_H_
33*c2676069SHans Petter Selasky 
34*c2676069SHans 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 
43*c2676069SHans Petter Selasky enum {
44*c2676069SHans Petter Selasky 	DUMP_PREFIX_NONE,
45*c2676069SHans Petter Selasky 	DUMP_PREFIX_ADDRESS,
46*c2676069SHans Petter Selasky 	DUMP_PREFIX_OFFSET
47*c2676069SHans Petter Selasky };
48*c2676069SHans Petter Selasky 
49*c2676069SHans Petter Selasky static inline void
50*c2676069SHans Petter Selasky print_hex_dump(const char *level, const char *prefix_str,
51*c2676069SHans Petter Selasky     const int prefix_type, const int rowsize, const int groupsize,
52*c2676069SHans Petter Selasky     const void *buf, size_t len, const bool ascii)
53*c2676069SHans Petter Selasky {
54*c2676069SHans Petter Selasky 	typedef const struct { long long value; } __packed *print_64p_t;
55*c2676069SHans Petter Selasky 	typedef const struct { uint32_t value; } __packed *print_32p_t;
56*c2676069SHans Petter Selasky 	typedef const struct { uint16_t value; } __packed *print_16p_t;
57*c2676069SHans Petter Selasky 	const void *buf_old = buf;
58*c2676069SHans Petter Selasky 	int row;
59*c2676069SHans Petter Selasky 
60*c2676069SHans Petter Selasky 	while (len > 0) {
61*c2676069SHans Petter Selasky 		if (level != NULL)
62*c2676069SHans Petter Selasky 			printf("%s", level);
63*c2676069SHans Petter Selasky 		if (prefix_str != NULL)
64*c2676069SHans Petter Selasky 			printf("%s ", prefix_str);
65*c2676069SHans Petter Selasky 
66*c2676069SHans Petter Selasky 		switch (prefix_type) {
67*c2676069SHans Petter Selasky 		case DUMP_PREFIX_ADDRESS:
68*c2676069SHans Petter Selasky 			printf("[%p] ", buf);
69*c2676069SHans Petter Selasky 			break;
70*c2676069SHans Petter Selasky 		case DUMP_PREFIX_OFFSET:
71*c2676069SHans Petter Selasky 			printf("[%p] ", (const char *)((const char *)buf -
72*c2676069SHans Petter Selasky 			    (const char *)buf_old));
73*c2676069SHans Petter Selasky 			break;
74*c2676069SHans Petter Selasky 		default:
75*c2676069SHans Petter Selasky 			break;
76*c2676069SHans Petter Selasky 		}
77*c2676069SHans Petter Selasky 		for (row = 0; row != rowsize; row++) {
78*c2676069SHans Petter Selasky 			if (groupsize == 8 && len > 7) {
79*c2676069SHans Petter Selasky 				printf("%016llx ", ((print_64p_t)buf)->value);
80*c2676069SHans Petter Selasky 				buf = (const uint8_t *)buf + 8;
81*c2676069SHans Petter Selasky 				len -= 8;
82*c2676069SHans Petter Selasky 			} else if (groupsize == 4 && len > 3) {
83*c2676069SHans Petter Selasky 				printf("%08x ", ((print_32p_t)buf)->value);
84*c2676069SHans Petter Selasky 				buf = (const uint8_t *)buf + 4;
85*c2676069SHans Petter Selasky 				len -= 4;
86*c2676069SHans Petter Selasky 			} else if (groupsize == 2 && len > 1) {
87*c2676069SHans Petter Selasky 				printf("%04x ", ((print_16p_t)buf)->value);
88*c2676069SHans Petter Selasky 				buf = (const uint8_t *)buf + 2;
89*c2676069SHans Petter Selasky 				len -= 2;
90*c2676069SHans Petter Selasky 			} else if (len > 0) {
91*c2676069SHans Petter Selasky 				printf("%02x ", *(const uint8_t *)buf);
92*c2676069SHans Petter Selasky 				buf = (const uint8_t *)buf + 1;
93*c2676069SHans Petter Selasky 				len--;
94*c2676069SHans Petter Selasky 			} else {
95*c2676069SHans Petter Selasky 				break;
96*c2676069SHans Petter Selasky 			}
97*c2676069SHans Petter Selasky 		}
98*c2676069SHans Petter Selasky 		printf("\n");
99*c2676069SHans Petter Selasky 	}
100*c2676069SHans Petter Selasky }
101*c2676069SHans Petter Selasky 
102*c2676069SHans Petter Selasky static inline void
103*c2676069SHans Petter Selasky print_hex_dump_bytes(const char *prefix_str, const int prefix_type,
104*c2676069SHans Petter Selasky     const void *buf, size_t len)
105*c2676069SHans Petter Selasky {
106*c2676069SHans Petter Selasky 	print_hex_dump(NULL, prefix_str, prefix_type, 16, 1, buf, len, 0);
107*c2676069SHans Petter Selasky }
108*c2676069SHans Petter Selasky 
109*c2676069SHans Petter Selasky #define	printk_ratelimited(...) do {		\
110*c2676069SHans Petter Selasky 	static linux_ratelimit_t __ratelimited;	\
111*c2676069SHans Petter Selasky 	if (linux_ratelimited(&__ratelimited))	\
112*c2676069SHans Petter Selasky 		printk(__VA_ARGS__);		\
113*c2676069SHans Petter Selasky } while (0)
114*c2676069SHans Petter Selasky 
115*c2676069SHans Petter Selasky #endif					/* _LINUX_PRINTK_H_ */
116