The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/hptrr/hptrr_osm_bsd.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) HighPoint Technologies, Inc.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include <dev/hptrr/hptrr_config.h>
   33 /* $Id: osm_bsd.c,v 1.27 2007/11/22 07:35:49 gmm Exp $
   34  *
   35  * HighPoint RAID Driver for FreeBSD
   36  * Copyright (C) 2005 HighPoint Technologies, Inc. All Rights Reserved.
   37  */
   38 #include <dev/hptrr/os_bsd.h>
   39 #include <dev/hptrr/hptintf.h>
   40 
   41 static int attach_generic = 0;
   42 TUNABLE_INT("hw.hptrr.attach_generic", &attach_generic);
   43 
   44 static HIM *hpt_match(device_t dev)
   45 {
   46         PCI_ID pci_id;
   47         int i;
   48         HIM *him;
   49 
   50         /* Some of supported chips are used not only by HPT. */
   51         if (pci_get_vendor(dev) != 0x1103 && !attach_generic)
   52                 return (NULL);
   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)){
   57                                 return (him);
   58                         }
   59                 }
   60         }
   61         return (NULL);
   62 }
   63 
   64 static int hpt_probe(device_t dev)
   65 {
   66         HIM *him;
   67 
   68         him = hpt_match(dev);
   69         if (him != NULL) {
   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)
   72                             ));
   73                 device_set_desc(dev, him->name);
   74                 return (BUS_PROBE_DEFAULT);
   75         }
   76 
   77         return (ENXIO);
   78 }
   79 
   80 static int hpt_attach(device_t dev)
   81 {
   82         PHBA hba = (PHBA)device_get_softc(dev);
   83         HIM *him;
   84         PCI_ID pci_id;
   85         HPT_UINT size;
   86         PVBUS vbus;
   87         PVBUS_EXT vbus_ext;
   88         
   89         KdPrint(("hpt_attach(%d/%d/%d)", pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)));
   90 
   91         him = hpt_match(dev);
   92         hba->ext_type = EXT_TYPE_HBA;
   93         hba->ldm_adapter.him = him;
   94 
   95         pci_enable_busmaster(dev);
   96 
   97         pci_id.vid = pci_get_vendor(dev);
   98         pci_id.did = pci_get_device(dev);
   99         pci_id.rev = pci_get_revid(dev);
  100 
  101         size = him->get_adapter_size(&pci_id);
  102         hba->ldm_adapter.him_handle = malloc(size, M_DEVBUF, M_WAITOK);
  103 
  104         hba->pcidev = dev;
  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);
  109 
  110         if (!him->create_adapter(&pci_id, hba->pciaddr, hba->ldm_adapter.him_handle, hba)) {
  111                 free(hba->ldm_adapter.him_handle, M_DEVBUF);
  112                 return ENXIO;
  113         }
  114 
  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));
  117 
  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 |
  121                     M_ZERO);
  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);
  125         }
  126 
  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;
  132                         break;
  133                 }
  134         }       
  135         return 0;
  136 }
  137 
  138 /*
  139  * Maybe we'd better to use the bus_dmamem_alloc to alloc DMA memory,
  140  * but there are some problems currently (alignment, etc).
  141  */
  142 static __inline void *__get_free_pages(int order)
  143 {
  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);
  147 }
  148 
  149 static __inline void free_pages(void *p, int order)
  150 {
  151         contigfree(p, PAGE_SIZE<<order, M_DEVBUF);
  152 }
  153 
  154 static int hpt_alloc_mem(PVBUS_EXT vbus_ext)
  155 {
  156         PHBA hba;
  157         struct freelist *f;
  158         HPT_UINT i;
  159         void **p;
  160 
  161         for (hba = vbus_ext->hba_list; hba; hba = hba->next)
  162                 hba->ldm_adapter.him->get_meminfo(hba->ldm_adapter.him_handle);
  163 
  164         ldm_get_mem_info((PVBUS)vbus_ext->vbus, 0);
  165 
  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);
  172                         *p = f->head;
  173                         f->head = p;
  174                 }
  175         }
  176 
  177         for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
  178                 int order, size, j;
  179 
  180                 HPT_ASSERT((f->size & (f->alignment-1))==0);
  181 
  182                 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1)
  183                         ;
  184 
  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);
  188 
  189                 for (i=0; i<f->count;) {
  190                         p = (void **)__get_free_pages(order);
  191                         if (!p) return -1;
  192                         for (j = size/f->size; j && i<f->count; i++,j--) {
  193                                 *p = f->head;
  194                                 *(BUS_ADDRESS *)(p+1) = (BUS_ADDRESS)vtophys(p);
  195                                 f->head = p;
  196                                 p = (void **)((unsigned long)p + f->size);
  197                         }
  198                 }
  199         }
  200         
  201         HPT_ASSERT(PAGE_SIZE==DMAPOOL_PAGE_SIZE);
  202 
  203         for (i=0; i<os_max_cache_pages; i++) {
  204                 p = (void **)__get_free_pages(0);
  205                 if (!p) return -1;
  206                 HPT_ASSERT(((HPT_UPTR)p & (DMAPOOL_PAGE_SIZE-1))==0);
  207                 dmapool_put_page((PVBUS)vbus_ext->vbus, p, (BUS_ADDRESS)vtophys(p));
  208         }
  209 
  210         return 0;
  211 }
  212 
  213 static void hpt_free_mem(PVBUS_EXT vbus_ext)
  214 {
  215         struct freelist *f;
  216         void *p;
  217         int i;
  218         BUS_ADDRESS bus;
  219 
  220         for (f=vbus_ext->freelist_head; f; f=f->next) {
  221 #if DBG
  222                 if (f->count!=f->reserved_count) {
  223                         KdPrint(("memory leak for freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
  224                 }
  225 #endif
  226                 while ((p=freelist_get(f)))
  227                         free(p, M_DEVBUF);
  228         }
  229 
  230         for (i=0; i<os_max_cache_pages; i++) {
  231                 p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus);
  232                 HPT_ASSERT(p);
  233                 free_pages(p, 0);
  234         }
  235 
  236         for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
  237                 int order, size;
  238 #if DBG
  239                 if (f->count!=f->reserved_count) {
  240                         KdPrint(("memory leak for dma freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
  241                 }
  242 #endif
  243                 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ;
  244 
  245                 while ((p=freelist_get_dma(f, &bus))) {
  246                         if (order)
  247                                 free_pages(p, order);
  248                         else {
  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);
  252                         }
  253                 }
  254         }
  255         
  256         while ((p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus)))
  257                 free_pages(p, 0);
  258 }
  259 
  260 static int hpt_init_vbus(PVBUS_EXT vbus_ext)
  261 {
  262         PHBA hba;
  263 
  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));
  267                         return -1;
  268                 }
  269 
  270         ldm_initialize_vbus((PVBUS)vbus_ext->vbus, &vbus_ext->hba_list->ldm_adapter);
  271         return 0;
  272 }
  273 
  274 static void hpt_flush_done(PCOMMAND pCmd)
  275 {
  276         PVDEV vd = pCmd->target;
  277 
  278         if (mIsArray(vd->type) && vd->u.array.transform && vd!=vd->u.array.transform->target) {
  279                 vd = vd->u.array.transform->target;
  280                 HPT_ASSERT(vd);
  281                 pCmd->target = vd;
  282                 pCmd->Result = RETURN_PENDING;
  283                 vdev_queue_cmd(pCmd);
  284                 return;
  285         }
  286 
  287         *(int *)pCmd->priv = 1;
  288         wakeup(pCmd);
  289 }
  290 
  291 /*
  292  * flush a vdev (without retry).
  293  */
  294 static int hpt_flush_vdev(PVBUS_EXT vbus_ext, PVDEV vd)
  295 {
  296         PCOMMAND pCmd;
  297         int result = 0, done;
  298         HPT_UINT count;
  299 
  300         KdPrint(("flusing dev %p", vd));
  301 
  302         hpt_assert_vbus_locked(vbus_ext);
  303 
  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);
  307         else
  308                 count = vd->cmds_per_request;
  309 
  310         pCmd = ldm_alloc_cmds(vd->vbus, count);
  311 
  312         if (!pCmd) {
  313                 return -1;
  314         }
  315 
  316         pCmd->type = CMD_TYPE_FLUSH;
  317         pCmd->flags.hard_flush = 1;
  318         pCmd->target = vd;
  319         pCmd->done = hpt_flush_done;
  320         done = 0;
  321         pCmd->priv = &done;
  322 
  323         ldm_queue_cmd(pCmd);
  324         
  325         if (!done) {
  326                 while (hpt_sleep(vbus_ext, pCmd, PPAUSE, "hptfls", HPT_OSM_TIMEOUT)) {
  327                         ldm_reset_vbus(vd->vbus);
  328                 }
  329         }
  330 
  331         KdPrint(("flush result %d", pCmd->Result));
  332 
  333         if (pCmd->Result!=RETURN_SUCCESS)
  334                 result = -1;
  335 
  336         ldm_free_cmds(pCmd);
  337 
  338         return result;
  339 }
  340 
  341 static void hpt_stop_tasks(PVBUS_EXT vbus_ext);
  342 static void hpt_shutdown_vbus(PVBUS_EXT vbus_ext, int howto)
  343 {
  344         PVBUS     vbus = (PVBUS)vbus_ext->vbus;
  345         PHBA hba;
  346         int i;
  347         
  348         KdPrint(("hpt_shutdown_vbus"));
  349 
  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;
  354 
  355         /* flush devices */
  356         for (i=0; i<osm_max_targets; i++) {
  357                 PVDEV vd = ldm_find_target(vbus, i);
  358                 if (vd) {
  359                         /* retry once */
  360                         if (hpt_flush_vdev(vbus_ext, vd))
  361                                 hpt_flush_vdev(vbus_ext, vd);
  362                 }
  363         }
  364 
  365         ldm_shutdown(vbus);
  366         hpt_unlock_vbus(vbus_ext);
  367 
  368         ldm_release_vbus(vbus);
  369 
  370         for (hba=vbus_ext->hba_list; hba; hba=hba->next)
  371                 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
  372 
  373         hpt_free_mem(vbus_ext);
  374 
  375         while ((hba=vbus_ext->hba_list)) {
  376                 vbus_ext->hba_list = hba->next;
  377                 free(hba->ldm_adapter.him_handle, M_DEVBUF);
  378         }
  379 
  380         callout_drain(&vbus_ext->timer);
  381         mtx_destroy(&vbus_ext->lock);
  382         free(vbus_ext, M_DEVBUF);
  383         KdPrint(("hpt_shutdown_vbus done"));
  384 }
  385 
  386 static void __hpt_do_tasks(PVBUS_EXT vbus_ext)
  387 {
  388         OSM_TASK *tasks;
  389 
  390         tasks = vbus_ext->tasks;
  391         vbus_ext->tasks = 0;
  392 
  393         while (tasks) {
  394                 OSM_TASK *t = tasks;
  395                 tasks = t->next;
  396                 t->next = 0;
  397                 t->func(vbus_ext->vbus, t->data);
  398         }
  399 }
  400 
  401 static void hpt_do_tasks(PVBUS_EXT vbus_ext, int pending)
  402 {
  403         if(vbus_ext){
  404                 hpt_lock_vbus(vbus_ext);
  405                 __hpt_do_tasks(vbus_ext);
  406                 hpt_unlock_vbus(vbus_ext);
  407         }
  408 }
  409 
  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);
  414 
  415 static __inline POS_CMDEXT cmdext_get(PVBUS_EXT vbus_ext)
  416 {
  417         POS_CMDEXT p = vbus_ext->cmdext_list;
  418         if (p)
  419                 vbus_ext->cmdext_list = p->next;
  420         return p;
  421 }
  422 
  423 static __inline void cmdext_put(POS_CMDEXT p)
  424 {
  425         p->next = p->vbus_ext->cmdext_list;
  426         p->vbus_ext->cmdext_list = p;
  427 }
  428 
  429 static void hpt_timeout(void *arg)
  430 {
  431         PCOMMAND pCmd = (PCOMMAND)arg;
  432         POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
  433         
  434         KdPrint(("pCmd %p timeout", pCmd));
  435         
  436         ldm_reset_vbus((PVBUS)ext->vbus_ext->vbus);
  437 }
  438 
  439 static void os_cmddone(PCOMMAND pCmd)
  440 {
  441         POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
  442         union ccb *ccb = ext->ccb;
  443 
  444         KdPrint(("os_cmddone(%p, %d)", pCmd, pCmd->Result));
  445 
  446         callout_stop(&ext->timeout);
  447 
  448         switch(pCmd->Result) {
  449         case RETURN_SUCCESS:
  450                 ccb->ccb_h.status = CAM_REQ_CMP;
  451                 break;
  452         case RETURN_BAD_DEVICE:
  453                 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
  454                 break;
  455         case RETURN_DEVICE_BUSY:
  456                 ccb->ccb_h.status = CAM_BUSY;
  457                 break;
  458         case RETURN_INVALID_REQUEST:
  459                 ccb->ccb_h.status = CAM_REQ_INVALID;
  460                 break;
  461         case RETURN_SELECTION_TIMEOUT:
  462                 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
  463                 break;
  464         case RETURN_RETRY:
  465                 ccb->ccb_h.status = CAM_BUSY;
  466                 break;
  467         default:
  468                 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
  469                 break;
  470         }
  471 
  472         if (pCmd->flags.data_in) {
  473                 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTREAD);
  474         }
  475         else if (pCmd->flags.data_out) {
  476                 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTWRITE);
  477         }
  478         
  479         bus_dmamap_unload(ext->vbus_ext->io_dmat, ext->dma_map);
  480 
  481         cmdext_put(ext);
  482         ldm_free_cmds(pCmd);
  483         xpt_done(ccb);
  484 }
  485 
  486 static int os_buildsgl(PCOMMAND pCmd, PSG pSg, int logical)
  487 {
  488         POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
  489         union ccb *ccb = ext->ccb;
  490 
  491         if (logical) {
  492                 os_set_sgptr(pSg, (HPT_U8 *)ccb->csio.data_ptr);
  493                 pSg->size = ccb->csio.dxfer_len;
  494                 pSg->eot = 1;
  495                 return TRUE;
  496         }
  497 
  498         /* since we have provided physical sg, nobody will ask us to build physical sg */
  499         HPT_ASSERT(0);
  500         return FALSE;
  501 }
  502 
  503 static void hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
  504 {
  505         PCOMMAND pCmd = (PCOMMAND)arg;
  506         POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
  507         PSG psg = pCmd->psg;
  508         int idx;
  509         
  510         HPT_ASSERT(pCmd->flags.physical_sg);
  511         
  512         if (error)
  513                 panic("busdma error");
  514                 
  515         HPT_ASSERT(nsegs<=os_max_sg_descriptors);
  516 
  517         if (nsegs != 0) {
  518                 for (idx = 0; idx < nsegs; idx++, psg++) {
  519                         psg->addr.bus = segs[idx].ds_addr;
  520                         psg->size = segs[idx].ds_len;
  521                         psg->eot = 0;
  522                 }
  523                         psg[-1].eot = 1;
  524                 
  525                 if (pCmd->flags.data_in) {
  526                         bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
  527                             BUS_DMASYNC_PREREAD);
  528                 }
  529                 else if (pCmd->flags.data_out) {
  530                         bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
  531                             BUS_DMASYNC_PREWRITE);
  532                 }
  533         }
  534         callout_reset(&ext->timeout, HPT_OSM_TIMEOUT, hpt_timeout, pCmd);
  535         ldm_queue_cmd(pCmd);
  536 }
  537 
  538 static void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb)
  539 {
  540         PVBUS vbus = (PVBUS)vbus_ext->vbus;
  541         PVDEV vd;
  542         PCOMMAND pCmd;
  543         POS_CMDEXT ext;
  544         HPT_U8 *cdb;
  545 
  546         if (ccb->ccb_h.flags & CAM_CDB_POINTER)
  547                 cdb = ccb->csio.cdb_io.cdb_ptr;
  548         else
  549                 cdb = ccb->csio.cdb_io.cdb_bytes;
  550         
  551         KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x",
  552                 ccb,
  553                 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
  554                 *(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8]
  555         ));
  556 
  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))
  561         {
  562                 ccb->ccb_h.status = CAM_TID_INVALID;
  563                 xpt_done(ccb);
  564                 return;
  565         }
  566 
  567         vd = ldm_find_target(vbus, ccb->ccb_h.target_id);
  568 
  569         if (!vd) {
  570                 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
  571                 xpt_done(ccb);
  572                 return;
  573         }
  574    
  575         switch (cdb[0]) {
  576         case TEST_UNIT_READY:
  577         case START_STOP_UNIT:
  578         case SYNCHRONIZE_CACHE:
  579                 ccb->ccb_h.status = CAM_REQ_CMP;
  580                 break;
  581 
  582         case INQUIRY:
  583                 {
  584                         PINQUIRYDATA inquiryData;
  585                         memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len);
  586                         inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr;
  587                 
  588                         inquiryData->AdditionalLength = 31;
  589                         inquiryData->CommandQueue = 1;
  590                         memcpy(&inquiryData->VendorId, "HPT     ", 8);
  591                         memcpy(&inquiryData->ProductId, "DISK 0_0        ", 16);
  592         
  593                         if (vd->target_id / 10) {
  594                                 inquiryData->ProductId[7] = (vd->target_id % 100) / 10 + '';
  595                                 inquiryData->ProductId[8] = (vd->target_id % 100) % 10 + '';
  596                         }
  597                         else
  598                                 inquiryData->ProductId[7] = (vd->target_id % 100) % 10 + '';
  599         
  600                         memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4);
  601         
  602                         ccb->ccb_h.status = CAM_REQ_CMP;
  603                 }
  604                 break;
  605 
  606         case READ_CAPACITY:
  607         {
  608                 HPT_U8 *rbuf = ccb->csio.data_ptr;
  609                 HPT_U32 cap;
  610                 
  611                 if (vd->capacity>0xfffffffful)
  612                         cap = 0xfffffffful;
  613                 else
  614                         cap = vd->capacity - 1;
  615         
  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;
  620                 rbuf[4] = 0;
  621                 rbuf[5] = 0;
  622                 rbuf[6] = 2;
  623                 rbuf[7] = 0;
  624 
  625                 ccb->ccb_h.status = CAM_REQ_CMP;
  626                 break;
  627         }
  628         
  629         case SERVICE_ACTION_IN: 
  630         {
  631                 HPT_U8 *rbuf = ccb->csio.data_ptr;
  632                 HPT_U64 cap = vd->capacity - 1;
  633                 
  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;
  642                 rbuf[8] = 0;
  643                 rbuf[9] = 0;
  644                 rbuf[10] = 2;
  645                 rbuf[11] = 0;
  646                 
  647                 ccb->ccb_h.status = CAM_REQ_CMP;
  648                 break;  
  649         }
  650         
  651         case READ_6:
  652         case READ_10:
  653         case READ_16:
  654         case WRITE_6:
  655         case WRITE_10:
  656         case WRITE_16:
  657         case 0x13:
  658         case 0x2f:
  659         {
  660                 int error;
  661 
  662                 pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
  663                 if(!pCmd){
  664                         KdPrint(("Failed to allocate command!"));
  665                         ccb->ccb_h.status = CAM_BUSY;
  666                         break;
  667                 }
  668 
  669                 switch (cdb[0]) {
  670                 case READ_6:
  671                 case WRITE_6:
  672                 case 0x13:
  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];
  675                         break;
  676                 case READ_16:
  677                 case WRITE_16: 
  678                 {
  679                         HPT_U64 block =
  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) |
  687                                 ((HPT_U64)cdb[9]);
  688                         pCmd->uCmd.Ide.Lba = block;
  689                         pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8);
  690                         break;
  691                 }
  692                 
  693                 default:
  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);
  696                         break;
  697                 }
  698                 
  699                 switch (cdb[0]) {
  700                 case READ_6:
  701                 case READ_10:
  702                 case READ_16:
  703                         pCmd->flags.data_in = 1;
  704                         break;
  705                 case WRITE_6:
  706                 case WRITE_10:
  707                 case WRITE_16:
  708                         pCmd->flags.data_out = 1;
  709                         break;
  710                 }
  711                 pCmd->priv = ext = cmdext_get(vbus_ext);
  712                 HPT_ASSERT(ext);
  713                 ext->ccb = ccb;
  714                 pCmd->target = vd;
  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, 
  720                                         ext->dma_map, 
  721                                         ccb,
  722                                         hpt_io_dmamap_callback, pCmd,
  723                                         BUS_DMA_WAITOK
  724                                         );
  725                 KdPrint(("bus_dmamap_load return %d", error));
  726                 if (error && error!=EINPROGRESS) {
  727                         os_printk("bus_dmamap_load error %d", error);
  728                         cmdext_put(ext);
  729                         ldm_free_cmds(pCmd);
  730                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
  731                         xpt_done(ccb);
  732                 }
  733                 return;
  734         }
  735 
  736         default:
  737                 ccb->ccb_h.status = CAM_REQ_INVALID;
  738                 break;
  739         }
  740 
  741         xpt_done(ccb);
  742         return;
  743 }
  744 
  745 static void hpt_action(struct cam_sim *sim, union ccb *ccb)
  746 {
  747         PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
  748 
  749         KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id));
  750 
  751         hpt_assert_vbus_locked(vbus_ext);
  752         switch (ccb->ccb_h.func_code) {
  753         
  754         case XPT_SCSI_IO:
  755                 hpt_scsi_io(vbus_ext, ccb);
  756                 return;
  757 
  758         case XPT_RESET_BUS:
  759                 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
  760                 break;
  761 
  762         case XPT_GET_TRAN_SETTINGS:
  763         case XPT_SET_TRAN_SETTINGS:
  764                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
  765                 break;
  766 
  767         case XPT_CALC_GEOMETRY:
  768                 cam_calc_geometry(&ccb->ccg, 1);
  769                 break;
  770 
  771         case XPT_PATH_INQ:
  772         {
  773                 struct ccb_pathinq *cpi = &ccb->cpi;
  774 
  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;
  781                 cpi->max_lun = 0;
  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;
  786 
  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;
  795                 break;
  796         }
  797 
  798         default:
  799                 ccb->ccb_h.status = CAM_REQ_INVALID;
  800                 break;
  801         }
  802 
  803         xpt_done(ccb);
  804         return;
  805 }
  806 
  807 static void hpt_pci_intr(void *arg)
  808 {       
  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);
  813 }
  814 
  815 static void hpt_poll(struct cam_sim *sim)
  816 {
  817         PVBUS_EXT vbus_ext = cam_sim_softc(sim);
  818         hpt_assert_vbus_locked(vbus_ext);
  819         ldm_intr((PVBUS)vbus_ext->vbus);
  820 }
  821 
  822 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg)
  823 {
  824         KdPrint(("hpt_async"));
  825 }
  826 
  827 static int hpt_shutdown(device_t dev)
  828 {
  829         KdPrint(("hpt_shutdown(dev=%p)", dev));
  830         return 0;
  831 }
  832 
  833 static int hpt_detach(device_t dev)
  834 {
  835         /* we don't allow the driver to be unloaded. */
  836         return EBUSY;
  837 }
  838 
  839 static void hpt_ioctl_done(struct _IOCTL_ARG *arg)
  840 {
  841         arg->ioctl_cmnd = 0;
  842         wakeup(arg);
  843 }
  844 
  845 static void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args)
  846 {
  847         ioctl_args->result = -1;
  848         ioctl_args->done = hpt_ioctl_done;
  849         ioctl_args->ioctl_cmnd = (void *)1;
  850 
  851         hpt_lock_vbus(vbus_ext);
  852         ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args);
  853 
  854         while (ioctl_args->ioctl_cmnd) {
  855                 if (hpt_sleep(vbus_ext, ioctl_args, PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
  856                         break;
  857                 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
  858                 __hpt_do_tasks(vbus_ext);
  859         }
  860 
  861         /* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */
  862 
  863         hpt_unlock_vbus(vbus_ext);
  864 }
  865 
  866 static void hpt_do_ioctl(IOCTL_ARG *ioctl_args)
  867 {
  868         PVBUS vbus;
  869         PVBUS_EXT vbus_ext;
  870         
  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)
  874                         return;
  875         }
  876 }
  877 
  878 #define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\
  879         IOCTL_ARG arg;\
  880         arg.dwIoControlCode = code;\
  881         arg.lpInBuffer = inbuf;\
  882         arg.lpOutBuffer = outbuf;\
  883         arg.nInBufferSize = insize;\
  884         arg.nOutBufferSize = outsize;\
  885         arg.lpBytesReturned = 0;\
  886         hpt_do_ioctl(&arg);\
  887         arg.result;\
  888 })
  889 
  890 #define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff))
  891 
  892 static int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount)
  893 {
  894         int i;
  895         HPT_U32 count = nMaxCount-1;
  896         
  897         if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES,
  898                         &count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount))
  899                 return -1;
  900 
  901         nMaxCount = (int)pIds[0];
  902         for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1];
  903         return nMaxCount;
  904 }
  905 
  906 static int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo)
  907 {
  908         return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3,
  909                                 &id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3));
  910 }
  911 
  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)
  914 {
  915         LOGICAL_DEVICE_INFO_V3 devinfo;
  916         int i, result;
  917         DEVICEID param[2] = { id, 0 };
  918         
  919         if (hpt_get_device_info_v3(id, &devinfo))
  920                 return -1;
  921                 
  922         if (devinfo.Type!=LDT_ARRAY)
  923                 return -1;
  924                 
  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;
  933         else
  934                 return -1;
  935 
  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);
  939                                 
  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]);
  943                         
  944         return result;
  945 }
  946 
  947 static void hpt_stop_tasks(PVBUS_EXT vbus_ext)
  948 {
  949         DEVICEID ids[32];
  950         int i, count;
  951 
  952         count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0]));
  953         
  954         for (i=0; i<count; i++)
  955                 __hpt_stop_tasks(vbus_ext, ids[i]);
  956 }
  957 
  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);
  962 
  963 static struct cdevsw hpt_cdevsw = {
  964         .d_open =       hpt_open,
  965         .d_close =      hpt_close,
  966         .d_ioctl =      hpt_ioctl,
  967         .d_name =       driver_name,
  968         .d_version =    D_VERSION,
  969 };
  970 
  971 static struct intr_config_hook hpt_ich;
  972 
  973 /*
  974  * hpt_final_init will be called after all hpt_attach.
  975  */
  976 static void hpt_final_init(void *dummy)
  977 {
  978         int       i;
  979         PVBUS_EXT vbus_ext;
  980         PVBUS vbus;
  981         PHBA hba;
  982 
  983         /* Clear the config hook */
  984         config_intrhook_disestablish(&hpt_ich);
  985 
  986         /* allocate memory */
  987         i = 0;
  988         ldm_for_each_vbus(vbus, vbus_ext) {
  989                 if (hpt_alloc_mem(vbus_ext)) {
  990                         os_printk("out of memory");
  991                         return;
  992                 }
  993                 i++;
  994         }
  995 
  996         if (!i) {
  997                 if (bootverbose)
  998                         os_printk("no controller detected.");
  999                 return;
 1000         }
 1001 
 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");
 1009                         break; /* FIXME */
 1010                 }
 1011         }
 1012 
 1013         /* register CAM interface */
 1014         ldm_for_each_vbus(vbus, vbus_ext) {
 1015                 struct cam_devq *devq;
 1016                 struct ccb_setasync     ccb;
 1017                 
 1018                 if (bus_dma_tag_create(NULL,/* parent */
 1019                                 4,      /* alignment */
 1020                                 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
 1021                                 BUS_SPACE_MAXADDR,      /* lowaddr */
 1022                                 BUS_SPACE_MAXADDR,      /* highaddr */
 1023                                 NULL, NULL,             /* filter, filterarg */
 1024                                 PAGE_SIZE * (os_max_sg_descriptors-1),  /* maxsize */
 1025                                 os_max_sg_descriptors,  /* nsegments */
 1026                                 0x10000,        /* maxsegsize */
 1027                                 BUS_DMA_WAITOK,         /* flags */
 1028                                 busdma_lock_mutex,      /* lockfunc */
 1029                                 &vbus_ext->lock,                /* lockfuncarg */
 1030                                 &vbus_ext->io_dmat      /* tag */))
 1031                 {
 1032                         return ;
 1033                 }
 1034 
 1035                 for (i=0; i<os_max_queue_comm; i++) {
 1036                         POS_CMDEXT ext = (POS_CMDEXT)malloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK);
 1037                         if (!ext) {
 1038                                 os_printk("Can't alloc cmdext(%d)", i);
 1039                                 return ;
 1040                         }
 1041                         ext->vbus_ext = vbus_ext;
 1042                         ext->next = vbus_ext->cmdext_list;
 1043                         vbus_ext->cmdext_list = ext;
 1044         
 1045                         if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) {
 1046                                 os_printk("Can't create dma map(%d)", i);
 1047                                 return ;
 1048                         }
 1049                         callout_init_mtx(&ext->timeout, &vbus_ext->lock, 0);
 1050                 }
 1051 
 1052                 if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) {
 1053                         os_printk("cam_simq_alloc failed");
 1054                         return ;
 1055                 }
 1056 
 1057                 vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
 1058                                 vbus_ext, 0, &vbus_ext->lock, os_max_queue_comm,
 1059                                 /*tagged*/8,  devq);
 1060                                 
 1061                 if (!vbus_ext->sim) {
 1062                         os_printk("cam_sim_alloc failed");
 1063                         cam_simq_free(devq);
 1064                         return ;
 1065                 }
 1066 
 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;
 1073                         return ;
 1074                 }
 1075         
 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)
 1079                 {
 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;
 1085                         return ;
 1086                 }
 1087                 hpt_unlock_vbus(vbus_ext);
 1088 
 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);
 1095 
 1096                 for (hba = vbus_ext->hba_list; hba; hba = hba->next) {
 1097                         int rid = 0;
 1098                         if ((hba->irq_res = bus_alloc_resource_any(hba->pcidev,
 1099                                 SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL)
 1100                         {
 1101                                 os_printk("can't allocate interrupt");
 1102                                 return ;
 1103                         }
 1104                         
 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)) 
 1107                         {
 1108                                 os_printk("can't set up interrupt");
 1109                                 return ;
 1110                         }
 1111                         hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE);
 1112                 }
 1113 
 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");
 1118         }
 1119         
 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);
 1124         }       
 1125 
 1126         make_dev(&hpt_cdevsw, DRIVER_MINOR, UID_ROOT, GID_OPERATOR,
 1127             S_IRUSR | S_IWUSR, "%s", driver_name);
 1128 }
 1129 
 1130 #if defined(KLD_MODULE)
 1131 
 1132 typedef struct driverlink *driverlink_t;
 1133 struct driverlink {
 1134         kobj_class_t    driver;
 1135         TAILQ_ENTRY(driverlink) link;   /* list of drivers in devclass */
 1136 };
 1137 
 1138 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
 1139 
 1140 struct devclass {
 1141         TAILQ_ENTRY(devclass) link;
 1142         devclass_t      parent;         /* parent in devclass hierarchy */
 1143         driver_list_t   drivers;     /* bus devclasses store drivers for bus */
 1144         char            *name;
 1145         device_t        *devices;       /* array of devices indexed by unit */
 1146         int             maxunit;        /* size of devices array */
 1147 };
 1148 
 1149 static void override_kernel_driver(void)
 1150 {
 1151         driverlink_t dl, dlfirst;
 1152         driver_t *tmpdriver;
 1153         devclass_t dc = devclass_find("pci");
 1154         
 1155         if (dc){
 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;
 1162                                 break;
 1163                         }
 1164                 }
 1165         }
 1166 }
 1167 
 1168 #else 
 1169 #define override_kernel_driver()
 1170 #endif
 1171 
 1172 static void hpt_init(void *dummy)
 1173 {
 1174         if (bootverbose)
 1175                 os_printk("%s %s", driver_name_long, driver_ver);
 1176 
 1177         override_kernel_driver();
 1178         init_config();
 1179 
 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",
 1184                     driver_name_long);
 1185         }
 1186 
 1187 }
 1188 SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
 1189 
 1190 /*
 1191  * CAM driver interface
 1192  */
 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),
 1199         DEVMETHOD_END
 1200 };
 1201 
 1202 static driver_t hpt_pci_driver = {
 1203         driver_name,
 1204         driver_methods,
 1205         sizeof(HBA)
 1206 };
 1207 
 1208 static devclass_t       hpt_devclass;
 1209 
 1210 #ifndef TARGETNAME
 1211 #error "no TARGETNAME found"
 1212 #endif
 1213 
 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);
 1221 
 1222 static int hpt_open(struct cdev *dev, int flags, int devtype, struct thread *td)
 1223 {
 1224         return 0;
 1225 }
 1226 
 1227 static int hpt_close(struct cdev *dev, int flags, int devtype, struct thread *td)
 1228 {
 1229         return 0;
 1230 }
 1231 
 1232 static int hpt_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
 1233 {
 1234         PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data;
 1235         IOCTL_ARG ioctl_args;
 1236         HPT_U32 bytesReturned = 0;
 1237 
 1238         switch (cmd){
 1239         case HPT_DO_IOCONTROL:
 1240         {       
 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,
 1244                                 piop->lpInBuffer,
 1245                                 piop->nInBufferSize,
 1246                                 piop->lpOutBuffer,
 1247                                 piop->nOutBufferSize));
 1248                         
 1249                 memset(&ioctl_args, 0, sizeof(ioctl_args));
 1250                 
 1251                 ioctl_args.dwIoControlCode = piop->dwIoControlCode;
 1252                 ioctl_args.nInBufferSize = piop->nInBufferSize;
 1253                 ioctl_args.nOutBufferSize = piop->nOutBufferSize;
 1254                 ioctl_args.lpBytesReturned = &bytesReturned;
 1255 
 1256                 if (ioctl_args.nInBufferSize) {
 1257                         ioctl_args.lpInBuffer = malloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK);
 1258                         if (!ioctl_args.lpInBuffer)
 1259                                 goto invalid;
 1260                         if (copyin((void*)piop->lpInBuffer,
 1261                                         ioctl_args.lpInBuffer, piop->nInBufferSize))
 1262                                 goto invalid;
 1263                 }
 1264         
 1265                 if (ioctl_args.nOutBufferSize) {
 1266                         ioctl_args.lpOutBuffer = malloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK | M_ZERO);
 1267                         if (!ioctl_args.lpOutBuffer)
 1268                                 goto invalid;
 1269                 }
 1270                 
 1271                 hpt_do_ioctl(&ioctl_args);
 1272         
 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))
 1277                                         goto invalid;
 1278                         }
 1279                         if (piop->lpBytesReturned) {
 1280                                 if (copyout(&bytesReturned,
 1281                                         (void*)piop->lpBytesReturned, sizeof(HPT_U32)))
 1282                                         goto invalid;
 1283                         }
 1284                         if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
 1285                         if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
 1286                         return 0;
 1287                 }
 1288 invalid:
 1289                 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
 1290                 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
 1291                 return EFAULT;
 1292         }
 1293         return EFAULT;
 1294         }
 1295 
 1296         case HPT_SCAN_BUS:
 1297         {
 1298                 return hpt_rescan_bus();
 1299         }
 1300         default:
 1301                 KdPrint(("invalid command!"));
 1302                 return EFAULT;
 1303         }       
 1304 
 1305 }
 1306 
 1307 static int      hpt_rescan_bus(void)
 1308 {
 1309         union ccb                       *ccb;
 1310         PVBUS                           vbus;
 1311         PVBUS_EXT                       vbus_ext;       
 1312                 
 1313         ldm_for_each_vbus(vbus, vbus_ext) {
 1314                 if ((ccb = xpt_alloc_ccb()) == NULL)
 1315                         return(ENOMEM);
 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) {
 1319                         xpt_free_ccb(ccb);
 1320                         return(EIO);
 1321                 }
 1322                 xpt_rescan(ccb);
 1323         }
 1324         
 1325         return(0);      
 1326 }

Cache object: 41b86afad29b529b25abdf1b0ddf584a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.