1 /* $NetBSD: scsipiconf.c,v 1.14 2001/07/18 18:27:08 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum; by Jason R. Thorpe of the Numerical Aerospace 9 * Simulation Facility, NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Originally written by Julian Elischer (julian@tfs.com) 42 * for TRW Financial Systems for use under the MACH(2.5) operating system. 43 * 44 * TRW Financial Systems, in accordance with their agreement with Carnegie 45 * Mellon University, makes this software available to CMU to distribute 46 * or use in any manner that they see fit as long as this message is kept with 47 * the software. For this reason TFS also grants any other persons or 48 * organisations permission to use or modify this software. 49 * 50 * TFS supplies this software to be publicly redistributed 51 * on the understanding that TFS is not responsible for the correct 52 * functioning of this software in any circumstances. 53 * 54 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992 55 */ 56 57 #include <sys/types.h> 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/malloc.h> 61 #include <sys/device.h> 62 #include <sys/proc.h> 63 64 #include <uvm/uvm_extern.h> 65 66 #include <dev/scsipi/scsipi_all.h> 67 #include <dev/scsipi/scsipiconf.h> 68 69 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377') 70 71 int 72 scsipi_command(periph, cmd, cmdlen, data_addr, datalen, retries, timeout, bp, 73 flags) 74 struct scsipi_periph *periph; 75 struct scsipi_generic *cmd; 76 int cmdlen; 77 u_char *data_addr; 78 int datalen; 79 int retries; 80 int timeout; 81 struct buf *bp; 82 int flags; 83 { 84 int error; 85 86 if ((flags & XS_CTL_DATA_ONSTACK) != 0) { 87 /* 88 * If the I/O buffer is allocated on stack, the 89 * process must NOT be swapped out, as the device will 90 * be accessing the stack. 91 */ 92 PHOLD(curproc); 93 } 94 error = (*periph->periph_channel->chan_bustype->bustype_cmd)(periph, 95 cmd, cmdlen, data_addr, datalen, retries, timeout, bp, flags); 96 if ((flags & XS_CTL_DATA_ONSTACK) != 0) 97 PRELE(curproc); 98 return (error); 99 } 100 101 /* 102 * allocate and init a scsipi_periph structure for a new device. 103 */ 104 struct scsipi_periph * 105 scsipi_alloc_periph(malloc_flag) 106 int malloc_flag; 107 { 108 struct scsipi_periph *periph; 109 int i; 110 111 periph = malloc(sizeof(*periph), M_DEVBUF, malloc_flag); 112 if (periph == NULL) 113 return NULL; 114 memset(periph, 0, sizeof(*periph)); 115 116 periph->periph_dev = NULL; 117 118 /* 119 * Start with one command opening. The periph driver 120 * will grow this if it knows it can take advantage of it. 121 */ 122 periph->periph_openings = 1; 123 periph->periph_active = 0; 124 125 for (i = 0; i < PERIPH_NTAGWORDS; i++) 126 periph->periph_freetags[i] = 0xffffffff; 127 128 TAILQ_INIT(&periph->periph_xferq); 129 callout_init(&periph->periph_callout); 130 131 return periph; 132 } 133 134 /* 135 * Return a priority based on how much of the inquiry data matches 136 * the patterns for the particular driver. 137 */ 138 caddr_t 139 scsipi_inqmatch(inqbuf, base, nmatches, matchsize, bestpriority) 140 struct scsipi_inquiry_pattern *inqbuf; 141 caddr_t base; 142 int nmatches, matchsize; 143 int *bestpriority; 144 { 145 u_int8_t type; 146 caddr_t bestmatch; 147 148 /* Include the qualifier to catch vendor-unique types. */ 149 type = inqbuf->type; 150 151 for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) { 152 struct scsipi_inquiry_pattern *match = (void *)base; 153 int priority, len; 154 155 if (type != match->type) 156 continue; 157 if (inqbuf->removable != match->removable) 158 continue; 159 priority = 2; 160 len = strlen(match->vendor); 161 if (memcmp(inqbuf->vendor, match->vendor, len)) 162 continue; 163 priority += len; 164 len = strlen(match->product); 165 if (memcmp(inqbuf->product, match->product, len)) 166 continue; 167 priority += len; 168 len = strlen(match->revision); 169 if (memcmp(inqbuf->revision, match->revision, len)) 170 continue; 171 priority += len; 172 173 #ifdef SCSIPI_DEBUG 174 printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n", 175 priority, match->type, match->removable, 176 match->vendor, match->product, match->revision); 177 #endif 178 if (priority > *bestpriority) { 179 *bestpriority = priority; 180 bestmatch = base; 181 } 182 } 183 184 return (bestmatch); 185 } 186 187 char * 188 scsipi_dtype(type) 189 int type; 190 { 191 char *dtype; 192 193 switch (type) { 194 case T_DIRECT: 195 dtype = "direct"; 196 break; 197 case T_SEQUENTIAL: 198 dtype = "sequential"; 199 break; 200 case T_PRINTER: 201 dtype = "printer"; 202 break; 203 case T_PROCESSOR: 204 dtype = "processor"; 205 break; 206 case T_WORM: 207 dtype = "worm"; 208 break; 209 case T_CDROM: 210 dtype = "cdrom"; 211 break; 212 case T_SCANNER: 213 dtype = "scanner"; 214 break; 215 case T_OPTICAL: 216 dtype = "optical"; 217 break; 218 case T_CHANGER: 219 dtype = "changer"; 220 break; 221 case T_COMM: 222 dtype = "communication"; 223 break; 224 case T_IT8_1: 225 case T_IT8_2: 226 dtype = "graphic arts pre-press"; 227 break; 228 case T_STORARRAY: 229 dtype = "storage array"; 230 break; 231 case T_ENCLOSURE: 232 dtype = "enclosure services"; 233 break; 234 case T_SIMPLE_DIRECT: 235 dtype = "simplified direct"; 236 break; 237 case T_OPTIC_CARD_RW: 238 dtype = "optical card r/w"; 239 break; 240 case T_OBJECT_STORED: 241 dtype = "object-based storage"; 242 break; 243 case T_NODEVICE: 244 panic("scsipi_dtype: impossible device type"); 245 default: 246 dtype = "unknown"; 247 break; 248 } 249 return (dtype); 250 } 251 252 void 253 scsipi_strvis(dst, dlen, src, slen) 254 u_char *dst, *src; 255 int dlen, slen; 256 { 257 258 /* Trim leading and trailing blanks and NULs. */ 259 while (slen > 0 && STRVIS_ISWHITE(src[0])) 260 ++src, --slen; 261 while (slen > 0 && STRVIS_ISWHITE(src[slen - 1])) 262 --slen; 263 264 while (slen > 0) { 265 if (*src < 0x20 || *src >= 0x80) { 266 /* non-printable characters */ 267 dlen -= 4; 268 if (dlen < 1) 269 break; 270 *dst++ = '\\'; 271 *dst++ = ((*src & 0300) >> 6) + '0'; 272 *dst++ = ((*src & 0070) >> 3) + '0'; 273 *dst++ = ((*src & 0007) >> 0) + '0'; 274 } else if (*src == '\\') { 275 /* quote characters */ 276 dlen -= 2; 277 if (dlen < 1) 278 break; 279 *dst++ = '\\'; 280 *dst++ = '\\'; 281 } else { 282 /* normal characters */ 283 if (--dlen < 1) 284 break; 285 *dst++ = *src; 286 } 287 ++src, --slen; 288 } 289 290 *dst++ = 0; 291 } 292