2 * Copyright (c) 1994 Ludd, University of Lule}, Sweden.
3 * Copyright (c) 2000 Sergey A. Babkin
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>.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 /* All bugs are subject to removal without further notice */
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.
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.
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
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
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
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
88 * W98 (on newer cards) - BIOS enabled; on older cards just remove the BIOS
90 * W99 (on newer cards) - ROM size (1-2) OFF, (3-4) ON
92 * W5 - terminator power
93 * ON - host supplies term. power
94 * OFF - target supplies term. power
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
101 * Default: I/O 0x350, IRQ15, DMA6
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
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.
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
125 #ifndef WDS_ENABLE_SMALLOG
126 #define WDS_ENABLE_SMALLOG 0
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 #include <sys/malloc.h>
141 #include <cam/cam_ccb.h>
142 #include <cam/cam_sim.h>
143 #include <cam/cam_xpt_sim.h>
144 #include <cam/cam_debug.h>
145 #include <cam/scsi/scsi_all.h>
146 #include <cam/scsi/scsi_message.h>
150 #include <vm/vm_param.h>
153 #include <sys/module.h>
155 #include <machine/bus.h>
156 #include <machine/resource.h>
157 #include <sys/rman.h>
159 #include <isa/isavar.h>
160 #include <isa/pnpvar.h>
162 #define WDSTOPHYS(wp, a) ( ((u_long)a) - ((u_long)wp->dx) + ((u_long)wp->dx_p) )
163 #define WDSTOVIRT(wp, a) ( ((char *)a) - ((char*)wp->dx_p) + ((char *)wp->dx) )
165 /* 0x10000 (64k) should be enough. But just to be sure... */
166 #define BUFSIZ 0x12000
167 /* buffer fragment size, no more than 32 frags per buffer */
168 #define FRAGSIZ 0x1000
171 /* WD7000 registers */
172 #define WDS_STAT 0 /* read */
173 #define WDS_IRQSTAT 1 /* read */
175 #define WDS_CMD 0 /* write */
176 #define WDS_IRQACK 1 /* write */
177 #define WDS_HCR 2 /* write */
179 #define WDS_NPORTS 4 /* number of ports used */
181 /* WDS_STAT (read) defs */
185 #define WDS_INIT 0x10
187 /* WDS_IRQSTAT (read) defs */
188 #define WDSI_MASK 0xc0
189 #define WDSI_ERR 0x00
190 #define WDSI_MFREE 0x80
191 #define WDSI_MSVC 0xc0
193 /* WDS_CMD (write) defs */
194 #define WDSC_NOOP 0x00
195 #define WDSC_INIT 0x01
196 #define WDSC_DISUNSOL 0x02 /* disable unsolicited ints */
197 #define WDSC_ENAUNSOL 0x03 /* enable unsolicited ints */
198 #define WDSC_IRQMFREE 0x04 /* interrupt on free RQM */
199 #define WDSC_SCSIRESETSOFT 0x05 /* soft reset */
200 #define WDSC_SCSIRESETHARD 0x06 /* hard reset ack */
201 #define WDSC_MSTART(m) (0x80 + (m)) /* start mailbox */
202 #define WDSC_MMSTART(m) (0xc0 + (m)) /* start all mailboxes */
204 /* WDS_HCR (write) defs */
205 #define WDSH_IRQEN 0x08
206 #define WDSH_DRQEN 0x04
207 #define WDSH_SCSIRESET 0x02
208 #define WDSH_ASCRESET 0x01
230 u_int8_t *buf; /* address of linear data buffer */
231 u_int32_t mask; /* mask of allocated fragments */
233 u_int8_t id; /* number of request */
236 #define WDSX_SCSICMD 0x00
237 #define WDSX_OPEN_RCVBUF 0x80
238 #define WDSX_RCV_CMD 0x81
239 #define WDSX_RCV_DATA 0x82
240 #define WDSX_RCV_DATASTAT 0x83
241 #define WDSX_SND_DATA 0x84
242 #define WDSX_SND_DATASTAT 0x85
243 #define WDSX_SND_CMDSTAT 0x86
244 #define WDSX_READINIT 0x88
245 #define WDSX_READSCSIID 0x89
246 #define WDSX_SETUNSOLIRQMASK 0x8a
247 #define WDSX_GETUNSOLIRQMASK 0x8b
248 #define WDSX_GETFIRMREV 0x8c
249 #define WDSX_EXECDIAG 0x8d
250 #define WDSX_SETEXECPARM 0x8e
251 #define WDSX_GETEXECPARM 0x8f
257 /* ICMB status value */
259 #define ICMB_OKERR 0x02
260 #define ICMB_ETIME 0x04
261 #define ICMB_ERESET 0x05
262 #define ICMB_ETARCMD 0x06
263 #define ICMB_ERESEL 0x80
264 #define ICMB_ESEL 0x81
265 #define ICMB_EABORT 0x82
266 #define ICMB_ESRESET 0x83
267 #define ICMB_EHRESET 0x84
280 /* the code depends on equality of these parameters */
282 #define WDS_NOMB MAXSIMUL
283 #define WDS_NIMB MAXSIMUL
288 /* structure for data exchange with controller */
291 struct wds_req req[MAXSIMUL];
292 struct wds_mb ombs[MAXSIMUL];
293 struct wds_mb imbs[MAXSIMUL];
294 u_int8_t data[BUFSIZ];
297 /* structure softc */
304 struct cam_sim *sim; /* SIM descriptor for this card */
305 struct cam_path *path; /* wildcard path for this card */
306 char want_wdsr; /* resource shortage flag */
310 struct wdsdx *dx_p; /* physical address */
311 struct resource *port_r;
313 struct resource *drq_r;
315 struct resource *intr_r;
318 bus_dma_tag_t bustag;
322 #define ccb_wdsr spriv_ptr1 /* for wds request */
324 static int wds_probe(device_t dev);
325 static int wds_attach(device_t dev);
326 static void wds_intr(struct wds *wp);
328 static void wds_action(struct cam_sim * sim, union ccb * ccb);
329 static void wds_poll(struct cam_sim * sim);
331 static int wds_preinit(struct wds *wp);
332 static int wds_init(struct wds *wp);
334 static void wds_alloc_callback(void *arg, bus_dma_segment_t *seg,
335 int nseg, int error);
336 static void wds_free_resources(struct wds *wp);
338 static struct wds_req *wdsr_alloc(struct wds *wp);
340 static void wds_scsi_io(struct cam_sim * sim, struct ccb_scsiio * csio);
341 static void wdsr_ccb_done(struct wds *wp, struct wds_req *r,
342 union ccb *ccb, u_int32_t status);
344 static void wds_done(struct wds *wp, struct wds_req *r, u_int8_t stat);
345 static int wds_runsense(struct wds *wp, struct wds_req *r);
346 static int wds_getvers(struct wds *wp);
348 static int wds_cmd(int base, u_int8_t * p, int l);
349 static void wds_wait(int reg, int mask, int val);
351 static struct wds_req *cmdtovirt(struct wds *wp, u_int32_t phys);
353 static u_int32_t frag_alloc(struct wds *wp, int size, u_int8_t **res,
355 static void frag_free(struct wds *wp, u_int32_t mask);
357 void wds_print(void);
359 #if WDS_ENABLE_SMALLOG==1
360 static __inline void smallog(char c);
361 void wds_printsmallog(void);
364 /* SCSI ID of the adapter itself */
370 #define LOGLINESIZ 81
371 #define NLOGLINES 300
372 #define DBX wds_nextlog(), LOGLINESIZ,
375 static char wds_log[NLOGLINES][LOGLINESIZ];
376 static int logwrite = 0, logread = 0;
377 static char *wds_nextlog(void);
378 void wds_printlog(void);
385 #define DBG if(0) printf
388 /* the table of supported bus methods */
389 static device_method_t wds_isa_methods[] = {
390 DEVMETHOD(device_probe, wds_probe),
391 DEVMETHOD(device_attach, wds_attach),
395 static driver_t wds_isa_driver = {
401 static devclass_t wds_devclass;
403 DRIVER_MODULE(wds, isa, wds_isa_driver, wds_devclass, 0, 0);
404 MODULE_DEPEND(wds, isa, 1, 1, 1);
405 MODULE_DEPEND(wds, cam, 1, 1, 1);
407 #if WDS_ENABLE_SMALLOG==1
408 #define SMALLOGSIZ 512
409 static char wds_smallog[SMALLOGSIZ];
410 static char *wds_smallogp = wds_smallog;
411 static char wds_smallogover = 0;
417 if (++wds_smallogp == &wds_smallog[SMALLOGSIZ]) {
418 wds_smallogp = wds_smallog;
423 #define smallog2(a, b) (smallog(a), smallog(b))
424 #define smallog3(a, b, c) (smallog(a), smallog(b), smallog(c))
425 #define smallog4(a, b, c, d) (smallog(a),smallog(b),smallog(c),smallog(d))
428 wds_printsmallog(void)
434 p = wds_smallogover ? wds_smallogp : wds_smallog;
438 if (++p == &wds_smallog[SMALLOGSIZ])
444 } while (p != wds_smallogp);
449 #define smallog2(a, b)
450 #define smallog3(a, b, c)
451 #define smallog4(a, b, c, d)
455 wds_probe(device_t dev)
462 if (isa_get_vendorid(dev))
465 wp = (struct wds *) device_get_softc(dev);
466 wp->unit = device_get_unit(dev);
469 wp->addr = bus_get_resource_start(dev, SYS_RES_IOPORT, 0 /*rid*/);
470 if (wp->addr == 0 || wp->addr <0x300
471 || wp->addr > 0x3f8 || wp->addr & 0x7) {
472 device_printf(dev, "invalid port address 0x%x\n", wp->addr);
476 if (bus_set_resource(dev, SYS_RES_IOPORT, 0, wp->addr, WDS_NPORTS) < 0)
480 wp->drq = bus_get_resource_start(dev, SYS_RES_DRQ, 0 /*rid*/);
481 if (wp->drq < 5 || wp->drq > 7) {
482 device_printf(dev, "invalid DRQ %d\n", wp->drq);
487 irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0 /*rid*/);
489 device_printf(dev, "invalid IRQ %d\n", irq);
494 wp->port_r = bus_alloc_resource(dev, SYS_RES_IOPORT, &wp->port_rid,
495 /*start*/ 0, /*end*/ ~0,
496 /*count*/ 0, RF_ACTIVE);
497 if (wp->port_r == NULL)
500 error = wds_preinit(wp);
503 * We cannot hold resources between probe and
504 * attach as we may never be attached.
506 wds_free_resources(wp);
512 wds_attach(device_t dev)
515 struct cam_devq *devq;
517 struct cam_path *pathp;
521 wp = (struct wds *)device_get_softc(dev);
524 wp->port_r = bus_alloc_resource(dev, SYS_RES_IOPORT, &wp->port_rid,
525 /*start*/ 0, /*end*/ ~0,
526 /*count*/ 0, RF_ACTIVE);
527 if (wp->port_r == NULL)
530 /* We must now release resources on error. */
533 wp->drq_r = bus_alloc_resource(dev, SYS_RES_DRQ, &wp->drq_rid,
534 /*start*/ 0, /*end*/ ~0,
535 /*count*/ 0, RF_ACTIVE);
536 if (wp->drq_r == NULL)
540 wp->intr_r = bus_alloc_resource(dev, SYS_RES_IRQ, &wp->intr_rid,
541 /*start*/ 0, /*end*/ ~0,
542 /*count*/ 0, RF_ACTIVE);
543 if (wp->intr_r == NULL)
545 error = bus_setup_intr(dev, wp->intr_r, INTR_TYPE_CAM | INTR_ENTROPY,
546 NULL, (driver_intr_t *)wds_intr, (void *)wp,
551 /* now create the memory buffer */
552 error = bus_dma_tag_create(bus_get_dma_tag(dev), /*alignment*/4,
554 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
555 /*highaddr*/ BUS_SPACE_MAXADDR,
556 /*filter*/ NULL, /*filterarg*/ NULL,
557 /*maxsize*/ sizeof(* wp->dx),
559 /*maxsegsz*/ sizeof(* wp->dx), /*flags*/ 0,
560 /*lockfunc*/busdma_lock_mutex,
566 error = bus_dmamem_alloc(wp->bustag, (void **)&wp->dx,
567 /*flags*/ 0, &wp->busmap);
571 bus_dmamap_load(wp->bustag, wp->busmap, (void *)wp->dx,
572 sizeof(* wp->dx), wds_alloc_callback,
573 (void *)&wp->dx_p, /*flags*/0);
575 /* initialize the wds_req structures on this unit */
576 for(i=0; i<MAXSIMUL; i++) {
577 wp->dx->req[i].id = i;
578 wp->wdsr_free |= 1<<i;
581 /* initialize the memory buffer allocation for this unit */
582 if (BUFSIZ / FRAGSIZ > 32) {
583 fragsiz = (BUFSIZ / 32) & ~0x01; /* keep it word-aligned */
584 device_printf(dev, "data buffer fragment size too small. "
585 "BUFSIZE / FRAGSIZE must be <= 32\n");
587 fragsiz = FRAGSIZ & ~0x01; /* keep it word-aligned */
591 for (i = fragsiz; i <= BUFSIZ; i += fragsiz) {
593 wp->data_free = (wp->data_free << 1) | 1;
596 /* complete the hardware initialization */
597 if (wds_init(wp) != 0)
600 if (wds_getvers(wp) == -1)
601 device_printf(dev, "getvers failed\n");
602 device_printf(dev, "using %d bytes / %d frags for dma buffer\n",
605 devq = cam_simq_alloc(MAXSIMUL);
609 sim = cam_sim_alloc(wds_action, wds_poll, "wds", (void *) wp,
610 wp->unit, &Giant, 1, 1, devq);
617 if (xpt_bus_register(sim, dev, 0) != CAM_SUCCESS) {
618 cam_sim_free(sim, /* free_devq */ TRUE);
621 if (xpt_create_path(&pathp, /* periph */ NULL,
622 cam_sim_path(sim), CAM_TARGET_WILDCARD,
623 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
624 xpt_bus_deregister(cam_sim_path(sim));
625 cam_sim_free(sim, /* free_devq */ TRUE);
633 wds_free_resources(wp);
636 else /* exact error is unknown */
640 /* callback to save the physical address */
642 wds_alloc_callback(void *arg, bus_dma_segment_t *seg, int nseg, int error)
644 *(bus_addr_t *)arg = seg[0].ds_addr;
648 wds_free_resources(struct wds *wp)
650 /* check every resource and free if not zero */
652 /* interrupt handler */
654 bus_teardown_intr(wp->dev, wp->intr_r, wp->intr_cookie);
655 bus_release_resource(wp->dev, SYS_RES_IRQ, wp->intr_rid,
660 /* all kinds of memory maps we could have allocated */
662 bus_dmamap_unload(wp->bustag, wp->busmap);
665 if (wp->dx) { /* wp->busmap may be legitimately equal to 0 */
666 /* the map will also be freed */
667 bus_dmamem_free(wp->bustag, wp->dx, wp->busmap);
671 bus_dma_tag_destroy(wp->bustag);
674 /* release all the bus resources */
676 bus_release_resource(wp->dev, SYS_RES_DRQ,
677 wp->drq_rid, wp->drq_r);
681 bus_release_resource(wp->dev, SYS_RES_IOPORT,
682 wp->port_rid, wp->port_r);
687 /* allocate contiguous fragments from the buffer */
689 frag_alloc(struct wds *wp, int size, u_int8_t **res, u_int32_t *maskp)
695 if (size > fragsiz * nfrags)
696 return (CAM_REQ_TOO_BIG);
698 mask = 1; /* always allocate at least 1 fragment */
699 for (i = fragsiz; i < size; i += fragsiz)
700 mask = (mask << 1) | 1;
702 free = wp->data_free;
704 i = ffs(free)-1; /* ffs counts bits from 1 */
705 for (mask <<= i; i < nfrags; i++) {
706 if ((free & mask) == mask) {
707 wp->data_free &= ~mask; /* mark frags as busy */
709 *res = &wp->dx->data[fragsiz * i];
710 DBG(DBX "wds%d: allocated buffer mask=0x%x\n",
712 return (CAM_REQ_CMP);
714 if (mask & 0x80000000)
720 return (CAM_REQUEUE_REQ); /* no free memory now, try later */
724 frag_free(struct wds *wp, u_int32_t mask)
726 wp->data_free |= mask; /* mark frags as free */
727 DBG(DBX "wds%d: freed buffer mask=0x%x\n", wp->unit, mask);
730 static struct wds_req *
731 wdsr_alloc(struct wds *wp)
740 /* anyway most of the time only 1 or 2 commands will
741 * be active because SCSI disconnect is not supported
742 * by hardware, so the search should be fast enough
744 i = ffs(wp->wdsr_free) - 1;
749 wp->wdsr_free &= ~ (1<<i);
751 r->flags = 0; /* reset all flags */
752 r->ombn = i; /* luckily we have one omb per wdsr */
753 wp->dx->ombs[i].stat = 1;
757 smallog3('r', i + '0', r->ombn + '0');
762 wds_intr(struct wds *wp)
770 DBG(DBX "wds%d: interrupt [\n", wp->unit);
773 if (inb(addr + WDS_STAT) & WDS_IRQ) {
774 c = inb(addr + WDS_IRQSTAT);
775 if ((c & WDSI_MASK) == WDSI_MSVC) {
777 in = &wp->dx->imbs[c];
779 rp = cmdtovirt(wp, scsi_3btoul(in->addr));
783 wds_done(wp, rp, stat);
785 device_printf(wp->dev,
786 "got weird command address %p"
787 "from controller\n", rp);
791 device_printf(wp->dev,
792 "weird interrupt, irqstat=0x%x\n", c);
793 outb(addr + WDS_IRQACK, 0);
798 DBG(DBX "wds%d: ]\n", wp->unit);
802 wds_done(struct wds *wp, struct wds_req *r, u_int8_t stat)
804 struct ccb_hdr *ccb_h;
805 struct ccb_scsiio *csio;
810 if (r->flags & WR_DONE) {
811 device_printf(wp->dev,
812 "request %d reported done twice\n", r->id);
813 smallog2('x', r->id + '0');
817 smallog(r->id + '0');
818 ccb_h = &r->ccb->ccb_h;
819 csio = &r->ccb->csio;
820 status = CAM_REQ_CMP_ERR;
822 DBG(DBX "wds%d: %s stat=0x%x c->stat=0x%x c->venderr=0x%x\n", wp->unit,
823 r->flags & WR_SENSE ? "(sense)" : "",
824 stat, r->cmd.stat, r->cmd.venderr);
826 if (r->flags & WR_SENSE) {
827 if (stat == ICMB_OK || (stat == ICMB_OKERR && r->cmd.stat == 0)) {
828 DBG(DBX "wds%d: sense 0x%x\n", wp->unit, r->buf[0]);
829 /* it has the same size now but for future */
830 bcopy(r->buf, &csio->sense_data,
831 sizeof(struct scsi_sense_data) > csio->sense_len ?
832 csio->sense_len : sizeof(struct scsi_sense_data));
833 if (sizeof(struct scsi_sense_data) >= csio->sense_len)
834 csio->sense_resid = 0;
838 - sizeof(struct scsi_sense_data);
839 status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
841 status = CAM_AUTOSENSE_FAIL;
848 csio->scsi_status = r->cmd.stat;
849 status = CAM_REQ_CMP;
854 csio->scsi_status = r->cmd.stat;
856 if (ccb_h->flags & CAM_DIS_AUTOSENSE)
857 status = CAM_SCSI_STATUS_ERROR;
859 if ( wds_runsense(wp, r) == CAM_REQ_CMP )
861 /* in case of error continue with freeing of CCB */
865 status = CAM_REQ_CMP;
871 status = CAM_SEL_TIMEOUT;
881 status = CAM_REQ_CMP_ERR;
885 if (ccb_h && (ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN) {
886 /* we accept only virtual addresses in wds_action() */
887 bcopy(r->buf, csio->data_ptr, csio->dxfer_len);
892 wp->dx->ombs[r->ombn].stat = 0;
895 wdsr_ccb_done(wp, r, r->ccb, status);
896 smallog3('-', ccb_h->target_id + '0', ccb_h->target_lun + '0');
898 frag_free(wp, r->mask);
901 xpt_release_simq(wp->sim, /* run queue */ 1);
903 wp->wdsr_free |= (1 << r->id);
906 DBG(DBX "wds%d: request %p done\n", wp->unit, r);
909 /* command returned bad status, request sense */
912 wds_runsense(struct wds *wp, struct wds_req *r)
915 struct ccb_hdr *ccb_h;
917 ccb_h = &r->ccb->ccb_h;
919 r->flags |= WR_SENSE;
920 scsi_ulto3b(WDSTOPHYS(wp, &r->cmd),
921 wp->dx->ombs[r->ombn].addr);
922 bzero(&r->cmd, sizeof r->cmd);
923 r->cmd.cmd = WDSX_SCSICMD;
924 r->cmd.targ = (ccb_h->target_id << 5) |
927 scsi_ulto3b(0, r->cmd.next);
929 r->cmd.scb[0] = REQUEST_SENSE;
930 r->cmd.scb[1] = ccb_h->target_lun << 5;
931 r->cmd.scb[4] = sizeof(struct scsi_sense_data);
933 scsi_ulto3b(WDSTOPHYS(wp, r->buf), r->cmd.data);
934 scsi_ulto3b(sizeof(struct scsi_sense_data), r->cmd.len);
937 outb(wp->addr + WDS_HCR, WDSH_IRQEN | WDSH_DRQEN);
939 wp->dx->ombs[r->ombn].stat = 1;
940 c = WDSC_MSTART(r->ombn);
942 if (wds_cmd(wp->addr, &c, sizeof c) != 0) {
943 device_printf(wp->dev, "unable to start outgoing sense mbox\n");
944 wp->dx->ombs[r->ombn].stat = 0;
945 wdsr_ccb_done(wp, r, r->ccb, CAM_AUTOSENSE_FAIL);
946 return CAM_AUTOSENSE_FAIL;
948 DBG(DBX "wds%d: enqueued status cmd 0x%x, r=%p\n",
949 wp->unit, r->cmd.scb[0] & 0xFF, r);
950 /* don't free CCB yet */
951 smallog3('*', ccb_h->target_id + '0',
952 ccb_h->target_lun + '0');
958 wds_getvers(struct wds *wp)
969 device_printf(wp->dev, "no request slot available!\n");
972 r->flags &= ~WR_DONE;
976 scsi_ulto3b(WDSTOPHYS(wp, &r->cmd), wp->dx->ombs[r->ombn].addr);
978 bzero(&r->cmd, sizeof r->cmd);
979 r->cmd.cmd = WDSX_GETFIRMREV;
981 outb(base + WDS_HCR, WDSH_DRQEN);
983 c = WDSC_MSTART(r->ombn);
984 if (wds_cmd(base, (u_int8_t *) & c, sizeof c)) {
985 device_printf(wp->dev, "version request failed\n");
986 wp->wdsr_free |= (1 << r->id);
987 wp->dx->ombs[r->ombn].stat = 0;
992 while ((inb(base + WDS_STAT) & WDS_IRQ) == 0) {
995 device_printf(wp->dev, "getvers timeout\n");
1000 if (r->flags & WR_DONE) {
1001 device_printf(wp->dev, "firmware version %d.%02d\n",
1002 r->cmd.targ, r->cmd.scb[0]);
1003 wp->wdsr_free |= (1 << r->id);
1010 wdsr_ccb_done(struct wds *wp, struct wds_req *r,
1011 union ccb *ccb, u_int32_t status)
1013 ccb->ccb_h.ccb_wdsr = 0;
1016 /* To implement timeouts we would need to know how to abort the
1017 * command on controller, and this is a great mystery.
1018 * So for now we just pass the responsibility for timeouts
1019 * to the controlles itself, it does that reasonably good.
1021 /* untimeout(_timeout, (caddr_t) hcb, ccb->ccb_h.timeout_ch); */
1022 /* we're about to free a hcb, so the shortage has ended */
1023 frag_free(wp, r->mask);
1024 if (wp->want_wdsr && status != CAM_REQUEUE_REQ) {
1026 status |= CAM_RELEASE_SIMQ;
1029 wp->wdsr_free |= (1 << r->id);
1032 status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
1037 wds_scsi_io(struct cam_sim * sim, struct ccb_scsiio * csio)
1039 int unit = cam_sim_unit(sim);
1041 struct ccb_hdr *ccb_h;
1048 wp = (struct wds *)cam_sim_softc(sim);
1049 ccb_h = &csio->ccb_h;
1051 DBG(DBX "wds%d: cmd TARG=%d LUN=%d\n", unit, ccb_h->target_id,
1054 if (ccb_h->target_id > 7 || ccb_h->target_id == WDS_HBA_ID) {
1055 ccb_h->status = CAM_TID_INVALID;
1056 xpt_done((union ccb *) csio);
1059 if (ccb_h->target_lun > 7) {
1060 ccb_h->status = CAM_LUN_INVALID;
1061 xpt_done((union ccb *) csio);
1064 if (csio->dxfer_len > BUFSIZ) {
1065 ccb_h->status = CAM_REQ_TOO_BIG;
1066 xpt_done((union ccb *) csio);
1069 if ((ccb_h->flags & CAM_DATA_MASK) != CAM_DATA_VADDR) {
1070 /* don't support these */
1071 ccb_h->status = CAM_REQ_INVALID;
1072 xpt_done((union ccb *) csio);
1078 * this check is mostly for debugging purposes,
1079 * "can't happen" normally.
1082 DBG(DBX "wds%d: someone already waits for buffer\n", unit);
1084 n = xpt_freeze_simq(sim, /* count */ 1);
1086 ccb_h->status = CAM_REQUEUE_REQ;
1087 xpt_done((union ccb *) csio);
1093 device_printf(wp->dev, "no request slot available!\n");
1095 n = xpt_freeze_simq(sim, /* count */ 1);
1096 smallog2('f', '0'+n);
1097 ccb_h->status = CAM_REQUEUE_REQ;
1098 xpt_done((union ccb *) csio);
1102 ccb_h->ccb_wdsr = (void *) r;
1103 r->ccb = (union ccb *) csio;
1105 switch (error = frag_alloc(wp, csio->dxfer_len, &r->buf, &r->mask)) {
1108 case CAM_REQUEUE_REQ:
1109 DBG(DBX "wds%d: no data buffer available\n", unit);
1111 n = xpt_freeze_simq(sim, /* count */ 1);
1112 smallog2('f', '0'+n);
1113 wdsr_ccb_done(wp, r, r->ccb, CAM_REQUEUE_REQ);
1116 DBG(DBX "wds%d: request is too big\n", unit);
1117 wdsr_ccb_done(wp, r, r->ccb, error);
1121 ccb_h->status |= CAM_SIM_QUEUED;
1122 r->flags &= ~WR_DONE;
1124 scsi_ulto3b(WDSTOPHYS(wp, &r->cmd), wp->dx->ombs[r->ombn].addr);
1126 bzero(&r->cmd, sizeof r->cmd);
1127 r->cmd.cmd = WDSX_SCSICMD;
1128 r->cmd.targ = (ccb_h->target_id << 5) | ccb_h->target_lun;
1130 if (ccb_h->flags & CAM_CDB_POINTER)
1131 bcopy(csio->cdb_io.cdb_ptr, &r->cmd.scb,
1132 csio->cdb_len < 12 ? csio->cdb_len : 12);
1134 bcopy(csio->cdb_io.cdb_bytes, &r->cmd.scb,
1135 csio->cdb_len < 12 ? csio->cdb_len : 12);
1137 scsi_ulto3b(csio->dxfer_len, r->cmd.len);
1139 if (csio->dxfer_len > 0
1140 && (ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1141 /* we already rejected physical or scattered addresses */
1142 bcopy(csio->data_ptr, r->buf, csio->dxfer_len);
1144 scsi_ulto3b(csio->dxfer_len ? WDSTOPHYS(wp, r->buf) : 0, r->cmd.data);
1146 if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN)
1147 r->cmd.write = 0x80;
1149 r->cmd.write = 0x00;
1151 scsi_ulto3b(0, r->cmd.next);
1153 outb(base + WDS_HCR, WDSH_IRQEN | WDSH_DRQEN);
1155 c = WDSC_MSTART(r->ombn);
1157 if (wds_cmd(base, &c, sizeof c) != 0) {
1158 device_printf(wp->dev, "unable to start outgoing mbox\n");
1159 wp->dx->ombs[r->ombn].stat = 0;
1160 wdsr_ccb_done(wp, r, r->ccb, CAM_RESRC_UNAVAIL);
1163 DBG(DBX "wds%d: enqueued cmd 0x%x, r=%p\n", unit,
1164 r->cmd.scb[0] & 0xFF, r);
1166 smallog3('+', ccb_h->target_id + '0', ccb_h->target_lun + '0');
1170 wds_action(struct cam_sim * sim, union ccb * ccb)
1172 int unit = cam_sim_unit(sim);
1175 DBG(DBX "wds%d: action 0x%x\n", unit, ccb->ccb_h.func_code);
1176 switch (ccb->ccb_h.func_code) {
1179 DBG(DBX "wds%d: SCSI IO entered\n", unit);
1180 wds_scsi_io(sim, &ccb->csio);
1181 DBG(DBX "wds%d: SCSI IO returned\n", unit);
1185 /* how to do it right ? */
1186 printf("wds%d: reset\n", unit);
1187 ccb->ccb_h.status = CAM_REQ_CMP;
1191 ccb->ccb_h.status = CAM_UA_ABORT;
1194 case XPT_CALC_GEOMETRY:
1196 struct ccb_calc_geometry *ccg;
1198 u_int32_t secs_per_cylinder;
1201 size_mb = ccg->volume_size
1202 / ((1024L * 1024L) / ccg->block_size);
1205 ccg->secs_per_track = 16;
1206 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1207 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1208 ccb->ccb_h.status = CAM_REQ_CMP;
1212 case XPT_PATH_INQ: /* Path routing inquiry */
1214 struct ccb_pathinq *cpi = &ccb->cpi;
1216 cpi->version_num = 1; /* XXX??? */
1217 cpi->hba_inquiry = 0; /* nothing fancy */
1218 cpi->target_sprt = 0;
1220 cpi->hba_eng_cnt = 0;
1221 cpi->max_target = 7;
1223 cpi->initiator_id = WDS_HBA_ID;
1225 cpi->bus_id = cam_sim_bus(sim);
1226 cpi->base_transfer_speed = 3300;
1227 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1228 strncpy(cpi->hba_vid, "WD/FDC", HBA_IDLEN);
1229 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1230 cpi->unit_number = cam_sim_unit(sim);
1231 cpi->ccb_h.status = CAM_REQ_CMP;
1236 ccb->ccb_h.status = CAM_REQ_INVALID;
1243 wds_poll(struct cam_sim * sim)
1245 wds_intr((struct wds *)cam_sim_softc(sim));
1248 /* part of initialization done in probe() */
1249 /* returns 0 if OK, ENXIO if bad */
1252 wds_preinit(struct wds *wp)
1260 * Sending a command causes the CMDRDY bit to clear.
1262 outb(base + WDS_CMD, WDSC_NOOP);
1263 if (inb(base + WDS_STAT) & WDS_RDY)
1267 * the controller exists. reset and init.
1269 outb(base + WDS_HCR, WDSH_ASCRESET | WDSH_SCSIRESET);
1271 outb(base + WDS_HCR, 0);
1273 if ((inb(base + WDS_STAT) & (WDS_RDY)) != WDS_RDY) {
1274 for (i = 0; i < 10; i++) {
1275 if ((inb(base + WDS_STAT) & (WDS_RDY)) == WDS_RDY)
1279 if ((inb(base + WDS_STAT) & (WDS_RDY)) != WDS_RDY)
1287 /* part of initialization done in attach() */
1288 /* returns 0 if OK, 1 if bad */
1291 wds_init(struct wds *wp)
1293 struct wds_setup init;
1300 outb(base + WDS_HCR, WDSH_DRQEN);
1302 isa_dmacascade(wp->drq);
1304 if ((inb(base + WDS_STAT) & (WDS_RDY)) != WDS_RDY) {
1305 for (i = 0; i < 10; i++) {
1306 if ((inb(base + WDS_STAT) & (WDS_RDY)) == WDS_RDY)
1310 if ((inb(base + WDS_STAT) & (WDS_RDY)) != WDS_RDY)
1314 bzero(&init, sizeof init);
1315 init.cmd = WDSC_INIT;
1316 init.scsi_id = WDS_HBA_ID;
1319 scsi_ulto3b(WDSTOPHYS(wp, &wp->dx->ombs), init.mbaddr);
1321 init.nomb = WDS_NOMB;
1322 init.nimb = WDS_NIMB;
1324 wds_wait(base + WDS_STAT, WDS_RDY, WDS_RDY);
1325 if (wds_cmd(base, (u_int8_t *) & init, sizeof init) != 0) {
1326 device_printf(wp->dev, "wds_cmd init failed\n");
1329 wds_wait(base + WDS_STAT, WDS_INIT, WDS_INIT);
1331 wds_wait(base + WDS_STAT, WDS_RDY, WDS_RDY);
1333 bzero(&wc, sizeof wc);
1334 wc.cmd = WDSC_DISUNSOL;
1335 if (wds_cmd(base, (char *) &wc, sizeof wc) != 0) {
1336 device_printf(wp->dev, "wds_cmd init2 failed\n");
1343 wds_cmd(int base, u_int8_t * p, int l)
1349 outb(base + WDS_CMD, *p);
1350 wds_wait(base + WDS_STAT, WDS_RDY, WDS_RDY);
1351 } while (inb(base + WDS_STAT) & WDS_REJ);
1355 wds_wait(base + WDS_STAT, WDS_RDY, WDS_RDY);
1363 wds_wait(int reg, int mask, int val)
1365 while ((inb(reg) & mask) != val)
1369 static struct wds_req *
1370 cmdtovirt(struct wds *wp, u_int32_t phys)
1374 a = WDSTOVIRT(wp, (uintptr_t)phys);
1375 if( a < (char *)&wp->dx->req[0] || a>= (char *)&wp->dx->req[MAXSIMUL]) {
1376 device_printf(wp->dev, "weird phys address 0x%x\n", phys);
1379 a -= (int)offsetof(struct wds_req, cmd); /* convert cmd to request */
1380 return ((struct wds_req *)a);
1383 /* for debugging, print out all the data about the status of devices */
1392 for (unit = 0; unit < devclass_get_maxunit(wds_devclass); unit++) {
1393 wp = (struct wds *) devclass_get_device(wds_devclass, unit);
1396 printf("wds%d: want_wdsr=0x%x stat=0x%x irq=%s irqstat=0x%x\n",
1397 unit, wp->want_wdsr, inb(wp->addr + WDS_STAT) & 0xff,
1398 (inb(wp->addr + WDS_STAT) & WDS_IRQ) ? "ready" : "no",
1399 inb(wp->addr + WDS_IRQSTAT) & 0xff);
1400 for (i = 0; i < MAXSIMUL; i++) {
1401 r = &wp->dx->req[i];
1402 if( wp->wdsr_free & (1 << r->id) ) {
1403 printf("req=%d flg=0x%x ombn=%d ombstat=%d "
1404 "mask=0x%x targ=%d lun=%d cmd=0x%x\n",
1405 i, r->flags, r->ombn,
1406 wp->dx->ombs[r->ombn].stat,
1407 r->mask, r->cmd.targ >> 5,
1408 r->cmd.targ & 7, r->cmd.scb[0]);
1415 /* create circular log buffer */
1421 if (++logwrite >= NLOGLINES)
1423 if (logread == logwrite)
1424 if (++logread >= NLOGLINES)
1426 return (wds_log[n]);
1432 /* print the circular buffer */
1435 for (i = logread; i != logwrite;) {
1436 printf("%s", wds_log[i]);
1443 #endif /* WDS_DEBUG */