2015-09-21 08:52:41 -07:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
2016-01-26 10:47:22 -07:00
|
|
|
* Copyright (c) Intel Corporation.
|
2015-09-21 08:52:41 -07:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * 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.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
|
|
|
|
* OWNER 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 <stdbool.h>
|
2016-09-30 14:14:18 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-09-21 08:52:41 -07:00
|
|
|
|
|
|
|
#include <rte_config.h>
|
|
|
|
#include <rte_lcore.h>
|
|
|
|
|
|
|
|
#include "spdk/nvme.h"
|
2016-08-10 10:41:12 -07:00
|
|
|
#include "spdk/env.h"
|
2015-09-21 08:52:41 -07:00
|
|
|
|
|
|
|
#define MAX_DEVS 64
|
|
|
|
|
|
|
|
struct dev {
|
2016-02-10 11:26:12 -07:00
|
|
|
struct spdk_nvme_ctrlr *ctrlr;
|
2016-02-09 11:06:48 -07:00
|
|
|
struct spdk_nvme_health_information_page *health_page;
|
|
|
|
uint32_t orig_temp_threshold;
|
|
|
|
char name[100];
|
2015-09-21 08:52:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct dev devs[MAX_DEVS];
|
|
|
|
static int num_devs = 0;
|
|
|
|
|
|
|
|
static int aer_done = 0;
|
|
|
|
|
|
|
|
|
|
|
|
#define foreach_dev(iter) \
|
|
|
|
for (iter = devs; iter - devs < num_devs; iter++)
|
|
|
|
|
|
|
|
|
|
|
|
static int temperature_done = 0;
|
2016-01-05 16:45:33 -07:00
|
|
|
static int failed = 0;
|
2015-09-21 08:52:41 -07:00
|
|
|
|
2016-02-09 11:06:48 -07:00
|
|
|
static void set_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl)
|
2015-09-21 08:52:41 -07:00
|
|
|
{
|
2016-01-05 16:45:33 -07:00
|
|
|
struct dev *dev = cb_arg;
|
|
|
|
|
2016-02-09 11:06:48 -07:00
|
|
|
if (spdk_nvme_cpl_is_error(cpl)) {
|
2016-01-05 16:45:33 -07:00
|
|
|
printf("%s: set feature (temp threshold) failed\n", dev->name);
|
|
|
|
failed = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-21 08:52:41 -07:00
|
|
|
/* Admin command completions are synchronized by the NVMe driver,
|
|
|
|
* so we don't need to do any special locking here. */
|
|
|
|
temperature_done++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_temp_threshold(struct dev *dev, uint32_t temp)
|
|
|
|
{
|
2016-02-09 11:06:48 -07:00
|
|
|
struct spdk_nvme_cmd cmd = {};
|
2015-09-21 08:52:41 -07:00
|
|
|
|
2016-02-09 11:06:48 -07:00
|
|
|
cmd.opc = SPDK_NVME_OPC_SET_FEATURES;
|
|
|
|
cmd.cdw10 = SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD;
|
2015-09-21 08:52:41 -07:00
|
|
|
cmd.cdw11 = temp;
|
|
|
|
|
2016-02-10 11:26:12 -07:00
|
|
|
return spdk_nvme_ctrlr_cmd_admin_raw(dev->ctrlr, &cmd, NULL, 0, set_feature_completion, dev);
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-09 11:06:48 -07:00
|
|
|
get_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl)
|
2015-09-21 08:52:41 -07:00
|
|
|
{
|
|
|
|
struct dev *dev = cb_arg;
|
|
|
|
|
2016-02-09 11:06:48 -07:00
|
|
|
if (spdk_nvme_cpl_is_error(cpl)) {
|
2015-09-21 08:52:41 -07:00
|
|
|
printf("%s: get feature (temp threshold) failed\n", dev->name);
|
2016-01-05 16:45:33 -07:00
|
|
|
failed = 1;
|
|
|
|
return;
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
|
2016-01-05 16:45:33 -07:00
|
|
|
dev->orig_temp_threshold = cpl->cdw0;
|
|
|
|
printf("%s: original temperature threshold: %u Kelvin (%d Celsius)\n",
|
|
|
|
dev->name, dev->orig_temp_threshold, dev->orig_temp_threshold - 273);
|
|
|
|
|
2015-09-21 08:52:41 -07:00
|
|
|
/* Set temperature threshold to a low value so the AER will trigger. */
|
|
|
|
set_temp_threshold(dev, 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_temp_threshold(struct dev *dev)
|
|
|
|
{
|
2016-02-09 11:06:48 -07:00
|
|
|
struct spdk_nvme_cmd cmd = {};
|
2015-09-21 08:52:41 -07:00
|
|
|
|
2016-02-09 11:06:48 -07:00
|
|
|
cmd.opc = SPDK_NVME_OPC_GET_FEATURES;
|
|
|
|
cmd.cdw10 = SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD;
|
2015-09-21 08:52:41 -07:00
|
|
|
|
2016-02-10 11:26:12 -07:00
|
|
|
return spdk_nvme_ctrlr_cmd_admin_raw(dev->ctrlr, &cmd, NULL, 0, get_feature_completion, dev);
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-09 11:06:48 -07:00
|
|
|
print_health_page(struct dev *dev, struct spdk_nvme_health_information_page *hip)
|
2015-09-21 08:52:41 -07:00
|
|
|
{
|
|
|
|
printf("%s: Current Temperature: %u Kelvin (%d Celsius)\n",
|
|
|
|
dev->name, hip->temperature, hip->temperature - 273);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-09 11:06:48 -07:00
|
|
|
get_log_page_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl)
|
2015-09-21 08:52:41 -07:00
|
|
|
{
|
|
|
|
struct dev *dev = cb_arg;
|
|
|
|
|
2016-02-09 11:06:48 -07:00
|
|
|
if (spdk_nvme_cpl_is_error(cpl)) {
|
2015-09-21 08:52:41 -07:00
|
|
|
printf("%s: get log page failed\n", dev->name);
|
2016-01-05 16:45:33 -07:00
|
|
|
failed = 1;
|
|
|
|
return;
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
|
2016-01-05 16:45:33 -07:00
|
|
|
print_health_page(dev, dev->health_page);
|
2015-09-21 08:52:41 -07:00
|
|
|
aer_done++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_health_log_page(struct dev *dev)
|
|
|
|
{
|
2016-02-10 11:26:12 -07:00
|
|
|
return spdk_nvme_ctrlr_cmd_get_log_page(dev->ctrlr, SPDK_NVME_LOG_HEALTH_INFORMATION,
|
|
|
|
SPDK_NVME_GLOBAL_NS_TAG, dev->health_page, sizeof(*dev->health_page),
|
|
|
|
get_log_page_completion, dev);
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
|
|
|
struct dev *dev;
|
|
|
|
|
|
|
|
foreach_dev(dev) {
|
|
|
|
if (dev->health_page) {
|
2016-08-17 13:35:18 -07:00
|
|
|
spdk_free(dev->health_page);
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 11:06:48 -07:00
|
|
|
static void aer_cb(void *arg, const struct spdk_nvme_cpl *cpl)
|
2015-09-21 08:52:41 -07:00
|
|
|
{
|
|
|
|
uint32_t log_page_id = (cpl->cdw0 & 0xFF0000) >> 16;
|
|
|
|
struct dev *dev = arg;
|
|
|
|
|
2016-02-09 11:06:48 -07:00
|
|
|
if (spdk_nvme_cpl_is_error(cpl)) {
|
2016-01-05 16:45:33 -07:00
|
|
|
printf("%s: AER failed\n", dev->name);
|
|
|
|
failed = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-21 08:52:41 -07:00
|
|
|
printf("%s: aer_cb for log page %d\n", dev->name, log_page_id);
|
|
|
|
|
|
|
|
/* Set the temperature threshold back to the original value
|
|
|
|
* so the AER doesn't trigger again.
|
|
|
|
*/
|
|
|
|
set_temp_threshold(dev, dev->orig_temp_threshold);
|
|
|
|
|
|
|
|
get_health_log_page(dev);
|
|
|
|
}
|
|
|
|
|
2016-01-29 13:15:29 -07:00
|
|
|
|
|
|
|
static bool
|
2016-10-31 16:55:14 -07:00
|
|
|
probe_cb(void *cb_ctx, const struct spdk_nvme_probe_info *probe_info,
|
|
|
|
struct spdk_nvme_ctrlr_opts *opts)
|
2016-01-29 13:15:29 -07:00
|
|
|
{
|
|
|
|
printf("Attaching to %04x:%02x:%02x.%02x\n",
|
2016-10-31 16:55:14 -07:00
|
|
|
probe_info->pci_addr.domain,
|
|
|
|
probe_info->pci_addr.bus,
|
|
|
|
probe_info->pci_addr.dev,
|
|
|
|
probe_info->pci_addr.func);
|
2016-01-29 13:15:29 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-10-31 16:55:14 -07:00
|
|
|
attach_cb(void *cb_ctx, const struct spdk_nvme_probe_info *probe_info,
|
|
|
|
struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
|
2016-01-29 13:15:29 -07:00
|
|
|
{
|
|
|
|
struct dev *dev;
|
|
|
|
|
|
|
|
/* add to dev list */
|
|
|
|
dev = &devs[num_devs++];
|
|
|
|
|
|
|
|
dev->ctrlr = ctrlr;
|
|
|
|
|
|
|
|
snprintf(dev->name, sizeof(dev->name), "%04x:%02x:%02x.%02x",
|
2016-10-31 16:55:14 -07:00
|
|
|
probe_info->pci_addr.domain,
|
|
|
|
probe_info->pci_addr.bus,
|
|
|
|
probe_info->pci_addr.dev,
|
|
|
|
probe_info->pci_addr.func);
|
2016-01-29 13:15:29 -07:00
|
|
|
|
|
|
|
printf("Attached to %s\n", dev->name);
|
|
|
|
|
2016-08-17 13:35:18 -07:00
|
|
|
dev->health_page = spdk_zmalloc(sizeof(*dev->health_page), 4096, NULL);
|
2016-01-29 13:15:29 -07:00
|
|
|
if (dev->health_page == NULL) {
|
|
|
|
printf("Allocation error (health page)\n");
|
|
|
|
failed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 08:52:41 -07:00
|
|
|
static const char *ealargs[] = {
|
|
|
|
"aer",
|
|
|
|
"-c 0x1",
|
|
|
|
"-n 4",
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct dev *dev;
|
|
|
|
int rc, i;
|
|
|
|
|
|
|
|
printf("Asynchronous Event Request test\n");
|
|
|
|
|
|
|
|
rc = rte_eal_init(sizeof(ealargs) / sizeof(ealargs[0]),
|
|
|
|
(char **)(void *)(uintptr_t)ealargs);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "could not initialize dpdk\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-06-21 16:49:26 -07:00
|
|
|
if (spdk_nvme_probe(NULL, probe_cb, attach_cb, NULL) != 0) {
|
2016-02-10 11:26:12 -07:00
|
|
|
fprintf(stderr, "spdk_nvme_probe() failed\n");
|
2016-01-29 13:15:29 -07:00
|
|
|
return 1;
|
|
|
|
}
|
2015-09-21 08:52:41 -07:00
|
|
|
|
2016-01-29 13:15:29 -07:00
|
|
|
if (failed) {
|
|
|
|
goto done;
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Registering asynchronous event callbacks...\n");
|
|
|
|
foreach_dev(dev) {
|
2016-02-10 11:26:12 -07:00
|
|
|
spdk_nvme_ctrlr_register_aer_callback(dev->ctrlr, aer_cb, dev);
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Setting temperature thresholds...\n");
|
|
|
|
foreach_dev(dev) {
|
|
|
|
/* Get the original temperature threshold and set it to a low value */
|
|
|
|
get_temp_threshold(dev);
|
|
|
|
}
|
|
|
|
|
2016-01-05 16:45:33 -07:00
|
|
|
while (!failed && temperature_done < num_devs) {
|
2015-09-21 08:52:41 -07:00
|
|
|
foreach_dev(dev) {
|
2016-02-10 11:26:12 -07:00
|
|
|
spdk_nvme_ctrlr_process_admin_completions(dev->ctrlr);
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 16:45:33 -07:00
|
|
|
if (failed) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2015-09-21 08:52:41 -07:00
|
|
|
printf("Waiting for all controllers to trigger AER...\n");
|
|
|
|
|
2016-01-05 16:45:33 -07:00
|
|
|
while (!failed && aer_done < num_devs) {
|
2015-09-21 08:52:41 -07:00
|
|
|
foreach_dev(dev) {
|
2016-02-10 11:26:12 -07:00
|
|
|
spdk_nvme_ctrlr_process_admin_completions(dev->ctrlr);
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Cleaning up...\n");
|
|
|
|
|
|
|
|
for (i = 0; i < num_devs; i++) {
|
|
|
|
struct dev *dev = &devs[i];
|
|
|
|
|
2016-02-10 11:26:12 -07:00
|
|
|
spdk_nvme_detach(dev->ctrlr);
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|
|
|
|
|
2016-01-05 16:45:33 -07:00
|
|
|
done:
|
2015-09-21 08:52:41 -07:00
|
|
|
cleanup();
|
|
|
|
|
2016-01-05 16:45:33 -07:00
|
|
|
return failed;
|
2015-09-21 08:52:41 -07:00
|
|
|
}
|