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 sc->ctlr_handle.osl_ctlr_ctxt = sc;
301 /* Initialize the softc structure. */
303 tw_osli_dbg_dprintf(3, sc, "entered");
304 sc->device_id = pci_get_device(dev);
306 /* Initialize the mutexes right here. */
307 sc->io_lock = &(sc->io_lock_handle);
308 spin_init(sc->io_lock, "twa_iolock");
309 sc->q_lock = &(sc->q_lock_handle);
310 spin_init(sc->q_lock, "twa_qlock");
311 sc->sim_lock = &(sc->sim_lock_handle);
312 lockinit(sc->sim_lock, "tw_osl_sim_lock", 0, LK_CANRECURSE);
314 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
315 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
316 OID_AUTO, "driver_version", CTLFLAG_RD,
317 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
319 /* Force the busmaster enable bit on, in case the BIOS forgot. */
320 pci_enable_busmaster(dev);
322 /* Allocate the PCI register window. */
323 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
324 &bar_num, &bar0_offset, &bar_size))) {
325 tw_osli_printf(sc, "error = %d",
326 TW_CL_SEVERITY_ERROR_STRING,
327 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
329 "Can't get PCI BAR info",
331 tw_osli_free_resources(sc);
334 sc->reg_res_id = PCIR_BARS + bar0_offset;
335 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
336 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
338 tw_osli_printf(sc, "error = %d",
339 TW_CL_SEVERITY_ERROR_STRING,
340 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
342 "Can't allocate register window",
344 tw_osli_free_resources(sc);
347 sc->bus_tag = rman_get_bustag(sc->reg_res);
348 sc->bus_handle = rman_get_bushandle(sc->reg_res);
350 /* Allocate and register our interrupt. */
352 sc->irq_type = pci_alloc_1intr(sc->bus_dev, twa_msi_enable,
353 &sc->irq_res_id, &irq_flags);
354 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
355 &(sc->irq_res_id), 0, ~0, 1,
356 irq_flags)) == NULL) {
357 tw_osli_printf(sc, "error = %d",
358 TW_CL_SEVERITY_ERROR_STRING,
359 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
361 "Can't allocate interrupt",
363 tw_osli_free_resources(sc);
366 if ((error = twa_setup_intr(sc))) {
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",
421 sc->watchdog_index = 0;
422 callout_init_mp(&(sc->watchdog_callout[0]));
423 callout_init_mp(&(sc->watchdog_callout[1]));
424 callout_reset(&(sc->watchdog_callout[0]), 5*hz, twa_watchdog, &sc->ctlr_handle);
431 twa_watchdog(TW_VOID *arg)
433 struct tw_cl_ctlr_handle *ctlr_handle =
434 (struct tw_cl_ctlr_handle *)arg;
435 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
437 int i_need_a_reset = 0;
438 int driver_is_active = 0;
439 TW_UINT64 current_time;
440 struct tw_osli_req_context *my_req;
443 //==============================================================================
444 current_time = (TW_UINT64) (tw_osl_get_local_time());
446 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
447 my_req = &(sc->req_ctx_buf[i]);
449 if ((my_req->state == TW_OSLI_REQ_STATE_BUSY) &&
450 (my_req->deadline) &&
451 (my_req->deadline < current_time)) {
452 tw_cl_set_reset_needed(ctlr_handle);
454 device_printf((sc)->bus_dev, "Request %d timed out! d = %llu, c = %llu\n", i, my_req->deadline, current_time);
455 #else /* TW_OSL_DEBUG */
456 device_printf((sc)->bus_dev, "Request %d timed out!\n", i);
457 #endif /* TW_OSL_DEBUG */
461 //==============================================================================
463 i_need_a_reset = tw_cl_is_reset_needed(ctlr_handle);
465 i = (int) ((sc->watchdog_index++) & 1);
467 driver_is_active = tw_cl_is_active(ctlr_handle);
469 if (i_need_a_reset) {
471 device_printf((sc)->bus_dev, "Watchdog rescheduled in 70 seconds\n");
472 #endif /* TW_OSL_DEBUG */
473 callout_reset(&(sc->watchdog_callout[i]), 70*hz, twa_watchdog, &sc->ctlr_handle);
474 tw_cl_reset_ctlr(ctlr_handle);
476 device_printf((sc)->bus_dev, "Watchdog reset completed!\n");
477 #endif /* TW_OSL_DEBUG */
478 } else if (driver_is_active) {
479 callout_reset(&(sc->watchdog_callout[i]), 5*hz, twa_watchdog, &sc->ctlr_handle);
483 device_printf((sc)->bus_dev, "i_need_a_reset = %d, "
484 "driver_is_active = %d\n",
485 i_need_a_reset, driver_is_active);
486 #endif /* TW_OSL_DEBUG */
491 * Function name: tw_osli_alloc_mem
492 * Description: Allocates memory needed both by CL and OSL.
494 * Input: sc -- OSL internal controller context
496 * Return value: 0 -- success
500 tw_osli_alloc_mem(struct twa_softc *sc)
502 struct tw_osli_req_context *req;
503 TW_UINT32 max_sg_elements;
504 TW_UINT32 non_dma_mem_size;
505 TW_UINT32 dma_mem_size;
509 tw_osli_dbg_dprintf(3, sc, "entered");
511 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
512 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
514 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
515 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
517 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
518 sc->device_id, TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
519 &(sc->alignment), &(sc->sg_size_factor),
520 &non_dma_mem_size, &dma_mem_size
522 tw_osli_printf(sc, "error = %d",
523 TW_CL_SEVERITY_ERROR_STRING,
524 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
526 "Can't get Common Layer's memory requirements",
531 sc->non_dma_mem = kmalloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
534 /* Create the parent dma tag. */
535 if (bus_dma_tag_create(NULL, /* parent */
536 sc->alignment, /* alignment */
537 TW_OSLI_DMA_BOUNDARY, /* boundary */
538 BUS_SPACE_MAXADDR, /* lowaddr */
539 BUS_SPACE_MAXADDR, /* highaddr */
540 NULL, NULL, /* filter, filterarg */
541 TW_CL_MAX_IO_SIZE, /* maxsize */
542 max_sg_elements, /* nsegments */
543 TW_CL_MAX_IO_SIZE, /* maxsegsize */
545 &sc->parent_tag /* tag */)) {
546 tw_osli_printf(sc, "error = %d",
547 TW_CL_SEVERITY_ERROR_STRING,
548 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
550 "Can't allocate parent DMA tag",
555 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
556 if (bus_dma_tag_create(sc->parent_tag, /* parent */
557 sc->alignment, /* alignment */
559 BUS_SPACE_MAXADDR, /* lowaddr */
560 BUS_SPACE_MAXADDR, /* highaddr */
561 NULL, NULL, /* filter, filterarg */
562 dma_mem_size, /* maxsize */
564 BUS_SPACE_MAXSIZE, /* maxsegsize */
566 &sc->cmd_tag /* tag */)) {
567 tw_osli_printf(sc, "error = %d",
568 TW_CL_SEVERITY_ERROR_STRING,
569 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
571 "Can't allocate DMA tag for Common Layer's "
577 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
578 BUS_DMA_NOWAIT, &sc->cmd_map)) {
579 /* Try a second time. */
580 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
581 BUS_DMA_NOWAIT, &sc->cmd_map)) {
582 tw_osli_printf(sc, "error = %d",
583 TW_CL_SEVERITY_ERROR_STRING,
584 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
586 "Can't allocate DMA'able memory for the"
593 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
594 dma_mem_size, twa_map_load_callback,
595 &sc->dma_mem_phys, 0);
598 * Create a dma tag for data buffers; size will be the maximum
599 * possible I/O size (128kB).
601 if (bus_dma_tag_create(sc->parent_tag, /* parent */
602 sc->alignment, /* alignment */
604 BUS_SPACE_MAXADDR, /* lowaddr */
605 BUS_SPACE_MAXADDR, /* highaddr */
606 NULL, NULL, /* filter, filterarg */
607 TW_CL_MAX_IO_SIZE, /* maxsize */
608 max_sg_elements, /* nsegments */
609 TW_CL_MAX_IO_SIZE, /* maxsegsize */
610 BUS_DMA_ALLOCNOW, /* flags */
611 &sc->dma_tag /* tag */)) {
612 tw_osli_printf(sc, "error = %d",
613 TW_CL_SEVERITY_ERROR_STRING,
614 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
616 "Can't allocate DMA tag for data buffers",
622 * Create a dma tag for ioctl data buffers; size will be the maximum
623 * possible I/O size (128kB).
625 if (bus_dma_tag_create(sc->parent_tag, /* parent */
626 sc->alignment, /* alignment */
628 BUS_SPACE_MAXADDR, /* lowaddr */
629 BUS_SPACE_MAXADDR, /* highaddr */
630 NULL, NULL, /* filter, filterarg */
631 TW_CL_MAX_IO_SIZE, /* maxsize */
632 max_sg_elements, /* nsegments */
633 TW_CL_MAX_IO_SIZE, /* maxsegsize */
634 BUS_DMA_ALLOCNOW, /* flags */
635 &sc->ioctl_tag /* tag */)) {
636 tw_osli_printf(sc, "error = %d",
637 TW_CL_SEVERITY_ERROR_STRING,
638 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
640 "Can't allocate DMA tag for ioctl data buffers",
645 /* Create just one map for all ioctl request data buffers. */
646 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
647 tw_osli_printf(sc, "error = %d",
648 TW_CL_SEVERITY_ERROR_STRING,
649 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
651 "Can't create ioctl map",
657 /* Initialize request queues. */
658 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
659 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
661 sc->req_ctx_buf = kmalloc((sizeof(struct tw_osli_req_context) *
662 TW_OSLI_MAX_NUM_REQUESTS), TW_OSLI_MALLOC_CLASS,
664 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
665 req = &(sc->req_ctx_buf[i]);
667 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
668 tw_osli_printf(sc, "request # = %d, error = %d",
669 TW_CL_SEVERITY_ERROR_STRING,
670 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
672 "Can't create dma map",
677 /* Initialize the ioctl wakeup/ timeout mutex */
678 req->ioctl_wake_timeout_lock = &(req->ioctl_wake_timeout_lock_handle);
679 lockinit(req->ioctl_wake_timeout_lock, "tw_ioctl_wake_timeout_lock", 0, 0);
681 /* Insert request into the free queue. */
682 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
691 * Function name: tw_osli_free_resources
692 * Description: Performs clean-up at the time of going down.
694 * Input: sc -- ptr to OSL internal ctlr context
699 tw_osli_free_resources(struct twa_softc *sc)
701 struct tw_osli_req_context *req;
704 tw_osli_dbg_dprintf(3, sc, "entered");
706 /* Detach from CAM */
707 tw_osli_cam_detach(sc);
710 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
712 lockuninit(req->ioctl_wake_timeout_lock);
714 if ((error = bus_dmamap_destroy(sc->dma_tag,
716 tw_osli_dbg_dprintf(1, sc,
717 "dmamap_destroy(dma) returned %d",
721 if ((sc->ioctl_tag) && (sc->ioctl_map))
722 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
723 tw_osli_dbg_dprintf(1, sc,
724 "dmamap_destroy(ioctl) returned %d", error);
726 /* Free all memory allocated so far. */
728 kfree(sc->req_ctx_buf, TW_OSLI_MALLOC_CLASS);
731 kfree(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
734 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
735 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
739 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
740 tw_osli_dbg_dprintf(1, sc,
741 "dma_tag_destroy(cmd) returned %d", error);
744 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
745 tw_osli_dbg_dprintf(1, sc,
746 "dma_tag_destroy(dma) returned %d", error);
749 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
750 tw_osli_dbg_dprintf(1, sc,
751 "dma_tag_destroy(ioctl) returned %d", error);
754 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
755 tw_osli_dbg_dprintf(1, sc,
756 "dma_tag_destroy(parent) returned %d", error);
759 /* Disconnect the interrupt handler. */
760 if ((error = twa_teardown_intr(sc)))
761 tw_osli_dbg_dprintf(1, sc,
762 "teardown_intr returned %d", error);
764 if (sc->irq_res != NULL)
765 if ((error = bus_release_resource(sc->bus_dev,
766 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
767 tw_osli_dbg_dprintf(1, sc,
768 "release_resource(irq) returned %d", error);
770 if (sc->irq_type == PCI_INTR_TYPE_MSI)
771 pci_release_msi(sc->bus_dev);
773 /* Release the register window mapping. */
774 if (sc->reg_res != NULL)
775 if ((error = bus_release_resource(sc->bus_dev,
776 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
777 tw_osli_dbg_dprintf(1, sc,
778 "release_resource(io) returned %d", error);
780 /* Destroy the control device. */
781 if (sc->ctrl_dev != NULL)
782 destroy_dev(sc->ctrl_dev);
783 dev_ops_remove_minor(&twa_ops, device_get_unit(sc->bus_dev));
789 * Function name: twa_detach
790 * Description: Called when the controller is being detached from
793 * Input: dev -- bus device corresponding to the ctlr
795 * Return value: 0 -- success
799 twa_detach(device_t dev)
801 struct twa_softc *sc = device_get_softc(dev);
804 tw_osli_dbg_dprintf(3, sc, "entered");
808 tw_osli_printf(sc, "error = %d",
809 TW_CL_SEVERITY_ERROR_STRING,
810 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
817 /* Shut the controller down. */
818 if ((error = twa_shutdown(dev)))
821 /* Free all resources associated with this controller. */
822 tw_osli_free_resources(sc);
832 * Function name: twa_shutdown
833 * Description: Called at unload/shutdown time. Lets the controller
834 * know that we are going down.
836 * Input: dev -- bus device corresponding to the ctlr
838 * Return value: 0 -- success
842 twa_shutdown(device_t dev)
844 struct twa_softc *sc = device_get_softc(dev);
847 tw_osli_dbg_dprintf(3, sc, "entered");
849 /* Disconnect interrupts. */
850 error = twa_teardown_intr(sc);
852 /* Stop watchdog task. */
853 callout_stop_sync(&(sc->watchdog_callout[0]));
854 callout_stop_sync(&(sc->watchdog_callout[1]));
856 /* Disconnect from the controller. */
857 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
858 tw_osli_printf(sc, "error = %d",
859 TW_CL_SEVERITY_ERROR_STRING,
860 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
862 "Failed to shutdown Common Layer/controller",
871 * Function name: twa_pci_intr
872 * Description: Interrupt handler. Wrapper for twa_interrupt.
874 * Input: arg -- ptr to OSL internal ctlr context
879 twa_pci_intr(TW_VOID *arg)
881 struct twa_softc *sc = (struct twa_softc *)arg;
883 tw_osli_dbg_dprintf(10, sc, "entered");
884 tw_cl_interrupt(&(sc->ctlr_handle));
889 * Function name: tw_osli_fw_passthru
890 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
892 * Input: sc -- ptr to OSL internal ctlr context
893 * buf -- ptr to ioctl pkt understood by CL
895 * Return value: 0 -- success
899 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
901 struct tw_osli_req_context *req;
902 struct tw_osli_ioctl_no_data_buf *user_buf =
903 (struct tw_osli_ioctl_no_data_buf *)buf;
905 TW_UINT32 timeout = 60;
906 TW_UINT32 data_buf_size_adjusted;
907 struct tw_cl_req_packet *req_pkt;
908 struct tw_cl_passthru_req_packet *pt_req;
911 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
913 if ((req = tw_osli_get_request(sc)) == NULL)
916 req->req_handle.osl_req_ctxt = req;
918 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
920 req_pkt = &(req->req_pkt);
922 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
923 /* Let the Common Layer retry the request on cmd queue full. */
924 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
926 pt_req = &(req_pkt->gen_req_pkt.pt_req);
928 * Make sure that the data buffer sent to firmware is a
929 * 512 byte multiple in size.
931 data_buf_size_adjusted =
932 (user_buf->driver_pkt.buffer_length +
933 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
934 if ((req->length = data_buf_size_adjusted)) {
935 req->data = kmalloc(data_buf_size_adjusted,
936 TW_OSLI_MALLOC_CLASS, M_WAITOK);
937 /* Copy the payload. */
938 if ((error = copyin((TW_VOID *)(user_buf->pdata),
940 user_buf->driver_pkt.buffer_length)) != 0) {
941 tw_osli_printf(sc, "error = %d",
942 TW_CL_SEVERITY_ERROR_STRING,
943 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
945 "Could not copyin fw_passthru data_buf",
947 goto fw_passthru_err;
949 pt_req->sgl_entries = 1; /* will be updated during mapping */
950 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
951 TW_OSLI_REQ_FLAGS_DATA_OUT);
953 pt_req->sgl_entries = 0; /* no payload */
955 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
956 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
958 if ((error = tw_osli_map_request(req)))
959 goto fw_passthru_err;
961 end_time = tw_osl_get_local_time() + timeout;
962 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
963 lockmgr(req->ioctl_wake_timeout_lock, LK_EXCLUSIVE);
964 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
966 error = lksleep(req, req->ioctl_wake_timeout_lock, 0,
967 "twa_passthru", timeout*hz);
968 lockmgr(req->ioctl_wake_timeout_lock, LK_RELEASE);
970 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
972 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
975 if (((error = req->error_code)) ||
976 ((error = (req->state !=
977 TW_OSLI_REQ_STATE_COMPLETE))) ||
978 ((error = req_pkt->status)))
979 goto fw_passthru_err;
983 if (req_pkt->status) {
984 error = req_pkt->status;
985 goto fw_passthru_err;
988 if (error == EWOULDBLOCK) {
990 if ((!(req->error_code)) &&
991 (req->state == TW_OSLI_REQ_STATE_COMPLETE) &&
992 (!(req_pkt->status)) ) {
994 tw_osli_printf(sc, "request = %p",
995 TW_CL_SEVERITY_ERROR_STRING,
996 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
998 "FALSE Passthru timeout!",
1000 #endif /* TW_OSL_DEBUG */
1001 error = 0; /* False error */
1004 if (!(tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
1006 tw_osli_printf(sc, "request = %p",
1007 TW_CL_SEVERITY_ERROR_STRING,
1008 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1010 "Passthru request timed out!",
1012 #else /* TW_OSL_DEBUG */
1013 device_printf((sc)->bus_dev, "Passthru request timed out!\n");
1014 #endif /* TW_OSL_DEBUG */
1015 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
1019 end_time = tw_osl_get_local_time() + timeout;
1022 * Don't touch req after a reset. It (and any
1023 * associated data) will be
1024 * unmapped by the callback.
1028 * Either the request got completed, or we were woken up by a
1029 * signal. Calculate the new timeout, in case it was the latter.
1031 timeout = (end_time - tw_osl_get_local_time());
1032 } /* End of while loop */
1034 /* If there was a payload, copy it back. */
1035 if ((!error) && (req->length))
1036 if ((error = copyout(req->data, user_buf->pdata,
1037 user_buf->driver_pkt.buffer_length)))
1038 tw_osli_printf(sc, "error = %d",
1039 TW_CL_SEVERITY_ERROR_STRING,
1040 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1042 "Could not copyout fw_passthru data_buf",
1047 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1050 user_buf->driver_pkt.os_status = error;
1051 /* Free resources. */
1053 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1054 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1061 * Function name: tw_osl_complete_passthru
1062 * Description: Called to complete passthru requests.
1064 * Input: req_handle -- ptr to request handle
1066 * Return value: None
1069 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1071 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1072 struct tw_cl_req_packet *req_pkt =
1073 (struct tw_cl_req_packet *)(&req->req_pkt);
1074 struct twa_softc *sc = req->ctlr;
1076 tw_osli_dbg_dprintf(5, sc, "entered");
1078 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1079 tw_osli_printf(sc, "request = %p, status = %d",
1080 TW_CL_SEVERITY_ERROR_STRING,
1081 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1083 "Unposted command completed!!",
1088 * Remove request from the busy queue. Just mark it complete.
1089 * There's no need to move it into the complete queue as we are
1090 * going to be done with it right now.
1092 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1093 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1095 tw_osli_unmap_request(req);
1098 * Don't do a wake up if there was an error even before the request
1099 * was sent down to the Common Layer, and we hadn't gotten an
1100 * EINPROGRESS. The request originator will then be returned an
1101 * error, and he can do the clean-up.
1103 if ((req->error_code) && (!(req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1106 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1107 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1108 /* Wake up the sleeping command originator. */
1109 tw_osli_dbg_dprintf(5, sc,
1110 "Waking up originator of request %p", req);
1111 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1115 * If the request completed even before mtx_sleep
1116 * was called, simply return.
1118 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1121 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1124 tw_osli_printf(sc, "request = %p",
1125 TW_CL_SEVERITY_ERROR_STRING,
1126 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1128 "Passthru callback called, "
1129 "and caller not sleeping",
1133 tw_osli_printf(sc, "request = %p",
1134 TW_CL_SEVERITY_ERROR_STRING,
1135 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1137 "Passthru callback called for non-passthru request",
1145 * Function name: tw_osli_get_request
1146 * Description: Gets a request pkt from the free queue.
1148 * Input: sc -- ptr to OSL internal ctlr context
1150 * Return value: ptr to request pkt -- success
1153 struct tw_osli_req_context *
1154 tw_osli_get_request(struct twa_softc *sc)
1156 struct tw_osli_req_context *req;
1158 tw_osli_dbg_dprintf(4, sc, "entered");
1160 /* Get a free request packet. */
1161 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1163 /* Initialize some fields to their defaults. */
1165 req->req_handle.osl_req_ctxt = NULL;
1166 req->req_handle.cl_req_ctxt = NULL;
1167 req->req_handle.is_io = 0;
1171 req->real_data = NULL;
1172 req->real_length = 0;
1173 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1175 req->error_code = 0;
1176 req->orig_req = NULL;
1178 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1187 * Function name: twa_map_load_data_callback
1188 * Description: Callback of bus_dmamap_load for the buffer associated
1189 * with data. Updates the cmd pkt (size/sgl_entries
1190 * fields, as applicable) to reflect the number of sg
1193 * Input: arg -- ptr to OSL internal request context
1194 * segs -- ptr to a list of segment descriptors
1195 * nsegments--# of segments
1196 * error -- 0 if no errors encountered before callback,
1197 * non-zero if errors were encountered
1199 * Return value: None
1202 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1203 TW_INT32 nsegments, TW_INT32 error)
1205 struct tw_osli_req_context *req =
1206 (struct tw_osli_req_context *)arg;
1207 struct twa_softc *sc = req->ctlr;
1208 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1210 tw_osli_dbg_dprintf(10, sc, "entered");
1212 if (error == EINVAL) {
1213 req->error_code = error;
1217 /* Mark the request as currently being processed. */
1218 req->state = TW_OSLI_REQ_STATE_BUSY;
1219 /* Move the request into the busy queue. */
1220 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1222 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1224 if (error == EFBIG) {
1225 req->error_code = error;
1229 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1230 struct tw_cl_passthru_req_packet *pt_req;
1232 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1233 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1234 BUS_DMASYNC_PREREAD);
1236 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1238 * If we're using an alignment buffer, and we're
1239 * writing data, copy the real data out.
1241 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1242 bcopy(req->real_data, req->data, req->real_length);
1243 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1244 BUS_DMASYNC_PREWRITE);
1247 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1248 pt_req->sg_list = (TW_UINT8 *)segs;
1249 pt_req->sgl_entries += (nsegments - 1);
1250 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1251 &(req->req_handle));
1253 struct tw_cl_scsi_req_packet *scsi_req;
1255 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1256 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1257 BUS_DMASYNC_PREREAD);
1259 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1261 * If we're using an alignment buffer, and we're
1262 * writing data, copy the real data out.
1264 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1265 bcopy(req->real_data, req->data, req->real_length);
1266 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1267 BUS_DMASYNC_PREWRITE);
1270 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1271 scsi_req->sg_list = (TW_UINT8 *)segs;
1272 scsi_req->sgl_entries += (nsegments - 1);
1273 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1274 &(req->req_handle));
1279 req->error_code = error;
1280 req_pkt->tw_osl_callback(&(req->req_handle));
1282 * If the caller had been returned EINPROGRESS, and he has
1283 * registered a callback for handling completion, the callback
1284 * will never get called because we were unable to submit the
1285 * request. So, free up the request right here.
1287 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1288 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1295 * Function name: twa_map_load_callback
1296 * Description: Callback of bus_dmamap_load for the buffer associated
1299 * Input: arg -- ptr to variable to hold phys addr
1300 * segs -- ptr to a list of segment descriptors
1301 * nsegments--# of segments
1302 * error -- 0 if no errors encountered before callback,
1303 * non-zero if errors were encountered
1305 * Return value: None
1308 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1309 TW_INT32 nsegments, TW_INT32 error)
1311 *((bus_addr_t *)arg) = segs[0].ds_addr;
1317 * Function name: tw_osli_map_request
1318 * Description: Maps a cmd pkt and data associated with it, into
1321 * Input: req -- ptr to request pkt
1323 * Return value: 0 -- success
1324 * non-zero-- failure
1327 tw_osli_map_request(struct tw_osli_req_context *req)
1329 struct twa_softc *sc = req->ctlr;
1332 tw_osli_dbg_dprintf(10, sc, "entered");
1334 /* If the command involves data, map that too. */
1335 if (req->data != NULL) {
1337 * It's sufficient for the data pointer to be 4-byte aligned
1338 * to work with 9000. However, if 4-byte aligned addresses
1339 * are passed to bus_dmamap_load, we can get back sg elements
1340 * that are not 512-byte multiples in size. So, we will let
1341 * only those buffers that are 512-byte aligned to pass
1342 * through, and bounce the rest, so as to make sure that we
1343 * always get back sg elements that are 512-byte multiples
1346 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1347 (req->length % sc->sg_size_factor)) {
1348 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1349 /* Save original data pointer and length. */
1350 req->real_data = req->data;
1351 req->real_length = req->length;
1352 req->length = (req->length +
1353 (sc->sg_size_factor - 1)) &
1354 ~(sc->sg_size_factor - 1);
1355 req->data = kmalloc(req->length, TW_OSLI_MALLOC_CLASS,
1357 if (req->data == NULL) {
1358 tw_osli_printf(sc, "error = %d",
1359 TW_CL_SEVERITY_ERROR_STRING,
1360 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1362 "Failed to allocate memory "
1363 "for bounce buffer",
1365 /* Restore original data pointer and length. */
1366 req->data = req->real_data;
1367 req->length = req->real_length;
1373 * Map the data buffer into bus space and build the SG list.
1375 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1376 /* Lock against multiple simultaneous ioctl calls. */
1377 spin_lock(sc->io_lock);
1378 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1379 req->data, req->length,
1380 twa_map_load_data_callback, req,
1382 spin_unlock(sc->io_lock);
1385 * There's only one CAM I/O thread running at a time.
1386 * So, there's no need to hold the io_lock.
1388 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1389 req->data, req->length,
1390 twa_map_load_data_callback, req,
1395 error = req->error_code;
1397 if (error == EINPROGRESS) {
1399 * Specifying sc->io_lock as the lockfuncarg
1400 * in ...tag_create should protect the access
1401 * of ...FLAGS_MAPPED from the callback.
1403 spin_lock(sc->io_lock);
1404 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED))
1405 req->flags |= TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1406 tw_osli_disallow_new_requests(sc, &(req->req_handle));
1407 spin_unlock(sc->io_lock);
1410 tw_osli_printf(sc, "error = %d",
1411 TW_CL_SEVERITY_ERROR_STRING,
1412 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1414 "Failed to map DMA memory "
1417 req->flags |= TW_OSLI_REQ_FLAGS_FAILED;
1418 /* Free alignment buffer if it was used. */
1420 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1421 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1423 * Restore original data pointer
1426 req->data = req->real_data;
1427 req->length = req->real_length;
1433 /* Mark the request as currently being processed. */
1434 req->state = TW_OSLI_REQ_STATE_BUSY;
1435 /* Move the request into the busy queue. */
1436 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1437 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1438 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1439 &(req->req_pkt), &(req->req_handle));
1441 error = tw_cl_start_io(&sc->ctlr_handle,
1442 &(req->req_pkt), &(req->req_handle));
1444 req->error_code = error;
1445 req->req_pkt.tw_osl_callback(&(req->req_handle));
1454 * Function name: tw_osli_unmap_request
1455 * Description: Undoes the mapping done by tw_osli_map_request.
1457 * Input: req -- ptr to request pkt
1459 * Return value: None
1462 tw_osli_unmap_request(struct tw_osli_req_context *req)
1464 struct twa_softc *sc = req->ctlr;
1466 tw_osli_dbg_dprintf(10, sc, "entered");
1468 /* If the command involved data, unmap that too. */
1469 if (req->data != NULL) {
1470 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1471 /* Lock against multiple simultaneous ioctl calls. */
1472 spin_lock(sc->io_lock);
1474 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1475 bus_dmamap_sync(sc->ioctl_tag,
1476 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1479 * If we are using a bounce buffer, and we are
1480 * reading data, copy the real data in.
1482 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1483 bcopy(req->data, req->real_data,
1487 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1488 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1489 BUS_DMASYNC_POSTWRITE);
1491 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1493 spin_unlock(sc->io_lock);
1495 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1496 bus_dmamap_sync(sc->dma_tag,
1497 req->dma_map, BUS_DMASYNC_POSTREAD);
1500 * If we are using a bounce buffer, and we are
1501 * reading data, copy the real data in.
1503 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1504 bcopy(req->data, req->real_data,
1507 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1508 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1509 BUS_DMASYNC_POSTWRITE);
1511 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1515 /* Free alignment buffer if it was used. */
1516 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1517 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1518 /* Restore original data pointer and length. */
1519 req->data = req->real_data;
1520 req->length = req->real_length;
1528 TW_VOID twa_report_stats(TW_VOID);
1529 TW_VOID twa_reset_stats(TW_VOID);
1530 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1531 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1535 * Function name: twa_report_stats
1536 * Description: For being called from ddb. Calls functions that print
1537 * OSL and CL internal stats for the controller.
1541 * Return value: None
1544 twa_report_stats(TW_VOID)
1546 struct twa_softc *sc;
1549 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1550 tw_osli_print_ctlr_stats(sc);
1551 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1558 * Function name: tw_osli_print_ctlr_stats
1559 * Description: For being called from ddb. Prints OSL controller stats
1561 * Input: sc -- ptr to OSL internal controller context
1563 * Return value: None
1566 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1568 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1569 twa_printf(sc, "OSLq type current max\n");
1570 twa_printf(sc, "free %04d %04d\n",
1571 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1572 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1573 twa_printf(sc, "busy %04d %04d\n",
1574 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1575 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1581 * Function name: twa_print_req_info
1582 * Description: For being called from ddb. Calls functions that print
1583 * OSL and CL internal details for the request.
1585 * Input: req -- ptr to OSL internal request context
1587 * Return value: None
1590 twa_print_req_info(struct tw_osli_req_context *req)
1592 struct twa_softc *sc = req->ctlr;
1594 twa_printf(sc, "OSL details for request:\n");
1595 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1596 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1597 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1598 "next_req = %p, prev_req = %p, dma_map = %p\n",
1599 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1600 req->data, req->length, req->real_data, req->real_length,
1601 req->state, req->flags, req->error_code, req->orig_req,
1602 req->link.next, req->link.prev, req->dma_map);
1603 tw_cl_print_req_info(&(req->req_handle));
1609 * Function name: twa_reset_stats
1610 * Description: For being called from ddb.
1611 * Resets some OSL controller stats.
1615 * Return value: None
1618 twa_reset_stats(TW_VOID)
1620 struct twa_softc *sc;
1623 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1624 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1625 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1626 tw_cl_reset_stats(&sc->ctlr_handle);
1630 #endif /* TW_OSL_DEBUG */