1 /* $NetBSD: scsipiconf.c,v 1.15 2001/11/13 06:56:40 lukem 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/cdefs.h> 58 __KERNEL_RCSID(0, "$NetBSD: scsipiconf.c,v 1.15 2001/11/13 06:56:40 lukem Exp $"); 59 60 #include <sys/types.h> 61 #include <sys/param.h> 62 #include <sys/systm.h> 63 #include <sys/malloc.h> 64 #include <sys/device.h> 65 #include <sys/proc.h> 66 67 #include <uvm/uvm_extern.h> 68 69 #include <dev/scsipi/scsipi_all.h> 70 #include <dev/scsipi/scsipiconf.h> 71 72 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377') 73 74 int 75 scsipi_command(periph, cmd, cmdlen, data_addr, datalen, retries, timeout, bp, 76 flags) 77 struct scsipi_periph *periph; 78 struct scsipi_generic *cmd; 79 int cmdlen; 80 u_char *data_addr; 81 int datalen; 82 int retries; 83 int timeout; 84 struct buf *bp; 85 int flags; 86 { 87 int error; 88 89 if ((flags & XS_CTL_DATA_ONSTACK) != 0) { 90 /* 91 * If the I/O buffer is allocated on stack, the 92 * process must NOT be swapped out, as the device will 93 * be accessing the stack. 94 */ 95 PHOLD(curproc); 96 } 97 error = (*periph->periph_channel->chan_bustype->bustype_cmd)(periph, 98 cmd, cmdlen, data_addr, datalen, retries, timeout, bp, flags); 99 if ((flags & XS_CTL_DATA_ONSTACK) != 0) 100 PRELE(curproc); 101 return (error); 102 } 103 104 /* 105 * allocate and init a scsipi_periph structure for a new device. 106 */ 107 struct scsipi_periph * 108 scsipi_alloc_periph(malloc_flag) 109 int malloc_flag; 110 { 111 struct scsipi_periph *periph; 112 int i; 113 114 periph = malloc(sizeof(*periph), M_DEVBUF, malloc_flag); 115 if (periph == NULL) 116 return NULL; 117 memset(periph, 0, sizeof(*periph)); 118 119 periph->periph_dev = NULL; 120 121 /* 122 * Start with one command opening. The periph driver 123 * will grow this if it knows it can take advantage of it. 124 */ 125 periph->periph_openings = 1; 126 periph->periph_active = 0; 127 128 for (i = 0; i < PERIPH_NTAGWORDS; i++) 129 periph->periph_freetags[i] = 0xffffffff; 130 131 TAILQ_INIT(&periph->periph_xferq); 132 callout_init(&periph->periph_callout); 133 134 return periph; 135 } 136 137 /* 138 * Return a priority based on how much of the inquiry data matches 139 * the patterns for the particular driver. 140 */ 141 caddr_t 142 scsipi_inqmatch(inqbuf, base, nmatches, matchsize, bestpriority) 143 struct scsipi_inquiry_pattern *inqbuf; 144 caddr_t base; 145 int nmatches, matchsize; 146 int *bestpriority; 147 { 148 u_int8_t type; 149 caddr_t bestmatch; 150 151 /* Include the qualifier to catch vendor-unique types. */ 152 type = inqbuf->type; 153 154 for (*bestpriority = 0, bestmatch = 0; nmatches--; base += matchsize) { 155 struct scsipi_inquiry_pattern *match = (void *)base; 156 int priority, len; 157 158 if (type != match->type) 159 continue; 160 if (inqbuf->removable != match->removable) 161 continue; 162 priority = 2; 163 len = strlen(match->vendor); 164 if (memcmp(inqbuf->vendor, match->vendor, len)) 165 continue; 166 priority += len; 167 len = strlen(match->product); 168 if (memcmp(inqbuf->product, match->product, len)) 169 continue; 170 priority += len; 171 len = strlen(match->revision); 172 if (memcmp(inqbuf->revision, match->revision, len)) 173 continue; 174 priority += len; 175 176 #ifdef SCSIPI_DEBUG 177 printf("scsipi_inqmatch: %d/%d/%d <%s, %s, %s>\n", 178 priority, match->type, match->removable, 179 match->vendor, match->product, match->revision); 180 #endif 181 if (priority > *bestpriority) { 182 *bestpriority = priority; 183 bestmatch = base; 184 } 185 } 186 187 return (bestmatch); 188 } 189 190 char * 191 scsipi_dtype(type) 192 int type; 193 { 194 char *dtype; 195 196 switch (type) { 197 case T_DIRECT: 198 dtype = "direct"; 199 break; 200 case T_SEQUENTIAL: 201 dtype = "sequential"; 202 break; 203 case T_PRINTER: 204 dtype = "printer"; 205 break; 206 case T_PROCESSOR: 207 dtype = "processor"; 208 break; 209 case T_WORM: 210 dtype = "worm"; 211 break; 212 case T_CDROM: 213 dtype = "cdrom"; 214 break; 215 case T_SCANNER: 216 dtype = "scanner"; 217 break; 218 case T_OPTICAL: 219 dtype = "optical"; 220 break; 221 case T_CHANGER: 222 dtype = "changer"; 223 break; 224 case T_COMM: 225 dtype = "communication"; 226 break; 227 case T_IT8_1: 228 case T_IT8_2: 229 dtype = "graphic arts pre-press"; 230 break; 231 case T_STORARRAY: 232 dtype = "storage array"; 233 break; 234 case T_ENCLOSURE: 235 dtype = "enclosure services"; 236 break; 237 case T_SIMPLE_DIRECT: 238 dtype = "simplified direct"; 239 break; 240 case T_OPTIC_CARD_RW: 241 dtype = "optical card r/w"; 242 break; 243 case T_OBJECT_STORED: 244 dtype = "object-based storage"; 245 break; 246 case T_NODEVICE: 247 panic("scsipi_dtype: impossible device type"); 248 default: 249 dtype = "unknown"; 250 break; 251 } 252 return (dtype); 253 } 254 255 void 256 scsipi_strvis(dst, dlen, src, slen) 257 u_char *dst, *src; 258 int dlen, slen; 259 { 260 261 /* Trim leading and trailing blanks and NULs. */ 262 while (slen > 0 && STRVIS_ISWHITE(src[0])) 263 ++src, --slen; 264 while (slen > 0 && STRVIS_ISWHITE(src[slen - 1])) 265 --slen; 266 267 while (slen > 0) { 268 if (*src < 0x20 || *src >= 0x80) { 269 /* non-printable characters */ 270 dlen -= 4; 271 if (dlen < 1) 272 break; 273 *dst++ = '\\'; 274 *dst++ = ((*src & 0300) >> 6) + '0'; 275 *dst++ = ((*src & 0070) >> 3) + '0'; 276 *dst++ = ((*src & 0007) >> 0) + '0'; 277 } else if (*src == '\\') { 278 /* quote characters */ 279 dlen -= 2; 280 if (dlen < 1) 281 break; 282 *dst++ = '\\'; 283 *dst++ = '\\'; 284 } else { 285 /* normal characters */ 286 if (--dlen < 1) 287 break; 288 *dst++ = *src; 289 } 290 ++src, --slen; 291 } 292 293 *dst++ = 0; 294 } 295