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