Home | History | Annotate | Line # | Download | only in scsipi
atapiconf.c revision 1.9.2.2
      1 /*	$NetBSD: atapiconf.c,v 1.9.2.2 1998/08/13 14:27:51 bouyer 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 #ifdef __BROKEN_INDIRECT_CONFIG
     59 int	atapibusmatch __P((struct device *, void *, void *));
     60 int	atapibussubmatch __P((struct device *, void *, void *));
     61 #else
     62 int	atapibusmatch __P((struct device *, struct cfdata *, void *));
     63 int	atapibussubmatch __P((struct device *, struct cfdata *, void *));
     64 #endif
     65 void	atapibusattach __P((struct device *, struct device *, void *));
     66 int	atapiprint __P((void *, const char *));
     67 
     68 int	atapi_probe_bus __P((int, int));
     69 void	atapi_probedev __P((struct atapibus_softc *, int ));
     70 
     71 struct cfattach atapibus_ca = {
     72 	sizeof(struct atapibus_softc), atapibusmatch, atapibusattach
     73 };
     74 
     75 extern struct cfdriver atapibus_cd;
     76 
     77 int atapibusprint __P((void *, const char *));
     78 
     79 struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
     80 	{{T_CDROM, T_REMOV,
     81 	 "GCD-R580B", "", "1.00"},                ADEV_LITTLETOC}, /* Goldstar */
     82 	{{T_CDROM, T_REMOV,
     83 	 "SANYO CRD-256P", "", "1.02"},           ADEV_NOCAPACITY},
     84 	{{T_CDROM, T_REMOV,
     85 	 "SANYO CRD-254P", "", "1.02"},           ADEV_NOCAPACITY},
     86 	{{T_CDROM, T_REMOV,
     87 	 "SANYO CRD-S54P", "", "1.08"},           ADEV_NOCAPACITY},
     88 	{{T_CDROM, T_REMOV,
     89 	 "CD-ROM  CDR-S1", "", "1.70"},           ADEV_NOCAPACITY}, /* Sanyo */
     90 	{{T_CDROM, T_REMOV,
     91 	 "CD-ROM  CDR-N16", "", "1.25"},          ADEV_NOCAPACITY}, /* Sanyo */
     92 	{{T_CDROM, T_REMOV,
     93 	 "UJDCD8730", "", "1.14"},                ADEV_NODOORLOCK}, /* Acer */
     94 	{{T_CDROM, T_REMOV,
     95 	 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, ADEV_NOTUR},
     96 	{{T_CDROM, T_REMOV,
     97 	 "NEC                 CD-ROM DRIVE:273", "", "4.21"}, ADEV_NOTUR},
     98 	{{T_CDROM, T_REMOV,
     99 	 "MATSHITA CR-574", "", "1.06"},          ADEV_NOCAPACITY},
    100 	{{T_CDROM, T_REMOV,
    101 	 "MATSHITA CR-574", "", "1.02"},          ADEV_NOCAPACITY},
    102 };
    103 
    104 int
    105 #ifdef __BROKEN_INDIRECT_CONFIG
    106 atapibusmatch(parent, match, aux)
    107 	struct device *parent;
    108 	void *match, *aux;
    109 {
    110 #else
    111 atapibusmatch(parent, cf, aux)
    112 	struct device *parent;
    113 	struct cfdata *cf;
    114 	void *aux;
    115 {
    116 #endif
    117 	struct ata_atapi_attach *aa_link = aux;
    118 
    119 	if (aa_link == NULL)
    120 		return (0);
    121 	if (aa_link->aa_type != T_ATAPI)
    122 		return (0);
    123 	if (cf->cf_loc[ATAPICF_CHANNEL] != aa_link->aa_channel &&
    124 	    cf->cf_loc[ATAPICF_CHANNEL] != ATAPICF_CHANNEL_DEFAULT)
    125 	    return 0;
    126 	return (1);
    127 }
    128 
    129 int
    130 #ifdef __BROKEN_INDIRECT_CONFIG
    131 atapibussubmatch(parent, match, aux)
    132 	struct device *parent;
    133 	void *match, *aux;
    134 {
    135 	struct cfdata *cf = match;
    136 #else
    137 atapibussubmatch(parent, cf, aux)
    138 	struct device *parent;
    139 	struct cfdata *cf;
    140 	void *aux;
    141 {
    142 #endif
    143 	struct scsipibus_attach_args *sa = aux;
    144 	struct scsipi_link *sc_link = sa->sa_sc_link;
    145 
    146 	if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
    147 	    cf->cf_loc[ATAPIBUSCF_DRIVE] != sc_link->scsipi_atapi.drive)
    148 		return (0);
    149 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    150 }
    151 
    152 #if 0
    153 void
    154 atapi_fixquirk(sc_link)
    155 	struct scsipi_link *ad_link;
    156 {
    157 	struct ataparams *id = &ad_link->id;
    158 	struct atapi_quirk_inquiry_pattern *quirk;
    159 
    160 	/*
    161 	 * Clean up the model name, serial and revision numbers.
    162 	 */
    163 	btrim(id->model, sizeof(id->model));
    164 	btrim(id->serial_number, sizeof(id->serial_number));
    165 	btrim(id->firmware_revision, sizeof(id->firmware_revision));
    166 }
    167 #endif
    168 
    169 void
    170 atapibusattach(parent, self, aux)
    171 	struct device *parent, *self;
    172 	void *aux;
    173 {
    174 	struct atapibus_softc *sc_ab = (struct atapibus_softc *)self;
    175 	struct ata_atapi_attach *aa_link = aux;
    176 	struct scsipi_link *sc_link_proto;
    177 	int nbytes;
    178 
    179 	printf("\n");
    180 
    181 	/* Initialize shared data. */
    182 	scsipi_init();
    183 
    184 	sc_link_proto = malloc(sizeof(struct scsipi_link),
    185 	    M_DEVBUF, M_NOWAIT);
    186 	if (sc_link_proto == NULL)
    187 	    printf("atapibusattach : can't allocate scsipi link proto\n");
    188 	memset(sc_link_proto, 0, sizeof(struct scsipi_link));
    189 
    190 	sc_link_proto->type = BUS_ATAPI;
    191 	sc_link_proto->openings = aa_link->aa_openings;
    192 	sc_link_proto->scsipi_atapi.channel = aa_link->aa_channel;
    193 	sc_link_proto->adapter_softc = parent;
    194 	sc_link_proto->adapter = aa_link->aa_bus_private;
    195 	sc_link_proto->scsipi_atapi.atapibus = sc_ab->sc_dev.dv_unit;
    196 	sc_link_proto->scsipi_cmd = atapi_scsipi_cmd;
    197 	sc_link_proto->scsipi_interpret_sense = atapi_interpret_sense;
    198 	sc_link_proto->sc_print_addr = atapi_print_addr;
    199 
    200 
    201 	sc_ab->adapter_link = sc_link_proto;
    202 	sc_ab->sc_drvs = aa_link->aa_drv_data;
    203 
    204 	nbytes = 2 * sizeof(struct scsipi_link **);
    205 	sc_ab->sc_link = (struct scsipi_link **)malloc(nbytes, M_DEVBUF,
    206 	    M_NOWAIT);
    207 	if (sc_ab->sc_link == NULL)
    208 		panic("scsibusattach: can't allocate target links");
    209 	memset(sc_ab->sc_link, 0, nbytes);
    210 	atapi_probe_bus(sc_ab->sc_dev.dv_unit, -1);
    211 }
    212 
    213 int
    214 atapi_probe_bus(bus, target)
    215 	int bus, target;
    216 {
    217 	int maxtarget, mintarget;
    218 	struct atapibus_softc *atapi;
    219 
    220 	if (bus < 0 || bus >= atapibus_cd.cd_ndevs)
    221 		return (ENXIO);
    222 	atapi = atapibus_cd.cd_devs[bus];
    223 	if (atapi == NULL)
    224 		return (ENXIO);
    225 
    226 	if (target == -1) {
    227 		maxtarget = 1;
    228 		mintarget = 0;
    229 	} else {
    230 		if (target < 0 || target > 1)
    231 			return (ENXIO);
    232 		maxtarget = mintarget = target;
    233 	}
    234 	for (target = mintarget; target <= maxtarget; target++)
    235 		atapi_probedev(atapi, target);
    236 	return (0);
    237 }
    238 
    239 void
    240 atapi_probedev(atapi, target)
    241 	struct atapibus_softc *atapi;
    242 	int target;
    243 {
    244 	struct scsipi_link *sc_link;
    245 	struct scsipibus_attach_args sa;
    246 	struct ataparams ids;
    247 	struct ataparams *id = &ids;
    248 	struct ata_drive_datas *drvp = &atapi->sc_drvs[target];
    249 	struct cfdata *cf;
    250 	struct scsi_quirk_inquiry_pattern *finger;
    251 	int priority;
    252 	char serial_number[20], model[40], firmware_revision[8];
    253 
    254 	/* skip if already attached */
    255 	if (atapi->sc_link[target])
    256 		return;
    257 
    258 	if (wdc_atapi_get_params(atapi->adapter_link, target,
    259 	    SCSI_POLL|SCSI_NOSLEEP, id) == COMPLETE) {
    260 #ifdef ATAPI_DEBUG_PROBE
    261 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
    262 		    atapi->sc_dev.dv_xname, target,
    263 		    id->config.cmd_drq_rem & ATAPI_PACKET_SIZE_MASK,
    264 		    id->config.cmd_drq_rem & ATAPI_DRQ_MASK);
    265 #endif
    266 		/*
    267 		 * Allocate a device link and try and attach
    268 		 * a driver to this device.  If we fail, free
    269 		 * the link.
    270 		 */
    271 		sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
    272 		if (sc_link == NULL) {
    273 			printf("%s: can't allocate link for drive %d\n",
    274 			    atapi->sc_dev.dv_xname, target);
    275 			return;
    276 		}
    277 		/* Fill in link. */
    278 		*sc_link = *atapi->adapter_link;
    279 		sc_link->scsipi_atapi.drive = target;
    280 		sc_link->device = NULL;
    281 #if defined(SCSIDEBUG) && DEBUGTYPE == BUS_ATAPI
    282 		if (DEBUGTARGET == -1 || target == DEBUGTARGET)
    283 			sc_link->flags |= DEBUGLEVEL;
    284 #endif /* SCSIDEBUG */
    285 		if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
    286 			sc_link->scsipi_atapi.cap |= ACAP_LEN;
    287 		sc_link->scsipi_atapi.cap |=
    288 		    (id->atap_config & ATAPI_CFG_DRQ_MASK);
    289 		sa.sa_sc_link = sc_link;
    290 		sa.sa_inqbuf.type =  ATAPI_CFG_TYPE(id->atap_config);
    291 		sa.sa_inqbuf.removable =
    292 		    id->atap_config & ATAPI_CFG_REMOV ? T_REMOV : T_FIXED;
    293 		if (sa.sa_inqbuf.removable)
    294 			sc_link->flags |= SDEV_REMOVABLE;
    295 		scsipi_strvis(model, 40, id->atap_model, 40);
    296 		scsipi_strvis(serial_number, 20, id->atap_serial, 20);
    297 		scsipi_strvis(firmware_revision, 8, id->atap_revision, 8);
    298 		sa.sa_inqbuf.vendor = model;
    299 		sa.sa_inqbuf.product = serial_number;
    300 		sa.sa_inqbuf.revision = firmware_revision;
    301 
    302 		finger = (struct scsi_quirk_inquiry_pattern *)scsipi_inqmatch(
    303 		    &sa.sa_inqbuf, (caddr_t)atapi_quirk_patterns,
    304 		    sizeof(atapi_quirk_patterns) /
    305 		        sizeof(atapi_quirk_patterns[0]),
    306 		    sizeof(atapi_quirk_patterns[0]), &priority);
    307 		if (priority != 0)
    308 			sc_link->quirks |= finger->quirks;
    309 
    310 		if ((cf = config_search(atapibussubmatch, &atapi->sc_dev,
    311 		    &sa)) != 0) {
    312 			atapi->sc_link[target] = sc_link;
    313 			drvp->drv_softc = config_attach(&atapi->sc_dev, cf,
    314 			    &sa, atapibusprint);
    315 			wdc_probe_caps(drvp);
    316 			/* drvp->drive_flags &= ~DRIVE_DMA; XXX */
    317 			return;
    318 		} else {
    319 			atapibusprint(&sa, atapi->sc_dev.dv_xname);
    320 			printf(" not configured\n");
    321 			free(sc_link, M_DEVBUF);
    322 			return;
    323 		}
    324 	}
    325 }
    326 
    327 int
    328 atapibusprint(aux, pnp)
    329 	void *aux;
    330 	const char *pnp;
    331 {
    332 	struct scsipibus_attach_args *sa = aux;
    333 	struct scsipi_inquiry_pattern *inqbuf;
    334 	char *dtype;
    335 
    336 	if (pnp != NULL)
    337 		printf("%s", pnp);
    338 
    339 	inqbuf = &sa->sa_inqbuf;
    340 
    341 	dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
    342 	printf(" drive %d: <%s, %s, %s> type %d %s %s",
    343 	    sa->sa_sc_link->scsipi_atapi.drive,inqbuf->vendor,
    344 	    inqbuf->product, inqbuf->revision, inqbuf->type, dtype,
    345 	    inqbuf->removable ? "removable" : "fixed");
    346 	return (UNCONF);
    347 }
    348