2 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
3 * Copyright (c) 2004-05 Vinod Kashyap.
4 * Copyright (c) 2000 Michael Smith
5 * Copyright (c) 2000 BSDi
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * AMCC'S 3ware driver for 9000 series storage controllers.
35 * Author: Vinod Kashyap
36 * Modifications by: Adam Radford
37 * Modifications by: Manjunath Ranganathaiah
42 * FreeBSD specific functions not related to CAM, and other
43 * miscellaneous functions.
47 #include <dev/twa/tw_osl_includes.h>
48 #include <dev/twa/tw_cl_fwif.h>
49 #include <dev/twa/tw_cl_ioctl.h>
50 #include <dev/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 cdevsw twa_cdevsw = {
65 .d_version = D_VERSION,
72 static devclass_t twa_devclass;
76 * Function name: twa_open
77 * Description: Called when the controller is opened.
78 * Simply marks the controller as open.
80 * Input: dev -- control device corresponding to the ctlr
81 * flags -- mode of open
82 * fmt -- device type (character/block etc.)
83 * proc -- current process
85 * Return value: 0 -- success
89 twa_open(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, struct thread *proc)
91 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
93 tw_osli_dbg_dprintf(5, sc, "entered");
94 sc->state |= TW_OSLI_CTLR_STATE_OPEN;
101 * Function name: twa_close
102 * Description: Called when the controller is closed.
103 * Simply marks the controller as not open.
105 * Input: dev -- control device corresponding to the ctlr
106 * flags -- mode of corresponding open
107 * fmt -- device type (character/block etc.)
108 * proc -- current process
110 * Return value: 0 -- success
114 twa_close(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, struct thread *proc)
116 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
118 tw_osli_dbg_dprintf(5, sc, "entered");
119 sc->state &= ~TW_OSLI_CTLR_STATE_OPEN;
126 * Function name: twa_ioctl
127 * Description: Called when an ioctl is posted to the controller.
128 * Handles any OS Layer specific cmds, passes the rest
129 * on to the Common Layer.
131 * Input: dev -- control device corresponding to the ctlr
133 * buf -- ptr to buffer in kernel memory, which is
134 * a copy of the input buffer in user-space
135 * flags -- mode of corresponding open
136 * proc -- current process
137 * Output: buf -- ptr to buffer in kernel memory, which will
138 * be copied to the output buffer in user-space
139 * Return value: 0 -- success
143 twa_ioctl(struct cdev *dev, u_long cmd, caddr_t buf, TW_INT32 flags, struct thread *proc)
145 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
148 tw_osli_dbg_dprintf(5, sc, "entered");
151 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
152 tw_osli_dbg_dprintf(6, sc, "ioctl: fw_passthru");
153 error = tw_osli_fw_passthru(sc, (TW_INT8 *)buf);
156 case TW_OSL_IOCTL_SCAN_BUS:
157 /* Request CAM for a bus scan. */
158 tw_osli_dbg_dprintf(6, sc, "ioctl: scan bus");
159 error = tw_osli_request_bus_scan(sc);
163 tw_osli_dbg_dprintf(6, sc, "ioctl: 0x%lx", cmd);
164 error = tw_cl_ioctl(&sc->ctlr_handle, cmd, buf);
172 static TW_INT32 twa_probe(device_t dev);
173 static TW_INT32 twa_attach(device_t dev);
174 static TW_INT32 twa_detach(device_t dev);
175 static TW_INT32 twa_shutdown(device_t dev);
176 static TW_VOID twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op);
177 #ifdef TW_OSLI_DEFERRED_INTR_USED
178 static int twa_pci_intr_fast(TW_VOID *arg);
179 static TW_VOID twa_deferred_intr(TW_VOID *context, TW_INT32 pending);
181 static TW_VOID twa_pci_intr(TW_VOID *arg);
182 #endif /* TW_OSLI_DEFERRED_INTR_USED */
184 static TW_INT32 tw_osli_alloc_mem(struct twa_softc *sc);
185 static TW_VOID tw_osli_free_resources(struct twa_softc *sc);
187 static TW_VOID twa_map_load_data_callback(TW_VOID *arg,
188 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
189 static TW_VOID twa_map_load_callback(TW_VOID *arg,
190 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
193 static device_method_t twa_methods[] = {
194 /* Device interface */
195 DEVMETHOD(device_probe, twa_probe),
196 DEVMETHOD(device_attach, twa_attach),
197 DEVMETHOD(device_detach, twa_detach),
198 DEVMETHOD(device_shutdown, twa_shutdown),
200 DEVMETHOD(bus_print_child, bus_generic_print_child),
201 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
205 static driver_t twa_pci_driver = {
208 sizeof(struct twa_softc)
211 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
212 MODULE_DEPEND(twa, cam, 1, 1, 1);
213 MODULE_DEPEND(twa, pci, 1, 1, 1);
217 * Function name: twa_probe
218 * Description: Called at driver load time. Claims 9000 ctlrs.
220 * Input: dev -- bus device corresponding to the ctlr
222 * Return value: <= 0 -- success
226 twa_probe(device_t dev)
228 static TW_UINT8 first_ctlr = 1;
230 tw_osli_dbg_printf(3, "entered");
232 if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
233 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
234 /* Print the driver version only once. */
236 printf("3ware device driver for 9000 series storage "
237 "controllers, version: %s\n",
238 TW_OSL_DRIVER_VERSION_STRING);
249 * Function name: twa_attach
250 * Description: Allocates pci resources; updates sc; adds a node to the
251 * sysctl tree to expose the driver version; makes calls
252 * (to the Common Layer) to initialize ctlr, and to
255 * Input: dev -- bus device corresponding to the ctlr
257 * Return value: 0 -- success
261 twa_attach(device_t dev)
263 struct twa_softc *sc = device_get_softc(dev);
266 TW_INT32 bar0_offset;
270 tw_osli_dbg_dprintf(3, sc, "entered");
272 sc->ctlr_handle.osl_ctlr_ctxt = sc;
274 /* Initialize the softc structure. */
276 sc->device_id = pci_get_device(dev);
278 /* Initialize the mutexes right here. */
279 sc->io_lock = &(sc->io_lock_handle);
280 mtx_init(sc->io_lock, "tw_osl_io_lock", NULL, MTX_SPIN);
281 sc->q_lock = &(sc->q_lock_handle);
282 mtx_init(sc->q_lock, "tw_osl_q_lock", NULL, MTX_SPIN);
283 sc->sim_lock = &(sc->sim_lock_handle);
284 mtx_init(sc->sim_lock, "tw_osl_sim_lock", NULL, MTX_DEF | MTX_RECURSE);
286 sysctl_ctx_init(&sc->sysctl_ctxt);
287 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
288 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
289 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
290 if (sc->sysctl_tree == NULL) {
291 tw_osli_printf(sc, "error = %d",
292 TW_CL_SEVERITY_ERROR_STRING,
293 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
295 "Cannot add sysctl tree node",
299 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
300 OID_AUTO, "driver_version", CTLFLAG_RD,
301 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
303 /* Make sure we are going to be able to talk to this board. */
304 command = pci_read_config(dev, PCIR_COMMAND, 2);
305 if ((command & PCIM_CMD_PORTEN) == 0) {
306 tw_osli_printf(sc, "error = %d",
307 TW_CL_SEVERITY_ERROR_STRING,
308 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
310 "Register window not available",
312 tw_osli_free_resources(sc);
316 /* Force the busmaster enable bit on, in case the BIOS forgot. */
317 command |= PCIM_CMD_BUSMASTEREN;
318 pci_write_config(dev, PCIR_COMMAND, command, 2);
320 /* Allocate the PCI register window. */
321 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
322 &bar_num, &bar0_offset, &bar_size))) {
323 tw_osli_printf(sc, "error = %d",
324 TW_CL_SEVERITY_ERROR_STRING,
325 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
327 "Can't get PCI BAR info",
329 tw_osli_free_resources(sc);
332 sc->reg_res_id = PCIR_BARS + bar0_offset;
333 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
334 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
336 tw_osli_printf(sc, "error = %d",
337 TW_CL_SEVERITY_ERROR_STRING,
338 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
340 "Can't allocate register window",
342 tw_osli_free_resources(sc);
345 sc->bus_tag = rman_get_bustag(sc->reg_res);
346 sc->bus_handle = rman_get_bushandle(sc->reg_res);
348 /* Allocate and register our interrupt. */
350 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
351 &(sc->irq_res_id), 0, ~0, 1,
352 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
353 tw_osli_printf(sc, "error = %d",
354 TW_CL_SEVERITY_ERROR_STRING,
355 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
357 "Can't allocate interrupt",
359 tw_osli_free_resources(sc);
362 if ((error = bus_setup_intr(sc->bus_dev, sc->irq_res,
363 #ifdef TW_OSLI_DEFERRED_INTR_USED
364 INTR_TYPE_CAM | INTR_FAST,
365 twa_pci_intr_fast, NULL,
367 INTR_TYPE_CAM | INTR_MPSAFE,
370 sc, &sc->intr_handle))) {
371 tw_osli_printf(sc, "error = %d",
372 TW_CL_SEVERITY_ERROR_STRING,
373 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
375 "Can't set up interrupt",
377 tw_osli_free_resources(sc);
381 #ifdef TW_OSLI_DEFERRED_INTR_USED
382 TASK_INIT(&sc->deferred_intr_callback, 0, twa_deferred_intr, sc);
383 #endif /* TW_OSLI_DEFERRED_INTR_USED */
385 if ((error = tw_osli_alloc_mem(sc))) {
386 tw_osli_printf(sc, "error = %d",
387 TW_CL_SEVERITY_ERROR_STRING,
388 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
390 "Memory allocation failure",
392 tw_osli_free_resources(sc);
396 /* Initialize the Common Layer for this controller. */
397 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
398 TW_OSLI_MAX_NUM_IOS, TW_OSLI_MAX_NUM_AENS,
399 sc->non_dma_mem, sc->dma_mem,
402 tw_osli_printf(sc, "error = %d",
403 TW_CL_SEVERITY_ERROR_STRING,
404 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
406 "Failed to initialize Common Layer/controller",
408 tw_osli_free_resources(sc);
412 /* Create the control device. */
413 sc->ctrl_dev = make_dev(&twa_cdevsw, device_get_unit(sc->bus_dev),
414 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
415 "twa%d", device_get_unit(sc->bus_dev));
416 sc->ctrl_dev->si_drv1 = sc;
418 if ((error = tw_osli_cam_attach(sc))) {
419 tw_osli_free_resources(sc);
420 tw_osli_printf(sc, "error = %d",
421 TW_CL_SEVERITY_ERROR_STRING,
422 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
424 "Failed to initialize CAM",
435 * Function name: tw_osli_alloc_mem
436 * Description: Allocates memory needed both by CL and OSL.
438 * Input: sc -- OSL internal controller context
440 * Return value: 0 -- success
444 tw_osli_alloc_mem(struct twa_softc *sc)
446 struct tw_osli_req_context *req;
447 TW_UINT32 max_sg_elements;
448 TW_UINT32 non_dma_mem_size;
449 TW_UINT32 dma_mem_size;
453 tw_osli_dbg_dprintf(3, sc, "entered");
455 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
456 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
457 #ifdef TW_OSLI_DEFERRED_INTR_USED
458 sc->flags |= TW_CL_DEFERRED_INTR_USED;
459 #endif /* TW_OSLI_DEFERRED_INTR_USED */
461 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
462 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
464 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
465 sc->device_id, TW_OSLI_MAX_NUM_IOS, TW_OSLI_MAX_NUM_AENS,
466 &(sc->alignment), &(sc->sg_size_factor),
467 &non_dma_mem_size, &dma_mem_size
469 tw_osli_printf(sc, "error = %d",
470 TW_CL_SEVERITY_ERROR_STRING,
471 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
473 "Can't get Common Layer's memory requirements",
478 if ((sc->non_dma_mem = malloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
479 M_WAITOK)) == NULL) {
480 tw_osli_printf(sc, "error = %d",
481 TW_CL_SEVERITY_ERROR_STRING,
482 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
484 "Can't allocate non-dma memory",
489 /* Create the parent dma tag. */
490 if (bus_dma_tag_create(NULL, /* parent */
491 sc->alignment, /* alignment */
492 TW_OSLI_DMA_BOUNDARY, /* boundary */
493 BUS_SPACE_MAXADDR, /* lowaddr */
494 BUS_SPACE_MAXADDR, /* highaddr */
495 NULL, NULL, /* filter, filterarg */
496 TW_CL_MAX_IO_SIZE, /* maxsize */
497 max_sg_elements, /* nsegments */
498 TW_CL_MAX_IO_SIZE, /* maxsegsize */
501 NULL, /* lockfuncarg */
502 &sc->parent_tag /* tag */)) {
503 tw_osli_printf(sc, "error = %d",
504 TW_CL_SEVERITY_ERROR_STRING,
505 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
507 "Can't allocate parent DMA tag",
512 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
513 if (bus_dma_tag_create(sc->parent_tag, /* parent */
514 sc->alignment, /* alignment */
516 BUS_SPACE_MAXADDR, /* lowaddr */
517 BUS_SPACE_MAXADDR, /* highaddr */
518 NULL, NULL, /* filter, filterarg */
519 dma_mem_size, /* maxsize */
521 BUS_SPACE_MAXSIZE, /* maxsegsize */
524 NULL, /* lockfuncarg */
525 &sc->cmd_tag /* tag */)) {
526 tw_osli_printf(sc, "error = %d",
527 TW_CL_SEVERITY_ERROR_STRING,
528 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
530 "Can't allocate DMA tag for Common Layer's "
536 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
537 BUS_DMA_NOWAIT, &sc->cmd_map)) {
538 /* Try a second time. */
539 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
540 BUS_DMA_NOWAIT, &sc->cmd_map)) {
541 tw_osli_printf(sc, "error = %d",
542 TW_CL_SEVERITY_ERROR_STRING,
543 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
545 "Can't allocate DMA'able memory for the"
552 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
553 dma_mem_size, twa_map_load_callback,
554 &sc->dma_mem_phys, 0);
557 * Create a dma tag for data buffers; size will be the maximum
558 * possible I/O size (128kB).
560 if (bus_dma_tag_create(sc->parent_tag, /* parent */
561 sc->alignment, /* alignment */
563 BUS_SPACE_MAXADDR, /* lowaddr */
564 BUS_SPACE_MAXADDR, /* highaddr */
565 NULL, NULL, /* filter, filterarg */
566 TW_CL_MAX_IO_SIZE, /* maxsize */
567 max_sg_elements, /* nsegments */
568 TW_CL_MAX_IO_SIZE, /* maxsegsize */
569 BUS_DMA_ALLOCNOW, /* flags */
570 twa_busdma_lock, /* lockfunc */
571 sc->io_lock, /* lockfuncarg */
572 &sc->dma_tag /* tag */)) {
573 tw_osli_printf(sc, "error = %d",
574 TW_CL_SEVERITY_ERROR_STRING,
575 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
577 "Can't allocate DMA tag for data buffers",
583 * Create a dma tag for ioctl data buffers; size will be the maximum
584 * possible I/O size (128kB).
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 TW_CL_MAX_IO_SIZE, /* maxsize */
593 max_sg_elements, /* nsegments */
594 TW_CL_MAX_IO_SIZE, /* maxsegsize */
595 BUS_DMA_ALLOCNOW, /* flags */
596 twa_busdma_lock, /* lockfunc */
597 sc->io_lock, /* lockfuncarg */
598 &sc->ioctl_tag /* tag */)) {
599 tw_osli_printf(sc, "error = %d",
600 TW_CL_SEVERITY_ERROR_STRING,
601 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
603 "Can't allocate DMA tag for ioctl data buffers",
608 /* Create just one map for all ioctl request data buffers. */
609 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
610 tw_osli_printf(sc, "error = %d",
611 TW_CL_SEVERITY_ERROR_STRING,
612 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
614 "Can't create ioctl map",
620 /* Initialize request queues. */
621 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
622 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
624 if ((sc->req_ctxt_buf = (struct tw_osli_req_context *)
625 malloc((sizeof(struct tw_osli_req_context) *
626 TW_OSLI_MAX_NUM_IOS),
627 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
628 tw_osli_printf(sc, "error = %d",
629 TW_CL_SEVERITY_ERROR_STRING,
630 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
632 "Failed to allocate request packets",
636 bzero(sc->req_ctxt_buf,
637 sizeof(struct tw_osli_req_context) * TW_OSLI_MAX_NUM_IOS);
639 for (i = 0; i < TW_OSLI_MAX_NUM_IOS; i++) {
640 req = &(sc->req_ctxt_buf[i]);
642 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
643 tw_osli_printf(sc, "request # = %d, error = %d",
644 TW_CL_SEVERITY_ERROR_STRING,
645 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
647 "Can't create dma map",
652 /* Insert request into the free queue. */
653 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
662 * Function name: tw_osli_free_resources
663 * Description: Performs clean-up at the time of going down.
665 * Input: sc -- ptr to OSL internal ctlr context
670 tw_osli_free_resources(struct twa_softc *sc)
672 struct tw_osli_req_context *req;
675 tw_osli_dbg_dprintf(3, sc, "entered");
677 /* Detach from CAM */
678 tw_osli_cam_detach(sc);
680 if (sc->req_ctxt_buf)
681 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
683 if ((error = bus_dmamap_destroy(sc->dma_tag,
685 tw_osli_dbg_dprintf(1, sc,
686 "dmamap_destroy(dma) returned %d",
689 if ((sc->ioctl_tag) && (sc->ioctl_map))
690 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
691 tw_osli_dbg_dprintf(1, sc,
692 "dmamap_destroy(ioctl) returned %d", error);
694 /* Free all memory allocated so far. */
695 if (sc->req_ctxt_buf)
696 free(sc->req_ctxt_buf, TW_OSLI_MALLOC_CLASS);
699 free(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
702 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
703 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
707 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
708 tw_osli_dbg_dprintf(1, sc,
709 "dma_tag_destroy(cmd) returned %d", error);
712 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
713 tw_osli_dbg_dprintf(1, sc,
714 "dma_tag_destroy(dma) returned %d", error);
717 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
718 tw_osli_dbg_dprintf(1, sc,
719 "dma_tag_destroy(ioctl) returned %d", error);
722 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
723 tw_osli_dbg_dprintf(1, sc,
724 "dma_tag_destroy(parent) returned %d", error);
727 /* Disconnect the interrupt handler. */
729 if ((error = bus_teardown_intr(sc->bus_dev,
730 sc->irq_res, sc->intr_handle)))
731 tw_osli_dbg_dprintf(1, sc,
732 "teardown_intr returned %d", error);
734 if (sc->irq_res != NULL)
735 if ((error = bus_release_resource(sc->bus_dev,
736 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
737 tw_osli_dbg_dprintf(1, sc,
738 "release_resource(irq) returned %d", error);
741 /* Release the register window mapping. */
742 if (sc->reg_res != NULL)
743 if ((error = bus_release_resource(sc->bus_dev,
744 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
745 tw_osli_dbg_dprintf(1, sc,
746 "release_resource(io) returned %d", error);
749 /* Destroy the control device. */
750 if (sc->ctrl_dev != (struct cdev *)NULL)
751 destroy_dev(sc->ctrl_dev);
753 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
754 tw_osli_dbg_dprintf(1, sc,
755 "sysctl_ctx_free returned %d", error);
762 * Function name: twa_detach
763 * Description: Called when the controller is being detached from
766 * Input: dev -- bus device corresponding to the ctlr
768 * Return value: 0 -- success
772 twa_detach(device_t dev)
774 struct twa_softc *sc = device_get_softc(dev);
777 tw_osli_dbg_dprintf(3, sc, "entered");
780 if (sc->state & TW_OSLI_CTLR_STATE_OPEN) {
781 tw_osli_printf(sc, "error = %d",
782 TW_CL_SEVERITY_ERROR_STRING,
783 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
790 /* Shut the controller down. */
791 if ((error = twa_shutdown(dev)))
794 /* Free all resources associated with this controller. */
795 tw_osli_free_resources(sc);
805 * Function name: twa_shutdown
806 * Description: Called at unload/shutdown time. Lets the controller
807 * know that we are going down.
809 * Input: dev -- bus device corresponding to the ctlr
811 * Return value: 0 -- success
815 twa_shutdown(device_t dev)
817 struct twa_softc *sc = device_get_softc(dev);
820 tw_osli_dbg_dprintf(3, sc, "entered");
822 /* Disconnect from the controller. */
823 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
824 tw_osli_printf(sc, "error = %d",
825 TW_CL_SEVERITY_ERROR_STRING,
826 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
828 "Failed to shutdown Common Layer/controller",
837 * Function name: twa_busdma_lock
838 * Description: Function to provide synchronization during busdma_swi.
840 * Input: lock_arg -- lock mutex sent as argument
841 * op -- operation (lock/unlock) expected of the function
846 twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op)
850 lock = (struct mtx *)lock_arg;
857 mtx_unlock_spin(lock);
861 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
866 #ifdef TW_OSLI_DEFERRED_INTR_USED
868 * Function name: twa_pci_intr_fast
869 * Description: Interrupt handler. Wrapper for twa_interrupt.
871 * Input: arg -- ptr to OSL internal ctlr context
872 * Output: FILTER_HANDLED or FILTER_STRAY
876 twa_pci_intr_fast(TW_VOID *arg)
878 struct twa_softc *sc = (struct twa_softc *)arg;
880 tw_osli_dbg_dprintf(10, sc, "entered");
881 if (tw_cl_interrupt(&(sc->ctlr_handle))) {
882 taskqueue_enqueue_fast(taskqueue_fast,
883 &(sc->deferred_intr_callback));
884 return(FILTER_HANDLED);
886 return(FILTER_STRAY);
890 * Function name: twa_pci_intr
891 * Description: Interrupt handler. Wrapper for twa_interrupt.
893 * Input: arg -- ptr to OSL internal ctlr context
898 twa_pci_intr(TW_VOID *arg)
900 struct twa_softc *sc = (struct twa_softc *)arg;
902 tw_osli_dbg_dprintf(10, sc, "entered");
903 if (tw_cl_interrupt(&(sc->ctlr_handle)))
904 tw_cl_deferred_interrupt(&(sc->ctlr_handle));
908 #ifdef TW_OSLI_DEFERRED_INTR_USED
911 * Function name: twa_deferred_intr
912 * Description: Deferred interrupt handler.
914 * Input: context -- ptr to OSL internal ctlr context
915 * pending -- not used
920 twa_deferred_intr(TW_VOID *context, TW_INT32 pending)
922 struct twa_softc *sc = (struct twa_softc *)context;
924 tw_osli_dbg_dprintf(10, sc, "entered");
926 tw_cl_deferred_interrupt(&(sc->ctlr_handle));
929 #endif /* TW_OSLI_DEFERRED_INTR_USED */
934 * Function name: tw_osli_fw_passthru
935 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
937 * Input: sc -- ptr to OSL internal ctlr context
938 * buf -- ptr to ioctl pkt understood by CL
940 * Return value: 0 -- success
944 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
946 struct tw_osli_req_context *req;
947 struct tw_osli_ioctl_no_data_buf *user_buf =
948 (struct tw_osli_ioctl_no_data_buf *)buf;
950 TW_UINT32 timeout = 60;
951 TW_UINT32 data_buf_size_adjusted;
952 struct tw_cl_req_packet *req_pkt;
953 struct tw_cl_passthru_req_packet *pt_req;
956 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
958 if ((req = tw_osli_get_request(sc)) == NULL)
961 req->req_handle.osl_req_ctxt = req;
963 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
965 req_pkt = &(req->req_pkt);
967 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
968 /* Let the Common Layer retry the request on cmd queue full. */
969 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
971 pt_req = &(req_pkt->gen_req_pkt.pt_req);
973 * Make sure that the data buffer sent to firmware is a
974 * 512 byte multiple in size.
976 data_buf_size_adjusted =
977 (user_buf->driver_pkt.buffer_length +
978 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
979 if ((req->length = data_buf_size_adjusted)) {
980 if ((req->data = malloc(data_buf_size_adjusted,
981 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
983 tw_osli_printf(sc, "error = %d",
984 TW_CL_SEVERITY_ERROR_STRING,
985 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
987 "Could not alloc mem for "
988 "fw_passthru data_buf",
990 goto fw_passthru_err;
992 /* Copy the payload. */
993 if ((error = copyin((TW_VOID *)(user_buf->pdata),
995 user_buf->driver_pkt.buffer_length)) != 0) {
996 tw_osli_printf(sc, "error = %d",
997 TW_CL_SEVERITY_ERROR_STRING,
998 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1000 "Could not copyin fw_passthru data_buf",
1002 goto fw_passthru_err;
1004 pt_req->sgl_entries = 1; /* will be updated during mapping */
1005 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
1006 TW_OSLI_REQ_FLAGS_DATA_OUT);
1008 pt_req->sgl_entries = 0; /* no payload */
1010 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
1011 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
1013 if ((error = tw_osli_map_request(req)))
1014 goto fw_passthru_err;
1016 end_time = tw_osl_get_local_time() + timeout;
1017 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
1018 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
1020 error = tsleep(req, PRIBIO, "twa_passthru", timeout * hz);
1022 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
1024 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1027 if (((error = req->error_code)) ||
1028 ((error = (req->state !=
1029 TW_OSLI_REQ_STATE_COMPLETE))) ||
1030 ((error = req_pkt->status)))
1031 goto fw_passthru_err;
1035 if (req_pkt->status) {
1036 error = req_pkt->status;
1037 goto fw_passthru_err;
1040 if (error == EWOULDBLOCK) {
1042 tw_osli_printf(sc, "request = %p",
1043 TW_CL_SEVERITY_ERROR_STRING,
1044 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1046 "Passthru request timed out!",
1049 * Should I check here if the timeout happened
1050 * because of yet another reset, and not do a
1053 tw_cl_reset_ctlr(&sc->ctlr_handle);
1055 * Don't touch req after a reset. It (and any
1056 * associated data) will already have been
1057 * unmapped by the callback.
1059 user_buf->driver_pkt.os_status = error;
1061 goto fw_passthru_err;
1064 * Either the request got completed, or we were woken up by a
1065 * signal. Calculate the new timeout, in case it was the latter.
1067 timeout = (end_time - tw_osl_get_local_time());
1070 /* If there was a payload, copy it back. */
1071 if ((!error) && (req->length))
1072 if ((error = copyout(req->data, user_buf->pdata,
1073 user_buf->driver_pkt.buffer_length)))
1074 tw_osli_printf(sc, "error = %d",
1075 TW_CL_SEVERITY_ERROR_STRING,
1076 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1078 "Could not copyout fw_passthru data_buf",
1083 * Print the failure message. For some reason, on certain OS versions,
1084 * printing this error message during reset hangs the display (although
1085 * the rest of the system is running fine. So, don't print it if the
1086 * failure was due to a reset.
1088 if ((error) && (error != TW_CL_ERR_REQ_BUS_RESET))
1089 tw_osli_printf(sc, "error = %d",
1090 TW_CL_SEVERITY_ERROR_STRING,
1091 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1093 "Firmware passthru failed!",
1096 user_buf->driver_pkt.os_status = error;
1097 /* Free resources. */
1099 free(req->data, TW_OSLI_MALLOC_CLASS);
1100 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1107 * Function name: tw_osl_complete_passthru
1108 * Description: Called to complete passthru requests.
1110 * Input: req_handle -- ptr to request handle
1112 * Return value: None
1115 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1117 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1118 struct twa_softc *sc = req->ctlr;
1120 tw_osli_dbg_dprintf(5, sc, "entered");
1122 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1123 tw_osli_printf(sc, "request = %p, status = %d",
1124 TW_CL_SEVERITY_ERROR_STRING,
1125 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1127 "Unposted command completed!!",
1132 * Remove request from the busy queue. Just mark it complete.
1133 * There's no need to move it into the complete queue as we are
1134 * going to be done with it right now.
1136 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1137 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1139 tw_osli_unmap_request(req);
1142 * Don't do a wake up if there was an error even before the request
1143 * was sent down to the Common Layer, and we hadn't gotten an
1144 * EINPROGRESS. The request originator will then be returned an
1145 * error, and he can do the clean-up.
1147 if ((req->error_code) &&
1148 (!(req->state & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1151 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1152 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1153 /* Wake up the sleeping command originator. */
1154 tw_osli_dbg_dprintf(5, sc,
1155 "Waking up originator of request %p", req);
1156 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1160 * If the request completed even before tsleep
1161 * was called, simply return.
1163 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1166 tw_osli_printf(sc, "request = %p",
1167 TW_CL_SEVERITY_ERROR_STRING,
1168 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1170 "Passthru callback called, "
1171 "and caller not sleeping",
1175 tw_osli_printf(sc, "request = %p",
1176 TW_CL_SEVERITY_ERROR_STRING,
1177 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1179 "Passthru callback called for non-passthru request",
1187 * Function name: tw_osli_get_request
1188 * Description: Gets a request pkt from the free queue.
1190 * Input: sc -- ptr to OSL internal ctlr context
1192 * Return value: ptr to request pkt -- success
1195 struct tw_osli_req_context *
1196 tw_osli_get_request(struct twa_softc *sc)
1198 struct tw_osli_req_context *req;
1200 tw_osli_dbg_dprintf(4, sc, "entered");
1202 /* Get a free request packet. */
1203 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1205 /* Initialize some fields to their defaults. */
1207 req->req_handle.osl_req_ctxt = NULL;
1208 req->req_handle.cl_req_ctxt = NULL;
1211 req->real_data = NULL;
1212 req->real_length = 0;
1213 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1215 req->error_code = 0;
1216 req->orig_req = NULL;
1218 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1227 * Function name: twa_map_load_data_callback
1228 * Description: Callback of bus_dmamap_load for the buffer associated
1229 * with data. Updates the cmd pkt (size/sgl_entries
1230 * fields, as applicable) to reflect the number of sg
1233 * Input: arg -- ptr to OSL internal request context
1234 * segs -- ptr to a list of segment descriptors
1235 * nsegments--# of segments
1236 * error -- 0 if no errors encountered before callback,
1237 * non-zero if errors were encountered
1239 * Return value: None
1242 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1243 TW_INT32 nsegments, TW_INT32 error)
1245 struct tw_osli_req_context *req =
1246 (struct tw_osli_req_context *)arg;
1247 struct twa_softc *sc = req->ctlr;
1248 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1250 tw_osli_dbg_dprintf(10, sc, "entered");
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;
1258 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1259 tw_osli_allow_new_requests(sc, (TW_VOID *)(req->orig_req));
1261 if (error == EFBIG) {
1262 req->error_code = error;
1266 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1267 struct tw_cl_passthru_req_packet *pt_req;
1269 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1270 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1271 BUS_DMASYNC_PREREAD);
1273 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1275 * If we're using an alignment buffer, and we're
1276 * writing data, copy the real data out.
1278 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1279 bcopy(req->real_data, req->data, req->real_length);
1280 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1281 BUS_DMASYNC_PREWRITE);
1284 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1285 pt_req->sg_list = (TW_UINT8 *)segs;
1286 pt_req->sgl_entries += (nsegments - 1);
1287 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1288 &(req->req_handle));
1290 struct tw_cl_scsi_req_packet *scsi_req;
1292 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1293 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1294 BUS_DMASYNC_PREREAD);
1296 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1298 * If we're using an alignment buffer, and we're
1299 * writing data, copy the real data out.
1301 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1302 bcopy(req->real_data, req->data, req->real_length);
1303 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1304 BUS_DMASYNC_PREWRITE);
1307 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1308 scsi_req->sg_list = (TW_UINT8 *)segs;
1309 scsi_req->sgl_entries += (nsegments - 1);
1310 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1311 &(req->req_handle));
1316 req->error_code = error;
1317 req_pkt->tw_osl_callback(&(req->req_handle));
1319 * If the caller had been returned EINPROGRESS, and he has
1320 * registered a callback for handling completion, the callback
1321 * will never get called because we were unable to submit the
1322 * request. So, free up the request right here.
1324 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1325 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1332 * Function name: twa_map_load_callback
1333 * Description: Callback of bus_dmamap_load for the buffer associated
1336 * Input: arg -- ptr to variable to hold phys addr
1337 * segs -- ptr to a list of segment descriptors
1338 * nsegments--# of segments
1339 * error -- 0 if no errors encountered before callback,
1340 * non-zero if errors were encountered
1342 * Return value: None
1345 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1346 TW_INT32 nsegments, TW_INT32 error)
1348 *((TW_UINT64 *)arg) = segs[0].ds_addr;
1354 * Function name: tw_osli_map_request
1355 * Description: Maps a cmd pkt and data associated with it, into
1358 * Input: req -- ptr to request pkt
1360 * Return value: 0 -- success
1361 * non-zero-- failure
1364 tw_osli_map_request(struct tw_osli_req_context *req)
1366 struct twa_softc *sc = req->ctlr;
1369 tw_osli_dbg_dprintf(10, sc, "entered");
1371 /* If the command involves data, map that too. */
1372 if (req->data != NULL) {
1374 * It's sufficient for the data pointer to be 4-byte aligned
1375 * to work with 9000. However, if 4-byte aligned addresses
1376 * are passed to bus_dmamap_load, we can get back sg elements
1377 * that are not 512-byte multiples in size. So, we will let
1378 * only those buffers that are 512-byte aligned to pass
1379 * through, and bounce the rest, so as to make sure that we
1380 * always get back sg elements that are 512-byte multiples
1383 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1384 (req->length % sc->sg_size_factor)) {
1385 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1386 /* Save original data pointer and length. */
1387 req->real_data = req->data;
1388 req->real_length = req->length;
1389 req->length = (req->length +
1390 (sc->sg_size_factor - 1)) &
1391 ~(sc->sg_size_factor - 1);
1392 req->data = malloc(req->length, TW_OSLI_MALLOC_CLASS,
1394 if (req->data == NULL) {
1395 tw_osli_printf(sc, "error = %d",
1396 TW_CL_SEVERITY_ERROR_STRING,
1397 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1399 "Failed to allocate memory "
1400 "for bounce buffer",
1402 /* Restore original data pointer and length. */
1403 req->data = req->real_data;
1404 req->length = req->real_length;
1410 * Map the data buffer into bus space and build the SG list.
1412 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1413 /* Lock against multiple simultaneous ioctl calls. */
1414 mtx_lock_spin(sc->io_lock);
1415 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1416 req->data, req->length,
1417 twa_map_load_data_callback, req,
1419 mtx_unlock_spin(sc->io_lock);
1422 * There's only one CAM I/O thread running at a time.
1423 * So, there's no need to hold the io_lock.
1425 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1426 req->data, req->length,
1427 twa_map_load_data_callback, req,
1432 error = req->error_code;
1434 if (error == EINPROGRESS) {
1436 * Specifying sc->io_lock as the lockfuncarg
1437 * in ...tag_create should protect the access
1438 * of ...FLAGS_MAPPED from the callback.
1440 mtx_lock_spin(sc->io_lock);
1441 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED)) {
1443 TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1444 tw_osli_disallow_new_requests(sc);
1446 mtx_unlock_spin(sc->io_lock);
1449 /* Free alignment buffer if it was used. */
1451 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1452 free(req->data, TW_OSLI_MALLOC_CLASS);
1454 * Restore original data pointer
1457 req->data = req->real_data;
1458 req->length = req->real_length;
1464 /* Mark the request as currently being processed. */
1465 req->state = TW_OSLI_REQ_STATE_BUSY;
1466 /* Move the request into the busy queue. */
1467 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1468 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1469 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1470 &(req->req_pkt), &(req->req_handle));
1472 error = tw_cl_start_io(&sc->ctlr_handle,
1473 &(req->req_pkt), &(req->req_handle));
1475 req->error_code = error;
1476 req->req_pkt.tw_osl_callback(&(req->req_handle));
1485 * Function name: tw_osli_unmap_request
1486 * Description: Undoes the mapping done by tw_osli_map_request.
1488 * Input: req -- ptr to request pkt
1490 * Return value: None
1493 tw_osli_unmap_request(struct tw_osli_req_context *req)
1495 struct twa_softc *sc = req->ctlr;
1497 tw_osli_dbg_dprintf(10, sc, "entered");
1499 /* If the command involved data, unmap that too. */
1500 if (req->data != NULL) {
1501 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1502 /* Lock against multiple simultaneous ioctl calls. */
1503 mtx_lock_spin(sc->io_lock);
1505 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1506 bus_dmamap_sync(sc->ioctl_tag,
1507 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1510 * If we are using a bounce buffer, and we are
1511 * reading data, copy the real data in.
1513 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1514 bcopy(req->data, req->real_data,
1518 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1519 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1520 BUS_DMASYNC_POSTWRITE);
1522 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1524 mtx_unlock_spin(sc->io_lock);
1526 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1527 bus_dmamap_sync(sc->dma_tag,
1528 req->dma_map, BUS_DMASYNC_POSTREAD);
1531 * If we are using a bounce buffer, and we are
1532 * reading data, copy the real data in.
1534 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1535 bcopy(req->data, req->real_data,
1538 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1539 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1540 BUS_DMASYNC_POSTWRITE);
1542 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1546 /* Free alignment buffer if it was used. */
1547 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1548 free(req->data, TW_OSLI_MALLOC_CLASS);
1549 /* Restore original data pointer and length. */
1550 req->data = req->real_data;
1551 req->length = req->real_length;
1559 TW_VOID twa_report_stats(TW_VOID);
1560 TW_VOID twa_reset_stats(TW_VOID);
1561 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1562 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1566 * Function name: twa_report_stats
1567 * Description: For being called from ddb. Calls functions that print
1568 * OSL and CL internal stats for the controller.
1572 * Return value: None
1575 twa_report_stats(TW_VOID)
1577 struct twa_softc *sc;
1580 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1581 tw_osli_print_ctlr_stats(sc);
1582 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1589 * Function name: tw_osli_print_ctlr_stats
1590 * Description: For being called from ddb. Prints OSL controller stats
1592 * Input: sc -- ptr to OSL internal controller context
1594 * Return value: None
1597 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1599 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1600 twa_printf(sc, "OSLq type current max\n");
1601 twa_printf(sc, "free %04d %04d\n",
1602 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1603 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1604 twa_printf(sc, "busy %04d %04d\n",
1605 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1606 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1612 * Function name: twa_print_req_info
1613 * Description: For being called from ddb. Calls functions that print
1614 * OSL and CL internal details for the request.
1616 * Input: req -- ptr to OSL internal request context
1618 * Return value: None
1621 twa_print_req_info(struct tw_osli_req_context *req)
1623 struct twa_softc *sc = req->ctlr;
1625 twa_printf(sc, "OSL details for request:\n");
1626 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1627 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1628 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1629 "next_req = %p, prev_req = %p, dma_map = %p\n",
1630 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1631 req->data, req->length, req->real_data, req->real_length,
1632 req->state, req->flags, req->error_code, req->orig_req,
1633 req->link.next, req->link.prev, req->dma_map);
1634 tw_cl_print_req_info(&(req->req_handle));
1640 * Function name: twa_reset_stats
1641 * Description: For being called from ddb.
1642 * Resets some OSL controller stats.
1646 * Return value: None
1649 twa_reset_stats(TW_VOID)
1651 struct twa_softc *sc;
1654 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1655 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1656 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1657 tw_cl_reset_stats(&sc->ctlr_handle);
1661 #endif /* TW_OSL_DEBUG */