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