1 /* $NetBSD: icpsp.c,v 1.6 2002/10/02 16:33:32 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.6 2002/10/02 16:33:32 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 ic = icp_ccb_alloc(icp); 166 rc = &ic->ic_cmd.cmd_packet.rc; 167 ic->ic_sg = rc->rc_sg; 168 ic->ic_service = ICP_SCSIRAWSERVICE; 169 soff = ICP_SCRATCH_SENSE + ic->ic_ident * 170 sizeof(struct scsipi_sense_data); 171 172 /* 173 * Build the command. We don't need to actively prevent 174 * access to array components, since the controller kindly 175 * takes care of that for us. 176 */ 177 ic->ic_cmd.cmd_opcode = htole16(ICP_WRITE); 178 memcpy(rc->rc_cdb, xs->cmd, xs->cmdlen); 179 180 rc->rc_padding0 = 0; 181 rc->rc_direction = htole32((flags & XS_CTL_DATA_IN) != 0 ? 182 ICP_DATA_IN : ICP_DATA_OUT); 183 rc->rc_mdisc_time = 0; 184 rc->rc_mcon_time = 0; 185 rc->rc_clen = htole32(xs->cmdlen); 186 rc->rc_target = periph->periph_target; 187 rc->rc_lun = periph->periph_lun; 188 rc->rc_bus = sc->sc_busno; 189 rc->rc_priority = 0; 190 rc->rc_sense_len = htole32(sizeof(xs->sense.scsi_sense)); 191 rc->rc_sense_addr = 192 htole32(soff + icp->icp_scr_seg[0].ds_addr); 193 rc->rc_padding1 = 0; 194 195 if (xs->datalen != 0) { 196 rv = icp_ccb_map(icp, ic, xs->data, xs->datalen, 197 (flags & XS_CTL_DATA_IN) != 0 ? IC_XFER_IN : 198 IC_XFER_OUT); 199 if (rv != 0) { 200 icp_ccb_free(icp, ic); 201 xs->error = XS_DRIVER_STUFFUP; 202 scsipi_done(xs); 203 return; 204 } 205 206 rc->rc_nsgent = htole32(ic->ic_nsgent); 207 rc->rc_sdata = ~0; 208 rc->rc_sdlen = htole32(xs->datalen); 209 } else { 210 rc->rc_nsgent = 0; 211 rc->rc_sdata = 0; 212 rc->rc_sdlen = 0; 213 } 214 215 ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd + 216 ic->ic_nsgent * sizeof(*ic->ic_sg); 217 218 bus_dmamap_sync(icp->icp_dmat, icp->icp_scr_dmamap, soff, 219 sizeof(xs->sense.scsi_sense), BUS_DMASYNC_PREREAD); 220 221 /* 222 * Fire it off to the controller. 223 */ 224 ic->ic_intr = icpsp_intr; 225 ic->ic_context = xs; 226 ic->ic_dv = &sc->sc_dv; 227 228 if ((flags & XS_CTL_POLL) != 0) { 229 s = splbio(); 230 rv = icp_ccb_poll(icp, ic, xs->timeout); 231 if (rv != 0) { 232 if (xs->datalen != 0) 233 icp_ccb_unmap(icp, ic); 234 icp_ccb_free(icp, ic); 235 xs->error = XS_TIMEOUT; 236 scsipi_done(xs); 237 238 /* 239 * XXX We're now in a bad way, because we 240 * don't know how to abort the command. 241 * That shouldn't matter too much, since 242 * polled commands won't be used while the 243 * system is running. 244 */ 245 } 246 splx(s); 247 } else 248 icp_ccb_enqueue(icp, ic); 249 250 break; 251 252 case ADAPTER_REQ_GROW_RESOURCES: 253 case ADAPTER_REQ_SET_XFER_MODE: 254 /* 255 * Neither of these cases are supported, and neither of them 256 * is particulatly relevant, since we have an abstract view 257 * of the bus; the controller takes care of all the nitty 258 * gritty. 259 */ 260 break; 261 } 262 } 263 264 void 265 icpsp_intr(struct icp_ccb *ic) 266 { 267 struct scsipi_xfer *xs; 268 struct icpsp_softc *sc; 269 struct icp_softc *icp; 270 int soff; 271 272 sc = (struct icpsp_softc *)ic->ic_dv; 273 xs = (struct scsipi_xfer *)ic->ic_context; 274 icp = (struct icp_softc *)ic->ic_dv->dv_parent; 275 soff = ICP_SCRATCH_SENSE + ic->ic_ident * 276 sizeof(struct scsipi_sense_data); 277 278 SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("icpsp_intr\n")); 279 280 bus_dmamap_sync(icp->icp_dmat, icp->icp_scr_dmamap, soff, 281 sizeof(xs->sense.scsi_sense), BUS_DMASYNC_POSTREAD); 282 283 if (ic->ic_status == ICP_S_OK) { 284 xs->status = SCSI_OK; 285 xs->resid = 0; 286 } else if (ic->ic_status != ICP_S_RAW_SCSI || icp->icp_info >= 0x100) { 287 xs->error = XS_SELTIMEOUT; 288 xs->resid = xs->datalen; 289 } else { 290 xs->status = icp->icp_info; 291 292 switch (xs->status) { 293 case SCSI_OK: 294 #ifdef DIAGNOSTIC 295 printf("%s: error return (%d), but SCSI_OK?\n", 296 sc->sc_dv.dv_xname, icp->icp_info); 297 #endif 298 xs->resid = 0; 299 break; 300 case SCSI_CHECK: 301 memcpy(&xs->sense.scsi_sense, icp->icp_scr + soff, 302 sizeof(xs->sense.scsi_sense)); 303 xs->error = XS_SENSE; 304 /* FALLTHROUGH */ 305 default: 306 /* 307 * XXX Don't know how to get residual count. 308 */ 309 xs->resid = xs->datalen; 310 break; 311 } 312 } 313 314 if (xs->datalen != 0) 315 icp_ccb_unmap(icp, ic); 316 icp_ccb_free(icp, ic); 317 scsipi_done(xs); 318 } 319