Remove the INTR_TYPE_* flags. The interrupt type is no longer used to
[dragonfly.git] / sys / dev / raid / twe / twe_freebsd.c
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2003 Paul Saab
4  * Copyright (c) 2003 Vinod Kashyap
5  * Copyright (c) 2000 BSDi
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/dev/twe/twe_freebsd.c,v 1.2.2.9 2004/06/11 18:57:31 vkashyap Exp $
30  * $DragonFly: src/sys/dev/raid/twe/twe_freebsd.c,v 1.17 2005/10/12 17:35:55 dillon Exp $
31  */
32
33 /*
34  * FreeBSD-specific code.
35  */
36
37 #include <dev/raid/twe/twe_compat.h>
38 #include <dev/raid/twe/twereg.h>
39 #include <dev/raid/twe/twe_tables.h>
40 #include <dev/raid/twe/tweio.h>
41 #include <dev/raid/twe/twevar.h>
42
43 static devclass_t       twe_devclass;
44
45 #ifdef TWE_DEBUG
46 static u_int32_t        twed_bio_in;
47 #define TWED_BIO_IN     twed_bio_in++
48 static u_int32_t        twed_bio_out;
49 #define TWED_BIO_OUT    twed_bio_out++
50 #else
51 #define TWED_BIO_IN
52 #define TWED_BIO_OUT
53 #endif
54
55 /********************************************************************************
56  ********************************************************************************
57                                                          Control device interface
58  ********************************************************************************
59  ********************************************************************************/
60
61 static  d_open_t                twe_open;
62 static  d_close_t               twe_close;
63 static  d_ioctl_t               twe_ioctl_wrapper;
64
65 static struct cdevsw twe_cdevsw = {
66         "twe",  /* name */
67         TWE_CDEV_MAJOR, /* major number */
68         0,      /* flags */
69         NULL,   /* device port */
70         NULL,   /* cloning */
71     twe_open,
72     twe_close,
73     noread,
74     nowrite,
75     twe_ioctl_wrapper,
76     nopoll,
77     nommap,
78     nostrategy,
79     nodump,
80     nopsize
81 };
82
83 /********************************************************************************
84  * Accept an open operation on the control device.
85  */
86 static int
87 twe_open(dev_t dev, int flags, int fmt, d_thread_t *td)
88 {
89     int                 unit = minor(dev);
90     struct twe_softc    *sc = devclass_get_softc(twe_devclass, unit);
91
92     sc->twe_state |= TWE_STATE_OPEN;
93     return(0);
94 }
95
96 /********************************************************************************
97  * Accept the last close on the control device.
98  */
99 static int
100 twe_close(dev_t dev, int flags, int fmt, d_thread_t *td)
101 {
102     int                 unit = minor(dev);
103     struct twe_softc    *sc = devclass_get_softc(twe_devclass, unit);
104
105     sc->twe_state &= ~TWE_STATE_OPEN;
106     return (0);
107 }
108
109 /********************************************************************************
110  * Handle controller-specific control operations.
111  */
112 static int
113 twe_ioctl_wrapper(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *td)
114 {
115     struct twe_softc            *sc = (struct twe_softc *)dev->si_drv1;
116     
117     return(twe_ioctl(sc, cmd, addr));
118 }
119
120 /********************************************************************************
121  ********************************************************************************
122                                                              PCI device interface
123  ********************************************************************************
124  ********************************************************************************/
125
126 static int      twe_probe(device_t dev);
127 static int      twe_attach(device_t dev);
128 static void     twe_free(struct twe_softc *sc);
129 static int      twe_detach(device_t dev);
130 static int      twe_shutdown(device_t dev);
131 static int      twe_suspend(device_t dev);
132 static int      twe_resume(device_t dev);
133 static void     twe_pci_intr(void *arg);
134 static void     twe_intrhook(void *arg);
135 static void     twe_free_request(struct twe_request *tr);
136 static void     twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs,
137                                                                   int nsegments, int error);
138 static void     twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs,
139                                                                          int nsegments, int error);
140
141 static device_method_t twe_methods[] = {
142     /* Device interface */
143     DEVMETHOD(device_probe,     twe_probe),
144     DEVMETHOD(device_attach,    twe_attach),
145     DEVMETHOD(device_detach,    twe_detach),
146     DEVMETHOD(device_shutdown,  twe_shutdown),
147     DEVMETHOD(device_suspend,   twe_suspend),
148     DEVMETHOD(device_resume,    twe_resume),
149
150     DEVMETHOD(bus_print_child,  bus_generic_print_child),
151     DEVMETHOD(bus_driver_added, bus_generic_driver_added),
152     { 0, 0 }
153 };
154
155 static driver_t twe_pci_driver = {
156         "twe",
157         twe_methods,
158         sizeof(struct twe_softc)
159 };
160
161 #ifdef TWE_OVERRIDE
162 DRIVER_MODULE(Xtwe, pci, twe_pci_driver, twe_devclass, 0, 0);
163 #else
164 DRIVER_MODULE(twe, pci, twe_pci_driver, twe_devclass, 0, 0);
165 #endif
166
167 /********************************************************************************
168  * Match a 3ware Escalade ATA RAID controller.
169  */
170 static int
171 twe_probe(device_t dev)
172 {
173
174     debug_called(4);
175
176     if ((pci_get_vendor(dev) == TWE_VENDOR_ID) &&
177         ((pci_get_device(dev) == TWE_DEVICE_ID) || 
178          (pci_get_device(dev) == TWE_DEVICE_ID_ASIC))) {
179         device_set_desc(dev, TWE_DEVICE_NAME " driver ver. " TWE_DRIVER_VERSION_STRING);
180 #ifdef TWE_OVERRIDE
181         return(0);
182 #else
183         return(-10);
184 #endif
185     }
186     return(ENXIO);
187 }
188
189 /********************************************************************************
190  * Allocate resources, initialise the controller.
191  */
192 static int
193 twe_attach(device_t dev)
194 {
195     struct twe_softc    *sc;
196     int                 rid, error;
197     u_int32_t           command;
198
199     debug_called(4);
200
201     /*
202      * Initialise the softc structure.
203      */
204     sc = device_get_softc(dev);
205     sc->twe_dev = dev;
206
207     sysctl_ctx_init(&sc->sysctl_ctx);
208     sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
209         SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
210         device_get_nameunit(dev), CTLFLAG_RD, 0, "");
211     if (sc->sysctl_tree == NULL) {
212         twe_printf(sc, "cannot add sysctl tree node\n");
213         return (ENXIO);
214     }
215     SYSCTL_ADD_STRING(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
216         OID_AUTO, "driver_version", CTLFLAG_RD, TWE_DRIVER_VERSION_STRING, 0,
217         "TWE driver version");
218
219     /*
220      * Make sure we are going to be able to talk to this board.
221      */
222     command = pci_read_config(dev, PCIR_COMMAND, 2);
223     if ((command & PCIM_CMD_PORTEN) == 0) {
224         twe_printf(sc, "register window not available\n");
225         return(ENXIO);
226     }
227     /*
228      * Force the busmaster enable bit on, in case the BIOS forgot.
229      */
230     command |= PCIM_CMD_BUSMASTEREN;
231     pci_write_config(dev, PCIR_COMMAND, command, 2);
232
233     /*
234      * Allocate the PCI register window.
235      */
236     rid = TWE_IO_CONFIG_REG;
237     if ((sc->twe_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) {
238         twe_printf(sc, "can't allocate register window\n");
239         twe_free(sc);
240         return(ENXIO);
241     }
242     sc->twe_btag = rman_get_bustag(sc->twe_io);
243     sc->twe_bhandle = rman_get_bushandle(sc->twe_io);
244
245     /*
246      * Allocate the parent bus DMA tag appropriate for PCI.
247      */
248     if (bus_dma_tag_create(NULL,                                /* parent */
249                            1, 0,                                /* alignment, boundary */
250                            BUS_SPACE_MAXADDR_32BIT,             /* lowaddr */
251                            BUS_SPACE_MAXADDR,                   /* highaddr */
252                            NULL, NULL,                          /* filter, filterarg */
253                            MAXBSIZE, TWE_MAX_SGL_LENGTH,        /* maxsize, nsegments */
254                            BUS_SPACE_MAXSIZE_32BIT,             /* maxsegsize */
255                            BUS_DMA_ALLOCNOW,                    /* flags */
256                            &sc->twe_parent_dmat)) {
257         twe_printf(sc, "can't allocate parent DMA tag\n");
258         twe_free(sc);
259         return(ENOMEM);
260     }
261
262     /* 
263      * Allocate and connect our interrupt.
264      */
265     rid = 0;
266     if ((sc->twe_irq = bus_alloc_resource(sc->twe_dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
267         twe_printf(sc, "can't allocate interrupt\n");
268         twe_free(sc);
269         return(ENXIO);
270     }
271     if (bus_setup_intr(sc->twe_dev, sc->twe_irq, INTR_ENTROPY,
272                         twe_pci_intr, sc, &sc->twe_intr, NULL)) {
273         twe_printf(sc, "can't set up interrupt\n");
274         twe_free(sc);
275         return(ENXIO);
276     }
277
278     /*
279      * Create DMA tag for mapping objects into controller-addressable space.
280      */
281     if (bus_dma_tag_create(sc->twe_parent_dmat,         /* parent */
282                            1, 0,                        /* alignment, boundary */
283                            BUS_SPACE_MAXADDR,           /* lowaddr */
284                            BUS_SPACE_MAXADDR,           /* highaddr */
285                            NULL, NULL,                  /* filter, filterarg */
286                            MAXBSIZE, TWE_MAX_SGL_LENGTH,/* maxsize, nsegments */
287                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
288                            0,                           /* flags */
289                            &sc->twe_buffer_dmat)) {
290         twe_printf(sc, "can't allocate data buffer DMA tag\n");
291         twe_free(sc);
292         return(ENOMEM);
293     }
294
295     /*
296      * Initialise the controller and driver core.
297      */
298     if ((error = twe_setup(sc))) {
299         twe_free(sc);
300         return(error);
301     }
302
303     /*
304      * Print some information about the controller and configuration.
305      */
306     twe_describe_controller(sc);
307
308     /*
309      * Create the control device.
310      */
311         cdevsw_add(&twe_cdevsw, -1, device_get_unit(sc->twe_dev));
312     sc->twe_dev_t = make_dev(&twe_cdevsw, device_get_unit(sc->twe_dev),
313                         UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR, "twe%d",
314                         device_get_unit(sc->twe_dev));
315     sc->twe_dev_t->si_drv1 = sc;
316     /*
317      * Schedule ourselves to bring the controller up once interrupts are available.
318      * This isn't strictly necessary, since we disable interrupts while probing the
319      * controller, but it is more in keeping with common practice for other disk 
320      * devices.
321      */
322     sc->twe_ich.ich_func = twe_intrhook;
323     sc->twe_ich.ich_arg = sc;
324     if (config_intrhook_establish(&sc->twe_ich) != 0) {
325         twe_printf(sc, "can't establish configuration hook\n");
326         twe_free(sc);
327         return(ENXIO);
328     }
329
330     return(0);
331 }
332
333 /********************************************************************************
334  * Free all of the resources associated with (sc).
335  *
336  * Should not be called if the controller is active.
337  */
338 static void
339 twe_free(struct twe_softc *sc)
340 {
341     struct twe_request  *tr;
342
343     debug_called(4);
344
345     /* throw away any command buffers */
346     while ((tr = twe_dequeue_free(sc)) != NULL)
347         twe_free_request(tr);
348
349     /* destroy the data-transfer DMA tag */
350     if (sc->twe_buffer_dmat)
351         bus_dma_tag_destroy(sc->twe_buffer_dmat);
352
353     /* disconnect the interrupt handler */
354     if (sc->twe_intr)
355         bus_teardown_intr(sc->twe_dev, sc->twe_irq, sc->twe_intr);
356     if (sc->twe_irq != NULL)
357         bus_release_resource(sc->twe_dev, SYS_RES_IRQ, 0, sc->twe_irq);
358
359     /* destroy the parent DMA tag */
360     if (sc->twe_parent_dmat)
361         bus_dma_tag_destroy(sc->twe_parent_dmat);
362
363     /* release the register window mapping */
364     if (sc->twe_io != NULL)
365         bus_release_resource(sc->twe_dev, SYS_RES_IOPORT, TWE_IO_CONFIG_REG, sc->twe_io);
366
367         cdevsw_remove(&twe_cdevsw, -1, device_get_unit(sc->twe_dev));
368     /* destroy control device */
369     if (sc->twe_dev_t != (dev_t)NULL)
370         destroy_dev(sc->twe_dev_t);
371
372     sysctl_ctx_free(&sc->sysctl_ctx);
373 }
374
375 /********************************************************************************
376  * Disconnect from the controller completely, in preparation for unload.
377  */
378 static int
379 twe_detach(device_t dev)
380 {
381     struct twe_softc    *sc = device_get_softc(dev);
382     int                 error;
383
384     debug_called(4);
385
386     error = EBUSY;
387     crit_enter();
388     if (sc->twe_state & TWE_STATE_OPEN)
389         goto out;
390
391     /*  
392      * Shut the controller down.
393      */
394     if ((error = twe_shutdown(dev)))
395         goto out;
396
397     twe_free(sc);
398
399     error = 0;
400  out:
401     crit_exit();
402     return(error);
403 }
404
405 /********************************************************************************
406  * Bring the controller down to a dormant state and detach all child devices.
407  *
408  * Note that we can assume that the bioq on the controller is empty, as we won't
409  * allow shutdown if any device is open.
410  */
411 static int
412 twe_shutdown(device_t dev)
413 {
414     struct twe_softc    *sc = device_get_softc(dev);
415     int                 i, error = 0;
416
417     debug_called(4);
418
419     crit_enter();
420
421     /* 
422      * Delete all our child devices.
423      */
424     for (i = 0; i < TWE_MAX_UNITS; i++) {
425       if (sc->twe_drive[i].td_disk != 0)
426         if ((error = twe_detach_drive(sc, i)) != 0)
427             goto out;
428     }
429
430     /*
431      * Bring the controller down.
432      */
433     twe_deinit(sc);
434
435  out:
436     crit_exit();
437     return(error);
438 }
439
440 /********************************************************************************
441  * Bring the controller to a quiescent state, ready for system suspend.
442  */
443 static int
444 twe_suspend(device_t dev)
445 {
446     struct twe_softc    *sc = device_get_softc(dev);
447
448     debug_called(4);
449
450     crit_enter();
451     sc->twe_state |= TWE_STATE_SUSPEND;
452     
453     twe_disable_interrupts(sc);
454     crit_exit();
455
456     return(0);
457 }
458
459 /********************************************************************************
460  * Bring the controller back to a state ready for operation.
461  */
462 static int
463 twe_resume(device_t dev)
464 {
465     struct twe_softc    *sc = device_get_softc(dev);
466
467     debug_called(4);
468
469     sc->twe_state &= ~TWE_STATE_SUSPEND;
470     twe_enable_interrupts(sc);
471
472     return(0);
473 }
474
475 /*******************************************************************************
476  * Take an interrupt, or be poked by other code to look for interrupt-worthy
477  * status.
478  */
479 static void
480 twe_pci_intr(void *arg)
481 {
482     twe_intr((struct twe_softc *)arg);
483 }
484
485 /********************************************************************************
486  * Delayed-startup hook
487  */
488 static void
489 twe_intrhook(void *arg)
490 {
491     struct twe_softc            *sc = (struct twe_softc *)arg;
492
493     /* pull ourselves off the intrhook chain */
494     config_intrhook_disestablish(&sc->twe_ich);
495
496     /* call core startup routine */
497     twe_init(sc);
498 }
499
500 /********************************************************************************
501  * Given a detected drive, attach it to the bio interface.
502  *
503  * This is called from twe_add_unit.
504  */
505 int
506 twe_attach_drive(struct twe_softc *sc, struct twe_drive *dr)
507 {
508     char        buf[80];
509     int         error = 0;
510
511     dr->td_disk =  device_add_child(sc->twe_dev, NULL, -1);
512     if (dr->td_disk == NULL) {
513         twe_printf(sc, "Cannot add unit\n");
514         return (EIO);
515     }
516     device_set_ivars(dr->td_disk, dr);
517
518     /* 
519      * XXX It would make sense to test the online/initialising bits, but they seem to be
520      * always set...
521      */
522     sprintf(buf, "Unit %d, %s, %s",
523             dr->td_twe_unit,
524             twe_describe_code(twe_table_unittype, dr->td_type),
525             twe_describe_code(twe_table_unitstate, dr->td_state & TWE_PARAM_UNITSTATUS_MASK));
526     device_set_desc_copy(dr->td_disk, buf);
527
528     if ((error = bus_generic_attach(sc->twe_dev)) != 0) {
529         twe_printf(sc, "Cannot attach unit to controller. error = %d\n", error);
530         error = EIO;
531     }
532     return (error);
533 }
534
535 /********************************************************************************
536  * Detach the specified unit if it exsists
537  *
538  * This is called from twe_del_unit.
539  */
540 int
541 twe_detach_drive(struct twe_softc *sc, int unit)
542 {
543     int error = 0;
544
545     if ((error = device_delete_child(sc->twe_dev, sc->twe_drive[unit].td_disk))) {
546         twe_printf(sc, "Cannot delete unit. error = %d\n", error);
547         return (error);
548     }
549     bzero(&sc->twe_drive[unit], sizeof(sc->twe_drive[unit]));
550     return (error);
551 }
552
553 /********************************************************************************
554  * Clear a PCI parity error.
555  */
556 void
557 twe_clear_pci_parity_error(struct twe_softc *sc)
558 {
559     TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PARITY_ERROR);
560     pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PARITY_ERROR, 2);
561 }
562
563 /********************************************************************************
564  * Clear a PCI abort.
565  */
566 void
567 twe_clear_pci_abort(struct twe_softc *sc)
568 {
569     TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PCI_ABORT);
570     pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PCI_ABORT, 2);
571 }
572
573 /********************************************************************************
574  ********************************************************************************
575                                                                       Disk device
576  ********************************************************************************
577  ********************************************************************************/
578
579 /*
580  * Disk device bus interface
581  */
582 static int twed_probe(device_t dev);
583 static int twed_attach(device_t dev);
584 static int twed_detach(device_t dev);
585
586 static device_method_t twed_methods[] = {
587     DEVMETHOD(device_probe,     twed_probe),
588     DEVMETHOD(device_attach,    twed_attach),
589     DEVMETHOD(device_detach,    twed_detach),
590     { 0, 0 }
591 };
592
593 static driver_t twed_driver = {
594     "twed",
595     twed_methods,
596     sizeof(struct twed_softc)
597 };
598
599 static devclass_t       twed_devclass;
600 #ifdef TWE_OVERRIDE
601 DRIVER_MODULE(Xtwed, Xtwe, twed_driver, twed_devclass, 0, 0);
602 #else
603 DRIVER_MODULE(twed, twe, twed_driver, twed_devclass, 0, 0);
604 #endif
605
606 /*
607  * Disk device control interface.
608  */
609 static  d_open_t        twed_open;
610 static  d_close_t       twed_close;
611 static  d_strategy_t    twed_strategy;
612 static  d_dump_t        twed_dump;
613
614 static struct cdevsw twed_cdevsw = {
615         "twed",
616         TWED_CDEV_MAJOR,
617         D_DISK,
618         /* port */ NULL,
619         /* clone */ NULL,
620     twed_open,
621     twed_close,
622     physread,
623     physwrite,
624     noioctl,
625     nopoll,
626     nommap,
627     twed_strategy,
628     twed_dump,
629     nopsize
630 };
631
632 #if 0
633 static struct cdevsw    tweddisk_cdevsw;
634 #endif
635 #ifdef FREEBSD_4
636 static int              disks_registered = 0;
637 #endif
638
639 /********************************************************************************
640  * Handle open from generic layer.
641  *
642  * Note that this is typically only called by the diskslice code, and not
643  * for opens on subdevices (eg. slices, partitions).
644  */
645 static int
646 twed_open(dev_t dev, int flags, int fmt, d_thread_t *td)
647 {
648     struct twed_softc   *sc = (struct twed_softc *)dev->si_drv1;
649     struct disklabel    *label;
650
651     debug_called(4);
652         
653     if (sc == NULL)
654         return (ENXIO);
655
656     /* check that the controller is up and running */
657     if (sc->twed_controller->twe_state & TWE_STATE_SHUTDOWN)
658         return(ENXIO);
659
660     /* build synthetic label */
661     label = &sc->twed_disk.d_label;
662     bzero(label, sizeof(*label));
663     label->d_type = DTYPE_ESDI;
664     label->d_secsize    = TWE_BLOCK_SIZE;
665     label->d_nsectors   = sc->twed_drive->td_sectors;
666     label->d_ntracks    = sc->twed_drive->td_heads;
667     label->d_ncylinders = sc->twed_drive->td_cylinders;
668     label->d_secpercyl  = sc->twed_drive->td_sectors * sc->twed_drive->td_heads;
669     label->d_secperunit = sc->twed_drive->td_size;
670
671     sc->twed_flags |= TWED_OPEN;
672     return (0);
673 }
674
675 /********************************************************************************
676  * Handle last close of the disk device.
677  */
678 static int
679 twed_close(dev_t dev, int flags, int fmt, d_thread_t *td)
680 {
681     struct twed_softc   *sc = (struct twed_softc *)dev->si_drv1;
682
683     debug_called(4);
684         
685     if (sc == NULL)
686         return (ENXIO);
687
688     sc->twed_flags &= ~TWED_OPEN;
689     return (0);
690 }
691
692 /********************************************************************************
693  * Handle an I/O request.
694  */
695 static void
696 twed_strategy(twe_bio *bp)
697 {
698     struct twed_softc   *sc = (struct twed_softc *)TWE_BIO_SOFTC(bp);
699
700     debug_called(4);
701
702     TWED_BIO_IN;
703
704     /* bogus disk? */
705     if ((sc == NULL) || (!sc->twed_drive->td_disk)) {
706         TWE_BIO_SET_ERROR(bp, EINVAL);
707         printf("twe: bio for invalid disk!\n");
708         TWE_BIO_DONE(bp);
709         TWED_BIO_OUT;
710         return;
711     }
712
713     /* perform accounting */
714     TWE_BIO_STATS_START(bp);
715
716     /* queue the bio on the controller */
717     twe_enqueue_bio(sc->twed_controller, bp);
718
719     /* poke the controller to start I/O */
720     twe_startio(sc->twed_controller);
721     return;
722 }
723
724 /********************************************************************************
725  * System crashdump support
726  */
727 static int
728 twed_dump(dev_t dev, u_int count, u_int blkno, u_int secsize)
729 {
730     struct twed_softc   *twed_sc = (struct twed_softc *)dev->si_drv1;
731     struct twe_softc    *twe_sc  = (struct twe_softc *)twed_sc->twed_controller;
732 #if 0
733     u_int               count, blkno, secsize;
734 #endif
735     vm_paddr_t          addr = 0;
736     long                blkcnt;
737     int                 dumppages = MAXDUMPPGS;
738     int                 error;
739     int                 i;
740
741 #if 0
742     if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
743         return(error);
744 #endif
745
746     if (!twed_sc || !twe_sc)
747         return(ENXIO);
748
749     blkcnt = howmany(PAGE_SIZE, secsize);
750
751     while (count > 0) {
752         caddr_t va = NULL;
753
754         if ((count / blkcnt) < dumppages)
755             dumppages = count / blkcnt;
756
757         for (i = 0; i < dumppages; ++i) {
758             vm_paddr_t a = addr + (i * PAGE_SIZE);
759             if (is_physical_memory(a))
760                 va = pmap_kenter_temporary(trunc_page(a), i);
761             else
762                 va = pmap_kenter_temporary(trunc_page(0), i);
763         }
764
765         if ((error = twe_dump_blocks(twe_sc, twed_sc->twed_drive->td_twe_unit, blkno, va, 
766                                      (PAGE_SIZE * dumppages) / TWE_BLOCK_SIZE)) != 0)
767             return(error);
768
769
770         if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
771             return(EINTR);
772
773         blkno += blkcnt * dumppages;
774         count -= blkcnt * dumppages;
775         addr += PAGE_SIZE * dumppages;
776     }
777     return(0);
778 }
779
780 /********************************************************************************
781  * Handle completion of an I/O request.
782  */
783 void
784 twed_intr(twe_bio *bp)
785 {
786     debug_called(4);
787
788     /* if no error, transfer completed */
789     if (!TWE_BIO_HAS_ERROR(bp))
790         TWE_BIO_RESID(bp) = 0;
791
792     TWE_BIO_STATS_END(bp);
793     TWE_BIO_DONE(bp);
794     TWED_BIO_OUT;
795 }
796
797 /********************************************************************************
798  * Default probe stub.
799  */
800 static int
801 twed_probe(device_t dev)
802 {
803     return (0);
804 }
805
806 /********************************************************************************
807  * Attach a unit to the controller.
808  */
809 static int
810 twed_attach(device_t dev)
811 {
812     struct twed_softc   *sc;
813     device_t            parent;
814     dev_t               dsk;
815     
816     debug_called(4);
817
818     /* initialise our softc */
819     sc = device_get_softc(dev);
820     parent = device_get_parent(dev);
821     sc->twed_controller = (struct twe_softc *)device_get_softc(parent);
822     sc->twed_drive = device_get_ivars(dev);
823     sc->twed_drive->td_sys_unit = device_get_unit(dev);
824     sc->twed_dev = dev;
825
826     /* report the drive */
827     twed_printf(sc, "%uMB (%u sectors)\n",
828                 sc->twed_drive->td_size / ((1024 * 1024) / TWE_BLOCK_SIZE),
829                 sc->twed_drive->td_size);
830     
831     devstat_add_entry(&sc->twed_stats, "twed", sc->twed_drive->td_sys_unit,
832                         TWE_BLOCK_SIZE,
833                         DEVSTAT_NO_ORDERED_TAGS,
834                         DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 
835                         DEVSTAT_PRIORITY_ARRAY);
836
837     /* attach a generic disk device to ourselves */
838     dsk = disk_create(sc->twed_drive->td_sys_unit, &sc->twed_disk,
839                         0, &twed_cdevsw);
840     dsk->si_drv1 = sc;
841 /*    dsk->si_drv2 = sc->twed_drive;*/
842     sc->twed_dev_t = dsk;
843 #ifdef FREEBSD_4
844     disks_registered++;
845 #endif
846
847     /* set the maximum I/O size to the theoretical maximum allowed by the S/G list size */
848     dsk->si_iosize_max = (TWE_MAX_SGL_LENGTH - 1) * PAGE_SIZE;
849
850     return (0);
851 }
852
853 /********************************************************************************
854  * Disconnect ourselves from the system.
855  */
856 static int
857 twed_detach(device_t dev)
858 {
859     struct twed_softc *sc = (struct twed_softc *)device_get_softc(dev);
860
861     debug_called(4);
862
863     if (sc->twed_flags & TWED_OPEN)
864         return(EBUSY);
865
866     devstat_remove_entry(&sc->twed_stats);
867     disk_destroy(&sc->twed_disk);
868 #ifdef FREEBSD_4
869         printf("Disks registered: %d\n", disks_registered);
870 #if 0
871     if (--disks_registered == 0)
872         cdevsw_remove(&tweddisk_cdevsw);
873 #endif
874 #endif
875
876     return(0);
877 }
878
879 /********************************************************************************
880  ********************************************************************************
881                                                                              Misc
882  ********************************************************************************
883  ********************************************************************************/
884
885 MALLOC_DEFINE(TWE_MALLOC_CLASS, "twe commands", "twe commands");
886 /********************************************************************************
887  * Allocate a command buffer
888  */
889 struct twe_request *
890 twe_allocate_request(struct twe_softc *sc)
891 {
892     struct twe_request  *tr;
893         int aligned_size;
894
895     /*
896      * TWE requires requests to be 512-byte aligned.  Depend on malloc()
897      * guarenteeing alignment for power-of-2 requests.  Note that the old
898      * (FreeBSD-4.x) malloc code aligned all requests, but the new slab
899      * allocator only guarentees same-size alignment for power-of-2 requests.
900      */
901     aligned_size = (sizeof(struct twe_request) + TWE_ALIGNMASK) &
902            ~TWE_ALIGNMASK;
903     tr = malloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT|M_ZERO);
904     tr->tr_sc = sc;
905     if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_cmdmap)) {
906         twe_free_request(tr);
907         return(NULL);
908     }
909     bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_cmdmap, &tr->tr_command,
910         sizeof(tr->tr_command), twe_setup_request_dmamap, tr, 0);
911     if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_dmamap)) {
912         bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_cmdmap);
913         twe_free_request(tr);
914         return(NULL);
915     }    
916     return(tr);
917 }
918
919 /********************************************************************************
920  * Permanently discard a command buffer.
921  */
922 static void
923 twe_free_request(struct twe_request *tr) 
924 {
925     struct twe_softc    *sc = tr->tr_sc;
926     
927     debug_called(4);
928
929     bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_cmdmap); 
930     bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_cmdmap);
931     bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_dmamap);
932     free(tr, TWE_MALLOC_CLASS);
933 }
934
935 /********************************************************************************
936  * Map/unmap (tr)'s command and data in the controller's addressable space.
937  *
938  * These routines ensure that the data which the controller is going to try to
939  * access is actually visible to the controller, in a machine-independant 
940  * fashion.  Due to a hardware limitation, I/O buffers must be 512-byte aligned
941  * and we take care of that here as well.
942  */
943 static void
944 twe_fillin_sgl(TWE_SG_Entry *sgl, bus_dma_segment_t *segs, int nsegments, int max_sgl)
945 {
946     int i;
947
948     for (i = 0; i < nsegments; i++) {
949         sgl[i].address = segs[i].ds_addr;
950         sgl[i].length = segs[i].ds_len;
951     }
952     for (; i < max_sgl; i++) {                          /* XXX necessary? */
953         sgl[i].address = 0;
954         sgl[i].length = 0;
955     }
956 }
957                 
958 static void
959 twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
960 {
961     struct twe_request  *tr = (struct twe_request *)arg;
962     TWE_Command         *cmd = &tr->tr_command;
963
964     debug_called(4);
965
966     if (tr->tr_flags & TWE_CMD_MAPPED)
967         panic("already mapped command");
968
969     tr->tr_flags |= TWE_CMD_MAPPED;
970
971     if (tr->tr_flags & TWE_CMD_IN_PROGRESS)
972         tr->tr_sc->twe_state &= ~TWE_STATE_FRZN;
973     /* save base of first segment in command (applicable if there only one segment) */
974     tr->tr_dataphys = segs[0].ds_addr;
975
976     /* correct command size for s/g list size */
977     tr->tr_command.generic.size += 2 * nsegments;
978
979     /*
980      * Due to the fact that parameter and I/O commands have the scatter/gather list in
981      * different places, we need to determine which sort of command this actually is
982      * before we can populate it correctly.
983      */
984     switch(cmd->generic.opcode) {
985     case TWE_OP_GET_PARAM:
986     case TWE_OP_SET_PARAM:
987         cmd->generic.sgl_offset = 2;
988         twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
989         break;
990     case TWE_OP_READ:
991     case TWE_OP_WRITE:
992         cmd->generic.sgl_offset = 3;
993         twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
994         break;
995     case TWE_OP_ATA_PASSTHROUGH:
996         cmd->generic.sgl_offset = 5;
997         twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
998         break;
999     default:
1000         /*
1001          * Fall back to what the linux driver does.
1002          * Do this because the API may send an opcode
1003          * the driver knows nothing about and this will
1004          * at least stop PCIABRT's from hosing us.
1005          */
1006         switch (cmd->generic.sgl_offset) {
1007         case 2:
1008             twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1009             break;
1010         case 3:
1011             twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1012             break;
1013         case 5:
1014             twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1015             break;
1016         }
1017     }
1018     if (tr->tr_flags & TWE_CMD_DATAIN)
1019         bus_dmamap_sync(tr->tr_sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_PREREAD);
1020     if (tr->tr_flags & TWE_CMD_DATAOUT) {
1021         /* if we're using an alignment buffer, and we're writing data, copy the real data out */
1022         if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1023             bcopy(tr->tr_realdata, tr->tr_data, tr->tr_length);
1024         bus_dmamap_sync(tr->tr_sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_PREWRITE);
1025     }
1026     if (twe_start(tr) == EBUSY) {
1027         tr->tr_sc->twe_state |= TWE_STATE_CTLR_BUSY;
1028         twe_requeue_ready(tr);
1029     }
1030 }
1031
1032 static void
1033 twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1034 {
1035     struct twe_request  *tr = (struct twe_request *)arg;
1036
1037     debug_called(4);
1038
1039     /* command can't cross a page boundary */
1040     tr->tr_cmdphys = segs[0].ds_addr;
1041 }
1042
1043 int
1044 twe_map_request(struct twe_request *tr)
1045 {
1046     struct twe_softc    *sc = tr->tr_sc;
1047     int                 error = 0;
1048
1049     debug_called(4);
1050
1051     if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN)) {
1052         twe_requeue_ready(tr);
1053         return (EBUSY);
1054     }
1055
1056     /*
1057      * Map the command into bus space.
1058      */
1059     bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_cmdmap, BUS_DMASYNC_PREWRITE);
1060
1061     /*
1062      * If the command involves data, map that too.
1063      */
1064     if ((tr->tr_data != NULL) && ((tr->tr_flags & TWE_CMD_MAPPED) == 0)) {
1065
1066         /* 
1067          * Data must be 512-byte aligned; allocate a fixup buffer if it's not.
1068          *
1069          * DragonFly's malloc only guarentees alignment for requests which
1070          * are power-of-2 sized.
1071          */
1072         if (((vm_offset_t)tr->tr_data % TWE_ALIGNMENT) != 0) {
1073             int aligned_size;
1074
1075             tr->tr_realdata = tr->tr_data;      /* save pointer to 'real' data */
1076             aligned_size = TWE_ALIGNMENT;
1077             while (aligned_size < tr->tr_length)
1078                 aligned_size <<= 1;
1079             tr->tr_flags |= TWE_CMD_ALIGNBUF;
1080             tr->tr_data = malloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT);
1081             if (tr->tr_data == NULL) {
1082                 twe_printf(sc, "%s: malloc failed\n", __func__);
1083                 tr->tr_data = tr->tr_realdata; /* restore original data pointer */
1084                 return(ENOMEM);
1085             }
1086         }
1087         
1088         /*
1089          * Map the data buffer into bus space and build the s/g list.
1090          */
1091         if ((error = bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_dmamap, tr->tr_data,
1092                         tr->tr_length, twe_setup_data_dmamap, tr, BUS_DMA_NOWAIT)
1093                         == EINPROGRESS)) {
1094             tr->tr_flags |= TWE_CMD_IN_PROGRESS;
1095             sc->twe_state |= TWE_STATE_FRZN;
1096             error = 0;
1097         }
1098     } else {
1099         if ((error = twe_start(tr)) == EBUSY) {
1100             sc->twe_state |= TWE_STATE_CTLR_BUSY;
1101             twe_requeue_ready(tr);
1102         }
1103     }
1104
1105     return(error);
1106 }
1107
1108 void
1109 twe_unmap_request(struct twe_request *tr)
1110 {
1111     struct twe_softc    *sc = tr->tr_sc;
1112     debug_called(4);
1113
1114     /*
1115      * Unmap the command from bus space.
1116      */
1117     bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_cmdmap, BUS_DMASYNC_POSTWRITE);
1118
1119     /*
1120      * If the command involved data, unmap that too.
1121      */
1122     if (tr->tr_data != NULL) {
1123         
1124         if (tr->tr_flags & TWE_CMD_DATAIN) {
1125             bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_POSTREAD);
1126             /* if we're using an alignment buffer, and we're reading data, copy the real data in */
1127             if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1128                 bcopy(tr->tr_data, tr->tr_realdata, tr->tr_length);
1129         }
1130         if (tr->tr_flags & TWE_CMD_DATAOUT)
1131             bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_POSTWRITE);
1132
1133         bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_dmamap); 
1134     }
1135
1136     /* free alignment buffer if it was used */
1137     if (tr->tr_flags & TWE_CMD_ALIGNBUF) {
1138         free(tr->tr_data, TWE_MALLOC_CLASS);
1139         tr->tr_data = tr->tr_realdata;          /* restore 'real' data pointer */
1140     }
1141 }
1142
1143 #ifdef TWE_DEBUG
1144 void twe_report(void);
1145 /********************************************************************************
1146  * Print current controller status, call from DDB.
1147  */
1148 void
1149 twe_report(void)
1150 {
1151     struct twe_softc    *sc;
1152     int                 i;
1153
1154     crit_enter();
1155     for (i = 0; (sc = devclass_get_softc(twe_devclass, i)) != NULL; i++)
1156         twe_print_controller(sc);
1157     printf("twed: total bio count in %u  out %u\n", twed_bio_in, twed_bio_out);
1158     crit_exit();
1159 }
1160 #endif