2 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
3 * Copyright (c) 2004-05 Vinod Kashyap.
4 * Copyright (c) 2000 Michael Smith
5 * Copyright (c) 2000 BSDi
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/sys/dev/twa/tw_osl_freebsd.c,v 1.19 2011/11/07 06:44:47 ed Exp $
33 * AMCC'S 3ware driver for 9000 series storage controllers.
35 * Author: Vinod Kashyap
36 * Modifications by: Adam Radford
37 * Modifications by: Manjunath Ranganathaiah
42 * FreeBSD specific functions not related to CAM, and other
43 * miscellaneous functions.
47 #include <dev/raid/twa/tw_osl_includes.h>
48 #include <dev/raid/twa/tw_cl_fwif.h>
49 #include <dev/raid/twa/tw_cl_ioctl.h>
50 #include <dev/raid/twa/tw_osl_ioctl.h>
53 TW_INT32 TW_DEBUG_LEVEL_FOR_OSL = TW_OSL_DEBUG;
54 TW_INT32 TW_OSL_DEBUG_LEVEL_FOR_CL = TW_OSL_DEBUG;
55 #endif /* TW_OSL_DEBUG */
57 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 = {
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),
204 DEVMETHOD(bus_print_child, bus_generic_print_child),
205 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
209 static driver_t twa_pci_driver = {
212 sizeof(struct twa_softc)
215 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, NULL, NULL);
216 MODULE_DEPEND(twa, cam, 1, 1, 1);
217 MODULE_DEPEND(twa, pci, 1, 1, 1);
218 MODULE_VERSION(twa, 1);
222 * Function name: twa_probe
223 * Description: Called at driver load time. Claims 9000 ctlrs.
225 * Input: dev -- bus device corresponding to the ctlr
227 * Return value: <= 0 -- success
231 twa_probe(device_t dev)
233 static TW_UINT8 first_ctlr = 1;
235 tw_osli_dbg_printf(3, "entered");
237 if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
238 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
239 /* Print the driver version only once. */
241 kprintf("3ware device driver for 9000 series storage "
242 "controllers, version: %s\n",
243 TW_OSL_DRIVER_VERSION_STRING);
251 int twa_setup_intr(struct twa_softc *sc)
255 if (!(sc->intr_handle) && (sc->irq_res)) {
256 error = bus_setup_intr(sc->bus_dev, sc->irq_res,
259 sc, &sc->intr_handle, NULL);
265 int twa_teardown_intr(struct twa_softc *sc)
269 if ((sc->intr_handle) && (sc->irq_res)) {
270 error = bus_teardown_intr(sc->bus_dev,
271 sc->irq_res, sc->intr_handle);
272 sc->intr_handle = NULL;
280 * Function name: twa_attach
281 * Description: Allocates pci resources; updates sc; adds a node to the
282 * sysctl tree to expose the driver version; makes calls
283 * (to the Common Layer) to initialize ctlr, and to
286 * Input: dev -- bus device corresponding to the ctlr
288 * Return value: 0 -- success
292 twa_attach(device_t dev)
294 struct twa_softc *sc = device_get_softc(dev);
297 TW_INT32 bar0_offset;
302 tw_osli_dbg_dprintf(3, sc, "entered");
304 sc->ctlr_handle.osl_ctlr_ctxt = sc;
306 /* Initialize the softc structure. */
308 sc->device_id = pci_get_device(dev);
310 /* Initialize the mutexes right here. */
311 sc->io_lock = &(sc->io_lock_handle);
312 spin_init(sc->io_lock);
313 sc->q_lock = &(sc->q_lock_handle);
314 spin_init(sc->q_lock);
315 sc->sim_lock = &(sc->sim_lock_handle);
316 lockinit(sc->sim_lock, "tw_osl_sim_lock", 0, LK_CANRECURSE);
318 sysctl_ctx_init(&sc->sysctl_ctxt);
319 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
320 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
321 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
322 if (sc->sysctl_tree == NULL) {
323 tw_osli_printf(sc, "error = %d",
324 TW_CL_SEVERITY_ERROR_STRING,
325 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
327 "Cannot add sysctl tree node",
331 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
332 OID_AUTO, "driver_version", CTLFLAG_RD,
333 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
335 /* Make sure we are going to be able to talk to this board. */
336 command = pci_read_config(dev, PCIR_COMMAND, 2);
337 if ((command & PCIM_CMD_PORTEN) == 0) {
338 tw_osli_printf(sc, "error = %d",
339 TW_CL_SEVERITY_ERROR_STRING,
340 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
342 "Register window not available",
344 tw_osli_free_resources(sc);
348 /* Force the busmaster enable bit on, in case the BIOS forgot. */
349 command |= PCIM_CMD_BUSMASTEREN;
350 pci_write_config(dev, PCIR_COMMAND, command, 2);
352 /* Allocate the PCI register window. */
353 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
354 &bar_num, &bar0_offset, &bar_size))) {
355 tw_osli_printf(sc, "error = %d",
356 TW_CL_SEVERITY_ERROR_STRING,
357 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
359 "Can't get PCI BAR info",
361 tw_osli_free_resources(sc);
364 sc->reg_res_id = PCIR_BARS + bar0_offset;
365 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
366 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
368 tw_osli_printf(sc, "error = %d",
369 TW_CL_SEVERITY_ERROR_STRING,
370 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
372 "Can't allocate register window",
374 tw_osli_free_resources(sc);
377 sc->bus_tag = rman_get_bustag(sc->reg_res);
378 sc->bus_handle = rman_get_bushandle(sc->reg_res);
380 /* Allocate and register our interrupt. */
382 sc->irq_type = pci_alloc_1intr(sc->bus_dev, twa_msi_enable,
383 &sc->irq_res_id, &irq_flags);
384 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
385 &(sc->irq_res_id), 0, ~0, 1,
386 irq_flags)) == NULL) {
387 tw_osli_printf(sc, "error = %d",
388 TW_CL_SEVERITY_ERROR_STRING,
389 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
391 "Can't allocate interrupt",
393 tw_osli_free_resources(sc);
396 if ((error = twa_setup_intr(sc))) {
397 tw_osli_printf(sc, "error = %d",
398 TW_CL_SEVERITY_ERROR_STRING,
399 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
401 "Can't set up interrupt",
403 tw_osli_free_resources(sc);
407 if ((error = tw_osli_alloc_mem(sc))) {
408 tw_osli_printf(sc, "error = %d",
409 TW_CL_SEVERITY_ERROR_STRING,
410 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
412 "Memory allocation failure",
414 tw_osli_free_resources(sc);
418 /* Initialize the Common Layer for this controller. */
419 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
420 TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
421 sc->non_dma_mem, sc->dma_mem,
424 tw_osli_printf(sc, "error = %d",
425 TW_CL_SEVERITY_ERROR_STRING,
426 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
428 "Failed to initialize Common Layer/controller",
430 tw_osli_free_resources(sc);
434 /* Create the control device. */
435 sc->ctrl_dev = make_dev(&twa_ops, device_get_unit(sc->bus_dev),
436 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
437 "twa%d", device_get_unit(sc->bus_dev));
438 sc->ctrl_dev->si_drv1 = sc;
440 if ((error = tw_osli_cam_attach(sc))) {
441 tw_osli_free_resources(sc);
442 tw_osli_printf(sc, "error = %d",
443 TW_CL_SEVERITY_ERROR_STRING,
444 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
446 "Failed to initialize CAM",
451 sc->watchdog_index = 0;
452 callout_init(&(sc->watchdog_callout[0]));
453 callout_init(&(sc->watchdog_callout[1]));
454 callout_reset(&(sc->watchdog_callout[0]), 5*hz, twa_watchdog, &sc->ctlr_handle);
461 twa_watchdog(TW_VOID *arg)
463 struct tw_cl_ctlr_handle *ctlr_handle =
464 (struct tw_cl_ctlr_handle *)arg;
465 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
467 int i_need_a_reset = 0;
468 int driver_is_active = 0;
469 TW_UINT64 current_time;
470 struct tw_osli_req_context *my_req;
473 //==============================================================================
474 current_time = (TW_UINT64) (tw_osl_get_local_time());
476 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
477 my_req = &(sc->req_ctx_buf[i]);
479 if ((my_req->state == TW_OSLI_REQ_STATE_BUSY) &&
480 (my_req->deadline) &&
481 (my_req->deadline < current_time)) {
482 tw_cl_set_reset_needed(ctlr_handle);
484 device_printf((sc)->bus_dev, "Request %d timed out! d = %llu, c = %llu\n", i, my_req->deadline, current_time);
485 #else /* TW_OSL_DEBUG */
486 device_printf((sc)->bus_dev, "Request %d timed out!\n", i);
487 #endif /* TW_OSL_DEBUG */
491 //==============================================================================
493 i_need_a_reset = tw_cl_is_reset_needed(ctlr_handle);
495 i = (int) ((sc->watchdog_index++) & 1);
497 driver_is_active = tw_cl_is_active(ctlr_handle);
499 if (i_need_a_reset) {
501 device_printf((sc)->bus_dev, "Watchdog rescheduled in 70 seconds\n");
502 #endif /* TW_OSL_DEBUG */
503 callout_reset(&(sc->watchdog_callout[i]), 70*hz, twa_watchdog, &sc->ctlr_handle);
504 tw_cl_reset_ctlr(ctlr_handle);
506 device_printf((sc)->bus_dev, "Watchdog reset completed!\n");
507 #endif /* TW_OSL_DEBUG */
508 } else if (driver_is_active) {
509 callout_reset(&(sc->watchdog_callout[i]), 5*hz, twa_watchdog, &sc->ctlr_handle);
513 device_printf((sc)->bus_dev, "i_need_a_reset = %d, "
514 "driver_is_active = %d\n",
515 i_need_a_reset, driver_is_active);
516 #endif /* TW_OSL_DEBUG */
521 * Function name: tw_osli_alloc_mem
522 * Description: Allocates memory needed both by CL and OSL.
524 * Input: sc -- OSL internal controller context
526 * Return value: 0 -- success
530 tw_osli_alloc_mem(struct twa_softc *sc)
532 struct tw_osli_req_context *req;
533 TW_UINT32 max_sg_elements;
534 TW_UINT32 non_dma_mem_size;
535 TW_UINT32 dma_mem_size;
539 tw_osli_dbg_dprintf(3, sc, "entered");
541 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
542 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
544 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
545 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
547 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
548 sc->device_id, TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
549 &(sc->alignment), &(sc->sg_size_factor),
550 &non_dma_mem_size, &dma_mem_size
552 tw_osli_printf(sc, "error = %d",
553 TW_CL_SEVERITY_ERROR_STRING,
554 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
556 "Can't get Common Layer's memory requirements",
561 sc->non_dma_mem = kmalloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
564 /* Create the parent dma tag. */
565 if (bus_dma_tag_create(NULL, /* parent */
566 sc->alignment, /* alignment */
567 TW_OSLI_DMA_BOUNDARY, /* boundary */
568 BUS_SPACE_MAXADDR, /* lowaddr */
569 BUS_SPACE_MAXADDR, /* highaddr */
570 NULL, NULL, /* filter, filterarg */
571 TW_CL_MAX_IO_SIZE, /* maxsize */
572 max_sg_elements, /* nsegments */
573 TW_CL_MAX_IO_SIZE, /* maxsegsize */
575 &sc->parent_tag /* tag */)) {
576 tw_osli_printf(sc, "error = %d",
577 TW_CL_SEVERITY_ERROR_STRING,
578 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
580 "Can't allocate parent DMA tag",
585 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
586 if (bus_dma_tag_create(sc->parent_tag, /* parent */
587 sc->alignment, /* alignment */
589 BUS_SPACE_MAXADDR, /* lowaddr */
590 BUS_SPACE_MAXADDR, /* highaddr */
591 NULL, NULL, /* filter, filterarg */
592 dma_mem_size, /* maxsize */
594 BUS_SPACE_MAXSIZE, /* maxsegsize */
596 &sc->cmd_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 Common Layer's "
607 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
608 BUS_DMA_NOWAIT, &sc->cmd_map)) {
609 /* Try a second time. */
610 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
611 BUS_DMA_NOWAIT, &sc->cmd_map)) {
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'able memory for the"
623 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
624 dma_mem_size, twa_map_load_callback,
625 &sc->dma_mem_phys, 0);
628 * Create a dma tag for data buffers; size will be the maximum
629 * possible I/O size (128kB).
631 if (bus_dma_tag_create(sc->parent_tag, /* parent */
632 sc->alignment, /* alignment */
634 BUS_SPACE_MAXADDR, /* lowaddr */
635 BUS_SPACE_MAXADDR, /* highaddr */
636 NULL, NULL, /* filter, filterarg */
637 TW_CL_MAX_IO_SIZE, /* maxsize */
638 max_sg_elements, /* nsegments */
639 TW_CL_MAX_IO_SIZE, /* maxsegsize */
640 BUS_DMA_ALLOCNOW, /* flags */
641 &sc->dma_tag /* tag */)) {
642 tw_osli_printf(sc, "error = %d",
643 TW_CL_SEVERITY_ERROR_STRING,
644 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
646 "Can't allocate DMA tag for data buffers",
652 * Create a dma tag for ioctl data buffers; size will be the maximum
653 * possible I/O size (128kB).
655 if (bus_dma_tag_create(sc->parent_tag, /* parent */
656 sc->alignment, /* alignment */
658 BUS_SPACE_MAXADDR, /* lowaddr */
659 BUS_SPACE_MAXADDR, /* highaddr */
660 NULL, NULL, /* filter, filterarg */
661 TW_CL_MAX_IO_SIZE, /* maxsize */
662 max_sg_elements, /* nsegments */
663 TW_CL_MAX_IO_SIZE, /* maxsegsize */
664 BUS_DMA_ALLOCNOW, /* flags */
665 &sc->ioctl_tag /* tag */)) {
666 tw_osli_printf(sc, "error = %d",
667 TW_CL_SEVERITY_ERROR_STRING,
668 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
670 "Can't allocate DMA tag for ioctl data buffers",
675 /* Create just one map for all ioctl request data buffers. */
676 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
677 tw_osli_printf(sc, "error = %d",
678 TW_CL_SEVERITY_ERROR_STRING,
679 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
681 "Can't create ioctl map",
687 /* Initialize request queues. */
688 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
689 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
691 sc->req_ctx_buf = kmalloc((sizeof(struct tw_osli_req_context) *
692 TW_OSLI_MAX_NUM_REQUESTS), TW_OSLI_MALLOC_CLASS,
694 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
695 req = &(sc->req_ctx_buf[i]);
697 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
698 tw_osli_printf(sc, "request # = %d, error = %d",
699 TW_CL_SEVERITY_ERROR_STRING,
700 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
702 "Can't create dma map",
707 /* Initialize the ioctl wakeup/ timeout mutex */
708 req->ioctl_wake_timeout_lock = &(req->ioctl_wake_timeout_lock_handle);
709 lockinit(req->ioctl_wake_timeout_lock, "tw_ioctl_wake_timeout_lock", 0, 0);
711 /* Insert request into the free queue. */
712 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
721 * Function name: tw_osli_free_resources
722 * Description: Performs clean-up at the time of going down.
724 * Input: sc -- ptr to OSL internal ctlr context
729 tw_osli_free_resources(struct twa_softc *sc)
731 struct tw_osli_req_context *req;
734 tw_osli_dbg_dprintf(3, sc, "entered");
736 /* Detach from CAM */
737 tw_osli_cam_detach(sc);
740 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
742 lockuninit(req->ioctl_wake_timeout_lock);
744 if ((error = bus_dmamap_destroy(sc->dma_tag,
746 tw_osli_dbg_dprintf(1, sc,
747 "dmamap_destroy(dma) returned %d",
751 if ((sc->ioctl_tag) && (sc->ioctl_map))
752 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
753 tw_osli_dbg_dprintf(1, sc,
754 "dmamap_destroy(ioctl) returned %d", error);
756 /* Free all memory allocated so far. */
758 kfree(sc->req_ctx_buf, TW_OSLI_MALLOC_CLASS);
761 kfree(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
764 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
765 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
769 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
770 tw_osli_dbg_dprintf(1, sc,
771 "dma_tag_destroy(cmd) returned %d", error);
774 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
775 tw_osli_dbg_dprintf(1, sc,
776 "dma_tag_destroy(dma) returned %d", error);
779 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
780 tw_osli_dbg_dprintf(1, sc,
781 "dma_tag_destroy(ioctl) returned %d", error);
784 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
785 tw_osli_dbg_dprintf(1, sc,
786 "dma_tag_destroy(parent) returned %d", error);
789 /* Disconnect the interrupt handler. */
790 if ((error = twa_teardown_intr(sc)))
791 tw_osli_dbg_dprintf(1, sc,
792 "teardown_intr returned %d", error);
794 if (sc->irq_res != NULL)
795 if ((error = bus_release_resource(sc->bus_dev,
796 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
797 tw_osli_dbg_dprintf(1, sc,
798 "release_resource(irq) returned %d", error);
800 if (sc->irq_type == PCI_INTR_TYPE_MSI)
801 pci_release_msi(sc->bus_dev);
803 /* Release the register window mapping. */
804 if (sc->reg_res != NULL)
805 if ((error = bus_release_resource(sc->bus_dev,
806 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
807 tw_osli_dbg_dprintf(1, sc,
808 "release_resource(io) returned %d", error);
810 /* Destroy the control device. */
811 if (sc->ctrl_dev != NULL)
812 destroy_dev(sc->ctrl_dev);
813 dev_ops_remove_minor(&twa_ops, device_get_unit(sc->bus_dev));
815 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
816 tw_osli_dbg_dprintf(1, sc,
817 "sysctl_ctx_free returned %d", error);
824 * Function name: twa_detach
825 * Description: Called when the controller is being detached from
828 * Input: dev -- bus device corresponding to the ctlr
830 * Return value: 0 -- success
834 twa_detach(device_t dev)
836 struct twa_softc *sc = device_get_softc(dev);
839 tw_osli_dbg_dprintf(3, sc, "entered");
843 tw_osli_printf(sc, "error = %d",
844 TW_CL_SEVERITY_ERROR_STRING,
845 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
852 /* Shut the controller down. */
853 if ((error = twa_shutdown(dev)))
856 /* Free all resources associated with this controller. */
857 tw_osli_free_resources(sc);
867 * Function name: twa_shutdown
868 * Description: Called at unload/shutdown time. Lets the controller
869 * know that we are going down.
871 * Input: dev -- bus device corresponding to the ctlr
873 * Return value: 0 -- success
877 twa_shutdown(device_t dev)
879 struct twa_softc *sc = device_get_softc(dev);
882 tw_osli_dbg_dprintf(3, sc, "entered");
884 /* Disconnect interrupts. */
885 error = twa_teardown_intr(sc);
887 /* Stop watchdog task. */
888 callout_stop_sync(&(sc->watchdog_callout[0]));
889 callout_stop_sync(&(sc->watchdog_callout[1]));
891 /* Disconnect from the controller. */
892 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
893 tw_osli_printf(sc, "error = %d",
894 TW_CL_SEVERITY_ERROR_STRING,
895 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
897 "Failed to shutdown Common Layer/controller",
906 * Function name: twa_pci_intr
907 * Description: Interrupt handler. Wrapper for twa_interrupt.
909 * Input: arg -- ptr to OSL internal ctlr context
914 twa_pci_intr(TW_VOID *arg)
916 struct twa_softc *sc = (struct twa_softc *)arg;
918 tw_osli_dbg_dprintf(10, sc, "entered");
919 tw_cl_interrupt(&(sc->ctlr_handle));
924 * Function name: tw_osli_fw_passthru
925 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
927 * Input: sc -- ptr to OSL internal ctlr context
928 * buf -- ptr to ioctl pkt understood by CL
930 * Return value: 0 -- success
934 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
936 struct tw_osli_req_context *req;
937 struct tw_osli_ioctl_no_data_buf *user_buf =
938 (struct tw_osli_ioctl_no_data_buf *)buf;
940 TW_UINT32 timeout = 60;
941 TW_UINT32 data_buf_size_adjusted;
942 struct tw_cl_req_packet *req_pkt;
943 struct tw_cl_passthru_req_packet *pt_req;
946 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
948 if ((req = tw_osli_get_request(sc)) == NULL)
951 req->req_handle.osl_req_ctxt = req;
953 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
955 req_pkt = &(req->req_pkt);
957 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
958 /* Let the Common Layer retry the request on cmd queue full. */
959 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
961 pt_req = &(req_pkt->gen_req_pkt.pt_req);
963 * Make sure that the data buffer sent to firmware is a
964 * 512 byte multiple in size.
966 data_buf_size_adjusted =
967 (user_buf->driver_pkt.buffer_length +
968 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
969 if ((req->length = data_buf_size_adjusted)) {
970 req->data = kmalloc(data_buf_size_adjusted,
971 TW_OSLI_MALLOC_CLASS, M_WAITOK);
972 /* Copy the payload. */
973 if ((error = copyin((TW_VOID *)(user_buf->pdata),
975 user_buf->driver_pkt.buffer_length)) != 0) {
976 tw_osli_printf(sc, "error = %d",
977 TW_CL_SEVERITY_ERROR_STRING,
978 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
980 "Could not copyin fw_passthru data_buf",
982 goto fw_passthru_err;
984 pt_req->sgl_entries = 1; /* will be updated during mapping */
985 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
986 TW_OSLI_REQ_FLAGS_DATA_OUT);
988 pt_req->sgl_entries = 0; /* no payload */
990 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
991 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
993 if ((error = tw_osli_map_request(req)))
994 goto fw_passthru_err;
996 end_time = tw_osl_get_local_time() + timeout;
997 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
998 lockmgr(req->ioctl_wake_timeout_lock, LK_EXCLUSIVE);
999 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
1001 error = lksleep(req, req->ioctl_wake_timeout_lock, 0,
1002 "twa_passthru", timeout*hz);
1003 lockmgr(req->ioctl_wake_timeout_lock, LK_RELEASE);
1005 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
1007 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1010 if (((error = req->error_code)) ||
1011 ((error = (req->state !=
1012 TW_OSLI_REQ_STATE_COMPLETE))) ||
1013 ((error = req_pkt->status)))
1014 goto fw_passthru_err;
1018 if (req_pkt->status) {
1019 error = req_pkt->status;
1020 goto fw_passthru_err;
1023 if (error == EWOULDBLOCK) {
1025 if ((!(req->error_code)) &&
1026 (req->state == TW_OSLI_REQ_STATE_COMPLETE) &&
1027 (!(req_pkt->status)) ) {
1029 tw_osli_printf(sc, "request = %p",
1030 TW_CL_SEVERITY_ERROR_STRING,
1031 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1033 "FALSE Passthru timeout!",
1035 #endif /* TW_OSL_DEBUG */
1036 error = 0; /* False error */
1039 if (!(tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
1041 tw_osli_printf(sc, "request = %p",
1042 TW_CL_SEVERITY_ERROR_STRING,
1043 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1045 "Passthru request timed out!",
1047 #else /* TW_OSL_DEBUG */
1048 device_printf((sc)->bus_dev, "Passthru request timed out!\n");
1049 #endif /* TW_OSL_DEBUG */
1050 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
1054 end_time = tw_osl_get_local_time() + timeout;
1057 * Don't touch req after a reset. It (and any
1058 * associated data) will be
1059 * unmapped by the callback.
1063 * Either the request got completed, or we were woken up by a
1064 * signal. Calculate the new timeout, in case it was the latter.
1066 timeout = (end_time - tw_osl_get_local_time());
1067 } /* End of while loop */
1069 /* If there was a payload, copy it back. */
1070 if ((!error) && (req->length))
1071 if ((error = copyout(req->data, user_buf->pdata,
1072 user_buf->driver_pkt.buffer_length)))
1073 tw_osli_printf(sc, "error = %d",
1074 TW_CL_SEVERITY_ERROR_STRING,
1075 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1077 "Could not copyout fw_passthru data_buf",
1082 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1085 user_buf->driver_pkt.os_status = error;
1086 /* Free resources. */
1088 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1089 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1096 * Function name: tw_osl_complete_passthru
1097 * Description: Called to complete passthru requests.
1099 * Input: req_handle -- ptr to request handle
1101 * Return value: None
1104 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1106 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1107 struct tw_cl_req_packet *req_pkt =
1108 (struct tw_cl_req_packet *)(&req->req_pkt);
1109 struct twa_softc *sc = req->ctlr;
1111 tw_osli_dbg_dprintf(5, sc, "entered");
1113 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1114 tw_osli_printf(sc, "request = %p, status = %d",
1115 TW_CL_SEVERITY_ERROR_STRING,
1116 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1118 "Unposted command completed!!",
1123 * Remove request from the busy queue. Just mark it complete.
1124 * There's no need to move it into the complete queue as we are
1125 * going to be done with it right now.
1127 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1128 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1130 tw_osli_unmap_request(req);
1133 * Don't do a wake up if there was an error even before the request
1134 * was sent down to the Common Layer, and we hadn't gotten an
1135 * EINPROGRESS. The request originator will then be returned an
1136 * error, and he can do the clean-up.
1138 if ((req->error_code) && (!(req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1141 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1142 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1143 /* Wake up the sleeping command originator. */
1144 tw_osli_dbg_dprintf(5, sc,
1145 "Waking up originator of request %p", req);
1146 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1150 * If the request completed even before mtx_sleep
1151 * was called, simply return.
1153 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1156 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1159 tw_osli_printf(sc, "request = %p",
1160 TW_CL_SEVERITY_ERROR_STRING,
1161 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1163 "Passthru callback called, "
1164 "and caller not sleeping",
1168 tw_osli_printf(sc, "request = %p",
1169 TW_CL_SEVERITY_ERROR_STRING,
1170 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1172 "Passthru callback called for non-passthru request",
1180 * Function name: tw_osli_get_request
1181 * Description: Gets a request pkt from the free queue.
1183 * Input: sc -- ptr to OSL internal ctlr context
1185 * Return value: ptr to request pkt -- success
1188 struct tw_osli_req_context *
1189 tw_osli_get_request(struct twa_softc *sc)
1191 struct tw_osli_req_context *req;
1193 tw_osli_dbg_dprintf(4, sc, "entered");
1195 /* Get a free request packet. */
1196 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1198 /* Initialize some fields to their defaults. */
1200 req->req_handle.osl_req_ctxt = NULL;
1201 req->req_handle.cl_req_ctxt = NULL;
1202 req->req_handle.is_io = 0;
1206 req->real_data = NULL;
1207 req->real_length = 0;
1208 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1210 req->error_code = 0;
1211 req->orig_req = NULL;
1213 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1222 * Function name: twa_map_load_data_callback
1223 * Description: Callback of bus_dmamap_load for the buffer associated
1224 * with data. Updates the cmd pkt (size/sgl_entries
1225 * fields, as applicable) to reflect the number of sg
1228 * Input: arg -- ptr to OSL internal request context
1229 * segs -- ptr to a list of segment descriptors
1230 * nsegments--# of segments
1231 * error -- 0 if no errors encountered before callback,
1232 * non-zero if errors were encountered
1234 * Return value: None
1237 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1238 TW_INT32 nsegments, TW_INT32 error)
1240 struct tw_osli_req_context *req =
1241 (struct tw_osli_req_context *)arg;
1242 struct twa_softc *sc = req->ctlr;
1243 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1245 tw_osli_dbg_dprintf(10, sc, "entered");
1247 if (error == EINVAL) {
1248 req->error_code = error;
1252 /* Mark the request as currently being processed. */
1253 req->state = TW_OSLI_REQ_STATE_BUSY;
1254 /* Move the request into the busy queue. */
1255 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1257 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1259 if (error == EFBIG) {
1260 req->error_code = error;
1264 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1265 struct tw_cl_passthru_req_packet *pt_req;
1267 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1268 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1269 BUS_DMASYNC_PREREAD);
1271 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1273 * If we're using an alignment buffer, and we're
1274 * writing data, copy the real data out.
1276 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1277 bcopy(req->real_data, req->data, req->real_length);
1278 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1279 BUS_DMASYNC_PREWRITE);
1282 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1283 pt_req->sg_list = (TW_UINT8 *)segs;
1284 pt_req->sgl_entries += (nsegments - 1);
1285 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1286 &(req->req_handle));
1288 struct tw_cl_scsi_req_packet *scsi_req;
1290 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1291 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1292 BUS_DMASYNC_PREREAD);
1294 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1296 * If we're using an alignment buffer, and we're
1297 * writing data, copy the real data out.
1299 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1300 bcopy(req->real_data, req->data, req->real_length);
1301 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1302 BUS_DMASYNC_PREWRITE);
1305 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1306 scsi_req->sg_list = (TW_UINT8 *)segs;
1307 scsi_req->sgl_entries += (nsegments - 1);
1308 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1309 &(req->req_handle));
1314 req->error_code = error;
1315 req_pkt->tw_osl_callback(&(req->req_handle));
1317 * If the caller had been returned EINPROGRESS, and he has
1318 * registered a callback for handling completion, the callback
1319 * will never get called because we were unable to submit the
1320 * request. So, free up the request right here.
1322 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1323 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1330 * Function name: twa_map_load_callback
1331 * Description: Callback of bus_dmamap_load for the buffer associated
1334 * Input: arg -- ptr to variable to hold phys addr
1335 * segs -- ptr to a list of segment descriptors
1336 * nsegments--# of segments
1337 * error -- 0 if no errors encountered before callback,
1338 * non-zero if errors were encountered
1340 * Return value: None
1343 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1344 TW_INT32 nsegments, TW_INT32 error)
1346 *((bus_addr_t *)arg) = segs[0].ds_addr;
1352 * Function name: tw_osli_map_request
1353 * Description: Maps a cmd pkt and data associated with it, into
1356 * Input: req -- ptr to request pkt
1358 * Return value: 0 -- success
1359 * non-zero-- failure
1362 tw_osli_map_request(struct tw_osli_req_context *req)
1364 struct twa_softc *sc = req->ctlr;
1367 tw_osli_dbg_dprintf(10, sc, "entered");
1369 /* If the command involves data, map that too. */
1370 if (req->data != NULL) {
1372 * It's sufficient for the data pointer to be 4-byte aligned
1373 * to work with 9000. However, if 4-byte aligned addresses
1374 * are passed to bus_dmamap_load, we can get back sg elements
1375 * that are not 512-byte multiples in size. So, we will let
1376 * only those buffers that are 512-byte aligned to pass
1377 * through, and bounce the rest, so as to make sure that we
1378 * always get back sg elements that are 512-byte multiples
1381 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1382 (req->length % sc->sg_size_factor)) {
1383 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1384 /* Save original data pointer and length. */
1385 req->real_data = req->data;
1386 req->real_length = req->length;
1387 req->length = (req->length +
1388 (sc->sg_size_factor - 1)) &
1389 ~(sc->sg_size_factor - 1);
1390 req->data = kmalloc(req->length, TW_OSLI_MALLOC_CLASS,
1392 if (req->data == NULL) {
1393 tw_osli_printf(sc, "error = %d",
1394 TW_CL_SEVERITY_ERROR_STRING,
1395 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1397 "Failed to allocate memory "
1398 "for bounce buffer",
1400 /* Restore original data pointer and length. */
1401 req->data = req->real_data;
1402 req->length = req->real_length;
1408 * Map the data buffer into bus space and build the SG list.
1410 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1411 /* Lock against multiple simultaneous ioctl calls. */
1412 spin_lock(sc->io_lock);
1413 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1414 req->data, req->length,
1415 twa_map_load_data_callback, req,
1417 spin_unlock(sc->io_lock);
1420 * There's only one CAM I/O thread running at a time.
1421 * So, there's no need to hold the io_lock.
1423 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1424 req->data, req->length,
1425 twa_map_load_data_callback, req,
1430 error = req->error_code;
1432 if (error == EINPROGRESS) {
1434 * Specifying sc->io_lock as the lockfuncarg
1435 * in ...tag_create should protect the access
1436 * of ...FLAGS_MAPPED from the callback.
1438 spin_lock(sc->io_lock);
1439 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED))
1440 req->flags |= TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1441 tw_osli_disallow_new_requests(sc, &(req->req_handle));
1442 spin_unlock(sc->io_lock);
1445 tw_osli_printf(sc, "error = %d",
1446 TW_CL_SEVERITY_ERROR_STRING,
1447 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1449 "Failed to map DMA memory "
1452 req->flags |= TW_OSLI_REQ_FLAGS_FAILED;
1453 /* Free alignment buffer if it was used. */
1455 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1456 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1458 * Restore original data pointer
1461 req->data = req->real_data;
1462 req->length = req->real_length;
1468 /* Mark the request as currently being processed. */
1469 req->state = TW_OSLI_REQ_STATE_BUSY;
1470 /* Move the request into the busy queue. */
1471 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1472 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1473 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1474 &(req->req_pkt), &(req->req_handle));
1476 error = tw_cl_start_io(&sc->ctlr_handle,
1477 &(req->req_pkt), &(req->req_handle));
1479 req->error_code = error;
1480 req->req_pkt.tw_osl_callback(&(req->req_handle));
1489 * Function name: tw_osli_unmap_request
1490 * Description: Undoes the mapping done by tw_osli_map_request.
1492 * Input: req -- ptr to request pkt
1494 * Return value: None
1497 tw_osli_unmap_request(struct tw_osli_req_context *req)
1499 struct twa_softc *sc = req->ctlr;
1501 tw_osli_dbg_dprintf(10, sc, "entered");
1503 /* If the command involved data, unmap that too. */
1504 if (req->data != NULL) {
1505 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1506 /* Lock against multiple simultaneous ioctl calls. */
1507 spin_lock(sc->io_lock);
1509 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1510 bus_dmamap_sync(sc->ioctl_tag,
1511 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1514 * If we are using a bounce buffer, and we are
1515 * reading data, copy the real data in.
1517 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1518 bcopy(req->data, req->real_data,
1522 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1523 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1524 BUS_DMASYNC_POSTWRITE);
1526 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1528 spin_unlock(sc->io_lock);
1530 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1531 bus_dmamap_sync(sc->dma_tag,
1532 req->dma_map, BUS_DMASYNC_POSTREAD);
1535 * If we are using a bounce buffer, and we are
1536 * reading data, copy the real data in.
1538 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1539 bcopy(req->data, req->real_data,
1542 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1543 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1544 BUS_DMASYNC_POSTWRITE);
1546 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1550 /* Free alignment buffer if it was used. */
1551 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1552 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1553 /* Restore original data pointer and length. */
1554 req->data = req->real_data;
1555 req->length = req->real_length;
1563 TW_VOID twa_report_stats(TW_VOID);
1564 TW_VOID twa_reset_stats(TW_VOID);
1565 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1566 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1570 * Function name: twa_report_stats
1571 * Description: For being called from ddb. Calls functions that print
1572 * OSL and CL internal stats for the controller.
1576 * Return value: None
1579 twa_report_stats(TW_VOID)
1581 struct twa_softc *sc;
1584 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1585 tw_osli_print_ctlr_stats(sc);
1586 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1593 * Function name: tw_osli_print_ctlr_stats
1594 * Description: For being called from ddb. Prints OSL controller stats
1596 * Input: sc -- ptr to OSL internal controller context
1598 * Return value: None
1601 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1603 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1604 twa_printf(sc, "OSLq type current max\n");
1605 twa_printf(sc, "free %04d %04d\n",
1606 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1607 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1608 twa_printf(sc, "busy %04d %04d\n",
1609 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1610 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1616 * Function name: twa_print_req_info
1617 * Description: For being called from ddb. Calls functions that print
1618 * OSL and CL internal details for the request.
1620 * Input: req -- ptr to OSL internal request context
1622 * Return value: None
1625 twa_print_req_info(struct tw_osli_req_context *req)
1627 struct twa_softc *sc = req->ctlr;
1629 twa_printf(sc, "OSL details for request:\n");
1630 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1631 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1632 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1633 "next_req = %p, prev_req = %p, dma_map = %p\n",
1634 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1635 req->data, req->length, req->real_data, req->real_length,
1636 req->state, req->flags, req->error_code, req->orig_req,
1637 req->link.next, req->link.prev, req->dma_map);
1638 tw_cl_print_req_info(&(req->req_handle));
1644 * Function name: twa_reset_stats
1645 * Description: For being called from ddb.
1646 * Resets some OSL controller stats.
1650 * Return value: None
1653 twa_reset_stats(TW_VOID)
1655 struct twa_softc *sc;
1658 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1659 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1660 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1661 tw_cl_reset_stats(&sc->ctlr_handle);
1665 #endif /* TW_OSL_DEBUG */