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->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 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->open = TW_CL_FALSE;
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 static TW_VOID twa_pci_intr(TW_VOID *arg);
178 static TW_VOID twa_watchdog(TW_VOID *arg);
179 int twa_setup_intr(struct twa_softc *sc);
180 int twa_teardown_intr(struct twa_softc *sc);
182 static TW_INT32 tw_osli_alloc_mem(struct twa_softc *sc);
183 static TW_VOID tw_osli_free_resources(struct twa_softc *sc);
185 static TW_VOID twa_map_load_data_callback(TW_VOID *arg,
186 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
187 static TW_VOID twa_map_load_callback(TW_VOID *arg,
188 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
191 static device_method_t twa_methods[] = {
192 /* Device interface */
193 DEVMETHOD(device_probe, twa_probe),
194 DEVMETHOD(device_attach, twa_attach),
195 DEVMETHOD(device_detach, twa_detach),
196 DEVMETHOD(device_shutdown, twa_shutdown),
198 DEVMETHOD(bus_print_child, bus_generic_print_child),
199 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
203 static driver_t twa_pci_driver = {
206 sizeof(struct twa_softc)
209 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
210 MODULE_DEPEND(twa, cam, 1, 1, 1);
211 MODULE_DEPEND(twa, pci, 1, 1, 1);
215 * Function name: twa_probe
216 * Description: Called at driver load time. Claims 9000 ctlrs.
218 * Input: dev -- bus device corresponding to the ctlr
220 * Return value: <= 0 -- success
224 twa_probe(device_t dev)
226 static TW_UINT8 first_ctlr = 1;
228 tw_osli_dbg_printf(3, "entered");
230 if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
231 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
232 /* Print the driver version only once. */
234 printf("3ware device driver for 9000 series storage "
235 "controllers, version: %s\n",
236 TW_OSL_DRIVER_VERSION_STRING);
244 int twa_setup_intr(struct twa_softc *sc)
248 if (!(sc->intr_handle) && (sc->irq_res)) {
249 error = bus_setup_intr(sc->bus_dev, sc->irq_res,
250 INTR_TYPE_CAM | INTR_MPSAFE,
252 sc, &sc->intr_handle);
258 int twa_teardown_intr(struct twa_softc *sc)
262 if ((sc->intr_handle) && (sc->irq_res)) {
263 error = bus_teardown_intr(sc->bus_dev,
264 sc->irq_res, sc->intr_handle);
265 sc->intr_handle = NULL;
273 * Function name: twa_attach
274 * Description: Allocates pci resources; updates sc; adds a node to the
275 * sysctl tree to expose the driver version; makes calls
276 * (to the Common Layer) to initialize ctlr, and to
279 * Input: dev -- bus device corresponding to the ctlr
281 * Return value: 0 -- success
285 twa_attach(device_t dev)
287 struct twa_softc *sc = device_get_softc(dev);
290 TW_INT32 bar0_offset;
294 tw_osli_dbg_dprintf(3, sc, "entered");
296 sc->ctlr_handle.osl_ctlr_ctxt = sc;
298 /* Initialize the softc structure. */
300 sc->device_id = pci_get_device(dev);
302 /* Initialize the mutexes right here. */
303 sc->io_lock = &(sc->io_lock_handle);
304 mtx_init(sc->io_lock, "tw_osl_io_lock", NULL, MTX_SPIN);
305 sc->q_lock = &(sc->q_lock_handle);
306 mtx_init(sc->q_lock, "tw_osl_q_lock", NULL, MTX_SPIN);
307 sc->sim_lock = &(sc->sim_lock_handle);
308 mtx_init(sc->sim_lock, "tw_osl_sim_lock", NULL, MTX_DEF | MTX_RECURSE);
310 sysctl_ctx_init(&sc->sysctl_ctxt);
311 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
312 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
313 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
314 if (sc->sysctl_tree == NULL) {
315 tw_osli_printf(sc, "error = %d",
316 TW_CL_SEVERITY_ERROR_STRING,
317 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
319 "Cannot add sysctl tree node",
323 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
324 OID_AUTO, "driver_version", CTLFLAG_RD,
325 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
327 /* Make sure we are going to be able to talk to this board. */
328 command = pci_read_config(dev, PCIR_COMMAND, 2);
329 if ((command & PCIM_CMD_PORTEN) == 0) {
330 tw_osli_printf(sc, "error = %d",
331 TW_CL_SEVERITY_ERROR_STRING,
332 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
334 "Register window not available",
336 tw_osli_free_resources(sc);
340 /* Force the busmaster enable bit on, in case the BIOS forgot. */
341 command |= PCIM_CMD_BUSMASTEREN;
342 pci_write_config(dev, PCIR_COMMAND, command, 2);
344 /* Allocate the PCI register window. */
345 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
346 &bar_num, &bar0_offset, &bar_size))) {
347 tw_osli_printf(sc, "error = %d",
348 TW_CL_SEVERITY_ERROR_STRING,
349 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
351 "Can't get PCI BAR info",
353 tw_osli_free_resources(sc);
356 sc->reg_res_id = PCIR_BARS + bar0_offset;
357 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
358 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
360 tw_osli_printf(sc, "error = %d",
361 TW_CL_SEVERITY_ERROR_STRING,
362 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
364 "Can't allocate register window",
366 tw_osli_free_resources(sc);
369 sc->bus_tag = rman_get_bustag(sc->reg_res);
370 sc->bus_handle = rman_get_bushandle(sc->reg_res);
372 /* Allocate and register our interrupt. */
374 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
375 &(sc->irq_res_id), 0, ~0, 1,
376 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
377 tw_osli_printf(sc, "error = %d",
378 TW_CL_SEVERITY_ERROR_STRING,
379 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
381 "Can't allocate interrupt",
383 tw_osli_free_resources(sc);
386 if ((error = twa_setup_intr(sc))) {
387 tw_osli_printf(sc, "error = %d",
388 TW_CL_SEVERITY_ERROR_STRING,
389 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
391 "Can't set up interrupt",
393 tw_osli_free_resources(sc);
397 if ((error = tw_osli_alloc_mem(sc))) {
398 tw_osli_printf(sc, "error = %d",
399 TW_CL_SEVERITY_ERROR_STRING,
400 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
402 "Memory allocation failure",
404 tw_osli_free_resources(sc);
408 /* Initialize the Common Layer for this controller. */
409 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
410 TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
411 sc->non_dma_mem, sc->dma_mem,
414 tw_osli_printf(sc, "error = %d",
415 TW_CL_SEVERITY_ERROR_STRING,
416 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
418 "Failed to initialize Common Layer/controller",
420 tw_osli_free_resources(sc);
424 /* Create the control device. */
425 sc->ctrl_dev = make_dev(&twa_cdevsw, device_get_unit(sc->bus_dev),
426 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
427 "twa%d", device_get_unit(sc->bus_dev));
428 sc->ctrl_dev->si_drv1 = sc;
430 if ((error = tw_osli_cam_attach(sc))) {
431 tw_osli_free_resources(sc);
432 tw_osli_printf(sc, "error = %d",
433 TW_CL_SEVERITY_ERROR_STRING,
434 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
436 "Failed to initialize CAM",
441 sc->watchdog_index = 0;
442 callout_init(&(sc->watchdog_callout[0]), CALLOUT_MPSAFE);
443 callout_init(&(sc->watchdog_callout[1]), CALLOUT_MPSAFE);
444 callout_reset(&(sc->watchdog_callout[0]), 5*hz, twa_watchdog, &sc->ctlr_handle);
451 twa_watchdog(TW_VOID *arg)
453 struct tw_cl_ctlr_handle *ctlr_handle =
454 (struct tw_cl_ctlr_handle *)arg;
455 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
457 int i_need_a_reset = 0;
458 int driver_is_active = 0;
459 int my_watchdog_was_pending = 1234;
460 TW_UINT64 current_time;
461 struct tw_osli_req_context *my_req;
464 //==============================================================================
465 current_time = (TW_UINT64) (tw_osl_get_local_time());
467 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
468 my_req = &(sc->req_ctx_buf[i]);
470 if ((my_req->state == TW_OSLI_REQ_STATE_BUSY) &&
471 (my_req->deadline) &&
472 (my_req->deadline < current_time)) {
473 tw_cl_set_reset_needed(ctlr_handle);
475 device_printf((sc)->bus_dev, "Request %d timed out! d = %llu, c = %llu\n", i, my_req->deadline, current_time);
476 #else /* TW_OSL_DEBUG */
477 device_printf((sc)->bus_dev, "Request %d timed out!\n", i);
478 #endif /* TW_OSL_DEBUG */
482 //==============================================================================
484 i_need_a_reset = tw_cl_is_reset_needed(ctlr_handle);
486 i = (int) ((sc->watchdog_index++) & 1);
488 driver_is_active = tw_cl_is_active(ctlr_handle);
490 if (i_need_a_reset) {
492 device_printf((sc)->bus_dev, "Watchdog rescheduled in 70 seconds\n");
493 #endif /* TW_OSL_DEBUG */
494 my_watchdog_was_pending =
495 callout_reset(&(sc->watchdog_callout[i]), 70*hz, twa_watchdog, &sc->ctlr_handle);
496 tw_cl_reset_ctlr(ctlr_handle);
498 device_printf((sc)->bus_dev, "Watchdog reset completed!\n");
499 #endif /* TW_OSL_DEBUG */
500 } else if (driver_is_active) {
501 my_watchdog_was_pending =
502 callout_reset(&(sc->watchdog_callout[i]), 5*hz, twa_watchdog, &sc->ctlr_handle);
505 if (i_need_a_reset || my_watchdog_was_pending)
506 device_printf((sc)->bus_dev, "i_need_a_reset = %d, "
507 "driver_is_active = %d, my_watchdog_was_pending = %d\n",
508 i_need_a_reset, driver_is_active, my_watchdog_was_pending);
509 #endif /* TW_OSL_DEBUG */
514 * Function name: tw_osli_alloc_mem
515 * Description: Allocates memory needed both by CL and OSL.
517 * Input: sc -- OSL internal controller context
519 * Return value: 0 -- success
523 tw_osli_alloc_mem(struct twa_softc *sc)
525 struct tw_osli_req_context *req;
526 TW_UINT32 max_sg_elements;
527 TW_UINT32 non_dma_mem_size;
528 TW_UINT32 dma_mem_size;
532 tw_osli_dbg_dprintf(3, sc, "entered");
534 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
535 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
537 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
538 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
540 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
541 sc->device_id, TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
542 &(sc->alignment), &(sc->sg_size_factor),
543 &non_dma_mem_size, &dma_mem_size
545 tw_osli_printf(sc, "error = %d",
546 TW_CL_SEVERITY_ERROR_STRING,
547 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
549 "Can't get Common Layer's memory requirements",
554 if ((sc->non_dma_mem = malloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
555 M_WAITOK)) == NULL) {
556 tw_osli_printf(sc, "error = %d",
557 TW_CL_SEVERITY_ERROR_STRING,
558 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
560 "Can't allocate non-dma memory",
565 /* Create the parent dma tag. */
566 if (bus_dma_tag_create(NULL, /* parent */
567 sc->alignment, /* alignment */
568 TW_OSLI_DMA_BOUNDARY, /* boundary */
569 BUS_SPACE_MAXADDR, /* lowaddr */
570 BUS_SPACE_MAXADDR, /* highaddr */
571 NULL, NULL, /* filter, filterarg */
572 TW_CL_MAX_IO_SIZE, /* maxsize */
573 max_sg_elements, /* nsegments */
574 TW_CL_MAX_IO_SIZE, /* maxsegsize */
577 NULL, /* lockfuncarg */
578 &sc->parent_tag /* tag */)) {
579 tw_osli_printf(sc, "error = %d",
580 TW_CL_SEVERITY_ERROR_STRING,
581 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
583 "Can't allocate parent DMA tag",
588 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
589 if (bus_dma_tag_create(sc->parent_tag, /* parent */
590 sc->alignment, /* alignment */
592 BUS_SPACE_MAXADDR, /* lowaddr */
593 BUS_SPACE_MAXADDR, /* highaddr */
594 NULL, NULL, /* filter, filterarg */
595 dma_mem_size, /* maxsize */
597 BUS_SPACE_MAXSIZE, /* maxsegsize */
600 NULL, /* lockfuncarg */
601 &sc->cmd_tag /* tag */)) {
602 tw_osli_printf(sc, "error = %d",
603 TW_CL_SEVERITY_ERROR_STRING,
604 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
606 "Can't allocate DMA tag for Common Layer's "
612 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
613 BUS_DMA_NOWAIT, &sc->cmd_map)) {
614 /* Try a second time. */
615 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
616 BUS_DMA_NOWAIT, &sc->cmd_map)) {
617 tw_osli_printf(sc, "error = %d",
618 TW_CL_SEVERITY_ERROR_STRING,
619 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
621 "Can't allocate DMA'able memory for the"
628 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
629 dma_mem_size, twa_map_load_callback,
630 &sc->dma_mem_phys, 0);
633 * Create a dma tag for data buffers; size will be the maximum
634 * possible I/O size (128kB).
636 if (bus_dma_tag_create(sc->parent_tag, /* parent */
637 sc->alignment, /* alignment */
639 BUS_SPACE_MAXADDR, /* lowaddr */
640 BUS_SPACE_MAXADDR, /* highaddr */
641 NULL, NULL, /* filter, filterarg */
642 TW_CL_MAX_IO_SIZE, /* maxsize */
643 max_sg_elements, /* nsegments */
644 TW_CL_MAX_IO_SIZE, /* maxsegsize */
645 BUS_DMA_ALLOCNOW, /* flags */
646 twa_busdma_lock, /* lockfunc */
647 sc->io_lock, /* lockfuncarg */
648 &sc->dma_tag /* tag */)) {
649 tw_osli_printf(sc, "error = %d",
650 TW_CL_SEVERITY_ERROR_STRING,
651 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
653 "Can't allocate DMA tag for data buffers",
659 * Create a dma tag for ioctl data buffers; size will be the maximum
660 * possible I/O size (128kB).
662 if (bus_dma_tag_create(sc->parent_tag, /* parent */
663 sc->alignment, /* alignment */
665 BUS_SPACE_MAXADDR, /* lowaddr */
666 BUS_SPACE_MAXADDR, /* highaddr */
667 NULL, NULL, /* filter, filterarg */
668 TW_CL_MAX_IO_SIZE, /* maxsize */
669 max_sg_elements, /* nsegments */
670 TW_CL_MAX_IO_SIZE, /* maxsegsize */
671 BUS_DMA_ALLOCNOW, /* flags */
672 twa_busdma_lock, /* lockfunc */
673 sc->io_lock, /* lockfuncarg */
674 &sc->ioctl_tag /* tag */)) {
675 tw_osli_printf(sc, "error = %d",
676 TW_CL_SEVERITY_ERROR_STRING,
677 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
679 "Can't allocate DMA tag for ioctl data buffers",
684 /* Create just one map for all ioctl request data buffers. */
685 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
686 tw_osli_printf(sc, "error = %d",
687 TW_CL_SEVERITY_ERROR_STRING,
688 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
690 "Can't create ioctl map",
696 /* Initialize request queues. */
697 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
698 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
700 if ((sc->req_ctx_buf = (struct tw_osli_req_context *)
701 malloc((sizeof(struct tw_osli_req_context) *
702 TW_OSLI_MAX_NUM_REQUESTS),
703 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
704 tw_osli_printf(sc, "error = %d",
705 TW_CL_SEVERITY_ERROR_STRING,
706 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
708 "Failed to allocate request packets",
712 bzero(sc->req_ctx_buf,
713 sizeof(struct tw_osli_req_context) * TW_OSLI_MAX_NUM_REQUESTS);
715 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
716 req = &(sc->req_ctx_buf[i]);
718 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
719 tw_osli_printf(sc, "request # = %d, error = %d",
720 TW_CL_SEVERITY_ERROR_STRING,
721 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
723 "Can't create dma map",
728 /* Initialize the ioctl wakeup/ timeout mutex */
729 req->ioctl_wake_timeout_lock = &(req->ioctl_wake_timeout_lock_handle);
730 mtx_init(req->ioctl_wake_timeout_lock, "tw_ioctl_wake_timeout_lock", NULL, MTX_DEF);
732 /* Insert request into the free queue. */
733 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
742 * Function name: tw_osli_free_resources
743 * Description: Performs clean-up at the time of going down.
745 * Input: sc -- ptr to OSL internal ctlr context
750 tw_osli_free_resources(struct twa_softc *sc)
752 struct tw_osli_req_context *req;
755 tw_osli_dbg_dprintf(3, sc, "entered");
757 /* Detach from CAM */
758 tw_osli_cam_detach(sc);
761 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
763 mtx_destroy(req->ioctl_wake_timeout_lock);
765 if ((error = bus_dmamap_destroy(sc->dma_tag,
767 tw_osli_dbg_dprintf(1, sc,
768 "dmamap_destroy(dma) returned %d",
772 if ((sc->ioctl_tag) && (sc->ioctl_map))
773 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
774 tw_osli_dbg_dprintf(1, sc,
775 "dmamap_destroy(ioctl) returned %d", error);
777 /* Free all memory allocated so far. */
779 free(sc->req_ctx_buf, TW_OSLI_MALLOC_CLASS);
782 free(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
785 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
786 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
790 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
791 tw_osli_dbg_dprintf(1, sc,
792 "dma_tag_destroy(cmd) returned %d", error);
795 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
796 tw_osli_dbg_dprintf(1, sc,
797 "dma_tag_destroy(dma) returned %d", error);
800 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
801 tw_osli_dbg_dprintf(1, sc,
802 "dma_tag_destroy(ioctl) returned %d", error);
805 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
806 tw_osli_dbg_dprintf(1, sc,
807 "dma_tag_destroy(parent) returned %d", error);
810 /* Disconnect the interrupt handler. */
811 if ((error = twa_teardown_intr(sc)))
812 tw_osli_dbg_dprintf(1, sc,
813 "teardown_intr returned %d", error);
815 if (sc->irq_res != NULL)
816 if ((error = bus_release_resource(sc->bus_dev,
817 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
818 tw_osli_dbg_dprintf(1, sc,
819 "release_resource(irq) returned %d", error);
822 /* Release the register window mapping. */
823 if (sc->reg_res != NULL)
824 if ((error = bus_release_resource(sc->bus_dev,
825 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
826 tw_osli_dbg_dprintf(1, sc,
827 "release_resource(io) returned %d", error);
830 /* Destroy the control device. */
831 if (sc->ctrl_dev != (struct cdev *)NULL)
832 destroy_dev(sc->ctrl_dev);
834 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
835 tw_osli_dbg_dprintf(1, sc,
836 "sysctl_ctx_free returned %d", error);
843 * Function name: twa_detach
844 * Description: Called when the controller is being detached from
847 * Input: dev -- bus device corresponding to the ctlr
849 * Return value: 0 -- success
853 twa_detach(device_t dev)
855 struct twa_softc *sc = device_get_softc(dev);
858 tw_osli_dbg_dprintf(3, sc, "entered");
862 tw_osli_printf(sc, "error = %d",
863 TW_CL_SEVERITY_ERROR_STRING,
864 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
871 /* Shut the controller down. */
872 if ((error = twa_shutdown(dev)))
875 /* Free all resources associated with this controller. */
876 tw_osli_free_resources(sc);
886 * Function name: twa_shutdown
887 * Description: Called at unload/shutdown time. Lets the controller
888 * know that we are going down.
890 * Input: dev -- bus device corresponding to the ctlr
892 * Return value: 0 -- success
896 twa_shutdown(device_t dev)
898 struct twa_softc *sc = device_get_softc(dev);
901 tw_osli_dbg_dprintf(3, sc, "entered");
903 /* Disconnect interrupts. */
904 error = twa_teardown_intr(sc);
906 /* Stop watchdog task. */
907 callout_drain(&(sc->watchdog_callout[0]));
908 callout_drain(&(sc->watchdog_callout[1]));
910 /* Disconnect from the controller. */
911 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
912 tw_osli_printf(sc, "error = %d",
913 TW_CL_SEVERITY_ERROR_STRING,
914 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
916 "Failed to shutdown Common Layer/controller",
925 * Function name: twa_busdma_lock
926 * Description: Function to provide synchronization during busdma_swi.
928 * Input: lock_arg -- lock mutex sent as argument
929 * op -- operation (lock/unlock) expected of the function
934 twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op)
938 lock = (struct mtx *)lock_arg;
945 mtx_unlock_spin(lock);
949 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
955 * Function name: twa_pci_intr
956 * Description: Interrupt handler. Wrapper for twa_interrupt.
958 * Input: arg -- ptr to OSL internal ctlr context
963 twa_pci_intr(TW_VOID *arg)
965 struct twa_softc *sc = (struct twa_softc *)arg;
967 tw_osli_dbg_dprintf(10, sc, "entered");
968 tw_cl_interrupt(&(sc->ctlr_handle));
973 * Function name: tw_osli_fw_passthru
974 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
976 * Input: sc -- ptr to OSL internal ctlr context
977 * buf -- ptr to ioctl pkt understood by CL
979 * Return value: 0 -- success
983 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
985 struct tw_osli_req_context *req;
986 struct tw_osli_ioctl_no_data_buf *user_buf =
987 (struct tw_osli_ioctl_no_data_buf *)buf;
989 TW_UINT32 timeout = 60;
990 TW_UINT32 data_buf_size_adjusted;
991 struct tw_cl_req_packet *req_pkt;
992 struct tw_cl_passthru_req_packet *pt_req;
995 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
997 if ((req = tw_osli_get_request(sc)) == NULL)
1000 req->req_handle.osl_req_ctxt = req;
1001 req->orig_req = buf;
1002 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
1004 req_pkt = &(req->req_pkt);
1005 req_pkt->status = 0;
1006 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
1007 /* Let the Common Layer retry the request on cmd queue full. */
1008 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
1010 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1012 * Make sure that the data buffer sent to firmware is a
1013 * 512 byte multiple in size.
1015 data_buf_size_adjusted =
1016 (user_buf->driver_pkt.buffer_length +
1017 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
1018 if ((req->length = data_buf_size_adjusted)) {
1019 if ((req->data = malloc(data_buf_size_adjusted,
1020 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
1022 tw_osli_printf(sc, "error = %d",
1023 TW_CL_SEVERITY_ERROR_STRING,
1024 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1026 "Could not alloc mem for "
1027 "fw_passthru data_buf",
1029 goto fw_passthru_err;
1031 /* Copy the payload. */
1032 if ((error = copyin((TW_VOID *)(user_buf->pdata),
1034 user_buf->driver_pkt.buffer_length)) != 0) {
1035 tw_osli_printf(sc, "error = %d",
1036 TW_CL_SEVERITY_ERROR_STRING,
1037 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1039 "Could not copyin fw_passthru data_buf",
1041 goto fw_passthru_err;
1043 pt_req->sgl_entries = 1; /* will be updated during mapping */
1044 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
1045 TW_OSLI_REQ_FLAGS_DATA_OUT);
1047 pt_req->sgl_entries = 0; /* no payload */
1049 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
1050 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
1052 if ((error = tw_osli_map_request(req)))
1053 goto fw_passthru_err;
1055 end_time = tw_osl_get_local_time() + timeout;
1056 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
1057 mtx_lock(req->ioctl_wake_timeout_lock);
1058 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
1060 error = mtx_sleep(req, req->ioctl_wake_timeout_lock, 0,
1061 "twa_passthru", timeout*hz);
1062 mtx_unlock(req->ioctl_wake_timeout_lock);
1064 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
1066 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1069 if (((error = req->error_code)) ||
1070 ((error = (req->state !=
1071 TW_OSLI_REQ_STATE_COMPLETE))) ||
1072 ((error = req_pkt->status)))
1073 goto fw_passthru_err;
1077 if (req_pkt->status) {
1078 error = req_pkt->status;
1079 goto fw_passthru_err;
1082 if (error == EWOULDBLOCK) {
1084 if ((!(req->error_code)) &&
1085 (req->state == TW_OSLI_REQ_STATE_COMPLETE) &&
1086 (!(req_pkt->status)) ) {
1088 tw_osli_printf(sc, "request = %p",
1089 TW_CL_SEVERITY_ERROR_STRING,
1090 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1092 "FALSE Passthru timeout!",
1094 #endif /* TW_OSL_DEBUG */
1095 error = 0; /* False error */
1098 if (!(tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
1100 tw_osli_printf(sc, "request = %p",
1101 TW_CL_SEVERITY_ERROR_STRING,
1102 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1104 "Passthru request timed out!",
1106 #else /* TW_OSL_DEBUG */
1107 device_printf((sc)->bus_dev, "Passthru request timed out!\n");
1108 #endif /* TW_OSL_DEBUG */
1109 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
1113 end_time = tw_osl_get_local_time() + timeout;
1116 * Don't touch req after a reset. It (and any
1117 * associated data) will be
1118 * unmapped by the callback.
1122 * Either the request got completed, or we were woken up by a
1123 * signal. Calculate the new timeout, in case it was the latter.
1125 timeout = (end_time - tw_osl_get_local_time());
1126 } /* End of while loop */
1128 /* If there was a payload, copy it back. */
1129 if ((!error) && (req->length))
1130 if ((error = copyout(req->data, user_buf->pdata,
1131 user_buf->driver_pkt.buffer_length)))
1132 tw_osli_printf(sc, "error = %d",
1133 TW_CL_SEVERITY_ERROR_STRING,
1134 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1136 "Could not copyout fw_passthru data_buf",
1141 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1144 user_buf->driver_pkt.os_status = error;
1145 /* Free resources. */
1147 free(req->data, TW_OSLI_MALLOC_CLASS);
1148 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1155 * Function name: tw_osl_complete_passthru
1156 * Description: Called to complete passthru requests.
1158 * Input: req_handle -- ptr to request handle
1160 * Return value: None
1163 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1165 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1166 struct tw_cl_req_packet *req_pkt =
1167 (struct tw_cl_req_packet *)(&req->req_pkt);
1168 struct twa_softc *sc = req->ctlr;
1170 tw_osli_dbg_dprintf(5, sc, "entered");
1172 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1173 tw_osli_printf(sc, "request = %p, status = %d",
1174 TW_CL_SEVERITY_ERROR_STRING,
1175 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1177 "Unposted command completed!!",
1182 * Remove request from the busy queue. Just mark it complete.
1183 * There's no need to move it into the complete queue as we are
1184 * going to be done with it right now.
1186 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1187 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1189 tw_osli_unmap_request(req);
1192 * Don't do a wake up if there was an error even before the request
1193 * was sent down to the Common Layer, and we hadn't gotten an
1194 * EINPROGRESS. The request originator will then be returned an
1195 * error, and he can do the clean-up.
1197 if ((req->error_code) && (!(req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1200 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1201 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1202 /* Wake up the sleeping command originator. */
1203 tw_osli_dbg_dprintf(5, sc,
1204 "Waking up originator of request %p", req);
1205 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1209 * If the request completed even before mtx_sleep
1210 * was called, simply return.
1212 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1215 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1218 tw_osli_printf(sc, "request = %p",
1219 TW_CL_SEVERITY_ERROR_STRING,
1220 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1222 "Passthru callback called, "
1223 "and caller not sleeping",
1227 tw_osli_printf(sc, "request = %p",
1228 TW_CL_SEVERITY_ERROR_STRING,
1229 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1231 "Passthru callback called for non-passthru request",
1239 * Function name: tw_osli_get_request
1240 * Description: Gets a request pkt from the free queue.
1242 * Input: sc -- ptr to OSL internal ctlr context
1244 * Return value: ptr to request pkt -- success
1247 struct tw_osli_req_context *
1248 tw_osli_get_request(struct twa_softc *sc)
1250 struct tw_osli_req_context *req;
1252 tw_osli_dbg_dprintf(4, sc, "entered");
1254 /* Get a free request packet. */
1255 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1257 /* Initialize some fields to their defaults. */
1259 req->req_handle.osl_req_ctxt = NULL;
1260 req->req_handle.cl_req_ctxt = NULL;
1261 req->req_handle.is_io = 0;
1265 req->real_data = NULL;
1266 req->real_length = 0;
1267 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1269 req->error_code = 0;
1270 req->orig_req = NULL;
1272 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1281 * Function name: twa_map_load_data_callback
1282 * Description: Callback of bus_dmamap_load for the buffer associated
1283 * with data. Updates the cmd pkt (size/sgl_entries
1284 * fields, as applicable) to reflect the number of sg
1287 * Input: arg -- ptr to OSL internal request context
1288 * segs -- ptr to a list of segment descriptors
1289 * nsegments--# of segments
1290 * error -- 0 if no errors encountered before callback,
1291 * non-zero if errors were encountered
1293 * Return value: None
1296 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1297 TW_INT32 nsegments, TW_INT32 error)
1299 struct tw_osli_req_context *req =
1300 (struct tw_osli_req_context *)arg;
1301 struct twa_softc *sc = req->ctlr;
1302 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1304 tw_osli_dbg_dprintf(10, sc, "entered");
1306 if (error == EINVAL) {
1307 req->error_code = error;
1311 /* Mark the request as currently being processed. */
1312 req->state = TW_OSLI_REQ_STATE_BUSY;
1313 /* Move the request into the busy queue. */
1314 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1316 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1318 if (error == EFBIG) {
1319 req->error_code = error;
1323 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1324 struct tw_cl_passthru_req_packet *pt_req;
1326 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1327 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1328 BUS_DMASYNC_PREREAD);
1330 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1332 * If we're using an alignment buffer, and we're
1333 * writing data, copy the real data out.
1335 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1336 bcopy(req->real_data, req->data, req->real_length);
1337 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1338 BUS_DMASYNC_PREWRITE);
1341 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1342 pt_req->sg_list = (TW_UINT8 *)segs;
1343 pt_req->sgl_entries += (nsegments - 1);
1344 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1345 &(req->req_handle));
1347 struct tw_cl_scsi_req_packet *scsi_req;
1349 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1350 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1351 BUS_DMASYNC_PREREAD);
1353 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1355 * If we're using an alignment buffer, and we're
1356 * writing data, copy the real data out.
1358 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1359 bcopy(req->real_data, req->data, req->real_length);
1360 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1361 BUS_DMASYNC_PREWRITE);
1364 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1365 scsi_req->sg_list = (TW_UINT8 *)segs;
1366 scsi_req->sgl_entries += (nsegments - 1);
1367 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1368 &(req->req_handle));
1373 req->error_code = error;
1374 req_pkt->tw_osl_callback(&(req->req_handle));
1376 * If the caller had been returned EINPROGRESS, and he has
1377 * registered a callback for handling completion, the callback
1378 * will never get called because we were unable to submit the
1379 * request. So, free up the request right here.
1381 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1382 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1389 * Function name: twa_map_load_callback
1390 * Description: Callback of bus_dmamap_load for the buffer associated
1393 * Input: arg -- ptr to variable to hold phys addr
1394 * segs -- ptr to a list of segment descriptors
1395 * nsegments--# of segments
1396 * error -- 0 if no errors encountered before callback,
1397 * non-zero if errors were encountered
1399 * Return value: None
1402 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1403 TW_INT32 nsegments, TW_INT32 error)
1405 *((bus_addr_t *)arg) = segs[0].ds_addr;
1411 * Function name: tw_osli_map_request
1412 * Description: Maps a cmd pkt and data associated with it, into
1415 * Input: req -- ptr to request pkt
1417 * Return value: 0 -- success
1418 * non-zero-- failure
1421 tw_osli_map_request(struct tw_osli_req_context *req)
1423 struct twa_softc *sc = req->ctlr;
1426 tw_osli_dbg_dprintf(10, sc, "entered");
1428 /* If the command involves data, map that too. */
1429 if (req->data != NULL) {
1431 * It's sufficient for the data pointer to be 4-byte aligned
1432 * to work with 9000. However, if 4-byte aligned addresses
1433 * are passed to bus_dmamap_load, we can get back sg elements
1434 * that are not 512-byte multiples in size. So, we will let
1435 * only those buffers that are 512-byte aligned to pass
1436 * through, and bounce the rest, so as to make sure that we
1437 * always get back sg elements that are 512-byte multiples
1440 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1441 (req->length % sc->sg_size_factor)) {
1442 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1443 /* Save original data pointer and length. */
1444 req->real_data = req->data;
1445 req->real_length = req->length;
1446 req->length = (req->length +
1447 (sc->sg_size_factor - 1)) &
1448 ~(sc->sg_size_factor - 1);
1449 req->data = malloc(req->length, TW_OSLI_MALLOC_CLASS,
1451 if (req->data == NULL) {
1452 tw_osli_printf(sc, "error = %d",
1453 TW_CL_SEVERITY_ERROR_STRING,
1454 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1456 "Failed to allocate memory "
1457 "for bounce buffer",
1459 /* Restore original data pointer and length. */
1460 req->data = req->real_data;
1461 req->length = req->real_length;
1467 * Map the data buffer into bus space and build the SG list.
1469 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1470 /* Lock against multiple simultaneous ioctl calls. */
1471 mtx_lock_spin(sc->io_lock);
1472 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1473 req->data, req->length,
1474 twa_map_load_data_callback, req,
1476 mtx_unlock_spin(sc->io_lock);
1479 * There's only one CAM I/O thread running at a time.
1480 * So, there's no need to hold the io_lock.
1482 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1483 req->data, req->length,
1484 twa_map_load_data_callback, req,
1489 error = req->error_code;
1491 if (error == EINPROGRESS) {
1493 * Specifying sc->io_lock as the lockfuncarg
1494 * in ...tag_create should protect the access
1495 * of ...FLAGS_MAPPED from the callback.
1497 mtx_lock_spin(sc->io_lock);
1498 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED))
1499 req->flags |= TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1500 tw_osli_disallow_new_requests(sc, &(req->req_handle));
1501 mtx_unlock_spin(sc->io_lock);
1504 tw_osli_printf(sc, "error = %d",
1505 TW_CL_SEVERITY_ERROR_STRING,
1506 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1508 "Failed to map DMA memory "
1511 req->flags |= TW_OSLI_REQ_FLAGS_FAILED;
1512 /* Free alignment buffer if it was used. */
1514 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1515 free(req->data, TW_OSLI_MALLOC_CLASS);
1517 * Restore original data pointer
1520 req->data = req->real_data;
1521 req->length = req->real_length;
1527 /* Mark the request as currently being processed. */
1528 req->state = TW_OSLI_REQ_STATE_BUSY;
1529 /* Move the request into the busy queue. */
1530 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1531 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1532 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1533 &(req->req_pkt), &(req->req_handle));
1535 error = tw_cl_start_io(&sc->ctlr_handle,
1536 &(req->req_pkt), &(req->req_handle));
1538 req->error_code = error;
1539 req->req_pkt.tw_osl_callback(&(req->req_handle));
1548 * Function name: tw_osli_unmap_request
1549 * Description: Undoes the mapping done by tw_osli_map_request.
1551 * Input: req -- ptr to request pkt
1553 * Return value: None
1556 tw_osli_unmap_request(struct tw_osli_req_context *req)
1558 struct twa_softc *sc = req->ctlr;
1560 tw_osli_dbg_dprintf(10, sc, "entered");
1562 /* If the command involved data, unmap that too. */
1563 if (req->data != NULL) {
1564 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1565 /* Lock against multiple simultaneous ioctl calls. */
1566 mtx_lock_spin(sc->io_lock);
1568 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1569 bus_dmamap_sync(sc->ioctl_tag,
1570 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1573 * If we are using a bounce buffer, and we are
1574 * reading data, copy the real data in.
1576 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1577 bcopy(req->data, req->real_data,
1581 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1582 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1583 BUS_DMASYNC_POSTWRITE);
1585 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1587 mtx_unlock_spin(sc->io_lock);
1589 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1590 bus_dmamap_sync(sc->dma_tag,
1591 req->dma_map, BUS_DMASYNC_POSTREAD);
1594 * If we are using a bounce buffer, and we are
1595 * reading data, copy the real data in.
1597 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1598 bcopy(req->data, req->real_data,
1601 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1602 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1603 BUS_DMASYNC_POSTWRITE);
1605 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1609 /* Free alignment buffer if it was used. */
1610 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1611 free(req->data, TW_OSLI_MALLOC_CLASS);
1612 /* Restore original data pointer and length. */
1613 req->data = req->real_data;
1614 req->length = req->real_length;
1622 TW_VOID twa_report_stats(TW_VOID);
1623 TW_VOID twa_reset_stats(TW_VOID);
1624 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1625 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1629 * Function name: twa_report_stats
1630 * Description: For being called from ddb. Calls functions that print
1631 * OSL and CL internal stats for the controller.
1635 * Return value: None
1638 twa_report_stats(TW_VOID)
1640 struct twa_softc *sc;
1643 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1644 tw_osli_print_ctlr_stats(sc);
1645 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1652 * Function name: tw_osli_print_ctlr_stats
1653 * Description: For being called from ddb. Prints OSL controller stats
1655 * Input: sc -- ptr to OSL internal controller context
1657 * Return value: None
1660 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1662 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1663 twa_printf(sc, "OSLq type current max\n");
1664 twa_printf(sc, "free %04d %04d\n",
1665 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1666 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1667 twa_printf(sc, "busy %04d %04d\n",
1668 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1669 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1675 * Function name: twa_print_req_info
1676 * Description: For being called from ddb. Calls functions that print
1677 * OSL and CL internal details for the request.
1679 * Input: req -- ptr to OSL internal request context
1681 * Return value: None
1684 twa_print_req_info(struct tw_osli_req_context *req)
1686 struct twa_softc *sc = req->ctlr;
1688 twa_printf(sc, "OSL details for request:\n");
1689 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1690 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1691 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1692 "next_req = %p, prev_req = %p, dma_map = %p\n",
1693 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1694 req->data, req->length, req->real_data, req->real_length,
1695 req->state, req->flags, req->error_code, req->orig_req,
1696 req->link.next, req->link.prev, req->dma_map);
1697 tw_cl_print_req_info(&(req->req_handle));
1703 * Function name: twa_reset_stats
1704 * Description: For being called from ddb.
1705 * Resets some OSL controller stats.
1709 * Return value: None
1712 twa_reset_stats(TW_VOID)
1714 struct twa_softc *sc;
1717 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1718 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1719 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1720 tw_cl_reset_stats(&sc->ctlr_handle);
1724 #endif /* TW_OSL_DEBUG */