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