Module Name:    src
Committed By:   nonaka
Date:           Wed Apr 18 10:11:45 UTC 2018

Modified Files:
        src/sbin/nvmectl: devlist.c firmware.c identify.c logpage.c nvme.h
            nvmectl.c perftest.c power.c util.c wdc.c
        src/sys/dev/ic: ld_nvme.c nvme.c nvmeio.h

Log Message:
nvmectl(8): Add big-endian support.

from FreeBSD nvmecontolr(8) r329824.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sbin/nvmectl/devlist.c \
    src/sbin/nvmectl/identify.c src/sbin/nvmectl/perftest.c
cvs rdiff -u -r1.3 -r1.4 src/sbin/nvmectl/firmware.c src/sbin/nvmectl/power.c \
    src/sbin/nvmectl/wdc.c
cvs rdiff -u -r1.6 -r1.7 src/sbin/nvmectl/logpage.c \
    src/sbin/nvmectl/nvmectl.c
cvs rdiff -u -r1.2 -r1.3 src/sbin/nvmectl/nvme.h
cvs rdiff -u -r1.1 -r1.2 src/sbin/nvmectl/util.c
cvs rdiff -u -r1.19 -r1.20 src/sys/dev/ic/ld_nvme.c
cvs rdiff -u -r1.38 -r1.39 src/sys/dev/ic/nvme.c
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/ic/nvmeio.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/nvmectl/devlist.c
diff -u src/sbin/nvmectl/devlist.c:1.4 src/sbin/nvmectl/devlist.c:1.5
--- src/sbin/nvmectl/devlist.c:1.4	Tue Apr 17 08:54:35 2018
+++ src/sbin/nvmectl/devlist.c	Wed Apr 18 10:11:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: devlist.c,v 1.4 2018/04/17 08:54:35 nonaka Exp $	*/
+/*	$NetBSD: devlist.c,v 1.5 2018/04/18 10:11:44 nonaka Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -30,9 +30,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: devlist.c,v 1.4 2018/04/17 08:54:35 nonaka Exp $");
+__RCSID("$NetBSD: devlist.c,v 1.5 2018/04/18 10:11:44 nonaka Exp $");
 #if 0
-__FBSDID("$FreeBSD: head/sbin/nvmecontrol/devlist.c 326276 2017-11-27 15:37:16Z pfg $");
+__FBSDID("$FreeBSD: head/sbin/nvmecontrol/devlist.c 329824 2018-02-22 13:32:31Z wma $");
 #endif
 #endif
 
Index: src/sbin/nvmectl/identify.c
diff -u src/sbin/nvmectl/identify.c:1.4 src/sbin/nvmectl/identify.c:1.5
--- src/sbin/nvmectl/identify.c:1.4	Tue Apr 17 08:54:35 2018
+++ src/sbin/nvmectl/identify.c	Wed Apr 18 10:11:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: identify.c,v 1.4 2018/04/17 08:54:35 nonaka Exp $	*/
+/*	$NetBSD: identify.c,v 1.5 2018/04/18 10:11:44 nonaka Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -30,9 +30,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: identify.c,v 1.4 2018/04/17 08:54:35 nonaka Exp $");
+__RCSID("$NetBSD: identify.c,v 1.5 2018/04/18 10:11:44 nonaka Exp $");
 #if 0
-__FBSDID("$FreeBSD: head/sbin/nvmecontrol/identify.c 326276 2017-11-27 15:37:16Z pfg $");
+__FBSDID("$FreeBSD: head/sbin/nvmecontrol/identify.c 329824 2018-02-22 13:32:31Z wma $");
 #endif
 #endif
 
Index: src/sbin/nvmectl/perftest.c
diff -u src/sbin/nvmectl/perftest.c:1.4 src/sbin/nvmectl/perftest.c:1.5
--- src/sbin/nvmectl/perftest.c:1.4	Tue Apr 17 08:54:35 2018
+++ src/sbin/nvmectl/perftest.c	Wed Apr 18 10:11:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: perftest.c,v 1.4 2018/04/17 08:54:35 nonaka Exp $	*/
+/*	$NetBSD: perftest.c,v 1.5 2018/04/18 10:11:44 nonaka Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -30,9 +30,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: perftest.c,v 1.4 2018/04/17 08:54:35 nonaka Exp $");
+__RCSID("$NetBSD: perftest.c,v 1.5 2018/04/18 10:11:44 nonaka Exp $");
 #if 0
-__FBSDID("$FreeBSD: head/sbin/nvmecontrol/perftest.c 326276 2017-11-27 15:37:16Z pfg $");
+__FBSDID("$FreeBSD: head/sbin/nvmecontrol/perftest.c 329824 2018-02-22 13:32:31Z wma $");
 #endif
 #endif
 

Index: src/sbin/nvmectl/firmware.c
diff -u src/sbin/nvmectl/firmware.c:1.3 src/sbin/nvmectl/firmware.c:1.4
--- src/sbin/nvmectl/firmware.c:1.3	Tue Apr 17 08:54:35 2018
+++ src/sbin/nvmectl/firmware.c	Wed Apr 18 10:11:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: firmware.c,v 1.3 2018/04/17 08:54:35 nonaka Exp $	*/
+/*	$NetBSD: firmware.c,v 1.4 2018/04/18 10:11:44 nonaka Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -33,9 +33,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: firmware.c,v 1.3 2018/04/17 08:54:35 nonaka Exp $");
+__RCSID("$NetBSD: firmware.c,v 1.4 2018/04/18 10:11:44 nonaka Exp $");
 #if 0
-__FBSDID("$FreeBSD: head/sbin/nvmecontrol/firmware.c 326276 2017-11-27 15:37:16Z pfg $");
+__FBSDID("$FreeBSD: head/sbin/nvmecontrol/firmware.c 329824 2018-02-22 13:32:31Z wma $");
 #endif
 #endif
 
@@ -188,7 +188,7 @@ firmware(int argc, char *argv[])
 	int				fd = -1;
 	int				a_flag, s_flag, f_flag;
 	int				commit_action, reboot_required;
-	int				ch,
+	int				ch;
 	char				*p, *image = NULL;
 	char				*controller = NULL, prompt[64];
 	void				*buf = NULL;
Index: src/sbin/nvmectl/power.c
diff -u src/sbin/nvmectl/power.c:1.3 src/sbin/nvmectl/power.c:1.4
--- src/sbin/nvmectl/power.c:1.3	Sat Mar 17 11:07:26 2018
+++ src/sbin/nvmectl/power.c	Wed Apr 18 10:11:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: power.c,v 1.3 2018/03/17 11:07:26 jdolecek Exp $	*/
+/*	$NetBSD: power.c,v 1.4 2018/04/18 10:11:44 nonaka Exp $	*/
 
 /*-
  * Copyright (c) 2016 Netflix, Inc
@@ -28,9 +28,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: power.c,v 1.3 2018/03/17 11:07:26 jdolecek Exp $");
+__RCSID("$NetBSD: power.c,v 1.4 2018/04/18 10:11:44 nonaka Exp $");
 #if 0
-__FBSDID("$FreeBSD: head/sbin/nvmecontrol/power.c 296672 2016-03-11 17:25:18Z dim $");
+__FBSDID("$FreeBSD: head/sbin/nvmecontrol/power.c 329824 2018-02-22 13:32:31Z wma $");
 #endif
 #endif
 
@@ -104,7 +104,7 @@ power_set(int fd, int power_val, int wor
 	p = perm ? (1u << 31) : 0;
 	memset(&pt, 0, sizeof(pt));
 	pt.cmd.opcode = NVM_ADMIN_SET_FEATURES;
-	pt.cmd.cdw10 = NVME_FEAT_POWER_MANAGEMENT | p;
+	pt.cmd.cdw10 = NVM_FEAT_POWER_MANAGEMENT | p;
 	pt.cmd.cdw11 = power_val | (workload << 5);
 
 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
@@ -121,7 +121,7 @@ power_show(int fd)
 
 	memset(&pt, 0, sizeof(pt));
 	pt.cmd.opcode = NVM_ADMIN_GET_FEATURES;
-	pt.cmd.cdw10 = NVME_FEAT_POWER_MANAGEMENT;
+	pt.cmd.cdw10 = NVM_FEAT_POWER_MANAGEMENT;
 
 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
 		err(1, "set feature power mgmt request failed");
Index: src/sbin/nvmectl/wdc.c
diff -u src/sbin/nvmectl/wdc.c:1.3 src/sbin/nvmectl/wdc.c:1.4
--- src/sbin/nvmectl/wdc.c:1.3	Tue Apr 17 08:54:35 2018
+++ src/sbin/nvmectl/wdc.c	Wed Apr 18 10:11:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: wdc.c,v 1.3 2018/04/17 08:54:35 nonaka Exp $	*/
+/*	$NetBSD: wdc.c,v 1.4 2018/04/18 10:11:44 nonaka Exp $	*/
 
 /*-
  * Copyright (c) 2017 Netflix, Inc
@@ -28,9 +28,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: wdc.c,v 1.3 2018/04/17 08:54:35 nonaka Exp $");
+__RCSID("$NetBSD: wdc.c,v 1.4 2018/04/18 10:11:44 nonaka Exp $");
 #if 0
-__FBSDID("$FreeBSD: head/sbin/nvmecontrol/wdc.c 320522 2017-07-01 02:19:48Z imp $");
+__FBSDID("$FreeBSD: head/sbin/nvmecontrol/wdc.c 329824 2018-02-22 13:32:31Z wma $");
 #endif
 #endif
 

Index: src/sbin/nvmectl/logpage.c
diff -u src/sbin/nvmectl/logpage.c:1.6 src/sbin/nvmectl/logpage.c:1.7
--- src/sbin/nvmectl/logpage.c:1.6	Tue Apr 17 08:54:35 2018
+++ src/sbin/nvmectl/logpage.c	Wed Apr 18 10:11:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: logpage.c,v 1.6 2018/04/17 08:54:35 nonaka Exp $	*/
+/*	$NetBSD: logpage.c,v 1.7 2018/04/18 10:11:44 nonaka Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -33,9 +33,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: logpage.c,v 1.6 2018/04/17 08:54:35 nonaka Exp $");
+__RCSID("$NetBSD: logpage.c,v 1.7 2018/04/18 10:11:44 nonaka Exp $");
 #if 0
-__FBSDID("$FreeBSD: head/sbin/nvmecontrol/logpage.c 326276 2017-11-27 15:37:16Z pfg $");
+__FBSDID("$FreeBSD: head/sbin/nvmecontrol/logpage.c 329824 2018-02-22 13:32:31Z wma $");
 #endif
 #endif
 
@@ -128,12 +128,30 @@ read_logpage(int fd, uint8_t log_page, i
 }
 
 static void
+nvme_error_information_entry_swapbytes(struct nvme_error_information_entry *e)
+{
+#if _BYTE_ORDER != _LITTLE_ENDIAN
+	e->error_count = le64toh(e->error_count);
+	e->sqid = le16toh(e->sqid);
+	e->cid = le16toh(e->cid);
+	e->status = le16toh(e->status);
+	e->error_location = le16toh(e->error_location);
+	e->lba = le64toh(e->lba);
+	e->nsid = le32toh(e->nsid);
+	e->command_specific = le64toh(e->command_specific);
+#endif
+}
+
+static void
 print_log_error(const struct nvm_identify_controller *cdata __unused, void *buf,
     uint32_t size)
 {
 	int					i, nentries;
 	struct nvme_error_information_entry	*entry = buf;
 
+	/* Convert data to host endian */
+	nvme_error_information_entry_swapbytes(entry);
+
 	printf("Error Information Log\n");
 	printf("=====================\n");
 
@@ -181,11 +199,38 @@ print_temp(uint16_t t)
 }
 
 static void
+nvme_health_information_page_swapbytes(struct nvme_health_information_page *e)
+{
+#if _BYTE_ORDER != _LITTLE_ENDIAN
+	u_int i;
+
+	e->composite_temperature = le16toh(e->composite_temperature);
+	nvme_le128toh(e->data_units_read);
+	nvme_le128toh(e->data_units_written);
+	nvme_le128toh(e->host_read_commands);
+	nvme_le128toh(e->host_write_commands);
+	nvme_le128toh(e->controller_busy_time);
+	nvme_le128toh(e->power_cycles);
+	nvme_le128toh(e->power_on_hours);
+	nvme_le128toh(e->unsafe_shutdowns);
+	nvme_le128toh(e->media_errors);
+	nvme_le128toh(e->num_error_info_log_entries);
+	e->warning_temp_time = le32toh(e->warning_temp_time);
+	e->error_temp_time = le32toh(e->error_temp_time);
+	for (i = 0; i < __arraycount(e->temp_sensor); i++)
+		e->temp_sensor[i] = le16toh(e->temp_sensor[i]);
+#endif
+}
+
+static void
 print_log_health(const struct nvm_identify_controller *cdata __unused, void *buf,
     uint32_t size __unused)
 {
 	struct nvme_health_information_page *health = buf;
-	int i;
+	u_int i;
+
+	/* Convert data to host endian */
+	nvme_health_information_page_swapbytes(health);
 
 	printf("SMART/Health Information Log\n");
 	printf("============================\n");
@@ -232,7 +277,7 @@ print_log_health(const struct nvm_identi
 
 	printf("Warning Temp Composite Time:    %d\n", health->warning_temp_time);
 	printf("Error Temp Composite Time:      %d\n", health->error_temp_time);
-	for (i = 0; i < 7; i++) {
+	for (i = 0; i < __arraycount(health->temp_sensor); i++) {
 		if (health->temp_sensor[i] == 0)
 			continue;
 		printf("Temperature Sensor %d:           ", i + 1);
@@ -241,6 +286,17 @@ print_log_health(const struct nvm_identi
 }
 
 static void
+nvme_firmware_page_swapbytes(struct nvme_firmware_page *e)
+{
+#if _BYTE_ORDER != _LITTLE_ENDIAN
+	u_int i;
+
+	for (i = 0; i < __arraycount(e->revision); i++)
+		e->revision[i] = le64toh(e->revision[i]);
+#endif
+}
+
+static void
 print_log_firmware(const struct nvm_identify_controller *cdata, void *buf,
     uint32_t size __unused)
 {
@@ -248,6 +304,9 @@ print_log_firmware(const struct nvm_iden
 	const char			*status;
 	struct nvme_firmware_page	*fw = buf;
 
+	/* Convert data to host endian */
+	nvme_firmware_page_swapbytes(fw);
+
 	printf("Firmware Slot Log\n");
 	printf("=================\n");
 
@@ -284,11 +343,29 @@ print_log_firmware(const struct nvm_iden
  * offset 147: it is only 1 byte, not 6.
  */
 static void
+intel_log_temp_stats_swapbytes(struct intel_log_temp_stats *e)
+{
+#if _BYTE_ORDER != _LITTLE_ENDIAN
+	e->current = le64toh(e->current);
+	e->overtemp_flag_last = le64toh(e->overtemp_flag_last);
+	e->overtemp_flag_life = le64toh(e->overtemp_flag_life);
+	e->max_temp = le64toh(e->max_temp);
+	e->min_temp = le64toh(e->min_temp);
+	e->max_oper_temp = le64toh(e->max_oper_temp);
+	e->min_oper_temp = le64toh(e->min_oper_temp);
+	e->est_offset = le64toh(e->est_offset);
+#endif
+}
+
+static void
 print_intel_temp_stats(const struct nvm_identify_controller *cdata __unused,
     void *buf, uint32_t size __unused)
 {
 	struct intel_log_temp_stats	*temp = buf;
 
+	/* Convert data to host endian */
+	intel_log_temp_stats_swapbytes(temp);
+
 	printf("Intel Temperature Log\n");
 	printf("=====================\n");
 
Index: src/sbin/nvmectl/nvmectl.c
diff -u src/sbin/nvmectl/nvmectl.c:1.6 src/sbin/nvmectl/nvmectl.c:1.7
--- src/sbin/nvmectl/nvmectl.c:1.6	Tue Apr 17 08:54:35 2018
+++ src/sbin/nvmectl/nvmectl.c	Wed Apr 18 10:11:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvmectl.c,v 1.6 2018/04/17 08:54:35 nonaka Exp $	*/
+/*	$NetBSD: nvmectl.c,v 1.7 2018/04/18 10:11:44 nonaka Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -30,9 +30,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: nvmectl.c,v 1.6 2018/04/17 08:54:35 nonaka Exp $");
+__RCSID("$NetBSD: nvmectl.c,v 1.7 2018/04/18 10:11:44 nonaka Exp $");
 #if 0
-__FBSDID("$FreeBSD: head/sbin/nvmecontrol/nvmecontrol.c 326276 2017-11-27 15:37:16Z pfg $");
+__FBSDID("$FreeBSD: head/sbin/nvmecontrol/nvmecontrol.c 329824 2018-02-22 13:32:31Z wma $");
 #endif
 #endif
 
@@ -167,6 +167,9 @@ read_controller_data(int fd, struct nvm_
 
 	if (nvme_completion_is_error(&pt.cpl))
 		errx(1, "identify request returned error");
+
+	/* Convert data to host endian */
+	nvme_identify_controller_swapbytes(cdata);
 }
 
 void
@@ -186,6 +189,9 @@ read_namespace_data(int fd, int nsid, st
 
 	if (nvme_completion_is_error(&pt.cpl))
 		errx(1, "identify request returned error");
+
+	/* Convert data to host endian */
+	nvme_identify_namespace_swapbytes(nsdata);
 }
 
 int

Index: src/sbin/nvmectl/nvme.h
diff -u src/sbin/nvmectl/nvme.h:1.2 src/sbin/nvmectl/nvme.h:1.3
--- src/sbin/nvmectl/nvme.h:1.2	Sat Apr 29 00:06:40 2017
+++ src/sbin/nvmectl/nvme.h	Wed Apr 18 10:11:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvme.h,v 1.2 2017/04/29 00:06:40 nonaka Exp $	*/
+/*	$NetBSD: nvme.h,v 1.3 2018/04/18 10:11:44 nonaka Exp $	*/
 
 /*-
  * Copyright (C) 2012-2013 Intel Corporation
@@ -34,26 +34,6 @@
 /* Cap nvme to 1MB transfers driver explodes with larger sizes */
 #define NVME_MAX_XFER_SIZE		(MAXPHYS < (1<<20) ? MAXPHYS : (1<<20))
 
-/* Get/Set Features */
-#define	NVME_FEAT_ARBITRATION				0x01
-#define	NVME_FEAT_POWER_MANAGEMENT			0x02
-#define	NVME_FEAT_LBA_RANGE_TYPE			0x03
-#define	NVME_FEAT_TEMPERATURE_THRESHOLD			0x04
-#define	NVME_FEAT_ERROR_RECOVERY			0x05
-#define	NVME_FEAT_VOLATILE_WRITE_CACHE			0x06
-#define	NVME_FEAT_NUMBER_OF_QUEUES			0x07
-#define	NVME_FEAT_INTERRUPT_COALESCING			0x08
-#define	NVME_FEAT_INTERRUPT_VECTOR_CONFIGURATION	0x09
-#define	NVME_FEAT_WRITE_ATOMICITY_NORMAL		0x0a
-#define	NVME_FEAT_ASYNC_EVENT_CONFIGURATION		0x0b
-#define	NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION	0x0c
-#define	NVME_FEAT_HOST_MEMORY_BUFFER			0x0d
-/* NVM Command Set specific */
-#define	NVME_FEAT_SOFTWARE_PROGRESS_MARKER		0x80
-#define	NVME_FEAT_HOST_IDENTIFIER			0x81
-#define	NVME_FEAT_RESERVATION_NOTIFICATION_MASK		0x82
-#define	NVME_FEAT_RESERVATION_PERSISTANCE		0x83
-
 /* Get Log Page */
 #define	NVME_LOG_ERROR					0x01
 #define	NVME_LOG_HEALTH_INFORMATION			0x02

Index: src/sbin/nvmectl/util.c
diff -u src/sbin/nvmectl/util.c:1.1 src/sbin/nvmectl/util.c:1.2
--- src/sbin/nvmectl/util.c:1.1	Tue Apr 17 08:54:35 2018
+++ src/sbin/nvmectl/util.c	Wed Apr 18 10:11:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: util.c,v 1.1 2018/04/17 08:54:35 nonaka Exp $	*/
+/*	$NetBSD: util.c,v 1.2 2018/04/18 10:11:44 nonaka Exp $	*/
 
 /*-
  * Copyright (c) 2017 Netflix, Inc
@@ -28,7 +28,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: util.c,v 1.1 2018/04/17 08:54:35 nonaka Exp $");
+__RCSID("$NetBSD: util.c,v 1.2 2018/04/18 10:11:44 nonaka Exp $");
 #if 0
 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/util.c 320423 2017-06-27 20:24:25Z imp $");
 #endif
@@ -89,8 +89,16 @@ print_bignum(const char *title, uint64_t
 {
 	char buf[64];
 	uint8_t tmp[16];
-	uint64_t l = le64toh(v[0]);
-	uint64_t h = le64toh(v[1]);
+	uint64_t h, l;
+
+#if _BYTE_ORDER != _LITTLE_ENDIAN
+	/* Already Converted to host endian */
+	h = v[0];
+	l = v[1];
+	memcpy(tmp, v, sizeof(tmp));
+#else
+	h = v[1];
+	l = v[0];
 
 	tmp[ 0] = (h >> 56) & 0xff;
 	tmp[ 1] = (h >> 48) & 0xff;
@@ -108,16 +116,17 @@ print_bignum(const char *title, uint64_t
 	tmp[13] = (l >> 16) & 0xff;
 	tmp[14] = (l >> 8) & 0xff;
 	tmp[15] = l & 0xff;
+#endif
 
 	buf[0] = '\0';
-	BIGNUM *bn = BN_bin2bn(tmp, __arraycount(tmp), NULL);
+	BIGNUM *bn = BN_bin2bn(tmp, sizeof(tmp), NULL);
 	if (bn != NULL) {
 		humanize_bignum(buf, METRIX_PREFIX_BUFSIZ + strlen(suffix),
 		    bn, suffix, HN_AUTOSCALE, HN_DECIMAL);
 		BN_free(bn);
 	}
 	if (buf[0] == '\0')
-		snprintf(buf, sizeof(buf), "0x%016jx%016jx", h, l);
+		snprintf(buf, sizeof(buf), "0x%016" PRIx64 "%016" PRIx64, h, l);
 	printf("%-31s %s\n", title, buf);
 }
 

Index: src/sys/dev/ic/ld_nvme.c
diff -u src/sys/dev/ic/ld_nvme.c:1.19 src/sys/dev/ic/ld_nvme.c:1.20
--- src/sys/dev/ic/ld_nvme.c:1.19	Fri Mar 16 23:31:19 2018
+++ src/sys/dev/ic/ld_nvme.c	Wed Apr 18 10:11:45 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: ld_nvme.c,v 1.19 2018/03/16 23:31:19 jdolecek Exp $	*/
+/*	$NetBSD: ld_nvme.c,v 1.20 2018/04/18 10:11:45 nonaka Exp $	*/
 
 /*-
  * Copyright (C) 2016 NONAKA Kimihiro <non...@netbsd.org>
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ld_nvme.c,v 1.19 2018/03/16 23:31:19 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ld_nvme.c,v 1.20 2018/04/18 10:11:45 nonaka Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -86,7 +86,6 @@ ld_nvme_attach(device_t parent, device_t
 	struct nvme_attach_args *naa = aux;
 	struct nvme_namespace *ns;
 	struct nvm_namespace_format *f;
-	uint64_t nsze;
 	int error;
 
 	ld->sc_dv = self;
@@ -104,11 +103,10 @@ ld_nvme_attach(device_t parent, device_t
 
 	ns = nvme_ns_get(sc->sc_nvme, sc->sc_nsid);
 	KASSERT(ns);
-	nsze = lemtoh64(&ns->ident->nsze);
 	f = &ns->ident->lbaf[NVME_ID_NS_FLBAS(ns->ident->flbas)];
 
 	ld->sc_secsize = 1 << f->lbads;
-	ld->sc_secperunit = nsze;
+	ld->sc_secperunit = ns->ident->nsze;
 	ld->sc_maxxfer = naa->naa_maxphys;
 	ld->sc_maxqueuecnt = naa->naa_qentries;
 	ld->sc_start = ld_nvme_start;

Index: src/sys/dev/ic/nvme.c
diff -u src/sys/dev/ic/nvme.c:1.38 src/sys/dev/ic/nvme.c:1.39
--- src/sys/dev/ic/nvme.c:1.38	Wed Apr 18 10:05:59 2018
+++ src/sys/dev/ic/nvme.c	Wed Apr 18 10:11:45 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvme.c,v 1.38 2018/04/18 10:05:59 nonaka Exp $	*/
+/*	$NetBSD: nvme.c,v 1.39 2018/04/18 10:11:45 nonaka Exp $	*/
 /*	$OpenBSD: nvme.c,v 1.49 2016/04/18 05:59:50 dlg Exp $ */
 
 /*
@@ -18,7 +18,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvme.c,v 1.38 2018/04/18 10:05:59 nonaka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvme.c,v 1.39 2018/04/18 10:11:45 nonaka Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -624,7 +624,9 @@ nvme_ns_identify(struct nvme_softc *sc, 
 
 	identify = kmem_zalloc(sizeof(*identify), KM_SLEEP);
 	*identify = *((volatile struct nvm_identify_namespace *)NVME_DMA_KVA(mem));
-	//memcpy(identify, NVME_DMA_KVA(mem), sizeof(*identify));
+
+	/* Convert data to host endian */
+	nvme_identify_namespace_swapbytes(identify);
 
 	ns = nvme_ns_get(sc, nsid);
 	KASSERT(ns);
@@ -899,7 +901,7 @@ nvme_getcache_fill(struct nvme_queue *q,
 	struct nvme_sqe *sqe = slot;
 
 	sqe->opcode = NVM_ADMIN_GET_FEATURES;
-	sqe->cdw10 = NVM_FEATURE_VOLATILE_WRITE_CACHE;
+	htolem32(&sqe->cdw10, NVM_FEATURE_VOLATILE_WRITE_CACHE);
 }
 
 static void
@@ -1122,7 +1124,7 @@ nvme_q_submit(struct nvme_softc *sc, str
 	    sizeof(*sqe) * tail, sizeof(*sqe), BUS_DMASYNC_POSTWRITE);
 	memset(sqe, 0, sizeof(*sqe));
 	(*fill)(q, ccb, sqe);
-	sqe->cid = ccb->ccb_id;
+	htolem16(&sqe->cid, ccb->ccb_id);
 	bus_dmamap_sync(sc->sc_dmat, NVME_DMA_MAP(q->q_sq_dmamem),
 	    sizeof(*sqe) * tail, sizeof(*sqe), BUS_DMASYNC_PREWRITE);
 
@@ -1334,25 +1336,28 @@ nvme_identify(struct nvme_softc *sc, u_i
 		goto done;
 
 	identify = NVME_DMA_KVA(mem);
+	sc->sc_identify = *identify;
+	identify = NULL;
+
+	/* Convert data to host endian */
+	nvme_identify_controller_swapbytes(&sc->sc_identify);
 
-	strnvisx(sn, sizeof(sn), (const char *)identify->sn,
-	    sizeof(identify->sn), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
-	strnvisx(mn, sizeof(mn), (const char *)identify->mn,
-	    sizeof(identify->mn), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
-	strnvisx(fr, sizeof(fr), (const char *)identify->fr,
-	    sizeof(identify->fr), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
+	strnvisx(sn, sizeof(sn), (const char *)sc->sc_identify.sn,
+	    sizeof(sc->sc_identify.sn), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
+	strnvisx(mn, sizeof(mn), (const char *)sc->sc_identify.mn,
+	    sizeof(sc->sc_identify.mn), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
+	strnvisx(fr, sizeof(fr), (const char *)sc->sc_identify.fr,
+	    sizeof(sc->sc_identify.fr), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
 	aprint_normal_dev(sc->sc_dev, "%s, firmware %s, serial %s\n", mn, fr,
 	    sn);
 
-	if (identify->mdts > 0) {
-		mdts = (1 << identify->mdts) * (1 << mps);
+	if (sc->sc_identify.mdts > 0) {
+		mdts = (1 << sc->sc_identify.mdts) * (1 << mps);
 		if (mdts < sc->sc_mdts)
 			sc->sc_mdts = mdts;
 	}
 
-	sc->sc_nn = lemtoh32(&identify->nn);
-
-	memcpy(&sc->sc_identify, identify, sizeof(sc->sc_identify));
+	sc->sc_nn = sc->sc_identify.nn;
 
 done:
 	nvme_dmamem_free(sc, mem);

Index: src/sys/dev/ic/nvmeio.h
diff -u src/sys/dev/ic/nvmeio.h:1.1 src/sys/dev/ic/nvmeio.h:1.2
--- src/sys/dev/ic/nvmeio.h:1.1	Sat Jun  4 16:11:51 2016
+++ src/sys/dev/ic/nvmeio.h	Wed Apr 18 10:11:45 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvmeio.h,v 1.1 2016/06/04 16:11:51 nonaka Exp $	*/
+/*	$NetBSD: nvmeio.h,v 1.2 2018/04/18 10:11:45 nonaka Exp $	*/
 
 /*-
  * Copyright (C) 2012-2013 Intel Corporation
@@ -25,12 +25,13 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/sys/dev/nvme/nvme.h 296617 2016-03-10 17:13:10Z mav $
+ * $FreeBSD: head/sys/dev/nvme/nvme.h 329824 2018-02-22 13:32:31Z wma $
  */
 
 #ifndef __NVMEIO_H__
 #define __NVMEIO_H__
 
+#include <sys/endian.h>
 #include <sys/ioccom.h>
 #include <dev/ic/nvmereg.h>
 
@@ -94,4 +95,103 @@ struct nvme_pt_command {
 	uint32_t		timeout;
 };
 
+/* Endianess conversion functions for NVMe structs */
+static inline void
+nvme_le128toh(uint64_t v[2])
+{
+#if _BYTE_ORDER != _LITTLE_ENDIAN
+	uint64_t t;
+
+	t = le64toh(v[0]);
+	v[0] = le64toh(v[1]);
+	v[1] = t;
+#endif
+}
+
+static inline void
+nvme_namespace_format_swapbytes(struct nvm_namespace_format *format)
+{
+
+#if _BYTE_ORDER != _LITTLE_ENDIAN
+	format->ms = le16toh(format->ms);
+#endif
+}
+
+static inline void
+nvme_identify_namespace_swapbytes(struct nvm_identify_namespace *identify)
+{
+#if _BYTE_ORDER != _LITTLE_ENDIAN
+	u_int i;
+
+	identify->nsze = le64toh(identify->nsze);
+	identify->ncap = le64toh(identify->ncap);
+	identify->nuse = le64toh(identify->nuse);
+	identify->nawun = le16toh(identify->nawun);
+	identify->nawupf = le16toh(identify->nawupf);
+	identify->nacwu = le16toh(identify->nacwu);
+	identify->nabsn = le16toh(identify->nabsn);
+	identify->nabo = le16toh(identify->nabo);
+	identify->nabspf = le16toh(identify->nabspf);
+	identify->noiob = le16toh(identify->noiob);
+	for (i = 0; i < __arraycount(identify->lbaf); i++)
+		nvme_namespace_format_swapbytes(&identify->lbaf[i]);
+#endif
+}
+
+static inline void
+nvme_identify_psd_swapbytes(struct nvm_identify_psd *psd)
+{
+
+#if _BYTE_ORDER != _LITTLE_ENDIAN
+	psd->mp = le16toh(psd->mp);
+	psd->enlat = le32toh(psd->enlat);
+	psd->exlat = le32toh(psd->exlat);
+	psd->idlp = le16toh(psd->idlp);
+	psd->actp = le16toh(psd->actp);
+	psd->ap = le16toh(psd->ap);
+#endif
+}
+
+static inline void
+nvme_identify_controller_swapbytes(struct nvm_identify_controller *identify)
+{
+#if _BYTE_ORDER != _LITTLE_ENDIAN
+	u_int i;
+
+	identify->vid = le16toh(identify->vid);
+	identify->ssvid = le16toh(identify->ssvid);
+	identify->cntlid = le16toh(identify->cntlid);
+	identify->ver = le32toh(identify->ver);
+	identify->rtd3r = le32toh(identify->rtd3r);
+	identify->rtd3e = le32toh(identify->rtd3e);
+	identify->oaes = le32toh(identify->oaes);
+	identify->ctrattr = le32toh(identify->ctrattr);
+	identify->oacs = le16toh(identify->oacs);
+	identify->wctemp = le16toh(identify->wctemp);
+	identify->cctemp = le16toh(identify->cctemp);
+	identify->mtfa = le16toh(identify->mtfa);
+	identify->hmpre = le32toh(identify->hmpre);
+	identify->hmmin = le32toh(identify->hmmin);
+	nvme_le128toh(identify->untncap.tnvmcap);
+	nvme_le128toh(identify->untncap.unvmcap);
+	identify->rpmbs = le32toh(identify->rpmbs);
+	identify->edstt = le16toh(identify->edstt);
+	identify->kas = le16toh(identify->kas);
+	identify->hctma = le16toh(identify->hctma);
+	identify->mntmt = le16toh(identify->mntmt);
+	identify->mxtmt = le16toh(identify->mxtmt);
+	identify->sanicap = le32toh(identify->sanicap);
+	identify->maxcmd = le16toh(identify->maxcmd);
+	identify->nn = le32toh(identify->nn);
+	identify->oncs = le16toh(identify->oncs);
+	identify->fuses = le16toh(identify->fuses);
+	identify->awun = le16toh(identify->awun);
+	identify->awupf = le16toh(identify->awupf);
+	identify->acwu = le16toh(identify->acwu);
+	identify->sgls = le32toh(identify->sgls);
+	for (i = 0; i < __arraycount(identify->psd); i++)
+		nvme_identify_psd_swapbytes(&identify->psd[i]);
+#endif
+}
+
 #endif /* __NVMEIO_H__ */

Reply via email to