Home | History | Annotate | Line # | Download | only in scsipi
atapiconf.c revision 1.28.2.6
      1  1.28.2.6   bouyer /*	$NetBSD: atapiconf.c,v 1.28.2.6 2000/11/20 09:59:23 bouyer Exp $	*/
      2       1.2   bouyer 
      3       1.2   bouyer /*
      4       1.2   bouyer  * Copyright (c) 1996 Manuel Bouyer.  All rights reserved.
      5       1.2   bouyer  *
      6       1.2   bouyer  * Redistribution and use in source and binary forms, with or without
      7       1.2   bouyer  * modification, are permitted provided that the following conditions
      8       1.2   bouyer  * are met:
      9       1.2   bouyer  * 1. Redistributions of source code must retain the above copyright
     10       1.2   bouyer  *    notice, this list of conditions and the following disclaimer.
     11       1.2   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.2   bouyer  *    notice, this list of conditions and the following disclaimer in the
     13       1.2   bouyer  *    documentation and/or other materials provided with the distribution.
     14       1.2   bouyer  * 3. All advertising materials mentioning features or use of this software
     15       1.2   bouyer  *    must display the following acknowledgement:
     16       1.2   bouyer  *	This product includes software developed by Manuel Bouyer.
     17       1.2   bouyer  * 4. The name of the author may not be used to endorse or promote products
     18       1.2   bouyer  *    derived from this software without specific prior written permission.
     19       1.2   bouyer  *
     20       1.2   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21       1.2   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22       1.2   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23       1.2   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24       1.2   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25       1.2   bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26       1.2   bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27       1.2   bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28       1.2   bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29       1.2   bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30       1.2   bouyer  */
     31       1.2   bouyer 
     32       1.2   bouyer #include <sys/types.h>
     33       1.2   bouyer #include <sys/param.h>
     34       1.2   bouyer #include <sys/systm.h>
     35       1.2   bouyer #include <sys/malloc.h>
     36       1.2   bouyer #include <sys/device.h>
     37       1.2   bouyer #include <sys/buf.h>
     38       1.4    enami #include <sys/proc.h>
     39  1.28.2.1  thorpej #include <sys/kthread.h>
     40       1.2   bouyer 
     41      1.13   bouyer #include <dev/ata/atavar.h>
     42       1.2   bouyer #include <dev/scsipi/scsipi_all.h>
     43       1.9      cgd #include <dev/scsipi/atapi_all.h>
     44       1.2   bouyer #include <dev/scsipi/scsipiconf.h>
     45       1.2   bouyer #include <dev/scsipi/atapiconf.h>
     46       1.4    enami 
     47       1.2   bouyer #include "locators.h"
     48       1.2   bouyer 
     49       1.2   bouyer #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
     50      1.27    enami #define MAX_TARGET 1
     51       1.2   bouyer 
     52  1.28.2.1  thorpej const struct scsipi_periphsw atapi_probe_periphsw = {
     53  1.28.2.1  thorpej 	NULL,
     54  1.28.2.1  thorpej 	NULL,
     55  1.28.2.1  thorpej 	NULL,
     56  1.28.2.1  thorpej 	NULL,
     57  1.28.2.1  thorpej };
     58  1.28.2.1  thorpej 
     59       1.2   bouyer int	atapibusmatch __P((struct device *, struct cfdata *, void *));
     60       1.2   bouyer void	atapibusattach __P((struct device *, struct device *, void *));
     61      1.27    enami int	atapibusactivate __P((struct device *, enum devact));
     62      1.27    enami int	atapibusdetach __P((struct device *, int flags));
     63      1.27    enami 
     64  1.28.2.1  thorpej int	atapibussubmatch __P((struct device *, struct cfdata *, void *));
     65  1.28.2.1  thorpej 
     66  1.28.2.1  thorpej int	atapi_probe_bus __P((struct atapibus_softc *, int));
     67       1.2   bouyer 
     68       1.2   bouyer struct cfattach atapibus_ca = {
     69      1.27    enami 	sizeof(struct atapibus_softc), atapibusmatch, atapibusattach,
     70      1.27    enami 	atapibusdetach, atapibusactivate,
     71       1.2   bouyer };
     72       1.2   bouyer 
     73       1.8  thorpej extern struct cfdriver atapibus_cd;
     74       1.2   bouyer 
     75       1.2   bouyer int atapibusprint __P((void *, const char *));
     76       1.2   bouyer 
     77       1.2   bouyer struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
     78       1.2   bouyer 	{{T_CDROM, T_REMOV,
     79  1.28.2.1  thorpej 	 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"},	PQUIRK_NOTUR},
     80       1.2   bouyer 	{{T_CDROM, T_REMOV,
     81  1.28.2.1  thorpej 	 "BCD-16X 1997-04-25", "", "VER 2.2"},	PQUIRK_NOSTARTUNIT},
     82      1.26   bouyer 	{{T_CDROM, T_REMOV,
     83  1.28.2.1  thorpej 	 "BCD-24X 1997-06-27", "", "VER 2.0"},	PQUIRK_NOSTARTUNIT},
     84      1.22   bouyer 	{{T_CDROM, T_REMOV,
     85  1.28.2.1  thorpej 	 "CR-2801TE", "", "1.07"},		PQUIRK_NOSENSE},
     86      1.23   bouyer 	{{T_CDROM, T_REMOV,
     87  1.28.2.1  thorpej 	 "CREATIVECD3630E", "", "AC101"},	PQUIRK_NOSENSE},
     88       1.2   bouyer 	{{T_CDROM, T_REMOV,
     89  1.28.2.1  thorpej 	 "FX320S", "", "q01"},			PQUIRK_NOSENSE},
     90      1.21   bouyer 	{{T_CDROM, T_REMOV,
     91  1.28.2.1  thorpej 	 "GCD-R580B", "", "1.00"},		PQUIRK_LITTLETOC},
     92      1.24   bouyer 	{{T_CDROM, T_REMOV,
     93  1.28.2.6   bouyer 	 "HITACHI CDR-7730", "", "0008a"},      PQUIRK_NOSENSE},
     94  1.28.2.6   bouyer 	{{T_CDROM, T_REMOV,
     95  1.28.2.1  thorpej 	 "MATSHITA CR-574", "", "1.02"},	PQUIRK_NOCAPACITY},
     96       1.3   bouyer 	{{T_CDROM, T_REMOV,
     97  1.28.2.1  thorpej 	 "MATSHITA CR-574", "", "1.06"},	PQUIRK_NOCAPACITY},
     98       1.7   bouyer 	{{T_CDROM, T_REMOV,
     99  1.28.2.1  thorpej 	 "Memorex CRW-2642", "", "1.0g"},	PQUIRK_NOSENSE},
    100       1.7   bouyer 	{{T_CDROM, T_REMOV,
    101  1.28.2.1  thorpej 	 "NEC                 CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR},
    102       1.2   bouyer 	{{T_CDROM, T_REMOV,
    103  1.28.2.1  thorpej 	 "SANYO CRD-256P", "", "1.02"},		PQUIRK_NOCAPACITY},
    104       1.2   bouyer 	{{T_CDROM, T_REMOV,
    105  1.28.2.1  thorpej 	 "SANYO CRD-254P", "", "1.02"},		PQUIRK_NOCAPACITY},
    106       1.3   bouyer 	{{T_CDROM, T_REMOV,
    107  1.28.2.1  thorpej 	 "SANYO CRD-S54P", "", "1.08"},		PQUIRK_NOCAPACITY},
    108       1.5   bouyer 	{{T_CDROM, T_REMOV,
    109  1.28.2.1  thorpej 	 "CD-ROM  CDR-S1", "", "1.70"},		PQUIRK_NOCAPACITY}, /* Sanyo */
    110      1.18   bouyer 	{{T_CDROM, T_REMOV,
    111  1.28.2.1  thorpej 	 "CD-ROM  CDR-N16", "", "1.25"},	PQUIRK_NOCAPACITY}, /* Sanyo */
    112      1.20   bouyer 	{{T_CDROM, T_REMOV,
    113  1.28.2.1  thorpej 	 "UJDCD8730", "", "1.14"},		PQUIRK_NODOORLOCK}, /* Acer */
    114       1.2   bouyer };
    115       1.2   bouyer 
    116       1.2   bouyer int
    117       1.2   bouyer atapibusmatch(parent, cf, aux)
    118       1.2   bouyer 	struct device *parent;
    119       1.2   bouyer 	struct cfdata *cf;
    120       1.2   bouyer 	void *aux;
    121       1.2   bouyer {
    122  1.28.2.1  thorpej 	struct ata_atapi_attach *aa = aux;
    123       1.2   bouyer 
    124  1.28.2.1  thorpej 	if (aa == NULL)
    125       1.4    enami 		return (0);
    126  1.28.2.1  thorpej 
    127  1.28.2.1  thorpej 	if (aa->aa_type != T_ATAPI)
    128       1.4    enami 		return (0);
    129  1.28.2.1  thorpej 
    130  1.28.2.1  thorpej 	if (cf->cf_loc[ATAPICF_CHANNEL] != aa->aa_channel &&
    131      1.13   bouyer 	    cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
    132  1.28.2.1  thorpej 		return (0);
    133  1.28.2.1  thorpej 
    134       1.4    enami 	return (1);
    135       1.2   bouyer }
    136       1.2   bouyer 
    137       1.2   bouyer int
    138       1.2   bouyer atapibussubmatch(parent, cf, aux)
    139       1.2   bouyer 	struct device *parent;
    140       1.2   bouyer 	struct cfdata *cf;
    141       1.2   bouyer 	void *aux;
    142       1.2   bouyer {
    143       1.2   bouyer 	struct scsipibus_attach_args *sa = aux;
    144  1.28.2.1  thorpej 	struct scsipi_periph *periph = sa->sa_periph;
    145       1.2   bouyer 
    146       1.2   bouyer 	if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
    147  1.28.2.1  thorpej 	    cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
    148       1.4    enami 		return (0);
    149       1.2   bouyer 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    150       1.2   bouyer }
    151       1.2   bouyer 
    152       1.2   bouyer void
    153       1.2   bouyer atapibusattach(parent, self, aux)
    154       1.4    enami 	struct device *parent, *self;
    155       1.4    enami 	void *aux;
    156       1.2   bouyer {
    157  1.28.2.1  thorpej 	struct atapibus_softc *sc = (void *) self;
    158  1.28.2.1  thorpej 	struct ata_atapi_attach *aa = aux;
    159  1.28.2.1  thorpej 	struct scsipi_channel *chan = aa->aa_bus_private;
    160  1.28.2.1  thorpej 
    161  1.28.2.1  thorpej 	sc->sc_channel = chan;
    162  1.28.2.1  thorpej 	sc->sc_drvs = aa->aa_drv_data;
    163  1.28.2.1  thorpej 
    164  1.28.2.1  thorpej 	/* ATAPI has no LUNs. */
    165  1.28.2.1  thorpej 	chan->chan_nluns = 1;
    166  1.28.2.1  thorpej 	printf(": %d targets\n", chan->chan_ntargets);
    167  1.28.2.1  thorpej 
    168  1.28.2.1  thorpej 	/* Initialize the channel. */
    169  1.28.2.1  thorpej 	scsipi_channel_init(chan);
    170  1.28.2.1  thorpej 
    171  1.28.2.1  thorpej 	/* Probe the bus for devices. */
    172  1.28.2.1  thorpej 	atapi_probe_bus(sc, -1);
    173      1.27    enami }
    174      1.27    enami 
    175      1.27    enami int
    176      1.27    enami atapibusactivate(self, act)
    177      1.27    enami 	struct device *self;
    178      1.27    enami 	enum devact act;
    179      1.27    enami {
    180  1.28.2.1  thorpej 	struct atapibus_softc *sc = (void *) self;
    181  1.28.2.1  thorpej 	struct scsipi_channel *chan = sc->sc_channel;
    182  1.28.2.1  thorpej 	struct scsipi_periph *periph;
    183      1.27    enami 	int target, error = 0, s;
    184      1.27    enami 
    185      1.27    enami 	s = splbio();
    186      1.27    enami 	switch (act) {
    187      1.27    enami 	case DVACT_ACTIVATE:
    188      1.27    enami 		error = EOPNOTSUPP;
    189      1.27    enami 		break;
    190      1.27    enami 
    191      1.27    enami 	case DVACT_DEACTIVATE:
    192  1.28.2.1  thorpej 		for (target = 0; target < chan->chan_ntargets; target++) {
    193  1.28.2.5  thorpej 			periph = scsipi_lookup_periph(chan, target, 0);
    194  1.28.2.1  thorpej 			if (periph == NULL)
    195      1.27    enami 				continue;
    196  1.28.2.1  thorpej 			error = config_deactivate(periph->periph_dev);
    197  1.28.2.1  thorpej 			if (error)
    198      1.27    enami 				goto out;
    199      1.27    enami 		}
    200      1.27    enami 		break;
    201      1.27    enami 	}
    202      1.27    enami  out:
    203      1.27    enami 	splx(s);
    204      1.27    enami 	return (error);
    205      1.27    enami }
    206      1.27    enami 
    207      1.27    enami int
    208      1.27    enami atapibusdetach(self, flags)
    209      1.27    enami 	struct device *self;
    210      1.27    enami 	int flags;
    211      1.27    enami {
    212  1.28.2.1  thorpej 	struct atapibus_softc *sc = (void *)self;
    213  1.28.2.1  thorpej 	struct scsipi_channel *chan = sc->sc_channel;
    214  1.28.2.1  thorpej 	struct scsipi_periph *periph;
    215      1.27    enami 	int target, error;
    216      1.27    enami 
    217  1.28.2.5  thorpej 	/*
    218  1.28.2.5  thorpej 	 * Shut down the channel.
    219  1.28.2.5  thorpej 	 */
    220  1.28.2.5  thorpej 	scsipi_channel_shutdown(chan);
    221  1.28.2.5  thorpej 
    222  1.28.2.5  thorpej 	/*
    223  1.28.2.5  thorpej 	 * Now detach all of the periphs.
    224  1.28.2.5  thorpej 	 */
    225  1.28.2.1  thorpej 	for (target = 0; target < chan->chan_ntargets; target++) {
    226  1.28.2.5  thorpej 		periph = scsipi_lookup_periph(chan, target, 0);
    227  1.28.2.1  thorpej 		if (periph == NULL)
    228      1.27    enami 			continue;
    229  1.28.2.1  thorpej 		error = config_detach(periph->periph_dev, flags);
    230  1.28.2.1  thorpej 		if (error)
    231      1.27    enami 			return (error);
    232      1.27    enami 
    233      1.27    enami 		/*
    234      1.27    enami 		 * We have successfully detached the child.  Drop the
    235      1.27    enami 		 * direct reference for the child so that wdcdetach
    236      1.27    enami 		 * won't call detach routine twice.
    237      1.27    enami 		 */
    238      1.27    enami #ifdef DIAGNOSTIC
    239  1.28.2.1  thorpej 		if (periph->periph_dev != sc->sc_drvs[target].drv_softc)
    240      1.27    enami 			panic("softc mismatch");
    241      1.27    enami #endif
    242      1.27    enami 		sc->sc_drvs[target].drv_softc = NULL;
    243      1.27    enami 
    244  1.28.2.5  thorpej 		scsipi_remove_periph(chan, periph);
    245  1.28.2.1  thorpej 		free(periph, M_DEVBUF);
    246      1.27    enami 	}
    247      1.27    enami 	return (0);
    248       1.2   bouyer }
    249       1.2   bouyer 
    250       1.4    enami int
    251  1.28.2.1  thorpej atapi_probe_bus(sc, target)
    252  1.28.2.1  thorpej 	struct atapibus_softc *sc;
    253  1.28.2.1  thorpej 	int target;
    254       1.2   bouyer {
    255  1.28.2.1  thorpej 	struct scsipi_channel *chan = sc->sc_channel;
    256       1.2   bouyer 	int maxtarget, mintarget;
    257      1.15  thorpej 	int error;
    258  1.28.2.6   bouyer 	struct atapi_adapter *atapi_adapter;
    259       1.4    enami 
    260       1.2   bouyer 	if (target == -1) {
    261       1.2   bouyer 		maxtarget = 1;
    262       1.2   bouyer 		mintarget = 0;
    263       1.2   bouyer 	} else {
    264  1.28.2.1  thorpej 		if (target < 0 || target >= chan->chan_ntargets)
    265       1.4    enami 			return (ENXIO);
    266       1.2   bouyer 		maxtarget = mintarget = target;
    267       1.2   bouyer 	}
    268  1.28.2.1  thorpej 
    269  1.28.2.1  thorpej 	if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
    270      1.15  thorpej 		return (error);
    271  1.28.2.6   bouyer 	atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
    272       1.4    enami 	for (target = mintarget; target <= maxtarget; target++)
    273  1.28.2.6   bouyer 		atapi_adapter->atapi_probe_device(sc, target);
    274  1.28.2.1  thorpej 	scsipi_adapter_delref(chan->chan_adapter);
    275       1.4    enami 	return (0);
    276       1.2   bouyer }
    277       1.2   bouyer 
    278  1.28.2.6   bouyer void *
    279  1.28.2.6   bouyer atapi_probe_device(sc, target, periph, sa)
    280  1.28.2.1  thorpej 	struct atapibus_softc *sc;
    281       1.2   bouyer 	int target;
    282  1.28.2.6   bouyer 	struct scsipi_periph *periph;
    283  1.28.2.6   bouyer 	struct scsipibus_attach_args *sa;
    284       1.2   bouyer {
    285  1.28.2.1  thorpej 	struct scsipi_channel *chan = sc->sc_channel;
    286       1.2   bouyer 	struct scsi_quirk_inquiry_pattern *finger;
    287  1.28.2.1  thorpej 	struct cfdata *cf;
    288  1.28.2.1  thorpej 	int priority, quirks;
    289       1.2   bouyer 
    290  1.28.2.6   bouyer 	finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
    291  1.28.2.6   bouyer 	    &sa->sa_inqbuf, (caddr_t)atapi_quirk_patterns,
    292  1.28.2.6   bouyer 	    sizeof(atapi_quirk_patterns) /
    293  1.28.2.6   bouyer 	        sizeof(atapi_quirk_patterns[0]),
    294  1.28.2.6   bouyer 	    sizeof(atapi_quirk_patterns[0]), &priority);
    295  1.28.2.6   bouyer 
    296  1.28.2.6   bouyer 	if (finger != NULL)
    297  1.28.2.6   bouyer 		quirks = finger->quirks;
    298  1.28.2.6   bouyer 	else
    299  1.28.2.6   bouyer 		quirks = 0;
    300  1.28.2.1  thorpej 
    301  1.28.2.6   bouyer 	/*
    302  1.28.2.6   bouyer 	 * Now apply any quirks from the table.
    303  1.28.2.6   bouyer 	 */
    304  1.28.2.6   bouyer 	periph->periph_quirks |= quirks;
    305  1.28.2.1  thorpej 
    306  1.28.2.6   bouyer 	if ((cf = config_search(atapibussubmatch, &sc->sc_dev,
    307  1.28.2.6   bouyer 	    sa)) != 0) {
    308  1.28.2.6   bouyer 		scsipi_insert_periph(chan, periph);
    309  1.28.2.1  thorpej 		/*
    310  1.28.2.6   bouyer 		 * XXX Can't assign periph_dev here, because we'll
    311  1.28.2.6   bouyer 		 * XXX need it before config_attach() returns.  Must
    312  1.28.2.6   bouyer 		 * XXX assign it in periph driver.
    313  1.28.2.1  thorpej 		 */
    314  1.28.2.6   bouyer 		return config_attach(&sc->sc_dev, cf, sa,
    315  1.28.2.6   bouyer 		    atapibusprint);
    316  1.28.2.6   bouyer 	} else {
    317  1.28.2.6   bouyer 		atapibusprint(&sa, sc->sc_dev.dv_xname);
    318  1.28.2.6   bouyer 		printf(" not configured\n");
    319  1.28.2.6   bouyer 		free(periph, M_DEVBUF);
    320  1.28.2.6   bouyer 		return NULL;
    321       1.2   bouyer 	}
    322  1.28.2.6   bouyer }
    323  1.28.2.6   bouyer 
    324  1.28.2.6   bouyer int
    325  1.28.2.6   bouyer atapiprint(aux, pnp)
    326  1.28.2.6   bouyer 	void *aux;
    327  1.28.2.6   bouyer 	const char *pnp;
    328  1.28.2.6   bouyer {
    329  1.28.2.6   bouyer 	struct ata_atapi_attach *aa_link = aux;
    330  1.28.2.6   bouyer 	if (pnp)
    331  1.28.2.6   bouyer 		printf("atapibus at %s", pnp);
    332  1.28.2.6   bouyer 	printf(" channel %d", aa_link->aa_channel);
    333  1.28.2.6   bouyer 	return (UNCONF);
    334       1.2   bouyer }
    335       1.2   bouyer 
    336       1.2   bouyer int
    337       1.2   bouyer atapibusprint(aux, pnp)
    338       1.2   bouyer 	void *aux;
    339       1.2   bouyer 	const char *pnp;
    340       1.2   bouyer {
    341       1.2   bouyer 	struct scsipibus_attach_args *sa = aux;
    342       1.2   bouyer 	struct scsipi_inquiry_pattern *inqbuf;
    343       1.2   bouyer 	char *dtype;
    344       1.2   bouyer 
    345       1.2   bouyer 	if (pnp != NULL)
    346       1.2   bouyer 		printf("%s", pnp);
    347       1.2   bouyer 
    348       1.2   bouyer 	inqbuf = &sa->sa_inqbuf;
    349       1.2   bouyer 
    350       1.2   bouyer 	dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
    351       1.2   bouyer 	printf(" drive %d: <%s, %s, %s> type %d %s %s",
    352  1.28.2.1  thorpej 	    sa->sa_periph->periph_target ,inqbuf->vendor,
    353       1.2   bouyer 	    inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
    354       1.2   bouyer 	    inqbuf->removable ? "removable" : "fixed");
    355       1.2   bouyer 	return (UNCONF);
    356       1.2   bouyer }
    357