159138Smsmith/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
459138Smsmith * Copyright (c) 1999 Michael Smith
559138Smsmith * All rights reserved.
659138Smsmith *
759138Smsmith * Redistribution and use in source and binary forms, with or without
859138Smsmith * modification, are permitted provided that the following conditions
959138Smsmith * are met:
1059138Smsmith * 1. Redistributions of source code must retain the above copyright
1159138Smsmith *    notice, this list of conditions and the following disclaimer.
1259138Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1359138Smsmith *    notice, this list of conditions and the following disclaimer in the
1459138Smsmith *    documentation and/or other materials provided with the distribution.
1559138Smsmith *
1659138Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1759138Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1859138Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1959138Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2059138Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2159138Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2259138Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2359138Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2459138Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2559138Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2659138Smsmith * SUCH DAMAGE.
2759138Smsmith *
2859138Smsmith *	$FreeBSD: stable/11/usr.sbin/mlxcontrol/util.c 330449 2018-03-05 07:26:05Z eadler $
2959138Smsmith */
3059138Smsmith
3159138Smsmith#include <sys/types.h>
3259138Smsmith#include <stdio.h>
3359138Smsmith#include <paths.h>
3459138Smsmith#include <string.h>
3559138Smsmith
3659159Smsmith#include <dev/mlx/mlxio.h>
3759159Smsmith#include <dev/mlx/mlxreg.h>
3859138Smsmith
3959138Smsmith#include "mlxcontrol.h"
4059138Smsmith
4159138Smsmith/********************************************************************************
4259138Smsmith * Various name-producing and -parsing functions
4359138Smsmith */
4459138Smsmith
4559138Smsmith/* return path of controller (unit) */
4659138Smsmithchar *
4759138Smsmithctrlrpath(int unit)
4859138Smsmith{
4959138Smsmith    static char	buf[32];
5059138Smsmith
5159138Smsmith    sprintf(buf, "%s%s", _PATH_DEV, ctrlrname(unit));
5259138Smsmith    return(buf);
5359138Smsmith}
5459138Smsmith
5559138Smsmith/* return name of controller (unit) */
5659138Smsmithchar *
5759138Smsmithctrlrname(int unit)
5859138Smsmith{
5959138Smsmith    static char	buf[32];
6059138Smsmith
6159138Smsmith    sprintf(buf, "mlx%d", unit);
6259138Smsmith    return(buf);
6359138Smsmith}
6459138Smsmith
6559138Smsmith/* return path of drive (unit) */
6659138Smsmithchar *
6759138Smsmithdrivepath(int unit)
6859138Smsmith{
6959138Smsmith    static char	buf[32];
7059138Smsmith
7159138Smsmith    sprintf(buf, "%s%s", _PATH_DEV, drivename(unit));
7259138Smsmith    return(buf);
7359138Smsmith}
7459138Smsmith
7559138Smsmith/* return name of drive (unit) */
7659138Smsmithchar *
7759138Smsmithdrivename(int unit)
7859138Smsmith{
7959138Smsmith    static char	buf[32];
8059138Smsmith
8159138Smsmith    sprintf(buf, "mlxd%d", unit);
8259138Smsmith    return(buf);
8359138Smsmith}
8459138Smsmith
8559138Smsmith/* get controller unit number from name in (str) */
8659138Smsmithint
8759138Smsmithctrlrunit(char *str)
8859138Smsmith{
8959138Smsmith    int		unit;
9059138Smsmith
9159138Smsmith    if (sscanf(str, "mlx%d", &unit) == 1)
9259138Smsmith	return(unit);
9359138Smsmith    return(-1);
9459138Smsmith}
9559138Smsmith
9659138Smsmith/* get drive unit number from name in (str) */
9759138Smsmithint
9859138Smsmithdriveunit(char *str)
9959138Smsmith{
10059138Smsmith    int		unit;
10159138Smsmith
10259138Smsmith    if (sscanf(str, "mlxd%d", &unit) == 1)
10359138Smsmith	return(unit);
10459138Smsmith    return(-1);
10559138Smsmith}
10659138Smsmith
10759138Smsmith/********************************************************************************
10859138Smsmith * Standardised output of various data structures.
10959138Smsmith */
11059138Smsmith
11159138Smsmithvoid
11259138Smsmithmlx_print_phys_drv(struct mlx_phys_drv *drv, int chn, int targ, char *prefix, int verbose)
11359138Smsmith{
11459138Smsmith    char	*type, *device, *vendor, *revision;
11559138Smsmith
11659138Smsmith    switch(drv->pd_flags2 & 0x03) {
11759138Smsmith    case MLX_PHYS_DRV_DISK:
11859138Smsmith	type = "disk";
11959138Smsmith	break;
12059138Smsmith    case MLX_PHYS_DRV_SEQUENTIAL:
12159138Smsmith	type = "tape";
12259138Smsmith	break;
12359138Smsmith    case MLX_PHYS_DRV_CDROM:
12459138Smsmith	type= "cdrom";
12559138Smsmith	break;
12659138Smsmith    case MLX_PHYS_DRV_OTHER:
12759138Smsmith    default:
12859138Smsmith	type = "unknown";
12959138Smsmith	break;
13059138Smsmith    }
13159138Smsmith    printf("%s%s%02d%02d ", prefix, type, chn, targ);
13259138Smsmith    switch(drv->pd_status) {
13359138Smsmith    case MLX_PHYS_DRV_DEAD:
13459138Smsmith	printf(" (dead)       ");
13559138Smsmith	break;
13659138Smsmith    case MLX_PHYS_DRV_WRONLY:
13759138Smsmith	printf(" (write-only) ");
13859138Smsmith	break;
13959138Smsmith    case MLX_PHYS_DRV_ONLINE:
14059138Smsmith	printf(" (online)     ");
14159138Smsmith	break;
14259138Smsmith    case MLX_PHYS_DRV_STANDBY:
14359138Smsmith	printf(" (standby)    ");
14459138Smsmith	break;
14559138Smsmith    default:
14659138Smsmith	printf(" (0x%02x)   ", drv->pd_status);
14759138Smsmith    }
14859138Smsmith    printf("\n");
14959138Smsmith
15059138Smsmith    if (verbose) {
15159138Smsmith
15259138Smsmith	printf("%s   ", prefix);
15359138Smsmith	if (!mlx_scsi_inquiry(0, chn, targ, &vendor, &device, &revision)) {
15459138Smsmith	    printf("'%8.8s' '%16.16s' '%4.4s'", vendor, device, revision);
15559138Smsmith	} else {
15659138Smsmith	    printf("<IDENTIFY FAILED>");
15759138Smsmith	}
15859138Smsmith
15959138Smsmith	printf(" %dMB ", drv->pd_config_size / 2048);
16059138Smsmith
16159138Smsmith	if (drv->pd_flags2 & MLX_PHYS_DRV_FAST20) {
16259138Smsmith	    printf(" fast20");
16359138Smsmith	} else if (drv->pd_flags2 & MLX_PHYS_DRV_FAST) {
16459138Smsmith	    printf(" fast");
16559138Smsmith	}
16659138Smsmith	if (drv->pd_flags2 & MLX_PHYS_DRV_WIDE)
16759138Smsmith	    printf(" wide");
16859138Smsmith	if (drv->pd_flags2 & MLX_PHYS_DRV_SYNC)
16959138Smsmith	    printf(" sync");
17059138Smsmith	if (drv->pd_flags2 & MLX_PHYS_DRV_TAG)
17159138Smsmith	    printf(" tag-enabled");
17259138Smsmith	printf("\n");
17359138Smsmith    }
17459138Smsmith}
175