dpt.c revision 1.45 1 1.45 christos /* $NetBSD: dpt.c,v 1.45 2005/12/11 12:21:26 christos Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.29 bouyer * Copyright (c) 1997, 1998, 1999, 2000, 2001 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.22 ad * by Andrew Doran, Charles M. Hannum and by Jason R. Thorpe of the Numerical
9 1.1 ad * Aerospace Simulation Facility, NASA Ames Research Center.
10 1.1 ad *
11 1.1 ad * Redistribution and use in source and binary forms, with or without
12 1.1 ad * modification, are permitted provided that the following conditions
13 1.1 ad * are met:
14 1.1 ad * 1. Redistributions of source code must retain the above copyright
15 1.1 ad * notice, this list of conditions and the following disclaimer.
16 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 ad * notice, this list of conditions and the following disclaimer in the
18 1.1 ad * documentation and/or other materials provided with the distribution.
19 1.1 ad * 3. All advertising materials mentioning features or use of this software
20 1.1 ad * must display the following acknowledgement:
21 1.1 ad * This product includes software developed by the NetBSD
22 1.1 ad * Foundation, Inc. and its contributors.
23 1.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 ad * contributors may be used to endorse or promote products derived
25 1.1 ad * from this software without specific prior written permission.
26 1.1 ad *
27 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
38 1.1 ad */
39 1.1 ad
40 1.1 ad /*
41 1.33 ad * Copyright (c) 1996-2000 Distributed Processing Technology Corporation
42 1.33 ad * Copyright (c) 2000 Adaptec Corporation
43 1.33 ad * All rights reserved.
44 1.33 ad *
45 1.33 ad * TERMS AND CONDITIONS OF USE
46 1.33 ad *
47 1.33 ad * Redistribution and use in source form, with or without modification, are
48 1.33 ad * permitted provided that redistributions of source code must retain the
49 1.33 ad * above copyright notice, this list of conditions and the following disclaimer.
50 1.33 ad *
51 1.33 ad * This software is provided `as is' by Adaptec and any express or implied
52 1.33 ad * warranties, including, but not limited to, the implied warranties of
53 1.33 ad * merchantability and fitness for a particular purpose, are disclaimed. In no
54 1.33 ad * event shall Adaptec be liable for any direct, indirect, incidental, special,
55 1.33 ad * exemplary or consequential damages (including, but not limited to,
56 1.33 ad * procurement of substitute goods or services; loss of use, data, or profits;
57 1.33 ad * or business interruptions) however caused and on any theory of liability,
58 1.33 ad * whether in contract, strict liability, or tort (including negligence or
59 1.33 ad * otherwise) arising in any way out of the use of this driver software, even
60 1.33 ad * if advised of the possibility of such damage.
61 1.33 ad */
62 1.33 ad
63 1.33 ad /*
64 1.1 ad * Portions of this code fall under the following copyright:
65 1.1 ad *
66 1.1 ad * Originally written by Julian Elischer (julian (at) tfs.com)
67 1.1 ad * for TRW Financial Systems for use under the MACH(2.5) operating system.
68 1.1 ad *
69 1.1 ad * TRW Financial Systems, in accordance with their agreement with Carnegie
70 1.1 ad * Mellon University, makes this software available to CMU to distribute
71 1.1 ad * or use in any manner that they see fit as long as this message is kept with
72 1.1 ad * the software. For this reason TFS also grants any other persons or
73 1.1 ad * organisations permission to use or modify this software.
74 1.1 ad *
75 1.1 ad * TFS supplies this software to be publicly redistributed
76 1.1 ad * on the understanding that TFS is not responsible for the correct
77 1.1 ad * functioning of this software in any circumstances.
78 1.1 ad */
79 1.31 lukem
80 1.31 lukem #include <sys/cdefs.h>
81 1.45 christos __KERNEL_RCSID(0, "$NetBSD: dpt.c,v 1.45 2005/12/11 12:21:26 christos Exp $");
82 1.1 ad
83 1.1 ad #include <sys/param.h>
84 1.1 ad #include <sys/systm.h>
85 1.1 ad #include <sys/device.h>
86 1.1 ad #include <sys/queue.h>
87 1.1 ad #include <sys/buf.h>
88 1.13 ad #include <sys/endian.h>
89 1.33 ad #include <sys/conf.h>
90 1.1 ad
91 1.24 thorpej #include <uvm/uvm_extern.h>
92 1.24 thorpej
93 1.1 ad #include <machine/bus.h>
94 1.33 ad #ifdef i386
95 1.33 ad #include <machine/pio.h>
96 1.33 ad #endif
97 1.1 ad
98 1.1 ad #include <dev/scsipi/scsi_all.h>
99 1.1 ad #include <dev/scsipi/scsipi_all.h>
100 1.1 ad #include <dev/scsipi/scsiconf.h>
101 1.1 ad
102 1.1 ad #include <dev/ic/dptreg.h>
103 1.1 ad #include <dev/ic/dptvar.h>
104 1.1 ad
105 1.33 ad #include <dev/i2o/dptivar.h>
106 1.33 ad
107 1.33 ad #ifdef DEBUG
108 1.33 ad #define DPRINTF(x) printf x
109 1.33 ad #else
110 1.33 ad #define DPRINTF(x)
111 1.33 ad #endif
112 1.33 ad
113 1.29 bouyer #define dpt_inb(x, o) \
114 1.29 bouyer bus_space_read_1((x)->sc_iot, (x)->sc_ioh, (o))
115 1.29 bouyer #define dpt_outb(x, o, d) \
116 1.29 bouyer bus_space_write_1((x)->sc_iot, (x)->sc_ioh, (o), (d))
117 1.29 bouyer
118 1.29 bouyer static const char * const dpt_cname[] = {
119 1.29 bouyer "3334", "SmartRAID IV",
120 1.29 bouyer "3332", "SmartRAID IV",
121 1.29 bouyer "2144", "SmartCache IV",
122 1.29 bouyer "2044", "SmartCache IV",
123 1.29 bouyer "2142", "SmartCache IV",
124 1.29 bouyer "2042", "SmartCache IV",
125 1.29 bouyer "2041", "SmartCache IV",
126 1.29 bouyer "3224", "SmartRAID III",
127 1.44 perry "3222", "SmartRAID III",
128 1.29 bouyer "3021", "SmartRAID III",
129 1.29 bouyer "2124", "SmartCache III",
130 1.29 bouyer "2024", "SmartCache III",
131 1.29 bouyer "2122", "SmartCache III",
132 1.29 bouyer "2022", "SmartCache III",
133 1.29 bouyer "2021", "SmartCache III",
134 1.44 perry "2012", "SmartCache Plus",
135 1.29 bouyer "2011", "SmartCache Plus",
136 1.29 bouyer NULL, "<unknown>",
137 1.1 ad };
138 1.1 ad
139 1.29 bouyer static void *dpt_sdh;
140 1.29 bouyer
141 1.33 ad dev_type_open(dptopen);
142 1.33 ad dev_type_ioctl(dptioctl);
143 1.33 ad
144 1.33 ad const struct cdevsw dpt_cdevsw = {
145 1.33 ad dptopen, nullclose, noread, nowrite, dptioctl,
146 1.33 ad nostop, notty, nopoll, nommap, nokqfilter,
147 1.33 ad };
148 1.33 ad
149 1.33 ad extern struct cfdriver dpt_cd;
150 1.33 ad
151 1.33 ad static struct dpt_sig dpt_sig = {
152 1.33 ad { 'd', 'P', 't', 'S', 'i', 'G'},
153 1.33 ad SIG_VERSION,
154 1.33 ad #if defined(i386)
155 1.33 ad PROC_INTEL,
156 1.33 ad #elif defined(powerpc)
157 1.33 ad PROC_POWERPC,
158 1.33 ad #elif defined(alpha)
159 1.33 ad PROC_ALPHA,
160 1.33 ad #elif defined(__mips__)
161 1.33 ad PROC_MIPS,
162 1.33 ad #elif defined(sparc64)
163 1.33 ad PROC_ULTRASPARC,
164 1.35 fvdl #else
165 1.35 fvdl 0xff,
166 1.33 ad #endif
167 1.33 ad #if defined(i386)
168 1.33 ad PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
169 1.33 ad #else
170 1.33 ad 0,
171 1.33 ad #endif
172 1.33 ad FT_HBADRVR,
173 1.33 ad 0,
174 1.33 ad OEM_DPT,
175 1.33 ad OS_FREE_BSD, /* XXX */
176 1.33 ad CAP_ABOVE16MB,
177 1.33 ad DEV_ALL,
178 1.33 ad ADF_ALL_EATA,
179 1.33 ad 0,
180 1.33 ad 0,
181 1.33 ad DPT_VERSION,
182 1.33 ad DPT_REVISION,
183 1.33 ad DPT_SUBREVISION,
184 1.33 ad DPT_MONTH,
185 1.33 ad DPT_DAY,
186 1.33 ad DPT_YEAR,
187 1.33 ad "" /* Will be filled later */
188 1.33 ad };
189 1.33 ad
190 1.29 bouyer static void dpt_ccb_abort(struct dpt_softc *, struct dpt_ccb *);
191 1.29 bouyer static void dpt_ccb_done(struct dpt_softc *, struct dpt_ccb *);
192 1.29 bouyer static int dpt_ccb_map(struct dpt_softc *, struct dpt_ccb *);
193 1.29 bouyer static int dpt_ccb_poll(struct dpt_softc *, struct dpt_ccb *);
194 1.29 bouyer static void dpt_ccb_unmap(struct dpt_softc *, struct dpt_ccb *);
195 1.29 bouyer static int dpt_cmd(struct dpt_softc *, struct dpt_ccb *, int, int);
196 1.33 ad static void dpt_ctlrinfo(struct dpt_softc *, struct dpt_eata_ctlrinfo *);
197 1.29 bouyer static void dpt_hba_inquire(struct dpt_softc *, struct eata_inquiry_data **);
198 1.29 bouyer static void dpt_minphys(struct buf *);
199 1.34 ad static int dpt_passthrough(struct dpt_softc *, struct eata_ucp *,
200 1.45 christos struct lwp *);
201 1.29 bouyer static void dpt_scsipi_request(struct scsipi_channel *,
202 1.29 bouyer scsipi_adapter_req_t, void *);
203 1.29 bouyer static void dpt_shutdown(void *);
204 1.33 ad static void dpt_sysinfo(struct dpt_softc *, struct dpt_sysinfo *);
205 1.29 bouyer static int dpt_wait(struct dpt_softc *, u_int8_t, u_int8_t, int);
206 1.29 bouyer
207 1.29 bouyer static __inline__ struct dpt_ccb *dpt_ccb_alloc(struct dpt_softc *);
208 1.29 bouyer static __inline__ void dpt_ccb_free(struct dpt_softc *, struct dpt_ccb *);
209 1.29 bouyer
210 1.29 bouyer static __inline__ struct dpt_ccb *
211 1.29 bouyer dpt_ccb_alloc(struct dpt_softc *sc)
212 1.29 bouyer {
213 1.29 bouyer struct dpt_ccb *ccb;
214 1.29 bouyer int s;
215 1.29 bouyer
216 1.29 bouyer s = splbio();
217 1.29 bouyer ccb = SLIST_FIRST(&sc->sc_ccb_free);
218 1.29 bouyer SLIST_REMOVE_HEAD(&sc->sc_ccb_free, ccb_chain);
219 1.29 bouyer splx(s);
220 1.29 bouyer
221 1.29 bouyer return (ccb);
222 1.29 bouyer }
223 1.29 bouyer
224 1.29 bouyer static __inline__ void
225 1.29 bouyer dpt_ccb_free(struct dpt_softc *sc, struct dpt_ccb *ccb)
226 1.29 bouyer {
227 1.29 bouyer int s;
228 1.1 ad
229 1.29 bouyer ccb->ccb_flg = 0;
230 1.33 ad ccb->ccb_savesp = NULL;
231 1.29 bouyer s = splbio();
232 1.29 bouyer SLIST_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
233 1.29 bouyer splx(s);
234 1.29 bouyer }
235 1.19 ad
236 1.1 ad /*
237 1.1 ad * Handle an interrupt from the HBA.
238 1.1 ad */
239 1.1 ad int
240 1.29 bouyer dpt_intr(void *cookie)
241 1.1 ad {
242 1.1 ad struct dpt_softc *sc;
243 1.1 ad struct dpt_ccb *ccb;
244 1.1 ad struct eata_sp *sp;
245 1.19 ad volatile int junk;
246 1.29 bouyer int forus;
247 1.1 ad
248 1.29 bouyer sc = cookie;
249 1.19 ad sp = sc->sc_stp;
250 1.29 bouyer forus = 0;
251 1.19 ad
252 1.7 ad for (;;) {
253 1.7 ad /*
254 1.7 ad * HBA might have interrupted while we were dealing with the
255 1.44 perry * last completed command, since we ACK before we deal; keep
256 1.19 ad * polling.
257 1.29 bouyer */
258 1.19 ad if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0)
259 1.7 ad break;
260 1.29 bouyer forus = 1;
261 1.29 bouyer
262 1.19 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
263 1.1 ad sizeof(struct eata_sp), BUS_DMASYNC_POSTREAD);
264 1.1 ad
265 1.29 bouyer /* Might have looped before HBA can reset HBA_AUX_INTR. */
266 1.1 ad if (sp->sp_ccbid == -1) {
267 1.1 ad DELAY(50);
268 1.19 ad
269 1.1 ad if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0)
270 1.1 ad return (0);
271 1.19 ad
272 1.19 ad printf("%s: no status\n", sc->sc_dv.dv_xname);
273 1.19 ad
274 1.1 ad /* Re-sync DMA map */
275 1.29 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
276 1.19 ad sc->sc_stpoff, sizeof(struct eata_sp),
277 1.1 ad BUS_DMASYNC_POSTREAD);
278 1.1 ad }
279 1.1 ad
280 1.29 bouyer /* Make sure CCB ID from status packet is realistic. */
281 1.29 bouyer if ((u_int)sp->sp_ccbid >= sc->sc_nccbs) {
282 1.44 perry printf("%s: bogus status (returned CCB id %d)\n",
283 1.1 ad sc->sc_dv.dv_xname, sp->sp_ccbid);
284 1.1 ad
285 1.1 ad /* Ack the interrupt */
286 1.20 ad sp->sp_ccbid = -1;
287 1.19 ad junk = dpt_inb(sc, HA_STATUS);
288 1.29 bouyer continue;
289 1.1 ad }
290 1.29 bouyer
291 1.29 bouyer /* Sync up DMA map and cache cmd status. */
292 1.29 bouyer ccb = sc->sc_ccbs + sp->sp_ccbid;
293 1.29 bouyer
294 1.29 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, CCB_OFF(sc, ccb),
295 1.29 bouyer sizeof(struct dpt_ccb), BUS_DMASYNC_POSTWRITE);
296 1.29 bouyer
297 1.29 bouyer ccb->ccb_hba_status = sp->sp_hba_status & 0x7f;
298 1.29 bouyer ccb->ccb_scsi_status = sp->sp_scsi_status;
299 1.33 ad if (ccb->ccb_savesp != NULL)
300 1.33 ad memcpy(ccb->ccb_savesp, sp, sizeof(*sp));
301 1.29 bouyer
302 1.44 perry /*
303 1.29 bouyer * Ack the interrupt and process the CCB. If this
304 1.29 bouyer * is a private CCB it's up to dpt_ccb_poll() to
305 1.29 bouyer * notice.
306 1.29 bouyer */
307 1.29 bouyer sp->sp_ccbid = -1;
308 1.29 bouyer ccb->ccb_flg |= CCB_INTR;
309 1.29 bouyer junk = dpt_inb(sc, HA_STATUS);
310 1.29 bouyer if ((ccb->ccb_flg & CCB_PRIVATE) == 0)
311 1.29 bouyer dpt_ccb_done(sc, ccb);
312 1.33 ad else if ((ccb->ccb_flg & CCB_WAIT) != 0)
313 1.33 ad wakeup(ccb);
314 1.1 ad }
315 1.1 ad
316 1.29 bouyer return (forus);
317 1.1 ad }
318 1.1 ad
319 1.1 ad /*
320 1.29 bouyer * Initialize and attach the HBA. This is the entry point from bus
321 1.1 ad * specific probe-and-attach code.
322 1.1 ad */
323 1.1 ad void
324 1.29 bouyer dpt_init(struct dpt_softc *sc, const char *intrstr)
325 1.1 ad {
326 1.29 bouyer struct scsipi_adapter *adapt;
327 1.29 bouyer struct scsipi_channel *chan;
328 1.1 ad struct eata_inquiry_data *ei;
329 1.29 bouyer int i, j, rv, rseg, maxchannel, maxtarget, mapsize;
330 1.1 ad bus_dma_segment_t seg;
331 1.4 ad struct eata_cfg *ec;
332 1.29 bouyer struct dpt_ccb *ccb;
333 1.1 ad char model[16];
334 1.29 bouyer
335 1.4 ad ec = &sc->sc_ec;
336 1.43 itojun snprintf(dpt_sig.dsDescription, sizeof(dpt_sig.dsDescription),
337 1.43 itojun "NetBSD %s DPT driver", osrelease);
338 1.33 ad
339 1.29 bouyer /*
340 1.29 bouyer * Allocate the CCB/status packet/scratch DMA map and load.
341 1.29 bouyer */
342 1.44 perry sc->sc_nccbs =
343 1.19 ad min(be16toh(*(int16_t *)ec->ec_queuedepth), DPT_MAX_CCBS);
344 1.19 ad sc->sc_stpoff = sc->sc_nccbs * sizeof(struct dpt_ccb);
345 1.19 ad sc->sc_scroff = sc->sc_stpoff + sizeof(struct eata_sp);
346 1.44 perry mapsize = sc->sc_nccbs * sizeof(struct dpt_ccb) +
347 1.29 bouyer DPT_SCRATCH_SIZE + sizeof(struct eata_sp);
348 1.29 bouyer
349 1.29 bouyer if ((rv = bus_dmamem_alloc(sc->sc_dmat, mapsize,
350 1.24 thorpej PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
351 1.39 thorpej aprint_error("%s: unable to allocate CCBs, rv = %d\n",
352 1.29 bouyer sc->sc_dv.dv_xname, rv);
353 1.1 ad return;
354 1.1 ad }
355 1.1 ad
356 1.29 bouyer if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, mapsize,
357 1.1 ad (caddr_t *)&sc->sc_ccbs, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
358 1.39 thorpej aprint_error("%s: unable to map CCBs, rv = %d\n",
359 1.29 bouyer sc->sc_dv.dv_xname, rv);
360 1.1 ad return;
361 1.1 ad }
362 1.1 ad
363 1.29 bouyer if ((rv = bus_dmamap_create(sc->sc_dmat, mapsize,
364 1.29 bouyer mapsize, 1, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
365 1.39 thorpej aprint_error("%s: unable to create CCB DMA map, rv = %d\n",
366 1.29 bouyer sc->sc_dv.dv_xname, rv);
367 1.1 ad return;
368 1.1 ad }
369 1.1 ad
370 1.29 bouyer if ((rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
371 1.29 bouyer sc->sc_ccbs, mapsize, NULL, BUS_DMA_NOWAIT)) != 0) {
372 1.39 thorpej aprint_error("%s: unable to load CCB DMA map, rv = %d\n",
373 1.29 bouyer sc->sc_dv.dv_xname, rv);
374 1.1 ad return;
375 1.1 ad }
376 1.1 ad
377 1.19 ad sc->sc_stp = (struct eata_sp *)((caddr_t)sc->sc_ccbs + sc->sc_stpoff);
378 1.19 ad sc->sc_stppa = sc->sc_dmamap->dm_segs[0].ds_addr + sc->sc_stpoff;
379 1.1 ad sc->sc_scr = (caddr_t)sc->sc_ccbs + sc->sc_scroff;
380 1.19 ad sc->sc_scrpa = sc->sc_dmamap->dm_segs[0].ds_addr + sc->sc_scroff;
381 1.19 ad sc->sc_stp->sp_ccbid = -1;
382 1.4 ad
383 1.29 bouyer /*
384 1.29 bouyer * Create the CCBs.
385 1.29 bouyer */
386 1.29 bouyer SLIST_INIT(&sc->sc_ccb_free);
387 1.29 bouyer memset(sc->sc_ccbs, 0, sizeof(struct dpt_ccb) * sc->sc_nccbs);
388 1.29 bouyer
389 1.29 bouyer for (i = 0, ccb = sc->sc_ccbs; i < sc->sc_nccbs; i++, ccb++) {
390 1.29 bouyer rv = bus_dmamap_create(sc->sc_dmat, DPT_MAX_XFER,
391 1.29 bouyer DPT_SG_SIZE, DPT_MAX_XFER, 0,
392 1.29 bouyer BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
393 1.29 bouyer &ccb->ccb_dmamap_xfer);
394 1.29 bouyer if (rv) {
395 1.44 perry aprint_error("%s: can't create ccb dmamap (%d)\n",
396 1.29 bouyer sc->sc_dv.dv_xname, rv);
397 1.29 bouyer break;
398 1.29 bouyer }
399 1.29 bouyer
400 1.29 bouyer ccb->ccb_id = i;
401 1.29 bouyer ccb->ccb_ccbpa = sc->sc_dmamap->dm_segs[0].ds_addr +
402 1.29 bouyer CCB_OFF(sc, ccb);
403 1.29 bouyer SLIST_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
404 1.29 bouyer }
405 1.1 ad
406 1.1 ad if (i == 0) {
407 1.39 thorpej aprint_error("%s: unable to create CCBs\n", sc->sc_dv.dv_xname);
408 1.1 ad return;
409 1.1 ad } else if (i != sc->sc_nccbs) {
410 1.39 thorpej aprint_error("%s: %d/%d CCBs created!\n", sc->sc_dv.dv_xname,
411 1.39 thorpej i, sc->sc_nccbs);
412 1.1 ad sc->sc_nccbs = i;
413 1.1 ad }
414 1.1 ad
415 1.29 bouyer /* Set shutdownhook before we start any device activity. */
416 1.23 ad if (dpt_sdh == NULL)
417 1.19 ad dpt_sdh = shutdownhook_establish(dpt_shutdown, NULL);
418 1.1 ad
419 1.29 bouyer /* Get the inquiry data from the HBA. */
420 1.1 ad dpt_hba_inquire(sc, &ei);
421 1.1 ad
422 1.44 perry /*
423 1.1 ad * dpt0 at pci0 dev 12 function 0: DPT SmartRAID III (PM3224A/9X-R)
424 1.1 ad * dpt0: interrupting at irq 10
425 1.1 ad * dpt0: 64 queued commands, 1 channel(s), adapter on ID(s) 7
426 1.1 ad */
427 1.1 ad for (i = 0; ei->ei_vendor[i] != ' ' && i < 8; i++)
428 1.1 ad ;
429 1.1 ad ei->ei_vendor[i] = '\0';
430 1.1 ad
431 1.1 ad for (i = 0; ei->ei_model[i] != ' ' && i < 7; i++)
432 1.1 ad model[i] = ei->ei_model[i];
433 1.25 cgd for (j = 0; ei->ei_suffix[j] != ' ' && j < 7; i++, j++)
434 1.25 cgd model[i] = ei->ei_model[i];
435 1.1 ad model[i] = '\0';
436 1.1 ad
437 1.29 bouyer /* Find the marketing name for the board. */
438 1.9 ad for (i = 0; dpt_cname[i] != NULL; i += 2)
439 1.29 bouyer if (memcmp(ei->ei_model + 2, dpt_cname[i], 4) == 0)
440 1.1 ad break;
441 1.29 bouyer
442 1.39 thorpej aprint_normal("%s %s (%s)\n", ei->ei_vendor, dpt_cname[i + 1], model);
443 1.1 ad
444 1.1 ad if (intrstr != NULL)
445 1.39 thorpej aprint_normal("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
446 1.29 bouyer intrstr);
447 1.1 ad
448 1.19 ad maxchannel = (ec->ec_feat3 & EC_F3_MAX_CHANNEL_MASK) >>
449 1.19 ad EC_F3_MAX_CHANNEL_SHIFT;
450 1.19 ad maxtarget = (ec->ec_feat3 & EC_F3_MAX_TARGET_MASK) >>
451 1.19 ad EC_F3_MAX_TARGET_SHIFT;
452 1.19 ad
453 1.39 thorpej aprint_normal("%s: %d queued commands, %d channel(s), adapter on ID(s)",
454 1.19 ad sc->sc_dv.dv_xname, sc->sc_nccbs, maxchannel + 1);
455 1.1 ad
456 1.19 ad for (i = 0; i <= maxchannel; i++) {
457 1.19 ad sc->sc_hbaid[i] = ec->ec_hba[3 - i];
458 1.39 thorpej aprint_normal(" %d", sc->sc_hbaid[i]);
459 1.19 ad }
460 1.39 thorpej aprint_normal("\n");
461 1.1 ad
462 1.29 bouyer /*
463 1.29 bouyer * Reset the SCSI controller chip(s) and bus. XXX Do we need to do
464 1.29 bouyer * this for each bus?
465 1.29 bouyer */
466 1.29 bouyer if (dpt_cmd(sc, NULL, CP_IMMEDIATE, CPI_BUS_RESET))
467 1.4 ad panic("%s: dpt_cmd failed", sc->sc_dv.dv_xname);
468 1.29 bouyer
469 1.29 bouyer /* Fill in the scsipi_adapter. */
470 1.29 bouyer adapt = &sc->sc_adapt;
471 1.29 bouyer memset(adapt, 0, sizeof(*adapt));
472 1.29 bouyer adapt->adapt_dev = &sc->sc_dv;
473 1.29 bouyer adapt->adapt_nchannels = maxchannel + 1;
474 1.33 ad adapt->adapt_openings = sc->sc_nccbs - 1;
475 1.33 ad adapt->adapt_max_periph = sc->sc_nccbs - 1;
476 1.29 bouyer adapt->adapt_request = dpt_scsipi_request;
477 1.29 bouyer adapt->adapt_minphys = dpt_minphys;
478 1.1 ad
479 1.19 ad for (i = 0; i <= maxchannel; i++) {
480 1.29 bouyer /* Fill in the scsipi_channel. */
481 1.29 bouyer chan = &sc->sc_chans[i];
482 1.29 bouyer memset(chan, 0, sizeof(*chan));
483 1.29 bouyer chan->chan_adapter = adapt;
484 1.29 bouyer chan->chan_bustype = &scsi_bustype;
485 1.29 bouyer chan->chan_channel = i;
486 1.29 bouyer chan->chan_ntargets = maxtarget + 1;
487 1.29 bouyer chan->chan_nluns = ec->ec_maxlun + 1;
488 1.29 bouyer chan->chan_id = sc->sc_hbaid[i];
489 1.29 bouyer config_found(&sc->sc_dv, chan, scsiprint);
490 1.23 ad }
491 1.1 ad }
492 1.1 ad
493 1.1 ad /*
494 1.1 ad * Read the EATA configuration from the HBA and perform some sanity checks.
495 1.1 ad */
496 1.1 ad int
497 1.29 bouyer dpt_readcfg(struct dpt_softc *sc)
498 1.1 ad {
499 1.4 ad struct eata_cfg *ec;
500 1.1 ad int i, j, stat;
501 1.1 ad u_int16_t *p;
502 1.1 ad
503 1.4 ad ec = &sc->sc_ec;
504 1.4 ad
505 1.29 bouyer /* Older firmware may puke if we talk to it too soon after reset. */
506 1.4 ad dpt_outb(sc, HA_COMMAND, CP_RESET);
507 1.19 ad DELAY(750000);
508 1.4 ad
509 1.4 ad for (i = 1000; i; i--) {
510 1.4 ad if ((dpt_inb(sc, HA_STATUS) & HA_ST_READY) != 0)
511 1.4 ad break;
512 1.4 ad DELAY(2000);
513 1.4 ad }
514 1.29 bouyer
515 1.4 ad if (i == 0) {
516 1.17 ad printf("%s: HBA not ready after reset (hba status:%02x)\n",
517 1.4 ad sc->sc_dv.dv_xname, dpt_inb(sc, HA_STATUS));
518 1.4 ad return (-1);
519 1.4 ad }
520 1.4 ad
521 1.1 ad while((((stat = dpt_inb(sc, HA_STATUS))
522 1.19 ad != (HA_ST_READY|HA_ST_SEEK_COMPLETE))
523 1.19 ad && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR))
524 1.19 ad && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR|HA_ST_DRQ)))
525 1.29 bouyer || (dpt_wait(sc, HA_ST_BUSY, 0, 2000))) {
526 1.19 ad /* RAID drives still spinning up? */
527 1.29 bouyer if(dpt_inb(sc, HA_ERROR) != 'D' ||
528 1.29 bouyer dpt_inb(sc, HA_ERROR + 1) != 'P' ||
529 1.29 bouyer dpt_inb(sc, HA_ERROR + 2) != 'T') {
530 1.29 bouyer printf("%s: HBA not ready\n", sc->sc_dv.dv_xname);
531 1.19 ad return (-1);
532 1.2 ad }
533 1.19 ad }
534 1.1 ad
535 1.44 perry /*
536 1.44 perry * Issue the read-config command and wait for the data to appear.
537 1.29 bouyer *
538 1.29 bouyer * Apparently certian firmware revisions won't DMA later on if we
539 1.29 bouyer * request the config data using PIO, but it makes it a lot easier
540 1.29 bouyer * as no DMA setup is required.
541 1.1 ad */
542 1.1 ad dpt_outb(sc, HA_COMMAND, CP_PIO_GETCFG);
543 1.4 ad memset(ec, 0, sizeof(*ec));
544 1.44 perry i = ((int)&((struct eata_cfg *)0)->ec_cfglen +
545 1.4 ad sizeof(ec->ec_cfglen)) >> 1;
546 1.4 ad p = (u_int16_t *)ec;
547 1.44 perry
548 1.4 ad if (dpt_wait(sc, 0xFF, HA_ST_DATA_RDY, 2000)) {
549 1.29 bouyer printf("%s: cfg data didn't appear (hba status:%02x)\n",
550 1.9 ad sc->sc_dv.dv_xname, dpt_inb(sc, HA_STATUS));
551 1.29 bouyer return (-1);
552 1.29 bouyer }
553 1.1 ad
554 1.29 bouyer /* Begin reading. */
555 1.29 bouyer while (i--)
556 1.27 briggs *p++ = bus_space_read_stream_2(sc->sc_iot, sc->sc_ioh, HA_DATA);
557 1.1 ad
558 1.19 ad if ((i = ec->ec_cfglen) > (sizeof(struct eata_cfg)
559 1.19 ad - (int)(&(((struct eata_cfg *)0L)->ec_cfglen))
560 1.19 ad - sizeof(ec->ec_cfglen)))
561 1.19 ad i = sizeof(struct eata_cfg)
562 1.19 ad - (int)(&(((struct eata_cfg *)0L)->ec_cfglen))
563 1.19 ad - sizeof(ec->ec_cfglen);
564 1.19 ad
565 1.44 perry j = i + (int)(&(((struct eata_cfg *)0L)->ec_cfglen)) +
566 1.19 ad sizeof(ec->ec_cfglen);
567 1.19 ad i >>= 1;
568 1.1 ad
569 1.1 ad while (i--)
570 1.27 briggs *p++ = bus_space_read_stream_2(sc->sc_iot, sc->sc_ioh, HA_DATA);
571 1.29 bouyer
572 1.19 ad /* Flush until we have read 512 bytes. */
573 1.19 ad i = (512 - j + 1) >> 1;
574 1.1 ad while (i--)
575 1.29 bouyer bus_space_read_stream_2(sc->sc_iot, sc->sc_ioh, HA_DATA);
576 1.44 perry
577 1.29 bouyer /* Defaults for older firmware... */
578 1.4 ad if (p <= (u_short *)&ec->ec_hba[DPT_MAX_CHANNELS - 1])
579 1.4 ad ec->ec_hba[DPT_MAX_CHANNELS - 1] = 7;
580 1.1 ad
581 1.19 ad if ((dpt_inb(sc, HA_STATUS) & HA_ST_ERROR) != 0) {
582 1.19 ad printf("%s: HBA error\n", sc->sc_dv.dv_xname);
583 1.19 ad return (-1);
584 1.19 ad }
585 1.29 bouyer
586 1.15 ad if (memcmp(ec->ec_eatasig, "EATA", 4) != 0) {
587 1.29 bouyer printf("%s: EATA signature mismatch\n", sc->sc_dv.dv_xname);
588 1.1 ad return (-1);
589 1.1 ad }
590 1.29 bouyer
591 1.19 ad if ((ec->ec_feat0 & EC_F0_HBA_VALID) == 0) {
592 1.19 ad printf("%s: ec_hba field invalid\n", sc->sc_dv.dv_xname);
593 1.1 ad return (-1);
594 1.1 ad }
595 1.29 bouyer
596 1.19 ad if ((ec->ec_feat0 & EC_F0_DMA_SUPPORTED) == 0) {
597 1.29 bouyer printf("%s: DMA not supported\n", sc->sc_dv.dv_xname);
598 1.1 ad return (-1);
599 1.1 ad }
600 1.1 ad
601 1.1 ad return (0);
602 1.1 ad }
603 1.1 ad
604 1.1 ad /*
605 1.29 bouyer * Our `shutdownhook' to cleanly shut down the HBA. The HBA must flush all
606 1.29 bouyer * data from it's cache and mark array groups as clean.
607 1.29 bouyer *
608 1.29 bouyer * XXX This doesn't always work (i.e., the HBA may still be flushing after
609 1.29 bouyer * we tell root that it's safe to power off).
610 1.1 ad */
611 1.29 bouyer static void
612 1.29 bouyer dpt_shutdown(void *cookie)
613 1.1 ad {
614 1.29 bouyer extern struct cfdriver dpt_cd;
615 1.29 bouyer struct dpt_softc *sc;
616 1.29 bouyer int i;
617 1.29 bouyer
618 1.29 bouyer printf("shutting down dpt devices...");
619 1.29 bouyer
620 1.29 bouyer for (i = 0; i < dpt_cd.cd_ndevs; i++) {
621 1.29 bouyer if ((sc = device_lookup(&dpt_cd, i)) == NULL)
622 1.29 bouyer continue;
623 1.29 bouyer dpt_cmd(sc, NULL, CP_IMMEDIATE, CPI_POWEROFF_WARN);
624 1.29 bouyer }
625 1.1 ad
626 1.29 bouyer delay(10000*1000);
627 1.29 bouyer printf(" done\n");
628 1.1 ad }
629 1.1 ad
630 1.1 ad /*
631 1.29 bouyer * Send an EATA command to the HBA.
632 1.1 ad */
633 1.29 bouyer static int
634 1.29 bouyer dpt_cmd(struct dpt_softc *sc, struct dpt_ccb *ccb, int eatacmd, int icmd)
635 1.1 ad {
636 1.29 bouyer u_int32_t pa;
637 1.29 bouyer int i, s;
638 1.1 ad
639 1.1 ad s = splbio();
640 1.1 ad
641 1.29 bouyer for (i = 20000; i != 0; i--) {
642 1.29 bouyer if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_BUSY) == 0)
643 1.29 bouyer break;
644 1.29 bouyer DELAY(50);
645 1.29 bouyer }
646 1.29 bouyer if (i == 0) {
647 1.29 bouyer splx(s);
648 1.29 bouyer return (-1);
649 1.29 bouyer }
650 1.29 bouyer
651 1.29 bouyer pa = (ccb != NULL ? ccb->ccb_ccbpa : 0);
652 1.29 bouyer dpt_outb(sc, HA_DMA_BASE + 0, (pa ) & 0xff);
653 1.29 bouyer dpt_outb(sc, HA_DMA_BASE + 1, (pa >> 8) & 0xff);
654 1.29 bouyer dpt_outb(sc, HA_DMA_BASE + 2, (pa >> 16) & 0xff);
655 1.29 bouyer dpt_outb(sc, HA_DMA_BASE + 3, (pa >> 24) & 0xff);
656 1.29 bouyer
657 1.29 bouyer if (eatacmd == CP_IMMEDIATE)
658 1.29 bouyer dpt_outb(sc, HA_ICMD, icmd);
659 1.1 ad
660 1.29 bouyer dpt_outb(sc, HA_COMMAND, eatacmd);
661 1.1 ad
662 1.29 bouyer splx(s);
663 1.1 ad return (0);
664 1.1 ad }
665 1.1 ad
666 1.1 ad /*
667 1.29 bouyer * Wait for the HBA status register to reach a specific state.
668 1.1 ad */
669 1.29 bouyer static int
670 1.29 bouyer dpt_wait(struct dpt_softc *sc, u_int8_t mask, u_int8_t state, int ms)
671 1.1 ad {
672 1.1 ad
673 1.29 bouyer for (ms *= 10; ms != 0; ms--) {
674 1.29 bouyer if ((dpt_inb(sc, HA_STATUS) & mask) == state)
675 1.29 bouyer return (0);
676 1.29 bouyer DELAY(100);
677 1.1 ad }
678 1.1 ad
679 1.29 bouyer return (-1);
680 1.1 ad }
681 1.1 ad
682 1.1 ad /*
683 1.29 bouyer * Spin waiting for a command to finish. The timeout value from the CCB is
684 1.29 bouyer * used. The CCB must be marked with CCB_PRIVATE, otherwise it'll will get
685 1.29 bouyer * recycled before we get a look at it.
686 1.1 ad */
687 1.29 bouyer static int
688 1.29 bouyer dpt_ccb_poll(struct dpt_softc *sc, struct dpt_ccb *ccb)
689 1.1 ad {
690 1.29 bouyer int i, s;
691 1.29 bouyer
692 1.29 bouyer #ifdef DEBUG
693 1.29 bouyer if ((ccb->ccb_flg & CCB_PRIVATE) == 0)
694 1.32 provos panic("dpt_ccb_poll: called for non-CCB_PRIVATE request");
695 1.29 bouyer #endif
696 1.1 ad
697 1.1 ad s = splbio();
698 1.1 ad
699 1.29 bouyer if ((ccb->ccb_flg & CCB_INTR) != 0) {
700 1.29 bouyer splx(s);
701 1.29 bouyer return (0);
702 1.29 bouyer }
703 1.29 bouyer
704 1.29 bouyer for (i = ccb->ccb_timeout * 20; i != 0; i--) {
705 1.29 bouyer if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) != 0)
706 1.29 bouyer dpt_intr(sc);
707 1.29 bouyer if ((ccb->ccb_flg & CCB_INTR) != 0)
708 1.1 ad break;
709 1.29 bouyer DELAY(50);
710 1.1 ad }
711 1.1 ad
712 1.1 ad splx(s);
713 1.29 bouyer return (i == 0);
714 1.1 ad }
715 1.1 ad
716 1.1 ad /*
717 1.29 bouyer * We have a command which has been processed by the HBA, so now we look to
718 1.29 bouyer * see how the operation went. CCBs marked CCB_PRIVATE are not passed here
719 1.29 bouyer * by dpt_intr().
720 1.1 ad */
721 1.29 bouyer static void
722 1.29 bouyer dpt_ccb_done(struct dpt_softc *sc, struct dpt_ccb *ccb)
723 1.1 ad {
724 1.1 ad struct scsipi_xfer *xs;
725 1.29 bouyer
726 1.1 ad xs = ccb->ccb_xs;
727 1.1 ad
728 1.29 bouyer SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("dpt_ccb_done\n"));
729 1.1 ad
730 1.1 ad /*
731 1.44 perry * If we were a data transfer, unload the map that described the
732 1.1 ad * data buffer.
733 1.1 ad */
734 1.29 bouyer if (xs->datalen != 0)
735 1.29 bouyer dpt_ccb_unmap(sc, ccb);
736 1.1 ad
737 1.1 ad if (xs->error == XS_NOERROR) {
738 1.19 ad if (ccb->ccb_hba_status != SP_HBA_NO_ERROR) {
739 1.1 ad switch (ccb->ccb_hba_status) {
740 1.19 ad case SP_HBA_ERROR_SEL_TO:
741 1.1 ad xs->error = XS_SELTIMEOUT;
742 1.1 ad break;
743 1.19 ad case SP_HBA_ERROR_RESET:
744 1.1 ad xs->error = XS_RESET;
745 1.1 ad break;
746 1.29 bouyer default:
747 1.1 ad printf("%s: HBA status %x\n",
748 1.1 ad sc->sc_dv.dv_xname, ccb->ccb_hba_status);
749 1.1 ad xs->error = XS_DRIVER_STUFFUP;
750 1.29 bouyer break;
751 1.1 ad }
752 1.1 ad } else if (ccb->ccb_scsi_status != SCSI_OK) {
753 1.1 ad switch (ccb->ccb_scsi_status) {
754 1.1 ad case SCSI_CHECK:
755 1.29 bouyer memcpy(&xs->sense.scsi_sense, &ccb->ccb_sense,
756 1.29 bouyer sizeof(xs->sense.scsi_sense));
757 1.1 ad xs->error = XS_SENSE;
758 1.1 ad break;
759 1.1 ad case SCSI_BUSY:
760 1.29 bouyer case SCSI_QUEUE_FULL:
761 1.1 ad xs->error = XS_BUSY;
762 1.1 ad break;
763 1.1 ad default:
764 1.29 bouyer scsipi_printaddr(xs->xs_periph);
765 1.29 bouyer printf("SCSI status %x\n",
766 1.29 bouyer ccb->ccb_scsi_status);
767 1.1 ad xs->error = XS_DRIVER_STUFFUP;
768 1.29 bouyer break;
769 1.1 ad }
770 1.1 ad } else
771 1.1 ad xs->resid = 0;
772 1.29 bouyer
773 1.7 ad xs->status = ccb->ccb_scsi_status;
774 1.1 ad }
775 1.1 ad
776 1.29 bouyer /* Free up the CCB and mark the command as done. */
777 1.29 bouyer dpt_ccb_free(sc, ccb);
778 1.1 ad scsipi_done(xs);
779 1.1 ad }
780 1.1 ad
781 1.1 ad /*
782 1.29 bouyer * Specified CCB has timed out, abort it.
783 1.1 ad */
784 1.29 bouyer static void
785 1.29 bouyer dpt_ccb_abort(struct dpt_softc *sc, struct dpt_ccb *ccb)
786 1.29 bouyer {
787 1.29 bouyer struct scsipi_periph *periph;
788 1.1 ad struct scsipi_xfer *xs;
789 1.29 bouyer int s;
790 1.1 ad
791 1.29 bouyer xs = ccb->ccb_xs;
792 1.29 bouyer periph = xs->xs_periph;
793 1.1 ad
794 1.29 bouyer scsipi_printaddr(periph);
795 1.44 perry printf("timed out (status:%02x aux status:%02x)",
796 1.29 bouyer dpt_inb(sc, HA_STATUS), dpt_inb(sc, HA_AUX_STATUS));
797 1.1 ad
798 1.1 ad s = splbio();
799 1.1 ad
800 1.29 bouyer if ((ccb->ccb_flg & CCB_ABORT) != 0) {
801 1.29 bouyer /* Abort timed out, reset the HBA */
802 1.29 bouyer printf(" AGAIN, resetting HBA\n");
803 1.29 bouyer dpt_outb(sc, HA_COMMAND, CP_RESET);
804 1.29 bouyer DELAY(750000);
805 1.1 ad } else {
806 1.29 bouyer /* Abort the operation that has timed out */
807 1.29 bouyer printf("\n");
808 1.29 bouyer xs->error = XS_TIMEOUT;
809 1.29 bouyer ccb->ccb_timeout = DPT_ABORT_TIMEOUT;
810 1.29 bouyer ccb->ccb_flg |= CCB_ABORT;
811 1.29 bouyer /* Start the abort */
812 1.29 bouyer if (dpt_cmd(sc, ccb, CP_IMMEDIATE, CPI_SPEC_ABORT))
813 1.29 bouyer printf("%s: dpt_cmd failed\n", sc->sc_dv.dv_xname);
814 1.29 bouyer }
815 1.29 bouyer
816 1.29 bouyer splx(s);
817 1.29 bouyer }
818 1.1 ad
819 1.29 bouyer /*
820 1.29 bouyer * Map a data transfer.
821 1.29 bouyer */
822 1.29 bouyer static int
823 1.29 bouyer dpt_ccb_map(struct dpt_softc *sc, struct dpt_ccb *ccb)
824 1.29 bouyer {
825 1.29 bouyer struct scsipi_xfer *xs;
826 1.29 bouyer bus_dmamap_t xfer;
827 1.29 bouyer bus_dma_segment_t *ds;
828 1.29 bouyer struct eata_sg *sg;
829 1.29 bouyer struct eata_cp *cp;
830 1.29 bouyer int rv, i;
831 1.1 ad
832 1.29 bouyer xs = ccb->ccb_xs;
833 1.29 bouyer xfer = ccb->ccb_dmamap_xfer;
834 1.29 bouyer cp = &ccb->ccb_eata_cp;
835 1.1 ad
836 1.29 bouyer rv = bus_dmamap_load(sc->sc_dmat, xfer, xs->data, xs->datalen, NULL,
837 1.44 perry ((xs->xs_control & XS_CTL_NOSLEEP) != 0 ?
838 1.30 thorpej BUS_DMA_NOWAIT : BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
839 1.30 thorpej ((xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMA_READ : BUS_DMA_WRITE));
840 1.29 bouyer
841 1.29 bouyer switch (rv) {
842 1.29 bouyer case 0:
843 1.29 bouyer break;
844 1.29 bouyer case ENOMEM:
845 1.29 bouyer case EAGAIN:
846 1.29 bouyer xs->error = XS_RESOURCE_SHORTAGE;
847 1.29 bouyer break;
848 1.29 bouyer default:
849 1.29 bouyer xs->error = XS_DRIVER_STUFFUP;
850 1.29 bouyer printf("%s: error %d loading map\n", sc->sc_dv.dv_xname, rv);
851 1.29 bouyer break;
852 1.29 bouyer }
853 1.1 ad
854 1.29 bouyer if (xs->error != XS_NOERROR) {
855 1.29 bouyer dpt_ccb_free(sc, ccb);
856 1.29 bouyer scsipi_done(xs);
857 1.29 bouyer return (-1);
858 1.1 ad }
859 1.1 ad
860 1.29 bouyer bus_dmamap_sync(sc->sc_dmat, xfer, 0, xfer->dm_mapsize,
861 1.29 bouyer (xs->xs_control & XS_CTL_DATA_IN) != 0 ? BUS_DMASYNC_PREREAD :
862 1.29 bouyer BUS_DMASYNC_PREWRITE);
863 1.29 bouyer
864 1.29 bouyer /* Don't bother using scatter/gather for just 1 seg */
865 1.29 bouyer if (xfer->dm_nsegs == 1) {
866 1.29 bouyer cp->cp_dataaddr = htobe32(xfer->dm_segs[0].ds_addr);
867 1.29 bouyer cp->cp_datalen = htobe32(xfer->dm_segs[0].ds_len);
868 1.29 bouyer } else {
869 1.29 bouyer /*
870 1.44 perry * Load the hardware scatter/gather map with
871 1.29 bouyer * the contents of the DMA map.
872 1.1 ad */
873 1.29 bouyer sg = ccb->ccb_sg;
874 1.29 bouyer ds = xfer->dm_segs;
875 1.29 bouyer for (i = 0; i < xfer->dm_nsegs; i++, sg++, ds++) {
876 1.29 bouyer sg->sg_addr = htobe32(ds->ds_addr);
877 1.29 bouyer sg->sg_len = htobe32(ds->ds_len);
878 1.29 bouyer }
879 1.44 perry cp->cp_dataaddr = htobe32(CCB_OFF(sc, ccb) +
880 1.29 bouyer sc->sc_dmamap->dm_segs[0].ds_addr +
881 1.29 bouyer offsetof(struct dpt_ccb, ccb_sg));
882 1.29 bouyer cp->cp_datalen = htobe32(i * sizeof(struct eata_sg));
883 1.29 bouyer cp->cp_ctl0 |= CP_C0_SCATTER;
884 1.1 ad }
885 1.1 ad
886 1.29 bouyer return (0);
887 1.29 bouyer }
888 1.29 bouyer
889 1.29 bouyer /*
890 1.29 bouyer * Unmap a transfer.
891 1.29 bouyer */
892 1.29 bouyer static void
893 1.29 bouyer dpt_ccb_unmap(struct dpt_softc *sc, struct dpt_ccb *ccb)
894 1.29 bouyer {
895 1.29 bouyer
896 1.29 bouyer bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
897 1.29 bouyer ccb->ccb_dmamap_xfer->dm_mapsize,
898 1.29 bouyer (ccb->ccb_eata_cp.cp_ctl0 & CP_C0_DATA_IN) != 0 ?
899 1.29 bouyer BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
900 1.29 bouyer bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
901 1.29 bouyer }
902 1.29 bouyer
903 1.29 bouyer /*
904 1.29 bouyer * Adjust the size of each I/O before it passes to the SCSI layer.
905 1.29 bouyer */
906 1.29 bouyer static void
907 1.29 bouyer dpt_minphys(struct buf *bp)
908 1.29 bouyer {
909 1.29 bouyer
910 1.29 bouyer if (bp->b_bcount > DPT_MAX_XFER)
911 1.29 bouyer bp->b_bcount = DPT_MAX_XFER;
912 1.29 bouyer minphys(bp);
913 1.29 bouyer }
914 1.29 bouyer
915 1.29 bouyer /*
916 1.29 bouyer * Start a SCSI command.
917 1.29 bouyer */
918 1.29 bouyer static void
919 1.29 bouyer dpt_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
920 1.29 bouyer void *arg)
921 1.29 bouyer {
922 1.29 bouyer struct dpt_softc *sc;
923 1.29 bouyer struct scsipi_xfer *xs;
924 1.29 bouyer int flags;
925 1.29 bouyer struct scsipi_periph *periph;
926 1.29 bouyer struct dpt_ccb *ccb;
927 1.29 bouyer struct eata_cp *cp;
928 1.29 bouyer
929 1.29 bouyer sc = (struct dpt_softc *)chan->chan_adapter->adapt_dev;
930 1.1 ad
931 1.29 bouyer switch (req) {
932 1.29 bouyer case ADAPTER_REQ_RUN_XFER:
933 1.29 bouyer xs = arg;
934 1.29 bouyer periph = xs->xs_periph;
935 1.29 bouyer flags = xs->xs_control;
936 1.1 ad
937 1.29 bouyer #ifdef DIAGNOSTIC
938 1.29 bouyer /* Cmds must be no more than 12 bytes for us. */
939 1.29 bouyer if (xs->cmdlen > 12) {
940 1.29 bouyer xs->error = XS_DRIVER_STUFFUP;
941 1.29 bouyer scsipi_done(xs);
942 1.29 bouyer break;
943 1.1 ad }
944 1.29 bouyer #endif
945 1.29 bouyer /*
946 1.29 bouyer * XXX We can't reset devices just yet. Apparently some
947 1.29 bouyer * older firmware revisions don't even support it.
948 1.29 bouyer */
949 1.29 bouyer if ((flags & XS_CTL_RESET) != 0) {
950 1.1 ad xs->error = XS_DRIVER_STUFFUP;
951 1.29 bouyer scsipi_done(xs);
952 1.29 bouyer break;
953 1.1 ad }
954 1.1 ad
955 1.29 bouyer /*
956 1.29 bouyer * Get a CCB and fill it.
957 1.29 bouyer */
958 1.29 bouyer ccb = dpt_ccb_alloc(sc);
959 1.29 bouyer ccb->ccb_xs = xs;
960 1.29 bouyer ccb->ccb_timeout = xs->timeout;
961 1.29 bouyer
962 1.29 bouyer cp = &ccb->ccb_eata_cp;
963 1.29 bouyer memcpy(&cp->cp_cdb_cmd, xs->cmd, xs->cmdlen);
964 1.29 bouyer cp->cp_ccbid = ccb->ccb_id;
965 1.29 bouyer cp->cp_senselen = sizeof(ccb->ccb_sense);
966 1.29 bouyer cp->cp_stataddr = htobe32(sc->sc_stppa);
967 1.29 bouyer cp->cp_ctl0 = CP_C0_AUTO_SENSE;
968 1.44 perry cp->cp_ctl1 = 0;
969 1.29 bouyer cp->cp_ctl2 = 0;
970 1.29 bouyer cp->cp_ctl3 = periph->periph_target << CP_C3_ID_SHIFT;
971 1.29 bouyer cp->cp_ctl3 |= chan->chan_channel << CP_C3_CHANNEL_SHIFT;
972 1.29 bouyer cp->cp_ctl4 = periph->periph_lun << CP_C4_LUN_SHIFT;
973 1.29 bouyer cp->cp_ctl4 |= CP_C4_DIS_PRI | CP_C4_IDENTIFY;
974 1.29 bouyer
975 1.29 bouyer if ((flags & XS_CTL_DATA_IN) != 0)
976 1.29 bouyer cp->cp_ctl0 |= CP_C0_DATA_IN;
977 1.29 bouyer if ((flags & XS_CTL_DATA_OUT) != 0)
978 1.29 bouyer cp->cp_ctl0 |= CP_C0_DATA_OUT;
979 1.29 bouyer if (sc->sc_hbaid[chan->chan_channel] == periph->periph_target)
980 1.29 bouyer cp->cp_ctl0 |= CP_C0_INTERPRET;
981 1.29 bouyer
982 1.29 bouyer /* Synchronous xfers musn't write-back through the cache. */
983 1.29 bouyer if (xs->bp != NULL)
984 1.29 bouyer if ((xs->bp->b_flags & (B_ASYNC | B_READ)) == 0)
985 1.29 bouyer cp->cp_ctl2 |= CP_C2_NO_CACHE;
986 1.29 bouyer
987 1.29 bouyer cp->cp_senseaddr =
988 1.29 bouyer htobe32(sc->sc_dmamap->dm_segs[0].ds_addr +
989 1.29 bouyer CCB_OFF(sc, ccb) + offsetof(struct dpt_ccb, ccb_sense));
990 1.1 ad
991 1.29 bouyer if (xs->datalen != 0) {
992 1.29 bouyer if (dpt_ccb_map(sc, ccb))
993 1.29 bouyer break;
994 1.9 ad } else {
995 1.29 bouyer cp->cp_dataaddr = 0;
996 1.29 bouyer cp->cp_datalen = 0;
997 1.1 ad }
998 1.1 ad
999 1.29 bouyer /* Sync up CCB and status packet. */
1000 1.29 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
1001 1.29 bouyer CCB_OFF(sc, ccb), sizeof(struct dpt_ccb),
1002 1.29 bouyer BUS_DMASYNC_PREWRITE);
1003 1.29 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
1004 1.29 bouyer sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
1005 1.1 ad
1006 1.44 perry /*
1007 1.29 bouyer * Start the command.
1008 1.29 bouyer */
1009 1.29 bouyer if ((xs->xs_control & XS_CTL_POLL) != 0)
1010 1.44 perry ccb->ccb_flg |= CCB_PRIVATE;
1011 1.1 ad
1012 1.29 bouyer if (dpt_cmd(sc, ccb, CP_DMA_CMD, 0)) {
1013 1.29 bouyer printf("%s: dpt_cmd failed\n", sc->sc_dv.dv_xname);
1014 1.29 bouyer xs->error = XS_DRIVER_STUFFUP;
1015 1.29 bouyer if (xs->datalen != 0)
1016 1.29 bouyer dpt_ccb_unmap(sc, ccb);
1017 1.29 bouyer dpt_ccb_free(sc, ccb);
1018 1.29 bouyer break;
1019 1.29 bouyer }
1020 1.1 ad
1021 1.29 bouyer if ((xs->xs_control & XS_CTL_POLL) == 0)
1022 1.29 bouyer break;
1023 1.1 ad
1024 1.29 bouyer if (dpt_ccb_poll(sc, ccb)) {
1025 1.29 bouyer dpt_ccb_abort(sc, ccb);
1026 1.29 bouyer /* Wait for abort to complete... */
1027 1.29 bouyer if (dpt_ccb_poll(sc, ccb))
1028 1.29 bouyer dpt_ccb_abort(sc, ccb);
1029 1.44 perry }
1030 1.1 ad
1031 1.29 bouyer dpt_ccb_done(sc, ccb);
1032 1.29 bouyer break;
1033 1.1 ad
1034 1.29 bouyer case ADAPTER_REQ_GROW_RESOURCES:
1035 1.29 bouyer /*
1036 1.29 bouyer * Not supported, since we allocate the maximum number of
1037 1.29 bouyer * CCBs up front.
1038 1.29 bouyer */
1039 1.29 bouyer break;
1040 1.1 ad
1041 1.29 bouyer case ADAPTER_REQ_SET_XFER_MODE:
1042 1.29 bouyer /*
1043 1.29 bouyer * This will be handled by the HBA itself, and we can't
1044 1.29 bouyer * modify that (ditto for tagged queueing).
1045 1.29 bouyer */
1046 1.29 bouyer break;
1047 1.1 ad }
1048 1.1 ad }
1049 1.1 ad
1050 1.1 ad /*
1051 1.1 ad * Get inquiry data from the adapter.
1052 1.1 ad */
1053 1.29 bouyer static void
1054 1.29 bouyer dpt_hba_inquire(struct dpt_softc *sc, struct eata_inquiry_data **ei)
1055 1.1 ad {
1056 1.1 ad struct dpt_ccb *ccb;
1057 1.1 ad struct eata_cp *cp;
1058 1.29 bouyer
1059 1.1 ad *ei = (struct eata_inquiry_data *)sc->sc_scr;
1060 1.1 ad
1061 1.1 ad /* Get a CCB and mark as private */
1062 1.29 bouyer ccb = dpt_ccb_alloc(sc);
1063 1.1 ad ccb->ccb_flg |= CCB_PRIVATE;
1064 1.1 ad ccb->ccb_timeout = 200;
1065 1.1 ad
1066 1.29 bouyer /* Put all the arguments into the CCB. */
1067 1.1 ad cp = &ccb->ccb_eata_cp;
1068 1.1 ad cp->cp_ccbid = ccb->ccb_id;
1069 1.1 ad cp->cp_senselen = sizeof(ccb->ccb_sense);
1070 1.29 bouyer cp->cp_senseaddr = 0;
1071 1.19 ad cp->cp_stataddr = htobe32(sc->sc_stppa);
1072 1.13 ad cp->cp_dataaddr = htobe32(sc->sc_scrpa);
1073 1.13 ad cp->cp_datalen = htobe32(sizeof(struct eata_inquiry_data));
1074 1.19 ad cp->cp_ctl0 = CP_C0_DATA_IN | CP_C0_INTERPRET;
1075 1.19 ad cp->cp_ctl1 = 0;
1076 1.19 ad cp->cp_ctl2 = 0;
1077 1.19 ad cp->cp_ctl3 = sc->sc_hbaid[0] << CP_C3_ID_SHIFT;
1078 1.19 ad cp->cp_ctl4 = CP_C4_DIS_PRI | CP_C4_IDENTIFY;
1079 1.19 ad
1080 1.29 bouyer /* Put together the SCSI inquiry command. */
1081 1.29 bouyer memset(&cp->cp_cdb_cmd, 0, 12);
1082 1.19 ad cp->cp_cdb_cmd = INQUIRY;
1083 1.19 ad cp->cp_cdb_len = sizeof(struct eata_inquiry_data);
1084 1.1 ad
1085 1.29 bouyer /* Sync up CCB, status packet and scratch area. */
1086 1.44 perry bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, CCB_OFF(sc, ccb),
1087 1.1 ad sizeof(struct dpt_ccb), BUS_DMASYNC_PREWRITE);
1088 1.44 perry bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
1089 1.1 ad sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
1090 1.44 perry bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_scroff,
1091 1.1 ad sizeof(struct eata_inquiry_data), BUS_DMASYNC_PREREAD);
1092 1.1 ad
1093 1.29 bouyer /* Start the command and poll on completion. */
1094 1.29 bouyer if (dpt_cmd(sc, ccb, CP_DMA_CMD, 0))
1095 1.4 ad panic("%s: dpt_cmd failed", sc->sc_dv.dv_xname);
1096 1.1 ad
1097 1.29 bouyer if (dpt_ccb_poll(sc, ccb))
1098 1.1 ad panic("%s: inquiry timed out", sc->sc_dv.dv_xname);
1099 1.1 ad
1100 1.19 ad if (ccb->ccb_hba_status != SP_HBA_NO_ERROR ||
1101 1.1 ad ccb->ccb_scsi_status != SCSI_OK)
1102 1.29 bouyer panic("%s: inquiry failed (hba:%02x scsi:%02x)",
1103 1.29 bouyer sc->sc_dv.dv_xname, ccb->ccb_hba_status,
1104 1.29 bouyer ccb->ccb_scsi_status);
1105 1.29 bouyer
1106 1.29 bouyer /* Sync up the DMA map and free CCB, returning. */
1107 1.44 perry bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_scroff,
1108 1.1 ad sizeof(struct eata_inquiry_data), BUS_DMASYNC_POSTREAD);
1109 1.29 bouyer dpt_ccb_free(sc, ccb);
1110 1.33 ad }
1111 1.33 ad
1112 1.33 ad int
1113 1.45 christos dptopen(dev_t dev, int flag, int mode, struct lwp *l)
1114 1.33 ad {
1115 1.33 ad
1116 1.33 ad if (securelevel > 1)
1117 1.33 ad return (EPERM);
1118 1.33 ad if (device_lookup(&dpt_cd, minor(dev)) == NULL)
1119 1.33 ad return (ENXIO);
1120 1.33 ad
1121 1.33 ad return (0);
1122 1.33 ad }
1123 1.33 ad
1124 1.33 ad int
1125 1.45 christos dptioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
1126 1.33 ad {
1127 1.33 ad struct dpt_softc *sc;
1128 1.33 ad int rv;
1129 1.33 ad
1130 1.33 ad sc = device_lookup(&dpt_cd, minor(dev));
1131 1.33 ad
1132 1.33 ad switch (cmd & 0xffff) {
1133 1.33 ad case DPT_SIGNATURE:
1134 1.33 ad memcpy(data, &dpt_sig, min(IOCPARM_LEN(cmd), sizeof(dpt_sig)));
1135 1.33 ad break;
1136 1.33 ad
1137 1.33 ad case DPT_CTRLINFO:
1138 1.33 ad dpt_ctlrinfo(sc, (struct dpt_eata_ctlrinfo *)data);
1139 1.33 ad break;
1140 1.33 ad
1141 1.33 ad case DPT_SYSINFO:
1142 1.33 ad dpt_sysinfo(sc, (struct dpt_sysinfo *)data);
1143 1.33 ad break;
1144 1.33 ad
1145 1.33 ad case DPT_BLINKLED:
1146 1.33 ad /*
1147 1.33 ad * XXX Don't know how to get this from EATA boards. I think
1148 1.33 ad * it involves waiting for a "DPT" sequence from HA_ERROR
1149 1.33 ad * and then reading one of the HA_ICMD registers.
1150 1.33 ad */
1151 1.33 ad *(int *)data = 0;
1152 1.33 ad break;
1153 1.33 ad
1154 1.33 ad case DPT_EATAUSRCMD:
1155 1.37 ad if (IOCPARM_LEN(cmd) < sizeof(struct eata_ucp)) {
1156 1.38 kleink DPRINTF(("%s: ucp %lu vs %lu bytes\n",
1157 1.37 ad sc->sc_dv.dv_xname, IOCPARM_LEN(cmd),
1158 1.38 kleink (unsigned long int)sizeof(struct eata_ucp)));
1159 1.37 ad return (EINVAL);
1160 1.37 ad }
1161 1.37 ad
1162 1.33 ad if (sc->sc_uactive++)
1163 1.33 ad tsleep(&sc->sc_uactive, PRIBIO, "dptslp", 0);
1164 1.33 ad
1165 1.45 christos rv = dpt_passthrough(sc, (struct eata_ucp *)data, l);
1166 1.33 ad
1167 1.33 ad sc->sc_uactive--;
1168 1.33 ad wakeup_one(&sc->sc_uactive);
1169 1.33 ad return (rv);
1170 1.33 ad
1171 1.33 ad default:
1172 1.33 ad DPRINTF(("%s: unknown ioctl %lx\n", sc->sc_dv.dv_xname, cmd));
1173 1.33 ad return (ENOTTY);
1174 1.33 ad }
1175 1.33 ad
1176 1.33 ad return (0);
1177 1.33 ad }
1178 1.33 ad
1179 1.33 ad void
1180 1.33 ad dpt_ctlrinfo(struct dpt_softc *sc, struct dpt_eata_ctlrinfo *info)
1181 1.33 ad {
1182 1.33 ad
1183 1.33 ad memset(info, 0, sizeof(*info));
1184 1.33 ad info->id = sc->sc_hbaid[0];
1185 1.33 ad info->vect = sc->sc_isairq;
1186 1.33 ad info->base = sc->sc_isaport;
1187 1.33 ad info->qdepth = sc->sc_nccbs;
1188 1.33 ad info->sgsize = DPT_SG_SIZE * sizeof(struct eata_sg);
1189 1.33 ad info->heads = 16;
1190 1.33 ad info->sectors = 63;
1191 1.33 ad info->do_drive32 = 1;
1192 1.33 ad info->primary = 1;
1193 1.33 ad info->cpLength = sizeof(struct eata_cp);
1194 1.33 ad info->spLength = sizeof(struct eata_sp);
1195 1.33 ad info->drqNum = sc->sc_isadrq;
1196 1.33 ad }
1197 1.33 ad
1198 1.33 ad void
1199 1.33 ad dpt_sysinfo(struct dpt_softc *sc, struct dpt_sysinfo *info)
1200 1.33 ad {
1201 1.33 ad #ifdef i386
1202 1.33 ad int i, j;
1203 1.33 ad #endif
1204 1.33 ad
1205 1.33 ad memset(info, 0, sizeof(*info));
1206 1.33 ad
1207 1.33 ad #ifdef i386
1208 1.33 ad outb (0x70, 0x12);
1209 1.33 ad i = inb(0x71);
1210 1.33 ad j = i >> 4;
1211 1.33 ad if (i == 0x0f) {
1212 1.33 ad outb (0x70, 0x19);
1213 1.33 ad j = inb (0x71);
1214 1.33 ad }
1215 1.33 ad info->drive0CMOS = j;
1216 1.33 ad
1217 1.33 ad j = i & 0x0f;
1218 1.33 ad if (i == 0x0f) {
1219 1.33 ad outb (0x70, 0x1a);
1220 1.33 ad j = inb (0x71);
1221 1.33 ad }
1222 1.33 ad info->drive1CMOS = j;
1223 1.33 ad info->processorFamily = dpt_sig.dsProcessorFamily;
1224 1.33 ad
1225 1.33 ad /*
1226 1.33 ad * Get the conventional memory size from CMOS.
1227 1.33 ad */
1228 1.33 ad outb(0x70, 0x16);
1229 1.33 ad j = inb(0x71);
1230 1.33 ad j <<= 8;
1231 1.33 ad outb(0x70, 0x15);
1232 1.33 ad j |= inb(0x71);
1233 1.33 ad info->conventionalMemSize = j;
1234 1.33 ad
1235 1.33 ad /*
1236 1.33 ad * Get the extended memory size from CMOS.
1237 1.33 ad */
1238 1.33 ad outb(0x70, 0x31);
1239 1.33 ad j = inb(0x71);
1240 1.33 ad j <<= 8;
1241 1.33 ad outb(0x70, 0x30);
1242 1.33 ad j |= inb(0x71);
1243 1.33 ad info->extendedMemSize = j;
1244 1.33 ad
1245 1.33 ad switch (cpu_class) {
1246 1.33 ad case CPUCLASS_386:
1247 1.33 ad info->processorType = PROC_386;
1248 1.33 ad break;
1249 1.33 ad case CPUCLASS_486:
1250 1.33 ad info->processorType = PROC_486;
1251 1.33 ad break;
1252 1.33 ad case CPUCLASS_586:
1253 1.33 ad info->processorType = PROC_PENTIUM;
1254 1.33 ad break;
1255 1.33 ad case CPUCLASS_686:
1256 1.33 ad default:
1257 1.33 ad info->processorType = PROC_SEXIUM;
1258 1.33 ad break;
1259 1.33 ad }
1260 1.33 ad
1261 1.33 ad info->flags = SI_CMOS_Valid | SI_BusTypeValid |
1262 1.33 ad SI_MemorySizeValid | SI_NO_SmartROM;
1263 1.33 ad #else
1264 1.33 ad info->flags = SI_BusTypeValid | SI_NO_SmartROM;
1265 1.33 ad #endif
1266 1.33 ad
1267 1.33 ad info->busType = sc->sc_bustype;
1268 1.33 ad }
1269 1.33 ad
1270 1.33 ad int
1271 1.45 christos dpt_passthrough(struct dpt_softc *sc, struct eata_ucp *ucp, struct lwp *l)
1272 1.33 ad {
1273 1.33 ad struct dpt_ccb *ccb;
1274 1.33 ad struct eata_sp sp;
1275 1.33 ad struct eata_cp *cp;
1276 1.33 ad struct eata_sg *sg;
1277 1.42 christos bus_dmamap_t xfer = 0; /* XXX: gcc */
1278 1.33 ad bus_dma_segment_t *ds;
1279 1.42 christos int datain = 0, s, rv = 0, i, uslen; /* XXX: gcc */
1280 1.33 ad
1281 1.33 ad /*
1282 1.33 ad * Get a CCB and fill.
1283 1.33 ad */
1284 1.33 ad ccb = dpt_ccb_alloc(sc);
1285 1.33 ad ccb->ccb_flg |= CCB_PRIVATE | CCB_WAIT;
1286 1.33 ad ccb->ccb_timeout = 0;
1287 1.33 ad ccb->ccb_savesp = &sp;
1288 1.33 ad
1289 1.33 ad cp = &ccb->ccb_eata_cp;
1290 1.34 ad memcpy(cp, ucp->ucp_cp, sizeof(ucp->ucp_cp));
1291 1.34 ad uslen = cp->cp_senselen;
1292 1.33 ad cp->cp_ccbid = ccb->ccb_id;
1293 1.33 ad cp->cp_senselen = sizeof(ccb->ccb_sense);
1294 1.33 ad cp->cp_senseaddr = htobe32(sc->sc_dmamap->dm_segs[0].ds_addr +
1295 1.33 ad CCB_OFF(sc, ccb) + offsetof(struct dpt_ccb, ccb_sense));
1296 1.33 ad cp->cp_stataddr = htobe32(sc->sc_stppa);
1297 1.33 ad
1298 1.33 ad /*
1299 1.33 ad * Map data transfers.
1300 1.33 ad */
1301 1.34 ad if (ucp->ucp_dataaddr && ucp->ucp_datalen) {
1302 1.33 ad xfer = ccb->ccb_dmamap_xfer;
1303 1.34 ad datain = ((cp->cp_ctl0 & CP_C0_DATA_IN) != 0);
1304 1.33 ad
1305 1.34 ad if (ucp->ucp_datalen > DPT_MAX_XFER) {
1306 1.33 ad DPRINTF(("%s: xfer too big\n", sc->sc_dv.dv_xname));
1307 1.33 ad dpt_ccb_free(sc, ccb);
1308 1.33 ad return (EFBIG);
1309 1.33 ad }
1310 1.33 ad rv = bus_dmamap_load(sc->sc_dmat, xfer,
1311 1.45 christos ucp->ucp_dataaddr, ucp->ucp_datalen, l->l_proc,
1312 1.33 ad BUS_DMA_WAITOK | BUS_DMA_STREAMING |
1313 1.33 ad (datain ? BUS_DMA_READ : BUS_DMA_WRITE));
1314 1.33 ad if (rv != 0) {
1315 1.33 ad DPRINTF(("%s: map failed; %d\n", sc->sc_dv.dv_xname,
1316 1.33 ad rv));
1317 1.33 ad dpt_ccb_free(sc, ccb);
1318 1.33 ad return (rv);
1319 1.33 ad }
1320 1.33 ad
1321 1.33 ad bus_dmamap_sync(sc->sc_dmat, xfer, 0, xfer->dm_mapsize,
1322 1.33 ad (datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
1323 1.33 ad
1324 1.33 ad sg = ccb->ccb_sg;
1325 1.33 ad ds = xfer->dm_segs;
1326 1.33 ad for (i = 0; i < xfer->dm_nsegs; i++, sg++, ds++) {
1327 1.33 ad sg->sg_addr = htobe32(ds->ds_addr);
1328 1.34 ad sg->sg_len = htobe32(ds->ds_len);
1329 1.33 ad }
1330 1.44 perry cp->cp_dataaddr = htobe32(CCB_OFF(sc, ccb) +
1331 1.33 ad sc->sc_dmamap->dm_segs[0].ds_addr +
1332 1.33 ad offsetof(struct dpt_ccb, ccb_sg));
1333 1.33 ad cp->cp_datalen = htobe32(i * sizeof(struct eata_sg));
1334 1.33 ad cp->cp_ctl0 |= CP_C0_SCATTER;
1335 1.33 ad } else {
1336 1.33 ad cp->cp_dataaddr = 0;
1337 1.33 ad cp->cp_datalen = 0;
1338 1.33 ad }
1339 1.33 ad
1340 1.33 ad /*
1341 1.33 ad * Start the command and sleep on completion.
1342 1.33 ad */
1343 1.36 thorpej PHOLD(curlwp); /* XXXJRT curlwp */
1344 1.44 perry bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, CCB_OFF(sc, ccb),
1345 1.33 ad sizeof(struct dpt_ccb), BUS_DMASYNC_PREWRITE);
1346 1.33 ad s = splbio();
1347 1.44 perry bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_stpoff,
1348 1.33 ad sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
1349 1.33 ad if (dpt_cmd(sc, ccb, CP_DMA_CMD, 0))
1350 1.33 ad panic("%s: dpt_cmd failed", sc->sc_dv.dv_xname);
1351 1.33 ad tsleep(ccb, PWAIT, "dptucmd", 0);
1352 1.33 ad splx(s);
1353 1.36 thorpej PRELE(curlwp); /* XXXJRT curlwp */
1354 1.33 ad
1355 1.33 ad /*
1356 1.33 ad * Sync up the DMA map and copy out results.
1357 1.33 ad */
1358 1.44 perry bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, CCB_OFF(sc, ccb),
1359 1.33 ad sizeof(struct dpt_ccb), BUS_DMASYNC_POSTWRITE);
1360 1.33 ad
1361 1.33 ad if (cp->cp_datalen != 0) {
1362 1.33 ad bus_dmamap_sync(sc->sc_dmat, xfer, 0, xfer->dm_mapsize,
1363 1.33 ad (datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
1364 1.33 ad bus_dmamap_unload(sc->sc_dmat, xfer);
1365 1.33 ad }
1366 1.33 ad
1367 1.34 ad if (ucp->ucp_stataddr != NULL) {
1368 1.34 ad rv = copyout(&sp, ucp->ucp_stataddr, sizeof(sp));
1369 1.33 ad if (rv != 0)
1370 1.33 ad DPRINTF(("%s: sp copyout() failed\n",
1371 1.33 ad sc->sc_dv.dv_xname));
1372 1.33 ad }
1373 1.34 ad if (rv == 0 && ucp->ucp_senseaddr != NULL) {
1374 1.34 ad i = min(uslen, sizeof(ccb->ccb_sense));
1375 1.34 ad rv = copyout(&ccb->ccb_sense, ucp->ucp_senseaddr, i);
1376 1.33 ad if (rv != 0)
1377 1.33 ad DPRINTF(("%s: sense copyout() failed\n",
1378 1.33 ad sc->sc_dv.dv_xname));
1379 1.33 ad }
1380 1.33 ad
1381 1.37 ad ucp->ucp_hstatus = (u_int8_t)ccb->ccb_hba_status;
1382 1.37 ad ucp->ucp_tstatus = (u_int8_t)ccb->ccb_scsi_status;
1383 1.33 ad dpt_ccb_free(sc, ccb);
1384 1.33 ad return (rv);
1385 1.1 ad }
1386