2 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
3 * Copyright (c) 2004-05 Vinod Kashyap.
4 * Copyright (c) 2000 Michael Smith
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/twa/tw_osl_freebsd.c,v 1.15 2010/06/09 21:40:38 delphij Exp $
33 * AMCC'S 3ware driver for 9000 series storage controllers.
35 * Author: Vinod Kashyap
36 * Modifications by: Adam Radford
37 * Modifications by: Manjunath Ranganathaiah
42 * FreeBSD specific functions not related to CAM, and other
43 * miscellaneous functions.
47 #include <dev/raid/twa/tw_osl_includes.h>
48 #include <dev/raid/twa/tw_cl_fwif.h>
49 #include <dev/raid/twa/tw_cl_ioctl.h>
50 #include <dev/raid/twa/tw_osl_ioctl.h>
53 TW_INT32 TW_DEBUG_LEVEL_FOR_OSL = TW_OSL_DEBUG;
54 TW_INT32 TW_OSL_DEBUG_LEVEL_FOR_CL = TW_OSL_DEBUG;
55 #endif /* TW_OSL_DEBUG */
57 MALLOC_DEFINE(TW_OSLI_MALLOC_CLASS, "twa_commands", "twa commands");
60 static d_open_t twa_open;
61 static d_close_t twa_close;
62 static d_ioctl_t twa_ioctl;
64 static struct dev_ops twa_ops = {
71 static devclass_t twa_devclass;
75 * Function name: twa_open
76 * Description: Called when the controller is opened.
77 * Simply marks the controller as open.
79 * Input: dev -- control device corresponding to the ctlr
80 * flags -- mode of open
81 * fmt -- device type (character/block etc.)
82 * proc -- current process
84 * Return value: 0 -- success
88 twa_open(struct dev_open_args *ap)
90 cdev_t dev = ap->a_head.a_dev;
91 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
93 tw_osli_dbg_dprintf(5, sc, "entered");
94 sc->open = TW_CL_TRUE;
101 * Function name: twa_close
102 * Description: Called when the controller is closed.
103 * Simply marks the controller as not open.
105 * Input: dev -- control device corresponding to the ctlr
106 * flags -- mode of corresponding open
107 * fmt -- device type (character/block etc.)
108 * proc -- current process
110 * Return value: 0 -- success
114 twa_close(struct dev_close_args *ap)
116 cdev_t dev = ap->a_head.a_dev;
117 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
119 tw_osli_dbg_dprintf(5, sc, "entered");
120 sc->open = TW_CL_FALSE;
127 * Function name: twa_ioctl
128 * Description: Called when an ioctl is posted to the controller.
129 * Handles any OS Layer specific cmds, passes the rest
130 * on to the Common Layer.
132 * Input: dev -- control device corresponding to the ctlr
134 * buf -- ptr to buffer in kernel memory, which is
135 * a copy of the input buffer in user-space
136 * flags -- mode of corresponding open
137 * proc -- current process
138 * Output: buf -- ptr to buffer in kernel memory, which will
139 * be copied to the output buffer in user-space
140 * Return value: 0 -- success
144 twa_ioctl(struct dev_ioctl_args *ap)
146 cdev_t dev = ap->a_head.a_dev;
147 u_long cmd = ap->a_cmd;
148 caddr_t buf = ap->a_data;
149 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
152 tw_osli_dbg_dprintf(5, sc, "entered");
155 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
156 tw_osli_dbg_dprintf(6, sc, "ioctl: fw_passthru");
157 error = tw_osli_fw_passthru(sc, (TW_INT8 *)buf);
160 case TW_OSL_IOCTL_SCAN_BUS:
161 /* Request CAM for a bus scan. */
162 tw_osli_dbg_dprintf(6, sc, "ioctl: scan bus");
163 error = tw_osli_request_bus_scan(sc);
167 tw_osli_dbg_dprintf(6, sc, "ioctl: 0x%lx", cmd);
168 error = tw_cl_ioctl(&sc->ctlr_handle, cmd, buf);
176 static TW_INT32 twa_probe(device_t dev);
177 static TW_INT32 twa_attach(device_t dev);
178 static TW_INT32 twa_detach(device_t dev);
179 static TW_INT32 twa_shutdown(device_t dev);
180 #if 0 /* XXX swildner */
181 static TW_VOID twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op);
183 static TW_VOID twa_pci_intr(TW_VOID *arg);
185 static TW_INT32 tw_osli_alloc_mem(struct twa_softc *sc);
186 static TW_VOID tw_osli_free_resources(struct twa_softc *sc);
188 static TW_VOID twa_map_load_data_callback(TW_VOID *arg,
189 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
190 static TW_VOID twa_map_load_callback(TW_VOID *arg,
191 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
194 static device_method_t twa_methods[] = {
195 /* Device interface */
196 DEVMETHOD(device_probe, twa_probe),
197 DEVMETHOD(device_attach, twa_attach),
198 DEVMETHOD(device_detach, twa_detach),
199 DEVMETHOD(device_shutdown, twa_shutdown),
201 DEVMETHOD(bus_print_child, bus_generic_print_child),
202 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
206 static driver_t twa_pci_driver = {
209 sizeof(struct twa_softc)
212 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
213 MODULE_DEPEND(twa, cam, 1, 1, 1);
214 MODULE_DEPEND(twa, pci, 1, 1, 1);
218 * Function name: twa_probe
219 * Description: Called at driver load time. Claims 9000 ctlrs.
221 * Input: dev -- bus device corresponding to the ctlr
223 * Return value: <= 0 -- success
227 twa_probe(device_t dev)
229 static TW_UINT8 first_ctlr = 1;
231 tw_osli_dbg_printf(3, "entered");
233 if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
234 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
235 /* Print the driver version only once. */
237 kprintf("3ware device driver for 9000 series storage "
238 "controllers, version: %s\n",
239 TW_OSL_DRIVER_VERSION_STRING);
250 * Function name: twa_attach
251 * Description: Allocates pci resources; updates sc; adds a node to the
252 * sysctl tree to expose the driver version; makes calls
253 * (to the Common Layer) to initialize ctlr, and to
256 * Input: dev -- bus device corresponding to the ctlr
258 * Return value: 0 -- success
262 twa_attach(device_t dev)
264 struct twa_softc *sc = device_get_softc(dev);
267 TW_INT32 bar0_offset;
271 tw_osli_dbg_dprintf(3, sc, "entered");
273 sc->ctlr_handle.osl_ctlr_ctxt = sc;
275 /* Initialize the softc structure. */
277 sc->device_id = pci_get_device(dev);
279 /* Initialize the mutexes right here. */
280 sc->io_lock = &(sc->io_lock_handle);
281 spin_init(sc->io_lock);
282 sc->q_lock = &(sc->q_lock_handle);
283 spin_init(sc->q_lock);
284 sc->sim_lock = &(sc->sim_lock_handle);
285 lockinit(sc->sim_lock, "tw_osl_sim_lock", 0, LK_CANRECURSE);
287 sysctl_ctx_init(&sc->sysctl_ctxt);
288 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
289 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
290 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
291 if (sc->sysctl_tree == NULL) {
292 tw_osli_printf(sc, "error = %d",
293 TW_CL_SEVERITY_ERROR_STRING,
294 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
296 "Cannot add sysctl tree node",
300 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
301 OID_AUTO, "driver_version", CTLFLAG_RD,
302 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
304 /* Make sure we are going to be able to talk to this board. */
305 command = pci_read_config(dev, PCIR_COMMAND, 2);
306 if ((command & PCIM_CMD_PORTEN) == 0) {
307 tw_osli_printf(sc, "error = %d",
308 TW_CL_SEVERITY_ERROR_STRING,
309 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
311 "Register window not available",
313 tw_osli_free_resources(sc);
317 /* Force the busmaster enable bit on, in case the BIOS forgot. */
318 command |= PCIM_CMD_BUSMASTEREN;
319 pci_write_config(dev, PCIR_COMMAND, command, 2);
321 /* Allocate the PCI register window. */
322 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
323 &bar_num, &bar0_offset, &bar_size))) {
324 tw_osli_printf(sc, "error = %d",
325 TW_CL_SEVERITY_ERROR_STRING,
326 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
328 "Can't get PCI BAR info",
330 tw_osli_free_resources(sc);
333 sc->reg_res_id = PCIR_BARS + bar0_offset;
334 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
335 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
337 tw_osli_printf(sc, "error = %d",
338 TW_CL_SEVERITY_ERROR_STRING,
339 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
341 "Can't allocate register window",
343 tw_osli_free_resources(sc);
346 sc->bus_tag = rman_get_bustag(sc->reg_res);
347 sc->bus_handle = rman_get_bushandle(sc->reg_res);
349 /* Allocate and register our interrupt. */
351 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
352 &(sc->irq_res_id), 0, ~0, 1,
353 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
354 tw_osli_printf(sc, "error = %d",
355 TW_CL_SEVERITY_ERROR_STRING,
356 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
358 "Can't allocate interrupt",
360 tw_osli_free_resources(sc);
363 if ((error = bus_setup_intr(sc->bus_dev, sc->irq_res,
366 sc, &sc->intr_handle, NULL))) {
367 tw_osli_printf(sc, "error = %d",
368 TW_CL_SEVERITY_ERROR_STRING,
369 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
371 "Can't set up interrupt",
373 tw_osli_free_resources(sc);
377 if ((error = tw_osli_alloc_mem(sc))) {
378 tw_osli_printf(sc, "error = %d",
379 TW_CL_SEVERITY_ERROR_STRING,
380 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
382 "Memory allocation failure",
384 tw_osli_free_resources(sc);
388 /* Initialize the Common Layer for this controller. */
389 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
390 TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
391 sc->non_dma_mem, sc->dma_mem,
394 tw_osli_printf(sc, "error = %d",
395 TW_CL_SEVERITY_ERROR_STRING,
396 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
398 "Failed to initialize Common Layer/controller",
400 tw_osli_free_resources(sc);
404 /* Create the control device. */
405 sc->ctrl_dev = make_dev(&twa_ops, device_get_unit(sc->bus_dev),
406 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
407 "twa%d", device_get_unit(sc->bus_dev));
408 sc->ctrl_dev->si_drv1 = sc;
410 if ((error = tw_osli_cam_attach(sc))) {
411 tw_osli_free_resources(sc);
412 tw_osli_printf(sc, "error = %d",
413 TW_CL_SEVERITY_ERROR_STRING,
414 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
416 "Failed to initialize CAM",
427 * Function name: tw_osli_alloc_mem
428 * Description: Allocates memory needed both by CL and OSL.
430 * Input: sc -- OSL internal controller context
432 * Return value: 0 -- success
436 tw_osli_alloc_mem(struct twa_softc *sc)
438 struct tw_osli_req_context *req;
439 TW_UINT32 max_sg_elements;
440 TW_UINT32 non_dma_mem_size;
441 TW_UINT32 dma_mem_size;
445 tw_osli_dbg_dprintf(3, sc, "entered");
447 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
448 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
450 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
451 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
453 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
454 sc->device_id, TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
455 &(sc->alignment), &(sc->sg_size_factor),
456 &non_dma_mem_size, &dma_mem_size
458 tw_osli_printf(sc, "error = %d",
459 TW_CL_SEVERITY_ERROR_STRING,
460 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
462 "Can't get Common Layer's memory requirements",
467 if ((sc->non_dma_mem = kmalloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
468 M_WAITOK)) == NULL) {
469 tw_osli_printf(sc, "error = %d",
470 TW_CL_SEVERITY_ERROR_STRING,
471 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
473 "Can't allocate non-dma memory",
478 /* Create the parent dma tag. */
479 if (bus_dma_tag_create(NULL, /* parent */
480 sc->alignment, /* alignment */
481 TW_OSLI_DMA_BOUNDARY, /* boundary */
482 BUS_SPACE_MAXADDR, /* lowaddr */
483 BUS_SPACE_MAXADDR, /* highaddr */
484 NULL, NULL, /* filter, filterarg */
485 TW_CL_MAX_IO_SIZE, /* maxsize */
486 max_sg_elements, /* nsegments */
487 TW_CL_MAX_IO_SIZE, /* maxsegsize */
489 &sc->parent_tag /* tag */)) {
490 tw_osli_printf(sc, "error = %d",
491 TW_CL_SEVERITY_ERROR_STRING,
492 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
494 "Can't allocate parent DMA tag",
499 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
500 if (bus_dma_tag_create(sc->parent_tag, /* parent */
501 sc->alignment, /* alignment */
503 BUS_SPACE_MAXADDR, /* lowaddr */
504 BUS_SPACE_MAXADDR, /* highaddr */
505 NULL, NULL, /* filter, filterarg */
506 dma_mem_size, /* maxsize */
508 BUS_SPACE_MAXSIZE, /* maxsegsize */
510 &sc->cmd_tag /* tag */)) {
511 tw_osli_printf(sc, "error = %d",
512 TW_CL_SEVERITY_ERROR_STRING,
513 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
515 "Can't allocate DMA tag for Common Layer's "
521 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
522 BUS_DMA_NOWAIT, &sc->cmd_map)) {
523 /* Try a second time. */
524 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
525 BUS_DMA_NOWAIT, &sc->cmd_map)) {
526 tw_osli_printf(sc, "error = %d",
527 TW_CL_SEVERITY_ERROR_STRING,
528 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
530 "Can't allocate DMA'able memory for the"
537 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
538 dma_mem_size, twa_map_load_callback,
539 &sc->dma_mem_phys, 0);
542 * Create a dma tag for data buffers; size will be the maximum
543 * possible I/O size (128kB).
545 if (bus_dma_tag_create(sc->parent_tag, /* parent */
546 sc->alignment, /* alignment */
548 BUS_SPACE_MAXADDR, /* lowaddr */
549 BUS_SPACE_MAXADDR, /* highaddr */
550 NULL, NULL, /* filter, filterarg */
551 TW_CL_MAX_IO_SIZE, /* maxsize */
552 max_sg_elements, /* nsegments */
553 TW_CL_MAX_IO_SIZE, /* maxsegsize */
554 BUS_DMA_ALLOCNOW, /* flags */
555 #if 0 /* XXX swildner */
556 twa_busdma_lock, /* lockfunc */
557 sc->io_lock, /* lockfuncarg */
559 &sc->dma_tag /* tag */)) {
560 tw_osli_printf(sc, "error = %d",
561 TW_CL_SEVERITY_ERROR_STRING,
562 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
564 "Can't allocate DMA tag for data buffers",
570 * Create a dma tag for ioctl data buffers; size will be the maximum
571 * possible I/O size (128kB).
573 if (bus_dma_tag_create(sc->parent_tag, /* parent */
574 sc->alignment, /* alignment */
576 BUS_SPACE_MAXADDR, /* lowaddr */
577 BUS_SPACE_MAXADDR, /* highaddr */
578 NULL, NULL, /* filter, filterarg */
579 TW_CL_MAX_IO_SIZE, /* maxsize */
580 max_sg_elements, /* nsegments */
581 TW_CL_MAX_IO_SIZE, /* maxsegsize */
582 BUS_DMA_ALLOCNOW, /* flags */
583 #if 0 /* XXX swildner */
584 twa_busdma_lock, /* lockfunc */
585 sc->io_lock, /* lockfuncarg */
587 &sc->ioctl_tag /* tag */)) {
588 tw_osli_printf(sc, "error = %d",
589 TW_CL_SEVERITY_ERROR_STRING,
590 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
592 "Can't allocate DMA tag for ioctl data buffers",
597 /* Create just one map for all ioctl request data buffers. */
598 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
599 tw_osli_printf(sc, "error = %d",
600 TW_CL_SEVERITY_ERROR_STRING,
601 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
603 "Can't create ioctl map",
609 /* Initialize request queues. */
610 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
611 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
613 if ((sc->req_ctx_buf = (struct tw_osli_req_context *)
614 kmalloc((sizeof(struct tw_osli_req_context) *
615 TW_OSLI_MAX_NUM_REQUESTS),
616 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
617 tw_osli_printf(sc, "error = %d",
618 TW_CL_SEVERITY_ERROR_STRING,
619 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
621 "Failed to allocate request packets",
625 bzero(sc->req_ctx_buf,
626 sizeof(struct tw_osli_req_context) * TW_OSLI_MAX_NUM_REQUESTS);
628 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
629 req = &(sc->req_ctx_buf[i]);
631 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
632 tw_osli_printf(sc, "request # = %d, error = %d",
633 TW_CL_SEVERITY_ERROR_STRING,
634 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
636 "Can't create dma map",
641 /* Initialize the ioctl wakeup/ timeout mutex */
642 req->ioctl_wake_timeout_lock = &(req->ioctl_wake_timeout_lock_handle);
643 lockinit(req->ioctl_wake_timeout_lock, "tw_ioctl_wake_timeout_lock", 0, 0);
645 /* Insert request into the free queue. */
646 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
655 * Function name: tw_osli_free_resources
656 * Description: Performs clean-up at the time of going down.
658 * Input: sc -- ptr to OSL internal ctlr context
663 tw_osli_free_resources(struct twa_softc *sc)
665 struct tw_osli_req_context *req;
668 tw_osli_dbg_dprintf(3, sc, "entered");
670 /* Detach from CAM */
671 tw_osli_cam_detach(sc);
674 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
676 lockuninit(req->ioctl_wake_timeout_lock);
678 if ((error = bus_dmamap_destroy(sc->dma_tag,
680 tw_osli_dbg_dprintf(1, sc,
681 "dmamap_destroy(dma) returned %d",
685 if ((sc->ioctl_tag) && (sc->ioctl_map))
686 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
687 tw_osli_dbg_dprintf(1, sc,
688 "dmamap_destroy(ioctl) returned %d", error);
690 /* Free all memory allocated so far. */
692 kfree(sc->req_ctx_buf, TW_OSLI_MALLOC_CLASS);
695 kfree(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
698 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
699 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
703 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
704 tw_osli_dbg_dprintf(1, sc,
705 "dma_tag_destroy(cmd) returned %d", error);
708 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
709 tw_osli_dbg_dprintf(1, sc,
710 "dma_tag_destroy(dma) returned %d", error);
713 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
714 tw_osli_dbg_dprintf(1, sc,
715 "dma_tag_destroy(ioctl) returned %d", error);
718 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
719 tw_osli_dbg_dprintf(1, sc,
720 "dma_tag_destroy(parent) returned %d", error);
723 /* Disconnect the interrupt handler. */
725 if ((error = bus_teardown_intr(sc->bus_dev,
726 sc->irq_res, sc->intr_handle)))
727 tw_osli_dbg_dprintf(1, sc,
728 "teardown_intr returned %d", error);
730 if (sc->irq_res != NULL)
731 if ((error = bus_release_resource(sc->bus_dev,
732 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
733 tw_osli_dbg_dprintf(1, sc,
734 "release_resource(irq) returned %d", error);
737 /* Release the register window mapping. */
738 if (sc->reg_res != NULL)
739 if ((error = bus_release_resource(sc->bus_dev,
740 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
741 tw_osli_dbg_dprintf(1, sc,
742 "release_resource(io) returned %d", error);
744 dev_ops_remove_minor(&twa_ops, device_get_unit(sc->bus_dev));
746 /* Destroy the control device. */
747 if (sc->ctrl_dev != (struct cdev *)NULL)
748 destroy_dev(sc->ctrl_dev);
750 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
751 tw_osli_dbg_dprintf(1, sc,
752 "sysctl_ctx_free returned %d", error);
759 * Function name: twa_detach
760 * Description: Called when the controller is being detached from
763 * Input: dev -- bus device corresponding to the ctlr
765 * Return value: 0 -- success
769 twa_detach(device_t dev)
771 struct twa_softc *sc = device_get_softc(dev);
774 tw_osli_dbg_dprintf(3, sc, "entered");
778 tw_osli_printf(sc, "error = %d",
779 TW_CL_SEVERITY_ERROR_STRING,
780 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
787 /* Shut the controller down. */
788 if ((error = twa_shutdown(dev)))
791 /* Free all resources associated with this controller. */
792 tw_osli_free_resources(sc);
802 * Function name: twa_shutdown
803 * Description: Called at unload/shutdown time. Lets the controller
804 * know that we are going down.
806 * Input: dev -- bus device corresponding to the ctlr
808 * Return value: 0 -- success
812 twa_shutdown(device_t dev)
814 struct twa_softc *sc = device_get_softc(dev);
817 tw_osli_dbg_dprintf(3, sc, "entered");
819 /* Disconnect from the controller. */
820 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
821 tw_osli_printf(sc, "error = %d",
822 TW_CL_SEVERITY_ERROR_STRING,
823 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
825 "Failed to shutdown Common Layer/controller",
833 #if 0 /* XXX swildner */
835 * Function name: twa_busdma_lock
836 * Description: Function to provide synchronization during busdma_swi.
838 * Input: lock_arg -- lock mutex sent as argument
839 * op -- operation (lock/unlock) expected of the function
844 twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op)
846 struct spinlock *lock;
848 lock = (struct spinlock *)lock_arg;
859 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
866 * Function name: twa_pci_intr
867 * Description: Interrupt handler. Wrapper for twa_interrupt.
869 * Input: arg -- ptr to OSL internal ctlr context
874 twa_pci_intr(TW_VOID *arg)
876 struct twa_softc *sc = (struct twa_softc *)arg;
878 tw_osli_dbg_dprintf(10, sc, "entered");
879 tw_cl_interrupt(&(sc->ctlr_handle));
884 * Function name: tw_osli_fw_passthru
885 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
887 * Input: sc -- ptr to OSL internal ctlr context
888 * buf -- ptr to ioctl pkt understood by CL
890 * Return value: 0 -- success
894 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
896 struct tw_osli_req_context *req;
897 struct tw_osli_ioctl_no_data_buf *user_buf =
898 (struct tw_osli_ioctl_no_data_buf *)buf;
900 TW_UINT32 timeout = 60;
901 TW_UINT32 data_buf_size_adjusted;
902 struct tw_cl_req_packet *req_pkt;
903 struct tw_cl_passthru_req_packet *pt_req;
906 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
908 if ((req = tw_osli_get_request(sc)) == NULL)
911 req->req_handle.osl_req_ctxt = req;
913 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
915 req_pkt = &(req->req_pkt);
917 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
918 /* Let the Common Layer retry the request on cmd queue full. */
919 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
921 pt_req = &(req_pkt->gen_req_pkt.pt_req);
923 * Make sure that the data buffer sent to firmware is a
924 * 512 byte multiple in size.
926 data_buf_size_adjusted =
927 (user_buf->driver_pkt.buffer_length +
928 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
929 if ((req->length = data_buf_size_adjusted)) {
930 if ((req->data = kmalloc(data_buf_size_adjusted,
931 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
933 tw_osli_printf(sc, "error = %d",
934 TW_CL_SEVERITY_ERROR_STRING,
935 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
937 "Could not alloc mem for "
938 "fw_passthru data_buf",
940 goto fw_passthru_err;
942 /* Copy the payload. */
943 if ((error = copyin((TW_VOID *)(user_buf->pdata),
945 user_buf->driver_pkt.buffer_length)) != 0) {
946 tw_osli_printf(sc, "error = %d",
947 TW_CL_SEVERITY_ERROR_STRING,
948 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
950 "Could not copyin fw_passthru data_buf",
952 goto fw_passthru_err;
954 pt_req->sgl_entries = 1; /* will be updated during mapping */
955 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
956 TW_OSLI_REQ_FLAGS_DATA_OUT);
958 pt_req->sgl_entries = 0; /* no payload */
960 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
961 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
963 if ((error = tw_osli_map_request(req)))
964 goto fw_passthru_err;
966 end_time = tw_osl_get_local_time() + timeout;
967 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
968 lockmgr(req->ioctl_wake_timeout_lock, LK_EXCLUSIVE);
969 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
971 error = lksleep(req, req->ioctl_wake_timeout_lock, 0,
972 "twa_passthru", timeout*hz);
973 lockmgr(req->ioctl_wake_timeout_lock, LK_RELEASE);
975 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
977 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
980 if (((error = req->error_code)) ||
981 ((error = (req->state !=
982 TW_OSLI_REQ_STATE_COMPLETE))) ||
983 ((error = req_pkt->status)))
984 goto fw_passthru_err;
988 if (req_pkt->status) {
989 error = req_pkt->status;
990 goto fw_passthru_err;
993 if (error == EWOULDBLOCK) {
995 if ((!(req->error_code)) &&
996 (req->state == TW_OSLI_REQ_STATE_COMPLETE) &&
997 (!(req_pkt->status)) ) {
999 tw_osli_printf(sc, "request = %p",
1000 TW_CL_SEVERITY_ERROR_STRING,
1001 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1003 "FALSE Passthru timeout!",
1005 #endif /* TW_OSL_DEBUG */
1006 error = 0; /* False error */
1009 tw_osli_printf(sc, "request = %p",
1010 TW_CL_SEVERITY_ERROR_STRING,
1011 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1013 "Passthru request timed out!",
1016 * Should I check here if the timeout happened
1017 * because of yet another reset, and not do a
1020 tw_cl_reset_ctlr(&sc->ctlr_handle);
1022 * Don't touch req after a reset. It (and any
1023 * associated data) will already have been
1024 * unmapped by the callback.
1026 user_buf->driver_pkt.os_status = error;
1028 goto fw_passthru_err;
1031 * Either the request got completed, or we were woken up by a
1032 * signal. Calculate the new timeout, in case it was the latter.
1034 timeout = (end_time - tw_osl_get_local_time());
1037 /* If there was a payload, copy it back. */
1038 if ((!error) && (req->length))
1039 if ((error = copyout(req->data, user_buf->pdata,
1040 user_buf->driver_pkt.buffer_length)))
1041 tw_osli_printf(sc, "error = %d",
1042 TW_CL_SEVERITY_ERROR_STRING,
1043 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1045 "Could not copyout fw_passthru data_buf",
1050 * Print the failure message. For some reason, on certain OS versions,
1051 * printing this error message during reset hangs the display (although
1052 * the rest of the system is running fine. So, don't print it if the
1053 * failure was due to a reset.
1055 if ((error) && (error != TW_CL_ERR_REQ_BUS_RESET))
1056 tw_osli_printf(sc, "error = %d",
1057 TW_CL_SEVERITY_ERROR_STRING,
1058 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1060 "Firmware passthru failed!",
1063 user_buf->driver_pkt.os_status = error;
1064 /* Free resources. */
1066 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1067 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1074 * Function name: tw_osl_complete_passthru
1075 * Description: Called to complete passthru requests.
1077 * Input: req_handle -- ptr to request handle
1079 * Return value: None
1082 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1084 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1085 struct twa_softc *sc = req->ctlr;
1087 tw_osli_dbg_dprintf(5, sc, "entered");
1089 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1090 tw_osli_printf(sc, "request = %p, status = %d",
1091 TW_CL_SEVERITY_ERROR_STRING,
1092 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1094 "Unposted command completed!!",
1099 * Remove request from the busy queue. Just mark it complete.
1100 * There's no need to move it into the complete queue as we are
1101 * going to be done with it right now.
1103 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1104 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1106 tw_osli_unmap_request(req);
1109 * Don't do a wake up if there was an error even before the request
1110 * was sent down to the Common Layer, and we hadn't gotten an
1111 * EINPROGRESS. The request originator will then be returned an
1112 * error, and he can do the clean-up.
1114 if ((req->error_code) && (!(req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1117 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1118 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1119 /* Wake up the sleeping command originator. */
1120 tw_osli_dbg_dprintf(5, sc,
1121 "Waking up originator of request %p", req);
1122 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1126 * If the request completed even before mtx_sleep
1127 * was called, simply return.
1129 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1132 tw_osli_printf(sc, "request = %p",
1133 TW_CL_SEVERITY_ERROR_STRING,
1134 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1136 "Passthru callback called, "
1137 "and caller not sleeping",
1141 tw_osli_printf(sc, "request = %p",
1142 TW_CL_SEVERITY_ERROR_STRING,
1143 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1145 "Passthru callback called for non-passthru request",
1153 * Function name: tw_osli_get_request
1154 * Description: Gets a request pkt from the free queue.
1156 * Input: sc -- ptr to OSL internal ctlr context
1158 * Return value: ptr to request pkt -- success
1161 struct tw_osli_req_context *
1162 tw_osli_get_request(struct twa_softc *sc)
1164 struct tw_osli_req_context *req;
1166 tw_osli_dbg_dprintf(4, sc, "entered");
1168 /* Get a free request packet. */
1169 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1171 /* Initialize some fields to their defaults. */
1173 req->req_handle.osl_req_ctxt = NULL;
1174 req->req_handle.cl_req_ctxt = NULL;
1175 req->req_handle.is_io = 0;
1178 req->real_data = NULL;
1179 req->real_length = 0;
1180 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1182 req->error_code = 0;
1183 req->orig_req = NULL;
1185 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1194 * Function name: twa_map_load_data_callback
1195 * Description: Callback of bus_dmamap_load for the buffer associated
1196 * with data. Updates the cmd pkt (size/sgl_entries
1197 * fields, as applicable) to reflect the number of sg
1200 * Input: arg -- ptr to OSL internal request context
1201 * segs -- ptr to a list of segment descriptors
1202 * nsegments--# of segments
1203 * error -- 0 if no errors encountered before callback,
1204 * non-zero if errors were encountered
1206 * Return value: None
1209 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1210 TW_INT32 nsegments, TW_INT32 error)
1212 struct tw_osli_req_context *req =
1213 (struct tw_osli_req_context *)arg;
1214 struct twa_softc *sc = req->ctlr;
1215 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1217 tw_osli_dbg_dprintf(10, sc, "entered");
1219 /* Mark the request as currently being processed. */
1220 req->state = TW_OSLI_REQ_STATE_BUSY;
1221 /* Move the request into the busy queue. */
1222 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1224 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1226 if (error == EFBIG) {
1227 req->error_code = error;
1231 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1232 struct tw_cl_passthru_req_packet *pt_req;
1234 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1235 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1236 BUS_DMASYNC_PREREAD);
1238 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1240 * If we're using an alignment buffer, and we're
1241 * writing data, copy the real data out.
1243 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1244 bcopy(req->real_data, req->data, req->real_length);
1245 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1246 BUS_DMASYNC_PREWRITE);
1249 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1250 pt_req->sg_list = (TW_UINT8 *)segs;
1251 pt_req->sgl_entries += (nsegments - 1);
1252 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1253 &(req->req_handle));
1255 struct tw_cl_scsi_req_packet *scsi_req;
1257 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1258 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1259 BUS_DMASYNC_PREREAD);
1261 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1263 * If we're using an alignment buffer, and we're
1264 * writing data, copy the real data out.
1266 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1267 bcopy(req->real_data, req->data, req->real_length);
1268 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1269 BUS_DMASYNC_PREWRITE);
1272 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1273 scsi_req->sg_list = (TW_UINT8 *)segs;
1274 scsi_req->sgl_entries += (nsegments - 1);
1275 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1276 &(req->req_handle));
1281 req->error_code = error;
1282 req_pkt->tw_osl_callback(&(req->req_handle));
1284 * If the caller had been returned EINPROGRESS, and he has
1285 * registered a callback for handling completion, the callback
1286 * will never get called because we were unable to submit the
1287 * request. So, free up the request right here.
1289 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1290 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1297 * Function name: twa_map_load_callback
1298 * Description: Callback of bus_dmamap_load for the buffer associated
1301 * Input: arg -- ptr to variable to hold phys addr
1302 * segs -- ptr to a list of segment descriptors
1303 * nsegments--# of segments
1304 * error -- 0 if no errors encountered before callback,
1305 * non-zero if errors were encountered
1307 * Return value: None
1310 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1311 TW_INT32 nsegments, TW_INT32 error)
1313 *((bus_addr_t *)arg) = segs[0].ds_addr;
1319 * Function name: tw_osli_map_request
1320 * Description: Maps a cmd pkt and data associated with it, into
1323 * Input: req -- ptr to request pkt
1325 * Return value: 0 -- success
1326 * non-zero-- failure
1329 tw_osli_map_request(struct tw_osli_req_context *req)
1331 struct twa_softc *sc = req->ctlr;
1334 tw_osli_dbg_dprintf(10, sc, "entered");
1336 /* If the command involves data, map that too. */
1337 if (req->data != NULL) {
1339 * It's sufficient for the data pointer to be 4-byte aligned
1340 * to work with 9000. However, if 4-byte aligned addresses
1341 * are passed to bus_dmamap_load, we can get back sg elements
1342 * that are not 512-byte multiples in size. So, we will let
1343 * only those buffers that are 512-byte aligned to pass
1344 * through, and bounce the rest, so as to make sure that we
1345 * always get back sg elements that are 512-byte multiples
1348 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1349 (req->length % sc->sg_size_factor)) {
1350 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1351 /* Save original data pointer and length. */
1352 req->real_data = req->data;
1353 req->real_length = req->length;
1354 req->length = (req->length +
1355 (sc->sg_size_factor - 1)) &
1356 ~(sc->sg_size_factor - 1);
1357 req->data = kmalloc(req->length, TW_OSLI_MALLOC_CLASS,
1359 if (req->data == NULL) {
1360 tw_osli_printf(sc, "error = %d",
1361 TW_CL_SEVERITY_ERROR_STRING,
1362 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1364 "Failed to allocate memory "
1365 "for bounce buffer",
1367 /* Restore original data pointer and length. */
1368 req->data = req->real_data;
1369 req->length = req->real_length;
1375 * Map the data buffer into bus space and build the SG list.
1377 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1378 /* Lock against multiple simultaneous ioctl calls. */
1379 spin_lock(sc->io_lock);
1380 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1381 req->data, req->length,
1382 twa_map_load_data_callback, req,
1384 spin_unlock(sc->io_lock);
1387 * There's only one CAM I/O thread running at a time.
1388 * So, there's no need to hold the io_lock.
1390 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1391 req->data, req->length,
1392 twa_map_load_data_callback, req,
1397 error = req->error_code;
1399 if (error == EINPROGRESS) {
1401 * Specifying sc->io_lock as the lockfuncarg
1402 * in ...tag_create should protect the access
1403 * of ...FLAGS_MAPPED from the callback.
1405 spin_lock(sc->io_lock);
1406 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED))
1407 req->flags |= TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1408 tw_osli_disallow_new_requests(sc, &(req->req_handle));
1409 spin_unlock(sc->io_lock);
1412 /* Free alignment buffer if it was used. */
1414 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1415 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1417 * Restore original data pointer
1420 req->data = req->real_data;
1421 req->length = req->real_length;
1427 /* Mark the request as currently being processed. */
1428 req->state = TW_OSLI_REQ_STATE_BUSY;
1429 /* Move the request into the busy queue. */
1430 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1431 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1432 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1433 &(req->req_pkt), &(req->req_handle));
1435 error = tw_cl_start_io(&sc->ctlr_handle,
1436 &(req->req_pkt), &(req->req_handle));
1438 req->error_code = error;
1439 req->req_pkt.tw_osl_callback(&(req->req_handle));
1448 * Function name: tw_osli_unmap_request
1449 * Description: Undoes the mapping done by tw_osli_map_request.
1451 * Input: req -- ptr to request pkt
1453 * Return value: None
1456 tw_osli_unmap_request(struct tw_osli_req_context *req)
1458 struct twa_softc *sc = req->ctlr;
1460 tw_osli_dbg_dprintf(10, sc, "entered");
1462 /* If the command involved data, unmap that too. */
1463 if (req->data != NULL) {
1464 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1465 /* Lock against multiple simultaneous ioctl calls. */
1466 spin_lock(sc->io_lock);
1468 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1469 bus_dmamap_sync(sc->ioctl_tag,
1470 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1473 * If we are using a bounce buffer, and we are
1474 * reading data, copy the real data in.
1476 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1477 bcopy(req->data, req->real_data,
1481 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1482 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1483 BUS_DMASYNC_POSTWRITE);
1485 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1487 spin_unlock(sc->io_lock);
1489 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1490 bus_dmamap_sync(sc->dma_tag,
1491 req->dma_map, BUS_DMASYNC_POSTREAD);
1494 * If we are using a bounce buffer, and we are
1495 * reading data, copy the real data in.
1497 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1498 bcopy(req->data, req->real_data,
1501 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1502 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1503 BUS_DMASYNC_POSTWRITE);
1505 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1509 /* Free alignment buffer if it was used. */
1510 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1511 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1512 /* Restore original data pointer and length. */
1513 req->data = req->real_data;
1514 req->length = req->real_length;
1522 TW_VOID twa_report_stats(TW_VOID);
1523 TW_VOID twa_reset_stats(TW_VOID);
1524 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1525 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1529 * Function name: twa_report_stats
1530 * Description: For being called from ddb. Calls functions that print
1531 * OSL and CL internal stats for the controller.
1535 * Return value: None
1538 twa_report_stats(TW_VOID)
1540 struct twa_softc *sc;
1543 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1544 tw_osli_print_ctlr_stats(sc);
1545 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1552 * Function name: tw_osli_print_ctlr_stats
1553 * Description: For being called from ddb. Prints OSL controller stats
1555 * Input: sc -- ptr to OSL internal controller context
1557 * Return value: None
1560 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1562 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1563 twa_printf(sc, "OSLq type current max\n");
1564 twa_printf(sc, "free %04d %04d\n",
1565 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1566 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1567 twa_printf(sc, "busy %04d %04d\n",
1568 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1569 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1575 * Function name: twa_print_req_info
1576 * Description: For being called from ddb. Calls functions that print
1577 * OSL and CL internal details for the request.
1579 * Input: req -- ptr to OSL internal request context
1581 * Return value: None
1584 twa_print_req_info(struct tw_osli_req_context *req)
1586 struct twa_softc *sc = req->ctlr;
1588 twa_printf(sc, "OSL details for request:\n");
1589 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1590 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1591 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1592 "next_req = %p, prev_req = %p, dma_map = %p\n",
1593 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1594 req->data, req->length, req->real_data, req->real_length,
1595 req->state, req->flags, req->error_code, req->orig_req,
1596 req->link.next, req->link.prev, req->dma_map);
1597 tw_cl_print_req_info(&(req->req_handle));
1603 * Function name: twa_reset_stats
1604 * Description: For being called from ddb.
1605 * Resets some OSL controller stats.
1609 * Return value: None
1612 twa_reset_stats(TW_VOID)
1614 struct twa_softc *sc;
1617 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1618 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1619 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1620 tw_cl_reset_stats(&sc->ctlr_handle);
1624 #endif /* TW_OSL_DEBUG */