1278866Shselasky/*-
2278866Shselasky * Copyright (c) 2010 Isilon Systems, Inc.
3278866Shselasky * Copyright (c) 2010 iX Systems, Inc.
4278866Shselasky * Copyright (c) 2010 Panasas, Inc.
5328653Shselasky * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
6278866Shselasky * All rights reserved.
7278866Shselasky *
8278866Shselasky * Redistribution and use in source and binary forms, with or without
9278866Shselasky * modification, are permitted provided that the following conditions
10278866Shselasky * are met:
11278866Shselasky * 1. Redistributions of source code must retain the above copyright
12278866Shselasky *    notice unmodified, this list of conditions, and the following
13278866Shselasky *    disclaimer.
14278866Shselasky * 2. Redistributions in binary form must reproduce the above copyright
15278866Shselasky *    notice, this list of conditions and the following disclaimer in the
16278866Shselasky *    documentation and/or other materials provided with the distribution.
17278866Shselasky *
18278866Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19278866Shselasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20278866Shselasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21278866Shselasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22278866Shselasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23278866Shselasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24278866Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25278866Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26278866Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27278866Shselasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28289644Shselasky *
29289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/printk.h 347792 2019-05-16 17:02:31Z hselasky $
30278866Shselasky */
31328653Shselasky#ifndef _LINUX_PRINTK_H_
32328653Shselasky#define	_LINUX_PRINTK_H_
33278866Shselasky
34328653Shselasky#include <linux/kernel.h>
35328653Shselasky
36278866Shselasky/* GID printing macros */
37278866Shselasky#define	GID_PRINT_FMT			"%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x"
38278866Shselasky#define	GID_PRINT_ARGS(gid_raw)		htons(((u16 *)gid_raw)[0]), htons(((u16 *)gid_raw)[1]),\
39278866Shselasky					htons(((u16 *)gid_raw)[2]), htons(((u16 *)gid_raw)[3]),\
40278866Shselasky					htons(((u16 *)gid_raw)[4]), htons(((u16 *)gid_raw)[5]),\
41278866Shselasky					htons(((u16 *)gid_raw)[6]), htons(((u16 *)gid_raw)[7])
42278866Shselasky
43328653Shselaskyenum {
44328653Shselasky	DUMP_PREFIX_NONE,
45328653Shselasky	DUMP_PREFIX_ADDRESS,
46328653Shselasky	DUMP_PREFIX_OFFSET
47328653Shselasky};
48328653Shselasky
49328653Shselaskystatic inline void
50328653Shselaskyprint_hex_dump(const char *level, const char *prefix_str,
51328653Shselasky    const int prefix_type, const int rowsize, const int groupsize,
52328653Shselasky    const void *buf, size_t len, const bool ascii)
53328653Shselasky{
54328653Shselasky	typedef const struct { long long value; } __packed *print_64p_t;
55328653Shselasky	typedef const struct { uint32_t value; } __packed *print_32p_t;
56328653Shselasky	typedef const struct { uint16_t value; } __packed *print_16p_t;
57328653Shselasky	const void *buf_old = buf;
58328653Shselasky	int row;
59328653Shselasky
60328653Shselasky	while (len > 0) {
61328653Shselasky		if (level != NULL)
62328653Shselasky			printf("%s", level);
63328653Shselasky		if (prefix_str != NULL)
64328653Shselasky			printf("%s ", prefix_str);
65328653Shselasky
66328653Shselasky		switch (prefix_type) {
67328653Shselasky		case DUMP_PREFIX_ADDRESS:
68328653Shselasky			printf("[%p] ", buf);
69328653Shselasky			break;
70328653Shselasky		case DUMP_PREFIX_OFFSET:
71328653Shselasky			printf("[%p] ", (const char *)((const char *)buf -
72328653Shselasky			    (const char *)buf_old));
73328653Shselasky			break;
74328653Shselasky		default:
75328653Shselasky			break;
76328653Shselasky		}
77328653Shselasky		for (row = 0; row != rowsize; row++) {
78328653Shselasky			if (groupsize == 8 && len > 7) {
79328653Shselasky				printf("%016llx ", ((print_64p_t)buf)->value);
80328653Shselasky				buf = (const uint8_t *)buf + 8;
81328653Shselasky				len -= 8;
82328653Shselasky			} else if (groupsize == 4 && len > 3) {
83328653Shselasky				printf("%08x ", ((print_32p_t)buf)->value);
84328653Shselasky				buf = (const uint8_t *)buf + 4;
85328653Shselasky				len -= 4;
86328653Shselasky			} else if (groupsize == 2 && len > 1) {
87328653Shselasky				printf("%04x ", ((print_16p_t)buf)->value);
88328653Shselasky				buf = (const uint8_t *)buf + 2;
89328653Shselasky				len -= 2;
90328653Shselasky			} else if (len > 0) {
91328653Shselasky				printf("%02x ", *(const uint8_t *)buf);
92328653Shselasky				buf = (const uint8_t *)buf + 1;
93328653Shselasky				len--;
94328653Shselasky			} else {
95328653Shselasky				break;
96328653Shselasky			}
97328653Shselasky		}
98328653Shselasky		printf("\n");
99328653Shselasky	}
100328653Shselasky}
101328653Shselasky
102328653Shselaskystatic inline void
103328653Shselaskyprint_hex_dump_bytes(const char *prefix_str, const int prefix_type,
104328653Shselasky    const void *buf, size_t len)
105328653Shselasky{
106328653Shselasky	print_hex_dump(NULL, prefix_str, prefix_type, 16, 1, buf, len, 0);
107328653Shselasky}
108328653Shselasky
109329962Shselasky#define	printk_ratelimit() ({			\
110328653Shselasky	static linux_ratelimit_t __ratelimited;	\
111329962Shselasky	linux_ratelimited(&__ratelimited);	\
112329962Shselasky})
113329962Shselasky
114329962Shselasky#define	printk_ratelimited(...) ({		\
115329962Shselasky	bool __retval = printk_ratelimit();	\
116329962Shselasky	if (__retval)				\
117328653Shselasky		printk(__VA_ARGS__);		\
118329962Shselasky	__retval;				\
119329962Shselasky})
120328653Shselasky
121330860Shselasky#define	pr_err_ratelimited(fmt, ...) \
122330860Shselasky	printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
123330860Shselasky
124347792Shselasky#define	print_hex_dump_debug(...) \
125347792Shselasky	print_hex_dump(KERN_DEBUG, ##__VA_ARGS__)
126347792Shselasky
127345936Shselasky#define	pr_info_ratelimited(fmt, ...) \
128345936Shselasky	printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
129345936Shselasky
130328653Shselasky#endif					/* _LINUX_PRINTK_H_ */
131