Home | History | Annotate | Line # | Download | only in scsipi
atapiconf.c revision 1.83.12.1
      1  1.83.12.1       mrg /*	$NetBSD: atapiconf.c,v 1.83.12.1 2012/04/29 23:04:59 mrg Exp $	*/
      2        1.2    bouyer 
      3        1.2    bouyer /*
      4       1.46    bouyer  * Copyright (c) 1996, 2001 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  *
     15        1.2    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16        1.2    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17        1.2    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18        1.2    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19        1.2    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20        1.2    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21        1.2    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22        1.2    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23        1.2    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24        1.2    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25        1.2    bouyer  */
     26       1.42     lukem 
     27       1.42     lukem #include <sys/cdefs.h>
     28  1.83.12.1       mrg __KERNEL_RCSID(0, "$NetBSD: atapiconf.c,v 1.83.12.1 2012/04/29 23:04:59 mrg Exp $");
     29        1.2    bouyer 
     30        1.2    bouyer #include <sys/param.h>
     31        1.2    bouyer #include <sys/systm.h>
     32        1.2    bouyer #include <sys/malloc.h>
     33        1.2    bouyer #include <sys/device.h>
     34        1.2    bouyer #include <sys/buf.h>
     35        1.4     enami #include <sys/proc.h>
     36       1.39    bouyer #include <sys/kthread.h>
     37        1.2    bouyer 
     38        1.2    bouyer #include <dev/scsipi/scsipi_all.h>
     39       1.39    bouyer #include <dev/scsipi/scsipiconf.h>
     40        1.2    bouyer #include <dev/scsipi/atapiconf.h>
     41        1.4     enami 
     42        1.2    bouyer #include "locators.h"
     43        1.2    bouyer 
     44        1.2    bouyer #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
     45       1.27     enami #define MAX_TARGET 1
     46        1.2    bouyer 
     47       1.39    bouyer const struct scsipi_periphsw atapi_probe_periphsw = {
     48       1.39    bouyer 	NULL,
     49       1.39    bouyer 	NULL,
     50       1.39    bouyer 	NULL,
     51       1.39    bouyer 	NULL,
     52       1.39    bouyer };
     53       1.39    bouyer 
     54       1.77      cube static int	atapibusmatch(device_t, cfdata_t, void *);
     55       1.75    dyoung static void	atapibusattach(device_t, device_t, void *);
     56       1.75    dyoung static int	atapibusdetach(device_t, int flags);
     57       1.75    dyoung static void	atapibuschilddet(device_t, device_t);
     58        1.2    bouyer 
     59       1.77      cube static int	atapibussubmatch(device_t, cfdata_t, const int *, void *);
     60       1.39    bouyer 
     61       1.64   thorpej static int	atapi_probe_bus(struct atapibus_softc *, int);
     62       1.64   thorpej 
     63       1.64   thorpej static int	atapibusprint(void *, const char *);
     64        1.2    bouyer 
     65       1.78    dyoung CFATTACH_DECL3_NEW(atapibus, sizeof(struct atapibus_softc),
     66       1.81    dyoung     atapibusmatch, atapibusattach, atapibusdetach, NULL, NULL,
     67       1.78    dyoung     atapibuschilddet, DVF_DETACH_SHUTDOWN);
     68        1.2    bouyer 
     69        1.8   thorpej extern struct cfdriver atapibus_cd;
     70        1.2    bouyer 
     71       1.64   thorpej static const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
     72        1.2    bouyer 	{{T_CDROM, T_REMOV,
     73       1.39    bouyer 	 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"},	PQUIRK_NOTUR},
     74       1.22    bouyer 	{{T_CDROM, T_REMOV,
     75       1.39    bouyer 	 "CR-2801TE", "", "1.07"},		PQUIRK_NOSENSE},
     76       1.23    bouyer 	{{T_CDROM, T_REMOV,
     77       1.39    bouyer 	 "CREATIVECD3630E", "", "AC101"},	PQUIRK_NOSENSE},
     78        1.2    bouyer 	{{T_CDROM, T_REMOV,
     79       1.39    bouyer 	 "FX320S", "", "q01"},			PQUIRK_NOSENSE},
     80       1.21    bouyer 	{{T_CDROM, T_REMOV,
     81       1.39    bouyer 	 "GCD-R580B", "", "1.00"},		PQUIRK_LITTLETOC},
     82       1.31  jdolecek 	{{T_CDROM, T_REMOV,
     83       1.39    bouyer 	 "HITACHI CDR-7730", "", "0008a"},      PQUIRK_NOSENSE},
     84       1.24    bouyer 	{{T_CDROM, T_REMOV,
     85       1.39    bouyer 	 "MATSHITA CR-574", "", "1.02"},	PQUIRK_NOCAPACITY},
     86        1.3    bouyer 	{{T_CDROM, T_REMOV,
     87       1.39    bouyer 	 "MATSHITA CR-574", "", "1.06"},	PQUIRK_NOCAPACITY},
     88        1.7    bouyer 	{{T_CDROM, T_REMOV,
     89       1.39    bouyer 	 "Memorex CRW-2642", "", "1.0g"},	PQUIRK_NOSENSE},
     90        1.7    bouyer 	{{T_CDROM, T_REMOV,
     91       1.39    bouyer 	 "NEC                 CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR},
     92        1.2    bouyer 	{{T_CDROM, T_REMOV,
     93       1.39    bouyer 	 "SANYO CRD-256P", "", "1.02"},		PQUIRK_NOCAPACITY},
     94        1.2    bouyer 	{{T_CDROM, T_REMOV,
     95       1.39    bouyer 	 "SANYO CRD-254P", "", "1.02"},		PQUIRK_NOCAPACITY},
     96        1.3    bouyer 	{{T_CDROM, T_REMOV,
     97       1.39    bouyer 	 "SANYO CRD-S54P", "", "1.08"},		PQUIRK_NOCAPACITY},
     98        1.5    bouyer 	{{T_CDROM, T_REMOV,
     99       1.39    bouyer 	 "CD-ROM  CDR-S1", "", "1.70"},		PQUIRK_NOCAPACITY}, /* Sanyo */
    100       1.18    bouyer 	{{T_CDROM, T_REMOV,
    101       1.39    bouyer 	 "CD-ROM  CDR-N16", "", "1.25"},	PQUIRK_NOCAPACITY}, /* Sanyo */
    102        1.2    bouyer };
    103        1.2    bouyer 
    104        1.2    bouyer int
    105       1.72  christos atapiprint(void *aux, const char *pnp)
    106       1.44    bouyer {
    107       1.44    bouyer 	if (pnp)
    108       1.55   thorpej 		aprint_normal("atapibus at %s", pnp);
    109       1.44    bouyer 	return (UNCONF);
    110       1.44    bouyer }
    111       1.44    bouyer 
    112       1.64   thorpej static int
    113       1.77      cube atapibusmatch(device_t parent, cfdata_t cf, void *aux)
    114        1.2    bouyer {
    115       1.44    bouyer 	struct scsipi_channel *chan = aux;
    116        1.2    bouyer 
    117       1.44    bouyer 	if (chan == NULL)
    118        1.4     enami 		return (0);
    119       1.39    bouyer 
    120  1.83.12.1       mrg 	if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) !=
    121  1.83.12.1       mrg 	    SCSIPI_BUSTYPE_ATAPI)
    122       1.39    bouyer 		return (0);
    123       1.39    bouyer 
    124        1.4     enami 	return (1);
    125        1.2    bouyer }
    126        1.2    bouyer 
    127       1.64   thorpej static int
    128       1.77      cube atapibussubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    129        1.2    bouyer {
    130        1.2    bouyer 	struct scsipibus_attach_args *sa = aux;
    131       1.39    bouyer 	struct scsipi_periph *periph = sa->sa_periph;
    132        1.2    bouyer 
    133        1.2    bouyer 	if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
    134       1.39    bouyer 	    cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
    135        1.4     enami 		return (0);
    136       1.50   thorpej 	return (config_match(parent, cf, aux));
    137        1.2    bouyer }
    138        1.2    bouyer 
    139       1.64   thorpej static void
    140       1.75    dyoung atapibusattach(device_t parent, device_t self, void *aux)
    141        1.2    bouyer {
    142       1.70   thorpej 	struct atapibus_softc *sc = device_private(self);
    143       1.44    bouyer 	struct scsipi_channel *chan = aux;
    144       1.39    bouyer 
    145       1.39    bouyer 	sc->sc_channel = chan;
    146       1.77      cube 	sc->sc_dev = self;
    147       1.48    bouyer 
    148       1.77      cube 	chan->chan_name = device_xname(sc->sc_dev);
    149       1.39    bouyer 
    150       1.39    bouyer 	/* ATAPI has no LUNs. */
    151       1.39    bouyer 	chan->chan_nluns = 1;
    152       1.73  jmcneill 	aprint_naive("\n");
    153       1.73  jmcneill 	aprint_normal(": %d targets\n", chan->chan_ntargets);
    154       1.39    bouyer 
    155       1.39    bouyer 	/* Initialize the channel. */
    156       1.49       jmc 	chan->chan_init_cb = NULL;
    157       1.49       jmc 	chan->chan_init_cb_arg = NULL;
    158       1.39    bouyer 	scsipi_channel_init(chan);
    159       1.39    bouyer 
    160       1.74  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    161       1.74  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    162       1.74  jmcneill 
    163       1.39    bouyer 	/* Probe the bus for devices. */
    164       1.39    bouyer 	atapi_probe_bus(sc, -1);
    165       1.27     enami }
    166       1.27     enami 
    167       1.75    dyoung static void
    168       1.75    dyoung atapibuschilddet(device_t self, device_t child)
    169       1.75    dyoung {
    170       1.75    dyoung 	struct atapibus_softc *sc = device_private(self);
    171       1.75    dyoung 	struct scsipi_channel *chan = sc->sc_channel;
    172       1.75    dyoung 	struct scsipi_periph *periph;
    173       1.75    dyoung 	int target;
    174       1.75    dyoung 
    175  1.83.12.1       mrg 	/* XXXSMP scsipi */
    176  1.83.12.1       mrg 	KERNEL_LOCK(1, curlwp);
    177  1.83.12.1       mrg 
    178       1.75    dyoung 	for (target = 0; target < chan->chan_ntargets; target++) {
    179       1.75    dyoung 		periph = scsipi_lookup_periph(chan, target, 0);
    180       1.75    dyoung 		if (periph == NULL || periph->periph_dev != child)
    181       1.75    dyoung 			continue;
    182       1.75    dyoung 		scsipi_remove_periph(chan, periph);
    183       1.75    dyoung 		free(periph, M_DEVBUF);
    184       1.75    dyoung 		break;
    185       1.75    dyoung 	}
    186  1.83.12.1       mrg 
    187  1.83.12.1       mrg 	/* XXXSMP scsipi */
    188  1.83.12.1       mrg 	KERNEL_UNLOCK_ONE(curlwp);
    189       1.75    dyoung }
    190       1.75    dyoung 
    191       1.64   thorpej static int
    192       1.75    dyoung atapibusdetach(device_t self, int flags)
    193       1.27     enami {
    194       1.70   thorpej 	struct atapibus_softc *sc = device_private(self);
    195       1.39    bouyer 	struct scsipi_channel *chan = sc->sc_channel;
    196       1.39    bouyer 	struct scsipi_periph *periph;
    197       1.27     enami 	int target, error;
    198       1.27     enami 
    199       1.39    bouyer 	/*
    200       1.39    bouyer 	 * Shut down the channel.
    201       1.39    bouyer 	 */
    202       1.39    bouyer 	scsipi_channel_shutdown(chan);
    203       1.39    bouyer 
    204       1.39    bouyer 	/*
    205       1.39    bouyer 	 * Now detach all of the periphs.
    206       1.39    bouyer 	 */
    207       1.39    bouyer 	for (target = 0; target < chan->chan_ntargets; target++) {
    208       1.39    bouyer 		periph = scsipi_lookup_periph(chan, target, 0);
    209       1.39    bouyer 		if (periph == NULL)
    210       1.27     enami 			continue;
    211       1.39    bouyer 		error = config_detach(periph->periph_dev, flags);
    212       1.39    bouyer 		if (error)
    213       1.27     enami 			return (error);
    214       1.76      yamt 		KASSERT(scsipi_lookup_periph(chan, target, 0) == NULL);
    215       1.27     enami 	}
    216       1.27     enami 	return (0);
    217        1.2    bouyer }
    218        1.2    bouyer 
    219       1.64   thorpej static int
    220       1.64   thorpej atapi_probe_bus(struct atapibus_softc *sc, int target)
    221        1.2    bouyer {
    222       1.39    bouyer 	struct scsipi_channel *chan = sc->sc_channel;
    223        1.2    bouyer 	int maxtarget, mintarget;
    224       1.15   thorpej 	int error;
    225       1.34    bouyer 	struct atapi_adapter *atapi_adapter;
    226        1.4     enami 
    227        1.2    bouyer 	if (target == -1) {
    228        1.2    bouyer 		maxtarget = 1;
    229        1.2    bouyer 		mintarget = 0;
    230        1.2    bouyer 	} else {
    231       1.39    bouyer 		if (target < 0 || target >= chan->chan_ntargets)
    232        1.4     enami 			return (ENXIO);
    233        1.2    bouyer 		maxtarget = mintarget = target;
    234        1.2    bouyer 	}
    235       1.39    bouyer 
    236       1.39    bouyer 	if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
    237       1.15   thorpej 		return (error);
    238       1.39    bouyer 	atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
    239        1.4     enami 	for (target = mintarget; target <= maxtarget; target++)
    240       1.39    bouyer 		atapi_adapter->atapi_probe_device(sc, target);
    241       1.39    bouyer 	scsipi_adapter_delref(chan->chan_adapter);
    242        1.4     enami 	return (0);
    243        1.2    bouyer }
    244        1.2    bouyer 
    245       1.34    bouyer void *
    246       1.72  christos atapi_probe_device(struct atapibus_softc *sc, int target,
    247       1.64   thorpej     struct scsipi_periph *periph, struct scsipibus_attach_args *sa)
    248        1.2    bouyer {
    249       1.39    bouyer 	struct scsipi_channel *chan = sc->sc_channel;
    250       1.67  christos 	const struct scsi_quirk_inquiry_pattern *finger;
    251       1.79    cegger 	cfdata_t cf;
    252       1.39    bouyer 	int priority, quirks;
    253        1.2    bouyer 
    254       1.67  christos 	finger = scsipi_inqmatch(
    255       1.67  christos 	    &sa->sa_inqbuf, (const void *)atapi_quirk_patterns,
    256       1.34    bouyer 	    sizeof(atapi_quirk_patterns) /
    257       1.34    bouyer 	        sizeof(atapi_quirk_patterns[0]),
    258       1.34    bouyer 	    sizeof(atapi_quirk_patterns[0]), &priority);
    259       1.34    bouyer 
    260       1.39    bouyer 	if (finger != NULL)
    261       1.39    bouyer 		quirks = finger->quirks;
    262       1.39    bouyer 	else
    263       1.39    bouyer 		quirks = 0;
    264       1.39    bouyer 
    265       1.39    bouyer 	/*
    266       1.39    bouyer 	 * Now apply any quirks from the table.
    267       1.39    bouyer 	 */
    268       1.39    bouyer 	periph->periph_quirks |= quirks;
    269       1.39    bouyer 
    270       1.77      cube 	if ((cf = config_search_ia(atapibussubmatch, sc->sc_dev,
    271       1.66  drochner 	    "atapibus", sa)) != 0) {
    272       1.39    bouyer 		scsipi_insert_periph(chan, periph);
    273       1.39    bouyer 		/*
    274       1.39    bouyer 		 * XXX Can't assign periph_dev here, because we'll
    275       1.39    bouyer 		 * XXX need it before config_attach() returns.  Must
    276       1.39    bouyer 		 * XXX assign it in periph driver.
    277       1.39    bouyer 		 */
    278       1.77      cube 		return config_attach(sc->sc_dev, cf, sa,
    279       1.39    bouyer 		    atapibusprint);
    280       1.34    bouyer 	} else {
    281       1.77      cube 		atapibusprint(sa, device_xname(sc->sc_dev));
    282       1.34    bouyer 		printf(" not configured\n");
    283       1.39    bouyer 		free(periph, M_DEVBUF);
    284       1.39    bouyer 		return NULL;
    285        1.2    bouyer 	}
    286        1.2    bouyer }
    287        1.2    bouyer 
    288       1.64   thorpej static int
    289       1.64   thorpej atapibusprint(void *aux, const char *pnp)
    290        1.2    bouyer {
    291        1.2    bouyer 	struct scsipibus_attach_args *sa = aux;
    292        1.2    bouyer 	struct scsipi_inquiry_pattern *inqbuf;
    293       1.65   thorpej 	const char *dtype;
    294        1.2    bouyer 
    295        1.2    bouyer 	if (pnp != NULL)
    296       1.55   thorpej 		aprint_normal("%s", pnp);
    297        1.2    bouyer 
    298        1.2    bouyer 	inqbuf = &sa->sa_inqbuf;
    299        1.2    bouyer 
    300        1.2    bouyer 	dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
    301       1.55   thorpej 	aprint_normal(" drive %d: <%s, %s, %s> %s %s",
    302       1.54     soren 	    sa->sa_periph->periph_target, inqbuf->vendor,
    303       1.54     soren 	    inqbuf->product, inqbuf->revision, dtype,
    304        1.2    bouyer 	    inqbuf->removable ? "removable" : "fixed");
    305        1.2    bouyer 	return (UNCONF);
    306        1.2    bouyer }
    307