1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/*
* zpcictl - Manage PCI devices on z Systems
*
* Copyright IBM Corp. 2018
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <time.h>
#include "lib/util_base.h"
#include "lib/util_libc.h"
#include "lib/util_opt.h"
#include "lib/util_path.h"
#include "lib/util_prg.h"
#include "lib/util_proc.h"
#include "lib/util_rec.h"
#include "lib/util_scandir.h"
#include "lib/util_sys.h"
#include "zpcictl.h"
#define SMARTCTL_CMDLINE "smartctl -x %s 2>/dev/null"
static const struct util_prg prg = {
.desc = "Use zpcictl to manage PCI devices on IBM Z\n"
"DEVICE is the slot ID or node of the device "
"(e.g. 0000:00:00.0 or /dev/nvme0)",
.args = "DEVICE",
.copyright_vec = {
{
.owner = "IBM Corp.",
.pub_first = 2018,
.pub_last = 2018,
},
UTIL_PRG_COPYRIGHT_END
}
};
/* Defines for options with no short command */
#define OPT_RESET 128
#define OPT_DECONF 129
#define OPT_REPORT_ERR 130
static struct util_opt opt_vec[] = {
UTIL_OPT_SECTION("ERROR HANDLING OPTIONS"),
{
.option = { "reset", no_argument, NULL, OPT_RESET },
.desc = "Reset the device",
.flags = UTIL_OPT_FLAG_NOSHORT,
},
{
.option = { "deconfigure", no_argument, NULL, OPT_DECONF },
.desc = "Deconfigure the device to prepare for any repair action",
.flags = UTIL_OPT_FLAG_NOSHORT,
},
{
.option = { "report-error", no_argument, NULL, OPT_REPORT_ERR },
.desc = "Report a device error to the Support Element (SE)",
.flags = UTIL_OPT_FLAG_NOSHORT,
},
UTIL_OPT_SECTION("GENERAL OPTIONS"),
UTIL_OPT_HELP,
UTIL_OPT_VERSION,
UTIL_OPT_END
};
static int is_char_dev(const char *dev)
{
struct stat s;
if (stat(dev, &s))
return 0;
return S_ISCHR(s.st_mode);
}
static int is_blk_dev(const char *dev)
{
struct stat s;
if (stat(dev, &s))
return 0;
return S_ISBLK(s.st_mode);
}
static void fopen_err(char *path)
{
warnx("Could not open file %s: %s", path, strerror(errno));
free(path);
exit(EXIT_FAILURE);
}
static void fclose_err(char *path)
{
if (errno == EIO || errno == EOPNOTSUPP)
warnx("Unsupported operation: %s: %s", path, strerror(errno));
else
warnx("Could not close file: %s: %s", path, strerror(errno));
free(path);
exit(EXIT_FAILURE);
}
static void fread_err(FILE *fp, char *path)
{
warnx("Could not read file: %s: %s", path, strerror(errno));
if (fclose(fp))
fclose_err(path);
free(path);
exit(EXIT_FAILURE);
}
static void fwrite_err(FILE *fp, char *path)
{
warnx("Could not write to file: %s: %s", path, strerror(errno));
if (fclose(fp))
fclose_err(path);
free(path);
exit(EXIT_FAILURE);
}
#define READ_CHUNK_SIZE 512
static char *collect_smart_data(struct zpci_device *pdev)
{
char *buffer = NULL;
size_t count = 0;
char *cmd;
FILE *fd;
if (!pdev->device)
return NULL;
util_asprintf(&cmd, SMARTCTL_CMDLINE, pdev->device);
fd = popen(cmd, "r");
if (!fd)
goto out;
while (!feof(fd)) {
buffer = realloc(buffer, count + READ_CHUNK_SIZE);
if (!buffer) {
warnx("Could not collect S.M.A.R.T. data");
goto out;
}
count += fread(&buffer[count], 1, READ_CHUNK_SIZE, fd);
if (ferror(fd)) {
free(buffer);
buffer = NULL;
goto out;
}
}
buffer = realloc(buffer, count);
if (!buffer && count > 0)
warnx("Could not collect S.M.A.R.T. data");
if (buffer)
buffer[count] = '\0';
out:
pclose(fd);
free(cmd);
return buffer;
}
static unsigned int sysfs_read_value(struct zpci_device *pdev, const char *attr)
{
unsigned int val;
char *path;
FILE *fp;
path = util_path_sysfs("bus/pci/devices/%s/%s", pdev->slot, attr);
fp = fopen(path, "r");
if (!fp)
fopen_err(path);
if (fscanf(fp, "%x", &val) != 1) {
fread_err(fp, path);
}
if (fclose(fp))
fclose_err(path);
free(path);
return val;
}
static void sysfs_write_value(struct zpci_device *pdev, const char *attr,
unsigned int val)
{
char *path;
FILE *fp;
path = util_path_sysfs("bus/pci/devices/%s/%s", pdev->slot, attr);
fp = fopen(path, "w");
if (!fp)
fopen_err(path);
if (fprintf(fp, "%x", val) < 0) {
fwrite_err(fp, path);
}
if (fclose(fp))
fclose_err(path);
free(path);
}
static void sysfs_report_error(struct zpci_report_error *report, char *slot)
{
size_t r_size;
char *path;
FILE *fp;
r_size = sizeof(*report);
path = util_path_sysfs("bus/pci/devices/%s/report_error", slot);
fp = fopen(path, "w");
if (!fp)
fopen_err(path);
if (fwrite(report, 1, r_size, fp) != r_size)
fwrite_err(fp, path);
if (fclose(fp))
fclose_err(path);
free(path);
}
static void get_device_node(struct zpci_device *pdev)
{
struct dirent **de_vec;
char *path, *dev;
char slot[13];
int count, i;
path = util_path_sysfs("bus/pci/devices/%s/nvme", pdev->slot);
count = util_scandir(&de_vec, alphasort, path, "nvme*");
if (count == -1) {
warnx("Could not read directory %s: %s", path, strerror(errno));
free(path);
return;
}
for (i = 0; i < count; i++) {
util_asprintf(&dev, "/dev/%s", de_vec[i]->d_name);
if (util_sys_get_dev_addr(dev, slot) != 0)
continue;
if (strcmp(slot, pdev->slot) == 0) {
pdev->device = dev;
break;
}
}
util_scandir_free(de_vec, count);
free(path);
}
static int device_exists(char *dev)
{
char *path;
int rc = 0;
/* In case a device node is specified, this will be sufficiant */
if (util_path_exists(dev) && !util_path_is_dir(dev))
return 1;
path = util_path_sysfs("bus/pci/devices/%s", dev);
if (util_path_exists(path))
rc = 1;
free(path);
return rc;
}
static void get_device_info(struct zpci_device *pdev, char *dev)
{
if (!device_exists(dev))
errx(EXIT_FAILURE, "Could not find device %s", dev);
if (is_blk_dev(dev))
errx(EXIT_FAILURE, "Unsupported device type %s", dev);
if (is_char_dev(dev)) {
if (util_sys_get_dev_addr(dev, pdev->slot) != 0)
errx(EXIT_FAILURE,
"Could not determine slot address for %s", dev);
pdev->device = dev;
} else {
strcpy(pdev->slot, dev);
}
pdev->class = sysfs_read_value(pdev, "class");
pdev->fid = sysfs_read_value(pdev, "function_id");
pdev->pchid = sysfs_read_value(pdev, "pchid");
/*
* In case a slot address was specified, the device node for NVMe
* devices is still needed. Otherwise it won't be possible to collect
* S.M.A.R.T. data at a later point.
*/
if (!pdev->device && pdev->class == PCI_CLASS_NVME)
get_device_node(pdev);
}
/*
* Issue an SCLP Adapter Error Notification event with a specific action
* qualifier.
*
* Collect additional information when possible (e.g. S.M.A.R.T. data for NVMe
* devices).
*/
static void sclp_issue_action(struct zpci_device *pdev, int action)
{
struct zpci_report_error report = {
.header = { 0 },
.data = { 0 }
};
char *sdata = NULL;
report.header.version = 1;
report.header.action = action;
report.header.length = sizeof(report.data);
report.data.timestamp = (__u64)time(NULL);
report.data.err_log_id = 0x4713;
if (pdev->class == PCI_CLASS_NVME)
sdata = collect_smart_data(pdev);
if (sdata) {
util_strlcpy(report.data.log_data, sdata,
sizeof(report.data.log_data));
free(sdata);
}
sysfs_report_error(&report, pdev->slot);
}
/*
* Reset the PCI device and initiate a re-initialization.
*/
static void sclp_reset_device(struct zpci_device *pdev)
{
sclp_issue_action(pdev, SCLP_ERRNOTIFY_AQ_RESET);
sysfs_write_value(pdev, "recover", 1);
}
/*
* De-Configure/repair PCI device. Moves the device from configured
* to reserved state.
*/
static void sclp_deconfigure(struct zpci_device *pdev)
{
sclp_issue_action(pdev, SCLP_ERRNOTIFY_AQ_DECONF);
}
/*
* Report an error to the SE.
*/
static void sclp_report_error(struct zpci_device *pdev)
{
sclp_issue_action(pdev, SCLP_ERRNOTIFY_AQ_REPORT_ERR);
}
static void parse_cmdline(int argc, char *argv[], struct options *opts)
{
int cmd;
util_prg_init(&prg);
util_opt_init(opt_vec, NULL);
do {
cmd = util_opt_getopt_long(argc, argv);
switch (cmd) {
case OPT_RESET:
opts->reset = 1;
break;
case OPT_DECONF:
opts->deconfigure = 1;
break;
case OPT_REPORT_ERR:
opts->report = 1;
break;
case 'h':
util_prg_print_help();
util_opt_print_help();
exit(EXIT_SUCCESS);
case 'v':
util_prg_print_version();
exit(EXIT_SUCCESS);
case -1:
/* End of options string */
if (argc == 1) {
errx(EXIT_FAILURE,
"Use '%s --help' for more information",
argv[0]);
}
break;
}
} while (cmd != -1);
}
int main(int argc, char *argv[])
{
struct zpci_device pdev = { 0 };
struct options opts = { 0 };
parse_cmdline(argc, argv, &opts);
if (optind >= argc)
errx(EXIT_FAILURE, "No device specified");
get_device_info(&pdev, argv[optind]);
if (opts.reset)
sclp_reset_device(&pdev);
else if (opts.deconfigure)
sclp_deconfigure(&pdev);
else if (opts.report)
sclp_report_error(&pdev);
return 0;
}
|