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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * AMCC'S 3ware driver for 9000 series storage controllers.
36 * Author: Vinod Kashyap
37 * Modifications by: Adam Radford
38 * Modifications by: Manjunath Ranganathaiah
43 * FreeBSD specific functions not related to CAM, and other
44 * miscellaneous functions.
48 #include <dev/twa/tw_osl_includes.h>
49 #include <dev/twa/tw_cl_fwif.h>
50 #include <dev/twa/tw_cl_ioctl.h>
51 #include <dev/twa/tw_osl_ioctl.h>
54 TW_INT32 TW_DEBUG_LEVEL_FOR_OSL = TW_OSL_DEBUG;
55 TW_INT32 TW_OSL_DEBUG_LEVEL_FOR_CL = TW_OSL_DEBUG;
56 #endif /* TW_OSL_DEBUG */
58 static MALLOC_DEFINE(TW_OSLI_MALLOC_CLASS, "twa_commands", "twa commands");
61 static d_open_t twa_open;
62 static d_close_t twa_close;
63 static d_ioctl_t twa_ioctl;
65 static struct cdevsw twa_cdevsw = {
66 .d_version = D_VERSION,
73 static devclass_t twa_devclass;
77 * Function name: twa_open
78 * Description: Called when the controller is opened.
79 * Simply marks the controller as open.
81 * Input: dev -- control device corresponding to the ctlr
82 * flags -- mode of open
83 * fmt -- device type (character/block etc.)
84 * proc -- current process
86 * Return value: 0 -- success
90 twa_open(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, struct thread *proc)
92 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
94 tw_osli_dbg_dprintf(5, sc, "entered");
95 sc->open = TW_CL_TRUE;
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, struct thread *proc)
117 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
119 tw_osli_dbg_dprintf(5, sc, "entered");
120 sc->open = TW_CL_FALSE;
127 * Function name: twa_ioctl
128 * Description: Called when an ioctl is posted to the controller.
129 * Handles any OS Layer specific cmds, passes the rest
130 * on to the Common Layer.
132 * Input: dev -- control device corresponding to the ctlr
134 * buf -- ptr to buffer in kernel memory, which is
135 * a copy of the input buffer in user-space
136 * flags -- mode of corresponding open
137 * proc -- current process
138 * Output: buf -- ptr to buffer in kernel memory, which will
139 * be copied to the output buffer in user-space
140 * Return value: 0 -- success
144 twa_ioctl(struct cdev *dev, u_long cmd, caddr_t buf, TW_INT32 flags, struct thread *proc)
146 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1);
149 tw_osli_dbg_dprintf(5, sc, "entered");
152 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
153 tw_osli_dbg_dprintf(6, sc, "ioctl: fw_passthru");
154 error = tw_osli_fw_passthru(sc, (TW_INT8 *)buf);
157 case TW_OSL_IOCTL_SCAN_BUS:
158 /* Request CAM for a bus scan. */
159 tw_osli_dbg_dprintf(6, sc, "ioctl: scan bus");
160 error = tw_osli_request_bus_scan(sc);
164 tw_osli_dbg_dprintf(6, sc, "ioctl: 0x%lx", cmd);
165 error = tw_cl_ioctl(&sc->ctlr_handle, cmd, buf);
173 static TW_INT32 twa_probe(device_t dev);
174 static TW_INT32 twa_attach(device_t dev);
175 static TW_INT32 twa_detach(device_t dev);
176 static TW_INT32 twa_shutdown(device_t dev);
177 static TW_VOID twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op);
178 static TW_VOID twa_pci_intr(TW_VOID *arg);
179 static TW_VOID twa_watchdog(TW_VOID *arg);
180 int twa_setup_intr(struct twa_softc *sc);
181 int twa_teardown_intr(struct twa_softc *sc);
183 static TW_INT32 tw_osli_alloc_mem(struct twa_softc *sc);
184 static TW_VOID tw_osli_free_resources(struct twa_softc *sc);
186 static TW_VOID twa_map_load_data_callback(TW_VOID *arg,
187 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
188 static TW_VOID twa_map_load_callback(TW_VOID *arg,
189 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error);
192 static device_method_t twa_methods[] = {
193 /* Device interface */
194 DEVMETHOD(device_probe, twa_probe),
195 DEVMETHOD(device_attach, twa_attach),
196 DEVMETHOD(device_detach, twa_detach),
197 DEVMETHOD(device_shutdown, twa_shutdown),
202 static driver_t twa_pci_driver = {
205 sizeof(struct twa_softc)
208 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, 0, 0);
209 MODULE_DEPEND(twa, cam, 1, 1, 1);
210 MODULE_DEPEND(twa, pci, 1, 1, 1);
214 * Function name: twa_probe
215 * Description: Called at driver load time. Claims 9000 ctlrs.
217 * Input: dev -- bus device corresponding to the ctlr
219 * Return value: <= 0 -- success
223 twa_probe(device_t dev)
225 static TW_UINT8 first_ctlr = 1;
227 tw_osli_dbg_printf(3, "entered");
229 if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) {
230 device_set_desc(dev, TW_OSLI_DEVICE_NAME);
231 /* Print the driver version only once. */
233 printf("3ware device driver for 9000 series storage "
234 "controllers, version: %s\n",
235 TW_OSL_DRIVER_VERSION_STRING);
243 int twa_setup_intr(struct twa_softc *sc)
247 if (!(sc->intr_handle) && (sc->irq_res)) {
248 error = bus_setup_intr(sc->bus_dev, sc->irq_res,
249 INTR_TYPE_CAM | INTR_MPSAFE,
251 sc, &sc->intr_handle);
257 int twa_teardown_intr(struct twa_softc *sc)
261 if ((sc->intr_handle) && (sc->irq_res)) {
262 error = bus_teardown_intr(sc->bus_dev,
263 sc->irq_res, sc->intr_handle);
264 sc->intr_handle = NULL;
272 * Function name: twa_attach
273 * Description: Allocates pci resources; updates sc; adds a node to the
274 * sysctl tree to expose the driver version; makes calls
275 * (to the Common Layer) to initialize ctlr, and to
278 * Input: dev -- bus device corresponding to the ctlr
280 * Return value: 0 -- success
284 twa_attach(device_t dev)
286 struct twa_softc *sc = device_get_softc(dev);
289 TW_INT32 bar0_offset;
293 tw_osli_dbg_dprintf(3, sc, "entered");
295 sc->ctlr_handle.osl_ctlr_ctxt = sc;
297 /* Initialize the softc structure. */
299 sc->device_id = pci_get_device(dev);
301 /* Initialize the mutexes right here. */
302 sc->io_lock = &(sc->io_lock_handle);
303 mtx_init(sc->io_lock, "tw_osl_io_lock", NULL, MTX_SPIN);
304 sc->q_lock = &(sc->q_lock_handle);
305 mtx_init(sc->q_lock, "tw_osl_q_lock", NULL, MTX_SPIN);
306 sc->sim_lock = &(sc->sim_lock_handle);
307 mtx_init(sc->sim_lock, "tw_osl_sim_lock", NULL, MTX_DEF | MTX_RECURSE);
309 sysctl_ctx_init(&sc->sysctl_ctxt);
310 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
311 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
312 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
313 if (sc->sysctl_tree == NULL) {
314 tw_osli_printf(sc, "error = %d",
315 TW_CL_SEVERITY_ERROR_STRING,
316 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
318 "Cannot add sysctl tree node",
322 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
323 OID_AUTO, "driver_version", CTLFLAG_RD,
324 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
326 /* Make sure we are going to be able to talk to this board. */
327 command = pci_read_config(dev, PCIR_COMMAND, 2);
328 if ((command & PCIM_CMD_PORTEN) == 0) {
329 tw_osli_printf(sc, "error = %d",
330 TW_CL_SEVERITY_ERROR_STRING,
331 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
333 "Register window not available",
335 tw_osli_free_resources(sc);
339 /* Force the busmaster enable bit on, in case the BIOS forgot. */
340 command |= PCIM_CMD_BUSMASTEREN;
341 pci_write_config(dev, PCIR_COMMAND, command, 2);
343 /* Allocate the PCI register window. */
344 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
345 &bar_num, &bar0_offset, &bar_size))) {
346 tw_osli_printf(sc, "error = %d",
347 TW_CL_SEVERITY_ERROR_STRING,
348 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
350 "Can't get PCI BAR info",
352 tw_osli_free_resources(sc);
355 sc->reg_res_id = PCIR_BARS + bar0_offset;
356 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
357 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
359 tw_osli_printf(sc, "error = %d",
360 TW_CL_SEVERITY_ERROR_STRING,
361 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
363 "Can't allocate register window",
365 tw_osli_free_resources(sc);
368 sc->bus_tag = rman_get_bustag(sc->reg_res);
369 sc->bus_handle = rman_get_bushandle(sc->reg_res);
371 /* Allocate and register our interrupt. */
373 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
374 &(sc->irq_res_id), 0, ~0, 1,
375 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
376 tw_osli_printf(sc, "error = %d",
377 TW_CL_SEVERITY_ERROR_STRING,
378 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
380 "Can't allocate interrupt",
382 tw_osli_free_resources(sc);
385 if ((error = twa_setup_intr(sc))) {
386 tw_osli_printf(sc, "error = %d",
387 TW_CL_SEVERITY_ERROR_STRING,
388 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
390 "Can't set up interrupt",
392 tw_osli_free_resources(sc);
396 if ((error = tw_osli_alloc_mem(sc))) {
397 tw_osli_printf(sc, "error = %d",
398 TW_CL_SEVERITY_ERROR_STRING,
399 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
401 "Memory allocation failure",
403 tw_osli_free_resources(sc);
407 /* Initialize the Common Layer for this controller. */
408 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
409 TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
410 sc->non_dma_mem, sc->dma_mem,
413 tw_osli_printf(sc, "error = %d",
414 TW_CL_SEVERITY_ERROR_STRING,
415 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
417 "Failed to initialize Common Layer/controller",
419 tw_osli_free_resources(sc);
423 /* Create the control device. */
424 sc->ctrl_dev = make_dev(&twa_cdevsw, device_get_unit(sc->bus_dev),
425 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
426 "twa%d", device_get_unit(sc->bus_dev));
427 sc->ctrl_dev->si_drv1 = sc;
429 if ((error = tw_osli_cam_attach(sc))) {
430 tw_osli_free_resources(sc);
431 tw_osli_printf(sc, "error = %d",
432 TW_CL_SEVERITY_ERROR_STRING,
433 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
435 "Failed to initialize CAM",
440 sc->watchdog_index = 0;
441 callout_init(&(sc->watchdog_callout[0]), CALLOUT_MPSAFE);
442 callout_init(&(sc->watchdog_callout[1]), CALLOUT_MPSAFE);
443 callout_reset(&(sc->watchdog_callout[0]), 5*hz, twa_watchdog, &sc->ctlr_handle);
450 twa_watchdog(TW_VOID *arg)
452 struct tw_cl_ctlr_handle *ctlr_handle =
453 (struct tw_cl_ctlr_handle *)arg;
454 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
456 int i_need_a_reset = 0;
457 int driver_is_active = 0;
458 int my_watchdog_was_pending = 1234;
459 TW_UINT64 current_time;
460 struct tw_osli_req_context *my_req;
463 //==============================================================================
464 current_time = (TW_UINT64) (tw_osl_get_local_time());
466 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
467 my_req = &(sc->req_ctx_buf[i]);
469 if ((my_req->state == TW_OSLI_REQ_STATE_BUSY) &&
470 (my_req->deadline) &&
471 (my_req->deadline < current_time)) {
472 tw_cl_set_reset_needed(ctlr_handle);
474 device_printf((sc)->bus_dev, "Request %d timed out! d = %llu, c = %llu\n", i, my_req->deadline, current_time);
475 #else /* TW_OSL_DEBUG */
476 device_printf((sc)->bus_dev, "Request %d timed out!\n", i);
477 #endif /* TW_OSL_DEBUG */
481 //==============================================================================
483 i_need_a_reset = tw_cl_is_reset_needed(ctlr_handle);
485 i = (int) ((sc->watchdog_index++) & 1);
487 driver_is_active = tw_cl_is_active(ctlr_handle);
489 if (i_need_a_reset) {
491 device_printf((sc)->bus_dev, "Watchdog rescheduled in 70 seconds\n");
492 #endif /* TW_OSL_DEBUG */
493 my_watchdog_was_pending =
494 callout_reset(&(sc->watchdog_callout[i]), 70*hz, twa_watchdog, &sc->ctlr_handle);
495 tw_cl_reset_ctlr(ctlr_handle);
497 device_printf((sc)->bus_dev, "Watchdog reset completed!\n");
498 #endif /* TW_OSL_DEBUG */
499 } else if (driver_is_active) {
500 my_watchdog_was_pending =
501 callout_reset(&(sc->watchdog_callout[i]), 5*hz, twa_watchdog, &sc->ctlr_handle);
504 if (i_need_a_reset || my_watchdog_was_pending)
505 device_printf((sc)->bus_dev, "i_need_a_reset = %d, "
506 "driver_is_active = %d, my_watchdog_was_pending = %d\n",
507 i_need_a_reset, driver_is_active, my_watchdog_was_pending);
508 #endif /* TW_OSL_DEBUG */
513 * Function name: tw_osli_alloc_mem
514 * Description: Allocates memory needed both by CL and OSL.
516 * Input: sc -- OSL internal controller context
518 * Return value: 0 -- success
522 tw_osli_alloc_mem(struct twa_softc *sc)
524 struct tw_osli_req_context *req;
525 TW_UINT32 max_sg_elements;
526 TW_UINT32 non_dma_mem_size;
527 TW_UINT32 dma_mem_size;
531 tw_osli_dbg_dprintf(3, sc, "entered");
533 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
534 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
536 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
537 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
539 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
540 sc->device_id, TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS,
541 &(sc->alignment), &(sc->sg_size_factor),
542 &non_dma_mem_size, &dma_mem_size
544 tw_osli_printf(sc, "error = %d",
545 TW_CL_SEVERITY_ERROR_STRING,
546 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
548 "Can't get Common Layer's memory requirements",
553 if ((sc->non_dma_mem = malloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
554 M_WAITOK)) == NULL) {
555 tw_osli_printf(sc, "error = %d",
556 TW_CL_SEVERITY_ERROR_STRING,
557 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
559 "Can't allocate non-dma memory",
564 /* Create the parent dma tag. */
565 if (bus_dma_tag_create(bus_get_dma_tag(sc->bus_dev), /* parent */
566 sc->alignment, /* alignment */
568 BUS_SPACE_MAXADDR, /* lowaddr */
569 BUS_SPACE_MAXADDR, /* highaddr */
570 NULL, NULL, /* filter, filterarg */
571 TW_CL_MAX_IO_SIZE, /* maxsize */
572 max_sg_elements, /* nsegments */
573 TW_CL_MAX_IO_SIZE, /* maxsegsize */
576 NULL, /* lockfuncarg */
577 &sc->parent_tag /* tag */)) {
578 tw_osli_printf(sc, "error = %d",
579 TW_CL_SEVERITY_ERROR_STRING,
580 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
582 "Can't allocate parent DMA tag",
587 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
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 dma_mem_size, /* maxsize */
596 BUS_SPACE_MAXSIZE, /* maxsegsize */
599 NULL, /* lockfuncarg */
600 &sc->cmd_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 Common Layer's "
611 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
612 BUS_DMA_NOWAIT, &sc->cmd_map)) {
613 /* Try a second time. */
614 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
615 BUS_DMA_NOWAIT, &sc->cmd_map)) {
616 tw_osli_printf(sc, "error = %d",
617 TW_CL_SEVERITY_ERROR_STRING,
618 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
620 "Can't allocate DMA'able memory for the"
627 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
628 dma_mem_size, twa_map_load_callback,
629 &sc->dma_mem_phys, 0);
632 * Create a dma tag for data buffers; size will be the maximum
633 * possible I/O size (128kB).
635 if (bus_dma_tag_create(sc->parent_tag, /* parent */
636 sc->alignment, /* alignment */
638 BUS_SPACE_MAXADDR, /* lowaddr */
639 BUS_SPACE_MAXADDR, /* highaddr */
640 NULL, NULL, /* filter, filterarg */
641 TW_CL_MAX_IO_SIZE, /* maxsize */
642 max_sg_elements, /* nsegments */
643 TW_CL_MAX_IO_SIZE, /* maxsegsize */
644 BUS_DMA_ALLOCNOW, /* flags */
645 twa_busdma_lock, /* lockfunc */
646 sc->io_lock, /* lockfuncarg */
647 &sc->dma_tag /* tag */)) {
648 tw_osli_printf(sc, "error = %d",
649 TW_CL_SEVERITY_ERROR_STRING,
650 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
652 "Can't allocate DMA tag for data buffers",
658 * Create a dma tag for ioctl data buffers; size will be the maximum
659 * possible I/O size (128kB).
661 if (bus_dma_tag_create(sc->parent_tag, /* parent */
662 sc->alignment, /* alignment */
664 BUS_SPACE_MAXADDR, /* lowaddr */
665 BUS_SPACE_MAXADDR, /* highaddr */
666 NULL, NULL, /* filter, filterarg */
667 TW_CL_MAX_IO_SIZE, /* maxsize */
668 max_sg_elements, /* nsegments */
669 TW_CL_MAX_IO_SIZE, /* maxsegsize */
670 BUS_DMA_ALLOCNOW, /* flags */
671 twa_busdma_lock, /* lockfunc */
672 sc->io_lock, /* lockfuncarg */
673 &sc->ioctl_tag /* tag */)) {
674 tw_osli_printf(sc, "error = %d",
675 TW_CL_SEVERITY_ERROR_STRING,
676 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
678 "Can't allocate DMA tag for ioctl data buffers",
683 /* Create just one map for all ioctl request data buffers. */
684 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
685 tw_osli_printf(sc, "error = %d",
686 TW_CL_SEVERITY_ERROR_STRING,
687 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
689 "Can't create ioctl map",
695 /* Initialize request queues. */
696 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
697 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
699 if ((sc->req_ctx_buf = (struct tw_osli_req_context *)
700 malloc((sizeof(struct tw_osli_req_context) *
701 TW_OSLI_MAX_NUM_REQUESTS),
702 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
703 tw_osli_printf(sc, "error = %d",
704 TW_CL_SEVERITY_ERROR_STRING,
705 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
707 "Failed to allocate request packets",
711 bzero(sc->req_ctx_buf,
712 sizeof(struct tw_osli_req_context) * TW_OSLI_MAX_NUM_REQUESTS);
714 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) {
715 req = &(sc->req_ctx_buf[i]);
717 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
718 tw_osli_printf(sc, "request # = %d, error = %d",
719 TW_CL_SEVERITY_ERROR_STRING,
720 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
722 "Can't create dma map",
727 /* Initialize the ioctl wakeup/ timeout mutex */
728 req->ioctl_wake_timeout_lock = &(req->ioctl_wake_timeout_lock_handle);
729 mtx_init(req->ioctl_wake_timeout_lock, "tw_ioctl_wake_timeout_lock", NULL, MTX_DEF);
731 /* Insert request into the free queue. */
732 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
741 * Function name: tw_osli_free_resources
742 * Description: Performs clean-up at the time of going down.
744 * Input: sc -- ptr to OSL internal ctlr context
749 tw_osli_free_resources(struct twa_softc *sc)
751 struct tw_osli_req_context *req;
754 tw_osli_dbg_dprintf(3, sc, "entered");
756 /* Detach from CAM */
757 tw_osli_cam_detach(sc);
760 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
762 mtx_destroy(req->ioctl_wake_timeout_lock);
764 if ((error = bus_dmamap_destroy(sc->dma_tag,
766 tw_osli_dbg_dprintf(1, sc,
767 "dmamap_destroy(dma) returned %d",
771 if ((sc->ioctl_tag) && (sc->ioctl_map))
772 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
773 tw_osli_dbg_dprintf(1, sc,
774 "dmamap_destroy(ioctl) returned %d", error);
776 /* Free all memory allocated so far. */
778 free(sc->req_ctx_buf, TW_OSLI_MALLOC_CLASS);
781 free(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
784 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
785 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
789 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
790 tw_osli_dbg_dprintf(1, sc,
791 "dma_tag_destroy(cmd) returned %d", error);
794 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
795 tw_osli_dbg_dprintf(1, sc,
796 "dma_tag_destroy(dma) returned %d", error);
799 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
800 tw_osli_dbg_dprintf(1, sc,
801 "dma_tag_destroy(ioctl) returned %d", error);
804 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
805 tw_osli_dbg_dprintf(1, sc,
806 "dma_tag_destroy(parent) returned %d", error);
809 /* Disconnect the interrupt handler. */
810 if ((error = twa_teardown_intr(sc)))
811 tw_osli_dbg_dprintf(1, sc,
812 "teardown_intr returned %d", error);
814 if (sc->irq_res != NULL)
815 if ((error = bus_release_resource(sc->bus_dev,
816 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
817 tw_osli_dbg_dprintf(1, sc,
818 "release_resource(irq) returned %d", error);
821 /* Release the register window mapping. */
822 if (sc->reg_res != NULL)
823 if ((error = bus_release_resource(sc->bus_dev,
824 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
825 tw_osli_dbg_dprintf(1, sc,
826 "release_resource(io) returned %d", error);
829 /* Destroy the control device. */
830 if (sc->ctrl_dev != (struct cdev *)NULL)
831 destroy_dev(sc->ctrl_dev);
833 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
834 tw_osli_dbg_dprintf(1, sc,
835 "sysctl_ctx_free returned %d", error);
842 * Function name: twa_detach
843 * Description: Called when the controller is being detached from
846 * Input: dev -- bus device corresponding to the ctlr
848 * Return value: 0 -- success
852 twa_detach(device_t dev)
854 struct twa_softc *sc = device_get_softc(dev);
857 tw_osli_dbg_dprintf(3, sc, "entered");
861 tw_osli_printf(sc, "error = %d",
862 TW_CL_SEVERITY_ERROR_STRING,
863 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
870 /* Shut the controller down. */
871 if ((error = twa_shutdown(dev)))
874 /* Free all resources associated with this controller. */
875 tw_osli_free_resources(sc);
885 * Function name: twa_shutdown
886 * Description: Called at unload/shutdown time. Lets the controller
887 * know that we are going down.
889 * Input: dev -- bus device corresponding to the ctlr
891 * Return value: 0 -- success
895 twa_shutdown(device_t dev)
897 struct twa_softc *sc = device_get_softc(dev);
900 tw_osli_dbg_dprintf(3, sc, "entered");
902 /* Disconnect interrupts. */
903 error = twa_teardown_intr(sc);
905 /* Stop watchdog task. */
906 callout_drain(&(sc->watchdog_callout[0]));
907 callout_drain(&(sc->watchdog_callout[1]));
909 /* Disconnect from the controller. */
910 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
911 tw_osli_printf(sc, "error = %d",
912 TW_CL_SEVERITY_ERROR_STRING,
913 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
915 "Failed to shutdown Common Layer/controller",
924 * Function name: twa_busdma_lock
925 * Description: Function to provide synchronization during busdma_swi.
927 * Input: lock_arg -- lock mutex sent as argument
928 * op -- operation (lock/unlock) expected of the function
933 twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op)
937 lock = (struct mtx *)lock_arg;
944 mtx_unlock_spin(lock);
948 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
954 * Function name: twa_pci_intr
955 * Description: Interrupt handler. Wrapper for twa_interrupt.
957 * Input: arg -- ptr to OSL internal ctlr context
962 twa_pci_intr(TW_VOID *arg)
964 struct twa_softc *sc = (struct twa_softc *)arg;
966 tw_osli_dbg_dprintf(10, sc, "entered");
967 tw_cl_interrupt(&(sc->ctlr_handle));
972 * Function name: tw_osli_fw_passthru
973 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
975 * Input: sc -- ptr to OSL internal ctlr context
976 * buf -- ptr to ioctl pkt understood by CL
978 * Return value: 0 -- success
982 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
984 struct tw_osli_req_context *req;
985 struct tw_osli_ioctl_no_data_buf *user_buf =
986 (struct tw_osli_ioctl_no_data_buf *)buf;
988 TW_UINT32 timeout = 60;
989 TW_UINT32 data_buf_size_adjusted;
990 struct tw_cl_req_packet *req_pkt;
991 struct tw_cl_passthru_req_packet *pt_req;
994 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
996 if ((req = tw_osli_get_request(sc)) == NULL)
999 req->req_handle.osl_req_ctxt = req;
1000 req->orig_req = buf;
1001 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
1003 req_pkt = &(req->req_pkt);
1004 req_pkt->status = 0;
1005 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
1006 /* Let the Common Layer retry the request on cmd queue full. */
1007 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
1009 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1011 * Make sure that the data buffer sent to firmware is a
1012 * 512 byte multiple in size.
1014 data_buf_size_adjusted =
1015 (user_buf->driver_pkt.buffer_length +
1016 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
1017 if ((req->length = data_buf_size_adjusted)) {
1018 if ((req->data = malloc(data_buf_size_adjusted,
1019 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
1021 tw_osli_printf(sc, "error = %d",
1022 TW_CL_SEVERITY_ERROR_STRING,
1023 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1025 "Could not alloc mem for "
1026 "fw_passthru data_buf",
1028 goto fw_passthru_err;
1030 /* Copy the payload. */
1031 if ((error = copyin((TW_VOID *)(user_buf->pdata),
1033 user_buf->driver_pkt.buffer_length)) != 0) {
1034 tw_osli_printf(sc, "error = %d",
1035 TW_CL_SEVERITY_ERROR_STRING,
1036 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1038 "Could not copyin fw_passthru data_buf",
1040 goto fw_passthru_err;
1042 pt_req->sgl_entries = 1; /* will be updated during mapping */
1043 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
1044 TW_OSLI_REQ_FLAGS_DATA_OUT);
1046 pt_req->sgl_entries = 0; /* no payload */
1048 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
1049 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
1051 if ((error = tw_osli_map_request(req)))
1052 goto fw_passthru_err;
1054 end_time = tw_osl_get_local_time() + timeout;
1055 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
1056 mtx_lock(req->ioctl_wake_timeout_lock);
1057 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
1059 error = mtx_sleep(req, req->ioctl_wake_timeout_lock, 0,
1060 "twa_passthru", timeout*hz);
1061 mtx_unlock(req->ioctl_wake_timeout_lock);
1063 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
1065 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1068 if (((error = req->error_code)) ||
1069 ((error = (req->state !=
1070 TW_OSLI_REQ_STATE_COMPLETE))) ||
1071 ((error = req_pkt->status)))
1072 goto fw_passthru_err;
1076 if (req_pkt->status) {
1077 error = req_pkt->status;
1078 goto fw_passthru_err;
1081 if (error == EWOULDBLOCK) {
1083 if ((!(req->error_code)) &&
1084 (req->state == TW_OSLI_REQ_STATE_COMPLETE) &&
1085 (!(req_pkt->status)) ) {
1087 tw_osli_printf(sc, "request = %p",
1088 TW_CL_SEVERITY_ERROR_STRING,
1089 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1091 "FALSE Passthru timeout!",
1093 #endif /* TW_OSL_DEBUG */
1094 error = 0; /* False error */
1097 if (!(tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
1099 tw_osli_printf(sc, "request = %p",
1100 TW_CL_SEVERITY_ERROR_STRING,
1101 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1103 "Passthru request timed out!",
1105 #else /* TW_OSL_DEBUG */
1106 device_printf((sc)->bus_dev, "Passthru request timed out!\n");
1107 #endif /* TW_OSL_DEBUG */
1108 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
1112 end_time = tw_osl_get_local_time() + timeout;
1115 * Don't touch req after a reset. It (and any
1116 * associated data) will be
1117 * unmapped by the callback.
1121 * Either the request got completed, or we were woken up by a
1122 * signal. Calculate the new timeout, in case it was the latter.
1124 timeout = (end_time - tw_osl_get_local_time());
1125 } /* End of while loop */
1127 /* If there was a payload, copy it back. */
1128 if ((!error) && (req->length))
1129 if ((error = copyout(req->data, user_buf->pdata,
1130 user_buf->driver_pkt.buffer_length)))
1131 tw_osli_printf(sc, "error = %d",
1132 TW_CL_SEVERITY_ERROR_STRING,
1133 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1135 "Could not copyout fw_passthru data_buf",
1140 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1143 user_buf->driver_pkt.os_status = error;
1144 /* Free resources. */
1146 free(req->data, TW_OSLI_MALLOC_CLASS);
1147 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1154 * Function name: tw_osl_complete_passthru
1155 * Description: Called to complete passthru requests.
1157 * Input: req_handle -- ptr to request handle
1159 * Return value: None
1162 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1164 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1165 struct tw_cl_req_packet *req_pkt =
1166 (struct tw_cl_req_packet *)(&req->req_pkt);
1167 struct twa_softc *sc = req->ctlr;
1169 tw_osli_dbg_dprintf(5, sc, "entered");
1171 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1172 tw_osli_printf(sc, "request = %p, status = %d",
1173 TW_CL_SEVERITY_ERROR_STRING,
1174 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1176 "Unposted command completed!!",
1181 * Remove request from the busy queue. Just mark it complete.
1182 * There's no need to move it into the complete queue as we are
1183 * going to be done with it right now.
1185 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1186 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1188 tw_osli_unmap_request(req);
1191 * Don't do a wake up if there was an error even before the request
1192 * was sent down to the Common Layer, and we hadn't gotten an
1193 * EINPROGRESS. The request originator will then be returned an
1194 * error, and he can do the clean-up.
1196 if ((req->error_code) && (!(req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1199 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1200 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1201 /* Wake up the sleeping command originator. */
1202 tw_osli_dbg_dprintf(5, sc,
1203 "Waking up originator of request %p", req);
1204 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1208 * If the request completed even before mtx_sleep
1209 * was called, simply return.
1211 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1214 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET)
1217 tw_osli_printf(sc, "request = %p",
1218 TW_CL_SEVERITY_ERROR_STRING,
1219 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1221 "Passthru callback called, "
1222 "and caller not sleeping",
1226 tw_osli_printf(sc, "request = %p",
1227 TW_CL_SEVERITY_ERROR_STRING,
1228 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1230 "Passthru callback called for non-passthru request",
1238 * Function name: tw_osli_get_request
1239 * Description: Gets a request pkt from the free queue.
1241 * Input: sc -- ptr to OSL internal ctlr context
1243 * Return value: ptr to request pkt -- success
1246 struct tw_osli_req_context *
1247 tw_osli_get_request(struct twa_softc *sc)
1249 struct tw_osli_req_context *req;
1251 tw_osli_dbg_dprintf(4, sc, "entered");
1253 /* Get a free request packet. */
1254 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1256 /* Initialize some fields to their defaults. */
1258 req->req_handle.osl_req_ctxt = NULL;
1259 req->req_handle.cl_req_ctxt = NULL;
1260 req->req_handle.is_io = 0;
1264 req->real_data = NULL;
1265 req->real_length = 0;
1266 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1268 req->error_code = 0;
1269 req->orig_req = NULL;
1271 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1280 * Function name: twa_map_load_data_callback
1281 * Description: Callback of bus_dmamap_load for the buffer associated
1282 * with data. Updates the cmd pkt (size/sgl_entries
1283 * fields, as applicable) to reflect the number of sg
1286 * Input: arg -- ptr to OSL internal request context
1287 * segs -- ptr to a list of segment descriptors
1288 * nsegments--# of segments
1289 * error -- 0 if no errors encountered before callback,
1290 * non-zero if errors were encountered
1292 * Return value: None
1295 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1296 TW_INT32 nsegments, TW_INT32 error)
1298 struct tw_osli_req_context *req =
1299 (struct tw_osli_req_context *)arg;
1300 struct twa_softc *sc = req->ctlr;
1301 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1303 tw_osli_dbg_dprintf(10, sc, "entered");
1305 if (error == EINVAL) {
1306 req->error_code = error;
1310 /* Mark the request as currently being processed. */
1311 req->state = TW_OSLI_REQ_STATE_BUSY;
1312 /* Move the request into the busy queue. */
1313 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1315 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1317 if (error == EFBIG) {
1318 req->error_code = error;
1322 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1323 struct tw_cl_passthru_req_packet *pt_req;
1325 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1326 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1327 BUS_DMASYNC_PREREAD);
1329 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1331 * If we're using an alignment buffer, and we're
1332 * writing data, copy the real data out.
1334 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1335 bcopy(req->real_data, req->data, req->real_length);
1336 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1337 BUS_DMASYNC_PREWRITE);
1340 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1341 pt_req->sg_list = (TW_UINT8 *)segs;
1342 pt_req->sgl_entries += (nsegments - 1);
1343 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1344 &(req->req_handle));
1346 struct tw_cl_scsi_req_packet *scsi_req;
1348 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1349 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1350 BUS_DMASYNC_PREREAD);
1352 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1354 * If we're using an alignment buffer, and we're
1355 * writing data, copy the real data out.
1357 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1358 bcopy(req->real_data, req->data, req->real_length);
1359 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1360 BUS_DMASYNC_PREWRITE);
1363 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1364 scsi_req->sg_list = (TW_UINT8 *)segs;
1365 scsi_req->sgl_entries += (nsegments - 1);
1366 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1367 &(req->req_handle));
1372 req->error_code = error;
1373 req_pkt->tw_osl_callback(&(req->req_handle));
1375 * If the caller had been returned EINPROGRESS, and he has
1376 * registered a callback for handling completion, the callback
1377 * will never get called because we were unable to submit the
1378 * request. So, free up the request right here.
1380 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1381 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1388 * Function name: twa_map_load_callback
1389 * Description: Callback of bus_dmamap_load for the buffer associated
1392 * Input: arg -- ptr to variable to hold phys addr
1393 * segs -- ptr to a list of segment descriptors
1394 * nsegments--# of segments
1395 * error -- 0 if no errors encountered before callback,
1396 * non-zero if errors were encountered
1398 * Return value: None
1401 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1402 TW_INT32 nsegments, TW_INT32 error)
1404 *((bus_addr_t *)arg) = segs[0].ds_addr;
1410 * Function name: tw_osli_map_request
1411 * Description: Maps a cmd pkt and data associated with it, into
1414 * Input: req -- ptr to request pkt
1416 * Return value: 0 -- success
1417 * non-zero-- failure
1420 tw_osli_map_request(struct tw_osli_req_context *req)
1422 struct twa_softc *sc = req->ctlr;
1425 tw_osli_dbg_dprintf(10, sc, "entered");
1427 /* If the command involves data, map that too. */
1428 if (req->data != NULL) {
1430 * It's sufficient for the data pointer to be 4-byte aligned
1431 * to work with 9000. However, if 4-byte aligned addresses
1432 * are passed to bus_dmamap_load, we can get back sg elements
1433 * that are not 512-byte multiples in size. So, we will let
1434 * only those buffers that are 512-byte aligned to pass
1435 * through, and bounce the rest, so as to make sure that we
1436 * always get back sg elements that are 512-byte multiples
1439 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1440 (req->length % sc->sg_size_factor)) {
1441 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1442 /* Save original data pointer and length. */
1443 req->real_data = req->data;
1444 req->real_length = req->length;
1445 req->length = (req->length +
1446 (sc->sg_size_factor - 1)) &
1447 ~(sc->sg_size_factor - 1);
1448 req->data = malloc(req->length, TW_OSLI_MALLOC_CLASS,
1450 if (req->data == NULL) {
1451 tw_osli_printf(sc, "error = %d",
1452 TW_CL_SEVERITY_ERROR_STRING,
1453 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1455 "Failed to allocate memory "
1456 "for bounce buffer",
1458 /* Restore original data pointer and length. */
1459 req->data = req->real_data;
1460 req->length = req->real_length;
1466 * Map the data buffer into bus space and build the SG list.
1468 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1469 /* Lock against multiple simultaneous ioctl calls. */
1470 mtx_lock_spin(sc->io_lock);
1471 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1472 req->data, req->length,
1473 twa_map_load_data_callback, req,
1475 mtx_unlock_spin(sc->io_lock);
1476 } else if (req->flags & TW_OSLI_REQ_FLAGS_CCB) {
1477 error = bus_dmamap_load_ccb(sc->dma_tag, req->dma_map,
1478 req->orig_req, twa_map_load_data_callback, req,
1482 * There's only one CAM I/O thread running at a time.
1483 * So, there's no need to hold the io_lock.
1485 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1486 req->data, req->length,
1487 twa_map_load_data_callback, req,
1492 error = req->error_code;
1494 if (error == EINPROGRESS) {
1496 * Specifying sc->io_lock as the lockfuncarg
1497 * in ...tag_create should protect the access
1498 * of ...FLAGS_MAPPED from the callback.
1500 mtx_lock_spin(sc->io_lock);
1501 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED))
1502 req->flags |= TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1503 tw_osli_disallow_new_requests(sc, &(req->req_handle));
1504 mtx_unlock_spin(sc->io_lock);
1507 tw_osli_printf(sc, "error = %d",
1508 TW_CL_SEVERITY_ERROR_STRING,
1509 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1511 "Failed to map DMA memory "
1514 req->flags |= TW_OSLI_REQ_FLAGS_FAILED;
1515 /* Free alignment buffer if it was used. */
1517 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1518 free(req->data, TW_OSLI_MALLOC_CLASS);
1520 * Restore original data pointer
1523 req->data = req->real_data;
1524 req->length = req->real_length;
1530 /* Mark the request as currently being processed. */
1531 req->state = TW_OSLI_REQ_STATE_BUSY;
1532 /* Move the request into the busy queue. */
1533 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1534 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1535 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1536 &(req->req_pkt), &(req->req_handle));
1538 error = tw_cl_start_io(&sc->ctlr_handle,
1539 &(req->req_pkt), &(req->req_handle));
1541 req->error_code = error;
1542 req->req_pkt.tw_osl_callback(&(req->req_handle));
1551 * Function name: tw_osli_unmap_request
1552 * Description: Undoes the mapping done by tw_osli_map_request.
1554 * Input: req -- ptr to request pkt
1556 * Return value: None
1559 tw_osli_unmap_request(struct tw_osli_req_context *req)
1561 struct twa_softc *sc = req->ctlr;
1563 tw_osli_dbg_dprintf(10, sc, "entered");
1565 /* If the command involved data, unmap that too. */
1566 if (req->data != NULL) {
1567 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1568 /* Lock against multiple simultaneous ioctl calls. */
1569 mtx_lock_spin(sc->io_lock);
1571 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1572 bus_dmamap_sync(sc->ioctl_tag,
1573 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1576 * If we are using a bounce buffer, and we are
1577 * reading data, copy the real data in.
1579 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1580 bcopy(req->data, req->real_data,
1584 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1585 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1586 BUS_DMASYNC_POSTWRITE);
1588 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1590 mtx_unlock_spin(sc->io_lock);
1592 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1593 bus_dmamap_sync(sc->dma_tag,
1594 req->dma_map, BUS_DMASYNC_POSTREAD);
1597 * If we are using a bounce buffer, and we are
1598 * reading data, copy the real data in.
1600 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1601 bcopy(req->data, req->real_data,
1604 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1605 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1606 BUS_DMASYNC_POSTWRITE);
1608 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1612 /* Free alignment buffer if it was used. */
1613 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1614 free(req->data, TW_OSLI_MALLOC_CLASS);
1615 /* Restore original data pointer and length. */
1616 req->data = req->real_data;
1617 req->length = req->real_length;
1625 TW_VOID twa_report_stats(TW_VOID);
1626 TW_VOID twa_reset_stats(TW_VOID);
1627 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1628 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1632 * Function name: twa_report_stats
1633 * Description: For being called from ddb. Calls functions that print
1634 * OSL and CL internal stats for the controller.
1638 * Return value: None
1641 twa_report_stats(TW_VOID)
1643 struct twa_softc *sc;
1646 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1647 tw_osli_print_ctlr_stats(sc);
1648 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1655 * Function name: tw_osli_print_ctlr_stats
1656 * Description: For being called from ddb. Prints OSL controller stats
1658 * Input: sc -- ptr to OSL internal controller context
1660 * Return value: None
1663 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1665 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1666 twa_printf(sc, "OSLq type current max\n");
1667 twa_printf(sc, "free %04d %04d\n",
1668 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1669 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1670 twa_printf(sc, "busy %04d %04d\n",
1671 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1672 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1678 * Function name: twa_print_req_info
1679 * Description: For being called from ddb. Calls functions that print
1680 * OSL and CL internal details for the request.
1682 * Input: req -- ptr to OSL internal request context
1684 * Return value: None
1687 twa_print_req_info(struct tw_osli_req_context *req)
1689 struct twa_softc *sc = req->ctlr;
1691 twa_printf(sc, "OSL details for request:\n");
1692 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1693 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1694 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1695 "next_req = %p, prev_req = %p, dma_map = %p\n",
1696 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1697 req->data, req->length, req->real_data, req->real_length,
1698 req->state, req->flags, req->error_code, req->orig_req,
1699 req->link.next, req->link.prev, req->dma_map);
1700 tw_cl_print_req_info(&(req->req_handle));
1706 * Function name: twa_reset_stats
1707 * Description: For being called from ddb.
1708 * Resets some OSL controller stats.
1712 * Return value: None
1715 twa_reset_stats(TW_VOID)
1717 struct twa_softc *sc;
1720 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1721 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1722 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1723 tw_cl_reset_stats(&sc->ctlr_handle);
1727 #endif /* TW_OSL_DEBUG */