Home | History | Annotate | Line # | Download | only in ic
icpsp.c revision 1.7
      1 /*	$NetBSD: icpsp.c,v 1.7 2003/05/13 15:42:34 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: icpsp.c,v 1.7 2003/05/13 15:42:34 thorpej Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/device.h>
     46 #include <sys/queue.h>
     47 #include <sys/proc.h>
     48 #include <sys/buf.h>
     49 #include <sys/endian.h>
     50 #include <sys/malloc.h>
     51 #include <sys/scsiio.h>
     52 #include <sys/lock.h>
     53 
     54 #include <machine/bswap.h>
     55 #include <machine/bus.h>
     56 
     57 #include <uvm/uvm_extern.h>
     58 
     59 #include <dev/scsipi/scsi_all.h>
     60 #include <dev/scsipi/scsi_disk.h>
     61 #include <dev/scsipi/scsipi_all.h>
     62 #include <dev/scsipi/scsiconf.h>
     63 #include <dev/scsipi/scsi_message.h>
     64 
     65 #include <dev/ic/icpreg.h>
     66 #include <dev/ic/icpvar.h>
     67 
     68 struct icpsp_softc {
     69 	struct	device sc_dv;
     70 	struct	scsipi_adapter sc_adapter;
     71 	struct	scsipi_channel sc_channel;
     72 	int	sc_busno;
     73 };
     74 
     75 void	icpsp_attach(struct device *, struct device *, void *);
     76 void	icpsp_intr(struct icp_ccb *);
     77 int	icpsp_match(struct device *, struct cfdata *, void *);
     78 void	icpsp_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
     79 			     void *);
     80 
     81 CFATTACH_DECL(icpsp, sizeof(struct icpsp_softc),
     82     icpsp_match, icpsp_attach, NULL, NULL);
     83 
     84 int
     85 icpsp_match(struct device *parent, struct cfdata *match, void *aux)
     86 {
     87 	struct icp_attach_args *icpa;
     88 
     89 	icpa = aux;
     90 
     91 	return (icpa->icpa_unit >= ICPA_UNIT_SCSI);
     92 }
     93 
     94 void
     95 icpsp_attach(struct device *parent, struct device *self, void *aux)
     96 {
     97 	struct icp_attach_args *icpa;
     98 	struct icpsp_softc *sc;
     99 	struct icp_softc *icp;
    100 
    101 	icpa = (struct icp_attach_args *)aux;
    102 	sc = (struct icpsp_softc *)self;
    103 	icp = (struct icp_softc *)parent;
    104 
    105 	sc->sc_busno = icpa->icpa_unit - ICPA_UNIT_SCSI;
    106 	printf(": physical SCSI channel %d\n", sc->sc_busno);
    107 
    108 	sc->sc_adapter.adapt_dev = &sc->sc_dv;
    109 	sc->sc_adapter.adapt_nchannels = 1;
    110 	sc->sc_adapter.adapt_openings = icp->icp_openings;
    111 	sc->sc_adapter.adapt_max_periph = icp->icp_openings;
    112 	sc->sc_adapter.adapt_minphys = minphys;
    113 	sc->sc_adapter.adapt_request = icpsp_scsipi_request;
    114 
    115 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    116 	sc->sc_channel.chan_bustype = &scsi_bustype;
    117 	sc->sc_channel.chan_channel = 0;
    118 	sc->sc_channel.chan_ntargets = ((icp->icp_class & ICP_FC) != 0 ?
    119 	    127 : 16); /* XXX bogus check */
    120 	sc->sc_channel.chan_nluns = 7;
    121 	sc->sc_channel.chan_id = icp->icp_bus_id[sc->sc_busno];
    122 	sc->sc_channel.chan_flags = SCSIPI_CHAN_NOSETTLE;
    123 
    124 	config_found(self, &sc->sc_channel, scsiprint);
    125 }
    126 
    127 void
    128 icpsp_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    129 		     void *arg)
    130 {
    131 	struct scsipi_xfer *xs;
    132 	struct scsipi_periph *periph;
    133 	struct icpsp_softc *sc;
    134 	struct icp_rawcmd *rc;
    135 	struct icp_softc *icp;
    136 	struct icp_ccb *ic;
    137 	int rv, flags, s, soff;
    138 
    139 	sc = (void *)chan->chan_adapter->adapt_dev;
    140 	icp = (struct icp_softc *)sc->sc_dv.dv_parent;
    141 
    142 	switch (req) {
    143 	case ADAPTER_REQ_RUN_XFER:
    144 		xs = arg;
    145 		periph = xs->xs_periph;
    146 		flags = xs->xs_control;
    147 
    148 		SC_DEBUG(periph, SCSIPI_DB2, ("icpsp_scsi_request run_xfer\n"));
    149 
    150 		if ((flags & XS_CTL_RESET) != 0) {
    151 			/* XXX Unimplemented. */
    152 			xs->error = XS_DRIVER_STUFFUP;
    153 			scsipi_done(xs);
    154 			return;
    155 		}
    156 
    157 #if defined(ICP_DEBUG) || defined(SCSIDEBUG)
    158 		if (xs->cmdlen > sizeof(rc->rc_cdb))
    159 			panic("%s: CDB too large", sc->sc_dv.dv_xname);
    160 #endif
    161 
    162 		/*
    163 		 * Allocate a CCB.
    164 		 */
    165 		if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL)) {
    166 			xs->error = XS_RESOURCE_SHORTAGE;
    167 			scsipi_done(xs);
    168 			return;
    169 		}
    170 		rc = &ic->ic_cmd.cmd_packet.rc;
    171 		ic->ic_sg = rc->rc_sg;
    172 		ic->ic_service = ICP_SCSIRAWSERVICE;
    173 		soff = ICP_SCRATCH_SENSE + ic->ic_ident *
    174 		    sizeof(struct scsipi_sense_data);
    175 
    176 		/*
    177 		 * Build the command.  We don't need to actively prevent
    178 		 * access to array components, since the controller kindly
    179 		 * takes care of that for us.
    180 		 */
    181 		ic->ic_cmd.cmd_opcode = htole16(ICP_WRITE);
    182 		memcpy(rc->rc_cdb, xs->cmd, xs->cmdlen);
    183 
    184 		rc->rc_padding0 = 0;
    185 		rc->rc_direction = htole32((flags & XS_CTL_DATA_IN) != 0 ?
    186 		    ICP_DATA_IN : ICP_DATA_OUT);
    187 		rc->rc_mdisc_time = 0;
    188 		rc->rc_mcon_time = 0;
    189 		rc->rc_clen = htole32(xs->cmdlen);
    190 		rc->rc_target = periph->periph_target;
    191 		rc->rc_lun = periph->periph_lun;
    192 		rc->rc_bus = sc->sc_busno;
    193 		rc->rc_priority = 0;
    194 		rc->rc_sense_len = htole32(sizeof(xs->sense.scsi_sense));
    195 		rc->rc_sense_addr =
    196 		    htole32(soff + icp->icp_scr_seg[0].ds_addr);
    197 		rc->rc_padding1 = 0;
    198 
    199 		if (xs->datalen != 0) {
    200 			rv = icp_ccb_map(icp, ic, xs->data, xs->datalen,
    201 			   (flags & XS_CTL_DATA_IN) != 0 ? IC_XFER_IN :
    202 			   IC_XFER_OUT);
    203 			if (rv != 0) {
    204 				icp_ccb_free(icp, ic);
    205 				xs->error = XS_DRIVER_STUFFUP;
    206 				scsipi_done(xs);
    207 				return;
    208 			}
    209 
    210 			rc->rc_nsgent = htole32(ic->ic_nsgent);
    211 			rc->rc_sdata = ~0;
    212 			rc->rc_sdlen = htole32(xs->datalen);
    213 		} else {
    214 			rc->rc_nsgent = 0;
    215 			rc->rc_sdata = 0;
    216 			rc->rc_sdlen = 0;
    217 		}
    218 
    219 		ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd +
    220 		    ic->ic_nsgent * sizeof(*ic->ic_sg);
    221 
    222 		bus_dmamap_sync(icp->icp_dmat, icp->icp_scr_dmamap, soff,
    223 		    sizeof(xs->sense.scsi_sense), BUS_DMASYNC_PREREAD);
    224 
    225 		/*
    226 		 * Fire it off to the controller.
    227 		 */
    228  		ic->ic_intr = icpsp_intr;
    229 		ic->ic_context = xs;
    230 		ic->ic_dv = &sc->sc_dv;
    231 
    232 		if ((flags & XS_CTL_POLL) != 0) {
    233 			s = splbio();
    234 			rv = icp_ccb_poll(icp, ic, xs->timeout);
    235 			if (rv != 0) {
    236 				if (xs->datalen != 0)
    237 					icp_ccb_unmap(icp, ic);
    238 				icp_ccb_free(icp, ic);
    239 				xs->error = XS_TIMEOUT;
    240 				scsipi_done(xs);
    241 
    242 				/*
    243 				 * XXX We're now in a bad way, because we
    244 				 * don't know how to abort the command.
    245 				 * That shouldn't matter too much, since
    246 				 * polled commands won't be used while the
    247 				 * system is running.
    248 				 */
    249 			}
    250 			splx(s);
    251 		} else
    252 			icp_ccb_enqueue(icp, ic);
    253 
    254 		break;
    255 
    256 	case ADAPTER_REQ_GROW_RESOURCES:
    257 	case ADAPTER_REQ_SET_XFER_MODE:
    258 		/*
    259 		 * Neither of these cases are supported, and neither of them
    260 		 * is particulatly relevant, since we have an abstract view
    261 		 * of the bus; the controller takes care of all the nitty
    262 		 * gritty.
    263 		 */
    264 		break;
    265 	}
    266 }
    267 
    268 void
    269 icpsp_intr(struct icp_ccb *ic)
    270 {
    271 	struct scsipi_xfer *xs;
    272 	struct icpsp_softc *sc;
    273  	struct icp_softc *icp;
    274  	int soff;
    275 
    276 	sc = (struct icpsp_softc *)ic->ic_dv;
    277 	xs = (struct scsipi_xfer *)ic->ic_context;
    278 	icp = (struct icp_softc *)ic->ic_dv->dv_parent;
    279 	soff = ICP_SCRATCH_SENSE + ic->ic_ident *
    280 	    sizeof(struct scsipi_sense_data);
    281 
    282 	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("icpsp_intr\n"));
    283 
    284 	bus_dmamap_sync(icp->icp_dmat, icp->icp_scr_dmamap, soff,
    285 	    sizeof(xs->sense.scsi_sense), BUS_DMASYNC_POSTREAD);
    286 
    287 	if (ic->ic_status == ICP_S_OK) {
    288 		xs->status = SCSI_OK;
    289 		xs->resid = 0;
    290 	} else if (ic->ic_status != ICP_S_RAW_SCSI || icp->icp_info >= 0x100) {
    291 		xs->error = XS_SELTIMEOUT;
    292 		xs->resid = xs->datalen;
    293 	} else {
    294 		xs->status = icp->icp_info;
    295 
    296 		switch (xs->status) {
    297 		case SCSI_OK:
    298 #ifdef DIAGNOSTIC
    299 			printf("%s: error return (%d), but SCSI_OK?\n",
    300 			    sc->sc_dv.dv_xname, icp->icp_info);
    301 #endif
    302 			xs->resid = 0;
    303 			break;
    304 		case SCSI_CHECK:
    305 			memcpy(&xs->sense.scsi_sense, icp->icp_scr + soff,
    306 			    sizeof(xs->sense.scsi_sense));
    307 			xs->error = XS_SENSE;
    308 			/* FALLTHROUGH */
    309 		default:
    310 			/*
    311 			 * XXX Don't know how to get residual count.
    312 			 */
    313 			xs->resid = xs->datalen;
    314 			break;
    315 		}
    316 	}
    317 
    318 	if (xs->datalen != 0)
    319 		icp_ccb_unmap(icp, ic);
    320 	icp_ccb_free(icp, ic);
    321 	scsipi_done(xs);
    322 }
    323