1/* $NetBSD: dklist.c,v 1.10 2014/06/11 14:51:49 joerg Exp $ */ 2 3/*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32/* 33 * Copyright (c) 1996 John M. Vinopal 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed for the NetBSD Project 47 * by John M. Vinopal. 48 * 4. The name of the author may not be used to endorse or promote products 49 * derived from this software without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 56 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 58 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 59 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 */ 63 64#ifndef lint 65#include <sys/cdefs.h> 66__RCSID("$NetBSD: dklist.c,v 1.10 2014/06/11 14:51:49 joerg Exp $"); 67#endif /* not lint */ 68 69#include <sys/types.h> 70#include <sys/iostat.h> 71#include <sys/ioctl.h> 72#include <sys/sysctl.h> 73 74#include <dev/ic/mlxreg.h> 75#include <dev/ic/mlxio.h> 76 77#include <ctype.h> 78#include <err.h> 79#include <errno.h> 80#include <fcntl.h> 81#include <limits.h> 82#include <stdio.h> 83#include <stdlib.h> 84#include <string.h> 85#include <unistd.h> 86#include <util.h> 87 88#include "extern.h" 89 90static SIMPLEQ_HEAD(, mlx_disk) mlx_disks; 91 92void 93mlx_disk_init(void) 94{ 95 96 SIMPLEQ_INIT(&mlx_disks); 97} 98 99int 100mlx_disk_empty(void) 101{ 102 103 return (SIMPLEQ_EMPTY(&mlx_disks)); 104} 105 106void 107mlx_disk_iterate(void (*func)(struct mlx_disk *)) 108{ 109 struct mlx_disk *md; 110 111 SIMPLEQ_FOREACH(md, &mlx_disks, chain) 112 (*func)(md); 113} 114 115static int 116mlx_disk_add0(const char *name) 117{ 118 struct mlx_disk *md; 119 int unit; 120 121 if (name[0] != 'l' || name[1] != 'd' || !isdigit((unsigned char)name[2])) 122 return (-1); 123 124 SIMPLEQ_FOREACH(md, &mlx_disks, chain) 125 if (strcmp(md->name, name) == 0) 126 return (0); 127 128 unit = atoi(name + 2); 129 if (ioctl(mlxfd, MLX_GET_SYSDRIVE, &unit) < 0) 130 return (-1); 131 132 if ((md = malloc(sizeof(*md))) == NULL) 133 err(EXIT_FAILURE, "mlx_disk_add()"); 134 135 strlcpy(md->name, name, sizeof(md->name)); 136 md->hwunit = unit; 137 SIMPLEQ_INSERT_TAIL(&mlx_disks, md, chain); 138 return (0); 139} 140 141void 142mlx_disk_add(const char *name) 143{ 144 145 if (mlx_disk_add0(name) != 0) 146 errx(EXIT_FAILURE, "%s is not attached to %s", name, mlxname); 147} 148 149void 150mlx_disk_add_all(void) 151{ 152 struct io_sysctl *data; 153 size_t i, len; 154 static const int mib[3] = { CTL_HW, HW_IOSTATS, sizeof(*data) }; 155 156 data = asysctl(mib, __arraycount(mib), &len); 157 len /= sizeof(*data); 158 159 if (data == NULL || len == 0) 160 errx(EXIT_FAILURE, "no drives attached."); 161 162 for (i = 0; i < len; ++i) { 163 if (data[i].type == IOSTAT_DISK) 164 mlx_disk_add0(data[i].name); 165 } 166 167 free(data); 168} 169