Home | History | Annotate | Line # | Download | only in ic
isp_netbsd.c revision 1.5.2.2
      1 /* $NetBSD: isp_netbsd.c,v 1.5.2.2 1998/11/07 05:50:35 cgd Exp $ */
      2 /*
      3  * Platform (NetBSD) dependent common attachment code for Qlogic adapters.
      4  *
      5  *---------------------------------------
      6  * Copyright (c) 1997, 1998 by Matthew Jacob
      7  * NASA/Ames Research Center
      8  * All rights reserved.
      9  *---------------------------------------
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice immediately at the beginning of the file, without modification,
     16  *    this list of conditions, and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  * The author may be reached via electronic communications at
     36  *
     37  *  mjacob (at) nas.nasa.gov
     38  *  mjacob (at) feral.com
     39  *
     40  * or, via United States Postal Address
     41  *
     42  *  Matthew Jacob
     43  *  Feral Software
     44  *  2339 3rd Street
     45  *  Suite 24
     46  *  San Francisco, CA, 94107
     47  */
     48 
     49 #include <dev/ic/isp_netbsd.h>
     50 
     51 struct cfdriver isp_cd = {
     52 	NULL, "isp", DV_DULL
     53 };
     54 
     55 static void ispminphys __P((struct buf *));
     56 static int32_t ispcmd __P((ISP_SCSI_XFER_T *));
     57 
     58 static struct scsipi_adapter isp_switch = {
     59 	ispcmd,			/* scsipi_cmd */
     60 	ispminphys,		/* scsipi_minphys */
     61 	NULL,			/* scsipi_ioctl */
     62 };
     63 
     64 static struct scsipi_device isp_dev = { NULL, NULL, NULL, NULL };
     65 static int isp_poll __P((struct ispsoftc *, ISP_SCSI_XFER_T *, int));
     66 
     67 /*
     68  * Complete attachment of hardware, include subdevices.
     69  */
     70 void
     71 isp_attach(isp)
     72 	struct ispsoftc *isp;
     73 {
     74 	isp->isp_state = ISP_RUNSTATE;
     75 	isp->isp_osinfo._link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
     76 	isp->isp_osinfo._link.adapter_softc = isp;
     77 	isp->isp_osinfo._link.device = &isp_dev;
     78 	isp->isp_osinfo._link.adapter = &isp_switch;
     79 
     80 	if (isp->isp_type & ISP_HA_FC) {
     81 		isp->isp_osinfo._link.scsipi_scsi.max_target = MAX_FC_TARG-1;
     82 		isp->isp_osinfo._link.openings =
     83 			RQUEST_QUEUE_LEN / (MAX_FC_TARG-1);
     84 		isp->isp_osinfo._link.scsipi_scsi.adapter_target = 0xff;
     85 	} else {
     86 		isp->isp_osinfo._link.openings =
     87 			RQUEST_QUEUE_LEN / (MAX_TARGETS-1);
     88 		isp->isp_osinfo._link.scsipi_scsi.max_target = MAX_TARGETS-1;
     89 		isp->isp_osinfo._link.scsipi_scsi.adapter_target =
     90 			((sdparam *)isp->isp_param)->isp_initiator_id;
     91 	}
     92 	if (isp->isp_osinfo._link.openings < 2)
     93 		isp->isp_osinfo._link.openings = 2;
     94 	isp->isp_osinfo._link.type = BUS_SCSI;
     95 	config_found((void *)isp, &isp->isp_osinfo._link, scsiprint);
     96 }
     97 
     98 /*
     99  * minphys our xfers
    100  *
    101  * Unfortunately, the buffer pointer describes the target device- not the
    102  * adapter device, so we can't use the pointer to find out what kind of
    103  * adapter we are and adjust accordingly.
    104  */
    105 
    106 static void
    107 ispminphys(bp)
    108 	struct buf *bp;
    109 {
    110 	/*
    111 	 * XX: Only the 1020 has a 24 bit limit.
    112 	 */
    113 	if (bp->b_bcount >= (1 << 24)) {
    114 		bp->b_bcount = (1 << 24);
    115 	}
    116 	minphys(bp);
    117 }
    118 
    119 static int
    120 ispcmd(xs)
    121 	ISP_SCSI_XFER_T *xs;
    122 {
    123 	struct ispsoftc *isp;
    124 	int result;
    125 	ISP_LOCKVAL_DECL;
    126 
    127 	isp = XS_ISP(xs);
    128 	ISP_LOCK(isp);
    129 	/*
    130 	 * This is less efficient than I would like in that the
    131 	 * majority of cases will have to do some pointer deferences
    132 	 * to find out that things don't need to be updated.
    133 	 */
    134 	if ((xs->flags & SCSI_AUTOCONF) == 0 && (isp->isp_type & ISP_HA_SCSI)) {
    135 		sdparam *sdp = isp->isp_param;
    136 		if (sdp->isp_devparam[XS_TGT(xs)].dev_flags !=
    137 		    sdp->isp_devparam[XS_TGT(xs)].cur_dflags) {
    138 			u_int16_t f = DPARM_WIDE|DPARM_SYNC|DPARM_TQING;
    139 #ifdef	SDEV_NOSYNC
    140 			if (xs->sc_link->quirks & SDEV_NOSYNC)
    141 				f &= ~DPARM_SYNC;
    142 #endif
    143 #ifdef	SDEV_NOWIDE
    144 			if (xs->sc_link->quirks & SDEV_NOWIDE)
    145 				f &= ~DPARM_WIDE;
    146 #endif
    147 #ifdef	SDEV_NOTAG
    148 			if (xs->sc_link->quirks & SDEV_NOTAG)
    149 				f &= ~DPARM_TQING;
    150 #endif
    151 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &=
    152 				~(DPARM_WIDE|DPARM_SYNC|DPARM_TQING);
    153 			sdp->isp_devparam[XS_TGT(xs)].dev_flags |= f;
    154 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
    155 			isp->isp_update = 1;
    156 		}
    157 	}
    158 	result = ispscsicmd(xs);
    159 	if (result != CMD_QUEUED || (xs->flags & SCSI_POLL) == 0) {
    160 		ISP_UNLOCK(isp);
    161 		return (result);
    162 	}
    163 
    164 	/*
    165 	 * If we can't use interrupts, poll on completion.
    166 	 */
    167 	if (isp_poll(isp, xs, XS_TIME(xs))) {
    168 		/*
    169 		 * If no other error occurred but we didn't finish,
    170 		 * something bad happened.
    171 		 */
    172 		if (XS_IS_CMD_DONE(xs) == 0) {
    173 			isp->isp_nactive--;
    174 			if (isp->isp_nactive < 0)
    175 				isp->isp_nactive = 0;
    176 			if (XS_NOERR(xs)) {
    177 				isp_lostcmd(isp, xs);
    178 				XS_SETERR(xs, HBA_BOTCH);
    179 			}
    180 		}
    181 	}
    182 	ISP_UNLOCK(isp);
    183 	return (CMD_COMPLETE);
    184 }
    185 
    186 static int
    187 isp_poll(isp, xs, mswait)
    188 	struct ispsoftc *isp;
    189 	ISP_SCSI_XFER_T *xs;
    190 	int mswait;
    191 {
    192 
    193 	while (mswait) {
    194 		/* Try the interrupt handling routine */
    195 		(void)isp_intr((void *)isp);
    196 
    197 		/* See if the xs is now done */
    198 		if (XS_IS_CMD_DONE(xs)) {
    199 			return (0);
    200 		}
    201 		SYS_DELAY(1000);	/* wait one millisecond */
    202 		mswait--;
    203 	}
    204 	return (1);
    205 }
    206