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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
/*
This file is provided under a dual BSD/GPLv2 license. When using or
redistributing this file, you may do so under either license.
GPL LICENSE SUMMARY
Copyright(c) 2015 Intel Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
Contact Information:
Intel Corporation, www.intel.com
BSD LICENSE
Copyright(c) 2015 Intel Corporation.
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.
*/
/* Copyright (c) 2003-2014 Intel Corporation. All rights reserved. */
/* This file contains a simple sysfs interface used by the low level
hfi protocol code. It also implements the interface to hfifs. */
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "opa_service.h"
static char *sysfs_path;
static size_t sysfs_path_len;
static char *hfifs_path;
static long sysfs_page_size;
int sysfs_init(const char *dflt_hfi_class_path)
{
int rv = 0;
if (NULL != (sysfs_path = getenv("HFI_SYSFS_PATH")))
{
char *syspath = strdup(sysfs_path);
if (!syspath)
{
_HFI_DBG("Failed to strdup(\"%s\") for syspath.\n",
sysfs_path);
rv = -1;
}
else
sysfs_path = syspath;
}
if (sysfs_path == NULL) {
unsigned len = strlen(dflt_hfi_class_path) + 4;
char *syspath = malloc(len);
if (!syspath)
{
_HFI_DBG("Failed to alloc %u bytes for syspath.\n",len);
rv = -1;
}
else
{
snprintf(syspath, len, "%s_0", dflt_hfi_class_path);
sysfs_path = syspath;
}
}
if (sysfs_path != NULL) {
struct stat s;
if (stat(sysfs_path, &s) || !S_ISDIR(s.st_mode))
{
_HFI_DBG("Did not find sysfs directory %s, using anyway\n",
sysfs_path);
rv = -1;
}
else
{
/* Remove the unit number from the sysfs path: */
char *lastUS = strrchr(sysfs_path, '_');
if ((NULL != lastUS) && (isdigit(lastUS[1])))
lastUS[1] = 0;
}
}
if (sysfs_path != NULL)
sysfs_path_len = strlen(sysfs_path);
if (hfifs_path == NULL)
hfifs_path = getenv("HFI_HFIFS_PATH");
if (hfifs_path == NULL)
hfifs_path = "/hfifs";
if (!sysfs_page_size)
sysfs_page_size = sysconf(_SC_PAGESIZE);
return rv;
}
void sysfs_fini(void)
{
free(sysfs_path);
}
const char *hfi_sysfs_path(void)
{
return sysfs_path;
}
size_t hfi_sysfs_path_len(void)
{
return sysfs_path_len;
}
const char *hfi_hfifs_path(void)
{
return hfifs_path;
}
int hfi_hfifs_open(const char *attr, int flags)
{
char buf[1024];
int saved_errno;
int fd;
snprintf(buf, sizeof(buf), "%s/%s", hfi_hfifs_path(), attr);
fd = open(buf, flags);
saved_errno = errno;
if (fd == -1) {
_HFI_DBG("Failed to open driver attribute '%s': %s\n", attr,
strerror(errno));
_HFI_DBG("Offending file name: %s\n", buf);
}
errno = saved_errno;
return fd;
}
int hfi_sysfs_unit_open(uint32_t unit, const char *attr, int flags)
{
int saved_errno;
char buf[1024];
int fd;
snprintf(buf, sizeof(buf), "%s%u/%s", hfi_sysfs_path(), unit, attr);
fd = open(buf, flags);
saved_errno = errno;
if (fd == -1) {
_HFI_DBG("Failed to open attribute '%s' of unit %d: %s\n", attr,
unit, strerror(errno));
_HFI_DBG("Offending file name: %s\n", buf);
}
errno = saved_errno;
return fd;
}
static int hfi_sysfs_unit_open_for_node(uint32_t unit, int flags)
{
int saved_errno;
char buf[1024];
int fd;
snprintf(buf, sizeof(buf), "%s%u/device/numa_node",
hfi_sysfs_path(), unit);
fd = open(buf, flags);
saved_errno = errno;
if (fd == -1) {
_HFI_DBG("Failed to open attribute numa_node of unit %d: %s\n",
unit, strerror(errno));
_HFI_DBG("Offending file name: %s\n", buf);
}
errno = saved_errno;
return fd;
}
int hfi_sysfs_port_open(uint32_t unit, uint32_t port, const char *attr,
int flags)
{
int saved_errno;
char buf[1024];
int fd;
snprintf(buf, sizeof(buf), "%s%u/ports/%u/%s", hfi_sysfs_path(),
unit, port, attr);
fd = open(buf, flags);
saved_errno = errno;
if (fd == -1) {
_HFI_DBG("Failed to open attribute '%s' of unit %d:%d: %s\n",
attr, unit, port, strerror(errno));
_HFI_DBG("Offending file name: %s\n", buf);
}
errno = saved_errno;
return fd;
}
int hfi_hfifs_unit_open(uint32_t unit, const char *attr, int flags)
{
int saved_errno;
char buf[1024];
int fd;
snprintf(buf, sizeof(buf), "%s/%u/%s", hfi_hfifs_path(), unit, attr);
fd = open(buf, flags);
saved_errno = errno;
if (fd == -1) {
_HFI_DBG("Failed to open attribute '%s' of unit %d: %s\n", attr,
unit, strerror(errno));
_HFI_DBG("Offending file name: %s\n", buf);
}
errno = saved_errno;
return fd;
}
static int read_page(int fd, char **datap)
{
char *data = NULL;
int saved_errno;
int ret = -1;
data = malloc(sysfs_page_size);
saved_errno = errno;
if (!data) {
_HFI_DBG("Could not allocate memory: %s\n", strerror(errno));
goto bail;
}
ret = read(fd, data, sysfs_page_size);
saved_errno = errno;
if (ret == -1) {
_HFI_DBG("Read of attribute failed: %s\n", strerror(errno));
goto bail;
}
bail:
if (ret == -1) {
free(data);
} else {
if (ret < sysfs_page_size)
data[ret] = 0;
else
data[sysfs_page_size-1] = 0;
*datap = data;
}
errno = saved_errno;
return ret;
}
/*
* On return, caller must free *datap.
*/
int hfi_sysfs_unit_read(uint32_t unit, const char *attr, char **datap)
{
int fd = -1, ret = -1;
int saved_errno;
fd = hfi_sysfs_unit_open(unit, attr, O_RDONLY);
saved_errno = errno;
if (fd == -1)
goto bail;
ret = read_page(fd, datap);
saved_errno = errno;
bail:
if (ret == -1)
*datap = NULL;
if (fd != -1) {
close(fd);
}
errno = saved_errno;
return ret;
}
/* read a string value into buff, no more than size bytes.
returns the number of bytes read */
size_t hfi_sysfs_unit_port_read(uint32_t unit, uint32_t port, const char *attr,
char *buff, size_t size)
{
int fd = -1;
size_t rv = 0;
fd = hfi_sysfs_port_open(unit, port, attr, O_RDONLY);
if (fd == -1)
return rv;
rv = read(fd, buff, size);
close(fd);
if (rv < size)
buff[rv] = 0;
else
buff[size-1] = 0;
return rv;
}
/*
* On return, caller must free *datap.
*/
int hfi_sysfs_port_read(uint32_t unit, uint32_t port, const char *attr,
char **datap)
{
int fd = -1, ret = -1;
int saved_errno;
fd = hfi_sysfs_port_open(unit, port, attr, O_RDONLY);
saved_errno = errno;
if (fd == -1)
goto bail;
ret = read_page(fd, datap);
saved_errno = errno;
bail:
if (ret == -1)
*datap = NULL;
if (fd != -1) {
close(fd);
}
errno = saved_errno;
return ret;
}
/*
* On return, caller must free *datap.
*/
int hfi_hfifs_read(const char *attr, char **datap)
{
int fd = -1, ret = -1;
int saved_errno;
fd = hfi_hfifs_open(attr, O_RDONLY);
saved_errno = errno;
if (fd == -1)
goto bail;
ret = read_page(fd, datap);
saved_errno = errno;
bail:
if (ret == -1)
*datap = NULL;
if (fd != -1) {
close(fd);
}
errno = saved_errno;
return ret;
}
/*
* On return, caller must free *datap.
*/
int hfi_hfifs_unit_read(uint32_t unit, const char *attr, char **datap)
{
int fd = -1, ret = -1;
int saved_errno;
fd = hfi_hfifs_unit_open(unit, attr, O_RDONLY);
saved_errno = errno;
if (fd == -1)
goto bail;
ret = read_page(fd, datap);
saved_errno = errno;
bail:
if (ret == -1)
*datap = NULL;
if (fd != -1) {
close(fd);
}
errno = saved_errno;
return ret;
}
/*
* The _rd routines jread directly into a supplied buffer,
* unlike the _read routines.
*/
int hfi_hfifs_rd(const char *attr, void *buf, int n)
{
int fd = -1, ret = -1;
int saved_errno;
fd = hfi_hfifs_open(attr, O_RDONLY);
saved_errno = errno;
if (fd == -1)
goto bail;
ret = read(fd, buf, n);
saved_errno = errno;
bail:
if (fd != -1) {
close(fd);
}
errno = saved_errno;
return ret;
}
int hfi_hfifs_unit_rd(uint32_t unit, const char *attr, void *buf, int n)
{
int fd = -1, ret = -1;
int saved_errno;
fd = hfi_hfifs_unit_open(unit, attr, O_RDONLY);
saved_errno = errno;
if (fd == -1)
goto bail;
ret = read(fd, buf, n);
saved_errno = errno;
bail:
if (fd != -1) {
close(fd);
}
errno = saved_errno;
return ret;
}
int hfi_sysfs_unit_read_s64(uint32_t unit, const char *attr,
int64_t *valp, int base)
{
char *data=NULL, *end;
int saved_errno;
long long val;
int ret;
ret = hfi_sysfs_unit_read(unit, attr, &data);
saved_errno = errno;
if (ret == -1) {
goto bail;
}
val = strtoll(data, &end, base);
saved_errno = errno;
if (!*data || !(*end == '\0' || isspace(*end))) {
ret = -1;
goto bail;
}
*valp = val;
ret = 0;
bail:
if (data)
free(data);
errno = saved_errno;
return ret;
}
static int hfi_sysfs_unit_read_node(uint32_t unit, char **datap)
{
int fd = -1, ret = -1;
int saved_errno;
fd = hfi_sysfs_unit_open_for_node(unit, O_RDONLY);
saved_errno = errno;
if (fd == -1)
goto bail;
ret = read_page(fd, datap);
if (ret == -1)
*datap = NULL;
saved_errno = errno;
close(fd);
bail:
errno = saved_errno;
return ret;
}
int64_t hfi_sysfs_unit_read_node_s64(uint32_t unit)
{
char *data=NULL, *end;
int saved_errno;
long long val;
int64_t ret = -1;
saved_errno = errno;
if (hfi_sysfs_unit_read_node(unit, &data) == -1) {
goto bail;
}
val = strtoll(data, &end, 0);
saved_errno = errno;
if (!*data || !(*end == '\0' || isspace(*end))) {
ret = -1;
goto bail;
}
ret = (int64_t) val;
bail:
free(data);
errno = saved_errno;
return ret;
}
int hfi_sysfs_port_read_s64(uint32_t unit, uint32_t port, const char *attr,
int64_t *valp, int base)
{
char *data, *end;
int saved_errno;
long long val;
int ret;
ret = hfi_sysfs_port_read(unit, port, attr, &data);
saved_errno = errno;
if (ret == -1) {
goto bail;
}
val = strtoll(data, &end, base);
saved_errno = errno;
if (!*data || !(*end == '\0' || isspace(*end))) {
ret = -1;
goto bail;
}
*valp = val;
ret = 0;
bail:
free(data);
errno = saved_errno;
return ret;
}
|