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