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