2 * Copyright (c) HighPoint Technologies, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <dev/hptrr/hptrr_config.h>
31 /* $Id: osm_bsd.c,v 1.27 2007/11/22 07:35:49 gmm Exp $
33 * HighPoint RAID Driver for FreeBSD
34 * Copyright (C) 2005 HighPoint Technologies, Inc. All Rights Reserved.
36 #include <dev/hptrr/os_bsd.h>
37 #include <dev/hptrr/hptintf.h>
39 static int attach_generic = 0;
40 TUNABLE_INT("hw.hptrr.attach_generic", &attach_generic);
42 static HIM *hpt_match(device_t dev)
48 /* Some of supported chips are used not only by HPT. */
49 if (pci_get_vendor(dev) != 0x1103 && !attach_generic)
51 for (him = him_list; him; him = him->next) {
52 for (i=0; him->get_supported_device_id(i, &pci_id); i++) {
53 if ((pci_get_vendor(dev) == pci_id.vid) &&
54 (pci_get_device(dev) == pci_id.did)){
62 static int hpt_probe(device_t dev)
68 KdPrint(("hpt_probe: adapter at PCI %d:%d:%d, IRQ %d",
69 pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev), pci_get_irq(dev)
71 device_set_desc(dev, him->name);
72 return (BUS_PROBE_DEFAULT);
78 static int hpt_attach(device_t dev)
80 PHBA hba = (PHBA)device_get_softc(dev);
87 KdPrint(("hpt_attach(%d/%d/%d)", pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)));
90 hba->ext_type = EXT_TYPE_HBA;
91 hba->ldm_adapter.him = him;
93 pci_enable_busmaster(dev);
95 pci_id.vid = pci_get_vendor(dev);
96 pci_id.did = pci_get_device(dev);
97 pci_id.rev = pci_get_revid(dev);
99 size = him->get_adapter_size(&pci_id);
100 hba->ldm_adapter.him_handle = malloc(size, M_DEVBUF, M_WAITOK);
103 hba->pciaddr.tree = 0;
104 hba->pciaddr.bus = pci_get_bus(dev);
105 hba->pciaddr.device = pci_get_slot(dev);
106 hba->pciaddr.function = pci_get_function(dev);
108 if (!him->create_adapter(&pci_id, hba->pciaddr, hba->ldm_adapter.him_handle, hba)) {
109 free(hba->ldm_adapter.him_handle, M_DEVBUF);
113 os_printk("adapter at PCI %d:%d:%d, IRQ %d",
114 hba->pciaddr.bus, hba->pciaddr.device, hba->pciaddr.function, pci_get_irq(dev));
116 if (!ldm_register_adapter(&hba->ldm_adapter)) {
117 size = ldm_get_vbus_size();
118 vbus_ext = malloc(sizeof(VBUS_EXT) + size, M_DEVBUF, M_WAITOK |
120 vbus_ext->ext_type = EXT_TYPE_VBUS;
121 ldm_create_vbus((PVBUS)vbus_ext->vbus, vbus_ext);
122 ldm_register_adapter(&hba->ldm_adapter);
125 ldm_for_each_vbus(vbus, vbus_ext) {
126 if (hba->ldm_adapter.vbus==vbus) {
127 hba->vbus_ext = vbus_ext;
128 hba->next = vbus_ext->hba_list;
129 vbus_ext->hba_list = hba;
137 * Maybe we'd better to use the bus_dmamem_alloc to alloc DMA memory,
138 * but there are some problems currently (alignment, etc).
140 static __inline void *__get_free_pages(int order)
142 /* don't use low memory - other devices may get starved */
143 return contigmalloc(PAGE_SIZE<<order,
144 M_DEVBUF, M_WAITOK, BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
147 static __inline void free_pages(void *p, int order)
149 contigfree(p, PAGE_SIZE<<order, M_DEVBUF);
152 static int hpt_alloc_mem(PVBUS_EXT vbus_ext)
159 for (hba = vbus_ext->hba_list; hba; hba = hba->next)
160 hba->ldm_adapter.him->get_meminfo(hba->ldm_adapter.him_handle);
162 ldm_get_mem_info((PVBUS)vbus_ext->vbus, 0);
164 for (f=vbus_ext->freelist_head; f; f=f->next) {
165 KdPrint(("%s: %d*%d=%d bytes",
166 f->tag, f->count, f->size, f->count*f->size));
167 for (i=0; i<f->count; i++) {
168 p = (void **)malloc(f->size, M_DEVBUF, M_WAITOK);
169 if (!p) return (ENXIO);
175 for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
178 HPT_ASSERT((f->size & (f->alignment-1))==0);
180 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1)
183 KdPrint(("%s: %d*%d=%d bytes, order %d",
184 f->tag, f->count, f->size, f->count*f->size, order));
185 HPT_ASSERT(f->alignment<=PAGE_SIZE);
187 for (i=0; i<f->count;) {
188 p = (void **)__get_free_pages(order);
190 for (j = size/f->size; j && i<f->count; i++,j--) {
192 *(BUS_ADDRESS *)(p+1) = (BUS_ADDRESS)vtophys(p);
194 p = (void **)((unsigned long)p + f->size);
199 HPT_ASSERT(PAGE_SIZE==DMAPOOL_PAGE_SIZE);
201 for (i=0; i<os_max_cache_pages; i++) {
202 p = (void **)__get_free_pages(0);
204 HPT_ASSERT(((HPT_UPTR)p & (DMAPOOL_PAGE_SIZE-1))==0);
205 dmapool_put_page((PVBUS)vbus_ext->vbus, p, (BUS_ADDRESS)vtophys(p));
211 static void hpt_free_mem(PVBUS_EXT vbus_ext)
218 for (f=vbus_ext->freelist_head; f; f=f->next) {
220 if (f->count!=f->reserved_count) {
221 KdPrint(("memory leak for freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
224 while ((p=freelist_get(f)))
228 for (i=0; i<os_max_cache_pages; i++) {
229 p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus);
234 for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
237 if (f->count!=f->reserved_count) {
238 KdPrint(("memory leak for dma freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
241 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ;
243 while ((p=freelist_get_dma(f, &bus))) {
245 free_pages(p, order);
247 /* can't free immediately since other blocks in this page may still be in the list */
248 if (((HPT_UPTR)p & (PAGE_SIZE-1))==0)
249 dmapool_put_page((PVBUS)vbus_ext->vbus, p, bus);
254 while ((p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus)))
258 static int hpt_init_vbus(PVBUS_EXT vbus_ext)
262 for (hba = vbus_ext->hba_list; hba; hba = hba->next)
263 if (!hba->ldm_adapter.him->initialize(hba->ldm_adapter.him_handle)) {
264 KdPrint(("fail to initialize %p", hba));
268 ldm_initialize_vbus((PVBUS)vbus_ext->vbus, &vbus_ext->hba_list->ldm_adapter);
272 static void hpt_flush_done(PCOMMAND pCmd)
274 PVDEV vd = pCmd->target;
276 if (mIsArray(vd->type) && vd->u.array.transform && vd!=vd->u.array.transform->target) {
277 vd = vd->u.array.transform->target;
280 pCmd->Result = RETURN_PENDING;
281 vdev_queue_cmd(pCmd);
285 *(int *)pCmd->priv = 1;
290 * flush a vdev (without retry).
292 static int hpt_flush_vdev(PVBUS_EXT vbus_ext, PVDEV vd)
295 int result = 0, done;
298 KdPrint(("flusing dev %p", vd));
300 hpt_assert_vbus_locked(vbus_ext);
302 if (mIsArray(vd->type) && vd->u.array.transform)
303 count = MAX(vd->u.array.transform->source->cmds_per_request,
304 vd->u.array.transform->target->cmds_per_request);
306 count = vd->cmds_per_request;
308 pCmd = ldm_alloc_cmds(vd->vbus, count);
314 pCmd->type = CMD_TYPE_FLUSH;
315 pCmd->flags.hard_flush = 1;
317 pCmd->done = hpt_flush_done;
324 while (hpt_sleep(vbus_ext, pCmd, PPAUSE, "hptfls", HPT_OSM_TIMEOUT)) {
325 ldm_reset_vbus(vd->vbus);
329 KdPrint(("flush result %d", pCmd->Result));
331 if (pCmd->Result!=RETURN_SUCCESS)
339 static void hpt_stop_tasks(PVBUS_EXT vbus_ext);
340 static void hpt_shutdown_vbus(PVBUS_EXT vbus_ext, int howto)
342 PVBUS vbus = (PVBUS)vbus_ext->vbus;
346 KdPrint(("hpt_shutdown_vbus"));
348 /* stop all ctl tasks and disable the worker taskqueue */
349 hpt_stop_tasks(vbus_ext);
350 hpt_lock_vbus(vbus_ext);
351 vbus_ext->worker.ta_context = 0;
354 for (i=0; i<osm_max_targets; i++) {
355 PVDEV vd = ldm_find_target(vbus, i);
358 if (hpt_flush_vdev(vbus_ext, vd))
359 hpt_flush_vdev(vbus_ext, vd);
364 hpt_unlock_vbus(vbus_ext);
366 ldm_release_vbus(vbus);
368 for (hba=vbus_ext->hba_list; hba; hba=hba->next)
369 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
371 hpt_free_mem(vbus_ext);
373 while ((hba=vbus_ext->hba_list)) {
374 vbus_ext->hba_list = hba->next;
375 free(hba->ldm_adapter.him_handle, M_DEVBUF);
378 callout_drain(&vbus_ext->timer);
379 mtx_destroy(&vbus_ext->lock);
380 free(vbus_ext, M_DEVBUF);
381 KdPrint(("hpt_shutdown_vbus done"));
384 static void __hpt_do_tasks(PVBUS_EXT vbus_ext)
388 tasks = vbus_ext->tasks;
395 t->func(vbus_ext->vbus, t->data);
399 static void hpt_do_tasks(PVBUS_EXT vbus_ext, int pending)
402 hpt_lock_vbus(vbus_ext);
403 __hpt_do_tasks(vbus_ext);
404 hpt_unlock_vbus(vbus_ext);
408 static void hpt_action(struct cam_sim *sim, union ccb *ccb);
409 static void hpt_poll(struct cam_sim *sim);
410 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg);
411 static void hpt_pci_intr(void *arg);
413 static __inline POS_CMDEXT cmdext_get(PVBUS_EXT vbus_ext)
415 POS_CMDEXT p = vbus_ext->cmdext_list;
417 vbus_ext->cmdext_list = p->next;
421 static __inline void cmdext_put(POS_CMDEXT p)
423 p->next = p->vbus_ext->cmdext_list;
424 p->vbus_ext->cmdext_list = p;
427 static void hpt_timeout(void *arg)
429 PCOMMAND pCmd = (PCOMMAND)arg;
430 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
432 KdPrint(("pCmd %p timeout", pCmd));
434 ldm_reset_vbus((PVBUS)ext->vbus_ext->vbus);
437 static void os_cmddone(PCOMMAND pCmd)
439 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
440 union ccb *ccb = ext->ccb;
442 KdPrint(("os_cmddone(%p, %d)", pCmd, pCmd->Result));
444 callout_stop(&ext->timeout);
446 switch(pCmd->Result) {
448 ccb->ccb_h.status = CAM_REQ_CMP;
450 case RETURN_BAD_DEVICE:
451 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
453 case RETURN_DEVICE_BUSY:
454 ccb->ccb_h.status = CAM_BUSY;
456 case RETURN_INVALID_REQUEST:
457 ccb->ccb_h.status = CAM_REQ_INVALID;
459 case RETURN_SELECTION_TIMEOUT:
460 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
463 ccb->ccb_h.status = CAM_BUSY;
466 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
470 if (pCmd->flags.data_in) {
471 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTREAD);
473 else if (pCmd->flags.data_out) {
474 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTWRITE);
477 bus_dmamap_unload(ext->vbus_ext->io_dmat, ext->dma_map);
484 static int os_buildsgl(PCOMMAND pCmd, PSG pSg, int logical)
486 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
487 union ccb *ccb = ext->ccb;
490 os_set_sgptr(pSg, (HPT_U8 *)ccb->csio.data_ptr);
491 pSg->size = ccb->csio.dxfer_len;
496 /* since we have provided physical sg, nobody will ask us to build physical sg */
501 static void hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
503 PCOMMAND pCmd = (PCOMMAND)arg;
504 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
508 HPT_ASSERT(pCmd->flags.physical_sg);
511 panic("busdma error");
513 HPT_ASSERT(nsegs<=os_max_sg_descriptors);
516 for (idx = 0; idx < nsegs; idx++, psg++) {
517 psg->addr.bus = segs[idx].ds_addr;
518 psg->size = segs[idx].ds_len;
523 if (pCmd->flags.data_in) {
524 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
525 BUS_DMASYNC_PREREAD);
527 else if (pCmd->flags.data_out) {
528 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
529 BUS_DMASYNC_PREWRITE);
532 callout_reset(&ext->timeout, HPT_OSM_TIMEOUT, hpt_timeout, pCmd);
536 static void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb)
538 PVBUS vbus = (PVBUS)vbus_ext->vbus;
544 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
545 cdb = ccb->csio.cdb_io.cdb_ptr;
547 cdb = ccb->csio.cdb_io.cdb_bytes;
549 KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x",
551 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
552 *(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8]
555 /* ccb->ccb_h.path_id is not our bus id - don't check it */
556 if (ccb->ccb_h.target_lun != 0 ||
557 ccb->ccb_h.target_id >= osm_max_targets ||
558 (ccb->ccb_h.flags & CAM_CDB_PHYS))
560 ccb->ccb_h.status = CAM_TID_INVALID;
565 vd = ldm_find_target(vbus, ccb->ccb_h.target_id);
568 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
574 case TEST_UNIT_READY:
575 case START_STOP_UNIT:
576 case SYNCHRONIZE_CACHE:
577 ccb->ccb_h.status = CAM_REQ_CMP;
582 PINQUIRYDATA inquiryData;
583 memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len);
584 inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr;
586 inquiryData->AdditionalLength = 31;
587 inquiryData->CommandQueue = 1;
588 memcpy(&inquiryData->VendorId, "HPT ", 8);
589 memcpy(&inquiryData->ProductId, "DISK 0_0 ", 16);
591 if (vd->target_id / 10) {
592 inquiryData->ProductId[7] = (vd->target_id % 100) / 10 + '0';
593 inquiryData->ProductId[8] = (vd->target_id % 100) % 10 + '0';
596 inquiryData->ProductId[7] = (vd->target_id % 100) % 10 + '0';
598 memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4);
600 ccb->ccb_h.status = CAM_REQ_CMP;
606 HPT_U8 *rbuf = ccb->csio.data_ptr;
609 if (vd->capacity>0xfffffffful)
612 cap = vd->capacity - 1;
614 rbuf[0] = (HPT_U8)(cap>>24);
615 rbuf[1] = (HPT_U8)(cap>>16);
616 rbuf[2] = (HPT_U8)(cap>>8);
617 rbuf[3] = (HPT_U8)cap;
623 ccb->ccb_h.status = CAM_REQ_CMP;
627 case SERVICE_ACTION_IN:
629 HPT_U8 *rbuf = ccb->csio.data_ptr;
630 HPT_U64 cap = vd->capacity - 1;
632 rbuf[0] = (HPT_U8)(cap>>56);
633 rbuf[1] = (HPT_U8)(cap>>48);
634 rbuf[2] = (HPT_U8)(cap>>40);
635 rbuf[3] = (HPT_U8)(cap>>32);
636 rbuf[4] = (HPT_U8)(cap>>24);
637 rbuf[5] = (HPT_U8)(cap>>16);
638 rbuf[6] = (HPT_U8)(cap>>8);
639 rbuf[7] = (HPT_U8)cap;
645 ccb->ccb_h.status = CAM_REQ_CMP;
660 pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
662 KdPrint(("Failed to allocate command!"));
663 ccb->ccb_h.status = CAM_BUSY;
671 pCmd->uCmd.Ide.Lba = ((HPT_U32)cdb[1] << 16) | ((HPT_U32)cdb[2] << 8) | (HPT_U32)cdb[3];
672 pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[4];
678 ((HPT_U64)cdb[2]<<56) |
679 ((HPT_U64)cdb[3]<<48) |
680 ((HPT_U64)cdb[4]<<40) |
681 ((HPT_U64)cdb[5]<<32) |
682 ((HPT_U64)cdb[6]<<24) |
683 ((HPT_U64)cdb[7]<<16) |
684 ((HPT_U64)cdb[8]<<8) |
686 pCmd->uCmd.Ide.Lba = block;
687 pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8);
692 pCmd->uCmd.Ide.Lba = (HPT_U32)cdb[5] | ((HPT_U32)cdb[4] << 8) | ((HPT_U32)cdb[3] << 16) | ((HPT_U32)cdb[2] << 24);
693 pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[8] | ((HPT_U16)cdb[7]<<8);
701 pCmd->flags.data_in = 1;
706 pCmd->flags.data_out = 1;
709 pCmd->priv = ext = cmdext_get(vbus_ext);
713 pCmd->done = os_cmddone;
714 pCmd->buildsgl = os_buildsgl;
715 pCmd->psg = ext->psg;
716 pCmd->flags.physical_sg = 1;
717 error = bus_dmamap_load_ccb(vbus_ext->io_dmat,
720 hpt_io_dmamap_callback, pCmd,
723 KdPrint(("bus_dmamap_load return %d", error));
724 if (error && error!=EINPROGRESS) {
725 os_printk("bus_dmamap_load error %d", error);
728 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
735 ccb->ccb_h.status = CAM_REQ_INVALID;
743 static void hpt_action(struct cam_sim *sim, union ccb *ccb)
745 PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
747 KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id));
749 hpt_assert_vbus_locked(vbus_ext);
750 switch (ccb->ccb_h.func_code) {
753 hpt_scsi_io(vbus_ext, ccb);
757 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
760 case XPT_GET_TRAN_SETTINGS:
761 case XPT_SET_TRAN_SETTINGS:
762 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
765 case XPT_CALC_GEOMETRY:
766 cam_calc_geometry(&ccb->ccg, 1);
771 struct ccb_pathinq *cpi = &ccb->cpi;
773 cpi->version_num = 1;
774 cpi->hba_inquiry = PI_SDTR_ABLE;
775 cpi->target_sprt = 0;
776 cpi->hba_misc = PIM_NOBUSRESET;
777 cpi->hba_eng_cnt = 0;
778 cpi->max_target = osm_max_targets;
780 cpi->unit_number = cam_sim_unit(sim);
781 cpi->bus_id = cam_sim_bus(sim);
782 cpi->initiator_id = osm_max_targets;
783 cpi->base_transfer_speed = 3300;
785 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
786 strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
787 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
788 cpi->transport = XPORT_SPI;
789 cpi->transport_version = 2;
790 cpi->protocol = PROTO_SCSI;
791 cpi->protocol_version = SCSI_REV_2;
792 cpi->ccb_h.status = CAM_REQ_CMP;
797 ccb->ccb_h.status = CAM_REQ_INVALID;
805 static void hpt_pci_intr(void *arg)
807 PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
808 hpt_lock_vbus(vbus_ext);
809 ldm_intr((PVBUS)vbus_ext->vbus);
810 hpt_unlock_vbus(vbus_ext);
813 static void hpt_poll(struct cam_sim *sim)
815 PVBUS_EXT vbus_ext = cam_sim_softc(sim);
816 hpt_assert_vbus_locked(vbus_ext);
817 ldm_intr((PVBUS)vbus_ext->vbus);
820 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg)
822 KdPrint(("hpt_async"));
825 static int hpt_shutdown(device_t dev)
827 KdPrint(("hpt_shutdown(dev=%p)", dev));
831 static int hpt_detach(device_t dev)
833 /* we don't allow the driver to be unloaded. */
837 static void hpt_ioctl_done(struct _IOCTL_ARG *arg)
843 static void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args)
845 ioctl_args->result = -1;
846 ioctl_args->done = hpt_ioctl_done;
847 ioctl_args->ioctl_cmnd = (void *)1;
849 hpt_lock_vbus(vbus_ext);
850 ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args);
852 while (ioctl_args->ioctl_cmnd) {
853 if (hpt_sleep(vbus_ext, ioctl_args, PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
855 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
856 __hpt_do_tasks(vbus_ext);
859 /* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */
861 hpt_unlock_vbus(vbus_ext);
864 static void hpt_do_ioctl(IOCTL_ARG *ioctl_args)
869 ldm_for_each_vbus(vbus, vbus_ext) {
870 __hpt_do_ioctl(vbus_ext, ioctl_args);
871 if (ioctl_args->result!=HPT_IOCTL_RESULT_WRONG_VBUS)
876 #define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\
878 arg.dwIoControlCode = code;\
879 arg.lpInBuffer = inbuf;\
880 arg.lpOutBuffer = outbuf;\
881 arg.nInBufferSize = insize;\
882 arg.nOutBufferSize = outsize;\
883 arg.lpBytesReturned = 0;\
888 #define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff))
890 static int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount)
893 HPT_U32 count = nMaxCount-1;
895 if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES,
896 &count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount))
899 nMaxCount = (int)pIds[0];
900 for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1];
904 static int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo)
906 return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3,
907 &id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3));
910 /* not belong to this file logically, but we want to use ioctl interface */
911 static int __hpt_stop_tasks(PVBUS_EXT vbus_ext, DEVICEID id)
913 LOGICAL_DEVICE_INFO_V3 devinfo;
915 DEVICEID param[2] = { id, 0 };
917 if (hpt_get_device_info_v3(id, &devinfo))
920 if (devinfo.Type!=LDT_ARRAY)
923 if (devinfo.u.array.Flags & ARRAY_FLAG_REBUILDING)
924 param[1] = AS_REBUILD_ABORT;
925 else if (devinfo.u.array.Flags & ARRAY_FLAG_VERIFYING)
926 param[1] = AS_VERIFY_ABORT;
927 else if (devinfo.u.array.Flags & ARRAY_FLAG_INITIALIZING)
928 param[1] = AS_INITIALIZE_ABORT;
929 else if (devinfo.u.array.Flags & ARRAY_FLAG_TRANSFORMING)
930 param[1] = AS_TRANSFORM_ABORT;
934 KdPrint(("SET_ARRAY_STATE(%x, %d)", param[0], param[1]));
935 result = HPT_DO_IOCTL(HPT_IOCTL_SET_ARRAY_STATE,
936 param, sizeof(param), 0, 0);
938 for (i=0; i<devinfo.u.array.nDisk; i++)
939 if (DEVICEID_VALID(devinfo.u.array.Members[i]))
940 __hpt_stop_tasks(vbus_ext, devinfo.u.array.Members[i]);
945 static void hpt_stop_tasks(PVBUS_EXT vbus_ext)
950 count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0]));
952 for (i=0; i<count; i++)
953 __hpt_stop_tasks(vbus_ext, ids[i]);
956 static d_open_t hpt_open;
957 static d_close_t hpt_close;
958 static d_ioctl_t hpt_ioctl;
959 static int hpt_rescan_bus(void);
961 static struct cdevsw hpt_cdevsw = {
963 .d_close = hpt_close,
964 .d_ioctl = hpt_ioctl,
965 .d_name = driver_name,
966 .d_version = D_VERSION,
969 static struct intr_config_hook hpt_ich;
972 * hpt_final_init will be called after all hpt_attach.
974 static void hpt_final_init(void *dummy)
981 /* Clear the config hook */
982 config_intrhook_disestablish(&hpt_ich);
984 /* allocate memory */
986 ldm_for_each_vbus(vbus, vbus_ext) {
987 if (hpt_alloc_mem(vbus_ext)) {
988 os_printk("out of memory");
996 os_printk("no controller detected.");
1000 /* initializing hardware */
1001 ldm_for_each_vbus(vbus, vbus_ext) {
1002 /* make timer available here */
1003 mtx_init(&vbus_ext->lock, "hptsleeplock", NULL, MTX_DEF);
1004 callout_init_mtx(&vbus_ext->timer, &vbus_ext->lock, 0);
1005 if (hpt_init_vbus(vbus_ext)) {
1006 os_printk("fail to initialize hardware");
1011 /* register CAM interface */
1012 ldm_for_each_vbus(vbus, vbus_ext) {
1013 struct cam_devq *devq;
1014 struct ccb_setasync ccb;
1016 if (bus_dma_tag_create(NULL,/* parent */
1018 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1019 BUS_SPACE_MAXADDR, /* lowaddr */
1020 BUS_SPACE_MAXADDR, /* highaddr */
1021 NULL, NULL, /* filter, filterarg */
1022 PAGE_SIZE * (os_max_sg_descriptors-1), /* maxsize */
1023 os_max_sg_descriptors, /* nsegments */
1024 0x10000, /* maxsegsize */
1025 BUS_DMA_WAITOK, /* flags */
1026 busdma_lock_mutex, /* lockfunc */
1027 &vbus_ext->lock, /* lockfuncarg */
1028 &vbus_ext->io_dmat /* tag */))
1033 for (i=0; i<os_max_queue_comm; i++) {
1034 POS_CMDEXT ext = (POS_CMDEXT)malloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK);
1036 os_printk("Can't alloc cmdext(%d)", i);
1039 ext->vbus_ext = vbus_ext;
1040 ext->next = vbus_ext->cmdext_list;
1041 vbus_ext->cmdext_list = ext;
1043 if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) {
1044 os_printk("Can't create dma map(%d)", i);
1047 callout_init_mtx(&ext->timeout, &vbus_ext->lock, 0);
1050 if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) {
1051 os_printk("cam_simq_alloc failed");
1055 vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1056 vbus_ext, 0, &vbus_ext->lock, os_max_queue_comm,
1059 if (!vbus_ext->sim) {
1060 os_printk("cam_sim_alloc failed");
1061 cam_simq_free(devq);
1065 hpt_lock_vbus(vbus_ext);
1066 if (xpt_bus_register(vbus_ext->sim, NULL, 0) != CAM_SUCCESS) {
1067 os_printk("xpt_bus_register failed");
1068 cam_sim_free(vbus_ext->sim, /*free devq*/ TRUE);
1069 hpt_unlock_vbus(vbus_ext);
1070 vbus_ext->sim = NULL;
1074 if (xpt_create_path(&vbus_ext->path, /*periph */ NULL,
1075 cam_sim_path(vbus_ext->sim), CAM_TARGET_WILDCARD,
1076 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1078 os_printk("xpt_create_path failed");
1079 xpt_bus_deregister(cam_sim_path(vbus_ext->sim));
1080 cam_sim_free(vbus_ext->sim, /*free_devq*/TRUE);
1081 hpt_unlock_vbus(vbus_ext);
1082 vbus_ext->sim = NULL;
1085 hpt_unlock_vbus(vbus_ext);
1087 xpt_setup_ccb(&ccb.ccb_h, vbus_ext->path, /*priority*/5);
1088 ccb.ccb_h.func_code = XPT_SASYNC_CB;
1089 ccb.event_enable = AC_LOST_DEVICE;
1090 ccb.callback = hpt_async;
1091 ccb.callback_arg = vbus_ext;
1092 xpt_action((union ccb *)&ccb);
1094 for (hba = vbus_ext->hba_list; hba; hba = hba->next) {
1096 if ((hba->irq_res = bus_alloc_resource_any(hba->pcidev,
1097 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1099 os_printk("can't allocate interrupt");
1103 if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM | INTR_MPSAFE,
1104 NULL, hpt_pci_intr, vbus_ext, &hba->irq_handle))
1106 os_printk("can't set up interrupt");
1109 hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE);
1112 vbus_ext->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1113 hpt_shutdown_vbus, vbus_ext, SHUTDOWN_PRI_DEFAULT);
1114 if (!vbus_ext->shutdown_eh)
1115 os_printk("Shutdown event registration failed");
1118 ldm_for_each_vbus(vbus, vbus_ext) {
1119 TASK_INIT(&vbus_ext->worker, 0, (task_fn_t *)hpt_do_tasks, vbus_ext);
1120 if (vbus_ext->tasks)
1121 TASK_ENQUEUE(&vbus_ext->worker);
1124 make_dev(&hpt_cdevsw, DRIVER_MINOR, UID_ROOT, GID_OPERATOR,
1125 S_IRUSR | S_IWUSR, "%s", driver_name);
1128 #if defined(KLD_MODULE)
1130 typedef struct driverlink *driverlink_t;
1132 kobj_class_t driver;
1133 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
1136 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
1139 TAILQ_ENTRY(devclass) link;
1140 devclass_t parent; /* parent in devclass hierarchy */
1141 driver_list_t drivers; /* bus devclasses store drivers for bus */
1143 device_t *devices; /* array of devices indexed by unit */
1144 int maxunit; /* size of devices array */
1147 static void override_kernel_driver(void)
1149 driverlink_t dl, dlfirst;
1150 driver_t *tmpdriver;
1151 devclass_t dc = devclass_find("pci");
1154 dlfirst = TAILQ_FIRST(&dc->drivers);
1155 for (dl = dlfirst; dl; dl = TAILQ_NEXT(dl, link)) {
1156 if(strcmp(dl->driver->name, driver_name) == 0) {
1157 tmpdriver=dl->driver;
1158 dl->driver=dlfirst->driver;
1159 dlfirst->driver=tmpdriver;
1167 #define override_kernel_driver()
1170 static void hpt_init(void *dummy)
1173 os_printk("%s %s", driver_name_long, driver_ver);
1175 override_kernel_driver();
1178 hpt_ich.ich_func = hpt_final_init;
1179 hpt_ich.ich_arg = NULL;
1180 if (config_intrhook_establish(&hpt_ich) != 0) {
1181 printf("%s: cannot establish configuration hook\n",
1186 SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
1189 * CAM driver interface
1191 static device_method_t driver_methods[] = {
1192 /* Device interface */
1193 DEVMETHOD(device_probe, hpt_probe),
1194 DEVMETHOD(device_attach, hpt_attach),
1195 DEVMETHOD(device_detach, hpt_detach),
1196 DEVMETHOD(device_shutdown, hpt_shutdown),
1200 static driver_t hpt_pci_driver = {
1206 static devclass_t hpt_devclass;
1209 #error "no TARGETNAME found"
1212 /* use this to make TARGETNAME be expanded */
1213 #define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
1214 #define __MODULE_VERSION(p1, p2) MODULE_VERSION(p1, p2)
1215 #define __MODULE_DEPEND(p1, p2, p3, p4, p5) MODULE_DEPEND(p1, p2, p3, p4, p5)
1216 __DRIVER_MODULE(TARGETNAME, pci, hpt_pci_driver, hpt_devclass, 0, 0);
1217 __MODULE_VERSION(TARGETNAME, 1);
1218 __MODULE_DEPEND(TARGETNAME, cam, 1, 1, 1);
1220 static int hpt_open(struct cdev *dev, int flags, int devtype, struct thread *td)
1225 static int hpt_close(struct cdev *dev, int flags, int devtype, struct thread *td)
1230 static int hpt_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1232 PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data;
1233 IOCTL_ARG ioctl_args;
1234 HPT_U32 bytesReturned;
1237 case HPT_DO_IOCONTROL:
1239 if (piop->Magic == HPT_IOCTL_MAGIC || piop->Magic == HPT_IOCTL_MAGIC32) {
1240 KdPrint(("ioctl=%x in=%p len=%d out=%p len=%d\n",
1241 piop->dwIoControlCode,
1243 piop->nInBufferSize,
1245 piop->nOutBufferSize));
1247 memset(&ioctl_args, 0, sizeof(ioctl_args));
1249 ioctl_args.dwIoControlCode = piop->dwIoControlCode;
1250 ioctl_args.nInBufferSize = piop->nInBufferSize;
1251 ioctl_args.nOutBufferSize = piop->nOutBufferSize;
1252 ioctl_args.lpBytesReturned = &bytesReturned;
1254 if (ioctl_args.nInBufferSize) {
1255 ioctl_args.lpInBuffer = malloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK);
1256 if (!ioctl_args.lpInBuffer)
1258 if (copyin((void*)piop->lpInBuffer,
1259 ioctl_args.lpInBuffer, piop->nInBufferSize))
1263 if (ioctl_args.nOutBufferSize) {
1264 ioctl_args.lpOutBuffer = malloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK);
1265 if (!ioctl_args.lpOutBuffer)
1269 hpt_do_ioctl(&ioctl_args);
1271 if (ioctl_args.result==HPT_IOCTL_RESULT_OK) {
1272 if (piop->nOutBufferSize) {
1273 if (copyout(ioctl_args.lpOutBuffer,
1274 (void*)piop->lpOutBuffer, piop->nOutBufferSize))
1277 if (piop->lpBytesReturned) {
1278 if (copyout(&bytesReturned,
1279 (void*)piop->lpBytesReturned, sizeof(HPT_U32)))
1282 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1283 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1287 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1288 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1296 return hpt_rescan_bus();
1299 KdPrint(("invalid command!"));
1305 static int hpt_rescan_bus(void)
1311 ldm_for_each_vbus(vbus, vbus_ext) {
1312 if ((ccb = xpt_alloc_ccb()) == NULL)
1314 if (xpt_create_path(&ccb->ccb_h.path, NULL,
1315 cam_sim_path(vbus_ext->sim),
1316 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {