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, d_thread_t *proc)
91 TW_INT32 unit = minor(dev);
92 struct twa_softc *sc = devclass_get_softc(twa_devclass, unit);
94 tw_osli_dbg_dprintf(5, sc, "entered");
95 sc->state |= TW_OSLI_CTLR_STATE_OPEN;
102 * Function name: twa_close
103 * Description: Called when the controller is closed.
104 * Simply marks the controller as not open.
106 * Input: dev -- control device corresponding to the ctlr
107 * flags -- mode of corresponding open
108 * fmt -- device type (character/block etc.)
109 * proc -- current process
111 * Return value: 0 -- success
115 twa_close(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, d_thread_t *proc)
117 TW_INT32 unit = minor(dev);
118 struct twa_softc *sc = devclass_get_softc(twa_devclass, unit);
120 tw_osli_dbg_dprintf(5, sc, "entered");
121 sc->state &= ~TW_OSLI_CTLR_STATE_OPEN;
128 * Function name: twa_ioctl
129 * Description: Called when an ioctl is posted to the controller.
130 * Handles any OS Layer specific cmds, passes the rest
131 * on to the Common Layer.
133 * Input: dev -- control device corresponding to the ctlr
135 * buf -- ptr to buffer in kernel memory, which is
136 * a copy of the input buffer in user-space
137 * flags -- mode of corresponding open
138 * proc -- current process
139 * Output: buf -- ptr to buffer in kernel memory, which will
140 * be copied to the output buffer in user-space
141 * Return value: 0 -- success
145 twa_ioctl(struct cdev *dev, u_long cmd, caddr_t buf, TW_INT32 flags, d_thread_t *proc)
147 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
150 tw_osli_dbg_dprintf(5, sc, "entered");
153 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
154 tw_osli_dbg_dprintf(6, sc, "ioctl: fw_passthru");
155 error = tw_osli_fw_passthru(sc, (TW_INT8 *)buf);
158 case TW_OSL_IOCTL_SCAN_BUS:
159 /* Request CAM for a bus scan. */
160 tw_osli_dbg_dprintf(6, sc, "ioctl: scan bus");
161 error = tw_osli_request_bus_scan(sc);
165 tw_osli_dbg_dprintf(6, sc, "ioctl: 0x%lx", cmd);
166 error = tw_cl_ioctl(&sc->ctlr_handle, cmd, buf);
174 static TW_INT32 twa_probe(device_t dev);
175 static TW_INT32 twa_attach(device_t dev);
176 static TW_INT32 twa_detach(device_t dev);
177 static TW_INT32 twa_shutdown(device_t dev);
178 static TW_VOID twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op);
179 #ifdef TW_OSLI_DEFERRED_INTR_USED
180 static int twa_pci_intr_fast(TW_VOID *arg);
181 static TW_VOID twa_deferred_intr(TW_VOID *context, TW_INT32 pending);
183 static TW_VOID twa_pci_intr(TW_VOID *arg);
184 #endif /* TW_OSLI_DEFERRED_INTR_USED */
186 static TW_INT32 tw_osli_alloc_mem(struct twa_softc *sc);
187 static TW_VOID tw_osli_free_resources(struct twa_softc *sc);
189 static TW_VOID twa_map_load_data_callback(TW_VOID *arg,
190 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
191 static TW_VOID twa_map_load_callback(TW_VOID *arg,
192 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
195 static device_method_t twa_methods[] = {
196 /* Device interface */
197 DEVMETHOD(device_probe, twa_probe),
198 DEVMETHOD(device_attach, twa_attach),
199 DEVMETHOD(device_detach, twa_detach),
200 DEVMETHOD(device_shutdown, twa_shutdown),
202 DEVMETHOD(bus_print_child, bus_generic_print_child),
203 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
207 static driver_t twa_pci_driver = {
210 sizeof(struct twa_softc)
213 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
214 MODULE_DEPEND(twa, cam, 1, 1, 1);
215 MODULE_DEPEND(twa, pci, 1, 1, 1);
219 * Function name: twa_probe
220 * Description: Called at driver load time. Claims 9000 ctlrs.
222 * Input: dev -- bus device corresponding to the ctlr
224 * Return value: <= 0 -- success
228 twa_probe(device_t dev)
230 static TW_UINT8 first_ctlr = 1;
232 tw_osli_dbg_printf(3, "entered");
234 if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
235 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
236 /* Print the driver version only once. */
238 printf("3ware device driver for 9000 series storage "
239 "controllers, version: %s\n",
240 TW_OSL_DRIVER_VERSION_STRING);
251 * Function name: twa_attach
252 * Description: Allocates pci resources; updates sc; adds a node to the
253 * sysctl tree to expose the driver version; makes calls
254 * (to the Common Layer) to initialize ctlr, and to
257 * Input: dev -- bus device corresponding to the ctlr
259 * Return value: 0 -- success
263 twa_attach(device_t dev)
265 struct twa_softc *sc = device_get_softc(dev);
268 TW_INT32 bar0_offset;
272 tw_osli_dbg_dprintf(3, sc, "entered");
274 sc->ctlr_handle.osl_ctlr_ctxt = sc;
276 /* Initialize the softc structure. */
278 sc->device_id = pci_get_device(dev);
280 /* Initialize the mutexes right here. */
281 sc->io_lock = &(sc->io_lock_handle);
282 mtx_init(sc->io_lock, "tw_osl_io_lock", NULL, MTX_SPIN);
283 sc->q_lock = &(sc->q_lock_handle);
284 mtx_init(sc->q_lock, "tw_osl_q_lock", NULL, MTX_SPIN);
285 sc->sim_lock = &(sc->sim_lock_handle);
286 mtx_init(sc->sim_lock, "tw_osl_sim_lock", NULL, MTX_DEF | MTX_RECURSE);
288 sysctl_ctx_init(&sc->sysctl_ctxt);
289 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
290 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
291 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
292 if (sc->sysctl_tree == NULL) {
293 tw_osli_printf(sc, "error = %d",
294 TW_CL_SEVERITY_ERROR_STRING,
295 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
297 "Cannot add sysctl tree node",
301 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
302 OID_AUTO, "driver_version", CTLFLAG_RD,
303 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
305 /* Make sure we are going to be able to talk to this board. */
306 command = pci_read_config(dev, PCIR_COMMAND, 2);
307 if ((command & PCIM_CMD_PORTEN) == 0) {
308 tw_osli_printf(sc, "error = %d",
309 TW_CL_SEVERITY_ERROR_STRING,
310 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
312 "Register window not available",
314 tw_osli_free_resources(sc);
318 /* Force the busmaster enable bit on, in case the BIOS forgot. */
319 command |= PCIM_CMD_BUSMASTEREN;
320 pci_write_config(dev, PCIR_COMMAND, command, 2);
322 /* Allocate the PCI register window. */
323 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
324 &bar_num, &bar0_offset, &bar_size))) {
325 tw_osli_printf(sc, "error = %d",
326 TW_CL_SEVERITY_ERROR_STRING,
327 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
329 "Can't get PCI BAR info",
331 tw_osli_free_resources(sc);
334 sc->reg_res_id = PCIR_BARS + bar0_offset;
335 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
336 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
338 tw_osli_printf(sc, "error = %d",
339 TW_CL_SEVERITY_ERROR_STRING,
340 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
342 "Can't allocate register window",
344 tw_osli_free_resources(sc);
347 sc->bus_tag = rman_get_bustag(sc->reg_res);
348 sc->bus_handle = rman_get_bushandle(sc->reg_res);
350 /* Allocate and register our interrupt. */
352 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
353 &(sc->irq_res_id), 0, ~0, 1,
354 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
355 tw_osli_printf(sc, "error = %d",
356 TW_CL_SEVERITY_ERROR_STRING,
357 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
359 "Can't allocate interrupt",
361 tw_osli_free_resources(sc);
364 if ((error = bus_setup_intr(sc->bus_dev, sc->irq_res,
365 #ifdef TW_OSLI_DEFERRED_INTR_USED
366 INTR_TYPE_CAM | INTR_FAST,
367 twa_pci_intr_fast, NULL,
369 INTR_TYPE_CAM | INTR_MPSAFE,
372 sc, &sc->intr_handle))) {
373 tw_osli_printf(sc, "error = %d",
374 TW_CL_SEVERITY_ERROR_STRING,
375 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
377 "Can't set up interrupt",
379 tw_osli_free_resources(sc);
383 #ifdef TW_OSLI_DEFERRED_INTR_USED
384 TASK_INIT(&sc->deferred_intr_callback, 0, twa_deferred_intr, sc);
385 #endif /* TW_OSLI_DEFERRED_INTR_USED */
387 if ((error = tw_osli_alloc_mem(sc))) {
388 tw_osli_printf(sc, "error = %d",
389 TW_CL_SEVERITY_ERROR_STRING,
390 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
392 "Memory allocation failure",
394 tw_osli_free_resources(sc);
398 /* Initialize the Common Layer for this controller. */
399 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
400 TW_OSLI_MAX_NUM_IOS, TW_OSLI_MAX_NUM_AENS,
401 sc->non_dma_mem, sc->dma_mem,
404 tw_osli_printf(sc, "error = %d",
405 TW_CL_SEVERITY_ERROR_STRING,
406 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
408 "Failed to initialize Common Layer/controller",
410 tw_osli_free_resources(sc);
414 /* Create the control device. */
415 sc->ctrl_dev = make_dev(&twa_cdevsw, device_get_unit(sc->bus_dev),
416 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
417 "twa%d", device_get_unit(sc->bus_dev));
418 sc->ctrl_dev->si_drv1 = sc;
420 if ((error = tw_osli_cam_attach(sc))) {
421 tw_osli_free_resources(sc);
422 tw_osli_printf(sc, "error = %d",
423 TW_CL_SEVERITY_ERROR_STRING,
424 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
426 "Failed to initialize CAM",
437 * Function name: tw_osli_alloc_mem
438 * Description: Allocates memory needed both by CL and OSL.
440 * Input: sc -- OSL internal controller context
442 * Return value: 0 -- success
446 tw_osli_alloc_mem(struct twa_softc *sc)
448 struct tw_osli_req_context *req;
449 TW_UINT32 max_sg_elements;
450 TW_UINT32 non_dma_mem_size;
451 TW_UINT32 dma_mem_size;
455 tw_osli_dbg_dprintf(3, sc, "entered");
457 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
458 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
459 #ifdef TW_OSLI_DEFERRED_INTR_USED
460 sc->flags |= TW_CL_DEFERRED_INTR_USED;
461 #endif /* TW_OSLI_DEFERRED_INTR_USED */
463 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
464 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
466 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
467 sc->device_id, TW_OSLI_MAX_NUM_IOS, TW_OSLI_MAX_NUM_AENS,
468 &(sc->alignment), &(sc->sg_size_factor),
469 &non_dma_mem_size, &dma_mem_size
471 tw_osli_printf(sc, "error = %d",
472 TW_CL_SEVERITY_ERROR_STRING,
473 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
475 "Can't get Common Layer's memory requirements",
480 if ((sc->non_dma_mem = malloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
481 M_WAITOK)) == NULL) {
482 tw_osli_printf(sc, "error = %d",
483 TW_CL_SEVERITY_ERROR_STRING,
484 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
486 "Can't allocate non-dma memory",
491 /* Create the parent dma tag. */
492 if (bus_dma_tag_create(NULL, /* parent */
493 sc->alignment, /* alignment */
494 TW_OSLI_DMA_BOUNDARY, /* boundary */
495 BUS_SPACE_MAXADDR, /* lowaddr */
496 BUS_SPACE_MAXADDR, /* highaddr */
497 NULL, NULL, /* filter, filterarg */
498 TW_CL_MAX_IO_SIZE, /* maxsize */
499 max_sg_elements, /* nsegments */
500 TW_CL_MAX_IO_SIZE, /* maxsegsize */
503 NULL, /* lockfuncarg */
504 &sc->parent_tag /* tag */)) {
505 tw_osli_printf(sc, "error = %d",
506 TW_CL_SEVERITY_ERROR_STRING,
507 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
509 "Can't allocate parent DMA tag",
514 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
515 if (bus_dma_tag_create(sc->parent_tag, /* parent */
516 sc->alignment, /* alignment */
518 BUS_SPACE_MAXADDR, /* lowaddr */
519 BUS_SPACE_MAXADDR, /* highaddr */
520 NULL, NULL, /* filter, filterarg */
521 dma_mem_size, /* maxsize */
523 BUS_SPACE_MAXSIZE, /* maxsegsize */
526 NULL, /* lockfuncarg */
527 &sc->cmd_tag /* tag */)) {
528 tw_osli_printf(sc, "error = %d",
529 TW_CL_SEVERITY_ERROR_STRING,
530 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
532 "Can't allocate DMA tag for Common Layer's "
538 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
539 BUS_DMA_NOWAIT, &sc->cmd_map)) {
540 /* Try a second time. */
541 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
542 BUS_DMA_NOWAIT, &sc->cmd_map)) {
543 tw_osli_printf(sc, "error = %d",
544 TW_CL_SEVERITY_ERROR_STRING,
545 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
547 "Can't allocate DMA'able memory for the"
554 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
555 dma_mem_size, twa_map_load_callback,
556 &sc->dma_mem_phys, 0);
559 * Create a dma tag for data buffers; size will be the maximum
560 * possible I/O size (128kB).
562 if (bus_dma_tag_create(sc->parent_tag, /* parent */
563 sc->alignment, /* alignment */
565 BUS_SPACE_MAXADDR, /* lowaddr */
566 BUS_SPACE_MAXADDR, /* highaddr */
567 NULL, NULL, /* filter, filterarg */
568 TW_CL_MAX_IO_SIZE, /* maxsize */
569 max_sg_elements, /* nsegments */
570 TW_CL_MAX_IO_SIZE, /* maxsegsize */
571 BUS_DMA_ALLOCNOW, /* flags */
572 twa_busdma_lock, /* lockfunc */
573 sc->io_lock, /* lockfuncarg */
574 &sc->dma_tag /* tag */)) {
575 tw_osli_printf(sc, "error = %d",
576 TW_CL_SEVERITY_ERROR_STRING,
577 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
579 "Can't allocate DMA tag for data buffers",
585 * Create a dma tag for ioctl data buffers; size will be the maximum
586 * possible I/O size (128kB).
588 if (bus_dma_tag_create(sc->parent_tag, /* parent */
589 sc->alignment, /* alignment */
591 BUS_SPACE_MAXADDR, /* lowaddr */
592 BUS_SPACE_MAXADDR, /* highaddr */
593 NULL, NULL, /* filter, filterarg */
594 TW_CL_MAX_IO_SIZE, /* maxsize */
595 max_sg_elements, /* nsegments */
596 TW_CL_MAX_IO_SIZE, /* maxsegsize */
597 BUS_DMA_ALLOCNOW, /* flags */
598 twa_busdma_lock, /* lockfunc */
599 sc->io_lock, /* lockfuncarg */
600 &sc->ioctl_tag /* tag */)) {
601 tw_osli_printf(sc, "error = %d",
602 TW_CL_SEVERITY_ERROR_STRING,
603 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
605 "Can't allocate DMA tag for ioctl data buffers",
610 /* Create just one map for all ioctl request data buffers. */
611 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
612 tw_osli_printf(sc, "error = %d",
613 TW_CL_SEVERITY_ERROR_STRING,
614 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
616 "Can't create ioctl map",
622 /* Initialize request queues. */
623 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
624 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
626 if ((sc->req_ctxt_buf = (struct tw_osli_req_context *)
627 malloc((sizeof(struct tw_osli_req_context) *
628 TW_OSLI_MAX_NUM_IOS),
629 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
630 tw_osli_printf(sc, "error = %d",
631 TW_CL_SEVERITY_ERROR_STRING,
632 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
634 "Failed to allocate request packets",
638 bzero(sc->req_ctxt_buf,
639 sizeof(struct tw_osli_req_context) * TW_OSLI_MAX_NUM_IOS);
641 for (i = 0; i < TW_OSLI_MAX_NUM_IOS; i++) {
642 req = &(sc->req_ctxt_buf[i]);
644 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
645 tw_osli_printf(sc, "request # = %d, error = %d",
646 TW_CL_SEVERITY_ERROR_STRING,
647 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
649 "Can't create dma map",
654 /* Insert request into the free queue. */
655 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
664 * Function name: tw_osli_free_resources
665 * Description: Performs clean-up at the time of going down.
667 * Input: sc -- ptr to OSL internal ctlr context
672 tw_osli_free_resources(struct twa_softc *sc)
674 struct tw_osli_req_context *req;
677 tw_osli_dbg_dprintf(3, sc, "entered");
679 /* Detach from CAM */
680 tw_osli_cam_detach(sc);
682 if (sc->req_ctxt_buf)
683 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
685 if ((error = bus_dmamap_destroy(sc->dma_tag,
687 tw_osli_dbg_dprintf(1, sc,
688 "dmamap_destroy(dma) returned %d",
691 if ((sc->ioctl_tag) && (sc->ioctl_map))
692 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
693 tw_osli_dbg_dprintf(1, sc,
694 "dmamap_destroy(ioctl) returned %d", error);
696 /* Free all memory allocated so far. */
697 if (sc->req_ctxt_buf)
698 free(sc->req_ctxt_buf, TW_OSLI_MALLOC_CLASS);
701 free(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
704 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
705 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
709 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
710 tw_osli_dbg_dprintf(1, sc,
711 "dma_tag_destroy(cmd) returned %d", error);
714 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
715 tw_osli_dbg_dprintf(1, sc,
716 "dma_tag_destroy(dma) returned %d", error);
719 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
720 tw_osli_dbg_dprintf(1, sc,
721 "dma_tag_destroy(ioctl) returned %d", error);
724 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
725 tw_osli_dbg_dprintf(1, sc,
726 "dma_tag_destroy(parent) returned %d", error);
729 /* Disconnect the interrupt handler. */
731 if ((error = bus_teardown_intr(sc->bus_dev,
732 sc->irq_res, sc->intr_handle)))
733 tw_osli_dbg_dprintf(1, sc,
734 "teardown_intr returned %d", error);
736 if (sc->irq_res != NULL)
737 if ((error = bus_release_resource(sc->bus_dev,
738 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
739 tw_osli_dbg_dprintf(1, sc,
740 "release_resource(irq) returned %d", error);
743 /* Release the register window mapping. */
744 if (sc->reg_res != NULL)
745 if ((error = bus_release_resource(sc->bus_dev,
746 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
747 tw_osli_dbg_dprintf(1, sc,
748 "release_resource(io) returned %d", error);
751 /* Destroy the control device. */
752 if (sc->ctrl_dev != (struct cdev *)NULL)
753 destroy_dev(sc->ctrl_dev);
755 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
756 tw_osli_dbg_dprintf(1, sc,
757 "sysctl_ctx_free returned %d", error);
764 * Function name: twa_detach
765 * Description: Called when the controller is being detached from
768 * Input: dev -- bus device corresponding to the ctlr
770 * Return value: 0 -- success
774 twa_detach(device_t dev)
776 struct twa_softc *sc = device_get_softc(dev);
779 tw_osli_dbg_dprintf(3, sc, "entered");
782 if (sc->state & TW_OSLI_CTLR_STATE_OPEN) {
783 tw_osli_printf(sc, "error = %d",
784 TW_CL_SEVERITY_ERROR_STRING,
785 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
792 /* Shut the controller down. */
793 if ((error = twa_shutdown(dev)))
796 /* Free all resources associated with this controller. */
797 tw_osli_free_resources(sc);
807 * Function name: twa_shutdown
808 * Description: Called at unload/shutdown time. Lets the controller
809 * know that we are going down.
811 * Input: dev -- bus device corresponding to the ctlr
813 * Return value: 0 -- success
817 twa_shutdown(device_t dev)
819 struct twa_softc *sc = device_get_softc(dev);
822 tw_osli_dbg_dprintf(3, sc, "entered");
824 /* Disconnect from the controller. */
825 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
826 tw_osli_printf(sc, "error = %d",
827 TW_CL_SEVERITY_ERROR_STRING,
828 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
830 "Failed to shutdown Common Layer/controller",
839 * Function name: twa_busdma_lock
840 * Description: Function to provide synchronization during busdma_swi.
842 * Input: lock_arg -- lock mutex sent as argument
843 * op -- operation (lock/unlock) expected of the function
848 twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op)
852 lock = (struct mtx *)lock_arg;
859 mtx_unlock_spin(lock);
863 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
868 #ifdef TW_OSLI_DEFERRED_INTR_USED
870 * Function name: twa_pci_intr_fast
871 * Description: Interrupt handler. Wrapper for twa_interrupt.
873 * Input: arg -- ptr to OSL internal ctlr context
874 * Output: FILTER_HANDLED or FILTER_STRAY
878 twa_pci_intr_fast(TW_VOID *arg)
880 struct twa_softc *sc = (struct twa_softc *)arg;
882 tw_osli_dbg_dprintf(10, sc, "entered");
883 if (tw_cl_interrupt(&(sc->ctlr_handle))) {
884 taskqueue_enqueue_fast(taskqueue_fast,
885 &(sc->deferred_intr_callback));
886 return(FILTER_HANDLED);
888 return(FILTER_STRAY);
892 * Function name: twa_pci_intr
893 * Description: Interrupt handler. Wrapper for twa_interrupt.
895 * Input: arg -- ptr to OSL internal ctlr context
900 twa_pci_intr(TW_VOID *arg)
902 struct twa_softc *sc = (struct twa_softc *)arg;
904 tw_osli_dbg_dprintf(10, sc, "entered");
905 if (tw_cl_interrupt(&(sc->ctlr_handle)))
906 tw_cl_deferred_interrupt(&(sc->ctlr_handle));
910 #ifdef TW_OSLI_DEFERRED_INTR_USED
913 * Function name: twa_deferred_intr
914 * Description: Deferred interrupt handler.
916 * Input: context -- ptr to OSL internal ctlr context
917 * pending -- not used
922 twa_deferred_intr(TW_VOID *context, TW_INT32 pending)
924 struct twa_softc *sc = (struct twa_softc *)context;
926 tw_osli_dbg_dprintf(10, sc, "entered");
928 tw_cl_deferred_interrupt(&(sc->ctlr_handle));
931 #endif /* TW_OSLI_DEFERRED_INTR_USED */
936 * Function name: tw_osli_fw_passthru
937 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
939 * Input: sc -- ptr to OSL internal ctlr context
940 * buf -- ptr to ioctl pkt understood by CL
942 * Return value: 0 -- success
946 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
948 struct tw_osli_req_context *req;
949 struct tw_osli_ioctl_no_data_buf *user_buf =
950 (struct tw_osli_ioctl_no_data_buf *)buf;
952 TW_UINT32 timeout = 60;
953 TW_UINT32 data_buf_size_adjusted;
954 struct tw_cl_req_packet *req_pkt;
955 struct tw_cl_passthru_req_packet *pt_req;
958 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
960 if ((req = tw_osli_get_request(sc)) == NULL)
963 req->req_handle.osl_req_ctxt = req;
965 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
967 req_pkt = &(req->req_pkt);
969 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
970 /* Let the Common Layer retry the request on cmd queue full. */
971 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
973 pt_req = &(req_pkt->gen_req_pkt.pt_req);
975 * Make sure that the data buffer sent to firmware is a
976 * 512 byte multiple in size.
978 data_buf_size_adjusted =
979 (user_buf->driver_pkt.buffer_length +
980 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
981 if ((req->length = data_buf_size_adjusted)) {
982 if ((req->data = malloc(data_buf_size_adjusted,
983 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
985 tw_osli_printf(sc, "error = %d",
986 TW_CL_SEVERITY_ERROR_STRING,
987 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
989 "Could not alloc mem for "
990 "fw_passthru data_buf",
992 goto fw_passthru_err;
994 /* Copy the payload. */
995 if ((error = copyin((TW_VOID *)(user_buf->pdata),
997 user_buf->driver_pkt.buffer_length)) != 0) {
998 tw_osli_printf(sc, "error = %d",
999 TW_CL_SEVERITY_ERROR_STRING,
1000 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1002 "Could not copyin fw_passthru data_buf",
1004 goto fw_passthru_err;
1006 pt_req->sgl_entries = 1; /* will be updated during mapping */
1007 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
1008 TW_OSLI_REQ_FLAGS_DATA_OUT);
1010 pt_req->sgl_entries = 0; /* no payload */
1012 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
1013 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
1015 if ((error = tw_osli_map_request(req)))
1016 goto fw_passthru_err;
1018 end_time = tw_osl_get_local_time() + timeout;
1019 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
1020 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
1022 error = tsleep(req, PRIBIO, "twa_passthru", timeout * hz);
1024 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
1026 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1029 if (((error = req->error_code)) ||
1030 ((error = (req->state !=
1031 TW_OSLI_REQ_STATE_COMPLETE))) ||
1032 ((error = req_pkt->status)))
1033 goto fw_passthru_err;
1037 if (req_pkt->status) {
1038 error = req_pkt->status;
1039 goto fw_passthru_err;
1042 if (error == EWOULDBLOCK) {
1044 tw_osli_printf(sc, "request = %p",
1045 TW_CL_SEVERITY_ERROR_STRING,
1046 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1048 "Passthru request timed out!",
1051 * Should I check here if the timeout happened
1052 * because of yet another reset, and not do a
1055 tw_cl_reset_ctlr(&sc->ctlr_handle);
1057 * Don't touch req after a reset. It (and any
1058 * associated data) will already have been
1059 * unmapped by the callback.
1061 user_buf->driver_pkt.os_status = error;
1063 goto fw_passthru_err;
1066 * Either the request got completed, or we were woken up by a
1067 * signal. Calculate the new timeout, in case it was the latter.
1069 timeout = (end_time - tw_osl_get_local_time());
1072 /* If there was a payload, copy it back. */
1073 if ((!error) && (req->length))
1074 if ((error = copyout(req->data, user_buf->pdata,
1075 user_buf->driver_pkt.buffer_length)))
1076 tw_osli_printf(sc, "error = %d",
1077 TW_CL_SEVERITY_ERROR_STRING,
1078 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1080 "Could not copyout fw_passthru data_buf",
1085 * Print the failure message. For some reason, on certain OS versions,
1086 * printing this error message during reset hangs the display (although
1087 * the rest of the system is running fine. So, don't print it if the
1088 * failure was due to a reset.
1090 if ((error) && (error != TW_CL_ERR_REQ_BUS_RESET))
1091 tw_osli_printf(sc, "error = %d",
1092 TW_CL_SEVERITY_ERROR_STRING,
1093 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1095 "Firmware passthru failed!",
1098 user_buf->driver_pkt.os_status = error;
1099 /* Free resources. */
1101 free(req->data, TW_OSLI_MALLOC_CLASS);
1102 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1109 * Function name: tw_osl_complete_passthru
1110 * Description: Called to complete passthru requests.
1112 * Input: req_handle -- ptr to request handle
1114 * Return value: None
1117 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1119 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1120 struct twa_softc *sc = req->ctlr;
1122 tw_osli_dbg_dprintf(5, sc, "entered");
1124 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1125 tw_osli_printf(sc, "request = %p, status = %d",
1126 TW_CL_SEVERITY_ERROR_STRING,
1127 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1129 "Unposted command completed!!",
1134 * Remove request from the busy queue. Just mark it complete.
1135 * There's no need to move it into the complete queue as we are
1136 * going to be done with it right now.
1138 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1139 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1141 tw_osli_unmap_request(req);
1144 * Don't do a wake up if there was an error even before the request
1145 * was sent down to the Common Layer, and we hadn't gotten an
1146 * EINPROGRESS. The request originator will then be returned an
1147 * error, and he can do the clean-up.
1149 if ((req->error_code) &&
1150 (!(req->state & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1153 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1154 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1155 /* Wake up the sleeping command originator. */
1156 tw_osli_dbg_dprintf(5, sc,
1157 "Waking up originator of request %p", req);
1158 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1162 * If the request completed even before tsleep
1163 * was called, simply return.
1165 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1168 tw_osli_printf(sc, "request = %p",
1169 TW_CL_SEVERITY_ERROR_STRING,
1170 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1172 "Passthru callback called, "
1173 "and caller not sleeping",
1177 tw_osli_printf(sc, "request = %p",
1178 TW_CL_SEVERITY_ERROR_STRING,
1179 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1181 "Passthru callback called for non-passthru request",
1189 * Function name: tw_osli_get_request
1190 * Description: Gets a request pkt from the free queue.
1192 * Input: sc -- ptr to OSL internal ctlr context
1194 * Return value: ptr to request pkt -- success
1197 struct tw_osli_req_context *
1198 tw_osli_get_request(struct twa_softc *sc)
1200 struct tw_osli_req_context *req;
1202 tw_osli_dbg_dprintf(4, sc, "entered");
1204 /* Get a free request packet. */
1205 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1207 /* Initialize some fields to their defaults. */
1209 req->req_handle.osl_req_ctxt = NULL;
1210 req->req_handle.cl_req_ctxt = NULL;
1213 req->real_data = NULL;
1214 req->real_length = 0;
1215 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1217 req->error_code = 0;
1218 req->orig_req = NULL;
1220 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1229 * Function name: twa_map_load_data_callback
1230 * Description: Callback of bus_dmamap_load for the buffer associated
1231 * with data. Updates the cmd pkt (size/sgl_entries
1232 * fields, as applicable) to reflect the number of sg
1235 * Input: arg -- ptr to OSL internal request context
1236 * segs -- ptr to a list of segment descriptors
1237 * nsegments--# of segments
1238 * error -- 0 if no errors encountered before callback,
1239 * non-zero if errors were encountered
1241 * Return value: None
1244 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1245 TW_INT32 nsegments, TW_INT32 error)
1247 struct tw_osli_req_context *req =
1248 (struct tw_osli_req_context *)arg;
1249 struct twa_softc *sc = req->ctlr;
1250 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1252 tw_osli_dbg_dprintf(10, sc, "entered");
1254 /* Mark the request as currently being processed. */
1255 req->state = TW_OSLI_REQ_STATE_BUSY;
1256 /* Move the request into the busy queue. */
1257 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1259 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1260 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1261 tw_osli_allow_new_requests(sc, (TW_VOID *)(req->orig_req));
1263 if (error == EFBIG) {
1264 req->error_code = error;
1268 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1269 struct tw_cl_passthru_req_packet *pt_req;
1271 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1272 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1273 BUS_DMASYNC_PREREAD);
1275 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1277 * If we're using an alignment buffer, and we're
1278 * writing data, copy the real data out.
1280 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1281 bcopy(req->real_data, req->data, req->real_length);
1282 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1283 BUS_DMASYNC_PREWRITE);
1286 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1287 pt_req->sg_list = (TW_UINT8 *)segs;
1288 pt_req->sgl_entries += (nsegments - 1);
1289 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1290 &(req->req_handle));
1292 struct tw_cl_scsi_req_packet *scsi_req;
1294 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1295 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1296 BUS_DMASYNC_PREREAD);
1298 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1300 * If we're using an alignment buffer, and we're
1301 * writing data, copy the real data out.
1303 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1304 bcopy(req->real_data, req->data, req->real_length);
1305 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1306 BUS_DMASYNC_PREWRITE);
1309 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1310 scsi_req->sg_list = (TW_UINT8 *)segs;
1311 scsi_req->sgl_entries += (nsegments - 1);
1312 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1313 &(req->req_handle));
1318 req->error_code = error;
1319 req_pkt->tw_osl_callback(&(req->req_handle));
1321 * If the caller had been returned EINPROGRESS, and he has
1322 * registered a callback for handling completion, the callback
1323 * will never get called because we were unable to submit the
1324 * request. So, free up the request right here.
1326 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1327 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1334 * Function name: twa_map_load_callback
1335 * Description: Callback of bus_dmamap_load for the buffer associated
1338 * Input: arg -- ptr to variable to hold phys addr
1339 * segs -- ptr to a list of segment descriptors
1340 * nsegments--# of segments
1341 * error -- 0 if no errors encountered before callback,
1342 * non-zero if errors were encountered
1344 * Return value: None
1347 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1348 TW_INT32 nsegments, TW_INT32 error)
1350 *((TW_UINT64 *)arg) = segs[0].ds_addr;
1356 * Function name: tw_osli_map_request
1357 * Description: Maps a cmd pkt and data associated with it, into
1360 * Input: req -- ptr to request pkt
1362 * Return value: 0 -- success
1363 * non-zero-- failure
1366 tw_osli_map_request(struct tw_osli_req_context *req)
1368 struct twa_softc *sc = req->ctlr;
1371 tw_osli_dbg_dprintf(10, sc, "entered");
1373 /* If the command involves data, map that too. */
1374 if (req->data != NULL) {
1376 * It's sufficient for the data pointer to be 4-byte aligned
1377 * to work with 9000. However, if 4-byte aligned addresses
1378 * are passed to bus_dmamap_load, we can get back sg elements
1379 * that are not 512-byte multiples in size. So, we will let
1380 * only those buffers that are 512-byte aligned to pass
1381 * through, and bounce the rest, so as to make sure that we
1382 * always get back sg elements that are 512-byte multiples
1385 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1386 (req->length % sc->sg_size_factor)) {
1387 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1388 /* Save original data pointer and length. */
1389 req->real_data = req->data;
1390 req->real_length = req->length;
1391 req->length = (req->length +
1392 (sc->sg_size_factor - 1)) &
1393 ~(sc->sg_size_factor - 1);
1394 req->data = malloc(req->length, TW_OSLI_MALLOC_CLASS,
1396 if (req->data == NULL) {
1397 tw_osli_printf(sc, "error = %d",
1398 TW_CL_SEVERITY_ERROR_STRING,
1399 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1401 "Failed to allocate memory "
1402 "for bounce buffer",
1404 /* Restore original data pointer and length. */
1405 req->data = req->real_data;
1406 req->length = req->real_length;
1412 * Map the data buffer into bus space and build the SG list.
1414 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1415 /* Lock against multiple simultaneous ioctl calls. */
1416 mtx_lock_spin(sc->io_lock);
1417 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1418 req->data, req->length,
1419 twa_map_load_data_callback, req,
1421 mtx_unlock_spin(sc->io_lock);
1424 * There's only one CAM I/O thread running at a time.
1425 * So, there's no need to hold the io_lock.
1427 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1428 req->data, req->length,
1429 twa_map_load_data_callback, req,
1434 error = req->error_code;
1436 if (error == EINPROGRESS) {
1438 * Specifying sc->io_lock as the lockfuncarg
1439 * in ...tag_create should protect the access
1440 * of ...FLAGS_MAPPED from the callback.
1442 mtx_lock_spin(sc->io_lock);
1443 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED)) {
1445 TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1446 tw_osli_disallow_new_requests(sc);
1448 mtx_unlock_spin(sc->io_lock);
1451 /* Free alignment buffer if it was used. */
1453 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1454 free(req->data, TW_OSLI_MALLOC_CLASS);
1456 * Restore original data pointer
1459 req->data = req->real_data;
1460 req->length = req->real_length;
1466 /* Mark the request as currently being processed. */
1467 req->state = TW_OSLI_REQ_STATE_BUSY;
1468 /* Move the request into the busy queue. */
1469 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1470 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1471 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1472 &(req->req_pkt), &(req->req_handle));
1474 error = tw_cl_start_io(&sc->ctlr_handle,
1475 &(req->req_pkt), &(req->req_handle));
1477 req->error_code = error;
1478 req->req_pkt.tw_osl_callback(&(req->req_handle));
1487 * Function name: tw_osli_unmap_request
1488 * Description: Undoes the mapping done by tw_osli_map_request.
1490 * Input: req -- ptr to request pkt
1492 * Return value: None
1495 tw_osli_unmap_request(struct tw_osli_req_context *req)
1497 struct twa_softc *sc = req->ctlr;
1499 tw_osli_dbg_dprintf(10, sc, "entered");
1501 /* If the command involved data, unmap that too. */
1502 if (req->data != NULL) {
1503 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1504 /* Lock against multiple simultaneous ioctl calls. */
1505 mtx_lock_spin(sc->io_lock);
1507 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1508 bus_dmamap_sync(sc->ioctl_tag,
1509 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1512 * If we are using a bounce buffer, and we are
1513 * reading data, copy the real data in.
1515 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1516 bcopy(req->data, req->real_data,
1520 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1521 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1522 BUS_DMASYNC_POSTWRITE);
1524 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1526 mtx_unlock_spin(sc->io_lock);
1528 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1529 bus_dmamap_sync(sc->dma_tag,
1530 req->dma_map, BUS_DMASYNC_POSTREAD);
1533 * If we are using a bounce buffer, and we are
1534 * reading data, copy the real data in.
1536 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1537 bcopy(req->data, req->real_data,
1540 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1541 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1542 BUS_DMASYNC_POSTWRITE);
1544 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1548 /* Free alignment buffer if it was used. */
1549 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1550 free(req->data, TW_OSLI_MALLOC_CLASS);
1551 /* Restore original data pointer and length. */
1552 req->data = req->real_data;
1553 req->length = req->real_length;
1561 TW_VOID twa_report_stats(TW_VOID);
1562 TW_VOID twa_reset_stats(TW_VOID);
1563 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1564 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1568 * Function name: twa_report_stats
1569 * Description: For being called from ddb. Calls functions that print
1570 * OSL and CL internal stats for the controller.
1574 * Return value: None
1577 twa_report_stats(TW_VOID)
1579 struct twa_softc *sc;
1582 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1583 tw_osli_print_ctlr_stats(sc);
1584 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1591 * Function name: tw_osli_print_ctlr_stats
1592 * Description: For being called from ddb. Prints OSL controller stats
1594 * Input: sc -- ptr to OSL internal controller context
1596 * Return value: None
1599 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1601 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1602 twa_printf(sc, "OSLq type current max\n");
1603 twa_printf(sc, "free %04d %04d\n",
1604 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1605 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1606 twa_printf(sc, "busy %04d %04d\n",
1607 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1608 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1614 * Function name: twa_print_req_info
1615 * Description: For being called from ddb. Calls functions that print
1616 * OSL and CL internal details for the request.
1618 * Input: req -- ptr to OSL internal request context
1620 * Return value: None
1623 twa_print_req_info(struct tw_osli_req_context *req)
1625 struct twa_softc *sc = req->ctlr;
1627 twa_printf(sc, "OSL details for request:\n");
1628 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1629 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1630 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1631 "next_req = %p, prev_req = %p, dma_map = %p\n",
1632 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1633 req->data, req->length, req->real_data, req->real_length,
1634 req->state, req->flags, req->error_code, req->orig_req,
1635 req->link.next, req->link.prev, req->dma_map);
1636 tw_cl_print_req_info(&(req->req_handle));
1642 * Function name: twa_reset_stats
1643 * Description: For being called from ddb.
1644 * Resets some OSL controller stats.
1648 * Return value: None
1651 twa_reset_stats(TW_VOID)
1653 struct twa_softc *sc;
1656 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1657 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1658 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1659 tw_cl_reset_stats(&sc->ctlr_handle);
1663 #endif /* TW_OSL_DEBUG */