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