1 /* 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 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/dev/twa/tw_osl_freebsd.c,v 1.19 2011/11/07 06:44:47 ed Exp $ 30 */ 31 32 /* 33 * AMCC'S 3ware driver for 9000 series storage controllers. 34 * 35 * Author: Vinod Kashyap 36 * Modifications by: Adam Radford 37 * Modifications by: Manjunath Ranganathaiah 38 */ 39 40 41 /* 42 * FreeBSD specific functions not related to CAM, and other 43 * miscellaneous functions. 44 */ 45 46 47 #include <dev/raid/twa/tw_osl_includes.h> 48 #include <dev/raid/twa/tw_cl_fwif.h> 49 #include <dev/raid/twa/tw_cl_ioctl.h> 50 #include <dev/raid/twa/tw_osl_ioctl.h> 51 52 #ifdef TW_OSL_DEBUG 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 */ 56 57 static MALLOC_DEFINE(TW_OSLI_MALLOC_CLASS, "twa_commands", "twa commands"); 58 59 60 static d_open_t twa_open; 61 static d_close_t twa_close; 62 static d_ioctl_t twa_ioctl; 63 64 static struct dev_ops twa_ops = { 65 { "twa", 0, 0 }, 66 .d_open = twa_open, 67 .d_close = twa_close, 68 .d_ioctl = twa_ioctl, 69 }; 70 71 static devclass_t twa_devclass; 72 73 static int twa_msi_enable = 0; 74 TUNABLE_INT("hw.twa.msi.enable", &twa_msi_enable); 75 76 77 /* 78 * Function name: twa_open 79 * Description: Called when the controller is opened. 80 * Simply marks the controller as open. 81 * 82 * Input: dev -- control device corresponding to the ctlr 83 * flags -- mode of open 84 * fmt -- device type (character/block etc.) 85 * proc -- current process 86 * Output: None 87 * Return value: 0 -- success 88 * non-zero-- failure 89 */ 90 static TW_INT32 91 twa_open(struct dev_open_args *ap) 92 { 93 cdev_t dev = ap->a_head.a_dev; 94 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1); 95 96 tw_osli_dbg_dprintf(5, sc, "entered"); 97 sc->open = TW_CL_TRUE; 98 return(0); 99 } 100 101 102 103 /* 104 * Function name: twa_close 105 * Description: Called when the controller is closed. 106 * Simply marks the controller as not open. 107 * 108 * Input: dev -- control device corresponding to the ctlr 109 * flags -- mode of corresponding open 110 * fmt -- device type (character/block etc.) 111 * proc -- current process 112 * Output: None 113 * Return value: 0 -- success 114 * non-zero-- failure 115 */ 116 static TW_INT32 117 twa_close(struct dev_close_args *ap) 118 { 119 cdev_t dev = ap->a_head.a_dev; 120 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1); 121 122 tw_osli_dbg_dprintf(5, sc, "entered"); 123 sc->open = TW_CL_FALSE; 124 return(0); 125 } 126 127 128 129 /* 130 * Function name: twa_ioctl 131 * Description: Called when an ioctl is posted to the controller. 132 * Handles any OS Layer specific cmds, passes the rest 133 * on to the Common Layer. 134 * 135 * Input: dev -- control device corresponding to the ctlr 136 * cmd -- ioctl cmd 137 * buf -- ptr to buffer in kernel memory, which is 138 * a copy of the input buffer in user-space 139 * flags -- mode of corresponding open 140 * proc -- current process 141 * Output: buf -- ptr to buffer in kernel memory, which will 142 * be copied to the output buffer in user-space 143 * Return value: 0 -- success 144 * non-zero-- failure 145 */ 146 static TW_INT32 147 twa_ioctl(struct dev_ioctl_args *ap) 148 { 149 cdev_t dev = ap->a_head.a_dev; 150 u_long cmd = ap->a_cmd; 151 caddr_t buf = ap->a_data; 152 struct twa_softc *sc = (struct twa_softc *)(dev->si_drv1); 153 TW_INT32 error; 154 155 tw_osli_dbg_dprintf(5, sc, "entered"); 156 157 switch (cmd) { 158 case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH: 159 tw_osli_dbg_dprintf(6, sc, "ioctl: fw_passthru"); 160 error = tw_osli_fw_passthru(sc, (TW_INT8 *)buf); 161 break; 162 163 case TW_OSL_IOCTL_SCAN_BUS: 164 /* Request CAM for a bus scan. */ 165 tw_osli_dbg_dprintf(6, sc, "ioctl: scan bus"); 166 error = tw_osli_request_bus_scan(sc); 167 break; 168 169 default: 170 tw_osli_dbg_dprintf(6, sc, "ioctl: 0x%lx", cmd); 171 error = tw_cl_ioctl(&sc->ctlr_handle, cmd, buf); 172 break; 173 } 174 return(error); 175 } 176 177 178 179 static TW_INT32 twa_probe(device_t dev); 180 static TW_INT32 twa_attach(device_t dev); 181 static TW_INT32 twa_detach(device_t dev); 182 static TW_INT32 twa_shutdown(device_t dev); 183 static TW_VOID twa_pci_intr(TW_VOID *arg); 184 static TW_VOID twa_watchdog(TW_VOID *arg); 185 int twa_setup_intr(struct twa_softc *sc); 186 int twa_teardown_intr(struct twa_softc *sc); 187 188 static TW_INT32 tw_osli_alloc_mem(struct twa_softc *sc); 189 static TW_VOID tw_osli_free_resources(struct twa_softc *sc); 190 191 static TW_VOID twa_map_load_data_callback(TW_VOID *arg, 192 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error); 193 static TW_VOID twa_map_load_callback(TW_VOID *arg, 194 bus_dma_segment_t *segs, TW_INT32 nsegments, TW_INT32 error); 195 196 197 static device_method_t twa_methods[] = { 198 /* Device interface */ 199 DEVMETHOD(device_probe, twa_probe), 200 DEVMETHOD(device_attach, twa_attach), 201 DEVMETHOD(device_detach, twa_detach), 202 DEVMETHOD(device_shutdown, twa_shutdown), 203 204 DEVMETHOD(bus_print_child, bus_generic_print_child), 205 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 206 DEVMETHOD_END 207 }; 208 209 static driver_t twa_pci_driver = { 210 "twa", 211 twa_methods, 212 sizeof(struct twa_softc) 213 }; 214 215 DRIVER_MODULE(twa, pci, twa_pci_driver, twa_devclass, NULL, NULL); 216 MODULE_DEPEND(twa, cam, 1, 1, 1); 217 MODULE_DEPEND(twa, pci, 1, 1, 1); 218 MODULE_VERSION(twa, 1); 219 220 221 /* 222 * Function name: twa_probe 223 * Description: Called at driver load time. Claims 9000 ctlrs. 224 * 225 * Input: dev -- bus device corresponding to the ctlr 226 * Output: None 227 * Return value: <= 0 -- success 228 * > 0 -- failure 229 */ 230 static TW_INT32 231 twa_probe(device_t dev) 232 { 233 static TW_UINT8 first_ctlr = 1; 234 235 tw_osli_dbg_printf(3, "entered"); 236 237 if (tw_cl_ctlr_supported(pci_get_vendor(dev), pci_get_device(dev))) { 238 device_set_desc(dev, TW_OSLI_DEVICE_NAME); 239 /* Print the driver version only once. */ 240 if (first_ctlr) { 241 kprintf("3ware device driver for 9000 series storage " 242 "controllers, version: %s\n", 243 TW_OSL_DRIVER_VERSION_STRING); 244 first_ctlr = 0; 245 } 246 return(0); 247 } 248 return(ENXIO); 249 } 250 251 int twa_setup_intr(struct twa_softc *sc) 252 { 253 int error = 0; 254 255 if (!(sc->intr_handle) && (sc->irq_res)) { 256 error = bus_setup_intr(sc->bus_dev, sc->irq_res, 257 INTR_MPSAFE, 258 twa_pci_intr, 259 sc, &sc->intr_handle, NULL); 260 } 261 return( error ); 262 } 263 264 265 int twa_teardown_intr(struct twa_softc *sc) 266 { 267 int error = 0; 268 269 if ((sc->intr_handle) && (sc->irq_res)) { 270 error = bus_teardown_intr(sc->bus_dev, 271 sc->irq_res, sc->intr_handle); 272 sc->intr_handle = NULL; 273 } 274 return( error ); 275 } 276 277 278 279 /* 280 * Function name: twa_attach 281 * Description: Allocates pci resources; updates sc; adds a node to the 282 * sysctl tree to expose the driver version; makes calls 283 * (to the Common Layer) to initialize ctlr, and to 284 * attach to CAM. 285 * 286 * Input: dev -- bus device corresponding to the ctlr 287 * Output: None 288 * Return value: 0 -- success 289 * non-zero-- failure 290 */ 291 static TW_INT32 292 twa_attach(device_t dev) 293 { 294 struct twa_softc *sc = device_get_softc(dev); 295 TW_UINT32 command; 296 TW_INT32 bar_num; 297 TW_INT32 bar0_offset; 298 TW_INT32 bar_size; 299 TW_INT32 irq_flags; 300 TW_INT32 error; 301 302 tw_osli_dbg_dprintf(3, sc, "entered"); 303 304 sc->ctlr_handle.osl_ctlr_ctxt = sc; 305 306 /* Initialize the softc structure. */ 307 sc->bus_dev = dev; 308 sc->device_id = pci_get_device(dev); 309 310 /* Initialize the mutexes right here. */ 311 sc->io_lock = &(sc->io_lock_handle); 312 spin_init(sc->io_lock); 313 sc->q_lock = &(sc->q_lock_handle); 314 spin_init(sc->q_lock); 315 sc->sim_lock = &(sc->sim_lock_handle); 316 lockinit(sc->sim_lock, "tw_osl_sim_lock", 0, LK_CANRECURSE); 317 318 sysctl_ctx_init(&sc->sysctl_ctxt); 319 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctxt, 320 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 321 device_get_nameunit(dev), CTLFLAG_RD, 0, ""); 322 if (sc->sysctl_tree == NULL) { 323 tw_osli_printf(sc, "error = %d", 324 TW_CL_SEVERITY_ERROR_STRING, 325 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 326 0x2000, 327 "Cannot add sysctl tree node", 328 ENXIO); 329 return(ENXIO); 330 } 331 SYSCTL_ADD_STRING(&sc->sysctl_ctxt, SYSCTL_CHILDREN(sc->sysctl_tree), 332 OID_AUTO, "driver_version", CTLFLAG_RD, 333 TW_OSL_DRIVER_VERSION_STRING, 0, "TWA driver version"); 334 335 /* Make sure we are going to be able to talk to this board. */ 336 command = pci_read_config(dev, PCIR_COMMAND, 2); 337 if ((command & PCIM_CMD_PORTEN) == 0) { 338 tw_osli_printf(sc, "error = %d", 339 TW_CL_SEVERITY_ERROR_STRING, 340 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 341 0x2001, 342 "Register window not available", 343 ENXIO); 344 tw_osli_free_resources(sc); 345 return(ENXIO); 346 } 347 348 /* Force the busmaster enable bit on, in case the BIOS forgot. */ 349 command |= PCIM_CMD_BUSMASTEREN; 350 pci_write_config(dev, PCIR_COMMAND, command, 2); 351 352 /* Allocate the PCI register window. */ 353 if ((error = tw_cl_get_pci_bar_info(sc->device_id, TW_CL_BAR_TYPE_MEM, 354 &bar_num, &bar0_offset, &bar_size))) { 355 tw_osli_printf(sc, "error = %d", 356 TW_CL_SEVERITY_ERROR_STRING, 357 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 358 0x201F, 359 "Can't get PCI BAR info", 360 error); 361 tw_osli_free_resources(sc); 362 return(error); 363 } 364 sc->reg_res_id = PCIR_BARS + bar0_offset; 365 if ((sc->reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 366 &(sc->reg_res_id), 0, ~0, 1, RF_ACTIVE)) 367 == NULL) { 368 tw_osli_printf(sc, "error = %d", 369 TW_CL_SEVERITY_ERROR_STRING, 370 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 371 0x2002, 372 "Can't allocate register window", 373 ENXIO); 374 tw_osli_free_resources(sc); 375 return(ENXIO); 376 } 377 sc->bus_tag = rman_get_bustag(sc->reg_res); 378 sc->bus_handle = rman_get_bushandle(sc->reg_res); 379 380 /* Allocate and register our interrupt. */ 381 sc->irq_res_id = 0; 382 sc->irq_type = pci_alloc_1intr(sc->bus_dev, twa_msi_enable, 383 &sc->irq_res_id, &irq_flags); 384 if ((sc->irq_res = bus_alloc_resource(sc->bus_dev, SYS_RES_IRQ, 385 &(sc->irq_res_id), 0, ~0, 1, 386 irq_flags)) == NULL) { 387 tw_osli_printf(sc, "error = %d", 388 TW_CL_SEVERITY_ERROR_STRING, 389 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 390 0x2003, 391 "Can't allocate interrupt", 392 ENXIO); 393 tw_osli_free_resources(sc); 394 return(ENXIO); 395 } 396 if ((error = twa_setup_intr(sc))) { 397 tw_osli_printf(sc, "error = %d", 398 TW_CL_SEVERITY_ERROR_STRING, 399 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 400 0x2004, 401 "Can't set up interrupt", 402 error); 403 tw_osli_free_resources(sc); 404 return(error); 405 } 406 407 if ((error = tw_osli_alloc_mem(sc))) { 408 tw_osli_printf(sc, "error = %d", 409 TW_CL_SEVERITY_ERROR_STRING, 410 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 411 0x2005, 412 "Memory allocation failure", 413 error); 414 tw_osli_free_resources(sc); 415 return(error); 416 } 417 418 /* Initialize the Common Layer for this controller. */ 419 if ((error = tw_cl_init_ctlr(&sc->ctlr_handle, sc->flags, sc->device_id, 420 TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS, 421 sc->non_dma_mem, sc->dma_mem, 422 sc->dma_mem_phys 423 ))) { 424 tw_osli_printf(sc, "error = %d", 425 TW_CL_SEVERITY_ERROR_STRING, 426 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 427 0x2006, 428 "Failed to initialize Common Layer/controller", 429 error); 430 tw_osli_free_resources(sc); 431 return(error); 432 } 433 434 /* Create the control device. */ 435 sc->ctrl_dev = make_dev(&twa_ops, device_get_unit(sc->bus_dev), 436 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR, 437 "twa%d", device_get_unit(sc->bus_dev)); 438 sc->ctrl_dev->si_drv1 = sc; 439 440 if ((error = tw_osli_cam_attach(sc))) { 441 tw_osli_free_resources(sc); 442 tw_osli_printf(sc, "error = %d", 443 TW_CL_SEVERITY_ERROR_STRING, 444 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 445 0x2007, 446 "Failed to initialize CAM", 447 error); 448 return(error); 449 } 450 451 sc->watchdog_index = 0; 452 callout_init(&(sc->watchdog_callout[0])); 453 callout_init(&(sc->watchdog_callout[1])); 454 callout_reset(&(sc->watchdog_callout[0]), 5*hz, twa_watchdog, &sc->ctlr_handle); 455 456 return(0); 457 } 458 459 460 static TW_VOID 461 twa_watchdog(TW_VOID *arg) 462 { 463 struct tw_cl_ctlr_handle *ctlr_handle = 464 (struct tw_cl_ctlr_handle *)arg; 465 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt; 466 int i; 467 int i_need_a_reset = 0; 468 int driver_is_active = 0; 469 TW_UINT64 current_time; 470 struct tw_osli_req_context *my_req; 471 472 473 //============================================================================== 474 current_time = (TW_UINT64) (tw_osl_get_local_time()); 475 476 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) { 477 my_req = &(sc->req_ctx_buf[i]); 478 479 if ((my_req->state == TW_OSLI_REQ_STATE_BUSY) && 480 (my_req->deadline) && 481 (my_req->deadline < current_time)) { 482 tw_cl_set_reset_needed(ctlr_handle); 483 #ifdef TW_OSL_DEBUG 484 device_printf((sc)->bus_dev, "Request %d timed out! d = %llu, c = %llu\n", i, my_req->deadline, current_time); 485 #else /* TW_OSL_DEBUG */ 486 device_printf((sc)->bus_dev, "Request %d timed out!\n", i); 487 #endif /* TW_OSL_DEBUG */ 488 break; 489 } 490 } 491 //============================================================================== 492 493 i_need_a_reset = tw_cl_is_reset_needed(ctlr_handle); 494 495 i = (int) ((sc->watchdog_index++) & 1); 496 497 driver_is_active = tw_cl_is_active(ctlr_handle); 498 499 if (i_need_a_reset) { 500 #ifdef TW_OSL_DEBUG 501 device_printf((sc)->bus_dev, "Watchdog rescheduled in 70 seconds\n"); 502 #endif /* TW_OSL_DEBUG */ 503 callout_reset(&(sc->watchdog_callout[i]), 70*hz, twa_watchdog, &sc->ctlr_handle); 504 tw_cl_reset_ctlr(ctlr_handle); 505 #ifdef TW_OSL_DEBUG 506 device_printf((sc)->bus_dev, "Watchdog reset completed!\n"); 507 #endif /* TW_OSL_DEBUG */ 508 } else if (driver_is_active) { 509 callout_reset(&(sc->watchdog_callout[i]), 5*hz, twa_watchdog, &sc->ctlr_handle); 510 } 511 #ifdef TW_OSL_DEBUG 512 if (i_need_a_reset) 513 device_printf((sc)->bus_dev, "i_need_a_reset = %d, " 514 "driver_is_active = %d\n", 515 i_need_a_reset, driver_is_active); 516 #endif /* TW_OSL_DEBUG */ 517 } 518 519 520 /* 521 * Function name: tw_osli_alloc_mem 522 * Description: Allocates memory needed both by CL and OSL. 523 * 524 * Input: sc -- OSL internal controller context 525 * Output: None 526 * Return value: 0 -- success 527 * non-zero-- failure 528 */ 529 static TW_INT32 530 tw_osli_alloc_mem(struct twa_softc *sc) 531 { 532 struct tw_osli_req_context *req; 533 TW_UINT32 max_sg_elements; 534 TW_UINT32 non_dma_mem_size; 535 TW_UINT32 dma_mem_size; 536 TW_INT32 error; 537 TW_INT32 i; 538 539 tw_osli_dbg_dprintf(3, sc, "entered"); 540 541 sc->flags |= (sizeof(bus_addr_t) == 8) ? TW_CL_64BIT_ADDRESSES : 0; 542 sc->flags |= (sizeof(bus_size_t) == 8) ? TW_CL_64BIT_SG_LENGTH : 0; 543 544 max_sg_elements = (sizeof(bus_addr_t) == 8) ? 545 TW_CL_MAX_64BIT_SG_ELEMENTS : TW_CL_MAX_32BIT_SG_ELEMENTS; 546 547 if ((error = tw_cl_get_mem_requirements(&sc->ctlr_handle, sc->flags, 548 sc->device_id, TW_OSLI_MAX_NUM_REQUESTS, TW_OSLI_MAX_NUM_AENS, 549 &(sc->alignment), &(sc->sg_size_factor), 550 &non_dma_mem_size, &dma_mem_size 551 ))) { 552 tw_osli_printf(sc, "error = %d", 553 TW_CL_SEVERITY_ERROR_STRING, 554 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 555 0x2008, 556 "Can't get Common Layer's memory requirements", 557 error); 558 return(error); 559 } 560 561 sc->non_dma_mem = kmalloc(non_dma_mem_size, TW_OSLI_MALLOC_CLASS, 562 M_WAITOK); 563 564 /* Create the parent dma tag. */ 565 if (bus_dma_tag_create(NULL, /* parent */ 566 sc->alignment, /* alignment */ 567 TW_OSLI_DMA_BOUNDARY, /* boundary */ 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 */ 574 0, /* flags */ 575 &sc->parent_tag /* tag */)) { 576 tw_osli_printf(sc, "error = %d", 577 TW_CL_SEVERITY_ERROR_STRING, 578 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 579 0x200A, 580 "Can't allocate parent DMA tag", 581 ENOMEM); 582 return(ENOMEM); 583 } 584 585 /* Create a dma tag for Common Layer's DMA'able memory (dma_mem). */ 586 if (bus_dma_tag_create(sc->parent_tag, /* parent */ 587 sc->alignment, /* alignment */ 588 0, /* boundary */ 589 BUS_SPACE_MAXADDR, /* lowaddr */ 590 BUS_SPACE_MAXADDR, /* highaddr */ 591 NULL, NULL, /* filter, filterarg */ 592 dma_mem_size, /* maxsize */ 593 1, /* nsegments */ 594 BUS_SPACE_MAXSIZE, /* maxsegsize */ 595 0, /* flags */ 596 &sc->cmd_tag /* tag */)) { 597 tw_osli_printf(sc, "error = %d", 598 TW_CL_SEVERITY_ERROR_STRING, 599 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 600 0x200B, 601 "Can't allocate DMA tag for Common Layer's " 602 "DMA'able memory", 603 ENOMEM); 604 return(ENOMEM); 605 } 606 607 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem, 608 BUS_DMA_NOWAIT, &sc->cmd_map)) { 609 /* Try a second time. */ 610 if (bus_dmamem_alloc(sc->cmd_tag, &sc->dma_mem, 611 BUS_DMA_NOWAIT, &sc->cmd_map)) { 612 tw_osli_printf(sc, "error = %d", 613 TW_CL_SEVERITY_ERROR_STRING, 614 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 615 0x200C, 616 "Can't allocate DMA'able memory for the" 617 "Common Layer", 618 ENOMEM); 619 return(ENOMEM); 620 } 621 } 622 623 bus_dmamap_load(sc->cmd_tag, sc->cmd_map, sc->dma_mem, 624 dma_mem_size, twa_map_load_callback, 625 &sc->dma_mem_phys, 0); 626 627 /* 628 * Create a dma tag for data buffers; size will be the maximum 629 * possible I/O size (128kB). 630 */ 631 if (bus_dma_tag_create(sc->parent_tag, /* parent */ 632 sc->alignment, /* alignment */ 633 0, /* boundary */ 634 BUS_SPACE_MAXADDR, /* lowaddr */ 635 BUS_SPACE_MAXADDR, /* highaddr */ 636 NULL, NULL, /* filter, filterarg */ 637 TW_CL_MAX_IO_SIZE, /* maxsize */ 638 max_sg_elements, /* nsegments */ 639 TW_CL_MAX_IO_SIZE, /* maxsegsize */ 640 BUS_DMA_ALLOCNOW, /* flags */ 641 &sc->dma_tag /* tag */)) { 642 tw_osli_printf(sc, "error = %d", 643 TW_CL_SEVERITY_ERROR_STRING, 644 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 645 0x200F, 646 "Can't allocate DMA tag for data buffers", 647 ENOMEM); 648 return(ENOMEM); 649 } 650 651 /* 652 * Create a dma tag for ioctl data buffers; size will be the maximum 653 * possible I/O size (128kB). 654 */ 655 if (bus_dma_tag_create(sc->parent_tag, /* parent */ 656 sc->alignment, /* alignment */ 657 0, /* boundary */ 658 BUS_SPACE_MAXADDR, /* lowaddr */ 659 BUS_SPACE_MAXADDR, /* highaddr */ 660 NULL, NULL, /* filter, filterarg */ 661 TW_CL_MAX_IO_SIZE, /* maxsize */ 662 max_sg_elements, /* nsegments */ 663 TW_CL_MAX_IO_SIZE, /* maxsegsize */ 664 BUS_DMA_ALLOCNOW, /* flags */ 665 &sc->ioctl_tag /* tag */)) { 666 tw_osli_printf(sc, "error = %d", 667 TW_CL_SEVERITY_ERROR_STRING, 668 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 669 0x2010, 670 "Can't allocate DMA tag for ioctl data buffers", 671 ENOMEM); 672 return(ENOMEM); 673 } 674 675 /* Create just one map for all ioctl request data buffers. */ 676 if (bus_dmamap_create(sc->ioctl_tag, 0, &sc->ioctl_map)) { 677 tw_osli_printf(sc, "error = %d", 678 TW_CL_SEVERITY_ERROR_STRING, 679 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 680 0x2011, 681 "Can't create ioctl map", 682 ENOMEM); 683 return(ENOMEM); 684 } 685 686 687 /* Initialize request queues. */ 688 tw_osli_req_q_init(sc, TW_OSLI_FREE_Q); 689 tw_osli_req_q_init(sc, TW_OSLI_BUSY_Q); 690 691 sc->req_ctx_buf = kmalloc((sizeof(struct tw_osli_req_context) * 692 TW_OSLI_MAX_NUM_REQUESTS), TW_OSLI_MALLOC_CLASS, 693 M_WAITOK | M_ZERO); 694 for (i = 0; i < TW_OSLI_MAX_NUM_REQUESTS; i++) { 695 req = &(sc->req_ctx_buf[i]); 696 req->ctlr = sc; 697 if (bus_dmamap_create(sc->dma_tag, 0, &req->dma_map)) { 698 tw_osli_printf(sc, "request # = %d, error = %d", 699 TW_CL_SEVERITY_ERROR_STRING, 700 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 701 0x2013, 702 "Can't create dma map", 703 i, ENOMEM); 704 return(ENOMEM); 705 } 706 707 /* Initialize the ioctl wakeup/ timeout mutex */ 708 req->ioctl_wake_timeout_lock = &(req->ioctl_wake_timeout_lock_handle); 709 lockinit(req->ioctl_wake_timeout_lock, "tw_ioctl_wake_timeout_lock", 0, 0); 710 711 /* Insert request into the free queue. */ 712 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q); 713 } 714 715 return(0); 716 } 717 718 719 720 /* 721 * Function name: tw_osli_free_resources 722 * Description: Performs clean-up at the time of going down. 723 * 724 * Input: sc -- ptr to OSL internal ctlr context 725 * Output: None 726 * Return value: None 727 */ 728 static TW_VOID 729 tw_osli_free_resources(struct twa_softc *sc) 730 { 731 struct tw_osli_req_context *req; 732 TW_INT32 error = 0; 733 734 tw_osli_dbg_dprintf(3, sc, "entered"); 735 736 /* Detach from CAM */ 737 tw_osli_cam_detach(sc); 738 739 if (sc->req_ctx_buf) 740 while ((req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q)) != 741 NULL) { 742 lockuninit(req->ioctl_wake_timeout_lock); 743 744 if ((error = bus_dmamap_destroy(sc->dma_tag, 745 req->dma_map))) 746 tw_osli_dbg_dprintf(1, sc, 747 "dmamap_destroy(dma) returned %d", 748 error); 749 } 750 751 if ((sc->ioctl_tag) && (sc->ioctl_map)) 752 if ((error = bus_dmamap_destroy(sc->ioctl_tag, sc->ioctl_map))) 753 tw_osli_dbg_dprintf(1, sc, 754 "dmamap_destroy(ioctl) returned %d", error); 755 756 /* Free all memory allocated so far. */ 757 if (sc->req_ctx_buf) 758 kfree(sc->req_ctx_buf, TW_OSLI_MALLOC_CLASS); 759 760 if (sc->non_dma_mem) 761 kfree(sc->non_dma_mem, TW_OSLI_MALLOC_CLASS); 762 763 if (sc->dma_mem) { 764 bus_dmamap_unload(sc->cmd_tag, sc->cmd_map); 765 bus_dmamem_free(sc->cmd_tag, sc->dma_mem, 766 sc->cmd_map); 767 } 768 if (sc->cmd_tag) 769 if ((error = bus_dma_tag_destroy(sc->cmd_tag))) 770 tw_osli_dbg_dprintf(1, sc, 771 "dma_tag_destroy(cmd) returned %d", error); 772 773 if (sc->dma_tag) 774 if ((error = bus_dma_tag_destroy(sc->dma_tag))) 775 tw_osli_dbg_dprintf(1, sc, 776 "dma_tag_destroy(dma) returned %d", error); 777 778 if (sc->ioctl_tag) 779 if ((error = bus_dma_tag_destroy(sc->ioctl_tag))) 780 tw_osli_dbg_dprintf(1, sc, 781 "dma_tag_destroy(ioctl) returned %d", error); 782 783 if (sc->parent_tag) 784 if ((error = bus_dma_tag_destroy(sc->parent_tag))) 785 tw_osli_dbg_dprintf(1, sc, 786 "dma_tag_destroy(parent) returned %d", error); 787 788 789 /* Disconnect the interrupt handler. */ 790 if ((error = twa_teardown_intr(sc))) 791 tw_osli_dbg_dprintf(1, sc, 792 "teardown_intr returned %d", error); 793 794 if (sc->irq_res != NULL) 795 if ((error = bus_release_resource(sc->bus_dev, 796 SYS_RES_IRQ, sc->irq_res_id, sc->irq_res))) 797 tw_osli_dbg_dprintf(1, sc, 798 "release_resource(irq) returned %d", error); 799 800 if (sc->irq_type == PCI_INTR_TYPE_MSI) 801 pci_release_msi(sc->bus_dev); 802 803 /* Release the register window mapping. */ 804 if (sc->reg_res != NULL) 805 if ((error = bus_release_resource(sc->bus_dev, 806 SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res))) 807 tw_osli_dbg_dprintf(1, sc, 808 "release_resource(io) returned %d", error); 809 810 /* Destroy the control device. */ 811 if (sc->ctrl_dev != NULL) 812 destroy_dev(sc->ctrl_dev); 813 dev_ops_remove_minor(&twa_ops, device_get_unit(sc->bus_dev)); 814 815 if ((error = sysctl_ctx_free(&sc->sysctl_ctxt))) 816 tw_osli_dbg_dprintf(1, sc, 817 "sysctl_ctx_free returned %d", error); 818 819 } 820 821 822 823 /* 824 * Function name: twa_detach 825 * Description: Called when the controller is being detached from 826 * the pci bus. 827 * 828 * Input: dev -- bus device corresponding to the ctlr 829 * Output: None 830 * Return value: 0 -- success 831 * non-zero-- failure 832 */ 833 static TW_INT32 834 twa_detach(device_t dev) 835 { 836 struct twa_softc *sc = device_get_softc(dev); 837 TW_INT32 error; 838 839 tw_osli_dbg_dprintf(3, sc, "entered"); 840 841 error = EBUSY; 842 if (sc->open) { 843 tw_osli_printf(sc, "error = %d", 844 TW_CL_SEVERITY_ERROR_STRING, 845 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 846 0x2014, 847 "Device open", 848 error); 849 goto out; 850 } 851 852 /* Shut the controller down. */ 853 if ((error = twa_shutdown(dev))) 854 goto out; 855 856 /* Free all resources associated with this controller. */ 857 tw_osli_free_resources(sc); 858 error = 0; 859 860 out: 861 return(error); 862 } 863 864 865 866 /* 867 * Function name: twa_shutdown 868 * Description: Called at unload/shutdown time. Lets the controller 869 * know that we are going down. 870 * 871 * Input: dev -- bus device corresponding to the ctlr 872 * Output: None 873 * Return value: 0 -- success 874 * non-zero-- failure 875 */ 876 static TW_INT32 877 twa_shutdown(device_t dev) 878 { 879 struct twa_softc *sc = device_get_softc(dev); 880 TW_INT32 error = 0; 881 882 tw_osli_dbg_dprintf(3, sc, "entered"); 883 884 /* Disconnect interrupts. */ 885 error = twa_teardown_intr(sc); 886 887 /* Stop watchdog task. */ 888 callout_stop_sync(&(sc->watchdog_callout[0])); 889 callout_stop_sync(&(sc->watchdog_callout[1])); 890 891 /* Disconnect from the controller. */ 892 if ((error = tw_cl_shutdown_ctlr(&(sc->ctlr_handle), 0))) { 893 tw_osli_printf(sc, "error = %d", 894 TW_CL_SEVERITY_ERROR_STRING, 895 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 896 0x2015, 897 "Failed to shutdown Common Layer/controller", 898 error); 899 } 900 return(error); 901 } 902 903 904 905 /* 906 * Function name: twa_pci_intr 907 * Description: Interrupt handler. Wrapper for twa_interrupt. 908 * 909 * Input: arg -- ptr to OSL internal ctlr context 910 * Output: None 911 * Return value: None 912 */ 913 static TW_VOID 914 twa_pci_intr(TW_VOID *arg) 915 { 916 struct twa_softc *sc = (struct twa_softc *)arg; 917 918 tw_osli_dbg_dprintf(10, sc, "entered"); 919 tw_cl_interrupt(&(sc->ctlr_handle)); 920 } 921 922 923 /* 924 * Function name: tw_osli_fw_passthru 925 * Description: Builds a fw passthru cmd pkt, and submits it to CL. 926 * 927 * Input: sc -- ptr to OSL internal ctlr context 928 * buf -- ptr to ioctl pkt understood by CL 929 * Output: None 930 * Return value: 0 -- success 931 * non-zero-- failure 932 */ 933 TW_INT32 934 tw_osli_fw_passthru(struct twa_softc *sc, TW_INT8 *buf) 935 { 936 struct tw_osli_req_context *req; 937 struct tw_osli_ioctl_no_data_buf *user_buf = 938 (struct tw_osli_ioctl_no_data_buf *)buf; 939 TW_TIME end_time; 940 TW_UINT32 timeout = 60; 941 TW_UINT32 data_buf_size_adjusted; 942 struct tw_cl_req_packet *req_pkt; 943 struct tw_cl_passthru_req_packet *pt_req; 944 TW_INT32 error; 945 946 tw_osli_dbg_dprintf(5, sc, "ioctl: passthru"); 947 948 if ((req = tw_osli_get_request(sc)) == NULL) 949 return(EBUSY); 950 951 req->req_handle.osl_req_ctxt = req; 952 req->orig_req = buf; 953 req->flags |= TW_OSLI_REQ_FLAGS_PASSTHRU; 954 955 req_pkt = &(req->req_pkt); 956 req_pkt->status = 0; 957 req_pkt->tw_osl_callback = tw_osl_complete_passthru; 958 /* Let the Common Layer retry the request on cmd queue full. */ 959 req_pkt->flags |= TW_CL_REQ_RETRY_ON_BUSY; 960 961 pt_req = &(req_pkt->gen_req_pkt.pt_req); 962 /* 963 * Make sure that the data buffer sent to firmware is a 964 * 512 byte multiple in size. 965 */ 966 data_buf_size_adjusted = 967 (user_buf->driver_pkt.buffer_length + 968 (sc->sg_size_factor - 1)) & ~(sc->sg_size_factor - 1); 969 if ((req->length = data_buf_size_adjusted)) { 970 req->data = kmalloc(data_buf_size_adjusted, 971 TW_OSLI_MALLOC_CLASS, M_WAITOK); 972 /* Copy the payload. */ 973 if ((error = copyin((TW_VOID *)(user_buf->pdata), 974 req->data, 975 user_buf->driver_pkt.buffer_length)) != 0) { 976 tw_osli_printf(sc, "error = %d", 977 TW_CL_SEVERITY_ERROR_STRING, 978 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 979 0x2017, 980 "Could not copyin fw_passthru data_buf", 981 error); 982 goto fw_passthru_err; 983 } 984 pt_req->sgl_entries = 1; /* will be updated during mapping */ 985 req->flags |= (TW_OSLI_REQ_FLAGS_DATA_IN | 986 TW_OSLI_REQ_FLAGS_DATA_OUT); 987 } else 988 pt_req->sgl_entries = 0; /* no payload */ 989 990 pt_req->cmd_pkt = (TW_VOID *)(&(user_buf->cmd_pkt)); 991 pt_req->cmd_pkt_length = sizeof(struct tw_cl_command_packet); 992 993 if ((error = tw_osli_map_request(req))) 994 goto fw_passthru_err; 995 996 end_time = tw_osl_get_local_time() + timeout; 997 while (req->state != TW_OSLI_REQ_STATE_COMPLETE) { 998 lockmgr(req->ioctl_wake_timeout_lock, LK_EXCLUSIVE); 999 req->flags |= TW_OSLI_REQ_FLAGS_SLEEPING; 1000 1001 error = lksleep(req, req->ioctl_wake_timeout_lock, 0, 1002 "twa_passthru", timeout*hz); 1003 lockmgr(req->ioctl_wake_timeout_lock, LK_RELEASE); 1004 1005 if (!(req->flags & TW_OSLI_REQ_FLAGS_SLEEPING)) 1006 error = 0; 1007 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING; 1008 1009 if (! error) { 1010 if (((error = req->error_code)) || 1011 ((error = (req->state != 1012 TW_OSLI_REQ_STATE_COMPLETE))) || 1013 ((error = req_pkt->status))) 1014 goto fw_passthru_err; 1015 break; 1016 } 1017 1018 if (req_pkt->status) { 1019 error = req_pkt->status; 1020 goto fw_passthru_err; 1021 } 1022 1023 if (error == EWOULDBLOCK) { 1024 /* Time out! */ 1025 if ((!(req->error_code)) && 1026 (req->state == TW_OSLI_REQ_STATE_COMPLETE) && 1027 (!(req_pkt->status)) ) { 1028 #ifdef TW_OSL_DEBUG 1029 tw_osli_printf(sc, "request = %p", 1030 TW_CL_SEVERITY_ERROR_STRING, 1031 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 1032 0x7777, 1033 "FALSE Passthru timeout!", 1034 req); 1035 #endif /* TW_OSL_DEBUG */ 1036 error = 0; /* False error */ 1037 break; 1038 } 1039 if (!(tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) { 1040 #ifdef TW_OSL_DEBUG 1041 tw_osli_printf(sc, "request = %p", 1042 TW_CL_SEVERITY_ERROR_STRING, 1043 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 1044 0x2018, 1045 "Passthru request timed out!", 1046 req); 1047 #else /* TW_OSL_DEBUG */ 1048 device_printf((sc)->bus_dev, "Passthru request timed out!\n"); 1049 #endif /* TW_OSL_DEBUG */ 1050 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle)); 1051 } 1052 1053 error = 0; 1054 end_time = tw_osl_get_local_time() + timeout; 1055 continue; 1056 /* 1057 * Don't touch req after a reset. It (and any 1058 * associated data) will be 1059 * unmapped by the callback. 1060 */ 1061 } 1062 /* 1063 * Either the request got completed, or we were woken up by a 1064 * signal. Calculate the new timeout, in case it was the latter. 1065 */ 1066 timeout = (end_time - tw_osl_get_local_time()); 1067 } /* End of while loop */ 1068 1069 /* If there was a payload, copy it back. */ 1070 if ((!error) && (req->length)) 1071 if ((error = copyout(req->data, user_buf->pdata, 1072 user_buf->driver_pkt.buffer_length))) 1073 tw_osli_printf(sc, "error = %d", 1074 TW_CL_SEVERITY_ERROR_STRING, 1075 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 1076 0x2019, 1077 "Could not copyout fw_passthru data_buf", 1078 error); 1079 1080 fw_passthru_err: 1081 1082 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET) 1083 error = EBUSY; 1084 1085 user_buf->driver_pkt.os_status = error; 1086 /* Free resources. */ 1087 if (req->data) 1088 kfree(req->data, TW_OSLI_MALLOC_CLASS); 1089 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q); 1090 return(error); 1091 } 1092 1093 1094 1095 /* 1096 * Function name: tw_osl_complete_passthru 1097 * Description: Called to complete passthru requests. 1098 * 1099 * Input: req_handle -- ptr to request handle 1100 * Output: None 1101 * Return value: None 1102 */ 1103 TW_VOID 1104 tw_osl_complete_passthru(struct tw_cl_req_handle *req_handle) 1105 { 1106 struct tw_osli_req_context *req = req_handle->osl_req_ctxt; 1107 struct tw_cl_req_packet *req_pkt = 1108 (struct tw_cl_req_packet *)(&req->req_pkt); 1109 struct twa_softc *sc = req->ctlr; 1110 1111 tw_osli_dbg_dprintf(5, sc, "entered"); 1112 1113 if (req->state != TW_OSLI_REQ_STATE_BUSY) { 1114 tw_osli_printf(sc, "request = %p, status = %d", 1115 TW_CL_SEVERITY_ERROR_STRING, 1116 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 1117 0x201B, 1118 "Unposted command completed!!", 1119 req, req->state); 1120 } 1121 1122 /* 1123 * Remove request from the busy queue. Just mark it complete. 1124 * There's no need to move it into the complete queue as we are 1125 * going to be done with it right now. 1126 */ 1127 req->state = TW_OSLI_REQ_STATE_COMPLETE; 1128 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q); 1129 1130 tw_osli_unmap_request(req); 1131 1132 /* 1133 * Don't do a wake up if there was an error even before the request 1134 * was sent down to the Common Layer, and we hadn't gotten an 1135 * EINPROGRESS. The request originator will then be returned an 1136 * error, and he can do the clean-up. 1137 */ 1138 if ((req->error_code) && (!(req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS))) 1139 return; 1140 1141 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) { 1142 if (req->flags & TW_OSLI_REQ_FLAGS_SLEEPING) { 1143 /* Wake up the sleeping command originator. */ 1144 tw_osli_dbg_dprintf(5, sc, 1145 "Waking up originator of request %p", req); 1146 req->flags &= ~TW_OSLI_REQ_FLAGS_SLEEPING; 1147 wakeup_one(req); 1148 } else { 1149 /* 1150 * If the request completed even before mtx_sleep 1151 * was called, simply return. 1152 */ 1153 if (req->flags & TW_OSLI_REQ_FLAGS_MAPPED) 1154 return; 1155 1156 if (req_pkt->status == TW_CL_ERR_REQ_BUS_RESET) 1157 return; 1158 1159 tw_osli_printf(sc, "request = %p", 1160 TW_CL_SEVERITY_ERROR_STRING, 1161 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 1162 0x201C, 1163 "Passthru callback called, " 1164 "and caller not sleeping", 1165 req); 1166 } 1167 } else { 1168 tw_osli_printf(sc, "request = %p", 1169 TW_CL_SEVERITY_ERROR_STRING, 1170 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 1171 0x201D, 1172 "Passthru callback called for non-passthru request", 1173 req); 1174 } 1175 } 1176 1177 1178 1179 /* 1180 * Function name: tw_osli_get_request 1181 * Description: Gets a request pkt from the free queue. 1182 * 1183 * Input: sc -- ptr to OSL internal ctlr context 1184 * Output: None 1185 * Return value: ptr to request pkt -- success 1186 * NULL -- failure 1187 */ 1188 struct tw_osli_req_context * 1189 tw_osli_get_request(struct twa_softc *sc) 1190 { 1191 struct tw_osli_req_context *req; 1192 1193 tw_osli_dbg_dprintf(4, sc, "entered"); 1194 1195 /* Get a free request packet. */ 1196 req = tw_osli_req_q_remove_head(sc, TW_OSLI_FREE_Q); 1197 1198 /* Initialize some fields to their defaults. */ 1199 if (req) { 1200 req->req_handle.osl_req_ctxt = NULL; 1201 req->req_handle.cl_req_ctxt = NULL; 1202 req->req_handle.is_io = 0; 1203 req->data = NULL; 1204 req->length = 0; 1205 req->deadline = 0; 1206 req->real_data = NULL; 1207 req->real_length = 0; 1208 req->state = TW_OSLI_REQ_STATE_INIT;/* req being initialized */ 1209 req->flags = 0; 1210 req->error_code = 0; 1211 req->orig_req = NULL; 1212 1213 bzero(&(req->req_pkt), sizeof(struct tw_cl_req_packet)); 1214 1215 } 1216 return(req); 1217 } 1218 1219 1220 1221 /* 1222 * Function name: twa_map_load_data_callback 1223 * Description: Callback of bus_dmamap_load for the buffer associated 1224 * with data. Updates the cmd pkt (size/sgl_entries 1225 * fields, as applicable) to reflect the number of sg 1226 * elements. 1227 * 1228 * Input: arg -- ptr to OSL internal request context 1229 * segs -- ptr to a list of segment descriptors 1230 * nsegments--# of segments 1231 * error -- 0 if no errors encountered before callback, 1232 * non-zero if errors were encountered 1233 * Output: None 1234 * Return value: None 1235 */ 1236 static TW_VOID 1237 twa_map_load_data_callback(TW_VOID *arg, bus_dma_segment_t *segs, 1238 TW_INT32 nsegments, TW_INT32 error) 1239 { 1240 struct tw_osli_req_context *req = 1241 (struct tw_osli_req_context *)arg; 1242 struct twa_softc *sc = req->ctlr; 1243 struct tw_cl_req_packet *req_pkt = &(req->req_pkt); 1244 1245 tw_osli_dbg_dprintf(10, sc, "entered"); 1246 1247 if (error == EINVAL) { 1248 req->error_code = error; 1249 return; 1250 } 1251 1252 /* Mark the request as currently being processed. */ 1253 req->state = TW_OSLI_REQ_STATE_BUSY; 1254 /* Move the request into the busy queue. */ 1255 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q); 1256 1257 req->flags |= TW_OSLI_REQ_FLAGS_MAPPED; 1258 1259 if (error == EFBIG) { 1260 req->error_code = error; 1261 goto out; 1262 } 1263 1264 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) { 1265 struct tw_cl_passthru_req_packet *pt_req; 1266 1267 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) 1268 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map, 1269 BUS_DMASYNC_PREREAD); 1270 1271 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) { 1272 /* 1273 * If we're using an alignment buffer, and we're 1274 * writing data, copy the real data out. 1275 */ 1276 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) 1277 bcopy(req->real_data, req->data, req->real_length); 1278 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map, 1279 BUS_DMASYNC_PREWRITE); 1280 } 1281 1282 pt_req = &(req_pkt->gen_req_pkt.pt_req); 1283 pt_req->sg_list = (TW_UINT8 *)segs; 1284 pt_req->sgl_entries += (nsegments - 1); 1285 error = tw_cl_fw_passthru(&(sc->ctlr_handle), req_pkt, 1286 &(req->req_handle)); 1287 } else { 1288 struct tw_cl_scsi_req_packet *scsi_req; 1289 1290 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) 1291 bus_dmamap_sync(sc->dma_tag, req->dma_map, 1292 BUS_DMASYNC_PREREAD); 1293 1294 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) { 1295 /* 1296 * If we're using an alignment buffer, and we're 1297 * writing data, copy the real data out. 1298 */ 1299 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) 1300 bcopy(req->real_data, req->data, req->real_length); 1301 bus_dmamap_sync(sc->dma_tag, req->dma_map, 1302 BUS_DMASYNC_PREWRITE); 1303 } 1304 1305 scsi_req = &(req_pkt->gen_req_pkt.scsi_req); 1306 scsi_req->sg_list = (TW_UINT8 *)segs; 1307 scsi_req->sgl_entries += (nsegments - 1); 1308 error = tw_cl_start_io(&(sc->ctlr_handle), req_pkt, 1309 &(req->req_handle)); 1310 } 1311 1312 out: 1313 if (error) { 1314 req->error_code = error; 1315 req_pkt->tw_osl_callback(&(req->req_handle)); 1316 /* 1317 * If the caller had been returned EINPROGRESS, and he has 1318 * registered a callback for handling completion, the callback 1319 * will never get called because we were unable to submit the 1320 * request. So, free up the request right here. 1321 */ 1322 if (req->flags & TW_OSLI_REQ_FLAGS_IN_PROGRESS) 1323 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q); 1324 } 1325 } 1326 1327 1328 1329 /* 1330 * Function name: twa_map_load_callback 1331 * Description: Callback of bus_dmamap_load for the buffer associated 1332 * with a cmd pkt. 1333 * 1334 * Input: arg -- ptr to variable to hold phys addr 1335 * segs -- ptr to a list of segment descriptors 1336 * nsegments--# of segments 1337 * error -- 0 if no errors encountered before callback, 1338 * non-zero if errors were encountered 1339 * Output: None 1340 * Return value: None 1341 */ 1342 static TW_VOID 1343 twa_map_load_callback(TW_VOID *arg, bus_dma_segment_t *segs, 1344 TW_INT32 nsegments, TW_INT32 error) 1345 { 1346 *((bus_addr_t *)arg) = segs[0].ds_addr; 1347 } 1348 1349 1350 1351 /* 1352 * Function name: tw_osli_map_request 1353 * Description: Maps a cmd pkt and data associated with it, into 1354 * DMA'able memory. 1355 * 1356 * Input: req -- ptr to request pkt 1357 * Output: None 1358 * Return value: 0 -- success 1359 * non-zero-- failure 1360 */ 1361 TW_INT32 1362 tw_osli_map_request(struct tw_osli_req_context *req) 1363 { 1364 struct twa_softc *sc = req->ctlr; 1365 TW_INT32 error = 0; 1366 1367 tw_osli_dbg_dprintf(10, sc, "entered"); 1368 1369 /* If the command involves data, map that too. */ 1370 if (req->data != NULL) { 1371 /* 1372 * It's sufficient for the data pointer to be 4-byte aligned 1373 * to work with 9000. However, if 4-byte aligned addresses 1374 * are passed to bus_dmamap_load, we can get back sg elements 1375 * that are not 512-byte multiples in size. So, we will let 1376 * only those buffers that are 512-byte aligned to pass 1377 * through, and bounce the rest, so as to make sure that we 1378 * always get back sg elements that are 512-byte multiples 1379 * in size. 1380 */ 1381 if (((vm_offset_t)req->data % sc->sg_size_factor) || 1382 (req->length % sc->sg_size_factor)) { 1383 req->flags |= TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED; 1384 /* Save original data pointer and length. */ 1385 req->real_data = req->data; 1386 req->real_length = req->length; 1387 req->length = (req->length + 1388 (sc->sg_size_factor - 1)) & 1389 ~(sc->sg_size_factor - 1); 1390 req->data = kmalloc(req->length, TW_OSLI_MALLOC_CLASS, 1391 M_NOWAIT); 1392 if (req->data == NULL) { 1393 tw_osli_printf(sc, "error = %d", 1394 TW_CL_SEVERITY_ERROR_STRING, 1395 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 1396 0x201E, 1397 "Failed to allocate memory " 1398 "for bounce buffer", 1399 ENOMEM); 1400 /* Restore original data pointer and length. */ 1401 req->data = req->real_data; 1402 req->length = req->real_length; 1403 return(ENOMEM); 1404 } 1405 } 1406 1407 /* 1408 * Map the data buffer into bus space and build the SG list. 1409 */ 1410 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) { 1411 /* Lock against multiple simultaneous ioctl calls. */ 1412 spin_lock(sc->io_lock); 1413 error = bus_dmamap_load(sc->ioctl_tag, sc->ioctl_map, 1414 req->data, req->length, 1415 twa_map_load_data_callback, req, 1416 BUS_DMA_WAITOK); 1417 spin_unlock(sc->io_lock); 1418 } else { 1419 /* 1420 * There's only one CAM I/O thread running at a time. 1421 * So, there's no need to hold the io_lock. 1422 */ 1423 error = bus_dmamap_load(sc->dma_tag, req->dma_map, 1424 req->data, req->length, 1425 twa_map_load_data_callback, req, 1426 BUS_DMA_WAITOK); 1427 } 1428 1429 if (!error) 1430 error = req->error_code; 1431 else { 1432 if (error == EINPROGRESS) { 1433 /* 1434 * Specifying sc->io_lock as the lockfuncarg 1435 * in ...tag_create should protect the access 1436 * of ...FLAGS_MAPPED from the callback. 1437 */ 1438 spin_lock(sc->io_lock); 1439 if (!(req->flags & TW_OSLI_REQ_FLAGS_MAPPED)) 1440 req->flags |= TW_OSLI_REQ_FLAGS_IN_PROGRESS; 1441 tw_osli_disallow_new_requests(sc, &(req->req_handle)); 1442 spin_unlock(sc->io_lock); 1443 error = 0; 1444 } else { 1445 tw_osli_printf(sc, "error = %d", 1446 TW_CL_SEVERITY_ERROR_STRING, 1447 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER, 1448 0x9999, 1449 "Failed to map DMA memory " 1450 "for I/O request", 1451 error); 1452 req->flags |= TW_OSLI_REQ_FLAGS_FAILED; 1453 /* Free alignment buffer if it was used. */ 1454 if (req->flags & 1455 TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) { 1456 kfree(req->data, TW_OSLI_MALLOC_CLASS); 1457 /* 1458 * Restore original data pointer 1459 * and length. 1460 */ 1461 req->data = req->real_data; 1462 req->length = req->real_length; 1463 } 1464 } 1465 } 1466 1467 } else { 1468 /* Mark the request as currently being processed. */ 1469 req->state = TW_OSLI_REQ_STATE_BUSY; 1470 /* Move the request into the busy queue. */ 1471 tw_osli_req_q_insert_tail(req, TW_OSLI_BUSY_Q); 1472 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) 1473 error = tw_cl_fw_passthru(&sc->ctlr_handle, 1474 &(req->req_pkt), &(req->req_handle)); 1475 else 1476 error = tw_cl_start_io(&sc->ctlr_handle, 1477 &(req->req_pkt), &(req->req_handle)); 1478 if (error) { 1479 req->error_code = error; 1480 req->req_pkt.tw_osl_callback(&(req->req_handle)); 1481 } 1482 } 1483 return(error); 1484 } 1485 1486 1487 1488 /* 1489 * Function name: tw_osli_unmap_request 1490 * Description: Undoes the mapping done by tw_osli_map_request. 1491 * 1492 * Input: req -- ptr to request pkt 1493 * Output: None 1494 * Return value: None 1495 */ 1496 TW_VOID 1497 tw_osli_unmap_request(struct tw_osli_req_context *req) 1498 { 1499 struct twa_softc *sc = req->ctlr; 1500 1501 tw_osli_dbg_dprintf(10, sc, "entered"); 1502 1503 /* If the command involved data, unmap that too. */ 1504 if (req->data != NULL) { 1505 if (req->flags & TW_OSLI_REQ_FLAGS_PASSTHRU) { 1506 /* Lock against multiple simultaneous ioctl calls. */ 1507 spin_lock(sc->io_lock); 1508 1509 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) { 1510 bus_dmamap_sync(sc->ioctl_tag, 1511 sc->ioctl_map, BUS_DMASYNC_POSTREAD); 1512 1513 /* 1514 * If we are using a bounce buffer, and we are 1515 * reading data, copy the real data in. 1516 */ 1517 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) 1518 bcopy(req->data, req->real_data, 1519 req->real_length); 1520 } 1521 1522 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) 1523 bus_dmamap_sync(sc->ioctl_tag, sc->ioctl_map, 1524 BUS_DMASYNC_POSTWRITE); 1525 1526 bus_dmamap_unload(sc->ioctl_tag, sc->ioctl_map); 1527 1528 spin_unlock(sc->io_lock); 1529 } else { 1530 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_IN) { 1531 bus_dmamap_sync(sc->dma_tag, 1532 req->dma_map, BUS_DMASYNC_POSTREAD); 1533 1534 /* 1535 * If we are using a bounce buffer, and we are 1536 * reading data, copy the real data in. 1537 */ 1538 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) 1539 bcopy(req->data, req->real_data, 1540 req->real_length); 1541 } 1542 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_OUT) 1543 bus_dmamap_sync(sc->dma_tag, req->dma_map, 1544 BUS_DMASYNC_POSTWRITE); 1545 1546 bus_dmamap_unload(sc->dma_tag, req->dma_map); 1547 } 1548 } 1549 1550 /* Free alignment buffer if it was used. */ 1551 if (req->flags & TW_OSLI_REQ_FLAGS_DATA_COPY_NEEDED) { 1552 kfree(req->data, TW_OSLI_MALLOC_CLASS); 1553 /* Restore original data pointer and length. */ 1554 req->data = req->real_data; 1555 req->length = req->real_length; 1556 } 1557 } 1558 1559 1560 1561 #ifdef TW_OSL_DEBUG 1562 1563 TW_VOID twa_report_stats(TW_VOID); 1564 TW_VOID twa_reset_stats(TW_VOID); 1565 TW_VOID tw_osli_print_ctlr_stats(struct twa_softc *sc); 1566 TW_VOID twa_print_req_info(struct tw_osli_req_context *req); 1567 1568 1569 /* 1570 * Function name: twa_report_stats 1571 * Description: For being called from ddb. Calls functions that print 1572 * OSL and CL internal stats for the controller. 1573 * 1574 * Input: None 1575 * Output: None 1576 * Return value: None 1577 */ 1578 TW_VOID 1579 twa_report_stats(TW_VOID) 1580 { 1581 struct twa_softc *sc; 1582 TW_INT32 i; 1583 1584 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) { 1585 tw_osli_print_ctlr_stats(sc); 1586 tw_cl_print_ctlr_stats(&sc->ctlr_handle); 1587 } 1588 } 1589 1590 1591 1592 /* 1593 * Function name: tw_osli_print_ctlr_stats 1594 * Description: For being called from ddb. Prints OSL controller stats 1595 * 1596 * Input: sc -- ptr to OSL internal controller context 1597 * Output: None 1598 * Return value: None 1599 */ 1600 TW_VOID 1601 tw_osli_print_ctlr_stats(struct twa_softc *sc) 1602 { 1603 twa_printf(sc, "osl_ctlr_ctxt = %p\n", sc); 1604 twa_printf(sc, "OSLq type current max\n"); 1605 twa_printf(sc, "free %04d %04d\n", 1606 sc->q_stats[TW_OSLI_FREE_Q].cur_len, 1607 sc->q_stats[TW_OSLI_FREE_Q].max_len); 1608 twa_printf(sc, "busy %04d %04d\n", 1609 sc->q_stats[TW_OSLI_BUSY_Q].cur_len, 1610 sc->q_stats[TW_OSLI_BUSY_Q].max_len); 1611 } 1612 1613 1614 1615 /* 1616 * Function name: twa_print_req_info 1617 * Description: For being called from ddb. Calls functions that print 1618 * OSL and CL internal details for the request. 1619 * 1620 * Input: req -- ptr to OSL internal request context 1621 * Output: None 1622 * Return value: None 1623 */ 1624 TW_VOID 1625 twa_print_req_info(struct tw_osli_req_context *req) 1626 { 1627 struct twa_softc *sc = req->ctlr; 1628 1629 twa_printf(sc, "OSL details for request:\n"); 1630 twa_printf(sc, "osl_req_ctxt = %p, cl_req_ctxt = %p\n" 1631 "data = %p, length = 0x%x, real_data = %p, real_length = 0x%x\n" 1632 "state = 0x%x, flags = 0x%x, error = 0x%x, orig_req = %p\n" 1633 "next_req = %p, prev_req = %p, dma_map = %p\n", 1634 req->req_handle.osl_req_ctxt, req->req_handle.cl_req_ctxt, 1635 req->data, req->length, req->real_data, req->real_length, 1636 req->state, req->flags, req->error_code, req->orig_req, 1637 req->link.next, req->link.prev, req->dma_map); 1638 tw_cl_print_req_info(&(req->req_handle)); 1639 } 1640 1641 1642 1643 /* 1644 * Function name: twa_reset_stats 1645 * Description: For being called from ddb. 1646 * Resets some OSL controller stats. 1647 * 1648 * Input: None 1649 * Output: None 1650 * Return value: None 1651 */ 1652 TW_VOID 1653 twa_reset_stats(TW_VOID) 1654 { 1655 struct twa_softc *sc; 1656 TW_INT32 i; 1657 1658 for (i = 0; (sc = devclass_get_softc(twa_devclass, i)) != NULL; i++) { 1659 sc->q_stats[TW_OSLI_FREE_Q].max_len = 0; 1660 sc->q_stats[TW_OSLI_BUSY_Q].max_len = 0; 1661 tw_cl_reset_stats(&sc->ctlr_handle); 1662 } 1663 } 1664 1665 #endif /* TW_OSL_DEBUG */ 1666