1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1999 Michael Smith
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	$FreeBSD: stable/11/usr.sbin/mlxcontrol/util.c 330449 2018-03-05 07:26:05Z eadler $
29 */
30
31#include <sys/types.h>
32#include <stdio.h>
33#include <paths.h>
34#include <string.h>
35
36#include <dev/mlx/mlxio.h>
37#include <dev/mlx/mlxreg.h>
38
39#include "mlxcontrol.h"
40
41/********************************************************************************
42 * Various name-producing and -parsing functions
43 */
44
45/* return path of controller (unit) */
46char *
47ctrlrpath(int unit)
48{
49    static char	buf[32];
50
51    sprintf(buf, "%s%s", _PATH_DEV, ctrlrname(unit));
52    return(buf);
53}
54
55/* return name of controller (unit) */
56char *
57ctrlrname(int unit)
58{
59    static char	buf[32];
60
61    sprintf(buf, "mlx%d", unit);
62    return(buf);
63}
64
65/* return path of drive (unit) */
66char *
67drivepath(int unit)
68{
69    static char	buf[32];
70
71    sprintf(buf, "%s%s", _PATH_DEV, drivename(unit));
72    return(buf);
73}
74
75/* return name of drive (unit) */
76char *
77drivename(int unit)
78{
79    static char	buf[32];
80
81    sprintf(buf, "mlxd%d", unit);
82    return(buf);
83}
84
85/* get controller unit number from name in (str) */
86int
87ctrlrunit(char *str)
88{
89    int		unit;
90
91    if (sscanf(str, "mlx%d", &unit) == 1)
92	return(unit);
93    return(-1);
94}
95
96/* get drive unit number from name in (str) */
97int
98driveunit(char *str)
99{
100    int		unit;
101
102    if (sscanf(str, "mlxd%d", &unit) == 1)
103	return(unit);
104    return(-1);
105}
106
107/********************************************************************************
108 * Standardised output of various data structures.
109 */
110
111void
112mlx_print_phys_drv(struct mlx_phys_drv *drv, int chn, int targ, char *prefix, int verbose)
113{
114    char	*type, *device, *vendor, *revision;
115
116    switch(drv->pd_flags2 & 0x03) {
117    case MLX_PHYS_DRV_DISK:
118	type = "disk";
119	break;
120    case MLX_PHYS_DRV_SEQUENTIAL:
121	type = "tape";
122	break;
123    case MLX_PHYS_DRV_CDROM:
124	type= "cdrom";
125	break;
126    case MLX_PHYS_DRV_OTHER:
127    default:
128	type = "unknown";
129	break;
130    }
131    printf("%s%s%02d%02d ", prefix, type, chn, targ);
132    switch(drv->pd_status) {
133    case MLX_PHYS_DRV_DEAD:
134	printf(" (dead)       ");
135	break;
136    case MLX_PHYS_DRV_WRONLY:
137	printf(" (write-only) ");
138	break;
139    case MLX_PHYS_DRV_ONLINE:
140	printf(" (online)     ");
141	break;
142    case MLX_PHYS_DRV_STANDBY:
143	printf(" (standby)    ");
144	break;
145    default:
146	printf(" (0x%02x)   ", drv->pd_status);
147    }
148    printf("\n");
149
150    if (verbose) {
151
152	printf("%s   ", prefix);
153	if (!mlx_scsi_inquiry(0, chn, targ, &vendor, &device, &revision)) {
154	    printf("'%8.8s' '%16.16s' '%4.4s'", vendor, device, revision);
155	} else {
156	    printf("<IDENTIFY FAILED>");
157	}
158
159	printf(" %dMB ", drv->pd_config_size / 2048);
160
161	if (drv->pd_flags2 & MLX_PHYS_DRV_FAST20) {
162	    printf(" fast20");
163	} else if (drv->pd_flags2 & MLX_PHYS_DRV_FAST) {
164	    printf(" fast");
165	}
166	if (drv->pd_flags2 & MLX_PHYS_DRV_WIDE)
167	    printf(" wide");
168	if (drv->pd_flags2 & MLX_PHYS_DRV_SYNC)
169	    printf(" sync");
170	if (drv->pd_flags2 & MLX_PHYS_DRV_TAG)
171	    printf(" tag-enabled");
172	printf("\n");
173    }
174}
175