1 /* $NetBSD: icpsp.c,v 1.23 2009/05/12 14:25:17 cegger 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 2009/05/12 14:25:17 cegger 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 <uvm/uvm_extern.h> 50 51 #include <dev/scsipi/scsi_all.h> 52 #include <dev/scsipi/scsi_disk.h> 53 #include <dev/scsipi/scsipi_all.h> 54 #include <dev/scsipi/scsiconf.h> 55 #include <dev/scsipi/scsi_message.h> 56 57 #include <dev/ic/icpreg.h> 58 #include <dev/ic/icpvar.h> 59 60 struct icpsp_softc { 61 struct device sc_dv; 62 struct scsipi_adapter sc_adapter; 63 struct scsipi_channel sc_channel; 64 int sc_busno; 65 int sc_openings; 66 }; 67 68 void icpsp_attach(device_t, device_t, void *); 69 void icpsp_intr(struct icp_ccb *); 70 int icpsp_match(device_t, cfdata_t, void *); 71 void icpsp_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t, 72 void *); 73 74 void icpsp_adjqparam(device_t, int); 75 76 CFATTACH_DECL(icpsp, sizeof(struct icpsp_softc), 77 icpsp_match, icpsp_attach, NULL, NULL); 78 79 static const struct icp_servicecb icpsp_servicecb = { 80 icpsp_adjqparam, 81 }; 82 83 int 84 icpsp_match(device_t parent, cfdata_t match, 85 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(device_t parent, device_t 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 sc->sc_openings = icp->icp_openings; 107 printf(": physical SCSI channel %d\n", sc->sc_busno); 108 109 icp_register_servicecb(icp, icpa->icpa_unit, &icpsp_servicecb); 110 111 sc->sc_adapter.adapt_dev = &sc->sc_dv; 112 sc->sc_adapter.adapt_nchannels = 1; 113 sc->sc_adapter.adapt_openings = icp->icp_openings; 114 sc->sc_adapter.adapt_max_periph = icp->icp_openings; 115 sc->sc_adapter.adapt_minphys = minphys; 116 sc->sc_adapter.adapt_request = icpsp_scsipi_request; 117 118 sc->sc_channel.chan_adapter = &sc->sc_adapter; 119 sc->sc_channel.chan_bustype = &scsi_bustype; 120 sc->sc_channel.chan_channel = 0; 121 sc->sc_channel.chan_ntargets = ((icp->icp_class & ICP_FC) != 0 ? 122 127 : 16); /* XXX bogus check */ 123 sc->sc_channel.chan_nluns = 8; 124 sc->sc_channel.chan_id = icp->icp_bus_id[sc->sc_busno]; 125 sc->sc_channel.chan_flags = SCSIPI_CHAN_NOSETTLE; 126 127 config_found(self, &sc->sc_channel, scsiprint); 128 } 129 130 void 131 icpsp_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, 132 void *arg) 133 { 134 struct scsipi_xfer *xs; 135 struct scsipi_periph *periph; 136 struct icpsp_softc *sc; 137 struct icp_rawcmd *rc; 138 struct icp_softc *icp; 139 struct icp_ccb *ic; 140 int rv, flags, s, soff; 141 142 sc = (void *)chan->chan_adapter->adapt_dev; 143 icp = (struct icp_softc *)device_parent(&sc->sc_dv); 144 145 switch (req) { 146 case ADAPTER_REQ_RUN_XFER: 147 xs = arg; 148 periph = xs->xs_periph; 149 flags = xs->xs_control; 150 151 SC_DEBUG(periph, SCSIPI_DB2, ("icpsp_scsi_request run_xfer\n")); 152 153 if ((flags & XS_CTL_RESET) != 0) { 154 /* XXX Unimplemented. */ 155 xs->error = XS_DRIVER_STUFFUP; 156 scsipi_done(xs); 157 return; 158 } 159 160 #if defined(ICP_DEBUG) || defined(SCSIDEBUG) 161 if (xs->cmdlen > sizeof(rc->rc_cdb)) 162 panic("%s: CDB too large", device_xname(&sc->sc_dv)); 163 #endif 164 165 /* 166 * Allocate a CCB. 167 */ 168 if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL)) { 169 xs->error = XS_RESOURCE_SHORTAGE; 170 scsipi_done(xs); 171 return; 172 } 173 rc = &ic->ic_cmd.cmd_packet.rc; 174 ic->ic_sg = rc->rc_sg; 175 ic->ic_service = ICP_SCSIRAWSERVICE; 176 soff = ICP_SCRATCH_SENSE + ic->ic_ident * 177 sizeof(struct scsi_sense_data); 178 179 /* 180 * Build the command. We don't need to actively prevent 181 * access to array components, since the controller kindly 182 * takes care of that for us. 183 */ 184 ic->ic_cmd.cmd_opcode = htole16(ICP_WRITE); 185 memcpy(rc->rc_cdb, xs->cmd, xs->cmdlen); 186 187 rc->rc_padding0 = 0; 188 rc->rc_direction = htole32((flags & XS_CTL_DATA_IN) != 0 ? 189 ICP_DATA_IN : ICP_DATA_OUT); 190 rc->rc_mdisc_time = 0; 191 rc->rc_mcon_time = 0; 192 rc->rc_clen = htole32(xs->cmdlen); 193 rc->rc_target = periph->periph_target; 194 rc->rc_lun = periph->periph_lun; 195 rc->rc_bus = sc->sc_busno; 196 rc->rc_priority = 0; 197 rc->rc_sense_len = htole32(sizeof(xs->sense.scsi_sense)); 198 rc->rc_sense_addr = 199 htole32(soff + icp->icp_scr_seg[0].ds_addr); 200 rc->rc_padding1 = 0; 201 202 if (xs->datalen != 0) { 203 rv = icp_ccb_map(icp, ic, xs->data, xs->datalen, 204 (flags & XS_CTL_DATA_IN) != 0 ? IC_XFER_IN : 205 IC_XFER_OUT); 206 if (rv != 0) { 207 icp_ccb_free(icp, ic); 208 xs->error = XS_DRIVER_STUFFUP; 209 scsipi_done(xs); 210 return; 211 } 212 213 rc->rc_nsgent = htole32(ic->ic_nsgent); 214 rc->rc_sdata = ~0; 215 rc->rc_sdlen = htole32(xs->datalen); 216 } else { 217 rc->rc_nsgent = 0; 218 rc->rc_sdata = 0; 219 rc->rc_sdlen = 0; 220 } 221 222 ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd + 223 ic->ic_nsgent * sizeof(*ic->ic_sg); 224 225 bus_dmamap_sync(icp->icp_dmat, icp->icp_scr_dmamap, soff, 226 sizeof(xs->sense.scsi_sense), BUS_DMASYNC_PREREAD); 227 228 /* 229 * Fire it off to the controller. 230 */ 231 ic->ic_intr = icpsp_intr; 232 ic->ic_context = xs; 233 ic->ic_dv = &sc->sc_dv; 234 235 if ((flags & XS_CTL_POLL) != 0) { 236 s = splbio(); 237 rv = icp_ccb_poll(icp, ic, xs->timeout); 238 if (rv != 0) { 239 if (xs->datalen != 0) 240 icp_ccb_unmap(icp, ic); 241 icp_ccb_free(icp, ic); 242 xs->error = XS_TIMEOUT; 243 scsipi_done(xs); 244 245 /* 246 * XXX We're now in a bad way, because we 247 * don't know how to abort the command. 248 * That shouldn't matter too much, since 249 * polled commands won't be used while the 250 * system is running. 251 */ 252 } 253 splx(s); 254 } else 255 icp_ccb_enqueue(icp, ic); 256 257 break; 258 259 case ADAPTER_REQ_GROW_RESOURCES: 260 case ADAPTER_REQ_SET_XFER_MODE: 261 /* 262 * Neither of these cases are supported, and neither of them 263 * is particulatly relevant, since we have an abstract view 264 * of the bus; the controller takes care of all the nitty 265 * gritty. 266 */ 267 break; 268 } 269 } 270 271 void 272 icpsp_intr(struct icp_ccb *ic) 273 { 274 struct scsipi_xfer *xs; 275 struct icpsp_softc *sc; 276 struct icp_softc *icp; 277 int soff; 278 279 sc = (struct icpsp_softc *)ic->ic_dv; 280 xs = (struct scsipi_xfer *)ic->ic_context; 281 icp = (struct icp_softc *)device_parent(ic->ic_dv); 282 soff = ICP_SCRATCH_SENSE + ic->ic_ident * 283 sizeof(struct scsi_sense_data); 284 285 SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("icpsp_intr\n")); 286 287 bus_dmamap_sync(icp->icp_dmat, icp->icp_scr_dmamap, soff, 288 sizeof(xs->sense.scsi_sense), BUS_DMASYNC_POSTREAD); 289 290 if (ic->ic_status == ICP_S_OK) { 291 xs->status = SCSI_OK; 292 xs->resid = 0; 293 } else if (ic->ic_status != ICP_S_RAW_SCSI || icp->icp_info >= 0x100) { 294 xs->error = XS_SELTIMEOUT; 295 xs->resid = xs->datalen; 296 } else { 297 xs->status = icp->icp_info; 298 299 switch (xs->status) { 300 case SCSI_OK: 301 #ifdef DIAGNOSTIC 302 printf("%s: error return (%d), but SCSI_OK?\n", 303 device_xname(&sc->sc_dv), icp->icp_info); 304 #endif 305 xs->resid = 0; 306 break; 307 case SCSI_CHECK: 308 memcpy(&xs->sense.scsi_sense, 309 (char *)icp->icp_scr + soff, 310 sizeof(xs->sense.scsi_sense)); 311 xs->error = XS_SENSE; 312 /* FALLTHROUGH */ 313 default: 314 /* 315 * XXX Don't know how to get residual count. 316 */ 317 xs->resid = xs->datalen; 318 break; 319 } 320 } 321 322 if (xs->datalen != 0) 323 icp_ccb_unmap(icp, ic); 324 icp_ccb_free(icp, ic); 325 scsipi_done(xs); 326 } 327 328 void 329 icpsp_adjqparam(device_t dv, int openings) 330 { 331 struct icpsp_softc *sc = (struct icpsp_softc *) dv; 332 int s; 333 334 s = splbio(); 335 sc->sc_adapter.adapt_openings += openings - sc->sc_openings; 336 sc->sc_openings = openings; 337 splx(s); 338 } 339