1 /* 2 * Copyright (c) HighPoint Technologies, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <dev/hptrr/hptrr_config.h> 31 /* $Id: osm_bsd.c,v 1.27 2007/11/22 07:35:49 gmm Exp $ 32 * 33 * HighPoint RAID Driver for FreeBSD 34 * Copyright (C) 2005 HighPoint Technologies, Inc. All Rights Reserved. 35 */ 36 #include <dev/hptrr/os_bsd.h> 37 #include <dev/hptrr/hptintf.h> 38 39 static int attach_generic = 0; 40 TUNABLE_INT("hw.hptrr.attach_generic", &attach_generic); 41 42 static int hpt_probe(device_t dev) 43 { 44 PCI_ID pci_id; 45 HIM *him; 46 int i; 47 PHBA hba; 48 49 /* Some of supported chips are used not only by HPT. */ 50 if (pci_get_vendor(dev) != 0x1103 && !attach_generic) 51 return (ENXIO); 52 for (him = him_list; him; him = him->next) { 53 for (i=0; him->get_supported_device_id(i, &pci_id); i++) { 54 if ((pci_get_vendor(dev) == pci_id.vid) && 55 (pci_get_device(dev) == pci_id.did)){ 56 KdPrint(("hpt_probe: adapter at PCI %d:%d:%d, IRQ %d", 57 pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev), pci_get_irq(dev) 58 )); 59 device_set_desc(dev, him->name); 60 hba = (PHBA)device_get_softc(dev); 61 memset(hba, 0, sizeof(HBA)); 62 hba->ext_type = EXT_TYPE_HBA; 63 hba->ldm_adapter.him = him; 64 return 0; 65 } 66 } 67 } 68 69 return (ENXIO); 70 } 71 72 static int hpt_attach(device_t dev) 73 { 74 PHBA hba = (PHBA)device_get_softc(dev); 75 HIM *him = hba->ldm_adapter.him; 76 PCI_ID pci_id; 77 HPT_UINT size; 78 PVBUS vbus; 79 PVBUS_EXT vbus_ext; 80 81 KdPrint(("hpt_attach(%d/%d/%d)", pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev))); 82 83 #if __FreeBSD_version >=440000 84 pci_enable_busmaster(dev); 85 #endif 86 87 pci_id.vid = pci_get_vendor(dev); 88 pci_id.did = pci_get_device(dev); 89 pci_id.rev = pci_get_revid(dev); 90 91 size = him->get_adapter_size(&pci_id); 92 hba->ldm_adapter.him_handle = malloc(size, M_DEVBUF, M_WAITOK); 93 if (!hba->ldm_adapter.him_handle) 94 return ENXIO; 95 96 hba->pcidev = dev; 97 hba->pciaddr.tree = 0; 98 hba->pciaddr.bus = pci_get_bus(dev); 99 hba->pciaddr.device = pci_get_slot(dev); 100 hba->pciaddr.function = pci_get_function(dev); 101 102 if (!him->create_adapter(&pci_id, hba->pciaddr, hba->ldm_adapter.him_handle, hba)) { 103 free(hba->ldm_adapter.him_handle, M_DEVBUF); 104 return -1; 105 } 106 107 os_printk("adapter at PCI %d:%d:%d, IRQ %d", 108 hba->pciaddr.bus, hba->pciaddr.device, hba->pciaddr.function, pci_get_irq(dev)); 109 110 if (!ldm_register_adapter(&hba->ldm_adapter)) { 111 size = ldm_get_vbus_size(); 112 vbus_ext = malloc(sizeof(VBUS_EXT) + size, M_DEVBUF, M_WAITOK); 113 if (!vbus_ext) { 114 free(hba->ldm_adapter.him_handle, M_DEVBUF); 115 return -1; 116 } 117 memset(vbus_ext, 0, sizeof(VBUS_EXT)); 118 vbus_ext->ext_type = EXT_TYPE_VBUS; 119 ldm_create_vbus((PVBUS)vbus_ext->vbus, vbus_ext); 120 ldm_register_adapter(&hba->ldm_adapter); 121 } 122 123 ldm_for_each_vbus(vbus, vbus_ext) { 124 if (hba->ldm_adapter.vbus==vbus) { 125 hba->vbus_ext = vbus_ext; 126 hba->next = vbus_ext->hba_list; 127 vbus_ext->hba_list = hba; 128 break; 129 } 130 } 131 return 0; 132 } 133 134 /* 135 * Maybe we'd better to use the bus_dmamem_alloc to alloc DMA memory, 136 * but there are some problems currently (alignment, etc). 137 */ 138 static __inline void *__get_free_pages(int order) 139 { 140 /* don't use low memory - other devices may get starved */ 141 return contigmalloc(PAGE_SIZE<<order, 142 M_DEVBUF, M_WAITOK, BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR, PAGE_SIZE, 0); 143 } 144 145 static __inline void free_pages(void *p, int order) 146 { 147 contigfree(p, PAGE_SIZE<<order, M_DEVBUF); 148 } 149 150 static int hpt_alloc_mem(PVBUS_EXT vbus_ext) 151 { 152 PHBA hba; 153 struct freelist *f; 154 HPT_UINT i; 155 void **p; 156 157 for (hba = vbus_ext->hba_list; hba; hba = hba->next) 158 hba->ldm_adapter.him->get_meminfo(hba->ldm_adapter.him_handle); 159 160 ldm_get_mem_info((PVBUS)vbus_ext->vbus, 0); 161 162 for (f=vbus_ext->freelist_head; f; f=f->next) { 163 KdPrint(("%s: %d*%d=%d bytes", 164 f->tag, f->count, f->size, f->count*f->size)); 165 for (i=0; i<f->count; i++) { 166 p = (void **)malloc(f->size, M_DEVBUF, M_WAITOK); 167 if (!p) return (ENXIO); 168 *p = f->head; 169 f->head = p; 170 } 171 } 172 173 for (f=vbus_ext->freelist_dma_head; f; f=f->next) { 174 int order, size, j; 175 176 HPT_ASSERT((f->size & (f->alignment-1))==0); 177 178 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ; 179 180 KdPrint(("%s: %d*%d=%d bytes, order %d", 181 f->tag, f->count, f->size, f->count*f->size, order)); 182 HPT_ASSERT(f->alignment<=PAGE_SIZE); 183 184 for (i=0; i<f->count;) { 185 p = (void **)__get_free_pages(order); 186 if (!p) return -1; 187 for (j = size/f->size; j && i<f->count; i++,j--) { 188 *p = f->head; 189 *(BUS_ADDRESS *)(p+1) = (BUS_ADDRESS)vtophys(p); 190 f->head = p; 191 p = (void **)((unsigned long)p + f->size); 192 } 193 } 194 } 195 196 HPT_ASSERT(PAGE_SIZE==DMAPOOL_PAGE_SIZE); 197 198 for (i=0; i<os_max_cache_pages; i++) { 199 p = (void **)__get_free_pages(0); 200 if (!p) return -1; 201 HPT_ASSERT(((HPT_UPTR)p & (DMAPOOL_PAGE_SIZE-1))==0); 202 dmapool_put_page((PVBUS)vbus_ext->vbus, p, (BUS_ADDRESS)vtophys(p)); 203 } 204 205 return 0; 206 } 207 208 static void hpt_free_mem(PVBUS_EXT vbus_ext) 209 { 210 struct freelist *f; 211 void *p; 212 int i; 213 BUS_ADDRESS bus; 214 215 for (f=vbus_ext->freelist_head; f; f=f->next) { 216 #if DBG 217 if (f->count!=f->reserved_count) { 218 KdPrint(("memory leak for freelist %s (%d/%d)", f->tag, f->count, f->reserved_count)); 219 } 220 #endif 221 while ((p=freelist_get(f))) 222 free(p, M_DEVBUF); 223 } 224 225 for (i=0; i<os_max_cache_pages; i++) { 226 p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus); 227 HPT_ASSERT(p); 228 free_pages(p, 0); 229 } 230 231 for (f=vbus_ext->freelist_dma_head; f; f=f->next) { 232 int order, size; 233 #if DBG 234 if (f->count!=f->reserved_count) { 235 KdPrint(("memory leak for dma freelist %s (%d/%d)", f->tag, f->count, f->reserved_count)); 236 } 237 #endif 238 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ; 239 240 while ((p=freelist_get_dma(f, &bus))) { 241 if (order) 242 free_pages(p, order); 243 else { 244 /* can't free immediately since other blocks in this page may still be in the list */ 245 if (((HPT_UPTR)p & (PAGE_SIZE-1))==0) 246 dmapool_put_page((PVBUS)vbus_ext->vbus, p, bus); 247 } 248 } 249 } 250 251 while ((p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus))) 252 free_pages(p, 0); 253 } 254 255 static int hpt_init_vbus(PVBUS_EXT vbus_ext) 256 { 257 PHBA hba; 258 259 for (hba = vbus_ext->hba_list; hba; hba = hba->next) 260 if (!hba->ldm_adapter.him->initialize(hba->ldm_adapter.him_handle)) { 261 KdPrint(("fail to initialize %p", hba)); 262 return -1; 263 } 264 265 ldm_initialize_vbus((PVBUS)vbus_ext->vbus, &vbus_ext->hba_list->ldm_adapter); 266 return 0; 267 } 268 269 static void hpt_flush_done(PCOMMAND pCmd) 270 { 271 PVDEV vd = pCmd->target; 272 273 if (mIsArray(vd->type) && vd->u.array.transform && vd!=vd->u.array.transform->target) { 274 vd = vd->u.array.transform->target; 275 HPT_ASSERT(vd); 276 pCmd->target = vd; 277 pCmd->Result = RETURN_PENDING; 278 vdev_queue_cmd(pCmd); 279 return; 280 } 281 282 *(int *)pCmd->priv = 1; 283 wakeup(pCmd); 284 } 285 286 /* 287 * flush a vdev (without retry). 288 */ 289 static int hpt_flush_vdev(PVBUS_EXT vbus_ext, PVDEV vd) 290 { 291 PCOMMAND pCmd; 292 int result = 0, done; 293 HPT_UINT count; 294 295 KdPrint(("flusing dev %p", vd)); 296 297 hpt_lock_vbus(vbus_ext); 298 299 if (mIsArray(vd->type) && vd->u.array.transform) 300 count = MAX(vd->u.array.transform->source->cmds_per_request, 301 vd->u.array.transform->target->cmds_per_request); 302 else 303 count = vd->cmds_per_request; 304 305 pCmd = ldm_alloc_cmds(vd->vbus, count); 306 307 if (!pCmd) { 308 hpt_unlock_vbus(vbus_ext); 309 return -1; 310 } 311 312 pCmd->type = CMD_TYPE_FLUSH; 313 pCmd->flags.hard_flush = 1; 314 pCmd->target = vd; 315 pCmd->done = hpt_flush_done; 316 done = 0; 317 pCmd->priv = &done; 318 319 ldm_queue_cmd(pCmd); 320 321 if (!done) { 322 while (hpt_sleep(vbus_ext, pCmd, PPAUSE, "hptfls", HPT_OSM_TIMEOUT)) { 323 ldm_reset_vbus(vd->vbus); 324 } 325 } 326 327 KdPrint(("flush result %d", pCmd->Result)); 328 329 if (pCmd->Result!=RETURN_SUCCESS) 330 result = -1; 331 332 ldm_free_cmds(pCmd); 333 334 hpt_unlock_vbus(vbus_ext); 335 336 return result; 337 } 338 339 static void hpt_stop_tasks(PVBUS_EXT vbus_ext); 340 static void hpt_shutdown_vbus(PVBUS_EXT vbus_ext, int howto) 341 { 342 PVBUS vbus = (PVBUS)vbus_ext->vbus; 343 PHBA hba; 344 int i; 345 346 KdPrint(("hpt_shutdown_vbus")); 347 348 /* stop all ctl tasks and disable the worker taskqueue */ 349 hpt_stop_tasks(vbus_ext); 350 vbus_ext->worker.ta_context = 0; 351 352 /* flush devices */ 353 for (i=0; i<osm_max_targets; i++) { 354 PVDEV vd = ldm_find_target(vbus, i); 355 if (vd) { 356 /* retry once */ 357 if (hpt_flush_vdev(vbus_ext, vd)) 358 hpt_flush_vdev(vbus_ext, vd); 359 } 360 } 361 362 hpt_lock_vbus(vbus_ext); 363 ldm_shutdown(vbus); 364 hpt_unlock_vbus(vbus_ext); 365 366 ldm_release_vbus(vbus); 367 368 for (hba=vbus_ext->hba_list; hba; hba=hba->next) 369 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle); 370 371 hpt_free_mem(vbus_ext); 372 373 while ((hba=vbus_ext->hba_list)) { 374 vbus_ext->hba_list = hba->next; 375 free(hba->ldm_adapter.him_handle, M_DEVBUF); 376 } 377 378 free(vbus_ext, M_DEVBUF); 379 KdPrint(("hpt_shutdown_vbus done")); 380 } 381 382 static void __hpt_do_tasks(PVBUS_EXT vbus_ext) 383 { 384 OSM_TASK *tasks; 385 386 tasks = vbus_ext->tasks; 387 vbus_ext->tasks = 0; 388 389 while (tasks) { 390 OSM_TASK *t = tasks; 391 tasks = t->next; 392 t->next = 0; 393 t->func(vbus_ext->vbus, t->data); 394 } 395 } 396 397 static void hpt_do_tasks(PVBUS_EXT vbus_ext, int pending) 398 { 399 if(vbus_ext){ 400 hpt_lock_vbus(vbus_ext); 401 __hpt_do_tasks(vbus_ext); 402 hpt_unlock_vbus(vbus_ext); 403 } 404 } 405 406 static void hpt_action(struct cam_sim *sim, union ccb *ccb); 407 static void hpt_poll(struct cam_sim *sim); 408 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg); 409 static void hpt_pci_intr(void *arg); 410 411 static __inline POS_CMDEXT cmdext_get(PVBUS_EXT vbus_ext) 412 { 413 POS_CMDEXT p = vbus_ext->cmdext_list; 414 if (p) 415 vbus_ext->cmdext_list = p->next; 416 return p; 417 } 418 419 static __inline void cmdext_put(POS_CMDEXT p) 420 { 421 p->next = p->vbus_ext->cmdext_list; 422 p->vbus_ext->cmdext_list = p; 423 } 424 425 static void hpt_timeout(void *arg) 426 { 427 PCOMMAND pCmd = (PCOMMAND)arg; 428 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv; 429 430 KdPrint(("pCmd %p timeout", pCmd)); 431 432 ldm_reset_vbus((PVBUS)ext->vbus_ext->vbus); 433 } 434 435 static void os_cmddone(PCOMMAND pCmd) 436 { 437 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv; 438 union ccb *ccb = ext->ccb; 439 440 KdPrint(("os_cmddone(%p, %d)", pCmd, pCmd->Result)); 441 442 untimeout(hpt_timeout, pCmd, ccb->ccb_h.timeout_ch); 443 444 switch(pCmd->Result) { 445 case RETURN_SUCCESS: 446 ccb->ccb_h.status = CAM_REQ_CMP; 447 break; 448 case RETURN_BAD_DEVICE: 449 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 450 break; 451 case RETURN_DEVICE_BUSY: 452 ccb->ccb_h.status = CAM_BUSY; 453 break; 454 case RETURN_INVALID_REQUEST: 455 ccb->ccb_h.status = CAM_REQ_INVALID; 456 break; 457 case RETURN_SELECTION_TIMEOUT: 458 ccb->ccb_h.status = CAM_SEL_TIMEOUT; 459 break; 460 case RETURN_RETRY: 461 ccb->ccb_h.status = CAM_BUSY; 462 break; 463 default: 464 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; 465 break; 466 } 467 468 if (pCmd->flags.data_in) { 469 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTREAD); 470 } 471 else if (pCmd->flags.data_out) { 472 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTWRITE); 473 } 474 475 bus_dmamap_unload(ext->vbus_ext->io_dmat, ext->dma_map); 476 477 cmdext_put(ext); 478 ldm_free_cmds(pCmd); 479 xpt_done(ccb); 480 } 481 482 static int os_buildsgl(PCOMMAND pCmd, PSG pSg, int logical) 483 { 484 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv; 485 union ccb *ccb = ext->ccb; 486 bus_dma_segment_t *sgList = (bus_dma_segment_t *)ccb->csio.data_ptr; 487 int idx; 488 489 if(logical) { 490 if (ccb->ccb_h.flags & CAM_DATA_PHYS) 491 panic("physical address unsupported"); 492 493 if (ccb->ccb_h.flags & CAM_SCATTER_VALID) { 494 if (ccb->ccb_h.flags & CAM_SG_LIST_PHYS) 495 panic("physical address unsupported"); 496 497 for (idx = 0; idx < ccb->csio.sglist_cnt; idx++) { 498 os_set_sgptr(&pSg[idx], (HPT_U8 *)(HPT_UPTR)sgList[idx].ds_addr); 499 pSg[idx].size = sgList[idx].ds_len; 500 pSg[idx].eot = (idx==ccb->csio.sglist_cnt-1)? 1 : 0; 501 } 502 } 503 else { 504 os_set_sgptr(pSg, (HPT_U8 *)ccb->csio.data_ptr); 505 pSg->size = ccb->csio.dxfer_len; 506 pSg->eot = 1; 507 } 508 return TRUE; 509 } 510 511 /* since we have provided physical sg, nobody will ask us to build physical sg */ 512 HPT_ASSERT(0); 513 return FALSE; 514 } 515 516 static void hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 517 { 518 PCOMMAND pCmd = (PCOMMAND)arg; 519 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv; 520 PSG psg = pCmd->psg; 521 int idx; 522 523 HPT_ASSERT(pCmd->flags.physical_sg); 524 525 if (error || nsegs == 0) 526 panic("busdma error"); 527 528 HPT_ASSERT(nsegs<=os_max_sg_descriptors); 529 530 for (idx = 0; idx < nsegs; idx++, psg++) { 531 psg->addr.bus = segs[idx].ds_addr; 532 psg->size = segs[idx].ds_len; 533 psg->eot = 0; 534 } 535 psg[-1].eot = 1; 536 537 if (pCmd->flags.data_in) { 538 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_PREREAD); 539 } 540 else if (pCmd->flags.data_out) { 541 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_PREWRITE); 542 } 543 544 ext->ccb->ccb_h.timeout_ch = timeout(hpt_timeout, pCmd, HPT_OSM_TIMEOUT); 545 ldm_queue_cmd(pCmd); 546 } 547 548 static void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb) 549 { 550 PVBUS vbus = (PVBUS)vbus_ext->vbus; 551 PVDEV vd; 552 PCOMMAND pCmd; 553 POS_CMDEXT ext; 554 HPT_U8 *cdb; 555 556 if (ccb->ccb_h.flags & CAM_CDB_POINTER) 557 cdb = ccb->csio.cdb_io.cdb_ptr; 558 else 559 cdb = ccb->csio.cdb_io.cdb_bytes; 560 561 KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x", 562 ccb, 563 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 564 *(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8] 565 )); 566 567 /* ccb->ccb_h.path_id is not our bus id - don't check it */ 568 if (ccb->ccb_h.target_lun != 0 || 569 ccb->ccb_h.target_id >= osm_max_targets || 570 (ccb->ccb_h.flags & CAM_CDB_PHYS)) 571 { 572 ccb->ccb_h.status = CAM_TID_INVALID; 573 xpt_done(ccb); 574 return; 575 } 576 577 vd = ldm_find_target(vbus, ccb->ccb_h.target_id); 578 579 if (!vd) { 580 ccb->ccb_h.status = CAM_TID_INVALID; 581 xpt_done(ccb); 582 return; 583 } 584 585 switch (cdb[0]) { 586 case TEST_UNIT_READY: 587 case START_STOP_UNIT: 588 case SYNCHRONIZE_CACHE: 589 ccb->ccb_h.status = CAM_REQ_CMP; 590 break; 591 592 case INQUIRY: 593 { 594 PINQUIRYDATA inquiryData; 595 memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len); 596 inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr; 597 598 inquiryData->AdditionalLength = 31; 599 inquiryData->CommandQueue = 1; 600 memcpy(&inquiryData->VendorId, "HPT ", 8); 601 memcpy(&inquiryData->ProductId, "DISK 0_0 ", 16); 602 603 if (vd->target_id / 10) { 604 inquiryData->ProductId[7] = (vd->target_id % 100) / 10 + '0'; 605 inquiryData->ProductId[8] = (vd->target_id % 100) % 10 + '0'; 606 } 607 else 608 inquiryData->ProductId[7] = (vd->target_id % 100) % 10 + '0'; 609 610 memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4); 611 612 ccb->ccb_h.status = CAM_REQ_CMP; 613 } 614 break; 615 616 case READ_CAPACITY: 617 { 618 HPT_U8 *rbuf = ccb->csio.data_ptr; 619 HPT_U32 cap; 620 621 if (vd->capacity>0xfffffffful) 622 cap = 0xfffffffful; 623 else 624 cap = vd->capacity - 1; 625 626 rbuf[0] = (HPT_U8)(cap>>24); 627 rbuf[1] = (HPT_U8)(cap>>16); 628 rbuf[2] = (HPT_U8)(cap>>8); 629 rbuf[3] = (HPT_U8)cap; 630 rbuf[4] = 0; 631 rbuf[5] = 0; 632 rbuf[6] = 2; 633 rbuf[7] = 0; 634 635 ccb->ccb_h.status = CAM_REQ_CMP; 636 break; 637 } 638 639 case SERVICE_ACTION_IN: 640 { 641 HPT_U8 *rbuf = ccb->csio.data_ptr; 642 HPT_U64 cap = vd->capacity - 1; 643 644 rbuf[0] = (HPT_U8)(cap>>56); 645 rbuf[1] = (HPT_U8)(cap>>48); 646 rbuf[2] = (HPT_U8)(cap>>40); 647 rbuf[3] = (HPT_U8)(cap>>32); 648 rbuf[4] = (HPT_U8)(cap>>24); 649 rbuf[5] = (HPT_U8)(cap>>16); 650 rbuf[6] = (HPT_U8)(cap>>8); 651 rbuf[7] = (HPT_U8)cap; 652 rbuf[8] = 0; 653 rbuf[9] = 0; 654 rbuf[10] = 2; 655 rbuf[11] = 0; 656 657 ccb->ccb_h.status = CAM_REQ_CMP; 658 break; 659 } 660 661 case READ_6: 662 case READ_10: 663 case READ_16: 664 case WRITE_6: 665 case WRITE_10: 666 case WRITE_16: 667 case 0x13: 668 case 0x2f: 669 { 670 pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request); 671 if(!pCmd){ 672 KdPrint(("Failed to allocate command!")); 673 ccb->ccb_h.status = CAM_BUSY; 674 break; 675 } 676 677 switch (cdb[0]) { 678 case READ_6: 679 case WRITE_6: 680 case 0x13: 681 pCmd->uCmd.Ide.Lba = ((HPT_U32)cdb[1] << 16) | ((HPT_U32)cdb[2] << 8) | (HPT_U32)cdb[3]; 682 pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[4]; 683 break; 684 case READ_16: 685 case WRITE_16: 686 { 687 HPT_U64 block = 688 ((HPT_U64)cdb[2]<<56) | 689 ((HPT_U64)cdb[3]<<48) | 690 ((HPT_U64)cdb[4]<<40) | 691 ((HPT_U64)cdb[5]<<32) | 692 ((HPT_U64)cdb[6]<<24) | 693 ((HPT_U64)cdb[7]<<16) | 694 ((HPT_U64)cdb[8]<<8) | 695 ((HPT_U64)cdb[9]); 696 pCmd->uCmd.Ide.Lba = block; 697 pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8); 698 break; 699 } 700 701 default: 702 pCmd->uCmd.Ide.Lba = (HPT_U32)cdb[5] | ((HPT_U32)cdb[4] << 8) | ((HPT_U32)cdb[3] << 16) | ((HPT_U32)cdb[2] << 24); 703 pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[8] | ((HPT_U16)cdb[7]<<8); 704 break; 705 } 706 707 switch (cdb[0]) { 708 case READ_6: 709 case READ_10: 710 case READ_16: 711 pCmd->flags.data_in = 1; 712 break; 713 case WRITE_6: 714 case WRITE_10: 715 case WRITE_16: 716 pCmd->flags.data_out = 1; 717 break; 718 } 719 pCmd->priv = ext = cmdext_get(vbus_ext); 720 HPT_ASSERT(ext); 721 ext->ccb = ccb; 722 pCmd->target = vd; 723 pCmd->done = os_cmddone; 724 pCmd->buildsgl = os_buildsgl; 725 726 pCmd->psg = ext->psg; 727 728 if (ccb->ccb_h.flags & CAM_SCATTER_VALID) { 729 int idx; 730 bus_dma_segment_t *sgList = (bus_dma_segment_t *)ccb->csio.data_ptr; 731 732 if (ccb->ccb_h.flags & CAM_SG_LIST_PHYS) 733 pCmd->flags.physical_sg = 1; 734 735 for (idx = 0; idx < ccb->csio.sglist_cnt; idx++) { 736 pCmd->psg[idx].addr.bus = sgList[idx].ds_addr; 737 pCmd->psg[idx].size = sgList[idx].ds_len; 738 pCmd->psg[idx].eot = (idx==ccb->csio.sglist_cnt-1)? 1 : 0; 739 } 740 741 ccb->ccb_h.timeout_ch = timeout(hpt_timeout, pCmd, HPT_OSM_TIMEOUT); 742 ldm_queue_cmd(pCmd); 743 } 744 else { 745 int error; 746 pCmd->flags.physical_sg = 1; 747 error = bus_dmamap_load(vbus_ext->io_dmat, 748 ext->dma_map, 749 ccb->csio.data_ptr, ccb->csio.dxfer_len, 750 hpt_io_dmamap_callback, pCmd, 751 BUS_DMA_WAITOK 752 ); 753 KdPrint(("bus_dmamap_load return %d", error)); 754 if (error && error!=EINPROGRESS) { 755 os_printk("bus_dmamap_load error %d", error); 756 cmdext_put(ext); 757 ldm_free_cmds(pCmd); 758 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 759 xpt_done(ccb); 760 } 761 } 762 return; 763 } 764 765 default: 766 ccb->ccb_h.status = CAM_REQ_INVALID; 767 break; 768 } 769 770 xpt_done(ccb); 771 return; 772 } 773 774 static void hpt_action(struct cam_sim *sim, union ccb *ccb) 775 { 776 PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim); 777 778 KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id)); 779 780 switch (ccb->ccb_h.func_code) { 781 782 case XPT_SCSI_IO: 783 hpt_lock_vbus(vbus_ext); 784 hpt_scsi_io(vbus_ext, ccb); 785 hpt_unlock_vbus(vbus_ext); 786 return; 787 788 case XPT_RESET_BUS: 789 hpt_lock_vbus(vbus_ext); 790 ldm_reset_vbus((PVBUS)vbus_ext->vbus); 791 hpt_unlock_vbus(vbus_ext); 792 break; 793 794 case XPT_GET_TRAN_SETTINGS: 795 case XPT_SET_TRAN_SETTINGS: 796 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 797 break; 798 799 case XPT_CALC_GEOMETRY: 800 #if __FreeBSD_version >= 500000 801 cam_calc_geometry(&ccb->ccg, 1); 802 #else 803 ccb->ccg.heads = 255; 804 ccb->ccg.secs_per_track = 63; 805 ccb->ccg.cylinders = ccb->ccg.volume_size / (ccb->ccg.heads * ccb->ccg.secs_per_track); 806 ccb->ccb_h.status = CAM_REQ_CMP; 807 #endif 808 break; 809 810 case XPT_PATH_INQ: 811 { 812 struct ccb_pathinq *cpi = &ccb->cpi; 813 814 cpi->version_num = 1; 815 cpi->hba_inquiry = PI_SDTR_ABLE; 816 cpi->target_sprt = 0; 817 cpi->hba_misc = PIM_NOBUSRESET; 818 cpi->hba_eng_cnt = 0; 819 cpi->max_target = osm_max_targets; 820 cpi->max_lun = 0; 821 cpi->unit_number = cam_sim_unit(sim); 822 cpi->bus_id = cam_sim_bus(sim); 823 cpi->initiator_id = osm_max_targets; 824 cpi->base_transfer_speed = 3300; 825 826 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 827 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN); 828 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 829 cpi->transport = XPORT_SPI; 830 cpi->transport_version = 2; 831 cpi->protocol = PROTO_SCSI; 832 cpi->protocol_version = SCSI_REV_2; 833 cpi->ccb_h.status = CAM_REQ_CMP; 834 break; 835 } 836 837 default: 838 ccb->ccb_h.status = CAM_REQ_INVALID; 839 break; 840 } 841 842 xpt_done(ccb); 843 return; 844 } 845 846 static void hpt_pci_intr(void *arg) 847 { 848 PVBUS_EXT vbus_ext = (PVBUS_EXT)arg; 849 hpt_lock_vbus(vbus_ext); 850 ldm_intr((PVBUS)vbus_ext->vbus); 851 hpt_unlock_vbus(vbus_ext); 852 } 853 854 static void hpt_poll(struct cam_sim *sim) 855 { 856 hpt_pci_intr(cam_sim_softc(sim)); 857 } 858 859 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg) 860 { 861 KdPrint(("hpt_async")); 862 } 863 864 static int hpt_shutdown(device_t dev) 865 { 866 KdPrint(("hpt_shutdown(dev=%p)", dev)); 867 return 0; 868 } 869 870 static int hpt_detach(device_t dev) 871 { 872 /* we don't allow the driver to be unloaded. */ 873 return EBUSY; 874 } 875 876 static void hpt_ioctl_done(struct _IOCTL_ARG *arg) 877 { 878 arg->ioctl_cmnd = 0; 879 wakeup(arg); 880 } 881 882 static void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args) 883 { 884 ioctl_args->result = -1; 885 ioctl_args->done = hpt_ioctl_done; 886 ioctl_args->ioctl_cmnd = (void *)1; 887 888 hpt_lock_vbus(vbus_ext); 889 ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args); 890 891 while (ioctl_args->ioctl_cmnd) { 892 if (hpt_sleep(vbus_ext, ioctl_args, PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0) 893 break; 894 ldm_reset_vbus((PVBUS)vbus_ext->vbus); 895 __hpt_do_tasks(vbus_ext); 896 } 897 898 /* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */ 899 900 hpt_unlock_vbus(vbus_ext); 901 } 902 903 static void hpt_do_ioctl(IOCTL_ARG *ioctl_args) 904 { 905 PVBUS vbus; 906 PVBUS_EXT vbus_ext; 907 908 ldm_for_each_vbus(vbus, vbus_ext) { 909 __hpt_do_ioctl(vbus_ext, ioctl_args); 910 if (ioctl_args->result!=HPT_IOCTL_RESULT_WRONG_VBUS) 911 return; 912 } 913 } 914 915 #define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\ 916 IOCTL_ARG arg;\ 917 arg.dwIoControlCode = code;\ 918 arg.lpInBuffer = inbuf;\ 919 arg.lpOutBuffer = outbuf;\ 920 arg.nInBufferSize = insize;\ 921 arg.nOutBufferSize = outsize;\ 922 arg.lpBytesReturned = 0;\ 923 hpt_do_ioctl(&arg);\ 924 arg.result;\ 925 }) 926 927 #define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff)) 928 929 static int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount) 930 { 931 int i; 932 HPT_U32 count = nMaxCount-1; 933 934 if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES, 935 &count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount)) 936 return -1; 937 938 nMaxCount = (int)pIds[0]; 939 for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1]; 940 return nMaxCount; 941 } 942 943 static int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo) 944 { 945 return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3, 946 &id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3)); 947 } 948 949 /* not belong to this file logically, but we want to use ioctl interface */ 950 static int __hpt_stop_tasks(PVBUS_EXT vbus_ext, DEVICEID id) 951 { 952 LOGICAL_DEVICE_INFO_V3 devinfo; 953 int i, result; 954 DEVICEID param[2] = { id, 0 }; 955 956 if (hpt_get_device_info_v3(id, &devinfo)) 957 return -1; 958 959 if (devinfo.Type!=LDT_ARRAY) 960 return -1; 961 962 if (devinfo.u.array.Flags & ARRAY_FLAG_REBUILDING) 963 param[1] = AS_REBUILD_ABORT; 964 else if (devinfo.u.array.Flags & ARRAY_FLAG_VERIFYING) 965 param[1] = AS_VERIFY_ABORT; 966 else if (devinfo.u.array.Flags & ARRAY_FLAG_INITIALIZING) 967 param[1] = AS_INITIALIZE_ABORT; 968 else if (devinfo.u.array.Flags & ARRAY_FLAG_TRANSFORMING) 969 param[1] = AS_TRANSFORM_ABORT; 970 else 971 return -1; 972 973 KdPrint(("SET_ARRAY_STATE(%x, %d)", param[0], param[1])); 974 result = HPT_DO_IOCTL(HPT_IOCTL_SET_ARRAY_STATE, 975 param, sizeof(param), 0, 0); 976 977 for (i=0; i<devinfo.u.array.nDisk; i++) 978 if (DEVICEID_VALID(devinfo.u.array.Members[i])) 979 __hpt_stop_tasks(vbus_ext, devinfo.u.array.Members[i]); 980 981 return result; 982 } 983 984 static void hpt_stop_tasks(PVBUS_EXT vbus_ext) 985 { 986 DEVICEID ids[32]; 987 int i, count; 988 989 count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0])); 990 991 for (i=0; i<count; i++) 992 __hpt_stop_tasks(vbus_ext, ids[i]); 993 } 994 995 static d_open_t hpt_open; 996 static d_close_t hpt_close; 997 static d_ioctl_t hpt_ioctl; 998 static int hpt_rescan_bus(void); 999 1000 static struct cdevsw hpt_cdevsw = { 1001 .d_open = hpt_open, 1002 .d_close = hpt_close, 1003 .d_ioctl = hpt_ioctl, 1004 .d_name = driver_name, 1005 #if __FreeBSD_version>=503000 1006 .d_version = D_VERSION, 1007 #endif 1008 #if (__FreeBSD_version>=503000 && __FreeBSD_version<600034) 1009 .d_flags = D_NEEDGIANT, 1010 #endif 1011 #if __FreeBSD_version<600034 1012 #if __FreeBSD_version>501000 1013 .d_maj = MAJOR_AUTO, 1014 #else 1015 .d_maj = HPT_DEV_MAJOR, 1016 #endif 1017 #endif 1018 }; 1019 1020 static struct intr_config_hook hpt_ich; 1021 1022 /* 1023 * hpt_final_init will be called after all hpt_attach. 1024 */ 1025 static void hpt_final_init(void *dummy) 1026 { 1027 int i; 1028 PVBUS_EXT vbus_ext; 1029 PVBUS vbus; 1030 PHBA hba; 1031 1032 /* Clear the config hook */ 1033 config_intrhook_disestablish(&hpt_ich); 1034 1035 /* allocate memory */ 1036 i = 0; 1037 ldm_for_each_vbus(vbus, vbus_ext) { 1038 if (hpt_alloc_mem(vbus_ext)) { 1039 os_printk("out of memory"); 1040 return; 1041 } 1042 i++; 1043 } 1044 1045 if (!i) { 1046 if (bootverbose) 1047 os_printk("no controller detected."); 1048 return; 1049 } 1050 1051 /* initializing hardware */ 1052 ldm_for_each_vbus(vbus, vbus_ext) { 1053 /* make timer available here */ 1054 callout_handle_init(&vbus_ext->timer); 1055 if (hpt_init_vbus(vbus_ext)) { 1056 os_printk("fail to initialize hardware"); 1057 break; /* FIXME */ 1058 } 1059 } 1060 1061 /* register CAM interface */ 1062 ldm_for_each_vbus(vbus, vbus_ext) { 1063 struct cam_devq *devq; 1064 struct ccb_setasync ccb; 1065 1066 #if (__FreeBSD_version >= 500000) 1067 mtx_init(&vbus_ext->lock, "hptsleeplock", NULL, MTX_DEF); 1068 #endif 1069 if (bus_dma_tag_create(NULL,/* parent */ 1070 4, /* alignment */ 1071 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */ 1072 BUS_SPACE_MAXADDR, /* lowaddr */ 1073 BUS_SPACE_MAXADDR, /* highaddr */ 1074 NULL, NULL, /* filter, filterarg */ 1075 PAGE_SIZE * (os_max_sg_descriptors-1), /* maxsize */ 1076 os_max_sg_descriptors, /* nsegments */ 1077 0x10000, /* maxsegsize */ 1078 BUS_DMA_WAITOK, /* flags */ 1079 #if __FreeBSD_version>502000 1080 busdma_lock_mutex, /* lockfunc */ 1081 &vbus_ext->lock, /* lockfuncarg */ 1082 #endif 1083 &vbus_ext->io_dmat /* tag */)) 1084 { 1085 return ; 1086 } 1087 1088 for (i=0; i<os_max_queue_comm; i++) { 1089 POS_CMDEXT ext = (POS_CMDEXT)malloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK); 1090 if (!ext) { 1091 os_printk("Can't alloc cmdext(%d)", i); 1092 return ; 1093 } 1094 ext->vbus_ext = vbus_ext; 1095 ext->next = vbus_ext->cmdext_list; 1096 vbus_ext->cmdext_list = ext; 1097 1098 if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) { 1099 os_printk("Can't create dma map(%d)", i); 1100 return ; 1101 } 1102 } 1103 1104 if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) { 1105 os_printk("cam_simq_alloc failed"); 1106 return ; 1107 } 1108 1109 #if __FreeBSD_version > 700025 1110 vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name, 1111 vbus_ext, 0, &Giant, os_max_queue_comm, /*tagged*/8, devq); 1112 #else 1113 vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name, 1114 vbus_ext, 0, os_max_queue_comm, /*tagged*/8, devq); 1115 #endif 1116 1117 if (!vbus_ext->sim) { 1118 os_printk("cam_sim_alloc failed"); 1119 cam_simq_free(devq); 1120 return ; 1121 } 1122 1123 #if __FreeBSD_version > 700044 1124 if (xpt_bus_register(vbus_ext->sim, NULL, 0) != CAM_SUCCESS) { 1125 #else 1126 if (xpt_bus_register(vbus_ext->sim, 0) != CAM_SUCCESS) { 1127 #endif 1128 os_printk("xpt_bus_register failed"); 1129 cam_sim_free(vbus_ext->sim, /*free devq*/ TRUE); 1130 vbus_ext->sim = NULL; 1131 return ; 1132 } 1133 1134 if (xpt_create_path(&vbus_ext->path, /*periph */ NULL, 1135 cam_sim_path(vbus_ext->sim), CAM_TARGET_WILDCARD, 1136 CAM_LUN_WILDCARD) != CAM_REQ_CMP) 1137 { 1138 os_printk("xpt_create_path failed"); 1139 xpt_bus_deregister(cam_sim_path(vbus_ext->sim)); 1140 cam_sim_free(vbus_ext->sim, /*free_devq*/TRUE); 1141 vbus_ext->sim = NULL; 1142 return ; 1143 } 1144 1145 xpt_setup_ccb(&ccb.ccb_h, vbus_ext->path, /*priority*/5); 1146 ccb.ccb_h.func_code = XPT_SASYNC_CB; 1147 ccb.event_enable = AC_LOST_DEVICE; 1148 ccb.callback = hpt_async; 1149 ccb.callback_arg = vbus_ext; 1150 xpt_action((union ccb *)&ccb); 1151 1152 for (hba = vbus_ext->hba_list; hba; hba = hba->next) { 1153 int rid = 0; 1154 if ((hba->irq_res = bus_alloc_resource(hba->pcidev, 1155 SYS_RES_IRQ, &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) 1156 { 1157 os_printk("can't allocate interrupt"); 1158 return ; 1159 } 1160 1161 if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM, 1162 #if __FreeBSD_version > 700025 1163 NULL, hpt_pci_intr, vbus_ext, &hba->irq_handle)) 1164 #else 1165 hpt_pci_intr, vbus_ext, &hba->irq_handle)) 1166 #endif 1167 { 1168 os_printk("can't set up interrupt"); 1169 return ; 1170 } 1171 hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE); 1172 } 1173 1174 vbus_ext->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final, 1175 hpt_shutdown_vbus, vbus_ext, SHUTDOWN_PRI_DEFAULT); 1176 if (!vbus_ext->shutdown_eh) 1177 os_printk("Shutdown event registration failed"); 1178 } 1179 1180 ldm_for_each_vbus(vbus, vbus_ext) { 1181 TASK_INIT(&vbus_ext->worker, 0, (task_fn_t *)hpt_do_tasks, vbus_ext); 1182 if (vbus_ext->tasks) 1183 TASK_ENQUEUE(&vbus_ext->worker); 1184 } 1185 1186 make_dev(&hpt_cdevsw, DRIVER_MINOR, UID_ROOT, GID_OPERATOR, 1187 S_IRUSR | S_IWUSR, "%s", driver_name); 1188 } 1189 1190 #if defined(KLD_MODULE) && (__FreeBSD_version >= 503000) 1191 1192 typedef struct driverlink *driverlink_t; 1193 struct driverlink { 1194 kobj_class_t driver; 1195 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */ 1196 }; 1197 1198 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t; 1199 1200 struct devclass { 1201 TAILQ_ENTRY(devclass) link; 1202 devclass_t parent; /* parent in devclass hierarchy */ 1203 driver_list_t drivers; /* bus devclasses store drivers for bus */ 1204 char *name; 1205 device_t *devices; /* array of devices indexed by unit */ 1206 int maxunit; /* size of devices array */ 1207 }; 1208 1209 static void override_kernel_driver(void) 1210 { 1211 driverlink_t dl, dlfirst; 1212 driver_t *tmpdriver; 1213 devclass_t dc = devclass_find("pci"); 1214 1215 if (dc){ 1216 dlfirst = TAILQ_FIRST(&dc->drivers); 1217 for (dl = dlfirst; dl; dl = TAILQ_NEXT(dl, link)) { 1218 if(strcmp(dl->driver->name, driver_name) == 0) { 1219 tmpdriver=dl->driver; 1220 dl->driver=dlfirst->driver; 1221 dlfirst->driver=tmpdriver; 1222 break; 1223 } 1224 } 1225 } 1226 } 1227 1228 #else 1229 #define override_kernel_driver() 1230 #endif 1231 1232 static void hpt_init(void *dummy) 1233 { 1234 if (bootverbose) 1235 os_printk("%s %s", driver_name_long, driver_ver); 1236 1237 override_kernel_driver(); 1238 init_config(); 1239 1240 hpt_ich.ich_func = hpt_final_init; 1241 hpt_ich.ich_arg = NULL; 1242 if (config_intrhook_establish(&hpt_ich) != 0) { 1243 printf("%s: cannot establish configuration hook\n", 1244 driver_name_long); 1245 } 1246 1247 } 1248 SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL); 1249 1250 /* 1251 * CAM driver interface 1252 */ 1253 static device_method_t driver_methods[] = { 1254 /* Device interface */ 1255 DEVMETHOD(device_probe, hpt_probe), 1256 DEVMETHOD(device_attach, hpt_attach), 1257 DEVMETHOD(device_detach, hpt_detach), 1258 DEVMETHOD(device_shutdown, hpt_shutdown), 1259 DEVMETHOD_END 1260 }; 1261 1262 static driver_t hpt_pci_driver = { 1263 driver_name, 1264 driver_methods, 1265 sizeof(HBA) 1266 }; 1267 1268 static devclass_t hpt_devclass; 1269 1270 #ifndef TARGETNAME 1271 #error "no TARGETNAME found" 1272 #endif 1273 1274 /* use this to make TARGETNAME be expanded */ 1275 #define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6) 1276 #define __MODULE_VERSION(p1, p2) MODULE_VERSION(p1, p2) 1277 #define __MODULE_DEPEND(p1, p2, p3, p4, p5) MODULE_DEPEND(p1, p2, p3, p4, p5) 1278 __DRIVER_MODULE(TARGETNAME, pci, hpt_pci_driver, hpt_devclass, 0, 0); 1279 __MODULE_VERSION(TARGETNAME, 1); 1280 __MODULE_DEPEND(TARGETNAME, cam, 1, 1, 1); 1281 1282 #if __FreeBSD_version>503000 1283 typedef struct cdev * ioctl_dev_t; 1284 #else 1285 typedef dev_t ioctl_dev_t; 1286 #endif 1287 1288 #if __FreeBSD_version >= 500000 1289 typedef struct thread * ioctl_thread_t; 1290 #else 1291 typedef struct proc * ioctl_thread_t; 1292 #endif 1293 1294 static int hpt_open(ioctl_dev_t dev, int flags, int devtype, ioctl_thread_t td) 1295 { 1296 return 0; 1297 } 1298 1299 static int hpt_close(ioctl_dev_t dev, int flags, int devtype, ioctl_thread_t td) 1300 { 1301 return 0; 1302 } 1303 1304 static int hpt_ioctl(ioctl_dev_t dev, u_long cmd, caddr_t data, int fflag, ioctl_thread_t td) 1305 { 1306 PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data; 1307 IOCTL_ARG ioctl_args; 1308 HPT_U32 bytesReturned; 1309 1310 switch (cmd){ 1311 case HPT_DO_IOCONTROL: 1312 { 1313 if (piop->Magic == HPT_IOCTL_MAGIC || piop->Magic == HPT_IOCTL_MAGIC32) { 1314 KdPrint(("ioctl=%x in=%p len=%d out=%p len=%d\n", 1315 piop->dwIoControlCode, 1316 piop->lpInBuffer, 1317 piop->nInBufferSize, 1318 piop->lpOutBuffer, 1319 piop->nOutBufferSize)); 1320 1321 memset(&ioctl_args, 0, sizeof(ioctl_args)); 1322 1323 ioctl_args.dwIoControlCode = piop->dwIoControlCode; 1324 ioctl_args.nInBufferSize = piop->nInBufferSize; 1325 ioctl_args.nOutBufferSize = piop->nOutBufferSize; 1326 ioctl_args.lpBytesReturned = &bytesReturned; 1327 1328 if (ioctl_args.nInBufferSize) { 1329 ioctl_args.lpInBuffer = malloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK); 1330 if (!ioctl_args.lpInBuffer) 1331 goto invalid; 1332 if (copyin((void*)piop->lpInBuffer, 1333 ioctl_args.lpInBuffer, piop->nInBufferSize)) 1334 goto invalid; 1335 } 1336 1337 if (ioctl_args.nOutBufferSize) { 1338 ioctl_args.lpOutBuffer = malloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK); 1339 if (!ioctl_args.lpOutBuffer) 1340 goto invalid; 1341 } 1342 1343 #if (__FreeBSD_version >= 500000) 1344 mtx_lock(&Giant); 1345 #endif 1346 1347 hpt_do_ioctl(&ioctl_args); 1348 1349 #if (__FreeBSD_version >= 500000) 1350 mtx_unlock(&Giant); 1351 #endif 1352 1353 if (ioctl_args.result==HPT_IOCTL_RESULT_OK) { 1354 if (piop->nOutBufferSize) { 1355 if (copyout(ioctl_args.lpOutBuffer, 1356 (void*)piop->lpOutBuffer, piop->nOutBufferSize)) 1357 goto invalid; 1358 } 1359 if (piop->lpBytesReturned) { 1360 if (copyout(&bytesReturned, 1361 (void*)piop->lpBytesReturned, sizeof(HPT_U32))) 1362 goto invalid; 1363 } 1364 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF); 1365 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF); 1366 return 0; 1367 } 1368 invalid: 1369 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF); 1370 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF); 1371 return EFAULT; 1372 } 1373 return EFAULT; 1374 } 1375 1376 case HPT_SCAN_BUS: 1377 { 1378 return hpt_rescan_bus(); 1379 } 1380 default: 1381 KdPrint(("invalid command!")); 1382 return EFAULT; 1383 } 1384 1385 } 1386 1387 static int hpt_rescan_bus(void) 1388 { 1389 union ccb *ccb; 1390 PVBUS vbus; 1391 PVBUS_EXT vbus_ext; 1392 1393 #if (__FreeBSD_version >= 500000) 1394 mtx_lock(&Giant); 1395 #endif 1396 1397 ldm_for_each_vbus(vbus, vbus_ext) { 1398 if ((ccb = xpt_alloc_ccb()) == NULL) 1399 return(ENOMEM); 1400 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, 1401 cam_sim_path(vbus_ext->sim), 1402 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1403 xpt_free_ccb(ccb); 1404 return(EIO); 1405 } 1406 xpt_rescan(ccb); 1407 } 1408 1409 #if (__FreeBSD_version >= 500000) 1410 mtx_unlock(&Giant); 1411 #endif 1412 1413 return(0); 1414 } 1415