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.2.2.9 2004/06/11 18:57:31 vkashyap Exp $
30 * $DragonFly: src/sys/dev/raid/twe/twe_freebsd.c,v 1.19 2006/02/17 19:18:06 dillon Exp $
34 * FreeBSD-specific code.
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>
43 static devclass_t twe_devclass;
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++
55 /********************************************************************************
56 ********************************************************************************
57 Control device interface
58 ********************************************************************************
59 ********************************************************************************/
61 static d_open_t twe_open;
62 static d_close_t twe_close;
63 static d_ioctl_t twe_ioctl_wrapper;
65 static struct cdevsw twe_cdevsw = {
67 TWE_CDEV_MAJOR, /* major number */
69 NULL, /* device port */
83 /********************************************************************************
84 * Accept an open operation on the control device.
87 twe_open(dev_t dev, int flags, int fmt, d_thread_t *td)
89 int unit = minor(dev);
90 struct twe_softc *sc = devclass_get_softc(twe_devclass, unit);
92 sc->twe_state |= TWE_STATE_OPEN;
96 /********************************************************************************
97 * Accept the last close on the control device.
100 twe_close(dev_t dev, int flags, int fmt, d_thread_t *td)
102 int unit = minor(dev);
103 struct twe_softc *sc = devclass_get_softc(twe_devclass, unit);
105 sc->twe_state &= ~TWE_STATE_OPEN;
109 /********************************************************************************
110 * Handle controller-specific control operations.
113 twe_ioctl_wrapper(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *td)
115 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1;
117 return(twe_ioctl(sc, cmd, addr));
120 /********************************************************************************
121 ********************************************************************************
123 ********************************************************************************
124 ********************************************************************************/
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);
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),
150 DEVMETHOD(bus_print_child, bus_generic_print_child),
151 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
155 static driver_t twe_pci_driver = {
158 sizeof(struct twe_softc)
162 DRIVER_MODULE(Xtwe, pci, twe_pci_driver, twe_devclass, 0, 0);
164 DRIVER_MODULE(twe, pci, twe_pci_driver, twe_devclass, 0, 0);
167 /********************************************************************************
168 * Match a 3ware Escalade ATA RAID controller.
171 twe_probe(device_t dev)
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);
189 /********************************************************************************
190 * Allocate resources, initialise the controller.
193 twe_attach(device_t dev)
195 struct twe_softc *sc;
202 * Initialise the softc structure.
204 sc = device_get_softc(dev);
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");
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");
220 * Make sure we are going to be able to talk to this board.
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");
228 * Force the busmaster enable bit on, in case the BIOS forgot.
230 command |= PCIM_CMD_BUSMASTEREN;
231 pci_write_config(dev, PCIR_COMMAND, command, 2);
234 * Allocate the PCI register window.
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");
242 sc->twe_btag = rman_get_bustag(sc->twe_io);
243 sc->twe_bhandle = rman_get_bushandle(sc->twe_io);
246 * Allocate the parent bus DMA tag appropriate for PCI.
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");
263 * Allocate and connect our interrupt.
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");
271 if (bus_setup_intr(sc->twe_dev, sc->twe_irq, 0,
272 twe_pci_intr, sc, &sc->twe_intr, NULL)) {
273 twe_printf(sc, "can't set up interrupt\n");
279 * Create DMA tag for mapping objects into controller-addressable space.
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 */
289 &sc->twe_buffer_dmat)) {
290 twe_printf(sc, "can't allocate data buffer DMA tag\n");
296 * Initialise the controller and driver core.
298 if ((error = twe_setup(sc))) {
304 * Print some information about the controller and configuration.
306 twe_describe_controller(sc);
309 * Create the control device.
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;
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
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");
333 /********************************************************************************
334 * Free all of the resources associated with (sc).
336 * Should not be called if the controller is active.
339 twe_free(struct twe_softc *sc)
341 struct twe_request *tr;
345 /* throw away any command buffers */
346 while ((tr = twe_dequeue_free(sc)) != NULL)
347 twe_free_request(tr);
349 /* destroy the data-transfer DMA tag */
350 if (sc->twe_buffer_dmat)
351 bus_dma_tag_destroy(sc->twe_buffer_dmat);
353 /* disconnect the interrupt handler */
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);
359 /* destroy the parent DMA tag */
360 if (sc->twe_parent_dmat)
361 bus_dma_tag_destroy(sc->twe_parent_dmat);
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);
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);
372 sysctl_ctx_free(&sc->sysctl_ctx);
375 /********************************************************************************
376 * Disconnect from the controller completely, in preparation for unload.
379 twe_detach(device_t dev)
381 struct twe_softc *sc = device_get_softc(dev);
388 if (sc->twe_state & TWE_STATE_OPEN)
392 * Shut the controller down.
394 if ((error = twe_shutdown(dev)))
405 /********************************************************************************
406 * Bring the controller down to a dormant state and detach all child devices.
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.
412 twe_shutdown(device_t dev)
414 struct twe_softc *sc = device_get_softc(dev);
422 * Delete all our child devices.
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)
431 * Bring the controller down.
440 /********************************************************************************
441 * Bring the controller to a quiescent state, ready for system suspend.
444 twe_suspend(device_t dev)
446 struct twe_softc *sc = device_get_softc(dev);
451 sc->twe_state |= TWE_STATE_SUSPEND;
453 twe_disable_interrupts(sc);
459 /********************************************************************************
460 * Bring the controller back to a state ready for operation.
463 twe_resume(device_t dev)
465 struct twe_softc *sc = device_get_softc(dev);
469 sc->twe_state &= ~TWE_STATE_SUSPEND;
470 twe_enable_interrupts(sc);
475 /*******************************************************************************
476 * Take an interrupt, or be poked by other code to look for interrupt-worthy
480 twe_pci_intr(void *arg)
482 twe_intr((struct twe_softc *)arg);
485 /********************************************************************************
486 * Delayed-startup hook
489 twe_intrhook(void *arg)
491 struct twe_softc *sc = (struct twe_softc *)arg;
493 /* pull ourselves off the intrhook chain */
494 config_intrhook_disestablish(&sc->twe_ich);
496 /* call core startup routine */
500 /********************************************************************************
501 * Given a detected drive, attach it to the bio interface.
503 * This is called from twe_add_unit.
506 twe_attach_drive(struct twe_softc *sc, struct twe_drive *dr)
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");
516 device_set_ivars(dr->td_disk, dr);
519 * XXX It would make sense to test the online/initialising bits, but they seem to be
522 sprintf(buf, "Unit %d, %s, %s",
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);
528 if ((error = bus_generic_attach(sc->twe_dev)) != 0) {
529 twe_printf(sc, "Cannot attach unit to controller. error = %d\n", error);
535 /********************************************************************************
536 * Detach the specified unit if it exsists
538 * This is called from twe_del_unit.
541 twe_detach_drive(struct twe_softc *sc, int unit)
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);
549 bzero(&sc->twe_drive[unit], sizeof(sc->twe_drive[unit]));
553 /********************************************************************************
554 * Clear a PCI parity error.
557 twe_clear_pci_parity_error(struct twe_softc *sc)
559 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PARITY_ERROR);
560 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PARITY_ERROR, 2);
563 /********************************************************************************
567 twe_clear_pci_abort(struct twe_softc *sc)
569 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PCI_ABORT);
570 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PCI_ABORT, 2);
573 /********************************************************************************
574 ********************************************************************************
576 ********************************************************************************
577 ********************************************************************************/
580 * Disk device bus interface
582 static int twed_probe(device_t dev);
583 static int twed_attach(device_t dev);
584 static int twed_detach(device_t dev);
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),
593 static driver_t twed_driver = {
596 sizeof(struct twed_softc)
599 static devclass_t twed_devclass;
601 DRIVER_MODULE(Xtwed, Xtwe, twed_driver, twed_devclass, 0, 0);
603 DRIVER_MODULE(twed, twe, twed_driver, twed_devclass, 0, 0);
607 * Disk device control interface.
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;
614 static struct cdevsw twed_cdevsw = {
633 static struct cdevsw tweddisk_cdevsw;
636 static int disks_registered = 0;
639 /********************************************************************************
640 * Handle open from generic layer.
642 * Note that this is typically only called by the diskslice code, and not
643 * for opens on subdevices (eg. slices, partitions).
646 twed_open(dev_t dev, int flags, int fmt, d_thread_t *td)
648 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1;
649 struct disklabel *label;
656 /* check that the controller is up and running */
657 if (sc->twed_controller->twe_state & TWE_STATE_SHUTDOWN)
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;
671 sc->twed_flags |= TWED_OPEN;
675 /********************************************************************************
676 * Handle last close of the disk device.
679 twed_close(dev_t dev, int flags, int fmt, d_thread_t *td)
681 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1;
688 sc->twed_flags &= ~TWED_OPEN;
692 /********************************************************************************
693 * Handle an I/O request.
696 twed_strategy(dev_t dev, struct bio *bio)
698 struct twed_softc *sc = dev->si_drv1;
699 struct buf *bp = bio->bio_buf;
701 bio->bio_driver_info = sc;
708 if ((sc == NULL) || (!sc->twed_drive->td_disk)) {
709 bp->b_error = EINVAL;
710 bp->b_flags |= B_ERROR;
711 printf("twe: bio for invalid disk!\n");
717 /* perform accounting */
718 devstat_start_transaction(&sc->twed_stats);
720 /* queue the bio on the controller */
721 twe_enqueue_bio(sc->twed_controller, bio);
723 /* poke the controller to start I/O */
724 twe_startio(sc->twed_controller);
728 /********************************************************************************
729 * System crashdump support
732 twed_dump(dev_t dev, u_int count, u_int blkno, u_int secsize)
734 struct twed_softc *twed_sc = (struct twed_softc *)dev->si_drv1;
735 struct twe_softc *twe_sc = (struct twe_softc *)twed_sc->twed_controller;
737 u_int count, blkno, secsize;
741 int dumppages = MAXDUMPPGS;
746 if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
750 if (!twed_sc || !twe_sc)
753 blkcnt = howmany(PAGE_SIZE, secsize);
758 if ((count / blkcnt) < dumppages)
759 dumppages = count / blkcnt;
761 for (i = 0; i < dumppages; ++i) {
762 vm_paddr_t a = addr + (i * PAGE_SIZE);
763 if (is_physical_memory(a))
764 va = pmap_kenter_temporary(trunc_page(a), i);
766 va = pmap_kenter_temporary(trunc_page(0), i);
769 if ((error = twe_dump_blocks(twe_sc, twed_sc->twed_drive->td_twe_unit, blkno, va,
770 (PAGE_SIZE * dumppages) / TWE_BLOCK_SIZE)) != 0)
774 if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
777 blkno += blkcnt * dumppages;
778 count -= blkcnt * dumppages;
779 addr += PAGE_SIZE * dumppages;
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;
794 /* if no error, transfer completed */
795 if (bp->b_flags & B_ERROR)
797 devstat_end_transaction_buf(&sc->twed_stats, bp);
802 /********************************************************************************
803 * Default probe stub.
806 twed_probe(device_t dev)
811 /********************************************************************************
812 * Attach a unit to the controller.
815 twed_attach(device_t dev)
817 struct twed_softc *sc;
823 /* initialise our softc */
824 sc = device_get_softc(dev);
825 parent = device_get_parent(dev);
826 sc->twed_controller = (struct twe_softc *)device_get_softc(parent);
827 sc->twed_drive = device_get_ivars(dev);
828 sc->twed_drive->td_sys_unit = device_get_unit(dev);
831 /* report the drive */
832 twed_printf(sc, "%uMB (%u sectors)\n",
833 sc->twed_drive->td_size / ((1024 * 1024) / TWE_BLOCK_SIZE),
834 sc->twed_drive->td_size);
836 devstat_add_entry(&sc->twed_stats, "twed", sc->twed_drive->td_sys_unit,
838 DEVSTAT_NO_ORDERED_TAGS,
839 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
840 DEVSTAT_PRIORITY_ARRAY);
842 /* attach a generic disk device to ourselves */
843 dsk = disk_create(sc->twed_drive->td_sys_unit, &sc->twed_disk,
846 /* dsk->si_drv2 = sc->twed_drive;*/
847 sc->twed_dev_t = dsk;
852 /* set the maximum I/O size to the theoretical maximum allowed by the S/G list size */
853 dsk->si_iosize_max = (TWE_MAX_SGL_LENGTH - 1) * PAGE_SIZE;
858 /********************************************************************************
859 * Disconnect ourselves from the system.
862 twed_detach(device_t dev)
864 struct twed_softc *sc = (struct twed_softc *)device_get_softc(dev);
868 if (sc->twed_flags & TWED_OPEN)
871 devstat_remove_entry(&sc->twed_stats);
872 disk_destroy(&sc->twed_disk);
874 printf("Disks registered: %d\n", disks_registered);
876 if (--disks_registered == 0)
877 cdevsw_remove(&tweddisk_cdevsw);
884 /********************************************************************************
885 ********************************************************************************
887 ********************************************************************************
888 ********************************************************************************/
890 MALLOC_DEFINE(TWE_MALLOC_CLASS, "twe commands", "twe commands");
891 /********************************************************************************
892 * Allocate a command buffer
895 twe_allocate_request(struct twe_softc *sc)
897 struct twe_request *tr;
901 * TWE requires requests to be 512-byte aligned. Depend on malloc()
902 * guarenteeing alignment for power-of-2 requests. Note that the old
903 * (FreeBSD-4.x) malloc code aligned all requests, but the new slab
904 * allocator only guarentees same-size alignment for power-of-2 requests.
906 aligned_size = (sizeof(struct twe_request) + TWE_ALIGNMASK) &
908 tr = malloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT|M_ZERO);
910 if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_cmdmap)) {
911 twe_free_request(tr);
914 bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_cmdmap, &tr->tr_command,
915 sizeof(tr->tr_command), twe_setup_request_dmamap, tr, 0);
916 if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_dmamap)) {
917 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_cmdmap);
918 twe_free_request(tr);
924 /********************************************************************************
925 * Permanently discard a command buffer.
928 twe_free_request(struct twe_request *tr)
930 struct twe_softc *sc = tr->tr_sc;
934 bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_cmdmap);
935 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_cmdmap);
936 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_dmamap);
937 free(tr, TWE_MALLOC_CLASS);
940 /********************************************************************************
941 * Map/unmap (tr)'s command and data in the controller's addressable space.
943 * These routines ensure that the data which the controller is going to try to
944 * access is actually visible to the controller, in a machine-independant
945 * fashion. Due to a hardware limitation, I/O buffers must be 512-byte aligned
946 * and we take care of that here as well.
949 twe_fillin_sgl(TWE_SG_Entry *sgl, bus_dma_segment_t *segs, int nsegments, int max_sgl)
953 for (i = 0; i < nsegments; i++) {
954 sgl[i].address = segs[i].ds_addr;
955 sgl[i].length = segs[i].ds_len;
957 for (; i < max_sgl; i++) { /* XXX necessary? */
964 twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
966 struct twe_request *tr = (struct twe_request *)arg;
967 TWE_Command *cmd = &tr->tr_command;
971 if (tr->tr_flags & TWE_CMD_MAPPED)
972 panic("already mapped command");
974 tr->tr_flags |= TWE_CMD_MAPPED;
976 if (tr->tr_flags & TWE_CMD_IN_PROGRESS)
977 tr->tr_sc->twe_state &= ~TWE_STATE_FRZN;
978 /* save base of first segment in command (applicable if there only one segment) */
979 tr->tr_dataphys = segs[0].ds_addr;
981 /* correct command size for s/g list size */
982 tr->tr_command.generic.size += 2 * nsegments;
985 * Due to the fact that parameter and I/O commands have the scatter/gather list in
986 * different places, we need to determine which sort of command this actually is
987 * before we can populate it correctly.
989 switch(cmd->generic.opcode) {
990 case TWE_OP_GET_PARAM:
991 case TWE_OP_SET_PARAM:
992 cmd->generic.sgl_offset = 2;
993 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
997 cmd->generic.sgl_offset = 3;
998 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1000 case TWE_OP_ATA_PASSTHROUGH:
1001 cmd->generic.sgl_offset = 5;
1002 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1006 * Fall back to what the linux driver does.
1007 * Do this because the API may send an opcode
1008 * the driver knows nothing about and this will
1009 * at least stop PCIABRT's from hosing us.
1011 switch (cmd->generic.sgl_offset) {
1013 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1016 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1019 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1023 if (tr->tr_flags & TWE_CMD_DATAIN)
1024 bus_dmamap_sync(tr->tr_sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_PREREAD);
1025 if (tr->tr_flags & TWE_CMD_DATAOUT) {
1026 /* if we're using an alignment buffer, and we're writing data, copy the real data out */
1027 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1028 bcopy(tr->tr_realdata, tr->tr_data, tr->tr_length);
1029 bus_dmamap_sync(tr->tr_sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_PREWRITE);
1031 if (twe_start(tr) == EBUSY) {
1032 tr->tr_sc->twe_state |= TWE_STATE_CTLR_BUSY;
1033 twe_requeue_ready(tr);
1038 twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1040 struct twe_request *tr = (struct twe_request *)arg;
1044 /* command can't cross a page boundary */
1045 tr->tr_cmdphys = segs[0].ds_addr;
1049 twe_map_request(struct twe_request *tr)
1051 struct twe_softc *sc = tr->tr_sc;
1056 if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN)) {
1057 twe_requeue_ready(tr);
1062 * Map the command into bus space.
1064 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_cmdmap, BUS_DMASYNC_PREWRITE);
1067 * If the command involves data, map that too.
1069 if ((tr->tr_data != NULL) && ((tr->tr_flags & TWE_CMD_MAPPED) == 0)) {
1072 * Data must be 512-byte aligned; allocate a fixup buffer if it's not.
1074 * DragonFly's malloc only guarentees alignment for requests which
1075 * are power-of-2 sized.
1077 if (((vm_offset_t)tr->tr_data % TWE_ALIGNMENT) != 0) {
1080 tr->tr_realdata = tr->tr_data; /* save pointer to 'real' data */
1081 aligned_size = TWE_ALIGNMENT;
1082 while (aligned_size < tr->tr_length)
1084 tr->tr_flags |= TWE_CMD_ALIGNBUF;
1085 tr->tr_data = malloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT);
1086 if (tr->tr_data == NULL) {
1087 twe_printf(sc, "%s: malloc failed\n", __func__);
1088 tr->tr_data = tr->tr_realdata; /* restore original data pointer */
1094 * Map the data buffer into bus space and build the s/g list.
1096 if ((error = bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_dmamap, tr->tr_data,
1097 tr->tr_length, twe_setup_data_dmamap, tr, BUS_DMA_NOWAIT)
1099 tr->tr_flags |= TWE_CMD_IN_PROGRESS;
1100 sc->twe_state |= TWE_STATE_FRZN;
1104 if ((error = twe_start(tr)) == EBUSY) {
1105 sc->twe_state |= TWE_STATE_CTLR_BUSY;
1106 twe_requeue_ready(tr);
1114 twe_unmap_request(struct twe_request *tr)
1116 struct twe_softc *sc = tr->tr_sc;
1120 * Unmap the command from bus space.
1122 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_cmdmap, BUS_DMASYNC_POSTWRITE);
1125 * If the command involved data, unmap that too.
1127 if (tr->tr_data != NULL) {
1129 if (tr->tr_flags & TWE_CMD_DATAIN) {
1130 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_POSTREAD);
1131 /* if we're using an alignment buffer, and we're reading data, copy the real data in */
1132 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1133 bcopy(tr->tr_data, tr->tr_realdata, tr->tr_length);
1135 if (tr->tr_flags & TWE_CMD_DATAOUT)
1136 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_POSTWRITE);
1138 bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_dmamap);
1141 /* free alignment buffer if it was used */
1142 if (tr->tr_flags & TWE_CMD_ALIGNBUF) {
1143 free(tr->tr_data, TWE_MALLOC_CLASS);
1144 tr->tr_data = tr->tr_realdata; /* restore 'real' data pointer */
1149 void twe_report(void);
1150 /********************************************************************************
1151 * Print current controller status, call from DDB.
1156 struct twe_softc *sc;
1160 for (i = 0; (sc = devclass_get_softc(twe_devclass, i)) != NULL; i++)
1161 twe_print_controller(sc);
1162 printf("twed: total bio count in %u out %u\n", twed_bio_in, twed_bio_out);