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
33 * AMCC'S 3ware driver for 9000 series storage controllers.
35 * Author: Vinod Kashyap
36 * Modifications by: Adam Radford
41 * FreeBSD specific functions not related to CAM, and other
42 * miscellaneous functions.
46 #include <dev/twa/tw_osl_includes.h>
47 #include <dev/twa/tw_cl_fwif.h>
48 #include <dev/twa/tw_cl_ioctl.h>
49 #include <dev/twa/tw_osl_ioctl.h>
52 TW_INT32 TW_DEBUG_LEVEL_FOR_OSL = TW_OSL_DEBUG;
53 TW_INT32 TW_OSL_DEBUG_LEVEL_FOR_CL = TW_OSL_DEBUG;
54 #endif /* TW_OSL_DEBUG */
56 MALLOC_DEFINE(TW_OSLI_MALLOC_CLASS, "twa_commands", "twa commands");
59 static d_open_t twa_open;
60 static d_close_t twa_close;
61 static d_ioctl_t twa_ioctl;
63 static struct cdevsw twa_cdevsw = {
64 .d_version = D_VERSION,
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 cdev *dev, TW_INT32 flags, TW_INT32 fmt, d_thread_t *proc)
90 TW_INT32 unit = minor(dev);
91 struct twa_softc *sc = devclass_get_softc(twa_devclass, unit);
93 tw_osli_dbg_dprintf(5, sc, "entered");
94 sc->state |= TW_OSLI_CTLR_STATE_OPEN;
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 cdev *dev, TW_INT32 flags, TW_INT32 fmt, d_thread_t *proc)
116 TW_INT32 unit = minor(dev);
117 struct twa_softc *sc = devclass_get_softc(twa_devclass, unit);
119 tw_osli_dbg_dprintf(5, sc, "entered");
120 sc->state &= ~TW_OSLI_CTLR_STATE_OPEN;
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 cdev *dev, u_long cmd, caddr_t buf, TW_INT32 flags, d_thread_t *proc)
146 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
149 tw_osli_dbg_dprintf(5, sc, "entered");
152 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
153 tw_osli_dbg_dprintf(6, sc, "ioctl: fw_passthru");
154 error = tw_osli_fw_passthru(sc, (TW_INT8 *)buf);
157 case TW_OSL_IOCTL_SCAN_BUS:
158 /* Request CAM for a bus scan. */
159 tw_osli_dbg_dprintf(6, sc, "ioctl: scan bus");
160 error = tw_osli_request_bus_scan(sc);
164 tw_osli_dbg_dprintf(6, sc, "ioctl: 0x%lx", cmd);
165 error = tw_cl_ioctl(&sc->ctlr_handle, cmd, buf);
173 static TW_INT32 twa_probe(device_t dev);
174 static TW_INT32 twa_attach(device_t dev);
175 static TW_INT32 twa_detach(device_t dev);
176 static TW_INT32 twa_shutdown(device_t dev);
177 static TW_VOID twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op);
178 #ifdef TW_OSLI_DEFERRED_INTR_USED
179 static int twa_pci_intr_fast(TW_VOID *arg);
180 static TW_VOID twa_deferred_intr(TW_VOID *context, TW_INT32 pending);
182 static TW_VOID twa_pci_intr(TW_VOID *arg);
183 #endif /* TW_OSLI_DEFERRED_INTR_USED */
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 printf("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 mtx_init(sc->io_lock, "tw_osl_io_lock", NULL, MTX_SPIN);
282 sc->q_lock = &(sc->q_lock_handle);
283 mtx_init(sc->q_lock, "tw_osl_q_lock", NULL, MTX_SPIN);
285 sysctl_ctx_init(&sc->sysctl_ctxt);
286 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
287 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
288 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
289 if (sc->sysctl_tree == NULL) {
290 tw_osli_printf(sc, "error = %d",
291 TW_CL_SEVERITY_ERROR_STRING,
292 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
294 "Cannot add sysctl tree node",
298 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
299 OID_AUTO, "driver_version", CTLFLAG_RD,
300 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
302 /* Make sure we are going to be able to talk to this board. */
303 command = pci_read_config(dev, PCIR_COMMAND, 2);
304 if ((command & PCIM_CMD_PORTEN) == 0) {
305 tw_osli_printf(sc, "error = %d",
306 TW_CL_SEVERITY_ERROR_STRING,
307 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
309 "Register window not available",
311 tw_osli_free_resources(sc);
315 /* Force the busmaster enable bit on, in case the BIOS forgot. */
316 command |= PCIM_CMD_BUSMASTEREN;
317 pci_write_config(dev, PCIR_COMMAND, command, 2);
319 /* Allocate the PCI register window. */
320 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
321 &bar_num, &bar0_offset, &bar_size))) {
322 tw_osli_printf(sc, "error = %d",
323 TW_CL_SEVERITY_ERROR_STRING,
324 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
326 "Can't get PCI BAR info",
328 tw_osli_free_resources(sc);
331 sc->reg_res_id = PCIR_BARS + bar0_offset;
332 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
333 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
335 tw_osli_printf(sc, "error = %d",
336 TW_CL_SEVERITY_ERROR_STRING,
337 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
339 "Can't allocate register window",
341 tw_osli_free_resources(sc);
344 sc->bus_tag = rman_get_bustag(sc->reg_res);
345 sc->bus_handle = rman_get_bushandle(sc->reg_res);
347 /* Allocate and register our interrupt. */
349 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
350 &(sc->irq_res_id), 0, ~0, 1,
351 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
352 tw_osli_printf(sc, "error = %d",
353 TW_CL_SEVERITY_ERROR_STRING,
354 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
356 "Can't allocate interrupt",
358 tw_osli_free_resources(sc);
361 if ((error = bus_setup_intr(sc->bus_dev, sc->irq_res,
363 #ifdef TW_OSLI_DEFERRED_INTR_USED
364 twa_pci_intr_fast, NULL,
368 sc, &sc->intr_handle))) {
369 tw_osli_printf(sc, "error = %d",
370 TW_CL_SEVERITY_ERROR_STRING,
371 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
373 "Can't set up interrupt",
375 tw_osli_free_resources(sc);
379 #ifdef TW_OSLI_DEFERRED_INTR_USED
380 TASK_INIT(&sc->deferred_intr_callback, 0, twa_deferred_intr, sc);
381 #endif /* TW_OSLI_DEFERRED_INTR_USED */
383 if ((error = tw_osli_alloc_mem(sc))) {
384 tw_osli_printf(sc, "error = %d",
385 TW_CL_SEVERITY_ERROR_STRING,
386 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
388 "Memory allocation failure",
390 tw_osli_free_resources(sc);
394 /* Initialize the Common Layer for this controller. */
395 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
396 TW_OSLI_MAX_NUM_IOS, TW_OSLI_MAX_NUM_AENS,
397 sc->non_dma_mem, sc->dma_mem,
400 tw_osli_printf(sc, "error = %d",
401 TW_CL_SEVERITY_ERROR_STRING,
402 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
404 "Failed to initialize Common Layer/controller",
406 tw_osli_free_resources(sc);
410 /* Create the control device. */
411 sc->ctrl_dev = make_dev(&twa_cdevsw, device_get_unit(sc->bus_dev),
412 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
413 "twa%d", device_get_unit(sc->bus_dev));
414 sc->ctrl_dev->si_drv1 = sc;
416 if ((error = tw_osli_cam_attach(sc))) {
417 tw_osli_free_resources(sc);
418 tw_osli_printf(sc, "error = %d",
419 TW_CL_SEVERITY_ERROR_STRING,
420 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
422 "Failed to initialize CAM",
433 * Function name: tw_osli_alloc_mem
434 * Description: Allocates memory needed both by CL and OSL.
436 * Input: sc -- OSL internal controller context
438 * Return value: 0 -- success
442 tw_osli_alloc_mem(struct twa_softc *sc)
444 struct tw_osli_req_context *req;
445 TW_UINT32 max_sg_elements;
446 TW_UINT32 non_dma_mem_size;
447 TW_UINT32 dma_mem_size;
451 tw_osli_dbg_dprintf(3, sc, "entered");
453 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
454 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
455 #ifdef TW_OSLI_DEFERRED_INTR_USED
456 sc->flags |= TW_CL_DEFERRED_INTR_USED;
457 #endif /* TW_OSLI_DEFERRED_INTR_USED */
459 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
460 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
462 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
463 sc->device_id, TW_OSLI_MAX_NUM_IOS, TW_OSLI_MAX_NUM_AENS,
464 &(sc->alignment), &(sc->sg_size_factor),
465 &non_dma_mem_size, &dma_mem_size
467 tw_osli_printf(sc, "error = %d",
468 TW_CL_SEVERITY_ERROR_STRING,
469 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
471 "Can't get Common Layer's memory requirements",
476 if ((sc->non_dma_mem = malloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
477 M_WAITOK)) == NULL) {
478 tw_osli_printf(sc, "error = %d",
479 TW_CL_SEVERITY_ERROR_STRING,
480 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
482 "Can't allocate non-dma memory",
487 /* Create the parent dma tag. */
488 if (bus_dma_tag_create(NULL, /* parent */
489 sc->alignment, /* alignment */
491 BUS_SPACE_MAXADDR, /* lowaddr */
492 BUS_SPACE_MAXADDR, /* highaddr */
493 NULL, NULL, /* filter, filterarg */
494 TW_CL_MAX_IO_SIZE, /* maxsize */
495 max_sg_elements, /* nsegments */
496 TW_CL_MAX_IO_SIZE, /* maxsegsize */
499 NULL, /* lockfuncarg */
500 &sc->parent_tag /* tag */)) {
501 tw_osli_printf(sc, "error = %d",
502 TW_CL_SEVERITY_ERROR_STRING,
503 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
505 "Can't allocate parent DMA tag",
510 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
511 if (bus_dma_tag_create(sc->parent_tag, /* parent */
512 sc->alignment, /* alignment */
514 BUS_SPACE_MAXADDR, /* lowaddr */
515 BUS_SPACE_MAXADDR, /* highaddr */
516 NULL, NULL, /* filter, filterarg */
517 dma_mem_size, /* maxsize */
519 BUS_SPACE_MAXSIZE, /* maxsegsize */
522 NULL, /* lockfuncarg */
523 &sc->cmd_tag /* tag */)) {
524 tw_osli_printf(sc, "error = %d",
525 TW_CL_SEVERITY_ERROR_STRING,
526 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
528 "Can't allocate DMA tag for Common Layer's "
534 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
535 BUS_DMA_NOWAIT, &sc->cmd_map)) {
536 /* Try a second time. */
537 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
538 BUS_DMA_NOWAIT, &sc->cmd_map)) {
539 tw_osli_printf(sc, "error = %d",
540 TW_CL_SEVERITY_ERROR_STRING,
541 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
543 "Can't allocate DMA'able memory for the"
550 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
551 dma_mem_size, twa_map_load_callback,
552 &sc->dma_mem_phys, 0);
555 * Create a dma tag for data buffers; size will be the maximum
556 * possible I/O size (128kB).
558 if (bus_dma_tag_create(sc->parent_tag, /* parent */
559 sc->alignment, /* alignment */
561 BUS_SPACE_MAXADDR, /* lowaddr */
562 BUS_SPACE_MAXADDR, /* highaddr */
563 NULL, NULL, /* filter, filterarg */
564 TW_CL_MAX_IO_SIZE, /* maxsize */
565 max_sg_elements, /* nsegments */
566 TW_CL_MAX_IO_SIZE, /* maxsegsize */
567 BUS_DMA_ALLOCNOW, /* flags */
568 twa_busdma_lock, /* lockfunc */
569 sc->io_lock, /* lockfuncarg */
570 &sc->dma_tag /* tag */)) {
571 tw_osli_printf(sc, "error = %d",
572 TW_CL_SEVERITY_ERROR_STRING,
573 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
575 "Can't allocate DMA tag for data buffers",
581 * Create a dma tag for ioctl data buffers; size will be the maximum
582 * possible I/O size (128kB).
584 if (bus_dma_tag_create(sc->parent_tag, /* parent */
585 sc->alignment, /* alignment */
587 BUS_SPACE_MAXADDR, /* lowaddr */
588 BUS_SPACE_MAXADDR, /* highaddr */
589 NULL, NULL, /* filter, filterarg */
590 TW_CL_MAX_IO_SIZE, /* maxsize */
591 max_sg_elements, /* nsegments */
592 TW_CL_MAX_IO_SIZE, /* maxsegsize */
593 BUS_DMA_ALLOCNOW, /* flags */
594 twa_busdma_lock, /* lockfunc */
595 sc->io_lock, /* lockfuncarg */
596 &sc->ioctl_tag /* tag */)) {
597 tw_osli_printf(sc, "error = %d",
598 TW_CL_SEVERITY_ERROR_STRING,
599 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
601 "Can't allocate DMA tag for ioctl data buffers",
606 /* Create just one map for all ioctl request data buffers. */
607 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
608 tw_osli_printf(sc, "error = %d",
609 TW_CL_SEVERITY_ERROR_STRING,
610 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
612 "Can't create ioctl map",
618 /* Initialize request queues. */
619 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
620 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
622 if ((sc->req_ctxt_buf = (struct tw_osli_req_context *)
623 malloc((sizeof(struct tw_osli_req_context) *
624 TW_OSLI_MAX_NUM_IOS),
625 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
626 tw_osli_printf(sc, "error = %d",
627 TW_CL_SEVERITY_ERROR_STRING,
628 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
630 "Failed to allocate request packets",
634 bzero(sc->req_ctxt_buf,
635 sizeof(struct tw_osli_req_context) * TW_OSLI_MAX_NUM_IOS);
637 for (i = 0; i < TW_OSLI_MAX_NUM_IOS; i++) {
638 req = &(sc->req_ctxt_buf[i]);
640 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
641 tw_osli_printf(sc, "request # = %d, error = %d",
642 TW_CL_SEVERITY_ERROR_STRING,
643 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
645 "Can't create dma map",
650 /* Insert request into the free queue. */
651 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
660 * Function name: tw_osli_free_resources
661 * Description: Performs clean-up at the time of going down.
663 * Input: sc -- ptr to OSL internal ctlr context
668 tw_osli_free_resources(struct twa_softc *sc)
670 struct tw_osli_req_context *req;
673 tw_osli_dbg_dprintf(3, sc, "entered");
675 /* Detach from CAM */
676 tw_osli_cam_detach(sc);
678 if (sc->req_ctxt_buf)
679 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
681 if ((error = bus_dmamap_destroy(sc->dma_tag,
683 tw_osli_dbg_dprintf(1, sc,
684 "dmamap_destroy(dma) returned %d",
687 if ((sc->ioctl_tag) && (sc->ioctl_map))
688 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
689 tw_osli_dbg_dprintf(1, sc,
690 "dmamap_destroy(ioctl) returned %d", error);
692 /* Free all memory allocated so far. */
693 if (sc->req_ctxt_buf)
694 free(sc->req_ctxt_buf, TW_OSLI_MALLOC_CLASS);
697 free(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
700 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
701 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
705 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
706 tw_osli_dbg_dprintf(1, sc,
707 "dma_tag_destroy(cmd) returned %d", error);
710 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
711 tw_osli_dbg_dprintf(1, sc,
712 "dma_tag_destroy(dma) returned %d", error);
715 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
716 tw_osli_dbg_dprintf(1, sc,
717 "dma_tag_destroy(ioctl) returned %d", error);
720 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
721 tw_osli_dbg_dprintf(1, sc,
722 "dma_tag_destroy(parent) returned %d", error);
725 /* Disconnect the interrupt handler. */
727 if ((error = bus_teardown_intr(sc->bus_dev,
728 sc->irq_res, sc->intr_handle)))
729 tw_osli_dbg_dprintf(1, sc,
730 "teardown_intr returned %d", error);
732 if (sc->irq_res != NULL)
733 if ((error = bus_release_resource(sc->bus_dev,
734 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
735 tw_osli_dbg_dprintf(1, sc,
736 "release_resource(irq) returned %d", error);
739 /* Release the register window mapping. */
740 if (sc->reg_res != NULL)
741 if ((error = bus_release_resource(sc->bus_dev,
742 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
743 tw_osli_dbg_dprintf(1, sc,
744 "release_resource(io) returned %d", error);
747 /* Destroy the control device. */
748 if (sc->ctrl_dev != (struct cdev *)NULL)
749 destroy_dev(sc->ctrl_dev);
751 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
752 tw_osli_dbg_dprintf(1, sc,
753 "sysctl_ctx_free returned %d", error);
760 * Function name: twa_detach
761 * Description: Called when the controller is being detached from
764 * Input: dev -- bus device corresponding to the ctlr
766 * Return value: 0 -- success
770 twa_detach(device_t dev)
772 struct twa_softc *sc = device_get_softc(dev);
775 tw_osli_dbg_dprintf(3, sc, "entered");
778 if (sc->state & TW_OSLI_CTLR_STATE_OPEN) {
779 tw_osli_printf(sc, "error = %d",
780 TW_CL_SEVERITY_ERROR_STRING,
781 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
788 /* Shut the controller down. */
789 if ((error = twa_shutdown(dev)))
792 /* Free all resources associated with this controller. */
793 tw_osli_free_resources(sc);
803 * Function name: twa_shutdown
804 * Description: Called at unload/shutdown time. Lets the controller
805 * know that we are going down.
807 * Input: dev -- bus device corresponding to the ctlr
809 * Return value: 0 -- success
813 twa_shutdown(device_t dev)
815 struct twa_softc *sc = device_get_softc(dev);
818 tw_osli_dbg_dprintf(3, sc, "entered");
820 /* Disconnect from the controller. */
821 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
822 tw_osli_printf(sc, "error = %d",
823 TW_CL_SEVERITY_ERROR_STRING,
824 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
826 "Failed to shutdown Common Layer/controller",
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)
848 lock = (struct mtx *)lock_arg;
855 mtx_unlock_spin(lock);
859 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
864 #ifdef TW_OSLI_DEFERRED_INTR_USED
866 * Function name: twa_pci_intr_fast
867 * Description: Interrupt handler. Wrapper for twa_interrupt.
869 * Input: arg -- ptr to OSL internal ctlr context
870 * Output: FILTER_HANDLED or FILTER_STRAY
874 twa_pci_intr_fast(TW_VOID *arg)
876 struct twa_softc *sc = (struct twa_softc *)arg;
878 tw_osli_dbg_dprintf(10, sc, "entered");
879 if (tw_cl_interrupt(&(sc->ctlr_handle))) {
880 tw_cl_deferred_interrupt(&(sc->ctlr_handle));
881 return(FILTER_HANDLED);
883 return(FILTER_STRAY);
887 * Function name: twa_pci_intr
888 * Description: Interrupt handler. Wrapper for twa_interrupt.
890 * Input: arg -- ptr to OSL internal ctlr context
895 twa_pci_intr(TW_VOID *arg)
897 struct twa_softc *sc = (struct twa_softc *)arg;
899 tw_osli_dbg_dprintf(10, sc, "entered");
900 if (tw_cl_interrupt(&(sc->ctlr_handle)))
901 tw_cl_deferred_interrupt(&(sc->ctlr_handle));
905 #ifdef TW_OSLI_DEFERRED_INTR_USED
908 * Function name: twa_deferred_intr
909 * Description: Deferred interrupt handler.
911 * Input: context -- ptr to OSL internal ctlr context
912 * pending -- not used
917 twa_deferred_intr(TW_VOID *context, TW_INT32 pending)
919 struct twa_softc *sc = (struct twa_softc *)context;
921 tw_osli_dbg_dprintf(10, sc, "entered");
923 tw_cl_deferred_interrupt(&(sc->ctlr_handle));
926 #endif /* TW_OSLI_DEFERRED_INTR_USED */
931 * Function name: tw_osli_fw_passthru
932 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
934 * Input: sc -- ptr to OSL internal ctlr context
935 * buf -- ptr to ioctl pkt understood by CL
937 * Return value: 0 -- success
941 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
943 struct tw_osli_req_context *req;
944 struct tw_osli_ioctl_no_data_buf *user_buf =
945 (struct tw_osli_ioctl_no_data_buf *)buf;
947 TW_UINT32 timeout = 60;
948 TW_UINT32 data_buf_size_adjusted;
949 struct tw_cl_req_packet *req_pkt;
950 struct tw_cl_passthru_req_packet *pt_req;
953 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
955 if ((req = tw_osli_get_request(sc)) == NULL)
958 req->req_handle.osl_req_ctxt = req;
960 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
962 req_pkt = &(req->req_pkt);
964 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
965 /* Let the Common Layer retry the request on cmd queue full. */
966 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
968 pt_req = &(req_pkt->gen_req_pkt.pt_req);
970 * Make sure that the data buffer sent to firmware is a
971 * 512 byte multiple in size.
973 data_buf_size_adjusted =
974 (user_buf->driver_pkt.buffer_length +
975 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
976 if ((req->length = data_buf_size_adjusted)) {
977 if ((req->data = malloc(data_buf_size_adjusted,
978 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
980 tw_osli_printf(sc, "error = %d",
981 TW_CL_SEVERITY_ERROR_STRING,
982 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
984 "Could not alloc mem for "
985 "fw_passthru data_buf",
987 goto fw_passthru_err;
989 /* Copy the payload. */
990 if ((error = copyin((TW_VOID *)(user_buf->pdata),
992 user_buf->driver_pkt.buffer_length)) != 0) {
993 tw_osli_printf(sc, "error = %d",
994 TW_CL_SEVERITY_ERROR_STRING,
995 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
997 "Could not copyin fw_passthru data_buf",
999 goto fw_passthru_err;
1001 pt_req->sgl_entries = 1; /* will be updated during mapping */
1002 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
1003 TW_OSLI_REQ_FLAGS_DATA_OUT);
1005 pt_req->sgl_entries = 0; /* no payload */
1007 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
1008 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
1010 if ((error = tw_osli_map_request(req)))
1011 goto fw_passthru_err;
1013 end_time = tw_osl_get_local_time() + timeout;
1014 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
1015 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
1017 error = tsleep(req, PRIBIO, "twa_passthru", timeout * hz);
1019 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
1021 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1024 if (((error = req->error_code)) ||
1025 ((error = (req->state !=
1026 TW_OSLI_REQ_STATE_COMPLETE))) ||
1027 ((error = req_pkt->status)))
1028 goto fw_passthru_err;
1032 if (req_pkt->status) {
1033 error = req_pkt->status;
1034 goto fw_passthru_err;
1037 if (error == EWOULDBLOCK) {
1039 tw_osli_printf(sc, "request = %p",
1040 TW_CL_SEVERITY_ERROR_STRING,
1041 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1043 "Passthru request timed out!",
1046 * Should I check here if the timeout happened
1047 * because of yet another reset, and not do a
1050 tw_cl_reset_ctlr(&sc->ctlr_handle);
1052 * Don't touch req after a reset. It (and any
1053 * associated data) will already have been
1054 * freed by the callback. Just return.
1056 user_buf->driver_pkt.os_status = error;
1060 * Either the request got completed, or we were woken up by a
1061 * signal. Calculate the new timeout, in case it was the latter.
1063 timeout = (end_time - tw_osl_get_local_time());
1066 /* If there was a payload, copy it back. */
1067 if ((!error) && (req->length))
1068 if ((error = copyout(req->data, user_buf->pdata,
1069 user_buf->driver_pkt.buffer_length)))
1070 tw_osli_printf(sc, "error = %d",
1071 TW_CL_SEVERITY_ERROR_STRING,
1072 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1074 "Could not copyout fw_passthru data_buf",
1079 * Print the failure message. For some reason, on certain OS versions,
1080 * printing this error message during reset hangs the display (although
1081 * the rest of the system is running fine. So, don't print it if the
1082 * failure was due to a reset.
1084 if ((error) && (error != TW_CL_ERR_REQ_BUS_RESET))
1085 tw_osli_printf(sc, "error = %d",
1086 TW_CL_SEVERITY_ERROR_STRING,
1087 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1089 "Firmware passthru failed!",
1092 user_buf->driver_pkt.os_status = error;
1093 /* Free resources. */
1095 free(req->data, TW_OSLI_MALLOC_CLASS);
1096 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1103 * Function name: tw_osl_complete_passthru
1104 * Description: Called to complete passthru requests.
1106 * Input: req_handle -- ptr to request handle
1108 * Return value: None
1111 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1113 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1114 struct twa_softc *sc = req->ctlr;
1116 tw_osli_dbg_dprintf(5, sc, "entered");
1118 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1119 tw_osli_printf(sc, "request = %p, status = %d",
1120 TW_CL_SEVERITY_ERROR_STRING,
1121 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1123 "Unposted command completed!!",
1128 * Remove request from the busy queue. Just mark it complete.
1129 * There's no need to move it into the complete queue as we are
1130 * going to be done with it right now.
1132 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1133 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1135 tw_osli_unmap_request(req);
1138 * Don't do a wake up if there was an error even before the request
1139 * was sent down to the Common Layer, and we hadn't gotten an
1140 * EINPROGRESS. The request originator will then be returned an
1141 * error, and he can do the clean-up.
1143 if ((req->error_code) &&
1144 (!(req->state & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1147 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1148 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1149 /* Wake up the sleeping command originator. */
1150 tw_osli_dbg_dprintf(5, sc,
1151 "Waking up originator of request %p", req);
1152 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1156 * If the request completed even before tsleep
1157 * was called, simply return.
1159 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1162 tw_osli_printf(sc, "request = %p",
1163 TW_CL_SEVERITY_ERROR_STRING,
1164 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1166 "Passthru callback called, "
1167 "and caller not sleeping",
1171 tw_osli_printf(sc, "request = %p",
1172 TW_CL_SEVERITY_ERROR_STRING,
1173 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1175 "Passthru callback called for non-passthru request",
1183 * Function name: tw_osli_get_request
1184 * Description: Gets a request pkt from the free queue.
1186 * Input: sc -- ptr to OSL internal ctlr context
1188 * Return value: ptr to request pkt -- success
1191 struct tw_osli_req_context *
1192 tw_osli_get_request(struct twa_softc *sc)
1194 struct tw_osli_req_context *req;
1196 tw_osli_dbg_dprintf(4, sc, "entered");
1198 /* Get a free request packet. */
1199 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1201 /* Initialize some fields to their defaults. */
1203 req->req_handle.osl_req_ctxt = NULL;
1204 req->req_handle.cl_req_ctxt = NULL;
1207 req->real_data = NULL;
1208 req->real_length = 0;
1209 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1211 req->error_code = 0;
1212 req->orig_req = NULL;
1214 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1223 * Function name: twa_map_load_data_callback
1224 * Description: Callback of bus_dmamap_load for the buffer associated
1225 * with data. Updates the cmd pkt (size/sgl_entries
1226 * fields, as applicable) to reflect the number of sg
1229 * Input: arg -- ptr to OSL internal request context
1230 * segs -- ptr to a list of segment descriptors
1231 * nsegments--# of segments
1232 * error -- 0 if no errors encountered before callback,
1233 * non-zero if errors were encountered
1235 * Return value: None
1238 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1239 TW_INT32 nsegments, TW_INT32 error)
1241 struct tw_osli_req_context *req =
1242 (struct tw_osli_req_context *)arg;
1243 struct twa_softc *sc = req->ctlr;
1244 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1246 tw_osli_dbg_dprintf(10, sc, "entered");
1248 /* Mark the request as currently being processed. */
1249 req->state = TW_OSLI_REQ_STATE_BUSY;
1250 /* Move the request into the busy queue. */
1251 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1253 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1254 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1255 tw_osli_allow_new_requests(sc, (TW_VOID *)(req->orig_req));
1257 if (error == EFBIG) {
1258 req->error_code = error;
1262 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1263 struct tw_cl_passthru_req_packet *pt_req;
1265 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1266 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1267 BUS_DMASYNC_PREREAD);
1269 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1271 * If we're using an alignment buffer, and we're
1272 * writing data, copy the real data out.
1274 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1275 bcopy(req->real_data, req->data, req->real_length);
1276 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1277 BUS_DMASYNC_PREWRITE);
1280 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1281 pt_req->sg_list = (TW_UINT8 *)segs;
1282 pt_req->sgl_entries += (nsegments - 1);
1283 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1284 &(req->req_handle));
1286 struct tw_cl_scsi_req_packet *scsi_req;
1288 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1289 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1290 BUS_DMASYNC_PREREAD);
1292 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1294 * If we're using an alignment buffer, and we're
1295 * writing data, copy the real data out.
1297 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1298 bcopy(req->real_data, req->data, req->real_length);
1299 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1300 BUS_DMASYNC_PREWRITE);
1303 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1304 scsi_req->sg_list = (TW_UINT8 *)segs;
1305 scsi_req->sgl_entries += (nsegments - 1);
1306 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1307 &(req->req_handle));
1312 req->error_code = error;
1313 req_pkt->tw_osl_callback(&(req->req_handle));
1315 * If the caller had been returned EINPROGRESS, and he has
1316 * registered a callback for handling completion, the callback
1317 * will never get called because we were unable to submit the
1318 * request. So, free up the request right here.
1320 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1321 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1328 * Function name: twa_map_load_callback
1329 * Description: Callback of bus_dmamap_load for the buffer associated
1332 * Input: arg -- ptr to variable to hold phys addr
1333 * segs -- ptr to a list of segment descriptors
1334 * nsegments--# of segments
1335 * error -- 0 if no errors encountered before callback,
1336 * non-zero if errors were encountered
1338 * Return value: None
1341 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1342 TW_INT32 nsegments, TW_INT32 error)
1344 *((bus_addr_t *)arg) = segs[0].ds_addr;
1350 * Function name: tw_osli_map_request
1351 * Description: Maps a cmd pkt and data associated with it, into
1354 * Input: req -- ptr to request pkt
1356 * Return value: 0 -- success
1357 * non-zero-- failure
1360 tw_osli_map_request(struct tw_osli_req_context *req)
1362 struct twa_softc *sc = req->ctlr;
1365 tw_osli_dbg_dprintf(10, sc, "entered");
1367 /* If the command involves data, map that too. */
1368 if (req->data != NULL) {
1370 * It's sufficient for the data pointer to be 4-byte aligned
1371 * to work with 9000. However, if 4-byte aligned addresses
1372 * are passed to bus_dmamap_load, we can get back sg elements
1373 * that are not 512-byte multiples in size. So, we will let
1374 * only those buffers that are 512-byte aligned to pass
1375 * through, and bounce the rest, so as to make sure that we
1376 * always get back sg elements that are 512-byte multiples
1379 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1380 (req->length % sc->sg_size_factor)) {
1381 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1382 /* Save original data pointer and length. */
1383 req->real_data = req->data;
1384 req->real_length = req->length;
1385 req->length = (req->length +
1386 (sc->sg_size_factor - 1)) &
1387 ~(sc->sg_size_factor - 1);
1388 req->data = malloc(req->length, TW_OSLI_MALLOC_CLASS,
1390 if (req->data == NULL) {
1391 tw_osli_printf(sc, "error = %d",
1392 TW_CL_SEVERITY_ERROR_STRING,
1393 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1395 "Failed to allocate memory "
1396 "for bounce buffer",
1398 /* Restore original data pointer and length. */
1399 req->data = req->real_data;
1400 req->length = req->real_length;
1406 * Map the data buffer into bus space and build the SG list.
1408 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1409 /* Lock against multiple simultaneous ioctl calls. */
1410 mtx_lock_spin(sc->io_lock);
1411 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1412 req->data, req->length,
1413 twa_map_load_data_callback, req,
1415 mtx_unlock_spin(sc->io_lock);
1418 * There's only one CAM I/O thread running at a time.
1419 * So, there's no need to hold the io_lock.
1421 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1422 req->data, req->length,
1423 twa_map_load_data_callback, req,
1428 error = req->error_code;
1430 if (error == EINPROGRESS) {
1432 * Specifying sc->io_lock as the lockfuncarg
1433 * in ...tag_create should protect the access
1434 * of ...FLAGS_MAPPED from the callback.
1436 mtx_lock_spin(sc->io_lock);
1437 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED)) {
1439 TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1440 tw_osli_disallow_new_requests(sc);
1442 mtx_unlock_spin(sc->io_lock);
1445 /* Free alignment buffer if it was used. */
1447 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1448 free(req->data, TW_OSLI_MALLOC_CLASS);
1450 * Restore original data pointer
1453 req->data = req->real_data;
1454 req->length = req->real_length;
1460 /* Mark the request as currently being processed. */
1461 req->state = TW_OSLI_REQ_STATE_BUSY;
1462 /* Move the request into the busy queue. */
1463 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1464 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1465 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1466 &(req->req_pkt), &(req->req_handle));
1468 error = tw_cl_start_io(&sc->ctlr_handle,
1469 &(req->req_pkt), &(req->req_handle));
1471 req->error_code = error;
1472 req->req_pkt.tw_osl_callback(&(req->req_handle));
1481 * Function name: tw_osli_unmap_request
1482 * Description: Undoes the mapping done by tw_osli_map_request.
1484 * Input: req -- ptr to request pkt
1486 * Return value: None
1489 tw_osli_unmap_request(struct tw_osli_req_context *req)
1491 struct twa_softc *sc = req->ctlr;
1493 tw_osli_dbg_dprintf(10, sc, "entered");
1495 /* If the command involved data, unmap that too. */
1496 if (req->data != NULL) {
1497 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1498 /* Lock against multiple simultaneous ioctl calls. */
1499 mtx_lock_spin(sc->io_lock);
1501 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1502 bus_dmamap_sync(sc->ioctl_tag,
1503 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1506 * If we are using a bounce buffer, and we are
1507 * reading data, copy the real data in.
1509 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1510 bcopy(req->data, req->real_data,
1514 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1515 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1516 BUS_DMASYNC_POSTWRITE);
1518 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1520 mtx_unlock_spin(sc->io_lock);
1522 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1523 bus_dmamap_sync(sc->dma_tag,
1524 req->dma_map, BUS_DMASYNC_POSTREAD);
1527 * If we are using a bounce buffer, and we are
1528 * reading data, copy the real data in.
1530 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1531 bcopy(req->data, req->real_data,
1534 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1535 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1536 BUS_DMASYNC_POSTWRITE);
1538 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1542 /* Free alignment buffer if it was used. */
1543 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1544 free(req->data, TW_OSLI_MALLOC_CLASS);
1545 /* Restore original data pointer and length. */
1546 req->data = req->real_data;
1547 req->length = req->real_length;
1555 TW_VOID twa_report_stats(TW_VOID);
1556 TW_VOID twa_reset_stats(TW_VOID);
1557 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1558 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1562 * Function name: twa_report_stats
1563 * Description: For being called from ddb. Calls functions that print
1564 * OSL and CL internal stats for the controller.
1568 * Return value: None
1571 twa_report_stats(TW_VOID)
1573 struct twa_softc *sc;
1576 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1577 tw_osli_print_ctlr_stats(sc);
1578 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1585 * Function name: tw_osli_print_ctlr_stats
1586 * Description: For being called from ddb. Prints OSL controller stats
1588 * Input: sc -- ptr to OSL internal controller context
1590 * Return value: None
1593 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1595 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1596 twa_printf(sc, "OSLq type current max\n");
1597 twa_printf(sc, "free %04d %04d\n",
1598 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1599 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1600 twa_printf(sc, "busy %04d %04d\n",
1601 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1602 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1608 * Function name: twa_print_req_info
1609 * Description: For being called from ddb. Calls functions that print
1610 * OSL and CL internal details for the request.
1612 * Input: req -- ptr to OSL internal request context
1614 * Return value: None
1617 twa_print_req_info(struct tw_osli_req_context *req)
1619 struct twa_softc *sc = req->ctlr;
1621 twa_printf(sc, "OSL details for request:\n");
1622 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1623 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1624 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1625 "next_req = %p, prev_req = %p, dma_map = %p\n",
1626 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1627 req->data, req->length, req->real_data, req->real_length,
1628 req->state, req->flags, req->error_code, req->orig_req,
1629 req->link.next, req->link.prev, req->dma_map);
1630 tw_cl_print_req_info(&(req->req_handle));
1636 * Function name: twa_reset_stats
1637 * Description: For being called from ddb.
1638 * Resets some OSL controller stats.
1642 * Return value: None
1645 twa_reset_stats(TW_VOID)
1647 struct twa_softc *sc;
1650 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1651 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1652 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1653 tw_cl_reset_stats(&sc->ctlr_handle);
1657 #endif /* TW_OSL_DEBUG */