Author: jhb
Date: Tue Aug  5 23:58:49 2014
New Revision: 269617
URL: http://svnweb.freebsd.org/changeset/base/269617

Log:
  Various fixes to hptmv(4):
  - Replace the global driver lock with a per-instance device lock.
  - Use the per-instance device lock instead of Giant for the CAM sim lock.
  - Add global locks to protect the adapter list and DPC queues.
  - Use wakeup() and mtx_sleep() to wait for certain events like the
    controller going idle rather than polling via timeouts passed to
    tsleep().
  - Use callout(9) instead of timeout(9).
  - Mark the interrupt handler MPSAFE.
  - Remove compat shims for FreeBSD versions older than 8.0.
  
  Reviewed by:  Steve Chang <ych...@highpoint-tech.com>

Modified:
  head/sys/dev/hptmv/entry.c
  head/sys/dev/hptmv/global.h
  head/sys/dev/hptmv/hptproc.c
  head/sys/dev/hptmv/ioctl.c
  head/sys/dev/hptmv/mv.c
  head/sys/dev/hptmv/osbsd.h

Modified: head/sys/dev/hptmv/entry.c
==============================================================================
--- head/sys/dev/hptmv/entry.c  Tue Aug  5 23:55:23 2014        (r269616)
+++ head/sys/dev/hptmv/entry.c  Tue Aug  5 23:58:49 2014        (r269617)
@@ -40,20 +40,12 @@ __FBSDID("$FreeBSD$");
 #include <sys/proc.h>
 #include <sys/kthread.h>
 
-#if (__FreeBSD_version >= 500000)
 #include <sys/mutex.h>
 #include <sys/module.h>
-#endif
+#include <sys/sx.h>
 
-#if (__FreeBSD_version >= 500000)
 #include <dev/pci/pcireg.h>
 #include <dev/pci/pcivar.h>
-#else 
-#include <pci/pcireg.h>
-#include <pci/pcivar.h>
-#include <sys/wait.h>
-#include <sys/sysproto.h>
-#endif
 
 #ifndef __KERNEL__
 #define __KERNEL__
@@ -119,6 +111,7 @@ static void HPTLIBAPI fOsCommandDone(_VB
 static void ccb_done(union ccb *ccb);
 static void hpt_queue_ccb(union ccb **ccb_Q, union ccb *ccb);
 static void hpt_free_ccb(union ccb **ccb_Q, union ccb *ccb);
+static void hpt_intr_locked(IAL_ADAPTER_T *pAdapter);
 static void    hptmv_free_edma_queues(IAL_ADAPTER_T *pAdapter);
 static void    hptmv_free_channel(IAL_ADAPTER_T *pAdapter, MV_U8 channelNum);
 static void    handleEdmaError(_VBUS_ARG PCommand pCmd);
@@ -143,6 +136,8 @@ static MV_BOOLEAN hptmv_event_notify(MV_
 #define ccb_ccb_ptr spriv_ptr0
 #define ccb_adapter ccb_h.spriv_ptr1
 
+static struct sx hptmv_list_lock;
+SX_SYSINIT(hptmv_list_lock, &hptmv_list_lock, "hptmv list");
 IAL_ADAPTER_T *gIal_Adapter = 0;
 IAL_ADAPTER_T *pCurAdapter = 0;
 static MV_SATA_CHANNEL gMvSataChannels[MAX_VBUS][MV_SATA_CHANNELS_NUM];
@@ -159,48 +154,11 @@ UCHAR DPC_Request_Nums = 0; 
 static ST_HPT_DPC DpcQueue[MAX_DPC];
 static int DpcQueue_First=0;
 static int DpcQueue_Last = 0;
+static struct mtx DpcQueue_Lock;
+MTX_SYSINIT(hpmtv_dpc_lock, &DpcQueue_Lock, "hptmv dpc", MTX_DEF);
 
 char DRIVER_VERSION[] = "v1.16";
 
-#if (__FreeBSD_version >= 500000)
-static struct mtx driver_lock;
-intrmask_t lock_driver()
-{
-
-       intrmask_t spl = 0;
-       mtx_lock(&driver_lock);
-       return spl;
-}
-void unlock_driver(intrmask_t spl)
-{
-       mtx_unlock(&driver_lock);
-}
-#else 
-static int driver_locked = 0;
-intrmask_t lock_driver()
-{
-       intrmask_t spl = splcam();
-loop:
-       while (driver_locked)
-               tsleep(&driver_locked, PRIBIO, "hptlck", hz);
-       atomic_add_int(&driver_locked, 1);
-       if (driver_locked>1) {
-               atomic_subtract_int(&driver_locked, 1);
-               goto loop;
-       }
-       return spl;
-}
-
-void unlock_driver(intrmask_t spl)
-{
-       atomic_subtract_int(&driver_locked, 1);
-       if (driver_locked==0) {
-               wakeup(&driver_locked);
-       }
-       splx(spl);
-}
-#endif
-
 
/*******************************************************************************
  *     Name:   hptmv_free_channel
  *
@@ -790,7 +748,8 @@ hptmv_handle_event(void * data, int flag
        IAL_ADAPTER_T   *pAdapter = (IAL_ADAPTER_T *)data;
        MV_SATA_ADAPTER *pMvSataAdapter = &pAdapter->mvSataAdapter;
        MV_U8           channelIndex;
- 
+
+       mtx_assert(&pAdapter->lock, MA_OWNED);
 /*     mvOsSemTake(&pMvSataAdapter->semaphore); */
        for (channelIndex = 0; channelIndex < MV_SATA_CHANNELS_NUM; 
channelIndex++)
        {
@@ -898,7 +857,7 @@ hptmv_event_notify(MV_SATA_ADAPTER *pMvS
                                        KdPrint(("RR18xx [%d,%d]: device 
connected event received\n",
                                                         
pMvSataAdapter->adapterId, channel));
                                        /* Delete previous timers (if multiple 
drives connected in the same time */
-                                       pAdapter->event_timer_connect = 
timeout(hptmv_handle_event_connect, pAdapter, 10*hz);
+                                       
callout_reset(&pAdapter->event_timer_connect, 10 * hz, 
hptmv_handle_event_connect, pAdapter);
                                }
                                else if (param1 == EVENT_DISCONNECT)
                                {
@@ -907,7 +866,7 @@ hptmv_event_notify(MV_SATA_ADAPTER *pMvS
                                                         
pMvSataAdapter->adapterId, channel));
                                        device_change(pAdapter, channel, FALSE);
                                        /* Delete previous timers (if multiple 
drives disconnected in the same time */
-                                       /*pAdapter->event_timer_disconnect = 
timeout(hptmv_handle_event_disconnect, pAdapter, 10*hz); */
+                                       
/*callout_reset(&pAdapter->event_timer_disconnect, 10 * hz, 
hptmv_handle_event_disconnect, pAdapter); */
                                        /*It is not necessary to wait, handle 
it directly*/
                                        hptmv_handle_event_disconnect(pAdapter);
                                }
@@ -1286,18 +1245,6 @@ dmamap_put(PBUS_DMAMAP p)
        p->pAdapter->pbus_dmamap_list = p;
 }
 
-/*Since mtx not provide the initialize when declare, so we Final init here to 
initialize the global mtx*/
-#if __FreeBSD_version >= 500000
-#define override_kernel_driver()
-
-static void hpt_init(void *dummy)
-{
-       override_kernel_driver();       
-       mtx_init(&driver_lock, "hptsleeplock", NULL, MTX_DEF);
-}
-SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
-#endif
-
 static int num_adapters = 0;
 static int
 init_adapter(IAL_ADAPTER_T *pAdapter)
@@ -1308,8 +1255,11 @@ init_adapter(IAL_ADAPTER_T *pAdapter)
 
        PVDevice pVDev;
 
-       intrmask_t oldspl = lock_driver();
+       mtx_init(&pAdapter->lock, "hptsleeplock", NULL, MTX_DEF);
+       callout_init_mtx(&pAdapter->event_timer_connect, &pAdapter->lock, 0);
+       callout_init_mtx(&pAdapter->event_timer_disconnect, &pAdapter->lock, 0);
 
+       sx_xlock(&hptmv_list_lock);
        pAdapter->next = 0;
 
        if(gIal_Adapter == 0){
@@ -1320,6 +1270,7 @@ init_adapter(IAL_ADAPTER_T *pAdapter)
                pCurAdapter->next = pAdapter;
                pCurAdapter = pAdapter;
        }
+       sx_xunlock(&hptmv_list_lock);
 
        pAdapter->outstandingCommands = 0;
 
@@ -1337,10 +1288,8 @@ init_adapter(IAL_ADAPTER_T *pAdapter)
                        MAX_SG_DESCRIPTORS, /* nsegments */
                        0x10000,        /* maxsegsize */
                        BUS_DMA_WAITOK,         /* flags */
-#if __FreeBSD_version>502000
                        busdma_lock_mutex,      /* lockfunc */
-                       &driver_lock,           /* lockfuncarg */
-#endif
+                       &pAdapter->lock,        /* lockfuncarg */
                        &pAdapter->io_dma_parent /* tag */))
                {
                        return ENXIO;
@@ -1350,7 +1299,6 @@ init_adapter(IAL_ADAPTER_T *pAdapter)
        if (hptmv_allocate_edma_queues(pAdapter))
        {
                MV_ERROR("RR18xx: Failed to allocate memory for EDMA queues\n");
-               unlock_driver(oldspl);
                return ENOMEM;
        }
 
@@ -1363,7 +1311,6 @@ init_adapter(IAL_ADAPTER_T *pAdapter)
        {
                MV_ERROR("RR18xx: Failed to remap memory space\n");
                hptmv_free_edma_queues(pAdapter);
-               unlock_driver(oldspl);
                return ENXIO;
        }
        else
@@ -1393,7 +1340,6 @@ init_adapter(IAL_ADAPTER_T *pAdapter)
 unregister:
                bus_release_resource(pAdapter->hpt_dev, SYS_RES_MEMORY, rid, 
pAdapter->mem_res);
                hptmv_free_edma_queues(pAdapter);
-               unlock_driver(oldspl);
                return ENXIO;
        }
        pAdapter->ver_601 = pMvSataAdapter->pcbVersion;
@@ -1438,7 +1384,7 @@ unregister:
                        free(pAdapter->pbus_dmamap, M_DEVBUF);
                        goto unregister;
                }
-               callout_handle_init(&pmap->timeout_ch);
+               callout_init_mtx(&pmap->timeout, &pAdapter->lock, 0);
        }
        /* setup PRD Tables */
        KdPrint(("Allocate PRD Tables\n"));
@@ -1537,7 +1483,6 @@ unregister:
 #endif
 
        mvSataUnmaskAdapterInterrupt(pMvSataAdapter);
-       unlock_driver(oldspl);
        return 0;
 }
 
@@ -2036,20 +1981,7 @@ hpt_attach(device_t dev)
        struct cam_devq *devq;
        struct cam_sim *hpt_vsim;
 
-       printf("%s Version %s \n", DRIVER_NAME, DRIVER_VERSION);
-
-       if (!pAdapter)
-       {
-               pAdapter = (IAL_ADAPTER_T *)malloc(sizeof (IAL_ADAPTER_T), 
M_DEVBUF, M_NOWAIT);
-#if __FreeBSD_version > 410000
-               device_set_softc(dev, (void *)pAdapter);
-#else 
-               device_set_driver(dev, (driver_t *)pAdapter);
-#endif
-       }
-
-       if (!pAdapter) return (ENOMEM);
-       bzero(pAdapter, sizeof(IAL_ADAPTER_T));
+       device_printf(dev, "%s Version %s \n", DRIVER_NAME, DRIVER_VERSION);
 
        pAdapter->hpt_dev = dev;
        
@@ -2064,13 +1996,9 @@ hpt_attach(device_t dev)
                return(ENXIO);
        }
 
-#if __FreeBSD_version <700000
-       if (bus_setup_intr(pAdapter->hpt_dev, pAdapter->hpt_irq, INTR_TYPE_CAM,
-                               hpt_intr, pAdapter, &pAdapter->hpt_intr))
-#else 
-       if (bus_setup_intr(pAdapter->hpt_dev, pAdapter->hpt_irq, INTR_TYPE_CAM,
+       if (bus_setup_intr(pAdapter->hpt_dev, pAdapter->hpt_irq,
+                               INTR_TYPE_CAM | INTR_MPSAFE,
                                NULL, hpt_intr, pAdapter, &pAdapter->hpt_intr))
-#endif
        {
                hpt_printk(("can't set up interrupt\n"));
                free(pAdapter, M_DEVBUF);
@@ -2100,25 +2028,19 @@ hpt_attach(device_t dev)
        /*
         * Construct our SIM entry
         */
-#if __FreeBSD_version <700000
-       hpt_vsim = cam_sim_alloc(hpt_action, hpt_poll, __str(PROC_DIR_NAME),
-                       pAdapter, device_get_unit(pAdapter->hpt_dev), 1, 8, 
devq);
-#else 
        hpt_vsim = cam_sim_alloc(hpt_action, hpt_poll, __str(PROC_DIR_NAME),
-                       pAdapter, device_get_unit(pAdapter->hpt_dev), &Giant, 
1, 8, devq);
-#endif
+                       pAdapter, device_get_unit(pAdapter->hpt_dev),
+                       &pAdapter->lock, 1, 8, devq);
        if (hpt_vsim == NULL) {
                cam_simq_free(devq);
                return ENOMEM;
        }
 
-#if __FreeBSD_version <700000
-       if (xpt_bus_register(hpt_vsim, 0) != CAM_SUCCESS)
-#else 
+       mtx_lock(&pAdapter->lock);
        if (xpt_bus_register(hpt_vsim, dev, 0) != CAM_SUCCESS)
-#endif
        {
                cam_sim_free(hpt_vsim, /*free devq*/ TRUE);
+               mtx_unlock(&pAdapter->lock);
                hpt_vsim = NULL;
                return ENXIO;
        }
@@ -2129,9 +2051,11 @@ hpt_attach(device_t dev)
        {
                xpt_bus_deregister(cam_sim_path(hpt_vsim));
                cam_sim_free(hpt_vsim, /*free_devq*/TRUE);
+               mtx_unlock(&pAdapter->lock);
                hpt_vsim = NULL;
                return ENXIO;
        }
+       mtx_unlock(&pAdapter->lock);
 
        xpt_setup_ccb(&(ccb->ccb_h), pAdapter->path, /*priority*/5);
        ccb->ccb_h.func_code = XPT_SASYNC_CB;
@@ -2164,7 +2088,11 @@ hpt_detach(device_t dev)
 static void
 hpt_poll(struct cam_sim *sim)
 {
-       hpt_intr((void *)cam_sim_softc(sim));
+       IAL_ADAPTER_T *pAdapter;
+
+       pAdapter = cam_sim_softc(sim);
+       
+       hpt_intr_locked((void *)cam_sim_softc(sim));
 }
 
 /****************************************************************
@@ -2174,9 +2102,19 @@ hpt_poll(struct cam_sim *sim)
 static void
 hpt_intr(void *arg)
 {
-       IAL_ADAPTER_T *pAdapter = (IAL_ADAPTER_T *)arg;
-       intrmask_t oldspl = lock_driver();
-       
+       IAL_ADAPTER_T *pAdapter;
+
+       pAdapter = arg;
+       mtx_lock(&pAdapter->lock);
+       hpt_intr_locked(pAdapter);
+       mtx_unlock(&pAdapter->lock);
+}
+
+static void
+hpt_intr_locked(IAL_ADAPTER_T *pAdapter)
+{
+
+       mtx_assert(&pAdapter->lock, MA_OWNED);
        /* KdPrintI(("----- Entering Isr() -----\n")); */
        if (mvSataInterruptServiceRoutine(&pAdapter->mvSataAdapter) == MV_TRUE)
        {
@@ -2185,7 +2123,6 @@ hpt_intr(void *arg)
        }
 
        /* KdPrintI(("----- Leaving Isr() -----\n")); */
-       unlock_driver(oldspl);
 }
 
 /**********************************************************
@@ -2228,11 +2165,11 @@ hpt_shutdown(device_t dev)
                IAL_ADAPTER_T *pAdapter;
        
                pAdapter = device_get_softc(dev);
-               if (pAdapter == NULL)
-                       return (EINVAL);
 
                EVENTHANDLER_DEREGISTER(shutdown_final, pAdapter->eh);
+               mtx_lock(&pAdapter->lock);
                FlushAdapter(pAdapter);
+               mtx_unlock(&pAdapter->lock);
                  /* give the flush some time to happen, 
                    *otherwise "shutdown -p now" will make file system 
corrupted */
                DELAY(1000 * 1000 * 5);
@@ -2288,6 +2225,7 @@ ccb_done(union ccb *ccb)
        {
                if(DPC_Request_Nums == 0)
                        Check_Idle_Call(pAdapter);
+               wakeup(pAdapter);
        }
 }
 
@@ -2301,11 +2239,11 @@ ccb_done(union ccb *ccb)
 void
 hpt_action(struct cam_sim *sim, union ccb *ccb)
 {
-       intrmask_t oldspl;
        IAL_ADAPTER_T * pAdapter = (IAL_ADAPTER_T *) cam_sim_softc(sim);
        PBUS_DMAMAP  pmap;
        _VBUS_INST(&pAdapter->VBus)
 
+       mtx_assert(&pAdapter->lock, MA_OWNED);
        CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("hpt_action\n"));
        KdPrint(("hpt_action(%lx,%lx{%x})\n", (u_long)sim, (u_long)ccb, 
ccb->ccb_h.func_code));
 
@@ -2327,7 +2265,6 @@ hpt_action(struct cam_sim *sim, union cc
                                return;
                        }
 
-                       oldspl = lock_driver();
                        if (pAdapter->outstandingCommands==0 && 
DPC_Request_Nums==0)
                                Check_Idle_Call(pAdapter);
 
@@ -2340,7 +2277,6 @@ hpt_action(struct cam_sim *sim, union cc
                                hpt_queue_ccb(&pAdapter->pending_Q, ccb);
                        else
                                OsSendCommand(_VBUS_P ccb);
-                       unlock_driver(oldspl);
 
                        /* KdPrint(("leave scsiio\n")); */
                        break;
@@ -2348,9 +2284,7 @@ hpt_action(struct cam_sim *sim, union cc
 
                case XPT_RESET_BUS:
                        KdPrint(("reset bus\n"));
-                       oldspl = lock_driver();
                        fResetVBus(_VBUS_P0);
-                       unlock_driver(oldspl);
                        xpt_done(ccb);
                        break;
 
@@ -2374,29 +2308,7 @@ hpt_action(struct cam_sim *sim, union cc
                        break;
 
                case XPT_CALC_GEOMETRY:
-#if __FreeBSD_version >= 500000
                        cam_calc_geometry(&ccb->ccg, 1);
-#else
-                       {
-                       struct    ccb_calc_geometry *ccg;
-                       u_int32_t size_mb;
-                       u_int32_t secs_per_cylinder;
-
-                       ccg = &ccb->ccg;
-                       size_mb = ccg->volume_size / ((1024L * 1024L) / 
ccg->block_size);
-
-                       if (size_mb > 1024 ) {
-                               ccg->heads = 255;
-                               ccg->secs_per_track = 63;
-                       } else {
-                               ccg->heads = 64;
-                               ccg->secs_per_track = 32;
-                       }
-                       secs_per_cylinder = ccg->heads * ccg->secs_per_track;
-                       ccg->cylinders = ccg->volume_size / secs_per_cylinder;
-                       ccb->ccb_h.status = CAM_REQ_CMP;
-                       }
-#endif
                        xpt_done(ccb);
                        break;
 
@@ -2482,20 +2394,20 @@ hpt_free_ccb(union ccb **ccb_Q, union cc
  ***************************************************************************/
 static void hpt_worker_thread(void)
 {
-       intrmask_t oldspl;
 
        for(;;) {
+               mtx_lock(&DpcQueue_Lock);
                while (DpcQueue_First!=DpcQueue_Last) {
                        ST_HPT_DPC p;
-                       oldspl = lock_driver();
                        p = DpcQueue[DpcQueue_First];
                        DpcQueue_First++;
                        DpcQueue_First %= MAX_DPC;
                        DPC_Request_Nums++;
-                       unlock_driver(oldspl);
+                       mtx_unlock(&DpcQueue_Lock);
                        p.dpc(p.pAdapter, p.arg, p.flags);
 
-                       oldspl = lock_driver();
+                       mtx_lock(&p.pAdapter->lock);
+                       mtx_lock(&DpcQueue_Lock);
                        DPC_Request_Nums--;
                        /* since we may have prevented Check_Idle_Call, do it 
here */
                        if (DPC_Request_Nums==0) {
@@ -2505,28 +2417,22 @@ static void hpt_worker_thread(void)
                                        CheckPendingCall(_VBUS_P0);
                                }
                        }
-                       unlock_driver(oldspl);
+                       mtx_unlock(&p.pAdapter->lock);
+                       mtx_unlock(&DpcQueue_Lock);
 
                        /*Schedule out*/
-#if (__FreeBSD_version < 500000)
-                       YIELD_THREAD;
-#else 
-#if (__FreeBSD_version > 700033)
-                       pause("sched", 1);
-#else
-                       tsleep((caddr_t)hpt_worker_thread, PPAUSE, "sched", 1); 
-#endif
-#endif
                        if (SIGISMEMBER(curproc->p_siglist, SIGSTOP)) {
                                /* abort rebuilding process. */
                                IAL_ADAPTER_T *pAdapter;
                                PVDevice      pArray;
                                PVBus         _vbus_p;
                                int i;
+
+                               sx_slock(&hptmv_list_lock);
                                pAdapter = gIal_Adapter;
 
                                while(pAdapter != 0){
-
+                                       mtx_lock(&pAdapter->lock);
                                        _vbus_p = &pAdapter->VBus;
 
                                        for (i=0;i<MAX_ARRAY_PER_VBUS;i++) 
@@ -2540,34 +2446,24 @@ static void hpt_worker_thread(void)
                                                                
pArray->u.array.rf_abort_rebuild = 1;
                                                        }
                                        }
+                                       mtx_unlock(&pAdapter->lock);
                                        pAdapter = pAdapter->next;
                                }
+                               sx_sunlock(&hptmv_list_lock);
                        }
+                       mtx_lock(&DpcQueue_Lock);
                }
+               mtx_unlock(&DpcQueue_Lock);
 
 /*Remove this debug option*/
 /*
 #ifdef DEBUG
                if (SIGISMEMBER(curproc->p_siglist, SIGSTOP))
-#if (__FreeBSD_version > 700033)
                        pause("hptrdy", 2*hz);
-#else
-                       tsleep((caddr_t)hpt_worker_thread, PPAUSE, "hptrdy", 
2*hz);
-#endif
 #endif
 */
-       #if (__FreeBSD_version >= 800002)
                kproc_suspend_check(curproc);
-       #elif (__FreeBSD_version >= 500043)
-               kthread_suspend_check(curproc);
-       #else 
-               kproc_suspend_loop(curproc);
-       #endif
-#if (__FreeBSD_version > 700033)
-               pause("hptrdy", 2*hz);  /* wait for something to do */
-#else
-               tsleep((caddr_t)hpt_worker_thread, PPAUSE, "hptrdy", 2*hz);  /* 
wait for something to do */
-#endif
+               pause("-", 2*hz);  /* wait for something to do */
        }
 }
 
@@ -2586,6 +2482,7 @@ launch_worker_thread(void)
        
        kproc_start(&hpt_kp);
 
+       sx_slock(&hptmv_list_lock);
        for (pAdapTemp = gIal_Adapter; pAdapTemp; pAdapTemp = pAdapTemp->next) {
 
                _VBUS_INST(&pAdapTemp->VBus)
@@ -2601,17 +2498,13 @@ launch_worker_thread(void)
                                        (UCHAR)((pVDev->u.array.CriticalMembers 
|| pVDev->VDeviceType == VD_RAID_1)? DUPLICATE : REBUILD_PARITY));
                        }
        }
+       sx_sunlock(&hptmv_list_lock);
 
        /*
         * hpt_worker_thread needs to be suspended after shutdown sync, when fs 
sync finished.
         */
-#if (__FreeBSD_version < 500043)
-       EVENTHANDLER_REGISTER(shutdown_post_sync, shutdown_kproc, hptdaemonproc,
-           SHUTDOWN_PRI_LAST);
-#else 
        EVENTHANDLER_REGISTER(shutdown_post_sync, kproc_shutdown, hptdaemonproc,
            SHUTDOWN_PRI_LAST);
-#endif
 }
 /*
  *SYSINIT(hptwt, SI_SUB_KTHREAD_IDLE, SI_ORDER_FIRST, launch_worker_thread, 
NULL);
@@ -2721,10 +2614,12 @@ SetInquiryData(PINQUIRYDATA inquiryData,
 static void
 hpt_timeout(void *arg)
 {
-       _VBUS_INST(&((PBUS_DMAMAP)((union ccb 
*)arg)->ccb_adapter)->pAdapter->VBus)
-       intrmask_t oldspl = lock_driver();
+       PBUS_DMAMAP pmap = (PBUS_DMAMAP)((union ccb *)arg)->ccb_adapter;
+       IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
+       _VBUS_INST(&pAdapter->VBus)
+
+       mtx_assert(&pAdapter->lock, MA_OWNED);
        fResetVBus(_VBUS_P0);
-       unlock_driver(oldspl);
 }
 
 static void 
@@ -2766,7 +2661,7 @@ hpt_io_dmamap_callback(void *arg, bus_dm
                }
        }
 
-       pmap->timeout_ch = timeout(hpt_timeout, (caddr_t)ccb, 20*hz);
+       callout_reset(&pmap->timeout, 20 * hz, hpt_timeout, ccb);
        pVDev->pfnSendCommand(_VBUS_P pCmd);
        CheckPendingCall(_VBUS_P0);
 }
@@ -2964,6 +2859,8 @@ OsSendCommand(_VBUS_ARG union ccb *ccb)
                                ccb->ccb_h.status = CAM_REQ_CMP_ERR;
                                dmamap_put(pmap);
                                pAdapter->outstandingCommands--;
+                               if (pAdapter->outstandingCommands == 0)
+                                       wakeup(pAdapter);
                                xpt_done(ccb);
                        }
                        goto Command_Complished;
@@ -2987,8 +2884,8 @@ fOsCommandDone(_VBUS_ARG PCommand pCmd)
        IAL_ADAPTER_T *pAdapter = pmap->pAdapter;
 
        KdPrint(("fOsCommandDone(pcmd=%p, result=%d)\n", pCmd, pCmd->Result));
-       
-       untimeout(hpt_timeout, (caddr_t)ccb, pmap->timeout_ch);
+
+       callout_stop(&pmap->timeout);
        
        switch(pCmd->Result) {
        case RETURN_SUCCESS:
@@ -3032,9 +2929,11 @@ hpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T
 {
        int p;
 
+       mtx_lock(&DpcQueue_Lock);
        p = (DpcQueue_Last + 1) % MAX_DPC;
        if (p==DpcQueue_First) {
                KdPrint(("DPC Queue full!\n"));
+               mtx_unlock(&DpcQueue_Lock);
                return -1;
        }
 
@@ -3043,6 +2942,7 @@ hpt_queue_dpc(HPT_DPC dpc, IAL_ADAPTER_T
        DpcQueue[DpcQueue_Last].arg = arg;
        DpcQueue[DpcQueue_Last].flags = flags;
        DpcQueue_Last = p;
+       mtx_unlock(&DpcQueue_Lock);
 
        return 0;
 }

Modified: head/sys/dev/hptmv/global.h
==============================================================================
--- head/sys/dev/hptmv/global.h Tue Aug  5 23:55:23 2014        (r269616)
+++ head/sys/dev/hptmv/global.h Tue Aug  5 23:58:49 2014        (r269617)
@@ -132,11 +132,6 @@ typedef struct _VDevice_Ext
 #define LongDiv(x, y)          (x / (UINT)(y))
 #define LongRem(x, y)          (x % (UINT)(y))
 #define LongMul(x, y)          (x * y)
-/* Minimum and maximum macros */
-#if (__FreeBSD_version < 501000)
-#define MAX(a, b)      (((a) > (b)) ? (a) : (b))
-#define MIN(a, b)      (((a) < (b)) ? (a) : (b))
-#endif
 
 /*************************************************************************
  * C library
@@ -151,9 +146,6 @@ unsigned HPTLIBAPI os_strlen(const char 
 #define memcpy os_memcpy
 #define memset os_memset
 #define strlen os_strlen
-#elif (__FreeBSD_version <= 410000) 
-#define        memcpy(d, s, len)     bcopy((s),(d),(len))
-#define        memset(d, s, len)     bzero((d),(len))
 #endif
 #define ZeroMemory(a, b)       memset((char *)a, 0, b)
 #define MemoryCopy(a,b,c)      memcpy((char *)(a), (char *)(b), (UINT)(c))

Modified: head/sys/dev/hptmv/hptproc.c
==============================================================================
--- head/sys/dev/hptmv/hptproc.c        Tue Aug  5 23:55:23 2014        
(r269616)
+++ head/sys/dev/hptmv/hptproc.c        Tue Aug  5 23:58:49 2014        
(r269617)
@@ -65,8 +65,8 @@ hpt_set_asc_info(IAL_ADAPTER_T *pAdapter
        PVDevice pSubArray, pVDev;
        UINT    i, iarray, ichan;
        struct cam_periph *periph = NULL;
-       intrmask_t oldspl;
 
+       mtx_lock(&pAdapter->lock);
 #ifdef SUPPORT_ARRAY   
        if (length>=8 && strncmp(buffer, "rebuild ", 8)==0) 
        {
@@ -74,7 +74,6 @@ hpt_set_asc_info(IAL_ADAPTER_T *pAdapter
                length-=8;
                if (length>=5 && strncmp(buffer, "start", 5)==0) 
                {
-                       oldspl = lock_driver();
                        for(i = 0; i < MAX_ARRAY_PER_VBUS; i++)
                                if 
((pArray=ArrayTables(i))->u.array.dArStamp==0)
                                        continue; 
@@ -83,12 +82,11 @@ hpt_set_asc_info(IAL_ADAPTER_T *pAdapter
                            hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, 
pAdapter, pArray, 
                                                        
(UCHAR)((pArray->u.array.CriticalMembers || pArray->VDeviceType == VD_RAID_1)? 
DUPLICATE : REBUILD_PARITY));
                                }
-                       unlock_driver(oldspl);
+                       mtx_unlock(&pAdapter->lock);
                        return orig_length;
                }
                else if (length>=4 && strncmp(buffer, "stop", 4)==0) 
                {
-                       oldspl = lock_driver();
                        for(i = 0; i < MAX_ARRAY_PER_VBUS; i++)
                                if 
((pArray=ArrayTables(i))->u.array.dArStamp==0)
                                        continue; 
@@ -96,7 +94,7 @@ hpt_set_asc_info(IAL_ADAPTER_T *pAdapter
                                        if (pArray->u.array.rf_rebuilding)
                                            pArray->u.array.rf_abort_rebuild = 
1;
                                }
-                       unlock_driver(oldspl);
+                       mtx_unlock(&pAdapter->lock);
                        return orig_length;
                }       
                else if (length>=3 && buffer[1]==','&& buffer[0]>='1'&& 
buffer[2]>='1') 
@@ -107,17 +105,24 @@ hpt_set_asc_info(IAL_ADAPTER_T *pAdapter
             if(iarray >= MAX_VDEVICE_PER_VBUS || ichan >= 
MV_SATA_CHANNELS_NUM) return -EINVAL;
 
                        pArray = _vbus_p->pVDevice[iarray];
-               if (!pArray || (pArray->vf_online == 0)) return -EINVAL;
+                       if (!pArray || (pArray->vf_online == 0)) {
+                               mtx_unlock(&pAdapter->lock);
+                               return -EINVAL;
+                       }
 
             for (i=0;i<MV_SATA_CHANNELS_NUM;i++)
                                if(i == ichan)
                                    goto rebuild;
 
+               mtx_unlock(&pAdapter->lock);
                return -EINVAL;
 
 rebuild:
                pVDev = &pAdapter->VDevices[ichan];
-               if(!pVDev->u.disk.df_on_line || pVDev->pParent) return -EINVAL;
+               if(!pVDev->u.disk.df_on_line || pVDev->pParent) {
+                       mtx_unlock(&pAdapter->lock);
+                       return -EINVAL;
+               }
 
                /* Not allow to use a mounted disk ??? test*/
                        for(i = 0; i < MAX_VDEVICE_PER_VBUS; i++)
@@ -127,6 +132,7 @@ rebuild:
                                        if (periph != NULL && periph->refcount 
>= 1)
                                        {
                                                hpt_printk(("Can not use disk 
used by OS!\n"));
+                           mtx_unlock(&pAdapter->lock);
                            return -EINVAL;     
                                        }
                                        /* the Mounted Disk isn't delete */
@@ -139,15 +145,13 @@ rebuild:
                                {
                                        pSubArray = pArray;
 loop:
-                                       oldspl = lock_driver();
                                        if(hpt_add_disk_to_array(_VBUS_P 
VDEV_TO_ID(pSubArray), VDEV_TO_ID(pVDev)) == -1) {
-                                               unlock_driver(oldspl);
+                                               mtx_unlock(&pAdapter->lock);
                                                return -EINVAL;
                                        }
                                        pSubArray->u.array.rf_auto_rebuild = 0;
                                        pSubArray->u.array.rf_abort_rebuild = 0;
                                        
hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pSubArray, DUPLICATE);
-                                       unlock_driver(oldspl);
                                        break;
                                }
                                case VD_RAID_0:
@@ -159,8 +163,10 @@ loop:
                                                          goto loop;
                                                }
                                default:
+                                       mtx_unlock(&pAdapter->lock);
                                        return -EINVAL;
                        }
+                       mtx_unlock(&pAdapter->lock);
                        return orig_length;
                }
        }
@@ -175,24 +181,31 @@ loop:
             if (length>=1 && *buffer>='1') 
                        {
                                iarray = *buffer-'1';
-                               if(iarray >= MAX_VDEVICE_PER_VBUS) return 
-EINVAL;
+                               if(iarray >= MAX_VDEVICE_PER_VBUS) {
+                                       mtx_unlock(&pAdapter->lock);
+                                       return -EINVAL;
+                               }
 
                                pArray = _vbus_p->pVDevice[iarray];
-                               if (!pArray || (pArray->vf_online == 0)) return 
-EINVAL;
+                               if (!pArray || (pArray->vf_online == 0)) {
+                                       mtx_unlock(&pAdapter->lock);
+                                       return -EINVAL;
+                               }
                                
-                               if(pArray->VDeviceType != VD_RAID_1 && 
pArray->VDeviceType != VD_RAID_5)
+                               if(pArray->VDeviceType != VD_RAID_1 && 
pArray->VDeviceType != VD_RAID_5) {
+                                       mtx_unlock(&pAdapter->lock);
                                        return -EINVAL;
+                               }
 
                                if (!(pArray->u.array.rf_need_rebuild ||
                                        pArray->u.array.rf_rebuilding ||
                                        pArray->u.array.rf_verifying ||
                                        pArray->u.array.rf_initializing))
                                {
-                                       oldspl = lock_driver();
                                        pArray->u.array.RebuildSectors = 0;
                                        
hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, VERIFY);
-                                       unlock_driver(oldspl);
                                }
+               mtx_unlock(&pAdapter->lock);
                 return orig_length;
                        }
                }
@@ -203,16 +216,21 @@ loop:
             if (length>=1 && *buffer>='1') 
                        {
                                iarray = *buffer-'1';
-                               if(iarray >= MAX_VDEVICE_PER_VBUS) return 
-EINVAL;
+                               if(iarray >= MAX_VDEVICE_PER_VBUS) {
+                                       mtx_unlock(&pAdapter->lock);
+                                       return -EINVAL;
+                               }
 
                                pArray = _vbus_p->pVDevice[iarray];
-                               if (!pArray || (pArray->vf_online == 0)) return 
-EINVAL;
+                               if (!pArray || (pArray->vf_online == 0)) {
+                                       mtx_unlock(&pAdapter->lock);
+                                       return -EINVAL;
+                               }
                                if(pArray->u.array.rf_verifying) 
                                {
-                                       oldspl = lock_driver();
                                    pArray->u.array.rf_abort_rebuild = 1;
-                                   unlock_driver(oldspl);
                                }
+                           mtx_unlock(&pAdapter->lock);
                            return orig_length;
                        }
                }
@@ -226,6 +244,7 @@ loop:
                        _vbus_(r5.enable_write_back) = *buffer-'0';
                        if (_vbus_(r5.enable_write_back))
                                hpt_printk(("RAID5 write back enabled"));
+                       mtx_unlock(&pAdapter->lock);
                        return orig_length;
                }
        }
@@ -239,6 +258,7 @@ loop:
                length-=9;
                if (length>=1 && *buffer>='0' && *buffer<='3') {
                        hpt_dbg_level = *buffer-'0';
+                       mtx_unlock(&pAdapter->lock);
                        return orig_length;
                }
        }
@@ -246,6 +266,7 @@ loop:
                /* TO DO */
        }
 #endif
+       mtx_unlock(&pAdapter->lock);
 
        return -EINVAL;
 }
@@ -486,12 +507,12 @@ hpt_get_info(IAL_ADAPTER_T *pAdapter, HP
        PVDevice pVDev;
 
 #ifndef FOR_DEMO
+       mtx_lock(&pAdapter->lock);
        if (pAdapter->beeping) {
-               intrmask_t oldspl = lock_driver();
                pAdapter->beeping = 0;
                BeepOff(pAdapter->mvSataAdapter.adapterIoBaseAddress);
-               unlock_driver(oldspl);
        }
+       mtx_unlock(&pAdapter->lock);
 #endif
 
        hpt_copy_info(pinfo, "Controller #%d:\n\n", 
pAdapter->mvSataAdapter.adapterId);
@@ -610,16 +631,16 @@ out:
 
 #define xhptregister_node(name) hptregister_node(name)
 
-#if (__FreeBSD_version < 500043)
+#if __FreeBSD_version >= 1100024
 #define hptregister_node(name) \
-       SYSCTL_NODE(, OID_AUTO, name, CTLFLAG_RW, 0, "Get/Set " #name " state 
root node") \
+       SYSCTL_ROOT_NODE(OID_AUTO, name, CTLFLAG_RW, 0, "Get/Set " #name " 
state root node"); \
        SYSCTL_OID(_ ## name, OID_AUTO, status, CTLTYPE_STRING|CTLFLAG_RW, \
        NULL, 0, hpt_status, "A", "Get/Set " #name " state")
-#else 
+#else
 #define hptregister_node(name) \
-       SYSCTL_ROOT_NODE(OID_AUTO, name, CTLFLAG_RW, 0, "Get/Set " #name " 
state root node"); \
+       SYSCTL_NODE(, OID_AUTO, name, CTLFLAG_RW, 0, "Get/Set " #name " state 
root node"); \
        SYSCTL_OID(_ ## name, OID_AUTO, status, CTLTYPE_STRING|CTLFLAG_RW, \
-       NULL, 0, hpt_status, "A", "Get/Set " #name " state");
+       NULL, 0, hpt_status, "A", "Get/Set " #name " state")
 #endif
        
 xhptregister_node(PROC_DIR_NAME);

Modified: head/sys/dev/hptmv/ioctl.c
==============================================================================
--- head/sys/dev/hptmv/ioctl.c  Tue Aug  5 23:55:23 2014        (r269616)
+++ head/sys/dev/hptmv/ioctl.c  Tue Aug  5 23:58:49 2014        (r269617)
@@ -33,13 +33,6 @@
 #include <sys/kernel.h>
 #include <sys/malloc.h>
 
-#if (__FreeBSD_version < 500000)
-#include <sys/proc.h>
-#include <sys/kthread.h>
-#include <sys/wait.h>
-#include <sys/sysproto.h>
-#endif
-
 #ifndef __KERNEL__
 #define __KERNEL__
 #endif
@@ -66,7 +59,7 @@ static int event_queue_head=0, event_que
 
 static int hpt_get_event(PHPT_EVENT pEvent);
 static int hpt_set_array_state(DEVICEID idArray, DWORD state);
-static intrmask_t lock_driver_idle(IAL_ADAPTER_T *pAdapter);
+static void lock_driver_idle(IAL_ADAPTER_T *pAdapter);
 static void HPTLIBAPI thread_io_done(_VBUS_ARG PCommand pCmd);
 static int HPTLIBAPI R1ControlSgl(_VBUS_ARG PCommand pCmd,
     FPSCAT_GATH pSgTable, int logical);
@@ -158,6 +151,7 @@ ioctl_ReportEvent(UCHAR event, PVOID par
                get_disk_location(&((PVDevice)param)->u.disk, &controller, 
&channel);
                hpt_printk(("Device removed: controller %d channel %d\n", 
controller, channel));
        }
+       wakeup(param);
 }
 
 static int
@@ -195,30 +189,17 @@ hpt_delete_array(_VBUS_ARG DEVICEID id, 
 /* just to prevent driver from sending more commands */
 static void HPTLIBAPI nothing(_VBUS_ARG void *notused){}
 
-intrmask_t
+void
 lock_driver_idle(IAL_ADAPTER_T *pAdapter)
 {
-       intrmask_t oldspl;
        _VBUS_INST(&pAdapter->VBus)
-       oldspl = lock_driver();
+       mtx_lock(&pAdapter->lock);
        while (pAdapter->outstandingCommands) {
                KdPrint(("outstandingCommands is %d, wait..\n", 
pAdapter->outstandingCommands));
                if (!mWaitingForIdle(_VBUS_P0)) CallWhenIdle(_VBUS_P nothing, 
0);
-               unlock_driver(oldspl);
-/*Schedule out*/
-#if (__FreeBSD_version < 500000)
-               YIELD_THREAD;
-#else 
-#if (__FreeBSD_version > 700033)
-               pause("switch", 1);
-#else
-               tsleep(lock_driver_idle, PPAUSE, "switch", 1);
-#endif
-#endif
-               oldspl = lock_driver();
+               mtx_sleep(pAdapter, &pAdapter->lock, 0, "hptidle", 0);
        }
        CheckIdleCall(_VBUS_P0);
-       return oldspl;
 }
 
 int Kernel_DeviceIoControl(_VBUS_ARG
@@ -329,9 +310,7 @@ int Kernel_DeviceIoControl(_VBUS_ARG
                default:
                {
                        PVDevice pVDev;
-#ifdef SUPPORT_ARRAY
-                       intrmask_t oldspl;
-#endif
+
                        switch(dwIoControlCode) {
                        /* read-only ioctl functions can be called directly. */
                        case HPT_IOCTL_GET_VERSION:
@@ -382,13 +361,13 @@ int Kernel_DeviceIoControl(_VBUS_ARG
                                         * create_array, and other functions 
can't be executed while channel is 
                                         * perform I/O commands. Wait until 
driver is idle.
                                         */
-                                       oldspl = lock_driver_idle(pAdapter);
+                                       lock_driver_idle(pAdapter);
                                        if (hpt_default_ioctl(_VBUS_P 
dwIoControlCode, lpInBuffer, nInBufferSize, 
                                                lpOutBuffer, nOutBufferSize, 
lpBytesReturned) == -1) {
-                                               unlock_driver(oldspl);
+                                               mtx_unlock(&pAdapter->lock);
                                                return -1;
                                        }
-                                       unlock_driver(oldspl);
+                                       mtx_unlock(&pAdapter->lock);
                                }
                                else
                                        return -1;
@@ -401,7 +380,7 @@ int Kernel_DeviceIoControl(_VBUS_ARG
                                case HPT_IOCTL_CREATE_ARRAY:
                                {
                                        pAdapter=(IAL_ADAPTER_T 
*)(ID_TO_VDEV(*(DEVICEID *)lpOutBuffer))->pVBus->OsExt;
-                                       oldspl = lock_driver();
+                                       mtx_lock(&pAdapter->lock);
                     if(((PCREATE_ARRAY_PARAMS)lpInBuffer)->CreateFlags & 
CAF_CREATE_AND_DUPLICATE)
                                    {
                                                  (ID_TO_VDEV(*(DEVICEID 
*)lpOutBuffer))->u.array.rf_auto_rebuild = 0;
@@ -415,7 +394,7 @@ int Kernel_DeviceIoControl(_VBUS_ARG
                                    {
                           hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, 
pAdapter, ID_TO_VDEV(*(DEVICEID *)lpOutBuffer), REBUILD_PARITY);
                                        }
-                                       unlock_driver(oldspl);
+                                       mtx_unlock(&pAdapter->lock);
                     break;
                                }
 
@@ -423,7 +402,7 @@ int Kernel_DeviceIoControl(_VBUS_ARG
                                case HPT_IOCTL_CREATE_ARRAY_V2:
                                {
                                        pAdapter=(IAL_ADAPTER_T 
*)(ID_TO_VDEV(*(DEVICEID *)lpOutBuffer))->pVBus->OsExt;
-                                       oldspl = lock_driver();
+                                       mtx_lock(&pAdapter->lock);
                                             
if(((PCREATE_ARRAY_PARAMS_V2)lpInBuffer)->CreateFlags & 
CAF_CREATE_AND_DUPLICATE) {
                                                  (ID_TO_VDEV(*(DEVICEID 
*)lpOutBuffer))->u.array.rf_auto_rebuild = 0;
                                                          
hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, ID_TO_VDEV(*(DEVICEID 
*)lpOutBuffer), DUPLICATE);
@@ -432,7 +411,7 @@ int Kernel_DeviceIoControl(_VBUS_ARG
                                        } else 
if(((PCREATE_ARRAY_PARAMS_V2)lpInBuffer)->CreateFlags & 
CAF_CREATE_R5_BUILD_PARITY) {
                                                          
hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, ID_TO_VDEV(*(DEVICEID 
*)lpOutBuffer), REBUILD_PARITY);
                                        }
-                                       unlock_driver(oldspl);

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to