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.18 2010/09/04 16:27:14 bz Exp $
33 * AMCC'S 3ware driver for 9000 series storage controllers.
35 * Author: Vinod Kashyap
36 * Modifications by: Adam Radford
37 * Modifications by: Manjunath Ranganathaiah
42 * FreeBSD specific functions not related to CAM, and other
43 * miscellaneous functions.
47 #include <dev/raid/twa/tw_osl_includes.h>
48 #include <dev/raid/twa/tw_cl_fwif.h>
49 #include <dev/raid/twa/tw_cl_ioctl.h>
50 #include <dev/raid/twa/tw_osl_ioctl.h>
53 TW_INT32 TW_DEBUG_LEVEL_FOR_OSL = TW_OSL_DEBUG;
54 TW_INT32 TW_OSL_DEBUG_LEVEL_FOR_CL = TW_OSL_DEBUG;
55 #endif /* TW_OSL_DEBUG */
57 MALLOC_DEFINE(TW_OSLI_MALLOC_CLASS, "twa_commands", "twa commands");
60 static d_open_t twa_open;
61 static d_close_t twa_close;
62 static d_ioctl_t twa_ioctl;
64 static struct dev_ops twa_ops = {
71 static devclass_t twa_devclass;
75 * Function name: twa_open
76 * Description: Called when the controller is opened.
77 * Simply marks the controller as open.
79 * Input: dev -- control device corresponding to the ctlr
80 * flags -- mode of open
81 * fmt -- device type (character/block etc.)
82 * proc -- current process
84 * Return value: 0 -- success
88 twa_open(struct dev_open_args *ap)
90 cdev_t dev = ap->a_head.a_dev;
91 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
93 tw_osli_dbg_dprintf(5, sc, "entered");
94 sc->open = TW_CL_TRUE;
101 * Function name: twa_close
102 * Description: Called when the controller is closed.
103 * Simply marks the controller as not open.
105 * Input: dev -- control device corresponding to the ctlr
106 * flags -- mode of corresponding open
107 * fmt -- device type (character/block etc.)
108 * proc -- current process
110 * Return value: 0 -- success
114 twa_close(struct dev_close_args *ap)
116 cdev_t dev = ap->a_head.a_dev;
117 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
119 tw_osli_dbg_dprintf(5, sc, "entered");
120 sc->open = TW_CL_FALSE;
127 * Function name: twa_ioctl
128 * Description: Called when an ioctl is posted to the controller.
129 * Handles any OS Layer specific cmds, passes the rest
130 * on to the Common Layer.
132 * Input: dev -- control device corresponding to the ctlr
134 * buf -- ptr to buffer in kernel memory, which is
135 * a copy of the input buffer in user-space
136 * flags -- mode of corresponding open
137 * proc -- current process
138 * Output: buf -- ptr to buffer in kernel memory, which will
139 * be copied to the output buffer in user-space
140 * Return value: 0 -- success
144 twa_ioctl(struct dev_ioctl_args *ap)
146 cdev_t dev = ap->a_head.a_dev;
147 u_long cmd = ap->a_cmd;
148 caddr_t buf = ap->a_data;
149 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
152 tw_osli_dbg_dprintf(5, sc, "entered");
155 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
156 tw_osli_dbg_dprintf(6, sc, "ioctl: fw_passthru");
157 error = tw_osli_fw_passthru(sc, (TW_INT8 *)buf);
160 case TW_OSL_IOCTL_SCAN_BUS:
161 /* Request CAM for a bus scan. */
162 tw_osli_dbg_dprintf(6, sc, "ioctl: scan bus");
163 error = tw_osli_request_bus_scan(sc);
167 tw_osli_dbg_dprintf(6, sc, "ioctl: 0x%lx", cmd);
168 error = tw_cl_ioctl(&sc->ctlr_handle, cmd, buf);
176 static TW_INT32 twa_probe(device_t dev);
177 static TW_INT32 twa_attach(device_t dev);
178 static TW_INT32 twa_detach(device_t dev);
179 static TW_INT32 twa_shutdown(device_t dev);
180 #if 0 /* XXX swildner */
181 static TW_VOID twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op);
183 static TW_VOID twa_pci_intr(TW_VOID *arg);
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);
221 * Function name: twa_probe
222 * Description: Called at driver load time. Claims 9000 ctlrs.
224 * Input: dev -- bus device corresponding to the ctlr
226 * Return value: <= 0 -- success
230 twa_probe(device_t dev)
232 static TW_UINT8 first_ctlr = 1;
234 tw_osli_dbg_printf(3, "entered");
236 if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
237 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
238 /* Print the driver version only once. */
240 kprintf("3ware device driver for 9000 series storage "
241 "controllers, version: %s\n",
242 TW_OSL_DRIVER_VERSION_STRING);
250 int twa_setup_intr(struct twa_softc *sc)
254 if (!(sc->intr_handle) && (sc->irq_res)) {
255 error = bus_setup_intr(sc->bus_dev, sc->irq_res,
258 sc, &sc->intr_handle, NULL);
264 int twa_teardown_intr(struct twa_softc *sc)
268 if ((sc->intr_handle) && (sc->irq_res)) {
269 error = bus_teardown_intr(sc->bus_dev,
270 sc->irq_res, sc->intr_handle);
271 sc->intr_handle = NULL;
279 * Function name: twa_attach
280 * Description: Allocates pci resources; updates sc; adds a node to the
281 * sysctl tree to expose the driver version; makes calls
282 * (to the Common Layer) to initialize ctlr, and to
285 * Input: dev -- bus device corresponding to the ctlr
287 * Return value: 0 -- success
291 twa_attach(device_t dev)
293 struct twa_softc *sc = device_get_softc(dev);
296 TW_INT32 bar0_offset;
300 tw_osli_dbg_dprintf(3, sc, "entered");
302 sc->ctlr_handle.osl_ctlr_ctxt = sc;
304 /* Initialize the softc structure. */
306 sc->device_id = pci_get_device(dev);
308 /* Initialize the mutexes right here. */
309 sc->io_lock = &(sc->io_lock_handle);
310 spin_init(sc->io_lock);
311 sc->q_lock = &(sc->q_lock_handle);
312 spin_init(sc->q_lock);
313 sc->sim_lock = &(sc->sim_lock_handle);
314 lockinit(sc->sim_lock, "tw_osl_sim_lock", 0, LK_CANRECURSE);
316 sysctl_ctx_init(&sc->sysctl_ctxt);
317 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
318 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
319 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
320 if (sc->sysctl_tree == NULL) {
321 tw_osli_printf(sc, "error = %d",
322 TW_CL_SEVERITY_ERROR_STRING,
323 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
325 "Cannot add sysctl tree node",
329 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
330 OID_AUTO, "driver_version", CTLFLAG_RD,
331 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
333 /* Make sure we are going to be able to talk to this board. */
334 command = pci_read_config(dev, PCIR_COMMAND, 2);
335 if ((command & PCIM_CMD_PORTEN) == 0) {
336 tw_osli_printf(sc, "error = %d",
337 TW_CL_SEVERITY_ERROR_STRING,
338 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
340 "Register window not available",
342 tw_osli_free_resources(sc);
346 /* Force the busmaster enable bit on, in case the BIOS forgot. */
347 command |= PCIM_CMD_BUSMASTEREN;
348 pci_write_config(dev, PCIR_COMMAND, command, 2);
350 /* Allocate the PCI register window. */
351 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
352 &bar_num, &bar0_offset, &bar_size))) {
353 tw_osli_printf(sc, "error = %d",
354 TW_CL_SEVERITY_ERROR_STRING,
355 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
357 "Can't get PCI BAR info",
359 tw_osli_free_resources(sc);
362 sc->reg_res_id = PCIR_BARS + bar0_offset;
363 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
364 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
366 tw_osli_printf(sc, "error = %d",
367 TW_CL_SEVERITY_ERROR_STRING,
368 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
370 "Can't allocate register window",
372 tw_osli_free_resources(sc);
375 sc->bus_tag = rman_get_bustag(sc->reg_res);
376 sc->bus_handle = rman_get_bushandle(sc->reg_res);
378 /* Allocate and register our interrupt. */
380 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
381 &(sc->irq_res_id), 0, ~0, 1,
382 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
383 tw_osli_printf(sc, "error = %d",
384 TW_CL_SEVERITY_ERROR_STRING,
385 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
387 "Can't allocate interrupt",
389 tw_osli_free_resources(sc);
392 if ((error = twa_setup_intr(sc))) {
393 tw_osli_printf(sc, "error = %d",
394 TW_CL_SEVERITY_ERROR_STRING,
395 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
397 "Can't set up interrupt",
399 tw_osli_free_resources(sc);
403 if ((error = tw_osli_alloc_mem(sc))) {
404 tw_osli_printf(sc, "error = %d",
405 TW_CL_SEVERITY_ERROR_STRING,
406 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
408 "Memory allocation failure",
410 tw_osli_free_resources(sc);
414 /* Initialize the Common Layer for this controller. */
415 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
416 TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
417 sc->non_dma_mem, sc->dma_mem,
420 tw_osli_printf(sc, "error = %d",
421 TW_CL_SEVERITY_ERROR_STRING,
422 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
424 "Failed to initialize Common Layer/controller",
426 tw_osli_free_resources(sc);
430 /* Create the control device. */
431 sc->ctrl_dev = make_dev(&twa_ops, device_get_unit(sc->bus_dev),
432 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
433 "twa%d", device_get_unit(sc->bus_dev));
434 sc->ctrl_dev->si_drv1 = sc;
436 if ((error = tw_osli_cam_attach(sc))) {
437 tw_osli_free_resources(sc);
438 tw_osli_printf(sc, "error = %d",
439 TW_CL_SEVERITY_ERROR_STRING,
440 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
442 "Failed to initialize CAM",
447 sc->watchdog_index = 0;
448 callout_init(&(sc->watchdog_callout[0]));
449 callout_init(&(sc->watchdog_callout[1]));
450 callout_reset(&(sc->watchdog_callout[0]), 5*hz, twa_watchdog, &sc->ctlr_handle);
457 twa_watchdog(TW_VOID *arg)
459 struct tw_cl_ctlr_handle *ctlr_handle =
460 (struct tw_cl_ctlr_handle *)arg;
461 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
463 int i_need_a_reset = 0;
464 int driver_is_active = 0;
465 TW_UINT64 current_time;
466 struct tw_osli_req_context *my_req;
469 //==============================================================================
470 current_time = (TW_UINT64) (tw_osl_get_local_time());
472 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
473 my_req = &(sc->req_ctx_buf[i]);
475 if ((my_req->state == TW_OSLI_REQ_STATE_BUSY) &&
476 (my_req->deadline) &&
477 (my_req->deadline < current_time)) {
478 tw_cl_set_reset_needed(ctlr_handle);
480 device_printf((sc)->bus_dev, "Request %d timed out! d = %llu, c = %llu\n", i, my_req->deadline, current_time);
481 #else /* TW_OSL_DEBUG */
482 device_printf((sc)->bus_dev, "Request %d timed out!\n", i);
483 #endif /* TW_OSL_DEBUG */
487 //==============================================================================
489 i_need_a_reset = tw_cl_is_reset_needed(ctlr_handle);
491 i = (int) ((sc->watchdog_index++) & 1);
493 driver_is_active = tw_cl_is_active(ctlr_handle);
495 if (i_need_a_reset) {
497 device_printf((sc)->bus_dev, "Watchdog rescheduled in 70 seconds\n");
498 #endif /* TW_OSL_DEBUG */
499 callout_reset(&(sc->watchdog_callout[i]), 70*hz, twa_watchdog, &sc->ctlr_handle);
500 tw_cl_reset_ctlr(ctlr_handle);
502 device_printf((sc)->bus_dev, "Watchdog reset completed!\n");
503 #endif /* TW_OSL_DEBUG */
504 } else if (driver_is_active) {
505 callout_reset(&(sc->watchdog_callout[i]), 5*hz, twa_watchdog, &sc->ctlr_handle);
509 device_printf((sc)->bus_dev, "i_need_a_reset = %d, "
510 "driver_is_active = %d\n",
511 i_need_a_reset, driver_is_active);
512 #endif /* TW_OSL_DEBUG */
517 * Function name: tw_osli_alloc_mem
518 * Description: Allocates memory needed both by CL and OSL.
520 * Input: sc -- OSL internal controller context
522 * Return value: 0 -- success
526 tw_osli_alloc_mem(struct twa_softc *sc)
528 struct tw_osli_req_context *req;
529 TW_UINT32 max_sg_elements;
530 TW_UINT32 non_dma_mem_size;
531 TW_UINT32 dma_mem_size;
535 tw_osli_dbg_dprintf(3, sc, "entered");
537 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
538 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
540 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
541 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
543 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
544 sc->device_id, TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
545 &(sc->alignment), &(sc->sg_size_factor),
546 &non_dma_mem_size, &dma_mem_size
548 tw_osli_printf(sc, "error = %d",
549 TW_CL_SEVERITY_ERROR_STRING,
550 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
552 "Can't get Common Layer's memory requirements",
557 sc->non_dma_mem = kmalloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
560 /* Create the parent dma tag. */
561 if (bus_dma_tag_create(NULL, /* parent */
562 sc->alignment, /* alignment */
563 TW_OSLI_DMA_BOUNDARY, /* boundary */
564 BUS_SPACE_MAXADDR, /* lowaddr */
565 BUS_SPACE_MAXADDR, /* highaddr */
566 NULL, NULL, /* filter, filterarg */
567 TW_CL_MAX_IO_SIZE, /* maxsize */
568 max_sg_elements, /* nsegments */
569 TW_CL_MAX_IO_SIZE, /* maxsegsize */
571 &sc->parent_tag /* tag */)) {
572 tw_osli_printf(sc, "error = %d",
573 TW_CL_SEVERITY_ERROR_STRING,
574 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
576 "Can't allocate parent DMA tag",
581 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
582 if (bus_dma_tag_create(sc->parent_tag, /* parent */
583 sc->alignment, /* alignment */
585 BUS_SPACE_MAXADDR, /* lowaddr */
586 BUS_SPACE_MAXADDR, /* highaddr */
587 NULL, NULL, /* filter, filterarg */
588 dma_mem_size, /* maxsize */
590 BUS_SPACE_MAXSIZE, /* maxsegsize */
592 &sc->cmd_tag /* tag */)) {
593 tw_osli_printf(sc, "error = %d",
594 TW_CL_SEVERITY_ERROR_STRING,
595 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
597 "Can't allocate DMA tag for Common Layer's "
603 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
604 BUS_DMA_NOWAIT, &sc->cmd_map)) {
605 /* Try a second time. */
606 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
607 BUS_DMA_NOWAIT, &sc->cmd_map)) {
608 tw_osli_printf(sc, "error = %d",
609 TW_CL_SEVERITY_ERROR_STRING,
610 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
612 "Can't allocate DMA'able memory for the"
619 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
620 dma_mem_size, twa_map_load_callback,
621 &sc->dma_mem_phys, 0);
624 * Create a dma tag for data buffers; size will be the maximum
625 * possible I/O size (128kB).
627 if (bus_dma_tag_create(sc->parent_tag, /* parent */
628 sc->alignment, /* alignment */
630 BUS_SPACE_MAXADDR, /* lowaddr */
631 BUS_SPACE_MAXADDR, /* highaddr */
632 NULL, NULL, /* filter, filterarg */
633 TW_CL_MAX_IO_SIZE, /* maxsize */
634 max_sg_elements, /* nsegments */
635 TW_CL_MAX_IO_SIZE, /* maxsegsize */
636 BUS_DMA_ALLOCNOW, /* flags */
637 #if 0 /* XXX swildner */
638 twa_busdma_lock, /* lockfunc */
639 sc->io_lock, /* lockfuncarg */
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 #if 0 /* XXX swildner */
666 twa_busdma_lock, /* lockfunc */
667 sc->io_lock, /* lockfuncarg */
669 &sc->ioctl_tag /* tag */)) {
670 tw_osli_printf(sc, "error = %d",
671 TW_CL_SEVERITY_ERROR_STRING,
672 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
674 "Can't allocate DMA tag for ioctl data buffers",
679 /* Create just one map for all ioctl request data buffers. */
680 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
681 tw_osli_printf(sc, "error = %d",
682 TW_CL_SEVERITY_ERROR_STRING,
683 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
685 "Can't create ioctl map",
691 /* Initialize request queues. */
692 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
693 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
695 sc->req_ctx_buf = kmalloc((sizeof(struct tw_osli_req_context) *
696 TW_OSLI_MAX_NUM_REQUESTS), TW_OSLI_MALLOC_CLASS,
698 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
699 req = &(sc->req_ctx_buf[i]);
701 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
702 tw_osli_printf(sc, "request # = %d, error = %d",
703 TW_CL_SEVERITY_ERROR_STRING,
704 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
706 "Can't create dma map",
711 /* Initialize the ioctl wakeup/ timeout mutex */
712 req->ioctl_wake_timeout_lock = &(req->ioctl_wake_timeout_lock_handle);
713 lockinit(req->ioctl_wake_timeout_lock, "tw_ioctl_wake_timeout_lock", 0, 0);
715 /* Insert request into the free queue. */
716 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
725 * Function name: tw_osli_free_resources
726 * Description: Performs clean-up at the time of going down.
728 * Input: sc -- ptr to OSL internal ctlr context
733 tw_osli_free_resources(struct twa_softc *sc)
735 struct tw_osli_req_context *req;
738 tw_osli_dbg_dprintf(3, sc, "entered");
740 /* Detach from CAM */
741 tw_osli_cam_detach(sc);
744 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
746 lockuninit(req->ioctl_wake_timeout_lock);
748 if ((error = bus_dmamap_destroy(sc->dma_tag,
750 tw_osli_dbg_dprintf(1, sc,
751 "dmamap_destroy(dma) returned %d",
755 if ((sc->ioctl_tag) && (sc->ioctl_map))
756 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
757 tw_osli_dbg_dprintf(1, sc,
758 "dmamap_destroy(ioctl) returned %d", error);
760 /* Free all memory allocated so far. */
762 kfree(sc->req_ctx_buf, TW_OSLI_MALLOC_CLASS);
765 kfree(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
768 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
769 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
773 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
774 tw_osli_dbg_dprintf(1, sc,
775 "dma_tag_destroy(cmd) returned %d", error);
778 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
779 tw_osli_dbg_dprintf(1, sc,
780 "dma_tag_destroy(dma) returned %d", error);
783 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
784 tw_osli_dbg_dprintf(1, sc,
785 "dma_tag_destroy(ioctl) returned %d", error);
788 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
789 tw_osli_dbg_dprintf(1, sc,
790 "dma_tag_destroy(parent) returned %d", error);
793 /* Disconnect the interrupt handler. */
794 if ((error = twa_teardown_intr(sc)))
795 tw_osli_dbg_dprintf(1, sc,
796 "teardown_intr returned %d", error);
798 if (sc->irq_res != NULL)
799 if ((error = bus_release_resource(sc->bus_dev,
800 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
801 tw_osli_dbg_dprintf(1, sc,
802 "release_resource(irq) returned %d", error);
805 /* Release the register window mapping. */
806 if (sc->reg_res != NULL)
807 if ((error = bus_release_resource(sc->bus_dev,
808 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
809 tw_osli_dbg_dprintf(1, sc,
810 "release_resource(io) returned %d", error);
812 dev_ops_remove_minor(&twa_ops, device_get_unit(sc->bus_dev));
814 /* Destroy the control device. */
815 if (sc->ctrl_dev != NULL)
816 destroy_dev(sc->ctrl_dev);
818 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
819 tw_osli_dbg_dprintf(1, sc,
820 "sysctl_ctx_free returned %d", error);
827 * Function name: twa_detach
828 * Description: Called when the controller is being detached from
831 * Input: dev -- bus device corresponding to the ctlr
833 * Return value: 0 -- success
837 twa_detach(device_t dev)
839 struct twa_softc *sc = device_get_softc(dev);
842 tw_osli_dbg_dprintf(3, sc, "entered");
846 tw_osli_printf(sc, "error = %d",
847 TW_CL_SEVERITY_ERROR_STRING,
848 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
855 /* Shut the controller down. */
856 if ((error = twa_shutdown(dev)))
859 /* Free all resources associated with this controller. */
860 tw_osli_free_resources(sc);
870 * Function name: twa_shutdown
871 * Description: Called at unload/shutdown time. Lets the controller
872 * know that we are going down.
874 * Input: dev -- bus device corresponding to the ctlr
876 * Return value: 0 -- success
880 twa_shutdown(device_t dev)
882 struct twa_softc *sc = device_get_softc(dev);
885 tw_osli_dbg_dprintf(3, sc, "entered");
887 /* Disconnect interrupts. */
888 error = twa_teardown_intr(sc);
890 #if 0 /* XXX swildner */
891 /* Stop watchdog task. */
892 callout_drain(&(sc->watchdog_callout[0]));
893 callout_drain(&(sc->watchdog_callout[1]));
896 /* Disconnect from the controller. */
897 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
898 tw_osli_printf(sc, "error = %d",
899 TW_CL_SEVERITY_ERROR_STRING,
900 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
902 "Failed to shutdown Common Layer/controller",
910 #if 0 /* XXX swildner */
912 * Function name: twa_busdma_lock
913 * Description: Function to provide synchronization during busdma_swi.
915 * Input: lock_arg -- lock mutex sent as argument
916 * op -- operation (lock/unlock) expected of the function
921 twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op)
923 struct spinlock *lock;
925 lock = (struct spinlock *)lock_arg;
936 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
943 * Function name: twa_pci_intr
944 * Description: Interrupt handler. Wrapper for twa_interrupt.
946 * Input: arg -- ptr to OSL internal ctlr context
951 twa_pci_intr(TW_VOID *arg)
953 struct twa_softc *sc = (struct twa_softc *)arg;
955 tw_osli_dbg_dprintf(10, sc, "entered");
956 tw_cl_interrupt(&(sc->ctlr_handle));
961 * Function name: tw_osli_fw_passthru
962 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
964 * Input: sc -- ptr to OSL internal ctlr context
965 * buf -- ptr to ioctl pkt understood by CL
967 * Return value: 0 -- success
971 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
973 struct tw_osli_req_context *req;
974 struct tw_osli_ioctl_no_data_buf *user_buf =
975 (struct tw_osli_ioctl_no_data_buf *)buf;
977 TW_UINT32 timeout = 60;
978 TW_UINT32 data_buf_size_adjusted;
979 struct tw_cl_req_packet *req_pkt;
980 struct tw_cl_passthru_req_packet *pt_req;
983 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
985 if ((req = tw_osli_get_request(sc)) == NULL)
988 req->req_handle.osl_req_ctxt = req;
990 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
992 req_pkt = &(req->req_pkt);
994 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
995 /* Let the Common Layer retry the request on cmd queue full. */
996 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
998 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1000 * Make sure that the data buffer sent to firmware is a
1001 * 512 byte multiple in size.
1003 data_buf_size_adjusted =
1004 (user_buf->driver_pkt.buffer_length +
1005 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
1006 if ((req->length = data_buf_size_adjusted)) {
1007 req->data = kmalloc(data_buf_size_adjusted,
1008 TW_OSLI_MALLOC_CLASS, M_WAITOK);
1009 /* Copy the payload. */
1010 if ((error = copyin((TW_VOID *)(user_buf->pdata),
1012 user_buf->driver_pkt.buffer_length)) != 0) {
1013 tw_osli_printf(sc, "error = %d",
1014 TW_CL_SEVERITY_ERROR_STRING,
1015 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1017 "Could not copyin fw_passthru data_buf",
1019 goto fw_passthru_err;
1021 pt_req->sgl_entries = 1; /* will be updated during mapping */
1022 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
1023 TW_OSLI_REQ_FLAGS_DATA_OUT);
1025 pt_req->sgl_entries = 0; /* no payload */
1027 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
1028 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
1030 if ((error = tw_osli_map_request(req)))
1031 goto fw_passthru_err;
1033 end_time = tw_osl_get_local_time() + timeout;
1034 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
1035 lockmgr(req->ioctl_wake_timeout_lock, LK_EXCLUSIVE);
1036 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
1038 error = lksleep(req, req->ioctl_wake_timeout_lock, 0,
1039 "twa_passthru", timeout*hz);
1040 lockmgr(req->ioctl_wake_timeout_lock, LK_RELEASE);
1042 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
1044 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1047 if (((error = req->error_code)) ||
1048 ((error = (req->state !=
1049 TW_OSLI_REQ_STATE_COMPLETE))) ||
1050 ((error = req_pkt->status)))
1051 goto fw_passthru_err;
1055 if (req_pkt->status) {
1056 error = req_pkt->status;
1057 goto fw_passthru_err;
1060 if (error == EWOULDBLOCK) {
1062 if ((!(req->error_code)) &&
1063 (req->state == TW_OSLI_REQ_STATE_COMPLETE) &&
1064 (!(req_pkt->status)) ) {
1066 tw_osli_printf(sc, "request = %p",
1067 TW_CL_SEVERITY_ERROR_STRING,
1068 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1070 "FALSE Passthru timeout!",
1072 #endif /* TW_OSL_DEBUG */
1073 error = 0; /* False error */
1076 if (!(tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
1078 tw_osli_printf(sc, "request = %p",
1079 TW_CL_SEVERITY_ERROR_STRING,
1080 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1082 "Passthru request timed out!",
1084 #else /* TW_OSL_DEBUG */
1085 device_printf((sc)->bus_dev, "Passthru request timed out!\n");
1086 #endif /* TW_OSL_DEBUG */
1087 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
1091 end_time = tw_osl_get_local_time() + timeout;
1094 * Don't touch req after a reset. It (and any
1095 * associated data) will be
1096 * unmapped by the callback.
1100 * Either the request got completed, or we were woken up by a
1101 * signal. Calculate the new timeout, in case it was the latter.
1103 timeout = (end_time - tw_osl_get_local_time());
1104 } /* End of while loop */
1106 /* If there was a payload, copy it back. */
1107 if ((!error) && (req->length))
1108 if ((error = copyout(req->data, user_buf->pdata,
1109 user_buf->driver_pkt.buffer_length)))
1110 tw_osli_printf(sc, "error = %d",
1111 TW_CL_SEVERITY_ERROR_STRING,
1112 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1114 "Could not copyout fw_passthru data_buf",
1119 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1122 user_buf->driver_pkt.os_status = error;
1123 /* Free resources. */
1125 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1126 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1133 * Function name: tw_osl_complete_passthru
1134 * Description: Called to complete passthru requests.
1136 * Input: req_handle -- ptr to request handle
1138 * Return value: None
1141 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1143 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1144 struct tw_cl_req_packet *req_pkt =
1145 (struct tw_cl_req_packet *)(&req->req_pkt);
1146 struct twa_softc *sc = req->ctlr;
1148 tw_osli_dbg_dprintf(5, sc, "entered");
1150 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1151 tw_osli_printf(sc, "request = %p, status = %d",
1152 TW_CL_SEVERITY_ERROR_STRING,
1153 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1155 "Unposted command completed!!",
1160 * Remove request from the busy queue. Just mark it complete.
1161 * There's no need to move it into the complete queue as we are
1162 * going to be done with it right now.
1164 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1165 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1167 tw_osli_unmap_request(req);
1170 * Don't do a wake up if there was an error even before the request
1171 * was sent down to the Common Layer, and we hadn't gotten an
1172 * EINPROGRESS. The request originator will then be returned an
1173 * error, and he can do the clean-up.
1175 if ((req->error_code) && (!(req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1178 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1179 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1180 /* Wake up the sleeping command originator. */
1181 tw_osli_dbg_dprintf(5, sc,
1182 "Waking up originator of request %p", req);
1183 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1187 * If the request completed even before mtx_sleep
1188 * was called, simply return.
1190 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1193 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1196 tw_osli_printf(sc, "request = %p",
1197 TW_CL_SEVERITY_ERROR_STRING,
1198 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1200 "Passthru callback called, "
1201 "and caller not sleeping",
1205 tw_osli_printf(sc, "request = %p",
1206 TW_CL_SEVERITY_ERROR_STRING,
1207 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1209 "Passthru callback called for non-passthru request",
1217 * Function name: tw_osli_get_request
1218 * Description: Gets a request pkt from the free queue.
1220 * Input: sc -- ptr to OSL internal ctlr context
1222 * Return value: ptr to request pkt -- success
1225 struct tw_osli_req_context *
1226 tw_osli_get_request(struct twa_softc *sc)
1228 struct tw_osli_req_context *req;
1230 tw_osli_dbg_dprintf(4, sc, "entered");
1232 /* Get a free request packet. */
1233 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1235 /* Initialize some fields to their defaults. */
1237 req->req_handle.osl_req_ctxt = NULL;
1238 req->req_handle.cl_req_ctxt = NULL;
1239 req->req_handle.is_io = 0;
1243 req->real_data = NULL;
1244 req->real_length = 0;
1245 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1247 req->error_code = 0;
1248 req->orig_req = NULL;
1250 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1259 * Function name: twa_map_load_data_callback
1260 * Description: Callback of bus_dmamap_load for the buffer associated
1261 * with data. Updates the cmd pkt (size/sgl_entries
1262 * fields, as applicable) to reflect the number of sg
1265 * Input: arg -- ptr to OSL internal request context
1266 * segs -- ptr to a list of segment descriptors
1267 * nsegments--# of segments
1268 * error -- 0 if no errors encountered before callback,
1269 * non-zero if errors were encountered
1271 * Return value: None
1274 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1275 TW_INT32 nsegments, TW_INT32 error)
1277 struct tw_osli_req_context *req =
1278 (struct tw_osli_req_context *)arg;
1279 struct twa_softc *sc = req->ctlr;
1280 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1282 tw_osli_dbg_dprintf(10, sc, "entered");
1284 if (error == EINVAL) {
1285 req->error_code = error;
1289 /* Mark the request as currently being processed. */
1290 req->state = TW_OSLI_REQ_STATE_BUSY;
1291 /* Move the request into the busy queue. */
1292 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1294 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1296 if (error == EFBIG) {
1297 req->error_code = error;
1301 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1302 struct tw_cl_passthru_req_packet *pt_req;
1304 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1305 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1306 BUS_DMASYNC_PREREAD);
1308 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1310 * If we're using an alignment buffer, and we're
1311 * writing data, copy the real data out.
1313 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1314 bcopy(req->real_data, req->data, req->real_length);
1315 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1316 BUS_DMASYNC_PREWRITE);
1319 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1320 pt_req->sg_list = (TW_UINT8 *)segs;
1321 pt_req->sgl_entries += (nsegments - 1);
1322 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1323 &(req->req_handle));
1325 struct tw_cl_scsi_req_packet *scsi_req;
1327 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1328 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1329 BUS_DMASYNC_PREREAD);
1331 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1333 * If we're using an alignment buffer, and we're
1334 * writing data, copy the real data out.
1336 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1337 bcopy(req->real_data, req->data, req->real_length);
1338 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1339 BUS_DMASYNC_PREWRITE);
1342 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1343 scsi_req->sg_list = (TW_UINT8 *)segs;
1344 scsi_req->sgl_entries += (nsegments - 1);
1345 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1346 &(req->req_handle));
1351 req->error_code = error;
1352 req_pkt->tw_osl_callback(&(req->req_handle));
1354 * If the caller had been returned EINPROGRESS, and he has
1355 * registered a callback for handling completion, the callback
1356 * will never get called because we were unable to submit the
1357 * request. So, free up the request right here.
1359 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1360 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1367 * Function name: twa_map_load_callback
1368 * Description: Callback of bus_dmamap_load for the buffer associated
1371 * Input: arg -- ptr to variable to hold phys addr
1372 * segs -- ptr to a list of segment descriptors
1373 * nsegments--# of segments
1374 * error -- 0 if no errors encountered before callback,
1375 * non-zero if errors were encountered
1377 * Return value: None
1380 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1381 TW_INT32 nsegments, TW_INT32 error)
1383 *((bus_addr_t *)arg) = segs[0].ds_addr;
1389 * Function name: tw_osli_map_request
1390 * Description: Maps a cmd pkt and data associated with it, into
1393 * Input: req -- ptr to request pkt
1395 * Return value: 0 -- success
1396 * non-zero-- failure
1399 tw_osli_map_request(struct tw_osli_req_context *req)
1401 struct twa_softc *sc = req->ctlr;
1404 tw_osli_dbg_dprintf(10, sc, "entered");
1406 /* If the command involves data, map that too. */
1407 if (req->data != NULL) {
1409 * It's sufficient for the data pointer to be 4-byte aligned
1410 * to work with 9000. However, if 4-byte aligned addresses
1411 * are passed to bus_dmamap_load, we can get back sg elements
1412 * that are not 512-byte multiples in size. So, we will let
1413 * only those buffers that are 512-byte aligned to pass
1414 * through, and bounce the rest, so as to make sure that we
1415 * always get back sg elements that are 512-byte multiples
1418 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1419 (req->length % sc->sg_size_factor)) {
1420 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1421 /* Save original data pointer and length. */
1422 req->real_data = req->data;
1423 req->real_length = req->length;
1424 req->length = (req->length +
1425 (sc->sg_size_factor - 1)) &
1426 ~(sc->sg_size_factor - 1);
1427 req->data = kmalloc(req->length, TW_OSLI_MALLOC_CLASS,
1429 if (req->data == NULL) {
1430 tw_osli_printf(sc, "error = %d",
1431 TW_CL_SEVERITY_ERROR_STRING,
1432 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1434 "Failed to allocate memory "
1435 "for bounce buffer",
1437 /* Restore original data pointer and length. */
1438 req->data = req->real_data;
1439 req->length = req->real_length;
1445 * Map the data buffer into bus space and build the SG list.
1447 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1448 /* Lock against multiple simultaneous ioctl calls. */
1449 spin_lock(sc->io_lock);
1450 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1451 req->data, req->length,
1452 twa_map_load_data_callback, req,
1454 spin_unlock(sc->io_lock);
1457 * There's only one CAM I/O thread running at a time.
1458 * So, there's no need to hold the io_lock.
1460 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1461 req->data, req->length,
1462 twa_map_load_data_callback, req,
1467 error = req->error_code;
1469 if (error == EINPROGRESS) {
1471 * Specifying sc->io_lock as the lockfuncarg
1472 * in ...tag_create should protect the access
1473 * of ...FLAGS_MAPPED from the callback.
1475 spin_lock(sc->io_lock);
1476 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED))
1477 req->flags |= TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1478 tw_osli_disallow_new_requests(sc, &(req->req_handle));
1479 spin_unlock(sc->io_lock);
1482 tw_osli_printf(sc, "error = %d",
1483 TW_CL_SEVERITY_ERROR_STRING,
1484 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1486 "Failed to map DMA memory "
1489 req->flags |= TW_OSLI_REQ_FLAGS_FAILED;
1490 /* Free alignment buffer if it was used. */
1492 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1493 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1495 * Restore original data pointer
1498 req->data = req->real_data;
1499 req->length = req->real_length;
1505 /* Mark the request as currently being processed. */
1506 req->state = TW_OSLI_REQ_STATE_BUSY;
1507 /* Move the request into the busy queue. */
1508 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1509 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1510 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1511 &(req->req_pkt), &(req->req_handle));
1513 error = tw_cl_start_io(&sc->ctlr_handle,
1514 &(req->req_pkt), &(req->req_handle));
1516 req->error_code = error;
1517 req->req_pkt.tw_osl_callback(&(req->req_handle));
1526 * Function name: tw_osli_unmap_request
1527 * Description: Undoes the mapping done by tw_osli_map_request.
1529 * Input: req -- ptr to request pkt
1531 * Return value: None
1534 tw_osli_unmap_request(struct tw_osli_req_context *req)
1536 struct twa_softc *sc = req->ctlr;
1538 tw_osli_dbg_dprintf(10, sc, "entered");
1540 /* If the command involved data, unmap that too. */
1541 if (req->data != NULL) {
1542 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1543 /* Lock against multiple simultaneous ioctl calls. */
1544 spin_lock(sc->io_lock);
1546 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1547 bus_dmamap_sync(sc->ioctl_tag,
1548 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1551 * If we are using a bounce buffer, and we are
1552 * reading data, copy the real data in.
1554 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1555 bcopy(req->data, req->real_data,
1559 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1560 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1561 BUS_DMASYNC_POSTWRITE);
1563 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1565 spin_unlock(sc->io_lock);
1567 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1568 bus_dmamap_sync(sc->dma_tag,
1569 req->dma_map, BUS_DMASYNC_POSTREAD);
1572 * If we are using a bounce buffer, and we are
1573 * reading data, copy the real data in.
1575 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1576 bcopy(req->data, req->real_data,
1579 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1580 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1581 BUS_DMASYNC_POSTWRITE);
1583 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1587 /* Free alignment buffer if it was used. */
1588 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1589 kfree(req->data, TW_OSLI_MALLOC_CLASS);
1590 /* Restore original data pointer and length. */
1591 req->data = req->real_data;
1592 req->length = req->real_length;
1600 TW_VOID twa_report_stats(TW_VOID);
1601 TW_VOID twa_reset_stats(TW_VOID);
1602 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1603 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1607 * Function name: twa_report_stats
1608 * Description: For being called from ddb. Calls functions that print
1609 * OSL and CL internal stats for the controller.
1613 * Return value: None
1616 twa_report_stats(TW_VOID)
1618 struct twa_softc *sc;
1621 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1622 tw_osli_print_ctlr_stats(sc);
1623 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1630 * Function name: tw_osli_print_ctlr_stats
1631 * Description: For being called from ddb. Prints OSL controller stats
1633 * Input: sc -- ptr to OSL internal controller context
1635 * Return value: None
1638 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1640 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1641 twa_printf(sc, "OSLq type current max\n");
1642 twa_printf(sc, "free %04d %04d\n",
1643 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1644 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1645 twa_printf(sc, "busy %04d %04d\n",
1646 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1647 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1653 * Function name: twa_print_req_info
1654 * Description: For being called from ddb. Calls functions that print
1655 * OSL and CL internal details for the request.
1657 * Input: req -- ptr to OSL internal request context
1659 * Return value: None
1662 twa_print_req_info(struct tw_osli_req_context *req)
1664 struct twa_softc *sc = req->ctlr;
1666 twa_printf(sc, "OSL details for request:\n");
1667 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1668 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1669 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1670 "next_req = %p, prev_req = %p, dma_map = %p\n",
1671 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1672 req->data, req->length, req->real_data, req->real_length,
1673 req->state, req->flags, req->error_code, req->orig_req,
1674 req->link.next, req->link.prev, req->dma_map);
1675 tw_cl_print_req_info(&(req->req_handle));
1681 * Function name: twa_reset_stats
1682 * Description: For being called from ddb.
1683 * Resets some OSL controller stats.
1687 * Return value: None
1690 twa_reset_stats(TW_VOID)
1692 struct twa_softc *sc;
1695 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1696 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1697 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1698 tw_cl_reset_stats(&sc->ctlr_handle);
1702 #endif /* TW_OSL_DEBUG */