Blame IbaTools/oparesolvehfiport/oparesolvehfiport.c

Packit 857059
/* BEGIN_ICS_COPYRIGHT7 ****************************************
Packit 857059
Packit 857059
Copyright (c) 2015-2017, Intel Corporation
Packit 857059
Packit 857059
Redistribution and use in source and binary forms, with or without
Packit 857059
modification, are permitted provided that the following conditions are met:
Packit 857059
Packit 857059
    * Redistributions of source code must retain the above copyright notice,
Packit 857059
      this list of conditions and the following disclaimer.
Packit 857059
    * Redistributions in binary form must reproduce the above copyright
Packit 857059
      notice, this list of conditions and the following disclaimer in the
Packit 857059
      documentation and/or other materials provided with the distribution.
Packit 857059
    * Neither the name of Intel Corporation nor the names of its contributors
Packit 857059
      may be used to endorse or promote products derived from this software
Packit 857059
      without specific prior written permission.
Packit 857059
Packit 857059
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 857059
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 857059
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit 857059
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
Packit 857059
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 857059
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit 857059
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Packit 857059
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit 857059
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 857059
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 857059
Packit 857059
** END_ICS_COPYRIGHT7   ****************************************/
Packit 857059
Packit 857059
/* [ICS VERSION STRING: unknown] */
Packit 857059
Packit 857059
Packit 857059
Packit 857059
#include <stdio.h>
Packit 857059
#include <stdlib.h>
Packit 857059
#include <unistd.h>
Packit 857059
#include <errno.h>
Packit 857059
#define _GNU_SOURCE
Packit 857059
#include <getopt.h>
Packit 857059
Packit 857059
#include <iba/ibt.h>
Packit 857059
#include <opamgt_priv.h>
Packit 857059
Packit 857059
void Usage(int exitcode)
Packit 857059
{
Packit 857059
	fprintf(stderr, "Usage: oparesolvehfiport [-o output] [hfi] [port]\n");
Packit 857059
	fprintf(stderr, "            or\n");
Packit 857059
	fprintf(stderr, "       oparesolvehfiport --help\n");
Packit 857059
	fprintf(stderr, "    hfi         - hfi, numbered 1..n, 0 = system wide port num\n");
Packit 857059
	fprintf(stderr, "                  (default is 0)\n");
Packit 857059
	fprintf(stderr, "    port        - port, numbered 1..n, 0 = 1st active\n");
Packit 857059
 	fprintf(stderr, "                  (default is 0)\n");
Packit 857059
	fprintf(stderr, "    -o/--output - output type\n");
Packit 857059
	fprintf(stderr, "\n");
Packit 857059
	fprintf(stderr, "Output Type:\n");
Packit 857059
	fprintf(stderr, "    devname - prints the device name, in the format hfiname:portnum\n");
Packit 857059
	fprintf(stderr, "              (default)\n");
Packit 857059
        fprintf(stderr, "    hfinum  - prints the hfi number\n");
Packit 857059
	fprintf(stderr, "\n");
Packit 857059
	fprintf(stderr, "The hfi and port permit a variety of selections:\n");
Packit 857059
	fprintf(stderr, "  0 0     - 1st active port in system\n");
Packit 857059
	fprintf(stderr, "  x 0     - 1st active port on HFI x\n");
Packit 857059
	fprintf(stderr, "  0 y     - port y within system (irrespective of which ports are active)\n");
Packit 857059
	fprintf(stderr, "  x y     - HFI x, port y\n");
Packit 857059
	fprintf(stderr, "\n");
Packit 857059
	fprintf(stderr, "Example:\n");
Packit 857059
	fprintf(stderr, "    oparesolvehfiport 0 1                #Output: hfi1_0:1\n");
Packit 857059
	fprintf(stderr, "    oparesolvehfiport -o devname 0 1     #Output: hfi1_0:1\n");
Packit 857059
	fprintf(stderr, "    oparesolvehfiport -o hfinum 0 1      #Output: 1\n");
Packit 857059
Packit 857059
	exit(exitcode);
Packit 857059
}
Packit 857059
Packit 857059
int main(int argc, char ** argv)
Packit 857059
{
Packit 857059
	FSTATUS             fstatus;
Packit 857059
	uint8               hfi         = 0;
Packit 857059
	uint8               port        = 0;
Packit 857059
	EUI64               portGuid    = -1;
Packit 857059
	int c;
Packit 857059
	int devName_out = 1; //default output
Packit 857059
	int hfiNum, hfiNum_out = 0;
Packit 857059
	const char *options = "o:";
Packit 857059
	const struct option longopts[] = {{"help", 0, 0, '$'},
Packit 857059
						{"output", required_argument, 0, 'o'},
Packit 857059
						{0, 0, 0, 0}};
Packit 857059
	uint32 caCount, portCount;
Packit 857059
	char fiName[255];
Packit 857059
	int caPort;
Packit 857059
Packit 857059
	while (-1 != (c = getopt_long(argc, argv, options, longopts, NULL))) {
Packit 857059
		switch (c) {
Packit 857059
		case '$':
Packit 857059
			Usage(0);
Packit 857059
			break;
Packit 857059
		case 'o':
Packit 857059
			if (strcmp(optarg, "hfinum") == 0) {
Packit 857059
				hfiNum_out = 1;
Packit 857059
			}
Packit 857059
			else if (strcmp(optarg, "devname") == 0) {
Packit 857059
				devName_out = 1;
Packit 857059
			}
Packit 857059
			else {
Packit 857059
				fprintf(stderr, "oparesolvehfiport: Invalid Output Type\n");
Packit 857059
				Usage(2);
Packit 857059
			}
Packit 857059
			break;
Packit 857059
		default:
Packit 857059
			Usage(2);
Packit 857059
		}
Packit 857059
	}
Packit 857059
Packit 857059
	if (argc > optind) {
Packit 857059
		if (FSUCCESS != StringToUint8(&hfi, argv[optind], NULL, 0, TRUE)) {
Packit 857059
			fprintf(stderr, "oparesolvehfiport: Invalid HFI Number: %s\n", argv[optind]);
Packit 857059
			Usage(2);
Packit 857059
		}
Packit 857059
	}
Packit 857059
	if (argc > optind+1) {
Packit 857059
		if (FSUCCESS != StringToUint8(&port, argv[optind+1], NULL, 0, TRUE)) {
Packit 857059
			fprintf(stderr, "oparesolvehfiport: Invalid Port Number: %s\n", argv[optind+1]);
Packit 857059
			Usage(2);
Packit 857059
		}
Packit 857059
	}
Packit 857059
Packit 857059
	// find portGuid specified
Packit 857059
	fstatus = omgt_get_portguid(hfi, port, NULL, NULL, NULL, &portGuid, NULL, NULL,
Packit 857059
								&caCount, &portCount, fiName, &caPort, NULL);
Packit 857059
Packit 857059
	if (FNOT_FOUND == fstatus) {
Packit 857059
		fprintf(stderr, "oparesolvehfiport: %s\n",
Packit 857059
			iba_format_get_portguid_error(hfi, port, caCount, portCount));
Packit 857059
		return 1;
Packit 857059
	} else if (FSUCCESS != fstatus) {
Packit 857059
		fprintf(stderr, "opasaquery: iba_get_portguid Failed: %s\n", iba_fstatus_msg(fstatus));
Packit 857059
		return 1;
Packit 857059
	}
Packit 857059
    //printf("PORT GUID 0x%016"PRIx64"\n",portGuid);
Packit 857059
Packit 857059
	if(hfiNum_out) {
Packit 857059
		hfiNum = omgt_get_hfi_num(fiName);
Packit 857059
		printf("%d\n", hfiNum);
Packit 857059
	}
Packit 857059
	else if(devName_out) {
Packit 857059
		printf("%s:%d\n", fiName, caPort);
Packit 857059
	}
Packit 857059
Packit 857059
	return 0;
Packit 857059
}