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