From 38441bd9a978287f0f37911afc046add95cc187c Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Fri, 19 Jul 2013 21:40:57 +0000 Subject: [PATCH] Add message when nvd disks are attached and detached. As part of this commit, add an nvme_strvis() function which borrows heavily from cam_strvis(). This will allow stripping of leading/trailing whitespace and also handle unprintable characters in model/serial numbers. This function goes into a new nvme_util.c file which is used by both the driver and nvmecontrol. Sponsored by: Intel Reviewed by: carl MFC after: 3 days --- sbin/nvmecontrol/Makefile | 4 ++- sbin/nvmecontrol/devlist.c | 4 ++- sbin/nvmecontrol/identify.c | 14 +++++---- sys/conf/files.amd64 | 1 + sys/conf/files.i386 | 1 + sys/dev/nvd/nvd.c | 37 +++++++++++++++++----- sys/dev/nvme/nvme.c | 1 - sys/dev/nvme/nvme.h | 6 ++-- sys/dev/nvme/nvme_util.c | 61 +++++++++++++++++++++++++++++++++++++ sys/modules/nvme/Makefile | 1 + 10 files changed, 111 insertions(+), 19 deletions(-) create mode 100644 sys/dev/nvme/nvme_util.c diff --git a/sbin/nvmecontrol/Makefile b/sbin/nvmecontrol/Makefile index 46820560f58b..ea60da3c2941 100644 --- a/sbin/nvmecontrol/Makefile +++ b/sbin/nvmecontrol/Makefile @@ -2,7 +2,9 @@ PROG= nvmecontrol SRCS= nvmecontrol.c devlist.c firmware.c identify.c logpage.c \ - perftest.c reset.c + perftest.c reset.c nvme_util.c MAN= nvmecontrol.8 +.PATH: ${.CURDIR}/../../sys/dev/nvme + .include diff --git a/sbin/nvmecontrol/devlist.c b/sbin/nvmecontrol/devlist.c index eb404345ddb8..f73d643e93d2 100644 --- a/sbin/nvmecontrol/devlist.c +++ b/sbin/nvmecontrol/devlist.c @@ -62,6 +62,7 @@ devlist(int argc, char *argv[]) struct nvme_controller_data cdata; struct nvme_namespace_data nsdata; char name[64]; + uint8_t mn[64]; uint32_t i; int ch, ctrlr, fd, found, ret; @@ -91,7 +92,8 @@ devlist(int argc, char *argv[]) found++; read_controller_data(fd, &cdata); - printf("%6s: %.*s\n", name, NVME_MODEL_NUMBER_LENGTH, cdata.mn); + nvme_strvis(mn, cdata.mn, sizeof(mn), NVME_MODEL_NUMBER_LENGTH); + printf("%6s: %s\n", name, mn); for (i = 0; i < cdata.nn; i++) { sprintf(name, "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr, diff --git a/sbin/nvmecontrol/identify.c b/sbin/nvmecontrol/identify.c index 65854303566b..58492e2fd956 100644 --- a/sbin/nvmecontrol/identify.c +++ b/sbin/nvmecontrol/identify.c @@ -43,16 +43,18 @@ __FBSDID("$FreeBSD$"); static void print_controller(struct nvme_controller_data *cdata) { + uint8_t str[128]; + printf("Controller Capabilities/Features\n"); printf("================================\n"); printf("Vendor ID: %04x\n", cdata->vid); printf("Subsystem Vendor ID: %04x\n", cdata->ssvid); - printf("Serial Number: %.*s\n", - NVME_SERIAL_NUMBER_LENGTH, cdata->sn); - printf("Model Number: %.*s\n", - NVME_MODEL_NUMBER_LENGTH, cdata->mn); - printf("Firmware Version: %.*s\n", - NVME_FIRMWARE_REVISION_LENGTH, cdata->fr); + nvme_strvis(str, cdata->sn, sizeof(str), NVME_SERIAL_NUMBER_LENGTH); + printf("Serial Number: %s\n", str); + nvme_strvis(str, cdata->mn, sizeof(str), NVME_MODEL_NUMBER_LENGTH); + printf("Model Number: %s\n", str); + nvme_strvis(str, cdata->fr, sizeof(str), NVME_FIRMWARE_REVISION_LENGTH); + printf("Firmware Version: %s\n", str); printf("Recommended Arb Burst: %d\n", cdata->rab); printf("IEEE OUI Identifier: %02x %02x %02x\n", cdata->ieee[0], cdata->ieee[1], cdata->ieee[2]); diff --git a/sys/conf/files.amd64 b/sys/conf/files.amd64 index 191f680d8d08..c898ef802789 100644 --- a/sys/conf/files.amd64 +++ b/sys/conf/files.amd64 @@ -234,6 +234,7 @@ dev/nvme/nvme_ns_cmd.c optional nvme dev/nvme/nvme_qpair.c optional nvme dev/nvme/nvme_sysctl.c optional nvme dev/nvme/nvme_test.c optional nvme +dev/nvme/nvme_util.c optional nvme dev/nvram/nvram.c optional nvram isa dev/random/ivy.c optional random rdrand_rng dev/random/nehemiah.c optional random padlock_rng diff --git a/sys/conf/files.i386 b/sys/conf/files.i386 index f4d792834e41..9a27bdec1c7b 100644 --- a/sys/conf/files.i386 +++ b/sys/conf/files.i386 @@ -243,6 +243,7 @@ dev/nvme/nvme_ns_cmd.c optional nvme dev/nvme/nvme_qpair.c optional nvme dev/nvme/nvme_sysctl.c optional nvme dev/nvme/nvme_test.c optional nvme +dev/nvme/nvme_util.c optional nvme dev/nvram/nvram.c optional nvram isa dev/pcf/pcf_isa.c optional pcf dev/random/ivy.c optional random rdrand_rng diff --git a/sys/dev/nvd/nvd.c b/sys/dev/nvd/nvd.c index 1bde1ca834aa..b2e880b5c5c2 100644 --- a/sys/dev/nvd/nvd.c +++ b/sys/dev/nvd/nvd.c @@ -40,6 +40,8 @@ __FBSDID("$FreeBSD$"); #include +#define NVD_STR "nvd" + struct nvd_disk; static disk_ioctl_t nvd_ioctl; @@ -102,7 +104,7 @@ static int nvd_modevent(module_t mod, int type, void *arg) } moduledata_t nvd_mod = { - "nvd", + NVD_STR, (modeventhand_t)nvd_modevent, 0 }; @@ -271,6 +273,7 @@ nvd_new_controller(struct nvme_controller *ctrlr) static void * nvd_new_disk(struct nvme_namespace *ns, void *ctrlr_arg) { + uint8_t descr[NVME_MODEL_NUMBER_LENGTH+1]; struct nvd_disk *ndisk; struct disk *disk; struct nvd_controller *ctrlr = ctrlr_arg; @@ -280,7 +283,7 @@ nvd_new_disk(struct nvme_namespace *ns, void *ctrlr_arg) disk = disk_alloc(); disk->d_strategy = nvd_strategy; disk->d_ioctl = nvd_ioctl; - disk->d_name = "nvd"; + disk->d_name = NVD_STR; disk->d_drv1 = ndisk; disk->d_maxsize = nvme_ns_get_max_io_xfer_size(ns); @@ -310,12 +313,14 @@ nvd_new_disk(struct nvme_namespace *ns, void *ctrlr_arg) * d_ident and d_descr are both far bigger than the length of either * the serial or model number strings. */ - strlcpy(disk->d_ident, nvme_ns_get_serial_number(ns), - min(sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH)); + nvme_strvis(disk->d_ident, nvme_ns_get_serial_number(ns), + sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH); + + nvme_strvis(descr, nvme_ns_get_model_number(ns), sizeof(descr), + NVME_MODEL_NUMBER_LENGTH); #if __FreeBSD_version >= 900034 - strlcpy(disk->d_descr, nvme_ns_get_model_number(ns), - min(sizeof(disk->d_descr), NVME_MODEL_NUMBER_LENGTH)); + strlcpy(disk->d_descr, descr, sizeof(descr)); #endif ndisk->ns = ns; @@ -335,15 +340,27 @@ nvd_new_disk(struct nvme_namespace *ns, void *ctrlr_arg) disk_create(disk, DISK_VERSION); + printf(NVD_STR"%u: <%s> NVMe namespace\n", disk->d_unit, descr); + printf(NVD_STR"%u: %juMB (%ju %u byte sectors)\n", disk->d_unit, + (uintmax_t)disk->d_mediasize / (1024*1024), + (uintmax_t)disk->d_mediasize / disk->d_sectorsize, + disk->d_sectorsize); + return (NULL); } static void destroy_geom_disk(struct nvd_disk *ndisk) { - struct bio *bp; + struct bio *bp; + struct disk *disk; + uint32_t unit; + int cnt = 0; + disk = ndisk->disk; + unit = disk->d_unit; taskqueue_free(ndisk->tq); + disk_destroy(ndisk->disk); mtx_lock(&ndisk->bioqlock); @@ -354,9 +371,13 @@ destroy_geom_disk(struct nvd_disk *ndisk) bp->bio_error = EIO; bp->bio_flags |= BIO_ERROR; bp->bio_resid = bp->bio_bcount; - + cnt++; biodone(bp); } + + printf(NVD_STR"%u: lost device - %d outstanding\n", unit, cnt); + printf(NVD_STR"%u: removing device entry\n", unit); + mtx_unlock(&ndisk->bioqlock); mtx_destroy(&ndisk->bioqlock); diff --git a/sys/dev/nvme/nvme.c b/sys/dev/nvme/nvme.c index 65bb05e14ca4..eacd0cc49974 100644 --- a/sys/dev/nvme/nvme.c +++ b/sys/dev/nvme/nvme.c @@ -383,4 +383,3 @@ nvme_completion_poll_cb(void *arg, const struct nvme_completion *cpl) wmb(); status->done = TRUE; } - diff --git a/sys/dev/nvme/nvme.h b/sys/dev/nvme/nvme.h index f30505a7bc8f..9df75da046b7 100644 --- a/sys/dev/nvme/nvme.h +++ b/sys/dev/nvme/nvme.h @@ -404,10 +404,10 @@ struct nvme_controller_data { uint16_t ssvid; /** serial number */ - int8_t sn[NVME_SERIAL_NUMBER_LENGTH]; + uint8_t sn[NVME_SERIAL_NUMBER_LENGTH]; /** model number */ - int8_t mn[NVME_MODEL_NUMBER_LENGTH]; + uint8_t mn[NVME_MODEL_NUMBER_LENGTH]; /** firmware revision */ uint8_t fr[NVME_FIRMWARE_REVISION_LENGTH]; @@ -786,6 +786,8 @@ struct nvme_pt_command { #define nvme_completion_is_error(cpl) \ ((cpl)->status.sc != 0 || (cpl)->status.sct != 0) +void nvme_strvis(uint8_t *dst, const uint8_t *src, int dstlen, int srclen); + #ifdef _KERNEL struct bio; diff --git a/sys/dev/nvme/nvme_util.c b/sys/dev/nvme/nvme_util.c new file mode 100644 index 000000000000..6d7084167a75 --- /dev/null +++ b/sys/dev/nvme/nvme_util.c @@ -0,0 +1,61 @@ +/*- + * Copyright (C) 2013 Intel Corporation + * Copyright (C) 1997 Justin T. Gibbs + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +void +nvme_strvis(uint8_t *dst, const uint8_t *src, int dstlen, int srclen) +{ + uint8_t *cur_pos; + + /* Trim leading/trailing spaces, nulls. */ + while (srclen > 0 && src[0] == ' ') + src++, srclen--; + while (srclen > 0 + && (src[srclen - 1] == ' ' || src[srclen - 1] == '\0')) + srclen--; + + while (srclen > 0 && dstlen > 1) { + cur_pos = dst; + + /* Show '?' for non-printable characters. */ + if (*src < 0x20 || *src >= 0x7F) + *cur_pos++ = '?'; + else + *cur_pos++ = *src; + src++; + srclen--; + dstlen -= cur_pos - dst; + dst = cur_pos; + } + *dst = '\0'; +} + diff --git a/sys/modules/nvme/Makefile b/sys/modules/nvme/Makefile index 17004bca5f32..ae727a7d0b1a 100644 --- a/sys/modules/nvme/Makefile +++ b/sys/modules/nvme/Makefile @@ -12,6 +12,7 @@ SRCS = nvme.c \ nvme_qpair.c \ nvme_sysctl.c \ nvme_test.c \ + nvme_util.c \ \ bus_if.h \ device_if.h \