Fix function name in debug message.
[mirror/winof/.git] / ulp / wsd / user / ibsp_iblow.c
1 /*\r
2  * Copyright (c) 2005 SilverStorm Technologies.  All rights reserved.\r
3  *\r
4  * This software is available to you under the OpenIB.org BSD license\r
5  * below:\r
6  *\r
7  *     Redistribution and use in source and binary forms, with or\r
8  *     without modification, are permitted provided that the following\r
9  *     conditions are met:\r
10  *\r
11  *      - Redistributions of source code must retain the above\r
12  *        copyright notice, this list of conditions and the following\r
13  *        disclaimer.\r
14  *\r
15  *      - Redistributions in binary form must reproduce the above\r
16  *        copyright notice, this list of conditions and the following\r
17  *        disclaimer in the documentation and/or other materials\r
18  *        provided with the distribution.\r
19  *\r
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
27  * SOFTWARE.\r
28  *\r
29  * $Id$\r
30  */\r
31 \r
32 #include "ibspdll.h"\r
33 \r
34 \r
35 typedef struct _io_comp_info\r
36 {\r
37         struct ibsp_socket_info *p_socket;\r
38         LPWSAOVERLAPPED                 p_ov;\r
39 \r
40 } io_comp_info_t;\r
41 \r
42 \r
43 /* Work queue entry completion routine. */\r
44 static void\r
45 complete_wq(\r
46         IN              const   ib_wc_t                                         *wc,\r
47                 OUT                     io_comp_info_t                          *p_io_info )\r
48 {\r
49         struct _wr                              *wr = NULL;\r
50         struct _recv_wr                 *p_recv_wr = NULL;\r
51         LPWSAOVERLAPPED                 lpOverlapped = NULL;\r
52         struct ibsp_socket_info *socket_info = NULL;\r
53 \r
54         IBSP_ENTER( IBSP_DBG_IO );\r
55 \r
56         wr = (struct _wr * __ptr64)wc->wr_id;\r
57         p_recv_wr = (struct _recv_wr * __ptr64)wc->wr_id;\r
58 \r
59         CL_ASSERT( wr );\r
60 \r
61         socket_info = wr->socket_info;\r
62         p_io_info->p_socket = socket_info;\r
63 \r
64         lpOverlapped = wr->lpOverlapped;\r
65 \r
66         IBSP_TRACE4( IBSP_DBG_IO,\r
67                 ("socket %p, ov %p, work completion status=%s, wc_type=%s\n",\r
68                 socket_info, lpOverlapped, ib_get_wc_status_str( wc->status ),\r
69                 ib_get_wc_type_str( wc->wc_type )) );\r
70 \r
71         /* Set the windows error code. It's not easy to find an easy\r
72          * correspondence between the IBAL error codes and windows error\r
73          * codes; but it probably does not matter, as long as it returns an\r
74          * error. */\r
75         switch( wc->status )\r
76         {\r
77         case IB_WCS_SUCCESS:\r
78                 /*\r
79                  * Set the length of the operation. Under Infiniband, the work\r
80                  * completion length is only valid for a receive\r
81                  * operation. Fortunately we had already set the length during the\r
82                  * send operation. \r
83                  *\r
84                  * lpWPUCompleteOverlappedRequest is supposed to store the length\r
85                  * into InternalHigh, however it will not be called if the low\r
86                  * order bit of lpOverlapped->hEvent is set. So we do it and hope\r
87                  * for the best. \r
88                  *\r
89                  * NOTE: Without a valid length, the switch doesn't seem to call \r
90                  * GetOverlappedResult() even if we call lpWPUCompleteOverlappedRequest()\r
91                  */\r
92                 if( wc->wc_type == IB_WC_RECV )\r
93                         lpOverlapped->InternalHigh = wc->length;\r
94 \r
95                 lpOverlapped->OffsetHigh = 0;\r
96                 break;\r
97 \r
98         case IB_WCS_WR_FLUSHED_ERR:\r
99                 cl_spinlock_acquire( &socket_info->mutex );\r
100 \r
101                 if( socket_info->socket_state == IBSP_DUPLICATING_REMOTE &&\r
102                         wc->wc_type == IB_WC_RECV )\r
103                 {\r
104                         /*\r
105                          * Take the wr off the wr_list, and place onto the\r
106                          * dup_wr_list.  We will post them later on the new QP. \r
107                          */\r
108                         cl_spinlock_acquire( &socket_info->recv_lock );\r
109 \r
110                         /* Copy to the duplicate WR array. */\r
111                         socket_info->dup_wr[socket_info->dup_idx] = *p_recv_wr;\r
112 \r
113 #if QP_ATTRIB_RQ_DEPTH == 256 || QP_ATTRIB_RQ_DEPTH == 128 || \\r
114         QP_ATTRIB_RQ_DEPTH == 64 || QP_ATTRIB_RQ_DEPTH == 32 || \\r
115         QP_ATTRIB_RQ_DEPTH == 16 || QP_ATTRIB_RQ_DEPTH == 8\r
116                         socket_info->dup_idx++;\r
117                         socket_info->dup_idx &= (QP_ATTRIB_RQ_DEPTH - 1);\r
118 #else\r
119                         if( ++socket_info->dup_idx == QP_ATTRIB_RQ_DEPTH )\r
120                                 socket_info->dup_idx = 0;\r
121 #endif\r
122 \r
123                         cl_atomic_inc( &socket_info->dup_cnt );\r
124                         /* ib_cq_comp will decrement the receive count. */\r
125                         cl_atomic_dec( &socket_info->recv_cnt );\r
126 \r
127                         cl_spinlock_release( &socket_info->recv_lock );\r
128 \r
129                         cl_spinlock_release( &socket_info->mutex );\r
130                         IBSP_EXIT( IBSP_DBG_IO );\r
131                         return;\r
132                 }\r
133                 \r
134                 /* Check for flushing the receive buffers on purpose. */\r
135                 if( socket_info->socket_state == IBSP_DUPLICATING_OLD )\r
136                         wr->lpOverlapped->OffsetHigh = 0;\r
137                 else\r
138                         wr->lpOverlapped->OffsetHigh = WSA_OPERATION_ABORTED;\r
139 \r
140                 cl_spinlock_release( &socket_info->mutex );\r
141 \r
142                 /* Override the length, as per the WSD specs. */\r
143                 wr->lpOverlapped->InternalHigh = 0;\r
144                 break;\r
145 \r
146         case IB_WCS_LOCAL_LEN_ERR:\r
147         case IB_WCS_LOCAL_OP_ERR:\r
148         case IB_WCS_LOCAL_PROTECTION_ERR:\r
149         case IB_WCS_MEM_WINDOW_BIND_ERR:\r
150         case IB_WCS_REM_ACCESS_ERR:\r
151         case IB_WCS_REM_OP_ERR:\r
152         case IB_WCS_RNR_RETRY_ERR:\r
153         case IB_WCS_TIMEOUT_RETRY_ERR:\r
154         case IB_WCS_REM_INVALID_REQ_ERR:\r
155         default:\r
156                 IBSP_ERROR( ("%s error: %s\n",\r
157                         ib_get_wc_type_str( wc->wc_type ),\r
158                         ib_get_wc_status_str( wc->status )) );\r
159                 lpOverlapped->OffsetHigh = WSAECONNABORTED;\r
160                 wr->lpOverlapped->InternalHigh = 0;\r
161                 socket_info->qp_error = WSAECONNABORTED;\r
162                 break;\r
163         }\r
164 \r
165 #ifdef _DEBUG_\r
166         if( wc->wc_type == IB_WC_RECV )\r
167         {\r
168                 // This code requires the recv count to be decremented here, but it needs\r
169                 // to be decremented after any callbacks are invoked so socket destruction\r
170                 // gets delayed until all callbacks have been invoked.\r
171                 //{\r
172                 //      uint8_t idx;\r
173 \r
174                 //      cl_spinlock_acquire( &socket_info->recv_lock );\r
175                 //      idx = socket_info->recv_idx - (uint8_t)socket_info->recv_cnt;\r
176                 //      if( idx >= QP_ATTRIB_RQ_DEPTH )\r
177                 //              idx += QP_ATTRIB_RQ_DEPTH;\r
178 \r
179                 //      CL_ASSERT( wc->wr_id == (uint64_t)(void* __ptr64)&socket_info->recv_wr[idx] );\r
180                 //      cl_atomic_dec( &socket_info->recv_cnt );\r
181                 //      cl_spinlock_release( &socket_info->recv_lock );\r
182                 //}\r
183 \r
184                 if( wc->status == IB_SUCCESS && p_recv_wr->ds_array[0].length >= 40 )\r
185                 {\r
186                         debug_dump_buffer( IBSP_DBG_WQ | IBSP_DBG_LEVEL4, "RECV",\r
187                                 (void * __ptr64)p_recv_wr->ds_array[0].vaddr, 40 );\r
188                 }\r
189 \r
190                 cl_atomic_dec( &g_ibsp.recv_count );\r
191                 cl_atomic_inc( &socket_info->recv_comp );\r
192 \r
193                 memset( p_recv_wr, 0x33, sizeof(struct _recv_wr) );\r
194         }\r
195         else\r
196         {\r
197                 // This code requires the send count to be decremented here, but it needs\r
198                 // to be decremented after any callbacks are invoked so socket destruction\r
199                 // gets delayed until all callbacks have been invoked.\r
200                 //{\r
201                 //      uint8_t idx;\r
202 \r
203                 //      cl_spinlock_acquire( &socket_info->send_lock );\r
204                 //      idx = socket_info->send_idx - (uint8_t)socket_info->send_cnt;\r
205                 //      if( idx >= QP_ATTRIB_SQ_DEPTH )\r
206                 //              idx += QP_ATTRIB_SQ_DEPTH;\r
207                 //      CL_ASSERT( wc->wr_id == (uint64_t)(void* __ptr64)&socket_info->send_wr[idx] );\r
208                 //      cl_atomic_dec( &socket_info->send_cnt );\r
209                 //      cl_spinlock_release( &socket_info->send_lock );\r
210                 //}\r
211 \r
212                 if( wc->wc_type == IB_WC_SEND )\r
213                 {\r
214                         cl_atomic_dec( &g_ibsp.send_count );\r
215                         cl_atomic_inc( &socket_info->send_comp );\r
216 \r
217                         fzprint(("%s():%d:0x%x:0x%x: send_count=%d\n",\r
218                                 __FUNCTION__,\r
219                                 __LINE__, GetCurrentProcessId(), GetCurrentThreadId(), g_ibsp.send_count));\r
220                 }\r
221 \r
222                 memset( wr, 0x33, sizeof(struct _wr) );\r
223         }\r
224 #endif\r
225 \r
226         IBSP_TRACE4( IBSP_DBG_IO,\r
227                 ("overlapped=%p, InternalHigh=%d, hEvent=%x\n",\r
228                 lpOverlapped, lpOverlapped->InternalHigh,\r
229                 (uintptr_t) lpOverlapped->hEvent) );\r
230 \r
231         /* Don't notify the switch for that completion only if:\r
232          *   - the switch don't want a notification\r
233          *   - the wq completed with success\r
234          *   - the socket is still connected\r
235          */\r
236         if( ((uintptr_t) lpOverlapped->hEvent) & 0x00000001 )\r
237         {\r
238                 /* Indicate this operation is complete. The switch will poll\r
239                  * with calls to WSPGetOverlappedResult(). */\r
240 \r
241 #ifdef _DEBUG_\r
242                 cl_atomic_dec( &g_ibsp.overlap_h1_comp_count );\r
243 \r
244                 fzprint(("%s():%d:0x%x:0x%x: ov=0x%p h0=%d h1=%d h1_c=%d send=%d recv=%d\n",\r
245                                  __FUNCTION__, __LINE__, GetCurrentProcessId(),\r
246                                  GetCurrentThreadId(), lpOverlapped,\r
247                                  g_ibsp.overlap_h0_count, g_ibsp.overlap_h1_count,\r
248                                  g_ibsp.overlap_h1_comp_count, g_ibsp.send_count, g_ibsp.recv_count));\r
249 #endif\r
250 \r
251                 IBSP_TRACE1( IBSP_DBG_IO,\r
252                         ("Not calling lpWPUCompleteOverlappedRequest: "\r
253                         "socket=%p, ov=%p OffsetHigh=%d, InternalHigh=%d hEvent=%p\n",\r
254                         socket_info, lpOverlapped, lpOverlapped->OffsetHigh,\r
255                         lpOverlapped->InternalHigh, lpOverlapped->hEvent) );\r
256 \r
257                 lpOverlapped->Internal = 0;\r
258                 p_io_info->p_ov = NULL;\r
259         }\r
260         else\r
261         {\r
262 #ifdef _DEBUG_\r
263                 cl_atomic_dec( &g_ibsp.overlap_h0_count );\r
264 \r
265                 fzprint(("%s():%d:0x%x:0x%x: ov=0x%p h0=%d h1=%d h1_c=%d send=%d recv=%d\n",\r
266                                  __FUNCTION__, __LINE__, GetCurrentProcessId(),\r
267                                  GetCurrentThreadId(), lpOverlapped,\r
268                                  g_ibsp.overlap_h0_count, g_ibsp.overlap_h1_count,\r
269                                  g_ibsp.overlap_h1_comp_count, g_ibsp.send_count, g_ibsp.recv_count));\r
270 #endif\r
271 \r
272                 IBSP_TRACE1( IBSP_DBG_IO,\r
273                         ("Calling lpWPUCompleteOverlappedRequest: "\r
274                         "socket=%p, ov=%p OffsetHigh=%d InternalHigh=%d hEvent=%p\n",\r
275                         socket_info, lpOverlapped, lpOverlapped->OffsetHigh,\r
276                         lpOverlapped->InternalHigh, lpOverlapped->hEvent) );\r
277 \r
278                 p_io_info->p_ov = lpOverlapped;\r
279                 cl_atomic_inc( &socket_info->ref_cnt );\r
280         }\r
281 \r
282         if( wc->wc_type == IB_WC_RECV )\r
283                 cl_atomic_dec( &socket_info->recv_cnt );\r
284         else\r
285                 cl_atomic_dec( &socket_info->send_cnt );\r
286 \r
287         IBSP_EXIT( IBSP_DBG_IO );\r
288 }\r
289 \r
290 \r
291 /* CQ completion handler. */\r
292 void\r
293 ib_cq_comp(\r
294                                         void                                            *cq_context )\r
295 {\r
296         struct cq_thread_info   *cq_tinfo = cq_context;\r
297         ib_api_status_t                 status;\r
298         ib_wc_t                                 wclist[WC_LIST_SIZE];\r
299         ib_wc_t                                 *free_wclist;\r
300         ib_wc_t                                 *done_wclist;\r
301         io_comp_info_t                  info[WC_LIST_SIZE];\r
302         int                                             cb_idx;\r
303         int                                             i;\r
304 #ifdef _DEBUG_\r
305         int                                             comp_count;\r
306 #endif\r
307 \r
308         IBSP_ENTER( IBSP_DBG_WQ );\r
309 \r
310         CL_ASSERT( WC_LIST_SIZE >= 1 );\r
311 \r
312         do\r
313         {\r
314                 /* Try to retrieve up to WC_LIST_SIZE completions at a time. */\r
315                 for( i = 0; i < (WC_LIST_SIZE - 1); i++ )\r
316                 {\r
317                         wclist[i].p_next = &wclist[i + 1];\r
318                 }\r
319                 wclist[(WC_LIST_SIZE - 1)].p_next = NULL;\r
320 \r
321                 free_wclist = &wclist[0];\r
322                 done_wclist = NULL;\r
323 \r
324                 status = ib_poll_cq( cq_tinfo->cq, &free_wclist, &done_wclist );\r
325 \r
326                 IBSP_TRACE( IBSP_DBG_WQ,\r
327                         ("%s():%d:0x%x:0x%x: poll CQ got status %d, free=%p, done=%p\n",\r
328                         __FUNCTION__, __LINE__, GetCurrentProcessId(), GetCurrentThreadId(),\r
329                         status, free_wclist, done_wclist) );\r
330 \r
331                 switch( status )\r
332                 {\r
333                 case IB_NOT_FOUND:\r
334                 case IB_SUCCESS:\r
335                         break;\r
336 \r
337                 case IB_INVALID_CQ_HANDLE:\r
338                         /* This happens when the switch closes the socket while the \r
339                          * execution thread was calling lpWPUCompleteOverlappedRequest. */\r
340                         IBSP_ERROR(\r
341                                 ("ib_poll_cq returned IB_INVLALID_CQ_HANDLE\n") );\r
342                         goto done;\r
343 \r
344                 default:\r
345                         IBSP_ERROR(\r
346                                 ("ib_poll_cq failed returned %s\n", ib_get_err_str( status )) );\r
347                         break;\r
348                 }\r
349 \r
350 #ifdef _DEBUG_\r
351                 comp_count = 0;\r
352 #endif\r
353 \r
354                 /* We have some completions. */\r
355                 cb_idx = 0;\r
356                 while( done_wclist )\r
357                 {\r
358 #ifdef _DEBUG_\r
359                         comp_count++;\r
360 #endif\r
361                         complete_wq( done_wclist, &info[cb_idx++] );\r
362 \r
363                         done_wclist = done_wclist->p_next;\r
364                 }\r
365 \r
366                 for( i = 0; i < cb_idx; i++ )\r
367                 {\r
368                         int error;\r
369                         int ret;\r
370 \r
371                         if( info[i].p_ov )\r
372                         {\r
373                                 IBSP_TRACE1( IBSP_DBG_IO,\r
374                                         ("Calling WPUCompleteOverlappedRequest for ov=%p\n",\r
375                                         info[i].p_ov) );\r
376 \r
377                                 ret = g_ibsp.up_call_table.lpWPUCompleteOverlappedRequest(\r
378                                         info[i].p_socket->switch_socket, info[i].p_ov,\r
379                                         info[i].p_ov->OffsetHigh,\r
380                                         (DWORD)info[i].p_ov->InternalHigh, &error );\r
381                                 if( ret != 0 )\r
382                                 {\r
383                                         IBSP_ERROR( ("WPUCompleteOverlappedRequest for ov=%p "\r
384                                                 "returned %d err %d\n", info[i].p_ov, ret, error) );\r
385                                 }\r
386                                 deref_socket_info( info[i].p_socket );\r
387                         }\r
388                 }\r
389 \r
390 #ifdef _DEBUG_\r
391                 if( comp_count > g_ibsp.max_comp_count )\r
392                 {\r
393                         g_ibsp.max_comp_count = comp_count;\r
394                 }\r
395 #endif\r
396         } while( !free_wclist );\r
397 \r
398         status = ib_rearm_cq( cq_tinfo->cq, FALSE );\r
399         if( status != IB_SUCCESS )\r
400         {\r
401                 IBSP_ERROR(\r
402                         ("ib_rearm_cq returned %s)\n", ib_get_err_str( status )) );\r
403         }\r
404 \r
405 done:\r
406 \r
407 #ifdef _DEBUG_\r
408         fzprint(("%s():%d:0x%x:0x%x: overlap_h0_count=%d overlap_h1_count=%d\n",\r
409                          __FUNCTION__,\r
410                          __LINE__, GetCurrentProcessId(),\r
411                          GetCurrentThreadId(), g_ibsp.overlap_h0_count, g_ibsp.overlap_h1_count));\r
412 #endif\r
413 \r
414         IBSP_EXIT( IBSP_DBG_WQ );\r
415 }\r
416 \r
417 \r
418 /* IB completion thread */\r
419 static DWORD WINAPI\r
420 ib_cq_thread(\r
421                                         LPVOID                                          lpParameter )\r
422 {\r
423         struct cq_thread_info *cq_tinfo = (struct cq_thread_info *)lpParameter;\r
424         cl_status_t cl_status;\r
425 \r
426         IBSP_ENTER( IBSP_DBG_HW );\r
427 \r
428 \r
429         fzprint(("%s():%d:0x%x:0x%x: cq_tinfo=0x%p\n", __FUNCTION__,\r
430                          __LINE__, GetCurrentProcessId(), GetCurrentThreadId(), cq_tinfo));\r
431 \r
432         do\r
433         {\r
434                 cl_status = cl_waitobj_wait_on( cq_tinfo->cq_waitobj, EVENT_NO_TIMEOUT, TRUE );\r
435                 if( cl_status != CL_SUCCESS )\r
436                 {\r
437                         IBSP_ERROR(\r
438                                 ("cl_waitobj_wait_on() (%d)\n", cl_status) );\r
439                 }\r
440 \r
441                 /* \r
442                  * TODO: By rearanging thread creation and cq creation, this check\r
443                  * may be eliminated.\r
444                  */\r
445                 if( cq_tinfo->cq != NULL )\r
446                 {\r
447                         fzprint(("%s():%d:0x%x:0x%x: Calling ib_cq_comp().\n", __FUNCTION__,\r
448                                          __LINE__, GetCurrentProcessId(), GetCurrentThreadId()));\r
449 \r
450                         ib_cq_comp( cq_tinfo );\r
451                         fzprint(("%s():%d:0x%x:0x%x: Done calling ib_cq_comp().\n", __FUNCTION__,\r
452                                          __LINE__, GetCurrentProcessId(), GetCurrentThreadId()));\r
453                 }\r
454 \r
455         } while( !cq_tinfo->ib_cq_thread_exit_wanted );\r
456 \r
457         cl_status = cl_waitobj_destroy( cq_tinfo->cq_waitobj );\r
458         if( cl_status != CL_SUCCESS )\r
459         {\r
460                 IBSP_ERROR(\r
461                         ("cl_waitobj_destroy() returned %s\n", CL_STATUS_MSG(cl_status)) );\r
462         }\r
463         HeapFree( g_ibsp.heap, 0, cq_tinfo );\r
464 \r
465         /* No special exit code, even on errors. */\r
466         IBSP_EXIT( IBSP_DBG_HW );\r
467         ExitThread( 0 );\r
468 }\r
469 \r
470 \r
471 /* Called with the HCA's CQ lock held. */\r
472 static struct cq_thread_info *\r
473 ib_alloc_cq_tinfo(\r
474                                         struct ibsp_hca                         *hca )\r
475 {\r
476         struct cq_thread_info *cq_tinfo = NULL;\r
477         ib_cq_create_t cq_create;\r
478         ib_api_status_t status;\r
479         cl_status_t cl_status;\r
480 \r
481         IBSP_ENTER( IBSP_DBG_HW );\r
482 \r
483         cq_tinfo = HeapAlloc(\r
484                 g_ibsp.heap, HEAP_ZERO_MEMORY, sizeof(struct cq_thread_info) );\r
485 \r
486         if( !cq_tinfo )\r
487         {\r
488                 IBSP_ERROR_EXIT( ("HeapAlloc() Failed.\n") );\r
489                 return NULL;\r
490         }\r
491 \r
492         cl_status = cl_waitobj_create( FALSE, &cq_tinfo->cq_waitobj );\r
493         if( cl_status != CL_SUCCESS )\r
494         {\r
495                 cq_tinfo->cq_waitobj = NULL;\r
496                 ib_destroy_cq_tinfo( cq_tinfo );\r
497                 IBSP_ERROR_EXIT(\r
498                         ("cl_waitobj_create() returned %s\n", CL_STATUS_MSG(cl_status)) );\r
499                 return NULL;\r
500         }\r
501 \r
502         cq_tinfo->hca = hca;\r
503         cq_tinfo->ib_cq_thread_exit_wanted = FALSE;\r
504 \r
505         /* Create a cleanup thread */\r
506         cq_tinfo->ib_cq_thread = CreateThread( NULL, 0, ib_cq_thread, cq_tinfo, 0, (LPDWORD)&cq_tinfo->ib_cq_thread_id );\r
507 \r
508         if( cq_tinfo->ib_cq_thread == NULL )\r
509         {\r
510                 ib_destroy_cq_tinfo( cq_tinfo );\r
511                 IBSP_ERROR_EXIT( ("CreateThread failed (%d)", GetLastError()) );\r
512                 return NULL;\r
513         }\r
514 \r
515         STAT_INC( thread_num );\r
516 \r
517         /* Completion queue */\r
518         cq_create.size = IB_CQ_SIZE;\r
519 \r
520         cq_create.pfn_comp_cb = NULL;\r
521         cq_create.h_wait_obj = cq_tinfo->cq_waitobj;\r
522 \r
523         status = ib_create_cq( hca->hca_handle, &cq_create, cq_tinfo,   /* context */\r
524                 NULL,   /* async handler */\r
525                 &cq_tinfo->cq );\r
526         if( status )\r
527         {\r
528                 ib_destroy_cq_tinfo( cq_tinfo );\r
529                 IBSP_ERROR_EXIT(\r
530                         ("ib_create_cq returned %s\n", ib_get_err_str( status )) );\r
531                 return NULL;\r
532         }\r
533 \r
534         STAT_INC( cq_num );\r
535 \r
536         status = ib_rearm_cq( cq_tinfo->cq, FALSE );\r
537         if( status )\r
538         {\r
539                 ib_destroy_cq_tinfo( cq_tinfo );\r
540                 IBSP_ERROR_EXIT(\r
541                         ("ib_rearm_cq returned %s\n", ib_get_err_str( status )) );\r
542                 return NULL;\r
543         }\r
544 \r
545         cq_tinfo->cqe_size = IB_CQ_SIZE;\r
546 \r
547         if( hca->cq_tinfo )\r
548         {\r
549                 __cl_primitive_insert(\r
550                         &hca->cq_tinfo->list_item, &cq_tinfo->list_item );\r
551         }\r
552         else\r
553         {\r
554                 /* Setup the list entry to point to itself. */\r
555                 cq_tinfo->list_item.p_next = &cq_tinfo->list_item;\r
556                 cq_tinfo->list_item.p_prev = &cq_tinfo->list_item;\r
557         }\r
558 \r
559         /* Upon allocation, the new CQ becomes the primary. */\r
560         hca->cq_tinfo = cq_tinfo;\r
561 \r
562         IBSP_EXIT( IBSP_DBG_HW );\r
563         return (cq_tinfo);\r
564 }\r
565 \r
566 \r
567 void\r
568 ib_destroy_cq_tinfo(\r
569                                         struct cq_thread_info           *cq_tinfo )\r
570 {\r
571         ib_wc_t wclist;\r
572         ib_wc_t *free_wclist;\r
573         ib_wc_t *done_wclist;\r
574         ib_api_status_t status;\r
575         HANDLE h_cq_thread;\r
576 \r
577         IBSP_ENTER( IBSP_DBG_HW );\r
578 \r
579         CL_ASSERT( cq_tinfo );\r
580         CL_ASSERT( cq_tinfo->qp_count == 0 );\r
581 \r
582         if( cq_tinfo->cq )\r
583         {\r
584                 wclist.p_next = NULL;\r
585                 free_wclist = &wclist;\r
586 \r
587                 while( ib_poll_cq(\r
588                         cq_tinfo->cq, &free_wclist, &done_wclist ) == IB_SUCCESS )\r
589                 {\r
590                         IBSP_TRACE1( IBSP_DBG_WQ,\r
591                                 ("free=%p, done=%p\n", free_wclist, done_wclist) );\r
592                 }\r
593 \r
594                 IBSP_TRACE4( IBSP_DBG_WQ, ("ib_destroy_cq() start..\n") );\r
595 \r
596                 /*\r
597                  * Called from cleanup thread, okay to block.\r
598                  */\r
599                 status = ib_destroy_cq( cq_tinfo->cq, ib_sync_destroy );\r
600                 if( status )\r
601                 {\r
602                         IBSP_ERROR(\r
603                                 ("ib_destroy_cq returned %s\n", ib_get_err_str( status )) );\r
604                 }\r
605                 else\r
606                 {\r
607                         IBSP_TRACE4( IBSP_DBG_WQ, ("ib_destroy_cq() finished.\n") );\r
608 \r
609                         cq_tinfo->cq = NULL;\r
610 \r
611                         STAT_DEC( cq_num );\r
612                 }\r
613         }\r
614 \r
615         if( cq_tinfo->ib_cq_thread )\r
616         {\r
617                 /* ib_cq_thread() will release the cq_tinfo before exit. Don't\r
618                    reference cq_tinfo after signaling  */\r
619                 h_cq_thread = cq_tinfo->ib_cq_thread;\r
620                 cq_tinfo->ib_cq_thread = NULL;\r
621 \r
622                 cq_tinfo->ib_cq_thread_exit_wanted = TRUE;\r
623                 cl_waitobj_signal( cq_tinfo->cq_waitobj );\r
624 \r
625                 /* Wait for ib_cq_thread to die, if we are not running on it */\r
626                 if( GetCurrentThreadId() != cq_tinfo->ib_cq_thread_id )\r
627                 {\r
628                         fzprint(("%s():%d:0x%x:0x%x: Waiting for ib_cq_thread=0x%x to die\n",\r
629                                          __FUNCTION__, __LINE__, GetCurrentProcessId(), GetCurrentThreadId(),\r
630                                          cq_tinfo->ib_cq_thread_id ));\r
631                         if( WaitForSingleObject( h_cq_thread, INFINITE ) != WAIT_OBJECT_0 )\r
632                         {\r
633                                 IBSP_ERROR( ("WaitForSingleObject failed\n") );\r
634                         }\r
635                         else\r
636                         {\r
637                                 STAT_DEC( thread_num );\r
638                         }\r
639                 }\r
640                 else\r
641                 {\r
642                         fzprint(("%s():%d:0x%x:0x%x: Currently on ib_cq_thread.\n", __FUNCTION__,\r
643                                          __LINE__, GetCurrentProcessId(), GetCurrentThreadId()));\r
644                         STAT_DEC( thread_num );\r
645                 }\r
646                 CloseHandle( h_cq_thread );\r
647         }\r
648         else\r
649         {\r
650                 /* There was no thread created, destroy cq_waitobj and\r
651                    free memory */\r
652                 if( cq_tinfo->cq_waitobj )\r
653                 {\r
654                         cl_waitobj_destroy( cq_tinfo->cq_waitobj );\r
655                         cq_tinfo->cq_waitobj = NULL;\r
656                 }\r
657                 HeapFree( g_ibsp.heap, 0, cq_tinfo );\r
658         }\r
659 \r
660         IBSP_EXIT( IBSP_DBG_HW );\r
661 }\r
662 \r
663 \r
664 static struct cq_thread_info *\r
665 ib_acquire_cq_tinfo(\r
666                                         struct ibsp_hca                         *hca )\r
667 {\r
668         struct cq_thread_info   *cq_tinfo = NULL;\r
669         uint32_t                                cqe_size;\r
670         ib_api_status_t                 status;\r
671 \r
672         IBSP_ENTER( IBSP_DBG_HW );\r
673 \r
674         cl_spinlock_acquire( &hca->cq_lock );\r
675 \r
676         if( !hca->cq_tinfo )\r
677         {\r
678                 cq_tinfo = ib_alloc_cq_tinfo( hca );\r
679                 if( !cq_tinfo )\r
680                 {\r
681                         IBSP_ERROR_EXIT( ("ib_alloc_cq_tinfo() failed\n") );\r
682                         cl_spinlock_release( &hca->cq_lock );\r
683                         return (NULL);\r
684                 }\r
685         }\r
686         else\r
687         {\r
688                 cq_tinfo = hca->cq_tinfo;\r
689         }\r
690 \r
691         CL_ASSERT( cq_tinfo != NULL );\r
692 \r
693         cqe_size = (cq_tinfo->qp_count + 1) * IB_CQ_SIZE;\r
694 \r
695         if( cq_tinfo->cqe_size < cqe_size )\r
696         {\r
697                 status = ib_modify_cq( cq_tinfo->cq, &cqe_size );\r
698                 switch( status )\r
699                 {\r
700                 case IB_INVALID_CQ_SIZE:\r
701                         cq_tinfo = ib_alloc_cq_tinfo( hca );\r
702                         if( !cq_tinfo )\r
703                                 break;\r
704 \r
705                         cq_tinfo->qp_count++;\r
706                         break;\r
707 \r
708                 case IB_SUCCESS:\r
709                         cq_tinfo->cqe_size = cqe_size;\r
710 \r
711                         cq_tinfo->qp_count++;\r
712 \r
713                         fzprint(("%s():%d:0x%x:0x%x: New cq size=%d.\n",\r
714                                          __FUNCTION__,\r
715                                          __LINE__, GetCurrentProcessId(),\r
716                                          GetCurrentThreadId(), cq_tinfo->cqe_size));\r
717                         break;\r
718 \r
719                 default:\r
720                         IBSP_ERROR_EXIT(\r
721                                 ("ib_modify_cq() returned %s\n", ib_get_err_str(status)) );\r
722                         cq_tinfo = NULL;\r
723                 }\r
724         }\r
725         else\r
726         {\r
727                 cq_tinfo->qp_count++;\r
728         }\r
729 \r
730         cl_spinlock_release( &hca->cq_lock );\r
731         IBSP_EXIT( IBSP_DBG_HW );\r
732         return (cq_tinfo);\r
733 }\r
734 \r
735 void\r
736 ib_release_cq_tinfo(\r
737                                         struct cq_thread_info           *cq_tinfo )\r
738 {\r
739         IBSP_ENTER( IBSP_DBG_HW );\r
740 \r
741         CL_ASSERT( cq_tinfo );\r
742         CL_ASSERT( cq_tinfo->hca );\r
743 \r
744         cl_spinlock_acquire( &cq_tinfo->hca->cq_lock );\r
745         /* If this CQ now has fewer QPs than the primary, make it the primary. */\r
746         if( --cq_tinfo->qp_count < cq_tinfo->hca->cq_tinfo->qp_count )\r
747                 cq_tinfo->hca->cq_tinfo = cq_tinfo;\r
748         cl_spinlock_release( &cq_tinfo->hca->cq_lock );\r
749 \r
750         IBSP_EXIT( IBSP_DBG_HW );\r
751 }\r
752 \r
753 \r
754 /* Release IB ressources. */\r
755 void\r
756 ib_release(void)\r
757 {\r
758         cl_fmap_item_t                  *p_item;\r
759 \r
760         IBSP_ENTER( IBSP_DBG_HW );\r
761 \r
762         if( g_ibsp.al_handle )\r
763         {\r
764                 cl_list_item_t *item;\r
765                 ib_api_status_t status;\r
766 \r
767                 unregister_pnp();\r
768 \r
769                 while( (item = cl_qlist_head( &g_ibsp.hca_list )) != cl_qlist_end( &g_ibsp.hca_list ) )\r
770                 {\r
771                         struct ibsp_hca *hca = PARENT_STRUCT(item, struct ibsp_hca, item);\r
772 \r
773                         pnp_ca_remove( hca );\r
774                 }\r
775 \r
776                 fzprint(("%s():%d:0x%x:0x%x: Calling ib_close_al...\n", __FUNCTION__,\r
777                                  __LINE__, GetCurrentProcessId(), GetCurrentThreadId()));\r
778 \r
779                 status = ib_close_al( g_ibsp.al_handle );\r
780 \r
781                 fzprint(("%s():%d:0x%x:0x%x: Done calling ib_close_al, status=%d.\n",\r
782                                  __FUNCTION__, __LINE__, GetCurrentProcessId(), GetCurrentThreadId(),\r
783                                  status));\r
784                 if( status != IB_SUCCESS )\r
785                 {\r
786                         IBSP_ERROR(\r
787                                 ("ib_close_al returned %s\n", ib_get_err_str( status )) );\r
788                 }\r
789                 else\r
790                 {\r
791                         IBSP_TRACE( IBSP_DBG_HW, ("ib_close_al success\n") );\r
792                         STAT_DEC( al_num );\r
793                 }\r
794                 g_ibsp.al_handle = NULL;\r
795         }\r
796 \r
797         for( p_item = cl_fmap_head( &g_ibsp.ip_map );\r
798                 p_item != cl_fmap_end( &g_ibsp.ip_map );\r
799                 p_item = cl_fmap_head( &g_ibsp.ip_map ) )\r
800         {\r
801                 cl_fmap_remove_item( &g_ibsp.ip_map, p_item );\r
802 \r
803                 HeapFree( g_ibsp.heap, 0,\r
804                         PARENT_STRUCT(p_item, struct ibsp_ip_addr, item) );\r
805         }\r
806 \r
807         IBSP_EXIT( IBSP_DBG_HW );\r
808 }\r
809 \r
810 \r
811 /* Initialize IB ressources. */\r
812 int\r
813 ibsp_initialize(void)\r
814 {\r
815         ib_api_status_t status;\r
816         int ret;\r
817 \r
818         IBSP_ENTER( IBSP_DBG_HW );\r
819 \r
820         CL_ASSERT( g_ibsp.al_handle == NULL );\r
821         CL_ASSERT( cl_qlist_count( &g_ibsp.hca_list ) == 0 );\r
822 \r
823         /* Open the IB library */\r
824         status = ib_open_al( &g_ibsp.al_handle );\r
825 \r
826         IBSP_TRACE( IBSP_DBG_HW, ("open is %d %p\n", status, g_ibsp.al_handle) );\r
827 \r
828         if( status != IB_SUCCESS )\r
829         {\r
830                 IBSP_ERROR( ("ib_open_al failed (%d)\n", status) );\r
831                 ret = WSAEPROVIDERFAILEDINIT;\r
832                 goto done;\r
833         }\r
834 \r
835         STAT_INC( al_num );\r
836 \r
837         /* Register for PNP events */\r
838         status = register_pnp();\r
839         if( status )\r
840         {\r
841                 IBSP_ERROR( ("register_pnp failed (%d)\n", status) );\r
842                 ret = WSAEPROVIDERFAILEDINIT;\r
843                 goto done;\r
844         }\r
845 \r
846         /* Populate IP list. */\r
847         update_all_ip_addrs();\r
848 \r
849         STAT_INC( thread_num );\r
850 \r
851         ret = 0;\r
852 done:\r
853         if( ret )\r
854         {\r
855                 /* Free up resources. */\r
856                 ib_release();\r
857         }\r
858 \r
859         IBSP_EXIT( IBSP_DBG_HW );\r
860 \r
861         return ret;\r
862 }\r
863 \r
864 \r
865 /* Destroys the infiniband ressources of a socket. */\r
866 void\r
867 ib_destroy_socket(\r
868         IN      OUT                     struct ibsp_socket_info         *socket_info )\r
869 {\r
870         ib_api_status_t status;\r
871 \r
872         IBSP_ENTER( IBSP_DBG_EP );\r
873 \r
874         if( socket_info->qp )\r
875         {\r
876                 cl_atomic_inc( &socket_info->ref_cnt );\r
877                 status = ib_destroy_qp( socket_info->qp, deref_socket_info );\r
878                 if( status != IB_SUCCESS )\r
879                 {\r
880                         IBSP_ERROR( ("ib_destroy_qp returned %s\n",\r
881                                 ib_get_err_str( status )) );\r
882                         deref_socket_info( socket_info );\r
883                 }\r
884 \r
885                 ib_release_cq_tinfo( socket_info->cq_tinfo );\r
886 \r
887                 socket_info->qp = NULL;\r
888         }\r
889 \r
890         IBSP_EXIT( IBSP_DBG_EP );\r
891 }\r
892 \r
893 \r
894 /*\r
895  * Creates the necessary IB ressources for a socket\r
896  */\r
897 int\r
898 ib_create_socket(\r
899         IN      OUT                     struct ibsp_socket_info         *socket_info)\r
900 {\r
901         ib_qp_create_t                  qp_create;\r
902         ib_api_status_t                 status;\r
903         ib_qp_attr_t                    qp_attr;\r
904 \r
905         IBSP_ENTER( IBSP_DBG_EP );\r
906 \r
907         CL_ASSERT( socket_info != NULL );\r
908         CL_ASSERT( socket_info->port != NULL );\r
909         CL_ASSERT( socket_info->qp == NULL );\r
910 \r
911         socket_info->hca_pd = socket_info->port->hca->pd;\r
912 \r
913         /* Get the completion queue and thread info for this socket */\r
914         socket_info->cq_tinfo = ib_acquire_cq_tinfo( socket_info->port->hca );\r
915         if( !socket_info->cq_tinfo )\r
916         {\r
917                 IBSP_ERROR_EXIT( ("ib_acquire_cq_tinfo failed\n") );\r
918                 return WSAENOBUFS;\r
919         }\r
920 \r
921         /* Queue pair */\r
922         qp_create.qp_type = IB_QPT_RELIABLE_CONN;\r
923         qp_create.sq_depth = QP_ATTRIB_SQ_DEPTH;\r
924         qp_create.rq_depth = QP_ATTRIB_RQ_DEPTH;\r
925         qp_create.sq_sge = QP_ATTRIB_SQ_SGE;\r
926         qp_create.rq_sge = 1;\r
927         qp_create.h_rq_cq = socket_info->cq_tinfo->cq;\r
928         qp_create.h_sq_cq = socket_info->cq_tinfo->cq;\r
929         qp_create.sq_signaled = TRUE;\r
930 \r
931         status = ib_create_qp( socket_info->hca_pd, &qp_create, socket_info,    /* context */\r
932                 NULL,   /* async handler */\r
933                 &socket_info->qp );\r
934         if( status )\r
935         {\r
936                 ib_release_cq_tinfo( socket_info->cq_tinfo );\r
937                 IBSP_ERROR_EXIT(\r
938                         ("ib_create_qp returned %s\n", ib_get_err_str( status )) );\r
939                 return WSAENOBUFS;\r
940         }\r
941 \r
942         status = ib_query_qp( socket_info->qp, &qp_attr );\r
943         if( status == IB_SUCCESS )\r
944         {\r
945                 socket_info->max_inline = min( g_max_inline, qp_attr.sq_max_inline );\r
946         }\r
947         else\r
948         {\r
949                 IBSP_ERROR( ("ib_query_qp returned %s\n", ib_get_err_str( status )) );\r
950                 socket_info->max_inline = 0;\r
951         }\r
952 \r
953         STAT_INC( qp_num );\r
954 \r
955         IBSP_EXIT( IBSP_DBG_EP );\r
956         return 0;\r
957 }\r
958 \r
959 \r
960 void\r
961 wait_cq_drain(\r
962         IN      OUT                     struct ibsp_socket_info         *socket_info )\r
963 {\r
964         IBSP_ENTER( IBSP_DBG_EP );\r
965 \r
966         if( socket_info->cq_tinfo == NULL )\r
967         {\r
968                 IBSP_EXIT( IBSP_DBG_EP );\r
969                 return;\r
970         }\r
971 \r
972         /* Wait for the QP to be drained. */\r
973         while( socket_info->send_cnt || socket_info->recv_cnt )\r
974         {\r
975                 fzprint(("%s():%d:0x%x:0x%x: socket=0x%p wr_list_count=%d qp state=%d\n",\r
976                                  __FUNCTION__, __LINE__, GetCurrentProcessId(), GetCurrentThreadId(),\r
977                                  socket_info, cl_qlist_count(&socket_info->wr_list)));\r
978 \r
979                 Sleep(100);\r
980         }\r
981 \r
982         IBSP_EXIT( IBSP_DBG_EP );\r
983 }\r
984 \r
985 \r
986 void\r
987 ibsp_dup_overlap_abort(\r
988         IN      OUT                     struct ibsp_socket_info         *socket_info )\r
989 {\r
990         LPWSAOVERLAPPED lpOverlapped = NULL;\r
991         int error;\r
992         int ret;\r
993         uint8_t                         idx;\r
994 \r
995         IBSP_ENTER( IBSP_DBG_EP );\r
996         CL_ASSERT( !socket_info->send_cnt && !socket_info->recv_cnt );\r
997 \r
998         /* Browse the list of all posted overlapped structures\r
999          * to mark them as aborted. */\r
1000         idx = socket_info->dup_idx - (uint8_t)socket_info->dup_cnt;\r
1001         if( idx >= QP_ATTRIB_RQ_DEPTH )\r
1002                 idx += QP_ATTRIB_RQ_DEPTH;\r
1003 \r
1004         while( socket_info->dup_cnt )\r
1005         {\r
1006                 lpOverlapped = socket_info->dup_wr[idx].wr.lpOverlapped;\r
1007 \r
1008                 fzprint(("%s():%d:0x%x:0x%x: socket=0x%p wr=0x%p overlapped=0x%p Internal=%d InternalHigh=%d hEvent=%d\n",\r
1009                         __FUNCTION__, __LINE__, GetCurrentProcessId(), GetCurrentThreadId(), socket_info, &socket_info->dup_wr[idx], lpOverlapped, lpOverlapped->Internal, lpOverlapped->InternalHigh, lpOverlapped->hEvent));\r
1010 \r
1011                 lpOverlapped->OffsetHigh = WSAECONNABORTED;\r
1012                 lpOverlapped->InternalHigh = 0;\r
1013 \r
1014                 if( ((uintptr_t) lpOverlapped->hEvent) & 0x00000001 )\r
1015                 {\r
1016                         /* Indicate this operation is complete. The switch will poll\r
1017                          * with calls to WSPGetOverlappedResult(). */\r
1018 #ifdef _DEBUG_\r
1019                         cl_atomic_dec(&g_ibsp.overlap_h1_comp_count);\r
1020 \r
1021                         fzprint(("%s():%d:0x%x:0x%x: ov=0x%p h0=%d h1=%d h1_c=%d send=%d recv=%d\n",\r
1022                                          __FUNCTION__, __LINE__, GetCurrentProcessId(),\r
1023                                          GetCurrentThreadId(), lpOverlapped,\r
1024                                          g_ibsp.overlap_h0_count, g_ibsp.overlap_h1_count,\r
1025                                          g_ibsp.overlap_h1_comp_count, g_ibsp.send_count, g_ibsp.recv_count));\r
1026 #endif\r
1027 \r
1028                         CL_TRACE(IBSP_DBG_WQ, gdbg_lvl,\r
1029                                          ("%s: set internal overlapped=0x%p Internal=%d OffsetHigh=%d\n",\r
1030                                           __FUNCTION__, lpOverlapped, lpOverlapped->Internal,\r
1031                                           lpOverlapped->OffsetHigh));\r
1032 \r
1033                         lpOverlapped->Internal = 0;\r
1034                 }\r
1035                 else\r
1036                 {\r
1037 #ifdef _DEBUG_\r
1038                         cl_atomic_dec(&g_ibsp.overlap_h0_count);\r
1039 \r
1040 \r
1041                         fzprint(("%s():%d:0x%x:0x%x: ov=0x%p h0=%d h1=%d h1_c=%d send=%d recv=%d\n",\r
1042                                          __FUNCTION__, __LINE__, GetCurrentProcessId(),\r
1043                                          GetCurrentThreadId(), lpOverlapped,\r
1044                                          g_ibsp.overlap_h0_count, g_ibsp.overlap_h1_count,\r
1045                                          g_ibsp.overlap_h1_comp_count, g_ibsp.send_count, g_ibsp.recv_count));\r
1046 #endif\r
1047                         CL_TRACE(IBSP_DBG_WQ, gdbg_lvl,\r
1048                                          ("%s: calls lpWPUCompleteOverlappedRequest, overlapped=0x%p OffsetHigh=%d InternalHigh=%d hEvent=%d\n",\r
1049                                           __FUNCTION__, lpOverlapped, lpOverlapped->OffsetHigh,\r
1050                                           lpOverlapped->InternalHigh, lpOverlapped->hEvent));\r
1051 \r
1052                         ret = g_ibsp.up_call_table.lpWPUCompleteOverlappedRequest\r
1053                                 (socket_info->switch_socket,\r
1054                                  lpOverlapped,\r
1055                                  lpOverlapped->OffsetHigh, (DWORD) lpOverlapped->InternalHigh, &error);\r
1056 \r
1057                         if( ret != 0 )\r
1058                         {\r
1059                                 CL_ERROR(IBSP_DBG_EP, gdbg_lvl,\r
1060                                                  ("lpWPUCompleteOverlappedRequest failed with %d/%d\n", ret,\r
1061                                                   error));\r
1062                         }\r
1063                 }\r
1064                 cl_atomic_dec( &socket_info->dup_cnt );\r
1065         }\r
1066 \r
1067         IBSP_EXIT( IBSP_DBG_EP );\r
1068 }\r
1069 \r
1070 \r
1071 /* Closes a connection and release its ressources. */\r
1072 void\r
1073 shutdown_and_destroy_socket_info(\r
1074         IN      OUT                     struct ibsp_socket_info         *socket_info )\r
1075 {\r
1076         enum ibsp_socket_state  old_state;\r
1077 \r
1078         IBSP_ENTER( IBSP_DBG_EP );\r
1079 \r
1080         cl_spinlock_acquire( &socket_info->mutex );\r
1081         old_state = socket_info->socket_state;\r
1082         IBSP_CHANGE_SOCKET_STATE( socket_info, IBSP_CLOSED );\r
1083         cl_spinlock_release( &socket_info->mutex );\r
1084 \r
1085         if( socket_info->duplicate.mmap_handle )\r
1086         {\r
1087                 CloseHandle( socket_info->duplicate.mmap_handle );\r
1088                 socket_info->duplicate.mmap_handle = NULL;\r
1089         }\r
1090 \r
1091         if( socket_info->listen.handle )\r
1092         {\r
1093                 /* Stop listening and reject queued connections. */\r
1094                 ib_listen_cancel( socket_info );\r
1095         }\r
1096 \r
1097         cl_spinlock_acquire( &g_ibsp.socket_info_mutex );\r
1098         cl_qlist_remove_item( &g_ibsp.socket_info_list, &socket_info->item );\r
1099 \r
1100         switch( old_state )\r
1101         {\r
1102         case IBSP_CREATE:\r
1103         case IBSP_LISTEN:\r
1104                 /* Nothing to do. */\r
1105                 break;\r
1106 \r
1107         case IBSP_CONNECTED:\r
1108                 {\r
1109                         struct disconnect_reason reason;\r
1110 \r
1111                         memset( &reason, 0, sizeof(reason) );\r
1112                         reason.type = DISC_SHUTDOWN;\r
1113                         ib_disconnect( socket_info, &reason );\r
1114                 }\r
1115                 /* Fall through. */\r
1116 \r
1117         case IBSP_CONNECT:\r
1118         case IBSP_DISCONNECTED:\r
1119                 /* We changed the state - remove from connection map. */\r
1120                 CL_ASSERT( socket_info->conn_item.p_map );\r
1121                 cl_rbmap_remove_item( &g_ibsp.conn_map, &socket_info->conn_item );\r
1122                 break;\r
1123         }\r
1124         cl_spinlock_release( &g_ibsp.socket_info_mutex );\r
1125 \r
1126         /* Flush all completions. */\r
1127         if( socket_info->dup_cnt )\r
1128                 ibsp_dup_overlap_abort( socket_info );\r
1129 \r
1130         while( socket_info->send_cnt || socket_info->recv_cnt )\r
1131                 ib_cq_comp( socket_info->cq_tinfo );\r
1132 \r
1133         ibsp_dereg_socket( socket_info );\r
1134 \r
1135         ib_destroy_socket( socket_info );\r
1136 \r
1137         /* Release the initial reference and clean up. */\r
1138         deref_socket_info( socket_info );\r
1139 \r
1140         IBSP_EXIT( IBSP_DBG_EP );\r
1141 }\r
1142 \r
1143 \r
1144 boolean_t\r
1145 ibsp_conn_insert(\r
1146         IN                              struct ibsp_socket_info         *s )\r
1147 {\r
1148         struct ibsp_socket_info         *p_sock;\r
1149         cl_rbmap_item_t                         *p_item, *p_insert_at;\r
1150         boolean_t                                       left = TRUE;\r
1151 \r
1152         p_item = cl_rbmap_root( &g_ibsp.conn_map );\r
1153         p_insert_at = p_item;\r
1154 \r
1155         cl_spinlock_acquire( &g_ibsp.socket_info_mutex );\r
1156         CL_ASSERT( !s->conn_item.p_map );\r
1157         while( p_item != cl_rbmap_end( &g_ibsp.conn_map ) )\r
1158         {\r
1159                 p_insert_at = p_item;\r
1160                 p_sock = PARENT_STRUCT( p_item, struct ibsp_socket_info, conn_item );\r
1161                 if( p_sock->local_addr.sin_family < s->local_addr.sin_family )\r
1162                         p_item = cl_rbmap_left( p_item ), left = TRUE;\r
1163                 else if( p_sock->local_addr.sin_family > s->local_addr.sin_family )\r
1164                         p_item = cl_rbmap_right( p_item ), left = FALSE;\r
1165                 else if( p_sock->local_addr.sin_addr.S_un.S_addr < s->local_addr.sin_addr.S_un.S_addr )\r
1166                         p_item = cl_rbmap_left( p_item ), left = TRUE;\r
1167                 else if( p_sock->local_addr.sin_addr.S_un.S_addr > s->local_addr.sin_addr.S_un.S_addr )\r
1168                         p_item = cl_rbmap_right( p_item ), left = FALSE;\r
1169                 else if( p_sock->local_addr.sin_port < s->local_addr.sin_port )\r
1170                         p_item = cl_rbmap_left( p_item ), left = TRUE;\r
1171                 else if( p_sock->local_addr.sin_port > s->local_addr.sin_port )\r
1172                         p_item = cl_rbmap_right( p_item ), left = FALSE;\r
1173                 else if( p_sock->peer_addr.sin_family < s->peer_addr.sin_family )\r
1174                         p_item = cl_rbmap_left( p_item ), left = TRUE;\r
1175                 else if( p_sock->peer_addr.sin_family > s->peer_addr.sin_family )\r
1176                         p_item = cl_rbmap_right( p_item ), left = FALSE;\r
1177                 else if( p_sock->peer_addr.sin_addr.S_un.S_addr < s->peer_addr.sin_addr.S_un.S_addr )\r
1178                         p_item = cl_rbmap_left( p_item ), left = TRUE;\r
1179                 else if( p_sock->peer_addr.sin_addr.S_un.S_addr > s->peer_addr.sin_addr.S_un.S_addr )\r
1180                         p_item = cl_rbmap_right( p_item ), left = FALSE;\r
1181                 else if( p_sock->peer_addr.sin_port < s->peer_addr.sin_port )\r
1182                         p_item = cl_rbmap_left( p_item ), left = TRUE;\r
1183                 else if( p_sock->peer_addr.sin_port > s->peer_addr.sin_port )\r
1184                         p_item = cl_rbmap_right( p_item ), left = FALSE;\r
1185                 else\r
1186                         goto done;\r
1187         }\r
1188 \r
1189         cl_rbmap_insert( &g_ibsp.conn_map, p_insert_at, &s->conn_item, left );\r
1190 \r
1191 done:\r
1192         cl_spinlock_release( &g_ibsp.socket_info_mutex );\r
1193         return p_item == cl_rbmap_end( &g_ibsp.conn_map );\r
1194 }\r
1195 \r
1196 \r
1197 void\r
1198 ibsp_conn_remove(\r
1199         IN                              struct ibsp_socket_info         *s )\r
1200 {\r
1201         cl_spinlock_acquire( &g_ibsp.socket_info_mutex );\r
1202         CL_ASSERT( s->conn_item.p_map );\r
1203         cl_rbmap_remove_item( &g_ibsp.conn_map, &s->conn_item );\r
1204         cl_spinlock_release( &g_ibsp.socket_info_mutex );\r
1205 }\r