xref: /freebsd/sbin/nvmecontrol/nc_util.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1e505b7ecSWarner Losh /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3e96f6edaSWarner Losh  *
452467047SWarner Losh  * Copyright (c) 2017 Netflix, Inc.
5e505b7ecSWarner Losh  *
6e505b7ecSWarner Losh  * Redistribution and use in source and binary forms, with or without
7e505b7ecSWarner Losh  * modification, are permitted provided that the following conditions
8e505b7ecSWarner Losh  * are met:
9e505b7ecSWarner Losh  * 1. Redistributions of source code must retain the above copyright
10e505b7ecSWarner Losh  *    notice, this list of conditions and the following disclaimer.
11e505b7ecSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
12e505b7ecSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
13e505b7ecSWarner Losh  *    documentation and/or other materials provided with the distribution.
14e505b7ecSWarner Losh  *
15e505b7ecSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16e505b7ecSWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17e505b7ecSWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18e505b7ecSWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19e505b7ecSWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20e505b7ecSWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21e505b7ecSWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22e505b7ecSWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23e505b7ecSWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e505b7ecSWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e505b7ecSWarner Losh  * SUCH DAMAGE.
26e505b7ecSWarner Losh  */
27e505b7ecSWarner Losh 
28e505b7ecSWarner Losh #include <sys/cdefs.h>
29e505b7ecSWarner Losh #include <sys/endian.h>
30e505b7ecSWarner Losh #include "nvmecontrol.h"
31e505b7ecSWarner Losh 
32e505b7ecSWarner Losh char *
uint128_to_str(uint128_t u,char * buf,size_t buflen)33e505b7ecSWarner Losh uint128_to_str(uint128_t u, char *buf, size_t buflen)
34e505b7ecSWarner Losh {
35e505b7ecSWarner Losh 	char *end = buf + buflen - 1;
36e505b7ecSWarner Losh 
37e505b7ecSWarner Losh 	*end-- = '\0';
38e505b7ecSWarner Losh 	if (u == 0)
39e505b7ecSWarner Losh 		*end-- = '0';
40e505b7ecSWarner Losh 	while (u && end >= buf) {
41e505b7ecSWarner Losh 		*end-- = u % 10 + '0';
42e505b7ecSWarner Losh 		u /= 10;
43e505b7ecSWarner Losh 	}
44e505b7ecSWarner Losh 	end++;
45e505b7ecSWarner Losh 	if (u != 0)
46e505b7ecSWarner Losh 		return NULL;
47e505b7ecSWarner Losh 
48e505b7ecSWarner Losh 	return end;
49e505b7ecSWarner Losh }
50e505b7ecSWarner Losh 
51e505b7ecSWarner Losh /* "Missing" from endian.h */
52e505b7ecSWarner Losh uint64_t
le48dec(const void * pp)53e505b7ecSWarner Losh le48dec(const void *pp)
54e505b7ecSWarner Losh {
55e505b7ecSWarner Losh 	uint8_t const *p = (uint8_t const *)pp;
56e505b7ecSWarner Losh 
57e505b7ecSWarner Losh 	return (((uint64_t)le16dec(p + 4) << 32) | le32dec(p));
58e505b7ecSWarner Losh }
59