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
33 * FreeBSD-specific code.
36 #include <dev/twe/twe_compat.h>
37 #include <dev/twe/twereg.h>
38 #include <dev/twe/tweio.h>
39 #include <dev/twe/twevar.h>
40 #include <dev/twe/twe_tables.h>
44 static devclass_t twe_devclass;
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++
56 static void twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
57 static void twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
59 /********************************************************************************
60 ********************************************************************************
61 Control device interface
62 ********************************************************************************
63 ********************************************************************************/
65 static d_open_t twe_open;
66 static d_close_t twe_close;
67 static d_ioctl_t twe_ioctl_wrapper;
69 static struct cdevsw twe_cdevsw = {
70 .d_version = D_VERSION,
71 .d_flags = D_NEEDGIANT,
74 .d_ioctl = twe_ioctl_wrapper,
78 /********************************************************************************
79 * Accept an open operation on the control device.
82 twe_open(struct cdev *dev, int flags, int fmt, struct thread *td)
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 cdev *dev, int flags, int fmt, struct thread *td)
96 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1;
98 sc->twe_state &= ~TWE_STATE_OPEN;
102 /********************************************************************************
103 * Handle controller-specific control operations.
106 twe_ioctl_wrapper(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
108 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1;
110 return(twe_ioctl(sc, cmd, addr));
113 /********************************************************************************
114 ********************************************************************************
116 ********************************************************************************
117 ********************************************************************************/
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);
129 static device_method_t twe_methods[] = {
130 /* Device interface */
131 DEVMETHOD(device_probe, twe_probe),
132 DEVMETHOD(device_attach, twe_attach),
133 DEVMETHOD(device_detach, twe_detach),
134 DEVMETHOD(device_shutdown, twe_shutdown),
135 DEVMETHOD(device_suspend, twe_suspend),
136 DEVMETHOD(device_resume, twe_resume),
138 DEVMETHOD(bus_print_child, bus_generic_print_child),
139 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
143 static driver_t twe_pci_driver = {
146 sizeof(struct twe_softc)
149 DRIVER_MODULE(twe, pci, twe_pci_driver, twe_devclass, 0, 0);
151 /********************************************************************************
152 * Match a 3ware Escalade ATA RAID controller.
155 twe_probe(device_t dev)
160 if ((pci_get_vendor(dev) == TWE_VENDOR_ID) &&
161 ((pci_get_device(dev) == TWE_DEVICE_ID) ||
162 (pci_get_device(dev) == TWE_DEVICE_ID_ASIC))) {
163 device_set_desc_copy(dev, TWE_DEVICE_NAME ". Driver version " TWE_DRIVER_VERSION_STRING);
164 return(BUS_PROBE_DEFAULT);
169 /********************************************************************************
170 * Allocate resources, initialise the controller.
173 twe_attach(device_t dev)
175 struct twe_softc *sc;
182 * Initialise the softc structure.
184 sc = device_get_softc(dev);
187 sysctl_ctx_init(&sc->sysctl_ctx);
188 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
189 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
190 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
191 if (sc->sysctl_tree == NULL) {
192 twe_printf(sc, "cannot add sysctl tree node\n");
195 SYSCTL_ADD_STRING(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
196 OID_AUTO, "driver_version", CTLFLAG_RD, TWE_DRIVER_VERSION_STRING, 0,
197 "TWE driver version");
200 * Make sure we are going to be able to talk to this board.
202 command = pci_read_config(dev, PCIR_COMMAND, 2);
203 if ((command & PCIM_CMD_PORTEN) == 0) {
204 twe_printf(sc, "register window not available\n");
208 * Force the busmaster enable bit on, in case the BIOS forgot.
210 command |= PCIM_CMD_BUSMASTEREN;
211 pci_write_config(dev, PCIR_COMMAND, command, 2);
214 * Allocate the PCI register window.
216 rid = TWE_IO_CONFIG_REG;
217 if ((sc->twe_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
218 RF_ACTIVE)) == NULL) {
219 twe_printf(sc, "can't allocate register window\n");
223 sc->twe_btag = rman_get_bustag(sc->twe_io);
224 sc->twe_bhandle = rman_get_bushandle(sc->twe_io);
227 * Allocate the parent bus DMA tag appropriate for PCI.
229 if (bus_dma_tag_create(NULL, /* parent */
230 1, 0, /* alignment, boundary */
231 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
232 BUS_SPACE_MAXADDR, /* highaddr */
233 NULL, NULL, /* filter, filterarg */
234 MAXBSIZE, TWE_MAX_SGL_LENGTH, /* maxsize, nsegments */
235 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
239 &sc->twe_parent_dmat)) {
240 twe_printf(sc, "can't allocate parent DMA tag\n");
246 * Allocate and connect our interrupt.
249 if ((sc->twe_irq = bus_alloc_resource_any(sc->twe_dev, SYS_RES_IRQ,
250 &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
251 twe_printf(sc, "can't allocate interrupt\n");
255 if (bus_setup_intr(sc->twe_dev, sc->twe_irq, INTR_TYPE_BIO | INTR_ENTROPY,
256 NULL, twe_pci_intr, sc, &sc->twe_intr)) {
257 twe_printf(sc, "can't set up interrupt\n");
263 * Create DMA tag for mapping command's into controller-addressable space.
265 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
266 1, 0, /* alignment, boundary */
267 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
268 BUS_SPACE_MAXADDR, /* highaddr */
269 NULL, NULL, /* filter, filterarg */
270 sizeof(TWE_Command) *
271 TWE_Q_LENGTH, 1, /* maxsize, nsegments */
272 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 busdma_lock_mutex, /* lockfunc */
306 &Giant, /* lockarg */
307 &sc->twe_buffer_dmat)) {
308 twe_printf(sc, "can't allocate data buffer DMA tag\n");
314 * Create DMA tag for mapping objects into controller-addressable space.
316 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
317 1, 0, /* alignment, boundary */
318 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
319 BUS_SPACE_MAXADDR, /* highaddr */
320 NULL, NULL, /* filter, filterarg */
321 MAXBSIZE, 1, /* maxsize, nsegments */
322 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
326 &sc->twe_immediate_dmat)) {
327 twe_printf(sc, "can't allocate data buffer DMA tag\n");
332 * Allocate memory for requests which cannot sleep or support continuation.
334 if (bus_dmamem_alloc(sc->twe_immediate_dmat, (void **)&sc->twe_immediate,
335 BUS_DMA_NOWAIT, &sc->twe_immediate_map)) {
336 twe_printf(sc, "can't allocate memory for immediate requests\n");
341 * Initialise the controller and driver core.
343 if ((error = twe_setup(sc))) {
349 * Print some information about the controller and configuration.
351 twe_describe_controller(sc);
354 * Create the control device.
356 sc->twe_dev_t = make_dev(&twe_cdevsw, device_get_unit(sc->twe_dev), UID_ROOT, GID_OPERATOR,
357 S_IRUSR | S_IWUSR, "twe%d", device_get_unit(sc->twe_dev));
358 sc->twe_dev_t->si_drv1 = sc;
360 * Schedule ourselves to bring the controller up once interrupts are available.
361 * This isn't strictly necessary, since we disable interrupts while probing the
362 * controller, but it is more in keeping with common practice for other disk
365 sc->twe_ich.ich_func = twe_intrhook;
366 sc->twe_ich.ich_arg = sc;
367 if (config_intrhook_establish(&sc->twe_ich) != 0) {
368 twe_printf(sc, "can't establish configuration hook\n");
376 /********************************************************************************
377 * Free all of the resources associated with (sc).
379 * Should not be called if the controller is active.
382 twe_free(struct twe_softc *sc)
384 struct twe_request *tr;
388 /* throw away any command buffers */
389 while ((tr = twe_dequeue_free(sc)) != NULL)
390 twe_free_request(tr);
392 if (sc->twe_cmd != NULL) {
393 bus_dmamap_unload(sc->twe_cmd_dmat, sc->twe_cmdmap);
394 bus_dmamem_free(sc->twe_cmd_dmat, sc->twe_cmd, sc->twe_cmdmap);
397 if (sc->twe_immediate != NULL) {
398 bus_dmamap_unload(sc->twe_immediate_dmat, sc->twe_immediate_map);
399 bus_dmamem_free(sc->twe_immediate_dmat, sc->twe_immediate,
400 sc->twe_immediate_map);
403 if (sc->twe_immediate_dmat)
404 bus_dma_tag_destroy(sc->twe_immediate_dmat);
406 /* destroy the data-transfer DMA tag */
407 if (sc->twe_buffer_dmat)
408 bus_dma_tag_destroy(sc->twe_buffer_dmat);
410 /* disconnect the interrupt handler */
412 bus_teardown_intr(sc->twe_dev, sc->twe_irq, sc->twe_intr);
413 if (sc->twe_irq != NULL)
414 bus_release_resource(sc->twe_dev, SYS_RES_IRQ, 0, sc->twe_irq);
416 /* destroy the parent DMA tag */
417 if (sc->twe_parent_dmat)
418 bus_dma_tag_destroy(sc->twe_parent_dmat);
420 /* release the register window mapping */
421 if (sc->twe_io != NULL)
422 bus_release_resource(sc->twe_dev, SYS_RES_IOPORT, TWE_IO_CONFIG_REG, sc->twe_io);
424 /* destroy control device */
425 if (sc->twe_dev_t != (struct cdev *)NULL)
426 destroy_dev(sc->twe_dev_t);
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);
509 sc->twe_state |= TWE_STATE_SUSPEND;
511 twe_disable_interrupts(sc);
517 /********************************************************************************
518 * Bring the controller back to a state ready for operation.
521 twe_resume(device_t dev)
523 struct twe_softc *sc = device_get_softc(dev);
527 sc->twe_state &= ~TWE_STATE_SUSPEND;
528 twe_enable_interrupts(sc);
533 /*******************************************************************************
534 * Take an interrupt, or be poked by other code to look for interrupt-worthy
538 twe_pci_intr(void *arg)
540 twe_intr((struct twe_softc *)arg);
543 /********************************************************************************
544 * Delayed-startup hook
547 twe_intrhook(void *arg)
549 struct twe_softc *sc = (struct twe_softc *)arg;
551 /* pull ourselves off the intrhook chain */
552 config_intrhook_disestablish(&sc->twe_ich);
554 /* call core startup routine */
558 /********************************************************************************
559 * Given a detected drive, attach it to the bio interface.
561 * This is called from twe_add_unit.
564 twe_attach_drive(struct twe_softc *sc, struct twe_drive *dr)
569 dr->td_disk = device_add_child(sc->twe_dev, NULL, -1);
570 if (dr->td_disk == NULL) {
571 twe_printf(sc, "Cannot add unit\n");
574 device_set_ivars(dr->td_disk, dr);
577 * XXX It would make sense to test the online/initialising bits, but they seem to be
580 sprintf(buf, "Unit %d, %s, %s",
582 twe_describe_code(twe_table_unittype, dr->td_type),
583 twe_describe_code(twe_table_unitstate, dr->td_state & TWE_PARAM_UNITSTATUS_MASK));
584 device_set_desc_copy(dr->td_disk, buf);
586 if ((error = bus_generic_attach(sc->twe_dev)) != 0) {
587 twe_printf(sc, "Cannot attach unit to controller. error = %d\n", error);
593 /********************************************************************************
594 * Detach the specified unit if it exsists
596 * This is called from twe_del_unit.
599 twe_detach_drive(struct twe_softc *sc, int unit)
603 if ((error = device_delete_child(sc->twe_dev, sc->twe_drive[unit].td_disk)) != 0) {
604 twe_printf(sc, "failed to delete unit %d\n", unit);
607 bzero(&sc->twe_drive[unit], sizeof(sc->twe_drive[unit]));
611 /********************************************************************************
612 * Clear a PCI parity error.
615 twe_clear_pci_parity_error(struct twe_softc *sc)
617 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PARITY_ERROR);
618 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PARITY_ERROR, 2);
621 /********************************************************************************
625 twe_clear_pci_abort(struct twe_softc *sc)
627 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PCI_ABORT);
628 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PCI_ABORT, 2);
631 /********************************************************************************
632 ********************************************************************************
634 ********************************************************************************
635 ********************************************************************************/
643 struct twe_softc *twed_controller; /* parent device softc */
644 struct twe_drive *twed_drive; /* drive data in parent softc */
645 struct disk *twed_disk; /* generic disk handle */
649 * Disk device bus interface
651 static int twed_probe(device_t dev);
652 static int twed_attach(device_t dev);
653 static int twed_detach(device_t dev);
655 static device_method_t twed_methods[] = {
656 DEVMETHOD(device_probe, twed_probe),
657 DEVMETHOD(device_attach, twed_attach),
658 DEVMETHOD(device_detach, twed_detach),
662 static driver_t twed_driver = {
665 sizeof(struct twed_softc)
668 static devclass_t twed_devclass;
669 DRIVER_MODULE(twed, twe, twed_driver, twed_devclass, 0, 0);
672 * Disk device control interface.
676 static int disks_registered = 0;
679 /********************************************************************************
680 * Handle open from generic layer.
682 * Note that this is typically only called by the diskslice code, and not
683 * for opens on subdevices (eg. slices, partitions).
686 twed_open(struct disk *dp)
688 struct twed_softc *sc = (struct twed_softc *)dp->d_drv1;
695 /* check that the controller is up and running */
696 if (sc->twed_controller->twe_state & TWE_STATE_SHUTDOWN)
702 /********************************************************************************
703 * Handle an I/O request.
706 twed_strategy(twe_bio *bp)
708 struct twed_softc *sc = (struct twed_softc *)TWE_BIO_SOFTC(bp);
712 bp->bio_driver1 = &sc->twed_drive->td_twe_unit;
716 if (sc == NULL || sc->twed_drive->td_disk == NULL) {
717 TWE_BIO_SET_ERROR(bp, EINVAL);
718 printf("twe: bio for invalid disk!\n");
724 /* perform accounting */
725 TWE_BIO_STATS_START(bp);
727 /* queue the bio on the controller */
728 twe_enqueue_bio(sc->twed_controller, bp);
730 /* poke the controller to start I/O */
731 twe_startio(sc->twed_controller);
735 /********************************************************************************
736 * System crashdump support
739 twed_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
741 struct twed_softc *twed_sc;
742 struct twe_softc *twe_sc;
747 twed_sc = (struct twed_softc *)dp->d_drv1;
750 twe_sc = (struct twe_softc *)twed_sc->twed_controller;
753 if ((error = twe_dump_blocks(twe_sc, twed_sc->twed_drive->td_twe_unit, offset / TWE_BLOCK_SIZE, virtual, length / TWE_BLOCK_SIZE)) != 0)
759 /********************************************************************************
760 * Handle completion of an I/O request.
763 twed_intr(twe_bio *bp)
767 /* if no error, transfer completed */
768 if (!TWE_BIO_HAS_ERROR(bp))
769 TWE_BIO_RESID(bp) = 0;
771 TWE_BIO_STATS_END(bp);
776 /********************************************************************************
777 * Default probe stub.
780 twed_probe(device_t dev)
785 /********************************************************************************
786 * Attach a unit to the controller.
789 twed_attach(device_t dev)
791 struct twed_softc *sc;
796 /* initialise our softc */
797 sc = device_get_softc(dev);
798 parent = device_get_parent(dev);
799 sc->twed_controller = (struct twe_softc *)device_get_softc(parent);
800 sc->twed_drive = device_get_ivars(dev);
803 /* report the drive */
804 twed_printf(sc, "%uMB (%u sectors)\n",
805 sc->twed_drive->td_size / ((1024 * 1024) / TWE_BLOCK_SIZE),
806 sc->twed_drive->td_size);
808 /* attach a generic disk device to ourselves */
810 sc->twed_drive->td_sys_unit = device_get_unit(dev);
812 sc->twed_disk = disk_alloc();
813 sc->twed_disk->d_open = twed_open;
814 sc->twed_disk->d_strategy = twed_strategy;
815 sc->twed_disk->d_dump = (dumper_t *)twed_dump;
816 sc->twed_disk->d_name = "twed";
817 sc->twed_disk->d_drv1 = sc;
818 sc->twed_disk->d_maxsize = (TWE_MAX_SGL_LENGTH - 1) * PAGE_SIZE;
819 sc->twed_disk->d_sectorsize = TWE_BLOCK_SIZE;
820 sc->twed_disk->d_mediasize = TWE_BLOCK_SIZE * (off_t)sc->twed_drive->td_size;
821 if (sc->twed_drive->td_type == TWE_UD_CONFIG_RAID0 ||
822 sc->twed_drive->td_type == TWE_UD_CONFIG_RAID5 ||
823 sc->twed_drive->td_type == TWE_UD_CONFIG_RAID10) {
824 sc->twed_disk->d_stripesize =
825 TWE_BLOCK_SIZE << sc->twed_drive->td_stripe;
826 sc->twed_disk->d_stripeoffset = 0;
828 sc->twed_disk->d_fwsectors = sc->twed_drive->td_sectors;
829 sc->twed_disk->d_fwheads = sc->twed_drive->td_heads;
830 sc->twed_disk->d_unit = sc->twed_drive->td_sys_unit;
831 sc->twed_disk->d_flags = DISKFLAG_NEEDSGIANT;
833 disk_create(sc->twed_disk, DISK_VERSION);
839 /* set the maximum I/O size to the theoretical maximum allowed by the S/G list size */
844 /********************************************************************************
845 * Disconnect ourselves from the system.
848 twed_detach(device_t dev)
850 struct twed_softc *sc = (struct twed_softc *)device_get_softc(dev);
854 if (sc->twed_disk->d_flags & DISKFLAG_OPEN)
857 disk_destroy(sc->twed_disk);
860 if (--disks_registered == 0)
861 cdevsw_remove(&tweddisk_cdevsw);
866 /********************************************************************************
867 ********************************************************************************
869 ********************************************************************************
870 ********************************************************************************/
872 /********************************************************************************
873 * Allocate a command buffer
875 MALLOC_DEFINE(TWE_MALLOC_CLASS, "twe_commands", "twe commands");
878 twe_allocate_request(struct twe_softc *sc, int tag)
880 struct twe_request *tr;
882 if ((tr = malloc(sizeof(struct twe_request), TWE_MALLOC_CLASS, M_WAITOK)) == NULL) {
883 twe_printf(sc, "unable to allocate memory for tag %d\n", tag);
886 bzero(tr, sizeof(*tr));
889 if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_dmamap)) {
890 twe_free_request(tr);
891 twe_printf(sc, "unable to allocate dmamap for tag %d\n", tag);
897 /********************************************************************************
898 * Permanently discard a command buffer.
901 twe_free_request(struct twe_request *tr)
903 struct twe_softc *sc = tr->tr_sc;
907 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_dmamap);
908 free(tr, TWE_MALLOC_CLASS);
911 /********************************************************************************
912 * Map/unmap (tr)'s command and data in the controller's addressable space.
914 * These routines ensure that the data which the controller is going to try to
915 * access is actually visible to the controller, in a machine-independant
916 * fashion. Due to a hardware limitation, I/O buffers must be 512-byte aligned
917 * and we take care of that here as well.
920 twe_fillin_sgl(TWE_SG_Entry *sgl, bus_dma_segment_t *segs, int nsegments, int max_sgl)
924 for (i = 0; i < nsegments; i++) {
925 sgl[i].address = segs[i].ds_addr;
926 sgl[i].length = segs[i].ds_len;
928 for (; i < max_sgl; i++) { /* XXX necessary? */
935 twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
937 struct twe_request *tr = (struct twe_request *)arg;
938 struct twe_softc *sc = tr->tr_sc;
939 TWE_Command *cmd = TWE_FIND_COMMAND(tr);
943 if (tr->tr_flags & TWE_CMD_MAPPED)
944 panic("already mapped command");
946 tr->tr_flags |= TWE_CMD_MAPPED;
948 if (tr->tr_flags & TWE_CMD_IN_PROGRESS)
949 sc->twe_state &= ~TWE_STATE_FRZN;
950 /* save base of first segment in command (applicable if there only one segment) */
951 tr->tr_dataphys = segs[0].ds_addr;
953 /* correct command size for s/g list size */
954 cmd->generic.size += 2 * nsegments;
957 * Due to the fact that parameter and I/O commands have the scatter/gather list in
958 * different places, we need to determine which sort of command this actually is
959 * before we can populate it correctly.
961 switch(cmd->generic.opcode) {
962 case TWE_OP_GET_PARAM:
963 case TWE_OP_SET_PARAM:
964 cmd->generic.sgl_offset = 2;
965 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
969 cmd->generic.sgl_offset = 3;
970 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
972 case TWE_OP_ATA_PASSTHROUGH:
973 cmd->generic.sgl_offset = 5;
974 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
978 * Fall back to what the linux driver does.
979 * Do this because the API may send an opcode
980 * the driver knows nothing about and this will
981 * at least stop PCIABRT's from hosing us.
983 switch (cmd->generic.sgl_offset) {
985 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
988 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
991 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
996 if (tr->tr_flags & TWE_CMD_DATAIN) {
997 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
998 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
999 BUS_DMASYNC_PREREAD);
1001 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1002 BUS_DMASYNC_PREREAD);
1006 if (tr->tr_flags & TWE_CMD_DATAOUT) {
1008 * if we're using an alignment buffer, and we're writing data
1009 * copy the real data out
1011 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1012 bcopy(tr->tr_realdata, tr->tr_data, tr->tr_length);
1014 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1015 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1016 BUS_DMASYNC_PREWRITE);
1018 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1019 BUS_DMASYNC_PREWRITE);
1023 if (twe_start(tr) == EBUSY) {
1024 tr->tr_sc->twe_state |= TWE_STATE_CTLR_BUSY;
1025 twe_requeue_ready(tr);
1030 twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1032 struct twe_softc *sc = (struct twe_softc *)arg;
1036 /* command can't cross a page boundary */
1037 sc->twe_cmdphys = segs[0].ds_addr;
1041 twe_map_request(struct twe_request *tr)
1043 struct twe_softc *sc = tr->tr_sc;
1048 if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN)) {
1049 twe_requeue_ready(tr);
1053 bus_dmamap_sync(sc->twe_cmd_dmat, sc->twe_cmdmap, BUS_DMASYNC_PREWRITE);
1056 * If the command involves data, map that too.
1058 if (tr->tr_data != NULL && ((tr->tr_flags & TWE_CMD_MAPPED) == 0)) {
1061 * Data must be 64-byte aligned; allocate a fixup buffer if it's not.
1063 if (((vm_offset_t)tr->tr_data % TWE_ALIGNMENT) != 0) {
1064 tr->tr_realdata = tr->tr_data; /* save pointer to 'real' data */
1065 tr->tr_flags |= TWE_CMD_ALIGNBUF;
1066 tr->tr_data = malloc(tr->tr_length, TWE_MALLOC_CLASS, M_NOWAIT);
1067 if (tr->tr_data == NULL) {
1068 twe_printf(sc, "%s: malloc failed\n", __func__);
1069 tr->tr_data = tr->tr_realdata; /* restore original data pointer */
1075 * Map the data buffer into bus space and build the s/g list.
1077 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1078 error = bus_dmamap_load(sc->twe_immediate_dmat, sc->twe_immediate_map, sc->twe_immediate,
1079 tr->tr_length, twe_setup_data_dmamap, tr, BUS_DMA_NOWAIT);
1081 error = bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_dmamap, tr->tr_data, tr->tr_length,
1082 twe_setup_data_dmamap, tr, 0);
1084 if (error == EINPROGRESS) {
1085 tr->tr_flags |= TWE_CMD_IN_PROGRESS;
1086 sc->twe_state |= TWE_STATE_FRZN;
1090 if ((error = twe_start(tr)) == EBUSY) {
1091 sc->twe_state |= TWE_STATE_CTLR_BUSY;
1092 twe_requeue_ready(tr);
1099 twe_unmap_request(struct twe_request *tr)
1101 struct twe_softc *sc = tr->tr_sc;
1105 bus_dmamap_sync(sc->twe_cmd_dmat, sc->twe_cmdmap, BUS_DMASYNC_POSTWRITE);
1108 * If the command involved data, unmap that too.
1110 if (tr->tr_data != NULL) {
1111 if (tr->tr_flags & TWE_CMD_DATAIN) {
1112 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1113 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1114 BUS_DMASYNC_POSTREAD);
1116 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1117 BUS_DMASYNC_POSTREAD);
1120 /* if we're using an alignment buffer, and we're reading data, copy the real data in */
1121 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1122 bcopy(tr->tr_data, tr->tr_realdata, tr->tr_length);
1124 if (tr->tr_flags & TWE_CMD_DATAOUT) {
1125 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1126 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1127 BUS_DMASYNC_POSTWRITE);
1129 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1130 BUS_DMASYNC_POSTWRITE);
1134 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1135 bus_dmamap_unload(sc->twe_immediate_dmat, sc->twe_immediate_map);
1137 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);