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