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