]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/wds/wd7000.c
This commit was generated by cvs2svn to compensate for changes in r161655,
[FreeBSD/FreeBSD.git] / sys / dev / wds / wd7000.c
1 /*-
2  * Copyright (c) 1994 Ludd, University of Lule}, Sweden.
3  * Copyright (c) 2000 Sergey A. Babkin
4  * All rights reserved.
5  *
6  * Written by Olof Johansson (offe@ludd.luth.se) 1995.
7  * Based on code written by Theo de Raadt (deraadt@fsa.ca).
8  * Resurrected, ported to CAM and generally cleaned up by Sergey Babkin
9  * <babkin@bellatlantic.net> or <babkin@users.sourceforge.net>.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *     This product includes software developed at Ludd, University of Lule}
22  *     and by the FreeBSD project.
23  * 4. The name of the author may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 /* All bugs are subject to removal without further notice */
43
44 /*
45  * offe 01/07/95
46  * 
47  * This version of the driver _still_ doesn't implement scatter/gather for the
48  * WD7000-FASST2. This is due to the fact that my controller doesn't seem to
49  * support it. That, and the lack of documentation makes it impossible for me
50  * to implement it. What I've done instead is allocated a local buffer,
51  * contiguous buffer big enough to handle the requests. I haven't seen any
52  * read/write bigger than 64k, so I allocate a buffer of 64+16k. The data
53  * that needs to be DMA'd to/from the controller is copied to/from that
54  * buffer before/after the command is sent to the card.
55  * 
56  * SB 03/30/00
57  * 
58  * An intermediate buffer is needed anyway to make sure that the buffer is
59  * located under 16MB, otherwise it's out of reach of ISA cards. I've added
60  * optimizations to allocate space in buffer in fragments.
61  */
62
63 /*
64  * Jumpers: (see The Ref(TM) for more info)
65  * W1/W2 - interrupt selection:
66  *  W1 (1-2) IRQ3, (3-4) IRQ4, (5-6) IRQ5, (7-8) IRQ7, (9-10) IRQ9
67  *  W2 (21-22) IRQ10, (19-20) IRQ11, (17-18) IRQ12, (15-16) IRQ14, (13-14) IRQ15
68  *
69  * W2 - DRQ/DACK selection, DRQ and DACK must be the same:
70  *  (5-6) DRQ5 (11-12) DACK5
71  *  (3-4) DRQ6 (9-10) DACK6
72  *  (1-2) DRQ7 (7-8) DACK7
73  *
74  * W3 - I/O address selection: open pair of pins (OFF) means 1, jumpered (ON) means 0
75  *  pair (1-2) is bit 3, ..., pair (9-10) is bit 7. All the other bits are equal
76  *  to the value 0x300. In bitwise representation that would be:
77  *   0 0 1 1 (9-10) (7-8) (5-6) (3-4) (1-2) 0 0 0
78  *  For example, address 0x3C0, bitwise 1111000000 will be represented as:
79  *   (9-10) OFF, (7-8) OFF, (5-6) ON, (3-4) ON, (1-2) ON
80  * 
81  * W4 - BIOS address: open pair of pins (OFF) means 1, jumpered (ON) means 0
82  *  pair (1-2) is bit 13, ..., pair (7-8) is bit 16. All the other bits are
83  *  equal to the value 0xC0000. In bitwise representation that would be:
84  *   1 1 0 (7-8) (5-6) (3-4) (1-2) 0 0000 0000 0000
85  *  For example, address 0xD8000 will be represented as:
86  *   (7-8) OFF, (5-6) OFF, (3-4) ON, (1-2) ON
87  *
88  * W98 (on newer cards) - BIOS enabled; on older cards just remove the BIOS
89  * chip to disable it
90  * W99 (on newer cards) - ROM size (1-2) OFF, (3-4) ON
91  *
92  * W5 - terminator power
93  *  ON - host supplies term. power
94  *  OFF - target supplies term. power
95  *
96  * W6, W9 - floppy support (a bit cryptic):
97  *  W6 ON, W9 ON - disabled
98  *  W6 OFF, W9 ON - enabled with HardCard only
99  *  W6 OFF, W9 OFF - enabled with no hardCard or Combo
100  *
101  * Default: I/O 0x350, IRQ15, DMA6
102  */
103
104 /*
105  * debugging levels: 
106  * 0 - disabled 
107  * 1 - print debugging messages 
108  * 2 - collect  debugging messages in an internal log buffer which can be 
109  *     printed later by calling wds_printlog from DDB 
110  *
111  * Both kind of logs are heavy and interact significantly with the timing 
112  * of commands, so the observed problems may become invisible if debug 
113  * logging is enabled.
114  * 
115  * The light-weight logging facility may be enabled by defining
116  * WDS_ENABLE_SMALLOG as 1. It has very little overhead and allows observing 
117  * the traces of various race conditions without affectiong them but the log is
118  * quite terse. The small log can be printer from DDB by calling
119  * wds_printsmallog.
120  */
121 #ifndef WDS_DEBUG
122 #define WDS_DEBUG 0
123 #endif
124
125 #ifndef WDS_ENABLE_SMALLOG 
126 #define WDS_ENABLE_SMALLOG 0
127 #endif
128
129 #include <sys/types.h>
130 #include <sys/param.h>
131 #include <sys/systm.h>
132 #include <sys/errno.h>
133 #include <sys/kernel.h>
134 #include <sys/assym.h>
135
136 #include <sys/bio.h>
137 #include <sys/buf.h>
138
139 #include <cam/cam.h>
140 #include <cam/cam_ccb.h>
141 #include <cam/cam_sim.h>
142 #include <cam/cam_xpt_sim.h>
143 #include <cam/cam_debug.h>
144 #include <cam/scsi/scsi_all.h>
145 #include <cam/scsi/scsi_message.h>
146
147
148 #include <vm/vm.h>
149 #include <vm/vm_param.h>
150 #include <vm/pmap.h>
151
152 #include <sys/module.h>
153 #include <sys/bus.h>
154 #include <machine/bus.h>
155 #include <machine/resource.h>
156 #include <sys/rman.h>
157
158 #include <isa/isavar.h>
159 #include <isa/pnpvar.h>
160
161 #define WDSTOPHYS(wp, a)        ( ((u_long)a) - ((u_long)wp->dx) + ((u_long)wp->dx_p) )
162 #define WDSTOVIRT(wp, a)        ( ((char *)a) - ((char*)wp->dx_p) + ((char *)wp->dx) )
163
164 /* 0x10000 (64k) should be enough. But just to be sure... */
165 #define BUFSIZ          0x12000
166 /* buffer fragment size, no more than 32 frags per buffer */
167 #define FRAGSIZ         0x1000
168
169
170 /* WD7000 registers */
171 #define WDS_STAT                0       /* read */
172 #define WDS_IRQSTAT             1       /* read */
173
174 #define WDS_CMD                 0       /* write */
175 #define WDS_IRQACK              1       /* write */
176 #define WDS_HCR                 2       /* write */
177
178 #define WDS_NPORTS              4 /* number of ports used */
179
180 /* WDS_STAT (read) defs */
181 #define WDS_IRQ                 0x80
182 #define WDS_RDY                 0x40
183 #define WDS_REJ                 0x20
184 #define WDS_INIT                0x10
185
186 /* WDS_IRQSTAT (read) defs */
187 #define WDSI_MASK               0xc0
188 #define WDSI_ERR                0x00
189 #define WDSI_MFREE              0x80
190 #define WDSI_MSVC               0xc0
191
192 /* WDS_CMD (write) defs */
193 #define WDSC_NOOP               0x00
194 #define WDSC_INIT               0x01
195 #define WDSC_DISUNSOL           0x02 /* disable unsolicited ints */
196 #define WDSC_ENAUNSOL           0x03 /* enable unsolicited ints */
197 #define WDSC_IRQMFREE           0x04 /* interrupt on free RQM */
198 #define WDSC_SCSIRESETSOFT      0x05 /* soft reset */
199 #define WDSC_SCSIRESETHARD      0x06 /* hard reset ack */
200 #define WDSC_MSTART(m)          (0x80 + (m)) /* start mailbox */
201 #define WDSC_MMSTART(m)         (0xc0 + (m)) /* start all mailboxes */
202
203 /* WDS_HCR (write) defs */
204 #define WDSH_IRQEN              0x08
205 #define WDSH_DRQEN              0x04
206 #define WDSH_SCSIRESET          0x02
207 #define WDSH_ASCRESET           0x01
208
209 struct wds_cmd {
210         u_int8_t        cmd;
211         u_int8_t        targ;
212         u_int8_t        scb[12];
213         u_int8_t        stat;
214         u_int8_t        venderr;
215         u_int8_t        len[3];
216         u_int8_t        data[3];
217         u_int8_t        next[3];
218         u_int8_t        write;
219         u_int8_t        xx[6];
220 };
221
222 struct wds_req {
223         struct     wds_cmd cmd;
224         union      ccb *ccb;
225         enum {
226                 WR_DONE = 0x01,
227                 WR_SENSE = 0x02
228         } flags;
229         u_int8_t  *buf;         /* address of linear data buffer */
230         u_int32_t  mask;        /* mask of allocated fragments */
231         u_int8_t        ombn;
232         u_int8_t        id;     /* number of request */
233 };
234
235 #define WDSX_SCSICMD            0x00
236 #define WDSX_OPEN_RCVBUF        0x80
237 #define WDSX_RCV_CMD            0x81
238 #define WDSX_RCV_DATA           0x82
239 #define WDSX_RCV_DATASTAT       0x83
240 #define WDSX_SND_DATA           0x84
241 #define WDSX_SND_DATASTAT       0x85
242 #define WDSX_SND_CMDSTAT        0x86
243 #define WDSX_READINIT           0x88
244 #define WDSX_READSCSIID         0x89
245 #define WDSX_SETUNSOLIRQMASK    0x8a
246 #define WDSX_GETUNSOLIRQMASK    0x8b
247 #define WDSX_GETFIRMREV         0x8c
248 #define WDSX_EXECDIAG           0x8d
249 #define WDSX_SETEXECPARM        0x8e
250 #define WDSX_GETEXECPARM        0x8f
251
252 struct wds_mb {
253         u_int8_t        stat;
254         u_int8_t        addr[3];
255 };
256 /* ICMB status value */
257 #define ICMB_OK                 0x01
258 #define ICMB_OKERR              0x02
259 #define ICMB_ETIME              0x04
260 #define ICMB_ERESET             0x05
261 #define ICMB_ETARCMD            0x06
262 #define ICMB_ERESEL             0x80
263 #define ICMB_ESEL               0x81
264 #define ICMB_EABORT             0x82
265 #define ICMB_ESRESET            0x83
266 #define ICMB_EHRESET            0x84
267
268 struct wds_setup {
269         u_int8_t        cmd;
270         u_int8_t        scsi_id;
271         u_int8_t        buson_t;
272         u_int8_t        busoff_t;
273         u_int8_t        xx;
274         u_int8_t        mbaddr[3];
275         u_int8_t        nomb;
276         u_int8_t        nimb;
277 };
278
279 /* the code depends on equality of these parameters */
280 #define MAXSIMUL        8
281 #define WDS_NOMB        MAXSIMUL
282 #define WDS_NIMB        MAXSIMUL
283
284 static int      fragsiz;
285 static int      nfrags;
286
287 /* structure for data exchange with controller */
288
289 struct wdsdx {
290         struct wds_req  req[MAXSIMUL];
291         struct wds_mb   ombs[MAXSIMUL];
292         struct wds_mb   imbs[MAXSIMUL];
293         u_int8_t        data[BUFSIZ];
294 };
295
296 /* structure softc */
297
298 struct wds {
299         device_t         dev;
300         int              unit;
301         int              addr;
302         int              drq;
303         struct cam_sim  *sim;   /* SIM descriptor for this card */
304         struct cam_path *path;  /* wildcard path for this card */
305         char             want_wdsr;     /* resource shortage flag */
306         u_int32_t        data_free;
307         u_int32_t        wdsr_free;
308         struct wdsdx    *dx;
309         struct wdsdx    *dx_p; /* physical address */
310         struct resource *port_r;
311         int              port_rid;
312         struct resource *drq_r;
313         int              drq_rid;
314         struct resource *intr_r;
315         int              intr_rid;
316         void            *intr_cookie;
317         bus_dma_tag_t    bustag;
318         bus_dmamap_t     busmap;
319 };
320
321 #define ccb_wdsr        spriv_ptr1      /* for wds request */
322
323 static int      wds_probe(device_t dev);
324 static int      wds_attach(device_t dev);
325 static void     wds_intr(struct wds *wp);
326
327 static void     wds_action(struct cam_sim * sim, union ccb * ccb);
328 static void     wds_poll(struct cam_sim * sim);
329
330 static int      wds_preinit(struct wds *wp);
331 static int      wds_init(struct wds *wp);
332
333 static void     wds_alloc_callback(void *arg, bus_dma_segment_t *seg,  
334          int nseg, int error);
335 static void     wds_free_resources(struct wds *wp);
336
337 static struct wds_req *wdsr_alloc(struct wds *wp);
338
339 static void     wds_scsi_io(struct cam_sim * sim, struct ccb_scsiio * csio);
340 static void     wdsr_ccb_done(struct wds *wp, struct wds_req *r, 
341                               union ccb *ccb, u_int32_t status);
342
343 static void     wds_done(struct wds *wp, struct wds_req *r, u_int8_t stat);
344 static int      wds_runsense(struct wds *wp, struct wds_req *r);
345 static int      wds_getvers(struct wds *wp);
346
347 static int      wds_cmd(int base, u_int8_t * p, int l);
348 static void     wds_wait(int reg, int mask, int val);
349
350 static struct wds_req *cmdtovirt(struct wds *wp, u_int32_t phys);
351
352 static u_int32_t frag_alloc(struct wds *wp, int size, u_int8_t **res, 
353                             u_int32_t *maskp);
354 static void     frag_free(struct wds *wp, u_int32_t mask);
355
356 void            wds_print(void);
357
358 #if WDS_ENABLE_SMALLOG==1
359 static __inline void   smallog(char c);
360 void    wds_printsmallog(void);
361 #endif /* SMALLOG */
362
363 /* SCSI ID of the adapter itself */
364 #ifndef WDS_HBA_ID
365 #define WDS_HBA_ID 7
366 #endif
367
368 #if WDS_DEBUG == 2
369 #define LOGLINESIZ      81
370 #define NLOGLINES       300
371 #define DBX     wds_nextlog(), LOGLINESIZ,
372 #define DBG     snprintf
373
374 static char     wds_log[NLOGLINES][LOGLINESIZ];
375 static int      logwrite = 0, logread = 0;
376 static char    *wds_nextlog(void);
377 void            wds_printlog(void);
378
379 #elif WDS_DEBUG != 0
380 #define DBX
381 #define DBG     printf
382 #else
383 #define DBX
384 #define DBG     if(0) printf
385 #endif
386
387 /* the table of supported bus methods */
388 static device_method_t wds_isa_methods[] = {
389         DEVMETHOD(device_probe,         wds_probe),
390         DEVMETHOD(device_attach,        wds_attach),
391         { 0, 0 }
392 };
393
394 static driver_t wds_isa_driver = {
395         "wds",
396         wds_isa_methods,
397         sizeof(struct wds),
398 };
399
400 static devclass_t wds_devclass;
401
402 DRIVER_MODULE(wds, isa, wds_isa_driver, wds_devclass, 0, 0);
403
404 #if WDS_ENABLE_SMALLOG==1
405 #define SMALLOGSIZ      512
406 static char      wds_smallog[SMALLOGSIZ];
407 static char     *wds_smallogp = wds_smallog;
408 static char      wds_smallogover = 0;
409
410 static __inline void
411 smallog(char c)
412 {
413         *wds_smallogp = c;
414         if (++wds_smallogp == &wds_smallog[SMALLOGSIZ]) {
415                 wds_smallogp = wds_smallog;
416                 wds_smallogover = 1;
417         }
418 }
419
420 #define smallog2(a, b)  (smallog(a), smallog(b))
421 #define smallog3(a, b, c)       (smallog(a), smallog(b), smallog(c))
422 #define smallog4(a, b, c, d)    (smallog(a),smallog(b),smallog(c),smallog(d))
423
424 void 
425 wds_printsmallog(void)
426 {
427         int      i;
428         char    *p;
429
430         printf("wds: ");
431         p = wds_smallogover ? wds_smallogp : wds_smallog;
432         i = 0;
433         do {
434                 printf("%c", *p);
435                 if (++p == &wds_smallog[SMALLOGSIZ])
436                         p = wds_smallog;
437                 if (++i == 70) {
438                         i = 0;
439                         printf("\nwds: ");
440                 }
441         } while (p != wds_smallogp);
442         printf("\n");
443 }
444 #else
445 #define smallog(a)
446 #define smallog2(a, b)
447 #define smallog3(a, b, c)
448 #define smallog4(a, b, c, d)
449 #endif                          /* SMALLOG */
450
451 static int
452 wds_probe(device_t dev)
453 {
454         struct  wds *wp;
455         int     error = 0;
456         int     irq;
457
458         /* No pnp support */
459         if (isa_get_vendorid(dev))
460                 return (ENXIO);
461
462         wp = (struct wds *) device_get_softc(dev);
463         wp->unit = device_get_unit(dev);
464         wp->dev = dev;
465
466         wp->addr = bus_get_resource_start(dev, SYS_RES_IOPORT, 0 /*rid*/);
467         if (wp->addr == 0 || wp->addr <0x300
468          || wp->addr > 0x3f8 || wp->addr & 0x7) {
469                 device_printf(dev, "invalid port address 0x%x\n", wp->addr);
470                 return (ENXIO);
471         }
472
473         if (bus_set_resource(dev, SYS_RES_IOPORT, 0, wp->addr, WDS_NPORTS) < 0)
474                 return (ENXIO);
475
476         /* get the DRQ */
477         wp->drq = bus_get_resource_start(dev, SYS_RES_DRQ, 0 /*rid*/);
478         if (wp->drq < 5 || wp->drq > 7) {
479                 device_printf(dev, "invalid DRQ %d\n", wp->drq);
480                 return (ENXIO);
481         }
482
483         /* get the IRQ */
484         irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0 /*rid*/);
485         if (irq < 3) {
486                 device_printf(dev, "invalid IRQ %d\n", irq);
487                 return (ENXIO);
488         }
489
490         wp->port_rid = 0;
491         wp->port_r = bus_alloc_resource(dev, SYS_RES_IOPORT,  &wp->port_rid,
492                                         /*start*/ 0, /*end*/ ~0,
493                                         /*count*/ 0, RF_ACTIVE);
494         if (wp->port_r == NULL)
495                 return (ENXIO);
496
497         error = wds_preinit(wp);
498
499         /*
500          * We cannot hold resources between probe and
501          * attach as we may never be attached.
502          */
503         wds_free_resources(wp);
504
505         return (error);
506 }
507
508 static int
509 wds_attach(device_t dev)
510 {
511         struct  wds *wp;
512         struct  cam_devq *devq;
513         struct  cam_sim *sim;
514         struct  cam_path *pathp;
515         int     i;
516         int     error = 0;
517
518         wp = (struct wds *)device_get_softc(dev);
519
520         wp->port_rid = 0;
521         wp->port_r = bus_alloc_resource(dev, SYS_RES_IOPORT,  &wp->port_rid,
522                                         /*start*/ 0, /*end*/ ~0,
523                                         /*count*/ 0, RF_ACTIVE);
524         if (wp->port_r == NULL)
525                 return (ENXIO);
526
527         /* We must now release resources on error. */
528
529         wp->drq_rid = 0;
530         wp->drq_r = bus_alloc_resource(dev, SYS_RES_DRQ,  &wp->drq_rid,
531                                        /*start*/ 0, /*end*/ ~0,
532                                        /*count*/ 0, RF_ACTIVE);
533         if (wp->drq_r == NULL)
534                 goto bad;
535
536         wp->intr_rid = 0;
537         wp->intr_r = bus_alloc_resource(dev, SYS_RES_IRQ,  &wp->intr_rid,
538                                         /*start*/ 0, /*end*/ ~0,
539                                         /*count*/ 0, RF_ACTIVE);
540         if (wp->intr_r == NULL)
541                 goto bad;
542         error = bus_setup_intr(dev, wp->intr_r, INTR_TYPE_CAM | INTR_ENTROPY,
543                                (driver_intr_t *)wds_intr, (void *)wp,
544                                &wp->intr_cookie);
545         if (error)
546                 goto bad;
547
548         /* now create the memory buffer */
549         error = bus_dma_tag_create(NULL, /*alignment*/4,
550                                    /*boundary*/0,
551                                    /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
552                                    /*highaddr*/ BUS_SPACE_MAXADDR,
553                                    /*filter*/ NULL, /*filterarg*/ NULL,
554                                    /*maxsize*/ sizeof(* wp->dx),
555                                    /*nsegments*/ 1,
556                                    /*maxsegsz*/ sizeof(* wp->dx), /*flags*/ 0,
557                                    /*lockfunc*/busdma_lock_mutex,
558                                    /*lockarg*/&Giant,
559                                    &wp->bustag);
560         if (error)
561                 goto bad;
562
563         error = bus_dmamem_alloc(wp->bustag, (void **)&wp->dx,
564                                  /*flags*/ 0, &wp->busmap);
565         if (error)
566                 goto bad;
567             
568         bus_dmamap_load(wp->bustag, wp->busmap, (void *)wp->dx,
569                         sizeof(* wp->dx), wds_alloc_callback,
570                         (void *)&wp->dx_p, /*flags*/0);
571
572         /* initialize the wds_req structures on this unit */
573         for(i=0; i<MAXSIMUL; i++)  {
574                 wp->dx->req[i].id = i;
575                 wp->wdsr_free |= 1<<i;
576         }
577
578         /* initialize the memory buffer allocation for this unit */
579         if (BUFSIZ / FRAGSIZ > 32) {
580                 fragsiz = (BUFSIZ / 32) & ~0x01; /* keep it word-aligned */
581                 device_printf(dev, "data buffer fragment size too small.  "
582                               "BUFSIZE / FRAGSIZE must be <= 32\n");
583         } else
584                 fragsiz = FRAGSIZ & ~0x01; /* keep it word-aligned */
585
586         wp->data_free = 0;
587         nfrags = 0;
588         for (i = fragsiz; i <= BUFSIZ; i += fragsiz) {
589                 nfrags++;
590                 wp->data_free = (wp->data_free << 1) | 1;
591         }
592
593         /* complete the hardware initialization */
594         if (wds_init(wp) != 0)
595                 goto bad;
596
597         if (wds_getvers(wp) == -1)
598                 device_printf(dev, "getvers failed\n");
599         device_printf(dev, "using %d bytes / %d frags for dma buffer\n",
600                       BUFSIZ, nfrags);
601
602         devq = cam_simq_alloc(MAXSIMUL);
603         if (devq == NULL)
604                 goto bad;
605
606         sim = cam_sim_alloc(wds_action, wds_poll, "wds", (void *) wp,
607                             wp->unit, 1, 1, devq);
608         if (sim == NULL) {
609                 cam_simq_free(devq);
610                 goto bad;
611         }
612         wp->sim = sim;
613
614         if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
615                 cam_sim_free(sim, /* free_devq */ TRUE);
616                 goto bad;
617         }
618         if (xpt_create_path(&pathp, /* periph */ NULL,
619                             cam_sim_path(sim), CAM_TARGET_WILDCARD,
620                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
621                 xpt_bus_deregister(cam_sim_path(sim));
622                 cam_sim_free(sim, /* free_devq */ TRUE);
623                 goto bad;
624         }
625         wp->path = pathp;
626
627         return (0);
628
629 bad:
630         wds_free_resources(wp);
631         if (error)  
632                 return (error);
633         else /* exact error is unknown */
634                 return (ENXIO);
635 }
636
637 /* callback to save the physical address */
638 static void     
639 wds_alloc_callback(void *arg, bus_dma_segment_t *seg,  int nseg, int error)
640 {
641         *(bus_addr_t *)arg = seg[0].ds_addr;
642 }
643
644 static void     
645 wds_free_resources(struct wds *wp)
646 {
647         /* check every resource and free if not zero */
648             
649         /* interrupt handler */
650         if (wp->intr_r) {
651                 bus_teardown_intr(wp->dev, wp->intr_r, wp->intr_cookie);
652                 bus_release_resource(wp->dev, SYS_RES_IRQ, wp->intr_rid,
653                                      wp->intr_r);
654                 wp->intr_r = 0;
655         }
656
657         /* all kinds of memory maps we could have allocated */
658         if (wp->dx_p) {
659                 bus_dmamap_unload(wp->bustag, wp->busmap);
660                 wp->dx_p = 0;
661         }
662         if (wp->dx) { /* wp->busmap may be legitimately equal to 0 */
663                 /* the map will also be freed */
664                 bus_dmamem_free(wp->bustag, wp->dx, wp->busmap);
665                 wp->dx = 0;
666         }
667         if (wp->bustag) {
668                 bus_dma_tag_destroy(wp->bustag);
669                 wp->bustag = 0;
670         }
671         /* release all the bus resources */
672         if (wp->drq_r) {
673                 bus_release_resource(wp->dev, SYS_RES_DRQ,
674                                      wp->drq_rid, wp->drq_r);
675                 wp->drq_r = 0;
676         }
677         if (wp->port_r) {
678                 bus_release_resource(wp->dev, SYS_RES_IOPORT,
679                                      wp->port_rid, wp->port_r);
680                 wp->port_r = 0;
681         }
682 }
683
684 /* allocate contiguous fragments from the buffer */
685 static u_int32_t
686 frag_alloc(struct wds *wp, int size, u_int8_t **res, u_int32_t *maskp)
687 {
688         int     i;
689         u_int32_t       mask;
690         u_int32_t       free;
691
692         if (size > fragsiz * nfrags)
693                 return (CAM_REQ_TOO_BIG);
694
695         mask = 1;               /* always allocate at least 1 fragment */
696         for (i = fragsiz; i < size; i += fragsiz)
697                 mask = (mask << 1) | 1;
698
699         free = wp->data_free;
700         if(free != 0) {
701                 i = ffs(free)-1; /* ffs counts bits from 1 */
702                 for (mask <<= i; i < nfrags; i++) {
703                         if ((free & mask) == mask) {
704                                 wp->data_free &= ~mask; /* mark frags as busy */
705                                 *maskp = mask;
706                                 *res = &wp->dx->data[fragsiz * i];
707                                 DBG(DBX "wds%d: allocated buffer mask=0x%x\n",
708                                         wp->unit, mask);
709                                 return (CAM_REQ_CMP);
710                         }
711                         if (mask & 0x80000000)
712                                 break;
713
714                         mask <<= 1;
715                 }
716         }
717         return (CAM_REQUEUE_REQ);       /* no free memory now, try later */
718 }
719
720 static void
721 frag_free(struct wds *wp, u_int32_t mask)
722 {
723         wp->data_free |= mask;  /* mark frags as free */
724         DBG(DBX "wds%d: freed buffer mask=0x%x\n", wp->unit, mask);
725 }
726
727 static struct wds_req *
728 wdsr_alloc(struct wds *wp)
729 {
730         struct  wds_req *r;
731         int     x;
732         int     i;
733
734         r = NULL;
735         x = splcam();
736
737         /* anyway most of the time only 1 or 2 commands will
738          * be active because SCSI disconnect is not supported
739          * by hardware, so the search should be fast enough
740          */
741         i = ffs(wp->wdsr_free) - 1;
742         if(i < 0) {
743                 splx(x);
744                 return (NULL);
745         }
746         wp->wdsr_free &= ~ (1<<i);
747         r = &wp->dx->req[i];
748         r->flags = 0;   /* reset all flags */
749         r->ombn = i;            /* luckily we have one omb per wdsr */
750         wp->dx->ombs[i].stat = 1;
751
752         r->mask = 0;
753         splx(x);
754         smallog3('r', i + '0', r->ombn + '0');
755         return (r);
756 }
757
758 static void
759 wds_intr(struct wds *wp)
760 {
761         struct   wds_req *rp;
762         struct   wds_mb *in;
763         u_int8_t stat;
764         u_int8_t c;
765         int      addr = wp->addr;
766
767         DBG(DBX "wds%d: interrupt [\n", wp->unit);
768         smallog('[');
769
770         if (inb(addr + WDS_STAT) & WDS_IRQ) {
771                 c = inb(addr + WDS_IRQSTAT);
772                 if ((c & WDSI_MASK) == WDSI_MSVC) {
773                         c = c & ~WDSI_MASK;
774                         in = &wp->dx->imbs[c];
775
776                         rp = cmdtovirt(wp, scsi_3btoul(in->addr));
777                         stat = in->stat;
778
779                         if (rp != NULL)
780                                 wds_done(wp, rp, stat);
781                         else
782                                 device_printf(wp->dev,
783                                               "got weird command address %p"
784                                               "from controller\n", rp);
785
786                         in->stat = 0;
787                 } else
788                         device_printf(wp->dev,
789                                       "weird interrupt, irqstat=0x%x\n", c);
790                 outb(addr + WDS_IRQACK, 0);
791         } else {
792                 smallog('?');
793         }
794         smallog(']');
795         DBG(DBX "wds%d: ]\n", wp->unit);
796 }
797
798 static void
799 wds_done(struct wds *wp, struct wds_req *r, u_int8_t stat)
800 {
801         struct  ccb_hdr *ccb_h;
802         struct  ccb_scsiio *csio;
803         int     status;
804
805         smallog('d');
806
807         if (r->flags & WR_DONE) {
808                 device_printf(wp->dev,
809                                 "request %d reported done twice\n", r->id);
810                 smallog2('x', r->id + '0');
811                 return;
812         }
813
814         smallog(r->id + '0');
815         ccb_h = &r->ccb->ccb_h;
816         csio = &r->ccb->csio;
817         status = CAM_REQ_CMP_ERR;
818
819         DBG(DBX "wds%d: %s stat=0x%x c->stat=0x%x c->venderr=0x%x\n", wp->unit,
820             r->flags & WR_SENSE ? "(sense)" : "", 
821                 stat, r->cmd.stat, r->cmd.venderr);
822
823         if (r->flags & WR_SENSE) {
824                 if (stat == ICMB_OK || (stat == ICMB_OKERR && r->cmd.stat == 0)) {
825                         DBG(DBX "wds%d: sense 0x%x\n", wp->unit, r->buf[0]);
826                         /* it has the same size now but for future */
827                         bcopy(r->buf, &csio->sense_data,
828                               sizeof(struct scsi_sense_data) > csio->sense_len ?
829                               csio->sense_len : sizeof(struct scsi_sense_data));
830                         if (sizeof(struct scsi_sense_data) >= csio->sense_len)
831                                 csio->sense_resid = 0;
832                         else
833                                 csio->sense_resid =
834                                         csio->sense_len
835                                       - sizeof(struct scsi_sense_data);
836                         status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
837                 } else {
838                         status = CAM_AUTOSENSE_FAIL;
839                 }
840         } else {
841                 switch (stat) {
842                 case ICMB_OK:
843                         if (ccb_h) {
844                                 csio->resid = 0;
845                                 csio->scsi_status = r->cmd.stat;
846                                 status = CAM_REQ_CMP;
847                         }
848                         break;
849                 case ICMB_OKERR:
850                         if (ccb_h) {
851                                 csio->scsi_status = r->cmd.stat;
852                                 if (r->cmd.stat) {
853                                         if (ccb_h->flags & CAM_DIS_AUTOSENSE)
854                                                 status = CAM_SCSI_STATUS_ERROR;
855                                         else {
856                                                 if ( wds_runsense(wp, r) == CAM_REQ_CMP )
857                                                         return;
858                                                 /* in case of error continue with freeing of CCB */
859                                         }
860                                 } else {
861                                         csio->resid = 0;
862                                         status = CAM_REQ_CMP;
863                                 }
864                         }
865                         break;
866                 case ICMB_ETIME:
867                         if (ccb_h)
868                                 status = CAM_SEL_TIMEOUT;
869                         break;
870                 case ICMB_ERESET:
871                 case ICMB_ETARCMD:
872                 case ICMB_ERESEL:
873                 case ICMB_ESEL:
874                 case ICMB_EABORT:
875                 case ICMB_ESRESET:
876                 case ICMB_EHRESET:
877                         if (ccb_h)
878                                 status = CAM_REQ_CMP_ERR;
879                         break;
880                 }
881
882                 if (ccb_h && (ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN) {
883                         /* we accept only virtual addresses in wds_action() */
884                         bcopy(r->buf, csio->data_ptr, csio->dxfer_len);
885                 }
886         }
887
888         r->flags |= WR_DONE;
889         wp->dx->ombs[r->ombn].stat = 0;
890
891         if (ccb_h) {
892                 wdsr_ccb_done(wp, r, r->ccb, status);
893                 smallog3('-', ccb_h->target_id + '0', ccb_h->target_lun + '0');
894         } else {
895                 frag_free(wp, r->mask);
896                 if (wp->want_wdsr) {
897                         wp->want_wdsr = 0;
898                         xpt_release_simq(wp->sim, /* run queue */ 1);
899                 }
900                 wp->wdsr_free |= (1 << r->id);
901         }
902
903         DBG(DBX "wds%d: request %p done\n", wp->unit, r);
904 }
905
906 /* command returned bad status, request sense */
907
908 static int
909 wds_runsense(struct wds *wp, struct wds_req *r)
910 {
911         u_int8_t          c;
912         struct  ccb_hdr *ccb_h;
913
914         ccb_h = &r->ccb->ccb_h;
915
916         r->flags |= WR_SENSE;
917         scsi_ulto3b(WDSTOPHYS(wp, &r->cmd),
918          wp->dx->ombs[r->ombn].addr);
919         bzero(&r->cmd, sizeof r->cmd);
920         r->cmd.cmd = WDSX_SCSICMD;
921         r->cmd.targ = (ccb_h->target_id << 5) |
922                 ccb_h->target_lun;
923
924         scsi_ulto3b(0, r->cmd.next);
925
926         r->cmd.scb[0] = REQUEST_SENSE;
927         r->cmd.scb[1] = ccb_h->target_lun << 5;
928         r->cmd.scb[4] = sizeof(struct scsi_sense_data);
929         r->cmd.scb[5] = 0;
930         scsi_ulto3b(WDSTOPHYS(wp, r->buf), r->cmd.data);
931         scsi_ulto3b(sizeof(struct scsi_sense_data), r->cmd.len);
932         r->cmd.write = 0x80;
933
934         outb(wp->addr + WDS_HCR, WDSH_IRQEN | WDSH_DRQEN);
935
936         wp->dx->ombs[r->ombn].stat = 1;
937         c = WDSC_MSTART(r->ombn);
938
939         if (wds_cmd(wp->addr, &c, sizeof c) != 0) {
940                 device_printf(wp->dev, "unable to start outgoing sense mbox\n");
941                 wp->dx->ombs[r->ombn].stat = 0;
942                 wdsr_ccb_done(wp, r, r->ccb, CAM_AUTOSENSE_FAIL);
943                 return CAM_AUTOSENSE_FAIL;
944         } else {
945                 DBG(DBX "wds%d: enqueued status cmd 0x%x, r=%p\n",
946                         wp->unit, r->cmd.scb[0] & 0xFF, r);
947                 /* don't free CCB yet */
948                 smallog3('*', ccb_h->target_id + '0',
949                          ccb_h->target_lun + '0');
950                 return CAM_REQ_CMP;
951         }
952 }
953
954 static int
955 wds_getvers(struct wds *wp)
956 {
957         struct   wds_req *r;
958         int      base;
959         u_int8_t c;
960         int      i;
961
962         base = wp->addr;
963
964         r = wdsr_alloc(wp);
965         if (!r) {
966                 device_printf(wp->dev, "no request slot available!\n");
967                 return (-1);
968         }
969         r->flags &= ~WR_DONE;
970
971         r->ccb = NULL;
972
973         scsi_ulto3b(WDSTOPHYS(wp, &r->cmd), wp->dx->ombs[r->ombn].addr);
974
975         bzero(&r->cmd, sizeof r->cmd);
976         r->cmd.cmd = WDSX_GETFIRMREV;
977
978         outb(base + WDS_HCR, WDSH_DRQEN);
979
980         c = WDSC_MSTART(r->ombn);
981         if (wds_cmd(base, (u_int8_t *) & c, sizeof c)) {
982                 device_printf(wp->dev, "version request failed\n");
983                 wp->wdsr_free |= (1 << r->id);
984                 wp->dx->ombs[r->ombn].stat = 0;
985                 return (-1);
986         }
987         while (1) {
988                 i = 0;
989                 while ((inb(base + WDS_STAT) & WDS_IRQ) == 0) {
990                         DELAY(9000);
991                         if (++i == 100) {
992                                 device_printf(wp->dev, "getvers timeout\n");
993                                 return (-1);
994                         }
995                 }
996                 wds_intr(wp);
997                 if (r->flags & WR_DONE) {
998                         device_printf(wp->dev, "firmware version %d.%02d\n",
999                                r->cmd.targ, r->cmd.scb[0]);
1000                         wp->wdsr_free |= (1 << r->id);
1001                         return (0);
1002                 }
1003         }
1004 }
1005
1006 static void
1007 wdsr_ccb_done(struct wds *wp, struct wds_req *r,
1008               union ccb *ccb, u_int32_t status)
1009 {
1010         ccb->ccb_h.ccb_wdsr = 0;
1011
1012         if (r != NULL) {
1013                 /* To implement timeouts we would need to know how to abort the
1014                  * command on controller, and this is a great mystery.
1015                  * So for now we just pass the responsibility for timeouts
1016                  * to the controlles itself, it does that reasonably good.
1017                  */
1018                 /* untimeout(_timeout, (caddr_t) hcb, ccb->ccb_h.timeout_ch); */
1019                 /* we're about to free a hcb, so the shortage has ended */
1020                 frag_free(wp, r->mask);
1021                 if (wp->want_wdsr && status != CAM_REQUEUE_REQ) {
1022                         wp->want_wdsr = 0;
1023                         status |= CAM_RELEASE_SIMQ;
1024                         smallog('R');
1025                 }
1026                 wp->wdsr_free |= (1 << r->id);
1027         }
1028         ccb->ccb_h.status =
1029             status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
1030         xpt_done(ccb);
1031 }
1032
1033 static void
1034 wds_scsi_io(struct cam_sim * sim, struct ccb_scsiio * csio)
1035 {
1036         int      unit = cam_sim_unit(sim);
1037         struct   wds *wp;
1038         struct   ccb_hdr *ccb_h;
1039         struct   wds_req *r;
1040         int      base;
1041         u_int8_t c;
1042         int      error;
1043         int      n;
1044
1045         wp = (struct wds *)cam_sim_softc(sim);
1046         ccb_h = &csio->ccb_h;
1047
1048         DBG(DBX "wds%d: cmd TARG=%d LUN=%d\n", unit, ccb_h->target_id,
1049             ccb_h->target_lun);
1050
1051         if (ccb_h->target_id > 7 || ccb_h->target_id == WDS_HBA_ID) {
1052                 ccb_h->status = CAM_TID_INVALID;
1053                 xpt_done((union ccb *) csio);
1054                 return;
1055         }
1056         if (ccb_h->target_lun > 7) {
1057                 ccb_h->status = CAM_LUN_INVALID;
1058                 xpt_done((union ccb *) csio);
1059                 return;
1060         }
1061         if (csio->dxfer_len > BUFSIZ) {
1062                 ccb_h->status = CAM_REQ_TOO_BIG;
1063                 xpt_done((union ccb *) csio);
1064                 return;
1065         }
1066         if (ccb_h->flags & (CAM_CDB_PHYS | CAM_SCATTER_VALID | CAM_DATA_PHYS)) {
1067                 /* don't support these */
1068                 ccb_h->status = CAM_REQ_INVALID;
1069                 xpt_done((union ccb *) csio);
1070                 return;
1071         }
1072         base = wp->addr;
1073
1074         /*
1075          * this check is mostly for debugging purposes,
1076          * "can't happen" normally.
1077          */
1078         if(wp->want_wdsr) {
1079                 DBG(DBX "wds%d: someone already waits for buffer\n", unit);
1080                 smallog('b');
1081                 n = xpt_freeze_simq(sim, /* count */ 1);
1082                 smallog('0'+n);
1083                 ccb_h->status = CAM_REQUEUE_REQ;
1084                 xpt_done((union ccb *) csio);
1085                 return;
1086         }
1087
1088         r = wdsr_alloc(wp);
1089         if (r == NULL) {
1090                 device_printf(wp->dev, "no request slot available!\n");
1091                 wp->want_wdsr = 1;
1092                 n = xpt_freeze_simq(sim, /* count */ 1);
1093                 smallog2('f', '0'+n);
1094                 ccb_h->status = CAM_REQUEUE_REQ;
1095                 xpt_done((union ccb *) csio);
1096                 return;
1097         }
1098
1099         ccb_h->ccb_wdsr = (void *) r;
1100         r->ccb = (union ccb *) csio;
1101
1102         switch (error = frag_alloc(wp, csio->dxfer_len, &r->buf, &r->mask)) {
1103         case CAM_REQ_CMP:
1104                 break;
1105         case CAM_REQUEUE_REQ:
1106                 DBG(DBX "wds%d: no data buffer available\n", unit);
1107                 wp->want_wdsr = 1;
1108                 n = xpt_freeze_simq(sim, /* count */ 1);
1109                 smallog2('f', '0'+n);
1110                 wdsr_ccb_done(wp, r, r->ccb, CAM_REQUEUE_REQ);
1111                 return;
1112         default:
1113                 DBG(DBX "wds%d: request is too big\n", unit);
1114                 wdsr_ccb_done(wp, r, r->ccb, error);
1115                 break;
1116         }
1117
1118         ccb_h->status |= CAM_SIM_QUEUED;
1119         r->flags &= ~WR_DONE;
1120
1121         scsi_ulto3b(WDSTOPHYS(wp, &r->cmd), wp->dx->ombs[r->ombn].addr);
1122
1123         bzero(&r->cmd, sizeof r->cmd);
1124         r->cmd.cmd = WDSX_SCSICMD;
1125         r->cmd.targ = (ccb_h->target_id << 5) | ccb_h->target_lun;
1126
1127         if (ccb_h->flags & CAM_CDB_POINTER)
1128                 bcopy(csio->cdb_io.cdb_ptr, &r->cmd.scb,
1129                       csio->cdb_len < 12 ? csio->cdb_len : 12);
1130         else
1131                 bcopy(csio->cdb_io.cdb_bytes, &r->cmd.scb,
1132                       csio->cdb_len < 12 ? csio->cdb_len : 12);
1133
1134         scsi_ulto3b(csio->dxfer_len, r->cmd.len);
1135
1136         if (csio->dxfer_len > 0
1137          && (ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1138                 /* we already rejected physical or scattered addresses */
1139                 bcopy(csio->data_ptr, r->buf, csio->dxfer_len);
1140         }
1141         scsi_ulto3b(csio->dxfer_len ? WDSTOPHYS(wp, r->buf) : 0, r->cmd.data);
1142
1143         if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN)
1144                 r->cmd.write = 0x80;
1145         else
1146                 r->cmd.write = 0x00;
1147
1148         scsi_ulto3b(0, r->cmd.next);
1149
1150         outb(base + WDS_HCR, WDSH_IRQEN | WDSH_DRQEN);
1151
1152         c = WDSC_MSTART(r->ombn);
1153
1154         if (wds_cmd(base, &c, sizeof c) != 0) {
1155                 device_printf(wp->dev, "unable to start outgoing mbox\n");
1156                 wp->dx->ombs[r->ombn].stat = 0;
1157                 wdsr_ccb_done(wp, r, r->ccb, CAM_RESRC_UNAVAIL);
1158                 return;
1159         }
1160         DBG(DBX "wds%d: enqueued cmd 0x%x, r=%p\n", unit,
1161             r->cmd.scb[0] & 0xFF, r);
1162
1163         smallog3('+', ccb_h->target_id + '0', ccb_h->target_lun + '0');
1164 }
1165
1166 static void
1167 wds_action(struct cam_sim * sim, union ccb * ccb)
1168 {
1169         int     unit = cam_sim_unit(sim);
1170         int     s;
1171
1172         DBG(DBX "wds%d: action 0x%x\n", unit, ccb->ccb_h.func_code);
1173         switch (ccb->ccb_h.func_code) {
1174         case XPT_SCSI_IO:
1175                 s = splcam();
1176                 DBG(DBX "wds%d: SCSI IO entered\n", unit);
1177                 wds_scsi_io(sim, &ccb->csio);
1178                 DBG(DBX "wds%d: SCSI IO returned\n", unit);
1179                 splx(s);
1180                 break;
1181         case XPT_RESET_BUS:
1182                 /* how to do it right ? */
1183                 printf("wds%d: reset\n", unit);
1184                 ccb->ccb_h.status = CAM_REQ_CMP;
1185                 xpt_done(ccb);
1186                 break;
1187         case XPT_ABORT:
1188                 ccb->ccb_h.status = CAM_UA_ABORT;
1189                 xpt_done(ccb);
1190                 break;
1191         case XPT_CALC_GEOMETRY:
1192         {
1193                 struct    ccb_calc_geometry *ccg;
1194                 u_int32_t size_mb;
1195                 u_int32_t secs_per_cylinder;
1196
1197                 ccg = &ccb->ccg;
1198                 size_mb = ccg->volume_size
1199                         / ((1024L * 1024L) / ccg->block_size);
1200
1201                 ccg->heads = 64;
1202                 ccg->secs_per_track = 16;
1203                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1204                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1205                 ccb->ccb_h.status = CAM_REQ_CMP;
1206                 xpt_done(ccb);
1207                 break;
1208         }
1209         case XPT_PATH_INQ:      /* Path routing inquiry */
1210         {
1211                 struct ccb_pathinq *cpi = &ccb->cpi;
1212
1213                 cpi->version_num = 1;   /* XXX??? */
1214                 cpi->hba_inquiry = 0;   /* nothing fancy */
1215                 cpi->target_sprt = 0;
1216                 cpi->hba_misc = 0;
1217                 cpi->hba_eng_cnt = 0;
1218                 cpi->max_target = 7;
1219                 cpi->max_lun = 7;
1220                 cpi->initiator_id = WDS_HBA_ID;
1221                 cpi->hba_misc = 0;
1222                 cpi->bus_id = cam_sim_bus(sim);
1223                 cpi->base_transfer_speed = 3300;
1224                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1225                 strncpy(cpi->hba_vid, "WD/FDC", HBA_IDLEN);
1226                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1227                 cpi->unit_number = cam_sim_unit(sim);
1228                 cpi->ccb_h.status = CAM_REQ_CMP;
1229                 xpt_done(ccb);
1230                 break;
1231         }
1232         default:
1233                 ccb->ccb_h.status = CAM_REQ_INVALID;
1234                 xpt_done(ccb);
1235                 break;
1236         }
1237 }
1238
1239 static void
1240 wds_poll(struct cam_sim * sim)
1241 {
1242         wds_intr((struct wds *)cam_sim_softc(sim));
1243 }
1244
1245 /* part of initialization done in probe() */
1246 /* returns 0 if OK, ENXIO if bad */
1247
1248 static int
1249 wds_preinit(struct wds *wp)
1250 {
1251         int     base;
1252         int     i;
1253
1254         base = wp->addr;
1255
1256         /*
1257          * Sending a command causes the CMDRDY bit to clear.
1258          */
1259         outb(base + WDS_CMD, WDSC_NOOP);
1260         if (inb(base + WDS_STAT) & WDS_RDY)
1261                 return (ENXIO);
1262
1263         /*
1264          * the controller exists. reset and init.
1265          */
1266         outb(base + WDS_HCR, WDSH_ASCRESET | WDSH_SCSIRESET);
1267         DELAY(30);
1268         outb(base + WDS_HCR, 0);
1269
1270         if ((inb(base + WDS_STAT) & (WDS_RDY)) != WDS_RDY) {
1271                 for (i = 0; i < 10; i++) {
1272                         if ((inb(base + WDS_STAT) & (WDS_RDY)) == WDS_RDY)
1273                                 break;
1274                         DELAY(40000);
1275                 }
1276                 if ((inb(base + WDS_STAT) & (WDS_RDY)) != WDS_RDY)
1277                         /* probe timeout */
1278                         return (ENXIO);
1279         }
1280
1281         return (0);
1282 }
1283
1284 /* part of initialization done in attach() */
1285 /* returns 0 if OK, 1 if bad */
1286
1287 static int
1288 wds_init(struct wds *wp)
1289 {
1290         struct  wds_setup init;
1291         int     base;
1292         int     i;
1293         struct  wds_cmd  wc;
1294
1295         base = wp->addr;
1296
1297         outb(base + WDS_HCR, WDSH_DRQEN);
1298
1299         isa_dmacascade(wp->drq);
1300
1301         if ((inb(base + WDS_STAT) & (WDS_RDY)) != WDS_RDY) {
1302                 for (i = 0; i < 10; i++) {
1303                         if ((inb(base + WDS_STAT) & (WDS_RDY)) == WDS_RDY)
1304                                 break;
1305                         DELAY(40000);
1306                 }
1307                 if ((inb(base + WDS_STAT) & (WDS_RDY)) != WDS_RDY)
1308                         /* probe timeout */
1309                         return (1);
1310         }
1311         bzero(&init, sizeof init);
1312         init.cmd = WDSC_INIT;
1313         init.scsi_id = WDS_HBA_ID;
1314         init.buson_t = 24;
1315         init.busoff_t = 48;
1316         scsi_ulto3b(WDSTOPHYS(wp, &wp->dx->ombs), init.mbaddr); 
1317         init.xx = 0;
1318         init.nomb = WDS_NOMB;
1319         init.nimb = WDS_NIMB;
1320
1321         wds_wait(base + WDS_STAT, WDS_RDY, WDS_RDY);
1322         if (wds_cmd(base, (u_int8_t *) & init, sizeof init) != 0) {
1323                 device_printf(wp->dev, "wds_cmd init failed\n");
1324                 return (1);
1325         }
1326         wds_wait(base + WDS_STAT, WDS_INIT, WDS_INIT);
1327
1328         wds_wait(base + WDS_STAT, WDS_RDY, WDS_RDY);
1329
1330         bzero(&wc, sizeof wc);
1331         wc.cmd = WDSC_DISUNSOL;
1332         if (wds_cmd(base, (char *) &wc, sizeof wc) != 0) {
1333                 device_printf(wp->dev, "wds_cmd init2 failed\n");
1334                 return (1);
1335         }
1336         return (0);
1337 }
1338
1339 static int
1340 wds_cmd(int base, u_int8_t * p, int l)
1341 {
1342         int     s = splcam();
1343
1344         while (l--) {
1345                 do {
1346                         outb(base + WDS_CMD, *p);
1347                         wds_wait(base + WDS_STAT, WDS_RDY, WDS_RDY);
1348                 } while (inb(base + WDS_STAT) & WDS_REJ);
1349                 p++;
1350         }
1351
1352         wds_wait(base + WDS_STAT, WDS_RDY, WDS_RDY);
1353
1354         splx(s);
1355
1356         return (0);
1357 }
1358
1359 static void
1360 wds_wait(int reg, int mask, int val)
1361 {
1362         while ((inb(reg) & mask) != val)
1363                 ;
1364 }
1365
1366 static struct wds_req *
1367 cmdtovirt(struct wds *wp, u_int32_t phys)
1368 {
1369         char    *a;
1370
1371         a = WDSTOVIRT(wp, (uintptr_t)phys);
1372         if( a < (char *)&wp->dx->req[0] || a>= (char *)&wp->dx->req[MAXSIMUL]) {
1373                 device_printf(wp->dev, "weird phys address 0x%x\n", phys);
1374                 return (NULL);
1375         }
1376         a -= (int)offsetof(struct wds_req, cmd); /* convert cmd to request */
1377         return ((struct wds_req *)a);
1378 }
1379
1380 /* for debugging, print out all the data about the status of devices */
1381 void
1382 wds_print(void)
1383 {
1384         int     unit;
1385         int     i;
1386         struct  wds_req *r;
1387         struct  wds     *wp;
1388
1389         for (unit = 0; unit < devclass_get_maxunit(wds_devclass); unit++) {
1390                 wp = (struct wds *) devclass_get_device(wds_devclass, unit);
1391                 if (wp == NULL)
1392                         continue;
1393                 printf("wds%d: want_wdsr=0x%x stat=0x%x irq=%s irqstat=0x%x\n",
1394                        unit, wp->want_wdsr, inb(wp->addr + WDS_STAT) & 0xff,
1395                        (inb(wp->addr + WDS_STAT) & WDS_IRQ) ? "ready" : "no",
1396                        inb(wp->addr + WDS_IRQSTAT) & 0xff);
1397                 for (i = 0; i < MAXSIMUL; i++) {
1398                         r = &wp->dx->req[i];
1399                         if( wp->wdsr_free & (1 << r->id) ) {
1400                                 printf("req=%d flg=0x%x ombn=%d ombstat=%d "
1401                                        "mask=0x%x targ=%d lun=%d cmd=0x%x\n",
1402                                        i, r->flags, r->ombn,
1403                                        wp->dx->ombs[r->ombn].stat,
1404                                        r->mask, r->cmd.targ >> 5,
1405                                        r->cmd.targ & 7, r->cmd.scb[0]);
1406                         }
1407                 }
1408         }
1409 }
1410
1411 #if WDS_DEBUG == 2
1412 /* create circular log buffer */
1413 static char    *
1414 wds_nextlog(void)
1415 {
1416         int     n = logwrite;
1417
1418         if (++logwrite >= NLOGLINES)
1419                 logwrite = 0;
1420         if (logread == logwrite)
1421                 if (++logread >= NLOGLINES)
1422                         logread = 0;
1423         return (wds_log[n]);
1424 }
1425
1426 void
1427 wds_printlog(void)
1428 {
1429         /* print the circular buffer */
1430         int     i;
1431
1432         for (i = logread; i != logwrite;) {
1433                 printf("%s", wds_log[i]);
1434                 if (i == NLOGLINES)
1435                         i = 0;
1436                 else
1437                         i++;
1438         }
1439 }
1440 #endif /* WDS_DEBUG */