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