2 * Copyright (c) 2004-05 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
40 * FreeBSD specific functions not related to CAM, and other
41 * miscellaneous functions.
45 #include <dev/twa/tw_osl_includes.h>
46 #include <dev/twa/tw_cl_fwif.h>
47 #include <dev/twa/tw_cl_ioctl.h>
48 #include <dev/twa/tw_osl_ioctl.h>
51 TW_INT32 TW_DEBUG_LEVEL_FOR_OSL = TW_OSL_DEBUG;
52 TW_INT32 TW_OSL_DEBUG_LEVEL_FOR_CL = TW_OSL_DEBUG;
53 #endif /* TW_OSL_DEBUG */
55 MALLOC_DEFINE(TW_OSLI_MALLOC_CLASS, "twa_commands", "twa commands");
58 static d_open_t twa_open;
59 static d_close_t twa_close;
60 static d_ioctl_t twa_ioctl;
62 static struct cdevsw twa_cdevsw = {
63 .d_version = D_VERSION,
70 static devclass_t twa_devclass;
74 * Function name: twa_open
75 * Description: Called when the controller is opened.
76 * Simply marks the controller as open.
78 * Input: dev -- control device corresponding to the ctlr
79 * flags -- mode of open
80 * fmt -- device type (character/block etc.)
81 * proc -- current process
83 * Return value: 0 -- success
87 twa_open(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, d_thread_t *proc)
89 TW_INT32 unit = minor(dev);
90 struct twa_softc *sc = devclass_get_softc(twa_devclass, unit);
92 tw_osli_dbg_dprintf(5, sc, "entered");
93 sc->state |= TW_OSLI_CTLR_STATE_OPEN;
100 * Function name: twa_close
101 * Description: Called when the controller is closed.
102 * Simply marks the controller as not open.
104 * Input: dev -- control device corresponding to the ctlr
105 * flags -- mode of corresponding open
106 * fmt -- device type (character/block etc.)
107 * proc -- current process
109 * Return value: 0 -- success
113 twa_close(struct cdev *dev, TW_INT32 flags, TW_INT32 fmt, d_thread_t *proc)
115 TW_INT32 unit = minor(dev);
116 struct twa_softc *sc = devclass_get_softc(twa_devclass, unit);
118 tw_osli_dbg_dprintf(5, sc, "entered");
119 sc->state &= ~TW_OSLI_CTLR_STATE_OPEN;
126 * Function name: twa_ioctl
127 * Description: Called when an ioctl is posted to the controller.
128 * Handles any OS Layer specific cmds, passes the rest
129 * on to the Common Layer.
131 * Input: dev -- control device corresponding to the ctlr
133 * buf -- ptr to buffer in kernel memory, which is
134 * a copy of the input buffer in user-space
135 * flags -- mode of corresponding open
136 * proc -- current process
137 * Output: buf -- ptr to buffer in kernel memory, which will
138 * be copied to the output buffer in user-space
139 * Return value: 0 -- success
143 twa_ioctl(struct cdev *dev, u_long cmd, caddr_t buf, TW_INT32 flags, d_thread_t *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 #ifdef TW_OSLI_DEFERRED_INTR_USED
179 static TW_VOID twa_deferred_intr(TW_VOID *context, TW_INT32 pending);
180 #endif /* TW_OSLI_DEFERRED_INTR_USED */
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);
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);
246 * Function name: twa_attach
247 * Description: Allocates pci resources; updates sc; adds a node to the
248 * sysctl tree to expose the driver version; makes calls
249 * (to the Common Layer) to initialize ctlr, and to
252 * Input: dev -- bus device corresponding to the ctlr
254 * Return value: 0 -- success
258 twa_attach(device_t dev)
260 struct twa_softc *sc = device_get_softc(dev);
263 TW_INT32 bar0_offset;
267 tw_osli_dbg_dprintf(3, sc, "entered");
269 sc->ctlr_handle.osl_ctlr_ctxt = sc;
271 /* Initialize the softc structure. */
273 sc->device_id = pci_get_device(dev);
275 /* Initialize the mutexes right here. */
276 sc->io_lock = &(sc->io_lock_handle);
277 mtx_init(sc->io_lock, "tw_osl_io_lock", NULL, MTX_SPIN);
278 sc->q_lock = &(sc->q_lock_handle);
279 mtx_init(sc->q_lock, "tw_osl_q_lock", NULL, MTX_SPIN);
281 sysctl_ctx_init(&sc->sysctl_ctxt);
282 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt,
283 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
284 device_get_nameunit(dev), CTLFLAG_RD, 0, "");
285 if (sc->sysctl_tree == NULL) {
286 tw_osli_printf(sc, "error = %d",
287 TW_CL_SEVERITY_ERROR_STRING,
288 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
290 "Cannot add sysctl tree node",
294 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree),
295 OID_AUTO, "driver_version", CTLFLAG_RD,
296 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version");
298 /* Make sure we are going to be able to talk to this board. */
299 command = pci_read_config(dev, PCIR_COMMAND, 2);
300 if ((command & PCIM_CMD_PORTEN) == 0) {
301 tw_osli_printf(sc, "error = %d",
302 TW_CL_SEVERITY_ERROR_STRING,
303 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
305 "Register window not available",
307 tw_osli_free_resources(sc);
311 /* Force the busmaster enable bit on, in case the BIOS forgot. */
312 command |= PCIM_CMD_BUSMASTEREN;
313 pci_write_config(dev, PCIR_COMMAND, command, 2);
315 /* Allocate the PCI register window. */
316 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM,
317 &bar_num, &bar0_offset, &bar_size))) {
318 tw_osli_printf(sc, "error = %d",
319 TW_CL_SEVERITY_ERROR_STRING,
320 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
322 "Can't get PCI BAR info",
324 tw_osli_free_resources(sc);
327 sc->reg_res_id = PCIR_BARS + bar0_offset;
328 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
329 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE))
331 tw_osli_printf(sc, "error = %d",
332 TW_CL_SEVERITY_ERROR_STRING,
333 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
335 "Can't allocate register window",
337 tw_osli_free_resources(sc);
340 sc->bus_tag = rman_get_bustag(sc->reg_res);
341 sc->bus_handle = rman_get_bushandle(sc->reg_res);
343 /* Allocate and register our interrupt. */
345 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ,
346 &(sc->irq_res_id), 0, ~0, 1,
347 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
348 tw_osli_printf(sc, "error = %d",
349 TW_CL_SEVERITY_ERROR_STRING,
350 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
352 "Can't allocate interrupt",
354 tw_osli_free_resources(sc);
357 if ((error = bus_setup_intr(sc->bus_dev, sc->irq_res,
358 ((mp_ncpus > 1) ? (INTR_MPSAFE
359 #ifdef TW_OSLI_DEFERRED_INTR_USED
361 #endif /* TW_OSLI_DEFERRED_INTR_USED */
362 ) : 0) | INTR_TYPE_CAM,
363 twa_pci_intr, sc, &sc->intr_handle))) {
364 tw_osli_printf(sc, "error = %d",
365 TW_CL_SEVERITY_ERROR_STRING,
366 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
368 "Can't set up interrupt",
370 tw_osli_free_resources(sc);
374 #ifdef TW_OSLI_DEFERRED_INTR_USED
375 TASK_INIT(&sc->deferred_intr_callback, 0, twa_deferred_intr, sc);
376 #endif /* TW_OSLI_DEFERRED_INTR_USED */
378 if ((error = tw_osli_alloc_mem(sc))) {
379 tw_osli_printf(sc, "error = %d",
380 TW_CL_SEVERITY_ERROR_STRING,
381 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
383 "Memory allocation failure",
385 tw_osli_free_resources(sc);
389 /* Initialize the Common Layer for this controller. */
390 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id,
391 TW_OSLI_MAX_NUM_IOS, TW_OSLI_MAX_NUM_AENS,
392 sc->non_dma_mem, sc->dma_mem,
394 #ifdef TW_OSL_FLASH_FIRMWARE
395 , sc->flash_dma_mem, sc->flash_dma_mem_phys
396 #endif /* TW_OSL_FLASH_FIRMWARE */
398 tw_osli_printf(sc, "error = %d",
399 TW_CL_SEVERITY_ERROR_STRING,
400 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
402 "Failed to initialize Common Layer/controller",
404 tw_osli_free_resources(sc);
408 #ifdef TW_OSL_FLASH_FIRMWARE
409 /* Free any memory allocated for firmware flashing. */
410 if (sc->flash_dma_mem) {
411 bus_dmamap_unload(sc->flash_tag, sc->flash_map);
412 bus_dmamem_free(sc->flash_tag, sc->flash_dma_mem,
416 bus_dma_tag_destroy(sc->flash_tag);
418 * Set flash_tag and flash_dma_mem to 0, so we don't try freeing them
422 sc->flash_dma_mem = 0;
423 #endif /* TW_OSL_FLASH_FIRMWARE */
425 /* Create the control device. */
426 sc->ctrl_dev = make_dev(&twa_cdevsw, device_get_unit(sc->bus_dev),
427 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
428 "twa%d", device_get_unit(sc->bus_dev));
429 sc->ctrl_dev->si_drv1 = sc;
431 if ((error = tw_osli_cam_attach(sc))) {
432 tw_osli_free_resources(sc);
433 tw_osli_printf(sc, "error = %d",
434 TW_CL_SEVERITY_ERROR_STRING,
435 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
437 "Failed to initialize CAM",
448 * Function name: tw_osli_alloc_mem
449 * Description: Allocates memory needed both by CL and OSL.
451 * Input: sc -- OSL internal controller context
453 * Return value: 0 -- success
457 tw_osli_alloc_mem(struct twa_softc *sc)
459 struct tw_osli_req_context *req;
460 TW_UINT32 max_sg_elements;
461 TW_UINT32 non_dma_mem_size;
462 TW_UINT32 dma_mem_size;
463 #ifdef TW_OSL_FLASH_FIRMWARE
464 TW_UINT32 flash_dma_mem_size;
465 #endif /* TW_OSL_FLASH_FIRMWARE */
466 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
467 TW_UINT32 per_req_dma_mem_size;
468 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
472 tw_osli_dbg_dprintf(3, sc, "entered");
474 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0;
475 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0;
476 #ifdef TW_OSL_FLASH_FIRMWARE
477 sc->flags |= TW_CL_FLASH_FIRMWARE;
478 #endif /* TW_OSL_FLASH_FIRMWARE */
479 #ifdef TW_OSLI_DEFERRED_INTR_USED
480 sc->flags |= TW_CL_DEFERRED_INTR_USED;
481 #endif /* TW_OSLI_DEFERRED_INTR_USED */
483 max_sg_elements = (sizeof(bus_addr_t) == 8) ?
484 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS;
486 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags,
487 sc->device_id, TW_OSLI_MAX_NUM_IOS, TW_OSLI_MAX_NUM_AENS,
488 &(sc->alignment), &(sc->sg_size_factor),
489 &non_dma_mem_size, &dma_mem_size
490 #ifdef TW_OSL_FLASH_FIRMWARE
491 , &flash_dma_mem_size
492 #endif /* TW_OSL_FLASH_FIRMWARE */
493 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
494 , &per_req_dma_mem_size
495 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
497 tw_osli_printf(sc, "error = %d",
498 TW_CL_SEVERITY_ERROR_STRING,
499 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
501 "Can't get Common Layer's memory requirements",
506 if ((sc->non_dma_mem = malloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS,
507 M_WAITOK)) == NULL) {
508 tw_osli_printf(sc, "error = %d",
509 TW_CL_SEVERITY_ERROR_STRING,
510 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
512 "Can't allocate non-dma memory",
517 /* Create the parent dma tag. */
518 if (bus_dma_tag_create(NULL, /* parent */
519 sc->alignment, /* alignment */
521 BUS_SPACE_MAXADDR, /* lowaddr */
522 BUS_SPACE_MAXADDR, /* highaddr */
523 NULL, NULL, /* filter, filterarg */
524 TW_CL_MAX_IO_SIZE, /* maxsize */
525 max_sg_elements, /* nsegments */
526 TW_CL_MAX_IO_SIZE, /* maxsegsize */
529 NULL, /* lockfuncarg */
530 &sc->parent_tag /* tag */)) {
531 tw_osli_printf(sc, "error = %d",
532 TW_CL_SEVERITY_ERROR_STRING,
533 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
535 "Can't allocate parent DMA tag",
540 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */
541 if (bus_dma_tag_create(sc->parent_tag, /* parent */
542 sc->alignment, /* alignment */
544 BUS_SPACE_MAXADDR, /* lowaddr */
545 BUS_SPACE_MAXADDR, /* highaddr */
546 NULL, NULL, /* filter, filterarg */
547 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
548 (TW_OSLI_MAX_NUM_IOS *
549 per_req_dma_mem_size) +
550 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
551 dma_mem_size, /* maxsize */
553 BUS_SPACE_MAXSIZE, /* maxsegsize */
556 NULL, /* lockfuncarg */
557 &sc->cmd_tag /* tag */)) {
558 tw_osli_printf(sc, "error = %d",
559 TW_CL_SEVERITY_ERROR_STRING,
560 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
562 "Can't allocate DMA tag for Common Layer's "
568 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
569 BUS_DMA_NOWAIT, &sc->cmd_map)) {
570 /* Try a second time. */
571 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem,
572 BUS_DMA_NOWAIT, &sc->cmd_map)) {
573 tw_osli_printf(sc, "error = %d",
574 TW_CL_SEVERITY_ERROR_STRING,
575 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
577 "Can't allocate DMA'able memory for the"
584 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem,
585 dma_mem_size, twa_map_load_callback,
586 &sc->dma_mem_phys, 0);
589 #ifdef TW_OSL_FLASH_FIRMWARE
591 * Create a dma tag for Common Layer's DMA'able memory,
592 * used to flash firmware (flash_dma_mem).
594 if (bus_dma_tag_create(sc->parent_tag, /* parent */
595 sc->alignment, /* alignment */
597 BUS_SPACE_MAXADDR, /* lowaddr */
598 BUS_SPACE_MAXADDR, /* highaddr */
599 NULL, NULL, /* filter, filterarg */
600 flash_dma_mem_size, /* maxsize */
602 flash_dma_mem_size, /* maxsegsize */
605 NULL, /* lockfuncarg */
606 &sc->flash_tag /* tag */)) {
607 tw_osli_printf(sc, "error = %d",
608 TW_CL_SEVERITY_ERROR_STRING,
609 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
611 "Can't allocate DMA tag for Common Layer's "
612 "firmware flash memory",
617 if (bus_dmamem_alloc(sc->flash_tag, &sc->flash_dma_mem,
618 BUS_DMA_NOWAIT, &sc->flash_map)) {
619 tw_osli_printf(sc, "error = %d",
620 TW_CL_SEVERITY_ERROR_STRING,
621 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
623 "Can't allocate DMA'able memory for Common Layer's "
629 bus_dmamap_load(sc->flash_tag, sc->flash_map, sc->flash_dma_mem,
630 flash_dma_mem_size, twa_map_load_callback,
631 &sc->flash_dma_mem_phys, 0);
633 #endif /* TW_OSL_FLASH_FIRMWARE */
636 * Create a dma tag for data buffers; size will be the maximum
637 * possible I/O size (128kB).
639 if (bus_dma_tag_create(sc->parent_tag, /* parent */
640 sc->alignment, /* alignment */
642 BUS_SPACE_MAXADDR, /* lowaddr */
643 BUS_SPACE_MAXADDR, /* highaddr */
644 NULL, NULL, /* filter, filterarg */
645 TW_CL_MAX_IO_SIZE, /* maxsize */
646 max_sg_elements, /* nsegments */
647 TW_CL_MAX_IO_SIZE, /* maxsegsize */
648 BUS_DMA_ALLOCNOW, /* flags */
649 twa_busdma_lock, /* lockfunc */
650 sc->io_lock, /* lockfuncarg */
651 &sc->dma_tag /* tag */)) {
652 tw_osli_printf(sc, "error = %d",
653 TW_CL_SEVERITY_ERROR_STRING,
654 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
656 "Can't allocate DMA tag for data buffers",
662 * Create a dma tag for ioctl data buffers; size will be the maximum
663 * possible I/O size (128kB).
665 if (bus_dma_tag_create(sc->parent_tag, /* parent */
666 sc->alignment, /* alignment */
668 BUS_SPACE_MAXADDR, /* lowaddr */
669 BUS_SPACE_MAXADDR, /* highaddr */
670 NULL, NULL, /* filter, filterarg */
671 TW_CL_MAX_IO_SIZE, /* maxsize */
672 max_sg_elements, /* nsegments */
673 TW_CL_MAX_IO_SIZE, /* maxsegsize */
674 BUS_DMA_ALLOCNOW, /* flags */
675 twa_busdma_lock, /* lockfunc */
676 sc->io_lock, /* lockfuncarg */
677 &sc->ioctl_tag /* tag */)) {
678 tw_osli_printf(sc, "error = %d",
679 TW_CL_SEVERITY_ERROR_STRING,
680 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
682 "Can't allocate DMA tag for ioctl data buffers",
687 /* Create just one map for all ioctl request data buffers. */
688 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) {
689 tw_osli_printf(sc, "error = %d",
690 TW_CL_SEVERITY_ERROR_STRING,
691 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
693 "Can't create ioctl map",
699 /* Initialize request queues. */
700 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q);
701 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q);
703 if ((sc->req_ctxt_buf = (struct tw_osli_req_context *)
704 malloc((sizeof(struct tw_osli_req_context) *
705 TW_OSLI_MAX_NUM_IOS),
706 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
707 tw_osli_printf(sc, "error = %d",
708 TW_CL_SEVERITY_ERROR_STRING,
709 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
711 "Failed to allocate request packets",
715 bzero(sc->req_ctxt_buf,
716 sizeof(struct tw_osli_req_context) * TW_OSLI_MAX_NUM_IOS);
718 for (i = 0; i < TW_OSLI_MAX_NUM_IOS; i++) {
719 req = &(sc->req_ctxt_buf[i]);
721 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) {
722 tw_osli_printf(sc, "request # = %d, error = %d",
723 TW_CL_SEVERITY_ERROR_STRING,
724 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
726 "Can't create dma map",
731 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
733 req->req_pkt.dma_mem = ((TW_INT8 *)(sc->dma_mem)) +
734 (i * per_req_dma_mem_size);
735 req->req_pkt.dma_mem_phys = sc->dma_mem_phys +
736 (i * per_req_dma_mem_size);
738 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
740 /* Insert request into the free queue. */
741 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
744 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
746 sc->dma_mem = ((TW_INT8 *)(sc->dma_mem)) +
747 (TW_OSLI_MAX_NUM_IOS * per_req_dma_mem_size);
748 sc->dma_mem_phys += (TW_OSLI_MAX_NUM_IOS * per_req_dma_mem_size);
750 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
758 * Function name: tw_osli_free_resources
759 * Description: Performs clean-up at the time of going down.
761 * Input: sc -- ptr to OSL internal ctlr context
766 tw_osli_free_resources(struct twa_softc *sc)
768 struct tw_osli_req_context *req;
771 tw_osli_dbg_dprintf(3, sc, "entered");
773 /* Detach from CAM */
774 tw_osli_cam_detach(sc);
776 if (sc->req_ctxt_buf)
777 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) !=
779 if ((error = bus_dmamap_destroy(sc->dma_tag,
781 tw_osli_dbg_dprintf(1, sc,
782 "dmamap_destroy(dma) returned %d",
785 if ((sc->ioctl_tag) && (sc->ioctl_map))
786 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map)))
787 tw_osli_dbg_dprintf(1, sc,
788 "dmamap_destroy(ioctl) returned %d", error);
790 /* Free all memory allocated so far. */
791 if (sc->req_ctxt_buf)
792 free(sc->req_ctxt_buf, TW_OSLI_MALLOC_CLASS);
795 free(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS);
798 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map);
799 bus_dmamem_free(sc->cmd_tag, sc->dma_mem,
803 if ((error = bus_dma_tag_destroy(sc->cmd_tag)))
804 tw_osli_dbg_dprintf(1, sc,
805 "dma_tag_destroy(cmd) returned %d", error);
808 #ifdef TW_OSL_FLASH_FIRMWARE
810 if (sc->flash_dma_mem) {
811 /* In case this piece of memory has already been freed. */
812 bus_dmamap_unload(sc->flash_tag, sc->flash_map);
813 bus_dmamem_free(sc->flash_tag, sc->flash_dma_mem,
817 if ((error = bus_dma_tag_destroy(sc->flash_tag)))
818 tw_osli_dbg_dprintf(1, sc,
819 "dma_tag_destroy(flash) returned %d", error);
821 #endif /* TW_OSL_FLASH_FIRMWARE */
824 if ((error = bus_dma_tag_destroy(sc->dma_tag)))
825 tw_osli_dbg_dprintf(1, sc,
826 "dma_tag_destroy(dma) returned %d", error);
829 if ((error = bus_dma_tag_destroy(sc->ioctl_tag)))
830 tw_osli_dbg_dprintf(1, sc,
831 "dma_tag_destroy(ioctl) returned %d", error);
834 if ((error = bus_dma_tag_destroy(sc->parent_tag)))
835 tw_osli_dbg_dprintf(1, sc,
836 "dma_tag_destroy(parent) returned %d", error);
839 /* Disconnect the interrupt handler. */
841 if ((error = bus_teardown_intr(sc->bus_dev,
842 sc->irq_res, sc->intr_handle)))
843 tw_osli_dbg_dprintf(1, sc,
844 "teardown_intr returned %d", error);
846 if (sc->irq_res != NULL)
847 if ((error = bus_release_resource(sc->bus_dev,
848 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res)))
849 tw_osli_dbg_dprintf(1, sc,
850 "release_resource(irq) returned %d", error);
853 /* Release the register window mapping. */
854 if (sc->reg_res != NULL)
855 if ((error = bus_release_resource(sc->bus_dev,
856 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res)))
857 tw_osli_dbg_dprintf(1, sc,
858 "release_resource(io) returned %d", error);
861 /* Destroy the control device. */
862 if (sc->ctrl_dev != (struct cdev *)NULL)
863 destroy_dev(sc->ctrl_dev);
865 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt)))
866 tw_osli_dbg_dprintf(1, sc,
867 "sysctl_ctx_free returned %d", error);
874 * Function name: twa_detach
875 * Description: Called when the controller is being detached from
878 * Input: dev -- bus device corresponding to the ctlr
880 * Return value: 0 -- success
884 twa_detach(device_t dev)
886 struct twa_softc *sc = device_get_softc(dev);
889 tw_osli_dbg_dprintf(3, sc, "entered");
892 if (sc->state & TW_OSLI_CTLR_STATE_OPEN) {
893 tw_osli_printf(sc, "error = %d",
894 TW_CL_SEVERITY_ERROR_STRING,
895 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
902 /* Shut the controller down. */
903 if ((error = twa_shutdown(dev)))
906 /* Free all resources associated with this controller. */
907 tw_osli_free_resources(sc);
917 * Function name: twa_shutdown
918 * Description: Called at unload/shutdown time. Lets the controller
919 * know that we are going down.
921 * Input: dev -- bus device corresponding to the ctlr
923 * Return value: 0 -- success
927 twa_shutdown(device_t dev)
929 struct twa_softc *sc = device_get_softc(dev);
932 tw_osli_dbg_dprintf(3, sc, "entered");
934 /* Disconnect from the controller. */
935 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) {
936 tw_osli_printf(sc, "error = %d",
937 TW_CL_SEVERITY_ERROR_STRING,
938 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
940 "Failed to shutdown Common Layer/controller",
949 * Function name: twa_busdma_lock
950 * Description: Function to provide synchronization during busdma_swi.
952 * Input: lock_arg -- lock mutex sent as argument
953 * op -- operation (lock/unlock) expected of the function
958 twa_busdma_lock(TW_VOID *lock_arg, bus_dma_lock_op_t op)
962 lock = (struct mtx *)lock_arg;
969 mtx_unlock_spin(lock);
973 panic("Unknown operation 0x%x for twa_busdma_lock!", op);
980 * Function name: twa_pci_intr
981 * Description: Interrupt handler. Wrapper for twa_interrupt.
983 * Input: arg -- ptr to OSL internal ctlr context
988 twa_pci_intr(TW_VOID *arg)
990 struct twa_softc *sc = (struct twa_softc *)arg;
992 tw_osli_dbg_dprintf(10, sc, "entered");
993 if (tw_cl_interrupt(&(sc->ctlr_handle)))
994 #ifdef TW_OSLI_DEFERRED_INTR_USED
995 taskqueue_enqueue_fast(taskqueue_fast,
996 &(sc->deferred_intr_callback));
997 #else /* TW_OSLI_DEFERRED_INTR_USED */
998 tw_cl_deferred_interrupt(&(sc->ctlr_handle));
999 #endif /* TW_OSLI_DEFERRED_INTR_USED */
1004 #ifdef TW_OSLI_DEFERRED_INTR_USED
1007 * Function name: twa_deferred_intr
1008 * Description: Deferred interrupt handler.
1010 * Input: context -- ptr to OSL internal ctlr context
1011 * pending -- not used
1013 * Return value: None
1016 twa_deferred_intr(TW_VOID *context, TW_INT32 pending)
1018 struct twa_softc *sc = (struct twa_softc *)context;
1020 tw_osli_dbg_dprintf(10, sc, "entered");
1022 tw_cl_deferred_interrupt(&(sc->ctlr_handle));
1025 #endif /* TW_OSLI_DEFERRED_INTR_USED */
1030 * Function name: tw_osli_fw_passthru
1031 * Description: Builds a fw passthru cmd pkt, and submits it to CL.
1033 * Input: sc -- ptr to OSL internal ctlr context
1034 * buf -- ptr to ioctl pkt understood by CL
1036 * Return value: 0 -- success
1037 * non-zero-- failure
1040 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf)
1042 struct tw_osli_req_context *req;
1043 struct tw_osli_ioctl_no_data_buf *user_buf =
1044 (struct tw_osli_ioctl_no_data_buf *)buf;
1046 TW_UINT32 timeout = 60;
1047 TW_UINT32 data_buf_size_adjusted;
1048 struct tw_cl_req_packet *req_pkt;
1049 struct tw_cl_passthru_req_packet *pt_req;
1052 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru");
1054 if ((req = tw_osli_get_request(sc)) == NULL)
1057 req->req_handle.osl_req_ctxt = req;
1058 req->orig_req = buf;
1059 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU;
1061 req_pkt = &(req->req_pkt);
1062 req_pkt->status = 0;
1063 req_pkt->tw_osl_callback = tw_osl_complete_passthru;
1064 /* Let the Common Layer retry the request on cmd queue full. */
1065 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY;
1067 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1069 * Make sure that the data buffer sent to firmware is a
1070 * 512 byte multiple in size.
1072 data_buf_size_adjusted =
1073 (user_buf->driver_pkt.buffer_length +
1074 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1);
1075 if ((req->length = data_buf_size_adjusted)) {
1076 if ((req->data = malloc(data_buf_size_adjusted,
1077 TW_OSLI_MALLOC_CLASS, M_WAITOK)) == NULL) {
1079 tw_osli_printf(sc, "error = %d",
1080 TW_CL_SEVERITY_ERROR_STRING,
1081 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1083 "Could not alloc mem for "
1084 "fw_passthru data_buf",
1086 goto fw_passthru_err;
1088 /* Copy the payload. */
1089 if ((error = copyin((TW_VOID *)(user_buf->pdata),
1091 user_buf->driver_pkt.buffer_length)) != 0) {
1092 tw_osli_printf(sc, "error = %d",
1093 TW_CL_SEVERITY_ERROR_STRING,
1094 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1096 "Could not copyin fw_passthru data_buf",
1098 goto fw_passthru_err;
1100 pt_req->sgl_entries = 1; /* will be updated during mapping */
1101 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN |
1102 TW_OSLI_REQ_FLAGS_DATA_OUT);
1104 pt_req->sgl_entries = 0; /* no payload */
1106 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt));
1107 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet);
1109 if ((error = tw_osli_map_request(req)))
1110 goto fw_passthru_err;
1112 end_time = tw_osl_get_local_time() + timeout;
1113 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) {
1114 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING;
1116 error = tsleep(req, PRIBIO, "twa_passthru", timeout * hz);
1118 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING))
1120 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1123 if (((error = req->error_code)) ||
1124 ((error = (req->state !=
1125 TW_OSLI_REQ_STATE_COMPLETE))) ||
1126 ((error = req_pkt->status)))
1127 goto fw_passthru_err;
1131 if (req_pkt->status) {
1132 error = req_pkt->status;
1133 goto fw_passthru_err;
1136 if (error == EWOULDBLOCK) {
1138 tw_osli_printf(sc, "request = %p",
1139 TW_CL_SEVERITY_ERROR_STRING,
1140 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1142 "Passthru request timed out!",
1145 * Should I check here if the timeout happened
1146 * because of yet another reset, and not do a
1149 tw_cl_reset_ctlr(&sc->ctlr_handle);
1151 * Don't touch req after a reset. It (and any
1152 * associated data) will already have been
1153 * freed by the callback. Just return.
1155 user_buf->driver_pkt.os_status = error;
1159 * Either the request got completed, or we were woken up by a
1160 * signal. Calculate the new timeout, in case it was the latter.
1162 timeout = (end_time - tw_osl_get_local_time());
1165 /* If there was a payload, copy it back. */
1166 if ((!error) && (req->length))
1167 if ((error = copyout(req->data, user_buf->pdata,
1168 user_buf->driver_pkt.buffer_length)))
1169 tw_osli_printf(sc, "error = %d",
1170 TW_CL_SEVERITY_ERROR_STRING,
1171 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1173 "Could not copyout fw_passthru data_buf",
1178 * Print the failure message. For some reason, on certain OS versions,
1179 * printing this error message during reset hangs the display (although
1180 * the rest of the system is running fine. So, don't print it if the
1181 * failure was due to a reset.
1183 if ((error) && (error != TW_CL_ERR_REQ_BUS_RESET))
1184 tw_osli_printf(sc, "error = %d",
1185 TW_CL_SEVERITY_ERROR_STRING,
1186 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1188 "Firmware passthru failed!",
1191 user_buf->driver_pkt.os_status = error;
1192 /* Free resources. */
1194 free(req->data, TW_OSLI_MALLOC_CLASS);
1195 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1202 * Function name: tw_osl_complete_passthru
1203 * Description: Called to complete passthru requests.
1205 * Input: req_handle -- ptr to request handle
1207 * Return value: None
1210 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle)
1212 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
1213 struct twa_softc *sc = req->ctlr;
1215 tw_osli_dbg_dprintf(5, sc, "entered");
1217 if (req->state != TW_OSLI_REQ_STATE_BUSY) {
1218 tw_osli_printf(sc, "request = %p, status = %d",
1219 TW_CL_SEVERITY_ERROR_STRING,
1220 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1222 "Unposted command completed!!",
1227 * Remove request from the busy queue. Just mark it complete.
1228 * There's no need to move it into the complete queue as we are
1229 * going to be done with it right now.
1231 req->state = TW_OSLI_REQ_STATE_COMPLETE;
1232 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
1234 tw_osli_unmap_request(req);
1237 * Don't do a wake up if there was an error even before the request
1238 * was sent down to the Common Layer, and we hadn't gotten an
1239 * EINPROGRESS. The request originator will then be returned an
1240 * error, and he can do the clean-up.
1242 if ((req->error_code) &&
1243 (!(req->state & TW_OSLI_REQ_FLAGS_IN_PROGRESS)))
1246 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1247 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) {
1248 /* Wake up the sleeping command originator. */
1249 tw_osli_dbg_dprintf(5, sc,
1250 "Waking up originator of request %p", req);
1251 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING;
1255 * If the request completed even before tsleep
1256 * was called, simply return.
1258 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED)
1261 tw_osli_printf(sc, "request = %p",
1262 TW_CL_SEVERITY_ERROR_STRING,
1263 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1265 "Passthru callback called, "
1266 "and caller not sleeping",
1270 tw_osli_printf(sc, "request = %p",
1271 TW_CL_SEVERITY_ERROR_STRING,
1272 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1274 "Passthru callback called for non-passthru request",
1282 * Function name: tw_osli_get_request
1283 * Description: Gets a request pkt from the free queue.
1285 * Input: sc -- ptr to OSL internal ctlr context
1287 * Return value: ptr to request pkt -- success
1290 struct tw_osli_req_context *
1291 tw_osli_get_request(struct twa_softc *sc)
1293 struct tw_osli_req_context *req;
1295 tw_osli_dbg_dprintf(4, sc, "entered");
1297 /* Get a free request packet. */
1298 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q);
1300 /* Initialize some fields to their defaults. */
1302 req->req_handle.osl_req_ctxt = NULL;
1303 req->req_handle.cl_req_ctxt = NULL;
1306 req->real_data = NULL;
1307 req->real_length = 0;
1308 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */
1310 req->error_code = 0;
1311 req->orig_req = NULL;
1313 #ifdef TW_OSL_DMA_MEM_ALLOC_PER_REQUEST
1315 /* Don't zero dma_mem & dma_mem_phys in req_pkt. */
1316 req->req_pkt.cmd = 0;
1317 req->req_pkt.flags = 0;
1318 req->req_pkt.status = 0;
1319 req->req_pkt.tw_osl_callback = NULL;
1320 bzero(&(req->req_pkt.gen_req_pkt),
1321 sizeof(req->req_pkt.gen_req_pkt));
1323 #else /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
1325 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet));
1327 #endif /* TW_OSL_DMA_MEM_ALLOC_PER_REQUEST */
1335 * Function name: twa_map_load_data_callback
1336 * Description: Callback of bus_dmamap_load for the buffer associated
1337 * with data. Updates the cmd pkt (size/sgl_entries
1338 * fields, as applicable) to reflect the number of sg
1341 * Input: arg -- ptr to OSL internal request context
1342 * segs -- ptr to a list of segment descriptors
1343 * nsegments--# of segments
1344 * error -- 0 if no errors encountered before callback,
1345 * non-zero if errors were encountered
1347 * Return value: None
1350 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1351 TW_INT32 nsegments, TW_INT32 error)
1353 struct tw_osli_req_context *req =
1354 (struct tw_osli_req_context *)arg;
1355 struct twa_softc *sc = req->ctlr;
1356 struct tw_cl_req_packet *req_pkt = &(req->req_pkt);
1358 tw_osli_dbg_dprintf(10, sc, "entered");
1360 /* Mark the request as currently being processed. */
1361 req->state = TW_OSLI_REQ_STATE_BUSY;
1362 /* Move the request into the busy queue. */
1363 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1365 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED;
1366 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1367 tw_osli_allow_new_requests(sc, (TW_VOID *)(req->orig_req));
1369 if (error == EFBIG) {
1370 req->error_code = error;
1374 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1375 struct tw_cl_passthru_req_packet *pt_req;
1377 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1378 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1379 BUS_DMASYNC_PREREAD);
1381 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1383 * If we're using an alignment buffer, and we're
1384 * writing data, copy the real data out.
1386 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1387 bcopy(req->real_data, req->data, req->real_length);
1388 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1389 BUS_DMASYNC_PREWRITE);
1392 pt_req = &(req_pkt->gen_req_pkt.pt_req);
1393 pt_req->sg_list = (TW_UINT8 *)segs;
1394 pt_req->sgl_entries += (nsegments - 1);
1395 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt,
1396 &(req->req_handle));
1398 struct tw_cl_scsi_req_packet *scsi_req;
1400 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN)
1401 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1402 BUS_DMASYNC_PREREAD);
1404 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) {
1406 * If we're using an alignment buffer, and we're
1407 * writing data, copy the real data out.
1409 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1410 bcopy(req->real_data, req->data, req->real_length);
1411 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1412 BUS_DMASYNC_PREWRITE);
1415 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
1416 scsi_req->sg_list = (TW_UINT8 *)segs;
1417 scsi_req->sgl_entries += (nsegments - 1);
1418 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt,
1419 &(req->req_handle));
1424 req->error_code = error;
1425 req_pkt->tw_osl_callback(&(req->req_handle));
1427 * If the caller had been returned EINPROGRESS, and he has
1428 * registered a callback for handling completion, the callback
1429 * will never get called because we were unable to submit the
1430 * request. So, free up the request right here.
1432 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS)
1433 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
1440 * Function name: twa_map_load_callback
1441 * Description: Callback of bus_dmamap_load for the buffer associated
1444 * Input: arg -- ptr to variable to hold phys addr
1445 * segs -- ptr to a list of segment descriptors
1446 * nsegments--# of segments
1447 * error -- 0 if no errors encountered before callback,
1448 * non-zero if errors were encountered
1450 * Return value: None
1453 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs,
1454 TW_INT32 nsegments, TW_INT32 error)
1456 *((bus_addr_t *)arg) = segs[0].ds_addr;
1462 * Function name: tw_osli_map_request
1463 * Description: Maps a cmd pkt and data associated with it, into
1466 * Input: req -- ptr to request pkt
1468 * Return value: 0 -- success
1469 * non-zero-- failure
1472 tw_osli_map_request(struct tw_osli_req_context *req)
1474 struct twa_softc *sc = req->ctlr;
1477 tw_osli_dbg_dprintf(10, sc, "entered");
1479 /* If the command involves data, map that too. */
1480 if (req->data != NULL) {
1482 * It's sufficient for the data pointer to be 4-byte aligned
1483 * to work with 9000. However, if 4-byte aligned addresses
1484 * are passed to bus_dmamap_load, we can get back sg elements
1485 * that are not 512-byte multiples in size. So, we will let
1486 * only those buffers that are 512-byte aligned to pass
1487 * through, and bounce the rest, so as to make sure that we
1488 * always get back sg elements that are 512-byte multiples
1491 if (((vm_offset_t)req->data % sc->sg_size_factor) ||
1492 (req->length % sc->sg_size_factor)) {
1493 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED;
1494 /* Save original data pointer and length. */
1495 req->real_data = req->data;
1496 req->real_length = req->length;
1497 req->length = (req->length +
1498 (sc->sg_size_factor - 1)) &
1499 ~(sc->sg_size_factor - 1);
1500 req->data = malloc(req->length, TW_OSLI_MALLOC_CLASS,
1502 if (req->data == NULL) {
1503 tw_osli_printf(sc, "error = %d",
1504 TW_CL_SEVERITY_ERROR_STRING,
1505 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
1507 "Failed to allocate memory "
1508 "for bounce buffer",
1510 /* Restore original data pointer and length. */
1511 req->data = req->real_data;
1512 req->length = req->real_length;
1518 * Map the data buffer into bus space and build the SG list.
1520 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1521 /* Lock against multiple simultaneous ioctl calls. */
1522 mtx_lock_spin(sc->io_lock);
1523 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map,
1524 req->data, req->length,
1525 twa_map_load_data_callback, req,
1527 mtx_unlock_spin(sc->io_lock);
1530 * There's only one CAM I/O thread running at a time.
1531 * So, there's no need to hold the io_lock.
1533 error = bus_dmamap_load(sc->dma_tag, req->dma_map,
1534 req->data, req->length,
1535 twa_map_load_data_callback, req,
1540 error = req->error_code;
1542 if (error == EINPROGRESS) {
1544 * Specifying sc->io_lock as the lockfuncarg
1545 * in ...tag_create should protect the access
1546 * of ...FLAGS_MAPPED from the callback.
1548 mtx_lock_spin(sc->io_lock);
1549 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED)) {
1551 TW_OSLI_REQ_FLAGS_IN_PROGRESS;
1552 tw_osli_disallow_new_requests(sc);
1554 mtx_unlock_spin(sc->io_lock);
1557 /* Free alignment buffer if it was used. */
1559 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1560 free(req->data, TW_OSLI_MALLOC_CLASS);
1562 * Restore original data pointer
1565 req->data = req->real_data;
1566 req->length = req->real_length;
1572 /* Mark the request as currently being processed. */
1573 req->state = TW_OSLI_REQ_STATE_BUSY;
1574 /* Move the request into the busy queue. */
1575 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q);
1576 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU)
1577 error = tw_cl_fw_passthru(&sc->ctlr_handle,
1578 &(req->req_pkt), &(req->req_handle));
1580 error = tw_cl_start_io(&sc->ctlr_handle,
1581 &(req->req_pkt), &(req->req_handle));
1583 req->error_code = error;
1584 req->req_pkt.tw_osl_callback(&(req->req_handle));
1593 * Function name: tw_osli_unmap_request
1594 * Description: Undoes the mapping done by tw_osli_map_request.
1596 * Input: req -- ptr to request pkt
1598 * Return value: None
1601 tw_osli_unmap_request(struct tw_osli_req_context *req)
1603 struct twa_softc *sc = req->ctlr;
1605 tw_osli_dbg_dprintf(10, sc, "entered");
1607 /* If the command involved data, unmap that too. */
1608 if (req->data != NULL) {
1609 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) {
1610 /* Lock against multiple simultaneous ioctl calls. */
1611 mtx_lock_spin(sc->io_lock);
1613 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1614 bus_dmamap_sync(sc->ioctl_tag,
1615 sc->ioctl_map, BUS_DMASYNC_POSTREAD);
1618 * If we are using a bounce buffer, and we are
1619 * reading data, copy the real data in.
1621 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1622 bcopy(req->data, req->real_data,
1626 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1627 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map,
1628 BUS_DMASYNC_POSTWRITE);
1630 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map);
1632 mtx_unlock_spin(sc->io_lock);
1634 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) {
1635 bus_dmamap_sync(sc->dma_tag,
1636 req->dma_map, BUS_DMASYNC_POSTREAD);
1639 * If we are using a bounce buffer, and we are
1640 * reading data, copy the real data in.
1642 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED)
1643 bcopy(req->data, req->real_data,
1646 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT)
1647 bus_dmamap_sync(sc->dma_tag, req->dma_map,
1648 BUS_DMASYNC_POSTWRITE);
1650 bus_dmamap_unload(sc->dma_tag, req->dma_map);
1654 /* Free alignment buffer if it was used. */
1655 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) {
1656 free(req->data, TW_OSLI_MALLOC_CLASS);
1657 /* Restore original data pointer and length. */
1658 req->data = req->real_data;
1659 req->length = req->real_length;
1667 TW_VOID twa_report_stats(TW_VOID);
1668 TW_VOID twa_reset_stats(TW_VOID);
1669 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc);
1670 TW_VOID twa_print_req_info(struct tw_osli_req_context *req);
1674 * Function name: twa_report_stats
1675 * Description: For being called from ddb. Calls functions that print
1676 * OSL and CL internal stats for the controller.
1680 * Return value: None
1683 twa_report_stats(TW_VOID)
1685 struct twa_softc *sc;
1688 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1689 tw_osli_print_ctlr_stats(sc);
1690 tw_cl_print_ctlr_stats(&sc->ctlr_handle);
1697 * Function name: tw_osli_print_ctlr_stats
1698 * Description: For being called from ddb. Prints OSL controller stats
1700 * Input: sc -- ptr to OSL internal controller context
1702 * Return value: None
1705 tw_osli_print_ctlr_stats(struct twa_softc *sc)
1707 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc);
1708 twa_printf(sc, "OSLq type current max\n");
1709 twa_printf(sc, "free %04d %04d\n",
1710 sc->q_stats[TW_OSLI_FREE_Q].cur_len,
1711 sc->q_stats[TW_OSLI_FREE_Q].max_len);
1712 twa_printf(sc, "busy %04d %04d\n",
1713 sc->q_stats[TW_OSLI_BUSY_Q].cur_len,
1714 sc->q_stats[TW_OSLI_BUSY_Q].max_len);
1720 * Function name: twa_print_req_info
1721 * Description: For being called from ddb. Calls functions that print
1722 * OSL and CL internal details for the request.
1724 * Input: req -- ptr to OSL internal request context
1726 * Return value: None
1729 twa_print_req_info(struct tw_osli_req_context *req)
1731 struct twa_softc *sc = req->ctlr;
1733 twa_printf(sc, "OSL details for request:\n");
1734 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n"
1735 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n"
1736 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n"
1737 "next_req = %p, prev_req = %p, dma_map = %p\n",
1738 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt,
1739 req->data, req->length, req->real_data, req->real_length,
1740 req->state, req->flags, req->error_code, req->orig_req,
1741 req->link.next, req->link.prev, req->dma_map);
1742 tw_cl_print_req_info(&(req->req_handle));
1748 * Function name: twa_reset_stats
1749 * Description: For being called from ddb.
1750 * Resets some OSL controller stats.
1754 * Return value: None
1757 twa_reset_stats(TW_VOID)
1759 struct twa_softc *sc;
1762 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) {
1763 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0;
1764 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0;
1765 tw_cl_reset_stats(&sc->ctlr_handle);
1769 #endif /* TW_OSL_DEBUG */