1 /* $Id: osm_bsd.c,v 1.36 2010/05/11 03:12:11 lcn Exp $ */
3 * HighPoint RAID Driver for FreeBSD
4 * Copyright (C) 2005-2011 HighPoint Technologies, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 #include <dev/hptnr/hptnr_config.h>
31 #include <dev/hptnr/os_bsd.h>
32 #include <dev/hptnr/hptintf.h>
35 static HIM *hpt_match(device_t dev)
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)){
55 static int hpt_probe(device_t dev)
61 KdPrint(("hpt_probe: adapter at PCI %d:%d:%d, IRQ %d",
62 pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev), pci_get_irq(dev)
64 device_set_desc(dev, him->name);
65 return (BUS_PROBE_DEFAULT);
71 static int hpt_attach(device_t dev)
73 PHBA hba = (PHBA)device_get_softc(dev);
80 KdPrint(("hpt_attach(%d/%d/%d)", pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)));
83 hba->ext_type = EXT_TYPE_HBA;
84 hba->ldm_adapter.him = him;
86 pci_enable_busmaster(dev);
88 pci_id.vid = pci_get_vendor(dev);
89 pci_id.did = pci_get_device(dev);
90 pci_id.rev = pci_get_revid(dev);
91 pci_id.subsys = (HPT_U32)(pci_get_subdevice(dev)) << 16 | pci_get_subvendor(dev);
93 size = him->get_adapter_size(&pci_id);
94 hba->ldm_adapter.him_handle = malloc(size, M_DEVBUF, M_WAITOK);
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);
102 if (!him->create_adapter(&pci_id, hba->pciaddr, hba->ldm_adapter.him_handle, hba)) {
103 free(hba->ldm_adapter.him_handle, M_DEVBUF);
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));
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 |
114 vbus_ext->ext_type = EXT_TYPE_VBUS;
115 ldm_create_vbus((PVBUS)vbus_ext->vbus, vbus_ext);
116 ldm_register_adapter(&hba->ldm_adapter);
119 ldm_for_each_vbus(vbus, vbus_ext) {
120 if (hba->ldm_adapter.vbus==vbus) {
121 hba->vbus_ext = vbus_ext;
122 hba->next = vbus_ext->hba_list;
123 vbus_ext->hba_list = hba;
131 * Maybe we'd better to use the bus_dmamem_alloc to alloc DMA memory,
132 * but there are some problems currently (alignment, etc).
134 static __inline void *__get_free_pages(int order)
136 /* don't use low memory - other devices may get starved */
137 return contigmalloc(PAGE_SIZE<<order,
138 M_DEVBUF, M_WAITOK, BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
141 static __inline void free_pages(void *p, int order)
143 contigfree(p, PAGE_SIZE<<order, M_DEVBUF);
146 static int hpt_alloc_mem(PVBUS_EXT vbus_ext)
153 for (hba = vbus_ext->hba_list; hba; hba = hba->next)
154 hba->ldm_adapter.him->get_meminfo(hba->ldm_adapter.him_handle);
156 ldm_get_mem_info((PVBUS)vbus_ext->vbus, 0);
158 for (f=vbus_ext->freelist_head; f; f=f->next) {
159 KdPrint(("%s: %d*%d=%d bytes",
160 f->tag, f->count, f->size, f->count*f->size));
161 for (i=0; i<f->count; i++) {
162 p = (void **)malloc(f->size, M_DEVBUF, M_WAITOK);
163 if (!p) return (ENXIO);
169 for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
172 HPT_ASSERT((f->size & (f->alignment-1))==0);
174 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1)
177 KdPrint(("%s: %d*%d=%d bytes, order %d",
178 f->tag, f->count, f->size, f->count*f->size, order));
179 HPT_ASSERT(f->alignment<=PAGE_SIZE);
181 for (i=0; i<f->count;) {
182 p = (void **)__get_free_pages(order);
184 for (j = size/f->size; j && i<f->count; i++,j--) {
186 *(BUS_ADDRESS *)(p+1) = (BUS_ADDRESS)vtophys(p);
188 p = (void **)((unsigned long)p + f->size);
193 HPT_ASSERT(PAGE_SIZE==DMAPOOL_PAGE_SIZE);
195 for (i=0; i<os_max_cache_pages; i++) {
196 p = (void **)__get_free_pages(0);
198 HPT_ASSERT(((HPT_UPTR)p & (DMAPOOL_PAGE_SIZE-1))==0);
199 dmapool_put_page((PVBUS)vbus_ext->vbus, p, (BUS_ADDRESS)vtophys(p));
205 static void hpt_free_mem(PVBUS_EXT vbus_ext)
212 for (f=vbus_ext->freelist_head; f; f=f->next) {
214 if (f->count!=f->reserved_count) {
215 KdPrint(("memory leak for freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
218 while ((p=freelist_get(f)))
222 for (i=0; i<os_max_cache_pages; i++) {
223 p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus);
228 for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
231 if (f->count!=f->reserved_count) {
232 KdPrint(("memory leak for dma freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
235 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ;
237 while ((p=freelist_get_dma(f, &bus))) {
239 free_pages(p, order);
241 /* can't free immediately since other blocks in this page may still be in the list */
242 if (((HPT_UPTR)p & (PAGE_SIZE-1))==0)
243 dmapool_put_page((PVBUS)vbus_ext->vbus, p, bus);
248 while ((p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus)))
252 static int hpt_init_vbus(PVBUS_EXT vbus_ext)
256 for (hba = vbus_ext->hba_list; hba; hba = hba->next)
257 if (!hba->ldm_adapter.him->initialize(hba->ldm_adapter.him_handle)) {
258 KdPrint(("fail to initialize %p", hba));
262 ldm_initialize_vbus((PVBUS)vbus_ext->vbus, &vbus_ext->hba_list->ldm_adapter);
266 static void hpt_flush_done(PCOMMAND pCmd)
268 PVDEV vd = pCmd->target;
270 if (mIsArray(vd->type) && vd->u.array.transform && vd!=vd->u.array.transform->target) {
271 vd = vd->u.array.transform->target;
274 pCmd->Result = RETURN_PENDING;
275 vdev_queue_cmd(pCmd);
279 *(int *)pCmd->priv = 1;
284 * flush a vdev (without retry).
286 static int hpt_flush_vdev(PVBUS_EXT vbus_ext, PVDEV vd)
289 int result = 0, done;
292 KdPrint(("flusing dev %p", vd));
294 hpt_assert_vbus_locked(vbus_ext);
296 if (mIsArray(vd->type) && vd->u.array.transform)
297 count = MAX(vd->u.array.transform->source->cmds_per_request,
298 vd->u.array.transform->target->cmds_per_request);
300 count = vd->cmds_per_request;
302 pCmd = ldm_alloc_cmds(vd->vbus, count);
308 pCmd->type = CMD_TYPE_FLUSH;
309 pCmd->flags.hard_flush = 1;
311 pCmd->done = hpt_flush_done;
318 while (hpt_sleep(vbus_ext, pCmd, PPAUSE, "hptfls", HPT_OSM_TIMEOUT)) {
319 ldm_reset_vbus(vd->vbus);
323 KdPrint(("flush result %d", pCmd->Result));
325 if (pCmd->Result!=RETURN_SUCCESS)
333 static void hpt_stop_tasks(PVBUS_EXT vbus_ext);
334 static void hpt_shutdown_vbus(PVBUS_EXT vbus_ext, int howto)
336 PVBUS vbus = (PVBUS)vbus_ext->vbus;
340 KdPrint(("hpt_shutdown_vbus"));
342 /* stop all ctl tasks and disable the worker taskqueue */
343 hpt_stop_tasks(vbus_ext);
344 hpt_lock_vbus(vbus_ext);
345 vbus_ext->worker.ta_context = 0;
348 for (i=0; i<osm_max_targets; i++) {
349 PVDEV vd = ldm_find_target(vbus, i);
352 if (hpt_flush_vdev(vbus_ext, vd))
353 hpt_flush_vdev(vbus_ext, vd);
358 hpt_unlock_vbus(vbus_ext);
360 ldm_release_vbus(vbus);
362 for (hba=vbus_ext->hba_list; hba; hba=hba->next)
363 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
365 hpt_free_mem(vbus_ext);
367 while ((hba=vbus_ext->hba_list)) {
368 vbus_ext->hba_list = hba->next;
369 free(hba->ldm_adapter.him_handle, M_DEVBUF);
372 callout_drain(&vbus_ext->timer);
373 mtx_destroy(&vbus_ext->lock);
374 free(vbus_ext, M_DEVBUF);
375 KdPrint(("hpt_shutdown_vbus done"));
378 static void __hpt_do_tasks(PVBUS_EXT vbus_ext)
382 tasks = vbus_ext->tasks;
389 t->func(vbus_ext->vbus, t->data);
393 static void hpt_do_tasks(PVBUS_EXT vbus_ext, int pending)
396 hpt_lock_vbus(vbus_ext);
397 __hpt_do_tasks(vbus_ext);
398 hpt_unlock_vbus(vbus_ext);
402 static void hpt_action(struct cam_sim *sim, union ccb *ccb);
403 static void hpt_poll(struct cam_sim *sim);
404 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg);
405 static void hpt_pci_intr(void *arg);
407 static __inline POS_CMDEXT cmdext_get(PVBUS_EXT vbus_ext)
409 POS_CMDEXT p = vbus_ext->cmdext_list;
411 vbus_ext->cmdext_list = p->next;
415 static __inline void cmdext_put(POS_CMDEXT p)
417 p->next = p->vbus_ext->cmdext_list;
418 p->vbus_ext->cmdext_list = p;
421 static void hpt_timeout(void *arg)
423 PCOMMAND pCmd = (PCOMMAND)arg;
424 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
426 KdPrint(("pCmd %p timeout", pCmd));
428 ldm_reset_vbus((PVBUS)ext->vbus_ext->vbus);
431 static void os_cmddone(PCOMMAND pCmd)
433 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
434 union ccb *ccb = ext->ccb;
437 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
438 cdb = ccb->csio.cdb_io.cdb_ptr;
440 cdb = ccb->csio.cdb_io.cdb_bytes;
442 KdPrint(("os_cmddone(%p, %d)", pCmd, pCmd->Result));
444 callout_stop(&ext->timeout);
446 case 0x85: /*ATA_16*/
447 case 0xA1: /*ATA_12*/
449 PassthroughCmd *passthru = &pCmd->uCmd.Passthrough;
450 HPT_U8 *sense_buffer = (HPT_U8 *)&ccb->csio.sense_data;
451 memset(&ccb->csio.sense_data, 0,sizeof(ccb->csio.sense_data));
453 sense_buffer[0] = 0x72; /* Response Code */
454 sense_buffer[7] = 14; /* Additional Sense Length */
456 sense_buffer[8] = 0x9; /* ATA Return Descriptor */
457 sense_buffer[9] = 0xc; /* Additional Descriptor Length */
458 sense_buffer[11] = (HPT_U8)passthru->bFeaturesReg; /* Error */
459 sense_buffer[13] = (HPT_U8)passthru->bSectorCountReg; /* Sector Count (7:0) */
460 sense_buffer[15] = (HPT_U8)passthru->bLbaLowReg; /* LBA Low (7:0) */
461 sense_buffer[17] = (HPT_U8)passthru->bLbaMidReg; /* LBA Mid (7:0) */
462 sense_buffer[19] = (HPT_U8)passthru->bLbaHighReg; /* LBA High (7:0) */
464 if ((cdb[0] == 0x85) && (cdb[1] & 0x1))
466 sense_buffer[10] = 1;
467 sense_buffer[12] = (HPT_U8)(passthru->bSectorCountReg >> 8); /* Sector Count (15:8) */
468 sense_buffer[14] = (HPT_U8)(passthru->bLbaLowReg >> 8); /* LBA Low (15:8) */
469 sense_buffer[16] = (HPT_U8)(passthru->bLbaMidReg >> 8); /* LBA Mid (15:8) */
470 sense_buffer[18] = (HPT_U8)(passthru->bLbaHighReg >> 8); /* LBA High (15:8) */
473 sense_buffer[20] = (HPT_U8)passthru->bDriveHeadReg; /* Device */
474 sense_buffer[21] = (HPT_U8)passthru->bCommandReg; /* Status */
475 KdPrint(("sts 0x%x err 0x%x low 0x%x mid 0x%x hig 0x%x dh 0x%x sc 0x%x",
476 passthru->bCommandReg,
477 passthru->bFeaturesReg,
478 passthru->bLbaLowReg,
479 passthru->bLbaMidReg,
480 passthru->bLbaHighReg,
481 passthru->bDriveHeadReg,
482 passthru->bSectorCountReg));
483 KdPrint(("result:0x%x,bFeaturesReg:0x%04x,bSectorCountReg:0x%04x,LBA:0x%04x%04x%04x ",
484 pCmd->Result,passthru->bFeaturesReg,passthru->bSectorCountReg,
485 passthru->bLbaHighReg,passthru->bLbaMidReg,passthru->bLbaLowReg));
491 switch(pCmd->Result) {
493 ccb->ccb_h.status = CAM_REQ_CMP;
495 case RETURN_BAD_DEVICE:
496 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
498 case RETURN_DEVICE_BUSY:
499 ccb->ccb_h.status = CAM_BUSY;
501 case RETURN_INVALID_REQUEST:
502 ccb->ccb_h.status = CAM_REQ_INVALID;
504 case RETURN_SELECTION_TIMEOUT:
505 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
508 ccb->ccb_h.status = CAM_BUSY;
511 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
515 if (pCmd->flags.data_in) {
516 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTREAD);
518 else if (pCmd->flags.data_out) {
519 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTWRITE);
522 bus_dmamap_unload(ext->vbus_ext->io_dmat, ext->dma_map);
529 static int os_buildsgl(PCOMMAND pCmd, PSG pSg, int logical)
531 /* since we have provided physical sg, nobody will ask us to build physical sg */
536 static void hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
538 PCOMMAND pCmd = (PCOMMAND)arg;
539 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
543 HPT_ASSERT(pCmd->flags.physical_sg);
546 panic("busdma error");
548 HPT_ASSERT(nsegs<=os_max_sg_descriptors);
551 for (idx = 0; idx < nsegs; idx++, psg++) {
552 psg->addr.bus = segs[idx].ds_addr;
553 psg->size = segs[idx].ds_len;
558 if (pCmd->flags.data_in) {
559 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
560 BUS_DMASYNC_PREREAD);
562 else if (pCmd->flags.data_out) {
563 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
564 BUS_DMASYNC_PREWRITE);
568 callout_reset(&ext->timeout, HPT_OSM_TIMEOUT, hpt_timeout, pCmd);
572 static void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb)
574 PVBUS vbus = (PVBUS)vbus_ext->vbus;
580 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
581 cdb = ccb->csio.cdb_io.cdb_ptr;
583 cdb = ccb->csio.cdb_io.cdb_bytes;
585 KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x",
587 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
588 *(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8]
591 /* ccb->ccb_h.path_id is not our bus id - don't check it */
592 if (ccb->ccb_h.target_lun != 0 ||
593 ccb->ccb_h.target_id >= osm_max_targets ||
594 (ccb->ccb_h.flags & CAM_CDB_PHYS))
596 ccb->ccb_h.status = CAM_TID_INVALID;
601 vd = ldm_find_target(vbus, ccb->ccb_h.target_id);
604 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
610 case TEST_UNIT_READY:
611 case START_STOP_UNIT:
612 case SYNCHRONIZE_CACHE:
613 ccb->ccb_h.status = CAM_REQ_CMP;
616 case 0x85: /*ATA_16*/
617 case 0xA1: /*ATA_12*/
621 PassthroughCmd *passthru;
623 if (mIsArray(vd->type)) {
624 ccb->ccb_h.status = CAM_PATH_INVALID;
628 HPT_ASSERT(vd->type == VD_RAW && vd->u.raw.legacy_disk);
630 prot = (cdb[1] & 0x1e) >> 1;
633 if (prot < 3 || prot > 5)
635 ccb->ccb_h.status = CAM_REQ_INVALID;
639 pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
642 ccb->ccb_h.status = CAM_BUSY;
646 passthru = &pCmd->uCmd.Passthrough;
647 if (cdb[0] == 0x85/*ATA_16*/) {
649 passthru->bFeaturesReg =
650 ((HPT_U16)cdb[3] << 8)
652 passthru->bSectorCountReg =
653 ((HPT_U16)cdb[5] << 8) |
655 passthru->bLbaLowReg =
656 ((HPT_U16)cdb[7] << 8) |
658 passthru->bLbaMidReg =
659 ((HPT_U16)cdb[9] << 8) |
661 passthru->bLbaHighReg =
662 ((HPT_U16)cdb[11] << 8) |
665 passthru->bFeaturesReg = cdb[4];
666 passthru->bSectorCountReg = cdb[6];
667 passthru->bLbaLowReg = cdb[8];
668 passthru->bLbaMidReg = cdb[10];
669 passthru->bLbaHighReg = cdb[12];
671 passthru->bDriveHeadReg = cdb[13];
672 passthru->bCommandReg = cdb[14];
676 passthru->bFeaturesReg = cdb[3];
677 passthru->bSectorCountReg = cdb[4];
678 passthru->bLbaLowReg = cdb[5];
679 passthru->bLbaMidReg = cdb[6];
680 passthru->bLbaHighReg = cdb[7];
681 passthru->bDriveHeadReg = cdb[8];
682 passthru->bCommandReg = cdb[9];
688 if (!(passthru->bCommandReg == ATA_CMD_READ_MULTI ||
689 passthru->bCommandReg == ATA_CMD_READ_MULTI_EXT ||
690 passthru->bCommandReg == ATA_CMD_WRITE_MULTI ||
691 passthru->bCommandReg == ATA_CMD_WRITE_MULTI_EXT ||
692 passthru->bCommandReg == ATA_CMD_WRITE_MULTI_FUA_EXT)
699 if (passthru->bFeaturesReg == ATA_SET_FEATURES_XFER &&
700 passthru->bCommandReg == ATA_CMD_SET_FEATURES) {
705 passthru->nSectors = ccb->csio.dxfer_len/ATA_SECTOR_SIZE;
707 default: /*None data*/
709 case 4: /*PIO data in, T_DIR=1 match check*/
713 OsPrint(("PIO data in, T_DIR=1 match check"));
716 pCmd->flags.data_in = 1;
718 case 5: /*PIO data out, T_DIR=0 match check*/
722 OsPrint(("PIO data out, T_DIR=0 match check"));
726 pCmd->flags.data_out = 1;
729 pCmd->type = CMD_TYPE_PASSTHROUGH;
730 pCmd->priv = ext = cmdext_get(vbus_ext);
734 pCmd->done = os_cmddone;
735 pCmd->buildsgl = os_buildsgl;
736 pCmd->psg = ext->psg;
738 if(!ccb->csio.dxfer_len)
743 pCmd->flags.physical_sg = 1;
744 error = bus_dmamap_load_ccb(vbus_ext->io_dmat,
746 hpt_io_dmamap_callback, pCmd,
749 KdPrint(("bus_dmamap_load return %d", error));
750 if (error && error!=EINPROGRESS) {
751 os_printk("bus_dmamap_load error %d", error);
754 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
760 ccb->ccb_h.status = CAM_PATH_INVALID;
766 PINQUIRYDATA inquiryData;
767 HIM_DEVICE_CONFIG devconf;
770 memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len);
771 inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr;
774 rbuf = (HPT_U8 *)inquiryData;
784 ccb->ccb_h.status = CAM_REQ_CMP;
790 if (vd->type == VD_RAW) {
792 vd->u.raw.him->get_device_config(vd->u.raw.phy_dev,&devconf);
793 memcpy(&rbuf[4], devconf.pIdentifyData->SerialNumber, 20);
794 ldm_ide_fixstring(&rbuf[4], 20);
799 ccb->ccb_h.status = CAM_REQ_CMP;
819 ccb->ccb_h.status = CAM_REQ_CMP;
822 ccb->ccb_h.status = CAM_REQ_INVALID;
829 ccb->ccb_h.status = CAM_REQ_INVALID;
833 inquiryData->DeviceType = 0; /*DIRECT_ACCESS_DEVICE*/
834 inquiryData->Versions = 5; /*SPC-3*/
835 inquiryData->ResponseDataFormat = 2;
836 inquiryData->AdditionalLength = 0x5b;
837 inquiryData->CommandQueue = 1;
839 if (ccb->csio.dxfer_len > 63) {
840 rbuf = (HPT_U8 *)inquiryData;
850 if (vd->type == VD_RAW) {
851 vd->u.raw.him->get_device_config(vd->u.raw.phy_dev,&devconf);
853 if ((devconf.pIdentifyData->GeneralConfiguration & 0x80))
854 inquiryData->RemovableMedia = 1;
857 memcpy(&inquiryData->VendorId, "ATA ", 8);
858 memcpy(&inquiryData->ProductId, devconf.pIdentifyData->ModelNumber, 16);
859 ldm_ide_fixstring((HPT_U8 *)&inquiryData->ProductId, 16);
860 memcpy(&inquiryData->ProductRevisionLevel, devconf.pIdentifyData->FirmwareRevision, 4);
861 ldm_ide_fixstring((HPT_U8 *)&inquiryData->ProductRevisionLevel, 4);
862 if (inquiryData->ProductRevisionLevel[0] == 0 || inquiryData->ProductRevisionLevel[0] == ' ')
863 memcpy(&inquiryData->ProductRevisionLevel, "n/a ", 4);
865 memcpy(&inquiryData->VendorId, "HPT ", 8);
866 snprintf((char *)&inquiryData->ProductId, 16, "DISK_%d_%d ",
867 os_get_vbus_seq(vbus_ext), vd->target_id);
868 inquiryData->ProductId[15] = ' ';
869 memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4);
872 ccb->ccb_h.status = CAM_REQ_CMP;
877 HPT_U8 *rbuf = ccb->csio.data_ptr;
879 HPT_U8 sector_size_shift = 0;
881 HPT_U32 sector_size = 0;
883 if (mIsArray(vd->type))
884 sector_size_shift = vd->u.array.sector_size_shift;
886 if(vd->type == VD_RAW){
887 sector_size = vd->u.raw.logical_sector_size;
890 switch (sector_size) {
892 KdPrint(("set 4k setctor size in READ_CAPACITY"));
893 sector_size_shift = 3;
899 new_cap = vd->capacity >> sector_size_shift;
901 if (new_cap > 0xfffffffful)
906 rbuf[0] = (HPT_U8)(cap>>24);
907 rbuf[1] = (HPT_U8)(cap>>16);
908 rbuf[2] = (HPT_U8)(cap>>8);
909 rbuf[3] = (HPT_U8)cap;
912 rbuf[6] = 2 << sector_size_shift;
915 ccb->ccb_h.status = CAM_REQ_CMP;
919 case SERVICE_ACTION_IN:
921 HPT_U8 *rbuf = ccb->csio.data_ptr;
923 HPT_U8 sector_size_shift = 0;
924 HPT_U32 sector_size = 0;
926 if(mIsArray(vd->type))
927 sector_size_shift = vd->u.array.sector_size_shift;
929 if(vd->type == VD_RAW){
930 sector_size = vd->u.raw.logical_sector_size;
933 switch (sector_size) {
935 KdPrint(("set 4k setctor size in SERVICE_ACTION_IN"));
936 sector_size_shift = 3;
942 cap = (vd->capacity >> sector_size_shift) - 1;
944 rbuf[0] = (HPT_U8)(cap>>56);
945 rbuf[1] = (HPT_U8)(cap>>48);
946 rbuf[2] = (HPT_U8)(cap>>40);
947 rbuf[3] = (HPT_U8)(cap>>32);
948 rbuf[4] = (HPT_U8)(cap>>24);
949 rbuf[5] = (HPT_U8)(cap>>16);
950 rbuf[6] = (HPT_U8)(cap>>8);
951 rbuf[7] = (HPT_U8)cap;
954 rbuf[10] = 2 << sector_size_shift;
957 ccb->ccb_h.status = CAM_REQ_CMP;
969 case 0x8f: /* VERIFY_16 */
972 HPT_U8 sector_size_shift = 0;
973 HPT_U32 sector_size = 0;
974 pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
976 KdPrint(("Failed to allocate command!"));
977 ccb->ccb_h.status = CAM_BUSY;
985 pCmd->uCmd.Ide.Lba = ((HPT_U32)cdb[1] << 16) | ((HPT_U32)cdb[2] << 8) | (HPT_U32)cdb[3];
986 pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[4];
990 case 0x8f: /* VERIFY_16 */
993 ((HPT_U64)cdb[2]<<56) |
994 ((HPT_U64)cdb[3]<<48) |
995 ((HPT_U64)cdb[4]<<40) |
996 ((HPT_U64)cdb[5]<<32) |
997 ((HPT_U64)cdb[6]<<24) |
998 ((HPT_U64)cdb[7]<<16) |
999 ((HPT_U64)cdb[8]<<8) |
1001 pCmd->uCmd.Ide.Lba = block;
1002 pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8);
1007 pCmd->uCmd.Ide.Lba = (HPT_U32)cdb[5] | ((HPT_U32)cdb[4] << 8) | ((HPT_U32)cdb[3] << 16) | ((HPT_U32)cdb[2] << 24);
1008 pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[8] | ((HPT_U16)cdb[7]<<8);
1012 if(mIsArray(vd->type)) {
1013 sector_size_shift = vd->u.array.sector_size_shift;
1016 if(vd->type == VD_RAW){
1017 sector_size = vd->u.raw.logical_sector_size;
1020 switch (sector_size) {
1022 KdPrint(("<8>resize sector size from 4k to 512"));
1023 sector_size_shift = 3;
1029 pCmd->uCmd.Ide.Lba <<= sector_size_shift;
1030 pCmd->uCmd.Ide.nSectors <<= sector_size_shift;
1037 pCmd->flags.data_in = 1;
1042 pCmd->flags.data_out = 1;
1045 pCmd->priv = ext = cmdext_get(vbus_ext);
1049 pCmd->done = os_cmddone;
1050 pCmd->buildsgl = os_buildsgl;
1051 pCmd->psg = ext->psg;
1052 pCmd->flags.physical_sg = 1;
1053 error = bus_dmamap_load_ccb(vbus_ext->io_dmat,
1055 hpt_io_dmamap_callback, pCmd,
1058 KdPrint(("bus_dmamap_load return %d", error));
1059 if (error && error!=EINPROGRESS) {
1060 os_printk("bus_dmamap_load error %d", error);
1062 ldm_free_cmds(pCmd);
1063 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1070 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1078 static void hpt_action(struct cam_sim *sim, union ccb *ccb)
1080 PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
1082 KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id));
1084 hpt_assert_vbus_locked(vbus_ext);
1085 switch (ccb->ccb_h.func_code) {
1088 hpt_scsi_io(vbus_ext, ccb);
1092 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
1095 case XPT_GET_TRAN_SETTINGS:
1096 case XPT_SET_TRAN_SETTINGS:
1097 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1100 case XPT_CALC_GEOMETRY:
1101 ccb->ccg.heads = 255;
1102 ccb->ccg.secs_per_track = 63;
1103 ccb->ccg.cylinders = ccb->ccg.volume_size / (ccb->ccg.heads * ccb->ccg.secs_per_track);
1104 ccb->ccb_h.status = CAM_REQ_CMP;
1109 struct ccb_pathinq *cpi = &ccb->cpi;
1111 cpi->version_num = 1;
1112 cpi->hba_inquiry = PI_SDTR_ABLE;
1113 cpi->target_sprt = 0;
1114 cpi->hba_misc = PIM_NOBUSRESET;
1115 cpi->hba_eng_cnt = 0;
1116 cpi->max_target = osm_max_targets;
1118 cpi->unit_number = cam_sim_unit(sim);
1119 cpi->bus_id = cam_sim_bus(sim);
1120 cpi->initiator_id = osm_max_targets;
1121 cpi->base_transfer_speed = 3300;
1123 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1124 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
1125 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1126 cpi->transport = XPORT_SPI;
1127 cpi->transport_version = 2;
1128 cpi->protocol = PROTO_SCSI;
1129 cpi->protocol_version = SCSI_REV_2;
1130 cpi->ccb_h.status = CAM_REQ_CMP;
1135 ccb->ccb_h.status = CAM_REQ_INVALID;
1143 static void hpt_pci_intr(void *arg)
1145 PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
1146 hpt_lock_vbus(vbus_ext);
1147 ldm_intr((PVBUS)vbus_ext->vbus);
1148 hpt_unlock_vbus(vbus_ext);
1151 static void hpt_poll(struct cam_sim *sim)
1153 PVBUS_EXT vbus_ext = cam_sim_softc(sim);
1154 hpt_assert_vbus_locked(vbus_ext);
1155 ldm_intr((PVBUS)vbus_ext->vbus);
1158 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg)
1160 KdPrint(("hpt_async"));
1163 static int hpt_shutdown(device_t dev)
1165 KdPrint(("hpt_shutdown(dev=%p)", dev));
1169 static int hpt_detach(device_t dev)
1171 /* we don't allow the driver to be unloaded. */
1175 static void hpt_ioctl_done(struct _IOCTL_ARG *arg)
1177 arg->ioctl_cmnd = 0;
1181 static void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args)
1183 ioctl_args->result = -1;
1184 ioctl_args->done = hpt_ioctl_done;
1185 ioctl_args->ioctl_cmnd = (void *)1;
1187 hpt_lock_vbus(vbus_ext);
1188 ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args);
1190 while (ioctl_args->ioctl_cmnd) {
1191 if (hpt_sleep(vbus_ext, ioctl_args, PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
1193 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
1194 __hpt_do_tasks(vbus_ext);
1197 /* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */
1199 hpt_unlock_vbus(vbus_ext);
1202 static void hpt_do_ioctl(IOCTL_ARG *ioctl_args)
1207 ldm_for_each_vbus(vbus, vbus_ext) {
1208 __hpt_do_ioctl(vbus_ext, ioctl_args);
1209 if (ioctl_args->result!=HPT_IOCTL_RESULT_WRONG_VBUS)
1214 #define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\
1216 arg.dwIoControlCode = code;\
1217 arg.lpInBuffer = inbuf;\
1218 arg.lpOutBuffer = outbuf;\
1219 arg.nInBufferSize = insize;\
1220 arg.nOutBufferSize = outsize;\
1221 arg.lpBytesReturned = 0;\
1222 hpt_do_ioctl(&arg);\
1226 #define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff))
1228 static int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount)
1231 HPT_U32 count = nMaxCount-1;
1233 if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES,
1234 &count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount))
1237 nMaxCount = (int)pIds[0];
1238 for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1];
1242 static int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo)
1244 return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3,
1245 &id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3));
1248 /* not belong to this file logically, but we want to use ioctl interface */
1249 static int __hpt_stop_tasks(PVBUS_EXT vbus_ext, DEVICEID id)
1251 LOGICAL_DEVICE_INFO_V3 devinfo;
1253 DEVICEID param[2] = { id, 0 };
1255 if (hpt_get_device_info_v3(id, &devinfo))
1258 if (devinfo.Type!=LDT_ARRAY)
1261 if (devinfo.u.array.Flags & ARRAY_FLAG_REBUILDING)
1262 param[1] = AS_REBUILD_ABORT;
1263 else if (devinfo.u.array.Flags & ARRAY_FLAG_VERIFYING)
1264 param[1] = AS_VERIFY_ABORT;
1265 else if (devinfo.u.array.Flags & ARRAY_FLAG_INITIALIZING)
1266 param[1] = AS_INITIALIZE_ABORT;
1267 else if (devinfo.u.array.Flags & ARRAY_FLAG_TRANSFORMING)
1268 param[1] = AS_TRANSFORM_ABORT;
1272 KdPrint(("SET_ARRAY_STATE(%x, %d)", param[0], param[1]));
1273 result = HPT_DO_IOCTL(HPT_IOCTL_SET_ARRAY_STATE,
1274 param, sizeof(param), 0, 0);
1276 for (i=0; i<devinfo.u.array.nDisk; i++)
1277 if (DEVICEID_VALID(devinfo.u.array.Members[i]))
1278 __hpt_stop_tasks(vbus_ext, devinfo.u.array.Members[i]);
1283 static void hpt_stop_tasks(PVBUS_EXT vbus_ext)
1288 count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0]));
1290 for (i=0; i<count; i++)
1291 __hpt_stop_tasks(vbus_ext, ids[i]);
1294 static d_open_t hpt_open;
1295 static d_close_t hpt_close;
1296 static d_ioctl_t hpt_ioctl;
1297 static int hpt_rescan_bus(void);
1299 static struct cdevsw hpt_cdevsw = {
1301 .d_close = hpt_close,
1302 .d_ioctl = hpt_ioctl,
1303 .d_name = driver_name,
1304 .d_version = D_VERSION,
1307 static struct intr_config_hook hpt_ich;
1310 * hpt_final_init will be called after all hpt_attach.
1312 static void hpt_final_init(void *dummy)
1314 int i,unit_number=0;
1319 /* Clear the config hook */
1320 config_intrhook_disestablish(&hpt_ich);
1322 /* allocate memory */
1324 ldm_for_each_vbus(vbus, vbus_ext) {
1325 if (hpt_alloc_mem(vbus_ext)) {
1326 os_printk("out of memory");
1334 os_printk("no controller detected.");
1338 /* initializing hardware */
1339 ldm_for_each_vbus(vbus, vbus_ext) {
1340 /* make timer available here */
1341 mtx_init(&vbus_ext->lock, "hptsleeplock", NULL, MTX_DEF);
1342 callout_init_mtx(&vbus_ext->timer, &vbus_ext->lock, 0);
1343 if (hpt_init_vbus(vbus_ext)) {
1344 os_printk("fail to initialize hardware");
1349 /* register CAM interface */
1350 ldm_for_each_vbus(vbus, vbus_ext) {
1351 struct cam_devq *devq;
1352 struct ccb_setasync ccb;
1354 if (bus_dma_tag_create(NULL,/* parent */
1356 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1357 BUS_SPACE_MAXADDR, /* lowaddr */
1358 BUS_SPACE_MAXADDR, /* highaddr */
1359 NULL, NULL, /* filter, filterarg */
1360 PAGE_SIZE * (os_max_sg_descriptors-1), /* maxsize */
1361 os_max_sg_descriptors, /* nsegments */
1362 0x10000, /* maxsegsize */
1363 BUS_DMA_WAITOK, /* flags */
1364 busdma_lock_mutex, /* lockfunc */
1365 &vbus_ext->lock, /* lockfuncarg */
1366 &vbus_ext->io_dmat /* tag */))
1371 for (i=0; i<os_max_queue_comm; i++) {
1372 POS_CMDEXT ext = (POS_CMDEXT)malloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK);
1374 os_printk("Can't alloc cmdext(%d)", i);
1377 ext->vbus_ext = vbus_ext;
1378 ext->next = vbus_ext->cmdext_list;
1379 vbus_ext->cmdext_list = ext;
1381 if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) {
1382 os_printk("Can't create dma map(%d)", i);
1385 callout_init_mtx(&ext->timeout, &vbus_ext->lock, 0);
1388 if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) {
1389 os_printk("cam_simq_alloc failed");
1393 hpt_lock_vbus(vbus_ext);
1394 vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1395 vbus_ext, unit_number, &vbus_ext->lock,
1396 os_max_queue_comm, /*tagged*/8, devq);
1398 if (!vbus_ext->sim) {
1399 os_printk("cam_sim_alloc failed");
1400 cam_simq_free(devq);
1401 hpt_unlock_vbus(vbus_ext);
1405 if (xpt_bus_register(vbus_ext->sim, NULL, 0) != CAM_SUCCESS) {
1406 os_printk("xpt_bus_register failed");
1407 cam_sim_free(vbus_ext->sim, /*free devq*/ TRUE);
1408 vbus_ext->sim = NULL;
1412 if (xpt_create_path(&vbus_ext->path, /*periph */ NULL,
1413 cam_sim_path(vbus_ext->sim), CAM_TARGET_WILDCARD,
1414 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1416 os_printk("xpt_create_path failed");
1417 xpt_bus_deregister(cam_sim_path(vbus_ext->sim));
1418 cam_sim_free(vbus_ext->sim, /*free_devq*/TRUE);
1419 hpt_unlock_vbus(vbus_ext);
1420 vbus_ext->sim = NULL;
1423 hpt_unlock_vbus(vbus_ext);
1425 xpt_setup_ccb(&ccb.ccb_h, vbus_ext->path, /*priority*/5);
1426 ccb.ccb_h.func_code = XPT_SASYNC_CB;
1427 ccb.event_enable = AC_LOST_DEVICE;
1428 ccb.callback = hpt_async;
1429 ccb.callback_arg = vbus_ext;
1430 xpt_action((union ccb *)&ccb);
1432 for (hba = vbus_ext->hba_list; hba; hba = hba->next) {
1434 if ((hba->irq_res = bus_alloc_resource(hba->pcidev,
1435 SYS_RES_IRQ, &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1437 os_printk("can't allocate interrupt");
1441 if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM | INTR_MPSAFE,
1442 NULL, hpt_pci_intr, vbus_ext, &hba->irq_handle))
1444 os_printk("can't set up interrupt");
1447 hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE);
1451 vbus_ext->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1452 hpt_shutdown_vbus, vbus_ext, SHUTDOWN_PRI_DEFAULT);
1453 if (!vbus_ext->shutdown_eh)
1454 os_printk("Shutdown event registration failed");
1457 ldm_for_each_vbus(vbus, vbus_ext) {
1458 TASK_INIT(&vbus_ext->worker, 0, (task_fn_t *)hpt_do_tasks, vbus_ext);
1459 if (vbus_ext->tasks)
1460 TASK_ENQUEUE(&vbus_ext->worker);
1463 make_dev(&hpt_cdevsw, DRIVER_MINOR, UID_ROOT, GID_OPERATOR,
1464 S_IRUSR | S_IWUSR, "%s", driver_name);
1467 #if defined(KLD_MODULE)
1469 typedef struct driverlink *driverlink_t;
1471 kobj_class_t driver;
1472 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
1475 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
1478 TAILQ_ENTRY(devclass) link;
1479 devclass_t parent; /* parent in devclass hierarchy */
1480 driver_list_t drivers; /* bus devclasses store drivers for bus */
1482 device_t *devices; /* array of devices indexed by unit */
1483 int maxunit; /* size of devices array */
1486 static void override_kernel_driver(void)
1488 driverlink_t dl, dlfirst;
1489 driver_t *tmpdriver;
1490 devclass_t dc = devclass_find("pci");
1493 dlfirst = TAILQ_FIRST(&dc->drivers);
1494 for (dl = dlfirst; dl; dl = TAILQ_NEXT(dl, link)) {
1495 if(strcmp(dl->driver->name, driver_name) == 0) {
1496 tmpdriver=dl->driver;
1497 dl->driver=dlfirst->driver;
1498 dlfirst->driver=tmpdriver;
1506 #define override_kernel_driver()
1509 static void hpt_init(void *dummy)
1512 os_printk("%s %s", driver_name_long, driver_ver);
1514 override_kernel_driver();
1517 hpt_ich.ich_func = hpt_final_init;
1518 hpt_ich.ich_arg = NULL;
1519 if (config_intrhook_establish(&hpt_ich) != 0) {
1520 printf("%s: cannot establish configuration hook\n",
1525 SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
1528 * CAM driver interface
1530 static device_method_t driver_methods[] = {
1531 /* Device interface */
1532 DEVMETHOD(device_probe, hpt_probe),
1533 DEVMETHOD(device_attach, hpt_attach),
1534 DEVMETHOD(device_detach, hpt_detach),
1535 DEVMETHOD(device_shutdown, hpt_shutdown),
1539 static driver_t hpt_pci_driver = {
1545 static devclass_t hpt_devclass;
1548 #error "no TARGETNAME found"
1551 /* use this to make TARGETNAME be expanded */
1552 #define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
1553 #define __MODULE_VERSION(p1, p2) MODULE_VERSION(p1, p2)
1554 #define __MODULE_DEPEND(p1, p2, p3, p4, p5) MODULE_DEPEND(p1, p2, p3, p4, p5)
1555 __DRIVER_MODULE(TARGETNAME, pci, hpt_pci_driver, hpt_devclass, 0, 0);
1556 __MODULE_VERSION(TARGETNAME, 1);
1557 __MODULE_DEPEND(TARGETNAME, cam, 1, 1, 1);
1559 static int hpt_open(struct cdev *dev, int flags, int devtype, struct thread *td)
1564 static int hpt_close(struct cdev *dev, int flags, int devtype, struct thread *td)
1569 static int hpt_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1571 PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data;
1572 IOCTL_ARG ioctl_args;
1573 HPT_U32 bytesReturned;
1576 case HPT_DO_IOCONTROL:
1578 if (piop->Magic == HPT_IOCTL_MAGIC || piop->Magic == HPT_IOCTL_MAGIC32) {
1579 KdPrint(("ioctl=%x in=%p len=%d out=%p len=%d\n",
1580 piop->dwIoControlCode,
1582 piop->nInBufferSize,
1584 piop->nOutBufferSize));
1586 memset(&ioctl_args, 0, sizeof(ioctl_args));
1588 ioctl_args.dwIoControlCode = piop->dwIoControlCode;
1589 ioctl_args.nInBufferSize = piop->nInBufferSize;
1590 ioctl_args.nOutBufferSize = piop->nOutBufferSize;
1591 ioctl_args.lpBytesReturned = &bytesReturned;
1593 if (ioctl_args.nInBufferSize) {
1594 ioctl_args.lpInBuffer = malloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK);
1595 if (!ioctl_args.lpInBuffer)
1597 if (copyin((void*)piop->lpInBuffer,
1598 ioctl_args.lpInBuffer, piop->nInBufferSize))
1602 if (ioctl_args.nOutBufferSize) {
1603 ioctl_args.lpOutBuffer = malloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK);
1604 if (!ioctl_args.lpOutBuffer)
1608 hpt_do_ioctl(&ioctl_args);
1610 if (ioctl_args.result==HPT_IOCTL_RESULT_OK) {
1611 if (piop->nOutBufferSize) {
1612 if (copyout(ioctl_args.lpOutBuffer,
1613 (void*)piop->lpOutBuffer, piop->nOutBufferSize))
1616 if (piop->lpBytesReturned) {
1617 if (copyout(&bytesReturned,
1618 (void*)piop->lpBytesReturned, sizeof(HPT_U32)))
1621 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1622 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1626 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1627 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1635 return hpt_rescan_bus();
1638 KdPrint(("invalid command!"));
1644 static int hpt_rescan_bus(void)
1650 ldm_for_each_vbus(vbus, vbus_ext) {
1651 if ((ccb = xpt_alloc_ccb()) == NULL)
1655 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(vbus_ext->sim),
1656 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)