iha.c revision 1.4 1 /* $NetBSD: iha.c,v 1.4 2001/07/07 16:13:48 thorpej Exp $ */
2 /*
3 * Initio INI-9xxxU/UW SCSI Device Driver
4 *
5 * Copyright (c) 2000 Ken Westerback
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 *-------------------------------------------------------------------------
30 *
31 * Ported from i91u.c, provided by Initio Corporation, which credits:
32 *
33 * Device driver for the INI-9XXXU/UW or INIC-940/950 PCI SCSI Controller.
34 *
35 * FreeBSD
36 *
37 * Written for 386bsd and FreeBSD by
38 * Winston Hung <winstonh (at) initio.com>
39 *
40 * Copyright (c) 1997-99 Initio Corp. All rights reserved.
41 *
42 *-------------------------------------------------------------------------
43 */
44
45 /*
46 * Ported to NetBSD by Izumi Tsutsui <tsutsui (at) ceres.dti.ne.jp> from OpenBSD:
47 * $OpenBSD: iha.c,v 1.3 2001/02/20 00:47:33 krw Exp $
48 */
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/buf.h>
54 #include <sys/device.h>
55 #include <sys/malloc.h>
56
57 #include <uvm/uvm_extern.h>
58
59 #include <machine/bus.h>
60 #include <machine/intr.h>
61
62 #include <dev/scsipi/scsi_all.h>
63 #include <dev/scsipi/scsipi_all.h>
64 #include <dev/scsipi/scsiconf.h>
65 #include <dev/scsipi/scsi_message.h>
66
67 #include <dev/ic/ihareg.h>
68 #include <dev/ic/ihavar.h>
69
70 /*
71 * SCSI Rate Table, indexed by FLAG_SCSI_RATE field of
72 * tcs flags.
73 */
74 static u_int8_t tul_rate_tbl[8] = {
75 /* fast 20 */
76 /* nanosecond divide by 4 */
77 12, /* 50ns, 20M */
78 18, /* 75ns, 13.3M */
79 25, /* 100ns, 10M */
80 31, /* 125ns, 8M */
81 37, /* 150ns, 6.6M */
82 43, /* 175ns, 5.7M */
83 50, /* 200ns, 5M */
84 62 /* 250ns, 4M */
85 };
86
87 static u_int16_t eeprom_default[EEPROM_SIZE] = {
88 /* -- Header ------------------------------------ */
89 /* signature */
90 EEP_SIGNATURE,
91 /* size, revision */
92 EEP_WORD(EEPROM_SIZE * 2, 0x01),
93 /* -- Host Adapter Structure -------------------- */
94 /* model */
95 0x0095,
96 /* model info, number of channel */
97 EEP_WORD(0x00, 1),
98 /* BIOS config */
99 EEP_BIOSCFG_DEFAULT,
100 /* host adapter config */
101 0,
102
103 /* -- eeprom_adapter[0] ------------------------------- */
104 /* ID, adapter config 1 */
105 EEP_WORD(7, CFG_DEFAULT),
106 /* adapter config 2, number of targets */
107 EEP_WORD(0x00, 8),
108 /* target flags */
109 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
110 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
111 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
112 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
113 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
114 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
115 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
116 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
117
118 /* -- eeprom_adapter[1] ------------------------------- */
119 /* ID, adapter config 1 */
120 EEP_WORD(7, CFG_DEFAULT),
121 /* adapter config 2, number of targets */
122 EEP_WORD(0x00, 8),
123 /* target flags */
124 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
125 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
126 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
127 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
128 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
129 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
130 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
131 EEP_WORD(FLAG_DEFAULT, FLAG_DEFAULT),
132 /* reserved[5] */
133 0, 0, 0, 0, 0,
134 /* checksum */
135 0
136 };
137
138 static u_int8_t tul_data_over_run(struct iha_scsi_req_q *);
139
140 static int tul_push_sense_request(struct iha_softc *, struct iha_scsi_req_q *);
141 static void tul_timeout(void *);
142 static int tul_alloc_sglist(struct iha_softc *);
143
144 static void tul_read_eeprom(struct iha_softc *, struct iha_eeprom *);
145 static void tul_se2_update_all(struct iha_softc *);
146 static int tul_se2_rd_all(struct iha_softc *, u_int16_t *);
147 static void tul_se2_wr(struct iha_softc *, int, u_int16_t);
148 static void tul_se2_instr(struct iha_softc *, int);
149 static u_int16_t tul_se2_rd(struct iha_softc *, int);
150
151 static void tul_reset_scsi_bus(struct iha_softc *);
152 static void tul_reset_chip(struct iha_softc *);
153 static void tul_reset_dma(struct iha_softc *);
154
155 static void tul_reset_tcs(struct tcs *, u_int8_t);
156
157 static void tul_done_scb(struct iha_softc *, struct iha_scsi_req_q *);
158 static void tul_exec_scb(struct iha_softc *, struct iha_scsi_req_q *);
159
160 static void tul_main(struct iha_softc *);
161 static void tul_scsi(struct iha_softc *);
162
163 static int tul_wait(struct iha_softc *, u_int8_t);
164
165 static __inline void tul_mark_busy_scb(struct iha_scsi_req_q *);
166
167 static void tul_append_free_scb(struct iha_softc *, struct iha_scsi_req_q *);
168 static void tul_append_done_scb(struct iha_softc *, struct iha_scsi_req_q *,
169 u_int8_t);
170 static __inline struct iha_scsi_req_q *tul_pop_done_scb(struct iha_softc *);
171
172 static __inline void tul_append_pend_scb(struct iha_softc *,
173 struct iha_scsi_req_q *);
174 static __inline void tul_push_pend_scb(struct iha_softc *,
175 struct iha_scsi_req_q *);
176 static __inline void tul_del_pend_scb(struct iha_softc *,
177 struct iha_scsi_req_q *);
178 static struct iha_scsi_req_q *tul_find_pend_scb(struct iha_softc *);
179
180 static void tul_sync_done(struct iha_softc *);
181 static void tul_wdtr_done(struct iha_softc *);
182 static void tul_bad_seq(struct iha_softc *);
183
184 static int tul_next_state(struct iha_softc *);
185 static int tul_state_1(struct iha_softc *);
186 static int tul_state_2(struct iha_softc *);
187 static int tul_state_3(struct iha_softc *);
188 static int tul_state_4(struct iha_softc *);
189 static int tul_state_5(struct iha_softc *);
190 static int tul_state_6(struct iha_softc *);
191 static int tul_state_8(struct iha_softc *);
192
193 static void tul_set_ssig(struct iha_softc *, u_int8_t, u_int8_t);
194
195 static int tul_xpad_in(struct iha_softc *);
196 static int tul_xpad_out(struct iha_softc *);
197
198 static int tul_xfer_data(struct iha_softc *, struct iha_scsi_req_q *,
199 int direction);
200
201 static int tul_status_msg(struct iha_softc *);
202
203 static int tul_msgin(struct iha_softc *);
204 static int tul_msgin_sync(struct iha_softc *);
205 static int tul_msgin_extend(struct iha_softc *);
206 static int tul_msgin_ignore_wid_resid(struct iha_softc *);
207
208 static int tul_msgout(struct iha_softc *, u_int8_t);
209 static void tul_msgout_abort(struct iha_softc *, u_int8_t);
210 static int tul_msgout_reject(struct iha_softc *);
211 static int tul_msgout_sync(struct iha_softc *);
212 static int tul_msgout_wide(struct iha_softc *);
213
214 static void tul_select(struct iha_softc *, struct iha_scsi_req_q *, u_int8_t);
215
216 static void tul_busfree(struct iha_softc *);
217 static int tul_resel(struct iha_softc *);
218
219 static void tul_abort_xs(struct iha_softc *, struct scsipi_xfer *, u_int8_t);
220
221 static void iha_minphys(struct buf *);
222 void iha_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
223 void *arg);
224
225 /*
226 * iha_intr - the interrupt service routine for the iha driver
227 */
228 int
229 iha_intr(arg)
230 void *arg;
231 {
232 bus_space_tag_t iot;
233 bus_space_handle_t ioh;
234 struct iha_softc *sc;
235 int s;
236
237 sc = (struct iha_softc *)arg;
238 iot = sc->sc_iot;
239 ioh = sc->sc_ioh;
240
241 if ((bus_space_read_1(iot, ioh, TUL_STAT0) & INTPD) == 0)
242 return (0);
243
244 s = splbio(); /* XXX - Or are interrupts off when ISR's are called? */
245
246 if (sc->sc_semaph != SEMAPH_IN_MAIN) {
247 /* XXX - need these inside a splbio()/splx()? */
248 bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
249 sc->sc_semaph = SEMAPH_IN_MAIN;
250
251 tul_main(sc);
252
253 sc->sc_semaph = ~SEMAPH_IN_MAIN;
254 bus_space_write_1(iot, ioh, TUL_IMSK, (MASK_ALL & ~MSCMP));
255 }
256
257 splx(s);
258
259 return (1);
260 }
261
262 void
263 iha_scsipi_request(chan, req, arg)
264 struct scsipi_channel *chan;
265 scsipi_adapter_req_t req;
266 void *arg;
267 {
268 struct scsipi_xfer *xs;
269 struct scsipi_periph *periph;
270 struct iha_scsi_req_q *scb;
271 struct iha_softc *sc;
272 int error, flags, s;
273
274 sc = (struct iha_softc *)chan->chan_adapter->adapt_dev;
275
276 switch (req) {
277 case ADAPTER_REQ_RUN_XFER:
278 xs = arg;
279 periph = xs->xs_periph;
280 flags = xs->xs_control;
281
282 if (xs->cmdlen > sizeof(struct scsi_generic) ||
283 periph->periph_target >= IHA_MAX_TARGETS) {
284 xs->error = XS_DRIVER_STUFFUP;
285 return;
286 }
287
288 s = splbio();
289 scb = TAILQ_FIRST(&sc->sc_freescb);
290 if (scb != NULL) {
291 scb->status = STATUS_RENT;
292 TAILQ_REMOVE(&sc->sc_freescb, scb, chain);
293 }
294 #ifdef DIAGNOSTIC
295 else {
296 scsipi_printaddr(periph);
297 printf("unable to allocate scb\n");
298 panic("iha_scsipi_request");
299 }
300 #endif
301 splx(s);
302
303 scb->target = periph->periph_target;
304 scb->lun = periph->periph_lun;
305 scb->tcs = &sc->sc_tcs[scb->target];
306 scb->flags = xs->xs_control; /* XXX */
307 scb->scb_id = MSG_IDENTIFY(periph->periph_lun,
308 (xs->xs_control & XS_CTL_REQSENSE) == 0);
309
310 scb->xs = xs;
311 scb->timeout = xs->timeout;
312 scb->cmdlen = xs->cmdlen;
313 memcpy(&scb->cmd, xs->cmd, xs->cmdlen);
314
315 scb->buflen = xs->datalen;
316
317 if (scb->buflen > 0) {
318 error = bus_dmamap_load(sc->sc_dmat, scb->dmap,
319 xs->data, scb->buflen, NULL,
320 (xs->xs_control & XS_CTL_NOSLEEP) ?
321 BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
322
323 if (error) {
324 printf("%s: error %d loading dma map\n",
325 sc->sc_dev.dv_xname, error);
326 tul_append_free_scb(sc, scb);
327 xs->error = XS_DRIVER_STUFFUP;
328 scsipi_done(xs);
329 return;
330 }
331 bus_dmamap_sync(sc->sc_dmat, scb->dmap,
332 0, scb->dmap->dm_mapsize,
333 (xs->xs_control & XS_CTL_DATA_IN) ?
334 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
335 }
336
337 tul_exec_scb(sc, scb);
338 return;
339
340 case ADAPTER_REQ_GROW_RESOURCES:
341 return; /* XXX */
342
343 case ADAPTER_REQ_SET_XFER_MODE:
344 return; /* XXX */
345 }
346 }
347
348 void
349 iha_attach(sc)
350 struct iha_softc *sc;
351 {
352 bus_space_tag_t iot = sc->sc_iot;
353 bus_space_handle_t ioh = sc->sc_ioh;
354 struct iha_scsi_req_q *scb;
355 struct iha_eeprom eeprom;
356 struct eeprom_adapter *conf;
357 int i, error, reg;
358
359 tul_read_eeprom(sc, &eeprom);
360
361 conf = &eeprom.adapter[0];
362
363 /*
364 * fill in the rest of the IHA_SOFTC fields
365 */
366 sc->sc_id = CFG_ID(conf->config1);
367 sc->sc_semaph = ~SEMAPH_IN_MAIN;
368 sc->sc_status0 = 0;
369 sc->sc_actscb = NULL;
370
371 TAILQ_INIT(&sc->sc_freescb);
372 TAILQ_INIT(&sc->sc_pendscb);
373 TAILQ_INIT(&sc->sc_donescb);
374 error = tul_alloc_sglist(sc);
375 if (error != 0) {
376 printf(": cannot allocate sglist\n");
377 return;
378 }
379
380 sc->sc_scb = malloc(sizeof(struct iha_scsi_req_q) * IHA_MAX_SCB,
381 M_DEVBUF, M_NOWAIT);
382 if (sc->sc_scb == NULL) {
383 printf(": cannot allocate SCB\n");
384 return;
385 }
386 memset(sc->sc_scb, 0, sizeof(struct iha_scsi_req_q) * IHA_MAX_SCB);
387
388 for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++) {
389 scb->scb_tagid = i;
390 scb->sgoffset = IHA_SG_SIZE * i;
391 scb->sglist = &sc->sc_sglist[i].sg_element[0];
392 scb->sg_addr =
393 sc->sc_dmamap->dm_segs[0].ds_addr + scb->sgoffset;
394
395 error = bus_dmamap_create(sc->sc_dmat,
396 (IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE, IHA_MAX_SG_ENTRIES,
397 (IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE, 0,
398 BUS_DMA_NOWAIT, &scb->dmap);
399
400 if (error != 0) {
401 printf(": couldn't create SCB DMA map, error = %d\n",
402 error);
403 return;
404 }
405 TAILQ_INSERT_TAIL(&sc->sc_freescb, scb, chain);
406 }
407
408 /* Mask all the interrupts */
409 bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
410
411 /* Stop any I/O and reset the scsi module */
412 tul_reset_dma(sc);
413 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSMOD);
414
415 /* Program HBA's SCSI ID */
416 bus_space_write_1(iot, ioh, TUL_SID, sc->sc_id << 4);
417
418 /*
419 * Configure the channel as requested by the NVRAM settings read
420 * by tul_read_eeprom() above.
421 */
422
423 sc->sc_sconf1 = SCONFIG0DEFAULT;
424 if ((conf->config1 & CFG_EN_PAR) != 0)
425 sc->sc_sconf1 |= SPCHK;
426 bus_space_write_1(iot, ioh, TUL_SCONFIG0, sc->sc_sconf1);
427
428 /* set selection time out 250 ms */
429 bus_space_write_1(iot, ioh, TUL_STIMO, STIMO_250MS);
430
431 /* Enable desired SCSI termination configuration read from eeprom */
432 reg = 0;
433 if (conf->config1 & CFG_ACT_TERM1)
434 reg |= ENTMW;
435 if (conf->config1 & CFG_ACT_TERM2)
436 reg |= ENTM;
437 bus_space_write_1(iot, ioh, TUL_DCTRL0, reg);
438
439 reg = bus_space_read_1(iot, ioh, TUL_GCTRL1) & ~ATDEN;
440 if (conf->config1 & CFG_AUTO_TERM)
441 reg |= ATDEN;
442 bus_space_write_1(iot, ioh, TUL_GCTRL1, reg);
443
444 for (i = 0; i < IHA_MAX_TARGETS / 2; i++) {
445 sc->sc_tcs[i * 2 ].flags = EEP_LBYTE(conf->tflags[i]);
446 sc->sc_tcs[i * 2 + 1].flags = EEP_HBYTE(conf->tflags[i]);
447 tul_reset_tcs(&sc->sc_tcs[i * 2 ], sc->sc_sconf1);
448 tul_reset_tcs(&sc->sc_tcs[i * 2 + 1], sc->sc_sconf1);
449 }
450
451 tul_reset_chip(sc);
452 bus_space_write_1(iot, ioh, TUL_SIEN, ALL_INTERRUPTS);
453
454 /*
455 * fill in the adapter.
456 */
457 sc->sc_adapter.adapt_dev = &sc->sc_dev;
458 sc->sc_adapter.adapt_nchannels = 1;
459 sc->sc_adapter.adapt_openings = IHA_MAX_SCB;
460 sc->sc_adapter.adapt_max_periph = IHA_MAX_SCB;
461 sc->sc_adapter.adapt_ioctl = NULL;
462 sc->sc_adapter.adapt_minphys = iha_minphys;
463 sc->sc_adapter.adapt_request = iha_scsipi_request;
464
465 /*
466 * fill in the channel.
467 */
468 sc->sc_channel.chan_adapter = &sc->sc_adapter;
469 sc->sc_channel.chan_bustype = &scsi_bustype;
470 sc->sc_channel.chan_channel = 0;
471 sc->sc_channel.chan_ntargets = CFG_TARGET(conf->config2);
472 sc->sc_channel.chan_nluns = 8;
473 sc->sc_channel.chan_id = sc->sc_id;
474
475 /*
476 * Now try to attach all the sub devices.
477 */
478 config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
479 }
480
481 /*
482 * iha_minphys - reduce bp->b_bcount to something less than
483 * or equal to the largest I/O possible through
484 * the adapter. Called from higher layers
485 * via sc->sc_adapter.scsi_minphys.
486 */
487 static void
488 iha_minphys(bp)
489 struct buf *bp;
490 {
491 if (bp->b_bcount > ((IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE))
492 bp->b_bcount = ((IHA_MAX_SG_ENTRIES - 1) * PAGE_SIZE);
493
494 minphys(bp);
495 }
496
497 /*
498 * tul_reset_dma - abort any active DMA xfer, reset tulip FIFO.
499 */
500 static void
501 tul_reset_dma(sc)
502 struct iha_softc *sc;
503 {
504 bus_space_tag_t iot = sc->sc_iot;
505 bus_space_handle_t ioh = sc->sc_ioh;
506
507 if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
508 /* if DMA xfer is pending, abort DMA xfer */
509 bus_space_write_1(iot, ioh, TUL_DCMD, ABTXFR);
510 /* wait Abort DMA xfer done */
511 while ((bus_space_read_1(iot, ioh, TUL_ISTUS0) & DABT) == 0)
512 ;
513 }
514
515 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
516 }
517
518 /*
519 * tul_append_free_scb - append the supplied SCB to the tail of the
520 * sc_freescb queue after clearing and resetting
521 * everything possible.
522 */
523 static void
524 tul_append_free_scb(sc, scb)
525 struct iha_softc *sc;
526 struct iha_scsi_req_q *scb;
527 {
528 int s;
529
530 s = splbio();
531
532 if (scb == sc->sc_actscb)
533 sc->sc_actscb = NULL;
534
535 scb->status = STATUS_QUEUED;
536 scb->ha_stat = HOST_OK;
537 scb->ta_stat = SCSI_OK;
538
539 scb->nextstat = 0;
540 scb->sg_index = 0;
541 scb->sg_max = 0;
542 scb->flags = 0;
543 scb->target = 0;
544 scb->lun = 0;
545 scb->buflen = 0;
546 scb->sg_size = 0;
547 scb->cmdlen = 0;
548 scb->scb_id = 0;
549 scb->scb_tagmsg = 0;
550 scb->timeout = 0;
551 scb->bufaddr = 0;
552
553 scb->xs = NULL;
554 scb->tcs = NULL;
555
556 memset(scb->cmd, 0, sizeof(scb->cmd));
557 memset(scb->sglist, 0, sizeof(scb->sglist));
558
559 /*
560 * scb_tagid, sg_addr, sglist
561 * SCB_SensePtr are set at initialization
562 * and never change
563 */
564
565 TAILQ_INSERT_TAIL(&sc->sc_freescb, scb, chain);
566
567 splx(s);
568 }
569
570 static __inline void
571 tul_append_pend_scb(sc, scb)
572 struct iha_softc *sc;
573 struct iha_scsi_req_q *scb;
574 {
575 /* ASSUMPTION: only called within a splbio()/splx() pair */
576
577 if (scb == sc->sc_actscb)
578 sc->sc_actscb = NULL;
579
580 scb->status = STATUS_QUEUED;
581
582 TAILQ_INSERT_TAIL(&sc->sc_pendscb, scb, chain);
583 }
584
585 static __inline void
586 tul_push_pend_scb(sc, scb)
587 struct iha_softc *sc;
588 struct iha_scsi_req_q *scb;
589 {
590 int s;
591
592 s = splbio();
593
594 if (scb == sc->sc_actscb)
595 sc->sc_actscb = NULL;
596
597 scb->status = STATUS_QUEUED;
598
599 TAILQ_INSERT_HEAD(&sc->sc_pendscb, scb, chain);
600
601 splx(s);
602 }
603
604 /*
605 * tul_find_pend_scb - scan the pending queue for a SCB that can be
606 * processed immediately. Return NULL if none found
607 * and a pointer to the SCB if one is found. If there
608 * is an active SCB, return NULL!
609 */
610 static struct iha_scsi_req_q *
611 tul_find_pend_scb(sc)
612 struct iha_softc *sc;
613 {
614 struct iha_scsi_req_q *scb;
615 struct tcs *tcs;
616 int s;
617
618 s = splbio();
619
620 if (sc->sc_actscb != NULL)
621 scb = NULL;
622
623 else
624 TAILQ_FOREACH(scb, &sc->sc_pendscb, chain) {
625 if ((scb->flags & XS_CTL_RESET) != 0)
626 /* ALWAYS willing to reset a device */
627 break;
628
629 tcs = scb->tcs;
630
631 if ((scb->scb_tagmsg) != 0) {
632 /*
633 * A Tagged I/O. OK to start If no
634 * non-tagged I/O is active on the same
635 * target
636 */
637 if (tcs->ntagscb == NULL)
638 break;
639
640 } else if (scb->cmd[0] == REQUEST_SENSE) {
641 /*
642 * OK to do a non-tagged request sense
643 * even if a non-tagged I/O has been
644 * started, 'cuz we don't allow any
645 * disconnect during a request sense op
646 */
647 break;
648
649 } else if (tcs->tagcnt == 0) {
650 /*
651 * No tagged I/O active on this target,
652 * ok to start a non-tagged one if one
653 * is not already active
654 */
655 if (tcs->ntagscb == NULL)
656 break;
657 }
658 }
659
660 splx(s);
661
662 return (scb);
663 }
664
665 /*
666 * tul_del_pend_scb - remove scb from sc_pendscb
667 */
668 static __inline void
669 tul_del_pend_scb(sc, scb)
670 struct iha_softc *sc;
671 struct iha_scsi_req_q *scb;
672 {
673 int s;
674
675 s = splbio();
676
677 TAILQ_REMOVE(&sc->sc_pendscb, scb, chain);
678
679 splx(s);
680 }
681
682 static __inline void
683 tul_mark_busy_scb(scb)
684 struct iha_scsi_req_q *scb;
685 {
686 int s;
687
688 s = splbio();
689
690 scb->status = STATUS_BUSY;
691
692 if (scb->scb_tagmsg == 0)
693 scb->tcs->ntagscb = scb;
694 else
695 scb->tcs->tagcnt++;
696
697 splx(s);
698 }
699
700 static void
701 tul_append_done_scb(sc, scb, hastat)
702 struct iha_softc *sc;
703 struct iha_scsi_req_q *scb;
704 u_int8_t hastat;
705 {
706 struct tcs *tcs;
707 int s;
708
709 s = splbio();
710
711 if (scb->xs != NULL)
712 callout_stop(&scb->xs->xs_callout);
713
714 if (scb == sc->sc_actscb)
715 sc->sc_actscb = NULL;
716
717 tcs = scb->tcs;
718
719 if (scb->scb_tagmsg != 0) {
720 if (tcs->tagcnt)
721 tcs->tagcnt--;
722 } else if (tcs->ntagscb == scb)
723 tcs->ntagscb = NULL;
724
725 scb->status = STATUS_QUEUED;
726 scb->ha_stat = hastat;
727
728 TAILQ_INSERT_TAIL(&sc->sc_donescb, scb, chain);
729
730 splx(s);
731 }
732
733 static __inline struct iha_scsi_req_q *
734 tul_pop_done_scb(sc)
735 struct iha_softc *sc;
736 {
737 struct iha_scsi_req_q *scb;
738 int s;
739
740 s = splbio();
741
742 scb = TAILQ_FIRST(&sc->sc_donescb);
743
744 if (scb != NULL) {
745 scb->status = STATUS_RENT;
746 TAILQ_REMOVE(&sc->sc_donescb, scb, chain);
747 }
748
749 splx(s);
750
751 return (scb);
752 }
753
754 /*
755 * tul_abort_xs - find the SCB associated with the supplied xs and
756 * stop all processing on it, moving it to the done
757 * queue with the supplied host status value.
758 */
759 static void
760 tul_abort_xs(sc, xs, hastat)
761 struct iha_softc *sc;
762 struct scsipi_xfer *xs;
763 u_int8_t hastat;
764 {
765 struct iha_scsi_req_q *scb;
766 int i, s;
767
768 s = splbio();
769
770 /* Check the pending queue for the SCB pointing to xs */
771
772 TAILQ_FOREACH(scb, &sc->sc_pendscb, chain)
773 if (scb->xs == xs) {
774 tul_del_pend_scb(sc, scb);
775 tul_append_done_scb(sc, scb, hastat);
776 splx(s);
777 return;
778 }
779
780 /*
781 * If that didn't work, check all BUSY/SELECTING SCB's for one
782 * pointing to xs
783 */
784
785 for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
786 switch (scb->status) {
787 case STATUS_BUSY:
788 case STATUS_SELECT:
789 if (scb->xs == xs) {
790 tul_append_done_scb(sc, scb, hastat);
791 splx(s);
792 return;
793 }
794 break;
795 default:
796 break;
797 }
798
799 splx(s);
800 }
801
802 /*
803 * tul_bad_seq - a SCSI bus phase was encountered out of the
804 * correct/expected sequence. Reset the SCSI bus.
805 */
806 static void
807 tul_bad_seq(sc)
808 struct iha_softc *sc;
809 {
810 struct iha_scsi_req_q *scb = sc->sc_actscb;
811
812 if (scb != NULL)
813 tul_append_done_scb(sc, scb, HOST_BAD_PHAS);
814
815 tul_reset_scsi_bus(sc);
816 tul_reset_chip(sc);
817 }
818
819 /*
820 * tul_push_sense_request - obtain auto sense data by pushing the
821 * SCB needing it back onto the pending
822 * queue with a REQUEST_SENSE CDB.
823 */
824 static int
825 tul_push_sense_request(sc, scb)
826 struct iha_softc *sc;
827 struct iha_scsi_req_q *scb;
828 {
829 struct scsipi_xfer *xs = scb->xs;
830 struct scsipi_periph *periph = xs->xs_periph;
831 struct scsipi_sense *ss = (struct scsipi_sense *)scb->cmd;
832 int lun = periph->periph_lun;
833 int err;
834
835 ss->opcode = REQUEST_SENSE;
836 ss->byte2 = lun << SCSI_CMD_LUN_SHIFT;
837 ss->unused[0] = ss->unused[1] = 0;
838 ss->length = sizeof(struct scsipi_sense_data);
839 ss->control = 0;
840
841 scb->flags &= ~(FLAG_SG | XS_CTL_DATA_OUT);
842 scb->flags |= FLAG_RSENS | XS_CTL_DATA_IN;
843
844 scb->scb_id &= ~MSG_IDENTIFY_DISCFLAG;
845
846 scb->scb_tagmsg = 0;
847 scb->ta_stat = SCSI_OK;
848
849 scb->cmdlen = sizeof(struct scsipi_sense);
850 scb->buflen = ss->length;
851
852 err = bus_dmamap_load(sc->sc_dmat, scb->dmap,
853 &xs->sense.scsi_sense, scb->buflen, NULL, BUS_DMA_NOWAIT);
854 if (err != 0) {
855 printf("iha_push_sense_request: cannot bus_dmamap_load()\n");
856 xs->error = XS_DRIVER_STUFFUP;
857 return 1;
858 }
859 bus_dmamap_sync(sc->sc_dmat, scb->dmap,
860 0, scb->buflen, BUS_DMASYNC_PREREAD);
861
862 /* XXX What about queued command? */
863 tul_exec_scb(sc, scb);
864
865 return 0;
866 }
867
868 /*
869 * tul_main - process the active SCB, taking one off pending and making it
870 * active if necessary, and any done SCB's created as
871 * a result until there are no interrupts pending and no pending
872 * SCB's that can be started.
873 */
874 static void
875 tul_main(sc)
876 struct iha_softc *sc;
877 {
878 bus_space_tag_t iot = sc->sc_iot;
879 bus_space_handle_t ioh =sc->sc_ioh;
880 struct iha_scsi_req_q *scb;
881
882 for (;;) {
883 tul_scsi(sc);
884
885 while ((scb = tul_pop_done_scb(sc)) != NULL)
886 tul_done_scb(sc, scb);
887
888 /*
889 * If there are no interrupts pending, or we can't start
890 * a pending sc, break out of the for(;;). Otherwise
891 * continue the good work with another call to
892 * tul_scsi().
893 */
894 if (((bus_space_read_1(iot, ioh, TUL_STAT0) & INTPD) == 0)
895 && (tul_find_pend_scb(sc) == NULL))
896 break;
897 }
898 }
899
900 /*
901 * tul_scsi - service any outstanding interrupts. If there are none, try to
902 * start another SCB currently in the pending queue.
903 */
904 static void
905 tul_scsi(sc)
906 struct iha_softc *sc;
907 {
908 bus_space_tag_t iot = sc->sc_iot;
909 bus_space_handle_t ioh = sc->sc_ioh;
910 struct iha_scsi_req_q *scb;
911 struct tcs *tcs;
912 u_int8_t stat;
913
914 /* service pending interrupts asap */
915
916 stat = bus_space_read_1(iot, ioh, TUL_STAT0);
917 if ((stat & INTPD) != 0) {
918 sc->sc_status0 = stat;
919 sc->sc_status1 = bus_space_read_1(iot, ioh, TUL_STAT1);
920 sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
921
922 sc->sc_phase = sc->sc_status0 & PH_MASK;
923
924 if ((sc->sc_sistat & SRSTD) != 0) {
925 tul_reset_scsi_bus(sc);
926 return;
927 }
928
929 if ((sc->sc_sistat & RSELED) != 0) {
930 tul_resel(sc);
931 return;
932 }
933
934 if ((sc->sc_sistat & (STIMEO | DISCD)) != 0) {
935 tul_busfree(sc);
936 return;
937 }
938
939 if ((sc->sc_sistat & (SCMDN | SBSRV)) != 0) {
940 tul_next_state(sc);
941 return;
942 }
943
944 if ((sc->sc_sistat & SELED) != 0)
945 tul_set_ssig(sc, 0, 0);
946 }
947
948 /*
949 * There were no interrupts pending which required action elsewhere, so
950 * see if it is possible to start the selection phase on a pending SCB
951 */
952 if ((scb = tul_find_pend_scb(sc)) == NULL)
953 return;
954
955 tcs = scb->tcs;
956
957 /* program HBA's SCSI ID & target SCSI ID */
958 bus_space_write_1(iot, ioh, TUL_SID, (sc->sc_id << 4) | scb->target);
959
960 if ((scb->flags & XS_CTL_RESET) == 0) {
961 bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
962
963 if ((tcs->flags & FLAG_NO_NEG_SYNC) == 0 ||
964 (tcs->flags & FLAG_NO_NEG_WIDE) == 0)
965 tul_select(sc, scb, SELATNSTOP);
966
967 else if (scb->scb_tagmsg != 0)
968 tul_select(sc, scb, SEL_ATN3);
969
970 else
971 tul_select(sc, scb, SEL_ATN);
972
973 } else {
974 tul_select(sc, scb, SELATNSTOP);
975 scb->nextstat = 8;
976 }
977
978 if ((scb->flags & XS_CTL_POLL) != 0) {
979 for (; scb->timeout > 0; scb->timeout--) {
980 if (tul_wait(sc, NO_OP) == -1)
981 break;
982 if (tul_next_state(sc) == -1)
983 break;
984 delay(1000); /* Only happens in boot, so it's ok */
985 }
986
987 /*
988 * Since done queue processing not done until AFTER this
989 * function returns, scb is on the done queue, not
990 * the free queue at this point and still has valid data
991 *
992 * Conversely, xs->error has not been set yet
993 */
994 if (scb->timeout == 0)
995 tul_timeout(scb);
996 }
997 }
998
999 /*
1000 * tul_data_over_run - return HOST_OK for all SCSI opcodes where BufLen
1001 * is an 'Allocation Length'. All other SCSI opcodes
1002 * get HOST_DO_DU as they SHOULD have xferred all the
1003 * data requested.
1004 *
1005 * The list of opcodes using 'Allocation Length' was
1006 * found by scanning all the SCSI-3 T10 drafts. See
1007 * www.t10.org for the curious with a .pdf reader.
1008 */
1009 static u_int8_t
1010 tul_data_over_run(scb)
1011 struct iha_scsi_req_q *scb;
1012 {
1013 switch (scb->cmd[0]) {
1014 case 0x03: /* Request Sense SPC-2 */
1015 case 0x12: /* Inquiry SPC-2 */
1016 case 0x1a: /* Mode Sense (6 byte version) SPC-2 */
1017 case 0x1c: /* Receive Diagnostic Results SPC-2 */
1018 case 0x23: /* Read Format Capacities MMC-2 */
1019 case 0x29: /* Read Generation SBC */
1020 case 0x34: /* Read Position SSC-2 */
1021 case 0x37: /* Read Defect Data SBC */
1022 case 0x3c: /* Read Buffer SPC-2 */
1023 case 0x42: /* Read Sub Channel MMC-2 */
1024 case 0x43: /* Read TOC/PMA/ATIP MMC */
1025
1026 /* XXX - 2 with same opcode of 0x44? */
1027 case 0x44: /* Read Header/Read Density Suprt MMC/SSC*/
1028
1029 case 0x46: /* Get Configuration MMC-2 */
1030 case 0x4a: /* Get Event/Status Notification MMC-2 */
1031 case 0x4d: /* Log Sense SPC-2 */
1032 case 0x51: /* Read Disc Information MMC */
1033 case 0x52: /* Read Track Information MMC */
1034 case 0x59: /* Read Master CUE MMC */
1035 case 0x5a: /* Mode Sense (10 byte version) SPC-2 */
1036 case 0x5c: /* Read Buffer Capacity MMC */
1037 case 0x5e: /* Persistant Reserve In SPC-2 */
1038 case 0x84: /* Receive Copy Results SPC-2 */
1039 case 0xa0: /* Report LUNs SPC-2 */
1040 case 0xa3: /* Various Report requests SBC-2/SCC-2*/
1041 case 0xa4: /* Report Key MMC-2 */
1042 case 0xad: /* Read DVD Structure MMC-2 */
1043 case 0xb4: /* Read Element Status (Attached) SMC */
1044 case 0xb5: /* Request Volume Element Address SMC */
1045 case 0xb7: /* Read Defect Data (12 byte ver.) SBC */
1046 case 0xb8: /* Read Element Status (Independ.) SMC */
1047 case 0xba: /* Report Redundancy SCC-2 */
1048 case 0xbd: /* Mechanism Status MMC */
1049 case 0xbe: /* Report Basic Redundancy SCC-2 */
1050
1051 return (HOST_OK);
1052 break;
1053
1054 default:
1055 return (HOST_DO_DU);
1056 break;
1057 }
1058 }
1059
1060 /*
1061 * tul_next_state - prcess the current SCB as requested in it's
1062 * nextstat member.
1063 */
1064 static int
1065 tul_next_state(sc)
1066 struct iha_softc *sc;
1067 {
1068
1069 if (sc->sc_actscb == NULL)
1070 return (-1);
1071
1072 switch (sc->sc_actscb->nextstat) {
1073 case 1:
1074 if (tul_state_1(sc) == 3)
1075 goto state_3;
1076 break;
1077
1078 case 2:
1079 switch (tul_state_2(sc)) {
1080 case 3:
1081 goto state_3;
1082 case 4:
1083 goto state_4;
1084 default:
1085 break;
1086 }
1087 break;
1088
1089 case 3:
1090 state_3:
1091 if (tul_state_3(sc) == 4)
1092 goto state_4;
1093 break;
1094
1095 case 4:
1096 state_4:
1097 switch (tul_state_4(sc)) {
1098 case 0:
1099 return (0);
1100 case 6:
1101 goto state_6;
1102 default:
1103 break;
1104 }
1105 break;
1106
1107 case 5:
1108 switch (tul_state_5(sc)) {
1109 case 4:
1110 goto state_4;
1111 case 6:
1112 goto state_6;
1113 default:
1114 break;
1115 }
1116 break;
1117
1118 case 6:
1119 state_6:
1120 tul_state_6(sc);
1121 break;
1122
1123 case 8:
1124 tul_state_8(sc);
1125 break;
1126
1127 default:
1128 #ifdef IHA_DEBUG_STATE
1129 printf("[debug] -unknown state: %i-\n",
1130 sc->sc_actscb->nextstat);
1131 #endif
1132 tul_bad_seq(sc);
1133 break;
1134 }
1135
1136 return (-1);
1137 }
1138
1139 /*
1140 * tul_state_1 - selection is complete after a SELATNSTOP. If the target
1141 * has put the bus into MSG_OUT phase start wide/sync
1142 * negotiation. Otherwise clear the FIFO and go to state 3,
1143 * which will send the SCSI CDB to the target.
1144 */
1145 static int
1146 tul_state_1(sc)
1147 struct iha_softc *sc;
1148 {
1149 bus_space_tag_t iot = sc->sc_iot;
1150 bus_space_handle_t ioh = sc->sc_ioh;
1151 struct iha_scsi_req_q *scb = sc->sc_actscb;
1152 struct tcs *tcs;
1153 int flags;
1154
1155 tul_mark_busy_scb(scb);
1156
1157 tcs = scb->tcs;
1158
1159 bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
1160
1161 /*
1162 * If we are in PHASE_MSG_OUT, send
1163 * a) IDENT message (with tags if appropriate)
1164 * b) WDTR if the target is configured to negotiate wide xfers
1165 * ** OR **
1166 * c) SDTR if the target is configured to negotiate sync xfers
1167 * but not wide ones
1168 *
1169 * If we are NOT, then the target is not asking for anything but
1170 * the data/command, so go straight to state 3.
1171 */
1172 if (sc->sc_phase == PHASE_MSG_OUT) {
1173 bus_space_write_1(iot, ioh, TUL_SCTRL1, (ESBUSIN | EHRSL));
1174 bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
1175
1176 if (scb->scb_tagmsg != 0) {
1177 bus_space_write_1(iot, ioh, TUL_SFIFO,
1178 scb->scb_tagmsg);
1179 bus_space_write_1(iot, ioh, TUL_SFIFO,
1180 scb->scb_tagid);
1181 }
1182
1183 flags = tcs->flags;
1184 if ((flags & FLAG_NO_NEG_WIDE) == 0) {
1185 if (tul_msgout_wide(sc) == -1)
1186 return (-1);
1187 } else if ((flags & FLAG_NO_NEG_SYNC) == 0) {
1188 if (tul_msgout_sync(sc) == -1)
1189 return (-1);
1190 }
1191
1192 } else {
1193 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1194 tul_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
1195 }
1196
1197 return (3);
1198 }
1199
1200 /*
1201 * tul_state_2 - selection is complete after a SEL_ATN or SEL_ATN3. If the SCSI
1202 * CDB has already been send, go to state 4 to start the data
1203 * xfer. Otherwise reset the FIFO and go to state 3, sending
1204 * the SCSI CDB.
1205 */
1206 static int
1207 tul_state_2(sc)
1208 struct iha_softc *sc;
1209 {
1210 bus_space_tag_t iot = sc->sc_iot;
1211 bus_space_handle_t ioh = sc->sc_ioh;
1212 struct iha_scsi_req_q *scb = sc->sc_actscb;
1213
1214 tul_mark_busy_scb(scb);
1215
1216 bus_space_write_1(iot, ioh, TUL_SCONFIG0, scb->tcs->sconfig0);
1217
1218 if ((sc->sc_status1 & CPDNE) != 0)
1219 return (4);
1220
1221 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1222
1223 tul_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
1224
1225 return (3);
1226 }
1227
1228 /*
1229 * tul_state_3 - send the SCSI CDB to the target, processing any status
1230 * or other messages received until that is done or
1231 * abandoned.
1232 */
1233 static int
1234 tul_state_3(sc)
1235 struct iha_softc *sc;
1236 {
1237 bus_space_tag_t iot = sc->sc_iot;
1238 bus_space_handle_t ioh = sc->sc_ioh;
1239 struct iha_scsi_req_q *scb = sc->sc_actscb;
1240 int flags;
1241
1242 for (;;) {
1243 switch (sc->sc_phase) {
1244 case PHASE_CMD_OUT:
1245 bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
1246 scb->cmd, scb->cmdlen);
1247 if (tul_wait(sc, XF_FIFO_OUT) == -1)
1248 return (-1);
1249 else if (sc->sc_phase == PHASE_CMD_OUT) {
1250 tul_bad_seq(sc);
1251 return (-1);
1252 } else
1253 return (4);
1254
1255 case PHASE_MSG_IN:
1256 scb->nextstat = 3;
1257 if (tul_msgin(sc) == -1)
1258 return (-1);
1259 break;
1260
1261 case PHASE_STATUS_IN:
1262 if (tul_status_msg(sc) == -1)
1263 return (-1);
1264 break;
1265
1266 case PHASE_MSG_OUT:
1267 flags = scb->tcs->flags;
1268 if ((flags & FLAG_NO_NEG_SYNC) != 0) {
1269 if (tul_msgout(sc, MSG_NOOP) == -1)
1270 return (-1);
1271 } else if (tul_msgout_sync(sc) == -1)
1272 return (-1);
1273 break;
1274
1275 default:
1276 printf("[debug] -s3- bad phase = %d\n", sc->sc_phase);
1277 tul_bad_seq(sc);
1278 return (-1);
1279 }
1280 }
1281 }
1282
1283 /*
1284 * tul_state_4 - start a data xfer. Handle any bus state
1285 * transitions until PHASE_DATA_IN/_OUT
1286 * or the attempt is abandoned. If there is
1287 * no data to xfer, go to state 6 and finish
1288 * processing the current SCB.
1289 */
1290 static int
1291 tul_state_4(sc)
1292 struct iha_softc *sc;
1293 {
1294 struct iha_scsi_req_q *scb = sc->sc_actscb;
1295
1296 if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) ==
1297 (XS_CTL_DATA_IN | XS_CTL_DATA_OUT))
1298 return (6); /* Both dir flags set => NO xfer was requested */
1299
1300 for (;;) {
1301 if (scb->buflen == 0)
1302 return (6);
1303
1304 switch (sc->sc_phase) {
1305 case PHASE_STATUS_IN:
1306 if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT))
1307 != 0)
1308 scb->ha_stat = tul_data_over_run(scb);
1309 if ((tul_status_msg(sc)) == -1)
1310 return (-1);
1311 break;
1312
1313 case PHASE_MSG_IN:
1314 scb->nextstat = 4;
1315 if (tul_msgin(sc) == -1)
1316 return (-1);
1317 break;
1318
1319 case PHASE_MSG_OUT:
1320 if ((sc->sc_status0 & SPERR) != 0) {
1321 scb->buflen = 0;
1322 scb->ha_stat = HOST_SPERR;
1323 if (tul_msgout(sc, MSG_INITIATOR_DET_ERR) == -1)
1324 return (-1);
1325 else
1326 return (6);
1327 } else {
1328 if (tul_msgout(sc, MSG_NOOP) == -1)
1329 return (-1);
1330 }
1331 break;
1332
1333 case PHASE_DATA_IN:
1334 return (tul_xfer_data(sc, scb, XS_CTL_DATA_IN));
1335
1336 case PHASE_DATA_OUT:
1337 return (tul_xfer_data(sc, scb, XS_CTL_DATA_OUT));
1338
1339 default:
1340 tul_bad_seq(sc);
1341 return (-1);
1342 }
1343 }
1344 }
1345
1346 /*
1347 * tul_state_5 - handle the partial or final completion of the current
1348 * data xfer. If DMA is still active stop it. If there is
1349 * more data to xfer, go to state 4 and start the xfer.
1350 * If not go to state 6 and finish the SCB.
1351 */
1352 static int
1353 tul_state_5(sc)
1354 struct iha_softc *sc;
1355 {
1356 bus_space_tag_t iot = sc->sc_iot;
1357 bus_space_handle_t ioh = sc->sc_ioh;
1358 struct iha_scsi_req_q *scb = sc->sc_actscb;
1359 struct iha_sg_element *sg;
1360 u_int32_t cnt;
1361 u_int8_t period, stat;
1362 long xcnt; /* cannot use unsigned!! see code: if (xcnt < 0) */
1363 int i;
1364
1365 cnt = bus_space_read_4(iot, ioh, TUL_STCNT0) & TCNT;
1366
1367 /*
1368 * Stop any pending DMA activity and check for parity error.
1369 */
1370
1371 if ((bus_space_read_1(iot, ioh, TUL_DCMD) & XDIR) != 0) {
1372 /* Input Operation */
1373 if ((sc->sc_status0 & SPERR) != 0)
1374 scb->ha_stat = HOST_SPERR;
1375
1376 if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
1377 bus_space_write_1(iot, ioh, TUL_DCTRL0,
1378 bus_space_read_1(iot, ioh, TUL_DCTRL0) | SXSTP);
1379 while (bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND)
1380 ;
1381 }
1382
1383 } else {
1384 /* Output Operation */
1385 if ((sc->sc_status1 & SXCMP) == 0) {
1386 period = scb->tcs->syncm;
1387 if ((period & PERIOD_WIDE_SCSI) != 0)
1388 cnt += (bus_space_read_1(iot, ioh,
1389 TUL_SFIFOCNT) & FIFOC) * 2;
1390 else
1391 cnt += bus_space_read_1(iot, ioh,
1392 TUL_SFIFOCNT) & FIFOC;
1393 }
1394
1395 if ((bus_space_read_1(iot, ioh, TUL_ISTUS1) & XPEND) != 0) {
1396 bus_space_write_1(iot, ioh, TUL_DCMD, ABTXFR);
1397 do
1398 stat = bus_space_read_1(iot, ioh, TUL_ISTUS0);
1399 while ((stat & DABT) == 0);
1400 }
1401
1402 if ((cnt == 1) && (sc->sc_phase == PHASE_DATA_OUT)) {
1403 if (tul_wait(sc, XF_FIFO_OUT) == -1)
1404 return (-1);
1405 cnt = 0;
1406
1407 } else if ((sc->sc_status1 & SXCMP) == 0)
1408 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1409 }
1410
1411 if (cnt == 0) {
1412 scb->buflen = 0;
1413 return (6);
1414 }
1415
1416 /* Update active data pointer and restart the I/O at the new point */
1417
1418 xcnt = scb->buflen - cnt; /* xcnt == bytes xferred */
1419 scb->buflen = cnt; /* cnt == bytes left */
1420
1421 if ((scb->flags & FLAG_SG) != 0) {
1422 sg = &scb->sglist[scb->sg_index];
1423 for (i = scb->sg_index; i < scb->sg_max; sg++, i++) {
1424 xcnt -= le32toh(sg->sg_len);
1425 if (xcnt < 0) {
1426 xcnt += le32toh(sg->sg_len);
1427
1428 sg->sg_addr =
1429 htole32(le32toh(sg->sg_addr) + xcnt);
1430 sg->sg_len =
1431 htole32(le32toh(sg->sg_len) - xcnt);
1432 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
1433 scb->sgoffset, IHA_SG_SIZE,
1434 BUS_DMASYNC_PREWRITE);
1435
1436 scb->bufaddr += (i - scb->sg_index) *
1437 sizeof(struct iha_sg_element);
1438 scb->sg_size = scb->sg_max - i;
1439 scb->sg_index = i;
1440
1441 return (4);
1442 }
1443 }
1444 return (6);
1445
1446 } else
1447 scb->bufaddr += xcnt;
1448
1449 return (4);
1450 }
1451
1452 /*
1453 * tul_state_6 - finish off the active scb (may require several
1454 * iterations if PHASE_MSG_IN) and return -1 to indicate
1455 * the bus is free.
1456 */
1457 static int
1458 tul_state_6(sc)
1459 struct iha_softc *sc;
1460 {
1461
1462 for (;;) {
1463 switch (sc->sc_phase) {
1464 case PHASE_STATUS_IN:
1465 if (tul_status_msg(sc) == -1)
1466 return (-1);
1467 break;
1468
1469 case PHASE_MSG_IN:
1470 sc->sc_actscb->nextstat = 6;
1471 if ((tul_msgin(sc)) == -1)
1472 return (-1);
1473 break;
1474
1475 case PHASE_MSG_OUT:
1476 if ((tul_msgout(sc, MSG_NOOP)) == -1)
1477 return (-1);
1478 break;
1479
1480 case PHASE_DATA_IN:
1481 if (tul_xpad_in(sc) == -1)
1482 return (-1);
1483 break;
1484
1485 case PHASE_DATA_OUT:
1486 if (tul_xpad_out(sc) == -1)
1487 return (-1);
1488 break;
1489
1490 default:
1491 tul_bad_seq(sc);
1492 return (-1);
1493 }
1494 }
1495 }
1496
1497 /*
1498 * tul_state_8 - reset the active device and all busy SCBs using it
1499 */
1500 static int
1501 tul_state_8(sc)
1502 struct iha_softc *sc;
1503 {
1504 bus_space_tag_t iot = sc->sc_iot;
1505 bus_space_handle_t ioh = sc->sc_ioh;
1506 struct iha_scsi_req_q *scb;
1507 int i;
1508 u_int8_t tar;
1509
1510 if (sc->sc_phase == PHASE_MSG_OUT) {
1511 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_BUS_DEV_RESET);
1512
1513 scb = sc->sc_actscb;
1514
1515 /* This SCB finished correctly -- resetting the device */
1516 tul_append_done_scb(sc, scb, HOST_OK);
1517
1518 tul_reset_tcs(scb->tcs, sc->sc_sconf1);
1519
1520 tar = scb->target;
1521 for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
1522 if (scb->target == tar)
1523 switch (scb->status) {
1524 case STATUS_BUSY:
1525 tul_append_done_scb(sc,
1526 scb, HOST_DEV_RST);
1527 break;
1528
1529 case STATUS_SELECT:
1530 tul_push_pend_scb(sc, scb);
1531 break;
1532
1533 default:
1534 break;
1535 }
1536
1537 sc->sc_flags |= FLAG_EXPECT_DISC;
1538
1539 if (tul_wait(sc, XF_FIFO_OUT) == -1)
1540 return (-1);
1541 }
1542
1543 tul_bad_seq(sc);
1544 return (-1);
1545 }
1546
1547 /*
1548 * tul_xfer_data - initiate the DMA xfer of the data
1549 */
1550 static int
1551 tul_xfer_data(sc, scb, direction)
1552 struct iha_softc *sc;
1553 struct iha_scsi_req_q *scb;
1554 int direction;
1555 {
1556 bus_space_tag_t iot = sc->sc_iot;
1557 bus_space_handle_t ioh = sc->sc_ioh;
1558 u_int32_t xferlen;
1559 u_int8_t xfertype;
1560
1561 if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) != direction)
1562 return (6); /* wrong direction, abandon I/O */
1563
1564 bus_space_write_4(iot, ioh, TUL_STCNT0, scb->buflen);
1565
1566 if ((scb->flags & FLAG_SG) == 0) {
1567 xferlen = scb->buflen;
1568 xfertype = (direction == XS_CTL_DATA_IN) ? ST_X_IN : ST_X_OUT;
1569
1570 } else {
1571 xferlen = scb->sg_size * sizeof(struct iha_sg_element);
1572 xfertype = (direction == XS_CTL_DATA_IN) ? ST_SG_IN : ST_SG_OUT;
1573 }
1574
1575 bus_space_write_4(iot, ioh, TUL_DXC, xferlen);
1576 bus_space_write_4(iot, ioh, TUL_DXPA, scb->bufaddr);
1577 bus_space_write_1(iot, ioh, TUL_DCMD, xfertype);
1578
1579 bus_space_write_1(iot, ioh, TUL_SCMD,
1580 (direction == XS_CTL_DATA_IN) ? XF_DMA_IN : XF_DMA_OUT);
1581
1582 scb->nextstat = 5;
1583
1584 return (0);
1585 }
1586
1587 static int
1588 tul_xpad_in(sc)
1589 struct iha_softc *sc;
1590 {
1591 bus_space_tag_t iot = sc->sc_iot;
1592 bus_space_handle_t ioh = sc->sc_ioh;
1593 struct iha_scsi_req_q *scb = sc->sc_actscb;
1594
1595 if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) != 0)
1596 scb->ha_stat = HOST_DO_DU;
1597
1598 for (;;) {
1599 if ((scb->tcs->syncm & PERIOD_WIDE_SCSI) != 0)
1600 bus_space_write_4(iot, ioh, TUL_STCNT0, 2);
1601 else
1602 bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
1603
1604 switch (tul_wait(sc, XF_FIFO_IN)) {
1605 case -1:
1606 return (-1);
1607
1608 case PHASE_DATA_IN:
1609 bus_space_read_1(iot, ioh, TUL_SFIFO);
1610 break;
1611
1612 default:
1613 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1614 return (6);
1615 }
1616 }
1617 }
1618
1619 static int
1620 tul_xpad_out(sc)
1621 struct iha_softc *sc;
1622 {
1623 bus_space_tag_t iot = sc->sc_iot;
1624 bus_space_handle_t ioh = sc->sc_ioh;
1625 struct iha_scsi_req_q *scb = sc->sc_actscb;
1626
1627 if ((scb->flags & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) != 0)
1628 scb->ha_stat = HOST_DO_DU;
1629
1630 for (;;) {
1631 if ((scb->tcs->syncm & PERIOD_WIDE_SCSI) != 0)
1632 bus_space_write_4(iot, ioh, TUL_STCNT0, 2);
1633 else
1634 bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
1635
1636 bus_space_write_1(iot, ioh, TUL_SFIFO, 0);
1637
1638 switch (tul_wait(sc, XF_FIFO_OUT)) {
1639 case -1:
1640 return (-1);
1641
1642 case PHASE_DATA_OUT:
1643 break;
1644
1645 default:
1646 /* Disable wide CPU to allow read 16 bits */
1647 bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
1648 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1649 return (6);
1650 }
1651 }
1652 }
1653
1654 static int
1655 tul_status_msg(sc)
1656 struct iha_softc *sc;
1657 {
1658 bus_space_tag_t iot = sc->sc_iot;
1659 bus_space_handle_t ioh = sc->sc_ioh;
1660 struct iha_scsi_req_q *scb;
1661 u_int8_t msg;
1662 int phase;
1663
1664 if ((phase = tul_wait(sc, CMD_COMP)) == -1)
1665 return (-1);
1666
1667 scb = sc->sc_actscb;
1668
1669 scb->ta_stat = bus_space_read_1(iot, ioh, TUL_SFIFO);
1670
1671 if (phase == PHASE_MSG_OUT) {
1672 if ((sc->sc_status0 & SPERR) == 0)
1673 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_NOOP);
1674 else
1675 bus_space_write_1(iot, ioh, TUL_SFIFO,
1676 MSG_PARITY_ERROR);
1677
1678 return (tul_wait(sc, XF_FIFO_OUT));
1679
1680 } else if (phase == PHASE_MSG_IN) {
1681 msg = bus_space_read_1(iot, ioh, TUL_SFIFO);
1682
1683 if ((sc->sc_status0 & SPERR) != 0)
1684 switch (tul_wait(sc, MSG_ACCEPT)) {
1685 case -1:
1686 return (-1);
1687 case PHASE_MSG_OUT:
1688 bus_space_write_1(iot, ioh, TUL_SFIFO,
1689 MSG_PARITY_ERROR);
1690 return (tul_wait(sc, XF_FIFO_OUT));
1691 default:
1692 tul_bad_seq(sc);
1693 return (-1);
1694 }
1695
1696 if (msg == MSG_CMDCOMPLETE) {
1697 if ((scb->ta_stat &
1698 (SCSI_INTERM | SCSI_BUSY)) == SCSI_INTERM) {
1699 tul_bad_seq(sc);
1700 return (-1);
1701 }
1702 sc->sc_flags |= FLAG_EXPECT_DONE_DISC;
1703 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1704 return (tul_wait(sc, MSG_ACCEPT));
1705 }
1706
1707 if ((msg == MSG_LINK_CMD_COMPLETE)
1708 || (msg == MSG_LINK_CMD_COMPLETEF)) {
1709 if ((scb->ta_stat &
1710 (SCSI_INTERM | SCSI_BUSY)) == SCSI_INTERM)
1711 return (tul_wait(sc, MSG_ACCEPT));
1712 }
1713 }
1714
1715 tul_bad_seq(sc);
1716 return (-1);
1717 }
1718
1719 /*
1720 * tul_busfree - SCSI bus free detected as a result of a TIMEOUT or
1721 * DISCONNECT interrupt. Reset the tulip FIFO and
1722 * SCONFIG0 and enable hardware reselect. Move any active
1723 * SCB to sc_donescb list. Return an appropriate host status
1724 * if an I/O was active.
1725 */
1726 static void
1727 tul_busfree(sc)
1728 struct iha_softc *sc;
1729 {
1730 bus_space_tag_t iot = sc->sc_iot;
1731 bus_space_handle_t ioh = sc->sc_ioh;
1732 struct iha_scsi_req_q *scb;
1733
1734 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1735 bus_space_write_1(iot, ioh, TUL_SCONFIG0, SCONFIG0DEFAULT);
1736 bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
1737
1738 scb = sc->sc_actscb;
1739
1740 if (scb != NULL) {
1741 if (scb->status == STATUS_SELECT)
1742 /* selection timeout */
1743 tul_append_done_scb(sc, scb, HOST_SEL_TOUT);
1744 else
1745 /* Unexpected bus free */
1746 tul_append_done_scb(sc, scb, HOST_BAD_PHAS);
1747 }
1748 }
1749
1750 static void
1751 tul_reset_scsi_bus(sc)
1752 struct iha_softc *sc;
1753 {
1754 struct iha_scsi_req_q *scb;
1755 struct tcs *tcs;
1756 int i, s;
1757
1758 s = splbio();
1759
1760 tul_reset_dma(sc);
1761
1762 for (i = 0, scb = sc->sc_scb; i < IHA_MAX_SCB; i++, scb++)
1763 switch (scb->status) {
1764 case STATUS_BUSY:
1765 tul_append_done_scb(sc, scb, HOST_SCSI_RST);
1766 break;
1767
1768 case STATUS_SELECT:
1769 tul_push_pend_scb(sc, scb);
1770 break;
1771
1772 default:
1773 break;
1774 }
1775
1776 for (i = 0, tcs = sc->sc_tcs; i < IHA_MAX_TARGETS; i++, tcs++)
1777 tul_reset_tcs(tcs, sc->sc_sconf1);
1778
1779 splx(s);
1780 }
1781
1782 /*
1783 * tul_resel - handle a detected SCSI bus reselection request.
1784 */
1785 static int
1786 tul_resel(sc)
1787 struct iha_softc *sc;
1788 {
1789 bus_space_tag_t iot = sc->sc_iot;
1790 bus_space_handle_t ioh = sc->sc_ioh;
1791 struct iha_scsi_req_q *scb;
1792 struct tcs *tcs;
1793 u_int8_t tag, target, lun, msg, abortmsg;
1794
1795 if (sc->sc_actscb != NULL) {
1796 if ((sc->sc_actscb->status == STATUS_SELECT))
1797 /* sets ActScb to NULL */
1798 tul_push_pend_scb(sc, sc->sc_actscb);
1799 else
1800 sc->sc_actscb = NULL;
1801 }
1802
1803 target = bus_space_read_1(iot, ioh, TUL_SBID);
1804 lun = bus_space_read_1(iot, ioh, TUL_SALVC) & MSG_IDENTIFY_LUNMASK;
1805
1806 tcs = &sc->sc_tcs[target];
1807
1808 bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
1809 bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
1810
1811 abortmsg = MSG_ABORT; /* until a valid tag has been obtained */
1812
1813 if (tcs->ntagscb != NULL)
1814 /* There is a non-tagged I/O active on the target */
1815 scb = tcs->ntagscb;
1816
1817 else {
1818 /*
1819 * Since there is no active non-tagged operation
1820 * read the tag type, the tag itself, and find
1821 * the appropriate scb by indexing sc_scb with
1822 * the tag.
1823 */
1824
1825 switch (tul_wait(sc, MSG_ACCEPT)) {
1826 case -1:
1827 return (-1);
1828 case PHASE_MSG_IN:
1829 bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
1830 if ((tul_wait(sc, XF_FIFO_IN)) == -1)
1831 return (-1);
1832 break;
1833 default:
1834 goto abort;
1835 }
1836
1837 msg = bus_space_read_1(iot, ioh, TUL_SFIFO); /* Read Tag Msg */
1838
1839 if ((msg < MSG_SIMPLE_Q_TAG) || (msg > MSG_ORDERED_Q_TAG))
1840 goto abort;
1841
1842 switch (tul_wait(sc, MSG_ACCEPT)) {
1843 case -1:
1844 return (-1);
1845 case PHASE_MSG_IN:
1846 bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
1847 if ((tul_wait(sc, XF_FIFO_IN)) == -1)
1848 return (-1);
1849 break;
1850 default:
1851 goto abort;
1852 }
1853
1854 tag = bus_space_read_1(iot, ioh, TUL_SFIFO); /* Read Tag ID */
1855 scb = &sc->sc_scb[tag];
1856
1857 abortmsg = MSG_ABORT_TAG; /* Now that we have valdid tag! */
1858 }
1859
1860 if ((scb->target != target)
1861 || (scb->lun != lun)
1862 || (scb->status != STATUS_BUSY)) {
1863 abort:
1864 tul_msgout_abort(sc, abortmsg);
1865 return (-1);
1866 }
1867
1868 sc->sc_actscb = scb;
1869
1870 if (tul_wait(sc, MSG_ACCEPT) == -1)
1871 return (-1);
1872
1873 return (tul_next_state(sc));
1874 }
1875
1876 static int
1877 tul_msgin(sc)
1878 struct iha_softc *sc;
1879 {
1880 bus_space_tag_t iot = sc->sc_iot;
1881 bus_space_handle_t ioh = sc->sc_ioh;
1882 int flags;
1883 int phase;
1884 u_int8_t msg;
1885
1886 for (;;) {
1887 if ((bus_space_read_1(iot, ioh, TUL_SFIFOCNT) & FIFOC) > 0)
1888 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1889
1890 bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
1891
1892 phase = tul_wait(sc, XF_FIFO_IN);
1893 msg = bus_space_read_1(iot, ioh, TUL_SFIFO);
1894
1895 switch (msg) {
1896 case MSG_DISCONNECT:
1897 sc->sc_flags |= FLAG_EXPECT_DISC;
1898 if (tul_wait(sc, MSG_ACCEPT) != -1)
1899 tul_bad_seq(sc);
1900 phase = -1;
1901 break;
1902 case MSG_SAVEDATAPOINTER:
1903 case MSG_RESTOREPOINTERS:
1904 case MSG_NOOP:
1905 phase = tul_wait(sc, MSG_ACCEPT);
1906 break;
1907 case MSG_MESSAGE_REJECT:
1908 /* XXX - need to clear FIFO like other 'Clear ATN'?*/
1909 tul_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
1910 flags = sc->sc_actscb->tcs->flags;
1911 if ((flags & FLAG_NO_NEG_SYNC) == 0)
1912 tul_set_ssig(sc, REQ | BSY | SEL, ATN);
1913 phase = tul_wait(sc, MSG_ACCEPT);
1914 break;
1915 case MSG_EXTENDED:
1916 phase = tul_msgin_extend(sc);
1917 break;
1918 case MSG_IGN_WIDE_RESIDUE:
1919 phase = tul_msgin_ignore_wid_resid(sc);
1920 break;
1921 case MSG_CMDCOMPLETE:
1922 sc->sc_flags |= FLAG_EXPECT_DONE_DISC;
1923 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
1924 phase = tul_wait(sc, MSG_ACCEPT);
1925 if (phase != -1) {
1926 tul_bad_seq(sc);
1927 return (-1);
1928 }
1929 break;
1930 default:
1931 printf("[debug] tul_msgin: bad msg type: %d\n", msg);
1932 phase = tul_msgout_reject(sc);
1933 break;
1934 }
1935
1936 if (phase != PHASE_MSG_IN)
1937 return (phase);
1938 }
1939 /* NOTREACHED */
1940 }
1941
1942 static int
1943 tul_msgin_ignore_wid_resid(sc)
1944 struct iha_softc *sc;
1945 {
1946 bus_space_tag_t iot = sc->sc_iot;
1947 bus_space_handle_t ioh = sc->sc_ioh;
1948 int phase;
1949
1950 phase = tul_wait(sc, MSG_ACCEPT);
1951
1952 if (phase == PHASE_MSG_IN) {
1953 if (tul_wait(sc, XF_FIFO_IN) == -1)
1954 return (-1);
1955
1956 bus_space_write_1(iot, ioh, TUL_SFIFO, 0); /* put pad */
1957 bus_space_read_1(iot, ioh, TUL_SFIFO); /* get IGNORE */
1958 bus_space_read_1(iot, ioh, TUL_SFIFO); /* get pad */
1959
1960 return (tul_wait(sc, MSG_ACCEPT));
1961 }
1962 else
1963 return (phase);
1964 }
1965
1966 static int
1967 tul_msgin_extend(sc)
1968 struct iha_softc *sc;
1969 {
1970 bus_space_tag_t iot = sc->sc_iot;
1971 bus_space_handle_t ioh = sc->sc_ioh;
1972 int flags, i, phase, msglen, msgcode;
1973
1974 /*
1975 * XXX - can we just stop reading and reject, or do we have to
1976 * read all input, discarding the excess, and then reject
1977 */
1978 for (i = 0; i < IHA_MAX_EXTENDED_MSG; i++) {
1979 phase = tul_wait(sc, MSG_ACCEPT);
1980
1981 if (phase != PHASE_MSG_IN)
1982 return (phase);
1983
1984 bus_space_write_4(iot, ioh, TUL_STCNT0, 1);
1985
1986 if (tul_wait(sc, XF_FIFO_IN) == -1)
1987 return (-1);
1988
1989 sc->sc_msg[i] = bus_space_read_1(iot, ioh, TUL_SFIFO);
1990
1991 if (sc->sc_msg[0] == i)
1992 break;
1993 }
1994
1995 msglen = sc->sc_msg[0];
1996 msgcode = sc->sc_msg[1];
1997
1998 if ((msglen == MSG_EXT_SDTR_LEN) && (msgcode == MSG_EXT_SDTR)) {
1999 if (tul_msgin_sync(sc) == 0) {
2000 tul_sync_done(sc);
2001 return (tul_wait(sc, MSG_ACCEPT));
2002 }
2003
2004 tul_set_ssig(sc, REQ | BSY | SEL, ATN);
2005
2006 phase = tul_wait(sc, MSG_ACCEPT);
2007 if (phase != PHASE_MSG_OUT)
2008 return (phase);
2009
2010 /* Clear FIFO for important message - final SYNC offer */
2011 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2012
2013 tul_sync_done(sc); /* This is our final offer */
2014
2015 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
2016 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_SDTR_LEN);
2017 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_SDTR);
2018 bus_space_write_1(iot, ioh, TUL_SFIFO, sc->sc_msg[2]);
2019 bus_space_write_1(iot, ioh, TUL_SFIFO, sc->sc_msg[3]);
2020
2021 } else if ((msglen == MSG_EXT_WDTR_LEN) && (msgcode == MSG_EXT_WDTR)) {
2022
2023 flags = sc->sc_actscb->tcs->flags;
2024
2025 if ((flags & FLAG_NO_WIDE) != 0)
2026 sc->sc_msg[2] = 0; /* Offer async xfers only */
2027
2028 else if (sc->sc_msg[2] > 2) /* BAD MSG: 2 is max value */
2029 return (tul_msgout_reject(sc));
2030
2031 else if (sc->sc_msg[2] == 2) /* a request for 32 bit xfers*/
2032 sc->sc_msg[2] = 1; /* Offer 16 instead */
2033
2034 else {
2035 tul_wdtr_done(sc);
2036 if ((flags & FLAG_NO_NEG_SYNC) == 0)
2037 tul_set_ssig(sc, REQ | BSY | SEL, ATN);
2038 return (tul_wait(sc, MSG_ACCEPT));
2039 }
2040
2041 tul_set_ssig(sc, REQ | BSY | SEL, ATN);
2042
2043 phase = tul_wait(sc, MSG_ACCEPT);
2044 if (phase != PHASE_MSG_OUT)
2045 return (phase);
2046
2047 /* WDTR msg out */
2048 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
2049 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_WDTR_LEN);
2050 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_WDTR);
2051 bus_space_write_1(iot, ioh, TUL_SFIFO, sc->sc_msg[2]);
2052
2053 } else
2054 return (tul_msgout_reject(sc));
2055
2056 return (tul_wait(sc, XF_FIFO_OUT));
2057 }
2058
2059 /*
2060 * tul_msgin_sync - check SDTR msg in sc_msg. If the offer is
2061 * acceptable leave sc_msg as is and return 0.
2062 * If the negotiation must continue, modify sc_msg
2063 * as needed and return 1. Else return 0.
2064 */
2065 static int
2066 tul_msgin_sync(sc)
2067 struct iha_softc *sc;
2068 {
2069 int flags;
2070 int newoffer;
2071 u_int8_t default_period;
2072
2073 flags = sc->sc_actscb->tcs->flags;
2074
2075 default_period = tul_rate_tbl[flags & FLAG_SCSI_RATE];
2076
2077 if (sc->sc_msg[3] == 0) /* target offered async only. Accept it. */
2078 return (0);
2079
2080 newoffer = 0;
2081
2082 if ((flags & FLAG_NO_SYNC) != 0) {
2083 sc->sc_msg[3] = 0;
2084 newoffer = 1;
2085 }
2086
2087 if (sc->sc_msg[3] > IHA_MAX_OFFSET) {
2088 sc->sc_msg[3] = IHA_MAX_OFFSET;
2089 newoffer = 1;
2090 }
2091
2092 if (sc->sc_msg[2] < default_period) {
2093 sc->sc_msg[2] = default_period;
2094 newoffer = 1;
2095 }
2096
2097 if (sc->sc_msg[2] >= 59) { /* XXX magic */
2098 sc->sc_msg[3] = 0;
2099 newoffer = 1;
2100 }
2101
2102 return (newoffer);
2103 }
2104
2105 static int
2106 tul_msgout(sc, msg)
2107 struct iha_softc *sc;
2108 u_int8_t msg;
2109 {
2110
2111 bus_space_write_1(sc->sc_iot, sc->sc_ioh, TUL_SFIFO, msg);
2112
2113 return (tul_wait(sc, XF_FIFO_OUT));
2114 }
2115
2116 static void
2117 tul_msgout_abort(sc, aborttype)
2118 struct iha_softc *sc;
2119 u_int8_t aborttype;
2120 {
2121 bus_space_tag_t iot = sc->sc_iot;
2122 bus_space_handle_t ioh = sc->sc_ioh;
2123
2124 tul_set_ssig(sc, REQ | BSY | SEL, ATN);
2125
2126 switch (tul_wait(sc, MSG_ACCEPT)) {
2127 case -1:
2128 break;
2129
2130 case PHASE_MSG_OUT:
2131 bus_space_write_1(iot, ioh, TUL_SFIFO, aborttype);
2132
2133 sc->sc_flags |= FLAG_EXPECT_DISC;
2134
2135 if (tul_wait(sc, XF_FIFO_OUT) != -1)
2136 tul_bad_seq(sc);
2137 break;
2138
2139 default:
2140 tul_bad_seq(sc);
2141 break;
2142 }
2143 }
2144
2145 static int
2146 tul_msgout_reject(sc)
2147 struct iha_softc *sc;
2148 {
2149 bus_space_tag_t iot = sc->sc_iot;
2150 bus_space_handle_t ioh = sc->sc_ioh;
2151 int phase;
2152
2153 tul_set_ssig(sc, REQ | BSY | SEL, ATN);
2154
2155 if ((phase = tul_wait(sc, MSG_ACCEPT)) == -1)
2156 return (-1);
2157
2158 if (phase == PHASE_MSG_OUT) {
2159 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_MESSAGE_REJECT);
2160 return (tul_wait(sc, XF_FIFO_OUT));
2161 }
2162
2163 return (phase);
2164 }
2165
2166 static int
2167 tul_msgout_wide(sc)
2168 struct iha_softc *sc;
2169 {
2170 bus_space_tag_t iot = sc->sc_iot;
2171 bus_space_handle_t ioh = sc->sc_ioh;
2172 int phase;
2173
2174 sc->sc_actscb->tcs->flags |= FLAG_WIDE_DONE;
2175
2176 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
2177 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_WDTR_LEN);
2178 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_WDTR);
2179 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_WDTR_BUS_16_BIT);
2180
2181 phase = tul_wait(sc, XF_FIFO_OUT);
2182
2183 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2184 tul_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
2185
2186 return (phase);
2187 }
2188
2189 static int
2190 tul_msgout_sync(sc)
2191 struct iha_softc *sc;
2192 {
2193 bus_space_tag_t iot = sc->sc_iot;
2194 bus_space_handle_t ioh = sc->sc_ioh;
2195 int rateindex;
2196 int phase;
2197 u_int8_t sync_rate;
2198
2199 rateindex = sc->sc_actscb->tcs->flags & FLAG_SCSI_RATE;
2200
2201 sync_rate = tul_rate_tbl[rateindex];
2202
2203 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXTENDED);
2204 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_SDTR_LEN);
2205 bus_space_write_1(iot, ioh, TUL_SFIFO, MSG_EXT_SDTR);
2206 bus_space_write_1(iot, ioh, TUL_SFIFO, sync_rate);
2207 bus_space_write_1(iot, ioh, TUL_SFIFO, IHA_MAX_OFFSET);/* REQ/ACK*/
2208
2209 phase = tul_wait(sc, XF_FIFO_OUT);
2210
2211 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2212 tul_set_ssig(sc, REQ | BSY | SEL | ATN, 0);
2213
2214 return (phase);
2215 }
2216
2217 static void
2218 tul_wdtr_done(sc)
2219 struct iha_softc *sc;
2220 {
2221 bus_space_tag_t iot = sc->sc_iot;
2222 bus_space_handle_t ioh = sc->sc_ioh;
2223 struct tcs *tcs = sc->sc_actscb->tcs;
2224
2225 tcs->syncm = 0;
2226 tcs->period = 0;
2227 tcs->offset = 0;
2228
2229 if (sc->sc_msg[2] != 0)
2230 tcs->syncm |= PERIOD_WIDE_SCSI;
2231
2232 tcs->sconfig0 &= ~ALTPD;
2233 tcs->flags &= ~FLAG_SYNC_DONE;
2234 tcs->flags |= FLAG_WIDE_DONE;
2235
2236 bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
2237 bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
2238 }
2239
2240 static void
2241 tul_sync_done(sc)
2242 struct iha_softc *sc;
2243 {
2244 bus_space_tag_t iot = sc->sc_iot;
2245 bus_space_handle_t ioh = sc->sc_ioh;
2246 struct tcs *tcs = sc->sc_actscb->tcs;
2247 int i;
2248
2249 if ((tcs->flags & FLAG_SYNC_DONE) == 0) {
2250 tcs->period = sc->sc_msg[2];
2251 tcs->offset = sc->sc_msg[3];
2252 if (tcs->offset != 0) {
2253 tcs->syncm |= tcs->offset;
2254
2255 /* pick the highest possible rate */
2256 for (i = 0; i < 8; i++)
2257 if (tul_rate_tbl[i] >= tcs->period)
2258 break;
2259
2260 tcs->syncm |= (i << 4);
2261 tcs->sconfig0 |= ALTPD;
2262 }
2263
2264 tcs->flags |= FLAG_SYNC_DONE;
2265
2266 bus_space_write_1(iot, ioh, TUL_SCONFIG0, tcs->sconfig0);
2267 bus_space_write_1(iot, ioh, TUL_SYNCM, tcs->syncm);
2268 }
2269 }
2270
2271 void
2272 tul_reset_chip(sc)
2273 struct iha_softc *sc;
2274 {
2275 bus_space_tag_t iot = sc->sc_iot;
2276 bus_space_handle_t ioh = sc->sc_ioh;
2277
2278 /* reset tulip chip */
2279
2280 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSCSI);
2281
2282 do {
2283 sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
2284 } while ((sc->sc_sistat & SRSTD) == 0);
2285
2286 tul_set_ssig(sc, 0, 0);
2287
2288 bus_space_read_1(iot, ioh, TUL_SISTAT); /* Clear any active interrupt*/
2289 }
2290
2291 static void
2292 tul_select(sc, scb, select_type)
2293 struct iha_softc *sc;
2294 struct iha_scsi_req_q *scb;
2295 u_int8_t select_type;
2296 {
2297 bus_space_tag_t iot = sc->sc_iot;
2298 bus_space_handle_t ioh = sc->sc_ioh;
2299
2300 switch (select_type) {
2301 case SEL_ATN:
2302 bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
2303 bus_space_write_multi_1(iot, ioh, TUL_SFIFO,
2304 scb->cmd, scb->cmdlen);
2305
2306 scb->nextstat = 2;
2307 break;
2308
2309 case SELATNSTOP:
2310 scb->nextstat = 1;
2311 break;
2312
2313 case SEL_ATN3:
2314 bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_id);
2315 bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_tagmsg);
2316 bus_space_write_1(iot, ioh, TUL_SFIFO, scb->scb_tagid);
2317
2318 bus_space_write_multi_1(iot, ioh, TUL_SFIFO, scb->cmd,
2319 scb->cmdlen);
2320
2321 scb->nextstat = 2;
2322 break;
2323
2324 default:
2325 printf("[debug] tul_select() - unknown select type = 0x%02x\n",
2326 select_type);
2327 return;
2328 }
2329
2330 tul_del_pend_scb(sc, scb);
2331 scb->status = STATUS_SELECT;
2332
2333 sc->sc_actscb = scb;
2334
2335 bus_space_write_1(iot, ioh, TUL_SCMD, select_type);
2336 }
2337
2338 /*
2339 * tul_wait - wait for an interrupt to service or a SCSI bus phase change
2340 * after writing the supplied command to the tulip chip. If
2341 * the command is NO_OP, skip the command writing.
2342 */
2343 static int
2344 tul_wait(sc, cmd)
2345 struct iha_softc *sc;
2346 u_int8_t cmd;
2347 {
2348 bus_space_tag_t iot = sc->sc_iot;
2349 bus_space_handle_t ioh = sc->sc_ioh;
2350
2351 if (cmd != NO_OP)
2352 bus_space_write_1(iot, ioh, TUL_SCMD, cmd);
2353
2354 /*
2355 * Have to do this here, in addition to in iha_isr, because
2356 * interrupts might be turned off when we get here.
2357 */
2358 do {
2359 sc->sc_status0 = bus_space_read_1(iot, ioh, TUL_STAT0);
2360 } while ((sc->sc_status0 & INTPD) == 0);
2361
2362 sc->sc_status1 = bus_space_read_1(iot, ioh, TUL_STAT1);
2363 sc->sc_sistat = bus_space_read_1(iot, ioh, TUL_SISTAT);
2364
2365 sc->sc_phase = sc->sc_status0 & PH_MASK;
2366
2367 if ((sc->sc_sistat & SRSTD) != 0) {
2368 /* SCSI bus reset interrupt */
2369 tul_reset_scsi_bus(sc);
2370 return (-1);
2371 }
2372
2373 if ((sc->sc_sistat & RSELED) != 0)
2374 /* Reselection interrupt */
2375 return (tul_resel(sc));
2376
2377 if ((sc->sc_sistat & STIMEO) != 0) {
2378 /* selected/reselected timeout interrupt */
2379 tul_busfree(sc);
2380 return (-1);
2381 }
2382
2383 if ((sc->sc_sistat & DISCD) != 0) {
2384 /* BUS disconnection interrupt */
2385 if ((sc->sc_flags & FLAG_EXPECT_DONE_DISC) != 0) {
2386 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2387 bus_space_write_1(iot, ioh, TUL_SCONFIG0,
2388 SCONFIG0DEFAULT);
2389 bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
2390 tul_append_done_scb(sc, sc->sc_actscb, HOST_OK);
2391 sc->sc_flags &= ~FLAG_EXPECT_DONE_DISC;
2392
2393 } else if ((sc->sc_flags & FLAG_EXPECT_DISC) != 0) {
2394 bus_space_write_1(iot, ioh, TUL_SCTRL0, RSFIFO);
2395 bus_space_write_1(iot, ioh, TUL_SCONFIG0,
2396 SCONFIG0DEFAULT);
2397 bus_space_write_1(iot, ioh, TUL_SCTRL1, EHRSL);
2398 sc->sc_actscb = NULL;
2399 sc->sc_flags &= ~FLAG_EXPECT_DISC;
2400
2401 } else
2402 tul_busfree(sc);
2403
2404 return (-1);
2405 }
2406
2407 return (sc->sc_phase);
2408 }
2409
2410 /*
2411 * tul_done_scb - We have a scb which has been processed by the
2412 * adaptor, now we look to see how the operation went.
2413 */
2414 static void
2415 tul_done_scb(sc, scb)
2416 struct iha_softc *sc;
2417 struct iha_scsi_req_q *scb;
2418 {
2419 struct scsipi_xfer *xs = scb->xs;
2420
2421 if (xs != NULL) {
2422 /* Cancel the timeout. */
2423 callout_stop(&xs->xs_callout);
2424
2425 if (xs->datalen > 0) {
2426 bus_dmamap_sync(sc->sc_dmat, scb->dmap,
2427 0, scb->dmap->dm_mapsize,
2428 (xs->xs_control & XS_CTL_DATA_IN) ?
2429 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2430 bus_dmamap_unload(sc->sc_dmat, scb->dmap);
2431 }
2432
2433 xs->status = scb->ta_stat;
2434
2435 switch (scb->ha_stat) {
2436 case HOST_OK:
2437 switch (scb->ta_stat) {
2438 case SCSI_OK:
2439 case SCSI_CONDITION_MET:
2440 case SCSI_INTERM:
2441 case SCSI_INTERM_COND_MET:
2442 xs->resid = scb->buflen;
2443 xs->error = XS_NOERROR;
2444 if ((scb->flags & FLAG_RSENS) != 0)
2445 xs->error = XS_SENSE;
2446 break;
2447
2448 case SCSI_RESV_CONFLICT:
2449 case SCSI_BUSY:
2450 case SCSI_QUEUE_FULL:
2451 xs->error = XS_BUSY;
2452 break;
2453
2454 case SCSI_TERMINATED:
2455 case SCSI_ACA_ACTIVE:
2456 case SCSI_CHECK:
2457 scb->tcs->flags &=
2458 ~(FLAG_SYNC_DONE | FLAG_WIDE_DONE);
2459
2460 if ((scb->flags & FLAG_RSENS) != 0 ||
2461 tul_push_sense_request(sc, scb) != 0) {
2462 scb->flags &= FLAG_RSENS;
2463 printf("%s: request sense failed\n",
2464 sc->sc_dev.dv_xname);
2465 xs->error = XS_DRIVER_STUFFUP;
2466 break;
2467 }
2468
2469 xs->error = XS_SENSE;
2470 return;
2471
2472 default:
2473 xs->error = XS_DRIVER_STUFFUP;
2474 break;
2475 }
2476 break;
2477
2478 case HOST_SEL_TOUT:
2479 xs->error = XS_SELTIMEOUT;
2480 break;
2481
2482 case HOST_SCSI_RST:
2483 case HOST_DEV_RST:
2484 xs->error = XS_RESET;
2485 break;
2486
2487 case HOST_SPERR:
2488 printf("%s: SCSI Parity error detected\n",
2489 sc->sc_dev.dv_xname);
2490 xs->error = XS_DRIVER_STUFFUP;
2491 break;
2492
2493 case HOST_TIMED_OUT:
2494 xs->error = XS_TIMEOUT;
2495 break;
2496
2497 case HOST_DO_DU:
2498 case HOST_BAD_PHAS:
2499 default:
2500 xs->error = XS_DRIVER_STUFFUP;
2501 break;
2502 }
2503
2504 scsipi_done(xs);
2505 }
2506
2507 tul_append_free_scb(sc, scb);
2508 }
2509
2510 static void
2511 tul_timeout(arg)
2512 void *arg;
2513 {
2514 struct iha_scsi_req_q *scb = (struct iha_scsi_req_q *)arg;
2515 struct scsipi_xfer *xs = scb->xs;
2516 struct scsipi_periph *periph = xs->xs_periph;
2517 struct iha_softc *sc;
2518
2519 sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
2520
2521 if (xs == NULL)
2522 printf("[debug] tul_timeout called with xs == NULL\n");
2523
2524 else {
2525 scsipi_printaddr(periph);
2526 printf("SCSI OpCode 0x%02x timed out\n", xs->cmd->opcode);
2527
2528 tul_abort_xs(sc, xs, HOST_TIMED_OUT);
2529 }
2530 }
2531
2532 static void
2533 tul_exec_scb(sc, scb)
2534 struct iha_softc *sc;
2535 struct iha_scsi_req_q *scb;
2536 {
2537 bus_space_tag_t iot;
2538 bus_space_handle_t ioh;
2539 bus_dmamap_t dm;
2540 struct scsipi_xfer *xs = scb->xs;
2541 int nseg, s;
2542
2543 dm = scb->dmap;
2544 nseg = dm->dm_nsegs;
2545
2546 if (nseg > 1) {
2547 struct iha_sg_element *sg = scb->sglist;
2548 int i;
2549
2550 for (i = 0; i < nseg; i++) {
2551 sg[i].sg_len = htole32(dm->dm_segs[i].ds_len);
2552 sg[i].sg_addr = htole32(dm->dm_segs[i].ds_addr);
2553 }
2554 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
2555 scb->sgoffset, IHA_SG_SIZE,
2556 BUS_DMASYNC_PREWRITE);
2557
2558 scb->flags |= FLAG_SG; /* XXX */
2559 scb->sg_size = scb->sg_max = nseg;
2560
2561 scb->bufaddr = scb->sg_addr;
2562 } else
2563 scb->bufaddr = dm->dm_segs[0].ds_addr;
2564
2565 if ((xs->xs_control & XS_CTL_POLL) == 0) {
2566 int timeout = xs->timeout;
2567 timeout = (timeout > 100000) ?
2568 timeout / 1000 * hz : timeout * hz / 1000;
2569 if (timeout == 0)
2570 timeout = 1;
2571 callout_reset(&xs->xs_callout, timeout, tul_timeout, scb);
2572 }
2573
2574 s = splbio();
2575
2576 if (((scb->flags & XS_RESET) != 0) || (scb->cmd[0] == REQUEST_SENSE))
2577 tul_push_pend_scb(sc, scb); /* Insert SCB at head of Pend */
2578 else
2579 tul_append_pend_scb(sc, scb); /* Append SCB to tail of Pend */
2580
2581 /*
2582 * Run through tul_main() to ensure something is active, if
2583 * only this new SCB.
2584 */
2585 if (sc->sc_semaph != SEMAPH_IN_MAIN) {
2586 iot = sc->sc_iot;
2587 ioh = sc->sc_ioh;
2588
2589 bus_space_write_1(iot, ioh, TUL_IMSK, MASK_ALL);
2590 sc->sc_semaph = SEMAPH_IN_MAIN;;
2591
2592 splx(s);
2593 tul_main(sc);
2594 s = splbio();
2595
2596 sc->sc_semaph = ~SEMAPH_IN_MAIN;;
2597 bus_space_write_1(iot, ioh, TUL_IMSK, (MASK_ALL & ~MSCMP));
2598 }
2599
2600 splx(s);
2601 }
2602
2603
2604 /*
2605 * tul_set_ssig - read the current scsi signal mask, then write a new
2606 * one which turns off/on the specified signals.
2607 */
2608 static void
2609 tul_set_ssig(sc, offsigs, onsigs)
2610 struct iha_softc *sc;
2611 u_int8_t offsigs, onsigs;
2612 {
2613 bus_space_tag_t iot = sc->sc_iot;
2614 bus_space_handle_t ioh = sc->sc_ioh;
2615 u_int8_t currsigs;
2616
2617 currsigs = bus_space_read_1(iot, ioh, TUL_SSIGI);
2618 bus_space_write_1(iot, ioh, TUL_SSIGO, (currsigs & ~offsigs) | onsigs);
2619 }
2620
2621 /*
2622 * tul_alloc_sglist - allocate and map sglist for SCB's
2623 */
2624 static int
2625 tul_alloc_sglist(sc)
2626 struct iha_softc *sc;
2627 {
2628 bus_dma_segment_t seg;
2629 int error, rseg;
2630
2631 /*
2632 * Allocate dma-safe memory for the SCB's sglist
2633 */
2634 if ((error = bus_dmamem_alloc(sc->sc_dmat,
2635 IHA_SG_SIZE * IHA_MAX_SCB,
2636 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
2637 printf(": unable to allocate sglist, error = %d\n", error);
2638 return (error);
2639 }
2640 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
2641 IHA_SG_SIZE * IHA_MAX_SCB, (caddr_t *)&sc->sc_sglist,
2642 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
2643 printf(": unable to map sglist, error = %d\n", error);
2644 return (error);
2645 }
2646
2647 /*
2648 * Create and load the DMA map used for the SCBs
2649 */
2650 if ((error = bus_dmamap_create(sc->sc_dmat,
2651 IHA_SG_SIZE * IHA_MAX_SCB, 1, IHA_SG_SIZE * IHA_MAX_SCB,
2652 0, BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
2653 printf(": unable to create control DMA map, error = %d\n",
2654 error);
2655 return (error);
2656 }
2657 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
2658 sc->sc_sglist, IHA_SG_SIZE * IHA_MAX_SCB,
2659 NULL, BUS_DMA_NOWAIT)) != 0) {
2660 printf(": unable to load control DMA map, error = %d\n", error);
2661 return (error);
2662 }
2663
2664 memset(sc->sc_sglist, 0, IHA_SG_SIZE * IHA_MAX_SCB);
2665
2666 return (0);
2667 }
2668
2669 /*
2670 * tul_read_eeprom - read Serial EEPROM value & set to defaults
2671 * if required. XXX - Writing does NOT work!
2672 */
2673 void
2674 tul_read_eeprom(sc, eeprom)
2675 struct iha_softc *sc;
2676 struct iha_eeprom *eeprom;
2677 {
2678 bus_space_tag_t iot = sc->sc_iot;
2679 bus_space_handle_t ioh = sc->sc_ioh;
2680 u_int16_t *buf = (u_int16_t *)eeprom;
2681 u_int8_t gctrl;
2682
2683 /*------Enable EEProm programming ---*/
2684 gctrl = bus_space_read_1(iot, ioh, TUL_GCTRL0) | EEPRG;
2685 bus_space_write_1(iot, ioh, TUL_GCTRL0, gctrl);
2686
2687 /*------ Program default pattern ----*/
2688 if (tul_se2_rd_all(sc, buf) == 0) {
2689 tul_se2_update_all(sc);
2690 if(tul_se2_rd_all(sc, buf) == 0)
2691 panic("could not program iha Tulip EEPROM\n");
2692 }
2693
2694 /*------ Disable EEProm programming ---*/
2695 gctrl = bus_space_read_1(iot, ioh, TUL_GCTRL0) & ~EEPRG;
2696 bus_space_write_1(iot, ioh, TUL_GCTRL0, gctrl);
2697 }
2698
2699 /*
2700 * tul_se2_update_all - Update SCSI H/A configuration parameters from
2701 * serial EEPROM Setup default pattern. Only
2702 * change those values different from the values
2703 * in tul_nvram.
2704 */
2705 void
2706 tul_se2_update_all(sc)
2707 struct iha_softc *sc;
2708 {
2709 bus_space_tag_t iot = sc->sc_iot;
2710 bus_space_handle_t ioh = sc->sc_ioh;
2711 u_int16_t *np;
2712 u_int32_t chksum;
2713 int i;
2714
2715 /* Enable erase/write state of EEPROM */
2716 tul_se2_instr(sc, ENABLE_ERASE);
2717 bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
2718 EEP_WAIT();
2719
2720 np = (u_int16_t *)&eeprom_default;
2721
2722 for (i = 0, chksum = 0; i < EEPROM_SIZE - 1; i++) {
2723 tul_se2_wr(sc, i, *np);
2724 chksum += *np++;
2725 }
2726
2727 chksum &= 0x0000ffff;
2728 tul_se2_wr(sc, 31, chksum);
2729
2730 /* Disable erase/write state of EEPROM */
2731 tul_se2_instr(sc, 0);
2732 bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
2733 EEP_WAIT();
2734 }
2735
2736 /*
2737 * tul_se2_wr - write the given 16 bit value into the Serial EEPROM
2738 * at the specified offset
2739 */
2740 void
2741 tul_se2_wr(sc, addr, writeword)
2742 struct iha_softc *sc;
2743 int addr;
2744 u_int16_t writeword;
2745 {
2746 bus_space_tag_t iot = sc->sc_iot;
2747 bus_space_handle_t ioh = sc->sc_ioh;
2748 int i, bit;
2749
2750 /* send 'WRITE' Instruction == address | WRITE bit */
2751 tul_se2_instr(sc, addr | WRITE);
2752
2753 for (i = 16; i > 0; i--) {
2754 if (writeword & (1 << (i - 1)))
2755 bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRDO);
2756 else
2757 bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2758 EEP_WAIT();
2759 bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
2760 EEP_WAIT();
2761 }
2762
2763 bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2764 EEP_WAIT();
2765 bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
2766 EEP_WAIT();
2767 bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2768 EEP_WAIT();
2769
2770 for (;;) {
2771 bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
2772 EEP_WAIT();
2773 bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2774 EEP_WAIT();
2775 bit = bus_space_read_1(iot, ioh, TUL_NVRAM) & NVRDI;
2776 EEP_WAIT();
2777 if (bit != 0)
2778 break; /* write complete */
2779 }
2780
2781 bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
2782 }
2783
2784 /*
2785 * tul_se2_rd - read & return the 16 bit value at the specified
2786 * offset in the Serial E2PROM
2787 *
2788 */
2789 u_int16_t
2790 tul_se2_rd(sc, addr)
2791 struct iha_softc *sc;
2792 int addr;
2793 {
2794 bus_space_tag_t iot = sc->sc_iot;
2795 bus_space_handle_t ioh = sc->sc_ioh;
2796 int i, bit;
2797 u_int16_t readword;
2798
2799 /* Send 'READ' instruction == address | READ bit */
2800 tul_se2_instr(sc, addr | READ);
2801
2802 readword = 0;
2803 for (i = 16; i > 0; i--) {
2804 bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS | NVRCK);
2805 EEP_WAIT();
2806 bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2807 EEP_WAIT();
2808 /* sample data after the following edge of clock */
2809 bit = bus_space_read_1(iot, ioh, TUL_NVRAM) & NVRDI ? 1 : 0;
2810 EEP_WAIT();
2811
2812 readword |= bit << (i - 1);
2813 }
2814
2815 bus_space_write_1(iot, ioh, TUL_NVRAM, 0);
2816
2817 return (readword);
2818 }
2819
2820 /*
2821 * tul_se2_rd_all - Read SCSI H/A config parameters from serial EEPROM
2822 */
2823 int
2824 tul_se2_rd_all(sc, buf)
2825 struct iha_softc *sc;
2826 u_int16_t *buf;
2827 {
2828 struct iha_eeprom *eeprom = (struct iha_eeprom *)buf;
2829 u_int32_t chksum;
2830 int i;
2831
2832 for (i = 0, chksum = 0; i < EEPROM_SIZE - 1; i++) {
2833 *buf = tul_se2_rd(sc, i);
2834 chksum += *buf++;
2835 }
2836 *buf = tul_se2_rd(sc, 31); /* just read checksum */
2837
2838 chksum &= 0x0000ffff; /* checksum is lower 16 bits of sum */
2839
2840 return (eeprom->signature == EEP_SIGNATURE) &&
2841 (eeprom->checksum == chksum);
2842 }
2843
2844 /*
2845 * tul_se2_instr - write an octet to serial E2PROM one bit at a time
2846 */
2847 void
2848 tul_se2_instr(sc, instr)
2849 struct iha_softc *sc;
2850 int instr;
2851 {
2852 bus_space_tag_t iot = sc->sc_iot;
2853 bus_space_handle_t ioh = sc->sc_ioh;
2854 int b, i;
2855
2856 b = NVRCS | NVRDO; /* Write the start bit (== 1) */
2857
2858 bus_space_write_1(iot, ioh, TUL_NVRAM, b);
2859 EEP_WAIT();
2860 bus_space_write_1(iot, ioh, TUL_NVRAM, b | NVRCK);
2861 EEP_WAIT();
2862
2863 for (i = 8; i > 0; i--) {
2864 if (instr & (1 << (i - 1)))
2865 b = NVRCS | NVRDO; /* Write a 1 bit */
2866 else
2867 b = NVRCS; /* Write a 0 bit */
2868
2869 bus_space_write_1(iot, ioh, TUL_NVRAM, b);
2870 EEP_WAIT();
2871 bus_space_write_1(iot, ioh, TUL_NVRAM, b | NVRCK);
2872 EEP_WAIT();
2873 }
2874
2875 bus_space_write_1(iot, ioh, TUL_NVRAM, NVRCS);
2876 }
2877
2878 /*
2879 * tul_reset_tcs - reset the target control structure pointed
2880 * to by tcs to default values. tcs flags
2881 * only has the negotiation done bits reset as
2882 * the other bits are fixed at initialization.
2883 */
2884 void
2885 tul_reset_tcs(tcs, config0)
2886 struct tcs *tcs;
2887 u_int8_t config0;
2888 {
2889
2890 tcs->flags &= ~(FLAG_SYNC_DONE | FLAG_WIDE_DONE);
2891 tcs->period = 0;
2892 tcs->offset = 0;
2893 tcs->tagcnt = 0;
2894 tcs->ntagscb = NULL;
2895 tcs->syncm = 0;
2896 tcs->sconfig0 = config0;
2897 }
2898