2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/dev/twe/twe_freebsd.c,v 1.2.2.5 2002/03/07 09:57:02 msmith Exp $
28 * $DragonFly: src/sys/dev/raid/twe/twe_freebsd.c,v 1.14 2005/06/10 17:10:26 swildner Exp $
32 * FreeBSD-specific code.
35 #include <sys/param.h>
37 #include <machine/bus.h>
38 #include <machine/clock.h>
39 #include <machine/md_var.h>
42 #include "twe_compat.h"
46 #include "twe_tables.h"
48 #include <sys/devicestat.h>
50 static devclass_t twe_devclass;
53 static u_int32_t twed_bio_in;
54 #define TWED_BIO_IN twed_bio_in++
55 static u_int32_t twed_bio_out;
56 #define TWED_BIO_OUT twed_bio_out++
62 /********************************************************************************
63 ********************************************************************************
64 Control device interface
65 ********************************************************************************
66 ********************************************************************************/
68 static d_open_t twe_open;
69 static d_close_t twe_close;
70 static d_ioctl_t twe_ioctl_wrapper;
72 #define TWE_CDEV_MAJOR 146
74 static struct cdevsw twe_cdevsw = {
76 /* cmaj */ TWE_CDEV_MAJOR,
93 /********************************************************************************
94 * Accept an open operation on the control device.
97 twe_open(dev_t dev, int flags, int fmt, d_thread_t *td)
99 int unit = minor(dev);
100 struct twe_softc *sc = devclass_get_softc(twe_devclass, unit);
102 sc->twe_state |= TWE_STATE_OPEN;
106 /********************************************************************************
107 * Accept the last close on the control device.
110 twe_close(dev_t dev, int flags, int fmt, d_thread_t *td)
112 int unit = minor(dev);
113 struct twe_softc *sc = devclass_get_softc(twe_devclass, unit);
115 sc->twe_state &= ~TWE_STATE_OPEN;
119 /********************************************************************************
120 * Handle controller-specific control operations.
123 twe_ioctl_wrapper(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *td)
125 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1;
127 return(twe_ioctl(sc, cmd, addr));
130 /********************************************************************************
131 ********************************************************************************
133 ********************************************************************************
134 ********************************************************************************/
136 static int twe_probe(device_t dev);
137 static int twe_attach(device_t dev);
138 static void twe_free(struct twe_softc *sc);
139 static int twe_detach(device_t dev);
140 static void twe_shutdown(device_t dev);
141 static int twe_suspend(device_t dev);
142 static int twe_resume(device_t dev);
143 static void twe_pci_intr(void *arg);
144 static void twe_intrhook(void *arg);
146 static device_method_t twe_methods[] = {
147 /* Device interface */
148 DEVMETHOD(device_probe, twe_probe),
149 DEVMETHOD(device_attach, twe_attach),
150 DEVMETHOD(device_detach, twe_detach),
151 DEVMETHOD(device_shutdown, twe_shutdown),
152 DEVMETHOD(device_suspend, twe_suspend),
153 DEVMETHOD(device_resume, twe_resume),
155 DEVMETHOD(bus_print_child, bus_generic_print_child),
156 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
160 static driver_t twe_pci_driver = {
163 sizeof(struct twe_softc)
167 DRIVER_MODULE(Xtwe, pci, twe_pci_driver, twe_devclass, 0, 0);
169 DRIVER_MODULE(twe, pci, twe_pci_driver, twe_devclass, 0, 0);
172 /********************************************************************************
173 * Match a 3ware Escalade ATA RAID controller.
176 twe_probe(device_t dev)
181 if ((pci_get_vendor(dev) == TWE_VENDOR_ID) &&
182 ((pci_get_device(dev) == TWE_DEVICE_ID) ||
183 (pci_get_device(dev) == TWE_DEVICE_ID_ASIC))) {
184 device_set_desc(dev, TWE_DEVICE_NAME);
194 /********************************************************************************
195 * Allocate resources, initialise the controller.
198 twe_attach(device_t dev)
200 struct twe_softc *sc;
208 * Initialise the softc structure.
210 sc = device_get_softc(dev);
213 sysctl_ctx_init(&sc->sysctl_ctx);
214 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
215 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
216 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
217 if (sc->sysctl_tree == NULL) {
218 twe_printf(sc, "cannot add sysctl tree node\n");
221 SYSCTL_ADD_STRING(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
222 OID_AUTO, "driver_version", CTLFLAG_RD, "$Revision$", 0,
223 "TWE driver version");
226 * Make sure we are going to be able to talk to this board.
228 command = pci_read_config(dev, PCIR_COMMAND, 2);
229 if ((command & PCIM_CMD_PORTEN) == 0) {
230 twe_printf(sc, "register window not available\n");
234 * Force the busmaster enable bit on, in case the BIOS forgot.
236 command |= PCIM_CMD_BUSMASTEREN;
237 pci_write_config(dev, PCIR_COMMAND, command, 2);
240 * Allocate the PCI register window.
242 rid = TWE_IO_CONFIG_REG;
243 if ((sc->twe_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) {
244 twe_printf(sc, "can't allocate register window\n");
248 sc->twe_btag = rman_get_bustag(sc->twe_io);
249 sc->twe_bhandle = rman_get_bushandle(sc->twe_io);
252 * Allocate the parent bus DMA tag appropriate for PCI.
254 if (bus_dma_tag_create(NULL, /* parent */
255 1, 0, /* alignment, boundary */
256 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
257 BUS_SPACE_MAXADDR, /* highaddr */
258 NULL, NULL, /* filter, filterarg */
259 MAXBSIZE, TWE_MAX_SGL_LENGTH, /* maxsize, nsegments */
260 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
261 BUS_DMA_ALLOCNOW, /* flags */
262 &sc->twe_parent_dmat)) {
263 twe_printf(sc, "can't allocate parent DMA tag\n");
269 * Allocate and connect our interrupt.
272 if ((sc->twe_irq = bus_alloc_resource(sc->twe_dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
273 twe_printf(sc, "can't allocate interrupt\n");
277 error = bus_setup_intr(sc->twe_dev, sc->twe_irq,
278 INTR_TYPE_BIO | INTR_ENTROPY, twe_pci_intr, sc,
279 &sc->twe_intr, NULL);
281 twe_printf(sc, "can't set up interrupt\n");
287 * Create DMA tag for mapping objects into controller-addressable space.
289 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
290 1, 0, /* alignment, boundary */
291 BUS_SPACE_MAXADDR, /* lowaddr */
292 BUS_SPACE_MAXADDR, /* highaddr */
293 NULL, NULL, /* filter, filterarg */
294 MAXBSIZE, TWE_MAX_SGL_LENGTH,/* maxsize, nsegments */
295 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
297 &sc->twe_buffer_dmat)) {
298 twe_printf(sc, "can't allocate data buffer DMA tag\n");
304 * Initialise the controller and driver core.
306 if ((error = twe_setup(sc)))
310 * Print some information about the controller and configuration.
312 twe_describe_controller(sc);
315 * Create the control device.
317 cdevsw_add(&twe_cdevsw, -1, device_get_unit(sc->twe_dev));
318 xdev = make_dev(&twe_cdevsw, device_get_unit(sc->twe_dev),
319 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
320 "twe%d", device_get_unit(sc->twe_dev));
324 * Schedule ourselves to bring the controller up once interrupts are available.
325 * This isn't strictly necessary, since we disable interrupts while probing the
326 * controller, but it is more in keeping with common practice for other disk
329 sc->twe_ich.ich_func = twe_intrhook;
330 sc->twe_ich.ich_arg = sc;
331 sc->twe_ich.ich_desc = "twe";
332 if (config_intrhook_establish(&sc->twe_ich) != 0) {
333 twe_printf(sc, "can't establish configuration hook\n");
341 /********************************************************************************
342 * Free all of the resources associated with (sc).
344 * Should not be called if the controller is active.
347 twe_free(struct twe_softc *sc)
349 struct twe_request *tr;
353 /* throw away any command buffers */
354 while ((tr = twe_dequeue_free(sc)) != NULL)
355 twe_free_request(tr);
357 /* destroy the data-transfer DMA tag */
358 if (sc->twe_buffer_dmat)
359 bus_dma_tag_destroy(sc->twe_buffer_dmat);
361 /* disconnect the interrupt handler */
363 bus_teardown_intr(sc->twe_dev, sc->twe_irq, sc->twe_intr);
364 if (sc->twe_irq != NULL)
365 bus_release_resource(sc->twe_dev, SYS_RES_IRQ, 0, sc->twe_irq);
367 /* destroy the parent DMA tag */
368 if (sc->twe_parent_dmat)
369 bus_dma_tag_destroy(sc->twe_parent_dmat);
371 /* release the register window mapping */
372 if (sc->twe_io != NULL)
373 bus_release_resource(sc->twe_dev, SYS_RES_IOPORT, TWE_IO_CONFIG_REG, sc->twe_io);
375 cdevsw_remove(&twe_cdevsw, -1, device_get_unit(sc->twe_dev));
377 sysctl_ctx_free(&sc->sysctl_ctx);
380 /********************************************************************************
381 * Disconnect from the controller completely, in preparation for unload.
384 twe_detach(device_t dev)
386 struct twe_softc *sc = device_get_softc(dev);
393 if (sc->twe_state & TWE_STATE_OPEN)
397 * Shut the controller down.
409 /********************************************************************************
410 * Bring the controller down to a dormant state and detach all child devices.
412 * Note that we can assume that the bioq on the controller is empty, as we won't
413 * allow shutdown if any device is open.
416 twe_shutdown(device_t dev)
418 struct twe_softc *sc = device_get_softc(dev);
426 * Delete all our child devices.
428 for (i = 0; i < TWE_MAX_UNITS; i++) {
429 twe_detach_drive(sc, i);
433 * 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, "device_add_child failed\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, "bus_generic_attach returned %d\n", error);
532 /********************************************************************************
533 * Detach the specified unit if it exsists
535 * This is called from twe_del_unit.
538 twe_detach_drive(struct twe_softc *sc, int unit)
541 if (sc->twe_drive[unit].td_disk != 0) {
542 if (device_delete_child(sc->twe_dev, sc->twe_drive[unit].td_disk) != 0)
543 twe_printf(sc, "failed to delete unit %d\n", unit);
544 sc->twe_drive[unit].td_disk = 0;
548 /********************************************************************************
549 * Clear a PCI parity error.
552 twe_clear_pci_parity_error(struct twe_softc *sc)
554 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PARITY_ERROR);
555 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PARITY_ERROR, 2);
558 /********************************************************************************
562 twe_clear_pci_abort(struct twe_softc *sc)
564 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PCI_ABORT);
565 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PCI_ABORT, 2);
568 /********************************************************************************
569 ********************************************************************************
571 ********************************************************************************
572 ********************************************************************************/
581 struct twe_softc *twed_controller; /* parent device softc */
582 struct twe_drive *twed_drive; /* drive data in parent softc */
583 struct disk twed_disk; /* generic disk handle */
584 struct devstat twed_stats; /* accounting */
585 struct disklabel twed_label; /* synthetic label */
587 #define TWED_OPEN (1<<0) /* drive is open (can't shut down) */
591 * Disk device bus interface
593 static int twed_probe(device_t dev);
594 static int twed_attach(device_t dev);
595 static int twed_detach(device_t dev);
597 static device_method_t twed_methods[] = {
598 DEVMETHOD(device_probe, twed_probe),
599 DEVMETHOD(device_attach, twed_attach),
600 DEVMETHOD(device_detach, twed_detach),
604 static driver_t twed_driver = {
607 sizeof(struct twed_softc)
610 static devclass_t twed_devclass;
612 DRIVER_MODULE(Xtwed, Xtwe, twed_driver, twed_devclass, 0, 0);
614 DRIVER_MODULE(twed, twe, twed_driver, twed_devclass, 0, 0);
618 * Disk device control interface.
620 static d_open_t twed_open;
621 static d_close_t twed_close;
622 static d_strategy_t twed_strategy;
623 static d_dump_t twed_dump;
625 #define TWED_CDEV_MAJOR 147
627 static struct cdevsw twed_cdevsw = {
646 /********************************************************************************
647 * Handle open from generic layer.
649 * Note that this is typically only called by the diskslice code, and not
650 * for opens on subdevices (eg. slices, partitions).
653 twed_open(dev_t dev, int flags, int fmt, d_thread_t *td)
655 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1;
656 struct disklabel *label;
663 /* check that the controller is up and running */
664 if (sc->twed_controller->twe_state & TWE_STATE_SHUTDOWN)
667 /* build synthetic label */
668 label = &sc->twed_disk.d_label;
669 bzero(label, sizeof(*label));
670 label->d_type = DTYPE_ESDI;
671 label->d_secsize = TWE_BLOCK_SIZE;
672 label->d_nsectors = sc->twed_drive->td_sectors;
673 label->d_ntracks = sc->twed_drive->td_heads;
674 label->d_ncylinders = sc->twed_drive->td_cylinders;
675 label->d_secpercyl = sc->twed_drive->td_sectors * sc->twed_drive->td_heads;
676 label->d_secperunit = sc->twed_drive->td_size;
678 sc->twed_flags |= TWED_OPEN;
682 /********************************************************************************
683 * Handle last close of the disk device.
686 twed_close(dev_t dev, int flags, int fmt, d_thread_t *td)
688 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1;
695 sc->twed_flags &= ~TWED_OPEN;
699 /********************************************************************************
700 * Handle an I/O request.
703 twed_strategy(twe_bio *bp)
705 struct twed_softc *sc = (struct twed_softc *)TWE_BIO_SOFTC(bp);
713 TWE_BIO_SET_ERROR(bp, EINVAL);
714 printf("twe: bio for invalid disk!\n");
720 /* perform accounting */
721 TWE_BIO_STATS_START(bp);
723 /* queue the bio on the controller */
724 twe_enqueue_bio(sc->twed_controller, bp);
726 /* poke the controller to start I/O */
727 twe_startio(sc->twed_controller);
731 /********************************************************************************
732 * System crashdump support
735 twed_dump(dev_t dev, u_int count, u_int blkno, u_int secsize)
737 struct twed_softc *twed_sc = (struct twed_softc *)dev->si_drv1;
738 struct twe_softc *twe_sc = (struct twe_softc *)twed_sc->twed_controller;
741 int dumppages = MAXDUMPPGS;
745 if (!twed_sc || !twe_sc)
748 blkcnt = howmany(PAGE_SIZE, secsize);
753 if ((count / blkcnt) < dumppages)
754 dumppages = count / blkcnt;
756 for (i = 0; i < dumppages; ++i) {
757 vm_paddr_t a = addr + (i * PAGE_SIZE);
758 if (is_physical_memory(a))
759 va = pmap_kenter_temporary(trunc_page(a), i);
761 va = pmap_kenter_temporary(trunc_page(0), i);
764 if ((error = twe_dump_blocks(twe_sc, twed_sc->twed_drive->td_unit, blkno, va,
765 (PAGE_SIZE * dumppages) / TWE_BLOCK_SIZE)) != 0)
769 if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
772 blkno += blkcnt * dumppages;
773 count -= blkcnt * dumppages;
774 addr += PAGE_SIZE * dumppages;
779 /********************************************************************************
780 * Handle completion of an I/O request.
783 twed_intr(twe_bio *bp)
787 /* if no error, transfer completed */
788 if (!TWE_BIO_HAS_ERROR(bp))
789 TWE_BIO_RESID(bp) = 0;
791 TWE_BIO_STATS_END(bp);
796 /********************************************************************************
797 * Default probe stub.
800 twed_probe(device_t dev)
805 /********************************************************************************
806 * Attach a unit to the controller.
809 twed_attach(device_t dev)
811 struct twed_softc *sc;
817 /* initialise our softc */
818 sc = device_get_softc(dev);
819 parent = device_get_parent(dev);
820 sc->twed_controller = (struct twe_softc *)device_get_softc(parent);
821 sc->twed_drive = device_get_ivars(dev);
824 /* report the drive */
825 twed_printf(sc, "%uMB (%u sectors)\n",
826 sc->twed_drive->td_size / ((1024 * 1024) / TWE_BLOCK_SIZE),
827 sc->twed_drive->td_size);
829 devstat_add_entry(&sc->twed_stats, "twed", device_get_unit(dev), TWE_BLOCK_SIZE,
830 DEVSTAT_NO_ORDERED_TAGS,
831 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
832 DEVSTAT_PRIORITY_ARRAY);
834 /* attach a generic disk device to ourselves */
835 dsk = disk_create(device_get_unit(dev), &sc->twed_disk, 0, &twed_cdevsw);
837 dsk->si_drv2 = &sc->twed_drive->td_unit;
838 sc->twed_dev_t = dsk;
840 /* set the maximum I/O size to the theoretical maximum allowed by the S/G list size */
841 dsk->si_iosize_max = (TWE_MAX_SGL_LENGTH - 1) * PAGE_SIZE;
846 /********************************************************************************
847 * Disconnect ourselves from the system.
850 twed_detach(device_t dev)
852 struct twed_softc *sc = (struct twed_softc *)device_get_softc(dev);
856 if (sc->twed_flags & TWED_OPEN)
859 devstat_remove_entry(&sc->twed_stats);
860 disk_destroy(&sc->twed_disk);
865 /********************************************************************************
866 ********************************************************************************
868 ********************************************************************************
869 ********************************************************************************/
871 static void twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
872 static void twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
874 /********************************************************************************
875 * Malloc space for a command buffer.
877 MALLOC_DEFINE(TWE_MALLOC_CLASS, "twe commands", "twe commands");
880 twe_allocate_request(struct twe_softc *sc)
882 struct twe_request *tr;
886 * TWE requires requests to be 512-byte aligned. Depend on malloc()
887 * guarenteeing alignment for power-of-2 requests. Note that the old
888 * (FreeBSD-4.x) malloc code aligned all requests, but the new slab
889 * allocator only guarentees same-size alignment for power-of-2 requests.
891 aligned_size = (sizeof(struct twe_request) + TWE_ALIGNMASK) &
893 tr = malloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT|M_ZERO);
895 if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_cmdmap)) {
896 twe_free_request(tr);
899 if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_dmamap)) {
900 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_cmdmap);
901 twe_free_request(tr);
907 /********************************************************************************
908 * Permanently discard a command buffer.
911 twe_free_request(struct twe_request *tr)
913 struct twe_softc *sc = tr->tr_sc;
917 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_cmdmap);
918 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_dmamap);
919 free(tr, TWE_MALLOC_CLASS);
922 /********************************************************************************
923 * Map/unmap (tr)'s command and data in the controller's addressable space.
925 * These routines ensure that the data which the controller is going to try to
926 * access is actually visible to the controller, in a machine-independant
927 * fashion. Due to a hardware limitation, I/O buffers must be 512-byte aligned
928 * and we take care of that here as well.
931 twe_fillin_sgl(TWE_SG_Entry *sgl, bus_dma_segment_t *segs, int nsegments, int max_sgl)
935 for (i = 0; i < nsegments; i++) {
936 sgl[i].address = segs[i].ds_addr;
937 sgl[i].length = segs[i].ds_len;
939 for (; i < max_sgl; i++) { /* XXX necessary? */
946 twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
948 struct twe_request *tr = (struct twe_request *)arg;
949 TWE_Command *cmd = &tr->tr_command;
953 /* save base of first segment in command (applicable if there only one segment) */
954 tr->tr_dataphys = segs[0].ds_addr;
956 /* correct command size for s/g list size */
957 tr->tr_command.generic.size += 2 * nsegments;
960 * Due to the fact that parameter and I/O commands have the scatter/gather list in
961 * different places, we need to determine which sort of command this actually is
962 * before we can populate it correctly.
964 switch(cmd->generic.opcode) {
965 case TWE_OP_GET_PARAM:
966 case TWE_OP_SET_PARAM:
967 cmd->generic.sgl_offset = 2;
968 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
972 cmd->generic.sgl_offset = 3;
973 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
975 case TWE_OP_ATA_PASSTHROUGH:
976 cmd->generic.sgl_offset = 5;
977 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
981 * Fall back to what the linux driver does.
982 * Do this because the API may send an opcode
983 * the driver knows nothing about and this will
984 * at least stop PCIABRT's from hosing us.
986 switch (cmd->generic.sgl_offset) {
988 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
991 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
994 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1001 twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1003 struct twe_request *tr = (struct twe_request *)arg;
1007 /* command can't cross a page boundary */
1008 tr->tr_cmdphys = segs[0].ds_addr;
1012 twe_map_request(struct twe_request *tr)
1014 struct twe_softc *sc = tr->tr_sc;
1020 * Map the command into bus space.
1022 bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_cmdmap, &tr->tr_command, sizeof(tr->tr_command),
1023 twe_setup_request_dmamap, tr, 0);
1024 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_cmdmap, BUS_DMASYNC_PREWRITE);
1027 * If the command involves data, map that too.
1029 if (tr->tr_data != NULL) {
1032 * Data must be 512-byte aligned; allocate a fixup buffer if it's not.
1034 if (((vm_offset_t)tr->tr_data % TWE_ALIGNMENT) != 0) {
1037 aligned_size = (tr->tr_length + TWE_ALIGNMASK) & ~TWE_ALIGNMASK;
1038 /* save pointer to 'real' data */
1039 tr->tr_realdata = tr->tr_data;
1040 tr->tr_flags |= TWE_CMD_ALIGNBUF;
1041 tr->tr_data = malloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT);
1045 * Map the data buffer into bus space and build the s/g list.
1047 bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_dmamap, tr->tr_data, tr->tr_length,
1048 twe_setup_data_dmamap, tr, 0);
1049 if (tr->tr_flags & TWE_CMD_DATAIN)
1050 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_PREREAD);
1051 if (tr->tr_flags & TWE_CMD_DATAOUT) {
1052 /* if we're using an alignment buffer, and we're writing data, copy the real data out */
1053 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1054 bcopy(tr->tr_realdata, tr->tr_data, tr->tr_length);
1055 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_PREWRITE);
1061 twe_unmap_request(struct twe_request *tr)
1063 struct twe_softc *sc = tr->tr_sc;
1068 * Unmap the command from bus space.
1070 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_cmdmap, BUS_DMASYNC_POSTWRITE);
1071 bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_cmdmap);
1074 * If the command involved data, unmap that too.
1076 if (tr->tr_data != NULL) {
1078 if (tr->tr_flags & TWE_CMD_DATAIN) {
1079 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_POSTREAD);
1080 /* if we're using an alignment buffer, and we're reading data, copy the real data in */
1081 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1082 bcopy(tr->tr_data, tr->tr_realdata, tr->tr_length);
1084 if (tr->tr_flags & TWE_CMD_DATAOUT)
1085 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_POSTWRITE);
1087 bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_dmamap);
1090 /* free alignment buffer if it was used */
1091 if (tr->tr_flags & TWE_CMD_ALIGNBUF) {
1092 free(tr->tr_data, TWE_MALLOC_CLASS);
1093 tr->tr_data = tr->tr_realdata; /* restore 'real' data pointer */
1098 /********************************************************************************
1099 * Print current controller status, call from DDB.
1104 struct twe_softc *sc;
1108 for (i = 0; (sc = devclass_get_softc(twe_devclass, i)) != NULL; i++)
1109 twe_print_controller(sc);
1110 printf("twed: total bio count in %u out %u\n", twed_bio_in, twed_bio_out);