[IBAL] Fix ND connection establishment races.
authorleonidk <leonidk@ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86>
Sun, 5 Apr 2009 15:24:20 +0000 (15:24 +0000)
committerleonidk <leonidk@ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86>
Sun, 5 Apr 2009 15:24:20 +0000 (15:24 +0000)
This patch fixes issues with connection establishment for NetworkDirect.  The root cause of the issue is 'too many cooks' - CIDs exposed to user-mode should not be destroyed in the kernel code without explicit request from the user.  Otherwise, the CID can get recycled in the kernel for the same process and improperly freed when the stale CID is released by the application (multiple connection objects in the app have the same CID.)

Unfortunately, the fix is not simple.  The QP references the CEP, so QP destruction frees the CEP, even if there's a reference to that CEP left in the application.  Removing the CEP reference form the QP solves this problem, but deadlocks the app if it destroys the QP before the CEP, since the QP is used to queue connection-related IRPs, and the CEP uses the QP as its context and so holds a reference on it.

This patch does the following:
- Remove CEP reference for ND related QP.
- Remove ND connection related IRP queue from QP.
- Remove ND IRP handling from CEP manager.
- Add a function to CEP manager to reference the context associated with a CEP if the context is non-NULL.
- Move ND connection related IRP management into al_ndi_cm.c, in nd_csq_t structure.

As part of testing, I needed to add NotifyDisconnect functionality, so this is also included in the patch.

Note that the patch depends on Sean's previous patch to change kal_cep_destroy to allow silently dropping a REQ.  I did not remove Sean's previous changes from this patch, so they are duplicated here.  This allows the patch to be applied and build.

-Fab

Signed-off-by: Fab Tillier <ftillier@microsoft.com>
git-svn-id: svn://openib.tc.cornell.edu/gen1/trunk@2079 ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86

12 files changed:
core/al/al_cm_cep.h
core/al/al_dev.h
core/al/al_qp.c
core/al/al_qp.h
core/al/kernel/al_cm.c
core/al/kernel/al_cm_cep.c
core/al/kernel/al_ndi_cm.c
core/al/kernel/al_ndi_cm.h
core/al/kernel/al_proxy_cep.c
core/al/kernel/al_proxy_ndi.c
core/al/user/ual_cm_cep.c
inc/iba/ib_al_ioctl.h

index a7011d4..07a838b 100644 (file)
@@ -116,7 +116,7 @@ kal_cep_alloc(
        IN                              ib_al_handle_t                          h_al,\r
        IN      OUT                     net32_t* const                          p_cid );\r
 \r
-void\r
+ib_api_status_t\r
 kal_cep_config(\r
        IN                              ib_al_handle_t                          h_al,\r
        IN                              net32_t                                         cid,\r
@@ -184,22 +184,11 @@ kal_cep_pre_rep(
        IN                              uint8_t                                         rnr_nak_timeout,\r
        IN      OUT                     ib_qp_mod_t* const                      p_init OPTIONAL );\r
 \r
-ib_api_status_t\r
-kal_cep_config_pre_rep_copy_cid(\r
-       IN                              ib_al_handle_t                          h_al,\r
-       IN                              net32_t                                         cid,\r
-       IN                              al_pfn_cep_cb_t                         pfn_cb,\r
-       IN                              void*                                           context,\r
-       IN                              ib_pfn_destroy_cb_t                     pfn_destroy_cb,\r
-       IN              const   iba_cm_rep* const                       p_cm_rep,\r
-       IN                              uint8_t                                         rnr_nak_timeout,\r
-       IN      OUT                     net32_t* const                          p_cid,\r
-               OUT                     ib_qp_mod_t* const                      p_init );\r
-\r
 void\r
 kal_cep_destroy(\r
        IN                              ib_al_handle_t                          h_al,\r
-       IN                              net32_t                                         cid );\r
+       IN                              net32_t                                         cid,\r
+       IN                              NTSTATUS                                        status );\r
 #endif\r
 \r
 ib_api_status_t\r
@@ -336,13 +325,6 @@ al_cep_queue_irp(
        IN                              net32_t                                         cid,\r
        IN                              IRP* const                                      p_irp );\r
 \r
-NTSTATUS\r
-al_cep_get_cid(\r
-       IN              ib_al_handle_t                                          h_al,\r
-       IN              net32_t                 const                           cid,\r
-       IN              PIRP                                                            h_ioctl\r
-       );\r
-\r
 NTSTATUS\r
 al_cep_get_pdata(\r
        IN                              ib_al_handle_t                          h_al,\r
@@ -352,6 +334,12 @@ al_cep_get_pdata(
        IN      OUT                     uint8_t                                         *p_psize,\r
                OUT                     uint8_t*                                        pdata );\r
 \r
+void*\r
+kal_cep_get_context(\r
+       IN                              ib_al_handle_t                          h_al,\r
+       IN                              net32_t                                         cid,\r
+       IN                              al_pfn_cep_cb_t                         pfn_cb,\r
+       IN                              ib_pfn_destroy_cb_t                     pfn_addref );\r
 #endif /* CL_KERNEL */\r
 \r
 \r
index 212dada..6d26e34 100644 (file)
@@ -55,7 +55,7 @@
 #define AL_DEVICE_NAME L"\\Device\\ibal"\r
 #define        ALDEV_KEY               (0x3B)  /* Matches FILE_DEVICE_INFINIBAND from wdm.h */\r
 \r
-#define AL_IOCTL_VERSION                       (11)\r
+#define AL_IOCTL_VERSION                       (12)\r
 \r
 /* max number of devices with non-default pkey */\r
 #define        MAX_NUM_PKEY    16\r
@@ -353,7 +353,6 @@ typedef enum _ual_cep_ops
        ual_cep_get_timewait,\r
        ual_cep_get_event,\r
        ual_cep_poll,\r
-       ual_cep_get_req_cid,\r
        ual_cep_get_pdata,\r
 \r
        al_cep_maxops\r
@@ -408,6 +407,9 @@ typedef enum _al_ndi_ops
        ual_ndi_dreq_cm_ioctl_cmd,\r
     ual_ndi_noop,\r
     ual_ndi_notify_dreq_cmd,\r
+       ual_ndi_cancel_cm_irps,\r
+       ual_ndi_listen_cm_cmd,\r
+       ual_ndi_get_req_cm_cmd,\r
 \r
        al_ndi_maxops\r
 \r
@@ -440,6 +442,9 @@ typedef enum _al_ioc_device_config
 #define UAL_NDI_DREQ_CM                        IOCTL_CODE(ALDEV_KEY, ual_ndi_dreq_cm_ioctl_cmd)\r
 #define UAL_NDI_NOOP            IOCTL_CODE(ALDEV_KEY, ual_ndi_noop)\r
 #define UAL_NDI_NOTIFY_DREQ     IOCTL_CODE(ALDEV_KEY, ual_ndi_notify_dreq_cmd)\r
+#define UAL_NDI_CANCEL_CM_IRPS IOCTL_CODE(ALDEV_KEY, ual_ndi_cancel_cm_irps)\r
+#define UAL_NDI_LISTEN_CM              IOCTL_CODE(ALDEV_KEY, ual_ndi_listen_cm_cmd)\r
+#define UAL_NDI_GET_REQ_CM             IOCTL_CODE(ALDEV_KEY, ual_ndi_get_req_cm_cmd)\r
 \r
 /*\r
  * Various Operation Allowable on the System Helper\r
@@ -541,7 +546,6 @@ typedef enum _al_ioc_device_config
 #define UAL_CEP_GET_TIMEWAIT   IOCTL_CODE(ALDEV_KEY, ual_cep_get_timewait)\r
 #define UAL_CEP_GET_EVENT      IOCTL_CODE(ALDEV_KEY, ual_cep_get_event)\r
 #define UAL_CEP_POLL           IOCTL_CODE(ALDEV_KEY, ual_cep_poll)\r
-#define UAL_CEP_GET_REQ_CID    IOCTL_CODE(ALDEV_KEY, ual_cep_get_req_cid)\r
 #define UAL_CEP_GET_PDATA      IOCTL_CODE(ALDEV_KEY, ual_cep_get_pdata)\r
 \r
 \r
index 05c99d5..a331301 100644 (file)
@@ -327,10 +327,6 @@ create_qp(
                        break;\r
                }\r
                status = init_conn_qp( (al_conn_qp_t*)h_qp, h_pd, p_qp_create, p_umv_buf );\r
-#ifdef CL_KERNEL\r
-               if( status == IB_SUCCESS && !NT_SUCCESS( ndi_qp_init(h_qp) ) )\r
-                       status = IB_ERROR;\r
-#endif\r
                break;\r
 \r
        case IB_QPT_UNRELIABLE_DGRM:\r
@@ -1117,9 +1113,6 @@ destroying_qp(
        case IB_QPT_RELIABLE_CONN:\r
        case IB_QPT_UNRELIABLE_CONN:\r
                al_destroy_cep( h_qp->obj.h_al, &((al_conn_qp_t*)h_qp)->cid, FALSE );\r
-#ifdef CL_KERNEL\r
-               ndi_qp_destroy( h_qp );\r
-#endif\r
 \r
                /* Fall through. */\r
        case IB_QPT_UNRELIABLE_DGRM:\r
@@ -1237,10 +1230,6 @@ free_qp(
        CL_ASSERT( p_obj );\r
        h_qp = PARENT_STRUCT( p_obj, ib_qp_t, obj );\r
 \r
-#ifdef CL_KERNEL\r
-       ndi_qp_free( h_qp );\r
-#endif\r
-\r
        destroy_al_obj( p_obj );\r
        cl_free( h_qp );\r
 }\r
index b884784..f2deaf4 100644 (file)
@@ -135,10 +135,6 @@ typedef struct _ib_qp
                IN              const   ib_qp_handle_t                          h_qp,\r
                IN              const   ib_mcast_req_t* const           p_mcast_req );\r
 \r
-#ifdef CL_KERNEL\r
-       ndi_qp_csq_t                            *p_irp_queue;\r
-#endif\r
-\r
 }      ib_qp_t;\r
 \r
 \r
index 3142462..48b0cb5 100644 (file)
@@ -90,7 +90,7 @@ cm_cep_handler(const ib_al_handle_t h_al, const net32_t cid)
 \r
                        id = cm_alloc_id(listen_id->callback, listen_id);\r
                        if (id == NULL) {\r
-                               kal_cep_destroy(h_al, new_cid);\r
+                               kal_cep_destroy(h_al, new_cid, STATUS_NO_MORE_ENTRIES);\r
                                ib_put_mad(mad);\r
                                continue;\r
                        }\r
@@ -103,7 +103,7 @@ cm_cep_handler(const ib_al_handle_t h_al, const net32_t cid)
                status = id->callback(id, &event);\r
                if (!NT_SUCCESS(status)) {\r
                        kal_cep_config(h_al, new_cid, NULL, NULL, NULL);\r
-                       kal_cep_destroy(h_al, id->cid);\r
+                       kal_cep_destroy(h_al, id->cid, status);\r
                        cm_free_id(id);\r
                }\r
                ib_put_mad(mad);\r
@@ -139,7 +139,7 @@ cm_destroy_id(iba_cm_id *p_id)
        iba_cm_id_priv  *id;\r
 \r
        id = CONTAINING_RECORD(p_id, iba_cm_id_priv, id);\r
-       kal_cep_destroy(gh_al, p_id->cid);\r
+       kal_cep_destroy(gh_al, p_id->cid, STATUS_SUCCESS);\r
        KeWaitForSingleObject(&id->destroy_event, Executive, KernelMode, FALSE, NULL);\r
        cm_free_id(p_id);\r
 }\r
index 5b3071a..5541033 100644 (file)
@@ -370,9 +370,6 @@ typedef struct _al_kcep
         * NDI stuff - TODO: manage above core kernel CM code\r
         */\r
 \r
-       /* IRP list head */\r
-       LIST_ENTRY                                      irp_que;\r
-\r
        /* private data of REQ, REP, REJ CM requests */ \r
        uint8_t                                         psize;\r
        uint8_t                                         pdata[IB_REP_PDATA_SIZE];\r
@@ -3382,24 +3379,6 @@ create_cep_mgr(
 * CEP manager API\r
 ******************************************************************************/\r
 \r
-static inline void \r
-__complete_ndi_irp(\r
-       IN                              PIRP                                            p_irp,\r
-       IN                              ib_mad_element_t*                       p_mad )\r
-{\r
-       net32_t* p_new_cid = (net32_t*)cl_ioctl_out_buf( p_irp );\r
-       kcep_t* p_cep = (kcep_t*)p_mad->send_context1;\r
-\r
-       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_CM,\r
-               ("Completing al_cep_get_cid IRP with CID = %d\n", p_cep->cid) );\r
-\r
-       *p_new_cid = p_cep->cid;\r
-       p_irp->IoStatus.Information = sizeof(uint32_t);\r
-       p_irp->IoStatus.Status = STATUS_SUCCESS;\r
-       IoCompleteRequest( p_irp, IO_NETWORK_INCREMENT );\r
-       ib_put_mad( p_mad );\r
-}\r
-\r
 \r
 /* Called with the CEP and CEP manager locks held */\r
 static ib_api_status_t\r
@@ -3422,8 +3401,6 @@ __cep_queue_mad(
        if ( p_cep->state == CEP_STATE_LISTEN &&\r
                (p_cep->sid & ~0x0ffffffI64) == IB_REQ_CM_RDMA_SID_PREFIX )\r
        { /* Try to complete pending IRP, if any */\r
-               PIRP p_irp;\r
-               PLIST_ENTRY p_list_entry;\r
                mad_cm_req_t* p_req = (mad_cm_req_t*)ib_get_mad_buf( p_mad );\r
                ib_cm_rdma_req_t *p_rdma_req = (ib_cm_rdma_req_t *)p_req->pdata;\r
 \r
@@ -3437,24 +3414,6 @@ __cep_queue_mad(
                                p_rdma_req->maj_min_ver, p_rdma_req->ipv ) );\r
                        return IB_UNSUPPORTED;\r
                }\r
-               \r
-               /* get a pending IRP */\r
-               if ( !IsListEmpty( &p_cep->irp_que ) )\r
-               {\r
-                       // get IRP\r
-                       p_list_entry = RemoveHeadList( &p_cep->irp_que );\r
-                       p_irp = (PIRP)CONTAINING_RECORD( p_list_entry, IRP, Tail.Overlay.ListEntry );\r
-\r
-                       // complete GetConnectionReq IRP\r
-#pragma warning(push, 3)\r
-                       IoSetCancelRoutine( p_irp, NULL );\r
-#pragma warning(pop)\r
-\r
-                       __complete_ndi_irp( p_irp, p_mad );\r
-\r
-                       AL_EXIT( AL_DBG_CM );\r
-                       return IB_PENDING;\r
-               }\r
        }\r
 \r
        /* Queue this MAD for processing. */\r
@@ -3628,8 +3587,6 @@ __create_cep()
 \r
        p_cep->p_cid->p_cep = p_cep;\r
 \r
-       InitializeListHead( &p_cep->irp_que );\r
-\r
        ref_al_obj( &gp_cep_mgr->obj );\r
 \r
        AL_EXIT( AL_DBG_CM );\r
@@ -3682,7 +3639,7 @@ kal_cep_alloc(
        return IB_SUCCESS;\r
 }\r
 \r
-void\r
+ib_api_status_t\r
 kal_cep_config(\r
        IN                              ib_al_handle_t                          h_al,\r
        IN                              net32_t                                         cid,\r
@@ -3695,13 +3652,18 @@ kal_cep_config(
 \r
        KeAcquireInStackQueuedSpinLock( &gp_cep_mgr->lock, &hdl );\r
        p_cep = __lookup_cep( h_al, cid );\r
-       CL_ASSERT( p_cep );\r
+       if( p_cep == NULL )\r
+       {\r
+               KeReleaseInStackQueuedSpinLock( &hdl );\r
+               return IB_INVALID_HANDLE;\r
+       }\r
 \r
        p_cep->pfn_cb = pfn_cb;\r
        p_cep->context = context;\r
        p_cep->pfn_destroy_cb = pfn_destroy_cb;\r
 \r
        KeReleaseInStackQueuedSpinLock( &hdl );\r
+       return IB_SUCCESS;\r
 }\r
 \r
 static inline void\r
@@ -4128,7 +4090,7 @@ al_create_cep(
        status = kal_cep_alloc(h_al, p_cid);\r
        if ( status == IB_SUCCESS )\r
        {\r
-               kal_cep_config(h_al, *p_cid, pfn_cb, context, pfn_destroy_cb);\r
+               status = kal_cep_config(h_al, *p_cid, pfn_cb, context, pfn_destroy_cb);\r
        }\r
 \r
        AL_EXIT( AL_DBG_CM );\r
@@ -4174,28 +4136,6 @@ al_destroy_cep(
        context = p_cep->context;\r
        pfn_destroy_cb = p_cep->pfn_destroy_cb;\r
 \r
-       /* Cancel any queued IRP */\r
-       __cep_complete_irp( p_cep, STATUS_CANCELLED, IO_NO_INCREMENT );\r
-\r
-    while( !IsListEmpty( &p_cep->irp_que ) )\r
-    {\r
-        LIST_ENTRY* p_list_entry;\r
-        IRP* p_irp;\r
-\r
-        // get IRP\r
-        p_list_entry = RemoveHeadList( &p_cep->irp_que );\r
-        p_irp = (PIRP)CONTAINING_RECORD( p_list_entry, IRP, Tail.Overlay.ListEntry );\r
-\r
-        // complete GetConnectionReq IRP\r
-#pragma warning(push, 3)\r
-        IoSetCancelRoutine( p_irp, NULL );\r
-#pragma warning(pop)\r
-\r
-        p_irp->IoStatus.Information = 0;\r
-        p_irp->IoStatus.Status = STATUS_CANCELLED;\r
-        IoCompleteRequest( p_irp, IO_NETWORK_INCREMENT );\r
-    }\r
-\r
        __unbind_cep( p_cep );\r
        ref_cnt = __cleanup_cep( p_cep );\r
     if( reusable )\r
@@ -4218,9 +4158,35 @@ al_destroy_cep(
 void\r
 kal_cep_destroy(\r
        IN                              ib_al_handle_t                          h_al,\r
-       IN                              net32_t                                         cid )\r
+       IN                              net32_t                                         cid,\r
+       IN                              NTSTATUS                                        status )\r
 {\r
-       al_destroy_cep(h_al, &cid, FALSE);\r
+       KLOCK_QUEUE_HANDLE      hdl;\r
+       kcep_t                          *p_cep;\r
+    ib_pfn_destroy_cb_t pfn_destroy_cb;\r
+       void                            *context;\r
+       int32_t                         ref_cnt;\r
+\r
+       KeAcquireInStackQueuedSpinLock( &gp_cep_mgr->lock, &hdl );\r
+       p_cep = __lookup_cep( h_al, cid );\r
+       CL_ASSERT( p_cep );\r
+\r
+       context = p_cep->context;\r
+       pfn_destroy_cb = p_cep->pfn_destroy_cb;\r
+\r
+       __unbind_cep( p_cep );\r
+       /* Drop new REQs so they can be retried when resources may be available */\r
+       if( status == STATUS_NO_MORE_ENTRIES &&\r
+               (p_cep->state == CEP_STATE_REQ_RCVD ||\r
+                p_cep->state == CEP_STATE_REQ_MRA_SENT) )\r
+       {\r
+               p_cep->state = CEP_STATE_IDLE;\r
+       }\r
+       ref_cnt = __cleanup_cep( p_cep );\r
+       KeReleaseInStackQueuedSpinLock( &hdl );\r
+\r
+       if( !ref_cnt && pfn_destroy_cb )\r
+               pfn_destroy_cb( context );\r
 }\r
 \r
 \r
@@ -5077,55 +5043,6 @@ out:
 }\r
 \r
 \r
-ib_api_status_t\r
-kal_cep_config_pre_rep_copy_cid(\r
-       IN                              ib_al_handle_t                          h_al,\r
-       IN                              net32_t                                         cid,\r
-       IN                              al_pfn_cep_cb_t                         pfn_cb,\r
-       IN                              void*                                           context,\r
-       IN                              ib_pfn_destroy_cb_t                     pfn_destroy_cb,\r
-       IN              const   iba_cm_rep* const                       p_cm_rep,\r
-       IN                              uint8_t                                         rnr_nak_timeout,\r
-       IN      OUT                     net32_t* const                          p_cid,\r
-               OUT                     ib_qp_mod_t* const                      p_init )\r
-{\r
-       kcep_t                          *p_cep;\r
-       KLOCK_QUEUE_HANDLE      hdl;\r
-       ib_api_status_t         status;\r
-\r
-       CL_ASSERT( h_al );\r
-       CL_ASSERT( p_cm_rep );\r
-       CL_ASSERT( p_init );\r
-\r
-       KeAcquireInStackQueuedSpinLock( &gp_cep_mgr->lock, &hdl );\r
-       if (*p_cid != AL_INVALID_CID)\r
-       {\r
-               status = IB_RESOURCE_BUSY;\r
-               goto out;\r
-       }\r
-\r
-       p_cep = __lookup_cep( h_al, cid );\r
-       if (!p_cep )\r
-       {\r
-               status = IB_INVALID_HANDLE;\r
-               goto out;\r
-       }\r
-\r
-       status = __al_cep_pre_rep( p_cep, p_cm_rep, rnr_nak_timeout, p_init );\r
-       if ( status == IB_SUCCESS )\r
-       {\r
-               p_cep->pfn_cb = pfn_cb;\r
-               p_cep->context = context;\r
-               p_cep->pfn_destroy_cb = pfn_destroy_cb;\r
-               *p_cid = cid;\r
-       }\r
-\r
-out:\r
-       KeReleaseInStackQueuedSpinLock( &hdl );\r
-       return status;\r
-}\r
-\r
-\r
 ib_api_status_t\r
 kal_cep_pre_rep(\r
        IN                              ib_al_handle_t                          h_al,\r
@@ -6678,60 +6595,6 @@ __cep_cancel_ndi_irp(
        AL_EXIT( AL_DBG_CM );\r
 }\r
 \r
-NTSTATUS\r
-al_cep_get_cid(\r
-       IN              ib_al_handle_t                                          h_al,\r
-       IN              net32_t                 const                           cid,\r
-       IN              PIRP                                                            p_irp\r
-       )\r
-{\r
-       kcep_t *p_cep;\r
-       NTSTATUS nt_status;\r
-       KLOCK_QUEUE_HANDLE hdl;\r
-       ib_mad_element_t* p_mad = NULL;\r
-\r
-       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_CM, ("[ CID = %d\n", cid) );\r
-       KeAcquireInStackQueuedSpinLock( &gp_cep_mgr->lock, &hdl );\r
-\r
-       p_cep = __lookup_cep( h_al, cid );\r
-       if( !p_cep )\r
-       {\r
-               p_irp->IoStatus.Information = 0;\r
-               p_irp->IoStatus.Status = STATUS_INVALID_PARAMETER;\r
-               IoCompleteRequest( p_irp, IO_NETWORK_INCREMENT );\r
-               nt_status = STATUS_EVENT_DONE;\r
-               goto exit;\r
-       }\r
-\r
-       if( !p_cep->p_mad_head )\r
-       { /* no pending MADs - queue the IRP */ \r
-               p_cep->signalled = FALSE;\r
-               InsertTailList( &p_cep->irp_que, &p_irp->Tail.Overlay.ListEntry );\r
-               p_irp->Tail.Overlay.DriverContext[0] = (void*)(size_t)cid;\r
-               p_irp->Tail.Overlay.DriverContext[1] = (void*)h_al;\r
-#pragma warning(push, 3)\r
-               IoSetCancelRoutine( p_irp, __cep_cancel_ndi_irp );\r
-#pragma warning(pop)\r
-               IoMarkIrpPending( p_irp );\r
-               nt_status = STATUS_PENDING;\r
-               goto exit;\r
-       }\r
-\r
-       /* Set the MAD. */\r
-       p_mad = p_cep->p_mad_head;\r
-       p_cep->p_mad_head = p_mad->p_next;\r
-       p_mad->p_next = NULL;\r
-\r
-       /* complete the IRP */\r
-       __complete_ndi_irp( p_irp, p_mad );\r
-       nt_status = STATUS_EVENT_DONE;\r
-       \r
-exit:\r
-       KeReleaseInStackQueuedSpinLock( &hdl );\r
-       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_CM, ("] returned %08x\n", nt_status) );\r
-       return nt_status;\r
-}\r
-\r
 \r
 NTSTATUS\r
 al_cep_get_pdata(\r
@@ -6779,3 +6642,57 @@ al_cep_get_pdata(
        AL_EXIT( AL_DBG_CM );\r
        return STATUS_SUCCESS;\r
 }\r
+\r
+\r
+/*\r
+ * This function is designed to support moving the NetorkDirect IRP queue to the CEP\r
+ * without performing major surgery on the CEP manager.\r
+ *\r
+ * It retrieves the context associated with a CEP, using the pfn_addref function\r
+ * to prevent the context from being destroyed after it is returned.\r
+ *\r
+ * It returns NULL if there is no context, requiring contexts to be pointers.\r
+ */\r
+void*\r
+kal_cep_get_context(\r
+       IN                              ib_al_handle_t                          h_al,\r
+       IN                              net32_t                                         cid,\r
+       IN                              al_pfn_cep_cb_t                         pfn_cb,\r
+       IN                              ib_pfn_destroy_cb_t                     pfn_addref )\r
+{\r
+       kcep_t                          *p_cep;\r
+       void*                           context = NULL;\r
+       KLOCK_QUEUE_HANDLE      hdl;\r
+\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_CM, ("[ CID = %d\n", cid) );\r
+\r
+       CL_ASSERT( h_al );\r
+       CL_ASSERT( pfn_addref );\r
+\r
+       KeAcquireInStackQueuedSpinLock( &gp_cep_mgr->lock, &hdl );\r
+       p_cep = __lookup_cep( h_al, cid );\r
+       if( !p_cep )\r
+       {\r
+               AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR, \r
+                       ("CEP not found for cid %d, h_al %p\n", cid, h_al ));\r
+               goto out;\r
+       }\r
+\r
+       if( p_cep->pfn_cb != pfn_cb )\r
+       {\r
+               AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
+                       ("CEP callback mismatch for cid %d, h_al %p\n", cid, h_al ));\r
+               goto out;\r
+       }\r
+\r
+       context = p_cep->context;\r
+       if( context != NULL )\r
+       {\r
+               pfn_addref( context );\r
+       }\r
+\r
+out:\r
+       KeReleaseInStackQueuedSpinLock( &hdl );\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_CM, ("] returning %p\n", context) );\r
+       return context;\r
+}\r
index 4638e07..8f3c95c 100644 (file)
@@ -81,7 +81,7 @@ __ndi_send_req(
 \r
 NTSTATUS\r
 __ndi_send_rep(\r
-       IN              ib_qp_handle_t                                          h_qp,\r
+       IN              nd_csq_t                                                        *p_csq,\r
        IN              PIRP                                                            p_irp\r
        );\r
 \r
@@ -90,11 +90,18 @@ __ndi_send_dreq(
        IN              IRP*                                                            p_irp\r
        );\r
 \r
+NTSTATUS\r
+__ndi_get_req(\r
+       IN              nd_csq_t                                                        *p_csq,\r
+       IN              IRP*                                                            p_irp\r
+       );\r
+\r
 static void\r
 __ndi_queue_drep(\r
        IN                              IRP                                                     *p_irp\r
        );\r
 \r
+\r
 /*******************************************************************\r
  *\r
  * Helpers\r
@@ -123,12 +130,12 @@ static char * State2String(ndi_cm_state_t state)
 \r
 static inline void\r
 __ndi_complete_irp(\r
-       IN      ib_qp_handle_t                                                  h_qp,\r
+       IN      nd_csq_t*                                                               p_csq,\r
        IN      PIRP                                                                    p_irp,\r
        IN      NTSTATUS                                                                status\r
        )\r
 {\r
-       AL_ENTER( AL_DBG_NDI );\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI, ("[ CID = %d\n", p_csq->cid) );\r
 \r
        CL_ASSERT( p_irp );\r
        CL_ASSERT( p_irp->Tail.Overlay.DriverContext[1] == NULL );\r
@@ -144,7 +151,7 @@ __ndi_complete_irp(
                p_irp->IoStatus.Information = 0;\r
                IoCompleteRequest( p_irp, 0 );\r
        }\r
-       deref_al_obj( &h_qp->obj ); /* Release IRP reference */\r
+       nd_csq_release( p_csq ); /* Release IRP reference */\r
 \r
        AL_EXIT( AL_DBG_NDI );\r
 }\r
@@ -159,23 +166,28 @@ __ndi_complete_irp(
  */\r
 static void\r
 __cep_timewait_qp(\r
-       IN              const   ib_qp_handle_t                          h_qp )\r
+       IN                              nd_csq_t                                        *p_csq )\r
 {\r
        uint64_t                        timewait = 0;\r
+       ib_qp_handle_t          h_qp;\r
        ib_qp_mod_t                     qp_mod;\r
        ib_api_status_t         status;\r
 \r
        AL_ENTER( AL_DBG_CM );\r
 \r
-       CL_ASSERT( h_qp );\r
+       CL_ASSERT( p_csq != NULL );\r
 \r
        /*\r
         * The CM should have set the proper timewait time-out value.  Reset\r
         * the QP and let it enter the timewait state.\r
         */\r
-       if( al_cep_get_timewait( h_qp->obj.h_al,\r
-               ((al_conn_qp_t*)h_qp)->cid, &timewait ) == IB_SUCCESS )\r
+       if( al_cep_get_timewait( p_csq->h_al, p_csq->cid, &timewait ) == IB_SUCCESS )\r
        {\r
+               h_qp = CONTAINING_RECORD(\r
+                       al_hdl_ref( p_csq->h_al, p_csq->h_qp, AL_OBJ_TYPE_H_QP ),\r
+                       ib_qp_t,\r
+                       obj );\r
+\r
                /* Special checks on the QP state for error handling - see above. */\r
                if( !h_qp || !AL_OBJ_IS_TYPE( h_qp, AL_OBJ_TYPE_H_QP ) ||\r
                        ( (h_qp->obj.state != CL_INITIALIZED) && \r
@@ -192,16 +204,16 @@ __cep_timewait_qp(
                status = h_qp->pfn_modify_qp( h_qp, &qp_mod, NULL );\r
                if( status != IB_SUCCESS )\r
                {\r
-                       AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
+                       AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
                                ("pfn_modify_qp to IB_QPS_ERROR returned %s\n",\r
                                ib_get_err_str( status )) );\r
-                       return;\r
                }\r
-\r
-#ifdef CL_KERNEL\r
-               /* Store the timestamp after which the QP exits timewait. */\r
-               h_qp->timewait = cl_get_time_stamp() + timewait;\r
-#endif /* CL_KERNEL */\r
+               else\r
+               {\r
+                       /* Store the timestamp after which the QP exits timewait. */\r
+                       h_qp->timewait = cl_get_time_stamp() + timewait;\r
+               }\r
+               deref_al_obj( &h_qp->obj );\r
        }\r
 \r
        AL_EXIT( AL_DBG_CM );\r
@@ -209,22 +221,39 @@ __cep_timewait_qp(
 \r
 static ib_api_status_t\r
 __ndi_qp2rts(\r
-       IN              ib_qp_handle_t  const                           h_qp,\r
+       IN              nd_csq_t*                                                       p_csq,\r
        IN              PIRP                                                            p_irp\r
        )\r
 {\r
        ib_api_status_t status;\r
+       ib_qp_handle_t h_qp;\r
        ib_qp_mod_t qp_mod;\r
 \r
-       AL_ENTER( AL_DBG_NDI );\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI,\r
+               ("[ CID = %d\n", p_csq->cid) );\r
+\r
+       h_qp = CONTAINING_RECORD(\r
+               al_hdl_ref( p_csq->h_al, p_csq->h_qp, AL_OBJ_TYPE_H_QP ),\r
+               ib_qp_t,\r
+               obj );\r
+       if( h_qp == NULL )\r
+       {\r
+               AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
+                       ("Bad QP %I64d\n", p_csq->h_qp) );\r
+               status = IB_INVALID_HANDLE;\r
+               goto err;\r
+       }\r
+\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI,\r
+               ("QP %p state %d\n", h_qp, h_qp->state) );\r
 \r
        /* fill required qp attributes */\r
-       status = al_cep_get_rtr_attr( qp_get_al( h_qp ), \r
-               ((al_conn_qp_t*)h_qp)->cid, &qp_mod );\r
+       status = al_cep_get_rtr_attr( p_csq->h_al, p_csq->cid, &qp_mod );\r
        if ( status != IB_SUCCESS )\r
        {\r
                AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
-                       ("al_cep_get_rtr_attr returned %s\n", ib_get_err_str( status )) );\r
+                       ("al_cep_get_rtr_attr for CID %d returned %s\n",\r
+                       p_csq->cid, ib_get_err_str( status )) );\r
                goto exit;\r
        }\r
 \r
@@ -234,17 +263,18 @@ __ndi_qp2rts(
        if ( status != IB_SUCCESS )\r
        {\r
                AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
-                       ("ndi_modify_qp to RTR returned %s.\n", ib_get_err_str(status) ) );\r
+                       ("ndi_modify_qp %p from %d to RTR returned %s.\n",\r
+                       h_qp, h_qp->state, ib_get_err_str(status) ) );\r
                goto exit;\r
        }\r
 \r
        /* fill required qp attributes */\r
-       status = al_cep_get_rts_attr( qp_get_al( h_qp ), \r
-               ((al_conn_qp_t*)h_qp)->cid, &qp_mod );\r
+       status = al_cep_get_rts_attr( p_csq->h_al, p_csq->cid, &qp_mod );\r
        if ( status != IB_SUCCESS )\r
        {\r
                AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
-                       ("al_cep_get_rts_attr returned %s\n", ib_get_err_str( status )) );\r
+                       ("al_cep_get_rts_attr for CID %d returned %s\n",\r
+                       p_csq->cid, ib_get_err_str( status )) );\r
                goto exit;\r
        }\r
 \r
@@ -254,10 +284,13 @@ __ndi_qp2rts(
        if ( status != IB_SUCCESS )\r
        {\r
                AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
-                       ("ndi_modify_qp to RTS returned %s.\n", ib_get_err_str(status) ) );\r
+                       ("ndi_modify_qp %p from %d to RTS returned %s.\n",\r
+                       h_qp, h_qp->state, ib_get_err_str(status) ) );\r
        }\r
 \r
 exit:\r
+       deref_al_obj( &h_qp->obj );\r
+err:\r
        AL_EXIT( AL_DBG_NDI );\r
        return status;\r
 }\r
@@ -277,11 +310,15 @@ static NTSTATUS __ndi_insert_irp_ex(
        )\r
 {\r
        NTSTATUS status;\r
-       ndi_qp_csq_t *p_ndi_csq = (ndi_qp_csq_t*)pCsq;\r
+       nd_csq_t *p_ndi_csq = CONTAINING_RECORD( pCsq, nd_csq_t, csq );\r
 \r
-       AL_ENTER( AL_DBG_NDI );\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI, ("[ CID = %d\n", p_ndi_csq->cid) );\r
        switch( (ULONG_PTR)Context )\r
        {\r
+       case NDI_CM_LISTEN:\r
+               status = __ndi_get_req( p_ndi_csq, pIrp );\r
+               break;\r
+\r
        case NDI_CM_CONNECTING_QPR_SENT:\r
                status = __ndi_pr_query( pIrp );\r
                break;\r
@@ -290,24 +327,38 @@ static NTSTATUS __ndi_insert_irp_ex(
                status = __ndi_send_req( pIrp );\r
                break;\r
 \r
-       case NDI_CM_CONNECTING_REP_SENT:\r
-               status = __ndi_send_rep( p_ndi_csq->h_qp, pIrp );\r
-               break;\r
-\r
        case NDI_CM_DISCONNECTING:\r
                status = __ndi_send_dreq( pIrp );\r
                break;\r
 \r
+       case NDI_CM_CONNECTED_DREQ_RCVD:\r
+               if( p_ndi_csq->state == NDI_CM_LISTEN )\r
+               {\r
+                       AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_NDI,\r
+                               ("] Invalid state (%d).\n", p_ndi_csq->state) );\r
+                       return STATUS_INVALID_DEVICE_REQUEST;\r
+               }\r
+\r
+               /*\r
+                * Overwrite the context so that the state change\r
+                * below turns into a noop.\r
+                */\r
+               Context = (VOID*)(ULONG_PTR)p_ndi_csq->state;\r
+               status = STATUS_PENDING;\r
+               break;\r
+\r
        default:\r
                status = STATUS_INVALID_DEVICE_REQUEST;\r
                ASSERT( FALSE );\r
        }\r
 \r
-       if( status == STATUS_SUCCESS )\r
+       ASSERT( status == STATUS_PENDING || !NT_SUCCESS( status ) );\r
+       if( NT_SUCCESS( status ) )\r
        {\r
+               AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI, ("] Queueing IRP\n") );\r
                p_ndi_csq->state = (ndi_cm_state_t)(ULONG_PTR)Context;\r
                InsertTailList( &p_ndi_csq->queue, &pIrp->Tail.Overlay.ListEntry );\r
-               ref_al_obj( &p_ndi_csq->h_qp->obj ); /* Take IRP reference. */\r
+               nd_csq_ref( p_ndi_csq ); /* Take IRP reference. */\r
        }\r
        AL_EXIT( AL_DBG_NDI );\r
        return status;\r
@@ -334,7 +385,7 @@ static PIRP __ndi_peek_next_irp(
        PIRP nextIrp = NULL;\r
        PLIST_ENTRY nextEntry;\r
        PLIST_ENTRY listHead;\r
-       ndi_qp_csq_t *p_ndi_csq = (ndi_qp_csq_t*)Csq;\r
+       nd_csq_t *p_ndi_csq = (nd_csq_t*)Csq;\r
 \r
        AL_ENTER( AL_DBG_NDI );\r
 \r
@@ -380,16 +431,12 @@ static PIRP __ndi_peek_next_irp(
 \r
 static VOID __ndi_acquire_lock(\r
        IN      PIO_CSQ                                                                 Csq,\r
-       OUT     PKIRQL                                                                  Irql\r
+       OUT     PKIRQL                                                                  pIrql\r
        )\r
 {\r
-       ndi_qp_csq_t *p_ndi_csq = (ndi_qp_csq_t*)Csq;\r
-       ib_qp_handle_t h_qp = p_ndi_csq->h_qp;\r
-       UNUSED_PARAM( Irql );\r
+       nd_csq_t *p_ndi_csq = (nd_csq_t*)Csq;\r
 \r
-       AL_ENTER( AL_DBG_NDI );\r
-       cl_spinlock_acquire( &h_qp->obj.lock );\r
-       AL_EXIT( AL_DBG_NDI );\r
+       KeAcquireSpinLock( &p_ndi_csq->lock, pIrql );\r
 }\r
 \r
 static VOID __ndi_release_lock(\r
@@ -397,13 +444,9 @@ static VOID __ndi_release_lock(
        IN      KIRQL                                                                   Irql\r
        )\r
 {\r
-       ndi_qp_csq_t *p_ndi_csq = (ndi_qp_csq_t*)Csq;\r
-       ib_qp_handle_t h_qp = p_ndi_csq->h_qp;\r
-       UNUSED_PARAM( Irql );\r
+       nd_csq_t *p_ndi_csq = (nd_csq_t*)Csq;\r
 \r
-       AL_ENTER( AL_DBG_NDI );\r
-       cl_spinlock_release( &h_qp->obj.lock );\r
-       AL_EXIT( AL_DBG_NDI );\r
+       KeReleaseSpinLock( &p_ndi_csq->lock, Irql );\r
 }\r
 \r
 static VOID __ndi_complete_cancelled_irp(\r
@@ -411,8 +454,7 @@ static VOID __ndi_complete_cancelled_irp(
        IN      PIRP                                                                    p_irp\r
        )\r
 {\r
-       ndi_qp_csq_t *p_ndi_csq = (ndi_qp_csq_t*)Csq;\r
-       ib_qp_handle_t h_qp = p_ndi_csq->h_qp;\r
+       nd_csq_t *p_ndi_csq = (nd_csq_t*)Csq;\r
        KIRQL irql;\r
        ib_query_handle_t h_query;\r
 \r
@@ -427,14 +469,11 @@ static VOID __ndi_complete_cancelled_irp(
                 * SA callback by the CSQ lock.\r
                 */\r
 #pragma warning( disable:4305 )\r
-               h_query = InterlockedExchangePointer( &h_qp->p_irp_queue->h_query, NULL );\r
+               h_query = InterlockedExchangePointer( &p_ndi_csq->h_query, NULL );\r
 #pragma warning( default:4305 )\r
                if( h_query != NULL )\r
                        al_cancel_sa_req( &h_query->sa_req );\r
 \r
-               /* Always try to destroy the CEP.  The CEP manager handles invalid CIDs. */\r
-               al_destroy_cep( qp_get_al( h_qp ), &((al_conn_qp_t*)h_qp)->cid, TRUE );\r
-\r
                if( p_ndi_csq->state != NDI_CM_INVALID )\r
                        p_ndi_csq->state = NDI_CM_IDLE;\r
 \r
@@ -443,7 +482,8 @@ static VOID __ndi_complete_cancelled_irp(
                __fallthrough;\r
 \r
        case UAL_NDI_NOTIFY_DREQ:\r
-               __ndi_complete_irp( h_qp, p_irp, STATUS_CANCELLED );\r
+       case UAL_NDI_GET_REQ_CM:\r
+               __ndi_complete_irp( p_ndi_csq, p_irp, STATUS_CANCELLED );\r
                break;\r
 \r
        case UAL_NDI_DREQ_CM:\r
@@ -457,138 +497,158 @@ static VOID __ndi_complete_cancelled_irp(
 \r
 \r
 NTSTATUS\r
-ndi_qp_init(\r
-       IN                              ib_qp_handle_t                          h_qp )\r
+nd_csq_init(\r
+       IN                              ib_al_handle_t                          h_al,\r
+       IN                              net32_t                                         cid,\r
+       IN                              uint64_t                                        h_qp,\r
+               OUT                     nd_csq_t                                        **pp_csq )\r
 {\r
-\r
+       nd_csq_t *p_nd_csq;\r
        NTSTATUS status;\r
+       ib_api_status_t ib_status;\r
 \r
        AL_ENTER( AL_DBG_NDI );\r
 \r
-       if ( h_qp->type != IB_QPT_RELIABLE_CONN )\r
-       {\r
-               AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-                       ("Only RC QP type is supported \n"));\r
-               status = STATUS_SUCCESS;\r
-               goto exit;\r
-       }\r
-       \r
-       h_qp->p_irp_queue = (ndi_qp_csq_t*)cl_zalloc(sizeof(ndi_qp_csq_t));\r
-       if (!h_qp->p_irp_queue)\r
+       p_nd_csq = (nd_csq_t*)cl_zalloc( sizeof(*p_nd_csq) );\r
+       if( p_nd_csq == NULL )\r
        {\r
                status = STATUS_NO_MEMORY;\r
                goto exit;\r
        }\r
 \r
-       status = IoCsqInitializeEx( &h_qp->p_irp_queue->csq, \r
+       KeInitializeSpinLock( &p_nd_csq->lock );\r
+       InitializeListHead( &p_nd_csq->queue );\r
+       p_nd_csq->h_al = h_al;\r
+       p_nd_csq->h_qp = h_qp;\r
+       p_nd_csq->h_query = NULL;\r
+       p_nd_csq->state = NDI_CM_IDLE;\r
+       p_nd_csq->cid = cid;\r
+\r
+       status = IoCsqInitializeEx( &p_nd_csq->csq, \r
                __ndi_insert_irp_ex, __ndi_remove_irp,\r
                __ndi_peek_next_irp, __ndi_acquire_lock,\r
                __ndi_release_lock, __ndi_complete_cancelled_irp );\r
        if ( !NT_SUCCESS( status ) )\r
+       {\r
+               cl_free( p_nd_csq );\r
                goto exit;\r
+       }\r
 \r
-       InitializeListHead( &h_qp->p_irp_queue->queue );\r
-       h_qp->p_irp_queue->h_qp = h_qp;\r
-       h_qp->p_irp_queue->h_query = NULL;\r
-       h_qp->p_irp_queue->state = NDI_CM_IDLE;\r
+       /*\r
+        * One reference for the CEP, one for the caller (so that if the CEP\r
+        * gets destroyed we don't blow up.)\r
+        */\r
+       p_nd_csq->ref_cnt = 2;\r
+\r
+       ib_status = kal_cep_config(\r
+               h_al, cid, nd_cm_handler, p_nd_csq, nd_csq_release );\r
+\r
+       if( ib_status != IB_SUCCESS )\r
+       {\r
+               status = STATUS_UNSUCCESSFUL;\r
+               cl_free( p_nd_csq );\r
+               goto exit;\r
+       }\r
+\r
+       *pp_csq = p_nd_csq;\r
        status = STATUS_SUCCESS;\r
 \r
-AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-       ("Creating h_qp %#I64x, uhdl %#I64x \n", \r
-       (uint64_t)(ULONG_PTR)h_qp, h_qp->obj.hdl ) );\r
+       AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
+               ("Creating CSQ %p, uhdl %#I64x \n", p_nd_csq, h_qp) );\r
 \r
 exit:\r
        AL_EXIT( AL_DBG_NDI );\r
        return status;\r
 }\r
 \r
-#pragma warning(disable:4706)\r
+\r
 void\r
-ndi_qp_destroy(\r
-       IN              ib_qp_handle_t                                  h_qp )\r
+ndi_cancel_cm_irps(\r
+       IN                              nd_csq_t                                        *p_nd_csq )\r
 {\r
-       KIRQL irql;\r
        PIRP Irp;\r
 \r
-       AL_ENTER( AL_DBG_NDI );\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI,\r
+               ("[ CSQ %p (CID = %d)\n", \r
+               p_nd_csq, p_nd_csq->cid ) );\r
 \r
-       if (h_qp->type == IB_QPT_RELIABLE_CONN && h_qp->p_irp_queue)\r
+       /* cancel pending IRPS for NDI type CQ */\r
+       AL_ENTER( AL_DBG_NDI );\r
+       for( Irp = IoCsqRemoveNextIrp( &p_nd_csq->csq, NULL );\r
+               Irp != NULL;\r
+               Irp = IoCsqRemoveNextIrp( &p_nd_csq->csq, NULL ) )\r
        {\r
-               AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-                       ("Destroying h_qp %#I64x, uhdl %#I64x, cid %d\n", \r
-                       (uint64_t)(ULONG_PTR)h_qp, h_qp->obj.hdl, ((al_conn_qp_t*)h_qp)->cid ) );\r
+               __ndi_complete_cancelled_irp( &p_nd_csq->csq, Irp );\r
+       }\r
+       for( Irp = IoCsqRemoveNextIrp( &p_nd_csq->csq, (VOID*)(ULONG_PTR)UAL_NDI_NOTIFY_DREQ );\r
+               Irp != NULL;\r
+               Irp = IoCsqRemoveNextIrp( &p_nd_csq->csq, (VOID*)(ULONG_PTR)UAL_NDI_NOTIFY_DREQ ) )\r
+       {\r
+               __ndi_complete_cancelled_irp( &p_nd_csq->csq, Irp );\r
+       }\r
 \r
-               /* Move the state before flushing, so that all new IRPs fail to queue. */\r
-               __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-               h_qp->p_irp_queue->state = NDI_CM_INVALID;\r
-               __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
+       AL_EXIT( AL_DBG_NDI );\r
+}\r
 \r
-               /* cancel pending IRPS for NDI type CQ */\r
-               AL_ENTER( AL_DBG_NDI );\r
-               while( Irp = IoCsqRemoveNextIrp( &h_qp->p_irp_queue->csq, NULL ) )\r
-               {\r
-                       AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI, \r
-                               ("h_qp %#I64x, uhdl %#I64x, ref_cnt %d\n", \r
-                               (uint64_t)(ULONG_PTR)h_qp, h_qp->obj.hdl, h_qp->obj.ref_cnt ) );\r
 \r
-                       __ndi_complete_cancelled_irp( &h_qp->p_irp_queue->csq, Irp );\r
-               }\r
-               while( Irp = IoCsqRemoveNextIrp(\r
-                       &h_qp->p_irp_queue->csq, (VOID*)(ULONG_PTR)UAL_NDI_NOTIFY_DREQ ) )\r
-               {\r
-                       AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI, \r
-                               ("h_qp %#I64x, uhdl %#I64x, ref_cnt %d\n", \r
-                               (uint64_t)(ULONG_PTR)h_qp, h_qp->obj.hdl, h_qp->obj.ref_cnt ) );\r
+void\r
+nd_csq_destroy(\r
+       IN                              nd_csq_t                                        *p_nd_csq )\r
+{\r
+       KIRQL irql;\r
 \r
-                       __ndi_complete_cancelled_irp( &h_qp->p_irp_queue->csq, Irp );\r
-               }\r
-               AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI, \r
-                       ("h_qp %#I64x, uhdl %#I64x, ref_cnt %d\n", \r
-                       (uint64_t)(ULONG_PTR)h_qp, h_qp->obj.hdl, h_qp->obj.ref_cnt ) );\r
-       }\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI,\r
+               ("[ CSQ %p (CID = %d)\n", \r
+               p_nd_csq, p_nd_csq->cid ) );\r
+\r
+       /* Move the state before flushing, so that all new IRPs fail to queue. */\r
+       __ndi_acquire_lock( &p_nd_csq->csq, &irql );\r
+       p_nd_csq->state = NDI_CM_INVALID;\r
+       __ndi_release_lock( &p_nd_csq->csq, irql );\r
+\r
+       /* cancel pending IRPS */\r
+       ndi_cancel_cm_irps( p_nd_csq );\r
+\r
+       cl_free( p_nd_csq );\r
 \r
        AL_EXIT( AL_DBG_NDI );\r
 }\r
-#pragma warning(default:4706)\r
 \r
 \r
 void\r
-ndi_qp_free(\r
-       IN              ib_qp_handle_t                                  h_qp )\r
+nd_csq_ref( nd_csq_t* p_csq )\r
 {\r
-       AL_ENTER( AL_DBG_NDI );\r
+       InterlockedIncrement( &p_csq->ref_cnt );\r
+}\r
 \r
-       if (h_qp->type == IB_QPT_RELIABLE_CONN && h_qp->p_irp_queue)\r
+\r
+void\r
+nd_csq_release( nd_csq_t* p_csq )\r
+{\r
+       if( InterlockedDecrement( &p_csq->ref_cnt ) == 0 )\r
        {\r
-               /* free NDI context */\r
-               cl_free( h_qp->p_irp_queue );\r
-               h_qp->p_irp_queue = NULL;\r
+               nd_csq_destroy( p_csq );\r
        }\r
-\r
-       AL_EXIT( AL_DBG_NDI );\r
 }\r
 \r
 \r
 static inline void\r
-__ndi_complete_req_irp(\r
-       IN      ib_qp_handle_t                                                  h_qp,\r
-       IN      NTSTATUS                                                                code\r
-       )\r
+__ndi_timeout_req_irp(\r
+       __in nd_csq_t* p_csq )\r
 {\r
        PIRP Irp;\r
        KIRQL irql;\r
 \r
        AL_ENTER( AL_DBG_NDI );\r
-       Irp = IoCsqRemoveNextIrp( &h_qp->p_irp_queue->csq, (VOID*)(ULONG_PTR)UAL_NDI_REQ_CM );\r
-       if ( Irp )\r
+       Irp = IoCsqRemoveNextIrp( &p_csq->csq, (VOID*)(ULONG_PTR)UAL_NDI_REQ_CM );\r
+       if( Irp )\r
        {\r
-               __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-               if( h_qp->p_irp_queue->state != NDI_CM_INVALID )\r
-                       h_qp->p_irp_queue->state = NDI_CM_IDLE;\r
-               __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
-               __ndi_complete_irp( h_qp, Irp, code );\r
+               __ndi_acquire_lock( &p_csq->csq, &irql );\r
+               if( p_csq->state != NDI_CM_INVALID )\r
+                       p_csq->state = NDI_CM_IDLE;\r
+               __ndi_release_lock( &p_csq->csq, irql );\r
+               __ndi_complete_irp( p_csq, Irp, STATUS_TIMEOUT );\r
        }\r
-       al_destroy_cep( qp_get_al( h_qp ), &((al_conn_qp_t*)h_qp)->cid, TRUE );\r
        AL_EXIT( AL_DBG_NDI );\r
 }\r
 \r
@@ -600,38 +660,41 @@ __ndi_complete_req_irp(
 \r
 static void\r
 __ndi_notify_dreq(\r
-       IN                              ib_qp_handle_t const            h_qp )\r
+       __in nd_csq_t* p_csq )\r
 {\r
-       IRP *p_irp = IoCsqRemoveNextIrp(\r
-               &h_qp->p_irp_queue->csq, (VOID*)(ULONG_PTR)UAL_NDI_NOTIFY_DREQ );\r
-\r
-       if( p_irp )\r
+       IRP *p_irp;\r
+       do\r
        {\r
-               __ndi_complete_irp( h_qp, p_irp, STATUS_SUCCESS );\r
-       }\r
+               p_irp = IoCsqRemoveNextIrp(\r
+                       &p_csq->csq, (VOID*)(ULONG_PTR)UAL_NDI_NOTIFY_DREQ );\r
+\r
+               if( p_irp )\r
+               {\r
+                       __ndi_complete_irp( p_csq, p_irp, STATUS_SUCCESS );\r
+               }\r
+\r
+       } while ( p_irp );\r
 }\r
 \r
 \r
 static void\r
 __ndi_proc_dreq(\r
-       IN                              ib_qp_handle_t const            h_qp )\r
+       IN                              nd_csq_t                                        *p_csq)\r
 {\r
        IRP *p_irp;\r
        KIRQL irql;\r
-       ndi_cm_state_t old_state;\r
 \r
-       __ndi_notify_dreq( h_qp );\r
+       __ndi_notify_dreq( p_csq );\r
 \r
-       __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-       old_state = h_qp->p_irp_queue->state;\r
-       if( old_state == NDI_CM_CONNECTED )\r
+       __ndi_acquire_lock( &p_csq->csq, &irql );\r
+       if( p_csq->state == NDI_CM_CONNECTED )\r
        {\r
-               h_qp->p_irp_queue->state = NDI_CM_CONNECTED_DREQ_RCVD;\r
+               p_csq->state = NDI_CM_CONNECTED_DREQ_RCVD;\r
        }\r
-       __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
+       __ndi_release_lock( &p_csq->csq, irql );\r
 \r
        p_irp = IoCsqRemoveNextIrp(\r
-               &h_qp->p_irp_queue->csq, (VOID*)(ULONG_PTR)UAL_NDI_DREQ_CM );\r
+               &p_csq->csq, (VOID*)(ULONG_PTR)UAL_NDI_DREQ_CM );\r
        if( p_irp != NULL )\r
        {\r
                __ndi_queue_drep( p_irp );\r
@@ -645,7 +708,7 @@ __ndi_proc_dreq(
  */\r
 static void\r
 __ndi_proc_rej(\r
-       IN                              ib_qp_handle_t const            h_qp,\r
+       IN                              nd_csq_t*                                       p_csq,\r
        IN              const   mad_cm_rej_t* const                     p_rej )\r
 {\r
        KIRQL irql;\r
@@ -654,73 +717,135 @@ __ndi_proc_rej(
        AL_ENTER( AL_DBG_NDI );\r
 \r
        AL_PRINT(TRACE_LEVEL_ERROR, AL_DBG_ERROR, \r
-               ("p_rej %p, h_qp %#I64x, uhdl %#I64x, connect reject, reason=%hd\n", \r
-               p_rej, (uint64_t)(ULONG_PTR)h_qp, h_qp->obj.hdl, cl_ntoh16(p_rej->reason) ) );\r
+               ("p_rej %p, CID=%d, uhdl %#I64x, connect reject, reason=%hd\n", \r
+               p_rej, p_csq->cid, p_csq->h_qp, cl_ntoh16(p_rej->reason) ) );\r
 \r
-       p_irp = IoCsqRemoveNextIrp( &h_qp->p_irp_queue->csq, NULL );\r
-       __ndi_notify_dreq( h_qp );\r
-       __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
+       p_irp = IoCsqRemoveNextIrp( &p_csq->csq, NULL );\r
+       __ndi_notify_dreq( p_csq );\r
+       __ndi_acquire_lock( &p_csq->csq, &irql );\r
        if( p_irp != NULL )\r
        {\r
                switch( cl_ioctl_ctl_code( p_irp ) )\r
                {\r
                case UAL_NDI_REQ_CM:\r
-                       if( h_qp->p_irp_queue->state != NDI_CM_INVALID )\r
-                               h_qp->p_irp_queue->state = NDI_CM_IDLE;\r
-                       if( p_rej->reason == IB_REJ_TIMEOUT )\r
-                               __ndi_complete_irp( h_qp, p_irp, STATUS_TIMEOUT );\r
+                       if( p_csq->state != NDI_CM_INVALID )\r
+                               p_csq->state = NDI_CM_IDLE;\r
+                       if( p_rej->reason == IB_REJ_USER_DEFINED )\r
+                               __ndi_complete_irp( p_csq, p_irp, STATUS_CONNECTION_REFUSED );\r
                        else\r
-                               __ndi_complete_irp( h_qp, p_irp, STATUS_CONNECTION_REFUSED );\r
+                               __ndi_complete_irp( p_csq, p_irp, STATUS_TIMEOUT );\r
 \r
-                       al_destroy_cep(\r
-                               qp_get_al( h_qp ), &((al_conn_qp_t*)h_qp)->cid, TRUE );\r
+                       /* We leave the CEP active so that the private data can be retrieved. */\r
                        break;\r
 \r
                case UAL_NDI_DREQ_CM:\r
                        __ndi_queue_drep( p_irp );\r
                        break;\r
+\r
+               case UAL_NDI_NOTIFY_DREQ:\r
+                       __ndi_complete_irp( p_csq, p_irp, STATUS_CONNECTION_ABORTED );\r
+                       break;\r
+\r
+               default:\r
+                       ASSERT( cl_ioctl_ctl_code( p_irp ) == UAL_NDI_REQ_CM ||\r
+                               cl_ioctl_ctl_code( p_irp ) == UAL_NDI_DREQ_CM ||\r
+                               cl_ioctl_ctl_code( p_irp ) == UAL_NDI_NOTIFY_DREQ );\r
                }\r
        }\r
-       else if( h_qp->p_irp_queue->state == NDI_CM_CONNECTED )\r
+       else if( p_csq->state == NDI_CM_CONNECTED || p_csq->state == NDI_CM_CONNECTING_REQ_RCVD )\r
        {\r
-               if( h_qp->p_irp_queue->state != NDI_CM_INVALID )\r
-                       h_qp->p_irp_queue->state = NDI_CM_CONNECTED_DREQ_RCVD;\r
+               p_csq->state = NDI_CM_CONNECTED_DREQ_RCVD;\r
        }\r
-       __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
+       __ndi_release_lock( &p_csq->csq, irql );\r
 \r
        AL_EXIT( AL_DBG_NDI );\r
 }\r
 \r
 \r
+static void\r
+__ndi_proc_req(\r
+       IN                              nd_csq_t*                                       p_csq,\r
+       IN                              net32_t                                         new_cid,\r
+       IN                              ib_mad_element_t                        *p_mad )\r
+{\r
+       IRP* p_irp;\r
+       KIRQL irql;\r
+       NTSTATUS status;\r
+       nd_csq_t* p_new_csq;\r
+\r
+       AL_ENTER( AL_DBG_NDI );\r
+\r
+       AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI ,("CID = %d\n", p_csq->cid));\r
+\r
+       status = nd_csq_init( p_csq->h_al, new_cid, 0, &p_new_csq );\r
+       if( status != STATUS_SUCCESS )\r
+       {\r
+               // Ignore the request.\r
+               kal_cep_destroy( p_csq->h_al, new_cid, STATUS_NO_MORE_ENTRIES );\r
+               ib_put_mad( p_mad );\r
+               return;\r
+       }\r
+\r
+       __ndi_acquire_lock( &p_new_csq->csq, &irql );\r
+       p_new_csq->state = NDI_CM_CONNECTING_REQ_RCVD;\r
+       __ndi_release_lock( &p_new_csq->csq, irql );\r
+       nd_csq_release( p_new_csq );\r
+\r
+       p_irp = IoCsqRemoveNextIrp( &p_csq->csq, (VOID*)(ULONG_PTR)UAL_NDI_GET_REQ_CM );\r
+       if( p_irp == NULL )\r
+       {\r
+               p_mad->send_context1 = (VOID*)(ULONG_PTR)new_cid;\r
+               __ndi_acquire_lock( &p_csq->csq, &irql );\r
+               if( p_csq->p_mad_head == NULL )\r
+               {\r
+                       p_csq->p_mad_head = p_mad;\r
+               }\r
+               else\r
+               {\r
+                       p_csq->p_mad_tail->p_next = p_mad;\r
+               }\r
+               p_csq->p_mad_tail = p_mad;\r
+               __ndi_release_lock( &p_csq->csq, irql );\r
+       }\r
+       else\r
+       {\r
+               *(net32_t*)cl_ioctl_out_buf( p_irp ) = new_cid;\r
+               __ndi_complete_irp( p_csq, p_irp, STATUS_SUCCESS );\r
+               ib_put_mad( p_mad );\r
+       }\r
+\r
+       AL_EXIT( AL_DBG_NDI );\r
+       return;\r
+}\r
+\r
+\r
 static void\r
 __ndi_proc_rep(\r
-       IN                              ib_qp_handle_t const            h_qp,\r
-       IN                              net32_t                                         cid )\r
+       IN                              nd_csq_t*                                       p_csq )\r
 {\r
-       ndi_qp_csq_t *p_ndi_csq = h_qp->p_irp_queue;\r
        IRP* p_irp;\r
        KIRQL irql;\r
 \r
        AL_ENTER( AL_DBG_NDI );\r
 \r
-       AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI ,("h_qp = 0x%p\n", h_qp));\r
+       AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI ,("CID = %d\n", p_csq->cid));\r
 \r
-       p_irp = IoCsqRemoveNextIrp( &h_qp->p_irp_queue->csq, (VOID*)(ULONG_PTR)UAL_NDI_REQ_CM );\r
-       __ndi_acquire_lock( &p_ndi_csq->csq, &irql );\r
+       p_irp = IoCsqRemoveNextIrp( &p_csq->csq, (VOID*)(ULONG_PTR)UAL_NDI_REQ_CM );\r
+       __ndi_acquire_lock( &p_csq->csq, &irql );\r
        if( p_irp == NULL )\r
        {\r
                AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR, \r
-                       ("Not the expected state %s\n", State2String( p_ndi_csq->state )));\r
-               CL_ASSERT( IsListEmpty( &h_qp->p_irp_queue->queue ) );\r
-               al_cep_rej( qp_get_al( h_qp ), cid, IB_REJ_INVALID_COMM_INSTANCE, NULL, 0, NULL, 0 );\r
+                       ("Not the expected state %s\n", State2String( p_csq->state )));\r
+               CL_ASSERT( IsListEmpty( &p_csq->queue ) );\r
+               al_cep_rej( p_csq->h_al, p_csq->cid, IB_REJ_INVALID_COMM_INSTANCE, NULL, 0, NULL, 0 );\r
        }\r
        else\r
        {\r
-               p_ndi_csq->state = NDI_CM_CONNECTING_REP_RCVD;\r
+               p_csq->state = NDI_CM_CONNECTING_REP_RCVD;\r
 \r
-               __ndi_complete_irp( h_qp, p_irp, STATUS_SUCCESS );\r
+               __ndi_complete_irp( p_csq, p_irp, STATUS_SUCCESS );\r
        }\r
-       __ndi_release_lock( &p_ndi_csq->csq, irql );\r
+       __ndi_release_lock( &p_csq->csq, irql );\r
 \r
        AL_EXIT( AL_DBG_NDI );\r
        return;\r
@@ -732,7 +857,8 @@ __ndi_do_drep(
        IN                              DEVICE_OBJECT*                          p_dev_obj,\r
        IN                              PIRP                                            p_irp )\r
 {\r
-       ib_qp_handle_t h_qp = p_irp->Tail.Overlay.DriverContext[0];\r
+       nd_csq_t* p_csq = p_irp->Tail.Overlay.DriverContext[0];\r
+       ib_qp_handle_t h_qp;\r
        ib_qp_mod_t qp_mod;\r
        ib_api_status_t status;\r
        uint64_t timewait_us;\r
@@ -746,11 +872,8 @@ __ndi_do_drep(
        CL_ASSERT( p_irp->Tail.Overlay.DriverContext[1] );\r
        IoFreeWorkItem( p_irp->Tail.Overlay.DriverContext[1] );\r
        p_irp->Tail.Overlay.DriverContext[1] = NULL;\r
-       deref_al_obj( &h_qp->obj ); /* Release work item reference. */\r
-\r
-       status = al_cep_get_timewait( qp_get_al( h_qp ), \r
-               ((al_conn_qp_t*)h_qp)->cid, &timewait_us );\r
 \r
+       status = al_cep_get_timewait( p_csq->h_al, p_csq->cid, &timewait_us );\r
        if (status != IB_SUCCESS)\r
        {\r
                nt_status = STATUS_CONNECTION_INVALID;\r
@@ -758,31 +881,44 @@ __ndi_do_drep(
        }\r
 \r
        /* Store the timestamp after which the QP exits timewait. */\r
-       h_qp->timewait = cl_get_time_stamp() + timewait_us;\r
+       h_qp = CONTAINING_RECORD(\r
+               al_hdl_ref( p_csq->h_al, p_csq->h_qp, AL_OBJ_TYPE_H_QP ),\r
+               ib_qp_t,\r
+               obj );\r
+       if( h_qp != NULL )\r
+       {\r
+               h_qp->timewait = cl_get_time_stamp() + timewait_us;\r
+       }\r
 \r
-       al_destroy_cep( qp_get_al( h_qp ), &((al_conn_qp_t*)h_qp)->cid, TRUE );\r
+       /* Send the DREP. */\r
+       al_cep_drep( p_csq->h_al, p_csq->cid, NULL, 0 );\r
 \r
        /* bring QP to error state */\r
-       cl_memclr( &qp_mod, sizeof(qp_mod) );\r
-       qp_mod.req_state = IB_QPS_ERROR;\r
-       \r
-       status = ndi_modify_qp( h_qp, &qp_mod, \r
-               cl_ioctl_out_size( p_irp ), cl_ioctl_out_buf( p_irp ) );\r
-       if ( status != IB_SUCCESS )\r
+       if( h_qp != NULL )\r
        {\r
-               AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
-                       ("ndi_modify_qp to ERROR returned %s.\n", ib_get_err_str(status) ) );\r
+               cl_memclr( &qp_mod, sizeof(qp_mod) );\r
+               qp_mod.req_state = IB_QPS_ERROR;\r
+               \r
+               status = ndi_modify_qp( h_qp, &qp_mod, \r
+                       cl_ioctl_out_size( p_irp ), cl_ioctl_out_buf( p_irp ) );\r
+               if ( status != IB_SUCCESS )\r
+               {\r
+                       AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
+                               ("ndi_modify_qp to ERROR returned %s.\n", ib_get_err_str(status) ) );\r
+               }\r
+               deref_al_obj( &h_qp->obj );\r
        }\r
 \r
        nt_status = ib_to_ntstatus( status );\r
 \r
 exit:\r
-       __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-       if( h_qp->p_irp_queue->state != NDI_CM_INVALID )\r
-               h_qp->p_irp_queue->state = NDI_CM_IDLE;\r
-       __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
+       __ndi_acquire_lock( &p_csq->csq, &irql );\r
+       if( p_csq->state != NDI_CM_INVALID )\r
+               p_csq->state = NDI_CM_IDLE;\r
+       __ndi_release_lock( &p_csq->csq, irql );\r
 \r
-       __ndi_complete_irp( h_qp, p_irp, nt_status );\r
+       __ndi_complete_irp( p_csq, p_irp, nt_status );\r
+       nd_csq_release( p_csq ); /* Release work item reference. */\r
        AL_EXIT( AL_DBG_NDI );\r
 }\r
 \r
@@ -799,16 +935,17 @@ __ndi_queue_drep(
 \r
 static void\r
 __ndi_proc_drep(\r
-       IN                              ib_qp_handle_t const            h_qp )\r
+       IN                              nd_csq_t*                                       p_csq )\r
 {\r
        IRP* p_irp;\r
 \r
        AL_ENTER( AL_DBG_NDI );\r
 \r
        p_irp = IoCsqRemoveNextIrp(\r
-               &h_qp->p_irp_queue->csq, (VOID*)(ULONG_PTR)UAL_NDI_DREQ_CM );\r
+               &p_csq->csq, (VOID*)(ULONG_PTR)UAL_NDI_DREQ_CM );\r
        if( p_irp != NULL )\r
        {\r
+               CL_ASSERT( p_irp->Tail.Overlay.DriverContext[0] == p_csq );\r
                __ndi_queue_drep( p_irp );\r
        }\r
 \r
@@ -816,8 +953,8 @@ __ndi_proc_drep(
 }\r
 \r
 \r
-static void\r
-__ndi_cm_handler(\r
+void\r
+nd_cm_handler(\r
        IN              const   ib_al_handle_t                          h_al,\r
        IN              const   net32_t                                         cid )\r
 {\r
@@ -829,8 +966,11 @@ __ndi_cm_handler(
 \r
        while( al_cep_poll( h_al, cid, &context, &new_cid, &p_mad_el ) == IB_SUCCESS )\r
        {\r
-               ib_mad_t*p_mad = ib_get_mad_buf( p_mad_el );\r
-               ib_qp_handle_t h_qp = (ib_qp_handle_t)context;\r
+               ib_mad_t* p_mad = ib_get_mad_buf( p_mad_el );\r
+               nd_csq_t* p_csq = (nd_csq_t*)context;\r
+\r
+               CL_ASSERT( p_csq != NULL );\r
+               CL_ASSERT( p_csq->cid == cid );\r
 \r
                if( p_mad_el->status != IB_SUCCESS )\r
                {\r
@@ -838,28 +978,28 @@ __ndi_cm_handler(
                        {\r
                        case CM_REQ_ATTR_ID:\r
                                AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-                                       ("REQ timeouted for CEP with cid %d, h_al %p, context %p, new_cid %d .\n", \r
-                                       cid, h_al, h_qp, new_cid ) );\r
-                               __ndi_complete_req_irp( h_qp, STATUS_TIMEOUT );\r
+                                       ("REQ timed out for CEP with cid %d, h_al %p, context %p.\n", \r
+                                       cid, h_al, p_csq ) );\r
+                               __ndi_timeout_req_irp( p_csq );\r
                                break;\r
 \r
                        case CM_REP_ATTR_ID:\r
                                AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-                                       ("REP timeouted for CEP with cid %d, h_al %p, context %p, new_cid %d .\n", \r
-                                       cid, h_al, h_qp, new_cid ) );\r
+                                       ("REP timed out for CEP with cid %d, h_al %p, context %p.\n", \r
+                                       cid, h_al, p_csq ) );\r
                                break;\r
 \r
                        case CM_DREQ_ATTR_ID:\r
                                AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-                                       ("DREQ timeouted for CEP with cid %d, h_al %p, context %p, new_cid %d .\n", \r
-                                       cid, h_al, h_qp, new_cid ) );\r
-                               __ndi_proc_drep( h_qp );\r
+                                       ("DREQ timed out for CEP with cid %d, h_al %p, context %p.\n", \r
+                                       cid, h_al, p_csq ) );\r
+                               __ndi_proc_drep( p_csq );\r
                                break;\r
 \r
                        default:\r
                                AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
                                        ("Unhandled failed MAD attr ID %d for CEP with cid %d, h_al %p, context %p, new_cid %d .\n", \r
-                                       p_mad->attr_id, cid, h_al, h_qp, new_cid ) );\r
+                                       p_mad->attr_id, cid, h_al, p_csq, new_cid ) );\r
                                break;\r
                        }\r
                }\r
@@ -867,50 +1007,51 @@ __ndi_cm_handler(
                {\r
                        switch( p_mad->attr_id )\r
                        {\r
+                       case CM_REQ_ATTR_ID:\r
+                               AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
+                                       ("REQ received for CEP with cid %d, h_al %p, context %p.\n", \r
+                                       cid, h_al, p_csq ) );\r
+                               __ndi_proc_req( p_csq, new_cid, p_mad_el );\r
+                               continue;\r
+\r
                        case CM_REP_ATTR_ID:\r
                                AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-                                       ("REP received for CEP with cid %d, h_al %p, context %p, new_cid %d .\n", \r
-                                       cid, h_al, h_qp, new_cid ) );\r
-                               CL_ASSERT( ((al_conn_qp_t*)h_qp)->cid == (int32_t)cid || \r
-                                       ((al_conn_qp_t*)h_qp)->cid == AL_INVALID_CID ||\r
-                                       ((al_conn_qp_t*)h_qp)->cid == AL_RESERVED_CID );\r
-                               __ndi_proc_rep( h_qp, cid );\r
+                                       ("REP received for CEP with cid %d, h_al %p, context %p.\n", \r
+                                       cid, h_al, p_csq ) );\r
+                               __ndi_proc_rep( p_csq );\r
                                break;\r
                        \r
                        case CM_REJ_ATTR_ID:\r
                                AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-                                       ("REJ received for CEP with cid %d, h_al %p, context %p, new_cid %d .\n", \r
-                                       cid, h_al, h_qp, new_cid ) );\r
-                               __ndi_proc_rej( h_qp, (mad_cm_rej_t*)p_mad );\r
+                                       ("REJ received for CEP with cid %d, h_al %p, context %p.\n", \r
+                                       cid, h_al, p_csq ) );\r
+                               __ndi_proc_rej( p_csq, (mad_cm_rej_t*)p_mad );\r
                                break;\r
                \r
                        case CM_DREQ_ATTR_ID:\r
                                AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-                                       ("DREQ received for CEP with cid %d, h_al %p, context %p, new_cid %d .\n", \r
-                                       cid, h_al, h_qp, new_cid ) );\r
-                               CL_ASSERT( ((al_conn_qp_t*)h_qp)->cid == (int32_t)cid || \r
-                               ((al_conn_qp_t*)h_qp)->cid == AL_INVALID_CID ||\r
-                               ((al_conn_qp_t*)h_qp)->cid == AL_RESERVED_CID );\r
-                               __ndi_proc_dreq( h_qp );\r
+                                       ("DREQ received for CEP with cid %d, h_al %p, context %p.\n", \r
+                                       cid, h_al, p_csq ) );\r
+                               __ndi_proc_dreq( p_csq );\r
                                break;\r
 \r
                        case CM_DREP_ATTR_ID:\r
                                AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-                                       ("DREP received for CEP with cid %d, h_al %p, context %p, new_cid %d .\n", \r
-                                       cid, h_al, h_qp, new_cid ) );\r
-                               __ndi_proc_drep( h_qp );\r
+                                       ("DREP received for CEP with cid %d, h_al %p, context %p.\n", \r
+                                       cid, h_al, p_csq ) );\r
+                               __ndi_proc_drep( p_csq );\r
                                break;\r
 \r
                        case CM_RTU_ATTR_ID:\r
                                AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-                                       ("RTU received for CEP with cid %d, h_al %p, context %p, new_cid %d.\n",\r
-                                       cid, h_al, h_qp, new_cid ) );\r
+                                       ("RTU received for CEP with cid %d, h_al %p, context %p.\n",\r
+                                       cid, h_al, p_csq ) );\r
                                break;\r
 \r
                        default:\r
                                AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
                                        ("Unhandled MAD attr ID %d for CEP with cid %d, h_al %p, context %p, new_cid %d .\n", \r
-                                       p_mad->attr_id, cid, h_al, h_qp, new_cid ) );\r
+                                       p_mad->attr_id, cid, h_al, p_csq, new_cid ) );\r
                        }\r
                }\r
 \r
@@ -922,24 +1063,26 @@ __ndi_cm_handler(
 \r
 static void\r
 __ndi_fill_cm_req(\r
-       IN              ib_qp_handle_t  const                           h_qp,\r
+       IN              net32_t                                                         qpn,\r
        IN              ual_ndi_req_cm_ioctl_in_t                       *p_req,\r
        IN              ib_path_rec_t                                           *p_path_rec,\r
-               OUT     ib_cm_req_t                                                     *p_cm_req)\r
+               OUT     iba_cm_req                                                      *p_cm_req)\r
 {\r
        AL_ENTER( AL_DBG_NDI );\r
 \r
-       memset( p_cm_req, 0, sizeof(ib_cm_req_t) );\r
+       memset( p_cm_req, 0, sizeof(*p_cm_req) );\r
 \r
-       p_cm_req->svc_id = IB_REQ_CM_RDMA_SID_PREFIX | (p_req->prot << 16) | p_req->dst_port;\r
-       p_cm_req->max_cm_retries = g_max_cm_retries;\r
-       p_cm_req->p_primary_path = p_path_rec;  \r
-\r
-       p_cm_req->p_req_pdata = (uint8_t *)&p_req->pdata;\r
-       p_cm_req->req_length = sizeof(p_req->pdata);\r
+       p_cm_req->service_id = IB_REQ_CM_RDMA_SID_PREFIX | (p_req->prot << 16) | p_req->dst_port;\r
+       p_cm_req->p_primary_path = p_path_rec;\r
 \r
+       p_cm_req->qpn = qpn;\r
        p_cm_req->qp_type = IB_QPT_RELIABLE_CONN;\r
-       p_cm_req->h_qp = h_qp;\r
+       p_cm_req->starting_psn = qpn;\r
+\r
+       p_cm_req->p_pdata = (uint8_t *)&p_req->pdata;\r
+       p_cm_req->pdata_len = sizeof(p_req->pdata);\r
+\r
+       p_cm_req->max_cm_retries = g_max_cm_retries;\r
        p_cm_req->resp_res = p_req->resp_res;\r
        p_cm_req->init_depth = p_req->init_depth;\r
 \r
@@ -959,10 +1102,8 @@ __ndi_fill_cm_req(
        else if( p_cm_req->local_resp_timeout < CM_MIN_LOCAL_TIMEOUT )\r
                p_cm_req->local_resp_timeout = CM_MIN_LOCAL_TIMEOUT;\r
 \r
-       p_cm_req->rnr_nak_timeout = QP_ATTRIB_RNR_NAK_TIMEOUT;\r
        p_cm_req->rnr_retry_cnt = QP_ATTRIB_RNR_RETRY;\r
        p_cm_req->retry_cnt = g_qp_retries;\r
-       p_cm_req->p_alt_path = NULL;\r
 \r
        AL_EXIT( AL_DBG_NDI );\r
 }\r
@@ -974,58 +1115,47 @@ __ndi_send_req(
        )\r
 {\r
        ib_api_status_t status;\r
-       ib_qp_handle_t h_qp = (ib_qp_handle_t)p_irp->Tail.Overlay.DriverContext[0];\r
+       nd_csq_t* p_csq = (nd_csq_t*)p_irp->Tail.Overlay.DriverContext[0];\r
        ib_path_rec_t *p_path_rec = p_irp->Tail.Overlay.DriverContext[1];\r
        ual_ndi_req_cm_ioctl_in_t *p_req = \r
                (ual_ndi_req_cm_ioctl_in_t*)cl_ioctl_in_buf( p_irp );\r
        NTSTATUS nt_status;\r
-       ib_cm_req_t cm_req;\r
-       ib_qp_mod_t qp_mod;\r
-       al_conn_qp_t *p_qp;\r
+       ib_qp_handle_t h_qp;\r
+       iba_cm_req cm_req;\r
 \r
-       AL_ENTER( AL_DBG_NDI );\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI,\r
+               ("[ CID = %d, h_al %p, context %p\n",\r
+               p_req->cid, p_csq->h_al, p_csq) );\r
 \r
        p_irp->Tail.Overlay.DriverContext[1] = NULL;\r
 \r
-       if( h_qp->p_irp_queue->state != NDI_CM_CONNECTING_QPR_SENT &&\r
-               h_qp->p_irp_queue->state != NDI_CM_IDLE )\r
+       if( p_csq->state != NDI_CM_CONNECTING_QPR_SENT &&\r
+               p_csq->state != NDI_CM_IDLE )\r
        {\r
                AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
-                       ("Unexpected state: %d\n", h_qp->p_irp_queue->state) );\r
+                       ("Unexpected state: %d\n", p_csq->state) );\r
                return STATUS_CONNECTION_ACTIVE;\r
        }\r
 \r
-       /* Get a CEP and bind it to the QP. */\r
-       p_qp = (al_conn_qp_t*)h_qp;\r
-       cl_spinlock_acquire( &h_qp->obj.lock );\r
-       if( h_qp->obj.state != CL_DESTROYING && p_qp->cid == AL_INVALID_CID )\r
+       h_qp = CONTAINING_RECORD(\r
+               al_hdl_ref( p_csq->h_al, p_req->h_qp, AL_OBJ_TYPE_H_QP ),\r
+               ib_qp_t,\r
+               obj );\r
+       if( !h_qp )\r
        {\r
-               status = al_create_cep( qp_get_al( h_qp ), __ndi_cm_handler,\r
-                                                               &h_qp->obj, deref_al_obj, &p_qp->cid );\r
-       }\r
-       else \r
-       {\r
-               status = IB_RESOURCE_BUSY;\r
-       }\r
-       cl_spinlock_release( &h_qp->obj.lock );\r
-       if( status != IB_SUCCESS )\r
-       {\r
-               h_qp->p_irp_queue->state = NDI_CM_IDLE;\r
+               /* The QP was valid when the IOCTL first came in... */\r
                AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
-                       ("al_create_cep returned %s.\n", ib_get_err_str( status )) );\r
-               return ib_to_ntstatus( status );\r
+                       ("Invalid QP: %I64d\n", p_req->h_qp) );\r
+               return STATUS_CONNECTION_ABORTED;\r
        }\r
-       AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
-               ("Created Active CEP with cid %d, h_al %p, context %p\n",\r
-               p_qp->cid, qp_get_al( h_qp ), h_qp ) );\r
-\r
-       ref_al_obj( &h_qp->obj ); /* Take CEP reference. */\r
 \r
        /* Format ib_cm_req_t structure */\r
-       __ndi_fill_cm_req( h_qp, p_req, p_path_rec, &cm_req );\r
+       __ndi_fill_cm_req( h_qp->num, p_req, p_path_rec, &cm_req );\r
+       deref_al_obj( &h_qp->obj );\r
 \r
        /* prepare CEP for connection */\r
-       status = al_cep_pre_req( qp_get_al( h_qp ), p_qp->cid, &cm_req, &qp_mod );\r
+       status = kal_cep_pre_req(\r
+               p_csq->h_al, p_csq->cid, &cm_req, QP_ATTRIB_RNR_NAK_TIMEOUT, NULL );\r
        if( status != STATUS_SUCCESS )\r
        {\r
                AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
@@ -1034,7 +1164,7 @@ __ndi_send_req(
        }\r
 \r
        /* send CM REQ */\r
-       status = al_cep_send_req( qp_get_al( h_qp ), p_qp->cid );\r
+       status = al_cep_send_req( p_csq->h_al, p_csq->cid );\r
        if( status != IB_SUCCESS )\r
        {\r
                AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
@@ -1044,11 +1174,9 @@ __ndi_send_req(
 \r
        /* SUCCESS ! */\r
        AL_EXIT( AL_DBG_NDI );\r
-       return STATUS_SUCCESS;\r
+       return STATUS_PENDING;\r
 \r
 error:\r
-       al_destroy_cep( qp_get_al( h_qp ), &p_qp->cid, TRUE );\r
-       \r
        switch( status )\r
        {\r
        case IB_INVALID_HANDLE:\r
@@ -1063,7 +1191,7 @@ error:
                nt_status = ib_to_ntstatus( status );\r
        }\r
 \r
-       h_qp->p_irp_queue->state = NDI_CM_IDLE;\r
+       p_csq->state = NDI_CM_IDLE;\r
        AL_EXIT( AL_DBG_NDI );\r
        return nt_status;\r
 }\r
@@ -1076,7 +1204,7 @@ __ndi_pr_query_cb(
        cl_ioctl_handle_t p_irp;\r
        uint8_t pkt_life;\r
        ib_path_rec_t *p_path_rec;\r
-       ib_qp_handle_t h_qp = (ib_qp_handle_t)p_query_rec->query_context;\r
+       nd_csq_t* p_csq = (nd_csq_t*)p_query_rec->query_context;\r
        NTSTATUS status;\r
        KIRQL irql;\r
 \r
@@ -1086,22 +1214,22 @@ __ndi_pr_query_cb(
                ("status is %d, count is %d, context %p\n", p_query_rec->status,\r
                p_query_rec->result_cnt, p_query_rec->query_context) );\r
 \r
-       p_irp = IoCsqRemoveNextIrp( &h_qp->p_irp_queue->csq, (VOID*)(ULONG_PTR)UAL_NDI_REQ_CM );\r
+       p_irp = IoCsqRemoveNextIrp( &p_csq->csq, (VOID*)(ULONG_PTR)UAL_NDI_REQ_CM );\r
        if( p_irp == NULL )\r
        {\r
                goto exit;\r
        }\r
 \r
 #pragma warning( disable:4305 )\r
-       InterlockedExchangePointer( &h_qp->p_irp_queue->h_query, NULL );\r
+       InterlockedExchangePointer( &p_csq->h_query, NULL );\r
 #pragma warning( default:4305 )\r
 \r
        if( p_query_rec->status != IB_SUCCESS || p_query_rec->result_cnt == 0 )\r
        {\r
-               __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-               if( h_qp->p_irp_queue->state != NDI_CM_INVALID )\r
-                       h_qp->p_irp_queue->state = NDI_CM_IDLE;\r
-               __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
+               __ndi_acquire_lock( &p_csq->csq, &irql );\r
+               if( p_csq->state != NDI_CM_INVALID )\r
+                       p_csq->state = NDI_CM_IDLE;\r
+               __ndi_release_lock( &p_csq->csq, irql );\r
                switch( p_query_rec->status )\r
                {\r
                case IB_TIMEOUT:\r
@@ -1129,7 +1257,7 @@ __ndi_pr_query_cb(
                        status = STATUS_HOST_UNREACHABLE;\r
                        break;\r
                }\r
-               __ndi_complete_irp( h_qp, p_irp, status );\r
+               __ndi_complete_irp( p_csq, p_irp, status );\r
                goto exit;\r
        }\r
 \r
@@ -1148,15 +1276,15 @@ __ndi_pr_query_cb(
        p_irp->Tail.Overlay.DriverContext[1] = p_path_rec;\r
 \r
        status = IoCsqInsertIrpEx(\r
-               &h_qp->p_irp_queue->csq,\r
+               &p_csq->csq,\r
                p_irp,\r
                NULL,\r
                (VOID*)(ULONG_PTR)NDI_CM_CONNECTING_REQ_SENT\r
                );\r
-       if( status != STATUS_SUCCESS )\r
+       if( !NT_SUCCESS( status ) )\r
        {\r
                p_irp->Tail.Overlay.DriverContext[1] = NULL;\r
-               __ndi_complete_irp( h_qp, p_irp, status );\r
+               __ndi_complete_irp( p_csq, p_irp, status );\r
        }\r
        else\r
        {\r
@@ -1164,14 +1292,14 @@ __ndi_pr_query_cb(
                 * Release the previous reference because IoCsqInsertIrpEx\r
                 * took a new one.\r
                 */\r
-               deref_al_obj( &h_qp->obj ); /* Release IRP reference. */\r
+               nd_csq_release( p_csq ); /* Release IRP reference. */\r
        }\r
 \r
 exit:\r
        if( p_query_rec->p_result_mad )\r
                ib_put_mad( p_query_rec->p_result_mad );\r
 \r
-       deref_al_obj( &h_qp->obj );     /* release path query reference */\r
+       nd_csq_release( p_csq );        /* release path query reference */\r
        AL_EXIT( AL_DBG_NDI );\r
 }\r
 \r
@@ -1190,16 +1318,16 @@ __ndi_pr_query(
        ib_api_status_t status;\r
        ual_ndi_req_cm_ioctl_in_t *p_req = \r
                (ual_ndi_req_cm_ioctl_in_t*)cl_ioctl_in_buf( p_irp );\r
-       ib_qp_handle_t h_qp = (ib_qp_handle_t)p_irp->Tail.Overlay.DriverContext[0];\r
+       nd_csq_t* p_csq = (nd_csq_t*)p_irp->Tail.Overlay.DriverContext[0];\r
     ib_gid_pair_t gids;\r
 \r
        AL_ENTER( AL_DBG_NDI );\r
 \r
-       if ( h_qp->p_irp_queue->state != NDI_CM_IDLE )\r
+       if ( p_csq->state != NDI_CM_IDLE )\r
        {\r
                AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR, \r
-                       ("STATUS_CONNECTION_ACTIVE: h_qp %#I64x, uhdl %#I64x, ref_cnt %d\n",\r
-                       (uint64_t)(ULONG_PTR)h_qp, h_qp->obj.hdl, h_qp->obj.ref_cnt ) );\r
+                       ("STATUS_CONNECTION_ACTIVE: CID=%d, uhdl %#I64x, ref_cnt %d\n",\r
+                       p_csq->cid, p_csq->h_qp, p_csq->ref_cnt ) );\r
                return STATUS_CONNECTION_ACTIVE;\r
        }\r
 \r
@@ -1212,41 +1340,50 @@ __ndi_pr_query(
        query_req.timeout_ms = g_sa_timeout;\r
        query_req.retry_cnt = g_sa_retries;\r
        query_req.flags = 0;    /* IB_FLAGS_SYNC */\r
-       query_req.query_context = h_qp;\r
+       query_req.query_context = p_csq;\r
        query_req.pfn_query_cb = __ndi_pr_query_cb;\r
 \r
        AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
                ("Query for path from %I64x to %I64x\n",\r
                p_req->guid, ib_gid_get_guid( &p_req->path.dgid )) );\r
 \r
-       ref_al_obj( &h_qp->obj );               /* take path query reference */\r
-       status = ib_query( qp_get_al( h_qp ), &query_req, &h_qp->p_irp_queue->h_query );\r
+       nd_csq_ref( p_csq );            /* take path query reference */\r
+       status = ib_query( p_csq->h_al, &query_req, &p_csq->h_query );\r
        if( status != IB_SUCCESS )\r
        {\r
-               h_qp->p_irp_queue->state = NDI_CM_IDLE;\r
+               p_csq->state = NDI_CM_IDLE;\r
                AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR, ("ib_query failed (%d)\n", status) );\r
-               deref_al_obj( &h_qp->obj );     /* release path query reference */\r
+               nd_csq_release( p_csq );        /* release path query reference */\r
                return ib_to_ntstatus( status );\r
        }\r
 \r
        AL_EXIT( AL_DBG_NDI );\r
-       return STATUS_SUCCESS;\r
+       return STATUS_PENDING;\r
 }\r
 \r
 \r
 NTSTATUS\r
 ndi_req_cm(\r
-       IN              ib_qp_handle_t  const                           h_qp,\r
+       IN              ib_al_handle_t                                          h_al,\r
        IN              IRP                                                                     *p_irp\r
        )\r
 {\r
        NTSTATUS status;\r
+       nd_csq_t* p_csq;\r
        ual_ndi_req_cm_ioctl_in_t *p_req = \r
                (ual_ndi_req_cm_ioctl_in_t*)cl_ioctl_in_buf( p_irp );\r
 \r
        AL_ENTER( AL_DBG_NDI );\r
 \r
-       p_irp->Tail.Overlay.DriverContext[0] = (ib_qp_t*)h_qp;\r
+       p_csq = kal_cep_get_context( h_al, p_req->cid, nd_cm_handler, nd_csq_ref );\r
+       if( p_csq == NULL )\r
+       {\r
+               status = nd_csq_init( h_al, p_req->cid, p_req->h_qp, &p_csq );\r
+               if( status != STATUS_SUCCESS )\r
+                       goto err;\r
+       }\r
+\r
+       p_irp->Tail.Overlay.DriverContext[0] = p_csq;\r
 \r
     if( p_req->path.dlid != 0 )\r
     {\r
@@ -1260,7 +1397,7 @@ ndi_req_cm(
 \r
            p_irp->Tail.Overlay.DriverContext[1] = &p_req->path;\r
            status = IoCsqInsertIrpEx(\r
-                   &h_qp->p_irp_queue->csq,\r
+                   &p_csq->csq,\r
                    p_irp,\r
                    NULL,\r
                    (VOID*)(ULONG_PTR)NDI_CM_CONNECTING_REQ_SENT\r
@@ -1269,15 +1406,15 @@ ndi_req_cm(
     else\r
     {\r
            status = IoCsqInsertIrpEx(\r
-                   &h_qp->p_irp_queue->csq,\r
+                   &p_csq->csq,\r
                    p_irp,\r
                    NULL,\r
                    (VOID*)(ULONG_PTR)NDI_CM_CONNECTING_QPR_SENT\r
                    );\r
     }\r
-       if( status == STATUS_SUCCESS )\r
-               status = STATUS_PENDING;\r
 \r
+       nd_csq_release( p_csq );\r
+err:\r
        AL_EXIT( AL_DBG_NDI );\r
        return status;\r
 }\r
@@ -1295,7 +1432,7 @@ __ndi_do_rtu(
        IN                              PIRP                                            p_irp )\r
 {\r
        ib_api_status_t status;\r
-       ib_qp_handle_t h_qp = p_irp->Tail.Overlay.DriverContext[0];\r
+       nd_csq_t* p_csq = p_irp->Tail.Overlay.DriverContext[0];\r
        KIRQL irql;\r
        NTSTATUS nt_status;\r
 \r
@@ -1308,67 +1445,67 @@ __ndi_do_rtu(
        {\r
                IoFreeWorkItem( p_irp->Tail.Overlay.DriverContext[1] );\r
                p_irp->Tail.Overlay.DriverContext[1] = NULL;\r
-               deref_al_obj( &h_qp->obj ); /* Release work item reference. */\r
+               nd_csq_release( p_csq ); /* Release work item reference. */\r
        }\r
 \r
-       __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-       if( h_qp->p_irp_queue->state != NDI_CM_CONNECTING_REP_RCVD )\r
+       __ndi_acquire_lock( &p_csq->csq, &irql );\r
+       if( p_csq->state != NDI_CM_CONNECTING_REP_RCVD )\r
        {\r
+               __ndi_release_lock( &p_csq->csq, irql );\r
                nt_status = STATUS_CONNECTION_ABORTED;\r
                goto exit;\r
        }\r
-       __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
+       __ndi_release_lock( &p_csq->csq, irql );\r
 \r
        /* change the QP state to RTS */\r
-       status = __ndi_qp2rts( h_qp, p_irp );\r
-\r
+       status = __ndi_qp2rts( p_csq, p_irp );\r
        if ( status != IB_SUCCESS )\r
        {\r
                goto err;\r
        }\r
        \r
        /* send RTU */\r
-       status = al_cep_rtu( qp_get_al( h_qp ), ((al_conn_qp_t*)h_qp)->cid, NULL, 0 );\r
+       status = al_cep_rtu( p_csq->h_al, p_csq->cid, NULL, 0 );\r
        if( status != IB_SUCCESS )\r
        {\r
 err:\r
-               /* Reject and abort the connection. */\r
-               al_cep_rej(\r
-                       qp_get_al( h_qp ), ((al_conn_qp_t*)h_qp)->cid, \r
-                       IB_REJ_INSUF_QP, NULL, 0, NULL, 0 );\r
-\r
-               __cep_timewait_qp( h_qp );\r
+               /*\r
+                * Reject the connection.  Note that we don't free the CEP since the\r
+                * usermode INDConnector object references it, and the CEP will be\r
+                * freed when that object is freed.\r
+                */\r
+               al_cep_rej( p_csq->h_al, p_csq->cid, IB_REJ_INSUF_QP, NULL, 0, NULL, 0 );\r
 \r
-               al_destroy_cep( qp_get_al( h_qp ), &((al_conn_qp_t*)h_qp)->cid, TRUE );\r
+               __cep_timewait_qp( p_csq );\r
 \r
                AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
                        ("al_cep_rtu returned %s.\n", ib_get_err_str( status )) );\r
 \r
-               __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-               if( h_qp->p_irp_queue->state != NDI_CM_INVALID )\r
-                       h_qp->p_irp_queue->state = NDI_CM_IDLE;\r
-               __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
+               __ndi_acquire_lock( &p_csq->csq, &irql );\r
+               if( p_csq->state != NDI_CM_INVALID )\r
+                       p_csq->state = NDI_CM_IDLE;\r
+               __ndi_release_lock( &p_csq->csq, irql );\r
 \r
                nt_status = STATUS_CONNECTION_ABORTED;\r
                goto exit;\r
        }\r
 \r
-       __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-       if( h_qp->p_irp_queue->state == NDI_CM_CONNECTING_REP_RCVD )\r
-               h_qp->p_irp_queue->state = NDI_CM_CONNECTED;\r
-       __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
+       __ndi_acquire_lock( &p_csq->csq, &irql );\r
+       if( p_csq->state == NDI_CM_CONNECTING_REP_RCVD )\r
+               p_csq->state = NDI_CM_CONNECTED;\r
+       __ndi_release_lock( &p_csq->csq, irql );\r
 \r
        nt_status = STATUS_SUCCESS;\r
 \r
 exit:\r
-       __ndi_complete_irp( h_qp, p_irp, nt_status );\r
+       __ndi_complete_irp( p_csq, p_irp, nt_status );\r
        AL_EXIT( AL_DBG_NDI );\r
 }\r
 \r
 \r
 cl_status_t\r
 ndi_rtu_cm(\r
-       IN              ib_qp_handle_t  const                           h_qp,\r
+       IN              nd_csq_t                                                        *p_csq,\r
        IN              PIRP                                                            p_irp\r
        )\r
 {\r
@@ -1376,16 +1513,16 @@ ndi_rtu_cm(
 \r
        AL_ENTER( AL_DBG_NDI );\r
 \r
-       p_irp->Tail.Overlay.DriverContext[0] = h_qp;\r
+       p_irp->Tail.Overlay.DriverContext[0] = p_csq;\r
+       nd_csq_ref( p_csq ); /* Take IRP reference. */\r
        p_io_stack = IoGetCurrentIrpStackLocation( p_irp );\r
        p_irp->Tail.Overlay.DriverContext[1] = IoAllocateWorkItem( p_io_stack->DeviceObject );\r
-       ref_al_obj( &h_qp->obj ); /* Take IRP reference. */\r
 \r
        IoMarkIrpPending( p_irp );\r
        if ( p_irp->Tail.Overlay.DriverContext[1] )\r
        { /* asyncronous performing */\r
                /* take a ref to prevent QP destroy before calling work item */\r
-               ref_al_obj( &h_qp->obj ); /* Take work item reference. */\r
+               nd_csq_ref( p_csq ); /* Take work item reference. */\r
                IoQueueWorkItem( p_irp->Tail.Overlay.DriverContext[1],\r
                        __ndi_do_rtu, DelayedWorkQueue, p_irp );\r
        }\r
@@ -1410,73 +1547,75 @@ __ndi_do_rep(
        IN                              DEVICE_OBJECT*                          p_dev_obj,\r
        IN              PIRP                                                            p_irp )\r
 {\r
-       ib_qp_handle_t h_qp = p_irp->Tail.Overlay.DriverContext[0];\r
+       nd_csq_t* p_csq = p_irp->Tail.Overlay.DriverContext[0];\r
        ib_api_status_t status;\r
-       ual_ndi_rep_cm_ioctl_in_t *p_rep;\r
        KIRQL irql;\r
        NTSTATUS nt_status;\r
 \r
        UNUSED_PARAM(p_dev_obj);\r
 \r
-       AL_ENTER( AL_DBG_NDI );\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI, ("[ CID = %d\n", p_csq->cid) );\r
 \r
        /* free the work item if any */\r
        CL_ASSERT( p_irp->Tail.Overlay.DriverContext[1] != NULL );\r
        IoFreeWorkItem( p_irp->Tail.Overlay.DriverContext[1] );\r
        p_irp->Tail.Overlay.DriverContext[1] = NULL;\r
-       deref_al_obj( &h_qp->obj ); /* Release work item reference. */\r
-\r
-       p_rep = (ual_ndi_rep_cm_ioctl_in_t*)cl_ioctl_in_buf( p_irp );\r
 \r
        /* change the QP state to RTS */\r
-       status = __ndi_qp2rts( h_qp, p_irp );\r
+       status = __ndi_qp2rts( p_csq, p_irp );\r
        if ( status != IB_SUCCESS )\r
        {\r
                goto err;\r
        }\r
        \r
        /* send REP */\r
-       status = al_cep_send_rep ( qp_get_al( h_qp ), p_rep->cid );\r
+       status = al_cep_send_rep ( p_csq->h_al, p_csq->cid );\r
        if( status != IB_SUCCESS )\r
        {\r
                AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
                        ("al_cep_send_rep returned %s\n", ib_get_err_str(status)) );\r
 err:\r
-               /* Reject and abort the connection. */\r
-               al_cep_rej( qp_get_al( h_qp ), p_rep->cid, IB_REJ_INSUF_QP, NULL, 0, NULL, 0 );\r
+               /*\r
+                * Reject the connection.  Note that we don't free the CEP since the\r
+                * usermode INDConnector object references it, and the CEP will be\r
+                * freed when that object is freed.\r
+                */\r
+               al_cep_rej( p_csq->h_al, p_csq->cid, IB_REJ_INSUF_QP, NULL, 0, NULL, 0 );\r
 \r
                /* transit QP to error state */\r
-               __cep_timewait_qp( h_qp );\r
-\r
-               al_destroy_cep( qp_get_al( h_qp ), &((al_conn_qp_t*)h_qp)->cid, TRUE );\r
+               __cep_timewait_qp( p_csq );\r
 \r
                AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
                        ("al_cep_rtu returned %s.\n", ib_get_err_str( status )) );\r
-               __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-               if( h_qp->p_irp_queue->state != NDI_CM_INVALID )\r
-                       h_qp->p_irp_queue->state = NDI_CM_IDLE;\r
-               __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
-               if (status == IB_INVALID_STATE)\r
+               __ndi_acquire_lock( &p_csq->csq, &irql );\r
+               if( p_csq->state != NDI_CM_INVALID )\r
+                       p_csq->state = NDI_CM_IDLE;\r
+               __ndi_release_lock( &p_csq->csq, irql );\r
+               if (status == IB_INVALID_STATE )\r
                        nt_status = STATUS_CONNECTION_ABORTED;\r
+               /* The HCA driver will return IB_INVALID_PARAMETER if the QP is in the wrong state. */\r
+               else if( status == IB_INVALID_HANDLE || status == IB_INVALID_PARAMETER )\r
+                       nt_status = STATUS_CANCELLED;\r
                else\r
-                       nt_status =STATUS_INSUFFICIENT_RESOURCES;\r
+                       nt_status = STATUS_INSUFFICIENT_RESOURCES;\r
                goto exit;\r
        }\r
 \r
-       __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-       if( h_qp->p_irp_queue->state == NDI_CM_CONNECTING_REP_SENT )\r
-               h_qp->p_irp_queue->state = NDI_CM_CONNECTED;\r
-       __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
+       __ndi_acquire_lock( &p_csq->csq, &irql );\r
+       if( p_csq->state == NDI_CM_CONNECTING_REP_SENT )\r
+               p_csq->state = NDI_CM_CONNECTED;\r
+       __ndi_release_lock( &p_csq->csq, irql );\r
        nt_status = STATUS_SUCCESS;\r
 \r
 exit:\r
-       __ndi_complete_irp( h_qp, p_irp, nt_status );\r
+       __ndi_complete_irp( p_csq, p_irp, nt_status );\r
+       nd_csq_release( p_csq ); /* Release work item reference. */\r
        AL_EXIT( AL_DBG_NDI );\r
 }\r
 \r
 static void\r
 __ndi_fill_cm_rep(\r
-       IN              ib_qp_handle_t  const                           h_qp,\r
+       IN              net32_t                                                         qpn,\r
        IN              ual_ndi_rep_cm_ioctl_in_t                       *p_rep,\r
                OUT     iba_cm_rep                                                      *p_cm_rep)\r
 {\r
@@ -1487,7 +1626,7 @@ __ndi_fill_cm_rep(
        p_cm_rep->p_pdata = p_rep->pdata;\r
        p_cm_rep->pdata_len = sizeof(p_rep->pdata);\r
 \r
-       p_cm_rep->qpn = h_qp->num;\r
+       p_cm_rep->qpn = qpn;\r
 \r
        p_cm_rep->init_depth = p_rep->init_depth;\r
        p_cm_rep->failover_accepted = IB_FAILOVER_ACCEPT_UNSUPPORTED;\r
@@ -1500,20 +1639,29 @@ __ndi_fill_cm_rep(
 \r
 NTSTATUS\r
 __ndi_send_rep(\r
-       IN              ib_qp_handle_t                                          h_qp,\r
+       IN              nd_csq_t                                                        *p_csq,\r
        IN              PIRP                                                            p_irp )\r
 {\r
        IO_STACK_LOCATION       *p_io_stack;\r
+       ib_qp_handle_t h_qp;\r
        iba_cm_rep cm_rep;\r
-       ib_qp_mod_t qp_mod;\r
        ib_api_status_t status;\r
        ual_ndi_rep_cm_ioctl_in_t *p_rep = \r
                (ual_ndi_rep_cm_ioctl_in_t*)cl_ioctl_in_buf( p_irp );\r
 \r
-       AL_ENTER( AL_DBG_NDI );\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI,\r
+               ("[ CID = %d\n", p_csq->cid) );\r
 \r
-       if( h_qp->p_irp_queue->state != NDI_CM_IDLE )\r
+       switch( p_csq->state )\r
        {\r
+       case NDI_CM_CONNECTING_REQ_RCVD:\r
+               break;\r
+\r
+       case NDI_CM_CONNECTED_DREQ_RCVD:\r
+               AL_EXIT( AL_DBG_NDI );\r
+               return STATUS_CONNECTION_ABORTED;\r
+\r
+       default:\r
                AL_EXIT( AL_DBG_NDI );\r
                return STATUS_CONNECTION_ACTIVE;\r
        }\r
@@ -1525,26 +1673,36 @@ __ndi_send_rep(
                AL_EXIT( AL_DBG_NDI );\r
                return STATUS_NO_MEMORY;\r
        }\r
-       ref_al_obj( &h_qp->obj ); /* Take work item reference. */\r
+       nd_csq_ref( p_csq ); /* Take work item reference. */\r
 \r
-       /* Format ib_cm_req_t structure */\r
-       __ndi_fill_cm_rep( h_qp, p_rep, &cm_rep );\r
+       h_qp = CONTAINING_RECORD(\r
+               al_hdl_ref( p_csq->h_al, p_csq->h_qp, AL_OBJ_TYPE_H_QP ),\r
+               ib_qp_t,\r
+               obj );\r
+       if( !h_qp )\r
+       {\r
+               /* The QP was valid when the IOCTL first came in... */\r
+               AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
+                       ("Invalid QP: %I64d\n", p_rep->h_qp) );\r
+               status = IB_INVALID_HANDLE;\r
+               goto err;\r
+       }\r
 \r
-       ref_al_obj( &h_qp->obj ); /* Take CEP reference. */\r
+       /* Format ib_cm_req_t structure */\r
+       __ndi_fill_cm_rep( h_qp->num, p_rep, &cm_rep );\r
+       deref_al_obj( &h_qp->obj );\r
 \r
        /* prepare Passive CEP for connection */\r
-       status = kal_cep_config_pre_rep_copy_cid(\r
-               qp_get_al( h_qp ), p_rep->cid, __ndi_cm_handler, &h_qp->obj, deref_al_obj,\r
-               &cm_rep, QP_ATTRIB_RNR_NAK_TIMEOUT, &((al_conn_qp_t*)h_qp)->cid, &qp_mod);\r
+       status = kal_cep_pre_rep(\r
+               p_csq->h_al, p_csq->cid, &cm_rep, QP_ATTRIB_RNR_NAK_TIMEOUT, NULL );\r
        if( status != IB_SUCCESS )\r
        {\r
+               AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
+                       ("kal_cep_pre_rep returned %s.\n", ib_get_err_str( status )) );\r
+err:\r
                IoFreeWorkItem( p_irp->Tail.Overlay.DriverContext[1] );\r
                p_irp->Tail.Overlay.DriverContext[1] = NULL;\r
-               deref_al_obj( &h_qp->obj ); /* Release work item reference. */\r
-               al_destroy_cep( qp_get_al( h_qp ), &p_rep->cid, FALSE );\r
-               deref_al_obj( &h_qp->obj ); /* Release CEP reference. */\r
-               AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
-                       ("kal_cep_config_pre_rep_copy_cid returned %s.\n", ib_get_err_str( status )) );\r
+               nd_csq_release( p_csq ); /* Release work item reference. */\r
                switch (status)\r
                {\r
                        case IB_INVALID_HANDLE:\r
@@ -1560,7 +1718,7 @@ __ndi_send_rep(
 \r
        AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_NDI,\r
                ("Prepared Passive CEP with cid %d, h_al %p, context %p\n",\r
-               p_rep->cid, qp_get_al( h_qp ), h_qp ) );\r
+               p_csq->cid, p_csq->h_al, h_qp ) );\r
 \r
        /*\r
         * transfer work to a worker thread so that QP transitions can be done\r
@@ -1570,39 +1728,52 @@ __ndi_send_rep(
                __ndi_do_rep, DelayedWorkQueue, p_irp );\r
 \r
        AL_EXIT( AL_DBG_NDI );\r
-       return STATUS_SUCCESS;\r
+       return STATUS_PENDING;\r
 }\r
 \r
 \r
 NTSTATUS\r
 ndi_rep_cm(\r
-       IN              ib_qp_handle_t  const                           h_qp,\r
+       IN              ib_al_handle_t                                          h_al,\r
        IN              PIRP                                                            p_irp\r
        )\r
 {\r
        NTSTATUS status;\r
+       nd_csq_t* p_csq;\r
+       ual_ndi_rep_cm_ioctl_in_t *p_rep = \r
+               (ual_ndi_rep_cm_ioctl_in_t*)cl_ioctl_in_buf( p_irp );\r
        KIRQL irql;\r
 \r
        AL_ENTER( AL_DBG_NDI );\r
 \r
-       p_irp->Tail.Overlay.DriverContext[0] = h_qp;\r
+       p_csq = kal_cep_get_context( h_al, p_rep->cid, nd_cm_handler, nd_csq_ref );\r
+       if( p_csq == NULL )\r
+       {\r
+               status = STATUS_CONNECTION_ABORTED;\r
+               goto err;\r
+       }\r
 \r
-       __ndi_acquire_lock( &h_qp->p_irp_queue->csq, &irql );\r
-       status = __ndi_send_rep( h_qp, p_irp );\r
-       if( status == STATUS_SUCCESS )\r
+       p_csq->h_qp = p_rep->h_qp;\r
+\r
+       p_irp->Tail.Overlay.DriverContext[0] = p_csq;\r
+\r
+       __ndi_acquire_lock( &p_csq->csq, &irql );\r
+       status = __ndi_send_rep( p_csq, p_irp );\r
+       if( status == STATUS_PENDING )\r
        {\r
                /*\r
                 * We're going to keep the IRP dangling for a bit - take a reference\r
                 * on the QP until it completes.\r
                 */\r
-               ref_al_obj( &h_qp->obj ); /* Take IRP reference. */\r
-               h_qp->p_irp_queue->state = NDI_CM_CONNECTING_REP_SENT;\r
+               nd_csq_ref( p_csq ); /* Take IRP reference. */\r
+               p_csq->state = NDI_CM_CONNECTING_REP_SENT;\r
                IoMarkIrpPending( p_irp );\r
-               status = STATUS_PENDING;\r
        }\r
-       __ndi_release_lock( &h_qp->p_irp_queue->csq, irql );\r
+       __ndi_release_lock( &p_csq->csq, irql );\r
 \r
-       AL_EXIT( AL_DBG_NDI );\r
+       nd_csq_release( p_csq );\r
+err:\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI, ("] returning %08x\n", status) );\r
        return status;\r
 }\r
 \r
@@ -1620,19 +1791,19 @@ __ndi_send_dreq(
        IN              IRP*                                                            p_irp\r
        )\r
 {\r
-       ib_qp_handle_t h_qp = (ib_qp_handle_t)p_irp->Tail.Overlay.DriverContext[0];\r
+       nd_csq_t *p_csq = (nd_csq_t*)p_irp->Tail.Overlay.DriverContext[0];\r
        IO_STACK_LOCATION       *p_io_stack;\r
        ib_api_status_t status;\r
        NTSTATUS nt_status;\r
 \r
        AL_ENTER( AL_DBG_NDI );\r
 \r
-       if ( h_qp->p_irp_queue->state != NDI_CM_CONNECTED &&\r
-               h_qp->p_irp_queue->state != NDI_CM_CONNECTED_DREQ_RCVD )\r
+       if ( p_csq->state != NDI_CM_CONNECTED &&\r
+               p_csq->state != NDI_CM_CONNECTED_DREQ_RCVD )\r
        {\r
                AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR, \r
-                       ("STATUS_CONNECTION_ACTIVE: h_qp %#I64x, uhdl %#I64x, ref_cnt %d\n",\r
-                       (uint64_t)(ULONG_PTR)h_qp, h_qp->obj.hdl, h_qp->obj.ref_cnt ) );\r
+                       ("STATUS_CONNECTION_ACTIVE: CID = %d, uhdl %#I64x, ref_cnt %d\n",\r
+                       p_csq->cid, p_csq->h_qp, p_csq->ref_cnt ) );\r
                return STATUS_CONNECTION_INVALID;\r
        }\r
 \r
@@ -1648,24 +1819,26 @@ __ndi_send_dreq(
                AL_EXIT( AL_DBG_NDI );\r
                return STATUS_NO_MEMORY;\r
        }\r
-       ref_al_obj( &h_qp->obj ); /* Take work item reference. */\r
+       nd_csq_ref( p_csq ); /* Take work item reference. */\r
 \r
-       status = al_cep_dreq( qp_get_al( h_qp ), ((al_conn_qp_t*)h_qp)->cid, NULL, 0 );\r
+       status = al_cep_dreq( p_csq->h_al, p_csq->cid, NULL, 0 );\r
        switch( status )\r
        {\r
        case IB_INVALID_STATE:\r
-               /* We might have just received a DREQ, so try sending a DREP. */\r
-               __ndi_queue_drep( p_irp );\r
-               IoMarkIrpPending( p_irp );\r
                /*\r
                 * We're going to keep the IRP dangling for a bit - take a reference\r
-                * on the QP until it completes.\r
+                * on the CSQ until it completes.\r
                 */\r
-               ref_al_obj( &h_qp->obj ); /* Take IRP reference. */\r
+               nd_csq_ref( p_csq ); /* Take IRP reference. */\r
+               /* We might have just received a DREQ, so try sending a DREP. */\r
+               IoMarkIrpPending( p_irp );\r
+               __ndi_queue_drep( p_irp );\r
+               AL_EXIT( AL_DBG_NDI );\r
+               return STATUS_INVALID_DEVICE_STATE;\r
 \r
        case IB_SUCCESS:\r
                AL_EXIT( AL_DBG_NDI );\r
-               return( ib_to_ntstatus( status ) );\r
+               return STATUS_PENDING;\r
 \r
        case IB_INVALID_HANDLE:\r
                nt_status = STATUS_CONNECTION_INVALID;\r
@@ -1675,7 +1848,7 @@ __ndi_send_dreq(
        }\r
        IoFreeWorkItem( p_irp->Tail.Overlay.DriverContext[1] );\r
        p_irp->Tail.Overlay.DriverContext[1] = NULL;\r
-       deref_al_obj( &h_qp->obj ); /* Release work item reference. */\r
+       nd_csq_release( p_csq ); /* Release work item reference. */\r
        AL_EXIT( AL_DBG_NDI );\r
        return nt_status;\r
 }\r
@@ -1683,7 +1856,7 @@ __ndi_send_dreq(
 \r
 NTSTATUS\r
 ndi_dreq_cm(\r
-       IN              ib_qp_handle_t  const                           h_qp,\r
+       IN              nd_csq_t*                                                       p_csq,\r
        IN              PIRP                                                            p_irp\r
        )\r
 {\r
@@ -1691,10 +1864,10 @@ ndi_dreq_cm(
 \r
        AL_ENTER( AL_DBG_NDI );\r
 \r
-       p_irp->Tail.Overlay.DriverContext[0] = h_qp;\r
+       p_irp->Tail.Overlay.DriverContext[0] = p_csq;\r
 \r
        status = IoCsqInsertIrpEx(\r
-               &h_qp->p_irp_queue->csq,\r
+               &p_csq->csq,\r
                p_irp,\r
                NULL,\r
                (VOID*)(ULONG_PTR)NDI_CM_DISCONNECTING\r
@@ -1706,10 +1879,136 @@ ndi_dreq_cm(
         * The IRP should never be queued if the work item is queued, so \r
         * we trap the special error code for INVALID_STATE.\r
         */\r
-       if( status == STATUS_SUCCESS || status == STATUS_INVALID_DEVICE_STATE )\r
+       if( status == STATUS_INVALID_DEVICE_STATE )\r
                status = STATUS_PENDING;\r
 \r
        AL_EXIT( AL_DBG_NDI );\r
        return status;\r
 }\r
 \r
+\r
+NTSTATUS\r
+ndi_listen_cm(\r
+       IN              ib_al_handle_t                                  h_al,\r
+       IN              ib_cep_listen_t                                 *p_listen,\r
+               OUT     net32_t                                                 *p_cid,\r
+               OUT     size_t                                                  *p_ret_bytes\r
+       )\r
+{\r
+       NTSTATUS status;\r
+       net32_t cid;\r
+       ib_api_status_t ib_status;\r
+       nd_csq_t *p_csq;\r
+       KIRQL irql;\r
+\r
+       AL_ENTER( AL_DBG_NDI );\r
+\r
+       ib_status = al_create_cep( h_al, NULL, NULL, NULL, &cid );\r
+       if( ib_status != IB_SUCCESS )\r
+       {\r
+               AL_EXIT( AL_DBG_NDI );\r
+               return ib_to_ntstatus( ib_status );\r
+       }\r
+\r
+       status = nd_csq_init( h_al, cid, 0, &p_csq );\r
+       if( status != STATUS_SUCCESS )\r
+       {\r
+               kal_cep_destroy( h_al, cid, STATUS_SUCCESS );\r
+               AL_EXIT( AL_DBG_NDI );\r
+               return status;\r
+       }\r
+\r
+       __ndi_acquire_lock( &p_csq->csq, &irql );\r
+       p_csq->state = NDI_CM_LISTEN;\r
+       __ndi_release_lock( &p_csq->csq, irql );\r
+\r
+       if( (p_listen->svc_id & 0xFFFF) == 0 )\r
+       {\r
+               p_listen->svc_id |= (USHORT)cid | (USHORT)(cid >> 16);\r
+       }\r
+\r
+       ib_status = al_cep_listen( h_al, cid, p_listen );\r
+       if( ib_status == IB_SUCCESS )\r
+       {\r
+               *p_cid = cid;\r
+               *p_ret_bytes = sizeof(*p_cid);\r
+       }\r
+\r
+       nd_csq_release( p_csq );\r
+       status = ib_to_ntstatus( ib_status );\r
+       AL_EXIT( AL_DBG_NDI );\r
+       return status;\r
+}\r
+\r
+\r
+NTSTATUS\r
+__ndi_get_req(\r
+       IN              nd_csq_t                                                        *p_csq,\r
+       IN              IRP*                                                            p_irp\r
+       )\r
+{\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI, ("[ CID = %d\n", p_csq->cid) );\r
+\r
+       if( p_csq->state != NDI_CM_LISTEN )\r
+       {\r
+               AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_NDI,\r
+                       ("] Invalid state (%d).\n", p_csq->state) );\r
+               return STATUS_INVALID_DEVICE_REQUEST;\r
+       }\r
+\r
+       /* Check the MAD list. */\r
+       if( p_csq->p_mad_head != NULL )\r
+       {\r
+               ib_mad_element_t* p_mad = p_csq->p_mad_head;\r
+               net32_t cid = (net32_t)(ULONG_PTR)p_mad->send_context1;\r
+               p_csq->p_mad_head = p_mad->p_next;\r
+               p_mad->p_next = NULL;\r
+\r
+               *(net32_t*)cl_ioctl_out_buf( p_irp ) = cid;\r
+               p_irp->IoStatus.Information = sizeof(net32_t);\r
+               p_irp->IoStatus.Status = STATUS_SUCCESS;\r
+               IoMarkIrpPending( p_irp );\r
+               IoCompleteRequest( p_irp, IO_NETWORK_INCREMENT );\r
+               ib_put_mad( p_mad );\r
+               AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI, ("] Returned new CID = %d\n", cid) );\r
+               return STATUS_INVALID_DEVICE_STATE;\r
+       }\r
+\r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI, ("] Queueing IRP\n") );\r
+       return STATUS_PENDING;\r
+}\r
+\r
+\r
+NTSTATUS\r
+ndi_get_req_cm(\r
+       IN              nd_csq_t                                                *p_csq,\r
+       IN              PIRP                                                    p_irp\r
+       )\r
+{\r
+       NTSTATUS status;\r
+\r
+       AL_ENTER( AL_DBG_NDI );\r
+\r
+       status = IoCsqInsertIrpEx(\r
+               &p_csq->csq,\r
+               p_irp,\r
+               NULL,\r
+               (VOID*)(ULONG_PTR)NDI_CM_LISTEN\r
+               );\r
+\r
+       /*\r
+        * __ndi_get_req will return STATUS_INVALID_DEVICE_STATE to prevent the IRP\r
+        * from being inserted into the CSQ because the IRP was immediately completed.\r
+        * In this case, we need to return STATUS_PENDING.\r
+        */\r
+       if( status == STATUS_INVALID_DEVICE_STATE )\r
+       {\r
+               status = STATUS_PENDING;\r
+       }\r
+\r
+       AL_EXIT( AL_DBG_NDI );\r
+       return status;\r
+}\r
+\r
+\r
+\r
index 029f769..554e397 100644 (file)
 typedef enum _ndi_cm_state \r
 {\r
        NDI_CM_IDLE,\r
+       NDI_CM_LISTEN,\r
        NDI_CM_CONNECTING_QPR_SENT, // QPR = Query path record\r
        NDI_CM_CONNECTING_REQ_SENT,\r
+       NDI_CM_CONNECTING_REQ_RCVD,\r
        NDI_CM_CONNECTING_REP_SENT,\r
        NDI_CM_CONNECTING_REP_RCVD,\r
        NDI_CM_CONNECTED,\r
@@ -77,16 +79,27 @@ typedef enum _ndi_cm_state
 \r
 typedef struct _ib_qp  ib_qp_t;\r
 \r
-typedef struct _ndi_qp_csq\r
+typedef struct _nd_csq\r
 {\r
        IO_CSQ                                          csq;\r
        LIST_ENTRY                                      queue;\r
-       ib_qp_t*                                        h_qp;\r
-       ib_query_handle_t                       h_query;\r
+       ib_al_handle_t                          h_al;\r
+       union {\r
+               uint64_t                                h_qp;\r
+               ib_mad_element_t                *p_mad_head;\r
+       };\r
+       union {\r
+               ib_query_handle_t               h_query;\r
+               ib_mad_element_t                *p_mad_tail;\r
+       };\r
+       net32_t                                         cid;\r
        ndi_cm_state_t                          state;\r
        PIO_WORKITEM                            p_workitem;\r
+       volatile LONG                           ref_cnt;\r
+       KSPIN_LOCK                                      lock;\r
+\r
+} nd_csq_t;\r
 \r
-} ndi_qp_csq_t;\r
 \r
 ib_api_status_t\r
 ndi_modify_qp(\r
@@ -95,41 +108,59 @@ ndi_modify_qp(
        IN              const   uint32_t                                        buf_size,\r
        IN                              uint8_t* const                          p_outbuf);\r
 \r
+void\r
+nd_csq_ref( nd_csq_t* p_csq );\r
+\r
+void\r
+nd_csq_release( nd_csq_t* p_csq );\r
+\r
+void\r
+nd_cm_handler(\r
+       IN              const   ib_al_handle_t                          h_al,\r
+       IN              const   net32_t                                         cid );\r
+\r
 NTSTATUS\r
 ndi_req_cm(\r
-       IN              ib_qp_handle_t  const                   h_qp,\r
-       IN              cl_ioctl_handle_t                               h_ioctl\r
+       IN              ib_al_handle_t                                  h_al,\r
+       IN              PIRP                                                    p_irp\r
        );\r
 \r
 NTSTATUS\r
 ndi_rep_cm(\r
-       IN              ib_qp_handle_t  const                   h_qp,\r
+       IN              ib_al_handle_t                                  h_al,\r
        IN              PIRP                                                    p_irp\r
        );\r
 \r
 cl_status_t\r
 ndi_rtu_cm(\r
-       IN              ib_qp_handle_t  const                   h_qp,\r
+       IN              nd_csq_t                                                *p_csq,\r
        IN              PIRP                                                    p_irp\r
        );\r
 \r
 NTSTATUS\r
 ndi_dreq_cm(\r
-       IN              ib_qp_handle_t  const                   h_qp,\r
+       IN              nd_csq_t                                                *p_csq,\r
        IN              PIRP                                                    p_irp\r
        );\r
-       \r
-NTSTATUS\r
-ndi_qp_init(\r
-       IN              ib_qp_handle_t                                  h_qp );\r
 \r
 void\r
-ndi_qp_destroy(\r
-       IN              ib_qp_handle_t                                  h_qp );\r
+ndi_cancel_cm_irps(\r
+       IN              nd_csq_t                                                *p_csq\r
+       );\r
 \r
-void\r
-ndi_qp_free(\r
-       IN              ib_qp_handle_t                                  h_qp );\r
+NTSTATUS\r
+ndi_listen_cm(\r
+       IN              ib_al_handle_t                                  h_al,\r
+       IN              ib_cep_listen_t                                 *p_listen,\r
+               OUT     net32_t                                                 *p_cid,\r
+               OUT     size_t                                                  *p_ret_bytes\r
+       );\r
+\r
+NTSTATUS\r
+ndi_get_req_cm(\r
+       IN              nd_csq_t                                                *p_csq,\r
+       IN              PIRP                                                    p_irp\r
+       );\r
 \r
 #endif\r
 \r
index 2b174ee..86b39c9 100644 (file)
@@ -53,7 +53,6 @@ proxy_create_cep(
                OUT     size_t                                  *p_ret_bytes )\r
 {\r
        al_dev_open_context_t           *p_context;\r
-       void*                                           user_context;\r
        ual_create_cep_ioctl_t          *p_ioctl;\r
 \r
        AL_ENTER( AL_DBG_CM );\r
@@ -62,19 +61,13 @@ proxy_create_cep(
        p_ioctl = (ual_create_cep_ioctl_t*)cl_ioctl_out_buf( h_ioctl );\r
 \r
        /* Validate user parameters. */\r
-       if( cl_ioctl_in_size( h_ioctl ) != sizeof(uint64_t) ||\r
-               cl_ioctl_out_size( h_ioctl ) != sizeof(ual_create_cep_ioctl_t) )\r
+       if( cl_ioctl_out_size( h_ioctl ) != sizeof(ual_create_cep_ioctl_t) )\r
        {\r
                AL_EXIT( AL_DBG_CM );\r
                return CL_INVALID_PARAMETER;\r
        }\r
-\r
-       user_context = *(void**)cl_ioctl_in_buf( h_ioctl );\r
-\r
        /* We use IRPs as notification mechanism so the callback is NULL. */\r
-       p_ioctl->cid = AL_INVALID_CID;\r
-       p_ioctl->status = al_create_cep( p_context->h_al, NULL,\r
-               user_context, NULL, &p_ioctl->cid );\r
+       p_ioctl->status = kal_cep_alloc( p_context->h_al, &p_ioctl->cid );\r
 \r
        *p_ret_bytes = sizeof(ual_create_cep_ioctl_t);\r
 \r
@@ -862,37 +855,6 @@ proxy_cep_get_event(
 }\r
 \r
 \r
-static cl_status_t\r
-proxy_cep_get_req_cid(\r
-       IN              void                                    *p_open_context,\r
-       IN              cl_ioctl_handle_t               h_ioctl,\r
-               OUT     size_t                                  *p_ret_bytes )\r
-{\r
-       cl_status_t cl_status;\r
-       al_dev_open_context_t *p_context;\r
-       UNUSED_PARAM(p_ret_bytes);\r
-       \r
-       AL_ENTER( AL_DBG_CM );\r
-\r
-       p_context = (al_dev_open_context_t*)p_open_context;\r
-\r
-       /* Validate user parameters. */\r
-       if( cl_ioctl_in_size( h_ioctl ) != sizeof(uint32_t) ||\r
-               cl_ioctl_out_size( h_ioctl ) != sizeof(uint32_t) )\r
-       {\r
-               AL_EXIT( AL_DBG_CM );\r
-               return CL_INVALID_PARAMETER;\r
-       }\r
-\r
-       /* get CID */\r
-       cl_status = al_cep_get_cid( p_context->h_al, \r
-               *(uint32_t*)cl_ioctl_in_buf( h_ioctl ), h_ioctl );\r
-\r
-       AL_EXIT( AL_DBG_CM );\r
-       return cl_status;\r
-}\r
-\r
-\r
 \r
 static cl_status_t\r
 proxy_cep_get_pdata(\r
@@ -902,8 +864,8 @@ proxy_cep_get_pdata(
 {\r
        al_dev_open_context_t           *p_context;\r
        ual_cep_get_pdata_ioctl_t       *p_ioctl;\r
-       al_conn_qp_t                            *p_qp;\r
        NTSTATUS                    status;\r
+       net32_t                                         cid;\r
 \r
        AL_ENTER( AL_DBG_CM );\r
 \r
@@ -922,23 +884,9 @@ proxy_cep_get_pdata(
                return CL_INVALID_PARAMETER;\r
        }\r
 \r
-       if ( p_ioctl->in.h_qp )\r
-       {\r
-               /* Get the kernel QP handle. */\r
-               p_qp = (al_conn_qp_t*)al_hdl_ref(\r
-                       p_context->h_al, p_ioctl->in.h_qp, AL_OBJ_TYPE_H_QP );\r
-               if( !p_qp )\r
-               {\r
-                       AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR, \r
-                               ("Invalid QP handle\n"));\r
-                       return CL_CONNECTION_INVALID;\r
-               }\r
-               p_ioctl->in.cid = p_qp->cid;\r
-               deref_al_obj( &p_qp->qp.obj );\r
-       }\r
-\r
+       cid = p_ioctl->in.cid;\r
        p_ioctl->out.pdata_len = sizeof(p_ioctl->out.pdata);\r
-       status = al_cep_get_pdata( p_context->h_al, p_ioctl->in.cid,\r
+       status = al_cep_get_pdata( p_context->h_al, cid,\r
         &p_ioctl->out.init_depth, &p_ioctl->out.resp_res,\r
                (uint8_t*)&p_ioctl->out.pdata_len, p_ioctl->out.pdata );\r
 \r
@@ -953,6 +901,7 @@ proxy_cep_get_pdata(
        return status;\r
 }\r
 \r
+\r
 cl_status_t cep_ioctl(\r
        IN              cl_ioctl_handle_t               h_ioctl,\r
                OUT     size_t                                  *p_ret_bytes )\r
@@ -1036,9 +985,6 @@ cl_status_t cep_ioctl(
        case UAL_CEP_POLL:\r
                cl_status = proxy_cep_poll( p_context, h_ioctl, p_ret_bytes );\r
                break;\r
-       case UAL_CEP_GET_REQ_CID:\r
-               cl_status = proxy_cep_get_req_cid( p_context, h_ioctl, p_ret_bytes );\r
-               break;\r
        case UAL_CEP_GET_PDATA:\r
                cl_status = proxy_cep_get_pdata( p_context, h_ioctl, p_ret_bytes );\r
                break;\r
index 3da4dbb..82c9f82 100644 (file)
@@ -322,7 +322,7 @@ __ndi_req_cm(
        p_context = (al_dev_open_context_t*)p_open_context;\r
 \r
        /* Validate user parameters. */\r
-       if( cl_ioctl_in_size( h_ioctl ) < sizeof(ual_ndi_req_cm_ioctl_in_t))\r
+       if( cl_ioctl_in_size( h_ioctl ) < sizeof(ual_ndi_req_cm_ioctl_in_t) )\r
        {\r
                cl_status = CL_INVALID_PARAMETER;\r
                goto exit;\r
@@ -351,7 +351,7 @@ __ndi_req_cm(
        }\r
 \r
        /* perform the ioctl */\r
-       cl_status = ndi_req_cm( h_qp, h_ioctl );\r
+       cl_status = ndi_req_cm( p_context->h_al, h_ioctl );\r
 \r
 err:\r
        deref_al_obj( &h_qp->obj );\r
@@ -370,7 +370,6 @@ __ndi_rep_cm(
        cl_status_t cl_status;\r
        ib_qp_handle_t h_qp = NULL;\r
        al_dev_open_context_t *p_context;\r
-       net32_t cid;\r
        ual_ndi_rep_cm_ioctl_in_t *p_rep = \r
                (ual_ndi_rep_cm_ioctl_in_t*)cl_ioctl_in_buf( h_ioctl );\r
        UNUSED_PARAM(p_ret_bytes);\r
@@ -386,6 +385,9 @@ __ndi_rep_cm(
                goto exit;\r
        }\r
 \r
+       AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI,\r
+               ("CID = %d\n", p_rep->cid) );\r
+\r
        /* Get and validate QP handle */\r
        h_qp = (ib_qp_handle_t)al_hdl_ref( p_context->h_al, p_rep->h_qp, AL_OBJ_TYPE_H_QP );\r
        if( !h_qp )\r
@@ -406,12 +408,9 @@ __ndi_rep_cm(
                cl_status = CL_INVALID_PARAMETER;\r
                goto err;\r
        }\r
-       \r
-       /* Get and validate CID */\r
-       cid = p_rep->cid;\r
 \r
        /* perform the ioctls */\r
-       cl_status = ndi_rep_cm( h_qp, h_ioctl );\r
+       cl_status = ndi_rep_cm( p_context->h_al, h_ioctl );\r
 \r
 err:\r
        deref_al_obj( &h_qp->obj );\r
@@ -455,7 +454,6 @@ __ndi_rej_cm(
                goto exit;\r
        }\r
 \r
-       al_destroy_cep( p_context->h_al, &p_rej->cid, FALSE );\r
        ntstatus = STATUS_SUCCESS;\r
 \r
 exit:\r
@@ -470,10 +468,8 @@ __ndi_rtu_cm(
                OUT     size_t                                  *p_ret_bytes )\r
 {\r
        cl_status_t cl_status;\r
-       ib_qp_handle_t h_qp = NULL;\r
+       nd_csq_t* p_csq;\r
        al_dev_open_context_t *p_context;\r
-       ual_ndi_rtu_cm_ioctl_in_t *p_rtu = \r
-               (ual_ndi_rtu_cm_ioctl_in_t*)cl_ioctl_in_buf( h_ioctl );\r
 \r
        UNUSED_PARAM(p_ret_bytes);\r
        \r
@@ -482,32 +478,28 @@ __ndi_rtu_cm(
        p_context = (al_dev_open_context_t*)p_open_context;\r
 \r
        /* Validate user parameters. */\r
-       if( cl_ioctl_in_size( h_ioctl ) < sizeof(ual_ndi_rtu_cm_ioctl_in_t))\r
+       if( cl_ioctl_in_size( h_ioctl ) < sizeof(net32_t) )\r
        {\r
                cl_status = CL_INVALID_PARAMETER;\r
                goto exit;\r
        }\r
 \r
-       /* Validate QP handle */\r
-       h_qp = (ib_qp_handle_t)al_hdl_ref( p_context->h_al, p_rtu->h_qp, AL_OBJ_TYPE_H_QP );\r
-       if( !h_qp )\r
+       p_csq = kal_cep_get_context(\r
+               p_context->h_al,\r
+               *(net32_t*)cl_ioctl_in_buf( h_ioctl ),\r
+               nd_cm_handler,\r
+               nd_csq_ref\r
+               );\r
+       if( p_csq == NULL )\r
        {\r
                cl_status = CL_INVALID_HANDLE;\r
                goto exit;\r
        }\r
 \r
-       /* Check QP type */\r
-       if( h_qp->type != IB_QPT_RELIABLE_CONN )\r
-       {\r
-               cl_status = CL_INVALID_HANDLE;\r
-               goto err;\r
-       }\r
-\r
        /* perform the ioctl */\r
-       cl_status = ndi_rtu_cm( h_qp, h_ioctl );\r
+       cl_status = ndi_rtu_cm( p_csq, h_ioctl );\r
 \r
-err:\r
-       deref_al_obj( &h_qp->obj );\r
+       nd_csq_release( p_csq );\r
 \r
 exit:\r
        AL_EXIT( AL_DBG_NDI );\r
@@ -521,7 +513,7 @@ __ndi_dreq_cm(
                OUT     size_t                                  *p_ret_bytes )\r
 {\r
        cl_status_t cl_status;\r
-       ib_qp_handle_t h_qp = NULL;\r
+       nd_csq_t *p_csq;\r
        al_dev_open_context_t *p_context;\r
 \r
        UNUSED_PARAM(p_ret_bytes);\r
@@ -531,37 +523,206 @@ __ndi_dreq_cm(
        p_context = (al_dev_open_context_t*)p_open_context;\r
 \r
        /* Validate user parameters. */\r
-       if( cl_ioctl_in_size( h_ioctl ) < sizeof(uint64_t))\r
+       if( cl_ioctl_in_size( h_ioctl ) < sizeof(net32_t) )\r
        {\r
                cl_status = CL_INVALID_PARAMETER;\r
                goto exit;\r
        }\r
 \r
-       /* Validate QP handle */\r
-       h_qp = (ib_qp_handle_t)al_hdl_ref( p_context->h_al, \r
-               *(uint64_t*)cl_ioctl_in_buf( h_ioctl ), AL_OBJ_TYPE_H_QP );\r
-       if( !h_qp )\r
+       /* Validate CID */\r
+       p_csq = (nd_csq_t*)kal_cep_get_context(\r
+               p_context->h_al,\r
+               *(net32_t*)cl_ioctl_in_buf( h_ioctl ),\r
+               nd_cm_handler,\r
+               nd_csq_ref\r
+               );\r
+\r
+       if( p_csq == NULL )\r
        {\r
                cl_status = CL_CONNECTION_INVALID;\r
                goto exit;\r
        }\r
 \r
-       /* Check QP type */\r
-       if( h_qp->type != IB_QPT_RELIABLE_CONN )\r
+       /* perform the ioctl */\r
+       cl_status = ndi_dreq_cm( p_csq, h_ioctl );\r
+\r
+       nd_csq_release( p_csq );\r
+\r
+exit:\r
+       AL_EXIT( AL_DBG_NDI );\r
+       return cl_status;\r
+}\r
+\r
+static NTSTATUS\r
+__ndi_notify_dreq_cm(\r
+       IN              void                                    *p_open_context,\r
+       IN              cl_ioctl_handle_t               h_ioctl,\r
+               OUT     size_t                                  *p_ret_bytes )\r
+{\r
+       NTSTATUS status;\r
+       nd_csq_t *p_csq;\r
+       al_dev_open_context_t *p_context;\r
+\r
+       UNUSED_PARAM(p_ret_bytes);\r
+       \r
+       AL_ENTER( AL_DBG_NDI );\r
+\r
+       p_context = (al_dev_open_context_t*)p_open_context;\r
+\r
+       /* Validate user parameters. */\r
+       if( cl_ioctl_in_size( h_ioctl ) < sizeof(net32_t) )\r
        {\r
-               cl_status = CL_CONNECTION_INVALID;\r
-               goto err;\r
+               status = STATUS_INVALID_PARAMETER;\r
+               goto exit;\r
+       }\r
+\r
+       /* Validate CID */\r
+       p_csq = (nd_csq_t*)kal_cep_get_context(\r
+               p_context->h_al,\r
+               *(net32_t*)cl_ioctl_in_buf( h_ioctl ),\r
+               nd_cm_handler,\r
+               nd_csq_ref\r
+               );\r
+\r
+       if( p_csq == NULL )\r
+       {\r
+               status = STATUS_CONNECTION_INVALID;\r
+               goto exit;\r
        }\r
 \r
        /* perform the ioctl */\r
-       cl_status = ndi_dreq_cm( h_qp, h_ioctl );\r
+       status = IoCsqInsertIrpEx(\r
+               &p_csq->csq,\r
+               h_ioctl,\r
+               NULL,\r
+               (VOID*)(ULONG_PTR)NDI_CM_CONNECTED_DREQ_RCVD\r
+               );\r
 \r
-err:\r
-       deref_al_obj( &h_qp->obj );\r
+       nd_csq_release( p_csq );\r
 \r
 exit:\r
        AL_EXIT( AL_DBG_NDI );\r
-       return cl_status;\r
+       return status;\r
+}\r
+\r
+static cl_status_t\r
+__ndi_cancel_cm_irps(\r
+       IN              void                                    *p_open_context,\r
+       IN              cl_ioctl_handle_t               h_ioctl,\r
+               OUT     size_t                                  *p_ret_bytes )\r
+{\r
+       nd_csq_t *p_csq;\r
+       al_dev_open_context_t *p_context;\r
+\r
+       UNUSED_PARAM(p_ret_bytes);\r
+       \r
+       AL_ENTER( AL_DBG_NDI );\r
+\r
+       p_context = (al_dev_open_context_t*)p_open_context;\r
+\r
+       /* Validate user parameters. */\r
+       if( cl_ioctl_in_size( h_ioctl ) < sizeof(net32_t) )\r
+       {\r
+               AL_EXIT( AL_DBG_NDI );\r
+               return STATUS_INVALID_PARAMETER;\r
+       }\r
+\r
+       /* Validate CID */\r
+       p_csq = (nd_csq_t*)kal_cep_get_context(\r
+               p_context->h_al,\r
+               *(net32_t*)cl_ioctl_in_buf( h_ioctl ),\r
+               nd_cm_handler,\r
+               nd_csq_ref\r
+               );\r
+\r
+       if( p_csq == NULL )\r
+       {\r
+               AL_EXIT( AL_DBG_NDI );\r
+               return STATUS_UNSUCCESSFUL;\r
+       }\r
+\r
+       /* perform the ioctl */\r
+       ndi_cancel_cm_irps( p_csq );\r
+       nd_csq_release( p_csq );\r
+\r
+       AL_EXIT( AL_DBG_NDI );\r
+       return STATUS_SUCCESS;\r
+}\r
+\r
+static cl_status_t\r
+__ndi_listen_cm(\r
+       IN              void                                    *p_open_context,\r
+       IN              cl_ioctl_handle_t               h_ioctl,\r
+               OUT     size_t                                  *p_ret_bytes )\r
+{\r
+       al_dev_open_context_t *p_context;\r
+       ual_cep_listen_ioctl_t *p_listen = \r
+               (ual_cep_listen_ioctl_t*)cl_ioctl_in_buf( h_ioctl );\r
+       net32_t* p_cid =\r
+               (net32_t*)cl_ioctl_out_buf( h_ioctl );\r
+\r
+       AL_ENTER( AL_DBG_NDI );\r
+\r
+       p_context = (al_dev_open_context_t*)p_open_context;\r
+\r
+       /* Validate user parameters. */\r
+       if( cl_ioctl_in_size( h_ioctl ) < sizeof(*p_listen) ||\r
+               cl_ioctl_out_size( h_ioctl ) != sizeof(*p_cid) )\r
+       {\r
+               AL_EXIT( AL_DBG_NDI );\r
+               return CL_INVALID_PARAMETER;\r
+       }\r
+\r
+       /* Set the private data compare buffer to our kernel copy. */\r
+       if( p_listen->cep_listen.p_cmp_buf )\r
+               p_listen->cep_listen.p_cmp_buf = p_listen->compare;\r
+\r
+       AL_EXIT( AL_DBG_NDI );\r
+       return ndi_listen_cm( p_context->h_al, &p_listen->cep_listen, p_cid, p_ret_bytes );\r
+}\r
+\r
+static cl_status_t\r
+__ndi_get_req_cm(\r
+       IN              void                                    *p_open_context,\r
+       IN              cl_ioctl_handle_t               h_ioctl,\r
+               OUT     size_t                                  *p_ret_bytes )\r
+{\r
+       al_dev_open_context_t *p_context;\r
+       nd_csq_t *p_csq;\r
+       NTSTATUS status;\r
+\r
+       AL_ENTER( AL_DBG_NDI );\r
+\r
+       UNREFERENCED_PARAMETER( p_ret_bytes );\r
+\r
+       p_context = (al_dev_open_context_t*)p_open_context;\r
+\r
+       /* Validate user parameters. */\r
+       if( cl_ioctl_in_size( h_ioctl ) != sizeof(net32_t) ||\r
+               cl_ioctl_out_size( h_ioctl ) != sizeof(net32_t) )\r
+       {\r
+               AL_EXIT( AL_DBG_NDI );\r
+               return CL_INVALID_PARAMETER;\r
+       }\r
+\r
+       /* Validate CID */\r
+       p_csq = (nd_csq_t*)kal_cep_get_context(\r
+               p_context->h_al,\r
+               *(net32_t*)cl_ioctl_in_buf( h_ioctl ),\r
+               nd_cm_handler,\r
+               nd_csq_ref\r
+               );\r
+\r
+       if( p_csq == NULL )\r
+       {\r
+               AL_EXIT( AL_DBG_NDI );\r
+               return STATUS_UNSUCCESSFUL;\r
+       }\r
+\r
+       status = ndi_get_req_cm( p_csq, h_ioctl );\r
+       nd_csq_release( p_csq );\r
+       AL_EXIT( AL_DBG_NDI );\r
+       return status;\r
 }\r
 \r
 cl_status_t\r
@@ -621,9 +782,23 @@ ndi_ioctl(
             h_ioctl->IoStatus.Status = CL_SUCCESS;\r
         h_ioctl->IoStatus.Information = 0;\r
 \r
+               AL_PRINT( TRACE_LEVEL_VERBOSE, AL_DBG_NDI,\r
+                       ("UAL_NDI_NOOP completed with %08x\n", h_ioctl->IoStatus.Status) );\r
         IoCompleteRequest( h_ioctl, IO_NETWORK_INCREMENT );\r
         cl_status = CL_PENDING;\r
         break;\r
+       case UAL_NDI_NOTIFY_DREQ:\r
+               cl_status = __ndi_notify_dreq_cm( p_context, h_ioctl, p_ret_bytes );\r
+               break;\r
+       case UAL_NDI_CANCEL_CM_IRPS:\r
+               cl_status = __ndi_cancel_cm_irps( p_context, h_ioctl, p_ret_bytes );\r
+               break;\r
+       case UAL_NDI_LISTEN_CM:\r
+               cl_status = __ndi_listen_cm( p_context, h_ioctl, p_ret_bytes );\r
+               break;\r
+       case UAL_NDI_GET_REQ_CM:\r
+               cl_status = __ndi_get_req_cm( p_context, h_ioctl, p_ret_bytes );\r
+               break;\r
        default:\r
                cl_status = CL_INVALID_PARAMETER;\r
                break;\r
index 280c2b2..cd1a0e6 100644 (file)
@@ -272,9 +272,8 @@ __create_ucep(
        /* Create a kernel CEP only if we don't already have a CID. */\r
        if( cid == AL_INVALID_CID )\r
        {\r
-               uint64_t cep_context = (ULONG_PTR)context;\r
-               if( !DeviceIoControl( g_al_device, UAL_CREATE_CEP, &cep_context,\r
-                       sizeof(cep_context), &ioctl, sizeof(ioctl), &bytes_ret, NULL ) ||\r
+               if( !DeviceIoControl( g_al_device, UAL_CREATE_CEP, NULL,\r
+                       0, &ioctl, sizeof(ioctl), &bytes_ret, NULL ) ||\r
                        bytes_ret != sizeof(ioctl) )\r
                {\r
                        __destroy_ucep( p_cep );\r
index 0b425c1..0884779 100644 (file)
@@ -3089,7 +3089,6 @@ typedef union _ual_cep_get_pdata_ioctl
 {\r
        struct _ual_cep_get_pdata_ioctl_in\r
        {\r
-               uint64_t                                h_qp;\r
                net32_t                                 cid;\r
 \r
        }       in;\r
@@ -3106,9 +3105,6 @@ typedef union _ual_cep_get_pdata_ioctl
 }      ual_cep_get_pdata_ioctl_t;\r
 /*\r
 * FIELDS\r
-*      h_qp\r
-*              A handle to the QP to modify.\r
-*\r
 *      in.cid\r
 *              The CID for the target CEP.\r
 *\r
@@ -3483,6 +3479,7 @@ typedef struct _ual_ndi_req_cm_ioctl_in
     ib_path_rec_t               path;\r
        uint64_t                                        h_qp;\r
        net64_t                                         guid;\r
+       net32_t                                         cid;\r
        uint16_t                                        dst_port;\r
     uint8_t                     resp_res;\r
     uint8_t                     init_depth;\r
@@ -3493,7 +3490,7 @@ typedef struct _ual_ndi_req_cm_ioctl_in
 }      ual_ndi_req_cm_ioctl_in_t;\r
 /*\r
 * NOTES\r
-*      The output parameter is the new QP state (RTS).\r
+*      There is no output parameter.\r
 *\r
 * FIELDS\r
 *      h_qp\r
@@ -3502,6 +3499,9 @@ typedef struct _ual_ndi_req_cm_ioctl_in
 *      guid\r
 *              Local port GUID to which to bind to.\r
 *\r
+*      cid\r
+*              CID of the CEP to use for the connection request.\r
+*\r
 *      dst_port\r
 *              Destination port number.\r
 *\r
@@ -3603,38 +3603,6 @@ typedef struct _ual_ndi_rej_cm_ioctl_in
 *\r
 *****/\r
 \r
-\r
-/****s* User-mode Access Layer/ual_ndi_rtu_cm_ioctl_in_t\r
-* NAME\r
-*      ual_ndi_rtu_cm_ioctl_in_t\r
-*\r
-* DESCRIPTION\r
-*      IOCTL structure containing the input parameters \r
-*      sending CM RTU response .\r
-*\r
-* SYNOPSIS\r
-*/\r
-typedef struct _ual_ndi_rtu_cm_ioctl_in\r
-{\r
-       uint64_t                                        h_qp;\r
-\r
-}      ual_ndi_rtu_cm_ioctl_in_t;\r
-/*\r
-* NOTES\r
-*      The output parameter is the new QP state (RTS).\r
-*\r
-* FIELDS\r
-*      h_qp\r
-*              A handle to the QP to modify.\r
-*\r
-*      init_depth\r
-*              The maximum number of outstanding RDMA read/atomic operations.\r
-*\r
-*      resp_res\r
-*              The maximum number of RDMA read/atomic operations from the recipient.\r
-*\r
-*****/\r
-\r
 /****s* User-mode Access Layer/ual_ndi_modify_qp_ioctl_in_t\r
 * NAME\r
 *      ual_ndi_modify_qp_ioctl_in_t\r