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