1 /*        $NetBSD: seagate.c,v 1.77 2022/09/25 17:11:48 thorpej Exp $ */
2 
3 /*
4  * ST01/02, Future Domain TMC-885, TMC-950 SCSI driver
5  *
6  * Copyright 1994, Charles M. Hannum (mycroft@ai.mit.edu)
7  * Copyright 1994, Kent Palmkvist (kentp@isy.liu.se)
8  * Copyright 1994, Robert Knier (rknier@qgraph.com)
9  * Copyright 1992, 1994 Drew Eckhardt (drew@colorado.edu)
10  * Copyright 1994, Julian Elischer (julian@tfs.com)
11  *
12  * Others that has contributed by example code is
13  *                  Glen Overby (overby@cray.com)
14  *                  Tatu Yllnen
15  *                  Brian E Litzinger
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPERS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 /*
40  * kentp  940307 alpha version based on newscsi-03 version of Julians SCSI-code
41  * kentp  940314 Added possibility to not use messages
42  * rknier 940331 Added fast transfer code
43  * rknier 940407 Added assembler coded data transfers
44  */
45 
46 /*
47  * What should really be done:
48  *
49  * Add missing tests for timeouts
50  * Restructure interrupt enable/disable code (runs to long with int disabled)
51  * Find bug? giving problem with tape status
52  * Add code to handle Future Domain 840, 841, 880 and 881
53  * adjust timeouts (startup is very slow)
54  * add code to use tagged commands in SCSI2
55  * Add code to handle slow devices better (sleep if device not disconnecting)
56  * Fix unnecessary interrupts
57  */
58 
59 /*
60  * Note to users trying to share a disk between DOS and unix:
61  * The ST01/02 is a translating host-adapter. It is not giving DOS
62  * the same number of heads/tracks/sectors as specified by the disk.
63  * It is therefore important to look at what numbers DOS thinks the
64  * disk has. Use these to disklabel your disk in an appropriate manner
65  */
66 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: seagate.c,v 1.77 2022/09/25 17:11:48 thorpej Exp $");
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/errno.h>
74 #include <sys/ioctl.h>
75 #include <sys/device.h>
76 #include <sys/buf.h>
77 #include <sys/proc.h>
78 #include <sys/queue.h>
79 #include <sys/kmem.h>
80 
81 #include <sys/intr.h>
82 #include <machine/pio.h>
83 
84 #include <dev/scsipi/scsi_all.h>
85 #include <dev/scsipi/scsipi_all.h>
86 #include <dev/scsipi/scsi_message.h>
87 #include <dev/scsipi/scsiconf.h>
88 
89 #include <dev/isa/isareg.h>
90 #include <dev/isa/isavar.h>   /* XXX USES ISA HOLE DIRECTLY */
91 
92 #define   SEA_SCB_MAX         32        /* allow maximally 8 scsi control blocks */
93 #define SCB_TABLE_SIZE        8         /* start with 8 scb entries in table */
94 #define BLOCK_SIZE  512       /* size of READ/WRITE areas on SCSI card */
95 
96 /*
97  * defining SEA_BLINDTRANSFER will make DATA IN and DATA OUT to be done with
98  * blind transfers, i.e. no check is done for scsi phase changes. This will
99  * result in data loss if the scsi device does not send its data using
100  * BLOCK_SIZE bytes at a time.
101  * If SEA_BLINDTRANSFER defined and SEA_ASSEMBLER also defined will result in
102  * the use of blind transfers coded in assembler. SEA_ASSEMBLER is no good
103  * without SEA_BLINDTRANSFER defined.
104  */
105 #define   SEA_BLINDTRANSFER   /* do blind transfers */
106 #define   SEA_ASSEMBLER                 /* Use assembly code for fast transfers */
107 
108 /*
109  * defining SEA_NOMSGS causes messages not to be used (thereby disabling
110  * disconnects)
111  */
112 #undef    SEA_NOMSGS
113 
114 /*
115  * defining SEA_NODATAOUT makes dataout phase being aborted
116  */
117 #undef    SEA_NODATAOUT
118 
119 /* Debugging definitions. Should not be used unless you want a lot of
120    printouts even under normal conditions */
121 
122 #undef    SEA_DEBUGQUEUE                /* Display info about queue-lengths */
123 
124 /******************************* board definitions **************************/
125 /*
126  * CONTROL defines
127  */
128 #define CMD_RST               0x01                /* scsi reset */
129 #define CMD_SEL               0x02                /* scsi select */
130 #define CMD_BSY               0x04                /* scsi busy */
131 #define   CMD_ATTN  0x08                /* scsi attention */
132 #define CMD_START_ARB         0x10                /* start arbitration bit */
133 #define   CMD_EN_PARITY       0x20                /* enable scsi parity generation */
134 #define CMD_INTR    0x40                /* enable scsi interrupts */
135 #define CMD_DRVR_ENABLE       0x80                /* scsi enable */
136 
137 /*
138  * STATUS
139  */
140 #define STAT_BSY    0x01                /* scsi busy */
141 #define STAT_MSG    0x02                /* scsi msg */
142 #define STAT_IO               0x04                /* scsi I/O */
143 #define STAT_CD               0x08                /* scsi C/D */
144 #define STAT_REQ    0x10                /* scsi req */
145 #define STAT_SEL    0x20                /* scsi select */
146 #define STAT_PARITY 0x40                /* parity error bit */
147 #define STAT_ARB_CMPL         0x80                /* arbitration complete bit */
148 
149 /*
150  * REQUESTS
151  */
152 #define PH_DATAOUT  (0)
153 #define PH_DATAIN   (STAT_IO)
154 #define PH_CMD                (STAT_CD)
155 #define PH_STAT               (STAT_CD | STAT_IO)
156 #define PH_MSGOUT   (STAT_MSG | STAT_CD)
157 #define PH_MSGIN    (STAT_MSG | STAT_CD | STAT_IO)
158 
159 #define PH_MASK               (STAT_MSG | STAT_CD | STAT_IO)
160 
161 #define PH_INVALID  0xff
162 
163 #define SEA_RAMOFFSET         0x00001800
164 
165 #define BASE_CMD    (CMD_INTR | CMD_EN_PARITY)
166 
167 #define   SEAGATE             1         /* Seagate ST0[12] */
168 #define   FDOMAIN             2         /* Future Domain TMC-{885,950} */
169 #define   FDOMAIN840          3         /* Future Domain TMC-{84[01],88[01]} */
170 
171 /******************************************************************************/
172 
173 /* scsi control block used to keep info about a scsi command */
174 struct sea_scb {
175         u_char *data;                             /* position in data buffer so far */
176           int datalen;                            /* bytes remaining to transfer */
177           TAILQ_ENTRY(sea_scb) chain;
178           struct scsipi_xfer *xs;                 /* the scsipi_xfer for this cmd */
179           int flags;                              /* status of the instruction */
180 #define   SCB_FREE  0
181 #define   SCB_ACTIVE          1
182 #define SCB_ABORTED 2
183 #define SCB_TIMEOUT 4
184 #define SCB_ERROR   8
185 };
186 
187 /*
188  * data structure describing current status of the scsi bus. One for each
189  * controller card.
190  */
191 struct sea_softc {
192           device_t sc_dev;
193           void *sc_ih;
194 
195           int type;                     /* board type */
196           void *    maddr;                        /* Base address for card */
197           void *    maddr_cr_sr;                  /* Address of control and status reg */
198           void *    maddr_dr;           /* Address of data register */
199 
200           struct scsipi_adapter sc_adapter;
201           struct scsipi_channel sc_channel;
202 
203           TAILQ_HEAD(, sea_scb) free_list, ready_list, nexus_list;
204           struct sea_scb *nexus;                  /* currently connected command */
205           int numscbs;                            /* number of scsi control blocks */
206           struct sea_scb scb[SCB_TABLE_SIZE];
207 
208           int our_id;                             /* our scsi id */
209           u_char our_id_mask;
210           volatile u_char busy[8];      /* index=target, bit=lun, Keep track of
211                                                      busy luns at device target */
212 };
213 
214 /* flag showing if main routine is running. */
215 static volatile int main_running = 0;
216 
217 #define   STATUS    (*(volatile u_char *)sea->maddr_cr_sr)
218 #define CONTROL     STATUS
219 #define DATA        (*(volatile u_char *)sea->maddr_dr)
220 
221 /*
222  * These are "special" values for the tag parameter passed to sea_select
223  * Not implemented right now.
224  */
225 #define TAG_NEXT    -1        /* Use next free tag */
226 #define TAG_NONE    -2        /*
227                                          * Establish I_T_L nexus instead of I_T_L_Q
228                                          * even on SCSI-II devices.
229                                          */
230 
231 typedef struct {
232           const char *signature;
233           int offset, length;
234           int type;
235 } BiosSignature;
236 
237 /*
238  * Signatures for automatic recognition of board type
239  */
240 static const BiosSignature signatures[] = {
241 {"ST01 v1.7  (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
242 {"SCSI BIOS 2.00  (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
243 
244 /*
245  * The following two lines are NOT mistakes. One detects ROM revision
246  * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
247  * and this is not going to change, the "SEAGATE" and "SCSI" together
248  * are probably "good enough"
249  */
250 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
251 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
252 
253 /*
254  * However, future domain makes several incompatible SCSI boards, so specific
255  * signatures must be used.
256  */
257 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 45, FDOMAIN},
258 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FDOMAIN},
259 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90",5, 47, FDOMAIN},
260 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90",5, 47, FDOMAIN},
261 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FDOMAIN},
262 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92",   5, 44, FDOMAIN},
263 {"FUTURE DOMAIN TMC-950",                            5, 21, FDOMAIN},
264 };
265 
266 #define   nsignatures         (sizeof(signatures) / sizeof(signatures[0]))
267 
268 #ifdef notdef
269 static const char *bases[] = {
270           (char *) 0xc8000, (char *) 0xca000, (char *) 0xcc000,
271           (char *) 0xce000, (char *) 0xdc000, (char *) 0xde000
272 };
273 
274 #define   nbases              (sizeof(bases) / sizeof(bases[0]))
275 #endif
276 
277 int seaintr(void *);
278 void sea_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t, void *);
279 void sea_timeout(void *);
280 void sea_done(struct sea_softc *, struct sea_scb *);
281 struct sea_scb *sea_get_scb(struct sea_softc *, int);
282 void sea_free_scb(struct sea_softc *, struct sea_scb *, int);
283 static void sea_main(void);
284 static void sea_information_transfer(struct sea_softc *);
285 int sea_poll(struct sea_softc *, struct scsipi_xfer *, int);
286 void sea_init(struct sea_softc *);
287 void sea_send_scb(struct sea_softc *sea, struct sea_scb *scb);
288 void sea_reselect(struct sea_softc *sea);
289 int sea_select(struct sea_softc *sea, struct sea_scb *scb);
290 int sea_transfer_pio(struct sea_softc *sea, u_char *phase,
291     int *count, u_char **data);
292 int sea_abort(struct sea_softc *, struct sea_scb *scb);
293 
294 void      sea_grow_scb(struct sea_softc *);
295 
296 int       seaprobe(device_t, cfdata_t, void *);
297 void      seaattach(device_t, device_t, void *);
298 
299 CFATTACH_DECL_NEW(sea, sizeof(struct sea_softc),
300     seaprobe, seaattach, NULL, NULL);
301 
302 extern struct cfdriver sea_cd;
303 
304 #ifdef SEA_DEBUGQUEUE
305 void
sea_queue_length(struct sea_softc * sea)306 sea_queue_length(struct sea_softc *sea)
307 {
308           struct sea_scb *scb;
309           int connected, issued, disconnected;
310 
311           connected = sea->nexus ? 1 : 0;
312           for (scb = sea->ready_list.tqh_first, issued = 0; scb;
313               scb = scb->chain.tqe_next, issued++);
314           for (scb = sea->nexus_list.tqh_first, disconnected = 0; scb;
315               scb = scb->chain.tqe_next, disconnected++);
316           printf("%s: length: %d/%d/%d\n", device_xname(sea->sc_dev), connected,
317               issued, disconnected);
318 }
319 #endif
320 
321 /*
322  * Check if the device can be found at the port given and if so, detect the
323  * type the type of board.  Set it up ready for further work. Takes the isa_dev
324  * structure from autoconf as an argument.
325  * Returns 1 if card recognized, 0 if errors.
326  */
327 int
seaprobe(device_t parent,cfdata_t match,void * aux)328 seaprobe(device_t parent, cfdata_t match, void *aux)
329 {
330           struct isa_attach_args *ia = aux;
331           int i, type = 0;
332           void *maddr;
333 
334           if (ia->ia_niomem < 1)
335                     return (0);
336           if (ia->ia_nirq < 1)
337                     return (0);
338 
339           if (ISA_DIRECT_CONFIG(ia))
340                     return (0);
341 
342           if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM)
343                     return (0);
344           if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
345                     return (0);
346 
347           /* XXX XXX XXX */
348           maddr = ISA_HOLE_VADDR(ia->ia_iomem[0].ir_addr);
349 
350           /* check board type */        /* No way to define this through config */
351           for (i = 0; i < nsignatures; i++)
352                     if (!memcmp((char *)maddr + signatures[i].offset,
353                         signatures[i].signature, signatures[i].length)) {
354                               type = signatures[i].type;
355                               break;
356                     }
357 
358           /* Find controller and data memory addresses */
359           switch (type) {
360           case SEAGATE:
361           case FDOMAIN840:
362           case FDOMAIN:
363                     break;
364           default:
365 #ifdef SEA_DEBUG
366                     printf("seaprobe: board type unknown at address %p\n", maddr);
367 #endif
368                     return 0;
369           }
370 
371           ia->ia_niomem = 1;
372           ia->ia_iomem[0].ir_size = 0x2000;
373 
374           ia->ia_nirq = 1;
375 
376           ia->ia_nio = 0;
377           ia->ia_ndrq = 0;
378 
379           return 1;
380 }
381 
382 /*
383  * Attach all sub-devices we can find
384  */
385 void
seaattach(device_t parent,device_t self,void * aux)386 seaattach(device_t parent, device_t self, void *aux)
387 {
388           struct isa_attach_args *ia = aux;
389           struct sea_softc *sea = device_private(self);
390           struct scsipi_adapter *adapt = &sea->sc_adapter;
391           struct scsipi_channel *chan = &sea->sc_channel;
392           int i;
393 
394           aprint_naive("\n");
395           sea->sc_dev = self;
396 
397           /* XXX XXX XXX */
398           sea->maddr = ISA_HOLE_VADDR(ia->ia_iomem[0].ir_addr);
399 
400           /* check board type */        /* No way to define this through config */
401           for (i = 0; i < nsignatures; i++)
402                     if (!memcmp((char *)sea->maddr + signatures[i].offset,
403                         signatures[i].signature, signatures[i].length)) {
404                               sea->type = signatures[i].type;
405                               break;
406                     }
407 
408           /* Find controller and data memory addresses */
409           switch (sea->type) {
410           case SEAGATE:
411           case FDOMAIN840:
412                     sea->maddr_cr_sr =
413                         (void *) (((u_char *)sea->maddr) + 0x1a00);
414                     sea->maddr_dr =
415                         (void *) (((u_char *)sea->maddr) + 0x1c00);
416                     break;
417           case FDOMAIN:
418                     sea->maddr_cr_sr =
419                         (void *) (((u_char *)sea->maddr) + 0x1c00);
420                     sea->maddr_dr =
421                         (void *) (((u_char *)sea->maddr) + 0x1e00);
422                     break;
423           default:
424 #ifdef DEBUG
425                     printf("%s: board type unknown at address %p\n",
426                         device_xname(sea->sc_dev), sea->maddr);
427 #endif
428                     return;
429           }
430 
431           /* Test controller RAM (works the same way on future domain cards?) */
432           *((u_char *)sea->maddr + SEA_RAMOFFSET) = 0xa5;
433           *((u_char *)sea->maddr + SEA_RAMOFFSET + 1) = 0x5a;
434 
435           if ((*((u_char *)sea->maddr + SEA_RAMOFFSET) != 0xa5) ||
436               (*((u_char *)sea->maddr + SEA_RAMOFFSET + 1) != 0x5a)) {
437                     aprint_error_dev(sea->sc_dev, "board RAM failure\n");
438                     return;
439           }
440 
441           sea_init(sea);
442 
443           /*
444            * Fill in the scsipi_adapter.
445            */
446           memset(adapt, 0, sizeof(*adapt));
447           adapt->adapt_dev = sea->sc_dev;
448           adapt->adapt_nchannels = 1;
449           adapt->adapt_openings = sea->numscbs;
450           adapt->adapt_max_periph = 1;
451           adapt->adapt_request = sea_scsipi_request;
452           adapt->adapt_minphys = minphys;
453 
454           /*
455            * Fill in the scsipi_channel.
456            */
457           memset(chan, 0, sizeof(*chan));
458           chan->chan_adapter = adapt;
459           chan->chan_bustype = &scsi_bustype;
460           chan->chan_channel = 0;
461           chan->chan_ntargets = 8;
462           chan->chan_nluns = 8;
463           chan->chan_id = sea->our_id;
464           chan->chan_flags = SCSIPI_CHAN_CANGROW;
465 
466           aprint_normal("\n");
467 
468           sea->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
469               IST_EDGE, IPL_BIO, seaintr, sea);
470 
471           /*
472            * ask the adapter what subunits are present
473            */
474           config_found(self, &sea->sc_channel, scsiprint, CFARGS_NONE);
475 }
476 
477 /*
478  * Catch an interrupt from the adaptor
479  */
480 int
seaintr(void * arg)481 seaintr(void *arg)
482 {
483           struct sea_softc *sea = arg;
484 
485 #ifdef DEBUG        /* extra overhead, and only needed for intr debugging */
486           if ((STATUS & STAT_PARITY) == 0 &&
487               (STATUS & (STAT_SEL | STAT_IO)) != (STAT_SEL | STAT_IO))
488                     return 0;
489 #endif
490 
491 loop:
492           /* dispatch to appropriate routine if found and done=0 */
493           /* should check to see that this card really caused the interrupt */
494 
495           if (STATUS & STAT_PARITY) {
496                     /* Parity error interrupt */
497                     aprint_error_dev(sea->sc_dev, "parity error\n");
498                     return 1;
499           }
500 
501           if ((STATUS & (STAT_SEL | STAT_IO)) == (STAT_SEL | STAT_IO)) {
502                     /* Reselect interrupt */
503                     sea_reselect(sea);
504                     if (!main_running)
505                               sea_main();
506                     goto loop;
507           }
508 
509           return 1;
510 }
511 
512 /*
513  * Setup data structures, and reset the board and the SCSI bus.
514  */
515 void
sea_init(struct sea_softc * sea)516 sea_init(struct sea_softc *sea)
517 {
518           int i;
519 
520           /* Reset the scsi bus (I don't know if this is needed */
521           CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_RST;
522           delay(25);          /* hold reset for at least 25 microseconds */
523           CONTROL = BASE_CMD;
524           delay(10);          /* wait a Bus Clear Delay (800 ns + bus free delay (800 ns) */
525 
526           /* Set our id (don't know anything about this) */
527           switch (sea->type) {
528           case SEAGATE:
529                     sea->our_id = 7;
530                     break;
531           case FDOMAIN:
532           case FDOMAIN840:
533                     sea->our_id = 6;
534                     break;
535           }
536           sea->our_id_mask = 1 << sea->our_id;
537 
538           /* init fields used by our routines */
539           sea->nexus = 0;
540           TAILQ_INIT(&sea->ready_list);
541           TAILQ_INIT(&sea->nexus_list);
542           TAILQ_INIT(&sea->free_list);
543           for (i = 0; i < 8; i++)
544                     sea->busy[i] = 0x00;
545 
546           /* link up the free list of scbs */
547           sea->numscbs = SCB_TABLE_SIZE;
548           for (i = 0; i < SCB_TABLE_SIZE; i++) {
549                     TAILQ_INSERT_TAIL(&sea->free_list, &sea->scb[i], chain);
550           }
551 }
552 
553 /*
554  * start a scsi operation given the command and the data address. Also needs
555  * the unit, target and lu.
556  */
557 void
sea_scsipi_request(struct scsipi_channel * chan,scsipi_adapter_req_t req,void * arg)558 sea_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
559     void *arg)
560 {
561           struct scsipi_xfer *xs;
562           struct scsipi_periph *periph __diagused;
563           struct sea_softc *sea = device_private(chan->chan_adapter->adapt_dev);
564           struct sea_scb *scb;
565           int flags;
566           int s;
567 
568           switch (req) {
569           case ADAPTER_REQ_RUN_XFER:
570                     xs = arg;
571                     periph = xs->xs_periph;
572                     flags = xs->xs_control;
573 
574                     SC_DEBUG(periph, SCSIPI_DB2, ("sea_scsipi_requeset\n"));
575 
576                     /* XXX Reset not implemented. */
577                     if (flags & XS_CTL_RESET) {
578                               printf("%s: resetting\n", device_xname(sea->sc_dev));
579                               xs->error = XS_DRIVER_STUFFUP;
580                               scsipi_done(xs);
581                               return;
582                     }
583 
584                     /* Get an SCB to use. */
585                     scb = sea_get_scb(sea, flags);
586 #ifdef DIAGNOSTIC
587                     /*
588                      * This should never happen as we track the resources
589                      * in the mid-layer.
590                      */
591                     if (scb == NULL) {
592                               scsipi_printaddr(periph);
593                               printf("unable to allocate scb\n");
594                               panic("sea_scsipi_request");
595                     }
596 #endif
597 
598                     scb->flags = SCB_ACTIVE;
599                     scb->xs = xs;
600 
601                     /*
602                      * Put all the arguments for the xfer in the scb
603                      */
604                     scb->datalen = xs->datalen;
605                     scb->data = xs->data;
606 
607 #ifdef SEA_DEBUGQUEUE
608                     sea_queue_length(sea);
609 #endif
610 
611                     s = splbio();
612 
613                     sea_send_scb(sea, scb);
614 
615                     if ((flags & XS_CTL_POLL) == 0) {
616                               callout_reset(&scb->xs->xs_callout,
617                                   mstohz(xs->timeout), sea_timeout, scb);
618                               splx(s);
619                               return;
620                     }
621 
622                     splx(s);
623 
624                     /*
625                      * If we can't use interrupts, poll on completion
626                      */
627                     if (sea_poll(sea, xs, xs->timeout)) {
628                               sea_timeout(scb);
629                               if (sea_poll(sea, xs, 2000))
630                                         sea_timeout(scb);
631                     }
632                     return;
633 
634           case ADAPTER_REQ_GROW_RESOURCES:
635                     sea_grow_scb(sea);
636                     return;
637 
638           case ADAPTER_REQ_SET_XFER_MODE:
639               {
640                     struct scsipi_xfer_mode *xm = arg;
641 
642                     /*
643                      * We don't support sync or wide or tagged queueing,
644                      * so announce that now.
645                      */
646                     xm->xm_mode = 0;
647                     xm->xm_period = 0;
648                     xm->xm_offset = 0;
649                     scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
650                     return;
651               }
652           }
653 }
654 
655 /*
656  * Get a free scb. If there are none, see if we can allocate a new one.  If so,
657  * put it in the hash table too; otherwise return an error or sleep.
658  */
659 struct sea_scb *
sea_get_scb(struct sea_softc * sea,int flags)660 sea_get_scb(struct sea_softc *sea, int flags)
661 {
662           int s;
663           struct sea_scb *scb;
664 
665           s = splbio();
666           if ((scb = TAILQ_FIRST(&sea->free_list)) != NULL)
667                     TAILQ_REMOVE(&sea->free_list, scb, chain);
668           splx(s);
669 
670           return (scb);
671 }
672 
673 /*
674  * Try to send this command to the board. Because this board does not use any
675  * mailboxes, this routine simply adds the command to the queue held by the
676  * sea_softc structure.
677  * A check is done to see if the command contains a REQUEST_SENSE command, and
678  * if so the command is put first in the queue, otherwise the command is added
679  * to the end of the queue. ?? Not correct ??
680  */
681 void
sea_send_scb(struct sea_softc * sea,struct sea_scb * scb)682 sea_send_scb(struct sea_softc *sea, struct sea_scb *scb)
683 {
684 
685           TAILQ_INSERT_TAIL(&sea->ready_list, scb, chain);
686           /* Try to do some work on the card. */
687           if (!main_running)
688                     sea_main();
689 }
690 
691 /*
692  * Coroutine that runs as long as more work can be done on the seagate host
693  * adapter in a system.  Both sea_scsi_cmd and sea_intr will try to start it in
694  * case it is not running.
695  */
696 
697 void
sea_main(void)698 sea_main(void)
699 {
700           struct sea_softc *sea;
701           struct sea_scb *scb;
702           int done;
703           int unit;
704           int s;
705 
706           main_running = 1;
707 
708           /*
709            * This should not be run with interrupts disabled, but use the splx
710            * code instead.
711            */
712 loop:
713           done = 1;
714           for (unit = 0; unit < sea_cd.cd_ndevs; unit++) {
715                     sea = device_lookup_private(&sea_cd, unit);
716                     if (!sea)
717                               continue;
718                     s = splbio();
719                     if (!sea->nexus) {
720                               /*
721                                * Search through the ready_list for a command
722                                * destined for a target that's not busy.
723                                */
724                               for (scb = sea->ready_list.tqh_first; scb;
725                                   scb = scb->chain.tqe_next) {
726                                         if ((sea->busy[scb->xs->xs_periph->periph_target] &
727                                             (1 << scb->xs->xs_periph->periph_lun)))
728                                                   continue;
729 
730                                         /* target/lun is not busy */
731                                         TAILQ_REMOVE(&sea->ready_list, scb, chain);
732 
733                                         /* Re-enable interrupts. */
734                                         splx(s);
735 
736                                         /*
737                                          * Attempt to establish an I_T_L nexus.
738                                          * On success, sea->nexus is set.
739                                          * On failure, we must add the command
740                                          * back to the issue queue so we can
741                                          * keep trying.
742                                          */
743 
744                                         /*
745                                          * REQUEST_SENSE commands are issued
746                                          * without tagged queueing, even on
747                                          * SCSI-II devices because the
748                                          * contingent alligence condition
749                                          * exists for the entire unit.
750                                          */
751 
752                                         /*
753                                          * First check that if any device has
754                                          * tried a reconnect while we have done
755                                          * other things with interrupts
756                                          * disabled.
757                                          */
758 
759                                         if ((STATUS & (STAT_SEL | STAT_IO)) ==
760                                             (STAT_SEL | STAT_IO)) {
761                                                   sea_reselect(sea);
762                                                   s = splbio();
763                                                   break;
764                                         }
765                                         if (sea_select(sea, scb)) {
766                                                   s = splbio();
767                                                   TAILQ_INSERT_HEAD(&sea->ready_list,
768                                                       scb, chain);
769                                         } else {
770                                                   s = splbio();
771                                                   break;
772                                         }
773                               }
774                               if (!sea->nexus) {
775                                         /* check for reselection phase */
776                                         if ((STATUS & (STAT_SEL | STAT_IO)) ==
777                                             (STAT_SEL | STAT_IO)) {
778                                                   sea_reselect(sea);
779                                         }
780                               }
781                     } /* if (!sea->nexus) */
782 
783                     splx(s);
784                     if (sea->nexus) {   /* we are connected. Do the task */
785                               sea_information_transfer(sea);
786                               done = 0;
787                     } else
788                               break;
789           } /* for instance */
790 
791           if (!done)
792                     goto loop;
793 
794           main_running = 0;
795 }
796 
797 /*
798  * Allocate an scb and add it to the free list.
799  * We are called at splbio.
800  */
801 void
sea_grow_scb(struct sea_softc * sea)802 sea_grow_scb(struct sea_softc *sea)
803 {
804           struct sea_scb *scb;
805 
806           if (sea->numscbs == SEA_SCB_MAX) {
807                     sea->sc_channel.chan_flags &= ~SCSIPI_CHAN_CANGROW;
808                     return;
809           }
810 
811           scb = kmem_zalloc(sizeof(*scb), KM_NOSLEEP);
812           if (scb == NULL)
813                     return;
814 
815           TAILQ_INSERT_TAIL(&sea->free_list, scb, chain);
816           sea->numscbs++;
817           sea->sc_adapter.adapt_openings++;
818 }
819 void
sea_free_scb(struct sea_softc * sea,struct sea_scb * scb,int flags)820 sea_free_scb(struct sea_softc *sea, struct sea_scb *scb, int flags)
821 {
822           int s;
823 
824           s = splbio();
825           scb->flags = SCB_FREE;
826           TAILQ_INSERT_HEAD(&sea->free_list, scb, chain);
827           splx(s);
828 }
829 
830 void
sea_timeout(void * arg)831 sea_timeout(void *arg)
832 {
833           struct sea_scb *scb = arg;
834           struct scsipi_xfer *xs = scb->xs;
835           struct scsipi_periph *periph = xs->xs_periph;
836           struct sea_softc *sea =
837               device_private(periph->periph_channel->chan_adapter->adapt_dev);
838           int s;
839 
840           scsipi_printaddr(periph);
841           printf("timed out");
842 
843           s = splbio();
844 
845           /*
846            * If it has been through before, then
847            * a previous abort has failed, don't
848            * try abort again
849            */
850           if (scb->flags & SCB_ABORTED) {
851                     /* abort timed out */
852                     printf(" AGAIN\n");
853                     scb->xs->xs_retries = 0;
854                     scb->flags |= SCB_ABORTED;
855                     sea_done(sea, scb);
856           } else {
857                     /* abort the operation that has timed out */
858                     printf("\n");
859                     scb->flags |= SCB_ABORTED;
860                     sea_abort(sea, scb);
861                     /* 2 secs for the abort */
862                     if ((xs->xs_control & XS_CTL_POLL) == 0)
863                               callout_reset(&scb->xs->xs_callout, 2 * hz,
864                                   sea_timeout, scb);
865           }
866 
867           splx(s);
868 }
869 
870 void
sea_reselect(struct sea_softc * sea)871 sea_reselect(struct sea_softc *sea)
872 {
873           u_char target_mask;
874           int i;
875           u_char lun, phase;
876           u_char msg[3];
877           int len;
878           u_char *data;
879           struct sea_scb *scb;
880           int abort = 0;
881 
882           if (!((target_mask = STATUS) & STAT_SEL)) {
883                     printf("%s: wrong state 0x%x\n", device_xname(sea->sc_dev),
884                         target_mask);
885                     return;
886           }
887 
888           /* wait for a device to win the reselection phase */
889           /* signals this by asserting the I/O signal */
890           for (i = 10; i && (STATUS & (STAT_SEL | STAT_IO | STAT_BSY)) !=
891               (STAT_SEL | STAT_IO | 0); i--);
892           /* !! Check for timeout here */
893           /* the data bus contains original initiator id ORed with target id */
894           target_mask = DATA;
895           /* see that we really are the initiator */
896           if (!(target_mask & sea->our_id_mask)) {
897                     printf("%s: polled reselection was not for me: 0x%x\n",
898                         device_xname(sea->sc_dev), target_mask);
899                     return;
900           }
901           /* find target who won */
902           target_mask &= ~sea->our_id_mask;
903           /* host responds by asserting the BSY signal */
904           CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY;
905           /* target should respond by deasserting the SEL signal */
906           for (i = 50000; i && (STATUS & STAT_SEL); i++);
907           /* remove the busy status */
908           CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
909           /* we are connected. Now we wait for the MSGIN condition */
910           for (i = 50000; i && !(STATUS & STAT_REQ); i--);
911           /* !! Add timeout check here */
912           /* hope we get an IDENTIFY message */
913           len = 3;
914           data = msg;
915           phase = PH_MSGIN;
916           sea_transfer_pio(sea, &phase, &len, &data);
917 
918           if (!MSG_ISIDENTIFY(msg[0])) {
919                     printf("%s: expecting IDENTIFY message, got 0x%x\n",
920                         device_xname(sea->sc_dev), msg[0]);
921                     abort = 1;
922                     scb = NULL;
923           } else {
924                     lun = msg[0] & 0x07;
925 
926                     /*
927                      * Find the command corresponding to the I_T_L or I_T_L_Q nexus
928                      * we just reestablished, and remove it from the disconnected
929                      * queue.
930                      */
931                     for (scb = sea->nexus_list.tqh_first; scb;
932                         scb = scb->chain.tqe_next)
933                               if (target_mask == (1 << scb->xs->xs_periph->periph_target) &&
934                                   lun == scb->xs->xs_periph->periph_lun) {
935                                         TAILQ_REMOVE(&sea->nexus_list, scb,
936                                             chain);
937                                         break;
938                               }
939                     if (!scb) {
940                               printf("%s: target %02x lun %d not disconnected\n",
941                                   device_xname(sea->sc_dev), target_mask, lun);
942                               /*
943                                * Since we have an established nexus that we can't do
944                                * anything with, we must abort it.
945                                */
946                               abort = 1;
947                     }
948           }
949 
950           if (abort) {
951                     msg[0] = MSG_ABORT;
952                     len = 1;
953                     data = msg;
954                     phase = PH_MSGOUT;
955                     CONTROL = BASE_CMD | CMD_ATTN;
956                     sea_transfer_pio(sea, &phase, &len, &data);
957           } else
958                     sea->nexus = scb;
959 
960           return;
961 }
962 
963 /*
964  * Transfer data in given phase using polled I/O.
965  */
966 int
sea_transfer_pio(struct sea_softc * sea,u_char * phase,int * count,u_char ** data)967 sea_transfer_pio(struct sea_softc *sea, u_char *phase, int *count, u_char **data)
968 {
969           u_char p = *phase, tmp;
970           int c = *count;
971           u_char *d = *data;
972           int timeout;
973 
974           do {
975                     /*
976                      * Wait for assertion of REQ, after which the phase bits will
977                      * be valid.
978                      */
979                     for (timeout = 0; timeout < 50000; timeout++)
980                               if ((tmp = STATUS) & STAT_REQ)
981                                         break;
982                     if (!(tmp & STAT_REQ)) {
983                               printf("%s: timeout waiting for STAT_REQ\n",
984                                   device_xname(sea->sc_dev));
985                               break;
986                     }
987 
988                     /*
989                      * Check for phase mismatch.  Reached if the target decides
990                      * that it has finished the transfer.
991                      */
992                     if (sea->type == FDOMAIN840)
993                               tmp = ((tmp & 0x08) >> 2) |
994                                     ((tmp & 0x02) << 2) |
995                                      (tmp & 0xf5);
996                     if ((tmp & PH_MASK) != p)
997                               break;
998 
999                     /* Do actual transfer from SCSI bus to/from memory. */
1000                     if (!(p & STAT_IO))
1001                               DATA = *d;
1002                     else
1003                               *d = DATA;
1004                     ++d;
1005 
1006                     /*
1007                      * The SCSI standard suggests that in MSGOUT phase, the
1008                      * initiator should drop ATN on the last byte of the message
1009                      * phase after REQ has been asserted for the handshake but
1010                      * before the initiator raises ACK.
1011                      * Don't know how to accomplish this on the ST01/02.
1012                      */
1013 
1014 #if 0
1015                     /*
1016                      * XXX
1017                      * The st01 code doesn't wait for STAT_REQ to be deasserted.
1018                      * Is this ok?
1019                      */
1020                     for (timeout = 0; timeout < 200000L; timeout++)
1021                               if (!(STATUS & STAT_REQ))
1022                                         break;
1023                     if (STATUS & STAT_REQ)
1024                               printf("%s: timeout on wait for !STAT_REQ",
1025                                   device_xname(sea->sc_dev));
1026 #endif
1027           } while (--c);
1028 
1029           *count = c;
1030           *data = d;
1031           tmp = STATUS;
1032           if (tmp & STAT_REQ)
1033                     *phase = tmp & PH_MASK;
1034           else
1035                     *phase = PH_INVALID;
1036 
1037           if (c && (*phase != p))
1038                     return -1;
1039           return 0;
1040 }
1041 
1042 /*
1043  * Establish I_T_L or I_T_L_Q nexus for new or existing command including
1044  * ARBITRATION, SELECTION, and initial message out for IDENTIFY and queue
1045  * messages.  Return -1 if selection could not execute for some reason, 0 if
1046  * selection succeded or failed because the target did not respond.
1047  */
1048 int
sea_select(struct sea_softc * sea,struct sea_scb * scb)1049 sea_select(struct sea_softc *sea, struct sea_scb *scb)
1050 {
1051           u_char msg[3], phase;
1052           u_char *data;
1053           int len;
1054           int timeout;
1055 
1056           CONTROL = BASE_CMD;
1057           DATA = sea->our_id_mask;
1058           CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_START_ARB;
1059 
1060           /* wait for arbitration to complete */
1061           for (timeout = 0; timeout < 3000000L; timeout++)
1062                     if (STATUS & STAT_ARB_CMPL)
1063                               break;
1064           if (!(STATUS & STAT_ARB_CMPL)) {
1065                     if (STATUS & STAT_SEL) {
1066                               printf("%s: arbitration lost\n", device_xname(sea->sc_dev));
1067                               scb->flags |= SCB_ERROR;
1068                     } else {
1069                               printf("%s: arbitration timeout\n",
1070                                   device_xname(sea->sc_dev));
1071                               scb->flags |= SCB_TIMEOUT;
1072                     }
1073                     CONTROL = BASE_CMD;
1074                     return -1;
1075           }
1076 
1077           delay(2);
1078           DATA = (u_char)((1 << scb->xs->xs_periph->periph_target) |
1079                     sea->our_id_mask);
1080           CONTROL =
1081 #ifdef SEA_NOMSGS
1082               (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_SEL;
1083 #else
1084               (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_SEL | CMD_ATTN;
1085 #endif
1086           delay(1);
1087 
1088           /* wait for a bsy from target */
1089           for (timeout = 0; timeout < 2000000L; timeout++)
1090                     if (STATUS & STAT_BSY)
1091                               break;
1092           if (!(STATUS & STAT_BSY)) {
1093                     /* should return some error to the higher level driver */
1094                     CONTROL = BASE_CMD;
1095                     scb->flags |= SCB_TIMEOUT;
1096                     return 0;
1097           }
1098 
1099           /* Try to make the target to take a message from us */
1100 #ifdef SEA_NOMSGS
1101           CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE;
1102 #else
1103           CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_ATTN;
1104 #endif
1105           delay(1);
1106 
1107           /* should start a msg_out phase */
1108           for (timeout = 0; timeout < 2000000L; timeout++)
1109                     if (STATUS & STAT_REQ)
1110                               break;
1111           /* Remove ATN. */
1112           CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
1113           if (!(STATUS & STAT_REQ)) {
1114                     /*
1115                      * This should not be taken as an error, but more like an
1116                      * unsupported feature!  Should set a flag indicating that the
1117                      * target don't support messages, and continue without failure.
1118                      * (THIS IS NOT AN ERROR!)
1119                      */
1120           } else {
1121                     msg[0] = MSG_IDENTIFY(scb->xs->xs_periph->periph_lun, 1);
1122                     len = 1;
1123                     data = msg;
1124                     phase = PH_MSGOUT;
1125                     /* Should do test on result of sea_transfer_pio(). */
1126                     sea_transfer_pio(sea, &phase, &len, &data);
1127           }
1128           if (!(STATUS & STAT_BSY))
1129                     printf("%s: after successful arbitrate: no STAT_BSY!\n",
1130                         device_xname(sea->sc_dev));
1131 
1132           sea->nexus = scb;
1133           sea->busy[scb->xs->xs_periph->periph_target] |=
1134               1 << scb->xs->xs_periph->periph_lun;
1135           /* This assignment should depend on possibility to send a message to target. */
1136           CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
1137           /* XXX Reset pointer in command? */
1138           return 0;
1139 }
1140 
1141 /*
1142  * Send an abort to the target.  Return 1 success, 0 on failure.
1143  */
1144 int
sea_abort(struct sea_softc * sea,struct sea_scb * scb)1145 sea_abort(struct sea_softc *sea, struct sea_scb *scb)
1146 {
1147           struct sea_scb *tmp;
1148           u_char msg, phase, *msgptr;
1149           int len;
1150 
1151           /*
1152            * If the command hasn't been issued yet, we simply remove it from the
1153            * issue queue
1154            * XXX Could avoid this loop.
1155            */
1156           for (tmp = sea->ready_list.tqh_first; tmp; tmp = tmp->chain.tqe_next)
1157                     if (scb == tmp) {
1158                               TAILQ_REMOVE(&sea->ready_list, scb, chain);
1159                               /* XXX Set some type of error result for operation. */
1160                               return 1;
1161                     }
1162 
1163           /*
1164            * If any commands are connected, we're going to fail the abort and let
1165            * the high level SCSI driver retry at a later time or issue a reset.
1166            */
1167           if (sea->nexus)
1168                     return 0;
1169 
1170           /*
1171            * If the command is currently disconnected from the bus, and there are
1172            * no connected commands, we reconnect the I_T_L or I_T_L_Q nexus
1173            * associated with it, go into message out, and send an abort message.
1174            */
1175           for (tmp = sea->nexus_list.tqh_first; tmp;
1176               tmp = tmp->chain.tqe_next)
1177                     if (scb == tmp) {
1178                               if (sea_select(sea, scb))
1179                                         return 0;
1180 
1181                               msg = MSG_ABORT;
1182                               msgptr = &msg;
1183                               len = 1;
1184                               phase = PH_MSGOUT;
1185                               CONTROL = BASE_CMD | CMD_ATTN;
1186                               sea_transfer_pio(sea, &phase, &len, &msgptr);
1187 
1188                               for (tmp = sea->nexus_list.tqh_first; tmp;
1189                                   tmp = tmp->chain.tqe_next)
1190                                         if (scb == tmp) {
1191                                                   TAILQ_REMOVE(&sea->nexus_list,
1192                                                       scb, chain);
1193                                                   /* XXX Set some type of error result
1194                                                      for the operation. */
1195                                                   return 1;
1196                                         }
1197                     }
1198 
1199           /* Command not found in any queue; race condition? */
1200           return 1;
1201 }
1202 
1203 void
sea_done(struct sea_softc * sea,struct sea_scb * scb)1204 sea_done(struct sea_softc *sea, struct sea_scb *scb)
1205 {
1206           struct scsipi_xfer *xs = scb->xs;
1207 
1208           callout_stop(&scb->xs->xs_callout);
1209 
1210           xs->resid = scb->datalen;
1211 
1212           /* XXXX need to get status */
1213           if (scb->flags == SCB_ACTIVE) {
1214                     xs->resid = 0;
1215           } else {
1216                     if (scb->flags & (SCB_TIMEOUT | SCB_ABORTED))
1217                               xs->error = XS_TIMEOUT;
1218                     if (scb->flags & SCB_ERROR)
1219                               xs->error = XS_DRIVER_STUFFUP;
1220           }
1221           sea_free_scb(sea, scb, xs->xs_control);
1222           scsipi_done(xs);
1223 }
1224 
1225 /*
1226  * Wait for completion of command in polled mode.
1227  */
1228 int
sea_poll(struct sea_softc * sea,struct scsipi_xfer * xs,int count)1229 sea_poll(struct sea_softc *sea, struct scsipi_xfer *xs, int count)
1230 {
1231           int s;
1232 
1233           while (count) {
1234                     /* try to do something */
1235                     s = splbio();
1236                     if (!main_running)
1237                               sea_main();
1238                     splx(s);
1239                     if (xs->xs_status & XS_STS_DONE)
1240                               return 0;
1241                     delay(1000);
1242                     count--;
1243           }
1244           return 1;
1245 }
1246 
1247 /*
1248  * Do the transfer.  We know we are connected.  Update the flags, and call
1249  * sea_done() when task accomplished.  Dialog controlled by the target.
1250  */
1251 void
sea_information_transfer(struct sea_softc * sea)1252 sea_information_transfer(struct sea_softc *sea)
1253 {
1254           int timeout;
1255           u_char msgout = MSG_NOOP;
1256           int len;
1257           int s;
1258           u_char *data;
1259           u_char phase, tmp, old_phase = PH_INVALID;
1260           struct sea_scb *scb = sea->nexus;
1261           int loop;
1262 
1263           for (timeout = 0; timeout < 10000000L; timeout++) {
1264                     tmp = STATUS;
1265                     if (tmp & STAT_PARITY)
1266                               printf("%s: parity error detected\n",
1267                                   device_xname(sea->sc_dev));
1268                     if (!(tmp & STAT_BSY)) {
1269                               for (loop = 0; loop < 20; loop++)
1270                                         if ((tmp = STATUS) & STAT_BSY)
1271                                                   break;
1272                               if (!(tmp & STAT_BSY)) {
1273                                         printf("%s: !STAT_BSY unit in data transfer!\n",
1274                                             device_xname(sea->sc_dev));
1275                                         s = splbio();
1276                                         sea->nexus = NULL;
1277                                         scb->flags = SCB_ERROR;
1278                                         splx(s);
1279                                         sea_done(sea, scb);
1280                                         return;
1281                               }
1282                     }
1283 
1284                     /* we only have a valid SCSI phase when REQ is asserted */
1285                     if (!(tmp & STAT_REQ))
1286                               continue;
1287 
1288                     if (sea->type == FDOMAIN840)
1289                               tmp = ((tmp & 0x08) >> 2) |
1290                                     ((tmp & 0x02) << 2) |
1291                                      (tmp & 0xf5);
1292                     phase = tmp & PH_MASK;
1293                     if (phase != old_phase)
1294                               old_phase = phase;
1295 
1296                     switch (phase) {
1297                     case PH_DATAOUT:
1298 #ifdef SEA_NODATAOUT
1299                               printf("%s: SEA_NODATAOUT set, attempted DATAOUT aborted\n",
1300                                   device_xname(sea->sc_dev));
1301                               msgout = MSG_ABORT;
1302                               CONTROL = BASE_CMD | CMD_ATTN;
1303                               break;
1304 #endif
1305                     case PH_DATAIN:
1306                               if (!scb->data)
1307                                         printf("no data address!\n");
1308 #ifdef SEA_BLINDTRANSFER
1309                               if (scb->datalen && !(scb->datalen % BLOCK_SIZE)) {
1310                                         while (scb->datalen) {
1311                                                   for (loop = 0; loop < 50000; loop++)
1312                                                             if ((tmp = STATUS) & STAT_REQ)
1313                                                                       break;
1314                                                   if (!(tmp & STAT_REQ)) {
1315                                                             printf("%s: timeout waiting for STAT_REQ\n",
1316                                                                 device_xname(sea->sc_dev));
1317                                                             /* XXX Do something? */
1318                                                   }
1319                                                   if (sea->type == FDOMAIN840)
1320                                                             tmp = ((tmp & 0x08) >> 2) |
1321                                                                   ((tmp & 0x02) << 2) |
1322                                                                    (tmp & 0xf5);
1323                                                   if ((tmp & PH_MASK) != phase)
1324                                                             break;
1325                                                   if (!(phase & STAT_IO)) {
1326 #ifdef SEA_ASSEMBLER
1327                                                             void *junk;
1328                                                             __asm("cld\n\t\
1329                                                                 rep\n\t\
1330                                                                 movsl" :
1331                                                                 "=S" (scb->data),
1332                                                                 "=c" (len),
1333                                                                 "=D" (junk) :
1334                                                                 "0" (scb->data),
1335                                                                 "1" (BLOCK_SIZE >> 2),
1336                                                                 "2" (sea->maddr_dr));
1337 #else
1338                                                           for (len = BLOCK_SIZE;
1339                                                                 len; len--)
1340                                                                       DATA = *(scb->data++);
1341 #endif
1342                                                   } else {
1343 #ifdef SEA_ASSEMBLER
1344                                                             void *junk;
1345                                                             __asm("cld\n\t\
1346                                                                 rep\n\t\
1347                                                                 movsl" :
1348                                                                 "=D" (scb->data),
1349                                                                 "=c" (len),
1350                                                                 "=S" (junk) :
1351                                                                 "0" (scb->data),
1352                                                                 "1" (BLOCK_SIZE >> 2),
1353                                                                 "2" (sea->maddr_dr));
1354 #else
1355                                                           for (len = BLOCK_SIZE;
1356                                                                 len; len--)
1357                                                                       *(scb->data++) = DATA;
1358 #endif
1359                                                   }
1360                                                   scb->datalen -= BLOCK_SIZE;
1361                                         }
1362                               }
1363 #endif
1364                               if (scb->datalen)
1365                                         sea_transfer_pio(sea, &phase, &scb->datalen,
1366                                             &scb->data);
1367                               break;
1368                     case PH_MSGIN:
1369                               /* Multibyte messages should not be present here. */
1370                               len = 1;
1371                               data = &tmp;
1372                               sea_transfer_pio(sea, &phase, &len, &data);
1373                               /* scb->MessageIn = tmp; */
1374 
1375                               switch (tmp) {
1376                               case MSG_ABORT:
1377                                         scb->flags = SCB_ABORTED;
1378                                         printf("sea: command aborted by target\n");
1379                                         CONTROL = BASE_CMD;
1380                                         sea_done(sea, scb);
1381                                         return;
1382                               case MSG_CMDCOMPLETE:
1383                                         s = splbio();
1384                                         sea->nexus = NULL;
1385                                         splx(s);
1386                                         sea->busy[scb->xs->xs_periph->periph_target] &=
1387                                             ~(1 << scb->xs->xs_periph->periph_lun);
1388                                         CONTROL = BASE_CMD;
1389                                         sea_done(sea, scb);
1390                                         return;
1391                               case MSG_MESSAGE_REJECT:
1392                                         printf("%s: message_reject received\n",
1393                                             device_xname(sea->sc_dev));
1394                                         break;
1395                               case MSG_DISCONNECT:
1396                                         s = splbio();
1397                                         TAILQ_INSERT_TAIL(&sea->nexus_list,
1398                                             scb, chain);
1399                                         sea->nexus = NULL;
1400                                         CONTROL = BASE_CMD;
1401                                         splx(s);
1402                                         return;
1403                               case MSG_SAVEDATAPOINTER:
1404                               case MSG_RESTOREPOINTERS:
1405                                         /* save/restore of pointers are ignored */
1406                                         break;
1407                               default:
1408                                         /*
1409                                          * This should be handled in the pio data
1410                                          * transfer phase, as the ATN should be raised
1411                                          * before ACK goes false when rejecting a
1412                                          * message.
1413                                          */
1414                                         printf("%s: unknown message in: %x\n",
1415                                             device_xname(sea->sc_dev), tmp);
1416                                         break;
1417                               } /* switch (tmp) */
1418                               break;
1419                     case PH_MSGOUT:
1420                               len = 1;
1421                               data = &msgout;
1422                               /* sea->last_message = msgout; */
1423                               sea_transfer_pio(sea, &phase, &len, &data);
1424                               if (msgout == MSG_ABORT) {
1425                                         printf("%s: sent message abort to target\n",
1426                                             device_xname(sea->sc_dev));
1427                                         s = splbio();
1428                                         sea->busy[scb->xs->xs_periph->periph_target] &=
1429                                             ~(1 << scb->xs->xs_periph->periph_lun);
1430                                         sea->nexus = NULL;
1431                                         scb->flags = SCB_ABORTED;
1432                                         splx(s);
1433                                         /* enable interrupt from scsi */
1434                                         sea_done(sea, scb);
1435                                         return;
1436                               }
1437                               msgout = MSG_NOOP;
1438                               break;
1439                     case PH_CMD:
1440                               len = scb->xs->cmdlen;
1441                               data = (char *) scb->xs->cmd;
1442                               sea_transfer_pio(sea, &phase, &len, &data);
1443                               break;
1444                     case PH_STAT:
1445                               len = 1;
1446                               data = &tmp;
1447                               sea_transfer_pio(sea, &phase, &len, &data);
1448                               scb->xs->status = tmp;
1449                               break;
1450                     default:
1451                               printf("sea: unknown phase\n");
1452                     } /* switch (phase) */
1453           } /* for (...) */
1454 
1455           /* If we get here we have got a timeout! */
1456           printf("%s: timeout in data transfer\n", device_xname(sea->sc_dev));
1457           scb->flags = SCB_TIMEOUT;
1458           /* XXX Should I clear scsi-bus state? */
1459           sea_done(sea, scb);
1460 }
1461