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: head/sys/dev/twa/tw_osl_freebsd.c 254263 2013-08-12 23:30:01Z scottl $
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 static 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 = {
65 { "twa", 0, D_MPSAFE },
71 static devclass_t twa_devclass;
73 static int twa_msi_enable = 0;
74 TUNABLE_INT("hw.twa.msi.enable", &twa_msi_enable);
78 * Function name: twa_open
79 * Description: Called when the controller is opened.
80 * Simply marks the controller as open.
82 * Input: dev -- control device corresponding to the ctlr
83 * flags -- mode of open
84 * fmt -- device type (character/block etc.)
85 * proc -- current process
87 * Return value: 0 -- success
91 twa_open(struct dev_open_args *ap)
93 cdev_t dev = ap->a_head.a_dev;
94 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
96 tw_osli_dbg_dprintf(5, sc, "entered");
97 sc->open = TW_CL_TRUE;
104 * Function name: twa_close
105 * Description: Called when the controller is closed.
106 * Simply marks the controller as not open.
108 * Input: dev -- control device corresponding to the ctlr
109 * flags -- mode of corresponding open
110 * fmt -- device type (character/block etc.)
111 * proc -- current process
113 * Return value: 0 -- success
117 twa_close(struct dev_close_args *ap)
119 cdev_t dev = ap->a_head.a_dev;
120 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
122 tw_osli_dbg_dprintf(5, sc, "entered");
123 sc->open = TW_CL_FALSE;
130 * Function name: twa_ioctl
131 * Description: Called when an ioctl is posted to the controller.
132 * Handles any OS Layer specific cmds, passes the rest
133 * on to the Common Layer.
135 * Input: dev -- control device corresponding to the ctlr
137 * buf -- ptr to buffer in kernel memory, which is
138 * a copy of the input buffer in user-space
139 * flags -- mode of corresponding open
140 * proc -- current process
141 * Output: buf -- ptr to buffer in kernel memory, which will
142 * be copied to the output buffer in user-space
143 * Return value: 0 -- success
147 twa_ioctl(struct dev_ioctl_args *ap)
149 cdev_t dev = ap->a_head.a_dev;
150 u_long cmd = ap->a_cmd;
151 caddr_t buf = ap->a_data;
152 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
155 tw_osli_dbg_dprintf(5, sc, "entered");
158 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
159 tw_osli_dbg_dprintf(6, sc, "ioctl: fw_passthru");
160 error = tw_osli_fw_passthru(sc, (TW_INT8 *)buf);
163 case TW_OSL_IOCTL_SCAN_BUS:
164 /* Request CAM for a bus scan. */
165 tw_osli_dbg_dprintf(6, sc, "ioctl: scan bus");
166 error = tw_osli_request_bus_scan(sc);
170 tw_osli_dbg_dprintf(6, sc, "ioctl: 0x%lx", cmd);
171 error = tw_cl_ioctl(&sc->ctlr_handle, cmd, buf);
179 static TW_INT32 twa_probe(device_t dev);
180 static TW_INT32 twa_attach(device_t dev);
181 static TW_INT32 twa_detach(device_t dev);
182 static TW_INT32 twa_shutdown(device_t dev);
183 static TW_VOID twa_pci_intr(TW_VOID *arg);
184 static TW_VOID twa_watchdog(TW_VOID *arg);
185 int twa_setup_intr(struct twa_softc *sc);
186 int twa_teardown_intr(struct twa_softc *sc);
188 static TW_INT32 tw_osli_alloc_mem(struct twa_softc *sc);
189 static TW_VOID tw_osli_free_resources(struct twa_softc *sc);
191 static TW_VOID twa_map_load_data_callback(TW_VOID *arg,
192 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
193 static TW_VOID twa_map_load_callback(TW_VOID *arg,
194 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
197 static device_method_t twa_methods[] = {
198 /* Device interface */
199 DEVMETHOD(device_probe, twa_probe),
200 DEVMETHOD(device_attach, twa_attach),
201 DEVMETHOD(device_detach, twa_detach),
202 DEVMETHOD(device_shutdown, twa_shutdown),
207 static driver_t twa_pci_driver = {
210 sizeof(struct twa_softc)
213 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, NULL, NULL);
214 MODULE_DEPEND(twa, cam, 1, 1, 1);
215 MODULE_DEPEND(twa, pci, 1, 1, 1);
216 MODULE_VERSION(twa, 1);
220 * Function name: twa_probe
221 * Description: Called at driver load time. Claims 9000 ctlrs.
223 * Input: dev -- bus device corresponding to the ctlr
225 * Return value: <= 0 -- success
229 twa_probe(device_t dev)
231 static TW_UINT8 first_ctlr = 1;
233 tw_osli_dbg_printf(3, "entered");
235 if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
236 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
237 /* Print the driver version only once. */
239 kprintf("3ware device driver for 9000 series storage "
240 "controllers, version: %s\n",
241 TW_OSL_DRIVER_VERSION_STRING);
249 int twa_setup_intr(struct twa_softc *sc)
253 if (!(sc->intr_handle) && (sc->irq_res)) {
254 error = bus_setup_intr(sc->bus_dev, sc->irq_res,
257 sc, &sc->intr_handle, NULL);
263 int twa_teardown_intr(struct twa_softc *sc)
267 if ((sc->intr_handle) && (sc->irq_res)) {
268 error = bus_teardown_intr(sc->bus_dev,
269 sc->irq_res, sc->intr_handle);
270 sc->intr_handle = NULL;
278 * Function name: twa_attach
279 * Description: Allocates pci resources; updates sc; adds a node to the
280 * sysctl tree to expose the driver version; makes calls
281 * (to the Common Layer) to initialize ctlr, and to
284 * Input: dev -- bus device corresponding to the ctlr
286 * Return value: 0 -- success
290 twa_attach(device_t dev)
292 struct twa_softc *sc = device_get_softc(dev);
294 TW_INT32 bar0_offset;
299 tw_osli_dbg_dprintf(3, sc, "entered");
301 sc->ctlr_handle.osl_ctlr_ctxt = sc;
303 /* Initialize the softc structure. */
305 sc->device_id = pci_get_device(dev);
307 /* Initialize the mutexes right here. */
308 sc->io_lock = &(sc->io_lock_handle);
309 spin_init(sc->io_lock);
310 sc->q_lock = &(sc->q_lock_handle);
311 spin_init(sc->q_lock);
312 sc->sim_lock = &(sc->sim_lock_handle);
313 lockinit(sc->sim_lock, "tw_osl_sim_lock", 0, LK_CANRECURSE);
315 sysctl_ctx_init(&sc->sysctl_ctxt);
316 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
317 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
318 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
319 if (sc->sysctl_tree == NULL) {
320 tw_osli_printf(sc, "error = %d",
321 TW_CL_SEVERITY_ERROR_STRING,
322 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
324 "Cannot add sysctl tree node",
328 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
329 OID_AUTO, "driver_version", CTLFLAG_RD,
330 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
332 /* Force the busmaster enable bit on, in case the BIOS forgot. */
333 pci_enable_busmaster(dev);
335 /* Allocate the PCI register window. */
336 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
337 &bar_num, &bar0_offset, &bar_size))) {
338 tw_osli_printf(sc, "error = %d",
339 TW_CL_SEVERITY_ERROR_STRING,
340 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
342 "Can't get PCI BAR info",
344 tw_osli_free_resources(sc);
347 sc->reg_res_id = PCIR_BARS + bar0_offset;
348 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
349 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
351 tw_osli_printf(sc, "error = %d",
352 TW_CL_SEVERITY_ERROR_STRING,
353 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
355 "Can't allocate register window",
357 tw_osli_free_resources(sc);
360 sc->bus_tag = rman_get_bustag(sc->reg_res);
361 sc->bus_handle = rman_get_bushandle(sc->reg_res);
363 /* Allocate and register our interrupt. */
365 sc->irq_type = pci_alloc_1intr(sc->bus_dev, twa_msi_enable,
366 &sc->irq_res_id, &irq_flags);
367 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
368 &(sc->irq_res_id), 0, ~0, 1,
369 irq_flags)) == NULL) {
370 tw_osli_printf(sc, "error = %d",
371 TW_CL_SEVERITY_ERROR_STRING,
372 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
374 "Can't allocate interrupt",
376 tw_osli_free_resources(sc);
379 if ((error = twa_setup_intr(sc))) {
380 tw_osli_printf(sc, "error = %d",
381 TW_CL_SEVERITY_ERROR_STRING,
382 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
384 "Can't set up interrupt",
386 tw_osli_free_resources(sc);
390 if ((error = tw_osli_alloc_mem(sc))) {
391 tw_osli_printf(sc, "error = %d",
392 TW_CL_SEVERITY_ERROR_STRING,
393 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
395 "Memory allocation failure",
397 tw_osli_free_resources(sc);
401 /* Initialize the Common Layer for this controller. */
402 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
403 TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
404 sc->non_dma_mem, sc->dma_mem,
407 tw_osli_printf(sc, "error = %d",
408 TW_CL_SEVERITY_ERROR_STRING,
409 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
411 "Failed to initialize Common Layer/controller",
413 tw_osli_free_resources(sc);
417 /* Create the control device. */
418 sc->ctrl_dev = make_dev(&twa_ops, device_get_unit(sc->bus_dev),
419 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
420 "twa%d", device_get_unit(sc->bus_dev));
421 sc->ctrl_dev->si_drv1 = sc;
423 if ((error = tw_osli_cam_attach(sc))) {
424 tw_osli_free_resources(sc);
425 tw_osli_printf(sc, "error = %d",
426 TW_CL_SEVERITY_ERROR_STRING,
427 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
429 "Failed to initialize CAM",
434 sc->watchdog_index = 0;
435 callout_init_mp(&(sc->watchdog_callout[0]));
436 callout_init_mp(&(sc->watchdog_callout[1]));
437 callout_reset(&(sc->watchdog_callout[0]), 5*hz, twa_watchdog, &sc->ctlr_handle);
444 twa_watchdog(TW_VOID *arg)
446 struct tw_cl_ctlr_handle *ctlr_handle =
447 (struct tw_cl_ctlr_handle *)arg;
448 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
450 int i_need_a_reset = 0;
451 int driver_is_active = 0;
452 TW_UINT64 current_time;
453 struct tw_osli_req_context *my_req;
456 //==============================================================================
457 current_time = (TW_UINT64) (tw_osl_get_local_time());
459 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
460 my_req = &(sc->req_ctx_buf[i]);
462 if ((my_req->state == TW_OSLI_REQ_STATE_BUSY) &&
463 (my_req->deadline) &&
464 (my_req->deadline < current_time)) {
465 tw_cl_set_reset_needed(ctlr_handle);
467 device_printf((sc)->bus_dev, "Request %d timed out! d = %llu, c = %llu\n", i, my_req->deadline, current_time);
468 #else /* TW_OSL_DEBUG */
469 device_printf((sc)->bus_dev, "Request %d timed out!\n", i);
470 #endif /* TW_OSL_DEBUG */
474 //==============================================================================
476 i_need_a_reset = tw_cl_is_reset_needed(ctlr_handle);
478 i = (int) ((sc->watchdog_index++) & 1);
480 driver_is_active = tw_cl_is_active(ctlr_handle);
482 if (i_need_a_reset) {
484 device_printf((sc)->bus_dev, "Watchdog rescheduled in 70 seconds\n");
485 #endif /* TW_OSL_DEBUG */
486 callout_reset(&(sc->watchdog_callout[i]), 70*hz, twa_watchdog, &sc->ctlr_handle);
487 tw_cl_reset_ctlr(ctlr_handle);
489 device_printf((sc)->bus_dev, "Watchdog reset completed!\n");
490 #endif /* TW_OSL_DEBUG */
491 } else if (driver_is_active) {
492 callout_reset(&(sc->watchdog_callout[i]), 5*hz, twa_watchdog, &sc->ctlr_handle);
496 device_printf((sc)->bus_dev, "i_need_a_reset = %d, "
497 "driver_is_active = %d\n",
498 i_need_a_reset, driver_is_active);
499 #endif /* TW_OSL_DEBUG */
504 * Function name: tw_osli_alloc_mem
505 * Description: Allocates memory needed both by CL and OSL.
507 * Input: sc -- OSL internal controller context
509 * Return value: 0 -- success
513 tw_osli_alloc_mem(struct twa_softc *sc)
515 struct tw_osli_req_context *req;
516 TW_UINT32 max_sg_elements;
517 TW_UINT32 non_dma_mem_size;
518 TW_UINT32 dma_mem_size;
522 tw_osli_dbg_dprintf(3, sc, "entered");
524 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
525 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
527 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
528 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
530 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
531 sc->device_id, TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
532 &(sc->alignment), &(sc->sg_size_factor),
533 &non_dma_mem_size, &dma_mem_size
535 tw_osli_printf(sc, "error = %d",
536 TW_CL_SEVERITY_ERROR_STRING,
537 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
539 "Can't get Common Layer's memory requirements",
544 sc->non_dma_mem = kmalloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
547 /* Create the parent dma tag. */
548 if (bus_dma_tag_create(NULL, /* parent */
549 sc->alignment, /* alignment */
550 TW_OSLI_DMA_BOUNDARY, /* boundary */
551 BUS_SPACE_MAXADDR, /* lowaddr */
552 BUS_SPACE_MAXADDR, /* highaddr */
553 NULL, NULL, /* filter, filterarg */
554 TW_CL_MAX_IO_SIZE, /* maxsize */
555 max_sg_elements, /* nsegments */
556 TW_CL_MAX_IO_SIZE, /* maxsegsize */
558 &sc->parent_tag /* tag */)) {
559 tw_osli_printf(sc, "error = %d",
560 TW_CL_SEVERITY_ERROR_STRING,
561 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
563 "Can't allocate parent DMA tag",
568 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
569 if (bus_dma_tag_create(sc->parent_tag, /* parent */
570 sc->alignment, /* alignment */
572 BUS_SPACE_MAXADDR, /* lowaddr */
573 BUS_SPACE_MAXADDR, /* highaddr */
574 NULL, NULL, /* filter, filterarg */
575 dma_mem_size, /* maxsize */
577 BUS_SPACE_MAXSIZE, /* maxsegsize */
579 &sc->cmd_tag /* tag */)) {
580 tw_osli_printf(sc, "error = %d",
581 TW_CL_SEVERITY_ERROR_STRING,
582 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
584 "Can't allocate DMA tag for Common Layer's "
590 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
591 BUS_DMA_NOWAIT, &sc->cmd_map)) {
592 /* Try a second time. */
593 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
594 BUS_DMA_NOWAIT, &sc->cmd_map)) {
595 tw_osli_printf(sc, "error = %d",
596 TW_CL_SEVERITY_ERROR_STRING,
597 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
599 "Can't allocate DMA'able memory for the"
606 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
607 dma_mem_size, twa_map_load_callback,
608 &sc->dma_mem_phys, 0);
611 * Create a dma tag for data buffers; size will be the maximum
612 * possible I/O size (128kB).
614 if (bus_dma_tag_create(sc->parent_tag, /* parent */
615 sc->alignment, /* alignment */
617 BUS_SPACE_MAXADDR, /* lowaddr */
618 BUS_SPACE_MAXADDR, /* highaddr */
619 NULL, NULL, /* filter, filterarg */
620 TW_CL_MAX_IO_SIZE, /* maxsize */
621 max_sg_elements, /* nsegments */
622 TW_CL_MAX_IO_SIZE, /* maxsegsize */
623 BUS_DMA_ALLOCNOW, /* flags */
624 &sc->dma_tag /* tag */)) {
625 tw_osli_printf(sc, "error = %d",
626 TW_CL_SEVERITY_ERROR_STRING,
627 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
629 "Can't allocate DMA tag for data buffers",
635 * Create a dma tag for ioctl data buffers; size will be the maximum
636 * possible I/O size (128kB).
638 if (bus_dma_tag_create(sc->parent_tag, /* parent */
639 sc->alignment, /* alignment */
641 BUS_SPACE_MAXADDR, /* lowaddr */
642 BUS_SPACE_MAXADDR, /* highaddr */
643 NULL, NULL, /* filter, filterarg */
644 TW_CL_MAX_IO_SIZE, /* maxsize */
645 max_sg_elements, /* nsegments */
646 TW_CL_MAX_IO_SIZE, /* maxsegsize */
647 BUS_DMA_ALLOCNOW, /* flags */
648 &sc->ioctl_tag /* tag */)) {
649 tw_osli_printf(sc, "error = %d",
650 TW_CL_SEVERITY_ERROR_STRING,
651 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
653 "Can't allocate DMA tag for ioctl data buffers",
658 /* Create just one map for all ioctl request data buffers. */
659 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
660 tw_osli_printf(sc, "error = %d",
661 TW_CL_SEVERITY_ERROR_STRING,
662 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
664 "Can't create ioctl map",
670 /* Initialize request queues. */
671 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
672 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
674 sc->req_ctx_buf = kmalloc((sizeof(struct tw_osli_req_context) *
675 TW_OSLI_MAX_NUM_REQUESTS), TW_OSLI_MALLOC_CLASS,
677 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
678 req = &(sc->req_ctx_buf[i]);
680 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
681 tw_osli_printf(sc, "request # = %d, error = %d",
682 TW_CL_SEVERITY_ERROR_STRING,
683 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
685 "Can't create dma map",
690 /* Initialize the ioctl wakeup/ timeout mutex */
691 req->ioctl_wake_timeout_lock = &(req->ioctl_wake_timeout_lock_handle);
692 lockinit(req->ioctl_wake_timeout_lock, "tw_ioctl_wake_timeout_lock", 0, 0);
694 /* Insert request into the free queue. */
695 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
704 * Function name: tw_osli_free_resources
705 * Description: Performs clean-up at the time of going down.
707 * Input: sc -- ptr to OSL internal ctlr context
712 tw_osli_free_resources(struct twa_softc *sc)
714 struct tw_osli_req_context *req;
717 tw_osli_dbg_dprintf(3, sc, "entered");
719 /* Detach from CAM */
720 tw_osli_cam_detach(sc);
723 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
725 lockuninit(req->ioctl_wake_timeout_lock);
727 if ((error = bus_dmamap_destroy(sc->dma_tag,
729 tw_osli_dbg_dprintf(1, sc,
730 "dmamap_destroy(dma) returned %d",
734 if ((sc->ioctl_tag) && (sc->ioctl_map))
735 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
736 tw_osli_dbg_dprintf(1, sc,
737 "dmamap_destroy(ioctl) returned %d", error);
739 /* Free all memory allocated so far. */
741 kfree(sc->req_ctx_buf, TW_OSLI_MALLOC_CLASS);
744 kfree(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
747 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
748 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
752 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
753 tw_osli_dbg_dprintf(1, sc,
754 "dma_tag_destroy(cmd) returned %d", error);
757 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
758 tw_osli_dbg_dprintf(1, sc,
759 "dma_tag_destroy(dma) returned %d", error);
762 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
763 tw_osli_dbg_dprintf(1, sc,
764 "dma_tag_destroy(ioctl) returned %d", error);
767 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
768 tw_osli_dbg_dprintf(1, sc,
769 "dma_tag_destroy(parent) returned %d", error);
772 /* Disconnect the interrupt handler. */
773 if ((error = twa_teardown_intr(sc)))
774 tw_osli_dbg_dprintf(1, sc,
775 "teardown_intr returned %d", error);
777 if (sc->irq_res != NULL)
778 if ((error = bus_release_resource(sc->bus_dev,
779 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
780 tw_osli_dbg_dprintf(1, sc,
781 "release_resource(irq) returned %d", error);
783 if (sc->irq_type == PCI_INTR_TYPE_MSI)
784 pci_release_msi(sc->bus_dev);
786 /* Release the register window mapping. */
787 if (sc->reg_res != NULL)
788 if ((error = bus_release_resource(sc->bus_dev,
789 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
790 tw_osli_dbg_dprintf(1, sc,
791 "release_resource(io) returned %d", error);
793 /* Destroy the control device. */
794 if (sc->ctrl_dev != NULL)
795 destroy_dev(sc->ctrl_dev);
796 dev_ops_remove_minor(&twa_ops, device_get_unit(sc->bus_dev));
798 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
799 tw_osli_dbg_dprintf(1, sc,
800 "sysctl_ctx_free returned %d", error);
807 * Function name: twa_detach
808 * Description: Called when the controller is being detached from
811 * Input: dev -- bus device corresponding to the ctlr
813 * Return value: 0 -- success
817 twa_detach(device_t dev)
819 struct twa_softc *sc = device_get_softc(dev);
822 tw_osli_dbg_dprintf(3, sc, "entered");
826 tw_osli_printf(sc, "error = %d",
827 TW_CL_SEVERITY_ERROR_STRING,
828 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
835 /* Shut the controller down. */
836 if ((error = twa_shutdown(dev)))
839 /* Free all resources associated with this controller. */
840 tw_osli_free_resources(sc);
850 * Function name: twa_shutdown
851 * Description: Called at unload/shutdown time. Lets the controller
852 * know that we are going down.
854 * Input: dev -- bus device corresponding to the ctlr
856 * Return value: 0 -- success
860 twa_shutdown(device_t dev)
862 struct twa_softc *sc = device_get_softc(dev);
865 tw_osli_dbg_dprintf(3, sc, "entered");
867 /* Disconnect interrupts. */
868 error = twa_teardown_intr(sc);
870 /* Stop watchdog task. */
871 callout_stop_sync(&(sc->watchdog_callout[0]));
872 callout_stop_sync(&(sc->watchdog_callout[1]));
874 /* Disconnect from the controller. */
875 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
876 tw_osli_printf(sc, "error = %d",
877 TW_CL_SEVERITY_ERROR_STRING,
878 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
880 "Failed to shutdown Common Layer/controller",
889 * Function name: twa_pci_intr
890 * Description: Interrupt handler. Wrapper for twa_interrupt.
892 * Input: arg -- ptr to OSL internal ctlr context
897 twa_pci_intr(TW_VOID *arg)
899 struct twa_softc *sc = (struct twa_softc *)arg;
901 tw_osli_dbg_dprintf(10, sc, "entered");
902 tw_cl_interrupt(&(sc->ctlr_handle));
907 * Function name: tw_osli_fw_passthru
908 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
910 * Input: sc -- ptr to OSL internal ctlr context
911 * buf -- ptr to ioctl pkt understood by CL
913 * Return value: 0 -- success
917 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
919 struct tw_osli_req_context *req;
920 struct tw_osli_ioctl_no_data_buf *user_buf =
921 (struct tw_osli_ioctl_no_data_buf *)buf;
923 TW_UINT32 timeout = 60;
924 TW_UINT32 data_buf_size_adjusted;
925 struct tw_cl_req_packet *req_pkt;
926 struct tw_cl_passthru_req_packet *pt_req;
929 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
931 if ((req = tw_osli_get_request(sc)) == NULL)
934 req->req_handle.osl_req_ctxt = req;
936 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
938 req_pkt = &(req->req_pkt);
940 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
941 /* Let the Common Layer retry the request on cmd queue full. */
942 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
944 pt_req = &(req_pkt->gen_req_pkt.pt_req);
946 * Make sure that the data buffer sent to firmware is a
947 * 512 byte multiple in size.
949 data_buf_size_adjusted =
950 (user_buf->driver_pkt.buffer_length +
951 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
952 if ((req->length = data_buf_size_adjusted)) {
953 req->data = kmalloc(data_buf_size_adjusted,
954 TW_OSLI_MALLOC_CLASS, M_WAITOK);
955 /* Copy the payload. */
956 if ((error = copyin((TW_VOID *)(user_buf->pdata),
958 user_buf->driver_pkt.buffer_length)) != 0) {
959 tw_osli_printf(sc, "error = %d",
960 TW_CL_SEVERITY_ERROR_STRING,
961 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
963 "Could not copyin fw_passthru data_buf",
965 goto fw_passthru_err;
967 pt_req->sgl_entries = 1; /* will be updated during mapping */
968 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
969 TW_OSLI_REQ_FLAGS_DATA_OUT);
971 pt_req->sgl_entries = 0; /* no payload */
973 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
974 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
976 if ((error = tw_osli_map_request(req)))
977 goto fw_passthru_err;
979 end_time = tw_osl_get_local_time() + timeout;
980 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
981 lockmgr(req->ioctl_wake_timeout_lock, LK_EXCLUSIVE);
982 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
984 error = lksleep(req, req->ioctl_wake_timeout_lock, 0,
985 "twa_passthru", timeout*hz);
986 lockmgr(req->ioctl_wake_timeout_lock, LK_RELEASE);
988 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
990 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
993 if (((error = req->error_code)) ||
994 ((error = (req->state !=
995 TW_OSLI_REQ_STATE_COMPLETE))) ||
996 ((error = req_pkt->status)))
997 goto fw_passthru_err;
1001 if (req_pkt->status) {
1002 error = req_pkt->status;
1003 goto fw_passthru_err;
1006 if (error == EWOULDBLOCK) {
1008 if ((!(req->error_code)) &&
1009 (req->state == TW_OSLI_REQ_STATE_COMPLETE) &&
1010 (!(req_pkt->status)) ) {
1012 tw_osli_printf(sc, "request = %p",
1013 TW_CL_SEVERITY_ERROR_STRING,
1014 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1016 "FALSE Passthru timeout!",
1018 #endif /* TW_OSL_DEBUG */
1019 error = 0; /* False error */
1022 if (!(tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
1024 tw_osli_printf(sc, "request = %p",
1025 TW_CL_SEVERITY_ERROR_STRING,
1026 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1028 "Passthru request timed out!",
1030 #else /* TW_OSL_DEBUG */
1031 device_printf((sc)->bus_dev, "Passthru request timed out!\n");
1032 #endif /* TW_OSL_DEBUG */
1033 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
1037 end_time = tw_osl_get_local_time() + timeout;
1040 * Don't touch req after a reset. It (and any
1041 * associated data) will be
1042 * unmapped by the callback.
1046 * Either the request got completed, or we were woken up by a
1047 * signal. Calculate the new timeout, in case it was the latter.
1049 timeout = (end_time - tw_osl_get_local_time());
1050 } /* End of while loop */
1052 /* If there was a payload, copy it back. */
1053 if ((!error) && (req->length))
1054 if ((error = copyout(req->data, user_buf->pdata,
1055 user_buf->driver_pkt.buffer_length)))
1056 tw_osli_printf(sc, "error = %d",
1057 TW_CL_SEVERITY_ERROR_STRING,
1058 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1060 "Could not copyout fw_passthru data_buf",
1065 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1068 user_buf->driver_pkt.os_status = error;
1069 /* Free resources. */
1071 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1072 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1079 * Function name: tw_osl_complete_passthru
1080 * Description: Called to complete passthru requests.
1082 * Input: req_handle -- ptr to request handle
1084 * Return value: None
1087 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1089 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1090 struct tw_cl_req_packet *req_pkt =
1091 (struct tw_cl_req_packet *)(&req->req_pkt);
1092 struct twa_softc *sc = req->ctlr;
1094 tw_osli_dbg_dprintf(5, sc, "entered");
1096 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1097 tw_osli_printf(sc, "request = %p, status = %d",
1098 TW_CL_SEVERITY_ERROR_STRING,
1099 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1101 "Unposted command completed!!",
1106 * Remove request from the busy queue. Just mark it complete.
1107 * There's no need to move it into the complete queue as we are
1108 * going to be done with it right now.
1110 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1111 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1113 tw_osli_unmap_request(req);
1116 * Don't do a wake up if there was an error even before the request
1117 * was sent down to the Common Layer, and we hadn't gotten an
1118 * EINPROGRESS. The request originator will then be returned an
1119 * error, and he can do the clean-up.
1121 if ((req->error_code) && (!(req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1124 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1125 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1126 /* Wake up the sleeping command originator. */
1127 tw_osli_dbg_dprintf(5, sc,
1128 "Waking up originator of request %p", req);
1129 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1133 * If the request completed even before mtx_sleep
1134 * was called, simply return.
1136 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1139 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1142 tw_osli_printf(sc, "request = %p",
1143 TW_CL_SEVERITY_ERROR_STRING,
1144 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1146 "Passthru callback called, "
1147 "and caller not sleeping",
1151 tw_osli_printf(sc, "request = %p",
1152 TW_CL_SEVERITY_ERROR_STRING,
1153 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1155 "Passthru callback called for non-passthru request",
1163 * Function name: tw_osli_get_request
1164 * Description: Gets a request pkt from the free queue.
1166 * Input: sc -- ptr to OSL internal ctlr context
1168 * Return value: ptr to request pkt -- success
1171 struct tw_osli_req_context *
1172 tw_osli_get_request(struct twa_softc *sc)
1174 struct tw_osli_req_context *req;
1176 tw_osli_dbg_dprintf(4, sc, "entered");
1178 /* Get a free request packet. */
1179 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1181 /* Initialize some fields to their defaults. */
1183 req->req_handle.osl_req_ctxt = NULL;
1184 req->req_handle.cl_req_ctxt = NULL;
1185 req->req_handle.is_io = 0;
1189 req->real_data = NULL;
1190 req->real_length = 0;
1191 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1193 req->error_code = 0;
1194 req->orig_req = NULL;
1196 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1205 * Function name: twa_map_load_data_callback
1206 * Description: Callback of bus_dmamap_load for the buffer associated
1207 * with data. Updates the cmd pkt (size/sgl_entries
1208 * fields, as applicable) to reflect the number of sg
1211 * Input: arg -- ptr to OSL internal request context
1212 * segs -- ptr to a list of segment descriptors
1213 * nsegments--# of segments
1214 * error -- 0 if no errors encountered before callback,
1215 * non-zero if errors were encountered
1217 * Return value: None
1220 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1221 TW_INT32 nsegments, TW_INT32 error)
1223 struct tw_osli_req_context *req =
1224 (struct tw_osli_req_context *)arg;
1225 struct twa_softc *sc = req->ctlr;
1226 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1228 tw_osli_dbg_dprintf(10, sc, "entered");
1230 if (error == EINVAL) {
1231 req->error_code = error;
1235 /* Mark the request as currently being processed. */
1236 req->state = TW_OSLI_REQ_STATE_BUSY;
1237 /* Move the request into the busy queue. */
1238 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1240 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1242 if (error == EFBIG) {
1243 req->error_code = error;
1247 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1248 struct tw_cl_passthru_req_packet *pt_req;
1250 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1251 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1252 BUS_DMASYNC_PREREAD);
1254 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1256 * If we're using an alignment buffer, and we're
1257 * writing data, copy the real data out.
1259 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1260 bcopy(req->real_data, req->data, req->real_length);
1261 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1262 BUS_DMASYNC_PREWRITE);
1265 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1266 pt_req->sg_list = (TW_UINT8 *)segs;
1267 pt_req->sgl_entries += (nsegments - 1);
1268 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1269 &(req->req_handle));
1271 struct tw_cl_scsi_req_packet *scsi_req;
1273 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1274 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1275 BUS_DMASYNC_PREREAD);
1277 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1279 * If we're using an alignment buffer, and we're
1280 * writing data, copy the real data out.
1282 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1283 bcopy(req->real_data, req->data, req->real_length);
1284 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1285 BUS_DMASYNC_PREWRITE);
1288 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1289 scsi_req->sg_list = (TW_UINT8 *)segs;
1290 scsi_req->sgl_entries += (nsegments - 1);
1291 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1292 &(req->req_handle));
1297 req->error_code = error;
1298 req_pkt->tw_osl_callback(&(req->req_handle));
1300 * If the caller had been returned EINPROGRESS, and he has
1301 * registered a callback for handling completion, the callback
1302 * will never get called because we were unable to submit the
1303 * request. So, free up the request right here.
1305 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1306 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1313 * Function name: twa_map_load_callback
1314 * Description: Callback of bus_dmamap_load for the buffer associated
1317 * Input: arg -- ptr to variable to hold phys addr
1318 * segs -- ptr to a list of segment descriptors
1319 * nsegments--# of segments
1320 * error -- 0 if no errors encountered before callback,
1321 * non-zero if errors were encountered
1323 * Return value: None
1326 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1327 TW_INT32 nsegments, TW_INT32 error)
1329 *((bus_addr_t *)arg) = segs[0].ds_addr;
1335 * Function name: tw_osli_map_request
1336 * Description: Maps a cmd pkt and data associated with it, into
1339 * Input: req -- ptr to request pkt
1341 * Return value: 0 -- success
1342 * non-zero-- failure
1345 tw_osli_map_request(struct tw_osli_req_context *req)
1347 struct twa_softc *sc = req->ctlr;
1350 tw_osli_dbg_dprintf(10, sc, "entered");
1352 /* If the command involves data, map that too. */
1353 if (req->data != NULL) {
1355 * It's sufficient for the data pointer to be 4-byte aligned
1356 * to work with 9000. However, if 4-byte aligned addresses
1357 * are passed to bus_dmamap_load, we can get back sg elements
1358 * that are not 512-byte multiples in size. So, we will let
1359 * only those buffers that are 512-byte aligned to pass
1360 * through, and bounce the rest, so as to make sure that we
1361 * always get back sg elements that are 512-byte multiples
1364 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1365 (req->length % sc->sg_size_factor)) {
1366 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1367 /* Save original data pointer and length. */
1368 req->real_data = req->data;
1369 req->real_length = req->length;
1370 req->length = (req->length +
1371 (sc->sg_size_factor - 1)) &
1372 ~(sc->sg_size_factor - 1);
1373 req->data = kmalloc(req->length, TW_OSLI_MALLOC_CLASS,
1375 if (req->data == NULL) {
1376 tw_osli_printf(sc, "error = %d",
1377 TW_CL_SEVERITY_ERROR_STRING,
1378 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1380 "Failed to allocate memory "
1381 "for bounce buffer",
1383 /* Restore original data pointer and length. */
1384 req->data = req->real_data;
1385 req->length = req->real_length;
1391 * Map the data buffer into bus space and build the SG list.
1393 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1394 /* Lock against multiple simultaneous ioctl calls. */
1395 spin_lock(sc->io_lock);
1396 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1397 req->data, req->length,
1398 twa_map_load_data_callback, req,
1400 spin_unlock(sc->io_lock);
1403 * There's only one CAM I/O thread running at a time.
1404 * So, there's no need to hold the io_lock.
1406 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1407 req->data, req->length,
1408 twa_map_load_data_callback, req,
1413 error = req->error_code;
1415 if (error == EINPROGRESS) {
1417 * Specifying sc->io_lock as the lockfuncarg
1418 * in ...tag_create should protect the access
1419 * of ...FLAGS_MAPPED from the callback.
1421 spin_lock(sc->io_lock);
1422 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED))
1423 req->flags |= TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1424 tw_osli_disallow_new_requests(sc, &(req->req_handle));
1425 spin_unlock(sc->io_lock);
1428 tw_osli_printf(sc, "error = %d",
1429 TW_CL_SEVERITY_ERROR_STRING,
1430 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1432 "Failed to map DMA memory "
1435 req->flags |= TW_OSLI_REQ_FLAGS_FAILED;
1436 /* Free alignment buffer if it was used. */
1438 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1439 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1441 * Restore original data pointer
1444 req->data = req->real_data;
1445 req->length = req->real_length;
1451 /* Mark the request as currently being processed. */
1452 req->state = TW_OSLI_REQ_STATE_BUSY;
1453 /* Move the request into the busy queue. */
1454 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1455 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1456 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1457 &(req->req_pkt), &(req->req_handle));
1459 error = tw_cl_start_io(&sc->ctlr_handle,
1460 &(req->req_pkt), &(req->req_handle));
1462 req->error_code = error;
1463 req->req_pkt.tw_osl_callback(&(req->req_handle));
1472 * Function name: tw_osli_unmap_request
1473 * Description: Undoes the mapping done by tw_osli_map_request.
1475 * Input: req -- ptr to request pkt
1477 * Return value: None
1480 tw_osli_unmap_request(struct tw_osli_req_context *req)
1482 struct twa_softc *sc = req->ctlr;
1484 tw_osli_dbg_dprintf(10, sc, "entered");
1486 /* If the command involved data, unmap that too. */
1487 if (req->data != NULL) {
1488 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1489 /* Lock against multiple simultaneous ioctl calls. */
1490 spin_lock(sc->io_lock);
1492 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1493 bus_dmamap_sync(sc->ioctl_tag,
1494 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1497 * If we are using a bounce buffer, and we are
1498 * reading data, copy the real data in.
1500 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1501 bcopy(req->data, req->real_data,
1505 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1506 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1507 BUS_DMASYNC_POSTWRITE);
1509 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1511 spin_unlock(sc->io_lock);
1513 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1514 bus_dmamap_sync(sc->dma_tag,
1515 req->dma_map, BUS_DMASYNC_POSTREAD);
1518 * If we are using a bounce buffer, and we are
1519 * reading data, copy the real data in.
1521 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1522 bcopy(req->data, req->real_data,
1525 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1526 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1527 BUS_DMASYNC_POSTWRITE);
1529 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1533 /* Free alignment buffer if it was used. */
1534 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1535 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1536 /* Restore original data pointer and length. */
1537 req->data = req->real_data;
1538 req->length = req->real_length;
1546 TW_VOID twa_report_stats(TW_VOID);
1547 TW_VOID twa_reset_stats(TW_VOID);
1548 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1549 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1553 * Function name: twa_report_stats
1554 * Description: For being called from ddb. Calls functions that print
1555 * OSL and CL internal stats for the controller.
1559 * Return value: None
1562 twa_report_stats(TW_VOID)
1564 struct twa_softc *sc;
1567 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1568 tw_osli_print_ctlr_stats(sc);
1569 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1576 * Function name: tw_osli_print_ctlr_stats
1577 * Description: For being called from ddb. Prints OSL controller stats
1579 * Input: sc -- ptr to OSL internal controller context
1581 * Return value: None
1584 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1586 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1587 twa_printf(sc, "OSLq type current max\n");
1588 twa_printf(sc, "free %04d %04d\n",
1589 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1590 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1591 twa_printf(sc, "busy %04d %04d\n",
1592 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1593 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1599 * Function name: twa_print_req_info
1600 * Description: For being called from ddb. Calls functions that print
1601 * OSL and CL internal details for the request.
1603 * Input: req -- ptr to OSL internal request context
1605 * Return value: None
1608 twa_print_req_info(struct tw_osli_req_context *req)
1610 struct twa_softc *sc = req->ctlr;
1612 twa_printf(sc, "OSL details for request:\n");
1613 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1614 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1615 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1616 "next_req = %p, prev_req = %p, dma_map = %p\n",
1617 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1618 req->data, req->length, req->real_data, req->real_length,
1619 req->state, req->flags, req->error_code, req->orig_req,
1620 req->link.next, req->link.prev, req->dma_map);
1621 tw_cl_print_req_info(&(req->req_handle));
1627 * Function name: twa_reset_stats
1628 * Description: For being called from ddb.
1629 * Resets some OSL controller stats.
1633 * Return value: None
1636 twa_reset_stats(TW_VOID)
1638 struct twa_softc *sc;
1641 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1642 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1643 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1644 tw_cl_reset_stats(&sc->ctlr_handle);
1648 #endif /* TW_OSL_DEBUG */