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