Home | History | Annotate | Line # | Download | only in scsipi
atapiconf.c revision 1.25.2.2
      1 /*	$NetBSD: atapiconf.c,v 1.25.2.2 2000/12/14 00:13:53 he Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Manuel Bouyer.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/malloc.h>
     36 #include <sys/device.h>
     37 #include <sys/buf.h>
     38 #include <sys/proc.h>
     39 
     40 #include <dev/ata/atareg.h>
     41 #include <dev/ata/atavar.h>
     42 #include <dev/scsipi/scsipi_all.h>
     43 #include <dev/scsipi/atapi_all.h>
     44 #include <dev/scsipi/scsipiconf.h>
     45 #include <dev/scsipi/atapiconf.h>
     46 
     47 #include "locators.h"
     48 
     49 #define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
     50 
     51 struct atapibus_softc {
     52 	struct device sc_dev;
     53 	struct scsipi_link *adapter_link;	/* proto supplied by adapter */
     54 	struct scsipi_link **sc_link;		/* dynamically allocated */
     55 	struct ata_drive_datas *sc_drvs;	/* array supplied by adapter */
     56 };
     57 
     58 int	atapibusmatch __P((struct device *, struct cfdata *, void *));
     59 int	atapibussubmatch __P((struct device *, struct cfdata *, void *));
     60 void	atapibusattach __P((struct device *, struct device *, void *));
     61 int	atapiprint __P((void *, const char *));
     62 
     63 int	atapi_probe_bus __P((int, int));
     64 void	atapi_probedev __P((struct atapibus_softc *, int ));
     65 
     66 struct cfattach atapibus_ca = {
     67 	sizeof(struct atapibus_softc), atapibusmatch, atapibusattach
     68 };
     69 
     70 extern struct cfdriver atapibus_cd;
     71 
     72 int atapibusprint __P((void *, const char *));
     73 
     74 struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
     75 	{{T_CDROM, T_REMOV,
     76 	 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"},	ADEV_NOTUR},
     77 	{{T_CDROM, T_REMOV,
     78 	 "BCD-16X 1997-04-25", "", "VER 2.2"},	SDEV_NOSTARTUNIT},
     79 	{{T_CDROM, T_REMOV,
     80 	 "BCD-24X 1997-06-27", "", "VER 2.0"},	SDEV_NOSTARTUNIT},
     81 	{{T_CDROM, T_REMOV,
     82 	 "CR-2801TE", "", "1.07"},		ADEV_NOSENSE},
     83 	{{T_CDROM, T_REMOV,
     84 	 "CREATIVECD3630E", "", "AC101"},	ADEV_NOSENSE},
     85 	{{T_CDROM, T_REMOV,
     86 	 "FX320S", "", "q01"},			ADEV_NOSENSE},
     87 	{{T_CDROM, T_REMOV,
     88 	 "GCD-R580B", "", "1.00"},		ADEV_LITTLETOC},
     89 	{{T_CDROM, T_REMOV,
     90 	 "HITACHI CDR-7730", "", "0008a"},	ADEV_NOSENSE},
     91 	{{T_CDROM, T_REMOV,
     92 	 "MATSHITA CR-574", "", "1.02"},	ADEV_NOCAPACITY},
     93 	{{T_CDROM, T_REMOV,
     94 	 "MATSHITA CR-574", "", "1.06"},	ADEV_NOCAPACITY},
     95 	{{T_CDROM, T_REMOV,
     96 	 "Memorex CRW-2642", "", "1.0g"},	ADEV_NOSENSE},
     97 	{{T_CDROM, T_REMOV,
     98 	 "NEC                 CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR},
     99 	{{T_CDROM, T_REMOV,
    100 	 "SANYO CRD-256P", "", "1.02"},		ADEV_NOCAPACITY},
    101 	{{T_CDROM, T_REMOV,
    102 	 "SANYO CRD-254P", "", "1.02"},		ADEV_NOCAPACITY},
    103 	{{T_CDROM, T_REMOV,
    104 	 "SANYO CRD-S54P", "", "1.08"},		ADEV_NOCAPACITY},
    105 	{{T_CDROM, T_REMOV,
    106 	 "CD-ROM  CDR-S1", "", "1.70"},		ADEV_NOCAPACITY}, /* Sanyo */
    107 	{{T_CDROM, T_REMOV,
    108 	 "CD-ROM  CDR-N16", "", "1.25"},	ADEV_NOCAPACITY}, /* Sanyo */
    109 	{{T_CDROM, T_REMOV,
    110 	 "UJDCD8730", "", "1.14"},		ADEV_NODOORLOCK}, /* Acer */
    111 };
    112 
    113 int
    114 atapibusmatch(parent, cf, aux)
    115 	struct device *parent;
    116 	struct cfdata *cf;
    117 	void *aux;
    118 {
    119 	struct ata_atapi_attach *aa_link = aux;
    120 
    121 	if (aa_link == NULL)
    122 		return (0);
    123 	if (aa_link->aa_type != T_ATAPI)
    124 		return (0);
    125 	if (cf->cf_loc[ATAPICF_CHANNEL] != aa_link->aa_channel &&
    126 	    cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
    127 	    return 0;
    128 	return (1);
    129 }
    130 
    131 int
    132 atapibussubmatch(parent, cf, aux)
    133 	struct device *parent;
    134 	struct cfdata *cf;
    135 	void *aux;
    136 {
    137 	struct scsipibus_attach_args *sa = aux;
    138 	struct scsipi_link *sc_link = sa->sa_sc_link;
    139 
    140 	if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
    141 	    cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive)
    142 		return (0);
    143 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    144 }
    145 
    146 #if 0
    147 void
    148 atapi_fixquirk(sc_link)
    149 	struct scsipi_link *ad_link;
    150 {
    151 	struct ataparams *id = &ad_link->id;
    152 	struct atapi_quirk_inquiry_pattern *quirk;
    153 
    154 	/*
    155 	 * Clean up the model name, serial and revision numbers.
    156 	 */
    157 	btrim(id->model, sizeof(id->model));
    158 	btrim(id->serial_number, sizeof(id->serial_number));
    159 	btrim(id->firmware_revision, sizeof(id->firmware_revision));
    160 }
    161 #endif
    162 
    163 void
    164 atapibusattach(parent, self, aux)
    165 	struct device *parent, *self;
    166 	void *aux;
    167 {
    168 	struct atapibus_softc *sc_ab = (struct atapibus_softc *)self;
    169 	struct ata_atapi_attach *aa_link = aux;
    170 	struct scsipi_link *sc_link_proto;
    171 	int nbytes;
    172 
    173 	printf("\n");
    174 
    175 	/* Initialize shared data. */
    176 	scsipi_init();
    177 
    178 	sc_link_proto = malloc(sizeof(struct scsipi_link),
    179 	    M_DEVBUF, M_NOWAIT);
    180 	if (sc_link_proto == NULL)
    181 	    printf("atapibusattach : can't allocate scsipi link proto\n");
    182 	memset(sc_link_proto, 0, sizeof(struct scsipi_link));
    183 
    184 	sc_link_proto->type = BUS_ATAPI;
    185 	sc_link_proto->openings = aa_link->aa_openings;
    186 	sc_link_proto->scsipi_atapi.channel = aa_link->aa_channel;
    187 	sc_link_proto->adapter_softc = parent;
    188 	sc_link_proto->adapter = aa_link->aa_bus_private;
    189 	sc_link_proto->scsipi_atapi.atapibus = sc_ab->sc_dev.dv_unit;
    190 	sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
    191 	sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
    192 	sc_link_proto->sc_print_addr = atapi_print_addr;
    193 
    194 
    195 	sc_ab->adapter_link = sc_link_proto;
    196 	sc_ab->sc_drvs = aa_link->aa_drv_data;
    197 
    198 	nbytes = 2 * sizeof(struct scsipi_link **);
    199 	sc_ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
    200 	    M_NOWAIT);
    201 	if (sc_ab->sc_link == NULL)
    202 		panic("scsibusattach: can't allocate target links");
    203 	memset(sc_ab->sc_link, 0, nbytes);
    204 	atapi_probe_bus(sc_ab->sc_dev.dv_unit, -1);
    205 }
    206 
    207 int
    208 atapi_probe_bus(bus, target)
    209 	int bus, target;
    210 {
    211 	int maxtarget, mintarget;
    212 	struct atapibus_softc *atapi;
    213 	int error;
    214 
    215 	if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
    216 		return (ENXIO);
    217 	atapi = atapibus_cd.cd_devs[bus];
    218 	if (atapi == NULL)
    219 		return (ENXIO);
    220 
    221 	if (target == -1) {
    222 		maxtarget = 1;
    223 		mintarget = 0;
    224 	} else {
    225 		if (target < 0 || target > 1)
    226 			return (ENXIO);
    227 		maxtarget = mintarget = target;
    228 	}
    229 	if ((error = scsipi_adapter_addref(atapi->adapter_link)) != 0)
    230 		return (error);
    231 	for (target = mintarget; target <= maxtarget; target++)
    232 		atapi_probedev(atapi, target);
    233 	scsipi_adapter_delref(atapi->adapter_link);
    234 	return (0);
    235 }
    236 
    237 void
    238 atapi_probedev(atapi, target)
    239 	struct atapibus_softc *atapi;
    240 	int target;
    241 {
    242 	struct scsipi_link *sc_link;
    243 	struct scsipibus_attach_args sa;
    244 	struct ataparams ids;
    245 	struct ataparams *id = &ids;
    246 	struct ata_drive_datas *drvp = &atapi->sc_drvs[target];
    247 	struct cfdata *cf;
    248 	struct scsi_quirk_inquiry_pattern *finger;
    249 	int priority;
    250 	char serial_number[21], model[41], firmware_revision[9];
    251 
    252 	/* skip if already attached */
    253 	if (atapi->sc_link[target])
    254 		return;
    255 
    256 	if (wdc_atapi_get_params(atapi->adapter_link, target,
    257 	    SCSI_POLL|SCSI_NOSLEEP, id) == COMPLETE) {
    258 #ifdef ATAPI_DEBUG_PROBE
    259 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
    260 		    atapi->sc_dev.dv_xname, target,
    261 		    id->atap_config & ATAPI_CFG_CMD_MASK,
    262 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
    263 #endif
    264 		/*
    265 		 * Allocate a device link and try and attach
    266 		 * a driver to this device.  If we fail, free
    267 		 * the link.
    268 		 */
    269 		sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
    270 		if (sc_link == NULL) {
    271 			printf("%s: can't allocate link for drive %d\n",
    272 			    atapi->sc_dev.dv_xname, target);
    273 			return;
    274 		}
    275 		/* Fill in link. */
    276 		*sc_link = *atapi->adapter_link;
    277 		sc_link->scsipi_atapi.drive = target;
    278 		sc_link->device = NULL;
    279 		TAILQ_INIT(&sc_link->pending_xfers);
    280 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
    281 		if (DEBUGTARGET == -1 || target == DEBUGTARGET)
    282 			sc_link->flags |= DEBUGLEVEL;
    283 #endif /* SCSIDEBUG */
    284 		if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
    285 			sc_link->scsipi_atapi.cap |= ACAP_LEN;
    286 		sc_link->scsipi_atapi.cap |=
    287 		    (id->atap_config & ATAPI_CFG_DRQ_MASK);
    288 		sa.sa_sc_link = sc_link;
    289 		sa.sa_inqbuf.type =  ATAPI_CFG_TYPE(id->atap_config);
    290 		sa.sa_inqbuf.removable =
    291 		    id->atap_config & ATAPI_CFG_REMOV ? T_REMOV : T_FIXED;
    292 		if (sa.sa_inqbuf.removable)
    293 			sc_link->flags |= SDEV_REMOVABLE;
    294 		scsipi_strvis(model, 40, id->atap_model, 40);
    295 		scsipi_strvis(serial_number, 20, id->atap_serial, 20);
    296 		scsipi_strvis(firmware_revision, 8, id->atap_revision, 8);
    297 		sa.sa_inqbuf.vendor = model;
    298 		sa.sa_inqbuf.product = serial_number;
    299 		sa.sa_inqbuf.revision = firmware_revision;
    300 
    301 		finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
    302 		    &sa.sa_inqbuf, (caddr_t)atapi_quirk_patterns,
    303 		    sizeof(atapi_quirk_patterns) /
    304 		        sizeof(atapi_quirk_patterns[0]),
    305 		    sizeof(atapi_quirk_patterns[0]), &priority);
    306 		if (priority != 0)
    307 			sc_link->quirks |= finger->quirks;
    308 
    309 		if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
    310 		    &sa)) != 0) {
    311 			atapi->sc_link[target] = sc_link;
    312 			drvp->drv_softc = config_attach(&atapi->sc_dev, cf,
    313 			    &sa, atapibusprint);
    314 			wdc_probe_caps(drvp);
    315 			return;
    316 		} else {
    317 			atapibusprint(&sa, atapi->sc_dev.dv_xname);
    318 			printf(" not configured\n");
    319 			free(sc_link, M_DEVBUF);
    320 			return;
    321 		}
    322 	}
    323 }
    324 
    325 int
    326 atapibusprint(aux, pnp)
    327 	void *aux;
    328 	const char *pnp;
    329 {
    330 	struct scsipibus_attach_args *sa = aux;
    331 	struct scsipi_inquiry_pattern *inqbuf;
    332 	char *dtype;
    333 
    334 	if (pnp != NULL)
    335 		printf("%s", pnp);
    336 
    337 	inqbuf = &sa->sa_inqbuf;
    338 
    339 	dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
    340 	printf(" drive %d: <%s, %s, %s> type %d %s %s",
    341 	    sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
    342 	    inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
    343 	    inqbuf->removable ? "removable" : "fixed");
    344 	return (UNCONF);
    345 }
    346