2 * Copyright (c) 2003-04 3ware, Inc.
3 * Copyright (c) 2000 Michael Smith
4 * Copyright (c) 2000 BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $DragonFly: src/sys/dev/raid/twa/twa_freebsd.c,v 1.14 2006/12/22 23:26:24 swildner Exp $
33 * 3ware driver for 9000 series storage controllers.
35 * Author: Vinod Kashyap
39 #include "twa_includes.h"
41 static void twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs,
42 int nsegments, int error);
43 static void twa_setup_request_dmamap(void *arg, bus_dma_segment_t *segs,
44 int nsegments, int error);
46 MALLOC_DEFINE(TWA_MALLOC_CLASS, "twa commands", "twa commands");
49 static d_open_t twa_open;
50 static d_close_t twa_close;
51 static d_ioctl_t twa_ioctl_wrapper;
53 static struct dev_ops twa_ops = {
54 { "twa", TWA_CDEV_MAJOR, 0 },
57 .d_ioctl = twa_ioctl_wrapper,
60 static devclass_t twa_devclass;
64 * Function name: twa_open
65 * Description: Called when the controller is opened.
66 * Simply marks the controller as open.
68 * Input: dev -- control device corresponding to the ctlr
69 * flags -- mode of open
70 * fmt -- device type (character/block etc.)
71 * proc -- current process
73 * Return value: 0 -- success
77 twa_open(struct dev_open_args *ap)
79 cdev_t dev = ap->a_head.a_dev;
80 int unit = minor(dev);
81 struct twa_softc *sc = devclass_get_softc(twa_devclass, unit);
83 sc->twa_state |= TWA_STATE_OPEN;
90 * Function name: twa_close
91 * Description: Called when the controller is closed.
92 * Simply marks the controller as not open.
94 * Input: dev -- control device corresponding to the ctlr
95 * flags -- mode of corresponding open
96 * fmt -- device type (character/block etc.)
97 * proc -- current process
99 * Return value: 0 -- success
103 twa_close(struct dev_close_args *ap)
105 cdev_t dev = ap->a_head.a_dev;
106 int unit = minor(dev);
107 struct twa_softc *sc = devclass_get_softc(twa_devclass, unit);
109 sc->twa_state &= ~TWA_STATE_OPEN;
116 * Function name: twa_ioctl_wrapper
117 * Description: Called when an ioctl is posted to the controller.
118 * Simply calls the ioctl handler.
120 * Input: dev -- control device corresponding to the ctlr
122 * buf -- ptr to buffer in kernel memory, which is
123 * a copy of the input buffer in user-space
124 * flags -- mode of corresponding open
125 * proc -- current process
126 * Output: buf -- ptr to buffer in kernel memory, which will
127 * be copied to the output buffer in user-space
128 * Return value: 0 -- success
132 twa_ioctl_wrapper(struct dev_ioctl_args *ap)
134 cdev_t dev = ap->a_head.a_dev;
135 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
137 return(twa_ioctl(sc, ap->a_cmd, ap->a_data));
142 static int twa_probe (device_t dev);
143 static int twa_attach (device_t dev);
144 static void twa_free (struct twa_softc *sc);
145 static int twa_detach (device_t dev);
146 static int twa_shutdown (device_t dev);
147 static int twa_suspend (device_t dev);
148 static int twa_resume (device_t dev);
149 static void twa_pci_intr(void *arg);
150 static void twa_intrhook (void *arg);
152 static device_method_t twa_methods[] = {
153 /* Device interface */
154 DEVMETHOD(device_probe, twa_probe),
155 DEVMETHOD(device_attach, twa_attach),
156 DEVMETHOD(device_detach, twa_detach),
157 DEVMETHOD(device_shutdown, twa_shutdown),
158 DEVMETHOD(device_suspend, twa_suspend),
159 DEVMETHOD(device_resume, twa_resume),
161 DEVMETHOD(bus_print_child, bus_generic_print_child),
162 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
166 static driver_t twa_pci_driver = {
169 sizeof(struct twa_softc)
172 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
177 * Function name: twa_probe
178 * Description: Called at driver load time. Claims 9000 ctlrs.
180 * Input: dev -- bus device corresponding to the ctlr
182 * Return value: <= 0 -- success
186 twa_probe(device_t dev)
188 static u_int8_t first_ctlr = 1;
190 twa_dbg_print(3, "entered");
192 if ((pci_get_vendor(dev) == TWA_VENDOR_ID) &&
193 (pci_get_device(dev) == TWA_DEVICE_ID_9K)) {
194 device_set_desc(dev, TWA_DEVICE_NAME);
195 /* Print the driver version only once. */
197 kprintf("3ware device driver for 9000 series storage controllers, version: %s\n",
198 TWA_DRIVER_VERSION_STRING);
209 * Function name: twa_attach
210 * Description: Allocates pci resources; updates sc; adds a node to the
211 * sysctl tree to expose the driver version; makes calls
212 * to initialize ctlr, and to attach to CAM.
214 * Input: dev -- bus device corresponding to the ctlr
216 * Return value: 0 -- success
220 twa_attach(device_t dev)
222 struct twa_softc *sc = device_get_softc(dev);
228 twa_dbg_dprint_enter(3, sc);
230 /* Initialize the softc structure. */
231 sc->twa_bus_dev = dev;
233 sysctl_ctx_init(&sc->twa_sysctl_ctx);
234 sc->twa_sysctl_tree = SYSCTL_ADD_NODE(&sc->twa_sysctl_ctx,
235 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
236 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
237 if (sc->twa_sysctl_tree == NULL) {
238 twa_printf(sc, "Cannot add sysctl tree node.\n");
241 SYSCTL_ADD_STRING(&sc->twa_sysctl_ctx, SYSCTL_CHILDREN(sc->twa_sysctl_tree),
242 OID_AUTO, "driver_version", CTLFLAG_RD,
243 TWA_DRIVER_VERSION_STRING, 0, "TWA driver version");
245 /* Make sure we are going to be able to talk to this board. */
246 command = pci_read_config(dev, PCIR_COMMAND, 2);
247 if ((command & PCIM_CMD_PORTEN) == 0) {
248 twa_printf(sc, "Register window not available.\n");
252 /* Force the busmaster enable bit on, in case the BIOS forgot. */
253 command |= PCIM_CMD_BUSMASTEREN;
254 pci_write_config(dev, PCIR_COMMAND, command, 2);
256 /* Allocate the PCI register window. */
257 res_id = TWA_IO_CONFIG_REG;
258 if ((sc->twa_io_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &res_id,
259 0, ~0, 1, RF_ACTIVE)) == NULL) {
260 twa_printf(sc, "can't allocate register window.\n");
264 sc->twa_bus_tag = rman_get_bustag(sc->twa_io_res);
265 sc->twa_bus_handle = rman_get_bushandle(sc->twa_io_res);
267 /* Allocate and connect our interrupt. */
269 if ((sc->twa_irq_res = bus_alloc_resource(sc->twa_bus_dev, SYS_RES_IRQ,
271 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
272 twa_printf(sc, "Can't allocate interrupt.\n");
276 if (bus_setup_intr(sc->twa_bus_dev, sc->twa_irq_res, 0,
277 twa_pci_intr, sc, &sc->twa_intr_handle, NULL)) {
278 twa_printf(sc, "Can't set up interrupt.\n");
283 /* Initialize the driver for this controller. */
284 if ((error = twa_setup(sc))) {
289 /* Print some information about the controller and configuration. */
290 twa_describe_controller(sc);
292 /* Create the control device. */
293 xdev = make_dev(&twa_ops, device_get_unit(sc->twa_bus_dev),
294 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
295 "twa%d", device_get_unit(sc->twa_bus_dev));
299 * Schedule ourselves to bring the controller up once interrupts are
300 * available. This isn't strictly necessary, since we disable
301 * interrupts while probing the controller, but it is more in keeping
302 * with common practice for other disk devices.
304 sc->twa_ich.ich_func = twa_intrhook;
305 sc->twa_ich.ich_arg = sc;
306 sc->twa_ich.ich_desc = "twa";
307 if (config_intrhook_establish(&sc->twa_ich) != 0) {
308 twa_printf(sc, "Can't establish configuration hook.\n");
313 if ((error = twa_cam_setup(sc))) {
323 * Function name: twa_free
324 * Description: Performs clean-up at the time of going down.
326 * Input: sc -- ptr to per ctlr structure
331 twa_free(struct twa_softc *sc)
333 struct twa_request *tr;
335 twa_dbg_dprint_enter(3, sc);
337 /* Detach from CAM */
340 /* Destroy dma handles. */
342 bus_dmamap_unload(sc->twa_dma_tag, sc->twa_cmd_map);
343 while ((tr = twa_dequeue_free(sc)) != NULL)
344 bus_dmamap_destroy(sc->twa_dma_tag, tr->tr_dma_map);
346 /* Free all memory allocated so far. */
348 kfree(sc->twa_req_buf, TWA_MALLOC_CLASS);
349 if (sc->twa_cmd_pkt_buf)
350 bus_dmamem_free(sc->twa_dma_tag, sc->twa_cmd_pkt_buf,
352 if (sc->twa_aen_queue[0])
353 kfree (sc->twa_aen_queue[0], M_DEVBUF);
355 /* Destroy the data-transfer DMA tag. */
357 bus_dma_tag_destroy(sc->twa_dma_tag);
359 /* Disconnect the interrupt handler. */
360 if (sc->twa_intr_handle)
361 bus_teardown_intr(sc->twa_bus_dev, sc->twa_irq_res,
362 sc->twa_intr_handle);
363 if (sc->twa_irq_res != NULL)
364 bus_release_resource(sc->twa_bus_dev, SYS_RES_IRQ,
367 /* Release the register window mapping. */
368 if (sc->twa_io_res != NULL)
369 bus_release_resource(sc->twa_bus_dev, SYS_RES_IOPORT,
370 TWA_IO_CONFIG_REG, sc->twa_io_res);
372 dev_ops_remove_minor(&twa_ops, device_get_unit(sc->twa_bus_dev));
374 sysctl_ctx_free(&sc->twa_sysctl_ctx);
380 * Function name: twa_detach
381 * Description: Called when the controller is being detached from
384 * Input: dev -- bus device corresponding to the ctlr
386 * Return value: 0 -- success
390 twa_detach(device_t dev)
392 struct twa_softc *sc = device_get_softc(dev);
395 twa_dbg_dprint_enter(3, sc);
399 if (sc->twa_state & TWA_STATE_OPEN)
402 /* Shut the controller down. */
403 if ((error = twa_shutdown(dev)))
406 /* Free all resources associated with this controller. */
418 * Function name: twa_shutdown
419 * Description: Called at unload/shutdown time. Lets the controller
420 * know that we are going down.
422 * Input: dev -- bus device corresponding to the ctlr
424 * Return value: 0 -- success
428 twa_shutdown(device_t dev)
430 struct twa_softc *sc = device_get_softc(dev);
433 twa_dbg_dprint_enter(3, sc);
437 /* Disconnect from the controller. */
438 error = twa_deinit_ctlr(sc);
447 * Function name: twa_suspend
448 * Description: Called to suspend I/O before hot-swapping PCI ctlrs.
449 * Doesn't do much as of now.
451 * Input: dev -- bus device corresponding to the ctlr
453 * Return value: 0 -- success
457 twa_suspend(device_t dev)
459 struct twa_softc *sc = device_get_softc(dev);
461 twa_dbg_dprint_enter(3, sc);
464 sc->twa_state |= TWA_STATE_SUSPEND;
466 twa_disable_interrupts(sc);
475 * Function name: twa_resume
476 * Description: Called to resume I/O after hot-swapping PCI ctlrs.
477 * Doesn't do much as of now.
479 * Input: dev -- bus device corresponding to the ctlr
481 * Return value: 0 -- success
485 twa_resume(device_t dev)
487 struct twa_softc *sc = device_get_softc(dev);
489 twa_dbg_dprint_enter(3, sc);
491 sc->twa_state &= ~TWA_STATE_SUSPEND;
492 twa_enable_interrupts(sc);
500 * Function name: twa_pci_intr
501 * Description: Interrupt handler. Wrapper for twa_interrupt.
503 * Input: arg -- ptr to per ctlr structure
508 twa_pci_intr(void *arg)
510 struct twa_softc *sc = (struct twa_softc *)arg;
518 * Function name: twa_intrhook
519 * Description: Callback for us to enable interrupts.
521 * Input: arg -- ptr to per ctlr structure
526 twa_intrhook(void *arg)
528 struct twa_softc *sc = (struct twa_softc *)arg;
530 twa_dbg_dprint(4, sc, "twa_intrhook Entered");
532 /* Pull ourselves off the intrhook chain. */
533 config_intrhook_disestablish(&sc->twa_ich);
535 /* Enable interrupts. */
536 twa_enable_interrupts(sc);
542 * Function name: twa_write_pci_config
543 * Description: Writes to the PCI config space.
545 * Input: sc -- ptr to per ctlr structure
546 * value -- value to be written
547 * size -- # of bytes to be written
552 twa_write_pci_config(struct twa_softc *sc, u_int32_t value, int size)
554 pci_write_config(sc->twa_bus_dev, PCIR_STATUS, value, size);
560 * Function name: twa_alloc_req_pkts
561 * Description: Allocates memory for, and initializes request pkts,
562 * and queues them in the free queue.
564 * Input: sc -- ptr to per ctlr structure
565 * num_reqs-- # of request pkts to allocate and initialize.
567 * Return value: 0 -- success
571 twa_alloc_req_pkts(struct twa_softc *sc, int num_reqs)
573 struct twa_request *tr;
576 sc->twa_req_buf = kmalloc(num_reqs * sizeof(struct twa_request),
577 TWA_MALLOC_CLASS, M_INTWAIT);
579 /* Allocate the bus DMA tag appropriate for PCI. */
580 if (bus_dma_tag_create(NULL, /* parent */
581 TWA_ALIGNMENT, /* alignment */
583 BUS_SPACE_MAXADDR, /* lowaddr */
584 BUS_SPACE_MAXADDR, /* highaddr */
585 NULL, NULL, /* filter, filterarg */
587 (sizeof(struct twa_command_packet)),/* maxsize */
588 TWA_MAX_SG_ELEMENTS, /* nsegments */
589 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
590 BUS_DMA_ALLOCNOW, /* flags */
591 &sc->twa_dma_tag /* tag */)) {
592 twa_printf(sc, "Can't allocate DMA tag.\n");
596 /* Allocate memory for cmd pkts. */
597 if (bus_dmamem_alloc(sc->twa_dma_tag,
598 (void *)(&(sc->twa_cmd_pkt_buf)),
599 BUS_DMA_WAITOK, &(sc->twa_cmd_map)))
602 bus_dmamap_load(sc->twa_dma_tag, sc->twa_cmd_map,
604 num_reqs * sizeof(struct twa_command_packet),
605 twa_setup_request_dmamap, sc, 0);
606 bzero(sc->twa_req_buf, num_reqs * sizeof(struct twa_request));
607 bzero(sc->twa_cmd_pkt_buf,
608 num_reqs * sizeof(struct twa_command_packet));
610 for (i = 0; i < num_reqs; i++) {
611 tr = &(sc->twa_req_buf[i]);
612 tr->tr_command = &(sc->twa_cmd_pkt_buf[i]);
613 tr->tr_cmd_phys = sc->twa_cmd_pkt_phys +
614 (i * sizeof(struct twa_command_packet));
615 tr->tr_request_id = i;
617 sc->twa_lookup[i] = tr;
620 * Create a map for data buffers. maxsize (256 * 1024) used in
621 * bus_dma_tag_create above should suffice the bounce page needs
622 * for data buffers, since the max I/O size we support is 128KB.
623 * If we supported I/O's bigger than 256KB, we would have to
624 * create a second dma_tag, with the appropriate maxsize.
626 if (bus_dmamap_create(sc->twa_dma_tag, 0,
630 /* Insert request into the free queue. */
631 twa_release_request(tr);
639 * Function name: twa_fillin_sgl
640 * Description: Fills in the scatter/gather list.
642 * Input: sgl -- ptr to sg list
643 * segs -- ptr to fill the sg list from
644 * nsegments--# of segments
649 twa_fillin_sgl(struct twa_sg *sgl, bus_dma_segment_t *segs, int nsegments)
653 for (i = 0; i < nsegments; i++) {
654 sgl[i].address = segs[i].ds_addr;
655 sgl[i].length = segs[i].ds_len;
662 * Function name: twa_setup_data_dmamap
663 * Description: Callback of bus_dmamap_load for the buffer associated
664 * with data. Updates the cmd pkt (size/sgl_entries
665 * fields, as applicable) to reflect the number of sg
668 * Input: arg -- ptr to request pkt
669 * segs -- ptr to a list of segment descriptors
670 * nsegments--# of segments
671 * error -- 0 if no errors encountered before callback,
672 * non-zero if errors were encountered
677 twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs,
678 int nsegments, int error)
680 struct twa_request *tr = (struct twa_request *)arg;
681 struct twa_command_packet *cmdpkt = tr->tr_command;
682 struct twa_command_9k *cmd9k;
683 union twa_command_7k *cmd7k;
686 twa_dbg_dprint_enter(10, tr->tr_sc);
688 if ((tr->tr_flags & TWA_CMD_IN_PROGRESS) &&
689 (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL))
690 twa_allow_new_requests(tr->tr_sc, (void *)(tr->tr_private));
692 if (error == EFBIG) {
693 tr->tr_error = error;
697 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
698 cmd9k = &(cmdpkt->command.cmd_pkt_9k);
699 twa_fillin_sgl(&(cmd9k->sg_list[0]), segs, nsegments);
700 cmd9k->sgl_entries += nsegments - 1;
702 /* It's a 7000 command packet. */
703 cmd7k = &(cmdpkt->command.cmd_pkt_7k);
704 if ((sgl_offset = cmdpkt->command.cmd_pkt_7k.generic.sgl_offset))
705 twa_fillin_sgl((struct twa_sg *)
706 (((u_int32_t *)cmd7k) + sgl_offset),
708 /* Modify the size field, based on sg address size. */
709 cmd7k->generic.size +=
710 ((TWA_64BIT_ADDRESSES ? 3 : 2) * nsegments);
713 if (tr->tr_flags & TWA_CMD_DATA_IN)
714 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
715 BUS_DMASYNC_PREREAD);
716 if (tr->tr_flags & TWA_CMD_DATA_OUT) {
718 * If we're using an alignment buffer, and we're
719 * writing data, copy the real data out.
721 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
722 bcopy(tr->tr_real_data, tr->tr_data, tr->tr_real_length);
723 bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
724 BUS_DMASYNC_PREWRITE);
726 error = twa_submit_io(tr);
730 twa_unmap_request(tr);
732 * If the caller had been returned EINPROGRESS, and he has
733 * registered a callback for handling completion, the callback
734 * will never get called because we were unable to submit the
735 * request. So, free up the request right here.
737 if ((tr->tr_flags & TWA_CMD_IN_PROGRESS) && (tr->tr_callback))
738 twa_release_request(tr);
745 * Function name: twa_setup_request_dmamap
746 * Description: Callback of bus_dmamap_load for the buffer associated
749 * Input: arg -- ptr to request pkt
750 * segs -- ptr to a list of segment descriptors
751 * nsegments--# of segments
752 * error -- 0 if no errors encountered before callback,
753 * non-zero if errors were encountered
758 twa_setup_request_dmamap(void *arg, bus_dma_segment_t *segs,
759 int nsegments, int error)
761 struct twa_softc *sc = (struct twa_softc *)arg;
763 twa_dbg_dprint_enter(10, sc);
765 sc->twa_cmd_pkt_phys = segs[0].ds_addr;
771 * Function name: twa_map_request
772 * Description: Maps a cmd pkt and data associated with it, into
775 * Input: tr -- ptr to request pkt
777 * Return value: 0 -- success
781 twa_map_request(struct twa_request *tr)
783 struct twa_softc *sc = tr->tr_sc;
786 twa_dbg_dprint_enter(10, sc);
788 /* If the command involves data, map that too. */
789 if (tr->tr_data != NULL) {
791 * It's sufficient for the data pointer to be 4-byte aligned
792 * to work with 9000. However, if 4-byte aligned addresses
793 * are passed to bus_dmamap_load, we can get back sg elements
794 * that are not 512-byte multiples in size. So, we will let
795 * only those buffers that are 512-byte aligned to pass
796 * through, and bounce the rest, so as to make sure that we
797 * always get back sg elements that are 512-byte multiples
800 * DragonFly's malloc only guarentees X alignment when X is
801 * a power of 2, otherwise we would have to use contigalloc,
802 * which is nasty. Use malloc.
804 if (((vm_offset_t)tr->tr_data % 512) || (tr->tr_length % 512)) {
805 tr->tr_flags |= TWA_CMD_DATA_COPY_NEEDED;
806 tr->tr_real_data = tr->tr_data; /* save original data pointer */
807 tr->tr_real_length = tr->tr_length; /* save original data length */
809 while (tr->tr_length < tr->tr_real_length)
811 tr->tr_data = kmalloc(tr->tr_length, TWA_MALLOC_CLASS, M_INTWAIT);
815 * Map the data buffer into bus space and build the s/g list.
817 if ((error = bus_dmamap_load(sc->twa_dma_tag, tr->tr_dma_map,
818 tr->tr_data, tr->tr_length,
819 twa_setup_data_dmamap, tr,
821 if (error == EINPROGRESS) {
822 tr->tr_flags |= TWA_CMD_IN_PROGRESS;
823 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL)
824 twa_disallow_new_requests(sc);
827 /* Free alignment buffer if it was used. */
828 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
829 kfree(tr->tr_data, TWA_MALLOC_CLASS);
830 tr->tr_data = tr->tr_real_data; /* restore 'real' data pointer */
831 tr->tr_length = tr->tr_real_length;/* restore 'real' data length */
835 error = tr->tr_error;
838 if ((error = twa_submit_io(tr)))
839 twa_unmap_request(tr);
847 * Function name: twa_unmap_request
848 * Description: Undoes the mapping done by twa_map_request.
850 * Input: tr -- ptr to request pkt
855 twa_unmap_request(struct twa_request *tr)
857 struct twa_softc *sc = tr->tr_sc;
860 twa_dbg_dprint_enter(10, sc);
862 /* If the command involved data, unmap that too. */
863 if (tr->tr_data != NULL) {
864 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K)
865 cmd_status = tr->tr_command->command.cmd_pkt_9k.status;
867 cmd_status = tr->tr_command->command.cmd_pkt_7k.generic.status;
869 if (tr->tr_flags & TWA_CMD_DATA_IN) {
870 bus_dmamap_sync(sc->twa_dma_tag,
871 tr->tr_dma_map, BUS_DMASYNC_POSTREAD);
874 * If we are using a bounce buffer, and we are reading
875 * data, copy the real data in.
877 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
879 bcopy(tr->tr_data, tr->tr_real_data,
882 if (tr->tr_flags & TWA_CMD_DATA_OUT)
883 bus_dmamap_sync(sc->twa_dma_tag, tr->tr_dma_map,
884 BUS_DMASYNC_POSTWRITE);
886 bus_dmamap_unload(sc->twa_dma_tag, tr->tr_dma_map);
889 /* Free alignment buffer if it was used. */
890 if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
891 kfree(tr->tr_data, TWA_MALLOC_CLASS);
892 tr->tr_data = tr->tr_real_data; /* restore 'real' data pointer */
893 tr->tr_length = tr->tr_real_length;/* restore 'real' data length */
900 void twa_report(void);
901 void twa_reset_stats(void);
902 void twa_print_request(struct twa_request *tr, int req_type);
907 * Function name: twa_report
908 * Description: For being called from ddb. Prints controller stats,
909 * and requests, if any, that are in the wrong queue.
918 struct twa_softc *sc;
919 struct twa_request *tr;
923 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
924 twa_print_controller(sc);
925 TAILQ_FOREACH(tr, &sc->twa_busy, tr_link)
926 twa_print_request(tr, TWA_CMD_BUSY);
927 TAILQ_FOREACH(tr, &sc->twa_complete, tr_link)
928 twa_print_request(tr, TWA_CMD_COMPLETE);
936 * Function name: twa_reset_stats
937 * Description: For being called from ddb.
938 * Resets some controller stats.
945 twa_reset_stats(void)
947 struct twa_softc *sc;
951 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
952 sc->twa_qstats[TWAQ_FREE].q_max = 0;
953 sc->twa_qstats[TWAQ_BUSY].q_max = 0;
954 sc->twa_qstats[TWAQ_PENDING].q_max = 0;
955 sc->twa_qstats[TWAQ_COMPLETE].q_max = 0;
963 * Function name: twa_print_request
964 * Description: Prints a given request if it's in the wrong queue.
966 * Input: tr -- ptr to request pkt
967 * req_type-- expected status of the given request
972 twa_print_request(struct twa_request *tr, int req_type)
974 struct twa_softc *sc = tr->tr_sc;
975 struct twa_command_packet *cmdpkt = tr->tr_command;
976 struct twa_command_9k *cmd9k;
977 union twa_command_7k *cmd7k;
981 if (tr->tr_status != req_type) {
982 twa_printf(sc, "Invalid %s request %p in queue! req_type = %x, queue_type = %x\n",
983 (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ? "INTERNAL" : "EXTERNAL",
984 tr, tr->tr_status, req_type);
986 if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
987 cmd9k = &(cmdpkt->command.cmd_pkt_9k);
988 cmd_phys_addr = cmd9k->sg_list[0].address;
989 twa_printf(sc, "9K cmd = %x %x %x %x %x %x %x %x %x\n",
990 cmd9k->command.opcode,
991 cmd9k->command.reserved,
998 cmd9k->sg_list[0].length);
999 cdb = (u_int8_t *)(cmdpkt->command.cmd_pkt_9k.cdb);
1000 twa_printf(sc, "cdb = %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",
1001 cdb[0], cdb[1], cdb[2], cdb[3], cdb[4], cdb[5], cdb[6], cdb[7],
1002 cdb[8], cdb[9], cdb[10], cdb[11], cdb[12], cdb[13], cdb[14], cdb[15]);
1004 cmd7k = &(cmdpkt->command.cmd_pkt_7k);
1005 twa_printf(sc, "7K cmd = %x %x %x %x %x %x %x %x %x\n",
1006 cmd7k->generic.opcode,
1007 cmd7k->generic.sgl_offset,
1008 cmd7k->generic.size,
1009 cmd7k->generic.request_id,
1010 cmd7k->generic.unit,
1011 cmd7k->generic.host_id,
1012 cmd7k->generic.status,
1013 cmd7k->generic.flags,
1014 cmd7k->generic.count);
1017 cmd_phys_addr = (int)(tr->tr_cmd_phys);
1018 twa_printf(sc, "cmdphys=0x%x data=%p length=0x%x\n",
1019 cmd_phys_addr, tr->tr_data, tr->tr_length);
1020 twa_printf(sc, "req_id=0x%x flags=0x%x callback=%p private=%p\n",
1021 tr->tr_request_id, tr->tr_flags,
1022 tr->tr_callback, tr->tr_private);