sfas.c revision 1.22 1 /* $NetBSD: sfas.c,v 1.22 2012/10/27 17:17:24 chs Exp $ */
2
3 /*
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Van Jacobson of Lawrence Berkeley Laboratory.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)scsi.c 7.5 (Berkeley) 5/4/91
35 */
36
37 /*
38 * Copyright (c) 1995 Scott Stevens
39 * Copyright (c) 1995 Daniel Widenfalk
40 * Copyright (c) 1994 Christian E. Hopps
41 *
42 * This code is derived from software contributed to Berkeley by
43 * Van Jacobson of Lawrence Berkeley Laboratory.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by the University of
56 * California, Berkeley and its contributors.
57 * 4. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 * @(#)scsi.c 7.5 (Berkeley) 5/4/91
74 */
75
76 /*
77 * Emulex FAS216 scsi adaptor driver
78 */
79
80 /*
81 * Modified for NetBSD/arm32 by Scott Stevens
82 */
83
84 #include <sys/cdefs.h>
85 __KERNEL_RCSID(0, "$NetBSD: sfas.c,v 1.22 2012/10/27 17:17:24 chs Exp $");
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/device.h>
90 #include <sys/buf.h>
91 #include <sys/proc.h>
92
93 #include <dev/scsipi/scsi_all.h>
94 #include <dev/scsipi/scsipi_all.h>
95 #include <dev/scsipi/scsiconf.h>
96
97 #include <uvm/uvm_extern.h>
98
99 #include <machine/pmap.h>
100 #include <machine/cpu.h>
101 #include <machine/io.h>
102 #include <machine/intr.h>
103 #include <arm/arm32/katelib.h>
104 #include <acorn32/podulebus/podulebus.h>
105 #include <acorn32/podulebus/sfasreg.h>
106 #include <acorn32/podulebus/sfasvar.h>
107
108 void sfas_minphys(struct buf *);
109 void sfas_init_nexus(struct sfas_softc *, struct nexus *);
110 void sfasinitialize(struct sfas_softc *);
111 void sfas_scsi_request(struct scsipi_channel *, scsipi_adapter_req_t, void *);
112 void sfas_donextcmd(struct sfas_softc *, struct sfas_pending *);
113 void sfas_scsidone(struct sfas_softc *, struct scsipi_xfer *, int);
114 void sfasintr(struct sfas_softc *);
115 void sfasiwait(struct sfas_softc *);
116 void sfas_ixfer(void *, int);
117 void sfasreset(struct sfas_softc *, int);
118 int sfasselect(struct sfas_softc *, struct sfas_pending *, unsigned char *,
119 int, unsigned char *, int, int);
120 void sfasicmd(struct sfas_softc *, struct sfas_pending *);
121 void sfasgo(struct sfas_softc *, struct sfas_pending *);
122 void sfas_save_pointers(struct sfas_softc *);
123 void sfas_restore_pointers(struct sfas_softc *);
124 void sfas_build_sdtrm(struct sfas_softc *, int, int);
125 int sfas_select_unit(struct sfas_softc *, short);
126 struct nexus *sfas_arbitate_target(struct sfas_softc *, int);
127 void sfas_setup_nexus(struct sfas_softc *, struct nexus *,
128 struct sfas_pending *, unsigned char *, int,
129 unsigned char *, int, int);
130 int sfas_pretests(struct sfas_softc *, sfas_regmap_p);
131 int sfas_midaction(struct sfas_softc *, sfas_regmap_p, struct nexus *);
132 int sfas_postaction(struct sfas_softc *, sfas_regmap_p, struct nexus *);
133
134 /*
135 * Initialize these to make 'em patchable. Defaults to enable sync and discon.
136 */
137 u_char sfas_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
138 u_char sfas_inhibit_disc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
139
140 #undef DEBUG
141 #define DEBUG
142 #ifdef DEBUG
143 #define QPRINTF(a) if (sfas_debug > 1) printf a
144 int sfas_debug = 2;
145 #else
146 #define QPRINTF
147 #endif
148
149 /*
150 * default minphys routine for sfas based controllers
151 */
152 void
153 sfas_minphys(struct buf *bp)
154 {
155
156 /*
157 * No max transfer at this level.
158 */
159 minphys(bp);
160 }
161
162 /*
163 * Initialize the nexus structs.
164 */
165 void
166 sfas_init_nexus(struct sfas_softc *dev, struct nexus *nexus)
167 {
168 memset(nexus, 0, sizeof(struct nexus));
169
170 nexus->state = SFAS_NS_IDLE;
171 nexus->period = 200;
172 nexus->offset = 0;
173 nexus->syncper = 5;
174 nexus->syncoff = 0;
175 nexus->config3 = dev->sc_config3 & ~SFAS_CFG3_FASTSCSI;
176 }
177
178 void
179 sfasinitialize(struct sfas_softc *dev)
180 {
181 u_int *pte;
182 int i;
183
184 dev->sc_led_status = 0;
185
186 TAILQ_INIT(&dev->sc_xs_pending);
187 TAILQ_INIT(&dev->sc_xs_free);
188
189 /*
190 * Initialize the sfas_pending structs and link them into the free list. We
191 * have to set vm_link_data.pages to 0 or the vm FIX won't work.
192 */
193 for(i=0; i<MAXPENDING; i++) {
194 TAILQ_INSERT_TAIL(&dev->sc_xs_free, &dev->sc_xs_store[i],
195 link);
196 }
197
198 /*
199 * Calculate the correct clock conversion factor 2 <= factor <= 8, i.e. set
200 * the factor to clock_freq / 5 (int).
201 */
202 if (dev->sc_clock_freq <= 10)
203 dev->sc_clock_conv_fact = 2;
204 if (dev->sc_clock_freq <= 40)
205 dev->sc_clock_conv_fact = 2+((dev->sc_clock_freq-10)/5);
206 else
207 panic("sfasinitialize: Clock frequence too high");
208
209 /* Setup and save the basic configuration registers */
210 dev->sc_config1 = (dev->sc_host_id & SFAS_CFG1_BUS_ID_MASK);
211 dev->sc_config2 = SFAS_CFG2_FEATURES_ENABLE;
212 dev->sc_config3 = (dev->sc_clock_freq > 25 ? SFAS_CFG3_FASTCLK : 0);
213
214 /* Precalculate timeout value and clock period. */
215 /* Ekkk ... floating point in the kernel !!!! */
216 /* dev->sc_timeout_val = 1+dev->sc_timeout*dev->sc_clock_freq/
217 (7.682*dev->sc_clock_conv_fact);*/
218 dev->sc_timeout_val = 1+dev->sc_timeout*dev->sc_clock_freq/
219 ((7682*dev->sc_clock_conv_fact)/1000);
220 dev->sc_clock_period = 1000/dev->sc_clock_freq;
221
222 sfasreset(dev, 1 | 2); /* Reset Chip and Bus */
223
224 dev->sc_units_disconnected = 0;
225 dev->sc_msg_in_len = 0;
226 dev->sc_msg_out_len = 0;
227
228 dev->sc_flags = 0;
229
230 for(i=0; i<8; i++)
231 sfas_init_nexus(dev, &dev->sc_nexus[i]);
232
233 if (dev->sc_ixfer == NULL)
234 dev->sc_ixfer = sfas_ixfer;
235
236 /*
237 * Setup bump buffer.
238 */
239 dev->sc_bump_va = (u_char *)uvm_km_alloc(kernel_map, dev->sc_bump_sz, 0,
240 UVM_KMF_WIRED | UVM_KMF_ZERO);
241 (void) pmap_extract(pmap_kernel(), (vaddr_t)dev->sc_bump_va,
242 (paddr_t *)&dev->sc_bump_pa);
243
244 /*
245 * Setup pages to noncachable, that way we don't have to flush the cache
246 * every time we need "bumped" transfer.
247 */
248 pte = vtopte((vaddr_t) dev->sc_bump_va);
249 *pte &= ~(L2_C | L2_B);
250 PTE_SYNC(pte);
251 cpu_tlb_flushD();
252 cpu_dcache_wbinv_range((vm_offset_t)dev->sc_bump_va, PAGE_SIZE);
253
254 printf(" dmabuf V0x%08x P0x%08x", (u_int)dev->sc_bump_va, (u_int)dev->sc_bump_pa);
255 }
256
257
258 /*
259 * used by specific sfas controller
260 */
261 void
262 sfas_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
263 void *arg)
264 {
265 struct scsipi_xfer *xs;
266 struct sfas_softc *dev = device_private(chan->chan_adapter->adapt_dev);
267 struct scsipi_periph *periph;
268 struct sfas_pending *pendp;
269 int flags, s, target;
270
271 switch (req) {
272 case ADAPTER_REQ_RUN_XFER:
273 xs = arg;
274 periph = xs->xs_periph;
275 flags = xs->xs_control;
276 target = periph->periph_target;
277
278 if (flags & XS_CTL_DATA_UIO)
279 panic("sfas: scsi data uio requested");
280
281 if ((flags & XS_CTL_POLL) && (dev->sc_flags & SFAS_ACTIVE))
282 panic("sfas_scsicmd: busy");
283
284 /* Get hold of a sfas_pending block. */
285 s = splbio();
286 pendp = dev->sc_xs_free.tqh_first;
287 if (pendp == NULL) {
288 xs->error = XS_RESOURCE_SHORTAGE;
289 scsipi_done(xs);
290 splx(s);
291 return;
292 }
293 TAILQ_REMOVE(&dev->sc_xs_free, pendp, link);
294 pendp->xs = xs;
295 splx(s);
296
297
298 /* If the chip if busy OR the unit is busy, we have to wait for out turn. */
299 if ((dev->sc_flags & SFAS_ACTIVE) ||
300 (dev->sc_nexus[target].flags & SFAS_NF_UNIT_BUSY)) {
301 s = splbio();
302 TAILQ_INSERT_TAIL(&dev->sc_xs_pending, pendp, link);
303 splx(s);
304 } else
305 sfas_donextcmd(dev, pendp);
306
307 return;
308
309 case ADAPTER_REQ_GROW_RESOURCES:
310 case ADAPTER_REQ_SET_XFER_MODE:
311 /* XXX Not supported. */
312 return;
313 }
314 }
315
316 /*
317 * Actually select the unit, whereby the whole scsi-process is started.
318 */
319 void
320 sfas_donextcmd(struct sfas_softc *dev, struct sfas_pending *pendp)
321 {
322 int s;
323
324 /*
325 * Special case for scsi unit reset. I think this is waterproof. We first
326 * select the unit during splbio. We then cycle through the generated
327 * interrupts until the interrupt routine signals that the unit has
328 * acknowledged the reset. After that we have to wait a reset to select
329 * delay before anything else can happend.
330 */
331 if (pendp->xs->xs_control & XS_CTL_RESET) {
332 struct nexus *nexus;
333
334 s = splbio();
335 while(!sfasselect(dev, pendp, 0, 0, 0, 0, SFAS_SELECT_K)) {
336 splx(s);
337 delay(10);
338 s = splbio();
339 }
340
341 nexus = dev->sc_cur_nexus;
342 while(nexus->flags & SFAS_NF_UNIT_BUSY) {
343 sfasiwait(dev);
344 sfasintr(dev);
345 }
346
347 nexus->flags |= SFAS_NF_UNIT_BUSY;
348 splx(s);
349
350 sfasreset(dev, 0);
351
352 s = splbio();
353 nexus->flags &= ~SFAS_NF_UNIT_BUSY;
354 splx(s);
355 }
356
357 /*
358 * If we are polling, go to splbio and perform the command, else we poke
359 * the scsi-bus via sfasgo to get the interrupt machine going.
360 */
361 if (pendp->xs->xs_control & XS_CTL_POLL) {
362 s = splbio();
363 sfasicmd(dev, pendp);
364 TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link);
365 splx(s);
366 } else {
367 sfasgo(dev, pendp);
368 }
369 }
370
371 void
372 sfas_scsidone(struct sfas_softc *dev, struct scsipi_xfer *xs, int stat)
373 {
374 struct sfas_pending *pendp;
375 int s;
376
377 xs->status = stat;
378
379 if (stat == 0)
380 xs->resid = 0;
381 else {
382 switch(stat) {
383 case SCSI_CHECK:
384 case SCSI_BUSY:
385 xs->error = XS_BUSY;
386 break;
387 case -1:
388 xs->error = XS_DRIVER_STUFFUP;
389 QPRINTF(("sfas_scsicmd() bad %x\n", stat));
390 break;
391 default:
392 xs->error = XS_TIMEOUT;
393 break;
394 }
395 }
396
397 /* Steal the next command from the queue so that one unit can't hog the bus. */
398 s = splbio();
399 pendp = dev->sc_xs_pending.tqh_first;
400 while(pendp) {
401 if (!(dev->sc_nexus[pendp->xs->xs_periph->periph_target].flags &
402 SFAS_NF_UNIT_BUSY))
403 break;
404 pendp = pendp->link.tqe_next;
405 }
406
407 if (pendp != NULL) {
408 TAILQ_REMOVE(&dev->sc_xs_pending, pendp, link);
409 }
410
411 splx(s);
412 scsipi_done(xs);
413
414 if (pendp)
415 sfas_donextcmd(dev, pendp);
416 }
417
418 /*
419 * There are two kinds of reset:
420 * 1) CHIP-bus reset. This also implies a SCSI-bus reset.
421 * 2) SCSI-bus reset.
422 * After the appropriate resets have been performed we wait a reset to select
423 * delay time.
424 */
425 void
426 sfasreset(struct sfas_softc *dev, int how)
427 {
428 sfas_regmap_p rp;
429 int i, s;
430
431 rp = dev->sc_fas;
432
433 if (how & 1) {
434 for(i=0; i<8; i++)
435 sfas_init_nexus(dev, &dev->sc_nexus[i]);
436
437 *rp->sfas_command = SFAS_CMD_RESET_CHIP;
438 delay(1);
439 *rp->sfas_command = SFAS_CMD_NOP;
440
441 *rp->sfas_config1 = dev->sc_config1;
442 *rp->sfas_config2 = dev->sc_config2;
443 *rp->sfas_config3 = dev->sc_config3;
444 *rp->sfas_timeout = dev->sc_timeout_val;
445 *rp->sfas_clkconv = dev->sc_clock_conv_fact &
446 SFAS_CLOCK_CONVERSION_MASK;
447 }
448
449 if (how & 2) {
450 for(i=0; i<8; i++)
451 sfas_init_nexus(dev, &dev->sc_nexus[i]);
452
453 s = splbio();
454
455 *rp->sfas_command = SFAS_CMD_RESET_SCSI_BUS;
456 delay(100);
457
458 /* Skip interrupt generated by RESET_SCSI_BUS */
459 while(*rp->sfas_status & SFAS_STAT_INTERRUPT_PENDING) {
460 dev->sc_status = *rp->sfas_status;
461 dev->sc_interrupt = *rp->sfas_interrupt;
462
463 delay(100);
464 }
465
466 dev->sc_status = *rp->sfas_status;
467 dev->sc_interrupt = *rp->sfas_interrupt;
468
469 splx(s);
470 }
471
472 if (dev->sc_config_flags & SFAS_SLOW_START)
473 delay(4*250000); /* RESET to SELECT DELAY*4 for slow devices */
474 else
475 delay(250000); /* RESET to SELECT DELAY */
476 }
477
478 /*
479 * Save active data pointers to the nexus block currently active.
480 */
481 void
482 sfas_save_pointers(struct sfas_softc *dev)
483 {
484 struct nexus *nx;
485
486 nx = dev->sc_cur_nexus;
487 if (nx) {
488 nx->cur_link = dev->sc_cur_link;
489 nx->max_link = dev->sc_max_link;
490 nx->buf = dev->sc_buf;
491 nx->len = dev->sc_len;
492 nx->dma_len = dev->sc_dma_len;
493 nx->dma_buf = dev->sc_dma_buf;
494 nx->dma_blk_flg = dev->sc_dma_blk_flg;
495 nx->dma_blk_len = dev->sc_dma_blk_len;
496 nx->dma_blk_ptr = dev->sc_dma_blk_ptr;
497 }
498 }
499
500 /*
501 * Restore data pointers from the currently active nexus block.
502 */
503 void
504 sfas_restore_pointers(struct sfas_softc *dev)
505 {
506 struct nexus *nx;
507
508 nx = dev->sc_cur_nexus;
509 if (nx) {
510 dev->sc_cur_link = nx->cur_link;
511 dev->sc_max_link = nx->max_link;
512 dev->sc_buf = nx->buf;
513 dev->sc_len = nx->len;
514 dev->sc_dma_len = nx->dma_len;
515 dev->sc_dma_buf = nx->dma_buf;
516 dev->sc_dma_blk_flg = nx->dma_blk_flg;
517 dev->sc_dma_blk_len = nx->dma_blk_len;
518 dev->sc_dma_blk_ptr = nx->dma_blk_ptr;
519 dev->sc_chain = nx->dma;
520 dev->sc_unit = (nx->lun_unit & 0x0F);
521 dev->sc_lun = (nx->lun_unit & 0xF0) >> 4;
522 }
523 }
524
525 /*
526 * sfasiwait is used during interrupt and polled IO to wait for an event from
527 * the FAS chip. This function MUST NOT BE CALLED without interrupt disabled.
528 */
529 void
530 sfasiwait(struct sfas_softc *dev)
531 {
532 sfas_regmap_p rp;
533
534 /*
535 * If SFAS_DONT_WAIT is set, we have already grabbed the interrupt info
536 * elsewhere. So we don't have to wait for it.
537 */
538 if (dev->sc_flags & SFAS_DONT_WAIT) {
539 dev->sc_flags &= ~SFAS_DONT_WAIT;
540 return;
541 }
542
543 rp = dev->sc_fas;
544
545 /* Wait for FAS chip to signal an interrupt. */
546 while(!(*rp->sfas_status & SFAS_STAT_INTERRUPT_PENDING))
547 delay(1);
548
549 /* Grab interrupt info from chip. */
550 dev->sc_status = *rp->sfas_status;
551 dev->sc_interrupt = *rp->sfas_interrupt;
552 if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
553 dev->sc_resel[0] = *rp->sfas_fifo;
554 dev->sc_resel[1] = *rp->sfas_fifo;
555 }
556 }
557
558 /*
559 * Transfer info to/from device. sfas_ixfer uses polled IO+sfasiwait so the
560 * rules that apply to sfasiwait also applies here.
561 */
562 void
563 sfas_ixfer(void *v, int polling)
564 {
565 struct sfas_softc *dev = v;
566 sfas_regmap_p rp;
567 u_char *buf;
568 int len, mode, phase;
569
570 rp = dev->sc_fas;
571 buf = dev->sc_buf;
572 len = dev->sc_len;
573
574 /*
575 * Decode the scsi phase to determine whether we are reading or writing.
576 * mode == 1 => READ, mode == 0 => WRITE
577 */
578 phase = dev->sc_status & SFAS_STAT_PHASE_MASK;
579 mode = (phase == SFAS_PHASE_DATA_IN);
580
581 while(len && ((dev->sc_status & SFAS_STAT_PHASE_MASK) == phase))
582 if (mode) {
583 *rp->sfas_command = SFAS_CMD_TRANSFER_INFO;
584
585 sfasiwait(dev);
586
587 *buf++ = *rp->sfas_fifo;
588 len--;
589 } else {
590 len--;
591 *rp->sfas_fifo = *buf++;
592 *rp->sfas_command = SFAS_CMD_TRANSFER_INFO;
593
594 sfasiwait(dev);
595 }
596
597 /* Update buffer pointers to reflect the sent/received data. */
598 dev->sc_buf = buf;
599 dev->sc_len = len;
600
601 /*
602 * Since the last sfasiwait will be a phase-change, we can't wait for it
603 * again later, so we have to signal that.
604 * Since this may be called from an interrupt initiated routine then we
605 * must call sfasintr again to avoid losing an interrupt. Phew!
606 */
607 if(polling)
608 dev->sc_flags |= SFAS_DONT_WAIT;
609 else
610 sfasintr(dev);
611 }
612
613 /*
614 * Build a Synchronous Data Transfer Request message
615 */
616 void
617 sfas_build_sdtrm(struct sfas_softc *dev, int period, int offset)
618 {
619 dev->sc_msg_out[0] = 0x01;
620 dev->sc_msg_out[1] = 0x03;
621 dev->sc_msg_out[2] = 0x01;
622 dev->sc_msg_out[3] = period/4;
623 dev->sc_msg_out[4] = offset;
624 dev->sc_msg_out_len= 5;
625 }
626
627 /*
628 * Arbitate the scsi bus and select the unit
629 */
630 int
631 sfas_select_unit(struct sfas_softc *dev, short target)
632 {
633 sfas_regmap_p rp;
634 struct nexus *nexus;
635 int s, retcode, i;
636 u_char cmd;
637
638 s = splbio(); /* Do this at splbio so that we won't be disturbed. */
639
640 retcode = 0;
641
642 nexus = &dev->sc_nexus[target];
643
644 /*
645 * Check if the chip is busy. If not the we mark it as so and hope that nobody
646 * reselects us until we have grabbed the bus.
647 */
648 if (!(dev->sc_flags & SFAS_ACTIVE) && !dev->sc_sel_nexus) {
649 dev->sc_flags |= SFAS_ACTIVE;
650
651 rp = dev->sc_fas;
652
653 *rp->sfas_syncper = nexus->syncper;
654 *rp->sfas_syncoff = nexus->syncoff;
655 *rp->sfas_config3 = nexus->config3;
656
657 *rp->sfas_config1 = dev->sc_config1;
658 *rp->sfas_timeout = dev->sc_timeout_val;
659 *rp->sfas_dest_id = target;
660
661 /* If nobody has stolen the bus, we can send a select command to the chip. */
662 if (!(*rp->sfas_status & SFAS_STAT_INTERRUPT_PENDING)) {
663 *rp->sfas_fifo = nexus->ID;
664 if ((nexus->flags & (SFAS_NF_DO_SDTR | SFAS_NF_RESET))
665 || (dev->sc_msg_out_len != 0))
666 cmd = SFAS_CMD_SEL_ATN_STOP;
667 else {
668 for(i=0; i<nexus->clen; i++)
669 *rp->sfas_fifo = nexus->cbuf[i];
670
671 cmd = SFAS_CMD_SEL_ATN;
672 }
673
674 dev->sc_sel_nexus = nexus;
675
676 *rp->sfas_command = cmd;
677 retcode = 1;
678 nexus->flags &= ~SFAS_NF_RETRY_SELECT;
679 } else
680 nexus->flags |= SFAS_NF_RETRY_SELECT;
681 } else
682 nexus->flags |= SFAS_NF_RETRY_SELECT;
683
684 splx(s);
685 return(retcode);
686 }
687
688 /*
689 * Grab the nexus if available else return 0.
690 */
691 struct nexus *
692 sfas_arbitate_target(struct sfas_softc *dev, int target)
693 {
694 struct nexus *nexus;
695 int s;
696
697 /*
698 * This is realy simple. Raise interrupt level to splbio. Grab the nexus and
699 * leave.
700 */
701 nexus = &dev->sc_nexus[target];
702
703 s = splbio();
704
705 if (nexus->flags & SFAS_NF_UNIT_BUSY)
706 nexus = 0;
707 else
708 nexus->flags |= SFAS_NF_UNIT_BUSY;
709
710 splx(s);
711 return(nexus);
712 }
713
714 /*
715 * Setup a nexus for use. Initializes command, buffer pointers and DMA chain.
716 */
717 void
718 sfas_setup_nexus(struct sfas_softc *dev, struct nexus *nexus, struct sfas_pending *pendp, unsigned char *cbuf, int clen, unsigned char *buf, int len, int mode)
719 {
720 char sync, target, lun;
721
722 target = pendp->xs->xs_periph->periph_target;
723 lun = pendp->xs->xs_periph->periph_lun;
724
725 /*
726 * Adopt mode to reflect the config flags.
727 * If we can't use DMA we can't use synch transfer. Also check the
728 * sfas_inhibit_xxx[target] flags.
729 */
730 if ((dev->sc_config_flags & (SFAS_NO_SYNCH | SFAS_NO_DMA)) ||
731 sfas_inhibit_sync[(int)target])
732 mode &= ~SFAS_SELECT_S;
733
734 if ((dev->sc_config_flags & SFAS_NO_RESELECT) ||
735 sfas_inhibit_disc[(int)target])
736 mode &= ~SFAS_SELECT_R;
737
738 nexus->xs = pendp->xs;
739
740 /* Setup the nexus struct. */
741 nexus->ID = ((mode & SFAS_SELECT_R) ? 0xC0 : 0x80) | lun;
742 nexus->clen = clen;
743 memcpy(nexus->cbuf, cbuf, nexus->clen);
744 nexus->cbuf[1] |= lun << 5; /* Fix the lun bits */
745 nexus->cur_link = 0;
746 nexus->dma_len = 0;
747 nexus->dma_buf = 0;
748 nexus->dma_blk_len = 0;
749 nexus->dma_blk_ptr = 0;
750 nexus->len = len;
751 nexus->buf = buf;
752 nexus->lun_unit = (lun << 4) | target;
753 nexus->state = SFAS_NS_SELECTED;
754
755 /* We must keep these flags. All else must be zero. */
756 nexus->flags &= SFAS_NF_UNIT_BUSY
757 | SFAS_NF_SYNC_TESTED | SFAS_NF_SELECT_ME;
758
759 if (mode & SFAS_SELECT_I)
760 nexus->flags |= SFAS_NF_IMMEDIATE;
761 if (mode & SFAS_SELECT_K)
762 nexus->flags |= SFAS_NF_RESET;
763
764 sync = ((mode & SFAS_SELECT_S) ? 1 : 0);
765
766 /* We can't use sync during polled IO. */
767 if (sync && (mode & SFAS_SELECT_I))
768 sync = 0;
769
770 if (!sync &&
771 ((nexus->flags & SFAS_NF_SYNC_TESTED) && (nexus->offset != 0))) {
772 /*
773 * If the scsi unit is set to synch transfer and we don't want
774 * that, we have to renegotiate.
775 */
776
777 nexus->flags |= SFAS_NF_DO_SDTR;
778 nexus->period = 200;
779 nexus->offset = 0;
780 } else if (sync && !(nexus->flags & SFAS_NF_SYNC_TESTED)) {
781 /*
782 * If the scsi unit is not set to synch transfer and we want
783 * that, we have to negotiate. This should realy base the
784 * period on the clock frequence rather than just check if
785 * >25 MHz
786 */
787
788 nexus->flags |= SFAS_NF_DO_SDTR;
789 nexus->period = ((dev->sc_clock_freq>25) ? 100 : 200);
790 nexus->offset = 8;
791
792 /* If the user has a long cable, we want to limit the period */
793 if ((nexus->period == 100) &&
794 (dev->sc_config_flags & SFAS_SLOW_CABLE))
795 nexus->period = 200;
796 }
797
798 /*
799 * Fake a DMA-block for polled IO. This way we can use the same code to handle
800 * reselection. Much nicer this way.
801 */
802 if ((mode & SFAS_SELECT_I) || (dev->sc_config_flags & SFAS_NO_DMA)) {
803 nexus->dma[0].ptr = buf;
804 nexus->dma[0].len = len;
805 nexus->dma[0].flg = SFAS_CHAIN_PRG;
806 nexus->max_link = 1;
807 } else {
808 nexus->max_link = dev->sc_build_dma_chain(dev, nexus->dma,
809 buf, len);
810 }
811
812 /* Flush the caches. */
813
814 if (len && !(mode & SFAS_SELECT_I))
815 cpu_dcache_wbinv_range((vm_offset_t)buf, len);
816 }
817
818 int
819 sfasselect(struct sfas_softc *dev, struct sfas_pending *pendp, unsigned char *cbuf, int clen, unsigned char *buf, int len, int mode)
820 {
821 struct nexus *nexus;
822
823 /* Get the nexus struct. */
824 nexus = sfas_arbitate_target(dev, pendp->xs->xs_periph->periph_target);
825 if (nexus == NULL)
826 return(0);
827
828 /* Setup the nexus struct. */
829 sfas_setup_nexus(dev, nexus, pendp, cbuf, clen, buf, len, mode);
830
831 /* Post it to the interrupt machine. */
832 sfas_select_unit(dev, pendp->xs->xs_periph->periph_target);
833
834 return(1);
835 }
836
837 void
838 sfasgo(struct sfas_softc *dev, struct sfas_pending *pendp)
839 {
840 int s;
841 char *buf;
842
843 buf = pendp->xs->data;
844
845 if (sfasselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen,
846 buf, pendp->xs->datalen, SFAS_SELECT_RS)) {
847 /*
848 * We got the command going so the sfas_pending struct is now
849 * free to reuse.
850 */
851
852 s = splbio();
853 TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link);
854 splx(s);
855 } else {
856 /*
857 * We couldn't make the command fly so we have to wait. The
858 * struct MUST be inserted at the head to keep the order of
859 * the commands.
860 */
861
862 s = splbio();
863 TAILQ_INSERT_HEAD(&dev->sc_xs_pending, pendp, link);
864 splx(s);
865 }
866
867 return;
868 }
869
870 /*
871 * Part one of the interrupt machine. Error checks and reselection test.
872 * We don't know if we have an active nexus here!
873 */
874 int
875 sfas_pretests(struct sfas_softc *dev, sfas_regmap_p rp)
876 {
877 struct nexus *nexus;
878 int i, s;
879
880 if (dev->sc_interrupt & SFAS_INT_SCSI_RESET_DETECTED) {
881 /*
882 * Cleanup and notify user. Lets hope that this is all we
883 * have to do
884 */
885
886 for(i=0; i<8; i++) {
887 if (dev->sc_nexus[i].xs)
888 sfas_scsidone(dev, dev->sc_nexus[i].xs, -2);
889
890 sfas_init_nexus(dev, &dev->sc_nexus[i]);
891 }
892 printf("sfasintr: SCSI-RESET detected!");
893 return(-1);
894 }
895
896 if (dev->sc_interrupt & SFAS_INT_ILLEGAL_COMMAND) {
897 /* Something went terrible wrong! Dump some data and panic! */
898
899 printf("FIFO:");
900 while(*rp->sfas_fifo_flags & SFAS_FIFO_COUNT_MASK)
901 printf(" %x", *rp->sfas_fifo);
902 printf("\n");
903
904 printf("CMD: %x\n", *rp->sfas_command);
905 panic("sfasintr: ILLEGAL COMMAND!");
906 }
907
908 if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
909 /* We were reselected. Set the chip as busy */
910
911 s = splbio();
912 dev->sc_flags |= SFAS_ACTIVE;
913 if (dev->sc_sel_nexus) {
914 dev->sc_sel_nexus->flags |= SFAS_NF_SELECT_ME;
915 dev->sc_sel_nexus = 0;
916 }
917 splx(s);
918
919 if (dev->sc_units_disconnected) {
920 /* Find out who reselected us. */
921
922 dev->sc_resel[0] &= ~(1<<dev->sc_host_id);
923
924 for(i=0; i<8; i++)
925 if (dev->sc_resel[0] & (1<<i))
926 break;
927
928 if (i == 8)
929 panic("Illegal reselection!");
930
931 if (dev->sc_nexus[i].state == SFAS_NS_DISCONNECTED) {
932 /*
933 * This unit had disconnected, so we reconnect
934 * it.
935 */
936
937 dev->sc_cur_nexus = &dev->sc_nexus[i];
938 nexus = dev->sc_cur_nexus;
939
940 *rp->sfas_syncper = nexus->syncper;
941 *rp->sfas_syncoff = nexus->syncoff;
942 *rp->sfas_config3 = nexus->config3;
943
944 *rp->sfas_dest_id = i & 7;
945
946 dev->sc_units_disconnected--;
947 dev->sc_msg_in_len= 0;
948
949 /* Restore active pointers. */
950 sfas_restore_pointers(dev);
951
952 nexus->state = SFAS_NS_RESELECTED;
953
954 *rp->sfas_command = SFAS_CMD_MESSAGE_ACCEPTED;
955
956 return(1);
957 }
958 }
959
960 /* Somehow we got an illegal reselection. Dump and panic. */
961 printf("sfasintr: resel[0] %x resel[1] %x disconnected %d\n",
962 dev->sc_resel[0], dev->sc_resel[1],
963 dev->sc_units_disconnected);
964 panic("sfasintr: Unexpected reselection!");
965 }
966
967 return(0);
968 }
969
970 /*
971 * Part two of the interrupt machine. Handle disconnection and post command
972 * processing. We know that we have an active nexus here.
973 */
974 int
975 sfas_midaction(struct sfas_softc *dev, sfas_regmap_p rp, struct nexus *nexus)
976 {
977 int i, left, len, s;
978 u_char status, msg;
979
980 if (dev->sc_interrupt & SFAS_INT_DISCONNECT) {
981 s = splbio();
982 dev->sc_cur_nexus = 0;
983
984 /* Mark chip as busy and clean up the chip FIFO. */
985 dev->sc_flags &= ~SFAS_ACTIVE;
986 *rp->sfas_command = SFAS_CMD_FLUSH_FIFO;
987
988 /* Let the nexus state reflect what we have to do. */
989 switch(nexus->state) {
990 case SFAS_NS_SELECTED:
991 dev->sc_sel_nexus = 0;
992 nexus->flags &= ~SFAS_NF_SELECT_ME;
993
994 /*
995 * We were trying to select the unit. Probably no unit
996 * at this ID.
997 */
998 nexus->xs->resid = dev->sc_len;
999
1000 nexus->status = -2;
1001 nexus->flags &= ~SFAS_NF_UNIT_BUSY;
1002 nexus->state = SFAS_NS_FINISHED;
1003 break;
1004
1005 case SFAS_NS_DONE:
1006 /* All done. */
1007 nexus->xs->resid = dev->sc_len;
1008
1009 nexus->flags &= ~SFAS_NF_UNIT_BUSY;
1010 nexus->state = SFAS_NS_FINISHED;
1011 dev->sc_led(dev, 0);
1012 break;
1013
1014 case SFAS_NS_DISCONNECTING:
1015 /*
1016 * We have received a DISCONNECT message, so we are
1017 * doing a normal disconnection.
1018 */
1019 nexus->state = SFAS_NS_DISCONNECTED;
1020
1021 dev->sc_units_disconnected++;
1022 break;
1023
1024 case SFAS_NS_RESET:
1025 /*
1026 * We were reseting this SCSI-unit. Clean up the
1027 * nexus struct.
1028 */
1029 dev->sc_led(dev, 0);
1030 sfas_init_nexus(dev, nexus);
1031 break;
1032
1033 default:
1034 /*
1035 * Unexpected disconnection! Cleanup and exit. This
1036 * shouldn't cause any problems.
1037 */
1038 printf("sfasintr: Unexpected disconnection\n");
1039 printf("sfasintr: u %x s %d p %d f %x c %x\n",
1040 nexus->lun_unit, nexus->state,
1041 dev->sc_status & SFAS_STAT_PHASE_MASK,
1042 nexus->flags, nexus->cbuf[0]);
1043
1044 nexus->xs->resid = dev->sc_len;
1045
1046 nexus->flags &= ~SFAS_NF_UNIT_BUSY;
1047 nexus->state = SFAS_NS_FINISHED;
1048 nexus->status = -3;
1049
1050 dev->sc_led(dev, 0);
1051 break;
1052 }
1053
1054 /*
1055 * If we have disconnected units, we MUST enable reselection
1056 * within 250ms.
1057 */
1058 if (dev->sc_units_disconnected &&
1059 !(dev->sc_flags & SFAS_ACTIVE))
1060 *rp->sfas_command = SFAS_CMD_ENABLE_RESEL;
1061
1062 splx(s);
1063
1064 /* Select the first pre-initialized nexus we find. */
1065 for(i=0; i<8; i++)
1066 if (dev->sc_nexus[i].flags & (SFAS_NF_SELECT_ME | SFAS_NF_RETRY_SELECT))
1067 if (sfas_select_unit(dev, i) == 2)
1068 break;
1069
1070 /* We are done with this nexus! */
1071 if (nexus->state == SFAS_NS_FINISHED)
1072 sfas_scsidone(dev, nexus->xs, nexus->status);
1073
1074 return(1);
1075 }
1076
1077 switch(nexus->state) {
1078 case SFAS_NS_SELECTED:
1079 dev->sc_cur_nexus = nexus;
1080 dev->sc_sel_nexus = 0;
1081
1082 nexus->flags &= ~SFAS_NF_SELECT_ME;
1083
1084 /*
1085 * We have selected a unit. Setup chip, restore pointers and
1086 * light the led.
1087 */
1088 *rp->sfas_syncper = nexus->syncper;
1089 *rp->sfas_syncoff = nexus->syncoff;
1090 *rp->sfas_config3 = nexus->config3;
1091
1092 sfas_restore_pointers(dev);
1093
1094 nexus->status = 0xFF;
1095 dev->sc_msg_in[0] = 0xFF;
1096 dev->sc_msg_in_len= 0;
1097
1098 dev->sc_led(dev, 1);
1099
1100 break;
1101
1102 case SFAS_NS_DATA_IN:
1103 case SFAS_NS_DATA_OUT:
1104 /* We have transfered data. */
1105 if (dev->sc_dma_len)
1106 if (dev->sc_cur_link < dev->sc_max_link) {
1107 /*
1108 * Clean up DMA and at the same time get how
1109 * many bytes that were NOT transfered.
1110 */
1111 left = dev->sc_setup_dma(dev, 0, 0, SFAS_DMA_CLEAR);
1112 len = dev->sc_dma_len;
1113
1114 if (nexus->state == SFAS_NS_DATA_IN) {
1115 /*
1116 * If we were bumping we may have had an odd length
1117 * which means that there may be bytes left in the
1118 * fifo. We also need to move the data from the
1119 * bump buffer to the actual memory.
1120 */
1121 if (dev->sc_dma_buf == dev->sc_bump_pa)
1122 {
1123 while((*rp->sfas_fifo_flags&SFAS_FIFO_COUNT_MASK)
1124 && left)
1125 dev->sc_bump_va[len-(left--)] = *rp->sfas_fifo;
1126
1127 memcpy(dev->sc_buf, dev->sc_bump_va, len-left);
1128 }
1129 } else {
1130 /* Count any unsent bytes and flush them. */
1131 left+= *rp->sfas_fifo_flags & SFAS_FIFO_COUNT_MASK;
1132 *rp->sfas_command = SFAS_CMD_FLUSH_FIFO;
1133 }
1134
1135 /*
1136 * Update pointers/length to reflect the transfered
1137 * data.
1138 */
1139 dev->sc_len -= len-left;
1140 dev->sc_buf += len-left;
1141
1142 dev->sc_dma_buf = (char *)dev->sc_dma_buf + len-left;
1143 dev->sc_dma_len = left;
1144
1145 dev->sc_dma_blk_ptr = (char *)dev->sc_dma_blk_ptr +
1146 len-left;
1147 dev->sc_dma_blk_len -= len-left;
1148
1149 /*
1150 * If it was the end of a DMA block, we select the
1151 * next to begin with.
1152 */
1153 if (!dev->sc_dma_blk_len)
1154 dev->sc_cur_link++;
1155 }
1156 break;
1157
1158 case SFAS_NS_STATUS:
1159 /*
1160 * If we were not sensing, grab the status byte. If we were
1161 * sensing and we got a bad status, let the user know.
1162 */
1163
1164 status = *rp->sfas_fifo;
1165 msg = *rp->sfas_fifo;
1166
1167 nexus->status = status;
1168 if (status != 0)
1169 nexus->status = -1;
1170
1171 /*
1172 * Preload the command complete message. Handeled in
1173 * sfas_postaction.
1174 */
1175 dev->sc_msg_in[0] = msg;
1176 dev->sc_msg_in_len = 1;
1177 nexus->flags |= SFAS_NF_HAS_MSG;
1178 break;
1179
1180 default:
1181 break;
1182 }
1183
1184 return(0);
1185 }
1186
1187 /*
1188 * Part three of the interrupt machine. Handle phase changes (and repeated
1189 * phase passes). We know that we have an active nexus here.
1190 */
1191 int
1192 sfas_postaction(struct sfas_softc *dev, sfas_regmap_p rp, struct nexus *nexus)
1193 {
1194 int i, len;
1195 u_char cmd;
1196 short offset, period;
1197
1198 cmd = 0;
1199
1200 switch(dev->sc_status & SFAS_STAT_PHASE_MASK) {
1201 case SFAS_PHASE_DATA_OUT:
1202 case SFAS_PHASE_DATA_IN:
1203 if ((dev->sc_status & SFAS_STAT_PHASE_MASK) ==
1204 SFAS_PHASE_DATA_OUT)
1205 nexus->state = SFAS_NS_DATA_OUT;
1206 else
1207 nexus->state = SFAS_NS_DATA_IN;
1208
1209 /* Make DMA ready to accept new data. Load active pointers
1210 * from the DMA block. */
1211 dev->sc_setup_dma(dev, 0, 0, SFAS_DMA_CLEAR);
1212 if (dev->sc_cur_link < dev->sc_max_link) {
1213 if (!dev->sc_dma_blk_len) {
1214 dev->sc_dma_blk_ptr = dev->sc_chain[dev->sc_cur_link].ptr;
1215 dev->sc_dma_blk_len = dev->sc_chain[dev->sc_cur_link].len;
1216 dev->sc_dma_blk_flg = dev->sc_chain[dev->sc_cur_link].flg;
1217 }
1218
1219 /* We should use polled IO here. */
1220 if (dev->sc_dma_blk_flg == SFAS_CHAIN_PRG) {
1221 dev->sc_ixfer(dev, nexus->xs->xs_control & XS_CTL_POLL);
1222 dev->sc_cur_link++;
1223 dev->sc_dma_len = 0;
1224 break;
1225 }
1226 else if (dev->sc_dma_blk_flg == SFAS_CHAIN_BUMP)
1227 len = dev->sc_dma_blk_len;
1228 else
1229 len = dev->sc_need_bump(dev, dev->sc_dma_blk_ptr,
1230 dev->sc_dma_blk_len);
1231
1232 /*
1233 * If len != 0 we must bump the data, else we just DMA it
1234 * straight into memory.
1235 */
1236 if (len) {
1237 dev->sc_dma_buf = dev->sc_bump_pa;
1238 dev->sc_dma_len = len;
1239
1240 if (nexus->state == SFAS_NS_DATA_OUT)
1241 memcpy(dev->sc_bump_va, dev->sc_buf, dev->sc_dma_len);
1242 } else {
1243 dev->sc_dma_buf = dev->sc_dma_blk_ptr;
1244 dev->sc_dma_len = dev->sc_dma_blk_len;
1245 }
1246
1247 /* Load DMA with address and length of transfer. */
1248 dev->sc_setup_dma(dev, dev->sc_dma_buf, dev->sc_dma_len,
1249 ((nexus->state == SFAS_NS_DATA_OUT) ?
1250 SFAS_DMA_WRITE : SFAS_DMA_READ));
1251
1252 /* printf("Using DMA !!!!\n");*/
1253 cmd = SFAS_CMD_TRANSFER_INFO | SFAS_CMD_DMA;
1254 } else {
1255 /*
1256 * Hmmm, the unit wants more info than we have or has
1257 * more than we want. Let the chip handle that.
1258 */
1259
1260 *rp->sfas_tc_low = 0; /* was 256 but this does not make sense */
1261 *rp->sfas_tc_mid = 1;
1262 *rp->sfas_tc_high = 0;
1263 cmd = SFAS_CMD_TRANSFER_PAD;
1264 }
1265 break;
1266
1267 case SFAS_PHASE_COMMAND:
1268 /* The scsi unit wants the command, send it. */
1269 nexus->state = SFAS_NS_SVC;
1270
1271 *rp->sfas_command = SFAS_CMD_FLUSH_FIFO;
1272 for(i=0; i<5; i++);
1273
1274 for(i=0; i<nexus->clen; i++)
1275 *rp->sfas_fifo = nexus->cbuf[i];
1276 cmd = SFAS_CMD_TRANSFER_INFO;
1277 break;
1278
1279 case SFAS_PHASE_STATUS:
1280 /*
1281 * We've got status phase. Request status and command
1282 * complete message.
1283 */
1284 nexus->state = SFAS_NS_STATUS;
1285 cmd = SFAS_CMD_COMMAND_COMPLETE;
1286 break;
1287
1288 case SFAS_PHASE_MESSAGE_OUT:
1289 /*
1290 * Either the scsi unit wants us to send a message or we have
1291 * asked for it by seting the ATN bit.
1292 */
1293 nexus->state = SFAS_NS_MSG_OUT;
1294
1295 *rp->sfas_command = SFAS_CMD_FLUSH_FIFO;
1296
1297 if (nexus->flags & SFAS_NF_DO_SDTR) {
1298 /* Send a Synchronous Data Transfer Request. */
1299
1300 sfas_build_sdtrm(dev, nexus->period, nexus->offset);
1301 nexus->flags |= SFAS_NF_SDTR_SENT;
1302 nexus->flags &= ~SFAS_NF_DO_SDTR;
1303 } else if (nexus->flags & SFAS_NF_RESET) {
1304 /* Send a reset scsi unit message. */
1305
1306 dev->sc_msg_out[0] = 0x0C;
1307 dev->sc_msg_out_len = 1;
1308 nexus->state = SFAS_NS_RESET;
1309 nexus->flags &= ~SFAS_NF_RESET;
1310 } else if (dev->sc_msg_out_len == 0) {
1311 /* Don't know what to send so we send a NOP message. */
1312
1313 dev->sc_msg_out[0] = 0x08;
1314 dev->sc_msg_out_len = 1;
1315 }
1316
1317 cmd = SFAS_CMD_TRANSFER_INFO;
1318
1319 for(i=0; i<dev->sc_msg_out_len; i++)
1320 *rp->sfas_fifo = dev->sc_msg_out[i];
1321 dev->sc_msg_out_len = 0;
1322
1323 break;
1324
1325 case SFAS_PHASE_MESSAGE_IN:
1326 /* Receive a message from the scsi unit. */
1327 nexus->state = SFAS_NS_MSG_IN;
1328
1329 while(!(nexus->flags & SFAS_NF_HAS_MSG)) {
1330 *rp->sfas_command = SFAS_CMD_TRANSFER_INFO;
1331 sfasiwait(dev);
1332
1333 dev->sc_msg_in[dev->sc_msg_in_len++] = *rp->sfas_fifo;
1334
1335 /* Check if we got all the bytes in the message. */
1336 if (dev->sc_msg_in[0] >= 0x80) ;
1337 else if (dev->sc_msg_in[0] >= 0x30) ;
1338 else if (((dev->sc_msg_in[0] >= 0x20) &&
1339 (dev->sc_msg_in_len == 2)) ||
1340 ((dev->sc_msg_in[0] != 0x01) &&
1341 (dev->sc_msg_in_len == 1))) {
1342 nexus->flags |= SFAS_NF_HAS_MSG;
1343 break;
1344 } else {
1345 if (dev->sc_msg_in_len >= 2)
1346 if ((dev->sc_msg_in[1]+2) == dev->sc_msg_in_len) {
1347 nexus->flags |= SFAS_NF_HAS_MSG;
1348 break;
1349 }
1350 }
1351
1352 *rp->sfas_command = SFAS_CMD_MESSAGE_ACCEPTED;
1353 sfasiwait(dev);
1354
1355 if ((dev->sc_status & SFAS_STAT_PHASE_MASK) !=
1356 SFAS_PHASE_MESSAGE_IN)
1357 break;
1358 }
1359
1360 cmd = SFAS_CMD_MESSAGE_ACCEPTED;
1361 if (nexus->flags & SFAS_NF_HAS_MSG) {
1362 /* We have a message. Decode it. */
1363
1364 switch(dev->sc_msg_in[0]) {
1365 case 0x00: /* COMMAND COMPLETE */
1366 nexus->state = SFAS_NS_DONE;
1367 break;
1368 case 0x04: /* DISCONNECT */
1369 nexus->state = SFAS_NS_DISCONNECTING;
1370 break;
1371 case 0x02: /* SAVE DATA POINTER */
1372 sfas_save_pointers(dev);
1373 break;
1374 case 0x03: /* RESTORE DATA POINTERS */
1375 sfas_restore_pointers(dev);
1376 break;
1377 case 0x07: /* MESSAGE REJECT */
1378 /*
1379 * If we had sent a SDTR and we got a message
1380 * reject, the scsi docs say that we must go
1381 * to async transfer.
1382 */
1383 if (nexus->flags & SFAS_NF_SDTR_SENT) {
1384 nexus->flags &= ~SFAS_NF_SDTR_SENT;
1385
1386 nexus->config3 &= ~SFAS_CFG3_FASTSCSI;
1387 nexus->syncper = 5;
1388 nexus->syncoff = 0;
1389
1390 *rp->sfas_syncper = nexus->syncper;
1391 *rp->sfas_syncoff = nexus->syncoff;
1392 *rp->sfas_config3 = nexus->config3;
1393 } else
1394 /*
1395 * Something was rejected but we don't know
1396 * what! PANIC!
1397 */
1398 panic("sfasintr: Unknown message rejected!");
1399 break;
1400 case 0x08: /* MO OPERATION */
1401 break;
1402 case 0x01: /* EXTENDED MESSAGE */
1403 switch(dev->sc_msg_in[2]) {
1404 case 0x01:/* SYNC. DATA TRANSFER REQUEST */
1405 /* Decode the SDTR message. */
1406 period = 4*dev->sc_msg_in[3];
1407 offset = dev->sc_msg_in[4];
1408
1409 /*
1410 * Make sure that the specs are within
1411 * chip limits. Note that if we
1412 * initiated the negotiation the specs
1413 * WILL be withing chip limits. If it
1414 * was the scsi unit that initiated
1415 * the negotiation, the specs may be
1416 * to high.
1417 */
1418 if (offset > 16)
1419 offset = 16;
1420 if ((period < 200) &&
1421 (dev->sc_clock_freq <= 25))
1422 period = 200;
1423
1424 if (offset == 0)
1425 period = 5*dev->sc_clock_period;
1426
1427 nexus->syncper = period/
1428 dev->sc_clock_period;
1429 nexus->syncoff = offset;
1430
1431 if (period < 200)
1432 nexus->config3 |= SFAS_CFG3_FASTSCSI;
1433 else
1434 nexus->config3 &=~SFAS_CFG3_FASTSCSI;
1435
1436 nexus->flags |= SFAS_NF_SYNC_TESTED;
1437
1438 *rp->sfas_syncper = nexus->syncper;
1439 *rp->sfas_syncoff = nexus->syncoff;
1440 *rp->sfas_config3 = nexus->config3;
1441
1442 /*
1443 * Hmmm, it seems that the scsi unit
1444 * initiated sync negotiation, so lets
1445 * reply acording to scsi-2 standard.
1446 */
1447 if (!(nexus->flags& SFAS_NF_SDTR_SENT))
1448 {
1449 if ((dev->sc_config_flags &
1450 SFAS_NO_SYNCH) ||
1451 (dev->sc_config_flags &
1452 SFAS_NO_DMA) ||
1453 sfas_inhibit_sync[
1454 nexus->lun_unit & 7]) {
1455 period = 200;
1456 offset = 0;
1457 }
1458
1459 nexus->offset = offset;
1460 nexus->period = period;
1461 nexus->flags |= SFAS_NF_DO_SDTR;
1462 *rp->sfas_command = SFAS_CMD_SET_ATN;
1463 }
1464
1465 nexus->flags &= ~SFAS_NF_SDTR_SENT;
1466 break;
1467
1468 case 0x00: /* MODIFY DATA POINTERS */
1469 case 0x02: /* EXTENDED IDENTIFY (SCSI-1) */
1470 case 0x03: /* WIDE DATA TRANSFER REQUEST */
1471 default:
1472 /* Reject any unhandeled messages. */
1473
1474 dev->sc_msg_out[0] = 0x07;
1475 dev->sc_msg_out_len = 1;
1476 *rp->sfas_command = SFAS_CMD_SET_ATN;
1477 cmd = SFAS_CMD_MESSAGE_ACCEPTED;
1478 break;
1479 }
1480 break;
1481
1482 default:
1483 /* Reject any unhandeled messages. */
1484
1485 dev->sc_msg_out[0] = 0x07;
1486 dev->sc_msg_out_len = 1;
1487 *rp->sfas_command = SFAS_CMD_SET_ATN;
1488 cmd = SFAS_CMD_MESSAGE_ACCEPTED;
1489 break;
1490 }
1491 nexus->flags &= ~SFAS_NF_HAS_MSG;
1492 dev->sc_msg_in_len = 0;
1493 }
1494 break;
1495 default:
1496 printf("SFASINTR: UNKNOWN PHASE! phase: %d\n",
1497 dev->sc_status & SFAS_STAT_PHASE_MASK);
1498 dev->sc_led(dev, 0);
1499 sfas_scsidone(dev, nexus->xs, -4);
1500
1501 return(-1);
1502 }
1503
1504 if (cmd)
1505 *rp->sfas_command = cmd;
1506
1507 return(0);
1508 }
1509
1510 /*
1511 * Stub for interrupt machine.
1512 */
1513 void
1514 sfasintr(struct sfas_softc *dev)
1515 {
1516 sfas_regmap_p rp;
1517 struct nexus *nexus;
1518
1519 rp = dev->sc_fas;
1520
1521 if (!sfas_pretests(dev, rp)) {
1522
1523 nexus = dev->sc_cur_nexus;
1524 if (nexus == NULL)
1525 nexus = dev->sc_sel_nexus;
1526
1527 if (nexus)
1528 if (!sfas_midaction(dev, rp, nexus))
1529 sfas_postaction(dev, rp, nexus);
1530 }
1531 }
1532
1533 /*
1534 * sfasicmd is used to perform IO when we can't use interrupts. sfasicmd
1535 * emulates the normal environment by waiting for the chip and calling
1536 * sfasintr.
1537 */
1538 void
1539 sfasicmd(struct sfas_softc *dev, struct sfas_pending *pendp)
1540 {
1541 sfas_regmap_p rp;
1542 struct nexus *nexus;
1543
1544 nexus = &dev->sc_nexus[pendp->xs->xs_periph->periph_target];
1545 rp = dev->sc_fas;
1546
1547 if (!sfasselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen,
1548 (char *)pendp->xs->data, pendp->xs->datalen,
1549 SFAS_SELECT_I))
1550 panic("sfasicmd: Couldn't select unit");
1551
1552 while(nexus->state != SFAS_NS_FINISHED) {
1553 sfasiwait(dev);
1554 sfasintr(dev);
1555 }
1556
1557 nexus->flags &= ~SFAS_NF_SYNC_TESTED;
1558 }
1559
1560
1561 #ifdef SFAS_DEBUG
1562
1563 void
1564 dump_nexus(struct nexus *nexus)
1565 {
1566 int loop;
1567
1568 printf("nexus=%08x\n", (u_int)nexus);
1569 printf("scsi_fer=%08x\n", (u_int)nexus->xs);
1570 printf("ID=%02x\n", nexus->ID);
1571 printf("clen=%02x\n", nexus->clen);
1572 printf("cbuf=");
1573 for (loop = 0; loop< 14; ++loop)
1574 printf(" %02x\n", nexus->cbuf[loop]);
1575 printf("\n");
1576 printf("DMA:\n");
1577 for (loop = 0; loop < MAXCHAIN; ++loop)
1578 printf("dma_chain: %08x %04x %04x\n", nexus->dma[loop].ptr,
1579 nexus->dma[loop].len, nexus->dma[loop].flg);
1580 printf("\n");
1581
1582 printf("max_link=%d\n", nexus->max_link);
1583 printf("cur_link=%d\n", nexus->cur_link);
1584
1585 printf("buf=%08x\n", (u_int)nexus->buf);
1586 printf("len=%08x\n", nexus->len);
1587 printf("dma_buf=%08x\n", (u_int)nexus->dma_buf);
1588 printf("dma_len=%08x\n", nexus->dma_len);
1589 printf("dma_blk_ptr=%08x\n", (u_int)nexus->dma_blk_ptr);
1590 printf("dma_blk_len=%08x\n", nexus->dma_blk_len);
1591 printf("dma_blk_flag=%08x\n", nexus->dma_blk_flg);
1592 printf("state=%02x\n", nexus->state);
1593 printf("flags=%04x\n", nexus->flags);
1594 printf("period=%d\n", nexus->period);
1595 printf("offset=%d\n", nexus->offset);
1596 printf("syncper=%d\n", nexus->syncper);
1597 printf("syncoff=%d\n", nexus->syncoff);
1598 printf("config3=%02x\n", nexus->config3);
1599 printf("lun_unit=%d\n", nexus->lun_unit);
1600 printf("status=%02x\n", nexus->status);
1601 printf("\n");
1602 }
1603
1604 void
1605 dump_nexii(struct sfas_softc *sc)
1606 {
1607 int loop;
1608
1609 for (loop = 0; loop < 8; ++loop) {
1610 dump_nexus(&sc->sc_nexus[loop]);
1611 }
1612 }
1613
1614 void
1615 dump_sfassoftc(struct sfas_softc *sc)
1616 {
1617 printf("sfassoftc @ 0x%08x\n", (u_int)sc);
1618 printf("clock_freq = %d\n", sc->sc_clock_freq);
1619 printf("timeout = %d\n", sc->sc_timeout);
1620 printf("host_id = %d\n", sc->sc_host_id);
1621 printf("config_flags = 0x%08x\n", sc->sc_config_flags);
1622 printf("led_status = %d\n", sc->sc_led_status);
1623
1624 dump_nexii(sc);
1625 printf("cur_nexus = 0x%08x\n", (u_int)sc->sc_cur_nexus);
1626 printf("sel_nexus = 0x%08x\n", (u_int)sc->sc_sel_nexus);
1627 printf("\n");
1628 }
1629
1630 #endif /* SFAS_DEBUG */
1631