2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <dev/hptrr/hptrr_config.h>
33 /* $Id: osm_bsd.c,v 1.27 2007/11/22 07:35:49 gmm Exp $
35 * HighPoint RAID Driver for FreeBSD
36 * Copyright (C) 2005 HighPoint Technologies, Inc. All Rights Reserved.
38 #include <dev/hptrr/os_bsd.h>
39 #include <dev/hptrr/hptintf.h>
41 static int attach_generic = 0;
42 TUNABLE_INT("hw.hptrr.attach_generic", &attach_generic);
44 static HIM *hpt_match(device_t dev)
50 /* Some of supported chips are used not only by HPT. */
51 if (pci_get_vendor(dev) != 0x1103 && !attach_generic)
53 for (him = him_list; him; him = him->next) {
54 for (i=0; him->get_supported_device_id(i, &pci_id); i++) {
55 if ((pci_get_vendor(dev) == pci_id.vid) &&
56 (pci_get_device(dev) == pci_id.did)){
64 static int hpt_probe(device_t dev)
70 KdPrint(("hpt_probe: adapter at PCI %d:%d:%d, IRQ %d",
71 pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev), pci_get_irq(dev)
73 device_set_desc(dev, him->name);
74 return (BUS_PROBE_DEFAULT);
80 static int hpt_attach(device_t dev)
82 PHBA hba = (PHBA)device_get_softc(dev);
89 KdPrint(("hpt_attach(%d/%d/%d)", pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)));
92 hba->ext_type = EXT_TYPE_HBA;
93 hba->ldm_adapter.him = him;
95 pci_enable_busmaster(dev);
97 pci_id.vid = pci_get_vendor(dev);
98 pci_id.did = pci_get_device(dev);
99 pci_id.rev = pci_get_revid(dev);
101 size = him->get_adapter_size(&pci_id);
102 hba->ldm_adapter.him_handle = malloc(size, M_DEVBUF, M_WAITOK);
105 hba->pciaddr.tree = 0;
106 hba->pciaddr.bus = pci_get_bus(dev);
107 hba->pciaddr.device = pci_get_slot(dev);
108 hba->pciaddr.function = pci_get_function(dev);
110 if (!him->create_adapter(&pci_id, hba->pciaddr, hba->ldm_adapter.him_handle, hba)) {
111 free(hba->ldm_adapter.him_handle, M_DEVBUF);
115 os_printk("adapter at PCI %d:%d:%d, IRQ %d",
116 hba->pciaddr.bus, hba->pciaddr.device, hba->pciaddr.function, pci_get_irq(dev));
118 if (!ldm_register_adapter(&hba->ldm_adapter)) {
119 size = ldm_get_vbus_size();
120 vbus_ext = malloc(sizeof(VBUS_EXT) + size, M_DEVBUF, M_WAITOK |
122 vbus_ext->ext_type = EXT_TYPE_VBUS;
123 ldm_create_vbus((PVBUS)vbus_ext->vbus, vbus_ext);
124 ldm_register_adapter(&hba->ldm_adapter);
127 ldm_for_each_vbus(vbus, vbus_ext) {
128 if (hba->ldm_adapter.vbus==vbus) {
129 hba->vbus_ext = vbus_ext;
130 hba->next = vbus_ext->hba_list;
131 vbus_ext->hba_list = hba;
139 * Maybe we'd better to use the bus_dmamem_alloc to alloc DMA memory,
140 * but there are some problems currently (alignment, etc).
142 static __inline void *__get_free_pages(int order)
144 /* don't use low memory - other devices may get starved */
145 return contigmalloc(PAGE_SIZE<<order,
146 M_DEVBUF, M_WAITOK, BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
149 static __inline void free_pages(void *p, int order)
151 contigfree(p, PAGE_SIZE<<order, M_DEVBUF);
154 static int hpt_alloc_mem(PVBUS_EXT vbus_ext)
161 for (hba = vbus_ext->hba_list; hba; hba = hba->next)
162 hba->ldm_adapter.him->get_meminfo(hba->ldm_adapter.him_handle);
164 ldm_get_mem_info((PVBUS)vbus_ext->vbus, 0);
166 for (f=vbus_ext->freelist_head; f; f=f->next) {
167 KdPrint(("%s: %d*%d=%d bytes",
168 f->tag, f->count, f->size, f->count*f->size));
169 for (i=0; i<f->count; i++) {
170 p = (void **)malloc(f->size, M_DEVBUF, M_WAITOK);
171 if (!p) return (ENXIO);
177 for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
180 HPT_ASSERT((f->size & (f->alignment-1))==0);
182 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1)
185 KdPrint(("%s: %d*%d=%d bytes, order %d",
186 f->tag, f->count, f->size, f->count*f->size, order));
187 HPT_ASSERT(f->alignment<=PAGE_SIZE);
189 for (i=0; i<f->count;) {
190 p = (void **)__get_free_pages(order);
192 for (j = size/f->size; j && i<f->count; i++,j--) {
194 *(BUS_ADDRESS *)(p+1) = (BUS_ADDRESS)vtophys(p);
196 p = (void **)((unsigned long)p + f->size);
201 HPT_ASSERT(PAGE_SIZE==DMAPOOL_PAGE_SIZE);
203 for (i=0; i<os_max_cache_pages; i++) {
204 p = (void **)__get_free_pages(0);
206 HPT_ASSERT(((HPT_UPTR)p & (DMAPOOL_PAGE_SIZE-1))==0);
207 dmapool_put_page((PVBUS)vbus_ext->vbus, p, (BUS_ADDRESS)vtophys(p));
213 static void hpt_free_mem(PVBUS_EXT vbus_ext)
220 for (f=vbus_ext->freelist_head; f; f=f->next) {
222 if (f->count!=f->reserved_count) {
223 KdPrint(("memory leak for freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
226 while ((p=freelist_get(f)))
230 for (i=0; i<os_max_cache_pages; i++) {
231 p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus);
236 for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
239 if (f->count!=f->reserved_count) {
240 KdPrint(("memory leak for dma freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
243 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ;
245 while ((p=freelist_get_dma(f, &bus))) {
247 free_pages(p, order);
249 /* can't free immediately since other blocks in this page may still be in the list */
250 if (((HPT_UPTR)p & (PAGE_SIZE-1))==0)
251 dmapool_put_page((PVBUS)vbus_ext->vbus, p, bus);
256 while ((p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus)))
260 static int hpt_init_vbus(PVBUS_EXT vbus_ext)
264 for (hba = vbus_ext->hba_list; hba; hba = hba->next)
265 if (!hba->ldm_adapter.him->initialize(hba->ldm_adapter.him_handle)) {
266 KdPrint(("fail to initialize %p", hba));
270 ldm_initialize_vbus((PVBUS)vbus_ext->vbus, &vbus_ext->hba_list->ldm_adapter);
274 static void hpt_flush_done(PCOMMAND pCmd)
276 PVDEV vd = pCmd->target;
278 if (mIsArray(vd->type) && vd->u.array.transform && vd!=vd->u.array.transform->target) {
279 vd = vd->u.array.transform->target;
282 pCmd->Result = RETURN_PENDING;
283 vdev_queue_cmd(pCmd);
287 *(int *)pCmd->priv = 1;
292 * flush a vdev (without retry).
294 static int hpt_flush_vdev(PVBUS_EXT vbus_ext, PVDEV vd)
297 int result = 0, done;
300 KdPrint(("flusing dev %p", vd));
302 hpt_assert_vbus_locked(vbus_ext);
304 if (mIsArray(vd->type) && vd->u.array.transform)
305 count = max(vd->u.array.transform->source->cmds_per_request,
306 vd->u.array.transform->target->cmds_per_request);
308 count = vd->cmds_per_request;
310 pCmd = ldm_alloc_cmds(vd->vbus, count);
316 pCmd->type = CMD_TYPE_FLUSH;
317 pCmd->flags.hard_flush = 1;
319 pCmd->done = hpt_flush_done;
326 while (hpt_sleep(vbus_ext, pCmd, PPAUSE, "hptfls", HPT_OSM_TIMEOUT)) {
327 ldm_reset_vbus(vd->vbus);
331 KdPrint(("flush result %d", pCmd->Result));
333 if (pCmd->Result!=RETURN_SUCCESS)
341 static void hpt_stop_tasks(PVBUS_EXT vbus_ext);
342 static void hpt_shutdown_vbus(PVBUS_EXT vbus_ext, int howto)
344 PVBUS vbus = (PVBUS)vbus_ext->vbus;
348 KdPrint(("hpt_shutdown_vbus"));
350 /* stop all ctl tasks and disable the worker taskqueue */
351 hpt_stop_tasks(vbus_ext);
352 hpt_lock_vbus(vbus_ext);
353 vbus_ext->worker.ta_context = 0;
356 for (i=0; i<osm_max_targets; i++) {
357 PVDEV vd = ldm_find_target(vbus, i);
360 if (hpt_flush_vdev(vbus_ext, vd))
361 hpt_flush_vdev(vbus_ext, vd);
366 hpt_unlock_vbus(vbus_ext);
368 ldm_release_vbus(vbus);
370 for (hba=vbus_ext->hba_list; hba; hba=hba->next)
371 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
373 hpt_free_mem(vbus_ext);
375 while ((hba=vbus_ext->hba_list)) {
376 vbus_ext->hba_list = hba->next;
377 free(hba->ldm_adapter.him_handle, M_DEVBUF);
380 callout_drain(&vbus_ext->timer);
381 mtx_destroy(&vbus_ext->lock);
382 free(vbus_ext, M_DEVBUF);
383 KdPrint(("hpt_shutdown_vbus done"));
386 static void __hpt_do_tasks(PVBUS_EXT vbus_ext)
390 tasks = vbus_ext->tasks;
397 t->func(vbus_ext->vbus, t->data);
401 static void hpt_do_tasks(PVBUS_EXT vbus_ext, int pending)
404 hpt_lock_vbus(vbus_ext);
405 __hpt_do_tasks(vbus_ext);
406 hpt_unlock_vbus(vbus_ext);
410 static void hpt_action(struct cam_sim *sim, union ccb *ccb);
411 static void hpt_poll(struct cam_sim *sim);
412 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg);
413 static void hpt_pci_intr(void *arg);
415 static __inline POS_CMDEXT cmdext_get(PVBUS_EXT vbus_ext)
417 POS_CMDEXT p = vbus_ext->cmdext_list;
419 vbus_ext->cmdext_list = p->next;
423 static __inline void cmdext_put(POS_CMDEXT p)
425 p->next = p->vbus_ext->cmdext_list;
426 p->vbus_ext->cmdext_list = p;
429 static void hpt_timeout(void *arg)
431 PCOMMAND pCmd = (PCOMMAND)arg;
432 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
434 KdPrint(("pCmd %p timeout", pCmd));
436 ldm_reset_vbus((PVBUS)ext->vbus_ext->vbus);
439 static void os_cmddone(PCOMMAND pCmd)
441 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
442 union ccb *ccb = ext->ccb;
444 KdPrint(("os_cmddone(%p, %d)", pCmd, pCmd->Result));
446 callout_stop(&ext->timeout);
448 switch(pCmd->Result) {
450 ccb->ccb_h.status = CAM_REQ_CMP;
452 case RETURN_BAD_DEVICE:
453 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
455 case RETURN_DEVICE_BUSY:
456 ccb->ccb_h.status = CAM_BUSY;
458 case RETURN_INVALID_REQUEST:
459 ccb->ccb_h.status = CAM_REQ_INVALID;
461 case RETURN_SELECTION_TIMEOUT:
462 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
465 ccb->ccb_h.status = CAM_BUSY;
468 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
472 if (pCmd->flags.data_in) {
473 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTREAD);
475 else if (pCmd->flags.data_out) {
476 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTWRITE);
479 bus_dmamap_unload(ext->vbus_ext->io_dmat, ext->dma_map);
486 static int os_buildsgl(PCOMMAND pCmd, PSG pSg, int logical)
488 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
489 union ccb *ccb = ext->ccb;
492 os_set_sgptr(pSg, (HPT_U8 *)ccb->csio.data_ptr);
493 pSg->size = ccb->csio.dxfer_len;
498 /* since we have provided physical sg, nobody will ask us to build physical sg */
503 static void hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
505 PCOMMAND pCmd = (PCOMMAND)arg;
506 POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
510 HPT_ASSERT(pCmd->flags.physical_sg);
513 panic("busdma error");
515 HPT_ASSERT(nsegs<=os_max_sg_descriptors);
518 for (idx = 0; idx < nsegs; idx++, psg++) {
519 psg->addr.bus = segs[idx].ds_addr;
520 psg->size = segs[idx].ds_len;
525 if (pCmd->flags.data_in) {
526 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
527 BUS_DMASYNC_PREREAD);
529 else if (pCmd->flags.data_out) {
530 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
531 BUS_DMASYNC_PREWRITE);
534 callout_reset(&ext->timeout, HPT_OSM_TIMEOUT, hpt_timeout, pCmd);
538 static void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb)
540 PVBUS vbus = (PVBUS)vbus_ext->vbus;
546 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
547 cdb = ccb->csio.cdb_io.cdb_ptr;
549 cdb = ccb->csio.cdb_io.cdb_bytes;
551 KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x",
553 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
554 *(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8]
557 /* ccb->ccb_h.path_id is not our bus id - don't check it */
558 if (ccb->ccb_h.target_lun != 0 ||
559 ccb->ccb_h.target_id >= osm_max_targets ||
560 (ccb->ccb_h.flags & CAM_CDB_PHYS))
562 ccb->ccb_h.status = CAM_TID_INVALID;
567 vd = ldm_find_target(vbus, ccb->ccb_h.target_id);
570 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
576 case TEST_UNIT_READY:
577 case START_STOP_UNIT:
578 case SYNCHRONIZE_CACHE:
579 ccb->ccb_h.status = CAM_REQ_CMP;
584 PINQUIRYDATA inquiryData;
585 memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len);
586 inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr;
588 inquiryData->AdditionalLength = 31;
589 inquiryData->CommandQueue = 1;
590 memcpy(&inquiryData->VendorId, "HPT ", 8);
591 memcpy(&inquiryData->ProductId, "DISK 0_0 ", 16);
593 if (vd->target_id / 10) {
594 inquiryData->ProductId[7] = (vd->target_id % 100) / 10 + '0';
595 inquiryData->ProductId[8] = (vd->target_id % 100) % 10 + '0';
598 inquiryData->ProductId[7] = (vd->target_id % 100) % 10 + '0';
600 memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4);
602 ccb->ccb_h.status = CAM_REQ_CMP;
608 HPT_U8 *rbuf = ccb->csio.data_ptr;
611 if (vd->capacity>0xfffffffful)
614 cap = vd->capacity - 1;
616 rbuf[0] = (HPT_U8)(cap>>24);
617 rbuf[1] = (HPT_U8)(cap>>16);
618 rbuf[2] = (HPT_U8)(cap>>8);
619 rbuf[3] = (HPT_U8)cap;
625 ccb->ccb_h.status = CAM_REQ_CMP;
629 case SERVICE_ACTION_IN:
631 HPT_U8 *rbuf = ccb->csio.data_ptr;
632 HPT_U64 cap = vd->capacity - 1;
634 rbuf[0] = (HPT_U8)(cap>>56);
635 rbuf[1] = (HPT_U8)(cap>>48);
636 rbuf[2] = (HPT_U8)(cap>>40);
637 rbuf[3] = (HPT_U8)(cap>>32);
638 rbuf[4] = (HPT_U8)(cap>>24);
639 rbuf[5] = (HPT_U8)(cap>>16);
640 rbuf[6] = (HPT_U8)(cap>>8);
641 rbuf[7] = (HPT_U8)cap;
647 ccb->ccb_h.status = CAM_REQ_CMP;
662 pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
664 KdPrint(("Failed to allocate command!"));
665 ccb->ccb_h.status = CAM_BUSY;
673 pCmd->uCmd.Ide.Lba = ((HPT_U32)cdb[1] << 16) | ((HPT_U32)cdb[2] << 8) | (HPT_U32)cdb[3];
674 pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[4];
680 ((HPT_U64)cdb[2]<<56) |
681 ((HPT_U64)cdb[3]<<48) |
682 ((HPT_U64)cdb[4]<<40) |
683 ((HPT_U64)cdb[5]<<32) |
684 ((HPT_U64)cdb[6]<<24) |
685 ((HPT_U64)cdb[7]<<16) |
686 ((HPT_U64)cdb[8]<<8) |
688 pCmd->uCmd.Ide.Lba = block;
689 pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8);
694 pCmd->uCmd.Ide.Lba = (HPT_U32)cdb[5] | ((HPT_U32)cdb[4] << 8) | ((HPT_U32)cdb[3] << 16) | ((HPT_U32)cdb[2] << 24);
695 pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[8] | ((HPT_U16)cdb[7]<<8);
703 pCmd->flags.data_in = 1;
708 pCmd->flags.data_out = 1;
711 pCmd->priv = ext = cmdext_get(vbus_ext);
715 pCmd->done = os_cmddone;
716 pCmd->buildsgl = os_buildsgl;
717 pCmd->psg = ext->psg;
718 pCmd->flags.physical_sg = 1;
719 error = bus_dmamap_load_ccb(vbus_ext->io_dmat,
722 hpt_io_dmamap_callback, pCmd,
725 KdPrint(("bus_dmamap_load return %d", error));
726 if (error && error!=EINPROGRESS) {
727 os_printk("bus_dmamap_load error %d", error);
730 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
737 ccb->ccb_h.status = CAM_REQ_INVALID;
745 static void hpt_action(struct cam_sim *sim, union ccb *ccb)
747 PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
749 KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id));
751 hpt_assert_vbus_locked(vbus_ext);
752 switch (ccb->ccb_h.func_code) {
755 hpt_scsi_io(vbus_ext, ccb);
759 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
762 case XPT_GET_TRAN_SETTINGS:
763 case XPT_SET_TRAN_SETTINGS:
764 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
767 case XPT_CALC_GEOMETRY:
768 cam_calc_geometry(&ccb->ccg, 1);
773 struct ccb_pathinq *cpi = &ccb->cpi;
775 cpi->version_num = 1;
776 cpi->hba_inquiry = PI_SDTR_ABLE;
777 cpi->target_sprt = 0;
778 cpi->hba_misc = PIM_NOBUSRESET;
779 cpi->hba_eng_cnt = 0;
780 cpi->max_target = osm_max_targets;
782 cpi->unit_number = cam_sim_unit(sim);
783 cpi->bus_id = cam_sim_bus(sim);
784 cpi->initiator_id = osm_max_targets;
785 cpi->base_transfer_speed = 3300;
787 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
788 strlcpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
789 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
790 cpi->transport = XPORT_SPI;
791 cpi->transport_version = 2;
792 cpi->protocol = PROTO_SCSI;
793 cpi->protocol_version = SCSI_REV_2;
794 cpi->ccb_h.status = CAM_REQ_CMP;
799 ccb->ccb_h.status = CAM_REQ_INVALID;
807 static void hpt_pci_intr(void *arg)
809 PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
810 hpt_lock_vbus(vbus_ext);
811 ldm_intr((PVBUS)vbus_ext->vbus);
812 hpt_unlock_vbus(vbus_ext);
815 static void hpt_poll(struct cam_sim *sim)
817 PVBUS_EXT vbus_ext = cam_sim_softc(sim);
818 hpt_assert_vbus_locked(vbus_ext);
819 ldm_intr((PVBUS)vbus_ext->vbus);
822 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg)
824 KdPrint(("hpt_async"));
827 static int hpt_shutdown(device_t dev)
829 KdPrint(("hpt_shutdown(dev=%p)", dev));
833 static int hpt_detach(device_t dev)
835 /* we don't allow the driver to be unloaded. */
839 static void hpt_ioctl_done(struct _IOCTL_ARG *arg)
845 static void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args)
847 ioctl_args->result = -1;
848 ioctl_args->done = hpt_ioctl_done;
849 ioctl_args->ioctl_cmnd = (void *)1;
851 hpt_lock_vbus(vbus_ext);
852 ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args);
854 while (ioctl_args->ioctl_cmnd) {
855 if (hpt_sleep(vbus_ext, ioctl_args, PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
857 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
858 __hpt_do_tasks(vbus_ext);
861 /* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */
863 hpt_unlock_vbus(vbus_ext);
866 static void hpt_do_ioctl(IOCTL_ARG *ioctl_args)
871 ldm_for_each_vbus(vbus, vbus_ext) {
872 __hpt_do_ioctl(vbus_ext, ioctl_args);
873 if (ioctl_args->result!=HPT_IOCTL_RESULT_WRONG_VBUS)
878 #define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\
880 arg.dwIoControlCode = code;\
881 arg.lpInBuffer = inbuf;\
882 arg.lpOutBuffer = outbuf;\
883 arg.nInBufferSize = insize;\
884 arg.nOutBufferSize = outsize;\
885 arg.lpBytesReturned = 0;\
890 #define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff))
892 static int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount)
895 HPT_U32 count = nMaxCount-1;
897 if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES,
898 &count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount))
901 nMaxCount = (int)pIds[0];
902 for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1];
906 static int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo)
908 return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3,
909 &id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3));
912 /* not belong to this file logically, but we want to use ioctl interface */
913 static int __hpt_stop_tasks(PVBUS_EXT vbus_ext, DEVICEID id)
915 LOGICAL_DEVICE_INFO_V3 devinfo;
917 DEVICEID param[2] = { id, 0 };
919 if (hpt_get_device_info_v3(id, &devinfo))
922 if (devinfo.Type!=LDT_ARRAY)
925 if (devinfo.u.array.Flags & ARRAY_FLAG_REBUILDING)
926 param[1] = AS_REBUILD_ABORT;
927 else if (devinfo.u.array.Flags & ARRAY_FLAG_VERIFYING)
928 param[1] = AS_VERIFY_ABORT;
929 else if (devinfo.u.array.Flags & ARRAY_FLAG_INITIALIZING)
930 param[1] = AS_INITIALIZE_ABORT;
931 else if (devinfo.u.array.Flags & ARRAY_FLAG_TRANSFORMING)
932 param[1] = AS_TRANSFORM_ABORT;
936 KdPrint(("SET_ARRAY_STATE(%x, %d)", param[0], param[1]));
937 result = HPT_DO_IOCTL(HPT_IOCTL_SET_ARRAY_STATE,
938 param, sizeof(param), 0, 0);
940 for (i=0; i<devinfo.u.array.nDisk; i++)
941 if (DEVICEID_VALID(devinfo.u.array.Members[i]))
942 __hpt_stop_tasks(vbus_ext, devinfo.u.array.Members[i]);
947 static void hpt_stop_tasks(PVBUS_EXT vbus_ext)
952 count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0]));
954 for (i=0; i<count; i++)
955 __hpt_stop_tasks(vbus_ext, ids[i]);
958 static d_open_t hpt_open;
959 static d_close_t hpt_close;
960 static d_ioctl_t hpt_ioctl;
961 static int hpt_rescan_bus(void);
963 static struct cdevsw hpt_cdevsw = {
965 .d_close = hpt_close,
966 .d_ioctl = hpt_ioctl,
967 .d_name = driver_name,
968 .d_version = D_VERSION,
971 static struct intr_config_hook hpt_ich;
974 * hpt_final_init will be called after all hpt_attach.
976 static void hpt_final_init(void *dummy)
983 /* Clear the config hook */
984 config_intrhook_disestablish(&hpt_ich);
986 /* allocate memory */
988 ldm_for_each_vbus(vbus, vbus_ext) {
989 if (hpt_alloc_mem(vbus_ext)) {
990 os_printk("out of memory");
998 os_printk("no controller detected.");
1002 /* initializing hardware */
1003 ldm_for_each_vbus(vbus, vbus_ext) {
1004 /* make timer available here */
1005 mtx_init(&vbus_ext->lock, "hptsleeplock", NULL, MTX_DEF);
1006 callout_init_mtx(&vbus_ext->timer, &vbus_ext->lock, 0);
1007 if (hpt_init_vbus(vbus_ext)) {
1008 os_printk("fail to initialize hardware");
1013 /* register CAM interface */
1014 ldm_for_each_vbus(vbus, vbus_ext) {
1015 struct cam_devq *devq;
1016 struct ccb_setasync ccb;
1018 if (bus_dma_tag_create(NULL,/* parent */
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 busdma_lock_mutex, /* lockfunc */
1029 &vbus_ext->lock, /* lockfuncarg */
1030 &vbus_ext->io_dmat /* tag */))
1035 for (i=0; i<os_max_queue_comm; i++) {
1036 POS_CMDEXT ext = (POS_CMDEXT)malloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK);
1038 os_printk("Can't alloc cmdext(%d)", i);
1041 ext->vbus_ext = vbus_ext;
1042 ext->next = vbus_ext->cmdext_list;
1043 vbus_ext->cmdext_list = ext;
1045 if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) {
1046 os_printk("Can't create dma map(%d)", i);
1049 callout_init_mtx(&ext->timeout, &vbus_ext->lock, 0);
1052 if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) {
1053 os_printk("cam_simq_alloc failed");
1057 vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1058 vbus_ext, 0, &vbus_ext->lock, os_max_queue_comm,
1061 if (!vbus_ext->sim) {
1062 os_printk("cam_sim_alloc failed");
1063 cam_simq_free(devq);
1067 hpt_lock_vbus(vbus_ext);
1068 if (xpt_bus_register(vbus_ext->sim, NULL, 0) != CAM_SUCCESS) {
1069 os_printk("xpt_bus_register failed");
1070 cam_sim_free(vbus_ext->sim, /*free devq*/ TRUE);
1071 hpt_unlock_vbus(vbus_ext);
1072 vbus_ext->sim = NULL;
1076 if (xpt_create_path(&vbus_ext->path, /*periph */ NULL,
1077 cam_sim_path(vbus_ext->sim), CAM_TARGET_WILDCARD,
1078 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1080 os_printk("xpt_create_path failed");
1081 xpt_bus_deregister(cam_sim_path(vbus_ext->sim));
1082 cam_sim_free(vbus_ext->sim, /*free_devq*/TRUE);
1083 hpt_unlock_vbus(vbus_ext);
1084 vbus_ext->sim = NULL;
1087 hpt_unlock_vbus(vbus_ext);
1089 xpt_setup_ccb(&ccb.ccb_h, vbus_ext->path, /*priority*/5);
1090 ccb.ccb_h.func_code = XPT_SASYNC_CB;
1091 ccb.event_enable = AC_LOST_DEVICE;
1092 ccb.callback = hpt_async;
1093 ccb.callback_arg = vbus_ext;
1094 xpt_action((union ccb *)&ccb);
1096 for (hba = vbus_ext->hba_list; hba; hba = hba->next) {
1098 if ((hba->irq_res = bus_alloc_resource_any(hba->pcidev,
1099 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1101 os_printk("can't allocate interrupt");
1105 if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM | INTR_MPSAFE,
1106 NULL, hpt_pci_intr, vbus_ext, &hba->irq_handle))
1108 os_printk("can't set up interrupt");
1111 hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE);
1114 vbus_ext->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1115 hpt_shutdown_vbus, vbus_ext, SHUTDOWN_PRI_DEFAULT);
1116 if (!vbus_ext->shutdown_eh)
1117 os_printk("Shutdown event registration failed");
1120 ldm_for_each_vbus(vbus, vbus_ext) {
1121 TASK_INIT(&vbus_ext->worker, 0, (task_fn_t *)hpt_do_tasks, vbus_ext);
1122 if (vbus_ext->tasks)
1123 TASK_ENQUEUE(&vbus_ext->worker);
1126 make_dev(&hpt_cdevsw, DRIVER_MINOR, UID_ROOT, GID_OPERATOR,
1127 S_IRUSR | S_IWUSR, "%s", driver_name);
1130 #if defined(KLD_MODULE)
1132 typedef struct driverlink *driverlink_t;
1134 kobj_class_t driver;
1135 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
1138 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
1141 TAILQ_ENTRY(devclass) link;
1142 devclass_t parent; /* parent in devclass hierarchy */
1143 driver_list_t drivers; /* bus devclasses store drivers for bus */
1145 device_t *devices; /* array of devices indexed by unit */
1146 int maxunit; /* size of devices array */
1149 static void override_kernel_driver(void)
1151 driverlink_t dl, dlfirst;
1152 driver_t *tmpdriver;
1153 devclass_t dc = devclass_find("pci");
1156 dlfirst = TAILQ_FIRST(&dc->drivers);
1157 for (dl = dlfirst; dl; dl = TAILQ_NEXT(dl, link)) {
1158 if(strcmp(dl->driver->name, driver_name) == 0) {
1159 tmpdriver=dl->driver;
1160 dl->driver=dlfirst->driver;
1161 dlfirst->driver=tmpdriver;
1169 #define override_kernel_driver()
1172 static void hpt_init(void *dummy)
1175 os_printk("%s %s", driver_name_long, driver_ver);
1177 override_kernel_driver();
1180 hpt_ich.ich_func = hpt_final_init;
1181 hpt_ich.ich_arg = NULL;
1182 if (config_intrhook_establish(&hpt_ich) != 0) {
1183 printf("%s: cannot establish configuration hook\n",
1188 SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
1191 * CAM driver interface
1193 static device_method_t driver_methods[] = {
1194 /* Device interface */
1195 DEVMETHOD(device_probe, hpt_probe),
1196 DEVMETHOD(device_attach, hpt_attach),
1197 DEVMETHOD(device_detach, hpt_detach),
1198 DEVMETHOD(device_shutdown, hpt_shutdown),
1202 static driver_t hpt_pci_driver = {
1208 static devclass_t hpt_devclass;
1211 #error "no TARGETNAME found"
1214 /* use this to make TARGETNAME be expanded */
1215 #define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
1216 #define __MODULE_VERSION(p1, p2) MODULE_VERSION(p1, p2)
1217 #define __MODULE_DEPEND(p1, p2, p3, p4, p5) MODULE_DEPEND(p1, p2, p3, p4, p5)
1218 __DRIVER_MODULE(TARGETNAME, pci, hpt_pci_driver, hpt_devclass, 0, 0);
1219 __MODULE_VERSION(TARGETNAME, 1);
1220 __MODULE_DEPEND(TARGETNAME, cam, 1, 1, 1);
1222 static int hpt_open(struct cdev *dev, int flags, int devtype, struct thread *td)
1227 static int hpt_close(struct cdev *dev, int flags, int devtype, struct thread *td)
1232 static int hpt_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1234 PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data;
1235 IOCTL_ARG ioctl_args;
1236 HPT_U32 bytesReturned = 0;
1239 case HPT_DO_IOCONTROL:
1241 if (piop->Magic == HPT_IOCTL_MAGIC || piop->Magic == HPT_IOCTL_MAGIC32) {
1242 KdPrint(("ioctl=%x in=%p len=%d out=%p len=%d\n",
1243 piop->dwIoControlCode,
1245 piop->nInBufferSize,
1247 piop->nOutBufferSize));
1249 memset(&ioctl_args, 0, sizeof(ioctl_args));
1251 ioctl_args.dwIoControlCode = piop->dwIoControlCode;
1252 ioctl_args.nInBufferSize = piop->nInBufferSize;
1253 ioctl_args.nOutBufferSize = piop->nOutBufferSize;
1254 ioctl_args.lpBytesReturned = &bytesReturned;
1256 if (ioctl_args.nInBufferSize) {
1257 ioctl_args.lpInBuffer = malloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK);
1258 if (!ioctl_args.lpInBuffer)
1260 if (copyin((void*)piop->lpInBuffer,
1261 ioctl_args.lpInBuffer, piop->nInBufferSize))
1265 if (ioctl_args.nOutBufferSize) {
1266 ioctl_args.lpOutBuffer = malloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK | M_ZERO);
1267 if (!ioctl_args.lpOutBuffer)
1271 hpt_do_ioctl(&ioctl_args);
1273 if (ioctl_args.result==HPT_IOCTL_RESULT_OK) {
1274 if (piop->nOutBufferSize) {
1275 if (copyout(ioctl_args.lpOutBuffer,
1276 (void*)piop->lpOutBuffer, piop->nOutBufferSize))
1279 if (piop->lpBytesReturned) {
1280 if (copyout(&bytesReturned,
1281 (void*)piop->lpBytesReturned, sizeof(HPT_U32)))
1284 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1285 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1289 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1290 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1298 return hpt_rescan_bus();
1301 KdPrint(("invalid command!"));
1307 static int hpt_rescan_bus(void)
1313 ldm_for_each_vbus(vbus, vbus_ext) {
1314 if ((ccb = xpt_alloc_ccb()) == NULL)
1316 if (xpt_create_path(&ccb->ccb_h.path, NULL,
1317 cam_sim_path(vbus_ext->sim),
1318 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {