dpt.c revision 1.1 1 1.1 ad /* $NetBSD: dpt.c,v 1.1 1999/09/27 23:41:47 ad Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 1997, 1998, 1999 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 Andy 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.1 ad * Portions of this code fall under the following copyright:
42 1.1 ad *
43 1.1 ad * Originally written by Julian Elischer (julian (at) tfs.com)
44 1.1 ad * for TRW Financial Systems for use under the MACH(2.5) operating system.
45 1.1 ad *
46 1.1 ad * TRW Financial Systems, in accordance with their agreement with Carnegie
47 1.1 ad * Mellon University, makes this software available to CMU to distribute
48 1.1 ad * or use in any manner that they see fit as long as this message is kept with
49 1.1 ad * the software. For this reason TFS also grants any other persons or
50 1.1 ad * organisations permission to use or modify this software.
51 1.1 ad *
52 1.1 ad * TFS supplies this software to be publicly redistributed
53 1.1 ad * on the understanding that TFS is not responsible for the correct
54 1.1 ad * functioning of this software in any circumstances.
55 1.1 ad *
56 1.1 ad * commenced: Sun Sep 27 18:14:01 PDT 1992
57 1.1 ad * slight mod to make work with 34F as well: Wed Jun 2 18:05:48 WST 1993
58 1.1 ad */
59 1.1 ad
60 1.1 ad /*
61 1.1 ad * Driver for DPT EATA SCSI adapters.
62 1.1 ad *
63 1.1 ad * TODO:
64 1.1 ad *
65 1.1 ad * o Occasionally, dpt_readcfg() will fail while waiting for the HBA - fix.
66 1.1 ad * o Need a front-end for EISA boards.
67 1.1 ad * o Need a front-end for (newer) ISA boards.
68 1.1 ad * o Handle older firmware better.
69 1.1 ad * o Find a bunch of different firmware EEPROMs and try them out.
70 1.1 ad * o Test with a bunch of different boards.
71 1.1 ad * o dpt_readcfg() should not be using CP_PIO_GETCFG.
72 1.1 ad * o An interface to userland applications.
73 1.1 ad * o A port of DPT Storage Manager included in the base system would be nice.
74 1.1 ad * o Some sysctls or a utility (eg dptctl(8)) to control parameters.
75 1.1 ad * o Commit the manpage.
76 1.1 ad * o Code needs KNF in places and sanity in others.
77 1.1 ad */
78 1.1 ad
79 1.1 ad #include <sys/cdefs.h>
80 1.1 ad __KERNEL_RCSID(0, "$NetBSD: dpt.c,v 1.1 1999/09/27 23:41:47 ad Exp $");
81 1.1 ad
82 1.1 ad #include <sys/param.h>
83 1.1 ad #include <sys/systm.h>
84 1.1 ad #include <sys/kernel.h>
85 1.1 ad #include <sys/device.h>
86 1.1 ad #include <sys/queue.h>
87 1.1 ad #include <sys/proc.h>
88 1.1 ad #include <sys/buf.h>
89 1.1 ad
90 1.1 ad #include <machine/endian.h>
91 1.1 ad #include <machine/bus.h>
92 1.1 ad
93 1.1 ad #include <dev/scsipi/scsi_all.h>
94 1.1 ad #include <dev/scsipi/scsipi_all.h>
95 1.1 ad #include <dev/scsipi/scsiconf.h>
96 1.1 ad
97 1.1 ad #include <dev/ic/dptreg.h>
98 1.1 ad #include <dev/ic/dptvar.h>
99 1.1 ad
100 1.1 ad /* A default for our link struct */
101 1.1 ad static struct scsipi_device dpt_dev = {
102 1.1 ad NULL, /* Use default error handler */
103 1.1 ad NULL, /* have a queue, served by this */
104 1.1 ad NULL, /* have no async handler */
105 1.1 ad NULL, /* Use default 'done' routine */
106 1.1 ad };
107 1.1 ad
108 1.1 ad static char *dpt_cname[] = {
109 1.1 ad #ifdef notdef
110 1.1 ad "PM3755", "SmartRAID V",
111 1.1 ad "PM3754", "SmartRAID V",
112 1.1 ad "PM2654", "SmartRAID V",
113 1.1 ad "PM2554", "SmartRAID V",
114 1.1 ad "PM1554", "SmartRAID V",
115 1.1 ad #endif
116 1.1 ad "PM3334", "SmartRAID IV",
117 1.1 ad "PM3332", "SmartRAID IV",
118 1.1 ad "PM2144", "SmartCache IV",
119 1.1 ad "PM2044", "SmartCache IV",
120 1.1 ad "PM2142", "SmartCache IV",
121 1.1 ad "PM2042", "SmartCache IV",
122 1.1 ad "PM2041", "SmartCache IV",
123 1.1 ad "PM3224", "SmartRAID III",
124 1.1 ad "PM3222", "SmartRAID III",
125 1.1 ad "PM3021", "SmartRAID III",
126 1.1 ad "PM2124", "SmartCache III",
127 1.1 ad "PM2024", "SmartCache III",
128 1.1 ad "PM2122", "SmartCache III",
129 1.1 ad "PM2022", "SmartCache III",
130 1.1 ad "PM2021", "SmartCache III",
131 1.1 ad "SK2012", "SmartCache Plus",
132 1.1 ad "SK2011", "SmartCache Plus",
133 1.1 ad NULL, "unknown adapter, please report using send-pr(1)",
134 1.1 ad };
135 1.1 ad
136 1.1 ad void dpt_shutdown __P((void *));
137 1.1 ad void dpt_timeout __P((void *));
138 1.1 ad void dpt_minphys __P((struct buf *));
139 1.1 ad int dpt_readcfg __P((struct dpt_softc *, struct eata_cfg *));
140 1.1 ad int dpt_scsi_cmd __P((struct scsipi_xfer *));
141 1.1 ad int dpt_wait __P((struct dpt_softc *, u_int8_t, u_int8_t, int));
142 1.1 ad int dpt_poll __P((struct dpt_softc *, struct dpt_ccb *));
143 1.1 ad void dpt_cmd __P((struct dpt_softc *, struct eata_cp *, u_int32_t, int, int));
144 1.1 ad void dpt_hba_inquire __P((struct dpt_softc *, struct eata_inquiry_data **));
145 1.1 ad
146 1.1 ad void dpt_reset_ccb __P((struct dpt_softc *, struct dpt_ccb *));
147 1.1 ad void dpt_free_ccb __P((struct dpt_softc *, struct dpt_ccb *));
148 1.1 ad void dpt_done_ccb __P((struct dpt_softc *, struct dpt_ccb *));
149 1.1 ad int dpt_init_ccb __P((struct dpt_softc *, struct dpt_ccb *));
150 1.1 ad int dpt_create_ccbs __P((struct dpt_softc *, struct dpt_ccb *, int));
151 1.1 ad
152 1.1 ad struct dpt_ccb *dpt_alloc_ccb __P((struct dpt_softc *, int));
153 1.1 ad
154 1.1 ad #if 0 && defined(DEBUG)
155 1.1 ad static void dpt_dump_sp __P((struct eata_sp *));
156 1.1 ad #endif
157 1.1 ad
158 1.1 ad /*
159 1.1 ad * Handle an interrupt from the HBA.
160 1.1 ad */
161 1.1 ad int
162 1.1 ad dpt_intr(xxx_sc)
163 1.1 ad void *xxx_sc;
164 1.1 ad {
165 1.1 ad struct dpt_softc *sc;
166 1.1 ad struct dpt_ccb *ccb;
167 1.1 ad struct eata_sp *sp;
168 1.1 ad volatile int junk;
169 1.1 ad
170 1.1 ad sc = xxx_sc;
171 1.1 ad sp = sc->sc_sp;
172 1.1 ad
173 1.1 ad #ifdef DEBUG
174 1.1 ad if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0)
175 1.1 ad printf("%s: spurious intr\n", sc->sc_dv.dv_xname);
176 1.1 ad #endif
177 1.1 ad
178 1.1 ad /*
179 1.1 ad * HBA might have interrupted while we were dealing with the last
180 1.1 ad * completed command, since we ACK before we deal; keep polling.
181 1.1 ad */
182 1.1 ad while ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) != 0) {
183 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, sc->sc_spoff,
184 1.1 ad sizeof(struct eata_sp), BUS_DMASYNC_POSTREAD);
185 1.1 ad
186 1.1 ad /* Might have looped before HBA can reset HBA_AUX_INTR */
187 1.1 ad if (sp->sp_ccbid == -1) {
188 1.1 ad DELAY(50);
189 1.1 ad #ifdef DIAGNOSTIC
190 1.1 ad printf("%s: slow reset of HA_AUX_STATUS?",
191 1.1 ad sc->sc_dv.dv_xname);
192 1.1 ad #endif
193 1.1 ad if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) == 0)
194 1.1 ad return (0);
195 1.1 ad #ifdef DIAGNOSTIC
196 1.1 ad printf("%s: was a slow reset of HA_AUX_STATUS",
197 1.1 ad sc->sc_dv.dv_xname);
198 1.1 ad #endif
199 1.1 ad /* Re-sync DMA map */
200 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb,
201 1.1 ad sc->sc_spoff, sizeof(struct eata_sp),
202 1.1 ad BUS_DMASYNC_POSTREAD);
203 1.1 ad }
204 1.1 ad
205 1.1 ad /* Make sure CCB ID from status packet is realistic */
206 1.1 ad if (sp->sp_ccbid >= 0 && sp->sp_ccbid < sc->sc_nccbs) {
207 1.1 ad /* Sync up DMA map and cache cmd status */
208 1.1 ad ccb = sc->sc_ccbs + sp->sp_ccbid;
209 1.1 ad
210 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb,
211 1.1 ad DPT_CCB_OFF(sc, ccb), sizeof(struct dpt_ccb),
212 1.1 ad BUS_DMASYNC_POSTWRITE);
213 1.1 ad
214 1.1 ad ccb->ccb_hba_status = sp->sp_hba_status;
215 1.1 ad ccb->ccb_scsi_status = sp->sp_scsi_status;
216 1.1 ad
217 1.1 ad /*
218 1.1 ad * Ack the interrupt and process the CCB. If this
219 1.1 ad * is a private CCB it's up to dpt_poll() to notice.
220 1.1 ad */
221 1.1 ad sp->sp_ccbid = -1;
222 1.1 ad ccb->ccb_flg |= CCB_INTR;
223 1.1 ad junk = dpt_inb(sc, HA_STATUS);
224 1.1 ad if ((ccb->ccb_flg & CCB_PRIVATE) == 0)
225 1.1 ad dpt_done_ccb(sc, ccb);
226 1.1 ad } else {
227 1.1 ad printf("%s: bogus status (returned CCB id %d)\n",
228 1.1 ad sc->sc_dv.dv_xname, sp->sp_ccbid);
229 1.1 ad
230 1.1 ad /* Ack the interrupt */
231 1.1 ad sp->sp_ccbid = -1;
232 1.1 ad junk = dpt_inb(sc, HA_STATUS);
233 1.1 ad }
234 1.1 ad }
235 1.1 ad
236 1.1 ad return (0);
237 1.1 ad }
238 1.1 ad
239 1.1 ad /*
240 1.1 ad * Initialize and attach the HBA. This is the entry point from bus
241 1.1 ad * specific probe-and-attach code.
242 1.1 ad */
243 1.1 ad void
244 1.1 ad dpt_init(sc, intrstr)
245 1.1 ad struct dpt_softc *sc;
246 1.1 ad const char *intrstr;
247 1.1 ad {
248 1.1 ad struct eata_inquiry_data *ei;
249 1.1 ad int i, j, error, rseg, mapsize;
250 1.1 ad bus_dma_segment_t seg;
251 1.1 ad struct eata_cfg dc;
252 1.1 ad char model[16];
253 1.1 ad
254 1.1 ad /* Older firmware may puke if we talk to it too soon after reset */
255 1.1 ad dpt_outb(sc, HA_COMMAND, CP_RESET);
256 1.1 ad DELAY(750000);
257 1.1 ad
258 1.1 ad for (i = 1000; i; i--) {
259 1.1 ad if ((dpt_inb(sc, HA_STATUS) & HA_ST_READY) != 0)
260 1.1 ad break;
261 1.1 ad DELAY(2000);
262 1.1 ad }
263 1.1 ad
264 1.1 ad if (i == 0) {
265 1.1 ad printf("%s: HBA not ready after reset: %02x\n",
266 1.1 ad sc->sc_dv.dv_xname, dpt_inb(sc, HA_STATUS));
267 1.1 ad return;
268 1.1 ad }
269 1.1 ad
270 1.1 ad if (dpt_readcfg(sc, &dc)) {
271 1.1 ad printf("%s: readcfg failed - see dpt(4)\n",
272 1.1 ad sc->sc_dv.dv_xname);
273 1.1 ad return;
274 1.1 ad }
275 1.1 ad
276 1.1 ad /* Allocate the CCB/status packet/scratch DMA map and load */
277 1.1 ad sc->sc_nccbs = min(SWAP16(*(int16_t *)dc.dc_queuedepth), DPT_MAX_CCBS);
278 1.1 ad sc->sc_spoff = sc->sc_nccbs * sizeof(struct dpt_ccb);
279 1.1 ad sc->sc_scroff = sc->sc_spoff + sizeof(struct eata_sp);
280 1.1 ad sc->sc_scrlen = 256; /* XXX */
281 1.1 ad mapsize = sc->sc_nccbs * sizeof(struct dpt_ccb) + sc->sc_scrlen +
282 1.1 ad sizeof(struct eata_sp);
283 1.1 ad
284 1.1 ad if ((error = bus_dmamem_alloc(sc->sc_dmat, mapsize, NBPG, 0,
285 1.1 ad &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
286 1.1 ad printf("%s: unable to allocate CCBs, error = %d\n",
287 1.1 ad sc->sc_dv.dv_xname, error);
288 1.1 ad return;
289 1.1 ad }
290 1.1 ad
291 1.1 ad if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, mapsize,
292 1.1 ad (caddr_t *)&sc->sc_ccbs, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
293 1.1 ad printf("%s: unable to map CCBs, error = %d\n",
294 1.1 ad sc->sc_dv.dv_xname, error);
295 1.1 ad return;
296 1.1 ad }
297 1.1 ad
298 1.1 ad if ((error = bus_dmamap_create(sc->sc_dmat, mapsize, mapsize, 1, 0,
299 1.1 ad BUS_DMA_NOWAIT, &sc->sc_dmamap_ccb)) != 0) {
300 1.1 ad printf("%s: unable to create CCB DMA map, error = %d\n",
301 1.1 ad sc->sc_dv.dv_xname, error);
302 1.1 ad return;
303 1.1 ad }
304 1.1 ad
305 1.1 ad if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_ccb,
306 1.1 ad sc->sc_ccbs, mapsize, NULL, BUS_DMA_NOWAIT)) != 0) {
307 1.1 ad printf("%s: unable to load CCB DMA map, error = %d\n",
308 1.1 ad sc->sc_dv.dv_xname, error);
309 1.1 ad return;
310 1.1 ad }
311 1.1 ad
312 1.1 ad sc->sc_sp = (struct eata_sp *)((caddr_t)sc->sc_ccbs + sc->sc_spoff);
313 1.1 ad sc->sc_sppa = sc->sc_dmamap_ccb->dm_segs[0].ds_addr + sc->sc_spoff;
314 1.1 ad sc->sc_scr = (caddr_t)sc->sc_ccbs + sc->sc_scroff;
315 1.1 ad sc->sc_scrpa = sc->sc_dmamap_ccb->dm_segs[0].ds_addr + sc->sc_scroff;
316 1.1 ad sc->sc_sp->sp_ccbid = -1;
317 1.1 ad #ifdef notdef
318 1.1 ad sc->sc_pending = 0;
319 1.1 ad #endif
320 1.1 ad /* Initialize the CCBs */
321 1.1 ad TAILQ_INIT(&sc->sc_free_ccb);
322 1.1 ad i = dpt_create_ccbs(sc, sc->sc_ccbs, sc->sc_nccbs);
323 1.1 ad
324 1.1 ad if (i == 0) {
325 1.1 ad printf("%s: unable to create CCBs\n", sc->sc_dv.dv_xname);
326 1.1 ad return;
327 1.1 ad } else if (i != sc->sc_nccbs) {
328 1.1 ad printf("%s: %d/%d CCBs created!\n", sc->sc_dv.dv_xname, i,
329 1.1 ad sc->sc_nccbs);
330 1.1 ad sc->sc_nccbs = i;
331 1.1 ad }
332 1.1 ad
333 1.1 ad /* Set shutdownhook before we start any device activity */
334 1.1 ad sc->sc_sdh = shutdownhook_establish(dpt_shutdown, sc);
335 1.1 ad
336 1.1 ad /* Get the page 0 inquiry data from the HBA */
337 1.1 ad dpt_hba_inquire(sc, &ei);
338 1.1 ad
339 1.1 ad /*
340 1.1 ad * dpt0 at pci0 dev 12 function 0: DPT SmartRAID III (PM3224A/9X-R)
341 1.1 ad * dpt0: interrupting at irq 10
342 1.1 ad * dpt0: 64 queued commands, 1 channel(s), adapter on ID(s) 7
343 1.1 ad */
344 1.1 ad for (i = 0; ei->ei_vendor[i] != ' ' && i < 8; i++)
345 1.1 ad ;
346 1.1 ad ei->ei_vendor[i] = '\0';
347 1.1 ad
348 1.1 ad for (i = 0; ei->ei_model[i] != ' ' && i < 7; i++)
349 1.1 ad model[i] = ei->ei_model[i];
350 1.1 ad for (j = 0; ei->ei_suffix[j] != ' ' && j < 7; j++)
351 1.1 ad model[i++] = ei->ei_model[i];
352 1.1 ad model[i] = '\0';
353 1.1 ad
354 1.1 ad /* Find the cannonical name for the board */
355 1.1 ad for (i = 0; dpt_cname[i]; i += 2)
356 1.1 ad if (memcmp(ei->ei_model, dpt_cname[i], 6) == 0)
357 1.1 ad break;
358 1.1 ad
359 1.1 ad printf("%s %s (%s)\n", ei->ei_vendor, dpt_cname[i + 1], model);
360 1.1 ad
361 1.1 ad if (intrstr != NULL)
362 1.1 ad printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname, intrstr);
363 1.1 ad
364 1.1 ad printf("%s: %d queued commands, %d channel(s), adapter on ID(s)",
365 1.1 ad sc->sc_dv.dv_xname, sc->sc_nccbs, dc.dc_maxchannel + 1);
366 1.1 ad
367 1.1 ad for (i = 0; i <= dc.dc_maxchannel; i++)
368 1.1 ad printf(" %d", dc.dc_hba[3 - i]);
369 1.1 ad printf("\n");
370 1.1 ad
371 1.1 ad /* Reset the SCSI bus */
372 1.1 ad dpt_cmd(sc, NULL, 0, CP_IMMEDIATE, CPI_BUS_RESET);
373 1.1 ad DELAY(20000);
374 1.1 ad
375 1.1 ad /* Fill in the adapter, each link and attach in turn */
376 1.1 ad sc->sc_adapter.scsipi_cmd = dpt_scsi_cmd;
377 1.1 ad sc->sc_adapter.scsipi_minphys = dpt_minphys;
378 1.1 ad
379 1.1 ad for (i = 0; i <= dc.dc_maxchannel; i++) {
380 1.1 ad struct scsipi_link *link;
381 1.1 ad
382 1.1 ad sc->sc_hbaid[i] = dc.dc_hba[3 - i];
383 1.1 ad link = &sc->sc_link[i];
384 1.1 ad memset(link, 0, sizeof(*link));
385 1.1 ad link->scsipi_scsi.channel = i;
386 1.1 ad link->scsipi_scsi.adapter_target = sc->sc_hbaid[i];
387 1.1 ad link->scsipi_scsi.max_lun = dc.dc_maxlun;
388 1.1 ad link->scsipi_scsi.max_target = dc.dc_maxtarget;
389 1.1 ad link->type = BUS_SCSI;
390 1.1 ad link->device = &dpt_dev;
391 1.1 ad link->adapter = &sc->sc_adapter;
392 1.1 ad link->adapter_softc = sc;
393 1.1 ad link->openings = sc->sc_nccbs;
394 1.1 ad config_found(&sc->sc_dv, link, scsiprint);
395 1.1 ad }
396 1.1 ad }
397 1.1 ad
398 1.1 ad /*
399 1.1 ad * Our 'shutdownhook' to cleanly shut down the HBA. The HBA must flush
400 1.1 ad * all data from it's cache and mark array groups as clean.
401 1.1 ad */
402 1.1 ad void
403 1.1 ad dpt_shutdown(xxx_sc)
404 1.1 ad void *xxx_sc;
405 1.1 ad {
406 1.1 ad struct dpt_softc *sc;
407 1.1 ad
408 1.1 ad sc = xxx_sc;
409 1.1 ad printf("shutting down %s...", sc->sc_dv.dv_xname);
410 1.1 ad dpt_cmd(sc, NULL, 0, CP_IMMEDIATE, CPI_POWEROFF_WARN);
411 1.1 ad DELAY(5000*1000);
412 1.1 ad printf(" done\n");
413 1.1 ad }
414 1.1 ad
415 1.1 ad /*
416 1.1 ad * Send an EATA command to the HBA.
417 1.1 ad */
418 1.1 ad void
419 1.1 ad dpt_cmd(sc, cp, addr, eatacmd, icmd)
420 1.1 ad struct dpt_softc *sc;
421 1.1 ad struct eata_cp *cp;
422 1.1 ad u_int32_t addr;
423 1.1 ad int eatacmd, icmd;
424 1.1 ad {
425 1.1 ad int i;
426 1.1 ad
427 1.1 ad for (i = 20000; i; i--) {
428 1.1 ad if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_BUSY) == 0)
429 1.1 ad break;
430 1.1 ad DELAY(50);
431 1.1 ad }
432 1.1 ad
433 1.1 ad if (i == 0) {
434 1.1 ad /* XXX what to do here? */
435 1.1 ad printf("%s: dpt_cmd failed\n", sc->sc_dv.dv_xname);
436 1.1 ad }
437 1.1 ad
438 1.1 ad if (cp == NULL)
439 1.1 ad addr = 0;
440 1.1 ad
441 1.1 ad dpt_outb(sc, HA_DMA_BASE + 0, (u_int32_t)addr);
442 1.1 ad dpt_outb(sc, HA_DMA_BASE + 1, (u_int32_t)addr >> 8);
443 1.1 ad dpt_outb(sc, HA_DMA_BASE + 2, (u_int32_t)addr >> 16);
444 1.1 ad dpt_outb(sc, HA_DMA_BASE + 3, (u_int32_t)addr >> 24);
445 1.1 ad
446 1.1 ad if (eatacmd == CP_IMMEDIATE) {
447 1.1 ad if (cp == NULL) {
448 1.1 ad /* XXX should really pass meaningful values */
449 1.1 ad dpt_outb(sc, HA_ICMD_CODE2, 0);
450 1.1 ad dpt_outb(sc, HA_ICMD_CODE1, 0);
451 1.1 ad }
452 1.1 ad dpt_outb(sc, HA_ICMD, icmd);
453 1.1 ad }
454 1.1 ad
455 1.1 ad dpt_outb(sc, HA_COMMAND, eatacmd);
456 1.1 ad }
457 1.1 ad
458 1.1 ad /*
459 1.1 ad * Wait for the HBA to reach an arbitrary state.
460 1.1 ad */
461 1.1 ad int
462 1.1 ad dpt_wait(sc, mask, state, ms)
463 1.1 ad struct dpt_softc *sc;
464 1.1 ad u_int8_t mask, state;
465 1.1 ad int ms;
466 1.1 ad {
467 1.1 ad
468 1.1 ad for (ms *= 10; ms; ms--) {
469 1.1 ad if ((dpt_inb(sc, HA_STATUS) & mask) == state)
470 1.1 ad return (0);
471 1.1 ad DELAY(100);
472 1.1 ad }
473 1.1 ad return (-1);
474 1.1 ad }
475 1.1 ad
476 1.1 ad /*
477 1.1 ad * Wait for the specified CCB to finish. This is used when we may not be
478 1.1 ad * able to sleep and/or interrupts are disabled (eg autoconfiguration).
479 1.1 ad * The timeout value from the CCB is used. This should only be used for
480 1.1 ad * CCB_PRIVATE requests; otherwise the CCB will get recycled before we get
481 1.1 ad * a look at it.
482 1.1 ad */
483 1.1 ad int
484 1.1 ad dpt_poll(sc, ccb)
485 1.1 ad struct dpt_softc *sc;
486 1.1 ad struct dpt_ccb *ccb;
487 1.1 ad {
488 1.1 ad int i;
489 1.1 ad
490 1.1 ad for (i = ccb->ccb_timeout * 20; i; i--) {
491 1.1 ad if ((ccb->ccb_flg & CCB_INTR) != 0)
492 1.1 ad return (0);
493 1.1 ad if ((dpt_inb(sc, HA_AUX_STATUS) & HA_AUX_INTR) != 0)
494 1.1 ad dpt_intr(sc);
495 1.1 ad if ((ccb->ccb_flg & CCB_INTR) != 0)
496 1.1 ad return (0);
497 1.1 ad DELAY(50);
498 1.1 ad }
499 1.1 ad return (-1);
500 1.1 ad }
501 1.1 ad
502 1.1 ad /*
503 1.1 ad * Read the EATA configuration from the HBA and perform some sanity checks.
504 1.1 ad */
505 1.1 ad int
506 1.1 ad dpt_readcfg(sc, dc)
507 1.1 ad struct dpt_softc *sc;
508 1.1 ad struct eata_cfg *dc;
509 1.1 ad {
510 1.1 ad int i, j, stat;
511 1.1 ad u_int16_t *p;
512 1.1 ad
513 1.1 ad while((((stat = dpt_inb(sc, HA_STATUS))
514 1.1 ad != (HA_ST_READY|HA_ST_SEEK_COMPLETE))
515 1.1 ad && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR))
516 1.1 ad && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR|HA_ST_DRQ)))
517 1.1 ad || (dpt_wait(sc, HA_ST_BUSY, 0, 200))) {
518 1.1 ad /* RAID drives still spinning up? */
519 1.1 ad if((dpt_inb(sc, HA_ERROR) != 'D')
520 1.1 ad || (dpt_inb(sc, HA_ERROR + 1) != 'P')
521 1.1 ad || (dpt_inb(sc, HA_ERROR + 2) != 'T'))
522 1.1 ad return (-1);
523 1.1 ad }
524 1.1 ad
525 1.1 ad /*
526 1.1 ad * Issue the read-config command and wait for the data to appear.
527 1.1 ad * XXX we shouldn't be doing this with PIO, but it makes it a lot
528 1.1 ad * easier as no DMA setup is required.
529 1.1 ad */
530 1.1 ad dpt_outb(sc, HA_COMMAND, CP_PIO_GETCFG);
531 1.1 ad memset(dc, 0, sizeof(*dc));
532 1.1 ad i = ((int)&((struct eata_cfg *)0)->dc_cfglen +
533 1.1 ad sizeof(dc->dc_cfglen)) >> 1;
534 1.1 ad p = (u_int16_t *)dc;
535 1.1 ad
536 1.1 ad if (dpt_wait(sc, 0xFF, HA_ST_DATA_RDY, 2000))
537 1.1 ad return (-1);
538 1.1 ad
539 1.1 ad /* Begin reading */
540 1.1 ad while (i--)
541 1.1 ad *p++ = dpt_inw(sc, HA_DATA);
542 1.1 ad
543 1.1 ad if ((i = dc->dc_cfglen) > (sizeof(struct eata_cfg)
544 1.1 ad - (int)(&(((struct eata_cfg *)0L)->dc_cfglen))
545 1.1 ad - sizeof(dc->dc_cfglen)))
546 1.1 ad i = sizeof(struct eata_cfg)
547 1.1 ad - (int)(&(((struct eata_cfg *)0L)->dc_cfglen))
548 1.1 ad - sizeof(dc->dc_cfglen);
549 1.1 ad
550 1.1 ad j = i + (int)(&(((struct eata_cfg *)0L)->dc_cfglen)) +
551 1.1 ad sizeof(dc->dc_cfglen);
552 1.1 ad i >>= 1;
553 1.1 ad
554 1.1 ad while (i--)
555 1.1 ad *p++ = dpt_inw(sc, HA_DATA);
556 1.1 ad
557 1.1 ad /* Flush until we have read 512 bytes. */
558 1.1 ad i = (512 - j + 1) >> 1;
559 1.1 ad while (i--)
560 1.1 ad dpt_inw(sc, HA_DATA);
561 1.1 ad
562 1.1 ad /* Defaults for older Firmware */
563 1.1 ad if (p <= (u_short *)&dc->dc_hba[DPT_MAX_CHANNELS - 1])
564 1.1 ad dc->dc_hba[DPT_MAX_CHANNELS - 1] = 7;
565 1.1 ad
566 1.1 ad if ((dpt_inb(sc, HA_STATUS) & HA_ST_ERROR) != 0) {
567 1.1 ad printf("%s: HBA error\n", sc->sc_dv.dv_xname);
568 1.1 ad return (-1);
569 1.1 ad }
570 1.1 ad
571 1.1 ad if (!dc->dc_hbavalid) {
572 1.1 ad printf("%s: dc.dc_hba field invalid\n", sc->sc_dv.dv_xname);
573 1.1 ad return (-1);
574 1.1 ad }
575 1.1 ad
576 1.1 ad if (memcmp(dc->dc_eatasig, "EATA", 4) != 0) {
577 1.1 ad printf("%s: EATA signature mismatch\n", sc->sc_dv.dv_xname);
578 1.1 ad return (-1);
579 1.1 ad }
580 1.1 ad
581 1.1 ad if (!dc->dc_dmasupported) {
582 1.1 ad printf("%s: DMA not supported\n", sc->sc_dv.dv_xname);
583 1.1 ad return (-1);
584 1.1 ad }
585 1.1 ad
586 1.1 ad return (0);
587 1.1 ad }
588 1.1 ad
589 1.1 ad /*
590 1.1 ad * Adjust the size of each I/O before it passes to the SCSI layer.
591 1.1 ad */
592 1.1 ad void
593 1.1 ad dpt_minphys(bp)
594 1.1 ad struct buf *bp;
595 1.1 ad {
596 1.1 ad
597 1.1 ad if (bp->b_bcount > DPT_MAX_XFER)
598 1.1 ad bp->b_bcount = DPT_MAX_XFER;
599 1.1 ad minphys(bp);
600 1.1 ad }
601 1.1 ad
602 1.1 ad /*
603 1.1 ad * Put a CCB onto the freelist.
604 1.1 ad */
605 1.1 ad void
606 1.1 ad dpt_free_ccb(sc, ccb)
607 1.1 ad struct dpt_softc *sc;
608 1.1 ad struct dpt_ccb *ccb;
609 1.1 ad {
610 1.1 ad int s;
611 1.1 ad
612 1.1 ad s = splbio();
613 1.1 ad ccb->ccb_flg = 0;
614 1.1 ad #ifdef notdef
615 1.1 ad sc->sc_pending--;
616 1.1 ad #endif
617 1.1 ad TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, ccb_chain);
618 1.1 ad
619 1.1 ad /* Wake anybody waiting for a free ccb */
620 1.1 ad if (ccb->ccb_chain.tqe_next == 0)
621 1.1 ad wakeup(&sc->sc_free_ccb);
622 1.1 ad
623 1.1 ad splx(s);
624 1.1 ad }
625 1.1 ad
626 1.1 ad /*
627 1.1 ad * Initialize the specified CCB.
628 1.1 ad */
629 1.1 ad int
630 1.1 ad dpt_init_ccb(sc, ccb)
631 1.1 ad struct dpt_softc *sc;
632 1.1 ad struct dpt_ccb *ccb;
633 1.1 ad {
634 1.1 ad int error;
635 1.1 ad
636 1.1 ad /* Create the DMA map for this CCB's data */
637 1.1 ad error = bus_dmamap_create(sc->sc_dmat, DPT_MAX_XFER, DPT_SG_SIZE,
638 1.1 ad DPT_MAX_XFER, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
639 1.1 ad &ccb->ccb_dmamap_xfer);
640 1.1 ad
641 1.1 ad if (error) {
642 1.1 ad printf("%s: can't create ccb dmamap (%d)\n",
643 1.1 ad sc->sc_dv.dv_xname, error);
644 1.1 ad return (error);
645 1.1 ad }
646 1.1 ad
647 1.1 ad ccb->ccb_flg = 0;
648 1.1 ad ccb->ccb_ccbpa = sc->sc_dmamap_ccb->dm_segs[0].ds_addr +
649 1.1 ad DPT_CCB_OFF(sc, ccb);
650 1.1 ad return (0);
651 1.1 ad }
652 1.1 ad
653 1.1 ad /*
654 1.1 ad * Create a set of CCBs and add them to the free list.
655 1.1 ad */
656 1.1 ad int
657 1.1 ad dpt_create_ccbs(sc, ccbstore, count)
658 1.1 ad struct dpt_softc *sc;
659 1.1 ad struct dpt_ccb *ccbstore;
660 1.1 ad int count;
661 1.1 ad {
662 1.1 ad struct dpt_ccb *ccb;
663 1.1 ad int i, error;
664 1.1 ad
665 1.1 ad memset(ccbstore, 0, sizeof(struct dpt_ccb) * count);
666 1.1 ad
667 1.1 ad for (i = 0, ccb = ccbstore; i < count; i++, ccb++) {
668 1.1 ad if ((error = dpt_init_ccb(sc, ccb)) != 0) {
669 1.1 ad printf("%s: unable to init ccb, error = %d\n",
670 1.1 ad sc->sc_dv.dv_xname, error);
671 1.1 ad break;
672 1.1 ad }
673 1.1 ad ccb->ccb_id = i;
674 1.1 ad TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_chain);
675 1.1 ad }
676 1.1 ad
677 1.1 ad return (i);
678 1.1 ad }
679 1.1 ad
680 1.1 ad /*
681 1.1 ad * Get a free ccb. If there are none, see if we can allocate a new one.
682 1.1 ad * Otherwise either return an error or if we are permitted to, sleep until
683 1.1 ad * one becomes free.
684 1.1 ad */
685 1.1 ad struct dpt_ccb *
686 1.1 ad dpt_alloc_ccb(sc, flg)
687 1.1 ad struct dpt_softc *sc;
688 1.1 ad int flg;
689 1.1 ad {
690 1.1 ad struct dpt_ccb *ccb;
691 1.1 ad int s;
692 1.1 ad
693 1.1 ad s = splbio();
694 1.1 ad
695 1.1 ad for (;;) {
696 1.1 ad ccb = sc->sc_free_ccb.tqh_first;
697 1.1 ad if (ccb) {
698 1.1 ad TAILQ_REMOVE(&sc->sc_free_ccb, ccb, ccb_chain);
699 1.1 ad break;
700 1.1 ad }
701 1.1 ad if ((flg & SCSI_NOSLEEP) != 0) {
702 1.1 ad splx(s);
703 1.1 ad return (NULL);
704 1.1 ad }
705 1.1 ad tsleep(&sc->sc_free_ccb, PRIBIO, "dptccb", 0);
706 1.1 ad }
707 1.1 ad
708 1.1 ad ccb->ccb_flg |= CCB_ALLOC;
709 1.1 ad #ifdef notdef
710 1.1 ad sc->sc_pending++;
711 1.1 ad #endif
712 1.1 ad splx(s);
713 1.1 ad return (ccb);
714 1.1 ad }
715 1.1 ad
716 1.1 ad /*
717 1.1 ad * We have a CCB which has been processed by the HBA, now we look to see how
718 1.1 ad * the operation went. CCBs marked with CCB_PRIVATE never get to here.
719 1.1 ad */
720 1.1 ad void
721 1.1 ad dpt_done_ccb(sc, ccb)
722 1.1 ad struct dpt_softc *sc;
723 1.1 ad struct dpt_ccb *ccb;
724 1.1 ad {
725 1.1 ad struct scsipi_sense_data *s1, *s2;
726 1.1 ad struct scsipi_xfer *xs;
727 1.1 ad bus_dma_tag_t dmat;
728 1.1 ad
729 1.1 ad dmat = sc->sc_dmat;
730 1.1 ad xs = ccb->ccb_xs;
731 1.1 ad
732 1.1 ad SC_DEBUG(xs->sc_link, SDEV_DB2, ("dpt_done_ccb\n"));
733 1.1 ad
734 1.1 ad /*
735 1.1 ad * If we were a data transfer, unload the map that described the
736 1.1 ad * data buffer.
737 1.1 ad */
738 1.1 ad if (xs->datalen) {
739 1.1 ad bus_dmamap_sync(dmat, ccb->ccb_dmamap_xfer, 0,
740 1.1 ad ccb->ccb_dmamap_xfer->dm_mapsize,
741 1.1 ad (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
742 1.1 ad BUS_DMASYNC_POSTWRITE);
743 1.1 ad bus_dmamap_unload(dmat, ccb->ccb_dmamap_xfer);
744 1.1 ad }
745 1.1 ad
746 1.1 ad /*
747 1.1 ad * Otherwise, put the results of the operation into the xfer and
748 1.1 ad * call whoever started it.
749 1.1 ad */
750 1.1 ad if ((ccb->ccb_flg & CCB_ALLOC) == 0) {
751 1.1 ad panic("%s: done ccb not allocated!\n", sc->sc_dv.dv_xname);
752 1.1 ad return;
753 1.1 ad }
754 1.1 ad
755 1.1 ad if (xs->error == XS_NOERROR) {
756 1.1 ad if (ccb->ccb_hba_status != HA_NO_ERROR) {
757 1.1 ad switch (ccb->ccb_hba_status) {
758 1.1 ad case HA_ERROR_SEL_TO:
759 1.1 ad xs->error = XS_SELTIMEOUT;
760 1.1 ad break;
761 1.1 ad case HA_ERROR_RESET:
762 1.1 ad xs->error = XS_RESET;
763 1.1 ad break;
764 1.1 ad default: /* Other scsi protocol messes */
765 1.1 ad printf("%s: HBA status %x\n",
766 1.1 ad sc->sc_dv.dv_xname, ccb->ccb_hba_status);
767 1.1 ad xs->error = XS_DRIVER_STUFFUP;
768 1.1 ad }
769 1.1 ad } else if (ccb->ccb_scsi_status != SCSI_OK) {
770 1.1 ad switch (ccb->ccb_scsi_status) {
771 1.1 ad case SCSI_CHECK:
772 1.1 ad s1 = &ccb->ccb_sense;
773 1.1 ad s2 = &xs->sense.scsi_sense;
774 1.1 ad *s2 = *s1;
775 1.1 ad xs->error = XS_SENSE;
776 1.1 ad break;
777 1.1 ad case SCSI_BUSY:
778 1.1 ad xs->error = XS_BUSY;
779 1.1 ad break;
780 1.1 ad default:
781 1.1 ad printf("%s: SCSI status %x\n",
782 1.1 ad sc->sc_dv.dv_xname, ccb->ccb_scsi_status);
783 1.1 ad xs->error = XS_DRIVER_STUFFUP;
784 1.1 ad }
785 1.1 ad } else
786 1.1 ad xs->resid = 0;
787 1.1 ad }
788 1.1 ad
789 1.1 ad /* Free up the CCB and mark the command as done */
790 1.1 ad dpt_free_ccb(sc, ccb);
791 1.1 ad xs->flags |= ITSDONE;
792 1.1 ad scsipi_done(xs);
793 1.1 ad
794 1.1 ad /*
795 1.1 ad * If there are queue entries in the software queue, try to run the
796 1.1 ad * first one. We should be more or less guaranteed to succeed, since
797 1.1 ad * we just freed an CCB. NOTE: dpt_scsi_cmd() relies on our calling
798 1.1 ad * it with the first entry in the queue.
799 1.1 ad */
800 1.1 ad if ((xs = TAILQ_FIRST(&sc->sc_queue)) != NULL)
801 1.1 ad dpt_scsi_cmd(xs);
802 1.1 ad }
803 1.1 ad
804 1.1 ad /*
805 1.1 ad * Start a SCSI command.
806 1.1 ad */
807 1.1 ad int
808 1.1 ad dpt_scsi_cmd(xs)
809 1.1 ad struct scsipi_xfer *xs;
810 1.1 ad {
811 1.1 ad int error, seg, flags, s, fromqueue, dontqueue;
812 1.1 ad struct scsipi_link *sc_link;
813 1.1 ad struct dpt_softc *sc;
814 1.1 ad struct dpt_ccb *ccb;
815 1.1 ad struct eata_sg *sg;
816 1.1 ad struct eata_cp *cp;
817 1.1 ad bus_dma_tag_t dmat;
818 1.1 ad
819 1.1 ad sc_link = xs->sc_link;
820 1.1 ad flags = xs->flags;
821 1.1 ad sc = sc_link->adapter_softc;
822 1.1 ad dmat = sc->sc_dmat;
823 1.1 ad fromqueue = 0;
824 1.1 ad dontqueue = 0;
825 1.1 ad
826 1.1 ad SC_DEBUG(sc_link, SDEV_DB2, ("dpt_scsi_cmd\n"));
827 1.1 ad
828 1.1 ad /* Protect the queue */
829 1.1 ad s = splbio();
830 1.1 ad
831 1.1 ad /*
832 1.1 ad * If we're running the queue from dpt_done_ccb(), we've been called
833 1.1 ad * with the first queue entry as our argument.
834 1.1 ad */
835 1.1 ad if (xs == TAILQ_FIRST(&sc->sc_queue)) {
836 1.1 ad TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
837 1.1 ad fromqueue = 1;
838 1.1 ad } else {
839 1.1 ad /* Cmds must be no more than 12 bytes for us */
840 1.1 ad if (xs->cmdlen > 12) {
841 1.1 ad splx(s);
842 1.1 ad xs->error = XS_DRIVER_STUFFUP;
843 1.1 ad return (COMPLETE);
844 1.1 ad }
845 1.1 ad
846 1.1 ad /* XXX we can't reset devices just yet */
847 1.1 ad if ((flags & SCSI_RESET) != 0) {
848 1.1 ad xs->error = XS_DRIVER_STUFFUP;
849 1.1 ad return (COMPLETE);
850 1.1 ad }
851 1.1 ad
852 1.1 ad /* Polled requests can't be queued for later */
853 1.1 ad dontqueue = flags & SCSI_POLL;
854 1.1 ad
855 1.1 ad /* If there are jobs in the queue, run them first */
856 1.1 ad if (TAILQ_FIRST(&sc->sc_queue) != NULL) {
857 1.1 ad /*
858 1.1 ad * If we can't queue, we have to abort, since we have
859 1.1 ad * to preserve the queue order.
860 1.1 ad */
861 1.1 ad if (dontqueue) {
862 1.1 ad splx(s);
863 1.1 ad xs->error = XS_DRIVER_STUFFUP;
864 1.1 ad return (TRY_AGAIN_LATER);
865 1.1 ad }
866 1.1 ad
867 1.1 ad /* Swap with the first queue entry. */
868 1.1 ad TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
869 1.1 ad xs = TAILQ_FIRST(&sc->sc_queue);
870 1.1 ad TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
871 1.1 ad fromqueue = 1;
872 1.1 ad }
873 1.1 ad }
874 1.1 ad
875 1.1 ad /*
876 1.1 ad * Get a CCB. If the transfer is from a buf (possibly from interrupt
877 1.1 ad * time) then we can't allow it to sleep.
878 1.1 ad */
879 1.1 ad if ((ccb = dpt_alloc_ccb(sc, flags)) == NULL) {
880 1.1 ad /* If we can't queue, we lose */
881 1.1 ad if (dontqueue) {
882 1.1 ad splx(s);
883 1.1 ad xs->error = XS_DRIVER_STUFFUP;
884 1.1 ad return (TRY_AGAIN_LATER);
885 1.1 ad }
886 1.1 ad
887 1.1 ad /*
888 1.1 ad * Stuff request into the queue, in front if we came off
889 1.1 ad * in the first place.
890 1.1 ad */
891 1.1 ad if (fromqueue)
892 1.1 ad TAILQ_INSERT_HEAD(&sc->sc_queue, xs, adapter_q);
893 1.1 ad else
894 1.1 ad TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
895 1.1 ad #ifdef notdef
896 1.1 ad sc->sc_pending++;
897 1.1 ad #endif
898 1.1 ad splx(s);
899 1.1 ad return (SUCCESSFULLY_QUEUED);
900 1.1 ad }
901 1.1 ad
902 1.1 ad #ifdef notdef
903 1.1 ad /*
904 1.1 ad * Request has been shifted from pending command queue to executing
905 1.1 ad * CCB queue. Bump sc_pending to compensate.
906 1.1 ad */
907 1.1 ad sc->sc_pending--;
908 1.1 ad #endif
909 1.1 ad splx(s);
910 1.1 ad
911 1.1 ad ccb->ccb_xs = xs;
912 1.1 ad ccb->ccb_timeout = xs->timeout;
913 1.1 ad
914 1.1 ad cp = &ccb->ccb_eata_cp;
915 1.1 ad memcpy(&cp->cp_scsi_cmd, xs->cmd, xs->cmdlen);
916 1.1 ad cp->cp_ccbid = ccb->ccb_id;
917 1.1 ad cp->cp_id = sc_link->scsipi_scsi.target;
918 1.1 ad cp->cp_lun = sc_link->scsipi_scsi.lun;
919 1.1 ad cp->cp_channel = sc_link->scsipi_scsi.channel;
920 1.1 ad cp->cp_senselen = sizeof(ccb->ccb_sense);
921 1.1 ad cp->cp_stataddr = SWAP32(sc->sc_sppa);
922 1.1 ad cp->cp_dispri = 1;
923 1.1 ad cp->cp_luntar = 0;
924 1.1 ad cp->cp_identify = 1;
925 1.1 ad cp->cp_autosense = 1;
926 1.1 ad cp->cp_quick = 0;
927 1.1 ad cp->cp_datain = ((flags & SCSI_DATA_IN) != 0);
928 1.1 ad cp->cp_dataout = ((flags & SCSI_DATA_OUT) != 0);
929 1.1 ad cp->cp_interpret = (sc->sc_hbaid[sc_link->scsipi_scsi.channel] ==
930 1.1 ad sc_link->scsipi_scsi.target);
931 1.1 ad
932 1.1 ad cp->cp_senseaddr = SWAP32(sc->sc_dmamap_ccb->dm_segs[0].ds_addr +
933 1.1 ad DPT_CCB_OFF(sc, ccb) + offsetof(struct dpt_ccb, ccb_sense));
934 1.1 ad
935 1.1 ad if (xs->datalen) {
936 1.1 ad sg = ccb->ccb_sg;
937 1.1 ad seg = 0;
938 1.1 ad #ifdef TFS
939 1.1 ad if (flags & SCSI_DATA_UIO) {
940 1.1 ad error = bus_dmamap_load_uio(dmat,
941 1.1 ad ccb->ccb_dmamap_xfer, (struct uio *)xs->data,
942 1.1 ad (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
943 1.1 ad BUS_DMA_WAITOK);
944 1.1 ad } else
945 1.1 ad #endif /*TFS */
946 1.1 ad {
947 1.1 ad error = bus_dmamap_load(dmat,
948 1.1 ad ccb->ccb_dmamap_xfer,
949 1.1 ad xs->data, xs->datalen, NULL,
950 1.1 ad (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
951 1.1 ad BUS_DMA_WAITOK);
952 1.1 ad }
953 1.1 ad
954 1.1 ad if (error) {
955 1.1 ad printf("%s: dpt_scsi_cmd: ", sc->sc_dv.dv_xname);
956 1.1 ad if (error == EFBIG)
957 1.1 ad printf("more than %d dma segs\n", DPT_SG_SIZE);
958 1.1 ad else
959 1.1 ad printf("error %d loading dma map\n", error);
960 1.1 ad
961 1.1 ad xs->error = XS_DRIVER_STUFFUP;
962 1.1 ad dpt_free_ccb(sc, ccb);
963 1.1 ad return (COMPLETE);
964 1.1 ad }
965 1.1 ad
966 1.1 ad bus_dmamap_sync(dmat, ccb->ccb_dmamap_xfer, 0,
967 1.1 ad ccb->ccb_dmamap_xfer->dm_mapsize,
968 1.1 ad (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
969 1.1 ad BUS_DMASYNC_PREWRITE);
970 1.1 ad
971 1.1 ad /*
972 1.1 ad * Load the hardware scatter/gather map with the
973 1.1 ad * contents of the DMA map.
974 1.1 ad */
975 1.1 ad for (seg = 0; seg < ccb->ccb_dmamap_xfer->dm_nsegs; seg++) {
976 1.1 ad ccb->ccb_sg[seg].sg_addr =
977 1.1 ad SWAP32(ccb->ccb_dmamap_xfer->dm_segs[seg].ds_addr);
978 1.1 ad ccb->ccb_sg[seg].sg_len =
979 1.1 ad SWAP32(ccb->ccb_dmamap_xfer->dm_segs[seg].ds_len);
980 1.1 ad }
981 1.1 ad
982 1.1 ad cp->cp_dataaddr = SWAP32(sc->sc_dmamap_ccb->dm_segs[0].ds_addr
983 1.1 ad + DPT_CCB_OFF(sc, ccb) + offsetof(struct dpt_ccb, ccb_sg));
984 1.1 ad cp->cp_datalen = SWAP32(seg * sizeof(struct eata_sg));
985 1.1 ad cp->cp_scatter = 1;
986 1.1 ad } else {
987 1.1 ad cp->cp_dataaddr = 0;
988 1.1 ad cp->cp_datalen = 0;
989 1.1 ad cp->cp_scatter = 0;
990 1.1 ad }
991 1.1 ad
992 1.1 ad /* Sync up CCB and status packet */
993 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, DPT_CCB_OFF(sc, ccb),
994 1.1 ad sizeof(struct dpt_ccb), BUS_DMASYNC_PREWRITE);
995 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, sc->sc_spoff,
996 1.1 ad sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
997 1.1 ad
998 1.1 ad /*
999 1.1 ad * Start the command. If we are polling on completion, mark it
1000 1.1 ad * private so that dpt_intr/dpt_done_ccb don't recycle the CCB
1001 1.1 ad * without us noticing.
1002 1.1 ad */
1003 1.1 ad if (dontqueue != 0)
1004 1.1 ad ccb->ccb_flg |= CCB_PRIVATE;
1005 1.1 ad
1006 1.1 ad dpt_cmd(sc, &ccb->ccb_eata_cp, ccb->ccb_ccbpa, CP_DMA_CMD, 0);
1007 1.1 ad
1008 1.1 ad if (dontqueue == 0)
1009 1.1 ad return (SUCCESSFULLY_QUEUED);
1010 1.1 ad
1011 1.1 ad /* Don't wait longer than this single command wants to wait */
1012 1.1 ad if (dpt_poll(sc, ccb)) {
1013 1.1 ad dpt_timeout(ccb);
1014 1.1 ad /* Wait for abort to complete */
1015 1.1 ad if (dpt_poll(sc, ccb))
1016 1.1 ad dpt_timeout(ccb);
1017 1.1 ad }
1018 1.1 ad
1019 1.1 ad dpt_done_ccb(sc, ccb);
1020 1.1 ad return (COMPLETE);
1021 1.1 ad }
1022 1.1 ad
1023 1.1 ad /*
1024 1.1 ad * Specified CCB has timed out, abort it.
1025 1.1 ad */
1026 1.1 ad void
1027 1.1 ad dpt_timeout(arg)
1028 1.1 ad void *arg;
1029 1.1 ad {
1030 1.1 ad struct scsipi_link *sc_link;
1031 1.1 ad struct scsipi_xfer *xs;
1032 1.1 ad struct dpt_softc *sc;
1033 1.1 ad struct dpt_ccb *ccb;
1034 1.1 ad int s;
1035 1.1 ad
1036 1.1 ad ccb = arg;
1037 1.1 ad xs = ccb->ccb_xs;
1038 1.1 ad sc_link = xs->sc_link;
1039 1.1 ad sc = sc_link->adapter_softc;
1040 1.1 ad
1041 1.1 ad scsi_print_addr(sc_link);
1042 1.1 ad printf("timed out (status:%02x aux status:%02x)",
1043 1.1 ad dpt_inb(sc, HA_STATUS), dpt_inb(sc, HA_AUX_STATUS));
1044 1.1 ad
1045 1.1 ad s = splbio();
1046 1.1 ad
1047 1.1 ad if ((ccb->ccb_flg & CCB_ABORT) != 0) {
1048 1.1 ad /* Abort timed out, reset the HBA */
1049 1.1 ad printf(" AGAIN, resetting HBA\n");
1050 1.1 ad dpt_outb(sc, HA_COMMAND, CP_RESET);
1051 1.1 ad DELAY(750000);
1052 1.1 ad } else {
1053 1.1 ad /* Abort the operation that has timed out */
1054 1.1 ad printf("\n");
1055 1.1 ad ccb->ccb_xs->error = XS_TIMEOUT;
1056 1.1 ad ccb->ccb_timeout = DPT_ABORT_TIMEOUT;
1057 1.1 ad ccb->ccb_flg |= CCB_ABORT;
1058 1.1 ad /* Start the abort */
1059 1.1 ad dpt_cmd(sc, &ccb->ccb_eata_cp, ccb->ccb_ccbpa, CP_IMMEDIATE,
1060 1.1 ad CPI_SPEC_ABORT);
1061 1.1 ad }
1062 1.1 ad
1063 1.1 ad splx(s);
1064 1.1 ad }
1065 1.1 ad
1066 1.1 ad #if 0 && defined(DEBUG)
1067 1.1 ad /*
1068 1.1 ad * Dump the contents of an EATA status packet.
1069 1.1 ad */
1070 1.1 ad static void
1071 1.1 ad dpt_dump_sp(sp)
1072 1.1 ad struct eata_sp *sp;
1073 1.1 ad {
1074 1.1 ad int i;
1075 1.1 ad
1076 1.1 ad printf("\thba_status\t%02x\n", sp->sp_hba_status);
1077 1.1 ad printf("\teoc\t\t%d\n", sp->sp_eoc);
1078 1.1 ad printf("\tscsi_status\t%02x\n", sp->sp_scsi_status);
1079 1.1 ad printf("\tinv_residue\t%d\n", sp->sp_inv_residue);
1080 1.1 ad printf("\tccbid\t\t%d\n", sp->sp_ccbid);
1081 1.1 ad printf("\tid_message\t%d\n", sp->sp_id_message);
1082 1.1 ad printf("\tque_message\t%d\n", sp->sp_que_message);
1083 1.1 ad printf("\ttag_message\t%d\n", sp->sp_tag_message);
1084 1.1 ad printf("\tmessages\t");
1085 1.1 ad
1086 1.1 ad for (i = 0; i < 9; i++)
1087 1.1 ad printf("%d ", sp->sp_messages[i]);
1088 1.1 ad
1089 1.1 ad printf("\n");
1090 1.1 ad }
1091 1.1 ad #endif /* DEBUG */
1092 1.1 ad
1093 1.1 ad /*
1094 1.1 ad * Get inquiry data from the adapter.
1095 1.1 ad */
1096 1.1 ad void
1097 1.1 ad dpt_hba_inquire(sc, ei)
1098 1.1 ad struct dpt_softc *sc;
1099 1.1 ad struct eata_inquiry_data **ei;
1100 1.1 ad {
1101 1.1 ad struct dpt_ccb *ccb;
1102 1.1 ad struct eata_cp *cp;
1103 1.1 ad bus_dma_tag_t dmat;
1104 1.1 ad
1105 1.1 ad *ei = (struct eata_inquiry_data *)sc->sc_scr;
1106 1.1 ad dmat = sc->sc_dmat;
1107 1.1 ad
1108 1.1 ad /* Get a CCB and mark as private */
1109 1.1 ad if ((ccb = dpt_alloc_ccb(sc, 0)) == NULL)
1110 1.1 ad panic("%s: no CCB for inquiry", sc->sc_dv.dv_xname);
1111 1.1 ad
1112 1.1 ad ccb->ccb_flg |= CCB_PRIVATE;
1113 1.1 ad ccb->ccb_timeout = 200;
1114 1.1 ad
1115 1.1 ad /* Put all the arguments into the CCB */
1116 1.1 ad cp = &ccb->ccb_eata_cp;
1117 1.1 ad memset(cp, 0, sizeof(*cp));
1118 1.1 ad
1119 1.1 ad cp->cp_ccbid = ccb->ccb_id;
1120 1.1 ad cp->cp_id = sc->sc_hbaid[0];
1121 1.1 ad cp->cp_lun = 0;
1122 1.1 ad cp->cp_channel = 0;
1123 1.1 ad cp->cp_senselen = sizeof(ccb->ccb_sense);
1124 1.1 ad cp->cp_stataddr = SWAP32(sc->sc_sppa);
1125 1.1 ad cp->cp_dispri = 1;
1126 1.1 ad cp->cp_luntar = 0;
1127 1.1 ad cp->cp_identify = 1;
1128 1.1 ad cp->cp_autosense = 0;
1129 1.1 ad cp->cp_quick = 0;
1130 1.1 ad cp->cp_interpret = 1;
1131 1.1 ad cp->cp_datain = 1;
1132 1.1 ad cp->cp_dataout = 0;
1133 1.1 ad cp->cp_senseaddr = 0;
1134 1.1 ad cp->cp_dataaddr = SWAP32(sc->sc_scrpa);
1135 1.1 ad cp->cp_datalen = SWAP32(sizeof(struct eata_inquiry_data));
1136 1.1 ad cp->cp_scatter = 0;
1137 1.1 ad
1138 1.1 ad /* Put together the SCSI inquiry command */
1139 1.1 ad memset(&cp->cp_scsi_cmd, 0, 12); /* XXX */
1140 1.1 ad cp->cp_scsi_cmd = INQUIRY;
1141 1.1 ad cp->cp_len = sizeof(struct eata_inquiry_data);
1142 1.1 ad
1143 1.1 ad /* Sync up CCB, status packet and scratch area */
1144 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, DPT_CCB_OFF(sc, ccb),
1145 1.1 ad sizeof(struct dpt_ccb), BUS_DMASYNC_PREWRITE);
1146 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, sc->sc_spoff,
1147 1.1 ad sizeof(struct eata_sp), BUS_DMASYNC_PREREAD);
1148 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, sc->sc_scroff,
1149 1.1 ad sizeof(struct eata_inquiry_data), BUS_DMASYNC_PREREAD);
1150 1.1 ad
1151 1.1 ad /* Start the command and poll on completion */
1152 1.1 ad dpt_cmd(sc, &ccb->ccb_eata_cp, ccb->ccb_ccbpa, CP_DMA_CMD, 0);
1153 1.1 ad
1154 1.1 ad if (dpt_poll(sc, ccb))
1155 1.1 ad panic("%s: inquiry timed out", sc->sc_dv.dv_xname);
1156 1.1 ad
1157 1.1 ad if (ccb->ccb_hba_status != HA_NO_ERROR ||
1158 1.1 ad ccb->ccb_scsi_status != SCSI_OK)
1159 1.1 ad panic("%s: inquiry failed (hba:%02x scsi:%02x",
1160 1.1 ad sc->sc_dv.dv_xname, ccb->ccb_hba_status,
1161 1.1 ad ccb->ccb_scsi_status);
1162 1.1 ad
1163 1.1 ad /* Sync up the DMA map and free CCB, returning */
1164 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ccb, sc->sc_scroff,
1165 1.1 ad sizeof(struct eata_inquiry_data), BUS_DMASYNC_POSTREAD);
1166 1.1 ad dpt_free_ccb(sc, ccb);
1167 1.1 ad }
1168