2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2003 Paul Saab
4 * Copyright (c) 2003 Vinod Kashyap
5 * Copyright (c) 2000 BSDi
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
29 * $FreeBSD: src/sys/dev/twe/twe_freebsd.c,v 1.48 2009/12/25 17:34:43 mav Exp $
33 * FreeBSD-specific code.
36 #include <dev/raid/twe/twe_compat.h>
37 #include <dev/raid/twe/twereg.h>
38 #include <dev/raid/twe/tweio.h>
39 #include <dev/raid/twe/twevar.h>
40 #include <dev/raid/twe/twe_tables.h>
41 #include <sys/dtype.h>
45 static devclass_t twe_devclass;
48 static u_int32_t twed_bio_in;
49 #define TWED_BIO_IN twed_bio_in++
50 static u_int32_t twed_bio_out;
51 #define TWED_BIO_OUT twed_bio_out++
57 static void twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
58 static void twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
60 /********************************************************************************
61 ********************************************************************************
62 Control device interface
63 ********************************************************************************
64 ********************************************************************************/
66 static d_open_t twe_open;
67 static d_close_t twe_close;
68 static d_ioctl_t twe_ioctl_wrapper;
70 static struct dev_ops twe_ops = {
74 .d_ioctl = twe_ioctl_wrapper,
77 /********************************************************************************
78 * Accept an open operation on the control device.
81 twe_open(struct dev_open_args *ap)
83 cdev_t dev = ap->a_head.a_dev;
84 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1;
86 sc->twe_state |= TWE_STATE_OPEN;
90 /********************************************************************************
91 * Accept the last close on the control device.
94 twe_close(struct dev_close_args *ap)
96 cdev_t dev = ap->a_head.a_dev;
97 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1;
99 sc->twe_state &= ~TWE_STATE_OPEN;
103 /********************************************************************************
104 * Handle controller-specific control operations.
107 twe_ioctl_wrapper(struct dev_ioctl_args *ap)
109 cdev_t dev = ap->a_head.a_dev;
110 u_long cmd = ap->a_cmd;
111 caddr_t addr = ap->a_data;
112 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1;
114 return(twe_ioctl(sc, cmd, addr));
117 /********************************************************************************
118 ********************************************************************************
120 ********************************************************************************
121 ********************************************************************************/
123 static int twe_probe(device_t dev);
124 static int twe_attach(device_t dev);
125 static void twe_free(struct twe_softc *sc);
126 static int twe_detach(device_t dev);
127 static int twe_shutdown(device_t dev);
128 static int twe_suspend(device_t dev);
129 static int twe_resume(device_t dev);
130 static void twe_pci_intr(void *arg);
131 static void twe_intrhook(void *arg);
133 static device_method_t twe_methods[] = {
134 /* Device interface */
135 DEVMETHOD(device_probe, twe_probe),
136 DEVMETHOD(device_attach, twe_attach),
137 DEVMETHOD(device_detach, twe_detach),
138 DEVMETHOD(device_shutdown, twe_shutdown),
139 DEVMETHOD(device_suspend, twe_suspend),
140 DEVMETHOD(device_resume, twe_resume),
142 DEVMETHOD(bus_print_child, bus_generic_print_child),
143 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
147 static driver_t twe_pci_driver = {
150 sizeof(struct twe_softc)
153 DRIVER_MODULE(twe, pci, twe_pci_driver, twe_devclass, NULL, NULL);
155 /********************************************************************************
156 * Match a 3ware Escalade ATA RAID controller.
159 twe_probe(device_t dev)
164 if ((pci_get_vendor(dev) == TWE_VENDOR_ID) &&
165 ((pci_get_device(dev) == TWE_DEVICE_ID) ||
166 (pci_get_device(dev) == TWE_DEVICE_ID_ASIC))) {
167 device_set_desc_copy(dev, TWE_DEVICE_NAME ". Driver version " TWE_DRIVER_VERSION_STRING);
168 return(BUS_PROBE_DEFAULT);
173 /********************************************************************************
174 * Allocate resources, initialise the controller.
177 twe_attach(device_t dev)
179 struct twe_softc *sc;
186 * Initialise the softc structure.
188 sc = device_get_softc(dev);
191 sysctl_ctx_init(&sc->sysctl_ctx);
192 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
193 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
194 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
195 if (sc->sysctl_tree == NULL) {
196 twe_printf(sc, "cannot add sysctl tree node\n");
199 SYSCTL_ADD_STRING(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
200 OID_AUTO, "driver_version", CTLFLAG_RD, TWE_DRIVER_VERSION_STRING, 0,
201 "TWE driver version");
204 * Make sure we are going to be able to talk to this board.
206 command = pci_read_config(dev, PCIR_COMMAND, 2);
207 if ((command & PCIM_CMD_PORTEN) == 0) {
208 twe_printf(sc, "register window not available\n");
212 * Force the busmaster enable bit on, in case the BIOS forgot.
214 command |= PCIM_CMD_BUSMASTEREN;
215 pci_write_config(dev, PCIR_COMMAND, command, 2);
218 * Allocate the PCI register window.
220 rid = TWE_IO_CONFIG_REG;
221 if ((sc->twe_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
222 RF_ACTIVE)) == NULL) {
223 twe_printf(sc, "can't allocate register window\n");
227 sc->twe_btag = rman_get_bustag(sc->twe_io);
228 sc->twe_bhandle = rman_get_bushandle(sc->twe_io);
231 * Allocate the parent bus DMA tag appropriate for PCI.
233 if (bus_dma_tag_create(NULL, /* parent */
234 1, 0, /* alignment, boundary */
235 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
236 BUS_SPACE_MAXADDR, /* highaddr */
237 NULL, NULL, /* filter, filterarg */
238 MAXBSIZE, TWE_MAX_SGL_LENGTH, /* maxsize, nsegments */
239 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
241 &sc->twe_parent_dmat)) {
242 twe_printf(sc, "can't allocate parent DMA tag\n");
248 * Allocate and connect our interrupt.
251 if ((sc->twe_irq = bus_alloc_resource_any(sc->twe_dev, SYS_RES_IRQ,
252 &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
253 twe_printf(sc, "can't allocate interrupt\n");
257 if (bus_setup_intr(sc->twe_dev, sc->twe_irq, 0,
258 twe_pci_intr, sc, &sc->twe_intr, NULL)) {
259 twe_printf(sc, "can't set up interrupt\n");
265 * Create DMA tag for mapping command's into controller-addressable space.
267 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
268 1, 0, /* alignment, boundary */
269 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
270 BUS_SPACE_MAXADDR, /* highaddr */
271 NULL, NULL, /* filter, filterarg */
272 sizeof(TWE_Command) *
273 TWE_Q_LENGTH, 1, /* maxsize, nsegments */
274 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
276 &sc->twe_cmd_dmat)) {
277 twe_printf(sc, "can't allocate data buffer DMA tag\n");
282 * Allocate memory and make it available for DMA.
284 if (bus_dmamem_alloc(sc->twe_cmd_dmat, (void **)&sc->twe_cmd,
285 BUS_DMA_NOWAIT, &sc->twe_cmdmap)) {
286 twe_printf(sc, "can't allocate command memory\n");
289 bus_dmamap_load(sc->twe_cmd_dmat, sc->twe_cmdmap, sc->twe_cmd,
290 sizeof(TWE_Command) * TWE_Q_LENGTH,
291 twe_setup_request_dmamap, sc, 0);
292 bzero(sc->twe_cmd, sizeof(TWE_Command) * TWE_Q_LENGTH);
295 * Create DMA tag for mapping objects into controller-addressable space.
297 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
298 1, 0, /* alignment, boundary */
299 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
300 BUS_SPACE_MAXADDR, /* highaddr */
301 NULL, NULL, /* filter, filterarg */
302 MAXBSIZE, TWE_MAX_SGL_LENGTH,/* maxsize, nsegments */
303 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
304 BUS_DMA_ALLOCNOW, /* flags */
305 &sc->twe_buffer_dmat)) {
306 twe_printf(sc, "can't allocate data buffer DMA tag\n");
312 * Create DMA tag for mapping objects into controller-addressable space.
314 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
315 1, 0, /* alignment, boundary */
316 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
317 BUS_SPACE_MAXADDR, /* highaddr */
318 NULL, NULL, /* filter, filterarg */
319 MAXBSIZE, 1, /* maxsize, nsegments */
320 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
322 &sc->twe_immediate_dmat)) {
323 twe_printf(sc, "can't allocate data buffer DMA tag\n");
328 * Allocate memory for requests which cannot sleep or support continuation.
330 if (bus_dmamem_alloc(sc->twe_immediate_dmat, (void **)&sc->twe_immediate,
331 BUS_DMA_NOWAIT, &sc->twe_immediate_map)) {
332 twe_printf(sc, "can't allocate memory for immediate requests\n");
337 * Initialise the controller and driver core.
339 if ((error = twe_setup(sc))) {
345 * Print some information about the controller and configuration.
347 twe_describe_controller(sc);
350 * Create the control device.
352 sc->twe_dev_t = make_dev(&twe_ops, device_get_unit(sc->twe_dev),
353 UID_ROOT, GID_OPERATOR,
354 S_IRUSR | S_IWUSR, "twe%d",
355 device_get_unit(sc->twe_dev));
356 sc->twe_dev_t->si_drv1 = sc;
359 * Schedule ourselves to bring the controller up once interrupts are
360 * available. This isn't strictly necessary, since we disable
361 * interrupts while probing the controller, but it is more in keeping
362 * with common practice for other disk devices.
364 sc->twe_ich.ich_func = twe_intrhook;
365 sc->twe_ich.ich_arg = sc;
366 if (config_intrhook_establish(&sc->twe_ich) != 0) {
367 twe_printf(sc, "can't establish configuration hook\n");
375 /********************************************************************************
376 * Free all of the resources associated with (sc).
378 * Should not be called if the controller is active.
381 twe_free(struct twe_softc *sc)
383 struct twe_request *tr;
387 /* throw away any command buffers */
388 while ((tr = twe_dequeue_free(sc)) != NULL)
389 twe_free_request(tr);
391 if (sc->twe_cmd != NULL) {
392 bus_dmamap_unload(sc->twe_cmd_dmat, sc->twe_cmdmap);
393 bus_dmamem_free(sc->twe_cmd_dmat, sc->twe_cmd, sc->twe_cmdmap);
396 if (sc->twe_immediate != NULL) {
397 bus_dmamap_unload(sc->twe_immediate_dmat, sc->twe_immediate_map);
398 bus_dmamem_free(sc->twe_immediate_dmat, sc->twe_immediate,
399 sc->twe_immediate_map);
402 if (sc->twe_immediate_dmat)
403 bus_dma_tag_destroy(sc->twe_immediate_dmat);
405 /* destroy the data-transfer DMA tag */
406 if (sc->twe_buffer_dmat)
407 bus_dma_tag_destroy(sc->twe_buffer_dmat);
409 /* disconnect the interrupt handler */
411 bus_teardown_intr(sc->twe_dev, sc->twe_irq, sc->twe_intr);
412 if (sc->twe_irq != NULL)
413 bus_release_resource(sc->twe_dev, SYS_RES_IRQ, 0, sc->twe_irq);
415 /* destroy the parent DMA tag */
416 if (sc->twe_parent_dmat)
417 bus_dma_tag_destroy(sc->twe_parent_dmat);
419 /* release the register window mapping */
420 if (sc->twe_io != NULL)
421 bus_release_resource(sc->twe_dev, SYS_RES_IOPORT, TWE_IO_CONFIG_REG, sc->twe_io);
423 /* destroy control device */
424 if (sc->twe_dev_t != NULL)
425 destroy_dev(sc->twe_dev_t);
426 dev_ops_remove_minor(&twe_ops, device_get_unit(sc->twe_dev));
428 sysctl_ctx_free(&sc->sysctl_ctx);
431 /********************************************************************************
432 * Disconnect from the controller completely, in preparation for unload.
435 twe_detach(device_t dev)
437 struct twe_softc *sc = device_get_softc(dev);
444 if (sc->twe_state & TWE_STATE_OPEN)
448 * Shut the controller down.
450 if (twe_shutdown(dev))
461 /********************************************************************************
462 * Bring the controller down to a dormant state and detach all child devices.
464 * Note that we can assume that the bioq on the controller is empty, as we won't
465 * allow shutdown if any device is open.
468 twe_shutdown(device_t dev)
470 struct twe_softc *sc = device_get_softc(dev);
478 * Delete all our child devices.
480 for (i = 0; i < TWE_MAX_UNITS; i++) {
481 if (sc->twe_drive[i].td_disk != 0) {
482 if ((error = twe_detach_drive(sc, i)) != 0)
488 * Bring the controller down.
497 /********************************************************************************
498 * Bring the controller to a quiescent state, ready for system suspend.
501 twe_suspend(device_t dev)
503 struct twe_softc *sc = device_get_softc(dev);
508 sc->twe_state |= TWE_STATE_SUSPEND;
510 twe_disable_interrupts(sc);
516 /********************************************************************************
517 * Bring the controller back to a state ready for operation.
520 twe_resume(device_t dev)
522 struct twe_softc *sc = device_get_softc(dev);
526 sc->twe_state &= ~TWE_STATE_SUSPEND;
527 twe_enable_interrupts(sc);
532 /*******************************************************************************
533 * Take an interrupt, or be poked by other code to look for interrupt-worthy
537 twe_pci_intr(void *arg)
539 twe_intr((struct twe_softc *)arg);
542 /********************************************************************************
543 * Delayed-startup hook
546 twe_intrhook(void *arg)
548 struct twe_softc *sc = (struct twe_softc *)arg;
550 /* pull ourselves off the intrhook chain */
551 config_intrhook_disestablish(&sc->twe_ich);
553 /* call core startup routine */
557 /********************************************************************************
558 * Given a detected drive, attach it to the bio interface.
560 * This is called from twe_add_unit.
563 twe_attach_drive(struct twe_softc *sc, struct twe_drive *dr)
568 dr->td_disk = device_add_child(sc->twe_dev, NULL, -1);
569 if (dr->td_disk == NULL) {
570 twe_printf(sc, "Cannot add unit\n");
573 device_set_ivars(dr->td_disk, dr);
576 * XXX It would make sense to test the online/initialising bits, but they seem to be
579 ksprintf(buf, "Unit %d, %s, %s",
581 twe_describe_code(twe_table_unittype, dr->td_type),
582 twe_describe_code(twe_table_unitstate, dr->td_state & TWE_PARAM_UNITSTATUS_MASK));
583 device_set_desc_copy(dr->td_disk, buf);
585 if ((error = bus_generic_attach(sc->twe_dev)) != 0) {
586 twe_printf(sc, "Cannot attach unit to controller. error = %d\n", error);
592 /********************************************************************************
593 * Detach the specified unit if it exsists
595 * This is called from twe_del_unit.
598 twe_detach_drive(struct twe_softc *sc, int unit)
602 if ((error = device_delete_child(sc->twe_dev, sc->twe_drive[unit].td_disk)) != 0) {
603 twe_printf(sc, "failed to delete unit %d\n", unit);
606 bzero(&sc->twe_drive[unit], sizeof(sc->twe_drive[unit]));
610 /********************************************************************************
611 * Clear a PCI parity error.
614 twe_clear_pci_parity_error(struct twe_softc *sc)
616 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PARITY_ERROR);
617 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PARITY_ERROR, 2);
620 /********************************************************************************
624 twe_clear_pci_abort(struct twe_softc *sc)
626 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PCI_ABORT);
627 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PCI_ABORT, 2);
630 /********************************************************************************
631 ********************************************************************************
633 ********************************************************************************
634 ********************************************************************************/
637 * Disk device bus interface
639 static int twed_probe(device_t dev);
640 static int twed_attach(device_t dev);
641 static int twed_detach(device_t dev);
643 static device_method_t twed_methods[] = {
644 DEVMETHOD(device_probe, twed_probe),
645 DEVMETHOD(device_attach, twed_attach),
646 DEVMETHOD(device_detach, twed_detach),
650 static driver_t twed_driver = {
653 sizeof(struct twed_softc)
656 static devclass_t twed_devclass;
657 DRIVER_MODULE(twed, twe, twed_driver, twed_devclass, NULL, NULL);
660 * Disk device control interface.
662 static d_open_t twed_open;
663 static d_close_t twed_close;
664 static d_strategy_t twed_strategy;
665 static d_dump_t twed_dump;
667 static struct dev_ops twed_ops = {
668 { "twed", 0, D_DISK },
670 .d_close = twed_close,
672 .d_write = physwrite,
673 .d_strategy = twed_strategy,
677 /********************************************************************************
678 * Handle open from generic layer.
680 * Note that this is typically only called by the diskslice code, and not
681 * for opens on subdevices (eg. slices, partitions).
684 twed_open(struct dev_open_args *ap)
686 cdev_t dev = ap->a_head.a_dev;
687 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1;
694 /* check that the controller is up and running */
695 if (sc->twed_controller->twe_state & TWE_STATE_SHUTDOWN)
698 sc->twed_flags |= TWED_OPEN;
702 /********************************************************************************
703 * Handle last close of the disk device.
706 twed_close(struct dev_close_args *ap)
708 cdev_t dev = ap->a_head.a_dev;
709 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1;
716 sc->twed_flags &= ~TWED_OPEN;
720 /********************************************************************************
721 * Handle an I/O request.
724 twed_strategy(struct dev_strategy_args *ap)
726 cdev_t dev = ap->a_head.a_dev;
727 struct bio *bio = ap->a_bio;
728 struct twed_softc *sc = dev->si_drv1;
729 struct buf *bp = bio->bio_buf;
731 bio->bio_driver_info = sc;
738 if (sc == NULL || sc->twed_drive->td_disk == NULL) {
739 bp->b_error = EINVAL;
740 bp->b_flags |= B_ERROR;
741 kprintf("twe: bio for invalid disk!\n");
747 /* perform accounting */
748 devstat_start_transaction(&sc->twed_stats);
750 /* queue the bio on the controller */
751 twe_enqueue_bio(sc->twed_controller, bio);
753 /* poke the controller to start I/O */
754 twe_startio(sc->twed_controller);
758 /********************************************************************************
759 * System crashdump support
762 twed_dump(struct dev_dump_args *ap)
764 cdev_t dev = ap->a_head.a_dev;
765 size_t length = ap->a_length;
766 off_t offset = ap->a_offset;
767 void *virtual = ap->a_virtual;
768 struct twed_softc *twed_sc;
769 struct twe_softc *twe_sc;
772 twed_sc = dev->si_drv1;
775 twe_sc = (struct twe_softc *)twed_sc->twed_controller;
778 if ((error = twe_dump_blocks(twe_sc, twed_sc->twed_drive->td_twe_unit, offset / TWE_BLOCK_SIZE, virtual, length / TWE_BLOCK_SIZE)) != 0)
784 /********************************************************************************
785 * Handle completion of an I/O request.
788 twed_intr(struct bio *bio)
790 struct buf *bp = bio->bio_buf;
791 struct twed_softc *sc = bio->bio_driver_info;
795 /* if no error, transfer completed */
796 if ((bp->b_flags & B_ERROR) == 0)
798 devstat_end_transaction_buf(&sc->twed_stats, bp);
803 /********************************************************************************
804 * Default probe stub.
807 twed_probe(device_t dev)
812 /********************************************************************************
813 * Attach a unit to the controller.
816 twed_attach(device_t dev)
818 struct twed_softc *sc;
819 struct disk_info info;
825 /* initialise our softc */
826 sc = device_get_softc(dev);
827 parent = device_get_parent(dev);
828 sc->twed_controller = (struct twe_softc *)device_get_softc(parent);
829 sc->twed_drive = device_get_ivars(dev);
832 /* report the drive */
833 twed_printf(sc, "%uMB (%u sectors)\n",
834 sc->twed_drive->td_size / ((1024 * 1024) / TWE_BLOCK_SIZE),
835 sc->twed_drive->td_size);
837 /* attach a generic disk device to ourselves */
839 sc->twed_drive->td_sys_unit = device_get_unit(dev);
841 devstat_add_entry(&sc->twed_stats, "twed", sc->twed_drive->td_sys_unit,
843 DEVSTAT_NO_ORDERED_TAGS,
844 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
845 DEVSTAT_PRIORITY_ARRAY);
847 dsk = disk_create(sc->twed_drive->td_sys_unit, &sc->twed_disk, &twed_ops);
849 sc->twed_dev_t = dsk;
851 /* set the maximum I/O size to the theoretical maximum allowed by the S/G list size */
852 dsk->si_iosize_max = (TWE_MAX_SGL_LENGTH - 1) * PAGE_SIZE;
855 * Set disk info, as it appears that all needed data is available already.
856 * Setting the disk info will also cause the probing to start.
858 bzero(&info, sizeof(info));
859 info.d_media_blksize = TWE_BLOCK_SIZE; /* mandatory */
860 info.d_media_blocks = sc->twed_drive->td_size;
862 info.d_type = DTYPE_ESDI; /* optional */
863 info.d_secpertrack = sc->twed_drive->td_sectors;
864 info.d_nheads = sc->twed_drive->td_heads;
865 info.d_ncylinders = sc->twed_drive->td_cylinders;
866 info.d_secpercyl = sc->twed_drive->td_sectors * sc->twed_drive->td_heads;
868 disk_setdiskinfo(&sc->twed_disk, &info);
873 /********************************************************************************
874 * Disconnect ourselves from the system.
877 twed_detach(device_t dev)
879 struct twed_softc *sc = (struct twed_softc *)device_get_softc(dev);
883 if (sc->twed_flags & TWED_OPEN)
886 devstat_remove_entry(&sc->twed_stats);
887 disk_destroy(&sc->twed_disk);
892 /********************************************************************************
893 ********************************************************************************
895 ********************************************************************************
896 ********************************************************************************/
898 /********************************************************************************
899 * Allocate a command buffer
901 MALLOC_DEFINE(TWE_MALLOC_CLASS, "twe_commands", "twe commands");
904 twe_allocate_request(struct twe_softc *sc, int tag)
906 struct twe_request *tr;
910 * TWE requires requests to be 512-byte aligned. Depend on malloc()
911 * guarenteeing alignment for power-of-2 requests. Note that the old
912 * (FreeBSD-4.x) malloc code aligned all requests, but the new slab
913 * allocator only guarentees same-size alignment for power-of-2 requests.
915 aligned_size = (sizeof(struct twe_request) + TWE_ALIGNMASK) &
917 tr = kmalloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT|M_ZERO);
920 if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_dmamap)) {
921 twe_free_request(tr);
922 twe_printf(sc, "unable to allocate dmamap for tag %d\n", tag);
928 /********************************************************************************
929 * Permanently discard a command buffer.
932 twe_free_request(struct twe_request *tr)
934 struct twe_softc *sc = tr->tr_sc;
938 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_dmamap);
939 kfree(tr, TWE_MALLOC_CLASS);
942 /********************************************************************************
943 * Map/unmap (tr)'s command and data in the controller's addressable space.
945 * These routines ensure that the data which the controller is going to try to
946 * access is actually visible to the controller, in a machine-independant
947 * fashion. Due to a hardware limitation, I/O buffers must be 512-byte aligned
948 * and we take care of that here as well.
951 twe_fillin_sgl(TWE_SG_Entry *sgl, bus_dma_segment_t *segs, int nsegments, int max_sgl)
955 for (i = 0; i < nsegments; i++) {
956 sgl[i].address = segs[i].ds_addr;
957 sgl[i].length = segs[i].ds_len;
959 for (; i < max_sgl; i++) { /* XXX necessary? */
966 twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
968 struct twe_request *tr = (struct twe_request *)arg;
969 struct twe_softc *sc = tr->tr_sc;
970 TWE_Command *cmd = TWE_FIND_COMMAND(tr);
974 if (tr->tr_flags & TWE_CMD_MAPPED)
975 panic("already mapped command");
977 tr->tr_flags |= TWE_CMD_MAPPED;
979 if (tr->tr_flags & TWE_CMD_IN_PROGRESS)
980 sc->twe_state &= ~TWE_STATE_FRZN;
981 /* save base of first segment in command (applicable if there only one segment) */
982 tr->tr_dataphys = segs[0].ds_addr;
984 /* correct command size for s/g list size */
985 cmd->generic.size += 2 * nsegments;
988 * Due to the fact that parameter and I/O commands have the scatter/gather list in
989 * different places, we need to determine which sort of command this actually is
990 * before we can populate it correctly.
992 switch(cmd->generic.opcode) {
993 case TWE_OP_GET_PARAM:
994 case TWE_OP_SET_PARAM:
995 cmd->generic.sgl_offset = 2;
996 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1000 cmd->generic.sgl_offset = 3;
1001 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1003 case TWE_OP_ATA_PASSTHROUGH:
1004 cmd->generic.sgl_offset = 5;
1005 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1009 * Fall back to what the linux driver does.
1010 * Do this because the API may send an opcode
1011 * the driver knows nothing about and this will
1012 * at least stop PCIABRT's from hosing us.
1014 switch (cmd->generic.sgl_offset) {
1016 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1019 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1022 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1027 if (tr->tr_flags & TWE_CMD_DATAIN) {
1028 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1029 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1030 BUS_DMASYNC_PREREAD);
1032 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1033 BUS_DMASYNC_PREREAD);
1037 if (tr->tr_flags & TWE_CMD_DATAOUT) {
1039 * if we're using an alignment buffer, and we're writing data
1040 * copy the real data out
1042 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1043 bcopy(tr->tr_realdata, tr->tr_data, tr->tr_length);
1045 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1046 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1047 BUS_DMASYNC_PREWRITE);
1049 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1050 BUS_DMASYNC_PREWRITE);
1054 if (twe_start(tr) == EBUSY) {
1055 tr->tr_sc->twe_state |= TWE_STATE_CTLR_BUSY;
1056 twe_requeue_ready(tr);
1061 twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1063 struct twe_softc *sc = (struct twe_softc *)arg;
1067 /* command can't cross a page boundary */
1068 sc->twe_cmdphys = segs[0].ds_addr;
1072 twe_map_request(struct twe_request *tr)
1074 struct twe_softc *sc = tr->tr_sc;
1079 if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN)) {
1080 twe_requeue_ready(tr);
1084 bus_dmamap_sync(sc->twe_cmd_dmat, sc->twe_cmdmap, BUS_DMASYNC_PREWRITE);
1087 * If the command involves data, map that too.
1089 if (tr->tr_data != NULL && ((tr->tr_flags & TWE_CMD_MAPPED) == 0)) {
1092 * Data must be 512-byte aligned; allocate a fixup buffer if it's not.
1094 * DragonFly's malloc only guarentees alignment for requests which
1095 * are power-of-2 sized.
1097 if (((vm_offset_t)tr->tr_data % TWE_ALIGNMENT) != 0) {
1100 tr->tr_realdata = tr->tr_data; /* save pointer to 'real' data */
1101 aligned_size = TWE_ALIGNMENT;
1102 while (aligned_size < tr->tr_length)
1104 tr->tr_flags |= TWE_CMD_ALIGNBUF;
1105 tr->tr_data = kmalloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT);
1106 if (tr->tr_data == NULL) {
1107 twe_printf(sc, "%s: malloc failed\n", __func__);
1108 tr->tr_data = tr->tr_realdata; /* restore original data pointer */
1114 * Map the data buffer into bus space and build the s/g list.
1116 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1117 error = bus_dmamap_load(sc->twe_immediate_dmat, sc->twe_immediate_map, sc->twe_immediate,
1118 tr->tr_length, twe_setup_data_dmamap, tr, BUS_DMA_NOWAIT);
1120 error = bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_dmamap, tr->tr_data, tr->tr_length,
1121 twe_setup_data_dmamap, tr, 0);
1123 if (error == EINPROGRESS) {
1124 tr->tr_flags |= TWE_CMD_IN_PROGRESS;
1125 sc->twe_state |= TWE_STATE_FRZN;
1129 if ((error = twe_start(tr)) == EBUSY) {
1130 sc->twe_state |= TWE_STATE_CTLR_BUSY;
1131 twe_requeue_ready(tr);
1138 twe_unmap_request(struct twe_request *tr)
1140 struct twe_softc *sc = tr->tr_sc;
1144 bus_dmamap_sync(sc->twe_cmd_dmat, sc->twe_cmdmap, BUS_DMASYNC_POSTWRITE);
1147 * If the command involved data, unmap that too.
1149 if (tr->tr_data != NULL) {
1150 if (tr->tr_flags & TWE_CMD_DATAIN) {
1151 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1152 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1153 BUS_DMASYNC_POSTREAD);
1155 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1156 BUS_DMASYNC_POSTREAD);
1159 /* if we're using an alignment buffer, and we're reading data, copy the real data in */
1160 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1161 bcopy(tr->tr_data, tr->tr_realdata, tr->tr_length);
1163 if (tr->tr_flags & TWE_CMD_DATAOUT) {
1164 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1165 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1166 BUS_DMASYNC_POSTWRITE);
1168 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1169 BUS_DMASYNC_POSTWRITE);
1173 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1174 bus_dmamap_unload(sc->twe_immediate_dmat, sc->twe_immediate_map);
1176 bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_dmamap);
1180 /* free alignment buffer if it was used */
1181 if (tr->tr_flags & TWE_CMD_ALIGNBUF) {
1182 kfree(tr->tr_data, TWE_MALLOC_CLASS);
1183 tr->tr_data = tr->tr_realdata; /* restore 'real' data pointer */
1188 void twe_report(void);
1189 /********************************************************************************
1190 * Print current controller status, call from DDB.
1195 struct twe_softc *sc;
1199 for (i = 0; (sc = devclass_get_softc(twe_devclass, i)) != NULL; i++)
1200 twe_print_controller(sc);
1201 kprintf("twed: total bio count in %u out %u\n", twed_bio_in, twed_bio_out);