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