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_ctx_init(&sc->sysctl_ctx);
200 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
201 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
202 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
203 if (sc->sysctl_tree == NULL) {
204 twe_printf(sc, "cannot add sysctl tree node\n");
207 SYSCTL_ADD_STRING(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
208 OID_AUTO, "driver_version", CTLFLAG_RD, TWE_DRIVER_VERSION_STRING, 0,
209 "TWE driver version");
212 * Force the busmaster enable bit on, in case the BIOS forgot.
214 pci_enable_busmaster(dev);
217 * Allocate the PCI register window.
219 rid = TWE_IO_CONFIG_REG;
220 if ((sc->twe_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
221 RF_ACTIVE)) == NULL) {
222 twe_printf(sc, "can't allocate register window\n");
228 * Allocate the parent bus DMA tag appropriate for PCI.
230 if (bus_dma_tag_create(NULL, /* parent */
231 1, 0, /* alignment, boundary */
232 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
233 BUS_SPACE_MAXADDR, /* highaddr */
234 NULL, NULL, /* filter, filterarg */
235 MAXBSIZE, TWE_MAX_SGL_LENGTH, /* maxsize, nsegments */
236 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
238 &sc->twe_parent_dmat)) {
239 twe_printf(sc, "can't allocate parent DMA tag\n");
245 * Allocate and connect our interrupt.
248 if ((sc->twe_irq = bus_alloc_resource_any(sc->twe_dev, SYS_RES_IRQ,
249 &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
250 twe_printf(sc, "can't allocate interrupt\n");
254 if (bus_setup_intr(sc->twe_dev, sc->twe_irq, INTR_MPSAFE,
255 twe_pci_intr, sc, &sc->twe_intr, NULL)) {
256 twe_printf(sc, "can't set up interrupt\n");
262 * Create DMA tag for mapping command's into controller-addressable space.
264 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
265 1, 0, /* alignment, boundary */
266 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
267 BUS_SPACE_MAXADDR, /* highaddr */
268 NULL, NULL, /* filter, filterarg */
269 sizeof(TWE_Command) *
270 TWE_Q_LENGTH, 1, /* maxsize, nsegments */
271 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
273 &sc->twe_cmd_dmat)) {
274 twe_printf(sc, "can't allocate data buffer DMA tag\n");
279 * Allocate memory and make it available for DMA.
281 if (bus_dmamem_alloc(sc->twe_cmd_dmat, (void **)&sc->twe_cmd,
282 BUS_DMA_NOWAIT, &sc->twe_cmdmap)) {
283 twe_printf(sc, "can't allocate command memory\n");
286 bus_dmamap_load(sc->twe_cmd_dmat, sc->twe_cmdmap, sc->twe_cmd,
287 sizeof(TWE_Command) * TWE_Q_LENGTH,
288 twe_setup_request_dmamap, sc, 0);
289 bzero(sc->twe_cmd, sizeof(TWE_Command) * TWE_Q_LENGTH);
292 * Create DMA tag for mapping objects into controller-addressable space.
294 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
295 1, 0, /* alignment, boundary */
296 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
297 BUS_SPACE_MAXADDR, /* highaddr */
298 NULL, NULL, /* filter, filterarg */
299 MAXBSIZE, TWE_MAX_SGL_LENGTH,/* maxsize, nsegments */
300 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
301 BUS_DMA_ALLOCNOW, /* flags */
302 &sc->twe_buffer_dmat)) {
303 twe_printf(sc, "can't allocate data buffer DMA tag\n");
309 * Create DMA tag for mapping objects into controller-addressable space.
311 if (bus_dma_tag_create(sc->twe_parent_dmat, /* parent */
312 1, 0, /* alignment, boundary */
313 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
314 BUS_SPACE_MAXADDR, /* highaddr */
315 NULL, NULL, /* filter, filterarg */
316 MAXBSIZE, 1, /* maxsize, nsegments */
317 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
319 &sc->twe_immediate_dmat)) {
320 twe_printf(sc, "can't allocate data buffer DMA tag\n");
325 * Allocate memory for requests which cannot sleep or support continuation.
327 if (bus_dmamem_alloc(sc->twe_immediate_dmat, (void **)&sc->twe_immediate,
328 BUS_DMA_NOWAIT, &sc->twe_immediate_map)) {
329 twe_printf(sc, "can't allocate memory for immediate requests\n");
334 * Initialise the controller and driver core.
336 if ((error = twe_setup(sc))) {
342 * Print some information about the controller and configuration.
344 twe_describe_controller(sc);
347 * Create the control device.
349 sc->twe_dev_t = make_dev(&twe_ops, device_get_unit(sc->twe_dev),
350 UID_ROOT, GID_OPERATOR,
351 S_IRUSR | S_IWUSR, "twe%d",
352 device_get_unit(sc->twe_dev));
353 sc->twe_dev_t->si_drv1 = sc;
356 * Schedule ourselves to bring the controller up once interrupts are
357 * available. This isn't strictly necessary, since we disable
358 * interrupts while probing the controller, but it is more in keeping
359 * with common practice for other disk devices.
361 sc->twe_ich.ich_func = twe_intrhook;
362 sc->twe_ich.ich_arg = sc;
363 sc->twe_ich.ich_desc = "twe";
364 if (config_intrhook_establish(&sc->twe_ich) != 0) {
365 twe_printf(sc, "can't establish configuration hook\n");
373 /********************************************************************************
374 * Free all of the resources associated with (sc).
376 * Should not be called if the controller is active.
379 twe_free(struct twe_softc *sc)
381 struct twe_request *tr;
385 /* throw away any command buffers */
386 while ((tr = twe_dequeue_free(sc)) != NULL)
387 twe_free_request(tr);
389 if (sc->twe_cmd != NULL) {
390 bus_dmamap_unload(sc->twe_cmd_dmat, sc->twe_cmdmap);
391 bus_dmamem_free(sc->twe_cmd_dmat, sc->twe_cmd, sc->twe_cmdmap);
394 if (sc->twe_immediate != NULL) {
395 bus_dmamap_unload(sc->twe_immediate_dmat, sc->twe_immediate_map);
396 bus_dmamem_free(sc->twe_immediate_dmat, sc->twe_immediate,
397 sc->twe_immediate_map);
400 if (sc->twe_immediate_dmat)
401 bus_dma_tag_destroy(sc->twe_immediate_dmat);
403 /* destroy the data-transfer DMA tag */
404 if (sc->twe_buffer_dmat)
405 bus_dma_tag_destroy(sc->twe_buffer_dmat);
407 /* disconnect the interrupt handler */
409 bus_teardown_intr(sc->twe_dev, sc->twe_irq, sc->twe_intr);
410 if (sc->twe_irq != NULL)
411 bus_release_resource(sc->twe_dev, SYS_RES_IRQ, 0, sc->twe_irq);
413 /* destroy the parent DMA tag */
414 if (sc->twe_parent_dmat)
415 bus_dma_tag_destroy(sc->twe_parent_dmat);
417 /* release the register window mapping */
418 if (sc->twe_io != NULL)
419 bus_release_resource(sc->twe_dev, SYS_RES_IOPORT, TWE_IO_CONFIG_REG, sc->twe_io);
421 /* destroy control device */
422 if (sc->twe_dev_t != NULL)
423 destroy_dev(sc->twe_dev_t);
424 dev_ops_remove_minor(&twe_ops, device_get_unit(sc->twe_dev));
426 sysctl_ctx_free(&sc->sysctl_ctx);
427 lockuninit(&sc->twe_config_lock);
428 lockuninit(&sc->twe_io_lock);
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);
442 if (sc->twe_state & TWE_STATE_OPEN) {
446 sc->twe_state |= TWE_STATE_DETACHING;
450 * Shut the controller down.
452 if (twe_shutdown(dev)) {
454 sc->twe_state &= ~TWE_STATE_DETACHING;
464 /********************************************************************************
465 * Bring the controller down to a dormant state and detach all child devices.
467 * Note that we can assume that the bioq on the controller is empty, as we won't
468 * allow shutdown if any device is open.
471 twe_shutdown(device_t dev)
473 struct twe_softc *sc = device_get_softc(dev);
479 * Delete all our child devices.
482 for (i = 0; i < TWE_MAX_UNITS; i++) {
483 if (sc->twe_drive[i].td_disk != 0) {
484 if ((error = twe_detach_drive(sc, i)) != 0) {
485 TWE_CONFIG_UNLOCK(sc);
490 TWE_CONFIG_UNLOCK(sc);
493 * Bring the controller down.
502 /********************************************************************************
503 * Bring the controller to a quiescent state, ready for system suspend.
506 twe_suspend(device_t dev)
508 struct twe_softc *sc = device_get_softc(dev);
513 sc->twe_state |= TWE_STATE_SUSPEND;
516 twe_disable_interrupts(sc);
522 /********************************************************************************
523 * Bring the controller back to a state ready for operation.
526 twe_resume(device_t dev)
528 struct twe_softc *sc = device_get_softc(dev);
533 sc->twe_state &= ~TWE_STATE_SUSPEND;
534 twe_enable_interrupts(sc);
540 /*******************************************************************************
541 * Take an interrupt, or be poked by other code to look for interrupt-worthy
545 twe_pci_intr(void *arg)
547 struct twe_softc *sc = arg;
554 /********************************************************************************
555 * Delayed-startup hook
558 twe_intrhook(void *arg)
560 struct twe_softc *sc = (struct twe_softc *)arg;
562 /* pull ourselves off the intrhook chain */
563 config_intrhook_disestablish(&sc->twe_ich);
565 /* call core startup routine */
569 /********************************************************************************
570 * Given a detected drive, attach it to the bio interface.
572 * This is called from twe_add_unit.
575 twe_attach_drive(struct twe_softc *sc, struct twe_drive *dr)
581 dr->td_disk = device_add_child(sc->twe_dev, NULL, -1);
582 if (dr->td_disk == NULL) {
584 twe_printf(sc, "Cannot add unit\n");
587 device_set_ivars(dr->td_disk, dr);
590 * XXX It would make sense to test the online/initialising bits, but they seem to be
593 ksprintf(buf, "Unit %d, %s, %s",
595 twe_describe_code(twe_table_unittype, dr->td_type),
596 twe_describe_code(twe_table_unitstate, dr->td_state & TWE_PARAM_UNITSTATUS_MASK));
597 device_set_desc_copy(dr->td_disk, buf);
599 error = device_probe_and_attach(dr->td_disk);
602 twe_printf(sc, "Cannot attach unit to controller. error = %d\n", error);
608 /********************************************************************************
609 * Detach the specified unit if it exsists
611 * This is called from twe_del_unit.
614 twe_detach_drive(struct twe_softc *sc, int unit)
618 TWE_CONFIG_ASSERT_LOCKED(sc);
620 error = device_delete_child(sc->twe_dev, sc->twe_drive[unit].td_disk);
623 twe_printf(sc, "failed to delete unit %d\n", unit);
626 bzero(&sc->twe_drive[unit], sizeof(sc->twe_drive[unit]));
630 /********************************************************************************
631 * Clear a PCI parity error.
634 twe_clear_pci_parity_error(struct twe_softc *sc)
636 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PARITY_ERROR);
637 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PARITY_ERROR, 2);
640 /********************************************************************************
644 twe_clear_pci_abort(struct twe_softc *sc)
646 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PCI_ABORT);
647 pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PCI_ABORT, 2);
650 /********************************************************************************
651 ********************************************************************************
653 ********************************************************************************
654 ********************************************************************************/
657 * Disk device bus interface
659 static int twed_probe(device_t dev);
660 static int twed_attach(device_t dev);
661 static int twed_detach(device_t dev);
663 static device_method_t twed_methods[] = {
664 DEVMETHOD(device_probe, twed_probe),
665 DEVMETHOD(device_attach, twed_attach),
666 DEVMETHOD(device_detach, twed_detach),
670 static driver_t twed_driver = {
673 sizeof(struct twed_softc)
676 static devclass_t twed_devclass;
677 DRIVER_MODULE(twed, twe, twed_driver, twed_devclass, NULL, NULL);
680 * Disk device control interface.
682 static d_open_t twed_open;
683 static d_close_t twed_close;
684 static d_strategy_t twed_strategy;
685 static d_dump_t twed_dump;
687 static struct dev_ops twed_ops = {
688 { "twed", 0, D_DISK | D_MPSAFE},
690 .d_close = twed_close,
692 .d_write = physwrite,
693 .d_strategy = twed_strategy,
697 /********************************************************************************
698 * Handle open from generic layer.
700 * Note that this is typically only called by the diskslice code, and not
701 * for opens on subdevices (eg. slices, partitions).
704 twed_open(struct dev_open_args *ap)
706 cdev_t dev = ap->a_head.a_dev;
707 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1;
714 /* check that the controller is up and running */
715 if (sc->twed_controller->twe_state & TWE_STATE_SHUTDOWN)
718 sc->twed_flags |= TWED_OPEN;
722 /********************************************************************************
723 * Handle last close of the disk device.
726 twed_close(struct dev_close_args *ap)
728 cdev_t dev = ap->a_head.a_dev;
729 struct twed_softc *sc = (struct twed_softc *)dev->si_drv1;
736 sc->twed_flags &= ~TWED_OPEN;
740 /********************************************************************************
741 * Handle an I/O request.
744 twed_strategy(struct dev_strategy_args *ap)
746 cdev_t dev = ap->a_head.a_dev;
747 struct bio *bio = ap->a_bio;
748 struct twed_softc *sc = dev->si_drv1;
749 struct buf *bp = bio->bio_buf;
751 bio->bio_driver_info = sc;
758 if (sc == NULL || sc->twed_drive->td_disk == NULL) {
759 bp->b_error = EINVAL;
760 bp->b_flags |= B_ERROR;
761 kprintf("twe: bio for invalid disk!\n");
767 /* perform accounting */
768 devstat_start_transaction(&sc->twed_stats);
770 /* queue the bio on the controller */
771 TWE_IO_LOCK(sc->twed_controller);
772 twe_enqueue_bio(sc->twed_controller, bio);
774 /* poke the controller to start I/O */
775 twe_startio(sc->twed_controller);
776 TWE_IO_UNLOCK(sc->twed_controller);
780 /********************************************************************************
781 * System crashdump support
784 twed_dump(struct dev_dump_args *ap)
786 cdev_t dev = ap->a_head.a_dev;
787 size_t length = ap->a_length;
788 off_t offset = ap->a_offset;
789 void *virtual = ap->a_virtual;
790 struct twed_softc *twed_sc;
791 struct twe_softc *twe_sc;
794 twed_sc = dev->si_drv1;
797 twe_sc = (struct twe_softc *)twed_sc->twed_controller;
800 if ((error = twe_dump_blocks(twe_sc, twed_sc->twed_drive->td_twe_unit, offset / TWE_BLOCK_SIZE, virtual, length / TWE_BLOCK_SIZE)) != 0)
806 /********************************************************************************
807 * Handle completion of an I/O request.
810 twed_intr(struct bio *bio)
812 struct buf *bp = bio->bio_buf;
813 struct twed_softc *sc = bio->bio_driver_info;
817 /* if no error, transfer completed */
818 if (!(bp->b_flags & B_ERROR))
820 devstat_end_transaction_buf(&sc->twed_stats, bp);
825 /********************************************************************************
826 * Default probe stub.
829 twed_probe(device_t dev)
834 /********************************************************************************
835 * Attach a unit to the controller.
838 twed_attach(device_t dev)
840 struct twed_softc *sc;
841 struct disk_info info;
847 /* initialise our softc */
848 sc = device_get_softc(dev);
849 parent = device_get_parent(dev);
850 sc->twed_controller = (struct twe_softc *)device_get_softc(parent);
851 sc->twed_drive = device_get_ivars(dev);
854 /* report the drive */
855 twed_printf(sc, "%uMB (%u sectors)\n",
856 sc->twed_drive->td_size / ((1024 * 1024) / TWE_BLOCK_SIZE),
857 sc->twed_drive->td_size);
859 /* attach a generic disk device to ourselves */
861 sc->twed_drive->td_sys_unit = device_get_unit(dev);
863 devstat_add_entry(&sc->twed_stats, "twed", sc->twed_drive->td_sys_unit,
865 DEVSTAT_NO_ORDERED_TAGS,
866 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
867 DEVSTAT_PRIORITY_ARRAY);
869 dsk = disk_create(sc->twed_drive->td_sys_unit, &sc->twed_disk, &twed_ops);
871 sc->twed_dev_t = dsk;
873 /* set the maximum I/O size to the theoretical maximum allowed by the S/G list size */
874 dsk->si_iosize_max = (TWE_MAX_SGL_LENGTH - 1) * PAGE_SIZE;
877 * Set disk info, as it appears that all needed data is available already.
878 * Setting the disk info will also cause the probing to start.
880 bzero(&info, sizeof(info));
881 info.d_media_blksize = TWE_BLOCK_SIZE; /* mandatory */
882 info.d_media_blocks = sc->twed_drive->td_size;
884 info.d_type = DTYPE_ESDI; /* optional */
885 info.d_secpertrack = sc->twed_drive->td_sectors;
886 info.d_nheads = sc->twed_drive->td_heads;
887 info.d_ncylinders = sc->twed_drive->td_cylinders;
888 info.d_secpercyl = sc->twed_drive->td_sectors * sc->twed_drive->td_heads;
890 disk_setdiskinfo(&sc->twed_disk, &info);
895 /********************************************************************************
896 * Disconnect ourselves from the system.
899 twed_detach(device_t dev)
901 struct twed_softc *sc = (struct twed_softc *)device_get_softc(dev);
905 if (sc->twed_flags & TWED_OPEN)
908 devstat_remove_entry(&sc->twed_stats);
909 disk_destroy(&sc->twed_disk);
914 /********************************************************************************
915 ********************************************************************************
917 ********************************************************************************
918 ********************************************************************************/
920 /********************************************************************************
921 * Allocate a command buffer
923 static MALLOC_DEFINE(TWE_MALLOC_CLASS, "twe_commands", "twe commands");
926 twe_allocate_request(struct twe_softc *sc, int tag)
928 struct twe_request *tr;
932 * TWE requires requests to be 512-byte aligned. Depend on malloc()
933 * guarenteeing alignment for power-of-2 requests. Note that the old
934 * (FreeBSD-4.x) malloc code aligned all requests, but the new slab
935 * allocator only guarentees same-size alignment for power-of-2 requests.
937 aligned_size = (sizeof(struct twe_request) + TWE_ALIGNMASK) &
939 tr = kmalloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT | M_ZERO);
942 if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_dmamap)) {
943 twe_free_request(tr);
944 twe_printf(sc, "unable to allocate dmamap for tag %d\n", tag);
950 /********************************************************************************
951 * Permanently discard a command buffer.
954 twe_free_request(struct twe_request *tr)
956 struct twe_softc *sc = tr->tr_sc;
960 bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_dmamap);
961 kfree(tr, TWE_MALLOC_CLASS);
964 /********************************************************************************
965 * Map/unmap (tr)'s command and data in the controller's addressable space.
967 * These routines ensure that the data which the controller is going to try to
968 * access is actually visible to the controller, in a machine-independant
969 * fashion. Due to a hardware limitation, I/O buffers must be 512-byte aligned
970 * and we take care of that here as well.
973 twe_fillin_sgl(TWE_SG_Entry *sgl, bus_dma_segment_t *segs, int nsegments, int max_sgl)
977 for (i = 0; i < nsegments; i++) {
978 sgl[i].address = segs[i].ds_addr;
979 sgl[i].length = segs[i].ds_len;
981 for (; i < max_sgl; i++) { /* XXX necessary? */
988 twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
990 struct twe_request *tr = (struct twe_request *)arg;
991 struct twe_softc *sc = tr->tr_sc;
992 TWE_Command *cmd = TWE_FIND_COMMAND(tr);
996 if (tr->tr_flags & TWE_CMD_MAPPED)
997 panic("already mapped command");
999 tr->tr_flags |= TWE_CMD_MAPPED;
1001 if (tr->tr_flags & TWE_CMD_IN_PROGRESS)
1002 sc->twe_state &= ~TWE_STATE_FRZN;
1003 /* save base of first segment in command (applicable if there only one segment) */
1004 tr->tr_dataphys = segs[0].ds_addr;
1006 /* correct command size for s/g list size */
1007 cmd->generic.size += 2 * nsegments;
1010 * Due to the fact that parameter and I/O commands have the scatter/gather list in
1011 * different places, we need to determine which sort of command this actually is
1012 * before we can populate it correctly.
1014 switch(cmd->generic.opcode) {
1015 case TWE_OP_GET_PARAM:
1016 case TWE_OP_SET_PARAM:
1017 cmd->generic.sgl_offset = 2;
1018 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1022 cmd->generic.sgl_offset = 3;
1023 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1025 case TWE_OP_ATA_PASSTHROUGH:
1026 cmd->generic.sgl_offset = 5;
1027 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1031 * Fall back to what the linux driver does.
1032 * Do this because the API may send an opcode
1033 * the driver knows nothing about and this will
1034 * at least stop PCIABRT's from hosing us.
1036 switch (cmd->generic.sgl_offset) {
1038 twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1041 twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1044 twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1049 if (tr->tr_flags & TWE_CMD_DATAIN) {
1050 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1051 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1052 BUS_DMASYNC_PREREAD);
1054 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1055 BUS_DMASYNC_PREREAD);
1059 if (tr->tr_flags & TWE_CMD_DATAOUT) {
1061 * if we're using an alignment buffer, and we're writing data
1062 * copy the real data out
1064 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1065 bcopy(tr->tr_realdata, tr->tr_data, tr->tr_length);
1067 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1068 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1069 BUS_DMASYNC_PREWRITE);
1071 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1072 BUS_DMASYNC_PREWRITE);
1076 if (twe_start(tr) == EBUSY) {
1077 tr->tr_sc->twe_state |= TWE_STATE_CTLR_BUSY;
1078 twe_requeue_ready(tr);
1083 twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1085 struct twe_softc *sc = (struct twe_softc *)arg;
1089 /* command can't cross a page boundary */
1090 sc->twe_cmdphys = segs[0].ds_addr;
1094 twe_map_request(struct twe_request *tr)
1096 struct twe_softc *sc = tr->tr_sc;
1101 twe_lockassert(&sc->twe_io_lock);
1102 if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN)) {
1103 twe_requeue_ready(tr);
1107 bus_dmamap_sync(sc->twe_cmd_dmat, sc->twe_cmdmap, BUS_DMASYNC_PREWRITE);
1110 * If the command involves data, map that too.
1112 if (tr->tr_data != NULL && ((tr->tr_flags & TWE_CMD_MAPPED) == 0)) {
1115 * Data must be 512-byte aligned; allocate a fixup buffer if it's not.
1117 * DragonFly's malloc only guarentees alignment for requests which
1118 * are power-of-2 sized.
1120 if (((vm_offset_t)tr->tr_data % TWE_ALIGNMENT) != 0) {
1123 tr->tr_realdata = tr->tr_data; /* save pointer to 'real' data */
1124 aligned_size = TWE_ALIGNMENT;
1125 while (aligned_size < tr->tr_length)
1127 tr->tr_flags |= TWE_CMD_ALIGNBUF;
1128 tr->tr_data = kmalloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT);
1129 if (tr->tr_data == NULL) {
1130 twe_printf(sc, "%s: malloc failed\n", __func__);
1131 tr->tr_data = tr->tr_realdata; /* restore original data pointer */
1137 * Map the data buffer into bus space and build the s/g list.
1139 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1140 error = bus_dmamap_load(sc->twe_immediate_dmat, sc->twe_immediate_map, sc->twe_immediate,
1141 tr->tr_length, twe_setup_data_dmamap, tr, BUS_DMA_NOWAIT);
1143 error = bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_dmamap, tr->tr_data, tr->tr_length,
1144 twe_setup_data_dmamap, tr, 0);
1146 if (error == EINPROGRESS) {
1147 tr->tr_flags |= TWE_CMD_IN_PROGRESS;
1148 sc->twe_state |= TWE_STATE_FRZN;
1152 if ((error = twe_start(tr)) == EBUSY) {
1153 sc->twe_state |= TWE_STATE_CTLR_BUSY;
1154 twe_requeue_ready(tr);
1161 twe_unmap_request(struct twe_request *tr)
1163 struct twe_softc *sc = tr->tr_sc;
1167 bus_dmamap_sync(sc->twe_cmd_dmat, sc->twe_cmdmap, BUS_DMASYNC_POSTWRITE);
1170 * If the command involved data, unmap that too.
1172 if (tr->tr_data != NULL) {
1173 if (tr->tr_flags & TWE_CMD_DATAIN) {
1174 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1175 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1176 BUS_DMASYNC_POSTREAD);
1178 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1179 BUS_DMASYNC_POSTREAD);
1182 /* if we're using an alignment buffer, and we're reading data, copy the real data in */
1183 if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1184 bcopy(tr->tr_data, tr->tr_realdata, tr->tr_length);
1186 if (tr->tr_flags & TWE_CMD_DATAOUT) {
1187 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1188 bus_dmamap_sync(sc->twe_immediate_dmat, sc->twe_immediate_map,
1189 BUS_DMASYNC_POSTWRITE);
1191 bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap,
1192 BUS_DMASYNC_POSTWRITE);
1196 if (tr->tr_flags & TWE_CMD_IMMEDIATE) {
1197 bus_dmamap_unload(sc->twe_immediate_dmat, sc->twe_immediate_map);
1199 bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_dmamap);
1203 /* free alignment buffer if it was used */
1204 if (tr->tr_flags & TWE_CMD_ALIGNBUF) {
1205 kfree(tr->tr_data, TWE_MALLOC_CLASS);
1206 tr->tr_data = tr->tr_realdata; /* restore 'real' data pointer */
1211 void twe_report(void);
1212 /********************************************************************************
1213 * Print current controller status, call from DDB.
1218 struct twe_softc *sc;
1221 for (i = 0; (sc = devclass_get_softc(twe_devclass, i)) != NULL; i++)
1222 twe_print_controller(sc);
1223 kprintf("twed: total bio count in %u out %u\n", twed_bio_in, twed_bio_out);