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.54 2012/11/17 01:52:19 svnexp 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>
42 #include <sys/mplock2.h>
46 static devclass_t twe_devclass;
49 static u_int32_t twed_bio_in;
50 #define TWED_BIO_IN twed_bio_in++
51 static u_int32_t twed_bio_out;
52 #define TWED_BIO_OUT twed_bio_out++
58 static void twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
59 static void twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error);
61 /********************************************************************************
62 ********************************************************************************
63 Control device interface
64 ********************************************************************************
65 ********************************************************************************/
67 static d_open_t twe_open;
68 static d_close_t twe_close;
69 static d_ioctl_t twe_ioctl_wrapper;
71 static struct dev_ops twe_ops = {
72 { "twe", 0, D_MPSAFE },
75 .d_ioctl = twe_ioctl_wrapper,
78 /********************************************************************************
79 * Accept an open operation on the control device.
82 twe_open(struct dev_open_args *ap)
84 cdev_t dev = ap->a_head.a_dev;
85 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1;
88 if (sc->twe_state & TWE_STATE_DETACHING) {
92 sc->twe_state |= TWE_STATE_OPEN;
97 /********************************************************************************
98 * Accept the last close on the control device.
101 twe_close(struct dev_close_args *ap)
103 cdev_t dev = ap->a_head.a_dev;
104 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1;
107 sc->twe_state &= ~TWE_STATE_OPEN;
112 /********************************************************************************
113 * Handle controller-specific control operations.
116 twe_ioctl_wrapper(struct dev_ioctl_args *ap)
118 cdev_t dev = ap->a_head.a_dev;
119 u_long cmd = ap->a_cmd;
120 caddr_t addr = ap->a_data;
121 struct twe_softc *sc = (struct twe_softc *)dev->si_drv1;
123 return(twe_ioctl(sc, cmd, addr));
126 /********************************************************************************
127 ********************************************************************************
129 ********************************************************************************
130 ********************************************************************************/
132 static int twe_probe(device_t dev);
133 static int twe_attach(device_t dev);
134 static void twe_free(struct twe_softc *sc);
135 static int twe_detach(device_t dev);
136 static int twe_shutdown(device_t dev);
137 static int twe_suspend(device_t dev);
138 static int twe_resume(device_t dev);
139 static void twe_pci_intr(void *arg);
140 static void twe_intrhook(void *arg);
142 static device_method_t twe_methods[] = {
143 /* Device interface */
144 DEVMETHOD(device_probe, twe_probe),
145 DEVMETHOD(device_attach, twe_attach),
146 DEVMETHOD(device_detach, twe_detach),
147 DEVMETHOD(device_shutdown, twe_shutdown),
148 DEVMETHOD(device_suspend, twe_suspend),
149 DEVMETHOD(device_resume, twe_resume),
154 static driver_t twe_pci_driver = {
157 sizeof(struct twe_softc)
160 DRIVER_MODULE(twe, pci, twe_pci_driver, twe_devclass, NULL, NULL);
162 /********************************************************************************
163 * Match a 3ware Escalade ATA RAID controller.
166 twe_probe(device_t dev)
171 if ((pci_get_vendor(dev) == TWE_VENDOR_ID) &&
172 ((pci_get_device(dev) == TWE_DEVICE_ID) ||
173 (pci_get_device(dev) == TWE_DEVICE_ID_ASIC))) {
174 device_set_desc_copy(dev, TWE_DEVICE_NAME ". Driver version " TWE_DRIVER_VERSION_STRING);
175 return(BUS_PROBE_DEFAULT);
180 /********************************************************************************
181 * Allocate resources, initialise the controller.
184 twe_attach(device_t dev)
186 struct twe_softc *sc;
192 * Initialise the softc structure.
194 sc = device_get_softc(dev);
196 lockinit(&sc->twe_io_lock, "twe I/O", 0, LK_CANRECURSE);
197 lockinit(&sc->twe_config_lock, "twe config", 0, LK_CANRECURSE);
199 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
200 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
201 OID_AUTO, "driver_version", CTLFLAG_RD, TWE_DRIVER_VERSION_STRING, 0,
202 "TWE driver version");
205 * Force the busmaster enable bit on, in case the BIOS forgot.
207 pci_enable_busmaster(dev);
210 * Allocate the PCI register window.
212 rid = TWE_IO_CONFIG_REG;
213 if ((sc->twe_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
214 RF_ACTIVE)) == NULL) {
215 twe_printf(sc, "can't allocate register window\n");
221 * Allocate the parent bus DMA tag appropriate for PCI.
223 if (bus_dma_tag_create(NULL, /* parent */
224 1, 0, /* alignment, boundary */
225 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
226 BUS_SPACE_MAXADDR, /* highaddr */
227 NULL, NULL, /* filter, filterarg */
228 MAXBSIZE, TWE_MAX_SGL_LENGTH, /* maxsize, nsegments */
229 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
231 &sc->twe_parent_dmat)) {
232 twe_printf(sc, "can't allocate parent DMA tag\n");
238 * Allocate and connect our interrupt.
241 if ((sc->twe_irq = bus_alloc_resource_any(sc->twe_dev, SYS_RES_IRQ,
242 &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
243 twe_printf(sc, "can't allocate interrupt\n");
247 if (bus_setup_intr(sc->twe_dev, sc->twe_irq, INTR_MPSAFE,
248 twe_pci_intr, sc, &sc->twe_intr, NULL)) {
249 twe_printf(sc, "can't set up interrupt\n");
255 * Create DMA tag for mapping command's into controller-addressable space.
257 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
258 1, 0, /* alignment, boundary */
259 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
260 BUS_SPACE_MAXADDR, /* highaddr */
261 NULL, NULL, /* filter, filterarg */
262 sizeof(TWE_Command) *
263 TWE_Q_LENGTH, 1, /* maxsize, nsegments */
264 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
266 &sc->twe_cmd_dmat)) {
267 twe_printf(sc, "can't allocate data buffer DMA tag\n");
272 * Allocate memory and make it available for DMA.
274 if (bus_dmamem_alloc(sc->twe_cmd_dmat, (void **)&sc->twe_cmd,
275 BUS_DMA_NOWAIT, &sc->twe_cmdmap)) {
276 twe_printf(sc, "can't allocate command memory\n");
279 bus_dmamap_load(sc->twe_cmd_dmat, sc->twe_cmdmap, sc->twe_cmd,
280 sizeof(TWE_Command) * TWE_Q_LENGTH,
281 twe_setup_request_dmamap, sc, 0);
282 bzero(sc->twe_cmd, sizeof(TWE_Command) * TWE_Q_LENGTH);
285 * Create DMA tag for mapping objects into controller-addressable space.
287 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
288 1, 0, /* alignment, boundary */
289 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
290 BUS_SPACE_MAXADDR, /* highaddr */
291 NULL, NULL, /* filter, filterarg */
292 MAXBSIZE, TWE_MAX_SGL_LENGTH,/* maxsize, nsegments */
293 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
294 BUS_DMA_ALLOCNOW, /* flags */
295 &sc->twe_buffer_dmat)) {
296 twe_printf(sc, "can't allocate data buffer DMA tag\n");
302 * Create DMA tag for mapping objects into controller-addressable space.
304 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
305 1, 0, /* alignment, boundary */
306 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
307 BUS_SPACE_MAXADDR, /* highaddr */
308 NULL, NULL, /* filter, filterarg */
309 MAXBSIZE, 1, /* maxsize, nsegments */
310 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
312 &sc->twe_immediate_dmat)) {
313 twe_printf(sc, "can't allocate data buffer DMA tag\n");
318 * Allocate memory for requests which cannot sleep or support continuation.
320 if (bus_dmamem_alloc(sc->twe_immediate_dmat, (void **)&sc->twe_immediate,
321 BUS_DMA_NOWAIT, &sc->twe_immediate_map)) {
322 twe_printf(sc, "can't allocate memory for immediate requests\n");
327 * Initialise the controller and driver core.
329 if ((error = twe_setup(sc))) {
335 * Print some information about the controller and configuration.
337 twe_describe_controller(sc);
340 * Create the control device.
342 sc->twe_dev_t = make_dev(&twe_ops, device_get_unit(sc->twe_dev),
343 UID_ROOT, GID_OPERATOR,
344 S_IRUSR | S_IWUSR, "twe%d",
345 device_get_unit(sc->twe_dev));
346 sc->twe_dev_t->si_drv1 = sc;
349 * Schedule ourselves to bring the controller up once interrupts are
350 * available. This isn't strictly necessary, since we disable
351 * interrupts while probing the controller, but it is more in keeping
352 * with common practice for other disk devices.
354 sc->twe_ich.ich_func = twe_intrhook;
355 sc->twe_ich.ich_arg = sc;
356 sc->twe_ich.ich_desc = "twe";
357 if (config_intrhook_establish(&sc->twe_ich) != 0) {
358 twe_printf(sc, "can't establish configuration hook\n");
366 /********************************************************************************
367 * Free all of the resources associated with (sc).
369 * Should not be called if the controller is active.
372 twe_free(struct twe_softc *sc)
374 struct twe_request *tr;
378 /* throw away any command buffers */
379 while ((tr = twe_dequeue_free(sc)) != NULL)
380 twe_free_request(tr);
382 if (sc->twe_cmd != NULL) {
383 bus_dmamap_unload(sc->twe_cmd_dmat, sc->twe_cmdmap);
384 bus_dmamem_free(sc->twe_cmd_dmat, sc->twe_cmd, sc->twe_cmdmap);
387 if (sc->twe_immediate != NULL) {
388 bus_dmamap_unload(sc->twe_immediate_dmat, sc->twe_immediate_map);
389 bus_dmamem_free(sc->twe_immediate_dmat, sc->twe_immediate,
390 sc->twe_immediate_map);
393 if (sc->twe_immediate_dmat)
394 bus_dma_tag_destroy(sc->twe_immediate_dmat);
396 /* destroy the data-transfer DMA tag */
397 if (sc->twe_buffer_dmat)
398 bus_dma_tag_destroy(sc->twe_buffer_dmat);
400 /* disconnect the interrupt handler */
402 bus_teardown_intr(sc->twe_dev, sc->twe_irq, sc->twe_intr);
403 if (sc->twe_irq != NULL)
404 bus_release_resource(sc->twe_dev, SYS_RES_IRQ, 0, sc->twe_irq);
406 /* destroy the parent DMA tag */
407 if (sc->twe_parent_dmat)
408 bus_dma_tag_destroy(sc->twe_parent_dmat);
410 /* release the register window mapping */
411 if (sc->twe_io != NULL)
412 bus_release_resource(sc->twe_dev, SYS_RES_IOPORT, TWE_IO_CONFIG_REG, sc->twe_io);
414 /* destroy control device */
415 if (sc->twe_dev_t != NULL)
416 destroy_dev(sc->twe_dev_t);
417 dev_ops_remove_minor(&twe_ops, device_get_unit(sc->twe_dev));
419 lockuninit(&sc->twe_config_lock);
420 lockuninit(&sc->twe_io_lock);
423 /********************************************************************************
424 * Disconnect from the controller completely, in preparation for unload.
427 twe_detach(device_t dev)
429 struct twe_softc *sc = device_get_softc(dev);
434 if (sc->twe_state & TWE_STATE_OPEN) {
438 sc->twe_state |= TWE_STATE_DETACHING;
442 * Shut the controller down.
444 if (twe_shutdown(dev)) {
446 sc->twe_state &= ~TWE_STATE_DETACHING;
456 /********************************************************************************
457 * Bring the controller down to a dormant state and detach all child devices.
459 * Note that we can assume that the bioq on the controller is empty, as we won't
460 * allow shutdown if any device is open.
463 twe_shutdown(device_t dev)
465 struct twe_softc *sc = device_get_softc(dev);
471 * Delete all our child devices.
474 for (i = 0; i < TWE_MAX_UNITS; i++) {
475 if (sc->twe_drive[i].td_disk != 0) {
476 if ((error = twe_detach_drive(sc, i)) != 0) {
477 TWE_CONFIG_UNLOCK(sc);
482 TWE_CONFIG_UNLOCK(sc);
485 * Bring the controller down.
494 /********************************************************************************
495 * Bring the controller to a quiescent state, ready for system suspend.
498 twe_suspend(device_t dev)
500 struct twe_softc *sc = device_get_softc(dev);
505 sc->twe_state |= TWE_STATE_SUSPEND;
508 twe_disable_interrupts(sc);
514 /********************************************************************************
515 * Bring the controller back to a state ready for operation.
518 twe_resume(device_t dev)
520 struct twe_softc *sc = device_get_softc(dev);
525 sc->twe_state &= ~TWE_STATE_SUSPEND;
526 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 struct twe_softc *sc = arg;
546 /********************************************************************************
547 * Delayed-startup hook
550 twe_intrhook(void *arg)
552 struct twe_softc *sc = (struct twe_softc *)arg;
554 /* pull ourselves off the intrhook chain */
555 config_intrhook_disestablish(&sc->twe_ich);
557 /* call core startup routine */
561 /********************************************************************************
562 * Given a detected drive, attach it to the bio interface.
564 * This is called from twe_add_unit.
567 twe_attach_drive(struct twe_softc *sc, struct twe_drive *dr)
573 dr->td_disk = device_add_child(sc->twe_dev, NULL, -1);
574 if (dr->td_disk == NULL) {
576 twe_printf(sc, "Cannot add unit\n");
579 device_set_ivars(dr->td_disk, dr);
582 * XXX It would make sense to test the online/initialising bits, but they seem to be
585 ksprintf(buf, "Unit %d, %s, %s",
587 twe_describe_code(twe_table_unittype, dr->td_type),
588 twe_describe_code(twe_table_unitstate, dr->td_state & TWE_PARAM_UNITSTATUS_MASK));
589 device_set_desc_copy(dr->td_disk, buf);
591 error = device_probe_and_attach(dr->td_disk);
594 twe_printf(sc, "Cannot attach unit to controller. error = %d\n", error);
600 /********************************************************************************
601 * Detach the specified unit if it exsists
603 * This is called from twe_del_unit.
606 twe_detach_drive(struct twe_softc *sc, int unit)
610 TWE_CONFIG_ASSERT_LOCKED(sc);
612 error = device_delete_child(sc->twe_dev, sc->twe_drive[unit].td_disk);
615 twe_printf(sc, "failed to delete unit %d\n", unit);
618 bzero(&sc->twe_drive[unit], sizeof(sc->twe_drive[unit]));
622 /********************************************************************************
623 * Clear a PCI parity error.
626 twe_clear_pci_parity_error(struct twe_softc *sc)
628 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PARITY_ERROR);
629 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PARITY_ERROR, 2);
632 /********************************************************************************
636 twe_clear_pci_abort(struct twe_softc *sc)
638 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PCI_ABORT);
639 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PCI_ABORT, 2);
642 /********************************************************************************
643 ********************************************************************************
645 ********************************************************************************
646 ********************************************************************************/
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, NULL, NULL);
672 * Disk device control interface.
674 static d_open_t twed_open;
675 static d_close_t twed_close;
676 static d_strategy_t twed_strategy;
677 static d_dump_t twed_dump;
679 static struct dev_ops twed_ops = {
680 { "twed", 0, D_DISK | D_MPSAFE},
682 .d_close = twed_close,
684 .d_write = physwrite,
685 .d_strategy = twed_strategy,
689 /********************************************************************************
690 * Handle open from generic layer.
692 * Note that this is typically only called by the diskslice code, and not
693 * for opens on subdevices (eg. slices, partitions).
696 twed_open(struct dev_open_args *ap)
698 cdev_t dev = ap->a_head.a_dev;
699 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1;
706 /* check that the controller is up and running */
707 if (sc->twed_controller->twe_state & TWE_STATE_SHUTDOWN)
710 sc->twed_flags |= TWED_OPEN;
714 /********************************************************************************
715 * Handle last close of the disk device.
718 twed_close(struct dev_close_args *ap)
720 cdev_t dev = ap->a_head.a_dev;
721 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1;
728 sc->twed_flags &= ~TWED_OPEN;
732 /********************************************************************************
733 * Handle an I/O request.
736 twed_strategy(struct dev_strategy_args *ap)
738 cdev_t dev = ap->a_head.a_dev;
739 struct bio *bio = ap->a_bio;
740 struct twed_softc *sc = dev->si_drv1;
741 struct buf *bp = bio->bio_buf;
743 bio->bio_driver_info = sc;
750 if (sc == NULL || sc->twed_drive->td_disk == NULL) {
751 bp->b_error = EINVAL;
752 bp->b_flags |= B_ERROR;
753 kprintf("twe: bio for invalid disk!\n");
759 /* perform accounting */
760 devstat_start_transaction(&sc->twed_stats);
762 /* queue the bio on the controller */
763 TWE_IO_LOCK(sc->twed_controller);
764 twe_enqueue_bio(sc->twed_controller, bio);
766 /* poke the controller to start I/O */
767 twe_startio(sc->twed_controller);
768 TWE_IO_UNLOCK(sc->twed_controller);
772 /********************************************************************************
773 * System crashdump support
776 twed_dump(struct dev_dump_args *ap)
778 cdev_t dev = ap->a_head.a_dev;
779 size_t length = ap->a_length;
780 off_t offset = ap->a_offset;
781 void *virtual = ap->a_virtual;
782 struct twed_softc *twed_sc;
783 struct twe_softc *twe_sc;
786 twed_sc = dev->si_drv1;
789 twe_sc = (struct twe_softc *)twed_sc->twed_controller;
792 if ((error = twe_dump_blocks(twe_sc, twed_sc->twed_drive->td_twe_unit, offset / TWE_BLOCK_SIZE, virtual, length / TWE_BLOCK_SIZE)) != 0)
798 /********************************************************************************
799 * Handle completion of an I/O request.
802 twed_intr(struct bio *bio)
804 struct buf *bp = bio->bio_buf;
805 struct twed_softc *sc = bio->bio_driver_info;
809 /* if no error, transfer completed */
810 if (!(bp->b_flags & B_ERROR))
812 devstat_end_transaction_buf(&sc->twed_stats, bp);
817 /********************************************************************************
818 * Default probe stub.
821 twed_probe(device_t dev)
826 /********************************************************************************
827 * Attach a unit to the controller.
830 twed_attach(device_t dev)
832 struct twed_softc *sc;
833 struct disk_info info;
839 /* initialise our softc */
840 sc = device_get_softc(dev);
841 parent = device_get_parent(dev);
842 sc->twed_controller = (struct twe_softc *)device_get_softc(parent);
843 sc->twed_drive = device_get_ivars(dev);
846 /* report the drive */
847 twed_printf(sc, "%uMB (%u sectors)\n",
848 sc->twed_drive->td_size / ((1024 * 1024) / TWE_BLOCK_SIZE),
849 sc->twed_drive->td_size);
851 /* attach a generic disk device to ourselves */
853 sc->twed_drive->td_sys_unit = device_get_unit(dev);
855 devstat_add_entry(&sc->twed_stats, "twed", sc->twed_drive->td_sys_unit,
857 DEVSTAT_NO_ORDERED_TAGS,
858 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
859 DEVSTAT_PRIORITY_ARRAY);
861 dsk = disk_create(sc->twed_drive->td_sys_unit, &sc->twed_disk, &twed_ops);
863 sc->twed_dev_t = dsk;
865 /* set the maximum I/O size to the theoretical maximum allowed by the S/G list size */
866 dsk->si_iosize_max = (TWE_MAX_SGL_LENGTH - 1) * PAGE_SIZE;
869 * Set disk info, as it appears that all needed data is available already.
870 * Setting the disk info will also cause the probing to start.
872 bzero(&info, sizeof(info));
873 info.d_media_blksize = TWE_BLOCK_SIZE; /* mandatory */
874 info.d_media_blocks = sc->twed_drive->td_size;
876 info.d_type = DTYPE_ESDI; /* optional */
877 info.d_secpertrack = sc->twed_drive->td_sectors;
878 info.d_nheads = sc->twed_drive->td_heads;
879 info.d_ncylinders = sc->twed_drive->td_cylinders;
880 info.d_secpercyl = sc->twed_drive->td_sectors * sc->twed_drive->td_heads;
882 disk_setdiskinfo(&sc->twed_disk, &info);
887 /********************************************************************************
888 * Disconnect ourselves from the system.
891 twed_detach(device_t dev)
893 struct twed_softc *sc = (struct twed_softc *)device_get_softc(dev);
897 if (sc->twed_flags & TWED_OPEN)
900 devstat_remove_entry(&sc->twed_stats);
901 disk_destroy(&sc->twed_disk);
906 /********************************************************************************
907 ********************************************************************************
909 ********************************************************************************
910 ********************************************************************************/
912 /********************************************************************************
913 * Allocate a command buffer
915 static MALLOC_DEFINE(TWE_MALLOC_CLASS, "twe_commands", "twe commands");
918 twe_allocate_request(struct twe_softc *sc, int tag)
920 struct twe_request *tr;
924 * TWE requires requests to be 512-byte aligned. Depend on malloc()
925 * guarenteeing alignment for power-of-2 requests. Note that the old
926 * (FreeBSD-4.x) malloc code aligned all requests, but the new slab
927 * allocator only guarentees same-size alignment for power-of-2 requests.
929 aligned_size = (sizeof(struct twe_request) + TWE_ALIGNMASK) &
931 tr = kmalloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT | M_ZERO);
934 if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_dmamap)) {
935 twe_free_request(tr);
936 twe_printf(sc, "unable to allocate dmamap for tag %d\n", tag);
942 /********************************************************************************
943 * Permanently discard a command buffer.
946 twe_free_request(struct twe_request *tr)
948 struct twe_softc *sc = tr->tr_sc;
952 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_dmamap);
953 kfree(tr, TWE_MALLOC_CLASS);
956 /********************************************************************************
957 * Map/unmap (tr)'s command and data in the controller's addressable space.
959 * These routines ensure that the data which the controller is going to try to
960 * access is actually visible to the controller, in a machine-independant
961 * fashion. Due to a hardware limitation, I/O buffers must be 512-byte aligned
962 * and we take care of that here as well.
965 twe_fillin_sgl(TWE_SG_Entry *sgl, bus_dma_segment_t *segs, int nsegments, int max_sgl)
969 for (i = 0; i < nsegments; i++) {
970 sgl[i].address = segs[i].ds_addr;
971 sgl[i].length = segs[i].ds_len;
973 for (; i < max_sgl; i++) { /* XXX necessary? */
980 twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
982 struct twe_request *tr = (struct twe_request *)arg;
983 struct twe_softc *sc = tr->tr_sc;
984 TWE_Command *cmd = TWE_FIND_COMMAND(tr);
988 if (tr->tr_flags & TWE_CMD_MAPPED)
989 panic("already mapped command");
991 tr->tr_flags |= TWE_CMD_MAPPED;
993 if (tr->tr_flags & TWE_CMD_IN_PROGRESS)
994 sc->twe_state &= ~TWE_STATE_FRZN;
995 /* save base of first segment in command (applicable if there only one segment) */
996 tr->tr_dataphys = segs[0].ds_addr;
998 /* correct command size for s/g list size */
999 cmd->generic.size += 2 * nsegments;
1002 * Due to the fact that parameter and I/O commands have the scatter/gather list in
1003 * different places, we need to determine which sort of command this actually is
1004 * before we can populate it correctly.
1006 switch(cmd->generic.opcode) {
1007 case TWE_OP_GET_PARAM:
1008 case TWE_OP_SET_PARAM:
1009 cmd->generic.sgl_offset = 2;
1010 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1014 cmd->generic.sgl_offset = 3;
1015 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1017 case TWE_OP_ATA_PASSTHROUGH:
1018 cmd->generic.sgl_offset = 5;
1019 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1023 * Fall back to what the linux driver does.
1024 * Do this because the API may send an opcode
1025 * the driver knows nothing about and this will
1026 * at least stop PCIABRT's from hosing us.
1028 switch (cmd->generic.sgl_offset) {
1030 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1033 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1036 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1041 if (tr->tr_flags & TWE_CMD_DATAIN) {
1042 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1043 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1044 BUS_DMASYNC_PREREAD);
1046 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1047 BUS_DMASYNC_PREREAD);
1051 if (tr->tr_flags & TWE_CMD_DATAOUT) {
1053 * if we're using an alignment buffer, and we're writing data
1054 * copy the real data out
1056 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1057 bcopy(tr->tr_realdata, tr->tr_data, tr->tr_length);
1059 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1060 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1061 BUS_DMASYNC_PREWRITE);
1063 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1064 BUS_DMASYNC_PREWRITE);
1068 if (twe_start(tr) == EBUSY) {
1069 tr->tr_sc->twe_state |= TWE_STATE_CTLR_BUSY;
1070 twe_requeue_ready(tr);
1075 twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1077 struct twe_softc *sc = (struct twe_softc *)arg;
1081 /* command can't cross a page boundary */
1082 sc->twe_cmdphys = segs[0].ds_addr;
1086 twe_map_request(struct twe_request *tr)
1088 struct twe_softc *sc = tr->tr_sc;
1093 twe_lockassert(&sc->twe_io_lock);
1094 if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN)) {
1095 twe_requeue_ready(tr);
1099 bus_dmamap_sync(sc->twe_cmd_dmat, sc->twe_cmdmap, BUS_DMASYNC_PREWRITE);
1102 * If the command involves data, map that too.
1104 if (tr->tr_data != NULL && ((tr->tr_flags & TWE_CMD_MAPPED) == 0)) {
1107 * Data must be 512-byte aligned; allocate a fixup buffer if it's not.
1109 * DragonFly's malloc only guarentees alignment for requests which
1110 * are power-of-2 sized.
1112 if (((vm_offset_t)tr->tr_data % TWE_ALIGNMENT) != 0) {
1115 tr->tr_realdata = tr->tr_data; /* save pointer to 'real' data */
1116 aligned_size = TWE_ALIGNMENT;
1117 while (aligned_size < tr->tr_length)
1119 tr->tr_flags |= TWE_CMD_ALIGNBUF;
1120 tr->tr_data = kmalloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT);
1121 if (tr->tr_data == NULL) {
1122 twe_printf(sc, "%s: malloc failed\n", __func__);
1123 tr->tr_data = tr->tr_realdata; /* restore original data pointer */
1129 * Map the data buffer into bus space and build the s/g list.
1131 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1132 error = bus_dmamap_load(sc->twe_immediate_dmat, sc->twe_immediate_map, sc->twe_immediate,
1133 tr->tr_length, twe_setup_data_dmamap, tr, BUS_DMA_NOWAIT);
1135 error = bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_dmamap, tr->tr_data, tr->tr_length,
1136 twe_setup_data_dmamap, tr, 0);
1138 if (error == EINPROGRESS) {
1139 tr->tr_flags |= TWE_CMD_IN_PROGRESS;
1140 sc->twe_state |= TWE_STATE_FRZN;
1144 if ((error = twe_start(tr)) == EBUSY) {
1145 sc->twe_state |= TWE_STATE_CTLR_BUSY;
1146 twe_requeue_ready(tr);
1153 twe_unmap_request(struct twe_request *tr)
1155 struct twe_softc *sc = tr->tr_sc;
1159 bus_dmamap_sync(sc->twe_cmd_dmat, sc->twe_cmdmap, BUS_DMASYNC_POSTWRITE);
1162 * If the command involved data, unmap that too.
1164 if (tr->tr_data != NULL) {
1165 if (tr->tr_flags & TWE_CMD_DATAIN) {
1166 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1167 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1168 BUS_DMASYNC_POSTREAD);
1170 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1171 BUS_DMASYNC_POSTREAD);
1174 /* if we're using an alignment buffer, and we're reading data, copy the real data in */
1175 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1176 bcopy(tr->tr_data, tr->tr_realdata, tr->tr_length);
1178 if (tr->tr_flags & TWE_CMD_DATAOUT) {
1179 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1180 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1181 BUS_DMASYNC_POSTWRITE);
1183 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1184 BUS_DMASYNC_POSTWRITE);
1188 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1189 bus_dmamap_unload(sc->twe_immediate_dmat, sc->twe_immediate_map);
1191 bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_dmamap);
1195 /* free alignment buffer if it was used */
1196 if (tr->tr_flags & TWE_CMD_ALIGNBUF) {
1197 kfree(tr->tr_data, TWE_MALLOC_CLASS);
1198 tr->tr_data = tr->tr_realdata; /* restore 'real' data pointer */
1203 void twe_report(void);
1204 /********************************************************************************
1205 * Print current controller status, call from DDB.
1210 struct twe_softc *sc;
1213 for (i = 0; (sc = devclass_get_softc(twe_devclass, i)) != NULL; i++)
1214 twe_print_controller(sc);
1215 kprintf("twed: total bio count in %u out %u\n", twed_bio_in, twed_bio_out);