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